aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x.download_from_Kalray.sh15
-rw-r--r--.gitignore16
-rw-r--r--.gitlab-ci.yml351
-rw-r--r--.gitmodules0
-rw-r--r--INSTALL.md74
-rw-r--r--INSTALL_CROSS.md5
-rw-r--r--Makefile85
-rw-r--r--Makefile.extr10
-rw-r--r--PROFILING.md34
-rw-r--r--README.md23
-rw-r--r--README_Kalray.md37
-rw-r--r--aarch64/Archi.v4
-rw-r--r--aarch64/Asm.v589
-rw-r--r--aarch64/Asmblock.v1049
-rw-r--r--aarch64/Asmblockdeps.v2688
-rw-r--r--aarch64/Asmblockgen.v1252
-rw-r--r--aarch64/Asmblockgenproof.v1547
-rw-r--r--aarch64/Asmblockgenproof0.v885
-rw-r--r--aarch64/Asmblockgenproof1.v1955
-rw-r--r--aarch64/Asmblockprops.v119
-rw-r--r--aarch64/Asmexpand.ml126
-rw-r--r--aarch64/Asmgen.v1451
-rw-r--r--aarch64/Asmgenproof.v3033
-rw-r--r--aarch64/BTL_SEsimplify.v41
-rw-r--r--aarch64/CSE2deps.v35
-rw-r--r--aarch64/CSE2depsproof.v209
-rw-r--r--aarch64/ConstpropOpproof.v121
-rw-r--r--aarch64/DuplicateOpcodeHeuristic.ml41
-rw-r--r--aarch64/ExpansionOracle.ml15
-rw-r--r--aarch64/Machregs.v1
-rw-r--r--aarch64/Machregsaux.ml8
-rw-r--r--aarch64/Machregsaux.mli20
-rw-r--r--aarch64/Op.v256
-rw-r--r--aarch64/OpWeights.ml353
-rw-r--r--aarch64/OpWeightsAsm.ml165
-rw-r--r--aarch64/PeepholeOracle.ml624
-rw-r--r--aarch64/PostpassScheduling.v146
-rw-r--r--aarch64/PostpassSchedulingOracle.ml674
-rw-r--r--aarch64/PostpassSchedulingproof.v482
-rw-r--r--aarch64/PrepassSchedulingOracle.ml297
-rw-r--r--aarch64/PrepassSchedulingOracleDeps.ml17
-rw-r--r--aarch64/SelectLongproof.v33
-rw-r--r--aarch64/SelectOp.vp15
-rw-r--r--aarch64/SelectOpproof.v61
-rw-r--r--aarch64/TargetPrinter.ml108
-rw-r--r--aarch64/ValueAOp.v24
-rw-r--r--aarch64/extractionMachdep.v8
-rw-r--r--arm/Archi.v2
-rw-r--r--arm/AsmToJSON.ml1
-rw-r--r--arm/Asmexpand.ml2
-rw-r--r--arm/Asmgen.v16
-rw-r--r--arm/Asmgenproof.v7
-rw-r--r--arm/Asmgenproof1.v32
l---------arm/BTL_SEsimplify.v1
-rw-r--r--arm/CSE2deps.v35
-rw-r--r--arm/CSE2depsproof.v211
-rw-r--r--arm/Constantexpand.ml1
-rw-r--r--arm/DuplicateOpcodeHeuristic.ml36
l---------arm/ExpansionOracle.ml1
-rw-r--r--arm/Machregs.v1
-rw-r--r--arm/Machregsaux.ml5
-rw-r--r--arm/Machregsaux.mli2
-rw-r--r--arm/Op.v109
l---------arm/PrepassSchedulingOracle.ml1
-rw-r--r--arm/SelectLong.vp2
-rw-r--r--arm/SelectLongproof.v1
-rw-r--r--arm/SelectOp.vp9
-rw-r--r--arm/SelectOpproof.v28
-rw-r--r--arm/TargetPrinter.ml57
-rw-r--r--backend/Allnontrap.v38
-rw-r--r--backend/Allnontrapproof.v227
-rw-r--r--backend/Allocation.v22
-rw-r--r--backend/Allocationproof.v (renamed from backend/Allocproof.v)121
-rw-r--r--backend/Asmaux.v19
-rw-r--r--backend/Asmexpandaux.ml4
-rw-r--r--backend/Bounds.v6
-rw-r--r--backend/CSE.v18
-rw-r--r--backend/CSE2.v413
-rw-r--r--backend/CSE2proof.v1755
-rw-r--r--backend/CSE3.v151
-rw-r--r--backend/CSE3analysis.v566
-rw-r--r--backend/CSE3analysisaux.ml319
-rw-r--r--backend/CSE3analysisproof.v1421
-rw-r--r--backend/CSE3proof.v1221
-rw-r--r--backend/CSEdomain.v13
-rw-r--r--backend/CSEproof.v170
-rw-r--r--backend/CleanupLabelsproof.v12
-rw-r--r--backend/Cminor.v2
-rw-r--r--backend/CminorSel.v8
-rw-r--r--backend/Cminortyping.v1
-rw-r--r--backend/Constprop.v10
-rw-r--r--backend/Constpropproof.v59
-rw-r--r--backend/Deadcode.v8
-rw-r--r--backend/Deadcodeproof.v75
-rw-r--r--backend/Debugvar.v2
-rw-r--r--backend/Debugvarproof.v16
-rw-r--r--backend/Duplicate.v232
-rw-r--r--backend/Duplicateaux.ml1137
-rw-r--r--backend/Duplicatepasses.v58
-rw-r--r--backend/Duplicateproof.v542
-rw-r--r--backend/FirstNop.v30
-rw-r--r--backend/FirstNopproof.v285
-rw-r--r--backend/ForwardMoves.v345
-rw-r--r--backend/ForwardMovesproof.v813
-rw-r--r--backend/IRC.ml7
-rw-r--r--backend/IRC.mli1
-rw-r--r--backend/Inject.v134
-rw-r--r--backend/Injectproof.v1806
-rw-r--r--backend/Inlining.v8
-rw-r--r--backend/Inliningaux.ml11
-rw-r--r--backend/Inliningproof.v54
-rw-r--r--backend/Inliningspec.v12
-rw-r--r--backend/JsonAST.ml2
-rw-r--r--backend/KillUselessMoves.v40
-rw-r--r--backend/KillUselessMovesproof.v361
-rw-r--r--backend/LICM.v21
-rw-r--r--backend/LICMaux.ml332
-rw-r--r--backend/LICMproof.v39
-rw-r--r--backend/LTL.v27
-rw-r--r--backend/LTLTunneling.v167
-rw-r--r--backend/LTLTunnelingaux.ml109
-rw-r--r--backend/LTLTunnelingproof.v666
-rw-r--r--backend/Linear.v23
-rw-r--r--backend/Linearize.v6
-rw-r--r--backend/Linearizeaux.ml81
-rw-r--r--backend/Linearizeproof.v48
-rw-r--r--backend/Lineartyping.v20
-rw-r--r--backend/Liveness.v4
-rw-r--r--backend/Mach.v19
-rw-r--r--backend/OpHelpers.v54
-rw-r--r--backend/OpHelpersproof.v90
-rw-r--r--backend/PrintAsm.ml2
-rw-r--r--backend/PrintAsmaux.ml85
-rw-r--r--backend/PrintCminor.ml2
-rw-r--r--backend/PrintLTL.ml16
-rw-r--r--backend/PrintMach.ml5
-rw-r--r--backend/PrintRTL.ml12
-rw-r--r--backend/PrintXTL.ml9
-rw-r--r--backend/Profiling.v77
-rw-r--r--backend/ProfilingExploit.v42
-rw-r--r--backend/ProfilingExploitproof.v236
-rw-r--r--backend/Profilingaux.ml85
-rw-r--r--backend/Profilingproof.v704
-rw-r--r--backend/RTL.v55
-rw-r--r--backend/RTLTunneling.v121
-rw-r--r--backend/RTLTunnelingaux.ml112
-rw-r--r--backend/RTLTunnelingproof.v609
-rw-r--r--backend/RTLcommonaux.ml105
-rw-r--r--backend/RTLgen.v6
-rw-r--r--backend/RTLgenaux.ml2
-rw-r--r--backend/RTLgenproof.v4
-rw-r--r--backend/RTLgenspec.v8
-rw-r--r--backend/RTLtyping.v32
-rw-r--r--backend/Regalloc.ml48
-rw-r--r--backend/Renumber.v4
-rw-r--r--backend/Renumberproof.v12
-rw-r--r--backend/SelectDiv.vp39
-rw-r--r--backend/SelectDivproof.v31
-rw-r--r--backend/Selection.v57
-rw-r--r--backend/Selectionaux.ml9
-rw-r--r--backend/Selectionproof.v120
-rw-r--r--backend/SplitLong.vp36
-rw-r--r--backend/SplitLongproof.v25
-rw-r--r--backend/Splitting.ml8
-rw-r--r--backend/Stacking.v4
-rw-r--r--backend/Stackingproof.v55
-rw-r--r--backend/Tailcallproof.v37
-rw-r--r--backend/Tunneling.v192
-rw-r--r--backend/Tunnelinglibs.ml272
-rw-r--r--backend/Tunnelingproof.v714
-rw-r--r--backend/Unusedglob.v6
-rw-r--r--backend/Unusedglobproof.v36
-rw-r--r--backend/ValueAnalysis.v31
-rw-r--r--backend/ValueDomain.v484
-rw-r--r--backend/XTL.ml10
-rw-r--r--backend/XTL.mli4
-rw-r--r--cfrontend/C2C.ml159
-rw-r--r--cfrontend/Cexec.v9
-rw-r--r--cfrontend/Cminorgenproof.v1
-rw-r--r--cfrontend/Cop.v13
-rw-r--r--cfrontend/Cshmgen.v6
-rw-r--r--cfrontend/Cshmgenproof.v38
-rw-r--r--cfrontend/Ctyping.v3
-rw-r--r--cfrontend/PrintClight.ml1
-rw-r--r--cfrontend/PrintCsyntax.ml2
-rw-r--r--common/AST.v43
-rw-r--r--common/DebugPrint.ml144
-rw-r--r--common/Events.v66
-rw-r--r--common/Memdata.v8
-rw-r--r--common/Memory.v156
-rw-r--r--common/PrintAST.ml15
-rw-r--r--common/Sections.ml25
-rw-r--r--common/Sections.mli4
-rw-r--r--common/Switchaux.ml3
-rw-r--r--common/Values.v212
-rw-r--r--compcert_build_env.dockerfile6
-rw-r--r--compcert_kvx.dockerfile19
-rw-r--r--compcert_kvx_pruned.dockerfile5
-rwxr-xr-xconfig_aarch64.sh1
-rwxr-xr-xconfig_arm.sh1
-rwxr-xr-xconfig_armhf.sh1
-rwxr-xr-xconfig_ia32.sh1
-rwxr-xr-xconfig_kvx.sh1
-rwxr-xr-xconfig_kvx_elf.sh1
-rwxr-xr-xconfig_macos_x86_64.sh1
-rwxr-xr-xconfig_ppc.sh1
-rwxr-xr-xconfig_ppc64.sh1
-rwxr-xr-xconfig_rv32.sh1
-rwxr-xr-xconfig_rv64.sh1
-rwxr-xr-xconfig_simple.sh11
-rwxr-xr-xconfig_x86_64.sh1
-rwxr-xr-xconfigure79
-rw-r--r--cparser/C.mli3
-rw-r--r--cparser/Cabs.v2
-rw-r--r--cparser/Ceval.ml4
-rw-r--r--cparser/Cleanup.ml8
-rw-r--r--cparser/Cprint.ml3
-rw-r--r--cparser/Elab.ml36
-rw-r--r--cparser/Lexer.mll2
-rw-r--r--cparser/Machine.ml52
-rw-r--r--cparser/Machine.mli5
-rw-r--r--cparser/Parser.vy4
-rw-r--r--cparser/Rename.ml7
-rw-r--r--cparser/StructPassing.ml11
-rw-r--r--cparser/deLexer.ml1
-rw-r--r--cparser/pre_parser.mly3
-rw-r--r--doc/index-kvx.html429
-rw-r--r--doc/index.html6
-rw-r--r--driver/Clflags.ml52
-rw-r--r--driver/Compiler.vexpand (renamed from driver/Compiler.v)152
-rw-r--r--driver/Compopts.v64
-rw-r--r--driver/Configuration.ml3
-rw-r--r--driver/Configuration.mli3
-rw-r--r--driver/Driver.ml96
-rw-r--r--driver/Frontend.ml5
-rw-r--r--export/ExportBase.ml1
-rw-r--r--export/ExportClight.ml1
-rw-r--r--export/ExportCsyntax.ml1
-rw-r--r--exportclight/ExportClight.ml559
-rw-r--r--extraction/debug/Asmgen.ml126
-rw-r--r--extraction/extraction.vexpand (renamed from extraction/extraction.v)78
-rw-r--r--kvx/Archi.v80
-rw-r--r--kvx/Asm.v758
-rw-r--r--kvx/AsmToJSON.ml23
-rw-r--r--kvx/Asmaux.v19
-rw-r--r--kvx/Asmblock.v394
-rw-r--r--kvx/Asmblockdeps.v1845
-rw-r--r--kvx/Asmblockgen.v1211
-rw-r--r--kvx/Asmblockgenproof.v1808
-rw-r--r--kvx/Asmblockgenproof0.v982
-rw-r--r--kvx/Asmblockgenproof1.v2500
-rw-r--r--kvx/Asmblockprops.v357
-rw-r--r--kvx/Asmexpand.ml642
-rw-r--r--kvx/Asmgen.v41
-rw-r--r--kvx/Asmgenproof.v96
-rw-r--r--kvx/Asmvliw.v1729
l---------kvx/BTL_SEsimplify.v1
-rw-r--r--kvx/Builtins1.v61
-rw-r--r--kvx/CBuiltins.ml145
-rw-r--r--kvx/CSE2deps.v35
-rw-r--r--kvx/CSE2depsproof.v146
-rw-r--r--kvx/Chunks.v36
-rw-r--r--kvx/CombineOp.v141
-rw-r--r--kvx/CombineOpproof.v176
-rw-r--r--kvx/ConstpropOp.vp312
-rw-r--r--kvx/ConstpropOpproof.v749
-rw-r--r--kvx/Conventions1.v431
-rw-r--r--kvx/DecBoolOps.v30
-rw-r--r--kvx/DuplicateOpcodeHeuristic.ml41
l---------kvx/ExpansionOracle.ml1
-rw-r--r--kvx/ExtFloats.v54
-rw-r--r--kvx/ExtValues.v756
-rw-r--r--kvx/Machregs.v245
-rw-r--r--kvx/Machregsaux.ml35
-rw-r--r--kvx/Machregsaux.mli20
-rw-r--r--kvx/NeedOp.v415
-rw-r--r--kvx/Op.v2014
-rw-r--r--kvx/OpWeights.ml115
-rw-r--r--kvx/Peephole.v158
-rw-r--r--kvx/PostpassScheduling.v526
-rw-r--r--kvx/PostpassSchedulingOracle.ml1035
-rw-r--r--kvx/PostpassSchedulingproof.v690
l---------kvx/PrepassSchedulingOracle.ml1
l---------kvx/PrepassSchedulingOracleDeps.ml1
-rw-r--r--kvx/PrintOp.ml229
-rw-r--r--kvx/SelectLong.vp463
-rw-r--r--kvx/SelectLongproof.v951
-rw-r--r--kvx/SelectOp.vp758
-rw-r--r--kvx/SelectOpproof.v1901
-rw-r--r--kvx/Stacklayout.v151
-rw-r--r--kvx/TargetPrinter.ml892
-rw-r--r--kvx/ValueAOp.v599
-rwxr-xr-xkvx/bitmasks.py12
-rw-r--r--kvx/extractionMachdep.v32
-rw-r--r--kvx/unittest/Makefile13
-rw-r--r--kvx/unittest/postpass_test.ml12
-rw-r--r--lib/Coqlib.v41
-rw-r--r--lib/Floats.v63
-rw-r--r--lib/HashedSet.v1414
-rw-r--r--lib/HashedSetaux.ml67
-rw-r--r--lib/HashedSetaux.mli18
-rw-r--r--lib/Impure/ImpConfig.v85
-rw-r--r--lib/Impure/ImpCore.v196
-rw-r--r--lib/Impure/ImpExtern.v7
-rw-r--r--lib/Impure/ImpHCons.v199
-rw-r--r--lib/Impure/ImpIO.v159
-rw-r--r--lib/Impure/ImpLoops.v123
-rw-r--r--lib/Impure/ImpMonads.v148
-rw-r--r--lib/Impure/ImpPrelude.v206
-rw-r--r--lib/Impure/LICENSE165
-rw-r--r--lib/Impure/README.md31
-rw-r--r--lib/Impure/ocaml/ImpHConsOracles.ml72
-rw-r--r--lib/Impure/ocaml/ImpHConsOracles.mli5
-rw-r--r--lib/Impure/ocaml/ImpIOOracles.ml142
-rw-r--r--lib/Impure/ocaml/ImpIOOracles.mli33
-rw-r--r--lib/Impure/ocaml/ImpLoopOracles.ml78
-rw-r--r--lib/Impure/ocaml/ImpLoopOracles.mli8
-rw-r--r--lib/Integers.v213
-rw-r--r--lib/IterList.v112
-rw-r--r--lib/Lattice.v110
-rw-r--r--lib/Maps.v161
-rw-r--r--lib/OptionMonad.v70
-rw-r--r--lib/UnionFind.v63
-rw-r--r--lib/extra/HashedMap.v460
-rwxr-xr-xmake_docker.sh3
-rw-r--r--powerpc/Archi.v2
-rw-r--r--powerpc/AsmToJSON.ml1
-rw-r--r--powerpc/Asmgen.v14
-rw-r--r--powerpc/Asmgenproof.v8
-rw-r--r--powerpc/Asmgenproof1.v5
l---------powerpc/BTL_SEsimplify.v1
-rw-r--r--powerpc/CSE2deps.v35
-rw-r--r--powerpc/CSE2depsproof.v214
-rw-r--r--powerpc/DuplicateOpcodeHeuristic.ml41
l---------powerpc/ExpansionOracle.ml1
-rw-r--r--powerpc/Machregsaux.ml5
-rw-r--r--powerpc/Machregsaux.mli2
-rw-r--r--powerpc/Op.v109
l---------powerpc/PrepassSchedulingOracle.ml1
-rw-r--r--powerpc/SelectLong.vp2
-rw-r--r--powerpc/SelectLongproof.v1
-rw-r--r--powerpc/SelectOp.vp11
-rw-r--r--powerpc/SelectOpproof.v29
-rw-r--r--powerpc/TargetPrinter.ml8
-rw-r--r--riscV/Archi.v2
-rw-r--r--riscV/Asm.v36
-rw-r--r--riscV/Asmexpand.ml42
-rw-r--r--riscV/Asmgen.v342
-rw-r--r--riscV/Asmgenproof.v50
-rw-r--r--riscV/Asmgenproof1.v463
-rw-r--r--riscV/BTL_SEsimplify.v1923
-rw-r--r--riscV/Builtins1.v27
-rw-r--r--riscV/CBuiltins.ml8
-rw-r--r--riscV/CSE2deps.v35
-rw-r--r--riscV/CSE2depsproof.v147
-rw-r--r--riscV/ConstpropOpproof.v152
-rw-r--r--riscV/DuplicateOpcodeHeuristic.ml41
-rw-r--r--riscV/ExpansionOracle.ml1019
-rw-r--r--riscV/ExtValues.v123
-rw-r--r--riscV/Machregsaux.ml7
-rw-r--r--riscV/Machregsaux.mli5
-rw-r--r--riscV/NeedOp.v60
-rw-r--r--riscV/Op.v1054
-rw-r--r--riscV/OpWeights.ml306
l---------riscV/PrepassSchedulingOracle.ml1
l---------riscV/PrepassSchedulingOracleDeps.ml1
-rw-r--r--riscV/PrintOp.ml96
-rw-r--r--riscV/SelectLong.vp2
-rw-r--r--riscV/SelectLongproof.v55
-rw-r--r--riscV/SelectOp.vp48
-rw-r--r--riscV/SelectOpproof.v221
-rw-r--r--riscV/TargetPrinter.ml6
-rw-r--r--riscV/ValueAOp.v329
-rw-r--r--runtime/Makefile23
l---------runtime/c/ccomp_kvx_fixes.h1
-rw-r--r--runtime/c/write_profiling_table.c70
-rw-r--r--runtime/include/ccomp_kvx_fixes.h58
-rw-r--r--runtime/include/math.h40
-rw-r--r--runtime/kvx/Makefile15
l---------runtime/kvx/ccomp_kvx_fixes.h1
-rw-r--r--runtime/kvx/i32_divmod.s120
-rw-r--r--runtime/kvx/i64_sdiv.c23
-rw-r--r--runtime/kvx/i64_smod.c5
-rw-r--r--runtime/kvx/i64_udiv.c6
-rw-r--r--runtime/kvx/i64_udivmod.c30
-rw-r--r--runtime/kvx/i64_udivmod_stsud.s218
-rw-r--r--runtime/kvx/i64_umod.c6
-rw-r--r--runtime/kvx/vararg.s54
-rw-r--r--scheduling/BTL.v758
-rw-r--r--scheduling/BTLRenumber.ml112
-rw-r--r--scheduling/BTLScheduleraux.ml344
-rw-r--r--scheduling/BTL_Livecheck.v690
-rw-r--r--scheduling/BTL_SEimpl.v1507
-rw-r--r--scheduling/BTL_SEsimuref.v852
-rw-r--r--scheduling/BTL_SEtheory.v1336
-rw-r--r--scheduling/BTL_Scheduler.v176
-rw-r--r--scheduling/BTL_Schedulerproof.v434
-rw-r--r--scheduling/BTLcommonaux.ml87
-rw-r--r--scheduling/BTLmatchRTL.v606
-rw-r--r--scheduling/BTLroadmap.md454
-rw-r--r--scheduling/BTLtoRTL.v26
-rw-r--r--scheduling/BTLtoRTLaux.ml88
-rw-r--r--scheduling/BTLtoRTLproof.v405
-rw-r--r--scheduling/BTLtypes.ml31
-rw-r--r--scheduling/InstructionScheduler.ml1753
-rw-r--r--scheduling/InstructionScheduler.mli135
-rw-r--r--scheduling/PrintBTL.ml118
-rw-r--r--scheduling/RTLtoBTL.v27
-rw-r--r--scheduling/RTLtoBTLaux.ml119
-rw-r--r--scheduling/RTLtoBTLproof.v758
-rw-r--r--scheduling/abstractbb/AbstractBasicBlocksDef.v471
-rw-r--r--scheduling/abstractbb/ImpSimuTest.v1286
-rw-r--r--scheduling/abstractbb/Parallelizability.v792
-rw-r--r--scheduling/abstractbb/README.md12
-rw-r--r--scheduling/abstractbb/SeqSimuTheory.v397
-rw-r--r--scheduling/postpass_lib/ForwardSimulationBlock.v387
-rw-r--r--scheduling/postpass_lib/Machblock.v388
-rw-r--r--scheduling/postpass_lib/Machblockgen.v221
-rw-r--r--scheduling/postpass_lib/Machblockgenproof.v810
-rwxr-xr-xscripts/cplex/ilp_solver10
-rwxr-xr-xscripts/gurobi/ilp_solver3
-rwxr-xr-xscripts/scip/ilp_solver11
-rw-r--r--test/Makefile11
-rw-r--r--test/c/Makefile47
-rw-r--r--test/c/Results/binarytrees-kvx4
-rw-r--r--test/c/Results/chomp-kvx9
-rw-r--r--test/c/Results/fannkuch-kvx31
-rw-r--r--test/c/Results/fft-kvx1
-rw-r--r--test/c/Results/fftsp-kvx1
-rw-r--r--test/c/Results/fftw-kvx16
-rw-r--r--test/c/Results/fib-kvx1
-rw-r--r--test/c/Results/integr-kvx1
-rw-r--r--test/c/Results/knucleotide-kvx0
-rw-r--r--test/c/Results/lists-kvx2
-rw-r--r--test/c/Results/mandelbrot-kvxbin0 -> 209 bytes
-rw-r--r--test/c/Results/nbody-kvx2
-rw-r--r--test/c/Results/nsieve-kvx3
-rw-r--r--test/c/Results/nsievebits-kvx3
-rw-r--r--test/c/Results/perlin-kvx1
-rw-r--r--test/c/Results/qsort-kvx1
-rw-r--r--test/c/Results/sha1-kvx2
-rw-r--r--test/c/Results/spectral-kvx1
-rw-r--r--test/c/Results/vmach-kvx2
-rwxr-xr-xtest/c/Runtest71
-rw-r--r--test/c/aes.c4
-rw-r--r--test/c/almabench.c7
-rw-r--r--test/c/binarytrees.c6
-rw-r--r--test/c/chomp.c5
-rw-r--r--test/c/fannkuch.c8
-rw-r--r--test/c/fft.c7
-rw-r--r--test/c/fftsp.c4
-rw-r--r--test/c/fftw.c4
-rw-r--r--test/c/fib.c4
-rw-r--r--test/c/integr.c4
-rw-r--r--test/c/knucleotide.c6
-rw-r--r--test/c/lists.c8
-rw-r--r--test/c/mandelbrot.c14
-rw-r--r--test/c/nbody.c4
-rw-r--r--test/c/nsieve.c8
-rw-r--r--test/c/nsievebits.c8
-rw-r--r--test/c/perlin.c15
-rw-r--r--test/c/qsort.c4
-rw-r--r--test/c/sha1.c4
-rw-r--r--test/c/sha3.c5
-rw-r--r--test/c/siphash24.c8
-rw-r--r--test/c/spectral.c4
-rw-r--r--test/c/vmach.c29
-rw-r--r--test/compression/Makefile14
-rw-r--r--test/cse2/globals.c8
-rw-r--r--test/cse2/indexed_addr.c6
-rw-r--r--test/endian.h2
-rw-r--r--test/gourdinl/builtin_memcpy.c9
-rw-r--r--test/gourdinl/c/add_return.c1
-rw-r--r--test/gourdinl/c/addresses.c32
-rw-r--r--test/gourdinl/c/arith.c16
-rw-r--r--test/gourdinl/c/arith_print.c19
-rw-r--r--test/gourdinl/c/armstrong.c21
-rw-r--r--test/gourdinl/c/array1.c64
-rw-r--r--test/gourdinl/c/array2.c74
-rw-r--r--test/gourdinl/c/biggest_of_3_int.c10
-rw-r--r--test/gourdinl/c/bitwise1.c8
-rw-r--r--test/gourdinl/c/cpintarray.c108
-rw-r--r--test/gourdinl/c/enum1.c52
-rw-r--r--test/gourdinl/c/enum2.c50
-rw-r--r--test/gourdinl/c/floop.c8
-rw-r--r--test/gourdinl/c/floor.c29
-rw-r--r--test/gourdinl/c/funcs.c36
-rw-r--r--test/gourdinl/c/hello.c6
-rw-r--r--test/gourdinl/c/if.c7
-rw-r--r--test/gourdinl/c/msb_pos.c20
-rw-r--r--test/gourdinl/c/power2.c42
-rw-r--r--test/gourdinl/c/prime.c23
-rw-r--r--test/gourdinl/c/random.c50
-rw-r--r--test/gourdinl/c/simple_op.c8
-rw-r--r--test/gourdinl/c/wloop.c8
-rw-r--r--test/gourdinl/clause.h12
-rw-r--r--test/gourdinl/clause2.c23
-rwxr-xr-xtest/gourdinl/compare_pp.sh16
-rw-r--r--test/gourdinl/cond_exp_mini_cse.c6
-rwxr-xr-xtest/gourdinl/cscript.sh20
-rw-r--r--test/gourdinl/fp_init.c7
-rwxr-xr-xtest/gourdinl/gen_asm_files.sh6
-rw-r--r--test/gourdinl/postpass_alternate_str.c11
-rw-r--r--test/gourdinl/postpass_exp.c5
-rw-r--r--test/kvx/.gitignore20
-rw-r--r--test/kvx/builtins/clzll.c7
-rw-r--r--test/kvx/builtins/stsud.c7
-rwxr-xr-xtest/kvx/coverage.sh24
-rw-r--r--test/kvx/coverage_helper.py45
-rwxr-xr-xtest/kvx/delout.sh6
-rw-r--r--test/kvx/do_test.sh50
-rw-r--r--test/kvx/general/clzd.c7
-rw-r--r--test/kvx/general/clzw.c7
-rw-r--r--test/kvx/general/ctzd.c7
-rw-r--r--test/kvx/general/ctzw.c7
-rw-r--r--test/kvx/general/satd.c7
-rw-r--r--test/kvx/general/sbmm8.c7
-rw-r--r--test/kvx/general/sbmmt8.c7
-rwxr-xr-xtest/kvx/hardcheck.sh6
-rwxr-xr-xtest/kvx/hardtest.sh6
-rw-r--r--test/kvx/instr/.gitignore1
-rw-r--r--test/kvx/instr/Makefile176
-rw-r--r--test/kvx/instr/builtin32.c12
-rw-r--r--test/kvx/instr/builtin64.c17
-rw-r--r--test/kvx/instr/div32.c5
-rw-r--r--test/kvx/instr/divf32.c5
-rw-r--r--test/kvx/instr/divf64.c5
-rw-r--r--test/kvx/instr/divu32.c7
-rw-r--r--test/kvx/instr/f32.c8
-rw-r--r--test/kvx/instr/f64.c8
-rwxr-xr-xtest/kvx/instr/floatcmp.py93
-rw-r--r--test/kvx/instr/framework.h66
-rw-r--r--test/kvx/instr/i32.c149
-rw-r--r--test/kvx/instr/i64.c169
-rw-r--r--test/kvx/instr/individual/andw.c5
-rw-r--r--test/kvx/instr/individual/branch.c10
-rw-r--r--test/kvx/instr/individual/branchz.c10
-rw-r--r--test/kvx/instr/individual/branchzu.c11
-rw-r--r--test/kvx/instr/individual/call.c16
-rw-r--r--test/kvx/instr/individual/cast_S32_S64.c7
-rw-r--r--test/kvx/instr/individual/cast_S64_U32.c7
-rw-r--r--test/kvx/instr/individual/cb.deqz.c10
-rw-r--r--test/kvx/instr/individual/cb.dgez.c10
-rw-r--r--test/kvx/instr/individual/cb.dgtz.c10
-rw-r--r--test/kvx/instr/individual/cb.dlez.c10
-rw-r--r--test/kvx/instr/individual/cb.dltz.c10
-rw-r--r--test/kvx/instr/individual/cb.dnez.c10
-rw-r--r--test/kvx/instr/individual/cb.wgez.c10
-rw-r--r--test/kvx/instr/individual/cb.wgtz.c10
-rw-r--r--test/kvx/instr/individual/cb.wlez.c10
-rw-r--r--test/kvx/instr/individual/cb.wltz.c10
-rw-r--r--test/kvx/instr/individual/compd.eq.c7
-rw-r--r--test/kvx/instr/individual/compd.geu.c7
-rw-r--r--test/kvx/instr/individual/compd.gt.c7
-rw-r--r--test/kvx/instr/individual/compd.le.c7
-rw-r--r--test/kvx/instr/individual/compd.leu.c7
-rw-r--r--test/kvx/instr/individual/compd.lt.c7
-rw-r--r--test/kvx/instr/individual/compd.ltu.c7
-rw-r--r--test/kvx/instr/individual/compd.ne.c7
-rw-r--r--test/kvx/instr/individual/compw.eq.c7
-rw-r--r--test/kvx/instr/individual/compw.geu.c7
-rw-r--r--test/kvx/instr/individual/compw.gt.c7
-rw-r--r--test/kvx/instr/individual/compw.gtu.c7
-rw-r--r--test/kvx/instr/individual/compw.le.c7
-rw-r--r--test/kvx/instr/individual/compw.leu.c7
-rw-r--r--test/kvx/instr/individual/compw.lt.c7
-rw-r--r--test/kvx/instr/individual/compw.ltu.c7
-rw-r--r--test/kvx/instr/individual/compw.ne.c7
-rw-r--r--test/kvx/instr/individual/div2.c7
-rw-r--r--test/kvx/instr/individual/doubleconv.c9
-rw-r--r--test/kvx/instr/individual/floatconv.c9
-rw-r--r--test/kvx/instr/individual/fmuld.c7
-rw-r--r--test/kvx/instr/individual/fmulw.c7
-rw-r--r--test/kvx/instr/individual/fnegd.c7
-rw-r--r--test/kvx/instr/individual/fnegw.c7
-rw-r--r--test/kvx/instr/individual/for.c9
-rw-r--r--test/kvx/instr/individual/forvar.c9
-rw-r--r--test/kvx/instr/individual/forvarl.c10
-rw-r--r--test/kvx/instr/individual/fsbfd.c7
-rw-r--r--test/kvx/instr/individual/fsbfw.c7
-rw-r--r--test/kvx/instr/individual/indirect_call.c33
-rw-r--r--test/kvx/instr/individual/indirect_tailcall.c33
-rw-r--r--test/kvx/instr/individual/lbs.c9
-rw-r--r--test/kvx/instr/individual/lbz.c9
-rw-r--r--test/kvx/instr/individual/muld.c7
-rw-r--r--test/kvx/instr/individual/mulw.c7
-rw-r--r--test/kvx/instr/individual/negd.c7
-rw-r--r--test/kvx/instr/individual/ord.c7
-rw-r--r--test/kvx/instr/individual/sbfd.c7
-rw-r--r--test/kvx/instr/individual/sbfw.c7
-rw-r--r--test/kvx/instr/individual/simple.c7
-rw-r--r--test/kvx/instr/individual/sllw.c7
-rw-r--r--test/kvx/instr/individual/srad.c7
-rw-r--r--test/kvx/instr/individual/srld.c7
-rw-r--r--test/kvx/instr/individual/tailcall.c16
-rw-r--r--test/kvx/instr/individual/udivd.c7
-rw-r--r--test/kvx/instr/individual/umodd.c7
-rw-r--r--test/kvx/instr/individual/xord.c7
-rw-r--r--test/kvx/instr/modi32.c5
-rw-r--r--test/kvx/instr/modui32.c7
-rw-r--r--test/kvx/instr/ui32.c12
-rw-r--r--test/kvx/instr/ui64.c10
-rw-r--r--test/kvx/interop/.gitignore1
-rw-r--r--test/kvx/interop/Makefile365
-rw-r--r--test/kvx/interop/common.c257
-rw-r--r--test/kvx/interop/common.h28
-rw-r--r--test/kvx/interop/framework.h66
-rw-r--r--test/kvx/interop/i32.c13
-rw-r--r--test/kvx/interop/i64.c14
-rw-r--r--test/kvx/interop/individual/i_multiiargs.c6
-rw-r--r--test/kvx/interop/individual/i_oneiarg.c6
-rw-r--r--test/kvx/interop/individual/ll_multillargs.c7
-rw-r--r--test/kvx/interop/individual/ll_onellarg.c7
-rw-r--r--test/kvx/interop/individual/ll_void.c7
-rw-r--r--test/kvx/interop/individual/void_void.c7
-rw-r--r--test/kvx/interop/stackhell.c9
-rw-r--r--test/kvx/interop/vaarg_common.c383
-rw-r--r--test/kvx/lib/Makefile133
-rw-r--r--test/kvx/lib/printf-test.c9
-rw-r--r--test/kvx/lib/printf.c9
-rw-r--r--test/kvx/mmult/.gitignore4
-rw-r--r--test/kvx/mmult/Makefile71
-rw-r--r--test/kvx/mmult/README.md17
-rw-r--r--test/kvx/mmult/mmult.c146
-rw-r--r--test/kvx/mmult/mmult.h10
-rw-r--r--test/kvx/prng/.gitignore3
-rw-r--r--test/kvx/prng/Makefile70
-rw-r--r--test/kvx/prng/README.md17
-rw-r--r--test/kvx/prng/prng.c41
-rw-r--r--test/kvx/prng/prng.h10
-rw-r--r--test/kvx/prng/types.h7
-rwxr-xr-xtest/kvx/simucheck.sh8
-rwxr-xr-xtest/kvx/simutest.sh8
-rw-r--r--test/kvx/sort/.gitignore9
-rw-r--r--test/kvx/sort/Makefile98
-rw-r--r--test/kvx/sort/README.md17
-rw-r--r--test/kvx/sort/insertion.c59
-rw-r--r--test/kvx/sort/insertion.h6
-rw-r--r--test/kvx/sort/main.c34
-rw-r--r--test/kvx/sort/merge.c92
-rw-r--r--test/kvx/sort/merge.h7
-rw-r--r--test/kvx/sort/selection.c62
-rw-r--r--test/kvx/sort/selection.h6
-rw-r--r--test/kvx/sort/test.h6
-rw-r--r--test/monniaux/.gitignore14
-rw-r--r--test/monniaux/Asmblockdeps.patch20
-rw-r--r--test/monniaux/BearSSL/.gitignore6
-rw-r--r--test/monniaux/BearSSL/Doxyfile2427
-rw-r--r--test/monniaux/BearSSL/LICENSE.txt21
-rw-r--r--test/monniaux/BearSSL/Makefile45
-rw-r--r--test/monniaux/BearSSL/README.txt136
-rwxr-xr-xtest/monniaux/BearSSL/compile_ccomp.sh1
-rw-r--r--test/monniaux/BearSSL/conf/Kalray.mk69
-rw-r--r--test/monniaux/BearSSL/conf/KalrayCompCert.mk69
-rw-r--r--test/monniaux/BearSSL/conf/Unix.mk69
-rw-r--r--test/monniaux/BearSSL/conf/Unix32.mk12
-rw-r--r--test/monniaux/BearSSL/conf/UnixClang.mk11
-rw-r--r--test/monniaux/BearSSL/conf/UnixCompCert.mk69
-rw-r--r--test/monniaux/BearSSL/conf/Win.mk70
-rw-r--r--test/monniaux/BearSSL/conf/samd20.mk20
-rw-r--r--test/monniaux/BearSSL/inc/bearssl.h170
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_aead.h1059
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_block.h2618
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_ec.h967
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_hash.h1346
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_hmac.h241
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_kdf.h284
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_pem.h294
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_prf.h150
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_rand.h397
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_rsa.h1655
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_ssl.h4296
-rw-r--r--test/monniaux/BearSSL/inc/bearssl_x509.h1397
-rw-r--r--test/monniaux/BearSSL/mk/Defaults.mk41
-rw-r--r--test/monniaux/BearSSL/mk/NMake.mk38
-rw-r--r--test/monniaux/BearSSL/mk/Rules.mk1322
-rw-r--r--test/monniaux/BearSSL/mk/SingleUnix.mk38
-rwxr-xr-xtest/monniaux/BearSSL/mk/mkT0.sh11
-rwxr-xr-xtest/monniaux/BearSSL/mk/mkrules.sh570
-rw-r--r--test/monniaux/BearSSL/samples/README.txt36
-rw-r--r--test/monniaux/BearSSL/samples/cert-ee-ec+rsa.pem14
-rw-r--r--test/monniaux/BearSSL/samples/cert-ee-ec.pem10
-rw-r--r--test/monniaux/BearSSL/samples/cert-ee-rsa.pem17
-rw-r--r--test/monniaux/BearSSL/samples/cert-ica-ec.pem10
-rw-r--r--test/monniaux/BearSSL/samples/cert-ica-rsa.pem17
-rw-r--r--test/monniaux/BearSSL/samples/cert-root-ec.pem9
-rw-r--r--test/monniaux/BearSSL/samples/cert-root-rsa.pem16
-rw-r--r--test/monniaux/BearSSL/samples/chain-ec+rsa.h166
-rw-r--r--test/monniaux/BearSSL/samples/chain-ec.h117
-rw-r--r--test/monniaux/BearSSL/samples/chain-rsa.h183
-rw-r--r--test/monniaux/BearSSL/samples/client_basic.c380
-rw-r--r--test/monniaux/BearSSL/samples/custom_profile.c601
-rw-r--r--test/monniaux/BearSSL/samples/key-ec.h40
-rw-r--r--test/monniaux/BearSSL/samples/key-ee-ec.pem5
-rw-r--r--test/monniaux/BearSSL/samples/key-ee-rsa.pem23
-rw-r--r--test/monniaux/BearSSL/samples/key-ica-ec.pem5
-rw-r--r--test/monniaux/BearSSL/samples/key-ica-rsa.pem23
-rw-r--r--test/monniaux/BearSSL/samples/key-root-ec.pem5
-rw-r--r--test/monniaux/BearSSL/samples/key-root-rsa.pem23
-rw-r--r--test/monniaux/BearSSL/samples/key-rsa.h108
-rw-r--r--test/monniaux/BearSSL/samples/server_basic.c436
-rw-r--r--test/monniaux/BearSSL/src/aead/ccm.c346
-rw-r--r--test/monniaux/BearSSL/src/aead/eax.c526
-rw-r--r--test/monniaux/BearSSL/src/aead/gcm.c319
-rw-r--r--test/monniaux/BearSSL/src/codec/ccopy.c44
-rw-r--r--test/monniaux/BearSSL/src/codec/dec16be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/dec16le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/dec32be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/dec32le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/dec64be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/dec64le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc16be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc16le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc32be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc32le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc64be.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/enc64le.c38
-rw-r--r--test/monniaux/BearSSL/src/codec/pemdec.c526
-rw-r--r--test/monniaux/BearSSL/src/codec/pemdec.t0314
-rw-r--r--test/monniaux/BearSSL/src/codec/pemenc.c173
-rw-r--r--test/monniaux/BearSSL/src/config.h230
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_all_m15.c121
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_all_m31.c171
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_i15.c398
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_i31.c390
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_m15.c1478
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_m31.c800
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_m62.c605
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_c25519_m64.c835
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_curve25519.c46
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_default.c36
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_keygen.c86
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_p256_m15.c2130
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_p256_m31.c1475
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_p256_m62.c1765
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_p256_m64.c1730
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_prime_i15.c820
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_prime_i31.c819
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_pubkey.c85
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_secp256r1.c51
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_secp384r1.c57
-rw-r--r--test/monniaux/BearSSL/src/ec/ec_secp521r1.c64
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_atr.c134
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_default_sign_asn1.c36
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_default_sign_raw.c36
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_asn1.c36
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_raw.c36
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i15_bits.c47
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_asn1.c45
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_raw.c174
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_asn1.c48
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_raw.c166
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i31_bits.c47
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_asn1.c45
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_raw.c173
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_asn1.c48
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_raw.c165
-rw-r--r--test/monniaux/BearSSL/src/ec/ecdsa_rta.c121
-rw-r--r--test/monniaux/BearSSL/src/hash/dig_oid.c84
-rw-r--r--test/monniaux/BearSSL/src/hash/dig_size.c50
-rw-r--r--test/monniaux/BearSSL/src/hash/ghash_ctmul.c345
-rw-r--r--test/monniaux/BearSSL/src/hash/ghash_ctmul32.c251
-rw-r--r--test/monniaux/BearSSL/src/hash/ghash_ctmul64.c154
-rw-r--r--test/monniaux/BearSSL/src/hash/ghash_pclmul.c389
-rw-r--r--test/monniaux/BearSSL/src/hash/ghash_pwr8.c411
-rw-r--r--test/monniaux/BearSSL/src/hash/md5.c208
-rw-r--r--test/monniaux/BearSSL/src/hash/md5sha1.c141
-rw-r--r--test/monniaux/BearSSL/src/hash/mgf1.c56
-rw-r--r--test/monniaux/BearSSL/src/hash/multihash.c166
-rw-r--r--test/monniaux/BearSSL/src/hash/sha1.c191
-rw-r--r--test/monniaux/BearSSL/src/hash/sha2big.c285
-rw-r--r--test/monniaux/BearSSL/src/hash/sha2small.c341
-rw-r--r--test/monniaux/BearSSL/src/inner.h2557
-rw-r--r--test/monniaux/BearSSL/src/int/i15_add.c46
-rw-r--r--test/monniaux/BearSSL/src/int/i15_bitlen.c44
-rw-r--r--test/monniaux/BearSSL/src/int/i15_decmod.c124
-rw-r--r--test/monniaux/BearSSL/src/int/i15_decode.c56
-rw-r--r--test/monniaux/BearSSL/src/int/i15_decred.c100
-rw-r--r--test/monniaux/BearSSL/src/int/i15_encode.c56
-rw-r--r--test/monniaux/BearSSL/src/int/i15_fmont.c59
-rw-r--r--test/monniaux/BearSSL/src/int/i15_iszero.c39
-rw-r--r--test/monniaux/BearSSL/src/int/i15_moddiv.c465
-rw-r--r--test/monniaux/BearSSL/src/int/i15_modpow.c50
-rw-r--r--test/monniaux/BearSSL/src/int/i15_modpow2.c160
-rw-r--r--test/monniaux/BearSSL/src/int/i15_montmul.c184
-rw-r--r--test/monniaux/BearSSL/src/int/i15_mulacc.c61
-rw-r--r--test/monniaux/BearSSL/src/int/i15_muladd.c173
-rw-r--r--test/monniaux/BearSSL/src/int/i15_ninv15.c38
-rw-r--r--test/monniaux/BearSSL/src/int/i15_reduce.c66
-rw-r--r--test/monniaux/BearSSL/src/int/i15_rshift.c47
-rw-r--r--test/monniaux/BearSSL/src/int/i15_sub.c46
-rw-r--r--test/monniaux/BearSSL/src/int/i15_tmont.c36
-rw-r--r--test/monniaux/BearSSL/src/int/i31_add.c46
-rw-r--r--test/monniaux/BearSSL/src/int/i31_bitlen.c44
-rw-r--r--test/monniaux/BearSSL/src/int/i31_decmod.c124
-rw-r--r--test/monniaux/BearSSL/src/int/i31_decode.c57
-rw-r--r--test/monniaux/BearSSL/src/int/i31_decred.c103
-rw-r--r--test/monniaux/BearSSL/src/int/i31_encode.c79
-rw-r--r--test/monniaux/BearSSL/src/int/i31_fmont.c60
-rw-r--r--test/monniaux/BearSSL/src/int/i31_iszero.c39
-rw-r--r--test/monniaux/BearSSL/src/int/i31_moddiv.c488
-rw-r--r--test/monniaux/BearSSL/src/int/i31_modpow.c65
-rw-r--r--test/monniaux/BearSSL/src/int/i31_modpow2.c160
-rw-r--r--test/monniaux/BearSSL/src/int/i31_montmul.c127
-rw-r--r--test/monniaux/BearSSL/src/int/i31_mulacc.c74
-rw-r--r--test/monniaux/BearSSL/src/int/i31_muladd.c157
-rw-r--r--test/monniaux/BearSSL/src/int/i31_ninv31.c39
-rw-r--r--test/monniaux/BearSSL/src/int/i31_reduce.c66
-rw-r--r--test/monniaux/BearSSL/src/int/i31_rshift.c47
-rw-r--r--test/monniaux/BearSSL/src/int/i31_sub.c46
-rw-r--r--test/monniaux/BearSSL/src/int/i31_tmont.c36
-rw-r--r--test/monniaux/BearSSL/src/int/i32_add.c51
-rw-r--r--test/monniaux/BearSSL/src/int/i32_bitlen.c44
-rw-r--r--test/monniaux/BearSSL/src/int/i32_decmod.c77
-rw-r--r--test/monniaux/BearSSL/src/int/i32_decode.c63
-rw-r--r--test/monniaux/BearSSL/src/int/i32_decred.c107
-rw-r--r--test/monniaux/BearSSL/src/int/i32_div32.c56
-rw-r--r--test/monniaux/BearSSL/src/int/i32_encode.c72
-rw-r--r--test/monniaux/BearSSL/src/int/i32_fmont.c60
-rw-r--r--test/monniaux/BearSSL/src/int/i32_iszero.c39
-rw-r--r--test/monniaux/BearSSL/src/int/i32_modpow.c65
-rw-r--r--test/monniaux/BearSSL/src/int/i32_montmul.c69
-rw-r--r--test/monniaux/BearSSL/src/int/i32_mulacc.c56
-rw-r--r--test/monniaux/BearSSL/src/int/i32_muladd.c138
-rw-r--r--test/monniaux/BearSSL/src/int/i32_ninv32.c39
-rw-r--r--test/monniaux/BearSSL/src/int/i32_reduce.c66
-rw-r--r--test/monniaux/BearSSL/src/int/i32_sub.c51
-rw-r--r--test/monniaux/BearSSL/src/int/i32_tmont.c36
-rw-r--r--test/monniaux/BearSSL/src/int/i62_modpow2.c493
-rw-r--r--test/monniaux/BearSSL/src/kdf/hkdf.c107
-rw-r--r--test/monniaux/BearSSL/src/kdf/shake.c590
-rw-r--r--test/monniaux/BearSSL/src/mac/hmac.c123
-rw-r--r--test/monniaux/BearSSL/src/mac/hmac_ct.c193
-rw-r--r--test/monniaux/BearSSL/src/rand/aesctr_drbg.c206
-rw-r--r--test/monniaux/BearSSL/src/rand/hmac_drbg.c157
-rw-r--r--test/monniaux/BearSSL/src/rand/sysrng.c170
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_keygen.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_modulus.c36
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_oaep_decrypt.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_oaep_encrypt.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_sign.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_vrfy.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_priv.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_privexp.c36
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pss_sign.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pss_vrfy.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pub.c38
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_default_pubexp.c36
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_keygen.c583
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_modulus.c99
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_decrypt.c41
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_encrypt.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_sign.c37
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_vrfy.c43
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_priv.c209
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_privexp.c320
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pss_sign.c40
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pss_vrfy.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pub.c113
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i15_pubexp.c152
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_keygen.c37
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_keygen_inner.c608
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_modulus.c99
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_decrypt.c41
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_encrypt.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_sign.c37
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_vrfy.c43
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_priv.c203
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_privexp.c318
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pss_sign.c40
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pss_vrfy.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pub.c106
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i31_pubexp.c152
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_decrypt.c41
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_encrypt.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_sign.c37
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_vrfy.c43
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_priv.c160
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_pss_sign.c40
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_pss_vrfy.c44
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i32_pub.c77
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_keygen.c57
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_decrypt.c61
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_encrypt.c64
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_sign.c57
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_vrfy.c63
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_priv.c223
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_pss_sign.c60
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_pss_vrfy.c64
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_i62_pub.c125
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_oaep_pad.c112
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_oaep_unpad.c145
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_pad.c100
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_unpad.c121
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_pss_sig_pad.c106
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_pss_sig_unpad.c121
-rw-r--r--test/monniaux/BearSSL/src/rsa/rsa_ssl_decrypt.c52
-rw-r--r--test/monniaux/BearSSL/src/settings.c306
-rw-r--r--test/monniaux/BearSSL/src/ssl/prf.c73
-rw-r--r--test/monniaux/BearSSL/src/ssl/prf_md5sha1.c43
-rw-r--r--test/monniaux/BearSSL/src/ssl/prf_sha256.c36
-rw-r--r--test/monniaux/BearSSL/src/ssl/prf_sha384.c36
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_ccert_single_ec.c156
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_ccert_single_rsa.c149
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_client.c78
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_client_default_rsapub.c32
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_client_full.c179
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine.c1569
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_aescbc.c64
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesccm.c67
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesgcm.c89
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_chapol.c65
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_descbc.c37
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_ec.c36
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_ecdsa.c38
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_engine_default_rsavrfy.c32
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hashes.c46
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hs_client.c1915
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hs_client.t01276
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hs_common.t01382
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hs_server.c2009
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_hs_server.t01510
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_io.c261
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_keyexport.c83
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_lru.c537
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_rec_cbc.c440
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_rec_ccm.c213
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_rec_chapol.c177
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_rec_gcm.c235
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_scert_single_ec.c142
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_scert_single_rsa.c162
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server.c52
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_full_ec.c149
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_full_rsa.c132
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_mine2c.c71
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_mine2g.c71
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_minf2c.c71
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_minf2g.c71
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_minr2g.c70
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_minu2g.c70
-rw-r--r--test/monniaux/BearSSL/src/ssl/ssl_server_minv2g.c70
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_cbcdec.c69
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_cbcenc.c67
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_ctr.c84
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_ctrcbc.c142
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_dec.c254
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_big_enc.c157
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_common.c112
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct.c328
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64.c398
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcdec.c104
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcenc.c81
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_ctr.c114
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_ctrcbc.c433
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_dec.c159
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct64_enc.c115
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_cbcdec.c111
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_cbcenc.c91
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_ctr.c116
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_ctrcbc.c422
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_dec.c170
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_ct_enc.c112
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_pwr8.c445
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcdec.c670
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcenc.c417
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctr.c717
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctrcbc.c946
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_cbcdec.c69
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_cbcenc.c67
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_ctr.c84
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_ctrcbc.c142
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_dec.c176
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_small_enc.c129
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_x86ni.c240
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcdec.c223
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcenc.c122
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctr.c211
-rw-r--r--test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctrcbc.c596
-rw-r--r--test/monniaux/BearSSL/src/symcipher/chacha20_ct.c106
-rw-r--r--test/monniaux/BearSSL/src/symcipher/chacha20_sse2.c237
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_ct.c411
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_ct_cbcdec.c87
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_ct_cbcenc.c69
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_support.c166
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_tab.c310
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_tab_cbcdec.c85
-rw-r--r--test/monniaux/BearSSL/src/symcipher/des_tab_cbcenc.c67
-rw-r--r--test/monniaux/BearSSL/src/symcipher/poly1305_ctmul.c260
-rw-r--r--test/monniaux/BearSSL/src/symcipher/poly1305_ctmul32.c297
-rw-r--r--test/monniaux/BearSSL/src/symcipher/poly1305_ctmulq.c475
-rw-r--r--test/monniaux/BearSSL/src/symcipher/poly1305_i15.c221
-rw-r--r--test/monniaux/BearSSL/src/x509/asn1.t0757
-rw-r--r--test/monniaux/BearSSL/src/x509/asn1enc.c93
-rw-r--r--test/monniaux/BearSSL/src/x509/encode_ec_pk8der.c110
-rw-r--r--test/monniaux/BearSSL/src/x509/encode_ec_rawder.c161
-rw-r--r--test/monniaux/BearSSL/src/x509/encode_rsa_pk8der.c97
-rw-r--r--test/monniaux/BearSSL/src/x509/encode_rsa_rawder.c96
-rw-r--r--test/monniaux/BearSSL/src/x509/skey_decoder.c650
-rw-r--r--test/monniaux/BearSSL/src/x509/skey_decoder.t0373
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_decoder.c773
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_decoder.t0321
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_knownkey.c105
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_minimal.c1713
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_minimal.t01508
-rw-r--r--test/monniaux/BearSSL/src/x509/x509_minimal_full.c59
-rw-r--r--test/monniaux/BearSSL/test/test_crypto.c9480
-rw-r--r--test/monniaux/BearSSL/test/test_math.c482
-rw-r--r--test/monniaux/BearSSL/test/test_speed.c1772
-rw-r--r--test/monniaux/BearSSL/test/test_x509.c2058
-rw-r--r--test/monniaux/BearSSL/test/x509/alltests.txt722
-rw-r--r--test/monniaux/BearSSL/test/x509/dn-ee.der1
-rw-r--r--test/monniaux/BearSSL/test/x509/dn-ica1.der1
-rw-r--r--test/monniaux/BearSSL/test/x509/dn-ica2.der1
-rw-r--r--test/monniaux/BearSSL/test/x509/dn-root-new.der1
-rw-r--r--test/monniaux/BearSSL/test/x509/dn-root.der1
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-badsig1.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-badsig2.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-cp1.crtbin0 -> 745 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-cp2.crtbin0 -> 702 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-cp3.crtbin0 -> 737 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-cp4.crtbin0 -> 748 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-dates.crtbin0 -> 872 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-md5.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-names.crtbin0 -> 892 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-names2.crtbin0 -> 842 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-names3.crtbin0 -> 843 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-names4.crtbin0 -> 938 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256-sha1.crtbin0 -> 472 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256-sha224.crtbin0 -> 473 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256-sha256.crtbin0 -> 474 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256-sha384.crtbin0 -> 475 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256-sha512.crtbin0 -> 474 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p256.crtbin0 -> 474 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p384.crtbin0 -> 534 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-p521.crtbin0 -> 610 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-sha1.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-sha224.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-sha384.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-sha512.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee-trailing.crtbin0 -> 871 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ee.crtbin0 -> 870 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-1016.crtbin0 -> 709 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-1017.crtbin0 -> 709 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-4096.crtbin0 -> 1098 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-p256.crtbin0 -> 446 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-p384.crtbin0 -> 507 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1-p521.crtbin0 -> 581 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica1.crtbin0 -> 842 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-1016.crtbin0 -> 725 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-1017.crtbin0 -> 726 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-4096.crtbin0 -> 1111 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-notCA.crtbin0 -> 852 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-p256.crtbin0 -> 460 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-p384.crtbin0 -> 520 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2-p521.crtbin0 -> 595 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/ica2.crtbin0 -> 855 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/junk.crtbin0 -> 800 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/names.crtbin0 -> 1366 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/root-p256.crtbin0 -> 413 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/root-p384.crtbin0 -> 475 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/root-p521.crtbin0 -> 549 bytes
-rw-r--r--test/monniaux/BearSSL/test/x509/root.crtbin0 -> 810 bytes
-rw-r--r--test/monniaux/BearSSL/tools/brssl.c122
-rw-r--r--test/monniaux/BearSSL/tools/brssl.h567
-rw-r--r--test/monniaux/BearSSL/tools/certs.c237
-rw-r--r--test/monniaux/BearSSL/tools/chain.c154
-rw-r--r--test/monniaux/BearSSL/tools/client.c1112
-rw-r--r--test/monniaux/BearSSL/tools/errors.c344
-rw-r--r--test/monniaux/BearSSL/tools/files.c329
-rw-r--r--test/monniaux/BearSSL/tools/impl.c48
-rw-r--r--test/monniaux/BearSSL/tools/keys.c234
-rw-r--r--test/monniaux/BearSSL/tools/names.c1056
-rw-r--r--test/monniaux/BearSSL/tools/server.c1235
-rw-r--r--test/monniaux/BearSSL/tools/skey.c784
-rw-r--r--test/monniaux/BearSSL/tools/sslio.c760
-rw-r--r--test/monniaux/BearSSL/tools/ta.c254
-rw-r--r--test/monniaux/BearSSL/tools/twrch.c1069
-rw-r--r--test/monniaux/BearSSL/tools/vector.c66
-rw-r--r--test/monniaux/BearSSL/tools/verify.c353
-rw-r--r--test/monniaux/BearSSL/tools/xmem.c120
-rw-r--r--test/monniaux/Makefile39
-rw-r--r--test/monniaux/PostpassSchedulingOracle.patch33
-rw-r--r--test/monniaux/README.md107
-rw-r--r--test/monniaux/acswap/test_swapd.c13
-rw-r--r--test/monniaux/acswap/test_swapw.c13
-rw-r--r--test/monniaux/benches.sh3
-rw-r--r--test/monniaux/binary_search/Makefile4
-rw-r--r--test/monniaux/binary_search/binary_search.c130
-rw-r--r--test/monniaux/bitfields/bitfields.c36
-rw-r--r--test/monniaux/bitfields/bitfields_long.c19
-rw-r--r--test/monniaux/bitsliced-aes/Makefile4
-rw-r--r--test/monniaux/bitsliced-aes/README.md59
-rw-r--r--test/monniaux/bitsliced-aes/aes.c129
-rw-r--r--test/monniaux/bitsliced-aes/aes.h12
-rw-r--r--test/monniaux/bitsliced-aes/bs.c1150
-rw-r--r--test/monniaux/bitsliced-aes/bs.h70
-rw-r--r--test/monniaux/bitsliced-aes/key_schedule.c87
-rw-r--r--test/monniaux/bitsliced-aes/main.c47
-rw-r--r--test/monniaux/bitsliced-aes/notes.org59
-rw-r--r--test/monniaux/bitsliced-aes/one_file/bitsliced-aes.c1542
-rwxr-xr-xtest/monniaux/bitsliced-aes/one_file/compare.sh12
-rw-r--r--test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_compute.c32
-rw-r--r--test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_main.c20
-rwxr-xr-xtest/monniaux/bitsliced-aes/one_file/reduce/compare.sh16
-rw-r--r--test/monniaux/bitsliced-aes/testbench/app.c144
-rw-r--r--test/monniaux/bitsliced-aes/testbench/app.h7
-rw-r--r--test/monniaux/bitsliced-aes/tests/tests.c108
-rw-r--r--test/monniaux/bitsliced-aes/tests/tests.h8
-rw-r--r--test/monniaux/bitsliced-aes/utils.c30
-rw-r--r--test/monniaux/bitsliced-aes/utils.h20
-rw-r--r--test/monniaux/bitsliced-tea/Makefile3
-rw-r--r--test/monniaux/bitsliced-tea/README.md3
-rw-r--r--test/monniaux/bitsliced-tea/bstea.c368
-rw-r--r--test/monniaux/bitsliced-tea/bstea.h149
-rw-r--r--test/monniaux/bitsliced-tea/bstea_run.c123
-rw-r--r--test/monniaux/bitsliced-tea/bstea_wordsize.h16
-rwxr-xr-xtest/monniaux/bitsliced-tea/reduce/compare.sh20
-rwxr-xr-xtest/monniaux/build_benches.sh26
-rw-r--r--test/monniaux/builtins/fma.c14
-rwxr-xr-xtest/monniaux/clean_benches.sh12
-rwxr-xr-xtest/monniaux/clean_csv.sh9
-rw-r--r--test/monniaux/clock.c32
-rw-r--r--test/monniaux/clock.h9
-rw-r--r--test/monniaux/cmov/cmov.c22
-rw-r--r--test/monniaux/cmov/cmov2.c28
-rw-r--r--test/monniaux/codegen/comp0.c3
-rw-r--r--test/monniaux/complex/Makefile4
-rw-r--r--test/monniaux/complex/complex_mat.c247
-rw-r--r--test/monniaux/crypto-algorithms/Makefile28
-rw-r--r--test/monniaux/crypto-algorithms/README.md18
-rw-r--r--test/monniaux/crypto-algorithms/aes.c1095
-rw-r--r--test/monniaux/crypto-algorithms/aes.h123
-rw-r--r--test/monniaux/crypto-algorithms/aes_test.c285
-rw-r--r--test/monniaux/crypto-algorithms/arcfour.c47
-rw-r--r--test/monniaux/crypto-algorithms/arcfour.h30
-rw-r--r--test/monniaux/crypto-algorithms/arcfour_test.c47
-rw-r--r--test/monniaux/crypto-algorithms/base64.c135
-rw-r--r--test/monniaux/crypto-algorithms/base64.h27
-rw-r--r--test/monniaux/crypto-algorithms/base64_test.c54
-rw-r--r--test/monniaux/crypto-algorithms/blowfish.c269
-rw-r--r--test/monniaux/crypto-algorithms/blowfish.h32
-rw-r--r--test/monniaux/crypto-algorithms/blowfish_test.c68
-rw-r--r--test/monniaux/crypto-algorithms/des.c269
-rw-r--r--test/monniaux/crypto-algorithms/des.h37
-rw-r--r--test/monniaux/crypto-algorithms/des_test.c83
-rw-r--r--test/monniaux/crypto-algorithms/md2.c104
-rw-r--r--test/monniaux/crypto-algorithms/md2.h33
-rw-r--r--test/monniaux/crypto-algorithms/md2_test.c58
-rw-r--r--test/monniaux/crypto-algorithms/md5.c189
-rw-r--r--test/monniaux/crypto-algorithms/md5.h34
-rw-r--r--test/monniaux/crypto-algorithms/md5_test.c60
-rw-r--r--test/monniaux/crypto-algorithms/rot-13.c35
-rw-r--r--test/monniaux/crypto-algorithms/rot-13.h20
-rw-r--r--test/monniaux/crypto-algorithms/rot-13_test.c44
-rw-r--r--test/monniaux/crypto-algorithms/sha1.c149
-rw-r--r--test/monniaux/crypto-algorithms/sha1.h35
-rw-r--r--test/monniaux/crypto-algorithms/sha1_test.c58
-rw-r--r--test/monniaux/crypto-algorithms/sha256.c158
-rw-r--r--test/monniaux/crypto-algorithms/sha256.h34
-rw-r--r--test/monniaux/crypto-algorithms/sha256_test.c61
-rw-r--r--test/monniaux/cse2/loopaccess.c7
-rw-r--r--test/monniaux/cse2/loopinvariant.c7
-rw-r--r--test/monniaux/cse2/loopload.c5
-rw-r--r--test/monniaux/cse2/noloopinvariant.c6
-rw-r--r--test/monniaux/cse2/storeload.c5
-rw-r--r--test/monniaux/csmith/Makefile116
-rw-r--r--test/monniaux/csmith/Makefile.old23
-rwxr-xr-xtest/monniaux/csmith/reduce/reduce.sh15
-rwxr-xr-xtest/monniaux/csmith/reduce/reduce_wrt_host.sh37
-rw-r--r--test/monniaux/cycles.h105
-rw-r--r--test/monniaux/des/des.c501
-rw-r--r--test/monniaux/division/harness.c82
-rw-r--r--test/monniaux/division/my_udiv32.s36
-rw-r--r--test/monniaux/dm_random.c7
-rw-r--r--test/monniaux/expect/expect.c7
-rw-r--r--test/monniaux/fill_buffer/fill_buffer.c35
-rw-r--r--test/monniaux/float_mat/Makefile4
-rw-r--r--test/monniaux/float_mat/float_mat.c228
-rw-r--r--test/monniaux/float_mat/float_mat.h55
-rw-r--r--test/monniaux/float_mat/float_mat_run.c133
-rw-r--r--test/monniaux/frame_pointer/Makefile16
-rw-r--r--test/monniaux/frame_pointer/a.c9
-rw-r--r--test/monniaux/frame_pointer/b.c11
-rw-r--r--test/monniaux/genann/Makefile4
-rw-r--r--test/monniaux/genann/example/iris.data150
-rw-r--r--test/monniaux/genann/example4shorter.c141
-rw-r--r--test/monniaux/genann/genann.c415
-rw-r--r--test/monniaux/genann/genann.h109
-rw-r--r--test/monniaux/glibc_qsort/Makefile3
-rw-r--r--test/monniaux/glibc_qsort/glibc_qsort.c209
-rw-r--r--test/monniaux/glibc_qsort/glibc_qsort.h4
-rw-r--r--test/monniaux/glibc_qsort/glibc_qsort_run.c49
-rw-r--r--test/monniaux/glibc_qsort/lgpl-2.1.txt502
-rw-r--r--test/monniaux/glpk-4.65/Makefile6
-rw-r--r--test/monniaux/glpk-4.65/config.h31
-rw-r--r--test/monniaux/glpk-4.65/examples/glpsol.c1598
-rw-r--r--test/monniaux/glpk-4.65/examples/prod.mod331
-rw-r--r--test/monniaux/glpk-4.65/src/amd/COPYING502
-rw-r--r--test/monniaux/glpk-4.65/src/amd/README58
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd.h67
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_1.c181
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_2.c1842
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_aat.c185
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_control.c64
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_defaults.c38
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_dump.c180
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_info.c120
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_internal.h117
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_order.c200
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_post_tree.c121
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_postorder.c207
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_preprocess.c119
-rw-r--r--test/monniaux/glpk-4.65/src/amd/amd_valid.c93
-rw-r--r--test/monniaux/glpk-4.65/src/api/advbas.c155
-rw-r--r--test/monniaux/glpk-4.65/src/api/asnhall.c163
-rw-r--r--test/monniaux/glpk-4.65/src/api/asnlp.c104
-rw-r--r--test/monniaux/glpk-4.65/src/api/asnokalg.c154
-rw-r--r--test/monniaux/glpk-4.65/src/api/ckasn.c78
-rw-r--r--test/monniaux/glpk-4.65/src/api/ckcnf.c82
-rw-r--r--test/monniaux/glpk-4.65/src/api/cplex.c1283
-rw-r--r--test/monniaux/glpk-4.65/src/api/cpp.c185
-rw-r--r--test/monniaux/glpk-4.65/src/api/cpxbas.c269
-rw-r--r--test/monniaux/glpk-4.65/src/api/graph.c504
-rw-r--r--test/monniaux/glpk-4.65/src/api/gridgen.c769
-rw-r--r--test/monniaux/glpk-4.65/src/api/intfeas1.c267
-rw-r--r--test/monniaux/glpk-4.65/src/api/maxffalg.c130
-rw-r--r--test/monniaux/glpk-4.65/src/api/maxflp.c114
-rw-r--r--test/monniaux/glpk-4.65/src/api/mcflp.c114
-rw-r--r--test/monniaux/glpk-4.65/src/api/mcfokalg.c221
-rw-r--r--test/monniaux/glpk-4.65/src/api/mcfrelax.c251
-rw-r--r--test/monniaux/glpk-4.65/src/api/minisat1.c161
-rw-r--r--test/monniaux/glpk-4.65/src/api/mpl.c269
-rw-r--r--test/monniaux/glpk-4.65/src/api/mps.c1452
-rw-r--r--test/monniaux/glpk-4.65/src/api/netgen.c1020
-rw-r--r--test/monniaux/glpk-4.65/src/api/npp.c143
-rw-r--r--test/monniaux/glpk-4.65/src/api/pript.c186
-rw-r--r--test/monniaux/glpk-4.65/src/api/prmip.c155
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob.h286
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob1.c1588
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob2.c491
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob3.c166
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob4.c156
-rw-r--r--test/monniaux/glpk-4.65/src/api/prob5.c168
-rw-r--r--test/monniaux/glpk-4.65/src/api/prrngs.c302
-rw-r--r--test/monniaux/glpk-4.65/src/api/prsol.c202
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdasn.c164
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdcc.c162
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdcnf.c136
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdipt.c185
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdmaxf.c163
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdmcf.c186
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdmip.c172
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdprob.c377
-rw-r--r--test/monniaux/glpk-4.65/src/api/rdsol.c225
-rw-r--r--test/monniaux/glpk-4.65/src/api/rmfgen.c368
-rw-r--r--test/monniaux/glpk-4.65/src/api/strong.c110
-rw-r--r--test/monniaux/glpk-4.65/src/api/topsort.c123
-rw-r--r--test/monniaux/glpk-4.65/src/api/wcliqex.c122
-rw-r--r--test/monniaux/glpk-4.65/src/api/weak.c150
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrasn.c107
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrcc.c102
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrcnf.c87
-rw-r--r--test/monniaux/glpk-4.65/src/api/wript.c124
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrmaxf.c104
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrmcf.c122
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrmip.c122
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrprob.c166
-rw-r--r--test/monniaux/glpk-4.65/src/api/wrsol.c174
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/btf.c569
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/btf.h207
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/btfint.c407
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/btfint.h73
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/fhv.c586
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/fhv.h114
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/fhvint.c168
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/fhvint.h78
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/ifu.c392
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/ifu.h99
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/luf.c713
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/luf.h227
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/lufint.c182
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/lufint.h73
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/scf.c523
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/scf.h211
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/scfint.c255
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/scfint.h89
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/sgf.c1443
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/sgf.h203
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/sva.c572
-rw-r--r--test/monniaux/glpk-4.65/src/bflib/sva.h161
-rw-r--r--test/monniaux/glpk-4.65/src/colamd/COPYING502
-rw-r--r--test/monniaux/glpk-4.65/src/colamd/README98
-rw-r--r--test/monniaux/glpk-4.65/src/colamd/colamd.c3622
-rw-r--r--test/monniaux/glpk-4.65/src/colamd/colamd.h69
-rw-r--r--test/monniaux/glpk-4.65/src/draft/bfd.c544
-rw-r--r--test/monniaux/glpk-4.65/src/draft/bfd.h107
-rw-r--r--test/monniaux/glpk-4.65/src/draft/bfx.c89
-rw-r--r--test/monniaux/glpk-4.65/src/draft/bfx.h67
-rw-r--r--test/monniaux/glpk-4.65/src/draft/draft.h22
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi06.c860
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi07.c499
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi08.c388
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi09.c798
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi10.c305
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi12.c2185
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpapi13.c710
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glphbm.c533
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glphbm.h127
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios01.c1685
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios02.c826
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios03.c1512
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios07.c551
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios09.c664
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios11.c435
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpios12.c177
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpipm.c1144
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpipm.h36
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpmat.c924
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpmat.h198
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glprgr.c173
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glprgr.h34
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpscl.c478
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpspm.c847
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpspm.h165
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpssx.h437
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpssx01.c839
-rw-r--r--test/monniaux/glpk-4.65/src/draft/glpssx02.c523
-rw-r--r--test/monniaux/glpk-4.65/src/draft/ios.h547
-rw-r--r--test/monniaux/glpk-4.65/src/draft/lux.c1030
-rw-r--r--test/monniaux/glpk-4.65/src/draft/lux.h220
-rw-r--r--test/monniaux/glpk-4.65/src/env/alloc.c252
-rw-r--r--test/monniaux/glpk-4.65/src/env/dlsup.c167
-rw-r--r--test/monniaux/glpk-4.65/src/env/env.c316
-rw-r--r--test/monniaux/glpk-4.65/src/env/env.h274
-rw-r--r--test/monniaux/glpk-4.65/src/env/error.c200
-rw-r--r--test/monniaux/glpk-4.65/src/env/stdc.c98
-rw-r--r--test/monniaux/glpk-4.65/src/env/stdc.h73
-rw-r--r--test/monniaux/glpk-4.65/src/env/stdout.c262
-rw-r--r--test/monniaux/glpk-4.65/src/env/stream.c517
-rw-r--r--test/monniaux/glpk-4.65/src/env/time.c150
-rw-r--r--test/monniaux/glpk-4.65/src/env/tls.c128
-rw-r--r--test/monniaux/glpk-4.65/src/glpk.h1175
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/cfg.c409
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/cfg.h138
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/cfg1.c703
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/cfg2.c91
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/clqcut.c134
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/covgen.c885
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/fpump.c360
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/gmicut.c284
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/gmigen.c142
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/mirgen.c1529
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/spv.c303
-rw-r--r--test/monniaux/glpk-4.65/src/intopt/spv.h83
-rw-r--r--test/monniaux/glpk-4.65/src/minisat/LICENSE20
-rw-r--r--test/monniaux/glpk-4.65/src/minisat/README22
-rw-r--r--test/monniaux/glpk-4.65/src/minisat/minisat.c1315
-rw-r--r--test/monniaux/glpk-4.65/src/minisat/minisat.h230
-rw-r--r--test/monniaux/glpk-4.65/src/misc/avl.c405
-rw-r--r--test/monniaux/glpk-4.65/src/misc/avl.h73
-rw-r--r--test/monniaux/glpk-4.65/src/misc/bignum.c286
-rw-r--r--test/monniaux/glpk-4.65/src/misc/bignum.h37
-rw-r--r--test/monniaux/glpk-4.65/src/misc/dimacs.c147
-rw-r--r--test/monniaux/glpk-4.65/src/misc/dimacs.h81
-rw-r--r--test/monniaux/glpk-4.65/src/misc/dmp.c243
-rw-r--r--test/monniaux/glpk-4.65/src/misc/dmp.h63
-rw-r--r--test/monniaux/glpk-4.65/src/misc/ffalg.c221
-rw-r--r--test/monniaux/glpk-4.65/src/misc/ffalg.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/fp2rat.c164
-rw-r--r--test/monniaux/glpk-4.65/src/misc/fvs.c137
-rw-r--r--test/monniaux/glpk-4.65/src/misc/fvs.h76
-rw-r--r--test/monniaux/glpk-4.65/src/misc/gcd.c102
-rw-r--r--test/monniaux/glpk-4.65/src/misc/jd.c152
-rw-r--r--test/monniaux/glpk-4.65/src/misc/jd.h32
-rw-r--r--test/monniaux/glpk-4.65/src/misc/keller.c235
-rw-r--r--test/monniaux/glpk-4.65/src/misc/keller.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/ks.c466
-rw-r--r--test/monniaux/glpk-4.65/src/misc/ks.h44
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mc13d.c314
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mc13d.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mc21a.c301
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mc21a.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/misc.h61
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mt1.c1110
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mt1.f277
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mt1.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mygmp.c1162
-rw-r--r--test/monniaux/glpk-4.65/src/misc/mygmp.h254
-rw-r--r--test/monniaux/glpk-4.65/src/misc/okalg.c382
-rw-r--r--test/monniaux/glpk-4.65/src/misc/okalg.h35
-rw-r--r--test/monniaux/glpk-4.65/src/misc/qmd.c584
-rw-r--r--test/monniaux/glpk-4.65/src/misc/qmd.h58
-rw-r--r--test/monniaux/glpk-4.65/src/misc/relax4.c2850
-rw-r--r--test/monniaux/glpk-4.65/src/misc/relax4.h102
-rw-r--r--test/monniaux/glpk-4.65/src/misc/rng.c227
-rw-r--r--test/monniaux/glpk-4.65/src/misc/rng.h67
-rw-r--r--test/monniaux/glpk-4.65/src/misc/rng1.c73
-rw-r--r--test/monniaux/glpk-4.65/src/misc/round2n.c64
-rw-r--r--test/monniaux/glpk-4.65/src/misc/str2int.c92
-rw-r--r--test/monniaux/glpk-4.65/src/misc/str2num.c110
-rw-r--r--test/monniaux/glpk-4.65/src/misc/strspx.c60
-rw-r--r--test/monniaux/glpk-4.65/src/misc/strtrim.c62
-rw-r--r--test/monniaux/glpk-4.65/src/misc/triang.c311
-rw-r--r--test/monniaux/glpk-4.65/src/misc/triang.h34
-rw-r--r--test/monniaux/glpk-4.65/src/misc/wclique.c242
-rw-r--r--test/monniaux/glpk-4.65/src/misc/wclique.h33
-rw-r--r--test/monniaux/glpk-4.65/src/misc/wclique1.c317
-rw-r--r--test/monniaux/glpk-4.65/src/misc/wclique1.h34
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl.h2598
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl1.c4718
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl2.c1202
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl3.c6100
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl4.c1426
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl5.c566
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mpl6.c1039
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mplsql.c1659
-rw-r--r--test/monniaux/glpk-4.65/src/mpl/mplsql.h63
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp.h645
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp1.c937
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp2.c1433
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp3.c2861
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp4.c1414
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp5.c809
-rw-r--r--test/monniaux/glpk-4.65/src/npp/npp6.c1500
-rw-r--r--test/monniaux/glpk-4.65/src/proxy/main.c.disabled87
-rw-r--r--test/monniaux/glpk-4.65/src/proxy/proxy.c1073
-rw-r--r--test/monniaux/glpk-4.65/src/proxy/proxy.h36
-rw-r--r--test/monniaux/glpk-4.65/src/proxy/proxy1.c88
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/simplex.h39
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxat.c265
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxat.h80
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxchuzc.c381
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxchuzc.h85
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxchuzr.c594
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxchuzr.h77
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxlp.c819
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxlp.h234
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxnt.c303
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxnt.h96
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxprim.c1860
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxprob.c679
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spxprob.h64
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spychuzc.c567
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spychuzc.h85
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spychuzr.c483
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spychuzr.h97
-rw-r--r--test/monniaux/glpk-4.65/src/simplex/spydual.c2101
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/README45
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/adler32.c169
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/compress.c80
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/crc32.c442
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/crc32.h441
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/deflate.c1834
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/deflate.h342
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/gzclose.c25
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/gzguts.h74
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/gzlib.c537
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/gzread.c653
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/gzwrite.c531
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inffast.c340
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inffast.h11
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inffixed.h94
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inflate.c1480
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inflate.h122
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inftrees.c330
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/inftrees.h62
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/trees.c1244
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/trees.h128
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/uncompr.c59
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zconf.h168
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zio.c92
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zio.h37
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zlib.h1613
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zutil.c318
-rw-r--r--test/monniaux/glpk-4.65/src/zlib/zutil.h93
-rw-r--r--test/monniaux/heapsort/Makefile3
-rw-r--r--test/monniaux/heapsort/heapsort.c58
-rw-r--r--test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified5333
-rw-r--r--test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified7328
-rw-r--r--test/monniaux/heapsort/heapsort.ccomp.k1c.s.orig358
-rw-r--r--test/monniaux/heapsort/heapsort.h7
-rw-r--r--test/monniaux/heapsort/heapsort_run.c22
-rw-r--r--test/monniaux/heptagon_radio_transmitter/Makefile3
-rw-r--r--test/monniaux/heptagon_radio_transmitter/_main.c74
-rw-r--r--test/monniaux/heptagon_radio_transmitter/_main.h9
-rw-r--r--test/monniaux/heptagon_radio_transmitter/pervasives.h45
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans.c571
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans.h67
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_controller.c160
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_controller.h30
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.c9
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.h12
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_types.c149
-rw-r--r--test/monniaux/heptagon_radio_transmitter/radiotrans_types.h56
-rw-r--r--test/monniaux/idea/Makefile3
-rw-r--r--test/monniaux/idea/idea.c406
-rw-r--r--test/monniaux/idea/idea.h30
-rw-r--r--test/monniaux/if/if2.c11
-rw-r--r--test/monniaux/jpeg-6b/Makefile60
-rw-r--r--test/monniaux/jpeg-6b/Makefile.orig319
-rw-r--r--test/monniaux/jpeg-6b/README385
-rw-r--r--test/monniaux/jpeg-6b/ansi2knr.136
-rw-r--r--test/monniaux/jpeg-6b/cderror.h132
-rw-r--r--test/monniaux/jpeg-6b/cdjpeg.c181
-rw-r--r--test/monniaux/jpeg-6b/cdjpeg.h184
-rw-r--r--test/monniaux/jpeg-6b/cjpeg.1292
-rw-r--r--test/monniaux/jpeg-6b/cjpeg.c619
-rw-r--r--test/monniaux/jpeg-6b/ckconfig.c402
-rw-r--r--test/monniaux/jpeg-6b/coderules.doc118
-rwxr-xr-xtest/monniaux/jpeg-6b/config.guess883
-rwxr-xr-xtest/monniaux/jpeg-6b/config.sub954
-rwxr-xr-xtest/monniaux/jpeg-6b/configure2011
-rw-r--r--test/monniaux/jpeg-6b/djpeg.1253
-rw-r--r--test/monniaux/jpeg-6b/djpeg.c630
-rw-r--r--test/monniaux/jpeg-6b/example.c433
-rw-r--r--test/monniaux/jpeg-6b/filelist.doc210
-rwxr-xr-xtest/monniaux/jpeg-6b/install-sh250
-rw-r--r--test/monniaux/jpeg-6b/install.doc1063
-rw-r--r--test/monniaux/jpeg-6b/jcapimin.c280
-rw-r--r--test/monniaux/jpeg-6b/jcapistd.c161
-rw-r--r--test/monniaux/jpeg-6b/jccoefct.c449
-rw-r--r--test/monniaux/jpeg-6b/jccolor.c459
-rw-r--r--test/monniaux/jpeg-6b/jcdctmgr.c387
-rw-r--r--test/monniaux/jpeg-6b/jchuff.c909
-rw-r--r--test/monniaux/jpeg-6b/jchuff.h47
-rw-r--r--test/monniaux/jpeg-6b/jcinit.c72
-rw-r--r--test/monniaux/jpeg-6b/jcmainct.c293
-rw-r--r--test/monniaux/jpeg-6b/jcmarker.c664
-rw-r--r--test/monniaux/jpeg-6b/jcmaster.c590
-rw-r--r--test/monniaux/jpeg-6b/jcomapi.c106
-rw-r--r--test/monniaux/jpeg-6b/jconfig.bcc48
-rw-r--r--test/monniaux/jpeg-6b/jconfig.cfg44
-rw-r--r--test/monniaux/jpeg-6b/jconfig.dj38
-rw-r--r--test/monniaux/jpeg-6b/jconfig.doc155
-rw-r--r--test/monniaux/jpeg-6b/jconfig.h45
-rw-r--r--test/monniaux/jpeg-6b/jconfig.mac43
-rw-r--r--test/monniaux/jpeg-6b/jconfig.manx43
-rw-r--r--test/monniaux/jpeg-6b/jconfig.mc652
-rw-r--r--test/monniaux/jpeg-6b/jconfig.sas43
-rw-r--r--test/monniaux/jpeg-6b/jconfig.st42
-rw-r--r--test/monniaux/jpeg-6b/jconfig.vc45
-rw-r--r--test/monniaux/jpeg-6b/jconfig.vms37
-rw-r--r--test/monniaux/jpeg-6b/jconfig.wat38
-rw-r--r--test/monniaux/jpeg-6b/jcparam.c610
-rw-r--r--test/monniaux/jpeg-6b/jcphuff.c833
-rw-r--r--test/monniaux/jpeg-6b/jcprepct.c354
-rw-r--r--test/monniaux/jpeg-6b/jcsample.c519
-rw-r--r--test/monniaux/jpeg-6b/jctrans.c388
-rw-r--r--test/monniaux/jpeg-6b/jdapimin.c395
-rw-r--r--test/monniaux/jpeg-6b/jdapistd.c275
-rw-r--r--test/monniaux/jpeg-6b/jdatadst.c151
-rw-r--r--test/monniaux/jpeg-6b/jdatasrc.c212
-rw-r--r--test/monniaux/jpeg-6b/jdcoefct.c736
-rw-r--r--test/monniaux/jpeg-6b/jdcolor.c396
-rw-r--r--test/monniaux/jpeg-6b/jdct.h176
-rw-r--r--test/monniaux/jpeg-6b/jddctmgr.c269
-rw-r--r--test/monniaux/jpeg-6b/jdhuff.c651
-rw-r--r--test/monniaux/jpeg-6b/jdhuff.h201
-rw-r--r--test/monniaux/jpeg-6b/jdinput.c381
-rw-r--r--test/monniaux/jpeg-6b/jdmainct.c512
-rw-r--r--test/monniaux/jpeg-6b/jdmarker.c1360
-rw-r--r--test/monniaux/jpeg-6b/jdmaster.c557
-rw-r--r--test/monniaux/jpeg-6b/jdmerge.c400
-rw-r--r--test/monniaux/jpeg-6b/jdosabcc.objbin0 -> 775 bytes
-rw-r--r--test/monniaux/jpeg-6b/jdosamsc.objbin0 -> 657 bytes
-rw-r--r--test/monniaux/jpeg-6b/jdosaobj.doc16
-rw-r--r--test/monniaux/jpeg-6b/jdphuff.c668
-rw-r--r--test/monniaux/jpeg-6b/jdpostct.c290
-rw-r--r--test/monniaux/jpeg-6b/jdsample.c478
-rw-r--r--test/monniaux/jpeg-6b/jdtrans.c143
-rw-r--r--test/monniaux/jpeg-6b/jerror.c252
-rw-r--r--test/monniaux/jpeg-6b/jerror.h291
-rw-r--r--test/monniaux/jpeg-6b/jfdctflt.c168
-rw-r--r--test/monniaux/jpeg-6b/jfdctfst.c224
-rw-r--r--test/monniaux/jpeg-6b/jfdctint.c283
-rw-r--r--test/monniaux/jpeg-6b/jidctflt.c242
-rw-r--r--test/monniaux/jpeg-6b/jidctfst.c368
-rw-r--r--test/monniaux/jpeg-6b/jidctint.c389
-rw-r--r--test/monniaux/jpeg-6b/jidctred.c398
-rw-r--r--test/monniaux/jpeg-6b/jinclude.h91
-rw-r--r--test/monniaux/jpeg-6b/jmemansi.c167
-rw-r--r--test/monniaux/jpeg-6b/jmemdos.c638
-rw-r--r--test/monniaux/jpeg-6b/jmemdosa.asm379
-rw-r--r--test/monniaux/jpeg-6b/jmemmac.c289
-rw-r--r--test/monniaux/jpeg-6b/jmemmgr.c1118
-rw-r--r--test/monniaux/jpeg-6b/jmemname.c276
-rw-r--r--test/monniaux/jpeg-6b/jmemnobs.c109
-rw-r--r--test/monniaux/jpeg-6b/jmemsys.h198
-rw-r--r--test/monniaux/jpeg-6b/jmorecfg.h363
-rw-r--r--test/monniaux/jpeg-6b/jpegint.h392
-rw-r--r--test/monniaux/jpeg-6b/jpeglib.h1096
-rw-r--r--test/monniaux/jpeg-6b/jpegtran.1238
-rw-r--r--test/monniaux/jpeg-6b/jpegtran.c504
-rw-r--r--test/monniaux/jpeg-6b/jquant1.c856
-rw-r--r--test/monniaux/jpeg-6b/jquant2.c1310
-rw-r--r--test/monniaux/jpeg-6b/jutils.c179
-rw-r--r--test/monniaux/jpeg-6b/jversion.h14
-rw-r--r--test/monniaux/jpeg-6b/libjpeg.doc3006
-rwxr-xr-xtest/monniaux/jpeg-6b/ltconfig1512
-rw-r--r--test/monniaux/jpeg-6b/ltmain.sh2453
-rw-r--r--test/monniaux/jpeg-6b/makcjpeg.st38
-rw-r--r--test/monniaux/jpeg-6b/makdjpeg.st38
-rw-r--r--test/monniaux/jpeg-6b/makeapps.ds828
-rw-r--r--test/monniaux/jpeg-6b/makefile.ansi214
-rw-r--r--test/monniaux/jpeg-6b/makefile.bcc285
-rw-r--r--test/monniaux/jpeg-6b/makefile.cfg319
-rw-r--r--test/monniaux/jpeg-6b/makefile.dj220
-rw-r--r--test/monniaux/jpeg-6b/makefile.manx214
-rw-r--r--test/monniaux/jpeg-6b/makefile.mc6249
-rw-r--r--test/monniaux/jpeg-6b/makefile.mms218
-rw-r--r--test/monniaux/jpeg-6b/makefile.sas252
-rw-r--r--test/monniaux/jpeg-6b/makefile.unix228
-rw-r--r--test/monniaux/jpeg-6b/makefile.vc211
-rw-r--r--test/monniaux/jpeg-6b/makefile.vms142
-rw-r--r--test/monniaux/jpeg-6b/makefile.wat233
-rw-r--r--test/monniaux/jpeg-6b/makelib.ds1046
-rw-r--r--test/monniaux/jpeg-6b/makeproj.mac213
-rw-r--r--test/monniaux/jpeg-6b/makljpeg.st70
-rw-r--r--test/monniaux/jpeg-6b/maktjpeg.st32
-rw-r--r--test/monniaux/jpeg-6b/makvms.opt4
-rw-r--r--test/monniaux/jpeg-6b/rdbmp.c439
-rw-r--r--test/monniaux/jpeg-6b/rdcolmap.c253
-rw-r--r--test/monniaux/jpeg-6b/rdgif.c38
-rw-r--r--test/monniaux/jpeg-6b/rdjpgcom.154
-rw-r--r--test/monniaux/jpeg-6b/rdjpgcom.c496
-rw-r--r--test/monniaux/jpeg-6b/rdppm.c458
-rw-r--r--test/monniaux/jpeg-6b/rdrle.c387
-rw-r--r--test/monniaux/jpeg-6b/rdswitch.c332
-rw-r--r--test/monniaux/jpeg-6b/rdtarga.c500
-rw-r--r--test/monniaux/jpeg-6b/readme.dos15
-rw-r--r--test/monniaux/jpeg-6b/structure.doc948
-rw-r--r--test/monniaux/jpeg-6b/testimg.bmpbin0 -> 35050 bytes
-rw-r--r--test/monniaux/jpeg-6b/testimg.jpgbin0 -> 5756 bytes
-rw-r--r--test/monniaux/jpeg-6b/testimg.ppm4
-rw-r--r--test/monniaux/jpeg-6b/testimgp.jpgbin0 -> 5645 bytes
-rw-r--r--test/monniaux/jpeg-6b/testorig.jpgbin0 -> 5770 bytes
-rw-r--r--test/monniaux/jpeg-6b/testprog.jpgbin0 -> 5655 bytes
-rw-r--r--test/monniaux/jpeg-6b/transupp.c928
-rw-r--r--test/monniaux/jpeg-6b/transupp.h135
-rw-r--r--test/monniaux/jpeg-6b/usage.doc562
-rw-r--r--test/monniaux/jpeg-6b/wizard.doc211
-rw-r--r--test/monniaux/jpeg-6b/wrbmp.c442
-rw-r--r--test/monniaux/jpeg-6b/wrgif.c399
-rw-r--r--test/monniaux/jpeg-6b/wrjpgcom.1103
-rw-r--r--test/monniaux/jpeg-6b/wrjpgcom.c583
-rw-r--r--test/monniaux/jpeg-6b/wrppm.c268
-rw-r--r--test/monniaux/jpeg-6b/wrrle.c305
-rw-r--r--test/monniaux/jpeg-6b/wrtarga.c253
-rw-r--r--test/monniaux/jumptable/jumptable.c34
-rw-r--r--test/monniaux/k1_builtins/atomics.c11
-rw-r--r--test/monniaux/k1_builtins/execute_code.c27
-rw-r--r--test/monniaux/k1_builtins/sbmm8.c15
-rwxr-xr-xtest/monniaux/k1_builtins/sbmm8_demo.sage21
-rw-r--r--test/monniaux/k1_builtins/test_k1_builtins.c40
-rw-r--r--test/monniaux/kill_pragma.h1
-rw-r--r--test/monniaux/latency/show_latency.c11
-rw-r--r--test/monniaux/licm/addv.c6
-rw-r--r--test/monniaux/longjmp/example.c55
-rw-r--r--test/monniaux/loop/fill_buffer.c37
-rw-r--r--test/monniaux/loop_nest/polybench.h202
-rw-r--r--test/monniaux/loop_nest/syrk.c28
-rw-r--r--test/monniaux/loop_nest/syrk.h54
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/Makefile3
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/arrow.h34
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/heater_control.c342
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/heater_control.h96
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/heater_control_alloc.h73
-rw-r--r--test/monniaux/lustrev4_lustrec_heater_control/heater_control_main.c48
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/Makefile3
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/heater_control.c275
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/heater_control.h47
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/heater_control.lus126
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/heater_control_loop.c64
-rw-r--r--test/monniaux/lustrev4_lv4_heater_control/make.proto3
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/Makefile3
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control.lus126
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.c309
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.h30
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control_loop.c63
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.c4
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.h9
-rw-r--r--test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_types.h41
-rw-r--r--test/monniaux/lustrev6-carlightV2/carlightV2_carlight.c282
-rw-r--r--test/monniaux/lustrev6-carlightV2/carlightV2_carlight.h52
-rw-r--r--test/monniaux/lustrev6-carlightV2/carlightV2_carlight_loop.c62
-rw-r--r--test/monniaux/lustrev6-carlightV2/lustre_consts.c4
-rw-r--r--test/monniaux/lustrev6-carlightV2/lustre_consts.h9
-rw-r--r--test/monniaux/lustrev6-carlightV2/lustre_types.h75
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/convertible_main.c1085
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/convertible_main.h110
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/convertible_main_loop.c86
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/lustre_consts.c4
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/lustre_consts.h23
-rw-r--r--test/monniaux/lustrev6-convertible-2cgc/lustre_types.h139
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/Makefile3
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.c3319
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.h52
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/convertible_main_loop.c86
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.c4
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.h23
-rw-r--r--test/monniaux/lustrev6-convertible-en-2cgc/lustre_types.h83
-rw-r--r--test/monniaux/lustrev6-convertible/convertible_main.c1251
-rw-r--r--test/monniaux/lustrev6-convertible/convertible_main.h287
-rw-r--r--test/monniaux/lustrev6-convertible/convertible_main_loop.c86
-rw-r--r--test/monniaux/lustrev6-convertible/lustre_consts.c4
-rw-r--r--test/monniaux/lustrev6-convertible/lustre_consts.h23
-rw-r--r--test/monniaux/lustrev6-convertible/lustre_types.h34
-rw-r--r--test/monniaux/madd/madd.c25
-rw-r--r--test/monniaux/madd/madd_run.c11
-rw-r--r--test/monniaux/many_temporaries/matrix_product.py20
-rw-r--r--test/monniaux/many_temporaries/matrix_product2.py24
-rw-r--r--test/monniaux/math/exceptions.c84
-rw-r--r--test/monniaux/math/rounding.c36
-rw-r--r--test/monniaux/math/test_gsl_pow.c44
-rwxr-xr-xtest/monniaux/mbedtls/download_and_patch.sh4
-rwxr-xr-xtest/monniaux/mbedtls/make.sh3
-rw-r--r--test/monniaux/mbedtls/mbedtls_Kalray.patch70
-rw-r--r--test/monniaux/memcpy/test_memcpy.c15
-rw-r--r--test/monniaux/micro-bunzip/Makefile53
-rw-r--r--test/monniaux/micro-bunzip/NOTES.txt5
-rw-r--r--test/monniaux/micro-bunzip/micro-bunzip.c525
-rw-r--r--test/monniaux/minisat/LICENSE20
-rw-r--r--test/monniaux/minisat/Makefile36
-rw-r--r--test/monniaux/minisat/Makefile.on_marte16
-rw-r--r--test/monniaux/minisat/Makefile.profiled64
-rw-r--r--test/monniaux/minisat/README15
l---------test/monniaux/minisat/clock.c1
l---------test/monniaux/minisat/cycles.h1
-rw-r--r--test/monniaux/minisat/k1c.inline_50.log14
-rw-r--r--test/monniaux/minisat/main.c222
-rw-r--r--test/monniaux/minisat/make.proto2
-rw-r--r--test/monniaux/minisat/solver.c1191
-rw-r--r--test/monniaux/minisat/solver.h142
l---------test/monniaux/minisat/sudoku.sat1
-rw-r--r--test/monniaux/minisat/vec.h84
-rw-r--r--test/monniaux/mod_int_mat/Makefile40
-rw-r--r--test/monniaux/mod_int_mat/int_mat.c218
-rw-r--r--test/monniaux/mod_int_mat/int_mat_run.c90
-rw-r--r--test/monniaux/mod_int_mat/modint.h51
-rw-r--r--test/monniaux/moves/array.c18
-rw-r--r--test/monniaux/multithreaded_volatile/Makefile18
-rw-r--r--test/monniaux/multithreaded_volatile/volatile.c30
-rw-r--r--test/monniaux/nand/nand.c48
-rw-r--r--test/monniaux/ncompress/Makefile4
-rw-r--r--test/monniaux/ncompress/README.md100
-rw-r--r--test/monniaux/ncompress/UNLICENSE24
-rw-r--r--test/monniaux/ncompress/compress42.c1948
-rw-r--r--test/monniaux/ncompress/patchlevel.h2
-rw-r--r--test/monniaux/number_theoretic_transform/Makefile3
-rw-r--r--test/monniaux/number_theoretic_transform/ntt.c163
-rw-r--r--test/monniaux/ocaml/Makefile6
-rw-r--r--test/monniaux/ocaml/Makefile.common30
-rw-r--r--test/monniaux/ocaml/VERSION4
-rw-r--r--test/monniaux/ocaml/byterun/.depend803
-rw-r--r--test/monniaux/ocaml/byterun/Makefile234
-rw-r--r--test/monniaux/ocaml/byterun/afl.c162
-rw-r--r--test/monniaux/ocaml/byterun/alloc.c247
-rw-r--r--test/monniaux/ocaml/byterun/array.c586
-rw-r--r--test/monniaux/ocaml/byterun/backtrace.c349
-rw-r--r--test/monniaux/ocaml/byterun/backtrace_prim.c457
-rw-r--r--test/monniaux/ocaml/byterun/bigarray.c1223
-rw-r--r--test/monniaux/ocaml/byterun/callback.c262
-rw-r--r--test/monniaux/ocaml/byterun/caml/address_class.h85
-rw-r--r--test/monniaux/ocaml/byterun/caml/alloc.h82
-rw-r--r--test/monniaux/ocaml/byterun/caml/backtrace.h136
-rw-r--r--test/monniaux/ocaml/byterun/caml/backtrace_prim.h91
-rw-r--r--test/monniaux/ocaml/byterun/caml/bigarray.h134
-rw-r--r--test/monniaux/ocaml/byterun/caml/callback.h63
-rw-r--r--test/monniaux/ocaml/byterun/caml/compact.h31
-rw-r--r--test/monniaux/ocaml/byterun/caml/compare.h25
-rw-r--r--test/monniaux/ocaml/byterun/caml/compatibility.h375
-rw-r--r--test/monniaux/ocaml/byterun/caml/config.h208
-rw-r--r--test/monniaux/ocaml/byterun/caml/custom.h73
-rw-r--r--test/monniaux/ocaml/byterun/caml/debugger.h117
-rw-r--r--test/monniaux/ocaml/byterun/caml/dynlink.h46
-rw-r--r--test/monniaux/ocaml/byterun/caml/exec.h65
-rw-r--r--test/monniaux/ocaml/byterun/caml/fail.h144
-rw-r--r--test/monniaux/ocaml/byterun/caml/finalise.h36
-rw-r--r--test/monniaux/ocaml/byterun/caml/fix_code.h45
-rw-r--r--test/monniaux/ocaml/byterun/caml/freelist.h38
-rw-r--r--test/monniaux/ocaml/byterun/caml/gc.h79
-rw-r--r--test/monniaux/ocaml/byterun/caml/gc_ctrl.h58
-rw-r--r--test/monniaux/ocaml/byterun/caml/globroots.h31
-rw-r--r--test/monniaux/ocaml/byterun/caml/hash.h39
-rw-r--r--test/monniaux/ocaml/byterun/caml/hooks.h42
-rw-r--r--test/monniaux/ocaml/byterun/caml/instrtrace.h35
-rw-r--r--test/monniaux/ocaml/byterun/caml/instruct.h68
-rw-r--r--test/monniaux/ocaml/byterun/caml/int64_emul.h293
-rw-r--r--test/monniaux/ocaml/byterun/caml/int64_format.h111
-rw-r--r--test/monniaux/ocaml/byterun/caml/int64_native.h67
-rw-r--r--test/monniaux/ocaml/byterun/caml/interp.h37
-rw-r--r--test/monniaux/ocaml/byterun/caml/intext.h207
-rw-r--r--test/monniaux/ocaml/byterun/caml/io.h126
-rw-r--r--test/monniaux/ocaml/byterun/caml/jumptbl.h40
-rw-r--r--test/monniaux/ocaml/byterun/caml/m.h86
-rw-r--r--test/monniaux/ocaml/byterun/caml/major_gc.h95
-rw-r--r--test/monniaux/ocaml/byterun/caml/md5.h47
-rw-r--r--test/monniaux/ocaml/byterun/caml/memory.h595
-rw-r--r--test/monniaux/ocaml/byterun/caml/minor_gc.h120
-rw-r--r--test/monniaux/ocaml/byterun/caml/misc.h525
-rw-r--r--test/monniaux/ocaml/byterun/caml/mlvalues.h377
-rw-r--r--test/monniaux/ocaml/byterun/caml/osdeps.h152
-rw-r--r--test/monniaux/ocaml/byterun/caml/prims.h40
-rw-r--r--test/monniaux/ocaml/byterun/caml/printexc.h35
-rw-r--r--test/monniaux/ocaml/byterun/caml/reverse.h92
-rw-r--r--test/monniaux/ocaml/byterun/caml/roots.h44
-rw-r--r--test/monniaux/ocaml/byterun/caml/s.h217
-rw-r--r--test/monniaux/ocaml/byterun/caml/signals.h59
-rw-r--r--test/monniaux/ocaml/byterun/caml/signals_machdep.h74
-rw-r--r--test/monniaux/ocaml/byterun/caml/spacetime.h203
-rw-r--r--test/monniaux/ocaml/byterun/caml/stack.h124
-rw-r--r--test/monniaux/ocaml/byterun/caml/stacks.h46
-rw-r--r--test/monniaux/ocaml/byterun/caml/startup.h52
-rw-r--r--test/monniaux/ocaml/byterun/caml/startup_aux.h44
-rw-r--r--test/monniaux/ocaml/byterun/caml/sys.h45
-rw-r--r--test/monniaux/ocaml/byterun/caml/ui.h32
-rw-r--r--test/monniaux/ocaml/byterun/caml/version.h6
-rw-r--r--test/monniaux/ocaml/byterun/caml/weak.h93
-rw-r--r--test/monniaux/ocaml/byterun/compact.c563
-rw-r--r--test/monniaux/ocaml/byterun/compare.c363
-rw-r--r--test/monniaux/ocaml/byterun/custom.c124
-rw-r--r--test/monniaux/ocaml/byterun/debugger.c454
-rw-r--r--test/monniaux/ocaml/byterun/dynlink.c300
-rw-r--r--test/monniaux/ocaml/byterun/extern.c925
-rw-r--r--test/monniaux/ocaml/byterun/fail.c204
-rw-r--r--test/monniaux/ocaml/byterun/finalise.c445
-rw-r--r--test/monniaux/ocaml/byterun/fix_code.c195
-rw-r--r--test/monniaux/ocaml/byterun/floats.c720
-rw-r--r--test/monniaux/ocaml/byterun/freelist.c621
-rw-r--r--test/monniaux/ocaml/byterun/gc_ctrl.c691
-rw-r--r--test/monniaux/ocaml/byterun/globroots.c291
-rw-r--r--test/monniaux/ocaml/byterun/hash.c419
-rw-r--r--test/monniaux/ocaml/byterun/instrtrace.c269
-rw-r--r--test/monniaux/ocaml/byterun/intern.c1048
-rw-r--r--test/monniaux/ocaml/byterun/interp.c1172
-rw-r--r--test/monniaux/ocaml/byterun/ints.c830
-rw-r--r--test/monniaux/ocaml/byterun/io.c828
-rw-r--r--test/monniaux/ocaml/byterun/lexing.c233
-rw-r--r--test/monniaux/ocaml/byterun/main.c60
-rw-r--r--test/monniaux/ocaml/byterun/major_gc.c943
-rw-r--r--test/monniaux/ocaml/byterun/md5.c325
-rw-r--r--test/monniaux/ocaml/byterun/memory.c1016
-rw-r--r--test/monniaux/ocaml/byterun/meta.c234
-rw-r--r--test/monniaux/ocaml/byterun/minor_gc.c558
-rw-r--r--test/monniaux/ocaml/byterun/misc.c276
-rw-r--r--test/monniaux/ocaml/byterun/obj.c390
-rw-r--r--test/monniaux/ocaml/byterun/parsing.c304
-rw-r--r--test/monniaux/ocaml/byterun/prims.c1153
-rw-r--r--test/monniaux/ocaml/byterun/printexc.c155
-rw-r--r--test/monniaux/ocaml/byterun/roots.c120
-rw-r--r--test/monniaux/ocaml/byterun/signals.c398
-rw-r--r--test/monniaux/ocaml/byterun/signals_byt.c101
-rw-r--r--test/monniaux/ocaml/byterun/spacetime.c38
-rw-r--r--test/monniaux/ocaml/byterun/stacks.c119
-rw-r--r--test/monniaux/ocaml/byterun/startup.c531
-rw-r--r--test/monniaux/ocaml/byterun/startup_aux.c168
-rw-r--r--test/monniaux/ocaml/byterun/str.c474
-rw-r--r--test/monniaux/ocaml/byterun/sys.c723
-rw-r--r--test/monniaux/ocaml/byterun/toto13996
-rw-r--r--test/monniaux/ocaml/byterun/unix.c459
-rw-r--r--test/monniaux/ocaml/byterun/weak.c427
-rw-r--r--test/monniaux/ocaml/config/Makefile109
-rw-r--r--test/monniaux/ocaml/config/Makefile-templ202
-rw-r--r--test/monniaux/ocaml/config/m-nt.h65
-rw-r--r--test/monniaux/ocaml/config/m-templ.h83
-rw-r--r--test/monniaux/ocaml/config/s-nt.h41
-rw-r--r--test/monniaux/ocaml/config/s-templ.h214
-rwxr-xr-xtest/monniaux/ocaml/examples/quicksortbin0 -> 146138 bytes
-rw-r--r--test/monniaux/ocaml/examples/quicksort.ml11
-rwxr-xr-xtest/monniaux/ocaml/tools/make-version-header.sh55
-rw-r--r--test/monniaux/pcre2-10.32/Makefile35
-rw-r--r--test/monniaux/pcre2-10.32/config.h366
-rw-r--r--test/monniaux/pcre2-10.32/pcre2.h974
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_auto_possess.c1322
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_chartables.c198
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_compile.c9921
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_config.c246
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_context.c476
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_convert.c1182
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_dfa_match.c3864
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_error.c334
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_extuni.c148
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_find_bracket.c219
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_internal.h1945
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_intmodedep.h918
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_jit_compile.c12708
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_jit_match.c189
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_jit_misc.c227
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_maketables.c150
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_match.c6860
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_match_data.c147
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_newline.c243
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_ord2utf.c120
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_pattern_info.c432
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_printint.c832
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_serialize.c286
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_string_utils.c237
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_study.c1635
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_substitute.c885
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_substring.c547
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_tables.c827
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_ucd.c4189
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_ucp.h288
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_valid_utf.c398
-rw-r--r--test/monniaux/pcre2-10.32/pcre2_xclass.c271
-rw-r--r--test/monniaux/pcre2-10.32/pcre2posix.c375
-rw-r--r--test/monniaux/pcre2-10.32/pcre2posix.h151
-rw-r--r--test/monniaux/pcre2-10.32/pcre2test.c8800
-rw-r--r--test/monniaux/pcre2-10.32/testdata/testinput64958
-rw-r--r--test/monniaux/pcre2-10.32/testdata/testoutput67786
-rw-r--r--test/monniaux/picosat-965/LICENSE20
-rw-r--r--test/monniaux/picosat-965/Makefile11
-rw-r--r--test/monniaux/picosat-965/NEWS162
-rw-r--r--test/monniaux/picosat-965/README5
-rw-r--r--test/monniaux/picosat-965/VERSION1
-rw-r--r--test/monniaux/picosat-965/app.c1214
-rw-r--r--test/monniaux/picosat-965/config.h3
-rwxr-xr-xtest/monniaux/picosat-965/configure.sh150
-rw-r--r--test/monniaux/picosat-965/main.c25
-rwxr-xr-xtest/monniaux/picosat-965/mkconfig.sh35
-rw-r--r--test/monniaux/picosat-965/onefile/picosat.c25
-rwxr-xr-xtest/monniaux/picosat-965/onefile/testcmp.sh146
-rw-r--r--test/monniaux/picosat-965/picogcnf.c165
-rw-r--r--test/monniaux/picosat-965/picomcs.c334
-rw-r--r--test/monniaux/picosat-965/picomus.c413
-rw-r--r--test/monniaux/picosat-965/picosat.c8508
-rw-r--r--test/monniaux/picosat-965/picosat.h660
-rw-r--r--test/monniaux/picosat-965/small.dat2
-rw-r--r--test/monniaux/picosat-965/sudoku.sat20519
-rw-r--r--test/monniaux/picosat-965/tiny.dat2
-rw-r--r--test/monniaux/picosat-965/version.c14
-rw-r--r--test/monniaux/predicated/predicated_run.c16
-rw-r--r--test/monniaux/profiling/compcert_profiling.datbin0 -> 96 bytes
-rw-r--r--test/monniaux/profiling/profiling_call.c27
-rwxr-xr-xtest/monniaux/profiling/test_profilingbin0 -> 14128 bytes
-rw-r--r--test/monniaux/profiling/test_profiling.c15
-rw-r--r--test/monniaux/quest/Makefile24
-rw-r--r--test/monniaux/quicksort/Makefile3
-rw-r--r--test/monniaux/quicksort/quicksort.c42
-rw-r--r--test/monniaux/quicksort/quicksort.ccomp.k1c.s_modified5285
-rw-r--r--test/monniaux/quicksort/quicksort.ccomp.k1c.s_orig301
-rw-r--r--test/monniaux/quicksort/quicksort.h7
-rw-r--r--test/monniaux/quicksort/quicksort_run.c22
-rw-r--r--test/monniaux/reduced_picosat/reduced_picosat.c23
-rw-r--r--test/monniaux/reduced_picosat/test_a.s10
-rw-r--r--test/monniaux/reduced_picosat/test_b.c9
-rwxr-xr-xtest/monniaux/reduced_picosat/testcmp.sh146
-rw-r--r--test/monniaux/regalloc/bigspill.c21
-rw-r--r--test/monniaux/regalloc/invalid_register_allocation.c12
-rw-r--r--test/monniaux/regalloc/invalid_register_allocation2.c7
-rw-r--r--test/monniaux/regalloc/spill_queue_issues.c5
-rw-r--r--test/monniaux/rotate/rotate.c10
-rw-r--r--test/monniaux/rules.mk162
-rwxr-xr-xtest/monniaux/run_benches.sh27
-rw-r--r--test/monniaux/sandbox/Makefile148
-rw-r--r--test/monniaux/sandbox/example.c42
-rw-r--r--test/monniaux/sandbox/f.c3
-rw-r--r--test/monniaux/sandbox/f.h1
-rw-r--r--test/monniaux/sandbox/sha-256.c387
-rw-r--r--test/monniaux/sandbox/sha-256.h2
-rw-r--r--test/monniaux/sandbox/sha-256_run.c286
-rw-r--r--test/monniaux/scheduling/mal_schedule.c14
-rw-r--r--test/monniaux/send_through/Makefile10
-rw-r--r--test/monniaux/send_through/send_through.h6
-rw-r--r--test/monniaux/send_through/send_through_ccomp.c20
-rw-r--r--test/monniaux/send_through/send_through_gcc.c18
-rw-r--r--test/monniaux/sha-2/LICENSE24
-rw-r--r--test/monniaux/sha-2/Makefile3
-rw-r--r--test/monniaux/sha-2/README.md99
-rw-r--r--test/monniaux/sha-2/sha-256.c374
-rw-r--r--test/monniaux/sha-2/sha-256.h1
-rw-r--r--test/monniaux/sha-2/sha-256_run.c285
-rw-r--r--test/monniaux/sizeof/sizeof.c46
-rw-r--r--test/monniaux/slow_globals/slow_globals.c15
-rw-r--r--test/monniaux/slow_globals/slow_globals2.c2
-rw-r--r--test/monniaux/tacle-bench-lift/Makefile4
-rw-r--r--test/monniaux/tacle-bench-lift/README.md1
-rw-r--r--test/monniaux/tacle-bench-lift/kill_pragma.h1
-rw-r--r--test/monniaux/tacle-bench-lift/lift.c145
-rw-r--r--test/monniaux/tacle-bench-lift/liftlibcontrol.c277
-rw-r--r--test/monniaux/tacle-bench-lift/liftlibcontrol.h64
-rw-r--r--test/monniaux/tacle-bench-lift/liftlibio.c65
-rw-r--r--test/monniaux/tacle-bench-lift/liftlibio.h27
-rw-r--r--test/monniaux/tacle-bench-powerwindow/Makefile4
-rw-r--r--test/monniaux/tacle-bench-powerwindow/changeLog42
-rw-r--r--test/monniaux/tacle-bench-powerwindow/kill_pragma.h1
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow.c627
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow.h22
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h139
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h32
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h126
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h32
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h126
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h31
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h126
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_private.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h33
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion.h72
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h30
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce.h96
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_private.h46
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_types.h31
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_model_reference_types.h51
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control.h135
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h52
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h33
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_continuous.h136
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_solver.h243
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtwtypes.h194
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_tmwtypes.h821
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_DRV.c291
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackL.c163
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackR.c163
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_Front.c163
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_const_params.c24
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_controlexclusion.c85
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_debounce.c210
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_inputs.c46
-rw-r--r--test/monniaux/tacle-bench-powerwindow/powerwindow_powerwindow_control.c523
-rw-r--r--test/monniaux/tacle-bench-powerwindow/wcclib.c12
-rw-r--r--test/monniaux/tacle-bench-powerwindow/wcclib.h14
-rw-r--r--test/monniaux/ternary/make.proto2
-rw-r--r--test/monniaux/ternary/ternary.c24
-rw-r--r--test/monniaux/ternary_builtin/ternary_builtin.c10
-rw-r--r--test/monniaux/thread_local/thread_local.c13
-rw-r--r--test/monniaux/thread_local/thread_local2.c18
-rw-r--r--test/monniaux/tiff-4.0.10/Makefile7
-rw-r--r--test/monniaux/tiff-4.0.10/example_bw.pbmbin0 -> 18262 bytes
-rw-r--r--test/monniaux/tiff-4.0.10/example_bw.pbm.bz2bin0 -> 4301 bytes
-rw-r--r--test/monniaux/tiff-4.0.10/make.proto4
-rw-r--r--test/monniaux/tiff-4.0.10/ppm2tiff.c411
-rw-r--r--test/monniaux/tiff-4.0.10/t4.h290
-rw-r--r--test/monniaux/tiff-4.0.10/tif_aux.c374
-rw-r--r--test/monniaux/tiff-4.0.10/tif_close.c138
-rw-r--r--test/monniaux/tiff-4.0.10/tif_codec.c171
-rw-r--r--test/monniaux/tiff-4.0.10/tif_color.c307
-rw-r--r--test/monniaux/tiff-4.0.10/tif_compress.c302
-rw-r--r--test/monniaux/tiff-4.0.10/tif_config.h369
-rw-r--r--test/monniaux/tiff-4.0.10/tif_config.vc.h137
-rw-r--r--test/monniaux/tiff-4.0.10/tif_config.wince.h69
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dir.c1768
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dir.h311
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dirinfo.c1081
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dirread.c5874
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dirwrite.c3025
-rw-r--r--test/monniaux/tiff-4.0.10/tif_dumpmode.c141
-rw-r--r--test/monniaux/tiff-4.0.10/tif_error.c86
-rw-r--r--test/monniaux/tiff-4.0.10/tif_extension.c116
-rw-r--r--test/monniaux/tiff-4.0.10/tif_fax3.c1646
-rw-r--r--test/monniaux/tiff-4.0.10/tif_fax3.h538
-rw-r--r--test/monniaux/tiff-4.0.10/tif_fax3sm.c1260
-rw-r--r--test/monniaux/tiff-4.0.10/tif_flush.c116
-rw-r--r--test/monniaux/tiff-4.0.10/tif_getimage.c3046
-rw-r--r--test/monniaux/tiff-4.0.10/tif_jbig.c232
-rw-r--r--test/monniaux/tiff-4.0.10/tif_jpeg.c2599
-rw-r--r--test/monniaux/tiff-4.0.10/tif_jpeg_12.c69
-rw-r--r--test/monniaux/tiff-4.0.10/tif_luv.c1765
-rw-r--r--test/monniaux/tiff-4.0.10/tif_lzma.c500
-rw-r--r--test/monniaux/tiff-4.0.10/tif_lzw.c1230
-rw-r--r--test/monniaux/tiff-4.0.10/tif_next.c187
-rw-r--r--test/monniaux/tiff-4.0.10/tif_ojpeg.c2561
-rw-r--r--test/monniaux/tiff-4.0.10/tif_open.c723
-rw-r--r--test/monniaux/tiff-4.0.10/tif_packbits.c309
-rw-r--r--test/monniaux/tiff-4.0.10/tif_pixarlog.c1483
-rw-r--r--test/monniaux/tiff-4.0.10/tif_predict.c879
-rw-r--r--test/monniaux/tiff-4.0.10/tif_predict.h81
-rw-r--r--test/monniaux/tiff-4.0.10/tif_print.c720
-rw-r--r--test/monniaux/tiff-4.0.10/tif_read.c1577
-rw-r--r--test/monniaux/tiff-4.0.10/tif_strip.c387
-rw-r--r--test/monniaux/tiff-4.0.10/tif_swab.c310
-rw-r--r--test/monniaux/tiff-4.0.10/tif_thunder.c206
-rw-r--r--test/monniaux/tiff-4.0.10/tif_tile.c320
-rw-r--r--test/monniaux/tiff-4.0.10/tif_unix.c384
-rw-r--r--test/monniaux/tiff-4.0.10/tif_version.c39
-rw-r--r--test/monniaux/tiff-4.0.10/tif_warning.c87
-rw-r--r--test/monniaux/tiff-4.0.10/tif_webp.c684
-rw-r--r--test/monniaux/tiff-4.0.10/tif_write.c834
-rw-r--r--test/monniaux/tiff-4.0.10/tif_zip.c474
-rw-r--r--test/monniaux/tiff-4.0.10/tif_zstd.c440
-rw-r--r--test/monniaux/tiff-4.0.10/tiff.h695
-rw-r--r--test/monniaux/tiff-4.0.10/tiffconf.h119
-rw-r--r--test/monniaux/tiff-4.0.10/tiffconf.vc.h152
-rw-r--r--test/monniaux/tiff-4.0.10/tiffconf.wince.h110
-rw-r--r--test/monniaux/tiff-4.0.10/tiffio.h558
-rw-r--r--test/monniaux/tiff-4.0.10/tiffiop.h454
-rw-r--r--test/monniaux/tiff-4.0.10/tiffvers.h9
-rw-r--r--test/monniaux/tiff-4.0.10/uvcode.h180
-rw-r--r--test/monniaux/tiny-AES-c/Makefile61
-rw-r--r--test/monniaux/tiny-AES-c/README.md80
-rw-r--r--test/monniaux/tiny-AES-c/aes.c571
-rw-r--r--test/monniaux/tiny-AES-c/aes.h90
-rw-r--r--test/monniaux/tiny-AES-c/aes.hpp12
-rw-r--r--test/monniaux/tiny-AES-c/library.json13
-rw-r--r--test/monniaux/tiny-AES-c/test.c316
-rw-r--r--test/monniaux/tiny-AES-c/unlicense.txt24
-rw-r--r--test/monniaux/too_slow/Makefile3
-rw-r--r--test/monniaux/too_slow/memset_from_bitsliced-aes.c43
-rw-r--r--test/monniaux/uzlib/LICENSE65
-rw-r--r--test/monniaux/uzlib/Makefile4
-rw-r--r--test/monniaux/uzlib/README.md310
-rwxr-xr-xtest/monniaux/uzlib/compile.sh1
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile31
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.b3231
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.dj230
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.dmc31
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.mgw28
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.vc28
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/makefile.wat31
-rw-r--r--test/monniaux/uzlib/examples/tgunzip/tgunzip.c167
-rw-r--r--test/monniaux/uzlib/examples/tgzip/makefile31
-rw-r--r--test/monniaux/uzlib/examples/tgzip/tgzip.c120
-rw-r--r--test/monniaux/uzlib/lib/empty.dir1
-rw-r--r--test/monniaux/uzlib/src/adler32.c78
-rw-r--r--test/monniaux/uzlib/src/crc32.c63
-rw-r--r--test/monniaux/uzlib/src/defl_static.c305
-rw-r--r--test/monniaux/uzlib/src/defl_static.h45
-rw-r--r--test/monniaux/uzlib/src/genlz77.c124
-rw-r--r--test/monniaux/uzlib/src/makefile36
-rw-r--r--test/monniaux/uzlib/src/makefile.b3234
-rw-r--r--test/monniaux/uzlib/src/makefile.dj233
-rw-r--r--test/monniaux/uzlib/src/makefile.dmc32
-rw-r--r--test/monniaux/uzlib/src/makefile.mgw31
-rw-r--r--test/monniaux/uzlib/src/makefile.vc30
-rw-r--r--test/monniaux/uzlib/src/makefile.wat35
-rw-r--r--test/monniaux/uzlib/src/nasm/crc32.nas118
-rw-r--r--test/monniaux/uzlib/src/nasm/nasmlcm.inc326
-rw-r--r--test/monniaux/uzlib/src/nasm/tinfzlib.nas160
-rw-r--r--test/monniaux/uzlib/src/tinf.h3
-rw-r--r--test/monniaux/uzlib/src/tinf_compat.h9
-rw-r--r--test/monniaux/uzlib/src/tinfgzip.c110
-rw-r--r--test/monniaux/uzlib/src/tinflate.c659
-rw-r--r--test/monniaux/uzlib/src/tinfzlib.c66
-rw-r--r--test/monniaux/uzlib/src/uzlib.h169
-rw-r--r--test/monniaux/uzlib/src/uzlib_conf.h22
-rw-r--r--test/monniaux/uzlib/tests/Makefile12
-rwxr-xr-xtest/monniaux/uzlib/tests/clang-ubsan-build.sh5
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs.ref232
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000000,sig:11,src:000000,op:flip1,pos:10bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000001,sig:11,src:000000,op:flip1,pos:10bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000002,sig:11,src:000000,op:flip1,pos:10bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000003,sig:11,src:000000,op:flip1,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000004,sig:11,src:000000,op:flip1,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000005,sig:11,src:000000,op:flip1,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000006,sig:11,src:000000,op:flip1,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000007,sig:11,src:000000,op:flip2,pos:11bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000008,sig:11,src:000000,op:flip2,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000009,sig:11,src:000000,op:flip2,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000010,sig:11,src:000000,op:flip4,pos:10bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000011,sig:11,src:000000,op:flip8,pos:10bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000012,sig:11,src:000000,op:arith8,pos:11,val:+6bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000013,sig:11,src:000000,op:arith8,pos:12,val:-9bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000014,sig:11,src:000000,op:havoc,rep:16bin0 -> 28 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000015,sig:11,src:000000,op:havoc,rep:2bin0 -> 27 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000016,sig:11,src:000000,op:havoc,rep:4bin0 -> 33 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000017,sig:11,src:000000,op:havoc,rep:4bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000018,sig:11,src:000000,op:havoc,rep:2bin0 -> 25 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000019,sig:11,src:000000,op:havoc,rep:8bin0 -> 33 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000020,sig:11,src:000000,op:havoc,rep:8bin0 -> 42 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000021,sig:11,src:000000,op:havoc,rep:8bin0 -> 34 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000022,sig:11,src:000000,op:havoc,rep:4bin0 -> 62 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000023,sig:06,src:000000,op:havoc,rep:8bin0 -> 60 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000024,sig:06,src:000000,op:havoc,rep:8bin0 -> 60 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000025,sig:06,src:000033,op:havoc,rep:8bin0 -> 64 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000026,sig:11,src:000033,op:havoc,rep:2bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000027,sig:11,src:000040,op:havoc,rep:2bin0 -> 26 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000028,sig:11,src:000070,op:havoc,rep:2bin0 -> 40 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000029,sig:11,src:000070,op:havoc,rep:8bin0 -> 32 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000030,sig:11,src:000070,op:havoc,rep:16bin0 -> 41 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000031,sig:11,src:000090,op:havoc,rep:4bin0 -> 29 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000032,sig:11,src:000096,op:flip1,pos:10bin0 -> 25 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000033,sig:11,src:000096,op:int16,pos:14,val:+1bin0 -> 25 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000034,sig:11,src:000104,op:int32,pos:16,val:-32768bin0 -> 30 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000035,sig:11,src:000118,op:flip1,pos:58bin0 -> 60 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000036,sig:11,src:000187,op:flip1,pos:10bin0 -> 52 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000037,sig:11,src:000187,op:flip1,pos:12bin0 -> 52 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000038,sig:11,src:000187,op:flip1,pos:15bin0 -> 52 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000000,sig:11,src:000001,op:flip1,pos:3bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000001,sig:11,src:000001,op:flip1,pos:30
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000002,sig:11,src:000001,op:flip2,pos:10bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000003,sig:11,src:000001,op:flip4,pos:10bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000004,sig:11,src:000001,op:flip4,pos:12bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000005,sig:11,src:000001,op:flip16,pos:10bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000008,sig:11,src:000001,op:havoc,rep:321
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000009,sig:11,src:000001,op:havoc,rep:16bin0 -> 36 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000010,sig:07,src:000001,op:havoc,rep:2bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000011,sig:11,src:000001,op:havoc,rep:16bin0 -> 48 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000012,sig:11,src:000001,op:havoc,rep:4bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000013,sig:11,src:000001,op:havoc,rep:4bin0 -> 29 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000014,sig:11,src:000001,op:havoc,rep:8bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000015,sig:11,src:000001,op:havoc,rep:32bin0 -> 26 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000016,sig:07,src:000001,op:havoc,rep:2bin0 -> 30 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000017,sig:11,src:000001,op:havoc,rep:2bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000018,sig:11,src:000001,op:havoc,rep:8bin0 -> 39 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000019,sig:11,src:000001,op:havoc,rep:8bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000020,sig:11,src:000001,op:havoc,rep:8bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000021,sig:11,src:000001,op:havoc,rep:4bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000022,sig:11,src:000001,op:havoc,rep:4bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000023,sig:06,src:000001,op:havoc,rep:4bin0 -> 15 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000024,sig:11,src:000001,op:havoc,rep:8bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000025,sig:11,src:000005,op:flip1,pos:22bin0 -> 48 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000026,sig:11,src:000005,op:havoc,rep:16bin0 -> 33 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000027,sig:11,src:000005,op:havoc,rep:2bin0 -> 48 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000028,sig:11,src:000005,op:havoc,rep:4bin0 -> 36 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000029,sig:11,src:000005,op:havoc,rep:8bin0 -> 99 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000030,sig:11,src:000005,op:havoc,rep:8bin0 -> 30 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000031,sig:11,src:000005,op:havoc,rep:4bin0 -> 23 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000032,sig:11,src:000005,op:havoc,rep:8bin0 -> 48 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000033,sig:11,src:000005,op:havoc,rep:16bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000034,sig:11,src:000005,op:havoc,rep:8bin0 -> 73 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000035,sig:11,src:000005,op:havoc,rep:8bin0 -> 40 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000000,sig:11,src:000001,op:flip1,pos:3bin0 -> 15 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000002,sig:11,src:000001,op:flip2,pos:10bin0 -> 16 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000003,sig:11,src:000001,op:flip4,pos:10bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000004,sig:11,src:000001,op:flip4,pos:12bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000005,sig:11,src:000001,op:flip16,pos:10bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21bin0 -> 14 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000bin0 -> 16 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000009,sig:11,src:000001,op:havoc,rep:16bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000010,sig:07,src:000001,op:havoc,rep:2bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000011,sig:11,src:000001,op:havoc,rep:16bin0 -> 42 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000012,sig:11,src:000001,op:havoc,rep:4bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000013,sig:11,src:000001,op:havoc,rep:4bin0 -> 15 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000014,sig:11,src:000001,op:havoc,rep:8bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000015,sig:11,src:000001,op:havoc,rep:32bin0 -> 26 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000016,sig:07,src:000001,op:havoc,rep:2bin0 -> 14 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000017,sig:11,src:000001,op:havoc,rep:2bin0 -> 15 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000018,sig:11,src:000001,op:havoc,rep:8bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000019,sig:11,src:000001,op:havoc,rep:8bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000020,sig:11,src:000001,op:havoc,rep:8bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000021,sig:11,src:000001,op:havoc,rep:4bin0 -> 13 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000022,sig:11,src:000001,op:havoc,rep:4bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000023,sig:06,src:000001,op:havoc,rep:4bin0 -> 15 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000024,sig:11,src:000001,op:havoc,rep:8bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000025,sig:11,src:000005,op:flip1,pos:22bin0 -> 26 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000026,sig:11,src:000005,op:havoc,rep:16bin0 -> 33 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000027,sig:11,src:000005,op:havoc,rep:2bin0 -> 45 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000028,sig:11,src:000005,op:havoc,rep:4bin0 -> 4 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000029,sig:11,src:000005,op:havoc,rep:8bin0 -> 14 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000030,sig:11,src:000005,op:havoc,rep:8bin0 -> 28 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000031,sig:11,src:000005,op:havoc,rep:4bin0 -> 23 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000032,sig:11,src:000005,op:havoc,rep:8bin0 -> 47 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000033,sig:11,src:000005,op:havoc,rep:16bin0 -> 13 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000034,sig:11,src:000005,op:havoc,rep:8bin0 -> 73 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000035,sig:11,src:000005,op:havoc,rep:8bin0 -> 36 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000000,sig:11,src:000001,op:havoc,rep:4bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000001,sig:11,src:000001,op:havoc,rep:2bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000002,sig:11,src:000001,op:havoc,rep:2bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000003,sig:11,src:000002,op:havoc,rep:4bin0 -> 51 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000000,sig:11,src:000001,op:havoc,rep:4bin0 -> 78 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000001,sig:11,src:000001,op:havoc,rep:2bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000002,sig:11,src:000001,op:havoc,rep:2bin0 -> 34 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000003,sig:11,src:000001,op:havoc,rep:2bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000004,sig:11,src:000002,op:havoc,rep:4bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000005,sig:11,src:000005,op:flip1,pos:20bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000006,sig:11,src:000005,op:flip1,pos:22bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000007,sig:11,src:000005,op:flip4,pos:24bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000008,sig:11,src:000005,op:arith8,pos:3,val:+10bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000009,sig:11,src:000005,op:arith8,pos:3,val:+18bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000010,sig:11,src:000005,op:int8,pos:23,val:-1bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000011,sig:11,src:000005,op:havoc,rep:4bin0 -> 74 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000012,sig:11,src:000005,op:havoc,rep:16bin0 -> 43 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000013,sig:11,src:000005,op:havoc,rep:8bin0 -> 65 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000014,sig:11,src:000005,op:havoc,rep:2bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000015,sig:11,src:000005,op:havoc,rep:2bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000016,sig:11,src:000005,op:havoc,rep:2bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000017,sig:11,src:000005,op:havoc,rep:2bin0 -> 50 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000018,sig:11,src:000082,op:flip32,pos:19bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000019,sig:11,src:000082,op:int32,pos:19,val:be:+65535bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000020,sig:11,src:000155,op:flip2,pos:28bin0 -> 34 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000021,sig:11,src:000166,op:flip2,pos:10bin0 -> 34 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000022,sig:11,src:000156,op:int16,pos:26,val:-129bin0 -> 30 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000023,sig:11,src:000193,op:int16,pos:31,val:be:+32bin0 -> 38 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000024,sig:11,src:000230,op:havoc,rep:2bin0 -> 55 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000025,sig:11,src:000230,op:havoc,rep:2bin0 -> 36 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000026,sig:11,src:000245,op:havoc,rep:2bin0 -> 32 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000027,sig:11,src:000254,op:flip1,pos:32bin0 -> 45 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000028,sig:11,src:000079,op:int32,pos:20,val:-128bin0 -> 28 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000029,sig:11,src:000247,op:havoc,rep:4bin0 -> 48 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000030,sig:11,src:000257,op:havoc,rep:2bin0 -> 79 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000031,sig:11,src:000257,op:havoc,rep:2bin0 -> 45 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000032,sig:11,src:000248,op:havoc,rep:2bin0 -> 36 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000033,sig:11,src:000291,op:havoc,rep:2bin0 -> 64 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000034,sig:11,src:000301,op:havoc,rep:64bin0 -> 1283 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000035,sig:11,src:000252,op:havoc,rep:2bin0 -> 42 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000036,sig:11,src:000310,op:arith32,pos:20,val:be:-8bin0 -> 92 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000037,sig:11,src:000319+000163,op:splice,rep:2bin0 -> 32 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000038,sig:11,src:000320,op:flip1,pos:27bin0 -> 60 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000039,sig:11,src:000320,op:ext_AO,pos:43bin0 -> 60 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000040,sig:11,src:000326,op:havoc,rep:32bin0 -> 286 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000041,sig:11,src:000016,op:havoc,rep:4bin0 -> 16 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000042,sig:11,src:000205+000322,op:splice,rep:64bin0 -> 1314 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000043,sig:11,src:000001+000321,op:splice,rep:4bin0 -> 64 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000000,sig:11,src:000001,op:havoc,rep:2bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000001,sig:11,src:000001,op:havoc,rep:2bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000002,sig:11,src:000001,op:havoc,rep:2bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000003,sig:11,src:000001,op:havoc,rep:4bin0 -> 16 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000004,sig:11,src:000034,op:int32,pos:44,val:+1000bin0 -> 51 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000005,sig:11,src:000063,op:int32,pos:17,val:+1000bin0 -> 24 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000006,sig:11,src:000019+000004,op:splice,rep:2bin0 -> 23 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000007,sig:11,src:000047+000264,op:splice,rep:2bin0 -> 21 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000008,sig:11,src:000194+000266,op:splice,rep:2bin0 -> 31 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000009,sig:11,src:000134+000005,op:splice,rep:2bin0 -> 49 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000010,sig:11,src:000220+000226,op:splice,rep:8bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000000,sig:06,src:000001,op:flip8,pos:12bin0 -> 21 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000001,sig:06,src:000001,op:havoc,rep:16bin0 -> 78 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000002,sig:06,src:000001,op:havoc,rep:4bin0 -> 37 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000003,sig:06,src:000001,op:havoc,rep:4bin0 -> 25 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000004,sig:06,src:000001,op:havoc,rep:4bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000005,sig:06,src:000012,op:flip1,pos:13bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000006,sig:06,src:000012,op:havoc,rep:16bin0 -> 38 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000007,sig:06,src:000108,op:arith16,pos:14,val:-2bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000008,sig:06,src:000003,op:havoc,rep:4bin0 -> 88 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000009,sig:06,src:000140,op:flip1,pos:15bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000010,sig:06,src:000092,op:flip4,pos:3bin0 -> 42 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000011,sig:06,src:000155,op:havoc,rep:8bin0 -> 85 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000012,sig:06,src:000163+000116,op:splice,rep:8bin0 -> 399 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000013,sig:06,src:000171,op:havoc,rep:2bin0 -> 28 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000014,sig:06,src:000170,op:havoc,rep:8bin0 -> 103 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000001,sig:06,src:000001,op:havoc,rep:16bin0 -> 54 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000002,sig:06,src:000001,op:havoc,rep:4bin0 -> 25 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000003,sig:06,src:000001,op:havoc,rep:8bin0 -> 21 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000004,sig:06,src:000001,op:havoc,rep:8bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000005,sig:06,src:000001,op:havoc,rep:4bin0 -> 21 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000007,sig:06,src:000121,op:arith8,pos:12,val:+26bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000008,sig:06,src:000137,op:flip1,pos:58bin0 -> 73 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000000,sig:06,src:000001,op:havoc,rep:8bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000001,sig:06,src:000019,op:havoc,rep:4bin0 -> 16 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000002,sig:06,src:000021,op:havoc,rep:8bin0 -> 44 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/08/id:000000,sig:06,src:000157,op:havoc,rep:4bin0 -> 35 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000000,sig:11,src:000001,op:int16,pos:11,val:be:-128bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000001,sig:11,src:000001,op:havoc,rep:2bin0 -> 17 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000002,sig:11,src:000023,op:flip2,pos:12bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000003,sig:11,src:000023,op:arith8,pos:14,val:-28bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000004,sig:11,src:000075,op:flip4,pos:61bin0 -> 75 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000005,sig:11,src:000075,op:arith8,pos:57,val:-5bin0 -> 75 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000006,sig:11,src:000075,op:arith16,pos:62,val:+28bin0 -> 75 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000007,sig:11,src:000075,op:havoc,rep:32bin0 -> 91 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000008,sig:11,src:000133,op:arith8,pos:13,val:-34bin0 -> 18 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000009,sig:11,src:000133,op:havoc,rep:2bin0 -> 22 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000010,sig:11,src:000133,op:havoc,rep:4bin0 -> 28 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000011,sig:11,src:000151,op:havoc,rep:2bin0 -> 68 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000012,sig:11,src:000212,op:arith8,pos:45,val:+9bin0 -> 87 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000013,sig:11,src:000212,op:int32,pos:15,val:+32767bin0 -> 87 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000014,sig:11,src:000212,op:havoc,rep:2bin0 -> 87 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000015,sig:11,src:000087,op:arith8,pos:20,val:+11bin0 -> 40 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000016,sig:11,src:000098,op:flip1,pos:13bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000017,sig:11,src:000098,op:flip2,pos:13bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000018,sig:11,src:000098,op:flip4,pos:12bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000019,sig:11,src:000098,op:havoc,rep:2bin0 -> 30 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000020,sig:11,src:000133,op:havoc,rep:2bin0 -> 32 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000021,sig:11,src:000134,op:int16,pos:13,val:be:-128bin0 -> 20 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000022,sig:11,src:000218,op:flip2,pos:40bin0 -> 114 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000023,sig:11,src:000218,op:int16,pos:111,val:+0bin0 -> 114 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000025,sig:11,src:000235,op:havoc,rep:8bin0 -> 71 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000026,sig:11,src:000143,op:arith8,pos:19,val:+3bin0 -> 52 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000027,sig:11,src:000024+000141,op:splice,rep:4bin0 -> 66 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000028,sig:11,src:000219,op:arith16,pos:62,val:be:-23bin0 -> 71 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000031,sig:11,src:000256,op:flip2,pos:480bin0 -> 501 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000032,sig:11,src:000256,op:flip4,pos:248bin0 -> 501 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000033,sig:11,src:000256,op:flip4,pos:364bin0 -> 501 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000034,sig:06,src:000256,op:arith8,pos:499,val:+8bin0 -> 501 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000036,sig:11,src:000256+000131,op:splice,rep:8bin0 -> 1411 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000037,sig:11,src:000260,op:flip2,pos:264bin0 -> 400 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000038,sig:11,src:000260,op:arith16,pos:254,val:-29bin0 -> 400 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000039,sig:11,src:000260,op:int16,pos:397,val:+0bin0 -> 400 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000040,sig:11,src:000272+000023,op:splice,rep:4bin0 -> 19 bytes
-rw-r--r--test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000041,sig:11,src:000253,op:havoc,rep:8bin0 -> 858 bytes
-rwxr-xr-xtest/monniaux/uzlib/tests/run-decomp-bad-inputs.sh22
-rw-r--r--test/monniaux/varargs/Makefile9
-rw-r--r--test/monniaux/varargs/varargs.c34
-rwxr-xr-xtest/monniaux/vocabulary.sh2
-rw-r--r--test/monniaux/volatile/slow_volatile.c14
-rw-r--r--test/monniaux/volatile/volatile_test.c33
-rw-r--r--test/monniaux/xor_and_mat/Makefile4
-rw-r--r--test/monniaux/xor_and_mat/int_mat.c210
-rw-r--r--test/monniaux/xor_and_mat/int_mat_run.c90
-rw-r--r--test/monniaux/xor_and_mat/xor_and.h50
-rw-r--r--test/monniaux/yarpgen/Makefile133
-rw-r--r--test/monniaux/yarpgen/Makefile.old52
-rw-r--r--test/monniaux/zlib-1.2.11/Makefile62
-rw-r--r--test/monniaux/zlib-1.2.11/adler32.c186
-rw-r--r--test/monniaux/zlib-1.2.11/compress.c86
-rw-r--r--test/monniaux/zlib-1.2.11/crc32.c442
-rw-r--r--test/monniaux/zlib-1.2.11/crc32.h441
-rw-r--r--test/monniaux/zlib-1.2.11/deflate.c2163
-rw-r--r--test/monniaux/zlib-1.2.11/deflate.h349
-rw-r--r--test/monniaux/zlib-1.2.11/gzclose.c25
-rw-r--r--test/monniaux/zlib-1.2.11/gzguts.h218
-rw-r--r--test/monniaux/zlib-1.2.11/gzlib.c637
-rw-r--r--test/monniaux/zlib-1.2.11/gzread.c654
-rw-r--r--test/monniaux/zlib-1.2.11/gzwrite.c665
-rw-r--r--test/monniaux/zlib-1.2.11/infback.c640
-rw-r--r--test/monniaux/zlib-1.2.11/inffast.c323
-rw-r--r--test/monniaux/zlib-1.2.11/inffast.h11
-rw-r--r--test/monniaux/zlib-1.2.11/inffixed.h94
-rw-r--r--test/monniaux/zlib-1.2.11/inflate.c1561
-rw-r--r--test/monniaux/zlib-1.2.11/inflate.h125
-rw-r--r--test/monniaux/zlib-1.2.11/inftrees.c304
-rw-r--r--test/monniaux/zlib-1.2.11/inftrees.h62
-rw-r--r--test/monniaux/zlib-1.2.11/make.proto4
-rw-r--r--test/monniaux/zlib-1.2.11/minigzip.c663
-rw-r--r--test/monniaux/zlib-1.2.11/trees.c1203
-rw-r--r--test/monniaux/zlib-1.2.11/trees.h128
-rw-r--r--test/monniaux/zlib-1.2.11/uncompr.c93
-rw-r--r--test/monniaux/zlib-1.2.11/zconf.h534
-rw-r--r--test/monniaux/zlib-1.2.11/zlib.h1912
-rw-r--r--test/monniaux/zlib-1.2.11/zlib_small.txt539
-rw-r--r--test/monniaux/zlib-1.2.11/zutil.c325
-rw-r--r--test/monniaux/zlib-1.2.11/zutil.h271
-rw-r--r--test/nardino/scheduling/entry_regs.c19
-rw-r--r--test/nardino/scheduling/spille_backw.c114
-rw-r--r--test/nardino/scheduling/spille_forw.c166
-rw-r--r--test/regression/Makefile16
-rw-r--r--test/regression/Results/builtins-common-kvx391
-rw-r--r--test/regression/Results/many_parameters1
-rw-r--r--test/regression/Results/varargs2-kvx12
-rw-r--r--test/regression/builtins-common.c2
-rw-r--r--test/regression/builtins-kvx.c72
-rw-r--r--test/regression/extasm.c1
-rw-r--r--test/regression/many_parameters.c313
-rw-r--r--test/regression/packedstruct1.c16
-rw-r--r--test/regression/ran000373_reduced.c36
-rw-r--r--test/regression/union_passing.c23
-rw-r--r--test/regression/varargs2.c16
-rw-r--r--tools/compiler_expand.ml194
-rwxr-xr-xtools/fix_html_date.sh8
-rw-r--r--x86/Asmexpand.ml2
-rw-r--r--x86/Asmgen.v12
-rw-r--r--x86/Asmgenproof.v12
-rw-r--r--x86/Asmgenproof1.v8
l---------x86/BTL_SEsimplify.v1
-rw-r--r--x86/CSE2deps.v38
-rw-r--r--x86/CSE2depsproof.v339
-rw-r--r--x86/Conventions1.v3
-rw-r--r--x86/DuplicateOpcodeHeuristic.ml41
l---------x86/ExpansionOracle.ml1
-rw-r--r--x86/Machregsaux.ml5
-rw-r--r--x86/Machregsaux.mli2
-rw-r--r--x86/Op.v115
-rw-r--r--x86/PrepassSchedulingOracle.ml4
-rw-r--r--x86/SelectLong.vp2
-rw-r--r--x86/SelectLongproof.v1
-rw-r--r--x86/SelectOp.vp12
-rw-r--r--x86/SelectOpproof.v29
-rw-r--r--x86/TargetPrinter.ml77
-rw-r--r--x86/ValueAOp.v21
-rw-r--r--x86_32/Archi.v1
-rw-r--r--x86_64/Archi.v2
2454 files changed, 651242 insertions, 4554 deletions
diff --git a/.download_from_Kalray.sh b/.download_from_Kalray.sh
new file mode 100755
index 00000000..16b7190f
--- /dev/null
+++ b/.download_from_Kalray.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+mkdir download
+cd download
+# sshpass "-p$ sftp -o "StrictHostKeyChecking=no" compcert@ssh.kalray.eu <<EOF
+# cd files
+# get DEB.tar
+# EOF
+# tar xf DEB.tar
+VERSION=ACE4.6.0
+FILE=$VERSION.tgz
+DEBPATH=$VERSION/ACE-acceleration/ubuntu1804/tools/DEB
+sshpass "-p$KALRAY_SFTP_PASSWORD" rsync -e 'ssh -v -o "ConnectionAttempts=10" -o "StrictHostKeyChecking=no"' --progress -a certicompil@pressembois.imag.fr:Kalray/ACE4.6.0.tgz .
+tar xvfz $FILE $DEBPATH/
+mv $DEBPATH/*.deb .
+rm -rf $VERSION
diff --git a/.gitignore b/.gitignore
index 99facd7e..7889af6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,9 @@
# Object files, in general
+**#*#
+**.swp
+**.bin
+**.out
+**.tok
*.vo
*.vok
*.vos
@@ -16,6 +21,7 @@
# Emacs saves
*~
# Executables and configuration
+/tools/compiler_expand
/ccomp
/ccomp.byte
/ccomp.prof
@@ -44,6 +50,9 @@
/riscV/ConstpropOp.v
/riscV/SelectOp.v
/riscV/SelectLong.v
+/kvx/ConstpropOp.v
+/kvx/SelectOp.v
+/kvx/SelectLong.v
/aarch64/ConstpropOp.v
/aarch64/SelectOp.v
/aarch64/SelectLong.v
@@ -66,6 +75,8 @@
/lib/Tokenize.ml
/lib/Responsefile.ml
/driver/Version.ml
+/driver/Compiler.v
+/extraction/extraction.v
# Documentation
/doc/coq2html
/doc/coq2html.ml
@@ -73,6 +84,11 @@
/doc/html/
# MacOS metadata
.DS_Store
+runtime/kvx/i64_sdiv.s
+runtime/kvx/i64_smod.s
+runtime/kvx/i64_udiv.s
+runtime/kvx/i64_udivmod.s
+runtime/kvx/i64_umod.s
# Test generated data
/test/export/clight/*.v
/test/export/csyntax/*.v
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 00000000..9534282d
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,351 @@
+stages:
+ - build
+
+check-admitted:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_x86_64.sh
+ - make check-admitted
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_x86_64:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_x86_64.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen
+ - ulimit -s65536 && make -C test/monniaux/csmith
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_ia32:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-multilib
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_ia32.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen BITS=32 TARGET_CC='gcc -m32'
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='gcc -m32'
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_aarch64:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-aarch64-linux-gnu qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_aarch64.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test SIMU='qemu-aarch64 -L /usr/aarch64-linux-gnu' EXECUTE='qemu-aarch64 -L /usr/aarch64-linux-gnu' all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='aarch64-linux-gnu-gcc' EXECUTE='qemu-aarch64 -L /usr/aarch64-linux-gnu'
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='aarch64-linux-gnu-gcc' EXECUTE='timeout 10s qemu-aarch64 -L /usr/aarch64-linux-gnu' CCOMPOPTS='-static' TARGET_CFLAGS='-static'
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_arm:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-arm-linux-gnueabi libc6-dev-armel-cross qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_arm.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test SIMU='qemu-arm -L /usr/arm-linux-gnueabi' EXECUTE='qemu-arm -L /usr/arm-linux-gnueabi' all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='arm-linux-gnueabi-gcc' EXECUTE='qemu-arm -L /usr/arm-linux-gnueabi' BITS=32
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='arm-linux-gnueabi-gcc' EXECUTE='timeout 10s qemu-arm -L /usr/arm-linux-gnueabi' CCOMPOPTS='-static' TARGET_CFLAGS='-static' BITS=32
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+
+build_armhf:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_armhf.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test SIMU='qemu-arm -L /usr/arm-linux-gnueabihf' EXECUTE='qemu-arm -L /usr/arm-linux-gnueabihf' all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='arm-linux-gnueabihf-gcc' EXECUTE='qemu-arm -L /usr/arm-linux-gnueabihf' BITS=32
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='arm-linux-gnueabihf-gcc' EXECUTE='timeout 10s qemu-arm -L /usr/arm-linux-gnueabihf' CCOMPOPTS='-static' TARGET_CFLAGS='-static' BITS=32
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_ppc:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-powerpc-linux-gnu wget ninja-build libglib2.0-dev libpixman-1-dev
+ - wget --no-verbose https://download.qemu.org/qemu-6.0.0.tar.xz
+ - tar xJf qemu-6.0.0.tar.xz
+ - (cd qemu-6.0.0 && ./configure --target-list=ppc-linux-user && make && sudo make install)
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_ppc.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - qemu-ppc --version
+ - make -C test SIMU='qemu-ppc -L /usr/powerpc-linux-gnu -cpu 7400' EXECUTE='qemu-ppc -L /usr/powerpc-linux-gnu -cpu 7400' all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='powerpc-linux-gnu-gcc' EXECUTE='qemu-ppc -L /usr/powerpc-linux-gnu -cpu 7400' BITS=32
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='powerpc-linux-gnu-gcc' EXECUTE='timeout 10s qemu-ppc -L /usr/powerpc-linux-gnu -cpu 7400'
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_ppc64:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-powerpc64-linux-gnu
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_ppc64.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ # - make -C test SIMU='qemu-ppc64 -L /usr/powerpc64-linux-gnu -cpu 7400' EXECUTE='qemu-ppc64 -L /usr/powerpc64-linux-gnu -cpu 7400' all test
+ # - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='powerpc64-linux-gnu-gcc' EXECUTE='qemu-ppc64 -L /usr/powerpc64-linux-gnu -cpu 7400' BITS=32
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_rv64:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-riscv64-linux-gnu qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_rv64.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - make -C test SIMU='qemu-riscv64 -L /usr/riscv64-linux-gnu' EXECUTE='qemu-riscv64 -L /usr/riscv64-linux-gnu' all test
+ - ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='riscv64-linux-gnu-gcc' EXECUTE='qemu-riscv64 -L /usr/riscv64-linux-gnu'
+ - ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='riscv64-linux-gnu-gcc' EXECUTE='timeout 10s qemu-riscv64 -L /usr/riscv64-linux-gnu' CCOMPOPTS='-static' TARGET_CFLAGS='-static'
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_rv32:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-riscv64-linux-gnu qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_rv32.sh -no-runtime-lib
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+build_kvx:
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install sshpass openssh-client libzip4 lttng-tools liblttng-ctl-dev liblttng-ust-dev babeltrace libzip4
+ - ./.download_from_Kalray.sh
+ - (cd download ; rm -f *dkms*.deb *eclipse*.deb *llvm*.deb *board-mgmt* *oce-host* *pocl* *flash-util* *barebox* *-kann-* *-kaf-* *-stb-* *-opencv* *-eigen* *-task* *-blis* *-lz4*)
+ - sudo dpkg -i download/*.deb
+ - rm -rf download
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - source /opt/kalray/accesscore/kalray.sh && ./config_kvx.sh
+ - source /opt/kalray/accesscore/kalray.sh && make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - source /opt/kalray/accesscore/kalray.sh && make -C test CCOMPOPTS=-static SIMU='kvx-cluster -- ' EXECUTE='kvx-cluster -- ' all test
+ - source /opt/kalray/accesscore/kalray.sh && ulimit -s65536 && make -C test/monniaux/csmith TARGET_CC='kvx-cos-gcc' CCOMPOPTS="-Wl,--defsym=USER_STACK_SIZE=0x80000" EXECUTE="timeout 15s kvx-cluster -- "
+ - source /opt/kalray/accesscore/kalray.sh && ulimit -s65536 && make -C test/monniaux/yarpgen TARGET_CC='kvx-cos-gcc' EXECUTE='kvx-cluster -- ' CCOMPOPTS='-static' TARGET_CFLAGS='-static'
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
+
+pages: # TODO: change to "deploy" when "build" succeeds (or integrate with "build_kvx" above ?)
+ stage: build
+ image: coqorg/coq:8.12.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install sshpass openssh-client libzip4 lttng-tools liblttng-ctl-dev liblttng-ust-dev babeltrace
+ - ./.download_from_Kalray.sh
+ - rm -f download/*dkms*.deb download/*eclipse*.deb download/*llvm*.deb download/*board-mgmt* download/*oce-host* download/*pocl* download/*flash-util* download/*barebox*
+ - sudo dpkg -i download/*.deb
+ - rm -rf download
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ - opam repo add coq-released https://coq.inria.fr/opam/released
+ - opam install coq-coq2html
+ script:
+ - source /opt/kalray/accesscore/kalray.sh && ./config_kvx.sh
+ - source /opt/kalray/accesscore/kalray.sh && make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ - source /opt/kalray/accesscore/kalray.sh && make documentation
+ - mkdir public
+ - cp -r doc/* public/
+ - tools/fix_html_date.sh doc/index-kvx.html " (" ")" > public/index.html
+ - rm public/index-kvx.html
+ artifacts:
+ paths:
+ - public
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+
+build_aarch64_coq13:
+ stage: build
+ image: coqorg/coq:8.13.2-ocaml-4.11.2-flambda
+ before_script:
+ - sudo apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update
+ - sudo apt-get -y install gcc-aarch64-linux-gnu qemu-user
+ - eval `opam config env`
+ - opam update
+ - opam install -y menhir
+ script:
+ - ./config_aarch64.sh
+ - make -j "$NJOBS"
+ - make -j "$NJOBS" clightgen
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "kvx-work"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-ssa"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "kvx-work-velus"'
+ when: always
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ when: always
+ - when: manual
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/.gitmodules
diff --git a/INSTALL.md b/INSTALL.md
new file mode 100644
index 00000000..f072a211
--- /dev/null
+++ b/INSTALL.md
@@ -0,0 +1,74 @@
+# CompCert Install Instructions
+
+## Dependencies
+
+### Additional dependencies
+
+Replace with the package manager for your distribution
+```
+sudo <pkg-manager> install -y mercurial darcs ocaml bubblewrap
+```
+
+### Opam
+
+```
+sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
+```
+
+## Post-install
+Run
+```
+eval `opam config env`
+```
+Add this to your `.bashrc` or `.bash_profile`
+```
+. $HOME/.opam/opam-init/init.sh > /dev/null 2> /dev/null || true
+```
+Switch to a recent OCaml compiler version
+```
+opam switch create 4.09.0
+opam switch 4.09.0
+```
+Install dependencies available through opam
+```
+opam install coq menhir
+```
+
+Note: it may happen that a newer version of Coq is not supported yet.
+You may downgrade to solve the problem:
+```
+opam pin add coq 8.11.0 # example of Coq version
+```
+
+## Compilation
+Pre-compilation configure replace the placeholder with your desired platform
+(for Kalray Coolidge it is `kvx-cos`)
+```
+./configure -prefix ~/.usr <platform>
+```
+
+`PREFIX` is where CompCert will be installed after `make install`
+
+If using Kalray's platform, make sure that the kvx tools are on your path
+Compile (adapt -j# to the number of cores and available RAM)
+```
+make -j12
+make install
+```
+
+## Utilization
+`ccomp` binaries are installed at `$(PREFIX)/bin`
+Make sure to add that to your path to ease its use
+Now you may use it like a regular compiler
+```
+ccomp -O3 test.c -S
+ccomp -O3 test.c -o test.bin
+```
+
+## Changing platform
+If you decide to change the platform, for instance from kvx-cos to kvx-mbr, you
+should change the `compcert.ini` file with the respective tools and then run
+```
+make install
+```
+
diff --git a/INSTALL_CROSS.md b/INSTALL_CROSS.md
new file mode 100644
index 00000000..0c2fce60
--- /dev/null
+++ b/INSTALL_CROSS.md
@@ -0,0 +1,5 @@
+It is possible to compile Coq on one architecture and then the resulting OCaml on another architecture.
+
+- Remove all '*.cm*' files.
+- Fix the path to MenhirLib in Makefile.config
+- make again
diff --git a/Makefile b/Makefile
index 4a9f3772..a5d56fee 100644
--- a/Makefile
+++ b/Makefile
@@ -23,22 +23,30 @@ TAG ?= $(tag)
BRANCH ?= $(branch)
ifeq ($(wildcard $(ARCH)_$(BITSIZE)),)
-ARCHDIRS=$(ARCH)
+ARCHDIRS?=$(ARCH)
else
-ARCHDIRS=$(ARCH)_$(BITSIZE) $(ARCH)
+ARCHDIRS?=$(ARCH)_$(BITSIZE) $(ARCH)
endif
-DIRS := lib common $(ARCHDIRS) backend cfrontend driver export cparser
+BACKENDLIB?=Asmgenproof0.v Asmgenproof1.v
-COQINCLUDES := $(foreach d, $(DIRS), -R $(d) compcert.$(d))
+DIRS := lib lib/Impure common $(ARCHDIRS) scheduling backend cfrontend driver \
+ export cparser
+
+RECDIRS:=lib common $(ARCHDIRS) scheduling backend cfrontend driver flocq exportclight \
+ cparser
+
+COQINCLUDES := $(foreach d, $(DIRS), -R $(d) compcert.$(subst /,.,$d))
ifeq ($(LIBRARY_FLOCQ),local)
DIRS += flocq/Core flocq/Prop flocq/Calc flocq/IEEE754
+RECDIRS += flocq
COQINCLUDES += -R flocq Flocq
endif
ifeq ($(LIBRARY_MENHIRLIB),local)
DIRS += MenhirLib
+RECDIRS += MenhirLib
COQINCLUDES += -R MenhirLib MenhirLib
endif
@@ -88,9 +96,13 @@ endif
# General-purpose libraries (in lib/)
VLIB=Axioms.v Coqlib.v Intv.v Maps.v Heaps.v Lattice.v Ordered.v \
+ HashedSet.v \
Iteration.v Zbits.v Integers.v Archi.v IEEE754_extra.v Floats.v \
Parmov.v UnionFind.v Wfsimpl.v \
- Postorder.v FSetAVLplus.v IntvSets.v Decidableplus.v BoolEqual.v
+ Postorder.v FSetAVLplus.v IntvSets.v Decidableplus.v BoolEqual.v \
+ OptionMonad.v \
+ ImpConfig.v ImpExtern.v ImpIO.v ImpMonads.v \
+ ImpCore.v ImpHCons.v ImpLoops.v ImpPrelude.v
# Parts common to the front-ends and the back-end (in common/)
@@ -102,7 +114,7 @@ COMMON=Errors.v AST.v Linking.v \
# Back-end modules (in backend/, $(ARCH)/)
BACKEND=\
- Cminor.v Cminortyping.v Op.v CminorSel.v \
+ Cminor.v Cminortyping.v Op.v CminorSel.v OpHelpers.v OpHelpersproof.v \
SelectOp.v SelectDiv.v SplitLong.v SelectLong.v Selection.v \
SelectOpproof.v SelectDivproof.v SplitLongproof.v \
SelectLongproof.v Selectionproof.v \
@@ -110,24 +122,44 @@ BACKEND=\
RTLgen.v RTLgenspec.v RTLgenproof.v \
Tailcall.v Tailcallproof.v \
Inlining.v Inliningspec.v Inliningproof.v \
+ Profiling.v Profilingproof.v \
+ ProfilingExploit.v ProfilingExploitproof.v \
Renumber.v Renumberproof.v \
+ Duplicate.v Duplicateproof.v Duplicatepasses.v \
RTLtyping.v \
Kildall.v Liveness.v \
ValueDomain.v ValueAOp.v ValueAnalysis.v \
ConstpropOp.v Constprop.v ConstpropOpproof.v Constpropproof.v \
+ Inject.v Injectproof.v \
CSEdomain.v CombineOp.v CSE.v CombineOpproof.v CSEproof.v \
+ CSE2deps.v CSE2depsproof.v \
+ CSE2.v CSE2proof.v \
+ CSE3analysis.v CSE3analysisproof.v CSE3.v CSE3proof.v \
+ KillUselessMoves.v KillUselessMovesproof.v \
+ LICM.v LICMproof.v \
NeedDomain.v NeedOp.v Deadcode.v Deadcodeproof.v \
Unusedglob.v Unusedglobproof.v \
Machregs.v Locations.v Conventions1.v Conventions.v LTL.v \
- Allocation.v Allocproof.v \
- Tunneling.v Tunnelingproof.v \
+ ForwardMoves.v ForwardMovesproof.v \
+ FirstNop.v FirstNopproof.v \
+ Allnontrap.v Allnontrapproof.v \
+ Allocation.v Allocationproof.v \
+ LTLTunneling.v LTLTunnelingproof.v \
+ RTLTunneling.v RTLTunnelingproof.v \
Linear.v Lineartyping.v \
Linearize.v Linearizeproof.v \
CleanupLabels.v CleanupLabelsproof.v \
Debugvar.v Debugvarproof.v \
Mach.v \
Bounds.v Stacklayout.v Stacking.v Stackingproof.v \
- Asm.v Asmgen.v Asmgenproof0.v Asmgenproof1.v Asmgenproof.v
+ Asm.v Asmgen.v Asmgenproof.v Asmaux.v \
+ BTL_SEsimplify.v \
+ $(BACKENDLIB)
+
+SCHEDULING= \
+ BTL.v BTLmatchRTL.v BTLtoRTL.v BTLtoRTLproof.v RTLtoBTL.v RTLtoBTLproof.v \
+ BTL_Livecheck.v BTL_Scheduler.v BTL_Schedulerproof.v\
+ BTL_SEtheory.v BTL_SEsimuref.v BTL_SEimpl.v
# C front-end modules (in cfrontend/)
@@ -166,7 +198,7 @@ endif
# All source files
-FILES=$(VLIB) $(COMMON) $(BACKEND) $(CFRONTEND) $(DRIVER) $(FLOCQ) \
+FILES=$(VLIB) $(COMMON) $(BACKEND) $(SCHEDULING) $(CFRONTEND) $(DRIVER) $(FLOCQ) \
$(MENHIRLIB) $(PARSER) $(EXPORTLIB)
# Generated source files
@@ -181,6 +213,7 @@ all:
$(MAKE) proof
$(MAKE) extraction
$(MAKE) ccomp
+ $(MAKE) ccomp.byte
ifeq ($(HAS_RUNTIME_LIB),true)
$(MAKE) runtime
endif
@@ -211,6 +244,10 @@ ccomp: .depend.extr compcert.ini driver/Version.ml FORCE
ccomp.byte: .depend.extr compcert.ini driver/Version.ml FORCE
$(MAKE) -f Makefile.extr ccomp.byte
+# DM force compilation without checking dependencies
+ccomp.force: .depend.extr compcert.ini driver/Version.ml FORCE
+ $(MAKE) -f Makefile.extr ccomp.force
+
clightgen: .depend.extr compcert.ini driver/Version.ml FORCE
$(MAKE) -f Makefile.extr clightgen
clightgen.byte: .depend.extr compcert.ini driver/Version.ml FORCE
@@ -236,6 +273,13 @@ else
ocamlc -o tools/ndfun str.cma tools/ndfun.ml
endif
+tools/compiler_expand: tools/compiler_expand.ml
+ifeq ($(OCAML_NATIVE_COMP),true)
+ ocamlopt -o $@ $+
+else
+ ocamlc -o $@ $+
+endif
+
tools/modorder: tools/modorder.ml
ifeq ($(OCAML_NATIVE_COMP),true)
ocamlopt -o tools/modorder str.cmxa tools/modorder.ml
@@ -257,6 +301,18 @@ latexdoc:
@tools/ndfun $*.vp > $*.v || { rm -f $*.v; exit 2; }
@chmod a-w $*.v
+# this trick aims to allow extraction to depend on the target processor
+# (currently: export extra Coq-functions for OCaml code, depending on the target)
+extraction/extraction.v: Makefile extraction/extraction.vexpand
+ (echo "(* WARNING: this file is generated from extraction.vexpand *)"; \
+ echo "(* by the Makefile -- target \"extraction/extraction.v\" *)"; \
+ cat extraction/extraction.vexpand; \
+ echo "$(EXTRA_EXTRACTION)"; \
+ echo ".") > extraction/extraction.v
+
+driver/Compiler.v: driver/Compiler.vexpand tools/compiler_expand
+ tools/compiler_expand driver/Compiler.vexpand $@
+
compcert.ini: Makefile.config
(echo "stdlib_path=$(LIBDIR)"; \
echo "prepro=$(CPREPRO)"; \
@@ -267,6 +323,7 @@ compcert.ini: Makefile.config
echo "linker_options=$(CLINKER_OPTIONS)";\
echo "arch=$(ARCH)"; \
echo "model=$(MODEL)"; \
+ echo "os=$(OS)"; \
echo "abi=$(ABI)"; \
echo "endianness=$(ENDIANNESS)"; \
echo "system=$(SYSTEM)"; \
@@ -334,10 +391,10 @@ clean:
rm -f $(patsubst %, %/*.vo*, $(DIRS))
rm -f $(patsubst %, %/.*.aux, $(DIRS))
rm -rf doc/html doc/*.glob
- rm -f driver/Version.ml
+ rm -f driver/Version.ml driver/Compiler.v
rm -f compcert.ini compcert.config
- rm -f extraction/STAMP extraction/*.ml extraction/*.mli .depend.extr
- rm -f tools/ndfun tools/modorder tools/*.cm? tools/*.o
+ rm -f extraction/STAMP extraction/*.ml extraction/*.mli .depend.extr extraction/extraction.v
+ rm -f tools/ndfun tools/modorder tools/compiler_expand tools/*.cm? tools/*.o
rm -f $(GENERATED) .depend
rm -f .lia.cache
$(MAKE) -f Makefile.extr clean
@@ -352,7 +409,7 @@ check-admitted: $(FILES)
@grep -w 'admit\|Admitted\|ADMITTED' $^ || echo "Nothing admitted."
check-proof: $(FILES)
- $(COQCHK) compcert.driver.Complements
+ $(COQCHK) -o compcert.driver.Complements
print-includes:
@echo $(COQINCLUDES)
diff --git a/Makefile.extr b/Makefile.extr
index 6035eb9a..6106aff2 100644
--- a/Makefile.extr
+++ b/Makefile.extr
@@ -44,14 +44,14 @@ cparser/pre_parser_messages.ml:
# Directories containing plain Caml code
DIRS=extraction \
- lib common $(ARCH) backend cfrontend cparser driver \
- export debug
+ lib common $(ARCH) scheduling backend cfrontend cparser driver \
+ export debug kvx/unittest lib/Impure/ocaml
INCLUDES=$(patsubst %,-I %, $(DIRS))
# Control of warnings:
-WARNINGS=-w +a-4-9-27-70
+WARNINGS=-w +a-4-9-27-70-42
extraction/%.cmx: WARNINGS +=-w -20-27-32..34-39-41-44..45-60-67
extraction/%.cmo: WARNINGS +=-w -20-27-32..34-39-41-44..45-60-67
cparser/pre_parser.cmx: WARNINGS += -w -41
@@ -108,6 +108,10 @@ ccomp: ccomp.byte
@$(COPY) $+ $@
endif
+# DM force compilation without checking dependencies
+ccomp.force:
+ $(OCAMLOPT) -o $@ $(LIBS) $(LINK_OPT) $(CCOMP_OBJS)
+
ccomp.byte: $(CCOMP_OBJS:.cmx=.cmo)
@echo "Linking $@"
@$(OCAMLC) -o $@ $(LIBS_BYTE) $+
diff --git a/PROFILING.md b/PROFILING.md
new file mode 100644
index 00000000..8eb8c585
--- /dev/null
+++ b/PROFILING.md
@@ -0,0 +1,34 @@
+This version of CompCert includes a profiling system. It tells CompCert's optimization phases for each conditional branch instruction which of the two branches was more frequently taken. This system is not available for all combinations of target architecture and operating system; see below.
+
+For using this profiling system one has to
+1. Compile a special version of the program that will count, for each branch, the number of times it was taken, and recording this information to a file.
+2. Execute this special version on representative examples. It will record the frequencies of execution of branches to a log file.
+3. Recompile the program, telling CompCert to use the information in the log file.
+
+This system does not use the same formats as gcc's gcov profiles, since it depends heavily on compiler internals. It seems however possible to profile and optimize programs consisting of modules compiled with gcc and CompCert by using both system simultaneously: compiler uses separate log files.
+
+To compile the special version that logs frequencies to files, use the option `-fprofile-arcs`. This option has to be specified at compile time but is not needed at link time (however, a reminder: if you link using another compiled than CompCert, you need to link against `libcompcert.a`). You may mix object files compiled with and without this option.
+
+This version may experience significant slowdown compared to normally compiled code, so do not use `-fprofile-arcs` for production code.
+
+At the end of execution of the program, frequency information will be logged to a file whose default name is `compcert_profiling.dat` (in the current directory). Another name may be used by specifying it using the `COMPCERT_PROFILING_DATA` environment variable. If this variable contains an empty string, no logging is done (but the slowdown still applies).
+
+Data are appended to the log file, never deleted, so it is safe to run the program several times on several test cases to accumulate data.
+
+Depending on the platform, this logging system is or is not thread-safe and is or is not compatible with position-independent code (PIC). In non thread-safe configurations, if two different execution threads execute code to be profiled, the profiling counters may end up with incorrect values.
+
+| Target platform | Available? | Thread-safe | PIC |
+|-----------------|------------|-------------|-----|
+| AArch64 | Yes | Yes | No |
+| ARM | Yes | No | No |
+| IA32 | Yes | No | No |
+| KVX | Yes | Yes | No |
+| PowerPC | No | | |
+| PowerPC 64 | No | | |
+| Risc-V 32 | No | | |
+| Risc-V 64 | No | | |
+| x86-64 | Yes | Yes | Yes |
+
+For recompiling the program using profiling information, use `-fprofile-use= compcert_profiling.dat -ftracelinearize` (substitute the appropriate filename for `compcert_profiling.dat` if needed). Experiments show performance improvement on KVX, not on other platforms.
+
+The same options (except for `-fprofile-use=` and `-fprofile-arcs`) should be used to compile the logging and optimized versions of the program: only functions that are exactly the same in the intermediate representation will be optimized according to profiling information.
diff --git a/README.md b/README.md
index fc12093b..3990048e 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,26 @@ features, installation instructions, using the compiler, etc), please
refer to the [Web site](https://compcert.org/) and especially
the [user's manual](https://compcert.org/man/).
+## Verimag-Kalray version
+This is a special version with additions from Verimag and Kalray :
+
+* A backend for the KVX processor: see [`README_Kalray.md`](README_Kalray.md) for details.
+* Some general-purpose optimization phases (e.g. profiling).
+ * see [`PROFILING.md`](PROFILING.md) for details on the profiling system
+
+The people responsible for this version are
+
+* Sylvain Boulmé (Grenoble-INP, Verimag)
+* David Monniaux (CNRS, Verimag)
+* Cyril Six (Kalray)
+
+## Papers, docs, etc on this CompCert version
+
+* [a 5-minutes video](http://www-verimag.imag.fr/~boulme/videos/poster-oopsla20.mp4) by C. Six, presenting the postpass scheduling and the KVX backend
+(also on [YouTube if you need subtitles](https://www.youtube.com/watch?v=RAzMDS9OVSw)).
+* [Certified and Efficient Instruction Scheduling](https://hal.archives-ouvertes.fr/hal-02185883), an OOPSLA'20 paper, by Six, Boulmé and Monniaux.
+* [the documentation of the KVX backend Coq sources](https://certicompil.gricad-pages.univ-grenoble-alpes.fr/compcert-kvx)
+
## License
CompCert is not free software. This non-commercial release can only
be used for evaluation, research, educational and personal purposes.
@@ -29,6 +49,7 @@ The CompCert verified compiler is Copyright Institut National de
Recherche en Informatique et en Automatique (INRIA) and
AbsInt Angewandte Informatik GmbH.
+The additions are Copyright Grenoble-INP, CNRS and Kalray.
## Contact
General discussions on CompCert take place on the
@@ -37,3 +58,5 @@ mailing list.
For inquiries on the commercial version of CompCert, please contact
info@absint.com
+
+For inquiries on the Verimag-specific additions, contact the researchers.
diff --git a/README_Kalray.md b/README_Kalray.md
new file mode 100644
index 00000000..86c49ad1
--- /dev/null
+++ b/README_Kalray.md
@@ -0,0 +1,37 @@
+# CompCert Kalray port
+The verified C compiler ported to Kalray.
+
+## Features
+
+This delivery contains (in addition to features from CompCert master branch):
+- A fully functional port of CompCert to Coolidge kvx VLIW core
+- Postpass scheduling optimization, only for kvx. Activated by default, it can be deactivated with the compiler flag `-fno-postpass`
+- Some experimental features that are work in progress:
+ - Slightly better subexpression eliminations, called CSE2 and CSE3. Both go through loops and feature a small alias analysis.
+ - `-fduplicate 0` to activate static branch prediction information. The branch prediction is basic, it annotates each `Icond` node by an `option bool`. A `Some true` annotation indicates we predict the branch will be taken. `Some false` indicates the fallthrough case is predicted. `None` indicates we could not predict anything, and are not sure about which control will be preferred.
+ - It is also possible to provide a number to perform tail duplication: `-fduplicate 5` will tail duplicate, stopping when more than 5 RTL instructions have been duplicated. This feature offers very variable performance (from -20% up to +20%) because of variations in the later register allocation phase that impacts the postpass scheduling. We intend to work on fine tuning the tail duplication phase once we have the prepass superblock scheduling.
+ - `-ftracelinearize` uses the branch prediction information to linearize LTL basic blocks in a slightly better way (in the `Linearize` phase).
+
+## Installing
+
+Please follow the instructions in `INSTALL.md`
+
+## Documentation of the Coq sources
+
+The documentation is available [online](https://certicompil.gricad-pages.univ-grenoble-alpes.fr/compcert-kvx).
+You may also generate it locally from `make documentation` (after installation via `INSTALL.md`): the entry-point is in `doc/index-kvx.html`.
+
+## Testing
+
+We modified most of the CompCert tests of the `c` folder in order for them to be executable in reasonable time by the simulator.
+
+To pass the testsuite, first, build and install CompCert using the instructions in `INSTALL.md`, then:
+```
+cd test/c
+make
+make test
+```
+
+The reference files were generated using `kvx-cos-gcc -O1`.
+
+We also have our own tests in `test/kvx/` - to run them, execute the script `simucheck.sh` located in that folder. These consist in comparing `compcert` output to `kvx-cos-gcc` output.
diff --git a/aarch64/Archi.v b/aarch64/Archi.v
index 4911db73..378ca0d1 100644
--- a/aarch64/Archi.v
+++ b/aarch64/Archi.v
@@ -88,6 +88,10 @@ Global Opaque ptr64 big_endian splitlong
(** Which ABI to implement *)
+Parameter pic_code: unit -> bool.
+
+Definition has_notrap_loads := false.
+
Inductive abi_kind: Type :=
| AAPCS64 (**r ARM's standard as used in Linux and other ELF platforms *)
| Apple. (**r the variant used in macOS and iOS *)
diff --git a/aarch64/Asm.v b/aarch64/Asm.v
index b5f4c838..e5111220 100644
--- a/aarch64/Asm.v
+++ b/aarch64/Asm.v
@@ -10,13 +10,20 @@
(* *)
(* *********************************************************************)
-(** Abstract syntax and semantics for AArch64 assembly language *)
+(** Abstract syntax and semantics for AArch64 assembly language
+
+W.r.t Asmblock, this is a "flat" syntax and semantics of "aarch64" assembly:
+ - without the basic block structure
+ - without the hierarchy of instructions.
+
+*)
Require Import Coqlib Zbits Maps.
Require Import AST Integers Floats.
Require Import Values Memory Events Globalenvs Smallstep.
Require Import Locations Conventions.
Require Stacklayout.
+Require Import OptionMonad.
(** * Abstract syntax *)
@@ -43,6 +50,9 @@ Coercion RR1: ireg >-> iregsp.
Lemma ireg_eq: forall (x y: ireg), {x=y} + {x<>y}.
Proof. decide equality. Defined.
+Lemma iregsp_eq: forall (x y: iregsp), {x=y} + {x<>y}.
+Proof. decide equality; apply ireg_eq. Defined.
+
(** In assembly files, [Dn] denotes the low 64-bit of a vector register,
and [Sn] the low 32 bits. *)
@@ -66,21 +76,29 @@ Inductive crbit: Type :=
Lemma crbit_eq: forall (x y: crbit), {x=y} + {x<>y}.
Proof. decide equality. Defined.
+Inductive dreg : Type :=
+ | IR: iregsp -> dreg (**r 64- or 32-bit integer registers *)
+ | FR: freg -> dreg. (**r double- or single-precision float registers *)
+
(** We model the following registers of the ARM architecture. *)
Inductive preg: Type :=
- | IR: ireg -> preg (**r 64- or 32-bit integer registers *)
- | FR: freg -> preg (**r double- or single-precision float registers *)
- | CR: crbit -> preg (**r bits in the condition register *)
- | SP: preg (**r register X31 used as stack pointer *)
- | PC: preg. (**r program counter *)
-
-Coercion IR: ireg >-> preg.
-Coercion FR: freg >-> preg.
+ | DR: dreg -> preg (** see dreg **)
+ | CR: crbit -> preg (**r bits in the condition register *)
+ | PC: preg. (**r program counter *)
+
+(* XXX: ireg no longer coerces to preg *)
+Coercion IR: iregsp >-> dreg.
+Coercion FR: freg >-> dreg.
+Coercion DR: dreg >-> preg.
+Definition SP:preg := XSP.
Coercion CR: crbit >-> preg.
+Lemma dreg_eq: forall (x y: dreg), {x=y} + {x<>y}.
+Proof. decide equality. apply iregsp_eq. apply freg_eq. Defined.
+
Lemma preg_eq: forall (x y: preg), {x=y} + {x<>y}.
-Proof. decide equality. apply ireg_eq. apply freg_eq. apply crbit_eq. Defined.
+Proof. decide equality. apply dreg_eq. apply crbit_eq. Defined.
Module PregEq.
Definition t := preg.
@@ -92,8 +110,6 @@ Module Pregmap := EMap(PregEq).
Definition preg_of_iregsp (r: iregsp) : preg :=
match r with RR1 r => IR r | XSP => SP end.
-Coercion preg_of_iregsp: iregsp >-> preg.
-
(** Conventional name for return address ([RA]) *)
Notation "'RA'" := X30 (only parsing) : asm.
@@ -177,14 +193,16 @@ Inductive instruction: Type :=
| Pldrsh (sz: isize) (rd: ireg) (a: addressing) (**r load int16, sign-extend *)
| Pldrzw (rd: ireg) (a: addressing) (**r load int32, zero-extend to int64 *)
| Pldrsw (rd: ireg) (a: addressing) (**r load int32, sign-extend to int64 *)
- | Pldp (rd1 rd2: ireg) (a: addressing) (**r load two int64 *)
+ | Pldpw (rd1 rd2: ireg) (chk1 chk2: memory_chunk) (a: addressing) (**r load two int32 *)
+ | Pldpx (rd1 rd2: ireg) (chk1 chk2: memory_chunk) (a: addressing) (**r load two int64 *)
| Pstrw (rs: ireg) (a: addressing) (**r store int32 *)
| Pstrw_a (rs: ireg) (a: addressing) (**r store int32 as any32 *)
| Pstrx (rs: ireg) (a: addressing) (**r store int64 *)
| Pstrx_a (rs: ireg) (a: addressing) (**r store int64 as any64 *)
| Pstrb (rs: ireg) (a: addressing) (**r store int8 *)
| Pstrh (rs: ireg) (a: addressing) (**r store int16 *)
- | Pstp (rs1 rs2: ireg) (a: addressing) (**r store two int64 *)
+ | Pstpw (rs1 rs2: ireg) (chk1 chk2: memory_chunk) (a: addressing) (**r store two int32 *)
+ | Pstpx (rs1 rs2: ireg) (chk1 chk2: memory_chunk) (a: addressing) (**r store two int64 *)
(** Integer arithmetic, immediate *)
| Paddimm (sz: isize) (rd: iregsp) (r1: iregsp) (n: Z) (**r addition *)
| Psubimm (sz: isize) (rd: iregsp) (r1: iregsp) (n: Z) (**r subtraction *)
@@ -255,9 +273,13 @@ Inductive instruction: Type :=
| Pldrs (rd: freg) (a: addressing) (**r load float32 (single precision) *)
| Pldrd (rd: freg) (a: addressing) (**r load float64 (double precision) *)
| Pldrd_a (rd: freg) (a: addressing) (**r load float64 as any64 *)
+ | Pldps (rd1 rd2: freg) (chk1 chk2: memory_chunk) (a: addressing) (**r load two float32 *)
+ | Pldpd (rd1 rd2: freg) (chk1 chk2: memory_chunk) (a: addressing) (**r load two float64 *)
| Pstrs (rs: freg) (a: addressing) (**r store float32 *)
| Pstrd (rs: freg) (a: addressing) (**r store float64 *)
| Pstrd_a (rs: freg) (a: addressing) (**r store float64 as any64 *)
+ | Pstps (rd1 rd2: freg) (chk1 chk2: memory_chunk) (a: addressing) (**r store two float32 *)
+ | Pstpd (rd1 rd2: freg) (chk1 chk2: memory_chunk) (a: addressing) (**r store two float64 *)
(** Floating-point move *)
| Pfmov (rd r1: freg)
| Pfmovimms (rd: freg) (f: float32) (**r load float32 constant *)
@@ -310,9 +332,30 @@ Definition code := list instruction.
Record function : Type := mkfunction { fn_sig: signature; fn_code: code }.
Definition fundef := AST.fundef function.
Definition program := AST.program fundef unit.
+Definition genv := Genv.t fundef unit.
+
+(** The two functions below axiomatize how the linker processes
+ symbolic references [symbol + offset]. It computes the
+ difference between the address and the PC, and splits it into:
+ - 12 low bits usable as an offset in an addressing mode;
+ - 21 high bits usable as argument to the ADRP instruction.
+
+ In CompCert's model, we cannot really describe PC-relative addressing,
+ but we can claim that the address of [symbol + offset] decomposes
+ as the sum of
+ - a low part, usable as an offset in an addressing mode;
+ - a high part, usable as argument to the ADRP instruction. *)
+
+Parameter symbol_low: genv -> ident -> ptrofs -> val.
+Parameter symbol_high: genv -> ident -> ptrofs -> val.
+
+Axiom symbol_high_low:
+ forall (ge: genv) (id: ident) (ofs: ptrofs),
+ Val.addl (symbol_high ge id ofs) (symbol_low ge id ofs) = Genv.symbol_address ge id ofs.
(** * Operational semantics *)
+
(** The semantics operates over a single mapping from registers
(type [preg]) to values. We maintain (but do not enforce)
the convention that integer registers are mapped to values of
@@ -320,22 +363,19 @@ Definition program := AST.program fundef unit.
and condition bits to either [Vzero] or [Vone]. *)
Definition regset := Pregmap.t val.
-Definition genv := Genv.t fundef unit.
(** The value of an [ireg0] is either the value of the integer register,
or 0. *)
-Definition ir0w (rs: regset) (r: ireg0) : val :=
- match r with RR0 r => rs (IR r) | XZR => Vint Int.zero end.
-Definition ir0x (rs: regset) (r: ireg0) : val :=
- match r with RR0 r => rs (IR r) | XZR => Vlong Int64.zero end.
+Definition ir0 (is:isize) (rs: regset) (r: ireg0) : val :=
+ match r with RR0 r => rs (IR r) | XZR => if is (* is W *) then Vint Int.zero else Vlong Int64.zero end.
(** Concise notations for accessing and updating the values of registers. *)
Notation "a # b" := (a b) (at level 1, only parsing) : asm.
Notation "a # b <- c" := (Pregmap.set b c a) (at level 1, b at next level) : asm.
-Notation "a ## b" := (ir0w a b) (at level 1, only parsing) : asm.
-Notation "a ### b" := (ir0x a b) (at level 1, only parsing) : asm.
+Notation "a ## b" := (ir0 W a b) (at level 1, only parsing) : asm.
+Notation "a ### b" := (ir0 X a b) (at level 1, only parsing) : asm.
Open Scope asm.
@@ -369,84 +409,16 @@ Fixpoint set_res (res: builtin_res preg) (v: val) (rs: regset) : regset :=
| BR_splitlong hi lo => set_res lo (Val.loword v) (set_res hi (Val.hiword v) rs)
end.
-(** The two functions below axiomatize how the linker processes
- symbolic references [symbol + offset]. It computes the
- difference between the address and the PC, and splits it into:
- - 12 low bits usable as an offset in an addressing mode;
- - 21 high bits usable as argument to the ADRP instruction.
-
- In CompCert's model, we cannot really describe PC-relative addressing,
- but we can claim that the address of [symbol + offset] decomposes
- as the sum of
- - a low part, usable as an offset in an addressing mode;
- - a high part, usable as argument to the ADRP instruction. *)
-
-Parameter symbol_low: genv -> ident -> ptrofs -> val.
-Parameter symbol_high: genv -> ident -> ptrofs -> val.
-
-Axiom symbol_high_low:
- forall (ge: genv) (id: ident) (ofs: ptrofs),
- Val.addl (symbol_high ge id ofs) (symbol_low ge id ofs) = Genv.symbol_address ge id ofs.
-
-Section RELSEM.
-
-Variable ge: genv.
-
-(** Looking up instructions in a code sequence by position. *)
-
-Fixpoint find_instr (pos: Z) (c: code) {struct c} : option instruction :=
- match c with
- | nil => None
- | i :: il => if zeq pos 0 then Some i else find_instr (pos - 1) il
- end.
-
-(** Position corresponding to a label *)
-
-Definition is_label (lbl: label) (instr: instruction) : bool :=
- match instr with
- | Plabel lbl' => if peq lbl lbl' then true else false
- | _ => false
- end.
-
-Lemma is_label_correct:
- forall lbl instr,
- if is_label lbl instr then instr = Plabel lbl else instr <> Plabel lbl.
-Proof.
- intros. destruct instr; simpl; try discriminate. destruct (peq lbl lbl0); congruence.
-Qed.
-
-Fixpoint label_pos (lbl: label) (pos: Z) (c: code) {struct c} : option Z :=
- match c with
- | nil => None
- | instr :: c' =>
- if is_label lbl instr then Some (pos + 1) else label_pos lbl (pos + 1) c'
- end.
-
(** The semantics is purely small-step and defined as a function
from the current state (a register set + a memory state)
to either [Next rs' m'] where [rs'] and [m'] are the updated register
set and memory state after execution of the instruction at [rs#PC],
or [Stuck] if the processor is stuck. *)
-Inductive outcome: Type :=
- | Next: regset -> mem -> outcome
- | Stuck: outcome.
-
-(** Manipulations over the [PC] register: continuing with the next
- instruction ([nextinstr]) or branching to a label ([goto_label]). *)
-
-Definition nextinstr (rs: regset) :=
- rs#PC <- (Val.offset_ptr rs#PC Ptrofs.one).
-
-Definition goto_label (f: function) (lbl: label) (rs: regset) (m: mem) :=
- match label_pos lbl 0 (fn_code f) with
- | None => Stuck
- | Some pos =>
- match rs#PC with
- | Vptr b ofs => Next (rs#PC <- (Vptr b (Ptrofs.repr pos))) m
- | _ => Stuck
- end
- end.
+Record state: Type := State { _rs: regset; _m: mem }.
+Definition outcome := option state.
+Definition Next rs m: outcome := Some (State rs m).
+Definition Stuck: outcome := None.
(** Testing a condition *)
@@ -516,10 +488,10 @@ Definition eval_testcond (c: testcond) (rs: regset) : option bool :=
(** Integer "is zero?" test *)
-Definition eval_testzero (sz: isize) (v: val) (m: mem): option bool :=
+Definition eval_testzero (sz: isize) (v: val): option bool :=
match sz with
- | W => Val.cmpu_bool (Mem.valid_pointer m) Ceq v (Vint Int.zero)
- | X => Val.cmplu_bool (Mem.valid_pointer m) Ceq v (Vlong Int64.zero)
+ | W => Val.mxcmpu_bool Ceq v (Vint Int.zero)
+ | X => Val.mxcmplu_bool Ceq v (Vlong Int64.zero)
end.
(** Integer "bit is set?" test *)
@@ -530,48 +502,19 @@ Definition eval_testbit (sz: isize) (v: val) (n: int): option bool :=
| X => Val.cmpl_bool Cne (Val.andl v (Vlong (Int64.shl' Int64.one n))) (Vlong Int64.zero)
end.
-(** Evaluating an addressing mode *)
-
-Definition eval_addressing (a: addressing) (rs: regset): val :=
- match a with
- | ADimm base n => Val.addl rs#base (Vlong n)
- | ADreg base r => Val.addl rs#base rs#r
- | ADlsl base r n => Val.addl rs#base (Val.shll rs#r (Vint n))
- | ADsxt base r n => Val.addl rs#base (Val.shll (Val.longofint rs#r) (Vint n))
- | ADuxt base r n => Val.addl rs#base (Val.shll (Val.longofintu rs#r) (Vint n))
- | ADadr base id ofs => Val.addl rs#base (symbol_low ge id ofs)
- | ADpostincr base n => Vundef (* not modeled yet *)
- end.
-
-(** Auxiliaries for memory accesses *)
-
-Definition exec_load (chunk: memory_chunk) (transf: val -> val)
- (a: addressing) (r: preg) (rs: regset) (m: mem) :=
- match Mem.loadv chunk m (eval_addressing a rs) with
- | None => Stuck
- | Some v => Next (nextinstr (rs#r <- (transf v))) m
- end.
-
-Definition exec_store (chunk: memory_chunk)
- (a: addressing) (v: val)
- (rs: regset) (m: mem) :=
- match Mem.storev chunk m (eval_addressing a rs) v with
- | None => Stuck
- | Some m' => Next (nextinstr rs) m'
- end.
(** Comparisons *)
-Definition compare_int (rs: regset) (v1 v2: val) (m: mem) :=
+Definition compare_int (rs: regset) (v1 v2: val) :=
rs#CN <- (Val.negative (Val.sub v1 v2))
- #CZ <- (Val.cmpu (Mem.valid_pointer m) Ceq v1 v2)
- #CC <- (Val.cmpu (Mem.valid_pointer m) Cge v1 v2)
+ #CZ <- (Val.mxcmpu Ceq v1 v2)
+ #CC <- (Val.mxcmpu Cge v1 v2)
#CV <- (Val.sub_overflow v1 v2).
-Definition compare_long (rs: regset) (v1 v2: val) (m: mem) :=
+Definition compare_long (rs: regset) (v1 v2: val) :=
rs#CN <- (Val.negativel (Val.subl v1 v2))
- #CZ <- (Val.maketotal (Val.cmplu (Mem.valid_pointer m) Ceq v1 v2))
- #CC <- (Val.maketotal (Val.cmplu (Mem.valid_pointer m) Cge v1 v2))
+ #CZ <- (Val.mxcmplu Ceq v1 v2)
+ #CC <- (Val.mxcmplu Cge v1 v2)
#CV <- (Val.subl_overflow v1 v2).
(** Semantics of [fcmp] instructions:
@@ -672,19 +615,230 @@ Definition float64_of_bits (v: val): val :=
| _ => Vundef
end.
-(** Execution of a single instruction [i] in initial state
- [rs] and [m]. Return updated state. For instructions
- that correspond to actual AArch64 instructions, the cases are
- straightforward transliterations of the informal descriptions
- given in the ARMv8 reference manuals. For pseudo-instructions,
- refer to the informal descriptions given above.
-
- Note that we set to [Vundef] the registers used as temporaries by
- the expansions of the pseudo-instructions, so that the code we
- generate cannot use those registers to hold values that must
- survive the execution of the pseudo-instruction.
+(* Recognition of FP numbers that are supported by the fmov #imm instructions:
+ "a normalized binary floating point encoding with 1 sign bit,
+ 4 bits of fraction and a 3-bit exponent"
*)
+Definition is_immediate_float64 (f: float): bool :=
+ let bits := Float.to_bits f in
+ let exp :=
+ Int64.sub
+ (Int64.and (Int64.shr' bits (Int.repr 52))
+ (Int64.repr 2047)) (Int64.repr 1023) in
+ let mant :=
+ Int64.and bits (Int64.repr 4503599627370495) in
+ andb (Int64.cmp Cge exp (Int64.repr (-3)))
+ (andb (Int64.cmp Cle exp (Int64.repr 4))
+ (Int64.eq
+ (Int64.and mant
+ (Int64.repr 4222124650659840)) mant)).
+
+Definition is_immediate_float32 (f: float32): bool :=
+ let bits := Float32.to_bits f in
+ let exp :=
+ Int.sub
+ (Int.and (Int.shr bits (Int.repr 23))
+ (Int.repr 255)) (Int.repr 127) in
+ let mant :=
+ Int.and bits (Int.repr 8388607) in
+ andb (Int.cmp Cge exp (Int.repr (-3)))
+ (andb (Int.cmp Cle exp (Int.repr 4))
+ (Int.eq
+ (Int.and mant
+ (Int.repr 7864320)) mant)).
+
+(** Translation of the LTL/Linear/Mach view of machine registers
+ to the AArch64 view. Note that no LTL register maps to [X16],
+ [X18], nor [X30].
+ [X18] is reserved as the platform register and never used by the
+ code generated by CompCert.
+ [X30] is used for the return address, and can also be used as temporary.
+ [X16] can be used as temporary. *)
+
+Definition dreg_of (r: mreg) : dreg :=
+ match r with
+ | R0 => X0 | R1 => X1 | R2 => X2 | R3 => X3
+ | R4 => X4 | R5 => X5 | R6 => X6 | R7 => X7
+ | R8 => X8 | R9 => X9 | R10 => X10 | R11 => X11
+ | R12 => X12 | R13 => X13 | R14 => X14 | R15 => X15
+ | R17 => X17 | R19 => X19
+ | R20 => X20 | R21 => X21 | R22 => X22 | R23 => X23
+ | R24 => X24 | R25 => X25 | R26 => X26 | R27 => X27
+ | R28 => X28 | R29 => X29
+ | F0 => D0 | F1 => D1 | F2 => D2 | F3 => D3
+ | F4 => D4 | F5 => D5 | F6 => D6 | F7 => D7
+ | F8 => D8 | F9 => D9 | F10 => D10 | F11 => D11
+ | F12 => D12 | F13 => D13 | F14 => D14 | F15 => D15
+ | F16 => D16 | F17 => D17 | F18 => D18 | F19 => D19
+ | F20 => D20 | F21 => D21 | F22 => D22 | F23 => D23
+ | F24 => D24 | F25 => D25 | F26 => D26 | F27 => D27
+ | F28 => D28 | F29 => D29 | F30 => D30 | F31 => D31
+ end.
+
+Definition preg_of (r: mreg) : preg :=
+ dreg_of r.
+
+(** Undefine all registers except SP and callee-save registers *)
+
+Definition undef_caller_save_regs (rs: regset) : regset :=
+ fun r =>
+ if preg_eq r SP
+ || In_dec preg_eq r (List.map preg_of (List.filter is_callee_save all_mregs))
+ then rs r
+ else Vundef.
+
+(** Extract the values of the arguments of an external call.
+ We exploit the calling conventions from module [Conventions], except that
+ we use AArch64 registers instead of locations. *)
+
+Inductive extcall_arg (rs: regset) (m: mem): loc -> val -> Prop :=
+ | extcall_arg_reg: forall r,
+ extcall_arg rs m (R r) (rs (preg_of r))
+ | extcall_arg_stack: forall ofs ty bofs v,
+ bofs = Stacklayout.fe_ofs_arg + 4 * ofs ->
+ Mem.loadv (chunk_of_type ty) m
+ (Val.offset_ptr rs#SP (Ptrofs.repr bofs)) = Some v ->
+ extcall_arg rs m (Locations.S Outgoing ofs ty) v.
+
+Inductive extcall_arg_pair (rs: regset) (m: mem): rpair loc -> val -> Prop :=
+ | extcall_arg_one: forall l v,
+ extcall_arg rs m l v ->
+ extcall_arg_pair rs m (One l) v
+ | extcall_arg_twolong: forall hi lo vhi vlo,
+ extcall_arg rs m hi vhi ->
+ extcall_arg rs m lo vlo ->
+ extcall_arg_pair rs m (Twolong hi lo) (Val.longofwords vhi vlo).
+
+Definition extcall_arguments
+ (rs: regset) (m: mem) (sg: signature) (args: list val) : Prop :=
+ list_forall2 (extcall_arg_pair rs m) (loc_arguments sg) args.
+
+Definition loc_external_result (sg: signature) : rpair preg :=
+ map_rpair preg_of (loc_result sg).
+
+(** Looking up instructions in a code sequence by position. *)
+
+Fixpoint find_instr (pos: Z) (c: code) {struct c} : option instruction :=
+ match c with
+ | nil => None
+ | i :: il => if zeq pos 0 then Some i else find_instr (pos - 1) il
+ end.
+
+(** Position corresponding to a label *)
+
+Definition is_label (lbl: label) (instr: instruction) : bool :=
+ match instr with
+ | Plabel lbl' => if peq lbl lbl' then true else false
+ | _ => false
+ end.
+
+Lemma is_label_correct:
+ forall lbl instr,
+ if is_label lbl instr then instr = Plabel lbl else instr <> Plabel lbl.
+Proof.
+ intros. destruct instr; simpl; try discriminate. destruct (peq lbl lbl0); congruence.
+Qed.
+
+Fixpoint label_pos (lbl: label) (pos: Z) (c: code) {struct c} : option Z :=
+ match c with
+ | nil => None
+ | instr :: c' =>
+ if is_label lbl instr then Some pos else label_pos lbl (pos + 1) c'
+ end.
+
+Definition nextinstr (rs: regset) :=
+ rs#PC <- (Val.offset_ptr rs#PC Ptrofs.one).
+
+Definition goto_label (f: function) (lbl: label) (rs: regset) (m: mem) :=
+ match label_pos lbl 0 (fn_code f) with
+ | None => Stuck
+ | Some pos =>
+ match rs#PC with
+ | Vptr b ofs => Next (rs#PC <- (Vptr b (Ptrofs.repr pos))) m
+ | _ => Stuck
+ end
+ end.
+
+Section RELSEM.
+
+Variable ge: genv.
+
+(** Evaluating an addressing mode *)
+
+Definition eval_addressing (a: addressing) (rs: regset): val :=
+ match a with
+ | ADimm base n => Val.addl rs#base (Vlong n)
+ | ADreg base r => Val.addl rs#base rs#r
+ | ADlsl base r n => Val.addl rs#base (Val.shll rs#r (Vint n))
+ | ADsxt base r n => Val.addl rs#base (Val.shll (Val.longofint rs#r) (Vint n))
+ | ADuxt base r n => Val.addl rs#base (Val.shll (Val.longofintu rs#r) (Vint n))
+ | ADadr base id ofs => Val.addl rs#base (symbol_low ge id ofs)
+ | ADpostincr base n => Vundef
+ end.
+
+Definition is_pair_addressing_mode_correct (a: addressing): bool :=
+ match a with
+ | ADimm _ _ => true
+ | _ => false
+ end.
+
+Definition get_offset_addr (a: addressing) (ofs: Z) : addressing :=
+ match a with
+ | ADimm base n => (ADimm base (Int64.add n (Int64.repr ofs)))
+ | _ => a
+ end.
+
+(** Auxiliaries for memory accesses *)
+
+Definition exec_load (chunk: memory_chunk) (transf: val -> val)
+ (a: addressing) (r: preg) (rs: regset) (m: mem) :=
+ match Mem.loadv chunk m (eval_addressing a rs) with
+ | None => Stuck
+ | Some v => Next (nextinstr (rs#r <- (transf v))) m
+ end.
+
+Definition exec_load_double (chk1 chk2: memory_chunk) (transf: val -> val)
+ (a: addressing) (rd1 rd2: preg) (rs: regset) (m: mem) :=
+ if is_pair_addressing_mode_correct a then
+ let addr := (eval_addressing a rs) in
+ let ofs := match chk1 with | Mint32 | Mfloat32 | Many32 => 4 | _ => 8 end in
+ let addr' := (eval_addressing (get_offset_addr a ofs) rs) in
+ match Mem.loadv chk1 m addr with
+ | None => Stuck
+ | Some v1 =>
+ match Mem.loadv chk2 m addr' with
+ | None => Stuck
+ | Some v2 =>
+ Next (nextinstr ((rs#rd1 <- (transf v1))#rd2 <- (transf v2))) m
+ end
+ end
+ else Stuck.
+
+Definition exec_store (chunk: memory_chunk)
+ (a: addressing) (v: val)
+ (rs: regset) (m: mem) :=
+ match Mem.storev chunk m (eval_addressing a rs) v with
+ | None => Stuck
+ | Some m' => Next (nextinstr rs) m'
+ end.
+
+Definition exec_store_double (chk1 chk2: memory_chunk)
+ (a: addressing) (v1 v2: val)
+ (rs: regset) (m: mem) :=
+ if is_pair_addressing_mode_correct a then
+ let addr := (eval_addressing a rs) in
+ let ofs := match chk1 with | Mint32 | Mfloat32 | Many32 => 4 | _ => 8 end in
+ let addr' := (eval_addressing (get_offset_addr a ofs) rs) in
+ match Mem.storev chk1 m addr v1 with
+ | None => Stuck
+ | Some m' => match Mem.storev chk2 m' addr' v2 with
+ | None => Stuck
+ | Some m'' => Next (nextinstr rs) m''
+ end
+ end
+ else Stuck.
+
Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : outcome :=
match i with
(** Branches *)
@@ -707,13 +861,13 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Pret r =>
Next (rs#PC <- (rs#r)) m
| Pcbnz sz r lbl =>
- match eval_testzero sz rs#r m with
+ match eval_testzero sz rs#r with
| Some true => Next (nextinstr rs) m
| Some false => goto_label f lbl rs m
| None => Stuck
end
| Pcbz sz r lbl =>
- match eval_testzero sz rs#r m with
+ match eval_testzero sz rs#r with
| Some true => goto_label f lbl rs m
| Some false => Next (nextinstr rs) m
| None => Stuck
@@ -781,13 +935,13 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Psubimm X rd r1 n =>
Next (nextinstr (rs#rd <- (Val.subl rs#r1 (Vlong (Int64.repr n))))) m
| Pcmpimm W r1 n =>
- Next (nextinstr (compare_int rs rs#r1 (Vint (Int.repr n)) m)) m
+ Next (nextinstr (compare_int rs rs#r1 (Vint (Int.repr n)))) m
| Pcmpimm X r1 n =>
- Next (nextinstr (compare_long rs rs#r1 (Vlong (Int64.repr n)) m)) m
+ Next (nextinstr (compare_long rs rs#r1 (Vlong (Int64.repr n)))) m
| Pcmnimm W r1 n =>
- Next (nextinstr (compare_int rs rs#r1 (Vint (Int.neg (Int.repr n))) m)) m
+ Next (nextinstr (compare_int rs rs#r1 (Vint (Int.neg (Int.repr n))))) m
| Pcmnimm X r1 n =>
- Next (nextinstr (compare_long rs rs#r1 (Vlong (Int64.neg (Int64.repr n))) m)) m
+ Next (nextinstr (compare_long rs rs#r1 (Vlong (Int64.neg (Int64.repr n))))) m
(** Move integer register *)
| Pmov rd r1 =>
Next (nextinstr (rs#rd <- (rs#r1))) m
@@ -805,9 +959,9 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Porrimm X rd r1 n =>
Next (nextinstr (rs#rd <- (Val.orl rs###r1 (Vlong (Int64.repr n))))) m
| Ptstimm W r1 n =>
- Next (nextinstr (compare_int rs (Val.and rs#r1 (Vint (Int.repr n))) (Vint Int.zero) m)) m
+ Next (nextinstr (compare_int rs (Val.and rs#r1 (Vint (Int.repr n))) (Vint Int.zero))) m
| Ptstimm X r1 n =>
- Next (nextinstr (compare_long rs (Val.andl rs#r1 (Vlong (Int64.repr n))) (Vlong Int64.zero) m)) m
+ Next (nextinstr (compare_long rs (Val.andl rs#r1 (Vlong (Int64.repr n))) (Vlong Int64.zero))) m
(** Move wide immediate *)
| Pmovz W rd n pos =>
Next (nextinstr (rs#rd <- (Vint (Int.repr (Z.shiftl n pos))))) m
@@ -853,22 +1007,22 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Psub X rd r1 r2 s =>
Next (nextinstr (rs#rd <- (Val.subl rs###r1 (eval_shift_op_long rs#r2 s)))) m
| Pcmp W r1 r2 s =>
- Next (nextinstr (compare_int rs rs##r1 (eval_shift_op_int rs#r2 s) m)) m
+ Next (nextinstr (compare_int rs rs##r1 (eval_shift_op_int rs#r2 s))) m
| Pcmp X r1 r2 s =>
- Next (nextinstr (compare_long rs rs###r1 (eval_shift_op_long rs#r2 s) m)) m
+ Next (nextinstr (compare_long rs rs###r1 (eval_shift_op_long rs#r2 s))) m
| Pcmn W r1 r2 s =>
- Next (nextinstr (compare_int rs rs##r1 (Val.neg (eval_shift_op_int rs#r2 s)) m)) m
+ Next (nextinstr (compare_int rs rs##r1 (Val.neg (eval_shift_op_int rs#r2 s)))) m
| Pcmn X r1 r2 s =>
- Next (nextinstr (compare_long rs rs###r1 (Val.negl (eval_shift_op_long rs#r2 s)) m)) m
+ Next (nextinstr (compare_long rs rs###r1 (Val.negl (eval_shift_op_long rs#r2 s)))) m
(** Integer arithmetic, extending register *)
| Paddext rd r1 r2 x =>
Next (nextinstr (rs#rd <- (Val.addl rs#r1 (eval_extend rs#r2 x)))) m
| Psubext rd r1 r2 x =>
Next (nextinstr (rs#rd <- (Val.subl rs#r1 (eval_extend rs#r2 x)))) m
| Pcmpext r1 r2 x =>
- Next (nextinstr (compare_long rs rs#r1 (eval_extend rs#r2 x) m)) m
+ Next (nextinstr (compare_long rs rs#r1 (eval_extend rs#r2 x))) m
| Pcmnext r1 r2 x =>
- Next (nextinstr (compare_long rs rs#r1 (Val.negl (eval_extend rs#r2 x)) m)) m
+ Next (nextinstr (compare_long rs rs#r1 (Val.negl (eval_extend rs#r2 x)))) m
(** Logical, shifted register *)
| Pand W rd r1 r2 s =>
Next (nextinstr (rs#rd <- (Val.and rs##r1 (eval_shift_op_int rs#r2 s)))) m
@@ -895,9 +1049,9 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Porn X rd r1 r2 s =>
Next (nextinstr (rs#rd <- (Val.orl rs###r1 (Val.notl (eval_shift_op_long rs#r2 s))))) m
| Ptst W r1 r2 s =>
- Next (nextinstr (compare_int rs (Val.and rs##r1 (eval_shift_op_int rs#r2 s)) (Vint Int.zero) m)) m
+ Next (nextinstr (compare_int rs (Val.and rs##r1 (eval_shift_op_int rs#r2 s)) (Vint Int.zero))) m
| Ptst X r1 r2 s =>
- Next (nextinstr (compare_long rs (Val.andl rs###r1 (eval_shift_op_long rs#r2 s)) (Vlong Int64.zero) m)) m
+ Next (nextinstr (compare_long rs (Val.andl rs###r1 (eval_shift_op_long rs#r2 s)) (Vlong Int64.zero))) m
(** Variable shifts *)
| Pasrv W rd r1 r2 =>
Next (nextinstr (rs#rd <- (Val.shr rs#r1 rs#r2))) m
@@ -969,10 +1123,16 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
(** Floating-point move *)
| Pfmov rd r1 =>
Next (nextinstr (rs#rd <- (rs#r1))) m
- | Pfmovimms rd f =>
- Next (nextinstr (rs#X16 <- Vundef #rd <- (Vsingle f))) m
+ | Pfmovimms rd f =>
+ if is_immediate_float32 f then
+ Next (nextinstr (rs#rd <- (Vsingle f))) m
+ else
+ Next (nextinstr ((rs#rd <- (Vsingle f))#X16 <- Vundef)) m
| Pfmovimmd rd f =>
- Next (nextinstr (rs#X16 <- Vundef #rd <- (Vfloat f))) m
+ if is_immediate_float64 f then
+ Next (nextinstr (rs#rd <- (Vfloat f))) m
+ else
+ Next (nextinstr ((rs#rd <- (Vfloat f))#X16 <- Vundef)) m
| Pfmovi S rd r1 =>
Next (nextinstr (rs#rd <- (float32_of_bits rs##r1))) m
| Pfmovi D rd r1 =>
@@ -1102,10 +1262,27 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| _ => Stuck
end
| Pbuiltin ef args res => Stuck (**r treated specially below *)
+ (** loads and stores pairs int/int64 *)
+ | Pldpw rd1 rd2 chk1 chk2 a =>
+ exec_load_double chk1 chk2 (fun v => v) a rd1 rd2 rs m
+ | Pldpx rd1 rd2 chk1 chk2 a =>
+ exec_load_double chk1 chk2 (fun v => v) a rd1 rd2 rs m
+ | Pstpw rs1 rs2 chk1 chk2 a =>
+ exec_store_double chk1 chk2 a rs#rs1 rs#rs2 rs m
+ | Pstpx rs1 rs2 chk1 chk2 a =>
+ exec_store_double chk1 chk2 a rs#rs1 rs#rs2 rs m
+ (** loads and stores pairs floating-point *)
+ | Pldps rd1 rd2 chk1 chk2 a =>
+ exec_load_double chk1 chk2 (fun v => v) a rd1 rd2 rs m
+ | Pldpd rd1 rd2 chk1 chk2 a =>
+ exec_load_double chk1 chk2 (fun v => v) a rd1 rd2 rs m
+ | Pstps rs1 rs2 chk1 chk2 a =>
+ exec_store_double chk1 chk2 a rs#rs1 rs#rs2 rs m
+ | Pstpd rs1 rs2 chk1 chk2 a =>
+ exec_store_double chk1 chk2 a rs#rs1 rs#rs2 rs m
+ | Pnop => Next (nextinstr rs) m
(** The following instructions and directives are not generated directly
by Asmgen, so we do not model them. *)
- | Pldp _ _ _
- | Pstp _ _ _
| Pcls _ _ _
| Pclz _ _ _
| Prev _ _ _
@@ -1118,86 +1295,14 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Pfnmsub _ _ _ _ _
| Pfmax _ _ _ _
| Pfmin _ _ _ _
- | Pnop
| Pcfi_adjust _
| Pcfi_rel_offset _ =>
Stuck
end.
-(** Translation of the LTL/Linear/Mach view of machine registers
- to the AArch64 view. Note that no LTL register maps to [X16],
- [X18], nor [X30].
- [X18] is reserved as the platform register and never used by the
- code generated by CompCert.
- [X30] is used for the return address, and can also be used as temporary.
- [X16] can be used as temporary. *)
-
-Definition preg_of (r: mreg) : preg :=
- match r with
- | R0 => X0 | R1 => X1 | R2 => X2 | R3 => X3
- | R4 => X4 | R5 => X5 | R6 => X6 | R7 => X7
- | R8 => X8 | R9 => X9 | R10 => X10 | R11 => X11
- | R12 => X12 | R13 => X13 | R14 => X14 | R15 => X15
- | R17 => X17 | R19 => X19
- | R20 => X20 | R21 => X21 | R22 => X22 | R23 => X23
- | R24 => X24 | R25 => X25 | R26 => X26 | R27 => X27
- | R28 => X28 | R29 => X29
- | F0 => D0 | F1 => D1 | F2 => D2 | F3 => D3
- | F4 => D4 | F5 => D5 | F6 => D6 | F7 => D7
- | F8 => D8 | F9 => D9 | F10 => D10 | F11 => D11
- | F12 => D12 | F13 => D13 | F14 => D14 | F15 => D15
- | F16 => D16 | F17 => D17 | F18 => D18 | F19 => D19
- | F20 => D20 | F21 => D21 | F22 => D22 | F23 => D23
- | F24 => D24 | F25 => D25 | F26 => D26 | F27 => D27
- | F28 => D28 | F29 => D29 | F30 => D30 | F31 => D31
- end.
-
-(** Undefine all registers except SP and callee-save registers *)
-
-Definition undef_caller_save_regs (rs: regset) : regset :=
- fun r =>
- if preg_eq r SP
- || In_dec preg_eq r (List.map preg_of (List.filter is_callee_save all_mregs))
- then rs r
- else Vundef.
-
-(** Extract the values of the arguments of an external call.
- We exploit the calling conventions from module [Conventions], except that
- we use AArch64 registers instead of locations. *)
-
-Inductive extcall_arg (rs: regset) (m: mem): loc -> val -> Prop :=
- | extcall_arg_reg: forall r,
- extcall_arg rs m (R r) (rs (preg_of r))
- | extcall_arg_stack: forall ofs ty bofs v,
- bofs = Stacklayout.fe_ofs_arg + 4 * ofs ->
- Mem.loadv (chunk_of_type ty) m
- (Val.offset_ptr rs#SP (Ptrofs.repr bofs)) = Some v ->
- extcall_arg rs m (Locations.S Outgoing ofs ty) v.
-
-Inductive extcall_arg_pair (rs: regset) (m: mem): rpair loc -> val -> Prop :=
- | extcall_arg_one: forall l v,
- extcall_arg rs m l v ->
- extcall_arg_pair rs m (One l) v
- | extcall_arg_twolong: forall hi lo vhi vlo,
- extcall_arg rs m hi vhi ->
- extcall_arg rs m lo vlo ->
- extcall_arg_pair rs m (Twolong hi lo) (Val.longofwords vhi vlo).
-
-Definition extcall_arguments
- (rs: regset) (m: mem) (sg: signature) (args: list val) : Prop :=
- list_forall2 (extcall_arg_pair rs m) (loc_arguments sg) args.
-
-Definition loc_external_result (sg: signature) : rpair preg :=
- map_rpair preg_of (loc_result sg).
-
-(** Execution of the instruction at [rs#PC]. *)
-
-Inductive state: Type :=
- | State: regset -> mem -> state.
-
Inductive step: state -> trace -> state -> Prop :=
| exec_step_internal:
- forall b ofs f i rs m rs' m',
+ forall b ofs (f:function) i rs m rs' m',
rs PC = Vptr b ofs ->
Genv.find_funct_ptr ge b = Some (Internal f) ->
find_instr (Ptrofs.unsigned ofs) f.(fn_code) = Some i ->
@@ -1212,7 +1317,7 @@ Inductive step: state -> trace -> state -> Prop :=
external_call ef ge vargs m t vres m' ->
rs' = nextinstr
(set_res res vres
- (undef_regs (IR X16 :: IR X30 :: map preg_of (destroyed_by_builtin ef)) rs)) ->
+ (undef_regs (DR X16 :: DR X30 :: map preg_of (destroyed_by_builtin ef)) rs)) ->
step (State rs m) t (State rs' m')
| exec_step_external:
forall b ef args res rs m t rs' m',
@@ -1308,11 +1413,11 @@ Qed.
Definition data_preg (r: preg) : bool :=
match r with
- | IR X16 => false
- | IR X30 => false
- | IR _ => true
- | FR _ => true
+ | DR (IR X16) => false
+ | DR (IR X30) => false
+ | DR (IR _) => true
+ | DR (FR _) => true
| CR _ => false
- | SP => true
+ (* | SP => true; subsumed by IR (iregsp) *)
| PC => false
end.
diff --git a/aarch64/Asmblock.v b/aarch64/Asmblock.v
new file mode 100644
index 00000000..073f1f4c
--- /dev/null
+++ b/aarch64/Asmblock.v
@@ -0,0 +1,1049 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Justus Fasse UGA, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+
+(* Asmblock language for aarch64
+
+WORK IN PROGRESS: we want to define an Asmblock syntax, with an Asmblock semantics
+(e.g. we don't need the parallel semantics of Asmvliw)
+
+
+NOTE: this file is inspired from
+ - aarch64/Asm.v
+ - kvx/Asmvliw.v (only the Asmblock syntax)
+ - kvx/Asmblock.v
+*)
+
+
+(** Abstract syntax and semantics for AArch64 assembly language *)
+
+Require Import Coqlib Zbits Maps.
+Require Import AST Integers Floats.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Locations Conventions.
+Require Stacklayout.
+Require Import OptionMonad Asm.
+Require Import Lia.
+Require Export Asm.
+
+Local Open Scope option_monad_scope.
+
+Notation regset := Asm.regset.
+
+(** * Abstract syntax *)
+
+(* First task: splitting the big [instruction] type below into CFI and basic instructions.
+ Actually a finer splitting in order to regroup "similar" instructions could be much better for automation of the scheduler proof!
+ e.g. "similar" means identical "interface" w.r.t. pseudo-registers when translated to AbstractBB,
+ or with a "similar" semantics.
+
+ see example of loads below.
+*)
+
+(** Control Flow instructions
+
+*)
+Inductive cf_instruction : Type :=
+ | Pb (lbl: label) (**r branch *)
+ | Pbc (c: testcond) (lbl: label) (**r conditional branch *)
+ | Pbl (id: ident) (sg: signature) (**r jump to function and link *)
+ | Pbs (id: ident) (sg: signature) (**r jump to function *)
+ | Pblr (r: ireg) (sg: signature) (**r indirect jump and link *)
+ | Pbr (r: ireg) (sg: signature) (**r indirect jump *)
+ | Pret (r: ireg) (**r return *)
+ | Pcbnz (sz: isize) (r: ireg) (lbl: label) (**r branch if not zero *)
+ | Pcbz (sz: isize) (r: ireg) (lbl: label) (**r branch if zero *)
+ | Ptbnz (sz: isize) (r: ireg) (n: int) (lbl: label) (**r branch if bit n is not zero *)
+ | Ptbz (sz: isize) (r: ireg) (n: int) (lbl: label) (**r branch if bit n is zero *)
+ (** Pseudo-instructions *)
+ | Pbtbl (r1: ireg) (tbl: list label) (**r N-way branch through a jump table *)
+ .
+
+(*
+A builtin is considered as a control-flow instruction, because it could emit a trace (cf. Machblock semantics).
+Here, we do not need to have builtins alone in basic-blocks (on the contrary to KVX bundles).
+*)
+
+Inductive control: Type :=
+ | PCtlFlow (i: cf_instruction)
+ (** Pseudo-instructions *)
+ | Pbuiltin (ef: external_function)
+ (args: list (builtin_arg dreg)) (res: builtin_res dreg) (**r built-in function (pseudo) *)
+ .
+
+(** Basic instructions *)
+
+(* Loads waiting for (rd: dreg) (a: addressing)
+ * XXX Use dreg because exec_load is defined in terms of it, thus allowing us to
+ * treat integer and floating point loads the same. *)
+Inductive load_rd_a: Type :=
+ (* Integer load *)
+ | Pldrw (**r load int32 *)
+ | Pldrw_a (**r load int32 as any32 *)
+ | Pldrx (**r load int64 *)
+ | Pldrx_a (**r load int64 as any64 *)
+ | Pldrb (sz: isize) (**r load int8, zero-extend *)
+ | Pldrsb (sz: isize) (**r load int8, sign-extend *)
+ | Pldrh (sz: isize) (**r load int16, zero-extend *)
+ | Pldrsh (sz: isize) (**r load int16, sign-extend *)
+ | Pldrzw (**r load int32, zero-extend to int64 *)
+ | Pldrsw (**r load int32, sign-extend to int64 *)
+ (* Floating-point load *)
+ | Pldrs (**r load float32 (single precision) *)
+ | Pldrd (**r load float64 (double precision) *)
+ | Pldrd_a (**r load float64 as any64 *)
+ .
+
+Inductive load_rd1_rd2_a: Type :=
+ | Pldpw
+ | Pldpx
+ | Pldps
+ | Pldpd
+ .
+
+Inductive ld_instruction: Type :=
+ | PLd_rd_a (ld: load_rd_a) (rd: dreg) (a: addressing)
+ | Pldp (ld: load_rd1_rd2_a) (rd1 rd2: dreg) (chk1 chk2: memory_chunk) (a: addressing) (**r load two int64 *)
+ .
+
+Inductive store_rs_a : Type :=
+ (* Integer store *)
+ | Pstrw (**r store int32 *)
+ | Pstrw_a (**r store int32 as any32 *)
+ | Pstrx (**r store int64 *)
+ | Pstrx_a (**r store int64 as any64 *)
+ | Pstrb (**r store int8 *)
+ | Pstrh (**r store int16 *)
+ (* Floating-point store *)
+ | Pstrs (**r store float32 *)
+ | Pstrd (**r store float64 *)
+ | Pstrd_a (**r store float64 as any64 *)
+ .
+
+Inductive store_rs1_rs2_a : Type :=
+ | Pstpw
+ | Pstpx
+ | Pstps
+ | Pstpd
+ .
+
+Inductive st_instruction : Type :=
+ | PSt_rs_a (st: store_rs_a) (rs: dreg) (a: addressing)
+ | Pstp (st: store_rs1_rs2_a) (rs1 rs2: dreg) (chk1 chk2: memory_chunk) (a: addressing) (**r store two int64 *)
+ .
+
+Inductive arith_p : Type :=
+ (** PC-relative addressing *)
+ | Padrp (id: ident) (ofs: ptrofs) (**r set [rd] to high address of [id + ofs] *)
+ (** Move wide immediate *)
+ | Pmovz (sz: isize) (n: Z) (pos: Z) (**r move [n << pos] to [rd] *)
+ | Pmovn (sz: isize) (n: Z) (pos: Z) (**r move [NOT(n << pos)] to [rd] *)
+ (** Floating-point move *)
+ | Pfmovimms (f: float32) (**r load float32 constant *)
+ | Pfmovimmd (f: float) (**r load float64 constant *)
+.
+
+Inductive arith_comparison_p : Type :=
+ (** Floating-point comparison *)
+ | Pfcmp0 (sz: fsize) (**r compare [r1] and [+0.0] *)
+ (** Integer arithmetic, immediate *)
+ | Pcmpimm (sz: isize) (n: Z) (**r compare *)
+ | Pcmnimm (sz: isize) (n: Z) (**r compare negative *)
+ (** Logical, immediate *)
+ | Ptstimm (sz: isize) (n: Z) (**r and, then set flags *)
+.
+
+Inductive arith_pp : Type :=
+ (** Move integer register *)
+ | Pmov
+ (** Move wide immediate *)
+ (* XXX: has to have the same register supplied both times *)
+ | Pmovk (sz: isize) (n: Z) (pos: Z) (**r insert 16 bits of [n] at [pos] in rd *)
+ (** PC-relative addressing *)
+ | Paddadr (id: ident) (ofs: ptrofs) (**r add the low address of [id + ofs] *)
+ (** Bit-field operations *)
+ | Psbfiz (sz: isize) (r: int) (s: Z) (**r sign extend and shift left *)
+ | Psbfx (sz: isize) (r: int) (s: Z) (**r shift right and sign extend *)
+ | Pubfiz (sz: isize) (r: int) (s: Z) (**r zero extend and shift left *)
+ | Pubfx (sz: isize) (r: int) (s: Z) (**r shift right and zero extend *)
+(* Bit operations are not used in the aarch64/asm semantics
+ (** Bit operations *)
+ | Pcls (sz: isize) (**r count leading sign bits *)
+ | Pclz (sz: isize) (**r count leading zero bits *)
+ | Prev (sz: isize) (**r reverse bytes *)
+ | Prev16 (sz: isize) (**r reverse bytes in each 16-bit word *)
+*)
+ (** Floating-point move *)
+ | Pfmov
+ (** Floating-point conversions *)
+ | Pfcvtds (**r convert float32 to float64 *)
+ | Pfcvtsd (**r convert float64 to float32 *)
+ (** Floating-point arithmetic *)
+ | Pfabs (sz: fsize) (**r absolute value *)
+ | Pfneg (sz: fsize) (**r negation *)
+ (* Pfsqrt is not used in the semantics of aarch64/asm
+ | Pfsqrt (sz: fsize) (**r square root *) *)
+ (** Floating-point conversions *)
+ | Pscvtf (fsz: fsize) (isz: isize) (**r convert signed int to float *)
+ | Pucvtf (fsz: fsize) (isz: isize) (**r convert unsigned int to float *)
+ | Pfcvtzs (isz: isize) (fsz: fsize) (**r convert float to signed int *)
+ | Pfcvtzu (isz: isize) (fsz: fsize) (**r convert float to unsigned int *)
+ (** Integer arithmetic, immediate *)
+ | Paddimm (sz: isize) (n: Z) (**r addition *)
+ | Psubimm (sz: isize) (n: Z) (**r subtraction *)
+.
+
+Inductive arith_comparison_r0r : Type :=
+ (** Integer arithmetic, shifted register *)
+ | Pcmp (is:isize) (s: shift_op) (**r compare *)
+ | Pcmn (is:isize) (s: shift_op) (**r compare negative *)
+ (** Logical, shifted register *)
+ | Ptst (is:isize) (s: shift_op) (**r and, then set flags *)
+.
+
+Inductive arith_comparison_pp : Type :=
+ (** Integer arithmetic, extending register *)
+ | Pcmpext (x: extend_op) (**r int64-int32 cmp *)
+ | Pcmnext (x: extend_op) (**r int64-int32 cmn *)
+ (** Floating-point comparison *)
+ | Pfcmp (sz: fsize) (**r compare [r1] and [r2] *)
+.
+
+Inductive arith_ppp : Type :=
+ (** Variable shifts *)
+ | Pasrv (sz: isize) (**r arithmetic right shift *)
+ | Plslv (sz: isize) (**r left shift *)
+ | Plsrv (sz: isize) (**r logical right shift *)
+ | Prorv (sz: isize) (**r rotate right *)
+ (** Integer multiply/divide *)
+ | Psmulh (**r signed multiply high *)
+ | Pumulh (**r unsigned multiply high *)
+ | Psdiv (sz: isize) (**r signed division *)
+ | Pudiv (sz: isize) (**r unsigned division *)
+ (** Integer arithmetic, extending register *)
+ | Paddext (x: extend_op) (**r int64-int32 add *)
+ | Psubext (x: extend_op) (**r int64-int32 sub *)
+ (** Floating-point arithmetic *)
+ | Pfadd (sz: fsize) (**r addition *)
+ | Pfdiv (sz: fsize) (**r division *)
+ | Pfmul (sz: fsize) (**r multiplication *)
+ | Pfsub (sz: fsize) (**r subtraction *)
+.
+
+Inductive arith_rr0r : Type :=
+ (** Integer arithmetic, shifted register *)
+ | Padd (sz:isize) (s: shift_op) (**r addition *)
+ | Psub (sz:isize) (s: shift_op) (**r subtraction *)
+ (** Logical, shifted register *)
+ | Pand (sz:isize) (s: shift_op) (**r and *)
+ | Pbic (sz:isize) (s: shift_op) (**r and-not *)
+ | Peon (sz:isize) (s: shift_op) (**r xor-not *)
+ | Peor (sz:isize) (s: shift_op) (**r xor *)
+ | Porr (sz:isize) (s: shift_op) (**r or *)
+ | Porn (sz:isize) (s: shift_op) (**r or-not *)
+.
+
+
+Inductive arith_rr0 : Type :=
+ (** Logical, immediate *)
+ | Pandimm (sz: isize) (n: Z) (**r and *)
+ | Peorimm (sz: isize) (n: Z) (**r xor *)
+ | Porrimm (sz: isize) (n: Z) (**r or *)
+.
+
+Inductive arith_arrrr0 : Type :=
+ (** Integer multiply/divide *)
+ | Pmadd (sz: isize) (**r multiply-add *)
+ | Pmsub (sz: isize) (**r multiply-sub *)
+.
+
+(* Currently not used by the semantics of aarch64/Asm
+ * Inductive arith_apppp : Type :=
+ * (** Floating-point arithmetic *)
+ * | Pfmadd (sz: fsize) (**r [rd = r3 + r1 * r2] *)
+ * | Pfmsub (sz: fsize) (**r [rd = r3 - r1 * r2] *)
+ * .
+
+ * Inductive arith_aapppp : Type :=
+ * (** Floating-point arithmetic *)
+ * | Pfnmadd (sz: fsize) (**r [rd = - r3 - r1 * r2] *)
+ * | Pfnmsub (sz: fsize) (**r [rd = - r3 + r1 * r2] *)
+ * . *)
+
+(* Notes on the naming scheme used here:
+ * R: ireg
+ * R0: ireg0
+ * Rsp: iregsp
+ * F: freg
+ * W/X: Occur in conjunction with R0, says whether an ireg0 should be evaluated
+ * as W regsiter (32 bit) or X register (64 bit)
+ * S/D: Used for completeness sake. Only used for copying an integer register
+ * to a floating point register. Could be removed.
+ * A: These instructions perform an additional arithmetic operation
+ XXX Does this interpretation match the use in kvx/Asmvliw?
+ * Comparison: For these instructions the first register is not the target.
+ * Instead, the condition register is mutated.
+ *)
+Inductive ar_instruction : Type :=
+ | PArithP (i : arith_p) (rd : dreg)
+ | PArithPP (i : arith_pp) (rd rs : dreg)
+ | PArithPPP (i : arith_ppp) (rd r1 r2 : dreg)
+ | PArithRR0R (i : arith_rr0r) (rd : ireg) (r1 : ireg0) (r2 : ireg)
+ | PArithRR0 (i : arith_rr0) (rd : ireg) (r1 : ireg0)
+ | PArithARRRR0 (i : arith_arrrr0) (rd r1 r2 : ireg) (r3 : ireg0)
+ (* Pfmadd and Pfmsub are currently not used by the semantics of aarch64/Asm
+ | PArithAPPPP (i : arith_apppp) (rd r1 r2 r3 : preg) *)
+ (* Pfnmadd and Pfnmsub are currently not used by the semantics of aarch64/Asm
+ | PArithAAPPPP (i : arith_aapppp) (rd r1 r2 r3 : preg) *)
+ | PArithComparisonPP (i : arith_comparison_pp) (r1 r2 : dreg)
+ | PArithComparisonR0R (i : arith_comparison_r0r) (r1 : ireg0) (r2 : ireg)
+ | PArithComparisonP (i : arith_comparison_p) (r1 : dreg)
+ (* Instructions without indirection sine they would be the only ones *)
+ (* PArithCP: Pcsetm is commented out by aarch64/Asm, so Pcset is alone *)
+ | Pcset (rd : ireg) (c : testcond) (**r set to 1/0 if cond is true/false *)
+ (* PArithFR0 *)
+ | Pfmovi (fsz : fsize) (rd : freg) (r1 : ireg0) (**r copy int reg to FP reg *)
+ (* PArithCPPP *)
+ | Pcsel (rd r1 r2 : dreg) (c : testcond) (**r int/float conditional move *)
+ (* PArithAFFF *)
+ | Pfnmul (fsz : fsize) (rd r1 r2 : freg) (**r multiply-negate *)
+.
+
+Inductive basic : Type :=
+ | PArith (i: ar_instruction)
+ | PLoad (ld: ld_instruction)
+ | PStore (st: st_instruction)
+ | Pallocframe (sz: Z) (linkofs: ptrofs) (**r allocate new stack frame *)
+ | Pfreeframe (sz: Z) (linkofs: ptrofs) (**r deallocate stack frame and restore previous frame *)
+ | Ploadsymbol (rd: ireg) (id: ident) (**r load the address of [id] *)
+ | Pcvtsw2x (rd: ireg) (r1: ireg) (**r sign-extend 32-bit int to 64-bit *)
+ | Pcvtuw2x (rd: ireg) (r1: ireg) (**r zero-extend 32-bit int to 64-bit *)
+ | Pcvtx2w (rd: ireg) (**r retype a 64-bit int as a 32-bit int *)
+ | Pnop (**r no operation *)
+(* NOT USED IN THE SEMANTICS !
+ | Pcfi_adjust (ofs: int) (**r .cfi_adjust debug directive *)
+ | Pcfi_rel_offset (ofs: int) (**r .cfi_rel_offset debug directive *)
+*)
+.
+
+Coercion PCtlFlow: cf_instruction >-> control.
+Coercion PLoad: ld_instruction >-> basic.
+Coercion PStore : st_instruction >-> basic.
+Coercion PArithP: arith_p >-> Funclass.
+Coercion PArithPP: arith_pp >-> Funclass.
+Coercion PArithPPP: arith_ppp >-> Funclass.
+Coercion PArithRR0: arith_rr0 >-> Funclass.
+Coercion PArithRR0R: arith_rr0r >-> Funclass.
+Coercion PArithARRRR0: arith_arrrr0 >-> Funclass.
+Coercion PArithComparisonP: arith_comparison_p >-> Funclass.
+Coercion PArithComparisonPP: arith_comparison_pp >-> Funclass.
+Coercion PArithComparisonR0R: arith_comparison_r0r >-> Funclass.
+Coercion PArith: ar_instruction >-> basic.
+
+
+(* Not used in Coq, declared in ocaml directly in PostpassSchedulingOracle.ml
+Inductive instruction : Type :=
+ | PBasic (i: basic)
+ | PControl (i: control).
+
+Coercion PBasic: basic >-> instruction.
+Coercion PControl: control >-> instruction. *)
+
+(** * Definition of a bblock
+
+A bblock must contain at least one instruction.
+
+This choice simplifies the definition of [find_bblock] below:
+indeed, each address of a code block identifies at most one bblock.
+*)
+
+Definition non_empty_body (body: list basic): bool :=
+ match body with
+ | nil => false
+ | _ => true
+ end.
+
+Definition non_empty_exit (exit: option control): bool :=
+ match exit with
+ | None => false
+ | _ => true
+ end.
+
+Definition non_empty_bblockb (body: list basic) (exit: option control): bool := non_empty_body body || non_empty_exit exit.
+
+(** A bblock is well-formed if he contains at least one instruction. *)
+
+Record bblock := mk_bblock {
+ header: list label;
+ body: list basic;
+ exit: option control;
+ correct: Is_true (non_empty_bblockb body exit)
+}.
+
+(* FIXME? redundant with definition in Machblock *)
+Definition length_opt {A} (o: option A) : nat :=
+ match o with
+ | Some o => 1
+ | None => 0
+ end.
+
+Program Definition no_header (bb : bblock) := {| header := nil; body := body bb; exit := exit bb |}.
+Next Obligation.
+ destruct bb; cbn. assumption.
+Defined.
+
+Program Definition stick_header (h : list label) (bb : bblock) := {| header := h; body := body bb; exit := exit bb |}.
+Next Obligation.
+ destruct bb; cbn. assumption.
+Defined.
+
+(* This notion of size induces the notion of "valid" code address given by [find_bblock]
+
+ The result is in Z to be compatible with operations on PC.
+*)
+Definition size (b:bblock): Z := Z.of_nat (length (header b) + length (body b) + length_opt (exit b)).
+
+Definition bblocks := list bblock.
+
+Fixpoint size_blocks (l: bblocks): Z :=
+ match l with
+ | nil => 0
+ | b :: l =>
+ (size b) + (size_blocks l)
+ end
+ .
+
+Lemma to_nat_pos : forall z:Z, (Z.to_nat z > 0)%nat -> z > 0.
+Proof.
+ intros. destruct z; auto.
+ - contradict H. cbn. apply gt_irrefl.
+ - apply Zgt_pos_0.
+ - contradict H. cbn. apply gt_irrefl.
+Qed.
+
+Lemma size_positive (b:bblock): size b > 0.
+Proof.
+ unfold size. destruct b as [hd bdy ex cor]. cbn.
+ destruct ex; destruct bdy; try (apply to_nat_pos; rewrite Nat2Z.id; cbn; lia);
+ unfold non_empty_bblockb in cor; simpl in cor.
+ inversion cor.
+Qed.
+
+Record function : Type := mkfunction { fn_sig: signature; fn_blocks: bblocks }.
+Definition fundef := AST.fundef function.
+Definition program := AST.program fundef unit.
+
+(** * Operational semantics *)
+
+(** See "Parameters" of the same names in Asm.v *)
+Record aarch64_linker: Type := {
+ symbol_low: ident -> ptrofs -> val;
+ symbol_high: ident -> ptrofs -> val
+}.
+
+Definition genv := Genv.t fundef unit.
+
+Section RELSEM.
+
+Variable lk: aarch64_linker.
+Variable ge: genv.
+
+(** Evaluating an addressing mode *)
+
+Definition eval_addressing (a: addressing) (rs: regset): val :=
+ match a with
+ | ADimm base n => Val.addl rs#base (Vlong n)
+ | ADreg base r => Val.addl rs#base rs#r
+ | ADlsl base r n => Val.addl rs#base (Val.shll rs#r (Vint n))
+ | ADsxt base r n => Val.addl rs#base (Val.shll (Val.longofint rs#r) (Vint n))
+ | ADuxt base r n => Val.addl rs#base (Val.shll (Val.longofintu rs#r) (Vint n))
+ | ADadr base id ofs => Val.addl rs#base (symbol_low lk id ofs)
+ | ADpostincr base n => Vundef
+ end.
+
+(** Auxiliaries for memory accesses *)
+
+Definition exec_load_rd_a (chunk: memory_chunk) (transf: val -> val)
+ (a: addressing) (r: dreg) (rs: regset) (m: mem) :=
+ SOME v <- Mem.loadv chunk m (eval_addressing a rs) IN
+ Next (rs#r <- (transf v)) m.
+
+Definition exec_load_double (chk1 chk2: memory_chunk) (transf: val -> val)
+ (a: addressing) (rd1 rd2: dreg) (rs: regset) (m: mem) :=
+ if is_pair_addressing_mode_correct a then
+ let addr := (eval_addressing a rs) in
+ let ofs := match chk1 with | Mint32 | Mfloat32 | Many32 => 4 | _ => 8 end in
+ let addr' := (eval_addressing (get_offset_addr a ofs) rs) in
+ match Mem.loadv chk1 m addr with
+ | None => Stuck
+ | Some v1 =>
+ match Mem.loadv chk2 m addr' with
+ | None => Stuck
+ | Some v2 =>
+ Next ((rs#rd1 <- (transf v1))#rd2 <- (transf v2)) m
+ end
+ end
+ else Stuck.
+
+Definition exec_store_rs_a (chunk: memory_chunk)
+ (a: addressing) (v: val)
+ (rs: regset) (m: mem) :=
+ SOME m' <- Mem.storev chunk m (eval_addressing a rs) v IN
+ Next rs m'.
+
+Definition exec_store_double (chk1 chk2: memory_chunk)
+ (a: addressing) (v1 v2: val)
+ (rs: regset) (m: mem) :=
+ if is_pair_addressing_mode_correct a then
+ let addr := (eval_addressing a rs) in
+ let ofs := match chk1 with | Mint32 | Mfloat32 | Many32 => 4 | _ => 8 end in
+ let addr' := (eval_addressing (get_offset_addr a ofs) rs) in
+ match Mem.storev chk1 m addr v1 with
+ | None => Stuck
+ | Some m' => match Mem.storev chk2 m' addr' v2 with
+ | None => Stuck
+ | Some m'' => Next rs m''
+ end
+ end
+ else Stuck.
+
+(** execution of loads
+*)
+
+Definition chunk_load (ld: load_rd_a): memory_chunk :=
+ match ld with
+ | Pldrw => Mint32
+ | Pldrw_a => Many32
+ | Pldrx => Mint64
+ | Pldrx_a => Many64
+ | Pldrb _ => Mint8unsigned
+ | Pldrsb _ => Mint8signed
+ | Pldrh _ => Mint16unsigned
+ | Pldrsh _ => Mint16signed
+ | Pldrzw => Mint32
+ | Pldrsw => Mint32
+ | Pldrs => Mfloat32
+ | Pldrd => Mfloat64
+ | Pldrd_a => Many64
+ end.
+
+Definition chunk_store (st: store_rs_a) : memory_chunk :=
+ match st with
+ | Pstrw => Mint32
+ | Pstrw_a => Many32
+ | Pstrx => Mint64
+ | Pstrx_a => Many64
+ | Pstrb => Mint8unsigned
+ | Pstrh => Mint16unsigned
+ | Pstrs => Mfloat32
+ | Pstrd => Mfloat64
+ | Pstrd_a => Many64
+ end.
+
+Definition interp_load (ld: load_rd_a): val -> val :=
+ match ld with
+ | Pldrb X => Val.longofintu
+ | Pldrsb X => Val.longofint
+ | Pldrh X => Val.longofintu
+ | Pldrsh X => Val.longofint
+ | Pldrzw => Val.longofintu
+ | Pldrsw => Val.longofint
+ (* Changed to exhaustive list because I tend to forgot all the places I need
+ * to check when changing things. *)
+ | Pldrb W | Pldrsb W | Pldrh W | Pldrsh W
+ | Pldrw | Pldrw_a | Pldrx
+ | Pldrx_a | Pldrs | Pldrd
+ | Pldrd_a => fun v => v
+ end.
+
+Definition exec_load (ldi: ld_instruction) (rs: regset) (m: mem) :=
+ match ldi with
+ | PLd_rd_a ld rd a => exec_load_rd_a (chunk_load ld) (interp_load ld) a rd rs m
+ | Pldp ld rd1 rd2 chk1 chk2 a => exec_load_double chk1 chk2 (fun v => v) a rd1 rd2 rs m
+ end.
+
+Definition exec_store (sti: st_instruction) (rs: regset) (m: mem) :=
+ match sti with
+ | PSt_rs_a st rsr a => exec_store_rs_a (chunk_store st) a rs#rsr rs m
+ | Pstp st rs1 rs2 chk1 chk2 a => exec_store_double chk1 chk2 a rs#rs1 rs#rs2 rs m
+ end.
+
+(** TODO: redundant w.r.t Machblock ?? *)
+Lemma in_dec (lbl: label) (l: list label): { List.In lbl l } + { ~(List.In lbl l) }.
+Proof.
+ apply List.in_dec.
+ apply Pos.eq_dec.
+Qed.
+
+(** Note: copy-paste from Machblock *)
+Definition is_label (lbl: label) (bb: bblock) : bool :=
+ if in_dec lbl (header bb) then true else false.
+
+Lemma is_label_correct_true lbl bb:
+ List.In lbl (header bb) <-> is_label lbl bb = true.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); simpl; intuition.
+Qed.
+
+Lemma is_label_correct_false lbl bb:
+ ~(List.In lbl (header bb)) <-> is_label lbl bb = false.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); simpl; intuition.
+Qed.
+
+(** convert a label into a position in the code *)
+Fixpoint label_pos (lbl: label) (pos: Z) (lb: bblocks) {struct lb} : option Z :=
+ match lb with
+ | nil => None
+ | b :: lb' => if is_label lbl b then Some pos else label_pos lbl (pos + (size b)) lb'
+ end.
+
+Definition goto_label (f: function) (lbl: label) (rs: regset) (m: mem) :=
+ SOME pos <- label_pos lbl 0 (fn_blocks f) IN
+ match rs#PC with
+ | Vptr b ofs => Next (rs#PC <- (Vptr b (Ptrofs.repr pos))) m
+ | _ => Stuck
+ end.
+
+(** Evaluating a branch
+
+Warning: PC is assumed to be already pointing on the next instruction !
+
+*)
+
+Definition eval_branch (f: function) (lbl: label) (rs: regset) (m: mem) (ores: option bool) :=
+ SOME res <- ores IN
+ if res then goto_label f lbl rs m else Next rs m.
+
+Definition eval_neg_branch (f: function) (lbl: label) (rs: regset) (m: mem) (ores: option bool) :=
+ SOME res <- ores IN
+ if res then Next rs m else goto_label f lbl rs m.
+
+Definition exec_cfi (f: function) (cfi: cf_instruction) (rs: regset) (m: mem) : outcome :=
+ match cfi with
+ (** Branches *)
+ | Pb lbl =>
+ goto_label f lbl rs m
+ | Pbc cond lbl =>
+ eval_branch f lbl rs m (eval_testcond cond rs)
+ | Pbl id sg =>
+ Next (rs#RA <- (rs#PC) #PC <- (Genv.symbol_address ge id Ptrofs.zero)) m
+ | Pbs id sg =>
+ Next (rs#PC <- (Genv.symbol_address ge id Ptrofs.zero)) m
+ | Pblr r sg =>
+ Next (rs#RA <- (rs#PC) #PC <- (rs#r)) m
+ | Pbr r sg =>
+ Next (rs#PC <- (rs#r)) m
+ | Pret r =>
+ Next (rs#PC <- (rs#r)) m
+ | Pcbnz sz r lbl =>
+ eval_neg_branch f lbl rs m (eval_testzero sz rs#r)
+ | Pcbz sz r lbl =>
+ eval_branch f lbl rs m (eval_testzero sz rs#r)
+ | Ptbnz sz r n lbl =>
+ eval_branch f lbl rs m (eval_testbit sz rs#r n)
+ | Ptbz sz r n lbl =>
+ eval_neg_branch f lbl rs m (eval_testbit sz rs#r n)
+ (** Pseudo-instructions *)
+ | Pbtbl r tbl =>
+ match (rs#X16 <- Vundef)#r with
+ | Vint n =>
+ SOME lbl <- list_nth_z tbl (Int.unsigned n) IN
+ goto_label f lbl (rs#X16 <- Vundef) m
+ | _ => Stuck
+ end
+ end.
+
+Definition arith_eval_p (i : arith_p) : val :=
+ match i with
+ | Padrp id ofs => symbol_high lk id ofs
+ (** Move wide immediate *)
+ | Pmovz W n pos => Vint (Int.repr (Z.shiftl n pos))
+ | Pmovz X n pos => Vlong (Int64.repr (Z.shiftl n pos))
+ | Pmovn W n pos => Vint (Int.repr (Z.lnot (Z.shiftl n pos)))
+ | Pmovn X n pos => Vlong (Int64.repr (Z.lnot (Z.shiftl n pos)))
+ (** Floating-point move *)
+ | Pfmovimms f => Vsingle f
+ | Pfmovimmd f => Vfloat f
+ end.
+
+Definition destroy_X16 (i : arith_p) : bool :=
+ match i with
+ | Pfmovimms d => negb (is_immediate_float32 d)
+ | Pfmovimmd d => negb (is_immediate_float64 d)
+ | _ => false
+ end.
+
+Definition if_opt_bool_val (c: option bool) v1 v2: val :=
+ match c with
+ | Some true => v1
+ | Some false => v2
+ | None => Vundef
+ end.
+
+Definition arith_eval_pp i v :=
+ match i with
+ | Pmov => v
+ | Pmovk W n pos => insert_in_int v n pos 16
+ | Pmovk X n pos => insert_in_long v n pos 16
+ | Paddadr id ofs => Val.addl v (symbol_low lk id ofs)
+ | Psbfiz W r s => Val.shl (Val.sign_ext s v) (Vint r)
+ | Psbfiz X r s => Val.shll (Val.sign_ext_l s v) (Vint r)
+ | Psbfx W r s => Val.sign_ext s (Val.shr v (Vint r))
+ | Psbfx X r s => Val.sign_ext_l s (Val.shrl v (Vint r))
+ | Pubfiz W r s => Val.shl (Val.zero_ext s v) (Vint r)
+ | Pubfiz X r s => Val.shll (Val.zero_ext_l s v) (Vint r)
+ | Pubfx W r s => Val.zero_ext s (Val.shru v (Vint r))
+ | Pubfx X r s => Val.zero_ext_l s (Val.shrlu v (Vint r))
+ | Pfmov => v
+ | Pfcvtds => Val.floatofsingle v
+ | Pfcvtsd => Val.singleoffloat v
+ | Pfabs S => Val.absfs v
+ | Pfabs D => Val.absf v
+ | Pfneg S => Val.negfs v
+ | Pfneg D => Val.negf v
+ | Pfcvtzs W S => Val.maketotal (Val.intofsingle v)
+ | Pfcvtzs W D => Val.maketotal (Val.intoffloat v)
+ | Pfcvtzs X S => Val.maketotal (Val.longofsingle v)
+ | Pfcvtzs X D => Val.maketotal (Val.longoffloat v)
+ | Pfcvtzu W S => Val.maketotal (Val.intuofsingle v)
+ | Pfcvtzu W D => Val.maketotal (Val.intuoffloat v)
+ | Pfcvtzu X S => Val.maketotal (Val.longuofsingle v)
+ | Pfcvtzu X D => Val.maketotal (Val.longuoffloat v)
+ | Paddimm W n => Val.add v (Vint (Int.repr n))
+ | Paddimm X n => Val.addl v (Vlong (Int64.repr n))
+ | Psubimm W n => Val.sub v (Vint (Int.repr n))
+ | Psubimm X n => Val.subl v (Vlong (Int64.repr n))
+ | Pscvtf S W => Val.maketotal (Val.singleofint v)
+ | Pscvtf D W => Val.maketotal (Val.floatofint v)
+ | Pscvtf S X => Val.maketotal (Val.singleoflong v)
+ | Pscvtf D X => Val.maketotal (Val.floatoflong v)
+ | Pucvtf S W => Val.maketotal (Val.singleofintu v)
+ | Pucvtf D W => Val.maketotal (Val.floatofintu v)
+ | Pucvtf S X => Val.maketotal (Val.singleoflongu v)
+ | Pucvtf D X => Val.maketotal (Val.floatoflongu v)
+ end.
+
+Definition arith_eval_ppp i v1 v2 :=
+ match i with
+ | Pasrv W => Val.shr v1 v2
+ | Pasrv X => Val.shrl v1 v2
+ | Plslv W => Val.shl v1 v2
+ | Plslv X => Val.shll v1 v2
+ | Plsrv W => Val.shru v1 v2
+ | Plsrv X => Val.shrlu v1 v2
+ | Prorv W => Val.ror v1 v2
+ | Prorv X => Val.rorl v1 v2
+ | Psmulh => Val.mullhs v1 v2
+ | Pumulh => Val.mullhu v1 v2
+ | Psdiv W => Val.maketotal (Val.divs v1 v2)
+ | Psdiv X => Val.maketotal (Val.divls v1 v2)
+ | Pudiv W => Val.maketotal (Val.divu v1 v2)
+ | Pudiv X => Val.maketotal (Val.divlu v1 v2)
+ | Paddext x => Val.addl v1 (eval_extend v2 x)
+ | Psubext x => Val.subl v1 (eval_extend v2 x)
+ | Pfadd S => Val.addfs v1 v2
+ | Pfadd D => Val.addf v1 v2
+ | Pfdiv S => Val.divfs v1 v2
+ | Pfdiv D => Val.divf v1 v2
+ | Pfmul S => Val.mulfs v1 v2
+ | Pfmul D => Val.mulf v1 v2
+ | Pfsub S => Val.subfs v1 v2
+ | Pfsub D => Val.subf v1 v2
+ end.
+
+Definition arith_rr0r_isize (i: arith_rr0r) :=
+ match i with
+ | Padd is _ => is
+ | Psub is _ => is
+ | Pand is _ => is
+ | Pbic is _ => is
+ | Peon is _ => is
+ | Peor is _ => is
+ | Porr is _ => is
+ | Porn is _ => is
+ end.
+
+(* obtain v1 by [ir0 (arith_rr0r_isize i) rs s1] *)
+Definition arith_eval_rr0r i v1 v2 :=
+ match i with
+ | Padd W s => Val.add v1 (eval_shift_op_int v2 s)
+ | Padd X s => Val.addl v1 (eval_shift_op_long v2 s)
+ | Psub W s => Val.sub v1 (eval_shift_op_int v2 s)
+ | Psub X s => Val.subl v1 (eval_shift_op_long v2 s)
+ | Pand W s => Val.and v1 (eval_shift_op_int v2 s)
+ | Pand X s => Val.andl v1 (eval_shift_op_long v2 s)
+ | Pbic W s => Val.and v1 (Val.notint (eval_shift_op_int v2 s))
+ | Pbic X s => Val.andl v1 (Val.notl (eval_shift_op_long v2 s))
+ | Peon W s => Val.xor v1 (Val.notint (eval_shift_op_int v2 s))
+ | Peon X s => Val.xorl v1 (Val.notl (eval_shift_op_long v2 s))
+ | Peor W s => Val.xor v1 (eval_shift_op_int v2 s)
+ | Peor X s => Val.xorl v1 (eval_shift_op_long v2 s)
+ | Porr W s => Val.or v1 (eval_shift_op_int v2 s)
+ | Porr X s => Val.orl v1 (eval_shift_op_long v2 s)
+ | Porn W s => Val.or v1 (Val.notint (eval_shift_op_int v2 s))
+ | Porn X s => Val.orl v1 (Val.notl (eval_shift_op_long v2 s))
+ end.
+
+Definition arith_rr0_isize (i : arith_rr0) :=
+ match i with
+ | Pandimm is _ | Peorimm is _ | Porrimm is _ => is
+ end.
+
+(* obtain v by [ir0 (arith_rr0_isize i) rs s] *)
+Definition arith_eval_rr0 i v :=
+ match i with
+ | Pandimm W n => Val.and v (Vint (Int.repr n))
+ | Pandimm X n => Val.andl v (Vlong (Int64.repr n))
+ | Peorimm W n => Val.xor v (Vint (Int.repr n))
+ | Peorimm X n => Val.xorl v (Vlong (Int64.repr n))
+ | Porrimm W n => Val.or v (Vint (Int.repr n))
+ | Porrimm X n => Val.orl v (Vlong (Int64.repr n))
+ end.
+
+Definition arith_arrrr0_isize (i : arith_arrrr0) :=
+ match i with
+ | Pmadd is | Pmsub is => is
+ end.
+
+(* obtain v3 by [ir0 (arith_arrrr0_isize i) rs s3] *)
+Definition arith_eval_arrrr0 i v1 v2 v3 :=
+ match i with
+ | Pmadd W => Val.add v3 (Val.mul v1 v2)
+ | Pmadd X => Val.addl v3 (Val.mull v1 v2)
+ | Pmsub W => Val.sub v3 (Val.mul v1 v2)
+ | Pmsub X => Val.subl v3 (Val.mull v1 v2)
+ end.
+
+Definition arith_prepare_comparison_pp i (v1 v2 : val) :=
+ match i with
+ | Pcmpext x => (v1, (eval_extend v2 x))
+ | Pcmnext x => (v1, (Val.negl (eval_extend v2 x)))
+ | Pfcmp _ => (v1, v2)
+ end.
+
+Definition arith_comparison_r0r_isize i :=
+ match i with
+ | Pcmp is _ => is
+ | Pcmn is _ => is
+ | Ptst is _ => is
+ end.
+
+Definition arith_prepare_comparison_r0r i v1 v2 :=
+ match i with
+ | Pcmp W s => (v1, (eval_shift_op_int v2 s))
+ | Pcmp X s => (v1, (eval_shift_op_long v2 s))
+ | Pcmn W s => (v1, (Val.neg (eval_shift_op_int v2 s)))
+ | Pcmn X s => (v1, (Val.negl (eval_shift_op_long v2 s)))
+ | Ptst W s => ((Val.and v1 (eval_shift_op_int v2 s)), (Vint Int.zero))
+ | Ptst X s => ((Val.andl v1 (eval_shift_op_long v2 s)), (Vlong Int64.zero))
+ end.
+
+Definition arith_prepare_comparison_p i v :=
+ match i with
+ | Pcmpimm W n => (v, (Vint (Int.repr n)))
+ | Pcmpimm X n => (v, (Vlong (Int64.repr n)))
+ | Pcmnimm W n => (v, (Vint (Int.neg (Int.repr n))))
+ | Pcmnimm X n => (v, (Vlong (Int64.neg (Int64.repr n))))
+ | Ptstimm W n => ((Val.and v (Vint (Int.repr n))), (Vint Int.zero))
+ | Ptstimm X n => ((Val.andl v (Vlong (Int64.repr n))), (Vlong Int64.zero))
+ | Pfcmp0 S => (v, (Vsingle Float32.zero))
+ | Pfcmp0 D => (v, (Vfloat Float.zero))
+ end.
+
+Definition arith_comparison_pp_compare i :=
+ match i with
+ | Pcmpext _ | Pcmnext _ => compare_long
+ | Pfcmp S => compare_single
+ | Pfcmp D => compare_float
+ end.
+
+Definition arith_comparison_p_compare i :=
+ match i with
+ | Pcmpimm W _ | Pcmnimm W _ | Ptstimm W _ => compare_int
+ | Pcmpimm X _ | Pcmnimm X _ | Ptstimm X _ => compare_long
+ | Pfcmp0 S => compare_single
+ | Pfcmp0 D => compare_float
+ end.
+
+Definition exec_arith_instr (ai: ar_instruction) (rs: regset): regset :=
+ match ai with
+ | PArithP i d =>
+ let rs' := rs#d <- (arith_eval_p i) in
+ if destroy_X16 i then rs'#X16 <- Vundef else rs'
+ | PArithPP i d s => rs#d <- (arith_eval_pp i rs#s)
+ | PArithPPP i d s1 s2 => rs#d <- (arith_eval_ppp i rs#s1 rs#s2)
+
+ | PArithRR0R i d s1 s2 => rs#d <- (arith_eval_rr0r i (ir0 (arith_rr0r_isize i) rs s1) rs#s2)
+
+ | PArithRR0 i d s => rs#d <- (arith_eval_rr0 i (ir0 (arith_rr0_isize i) rs s))
+
+ | PArithARRRR0 i d s1 s2 s3 =>
+ rs#d <- (arith_eval_arrrr0 i rs#s1 rs#s2 (ir0 (arith_arrrr0_isize i) rs s3))
+
+ | PArithComparisonPP i s1 s2 =>
+ let (v1, v2) := arith_prepare_comparison_pp i rs#s1 rs#s2 in
+ arith_comparison_pp_compare i rs v1 v2
+ | PArithComparisonR0R i s1 s2 =>
+ let is := arith_comparison_r0r_isize i in
+ let (v1, v2) := arith_prepare_comparison_r0r i (ir0 is rs s1) rs#s2 in
+ (if is (* is W *) then compare_int else compare_long) rs v1 v2
+ | PArithComparisonP i s =>
+ let (v1, v2) := arith_prepare_comparison_p i rs#s in
+ arith_comparison_p_compare i rs v1 v2
+ | Pcset d c => rs#d <- (if_opt_bool_val (eval_testcond c rs) (Vint Int.one) (Vint Int.zero))
+ | Pfmovi S d s => rs#d <- (float32_of_bits rs##s)
+ | Pfmovi D d s => rs#d <- (float64_of_bits rs###s)
+ | Pcsel d s1 s2 c => rs#d <- (if_opt_bool_val (eval_testcond c rs) (rs#s1) (rs#s2))
+ | Pfnmul S d s1 s2 => rs#d <- (Val.negfs (Val.mulfs rs#s1 rs#s2))
+ | Pfnmul D d s1 s2 => rs#d <- (Val.negf (Val.mulf rs#s1 rs#s2))
+ end.
+
+(* basic exec *)
+Definition exec_basic (b: basic) (rs: regset) (m: mem): outcome :=
+ match b with
+ | PArith ai => Next (exec_arith_instr ai rs) m
+ | PLoad ldi => exec_load ldi rs m
+ | PStore sti => exec_store sti rs m
+ | Pallocframe sz pos =>
+ let (m1, stk) := Mem.alloc m 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ SOME m2 <- Mem.storev Mint64 m1 (Val.offset_ptr sp pos) rs#SP IN
+ Next (rs #X29 <- (rs#SP) #SP <- sp #X16 <- Vundef) m2
+ | Pfreeframe sz pos =>
+ SOME v <- Mem.loadv Mint64 m (Val.offset_ptr rs#SP pos) IN
+ match rs#SP with
+ | Vptr stk ofs =>
+ SOME m' <- Mem.free m stk 0 sz IN
+ Next (rs#SP <- v #X16 <- Vundef) m'
+ | _ => Stuck
+ end
+ | Ploadsymbol rd id =>
+ Next (rs#rd <- (Genv.symbol_address ge id Ptrofs.zero)) m
+ | Pcvtsw2x rd r1 =>
+ Next (rs#rd <- (Val.longofint rs#r1)) m
+ | Pcvtuw2x rd r1 =>
+ Next (rs#rd <- (Val.longofintu rs#r1)) m
+ | Pcvtx2w rd =>
+ Next (rs#rd <- (Val.loword rs#rd)) m
+ | Pnop => Next rs m
+ end.
+
+(** execution of the body of a bblock *)
+Fixpoint exec_body (body: list basic) (rs: regset) (m: mem): outcome :=
+ match body with
+ | nil => Next rs m
+ | bi::body' =>
+ SOME o <- exec_basic bi rs m IN
+ exec_body body' (_rs o) (_m o)
+ end.
+
+Definition incrPC size_b (rs: regset) :=
+ rs#PC <- (Val.offset_ptr rs#PC size_b).
+
+Definition estep (f: function) oc size_b (rs: regset) (m: mem) :=
+ match oc with
+ | Some (PCtlFlow cfi) => exec_cfi f cfi (incrPC size_b rs) m
+ | Some (Pbuiltin ef args res) => Next (incrPC size_b rs) m
+ | None => Next (incrPC size_b rs) m
+ end.
+
+(** execution of the exit instruction of a bblock *)
+Inductive exec_exit (f: function) size_b (rs: regset) (m: mem): (option control) -> trace -> regset -> mem -> Prop :=
+ | none_step:
+ exec_exit f size_b rs m None E0 (incrPC size_b rs) m
+ | cfi_step (cfi: cf_instruction) rs' m':
+ exec_cfi f cfi (incrPC size_b rs) m = Next rs' m' ->
+ exec_exit f size_b rs m (Some (PCtlFlow cfi)) E0 rs' m'
+ | builtin_step ef args res vargs t vres rs' m':
+ eval_builtin_args ge (fun (r: dreg) => rs r) rs#SP m args vargs ->
+ external_call ef ge vargs m t vres m' ->
+ rs' = incrPC size_b
+ (set_res (map_builtin_res DR res) vres
+ (undef_regs (DR (IR X16) :: DR (IR X30) :: map preg_of (destroyed_by_builtin ef)) rs)) ->
+ exec_exit f size_b rs m (Some (Pbuiltin ef args res)) t rs' m'
+ .
+
+(*Definition bbstep f cfi size_b bdy rs m :=*)
+ (*match exec_body bdy rs m with*)
+ (*| Some (State rs' m') => estep f cfi size_b rs' m'*)
+ (*| Stuck => Stuck*)
+ (*end.*)
+
+Definition bbstep f bb rs m :=
+ match exec_body (body bb) rs m with
+ | Some (State rs' m') => estep f (exit bb) (Ptrofs.repr (size bb)) rs' m'
+ | Stuck => Stuck
+ end.
+
+Definition exec_bblock (f: function) (b: bblock) (rs: regset) (m: mem) (t:trace) (rs':regset) (m':mem): Prop
+ := exists rs1 m1, exec_body (body b) rs m = Next rs1 m1 /\ exec_exit f (Ptrofs.repr (size b)) rs1 m1 (exit b) t rs' m'.
+
+Fixpoint find_bblock (pos: Z) (lb: bblocks) {struct lb} : option bblock :=
+ match lb with
+ | nil => None
+ | b :: il =>
+ if zlt pos 0 then None (* NOTE: It is impossible to branch inside a block *)
+ else if zeq pos 0 then Some b
+ else find_bblock (pos - (size b)) il
+ end.
+
+(** Execution of the instruction at [rs PC]. *)
+
+Inductive step: state -> trace -> state -> Prop :=
+ | exec_step_internal:
+ forall b ofs f bi rs m t rs' m',
+ rs PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bi ->
+ exec_bblock f bi rs m t rs' m' ->
+ step (State rs m) t (State rs' m')
+ | exec_step_external:
+ forall b ef args res rs m t rs' m',
+ rs PC = Vptr b Ptrofs.zero ->
+ Genv.find_funct_ptr ge b = Some (External ef) ->
+ external_call ef ge args m t res m' ->
+ extcall_arguments rs m (ef_sig ef) args ->
+ rs' = (set_pair (loc_external_result (ef_sig ef) ) res (undef_caller_save_regs rs))#PC <- (rs RA) ->
+ step (State rs m) t (State rs' m')
+ .
+
+
+End RELSEM.
+
+
+(** Execution of whole programs. *)
+
+Inductive initial_state (p: program): state -> Prop :=
+ | initial_state_intro: forall m0,
+ Genv.init_mem p = Some m0 ->
+ let ge := Genv.globalenv p in
+ let rs0 :=
+ (Pregmap.init Vundef)
+ # PC <- (Genv.symbol_address ge p.(prog_main) Ptrofs.zero)
+ # RA <- Vnullptr
+ # SP <- Vnullptr in
+ initial_state p (State rs0 m0).
+
+Inductive final_state: state -> int -> Prop :=
+ | final_state_intro: forall rs m r,
+ rs#PC = Vnullptr ->
+ rs#X0 = Vint r ->
+ final_state (State rs m) r.
+
+Definition semantics (lk: aarch64_linker) (p: program) :=
+ Semantics (step lk) (initial_state p) final_state (Genv.globalenv p).
diff --git a/aarch64/Asmblockdeps.v b/aarch64/Asmblockdeps.v
new file mode 100644
index 00000000..5cd049c5
--- /dev/null
+++ b/aarch64/Asmblockdeps.v
@@ -0,0 +1,2688 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Translation from [Asmblock] to [AbstractBB] *)
+
+(** We define a specific instance [L] of [AbstractBB] and translate [bblocks] from [Asmblock] into [L].
+ [AbstractBB] will then define a sequential semantics for [L].
+ We prove a bisimulation between the sequential semantics of [L] and [Asmblock].
+ Then, the checker on [Asmblock] is deduced from those of [L].
+ *)
+
+Require Import AST.
+Require Import Asm Asmblock.
+Require Import Asmblockprops.
+Require Import Values.
+Require Import Globalenvs.
+Require Import Memory.
+Require Import Errors.
+Require Import Integers.
+Require Import Floats.
+Require Import ZArith.
+Require Import Coqlib.
+Require Import ImpSimuTest.
+Require Import Axioms.
+Require Import Permutation.
+Require Import Events.
+
+Require Import Lia.
+
+Import ListNotations.
+Local Open Scope list_scope.
+
+Open Scope impure.
+(** auxiliary treatments of builtins *)
+
+Definition is_builtin(ex: option control): bool :=
+ match ex with
+ | Some (Pbuiltin _ _ _) => true
+ | _ => false
+ end.
+
+Definition has_builtin(bb: bblock): bool :=
+ is_builtin (exit bb).
+
+Remark builtin_arg_eq_dreg: forall (a b: builtin_arg dreg), {a=b} + {a<>b}.
+Proof.
+ intros.
+ apply (builtin_arg_eq dreg_eq).
+Qed.
+
+Remark builtin_res_eq_dreg: forall (a b: builtin_res dreg), {a=b} + {a<>b}.
+Proof.
+ intros.
+ apply (builtin_res_eq dreg_eq).
+Qed.
+
+Definition assert_same_builtin (bb1 bb2: bblock): ?? unit :=
+ match exit bb1 with
+ | Some (Pbuiltin ef1 lbar1 brr1) =>
+ match exit bb2 with
+ | Some (Pbuiltin ef2 lbar2 brr2) =>
+ if (external_function_eq ef1 ef2) then
+ if (list_eq_dec builtin_arg_eq_dreg lbar1 lbar2) then
+ if (builtin_res_eq_dreg brr1 brr2) then RET tt
+ else FAILWITH "Different brr in Pbuiltin"
+ else FAILWITH "Different lbar in Pbuiltin"
+ else FAILWITH "Different ef in Pbuiltin"
+ | _ => FAILWITH "Expected a builtin: found something else" (* XXX: on peut raffiner le message d'erreur si nécessaire *)
+ end
+ | _ => match exit bb2 with
+ | Some (Pbuiltin ef2 lbar2 brr2) => FAILWITH "Expected a something else: found a builtin"
+ | _ => RET tt (* ok *)
+ end
+ end.
+
+Lemma assert_same_builtin_correct (bb1 bb2: bblock):
+ WHEN assert_same_builtin bb1 bb2 ~> _ THEN
+ has_builtin bb1 = true \/ has_builtin bb2 = true -> exit bb1 = exit bb2.
+Proof.
+ unfold assert_same_builtin, has_builtin.
+ destruct (exit bb1) as [[]|]; simpl;
+ destruct (exit bb2) as [[]|]; wlp_simplify; try congruence.
+Qed.
+Global Opaque assert_same_builtin.
+Local Hint Resolve assert_same_builtin_correct: wlp.
+
+(** Definition of [L] *)
+
+Module P<: ImpParam.
+Module R := Pos.
+
+Section IMPPARAM.
+
+Definition env := Genv.t fundef unit.
+
+Record genv_wrap := { _genv: env; _fn: function; _lk: aarch64_linker }.
+Definition genv := genv_wrap.
+
+Variable Ge: genv.
+
+Inductive value_wrap :=
+ | Val (v: val)
+ | Memstate (m: mem)
+ | Bool (b: bool)
+.
+
+Definition value := value_wrap.
+
+Record CRflags := { _CN: val; _CZ:val; _CC: val; _CV: val }.
+
+Inductive control_op :=
+ | Ob (l: label)
+ | Obc (c: testcond) (l: label)
+ | Obl (id: ident)
+ | Obs (id: ident)
+ | Ocbnz (sz: isize) (l: label)
+ | Ocbz (sz: isize) (l: label)
+ | Otbnz (sz: isize) (n: int) (l: label)
+ | Otbz (sz: isize) (n: int) (l: label)
+ | Obtbl (l: list label)
+ | OError
+ | OIncremPC (sz: Z)
+.
+
+Inductive arith_op :=
+ | OArithP (n: arith_p)
+ | OArithPP (n: arith_pp)
+ | OArithPPP (n: arith_ppp)
+ | OArithRR0R (n: arith_rr0r)
+ | OArithRR0R_XZR (n: arith_rr0r) (vz: val)
+ | OArithRR0 (n: arith_rr0)
+ | OArithRR0_XZR (n: arith_rr0) (vz: val)
+ | OArithARRRR0 (n: arith_arrrr0)
+ | OArithARRRR0_XZR (n: arith_arrrr0) (vz: val)
+ | OArithComparisonPP_CN (n: arith_comparison_pp)
+ | OArithComparisonPP_CZ (n: arith_comparison_pp)
+ | OArithComparisonPP_CC (n: arith_comparison_pp)
+ | OArithComparisonPP_CV (n: arith_comparison_pp)
+ | OArithComparisonR0R_CN (n: arith_comparison_r0r) (is: isize)
+ | OArithComparisonR0R_CZ (n: arith_comparison_r0r) (is: isize)
+ | OArithComparisonR0R_CC (n: arith_comparison_r0r) (is: isize)
+ | OArithComparisonR0R_CV (n: arith_comparison_r0r) (is: isize)
+ | OArithComparisonR0R_CN_XZR (n: arith_comparison_r0r) (is: isize) (vz: val)
+ | OArithComparisonR0R_CZ_XZR (n: arith_comparison_r0r) (is: isize) (vz: val)
+ | OArithComparisonR0R_CC_XZR (n: arith_comparison_r0r) (is: isize) (vz: val)
+ | OArithComparisonR0R_CV_XZR (n: arith_comparison_r0r) (is: isize) (vz: val)
+ | OArithComparisonP_CN (n: arith_comparison_p)
+ | OArithComparisonP_CZ (n: arith_comparison_p)
+ | OArithComparisonP_CC (n: arith_comparison_p)
+ | OArithComparisonP_CV (n: arith_comparison_p)
+ | Ocset (c: testcond)
+ | Ofmovi (fsz: fsize)
+ | Ofmovi_XZR (fsz: fsize)
+ | Ocsel (c: testcond)
+ | Ofnmul (fsz: fsize)
+.
+
+Inductive store_op :=
+ | Ostore1 (st: store_rs_a) (chunk: memory_chunk) (a: addressing)
+ | Ostore2 (st: store_rs_a) (chunk: memory_chunk) (a: addressing)
+ | OstoreU (st: store_rs_a) (chunk: memory_chunk) (a: addressing)
+.
+
+Inductive load_op :=
+ | Oload1 (ld: load_rd_a) (chunk: memory_chunk) (a: addressing)
+ | Oload2 (ld: load_rd_a) (chunk: memory_chunk) (a: addressing)
+ | OloadU (ld: load_rd_a) (chunk: memory_chunk) (a: addressing)
+.
+
+Inductive allocf_op :=
+ | OAllocf_SP (sz: Z) (linkofs: ptrofs)
+ | OAllocf_Mem (sz: Z) (linkofs: ptrofs)
+.
+
+Inductive freef_op :=
+ | OFreef_SP (sz: Z) (linkofs: ptrofs)
+ | OFreef_Mem (sz: Z) (linkofs: ptrofs)
+.
+
+Inductive op_wrap :=
+ (* arithmetic operation *)
+ | Arith (op: arith_op)
+ | Load (ld: load_op)
+ | Store (st: store_op)
+ | Allocframe (al: allocf_op)
+ | Freeframe (fr: freef_op)
+ | Loadsymbol (id: ident)
+ | Cvtsw2x
+ | Cvtuw2x
+ | Cvtx2w
+ | Control (co: control_op)
+ | Constant (v: val)
+.
+
+Definition op:=op_wrap.
+
+Coercion Arith: arith_op >-> op_wrap.
+Coercion Control: control_op >-> op_wrap.
+
+Definition v_compare_int (v1 v2: val) : CRflags :=
+ {| _CN := (Val.negative (Val.sub v1 v2));
+ _CZ := (Val.mxcmpu Ceq v1 v2);
+ _CC := (Val.mxcmpu Cge v1 v2);
+ _CV := (Val.sub_overflow v1 v2) |}.
+
+Definition v_compare_long (v1 v2: val) : CRflags :=
+ {| _CN := (Val.negativel (Val.subl v1 v2));
+ _CZ := (Val.mxcmplu Ceq v1 v2);
+ _CC := (Val.mxcmplu Cge v1 v2);
+ _CV := (Val.subl_overflow v1 v2) |}.
+
+Definition v_compare_float (v1 v2: val) : CRflags :=
+ match v1, v2 with
+ | Vfloat f1, Vfloat f2 =>
+ {| _CN := (Val.of_bool (Float.cmp Clt f1 f2));
+ _CZ := (Val.of_bool (Float.cmp Ceq f1 f2));
+ _CC := (Val.of_bool (negb (Float.cmp Clt f1 f2)));
+ _CV := (Val.of_bool (negb (Float.ordered f1 f2))) |}
+ | _, _ =>
+ {| _CN := Vundef;
+ _CZ := Vundef;
+ _CC := Vundef;
+ _CV := Vundef |}
+ end.
+
+Definition v_compare_single (v1 v2: val) : CRflags :=
+ match v1, v2 with
+ | Vsingle f1, Vsingle f2 =>
+ {| _CN := (Val.of_bool (Float32.cmp Clt f1 f2));
+ _CZ := (Val.of_bool (Float32.cmp Ceq f1 f2));
+ _CC := (Val.of_bool (negb (Float32.cmp Clt f1 f2)));
+ _CV := (Val.of_bool (negb (Float32.ordered f1 f2))) |}
+ | _, _ =>
+ {| _CN := Vundef;
+ _CZ := Vundef;
+ _CC := Vundef;
+ _CV := Vundef |}
+ end.
+
+Definition arith_eval_comparison_pp (n: arith_comparison_pp) (v1 v2: val) :=
+ let (v1',v2') := arith_prepare_comparison_pp n v1 v2 in
+ match n with
+ | Pcmpext _ | Pcmnext _ => v_compare_long v1' v2'
+ | Pfcmp S => v_compare_single v1' v2'
+ | Pfcmp D => v_compare_float v1' v2'
+ end.
+
+Definition arith_eval_comparison_p (n: arith_comparison_p) (v: val) :=
+ let (v1',v2') := arith_prepare_comparison_p n v in
+ match n with
+ | Pcmpimm W _ | Pcmnimm W _ | Ptstimm W _ => v_compare_int v1' v2'
+ | Pcmpimm X _ | Pcmnimm X _ | Ptstimm X _ => v_compare_long v1' v2'
+ | Pfcmp0 S => v_compare_single v1' v2'
+ | Pfcmp0 D => v_compare_float v1' v2'
+ end.
+
+Definition arith_eval_comparison_r0r (n: arith_comparison_r0r) (v1 v2: val) (is: isize) :=
+ let (v1',v2') := arith_prepare_comparison_r0r n v1 v2 in
+ if is then v_compare_int v1' v2' else v_compare_long v1' v2'.
+
+Definition flags_testcond_value (c: testcond) (vCN vCZ vCC vCV: val) :=
+ match c with
+ | TCeq => (**r equal *)
+ match vCZ with
+ | Vint n => Some (Int.eq n Int.one)
+ | _ => None
+ end
+ | TCne => (**r not equal *)
+ match vCZ with
+ | Vint n => Some (Int.eq n Int.zero)
+ | _ => None
+ end
+ | TClo => (**r unsigned less than *)
+ match vCC with
+ | Vint n => Some (Int.eq n Int.zero)
+ | _ => None
+ end
+ | TCls => (**r unsigned less or equal *)
+ match vCC, vCZ with
+ | Vint c, Vint z => Some (Int.eq c Int.zero || Int.eq z Int.one)
+ | _, _ => None
+ end
+ | TChs => (**r unsigned greater or equal *)
+ match vCC with
+ | Vint n => Some (Int.eq n Int.one)
+ | _ => None
+ end
+ | TChi => (**r unsigned greater *)
+ match vCC, vCZ with
+ | Vint c, Vint z => Some (Int.eq c Int.one && Int.eq z Int.zero)
+ | _, _ => None
+ end
+ | TClt => (**r signed less than *)
+ match vCV, vCN with
+ | Vint o, Vint s => Some (Int.eq (Int.xor o s) Int.one)
+ | _, _ => None
+ end
+ | TCle => (**r signed less or equal *)
+ match vCV, vCN, vCZ with
+ | Vint o, Vint s, Vint z => Some (Int.eq (Int.xor o s) Int.one || Int.eq z Int.one)
+ | _, _, _ => None
+ end
+ | TCge => (**r signed greater or equal *)
+ match vCV, vCN with
+ | Vint o, Vint s => Some (Int.eq (Int.xor o s) Int.zero)
+ | _, _ => None
+ end
+ | TCgt => (**r signed greater *)
+ match vCV, vCN, vCZ with
+ | Vint o, Vint s, Vint z => Some (Int.eq (Int.xor o s) Int.zero && Int.eq z Int.zero)
+ | _, _, _ => None
+ end
+ | TCpl => (**r positive *)
+ match vCN with
+ | Vint n => Some (Int.eq n Int.zero)
+ | _ => None
+ end
+ | TCmi => (**r negative *)
+ match vCN with
+ | Vint n => Some (Int.eq n Int.one)
+ | _ => None
+ end
+ end.
+
+(* The is argument is used to identify the source inst and avoid rewriting some code
+ 0 -> Ocset
+ 1 -> Ocsel
+ 2 -> Obc *)
+Definition cond_eval_is (c: testcond) (v1 v2 vCN vCZ vCC vCV: val) (is: Z) :=
+ let res := flags_testcond_value c vCN vCZ vCC vCV in
+ match is, res with
+ | 0, res => Some (Val (if_opt_bool_val res (Vint Int.one) (Vint Int.zero)))
+ | 1, res => Some (Val (if_opt_bool_val res v1 v2))
+ | 2, Some b => Some (Bool (b))
+ | _, _ => None
+ end.
+
+Definition fmovi_eval (fsz: fsize) (v: val) :=
+ match fsz with
+ | S => float32_of_bits v
+ | D => float64_of_bits v
+ end.
+
+Definition fmovi_eval_xzr (fsz: fsize) :=
+ match fsz with
+ | S => float32_of_bits (Vint Int.zero)
+ | D => float64_of_bits (Vlong Int64.zero)
+ end.
+
+Definition fnmul_eval (fsz: fsize) (v1 v2: val) :=
+ match fsz with
+ | S => Val.negfs (Val.mulfs v1 v2)
+ | D => Val.negf (Val.mulf v1 v2)
+ end.
+
+Definition cflags_eval (c: testcond) (l: list value) (v1 v2: val) (is: Z) :=
+ match c, l with
+ | TCeq, [Val vCZ] => cond_eval_is TCeq v1 v2 Vundef vCZ Vundef Vundef is
+ | TCne, [Val vCZ] => cond_eval_is TCne v1 v2 Vundef vCZ Vundef Vundef is
+ | TChs, [Val vCC] => cond_eval_is TChs v1 v2 Vundef Vundef vCC Vundef is
+ | TClo, [Val vCC] => cond_eval_is TClo v1 v2 Vundef Vundef vCC Vundef is
+ | TCmi, [Val vCN] => cond_eval_is TCmi v1 v2 vCN Vundef Vundef Vundef is
+ | TCpl, [Val vCN] => cond_eval_is TCpl v1 v2 vCN Vundef Vundef Vundef is
+ | TChi, [Val vCZ; Val vCC] => cond_eval_is TChi v1 v2 Vundef vCZ vCC Vundef is
+ | TCls, [Val vCZ; Val vCC] => cond_eval_is TCls v1 v2 Vundef vCZ vCC Vundef is
+ | TCge, [Val vCN; Val vCV] => cond_eval_is TCge v1 v2 vCN Vundef Vundef vCV is
+ | TClt, [Val vCN; Val vCV] => cond_eval_is TClt v1 v2 vCN Vundef Vundef vCV is
+ | TCgt, [Val vCN; Val vCZ; Val vCV] => cond_eval_is TCgt v1 v2 vCN vCZ Vundef vCV is
+ | TCle, [Val vCN; Val vCZ; Val vCV] => cond_eval_is TCle v1 v2 vCN vCZ Vundef vCV is
+ | _, _ => None
+ end.
+
+Definition arith_op_eval (op: arith_op) (l: list value) :=
+ match op, l with
+ | OArithP n, [] => Some (Val (arith_eval_p Ge.(_lk) n))
+ | OArithPP n, [Val v] => Some (Val (arith_eval_pp Ge.(_lk) n v))
+ | OArithPPP n, [Val v1; Val v2] => Some (Val (arith_eval_ppp n v1 v2))
+ | OArithRR0R n, [Val v1; Val v2] => Some (Val (arith_eval_rr0r n v1 v2))
+ | OArithRR0R_XZR n vz, [Val v] => Some (Val (arith_eval_rr0r n vz v))
+ | OArithRR0 n, [Val v] => Some (Val (arith_eval_rr0 n v))
+ | OArithRR0_XZR n vz, [] => Some (Val (arith_eval_rr0 n vz))
+ | OArithARRRR0 n, [Val v1; Val v2; Val v3] => Some (Val (arith_eval_arrrr0 n v1 v2 v3))
+ | OArithARRRR0_XZR n vz, [Val v1; Val v2] => Some (Val (arith_eval_arrrr0 n v1 v2 vz))
+ | OArithComparisonPP_CN n, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_pp n v1 v2).(_CN)))
+ | OArithComparisonPP_CZ n, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_pp n v1 v2).(_CZ)))
+ | OArithComparisonPP_CC n, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_pp n v1 v2).(_CC)))
+ | OArithComparisonPP_CV n, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_pp n v1 v2).(_CV)))
+ | OArithComparisonR0R_CN n is, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_r0r n v1 v2 is).(_CN)))
+ | OArithComparisonR0R_CZ n is, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_r0r n v1 v2 is).(_CZ)))
+ | OArithComparisonR0R_CC n is, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_r0r n v1 v2 is).(_CC)))
+ | OArithComparisonR0R_CV n is, [Val v1; Val v2] => Some (Val ((arith_eval_comparison_r0r n v1 v2 is).(_CV)))
+ | OArithComparisonR0R_CN_XZR n is vz, [Val v2] => Some (Val ((arith_eval_comparison_r0r n vz v2 is).(_CN)))
+ | OArithComparisonR0R_CZ_XZR n is vz, [Val v2] => Some (Val ((arith_eval_comparison_r0r n vz v2 is).(_CZ)))
+ | OArithComparisonR0R_CC_XZR n is vz, [Val v2] => Some (Val ((arith_eval_comparison_r0r n vz v2 is).(_CC)))
+ | OArithComparisonR0R_CV_XZR n is vz, [Val v2] => Some (Val ((arith_eval_comparison_r0r n vz v2 is).(_CV)))
+ | OArithComparisonP_CN n, [Val v] => Some (Val ((arith_eval_comparison_p n v).(_CN)))
+ | OArithComparisonP_CZ n, [Val v] => Some (Val ((arith_eval_comparison_p n v).(_CZ)))
+ | OArithComparisonP_CC n, [Val v] => Some (Val ((arith_eval_comparison_p n v).(_CC)))
+ | OArithComparisonP_CV n, [Val v] => Some (Val ((arith_eval_comparison_p n v).(_CV)))
+ | Ocset c, l => cflags_eval c l Vundef Vundef 0
+ | Ofmovi fsz, [Val v] => Some (Val (fmovi_eval fsz v))
+ | Ofmovi_XZR fsz, [] => Some (Val (fmovi_eval_xzr fsz))
+ | Ocsel c, Val v1 :: Val v2 :: l' => cflags_eval c l' v1 v2 1
+ | Ofnmul fsz, [Val v1; Val v2] => Some (Val (fnmul_eval fsz v1 v2))
+ | _, _ => None
+ end.
+
+Definition call_ll_storev (c: memory_chunk) (m: mem) (v: option val) (vs: val) :=
+ match v with
+ | Some va => match Mem.storev c m va vs with
+ | Some m' => Some (Memstate m')
+ | None => None
+ end
+ | None => None (* should never occurs *)
+ end.
+
+Definition exec_store1 (n: store_rs_a) (m: mem) (chunk: memory_chunk) (a: addressing) (vr vs: val) :=
+ let v :=
+ match a with
+ | ADimm _ n => Some (Val.addl vs (Vlong n))
+ | ADadr _ id ofs => Some (Val.addl vs (symbol_low Ge.(_lk) id ofs))
+ | _ => None
+ end in
+ call_ll_storev chunk m v vr.
+
+Definition exec_store2 (n: store_rs_a) (m: mem) (chunk: memory_chunk) (a: addressing) (vr vs1 vs2: val) :=
+ let v :=
+ match a with
+ | ADreg _ _ => Some (Val.addl vs1 vs2)
+ | ADlsl _ _ n => Some (Val.addl vs1 (Val.shll vs2 (Vint n)))
+ | ADsxt _ _ n => Some (Val.addl vs1 (Val.shll (Val.longofint vs2) (Vint n)))
+ | ADuxt _ _ n => Some (Val.addl vs1 (Val.shll (Val.longofintu vs2) (Vint n)))
+ | _ => None
+ end in
+ call_ll_storev chunk m v vr.
+
+Definition exec_storeU (n: store_rs_a) (m: mem) (chunk: memory_chunk) (a: addressing) (vr: val) :=
+ call_ll_storev chunk m None vr.
+
+Definition goto_label_deps (f: function) (lbl: label) (vpc: val) :=
+ match label_pos lbl 0 (fn_blocks f) with
+ | None => None
+ | Some pos =>
+ match vpc with
+ | Vptr b ofs => Some (Val (Vptr b (Ptrofs.repr pos)))
+ | _ => None
+ end
+ end.
+
+Definition control_eval (o: control_op) (l: list value) :=
+ let (ge, fn, lk) := Ge in
+ match o, l with
+ | Ob lbl, [Val vpc] => goto_label_deps fn lbl vpc
+ | Obc c lbl, Val vpc :: l' => match cflags_eval c l' Vundef Vundef 2 with
+ | Some (Bool true) => goto_label_deps fn lbl vpc
+ | Some (Bool false) => Some (Val vpc)
+ | _ => None
+ end
+ | Obl id, [] => Some (Val (Genv.symbol_address Ge.(_genv) id Ptrofs.zero))
+ | Obs id, [] => Some (Val (Genv.symbol_address Ge.(_genv) id Ptrofs.zero))
+ | Ocbnz sz lbl, [Val v; Val vpc] => match eval_testzero sz v with
+ | Some (true) => Some (Val vpc)
+ | Some (false) => goto_label_deps fn lbl vpc
+ | None => None
+ end
+ | Ocbz sz lbl, [Val v; Val vpc] => match eval_testzero sz v with
+ | Some (true) => goto_label_deps fn lbl vpc
+ | Some (false) => Some (Val vpc)
+ | None => None
+ end
+ | Otbnz sz n lbl, [Val v; Val vpc] => match eval_testbit sz v n with
+ | Some (true) => goto_label_deps fn lbl vpc
+ | Some (false) => Some (Val vpc)
+ | None => None
+ end
+ | Otbz sz n lbl, [Val v; Val vpc] => match eval_testbit sz v n with
+ | Some (true) => Some (Val vpc)
+ | Some (false) => goto_label_deps fn lbl vpc
+ | None => None
+ end
+ | Obtbl tbl, [Val index; Val vpc] => match index with
+ | Vint n =>
+ match list_nth_z tbl (Int.unsigned n) with
+ | None => None
+ | Some lbl => goto_label_deps fn lbl vpc
+ end
+ | _ => None
+ end
+ | OIncremPC sz, [Val vpc] => Some (Val (Val.offset_ptr vpc (Ptrofs.repr sz)))
+ | OError, _ => None
+ | _, _ => None
+ end.
+
+Definition store_eval (o: store_op) (l: list value) :=
+ match o, l with
+ | Ostore1 st chunk a, [Val vr; Val vs; Memstate m] => exec_store1 st m chunk a vr vs
+ | Ostore2 st chunk a, [Val vr; Val vs1; Val vs2; Memstate m] => exec_store2 st m chunk a vr vs1 vs2
+ | OstoreU st chunk a, [Val vr; Memstate m] => exec_storeU st m chunk a vr
+ | _, _ => None
+ end.
+
+Definition call_ll_loadv (c: memory_chunk) (transf: val -> val) (m: mem) (v: option val) :=
+ match v with
+ | Some va => match Mem.loadv c m va with
+ | Some v' => Some (Val (transf v'))
+ | None => None
+ end
+ | None => None (* should never occurs *)
+ end.
+
+Definition exec_load1 (ld: load_rd_a) (m: mem) (chunk: memory_chunk) (a: addressing) (vl: val) :=
+ let v :=
+ match a with
+ | ADimm _ n => Some (Val.addl vl (Vlong n))
+ | ADadr _ id ofs => Some (Val.addl vl (symbol_low Ge.(_lk) id ofs))
+ | _ => None
+ end in
+ call_ll_loadv chunk (interp_load ld) m v.
+
+Definition exec_load2 (ld: load_rd_a) (m: mem) (chunk: memory_chunk) (a: addressing) (vl1 vl2: val) :=
+ let v :=
+ match a with
+ | ADreg _ _ => Some (Val.addl vl1 vl2)
+ | ADlsl _ _ n => Some (Val.addl vl1 (Val.shll vl2 (Vint n)))
+ | ADsxt _ _ n => Some (Val.addl vl1 (Val.shll (Val.longofint vl2) (Vint n)))
+ | ADuxt _ _ n => Some (Val.addl vl1 (Val.shll (Val.longofintu vl2) (Vint n)))
+ | _ => None
+ end in
+ call_ll_loadv chunk (interp_load ld) m v.
+
+Definition exec_loadU (n: load_rd_a) (m: mem) (chunk: memory_chunk) (a: addressing) :=
+ call_ll_loadv chunk (interp_load n) m None.
+
+Definition load_eval (o: load_op) (l: list value) :=
+ match o, l with
+ | Oload1 ld chunk a, [Val vs; Memstate m] => exec_load1 ld m chunk a vs
+ | Oload2 ld chunk a, [Val vs1; Val vs2; Memstate m] => exec_load2 ld m chunk a vs1 vs2
+ | OloadU st chunk a, [Memstate m] => exec_loadU st m chunk a
+ | _, _ => None
+ end.
+
+Definition eval_allocf (o: allocf_op) (l: list value) :=
+ match o, l with
+ | OAllocf_Mem sz linkofs, [Val spv; Memstate m] =>
+ let (m1, stk) := Mem.alloc m 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ call_ll_storev Mint64 m1 (Some (Val.offset_ptr sp linkofs)) spv
+ | OAllocf_SP sz linkofs, [Val spv; Memstate m] =>
+ let (m1, stk) := Mem.alloc m 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ match call_ll_storev Mint64 m1 (Some (Val.offset_ptr sp linkofs)) spv with
+ | None => None
+ | Some ms => Some (Val sp)
+ end
+ | _, _ => None
+ end.
+
+Definition eval_freef (o: freef_op) (l: list value) :=
+ match o, l with
+ | OFreef_Mem sz linkofs, [Val spv; Memstate m] =>
+ match call_ll_loadv Mint64 (fun v => v) m (Some (Val.offset_ptr spv linkofs)) with
+ | None => None
+ | Some v =>
+ match spv with
+ | Vptr stk ofs =>
+ match Mem.free m stk 0 sz with
+ | None => None
+ | Some m' => Some (Memstate m')
+ end
+ | _ => None
+ end
+ end
+ | OFreef_SP sz linkofs, [Val spv; Memstate m] =>
+ match call_ll_loadv Mint64 (fun v => v) m (Some (Val.offset_ptr spv linkofs)) with
+ | None => None
+ | Some v =>
+ match spv with
+ | Vptr stk ofs =>
+ match Mem.free m stk 0 sz with
+ | None => None
+ | Some m' => Some (v)
+ end
+ | _ => None
+ end
+ end
+ | _, _ => None
+ end.
+
+Definition op_eval (op: op) (l:list value) :=
+ match op, l with
+ | Arith op, l => arith_op_eval op l
+ | Load o, l => load_eval o l
+ | Store o, l => store_eval o l
+ | Allocframe o, l => eval_allocf o l
+ | Freeframe o, l => eval_freef o l
+ | Loadsymbol id, [] => Some (Val (Genv.symbol_address Ge.(_genv) id Ptrofs.zero))
+ | Cvtsw2x, [Val v] => Some (Val (Val.longofint v))
+ | Cvtuw2x, [Val v] => Some (Val (Val.longofintu v))
+ | Cvtx2w, [Val v] => Some (Val (Val.loword v))
+ | Control o, l => control_eval o l
+ | Constant v, [] => Some (Val v)
+ | _, _ => None
+ end.
+
+Definition vz_eq (vz1 vz2: val) : ?? bool :=
+ RET (match vz1 with
+ | Vint i1 => match vz2 with
+ | Vint i2 => Int.eq i1 i2
+ | _ => false
+ end
+ | Vlong l1 => match vz2 with
+ | Vlong l2 => Int64.eq l1 l2
+ | _ => false
+ end
+ | _ => false
+ end).
+
+Lemma vz_eq_correct vz1 vz2:
+ WHEN vz_eq vz1 vz2 ~> b THEN b = true -> vz1 = vz2.
+Proof.
+ wlp_simplify.
+ destruct vz1; destruct vz2; trivial; try discriminate.
+ - eapply f_equal; apply Int.same_if_eq; auto.
+ - eapply f_equal. apply Int64.same_if_eq; auto.
+Qed.
+Hint Resolve vz_eq_correct: wlp.
+
+Definition is_eq (is1 is2: isize) : ?? bool :=
+ RET (match is1 with
+ | W => match is2 with
+ | W => true
+ | _ => false
+ end
+ | X => match is2 with
+ | X => true
+ | _ => false
+ end
+ end).
+
+Lemma is_eq_correct is1 is2:
+ WHEN is_eq is1 is2 ~> b THEN b = true -> is1 = is2.
+Proof.
+ wlp_simplify; destruct is1; destruct is2; trivial; try discriminate.
+Qed.
+Hint Resolve is_eq_correct: wlp.
+
+Definition arith_op_eq (o1 o2: arith_op): ?? bool :=
+ match o1 with
+ | OArithP n1 =>
+ match o2 with OArithP n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithPP n1 =>
+ match o2 with OArithPP n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithPPP n1 =>
+ match o2 with OArithPPP n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithRR0R n1 =>
+ match o2 with OArithRR0R n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithRR0R_XZR n1 vz1 =>
+ match o2 with OArithRR0R_XZR n2 vz2 => iandb (phys_eq n1 n2) (vz_eq vz1 vz2) | _ => RET false end
+ | OArithRR0 n1 =>
+ match o2 with OArithRR0 n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithRR0_XZR n1 vz1 =>
+ match o2 with OArithRR0_XZR n2 vz2 => iandb (phys_eq n1 n2) (vz_eq vz1 vz2) | _ => RET false end
+ | OArithARRRR0 n1 =>
+ match o2 with OArithARRRR0 n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithARRRR0_XZR n1 vz1 =>
+ match o2 with OArithARRRR0_XZR n2 vz2 => iandb (phys_eq n1 n2) (vz_eq vz1 vz2) | _ => RET false end
+ | OArithComparisonPP_CN n1 =>
+ match o2 with OArithComparisonPP_CN n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonPP_CZ n1 =>
+ match o2 with OArithComparisonPP_CZ n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonPP_CC n1 =>
+ match o2 with OArithComparisonPP_CC n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonPP_CV n1 =>
+ match o2 with OArithComparisonPP_CV n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonR0R_CN n1 is1 =>
+ match o2 with OArithComparisonR0R_CN n2 is2 => iandb (phys_eq n1 n2) (is_eq is1 is2) | _ => RET false end
+ | OArithComparisonR0R_CZ n1 is1 =>
+ match o2 with OArithComparisonR0R_CZ n2 is2 => iandb (phys_eq n1 n2) (is_eq is1 is2) | _ => RET false end
+ | OArithComparisonR0R_CC n1 is1 =>
+ match o2 with OArithComparisonR0R_CC n2 is2 => iandb (phys_eq n1 n2) (is_eq is1 is2) | _ => RET false end
+ | OArithComparisonR0R_CV n1 is1 =>
+ match o2 with OArithComparisonR0R_CV n2 is2 => iandb (phys_eq n1 n2) (is_eq is1 is2) | _ => RET false end
+ | OArithComparisonR0R_CN_XZR n1 is1 vz1 =>
+ match o2 with OArithComparisonR0R_CN_XZR n2 is2 vz2 => iandb (vz_eq vz1 vz2) (iandb (phys_eq n1 n2) (is_eq is1 is2)) | _ => RET false end
+ | OArithComparisonR0R_CZ_XZR n1 is1 vz1 =>
+ match o2 with OArithComparisonR0R_CZ_XZR n2 is2 vz2 => iandb (vz_eq vz1 vz2) (iandb (phys_eq n1 n2) (is_eq is1 is2)) | _ => RET false end
+ | OArithComparisonR0R_CC_XZR n1 is1 vz1 =>
+ match o2 with OArithComparisonR0R_CC_XZR n2 is2 vz2 => iandb (vz_eq vz1 vz2) (iandb (phys_eq n1 n2) (is_eq is1 is2)) | _ => RET false end
+ | OArithComparisonR0R_CV_XZR n1 is1 vz1 =>
+ match o2 with OArithComparisonR0R_CV_XZR n2 is2 vz2 => iandb (vz_eq vz1 vz2) (iandb (phys_eq n1 n2) (is_eq is1 is2)) | _ => RET false end
+ | OArithComparisonP_CN n1 =>
+ match o2 with OArithComparisonP_CN n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonP_CZ n1 =>
+ match o2 with OArithComparisonP_CZ n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonP_CC n1 =>
+ match o2 with OArithComparisonP_CC n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithComparisonP_CV n1 =>
+ match o2 with OArithComparisonP_CV n2 => phys_eq n1 n2 | _ => RET false end
+ | Ocset c1 =>
+ match o2 with Ocset c2 => struct_eq c1 c2 | _ => RET false end
+ | Ofmovi fsz1 =>
+ match o2 with Ofmovi fsz2 => phys_eq fsz1 fsz2 | _ => RET false end
+ | Ofmovi_XZR fsz1 =>
+ match o2 with Ofmovi_XZR fsz2 => phys_eq fsz1 fsz2 | _ => RET false end
+ | Ocsel c1 =>
+ match o2 with Ocsel c2 => struct_eq c1 c2 | _ => RET false end
+ | Ofnmul fsz1 =>
+ match o2 with Ofnmul fsz2 => phys_eq fsz1 fsz2 | _ => RET false end
+ end.
+
+Ltac my_wlp_simplify := wlp_xsimplify ltac:(intros; subst; simpl in * |- *; congruence || intuition eauto with wlp).
+
+Lemma arith_op_eq_correct o1 o2:
+ WHEN arith_op_eq o1 o2 ~> b THEN b = true -> o1 = o2.
+Proof.
+ destruct o1, o2; my_wlp_simplify; try congruence;
+ try (destruct vz; destruct vz0); try (destruct is; destruct is0);
+ repeat apply f_equal; try congruence;
+ try apply Int.same_if_eq; try apply Int64.same_if_eq; try auto.
+Qed.
+Hint Resolve arith_op_eq_correct: wlp.
+Opaque arith_op_eq_correct.
+
+Definition control_op_eq (c1 c2: control_op): ?? bool :=
+ match c1 with
+ | Ob lbl1 =>
+ match c2 with Ob lbl2 => phys_eq lbl1 lbl2 | _ => RET false end
+ | Obc c1 lbl1 =>
+ match c2 with Obc c2 lbl2 => iandb (struct_eq c1 c2) (phys_eq lbl1 lbl2) | _ => RET false end
+ | Obl id1 =>
+ match c2 with Obl id2 => phys_eq id1 id2 | _ => RET false end
+ | Obs id1 =>
+ match c2 with Obs id2 => phys_eq id1 id2 | _ => RET false end
+ | Ocbnz sz1 lbl1 =>
+ match c2 with Ocbnz sz2 lbl2 => iandb (phys_eq sz1 sz2) (phys_eq lbl1 lbl2) | _ => RET false end
+ | Ocbz sz1 lbl1 =>
+ match c2 with Ocbz sz2 lbl2 => iandb (phys_eq sz1 sz2) (phys_eq lbl1 lbl2) | _ => RET false end
+ | Otbnz sz1 n1 lbl1 =>
+ match c2 with Otbnz sz2 n2 lbl2 => iandb (RET (Int.eq n1 n2)) (iandb (phys_eq sz1 sz2) (phys_eq lbl1 lbl2)) | _ => RET false end
+ | Otbz sz1 n1 lbl1 =>
+ match c2 with Otbz sz2 n2 lbl2 => iandb (RET (Int.eq n1 n2)) (iandb (phys_eq sz1 sz2) (phys_eq lbl1 lbl2)) | _ => RET false end
+ | Obtbl tbl1 =>
+ match c2 with Obtbl tbl2 => (phys_eq tbl1 tbl2) | _ => RET false end
+ | OIncremPC sz1 =>
+ match c2 with OIncremPC sz2 => RET (Z.eqb sz1 sz2) | _ => RET false end
+ | OError =>
+ match c2 with OError => RET true | _ => RET false end
+ end.
+
+Lemma control_op_eq_correct c1 c2:
+ WHEN control_op_eq c1 c2 ~> b THEN b = true -> c1 = c2.
+Proof.
+ destruct c1, c2; wlp_simplify; try rewrite Z.eqb_eq in * |-; try congruence;
+ try apply Int.same_if_eq in H; try congruence.
+Qed.
+Hint Resolve control_op_eq_correct: wlp.
+Opaque control_op_eq_correct.
+
+Definition store_op_eq (s1 s2: store_op): ?? bool :=
+ match s1 with
+ | Ostore1 st1 chk1 a1 =>
+ match s2 with Ostore1 st2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq st1 st2) (struct_eq a1 a2)) | _ => RET false end
+ | Ostore2 st1 chk1 a1 =>
+ match s2 with Ostore2 st2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq st1 st2) (struct_eq a1 a2)) | _ => RET false end
+ | OstoreU st1 chk1 a1 =>
+ match s2 with OstoreU st2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq st1 st2) (struct_eq a1 a2)) | _ => RET false end
+ end.
+
+Lemma store_op_eq_correct s1 s2:
+ WHEN store_op_eq s1 s2 ~> b THEN b = true -> s1 = s2.
+Proof.
+ destruct s1, s2; wlp_simplify; try congruence.
+ all: rewrite H1 in H0; rewrite H0, H; reflexivity.
+Qed.
+Hint Resolve store_op_eq_correct: wlp.
+Opaque store_op_eq_correct.
+
+Definition load_op_eq (l1 l2: load_op): ?? bool :=
+ match l1 with
+ | Oload1 ld1 chk1 a1 =>
+ match l2 with Oload1 ld2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq ld1 ld2) (struct_eq a1 a2)) | _ => RET false end
+ | Oload2 ld1 chk1 a1 =>
+ match l2 with Oload2 ld2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq ld1 ld2) (struct_eq a1 a2)) | _ => RET false end
+ | OloadU ld1 chk1 a1 =>
+ match l2 with OloadU ld2 chk2 a2 => iandb (struct_eq chk1 chk2) (iandb (struct_eq ld1 ld2) (struct_eq a1 a2)) | _ => RET false end
+ end.
+
+Lemma load_op_eq_correct l1 l2:
+ WHEN load_op_eq l1 l2 ~> b THEN b = true -> l1 = l2.
+Proof.
+ destruct l1, l2; wlp_simplify; try congruence.
+ all: rewrite H1 in H0; rewrite H, H0; reflexivity.
+Qed.
+Hint Resolve load_op_eq_correct: wlp.
+Opaque load_op_eq_correct.
+
+Definition allocf_op_eq (al1 al2: allocf_op): ?? bool :=
+ match al1 with
+ | OAllocf_SP sz1 linkofs1 =>
+ match al2 with OAllocf_SP sz2 linkofs2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq linkofs1 linkofs2) | _ => RET false end
+ | OAllocf_Mem sz1 linkofs1 =>
+ match al2 with OAllocf_Mem sz2 linkofs2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq linkofs1 linkofs2) | _ => RET false end
+ end.
+
+Lemma allocf_op_eq_correct al1 al2:
+ WHEN allocf_op_eq al1 al2 ~> b THEN b = true -> al1 = al2.
+Proof.
+ destruct al1, al2; wlp_simplify; try congruence.
+ all: rewrite H2; rewrite Z.eqb_eq in H; rewrite H; reflexivity.
+Qed.
+Hint Resolve allocf_op_eq_correct: wlp.
+Opaque allocf_op_eq_correct.
+
+Definition freef_op_eq (fr1 fr2: freef_op): ?? bool :=
+ match fr1 with
+ | OFreef_SP sz1 linkofs1 =>
+ match fr2 with OFreef_SP sz2 linkofs2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq linkofs1 linkofs2) | _ => RET false end
+ | OFreef_Mem sz1 linkofs1 =>
+ match fr2 with OFreef_Mem sz2 linkofs2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq linkofs1 linkofs2) | _ => RET false end
+ end.
+
+Lemma freef_op_eq_correct fr1 fr2:
+ WHEN freef_op_eq fr1 fr2 ~> b THEN b = true -> fr1 = fr2.
+Proof.
+ destruct fr1, fr2; wlp_simplify; try congruence.
+ all: rewrite H2; rewrite Z.eqb_eq in H; rewrite H; reflexivity.
+Qed.
+Hint Resolve freef_op_eq_correct: wlp.
+Opaque freef_op_eq_correct.
+
+Definition op_eq (o1 o2: op): ?? bool :=
+ match o1 with
+ | Arith i1 =>
+ match o2 with Arith i2 => arith_op_eq i1 i2 | _ => RET false end
+ | Control i1 =>
+ match o2 with Control i2 => control_op_eq i1 i2 | _ => RET false end
+ | Load i1 =>
+ match o2 with Load i2 => load_op_eq i1 i2 | _ => RET false end
+ | Store i1 =>
+ match o2 with Store i2 => store_op_eq i1 i2 | _ => RET false end
+ | Allocframe i1 =>
+ match o2 with Allocframe i2 => allocf_op_eq i1 i2 | _ => RET false end
+ | Freeframe i1 =>
+ match o2 with Freeframe i2 => freef_op_eq i1 i2 | _ => RET false end
+ | Loadsymbol id1 =>
+ match o2 with Loadsymbol id2 => phys_eq id1 id2 | _ => RET false end
+ | Cvtsw2x =>
+ match o2 with Cvtsw2x => RET true | _ => RET false end
+ | Cvtuw2x =>
+ match o2 with Cvtuw2x => RET true | _ => RET false end
+ | Cvtx2w =>
+ match o2 with Cvtx2w => RET true | _ => RET false end
+ | Constant c1 =>
+ match o2 with Constant c2 => phys_eq c1 c2 | _ => RET false end
+ end.
+
+Lemma op_eq_correct o1 o2:
+ WHEN op_eq o1 o2 ~> b THEN b=true -> o1 = o2.
+Proof.
+ destruct o1, o2; wlp_simplify; congruence.
+Qed.
+
+End IMPPARAM.
+
+End P.
+
+Module L <: ISeqLanguage with Module LP:=P.
+
+Module LP:=P.
+
+Include MkSeqLanguage P.
+
+End L.
+
+Module IST := ImpSimu L ImpPosDict.
+
+Import L.
+Import P.
+
+(** Compilation from [Asmblock] to [L] *)
+
+Local Open Scope positive_scope.
+
+Definition pmem : R.t := 1.
+
+Definition ireg_to_pos (ir: ireg) : R.t :=
+ match ir with
+ | X0 => 8 | X1 => 9 | X2 => 10 | X3 => 11 | X4 => 12 | X5 => 13 | X6 => 14 | X7 => 15
+ | X8 => 16 | X9 => 17 | X10 => 18 | X11 => 19 | X12 => 20 | X13 => 21 | X14 => 22 | X15 => 23
+ | X16 => 24 | X17 => 25 | X18 => 26 | X19 => 27 | X20 => 28 | X21 => 29 | X22 => 30 | X23 => 31
+ | X24 => 32 | X25 => 33 | X26 => 34 | X27 => 35 | X28 => 36 | X29 => 37 | X30 => 38
+ end
+.
+
+Definition freg_to_pos (fr: freg) : R.t :=
+ match fr with
+ | D0 => 39 | D1 => 40 | D2 => 41 | D3 => 42 | D4 => 43 | D5 => 44 | D6 => 45 | D7 => 46
+ | D8 => 47 | D9 => 48 | D10 => 49 | D11 => 50 | D12 => 51 | D13 => 52 | D14 => 53 | D15 => 54
+ | D16 => 55 | D17 => 56 | D18 => 57 | D19 => 58 | D20 => 59 | D21 => 60 | D22 => 61 | D23 => 62
+ | D24 => 63 | D25 => 64 | D26 => 65 | D27 => 66 | D28 => 67 | D29 => 68 | D30 => 69 | D31 => 70
+ end
+.
+
+Lemma ireg_to_pos_discr: forall r r', r <> r' -> ireg_to_pos r <> ireg_to_pos r'.
+Proof.
+ destruct r; destruct r'; try contradiction; discriminate.
+Qed.
+
+Lemma freg_to_pos_discr: forall r r', r <> r' -> freg_to_pos r <> freg_to_pos r'.
+Proof.
+ destruct r; destruct r'; try contradiction; discriminate.
+Qed.
+
+Definition ppos (r: preg) : R.t :=
+ match r with
+ | CR c => match c with
+ | CN => 2
+ | CZ => 3
+ | CC => 4
+ | CV => 5
+ end
+ | PC => 6
+ | DR d => match d with
+ | IR i => match i with
+ | XSP => 7
+ | RR1 ir => ireg_to_pos ir
+ end
+ | FR fr => freg_to_pos fr
+ end
+ end
+.
+
+Notation "# r" := (ppos r) (at level 100, right associativity).
+
+Lemma not_eq_add:
+ forall k n n', n <> n' -> k + n <> k + n'.
+Proof.
+ intros k n n' H1 H2. apply H1; clear H1. eapply Pos.add_reg_l; eauto.
+Qed.
+
+Lemma ppos_equal: forall r r', r = r' <-> ppos r = ppos r'.
+Proof.
+ destruct r as [dr|cr|]; destruct r' as [dr'|cr'|];
+ try destruct dr as [ir|fr]; try destruct dr' as [ir'|fr'];
+ try destruct ir as [irr|]; try destruct ir' as [irr'|].
+ all: split; intros; try rewrite H; try discriminate; try contradiction; simpl; eauto;
+ try destruct irr; try destruct irr';
+ try destruct fr; try destruct fr';
+ try destruct cr; try destruct cr';
+ simpl; try discriminate; try reflexivity.
+Qed.
+
+Lemma ppos_discr: forall r r', r <> r' <-> ppos r <> ppos r'.
+Proof.
+ split; unfold not; try intros; try apply ppos_equal in H0; try discriminate; try contradiction.
+Qed.
+
+Lemma ppos_pmem_discr: forall r, pmem <> ppos r.
+Proof.
+ intros. destruct r as [dr|cr|].
+ - destruct dr as [ir|fr]; try destruct ir as [irr|]; try destruct irr; try destruct fr;
+ unfold ppos; unfold pmem; discriminate.
+ - unfold ppos; unfold pmem; destruct cr; discriminate.
+ - unfold ppos; unfold pmem; discriminate.
+Qed.
+
+(** Inversion functions, used for debug traces *)
+
+Definition pos_to_ireg (p: R.t) : option ireg :=
+ match p with
+ | 8 => Some (X0) | 9 => Some (X1) | 10 => Some (X2) | 11 => Some (X3) | 12 => Some (X4) | 13 => Some (X5) | 14 => Some (X6) | 15 => Some (X7)
+ | 16 => Some (X8) | 17 => Some (X9) | 18 => Some (X10) | 19 => Some (X11) | 20 => Some (X12) | 21 => Some (X13) | 22 => Some (X14) | 23 => Some (X15)
+ | 24 => Some (X16) | 25 => Some (X17) | 26 => Some (X18) | 27 => Some (X19) | 28 => Some (X20) | 29 => Some (X21) | 30 => Some (X22) | 31 => Some (X23)
+ | 32 => Some (X24) | 33 => Some (X25) | 34 => Some (X26) | 35 => Some (X27) | 36 => Some (X28) | 37 => Some (X29) | 38 => Some (X30) | _ => None
+ end.
+
+Definition pos_to_freg (p: R.t) : option freg :=
+ match p with
+ | 39 => Some(D0) | 40 => Some(D1) | 41 => Some(D2) | 42 => Some(D3) | 43 => Some(D4) | 44 => Some(D5) | 45 => Some(D6) | 46 => Some(D7)
+ | 47 => Some(D8) | 48 => Some(D9) | 49 => Some(D10) | 50 => Some(D11) | 51 => Some(D12) | 52 => Some(D13) | 53 => Some(D14) | 54 => Some(D15)
+ | 55 => Some(D16) | 56 => Some(D17) | 57 => Some(D18) | 58 => Some(D19) | 59 => Some(D20) | 60 => Some(D21) | 61 => Some(D22) | 62 => Some(D23)
+ | 63 => Some(D24) | 64 => Some(D25) | 65 => Some(D26) | 66 => Some(D27) | 67 => Some(D28) | 68 => Some(D29) | 69 => Some(D30) | 70 => Some(D31) | _ => None
+ end.
+
+Definition inv_ppos (p: R.t) : option preg :=
+ match p with
+ | 1 => None
+ | 2 => Some (CR CN)
+ | 3 => Some (CR CZ)
+ | 4 => Some (CR CC)
+ | 5 => Some (CR CV)
+ | 6 => Some (PC)
+ | 7 => Some (DR (IR XSP))
+ | n => match pos_to_ireg n with
+ | None => match pos_to_freg n with
+ | None => None
+ | Some fr => Some (DR (FR fr))
+ end
+ | Some ir => Some (DR (IR ir))
+ end
+ end.
+
+Notation "a @ b" := (Econs a b) (at level 102, right associativity).
+
+(** Translations of instructions *)
+
+Definition get_testcond_rlocs (c: testcond) :=
+ match c with
+ | TCeq => (PReg(#CZ) @ Enil)
+ | TCne => (PReg(#CZ) @ Enil)
+ | TChs => (PReg(#CC) @ Enil)
+ | TClo => (PReg(#CC) @ Enil)
+ | TCmi => (PReg(#CN) @ Enil)
+ | TCpl => (PReg(#CN) @ Enil)
+ | TChi => (PReg(#CZ) @ PReg(#CC) @ Enil)
+ | TCls => (PReg(#CZ) @ PReg(#CC) @ Enil)
+ | TCge => (PReg(#CN) @ PReg(#CV) @ Enil)
+ | TClt => (PReg(#CN) @ PReg(#CV) @ Enil)
+ | TCgt => (PReg(#CN) @ PReg(#CZ) @ PReg(#CV) @ Enil)
+ | TCle => (PReg(#CN) @ PReg(#CZ) @ PReg(#CV) @ Enil)
+ end.
+
+Definition trans_control (ctl: control) : inst :=
+ match ctl with
+ | Pb lbl => [(#PC, Op (Control (Ob lbl)) (PReg(#PC) @ Enil))]
+ | Pbc c lbl =>
+ let lr := get_testcond_rlocs c in
+ [(#PC, Op (Control (Obc c lbl)) (PReg(#PC) @ lr))]
+ | Pbl id sg => [(#RA, PReg(#PC));
+ (#PC, Op (Control (Obl id)) Enil)]
+ | Pbs id sg => [(#PC, Op (Control (Obs id)) Enil)]
+ | Pblr r sg => [(#RA, PReg(#PC));
+ (#PC, Old (PReg(#r)))]
+ | Pbr r sg => [(#PC, PReg(#r))]
+ | Pret r => [(#PC, PReg(#r))]
+ | Pcbnz sz r lbl => [(#PC, Op (Control (Ocbnz sz lbl)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Pcbz sz r lbl => [(#PC, Op (Control (Ocbz sz lbl)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Ptbnz sz r n lbl => [(#PC, Op (Control (Otbnz sz n lbl)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Ptbz sz r n lbl => [(#PC, Op (Control (Otbz sz n lbl)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Pbtbl r tbl => [(#X16, Op (Constant Vundef) Enil);
+ (#PC, Op (Control (Obtbl tbl)) (PReg(#r) @ PReg(#PC) @ Enil));
+ (#X16, Op (Constant Vundef) Enil)]
+ | Pbuiltin ef args res => []
+ end.
+
+Definition trans_exit (ex: option control) : L.inst :=
+ match ex with
+ | None => []
+ | Some ctl => trans_control ctl
+ end
+.
+
+Definition trans_arith (ai: ar_instruction) : inst :=
+ match ai with
+ | PArithP n rd =>
+ if destroy_X16 n then [(#rd, Op(Arith (OArithP n)) Enil); (#X16, Op (Constant Vundef) Enil)]
+ else [(#rd, Op(Arith (OArithP n)) Enil)]
+ | PArithPP n rd r1 => [(#rd, Op(Arith (OArithPP n)) (PReg(#r1) @ Enil))]
+ | PArithPPP n rd r1 r2 => [(#rd, Op(Arith (OArithPPP n)) (PReg(#r1) @ PReg(#r2) @ Enil))]
+ | PArithRR0R n rd r1 r2 =>
+ let lr := match r1 with
+ | RR0 r1' => Op(Arith (OArithRR0R n)) (PReg(#r1') @ PReg(#r2) @ Enil)
+ | XZR => let vz := if arith_rr0r_isize n then Vint Int.zero else Vlong Int64.zero in
+ Op(Arith (OArithRR0R_XZR n vz)) (PReg(#r2) @ Enil)
+ end in
+ [(#rd, lr)]
+ | PArithRR0 n rd r1 =>
+ let lr := match r1 with
+ | RR0 r1' => Op(Arith (OArithRR0 n)) (PReg(#r1') @ Enil)
+ | XZR => let vz := if arith_rr0_isize n then Vint Int.zero else Vlong Int64.zero in
+ Op(Arith (OArithRR0_XZR n vz)) (Enil)
+ end in
+ [(#rd, lr)]
+ | PArithARRRR0 n rd r1 r2 r3 =>
+ let lr := match r3 with
+ | RR0 r3' => Op(Arith (OArithARRRR0 n)) (PReg(#r1) @ PReg (#r2) @ PReg(#r3') @ Enil)
+ | XZR => let vz := if arith_arrrr0_isize n then Vint Int.zero else Vlong Int64.zero in
+ Op(Arith (OArithARRRR0_XZR n vz)) (PReg(#r1) @ PReg(#r2) @ Enil)
+ end in
+ [(#rd, lr)]
+ | PArithComparisonPP n r1 r2 =>
+ [(#CN, Op(Arith (OArithComparisonPP_CN n)) (PReg(#r1) @ PReg(#r2) @ Enil));
+ (#CZ, Op(Arith (OArithComparisonPP_CZ n)) (PReg(#r1) @ PReg(#r2) @ Enil));
+ (#CC, Op(Arith (OArithComparisonPP_CC n)) (PReg(#r1) @ PReg(#r2) @ Enil));
+ (#CV, Op(Arith (OArithComparisonPP_CV n)) (PReg(#r1) @ PReg(#r2) @ Enil))]
+ | PArithComparisonR0R n r1 r2 =>
+ let is := arith_comparison_r0r_isize n in
+ match r1 with
+ | RR0 r1' => [(#CN, Op(Arith (OArithComparisonR0R_CN n is)) (PReg(#r1') @ PReg(#r2) @ Enil));
+ (#CZ, Op(Arith (OArithComparisonR0R_CZ n is)) (PReg(#r1') @ PReg(#r2) @ Enil));
+ (#CC, Op(Arith (OArithComparisonR0R_CC n is)) (PReg(#r1') @ PReg(#r2) @ Enil));
+ (#CV, Op(Arith (OArithComparisonR0R_CV n is)) (PReg(#r1') @ PReg(#r2) @ Enil))]
+ | XZR => let vz := if is then Vint Int.zero else Vlong Int64.zero in
+ [(#CN, Op(Arith (OArithComparisonR0R_CN_XZR n is vz)) (PReg(#r2) @ Enil));
+ (#CZ, Op(Arith (OArithComparisonR0R_CZ_XZR n is vz)) (PReg(#r2) @ Enil));
+ (#CC, Op(Arith (OArithComparisonR0R_CC_XZR n is vz)) (PReg(#r2) @ Enil));
+ (#CV, Op(Arith (OArithComparisonR0R_CV_XZR n is vz)) (PReg(#r2) @ Enil))]
+ end
+ | PArithComparisonP n r1 =>
+ [(#CN, Op(Arith (OArithComparisonP_CN n)) (PReg(#r1) @ Enil));
+ (#CZ, Op(Arith (OArithComparisonP_CZ n)) (PReg(#r1) @ Enil));
+ (#CC, Op(Arith (OArithComparisonP_CC n)) (PReg(#r1) @ Enil));
+ (#CV, Op(Arith (OArithComparisonP_CV n)) (PReg(#r1) @ Enil))]
+ | Pcset rd c =>
+ let lr := get_testcond_rlocs c in
+ [(#rd, Op(Arith (Ocset c)) lr)]
+ | Pfmovi fsz rd r1 =>
+ let lr := match r1 with
+ | RR0 r1' => Op(Arith (Ofmovi fsz)) (PReg(#r1') @ Enil)
+ | XZR => Op(Arith (Ofmovi_XZR fsz)) Enil
+ end in
+ [(#rd, lr)]
+ | Pcsel rd r1 r2 c =>
+ let lr := get_testcond_rlocs c in
+ [(#rd, Op(Arith (Ocsel c)) (PReg(#r1) @ PReg (#r2) @ lr))]
+ | Pfnmul fsz rd r1 r2 => [(#rd, Op(Arith (Ofnmul fsz)) (PReg(#r1) @ PReg(#r2) @ Enil))]
+ end.
+
+Definition eval_addressing_rlocs_st (st: store_rs_a) (chunk: memory_chunk) (rs: dreg) (a: addressing) :=
+ match a with
+ | ADimm base n => Op (Store (Ostore1 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg (pmem) @ Enil)
+ | ADreg base r => Op (Store (Ostore2 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADlsl base r n => Op (Store (Ostore2 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADsxt base r n => Op (Store (Ostore2 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADuxt base r n => Op (Store (Ostore2 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADadr base id ofs => Op (Store (Ostore1 st chunk a)) (PReg (#rs) @ PReg (#base) @ PReg (pmem) @ Enil)
+ | ADpostincr base n => Op (Store (OstoreU st chunk a)) (PReg (#rs) @ PReg (pmem) @ Enil) (* not modeled yet *)
+ end.
+
+Definition eval_addressing_rlocs_ld (ld: load_rd_a) (chunk: memory_chunk) (a: addressing) :=
+ match a with
+ | ADimm base n => Op (Load (Oload1 ld chunk a)) (PReg (#base) @ PReg (pmem) @ Enil)
+ | ADreg base r => Op (Load (Oload2 ld chunk a)) (PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADlsl base r n => Op (Load (Oload2 ld chunk a)) (PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADsxt base r n => Op (Load (Oload2 ld chunk a)) (PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADuxt base r n => Op (Load (Oload2 ld chunk a)) (PReg (#base) @ PReg(#r) @ PReg (pmem) @ Enil)
+ | ADadr base id ofs => Op (Load (Oload1 ld chunk a)) (PReg (#base) @ PReg (pmem) @ Enil)
+ | ADpostincr base n => Op (Load (Oload1 ld chunk a)) (PReg (#base) @ PReg (pmem) @ Enil)
+ end.
+
+Definition trans_ldp_chunk (chunk: memory_chunk) (r: dreg): load_rd_a :=
+ match chunk with
+ | Mint32 => Pldrw
+ | Mint64 => Pldrx
+ | Mfloat32 => Pldrs
+ | Mfloat64 => Pldrd
+ | Many32 => Pldrw_a
+ | _ => (* This case should always correspond to Many64 *)
+ match r with
+ | IR _ => Pldrx_a
+ | FR _ => Pldrd_a
+ end
+ end.
+
+Definition trans_stp_chunk (chunk: memory_chunk) (r: dreg): store_rs_a :=
+ match chunk with
+ | Mint32 => Pstrw
+ | Mint64 => Pstrx
+ | Mfloat32 => Pstrs
+ | Mfloat64 => Pstrd
+ | Many32 => Pstrw_a
+ | _ => (* This case should always correspond to Many64 *)
+ match r with
+ | IR _ => Pstrx_a
+ | FR _ => Pstrd_a
+ end
+ end.
+
+Definition trans_load (ldi: ld_instruction) :=
+ match ldi with
+ | PLd_rd_a ld r a =>
+ let lr := eval_addressing_rlocs_ld ld (chunk_load ld) a in [(#r, lr)]
+ | Pldp ld r1 r2 chk1 chk2 a =>
+ let ldi1 := trans_ldp_chunk chk1 r1 in
+ let ldi2 := trans_ldp_chunk chk2 r1 in
+ let lr := eval_addressing_rlocs_ld ldi1 chk1 a in
+ let ofs := match chk1 with | Mint32 | Mfloat32 | Many32 => 4%Z | _ => 8%Z end in
+ match a with
+ | ADimm base n =>
+ let a' := (get_offset_addr a ofs) in
+ [(#r1, lr);
+ (#r2, Op (Load (Oload1 ldi2 chk2 a'))
+ (Old(PReg (#base)) @ PReg (pmem) @ Enil))]
+ | _ => [(#PC, (Op (OError)) Enil)]
+ end
+ end.
+
+Definition trans_store (sti: st_instruction) :=
+ match sti with
+ | PSt_rs_a st r a =>
+ let lr := eval_addressing_rlocs_st st (chunk_store st) r a in [(pmem, lr)]
+ | Pstp st r1 r2 chk1 chk2 a =>
+ let sti1 := trans_stp_chunk chk1 r1 in
+ let sti2 := trans_stp_chunk chk2 r1 in
+ let lr := eval_addressing_rlocs_st sti1 chk1 r1 a in
+ let ofs := match chk1 with | Mint32 | Mfloat32| Many32 => 4%Z | _ => 8%Z end in
+ match a with
+ | ADimm base n =>
+ let a' := (get_offset_addr a ofs) in
+ [(pmem, lr);
+ (pmem, Op (Store (Ostore1 sti2 chk2 a'))
+ (PReg (#r2) @ Old(PReg (#base)) @ PReg (pmem) @ Enil))]
+ | _ => [(#PC, (Op (OError)) Enil)]
+ end
+ end.
+
+Definition trans_basic (b: basic) : inst :=
+ match b with
+ | PArith ai => trans_arith ai
+ | PLoad ld => trans_load ld
+ | PStore st => trans_store st
+ | Pallocframe sz linkofs =>
+ [(#X29, PReg(#SP));
+ (#SP, Op (Allocframe (OAllocf_SP sz linkofs)) (PReg (#SP) @ PReg pmem @ Enil));
+ (#X16, Op (Constant Vundef) Enil);
+ (pmem, Op (Allocframe (OAllocf_Mem sz linkofs)) (Old(PReg(#SP)) @ PReg pmem @ Enil))]
+ | Pfreeframe sz linkofs =>
+ [(pmem, Op (Freeframe (OFreef_Mem sz linkofs)) (PReg (#SP) @ PReg pmem @ Enil));
+ (#SP, Op (Freeframe (OFreef_SP sz linkofs)) (PReg (#SP) @ Old (PReg pmem) @ Enil));
+ (#X16, Op (Constant Vundef) Enil)]
+ | Ploadsymbol rd id => [(#rd, Op (Loadsymbol id) Enil)]
+ | Pcvtsw2x rd r1 => [(#rd, Op (Cvtsw2x) (PReg (#r1) @ Enil))]
+ | Pcvtuw2x rd r1 => [(#rd, Op (Cvtuw2x) (PReg (#r1) @ Enil))]
+ | Pcvtx2w rd => [(#rd, Op (Cvtx2w) (PReg (#rd) @ Enil))]
+ | Pnop => []
+ end.
+
+Fixpoint trans_body (b: list basic) : list L.inst :=
+ match b with
+ | nil => nil
+ | b :: lb => (trans_basic b) :: (trans_body lb)
+ end.
+
+Definition trans_pcincr (sz: Z) (k: L.inst) := (#PC, Op (Control (OIncremPC sz)) (PReg(#PC) @ Enil)) :: k.
+
+Definition trans_block (b: Asmblock.bblock) : L.bblock :=
+ trans_body (body b) ++ (trans_pcincr (size b) (trans_exit (exit b)) :: nil).
+
+(*Theorem trans_block_noheader_inv: forall bb, trans_block (no_header bb) = trans_block bb.*)
+(*Proof.*)
+ (*intros. destruct bb as [hd bdy ex COR]; unfold no_header; simpl. unfold trans_block. simpl. reflexivity.*)
+(*Qed.*)
+
+(*Theorem trans_block_header_inv: forall bb hd, trans_block (stick_header hd bb) = trans_block bb.*)
+(*Proof.*)
+ (*intros. destruct bb as [hdr bdy ex COR]; unfold no_header; simpl. unfold trans_block. simpl. reflexivity.*)
+(*Qed.*)
+
+(** Lemmas on the translation *)
+
+Definition state := L.mem.
+Definition exec := L.run.
+
+Definition match_states (s: Asm.state) (s': state) :=
+ let (rs, m) := s in
+ s' pmem = Memstate m
+ /\ forall r, s' (#r) = Val (rs r).
+
+Definition match_outcome (o:outcome) (s: option state) :=
+ match o with
+ | Some n => exists s', s=Some s' /\ match_states n s'
+ | None => s=None
+ end.
+
+Notation "a <[ b <- c ]>" := (assign a b c) (at level 102, right associativity).
+
+Definition trans_state (s: Asm.state) : state :=
+ let (rs, m) := s in
+ fun x => if (Pos.eq_dec x pmem) then Memstate m
+ else match (inv_ppos x) with
+ | Some r => Val (rs r)
+ | None => Val Vundef
+ end.
+
+Lemma not_eq_IR:
+ forall r r', r <> r' -> IR r <> IR r'.
+Proof.
+ intros. congruence.
+Qed.
+
+Lemma ireg_pos_ppos: forall r,
+ ireg_to_pos r = # r.
+Proof.
+ intros. simpl. reflexivity.
+Qed.
+
+Lemma freg_pos_ppos: forall r,
+ freg_to_pos r = # r.
+Proof.
+ intros. simpl. reflexivity.
+Qed.
+
+Lemma ireg_not_pc: forall r,
+ (#PC) <> ireg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma ireg_not_pmem: forall r,
+ ireg_to_pos r <> pmem.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma ireg_not_CN: forall r,
+ 2 <> ireg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma ireg_not_CZ: forall r,
+ 3 <> ireg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma ireg_not_CC: forall r,
+ 4 <> ireg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma ireg_not_CV: forall r,
+ 5 <> ireg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma freg_not_pmem: forall r,
+ freg_to_pos r <> pmem.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma freg_not_CN: forall r,
+ 2 <> freg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma freg_not_CZ: forall r,
+ 3 <> freg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma freg_not_CC: forall r,
+ 4 <> freg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma freg_not_CV: forall r,
+ 5 <> freg_to_pos r.
+Proof.
+ intros; destruct r; discriminate.
+Qed.
+
+Lemma dreg_not_pmem: forall (r: dreg),
+ (# r) <> pmem.
+Proof.
+ intros; destruct r as [i|f].
+ - destruct i. apply ireg_not_pmem. discriminate.
+ - apply freg_not_pmem.
+Qed.
+
+Ltac DPRM pr :=
+ destruct pr as [drDPRF|crDPRF|];
+ [destruct drDPRF as [irDPRF|frDPRF]; [destruct irDPRF |]
+ | destruct crDPRF|].
+
+Ltac DPRF pr :=
+ destruct pr as [drDPRF|crDPRF|];
+ [destruct drDPRF as [irDPRF|frDPRF]; [destruct irDPRF as [irrDPRF|]; [destruct irrDPRF|]
+ | destruct frDPRF]
+ | destruct crDPRF|].
+
+Lemma preg_not_pmem: forall r,
+ pmem <> # r.
+Proof.
+ intros. DPRF r; simpl; discriminate.
+Qed.
+
+Ltac DIRN1 ir := destruct ir as [irrDIRN1|]; subst; try destruct irrDIRN1; simpl.
+
+Lemma dreg_not_CN: forall (r: dreg),
+ 2 <> (#r).
+Proof.
+ intros; DIRN1 r; [ apply ireg_not_CN | discriminate | apply freg_not_CN].
+Qed.
+
+Lemma dreg_not_CZ: forall (r: dreg),
+ 3 <> (#r).
+Proof.
+ intros; DIRN1 r; [ apply ireg_not_CZ | discriminate | apply freg_not_CZ].
+Qed.
+
+Lemma dreg_not_CC: forall (r: dreg),
+ 4 <> (#r).
+Proof.
+ intros; DIRN1 r; [ apply ireg_not_CC | discriminate | apply freg_not_CC].
+Qed.
+
+Lemma dreg_not_CV: forall (r: dreg),
+ 5 <> (#r).
+Proof.
+ intros; DIRN1 r; [ apply ireg_not_CV | discriminate | apply freg_not_CV].
+Qed.
+
+Lemma sr_update_both: forall sr rsr r1 rr v
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ (sr <[ (#r1) <- Val (v) ]>) (#rr) =
+ Val (rsr # r1 <- v rr).
+Proof.
+ intros. unfold assign.
+ destruct (R.eq_dec (#r1) (#rr)); subst.
+ - apply ppos_equal in e; subst; rewrite Pregmap.gss; reflexivity.
+ - rewrite Pregmap.gso; eapply ppos_discr in n; auto.
+Qed.
+
+Lemma sr_gss: forall sr pos v,
+ (sr <[ pos <- v ]>) pos = v.
+Proof.
+ intros. unfold assign.
+ destruct (R.eq_dec pos pos) eqn:REQ; try reflexivity; try congruence.
+Qed.
+
+Lemma sr_update_overwrite: forall sr pos v1 v2,
+ (sr <[ pos <- v1 ]>) <[ pos <- v2 ]> = (sr <[ pos <- v2 ]>).
+Proof.
+ intros.
+ unfold assign. apply functional_extensionality; intros x.
+ destruct (R.eq_dec pos x); reflexivity.
+Qed.
+
+Ltac sr_val_rwrt :=
+ repeat match goal with
+ | [H: forall r: preg, ?sr (# r) = Val (?rsr r) |- _ ]
+ => rewrite H
+ end.
+
+Ltac sr_memstate_rwrt :=
+ repeat match goal with
+ | [H: ?sr pmem = Memstate ?mr |- _ ]
+ => rewrite <- H
+ end.
+
+Ltac replace_ppos :=
+ try erewrite !ireg_pos_ppos;
+ try erewrite !freg_pos_ppos;
+ try replace (7) with (#XSP) by eauto;
+ try replace (24) with (#X16) by auto.
+
+Ltac DDRM dr :=
+ destruct dr as [irsDDRF|frDDRF];
+ [destruct irsDDRF
+ | idtac ].
+
+(* Ltac DDRF dr :=
+ destruct dr as [irsDDRF|frDDRF];
+ [destruct irsDDRF as [irsDDRF|]; [destruct irsDDRF|]
+ | destruct frDDRF]. *)
+
+(* Ltac DPRI pr :=
+ destruct pr as [drDPRI|crDPRI|];
+ [destruct drDPRI as [irDPRI|frDPRI]; [destruct irDPRI as [irrDPRI|]; [destruct irrDPRI|]|]
+ | idtac
+ | idtac ]. *)
+
+Ltac discriminate_ppos :=
+ try apply ireg_not_pmem;
+ try apply ireg_not_pc;
+ try apply freg_not_pmem;
+ try apply dreg_not_pmem;
+ try apply ireg_not_CN;
+ try apply ireg_not_CZ;
+ try apply ireg_not_CC;
+ try apply ireg_not_CV;
+ try apply freg_not_CN;
+ try apply freg_not_CZ;
+ try apply freg_not_CC;
+ try apply freg_not_CV;
+ try apply dreg_not_CN;
+ try apply dreg_not_CZ;
+ try apply dreg_not_CC;
+ try apply dreg_not_CV;
+ try(simpl; discriminate).
+
+Ltac replace_pc := try replace (6) with (#PC) by eauto.
+
+Ltac replace_regs_pos sr :=
+ try replace (sr 7) with (sr (ppos XSP)) by eauto;
+ try replace (sr 6) with (sr (ppos PC)) by eauto;
+ try replace (sr 2) with (sr (ppos CN)) by eauto;
+ try replace (sr 3) with (sr (ppos CZ)) by eauto;
+ try replace (sr 4) with (sr (ppos CC)) by eauto;
+ try replace (sr 5) with (sr (ppos CV)) by eauto.
+
+Ltac Simpl_exists sr :=
+ replace_ppos;
+ replace_regs_pos sr;
+ try sr_val_rwrt;
+ try (eexists; split; [| split]); eauto;
+ try (sr_memstate_rwrt; rewrite assign_diff;
+ try reflexivity;
+ discriminate_ppos
+ ).
+
+Ltac Simpl_rep sr :=
+ replace_ppos;
+ replace_regs_pos sr;
+ try sr_val_rwrt;
+ try (sr_memstate_rwrt; rewrite assign_diff;
+ try reflexivity;
+ discriminate_ppos
+ ).
+
+Ltac Simpl_update :=
+ try eapply sr_update_both; eauto.
+
+Ltac Simpl sr := Simpl_exists sr; try (intros rr(* ; try rewrite sr_update_overwrite; replace_regs_pos sr; DPRM rr *)); Simpl_update.
+
+Ltac destruct_res_flag rsr := try (rewrite Pregmap.gso; discriminate_ppos); destruct (rsr _); simpl; try reflexivity.
+
+(* Ltac discriminate_preg_flags := rewrite !assign_diff; try rewrite !Pregmap.gso; discriminate_ppos; sr_val_rwrt; reflexivity. *)
+
+(* Ltac destruct_reg_neq r1 r2 :=
+ destruct (PregEq.eq r1 r2); subst;
+ [ rewrite sr_gss; rewrite Pregmap.gss; reflexivity |
+ rewrite assign_diff; try rewrite Pregmap.gso; fold (ppos r1); try apply ppos_discr; auto]. *)
+
+Lemma reg_update_overwrite: forall rsr sr r rd v1 v2
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ ((sr <[ # rd <- Val (v1) ]>) <[ # rd <- Val (v2) ]>) (# r) =
+ Val ((rsr # rd <- v1) # rd <- v2 r).
+Proof.
+ intros.
+ unfold Pregmap.set; destruct (PregEq.eq r rd).
+ - rewrite e; apply sr_gss; reflexivity.
+ - rewrite sr_update_overwrite. rewrite assign_diff; eauto.
+ unfold not; intros. apply ppos_equal in H. congruence.
+Qed.
+
+Ltac replace_regs_cond_force :=
+ try replace (5) with (#CV) in * by auto;
+ try replace (4) with (#CC) in * by auto;
+ try replace (3) with (#CZ) in * by auto;
+ try replace (2) with (#CN) in * by auto.
+
+Ltac validate_crbit_flags rr v1 v2 :=
+ destruct (R.eq_dec 5 (#rr)) as [e0|n0];
+ destruct (R.eq_dec 4 (#rr)) as [e1|n1];
+ destruct (R.eq_dec 3 (#rr)) as [e2|n2];
+ destruct (R.eq_dec 2 (#rr)) as [e3|n3];
+ replace_regs_cond_force;
+ try apply ppos_equal in e0;
+ try apply ppos_equal in e1;
+ try apply ppos_equal in e2;
+ try apply ppos_equal in e3;
+ try apply ppos_discr in n0;
+ try apply ppos_discr in n1;
+ try apply ppos_discr in n2;
+ try apply ppos_discr in n3;
+ subst.
+
+Ltac Simpl_flags :=
+ try (rewrite Pregmap.gss; reflexivity);
+ try (rewrite Pregmap.gso, Pregmap.gss; [reflexivity|]; try auto);
+ try (rewrite Pregmap.gso; try auto);
+ try (rewrite !Pregmap.gso; try auto).
+
+Lemma compare_single_res: forall sr mr rsr rr v1 v2
+ (HMEM: sr pmem = Memstate mr)
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ ((((sr <[ 2 <- Val (_CN (v_compare_single v1 v2)) ]>) <[ 3 <-
+ Val (_CZ (v_compare_single v1 v2)) ]>) <[ 4 <-
+ Val (_CC (v_compare_single v1 v2)) ]>) <[ 5 <-
+ Val (_CV (v_compare_single v1 v2)) ]>) (# rr) =
+Val
+ ((compare_single rsr v1 v2) rr).
+Proof.
+ intros. unfold v_compare_single, compare_single, assign.
+ validate_crbit_flags rr v1 v2.
+ all: destruct v1; destruct v2; Simpl_flags.
+Qed.
+
+Lemma compare_float_res: forall sr mr rsr rr v1 v2
+ (HMEM: sr pmem = Memstate mr)
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ ((((sr <[ 2 <- Val (_CN (v_compare_float v1 v2)) ]>) <[ 3 <-
+ Val (_CZ (v_compare_float v1 v2)) ]>) <[ 4 <-
+ Val (_CC (v_compare_float v1 v2)) ]>) <[ 5 <-
+ Val (_CV (v_compare_float v1 v2)) ]>) (# rr) =
+Val
+ ((compare_float rsr v1 v2) rr).
+Proof.
+ intros. unfold v_compare_float, compare_float, assign.
+ validate_crbit_flags rr v1 v2.
+ all: destruct v1; destruct v2; Simpl_flags.
+Qed.
+
+Lemma compare_long_res: forall sr mr rsr rr v1 v2
+ (HMEM: sr pmem = Memstate mr)
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ ((((sr <[ 2 <- Val (_CN (v_compare_long v1 v2)) ]>) <[ 3 <-
+ Val (_CZ (v_compare_long v1 v2)) ]>) <[ 4 <-
+ Val (_CC (v_compare_long v1 v2)) ]>) <[ 5 <-
+ Val (_CV (v_compare_long v1 v2)) ]>) (# rr) =
+Val
+ ((compare_long rsr v1 v2) rr).
+Proof.
+ intros. unfold v_compare_long, compare_long, assign.
+ validate_crbit_flags rr v1 v2.
+ all: Simpl_flags.
+Qed.
+
+Lemma compare_int_res: forall sr mr rsr rr v1 v2
+ (HMEM: sr pmem = Memstate mr)
+ (HEQV: forall r : preg, sr (# r) = Val (rsr r)),
+ ((((sr <[ 2 <- Val (_CN (v_compare_int v1 v2)) ]>) <[ 3 <-
+ Val (_CZ (v_compare_int v1 v2)) ]>) <[ 4 <-
+ Val (_CC (v_compare_int v1 v2)) ]>) <[ 5 <-
+ Val (_CV (v_compare_int v1 v2)) ]>) (# rr) =
+Val
+ ((compare_int rsr v1 v2) rr).
+Proof.
+ intros. unfold v_compare_int, compare_int, assign.
+ validate_crbit_flags rr v1 v2.
+ all: Simpl_flags.
+Qed.
+
+Section SECT_SEQ.
+
+Variable Ge: genv.
+
+Lemma trans_arith_correct rsr mr sr rsw' old i:
+ match_states (State rsr mr) sr ->
+ exec_arith_instr Ge.(_lk) i rsr = rsw' ->
+ exists sw,
+ inst_run Ge (trans_arith i) sr old = Some sw
+ /\ match_states (State rsw' mr) sw.
+Proof.
+ induction i.
+ all: intros MS EARITH; subst; inv MS; unfold exec_arith_instr.
+ - (* PArithP *)
+ destruct i.
+ 1,2,3: DIRN1 rd; Simpl sr.
+ (* Special case for Pmovimms/Pmovimmd *)
+ all: simpl;
+ try(destruct (negb (is_immediate_float32 _)));
+ try(destruct (negb (is_immediate_float64 _)));
+ DIRN1 rd; Simpl sr;
+ try (rewrite assign_diff; discriminate_ppos; reflexivity);
+ try (intros rr'; Simpl_update).
+ - (* PArithPP *)
+ DIRN1 rs; DIRN1 rd; Simpl sr.
+ - (* PArithPPP *)
+ DIRN1 r1; DIRN1 r2; DIRN1 rd; Simpl sr.
+ - (* PArithRR0R *)
+ simpl. destruct r1.
+ + (* OArithRR0R *) simpl; Simpl sr.
+ + (* OArithRR0R_XZR *) simpl; destruct (arith_rr0r_isize _); Simpl sr.
+ - (* PArithRR0 *)
+ simpl. destruct r1.
+ + (* OArithRR0 *) simpl; Simpl sr.
+ + (* OArithRR0_XZR *) simpl; destruct (arith_rr0_isize _); Simpl sr.
+ - (* PArithARRRR0 *)
+ simpl. destruct r3.
+ + (* OArithARRRR0 *) simpl; Simpl sr.
+ + (* OArithARRRR0_XZR *) simpl; destruct (arith_arrrr0_isize _); Simpl sr.
+ - (* PArithComparisonPP *)
+ simpl; destruct i;
+ unfold arith_eval_comparison_pp, arith_prepare_comparison_pp; simpl;
+ fold (ppos r2); fold(ppos r1); try rewrite !H0; repeat Simpl_rep sr;
+ Simpl sr;
+ try destruct sz;
+ try (eapply compare_single_res; eauto);
+ try (eapply compare_long_res; eauto);
+ try (eapply compare_float_res; eauto).
+ - (* PArithComparisonR0R *)
+ simpl; destruct r1; simpl; destruct i;
+ repeat (replace_regs_cond_force; try fold (ppos r); try fold(ppos r2);
+ try rewrite !assign_diff; discriminate_ppos; try rewrite !H0);
+ Simpl sr; unfold arith_eval_comparison_r0r, arith_comparison_r0r_isize;
+ destruct arith_prepare_comparison_r0r; destruct is;
+ try (eapply compare_long_res; eauto);
+ try (eapply compare_int_res; eauto).
+ - (* PArithComparisonP *)
+ simpl; destruct i;
+ unfold arith_eval_comparison_p, arith_prepare_comparison_p; simpl;
+ fold(ppos r1); try rewrite !H0; repeat Simpl_rep sr;
+ Simpl sr;
+ try destruct sz;
+ try (eapply compare_int_res; eauto);
+ try (eapply compare_single_res; eauto);
+ try (eapply compare_long_res; eauto);
+ try (eapply compare_float_res; eauto).
+ - (* Pcset *)
+ simpl; unfold eval_testcond, get_testcond_rlocs, cflags_eval;
+ unfold cond_eval_is; unfold flags_testcond_value, list_exp_eval; destruct c; simpl;
+ repeat Simpl_rep sr; Simpl_exists sr;
+ destruct_res_flag rsr;
+ Simpl sr.
+ - (* Pfmovi *)
+ simpl; destruct r1; simpl; destruct fsz; Simpl sr.
+ - (* Pcsel *)
+ destruct c; simpl; DIRN1 rd; fold (ppos r2); fold (ppos r1); Simpl sr.
+ - (* Pfnmul *)
+ simpl; destruct fsz; Simpl sr.
+Qed.
+
+Lemma sp_xsp:
+ SP = XSP.
+Proof.
+ econstructor.
+Qed.
+
+Lemma load_chunk_neutral: forall chk v r,
+ interp_load (trans_ldp_chunk chk r) v = v.
+Proof.
+ intros; destruct chk; destruct r; simpl; reflexivity.
+Qed.
+
+Theorem bisimu_basic rsr mr sr bi:
+ match_states (State rsr mr) sr ->
+ match_outcome (exec_basic Ge.(_lk) Ge.(_genv) bi rsr mr) (inst_run Ge (trans_basic bi) sr sr).
+Proof.
+(* a little tactic to automate reasoning on preg_eq *)
+Local Hint Resolve not_eq_sym ppos_pmem_discr ppos_discr: core.
+Local Ltac preg_eq_discr r rd :=
+ destruct (preg_eq r rd); try (subst r; rewrite assign_eq, Pregmap.gss; auto);
+ rewrite (assign_diff _ (#rd) (#r) _); auto;
+ rewrite Pregmap.gso; auto.
+
+ intros MS; inversion MS as (H & H0).
+ destruct bi; simpl.
+ (* Loadsymbol / Cvtsw2x / Cvtuw2x / Cvtx2w *)
+ 6,7,8,9: Simpl sr.
+ - (* Arith *)
+ exploit trans_arith_correct; eauto.
+ - (* Load *)
+ destruct ld.
+ + unfold exec_load, exec_load_rd_a, eval_addressing_rlocs_ld, exp_eval;
+ destruct ld; destruct a; simpl;
+ try fold (ppos base); try fold (ppos r);
+ erewrite !H0, H; simpl;
+ unfold exec_load1, exec_load2, chunk_load; unfold call_ll_loadv;
+ try destruct (Mem.loadv _ _ _); simpl; auto.
+ all: try (fold (ppos rd); Simpl_exists sr; auto; intros rr; Simpl_update).
+ +
+ unfold exec_load, exec_load_double, eval_addressing_rlocs_ld, exp_eval;
+ destruct ld; destruct a; simpl; unfold control_eval; destruct Ge; auto;
+ try fold (ppos base);
+ try erewrite !H0, H; simpl;
+ unfold exec_load1, exec_load2; unfold call_ll_loadv;
+ destruct (Mem.loadv _ _ _); simpl; auto;
+ fold (ppos rd1); rewrite assign_diff; discriminate_ppos; rewrite H;
+ try destruct (Mem.loadv _ _ _); simpl; auto; Simpl_exists sr;
+ rewrite !load_chunk_neutral;
+ try (rewrite !assign_diff; discriminate_ppos; reflexivity);
+ try (destruct rd1 as [ir1|fr1]; try destruct ir1; destruct rd2 as [ir2|fr2]; try destruct ir2;
+ destruct base; discriminate_ppos);
+ repeat (try fold (ppos r); try fold (ppos r0);
+ try fold (ppos fr1); try fold (ppos fr2); intros; Simpl_update).
+ - (* Store *)
+ destruct st.
+ + unfold exec_store, exec_store_rs_a, eval_addressing_rlocs_st, exp_eval;
+ destruct st; destruct a; simpl;
+ try fold (ppos base); try fold (ppos rs); try fold (ppos r);
+ erewrite !H0, H; simpl;
+ unfold exec_store1, exec_store2, chunk_store; unfold call_ll_storev;
+ try destruct (Mem.storev _ _ _ _); simpl; auto.
+ all: eexists; split; [| split]; eauto;
+ intros rr; rewrite assign_diff; try rewrite H0; auto; discriminate_ppos.
+ + unfold exec_store, exec_store_double, eval_addressing_rlocs_st, exp_eval;
+ destruct st; destruct a; simpl; unfold control_eval; destruct Ge; auto;
+ try fold (ppos base); try fold (ppos rs1);
+ erewrite !H0, H; simpl;
+ unfold exec_store1, exec_store2; unfold call_ll_storev;
+ try destruct (Mem.storev _ _ _ _); simpl; auto;
+ fold (ppos rs2); rewrite assign_diff; try congruence; try rewrite H0; simpl;
+ try destruct (Mem.storev _ _ _ _); simpl; auto.
+ all: eexists; split; [| split]; eauto;
+ repeat (try intros rr; rewrite assign_diff; try rewrite H0; auto; discriminate_ppos).
+ - (* Alloc *)
+ destruct (Mem.alloc _ _ _) eqn:MEMAL. destruct (Mem.store _ _ _ _) eqn:MEMS.
+ + eexists; repeat split.
+ * rewrite !assign_diff; try discriminate_ppos; Simpl_exists sr;
+ rewrite H; destruct (Mem.alloc _ _ _) eqn:MEMAL2;
+ injection MEMAL; intros Hm Hb; try rewrite Hm, Hb;
+ rewrite sp_xsp in MEMS; rewrite MEMS.
+ rewrite !assign_diff; try discriminate_ppos; Simpl_exists sr; rewrite H;
+ destruct (Mem.alloc _ _ _) eqn:MEMAL3;
+ injection MEMAL2; intros Hm2 Hb2; try rewrite Hm2, Hb2;
+ rewrite Hm, Hb; rewrite MEMS; reflexivity.
+ * eauto.
+ * intros rr. rewrite assign_diff; try apply preg_not_pmem; try rewrite sp_xsp.
+ replace 37 with (#X29) by auto; repeat (Simpl_update; intros).
+ + simpl; repeat Simpl_exists sr. erewrite H. destruct (Mem.alloc _ _ _) eqn:HMEMAL2.
+ injection MEMAL; intros Hm Hb.
+ try rewrite Hm, Hb; clear Hm Hb.
+ try rewrite sp_xsp in MEMS; rewrite MEMS. reflexivity.
+ - (* Free *)
+ destruct (Mem.loadv _ _ _) eqn:MLOAD; simpl; auto;
+ repeat Simpl_exists sr; rewrite H; simpl.
+ + destruct (rsr SP) eqn:EQSP; simpl; rewrite <- sp_xsp; rewrite EQSP; rewrite MLOAD; try reflexivity.
+ destruct (Mem.free _ _ _) eqn:EQFREE; try reflexivity. rewrite assign_diff; discriminate_ppos.
+ replace_regs_pos sr; sr_val_rwrt. rewrite <- sp_xsp; rewrite EQSP; rewrite MLOAD. rewrite EQFREE.
+ replace 24 with (#X16) by auto; rewrite sp_xsp; Simpl sr.
+ intros rr'; destruct (PregEq.eq XSP rr').
+ * rewrite e; rewrite Pregmap.gss, sr_gss; auto.
+ * rewrite Pregmap.gso, !assign_diff; auto; apply ppos_discr in n; auto.
+ + rewrite <- sp_xsp; rewrite MLOAD; reflexivity.
+ - (* Nop *)
+ Simpl sr.
+Qed.
+
+Theorem bisimu_body:
+ forall bdy rsr mr sr,
+ match_states (State rsr mr) sr ->
+ match_outcome (exec_body Ge.(_lk) Ge.(_genv) bdy rsr mr) (exec Ge (trans_body bdy) sr).
+Proof.
+ induction bdy as [|i bdy]; simpl; eauto.
+ intros.
+ exploit (bisimu_basic rsr mr sr i); eauto.
+ destruct (exec_basic _ _ _ _ _); simpl.
+ - intros (s' & X1 & X2).
+ rewrite X1; simpl; eauto. eapply IHbdy; eauto; simpl.
+ unfold match_states in *. destruct s. unfold Asm._m. eauto.
+ - intros X; rewrite X; simpl; auto.
+Qed.
+
+Theorem bisimu_control ex sz rsr mr sr:
+ match_states (State rsr mr) sr ->
+ match_outcome (exec_cfi Ge.(_genv) Ge.(_fn) ex (incrPC (Ptrofs.repr sz) rsr) mr) (inst_run Ge (trans_pcincr sz (trans_exit (Some (PCtlFlow ex)))) sr sr).
+Proof.
+ intros MS.
+ simpl in *. inv MS.
+ destruct ex.
+ (* Obr / Oret *)
+ 6,7: unfold control_eval, incrPC; simpl; destruct Ge;
+ replace_pc; rewrite (H0 PC);
+ repeat Simpl_rep sr; Simpl_exists sr;
+ intros rr; destruct (preg_eq rr PC); [
+ rewrite e; rewrite sr_gss; rewrite Pregmap.gss;
+ try rewrite Pregmap.gso; discriminate_ppos; fold (ppos r); auto |
+ repeat Simpl_rep sr; try rewrite !Pregmap.gso; auto; apply ppos_discr in n; auto ].
+ (* Ocbnz / Ocbz *)
+ 6,7,8,9: unfold control_eval; destruct Ge; simpl;
+ replace_pc; rewrite (H0 PC);
+ unfold eval_branch, eval_neg_branch, eval_testzero, eval_testbit,
+ incrPC, goto_label_deps, goto_label;
+ destruct (PregEq.eq r PC);
+ [ rewrite e; destruct sz0; simpl; Simpl sr |
+ destruct sz0; simpl; replace_pc; rewrite Pregmap.gso; auto; repeat Simpl_rep sr; try rewrite H0;
+ try (destruct (Val.mxcmpu_bool _ _ _) eqn:EQCMP; try reflexivity; destruct b);
+ try (destruct (Val.mxcmplu_bool _ _ _) eqn:EQCMP; try reflexivity; destruct b);
+ try (destruct (Val.cmp_bool _ _ _) eqn:EQCMP; try reflexivity; destruct b);
+ try (destruct (Val.cmpl_bool _ _ _) eqn:EQCMP; try reflexivity; destruct b);
+ try (simpl; rewrite sr_update_overwrite; replace_pc; Simpl sr);
+ destruct (label_pos _ _ _); try reflexivity; rewrite Pregmap.gss;
+ destruct Val.offset_ptr; try reflexivity; rewrite sr_update_overwrite;
+ simpl; Simpl sr; (destruct (PregEq.eq rr PC); subst;
+ [ rewrite sr_gss, Pregmap.gss; reflexivity |
+ rewrite !assign_diff, !Pregmap.gso; replace_pc; auto; apply ppos_discr; auto]) ].
+ - (* Ob *)
+ replace_pc. rewrite (H0 PC). simpl.
+ unfold goto_label, control_eval. destruct Ge.
+ unfold goto_label_deps. destruct (label_pos _ _ _); auto.
+ + unfold incrPC. rewrite Pregmap.gss; eauto. destruct (Val.offset_ptr _ _); auto;
+ try (rewrite sr_gss; unfold Stuck; reflexivity).
+ simpl. eexists; split; split.
+ * rewrite sr_update_overwrite. unfold pmem, assign in *. simpl. rewrite H; reflexivity.
+ * intros. rewrite sr_update_overwrite. unfold Pregmap.set, assign.
+ destruct r as [dr|cr|]; try destruct dr as [ir|fr]; try destruct ir as [irr|];
+ try destruct irr; try destruct fr; try destruct cr; simpl; try rewrite <- H0; eauto.
+ + rewrite sr_gss; reflexivity.
+ - (* Obc *)
+ replace_pc. rewrite (H0 PC). simpl.
+ unfold eval_branch, goto_label, control_eval. destruct Ge.
+ unfold goto_label_deps, cflags_eval, eval_testcond, list_exp_eval.
+ destruct c; simpl; unfold incrPC;
+ repeat (replace_ppos; replace_pc; replace_regs_pos sr; sr_val_rwrt; try rewrite !assign_diff; discriminate_ppos).
+ 1,2,3,4,5,6: destruct_res_flag rsr.
+ 7,8,9,10: do 2 (destruct_res_flag rsr).
+ 11,12 : do 3 (destruct_res_flag rsr).
+ 1,2,3,4,5,6,9,10: destruct (Int.eq _ _); [| simpl; rewrite sr_update_overwrite; replace_pc; Simpl sr ];
+ destruct (label_pos _ _ _); [| reflexivity]; replace_pc;
+ rewrite !Pregmap.gss; destruct Val.offset_ptr;
+ try (unfold Stuck; reflexivity); Simpl_exists sr; intros rr;
+ apply reg_update_overwrite; eauto.
+ 1,3: destruct (andb); [| simpl; rewrite sr_update_overwrite; replace_pc; Simpl sr ];
+ destruct (label_pos _ _ _); [| reflexivity]; replace_pc; rewrite !Pregmap.gss;
+ destruct Val.offset_ptr; try (unfold Stuck; reflexivity); Simpl_exists sr;
+ intros rr; apply reg_update_overwrite; eauto.
+ 1,2: destruct (orb); [| simpl; rewrite sr_update_overwrite; replace_pc; Simpl sr ];
+ destruct (label_pos _ _ _); [| reflexivity]; replace_pc; rewrite !Pregmap.gss;
+ destruct Val.offset_ptr; try (unfold Stuck; reflexivity); Simpl_exists sr;
+ intros rr; apply reg_update_overwrite; eauto.
+ - (* Obl *)
+ replace_pc. rewrite (H0 PC). simpl.
+ unfold control_eval. destruct Ge.
+ rewrite sr_gss. Simpl sr.
+ replace_pc; try replace (38) with (#X30) by eauto; unfold incrPC. Simpl_update.
+ repeat (intros; Simpl_update).
+ - (* Obs *)
+ unfold control_eval. destruct Ge. replace_pc. rewrite (H0 PC). simpl; unfold incrPC.
+ replace_pc; Simpl_exists sr; intros rr; apply reg_update_overwrite; eauto.
+ - (* Oblr *)
+ replace_pc. rewrite (H0 PC).
+ unfold control_eval. destruct Ge. simpl. unfold incrPC.
+ try (eexists; split; [ | split ]); eauto.
+ intros rr; destruct (PregEq.eq PC rr).
+ + replace_pc; rewrite e; rewrite !Pregmap.gss, !sr_gss;
+ rewrite Pregmap.gso; fold (ppos r); try rewrite H0; auto.
+ rewrite <- e; destruct r; discriminate.
+ + replace_pc; rewrite Pregmap.gso, assign_diff; auto; apply ppos_discr in n; auto;
+ rewrite Pregmap.gss, sr_gss. destruct (PregEq.eq X30 rr); replace 38 with (#X30) by auto.
+ * rewrite e; rewrite Pregmap.gss, sr_gss; auto.
+ * replace_pc; rewrite !Pregmap.gso, !assign_diff; auto; apply ppos_discr in n0; auto;
+ apply ppos_discr; eauto.
+ - (* Obtbl *)
+ replace_pc; rewrite (H0 PC);
+ unfold control_eval; destruct Ge; simpl; unfold incrPC.
+ destruct (PregEq.eq X16 r1).
+ + fold (ppos r1); rewrite <- e; rewrite Pregmap.gss, sr_gss; reflexivity.
+ + rewrite !Pregmap.gso; auto; rewrite ppos_discr in n;
+ fold (ppos r1); replace 24 with (#X16) by auto; try rewrite !assign_diff; auto;
+ discriminate_ppos; rewrite H0; destruct (rsr r1) eqn:EQR;
+ try (try rewrite EQR; reflexivity).
+ try destruct (list_nth_z _ _); try reflexivity;
+ unfold goto_label, goto_label_deps; destruct (label_pos _ _ _);
+ try rewrite 2Pregmap.gso, Pregmap.gss; destruct (Val.offset_ptr (rsr PC) (Ptrofs.repr sz));
+ try reflexivity; discriminate_ppos. Simpl sr.
+ destruct (PregEq.eq X16 rr); [ subst; Simpl_update |];
+ destruct (PregEq.eq PC rr); [ subst; Simpl_update |].
+ rewrite !Pregmap.gso; auto;
+ apply ppos_discr in n0; apply ppos_discr in n1;
+ rewrite !assign_diff; auto.
+Qed.
+
+Theorem bisimu_exit ex sz rsr mr sr:
+ match_states (State rsr mr) sr ->
+ (*is_builtin ex = false ->*)
+ match_outcome (estep Ge.(_genv) Ge.(_fn) ex (Ptrofs.repr sz) rsr mr) (inst_run Ge (trans_pcincr sz (trans_exit ex)) sr sr).
+Proof.
+ intros MS; unfold estep.
+ destruct ex.
+ - destruct c.
+ + exploit (bisimu_control i sz rsr mr sr); eauto.
+ + simpl. inv MS. eexists; split; [| split].
+ unfold control_eval; destruct Ge.
+ replace_pc; rewrite (H0 PC); eauto.
+ rewrite assign_diff; auto.
+ intros rr. unfold incrPC. destruct (PregEq.eq rr PC); subst.
+ * rewrite sr_gss, Pregmap.gss. reflexivity.
+ * rewrite assign_diff, Pregmap.gso; try rewrite H0;
+ auto; try rewrite ppos_discr in n; auto.
+ - simpl. inv MS. eexists; split; [| split].
+ + unfold control_eval; destruct Ge.
+ replace_pc; rewrite (H0 PC); eauto.
+ + rewrite assign_diff; auto.
+ + intros rr. unfold incrPC. destruct (PregEq.eq rr PC); subst.
+ * rewrite sr_gss, Pregmap.gss. reflexivity.
+ * rewrite assign_diff, Pregmap.gso; try rewrite H0; auto; try rewrite ppos_discr in n; auto.
+Qed.
+
+(* Definition trans_block_aux bdy sz ex := (trans_body bdy) ++ (trans_pcincr sz (trans_exit ex) :: nil). *)
+
+Theorem bisimu rsr mr sr bb:
+ match_states (State rsr mr) sr ->
+ match_outcome (bbstep Ge.(_lk) Ge.(_genv) Ge.(_fn) bb rsr mr) (exec Ge (trans_block bb) sr).
+Proof.
+ intros MS. unfold bbstep, trans_block.
+ exploit (bisimu_body (body bb) rsr mr sr); eauto.
+ destruct (exec_body _ _ _ _ _); simpl.
+ - unfold match_states in *. intros (s' & X1 & X2). destruct s.
+ erewrite run_app_Some; eauto.
+ exploit (bisimu_exit (exit bb) (size bb) _rs _m s'); eauto.
+ destruct Ge; simpl. destruct MS as (Y1 & Y2). destruct X2 as (X2 & X3).
+ replace_pc. erewrite !X3; simpl.
+ destruct (inst_run _ _ _ _); simpl; auto.
+ - intros X; erewrite run_app_None; eauto.
+Qed.
+
+Theorem trans_state_match: forall S, match_states S (trans_state S).
+Proof.
+ intros. destruct S as (rs & m). simpl.
+ split. reflexivity.
+ intro. destruct r as [dr|cr|]; try destruct dr as [ir|fr]; try destruct cr;
+ try destruct ir as [irr|]; try destruct irr; try destruct fr; try reflexivity.
+Qed.
+
+Lemma state_eq_decomp:
+ forall rs1 m1 rs2 m2, rs1 = rs2 -> m1 = m2 -> State rs1 m1 = State rs2 m2.
+Proof.
+ intros. congruence.
+Qed.
+
+Theorem state_equiv S1 S2 S': match_states S1 S' -> match_states S2 S' -> S1 = S2.
+Proof.
+ unfold match_states; intros H0 H1. destruct S1 as (rs1 & m1). destruct S2 as (rs2 & m2). inv H0. inv H1.
+ apply state_eq_decomp.
+ - apply functional_extensionality. intros. assert (Val (rs1 x) = Val (rs2 x)) by congruence. congruence.
+ - congruence.
+Qed.
+
+End SECT_SEQ.
+
+Section SECT_BBLOCK_EQUIV.
+
+Variable Ge: genv.
+
+Local Hint Resolve trans_state_match: core.
+
+Lemma bblock_simu_reduce_aux:
+ forall p1 p2,
+ L.bblock_simu Ge (trans_block p1) (trans_block p2) ->
+ Asmblockprops.bblock_simu_aux Ge.(_lk) Ge.(_genv) Ge.(_fn) p1 p2.
+Proof.
+ intros p1 p2 H0 rs m EBB.
+ generalize (H0 (trans_state (State rs m))); clear H0.
+ intro H0.
+ exploit (bisimu Ge rs m (trans_state (State rs m)) p1); eauto.
+ exploit (bisimu Ge rs m (trans_state (State rs m)) p2); eauto.
+ destruct (bbstep Ge.(_lk) Ge.(_genv) Ge.(_fn) p1 rs m); try (unfold Stuck in EBB; congruence).
+ intros H1 (s2' & exp2 & MS'). unfold exec in exp2, H1. rewrite exp2 in H0.
+ destruct H0 as (m2' & H0 & H2). discriminate. rewrite H0 in H1.
+ destruct (bbstep Ge.(_lk) Ge.(_genv) Ge.(_fn) p2 rs m); simpl in H1.
+ * unfold match_states in H1, MS'. destruct s, s0.
+ destruct H1 as (s' & H1 & H3 & H4). inv H1. inv MS'.
+ replace (_rs0) with (_rs).
+ - replace (_m0) with (_m); auto. congruence.
+ - apply functional_extensionality. intros r.
+ generalize (H1 r). intros Hr. congruence.
+ * discriminate.
+Qed.
+
+Lemma incrPC_set_res_commut res: forall d vres rs,
+ incrPC d (set_res (map_builtin_res DR res) vres rs) =
+ set_res (map_builtin_res DR res) vres (incrPC d rs).
+Proof.
+ induction res; simpl; auto.
+ - intros; apply functional_extensionality.
+ unfold incrPC; intros x0.
+ destruct (PregEq.eq x0 PC).
+ + subst; rewrite! Pregmap.gss; auto.
+ + rewrite Pregmap.gso; auto.
+ destruct (PregEq.eq x x0).
+ * subst; rewrite! Pregmap.gss; auto.
+ * rewrite !Pregmap.gso; auto.
+ - intros; rewrite IHres2. f_equal. auto.
+Qed.
+
+Lemma incrPC_undef_regs_commut l : forall d rs,
+ incrPC d (undef_regs l rs) = undef_regs l (incrPC d rs).
+Proof.
+ induction l; simpl; auto.
+ intros. rewrite IHl. unfold incrPC.
+ destruct (PregEq.eq a PC).
+ - rewrite e. rewrite Pregmap.gss.
+ simpl. apply f_equal. unfold Pregmap.set.
+ apply functional_extensionality. intros x.
+ destruct (PregEq.eq x PC); auto.
+ - rewrite Pregmap.gso; auto.
+ apply f_equal. unfold Pregmap.set.
+ apply functional_extensionality. intros x.
+ destruct (PregEq.eq x PC).
+ + subst. destruct a; simpl; auto. congruence.
+ + auto.
+Qed.
+
+Lemma bblock_simu_reduce:
+ forall p1 p2,
+ L.bblock_simu Ge (trans_block p1) (trans_block p2) ->
+ (has_builtin p1 = true \/ has_builtin p2 = true -> exit p1 = exit p2) ->
+ Asmblockprops.bblock_simu Ge.(_lk) Ge.(_genv) Ge.(_fn) p1 p2.
+Proof.
+ unfold bblock_simu. intros p1 p2 H0 BLT rs m EBB.
+ unfold exec_bblock.
+ generalize (bblock_simu_reduce_aux p1 p2 H0).
+ unfold bblock_simu_aux. clear H0.
+ unfold exec_bblock, bbstep. intros H0 m' t0 (rs1 & m1 & H1 & H2).
+ assert ((has_builtin p1 = false /\ has_builtin p2 = false) \/ (has_builtin p1 = true \/ has_builtin p2 = true)). { repeat destruct (has_builtin _); simpl; intuition. }
+ destruct H as [[X1 X2]|H].
+ - (* Not a builtin *)
+ exploit (H0); eauto; erewrite H1; simpl; (*gen*)
+ unfold estep; unfold has_builtin in *.
+ { destruct (exit p1) as [[]|] eqn: EQEX1; try discriminate; simpl in *.
+ inversion H2; subst. rewrite H3; discriminate. }
+ destruct (exit p1) as [[]|] eqn: EQEX1; try discriminate; simpl in *.
+ { inversion H2; subst. rewrite H3.
+ destruct (exec_body _ _ (body p2) _ _) as [[rs2 m2]|]; try discriminate.
+ intros H4. eexists; eexists; split; try reflexivity.
+ destruct (exit p2) as [[]|] eqn:EQEX2; simpl in *; try discriminate; try econstructor; eauto.
+ inversion H4. econstructor. }
+ { inversion H2; subst.
+ destruct (exec_body _ _ (body p2) _ _) as [[rs2 m2]|]; try discriminate.
+intros H4. eexists; eexists; split; try reflexivity.
+ destruct (exit p2) as [[]|] eqn:EQEX2; simpl in *; try discriminate; try econstructor; eauto.
+ inversion H4. rewrite H3. econstructor. }
+ - (* Builtin *)
+ exploit (BLT); eauto.
+ intros EXIT.
+ unfold has_builtin in H.
+ assert (is_builtin (exit p1) = true). { rewrite <- EXIT in H; intuition. }
+ clear H.
+ generalize (H0 rs m); eauto; erewrite H1; simpl;
+ unfold estep. destruct (exit p1) as [[]|] eqn:EQEX1; try discriminate.
+ rewrite <- EXIT.
+ intros CONTRA.
+ exploit (CONTRA); try discriminate.
+ destruct (exec_body _ _ (body p2) _ _) as [[rs2 m2]|]; try discriminate.
+ intros H4. eexists; eexists; split; try reflexivity.
+ inversion H2; subst. inversion H4; subst.
+ econstructor; eauto.
+ + unfold incrPC in H5.
+ replace (rs2 SP) with (rs1 SP).
+ replace (fun r : dreg => rs2 r) with (fun r : dreg => rs1 r); auto.
+ * apply functional_extensionality.
+ intros r; destruct (PregEq.eq r PC); try discriminate.
+ replace (rs1 r) with (rs1 # PC <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size p1))) r) by auto.
+ rewrite H5; rewrite Pregmap.gso; auto.
+ * replace (rs1 SP) with (rs1 # PC <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size p1))) SP) by auto.
+ rewrite H5; rewrite Pregmap.gso; auto; try discriminate.
+ + rewrite !incrPC_set_res_commut.
+ rewrite !incrPC_undef_regs_commut.
+ rewrite H5.
+ reflexivity.
+Qed.
+
+(** Used for debug traces *)
+
+Definition ireg_name (ir: ireg) : pstring :=
+ match ir with
+ | X0 => Str ("X0") | X1 => Str ("X1") | X2 => Str ("X2") | X3 => Str ("X3") | X4 => Str ("X4") | X5 => Str ("X5") | X6 => Str ("X6") | X7 => Str ("X7")
+ | X8 => Str ("X8") | X9 => Str ("X9") | X10 => Str ("X10") | X11 => Str ("X11") | X12 => Str ("X12") | X13 => Str ("X13") | X14 => Str ("X14") | X15 => Str ("X15")
+ | X16 => Str ("X16") | X17 => Str ("X17") | X18 => Str ("X18") | X19 => Str ("X19") | X20 => Str ("X20") | X21 => Str ("X21") | X22 => Str ("X22") | X23 => Str ("X23")
+ | X24 => Str ("X24") | X25 => Str ("X25") | X26 => Str ("X26") | X27 => Str ("X27") | X28 => Str ("X28") | X29 => Str ("X29") | X30 => Str ("X30")
+ end
+.
+
+Definition freg_name (fr: freg) : pstring :=
+ match fr with
+ | D0 => Str ("D0") | D1 => Str ("D1") | D2 => Str ("D2") | D3 => Str ("D3") | D4 => Str ("D4") | D5 => Str ("D5") | D6 => Str ("D6") | D7 => Str ("D7")
+ | D8 => Str ("D8") | D9 => Str ("D9") | D10 => Str ("D10") | D11 => Str ("D11") | D12 => Str ("D12") | D13 => Str ("D13") | D14 => Str ("D14") | D15 => Str ("D15")
+ | D16 => Str ("D16") | D17 => Str ("D17") | D18 => Str ("D18") | D19 => Str ("D19") | D20 => Str ("D20") | D21 => Str ("D21") | D22 => Str ("D22") | D23 => Str ("D23")
+ | D24 => Str ("D24") | D25 => Str ("D25") | D26 => Str ("D26") | D27 => Str ("D27") | D28 => Str ("D28") | D29 => Str ("D29") | D30 => Str ("D30") | D31 => Str ("D31")
+ end
+.
+
+Definition iregsp_name (irsp: iregsp) : pstring :=
+ match irsp with
+ | RR1 ir => ireg_name ir
+ | XSP => Str ("XSP")
+ end.
+
+Definition dreg_name (dr: dreg) : ?? pstring :=
+ match dr with
+ | IR ir => match ir with
+ | XSP => RET (Str ("XSP"))
+ | RR1 irr => RET (ireg_name irr)
+ end
+ | FR fr => RET (freg_name fr)
+ end.
+
+Definition string_of_name (x: P.R.t): ?? pstring :=
+ if (Pos.eqb x pmem) then
+ RET (Str "MEM")
+ else
+ match inv_ppos x with
+ | Some (CR cr) => match cr with
+ | CN => RET (Str ("CN"))
+ | CZ => RET (Str ("CZ"))
+ | CC => RET (Str ("CC"))
+ | CV => RET (Str ("CV"))
+ end
+ | Some (PC) => RET (Str ("PC"))
+ | Some (DR dr) => dreg_name dr
+ | _ => RET (Str ("UNDEFINED"))
+ end.
+
+Definition string_of_name_ArithP (n: arith_p) : pstring :=
+ match n with
+ | Padrp _ _ => "Padrp"
+ | Pmovz _ _ _ => "Pmov"
+ | Pmovn _ _ _ => "Pmov"
+ | Pfmovimms _ => "Pfmovimm"
+ | Pfmovimmd _ => "Pfmovimm"
+ end.
+
+Definition string_of_name_ArithPP (n: arith_pp) : pstring :=
+ match n with
+ | Pmov => "Pmov"
+ | Pmovk _ _ _ => "Pmovk"
+ | Paddadr _ _ => "Paddadr"
+ | Psbfiz _ _ _ => "Psbfiz"
+ | Psbfx _ _ _ => "Psbfx"
+ | Pubfiz _ _ _ => "Pubfiz"
+ | Pubfx _ _ _ => "Pubfx"
+ | Pfmov => "Pfmov"
+ | Pfcvtds => "Pfcvtds"
+ | Pfcvtsd => "Pfcvtsd"
+ | Pfabs _ => "Pfabs"
+ | Pfneg _ => "Pfneg"
+ | Pscvtf _ _ => "Pscvtf"
+ | Pucvtf _ _ => "Pucvtf"
+ | Pfcvtzs _ _ => "Pfcvtzs"
+ | Pfcvtzu _ _ => "Pfcvtzu"
+ | Paddimm _ _ => "Paddimm"
+ | Psubimm _ _ => "Psubimm"
+ end.
+
+Definition string_of_name_ArithPPP (n: arith_ppp) : pstring :=
+ match n with
+ | Pasrv _ => "Pasrv"
+ | Plslv _ => "Plslv"
+ | Plsrv _ => "Plsrv"
+ | Prorv _ => "Prorv"
+ | Psmulh => "Psmulh"
+ | Pumulh => "Pumulh"
+ | Psdiv _ => "Psdiv"
+ | Pudiv _ => "Pudiv"
+ | Paddext _ => "Paddext"
+ | Psubext _ => "Psubext"
+ | Pfadd _ => "Pfadd"
+ | Pfdiv _ => "Pfdiv"
+ | Pfmul _ => "Pfmul"
+ | Pfsub _ => "Pfsub"
+ end.
+
+Definition string_of_name_ArithRR0R (n: arith_rr0r) : pstring :=
+ match n with
+ | Padd _ _ => "ArithRR0R=>Padd"
+ | Psub _ _ => "ArithRR0R=>Psub"
+ | Pand _ _ => "ArithRR0R=>Pand"
+ | Pbic _ _ => "ArithRR0R=>Pbic"
+ | Peon _ _ => "ArithRR0R=>Peon"
+ | Peor _ _ => "ArithRR0R=>Peor"
+ | Porr _ _ => "ArithRR0R=>Porr"
+ | Porn _ _ => "ArithRR0R=>Porn"
+ end.
+
+Definition string_of_name_ArithRR0R_XZR (n: arith_rr0r) : pstring :=
+ match n with
+ | Padd _ _ => "ArithRR0R_XZR=>Padd"
+ | Psub _ _ => "ArithRR0R_XZR=>Psub"
+ | Pand _ _ => "ArithRR0R_XZR=>Pand"
+ | Pbic _ _ => "ArithRR0R_XZR=>Pbic"
+ | Peon _ _ => "ArithRR0R_XZR=>Peon"
+ | Peor _ _ => "ArithRR0R_XZR=>Peor"
+ | Porr _ _ => "ArithRR0R_XZR=>Porr"
+ | Porn _ _ => "ArithRR0R_XZR=>Porn"
+ end.
+
+Definition string_of_name_ArithRR0 (n: arith_rr0) : pstring :=
+ match n with
+ | Pandimm _ _ => "ArithRR0=>Pandimm"
+ | Peorimm _ _ => "ArithRR0=>Peorimm"
+ | Porrimm _ _ => "ArithRR0=>Porrimm"
+ end.
+
+Definition string_of_name_ArithRR0_XZR (n: arith_rr0) : pstring :=
+match n with
+ | Pandimm _ _ => "ArithRR0_XZR=>Pandimm"
+ | Peorimm _ _ => "ArithRR0_XZR=>Peorimm"
+ | Porrimm _ _ => "ArithRR0_XZR=>Porrimm"
+ end.
+
+Definition string_of_name_ArithARRRR0 (n: arith_arrrr0) : pstring :=
+ match n with
+ | Pmadd _ => "ArithARRRR0=>Pmadd"
+ | Pmsub _ => "ArithARRRR0=>Pmsub"
+ end.
+
+Definition string_of_name_ArithARRRR0_XZR (n: arith_arrrr0) : pstring :=
+ match n with
+ | Pmadd _ => "ArithARRRR0_XZR=>Pmadd"
+ | Pmsub _ => "ArithARRRR0_XZR=>Pmsub"
+ end.
+
+Definition string_of_name_ArithComparisonPP_CN (n: arith_comparison_pp) : pstring :=
+ match n with
+ | Pcmpext _ => "ArithComparisonPP_CN=>Pcmpext"
+ | Pcmnext _ => "ArithComparisonPP_CN=>Pcmnext"
+ | Pfcmp _ => "ArithComparisonPP_CN=>Pfcmp"
+ end.
+
+Definition string_of_name_ArithComparisonPP_CZ (n: arith_comparison_pp) : pstring :=
+ match n with
+ | Pcmpext _ => "ArithComparisonPP_CZ=>Pcmpext"
+ | Pcmnext _ => "ArithComparisonPP_CZ=>Pcmnext"
+ | Pfcmp _ => "ArithComparisonPP_CZ=>Pfcmp"
+ end.
+
+Definition string_of_name_ArithComparisonPP_CC (n: arith_comparison_pp) : pstring :=
+match n with
+ | Pcmpext _ => "ArithComparisonPP_CC=>Pcmpext"
+ | Pcmnext _ => "ArithComparisonPP_CC=>Pcmnext"
+ | Pfcmp _ => "ArithComparisonPP_CC=>Pfcmp"
+ end.
+
+Definition string_of_name_ArithComparisonPP_CV (n: arith_comparison_pp) : pstring :=
+match n with
+ | Pcmpext _ => "ArithComparisonPP_CV=>Pcmpext"
+ | Pcmnext _ => "ArithComparisonPP_CV=>Pcmnext"
+ | Pfcmp _ => "ArithComparisonPP_CV=>Pfcmp"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CN (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CN=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CN=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CN=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CZ (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CZ=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CZ=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CZ=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CC (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CC=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CC=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CC=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CV (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CV=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CV=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CV=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CN_XZR (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CN_XZR=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CN_XZR=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CN_XZR=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CZ_XZR (n: arith_comparison_r0r) : pstring :=
+ match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CZ_XZR=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CZ_XZR=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CZ_XZR=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CC_XZR (n: arith_comparison_r0r) : pstring :=
+match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CC_XZR=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CC_XZR=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CC_XZR=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonR0R_CV_XZR (n: arith_comparison_r0r) : pstring :=
+match n with
+ | Pcmp _ _ => "ArithComparisonR0R_CV_XZR=>Pcmp"
+ | Pcmn _ _ => "ArithComparisonR0R_CV_XZR=>Pcmn"
+ | Ptst _ _ => "ArithComparisonR0R_CV_XZR=>Ptst"
+ end.
+
+Definition string_of_name_ArithComparisonP_CN (n: arith_comparison_p) : pstring :=
+ match n with
+ | Pfcmp0 _ => "ArithComparisonP_CN=>Pfcmp0"
+ | Pcmpimm _ _ => "ArithComparisonP_CN=>Pcmpimm"
+ | Pcmnimm _ _ => "ArithComparisonP_CN=>Pcmnimm"
+ | Ptstimm _ _ => "ArithComparisonP_CN=>Ptstimm"
+ end.
+
+Definition string_of_name_ArithComparisonP_CZ (n: arith_comparison_p) : pstring :=
+ match n with
+ | Pfcmp0 _ => "ArithComparisonP_CZ=>Pfcmp0"
+ | Pcmpimm _ _ => "ArithComparisonP_CZ=>Pcmpimm"
+ | Pcmnimm _ _ => "ArithComparisonP_CZ=>Pcmnimm"
+ | Ptstimm _ _ => "ArithComparisonP_CZ=>Ptstimm"
+ end.
+
+Definition string_of_name_ArithComparisonP_CC (n: arith_comparison_p) : pstring :=
+ match n with
+ | Pfcmp0 _ => "ArithComparisonP_CC=>Pfcmp0"
+ | Pcmpimm _ _ => "ArithComparisonP_CC=>Pcmpimm"
+ | Pcmnimm _ _ => "ArithComparisonP_CC=>Pcmnimm"
+ | Ptstimm _ _ => "ArithComparisonP_CC=>Ptstimm"
+ end.
+
+Definition string_of_name_ArithComparisonP_CV (n: arith_comparison_p) : pstring :=
+ match n with
+ | Pfcmp0 _ => "ArithComparisonP_CV=>Pfcmp0"
+ | Pcmpimm _ _ => "ArithComparisonP_CV=>Pcmpimm"
+ | Pcmnimm _ _ => "ArithComparisonP_CV=>Pcmnimm"
+ | Ptstimm _ _ => "ArithComparisonP_CV=>Ptstimm"
+ end.
+
+Definition string_of_name_cset (c: testcond) : pstring :=
+ match c with
+ | TCeq => "Cset=>TCeq"
+ | TCne => "Cset=>TCne"
+ | TChs => "Cset=>TChs"
+ | TClo => "Cset=>TClo"
+ | TCmi => "Cset=>TCmi"
+ | TCpl => "Cset=>TCpl"
+ | TChi => "Cset=>TChi"
+ | TCls => "Cset=>TCls"
+ | TCge => "Cset=>TCge"
+ | TClt => "Cset=>TClt"
+ | TCgt => "Cset=>TCgt"
+ | TCle => "Cset=>TCle"
+ end.
+
+Definition string_of_arith (op: arith_op): pstring :=
+ match op with
+ | OArithP n => string_of_name_ArithP n
+ | OArithPP n => string_of_name_ArithPP n
+ | OArithPPP n => string_of_name_ArithPPP n
+ | OArithRR0R n => string_of_name_ArithRR0R n
+ | OArithRR0R_XZR n _ => string_of_name_ArithRR0R_XZR n
+ | OArithRR0 n => string_of_name_ArithRR0 n
+ | OArithRR0_XZR n _ => string_of_name_ArithRR0_XZR n
+ | OArithARRRR0 n => string_of_name_ArithARRRR0 n
+ | OArithARRRR0_XZR n _ => string_of_name_ArithARRRR0_XZR n
+ | OArithComparisonPP_CN n => string_of_name_ArithComparisonPP_CN n
+ | OArithComparisonPP_CZ n => string_of_name_ArithComparisonPP_CZ n
+ | OArithComparisonPP_CC n => string_of_name_ArithComparisonPP_CC n
+ | OArithComparisonPP_CV n => string_of_name_ArithComparisonPP_CV n
+ | OArithComparisonR0R_CN n _ => string_of_name_ArithComparisonR0R_CN n
+ | OArithComparisonR0R_CZ n _ => string_of_name_ArithComparisonR0R_CZ n
+ | OArithComparisonR0R_CC n _ => string_of_name_ArithComparisonR0R_CC n
+ | OArithComparisonR0R_CV n _ => string_of_name_ArithComparisonR0R_CV n
+ | OArithComparisonR0R_CN_XZR n _ _ => string_of_name_ArithComparisonR0R_CN_XZR n
+ | OArithComparisonR0R_CZ_XZR n _ _ => string_of_name_ArithComparisonR0R_CZ_XZR n
+ | OArithComparisonR0R_CC_XZR n _ _ => string_of_name_ArithComparisonR0R_CC_XZR n
+ | OArithComparisonR0R_CV_XZR n _ _ => string_of_name_ArithComparisonR0R_CV_XZR n
+ | OArithComparisonP_CN n => string_of_name_ArithComparisonP_CN n
+ | OArithComparisonP_CZ n => string_of_name_ArithComparisonP_CZ n
+ | OArithComparisonP_CC n => string_of_name_ArithComparisonP_CC n
+ | OArithComparisonP_CV n => string_of_name_ArithComparisonP_CV n
+ | Ocset c => string_of_name_cset c
+ | Ofmovi _ => "Ofmovi"
+ | Ofmovi_XZR _ => "Ofmovi_XZR"
+ | Ocsel _ => "Ocsel"
+ | Ofnmul _ => "Ofnmul"
+ end.
+
+Definition string_of_ofs (ofs: ptrofs) : ?? pstring :=
+ (string_of_Z (Ptrofs.signed ofs)).
+
+Definition string_of_int (n: int) : ?? pstring :=
+ (string_of_Z (Int.signed n)).
+
+Definition string_of_int64 (n: int64) : ?? pstring :=
+ (string_of_Z (Int64.signed n)).
+
+Notation "x +; y" := (Concat x y).
+
+Definition string_of_addressing (a: addressing) : ?? pstring :=
+ match a with
+ | ADimm base n =>
+ DO n' <~ string_of_int64 n;;
+ RET ((Str "[ADimm ") +; (iregsp_name base) +; " " +; n' +; "]")
+ | ADreg base r =>
+ RET ((Str "[ADreg ") +; (iregsp_name base) +; " " +; (ireg_name r) +; "]")
+ | ADlsl base r n =>
+ DO n' <~ string_of_int n;;
+ RET ((Str "[ADlsl ") +; (iregsp_name base) +; " " +; (ireg_name r) +; " " +; n' +; "]")
+ | ADsxt base r n =>
+ DO n' <~ string_of_int n;;
+ RET ((Str "[ADsxt ") +; (iregsp_name base) +; " " +; (ireg_name r) +; " " +; n' +; "]")
+ | ADuxt base r n =>
+ DO n' <~ string_of_int n;;
+ RET ((Str "[ADuxt ") +; (iregsp_name base) +; " " +; (ireg_name r) +; " " +; n' +; "]")
+ | ADadr base id ofs =>
+ DO id' <~ string_of_Z (Zpos id);;
+ DO ofs' <~ string_of_ofs ofs;;
+ RET ((Str "[ADadr ") +; (iregsp_name base) +; " " +; id' +; " " +; ofs' +; "]")
+ | ADpostincr base n =>
+ DO n' <~ string_of_int64 n;;
+ RET ((Str "[ADpostincr ") +; (iregsp_name base) +; " " +; n' +; "]")
+ end.
+
+Definition string_of_ld_rd_a (ld: load_rd_a) : pstring :=
+ match ld with
+ | Pldrw => Str "Pldrw"
+ | Pldrw_a => Str "Pldrw_a"
+ | Pldrx => Str "Pldrx"
+ | Pldrx_a => Str "Pldrx_a"
+ | Pldrb _ => Str "Pldrb"
+ | Pldrsb _ => Str "Pldrsb"
+ | Pldrh _ => Str "Pldrh"
+ | Pldrsh _ => Str "Pldrsh"
+ | Pldrzw => Str "Pldrzw"
+ | Pldrsw => Str "Pldrsw"
+ | Pldrs => Str "Pldrs"
+ | Pldrd => Str "Pldrd"
+ | Pldrd_a => Str "Pldrd_a"
+ end.
+
+Definition string_of_ldi (ldi: ld_instruction) : ?? pstring:=
+ match ldi with
+ | PLd_rd_a ld rd a =>
+ DO a' <~ string_of_addressing a;;
+ DO rd' <~ dreg_name rd;;
+ RET (Str "PLd_rd_a (" +; (string_of_ld_rd_a ld) +; " " +; rd' +; " " +; a' +; ")")
+ | Pldp _ _ _ _ _ a =>
+ DO a' <~ string_of_addressing a;;
+ RET (Str "Pldp (" +; a' +; ")")
+ end.
+
+Definition string_of_load (op: load_op) : ?? pstring :=
+ match op with
+ | Oload1 ld _ a =>
+ DO a' <~ string_of_addressing a;;
+ (*DO ld' <~ string_of_ldi ld;;*)
+ RET((Str "Oload1 ") +; " " +; string_of_ld_rd_a ld +; " " +; a' +; " ")
+ | Oload2 ld _ a =>
+ DO a' <~ string_of_addressing a;;
+ (*DO ld' <~ string_of_ldi ld;;*)
+ RET((Str "Oload2 ") +; " " +; string_of_ld_rd_a ld +; " " +; a' +; " ")
+ | OloadU _ _ _ => RET (Str "OloadU")
+ end.
+
+Definition string_of_st_rs_a (st: store_rs_a) : pstring :=
+ match st with
+ | Pstrw => Str "Pstrw"
+ | Pstrw_a => Str "Pstrw_a"
+ | Pstrx => Str "Pstrx"
+ | Pstrx_a => Str "Pstrx_a"
+ | Pstrb => Str "Pstrb"
+ | Pstrh => Str "Pstrh"
+ | Pstrs => Str "Pstrs"
+ | Pstrd => Str "Pstrd"
+ | Pstrd_a => Str "Pstrd_a"
+ end.
+
+Definition string_of_sti (sti: st_instruction) : ?? pstring:=
+ match sti with
+ | PSt_rs_a st rs a =>
+ DO a' <~ string_of_addressing a;;
+ DO rs' <~ dreg_name rs;;
+ RET (Str "PSt_rs_a (" +; (string_of_st_rs_a st) +; " " +; rs' +; " " +; a' +; ")")
+ | Pstp _ _ _ _ _ a =>
+ DO a' <~ string_of_addressing a;;
+ RET (Str "Pstp (" +; a' +; ")")
+ end.
+
+Definition string_of_store (op: store_op) : ?? pstring :=
+ match op with
+ | Ostore1 st _ a =>
+ DO a' <~ string_of_addressing a;;
+ (*DO st' <~ string_of_sti st;;*)
+ RET((Str "Ostore1 ") +; " " +; string_of_st_rs_a st +; " " +; a' +; " ")
+ | Ostore2 st _ a =>
+ DO a' <~ string_of_addressing a;;
+ (*DO st' <~ string_of_sti st;;*)
+ RET((Str "Ostore2 ") +; " " +; string_of_st_rs_a st +; " " +; a' +; " ")
+ | OstoreU _ _ _ => RET (Str "OstoreU")
+ end.
+
+Definition string_of_control (op: control_op) : pstring :=
+ match op with
+ | Ob _ => "Ob"
+ | Obc _ _ => "Obc"
+ | Obl _ => "Obl"
+ | Obs _ => "Obs"
+ | Ocbnz _ _ => "Ocbnz"
+ | Ocbz _ _ => "Ocbz"
+ | Otbnz _ _ _ => "Otbnz"
+ | Otbz _ _ _ => "Otbz"
+ | Obtbl _ => "Obtbl"
+ | OIncremPC _ => "OIncremPC"
+ | OError => "OError"
+ end.
+
+Definition string_of_allocf (op: allocf_op) : pstring :=
+ match op with
+ | OAllocf_SP _ _ => "OAllocf_SP"
+ | OAllocf_Mem _ _ => "OAllocf_Mem"
+ end.
+
+Definition string_of_freef (op: freef_op) : pstring :=
+ match op with
+ | OFreef_SP _ _ => "OFreef_SP"
+ | OFreef_Mem _ _ => "OFreef_Mem"
+ end.
+
+Definition string_of_op (op: P.op): ?? pstring :=
+ match op with
+ | Arith op => RET (string_of_arith op)
+ | Load op => string_of_load op
+ | Store op => string_of_store op
+ | Control op => RET (string_of_control op)
+ | Allocframe op => RET (string_of_allocf op)
+ | Freeframe op => RET (string_of_freef op)
+ | Loadsymbol _ => RET (Str "Loadsymbol")
+ | Constant _ => RET (Str "Constant")
+ | Cvtsw2x => RET (Str "Cvtsw2x")
+ | Cvtuw2x => RET (Str "Cvtuw2x")
+ | Cvtx2w => RET (Str "Cvtx2w")
+ (*| Fail => RET (Str "Fail")*)
+ end.
+End SECT_BBLOCK_EQUIV.
+
+(** REWRITE RULES *)
+
+Definition is_constant (o: op): bool :=
+ match o with
+ | OArithP _ | OArithRR0_XZR _ _ | Ofmovi_XZR _ | Loadsymbol _ | Constant _ | Obl _ | Obs _ => true
+ | _ => false
+ end.
+
+Lemma is_constant_correct ge o: is_constant o = true -> op_eval ge o [] <> None.
+Proof.
+ destruct o; simpl in * |- *; try congruence.
+ destruct op0; simpl in * |- *; try congruence.
+ destruct co; simpl in * |- *; try congruence;
+ unfold control_eval; destruct ge; simpl in * |- *; try congruence.
+Qed.
+
+Definition main_reduce (t: Terms.term):= RET (Terms.nofail is_constant t).
+
+Local Hint Resolve is_constant_correct: wlp.
+
+Lemma main_reduce_correct t:
+ WHEN main_reduce t ~> pt THEN Terms.match_pt t pt.
+Proof.
+ wlp_simplify.
+Qed.
+
+Definition reduce := {| Terms.result := main_reduce; Terms.result_correct := main_reduce_correct |}.
+
+Definition bblock_simu_test (verb: bool) (p1 p2: Asmblock.bblock) : ?? bool :=
+ assert_same_builtin p1 p2;;
+ if verb then
+ IST.verb_bblock_simu_test reduce string_of_name string_of_op (trans_block p1) (trans_block p2)
+ else
+ IST.bblock_simu_test reduce (trans_block p1) (trans_block p2).
+
+Local Hint Resolve IST.bblock_simu_test_correct IST.verb_bblock_simu_test_correct: wlp.
+
+(** Main simulation (Impure) theorem *)
+Theorem bblock_simu_test_correct verb p1 p2 :
+ WHEN bblock_simu_test verb p1 p2 ~> b THEN b=true -> forall ge fn lk, Asmblockprops.bblock_simu lk ge fn p1 p2.
+Proof.
+ wlp_simplify; eapply bblock_simu_reduce with (Ge:={| _genv := ge; _fn := fn; _lk := lk |}); eauto;
+ intros; destruct H; auto.
+Qed.
+
+Hint Resolve bblock_simu_test_correct: wlp.
+
+(** ** Coerce bblock_simu_test into a pure function (this is a little unsafe like all oracles in CompCert). *)
+
+Import UnsafeImpure.
+
+Definition pure_bblock_simu_test (verb: bool) (p1 p2: Asmblock.bblock): bool :=
+ match unsafe_coerce (bblock_simu_test verb p1 p2) with
+ | Some b => b
+ | None => false
+ end.
+
+Theorem pure_bblock_simu_test_correct verb p1 p2 lk ge fn: pure_bblock_simu_test verb p1 p2 = true -> Asmblockprops.bblock_simu lk ge fn p1 p2.
+Proof.
+ unfold pure_bblock_simu_test.
+ destruct (unsafe_coerce (bblock_simu_test verb p1 p2)) eqn: UNSAFE; try discriminate.
+ intros; subst. eapply bblock_simu_test_correct; eauto.
+ apply unsafe_coerce_not_really_correct; eauto.
+Qed.
+
+Definition bblock_simub: Asmblock.bblock -> Asmblock.bblock -> bool := pure_bblock_simu_test true.
+
+Lemma bblock_simub_correct p1 p2 lk ge fn: bblock_simub p1 p2 = true -> Asmblockprops.bblock_simu lk ge fn p1 p2.
+Proof.
+ eapply (pure_bblock_simu_test_correct true).
+Qed.
diff --git a/aarch64/Asmblockgen.v b/aarch64/Asmblockgen.v
new file mode 100644
index 00000000..acb5a1e6
--- /dev/null
+++ b/aarch64/Asmblockgen.v
@@ -0,0 +1,1252 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Translation from Machblock to AArch64 assembly block language (Asmblock)
+ Inspired from the Mach->Asm pass of original Leroy's backend, but adapted to the block structure like the KVX backend. *)
+
+Require Import Recdef Coqlib Zwf Zbits.
+Require Import Errors AST Integers Floats Op.
+Require Import Locations Machblock Asm Asmblock.
+
+Local Open Scope string_scope.
+Local Open Scope list_scope.
+Local Open Scope error_monad_scope.
+
+(** Extracting integer or float registers. *)
+
+Definition ireg_of (r: mreg) : res ireg :=
+ match preg_of r with
+ | IR irs => match irs with
+ | RR1 mr => OK mr
+ | _ => Error(msg "Asmgenblock.ireg_of")
+ end
+ | _ => Error(msg "Asmgenblock.iregsp_of")
+ end.
+
+Definition freg_of (r: mreg) : res freg :=
+ match preg_of r with FR mr => OK mr | _ => Error(msg "Asmgenblock.freg_of") end.
+
+(** Recognition of immediate arguments for logical integer operations.*)
+
+(** Valid immediate arguments are repetitions of a bit pattern [B]
+ of length [e] = 2, 4, 8, 16, 32 or 64.
+ The bit pattern [B] must be of the form [0*1*0*] or [1*0*1*]
+ but must not be all zeros or all ones. *)
+
+(** The following automaton recognizes [0*1*0*|1*0*1*].
+<<
+ 0 1 0
+ / \ / \ / \
+ \ / \ / \ /
+ -0--> [B] --1--> [D] --0--> [F]
+ /
+ [A]
+ \
+ -1--> [C] --0--> [E] --1--> [G]
+ / \ / \ / \
+ \ / \ / \ /
+ 1 0 1
+>>
+*)
+
+Module Automaton.
+
+Inductive state : Type := SA | SB | SC | SD | SE | SF | SG | Sbad.
+
+Definition start := SA.
+
+Definition next (s: state) (b: bool) :=
+ match s, b with
+ | SA,false => SB | SA,true => SC
+ | SB,false => SB | SB,true => SD
+ | SC,false => SE | SC,true => SC
+ | SD,false => SF | SD,true => SD
+ | SE,false => SE | SE,true => SG
+ | SF,false => SF | SF,true => Sbad
+ | SG,false => Sbad | SG,true => SG
+ | Sbad,_ => Sbad
+ end.
+
+Definition accepting (s: state) :=
+ match s with
+ | SA | SB | SC | SD | SE | SF | SG => true
+ | Sbad => false
+ end.
+
+Fixpoint run (len: nat) (s: state) (x: Z) : bool :=
+ match len with
+ | Datatypes.O => accepting s
+ | Datatypes.S len => run len (next s (Z.odd x)) (Z.div2 x)
+ end.
+
+End Automaton.
+
+(** The following function determines the candidate length [e],
+ ensuring that [x] is a repetition [BB...B]
+ of a bit pattern [B] of length [e]. *)
+
+Definition logical_imm_length (x: Z) (sixtyfour: bool) : nat :=
+ (** [test n] checks that the low [2n] bits of [x] are of the
+ form [BB], that is, two occurrences of the same [n] bits *)
+ let test (n: Z) : bool :=
+ Z.eqb (Zzero_ext n x) (Zzero_ext n (Z.shiftr x n)) in
+ (** If [test n] fails, we know that the candidate length [e] is
+ at least [2n]. Hence we test with decreasing values of [n]:
+ 32, 16, 8, 4, 2. *)
+ if sixtyfour && negb (test 32) then 64%nat
+ else if negb (test 16) then 32%nat
+ else if negb (test 8) then 16%nat
+ else if negb (test 4) then 8%nat
+ else if negb (test 2) then 4%nat
+ else 2%nat.
+
+(** A valid logical immediate is
+- neither [0] nor [-1];
+- composed of a repetition [BBBBB] of a bit-pattern [B] of length [e]
+- the low [e] bits of the number, that is, [B], match [0*1*0*] or [1*0*1*].
+*)
+
+Definition is_logical_imm32 (x: int) : bool :=
+ negb (Int.eq x Int.zero) && negb (Int.eq x Int.mone) &&
+ Automaton.run (logical_imm_length (Int.unsigned x) false)
+ Automaton.start (Int.unsigned x).
+
+Definition is_logical_imm64 (x: int64) : bool :=
+ negb (Int64.eq x Int64.zero) && negb (Int64.eq x Int64.mone) &&
+ Automaton.run (logical_imm_length (Int64.unsigned x) true)
+ Automaton.start (Int64.unsigned x).
+
+(** Arithmetic immediates are 12-bit unsigned numbers, possibly shifted left 12 bits *)
+
+Definition is_arith_imm32 (x: int) : bool :=
+ Int.eq x (Int.zero_ext 12 x)
+ || Int.eq x (Int.shl (Int.zero_ext 12 (Int.shru x (Int.repr 12))) (Int.repr 12)).
+
+Definition is_arith_imm64 (x: int64) : bool :=
+ Int64.eq x (Int64.zero_ext 12 x)
+ || Int64.eq x (Int64.shl (Int64.zero_ext 12 (Int64.shru x (Int64.repr 12))) (Int64.repr 12)).
+
+Definition bcode := list basic.
+
+Program Definition single_basic (bi: basic): bblock :=
+ {| header := nil; body:= bi::nil; exit := None |}.
+
+(* insert [bi] at the head of [k] *)
+Program Definition insert_basic (bi: basic) (k:bblocks): bblocks :=
+ match k with
+ | bb::k' =>
+ match bb.(header) with
+ | nil => {| header := nil; body := bi :: (body bb); exit := exit bb |}::k'
+ | _ => (single_basic bi)::k
+ end
+ | _ => (single_basic bi)::k
+ end.
+
+Notation "bi ::b k" := (insert_basic bi k) (at level 49, right associativity).
+
+(* NB: this notation helps the Coq typechecker to infer coercion [PArith] in [bcode] expressions *)
+(** Alignment check for symbols *)
+Notation "i ::bi k" := (cons (A:=basic) i k) (at level 49, right associativity).
+Notation "a @@ b" := (app a b) (at level 49, right associativity).
+
+(* The pop_bc and push_bc functions are used to adapt the output of some definitions
+ in bblocks format and avoid some redefinitions. *)
+
+(* pop_bc takes the body of the first bblock in the list if it does not have a header. *)
+Definition pop_bc (k: bblocks): bcode :=
+ match k with
+ | bb :: k' => match bb.(header) with
+ | nil => (body bb)
+ | _ => nil
+ end
+ | _ => nil
+ end.
+
+(* push_bc tries to overwrite code in the first bblock if it does not have a header,
+ otherwise, a new bblock is created and appended to the list. *)
+Program Definition push_bc (bc: bcode) (k:bblocks): bblocks :=
+ match bc with
+ | bi :: bc' => match k with
+ | bb :: k' => match bb.(header) with
+ | nil => {| header := nil; body := bc; exit := exit bb |} :: k'
+ | _ => {| header := nil; body := bc; exit := None |} :: k
+ end
+ | _ => {| header := nil; body := bc; exit := None |} :: nil
+ end
+ | nil => k
+ end.
+Next Obligation.
+ simpl; auto.
+Qed.
+Next Obligation.
+ simpl; auto.
+Qed.
+Next Obligation.
+ simpl; auto.
+Qed.
+
+Parameter symbol_is_aligned : ident -> Z -> bool.
+(** [symbol_is_aligned id sz] checks whether the symbol [id] is [sz] aligned *)
+
+(***************************************************************************************)
+
+(** Decompose integer literals into 16-bit fragments *)
+
+Fixpoint decompose_int (N: nat) (n p: Z) {struct N} : list (Z * Z) :=
+ match N with
+ | Datatypes.O => nil
+ | Datatypes.S N =>
+ let frag := Zzero_ext 16 (Z.shiftr n p) in
+ if Z.eqb frag 0 then
+ decompose_int N n (p + 16)
+ else
+ (frag, p) :: decompose_int N (Z.ldiff n (Z.shiftl 65535 p)) (p + 16)
+ end.
+
+Definition negate_decomposition (l: list (Z * Z)) :=
+ List.map (fun np => (Z.lxor (fst np) 65535, snd np)) l.
+
+Definition loadimm_k (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: bcode) : bcode :=
+ List.fold_right (fun np k => Pmovk sz (fst np) (snd np) rd rd ::bi k) k l.
+
+Definition loadimm_z (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: bcode) : bcode :=
+ match l with
+ | nil => Pmovz sz 0 0 rd ::bi k
+ | (n1, p1) :: l => (Pmovz sz n1 p1 rd) ::bi loadimm_k sz rd l k
+ end.
+
+Definition loadimm_n (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: bcode) : bcode :=
+ match l with
+ | nil => Pmovn sz 0 0 rd ::bi k
+ | (n1, p1) :: l => Pmovn sz n1 p1 rd ::bi loadimm_k sz rd (negate_decomposition l) k
+ end.
+
+Definition loadimm (sz: isize) (rd: ireg) (n: Z) (k: bcode) : bcode :=
+ let N := match sz with W => 2%nat | X => 4%nat end in
+ let dz := decompose_int N n 0 in
+ let dn := decompose_int N (Z.lnot n) 0 in
+ if Nat.leb (List.length dz) (List.length dn)
+ then loadimm_z sz rd dz k
+ else loadimm_n sz rd dn k.
+
+Definition loadimm32 (rd: ireg) (n: int) (k: bcode) : bcode :=
+ if is_logical_imm32 n
+ then Porrimm W (Int.unsigned n) rd XZR ::bi k
+ else loadimm W rd (Int.unsigned n) k.
+
+Definition loadimm64 (rd: ireg) (n: int64) (k: bcode) : bcode :=
+ if is_logical_imm64 n
+ then Porrimm X (Int64.unsigned n) rd XZR ::bi k
+ else loadimm X rd (Int64.unsigned n) k.
+
+Definition offset_representable (sz: Z) (ofs: int64) : bool :=
+ let isz := Int64.repr sz in
+ (** either unscaled 9-bit signed *)
+ Int64.eq ofs (Int64.sign_ext 9 ofs) ||
+ (** or scaled 12-bit unsigned *)
+ (Int64.eq (Int64.modu ofs isz) Int64.zero
+ && Int64.ltu ofs (Int64.shl isz (Int64.repr 12))).
+
+Definition indexed_memory_access_bc (insn: addressing -> basic)
+ (sz: Z) (base: iregsp) (ofs: ptrofs) (k: bcode) : bcode :=
+ let ofs := Ptrofs.to_int64 ofs in
+ if offset_representable sz ofs
+ then insn (ADimm base ofs) :: k
+ else loadimm64 X16 ofs (insn (ADreg base X16) :: k).
+
+Definition loadind (base: iregsp) (ofs: ptrofs) (ty: typ) (dst: mreg) (k: bcode) :=
+ match ty, preg_of dst with
+ | Tint, IR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrw rd) 4 base ofs k)
+ | Tlong, IR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrx rd) 8 base ofs k)
+ | Tsingle, FR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrs rd) 4 base ofs k)
+ | Tfloat, FR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrd rd) 8 base ofs k)
+ | Tany32, IR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrw_a rd) 4 base ofs k)
+ | Tany64, IR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrx_a rd) 8 base ofs k)
+ | Tany64, FR rd => OK (indexed_memory_access_bc (PLd_rd_a Pldrd_a rd) 8 base ofs k)
+ | _, _ => Error (msg "Asmgen.loadind")
+ end.
+
+Definition storeind (src: mreg) (base: iregsp) (ofs: ptrofs) (ty: typ) (k: bcode) :=
+ match ty, preg_of src with
+ | Tint, IR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrw rd) 4 base ofs k)
+ | Tlong, IR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrx rd) 8 base ofs k)
+ | Tsingle, FR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrs rd) 4 base ofs k)
+ | Tfloat, FR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrd rd) 8 base ofs k)
+ | Tany32, IR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrw_a rd) 4 base ofs k)
+ | Tany64, IR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrx_a rd) 8 base ofs k)
+ | Tany64, FR rd => OK (indexed_memory_access_bc (PSt_rs_a Pstrd_a rd) 8 base ofs k)
+ | _, _ => Error (msg "Asmgen.storeind")
+ end.
+
+Definition loadptr_bc (base: iregsp) (ofs: ptrofs) (dst: ireg) (k: bcode): bcode :=
+ indexed_memory_access_bc (PLd_rd_a Pldrx dst) 8 base ofs k.
+
+Definition storeptr_bc (src: ireg) (base: iregsp) (ofs: ptrofs) (k: bcode): bcode :=
+ indexed_memory_access_bc (PSt_rs_a Pstrx src) 8 base ofs k.
+
+(** Function epilogue *)
+
+Definition make_epilogue (f: Machblock.function) : bcode :=
+ loadptr_bc XSP f.(fn_retaddr_ofs) RA
+ (Pfreeframe f.(fn_stacksize) f.(fn_link_ofs)::nil).
+
+(** Add immediate *)
+
+Definition addimm_aux (insn: Z -> arith_pp)
+ (rd r1: iregsp) (n: Z) (k: bcode) :=
+ let nlo := Zzero_ext 12 n in
+ let nhi := n - nlo in
+ if Z.eqb nhi 0 then
+ insn nlo rd r1 ::bi k
+ else if Z.eqb nlo 0 then
+ insn nhi rd r1 ::bi k
+ else
+ insn nhi rd r1 ::bi insn nlo rd rd ::bi k.
+
+Definition addimm32 (rd r1: ireg) (n: int) (k: bcode) : bcode :=
+ let m := Int.neg n in
+ if Int.eq n (Int.zero_ext 24 n) then
+ addimm_aux (Paddimm W) rd r1 (Int.unsigned n) k
+ else if Int.eq m (Int.zero_ext 24 m) then
+ addimm_aux (Psubimm W) rd r1 (Int.unsigned m) k
+ else if Int.lt n Int.zero then
+ loadimm32 X16 m (Psub W SOnone rd r1 X16 ::bi k)
+ else
+ loadimm32 X16 n (Padd W SOnone rd r1 X16 ::bi k).
+
+Definition addimm64 (rd r1: iregsp) (n: int64) (k: bcode) : bcode :=
+ let m := Int64.neg n in
+ if Int64.eq n (Int64.zero_ext 24 n) then
+ addimm_aux (Paddimm X) rd r1 (Int64.unsigned n) k
+ else if Int64.eq m (Int64.zero_ext 24 m) then
+ addimm_aux (Psubimm X) rd r1 (Int64.unsigned m) k
+ else if Int64.lt n Int64.zero then
+ loadimm64 X16 m (Psubext (EOuxtx Int.zero) rd r1 X16 ::bi k)
+ else
+ loadimm64 X16 n (Paddext (EOuxtx Int.zero) rd r1 X16 ::bi k).
+
+(** Logical immediate *)
+
+Definition logicalimm32
+ (insn1: Z -> arith_rr0)
+ (insn2: shift_op -> arith_rr0r)
+ (rd r1: ireg) (n: int) (k: bcode) : bcode :=
+ if is_logical_imm32 n
+ then insn1 (Int.unsigned n) rd r1 ::bi k
+ else loadimm32 X16 n (insn2 SOnone rd r1 X16 ::bi k).
+
+Definition logicalimm64
+ (insn1: Z -> arith_rr0)
+ (insn2: shift_op -> arith_rr0r)
+ (rd r1: ireg) (n: int64) (k: bcode) : bcode :=
+ if is_logical_imm64 n
+ then insn1 (Int64.unsigned n) rd r1 ::bi k
+ else loadimm64 X16 n (insn2 SOnone rd r1 X16 ::bi k).
+
+(** Sign- or zero-extended arithmetic *)
+
+Definition transl_extension (ex: extension) (a: int) : extend_op :=
+ match ex with Xsgn32 => EOsxtw a | Xuns32 => EOuxtw a end.
+
+Definition move_extended_base
+ (rd: ireg) (r1: ireg) (ex: extension) (k: bcode) : bcode :=
+ match ex with
+ | Xsgn32 => Pcvtsw2x rd r1 ::bi k
+ | Xuns32 => Pcvtuw2x rd r1 ::bi k
+ end.
+
+Definition move_extended
+ (rd: ireg) (r1: ireg) (ex: extension) (a: int) (k: bcode) : bcode :=
+ if Int.eq a Int.zero then
+ move_extended_base rd r1 ex k
+ else
+ move_extended_base rd r1 ex (Padd X (SOlsl a) rd XZR rd ::bi k).
+
+Definition arith_extended
+ (insnX: extend_op -> arith_ppp)
+ (insnS: shift_op -> arith_rr0r)
+ (rd r1 r2: ireg) (ex: extension) (a: int) (k: bcode) : bcode :=
+ if Int.ltu a (Int.repr 5) then
+ insnX (transl_extension ex a) rd r1 r2 ::bi k
+ else
+ move_extended_base X16 r2 ex (insnS (SOlsl a) rd r1 X16 ::bi k).
+
+(** Extended right shift *)
+
+Definition shrx32 (rd r1: ireg) (n: int) (k: bcode) : bcode :=
+ if Int.eq n Int.zero then
+ Pmov rd r1 ::bi k
+ else
+ Porr W (SOasr (Int.repr 31)) X16 XZR r1 ::bi
+ Padd W (SOlsr (Int.sub Int.iwordsize n)) X16 r1 X16 ::bi
+ Porr W (SOasr n) rd XZR X16 ::bi k.
+
+Definition shrx64 (rd r1: ireg) (n: int) (k: bcode) : bcode :=
+ if Int.eq n Int.zero then
+ Pmov rd r1 ::bi k
+ else
+ Porr X (SOasr (Int.repr 63)) X16 XZR r1 ::bi
+ Padd X (SOlsr (Int.sub Int64.iwordsize' n)) X16 r1 X16 ::bi
+ Porr X (SOasr n) rd XZR X16 ::bi k.
+
+(** Load the address [id + ofs] in [rd] *)
+
+Definition loadsymbol (rd: ireg) (id: ident) (ofs: ptrofs) (k: bcode) : bcode :=
+ if Archi.pic_code tt then
+ if Ptrofs.eq ofs Ptrofs.zero then
+ Ploadsymbol rd id ::bi k
+ else
+ Ploadsymbol rd id :: addimm64 rd rd (Ptrofs.to_int64 ofs) k
+ else
+ Padrp id ofs rd ::bi Paddadr id ofs rd rd ::bi k.
+
+(** Translate a shifted operand *)
+
+Definition transl_shift (s: Op.shift) (a: int): Asm.shift_op :=
+ match s with
+ | Slsl => SOlsl a
+ | Slsr => SOlsr a
+ | Sasr => SOasr a
+ | Sror => SOror a
+ end.
+
+(** Translation of a condition. Prepends to [k] the instructions
+ that evaluate the condition and leave its boolean result in one of
+ the bits of the condition register. The bit in question is
+ determined by the [crbit_for_cond] function. *)
+
+Definition transl_cond
+ (cond: condition) (args: list mreg) (k: bcode) :=
+ match cond, args with
+ | (Ccomp c | Ccompu c), a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pcmp W SOnone r1 r2 ::bi k)
+ | (Ccompshift c s a | Ccompushift c s a), a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pcmp W (transl_shift s a) r1 r2 ::bi k)
+ | (Ccompimm c n | Ccompuimm c n), a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if is_arith_imm32 n then
+ Pcmpimm W (Int.unsigned n) r1 ::bi k
+ else if is_arith_imm32 (Int.neg n) then
+ Pcmnimm W (Int.unsigned (Int.neg n)) r1 ::bi k
+ else
+ loadimm32 X16 n (Pcmp W SOnone r1 X16 ::bi k))
+ | (Cmaskzero n | Cmasknotzero n), a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if is_logical_imm32 n then
+ Ptstimm W (Int.unsigned n) r1 ::bi k
+ else
+ loadimm32 X16 n (Ptst W SOnone r1 X16 ::bi k))
+ | (Ccompl c | Ccomplu c), a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pcmp X SOnone r1 r2 ::bi k)
+ | (Ccomplshift c s a | Ccomplushift c s a), a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pcmp X (transl_shift s a) r1 r2 ::bi k)
+ | (Ccomplimm c n | Ccompluimm c n), a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if is_arith_imm64 n then
+ Pcmpimm X (Int64.unsigned n) r1 ::bi k
+ else if is_arith_imm64 (Int64.neg n) then
+ Pcmnimm X (Int64.unsigned (Int64.neg n)) r1 ::bi k
+ else
+ loadimm64 X16 n (Pcmp X SOnone r1 X16 ::bi k))
+ | (Cmasklzero n | Cmasklnotzero n), a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if is_logical_imm64 n then
+ Ptstimm X (Int64.unsigned n) r1 ::bi k
+ else
+ loadimm64 X16 n (Ptst X SOnone r1 X16 ::bi k))
+ | Ccompf cmp, a1 :: a2 :: nil =>
+ do r1 <- freg_of a1; do r2 <- freg_of a2;
+ OK (Pfcmp D r1 r2 ::bi k)
+ | Cnotcompf cmp, a1 :: a2 :: nil =>
+ do r1 <- freg_of a1; do r2 <- freg_of a2;
+ OK (Pfcmp D r1 r2 ::bi k)
+ | Ccompfzero cmp, a1 :: nil =>
+ do r1 <- freg_of a1;
+ OK (Pfcmp0 D r1 ::bi k)
+ | Cnotcompfzero cmp, a1 :: nil =>
+ do r1 <- freg_of a1;
+ OK (Pfcmp0 D r1 ::bi k)
+ | Ccompfs cmp, a1 :: a2 :: nil =>
+ do r1 <- freg_of a1; do r2 <- freg_of a2;
+ OK (Pfcmp S r1 r2 ::bi k)
+ | Cnotcompfs cmp, a1 :: a2 :: nil =>
+ do r1 <- freg_of a1; do r2 <- freg_of a2;
+ OK (Pfcmp S r1 r2 ::bi k)
+ | Ccompfszero cmp, a1 :: nil =>
+ do r1 <- freg_of a1;
+ OK (Pfcmp0 S r1 ::bi k)
+ | Cnotcompfszero cmp, a1 :: nil =>
+ do r1 <- freg_of a1;
+ OK (Pfcmp0 S r1 ::bi k)
+ | _, _ =>
+ Error(msg "Asmgenblock.transl_cond")
+ end.
+
+Definition cond_for_signed_cmp (cmp: comparison) :=
+ match cmp with
+ | Ceq => TCeq
+ | Cne => TCne
+ | Clt => TClt
+ | Cle => TCle
+ | Cgt => TCgt
+ | Cge => TCge
+ end.
+
+Definition cond_for_unsigned_cmp (cmp: comparison) :=
+ match cmp with
+ | Ceq => TCeq
+ | Cne => TCne
+ | Clt => TClo
+ | Cle => TCls
+ | Cgt => TChi
+ | Cge => TChs
+ end.
+
+Definition cond_for_float_cmp (cmp: comparison) :=
+ match cmp with
+ | Ceq => TCeq
+ | Cne => TCne
+ | Clt => TCmi
+ | Cle => TCls
+ | Cgt => TCgt
+ | Cge => TCge
+ end.
+
+Definition cond_for_float_not_cmp (cmp: comparison) :=
+ match cmp with
+ | Ceq => TCne
+ | Cne => TCeq
+ | Clt => TCpl
+ | Cle => TChi
+ | Cgt => TCle
+ | Cge => TClt
+ end.
+
+Definition cond_for_cond (cond: condition) :=
+ match cond with
+ | Ccomp cmp => cond_for_signed_cmp cmp
+ | Ccompu cmp => cond_for_unsigned_cmp cmp
+ | Ccompshift cmp s a => cond_for_signed_cmp cmp
+ | Ccompushift cmp s a => cond_for_unsigned_cmp cmp
+ | Ccompimm cmp n => cond_for_signed_cmp cmp
+ | Ccompuimm cmp n => cond_for_unsigned_cmp cmp
+ | Cmaskzero n => TCeq
+ | Cmasknotzero n => TCne
+ | Ccompl cmp => cond_for_signed_cmp cmp
+ | Ccomplu cmp => cond_for_unsigned_cmp cmp
+ | Ccomplshift cmp s a => cond_for_signed_cmp cmp
+ | Ccomplushift cmp s a => cond_for_unsigned_cmp cmp
+ | Ccomplimm cmp n => cond_for_signed_cmp cmp
+ | Ccompluimm cmp n => cond_for_unsigned_cmp cmp
+ | Cmasklzero n => TCeq
+ | Cmasklnotzero n => TCne
+ | Ccompf cmp => cond_for_float_cmp cmp
+ | Cnotcompf cmp => cond_for_float_not_cmp cmp
+ | Ccompfzero cmp => cond_for_float_cmp cmp
+ | Cnotcompfzero cmp => cond_for_float_not_cmp cmp
+ | Ccompfs cmp => cond_for_float_cmp cmp
+ | Cnotcompfs cmp => cond_for_float_not_cmp cmp
+ | Ccompfszero cmp => cond_for_float_cmp cmp
+ | Cnotcompfszero cmp => cond_for_float_not_cmp cmp
+ end.
+
+(** Translation of a conditional branch. Prepends to [k] the instructions
+ that evaluate the condition and ranch to [lbl] if it holds.
+ We recognize some conditional branches that can be implemented
+ without setting then testing condition flags. *)
+
+Definition transl_cond_branch_default
+ (c: condition) (args: list mreg) (lbl: label) (k: bcode) : res (bcode*cf_instruction) :=
+ do ccode <- transl_cond c args k;
+ OK(ccode, Pbc (cond_for_cond c) lbl).
+
+Definition transl_cond_branch
+ (c: condition) (args: list mreg) (lbl: label) (k: bcode) : res (bcode*cf_instruction) :=
+ match args, c with
+ | a1 :: nil, (Ccompimm Cne n | Ccompuimm Cne n) =>
+ if Int.eq n Int.zero
+ then (do r1 <- ireg_of a1; OK (k, Pcbnz W r1 lbl))
+ else transl_cond_branch_default c args lbl k
+ | a1 :: nil, (Ccompimm Ceq n | Ccompuimm Ceq n) =>
+ if Int.eq n Int.zero
+ then (do r1 <- ireg_of a1; OK (k, Pcbz W r1 lbl))
+ else transl_cond_branch_default c args lbl k
+ | a1 :: nil, (Ccomplimm Cne n | Ccompluimm Cne n) =>
+ if Int64.eq n Int64.zero
+ then (do r1 <- ireg_of a1; OK (k, Pcbnz X r1 lbl))
+ else transl_cond_branch_default c args lbl k
+ | a1 :: nil, (Ccomplimm Ceq n | Ccompluimm Ceq n) =>
+ if Int64.eq n Int64.zero
+ then (do r1 <- ireg_of a1; OK (k, Pcbz X r1 lbl))
+ else transl_cond_branch_default c args lbl k
+ | a1 :: nil, Cmaskzero n =>
+ match Int.is_power2 n with
+ | Some bit => do r1 <- ireg_of a1; OK (k, Ptbz W r1 bit lbl)
+ | None => transl_cond_branch_default c args lbl k
+ end
+ | a1 :: nil, Cmasknotzero n =>
+ match Int.is_power2 n with
+ | Some bit => do r1 <- ireg_of a1; OK (k, Ptbnz W r1 bit lbl)
+ | None => transl_cond_branch_default c args lbl k
+ end
+ | a1 :: nil, Cmasklzero n =>
+ match Int64.is_power2' n with
+ | Some bit => do r1 <- ireg_of a1; OK (k, Ptbz X r1 bit lbl)
+ | None => transl_cond_branch_default c args lbl k
+ end
+ | a1 :: nil, Cmasklnotzero n =>
+ match Int64.is_power2' n with
+ | Some bit => do r1 <- ireg_of a1; OK (k, Ptbnz X r1 bit lbl)
+ | None => transl_cond_branch_default c args lbl k
+ end
+ | _, _ =>
+ transl_cond_branch_default c args lbl k
+ end.
+
+(** Translation of the arithmetic operation [r <- op(args)].
+ The corresponding instructions are prepended to [k]. *)
+
+Definition transl_op
+ (op: operation) (args: list mreg) (res: mreg) (k: bcode) :=
+ match op, args with
+ | Omove, a1 :: nil =>
+ match preg_of res, preg_of a1 with
+ | IR r, IR a => OK (Pmov r a ::bi k)
+ | FR r, FR a => OK (Pfmov r a ::bi k)
+ | _ , _ => Error(msg "Asmgenblock.Omove")
+ end
+ | Ointconst n, nil =>
+ do rd <- ireg_of res;
+ OK (loadimm32 rd n k)
+ | Olongconst n, nil =>
+ do rd <- ireg_of res;
+ OK (loadimm64 rd n k)
+ | Ofloatconst f, nil =>
+ do rd <- freg_of res;
+ OK (if Float.eq_dec f Float.zero
+ then Pfmovi D rd XZR ::bi k
+ else Pfmovimmd f rd ::bi k)
+ | Osingleconst f, nil =>
+ do rd <- freg_of res;
+ OK (if Float32.eq_dec f Float32.zero
+ then Pfmovi S rd XZR ::bi k
+ else Pfmovimms f rd ::bi k)
+ | Oaddrsymbol id ofs, nil =>
+ do rd <- ireg_of res;
+ OK (loadsymbol rd id ofs k)
+ | Oaddrstack ofs, nil =>
+ do rd <- ireg_of res;
+ OK (addimm64 rd XSP (Ptrofs.to_int64 ofs) k)
+ (** 32-bit integer arithmetic *)
+ | Oshift s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porr W (transl_shift s a) rd XZR r1 ::bi k)
+ | Oadd, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Padd W SOnone rd r1 r2 ::bi k)
+ | Oaddshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Padd W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oaddimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (addimm32 rd rs n k)
+ | Oneg, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psub W SOnone rd XZR r1 ::bi k)
+ | Onegshift s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psub W (transl_shift s a) rd XZR r1 ::bi k)
+ | Osub, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psub W SOnone rd r1 r2 ::bi k)
+ | Osubshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psub W (transl_shift s a) rd r1 r2 ::bi k)
+ | Omul, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pmadd W rd rs1 rs2 XZR ::bi k)
+ | Omuladd, a1 :: a2 :: a3 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
+ OK (Pmadd W rd r2 r3 r1 ::bi k)
+ | Omulsub, a1 :: a2 :: a3 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
+ OK (Pmsub W rd r2 r3 r1 ::bi k)
+ | Odiv, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psdiv W rd r1 r2 ::bi k)
+ | Odivu, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pudiv W rd r1 r2 ::bi k)
+ | Oand, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pand W SOnone rd r1 r2 ::bi k)
+ | Oandshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pand W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oandimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm32 (Pandimm W) (Pand W) rd r1 n k)
+ | Oor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porr W SOnone rd r1 r2 ::bi k)
+ | Oorshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porr W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm32 (Porrimm W) (Porr W) rd r1 n k)
+ | Oxor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peor W SOnone rd r1 r2 ::bi k)
+ | Oxorshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peor W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oxorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm32 (Peorimm W) (Peor W) rd r1 n k)
+ | Onot, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porn W SOnone rd XZR r1 ::bi k)
+ | Onotshift s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porn W (transl_shift s a) rd XZR r1 ::bi k)
+ | Obic, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pbic W SOnone rd r1 r2 ::bi k)
+ | Obicshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pbic W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oorn, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porn W SOnone rd r1 r2 ::bi k)
+ | Oornshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porn W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oeqv, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peon W SOnone rd r1 r2 ::bi k)
+ | Oeqvshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peon W (transl_shift s a) rd r1 r2 ::bi k)
+ | Oshl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Plslv W rd r1 r2 ::bi k)
+ | Oshr, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pasrv W rd r1 r2 ::bi k)
+ | Oshru, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Plsrv W rd r1 r2 ::bi k)
+ | Oshrximm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (shrx32 rd r1 n k)
+ | Ozext s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfiz W Int.zero s rd r1 ::bi k)
+ | Osext s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfiz W Int.zero s rd r1 ::bi k)
+ | Oshlzext s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfiz W a (Z.min s (Int.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Oshlsext s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfiz W a (Z.min s (Int.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Ozextshr a s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfx W a (Z.min s (Int.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Osextshr a s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfx W a (Z.min s (Int.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ (** 64-bit integer arithmetic *)
+ | Oshiftl s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porr X (transl_shift s a) rd XZR r1 ::bi k)
+ | Oextend x a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (move_extended rd r1 x a k)
+ (* [Omakelong] and [Ohighlong] should not occur *)
+ | Olowlong, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ assertion (ireg_eq rd r1);
+ OK (Pcvtx2w rd ::bi k)
+ | Oaddl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Padd X SOnone rd r1 r2 ::bi k)
+ | Oaddlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Padd X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oaddlext x a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (arith_extended Paddext (Padd X) rd r1 r2 x a k)
+ | Oaddlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (addimm64 rd r1 n k)
+ | Onegl, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psub X SOnone rd XZR r1 ::bi k)
+ | Oneglshift s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psub X (transl_shift s a) rd XZR r1 ::bi k)
+ | Osubl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psub X SOnone rd r1 r2 ::bi k)
+ | Osublshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psub X (transl_shift s a) rd r1 r2 ::bi k)
+ | Osublext x a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (arith_extended Psubext (Psub X) rd r1 r2 x a k)
+ | Omull, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pmadd X rd r1 r2 XZR ::bi k)
+ | Omulladd, a1 :: a2 :: a3 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
+ OK (Pmadd X rd r2 r3 r1 ::bi k)
+ | Omullsub, a1 :: a2 :: a3 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
+ OK (Pmsub X rd r2 r3 r1 ::bi k)
+ | Omullhs, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psmulh rd r1 r2 ::bi k)
+ | Omullhu, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pumulh rd r1 r2 ::bi k)
+ | Odivl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Psdiv X rd r1 r2 ::bi k)
+ | Odivlu, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pudiv X rd r1 r2 ::bi k)
+ | Oandl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pand X SOnone rd r1 r2 ::bi k)
+ | Oandlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pand X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oandlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm64 (Pandimm X) (Pand X) rd r1 n k)
+ | Oorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porr X SOnone rd r1 r2 ::bi k)
+ | Oorlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porr X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm64 (Porrimm X) (Porr X) rd r1 n k)
+ | Oxorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peor X SOnone rd r1 r2 ::bi k)
+ | Oxorlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peor X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oxorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (logicalimm64 (Peorimm X) (Peor X) rd r1 n k)
+ | Onotl, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porn X SOnone rd XZR r1 ::bi k)
+ | Onotlshift s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Porn X (transl_shift s a) rd XZR r1 ::bi k)
+ | Obicl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pbic X SOnone rd r1 r2 ::bi k)
+ | Obiclshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pbic X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oornl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porn X SOnone rd r1 r2 ::bi k)
+ | Oornlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Porn X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oeqvl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peon X SOnone rd r1 r2 ::bi k)
+ | Oeqvlshift s a, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Peon X (transl_shift s a) rd r1 r2 ::bi k)
+ | Oshll, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Plslv X rd r1 r2 ::bi k)
+ | Oshrl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Pasrv X rd r1 r2 ::bi k)
+ | Oshrlu, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (Plsrv X rd r1 r2 ::bi k)
+ | Oshrlximm n, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (shrx64 rd r1 n k)
+ | Ozextl s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfiz X Int.zero s rd r1 ::bi k)
+ | Osextl s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfiz X Int.zero s rd r1 ::bi k)
+ | Oshllzext s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfiz X a (Z.min s (Int64.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Oshllsext s a, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfiz X a (Z.min s (Int64.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Ozextshrl a s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Pubfx X a (Z.min s (Int64.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ | Osextshrl a s, a1 :: nil =>
+ do rd <- ireg_of res; do r1 <- ireg_of a1;
+ OK (Psbfx X a (Z.min s (Int64.zwordsize - Int.unsigned a)) rd r1 ::bi k)
+ (** 64-bit floating-point arithmetic *)
+ | Onegf, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfneg D rd rs ::bi k)
+ | Oabsf, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfabs D rd rs ::bi k)
+ | Oaddf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfadd D rd rs1 rs2 ::bi k)
+ | Osubf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfsub D rd rs1 rs2 ::bi k)
+ | Omulf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmul D rd rs1 rs2 ::bi k)
+ | Odivf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfdiv D rd rs1 rs2 ::bi k)
+ (** 32-bit floating-point arithmetic *)
+ | Onegfs, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfneg S rd rs ::bi k)
+ | Oabsfs, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfabs S rd rs ::bi k)
+ | Oaddfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfadd S rd rs1 rs2 ::bi k)
+ | Osubfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfsub S rd rs1 rs2 ::bi k)
+ | Omulfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmul S rd rs1 rs2 ::bi k)
+ | Odivfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfdiv S rd rs1 rs2 ::bi k)
+ | Osingleoffloat, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfcvtsd rd rs ::bi k)
+ | Ofloatofsingle, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfcvtds rd rs ::bi k)
+ (** Conversions between int and float *)
+ | Ointoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzs W D rd rs ::bi k)
+ | Ointuoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzu W D rd rs ::bi k)
+ | Ofloatofint, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pscvtf D W rd rs ::bi k)
+ | Ofloatofintu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pucvtf D W rd rs ::bi k)
+ | Ointofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzs W S rd rs ::bi k)
+ | Ointuofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzu W S rd rs ::bi k)
+ | Osingleofint, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pscvtf S W rd rs ::bi k)
+ | Osingleofintu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pucvtf S W rd rs ::bi k)
+ | Olongoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzs X D rd rs ::bi k)
+ | Olonguoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzu X D rd rs ::bi k)
+ | Ofloatoflong, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pscvtf D X rd rs ::bi k)
+ | Ofloatoflongu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pucvtf D X rd rs ::bi k)
+ | Olongofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzs X S rd rs ::bi k)
+ | Olonguofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfcvtzu X S rd rs ::bi k)
+ | Osingleoflong, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pscvtf S X rd rs ::bi k)
+ | Osingleoflongu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pucvtf S X rd rs ::bi k)
+ (** Boolean tests *)
+ | Ocmp c, _ =>
+ do rd <- ireg_of res;
+ transl_cond c args (Pcset rd (cond_for_cond c) ::bi k)
+ (** Conditional move *)
+ | Osel cmp ty, a1 :: a2 :: args =>
+ match preg_of res with
+ | IR r =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ transl_cond cmp args (Pcsel r r1 r2 (cond_for_cond cmp) ::bi k)
+ | FR r =>
+ do r1 <- freg_of a1; do r2 <- freg_of a2;
+ transl_cond cmp args (Pcsel r r1 r2 (cond_for_cond cmp) ::bi k)
+ | _ =>
+ Error(msg "Asmgenblock.Osel")
+ end
+ | _, _ => Error(msg "Asmgenblock.transl_op")
+ end.
+
+(** Translation of addressing modes *)
+
+Definition transl_addressing (sz: Z) (addr: Op.addressing) (args: list mreg)
+ (insn: Asm.addressing -> basic) (k: bcode) : res bcode :=
+ match addr, args with
+ | Aindexed ofs, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ if offset_representable sz ofs then
+ OK (insn (ADimm r1 ofs) ::bi k)
+ else
+ OK (loadimm64 X16 ofs (insn (ADreg r1 X16) ::bi k))
+ | Aindexed2, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (insn (ADreg r1 r2) ::bi k)
+ | Aindexed2shift a, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ if Int.eq a Int.zero then
+ OK (insn (ADreg r1 r2) ::bi k)
+ else if Int.eq (Int.shl Int.one a) (Int.repr sz) then
+ OK (insn (ADlsl r1 r2 a) ::bi k)
+ else
+ OK (Padd X (SOlsl a) X16 r1 r2 ::bi insn (ADimm X16 Int64.zero) ::bi k)
+ | Aindexed2ext x a, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ if Int.eq a Int.zero || Int.eq (Int.shl Int.one a) (Int.repr sz) then
+ OK (insn (match x with Xsgn32 => ADsxt r1 r2 a
+ | Xuns32 => ADuxt r1 r2 a end) ::bi k)
+ else
+ OK (arith_extended Paddext (Padd X) X16 r1 r2 x a
+ (insn (ADimm X16 Int64.zero) ::bi k))
+ | Aglobal id ofs, nil =>
+ assertion (negb (Archi.pic_code tt));
+ if Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero && symbol_is_aligned id sz
+ then OK (Padrp id ofs X16 ::bi insn (ADadr X16 id ofs) ::bi k)
+ else OK (loadsymbol X16 id ofs (insn (ADimm X16 Int64.zero) ::bi k))
+ | Ainstack ofs, nil =>
+ let ofs := Ptrofs.to_int64 ofs in
+ if offset_representable sz ofs then
+ OK (insn (ADimm XSP ofs) ::bi k)
+ else
+ OK (loadimm64 X16 ofs (insn (ADreg XSP X16) ::bi k))
+ | _, _ =>
+ Error(msg "Asmgen.transl_addressing")
+ end.
+
+(** Translation of loads and stores *)
+
+Definition transl_load (chunk: memory_chunk) (addr: Op.addressing)
+ (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
+ match chunk with
+ | Mint8unsigned =>
+ do rd <- ireg_of dst; transl_addressing 1 addr args (PLd_rd_a (Pldrb W) rd) k
+ | Mint8signed =>
+ do rd <- ireg_of dst; transl_addressing 1 addr args (PLd_rd_a (Pldrsb W) rd) k
+ | Mint16unsigned =>
+ do rd <- ireg_of dst; transl_addressing 2 addr args (PLd_rd_a (Pldrh W) rd) k
+ | Mint16signed =>
+ do rd <- ireg_of dst; transl_addressing 2 addr args (PLd_rd_a (Pldrsh W) rd) k
+ | Mint32 =>
+ do rd <- ireg_of dst; transl_addressing 4 addr args (PLd_rd_a Pldrw rd) k
+ | Mint64 =>
+ do rd <- ireg_of dst; transl_addressing 8 addr args (PLd_rd_a Pldrx rd) k
+ | Mfloat32 =>
+ do rd <- freg_of dst; transl_addressing 4 addr args (PLd_rd_a Pldrs rd) k
+ | Mfloat64 =>
+ do rd <- freg_of dst; transl_addressing 8 addr args (PLd_rd_a Pldrd rd) k
+ | Many32 =>
+ do rd <- ireg_of dst; transl_addressing 4 addr args (PLd_rd_a Pldrw_a rd) k
+ | Many64 =>
+ do rd <- ireg_of dst; transl_addressing 8 addr args (PLd_rd_a Pldrx_a rd) k
+ end.
+
+Definition transl_store (chunk: memory_chunk) (addr: Op.addressing)
+ (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
+ match chunk with
+ | Mint8unsigned | Mint8signed =>
+ do r1 <- ireg_of src; transl_addressing 1 addr args (PSt_rs_a Pstrb r1) k
+ | Mint16unsigned | Mint16signed =>
+ do r1 <- ireg_of src; transl_addressing 2 addr args (PSt_rs_a Pstrh r1) k
+ | Mint32 =>
+ do r1 <- ireg_of src; transl_addressing 4 addr args (PSt_rs_a Pstrw r1) k
+ | Mint64 =>
+ do r1 <- ireg_of src; transl_addressing 8 addr args (PSt_rs_a Pstrx r1) k
+ | Mfloat32 =>
+ do r1 <- freg_of src; transl_addressing 4 addr args (PSt_rs_a Pstrs r1) k
+ | Mfloat64 =>
+ do r1 <- freg_of src; transl_addressing 8 addr args (PSt_rs_a Pstrd r1) k
+ | Many32 =>
+ do r1 <- ireg_of src; transl_addressing 4 addr args (PSt_rs_a Pstrw_a r1) k
+ | Many64 =>
+ do r1 <- ireg_of src; transl_addressing 8 addr args (PSt_rs_a Pstrx_a r1) k
+ end.
+
+(** Translation of a Machblock instruction. *)
+
+Definition transl_instr_basic (f: Machblock.function) (i: Machblock.basic_inst)
+ (ep: bool) (k: bcode) :=
+ match i with
+ | MBgetstack ofs ty dst =>
+ loadind XSP ofs ty dst k
+ | MBsetstack src ofs ty =>
+ storeind src XSP ofs ty k
+ | MBgetparam ofs ty dst =>
+ do c <- loadind X29 ofs ty dst k;
+ OK (if ep then c else loadptr_bc XSP f.(fn_link_ofs) X29 c)
+ | MBop op args res =>
+ transl_op op args res k
+ | MBload t chunk addr args dst =>
+ match t with
+ | TRAP => transl_load chunk addr args dst k
+ | NOTRAP => Error(msg "Asmgenblock.transl_instr_basic: NOTRAP load not supported in aarch64.")
+ end
+ | MBstore chunk addr args src =>
+ transl_store chunk addr args src k
+ end.
+
+(** Translation of a code sequence *)
+
+Definition it1_is_parent (before: bool) (i: Machblock.basic_inst) : bool :=
+ match i with
+ (*| MBgetstack ofs ty dst => before && negb (mreg_eq dst R29)*)
+ | MBsetstack src ofs ty => before
+ | MBgetparam ofs ty dst => negb (mreg_eq dst R29)
+ | MBop op args res => before && negb (mreg_eq res R29)
+ (*| MBload trapping_mode chunk addr args dst => before && negb (mreg_eq dst R29)*)
+ | _ => false
+ end.
+
+Fixpoint transl_basic_code (f: Machblock.function) (il: list Machblock.basic_inst) (it1p: bool) :=
+ match il with
+ | nil => OK (nil)
+ | i1 :: il' =>
+ do k1 <- transl_basic_code f il' (it1_is_parent it1p i1);
+ transl_instr_basic f i1 it1p k1
+ end.
+
+Program Definition cons_bblocks (ll: list label) (bdy: list basic) (ex: option control): bblocks :=
+ match ex with
+ | None =>
+ match bdy with
+ | nil => {| header := ll; body:= Pnop::nil; exit := None |} :: nil
+ | _ => {| header := ll; body:= bdy; exit := None |} :: nil
+ end
+ | _ =>
+ match bdy with
+ | nil => {| header := ll; body:= nil; exit := ex |} :: nil
+ | _ => {| header := ll; body:= bdy; exit := ex |} :: nil
+ end
+ end.
+Next Obligation.
+ induction bdy. congruence.
+ simpl. auto.
+Qed.
+Next Obligation.
+ destruct ex. simpl. auto.
+ congruence.
+Qed.
+Next Obligation.
+ induction bdy. congruence.
+ simpl. auto.
+Qed.
+
+Definition transl_control (f: Machblock.function) (ctl: control_flow_inst) : res (bcode*control) :=
+ match ctl with
+ | MBcall sig (inl r) => do r1 <- ireg_of r;
+ OK (nil, PCtlFlow (Pblr r1 sig))
+ | MBcall sig (inr symb) => OK (nil, PCtlFlow (Pbl symb sig))
+ | MBtailcall sig (inr symb) => OK(make_epilogue f, PCtlFlow (Pbs symb sig))
+ | MBtailcall sig (inl r) => do r1 <- ireg_of r;
+ OK (make_epilogue f, PCtlFlow (Pbr r1 sig))
+ | MBbuiltin ef args res => OK (nil, Pbuiltin ef (List.map (map_builtin_arg dreg_of) args) (map_builtin_res dreg_of res))
+ | MBgoto lbl => OK (nil, PCtlFlow (Pb lbl))
+ | MBcond cond args lbl => do (bc, c) <- transl_cond_branch cond args lbl nil; OK (bc, PCtlFlow c)
+ | MBreturn => OK (make_epilogue f, PCtlFlow (Pret RA))
+ | MBjumptable arg tbl => do r <- ireg_of arg;
+ OK (nil, PCtlFlow (Pbtbl r tbl))
+ end.
+
+Definition transl_exit (f: Machblock.function) (ext: option control_flow_inst) : res (bcode*option control) :=
+ match ext with
+ Some ctl => do (b,c) <- transl_control f ctl; OK (b, Some c)
+ | None => OK (nil, None)
+ end.
+
+Definition transl_block (f: Machblock.function) (fb: Machblock.bblock) (ep: bool) : res (list bblock) :=
+ do (bdy2, ex) <- transl_exit f fb.(Machblock.exit);
+ do bdy1 <- transl_basic_code f fb.(Machblock.body) ep;
+ OK (cons_bblocks fb.(Machblock.header) (bdy1 @@ bdy2) ex)
+ .
+
+Fixpoint transl_blocks (f: Machblock.function) (lmb: list Machblock.bblock) (ep: bool) :=
+ match lmb with
+ | nil => OK nil
+ | mb :: lmb =>
+ do lb <- transl_block f mb (if Machblock.header mb then ep else false);
+ do lb' <- transl_blocks f lmb false;
+ OK (lb @@ lb')
+ end
+.
+
+Program Definition make_prologue (f: Machblock.function) (k:bblocks) :=
+ {| header := nil; body := Pallocframe f.(fn_stacksize) f.(fn_link_ofs) ::bi
+ ((PSt_rs_a Pstrx RA) (ADimm XSP (Ptrofs.to_int64 (f.(fn_retaddr_ofs))))) ::bi nil;
+ exit := None |} :: k.
+
+Definition transl_function (f: Machblock.function) : res Asmblock.function :=
+ do lb <- transl_blocks f f.(Machblock.fn_code) true;
+ OK (mkfunction f.(Machblock.fn_sig)
+ (make_prologue f lb)).
+
+Definition transf_function (f: Machblock.function) : res Asmblock.function :=
+ do tf <- transl_function f;
+ if zlt Ptrofs.max_unsigned (size_blocks tf.(fn_blocks))
+ then Error (msg "code size exceeded")
+ else OK tf.
+
+Definition transf_fundef (f: Machblock.fundef) : res Asmblock.fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: Machblock.program) : res Asmblock.program :=
+ transform_partial_program transf_fundef p.
+
diff --git a/aarch64/Asmblockgenproof.v b/aarch64/Asmblockgenproof.v
new file mode 100644
index 00000000..11219928
--- /dev/null
+++ b/aarch64/Asmblockgenproof.v
@@ -0,0 +1,1547 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Errors.
+Require Import Integers Floats AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations Machblock Conventions Asmblock IterList.
+Require Import Asmblockgen Asmblockgenproof0 Asmblockgenproof1 Asmblockprops.
+Require Import Lia.
+
+Module MB := Machblock.
+Module AB := Asmblock.
+
+Definition match_prog (p: MB.program) (tp: AB.program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros. eapply match_transform_partial_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variable lk: aarch64_linker.
+Variable prog: Machblock.program.
+Variable tprog: Asmblock.program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma symbols_preserved:
+ forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof (Genv.find_symbol_match TRANSF).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSF).
+
+Lemma functions_translated:
+ forall b f,
+ Genv.find_funct_ptr ge b = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge b = Some tf /\ transf_fundef f = OK tf.
+Proof (Genv.find_funct_ptr_transf_partial TRANSF).
+
+Lemma functions_transl:
+ forall fb f tf,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transf_function f = OK tf ->
+ Genv.find_funct_ptr tge fb = Some (Internal tf).
+Proof.
+ intros. exploit functions_translated; eauto. intros [tf' [A B]].
+ monadInv B. rewrite H0 in EQ; inv EQ; auto.
+Qed.
+
+Lemma transf_function_no_overflow:
+ forall f tf,
+ transf_function f = OK tf -> size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned.
+Proof.
+ intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (size_blocks x.(fn_blocks))); inv EQ0.
+ lia.
+Qed.
+
+Hypothesis symbol_high_low: forall (id: ident) (ofs: ptrofs),
+ Val.addl (symbol_high lk id ofs) (symbol_low lk id ofs) = Genv.symbol_address tge id ofs.
+
+(** * Proof of semantic preservation *)
+
+(** Semantic preservation is proved using a complex simulation diagram
+ of the following form.
+<<
+ MB.step
+ ---------------------------------------->
+ header body exit
+ st1 -----> st2 -----> st3 ------------------> st4
+ | | | |
+ | (A) | (B) | (C) |
+ match_codestate | | | |
+ | header | body1 | body2 | match_states
+ cs1 -----> cs2 -----> cs3 ------> cs4 |
+ | / \ exit |
+ match_asmstate | --------------- --->--- |
+ | / match_asmstate \ |
+ st'1 ---------------------------------------> st'2
+ AB.step *
+>>
+ The invariant between each MB.step/AB.step is the [match_states] predicate below.
+ However, we also need to introduce an intermediary state [Codestate] which allows
+ us to reason on a finer grain, executing header, body and exit separately.
+
+ This [Codestate] consists in a state like [Asmblock.State], except that the
+ code is directly stored in the state, much like [Machblock.State]. It also features
+ additional useful elements to keep track of while executing a bblock.
+*)
+
+Inductive match_states: Machblock.state -> Asm.state -> Prop :=
+ | match_states_intro:
+ forall s fb sp c ep ms m m' rs f tf tc
+ (STACKS: match_stack ge s)
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (MEXT: Mem.extends m m')
+ (AT: transl_code_at_pc ge (rs PC) fb f c ep tf tc)
+ (AG: agree ms sp rs)
+ (DXP: ep = true -> rs#X29 = parent_sp s),
+ match_states (Machblock.State s fb sp c ms m)
+ (Asm.State rs m')
+ | match_states_call:
+ forall s fb ms m m' rs
+ (STACKS: match_stack ge s)
+ (MEXT: Mem.extends m m')
+ (AG: agree ms (parent_sp s) rs)
+ (ATPC: rs PC = Vptr fb Ptrofs.zero)
+ (ATLR: rs RA = parent_ra s),
+ match_states (Machblock.Callstate s fb ms m)
+ (Asm.State rs m')
+ | match_states_return:
+ forall s ms m m' rs
+ (STACKS: match_stack ge s)
+ (MEXT: Mem.extends m m')
+ (AG: agree ms (parent_sp s) rs)
+ (ATPC: rs PC = parent_ra s),
+ match_states (Machblock.Returnstate s ms m)
+ (Asm.State rs m').
+
+Section TRANSL_LABEL. (* Lemmas on translation of MB.is_label into AB.is_label *)
+
+Lemma cons_bblocks_label:
+ forall hd bdy ex tbb tc,
+ cons_bblocks hd bdy ex = tbb::tc ->
+ header tbb = hd.
+Proof.
+ intros until tc. intros CONSB. unfold cons_bblocks in CONSB.
+ destruct ex; try destruct bdy; try destruct c; try destruct i.
+ all: inv CONSB; simpl; auto.
+Qed.
+
+Lemma cons_bblocks_label2:
+ forall hd bdy ex tbb1 tbb2,
+ cons_bblocks hd bdy ex = tbb1::tbb2::nil ->
+ header tbb2 = nil.
+Proof.
+ intros until tbb2. intros CONSB. unfold cons_bblocks in CONSB.
+ destruct ex; try destruct bdy; try destruct c; try destruct i.
+ all: inv CONSB; simpl; auto.
+Qed.
+
+Remark in_dec_transl:
+ forall lbl hd,
+ (if in_dec lbl hd then true else false) = (if MB.in_dec lbl hd then true else false).
+Proof.
+ intros. destruct (in_dec lbl hd), (MB.in_dec lbl hd). all: tauto.
+Qed.
+
+Lemma transl_is_label:
+ forall lbl bb tbb f ep tc,
+ transl_block f bb ep = OK (tbb::tc) ->
+ is_label lbl tbb = MB.is_label lbl bb.
+Proof.
+ intros until tc. intros TLB.
+ destruct tbb as [thd tbdy tex]; simpl in *.
+ monadInv TLB.
+ unfold is_label. simpl.
+ apply cons_bblocks_label in H0. simpl in H0. subst.
+ rewrite in_dec_transl. auto.
+Qed.
+
+Lemma transl_is_label_false2:
+ forall lbl bb f ep tbb1 tbb2,
+ transl_block f bb ep = OK (tbb1::tbb2::nil) ->
+ is_label lbl tbb2 = false.
+Proof.
+ intros until tbb2. intros TLB.
+ destruct tbb2 as [thd tbdy tex]; simpl in *.
+ monadInv TLB. apply cons_bblocks_label2 in H0. simpl in H0. subst.
+ apply is_label_correct_false. simpl. auto.
+Qed.
+
+Lemma transl_block_nonil:
+ forall f c ep tc,
+ transl_block f c ep = OK tc ->
+ tc <> nil.
+Proof.
+ intros. monadInv H. unfold cons_bblocks.
+ destruct x0; try destruct (x1 @@ x); try destruct c0; try destruct i.
+ all: discriminate.
+Qed.
+
+Lemma transl_block_limit: forall f bb ep tbb1 tbb2 tbb3 tc,
+ ~transl_block f bb ep = OK (tbb1 :: tbb2 :: tbb3 :: tc).
+Proof.
+ intros. intro. monadInv H.
+ unfold cons_bblocks in H0.
+ destruct x0; try destruct (x1 @@ x); try destruct c0; try destruct i.
+ all: discriminate.
+Qed.
+
+Lemma find_label_transl_false:
+ forall x f lbl bb ep x',
+ transl_block f bb ep = OK x ->
+ MB.is_label lbl bb = false ->
+ find_label lbl (x++x') = find_label lbl x'.
+Proof.
+ intros until x'. intros TLB MBis; simpl; auto.
+ destruct x as [|x0 x1]; simpl; auto.
+ destruct x1 as [|x1 x2]; simpl; auto.
+ - erewrite <- transl_is_label in MBis; eauto. rewrite MBis. auto.
+ - destruct x2 as [|x2 x3]; simpl; auto.
+ + erewrite <- transl_is_label in MBis; eauto. rewrite MBis.
+ erewrite transl_is_label_false2; eauto.
+ + apply transl_block_limit in TLB. destruct TLB.
+Qed.
+
+Lemma transl_blocks_label:
+ forall lbl f c tc ep,
+ transl_blocks f c ep = OK tc ->
+ match MB.find_label lbl c with
+ | None => find_label lbl tc = None
+ | Some c' => exists tc', find_label lbl tc = Some tc' /\ transl_blocks f c' false = OK tc'
+ end.
+Proof.
+ induction c; simpl; intros.
+ inv H. auto.
+ monadInv H.
+ destruct (MB.is_label lbl a) eqn:MBis.
+ - destruct x as [|tbb tc]. { apply transl_block_nonil in EQ. contradiction. }
+ simpl find_label. exploit transl_is_label; eauto. intros ABis. rewrite MBis in ABis.
+ rewrite ABis.
+ eexists. eexists. split; eauto. simpl transl_blocks.
+ assert (MB.header a <> nil).
+ { apply MB.is_label_correct_true in MBis.
+ destruct (MB.header a). contradiction. discriminate. }
+ destruct (MB.header a); try contradiction.
+ rewrite EQ. simpl. rewrite EQ1. simpl. auto.
+ - apply IHc in EQ1. destruct (MB.find_label lbl c).
+ + destruct EQ1 as (tc' & FIND & TLBS). exists tc'; eexists; auto.
+ erewrite find_label_transl_false; eauto.
+ + erewrite find_label_transl_false; eauto.
+Qed.
+
+Lemma find_label_nil:
+ forall bb lbl c,
+ header bb = nil ->
+ find_label lbl (bb::c) = find_label lbl c.
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *. subst.
+ assert (is_label lbl {| AB.header := nil; AB.body := bdy; AB.exit := ex; AB.correct := correct |} = false).
+ { erewrite <- is_label_correct_false. simpl. auto. }
+ rewrite H. auto.
+Qed.
+
+Theorem transl_find_label:
+ forall lbl f tf,
+ transf_function f = OK tf ->
+ match MB.find_label lbl f.(MB.fn_code) with
+ | None => find_label lbl tf.(fn_blocks) = None
+ | Some c => exists tc, find_label lbl tf.(fn_blocks) = Some tc /\ transl_blocks f c false = OK tc
+ end.
+Proof.
+ intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks x))); inv EQ0. clear g.
+ monadInv EQ. unfold make_prologue. simpl fn_blocks. repeat (rewrite find_label_nil); simpl; auto.
+ eapply transl_blocks_label; eauto.
+Qed.
+
+End TRANSL_LABEL.
+
+(** A valid branch in a piece of Machblock code translates to a valid ``go to''
+ transition in the generated Asmblock code. *)
+
+Lemma find_label_goto_label:
+ forall f tf lbl rs m c' b ofs,
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ transf_function f = OK tf ->
+ rs PC = Vptr b ofs ->
+ MB.find_label lbl f.(MB.fn_code) = Some c' ->
+ exists tc', exists rs',
+ goto_label tf lbl rs m = Next rs' m
+ /\ transl_code_at_pc ge (rs' PC) b f c' false tf tc'
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ intros. exploit (transl_find_label lbl f tf); eauto. rewrite H2.
+ intros (tc & A & B).
+ exploit label_pos_code_tail; eauto. instantiate (1 := 0).
+ intros [pos' [P [Q R]]].
+ exists tc; exists (rs#PC <- (Vptr b (Ptrofs.repr pos'))).
+ split. unfold goto_label. rewrite P. rewrite H1. auto.
+ split. rewrite Pregmap.gss. constructor; auto.
+ rewrite Ptrofs.unsigned_repr. replace (pos' - 0) with pos' in Q.
+ auto. lia.
+ generalize (transf_function_no_overflow _ _ H0). lia.
+ intros. apply Pregmap.gso; auto.
+Qed.
+
+(** Existence of return addresses *)
+
+Lemma return_address_exists:
+ forall b f c, is_tail (b :: c) f.(MB.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros. eapply Asmblockgenproof0.return_address_exists; eauto.
+
+- intros. monadInv H0.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks x.(fn_blocks))); inv EQ0. monadInv EQ. simpl.
+ exists x; exists true; split; auto.
+ repeat constructor.
+- exact transf_function_no_overflow.
+Qed.
+
+(* Useful for dealing with the many cases in some proofs *)
+Ltac exploreInst :=
+ repeat match goal with
+ | [ H : match ?var with | _ => _ end = _ |- _ ] => destruct var
+ | [ H : OK _ = OK _ |- _ ] => monadInv H
+ | [ |- context[if ?b then _ else _] ] => destruct b
+ | [ |- context[match ?m with | _ => _ end] ] => destruct m
+ | [ |- context[match ?m as _ return _ with | _ => _ end]] => destruct m
+ | [ H : bind _ _ = OK _ |- _ ] => monadInv H
+ | [ H : Error _ = OK _ |- _ ] => inversion H
+ end.
+
+(** Some translation properties *)
+
+Lemma transl_blocks_distrib:
+ forall c f bb tbb tc ep,
+ transl_blocks f (bb::c) ep = OK (tbb::tc)
+ -> transl_block f bb (if MB.header bb then ep else false) = OK (tbb :: nil)
+ /\ transl_blocks f c false = OK tc.
+Proof.
+ intros until ep. intros TLBS.
+ destruct bb as [hd bdy ex].
+ monadInv TLBS. monadInv EQ.
+ unfold transl_block.
+ rewrite EQ0; simpl.
+ simpl in EQ; rewrite EQ; simpl.
+ unfold cons_bblocks in *. simpl in EQ0.
+ destruct ex.
+ - simpl in *. monadInv EQ0.
+ destruct (x3 @@ x1) eqn: CBDY; inv H0; inv EQ1; auto.
+ - simpl in *. inv EQ0. destruct (x3 @@ nil) eqn: CBDY; inv H0; inv EQ1; auto.
+Qed.
+
+Lemma cons_bblocks_decomp:
+ forall thd tbdy tex tbb,
+ (tbdy <> nil \/ tex <> None) ->
+ cons_bblocks thd tbdy tex = tbb :: nil ->
+ header tbb = thd
+ /\ body tbb = tbdy
+ /\ exit tbb = tex.
+Proof.
+ intros until tbb. intros Hnonil CONSB. unfold cons_bblocks in CONSB.
+ destruct (tex) eqn:ECTL.
+ - destruct tbdy; inv CONSB; simpl; auto.
+ - inversion Hnonil.
+ + destruct tbdy as [|bi tbdy]; [ contradiction H; auto | inv CONSB; auto].
+ + contradict H; simpl; auto.
+Qed.
+
+Lemma transl_blocks_nonil:
+ forall f bb c tc ep,
+ transl_blocks f (bb::c) ep = OK tc ->
+ exists tbb tc', tc = tbb :: tc'.
+Proof.
+ intros until ep. intros TLBS. monadInv TLBS. monadInv EQ. unfold cons_bblocks.
+ destruct (x2);
+ destruct (x3 @@ x1); simpl; eauto.
+Qed.
+
+Definition mb_remove_header bb := {| MB.header := nil; MB.body := MB.body bb; MB.exit := MB.exit bb |}.
+
+Definition mb_remove_body (bb: MB.bblock) :=
+ {| MB.header := MB.header bb; MB.body := nil; MB.exit := MB.exit bb |}.
+
+Definition mbsize (bb: MB.bblock) := (length (MB.body bb) + length_opt (MB.exit bb))%nat.
+
+Lemma mbsize_eqz:
+ forall bb, mbsize bb = 0%nat -> MB.body bb = nil /\ MB.exit bb = None.
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *. unfold mbsize in H.
+ remember (length _) as a. remember (length_opt _) as b.
+ assert (a = 0%nat) by lia. assert (b = 0%nat) by lia. subst. clear H.
+ inv H0. inv H1. destruct bdy; destruct ex; auto.
+ all: try discriminate.
+Qed.
+
+Lemma mbsize_neqz:
+ forall bb, mbsize bb <> 0%nat -> (MB.body bb <> nil \/ MB.exit bb <> None).
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *.
+ destruct bdy; destruct ex; try (right; discriminate); try (left; discriminate).
+ contradict H. unfold mbsize. simpl. auto.
+Qed.
+
+Record codestate :=
+ Codestate { pstate: state; (**r projection to Asmblock.state *)
+ pheader: list label;
+ pbody1: list basic; (**r list of basic instructions coming from the translation of the Machblock body *)
+ pbody2: list basic; (**r list of basic instructions coming from the translation of the Machblock exit *)
+ pctl: option control; (**r exit instruction, coming from the translation of the Machblock exit *)
+ ep: bool; (**r reflects the [ep] variable used in the translation *)
+ rem: list AB.bblock; (**r remaining bblocks to execute *)
+ cur: bblock (**r current bblock to execute - to keep track of its size when incrementing PC *)
+ }.
+
+(* The part that deals with Machblock <-> Codestate agreement
+ * Note about DXP: the property of [ep] only matters if the current block doesn't have a header, hence the condition *)
+Inductive match_codestate fb: Machblock.state -> codestate -> Prop :=
+ | match_codestate_intro:
+ forall s sp ms m rs0 m0 f tc ep c bb tbb tbc1 tbc2 ex
+ (STACKS: match_stack ge s)
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (MEXT: Mem.extends m m0)
+ (TBC: transl_basic_code f (MB.body bb) (if MB.header bb then ep else false) = OK tbc1)
+ (TIC: transl_exit f (MB.exit bb) = OK (tbc2, ex))
+ (TBLS: transl_blocks f c false = OK tc)
+ (AG: agree ms sp rs0)
+ (DXP: (if MB.header bb then ep else false) = true -> rs0#X29 = parent_sp s)
+ ,
+ match_codestate fb (Machblock.State s fb sp (bb::c) ms m)
+ {| pstate := (Asm.State rs0 m0);
+ pheader := (MB.header bb);
+ pbody1 := tbc1;
+ pbody2 := tbc2;
+ pctl := ex;
+ ep := ep;
+ rem := tc;
+ cur := tbb
+ |}
+.
+
+(* The part ensuring that the code in Codestate actually resides at [rs PC] *)
+Inductive match_asmstate fb: codestate -> Asm.state -> Prop :=
+ | match_asmstate_some:
+ forall rs f tf tc m tbb ofs ep tbdy1 tbdy2 tex lhd
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (TRANSF: transf_function f = OK tf)
+ (PCeq: rs PC = Vptr fb ofs)
+ (TAIL: code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) (tbb::tc))
+ ,
+ match_asmstate fb
+ {| pstate := (Asm.State rs m);
+ pheader := lhd;
+ pbody1 := tbdy1;
+ pbody2 := tbdy2;
+ pctl := tex;
+ ep := ep;
+ rem := tc;
+ cur := tbb |}
+ (Asm.State rs m)
+.
+
+Lemma indexed_memory_access_nonil: forall f ofs r i k,
+ indexed_memory_access_bc f ofs r i k <> nil.
+Proof.
+ intros.
+ unfold indexed_memory_access_bc, loadimm64, loadimm, loadimm_z, loadimm_n;
+ desif; try congruence.
+ all: destruct decompose_int; try destruct p; try congruence.
+Qed.
+
+Lemma loadimm_nonil: forall sz x n k,
+ loadimm sz x n k <> nil.
+Proof.
+ intros.
+ unfold loadimm. desif;
+ unfold loadimm_n, loadimm_z.
+ all: destruct decompose_int; try destruct p; try congruence.
+Qed.
+
+Lemma loadimm32_nonil: forall sz x n,
+ loadimm32 sz x n <> nil.
+Proof.
+ intros.
+ unfold loadimm32. desif; try congruence.
+ apply loadimm_nonil.
+Qed.
+
+Lemma loadimm64_nonil: forall sz x n,
+ loadimm64 sz x n <> nil.
+Proof.
+ intros.
+ unfold loadimm64. desif; try congruence.
+ apply loadimm_nonil.
+Qed.
+
+Lemma loadsymbol_nonil: forall sz x n k,
+ loadsymbol sz x n k <> nil.
+Proof.
+ intros.
+ unfold loadsymbol. desif; try congruence.
+Qed.
+
+Lemma move_extended_nonil: forall x0 x1 x2 a k,
+ move_extended x1 x2 x0 a k <> nil.
+Proof.
+ intros. unfold move_extended, move_extended_base.
+ desif; try congruence.
+Qed.
+
+Lemma arith_extended_nonil: forall insnX insnS x0 x1 x2 x3 a k,
+ arith_extended insnX insnS x1 x2 x3 x0 a k <> nil.
+Proof.
+ intros. unfold arith_extended, move_extended_base.
+ desif; try congruence.
+Qed.
+
+Lemma transl_instr_basic_nonil:
+ forall k f bi ep x,
+ transl_instr_basic f bi ep k = OK x ->
+ x <> nil.
+Proof.
+ intros until x. intros TIB.
+ destruct bi.
+ - simpl in TIB. unfold loadind in TIB;
+ exploreInst; try discriminate; apply indexed_memory_access_nonil.
+ - simpl in TIB. unfold storeind in TIB;
+ exploreInst; try discriminate; apply indexed_memory_access_nonil.
+ - simpl in TIB. monadInv TIB. unfold loadind in EQ. exploreInst; try discriminate;
+ unfold loadptr_bc; apply indexed_memory_access_nonil.
+ - simpl in TIB. unfold transl_op in TIB. exploreInst; try discriminate;
+ unfold addimm32, addimm64, shrx32, shrx64,
+ logicalimm32, logicalimm64, addimm_aux.
+ all: desif; try congruence;
+ try apply loadimm32_nonil; try apply loadimm64_nonil; try apply loadsymbol_nonil;
+ try apply move_extended_nonil; try apply arith_extended_nonil.
+ all: unfold transl_cond in *; exploreInst; try discriminate;
+ try apply loadimm32_nonil; try apply loadimm64_nonil.
+ - simpl in TIB. unfold transl_load in TIB. exploreInst; try discriminate;
+ unfold transl_addressing in *; exploreInst; try discriminate.
+ all: try apply loadimm64_nonil; try apply arith_extended_nonil; try apply loadsymbol_nonil.
+ - simpl in TIB. unfold transl_store in TIB. exploreInst; try discriminate;
+ unfold transl_addressing in *; exploreInst; try discriminate.
+ all: try apply loadimm64_nonil; try apply arith_extended_nonil; try apply loadsymbol_nonil.
+Qed.
+
+Lemma transl_basic_code_nonil:
+ forall bdy f x ep,
+ bdy <> nil ->
+ transl_basic_code f bdy ep = OK x ->
+ x <> nil.
+Proof.
+ induction bdy as [|bi bdy].
+ intros. contradict H0; auto.
+ destruct bdy as [|bi2 bdy].
+ - clear IHbdy. intros f x b _ TBC. simpl in TBC. eapply transl_instr_basic_nonil; eauto.
+ - intros f x b Hnonil TBC. remember (bi2 :: bdy) as bdy'.
+ monadInv TBC.
+ assert (x0 <> nil).
+ eapply IHbdy; eauto. subst bdy'. discriminate.
+ eapply transl_instr_basic_nonil; eauto.
+Qed.
+
+Lemma transl_exit_nonil:
+ forall ex f bdy x,
+ ex <> None ->
+ transl_exit f ex = OK(bdy, x) ->
+ x <> None.
+Proof.
+ intros ex f bdy x Hnonil TIC.
+ destruct ex as [ex|].
+ - clear Hnonil. destruct ex.
+ all: try (simpl in TIC; try monadInv TIC; exploreInst; discriminate).
+ - contradict Hnonil; auto.
+Qed.
+
+Theorem app_nonil: forall A (l1 l2: list A),
+ l1 <> nil ->
+ l1 @@ l2 <> nil.
+Proof.
+ induction l2.
+ - intros; rewrite app_nil_r; auto.
+ - intros. unfold not. intros. symmetry in H0.
+ generalize (app_cons_not_nil); intros. unfold not in H1.
+ generalize (H0). apply H1.
+Qed.
+
+Theorem match_state_codestate:
+ forall mbs abs s fb sp bb c ms m,
+ (MB.body bb <> nil \/ MB.exit bb <> None) ->
+ mbs = (Machblock.State s fb sp (bb::c) ms m) ->
+ match_states mbs abs ->
+ exists cs fb f tbb tc ep,
+ match_codestate fb mbs cs /\ match_asmstate fb cs abs
+ /\ Genv.find_funct_ptr ge fb = Some (Internal f)
+ /\ transl_blocks f (bb::c) ep = OK (tbb::tc)
+ /\ body tbb = pbody1 cs ++ pbody2 cs
+ /\ exit tbb = pctl cs
+ /\ cur cs = tbb /\ rem cs = tc
+ /\ pstate cs = abs.
+Proof.
+ intros until m. intros Hnotempty Hmbs MS. subst. inv MS.
+ inv AT. clear H0. exploit transl_blocks_nonil; eauto. intros (tbb & tc' & Htc). subst.
+ exploit transl_blocks_distrib; eauto. intros (TLB & TLBS). clear H2.
+ monadInv TLB. exploit cons_bblocks_decomp; eauto.
+ { inversion Hnotempty.
+ - destruct (MB.body bb) as [|bi bdy]; try (contradict H0; simpl; auto; fail).
+ left. apply app_nonil. eapply transl_basic_code_nonil; eauto.
+ - destruct (MB.exit bb) as [ei|]; try (contradict H0; simpl; auto; fail).
+ right. eapply transl_exit_nonil; eauto. }
+ intros (Hth & Htbdy & Htexit).
+ exists {| pstate := (State rs m'); pheader := (Machblock.header bb); pbody1 := x1; pbody2 := x;
+ pctl := x0; ep := ep0; rem := tc'; cur := tbb |}, fb, f, tbb, tc', ep0.
+ repeat split. 1-2: econstructor; eauto.
+ { destruct (MB.header bb). eauto. discriminate. } eauto.
+ unfold transl_blocks. fold transl_blocks. unfold transl_block. rewrite EQ. simpl. rewrite EQ1; simpl.
+ rewrite TLBS. simpl. rewrite H2.
+ all: simpl; auto.
+Qed.
+
+Lemma exec_straight_body:
+ forall c c' rs1 m1 rs2 m2,
+ exec_straight tge lk c rs1 m1 c' rs2 m2 ->
+ exists l,
+ c = l ++ c'
+ /\ exec_body lk tge l rs1 m1 = Next rs2 m2.
+Proof.
+ induction c; try (intros; inv H; fail).
+ intros until m2. intros EXES. inv EXES.
+ - exists (a :: nil). repeat (split; simpl; auto). rewrite H6. auto.
+ - eapply IHc in H7; eauto. destruct H7 as (l' & Hc & EXECB). subst.
+ exists (a :: l'). repeat (split; simpl; auto).
+ rewrite H1. auto.
+Qed.
+
+Lemma exec_straight_body2:
+ forall c rs1 m1 c' rs2 m2,
+ exec_straight tge lk c rs1 m1 c' rs2 m2 ->
+ exists body,
+ exec_body lk tge body rs1 m1 = Next rs2 m2
+ /\ body ++ c' = c.
+Proof.
+ intros until m2. induction 1.
+ - exists (i1::nil). split; auto. simpl. rewrite H. auto.
+ - destruct IHexec_straight as (bdy & EXEB & BTC).
+ exists (i:: bdy). split; simpl.
+ + rewrite H. auto.
+ + congruence.
+Qed.
+
+Lemma exec_straight_opt_body2:
+ forall c rs1 m1 c' rs2 m2,
+ exec_straight_opt tge lk c rs1 m1 c' rs2 m2 ->
+ exists body,
+ exec_body lk tge body rs1 m1 = Next rs2 m2
+ /\ body ++ c' = c.
+Proof.
+ intros until m2. intros EXES.
+ inv EXES.
+ - exists nil. split; auto.
+ - eapply exec_straight_body2. auto.
+Qed.
+
+Lemma PC_not_data_preg: forall r ,
+ data_preg r = true ->
+ r <> PC.
+Proof.
+ intros. destruct (PregEq.eq r PC); [ rewrite e in H; simpl in H; discriminate | auto ].
+Qed.
+
+Lemma X30_not_data_preg: forall r ,
+ data_preg r = true ->
+ r <> X30.
+Proof.
+ intros. destruct (PregEq.eq r X30); [ rewrite e in H; simpl in H; discriminate | auto ].
+Qed.
+
+Lemma X16_not_data_preg: forall r ,
+ data_preg r = true ->
+ r <> X16.
+Proof.
+ intros. destruct (PregEq.eq r X16); [ rewrite e in H; simpl in H; discriminate | auto ].
+Qed.
+
+Lemma undef_regs_other_2':
+ forall r rl rs,
+ data_preg r = true ->
+ preg_notin r rl ->
+ undef_regs (DR (IR X16) :: DR (IR X30) :: map preg_of rl) rs r = rs r.
+Proof.
+ intros. apply undef_regs_other. intros. simpl in H1.
+ destruct H1 as [HX16 | [HX30 | HDES]]; subst.
+ apply X16_not_data_preg; auto. apply X30_not_data_preg; auto.
+ exploit list_in_map_inv; eauto. intros [mr [A B]]. subst.
+ rewrite preg_notin_charact in H0. auto.
+Qed.
+
+Ltac Simpl :=
+ rewrite Pregmap.gso; try apply PC_not_data_preg; try apply X30_not_data_preg.
+
+(* See (C) in the diagram. The proofs are mostly adapted from the previous Mach->Asm proofs, but are
+ unfortunately quite cumbersome. To reproduce them, it's best to have a Coq IDE with you and see by
+ yourself the steps *)
+Theorem step_simu_control:
+ forall bb' fb fn s sp c ms' m' rs2 m2 t S'' rs1 m1 tbb tbdy2 tex cs2,
+ MB.body bb' = nil ->
+ Genv.find_funct_ptr tge fb = Some (Internal fn) ->
+ pstate cs2 = (State rs2 m2) ->
+ pbody1 cs2 = nil -> pbody2 cs2 = tbdy2 -> pctl cs2 = tex ->
+ cur cs2 = tbb ->
+ match_codestate fb (MB.State s fb sp (bb'::c) ms' m') cs2 ->
+ match_asmstate fb cs2 (State rs1 m1) ->
+ exit_step return_address_offset ge (MB.exit bb') (MB.State s fb sp (bb'::c) ms' m') t S'' ->
+ (exists rs3 m3 rs4 m4,
+ exec_body lk tge tbdy2 rs2 m2 = Next rs3 m3
+ /\ exec_exit tge fn (Ptrofs.repr (size tbb)) rs3 m3 tex t rs4 m4
+ /\ match_states S'' (State rs4 m4)).
+Proof.
+ intros until cs2. intros Hbody FIND Hpstate Hpbody1 Hpbody2 Hpctl Hcur MCS MAS ESTEP.
+ inv ESTEP.
+ - inv MCS. inv MAS. simpl in *.
+ inv Hpstate.
+ destruct ctl.
+ + (* MBcall *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ destruct s1 as [rf|fid]; simpl in H1.
+ * (* Indirect call *)
+ monadInv H1. monadInv EQ.
+ assert (ms' rf = Vptr f' Ptrofs.zero).
+ { unfold find_function_ptr in H12. destruct (ms' rf); try discriminate.
+ revert H12; predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
+ assert (rs2 x = Vptr f' Ptrofs.zero).
+ { exploit ireg_val; eauto. rewrite H; intros LD; inv LD; auto. }
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+ remember (Ptrofs.add _ _) as ofs'.
+ assert (TCA: transl_code_at_pc ge (Vptr fb ofs') fb f c false tf tc).
+ { econstructor; eauto. }
+ assert (f1 = f) by congruence. subst f1.
+ exploit return_address_offset_correct; eauto. intros; subst ra.
+
+ repeat eexists.
+ econstructor; eauto. econstructor.
+ econstructor; eauto. econstructor; eauto.
+ eapply agree_sp_def; eauto. simpl. eapply agree_exten; eauto. intros.
+ unfold incrPC; repeat Simpl; auto.
+ simpl. unfold incrPC; rewrite Pregmap.gso; auto; try discriminate.
+ rewrite !Pregmap.gss. rewrite PCeq. rewrite Heqofs'. simpl. auto.
+
+ * (* Direct call *)
+ monadInv H1.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+ remember (Ptrofs.add _ _) as ofs'.
+ assert (TCA: transl_code_at_pc ge (Vptr fb ofs') fb f c false tf tc).
+ econstructor; eauto.
+ assert (f1 = f) by congruence. subst f1.
+ exploit return_address_offset_correct; eauto. intros; subst ra.
+ repeat eexists.
+ econstructor; eauto. econstructor.
+ econstructor; eauto. econstructor; eauto. eapply agree_sp_def; eauto. simpl. eapply agree_exten; eauto. intros.
+ unfold incrPC; repeat Simpl; auto. unfold Genv.symbol_address. rewrite symbols_preserved. simpl in H12. rewrite H12. auto.
+ unfold incrPC; simpl; rewrite Pregmap.gso; try discriminate. rewrite !Pregmap.gss.
+ subst. unfold Val.offset_ptr. rewrite PCeq. auto.
+ + (* MBtailcall *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ exploit Mem.loadv_extends. eauto. eexact H13. auto. simpl. intros [parent' [A B]].
+ destruct s1 as [rf|fid]; simpl in H11.
+ * monadInv H1. monadInv EQ.
+ assert (ms' rf = Vptr f' Ptrofs.zero).
+ { destruct (ms' rf); try discriminate. revert H11. predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
+ assert (rs2 x = Vptr f' Ptrofs.zero).
+ { exploit ireg_val; eauto. rewrite H; intros LD; inv LD; auto. }
+
+ assert (f = f1) by congruence. subst f1. clear FIND1. clear H12.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ intros (l & MKEPI & EXEB).
+ repeat eexists. rewrite app_nil_r in MKEPI.
+ rewrite <- MKEPI in EXEB.
+ eauto. econstructor. simpl. unfold incrPC.
+ rewrite !Pregmap.gso; try discriminate. eauto.
+ econstructor; eauto.
+ { apply agree_set_other.
+ - econstructor; auto with asmgen.
+ + apply V.
+ + intro r. destruct r; apply V; auto.
+ - eauto with asmgen. }
+ rewrite Pregmap.gss. rewrite Z; auto; try discriminate.
+ eapply ireg_of_not_X30''; eauto.
+ eapply ireg_of_not_X16''; eauto.
+ * monadInv H1. assert (f = f1) by congruence. subst f1. clear FIND1. clear H12.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ intros (l & MKEPI & EXEB).
+ repeat eexists. inv EQ. rewrite app_nil_r in MKEPI.
+ rewrite <- MKEPI in EXEB.
+ eauto. inv EQ. econstructor. simpl. unfold incrPC.
+ eauto.
+ econstructor; eauto.
+ { apply agree_set_other.
+ - econstructor; auto with asmgen.
+ + apply V.
+ + intro r. destruct r; apply V; auto.
+ - eauto with asmgen. }
+ { rewrite Pregmap.gss. unfold Genv.symbol_address. rewrite symbols_preserved. rewrite H11. auto. }
+ + (* MBbuiltin *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+ remember (Ptrofs.add _ _) as ofs'.
+ assert (TCA: transl_code_at_pc ge (Vptr fb ofs') fb f c false tf tc).
+ econstructor; eauto.
+
+ monadInv TBC. monadInv TIC. inv H0.
+
+ exploit builtin_args_match; eauto. intros [vargs' [P Q]].
+ exploit external_call_mem_extends; eauto.
+ intros [vres' [m2' [A [B [C D]]]]].
+
+ repeat eexists. econstructor. erewrite <- sp_val by eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved. eauto.
+ econstructor; eauto.
+ unfold incrPC. rewrite Pregmap.gss.
+ rewrite set_res_other. rewrite undef_regs_other. unfold Val.offset_ptr. rewrite PCeq.
+ eauto.
+ intros; simpl in *; destruct H as [HX16 | [HX30 | HDES]]; subst; try discriminate;
+ exploit list_in_map_inv; eauto; intros [mr [E F]]; subst; discriminate.
+ auto with asmgen. apply agree_nextblock. eapply agree_set_res; auto.
+ eapply agree_undef_regs; eauto. intros. rewrite undef_regs_other_2'; auto.
+ intros. discriminate.
+ + (* MBgoto *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0. assert (f1 = f) by congruence. subst f1. clear H9.
+ remember (incrPC (Ptrofs.repr (size tbb)) rs2) as rs2'.
+ exploit functions_transl. eapply FIND0. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+ exploit find_label_goto_label.
+ eauto. eauto.
+ instantiate (2 := rs2').
+ { subst. unfold incrPC. rewrite Pregmap.gss. unfold Val.offset_ptr. rewrite PCeq. eauto. }
+ eauto.
+ intros (tc' & rs' & GOTO & AT2 & INV).
+
+ eexists. eexists. repeat eexists. repeat split.
+ econstructor; eauto.
+ rewrite Heqrs2' in INV. unfold incrPC in INV.
+ rewrite Heqrs2' in GOTO; simpl; eauto.
+ econstructor; eauto.
+ eapply agree_exten; eauto with asmgen.
+ assert (forall r : preg, r <> PC -> rs' r = rs2 r).
+ { intros. rewrite Heqrs2' in INV.
+ rewrite INV; unfold incrPC; try rewrite Pregmap.gso; auto. }
+ eauto with asmgen.
+ congruence.
+ + (* MBcond *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0. monadInv H1. monadInv EQ.
+
+ * (* MBcond true *)
+ assert (f0 = f) by congruence. subst f0.
+ exploit eval_condition_lessdef.
+ eapply preg_vals; eauto.
+ all: eauto.
+ intros EC.
+ exploit transl_cbranch_correct_1; eauto. intros (rs', H).
+ destruct H as [ES [ECFI]].
+ exploit exec_straight_opt_body2. eauto. intros (bdy & EXEB & BTC).
+ assert (PCeq': rs2 PC = rs' PC). { inv ES; auto. erewrite <- exec_straight_pc. 2: eapply H0. eauto. }
+ rewrite PCeq' in PCeq.
+ assert (f1 = f) by congruence. subst f1.
+ exploit find_label_goto_label.
+ 4: eapply H14. 1-2: eauto. instantiate (2 := (incrPC (Ptrofs.repr (size tbb)) rs')).
+ unfold incrPC, Val.offset_ptr. rewrite PCeq. rewrite Pregmap.gss. eauto.
+ intros (tc' & rs3 & GOTOL & TLPC & Hrs3).
+ exploit functions_transl. eapply FIND1. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+
+ repeat eexists.
+ rewrite <- BTC. simpl. rewrite app_nil_r. eauto.
+ rewrite <- BTC. simpl. econstructor. rewrite ECFI. eauto.
+
+ econstructor; eauto.
+ eapply agree_exten with rs2; eauto with asmgen.
+ { intros. rewrite Hrs3; unfold incrPC. Simpl. rewrite H. all: auto. apply PC_not_data_preg; auto. }
+ intros. discriminate.
+ * (* MBcond false *)
+ assert (f0 = f) by congruence. subst f0. monadInv H1. monadInv EQ.
+ exploit eval_condition_lessdef.
+ eapply preg_vals; eauto.
+ all: eauto.
+ intros EC.
+
+ exploit transl_cbranch_correct_1; eauto. intros (rs', H).
+ destruct H as [ES [ECFI]].
+ exploit exec_straight_opt_body2. eauto. intros (bdy & EXEB & BTC).
+ assert (PCeq': rs2 PC = rs' PC). { inv ES; auto. erewrite <- exec_straight_pc. 2: eapply H0. eauto. }
+ rewrite PCeq' in PCeq.
+ exploit functions_transl. eapply FIND1. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+
+ assert (NOOV: size_blocks fn.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+
+ repeat eexists.
+ rewrite <- BTC. simpl. rewrite app_nil_r. eauto.
+ rewrite <- BTC. simpl. econstructor. rewrite ECFI. eauto.
+
+ econstructor; eauto.
+ unfold incrPC. rewrite Pregmap.gss. unfold Val.offset_ptr. rewrite PCeq. econstructor; eauto.
+ eapply agree_exten with rs2; eauto with asmgen.
+ { intros. unfold incrPC. Simpl. rewrite H. all: auto. }
+ intros. discriminate.
+ + (* MBjumptable *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ monadInv H1. monadInv EQ.
+ generalize (transf_function_no_overflow _ _ TRANSF0); intro NOOV.
+ assert (f1 = f) by congruence. subst f1.
+ exploit find_label_goto_label. 4: eapply H14. 1-2: eauto. instantiate (2 := (incrPC (Ptrofs.repr (size tbb)) rs2) # X16 <- Vundef).
+ unfold incrPC. Simpl. unfold Val.offset_ptr. rewrite PCeq. reflexivity. discriminate.
+ exploit functions_transl. eapply FIND0. eapply TRANSF0. intros FIND3. assert (fn = tf) by congruence. subst fn.
+
+ intros [tc' [rs' [A [B C]]]].
+ exploit ireg_val; eauto. rewrite H11. intros LD; inv LD.
+
+ repeat eexists. econstructor. simpl. Simpl. 2: { eapply ireg_of_not_X16''; eauto. }
+ unfold incrPC. rewrite Pregmap.gso; try discriminate. rewrite <- H1.
+ simpl. unfold Mach.label in H12. unfold label. rewrite H12. eapply A.
+ econstructor; eauto.
+ eapply agree_undef_regs; eauto. intros. rewrite C; auto with asmgen.
+ { unfold incrPC. repeat Simpl; auto. apply X16_not_data_preg; auto. }
+ discriminate.
+ + (* MBreturn *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ simpl. eauto.
+ intros EXEB. destruct EXEB as [l [MKEPI EXEB]].
+ assert (f1 = f) by congruence. subst f1.
+
+ repeat eexists.
+ rewrite app_nil_r in MKEPI. rewrite <- MKEPI in EXEB. eauto.
+ econstructor. simpl. reflexivity.
+ econstructor; eauto.
+ unfold incrPC. repeat apply agree_set_other; auto with asmgen.
+
+ - inv MCS. inv MAS. simpl in *. subst. inv Hpstate.
+ destruct bb' as [hd' bdy' ex']; simpl in *. subst.
+ monadInv TBC. monadInv TIC. simpl in *.
+ simpl. repeat eexists.
+ econstructor. econstructor. 4: instantiate (3 := false). all:eauto.
+ unfold incrPC. rewrite Pregmap.gss. unfold Val.offset_ptr. rewrite PCeq.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ assert (f = f0) by congruence. subst f0. econstructor; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1. eauto.
+ eapply agree_exten; eauto. intros. unfold incrPC; Simpl; auto.
+ discriminate.
+Qed.
+
+(* Handling the individual instructions of theorem (B) in the above diagram. A bit less cumbersome, but still tough *)
+Theorem step_simu_basic:
+ forall bb bb' s fb sp c ms m rs1 m1 ms' m' bi cs1 tbdy bdy,
+ MB.header bb = nil -> MB.body bb = bi::(bdy) ->
+ bb' = {| MB.header := nil; MB.body := bdy; MB.exit := MB.exit bb |} ->
+ basic_step ge s fb sp ms m bi ms' m' ->
+ pstate cs1 = (State rs1 m1) -> pbody1 cs1 = tbdy ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists rs2 m2 l cs2 tbdy',
+ cs2 = {| pstate := (State rs2 m2); pheader := nil; pbody1 := tbdy'; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := it1_is_parent (ep cs1) bi; rem := rem cs1; cur := cur cs1 |}
+ /\ tbdy = l ++ tbdy'
+ /\ exec_body lk tge l rs1 m1 = Next rs2 m2
+ /\ match_codestate fb (MB.State s fb sp (bb'::c) ms' m') cs2).
+Proof.
+ intros until bdy. intros Hheader Hbody (* Hnotempty *) Hbb' BSTEP Hpstate Hpbody1 MCS. inv MCS.
+ simpl in *. inv Hpstate.
+ rewrite Hbody in TBC. monadInv TBC.
+ inv BSTEP.
+
+ - (* MBgetstack *)
+ simpl in EQ0.
+ unfold Mach.load_stack in H.
+ exploit Mem.loadv_extends; eauto. intros [v' [A B]].
+ rewrite (sp_val _ _ _ AG) in A.
+ exploit loadind_correct; eauto with asmgen.
+ intros (rs2 & EXECS & Hrs'1 & Hrs'2).
+ eapply exec_straight_body in EXECS.
+ destruct EXECS as (l & Hlbi & EXECB).
+ exists rs2, m1, l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. simpl. auto. }
+ subst. simpl in Hheadereq.
+
+ eapply match_codestate_intro; eauto.
+ eapply agree_set_mreg; eauto with asmgen.
+ intro Hep. simpl in Hep. discriminate.
+ - (* MBsetstack *)
+ simpl in EQ0.
+ unfold Mach.store_stack in H.
+ assert (Val.lessdef (ms src) (rs1 (preg_of src))). { eapply preg_val; eauto. }
+ exploit Mem.storev_extends; eauto. intros [m2' [A B]].
+ exploit storeind_correct; eauto with asmgen.
+ rewrite (sp_val _ _ _ AG) in A. eauto. intros [rs' [P Q]].
+
+ eapply exec_straight_body in P.
+ destruct P as (l & ll & EXECB).
+ exists rs', m2', l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ. rewrite Hheader in EQ. auto.
+ eapply agree_undef_regs; eauto with asmgen.
+ simpl; intros. rewrite Q; auto with asmgen. rewrite Hheader in DXP. auto.
+ - (* MBgetparam *)
+ simpl in EQ0.
+
+ assert (f0 = f) by congruence; subst f0.
+ unfold Mach.load_stack in *.
+ exploit Mem.loadv_extends. eauto. eexact H0. auto.
+ intros [parent' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit lessdef_parent_sp; eauto. clear B; intros B; subst parent'.
+ exploit Mem.loadv_extends. eauto. eexact H1. auto.
+ intros [v' [C D]].
+
+ monadInv EQ0. rewrite Hheader. rewrite Hheader in DXP.
+ destruct ep0 eqn:EPeq.
+
+ (* X29 contains parent *)
+ + exploit loadind_correct. eexact EQ1.
+ instantiate (2 := rs1). rewrite DXP; eauto. discriminate.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ destruct P as (l & ll & EXECB).
+ exists rs2, m1, l. eexists.
+ eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. simpl. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+
+ eapply agree_set_mreg. eapply agree_set_mreg; eauto. congruence. auto with asmgen.
+ simpl; intros. rewrite R; auto with asmgen. unfold preg_of.
+ apply preg_of_not_X29; auto.
+
+ (* X29 does not contain parent *)
+ + rewrite chunk_of_Tptr in A.
+ exploit loadptr_correct. eexact A. discriminate. intros [rs2 [P [Q R]]].
+ exploit loadind_correct. eexact EQ1. instantiate (2 := rs2). rewrite Q. eauto.
+ discriminate.
+ intros [rs3 [S [T U]]].
+
+ exploit exec_straight_trans.
+ eapply P.
+ eapply S.
+ intros EXES.
+
+ eapply exec_straight_body in EXES.
+ destruct EXES as (l & ll & EXECB).
+ exists rs3, m1, l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+ eapply agree_set_mreg. eapply agree_set_mreg. eauto. eauto.
+ instantiate (1 := rs2#X29 <- (rs3#X29)). intros.
+ rewrite Pregmap.gso; auto with asmgen.
+ congruence.
+ intros. unfold Pregmap.set. destruct (PregEq.eq r' X29). congruence. auto with asmgen.
+ simpl; intros. rewrite U; auto with asmgen.
+ apply preg_of_not_X29; auto.
+ - (* MBop *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (eval_operation tge sp op (map ms args) m' = Some v).
+ rewrite <- H. apply eval_operation_preserved. exact symbols_preserved.
+ exploit eval_operation_lessdef.
+ eapply preg_vals; eauto.
+ 2: eexact H0.
+ all: eauto.
+ intros [v' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit transl_op_correct; eauto. intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ destruct P as (l & ll & EXECB).
+ exists rs2, m1, l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ. rewrite Hheader in EQ. auto.
+ apply agree_set_undef_mreg with rs1; auto.
+ apply Val.lessdef_trans with v'; auto.
+ simpl; intros. destruct (andb_prop _ _ H1); clear H1.
+ rewrite R; auto. apply preg_of_not_X29; auto.
+Local Transparent destroyed_by_op.
+ destruct op; simpl; auto; try discriminate.
+ - (* MBload *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (Op.eval_addressing tge sp addr (map ms args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
+ intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit Mem.loadv_extends; eauto. intros [v' [C D]]. destruct trap; try discriminate.
+ exploit transl_load_correct; eauto.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ destruct P as (l & ll & EXECB).
+ exists rs2, m1, l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+ eapply agree_set_mreg; eauto with asmgen.
+ intro Hep. simpl in Hep. discriminate.
+ - (* MBload notrap1 *)
+ simpl in EQ0. unfold transl_load in EQ0. discriminate.
+ - (* MBload notrap2 *)
+ simpl in EQ0. unfold transl_load in EQ0. discriminate.
+ - (* MBstore *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (Op.eval_addressing tge sp addr (map ms args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
+ intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ assert (Val.lessdef (ms src) (rs1 (preg_of src))). eapply preg_val; eauto.
+ exploit Mem.storev_extends; eauto. intros [m2' [C D]].
+ exploit transl_store_correct; eauto. intros [rs2 [P Q]].
+
+ eapply exec_straight_body in P.
+ destruct P as (l & ll & EXECB).
+ exists rs2, m2', l.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+ eapply agree_undef_regs; eauto with asmgen.
+ intro Hep. simpl in Hep. discriminate.
+Qed.
+
+Lemma exec_body_trans:
+ forall l l' rs0 m0 rs1 m1 rs2 m2,
+ exec_body lk tge l rs0 m0 = Next rs1 m1 ->
+ exec_body lk tge l' rs1 m1 = Next rs2 m2 ->
+ exec_body lk tge (l++l') rs0 m0 = Next rs2 m2.
+Proof.
+ induction l.
+ - simpl. induction l'. intros.
+ + simpl in *. congruence.
+ + intros. inv H. auto.
+ - intros until m2. intros EXEB1 EXEB2.
+ inv EXEB1. destruct (exec_basic _) eqn:EBI; try discriminate.
+ simpl. rewrite EBI. eapply IHl; eauto.
+Qed.
+
+Lemma exec_body_control:
+ forall b t rs1 m1 rs2 m2 rs3 m3 fn,
+ exec_body lk tge (body b) rs1 m1 = Next rs2 m2 ->
+ exec_exit tge fn (Ptrofs.repr (size b)) rs2 m2 (exit b) t rs3 m3 ->
+ exec_bblock lk tge fn b rs1 m1 t rs3 m3.
+Proof.
+ intros until fn. intros EXEB EXECTL.
+ econstructor; eauto.
+Qed.
+
+Inductive exec_header: codestate -> codestate -> Prop :=
+ | exec_header_cons: forall cs1,
+ exec_header cs1 {| pstate := pstate cs1; pheader := nil; pbody1 := pbody1 cs1; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := (if pheader cs1 then ep cs1 else false); rem := rem cs1;
+ cur := cur cs1 |}.
+
+(* Theorem (A) in the diagram, the easiest of all *)
+Theorem step_simu_header:
+ forall bb s fb sp c ms m rs1 m1 cs1,
+ pstate cs1 = (State rs1 m1) ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists cs1',
+ exec_header cs1 cs1'
+ /\ match_codestate fb (MB.State s fb sp (mb_remove_header bb::c) ms m) cs1').
+Proof.
+ intros until cs1. intros Hpstate MCS.
+ eexists. split; eauto.
+ econstructor; eauto.
+ inv MCS. simpl in *. inv Hpstate.
+ econstructor; eauto.
+Qed.
+
+(* Theorem (B) in the diagram, using step_simu_basic + induction on the Machblock body *)
+Theorem step_simu_body:
+ forall bb s fb sp c ms m rs1 m1 ms' cs1 m',
+ MB.header bb = nil ->
+ body_step ge s fb sp (MB.body bb) ms m ms' m' ->
+ pstate cs1 = (State rs1 m1) ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists rs2 m2 cs2 ep,
+ cs2 = {| pstate := (State rs2 m2); pheader := nil; pbody1 := nil; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := ep; rem := rem cs1; cur := cur cs1 |}
+ /\ exec_body lk tge (pbody1 cs1) rs1 m1 = Next rs2 m2
+ /\ match_codestate fb (MB.State s fb sp ({| MB.header := nil; MB.body := nil; MB.exit := MB.exit bb |}::c) ms' m') cs2).
+Proof.
+ intros bb. destruct bb as [hd bdy ex]; simpl; auto. induction bdy as [|bi bdy].
+ - intros until m'. intros Hheader BSTEP Hpstate MCS.
+ inv BSTEP.
+ exists rs1, m1, cs1, (ep cs1).
+ inv MCS. inv Hpstate. simpl in *. monadInv TBC. repeat (split; simpl; auto).
+ econstructor; eauto.
+ - intros until m'. intros Hheader BSTEP Hpstate MCS. inv BSTEP.
+ rename ms' into ms''. rename m' into m''. rename rs' into ms'. rename m'0 into m'.
+ exploit (step_simu_basic); eauto. simpl. eauto. simpl; auto. simpl; auto.
+ intros (rs2 & m2 & l & cs2 & tbdy' & Hcs2 & Happ & EXEB & MCS').
+ simpl in *.
+ exploit IHbdy. auto. eapply H6. 2: eapply MCS'. all: eauto. subst; eauto. simpl; auto.
+ intros (rs3 & m3 & cs3 & ep & Hcs3 & EXEB' & MCS'').
+ exists rs3, m3, cs3, ep.
+ repeat (split; simpl; auto). subst. simpl in *. auto.
+ rewrite Happ. eapply exec_body_trans; eauto. rewrite Hcs2 in EXEB'; simpl in EXEB'. auto.
+Qed.
+
+(* Bringing theorems (A), (B) and (C) together, for the case of the absence of builtin instruction *)
+(* This more general form is easier to prove, but the actual theorem is step_simulation_bblock further below *)
+Lemma step_simulation_bblock':
+ forall t sf f sp bb bb' bb'' rs m rs' m' s'' c S1,
+ bb' = mb_remove_header bb ->
+ body_step ge sf f sp (Machblock.body bb') rs m rs' m' ->
+ bb'' = mb_remove_body bb' ->
+ exit_step return_address_offset ge (Machblock.exit bb'') (Machblock.State sf f sp (bb'' :: c) rs' m') t s'' ->
+ match_states (Machblock.State sf f sp (bb :: c) rs m) S1 ->
+ exists S2 : state, plus (step lk) tge S1 t S2 /\ match_states s'' S2.
+Proof.
+ intros until S1. intros Hbb' BSTEP Hbb'' ESTEP MS.
+ destruct (mbsize bb) eqn:SIZE.
+ - apply mbsize_eqz in SIZE. destruct SIZE as (Hbody & Hexit).
+ destruct bb as [hd bdy ex]; simpl in *; subst.
+ inv MS. inv AT. exploit transl_blocks_nonil; eauto. intros (tbb & tc' & Htc). subst. rename tc' into tc.
+ monadInv H2. simpl in *. inv ESTEP. inv BSTEP.
+ eexists. split.
+ + eapply plus_one.
+ exploit functions_translated; eauto. intros (tf0 & FIND' & TRANSF'). monadInv TRANSF'.
+ assert (x = tf) by congruence. subst x.
+ eapply exec_step_internal; eauto. eapply find_bblock_tail; eauto.
+ unfold exec_bblock. simpl.
+ eexists; eexists; split; eauto.
+ econstructor.
+ + econstructor.
+ 1,2,3: eauto.
+ *
+ unfold incrPC. rewrite Pregmap.gss.
+ unfold Val.offset_ptr. rewrite <- H.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ { eapply transf_function_no_overflow; eauto. }
+ econstructor; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV H3). intro CT1. eauto.
+ *
+ eapply agree_exten; eauto. intros. unfold incrPC. rewrite Pregmap.gso; auto.
+ unfold data_preg in H2. destruct r; try congruence.
+ *
+ intros. discriminate.
+ - subst. exploit mbsize_neqz. { instantiate (1 := bb). rewrite SIZE. discriminate. }
+ intros Hnotempty.
+
+ (* initial setting *)
+ exploit match_state_codestate.
+ eapply Hnotempty.
+ all: eauto.
+ intros (cs1 & fb & f0 & tbb & tc & ep & MCS & MAS & FIND & TLBS & Hbody & Hexit & Hcur & Hrem & Hpstate).
+
+ (* step_simu_header part *)
+ assert (exists rs1 m1, pstate cs1 = State rs1 m1). { inv MAS. simpl. eauto. }
+ destruct H as (rs1 & m1 & Hpstate2). subst.
+ assert (f = fb). { inv MCS. auto. } subst fb.
+ exploit step_simu_header.
+ 2: eapply MCS.
+ all: eauto.
+ intros (cs1' & EXEH & MCS2).
+
+ (* step_simu_body part *)
+ assert (Hpstate': pstate cs1' = pstate cs1). { inv EXEH; auto. }
+ exploit step_simu_body.
+ 2: eapply BSTEP.
+ 3: eapply MCS2.
+ all: eauto. rewrite Hpstate'. eauto.
+ intros (rs2 & m2 & cs2 & ep' & Hcs2 & EXEB & MCS').
+
+ (* step_simu_control part *)
+ assert (exists tf, Genv.find_funct_ptr tge f = Some (Internal tf)).
+ { exploit functions_translated; eauto. intros (tf & FIND' & TRANSF'). monadInv TRANSF'. eauto. }
+ destruct H as (tf & FIND').
+ inv EXEH. simpl in *.
+ subst. exploit step_simu_control.
+ 8: eapply MCS'. all: simpl.
+ 9: eapply ESTEP.
+ all: simpl; eauto.
+ { inv MAS; simpl in *. inv Hpstate2. eapply match_asmstate_some; eauto.
+ erewrite exec_body_pc; eauto. }
+ intros (rs3 & m3 & rs4 & m4 & EXEB' & EXECTL' & MS').
+
+ (* bringing the pieces together *)
+ exploit exec_body_trans.
+ eapply EXEB.
+ eauto.
+ intros EXEB2.
+ exploit exec_body_control; eauto.
+ rewrite <- Hbody in EXEB2. eauto.
+ rewrite Hexit. eauto.
+ intros EXECB. (* inv EXECB. *)
+ exists (State rs4 m4).
+ split; auto. eapply plus_one. rewrite Hpstate2.
+ assert (exists ofs, rs1 PC = Vptr f ofs).
+ { rewrite Hpstate2 in MAS. inv MAS. simpl in *. eauto. }
+ destruct H as (ofs & Hrs1pc).
+ eapply exec_step_internal; eauto.
+
+ (* proving the initial find_bblock *)
+ rewrite Hpstate2 in MAS. inv MAS. simpl in *.
+ assert (f1 = f0) by congruence. subst f0.
+ rewrite PCeq in Hrs1pc. inv Hrs1pc.
+ exploit functions_translated; eauto. intros (tf1 & FIND'' & TRANS''). rewrite FIND' in FIND''.
+ inv FIND''. monadInv TRANS''. rewrite TRANSF0 in EQ. inv EQ.
+ eapply find_bblock_tail; eauto.
+Qed.
+
+Theorem step_simulation_bblock:
+ forall t sf f sp bb ms m ms' m' S2 c,
+ body_step ge sf f sp (Machblock.body bb) ms m ms' m' ->
+ exit_step return_address_offset ge (Machblock.exit bb) (Machblock.State sf f sp (bb :: c) ms' m') t S2 ->
+ forall S1', match_states (Machblock.State sf f sp (bb :: c) ms m) S1' ->
+ exists S2' : state, plus (step lk) tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ intros until c. intros BSTEP ESTEP S1' MS.
+ eapply step_simulation_bblock'; eauto.
+ all: destruct bb as [hd bdy ex]; simpl in *; eauto.
+ inv ESTEP.
+ - econstructor. inv H; try (econstructor; eauto; fail).
+ - econstructor.
+Qed.
+
+(* Measure to prove finite stuttering, see the other backends *)
+Definition measure (s: MB.state) : nat :=
+ match s with
+ | MB.State _ _ _ _ _ _ => 0%nat
+ | MB.Callstate _ _ _ _ => 0%nat
+ | MB.Returnstate _ _ _ => 1%nat
+ end.
+
+Lemma next_sep:
+ forall rs m rs' m', rs = rs' -> m = m' -> Next rs m = Next rs' m'.
+Proof.
+ congruence.
+Qed.
+
+(* The actual MB.step/AB.step simulation, using the above theorems, plus extra proofs
+ for the internal and external function cases *)
+Theorem step_simulation:
+ forall S1 t S2, MB.step return_address_offset ge S1 t S2 ->
+ forall S1' (MS: match_states S1 S1'),
+ (exists S2', plus (step lk) tge S1' t S2' /\ match_states S2 S2')
+ \/ (measure S2 < measure S1 /\ t = E0 /\ match_states S2 S1')%nat.
+Proof.
+ induction 1; intros.
+
+- (* bblock *)
+ left. destruct (Machblock.exit bb) eqn:MBE; try destruct c0.
+ all: try(inversion H0; subst; inv H2; eapply step_simulation_bblock;
+ try (rewrite MBE; try discriminate); eauto).
+ + inversion H0. subst. eapply step_simulation_bblock; try (rewrite MBE; try discriminate); eauto.
+- (* internal function *)
+ inv MS.
+ exploit functions_translated; eauto. intros [tf [A B]]. monadInv B.
+ generalize EQ; intros EQ'. monadInv EQ'.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks x0.(fn_blocks))); inversion EQ1. clear EQ1. subst x0.
+ unfold Mach.store_stack in *.
+ exploit Mem.alloc_extends. eauto. eauto. apply Z.le_refl. apply Z.le_refl.
+ intros [m1' [C D]].
+ exploit Mem.storev_extends. eexact D. eexact H1. eauto. eauto.
+ intros [m2' [F G]].
+ simpl chunk_of_type in F.
+ exploit Mem.storev_extends. eexact G. eexact H2. eauto. eauto.
+ intros [m3' [P Q]].
+ (* Execution of function prologue *)
+ monadInv EQ0.
+ set (tfbody := make_prologue f x0) in *.
+ set (tf := {| fn_sig := MB.fn_sig f; fn_blocks := tfbody |}) in *.
+ set (rs2 := rs0#X29 <- (parent_sp s) #SP <- sp #X16 <- Vundef).
+ exploit (storeptr_correct lk tge XSP (fn_retaddr_ofs f) RA nil m2' m3' rs2).
+ { rewrite chunk_of_Tptr in P.
+ assert (rs0 X30 = rs2 RA) by auto.
+ rewrite <- H3.
+ rewrite ATLR.
+ change (rs2 XSP) with sp. eexact P. }
+ 1-2: discriminate.
+ intros (rs3 & U & V).
+ assert (EXEC_PROLOGUE: exists rs3',
+ exec_straight_blocks tge lk tf
+ tf.(fn_blocks) rs0 m'
+ x0 rs3' m3'
+ /\ forall r, r <> PC -> r <> X16 -> rs3' r = rs3 r).
+ { eexists. split.
+ - change (fn_blocks tf) with tfbody; unfold tfbody.
+ econstructor; eauto.
+ assert (Archi.ptr64 = true) as SF; auto.
+ + unfold exec_bblock. simpl exec_body.
+ rewrite C. fold sp. rewrite <- (sp_val _ _ _ AG). rewrite chunk_of_Tptr in F.
+ assert (Mptr = Mint64) by auto. rewrite H3 in F. simpl in F. rewrite F. simpl.
+ unfold exec_store_rs_a. repeat Simpl; try discriminate.
+ exists rs2. exists m3'. split.
+ * unfold eval_addressing. Simpl; try discriminate. rewrite Pregmap.gss.
+ rewrite chunk_of_Tptr in P. rewrite H3 in P.
+ unfold Val.addl. unfold Val.offset_ptr in P.
+ destruct sp; simpl; try discriminate. rewrite SF; simpl.
+ rewrite Ptrofs.of_int64_to_int64. unfold Mem.storev in P. rewrite ATLR.
+ rewrite P. simpl. apply next_sep; eauto. apply SF.
+ * econstructor.
+ + eauto.
+ - intros. unfold incrPC.
+ rewrite Pregmap.gso; auto. rewrite V; auto.
+ } destruct EXEC_PROLOGUE as (rs3' & EXEC_PROLOGUE & Heqrs3').
+ exploit exec_straight_steps_2; eauto using functions_transl.
+ simpl fn_blocks. simpl fn_blocks in g. lia. constructor.
+ intros (ofs' & X & Y).
+ left; exists (State rs3' m3'); split.
+ eapply exec_straight_steps_1; eauto.
+ simpl fn_blocks. simpl fn_blocks in g. lia.
+ constructor.
+ econstructor; eauto.
+ rewrite X; econstructor; eauto.
+ apply agree_exten with rs2; eauto with asmgen.
+ unfold rs2.
+ apply agree_set_other; auto with asmgen.
+ apply agree_change_sp with (parent_sp s).
+ apply agree_undef_regs with rs0. auto.
+Local Transparent destroyed_at_function_entry.
+ simpl; intros; Simpl. auto.
+ assert (r' <> X29). { contradict H3; rewrite H3; unfold data_preg; auto. } auto.
+ unfold sp; congruence.
+
+ intros.
+
+ rewrite Heqrs3'. rewrite V. 2-5: try apply X16_not_data_preg; try apply PC_not_data_preg; auto.
+ auto.
+ intros. rewrite Heqrs3'; try discriminate. rewrite V by auto with asmgen. reflexivity.
+- (* external function *)
+ inv MS.
+ exploit functions_translated; eauto.
+ intros [tf [A B]]. simpl in B. inv B.
+ exploit extcall_arguments_match; eauto.
+ intros [args' [C D]].
+ exploit external_call_mem_extends; eauto.
+ intros [res' [m2' [P [Q [R S]]]]].
+ left; econstructor; split.
+ apply plus_one. eapply exec_step_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ econstructor; eauto.
+ unfold loc_external_result.
+ apply agree_set_other; auto.
+ apply agree_set_pair; auto.
+ apply agree_undef_caller_save_regs; auto.
+
+- (* return *)
+ inv MS.
+ inv STACKS. simpl in *.
+ right. split. lia. split. auto.
+ rewrite <- ATPC in H5.
+ econstructor; eauto. congruence.
+Qed.
+
+Lemma transf_initial_states:
+ forall st1, MB.initial_state prog st1 ->
+ exists st2, AB.initial_state tprog st2 /\ match_states st1 st2.
+Proof.
+ intros. inversion H. unfold ge0 in *.
+ econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf_partial TRANSF); eauto.
+ replace (Genv.symbol_address (Genv.globalenv tprog) (prog_main tprog) Ptrofs.zero)
+ with (Vptr fb Ptrofs.zero).
+ econstructor; eauto.
+ constructor.
+ apply Mem.extends_refl.
+ split. auto. simpl. unfold Vnullptr; destruct Archi.ptr64; congruence.
+ intros. rewrite Mach.Regmap.gi. auto.
+ unfold Genv.symbol_address.
+ rewrite (match_program_main TRANSF).
+ rewrite symbols_preserved.
+ unfold ge; rewrite H1. auto.
+Qed.
+
+Lemma transf_final_states:
+ forall st1 st2 r,
+ match_states st1 st2 -> MB.final_state st1 r -> AB.final_state st2 r.
+Proof.
+ intros. inv H0. inv H. constructor. assumption.
+ compute in H1. inv H1.
+ generalize (preg_val _ _ _ R0 AG). rewrite H2. intros LD; inv LD. auto.
+Qed.
+
+Definition return_address_offset : Machblock.function -> Machblock.code -> ptrofs -> Prop :=
+ Asmblockgenproof0.return_address_offset.
+
+Lemma transf_program_correct:
+ forward_simulation (MB.semantics return_address_offset prog) (AB.semantics lk tprog).
+Proof.
+ eapply forward_simulation_star with (measure := measure).
+ - apply senv_preserved.
+ - eexact transf_initial_states.
+ - eexact transf_final_states.
+ - exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/aarch64/Asmblockgenproof0.v b/aarch64/Asmblockgenproof0.v
new file mode 100644
index 00000000..004cfd5c
--- /dev/null
+++ b/aarch64/Asmblockgenproof0.v
@@ -0,0 +1,885 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * "block" version of Asmgenproof0
+
+ This module is largely adapted from Asmgenproof0.v of the other backends
+ It needs to stand apart because of the block structure, and the distinction control/basic that there isn't in the other backends
+ It has similar definitions than Asmgenproof0, but adapted to this new structure *)
+
+Require Import Coqlib.
+Require Intv.
+Require Import AST.
+Require Import Errors.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Events.
+Require Import Smallstep.
+Require Import Locations.
+Require Import Machblock.
+Require Import Asmblock.
+Require Import Asmblockgen.
+Require Import Conventions1.
+Require Import Axioms.
+Require Import Asmblockprops.
+Require Import Lia.
+
+Module MB:=Machblock.
+Module AB:=Asmblock.
+
+(** * Agreement between Mach registers and processor registers *)
+
+Hint Extern 2 (_ <> _) => congruence: asmgen.
+
+Lemma ireg_of_eq:
+ forall r r', ireg_of r = OK r' -> preg_of r = IR r'.
+Proof.
+ unfold ireg_of; intros. destruct (preg_of r) as [[[rr1|]|]|xsp|]; inv H; auto.
+Qed.
+
+Lemma freg_of_eq:
+ forall r r', freg_of r = OK r' -> preg_of r = FR r'.
+Proof.
+ unfold freg_of; intros. destruct (preg_of r) as [[fr|]|xsp|]; inv H; auto.
+Qed.
+
+Lemma ireg_of_eq':
+ forall r r', ireg_of r = OK r' -> dreg_of r = IR r'.
+Proof.
+ unfold ireg_of; intros. destruct r; simpl in *; inv H; auto.
+Qed.
+
+Lemma freg_of_eq':
+ forall r r', freg_of r = OK r' -> dreg_of r = FR r'.
+Proof.
+ unfold freg_of; intros. destruct r; simpl in *; inv H; auto.
+Qed.
+
+Fixpoint preg_notin (r: preg) (rl: list mreg) : Prop :=
+ match rl with
+ | nil => True
+ | r1 :: nil => r <> preg_of r1
+ | r1 :: rl => r <> preg_of r1 /\ preg_notin r rl
+ end.
+
+Remark preg_notin_charact:
+ forall r rl,
+ preg_notin r rl <-> (forall mr, In mr rl -> r <> preg_of mr).
+Proof.
+ induction rl; simpl; intros.
+ tauto.
+ destruct rl.
+ simpl. split. intros. intuition congruence. auto.
+ rewrite IHrl. split.
+ intros [A B]. intros. destruct H. congruence. auto.
+ auto.
+Qed.
+
+Record agree (ms: Mach.regset) (sp: val) (rs: AB.regset) : Prop := mkagree {
+ agree_sp: rs#SP = sp;
+ agree_sp_def: sp <> Vundef;
+ agree_mregs: forall r: mreg, Val.lessdef (ms r) (rs#(preg_of r))
+}.
+
+Lemma agree_exten:
+ forall ms sp rs rs',
+ agree ms sp rs ->
+ (forall r, data_preg r = true -> rs'#r = rs#r) ->
+ agree ms sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite H0; auto. auto.
+ intros. rewrite H0; auto. apply preg_of_data.
+Qed.
+
+Lemma preg_val:
+ forall ms sp rs r, agree ms sp rs -> Val.lessdef (ms r) rs#(preg_of r).
+Proof.
+ intros. destruct H. auto.
+Qed.
+
+Lemma preg_vals:
+ forall ms sp rs, agree ms sp rs ->
+ forall l, Val.lessdef_list (map ms l) (map rs (map preg_of l)).
+Proof.
+ induction l; simpl. constructor. constructor. eapply preg_val; eauto. auto.
+Qed.
+
+Lemma preg_of_injective:
+ forall r1 r2, preg_of r1 = preg_of r2 -> r1 = r2.
+Proof.
+ destruct r1; destruct r2; simpl; intros; reflexivity || discriminate.
+Qed.
+
+Lemma sp_val:
+ forall ms sp rs, agree ms sp rs -> sp = rs#SP.
+Proof.
+ intros. destruct H; auto.
+Qed.
+
+Lemma ireg_val:
+ forall ms sp rs r r',
+ agree ms sp rs ->
+ ireg_of r = OK r' ->
+ Val.lessdef (ms r) rs#r'.
+Proof.
+ intros. rewrite <- (ireg_of_eq _ _ H0). eapply preg_val; eauto.
+Qed.
+
+Lemma preg_of_not_X29: forall dst,
+ negb (mreg_eq dst R29) = true ->
+ DR (IR X29) <> preg_of dst.
+Proof.
+ intros. destruct dst; try discriminate.
+Qed.
+
+Hint Resolve preg_of_not_SP preg_of_not_PC: asmgen.
+
+(** Preservation of register agreement under various assignments. *)
+
+Lemma agree_set_mreg:
+ forall ms sp rs r v rs',
+ agree ms sp rs ->
+ Val.lessdef v (rs'#(preg_of r)) ->
+ (forall r', data_preg r' = true -> r' <> preg_of r -> rs'#r' = rs#r') ->
+ agree (Mach.Regmap.set r v ms) sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite H1; auto. apply not_eq_sym. apply preg_of_not_SP.
+ intros. unfold Mach.Regmap.set. destruct (Mach.RegEq.eq r0 r). congruence.
+ rewrite H1. auto. apply preg_of_data.
+ red; intros; elim n. eapply preg_of_injective; eauto.
+Qed.
+
+Corollary agree_set_mreg_parallel:
+ forall ms sp rs r v v',
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.Regmap.set r v ms) sp (Pregmap.set (preg_of r) v' rs).
+Proof.
+ intros. eapply agree_set_mreg; eauto. rewrite Pregmap.gss; auto. intros; apply Pregmap.gso; auto.
+Qed.
+
+Lemma agree_set_other:
+ forall ms sp rs r v,
+ agree ms sp rs ->
+ data_preg r = false ->
+ agree ms sp (rs#r <- v).
+Proof.
+ intros. apply agree_exten with rs. auto.
+ intros. apply Pregmap.gso. congruence.
+Qed.
+
+Lemma agree_nextblock:
+ forall ms sp rs b,
+ agree ms sp rs -> agree ms sp (incrPC (Ptrofs.repr (size b)) rs).
+Proof.
+ intros. unfold incrPC. apply agree_set_other. auto. auto.
+Qed.
+
+Lemma agree_set_pair:
+ forall sp p v v' ms rs,
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.set_pair p v ms) sp (set_pair (map_rpair preg_of p) v' rs).
+Proof.
+ intros. destruct p; simpl.
+ - apply agree_set_mreg_parallel; auto.
+ - apply agree_set_mreg_parallel. apply agree_set_mreg_parallel; auto.
+ apply Val.hiword_lessdef; auto. apply Val.loword_lessdef; auto.
+Qed.
+
+Lemma agree_set_res:
+ forall res ms sp rs v v',
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.set_res res v ms) sp (set_res (map_builtin_res DR (map_builtin_res dreg_of res)) v' rs).
+Proof.
+ induction res; simpl; intros.
+ - eapply agree_set_mreg; eauto. rewrite Pregmap.gss. auto.
+ intros. apply Pregmap.gso; auto.
+ - auto.
+ - apply IHres2. apply IHres1. auto.
+ apply Val.hiword_lessdef; auto.
+ apply Val.loword_lessdef; auto.
+Qed.
+
+Lemma agree_undef_regs:
+ forall ms sp rl rs rs',
+ agree ms sp rs ->
+ (forall r', data_preg r' = true -> preg_notin r' rl -> rs'#r' = rs#r') ->
+ agree (Mach.undef_regs rl ms) sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite <- agree_sp0. apply H0; auto.
+ rewrite preg_notin_charact. intros. apply not_eq_sym. apply preg_of_not_SP.
+ intros. destruct (In_dec mreg_eq r rl).
+ rewrite Mach.undef_regs_same; auto.
+ rewrite Mach.undef_regs_other; auto. rewrite H0; auto.
+ apply preg_of_data.
+ rewrite preg_notin_charact. intros; red; intros. elim n.
+ exploit preg_of_injective; eauto. congruence.
+Qed.
+
+Lemma agree_set_undef_mreg:
+ forall ms sp rs r v rl rs',
+ agree ms sp rs ->
+ Val.lessdef v (rs'#(preg_of r)) ->
+ (forall r', data_preg r' = true -> r' <> preg_of r -> preg_notin r' rl -> rs'#r' = rs#r') ->
+ agree (Mach.Regmap.set r v (Mach.undef_regs rl ms)) sp rs'.
+Proof.
+ intros. apply agree_set_mreg with (rs'#(preg_of r) <- (rs#(preg_of r))); auto.
+ apply agree_undef_regs with rs; auto.
+ intros. unfold Pregmap.set. destruct (PregEq.eq r' (preg_of r)).
+ congruence. auto.
+ intros. rewrite Pregmap.gso; auto.
+Qed.
+
+Lemma agree_undef_caller_save_regs:
+ forall ms sp rs,
+ agree ms sp rs ->
+ agree (Mach.undef_caller_save_regs ms) sp (undef_caller_save_regs rs).
+Proof.
+ intros. destruct H. unfold Mach.undef_caller_save_regs, undef_caller_save_regs; split.
+ - unfold proj_sumbool; rewrite dec_eq_true. auto.
+ - auto.
+ - intros. unfold proj_sumbool. rewrite dec_eq_false by (apply preg_of_not_SP).
+ destruct (List.in_dec preg_eq (preg_of r) (List.map preg_of (List.filter is_callee_save all_mregs))); simpl.
+ + apply list_in_map_inv in i. destruct i as (mr & A & B).
+ assert (r = mr) by (apply preg_of_injective; auto). subst mr; clear A.
+ apply List.filter_In in B. destruct B as [C D]. rewrite D. auto.
+ + destruct (is_callee_save r) eqn:CS; auto.
+ elim n. apply List.in_map. apply List.filter_In. auto using all_mregs_complete.
+Qed.
+
+Lemma agree_change_sp:
+ forall ms sp rs sp',
+ agree ms sp rs -> sp' <> Vundef ->
+ agree ms sp' (rs#SP <- sp').
+Proof.
+ intros. inv H. split; auto.
+ intros. rewrite Pregmap.gso; auto with asmgen.
+Qed.
+
+Remark builtin_arg_match:
+ forall ge (rs: regset) sp m a v,
+ eval_builtin_arg ge (fun r => rs (dreg_of r)) sp m a v ->
+ eval_builtin_arg ge (fun r => rs (DR r)) sp m (map_builtin_arg dreg_of a) v.
+Proof.
+ induction 1; simpl; eauto with barg. econstructor.
+Qed.
+
+Lemma builtin_args_match:
+ forall ge ms sp rs m m', agree ms sp rs -> Mem.extends m m' ->
+ forall al vl, eval_builtin_args ge ms sp m al vl ->
+ exists vl', eval_builtin_args ge (fun r => rs (DR r)) sp m' (map (map_builtin_arg dreg_of) al) vl'
+ /\ Val.lessdef_list vl vl'.
+Proof.
+ induction 3; intros; simpl.
+ exists (@nil val); split; constructor.
+ exploit (@eval_builtin_arg_lessdef _ ge ms (fun r => rs (preg_of r))); eauto.
+ intros; eapply preg_val; eauto.
+ intros (v1' & A & B).
+ destruct IHlist_forall2 as [vl' [C D]].
+ exists (v1' :: vl'); split; constructor; auto. apply builtin_arg_match; auto.
+Qed.
+
+(** Connection between Mach and Asm calling conventions for external
+ functions. *)
+
+Lemma extcall_arg_match:
+ forall ms sp rs m m' l v,
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ Mach.extcall_arg ms m sp l v ->
+ exists v', extcall_arg rs m' l v' /\ Val.lessdef v v'.
+Proof.
+ intros. inv H1.
+ exists (rs#(preg_of r)); split. constructor. eapply preg_val; eauto.
+ unfold Mach.load_stack in H2.
+ exploit Mem.loadv_extends; eauto. intros [v' [A B]].
+ rewrite (sp_val _ _ _ H) in A.
+ exists v'; split; auto.
+ econstructor. eauto. assumption.
+Qed.
+
+Lemma extcall_arg_pair_match:
+ forall ms sp rs m m' p v,
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ Mach.extcall_arg_pair ms m sp p v ->
+ exists v', extcall_arg_pair rs m' p v' /\ Val.lessdef v v'.
+Proof.
+ intros. inv H1.
+ - exploit extcall_arg_match; eauto. intros (v' & A & B). exists v'; split; auto. constructor; auto.
+ - exploit extcall_arg_match. eauto. eauto. eexact H2. intros (v1 & A1 & B1).
+ exploit extcall_arg_match. eauto. eauto. eexact H3. intros (v2 & A2 & B2).
+ exists (Val.longofwords v1 v2); split. constructor; auto. apply Val.longofwords_lessdef; auto.
+Qed.
+
+Lemma extcall_args_match:
+ forall ms sp rs m m', agree ms sp rs -> Mem.extends m m' ->
+ forall ll vl,
+ list_forall2 (Mach.extcall_arg_pair ms m sp) ll vl ->
+ exists vl', list_forall2 (extcall_arg_pair rs m') ll vl' /\ Val.lessdef_list vl vl'.
+Proof.
+ induction 3; intros.
+ exists (@nil val); split. constructor. constructor.
+ exploit extcall_arg_pair_match; eauto. intros [v1' [A B]].
+ destruct IHlist_forall2 as [vl' [C D]].
+ exists (v1' :: vl'); split; constructor; auto.
+Qed.
+
+Lemma extcall_arguments_match:
+ forall ms m m' sp rs sg args,
+ agree ms sp rs -> Mem.extends m m' ->
+ Mach.extcall_arguments ms m sp sg args ->
+ exists args', extcall_arguments rs m' sg args' /\ Val.lessdef_list args args'.
+Proof.
+ unfold Mach.extcall_arguments, extcall_arguments; intros.
+ eapply extcall_args_match; eauto.
+Qed.
+
+Lemma set_res_other:
+ forall r res v rs,
+ data_preg r = false ->
+ set_res (map_builtin_res DR (map_builtin_res dreg_of res)) v rs r = rs r.
+Proof.
+ induction res; simpl; intros.
+ - apply Pregmap.gso. red; intros; subst r. rewrite dreg_of_data in H; discriminate.
+ - auto.
+ - rewrite IHres2, IHres1; auto.
+Qed.
+
+Lemma undef_regs_other:
+ forall r rl rs,
+ (forall r', In r' rl -> r <> r') ->
+ undef_regs rl rs r = rs r.
+Proof.
+ induction rl; simpl; intros. auto.
+ rewrite IHrl by auto. rewrite Pregmap.gso; auto.
+Qed.
+
+Lemma undef_regs_other_2:
+ forall r rl rs,
+ preg_notin r rl ->
+ undef_regs (map preg_of rl) rs r = rs r.
+Proof.
+ intros. apply undef_regs_other. intros.
+ exploit list_in_map_inv; eauto. intros [mr [A B]]. subst.
+ rewrite preg_notin_charact in H. auto.
+Qed.
+
+Inductive code_tail: Z -> bblocks -> bblocks -> Prop :=
+ | code_tail_0: forall c,
+ code_tail 0 c c
+ | code_tail_S: forall pos bi c1 c2,
+ code_tail pos c1 c2 ->
+ code_tail (pos + (size bi)) (bi :: c1) c2.
+
+Lemma code_tail_pos:
+ forall pos c1 c2, code_tail pos c1 c2 -> pos >= 0.
+Proof.
+ induction 1. lia. generalize (size_positive bi); intros; lia.
+Qed.
+
+Lemma find_bblock_tail:
+ forall c1 bi c2 pos,
+ code_tail pos c1 (bi :: c2) ->
+ find_bblock pos c1 = Some bi.
+Proof.
+ induction c1; simpl; intros.
+ inversion H.
+ destruct (zlt pos 0). generalize (code_tail_pos _ _ _ H); intro; lia.
+ destruct (zeq pos 0). subst pos.
+ inv H. auto. generalize (size_positive a) (code_tail_pos _ _ _ H4). intro; lia.
+ inv H. congruence. replace (pos0 + size a - size a) with pos0 by lia.
+ eauto.
+Qed.
+
+Local Hint Resolve code_tail_0 code_tail_S: core.
+
+Lemma code_tail_next:
+ forall fn ofs c0,
+ code_tail ofs fn c0 ->
+ forall bi c1, c0 = bi :: c1 -> code_tail (ofs + (size bi)) fn c1.
+Proof.
+ induction 1; intros.
+ - subst; eauto.
+ - replace (pos + size bi + size bi0) with ((pos + size bi0) + size bi); eauto.
+ lia.
+Qed.
+
+Lemma size_blocks_pos c: 0 <= size_blocks c.
+Proof.
+ induction c as [| a l ]; simpl; try lia.
+ generalize (size_positive a); lia.
+Qed.
+
+Remark code_tail_positive:
+ forall fn ofs c,
+ code_tail ofs fn c -> 0 <= ofs.
+Proof.
+ induction 1; intros; simpl.
+ - lia.
+ - generalize (size_positive bi). lia.
+Qed.
+
+Remark code_tail_size:
+ forall fn ofs c,
+ code_tail ofs fn c -> size_blocks fn = ofs + size_blocks c.
+Proof.
+ induction 1; intros; simpl; try lia.
+Qed.
+
+Remark code_tail_bounds fn ofs c:
+ code_tail ofs fn c -> 0 <= ofs <= size_blocks fn.
+Proof.
+ intro H;
+ exploit code_tail_size; eauto.
+ generalize (code_tail_positive _ _ _ H), (size_blocks_pos c).
+ lia.
+Qed.
+
+Local Hint Resolve code_tail_next: core.
+
+Lemma code_tail_next_int:
+ forall fn ofs bi c,
+ size_blocks fn <= Ptrofs.max_unsigned ->
+ code_tail (Ptrofs.unsigned ofs) fn (bi :: c) ->
+ code_tail (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr (size bi)))) fn c.
+Proof.
+ intros.
+ exploit code_tail_size; eauto.
+ simpl; generalize (code_tail_positive _ _ _ H0), (size_positive bi), (size_blocks_pos c).
+ intros.
+ rewrite Ptrofs.add_unsigned, Ptrofs.unsigned_repr.
+ - rewrite Ptrofs.unsigned_repr; eauto.
+ lia.
+ - rewrite Ptrofs.unsigned_repr; lia.
+Qed.
+
+(** The [find_label] function returns the code tail starting at the
+ given label. A connection with [code_tail] is then established. *)
+
+Fixpoint find_label (lbl: label) (c: bblocks) {struct c} : option bblocks :=
+ match c with
+ | nil => None
+ | bb1 :: bbl => if is_label lbl bb1 then Some c else find_label lbl bbl
+ end.
+
+(* inspired from Mach *)
+
+Lemma find_label_tail:
+ forall lbl c c', MB.find_label lbl c = Some c' -> is_tail c' c.
+Proof.
+ induction c; simpl; intros. discriminate.
+ destruct (MB.is_label lbl a). inv H. auto with coqlib. eauto with coqlib.
+Qed.
+
+Lemma label_pos_code_tail:
+ forall lbl c pos c',
+ find_label lbl c = Some c' ->
+ exists pos',
+ label_pos lbl pos c = Some pos'
+ /\ code_tail (pos' - pos) c c'
+ /\ pos <= pos' <= pos + size_blocks c.
+Proof.
+ induction c.
+ simpl; intros. discriminate.
+ simpl; intros until c'.
+ case (is_label lbl a).
+ - intros. inv H. exists pos. split; auto. split.
+ replace (pos - pos) with 0 by lia. constructor. constructor; try lia.
+ generalize (size_blocks_pos c). generalize (size_positive a). lia.
+ - intros. generalize (IHc (pos+size a) c' H). intros [pos' [A [B C]]].
+ exists pos'. split. auto. split.
+ replace (pos' - pos) with ((pos' - (pos + (size a))) + (size a)) by lia.
+ constructor. auto. generalize (size_positive a). lia.
+Qed.
+
+(** Predictor for return addresses in generated Asm code.
+
+ The [return_address_offset] predicate defined here is used in the
+ semantics for Mach to determine the return addresses that are
+ stored in activation records. *)
+
+(** Consider a Mach function [f] and a sequence [c] of Mach instructions
+ representing the Mach code that remains to be executed after a
+ function call returns. The predicate [return_address_offset f c ofs]
+ holds if [ofs] is the integer offset of the PPC instruction
+ following the call in the Asm code obtained by translating the
+ code of [f]. Graphically:
+<<
+ Mach function f |--------- Mcall ---------|
+ Mach code c | |--------|
+ | \ \
+ | \ \
+ | \ \
+ Asm code | |--------|
+ Asm function |------------- Pcall ---------|
+
+ <-------- ofs ------->
+>>
+*)
+
+Definition return_address_offset (f: MB.function) (c: MB.code) (ofs: ptrofs) : Prop :=
+ forall tf tc,
+ transf_function f = OK tf ->
+ transl_blocks f c false = OK tc ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) tc.
+
+Lemma transl_blocks_tail:
+ forall f c1 c2, is_tail c1 c2 ->
+ forall tc2 ep2, transl_blocks f c2 ep2 = OK tc2 ->
+ exists tc1, exists ep1, transl_blocks f c1 ep1 = OK tc1 /\ is_tail tc1 tc2.
+Proof.
+ induction 1; simpl; intros.
+ exists tc2; exists ep2; split; auto with coqlib.
+ monadInv H0. exploit IHis_tail; eauto. intros (tc1 & ep1 & A & B).
+ exists tc1; exists ep1; split. auto.
+ eapply is_tail_trans with x0; eauto with coqlib.
+Qed.
+
+Lemma is_tail_code_tail:
+ forall c1 c2, is_tail c1 c2 -> exists ofs, code_tail ofs c2 c1.
+Proof.
+ induction 1; eauto.
+ destruct IHis_tail; eauto.
+Qed.
+
+Section RETADDR_EXISTS.
+
+Hypothesis transf_function_inv:
+ forall f tf, transf_function f = OK tf ->
+ exists tc ep, transl_blocks f (Machblock.fn_code f) ep = OK tc /\ is_tail tc (fn_blocks tf).
+
+Hypothesis transf_function_len:
+ forall f tf, transf_function f = OK tf -> size_blocks (fn_blocks tf) <= Ptrofs.max_unsigned.
+
+
+Lemma return_address_exists:
+ forall b f c, is_tail (b :: c) f.(MB.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros. destruct (transf_function f) as [tf|] eqn:TF.
+ + exploit transf_function_inv; eauto. intros (tc1 & ep1 & TR1 & TL1).
+ exploit transl_blocks_tail; eauto. intros (tc2 & ep2 & TR2 & TL2).
+ monadInv TR2.
+ assert (TL3: is_tail x0 (fn_blocks tf)).
+ { apply is_tail_trans with tc1; auto.
+ apply is_tail_trans with (x++x0); auto. eapply is_tail_app.
+ }
+ exploit is_tail_code_tail. eexact TL3. intros [ofs CT].
+ exists (Ptrofs.repr ofs). red; intros.
+ rewrite Ptrofs.unsigned_repr. congruence.
+ exploit code_tail_bounds; eauto.
+ intros; apply transf_function_len in TF. lia.
+ + exists Ptrofs.zero; red; intros. congruence.
+Qed.
+
+End RETADDR_EXISTS.
+
+(** [transl_code_at_pc pc fb f c ep tf tc] holds if the code pointer [pc] points
+ within the Asmblock code generated by translating Machblock function [f],
+ and [tc] is the tail of the generated code at the position corresponding
+ to the code pointer [pc]. *)
+
+Inductive transl_code_at_pc (ge: MB.genv):
+ val -> block -> MB.function -> MB.code -> bool -> AB.function -> AB.bblocks -> Prop :=
+ transl_code_at_pc_intro:
+ forall b ofs f c ep tf tc,
+ Genv.find_funct_ptr ge b = Some(Internal f) ->
+ transf_function f = Errors.OK tf ->
+ transl_blocks f c ep = OK tc ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) tc ->
+ transl_code_at_pc ge (Vptr b ofs) b f c ep tf tc.
+
+Remark code_tail_no_bigger:
+ forall pos c1 c2, code_tail pos c1 c2 -> (length c2 <= length c1)%nat.
+Proof.
+ induction 1; simpl; lia.
+Qed.
+
+Remark code_tail_unique:
+ forall fn c pos pos',
+ code_tail pos fn c -> code_tail pos' fn c -> pos = pos'.
+Proof.
+ induction fn; intros until pos'; intros ITA CT; inv ITA; inv CT; auto.
+ generalize (code_tail_no_bigger _ _ _ H3); simpl; intro; lia.
+ generalize (code_tail_no_bigger _ _ _ H3); simpl; intro; lia.
+ f_equal. eauto.
+Qed.
+
+Lemma return_address_offset_correct:
+ forall ge b ofs fb f c tf tc ofs',
+ transl_code_at_pc ge (Vptr b ofs) fb f c false tf tc ->
+ return_address_offset f c ofs' ->
+ ofs' = ofs.
+Proof.
+ intros. inv H. red in H0.
+ exploit code_tail_unique. eexact H12. eapply H0; eauto. intro.
+ rewrite <- (Ptrofs.repr_unsigned ofs).
+ rewrite <- (Ptrofs.repr_unsigned ofs').
+ congruence.
+Qed.
+
+Section STRAIGHTLINE.
+
+Variable ge: genv.
+Variable lk: aarch64_linker.
+Variable fn: function.
+
+(** Straight-line code is composed of processor instructions that execute
+ in sequence (no branches, no function calls and returns).
+ The following inductive predicate relates the machine states
+ before and after executing a straight-line sequence of instructions.
+ Instructions are taken from the first list instead of being fetched
+ from memory. *)
+
+Inductive exec_straight: list basic -> regset -> mem ->
+ list basic -> regset -> mem -> Prop :=
+ | exec_straight_one:
+ forall i1 c rs1 m1 rs2 m2,
+ exec_basic lk ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_straight (i1 :: c) rs1 m1 c rs2 m2
+ | exec_straight_step:
+ forall i c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_basic lk ge i rs1 m1 = Next rs2 m2 ->
+ exec_straight c rs2 m2 c' rs3 m3 ->
+ exec_straight (i :: c) rs1 m1 c' rs3 m3.
+
+Lemma exec_straight_trans:
+ forall c1 rs1 m1 c2 rs2 m2 c3 rs3 m3,
+ exec_straight c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight c1 rs1 m1 c3 rs3 m3.
+Proof.
+ induction 1; intros.
+ apply exec_straight_step with rs2 m2; auto.
+ apply exec_straight_step with rs2 m2; auto.
+Qed.
+
+Lemma exec_straight_two:
+ forall i1 i2 c rs1 m1 rs2 m2 rs3 m3,
+ exec_basic lk ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_basic lk ge i2 rs2 m2 = Next rs3 m3 ->
+ exec_straight (i1 :: i2 :: c) rs1 m1 c rs3 m3.
+Proof.
+ intros. apply exec_straight_step with rs2 m2; auto.
+ apply exec_straight_one; auto.
+Qed.
+
+Lemma exec_straight_three:
+ forall i1 i2 i3 c rs1 m1 rs2 m2 rs3 m3 rs4 m4,
+ exec_basic lk ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_basic lk ge i2 rs2 m2 = Next rs3 m3 ->
+ exec_basic lk ge i3 rs3 m3 = Next rs4 m4 ->
+ exec_straight (i1 :: i2 :: i3 :: c) rs1 m1 c rs4 m4.
+Proof.
+ intros. apply exec_straight_step with rs2 m2; auto.
+ eapply exec_straight_two; eauto.
+Qed.
+
+Inductive exec_straight_opt: list basic -> regset -> mem -> list basic -> regset -> mem -> Prop :=
+ | exec_straight_opt_refl: forall c rs m,
+ exec_straight_opt c rs m c rs m
+ | exec_straight_opt_intro: forall c1 rs1 m1 c2 rs2 m2,
+ exec_straight c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight_opt c1 rs1 m1 c2 rs2 m2.
+
+Remark exec_straight_opt_right:
+ forall c3 rs3 m3 c1 rs1 m1 c2 rs2 m2,
+ exec_straight_opt c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight c1 rs1 m1 c3 rs3 m3.
+Proof.
+ destruct 1; intros. auto. eapply exec_straight_trans; eauto.
+Qed.
+
+Lemma exec_straight_opt_step:
+ forall i c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_basic lk ge i rs1 m1 = Next rs2 m2 ->
+ exec_straight_opt c rs2 m2 c' rs3 m3 ->
+ exec_straight (i :: c) rs1 m1 c' rs3 m3.
+Proof.
+ intros. inv H0.
+ - apply exec_straight_one; auto.
+ - eapply exec_straight_step; eauto.
+Qed.
+
+Lemma exec_straight_opt_step_opt:
+ forall i c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_basic lk ge i rs1 m1 = Next rs2 m2 ->
+ exec_straight_opt c rs2 m2 c' rs3 m3 ->
+ exec_straight_opt (i :: c) rs1 m1 c' rs3 m3.
+Proof.
+ intros. apply exec_straight_opt_intro. eapply exec_straight_opt_step; eauto.
+Qed.
+
+(** Like exec_straight predicate, but on blocks *)
+
+Inductive exec_straight_blocks: bblocks -> regset -> mem ->
+ bblocks -> regset -> mem -> Prop :=
+ | exec_straight_blocks_one:
+ forall b1 c rs1 m1 rs2 m2,
+ exec_bblock lk ge fn b1 rs1 m1 E0 rs2 m2 ->
+ rs2#PC = Val.offset_ptr rs1#PC (Ptrofs.repr (size b1)) ->
+ exec_straight_blocks (b1 :: c) rs1 m1 c rs2 m2
+ | exec_straight_blocks_step:
+ forall b c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_bblock lk ge fn b rs1 m1 E0 rs2 m2 ->
+ rs2#PC = Val.offset_ptr rs1#PC (Ptrofs.repr (size b)) ->
+ exec_straight_blocks c rs2 m2 c' rs3 m3 ->
+ exec_straight_blocks (b :: c) rs1 m1 c' rs3 m3.
+
+Lemma exec_straight_blocks_trans:
+ forall c1 rs1 m1 c2 rs2 m2 c3 rs3 m3,
+ exec_straight_blocks c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight_blocks c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight_blocks c1 rs1 m1 c3 rs3 m3.
+Proof.
+ induction 1; intros.
+ eapply exec_straight_blocks_step; eauto.
+ eapply exec_straight_blocks_step; eauto.
+Qed.
+
+(** Linking exec_straight with exec_straight_blocks *)
+
+Lemma exec_straight_pc:
+ forall c c' rs1 m1 rs2 m2,
+ exec_straight c rs1 m1 c' rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ induction c; intros; try (inv H; fail).
+ inv H.
+ - eapply exec_basic_instr_pc; eauto.
+ - rewrite (IHc c' rs3 m3 rs2 m2); auto.
+ erewrite exec_basic_instr_pc; eauto.
+Qed.
+
+Lemma exec_body_pc:
+ forall ge l rs1 m1 rs2 m2,
+ exec_body lk ge l rs1 m1 = Next rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ induction l.
+ - intros. inv H. auto.
+ - intros until m2. intro EXEB.
+ inv EXEB. destruct (exec_basic _ _ _ _ _) eqn:EBI; try discriminate.
+ eapply IHl in H0. rewrite H0.
+ destruct s.
+ erewrite exec_basic_instr_pc; eauto.
+Qed.
+
+(** The following lemmas show that straight-line executions
+ (predicate [exec_straight_blocks]) correspond to correct Asm executions. *)
+
+Lemma exec_straight_steps_1:
+ forall c rs m c' rs' m',
+ exec_straight_blocks c rs m c' rs' m' ->
+ size_blocks (fn_blocks fn) <= Ptrofs.max_unsigned ->
+ forall b ofs,
+ rs#PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal fn) ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks fn) c ->
+ plus (step lk) ge (State rs m) E0 (State rs' m').
+Proof.
+ induction 1; intros.
+ apply plus_one.
+ econstructor; eauto.
+ eapply find_bblock_tail. eauto.
+ eapply plus_left'.
+ econstructor; eauto.
+ eapply find_bblock_tail. eauto.
+ apply IHexec_straight_blocks with b0 (Ptrofs.add ofs (Ptrofs.repr (size b))).
+ auto. rewrite H0. rewrite H3. reflexivity.
+ auto.
+ apply code_tail_next_int; auto.
+ traceEq.
+Qed.
+
+Lemma exec_straight_steps_2:
+ forall c rs m c' rs' m',
+ exec_straight_blocks c rs m c' rs' m' ->
+ size_blocks (fn_blocks fn) <= Ptrofs.max_unsigned ->
+ forall b ofs,
+ rs#PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal fn) ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks fn) c ->
+ exists ofs',
+ rs'#PC = Vptr b ofs'
+ /\ code_tail (Ptrofs.unsigned ofs') (fn_blocks fn) c'.
+Proof.
+ induction 1; intros.
+ exists (Ptrofs.add ofs (Ptrofs.repr (size b1))). split.
+ rewrite H0. rewrite H2. auto.
+ apply code_tail_next_int; auto.
+ apply IHexec_straight_blocks with (Ptrofs.add ofs (Ptrofs.repr (size b))).
+ auto. rewrite H0. rewrite H3. reflexivity. auto.
+ apply code_tail_next_int; auto.
+Qed.
+
+End STRAIGHTLINE.
+
+(** * Properties of the Machblock call stack *)
+
+Section MATCH_STACK.
+
+Variable ge: MB.genv.
+
+Inductive match_stack: list MB.stackframe -> Prop :=
+ | match_stack_nil:
+ match_stack nil
+ | match_stack_cons: forall fb sp ra c s f tf tc,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transl_code_at_pc ge ra fb f c false tf tc ->
+ sp <> Vundef ->
+ match_stack s ->
+ match_stack (Stackframe fb sp ra c :: s).
+
+Lemma parent_sp_def: forall s, match_stack s -> parent_sp s <> Vundef.
+Proof.
+ induction 1; simpl.
+ unfold Vnullptr; destruct Archi.ptr64; congruence.
+ auto.
+Qed.
+
+Lemma parent_ra_def: forall s, match_stack s -> parent_ra s <> Vundef.
+Proof.
+ induction 1; simpl.
+ unfold Vnullptr; destruct Archi.ptr64; congruence.
+ inv H0. congruence.
+Qed.
+
+Lemma lessdef_parent_sp:
+ forall s v,
+ match_stack s -> Val.lessdef (parent_sp s) v -> v = parent_sp s.
+Proof.
+ intros. inv H0. auto. exploit parent_sp_def; eauto. tauto.
+Qed.
+
+Lemma lessdef_parent_ra:
+ forall s v,
+ match_stack s -> Val.lessdef (parent_ra s) v -> v = parent_ra s.
+Proof.
+ intros. inv H0. auto. exploit parent_ra_def; eauto. tauto.
+Qed.
+
+End MATCH_STACK.
diff --git a/aarch64/Asmblockgenproof1.v b/aarch64/Asmblockgenproof1.v
new file mode 100644
index 00000000..61d77881
--- /dev/null
+++ b/aarch64/Asmblockgenproof1.v
@@ -0,0 +1,1955 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Proof of correctness for individual instructions *)
+
+Require Import Coqlib Errors Maps Zbits.
+Require Import AST Integers Floats Values Memory Globalenvs Linking.
+Require Import Op Locations Machblock Conventions Lia.
+Require Import Asmblock Asmblockgen Asmblockgenproof0 Asmblockprops.
+
+Module MB := Machblock.
+Module AB := Asmblock.
+
+Section CONSTRUCTORS.
+
+Variable lk: aarch64_linker.
+Variable ge: genv.
+Variable fn: function.
+
+Hypothesis symbol_high_low: forall (id: ident) (ofs: ptrofs),
+ Val.addl (symbol_high lk id ofs) (symbol_low lk id ofs) = Genv.symbol_address ge id ofs.
+
+Ltac Simplif :=
+ ((rewrite Pregmap.gss)
+ || (rewrite Pregmap.gso by eauto with asmgen)); auto with asmgen.
+
+Ltac Simpl := repeat Simplif.
+
+Ltac ArgsInv :=
+ repeat (match goal with
+ | [ H: Error _ = OK _ |- _ ] => discriminate
+ | [ H: match ?args with nil => _ | _ :: _ => _ end = OK _ |- _ ] => destruct args
+ | [ H: bind _ _ = OK _ |- _ ] => monadInv H
+ | [ H: match _ with left _ => _ | right _ => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
+ | [ H: match _ with true => _ | false => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
+ end);
+ subst;
+ repeat (match goal with
+ | [ H: ireg_of _ = OK _ |- _ ] => simpl in *; rewrite (ireg_of_eq _ _ H) in *
+ | [ H: freg_of _ = OK _ |- _ ] => simpl in *; rewrite (freg_of_eq _ _ H) in *
+ end).
+
+Ltac SimplEval H :=
+ match type of H with
+ | Some _ = None _ => discriminate
+ | Some _ = Some _ => inversion H; subst
+ | ?a = Some ?b => let A := fresh in assert (A: Val.maketotal a = b) by (rewrite H; reflexivity)
+end.
+
+Ltac TranslOpSimpl :=
+ econstructor; split;
+ [ apply exec_straight_one; reflexivity
+ | split; [ apply Val.lessdef_same; simpl; Simpl; fail | intros; simpl; Simpl; fail ] ].
+
+Ltac TranslOpSimplN :=
+ econstructor; split;
+ try apply exec_straight_one; try reflexivity; try split; try apply Val.lessdef_same;
+ Simpl; simpl; try destruct negb; Simpl; try intros; Simpl; simpl; try destruct negb; Simpl.
+
+Lemma preg_of_iregsp_not_PC: forall r, preg_of_iregsp r <> PC.
+Proof.
+ destruct r; simpl; try discriminate.
+Qed.
+Hint Resolve preg_of_iregsp_not_PC: asmgen.
+
+Lemma preg_of_not_X16: forall r, preg_of r <> X16.
+Proof.
+ destruct r; simpl; try discriminate.
+Qed.
+
+Lemma preg_of_not_X30: forall r, preg_of r <> X30.
+Proof.
+ destruct r; simpl; try discriminate.
+Qed.
+
+Lemma ireg_of_not_X16: forall r x, ireg_of r = OK x -> x <> X16.
+Proof.
+ unfold ireg_of; intros. destruct (preg_of r) eqn:E; inv H.
+ red; intros; subst x. elim (preg_of_not_X16 r); auto.
+ destruct d. destruct i. inv H1; auto.
+ all: discriminate.
+Qed.
+
+Lemma ireg_of_not_X16': forall r x, ireg_of r = OK x -> IR x <> IR X16.
+Proof.
+ intros. apply ireg_of_not_X16 in H. congruence.
+Qed.
+
+Lemma ireg_of_not_X16'': forall r x, ireg_of r = OK x -> DR (IR x) <> DR (IR X16).
+Proof.
+ intros. apply ireg_of_not_X16 in H. congruence.
+Qed.
+
+Lemma ireg_of_not_X30: forall r x, ireg_of r = OK x -> x <> X30.
+Proof.
+ unfold ireg_of; intros. destruct (preg_of r) eqn:E; inv H.
+ red; intros; subst x. elim (preg_of_not_X30 r); auto.
+ destruct d. destruct i. inv H1; auto.
+ all: discriminate.
+Qed.
+
+Lemma ireg_of_not_X30': forall r x, ireg_of r = OK x -> IR x <> IR X30.
+Proof.
+ intros. apply ireg_of_not_X30 in H. congruence.
+Qed.
+
+Lemma ireg_of_not_X30'': forall r x, ireg_of r = OK x -> DR (IR x) <> DR (IR X30).
+Proof.
+ intros. apply ireg_of_not_X30 in H. congruence.
+Qed.
+
+Hint Resolve preg_of_not_X16 ireg_of_not_X16 ireg_of_not_X16' ireg_of_not_X16'': asmgen.
+Hint Resolve preg_of_not_X30 ireg_of_not_X30 ireg_of_not_X30' ireg_of_not_X30'': asmgen.
+
+Inductive wf_decomposition: list (Z * Z) -> Prop :=
+ | wf_decomp_nil:
+ wf_decomposition nil
+ | wf_decomp_cons: forall m n p l,
+ n = Zzero_ext 16 m -> 0 <= p -> wf_decomposition l ->
+ wf_decomposition ((n, p) :: l).
+
+Lemma decompose_int_wf:
+ forall N n p, 0 <= p -> wf_decomposition (decompose_int N n p).
+Proof.
+Local Opaque Zzero_ext.
+ induction N as [ | N]; simpl; intros.
+ - constructor.
+ - set (frag := Zzero_ext 16 (Z.shiftr n p)) in *. destruct (Z.eqb frag 0).
+ + apply IHN. lia.
+ + econstructor. reflexivity. lia. apply IHN; lia.
+Qed.
+
+Fixpoint recompose_int (accu: Z) (l: list (Z * Z)) : Z :=
+ match l with
+ | nil => accu
+ | (n, p) :: l => recompose_int (Zinsert accu n p 16) l
+ end.
+
+Lemma decompose_int_correct:
+ forall N n p accu,
+ 0 <= p ->
+ (forall i, p <= i -> Z.testbit accu i = false) ->
+ (forall i, 0 <= i < p + Z.of_nat N * 16 ->
+ Z.testbit (recompose_int accu (decompose_int N n p)) i =
+ if zlt i p then Z.testbit accu i else Z.testbit n i).
+Proof.
+ induction N as [ | N]; intros until accu; intros PPOS ABOVE i RANGE.
+ - simpl. rewrite zlt_true; auto. lia.
+ - rewrite inj_S in RANGE. simpl.
+ set (frag := Zzero_ext 16 (Z.shiftr n p)).
+ assert (FRAG: forall i, p <= i < p + 16 -> Z.testbit n i = Z.testbit frag (i - p)).
+ { unfold frag; intros. rewrite Zzero_ext_spec by lia. rewrite zlt_true by lia.
+ rewrite Z.shiftr_spec by lia. f_equal; lia. }
+ destruct (Z.eqb_spec frag 0).
+ + rewrite IHN.
+ * destruct (zlt i p). rewrite zlt_true by lia. auto.
+ destruct (zlt i (p + 16)); auto.
+ rewrite ABOVE by lia. rewrite FRAG by lia. rewrite e, Z.testbit_0_l. auto.
+ * lia.
+ * intros; apply ABOVE; lia.
+ * lia.
+ + simpl. rewrite IHN.
+ * destruct (zlt i (p + 16)).
+ ** rewrite Zinsert_spec by lia. unfold proj_sumbool.
+ rewrite zlt_true by lia.
+ destruct (zlt i p).
+ rewrite zle_false by lia. auto.
+ rewrite zle_true by lia. simpl. symmetry; apply FRAG; lia.
+ ** rewrite Z.ldiff_spec, Z.shiftl_spec by lia.
+ change 65535 with (two_p 16 - 1). rewrite Ztestbit_two_p_m1 by lia.
+ rewrite zlt_false by lia. rewrite zlt_false by lia. apply andb_true_r.
+ * lia.
+ * intros. rewrite Zinsert_spec by lia. unfold proj_sumbool.
+ rewrite zle_true by lia. rewrite zlt_false by lia. simpl.
+ apply ABOVE. lia.
+ * lia.
+Qed.
+
+Corollary decompose_int_eqmod: forall N n,
+ eqmod (two_power_nat (N * 16)%nat) (recompose_int 0 (decompose_int N n 0)) n.
+Proof.
+ intros; apply eqmod_same_bits; intros.
+ rewrite decompose_int_correct. apply zlt_false; lia.
+ lia. intros; apply Z.testbit_0_l. lia.
+Qed.
+
+Corollary decompose_notint_eqmod: forall N n,
+ eqmod (two_power_nat (N * 16)%nat)
+ (Z.lnot (recompose_int 0 (decompose_int N (Z.lnot n) 0))) n.
+Proof.
+ intros; apply eqmod_same_bits; intros.
+ rewrite Z.lnot_spec, decompose_int_correct.
+ rewrite zlt_false by lia. rewrite Z.lnot_spec by lia. apply negb_involutive.
+ lia. intros; apply Z.testbit_0_l. lia. lia.
+Qed.
+
+Lemma negate_decomposition_wf:
+ forall l, wf_decomposition l -> wf_decomposition (negate_decomposition l).
+Proof.
+ induction 1; simpl; econstructor; auto.
+ instantiate (1 := (Z.lnot m)).
+ apply equal_same_bits; intros.
+ rewrite H. change 65535 with (two_p 16 - 1).
+ rewrite Z.lxor_spec, !Zzero_ext_spec, Z.lnot_spec, Ztestbit_two_p_m1 by lia.
+ destruct (zlt i 16).
+ apply xorb_true_r.
+ auto.
+Qed.
+
+Lemma Zinsert_eqmod:
+ forall n x1 x2 y p l, 0 <= p -> 0 <= l ->
+ eqmod (two_power_nat n) x1 x2 ->
+ eqmod (two_power_nat n) (Zinsert x1 y p l) (Zinsert x2 y p l).
+Proof.
+ intros. apply eqmod_same_bits; intros. rewrite ! Zinsert_spec by lia.
+ destruct (zle p i && zlt i (p + l)); auto.
+ apply same_bits_eqmod with n; auto.
+Qed.
+
+Lemma Zinsert_0_l:
+ forall y p l,
+ 0 <= p -> 0 <= l ->
+ Z.shiftl (Zzero_ext l y) p = Zinsert 0 (Zzero_ext l y) p l.
+Proof.
+ intros. apply equal_same_bits; intros.
+ rewrite Zinsert_spec by lia. unfold proj_sumbool.
+ destruct (zlt i p); [rewrite zle_false by lia|rewrite zle_true by lia]; simpl.
+ - rewrite Z.testbit_0_l, Z.shiftl_spec_low by auto. auto.
+ - rewrite Z.shiftl_spec by lia.
+ destruct (zlt i (p + l)); auto.
+ rewrite Zzero_ext_spec, zlt_false, Z.testbit_0_l by lia. auto.
+Qed.
+
+Lemma recompose_int_negated:
+ forall l, wf_decomposition l ->
+ forall accu, recompose_int (Z.lnot accu) (negate_decomposition l) = Z.lnot (recompose_int accu l).
+Proof.
+ induction 1; intros accu; simpl.
+ - auto.
+ - rewrite <- IHwf_decomposition. f_equal. apply equal_same_bits; intros.
+ rewrite Z.lnot_spec, ! Zinsert_spec, Z.lxor_spec, Z.lnot_spec by lia.
+ unfold proj_sumbool.
+ destruct (zle p i); simpl; auto.
+ destruct (zlt i (p + 16)); simpl; auto.
+ change 65535 with (two_p 16 - 1).
+ rewrite Ztestbit_two_p_m1 by lia. rewrite zlt_true by lia.
+ apply xorb_true_r.
+Qed.
+
+Lemma exec_loadimm_k_w:
+ forall (rd: ireg) k m l,
+ wf_decomposition l ->
+ forall (rs: regset) accu,
+ rs#rd = Vint (Int.repr accu) ->
+ exists rs',
+ exec_straight_opt ge lk (loadimm_k W rd l k) rs m k rs' m
+ /\ rs'#rd = Vint (Int.repr (recompose_int accu l))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ induction 1; intros rs accu ACCU; simpl.
+ - exists rs; split. apply exec_straight_opt_refl. auto.
+ - destruct (IHwf_decomposition
+ ((rs#rd <- (insert_in_int rs#rd n p 16)))
+ (Zinsert accu n p 16))
+ as (rs' & P & Q & R).
+ Simpl. rewrite ACCU. simpl. f_equal. apply Int.eqm_samerepr.
+ apply Zinsert_eqmod. auto. lia. apply Int.eqm_sym; apply Int.eqm_unsigned_repr.
+ exists rs'; split.
+ eapply exec_straight_opt_step_opt. simpl. eauto. auto.
+ split. exact Q. intros; Simpl. rewrite R by auto. Simpl.
+Qed.
+
+Lemma exec_loadimm_z_w:
+ forall rd l k rs m,
+ wf_decomposition l ->
+ exists rs',
+ exec_straight ge lk (loadimm_z W rd l k) rs m k rs' m
+ /\ rs'#rd = Vint (Int.repr (recompose_int 0 l))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm_z; destruct 1.
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+ - set (accu0 := Zinsert 0 n p 16).
+ set (rs1 := rs#rd <- (Vint (Int.repr accu0))).
+ destruct (exec_loadimm_k_w rd k m l H1 rs1 accu0) as (rs2 & P & Q & R); auto.
+ unfold rs1; Simpl.
+ exists rs2; split.
+ eapply exec_straight_opt_step; eauto.
+ simpl. unfold rs1. do 5 f_equal. unfold accu0. rewrite H. apply Zinsert_0_l; lia.
+ split. exact Q.
+ intros. rewrite R by auto. unfold rs1; Simpl.
+Qed.
+
+Lemma exec_loadimm_n_w:
+ forall rd l k rs m,
+ wf_decomposition l ->
+ exists rs',
+ exec_straight ge lk (loadimm_n W rd l k) rs m k rs' m
+ /\ rs'#rd = Vint (Int.repr (Z.lnot (recompose_int 0 l)))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm_n; destruct 1.
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+ - set (accu0 := Z.lnot (Zinsert 0 n p 16)).
+ set (rs1 := rs#rd <- (Vint (Int.repr accu0))).
+ destruct (exec_loadimm_k_w rd k m (negate_decomposition l)
+ (negate_decomposition_wf l H1)
+ rs1 accu0) as (rs2 & P & Q & R).
+ unfold rs1; Simpl.
+ exists rs2; split.
+ eapply exec_straight_opt_step; eauto.
+ simpl. unfold rs1. do 5 f_equal.
+ unfold accu0. f_equal. rewrite H. apply Zinsert_0_l; lia.
+ split. unfold accu0 in Q; rewrite recompose_int_negated in Q by auto. exact Q.
+ intros. rewrite R by auto. unfold rs1; Simpl.
+Qed.
+
+Lemma exec_loadimm32:
+ forall rd n k rs m,
+ exists rs',
+ exec_straight ge lk (loadimm32 rd n k) rs m k rs' m
+ /\ rs'#rd = Vint n
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm32, loadimm; intros.
+ destruct (is_logical_imm32 n).
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. rewrite Int.repr_unsigned, Int.or_zero_l; auto.
+ intros; Simpl.
+ - set (dz := decompose_int 2%nat (Int.unsigned n) 0).
+ set (dn := decompose_int 2%nat (Z.lnot (Int.unsigned n)) 0).
+ assert (A: Int.repr (recompose_int 0 dz) = n).
+ { transitivity (Int.repr (Int.unsigned n)).
+ apply Int.eqm_samerepr. apply decompose_int_eqmod.
+ apply Int.repr_unsigned. }
+ assert (B: Int.repr (Z.lnot (recompose_int 0 dn)) = n).
+ { transitivity (Int.repr (Int.unsigned n)).
+ apply Int.eqm_samerepr. apply decompose_notint_eqmod.
+ apply Int.repr_unsigned. }
+ destruct Nat.leb.
+ + rewrite <- A. apply exec_loadimm_z_w. apply decompose_int_wf; lia.
+ + rewrite <- B. apply exec_loadimm_n_w. apply decompose_int_wf; lia.
+Qed.
+
+Lemma exec_loadimm_k_x:
+ forall (rd: ireg) k m l,
+ wf_decomposition l ->
+ forall (rs: regset) accu,
+ rs#rd = Vlong (Int64.repr accu) ->
+ exists rs',
+ exec_straight_opt ge lk (loadimm_k X rd l k) rs m k rs' m
+ /\ rs'#rd = Vlong (Int64.repr (recompose_int accu l))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ induction 1; intros rs accu ACCU; simpl.
+ - exists rs; split. apply exec_straight_opt_refl. auto.
+ - destruct (IHwf_decomposition
+ (rs#rd <- (insert_in_long rs#rd n p 16))
+ (Zinsert accu n p 16))
+ as (rs' & P & Q & R).
+ Simpl. rewrite ACCU. simpl. f_equal. apply Int64.eqm_samerepr.
+ apply Zinsert_eqmod. auto. lia. apply Int64.eqm_sym; apply Int64.eqm_unsigned_repr.
+ exists rs'; split.
+ eapply exec_straight_opt_step_opt. simpl; eauto. auto.
+ split. exact Q. intros; Simpl. rewrite R by auto. Simpl.
+Qed.
+
+Lemma exec_loadimm_z_x:
+ forall rd l k rs m,
+ wf_decomposition l ->
+ exists rs',
+ exec_straight ge lk (loadimm_z X rd l k) rs m k rs' m
+ /\ rs'#rd = Vlong (Int64.repr (recompose_int 0 l))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm_z; destruct 1.
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+ - set (accu0 := Zinsert 0 n p 16).
+ set (rs1 := rs#rd <- (Vlong (Int64.repr accu0))).
+ destruct (exec_loadimm_k_x rd k m l H1 rs1 accu0) as (rs2 & P & Q & R); auto.
+ unfold rs1; Simpl.
+ exists rs2; split.
+ eapply exec_straight_opt_step; eauto.
+ simpl. unfold rs1. do 5 f_equal. unfold accu0. rewrite H. apply Zinsert_0_l; lia.
+ split. exact Q.
+ intros. rewrite R by auto. unfold rs1; Simpl.
+Qed.
+
+Lemma exec_loadimm_n_x:
+ forall rd l k rs m,
+ wf_decomposition l ->
+ exists rs',
+ exec_straight ge lk (loadimm_n X rd l k) rs m k rs' m
+ /\ rs'#rd = Vlong (Int64.repr (Z.lnot (recompose_int 0 l)))
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm_n; destruct 1.
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+ - set (accu0 := Z.lnot (Zinsert 0 n p 16)).
+ set (rs1 := rs#rd <- (Vlong (Int64.repr accu0))).
+ destruct (exec_loadimm_k_x rd k m (negate_decomposition l)
+ (negate_decomposition_wf l H1)
+ rs1 accu0) as (rs2 & P & Q & R).
+ unfold rs1; Simpl.
+ exists rs2; split.
+ eapply exec_straight_opt_step; eauto.
+ simpl. unfold rs1. do 5 f_equal.
+ unfold accu0. f_equal. rewrite H. apply Zinsert_0_l; lia.
+ split. unfold accu0 in Q; rewrite recompose_int_negated in Q by auto. exact Q.
+ intros. rewrite R by auto. unfold rs1; Simpl.
+Qed.
+
+Lemma exec_loadimm64:
+ forall rd n k rs m,
+ exists rs',
+ exec_straight ge lk (loadimm64 rd n k) rs m k rs' m
+ /\ rs'#rd = Vlong n
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm64, loadimm; intros.
+ destruct (is_logical_imm64 n).
+ - econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. rewrite Int64.repr_unsigned, Int64.or_zero_l; auto.
+ intros; Simpl.
+ - set (dz := decompose_int 4%nat (Int64.unsigned n) 0).
+ set (dn := decompose_int 4%nat (Z.lnot (Int64.unsigned n)) 0).
+ assert (A: Int64.repr (recompose_int 0 dz) = n).
+ { transitivity (Int64.repr (Int64.unsigned n)).
+ apply Int64.eqm_samerepr. apply decompose_int_eqmod.
+ apply Int64.repr_unsigned. }
+ assert (B: Int64.repr (Z.lnot (recompose_int 0 dn)) = n).
+ { transitivity (Int64.repr (Int64.unsigned n)).
+ apply Int64.eqm_samerepr. apply decompose_notint_eqmod.
+ apply Int64.repr_unsigned. }
+ destruct Nat.leb.
+ + rewrite <- A. apply exec_loadimm_z_x. apply decompose_int_wf; lia.
+ + rewrite <- B. apply exec_loadimm_n_x. apply decompose_int_wf; lia.
+Qed.
+
+(** Add immediate *)
+
+Lemma exec_addimm_aux_32:
+ forall (insn: Z -> arith_pp) (sem: val -> val -> val),
+ (forall rd r1 n rs m,
+ exec_basic lk ge (PArith (PArithPP (insn n) rd r1)) rs m =
+ Next (rs#rd <- (sem rs#r1 (Vint (Int.repr n)))) m) ->
+ (forall v n1 n2, sem (sem v (Vint n1)) (Vint n2) = sem v (Vint (Int.add n1 n2))) ->
+ forall rd r1 n k rs m,
+ exists rs',
+ exec_straight ge lk (addimm_aux insn rd r1 (Int.unsigned n) k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Vint n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros insn sem SEM ASSOC; intros. unfold addimm_aux.
+ set (nlo := Zzero_ext 12 (Int.unsigned n)). set (nhi := Int.unsigned n - nlo).
+ assert (E: Int.unsigned n = nhi + nlo) by (unfold nhi; lia).
+ rewrite <- (Int.repr_unsigned n).
+ destruct (Z.eqb_spec nhi 0); [|destruct (Z.eqb_spec nlo 0)].
+ - econstructor; split. apply exec_straight_one. apply SEM. Simpl.
+ split. Simpl. do 3 f_equal; lia.
+ intros; Simpl.
+ - econstructor; split. apply exec_straight_one. apply SEM. Simpl.
+ split. Simpl. do 3 f_equal; lia.
+ intros; Simpl.
+ - econstructor; split. eapply exec_straight_two.
+ apply SEM. apply SEM. simpl. Simpl.
+ split. Simpl. simpl. rewrite ASSOC. do 2 f_equal. apply Int.eqm_samerepr.
+ rewrite E. auto with ints.
+ intros; Simpl.
+Qed.
+
+Lemma exec_addimm32:
+ forall (rd r1: ireg) n k rs m,
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (addimm32 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = Val.add rs#r1 (Vint n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. unfold addimm32. set (nn := Int.neg n).
+ destruct (Int.eq n (Int.zero_ext 24 n)); [| destruct (Int.eq nn (Int.zero_ext 24 nn))].
+ - apply exec_addimm_aux_32 with (sem := Val.add). auto. intros; apply Val.add_assoc.
+ - rewrite <- Val.sub_opp_add.
+ apply exec_addimm_aux_32 with (sem := Val.sub). auto.
+ intros. rewrite ! Val.sub_add_opp, Val.add_assoc. rewrite Int.neg_add_distr. auto.
+ - destruct (Int.lt n Int.zero).
+ + rewrite <- Val.sub_opp_add; fold nn.
+ edestruct (exec_loadimm32 X16 nn) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. eapply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. rewrite B, C; eauto with asmgen.
+ intros; Simpl.
+ + edestruct (exec_loadimm32 X16 n) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. eapply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. rewrite B, C; eauto with asmgen.
+ intros; Simpl.
+Qed.
+
+Lemma exec_addimm_aux_64:
+ forall (insn: Z -> arith_pp) (sem: val -> val -> val),
+ (forall rd r1 n rs m,
+ exec_basic lk ge (PArith (PArithPP (insn n) rd r1)) rs m =
+ Next (rs#rd <- (sem rs#r1 (Vlong (Int64.repr n)))) m) ->
+ (forall v n1 n2, sem (sem v (Vlong n1)) (Vlong n2) = sem v (Vlong (Int64.add n1 n2))) ->
+ forall rd r1 n k rs m,
+ exists rs',
+ exec_straight ge lk (addimm_aux insn rd r1 (Int64.unsigned n) k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Vlong n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros insn sem SEM ASSOC; intros. unfold addimm_aux.
+ set (nlo := Zzero_ext 12 (Int64.unsigned n)). set (nhi := Int64.unsigned n - nlo).
+ assert (E: Int64.unsigned n = nhi + nlo) by (unfold nhi; lia).
+ rewrite <- (Int64.repr_unsigned n).
+ destruct (Z.eqb_spec nhi 0); [|destruct (Z.eqb_spec nlo 0)].
+ - econstructor; split. apply exec_straight_one. apply SEM. Simpl.
+ split. Simpl. do 3 f_equal; lia.
+ intros; Simpl.
+ - econstructor; split. apply exec_straight_one. apply SEM. Simpl.
+ split. Simpl. do 3 f_equal; lia.
+ intros; Simpl.
+ - econstructor; split. eapply exec_straight_two.
+ apply SEM. apply SEM. Simpl. Simpl.
+ split. Simpl. rewrite ASSOC. do 2 f_equal. apply Int64.eqm_samerepr.
+ rewrite E. auto with ints.
+ intros; Simpl.
+Qed.
+
+Lemma exec_addimm64:
+ forall (rd r1: iregsp) n k rs m,
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (addimm64 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = Val.addl rs#r1 (Vlong n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros.
+ unfold addimm64. set (nn := Int64.neg n).
+ destruct (Int64.eq n (Int64.zero_ext 24 n)); [| destruct (Int64.eq nn (Int64.zero_ext 24 nn))].
+ - apply exec_addimm_aux_64 with (sem := Val.addl). auto. intros; apply Val.addl_assoc.
+ - rewrite <- Val.subl_opp_addl.
+ apply exec_addimm_aux_64 with (sem := Val.subl). auto.
+ intros. rewrite ! Val.subl_addl_opp, Val.addl_assoc. rewrite Int64.neg_add_distr. auto.
+ - destruct (Int64.lt n Int64.zero).
+ + rewrite <- Val.subl_opp_addl; fold nn.
+ edestruct (exec_loadimm64 X16 nn) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. eapply exec_straight_one. simpl; eauto. Simpl.
+ split. Simpl. rewrite B, C; eauto with asmgen. simpl. rewrite Int64.shl'_zero. auto.
+ intros; Simpl.
+ + edestruct (exec_loadimm64 X16 n) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. eapply exec_straight_one. simpl; eauto. Simpl.
+ split. Simpl. rewrite B, C; eauto with asmgen. simpl. rewrite Int64.shl'_zero. auto.
+ intros; Simpl.
+Qed.
+
+(** Logical immediate *)
+
+Lemma exec_logicalimm32:
+ forall (insn1: Z -> arith_rr0)
+ (insn2: shift_op -> arith_rr0r)
+ (sem: val -> val -> val),
+ (forall rd r1 n rs m,
+ exec_basic lk ge (PArith (PArithRR0 (insn1 n) rd r1)) rs m =
+ Next (rs#rd <- (sem rs##r1 (Vint (Int.repr n)))) m) ->
+ (forall rd r1 r2 s rs m,
+ exec_basic lk ge (PArith (PArithRR0R (insn2 s) rd r1 r2)) rs m =
+ Next (rs#rd <- (sem rs##r1 (eval_shift_op_int rs#r2 s))) m) ->
+ forall rd r1 n k rs m,
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (logicalimm32 insn1 insn2 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Vint n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until sem; intros SEM1 SEM2; intros. unfold logicalimm32.
+ destruct (is_logical_imm32 n).
+ - econstructor; split.
+ apply exec_straight_one. apply SEM1.
+ split. Simpl. rewrite Int.repr_unsigned; auto. intros; Simpl.
+ - edestruct (exec_loadimm32 X16 n) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. apply SEM2.
+ split. Simpl. f_equal; auto. apply C; auto with asmgen.
+ intros; Simpl.
+Qed.
+
+Lemma exec_logicalimm64:
+ forall (insn1: Z -> arith_rr0)
+ (insn2: shift_op -> arith_rr0r)
+ (sem: val -> val -> val),
+ (forall rd r1 n rs m,
+ exec_basic lk ge (PArith (PArithRR0 (insn1 n) rd r1)) rs m =
+ Next (rs#rd <- (sem rs###r1 (Vlong (Int64.repr n)))) m) ->
+ (forall rd r1 r2 s rs m,
+ exec_basic lk ge (PArith (PArithRR0R (insn2 s) rd r1 r2)) rs m =
+ Next (rs#rd <- (sem rs###r1 (eval_shift_op_long rs#r2 s))) m) ->
+ forall rd r1 n k rs m,
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (logicalimm64 insn1 insn2 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Vlong n)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until sem; intros SEM1 SEM2; intros. unfold logicalimm64.
+ destruct (is_logical_imm64 n).
+ - econstructor; split.
+ apply exec_straight_one. apply SEM1.
+ split. Simpl. rewrite Int64.repr_unsigned. auto. intros; Simpl.
+ - edestruct (exec_loadimm64 X16 n) as (rs1 & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. apply SEM2.
+ split. Simpl. f_equal; auto. apply C; auto with asmgen.
+ intros; Simpl.
+Qed.
+
+(** Load address of symbol *)
+
+Lemma exec_loadsymbol: forall rd s ofs k rs m,
+ rd <> X16 \/ Archi.pic_code tt = false ->
+ exists rs',
+ exec_straight ge lk (loadsymbol rd s ofs k) rs m k rs' m
+ /\ rs'#rd = Genv.symbol_address ge s ofs
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadsymbol; intros. destruct (Archi.pic_code tt).
+ - predSpec Ptrofs.eq Ptrofs.eq_spec ofs Ptrofs.zero.
+ + subst ofs. econstructor; split.
+ apply exec_straight_one. simpl; eauto.
+ split. Simpl. intros; Simpl.
+ + exploit exec_addimm64. instantiate (1 := rd). simpl. destruct H; congruence.
+ intros (rs1 & A & B & C).
+ econstructor; split.
+ econstructor. simpl; eauto. auto. eexact A.
+ split. simpl in B; rewrite B. Simpl.
+ rewrite <- Genv.shift_symbol_address_64 by auto.
+ rewrite Ptrofs.add_zero_l, Ptrofs.of_int64_to_int64 by auto. auto.
+ intros. rewrite C by auto. Simpl.
+ - econstructor; split.
+ eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
+ split. Simpl.
+ intros; Simpl.
+Qed.
+
+(** Shifted operands *)
+
+Remark transl_shift_not_none:
+ forall s a, transl_shift s a <> SOnone.
+Proof.
+ destruct s; intros; simpl; congruence.
+Qed.
+
+Remark or_zero_eval_shift_op_int:
+ forall v s, s <> SOnone -> Val.or (Vint Int.zero) (eval_shift_op_int v s) = eval_shift_op_int v s.
+Proof.
+ intros; destruct s; try congruence; destruct v; auto; simpl;
+ destruct (Int.ltu n Int.iwordsize); auto; rewrite Int.or_zero_l; auto.
+Qed.
+
+Remark or_zero_eval_shift_op_long:
+ forall v s, s <> SOnone -> Val.orl (Vlong Int64.zero) (eval_shift_op_long v s) = eval_shift_op_long v s.
+Proof.
+ intros; destruct s; try congruence; destruct v; auto; simpl;
+ destruct (Int.ltu n Int64.iwordsize'); auto; rewrite Int64.or_zero_l; auto.
+Qed.
+
+Remark add_zero_eval_shift_op_long:
+ forall v s, s <> SOnone -> Val.addl (Vlong Int64.zero) (eval_shift_op_long v s) = eval_shift_op_long v s.
+Proof.
+ intros; destruct s; try congruence; destruct v; auto; simpl;
+ destruct (Int.ltu n Int64.iwordsize'); auto; rewrite Int64.add_zero_l; auto.
+Qed.
+
+Lemma transl_eval_shift: forall s v (a: amount32),
+ eval_shift_op_int v (transl_shift s a) = eval_shift s v a.
+Proof.
+ intros. destruct s; simpl; auto.
+Qed.
+
+Lemma transl_eval_shift': forall s v (a: amount32),
+ Val.or (Vint Int.zero) (eval_shift_op_int v (transl_shift s a)) = eval_shift s v a.
+Proof.
+ intros. rewrite or_zero_eval_shift_op_int by (apply transl_shift_not_none).
+ apply transl_eval_shift.
+Qed.
+
+Lemma transl_eval_shiftl: forall s v (a: amount64),
+ eval_shift_op_long v (transl_shift s a) = eval_shiftl s v a.
+Proof.
+ intros. destruct s; simpl; auto.
+Qed.
+
+Lemma transl_eval_shiftl': forall s v (a: amount64),
+ Val.orl (Vlong Int64.zero) (eval_shift_op_long v (transl_shift s a)) = eval_shiftl s v a.
+Proof.
+ intros. rewrite or_zero_eval_shift_op_long by (apply transl_shift_not_none).
+ apply transl_eval_shiftl.
+Qed.
+
+Lemma transl_eval_shiftl'': forall s v (a: amount64),
+ Val.addl (Vlong Int64.zero) (eval_shift_op_long v (transl_shift s a)) = eval_shiftl s v a.
+Proof.
+ intros. rewrite add_zero_eval_shift_op_long by (apply transl_shift_not_none).
+ apply transl_eval_shiftl.
+Qed.
+
+(** Zero- and Sign- extensions *)
+
+Lemma exec_move_extended_base: forall rd r1 ex k rs m,
+ exists rs',
+ exec_straight ge lk (move_extended_base rd r1 ex k) rs m k rs' m
+ /\ rs' rd = match ex with Xsgn32 => Val.longofint rs#r1 | Xuns32 => Val.longofintu rs#r1 end
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold move_extended_base; destruct ex; econstructor;
+ (split; [apply exec_straight_one; simpl; eauto | split; [Simpl|intros;Simpl]]).
+Qed.
+
+Lemma exec_move_extended: forall rd r1 ex (a: amount64) k rs m,
+ exists rs',
+ exec_straight ge lk (move_extended rd r1 ex a k) rs m k rs' m
+ /\ rs' rd = Op.eval_extend ex rs#r1 a
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold move_extended; intros. predSpec Int.eq Int.eq_spec a Int.zero.
+ - exploit (exec_move_extended_base rd r1 ex). intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. unfold Op.eval_extend. rewrite H. rewrite B.
+ destruct ex, (rs r1); simpl; auto; rewrite Int64.shl'_zero; auto.
+ auto.
+ - Local Opaque Val.addl.
+ exploit (exec_move_extended_base rd r1 ex). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ change (SOlsl a) with (transl_shift Slsl a). rewrite transl_eval_shiftl''. eauto. auto.
+ split. Simpl. rewrite B. auto.
+ intros; Simpl.
+Qed.
+
+Lemma exec_arith_extended:
+ forall (sem: val -> val -> val)
+ (insnX: extend_op -> arith_ppp)
+ (insnS: shift_op -> arith_rr0r),
+ (forall rd r1 r2 x rs m,
+ exec_basic lk ge (PArith (PArithPPP (insnX x) rd r1 r2)) rs m =
+ Next (rs#rd <- (sem rs#r1 (eval_extend rs#r2 x))) m) ->
+ (forall rd r1 r2 s rs m,
+ exec_basic lk ge (PArith (PArithRR0R (insnS s) rd r1 r2)) rs m =
+ Next (rs#rd <- (sem rs###r1 (eval_shift_op_long rs#r2 s))) m) ->
+ forall (rd r1 r2: ireg) (ex: extension) (a: amount64) (k: bcode) rs m,
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (arith_extended insnX insnS rd r1 r2 ex a k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Op.eval_extend ex rs#r2 a)
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros sem insnX insnS EX ES; intros. unfold arith_extended. destruct (Int.ltu a (Int.repr 5)).
+ - econstructor; split.
+ apply exec_straight_one. rewrite EX; eauto. auto.
+ split. Simpl. f_equal. destruct ex; auto.
+ intros; Simpl.
+ - exploit (exec_move_extended_base X16 r2 ex). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ rewrite ES. eauto. auto.
+ split. Simpl. unfold ir0. rewrite C by eauto with asmgen. f_equal.
+ rewrite B. destruct ex; auto.
+ intros; Simpl.
+Qed.
+
+(** Extended right shift *)
+
+Lemma exec_shrx32: forall (rd r1: ireg) (n: int) k v (rs: regset) m,
+ Val.shrx rs#r1 (Vint n) = Some v ->
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (shrx32 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = v
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold shrx32; intros. apply Val.shrx_shr_2 in H.
+ destruct (Int.eq n Int.zero) eqn:E0.
+ - econstructor; split. apply exec_straight_one; simpl; eauto.
+ split. Simpl. subst v; auto. intros; Simpl.
+ - econstructor; split. eapply exec_straight_three.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_int by congruence. eauto.
+ simpl; eauto.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_int by congruence. eauto.
+ split. subst v; Simpl. intros; Simpl.
+Qed.
+
+Lemma exec_shrx32_none: forall (rd r1: ireg) (n: int) k x (rs: regset) m,
+ Val.shrx rs#r1 (Vint n) = None ->
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (shrx32 rd r1 n k) rs m k rs' m
+ /\ Val.lessdef (Val.maketotal None) (rs' x)
+ /\ forall r, data_preg r = true -> r <> rd -> preg_notin r (destroyed_by_op (Oshrximm n)) -> rs'#r = rs#r.
+Proof.
+ unfold shrx32; intros.
+ destruct (Int.eq n Int.zero) eqn:E0.
+ - econstructor; split. apply exec_straight_one; simpl; eauto.
+ split. Simpl. auto. intros; Simpl.
+ - econstructor; split. eapply exec_straight_three.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_int by congruence. eauto.
+ simpl; eauto.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_int by congruence. eauto.
+ split. Simpl. intros; Simpl.
+Qed.
+
+Lemma exec_shrx64: forall (rd r1: ireg) (n: int) k v (rs: regset) m,
+ Val.shrxl rs#r1 (Vint n) = Some v ->
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (shrx64 rd r1 n k) rs m k rs' m
+ /\ rs'#rd = v
+ /\ forall r, data_preg r = true -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold shrx64; intros. apply Val.shrxl_shrl_2 in H.
+ destruct (Int.eq n Int.zero) eqn:E.
+ - econstructor; split. apply exec_straight_one; simpl; eauto.
+ split. Simpl. subst v; auto. intros; Simpl.
+ - econstructor; split. eapply exec_straight_three.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_long by congruence. eauto.
+ simpl; eauto.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_long by congruence. eauto.
+ split. subst v; Simpl. intros; Simpl.
+Qed.
+
+Lemma exec_shrx64_none: forall (rd r1: ireg) (n: int) k x (rs: regset) m,
+ Val.shrxl rs#r1 (Vint n) = None ->
+ r1 <> X16 ->
+ exists rs',
+ exec_straight ge lk (shrx64 rd r1 n k) rs m k rs' m
+ /\ Val.lessdef (Val.maketotal None) (rs' x)
+ /\ forall r, data_preg r = true -> r <> rd -> preg_notin r (destroyed_by_op (Oshrximm n)) -> rs'#r = rs#r.
+Proof.
+ unfold shrx64; intros.
+ destruct (Int.eq n Int.zero) eqn:E.
+ - econstructor; split. apply exec_straight_one; simpl; eauto.
+ split. Simpl. auto. intros; Simpl.
+ - econstructor; split. eapply exec_straight_three.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_long by congruence. eauto.
+ simpl; eauto.
+ unfold exec_basic, exec_arith_instr, arith_eval_rr0r.
+ rewrite or_zero_eval_shift_op_long by congruence. eauto.
+ split. Simpl. intros; Simpl.
+Qed.
+
+Ltac TranslOpBase :=
+ econstructor; split;
+ [ apply exec_straight_one; [simpl; eauto ]
+ | split; [ rewrite ? transl_eval_shift, ? transl_eval_shiftl; Simpl
+ | intros; Simpl; fail ] ].
+
+(** Condition bits *)
+
+Lemma compare_int_spec: forall rs v1 v2,
+ let rs' := compare_int rs v1 v2 in
+ rs'#CN = (Val.negative (Val.sub v1 v2))
+ /\ rs'#CZ = (Val.mxcmpu Ceq v1 v2)
+ /\ rs'#CC = (Val.mxcmpu Cge v1 v2)
+ /\ rs'#CV = (Val.sub_overflow v1 v2).
+Proof.
+ intros; unfold rs'; auto.
+Qed.
+
+Lemma eval_testcond_compare_sint: forall c v1 v2 b rs,
+ Val.cmp_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_signed_cmp c) (compare_int rs v1 v2) = Some b.
+Proof.
+ intros. generalize (compare_int_spec rs v1 v2).
+ set (rs' := compare_int rs v1 v2). intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+ destruct v1; try discriminate; destruct v2; try discriminate.
+ simpl in H; inv H.
+ unfold Val.mxcmpu; simpl. destruct c; simpl.
+ - destruct (Int.eq i i0); auto.
+ - destruct (Int.eq i i0); auto.
+ - rewrite Int.lt_sub_overflow. destruct (Int.lt i i0); auto.
+ - rewrite Int.lt_sub_overflow, Int.not_lt.
+ destruct (Int.eq i i0), (Int.lt i i0); auto.
+ - rewrite Int.lt_sub_overflow, (Int.lt_not i).
+ destruct (Int.eq i i0), (Int.lt i i0); auto.
+ - rewrite Int.lt_sub_overflow. destruct (Int.lt i i0); auto.
+Qed.
+
+Lemma eval_testcond_compare_uint: forall c v1 v2 b rs,
+ Val.mxcmpu_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_unsigned_cmp c) (compare_int rs v1 v2) = Some b.
+Proof.
+ intros. generalize (compare_int_spec rs v1 v2).
+ set (rs' := compare_int rs v1 v2). intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+ destruct v1; try discriminate; destruct v2; try discriminate.
+ simpl in H; inv H.
+ unfold Val.mxcmpu; simpl. destruct c; simpl.
+ - destruct (Int.eq i i0); auto.
+ - destruct (Int.eq i i0); auto.
+ - destruct (Int.ltu i i0); auto.
+ - rewrite (Int.not_ltu i). destruct (Int.eq i i0), (Int.ltu i i0); auto.
+ - rewrite (Int.ltu_not i). destruct (Int.eq i i0), (Int.ltu i i0); auto.
+ - destruct (Int.ltu i i0); auto.
+Qed.
+
+Lemma compare_long_spec: forall rs v1 v2,
+ let rs' := compare_long rs v1 v2 in
+ rs'#CN = (Val.negativel (Val.subl v1 v2))
+ /\ rs'#CZ = (Val.mxcmplu Ceq v1 v2)
+ /\ rs'#CC = (Val.mxcmplu Cge v1 v2)
+ /\ rs'#CV = (Val.subl_overflow v1 v2).
+Proof.
+ intros; unfold rs'; auto.
+Qed.
+
+Remark int64_sub_overflow:
+ forall x y,
+ Int.xor (Int.repr (Int64.unsigned (Int64.sub_overflow x y Int64.zero)))
+ (Int.repr (Int64.unsigned (Int64.negative (Int64.sub x y)))) =
+ (if Int64.lt x y then Int.one else Int.zero).
+Proof.
+ intros.
+ transitivity (Int.repr (Int64.unsigned (if Int64.lt x y then Int64.one else Int64.zero))).
+ rewrite <- (Int64.lt_sub_overflow x y).
+ unfold Int64.sub_overflow, Int64.negative.
+ set (s := Int64.signed x - Int64.signed y - Int64.signed Int64.zero).
+ destruct (zle Int64.min_signed s && zle s Int64.max_signed);
+ destruct (Int64.lt (Int64.sub x y) Int64.zero);
+ auto.
+ destruct (Int64.lt x y); auto.
+Qed.
+
+Lemma eval_testcond_compare_slong: forall c v1 v2 b rs,
+ Val.cmpl_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_signed_cmp c) (compare_long rs v1 v2) = Some b.
+Proof.
+ intros. generalize (compare_long_spec rs v1 v2).
+ set (rs' := compare_long rs v1 v2). intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+ destruct v1; try discriminate; destruct v2; try discriminate.
+ simpl in H; inv H.
+ unfold Val.mxcmplu; simpl. destruct c; simpl.
+ - destruct (Int64.eq i i0); auto.
+ - destruct (Int64.eq i i0); auto.
+ - rewrite int64_sub_overflow. destruct (Int64.lt i i0); auto.
+ - rewrite int64_sub_overflow, Int64.not_lt.
+ destruct (Int64.eq i i0), (Int64.lt i i0); auto.
+ - rewrite int64_sub_overflow, (Int64.lt_not i).
+ destruct (Int64.eq i i0), (Int64.lt i i0); auto.
+ - rewrite int64_sub_overflow. destruct (Int64.lt i i0); auto.
+Qed.
+
+Lemma eval_testcond_compare_ulong: forall c v1 v2 b rs,
+ Val.mxcmplu_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_unsigned_cmp c) (compare_long rs v1 v2) = Some b.
+Proof.
+ intros. generalize (compare_long_spec rs v1 v2).
+ set (rs' := compare_long rs v1 v2). intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E; unfold Val.mxcmplu.
+ destruct v1; try discriminate; destruct v2; try discriminate; simpl in H.
+ - (* int-int *)
+ inv H. destruct c; simpl.
+ + destruct (Int64.eq i i0); auto.
+ + destruct (Int64.eq i i0); auto.
+ + destruct (Int64.ltu i i0); auto.
+ + rewrite (Int64.not_ltu i). destruct (Int64.eq i i0), (Int64.ltu i i0); auto.
+ + rewrite (Int64.ltu_not i). destruct (Int64.eq i i0), (Int64.ltu i i0); auto.
+ + destruct (Int64.ltu i i0); auto.
+ - (* int-ptr *)
+ simpl.
+ destruct (Archi.ptr64); simpl; try discriminate.
+ destruct (Int64.eq i Int64.zero); simpl; try discriminate.
+ destruct c; simpl in H; inv H; reflexivity.
+ - (* ptr-int *)
+ simpl.
+ destruct (Archi.ptr64); simpl; try discriminate.
+ destruct (Int64.eq i0 Int64.zero); try discriminate.
+ destruct c; simpl in H; inv H; reflexivity.
+ - (* ptr-ptr *)
+ simpl.
+ destruct (eq_block b0 b1).
+ destruct (Archi.ptr64); simpl; try discriminate.
+ inv H.
+ destruct c; simpl.
+ * destruct (Ptrofs.eq i i0); auto.
+ * destruct (Ptrofs.eq i i0); auto.
+ * destruct (Ptrofs.ltu i i0); auto.
+ * rewrite (Ptrofs.not_ltu i). destruct (Ptrofs.eq i i0), (Ptrofs.ltu i i0); auto.
+ * rewrite (Ptrofs.ltu_not i). destruct (Ptrofs.eq i i0), (Ptrofs.ltu i i0); auto.
+ * destruct (Ptrofs.ltu i i0); auto.
+ * destruct c; simpl in H; inv H; reflexivity.
+Qed.
+
+Lemma compare_float_spec: forall rs f1 f2,
+ let rs' := compare_float rs (Vfloat f1) (Vfloat f2) in
+ rs'#CN = (Val.of_bool (Float.cmp Clt f1 f2))
+ /\ rs'#CZ = (Val.of_bool (Float.cmp Ceq f1 f2))
+ /\ rs'#CC = (Val.of_bool (negb (Float.cmp Clt f1 f2)))
+ /\ rs'#CV = (Val.of_bool (negb (Float.ordered f1 f2))).
+Proof.
+ intros; auto.
+Qed.
+
+Lemma eval_testcond_compare_float: forall c v1 v2 b rs,
+ Val.cmpf_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_float_cmp c) (compare_float rs v1 v2) = Some b.
+Proof.
+ intros. destruct v1; try discriminate; destruct v2; simpl in H; inv H.
+ generalize (compare_float_spec rs f f0).
+ set (rs' := compare_float rs (Vfloat f) (Vfloat f0)).
+ intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+Local Transparent Float.cmp Float.ordered.
+ unfold Float.cmp, Float.ordered;
+ destruct c; destruct (Float.compare f f0) as [[]|]; reflexivity.
+Qed.
+
+Lemma eval_testcond_compare_not_float: forall c v1 v2 b rs,
+ option_map negb (Val.cmpf_bool c v1 v2) = Some b ->
+ eval_testcond (cond_for_float_not_cmp c) (compare_float rs v1 v2) = Some b.
+Proof.
+ intros. destruct v1; try discriminate; destruct v2; simpl in H; inv H.
+ generalize (compare_float_spec rs f f0).
+ set (rs' := compare_float rs (Vfloat f) (Vfloat f0)).
+ intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+Local Transparent Float.cmp Float.ordered.
+ unfold Float.cmp, Float.ordered;
+ destruct c; destruct (Float.compare f f0) as [[]|]; reflexivity.
+Qed.
+
+Lemma compare_single_spec: forall rs f1 f2,
+ let rs' := compare_single rs (Vsingle f1) (Vsingle f2) in
+ rs'#CN = (Val.of_bool (Float32.cmp Clt f1 f2))
+ /\ rs'#CZ = (Val.of_bool (Float32.cmp Ceq f1 f2))
+ /\ rs'#CC = (Val.of_bool (negb (Float32.cmp Clt f1 f2)))
+ /\ rs'#CV = (Val.of_bool (negb (Float32.ordered f1 f2))).
+Proof.
+ intros; auto.
+Qed.
+
+Lemma eval_testcond_compare_single: forall c v1 v2 b rs,
+ Val.cmpfs_bool c v1 v2 = Some b ->
+ eval_testcond (cond_for_float_cmp c) (compare_single rs v1 v2) = Some b.
+Proof.
+ intros. destruct v1; try discriminate; destruct v2; simpl in H; inv H.
+ generalize (compare_single_spec rs f f0).
+ set (rs' := compare_single rs (Vsingle f) (Vsingle f0)).
+ intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+Local Transparent Float32.cmp Float32.ordered.
+ unfold Float32.cmp, Float32.ordered;
+ destruct c; destruct (Float32.compare f f0) as [[]|]; reflexivity.
+Qed.
+
+Lemma eval_testcond_compare_not_single: forall c v1 v2 b rs,
+ option_map negb (Val.cmpfs_bool c v1 v2) = Some b ->
+ eval_testcond (cond_for_float_not_cmp c) (compare_single rs v1 v2) = Some b.
+Proof.
+ intros. destruct v1; try discriminate; destruct v2; simpl in H; inv H.
+ generalize (compare_single_spec rs f f0).
+ set (rs' := compare_single rs (Vsingle f) (Vsingle f0)).
+ intros (B & C & D & E).
+ unfold eval_testcond; rewrite B, C, D, E.
+Local Transparent Float32.cmp Float32.ordered.
+ unfold Float32.cmp, Float32.ordered;
+ destruct c; destruct (Float32.compare f f0) as [[]|]; reflexivity.
+Qed.
+
+Remark compare_float_inv: forall rs v1 v2 r,
+ match r with CR _ => False | _ => True end ->
+ (compare_float rs v1 v2)#r = rs#r.
+Proof.
+ intros; unfold compare_float.
+ destruct r; try contradiction; destruct v1; auto; destruct v2; auto.
+Qed.
+
+Remark compare_single_inv: forall rs v1 v2 r,
+ match r with CR _ => False | _ => True end ->
+ (compare_single rs v1 v2)#r = rs#r.
+Proof.
+ intros; unfold compare_single.
+ destruct r; try contradiction; destruct v1; auto; destruct v2; auto.
+Qed.
+
+Lemma transl_cond_correct:
+ forall cond args k c rs m,
+ transl_cond cond args k = OK c ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m
+ /\ (forall b,
+ eval_condition cond (map rs (map preg_of args)) m = Some b ->
+ eval_testcond (cond_for_cond cond) rs' = Some b)
+ /\ forall r, data_preg r = true -> rs'#r = rs#r.
+Proof.
+ intros until m; intros TR. destruct cond; simpl in TR; ArgsInv.
+ - (* Ccomp *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_sint; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccompu *)
+ econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. apply eval_testcond_compare_uint; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccompimm *)
+ destruct (is_arith_imm32 n); [|destruct (is_arith_imm32 (Int.neg n))].
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int.repr_unsigned. apply eval_testcond_compare_sint; auto.
+ destruct r; reflexivity || discriminate.
+ + econstructor; split.
+ apply exec_straight_one. simpl. rewrite Int.repr_unsigned, Int.neg_involutive. eauto. auto.
+ split; intros. apply eval_testcond_compare_sint; auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm32 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ simpl. rewrite B, C by eauto with asmgen. eauto.
+ split; intros. apply eval_testcond_compare_sint; auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccompuimm *)
+ destruct (is_arith_imm32 n); [|destruct (is_arith_imm32 (Int.neg n))].
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int.repr_unsigned. apply eval_testcond_compare_uint; auto.
+ destruct r; reflexivity || discriminate.
+ + econstructor; split.
+ apply exec_straight_one. simpl. rewrite Int.repr_unsigned, Int.neg_involutive. eauto. auto.
+ split; intros. apply eval_testcond_compare_uint; auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm32 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ simpl. rewrite B, C by eauto with asmgen. eauto. auto.
+ split; intros. apply eval_testcond_compare_uint; auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccompshift *)
+ econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite transl_eval_shift. apply eval_testcond_compare_sint; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccompushift *)
+ econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite transl_eval_shift. apply eval_testcond_compare_uint; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Cmaskzero *)
+ destruct (is_logical_imm32 n).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int.repr_unsigned. apply (eval_testcond_compare_sint Ceq); auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm32 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. simpl. rewrite B, C by eauto with asmgen. eauto. auto.
+ split; intros. apply (eval_testcond_compare_sint Ceq); auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Cmasknotzero *)
+ destruct (is_logical_imm32 n).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int.repr_unsigned. apply (eval_testcond_compare_sint Cne); auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm32 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. simpl. rewrite B, C by eauto with asmgen. eauto. auto.
+ split; intros. apply (eval_testcond_compare_sint Cne); auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccompl *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_slong; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccomplu *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_ulong; auto.
+ erewrite Val.mxcmplu_bool_correct; eauto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccomplimm *)
+ destruct (is_arith_imm64 n); [|destruct (is_arith_imm64 (Int64.neg n))].
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int64.repr_unsigned. apply eval_testcond_compare_slong; auto.
+ destruct r; reflexivity || discriminate.
+ + econstructor; split.
+ apply exec_straight_one. simpl. rewrite Int64.repr_unsigned, Int64.neg_involutive. eauto.
+ split; intros. apply eval_testcond_compare_slong; auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm64 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ simpl. rewrite B, C by eauto with asmgen. eauto. auto.
+ split; intros. apply eval_testcond_compare_slong; auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccompluimm *)
+ destruct (is_arith_imm64 n); [|destruct (is_arith_imm64 (Int64.neg n))].
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros. rewrite Int64.repr_unsigned. apply eval_testcond_compare_ulong; auto.
+ erewrite Val.mxcmplu_bool_correct; eauto.
+ destruct r; reflexivity || discriminate.
+ + econstructor; split.
+ apply exec_straight_one. simpl. rewrite Int64.repr_unsigned, Int64.neg_involutive. eauto.
+ split; intros. apply eval_testcond_compare_ulong; auto.
+ erewrite Val.mxcmplu_bool_correct; eauto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm64 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one.
+ simpl. rewrite B, C by eauto with asmgen. eauto.
+ split; intros. apply eval_testcond_compare_ulong; auto.
+ erewrite Val.mxcmplu_bool_correct; eauto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccomplshift *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. rewrite transl_eval_shiftl. apply eval_testcond_compare_slong; auto.
+ destruct r; reflexivity || discriminate.
+ - (* Ccomplushift *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. rewrite transl_eval_shiftl. apply eval_testcond_compare_ulong; auto.
+ erewrite Val.mxcmplu_bool_correct; eauto.
+ destruct r; reflexivity || discriminate.
+ - (* Cmasklzero *)
+ destruct (is_logical_imm64 n).
+ + econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. rewrite Int64.repr_unsigned. apply (eval_testcond_compare_slong Ceq); auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm64 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. simpl. rewrite B, C by eauto with asmgen. eauto.
+ split; intros. apply (eval_testcond_compare_slong Ceq); auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Cmasknotzero *)
+ destruct (is_logical_imm64 n).
+ + econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. rewrite Int64.repr_unsigned. apply (eval_testcond_compare_slong Cne); auto.
+ destruct r; reflexivity || discriminate.
+ + exploit (exec_loadimm64 X16 n). intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A.
+ apply exec_straight_one. simpl. rewrite B, C by eauto with asmgen. eauto.
+ split; intros. apply (eval_testcond_compare_slong Cne); auto.
+ transitivity (rs' r). destruct r; reflexivity || discriminate. auto with asmgen.
+ - (* Ccompf *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_float; auto.
+ destruct r; discriminate || rewrite compare_float_inv; auto.
+ - (* Cnotcompf *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_not_float; auto.
+ destruct r; discriminate || rewrite compare_float_inv; auto.
+ - (* Ccompfzero *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_float; auto.
+ destruct r; discriminate || rewrite compare_float_inv; auto.
+ - (* Cnotcompfzero *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_not_float; auto.
+ destruct r; discriminate || rewrite compare_float_inv; auto.
+ - (* Ccompfs *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_single; auto.
+ destruct r; discriminate || rewrite compare_single_inv; auto.
+ - (* Cnotcompfs *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_not_single; auto.
+ destruct r; discriminate || rewrite compare_single_inv; auto.
+ - (* Ccompfszero *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_single; auto.
+ destruct r; discriminate || rewrite compare_single_inv; auto.
+ - (* Cnotcompfszero *)
+ econstructor; split. apply exec_straight_one. simpl; eauto.
+ split; intros. apply eval_testcond_compare_not_single; auto.
+ destruct r; discriminate || rewrite compare_single_inv; auto.
+Qed.
+
+Lemma transl_cond_correct':
+ forall cond args k c tbb rs m,
+ transl_cond cond args k = OK c ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m
+ /\ (forall b,
+ eval_condition cond (map rs (map preg_of args)) m = Some b ->
+ eval_testcond (cond_for_cond cond) (incrPC (Ptrofs.repr (size tbb)) rs') = Some b)
+ /\ forall r, data_preg r = true -> rs'#r = rs#r.
+Proof.
+ intros until m; intros TR.
+ eapply transl_cond_correct; eauto.
+Qed.
+
+Lemma transl_cbranch_correct_1:
+ forall cond args lbl c k b m rs tbb bdy,
+ transl_cond_branch cond args lbl k = OK (bdy,c) ->
+ eval_condition cond (map rs (map preg_of args)) m = Some b ->
+ exists rs',
+ exec_straight_opt ge lk bdy rs m k rs' m
+ /\ exec_cfi ge fn c (incrPC (Ptrofs.repr (size tbb)) rs') m =
+ (if b then goto_label fn lbl (incrPC (Ptrofs.repr (size tbb)) rs') m else
+ Next (incrPC (Ptrofs.repr (size tbb)) rs') m)
+ /\ forall r, data_preg r = true -> rs'#r = rs#r.
+Proof.
+intros until bdy; intros TR EV.
+ assert (Archi.ptr64 = true) as SF; auto.
+ assert (DFL:
+ transl_cond_branch_default cond args lbl k = OK (bdy,c) ->
+ exists rs',
+ exec_straight_opt ge lk bdy rs m k rs' m
+ /\ exec_cfi ge fn c (incrPC (Ptrofs.repr (size tbb)) rs') m = eval_branch fn lbl (incrPC (Ptrofs.repr (size tbb)) rs') m (Some b)
+ /\ forall r, data_preg r = true -> rs'#r = rs#r).
+ {
+ unfold transl_cond_branch_default; intros. monadInv H.
+ exploit transl_cond_correct'; eauto. intros (rs' & A & B & C).
+ eexists; split.
+ apply exec_straight_opt_intro. eexact A.
+ split; auto. simpl. rewrite (B b) by auto. auto.
+ }
+Local Opaque transl_cond transl_cond_branch_default.
+ destruct args as [ | a1 args]; simpl in TR; auto.
+ destruct args as [ | a2 args]; simpl in TR; auto.
+ destruct cond; simpl in TR; auto.
+ - (* Ccompimm *)
+ destruct c0; auto; destruct (Int.eq n Int.zero) eqn:N0; auto;
+ apply Int.same_if_eq in N0; subst n; ArgsInv.
+ + (* Ccompimm Cne 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. destruct (rs x) eqn:EQRSX; simpl in EV; inv EV.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl. auto.
+ + (* Ccompimm Ceq 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. destruct (rs x) eqn:EQRSX; simpl in EV; inv EV. simpl.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl.
+ destruct (Int.eq i Int.zero); auto.
+ - (* Ccompuimm *)
+ destruct c0; auto; destruct (Int.eq n Int.zero) eqn:N0; auto.
+ apply Int.same_if_eq in N0; subst n; ArgsInv.
+ + (* Ccompuimm Cne 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. apply Val.mxcmpu_bool_correct in EV.
+ unfold incrPC. Simpl. rewrite EV. auto.
+ + (* Ccompuimm Ceq 0 *)
+ monadInv TR. ArgsInv. simpl in *.
+ econstructor; split. econstructor.
+ split; auto. simpl. unfold incrPC. Simpl.
+ apply Int.same_if_eq in N0; subst.
+ rewrite (Val.negate_cmpu_bool (Mem.valid_pointer m) Cne), EV.
+ destruct b; auto.
+ - (* Cmaskzero *)
+ destruct (Int.is_power2 n) as [bit|] eqn:P2; auto. ArgsInv.
+ econstructor; split. econstructor.
+ split; auto. simpl.
+ erewrite <- Int.mul_pow2, Int.mul_commut, Int.mul_one by eauto.
+ unfold incrPC. Simpl.
+ rewrite (Val.negate_cmp_bool Ceq), EV. destruct b; auto.
+ - (* Cmasknotzero *)
+ destruct (Int.is_power2 n) as [bit|] eqn:P2; auto. ArgsInv.
+ econstructor; split. econstructor.
+ split; auto. simpl.
+ erewrite <- Int.mul_pow2, Int.mul_commut, Int.mul_one by eauto.
+ unfold incrPC. Simpl.
+ rewrite EV. auto.
+ - (* Ccomplimm *)
+ destruct c0; auto; destruct (Int64.eq n Int64.zero) eqn:N0; auto;
+ apply Int64.same_if_eq in N0; subst n; ArgsInv.
+ + (* Ccomplimm Cne 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. destruct (rs x) eqn:EQRSX; simpl in EV; inv EV.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl. auto.
+ + (* Ccomplimm Ceq 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. destruct (rs x) eqn:EQRSX; simpl in EV; inv EV. simpl.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl.
+ destruct (Int64.eq i Int64.zero); auto.
+ - (* Ccompluimm *)
+ destruct c0; auto; destruct (Int64.eq n Int64.zero) eqn:N0; auto;
+ apply Int64.same_if_eq in N0; subst n; ArgsInv.
+ + (* Ccompluimm Cne 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. apply Val.mxcmplu_bool_correct in EV.
+ unfold incrPC. Simpl. rewrite EV. auto.
+ + (* Ccompluimm Ceq 0 *)
+ econstructor; split. econstructor.
+ split; auto. simpl. destruct (rs x) eqn:EQRSX; simpl in EV; inv EV. simpl.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl.
+ destruct (Int64.eq i Int64.zero); auto.
+ unfold incrPC. Simpl. rewrite EQRSX. simpl.
+ rewrite SF in *; simpl in *.
+ rewrite Int64.eq_true in *.
+ destruct ((Mem.valid_pointer m b0 (Ptrofs.unsigned i) || Mem.valid_pointer m b0 (Ptrofs.unsigned i - 1))); simpl in *.
+ assert (b = true). { destruct b; try congruence. }
+ rewrite H; auto. discriminate.
+ - (* Cmasklzero *)
+ destruct (Int64.is_power2' n) as [bit|] eqn:P2; auto. ArgsInv.
+ econstructor; split. econstructor.
+ split; auto.
+ erewrite <- Int64.mul_pow2', Int64.mul_commut, Int64.mul_one by eauto.
+ unfold incrPC. Simpl.
+ rewrite (Val.negate_cmpl_bool Ceq), EV. destruct b; auto.
+ - (* Cmasklnotzero *)
+ destruct (Int64.is_power2' n) as [bit|] eqn:P2; auto. ArgsInv.
+ econstructor; split. econstructor.
+ split; auto.
+ erewrite <- Int64.mul_pow2', Int64.mul_commut, Int64.mul_one by eauto.
+ unfold incrPC. Simpl.
+ rewrite EV. auto.
+Qed.
+
+Lemma transl_op_correct:
+ forall op args res k (rs: regset) m v c,
+ transl_op op args res k = OK c ->
+ eval_operation ge (rs#SP) op (map rs (map preg_of args)) m = Some v ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m
+ /\ Val.lessdef v rs'#(preg_of res)
+ /\ forall r, data_preg r = true -> r <> preg_of res -> preg_notin r (destroyed_by_op op) -> rs' r = rs r.
+Proof.
+ (* assert (SAME: forall v1 v2, v1 = v2 -> Val.lessdef v2 v1). { intros; subst; auto. } *)
+Local Opaque Int.eq Int64.eq Val.add Val.addl Int.zwordsize Int64.zwordsize.
+ intros until c; intros TR EV.
+ unfold transl_op in TR; destruct op; ArgsInv; simpl in EV; SimplEval EV; try TranslOpSimpl;
+ try (rewrite <- transl_eval_shift; TranslOpSimpl).
+ - (* move *)
+ destruct (preg_of res), (preg_of m0); try destruct d; try destruct d0; inv TR; TranslOpSimpl.
+ - (* intconst *)
+ exploit exec_loadimm32. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. intros; auto with asmgen.
+ - (* longconst *)
+ exploit exec_loadimm64. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. intros; auto with asmgen.
+ - (* floatconst *)
+ destruct (Float.eq_dec n Float.zero).
+ + subst n. TranslOpSimpl.
+ + TranslOpSimplN.
+ - (* singleconst *)
+ destruct (Float32.eq_dec n Float32.zero).
+ + subst n. TranslOpSimpl.
+ + TranslOpSimplN.
+ - (* loadsymbol *)
+ exploit (exec_loadsymbol x id ofs). eauto with asmgen. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* addrstack *)
+ exploit (exec_addimm64 x XSP (Ptrofs.to_int64 ofs)); try discriminate. simpl; eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. replace (DR XSP) with (SP) in B by auto. rewrite B.
+ Local Transparent Val.addl.
+ destruct (rs SP); simpl; auto. rewrite Ptrofs.of_int64_to_int64 by auto. auto.
+ auto.
+ - (* shift *)
+ rewrite <- transl_eval_shift'. TranslOpSimpl.
+ - (* addimm *)
+ exploit (exec_addimm32 x x0 n). eauto with asmgen. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* mul *)
+ TranslOpBase.
+ Local Transparent Val.add.
+ destruct (rs x0); auto; destruct (rs x1); auto. simpl. rewrite Int.add_zero_l; auto.
+ - (* andimm *)
+ exploit (exec_logicalimm32 (Pandimm W) (Pand W)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* orimm *)
+ exploit (exec_logicalimm32 (Porrimm W) (Porr W)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* xorimm *)
+ exploit (exec_logicalimm32 (Peorimm W) (Peor W)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* not *)
+ TranslOpBase.
+ destruct (rs x0); auto. simpl. rewrite Int.or_zero_l; auto.
+ - (* notshift *)
+ TranslOpBase.
+ destruct (eval_shift s (rs x0) a); auto. simpl. rewrite Int.or_zero_l; auto.
+ - (* shrx *)
+ assert (Val.maketotal (Val.shrx (rs x0) (Vint n)) = Val.maketotal (Val.shrx (rs x0) (Vint n))) by eauto.
+ destruct (Val.shrx) eqn:E.
+ + exploit (exec_shrx32 x x0 n); eauto with asmgen. intros (rs' & A & B & C).
+ econstructor; split. eexact A. split. rewrite B; auto. auto.
+ + exploit (exec_shrx32_none x x0 n); eauto with asmgen.
+ - (* zero-ext *)
+ TranslOpBase.
+ destruct (rs x0); auto; simpl. rewrite Int.shl_zero. auto.
+ - (* sign-ext *)
+ TranslOpBase.
+ destruct (rs x0); auto; simpl. rewrite Int.shl_zero. auto.
+ - (* shlzext *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite <- Int.shl_zero_ext_min; auto using a32_range.
+ - (* shlsext *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite <- Int.shl_sign_ext_min; auto using a32_range.
+ - (* zextshr *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite ! a32_range; simpl. rewrite <- Int.zero_ext_shru_min; auto using a32_range.
+ - (* sextshr *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite ! a32_range; simpl. rewrite <- Int.sign_ext_shr_min; auto using a32_range.
+ - (* shiftl *)
+ rewrite <- transl_eval_shiftl'. TranslOpSimpl.
+ - (* extend *)
+ exploit (exec_move_extended x0 x1 x a k). intros (rs' & A & B & C).
+ econstructor; split. eexact A.
+ split. rewrite B; auto. eauto with asmgen.
+ - (* addlshift *)
+ TranslOpBase.
+ - (* addext *)
+ exploit (exec_arith_extended Val.addl Paddext (Padd X)).
+ auto. auto. instantiate (1 := x1). eauto with asmgen. intros (rs' & A & B & C).
+ econstructor; split. eexact A. split. rewrite B; auto. auto.
+ - (* addlimm *)
+ exploit (exec_addimm64 x x0 n). simpl. generalize (ireg_of_not_X16 _ _ EQ1). congruence.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. simpl in B; rewrite B; auto. auto.
+ - (* neglshift *)
+ TranslOpBase.
+ - (* sublshift *)
+ TranslOpBase.
+ - (* subext *)
+ exploit (exec_arith_extended Val.subl Psubext (Psub X)).
+ auto. auto. instantiate (1 := x1). eauto with asmgen. intros (rs' & A & B & C).
+ econstructor; split. eexact A. split. rewrite B; auto. auto.
+ - (* mull *)
+ TranslOpBase.
+ destruct (rs x0); auto; destruct (rs x1); auto. simpl. rewrite Int64.add_zero_l; auto.
+ - (* andlshift *)
+ TranslOpBase.
+ - (* andlimm *)
+ exploit (exec_logicalimm64 (Pandimm X) (Pand X)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* orlshift *)
+ TranslOpBase.
+ - (* orlimm *)
+ exploit (exec_logicalimm64 (Porrimm X) (Porr X)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* orlshift *)
+ TranslOpBase.
+ - (* xorlimm *)
+ exploit (exec_logicalimm64 (Peorimm X) (Peor X)).
+ intros; reflexivity. intros; reflexivity. instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split. rewrite B; auto. auto.
+ - (* notl *)
+ TranslOpBase.
+ destruct (rs x0); auto. simpl. rewrite Int64.or_zero_l; auto.
+ - (* notlshift *)
+ TranslOpBase.
+ destruct (eval_shiftl s (rs x0) a); auto. simpl. rewrite Int64.or_zero_l; auto.
+ - (* biclshift *)
+ TranslOpBase.
+ - (* ornlshift *)
+ TranslOpBase.
+ - (* eqvlshift *)
+ TranslOpBase.
+ - (* shrx *)
+ assert (Val.maketotal (Val.shrxl (rs x0) (Vint n)) = Val.maketotal (Val.shrxl (rs x0) (Vint n))) by eauto.
+ destruct (Val.shrxl) eqn:E.
+ + exploit (exec_shrx64 x x0 n); eauto with asmgen. intros (rs' & A & B & C).
+ econstructor; split. eexact A. split. rewrite B; auto. auto.
+ + exploit (exec_shrx64_none x x0 n); eauto with asmgen.
+ - (* zero-ext-l *)
+ TranslOpBase.
+ destruct (rs x0); auto; simpl. rewrite Int64.shl'_zero. auto.
+ - (* sign-ext-l *)
+ TranslOpBase.
+ destruct (rs x0); auto; simpl. rewrite Int64.shl'_zero. auto.
+ - (* shllzext *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite <- Int64.shl'_zero_ext_min; auto using a64_range.
+ - (* shllsext *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite <- Int64.shl'_sign_ext_min; auto using a64_range.
+ - (* zextshrl *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite ! a64_range; simpl. rewrite <- Int64.zero_ext_shru'_min; auto using a64_range.
+ - (* sextshrl *)
+ TranslOpBase.
+ destruct (rs x0); simpl; auto. rewrite ! a64_range; simpl. rewrite <- Int64.sign_ext_shr'_min; auto using a64_range.
+ - (* condition *)
+ exploit (transl_cond_correct cond args); eauto. intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. destruct (eval_condition cond (map rs (map preg_of args)) m) as [b|]; simpl in *.
+ rewrite (B b) by auto. auto.
+ auto.
+ intros; Simpl.
+ - (* select *)
+ destruct (preg_of res) as [[ir|fr]|cr|] eqn:RES; monadInv TR.
+ + (* integer *)
+ generalize (ireg_of_eq _ _ EQ) (ireg_of_eq _ _ EQ1); intros E1 E2; rewrite E1, E2.
+ exploit (transl_cond_correct cond args); eauto. intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one. simpl; eauto.
+ split. Simpl. destruct (eval_condition cond (map rs (map preg_of args)) m) as [b|]; simpl in *.
+ rewrite (B b) by auto. rewrite !C. apply Val.lessdef_normalize.
+ rewrite <- E2; auto with asmgen. rewrite <- E1; auto with asmgen.
+ auto.
+ intros; Simpl.
+ + (* FP *)
+ generalize (freg_of_eq _ _ EQ) (freg_of_eq _ _ EQ1); intros E1 E2; rewrite E1, E2.
+ exploit (transl_cond_correct cond args); eauto. intros (rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_trans. eexact A. apply exec_straight_one. simpl; eauto.
+ split. Simpl. destruct (eval_condition cond (map rs (map preg_of args)) m) as [b|]; simpl in *.
+ rewrite (B b) by auto. rewrite !C. apply Val.lessdef_normalize.
+ rewrite <- E2; auto with asmgen. rewrite <- E1; auto with asmgen.
+ auto.
+ intros; Simpl.
+Qed.
+
+(** Translation of addressing modes, loads, stores *)
+
+Lemma transl_addressing_correct:
+ forall sz addr args (insn: Asm.addressing -> basic) k (rs: regset) m c b o,
+ transl_addressing sz addr args insn k = OK c ->
+ Op.eval_addressing ge (rs#SP) addr (map rs (map preg_of args)) = Some (Vptr b o) ->
+ exists ad rs',
+ exec_straight_opt ge lk c rs m (insn ad :: k) rs' m
+ /\ eval_addressing lk ad rs' = Vptr b o
+ /\ forall r, data_preg r = true -> rs' r = rs r.
+Proof.
+ intros until o; intros TR EV.
+ unfold transl_addressing in TR; destruct addr; ArgsInv; SimplEval EV.
+ - (* Aindexed *)
+ destruct (offset_representable sz ofs); inv EQ0.
+ + econstructor; econstructor; split. apply exec_straight_opt_refl.
+ auto.
+ + exploit (exec_loadimm64 X16 ofs). intros (rs' & A & B & C).
+ econstructor; exists rs'; split. apply exec_straight_opt_intro; eexact A.
+ split. simpl. rewrite B, C by eauto with asmgen. auto.
+ eauto with asmgen.
+ - (* Aindexed2 *)
+ econstructor; econstructor; split. apply exec_straight_opt_refl.
+ auto.
+ - (* Aindexed2shift *)
+ destruct (Int.eq a Int.zero) eqn:E; [|destruct (Int.eq (Int.shl Int.one a) (Int.repr sz))]; inv EQ2.
+ + apply Int.same_if_eq in E. rewrite E.
+ econstructor; econstructor; split. apply exec_straight_opt_refl.
+ split; auto. simpl.
+ rewrite Val.addl_commut in H0. destruct (rs x0); try discriminate.
+ unfold Val.shll. rewrite Int64.shl'_zero. auto.
+ + econstructor; econstructor; split. apply exec_straight_opt_refl.
+ auto.
+ + econstructor; econstructor; split.
+ apply exec_straight_opt_intro. apply exec_straight_one. simpl; eauto.
+ split. simpl. Simpl. rewrite H0. simpl. rewrite Ptrofs.add_zero. auto.
+ intros; Simpl.
+ - (* Aindexed2ext *)
+ destruct (Int.eq a Int.zero || Int.eq (Int.shl Int.one a) (Int.repr sz)); inv EQ2.
+ + econstructor; econstructor; split. apply exec_straight_opt_refl.
+ split; auto. destruct x; auto.
+ + exploit (exec_arith_extended Val.addl Paddext (Padd X)); auto.
+ instantiate (1 := x0). eauto with asmgen.
+ intros (rs' & A & B & C).
+ econstructor; exists rs'; split.
+ apply exec_straight_opt_intro. eexact A.
+ split. simpl. rewrite B. rewrite Val.addl_assoc. f_equal.
+ unfold Op.eval_extend; destruct x, (rs x1); simpl; auto; rewrite ! a64_range;
+ simpl; rewrite Int64.add_zero; auto.
+ intros. apply C; eauto with asmgen.
+ - (* Aglobal *)
+ destruct (Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero && symbol_is_aligned id sz); inv TR.
+ + econstructor; econstructor; split.
+ apply exec_straight_opt_intro. apply exec_straight_one. simpl; eauto.
+ split. simpl. Simpl. rewrite symbol_high_low. simpl in EV. congruence.
+ intros; Simpl.
+ + exploit (exec_loadsymbol X16 id ofs). auto. intros (rs' & A & B & C).
+ econstructor; exists rs'; split.
+ apply exec_straight_opt_intro. eexact A.
+ split. simpl.
+ rewrite B. rewrite <- Genv.shift_symbol_address_64, Ptrofs.add_zero by auto.
+ simpl in EV. congruence.
+ auto with asmgen.
+ - (* Ainstrack *)
+ assert (E: Val.addl (rs SP) (Vlong (Ptrofs.to_int64 ofs)) = Vptr b o).
+ { simpl in EV. inv EV. destruct (rs SP); simpl in H1; inv H1. simpl.
+ rewrite Ptrofs.of_int64_to_int64 by auto. auto. }
+ destruct (offset_representable sz (Ptrofs.to_int64 ofs)); inv TR.
+ + econstructor; econstructor; split. apply exec_straight_opt_refl.
+ auto.
+ + exploit (exec_loadimm64 X16 (Ptrofs.to_int64 ofs)). intros (rs' & A & B & C).
+ econstructor; exists rs'; split.
+ apply exec_straight_opt_intro. eexact A.
+ split. simpl. rewrite B, C by eauto with asmgen. auto.
+ auto with asmgen.
+Qed.
+
+Lemma transl_load_correct:
+ forall chunk addr args dst k c (rs: regset) m vaddr v,
+ transl_load chunk addr args dst k = OK c ->
+ Op.eval_addressing ge (rs#SP) addr (map rs (map preg_of args)) = Some vaddr ->
+ Mem.loadv chunk m vaddr = Some v ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m
+ /\ rs'#(preg_of dst) = v
+ /\ forall r, data_preg r = true -> r <> preg_of dst -> rs' r = rs r.
+Proof.
+ intros. destruct vaddr; try discriminate.
+ assert (A: exists sz insn,
+ transl_addressing sz addr args insn k = OK c
+ /\ (forall ad rs', exec_basic lk ge (insn ad) rs' m =
+ exec_load_rd_a lk chunk (fun v => v) ad (dreg_of dst) rs' m)).
+ {
+ destruct chunk; monadInv H;
+ try rewrite (ireg_of_eq' _ _ EQ); try rewrite (freg_of_eq' _ _ EQ);
+ do 2 econstructor; (split; [eassumption|auto]).
+ }
+ destruct A as (sz & insn & B & C).
+ exploit transl_addressing_correct. eexact B. eexact H0. intros (ad & rs' & P & Q & R).
+ assert (X: exec_load_rd_a lk chunk (fun v => v) ad (dreg_of dst) rs' m =
+ Next (rs'#(preg_of dst) <- v) m).
+ { unfold exec_load_rd_a. rewrite Q, H1. auto. }
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact P.
+ apply exec_straight_one. rewrite C, X; eauto. Simpl.
+ split. auto. intros; Simpl.
+Qed.
+
+Lemma transl_store_correct:
+ forall chunk addr args src k c (rs: regset) m vaddr m',
+ transl_store chunk addr args src k = OK c ->
+ Op.eval_addressing ge (rs#SP) addr (map rs (map preg_of args)) = Some vaddr ->
+ Mem.storev chunk m vaddr rs#(preg_of src) = Some m' ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m'
+ /\ forall r, data_preg r = true -> rs' r = rs r.
+Proof.
+ intros. destruct vaddr; try discriminate.
+ set (chunk' := match chunk with Mint8signed => Mint8unsigned
+ | Mint16signed => Mint16unsigned
+ | _ => chunk end).
+ assert (A: exists sz insn,
+ transl_addressing sz addr args insn k = OK c
+ /\ (forall ad rs', exec_basic lk ge (insn ad) rs' m =
+ exec_store_rs_a lk chunk' ad rs'#(preg_of src) rs' m)).
+ {
+ unfold chunk'; destruct chunk; monadInv H;
+ try rewrite (ireg_of_eq _ _ EQ); try rewrite (freg_of_eq _ _ EQ);
+ do 2 econstructor; (split; [eassumption|auto]).
+ }
+ destruct A as (sz & insn & B & C).
+ exploit transl_addressing_correct. eexact B. eexact H0. intros (ad & rs' & P & Q & R).
+ assert (X: Mem.storev chunk' m (Vptr b i) rs#(preg_of src) = Some m').
+ { rewrite <- H1. unfold chunk'. destruct chunk; auto; simpl; symmetry.
+ apply Mem.store_signed_unsigned_8.
+ apply Mem.store_signed_unsigned_16. }
+ assert (Y: exec_store_rs_a lk chunk' ad rs'#(preg_of src) rs' m =
+ Next rs' m').
+ { unfold exec_store_rs_a. rewrite Q, R, X by auto with asmgen. auto. }
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact P.
+ apply exec_straight_one. rewrite C, Y; eauto.
+ intros; Simpl. rewrite R; auto.
+Qed.
+
+(** Memory accesses *)
+
+Lemma indexed_memory_access_correct: forall insn sz (base: iregsp) ofs k (rs: regset) m b i,
+ preg_of_iregsp base <> X16 ->
+ Val.offset_ptr rs#base ofs = Vptr b i ->
+ exists ad rs',
+ exec_straight_opt ge lk (indexed_memory_access_bc insn sz base ofs k) rs m (insn ad :: k) rs' m
+ /\ eval_addressing lk ad rs' = Vptr b i
+ /\ forall r, r <> PC -> r <> X16 -> rs' r = rs r.
+Proof.
+ unfold indexed_memory_access_bc; intros.
+ assert (Val.addl rs#base (Vlong (Ptrofs.to_int64 ofs)) = Vptr b i).
+ { destruct (rs base); try discriminate. simpl in *. rewrite Ptrofs.of_int64_to_int64 by auto. auto. }
+ destruct offset_representable.
+ - econstructor; econstructor; split. apply exec_straight_opt_refl. auto.
+ - exploit (exec_loadimm64 X16); eauto. intros (rs' & A & B & C).
+ econstructor; econstructor; split. apply exec_straight_opt_intro; eexact A.
+ split. simpl. rewrite B, C; eauto; try discriminate.
+ unfold preg_of_iregsp in H. destruct base; auto. auto.
+Qed.
+
+Lemma loadptr_correct: forall (base: iregsp) ofs dst k m v (rs: regset),
+ Mem.loadv Mint64 m (Val.offset_ptr rs#base ofs) = Some v ->
+ preg_of_iregsp base <> IR X16 ->
+ exists rs',
+ exec_straight ge lk (loadptr_bc base ofs dst k) rs m k rs' m
+ /\ rs'#dst = v
+ /\ forall r, r <> PC -> r <> X16 -> r <> dst -> rs' r = rs r.
+Proof.
+ intros.
+ destruct (Val.offset_ptr rs#base ofs) eqn:V; try discriminate.
+ exploit indexed_memory_access_correct; eauto. intros (ad & rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A.
+ apply exec_straight_one. simpl. unfold exec_load_rd_a. rewrite B, H. eauto.
+ split. Simpl. intros; Simpl.
+Qed.
+
+Lemma storeptr_correct: forall (base: iregsp) ofs (src: ireg) k m m' (rs: regset),
+ Mem.storev Mint64 m (Val.offset_ptr rs#base ofs) rs#src = Some m' ->
+ preg_of_iregsp base <> IR X16 ->
+ (DR (IR (RR1 src))) <> (DR (IR (RR1 X16))) ->
+ exists rs',
+ exec_straight ge lk (storeptr_bc src base ofs k) rs m k rs' m'
+ /\ forall r, r <> PC -> r <> X16 -> rs' r = rs r.
+Proof.
+ intros.
+ destruct (Val.offset_ptr rs#base ofs) eqn:V; try discriminate.
+ exploit indexed_memory_access_correct; eauto. intros (ad & rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A.
+ apply exec_straight_one. simpl. unfold exec_store_rs_a. rewrite B, C, H. eauto.
+ discriminate. auto.
+ intros; Simpl. rewrite C; auto.
+Qed.
+
+Lemma loadind_correct:
+ forall (base: iregsp) ofs ty dst k c (rs: regset) m v,
+ loadind base ofs ty dst k = OK c ->
+ Mem.loadv (chunk_of_type ty) m (Val.offset_ptr rs#base ofs) = Some v ->
+ preg_of_iregsp base <> IR X16 ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m
+ /\ rs'#(preg_of dst) = v
+ /\ forall r, data_preg r = true -> r <> preg_of dst -> rs'#r = rs#r.
+Proof.
+ intros.
+ destruct (Val.offset_ptr rs#base ofs) eqn:V; try discriminate.
+ assert (X: exists sz (insn: addressing -> ld_instruction),
+ c = indexed_memory_access_bc insn sz base ofs k
+ /\ (forall ad rs', exec_basic lk ge (insn ad) rs' m =
+ exec_load_rd_a lk (chunk_of_type ty) (fun v => v) ad (dreg_of dst) rs' m)).
+ {
+ unfold loadind in H; destruct ty; destruct (dst); inv H;
+ do 2 econstructor; split; eauto.
+ }
+ destruct X as (sz & insn & EQ & SEM). subst c.
+ exploit indexed_memory_access_correct; eauto. intros (ad & rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A.
+ apply exec_straight_one. rewrite SEM. unfold exec_load.
+ unfold exec_load_rd_a. rewrite B, H0. eauto. Simpl.
+ split. auto. intros; Simpl.
+Qed.
+
+Lemma storeind_correct: forall (base: iregsp) ofs ty src k c (rs: regset) m m',
+ storeind src base ofs ty k = OK c ->
+ Mem.storev (chunk_of_type ty) m (Val.offset_ptr rs#base ofs) rs#(preg_of src) = Some m' ->
+ preg_of_iregsp base <> IR X16 ->
+ exists rs',
+ exec_straight ge lk c rs m k rs' m'
+ /\ forall r, data_preg r = true -> rs' r = rs r.
+Proof.
+ intros.
+ destruct (Val.offset_ptr rs#base ofs) eqn:V; try discriminate.
+ assert (X: exists sz (insn: addressing -> st_instruction),
+ c = indexed_memory_access_bc insn sz base ofs k
+ /\ (forall ad rs', exec_basic lk ge (insn ad) rs' m =
+ exec_store_rs_a lk (chunk_of_type ty) ad rs'#(preg_of src) rs' m)).
+ {
+ unfold storeind in H; destruct ty; destruct (preg_of src) as [[ir|fr]|cr|]; inv H; do 2 econstructor; split; eauto.
+ }
+ destruct X as (sz & insn & EQ & SEM). subst c.
+ exploit indexed_memory_access_correct; eauto. intros (ad & rs' & A & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A.
+ apply exec_straight_one. rewrite SEM.
+ unfold exec_store. unfold exec_store_rs_a.
+ rewrite B, C, H0 by eauto with asmgen. eauto.
+ intros; Simpl. unfold data_preg in H2. destruct r as [[[ir|]|fr]|cr|].
+ all: rewrite C; auto; try discriminate;
+ destruct ir; try discriminate.
+Qed.
+
+Lemma make_epilogue_correct:
+ forall ge0 f m stk soff cs m' ms rs tm,
+ Mach.load_stack m (Vptr stk soff) Tptr f.(fn_link_ofs) = Some (parent_sp cs) ->
+ Mach.load_stack m (Vptr stk soff) Tptr f.(fn_retaddr_ofs) = Some (parent_ra cs) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ agree ms (Vptr stk soff) rs ->
+ Mem.extends m tm ->
+ match_stack ge0 cs ->
+ exists rs', exists tm',
+ exec_straight ge lk (make_epilogue f) rs tm nil rs' tm'
+ /\ agree ms (parent_sp cs) rs'
+ /\ Mem.extends m' tm'
+ /\ rs'#RA = parent_ra cs
+ /\ rs'#SP = parent_sp cs
+ /\ (forall r, r <> PC -> r <> SP -> r <> X30 -> r <> X16 -> rs'#r = rs#r).
+Proof.
+ assert (Archi.ptr64 = true) as SF; auto.
+ intros until tm; intros LP LRA FREE AG MEXT MCS.
+ exploit Mem.loadv_extends. eauto. eexact LP. auto. simpl. intros (parent' & LP' & LDP').
+ exploit Mem.loadv_extends. eauto. eexact LRA. auto. simpl. intros (ra' & LRA' & LDRA').
+ exploit lessdef_parent_sp; eauto. intros EQ; subst parent'; clear LDP'.
+ exploit lessdef_parent_ra; eauto. intros EQ; subst ra'; clear LDRA'.
+ exploit Mem.free_parallel_extends; eauto. intros (tm' & FREE' & MEXT').
+ unfold make_epilogue.
+ rewrite chunk_of_Tptr in *. unfold Mptr in *. rewrite SF in *.
+
+ exploit (loadptr_correct XSP (fn_retaddr_ofs f)).
+ instantiate (2 := rs). simpl.
+ replace (rs XSP) with (rs SP) by auto.
+ rewrite <- (sp_val _ _ _ AG). simpl. eexact LRA'. simpl; discriminate.
+
+ intros (rs1 & A1 & B1 & C1).
+ econstructor; econstructor; split.
+ eapply exec_straight_trans. eexact A1. apply exec_straight_one. simpl.
+ simpl; rewrite (C1 SP) by auto with asmgen. rewrite <- (sp_val _ _ _ AG). simpl; rewrite LP'.
+ rewrite FREE'. eauto.
+ split. apply agree_set_other; auto.
+ apply agree_change_sp with (Vptr stk soff).
+ apply agree_exten with rs; auto. intros; apply C1; auto with asmgen.
+ eapply parent_sp_def; eauto.
+ split. auto.
+ split. Simpl.
+ split. Simpl.
+ intros. Simpl.
+Qed.
+
+End CONSTRUCTORS.
diff --git a/aarch64/Asmblockprops.v b/aarch64/Asmblockprops.v
new file mode 100644
index 00000000..38fbd6d3
--- /dev/null
+++ b/aarch64/Asmblockprops.v
@@ -0,0 +1,119 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Common definition and proofs on Asmblock required by various modules *)
+
+Require Import Coqlib.
+Require Import Integers.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Values.
+Require Import Asmblock.
+Require Import Axioms.
+
+Definition bblock_simu (lk: aarch64_linker) (ge: Genv.t fundef unit) (f: function) (bb bb': bblock) :=
+ forall rs m rs' m' t,
+ exec_bblock lk ge f bb rs m t rs' m' -> exec_bblock lk ge f bb' rs m t rs' m'.
+
+Definition bblock_simu_aux (lk: aarch64_linker) (ge: Genv.t fundef unit) (f: function) (bb bb': bblock) :=
+ forall rs m,
+ bbstep lk ge f bb rs m <> Stuck ->
+ bbstep lk ge f bb rs m = bbstep lk ge f bb' rs m.
+
+Hint Extern 2 (_ <> _) => congruence: asmgen.
+
+Lemma preg_of_data:
+ forall r, data_preg (preg_of r) = true.
+Proof.
+ intros. destruct r; reflexivity.
+Qed.
+
+Lemma dreg_of_data:
+ forall r, data_preg (dreg_of r) = true.
+Proof.
+ intros. destruct r; reflexivity.
+Qed.
+Hint Resolve preg_of_data dreg_of_data: asmgen.
+
+Lemma data_diff:
+ forall r r',
+ data_preg r = true -> data_preg r' = false -> r <> r'.
+Proof.
+ congruence.
+Qed.
+Hint Resolve data_diff: asmgen.
+
+Lemma preg_of_not_PC:
+ forall r, preg_of r <> PC.
+Proof.
+ intros. apply data_diff; auto with asmgen.
+Qed.
+
+Lemma preg_of_not_SP:
+ forall r, preg_of r <> SP.
+Proof.
+ intros. unfold preg_of; destruct r; simpl; try discriminate.
+Qed.
+
+Ltac desif :=
+ repeat match goal with
+ | [ |- context[ if ?f then _ else _ ] ] => destruct f
+ end.
+
+Ltac decomp :=
+ repeat match goal with
+ | [ |- context[ match (?rs ?r) with | _ => _ end ] ] => destruct (rs r)
+ end.
+
+Ltac Simplif :=
+ ((desif)
+ || (try unfold compare_long)
+ || (try unfold compare_int)
+ || (try unfold compare_float)
+ || (try unfold compare_single)
+ || decomp
+ || (rewrite Pregmap.gss)
+ || (rewrite Pregmap.gso by eauto with asmgen)
+ ); auto with asmgen.
+
+Ltac Simpl := repeat Simplif.
+
+Section EPC.
+
+Variable lk: aarch64_linker.
+
+(* For Asmblockgenproof0 *)
+
+Theorem exec_basic_instr_pc:
+ forall ge b rs1 m1 rs2 m2,
+ exec_basic lk ge b rs1 m1 = Next rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ intros. destruct b; try destruct i; try destruct i; try destruct ld; try destruct ld;
+ try destruct st; try destruct st; try destruct a.
+ all: try (inv H; simpl in *; auto; Simpl).
+ all: try (try unfold exec_load_rd_a in H1; try destruct (Mem.loadv _ _ _); inv H1; Simpl).
+ all: try (try unfold exec_load_double in H0; destruct (Mem.loadv _ _ _); simpl in *;
+ try destruct (Mem.loadv _ _ _); simpl in *; inv H0; Simpl).
+ all: try (try unfold exec_store_rs_a in H0; try destruct (Mem.storev _ _ _); inv H0; auto; Simpl).
+ all: try (try unfold exec_store_double in H1; destruct (Mem.storev _ _ _); simpl in *;
+ try destruct (Mem.storev _ _ _); simpl in *; inv H1; auto; Simpl).
+ - (* alloc *)
+ destruct (Mem.alloc _ _ _); destruct (Mem.store _ _ _); inv H1; auto; Simpl.
+ - (* free *)
+ destruct (rs1 SP); try discriminate; destruct (Mem.free _ _ _ _); inv H1; Simpl.
+Qed.
+
+End EPC.
diff --git a/aarch64/Asmexpand.ml b/aarch64/Asmexpand.ml
index 573e8b92..eb9ec600 100644
--- a/aarch64/Asmexpand.ml
+++ b/aarch64/Asmexpand.ml
@@ -34,13 +34,13 @@ let _m1 = Z.of_sint (-1)
(* Emit instruction sequences that set or offset a register by a constant. *)
let expand_loadimm32 (dst: ireg) n =
- List.iter emit (Asmgen.loadimm32 dst n [])
+ List.iter emit (Asmgen.Asmgen_expand.loadimm32 dst n [])
let expand_addimm64 (dst: iregsp) (src: iregsp) n =
- List.iter emit (Asmgen.addimm64 dst src n [])
+ List.iter emit (Asmgen.Asmgen_expand.addimm64 dst src n [])
let expand_storeptr (src: ireg) (base: iregsp) ofs =
- List.iter emit (Asmgen.storeptr src base ofs [])
+ List.iter emit (Asmgen.Asmgen_expand.storeptr src base ofs [])
(* Handling of varargs *)
@@ -84,8 +84,8 @@ let save_parameter_registers ir fr =
while !i < 8 do
let pos = 8*16 + !i*8 in
if !i land 1 = 0 then begin
- emit (Pstp(int_param_regs.(!i), int_param_regs.(!i + 1),
- ADimm(XSP, Z.of_uint pos)));
+ emit (Pstpx(int_param_regs.(!i), int_param_regs.(!i + 1),
+ Mint64, Mint64, ADimm(XSP, Z.of_uint pos)));
i := !i + 2
end else begin
emit (Pstrx(int_param_regs.(!i), ADimm(XSP, Z.of_uint pos)));
@@ -160,9 +160,9 @@ let expand_builtin_va_start r =
let expand_annot_val kind txt targ args res =
emit (Pbuiltin (EF_annot(kind,txt,[targ]), args, BR_none));
match args, res with
- | [BA(IR src)], BR(IR dst) ->
- if dst <> src then emit (Pmov (RR1 dst, RR1 src))
- | [BA(FR src)], BR(FR dst) ->
+ | [BA(DR(IR src))], BR(DR(IR dst)) ->
+ if dst <> src then emit (Pmov (dst, src))
+ | [BA(DR(FR src))], BR(DR(FR dst)) ->
if dst <> src then emit (Pfmov (dst, src))
| _, _ ->
raise (Error "ill-formed __builtin_annot_val")
@@ -180,8 +180,8 @@ let offset_in_range ofs =
let memcpy_small_arg sz arg tmp =
match arg with
- | BA (IR r) ->
- (RR1 r, _0)
+ | BA (DR(IR r)) ->
+ (r, _0)
| BA_addrstack ofs ->
if offset_in_range ofs
&& offset_in_range (Ptrofs.add ofs (Ptrofs.repr (Z.of_uint sz)))
@@ -192,14 +192,14 @@ let memcpy_small_arg sz arg tmp =
assert false
let expand_builtin_memcpy_small sz al src dst =
- let tsrc = if dst <> BA (IR X17) then X17 else X29 in
- let tdst = if src <> BA (IR X29) then X29 else X17 in
+ let tsrc = if dst <> BA (DR(IR(RR1 X17))) then X17 else X29 in
+ let tdst = if src <> BA (DR(IR(RR1 X29))) then X29 else X17 in
let (rsrc, osrc) = memcpy_small_arg sz src tsrc in
let (rdst, odst) = memcpy_small_arg sz dst tdst in
let rec copy osrc odst sz =
if sz >= 16 then begin
- emit (Pldp(X16, X30, ADimm(rsrc, osrc)));
- emit (Pstp(X16, X30, ADimm(rdst, odst)));
+ emit (Pldpx(X16, X30, Mint64, Mint64, ADimm(rsrc, osrc)));
+ emit (Pstpx(X16, X30, Mint64, Mint64, ADimm(rdst, odst)));
copy (Ptrofs.add osrc _16) (Ptrofs.add odst _16) (sz - 16)
end
else if sz >= 8 then begin
@@ -226,7 +226,7 @@ let expand_builtin_memcpy_small sz al src dst =
let memcpy_big_arg arg tmp =
match arg with
- | BA (IR r) -> emit (Pmov(RR1 tmp, RR1 r))
+ | BA (DR(IR r)) -> emit (Pmov(RR1 tmp, r))
| BA_addrstack ofs -> expand_addimm64 (RR1 tmp) XSP ofs
| _ -> assert false
@@ -237,8 +237,8 @@ let expand_builtin_memcpy_big sz al src dst =
let lbl = new_label () in
expand_loadimm32 X15 (Z.of_uint (sz / 16));
emit (Plabel lbl);
- emit (Pldp(X16, X17, ADpostincr(RR1 X30, _16)));
- emit (Pstp(X16, X17, ADpostincr(RR1 X29, _16)));
+ emit (Pldpx(X16, X17, Mint64, Mint64, ADpostincr(RR1 X30, _16)));
+ emit (Pstpx(X16, X17, Mint64, Mint64, ADpostincr(RR1 X29, _16)));
emit (Psubimm(W, RR1 X15, RR1 X15, _1));
emit (Pcbnz(W, X15, lbl));
if sz mod 16 >= 8 then begin
@@ -270,29 +270,29 @@ let expand_builtin_memcpy sz al args =
let expand_builtin_vload_common chunk base ofs res =
let addr = ADimm(base, ofs) in
match chunk, res with
- | Mint8unsigned, BR(IR res) ->
+ | Mint8unsigned, BR(DR(IR(RR1 res))) ->
emit (Pldrb(W, res, addr))
- | Mint8signed, BR(IR res) ->
+ | Mint8signed, BR(DR(IR(RR1 res))) ->
emit (Pldrsb(W, res, addr))
- | Mint16unsigned, BR(IR res) ->
+ | Mint16unsigned, BR(DR(IR(RR1 res))) ->
emit (Pldrh(W, res, addr))
- | Mint16signed, BR(IR res) ->
+ | Mint16signed, BR(DR(IR(RR1 res))) ->
emit (Pldrsh(W, res, addr))
- | Mint32, BR(IR res) ->
+ | Mint32, BR(DR(IR(RR1 res))) ->
emit (Pldrw(res, addr))
- | Mint64, BR(IR res) ->
+ | Mint64, BR(DR(IR(RR1 res))) ->
emit (Pldrx(res, addr))
- | Mfloat32, BR(FR res) ->
+ | Mfloat32, BR(DR(FR res)) ->
emit (Pldrs(res, addr))
- | Mfloat64, BR(FR res) ->
+ | Mfloat64, BR(DR(FR res)) ->
emit (Pldrd(res, addr))
| _ ->
assert false
let expand_builtin_vload chunk args res =
match args with
- | [BA(IR addr)] ->
- expand_builtin_vload_common chunk (RR1 addr) _0 res
+ | [BA(DR(IR addr))] ->
+ expand_builtin_vload_common chunk addr _0 res
| [BA_addrstack ofs] ->
if offset_in_range (Z.add ofs (Memdata.size_chunk chunk)) then
expand_builtin_vload_common chunk XSP ofs res
@@ -300,11 +300,11 @@ let expand_builtin_vload chunk args res =
expand_addimm64 (RR1 X16) XSP ofs; (* X16 <- SP + ofs *)
expand_builtin_vload_common chunk (RR1 X16) _0 res
end
- | [BA_addptr(BA(IR addr), BA_long ofs)] ->
+ | [BA_addptr(BA(DR(IR addr)), BA_long ofs)] ->
if offset_in_range (Z.add ofs (Memdata.size_chunk chunk)) then
- expand_builtin_vload_common chunk (RR1 addr) ofs res
+ expand_builtin_vload_common chunk addr ofs res
else begin
- expand_addimm64 (RR1 X16) (RR1 addr) ofs; (* X16 <- addr + ofs *)
+ expand_addimm64 (RR1 X16) addr ofs; (* X16 <- addr + ofs *)
expand_builtin_vload_common chunk (RR1 X16) _0 res
end
| _ ->
@@ -313,25 +313,25 @@ let expand_builtin_vload chunk args res =
let expand_builtin_vstore_common chunk base ofs src =
let addr = ADimm(base, ofs) in
match chunk, src with
- | (Mint8signed | Mint8unsigned), BA(IR src) ->
+ | (Mint8signed | Mint8unsigned), BA(DR(IR(RR1 src))) ->
emit (Pstrb(src, addr))
- | (Mint16signed | Mint16unsigned), BA(IR src) ->
+ | (Mint16signed | Mint16unsigned), BA(DR(IR(RR1 src))) ->
emit (Pstrh(src, addr))
- | Mint32, BA(IR src) ->
+ | Mint32, BA(DR(IR(RR1 src))) ->
emit (Pstrw(src, addr))
- | Mint64, BA(IR src) ->
+ | Mint64, BA(DR(IR(RR1 src))) ->
emit (Pstrx(src, addr))
- | Mfloat32, BA(FR src) ->
+ | Mfloat32, BA(DR(FR src)) ->
emit (Pstrs(src, addr))
- | Mfloat64, BA(FR src) ->
+ | Mfloat64, BA(DR(FR src)) ->
emit (Pstrd(src, addr))
| _ ->
assert false
let expand_builtin_vstore chunk args =
match args with
- | [BA(IR addr); src] ->
- expand_builtin_vstore_common chunk (RR1 addr) _0 src
+ | [BA(DR(IR addr)); src] ->
+ expand_builtin_vstore_common chunk addr _0 src
| [BA_addrstack ofs; src] ->
if offset_in_range (Z.add ofs (Memdata.size_chunk chunk)) then
expand_builtin_vstore_common chunk XSP ofs src
@@ -339,11 +339,11 @@ let expand_builtin_vstore chunk args =
expand_addimm64 (RR1 X16) XSP ofs; (* X16 <- SP + ofs *)
expand_builtin_vstore_common chunk (RR1 X16) _0 src
end
- | [BA_addptr(BA(IR addr), BA_long ofs); src] ->
+ | [BA_addptr(BA(DR(IR addr)), BA_long ofs); src] ->
if offset_in_range (Z.add ofs (Memdata.size_chunk chunk)) then
- expand_builtin_vstore_common chunk (RR1 addr) ofs src
+ expand_builtin_vstore_common chunk addr ofs src
else begin
- expand_addimm64 (RR1 X16) (RR1 addr) ofs; (* X16 <- addr + ofs *)
+ expand_addimm64 (RR1 X16) addr ofs; (* X16 <- addr + ofs *)
expand_builtin_vstore_common chunk (RR1 X16) _0 src
end
| _ ->
@@ -363,45 +363,47 @@ let expand_builtin_inline name args res =
| "__builtin_unreachable", [], _ ->
()
(* Byte swap *)
- | ("__builtin_bswap" | "__builtin_bswap32"), [BA(IR a1)], BR(IR res) ->
+ | ("__builtin_bswap" | "__builtin_bswap32"), [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Prev(W, res, a1))
- | "__builtin_bswap64", [BA(IR a1)], BR(IR res) ->
+ | "__builtin_bswap64", [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Prev(X, res, a1))
- | "__builtin_bswap16", [BA(IR a1)], BR(IR res) ->
+ | "__builtin_bswap16", [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Prev16(W, res, a1));
emit (Pandimm(W, res, RR0 res, Z.of_uint 0xFFFF))
(* Count leading zeros, leading sign bits, trailing zeros *)
- | "__builtin_clz", [BA(IR a1)], BR(IR res) ->
+ | "__builtin_clz", [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Pclz(W, res, a1))
- | ("__builtin_clzl" | "__builtin_clzll"), [BA(IR a1)], BR(IR res) ->
+ | ("__builtin_clzl" | "__builtin_clzll"), [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Pclz(X, res, a1))
- | "__builtin_cls", [BA(IR a1)], BR(IR res) ->
+ | "__builtin_cls", [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Pcls(W, res, a1))
- | ("__builtin_clsl" | "__builtin_clsll"), [BA(IR a1)], BR(IR res) ->
+ | ("__builtin_clsl" | "__builtin_clsll"), [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Pcls(X, res, a1))
- | "__builtin_ctz", [BA(IR a1)], BR(IR res) ->
+ | "__builtin_ctz", [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Prbit(W, res, a1));
emit (Pclz(W, res, res))
- | ("__builtin_ctzl" | "__builtin_ctzll"), [BA(IR a1)], BR(IR res) ->
+ | ("__builtin_ctzl" | "__builtin_ctzll"), [BA(DR(IR(RR1 a1)))], BR(DR(IR(RR1 res))) ->
emit (Prbit(X, res, a1));
emit (Pclz(X, res, res))
(* Float arithmetic *)
- | ("__builtin_fsqrt" | "__builtin_sqrt"), [BA(FR a1)], BR(FR res) ->
+ | "__builtin_fabs", [BA(DR(FR a1))], BR(DR(FR res)) ->
+ emit (Pfabs(D, res, a1))
+ | ("__builtin_fsqrt" | "__builtin_sqrt"), [BA(DR(FR a1))], BR(DR(FR res)) ->
emit (Pfsqrt(D, res, a1))
- | "__builtin_fmadd", [BA(FR a1); BA(FR a2); BA(FR a3)], BR(FR res) ->
+ | "__builtin_fmadd", [BA(DR(FR a1)); BA(DR(FR a2)); BA(DR(FR a3))], BR(DR(FR res)) ->
emit (Pfmadd(D, res, a1, a2, a3))
- | "__builtin_fmsub", [BA(FR a1); BA(FR a2); BA(FR a3)], BR(FR res) ->
+ | "__builtin_fmsub", [BA(DR(FR a1)); BA(DR(FR a2)); BA(DR(FR a3))], BR(DR(FR res)) ->
emit (Pfmsub(D, res, a1, a2, a3))
- | "__builtin_fnmadd", [BA(FR a1); BA(FR a2); BA(FR a3)], BR(FR res) ->
+ | "__builtin_fnmadd", [BA(DR(FR a1)); BA(DR(FR a2)); BA(DR(FR a3))], BR(DR(FR res)) ->
emit (Pfnmadd(D, res, a1, a2, a3))
- | "__builtin_fnmsub", [BA(FR a1); BA(FR a2); BA(FR a3)], BR(FR res) ->
+ | "__builtin_fnmsub", [BA(DR(FR a1)); BA(DR(FR a2)); BA(DR(FR a3))], BR(DR(FR res)) ->
emit (Pfnmsub(D, res, a1, a2, a3))
- | "__builtin_fmax", [BA (FR a1); BA (FR a2)], BR (FR res) ->
+ | "__builtin_fmax", [BA(DR(FR a1)); BA(DR(FR a2))], BR(DR(FR res)) ->
emit (Pfmax (D, res, a1, a2))
- | "__builtin_fmin", [BA (FR a1); BA (FR a2)], BR (FR res) ->
+ | "__builtin_fmin", [BA(DR(FR a1)); BA(DR(FR a2))], BR(DR(FR res)) ->
emit (Pfmin (D, res, a1, a2))
(* Vararg *)
- | "__builtin_va_start", [BA(IR a)], _ ->
+ | "__builtin_va_start", [BA(DR(IR(RR1 a)))], _ ->
expand_builtin_va_start a
(* Catch-all *)
| _ ->
@@ -441,7 +443,7 @@ let expand_instruction instr =
expand_annot_val kind txt targ args res
| EF_memcpy(sz, al) ->
expand_builtin_memcpy (Z.to_int sz) (Z.to_int al) args
- | EF_annot _ | EF_debug _ | EF_inline_asm _ ->
+ | EF_annot _ | EF_debug _ | EF_inline_asm _ | EF_profiling _ ->
emit instr
| _ ->
assert false
@@ -468,9 +470,9 @@ let float_reg_to_dwarf = function
| D30 -> 94 | D31 -> 95
let preg_to_dwarf = function
- | IR r -> int_reg_to_dwarf r
- | FR r -> float_reg_to_dwarf r
- | SP -> 31
+ | DR(IR(RR1 r)) -> int_reg_to_dwarf r
+ | DR(FR r) -> float_reg_to_dwarf r
+ | DR(IR(XSP)) -> 31
| _ -> assert false
let expand_function id fn =
diff --git a/aarch64/Asmgen.v b/aarch64/Asmgen.v
index baaab6c4..6bb791c4 100644
--- a/aarch64/Asmgen.v
+++ b/aarch64/Asmgen.v
@@ -1,168 +1,46 @@
-(* *********************************************************************)
-(* *)
-(* The Compcert verified compiler *)
-(* *)
-(* Xavier Leroy, Collège de France and INRIA Paris *)
-(* *)
-(* Copyright Institut National de Recherche en Informatique et en *)
-(* Automatique. All rights reserved. This file is distributed *)
-(* under the terms of the INRIA Non-Commercial License Agreement. *)
-(* *)
-(* *********************************************************************)
-
-(** Translation from Mach to AArch64. *)
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Justus Fasse UGA, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
Require Import Recdef Coqlib Zwf Zbits.
Require Import Errors AST Integers Floats Op.
-Require Import Locations Mach Asm.
-Require SelectOp.
+Require Import Locations Compopts.
+Require Import Mach Asm Asmblock Asmblockgen Machblockgen PostpassScheduling.
-Local Open Scope string_scope.
-Local Open Scope list_scope.
Local Open Scope error_monad_scope.
-(** Alignment check for symbols *)
-
-Parameter symbol_is_aligned : ident -> Z -> bool.
-(** [symbol_is_aligned id sz] checks whether the symbol [id] is [sz] aligned *)
-
-(** Extracting integer or float registers. *)
-
-Definition ireg_of (r: mreg) : res ireg :=
- match preg_of r with IR mr => OK mr | _ => Error(msg "Asmgen.ireg_of") end.
-
-Definition freg_of (r: mreg) : res freg :=
- match preg_of r with FR mr => OK mr | _ => Error(msg "Asmgen.freg_of") end.
-
-(** Recognition of immediate arguments for logical integer operations.*)
-
-(** Valid immediate arguments are repetitions of a bit pattern [B]
- of length [e] = 2, 4, 8, 16, 32 or 64.
- The bit pattern [B] must be of the form [0*1*0*] or [1*0*1*]
- but must not be all zeros or all ones. *)
-
-(** The following automaton recognizes [0*1*0*|1*0*1*].
-<<
- 0 1 0
- / \ / \ / \
- \ / \ / \ /
- -0--> [B] --1--> [D] --0--> [F]
- /
- [A]
- \
- -1--> [C] --0--> [E] --1--> [G]
- / \ / \ / \
- \ / \ / \ /
- 1 0 1
->>
-*)
-
-Module Automaton.
-
-Inductive state : Type := SA | SB | SC | SD | SE | SF | SG | Sbad.
-
-Definition start := SA.
-
-Definition next (s: state) (b: bool) :=
- match s, b with
- | SA,false => SB | SA,true => SC
- | SB,false => SB | SB,true => SD
- | SC,false => SE | SC,true => SC
- | SD,false => SF | SD,true => SD
- | SE,false => SE | SE,true => SG
- | SF,false => SF | SF,true => Sbad
- | SG,false => Sbad | SG,true => SG
- | Sbad,_ => Sbad
- end.
-
-Definition accepting (s: state) :=
- match s with
- | SA | SB | SC | SD | SE | SF | SG => true
- | Sbad => false
- end.
-
-Fixpoint run (len: nat) (s: state) (x: Z) : bool :=
- match len with
- | Datatypes.O => accepting s
- | Datatypes.S len => run len (next s (Z.odd x)) (Z.div2 x)
- end.
-
-End Automaton.
-
-(** The following function determines the candidate length [e],
- ensuring that [x] is a repetition [BB...B]
- of a bit pattern [B] of length [e]. *)
-
-Definition logical_imm_length (x: Z) (sixtyfour: bool) : nat :=
- (** [test n] checks that the low [2n] bits of [x] are of the
- form [BB], that is, two occurrences of the same [n] bits *)
- let test (n: Z) : bool :=
- Z.eqb (Zzero_ext n x) (Zzero_ext n (Z.shiftr x n)) in
- (** If [test n] fails, we know that the candidate length [e] is
- at least [2n]. Hence we test with decreasing values of [n]:
- 32, 16, 8, 4, 2. *)
- if sixtyfour && negb (test 32) then 64%nat
- else if negb (test 16) then 32%nat
- else if negb (test 8) then 16%nat
- else if negb (test 4) then 8%nat
- else if negb (test 2) then 4%nat
- else 2%nat.
+(** Functions called by the Asmexpand ocaml file, inspired and adapted from Asmblockgen.v *)
-(** A valid logical immediate is
-- neither [0] nor [-1];
-- composed of a repetition [BBBBB] of a bit-pattern [B] of length [e]
-- the low [e] bits of the number, that is, [B], match [0*1*0*] or [1*0*1*].
-*)
+Module Asmgen_expand.
-Definition is_logical_imm32 (x: int) : bool :=
- negb (Int.eq x Int.zero) && negb (Int.eq x Int.mone) &&
- Automaton.run (logical_imm_length (Int.unsigned x) false)
- Automaton.start (Int.unsigned x).
-
-Definition is_logical_imm64 (x: int64) : bool :=
- negb (Int64.eq x Int64.zero) && negb (Int64.eq x Int64.mone) &&
- Automaton.run (logical_imm_length (Int64.unsigned x) true)
- Automaton.start (Int64.unsigned x).
-
-(** Arithmetic immediates are 12-bit unsigned numbers, possibly shifted left 12 bits *)
-
-Definition is_arith_imm32 (x: int) : bool :=
- Int.eq x (Int.zero_ext 12 x)
- || Int.eq x (Int.shl (Int.zero_ext 12 (Int.shru x (Int.repr 12))) (Int.repr 12)).
-
-Definition is_arith_imm64 (x: int64) : bool :=
- Int64.eq x (Int64.zero_ext 12 x)
- || Int64.eq x (Int64.shl (Int64.zero_ext 12 (Int64.shru x (Int64.repr 12))) (Int64.repr 12)).
-
-(** Decompose integer literals into 16-bit fragments *)
-
-Fixpoint decompose_int (N: nat) (n p: Z) {struct N} : list (Z * Z) :=
- match N with
- | Datatypes.O => nil
- | Datatypes.S N =>
- let frag := Zzero_ext 16 (Z.shiftr n p) in
- if Z.eqb frag 0 then
- decompose_int N n (p + 16)
- else
- (frag, p) :: decompose_int N (Z.ldiff n (Z.shiftl 65535 p)) (p + 16)
- end.
-
-Definition negate_decomposition (l: list (Z * Z)) :=
- List.map (fun np => (Z.lxor (fst np) 65535, snd np)) l.
+(* Load immediate *)
Definition loadimm_k (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: code) : code :=
- List.fold_right (fun np k => Pmovk sz rd (fst np) (snd np) :: k) k l.
+ List.fold_right (fun np k => Asm.Pmovk sz rd (fst np) (snd np) :: k) k l.
Definition loadimm_z (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: code) : code :=
match l with
- | nil => Pmovz sz rd 0 0 :: k
- | (n1, p1) :: l => Pmovz sz rd n1 p1 :: loadimm_k sz rd l k
+ | nil => Asm.Pmovz sz rd 0 0 :: k
+ | (n1, p1) :: l => Asm.Pmovz sz rd n1 p1 :: loadimm_k sz rd l k
end.
Definition loadimm_n (sz: isize) (rd: ireg) (l: list (Z * Z)) (k: code) : code :=
match l with
- | nil => Pmovn sz rd 0 0 :: k
- | (n1, p1) :: l => Pmovn sz rd n1 p1 :: loadimm_k sz rd (negate_decomposition l) k
+ | nil => Asm.Pmovn sz rd 0 0 :: k
+ | (n1, p1) :: l => Asm.Pmovn sz rd n1 p1 :: loadimm_k sz rd (negate_decomposition l) k
end.
Definition loadimm (sz: isize) (rd: ireg) (n: Z) (k: code) : code :=
@@ -175,15 +53,15 @@ Definition loadimm (sz: isize) (rd: ireg) (n: Z) (k: code) : code :=
Definition loadimm32 (rd: ireg) (n: int) (k: code) : code :=
if is_logical_imm32 n
- then Porrimm W rd XZR (Int.unsigned n) :: k
+ then Asm.Porrimm W rd XZR (Int.unsigned n) :: k
else loadimm W rd (Int.unsigned n) k.
Definition loadimm64 (rd: ireg) (n: int64) (k: code) : code :=
if is_logical_imm64 n
- then Porrimm X rd XZR (Int64.unsigned n) :: k
+ then Asm.Porrimm X rd XZR (Int64.unsigned n) :: k
else loadimm X rd (Int64.unsigned n) k.
-(** Add immediate *)
+(* Add immediate *)
Definition addimm_aux (insn: iregsp -> iregsp -> Z -> instruction)
(rd r1: iregsp) (n: Z) (k: code) :=
@@ -196,962 +74,393 @@ Definition addimm_aux (insn: iregsp -> iregsp -> Z -> instruction)
else
insn rd r1 nhi :: insn rd rd nlo :: k.
-Definition addimm32 (rd r1: ireg) (n: int) (k: code) : code :=
- let m := Int.neg n in
- if Int.eq n (Int.zero_ext 24 n) then
- addimm_aux (Paddimm W) rd r1 (Int.unsigned n) k
- else if Int.eq m (Int.zero_ext 24 m) then
- addimm_aux (Psubimm W) rd r1 (Int.unsigned m) k
- else if Int.lt n Int.zero then
- loadimm32 X16 m (Psub W rd r1 X16 SOnone :: k)
- else
- loadimm32 X16 n (Padd W rd r1 X16 SOnone :: k).
-
Definition addimm64 (rd r1: iregsp) (n: int64) (k: code) : code :=
let m := Int64.neg n in
if Int64.eq n (Int64.zero_ext 24 n) then
- addimm_aux (Paddimm X) rd r1 (Int64.unsigned n) k
+ addimm_aux (Asm.Paddimm X) rd r1 (Int64.unsigned n) k
else if Int64.eq m (Int64.zero_ext 24 m) then
- addimm_aux (Psubimm X) rd r1 (Int64.unsigned m) k
+ addimm_aux (Asm.Psubimm X) rd r1 (Int64.unsigned m) k
else if Int64.lt n Int64.zero then
- loadimm64 X16 m (Psubext rd r1 X16 (EOuxtx Int.zero) :: k)
- else
- loadimm64 X16 n (Paddext rd r1 X16 (EOuxtx Int.zero) :: k).
-
-(** Logical immediate *)
-
-Definition logicalimm32
- (insn1: ireg -> ireg0 -> Z -> instruction)
- (insn2: ireg -> ireg0 -> ireg -> shift_op -> instruction)
- (rd r1: ireg) (n: int) (k: code) : code :=
- if is_logical_imm32 n
- then insn1 rd r1 (Int.unsigned n) :: k
- else loadimm32 X16 n (insn2 rd r1 X16 SOnone :: k).
-
-Definition logicalimm64
- (insn1: ireg -> ireg0 -> Z -> instruction)
- (insn2: ireg -> ireg0 -> ireg -> shift_op -> instruction)
- (rd r1: ireg) (n: int64) (k: code) : code :=
- if is_logical_imm64 n
- then insn1 rd r1 (Int64.unsigned n) :: k
- else loadimm64 X16 n (insn2 rd r1 X16 SOnone :: k).
-
-(** Sign- or zero-extended arithmetic *)
-
-Definition transl_extension (ex: extension) (a: int) : extend_op :=
- match ex with Xsgn32 => EOsxtw a | Xuns32 => EOuxtw a end.
-
-Definition move_extended_base
- (rd: ireg) (r1: ireg) (ex: extension) (k: code) : code :=
- match ex with
- | Xsgn32 => Pcvtsw2x rd r1 :: k
- | Xuns32 => Pcvtuw2x rd r1 :: k
- end.
-
-Definition move_extended
- (rd: ireg) (r1: ireg) (ex: extension) (a: int) (k: code) : code :=
- if Int.eq a Int.zero then
- move_extended_base rd r1 ex k
- else
- move_extended_base rd r1 ex (Padd X rd XZR rd (SOlsl a) :: k).
-
-Definition arith_extended
- (insnX: iregsp -> iregsp -> ireg -> extend_op -> instruction)
- (insnS: ireg -> ireg0 -> ireg -> shift_op -> instruction)
- (rd r1 r2: ireg) (ex: extension) (a: int) (k: code) : code :=
- if Int.ltu a (Int.repr 5) then
- insnX rd r1 r2 (transl_extension ex a) :: k
- else
- move_extended_base X16 r2 ex (insnS rd r1 X16 (SOlsl a) :: k).
-
-(** Extended right shift *)
-
-Definition shrx32 (rd r1: ireg) (n: int) (k: code) : code :=
- if Int.eq n Int.zero then
- Pmov rd r1 :: k
- else
- Porr W X16 XZR r1 (SOasr (Int.repr 31)) ::
- Padd W X16 r1 X16 (SOlsr (Int.sub Int.iwordsize n)) ::
- Porr W rd XZR X16 (SOasr n) :: k.
-
-Definition shrx64 (rd r1: ireg) (n: int) (k: code) : code :=
- if Int.eq n Int.zero then
- Pmov rd r1 :: k
- else
- Porr X X16 XZR r1 (SOasr (Int.repr 63)) ::
- Padd X X16 r1 X16 (SOlsr (Int.sub Int64.iwordsize' n)) ::
- Porr X rd XZR X16 (SOasr n) :: k.
-
-(** Load the address [id + ofs] in [rd] *)
-
-Definition loadsymbol (rd: ireg) (id: ident) (ofs: ptrofs) (k: code) : code :=
- if SelectOp.symbol_is_relocatable id then
- if Ptrofs.eq ofs Ptrofs.zero then
- Ploadsymbol rd id :: k
- else
- Ploadsymbol rd id :: addimm64 rd rd (Ptrofs.to_int64 ofs) k
+ loadimm64 X16 m (Asm.Psubext rd r1 X16 (EOuxtx Int.zero) :: k)
else
- Padrp rd id ofs :: Paddadr rd rd id ofs :: k.
+ loadimm64 X16 n (Asm.Paddext rd r1 X16 (EOuxtx Int.zero) :: k).
-(** Translate a shifted operand *)
+(** Register-indexed stores *)
-Definition transl_shift (s: Op.shift) (a: int): Asm.shift_op :=
- match s with
- | Slsl => SOlsl a
- | Slsr => SOlsr a
- | Sasr => SOasr a
- | Sror => SOror a
- end.
-
-(** Translation of a condition. Prepends to [k] the instructions
- that evaluate the condition and leave its boolean result in one of
- the bits of the condition register. The bit in question is
- determined by the [crbit_for_cond] function. *)
+Definition indexed_memory_access (insn: Asm.addressing -> instruction)
+ (sz: Z) (base: iregsp) (ofs: ptrofs) (k: code) :=
+ let ofs := Ptrofs.to_int64 ofs in
+ if offset_representable sz ofs
+ then insn (ADimm base ofs) :: k
+ else loadimm64 X16 ofs (insn (ADreg base X16) :: k).
-Definition transl_cond
- (cond: condition) (args: list mreg) (k: code) :=
- match cond, args with
- | (Ccomp c | Ccompu c), a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pcmp W r1 r2 SOnone :: k)
- | (Ccompshift c s a | Ccompushift c s a), a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pcmp W r1 r2 (transl_shift s a) :: k)
- | (Ccompimm c n | Ccompuimm c n), a1 :: nil =>
- do r1 <- ireg_of a1;
- OK (if is_arith_imm32 n then
- Pcmpimm W r1 (Int.unsigned n) :: k
- else if is_arith_imm32 (Int.neg n) then
- Pcmnimm W r1 (Int.unsigned (Int.neg n)) :: k
- else
- loadimm32 X16 n (Pcmp W r1 X16 SOnone :: k))
- | (Cmaskzero n | Cmasknotzero n), a1 :: nil =>
- do r1 <- ireg_of a1;
- OK (if is_logical_imm32 n then
- Ptstimm W r1 (Int.unsigned n) :: k
- else
- loadimm32 X16 n (Ptst W r1 X16 SOnone :: k))
- | (Ccompl c | Ccomplu c), a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pcmp X r1 r2 SOnone :: k)
- | (Ccomplshift c s a | Ccomplushift c s a), a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pcmp X r1 r2 (transl_shift s a) :: k)
- | (Ccomplimm c n | Ccompluimm c n), a1 :: nil =>
- do r1 <- ireg_of a1;
- OK (if is_arith_imm64 n then
- Pcmpimm X r1 (Int64.unsigned n) :: k
- else if is_arith_imm64 (Int64.neg n) then
- Pcmnimm X r1 (Int64.unsigned (Int64.neg n)) :: k
- else
- loadimm64 X16 n (Pcmp X r1 X16 SOnone :: k))
- | (Cmasklzero n | Cmasklnotzero n), a1 :: nil =>
- do r1 <- ireg_of a1;
- OK (if is_logical_imm64 n then
- Ptstimm X r1 (Int64.unsigned n) :: k
- else
- loadimm64 X16 n (Ptst X r1 X16 SOnone :: k))
- | Ccompf cmp, a1 :: a2 :: nil =>
- do r1 <- freg_of a1; do r2 <- freg_of a2;
- OK (Pfcmp D r1 r2 :: k)
- | Cnotcompf cmp, a1 :: a2 :: nil =>
- do r1 <- freg_of a1; do r2 <- freg_of a2;
- OK (Pfcmp D r1 r2 :: k)
- | Ccompfzero cmp, a1 :: nil =>
- do r1 <- freg_of a1;
- OK (Pfcmp0 D r1 :: k)
- | Cnotcompfzero cmp, a1 :: nil =>
- do r1 <- freg_of a1;
- OK (Pfcmp0 D r1 :: k)
- | Ccompfs cmp, a1 :: a2 :: nil =>
- do r1 <- freg_of a1; do r2 <- freg_of a2;
- OK (Pfcmp S r1 r2 :: k)
- | Cnotcompfs cmp, a1 :: a2 :: nil =>
- do r1 <- freg_of a1; do r2 <- freg_of a2;
- OK (Pfcmp S r1 r2 :: k)
- | Ccompfszero cmp, a1 :: nil =>
- do r1 <- freg_of a1;
- OK (Pfcmp0 S r1 :: k)
- | Cnotcompfszero cmp, a1 :: nil =>
- do r1 <- freg_of a1;
- OK (Pfcmp0 S r1 :: k)
- | _, _ =>
- Error(msg "Asmgen.transl_cond")
- end.
+Definition storeptr (src: ireg) (base: iregsp) (ofs: ptrofs) (k: code) :=
+ indexed_memory_access (Asm.Pstrx src) 8 base ofs k.
-Definition cond_for_signed_cmp (cmp: comparison) :=
- match cmp with
- | Ceq => TCeq
- | Cne => TCne
- | Clt => TClt
- | Cle => TCle
- | Cgt => TCgt
- | Cge => TCge
- end.
+End Asmgen_expand.
-Definition cond_for_unsigned_cmp (cmp: comparison) :=
- match cmp with
- | Ceq => TCeq
- | Cne => TCne
- | Clt => TClo
- | Cle => TCls
- | Cgt => TChi
- | Cge => TChs
- end.
+(** * Translation from Asmblock to assembly language
+ Inspired from the KVX backend (see kvx/Asm.v and kvx/Asmgen.v) *)
-Definition cond_for_float_cmp (cmp: comparison) :=
- match cmp with
- | Ceq => TCeq
- | Cne => TCne
- | Clt => TCmi
- | Cle => TCls
- | Cgt => TCgt
- | Cge => TCge
- end.
+Module Asmblock_TRANSF.
+(* STUB *)
-Definition cond_for_float_not_cmp (cmp: comparison) :=
- match cmp with
- | Ceq => TCne
- | Cne => TCeq
- | Clt => TCpl
- | Cle => TChi
- | Cgt => TCle
- | Cge => TClt
+Definition ireg_of_preg (p : Asm.preg) : res ireg :=
+ match p with
+ | DR (IR (RR1 r)) => OK r
+ | _ => Error (msg "Asmgen.ireg_of_preg")
end.
-Definition cond_for_cond (cond: condition) :=
- match cond with
- | Ccomp cmp => cond_for_signed_cmp cmp
- | Ccompu cmp => cond_for_unsigned_cmp cmp
- | Ccompshift cmp s a => cond_for_signed_cmp cmp
- | Ccompushift cmp s a => cond_for_unsigned_cmp cmp
- | Ccompimm cmp n => cond_for_signed_cmp cmp
- | Ccompuimm cmp n => cond_for_unsigned_cmp cmp
- | Cmaskzero n => TCeq
- | Cmasknotzero n => TCne
- | Ccompl cmp => cond_for_signed_cmp cmp
- | Ccomplu cmp => cond_for_unsigned_cmp cmp
- | Ccomplshift cmp s a => cond_for_signed_cmp cmp
- | Ccomplushift cmp s a => cond_for_unsigned_cmp cmp
- | Ccomplimm cmp n => cond_for_signed_cmp cmp
- | Ccompluimm cmp n => cond_for_unsigned_cmp cmp
- | Cmasklzero n => TCeq
- | Cmasklnotzero n => TCne
- | Ccompf cmp => cond_for_float_cmp cmp
- | Cnotcompf cmp => cond_for_float_not_cmp cmp
- | Ccompfzero cmp => cond_for_float_cmp cmp
- | Cnotcompfzero cmp => cond_for_float_not_cmp cmp
- | Ccompfs cmp => cond_for_float_cmp cmp
- | Cnotcompfs cmp => cond_for_float_not_cmp cmp
- | Ccompfszero cmp => cond_for_float_cmp cmp
- | Cnotcompfszero cmp => cond_for_float_not_cmp cmp
+Definition freg_of_preg (p : Asm.preg) : res freg :=
+ match p with
+ | DR (FR r) => OK r
+ | _ => Error (msg "Asmgen.freg_of_preg")
end.
-(** Translation of a conditional branch. Prepends to [k] the instructions
- that evaluate the condition and ranch to [lbl] if it holds.
- We recognize some conditional branches that can be implemented
- without setting then testing condition flags. *)
-
-Definition transl_cond_branch_default
- (c: condition) (args: list mreg) (lbl: label) (k: code) :=
- transl_cond c args (Pbc (cond_for_cond c) lbl :: k).
-
-Definition transl_cond_branch
- (c: condition) (args: list mreg) (lbl: label) (k: code) :=
- match args, c with
- | a1 :: nil, (Ccompimm Cne n | Ccompuimm Cne n) =>
- if Int.eq n Int.zero
- then (do r1 <- ireg_of a1; OK (Pcbnz W r1 lbl :: k))
- else transl_cond_branch_default c args lbl k
- | a1 :: nil, (Ccompimm Ceq n | Ccompuimm Ceq n) =>
- if Int.eq n Int.zero
- then (do r1 <- ireg_of a1; OK (Pcbz W r1 lbl :: k))
- else transl_cond_branch_default c args lbl k
- | a1 :: nil, (Ccomplimm Cne n | Ccompluimm Cne n) =>
- if Int64.eq n Int64.zero
- then (do r1 <- ireg_of a1; OK (Pcbnz X r1 lbl :: k))
- else transl_cond_branch_default c args lbl k
- | a1 :: nil, (Ccomplimm Ceq n | Ccompluimm Ceq n) =>
- if Int64.eq n Int64.zero
- then (do r1 <- ireg_of a1; OK (Pcbz X r1 lbl :: k))
- else transl_cond_branch_default c args lbl k
- | a1 :: nil, Cmaskzero n =>
- match Int.is_power2 n with
- | Some bit => do r1 <- ireg_of a1; OK (Ptbz W r1 bit lbl :: k)
- | None => transl_cond_branch_default c args lbl k
- end
- | a1 :: nil, Cmasknotzero n =>
- match Int.is_power2 n with
- | Some bit => do r1 <- ireg_of a1; OK (Ptbnz W r1 bit lbl :: k)
- | None => transl_cond_branch_default c args lbl k
- end
- | a1 :: nil, Cmasklzero n =>
- match Int64.is_power2' n with
- | Some bit => do r1 <- ireg_of a1; OK (Ptbz X r1 bit lbl :: k)
- | None => transl_cond_branch_default c args lbl k
- end
- | a1 :: nil, Cmasklnotzero n =>
- match Int64.is_power2' n with
- | Some bit => do r1 <- ireg_of a1; OK (Ptbnz X r1 bit lbl :: k)
- | None => transl_cond_branch_default c args lbl k
- end
- | _, _ =>
- transl_cond_branch_default c args lbl k
+Definition iregsp_of_preg (p : Asm.preg) : res iregsp :=
+ match p with
+ | DR (IR r) => OK r
+ | _ => Error (msg "Asmgen.iregsp_of_preg")
end.
-
-(** Translation of the arithmetic operation [res <- op(args)].
- The corresponding instructions are prepended to [k]. *)
-Definition transl_op
- (op: operation) (args: list mreg) (res: mreg) (k: code) :=
- match op, args with
- | Omove, a1 :: nil =>
- match preg_of res, preg_of a1 with
- | IR r, IR a => OK (Pmov r a :: k)
- | FR r, FR a => OK (Pfmov r a :: k)
- | _ , _ => Error(msg "Asmgen.Omove")
- end
- | Ointconst n, nil =>
- do rd <- ireg_of res;
- OK (loadimm32 rd n k)
- | Olongconst n, nil =>
- do rd <- ireg_of res;
- OK (loadimm64 rd n k)
- | Ofloatconst f, nil =>
- do rd <- freg_of res;
- OK (if Float.eq_dec f Float.zero
- then Pfmovi D rd XZR :: k
- else Pfmovimmd rd f :: k)
- | Osingleconst f, nil =>
- do rd <- freg_of res;
- OK (if Float32.eq_dec f Float32.zero
- then Pfmovi S rd XZR :: k
- else Pfmovimms rd f :: k)
- | Oaddrsymbol id ofs, nil =>
- do rd <- ireg_of res;
- OK (loadsymbol rd id ofs k)
- | Oaddrstack ofs, nil =>
- do rd <- ireg_of res;
- OK (addimm64 rd XSP (Ptrofs.to_int64 ofs) k)
-(** 32-bit integer arithmetic *)
- | Oshift s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porr W rd XZR r1 (transl_shift s a) :: k)
- | Oadd, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Padd W rd r1 r2 SOnone :: k)
- | Oaddshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Padd W rd r1 r2 (transl_shift s a) :: k)
- | Oaddimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (addimm32 rd r1 n k)
- | Oneg, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psub W rd XZR r1 SOnone :: k)
- | Onegshift s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psub W rd XZR r1 (transl_shift s a) :: k)
- | Osub, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psub W rd r1 r2 SOnone :: k)
- | Osubshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psub W rd r1 r2 (transl_shift s a) :: k)
- | Omul, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pmadd W rd r1 r2 XZR :: k)
- | Omuladd, a1 :: a2 :: a3 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
- OK (Pmadd W rd r2 r3 r1 :: k)
- | Omulsub, a1 :: a2 :: a3 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
- OK (Pmsub W rd r2 r3 r1 :: k)
- | Odiv, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psdiv W rd r1 r2 :: k)
- | Odivu, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pudiv W rd r1 r2 :: k)
- | Oand, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pand W rd r1 r2 SOnone :: k)
- | Oandshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pand W rd r1 r2 (transl_shift s a) :: k)
- | Oandimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm32 (Pandimm W) (Pand W) rd r1 n k)
- | Oor, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porr W rd r1 r2 SOnone :: k)
- | Oorshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porr W rd r1 r2 (transl_shift s a) :: k)
- | Oorimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm32 (Porrimm W) (Porr W) rd r1 n k)
- | Oxor, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peor W rd r1 r2 SOnone :: k)
- | Oxorshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peor W rd r1 r2 (transl_shift s a) :: k)
- | Oxorimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm32 (Peorimm W) (Peor W) rd r1 n k)
- | Onot, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porn W rd XZR r1 SOnone :: k)
- | Onotshift s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porn W rd XZR r1 (transl_shift s a) :: k)
- | Obic, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pbic W rd r1 r2 SOnone :: k)
- | Obicshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pbic W rd r1 r2 (transl_shift s a) :: k)
- | Oorn, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porn W rd r1 r2 SOnone :: k)
- | Oornshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porn W rd r1 r2 (transl_shift s a) :: k)
- | Oeqv, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peon W rd r1 r2 SOnone :: k)
- | Oeqvshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peon W rd r1 r2 (transl_shift s a) :: k)
- | Oshl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Plslv W rd r1 r2 :: k)
- | Oshr, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pasrv W rd r1 r2 :: k)
- | Oshru, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Plsrv W rd r1 r2 :: k)
- | Oshrximm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (shrx32 rd r1 n k)
- | Ozext s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfiz W rd r1 Int.zero s :: k)
- | Osext s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfiz W rd r1 Int.zero s :: k)
- | Oshlzext s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfiz W rd r1 a (Z.min s (Int.zwordsize - Int.unsigned a)) :: k)
- | Oshlsext s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfiz W rd r1 a (Z.min s (Int.zwordsize - Int.unsigned a)) :: k)
- | Ozextshr a s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfx W rd r1 a (Z.min s (Int.zwordsize - Int.unsigned a)) :: k)
- | Osextshr a s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfx W rd r1 a (Z.min s (Int.zwordsize - Int.unsigned a)) :: k)
-(** 64-bit integer arithmetic *)
- | Oshiftl s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porr X rd XZR r1 (transl_shift s a) :: k)
- | Oextend x a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (move_extended rd r1 x a k)
- (* [Omakelong] and [Ohighlong] should not occur *)
- | Olowlong, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- assertion (ireg_eq rd r1);
- OK (Pcvtx2w rd :: k)
- | Oaddl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Padd X rd r1 r2 SOnone :: k)
- | Oaddlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Padd X rd r1 r2 (transl_shift s a) :: k)
- | Oaddlext x a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (arith_extended Paddext (Padd X) rd r1 r2 x a k)
- | Oaddlimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (addimm64 rd r1 n k)
- | Onegl, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psub X rd XZR r1 SOnone :: k)
- | Oneglshift s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psub X rd XZR r1 (transl_shift s a) :: k)
- | Osubl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psub X rd r1 r2 SOnone :: k)
- | Osublshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psub X rd r1 r2 (transl_shift s a) :: k)
- | Osublext x a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (arith_extended Psubext (Psub X) rd r1 r2 x a k)
- | Omull, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pmadd X rd r1 r2 XZR :: k)
- | Omulladd, a1 :: a2 :: a3 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
- OK (Pmadd X rd r2 r3 r1 :: k)
- | Omullsub, a1 :: a2 :: a3 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2; do r3 <- ireg_of a3;
- OK (Pmsub X rd r2 r3 r1 :: k)
- | Omullhs, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psmulh rd r1 r2 :: k)
- | Omullhu, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pumulh rd r1 r2 :: k)
- | Odivl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Psdiv X rd r1 r2 :: k)
- | Odivlu, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pudiv X rd r1 r2 :: k)
- | Oandl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pand X rd r1 r2 SOnone :: k)
- | Oandlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pand X rd r1 r2 (transl_shift s a) :: k)
- | Oandlimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm64 (Pandimm X) (Pand X) rd r1 n k)
- | Oorl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porr X rd r1 r2 SOnone :: k)
- | Oorlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porr X rd r1 r2 (transl_shift s a) :: k)
- | Oorlimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm64 (Porrimm X) (Porr X) rd r1 n k)
- | Oxorl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peor X rd r1 r2 SOnone :: k)
- | Oxorlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peor X rd r1 r2 (transl_shift s a) :: k)
- | Oxorlimm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (logicalimm64 (Peorimm X) (Peor X) rd r1 n k)
- | Onotl, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porn X rd XZR r1 SOnone :: k)
- | Onotlshift s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Porn X rd XZR r1 (transl_shift s a) :: k)
- | Obicl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pbic X rd r1 r2 SOnone :: k)
- | Obiclshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pbic X rd r1 r2 (transl_shift s a) :: k)
- | Oornl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porn X rd r1 r2 SOnone :: k)
- | Oornlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Porn X rd r1 r2 (transl_shift s a) :: k)
- | Oeqvl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peon X rd r1 r2 SOnone :: k)
- | Oeqvlshift s a, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Peon X rd r1 r2 (transl_shift s a) :: k)
- | Oshll, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Plslv X rd r1 r2 :: k)
- | Oshrl, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Pasrv X rd r1 r2 :: k)
- | Oshrlu, a1 :: a2 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (Plsrv X rd r1 r2 :: k)
- | Oshrlximm n, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (shrx64 rd r1 n k)
- | Ozextl s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfiz X rd r1 Int.zero s :: k)
- | Osextl s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfiz X rd r1 Int.zero s :: k)
- | Oshllzext s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfiz X rd r1 a (Z.min s (Int64.zwordsize - Int.unsigned a)) :: k)
- | Oshllsext s a, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfiz X rd r1 a (Z.min s (Int64.zwordsize - Int.unsigned a)) :: k)
- | Ozextshrl a s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Pubfx X rd r1 a (Z.min s (Int64.zwordsize - Int.unsigned a)) :: k)
- | Osextshrl a s, a1 :: nil =>
- do rd <- ireg_of res; do r1 <- ireg_of a1;
- OK (Psbfx X rd r1 a (Z.min s (Int64.zwordsize - Int.unsigned a)) :: k)
-(** 64-bit floating-point arithmetic *)
- | Onegf, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfneg D rd rs :: k)
- | Oabsf, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfabs D rd rs :: k)
- | Oaddf, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfadd D rd rs1 rs2 :: k)
- | Osubf, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfsub D rd rs1 rs2 :: k)
- | Omulf, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfmul D rd rs1 rs2 :: k)
- | Odivf, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfdiv D rd rs1 rs2 :: k)
-(** 32-bit floating-point arithmetic *)
- | Onegfs, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfneg S rd rs :: k)
- | Oabsfs, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfabs S rd rs :: k)
- | Oaddfs, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfadd S rd rs1 rs2 :: k)
- | Osubfs, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfsub S rd rs1 rs2 :: k)
- | Omulfs, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfmul S rd rs1 rs2 :: k)
- | Odivfs, a1 :: a2 :: nil =>
- do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
- OK (Pfdiv S rd rs1 rs2 :: k)
- | Osingleoffloat, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfcvtsd rd rs :: k)
- | Ofloatofsingle, a1 :: nil =>
- do rd <- freg_of res; do rs <- freg_of a1;
- OK (Pfcvtds rd rs :: k)
-(** Conversions between int and float *)
- | Ointoffloat, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzs W D rd rs :: k)
- | Ointuoffloat, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzu W D rd rs :: k)
- | Ofloatofint, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pscvtf D W rd rs :: k)
- | Ofloatofintu, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pucvtf D W rd rs :: k)
- | Ointofsingle, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzs W S rd rs :: k)
- | Ointuofsingle, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzu W S rd rs :: k)
- | Osingleofint, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pscvtf S W rd rs :: k)
- | Osingleofintu, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pucvtf S W rd rs :: k)
- | Olongoffloat, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzs X D rd rs :: k)
- | Olonguoffloat, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzu X D rd rs :: k)
- | Ofloatoflong, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pscvtf D X rd rs :: k)
- | Ofloatoflongu, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pucvtf D X rd rs :: k)
- | Olongofsingle, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzs X S rd rs :: k)
- | Olonguofsingle, a1 :: nil =>
- do rd <- ireg_of res; do rs <- freg_of a1;
- OK (Pfcvtzu X S rd rs :: k)
- | Osingleoflong, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pscvtf S X rd rs :: k)
- | Osingleoflongu, a1 :: nil =>
- do rd <- freg_of res; do rs <- ireg_of a1;
- OK (Pucvtf S X rd rs :: k)
-(** Boolean tests *)
- | Ocmp c, _ =>
- do rd <- ireg_of res;
- transl_cond c args (Pcset rd (cond_for_cond c) :: k)
-(** Conditional move *)
- | Osel cmp ty, a1 :: a2 :: args =>
- match preg_of res with
- | IR r =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- transl_cond cmp args (Pcsel r r1 r2 (cond_for_cond cmp) :: k)
- | FR r =>
- do r1 <- freg_of a1; do r2 <- freg_of a2;
- transl_cond cmp args (Pfsel r r1 r2 (cond_for_cond cmp) :: k)
- | _ =>
- Error(msg "Asmgen.Osel")
+Definition basic_to_instruction (b: basic) : res Asm.instruction :=
+ match b with
+ (* Aithmetic instructions *)
+ | PArith (PArithP (Padrp id ofs) rd) => do rd' <- ireg_of_preg rd;
+ OK (Asm.Padrp rd' id ofs)
+ | PArith (PArithP (Pmovz sz n pos) rd) => do rd' <- ireg_of_preg rd;
+ OK (Asm.Pmovz sz rd' n pos)
+ | PArith (PArithP (Pmovn sz n pos) rd) => do rd' <- ireg_of_preg rd;
+ OK (Asm.Pmovn sz rd' n pos)
+ | PArith (PArithP (Pfmovimms f) rd) => do rd' <- freg_of_preg rd;
+ OK (Asm.Pfmovimms rd' f)
+ | PArith (PArithP (Pfmovimmd f) rd) => do rd' <- freg_of_preg rd;
+ OK (Asm.Pfmovimmd rd' f)
+
+ | PArith (PArithPP (Pmovk sz n pos) rd rs) =>
+ if (Asm.preg_eq rd rs) then (
+ do rd' <- ireg_of_preg rd;
+ OK (Asm.Pmovk sz rd' n pos)
+ ) else
+ Error (msg "Asmgen.basic_to_instruction: Pmovk uses a single register as both source and target")
+ | PArith (PArithPP Pmov rd rs) => do rd' <- iregsp_of_preg rd;
+ do rs' <- iregsp_of_preg rs;
+ OK (Asm.Pmov rd' rs')
+ | PArith (PArithPP (Paddadr id ofs) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Paddadr rd' rs' id ofs)
+ | PArith (PArithPP (Psbfiz sz r s) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Psbfiz sz rd' rs' r s)
+ | PArith (PArithPP (Psbfx sz r s) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Psbfx sz rd' rs' r s)
+ | PArith (PArithPP (Pubfiz sz r s) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Pubfiz sz rd' rs' r s)
+ | PArith (PArithPP (Pubfx sz r s) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Pubfx sz rd' rs' r s)
+ | PArith (PArithPP Pfmov rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfmov rd' rs')
+ | PArith (PArithPP Pfcvtds rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfcvtds rd' rs')
+ | PArith (PArithPP Pfcvtsd rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfcvtsd rd' rs')
+ | PArith (PArithPP (Pfabs sz) rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfabs sz rd' rs')
+ | PArith (PArithPP (Pfneg sz) rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfneg sz rd' rs')
+ | PArith (PArithPP (Pscvtf fsz isz) rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Pscvtf fsz isz rd' rs')
+ | PArith (PArithPP (Pucvtf fsz isz) rd rs) => do rd' <- freg_of_preg rd;
+ do rs' <- ireg_of_preg rs;
+ OK (Asm.Pucvtf fsz isz rd' rs')
+ | PArith (PArithPP (Pfcvtzs isz fsz) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfcvtzs isz fsz rd' rs')
+ | PArith (PArithPP (Pfcvtzu isz fsz) rd rs) => do rd' <- ireg_of_preg rd;
+ do rs' <- freg_of_preg rs;
+ OK (Asm.Pfcvtzu isz fsz rd' rs')
+ | PArith (PArithPP (Paddimm sz n) rd rs) => do rd' <- iregsp_of_preg rd;
+ do rs' <- iregsp_of_preg rs;
+ OK (Asm.Paddimm sz rd' rs' n)
+ | PArith (PArithPP (Psubimm sz n) rd rs) => do rd' <- iregsp_of_preg rd;
+ do rs' <- iregsp_of_preg rs;
+ OK (Asm.Psubimm sz rd' rs' n)
+
+ | PArith (PArithPPP (Pasrv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Pasrv sz rd' r1' r2')
+ | PArith (PArithPPP (Plslv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Plslv sz rd' r1' r2')
+ | PArith (PArithPPP (Plsrv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Plsrv sz rd' r1' r2')
+ | PArith (PArithPPP (Prorv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Prorv sz rd' r1' r2')
+ | PArith (PArithPPP Psmulh rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Psmulh rd' r1' r2')
+ | PArith (PArithPPP Pumulh rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Pumulh rd' r1' r2')
+ | PArith (PArithPPP (Psdiv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Psdiv sz rd' r1' r2')
+ | PArith (PArithPPP (Pudiv sz) rd r1 r2) => do rd' <- ireg_of_preg rd;
+ do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Pudiv sz rd' r1' r2')
+ | PArith (PArithPPP (Paddext x) rd r1 r2) => do rd' <- iregsp_of_preg rd;
+ do r1' <- iregsp_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Paddext rd' r1' r2' x)
+ | PArith (PArithPPP (Psubext x) rd r1 r2) => do rd' <- iregsp_of_preg rd;
+ do r1' <- iregsp_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Psubext rd' r1' r2' x)
+ | PArith (PArithPPP (Pfadd sz) rd r1 r2) => do rd' <- freg_of_preg rd;
+ do r1' <- freg_of_preg r1;
+ do r2' <- freg_of_preg r2;
+ OK (Asm.Pfadd sz rd' r1' r2')
+ | PArith (PArithPPP (Pfdiv sz) rd r1 r2) => do rd' <- freg_of_preg rd;
+ do r1' <- freg_of_preg r1;
+ do r2' <- freg_of_preg r2;
+ OK (Asm.Pfdiv sz rd' r1' r2')
+ | PArith (PArithPPP (Pfmul sz) rd r1 r2) => do rd' <- freg_of_preg rd;
+ do r1' <- freg_of_preg r1;
+ do r2' <- freg_of_preg r2;
+ OK (Asm.Pfmul sz rd' r1' r2')
+ | PArith (PArithPPP (Pfsub sz) rd r1 r2) => do rd' <- freg_of_preg rd;
+ do r1' <- freg_of_preg r1;
+ do r2' <- freg_of_preg r2;
+ OK (Asm.Pfsub sz rd' r1' r2')
+
+ | PArith (PArithRR0 (Pandimm sz n) rd r1) => OK (Asm.Pandimm sz rd r1 n)
+ | PArith (PArithRR0 (Peorimm sz n) rd r1) => OK (Asm.Peorimm sz rd r1 n)
+ | PArith (PArithRR0 (Porrimm sz n) rd r1) => OK (Asm.Porrimm sz rd r1 n)
+
+
+ | PArith (PArithRR0R (Padd sz s) rd r1 r2) => OK (Asm.Padd sz rd r1 r2 s)
+ | PArith (PArithRR0R (Psub sz s) rd r1 r2) => OK (Asm.Psub sz rd r1 r2 s)
+ | PArith (PArithRR0R (Pand sz s) rd r1 r2) => OK (Asm.Pand sz rd r1 r2 s)
+ | PArith (PArithRR0R (Pbic sz s) rd r1 r2) => OK (Asm.Pbic sz rd r1 r2 s)
+ | PArith (PArithRR0R (Peon sz s) rd r1 r2) => OK (Asm.Peon sz rd r1 r2 s)
+ | PArith (PArithRR0R (Peor sz s) rd r1 r2) => OK (Asm.Peor sz rd r1 r2 s)
+ | PArith (PArithRR0R (Porr sz s) rd r1 r2) => OK (Asm.Porr sz rd r1 r2 s)
+ | PArith (PArithRR0R (Porn sz s) rd r1 r2) => OK (Asm.Porn sz rd r1 r2 s)
+
+ | PArith (PArithARRRR0 (Pmadd sz) rd r1 r2 r3) => OK (Asm.Pmadd sz rd r1 r2 r3)
+ | PArith (PArithARRRR0 (Pmsub sz) rd r1 r2 r3) => OK (Asm.Pmsub sz rd r1 r2 r3)
+
+ | PArith (PArithComparisonPP (Pcmpext x) r1 r2) => do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Pcmpext r1' r2' x)
+ | PArith (PArithComparisonPP (Pcmnext x) r1 r2) => do r1' <- ireg_of_preg r1;
+ do r2' <- ireg_of_preg r2;
+ OK (Asm.Pcmnext r1' r2' x)
+ | PArith (PArithComparisonPP (Pfcmp sz) r1 r2) => do r1' <- freg_of_preg r1;
+ do r2' <- freg_of_preg r2;
+ OK (Asm.Pfcmp sz r1' r2')
+
+ | PArith (PArithComparisonR0R (Pcmp is s) r1 r2) => OK (Asm.Pcmp is r1 r2 s)
+ | PArith (PArithComparisonR0R (Pcmn is s) r1 r2) => OK (Asm.Pcmn is r1 r2 s)
+ | PArith (PArithComparisonR0R (Ptst is s) r1 r2) => OK (Asm.Ptst is r1 r2 s)
+
+ | PArith (PArithComparisonP (Pcmpimm sz n) r1) => do r1' <- ireg_of_preg r1;
+ OK (Asm.Pcmpimm sz r1' n)
+ | PArith (PArithComparisonP (Pcmnimm sz n) r1) => do r1' <- ireg_of_preg r1;
+ OK (Asm.Pcmnimm sz r1' n)
+ | PArith (PArithComparisonP (Ptstimm sz n) r1) => do r1' <- ireg_of_preg r1;
+ OK (Asm.Ptstimm sz r1' n)
+ | PArith (PArithComparisonP (Pfcmp0 sz) r1) => do r1' <- freg_of_preg r1;
+ OK (Asm.Pfcmp0 sz r1')
+
+ | PArith (Pcset rd c) => OK (Asm.Pcset rd c)
+ | PArith (Pfmovi fsz rd r1) => OK (Asm.Pfmovi fsz rd r1)
+ | PArith (Pcsel rd r1 r2 c) =>
+ match r1, r2 with
+ | IR r1', IR r2' => do rd' <- ireg_of_preg rd;
+ do r1'' <- ireg_of_preg r1';
+ do r2'' <- ireg_of_preg r2';
+ OK (Asm.Pcsel rd' r1'' r2'' c)
+ | FR r1', FR r2' => do rd' <- freg_of_preg rd;
+ do r1'' <- freg_of_preg r1';
+ do r2'' <- freg_of_preg r2';
+ OK (Asm.Pfsel rd' r1'' r2'' c)
+ | _, _ => Error (msg "Asmgen.basic_to_instruction: Pcsel is only defind on iregs and fregs.")
end
- | _, _ =>
- Error(msg "Asmgen.transl_op")
+ | PArith (Pfnmul fsz rd r1 r2) => OK (Asm.Pfnmul fsz rd r1 r2)
+
+ | PLoad (PLd_rd_a Pldrw rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrw rd' a)
+ | PLoad (PLd_rd_a Pldrw_a rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrw_a rd' a)
+ | PLoad (PLd_rd_a Pldrx rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrx rd' a)
+ | PLoad (PLd_rd_a Pldrx_a rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrx_a rd' a)
+ | PLoad (PLd_rd_a (Pldrb sz) rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrb sz rd' a)
+ | PLoad (PLd_rd_a (Pldrsb sz) rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrsb sz rd' a)
+ | PLoad (PLd_rd_a (Pldrh sz) rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrh sz rd' a)
+ | PLoad (PLd_rd_a (Pldrsh sz) rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrsh sz rd' a)
+ | PLoad (PLd_rd_a Pldrzw rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrzw rd' a)
+ | PLoad (PLd_rd_a Pldrsw rd a) => do rd' <- ireg_of_preg rd; OK (Asm.Pldrsw rd' a)
+
+ | PLoad (PLd_rd_a Pldrs rd a) => do rd' <- freg_of_preg rd; OK (Asm.Pldrs rd' a)
+ | PLoad (PLd_rd_a Pldrd rd a) => do rd' <- freg_of_preg rd; OK (Asm.Pldrd rd' a)
+ | PLoad (PLd_rd_a Pldrd_a rd a) => do rd' <- freg_of_preg rd; OK (Asm.Pldrd_a rd' a)
+
+ | PLoad (Pldp Pldpw rd1 rd2 chk1 chk2 a) => do rd1' <- ireg_of_preg rd1;
+ do rd2' <- ireg_of_preg rd2;
+ OK (Asm.Pldpw rd1' rd2' chk1 chk2 a)
+ | PLoad (Pldp Pldpx rd1 rd2 chk1 chk2 a) => do rd1' <- ireg_of_preg rd1;
+ do rd2' <- ireg_of_preg rd2;
+ OK (Asm.Pldpx rd1' rd2' chk1 chk2 a)
+ | PLoad (Pldp Pldps rd1 rd2 chk1 chk2 a) => do rd1' <- freg_of_preg rd1;
+ do rd2' <- freg_of_preg rd2;
+ OK (Asm.Pldps rd1' rd2' chk1 chk2 a)
+ | PLoad (Pldp Pldpd rd1 rd2 chk1 chk2 a) => do rd1' <- freg_of_preg rd1;
+ do rd2' <- freg_of_preg rd2;
+ OK (Asm.Pldpd rd1' rd2' chk1 chk2 a)
+
+ | PStore (PSt_rs_a Pstrw r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrw r' a)
+ | PStore (PSt_rs_a Pstrw_a r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrw_a r' a)
+ | PStore (PSt_rs_a Pstrx r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrx r' a)
+ | PStore (PSt_rs_a Pstrx_a r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrx_a r' a)
+ | PStore (PSt_rs_a Pstrb r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrb r' a)
+ | PStore (PSt_rs_a Pstrh r a) => do r' <- ireg_of_preg r; OK (Asm.Pstrh r' a)
+
+ | PStore (PSt_rs_a Pstrs r a) => do r' <- freg_of_preg r; OK (Asm.Pstrs r' a)
+ | PStore (PSt_rs_a Pstrd r a) => do r' <- freg_of_preg r; OK (Asm.Pstrd r' a)
+ | PStore (PSt_rs_a Pstrd_a r a) => do r' <- freg_of_preg r; OK (Asm.Pstrd_a r' a)
+
+ | PStore (Pstp Pstpw rs1 rs2 chk1 chk2 a) => do rs1' <- ireg_of_preg rs1;
+ do rs2' <- ireg_of_preg rs2;
+ OK (Asm.Pstpw rs1' rs2' chk1 chk2 a)
+ | PStore (Pstp Pstpx rs1 rs2 chk1 chk2 a) => do rs1' <- ireg_of_preg rs1;
+ do rs2' <- ireg_of_preg rs2;
+ OK (Asm.Pstpx rs1' rs2' chk1 chk2 a)
+ | PStore (Pstp Pstps rs1 rs2 chk1 chk2 a) => do rs1' <- freg_of_preg rs1;
+ do rs2' <- freg_of_preg rs2;
+ OK (Asm.Pstps rs1' rs2' chk1 chk2 a)
+ | PStore (Pstp Pstpd rs1 rs2 chk1 chk2 a) => do rs1' <- freg_of_preg rs1;
+ do rs2' <- freg_of_preg rs2;
+ OK (Asm.Pstpd rs1' rs2' chk1 chk2 a)
+
+ | Pallocframe sz linkofs => OK (Asm.Pallocframe sz linkofs)
+ | Pfreeframe sz linkofs => OK (Asm.Pfreeframe sz linkofs)
+
+ | Ploadsymbol rd id => OK (Asm.Ploadsymbol rd id)
+
+ | Pcvtsw2x rd r1 => OK (Asm.Pcvtsw2x rd r1)
+
+ | Pcvtuw2x rd r1 => OK (Asm.Pcvtuw2x rd r1)
+
+ | Pcvtx2w rd => OK (Asm.Pcvtx2w rd)
+ | Pnop => OK (Asm.Pnop)
end.
-(** Translation of addressing modes *)
-
-Definition offset_representable (sz: Z) (ofs: int64) : bool :=
- let isz := Int64.repr sz in
- (** either unscaled 9-bit signed *)
- Int64.eq ofs (Int64.sign_ext 9 ofs) ||
- (** or scaled 12-bit unsigned *)
- (Int64.eq (Int64.modu ofs isz) Int64.zero
- && Int64.ltu ofs (Int64.shl isz (Int64.repr 12))).
-
-Definition transl_addressing (sz: Z) (addr: Op.addressing) (args: list mreg)
- (insn: Asm.addressing -> instruction) (k: code) : res code :=
- match addr, args with
- | Aindexed ofs, a1 :: nil =>
- do r1 <- ireg_of a1;
- if offset_representable sz ofs then
- OK (insn (ADimm r1 ofs) :: k)
- else
- OK (loadimm64 X16 ofs (insn (ADreg r1 X16) :: k))
- | Aindexed2, a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (insn (ADreg r1 r2) :: k)
- | Aindexed2shift a, a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- if Int.eq a Int.zero then
- OK (insn (ADreg r1 r2) :: k)
- else if Int.eq (Int.shl Int.one a) (Int.repr sz) then
- OK (insn (ADlsl r1 r2 a) :: k)
- else
- OK (Padd X X16 r1 r2 (SOlsl a) :: insn (ADimm X16 Int64.zero) :: k)
- | Aindexed2ext x a, a1 :: a2 :: nil =>
- do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- if Int.eq a Int.zero || Int.eq (Int.shl Int.one a) (Int.repr sz) then
- OK (insn (match x with Xsgn32 => ADsxt r1 r2 a
- | Xuns32 => ADuxt r1 r2 a end) :: k)
- else
- OK (arith_extended Paddext (Padd X) X16 r1 r2 x a
- (insn (ADimm X16 Int64.zero) :: k))
- | Aglobal id ofs, nil =>
- assertion (negb (SelectOp.symbol_is_relocatable id));
- if Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero && symbol_is_aligned id sz
- then OK (Padrp X16 id ofs :: insn (ADadr X16 id ofs) :: k)
- else OK (loadsymbol X16 id ofs (insn (ADimm X16 Int64.zero) :: k))
- | Ainstack ofs, nil =>
- let ofs := Ptrofs.to_int64 ofs in
- if offset_representable sz ofs then
- OK (insn (ADimm XSP ofs) :: k)
- else
- OK (loadimm64 X16 ofs (insn (ADreg XSP X16) :: k))
- | _, _ =>
- Error(msg "Asmgen.transl_addressing")
+Definition cf_instruction_to_instruction (cfi: cf_instruction) : Asm.instruction :=
+ match cfi with
+ | Pb l => Asm.Pb l
+ | Pbc c lbl => Asm.Pbc c lbl
+ | Pbl id sg => Asm.Pbl id sg
+ | Pbs id sg => Asm.Pbs id sg
+ | Pblr r sg => Asm.Pblr r sg
+ | Pbr r sg => Asm.Pbr r sg
+ | Pret r => Asm.Pret r
+ | Pcbnz sz r lbl => Asm.Pcbnz sz r lbl
+ | Pcbz sz r lbl => Asm.Pcbz sz r lbl
+ | Ptbnz sz r n lbl => Asm.Ptbnz sz r n lbl
+ | Ptbz sz r n lbl => Asm.Ptbz sz r n lbl
+ | Pbtbl r1 tbl => Asm.Pbtbl r1 tbl
end.
-(** Translation of loads and stores *)
-
-Definition transl_load (chunk: memory_chunk) (addr: Op.addressing)
- (args: list mreg) (dst: mreg) (k: code) : res code :=
- match chunk with
- | Mint8unsigned =>
- do rd <- ireg_of dst; transl_addressing 1 addr args (Pldrb W rd) k
- | Mint8signed =>
- do rd <- ireg_of dst; transl_addressing 1 addr args (Pldrsb W rd) k
- | Mint16unsigned =>
- do rd <- ireg_of dst; transl_addressing 2 addr args (Pldrh W rd) k
- | Mint16signed =>
- do rd <- ireg_of dst; transl_addressing 2 addr args (Pldrsh W rd) k
- | Mint32 =>
- do rd <- ireg_of dst; transl_addressing 4 addr args (Pldrw rd) k
- | Mint64 =>
- do rd <- ireg_of dst; transl_addressing 8 addr args (Pldrx rd) k
- | Mfloat32 =>
- do rd <- freg_of dst; transl_addressing 4 addr args (Pldrs rd) k
- | Mfloat64 =>
- do rd <- freg_of dst; transl_addressing 8 addr args (Pldrd rd) k
- | Many32 =>
- do rd <- ireg_of dst; transl_addressing 4 addr args (Pldrw_a rd) k
- | Many64 =>
- do rd <- ireg_of dst; transl_addressing 8 addr args (Pldrx_a rd) k
+Definition control_to_instruction (c: control) :=
+ match c with
+ | PCtlFlow i => cf_instruction_to_instruction i
+ | Pbuiltin ef args res => Asm.Pbuiltin ef (List.map (map_builtin_arg DR) args) (map_builtin_res DR res)
end.
-Definition transl_store (chunk: memory_chunk) (addr: Op.addressing)
- (args: list mreg) (src: mreg) (k: code) : res code :=
- match chunk with
- | Mint8unsigned | Mint8signed =>
- do r1 <- ireg_of src; transl_addressing 1 addr args (Pstrb r1) k
- | Mint16unsigned | Mint16signed =>
- do r1 <- ireg_of src; transl_addressing 2 addr args (Pstrh r1) k
- | Mint32 =>
- do r1 <- ireg_of src; transl_addressing 4 addr args (Pstrw r1) k
- | Mint64 =>
- do r1 <- ireg_of src; transl_addressing 8 addr args (Pstrx r1) k
- | Mfloat32 =>
- do r1 <- freg_of src; transl_addressing 4 addr args (Pstrs r1) k
- | Mfloat64 =>
- do r1 <- freg_of src; transl_addressing 8 addr args (Pstrd r1) k
- | Many32 =>
- do r1 <- ireg_of src; transl_addressing 4 addr args (Pstrw_a r1) k
- | Many64 =>
- do r1 <- ireg_of src; transl_addressing 8 addr args (Pstrx_a r1) k
+Fixpoint unfold_label (ll: list label) :=
+ match ll with
+ | nil => nil
+ | l :: ll => Plabel l :: unfold_label ll
end.
-(** Register-indexed loads and stores *)
-
-Definition indexed_memory_access (insn: Asm.addressing -> instruction)
- (sz: Z) (base: iregsp) (ofs: ptrofs) (k: code) :=
- let ofs := Ptrofs.to_int64 ofs in
- if offset_representable sz ofs
- then insn (ADimm base ofs) :: k
- else loadimm64 X16 ofs (insn (ADreg base X16) :: k).
-
-Definition loadind (base: iregsp) (ofs: ptrofs) (ty: typ) (dst: mreg) (k: code) :=
- match ty, preg_of dst with
- | Tint, IR rd => OK (indexed_memory_access (Pldrw rd) 4 base ofs k)
- | Tlong, IR rd => OK (indexed_memory_access (Pldrx rd) 8 base ofs k)
- | Tsingle, FR rd => OK (indexed_memory_access (Pldrs rd) 4 base ofs k)
- | Tfloat, FR rd => OK (indexed_memory_access (Pldrd rd) 8 base ofs k)
- | Tany32, IR rd => OK (indexed_memory_access (Pldrw_a rd) 4 base ofs k)
- | Tany64, IR rd => OK (indexed_memory_access (Pldrx_a rd) 8 base ofs k)
- | Tany64, FR rd => OK (indexed_memory_access (Pldrd_a rd) 8 base ofs k)
- | _, _ => Error (msg "Asmgen.loadind")
- end.
-
-Definition storeind (src: mreg) (base: iregsp) (ofs: ptrofs) (ty: typ) (k: code) :=
- match ty, preg_of src with
- | Tint, IR rd => OK (indexed_memory_access (Pstrw rd) 4 base ofs k)
- | Tlong, IR rd => OK (indexed_memory_access (Pstrx rd) 8 base ofs k)
- | Tsingle, FR rd => OK (indexed_memory_access (Pstrs rd) 4 base ofs k)
- | Tfloat, FR rd => OK (indexed_memory_access (Pstrd rd) 8 base ofs k)
- | Tany32, IR rd => OK (indexed_memory_access (Pstrw_a rd) 4 base ofs k)
- | Tany64, IR rd => OK (indexed_memory_access (Pstrx_a rd) 8 base ofs k)
- | Tany64, FR rd => OK (indexed_memory_access (Pstrd_a rd) 8 base ofs k)
- | _, _ => Error (msg "Asmgen.storeind")
- end.
-
-Definition loadptr (base: iregsp) (ofs: ptrofs) (dst: ireg) (k: code) :=
- indexed_memory_access (Pldrx dst) 8 base ofs k.
-
-Definition storeptr (src: ireg) (base: iregsp) (ofs: ptrofs) (k: code) :=
- indexed_memory_access (Pstrx src) 8 base ofs k.
-
-(** Function epilogue *)
-
-Definition make_epilogue (f: Mach.function) (k: code) :=
- loadptr XSP f.(fn_retaddr_ofs) RA
- (Pfreeframe f.(fn_stacksize) f.(fn_link_ofs) :: k).
-
-(** Translation of a Mach instruction. *)
-
-Definition transl_instr (f: Mach.function) (i: Mach.instruction)
- (r29_is_parent: bool) (k: code) : res code :=
- match i with
- | Mgetstack ofs ty dst =>
- loadind XSP ofs ty dst k
- | Msetstack src ofs ty =>
- storeind src XSP ofs ty k
- | Mgetparam ofs ty dst =>
- (* load via the frame pointer if it is valid *)
- do c <- loadind X29 ofs ty dst k;
- OK (if r29_is_parent then c else loadptr XSP f.(fn_link_ofs) X29 c)
- | Mop op args res =>
- transl_op op args res k
- | Mload chunk addr args dst =>
- transl_load chunk addr args dst k
- | Mstore chunk addr args src =>
- transl_store chunk addr args src k
- | Mcall sig (inl r) =>
- do r1 <- ireg_of r; OK (Pblr r1 sig :: k)
- | Mcall sig (inr symb) =>
- OK (Pbl symb sig :: k)
- | Mtailcall sig (inl r) =>
- do r1 <- ireg_of r;
- OK (make_epilogue f (Pbr r1 sig :: k))
- | Mtailcall sig (inr symb) =>
- OK (make_epilogue f (Pbs symb sig :: k))
- | Mbuiltin ef args res =>
- OK (Pbuiltin ef (List.map (map_builtin_arg preg_of) args) (map_builtin_res preg_of res) :: k)
- | Mlabel lbl =>
- OK (Plabel lbl :: k)
- | Mgoto lbl =>
- OK (Pb lbl :: k)
- | Mcond cond args lbl =>
- transl_cond_branch cond args lbl k
- | Mjumptable arg tbl =>
- do r <- ireg_of arg;
- OK (Pbtbl r tbl :: k)
- | Mreturn =>
- OK (make_epilogue f (Pret RA :: k))
- end.
-
-(** Translation of a code sequence *)
-
-Definition it1_is_parent (before: bool) (i: Mach.instruction) : bool :=
- match i with
- | Msetstack src ofs ty => before
- | Mgetparam ofs ty dst => negb (mreg_eq dst R29)
- | Mop op args res => before && negb (mreg_eq res R29)
- | _ => false
- end.
-
-(** This is the naive definition that we no longer use because it
- is not tail-recursive. It is kept as specification. *)
-
-Fixpoint transl_code (f: Mach.function) (il: list Mach.instruction) (it1p: bool) :=
- match il with
+Fixpoint unfold_body (lb: list basic) : res Asm.code :=
+ match lb with
| nil => OK nil
- | i1 :: il' =>
- do k <- transl_code f il' (it1_is_parent it1p i1);
- transl_instr f i1 it1p k
+ | b :: lb =>
+ (* x_is: x's instructions *)
+ do b_is <- basic_to_instruction b;
+ do lb_is <- unfold_body lb;
+ OK (b_is :: lb_is)
end.
-(** This is an equivalent definition in continuation-passing style
- that runs in constant stack space. *)
-
-Fixpoint transl_code_rec (f: Mach.function) (il: list Mach.instruction)
- (it1p: bool) (k: code -> res code) :=
- match il with
- | nil => k nil
- | i1 :: il' =>
- transl_code_rec f il' (it1_is_parent it1p i1)
- (fun c1 => do c2 <- transl_instr f i1 it1p c1; k c2)
+Definition unfold_exit (oc: option control) :=
+ match oc with
+ | None => nil
+ | Some c => control_to_instruction c :: nil
end.
-Definition transl_code' (f: Mach.function) (il: list Mach.instruction) (it1p: bool) :=
- transl_code_rec f il it1p (fun c => OK c).
+Definition unfold_bblock (bb: bblock) :=
+ let lbl := unfold_label (header bb) in
+ (*
+ * With this dynamically checked assumption on a previous optimization we
+ * can show that [Asmblock.label_pos] and [Asm.label_pos] retrieve the same
+ * exact address. Maintaining this property allows us to use the simple
+ * formulation of match_states defined as equality.
+ * Otherwise we would have to deal with the case of a basic block header
+ * that has multiple labels. Asmblock.label_pos will, for all labels, point
+ * to the same location at the beginning of the basic block. Asm.label_pos
+ * on the other hand could return a position pointing into the original
+ * basic block.
+ *)
+ if zle (list_length_z (header bb)) 1 then
+ do bo_is <- unfold_body (body bb);
+ OK (lbl ++ bo_is ++ unfold_exit (exit bb))
+ else
+ Error (msg "Asmgen.unfold_bblock: Multiple labels were generated.").
+
+Fixpoint unfold (bbs: Asmblock.bblocks) : res Asm.code :=
+ match bbs with
+ | nil => OK (nil)
+ | bb :: bbs' =>
+ do bb_is <- unfold_bblock bb;
+ do bbs'_is <- unfold bbs';
+ OK (bb_is ++ bbs'_is)
+ end.
-(** Translation of a whole function. Note that we must check
- that the generated code contains less than [2^32] instructions,
- otherwise the offset part of the [PC] code pointer could wrap
- around, leading to incorrect executions. *)
+Definition transf_function (f: Asmblock.function) : res Asm.function :=
+ do c <- unfold (Asmblock.fn_blocks f);
+ if zlt Ptrofs.max_unsigned (list_length_z c)
+ then Error (msg "Asmgen.trans_function: code size exceeded")
+ else OK {| Asm.fn_sig := Asmblock.fn_sig f; Asm.fn_code := c |}.
-Definition transl_function (f: Mach.function) :=
- do c <- transl_code' f f.(Mach.fn_code) true;
- OK (mkfunction f.(Mach.fn_sig)
- (Pallocframe f.(fn_stacksize) f.(fn_link_ofs) ::
- storeptr RA XSP f.(fn_retaddr_ofs) c)).
+Definition transf_fundef (f: Asmblock.fundef) : res Asm.fundef :=
+ transf_partial_fundef transf_function f.
-Definition transf_function (f: Mach.function) : res Asm.function :=
- do tf <- transl_function f;
- if zlt Ptrofs.max_unsigned (list_length_z tf.(fn_code))
- then Error (msg "code size exceeded")
- else OK tf.
+Definition transf_program (p: Asmblock.program) : res Asm.program :=
+ transform_partial_program transf_fundef p.
-Definition transf_fundef (f: Mach.fundef) : res Asm.fundef :=
- transf_partial_fundef transf_function f.
+End Asmblock_TRANSF.
Definition transf_program (p: Mach.program) : res Asm.program :=
- transform_partial_program transf_fundef p.
+ let mbp := Machblockgen.transf_program p in
+ do abp <- Asmblockgen.transf_program mbp;
+ do abp' <- (time "PostpassScheduling total oracle+verification" PostpassScheduling.transf_program) abp;
+ Asmblock_TRANSF.transf_program abp'.
diff --git a/aarch64/Asmgenproof.v b/aarch64/Asmgenproof.v
index dc0bc509..d27b3f8c 100644
--- a/aarch64/Asmgenproof.v
+++ b/aarch64/Asmgenproof.v
@@ -1,24 +1,35 @@
-(* *********************************************************************)
-(* *)
-(* The Compcert verified compiler *)
-(* *)
-(* Xavier Leroy, Collège de France and INRIA Paris *)
-(* *)
-(* Copyright Institut National de Recherche en Informatique et en *)
-(* Automatique. All rights reserved. This file is distributed *)
-(* under the terms of the INRIA Non-Commercial License Agreement. *)
-(* *)
-(* *********************************************************************)
-
-(** Correctness proof for AArch64 code generation. *)
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* Justus Fasse UGA, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
Require Import Coqlib Errors.
Require Import Integers Floats AST Linking.
Require Import Values Memory Events Globalenvs Smallstep.
-Require Import Op Locations Mach Conventions Asm.
-Require Import Asmgen Asmgenproof0 Asmgenproof1.
+Require Import Op Locations Machblock Conventions Asm Asmblock.
+Require Machblockgenproof Asmblockgenproof PostpassSchedulingproof.
+Require Import Asmgen.
+Require Import Axioms.
+Require Import IterList.
+Require Import Ring Lia.
-Definition match_prog (p: Mach.program) (tp: Asm.program) :=
+Module Asmblock_PRESERVATION.
+
+Import Asmblock_TRANSF.
+
+Definition match_prog (p: Asmblock.program) (tp: Asm.program) :=
match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
Lemma transf_program_match:
@@ -29,20 +40,36 @@ Qed.
Section PRESERVATION.
-Variable prog: Mach.program.
+Variable prog: Asmblock.program.
Variable tprog: Asm.program.
Hypothesis TRANSF: match_prog prog tprog.
Let ge := Genv.globalenv prog.
Let tge := Genv.globalenv tprog.
+Definition lk :aarch64_linker := {| Asmblock.symbol_low:=Asm.symbol_low tge; Asmblock.symbol_high:=Asm.symbol_high tge|}.
+
Lemma symbols_preserved:
forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
Proof (Genv.find_symbol_match TRANSF).
+Lemma symbol_addresses_preserved:
+ forall (s: ident) (ofs: ptrofs),
+ Genv.symbol_address tge s ofs = Genv.symbol_address ge s ofs.
+Proof.
+ intros; unfold Genv.symbol_address; rewrite symbols_preserved; reflexivity.
+Qed.
+
Lemma senv_preserved:
Senv.equiv ge tge.
Proof (Genv.senv_match TRANSF).
+Lemma symbol_high_low: forall (id: ident) (ofs: ptrofs),
+ Val.addl (Asmblock.symbol_high lk id ofs) (Asmblock.symbol_low lk id ofs) = Genv.symbol_address ge id ofs.
+Proof.
+ unfold lk; simpl. intros; rewrite Asm.symbol_high_low; unfold Genv.symbol_address;
+ rewrite symbols_preserved; reflexivity.
+Qed.
+
Lemma functions_translated:
forall b f,
Genv.find_funct_ptr ge b = Some f ->
@@ -50,980 +77,2242 @@ Lemma functions_translated:
Genv.find_funct_ptr tge b = Some tf /\ transf_fundef f = OK tf.
Proof (Genv.find_funct_ptr_transf_partial TRANSF).
-Lemma functions_transl:
- forall fb f tf,
+Lemma internal_functions_translated:
+ forall b f,
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ exists tf,
+ Genv.find_funct_ptr tge b = Some (Internal tf) /\ transf_function f = OK tf.
+Proof.
+ intros; exploit functions_translated; eauto.
+ intros (x & FIND & TRANSf).
+ apply bind_inversion in TRANSf.
+ destruct TRANSf as (tf & TRANSf & X).
+ inv X.
+ eauto.
+Qed.
+
+Lemma internal_functions_unfold:
+ forall b f,
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ exists tc,
+ Genv.find_funct_ptr tge b = Some (Internal (Asm.mkfunction (fn_sig f) tc))
+ /\ unfold (fn_blocks f) = OK tc
+ /\ list_length_z tc <= Ptrofs.max_unsigned.
+Proof.
+ intros.
+ exploit internal_functions_translated; eauto.
+ intros (tf & FINDtf & TRANStf).
+ unfold transf_function in TRANStf.
+ monadInv TRANStf.
+ destruct (zlt _ _); try congruence.
+ inv EQ. inv EQ0.
+ eexists; intuition eauto.
+ lia.
+Qed.
+
+
+Inductive is_nth_inst (bb: bblock) (n:Z) (i:Asm.instruction): Prop :=
+ | is_nth_label l:
+ list_nth_z (header bb) n = Some l ->
+ i = Asm.Plabel l ->
+ is_nth_inst bb n i
+ | is_nth_basic bi:
+ list_nth_z (body bb) (n - list_length_z (header bb)) = Some bi ->
+ basic_to_instruction bi = OK i ->
+ is_nth_inst bb n i
+ | is_nth_ctlflow cfi:
+ (exit bb) = Some cfi ->
+ n = size bb - 1 ->
+ i = control_to_instruction cfi ->
+ is_nth_inst bb n i.
+
+(* Asmblock and Asm share the same definition of state *)
+Definition match_states (s1 s2 : state) := s1 = s2.
+
+Inductive match_internal: forall n, state -> state -> Prop :=
+ | match_internal_intro n rs1 m1 rs2 m2
+ (MEM: m1 = m2)
+ (AG: forall r, r <> PC -> rs1 r = rs2 r)
+ (AGPC: Val.offset_ptr (rs1 PC) (Ptrofs.repr n) = rs2 PC)
+ : match_internal n (State rs1 m1) (State rs2 m2).
+
+Lemma match_internal_set_parallel:
+ forall n rs1 m1 rs2 m2 r val,
+ match_internal n (State rs1 m1) (State rs2 m2) ->
+ r <> PC ->
+ match_internal n (State (rs1#r <- val) m1) (State (rs2#r <- val ) m2).
+Proof.
+ intros n rs1 m1 rs2 m2 r v MI.
+ inversion MI; constructor; auto.
+ - intros r' NOTPC.
+ unfold Pregmap.set; rewrite AG. reflexivity. assumption.
+ - unfold Pregmap.set; destruct (PregEq.eq PC r); congruence.
+Qed.
+
+Lemma agree_match_states:
+ forall rs1 m1 rs2 m2,
+ match_states (State rs1 m1) (State rs2 m2) ->
+ forall r : preg, rs1#r = rs2#r.
+Proof.
+ intros.
+ unfold match_states in *.
+ assert (rs1 = rs2) as EQ. { congruence. }
+ rewrite EQ. reflexivity.
+Qed.
+
+Lemma match_states_set_parallel:
+ forall rs1 m1 rs2 m2 r v,
+ match_states (State rs1 m1) (State rs2 m2) ->
+ match_states (State (rs1#r <- v) m1) (State (rs2#r <- v) m2).
+Proof.
+ intros; unfold match_states in *.
+ assert (rs1 = rs2) as RSEQ. { congruence. }
+ assert (m1 = m2) as MEQ. { congruence. }
+ rewrite RSEQ in *; rewrite MEQ in *; unfold Pregmap.set; reflexivity.
+Qed.
+
+(* match_internal from match_states *)
+Lemma mi_from_ms:
+ forall rs1 m1 rs2 m2 b ofs,
+ match_states (State rs1 m1) (State rs2 m2) ->
+ rs1#PC = Vptr b ofs ->
+ match_internal 0 (State rs1 m1) (State rs2 m2).
+Proof.
+ intros rs1 m1 rs2 m2 b ofs MS PCVAL.
+ inv MS; constructor; auto; unfold Val.offset_ptr;
+ rewrite PCVAL; rewrite Ptrofs.add_zero; reflexivity.
+Qed.
+
+Lemma transf_initial_states:
+ forall s1, Asmblock.initial_state prog s1 ->
+ exists s2, Asm.initial_state tprog s2 /\ match_states s1 s2.
+Proof.
+ intros ? INIT_s1.
+ inversion INIT_s1 as (m, ?, ge0, rs). unfold ge0 in *.
+ econstructor; split.
+ - econstructor.
+ eapply (Genv.init_mem_transf_partial TRANSF); eauto.
+ - rewrite (match_program_main TRANSF); rewrite symbol_addresses_preserved.
+ unfold rs; reflexivity.
+Qed.
+
+Lemma transf_final_states:
+ forall s1 s2 r,
+ match_states s1 s2 -> Asmblock.final_state s1 r -> Asm.final_state s2 r.
+Proof.
+ intros s1 s2 r MATCH FINAL_s1.
+ inv FINAL_s1; inv MATCH; constructor; assumption.
+Qed.
+
+Definition max_pos (f : Asm.function) := list_length_z f.(Asm.fn_code).
+
+Lemma functions_bound_max_pos: forall fb f tf,
Genv.find_funct_ptr ge fb = Some (Internal f) ->
transf_function f = OK tf ->
- Genv.find_funct_ptr tge fb = Some (Internal tf).
+ max_pos tf <= Ptrofs.max_unsigned.
+Proof.
+ intros fb f tf FINDf TRANSf.
+ unfold transf_function in TRANSf.
+ apply bind_inversion in TRANSf.
+ destruct TRANSf as (c & TRANSf).
+ destruct TRANSf as (_ & TRANSf).
+ destruct (zlt _ _).
+ - inversion TRANSf.
+ - unfold max_pos.
+ assert (Asm.fn_code tf = c) as H. { inversion TRANSf as (H'); auto. }
+ rewrite H; lia.
+Qed.
+
+Lemma one_le_max_unsigned:
+ 1 <= Ptrofs.max_unsigned.
+Proof.
+ unfold Ptrofs.max_unsigned; simpl; unfold Ptrofs.wordsize;
+ unfold Wordsize_Ptrofs.wordsize; destruct Archi.ptr64; simpl; lia.
+Qed.
+
+(* NB: does not seem useful anymore, with the [exec_header_simulation] proof below
+Lemma match_internal_exec_label:
+ forall n rs1 m1 rs2 m2 l fb f tf,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transf_function f = OK tf ->
+ match_internal n (State rs1 m1) (State rs2 m2) ->
+ n >= 0 ->
+ (* There is no step if n is already max_pos *)
+ n < (max_pos tf) ->
+ exists rs2' m2', Asm.exec_instr tge tf (Asm.Plabel l) rs2 m2 = Next rs2' m2'
+ /\ match_internal (n+1) (State rs1 m1) (State rs2' m2').
+Proof.
+ intros. (* XXX auto generated names *)
+ unfold Asm.exec_instr.
+ eexists; eexists; split; eauto.
+ inversion H1; constructor; auto.
+ - intros; unfold Asm.nextinstr; unfold Pregmap.set;
+ destruct (PregEq.eq r PC); auto; contradiction.
+ - unfold Asm.nextinstr; rewrite Pregmap.gss; unfold Ptrofs.one.
+ rewrite <- AGPC; rewrite Val.offset_ptr_assoc; unfold Ptrofs.add;
+ rewrite Ptrofs.unsigned_repr. rewrite Ptrofs.unsigned_repr; trivial.
+ + split.
+ * apply Z.le_0_1.
+ * apply one_le_max_unsigned.
+ + split.
+ * apply Z.ge_le; assumption.
+ * rewrite <- functions_bound_max_pos; eauto; lia.
+Qed.
+*)
+
+Lemma incrPC_agree_but_pc:
+ forall rs r ofs,
+ r <> PC ->
+ (incrPC ofs rs)#r = rs#r.
+Proof.
+ intros rs r ofs NOTPC.
+ unfold incrPC; unfold Pregmap.set; destruct (PregEq.eq r PC).
+ - contradiction.
+ - reflexivity.
+Qed.
+
+Lemma bblock_non_empty bb: body bb <> nil \/ exit bb <> None.
+Proof.
+ destruct bb. simpl.
+ unfold non_empty_bblockb in correct.
+ unfold non_empty_body, non_empty_exit, Is_true in correct.
+ destruct body, exit.
+ - right. discriminate.
+ - contradiction.
+ - right. discriminate.
+ - left. discriminate.
+Qed.
+
+Lemma list_length_z_aux_increase A (l: list A): forall acc,
+ list_length_z_aux l acc >= acc.
+Proof.
+ induction l; simpl; intros.
+ - lia.
+ - generalize (IHl (Z.succ acc)). lia.
+Qed.
+
+Lemma bblock_size_aux_pos bb: list_length_z (body bb) + Z.of_nat (length_opt (exit bb)) >= 1.
+Proof.
+ destruct (bblock_non_empty bb), (body bb) as [|hd tl], (exit bb); simpl;
+ try (congruence || lia);
+ unfold list_length_z; simpl;
+ generalize (list_length_z_aux_increase _ tl 1); lia.
+Qed.
+
+
+Lemma list_length_add_acc A (l : list A) acc:
+ list_length_z_aux l acc = (list_length_z l) + acc.
+Proof.
+ unfold list_length_z, list_length_z_aux. simpl.
+ fold list_length_z_aux.
+ rewrite (list_length_z_aux_shift l acc 0).
+ lia.
+Qed.
+
+Lemma list_length_z_cons A hd (tl : list A):
+ list_length_z (hd :: tl) = list_length_z tl + 1.
Proof.
- intros. exploit functions_translated; eauto. intros [tf' [A B]].
- monadInv B. rewrite H0 in EQ; inv EQ; auto.
+ unfold list_length_z; simpl; rewrite list_length_add_acc; reflexivity.
Qed.
-(** * Properties of control flow *)
+Lemma bblock_size_aux bb: size bb = list_length_z (header bb) + list_length_z (body bb) + Z.of_nat (length_opt (exit bb)).
+Proof.
+ unfold size.
+ repeat (rewrite list_length_z_nat). repeat (rewrite Nat2Z.inj_add). reflexivity.
+Qed.
+
+Lemma header_size_lt_block_size bb:
+ list_length_z (header bb) < size bb.
+Proof.
+ rewrite bblock_size_aux.
+ generalize (bblock_non_empty bb); intros NEMPTY; destruct NEMPTY as [HDR|EXIT].
+ - destruct (body bb); try contradiction; rewrite list_length_z_cons;
+ repeat rewrite list_length_z_nat; lia.
+ - destruct (exit bb); try contradiction; simpl; repeat rewrite list_length_z_nat; lia.
+Qed.
-Lemma transf_function_no_overflow:
- forall f tf,
- transf_function f = OK tf -> list_length_z tf.(fn_code) <= Ptrofs.max_unsigned.
+Lemma body_size_le_block_size bb:
+ list_length_z (body bb) <= size bb.
Proof.
- intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (list_length_z x.(fn_code))); inv EQ0.
+ rewrite bblock_size_aux; repeat rewrite list_length_z_nat; lia.
+Qed.
+
+
+Lemma bblock_size_pos bb: size bb >= 1.
+Proof.
+ rewrite (bblock_size_aux bb).
+ generalize (bblock_size_aux_pos bb).
+ generalize (list_length_z_pos (header bb)).
lia.
Qed.
-Lemma exec_straight_exec:
- forall fb f c ep tf tc c' rs m rs' m',
- transl_code_at_pc ge (rs PC) fb f c ep tf tc ->
- exec_straight tge tf tc rs m c' rs' m' ->
- plus step tge (State rs m) E0 (State rs' m').
-Proof.
- intros. inv H.
- eapply exec_straight_steps_1; eauto.
- eapply transf_function_no_overflow; eauto.
- eapply functions_transl; eauto.
-Qed.
-
-Lemma exec_straight_at:
- forall fb f c ep tf tc c' ep' tc' rs m rs' m',
- transl_code_at_pc ge (rs PC) fb f c ep tf tc ->
- transl_code f c' ep' = OK tc' ->
- exec_straight tge tf tc rs m tc' rs' m' ->
- transl_code_at_pc ge (rs' PC) fb f c' ep' tf tc'.
-Proof.
- intros. inv H.
- exploit exec_straight_steps_2; eauto.
- eapply transf_function_no_overflow; eauto.
- eapply functions_transl; eauto.
- intros [ofs' [PC' CT']].
- rewrite PC'. constructor; auto.
-Qed.
-
-(** The following lemmas show that the translation from Mach to Asm
- preserves labels, in the sense that the following diagram commutes:
-<<
- translation
- Mach code ------------------------ Asm instr sequence
- | |
- | Mach.find_label lbl find_label lbl |
- | |
- v v
- Mach code tail ------------------- Asm instr seq tail
- translation
->>
- The proof demands many boring lemmas showing that Asm constructor
- functions do not introduce new labels.
-*)
+Lemma unfold_car_cdr bb bbs tc:
+ unfold (bb :: bbs) = OK tc ->
+ exists tbb tc', unfold_bblock bb = OK tbb
+ /\ unfold bbs = OK tc'
+ /\ unfold (bb :: bbs) = OK (tbb ++ tc').
+Proof.
+ intros UNFOLD.
+ assert (UF := UNFOLD).
+ unfold unfold in UNFOLD.
+ apply bind_inversion in UNFOLD. destruct UNFOLD as (? & UBB). destruct UBB as (UBB & REST).
+ apply bind_inversion in REST. destruct REST as (? & UNFOLD').
+ fold unfold in UNFOLD'. destruct UNFOLD' as (UNFOLD' & UNFOLD).
+ rewrite <- UNFOLD in UF.
+ eauto.
+Qed.
-Section TRANSL_LABEL.
+Lemma unfold_cdr bb bbs tc:
+ unfold (bb :: bbs) = OK tc ->
+ exists tc', unfold bbs = OK tc'.
+Proof.
+ intros; exploit unfold_car_cdr; eauto. intros (_ & ? & _ & ? & _).
+ eexists; eauto.
+Qed.
-Remark loadimm_z_label: forall sz rd l k, tail_nolabel k (loadimm_z sz rd l k).
-Proof.
- intros; destruct l as [ | [n1 p1] l]; simpl; TailNoLabel.
- induction l as [ | [n p] l]; simpl; TailNoLabel.
+Lemma unfold_car bb bbs tc:
+ unfold (bb :: bbs) = OK tc ->
+ exists tbb, unfold_bblock bb = OK tbb.
+Proof.
+ intros; exploit unfold_car_cdr; eauto. intros (? & _ & ? & _ & _).
+ eexists; eauto.
Qed.
-Remark loadimm_n_label: forall sz rd l k, tail_nolabel k (loadimm_n sz rd l k).
-Proof.
- intros; destruct l as [ | [n1 p1] l]; simpl; TailNoLabel.
- induction l as [ | [n p] l]; simpl; TailNoLabel.
+Lemma all_blocks_translated:
+ forall bbs tc,
+ unfold bbs = OK tc ->
+ forall bb, In bb bbs ->
+ exists c, unfold_bblock bb = OK c.
+Proof.
+ induction bbs as [| bb bbs IHbbs].
+ - contradiction.
+ - intros ? UNFOLD ? IN.
+ (* unfold proceeds by unfolding the basic block at the head of the list and
+ * then recurring *)
+ exploit unfold_car_cdr; eauto. intros (? & ? & ? & ? & _).
+ (* basic block is either in head or tail *)
+ inversion IN as [EQ | NEQ].
+ + rewrite <- EQ; eexists; eauto.
+ + eapply IHbbs; eauto.
Qed.
-Remark loadimm_label: forall sz rd n k, tail_nolabel k (loadimm sz rd n k).
+Lemma entire_body_translated:
+ forall lbi tc,
+ unfold_body lbi = OK tc ->
+ forall bi, In bi lbi ->
+ exists bi', basic_to_instruction bi = OK bi'.
Proof.
- unfold loadimm; intros. destruct Nat.leb; [apply loadimm_z_label|apply loadimm_n_label].
+ induction lbi as [| a lbi IHlbi].
+ - intros. contradiction.
+ - intros tc UNFOLD_BODY bi IN.
+ unfold unfold_body in UNFOLD_BODY. apply bind_inversion in UNFOLD_BODY.
+ destruct UNFOLD_BODY as (? & TRANSbi & REST).
+ apply bind_inversion in REST. destruct REST as (? & UNFOLD_BODY' & ?).
+ fold unfold_body in UNFOLD_BODY'.
+
+ inversion IN as [EQ | NEQ].
+ + rewrite <- EQ; eauto.
+ + eapply IHlbi; eauto.
Qed.
-Hint Resolve loadimm_label: labels.
-Remark loadimm32_label: forall r n k, tail_nolabel k (loadimm32 r n k).
+Lemma bblock_in_bblocks bbs bb: forall
+ tc pos
+ (UNFOLD: unfold bbs = OK tc)
+ (FINDBB: find_bblock pos bbs = Some bb),
+ In bb bbs.
Proof.
- unfold loadimm32; intros. destruct (is_logical_imm32 n); TailNoLabel.
+ induction bbs as [| b bbs IH].
+ - intros. inversion FINDBB.
+ - destruct pos.
+ + intros. inversion FINDBB as (EQ). rewrite <- EQ. apply in_eq.
+ + intros.
+ exploit unfold_cdr; eauto. intros (tc' & UNFOLD').
+ unfold find_bblock in FINDBB. simpl in FINDBB.
+ fold find_bblock in FINDBB.
+ apply in_cons. eapply IH; eauto.
+ + intros. inversion FINDBB.
Qed.
-Hint Resolve loadimm32_label: labels.
-Remark loadimm64_label: forall r n k, tail_nolabel k (loadimm64 r n k).
+Lemma blocks_translated tc pos bbs bb: forall
+ (UNFOLD: unfold bbs = OK tc)
+ (FINDBB: find_bblock pos bbs = Some bb),
+ exists tbb, unfold_bblock bb = OK tbb.
Proof.
- unfold loadimm64; intros. destruct (is_logical_imm64 n); TailNoLabel.
+ intros; exploit bblock_in_bblocks; eauto; intros;
+ eapply all_blocks_translated; eauto.
Qed.
-Hint Resolve loadimm64_label: labels.
-Remark addimm_aux: forall insn rd r1 n k,
- (forall rd r1 n, nolabel (insn rd r1 n)) ->
- tail_nolabel k (addimm_aux insn rd r1 n k).
+Lemma size_header b pos f bb: forall
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock pos (fn_blocks f) = Some bb),
+ list_length_z (header bb) <= 1.
Proof.
- unfold addimm_aux; intros.
- destruct Z.eqb. TailNoLabel. destruct Z.eqb; TailNoLabel.
+ intros.
+ exploit internal_functions_unfold; eauto.
+ intros (tc & FINDtf & TRANStf & ?).
+ exploit blocks_translated; eauto. intros TBB.
+
+ unfold unfold_bblock in TBB.
+ destruct (zle (list_length_z (header bb)) 1).
+ - assumption.
+ - destruct TBB as (? & TBB). discriminate TBB.
Qed.
-Remark addimm32_label: forall rd r1 n k, tail_nolabel k (addimm32 rd r1 n k).
+Lemma list_nth_z_neg A (l: list A): forall n,
+ n < 0 -> list_nth_z l n = None.
Proof.
- unfold addimm32; intros.
- destruct Int.eq. apply addimm_aux; intros; red; auto.
- destruct Int.eq. apply addimm_aux; intros; red; auto.
- destruct Int.lt; eapply tail_nolabel_trans; TailNoLabel.
+ induction l; simpl; auto.
+ intros n H; destruct (zeq _ _); (try eapply IHl); lia.
Qed.
-Hint Resolve addimm32_label: labels.
-Remark addimm64_label: forall rd r1 n k, tail_nolabel k (addimm64 rd r1 n k).
+Lemma find_bblock_neg bbs: forall pos,
+ pos < 0 -> find_bblock pos bbs = None.
Proof.
- unfold addimm64; intros.
- destruct Int64.eq. apply addimm_aux; intros; red; auto.
- destruct Int64.eq. apply addimm_aux; intros; red; auto.
- destruct Int64.lt; eapply tail_nolabel_trans; TailNoLabel.
+ induction bbs; simpl; auto.
+ intros. destruct (zlt pos 0). { reflexivity. }
+ destruct (zeq pos 0); contradiction.
Qed.
-Hint Resolve addimm64_label: labels.
-Remark logicalimm32_label: forall insn1 insn2 rd r1 n k,
- (forall rd r1 n, nolabel (insn1 rd r1 n)) ->
- (forall rd r1 r2 s, nolabel (insn2 rd r1 r2 s)) ->
- tail_nolabel k (logicalimm32 insn1 insn2 rd r1 n k).
+Lemma equal_header_size bb:
+ length (header bb) = length (unfold_label (header bb)).
Proof.
- unfold logicalimm32; intros.
- destruct (is_logical_imm32 n). TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
+ induction (header bb); auto.
+ simpl. rewrite IHl. auto.
Qed.
-Remark logicalimm64_label: forall insn1 insn2 rd r1 n k,
- (forall rd r1 n, nolabel (insn1 rd r1 n)) ->
- (forall rd r1 r2 s, nolabel (insn2 rd r1 r2 s)) ->
- tail_nolabel k (logicalimm64 insn1 insn2 rd r1 n k).
+Lemma equal_body_size:
+ forall bb tb,
+ unfold_body (body bb) = OK tb ->
+ length (body bb) = length tb.
Proof.
- unfold logicalimm64; intros.
- destruct (is_logical_imm64 n). TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
+ intros bb. induction (body bb).
+ - simpl. intros ? H. inversion H. auto.
+ - intros tb H. simpl in H. apply bind_inversion in H. destruct H as (? & BI & TAIL).
+ apply bind_inversion in TAIL. destruct TAIL as (tb' & BODY' & CONS). inv CONS.
+ simpl. specialize (IHl tb' BODY'). rewrite IHl. reflexivity.
Qed.
-Remark move_extended_label: forall rd r1 ex a k, tail_nolabel k (move_extended rd r1 ex a k).
+Lemma equal_exit_size bb:
+ length_opt (exit bb) = length (unfold_exit (exit bb)).
Proof.
- unfold move_extended, move_extended_base; intros. destruct Int.eq, ex; TailNoLabel.
+ destruct (exit bb); trivial.
Qed.
-Hint Resolve move_extended_label: labels.
-Remark arith_extended_label: forall insnX insnS rd r1 r2 ex a k,
- (forall rd r1 r2 x, nolabel (insnX rd r1 r2 x)) ->
- (forall rd r1 r2 s, nolabel (insnS rd r1 r2 s)) ->
- tail_nolabel k (arith_extended insnX insnS rd r1 r2 ex a k).
+Lemma bblock_size_preserved bb tb:
+ unfold_bblock bb = OK tb ->
+ size bb = list_length_z tb.
Proof.
- unfold arith_extended; intros. destruct Int.ltu.
- TailNoLabel.
- destruct ex; simpl; TailNoLabel.
+ unfold unfold_bblock. intros UNFOLD_BBLOCK.
+ destruct (zle (list_length_z (header bb)) 1). 2: { inversion UNFOLD_BBLOCK. }
+ apply bind_inversion in UNFOLD_BBLOCK. destruct UNFOLD_BBLOCK as (? & UNFOLD_BODY & CONS).
+ inversion CONS.
+ unfold size.
+ rewrite equal_header_size, equal_exit_size.
+ erewrite equal_body_size; eauto.
+ rewrite list_length_z_nat.
+ repeat (rewrite app_length).
+ rewrite plus_assoc. auto.
Qed.
-Remark loadsymbol_label: forall r id ofs k, tail_nolabel k (loadsymbol r id ofs k).
+Lemma size_of_blocks_max_pos_aux:
+ forall bbs tbbs pos bb,
+ find_bblock pos bbs = Some bb ->
+ unfold bbs = OK tbbs ->
+ pos + size bb <= list_length_z tbbs.
Proof.
- intros; unfold loadsymbol.
- destruct (SelectOp.symbol_is_relocatable id); TailNoLabel. destruct Ptrofs.eq; TailNoLabel.
-Qed.
-Hint Resolve loadsymbol_label: labels.
+ induction bbs as [| bb ? IHbbs].
+ - intros tbbs ? ? FINDBB; inversion FINDBB.
+ - simpl; intros tbbs pos bb' FINDBB UNFOLD.
+ apply bind_inversion in UNFOLD; destruct UNFOLD as (tbb & UNFOLD_BBLOCK & H).
+ apply bind_inversion in H; destruct H as (tbbs' & UNFOLD & CONS).
+ inv CONS.
+ destruct (zlt pos 0). { discriminate FINDBB. }
+ destruct (zeq pos 0).
+ + inv FINDBB.
+ exploit bblock_size_preserved; eauto; intros SIZE; rewrite SIZE.
+ repeat (rewrite list_length_z_nat). rewrite app_length, Nat2Z.inj_add.
+ lia.
+ + generalize (IHbbs tbbs' (pos - size bb) bb' FINDBB UNFOLD). intros IH.
+ exploit bblock_size_preserved; eauto; intros SIZE.
+ repeat (rewrite list_length_z_nat); rewrite app_length.
+ rewrite Nat2Z.inj_add; repeat (rewrite <- list_length_z_nat).
+ lia.
+Qed.
-Remark transl_cond_label: forall cond args k c,
- transl_cond cond args k = OK c -> tail_nolabel k c.
+Lemma size_of_blocks_max_pos pos f tf bi:
+ find_bblock pos (fn_blocks f) = Some bi ->
+ transf_function f = OK tf ->
+ pos + size bi <= max_pos tf.
Proof.
- unfold transl_cond; intros; destruct cond; TailNoLabel.
-- destruct is_arith_imm32; TailNoLabel. destruct is_arith_imm32; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_arith_imm32; TailNoLabel. destruct is_arith_imm32; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_logical_imm32; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_logical_imm32; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_arith_imm64; TailNoLabel. destruct is_arith_imm64; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_arith_imm64; TailNoLabel. destruct is_arith_imm64; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_logical_imm64; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
-- destruct is_logical_imm64; TailNoLabel. eapply tail_nolabel_trans; TailNoLabel.
+ unfold transf_function, max_pos.
+ intros FINDBB UNFOLD.
+ apply bind_inversion in UNFOLD. destruct UNFOLD as (? & UNFOLD & H).
+ destruct (zlt Ptrofs.max_unsigned (list_length_z x)). { discriminate H. }
+ inv H. simpl.
+ eapply size_of_blocks_max_pos_aux; eauto.
Qed.
-Remark transl_cond_branch_default_label: forall cond args lbl k c,
- transl_cond_branch_default cond args lbl k = OK c -> tail_nolabel k c.
+Lemma unfold_bblock_not_nil bb:
+ unfold_bblock bb = OK nil -> False.
Proof.
- unfold transl_cond_branch_default; intros.
- eapply tail_nolabel_trans; [eapply transl_cond_label;eauto|TailNoLabel].
+ intros.
+ exploit bblock_size_preserved; eauto. unfold list_length_z; simpl. intros SIZE.
+ generalize (bblock_size_pos bb). intros SIZE'. lia.
Qed.
-Hint Resolve transl_cond_branch_default_label: labels.
-Remark transl_cond_branch_label: forall cond args lbl k c,
- transl_cond_branch cond args lbl k = OK c -> tail_nolabel k c.
+(* same proof as list_nth_z_range (Coqlib) *)
+Lemma find_instr_range:
+ forall c n i,
+ Asm.find_instr n c = Some i -> 0 <= n < list_length_z c.
Proof.
- unfold transl_cond_branch; intros; destruct args; TailNoLabel; destruct cond; TailNoLabel.
-- destruct c0; TailNoLabel.
-- destruct c0; TailNoLabel.
-- destruct (Int.is_power2 n); TailNoLabel.
-- destruct (Int.is_power2 n); TailNoLabel.
-- destruct c0; TailNoLabel.
-- destruct c0; TailNoLabel.
-- destruct (Int64.is_power2' n); TailNoLabel.
-- destruct (Int64.is_power2' n); TailNoLabel.
+ induction c; simpl; intros.
+ discriminate.
+ rewrite list_length_z_cons. destruct (zeq n 0).
+ generalize (list_length_z_pos c); lia.
+ exploit IHc; eauto. lia.
Qed.
-Remark transl_op_label:
- forall op args r k c,
- transl_op op args r k = OK c -> tail_nolabel k c.
+Lemma find_instr_tail:
+ forall tbb pos c i,
+ Asm.find_instr pos c = Some i ->
+ Asm.find_instr (pos + list_length_z tbb) (tbb ++ c) = Some i.
Proof.
- unfold transl_op; intros; destruct op; TailNoLabel.
-- destruct (preg_of r); try discriminate; destruct (preg_of m); inv H; TailNoLabel.
-- destruct (Float.eq_dec n Float.zero); TailNoLabel.
-- destruct (Float32.eq_dec n Float32.zero); TailNoLabel.
-- apply logicalimm32_label; unfold nolabel; auto.
-- apply logicalimm32_label; unfold nolabel; auto.
-- apply logicalimm32_label; unfold nolabel; auto.
-- unfold shrx32. destruct Int.eq; TailNoLabel.
-- apply arith_extended_label; unfold nolabel; auto.
-- apply arith_extended_label; unfold nolabel; auto.
-- apply logicalimm64_label; unfold nolabel; auto.
-- apply logicalimm64_label; unfold nolabel; auto.
-- apply logicalimm64_label; unfold nolabel; auto.
-- unfold shrx64. destruct Int.eq; TailNoLabel.
-- eapply tail_nolabel_trans. eapply transl_cond_label; eauto. TailNoLabel.
-- destruct (preg_of r); try discriminate; TailNoLabel;
- (eapply tail_nolabel_trans; [eapply transl_cond_label; eauto | TailNoLabel]).
+ induction tbb as [| ? ? IHtbb].
+ - intros. unfold list_length_z; simpl. rewrite Z.add_0_r. assumption.
+ - intros. rewrite list_length_z_cons. simpl.
+ destruct (zeq (pos + (list_length_z tbb + 1)) 0).
+ + exploit find_instr_range; eauto. intros POS_RANGE.
+ generalize (list_length_z_pos tbb). lia.
+ + replace (pos + (list_length_z tbb + 1) - 1) with (pos + list_length_z tbb) by lia.
+ eapply IHtbb; eauto.
Qed.
-Remark transl_addressing_label:
- forall sz addr args insn k c,
- transl_addressing sz addr args insn k = OK c ->
- (forall ad, nolabel (insn ad)) ->
- tail_nolabel k c.
+Lemma size_of_blocks_bounds fb pos f bi:
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ find_bblock pos (fn_blocks f) = Some bi ->
+ pos + size bi <= Ptrofs.max_unsigned.
Proof.
- unfold transl_addressing; intros; destruct addr; TailNoLabel;
- eapply tail_nolabel_trans; TailNoLabel.
- eapply tail_nolabel_trans. apply arith_extended_label; unfold nolabel; auto. TailNoLabel.
+ intros; exploit internal_functions_translated; eauto.
+ intros (tf & _ & TRANSf).
+ assert (pos + size bi <= max_pos tf). { eapply size_of_blocks_max_pos; eauto. }
+ assert (max_pos tf <= Ptrofs.max_unsigned). { eapply functions_bound_max_pos; eauto. }
+ lia.
Qed.
-Remark transl_load_label:
- forall chunk addr args dst k c,
- transl_load chunk addr args dst k = OK c -> tail_nolabel k c.
+Lemma find_instr_bblock_tail:
+ forall tbb bb pos c i,
+ Asm.find_instr pos c = Some i ->
+ unfold_bblock bb = OK tbb ->
+ Asm.find_instr (pos + size bb ) (tbb ++ c) = Some i.
Proof.
- unfold transl_load; intros; destruct chunk; TailNoLabel; eapply transl_addressing_label; eauto; unfold nolabel; auto.
+ induction tbb.
+ - intros. exploit unfold_bblock_not_nil; eauto. intros. contradiction.
+ - intros. simpl.
+ destruct (zeq (pos + size bb) 0).
+ + (* absurd *)
+ exploit find_instr_range; eauto. intros POS_RANGE.
+ generalize (bblock_size_pos bb). intros SIZE. lia.
+ + erewrite bblock_size_preserved; eauto.
+ rewrite list_length_z_cons.
+ replace (pos + (list_length_z tbb + 1) - 1) with (pos + list_length_z tbb) by lia.
+ apply find_instr_tail; auto.
Qed.
-Remark transl_store_label:
- forall chunk addr args src k c,
- transl_store chunk addr args src k = OK c -> tail_nolabel k c.
+Lemma list_nth_z_find_label:
+ forall (ll : list label) il n l,
+ list_nth_z ll n = Some l ->
+ Asm.find_instr n ((unfold_label ll) ++ il) = Some (Asm.Plabel l).
Proof.
- unfold transl_store; intros; destruct chunk; TailNoLabel; eapply transl_addressing_label; eauto; unfold nolabel; auto.
+ induction ll.
+ - intros. inversion H.
+ - intros. simpl.
+ destruct (zeq n 0) as [Z | NZ].
+ + inversion H as (H'). rewrite Z in H'. simpl in H'. inv H'. reflexivity.
+ + simpl in H. destruct (zeq n 0). { contradiction. }
+ apply IHll; auto.
Qed.
-Remark indexed_memory_access_label:
- forall insn sz base ofs k,
- (forall ad, nolabel (insn ad)) ->
- tail_nolabel k (indexed_memory_access insn sz base ofs k).
+Lemma list_nth_z_find_bi:
+ forall lbi bi tlbi n bi' exit,
+ list_nth_z lbi n = Some bi ->
+ unfold_body lbi = OK tlbi ->
+ basic_to_instruction bi = OK bi' ->
+ Asm.find_instr n (tlbi ++ exit) = Some bi'.
Proof.
- unfold indexed_memory_access; intros. destruct offset_representable.
- TailNoLabel.
- eapply tail_nolabel_trans; TailNoLabel.
+ induction lbi.
+ - intros. inversion H.
+ - simpl. intros.
+ apply bind_inversion in H0. destruct H0 as (? & ? & ?).
+ apply bind_inversion in H2. destruct H2 as (? & ? & ?).
+ destruct (zeq n 0) as [Z | NZ].
+ + destruct n.
+ * inversion H as (BI). rewrite BI in *.
+ inversion H3. simpl. congruence.
+ * (* absurd *) congruence.
+ * (* absurd *) congruence.
+ + inv H3. simpl. destruct (zeq n 0). { contradiction. }
+ eapply IHlbi; eauto.
Qed.
-Remark loadind_label:
- forall base ofs ty dst k c,
- loadind base ofs ty dst k = OK c -> tail_nolabel k c.
+Lemma list_nth_z_find_bi_with_header:
+ forall ll lbi bi tlbi n bi' (rest : list Asm.instruction),
+ list_nth_z lbi (n - list_length_z ll) = Some bi ->
+ unfold_body lbi = OK tlbi ->
+ basic_to_instruction bi = OK bi' ->
+ Asm.find_instr n ((unfold_label ll) ++ (tlbi) ++ (rest)) = Some bi'.
Proof.
- unfold loadind; intros.
- destruct ty, (preg_of dst); inv H; apply indexed_memory_access_label; intros; exact I.
+ induction ll.
+ - unfold list_length_z. simpl. intros.
+ replace (n - 0) with n in H by lia. eapply list_nth_z_find_bi; eauto.
+ - intros. simpl. destruct (zeq n 0).
+ + rewrite list_length_z_cons in H. rewrite e in H.
+ replace (0 - (list_length_z ll + 1)) with (-1 - (list_length_z ll)) in H by lia.
+ generalize (list_length_z_pos ll). intros.
+ rewrite list_nth_z_neg in H; try lia. inversion H.
+ + rewrite list_length_z_cons in H.
+ replace (n - (list_length_z ll + 1)) with (n -1 - (list_length_z ll)) in H by lia.
+ eapply IHll; eauto.
Qed.
-Remark storeind_label:
- forall src base ofs ty k c,
- storeind src base ofs ty k = OK c -> tail_nolabel k c.
+(* XXX unused *)
+Lemma range_list_nth_z:
+ forall (A: Type) (l: list A) n,
+ 0 <= n < list_length_z l ->
+ exists x, list_nth_z l n = Some x.
Proof.
- unfold storeind; intros.
- destruct ty, (preg_of src); inv H; apply indexed_memory_access_label; intros; exact I.
+ induction l.
+ - intros. unfold list_length_z in H. simpl in H. lia.
+ - intros n. destruct (zeq n 0).
+ + intros. simpl. destruct (zeq n 0). { eauto. } contradiction.
+ + intros H. rewrite list_length_z_cons in H.
+ simpl. destruct (zeq n 0). { contradiction. }
+ replace (Z.pred n) with (n - 1) by lia.
+ eapply IHl; lia.
Qed.
-Remark loadptr_label:
- forall base ofs dst k, tail_nolabel k (loadptr base ofs dst k).
+Lemma list_nth_z_n_too_big:
+ forall (A: Type) (l: list A) n,
+ 0 <= n ->
+ list_nth_z l n = None ->
+ n >= list_length_z l.
Proof.
- intros. apply indexed_memory_access_label. unfold nolabel; auto.
+ induction l.
+ - intros. unfold list_length_z. simpl. lia.
+ - intros. rewrite list_length_z_cons.
+ simpl in H0.
+ destruct (zeq n 0) as [N | N].
+ + inversion H0.
+ + (* XXX there must be a more elegant way to prove this simple fact *)
+ assert (n > 0). { lia. }
+ assert (0 <= n - 1). { lia. }
+ generalize (IHl (n - 1)). intros IH.
+ assert (n - 1 >= list_length_z l). { auto. }
+ assert (n > list_length_z l); lia.
Qed.
-Remark storeptr_label:
- forall src base ofs k, tail_nolabel k (storeptr src base ofs k).
+Lemma find_instr_past_header:
+ forall labels n rest,
+ list_nth_z labels n = None ->
+ Asm.find_instr n (unfold_label labels ++ rest) =
+ Asm.find_instr (n - list_length_z labels) rest.
Proof.
- intros. apply indexed_memory_access_label. unfold nolabel; auto.
+ induction labels as [| label labels' IH].
+ - unfold list_length_z; simpl; intros; rewrite Z.sub_0_r; reflexivity.
+ - intros. simpl. destruct (zeq n 0) as [N | N].
+ + rewrite N in H. inversion H.
+ + rewrite list_length_z_cons.
+ replace (n - (list_length_z labels' + 1)) with (n - 1 - list_length_z labels') by lia.
+ simpl in H. destruct (zeq n 0). { contradiction. }
+ replace (Z.pred n) with (n - 1) in H by lia.
+ apply IH; auto.
Qed.
-Remark make_epilogue_label:
- forall f k, tail_nolabel k (make_epilogue f k).
+(* very similar to find_instr_past_header *)
+Lemma find_instr_past_body:
+ forall lbi n tlbi rest,
+ list_nth_z lbi n = None ->
+ unfold_body lbi = OK tlbi ->
+ Asm.find_instr n (tlbi ++ rest) =
+ Asm.find_instr (n - list_length_z lbi) rest.
Proof.
- unfold make_epilogue; intros. eapply tail_nolabel_trans. apply loadptr_label. TailNoLabel.
+ induction lbi.
+ - unfold list_length_z; simpl; intros ? ? ? ? H. inv H; rewrite Z.sub_0_r; reflexivity.
+ - intros n tlib ? NTH UNFOLD_BODY.
+ unfold unfold_body in UNFOLD_BODY. apply bind_inversion in UNFOLD_BODY.
+ destruct UNFOLD_BODY as (? & BI & H).
+ apply bind_inversion in H. destruct H as (? & UNFOLD_BODY' & CONS).
+ fold unfold_body in UNFOLD_BODY'. inv CONS.
+ simpl; destruct (zeq n 0) as [N|N].
+ + rewrite N in NTH; inversion NTH.
+ + rewrite list_length_z_cons.
+ replace (n - (list_length_z lbi + 1)) with (n - 1 - list_length_z lbi) by lia.
+ simpl in NTH. destruct (zeq n 0). { contradiction. }
+ replace (Z.pred n) with (n - 1) in NTH by lia.
+ apply IHlbi; auto.
Qed.
-Lemma transl_instr_label:
- forall f i ep k c,
- transl_instr f i ep k = OK c ->
- match i with Mlabel lbl => c = Plabel lbl :: k | _ => tail_nolabel k c end.
+Lemma n_beyond_body:
+ forall bb n,
+ 0 <= n < size bb ->
+ list_nth_z (header bb) n = None ->
+ list_nth_z (body bb) (n - list_length_z (header bb)) = None ->
+ n >= Z.of_nat (length (header bb) + length (body bb)).
Proof.
- unfold transl_instr; intros; destruct i; TailNoLabel.
-- eapply loadind_label; eauto.
-- eapply storeind_label; eauto.
-- destruct ep. eapply loadind_label; eauto.
- eapply tail_nolabel_trans. apply loadptr_label. eapply loadind_label; eauto.
-- eapply transl_op_label; eauto.
-- eapply transl_load_label; eauto.
-- eapply transl_store_label; eauto.
-- destruct s0; monadInv H; TailNoLabel.
-- destruct s0; monadInv H; (eapply tail_nolabel_trans; [eapply make_epilogue_label|TailNoLabel]).
-- eapply transl_cond_branch_label; eauto.
-- eapply tail_nolabel_trans; [eapply make_epilogue_label|TailNoLabel].
+ intros.
+ assert (0 <= n). { lia. }
+ generalize (list_nth_z_n_too_big label (header bb) n H2 H0). intros.
+ generalize (list_nth_z_n_too_big _ (body bb) (n - list_length_z (header bb))). intros.
+ unfold size in H.
+
+ assert (0 <= n - list_length_z (header bb)). { lia. }
+ assert (n - list_length_z (header bb) >= list_length_z (body bb)). { apply H4; auto. }
+
+ assert (n >= list_length_z (header bb) + list_length_z (body bb)). { lia. }
+ rewrite Nat2Z.inj_add.
+ repeat (rewrite <- list_length_z_nat). assumption.
Qed.
-Lemma transl_instr_label':
- forall lbl f i ep k c,
- transl_instr f i ep k = OK c ->
- find_label lbl c = if Mach.is_label lbl i then Some k else find_label lbl k.
+Lemma exec_arith_instr_dont_move_PC ai rs rs': forall
+ (BASIC: exec_arith_instr lk ai rs = rs'),
+ rs PC = rs' PC.
Proof.
- intros. exploit transl_instr_label; eauto.
- destruct i; try (intros [A B]; apply B).
- intros. subst c. simpl. auto.
+ destruct ai; simpl; intros;
+ try (rewrite <- BASIC; rewrite Pregmap.gso; auto; discriminate).
+ - destruct i; simpl in BASIC;
+ try destruct (negb _); rewrite <- BASIC;
+ repeat rewrite Pregmap.gso; try discriminate; reflexivity.
+ - destruct i; simpl in BASIC.
+ 1,2: rewrite <- BASIC; repeat rewrite Pregmap.gso; try discriminate; reflexivity.
+ destruct sz;
+ try (unfold compare_single in BASIC || unfold compare_float in BASIC);
+ destruct (rs r1), (rs r2);
+ try (rewrite <- BASIC; repeat rewrite Pregmap.gso; try (discriminate || reflexivity)).
+ - destruct i; simpl in BASIC;
+ destruct is;
+ try (unfold compare_int in BASIC || unfold compare_long in BASIC);
+ try (rewrite <- BASIC; repeat rewrite Pregmap.gso; try (discriminate || reflexivity)).
+ - destruct i; simpl in BASIC; destruct sz;
+ try (unfold compare_single in BASIC || unfold compare_float in BASIC);
+ destruct (rs r1);
+ try (rewrite <- BASIC; repeat rewrite Pregmap.gso; try (discriminate || reflexivity)).
+ - destruct fsz; rewrite <- BASIC; rewrite Pregmap.gso; try (discriminate || reflexivity).
+ - destruct fsz; rewrite <- BASIC; rewrite Pregmap.gso; try (discriminate || reflexivity).
Qed.
-Lemma transl_code_label:
- forall lbl f c ep tc,
- transl_code f c ep = OK tc ->
- match Mach.find_label lbl c with
- | None => find_label lbl tc = None
- | Some c' => exists tc', find_label lbl tc = Some tc' /\ transl_code f c' false = OK tc'
- end.
+Lemma exec_basic_dont_move_PC bi rs m rs' m': forall
+ (BASIC: exec_basic lk ge bi rs m = Next rs' m'),
+ rs PC = rs' PC.
Proof.
- induction c; simpl; intros.
- inv H. auto.
- monadInv H. rewrite (transl_instr_label' lbl _ _ _ _ _ EQ0).
- generalize (Mach.is_label_correct lbl a).
- destruct (Mach.is_label lbl a); intros.
- subst a. simpl in EQ. exists x; auto.
- eapply IHc; eauto.
+ destruct bi; simpl; intros.
+ - inv BASIC. exploit exec_arith_instr_dont_move_PC; eauto.
+ - unfold exec_load in BASIC.
+ destruct ld.
+ + unfold exec_load_rd_a in BASIC.
+ destruct Mem.loadv. 2: { discriminate BASIC. }
+ inv BASIC. rewrite Pregmap.gso; try discriminate; auto.
+ + unfold exec_load_double, is_pair_addressing_mode_correct in BASIC.
+ destruct a; try discriminate BASIC.
+ do 2 (destruct Mem.loadv; try discriminate BASIC).
+ inv BASIC. rewrite Pregmap.gso; try discriminate; auto.
+ - unfold exec_store in BASIC.
+ destruct st.
+ + unfold exec_store_rs_a in BASIC.
+ destruct Mem.storev. 2: { discriminate BASIC. }
+ inv BASIC; reflexivity.
+ + unfold exec_store_double in BASIC.
+ destruct a; try discriminate BASIC.
+ do 2 (destruct Mem.storev; try discriminate BASIC).
+ inv BASIC; reflexivity.
+ - destruct Mem.alloc, Mem.store. 2: { discriminate BASIC. }
+ inv BASIC. repeat (rewrite Pregmap.gso; try discriminate). reflexivity.
+ - destruct Mem.loadv. 2: { discriminate BASIC. }
+ destruct rs, Mem.free; try discriminate BASIC.
+ inv BASIC; rewrite Pregmap.gso; try discriminate; auto.
+ - inv BASIC; rewrite Pregmap.gso; try discriminate; auto.
+ - inv BASIC; rewrite Pregmap.gso; try discriminate; auto.
+ - inv BASIC; rewrite Pregmap.gso; try discriminate; auto.
+ - inv BASIC; rewrite Pregmap.gso; try discriminate; auto.
+ - inv BASIC; auto.
Qed.
-Lemma transl_find_label:
- forall lbl f tf,
- transf_function f = OK tf ->
- match Mach.find_label lbl f.(Mach.fn_code) with
- | None => find_label lbl tf.(fn_code) = None
- | Some c => exists tc, find_label lbl tf.(fn_code) = Some tc /\ transl_code f c false = OK tc
+Lemma exec_body_dont_move_PC_aux:
+ forall bis rs m rs' m'
+ (BODY: exec_body lk ge bis rs m = Next rs' m'),
+ rs PC = rs' PC.
+Proof.
+ induction bis.
+ - intros; inv BODY; reflexivity.
+ - simpl; intros.
+ remember (exec_basic lk ge a rs m) as bi eqn:BI; destruct bi. 2: { discriminate BODY. }
+ symmetry in BI; destruct s in BODY, BI; simpl in BODY, BI.
+ exploit exec_basic_dont_move_PC; eauto; intros AGPC; rewrite AGPC.
+ eapply IHbis; eauto.
+Qed.
+
+Lemma exec_body_dont_move_PC bb rs m rs' m': forall
+ (BODY: exec_body lk ge (body bb) rs m = Next rs' m'),
+ rs PC = rs' PC.
+Proof. apply exec_body_dont_move_PC_aux. Qed.
+
+Lemma find_instr_bblock:
+ forall n lb pos bb tlb
+ (FINDBB: find_bblock pos lb = Some bb)
+ (UNFOLD: unfold lb = OK tlb)
+ (SIZE: 0 <= n < size bb),
+ exists i, is_nth_inst bb n i /\ Asm.find_instr (pos+n) tlb = Some i.
+Proof.
+ induction lb as [| b lb IHlb].
+ - intros. inversion FINDBB.
+ - intros pos bb tlb FINDBB UNFOLD SIZE.
+ destruct pos.
+ + inv FINDBB. simpl.
+ exploit unfold_car_cdr; eauto. intros (tbb & tlb' & UNFOLD_BBLOCK & UNFOLD' & UNFOLD_cons).
+ rewrite UNFOLD in UNFOLD_cons. inversion UNFOLD_cons.
+ unfold unfold_bblock in UNFOLD_BBLOCK.
+ destruct (zle (list_length_z (header bb)) 1). 2: { inversion UNFOLD_BBLOCK. }
+ apply bind_inversion in UNFOLD_BBLOCK.
+ destruct UNFOLD_BBLOCK as (? & UNFOLD_BODY & H).
+ inversion H as (UNFOLD_BBLOCK).
+ remember (list_nth_z (header bb) n) as label_opt eqn:LBL. destruct label_opt.
+ * (* nth instruction is a label *)
+ eexists; split. { eapply is_nth_label; eauto. }
+ inversion UNFOLD_cons.
+ symmetry in LBL.
+ rewrite <- app_assoc.
+ apply list_nth_z_find_label; auto.
+ * remember (list_nth_z (body bb) (n - list_length_z (header bb))) as bi_opt eqn:BI.
+ destruct bi_opt.
+ -- (* nth instruction is a basic instruction *)
+ exploit list_nth_z_in; eauto. intros INBB.
+ exploit entire_body_translated; eauto. intros BI'.
+ destruct BI'.
+ eexists; split.
+ ++ eapply is_nth_basic; eauto.
+ ++ repeat (rewrite <- app_assoc). eapply list_nth_z_find_bi_with_header; eauto.
+ -- (* nth instruction is the exit instruction *)
+ generalize n_beyond_body. intros TEMP.
+ assert (n >= Z.of_nat (Datatypes.length (header bb)
+ + Datatypes.length (body bb))) as NGE. { auto. } clear TEMP.
+ remember (exit bb) as exit_opt eqn:EXIT. destruct exit_opt.
+ ++ rewrite <- app_assoc. rewrite find_instr_past_header; auto.
+ rewrite <- app_assoc. erewrite find_instr_past_body; eauto.
+ assert (SIZE' := SIZE).
+ unfold size in SIZE. rewrite <- EXIT in SIZE. simpl in SIZE.
+ destruct SIZE as (LOWER & UPPER).
+ repeat (rewrite Nat2Z.inj_add in UPPER).
+ repeat (rewrite <- list_length_z_nat in UPPER). repeat (rewrite Nat2Z.inj_add in NGE).
+ repeat (rewrite <- list_length_z_nat in NGE). simpl in UPPER.
+ assert (n = list_length_z (header bb) + list_length_z (body bb)). { lia. }
+ assert (n = size bb - 1). {
+ unfold size. rewrite <- EXIT. simpl.
+ repeat (rewrite Nat2Z.inj_add). repeat (rewrite <- list_length_z_nat). simpl. lia.
+ }
+ symmetry in EXIT.
+ eexists; split.
+ ** eapply is_nth_ctlflow; eauto.
+ ** simpl.
+ destruct (zeq (n - list_length_z (header bb) - list_length_z (body bb)) 0). { reflexivity. }
+ (* absurd *) lia.
+ ++ (* absurd *)
+ unfold size in SIZE. rewrite <- EXIT in SIZE. simpl in SIZE.
+ destruct SIZE as (? & SIZE'). rewrite Nat.add_0_r in SIZE'. lia.
+ + unfold find_bblock in FINDBB; simpl in FINDBB; fold find_bblock in FINDBB.
+ inversion UNFOLD as (UNFOLD').
+ apply bind_inversion in UNFOLD'. destruct UNFOLD' as (? & (UNFOLD_BBLOCK' & UNFOLD')).
+ apply bind_inversion in UNFOLD'. destruct UNFOLD' as (? & (UNFOLD' & TLB)).
+ inversion TLB.
+ generalize (IHlb _ _ _ FINDBB UNFOLD'). intros IH.
+ destruct IH as (? & (IH_is_nth & IH_find_instr)); eauto.
+ eexists; split.
+ * apply IH_is_nth.
+ * replace (Z.pos p + n) with (Z.pos p + n - size b + size b) by lia.
+ eapply find_instr_bblock_tail; try assumption.
+ replace (Z.pos p + n - size b) with (Z.pos p - size b + n) by lia.
+ apply IH_find_instr.
+ + (* absurd *)
+ generalize (Pos2Z.neg_is_neg p). intros. exploit (find_bblock_neg (b :: lb)); eauto.
+ rewrite FINDBB. intros CONTRA. inversion CONTRA.
+Qed.
+
+Lemma exec_header_simulation b ofs f bb rs m: forall
+ (ATPC: rs PC = Vptr b ofs)
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb),
+ exists s', star Asm.step tge (State rs m) E0 s'
+ /\ match_internal (list_length_z (header bb)) (State rs m) s'.
+Proof.
+ intros.
+ exploit internal_functions_unfold; eauto.
+ intros (tc & FINDtf & TRANStf & _).
+ assert (BNDhead: list_length_z (header bb) <= 1). { eapply size_header; eauto. }
+ destruct (header bb) as [|l[|]] eqn: EQhead.
+ + (* header nil *)
+ eexists; split.
+ - eapply star_refl.
+ - split; eauto.
+ unfold list_length_z; rewrite !ATPC; simpl.
+ rewrite Ptrofs.add_zero; auto.
+ + (* header one *)
+ assert (Lhead: list_length_z (header bb) = 1). { rewrite EQhead; unfold list_length_z; simpl. auto. }
+ exploit (find_instr_bblock 0); eauto.
+ { generalize (bblock_size_pos bb). lia. }
+ intros (i & NTH & FIND_INSTR).
+ inv NTH.
+ * rewrite EQhead in H; simpl in H. inv H.
+ replace (Ptrofs.unsigned ofs + 0) with (Ptrofs.unsigned ofs) in FIND_INSTR by lia.
+ eexists. split.
+ - eapply star_one.
+ eapply Asm.exec_step_internal; eauto.
+ simpl; eauto.
+ - unfold list_length_z; simpl. split; eauto.
+ intros r; destruct r; simpl; congruence || auto.
+ * (* absurd case *)
+ erewrite list_nth_z_neg in * |-; [ congruence | rewrite Lhead; lia].
+ * (* absurd case *)
+ rewrite bblock_size_aux, Lhead in *. generalize (bblock_size_aux_pos bb). lia.
+ + (* absurd case *)
+ unfold list_length_z in BNDhead. simpl in *.
+ generalize (list_length_z_aux_increase _ l1 2); lia.
+Qed.
+
+Lemma eval_addressing_preserved a rs1 rs2:
+ (forall r : preg, r <> PC -> rs1 r = rs2 r) ->
+ eval_addressing lk a rs1 = Asm.eval_addressing tge a rs2.
+Proof.
+ intros EQ.
+ destruct a; simpl; try (rewrite !EQ; congruence).
+ auto.
+Qed.
+
+Ltac next_stuck_cong := try (unfold Next, Stuck in *; congruence).
+
+Ltac inv_ok_eq :=
+ repeat match goal with
+ | [EQ: OK ?x = OK ?y |- _ ]
+ => inversion EQ; clear EQ; subst
end.
+
+Ltac reg_rwrt :=
+ match goal with
+ | [e: DR _ = DR _ |- _ ]
+ => rewrite e in *
+ end.
+
+Ltac destruct_reg_inv :=
+ repeat match goal with
+ | [ H : match ?reg with _ => _ end = _ |- _ ]
+ => simpl in *; destruct reg; try congruence; try inv_ok_eq; try reg_rwrt
+ end.
+
+Ltac destruct_ireg_inv :=
+ repeat match goal with
+ | [ H : match ?reg with _ => _ end = _ |- _ ]
+ => destruct reg as [[r|]|]; try congruence; try inv_ok_eq; subst
+ end.
+
+Ltac destruct_reg_size :=
+ simpl in *;
+ match goal with
+ | [ |- context [ match ?reg with _ => _ end ] ]
+ => destruct reg; try congruence
+ end.
+
+Ltac find_rwrt_ag :=
+ simpl in *;
+ match goal with
+ | [ AG: forall r, r <> ?PC -> _ r = _ r |- _ ]
+ => repeat rewrite <- AG; try congruence
+ end.
+
+Ltac inv_matchi :=
+ match goal with
+ | [ MATCHI : match_internal _ _ _ |- _ ]
+ => inversion MATCHI; subst; find_rwrt_ag
+ end.
+
+Ltac destruct_ir0_reg :=
+ match goal with
+ | [ |- context [ ir0 _ _ ?r ] ]
+ => unfold ir0 in *; destruct r; find_rwrt_ag; eauto
+ end.
+
+Ltac pc_not_sp :=
+ match goal with
+ | [ |- ?PC <> ?SP ]
+ => destruct (PregEq.eq SP PC); repeat congruence; discriminate
+ end.
+
+Ltac update_x_access_x :=
+ subst; rewrite !Pregmap.gss; auto.
+
+Ltac update_x_access_r :=
+ rewrite !Pregmap.gso; auto.
+
+Lemma nextinstr_agree_but_pc rs1 rs2: forall
+ (AG: forall r, r <> PC -> rs1 r = rs2 r),
+ forall r, r <> PC -> rs1 r = Asm.nextinstr rs2 r.
Proof.
- intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (list_length_z x.(fn_code))); inv EQ0.
- monadInv EQ. rewrite transl_code'_transl_code in EQ0. unfold fn_code.
- simpl. destruct (storeptr_label X30 XSP (fn_retaddr_ofs f) x) as [A B]; rewrite B.
- eapply transl_code_label; eauto.
+ intros; unfold Asm.nextinstr in *; rewrite Pregmap.gso in *; eauto.
Qed.
-End TRANSL_LABEL.
+Lemma ptrofs_nextinstr_agree rs1 rs2 n: forall
+ (BOUNDED : 0 <= n <= Ptrofs.max_unsigned)
+ (AGPC : Val.offset_ptr (rs1 PC) (Ptrofs.repr n) = rs2 PC),
+ Val.offset_ptr (rs1 PC) (Ptrofs.repr (n + 1)) = Asm.nextinstr rs2 PC.
+Proof.
+ intros; unfold Asm.nextinstr; rewrite Pregmap.gss.
+ rewrite <- Ptrofs.unsigned_one; rewrite <- (Ptrofs.unsigned_repr n); eauto;
+ rewrite <- Ptrofs.add_unsigned; rewrite <- Val.offset_ptr_assoc; rewrite AGPC; eauto.
+Qed.
-(** A valid branch in a piece of Mach code translates to a valid ``go to''
- transition in the generated Asm code. *)
+Lemma load_rd_a_preserved n rs1 m1 rs1' m1' rs2 m2 rd chk f a: forall
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (HLOAD: exec_load_rd_a lk chk f a rd rs1 m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem), Asm.exec_load tge chk f a rd rs2 m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ unfold exec_load_rd_a, Asm.exec_load in *.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ rewrite <- (eval_addressing_preserved a rs1 rs2); auto.
+ destruct (Mem.loadv _ _ _).
+ + inversion HLOAD; auto. repeat (econstructor; eauto).
+ * eapply nextinstr_agree_but_pc; intros.
+ destruct (PregEq.eq r rd); try update_x_access_x; try update_x_access_r.
+ * eapply ptrofs_nextinstr_agree; eauto.
+ + next_stuck_cong.
+Qed.
+
+Lemma load_double_preserved n rs1 m1 rs1' m1' rs2 m2 rd1 rd2 chk1 chk2 f a: forall
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (HLOAD: exec_load_double lk chk1 chk2 f a rd1 rd2 rs1 m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem), Asm.exec_load_double tge chk1 chk2 f a rd1 rd2 rs2 m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ unfold exec_load_double, Asm.exec_load_double in *.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ erewrite <- !eval_addressing_preserved; eauto.
+ destruct (is_pair_addressing_mode_correct a); try discriminate.
+ destruct (Mem.loadv _ _ _);
+ destruct (Mem.loadv chk2 m2
+ (eval_addressing lk
+ (get_offset_addr a match chk1 with
+ | Mint32 | Mfloat32| Many32 => 4
+ | _ => 8
+ end) rs1));
+ inversion HLOAD; auto.
+ repeat (econstructor; eauto).
+ * eapply nextinstr_agree_but_pc; intros.
+ destruct (PregEq.eq r rd2); destruct (PregEq.eq r rd1).
+ - try update_x_access_x.
+ - try update_x_access_x.
+ - subst; repeat rewrite Pregmap.gso, Pregmap.gss; auto.
+ - try update_x_access_r.
+ * eapply ptrofs_nextinstr_agree; eauto.
+Qed.
-Lemma find_label_goto_label:
- forall f tf lbl rs m c' b ofs,
- Genv.find_funct_ptr ge b = Some (Internal f) ->
- transf_function f = OK tf ->
- rs PC = Vptr b ofs ->
- Mach.find_label lbl f.(Mach.fn_code) = Some c' ->
- exists tc', exists rs',
- goto_label tf lbl rs m = Next rs' m
- /\ transl_code_at_pc ge (rs' PC) b f c' false tf tc'
- /\ forall r, r <> PC -> rs'#r = rs#r.
-Proof.
- intros. exploit (transl_find_label lbl f tf); eauto. rewrite H2.
- intros [tc [A B]].
- exploit label_pos_code_tail; eauto. instantiate (1 := 0).
- intros [pos' [P [Q R]]].
- exists tc; exists (rs#PC <- (Vptr b (Ptrofs.repr pos'))).
- split. unfold goto_label. rewrite P. rewrite H1. auto.
- split. rewrite Pregmap.gss. constructor; auto.
- rewrite Ptrofs.unsigned_repr. replace (pos' - 0) with pos' in Q.
- auto. lia.
- generalize (transf_function_no_overflow _ _ H0). lia.
- intros. apply Pregmap.gso; auto.
-Qed.
-
-(** Existence of return addresses *)
+Lemma store_rs_a_preserved n rs1 m1 rs1' m1' rs2 m2 v chk a: forall
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (HSTORE: exec_store_rs_a lk chk a v rs1 m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem), Asm.exec_store tge chk a v rs2 m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ unfold exec_store_rs_a, Asm.exec_store in *.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ rewrite <- (eval_addressing_preserved a rs1 rs2); auto.
+ destruct (Mem.storev _ _ _ _).
+ + inversion HSTORE; auto. repeat (econstructor; eauto).
+ * eapply nextinstr_agree_but_pc; intros.
+ subst. apply EQR. auto.
+ * eapply ptrofs_nextinstr_agree; subst; eauto.
+ + next_stuck_cong.
+Qed.
-Lemma return_address_exists:
- forall f sg ros c, is_tail (Mcall sg ros :: c) f.(Mach.fn_code) ->
- exists ra, return_address_offset f c ra.
+Lemma store_double_preserved n rs1 m1 rs1' m1' rs2 m2 v1 v2 chk1 chk2 a: forall
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (HSTORE: exec_store_double lk chk1 chk2 a v1 v2 rs1 m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem), Asm.exec_store_double tge chk1 chk2 a v1 v2 rs2 m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ unfold exec_store_double, Asm.exec_store_double in *.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ erewrite <- !eval_addressing_preserved; eauto.
+ destruct (is_pair_addressing_mode_correct a); try discriminate.
+ destruct (Mem.storev _ _ _ _);
+ try destruct (Mem.storev chk2 m
+ (eval_addressing lk
+ (get_offset_addr a
+ match chk1 with
+ | Mint32 | Mfloat32 | Many32 => 4
+ | _ => 8
+ end) rs1) v2);
+ inversion HSTORE; auto.
+ repeat (econstructor; eauto).
+ * eapply nextinstr_agree_but_pc; intros.
+ subst. apply EQR. auto.
+ * eapply ptrofs_nextinstr_agree; subst; eauto.
+Qed.
+
+Lemma next_inst_preserved n rs1 m1 rs1' m1' rs2 m2 (x: dreg) v: forall
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (NEXTI: Next rs1 # x <- v m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem),
+ Next (Asm.nextinstr rs2 # x <- v) m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ inversion NEXTI. repeat (econstructor; eauto).
+ * eapply nextinstr_agree_but_pc; intros.
+ destruct (PregEq.eq r x); try update_x_access_x; try update_x_access_r.
+ * eapply ptrofs_nextinstr_agree; eauto.
+Qed.
+
+Lemma match_internal_nextinstr_switch:
+ forall n s rs2 m2 r v,
+ r <> PC ->
+ match_internal n s (State ((Asm.nextinstr rs2)#r <- v) m2) ->
+ match_internal n s (State (Asm.nextinstr (rs2#r <- v)) m2).
Proof.
- intros. eapply Asmgenproof0.return_address_exists; eauto.
-- intros. exploit transl_instr_label; eauto.
- destruct i; try (intros [A B]; apply A). intros. subst c0. repeat constructor.
-- intros. monadInv H0.
- destruct (zlt Ptrofs.max_unsigned (list_length_z x.(fn_code))); inv EQ0. monadInv EQ.
- rewrite transl_code'_transl_code in EQ0.
- exists x; exists true; split; auto. unfold fn_code.
- constructor. apply (storeptr_label X30 XSP (fn_retaddr_ofs f0) x).
-- exact transf_function_no_overflow.
-Qed.
-
-(** * Proof of semantic preservation *)
-
-(** Semantic preservation is proved using simulation diagrams
- of the following form.
-<<
- st1 --------------- st2
- | |
- t| *|t
- | |
- v v
- st1'--------------- st2'
->>
- The invariant is the [match_states] predicate below, which includes:
-- The Asm code pointed by the PC register is the translation of
- the current Mach code sequence.
-- Mach register values and Asm register values agree.
+ unfold Asm.nextinstr; intros n s rs2 m2 r v NOTPC1 MI.
+ inversion MI; subst; constructor; auto.
+ - eapply nextinstr_agree_but_pc; intros.
+ rewrite AG; try congruence.
+ destruct (PregEq.eq r r0); try update_x_access_x; try update_x_access_r.
+ - rewrite !Pregmap.gss, !Pregmap.gso; try congruence.
+ rewrite AGPC.
+ rewrite Pregmap.gso, Pregmap.gss; try congruence.
+Qed.
+
+Lemma match_internal_nextinstr_set_parallel:
+ forall n rs1 m1 rs2 m2 r v1 v2,
+ r <> PC ->
+ match_internal n (State rs1 m1) (State (Asm.nextinstr rs2) m2) ->
+ v1 = v2 ->
+ match_internal n (State (rs1#r <- v1) m1) (State (Asm.nextinstr (rs2#r <- v2)) m2).
+Proof.
+ intros; subst; eapply match_internal_nextinstr_switch; eauto.
+ intros; eapply match_internal_set_parallel; eauto.
+Qed.
+
+Lemma exec_basic_simulation:
+ forall tf n rs1 m1 rs1' m1' rs2 m2 bi tbi
+ (BOUNDED: 0 <= n <= Ptrofs.max_unsigned)
+ (BASIC: exec_basic lk ge bi rs1 m1 = Next rs1' m1')
+ (MATCHI: match_internal n (State rs1 m1) (State rs2 m2))
+ (TRANSBI: basic_to_instruction bi = OK tbi),
+ exists rs2' m2', Asm.exec_instr tge tf tbi
+ rs2 m2 = Next rs2' m2'
+ /\ match_internal (n + 1) (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ destruct bi.
+ { (* PArith *)
+ simpl in *; destruct i.
+ 1: {
+ destruct i.
+ 1,2,3:
+ try (destruct sumbool_rec; try congruence);
+ try (monadInv TRANSBI);
+ try (destruct_reg_inv);
+ try (inv_matchi);
+ try (exploit next_inst_preserved; eauto);
+ try (repeat destruct_reg_size);
+ try (destruct_ir0_reg).
+ 1,2: (* Special case for Pfmovimmd / Pfmovimms *)
+ try (monadInv TRANSBI);
+ try (destruct_reg_inv);
+ try (inv_matchi);
+ inversion BASIC; clear BASIC; subst;
+ try (destruct (is_immediate_float64 _));
+ try (destruct (is_immediate_float32 _));
+ eexists; eexists; split; eauto;
+ repeat (eapply match_internal_nextinstr_set_parallel; try congruence);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto).
+ }
+ 1,2,3,4,5: (* PArithP, PArithPP, PArithPPP, PArithRR0R, PArithRR0, PArithARRRR0 *)
+ destruct i;
+ try (destruct sumbool_rec; try congruence);
+ try (monadInv TRANSBI);
+ try (destruct_reg_inv);
+ try (inv_matchi);
+ try (exploit next_inst_preserved; eauto);
+ try (repeat destruct_reg_size);
+ try (destruct_ir0_reg).
+ { (* PArithComparisonPP *)
+ destruct i;
+ try (monadInv TRANSBI);
+ try (inv_matchi);
+ try (destruct_reg_inv);
+ simpl in *.
+ 1,2: (* compare_long *)
+ inversion BASIC; clear BASIC; subst;
+ eexists; eexists; split; eauto;
+ unfold compare_long;
+ repeat (eapply match_internal_nextinstr_set_parallel; [ congruence | idtac | try (rewrite !AG; congruence)]);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto).
+
+ destruct sz.
+ - (* compare_single *)
+ unfold compare_single in BASIC.
+ destruct (rs1 x), (rs1 x0);
+ inversion BASIC;
+ eexists; eexists; split; eauto;
+ repeat (eapply match_internal_nextinstr_set_parallel; [ congruence | idtac | try (rewrite !AG; congruence)]);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto).
+ - (* compare_float *)
+ unfold compare_float in BASIC.
+ destruct (rs1 x), (rs1 x0);
+ inversion BASIC;
+ eexists; eexists; split; eauto;
+ repeat (eapply match_internal_nextinstr_set_parallel; [ congruence | idtac | try (rewrite !AG; congruence)]);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto). }
+ 1,2: (* PArithComparisonR0R, PArithComparisonP *)
+ destruct i;
+ try (monadInv TRANSBI);
+ try (inv_matchi);
+ try (destruct_reg_inv);
+ try (destruct_reg_size);
+ simpl in *;
+ inversion BASIC; clear BASIC; subst;
+ eexists; eexists; split; eauto;
+ unfold compare_long, compare_int, compare_float, compare_single;
+ try (destruct_reg_size);
+ repeat (eapply match_internal_nextinstr_set_parallel; [ congruence | idtac | try (rewrite !AG; congruence)]);
+ try (econstructor; eauto);
+ try (destruct_ir0_reg);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto).
+ { (* Pcset *)
+ try (monadInv TRANSBI);
+ try (inv_matchi).
+ try (exploit next_inst_preserved; eauto);
+ try (simpl in *; intros;
+ unfold if_opt_bool_val in *; unfold eval_testcond in *;
+ rewrite <- !AG; try congruence; eauto). }
+ { (* Pfmovi *)
+ try (monadInv TRANSBI);
+ try (inv_matchi);
+ try (destruct_reg_size);
+ try (destruct_ir0_reg);
+ try (exploit next_inst_preserved; eauto). }
+ { (* Pcsel *)
+ try (destruct_reg_inv);
+ try (monadInv TRANSBI);
+ try (destruct_reg_inv);
+ try (inv_matchi);
+ try (exploit next_inst_preserved; eauto);
+ simpl in *; intros;
+ unfold if_opt_bool_val in *; unfold eval_testcond in *;
+ rewrite <- !AG; try congruence; eauto. }
+ { (* Pfnmul *)
+ try (monadInv TRANSBI);
+ try (inv_matchi);
+ try (destruct_reg_size);
+ try (exploit next_inst_preserved; eauto);
+ try (find_rwrt_ag). } }
+ { (* PLoad *)
+ destruct ld.
+ - destruct ld; monadInv TRANSBI; try destruct_ireg_inv; exploit load_rd_a_preserved; eauto;
+ intros; simpl in *; destruct sz; eauto.
+ - destruct ld; monadInv TRANSBI; destruct rd1 as [[rd1'|]|]; destruct rd2 as [[rd2'|]|];
+ inv EQ; inv EQ1; exploit load_double_preserved; eauto. }
+ { (* PStore *)
+ destruct st.
+ - destruct st; monadInv TRANSBI; try destruct_ireg_inv; exploit store_rs_a_preserved; eauto;
+ simpl in *; inv_matchi; find_rwrt_ag.
+ - destruct st; monadInv TRANSBI; destruct rs0 as [[rs0'|]|]; destruct rs3 as [[rs3'|]|];
+ inv EQ; inv EQ1; exploit store_double_preserved; eauto;
+ simpl in *; inv_matchi; find_rwrt_ag. }
+ { (* Pallocframe *)
+ monadInv TRANSBI;
+ inv_matchi; try pc_not_sp;
+ destruct sz eqn:EQSZ;
+ destruct Mem.alloc eqn:EQALLOC;
+ destruct Mem.store eqn:EQSTORE; inversion BASIC; try pc_not_sp;
+ eexists; eexists; split; eauto;
+ repeat (eapply match_internal_nextinstr_set_parallel; [ try (pc_not_sp; congruence) | idtac | try (reflexivity)]);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto). }
+ { (* Pfreeframe *)
+ monadInv TRANSBI;
+ inv_matchi; try pc_not_sp;
+ destruct sz eqn:EQSZ;
+ destruct Mem.loadv eqn:EQLOAD;
+ destruct (rs1 SP) eqn:EQRS1SP;
+ try (destruct Mem.free eqn:EQFREE);
+ inversion BASIC; try pc_not_sp;
+ eexists; eexists; split; eauto;
+ repeat (eapply match_internal_nextinstr_set_parallel; [ try (pc_not_sp; congruence) | idtac | try (reflexivity)]);
+ try (econstructor; eauto);
+ try (eapply nextinstr_agree_but_pc; eauto);
+ try (eapply ptrofs_nextinstr_agree; eauto). }
+ 1,2,3,4: (* Ploadsymbol, Pcvtsw2x, Pcvtuw2x, Pcvtx2w *)
+ try (monadInv TRANSBI);
+ try (inv_matchi);
+ try (exploit next_inst_preserved; eauto);
+ rewrite symbol_addresses_preserved; eauto;
+ try (find_rwrt_ag).
+ { (* Pnop *)
+ monadInv TRANSBI; inv_matchi.
+ inversion BASIC.
+ repeat (econstructor; eauto).
+ eapply nextinstr_agree_but_pc; intros;
+ try rewrite <- H0, AG; auto.
+ try eapply ptrofs_nextinstr_agree; auto; rewrite <- H0;
+ assumption. }
+Qed.
+
+Lemma find_basic_instructions b ofs f bb tc: forall
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (UNFOLD: unfold (fn_blocks f) = OK tc),
+ forall n,
+ (n < length (body bb))%nat ->
+ exists (i : Asm.instruction) (bi : basic),
+ list_nth_z (body bb) (Z.of_nat n) = Some bi
+ /\ basic_to_instruction bi = OK i
+ /\ Asm.find_instr (Ptrofs.unsigned ofs
+ + (list_length_z (header bb))
+ + Z.of_nat n) tc
+ = Some i.
+Proof.
+ intros until n; intros NLT.
+ exploit internal_functions_unfold; eauto.
+ intros (tc' & FINDtf & TRANStf & _).
+ assert (tc' = tc) by congruence; subst.
+ exploit (find_instr_bblock (list_length_z (header bb) + Z.of_nat n)); eauto.
+ { unfold size; split.
+ - rewrite list_length_z_nat; lia.
+ - repeat (rewrite list_length_z_nat). repeat (rewrite Nat2Z.inj_add). lia. }
+ intros (i & NTH & FIND_INSTR).
+ exists i; intros.
+ inv NTH.
+ - (* absurd *) apply list_nth_z_range in H; lia.
+ - exists bi;
+ rewrite Z.add_simpl_l in H;
+ rewrite Z.add_assoc in FIND_INSTR;
+ intuition.
+ - (* absurd *) rewrite bblock_size_aux in H0;
+ rewrite H in H0; simpl in H0; repeat rewrite list_length_z_nat in H0; lia.
+Qed.
+
+(* TODO: remplacer find_basic_instructions directement par ce lemme ? *)
+Lemma find_basic_instructions_alt b ofs f bb tc n: forall
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (UNFOLD: unfold (fn_blocks f) = OK tc)
+ (BOUND: 0 <= n < list_length_z (body bb)),
+ exists (i : Asm.instruction) (bi : basic),
+ list_nth_z (body bb) n = Some bi
+ /\ basic_to_instruction bi = OK i
+ /\ Asm.find_instr (Ptrofs.unsigned ofs
+ + (list_length_z (header bb))
+ + n) tc
+ = Some i.
+Proof.
+ intros; assert ((Z.to_nat n) < length (body bb))%nat.
+ { rewrite Nat2Z.inj_lt, <- list_length_z_nat, Z2Nat.id; try lia. }
+ exploit find_basic_instructions; eauto.
+ rewrite Z2Nat.id; try lia. intros (i & bi & X).
+ eexists; eexists; intuition eauto.
+Qed.
+
+Lemma header_body_tail_bound: forall (a: basic) (li: list basic) bb ofs
+ (BOUNDBB : Ptrofs.unsigned ofs + size bb <= Ptrofs.max_unsigned)
+ (BDYLENPOS : 0 <= list_length_z (body bb) - list_length_z (a :: li) <
+ list_length_z (body bb)),
+0 <= list_length_z (header bb) + list_length_z (body bb) - list_length_z (a :: li) <=
+Ptrofs.max_unsigned.
+Proof.
+ intros.
+ assert (HBBPOS: list_length_z (header bb) >= 0) by eapply list_length_z_pos.
+ assert (HBBSIZE: list_length_z (header bb) < size bb) by eapply header_size_lt_block_size.
+ assert (OFSBOUND: 0 <= Ptrofs.unsigned ofs <= Ptrofs.max_unsigned) by eapply Ptrofs.unsigned_range_2.
+ assert (BBSIZE: size bb <= Ptrofs.max_unsigned) by lia.
+ unfold size in BBSIZE.
+ rewrite !Nat2Z.inj_add in BBSIZE.
+ rewrite <- !list_length_z_nat in BBSIZE.
+ lia.
+Qed.
+
+(* A more general version of the exec_body_simulation_plus lemma below.
+ This generalization is necessary for the induction proof inside the body.
*)
+Lemma exec_body_simulation_plus_gen li: forall b ofs f bb rs m s2 rs' m'
+ (BLI: is_tail li (body bb))
+ (ATPC: rs PC = Vptr b ofs)
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (NEMPTY_BODY: li <> nil)
+ (MATCHI: match_internal ((list_length_z (header bb)) + (list_length_z (body bb)) - (list_length_z li)) (State rs m) s2)
+ (BODY: exec_body lk ge li rs m = Next rs' m'),
+ exists s2', plus Asm.step tge s2 E0 s2'
+ /\ match_internal (size bb - (Z.of_nat (length_opt (exit bb)))) (State rs' m') s2'.
+Proof.
+ induction li as [|a li]; simpl; try congruence.
+ intros.
+ assert (BDYLENPOS: 0 <= (list_length_z (body bb) - list_length_z (a::li)) < list_length_z (body bb)). {
+ assert (Z.of_nat O < list_length_z (a::li) <= list_length_z (body bb)); try lia.
+ rewrite !list_length_z_nat; split.
+ - rewrite <- Nat2Z.inj_lt. simpl. lia.
+ - rewrite <- Nat2Z.inj_le; eapply is_tail_bound; eauto.
+ }
+ exploit internal_functions_unfold; eauto.
+ intros (tc & FINDtf & TRANStf & _).
+ exploit find_basic_instructions_alt; eauto.
+ intros (tbi & (bi & (NTHBI & TRANSBI & FIND_INSTR))).
+ exploit is_tail_list_nth_z; eauto.
+ rewrite NTHBI; simpl.
+ intros X; inversion X; subst; clear X NTHBI.
+ destruct (exec_basic _ _ _ _ _) eqn:EXEC_BASIC; next_stuck_cong.
+ destruct s as (rs1 & m1); simpl in *.
+ destruct s2 as (rs2 & m2); simpl in *.
+ assert (BOUNDBBMAX: Ptrofs.unsigned ofs + size bb <= Ptrofs.max_unsigned)
+ by (eapply size_of_blocks_bounds; eauto).
+ exploit header_body_tail_bound; eauto. intros BDYTAIL.
+ exploit exec_basic_simulation; eauto.
+ intros (rs_next' & m_next' & EXEC_INSTR & MI_NEXT).
+ exploit exec_basic_dont_move_PC; eauto. intros AGPC.
+ inversion MI_NEXT as [A B C D E M_NEXT_AGREE RS_NEXT_AGREE ATPC_NEXT PC_OFS_NEXT RS RS'].
+ subst A. subst B. subst C. subst D. subst E.
+ rewrite ATPC in AGPC. symmetry in AGPC, ATPC_NEXT.
+
+ inv MATCHI. symmetry in AGPC0.
+ rewrite ATPC in AGPC0.
+ unfold Val.offset_ptr in AGPC0.
+
+ simpl in FIND_INSTR.
+ (* Execute internal step. *)
+ exploit (Asm.exec_step_internal tge b); eauto.
+ {
+ rewrite Ptrofs.add_unsigned.
+ repeat (rewrite Ptrofs.unsigned_repr); try lia.
+ 2: {
+ assert (BOUNDOFS: 0 <= Ptrofs.unsigned ofs <= Ptrofs.max_unsigned) by eapply Ptrofs.unsigned_range_2.
+ assert (list_length_z (body bb) <= size bb) by eapply body_size_le_block_size.
+ assert (list_length_z (header bb) <= 1). { eapply size_header; eauto. }
+ lia. }
+ try rewrite list_length_z_nat; try split;
+ simpl; rewrite <- !list_length_z_nat;
+ replace (Ptrofs.unsigned ofs + (list_length_z (header bb) + list_length_z (body bb) -
+ list_length_z (a :: li))) with (Ptrofs.unsigned ofs + list_length_z (header bb) +
+ (list_length_z (body bb) - list_length_z (a :: li))) by lia;
+ try assumption; try lia. }
+
+ (* This is our STEP hypothesis. *)
+ intros STEP_NEXT.
+ destruct li as [|a' li]; simpl in *.
+ - (* case of a single instruction in li: this our base case in the induction *)
+ inversion BODY; subst.
+ eexists; split.
+ + apply plus_one. eauto.
+ + constructor; auto.
+ rewrite ATPC_NEXT.
+ apply f_equal.
+ apply f_equal.
+ rewrite bblock_size_aux, list_length_z_cons; simpl.
+ lia.
+ - exploit (IHli b ofs f bb rs1 m_next' (State rs_next' m_next')); congruence || eauto.
+ + exploit is_tail_app_def; eauto.
+ intros (l3 & EQ); rewrite EQ.
+ exploit (is_tail_app_right (l3 ++ a::nil)).
+ rewrite <- app_assoc; simpl; eauto.
+ + constructor; auto.
+ rewrite ATPC_NEXT.
+ apply f_equal.
+ apply f_equal.
+ rewrite! list_length_z_cons; simpl.
+ lia.
+ + intros (s2' & LAST_STEPS & LAST_MATCHS).
+ eexists. split; eauto.
+ eapply plus_left'; eauto.
+Qed.
-Inductive match_states: Mach.state -> Asm.state -> Prop :=
- | match_states_intro:
- forall s fb sp c ep ms m m' rs f tf tc
- (STACKS: match_stack ge s)
- (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
- (MEXT: Mem.extends m m')
- (AT: transl_code_at_pc ge (rs PC) fb f c ep tf tc)
- (AG: agree ms sp rs)
- (DXP: ep = true -> rs#X29 = parent_sp s),
- match_states (Mach.State s fb sp c ms m)
- (Asm.State rs m')
- | match_states_call:
- forall s fb ms m m' rs
- (STACKS: match_stack ge s)
- (MEXT: Mem.extends m m')
- (AG: agree ms (parent_sp s) rs)
- (ATPC: rs PC = Vptr fb Ptrofs.zero)
- (ATLR: rs RA = parent_ra s),
- match_states (Mach.Callstate s fb ms m)
- (Asm.State rs m')
- | match_states_return:
- forall s ms m m' rs
- (STACKS: match_stack ge s)
- (MEXT: Mem.extends m m')
- (AG: agree ms (parent_sp s) rs)
- (ATPC: rs PC = parent_ra s),
- match_states (Mach.Returnstate s ms m)
- (Asm.State rs m').
-
-Lemma exec_straight_steps:
- forall s fb f rs1 i c ep tf tc m1' m2 m2' sp ms2,
- match_stack ge s ->
- Mem.extends m2 m2' ->
- Genv.find_funct_ptr ge fb = Some (Internal f) ->
- transl_code_at_pc ge (rs1 PC) fb f (i :: c) ep tf tc ->
- (forall k c (TR: transl_instr f i ep k = OK c),
- exists rs2,
- exec_straight tge tf c rs1 m1' k rs2 m2'
- /\ agree ms2 sp rs2
- /\ (it1_is_parent ep i = true -> rs2#X29 = parent_sp s)) ->
- exists st',
- plus step tge (State rs1 m1') E0 st' /\
- match_states (Mach.State s fb sp c ms2 m2) st'.
-Proof.
- intros. inversion H2. subst. monadInv H7.
- exploit H3; eauto. intros [rs2 [A [B C]]].
- exists (State rs2 m2'); split.
- eapply exec_straight_exec; eauto.
- econstructor; eauto. eapply exec_straight_at; eauto.
-Qed.
-
-Lemma exec_straight_steps_goto:
- forall s fb f rs1 i c ep tf tc m1' m2 m2' sp ms2 lbl c',
- match_stack ge s ->
- Mem.extends m2 m2' ->
- Genv.find_funct_ptr ge fb = Some (Internal f) ->
- Mach.find_label lbl f.(Mach.fn_code) = Some c' ->
- transl_code_at_pc ge (rs1 PC) fb f (i :: c) ep tf tc ->
- it1_is_parent ep i = false ->
- (forall k c (TR: transl_instr f i ep k = OK c),
- exists jmp, exists k', exists rs2,
- exec_straight tge tf c rs1 m1' (jmp :: k') rs2 m2'
- /\ agree ms2 sp rs2
- /\ exec_instr tge tf jmp rs2 m2' = goto_label tf lbl rs2 m2') ->
- exists st',
- plus step tge (State rs1 m1') E0 st' /\
- match_states (Mach.State s fb sp c' ms2 m2) st'.
-Proof.
- intros. inversion H3. subst. monadInv H9.
- exploit H5; eauto. intros [jmp [k' [rs2 [A [B C]]]]].
- generalize (functions_transl _ _ _ H7 H8); intro FN.
- generalize (transf_function_no_overflow _ _ H8); intro NOOV.
- exploit exec_straight_steps_2; eauto.
- intros [ofs' [PC2 CT2]].
- exploit find_label_goto_label; eauto.
- intros [tc' [rs3 [GOTO [AT' OTH]]]].
- exists (State rs3 m2'); split.
- eapply plus_right'.
- eapply exec_straight_steps_1; eauto.
- econstructor; eauto.
- eapply find_instr_tail. eauto.
- rewrite C. eexact GOTO.
- traceEq.
- econstructor; eauto.
- apply agree_exten with rs2; auto with asmgen.
- congruence.
-Qed.
-
-Lemma exec_straight_opt_steps_goto:
- forall s fb f rs1 i c ep tf tc m1' m2 m2' sp ms2 lbl c',
- match_stack ge s ->
- Mem.extends m2 m2' ->
- Genv.find_funct_ptr ge fb = Some (Internal f) ->
- Mach.find_label lbl f.(Mach.fn_code) = Some c' ->
- transl_code_at_pc ge (rs1 PC) fb f (i :: c) ep tf tc ->
- it1_is_parent ep i = false ->
- (forall k c (TR: transl_instr f i ep k = OK c),
- exists jmp, exists k', exists rs2,
- exec_straight_opt tge tf c rs1 m1' (jmp :: k') rs2 m2'
- /\ agree ms2 sp rs2
- /\ exec_instr tge tf jmp rs2 m2' = goto_label tf lbl rs2 m2') ->
- exists st',
- plus step tge (State rs1 m1') E0 st' /\
- match_states (Mach.State s fb sp c' ms2 m2) st'.
-Proof.
- intros. inversion H3. subst. monadInv H9.
- exploit H5; eauto. intros [jmp [k' [rs2 [A [B C]]]]].
- generalize (functions_transl _ _ _ H7 H8); intro FN.
- generalize (transf_function_no_overflow _ _ H8); intro NOOV.
- inv A.
-- exploit find_label_goto_label; eauto.
- intros [tc' [rs3 [GOTO [AT' OTH]]]].
- exists (State rs3 m2'); split.
- apply plus_one. econstructor; eauto.
- eapply find_instr_tail. eauto.
- rewrite C. eexact GOTO.
- econstructor; eauto.
- apply agree_exten with rs2; auto with asmgen.
- congruence.
-- exploit exec_straight_steps_2; eauto.
- intros [ofs' [PC2 CT2]].
- exploit find_label_goto_label; eauto.
- intros [tc' [rs3 [GOTO [AT' OTH]]]].
- exists (State rs3 m2'); split.
- eapply plus_right'.
- eapply exec_straight_steps_1; eauto.
- econstructor; eauto.
- eapply find_instr_tail. eauto.
- rewrite C. eexact GOTO.
- traceEq.
- econstructor; eauto.
- apply agree_exten with rs2; auto with asmgen.
- congruence.
-Qed.
-
-(** We need to show that, in the simulation diagram, we cannot
- take infinitely many Mach transitions that correspond to zero
- transitions on the Asm side. Actually, all Mach transitions
- correspond to at least one Asm transition, except the
- transition from [Machsem.Returnstate] to [Machsem.State].
- So, the following integer measure will suffice to rule out
- the unwanted behaviour. *)
-
-Definition measure (s: Mach.state) : nat :=
- match s with
- | Mach.State _ _ _ _ _ _ => 0%nat
- | Mach.Callstate _ _ _ _ => 0%nat
- | Mach.Returnstate _ _ _ => 1%nat
+Lemma exec_body_simulation_plus b ofs f bb rs m s2 rs' m': forall
+ (ATPC: rs PC = Vptr b ofs)
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (NEMPTY_BODY: body bb <> nil)
+ (MATCHI: match_internal (list_length_z (header bb)) (State rs m) s2)
+ (BODY: exec_body lk ge (body bb) rs m = Next rs' m'),
+ exists s2', plus Asm.step tge s2 E0 s2'
+ /\ match_internal (size bb - (Z.of_nat (length_opt (exit bb)))) (State rs' m') s2'.
+Proof.
+ intros.
+ exploit exec_body_simulation_plus_gen; eauto.
+ - constructor.
+ - replace (list_length_z (header bb) + list_length_z (body bb) - list_length_z (body bb)) with (list_length_z (header bb)); auto.
+ lia.
+Qed.
+
+Lemma exec_body_simulation_star b ofs f bb rs m s2 rs' m': forall
+ (ATPC: rs PC = Vptr b ofs)
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (MATCHI: match_internal (list_length_z (header bb)) (State rs m) s2)
+ (BODY: exec_body lk ge (body bb) rs m = Next rs' m'),
+ exists s2', star Asm.step tge s2 E0 s2'
+ /\ match_internal (size bb - (Z.of_nat (length_opt (exit bb)))) (State rs' m') s2'.
+Proof.
+ intros.
+ destruct (body bb) eqn: Hbb.
+ - simpl in BODY. inv BODY.
+ eexists. split.
+ eapply star_refl; eauto.
+ assert (EQ: (size bb - Z.of_nat (length_opt (exit bb))) = list_length_z (header bb)).
+ { rewrite bblock_size_aux. rewrite Hbb; unfold list_length_z; simpl. lia. }
+ rewrite EQ; eauto.
+ - exploit exec_body_simulation_plus; congruence || eauto.
+ { rewrite Hbb; eauto. }
+ intros (s2' & PLUS & MATCHI').
+ eexists; split; eauto.
+ eapply plus_star; eauto.
+Qed.
+
+Lemma list_nth_z_range_exceeded A (l : list A) n:
+ n >= list_length_z l ->
+ list_nth_z l n = None.
+Proof.
+ intros N.
+ remember (list_nth_z l n) as opt eqn:H. symmetry in H.
+ destruct opt; auto.
+ exploit list_nth_z_range; eauto. lia.
+Qed.
+
+Lemma label_in_header_list lbl a:
+ is_label lbl a = true -> list_length_z (header a) <= 1 -> header a = lbl :: nil.
+Proof.
+ intros.
+ eapply is_label_correct_true in H.
+ destruct (header a).
+ - eapply in_nil in H. contradiction.
+ - rewrite list_length_z_cons in H0.
+ assert (list_length_z l0 >= 0) by eapply list_length_z_pos.
+ assert (list_length_z l0 = 0) by lia.
+ rewrite list_length_z_nat in H2.
+ assert (Datatypes.length l0 = 0%nat) by lia.
+ eapply length_zero_iff_nil in H3. subst.
+ unfold In in H. destruct H.
+ + subst; eauto.
+ + destruct H.
+Qed.
+
+Lemma no_label_in_basic_inst: forall a lbl x,
+ basic_to_instruction a = OK x -> Asm.is_label lbl x = false.
+Proof.
+ intros.
+ destruct a; simpl in *;
+ repeat destruct i;
+ repeat destruct ld; repeat destruct st;
+ simpl in *;
+ try (try destruct_reg_inv; monadInv H; simpl in *; reflexivity).
+Qed.
+
+Lemma label_pos_body bdy: forall c1 c2 z ex lbl
+ (HUNF : unfold_body bdy = OK c2),
+ Asm.label_pos lbl (z + Z.of_nat ((Datatypes.length bdy) + length_opt ex)) c1 = Asm.label_pos lbl (z) ((c2 ++ unfold_exit ex) ++ c1).
+Proof.
+ induction bdy.
+ - intros. inversion HUNF. simpl in *.
+ destruct ex eqn:EQEX.
+ + simpl in *. unfold Asm.is_label. destruct c; simpl; try congruence.
+ destruct i; simpl; try congruence.
+ + simpl in *. ring_simplify (z + 0). auto.
+ - intros. inversion HUNF; clear HUNF. monadInv H0. simpl in *.
+ erewrite no_label_in_basic_inst; eauto. rewrite <- IHbdy; eauto.
+ erewrite Zpos_P_of_succ_nat.
+ apply f_equal2; auto. lia.
+Qed.
+
+Lemma asm_label_pos_header: forall z a x0 x1 lbl
+ (HUNF: unfold_body (body a) = OK x1),
+ Asm.label_pos lbl (z + size a) x0 =
+ Asm.label_pos lbl (z + list_length_z (header a)) ((x1 ++ unfold_exit (exit a)) ++ x0).
+Proof.
+ intros.
+ unfold size.
+ rewrite <- plus_assoc. rewrite Nat2Z.inj_add.
+ rewrite list_length_z_nat.
+ replace (z + (Z.of_nat (Datatypes.length (header a)) + Z.of_nat (Datatypes.length (body a) + length_opt (exit a)))) with (z + Z.of_nat (Datatypes.length (header a)) + Z.of_nat (Datatypes.length (body a) + length_opt (exit a))) by lia.
+ eapply (label_pos_body (body a) x0 x1 (z + Z.of_nat (Datatypes.length (header a))) (exit a) lbl). auto.
+Qed.
+
+Lemma header_size_cons_nil: forall (l0: label) (l1: list label)
+ (HSIZE: list_length_z (l0 :: l1) <= 1),
+ l1 = nil.
+Proof.
+ intros.
+ destruct l1; try congruence. rewrite !list_length_z_cons in HSIZE.
+ assert (list_length_z l1 >= 0) by eapply list_length_z_pos.
+ assert (list_length_z l1 + 1 + 1 >= 2) by lia.
+ assert (2 <= 1) by lia. contradiction H1. lia.
+Qed.
+
+Lemma label_pos_preserved_gen bbs: forall lbl c z
+ (HUNF: unfold bbs = OK c),
+ label_pos lbl z bbs = Asm.label_pos lbl z c.
+Proof.
+ induction bbs.
+ - intros. simpl in *. inversion HUNF. simpl. reflexivity.
+ - intros. simpl in *. monadInv HUNF. unfold unfold_bblock in EQ.
+ destruct (zle _ _); try congruence. monadInv EQ.
+ destruct (is_label _ _) eqn:EQLBL.
+ + erewrite label_in_header_list; eauto.
+ simpl in *. destruct (peq lbl lbl); try congruence.
+ + erewrite IHbbs; eauto.
+ rewrite (asm_label_pos_header z a x0 x1 lbl); auto.
+ unfold is_label in *.
+ destruct (header a).
+ * replace (z + list_length_z (@nil label)) with (z); eauto.
+ unfold list_length_z. simpl. lia.
+ * eapply header_size_cons_nil in l as HL1.
+ subst. simpl in *. destruct (in_dec _ _); try congruence.
+ simpl in *.
+ destruct (peq _ _); try intuition congruence.
+Qed.
+
+Lemma label_pos_preserved f lbl z tf: forall
+ (FINDF: transf_function f = OK tf),
+ label_pos lbl z (fn_blocks f) = Asm.label_pos lbl z (Asm.fn_code tf).
+Proof.
+ intros.
+ eapply label_pos_preserved_gen.
+ unfold transf_function in FINDF. monadInv FINDF.
+ destruct zlt; try congruence. inversion EQ0. eauto.
+Qed.
+
+Lemma goto_label_preserved bb rs1 m1 rs1' m1' rs2 m2 lbl f tf v: forall
+ (FINDF: transf_function f = OK tf)
+ (BOUNDED: size bb <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal (size bb - 1) (State rs1 m1) (State rs2 m2))
+ (HGOTO: goto_label f lbl (incrPC v rs1) m1 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem), Asm.goto_label tf lbl rs2 m2 = Next rs2' m2'
+ /\ match_states (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ unfold goto_label, Asm.goto_label in *.
+ rewrite <- (label_pos_preserved f); auto.
+ inversion MATCHI as [n0 r1 mx1 r2 mx2 EQM EQR EQPC]; subst.
+ destruct label_pos; next_stuck_cong.
+ destruct (incrPC v rs1 PC) eqn:INCRPC; next_stuck_cong.
+ inversion HGOTO; auto. repeat (econstructor; eauto).
+ rewrite <- EQPC.
+ unfold incrPC in *.
+ rewrite !Pregmap.gss in *.
+ destruct (rs1 PC) eqn:EQRS1; simpl in *; try congruence.
+ replace (rs2 # PC <- (Vptr b0 (Ptrofs.repr z))) with ((rs1 # PC <- (Vptr b0 (Ptrofs.add i0 v))) # PC <- (Vptr b (Ptrofs.repr z))); auto.
+ eapply functional_extensionality. intros.
+ destruct (PregEq.eq x PC); subst.
+ rewrite !Pregmap.gss. congruence.
+ rewrite !Pregmap.gso; auto.
+Qed.
+
+Lemma next_inst_incr_pc_preserved bb rs1 m1 rs1' m1' rs2 m2 f tf: forall
+ (FINDF: transf_function f = OK tf)
+ (BOUNDED: size bb <= Ptrofs.max_unsigned)
+ (MATCHI: match_internal (size bb - 1) (State rs1 m1) (State rs2 m2))
+ (NEXT: Next (incrPC (Ptrofs.repr (size bb)) rs1) m2 = Next rs1' m1'),
+ exists (rs2' : regset) (m2' : mem),
+ Next (Asm.nextinstr rs2) m2 = Next rs2' m2'
+ /\ match_states (State rs1' m1') (State rs2' m2').
+Proof.
+ intros; simpl in *; unfold incrPC in NEXT;
+ inv_matchi;
+ assert (size bb >= 1) by eapply bblock_size_pos;
+ assert (0 <= size bb - 1 <= Ptrofs.max_unsigned) by lia;
+ inversion NEXT; subst;
+ eexists; eexists; split; eauto.
+ assert (rs1 # PC <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size bb))) = Asm.nextinstr rs2). {
+ unfold Pregmap.set. apply functional_extensionality.
+ intros x. destruct (PregEq.eq x PC).
+ -- unfold Asm.nextinstr. rewrite <- AGPC.
+ rewrite Val.offset_ptr_assoc. rewrite Ptrofs.add_unsigned.
+ rewrite (Ptrofs.unsigned_repr (size bb - 1)); try lia.
+ rewrite Ptrofs.unsigned_one.
+ replace (size bb - 1 + 1) with (size bb) by lia.
+ rewrite e. rewrite Pregmap.gss.
+ reflexivity.
+ -- eapply nextinstr_agree_but_pc; eauto. }
+ rewrite H1. econstructor.
+Qed.
+
+Lemma pc_reg_overwrite: forall (r: ireg) rs1 m1 rs2 m2 bb
+ (MATCHI: match_internal (size bb - 1) (State rs1 m1) (State rs2 m2)),
+ rs2 # PC <- (rs2 r) =
+ (rs1 # PC <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size bb)))) # PC <-
+ (rs1 r).
+Proof.
+ intros.
+ unfold Pregmap.set; apply functional_extensionality.
+ intros x; destruct (PregEq.eq x PC) as [X | X]; try discriminate; inv_matchi.
+Qed.
+
+Lemma exec_cfi_simulation:
+ forall bb f tf rs1 m1 rs1' m1' rs2 m2 cfi
+ (SIZE: size bb <= Ptrofs.max_unsigned)
+ (FINDF: transf_function f = OK tf)
+ (* Warning: Asmblock's PC is assumed to be already pointing on the next instruction ! *)
+ (CFI: exec_cfi ge f cfi (incrPC (Ptrofs.repr (size bb)) rs1) m1 = Next rs1' m1')
+ (MATCHI: match_internal (size bb - 1) (State rs1 m1) (State rs2 m2)),
+ exists rs2' m2', Asm.exec_instr tge tf (cf_instruction_to_instruction cfi)
+ rs2 m2 = Next rs2' m2'
+ /\ match_states (State rs1' m1') (State rs2' m2').
+Proof.
+ intros.
+ assert (BBPOS: size bb >= 1) by eapply bblock_size_pos.
+ destruct cfi; inv CFI; simpl.
+ - (* Pb *)
+ exploit goto_label_preserved; eauto.
+ - (* Pbc *)
+ inv_matchi.
+ unfold eval_testcond in *. destruct c;
+ erewrite !incrPC_agree_but_pc in H0; try rewrite <- !AG; try congruence.
+ all:
+ destruct_reg_size;
+ try destruct b eqn:EQB.
+ 1,4,7,10,13,16,19,22,25,28,31,34:
+ exploit goto_label_preserved; eauto.
+ 1,3,5,7,9,11,13,15,17,19,21,23:
+ exploit next_inst_incr_pc_preserved; eauto.
+ all: repeat (econstructor; eauto).
+ - (* Pbl *)
+ eexists; eexists; split; eauto.
+ assert ( ((incrPC (Ptrofs.repr (size bb)) rs1) # X30 <- (incrPC (Ptrofs.repr (size bb)) rs1 PC))
+ # PC <- (Genv.symbol_address ge id Ptrofs.zero)
+ = (rs2 # X30 <- (Val.offset_ptr (rs2 PC) Ptrofs.one))
+ # PC <- (Genv.symbol_address tge id Ptrofs.zero)
+ ) as EQRS. {
+ unfold incrPC. unfold Pregmap.set. simpl. apply functional_extensionality.
+ intros x. destruct (PregEq.eq x PC).
+ * rewrite symbol_addresses_preserved. reflexivity.
+ * destruct (PregEq.eq x X30).
+ -- inv MATCHI. rewrite <- AGPC. rewrite Val.offset_ptr_assoc.
+ unfold Ptrofs.add, Ptrofs.one. repeat (rewrite Ptrofs.unsigned_repr); try lia.
+ replace (size bb - 1 + 1) with (size bb) by lia. reflexivity.
+ -- inv MATCHI; rewrite AG; try assumption; reflexivity.
+ } rewrite EQRS; inv MATCHI; reflexivity.
+ - (* Pbs *)
+ eexists; eexists; split; eauto.
+ assert ( (incrPC (Ptrofs.repr (size bb)) rs1) # PC <-
+ (Genv.symbol_address ge id Ptrofs.zero)
+ = rs2 # PC <- (Genv.symbol_address tge id Ptrofs.zero)
+ ) as EQRS. {
+ unfold incrPC, Pregmap.set. rewrite symbol_addresses_preserved. inv MATCHI.
+ apply functional_extensionality. intros x. destruct (PregEq.eq x PC); auto.
+ } rewrite EQRS; inv MATCHI; reflexivity.
+ - (* Pblr *)
+ eexists; eexists; split; eauto.
+ unfold incrPC. rewrite Pregmap.gss. rewrite Pregmap.gso; try discriminate.
+ assert ( (rs2 # X30 <- (Val.offset_ptr (rs2 PC) Ptrofs.one)) # PC <- (rs2 r)
+ = ((rs1 # PC <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size bb))))
+ # X30 <- (Val.offset_ptr (rs1 PC) (Ptrofs.repr (size bb))))
+ # PC <- (rs1 r)
+ ) as EQRS. {
+ unfold Pregmap.set. apply functional_extensionality.
+ intros x; destruct (PregEq.eq x PC) as [X | X].
+ - inv_matchi; rewrite AG; auto.
+ - destruct (PregEq.eq x X30) as [X' | X'].
+ + inversion MATCHI; subst. rewrite <- AGPC.
+ rewrite Val.offset_ptr_assoc. unfold Ptrofs.one.
+ rewrite Ptrofs.add_unsigned. rewrite Ptrofs.unsigned_repr; try lia. rewrite Ptrofs.unsigned_repr; try lia.
+ rewrite Z.sub_add; reflexivity.
+ + inv_matchi.
+ } rewrite EQRS. inv_matchi.
+ - (* Pbr *)
+ eexists; eexists; split; eauto.
+ unfold incrPC. rewrite Pregmap.gso; try discriminate.
+ rewrite (pc_reg_overwrite r rs1 m1' rs2 m2 bb); auto.
+ inv_matchi.
+ - (* Pret *)
+ eexists; eexists; split; eauto.
+ unfold incrPC. rewrite Pregmap.gso; try discriminate.
+ rewrite (pc_reg_overwrite r rs1 m1' rs2 m2 bb); auto.
+ inv_matchi.
+ - (* Pcbnz *)
+ inv_matchi.
+ unfold eval_neg_branch in *.
+ erewrite incrPC_agree_but_pc in H0; try congruence.
+ destruct eval_testzero; next_stuck_cong.
+ destruct b.
+ * exploit next_inst_incr_pc_preserved; eauto.
+ * exploit goto_label_preserved; eauto.
+ - (* Pcbz *)
+ inv_matchi.
+ unfold eval_branch in *.
+ erewrite incrPC_agree_but_pc in H0; try congruence.
+ destruct eval_testzero; next_stuck_cong.
+ destruct b.
+ * exploit goto_label_preserved; eauto.
+ * exploit next_inst_incr_pc_preserved; eauto.
+ - (* Ptbnbz *)
+ inv_matchi.
+ unfold eval_branch in *.
+ erewrite incrPC_agree_but_pc in H0; try congruence.
+ destruct eval_testbit; next_stuck_cong.
+ destruct b.
+ * exploit goto_label_preserved; eauto.
+ * exploit next_inst_incr_pc_preserved; eauto.
+ - (* Ptbz *)
+ inv_matchi.
+ unfold eval_neg_branch in *.
+ erewrite incrPC_agree_but_pc in H0; try congruence.
+ destruct eval_testbit; next_stuck_cong.
+ destruct b.
+ * exploit next_inst_incr_pc_preserved; eauto.
+ * exploit goto_label_preserved; eauto.
+ - (* Pbtbl *)
+ assert (rs2 # X16 <- Vundef r1 = (incrPC (Ptrofs.repr (size bb)) rs1) # X16 <- Vundef r1)
+ as EQUNDEFX16. {
+ unfold incrPC, Pregmap.set.
+ destruct (PregEq.eq r1 X16) as [X16 | X16]; auto.
+ destruct (PregEq.eq r1 PC) as [PC' | PC']; try discriminate.
+ inv MATCHI; rewrite AG; auto.
+ } rewrite <- EQUNDEFX16 in H0.
+ destruct_reg_inv; next_stuck_cong.
+ unfold goto_label, Asm.goto_label in *.
+ rewrite <- (label_pos_preserved f); auto.
+ inversion MATCHI; subst.
+ destruct label_pos; next_stuck_cong.
+ destruct ((incrPC (Ptrofs.repr (size bb)) rs1) # X16 <- Vundef PC) eqn:INCRPC; next_stuck_cong.
+ inversion H0; auto. repeat (econstructor; eauto).
+ rewrite !Pregmap.gso; try congruence.
+ rewrite <- AGPC.
+ unfold incrPC in *.
+ destruct (rs1 PC) eqn:EQRS1; simpl in *; try discriminate.
+ replace ((rs2 # X16 <- Vundef) # PC <- (Vptr b0 (Ptrofs.repr z))) with
+ (((rs1 # PC <- (Vptr b0 (Ptrofs.add i1 (Ptrofs.repr (size bb))))) # X16 <-
+ Vundef) # PC <- (Vptr b (Ptrofs.repr z))); auto.
+ eapply functional_extensionality; intros x.
+ destruct (PregEq.eq x PC); subst.
+ + rewrite Pregmap.gso in INCRPC; try congruence.
+ rewrite Pregmap.gss in INCRPC.
+ rewrite !Pregmap.gss in *; congruence.
+ + rewrite Pregmap.gso; auto.
+ rewrite (Pregmap.gso (i := x) (j := PC)); auto.
+ destruct (PregEq.eq x X16); subst.
+ * rewrite !Pregmap.gss; auto.
+ * rewrite !Pregmap.gso; auto.
+Qed.
+
+Lemma last_instruction_cannot_be_label bb:
+ list_nth_z (header bb) (size bb - 1) = None.
+Proof.
+ assert (list_length_z (header bb) <= size bb - 1). {
+ rewrite bblock_size_aux. generalize (bblock_size_aux_pos bb). lia.
+ }
+ remember (list_nth_z (header bb) (size bb - 1)) as label_opt; destruct label_opt; auto;
+ exploit list_nth_z_range; eauto; lia.
+Qed.
+
+Lemma pc_ptr_exec_step: forall ofs bb b rs m _rs _m
+ (ATPC : rs PC = Vptr b ofs)
+ (MATCHI : match_internal (size bb - 1)
+ {| _rs := rs; _m := m |}
+ {| _rs := _rs; _m := _m |}),
+ _rs PC = Vptr b (Ptrofs.add ofs (Ptrofs.repr (size bb - 1))).
+Proof.
+ intros; inv MATCHI. rewrite <- AGPC; rewrite ATPC; unfold Val.offset_ptr; eauto.
+Qed.
+
+Lemma find_instr_ofs_somei: forall ofs bb f tc asmi rs m _rs _m
+ (BOUNDOFS : Ptrofs.unsigned ofs + size bb <= Ptrofs.max_unsigned)
+ (FIND_INSTR : Asm.find_instr (Ptrofs.unsigned ofs + (size bb - 1)) tc =
+ Some (asmi))
+ (MATCHI : match_internal (size bb - 1)
+ {| _rs := rs; _m := m |}
+ {| _rs := _rs; _m := _m |}),
+ Asm.find_instr (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr (size bb - 1))))
+ (Asm.fn_code {| Asm.fn_sig := fn_sig f; Asm.fn_code := tc |}) =
+ Some (asmi).
+Proof.
+ intros; simpl.
+ replace (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr (size bb - 1))))
+ with (Ptrofs.unsigned ofs + (size bb - 1)); try assumption.
+ generalize (bblock_size_pos bb); generalize (Ptrofs.unsigned_range_2 ofs); intros.
+ unfold Ptrofs.add.
+ rewrite Ptrofs.unsigned_repr. rewrite Ptrofs.unsigned_repr; try lia.
+ rewrite Ptrofs.unsigned_repr; lia.
+Qed.
+
+Lemma eval_builtin_arg_match: forall rs _m _rs a1 b1
+ (AG : forall r : preg, r <> PC -> rs r = _rs r)
+ (EVAL : eval_builtin_arg tge (fun r : dreg => rs r) (rs SP) _m a1 b1),
+ eval_builtin_arg tge _rs (_rs SP) _m (map_builtin_arg DR a1) b1.
+Proof.
+ intros; induction EVAL; simpl in *; try rewrite AG; try rewrite AG in EVAL; try discriminate; try congruence; eauto with barg.
+ econstructor. rewrite <- AG; try discriminate; auto.
+Qed.
+
+Lemma eval_builtin_args_match: forall bb rs m _rs _m args vargs
+ (MATCHI : match_internal (size bb - 1)
+ {| _rs := rs; _m := m |}
+ {| _rs := _rs; _m := _m |})
+ (EVAL : eval_builtin_args tge (fun r : dreg => rs r) (rs SP) m args vargs),
+ eval_builtin_args tge _rs (_rs SP) _m (map (map_builtin_arg DR) args) vargs.
+Proof.
+ intros; inv MATCHI.
+ induction EVAL; subst.
+ - econstructor.
+ - econstructor.
+ + eapply eval_builtin_arg_match; eauto.
+ + eauto.
+Qed.
+
+Lemma pc_both_sides: forall (rs _rs: regset) v
+ (AG : forall r : preg, r <> PC -> rs r = _rs r),
+ rs # PC <- v = _rs # PC <- v.
+Proof.
+ intros; unfold Pregmap.set; apply functional_extensionality; intros y.
+ destruct (PregEq.eq y PC); try rewrite AG; eauto.
+Qed.
+
+Lemma set_buitin_res_sym res: forall vres rs _rs r
+ (NPC: r <> PC)
+ (AG : forall r : preg, r <> PC -> rs r = _rs r),
+ set_res res vres rs r = set_res res vres _rs r.
+Proof.
+ induction res; simpl; intros; unfold Pregmap.set; try rewrite AG; eauto.
+Qed.
+
+Lemma set_builtin_res_dont_move_pc_gen res: forall vres rs _rs v1 v2
+ (HV: v1 = v2)
+ (AG : forall r : preg, r <> PC -> rs r = _rs r),
+ (set_res res vres rs) # PC <- v1 =
+ (set_res res vres _rs) # PC <- v2.
+Proof.
+ intros. rewrite HV. generalize res vres rs _rs AG v2.
+ clear res vres rs _rs AG v1 v2 HV.
+ induction res.
+ - simpl; intros. apply pc_both_sides; intros.
+ unfold Pregmap.set; try rewrite AG; eauto.
+ - simpl; intros; apply pc_both_sides; eauto.
+ - simpl; intros.
+ erewrite IHres2; eauto; intros.
+ eapply set_buitin_res_sym; eauto.
+Qed.
+
+Lemma set_builtin_map_not_pc (res: builtin_res dreg): forall vres rs,
+ set_res (map_builtin_res DR res) vres rs PC = rs PC.
+Proof.
+ induction res.
+ - intros; simpl. unfold Pregmap.set. destruct (PregEq.eq PC x); try congruence.
+ - intros; simpl; congruence.
+ - intros; simpl in *. rewrite IHres2. rewrite IHres1. reflexivity.
+Qed.
+
+Lemma undef_reg_preserved (rl: list mreg): forall rs _rs r
+ (NPC: r <> PC)
+ (AG : forall r : preg, r <> PC -> rs r = _rs r),
+ undef_regs (map preg_of rl) rs r = undef_regs (map preg_of rl) _rs r.
+Proof.
+ induction rl.
+ - simpl; auto.
+ - simpl; intros. erewrite IHrl; eauto.
+ intros. unfold Pregmap.set. destruct (PregEq.eq r0 (preg_of a)); try rewrite AG; eauto.
+Qed.
+
+Lemma undef_regs_other:
+ forall r rl rs,
+ (forall r', In r' rl -> r <> r') ->
+ undef_regs rl rs r = rs r.
+Proof.
+ induction rl; simpl; intros. auto.
+ rewrite IHrl by auto. rewrite Pregmap.gso; auto.
+Qed.
+
+Fixpoint preg_notin (r: preg) (rl: list mreg) : Prop :=
+ match rl with
+ | nil => True
+ | r1 :: nil => r <> preg_of r1
+ | r1 :: rl => r <> preg_of r1 /\ preg_notin r rl
end.
-Remark preg_of_not_X29: forall r, negb (mreg_eq r R29) = true -> IR X29 <> preg_of r.
-Proof.
- intros. change (IR X29) with (preg_of R29). red; intros.
- exploit preg_of_injective; eauto. intros; subst r; discriminate.
-Qed.
-
-Lemma sp_val': forall ms sp rs, agree ms sp rs -> sp = rs XSP.
-Proof.
- intros. eapply sp_val; eauto.
-Qed.
-
-(** This is the simulation diagram. We prove it by case analysis on the Mach transition. *)
-
-Theorem step_simulation:
- forall S1 t S2, Mach.step return_address_offset ge S1 t S2 ->
- forall S1' (MS: match_states S1 S1'),
- (exists S2', plus step tge S1' t S2' /\ match_states S2 S2')
- \/ (measure S2 < measure S1 /\ t = E0 /\ match_states S2 S1')%nat.
-Proof.
- induction 1; intros; inv MS.
-
-- (* Mlabel *)
- left; eapply exec_straight_steps; eauto; intros.
- monadInv TR. econstructor; split. apply exec_straight_one. simpl; eauto. auto.
- split. apply agree_nextinstr; auto. simpl; congruence.
-
-- (* Mgetstack *)
- unfold load_stack in H.
- exploit Mem.loadv_extends; eauto. intros [v' [A B]].
- rewrite (sp_val _ _ _ AG) in A.
- left; eapply exec_straight_steps; eauto. intros. simpl in TR.
- exploit loadind_correct; eauto with asmgen. intros [rs' [P [Q R]]].
- exists rs'; split. eauto.
- split. eapply agree_set_mreg; eauto with asmgen. congruence.
- simpl; congruence.
-
-- (* Msetstack *)
- unfold store_stack in H.
- assert (Val.lessdef (rs src) (rs0 (preg_of src))) by (eapply preg_val; eauto).
- exploit Mem.storev_extends; eauto. intros [m2' [A B]].
- left; eapply exec_straight_steps; eauto.
- rewrite (sp_val _ _ _ AG) in A. intros. simpl in TR.
- exploit storeind_correct; eauto with asmgen. intros [rs' [P Q]].
- exists rs'; split. eauto.
- split. eapply agree_undef_regs; eauto with asmgen.
- simpl; intros. rewrite Q; auto with asmgen.
-
-- (* Mgetparam *)
- assert (f0 = f) by congruence; subst f0.
- unfold load_stack in *.
- exploit Mem.loadv_extends. eauto. eexact H0. auto.
- intros [parent' [A B]]. rewrite (sp_val' _ _ _ AG) in A.
- exploit lessdef_parent_sp; eauto. clear B; intros B; subst parent'.
- exploit Mem.loadv_extends. eauto. eexact H1. auto.
- intros [v' [C D]].
-Opaque loadind.
- left; eapply exec_straight_steps; eauto; intros. monadInv TR.
- destruct ep.
-(* X30 contains parent *)
- exploit loadind_correct. eexact EQ.
- instantiate (2 := rs0). simpl; rewrite DXP; eauto. simpl; congruence.
- intros [rs1 [P [Q R]]].
- exists rs1; split. eauto.
- split. eapply agree_set_mreg. eapply agree_set_mreg; eauto. congruence. auto with asmgen.
- simpl; intros. rewrite R; auto with asmgen.
- apply preg_of_not_X29; auto.
-(* X30 does not contain parent *)
- exploit loadptr_correct. eexact A. simpl; congruence. intros [rs1 [P [Q R]]].
- exploit loadind_correct. eexact EQ. instantiate (2 := rs1). simpl; rewrite Q. eauto. simpl; congruence.
- intros [rs2 [S [T U]]].
- exists rs2; split. eapply exec_straight_trans; eauto.
- split. eapply agree_set_mreg. eapply agree_set_mreg. eauto. eauto.
- instantiate (1 := rs1#X29 <- (rs2#X29)). intros.
- rewrite Pregmap.gso; auto with asmgen.
- congruence.
- intros. unfold Pregmap.set. destruct (PregEq.eq r' X29). congruence. auto with asmgen.
- simpl; intros. rewrite U; auto with asmgen.
- apply preg_of_not_X29; auto.
-
-- (* Mop *)
- assert (eval_operation tge sp op (map rs args) m = Some v).
- { rewrite <- H. apply eval_operation_preserved. exact symbols_preserved. }
- exploit eval_operation_lessdef. eapply preg_vals; eauto. eauto. eexact H0.
- intros [v' [A B]]. rewrite (sp_val _ _ _ AG) in A.
- left; eapply exec_straight_steps; eauto; intros. simpl in TR.
- exploit transl_op_correct; eauto. intros [rs2 [P [Q R]]].
- exists rs2; split. eauto. split.
- apply agree_set_undef_mreg with rs0; auto.
- apply Val.lessdef_trans with v'; auto.
- simpl; intros. InvBooleans.
- rewrite R; auto. apply preg_of_not_X29; auto.
-Local Transparent destroyed_by_op.
- destruct op; try exact I; simpl; congruence.
-
-- (* Mload *)
- assert (Op.eval_addressing tge sp addr (map rs args) = Some a).
- { rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved. }
- exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
- intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
- exploit Mem.loadv_extends; eauto. intros [v' [C D]].
- left; eapply exec_straight_steps; eauto; intros. simpl in TR.
- exploit transl_load_correct; eauto. intros [rs2 [P [Q R]]].
- exists rs2; split. eauto.
- split. eapply agree_set_undef_mreg; eauto. congruence.
- simpl; congruence.
-
-- (* Mstore *)
- assert (Op.eval_addressing tge sp addr (map rs args) = Some a).
- { rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved. }
- exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
- intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
- assert (Val.lessdef (rs src) (rs0 (preg_of src))) by (eapply preg_val; eauto).
- exploit Mem.storev_extends; eauto. intros [m2' [C D]].
- left; eapply exec_straight_steps; eauto.
- intros. simpl in TR. exploit transl_store_correct; eauto. intros [rs2 [P Q]].
- exists rs2; split. eauto.
- split. eapply agree_undef_regs; eauto with asmgen.
- simpl; congruence.
-
-- (* Mcall *)
- assert (f0 = f) by congruence. subst f0.
- inv AT.
- assert (NOOV: list_length_z tf.(fn_code) <= Ptrofs.max_unsigned).
- { eapply transf_function_no_overflow; eauto. }
- destruct ros as [rf|fid]; simpl in H; monadInv H5.
-+ (* Indirect call *)
- assert (rs rf = Vptr f' Ptrofs.zero).
- { destruct (rs rf); try discriminate.
- revert H; predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
- assert (rs0 x0 = Vptr f' Ptrofs.zero).
- { exploit ireg_val; eauto. rewrite H5; intros LD; inv LD; auto. }
- generalize (code_tail_next_int _ _ _ _ NOOV H6). intro CT1.
- assert (TCA: transl_code_at_pc ge (Vptr fb (Ptrofs.add ofs Ptrofs.one)) fb f c false tf x).
- { econstructor; eauto. }
- exploit return_address_offset_correct; eauto. intros; subst ra.
- left; econstructor; split.
- apply plus_one. eapply exec_step_internal. Simpl. rewrite <- H2; simpl; eauto.
- eapply functions_transl; eauto. eapply find_instr_tail; eauto.
- simpl. eauto.
- econstructor; eauto.
- econstructor; eauto.
- eapply agree_sp_def; eauto.
- simpl. eapply agree_exten; eauto. intros. Simpl.
- Simpl. rewrite <- H2. auto.
-+ (* Direct call *)
- generalize (code_tail_next_int _ _ _ _ NOOV H6). intro CT1.
- assert (TCA: transl_code_at_pc ge (Vptr fb (Ptrofs.add ofs Ptrofs.one)) fb f c false tf x).
- econstructor; eauto.
- exploit return_address_offset_correct; eauto. intros; subst ra.
- left; econstructor; split.
- apply plus_one. eapply exec_step_internal. eauto.
- eapply functions_transl; eauto. eapply find_instr_tail; eauto.
- simpl. unfold Genv.symbol_address. rewrite symbols_preserved. rewrite H. eauto.
- econstructor; eauto.
- econstructor; eauto.
- eapply agree_sp_def; eauto.
- simpl. eapply agree_exten; eauto. intros. Simpl.
- Simpl. rewrite <- H2. auto.
-
-- (* Mtailcall *)
- assert (f0 = f) by congruence. subst f0.
- inversion AT; subst.
- assert (NOOV: list_length_z tf.(fn_code) <= Ptrofs.max_unsigned).
- { eapply transf_function_no_overflow; eauto. }
- exploit Mem.loadv_extends. eauto. eexact H1. auto. simpl. intros [parent' [A B]].
- destruct ros as [rf|fid]; simpl in H; monadInv H7.
-+ (* Indirect call *)
- assert (rs rf = Vptr f' Ptrofs.zero).
- { destruct (rs rf); try discriminate.
- revert H; predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
- assert (rs0 x0 = Vptr f' Ptrofs.zero).
- { exploit ireg_val; eauto. rewrite H7; intros LD; inv LD; auto. }
- exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
- exploit exec_straight_steps_2; eauto using functions_transl.
- intros (ofs' & P & Q).
- left; econstructor; split.
- (* execution *)
- eapply plus_right'. eapply exec_straight_exec; eauto.
- econstructor. eexact P. eapply functions_transl; eauto. eapply find_instr_tail. eexact Q.
- simpl. reflexivity.
- traceEq.
- (* match states *)
- econstructor; eauto.
- apply agree_set_other; auto with asmgen.
- Simpl. rewrite Z by (rewrite <- (ireg_of_eq _ _ EQ1); eauto with asmgen). assumption.
-+ (* Direct call *)
- exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
- exploit exec_straight_steps_2; eauto using functions_transl.
- intros (ofs' & P & Q).
- left; econstructor; split.
- (* execution *)
- eapply plus_right'. eapply exec_straight_exec; eauto.
- econstructor. eexact P. eapply functions_transl; eauto. eapply find_instr_tail. eexact Q.
- simpl. reflexivity.
- traceEq.
- (* match states *)
- econstructor; eauto.
- apply agree_set_other; auto with asmgen.
- Simpl. unfold Genv.symbol_address. rewrite symbols_preserved. rewrite H. auto.
-
-- (* Mbuiltin *)
- inv AT. monadInv H4.
- exploit functions_transl; eauto. intro FN.
- generalize (transf_function_no_overflow _ _ H3); intro NOOV.
- exploit builtin_args_match; eauto. intros [vargs' [P Q]].
- exploit external_call_mem_extends; eauto.
- intros [vres' [m2' [A [B [C D]]]]].
- left. econstructor; split. apply plus_one.
- eapply exec_step_builtin. eauto. eauto.
- eapply find_instr_tail; eauto.
- erewrite <- sp_val by eauto.
- eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
- eapply external_call_symbols_preserved; eauto. apply senv_preserved.
- eauto.
- econstructor; eauto.
- instantiate (2 := tf); instantiate (1 := x).
- unfold nextinstr. rewrite Pregmap.gss.
- rewrite set_res_other. rewrite undef_regs_other.
- rewrite <- H1. simpl. econstructor; eauto.
- eapply code_tail_next_int; eauto.
- simpl; intros. destruct H4. congruence. destruct H4. congruence.
- exploit list_in_map_inv; eauto. intros (mr & U & V). subst.
- auto with asmgen.
- auto with asmgen.
- apply agree_nextinstr. eapply agree_set_res; auto.
- eapply agree_undef_regs; eauto. intros.
- simpl. rewrite undef_regs_other_2; auto. Simpl.
- congruence.
-
-- (* Mgoto *)
- assert (f0 = f) by congruence. subst f0.
- inv AT. monadInv H4.
- exploit find_label_goto_label; eauto. intros [tc' [rs' [GOTO [AT2 INV]]]].
- left; exists (State rs' m'); split.
- apply plus_one. econstructor; eauto.
- eapply functions_transl; eauto.
- eapply find_instr_tail; eauto.
- simpl; eauto.
- econstructor; eauto.
- eapply agree_exten; eauto with asmgen.
- congruence.
-
-- (* Mcond true *)
- assert (f0 = f) by congruence. subst f0.
- exploit eval_condition_lessdef. eapply preg_vals; eauto. eauto. eauto. intros EC.
- left; eapply exec_straight_opt_steps_goto; eauto.
- intros. simpl in TR.
- exploit transl_cond_branch_correct; eauto. intros (rs' & jmp & A & B & C).
- exists jmp; exists k; exists rs'.
- split. eexact A.
- split. apply agree_exten with rs0; auto with asmgen.
- exact B.
-
-- (* Mcond false *)
- exploit eval_condition_lessdef. eapply preg_vals; eauto. eauto. eauto. intros EC.
- left; eapply exec_straight_steps; eauto. intros. simpl in TR.
- exploit transl_cond_branch_correct; eauto. intros (rs' & jmp & A & B & C).
- econstructor; split.
- eapply exec_straight_opt_right. eexact A. apply exec_straight_one. eexact B. auto.
- split. apply agree_exten with rs0; auto. intros. Simpl.
- simpl; congruence.
-
-- (* Mjumptable *)
- assert (f0 = f) by congruence. subst f0.
- inv AT. monadInv H6.
- exploit functions_transl; eauto. intro FN.
- generalize (transf_function_no_overflow _ _ H5); intro NOOV.
- exploit find_label_goto_label. eauto. eauto.
- instantiate (2 := rs0#X16 <- Vundef).
- Simpl. eauto.
- eauto.
- intros [tc' [rs' [A [B C]]]].
- exploit ireg_val; eauto. rewrite H. intros LD; inv LD.
- left; econstructor; split.
- apply plus_one. econstructor; eauto.
- eapply find_instr_tail; eauto.
- simpl. Simpl. rewrite <- H9. unfold Mach.label in H0; unfold label; rewrite H0. eexact A.
- econstructor; eauto.
- eapply agree_undef_regs; eauto.
- simpl. intros. rewrite C; auto with asmgen. Simpl.
- congruence.
-
-- (* Mreturn *)
- assert (f0 = f) by congruence. subst f0.
- inversion AT; subst. simpl in H6; monadInv H6.
- assert (NOOV: list_length_z tf.(fn_code) <= Ptrofs.max_unsigned).
- eapply transf_function_no_overflow; eauto.
- exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
- exploit exec_straight_steps_2; eauto using functions_transl.
- intros (ofs' & P & Q).
- left; econstructor; split.
- (* execution *)
- eapply plus_right'. eapply exec_straight_exec; eauto.
- econstructor. eexact P. eapply functions_transl; eauto. eapply find_instr_tail. eexact Q.
- simpl. reflexivity.
- traceEq.
- (* match states *)
- econstructor; eauto.
- apply agree_set_other; auto with asmgen.
-
-- (* internal function *)
-
- exploit functions_translated; eauto. intros [tf [A B]]. monadInv B.
- generalize EQ; intros EQ'. monadInv EQ'.
- destruct (zlt Ptrofs.max_unsigned (list_length_z x0.(fn_code))); inversion EQ1. clear EQ1. subst x0.
- unfold store_stack in *.
- exploit Mem.alloc_extends. eauto. eauto. apply Z.le_refl. apply Z.le_refl.
- intros [m1' [C D]].
- exploit Mem.storev_extends. eexact D. eexact H1. eauto. eauto.
- intros [m2' [F G]].
- simpl chunk_of_type in F.
- exploit Mem.storev_extends. eexact G. eexact H2. eauto. eauto.
- intros [m3' [P Q]].
- change (chunk_of_type Tptr) with Mint64 in *.
- (* Execution of function prologue *)
- monadInv EQ0. rewrite transl_code'_transl_code in EQ1.
- set (tfbody := Pallocframe (fn_stacksize f) (fn_link_ofs f) ::
- storeptr RA XSP (fn_retaddr_ofs f) x0) in *.
- set (tf := {| fn_sig := Mach.fn_sig f; fn_code := tfbody |}) in *.
- set (rs2 := nextinstr (rs0#X29 <- (parent_sp s) #SP <- sp #X16 <- Vundef)).
- exploit (storeptr_correct tge tf XSP (fn_retaddr_ofs f) RA x0 m2' m3' rs2).
- simpl preg_of_iregsp. change (rs2 X30) with (rs0 X30). rewrite ATLR.
- change (rs2 X2) with sp. eexact P.
- simpl; congruence. congruence.
- intros (rs3 & U & V).
- assert (EXEC_PROLOGUE:
- exec_straight tge tf
- tf.(fn_code) rs0 m'
- x0 rs3 m3').
- { change (fn_code tf) with tfbody; unfold tfbody.
- apply exec_straight_step with rs2 m2'.
- unfold exec_instr. rewrite C. fold sp.
- rewrite <- (sp_val _ _ _ AG). rewrite F. reflexivity.
- reflexivity.
- eexact U. }
- exploit exec_straight_steps_2; eauto using functions_transl. lia. constructor.
- intros (ofs' & X & Y).
- left; exists (State rs3 m3'); split.
- eapply exec_straight_steps_1; eauto. lia. constructor.
- econstructor; eauto.
- rewrite X; econstructor; eauto.
- apply agree_exten with rs2; eauto with asmgen.
- unfold rs2.
- apply agree_nextinstr. apply agree_set_other; auto with asmgen.
- apply agree_change_sp with (parent_sp s).
- apply agree_undef_regs with rs0. auto.
-Local Transparent destroyed_at_function_entry. simpl.
- simpl; intros; Simpl.
- unfold sp; congruence.
- intros. rewrite V by auto with asmgen. reflexivity.
-
-- (* external function *)
- exploit functions_translated; eauto.
- intros [tf [A B]]. simpl in B. inv B.
- exploit extcall_arguments_match; eauto.
- intros [args' [C D]].
- exploit external_call_mem_extends; eauto.
- intros [res' [m2' [P [Q [R S]]]]].
- left; econstructor; split.
- apply plus_one. eapply exec_step_external; eauto.
- eapply external_call_symbols_preserved; eauto. apply senv_preserved.
- econstructor; eauto.
- unfold loc_external_result. apply agree_set_other; auto. apply agree_set_pair; auto.
- apply agree_undef_caller_save_regs; auto.
-
-- (* return *)
- inv STACKS. simpl in *.
- right. split. lia. split. auto.
- rewrite <- ATPC in H5.
- econstructor; eauto. congruence.
+Remark preg_notin_charact:
+ forall r rl,
+ preg_notin r rl <-> (forall mr, In mr rl -> r <> preg_of mr).
+Proof.
+ induction rl; simpl; intros.
+ tauto.
+ destruct rl.
+ simpl. split. intros. intuition congruence. auto.
+ rewrite IHrl. split.
+ intros [A B]. intros. destruct H. congruence. auto.
+ auto.
Qed.
-Lemma transf_initial_states:
- forall st1, Mach.initial_state prog st1 ->
- exists st2, Asm.initial_state tprog st2 /\ match_states st1 st2.
+Lemma undef_regs_other_2:
+ forall r rl rs,
+ preg_notin r rl ->
+ undef_regs (map preg_of rl) rs r = rs r.
Proof.
- intros. inversion H. unfold ge0 in *.
- econstructor; split.
- econstructor.
- eapply (Genv.init_mem_transf_partial TRANSF); eauto.
- replace (Genv.symbol_address (Genv.globalenv tprog) (prog_main tprog) Ptrofs.zero)
- with (Vptr fb Ptrofs.zero).
- econstructor; eauto.
- constructor.
- apply Mem.extends_refl.
- split. auto. simpl. unfold Vnullptr; destruct Archi.ptr64; congruence.
- intros. rewrite Regmap.gi. auto.
- unfold Genv.symbol_address.
- rewrite (match_program_main TRANSF).
- rewrite symbols_preserved.
- unfold ge; rewrite H1. auto.
+ intros. apply undef_regs_other. intros.
+ exploit list_in_map_inv; eauto. intros [mr [A B]]. subst.
+ rewrite preg_notin_charact in H. auto.
Qed.
-Lemma transf_final_states:
- forall st1 st2 r,
- match_states st1 st2 -> Mach.final_state st1 r -> Asm.final_state st2 r.
+Lemma exec_exit_simulation_plus b ofs f bb s2 t rs m rs' m': forall
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (NEMPTY_EXIT: exit bb <> None)
+ (MATCHI: match_internal (size bb - Z.of_nat (length_opt (exit bb))) (State rs m) s2)
+ (EXIT: exec_exit ge f (Ptrofs.repr (size bb)) rs m (exit bb) t rs' m')
+ (ATPC: rs PC = Vptr b ofs),
+ plus Asm.step tge s2 t (State rs' m').
+Proof.
+ intros.
+ exploit internal_functions_unfold; eauto.
+ intros (tc & FINDtf & TRANStf & _).
+
+ exploit (find_instr_bblock (size bb - 1)); eauto.
+ { generalize (bblock_size_pos bb). lia. }
+ intros (i' & NTH & FIND_INSTR).
+
+ inv NTH.
+ + rewrite last_instruction_cannot_be_label in *. discriminate.
+ + destruct (exit bb) as [ctrl |] eqn:NEMPTY_EXIT'. 2: { contradiction. }
+ rewrite bblock_size_aux in *. rewrite NEMPTY_EXIT' in *. simpl in *.
+ (* XXX: Is there a better way to simplify this expression i.e. automatically? *)
+ replace (list_length_z (header bb) + list_length_z (body bb) + 1 - 1 -
+ list_length_z (header bb)) with (list_length_z (body bb)) in H by lia.
+ rewrite list_nth_z_range_exceeded in H; try lia. discriminate.
+ + assert (Ptrofs.unsigned ofs + size bb <= Ptrofs.max_unsigned). {
+ eapply size_of_blocks_bounds; eauto.
+ }
+ assert (size bb <= Ptrofs.max_unsigned). { generalize (Ptrofs.unsigned_range_2 ofs); lia. }
+ destruct cfi.
+ * (* control flow instruction *)
+ destruct s2.
+ rewrite H in EXIT. (* exit bb is a cfi *)
+ inv EXIT.
+ rewrite H in MATCHI. simpl in MATCHI.
+ exploit internal_functions_translated; eauto.
+ rewrite FINDtf.
+ intros (tf & FINDtf' & TRANSf). inversion FINDtf'; subst; clear FINDtf'.
+ exploit exec_cfi_simulation; eauto.
+ (* extract exec_cfi_simulation's conclusion as separate hypotheses *)
+ intros (rs2' & m2' & EXECI & MATCHS); rewrite MATCHS.
+ apply plus_one.
+ eapply Asm.exec_step_internal; eauto.
+ - eapply pc_ptr_exec_step; eauto.
+ - eapply find_instr_ofs_somei; eauto.
+ * (* builtin *)
+ destruct s2.
+ rewrite H in EXIT.
+ rewrite H in MATCHI. simpl in MATCHI.
+ simpl in FIND_INSTR.
+ inversion EXIT.
+ apply plus_one.
+ eapply external_call_symbols_preserved in H10; try (apply senv_preserved).
+ eapply eval_builtin_args_preserved in H6; try (apply symbols_preserved).
+ eapply Asm.exec_step_builtin; eauto.
+ - eapply pc_ptr_exec_step; eauto.
+ - eapply find_instr_ofs_somei; eauto.
+ - eapply eval_builtin_args_match; eauto.
+ - inv MATCHI; eauto.
+ - inv MATCHI.
+ unfold Asm.nextinstr, incrPC.
+ assert (HPC: Val.offset_ptr (rs PC) (Ptrofs.repr (size bb))
+ = Val.offset_ptr (_rs PC) Ptrofs.one).
+ { rewrite <- AGPC. rewrite ATPC. unfold Val.offset_ptr.
+ rewrite Ptrofs.add_assoc. unfold Ptrofs.add.
+ assert (BBPOS: size bb >= 1) by eapply bblock_size_pos.
+ rewrite (Ptrofs.unsigned_repr (size bb - 1)); try lia.
+ rewrite Ptrofs.unsigned_one.
+ replace (size bb - 1 + 1) with (size bb) by lia.
+ reflexivity. }
+ apply set_builtin_res_dont_move_pc_gen.
+ -- erewrite !set_builtin_map_not_pc.
+ erewrite !undef_regs_other.
+ rewrite HPC; auto.
+ all: intros; simpl in *; destruct H3 as [HX16 | [HX30 | HDES]]; subst; try discriminate;
+ exploit list_in_map_inv; eauto; intros [mr [A B]]; subst; discriminate.
+ -- intros. eapply undef_reg_preserved; eauto.
+ intros. destruct (PregEq.eq X16 r0); destruct (PregEq.eq X30 r0); subst.
+ rewrite Pregmap.gso, Pregmap.gss; try congruence.
+ do 2 (rewrite Pregmap.gso, Pregmap.gss; try discriminate; auto).
+ rewrite 2Pregmap.gss; auto.
+ rewrite !Pregmap.gso; auto.
+Qed.
+
+Lemma exec_exit_simulation_star b ofs f bb s2 t rs m rs' m': forall
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (MATCHI: match_internal (size bb - Z.of_nat (length_opt (exit bb))) (State rs m) s2)
+ (EXIT: exec_exit ge f (Ptrofs.repr (size bb)) rs m (exit bb) t rs' m')
+ (ATPC: rs PC = Vptr b ofs),
+ star Asm.step tge s2 t (State rs' m').
+Proof.
+ intros.
+ destruct (exit bb) eqn: Hex.
+ - eapply plus_star.
+ eapply exec_exit_simulation_plus; try rewrite Hex; congruence || eauto.
+ - inv MATCHI.
+ inv EXIT.
+ assert (X: rs2 = incrPC (Ptrofs.repr (size bb)) rs). {
+ unfold incrPC. unfold Pregmap.set.
+ apply functional_extensionality. intros x.
+ destruct (PregEq.eq x PC) as [X|].
+ - rewrite X. rewrite <- AGPC. simpl.
+ replace (size bb - 0) with (size bb) by lia. reflexivity.
+ - rewrite AG; try assumption. reflexivity.
+ }
+ destruct X.
+ subst; eapply star_refl; eauto.
+Qed.
+
+Lemma exec_bblock_simulation b ofs f bb t rs m rs' m': forall
+ (ATPC: rs PC = Vptr b ofs)
+ (FINDF: Genv.find_funct_ptr ge b = Some (Internal f))
+ (FINDBB: find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb)
+ (EXECBB: exec_bblock lk ge f bb rs m t rs' m'),
+ plus Asm.step tge (State rs m) t (State rs' m').
Proof.
- intros. inv H0. inv H. constructor. assumption.
- compute in H1. inv H1.
- generalize (preg_val _ _ _ R0 AG). rewrite H2. intros LD; inv LD. auto.
+ intros; destruct EXECBB as (rs1 & m1 & BODY & CTL).
+ exploit exec_header_simulation; eauto.
+ intros (s0 & STAR & MATCH0).
+ eapply star_plus_trans; traceEq || eauto.
+ destruct (bblock_non_empty bb).
+ - (* body bb <> nil *)
+ exploit exec_body_simulation_plus; eauto.
+ intros (s1 & PLUS & MATCH1).
+ eapply plus_star_trans; traceEq || eauto.
+ eapply exec_exit_simulation_star; eauto.
+ erewrite <- exec_body_dont_move_PC; eauto.
+ - (* exit bb <> None *)
+ exploit exec_body_simulation_star; eauto.
+ intros (s1 & STAR1 & MATCH1).
+ eapply star_plus_trans; traceEq || eauto.
+ eapply exec_exit_simulation_plus; eauto.
+ erewrite <- exec_body_dont_move_PC; eauto.
Qed.
+Lemma step_simulation s t s':
+ Asmblock.step lk ge s t s' -> plus Asm.step tge s t s'.
+Proof.
+ intros STEP.
+ inv STEP; simpl; exploit functions_translated; eauto;
+ intros (tf0 & FINDtf & TRANSf);
+ monadInv TRANSf.
+ - (* internal step *) eapply exec_bblock_simulation; eauto.
+ - (* external step *)
+ apply plus_one.
+ exploit external_call_symbols_preserved; eauto. apply senv_preserved.
+ intros ?.
+ eapply Asm.exec_step_external; eauto.
+Qed.
+
+Lemma transf_program_correct:
+ forward_simulation (Asmblock.semantics lk prog) (Asm.semantics tprog).
+Proof.
+ eapply forward_simulation_plus.
+ - apply senv_preserved.
+ - eexact transf_initial_states.
+ - eexact transf_final_states.
+ - unfold match_states.
+ simpl; intros; subst; eexists; split; eauto.
+ eapply step_simulation; eauto.
+Qed.
+
+End PRESERVATION.
+
+End Asmblock_PRESERVATION.
+
+
+Local Open Scope linking_scope.
+
+Definition block_passes :=
+ mkpass Machblockgenproof.match_prog
+ ::: mkpass Asmblockgenproof.match_prog
+ ::: mkpass PostpassSchedulingproof.match_prog
+ ::: mkpass Asmblock_PRESERVATION.match_prog
+ ::: pass_nil _.
+
+Definition match_prog := pass_match (compose_passes block_passes).
+
+Lemma transf_program_match:
+ forall p tp, Asmgen.transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros p tp H.
+ unfold Asmgen.transf_program in H. apply bind_inversion in H. destruct H.
+ inversion_clear H. apply bind_inversion in H1. destruct H1.
+ inversion_clear H.
+ unfold Compopts.time in *. remember (Machblockgen.transf_program p) as mbp.
+ unfold match_prog; simpl.
+ exists mbp; split. apply Machblockgenproof.transf_program_match; auto.
+ exists x; split. apply Asmblockgenproof.transf_program_match; auto.
+ exists x0; split. apply PostpassSchedulingproof.transf_program_match; auto.
+ exists tp; split. apply Asmblock_PRESERVATION.transf_program_match; auto. auto.
+Qed.
+
+(** Return Address Offset *)
+
+Definition return_address_offset: Mach.function -> Mach.code -> ptrofs -> Prop :=
+ Machblockgenproof.Mach_return_address_offset (Asmblockgenproof.return_address_offset).
+
+Lemma return_address_exists:
+ forall f sg ros c, is_tail (Mach.Mcall sg ros :: c) f.(Mach.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros; unfold return_address_offset; eapply Machblockgenproof.Mach_return_address_exists; eauto.
+ intros; eapply Asmblockgenproof.return_address_exists; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variable prog: Mach.program.
+Variable tprog: Asm.program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
Theorem transf_program_correct:
forward_simulation (Mach.semantics return_address_offset prog) (Asm.semantics tprog).
Proof.
- eapply forward_simulation_star with (measure := measure).
- apply senv_preserved.
- eexact transf_initial_states.
- eexact transf_final_states.
- exact step_simulation.
+ unfold match_prog in TRANSF. simpl in TRANSF.
+ inv TRANSF. inv H. inv H1. inv H. inv H2. inv H. inv H3. inv H.
+ eapply compose_forward_simulations.
+ { exploit Machblockgenproof.transf_program_correct; eauto. }
+
+ eapply compose_forward_simulations.
+ + apply Asmblockgenproof.transf_program_correct; eauto.
+ { intros.
+ unfold Genv.symbol_address.
+ erewrite <- PostpassSchedulingproof.symbols_preserved; eauto.
+ erewrite Asmblock_PRESERVATION.symbol_high_low; eauto.
+ reflexivity.
+ }
+ + eapply compose_forward_simulations.
+ - apply PostpassSchedulingproof.transf_program_correct; eauto.
+ - apply Asmblock_PRESERVATION.transf_program_correct; eauto.
Qed.
End PRESERVATION.
+
+Instance TransfAsm: TransfLink match_prog := pass_match_link (compose_passes block_passes).
+
+(*******************************************)
+(* Stub actually needed by driver/Compiler *)
+
+Module Asmgenproof0.
+
+Definition return_address_offset := return_address_offset.
+
+End Asmgenproof0.
diff --git a/aarch64/BTL_SEsimplify.v b/aarch64/BTL_SEsimplify.v
new file mode 100644
index 00000000..3e930439
--- /dev/null
+++ b/aarch64/BTL_SEsimplify.v
@@ -0,0 +1,41 @@
+Require Import Coqlib Floats Values Memory.
+Require Import Integers.
+Require Import Op Registers.
+Require Import BTL_SEtheory.
+Require Import BTL_SEsimuref.
+
+(** Target op simplifications using "fake" values *)
+
+Definition target_op_simplify (op: operation) (lr: list reg) (hrs: ristate): option sval :=
+ None.
+
+Definition target_cbranch_expanse (prev: ristate) (cond: condition) (args: list reg) : option (condition * list_sval) :=
+ None.
+
+(* Main proof of simplification *)
+
+Lemma target_op_simplify_correct ctx op lr hrs fsv st args: forall
+ (H: target_op_simplify op lr hrs = Some fsv)
+ (REF: ris_refines ctx hrs st)
+ (OK0: ris_ok ctx hrs)
+ (OK1: eval_list_sval ctx (list_sval_inj (map (si_sreg st) lr)) = Some args),
+ eval_sval ctx fsv = eval_operation (cge ctx) (csp ctx) op args (cm0 ctx).
+Proof.
+ unfold target_op_simplify; simpl.
+ intros H ? ? ?.
+ congruence.
+Qed.
+
+Lemma target_cbranch_expanse_correct hrs c l ctx st c' l': forall
+ (TARGET: target_cbranch_expanse hrs c l = Some (c', l'))
+ (REF: ris_refines ctx hrs st)
+ (OK: ris_ok ctx hrs),
+ eval_scondition ctx c' l' =
+ eval_scondition ctx c (list_sval_inj (map (si_sreg st) l)).
+Proof.
+ unfold target_cbranch_expanse; simpl.
+ intros H ? ?.
+ congruence.
+Qed.
+Global Opaque target_op_simplify.
+Global Opaque target_cbranch_expanse.
diff --git a/aarch64/CSE2deps.v b/aarch64/CSE2deps.v
new file mode 100644
index 00000000..d5c7ee0f
--- /dev/null
+++ b/aarch64/CSE2deps.v
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs (Int64.unsigned ofs') chunk' (Int64.unsigned ofs) chunk)
+ else true
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/aarch64/CSE2depsproof.v b/aarch64/CSE2depsproof.v
new file mode 100644
index 00000000..653c88f4
--- /dev/null
+++ b/aarch64/CSE2depsproof.v
@@ -0,0 +1,209 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_size :
+ Ptrofs.wordsize = 64%nat.
+Proof.
+ unfold Ptrofs.wordsize.
+ unfold Wordsize_Ptrofs.wordsize.
+ trivial.
+Qed.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = 18446744073709551616.
+Proof.
+ reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : int64.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= Int64.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Int64.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Int64.unsigned ofsw + size_chunk chunkw <= Int64.unsigned ofsr
+ \/ Int64.unsigned ofsr + size_chunk chunkr <= Int64.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+
+ destruct base; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: unfold Ptrofs.of_int64.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.repr (Int64.unsigned ofsr))) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.repr (Int64.unsigned ofsw))) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: repeat rewrite Ptrofs.unsigned_repr by (change Ptrofs.max_unsigned with 18446744073709551615; lia).
+
+ all: try rewrite ptrofs_modulus in *.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs (Int64.unsigned ofsr) chunkr (Int64.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End MEMORY_WRITE.
+
+
+Section STACK_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Ainstack ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Ainstack ofsr) nil = Some addrr.
+
+ Lemma stack_load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+
+ destruct sp; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+
+ all: try rewrite ptrofs_modulus in *.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem stack_load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply stack_load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End STACK_WRITE.
+End SOUNDNESS.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+ - (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ simpl in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs (Int64.unsigned ofs0) chunk' (Int64.unsigned ofs) chunk) eqn:SWAP.
+ 2: discriminate.
+ simpl in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+- (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned ofs0) chunk' (Ptrofs.unsigned ofs) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply stack_load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/aarch64/ConstpropOpproof.v b/aarch64/ConstpropOpproof.v
index 7f5f1e06..24498aa4 100644
--- a/aarch64/ConstpropOpproof.v
+++ b/aarch64/ConstpropOpproof.v
@@ -335,40 +335,63 @@ Qed.
Lemma make_divimm_correct:
forall n r1 r2 v,
- Val.divs e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divs e#r1 e#r2) = v ->
e#r2 = Vint n ->
let (op, args) := make_divimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divimm.
- predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
- destruct (e#r1) eqn:?;
- try (rewrite Val.divs_one in H; exists (Vint i); split; simpl; try rewrite Heqv0; auto);
- inv H; auto.
- destruct (Int.is_power2 n) eqn:?.
- destruct (Int.ltu i (Int.repr 31)) eqn:?.
- exists v; split; auto. simpl. eapply Val.divs_pow2; eauto. congruence.
- exists v; auto.
- exists v; auto.
+ predSpec Int.eq Int.eq_spec n Int.one; intros; subst; rewrite H0.
+ { destruct (e # r1) eqn:Er1.
+ all: try (cbn; exists (e # r1); split; auto; fail).
+ rewrite Val.divs_one.
+ cbn.
+ rewrite Er1.
+ exists (Vint i); split; auto.
+ }
+ destruct (Int.is_power2 n) eqn:Power2.
+ {
+ destruct (Int.ltu i (Int.repr 31)) eqn:iLT31.
+ {
+ cbn.
+ exists (Val.maketotal (Val.shrx e # r1 (Vint i))); split; auto.
+ destruct (Val.divs e # r1 (Vint n)) eqn:DIVS; cbn; auto.
+ rewrite Val.divs_pow2 with (y:=v) (n:=n).
+ cbn.
+ all: auto.
+ }
+ exists (Val.maketotal (Val.divs e # r1 (Vint n))); split; cbn; auto; congruence.
+ }
+ exists (Val.maketotal (Val.divs e # r1 (Vint n))); split; cbn; auto; congruence.
Qed.
+
Lemma make_divuimm_correct:
forall n r1 r2 v,
- Val.divu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divu e#r1 e#r2) = v ->
e#r2 = Vint n ->
let (op, args) := make_divuimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divuimm.
- predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
- destruct (e#r1) eqn:?;
- try (rewrite Val.divu_one in H; exists (Vint i); split; simpl; try rewrite Heqv0; auto);
- inv H; auto.
- destruct (Int.is_power2 n) eqn:?.
- econstructor; split. simpl; eauto.
- rewrite mk_amount32_eq by (eapply Int.is_power2_range; eauto).
- rewrite H0 in H. erewrite Val.divu_pow2 by eauto. auto.
- exists v; auto.
+ predSpec Int.eq Int.eq_spec n Int.one; intros; subst; rewrite H0.
+ { destruct (e # r1) eqn:Er1.
+ all: try (cbn; exists (e # r1); split; auto; fail).
+ rewrite Val.divu_one.
+ cbn.
+ rewrite Er1.
+ exists (Vint i); split; auto.
+ }
+ destruct (Int.is_power2 n) eqn:Power2.
+ {
+ cbn.
+ rewrite mk_amount32_eq by (eapply Int.is_power2_range; eauto).
+ exists (Val.shru e # r1 (Vint i)); split; auto.
+ destruct (Val.divu e # r1 (Vint n)) eqn:DIVU; cbn; auto.
+ rewrite Val.divu_pow2 with (y:=v) (n:=n).
+ all: auto.
+ }
+ exists (Val.maketotal (Val.divu e # r1 (Vint n))); split; cbn; auto; congruence.
Qed.
Lemma make_andimm_correct:
@@ -503,34 +526,60 @@ Qed.
Lemma make_divlimm_correct:
forall n r1 r2 v,
- Val.divls e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divls e#r1 e#r2) = v ->
e#r2 = Vlong n ->
let (op, args) := make_divlimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divlimm.
- destruct (Int64.is_power2' n) eqn:?. destruct (Int.ltu i (Int.repr 63)) eqn:?.
- rewrite H0 in H. econstructor; split. simpl; eauto. eapply Val.divls_pow2; eauto. auto.
- exists v; auto.
- exists v; auto.
+ destruct (Int64.is_power2' n) eqn:Power2.
+ {
+ destruct (Int.ltu i (Int.repr 63)) eqn:iLT63.
+ {
+ cbn.
+ exists (Val.maketotal (Val.shrxl e # r1 (Vint i))); split; auto.
+ rewrite H0 in H.
+ destruct (Val.divls e # r1 (Vlong n)) eqn:DIVS; cbn in H; auto.
+ {
+ subst v0.
+ rewrite Val.divls_pow2 with (y:=v) (n:=n).
+ cbn.
+ all: auto.
+ }
+ subst. auto.
+ }
+ cbn. subst. rewrite H0.
+ exists (Val.maketotal (Val.divls e # r1 (Vlong n))); split; auto.
+ }
+ cbn. subst. rewrite H0.
+ exists (Val.maketotal (Val.divls e # r1 (Vlong n))); split; auto.
Qed.
+
Lemma make_divluimm_correct:
forall n r1 r2 v,
- Val.divlu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divlu e#r1 e#r2) = v ->
e#r2 = Vlong n ->
let (op, args) := make_divluimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divluimm.
destruct (Int64.is_power2' n) eqn:?.
+ {
econstructor; split. simpl; eauto.
- rewrite mk_amount64_eq by (eapply Int64.is_power2'_range; eauto).
- rewrite H0 in H. destruct (e#r1); inv H. destruct (Int64.eq n Int64.zero); inv H2.
- simpl.
- erewrite Int64.is_power2'_range by eauto.
- erewrite Int64.divu_pow2' by eauto. auto.
- exists v; auto.
+ rewrite H0 in H. destruct (e#r1); inv H.
+ all: cbn; auto.
+ {
+ rewrite mk_amount64_eq by (eapply Int64.is_power2'_range; eauto).
+ destruct (Int64.eq n Int64.zero); cbn; auto.
+ erewrite Int64.is_power2'_range by eauto.
+ erewrite Int64.divu_pow2' by eauto. auto.
+ }
+ }
+ exists v; split; auto.
+ cbn.
+ rewrite H.
+ reflexivity.
Qed.
Lemma make_andlimm_correct:
@@ -679,10 +728,10 @@ Proof.
InvApproxRegs; SimplVM; inv H0. apply make_mulimm_correct; auto.
- (* divs *)
assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
- apply make_divimm_correct; auto.
+ apply make_divimm_correct; auto. congruence.
- (* divu *)
assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
- apply make_divuimm_correct; auto.
+ apply make_divuimm_correct; auto. congruence.
- (* and 1 *)
rewrite Val.and_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andimm_correct; auto.
- (* and 2 *)
@@ -745,10 +794,10 @@ Proof.
InvApproxRegs; SimplVM; inv H0. apply make_mullimm_correct; auto.
- (* divl *)
assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
- apply make_divlimm_correct; auto.
+ apply make_divlimm_correct; auto. congruence.
- (* divlu *)
assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
- apply make_divluimm_correct; auto.
+ apply make_divluimm_correct; auto. congruence.
- (* andl 1 *)
rewrite Val.andl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andlimm_correct; auto.
- (* andl 2 *)
diff --git a/aarch64/DuplicateOpcodeHeuristic.ml b/aarch64/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..3a3b87fc
--- /dev/null
+++ b/aarch64/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccomplimm (c, n) | Ccompluimm (c, n) -> if n == Integers.Int64.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c | Ccompfs c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c | Cnotcompfs c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
+
diff --git a/aarch64/ExpansionOracle.ml b/aarch64/ExpansionOracle.ml
new file mode 100644
index 00000000..afcb29c2
--- /dev/null
+++ b/aarch64/ExpansionOracle.ml
@@ -0,0 +1,15 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+let expanse n ibf btl = btl
+
+let find_last_reg c = ()
diff --git a/aarch64/Machregs.v b/aarch64/Machregs.v
index b2a2308e..bfe23e83 100644
--- a/aarch64/Machregs.v
+++ b/aarch64/Machregs.v
@@ -158,6 +158,7 @@ Definition destroyed_by_builtin (ef: external_function): list mreg :=
match ef with
| EF_memcpy sz al => R15 :: R17 :: R29 :: nil
| EF_inline_asm txt sg clob => destroyed_by_clobber clob
+ | EF_profiling _ _ => R15 :: R17 :: R29 :: nil
| _ => nil
end.
diff --git a/aarch64/Machregsaux.ml b/aarch64/Machregsaux.ml
index 5f3c1a8d..98e461eb 100644
--- a/aarch64/Machregsaux.ml
+++ b/aarch64/Machregsaux.ml
@@ -14,3 +14,11 @@
let is_scratch_register s =
s = "X16" || s = "x16" || s = "X30" || s = "x30"
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong -> 0
+ | AST.Tfloat | AST.Tsingle -> 1
+ | AST.Tany32 | AST.Tany64 -> assert false
+
+(* number of available registers per class *)
+let nr_regs = [| 29; 32 |]
diff --git a/aarch64/Machregsaux.mli b/aarch64/Machregsaux.mli
new file mode 100644
index 00000000..23ac1c9a
--- /dev/null
+++ b/aarch64/Machregsaux.mli
@@ -0,0 +1,20 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Auxiliary functions on machine registers *)
+
+val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
+
+(* Number of registers in each class *)
+val nr_regs : int array
diff --git a/aarch64/Op.v b/aarch64/Op.v
index f8d2510e..4c0dfb72 100644
--- a/aarch64/Op.v
+++ b/aarch64/Op.v
@@ -386,8 +386,8 @@ Definition eval_operation
| Omul, v1 :: v2 :: nil => Some (Val.mul v1 v2)
| Omuladd, v1 :: v2 :: v3 :: nil => Some (Val.add v1 (Val.mul v2 v3))
| Omulsub, v1 :: v2 :: v3 :: nil => Some (Val.sub v1 (Val.mul v2 v3))
- | Odiv, v1 :: v2 :: nil => Val.divs v1 v2
- | Odivu, v1 :: v2 :: nil => Val.divu v1 v2
+ | Odiv, v1 :: v2 :: nil => Some (Val.maketotal (Val.divs v1 v2))
+ | Odivu, v1 :: v2 :: nil => Some (Val.maketotal (Val.divu v1 v2))
| Oand, v1 :: v2 :: nil => Some (Val.and v1 v2)
| Oandshift s a, v1 :: v2 :: nil => Some (Val.and v1 (eval_shift s v2 a))
| Oandimm n, v1 :: nil => Some (Val.and v1 (Vint n))
@@ -408,7 +408,7 @@ Definition eval_operation
| Oshl, v1 :: v2 :: nil => Some (Val.shl v1 v2)
| Oshr, v1 :: v2 :: nil => Some (Val.shr v1 v2)
| Oshru, v1 :: v2 :: nil => Some (Val.shru v1 v2)
- | Oshrximm n, v1::nil => Val.shrx v1 (Vint n)
+ | Oshrximm n, v1::nil => Some (Val.maketotal (Val.shrx v1 (Vint n)))
| Ozext s, v1 :: nil => Some (Val.zero_ext s v1)
| Osext s, v1 :: nil => Some (Val.sign_ext s v1)
| Oshlzext s a, v1 :: nil => Some (Val.shl (Val.zero_ext s v1) (Vint a))
@@ -435,8 +435,8 @@ Definition eval_operation
| Omullsub, v1 :: v2 :: v3 :: nil => Some (Val.subl v1 (Val.mull v2 v3))
| Omullhs, v1::v2::nil => Some (Val.mullhs v1 v2)
| Omullhu, v1::v2::nil => Some (Val.mullhu v1 v2)
- | Odivl, v1 :: v2 :: nil => Val.divls v1 v2
- | Odivlu, v1 :: v2 :: nil => Val.divlu v1 v2
+ | Odivl, v1 :: v2 :: nil => Some (Val.maketotal (Val.divls v1 v2))
+ | Odivlu, v1 :: v2 :: nil => Some (Val.maketotal (Val.divlu v1 v2))
| Oandl, v1 :: v2 :: nil => Some (Val.andl v1 v2)
| Oandlshift s a, v1 :: v2 :: nil => Some (Val.andl v1 (eval_shiftl s v2 a))
| Oandlimm n, v1 :: nil => Some (Val.andl v1 (Vlong n))
@@ -457,7 +457,7 @@ Definition eval_operation
| Oshll, v1 :: v2 :: nil => Some (Val.shll v1 v2)
| Oshrl, v1 :: v2 :: nil => Some (Val.shrl v1 v2)
| Oshrlu, v1 :: v2 :: nil => Some (Val.shrlu v1 v2)
- | Oshrlximm n, v1::nil => Val.shrxl v1 (Vint n)
+ | Oshrlximm n, v1::nil => Some (Val.maketotal (Val.shrxl v1 (Vint n)))
| Ozextl s, v1 :: nil => Some (Val.zero_ext_l s v1)
| Osextl s, v1 :: nil => Some (Val.sign_ext_l s v1)
| Oshllzext s a, v1 :: nil => Some (Val.shll (Val.zero_ext_l s v1) (Vint a))
@@ -481,22 +481,22 @@ Definition eval_operation
| Osingleoffloat, v1::nil => Some (Val.singleoffloat v1)
| Ofloatofsingle, v1::nil => Some (Val.floatofsingle v1)
- | Ointoffloat, v1::nil => Val.intoffloat v1
- | Ointuoffloat, v1::nil => Val.intuoffloat v1
- | Ofloatofint, v1::nil => Val.floatofint v1
- | Ofloatofintu, v1::nil => Val.floatofintu v1
- | Ointofsingle, v1::nil => Val.intofsingle v1
- | Ointuofsingle, v1::nil => Val.intuofsingle v1
- | Osingleofint, v1::nil => Val.singleofint v1
- | Osingleofintu, v1::nil => Val.singleofintu v1
- | Olongoffloat, v1::nil => Val.longoffloat v1
- | Olonguoffloat, v1::nil => Val.longuoffloat v1
- | Ofloatoflong, v1::nil => Val.floatoflong v1
- | Ofloatoflongu, v1::nil => Val.floatoflongu v1
- | Olongofsingle, v1::nil => Val.longofsingle v1
- | Olonguofsingle, v1::nil => Val.longuofsingle v1
- | Osingleoflong, v1::nil => Val.singleoflong v1
- | Osingleoflongu, v1::nil => Val.singleoflongu v1
+ | Ointoffloat, v1::nil => Some (Val.maketotal (Val.intoffloat v1))
+ | Ointuoffloat, v1::nil => Some (Val.maketotal (Val.intuoffloat v1))
+ | Ofloatofint, v1::nil => Some (Val.maketotal (Val.floatofint v1))
+ | Ofloatofintu, v1::nil => Some (Val.maketotal (Val.floatofintu v1))
+ | Ointofsingle, v1::nil => Some (Val.maketotal (Val.intofsingle v1))
+ | Ointuofsingle, v1::nil => Some (Val.maketotal (Val.intuofsingle v1))
+ | Osingleofint, v1::nil => Some (Val.maketotal (Val.singleofint v1))
+ | Osingleofintu, v1::nil => Some (Val.maketotal (Val.singleofintu v1))
+ | Olongoffloat, v1::nil => Some (Val.maketotal (Val.longoffloat v1))
+ | Olonguoffloat, v1::nil => Some (Val.maketotal (Val.longuoffloat v1))
+ | Ofloatoflong, v1::nil => Some (Val.maketotal (Val.floatoflong v1))
+ | Ofloatoflongu, v1::nil => Some (Val.maketotal (Val.floatoflongu v1))
+ | Olongofsingle, v1::nil => Some (Val.maketotal (Val.longofsingle v1))
+ | Olonguofsingle, v1::nil => Some (Val.maketotal (Val.longuofsingle v1))
+ | Osingleoflong, v1::nil => Some (Val.maketotal (Val.singleoflong v1))
+ | Osingleoflongu, v1::nil => Some (Val.maketotal (Val.singleoflongu v1))
| Ocmp c, _ => Some (Val.of_optbool (eval_condition c vl m))
| Osel c ty, v1::v2::vl => Some(Val.select (eval_condition c vl m) v1 v2 ty)
@@ -788,10 +788,10 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0... destruct v1...
- apply type_add.
- apply type_sub.
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero); inv H2...
+ - destruct v0; destruct v1; cbn in *; trivial.
+ destruct (_ || _); trivial...
+ - destruct v0; destruct v1; cbn in *; trivial.
+ destruct (Int.eq i0 Int.zero); constructor.
- destruct v0... destruct v1...
- destruct v0... destruct (eval_shift s v1 a)...
- destruct v0...
@@ -812,7 +812,8 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int.iwordsize)...
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int.iwordsize)...
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int.iwordsize)...
- - destruct v0; simpl in H0; try discriminate. destruct (Int.ltu n (Int.repr 31)); inv H0...
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 31)); cbn; trivial.
- destruct v0...
- destruct v0...
- destruct (Val.zero_ext s v0)... simpl; rewrite a32_range...
@@ -843,10 +844,10 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- apply type_subl.
- destruct v0... destruct v1...
- destruct v0... destruct v1...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero); inv H2...
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (_ || _); cbn; trivial.
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero); cbn; trivial.
- destruct v0... destruct v1...
- destruct v0... destruct (eval_shiftl s v1 a)...
- destruct v0...
@@ -867,7 +868,8 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int64.iwordsize')...
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int64.iwordsize')...
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int64.iwordsize')...
- - destruct v0; simpl in H0; try discriminate. destruct (Int.ltu n (Int.repr 63)); inv H0...
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 63)); cbn; trivial.
- destruct v0...
- destruct v0...
- destruct (Val.zero_ext_l s v0)... simpl; rewrite a64_range...
@@ -893,34 +895,60 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0...
- destruct v0...
(* intoffloat, intuoffloat *)
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_int f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_intu f); inv H2...
+ - destruct v0; cbn; trivial. destruct (Float.to_int f); cbn; trivial.
+ - destruct v0; cbn; trivial. destruct (Float.to_intu f); cbn; trivial.
(* floatofint, floatofintu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* intofsingle, intuofsingle *)
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_int f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_intu f); inv H2...
+ - destruct v0; cbn; trivial. destruct (Float32.to_int f); cbn; trivial.
+ - destruct v0; cbn; trivial. destruct (Float32.to_intu f); cbn; trivial.
(* singleofint, singleofintu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* longoffloat, longuoffloat *)
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_long f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_longu f); inv H2...
+ - destruct v0; cbn; trivial. destruct (Float.to_long f); cbn; trivial.
+ - destruct v0; cbn; trivial. destruct (Float.to_longu f); cbn; trivial.
(* floatoflong, floatoflongu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* longofsingle, longuofsingle *)
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_long f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_longu f); inv H2...
+ - destruct v0; cbn; trivial. destruct (Float32.to_long f); cbn; trivial.
+ - destruct v0; cbn; trivial. destruct (Float32.to_longu f); cbn; trivial.
(* singleoflong, singleoflongu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* cmp *)
- destruct (eval_condition cond vl m) as [[]|]...
- unfold Val.select. destruct (eval_condition cond vl m). apply Val.normalize_type. exact I.
Qed.
+
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Omove => false
+ | _ => false
+ end.
+
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op; destruct eq_operation; intros; simpl in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+Qed.
End SOUNDNESS.
(** * Manipulating and transforming operations *)
@@ -1174,6 +1202,28 @@ Proof.
rewrite (cond_depends_on_memory_correct cond args m1 m2 H). auto.
Qed.
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. intro MEM. destruct op eqn:OP; simpl; try congruence.
+ - f_equal; f_equal; auto using cond_valid_pointer_eq.
+ - destruct cond; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
(** Global variables mentioned in an operation or addressing mode *)
Definition globals_addressing (addr: addressing) : list ident :=
@@ -1374,12 +1424,12 @@ Proof.
- apply Val.add_inject; auto. inv H2; inv H3; simpl; auto.
- apply Val.sub_inject; auto. inv H2; inv H3; simpl; auto.
(* div, divu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int.eq i0 Int.zero
- || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int.eq i0 Int.zero); inv H2. TrivialExists.
+ - inv H4; inv H2; trivial. cbn.
+ destruct (_ || _); cbn;
+ constructor.
+ - inv H4; inv H2; trivial. cbn.
+ destruct (Int.eq i0 Int.zero); cbn;
+ constructor.
(* and*)
- inv H4; inv H2; simpl; auto.
- generalize (eval_shift_inject s a H2); intros J; inv H4; inv J; simpl; auto.
@@ -1411,8 +1461,8 @@ Proof.
(* shru *)
- inv H4; inv H2; simpl; auto. destruct (Int.ltu i0 Int.iwordsize); auto.
(* shrx *)
- - inv H4; simpl in H1; try discriminate. simpl.
- destruct (Int.ltu n (Int.repr 31)); inv H1. TrivialExists.
+ - inv H4; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 31)); inv H; cbn; trivial.
(* shift-ext *)
- inv H4; simpl; auto.
- inv H4; simpl; auto.
@@ -1447,12 +1497,10 @@ Proof.
- inv H4; inv H2; simpl; auto.
- inv H4; inv H2; simpl; auto.
(* divl, divlu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int64.eq i0 Int64.zero
- || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int64.eq i0 Int64.zero); inv H2. TrivialExists.
+ - inv H4; inv H2; cbn; trivial.
+ destruct (_ || _); cbn; trivial.
+ - inv H4; inv H2; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero); cbn; trivial.
(* andl *)
- inv H4; inv H2; simpl; auto.
- generalize (eval_shiftl_inject s a H2); intros J; inv H4; inv J; simpl; auto.
@@ -1484,8 +1532,8 @@ Proof.
(* shrlu *)
- inv H4; inv H2; simpl; auto. destruct (Int.ltu i0 Int64.iwordsize'); auto.
(* shrlx *)
- - inv H4; simpl in H1; try discriminate. simpl.
- destruct (Int.ltu n (Int.repr 63)); inv H1. TrivialExists.
+ - inv H4; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 63)); inv H; cbn; trivial.
(* shift-ext *)
- inv H4; simpl; auto.
- inv H4; simpl; auto.
@@ -1516,37 +1564,29 @@ Proof.
- inv H4; simpl; auto.
- inv H4; simpl; auto.
(* intoffloat, intuoffloat *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_int f0); simpl in H2; inv H2.
- exists (Vint i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_intu f0); simpl in H2; inv H2.
- exists (Vint i); auto.
+ - inv H4; cbn; trivial. destruct (Float.to_int f0); cbn; trivial.
+ - inv H4; cbn; trivial. destruct (Float.to_intu f0); cbn; trivial.
(* floatofint, floatofintu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; trivial.
+ - inv H4; cbn; trivial.
(* intofsingle, intuofsingle *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_int f0); simpl in H2; inv H2.
- exists (Vint i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_intu f0); simpl in H2; inv H2.
- exists (Vint i); auto.
+ - inv H4; cbn; trivial. destruct (Float32.to_int f0); cbn; trivial.
+ - inv H4; cbn; trivial. destruct (Float32.to_intu f0); cbn; trivial.
(* singleofint, singleofintu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; trivial.
+ - inv H4; cbn; trivial.
(* longoffloat, longuoffloat *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_long f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_longu f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
+ - inv H4; cbn; trivial. destruct (Float.to_long f0); cbn; trivial.
+ - inv H4; cbn; trivial. destruct (Float.to_longu f0); cbn; trivial.
(* floatoflong, floatoflongu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; trivial.
+ - inv H4; cbn; trivial.
(* longofsingle, longuofsingle *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_long f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_longu f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
+ - inv H4; cbn; trivial. destruct (Float32.to_long f0); cbn; trivial.
+ - inv H4; cbn; trivial. destruct (Float32.to_longu f0); cbn; trivial.
(* singleoflong, singleoflongu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; trivial.
+ - inv H4; cbn; trivial.
(* cmp, sel *)
- subst v1. destruct (eval_condition cond vl1 m1) eqn:?.
exploit eval_condition_inj; eauto. intros EQ; rewrite EQ.
@@ -1576,6 +1616,21 @@ Proof.
- apply Val.offset_ptr_inject; auto.
Qed.
+
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; simpl in *;
+ inv Hinjvl; trivial; try discriminate; inv H0; trivial; try discriminate; inv H2; trivial; try discriminate.
+Qed.
End EVAL_COMPAT.
(** Compatibility of the evaluation functions with the ``is less defined'' relation over values. *)
@@ -1682,6 +1737,18 @@ Proof.
destruct H1 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
Qed.
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros. rewrite val_inject_list_lessdef in H.
+ eapply eval_addressing_inj_none with (sp1 := sp).
+ intros. rewrite <- val_inject_lessdef; auto.
+ rewrite <- val_inject_lessdef; auto.
+ eauto. auto.
+Qed.
End EVAL_LESSDEF.
(** Compatibility of the evaluation functions with memory injections. *)
@@ -1734,6 +1801,19 @@ Proof.
econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
Qed.
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
Lemma eval_operation_inject:
forall op vl1 vl2 v1 m1 m2,
Val.inject_list f vl1 vl2 ->
diff --git a/aarch64/OpWeights.ml b/aarch64/OpWeights.ml
new file mode 100644
index 00000000..5cdd002c
--- /dev/null
+++ b/aarch64/OpWeights.ml
@@ -0,0 +1,353 @@
+open Op;;
+open PrepassSchedulingOracleDeps;;
+
+module Cortex_A53=
+ struct
+ let resource_bounds = [| 2; 2; 1; 1 |];; (* instr ; ALU ; MAC; LSU *)
+ let nr_non_pipelined_units = 1;;
+
+ let latency_of_op (op : operation) (nargs : int) =
+ match op with
+ | Omove
+ | Ointconst _
+ | Olongconst _
+ | Ofloatconst _
+ | Osingleconst _
+ | Oaddrsymbol _
+ | Oaddrstack _ -> 1
+ | Oshift _ -> 2
+ | Oadd -> 1
+ | Oaddshift _ -> 2
+ | Oaddimm _
+ | Oneg -> 1
+ | Onegshift _ -> 2
+ | Osub -> 1
+ | Osubshift _ -> 2
+ | Omul
+ | Omuladd
+ | Omulsub -> 4
+ | Odiv
+ | Odivu -> 29
+ | Oand -> 1
+ | Oandshift _ -> 2
+ | Oandimm _ -> 1
+ | Oor -> 1
+ | Oorshift _ -> 2
+ | Oorimm _ -> 1
+ | Oxor -> 1
+ | Oxorshift _ -> 2
+ | Oxorimm _ -> 1
+ | Onot -> 1
+ | Onotshift _ -> 2
+ | Obic -> 1
+ | Obicshift _ -> 2
+ | Oorn -> 1
+ | Oornshift _ -> 2
+ | Oeqv -> 1
+ | Oeqvshift _ -> 2
+ | Oshl
+ | Oshr
+ | Oshru -> 2
+ | Oshrximm _ -> 6
+ | Ozext _
+ | Osext _ -> 1
+ | Oshlzext _
+ | Oshlsext _
+ | Ozextshr _
+ | Osextshr _ -> 2
+
+ (* 64-bit integer arithmetic *)
+ | Oshiftl _ -> 2
+ | Oextend _ -> 1
+ | Omakelong
+ | Olowlong
+ | Ohighlong
+ | Oaddl -> 1
+ | Oaddlshift _
+ | Oaddlext _ -> 2
+ | Oaddlimm _
+ | Onegl -> 1
+ | Oneglshift _ -> 2
+ | Osubl -> 1
+ | Osublshift _
+ | Osublext _ -> 2
+ | Omull
+ | Omulladd
+ | Omullsub
+ | Omullhs
+ | Omullhu -> 4
+ | Odivl -> 50
+ | Odivlu -> 50
+ | Oandl -> 1
+ | Oandlshift _ -> 2
+ | Oandlimm _
+ | Oorl -> 1
+ | Oorlshift _ -> 2
+ | Oorlimm _
+ | Oxorl -> 1
+ | Oxorlshift _ -> 2
+ | Oxorlimm _
+ | Onotl -> 1
+ | Onotlshift _ -> 2
+ | Obicl -> 1
+ | Obiclshift _ -> 2
+ | Oornl -> 1
+ | Oornlshift _ -> 2
+ | Oeqvl -> 1
+ | Oeqvlshift _ -> 2
+ | Oshll
+ | Oshrl
+ | Oshrlu -> 2
+ | Oshrlximm _ -> 6
+ | Ozextl _
+ | Osextl _ -> 1
+ | Oshllzext _
+ | Oshllsext _
+ | Ozextshrl _
+ | Osextshrl _ -> 2
+
+ (* 64-bit floating-point arithmetic *)
+ | Onegf (* r [rd = - r1] *)
+ | Oabsf (* r [rd = abs(r1)] *)
+ | Oaddf (* r [rd = r1 + r2] *)
+ | Osubf (* r [rd = r1 - r2] *)
+ | Omulf (* r [rd = r1 * r2] *)
+(* 32-bit floating-point arithmetic *)
+ | Onegfs (* r [rd = - r1] *)
+ | Oabsfs (* r [rd = abs(r1)] *)
+ | Oaddfs (* r [rd = r1 + r2] *)
+ | Osubfs (* r [rd = r1 - r2] *)
+ | Omulfs (* r [rd = r1 * r2] *)
+ | Osingleoffloat (* r [rd] is [r1] truncated to single-precision float *)
+ | Ofloatofsingle (* r [rd] is [r1] extended to double-precision float *)
+(* Conversions between int and float *)
+ | Ointoffloat (* r [rd = signed_int_of_float64(r1)] *)
+ | Ointuoffloat (* r [rd = unsigned_int_of_float64(r1)] *)
+ | Ofloatofint (* r [rd = float64_of_signed_int(r1)] *)
+ | Ofloatofintu (* r [rd = float64_of_unsigned_int(r1)] *)
+ | Ointofsingle (* r [rd = signed_int_of_float32(r1)] *)
+ | Ointuofsingle (* r [rd = unsigned_int_of_float32(r1)] *)
+ | Osingleofint (* r [rd = float32_of_signed_int(r1)] *)
+ | Osingleofintu (* r [rd = float32_of_unsigned_int(r1)] *)
+ | Olongoffloat (* r [rd = signed_long_of_float64(r1)] *)
+ | Olonguoffloat (* r [rd = unsigned_long_of_float64(r1)] *)
+ | Ofloatoflong (* r [rd = float64_of_signed_long(r1)] *)
+ | Ofloatoflongu (* r [rd = float64_of_unsigned_long(r1)] *)
+ | Olongofsingle (* r [rd = signed_long_of_float32(r1)] *)
+ | Olonguofsingle (* r [rd = unsigned_long_of_float32(r1)] *)
+ | Osingleoflong (* r [rd = float32_of_signed_long(r1)] *)
+ | Osingleoflongu (* r [rd = float32_of_unsigned_int(r1)] *)
+ -> 6
+ | Odivf -> 50 (* r [rd = r1 / r2] *)
+ | Odivfs -> 20
+ (* Boolean tests *)
+ | Ocmp cmp | Osel (cmp, _) ->
+ (match cmp with
+ | Ccompf _ (* r FP comparison *)
+ | Cnotcompf _ (* r negation of an FP comparison *)
+ | Ccompfzero _ (* r comparison with 0.0 *)
+ | Cnotcompfzero _ (* r negation of comparison with 0.0 *)
+ | Ccompfs _ (* r FP comparison *)
+ | Cnotcompfs _ (* r negation of an FP comparison *)
+ | Ccompfszero _ (* r equal to 0.0 *)
+ | Cnotcompfszero _ (* r not equal to 0.0 *) -> 6
+ | _ -> 1);;
+
+ let resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Omove
+ | Ointconst _
+ | Olongconst _
+ | Ofloatconst _
+ | Osingleconst _
+ | Oaddrsymbol _
+ | Oaddrstack _
+ (* 32-bit integer arithmetic *)
+ | Oshift _
+ | Oadd
+ | Oaddshift _
+ | Oaddimm _
+ | Oneg
+ | Onegshift _
+ | Osub
+ | Osubshift _ -> [| 1 ; 1; 0; 0 |]
+ | Omul
+ | Omuladd
+ | Omulsub -> [| 1; 1; 1; 0 |]
+ | Odiv
+ | Odivu -> [| 1; 0; 0; 0 |]
+ | Oand
+ | Oandshift _
+ | Oandimm _
+ | Oor
+ | Oorshift _
+ | Oorimm _
+ | Oxor
+ | Oxorshift _
+ | Oxorimm _
+ | Onot
+ | Onotshift _
+ | Obic
+ | Obicshift _
+ | Oorn
+ | Oornshift _
+ | Oeqv
+ | Oeqvshift _
+ | Oshl
+ | Oshr
+ | Oshru
+ | Oshrximm _
+ | Ozext _
+ | Osext _
+ | Oshlzext _
+ | Oshlsext _
+ | Ozextshr _
+ | Osextshr _
+
+(* 64-bit integer arithmetic *)
+ | Oshiftl _
+ | Oextend _
+ | Omakelong
+ | Olowlong
+ | Ohighlong
+ | Oaddl
+ | Oaddlshift _
+ | Oaddlext _
+ | Oaddlimm _
+ | Onegl
+ | Oneglshift _
+ | Osubl
+ | Osublshift _
+ | Osublext _ -> [| 1 ; 1 ; 0; 0 |]
+ | Omull
+ | Omulladd
+ | Omullsub
+ | Omullhs
+ | Omullhu -> [| 1 ; 1 ; 1; 0 |]
+ | Odivl
+ | Odivlu -> [| 1; 0; 0; 0 |]
+ | Oandl
+ | Oandlshift _
+ | Oandlimm _
+ | Oorl
+ | Oorlshift _
+ | Oorlimm _
+ | Oxorl
+ | Oxorlshift _
+ | Oxorlimm _
+ | Onotl
+ | Onotlshift _
+ | Obicl
+ | Obiclshift _
+ | Oornl
+ | Oornlshift _
+ | Oeqvl
+ | Oeqvlshift _
+ | Oshll
+ | Oshrl
+ | Oshrlu
+ | Oshrlximm _
+ | Ozextl _
+ | Osextl _
+ | Oshllzext _
+ | Oshllsext _
+ | Ozextshrl _
+ | Osextshrl _ -> [| 1; 1; 0; 0 |]
+ (* 64-bit floating-point arithmetic *)
+ | Onegf (* r [rd = - r1] *)
+ | Oabsf (* r [rd = abs(r1)] *)
+ | Oaddf (* r [rd = r1 + r2] *)
+ | Osubf (* r [rd = r1 - r2] *)
+ | Omulf (* r [rd = r1 * r2] *)
+ | Odivf
+ (* 32-bit floating-point arithmetic *)
+ | Onegfs (* r [rd = - r1] *)
+ | Oabsfs (* r [rd = abs(r1)] *)
+ | Oaddfs (* r [rd = r1 + r2] *)
+ | Osubfs (* r [rd = r1 - r2] *)
+ | Omulfs (* r [rd = r1 * r2] *)
+ | Odivfs (* r [rd = r1 / r2] *)
+ | Osingleoffloat (* r [rd] is [r1] truncated to single-precision float *)
+ | Ofloatofsingle (* r [rd] is [r1] extended to double-precision float *)
+(* Conversions between int and float *)
+ | Ointoffloat (* r [rd = signed_int_of_float64(r1)] *)
+ | Ointuoffloat (* r [rd = unsigned_int_of_float64(r1)] *)
+ | Ofloatofint (* r [rd = float64_of_signed_int(r1)] *)
+ | Ofloatofintu (* r [rd = float64_of_unsigned_int(r1)] *)
+ | Ointofsingle (* r [rd = signed_int_of_float32(r1)] *)
+ | Ointuofsingle (* r [rd = unsigned_int_of_float32(r1)] *)
+ | Osingleofint (* r [rd = float32_of_signed_int(r1)] *)
+ | Osingleofintu (* r [rd = float32_of_unsigned_int(r1)] *)
+ | Olongoffloat (* r [rd = signed_long_of_float64(r1)] *)
+ | Olonguoffloat (* r [rd = unsigned_long_of_float64(r1)] *)
+ | Ofloatoflong (* r [rd = float64_of_signed_long(r1)] *)
+ | Ofloatoflongu (* r [rd = float64_of_unsigned_long(r1)] *)
+ | Olongofsingle (* r [rd = signed_long_of_float32(r1)] *)
+ | Olonguofsingle (* r [rd = unsigned_long_of_float32(r1)] *)
+ | Osingleoflong (* r [rd = float32_of_signed_long(r1)] *)
+ | Osingleoflongu (* r [rd = float32_of_unsigned_int(r1)] *)
+ -> [| 1 ; 1; 1; 0 |]
+
+(* Boolean tests *)
+ | Ocmp cmp | Osel (cmp, _) ->
+ (match cmp with
+ | Ccompf _ (* r FP comparison *)
+ | Cnotcompf _ (* r negation of an FP comparison *)
+ | Ccompfzero _ (* r comparison with 0.0 *)
+ | Cnotcompfzero _ (* r negation of comparison with 0.0 *)
+ | Ccompfs _ (* r FP comparison *)
+ | Cnotcompfs _ (* r negation of an FP comparison *)
+ | Ccompfszero _ (* r equal to 0.0 *)
+ | Cnotcompfszero _ (* r not equal to 0.0 *) ->
+ [| 1; 1; 1; 0 |]
+ | _ -> [| 1; 1; 0; 0 |] );;
+
+ let non_pipelined_resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Odiv | Odivu -> [| 29 |]
+ | Odivfs -> [| 20 |]
+ | Odivl | Odivlu | Odivf -> [| 50 |]
+ | _ -> [| -1 |];;
+
+ let resources_of_cond (cmp : condition) (nargs : int) =
+ (match cmp with
+ | Ccompf _ (* r FP comparison *)
+ | Cnotcompf _ (* r negation of an FP comparison *)
+ | Ccompfzero _ (* r comparison with 0.0 *)
+ | Cnotcompfzero _ (* r negation of comparison with 0.0 *)
+ | Ccompfs _ (* r FP comparison *)
+ | Cnotcompfs _ (* r negation of an FP comparison *)
+ | Ccompfszero _ (* r equal to 0.0 *)
+ | Cnotcompfszero _ (* r not equal to 0.0 *) ->
+ [| 1; 1; 1; 0 |]
+ | _ -> [| 1; 1; 0; 0 |] );;
+
+ let latency_of_load trap chunk (addr : addressing) (nargs : int) = 3;;
+ let latency_of_call _ _ = 6;;
+
+ let resources_of_load trap chunk addressing nargs = [| 1; 0; 0; 1 |];;
+
+ let resources_of_store chunk addressing nargs = [| 1; 0; 0; 1 |];;
+
+ let resources_of_call _ _ = resource_bounds;;
+ let resources_of_builtin _ = resource_bounds;;
+ end;;
+
+let get_opweights () : opweights =
+ match !Clflags.option_mtune with
+ | "cortex-a53" | "cortex-a35" | "" ->
+ {
+ pipelined_resource_bounds = Cortex_A53.resource_bounds;
+ nr_non_pipelined_units = Cortex_A53.nr_non_pipelined_units;
+ latency_of_op = Cortex_A53.latency_of_op;
+ resources_of_op = Cortex_A53.resources_of_op;
+ non_pipelined_resources_of_op = Cortex_A53.non_pipelined_resources_of_op;
+ latency_of_load = Cortex_A53.latency_of_load;
+ resources_of_load = Cortex_A53.resources_of_load;
+ resources_of_store = Cortex_A53.resources_of_store;
+ resources_of_cond = Cortex_A53.resources_of_cond;
+ latency_of_call = Cortex_A53.latency_of_call;
+ resources_of_call = Cortex_A53.resources_of_call;
+ resources_of_builtin = Cortex_A53.resources_of_builtin
+ }
+ | xxx -> failwith (Printf.sprintf "unknown -mtune: %s" xxx);;
diff --git a/aarch64/OpWeightsAsm.ml b/aarch64/OpWeightsAsm.ml
new file mode 100644
index 00000000..44c6f16b
--- /dev/null
+++ b/aarch64/OpWeightsAsm.ml
@@ -0,0 +1,165 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Asmblock
+
+(*type called_function = (Registers.reg, AST.ident) Datatypes.sum*)
+
+type instruction = PBasic of Asmblock.basic | PControl of Asmblock.control
+
+type opweights = {
+ pipelined_resource_bounds : int array;
+ nr_non_pipelined_units : int;
+ latency_of_op : instruction -> int -> int;
+ resources_of_op : instruction -> int -> int array;
+ (*non_pipelined_resources_of_op : Op.operation -> int -> int array;*)
+ (*latency_of_load : AST.trapping_mode -> AST.memory_chunk -> Op.addressing -> int -> int;*)
+ (*resources_of_load : AST.trapping_mode -> AST.memory_chunk -> Op.addressing -> int -> int array;*)
+ (*resources_of_store : AST.memory_chunk -> Op.addressing -> int -> int array;*)
+ (*resources_of_cond : Op.condition -> int -> int array;*)
+ (*latency_of_call : AST.signature -> called_function -> int;*)
+ (*resources_of_call : AST.signature -> called_function -> int array;*)
+ (*resources_of_builtin : AST.external_function -> int array*)
+}
+
+module Cortex_A53 = struct
+ let resource_bounds = [| 2; 2; 1; 1 |]
+
+ (* instr ; ALU ; MAC; LSU *)
+ let nr_non_pipelined_units = 1
+
+ let latency_of_op (i : instruction) (nargs : int) =
+ match i with
+ | PBasic (PArith (PArithP (_, _))) -> 1
+ | PBasic (PArith (PArithPP (i', _, _))) -> (
+ match i' with
+ | Psbfiz (_, _, _) | Psbfx (_, _, _) | Pubfiz (_, _, _) | Pubfx (_, _, _)
+ ->
+ 2
+ | Pfcvtds | Pfcvtsd
+ | Pfcvtzs (_, _)
+ | Pfcvtzu (_, _)
+ | Pfabs _ | Pfneg _
+ | Pscvtf (_, _)
+ | Pucvtf (_, _) ->
+ 6
+ | _ -> 1)
+ | PBasic (PArith (PArithPPP (i', _, _, _))) -> (
+ match i' with
+ | Pasrv _ | Plslv _ | Plsrv _ | Prorv _ | Paddext _ | Psubext _ -> 2
+ | Psmulh | Pumulh -> 4
+ | Pfdiv _ | Psdiv _ | Pudiv _ -> 50
+ | _ -> 6)
+ | PBasic (PArith (PArithRR0R (_, _, _, _))) -> 2
+ | PBasic (PArith (PArithRR0 (_, _, _))) -> 1
+ | PBasic (PArith (PArithARRRR0 (_, _, _, _, _))) -> 4
+ | PBasic (PArith (PArithComparisonPP (_, _, _))) -> 6
+ | PBasic (PArith (PArithComparisonR0R (_, _, _))) -> 1
+ | PBasic (PArith (PArithComparisonP (i', _))) -> (
+ match i' with Pfcmp0 _ -> 6 | _ -> 1)
+ | PBasic (PArith (Pcset (_, _))) | PBasic (PArith (Pcsel (_, _, _, _))) -> 6
+ | PBasic (PArith _) -> 1
+ | PBasic (PLoad _) -> 3
+ | PBasic (PStore _) -> 3
+ | PBasic (Pallocframe (_, _)) -> 3
+ | PBasic (Pfreeframe (_, _)) -> 1
+ | PBasic (Ploadsymbol (_, _)) -> 1
+ | PBasic (Pcvtsw2x (_, _)) -> 2
+ | PBasic (Pcvtuw2x (_, _)) -> 2
+ | PBasic (Pcvtx2w _) -> 1
+ | PBasic (Pnop) -> 0
+ | PControl _ -> 6
+
+ let resources_of_op (i : instruction) (nargs : int) =
+ match i with
+ | PBasic (PArith (PArithP (i', _))) -> (
+ match i' with
+ | Pfmovimmd _ | Pfmovimms _ -> [| 1; 1; 0; 1 |]
+ | _ -> [| 1; 1; 0; 0 |])
+ | PBasic (PArith (PArithPP (i', _, _))) -> (
+ match i' with
+ | Pfcvtds | Pfcvtsd
+ | Pfcvtzs (_, _)
+ | Pfcvtzu (_, _)
+ | Pfabs _ | Pfneg _
+ | Pscvtf (_, _)
+ | Pucvtf (_, _) ->
+ [| 1 ; 1; 1; 0 |]
+ | _ -> [| 1; 1; 0; 0 |])
+ | PBasic (PArith (PArithPPP (i', _, _, _))) -> (
+ match i' with
+ | Pasrv _ | Plslv _ | Plsrv _ | Prorv _ | Paddext _ | Psubext _ -> [| 1; 1; 0; 0 |]
+ | Pfdiv _ | Psdiv _ | Pudiv _ -> [| 1; 0; 0; 0 |]
+ | _ -> [| 1; 1; 1; 0 |])
+ | PBasic (PArith (PArithRR0R (_, _, _, _))) -> [| 1; 1; 0; 0 |]
+ | PBasic (PArith (PArithRR0 (_, _, _))) -> [| 1; 1; 0; 0 |]
+ | PBasic (PArith (PArithARRRR0 (_, _, _, _, _))) -> [| 1; 1; 1; 0 |]
+ | PBasic (PArith (PArithComparisonPP (_, _, _))) -> [| 1; 1; 1; 0 |]
+ | PBasic (PArith (PArithComparisonR0R (_, _, _))) -> [| 1; 1; 0; 0 |]
+ | PBasic (PArith (PArithComparisonP (i', _))) -> (
+ match i' with Pfcmp0 _ -> [| 1; 1; 1; 0 |] | _ -> [| 1; 1; 0; 0 |])
+ | PBasic (PArith (Pcset (_, _))) | PBasic (PArith (Pcsel (_, _, _, _))) -> [| 1; 1; 1; 0 |]
+ | PBasic (PArith _) -> [| 1; 1; 0; 0 |]
+ | PBasic (PLoad _) -> [| 1; 0; 0; 1 |]
+ | PBasic (PStore _) -> [| 1; 0; 0; 1 |]
+ | PBasic (Pallocframe (_, _)) -> [| 1; 0; 0; 1 |]
+ | PBasic (Pfreeframe (_, _)) -> [| 1; 1; 0; 0 |]
+ | PBasic (Ploadsymbol (_, _)) -> [| 1; 1; 0; 0 |]
+ | PBasic (Pcvtsw2x (_, _)) -> [| 1; 1; 0; 0 |]
+ | PBasic (Pcvtuw2x (_, _)) -> [| 1; 1; 0; 0 |]
+ | PBasic (Pcvtx2w _) -> [| 1; 1; 0; 0 |]
+ | PBasic (Pnop) -> [| 0; 0; 0; 0 |]
+ | PControl _ -> resource_bounds
+
+ (*let non_pipelined_resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Odiv | Odivu -> [| 29 |]
+ | Odivfs -> [| 20 |]
+ | Odivl | Odivlu | Odivf -> [| 50 |]
+ | _ -> [| -1 |];;
+
+ let resources_of_cond (cmp : condition) (nargs : int) =
+ (match cmp with
+ | Ccompf _ (* r FP comparison *)
+ | Cnotcompf _ (* r negation of an FP comparison *)
+ | Ccompfzero _ (* r comparison with 0.0 *)
+ | Cnotcompfzero _ (* r negation of comparison with 0.0 *)
+ | Ccompfs _ (* r FP comparison *)
+ | Cnotcompfs _ (* r negation of an FP comparison *)
+ | Ccompfszero _ (* r equal to 0.0 *)
+ | Cnotcompfszero _ (* r not equal to 0.0 *) ->
+ [| 1; 1; 1; 0 |]
+ | _ -> [| 1; 1; 0; 0 |] )*)
+
+end
+
+let get_opweights () : opweights =
+ (*match !Clflags.option_mtune with*)
+ (*| "cortex-a53" | "cortex-a35" | "" ->*)
+ {
+ pipelined_resource_bounds = Cortex_A53.resource_bounds;
+ nr_non_pipelined_units = Cortex_A53.nr_non_pipelined_units;
+ latency_of_op = Cortex_A53.latency_of_op;
+ resources_of_op =
+ Cortex_A53.resources_of_op
+ (*non_pipelined_resources_of_op = Cortex_A53.non_pipelined_resources_of_op;*)
+ (*latency_of_load = Cortex_A53.latency_of_load;*)
+ (*resources_of_load = Cortex_A53.resources_of_load;*)
+ (*resources_of_store = Cortex_A53.resources_of_store;*)
+ (*resources_of_cond = Cortex_A53.resources_of_cond;*)
+ (*latency_of_call = Cortex_A53.latency_of_call;*)
+ (*resources_of_call = Cortex_A53.resources_of_call;*)
+ (*resources_of_builtin = Cortex_A53.resources_of_builtin*);
+ }
+
+(*| xxx -> failwith (Printf.sprintf "unknown -mtune: %s" xxx);;*)
diff --git a/aarch64/PeepholeOracle.ml b/aarch64/PeepholeOracle.ml
new file mode 100644
index 00000000..1d890f2c
--- /dev/null
+++ b/aarch64/PeepholeOracle.ml
@@ -0,0 +1,624 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Camlcoq
+open Asmblock
+open Asm
+open Int64
+open Printf
+
+(* If true, the oracle will print a msg for each applied peephole *)
+let debug = false
+
+(* Functions to verify the immediate offset range for ldp/stp *)
+let is_valid_immofs_32 z =
+ if z <= 252 && z >= -256 && z mod 4 = 0 then true else false
+
+let is_valid_immofs_64 z =
+ if z <= 504 && z >= -512 && z mod 8 = 0 then true else false
+
+(* Functions to check if a ldp/stp replacement is valid according to args *)
+let is_valid_ldr32 rd1 rd2 b1 b2 n1 n2 =
+ let z1 = to_int (camlint64_of_coqint n1) in
+ let z2 = to_int (camlint64_of_coqint n2) in
+ if
+ (not (dreg_eq rd1 rd2))
+ && iregsp_eq b1 b2
+ && (not (dreg_eq rd1 (IR b2)))
+ && (z2 = z1 + 4 || z2 = z1 - 4)
+ && is_valid_immofs_32 z1
+ then true
+ else false
+
+let is_valid_ldr64 rd1 rd2 b1 b2 n1 n2 =
+ let z1 = to_int (camlint64_of_coqint n1) in
+ let z2 = to_int (camlint64_of_coqint n2) in
+ if
+ (not (dreg_eq rd1 rd2))
+ && iregsp_eq b1 b2
+ && (not (dreg_eq rd1 (IR b2)))
+ && (z2 = z1 + 8 || z2 = z1 - 8)
+ && is_valid_immofs_64 z1
+ then true
+ else false
+
+let is_valid_str32 b1 b2 n1 n2 =
+ let z1 = to_int (camlint64_of_coqint n1) in
+ let z2 = to_int (camlint64_of_coqint n2) in
+ if iregsp_eq b1 b2 && z2 = z1 + 4 && is_valid_immofs_32 z1 then true
+ else false
+
+let is_valid_str64 b1 b2 n1 n2 =
+ let z1 = to_int (camlint64_of_coqint n1) in
+ let z2 = to_int (camlint64_of_coqint n2) in
+ if iregsp_eq b1 b2 && z2 = z1 + 8 && is_valid_immofs_64 z1 then true
+ else false
+
+let dreg_of_ireg r = IR (RR1 r)
+
+let dreg_of_freg r = FR r
+
+(* Return true if an intermediate
+ * affectation eliminates the potential
+ * candidate *)
+let verify_load_affect reg rd b rev =
+ let b = IR b in
+ if not rev then dreg_eq reg b else dreg_eq reg b || dreg_eq reg rd
+
+(* Return true if an intermediate
+ * read eliminates the potential
+ * candidate *)
+let verify_load_read reg rd b rev = dreg_eq reg rd
+
+(* Return true if an intermediate
+ * affectation eliminates the potential
+ * candidate *)
+let verify_store_affect reg rs b rev =
+ let b = IR b in
+ dreg_eq reg b || dreg_eq reg rs
+
+type ph_type = P32 | P32f | P64 | P64f
+
+type inst_type = Ldr of ph_type | Str of ph_type
+
+let ph_ty_to_string = function
+ | Ldr P32 -> "ldr32"
+ | Ldr P32f -> "ldr32f"
+ | Ldr P64 -> "ldr64"
+ | Ldr P64f -> "ldr64f"
+ | Str P32 -> "str32"
+ | Str P32f -> "str32f"
+ | Str P64 -> "str64"
+ | Str P64f -> "str64f"
+
+let print_ph_ty chan v = output_string chan (ph_ty_to_string v)
+
+let symb_mem = Hashtbl.create 9
+
+(* Affect a symbolic memory list of potential replacements
+ * for a given write in reg *)
+let rec affect_symb_mem reg insta pot_rep stype rev =
+ match pot_rep with
+ | [] -> []
+ | h0 :: t0 -> (
+ match (insta.(h0), stype) with
+ | PLoad (PLd_rd_a (_, rd, ADimm (b, n))), Ldr _ ->
+ if verify_load_affect reg rd b rev then
+ affect_symb_mem reg insta t0 stype rev
+ else h0 :: affect_symb_mem reg insta t0 stype rev
+ | PStore (PSt_rs_a (_, rs, ADimm (b, n))), Str _ ->
+ if verify_store_affect reg rs b rev then
+ affect_symb_mem reg insta t0 stype rev
+ else h0 :: affect_symb_mem reg insta t0 stype rev
+ | _, _ ->
+ failwith "affect_symb_mem: Found an inconsistent inst in pot_rep")
+
+(* Affect a symbolic memory list of potential replacements
+ * for a given read in reg *)
+let rec read_symb_mem reg insta pot_rep stype rev =
+ match pot_rep with
+ | [] -> []
+ | h0 :: t0 -> (
+ match (insta.(h0), stype) with
+ | PLoad (PLd_rd_a (_, rd, ADimm (b, n))), Ldr _ ->
+ if verify_load_read reg rd b rev then
+ read_symb_mem reg insta t0 stype rev
+ else h0 :: read_symb_mem reg insta t0 stype rev
+ | PStore (PSt_rs_a (_, rs, ADimm (b, n))), Str _ ->
+ h0 :: read_symb_mem reg insta t0 stype rev
+ | _, _ -> failwith "read_symb_mem: Found an inconsistent inst in pot_rep")
+
+(* Update a symbolic memory list of potential replacements
+ * for any addressing *)
+let update_pot_rep_addressing a insta pot_rep stype rev =
+ match a with
+ | ADimm (base, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev
+ | ADreg (base, r) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
+ | ADlsl (base, r, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
+ | ADsxt (base, r, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
+ | ADuxt (base, r, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
+ | ADadr (base, _, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev
+ | ADpostincr (base, _) ->
+ pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev
+
+(* Update a symbolic memory list of potential replacements
+ * for any basic instruction *)
+let update_pot_rep_basic inst insta stype rev =
+ let pot_rep = Hashtbl.find symb_mem stype in
+ (match inst with
+ | PArith i -> (
+ match i with
+ | PArithP (_, rd) ->
+ pot_rep := affect_symb_mem rd insta !pot_rep stype rev
+ | PArithPP (_, rd, rs) ->
+ pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs insta !pot_rep stype rev
+ | PArithPPP (_, rd, rs1, rs2) ->
+ pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
+ | PArithRR0R (_, rd, rs1, rs2) ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ (match rs1 with
+ | RR0 rs1 ->
+ pot_rep :=
+ read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
+ | _ -> ());
+ pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev
+ | PArithRR0 (_, rd, rs) -> (
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ match rs with
+ | RR0 rs1 ->
+ pot_rep :=
+ read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
+ | _ -> ())
+ | PArithARRRR0 (_, rd, rs1, rs2, rs3) -> (
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev;
+ match rs3 with
+ | RR0 rs1 ->
+ pot_rep :=
+ read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
+ | _ -> ())
+ | PArithComparisonPP (_, rs1, rs2) ->
+ pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
+ | PArithComparisonR0R (_, rs1, rs2) ->
+ (match rs1 with
+ | RR0 rs1 ->
+ pot_rep :=
+ read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
+ | _ -> ());
+ pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev
+ | PArithComparisonP (_, rs1) ->
+ pot_rep := read_symb_mem rs1 insta !pot_rep stype rev
+ | Pcset (rd, _) ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
+ | Pfmovi (_, rd, rs) -> (
+ pot_rep := affect_symb_mem (dreg_of_freg rd) insta !pot_rep stype rev;
+ match rs with
+ | RR0 rs ->
+ pot_rep :=
+ read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
+ | _ -> ())
+ | Pcsel (rd, rs1, rs2, _) ->
+ pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
+ pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
+ | Pfnmul (_, rd, rs1, rs2) ->
+ pot_rep := affect_symb_mem (dreg_of_freg rd) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_freg rs1) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_freg rs2) insta !pot_rep stype rev)
+ | PLoad i -> (
+ (* Here, we consider a different behavior for load and store potential candidates:
+ * a load does not obviously cancel the ldp candidates, but it does for any stp candidate. *)
+ match stype with
+ | Ldr _ -> (
+ match i with
+ | PLd_rd_a (_, rd, a) ->
+ pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
+ update_pot_rep_addressing a insta pot_rep stype rev
+ | Pldp (_, rd1, rd2, _, _, a) ->
+ pot_rep := affect_symb_mem rd1 insta !pot_rep stype rev;
+ pot_rep := affect_symb_mem rd2 insta !pot_rep stype rev;
+ update_pot_rep_addressing a insta pot_rep stype rev)
+ | _ -> pot_rep := [])
+ | PStore _ -> (
+ (* Here, we consider that a store cancel all ldp candidates, but it is far more complicated for stp ones :
+ * if we cancel stp candidates here, we would prevent ourselves to apply the non-consec store peephole.
+ * To solve this issue, the store candidates cleaning is managed directly in the peephole function below. *)
+ match stype with Ldr _ -> pot_rep := [] | _ -> ())
+ | Pallocframe (_, _) -> pot_rep := []
+ | Pfreeframe (_, _) -> pot_rep := []
+ | Ploadsymbol (rd, _) ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
+ | Pcvtsw2x (rd, rs) ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
+ | Pcvtuw2x (rd, rs) ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
+ | Pcvtx2w rd ->
+ pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
+ pot_rep := read_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
+ | Pnop -> ());
+ Hashtbl.replace symb_mem stype pot_rep
+
+(* This is useful to manage the case were the immofs
+ * of the first ldr/str is greater than the second one *)
+let min_is_rev n1 n2 =
+ let z1 = to_int (camlint64_of_coqint n1) in
+ let z2 = to_int (camlint64_of_coqint n2) in
+ if z1 < z2 then true else false
+
+(* Below functions were added to merge pattern matching cases in peephole,
+ * thanks to this, we can make the chunk difference (int/any) compatible. *)
+let trans_ldi (ldi : load_rd_a) : load_rd1_rd2_a =
+ match ldi with
+ | Pldrw | Pldrw_a -> Pldpw
+ | Pldrx | Pldrx_a -> Pldpx
+ | Pldrs -> Pldps
+ | Pldrd | Pldrd_a -> Pldpd
+ | _ -> failwith "trans_ldi: Found a non compatible load to translate"
+
+let trans_sti (sti : store_rs_a) : store_rs1_rs2_a =
+ match sti with
+ | Pstrw | Pstrw_a -> Pstpw
+ | Pstrx | Pstrx_a -> Pstpx
+ | Pstrs -> Pstps
+ | Pstrd | Pstrd_a -> Pstpd
+ | _ -> failwith "trans_sti: Found a non compatible store to translate"
+
+let is_compat_load (ldi : load_rd_a) =
+ match ldi with
+ | Pldrw | Pldrw_a | Pldrx | Pldrx_a | Pldrs | Pldrd | Pldrd_a -> true
+ | _ -> false
+
+let are_compat_load (ldi1 : load_rd_a) (ldi2 : load_rd_a) =
+ match ldi1 with
+ | Pldrw | Pldrw_a -> ( match ldi2 with Pldrw | Pldrw_a -> true | _ -> false)
+ | Pldrx | Pldrx_a -> ( match ldi2 with Pldrx | Pldrx_a -> true | _ -> false)
+ | Pldrs -> ( match ldi2 with Pldrs -> true | _ -> false)
+ | Pldrd | Pldrd_a -> ( match ldi2 with Pldrd | Pldrd_a -> true | _ -> false)
+ | _ -> false
+
+let is_compat_store (sti : store_rs_a) =
+ match sti with
+ | Pstrw | Pstrw_a | Pstrx | Pstrx_a | Pstrs | Pstrd | Pstrd_a -> true
+ | _ -> false
+
+let are_compat_store (sti1 : store_rs_a) (sti2 : store_rs_a) =
+ match sti1 with
+ | Pstrw | Pstrw_a -> ( match sti2 with Pstrw | Pstrw_a -> true | _ -> false)
+ | Pstrx | Pstrx_a -> ( match sti2 with Pstrx | Pstrx_a -> true | _ -> false)
+ | Pstrs -> ( match sti2 with Pstrs -> true | _ -> false)
+ | Pstrd | Pstrd_a -> ( match sti2 with Pstrd | Pstrd_a -> true | _ -> false)
+ | _ -> false
+
+let get_load_pht (ldi : load_rd_a) =
+ match ldi with
+ | Pldrw | Pldrw_a -> Ldr P32
+ | Pldrs -> Ldr P32f
+ | Pldrx | Pldrx_a -> Ldr P64
+ | Pldrd | Pldrd_a -> Ldr P64f
+ | _ -> failwith "get_load_string: Found a non compatible load to translate"
+
+let get_store_pht (sti : store_rs_a) =
+ match sti with
+ | Pstrw | Pstrw_a -> Str P32
+ | Pstrs -> Str P32f
+ | Pstrx | Pstrx_a -> Str P64
+ | Pstrd | Pstrd_a -> Str P64f
+ | _ -> failwith "get_store_string: Found a non compatible store to translate"
+
+let is_valid_ldr rd1 rd2 b1 b2 n1 n2 stype =
+ match stype with
+ | Ldr P32 | Ldr P32f -> is_valid_ldr32 rd1 rd2 b1 b2 n1 n2
+ | _ -> is_valid_ldr64 rd1 rd2 b1 b2 n1 n2
+
+let is_valid_str b1 b2 n1 n2 stype =
+ match stype with
+ | Str P32 | Str P32f -> is_valid_str32 b1 b2 n1 n2
+ | _ -> is_valid_str64 b1 b2 n1 n2
+
+(* Try to find the index of the first previous compatible
+ * replacement in a given symbolic memory *)
+let rec search_compat_rep r2 b2 n2 insta pot_rep stype =
+ match pot_rep with
+ | [] -> None
+ | h0 :: t0 -> (
+ match insta.(h0) with
+ | PLoad (PLd_rd_a (ld1, rd1, ADimm (b1, n1))) ->
+ if is_valid_ldr rd1 r2 b1 b2 n1 n2 stype then
+ Some (h0, chunk_load ld1, rd1, b1, n1)
+ else search_compat_rep r2 b2 n2 insta t0 stype
+ | PStore (PSt_rs_a (st1, rs1, ADimm (b1, n1))) ->
+ if is_valid_str b1 b2 n1 n2 stype then
+ Some (h0, chunk_store st1, rs1, b1, n1)
+ else search_compat_rep r2 b2 n2 insta t0 stype
+ | _ -> failwith "search_compat_rep: Found an inconsistent inst in pot_rep"
+ )
+
+(* Try to find the index of the first previous compatible
+ * replacement in a given symbolic memory (when iterating in the reversed list) *)
+let rec search_compat_rep_inv r2 b2 n2 insta pot_rep stype =
+ match pot_rep with
+ | [] -> None
+ | h0 :: t0 -> (
+ match insta.(h0) with
+ | PLoad (PLd_rd_a (ld1, rd1, ADimm (b1, n1))) ->
+ if is_valid_ldr r2 rd1 b2 b1 n2 n1 stype then
+ Some (h0, chunk_load ld1, rd1, b1, n1)
+ else search_compat_rep_inv r2 b2 n2 insta t0 stype
+ | PStore (PSt_rs_a (st1, rs1, ADimm (b1, n1))) ->
+ if is_valid_str b2 b1 n2 n1 stype then
+ Some (h0, chunk_store st1, rs1, b1, n1)
+ else search_compat_rep_inv r2 b2 n2 insta t0 stype
+ | _ ->
+ failwith
+ "search_compat_rep_ldst_inv: Found an inconsistent inst in pot_rep")
+
+let init_symb_mem () =
+ Hashtbl.clear symb_mem;
+ Hashtbl.add symb_mem (Ldr P32) (ref []);
+ Hashtbl.add symb_mem (Ldr P64) (ref []);
+ Hashtbl.add symb_mem (Ldr P32f) (ref []);
+ Hashtbl.add symb_mem (Ldr P64f) (ref []);
+ Hashtbl.add symb_mem (Str P32) (ref []);
+ Hashtbl.add symb_mem (Str P64) (ref []);
+ Hashtbl.add symb_mem (Str P32f) (ref []);
+ Hashtbl.add symb_mem (Str P64f) (ref [])
+
+let reset_str_symb_mem () =
+ Hashtbl.replace symb_mem (Str P32) (ref []);
+ Hashtbl.replace symb_mem (Str P64) (ref []);
+ Hashtbl.replace symb_mem (Str P32f) (ref []);
+ Hashtbl.replace symb_mem (Str P64f) (ref [])
+
+(* Main peephole function in backward style *)
+let pair_rep_inv insta =
+ (* Each list below is a symbolic mem representation
+ * for one type of inst. Lists contains integers which
+ * are the indices of insts in the main array "insta". *)
+ init_symb_mem ();
+ for i = Array.length insta - 1 downto 0 do
+ let h0 = insta.(i) in
+ (* Here we need to update every symbolic memory according to the matched inst *)
+ update_pot_rep_basic h0 insta (Ldr P32) true;
+ update_pot_rep_basic h0 insta (Ldr P64) true;
+ update_pot_rep_basic h0 insta (Ldr P32f) true;
+ update_pot_rep_basic h0 insta (Ldr P64f) true;
+ update_pot_rep_basic h0 insta (Str P32) true;
+ update_pot_rep_basic h0 insta (Str P64) true;
+ update_pot_rep_basic h0 insta (Str P32f) true;
+ update_pot_rep_basic h0 insta (Str P64f) true;
+ match h0 with
+ (* Non-consecutive ldr *)
+ | PLoad (PLd_rd_a (ldi, rd1, ADimm (b1, n1))) ->
+ if is_compat_load ldi then (
+ (* Search a previous compatible load *)
+ let ld_t = get_load_pht ldi in
+ let pot_rep = Hashtbl.find symb_mem ld_t in
+ (match search_compat_rep_inv rd1 b1 n1 insta !pot_rep ld_t with
+ (* If we can't find a candidate, add the current load as a potential future one *)
+ | None -> pot_rep := i :: !pot_rep
+ (* Else, perform the peephole *)
+ | Some (rep, c, r, b, n) ->
+ (* The two lines below are used to filter the elected candidate *)
+ let filt x = x != rep in
+ pot_rep := List.filter filt !pot_rep;
+ insta.(rep) <- Pnop;
+ if min_is_rev n n1 then (
+ if debug then
+ eprintf "LDP_BACK_SPACED_PEEP_IMM_INC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ (trans_ldi ldi, r, rd1, c, chunk_load ldi, ADimm (b, n))))
+ else (
+ if debug then
+ eprintf "LDP_BACK_SPACED_PEEP_IMM_DEC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ (trans_ldi ldi, rd1, r, chunk_load ldi, c, ADimm (b, n1)))));
+ Hashtbl.replace symb_mem ld_t pot_rep)
+ (* Non-consecutive str *)
+ | PStore (PSt_rs_a (sti, rd1, ADimm (b1, n1))) ->
+ if is_compat_store sti then (
+ (* Search a previous compatible store *)
+ let st_t = get_store_pht sti in
+ let pot_rep = Hashtbl.find symb_mem st_t in
+ (match search_compat_rep_inv rd1 b1 n1 insta !pot_rep st_t with
+ (* If we can't find a candidate, clean and add the current store as a potential future one *)
+ | None ->
+ reset_str_symb_mem ();
+ pot_rep := [ i ]
+ (* Else, perform the peephole *)
+ | Some (rep, c, r, b, n) ->
+ (* The two lines below are used to filter the elected candidate *)
+ let filt x = x != rep in
+ pot_rep := List.filter filt !pot_rep;
+ insta.(rep) <- Pnop;
+ if debug then
+ eprintf "STP_BACK_SPACED_PEEP_IMM_INC_%a\n" print_ph_ty st_t;
+ insta.(i) <-
+ PStore
+ (Pstp
+ (trans_sti sti, rd1, r, chunk_store sti, c, ADimm (b, n1))));
+ Hashtbl.replace symb_mem st_t pot_rep
+ (* Any other inst *))
+ else reset_str_symb_mem ()
+ | i -> (
+ (* Clear list of candidates if there is a non supported store *)
+ match i with PStore _ -> reset_str_symb_mem () | _ -> ())
+ done
+
+(* Main peephole function in forward style *)
+let pair_rep insta =
+ (* Each list below is a symbolic mem representation
+ * for one type of inst. Lists contains integers which
+ * are the indices of insts in the main array "insta". *)
+ init_symb_mem ();
+ for i = 0 to Array.length insta - 2 do
+ let h0 = insta.(i) in
+ let h1 = insta.(i + 1) in
+ (* Here we need to update every symbolic memory according to the matched inst *)
+ update_pot_rep_basic h0 insta (Ldr P32) false;
+ update_pot_rep_basic h0 insta (Ldr P64) false;
+ update_pot_rep_basic h0 insta (Ldr P32f) false;
+ update_pot_rep_basic h0 insta (Ldr P64f) false;
+ update_pot_rep_basic h0 insta (Str P32) false;
+ update_pot_rep_basic h0 insta (Str P64) false;
+ update_pot_rep_basic h0 insta (Str P32f) false;
+ update_pot_rep_basic h0 insta (Str P64f) false;
+ match (h0, h1) with
+ (* Consecutive ldr *)
+ | ( PLoad (PLd_rd_a (ldi1, rd1, ADimm (b1, n1))),
+ PLoad (PLd_rd_a (ldi2, rd2, ADimm (b2, n2))) ) ->
+ if are_compat_load ldi1 ldi2 then
+ let ld_t = get_load_pht ldi1 in
+ if is_valid_ldr rd1 rd2 b1 b2 n1 n2 ld_t then (
+ if min_is_rev n1 n2 then (
+ if debug then
+ eprintf "LDP_CONSEC_PEEP_IMM_INC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ ( trans_ldi ldi1,
+ rd1,
+ rd2,
+ chunk_load ldi1,
+ chunk_load ldi2,
+ ADimm (b1, n1) )))
+ else (
+ if debug then
+ eprintf "LDP_CONSEC_PEEP_IMM_DEC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ ( trans_ldi ldi1,
+ rd2,
+ rd1,
+ chunk_load ldi2,
+ chunk_load ldi1,
+ ADimm (b1, n2) )));
+ insta.(i + 1) <- Pnop)
+ (* Non-consecutive ldr *)
+ | PLoad (PLd_rd_a (ldi, rd1, ADimm (b1, n1))), _ ->
+ if is_compat_load ldi then (
+ (* Search a previous compatible load *)
+ let ld_t = get_load_pht ldi in
+ let pot_rep = Hashtbl.find symb_mem ld_t in
+ (match search_compat_rep rd1 b1 n1 insta !pot_rep ld_t with
+ (* If we can't find a candidate, add the current load as a potential future one *)
+ | None -> pot_rep := i :: !pot_rep
+ (* Else, perform the peephole *)
+ | Some (rep, c, r, b, n) ->
+ (* The two lines below are used to filter the elected candidate *)
+ let filt x = x != rep in
+ pot_rep := List.filter filt !pot_rep;
+ insta.(rep) <- Pnop;
+ if min_is_rev n n1 then (
+ if debug then
+ eprintf "LDP_FORW_SPACED_PEEP_IMM_INC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ (trans_ldi ldi, r, rd1, c, chunk_load ldi, ADimm (b, n))))
+ else (
+ if debug then
+ eprintf "LDP_FORW_SPACED_PEEP_IMM_DEC_%a\n" print_ph_ty ld_t;
+ insta.(i) <-
+ PLoad
+ (Pldp
+ (trans_ldi ldi, rd1, r, chunk_load ldi, c, ADimm (b, n1)))));
+ Hashtbl.replace symb_mem ld_t pot_rep)
+ (* Consecutive str *)
+ | ( PStore (PSt_rs_a (sti1, rd1, ADimm (b1, n1))),
+ PStore (PSt_rs_a (sti2, rd2, ADimm (b2, n2))) ) ->
+ (* Regardless of whether we can perform the peephole or not,
+ * we have to clean the potential candidates for stp now as we are
+ * looking at two new store instructions. *)
+ reset_str_symb_mem ();
+ if are_compat_store sti1 sti2 then
+ let st_t = get_store_pht sti1 in
+ if is_valid_str b1 b2 n1 n2 st_t then (
+ if debug then
+ eprintf "STP_CONSEC_PEEP_IMM_INC_%a\n" print_ph_ty st_t;
+ insta.(i) <-
+ PStore
+ (Pstp
+ ( trans_sti sti1,
+ rd1,
+ rd2,
+ chunk_store sti1,
+ chunk_store sti2,
+ ADimm (b1, n1) ));
+ insta.(i + 1) <- Pnop)
+ (* Non-consecutive str *)
+ | PStore (PSt_rs_a (sti, rd1, ADimm (b1, n1))), _ ->
+ if is_compat_store sti then (
+ (* Search a previous compatible store *)
+ let st_t = get_store_pht sti in
+ let pot_rep = Hashtbl.find symb_mem st_t in
+ (match search_compat_rep rd1 b1 n1 insta !pot_rep st_t with
+ (* If we can't find a candidate, clean and add the current store as a potential future one *)
+ | None ->
+ reset_str_symb_mem ();
+ pot_rep := [ i ]
+ (* Else, perform the peephole *)
+ | Some (rep, c, r, b, n) ->
+ (* The two lines below are used to filter the elected candidate *)
+ let filt x = x != rep in
+ pot_rep := List.filter filt !pot_rep;
+ insta.(rep) <- Pnop;
+ if debug then
+ eprintf "STP_FORW_SPACED_PEEP_IMM_INC_%a\n" print_ph_ty st_t;
+ insta.(i) <-
+ PStore
+ (Pstp (trans_sti sti, r, rd1, c, chunk_store sti, ADimm (b, n))));
+ Hashtbl.replace symb_mem st_t pot_rep)
+ else reset_str_symb_mem ()
+ (* Any other inst *)
+ | i, _ -> (
+ (* Clear list of candidates if there is a non supported store *)
+ match i with PStore _ -> reset_str_symb_mem () | _ -> ())
+ done
+
+(* Calling peephole if flag is set *)
+let optimize_bdy (bdy : basic list) : basic list =
+ if !Clflags.option_fcoalesce_mem then (
+ let insta = Array.of_list bdy in
+ pair_rep insta;
+ pair_rep_inv insta;
+ Array.to_list insta)
+ else bdy
+
+(* Called peephole function from Coq *)
+let peephole_opt bdy =
+ Timing.time_coq
+ [
+ 'P'; 'e'; 'e'; 'p'; 'h'; 'o'; 'l'; 'e'; ' '; 'o'; 'r'; 'a'; 'c'; 'l'; 'e';
+ ]
+ optimize_bdy bdy
diff --git a/aarch64/PostpassScheduling.v b/aarch64/PostpassScheduling.v
new file mode 100644
index 00000000..f826632b
--- /dev/null
+++ b/aarch64/PostpassScheduling.v
@@ -0,0 +1,146 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Implementation (and basic properties) of the verified postpass scheduler *)
+
+Require Import Coqlib Errors AST Integers.
+Require Import Asmblock Axioms Memory Globalenvs.
+Require Import Asmblockdeps Asmblockprops.
+Require Import IterList.
+
+Local Open Scope error_monad_scope.
+
+(** * Oracle taking as input a basic block,
+ returns a scheduled basic block *)
+Axiom schedule: bblock -> (list basic) * option control.
+
+Axiom peephole_opt: (list basic) -> list basic.
+
+Extract Constant schedule => "PostpassSchedulingOracle.schedule".
+
+Extract Constant peephole_opt => "PeepholeOracle.peephole_opt".
+
+Section verify_schedule.
+
+Variable lk: aarch64_linker.
+
+Definition verify_schedule (bb bb' : bblock) : res unit :=
+ match bblock_simub bb bb' with
+ | true => OK tt
+ | false => Error (msg "PostpassScheduling.verify_schedule")
+ end.
+
+Definition verify_size bb bb' := if (Z.eqb (size bb) (size bb')) then OK tt else Error (msg "PostpassScheduling:verify_size: wrong size").
+
+Lemma verify_size_size:
+ forall bb bb', verify_size bb bb' = OK tt -> size bb = size bb'.
+Proof.
+ intros. unfold verify_size in H. destruct (size bb =? size bb') eqn:SIZE; try discriminate.
+ apply Z.eqb_eq. assumption.
+Qed.
+
+Program Definition make_bblock_from_basics lb :=
+ match lb with
+ | nil => Error (msg "PostpassScheduling.make_bblock_from_basics")
+ | b :: lb => OK {| header := nil; body := b::lb; exit := None |}
+ end.
+
+Program Definition schedule_to_bblock (lb: list basic) (oc: option control) : res bblock :=
+ match oc with
+ | None => make_bblock_from_basics lb
+ | Some c => OK {| header := nil; body := lb; exit := Some c |}
+ end.
+Next Obligation.
+ unfold Is_true, non_empty_bblockb.
+ unfold non_empty_exit. rewrite orb_true_r. reflexivity.
+Qed.
+
+Definition do_schedule (bb: bblock) : res bblock :=
+ if (Z.eqb (size bb) 1) then OK (bb)
+ else match (schedule bb) with (lb, oc) => schedule_to_bblock lb oc end.
+
+(*Definition do_peephole (bb: bblock) : bblock :=*)
+ (*let res := Peephole.optimize_bblock bb in*)
+ (*if (size res =? size bb) then res else bb.*)
+
+Program Definition do_peephole (bb : bblock) :=
+ let optimized := peephole_opt (body bb) in
+ let wf_ok := non_empty_bblockb optimized (exit bb) in
+ {| header := header bb;
+ body := if wf_ok then optimized else (body bb);
+ exit := exit bb |}.
+Next Obligation.
+ destruct (non_empty_bblockb (peephole_opt (body bb))) eqn:Rwf.
+ - rewrite Rwf. cbn. trivial.
+ - exact (correct bb).
+Qed.
+
+Definition verified_schedule (bb : bblock) : res bblock :=
+ let nhbb := no_header bb in
+ let phbb := do_peephole nhbb in
+ do schbb <- do_schedule phbb;
+ let bb' := stick_header (header bb) schbb in
+ do sizecheck <- verify_size bb bb';
+ do schedcheck <- verify_schedule bb bb';
+ OK (bb').
+
+Lemma verified_schedule_size:
+ forall bb bb', verified_schedule bb = OK bb' -> size bb = size bb'.
+Proof.
+ intros. unfold verified_schedule in H.
+ monadInv H.
+ unfold verify_size in EQ1.
+ destruct (size _ =? size _) eqn:ESIZE_H in EQ1; try discriminate.
+ rewrite Z.eqb_eq in ESIZE_H; rewrite ESIZE_H; reflexivity.
+Qed.
+
+Theorem verified_schedule_correct:
+ forall ge f bb bb',
+ verified_schedule bb = OK bb' ->
+ bblock_simu lk ge f bb bb'.
+Proof.
+ intros.
+ monadInv H.
+ eapply bblock_simub_correct.
+ unfold verify_schedule in EQ0.
+ destruct (bblock_simub _ _) in *; try discriminate; auto.
+Qed.
+
+End verify_schedule.
+
+Fixpoint transf_blocks (lbb : list bblock) : res (list bblock) :=
+ match lbb with
+ | nil => OK nil
+ | bb :: lbb =>
+ do tlbb <- transf_blocks lbb;
+ do tbb <- verified_schedule bb;
+ OK (tbb :: tlbb)
+ end.
+
+Definition transl_function (f: function) : res function :=
+ do lb <- transf_blocks (fn_blocks f);
+ OK (mkfunction (fn_sig f) lb).
+
+Definition transf_function (f: function) : res function :=
+ do tf <- transl_function f;
+ if zlt Ptrofs.max_unsigned (size_blocks tf.(fn_blocks))
+ then Error (msg "code size exceeded")
+ else OK tf.
+
+Definition transf_fundef (f: fundef) : res fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program transf_fundef p.
diff --git a/aarch64/PostpassSchedulingOracle.ml b/aarch64/PostpassSchedulingOracle.ml
new file mode 100644
index 00000000..6f784238
--- /dev/null
+++ b/aarch64/PostpassSchedulingOracle.ml
@@ -0,0 +1,674 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Asmblock
+open OpWeightsAsm
+open InstructionScheduler
+
+let debug = false
+
+let stats = false
+
+(**
+ * Extracting infos from Asm instructions
+ *)
+
+type location = Reg of Asm.preg | Mem | IREG0_XZR
+
+type ab_inst_rec = {
+ inst : instruction;
+ write_locs : location list;
+ read_locs : location list;
+ is_control : bool;
+}
+
+(** Asm constructor to real instructions *)
+
+exception OpaqueInstruction
+
+let is_XZR = function IREG0_XZR -> true | _ -> false
+
+let reg_of_pc = Reg Asm.PC
+
+let reg_of_dreg r = Reg (Asm.DR r)
+
+let reg_of_ireg r = Reg (Asm.DR (Asm.IR (Asm.RR1 r)))
+
+let reg_of_iregsp r = Reg (Asm.DR (Asm.IR r))
+
+let reg_of_ireg0 r =
+ match r with Asm.RR0 ir -> reg_of_ireg ir | Asm.XZR -> IREG0_XZR
+
+let reg_of_freg r = Reg (Asm.DR (Asm.FR r))
+
+let reg_of_cr r = Reg (Asm.CR r)
+
+let regXSP = Reg (Asm.DR (Asm.IR Asm.XSP))
+
+let flags_wlocs =
+ [ reg_of_cr Asm.CN; reg_of_cr Asm.CZ; reg_of_cr Asm.CC; reg_of_cr Asm.CV ]
+
+let get_arith_p_wlocs = function
+ | Pfmovimms _ -> [ reg_of_ireg Asm.X16 ]
+ | Pfmovimmd _ -> [ reg_of_ireg Asm.X16 ]
+ | _ -> []
+
+let arith_p_rec i i' rd =
+ {
+ inst = i;
+ write_locs = [ rd ] @ get_arith_p_wlocs i';
+ read_locs = [];
+ is_control = false;
+ }
+
+let arith_pp_rec i rd rs =
+ { inst = i; write_locs = [ rd ]; read_locs = [ rs ]; is_control = false }
+
+let arith_ppp_rec i rd r1 r2 =
+ { inst = i; write_locs = [ rd ]; read_locs = [ r1; r2 ]; is_control = false }
+
+let arith_rr0r_rec i rd r1 r2 =
+ let rlocs = if is_XZR r1 then [ r2 ] else [ r1; r2 ] in
+ { inst = i; write_locs = [ rd ]; read_locs = rlocs; is_control = false }
+
+let arith_rr0_rec i rd r1 =
+ let rlocs = if is_XZR r1 then [] else [ r1 ] in
+ { inst = i; write_locs = [ rd ]; read_locs = rlocs; is_control = false }
+
+let arith_arrrr0_rec i rd r1 r2 r3 =
+ let rlocs = if is_XZR r3 then [ r1; r2 ] else [ r1; r2; r3 ] in
+ { inst = i; write_locs = [ rd ]; read_locs = rlocs; is_control = false }
+
+let arith_comparison_pp_rec i r1 r2 =
+ {
+ inst = i;
+ write_locs = flags_wlocs;
+ read_locs = [ r1; r2 ];
+ is_control = false;
+ }
+
+let arith_comparison_r0r_rec i r1 r2 =
+ let rlocs = if is_XZR r1 then [ r2 ] else [ r1; r2 ] in
+ { inst = i; write_locs = flags_wlocs; read_locs = rlocs; is_control = false }
+
+let arith_comparison_p_rec i r1 =
+ { inst = i; write_locs = flags_wlocs; read_locs = [ r1 ]; is_control = false }
+
+let get_eval_addressing_rlocs a =
+ match a with
+ | Asm.ADimm (base, _) -> [ reg_of_iregsp base ]
+ | Asm.ADreg (base, r) -> [ reg_of_iregsp base; reg_of_ireg r ]
+ | Asm.ADlsl (base, r, _) -> [ reg_of_iregsp base; reg_of_ireg r ]
+ | Asm.ADsxt (base, r, _) -> [ reg_of_iregsp base; reg_of_ireg r ]
+ | Asm.ADuxt (base, r, _) -> [ reg_of_iregsp base; reg_of_ireg r ]
+ | Asm.ADadr (base, _, _) -> [ reg_of_iregsp base ]
+ | Asm.ADpostincr (base, _) -> [ reg_of_iregsp base ]
+
+let load_rd_a_rec ld rd a =
+ {
+ inst = ld;
+ write_locs = [ rd ];
+ read_locs = [ Mem ] @ get_eval_addressing_rlocs a;
+ is_control = false;
+ }
+
+let load_rd1_rd2_a_rec ld rd1 rd2 a =
+ {
+ inst = ld;
+ write_locs = [ rd1; rd2 ];
+ read_locs = [ Mem ] @ get_eval_addressing_rlocs a;
+ is_control = false;
+ }
+
+let load_rec ldi =
+ match ldi with
+ | PLd_rd_a (ld, rd, a) ->
+ load_rd_a_rec (PBasic (PLoad ldi)) (reg_of_dreg rd) a
+ | Pldp (ld, rd1, rd2, _, _, a) ->
+ load_rd1_rd2_a_rec (PBasic (PLoad ldi)) (reg_of_dreg rd1)
+ (reg_of_dreg rd2) a
+
+let store_rs_a_rec st rs a =
+ {
+ inst = st;
+ write_locs = [ Mem ];
+ read_locs = [ rs; Mem ] @ get_eval_addressing_rlocs a;
+ is_control = false;
+ }
+
+let store_rs1_rs2_a_rec st rs1 rs2 a =
+ {
+ inst = st;
+ write_locs = [ Mem ];
+ read_locs = [ rs1; rs2; Mem ] @ get_eval_addressing_rlocs a;
+ is_control = false;
+ }
+
+let store_rec sti =
+ match sti with
+ | PSt_rs_a (st, rs, a) ->
+ store_rs_a_rec (PBasic (PStore sti)) (reg_of_dreg rs) a
+ | Pstp (st, rs1, rs2, _, _, a) ->
+ store_rs1_rs2_a_rec (PBasic (PStore sti)) (reg_of_dreg rs1)
+ (reg_of_dreg rs2) a
+
+let loadsymbol_rec i rd id =
+ { inst = i; write_locs = [ rd ]; read_locs = [ Mem ]; is_control = false }
+
+let cvtsw2x_rec i rd r1 =
+ { inst = i; write_locs = [ rd ]; read_locs = [ r1 ]; is_control = false }
+
+let cvtuw2x_rec i rd r1 =
+ { inst = i; write_locs = [ rd ]; read_locs = [ r1 ]; is_control = false }
+
+let cvtx2w_rec i rd =
+ { inst = i; write_locs = [ rd ]; read_locs = [ rd ]; is_control = false }
+
+let get_testcond_rlocs c =
+ match c with
+ | Asm.TCeq -> [ reg_of_cr Asm.CZ ]
+ | Asm.TCne -> [ reg_of_cr Asm.CZ ]
+ | Asm.TChs -> [ reg_of_cr Asm.CC ]
+ | Asm.TClo -> [ reg_of_cr Asm.CC ]
+ | Asm.TCmi -> [ reg_of_cr Asm.CN ]
+ | Asm.TCpl -> [ reg_of_cr Asm.CN ]
+ | Asm.TChi -> [ reg_of_cr Asm.CZ; reg_of_cr Asm.CC ]
+ | Asm.TCls -> [ reg_of_cr Asm.CZ; reg_of_cr Asm.CC ]
+ | Asm.TCge -> [ reg_of_cr Asm.CN; reg_of_cr Asm.CV ]
+ | Asm.TClt -> [ reg_of_cr Asm.CN; reg_of_cr Asm.CV ]
+ | Asm.TCgt -> [ reg_of_cr Asm.CN; reg_of_cr Asm.CZ; reg_of_cr Asm.CV ]
+ | Asm.TCle -> [ reg_of_cr Asm.CN; reg_of_cr Asm.CZ; reg_of_cr Asm.CV ]
+
+let cset_rec i rd c =
+ {
+ inst = i;
+ write_locs = [ rd ];
+ read_locs = get_testcond_rlocs c;
+ is_control = false;
+ }
+
+let csel_rec i rd r1 r2 c =
+ {
+ inst = i;
+ write_locs = [ rd ];
+ read_locs = [ r1; r2 ] @ get_testcond_rlocs c;
+ is_control = false;
+ }
+
+let fmovi_rec i fsz rd r1 =
+ let rlocs = if is_XZR r1 then [] else [ r1 ] in
+ { inst = i; write_locs = [ rd ]; read_locs = rlocs; is_control = false }
+
+let fnmul_rec i fsz rd r1 r2 =
+ { inst = i; write_locs = [ rd ]; read_locs = [ r1; r2 ]; is_control = false }
+
+let allocframe_rec i sz linkofs =
+ {
+ inst = i;
+ write_locs = [ Mem; regXSP; reg_of_ireg Asm.X16; reg_of_ireg Asm.X29 ];
+ read_locs = [ regXSP; Mem ];
+ is_control = false;
+ }
+
+let freeframe_rec i sz linkofs =
+ {
+ inst = i;
+ write_locs = [ Mem; regXSP; reg_of_ireg Asm.X16 ];
+ read_locs = [ regXSP; Mem ];
+ is_control = false;
+ }
+
+let nop_rec i =
+ { inst = i; write_locs = []; read_locs = []; is_control = false }
+
+let arith_rec i =
+ match i with
+ | PArithP (i', rd) -> arith_p_rec (PBasic (PArith i)) i' (reg_of_dreg rd)
+ | PArithPP (i', rd, rs) ->
+ arith_pp_rec (PBasic (PArith i)) (reg_of_dreg rd) (reg_of_dreg rs)
+ | PArithPPP (i', rd, r1, r2) ->
+ arith_ppp_rec (PBasic (PArith i)) (reg_of_dreg rd) (reg_of_dreg r1)
+ (reg_of_dreg r2)
+ | PArithRR0R (i', rd, r1, r2) ->
+ arith_rr0r_rec (PBasic (PArith i)) (reg_of_ireg rd) (reg_of_ireg0 r1)
+ (reg_of_ireg r2)
+ | PArithRR0 (i', rd, r1) ->
+ arith_rr0_rec (PBasic (PArith i)) (reg_of_ireg rd) (reg_of_ireg0 r1)
+ | PArithARRRR0 (i', rd, r1, r2, r3) ->
+ arith_arrrr0_rec (PBasic (PArith i)) (reg_of_ireg rd) (reg_of_ireg r1)
+ (reg_of_ireg r2) (reg_of_ireg0 r3)
+ | PArithComparisonPP (i', r1, r2) ->
+ arith_comparison_pp_rec (PBasic (PArith i)) (reg_of_dreg r1)
+ (reg_of_dreg r2)
+ | PArithComparisonR0R (i', r1, r2) ->
+ arith_comparison_r0r_rec (PBasic (PArith i)) (reg_of_ireg0 r1)
+ (reg_of_ireg r2)
+ | PArithComparisonP (i', r1) ->
+ arith_comparison_p_rec (PBasic (PArith i)) (reg_of_dreg r1)
+ | Pcset (rd, c) -> cset_rec (PBasic (PArith i)) (reg_of_ireg rd) c
+ | Pfmovi (fsz, rd, r1) ->
+ fmovi_rec (PBasic (PArith i)) fsz (reg_of_freg rd) (reg_of_ireg0 r1)
+ | Pcsel (rd, r1, r2, c) ->
+ csel_rec (PBasic (PArith i)) (reg_of_dreg rd) (reg_of_dreg r1)
+ (reg_of_dreg r2) c
+ | Pfnmul (fsz, rd, r1, r2) ->
+ fnmul_rec (PBasic (PArith i)) fsz (reg_of_freg rd) (reg_of_freg r1)
+ (reg_of_freg r2)
+
+let basic_rec i =
+ match i with
+ | PArith i' -> arith_rec i'
+ | PLoad ld -> load_rec ld
+ | PStore st -> store_rec st
+ | Pallocframe (sz, linkofs) -> allocframe_rec (PBasic i) sz linkofs
+ | Pfreeframe (sz, linkofs) -> freeframe_rec (PBasic i) sz linkofs
+ | Ploadsymbol (rd, id) -> loadsymbol_rec (PBasic i) (reg_of_ireg rd) id
+ | Pcvtsw2x (rd, r1) ->
+ cvtsw2x_rec (PBasic i) (reg_of_ireg rd) (reg_of_ireg r1)
+ | Pcvtuw2x (rd, r1) ->
+ cvtuw2x_rec (PBasic i) (reg_of_ireg rd) (reg_of_ireg r1)
+ | Pcvtx2w rd -> cvtx2w_rec (PBasic i) (reg_of_ireg rd)
+ | Pnop -> nop_rec (PBasic i)
+
+let builtin_rec i ef args res =
+ { inst = i; write_locs = [ Mem ]; read_locs = [ Mem ]; is_control = true }
+
+let ctl_flow_rec i =
+ match i with
+ | Pb lbl ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_pc ];
+ is_control = true;
+ }
+ | Pbc (c, lbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_pc ];
+ is_control = true;
+ }
+ | Pbl (id, sg) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_ireg Asm.X30; reg_of_pc ];
+ read_locs = [ reg_of_pc ];
+ is_control = true;
+ }
+ | Pbs (id, sg) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [];
+ is_control = true;
+ }
+ | Pblr (r, sg) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_ireg Asm.X30; reg_of_pc ];
+ read_locs = [ reg_of_ireg r ];
+ is_control = true;
+ }
+ | Pbr (r, sg) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r ];
+ is_control = true;
+ }
+ | Pret r ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r ];
+ is_control = true;
+ }
+ | Pcbnz (sz, r, lbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r; reg_of_pc ];
+ is_control = true;
+ }
+ | Pcbz (sz, r, lbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r; reg_of_pc ];
+ is_control = true;
+ }
+ | Ptbnz (sz, r, n, lbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r; reg_of_pc ];
+ is_control = true;
+ }
+ | Ptbz (sz, r, n, lbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_pc ];
+ read_locs = [ reg_of_ireg r; reg_of_pc ];
+ is_control = true;
+ }
+ | Pbtbl (r1, tbl) ->
+ {
+ inst = PControl (PCtlFlow i);
+ write_locs = [ reg_of_ireg Asm.X16; reg_of_ireg Asm.X17; reg_of_pc ];
+ read_locs = [ reg_of_ireg r1; reg_of_pc ];
+ is_control = true;
+ }
+
+let control_rec i =
+ match i with
+ | Pbuiltin (ef, args, res) -> builtin_rec (PControl i) ef args res
+ | PCtlFlow i' -> ctl_flow_rec i'
+
+let rec basic_recs body =
+ match body with [] -> [] | bi :: body -> basic_rec bi :: basic_recs body
+
+let exit_rec exit = match exit with None -> [] | Some ex -> [ control_rec ex ]
+
+let instruction_recs bb = basic_recs bb.body @ exit_rec bb.exit
+
+(**
+ * Providing informations relative to the real instructions
+ *)
+
+type inst_info = {
+ write_locs : location list;
+ read_locs : location list;
+ is_control : bool;
+ (* resources consumed by the instruction *)
+ usage : int array;
+ latency : int;
+}
+
+(** Abstraction providing all the necessary informations for solving the scheduling problem *)
+
+let rec_to_info (r : ab_inst_rec) : inst_info =
+ let opweights = OpWeightsAsm.get_opweights () in
+ let usage = opweights.resources_of_op r.inst 0
+ and latency = opweights.latency_of_op r.inst 0 in
+ {
+ write_locs = r.write_locs;
+ read_locs = r.read_locs;
+ usage;
+ latency;
+ is_control = r.is_control;
+ }
+
+let instruction_infos bb = List.map rec_to_info (instruction_recs bb)
+
+let instruction_usages bb =
+ let usages = List.map (fun info -> info.usage) (instruction_infos bb) in
+ Array.of_list usages
+
+(**
+ * Latency constraints building
+ *)
+
+module LocHash = Hashtbl
+
+(* Hash table : location => list of instruction ids *)
+
+let rec intlist n =
+ if n < 0 then failwith "intlist: n < 0"
+ else if n = 0 then []
+ else (n - 1) :: intlist (n - 1)
+
+let find_in_hash hashloc loc =
+ match LocHash.find_opt hashloc loc with Some idl -> idl | None -> []
+
+(* Returns a list of instruction ids *)
+let rec get_accesses hashloc (ll : location list) =
+ match ll with
+ | [] -> []
+ | loc :: llocs -> find_in_hash hashloc loc @ get_accesses hashloc llocs
+
+let compute_latency (ifrom : inst_info) = ifrom.latency
+
+let latency_constraints bb =
+ let written = LocHash.create 70
+ and read = LocHash.create 70
+ and count = ref 0
+ and constraints = ref []
+ and instr_infos = instruction_infos bb in
+ let step (i : inst_info) =
+ let raw = get_accesses written i.read_locs
+ and waw = get_accesses written i.write_locs
+ and war = get_accesses read i.write_locs in
+ List.iter
+ (fun i ->
+ constraints :=
+ {
+ instr_from = i;
+ instr_to = !count;
+ latency = compute_latency (List.nth instr_infos i);
+ }
+ :: !constraints)
+ raw;
+ List.iter
+ (fun i ->
+ constraints :=
+ {
+ instr_from = i;
+ instr_to = !count;
+ latency = compute_latency (List.nth instr_infos i);
+ }
+ :: !constraints)
+ waw;
+ List.iter
+ (fun i ->
+ constraints :=
+ { instr_from = i; instr_to = !count; latency = 0 } :: !constraints)
+ war;
+ if i.is_control then
+ List.iter
+ (fun n ->
+ constraints :=
+ { instr_from = n; instr_to = !count; latency = 0 } :: !constraints)
+ (intlist !count);
+ (* Updating "read" and "written" hashmaps *)
+ List.iter
+ (fun loc ->
+ LocHash.replace written loc [ !count ];
+ LocHash.replace read loc []
+ (* Clearing all the entries of "read" hashmap when a register is written *))
+ i.write_locs;
+ List.iter
+ (fun loc -> LocHash.replace read loc (!count :: find_in_hash read loc))
+ i.read_locs;
+ count := !count + 1
+ in
+ List.iter step instr_infos;
+ !constraints
+
+(**
+ * Using the InstructionScheduler
+ *)
+
+let opweights = OpWeightsAsm.get_opweights ()
+
+let build_problem bb =
+ {
+ max_latency = -1;
+ resource_bounds = opweights.pipelined_resource_bounds;
+ live_regs_entry = Registers.Regset.empty; (* unused here *)
+ typing = (fun x -> AST.Tint); (* unused here *)
+ reference_counting = None;
+ instruction_usages = instruction_usages bb;
+ latency_constraints = latency_constraints bb;
+ }
+
+let get_from_indexes indexes l = List.map (List.nth l) indexes
+
+(*let is_basic = function PBasic _ -> true | _ -> false*)
+let is_control = function PControl _ -> true | _ -> false
+
+let to_control = function
+ | PControl i -> i
+ | _ -> failwith "to_control: basic instruction found"
+
+let rec body_to_instrs bdy =
+ match bdy with [] -> [] | i :: l' -> PBasic i :: body_to_instrs l'
+
+let rec instrs_to_bdy instrs =
+ match instrs with
+ | [] -> []
+ | PBasic i :: l' -> i :: instrs_to_bdy l'
+ | PControl _ :: l' -> failwith "instrs_to_bdy: control instruction found"
+
+let repack li hd =
+ let last = List.nth li (List.length li - 1) in
+ if is_control last then
+ let cut_li =
+ Array.to_list @@ Array.sub (Array.of_list li) 0 (List.length li - 1)
+ in
+ { header = hd; body = instrs_to_bdy cut_li; exit = Some (to_control last) }
+ else { header = hd; body = instrs_to_bdy li; exit = None }
+
+module TimeHash = Hashtbl
+
+(*Hash table : time => list of instruction ids *)
+
+(* Flattening the minpack result *)
+let hashtbl2flatarray h maxint =
+ let rec f i =
+ match TimeHash.find_opt h i with
+ | None -> if i > maxint then [] else f (i + 1)
+ | Some bund -> bund @ f (i + 1)
+ in
+ f 0
+
+let find_max l =
+ let rec f = function
+ | [] -> None
+ | e :: l -> (
+ match f l with
+ | None -> Some e
+ | Some m -> if e > m then Some e else Some m)
+ in
+ match f l with None -> raise Not_found | Some m -> m
+
+(* We still use the minpack algorithm even without bundles, but the result is then flattened *)
+(*(* [0, 2, 3, 1, 1, 2, 4, 5] -> [[0], [3, 4], [1, 5], [2], [6], [7]] *)*)
+let minpack_list (l : int list) =
+ let timehash = TimeHash.create (List.length l) in
+ let rec f i = function
+ | [] -> ()
+ | t :: l ->
+ (match TimeHash.find_opt timehash t with
+ | None -> TimeHash.add timehash t [ i ]
+ | Some bund -> TimeHash.replace timehash t (bund @ [ i ]));
+ f (i + 1) l
+ in
+ f 0 l;
+ hashtbl2flatarray timehash (find_max l)
+
+let bb_to_instrs bb =
+ body_to_instrs bb.body
+ @ match bb.exit with None -> [] | Some e -> [ PControl e ]
+
+let build_solution bb sol =
+ (* Remove last element - the total *)
+ let tmp = Array.to_list @@ Array.sub sol 0 (Array.length sol - 1) in
+ let pack = minpack_list tmp and instrs = bb_to_instrs bb in
+ repack (get_from_indexes pack instrs) bb.header
+
+let print_schedule sched =
+ print_string "[ ";
+ Array.iter (fun x -> Printf.printf "%d; " x) sched;
+ print_endline "]"
+
+let do_schedule bb =
+ let problem = build_problem bb in
+ if debug then print_problem stdout problem;
+ let solution = scheduler_by_name !Clflags.option_fpostpass_sched problem in
+ match solution with
+ | None -> failwith "Could not find a valid schedule"
+ | Some sol ->
+ if debug then print_schedule sol;
+ build_solution bb sol
+
+(**
+ * Dumb schedule if the above doesn't work
+ *)
+
+(* Pack result *)
+let pack_result (bb : bblock) = (bb.body, bb.exit)
+
+let smart_schedule bb =
+ let bb' =
+ try do_schedule bb with
+ | OpaqueInstruction ->
+ if debug then
+ Printf.eprintf "OpaqueInstruction raised, using identity scheduling\n";
+ bb (* Identity in case of failure *)
+ | e ->
+ let msg = Printexc.to_string e and stack = Printexc.get_backtrace () in
+ Printf.eprintf "Postpass scheduling could not complete: %s\n%s" msg
+ stack;
+ failwith "Invalid schedule"
+ in
+ pack_result bb'
+
+let bblock_schedule bb =
+ let identity_mode = not !Clflags.option_fpostpass in
+ if debug && not identity_mode then (
+ Printf.eprintf "###############################\n";
+ Printf.eprintf "SCHEDULING\n");
+ if stats then (
+ let oc =
+ open_out_gen [ Open_append; Open_creat ] 0o666 "oracle_stats.csv"
+ in
+ Printf.fprintf oc "%d\n" (Camlcoq.Z.to_int (size bb));
+ close_out oc);
+ if identity_mode then pack_result bb else smart_schedule bb
+
+(** Called schedule function from Coq *)
+
+(*let schedule_notime bb = let toto = bblock_schedule in toto*)
+let schedule bb =
+ Timing.time_coq
+ [
+ 'P';
+ 'o';
+ 's';
+ 't';
+ 'p';
+ 'a';
+ 's';
+ 's';
+ 'S';
+ 'c';
+ 'h';
+ 'e';
+ 'd';
+ 'u';
+ 'l';
+ 'i';
+ 'n';
+ 'g';
+ ' ';
+ 'o';
+ 'r';
+ 'a';
+ 'c';
+ 'l';
+ 'e';
+ ]
+ bblock_schedule bb
diff --git a/aarch64/PostpassSchedulingproof.v b/aarch64/PostpassSchedulingproof.v
new file mode 100644
index 00000000..a5084b5f
--- /dev/null
+++ b/aarch64/PostpassSchedulingproof.v
@@ -0,0 +1,482 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Errors.
+Require Import Integers Floats AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations Machblock Conventions Asmblock.
+Require Import Asmblockprops.
+Require Import PostpassScheduling.
+Require Import Asmblockgenproof.
+Require Import Axioms.
+Require Import Lia.
+
+Local Open Scope error_monad_scope.
+
+Definition match_prog (p tp: program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros. eapply match_transform_partial_program; eauto.
+Qed.
+
+Section PRESERVATION_ASMBLOCK.
+
+Variables prog tprog: program.
+Variable lk: aarch64_linker.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_match TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ transf_fundef f = OK tf.
+Proof (Genv.find_funct_ptr_transf_partial TRANSL).
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s1 s2, s1 = s2 -> match_states s1 s2.
+
+Lemma prog_main_preserved:
+ prog_main tprog = prog_main prog.
+Proof (match_program_main TRANSL).
+
+Lemma prog_main_address_preserved:
+ (Genv.symbol_address (Genv.globalenv prog) (prog_main prog) Ptrofs.zero) =
+ (Genv.symbol_address (Genv.globalenv tprog) (prog_main tprog) Ptrofs.zero).
+Proof.
+ unfold Genv.symbol_address. rewrite symbols_preserved.
+ rewrite prog_main_preserved. auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall st1, initial_state prog st1 ->
+ exists st2, initial_state tprog st2 /\ match_states st1 st2.
+Proof.
+ intros. inv H.
+ econstructor; split.
+ - eapply initial_state_intro.
+ eapply (Genv.init_mem_transf_partial TRANSL); eauto.
+ - econstructor; eauto. subst ge0. subst rs0. rewrite prog_main_address_preserved. auto.
+Qed.
+
+Lemma transf_final_states:
+ forall st1 st2 r,
+ match_states st1 st2 -> final_state st1 r -> final_state st2 r.
+Proof.
+ intros. inv H0. inv H. econstructor; eauto.
+Qed.
+
+Lemma transf_find_bblock:
+ forall ofs f bb tf,
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb ->
+ transf_function f = OK tf ->
+ exists tbb,
+ verified_schedule bb = OK tbb
+ /\ find_bblock (Ptrofs.unsigned ofs) (fn_blocks tf) = Some tbb.
+Proof.
+ intros.
+ monadInv H0. destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks x))); try (inv EQ0; fail). inv EQ0.
+ monadInv EQ. simpl in *.
+ generalize (Ptrofs.unsigned ofs) H x EQ0; clear ofs H x g EQ0.
+ induction (fn_blocks f).
+ - intros. simpl in *. discriminate.
+ - intros. simpl in *.
+ monadInv EQ0. simpl.
+ destruct (zlt z 0); try discriminate.
+ destruct (zeq z 0).
+ + inv H. eauto.
+ + monadInv EQ0.
+ exploit IHb; eauto.
+ intros (tbb & SCH & FIND).
+ eexists; split; eauto.
+ inv FIND.
+ unfold verify_size in EQ0.
+ destruct (size a =? size (stick_header (header a) x)) eqn:EQSIZE; try discriminate.
+ rewrite Z.eqb_eq in EQSIZE; rewrite EQSIZE.
+ reflexivity.
+Qed.
+
+Lemma stick_header_neutral: forall a,
+ a = (stick_header (header a) (no_header a)).
+Proof.
+ intros.
+ unfold stick_header. unfold Asmblock.stick_header_obligation_1. simpl. destruct a.
+ simpl. reflexivity.
+Qed.
+
+Lemma symbol_address_preserved:
+ forall l ofs, Genv.symbol_address ge l ofs = Genv.symbol_address tge l ofs.
+Proof.
+ intros. unfold Genv.symbol_address. repeat (rewrite symbols_preserved). reflexivity.
+Qed.
+
+Lemma verified_schedule_label:
+ forall bb tbb l,
+ verified_schedule bb = OK (tbb) ->
+ is_label l bb = is_label l tbb.
+Proof.
+ intros.
+ unfold is_label.
+ monadInv H. simpl. auto.
+Qed.
+
+Remark label_pos_pvar_none_add:
+ forall tc l p p' k,
+ label_pos l (p+k) tc = None -> label_pos l (p'+k) tc = None.
+Proof.
+ induction tc.
+ - intros. simpl. auto.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + discriminate.
+ + pose (IHtc l p p' (k + size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar_none:
+ forall tc l p p',
+ label_pos l p tc = None -> label_pos l p' tc = None.
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_none_add; eauto.
+Qed.
+
+Remark label_pos_pvar_some_add_add:
+ forall tc l p p' k k',
+ label_pos l (p+k') tc = Some (p+k) -> label_pos l (p'+k') tc = Some (p'+k).
+Proof.
+ induction tc.
+ - intros. simpl in H. discriminate.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + inv H. assert (k = k') by lia. subst. reflexivity.
+ + pose (IHtc l p p' k (k' + size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar_some_add:
+ forall tc l p p' k,
+ label_pos l p tc = Some (p+k) -> label_pos l p' tc = Some (p'+k).
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_some_add_add; eauto.
+Qed.
+
+Remark label_pos_pvar_add:
+ forall c tc l p p' k,
+ label_pos l (p+k) c = label_pos l p tc ->
+ label_pos l (p'+k) c = label_pos l p' tc.
+Proof.
+ induction c.
+ - intros. simpl in *.
+ exploit label_pos_pvar_none; eauto.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + exploit label_pos_pvar_some_add; eauto.
+ + pose (IHc tc l p p' (k+size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar:
+ forall c tc l p p',
+ label_pos l p c = label_pos l p tc ->
+ label_pos l p' c = label_pos l p' tc.
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_add; eauto.
+Qed.
+
+Lemma label_pos_head_cons:
+ forall c bb tbb l tc p,
+ verified_schedule bb = OK tbb ->
+ label_pos l p c = label_pos l p tc ->
+ label_pos l p (bb :: c) = label_pos l p (tbb :: tc).
+Proof.
+ intros. simpl.
+ exploit verified_schedule_label; eauto. intros ISLBL.
+ rewrite ISLBL.
+ destruct (is_label l tbb) eqn:ISLBL'; simpl; auto.
+ eapply label_pos_pvar in H0. erewrite H0.
+ erewrite verified_schedule_size; eauto.
+Qed.
+
+Lemma label_pos_preserved:
+ forall c tc l,
+ transf_blocks c = OK tc -> label_pos l 0 c = label_pos l 0 tc.
+Proof.
+ induction c.
+ - intros. simpl in *. inv H. reflexivity.
+ - intros. unfold transf_blocks in H; fold transf_blocks in H. monadInv H.
+ eapply IHc in EQ. eapply label_pos_head_cons; eauto.
+Qed.
+
+Lemma label_pos_preserved_blocks:
+ forall l f tf,
+ transf_function f = OK tf ->
+ label_pos l 0 (fn_blocks f) = label_pos l 0 (fn_blocks tf).
+Proof.
+ intros. monadInv H. monadInv EQ.
+ destruct (zlt Ptrofs.max_unsigned _); try discriminate.
+ monadInv EQ0. simpl. eapply label_pos_preserved; eauto.
+Qed.
+
+Lemma transf_exec_basic:
+ forall i rs m, exec_basic lk ge i rs m = exec_basic lk tge i rs m.
+Proof.
+ intros. pose symbol_address_preserved.
+ unfold exec_basic.
+ destruct i; simpl; auto; try congruence.
+Qed.
+
+Lemma transf_exec_body:
+ forall bdy rs m, exec_body lk ge bdy rs m = exec_body lk tge bdy rs m.
+Proof.
+ induction bdy; intros.
+ - simpl. reflexivity.
+ - simpl. rewrite transf_exec_basic.
+ destruct (exec_basic _ _ _); auto.
+Qed.
+
+Lemma transf_exec_cfi: forall f tf cfi rs m,
+ transf_function f = OK tf ->
+ exec_cfi ge f cfi rs m = exec_cfi tge tf cfi rs m.
+Proof.
+ intros. destruct cfi; simpl; auto;
+ assert (ge = Genv.globalenv prog); auto;
+ assert (tge = Genv.globalenv tprog); auto;
+ pose symbol_address_preserved.
+ all: try unfold eval_branch; try unfold eval_neg_branch; try unfold goto_label;
+ try erewrite label_pos_preserved_blocks; try rewrite e; eauto.
+ destruct (rs # X16 <- Vundef r1); auto.
+ destruct (list_nth_z tbl (Int.unsigned i)); auto.
+ erewrite label_pos_preserved_blocks; eauto.
+Qed.
+
+Lemma transf_exec_exit:
+ forall f tf sz ex t rs m rs' m',
+ transf_function f = OK tf ->
+ exec_exit ge f sz rs m ex t rs' m' ->
+ exec_exit tge tf sz rs m ex t rs' m'.
+Proof.
+ intros. induction H0.
+ - econstructor.
+ - econstructor. erewrite <- transf_exec_cfi; eauto.
+ - econstructor; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+Qed.
+
+Lemma transf_exec_bblock:
+ forall f tf bb t rs m rs' m',
+ transf_function f = OK tf ->
+ exec_bblock lk ge f bb rs m t rs' m' ->
+ exec_bblock lk tge tf bb rs m t rs' m'.
+Proof.
+ intros.
+ destruct H0 as [rs1[m1[BDY EXIT]]].
+ unfold exec_bblock.
+ eexists; eexists; split.
+ rewrite <- transf_exec_body; eauto.
+ eapply transf_exec_exit; eauto.
+Qed.
+
+Theorem transf_step_correct:
+ forall s1 t s2, step lk ge s1 t s2 ->
+ forall s1' (MS: match_states s1 s1'),
+ (exists s2', step lk tge s1' t s2' /\ match_states s2 s2').
+Proof.
+ induction 1; intros; inv MS.
+ - exploit function_ptr_translated; eauto. intros (tf & FFP & TRANSF). monadInv TRANSF.
+ exploit transf_find_bblock; eauto. intros (tbb & VES & FIND).
+ exploit verified_schedule_correct; eauto. intros EBB.
+ eapply transf_exec_bblock in EBB; eauto.
+ exists (State rs' m').
+ split; try (econstructor; eauto).
+ - exploit function_ptr_translated; eauto. intros (tf & FFP & TRANSF). monadInv TRANSF.
+ remember (State _ m') as s'. exists s'. split; try constructor; auto.
+ subst s'. eapply exec_step_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+Qed.
+
+Theorem transf_program_correct_Asmblock:
+ forward_simulation (Asmblock.semantics lk prog) (Asmblock.semantics lk tprog).
+Proof.
+ eapply forward_simulation_step.
+ - apply senv_preserved.
+ - apply transf_initial_states.
+ - apply transf_final_states.
+ - apply transf_step_correct.
+Qed.
+
+End PRESERVATION_ASMBLOCK.
+
+(*
+Require Import Asm.
+
+Lemma verified_par_checks_alls_bundles lb x: forall bundle,
+ verify_par lb = OK x ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ induction lb; simpl; try tauto.
+ intros bundle H; monadInv H.
+ destruct 1; subst; eauto.
+ destruct x0; auto.
+Qed.
+
+Lemma verified_schedule_nob_checks_alls_bundles bb lb bundle:
+ verified_schedule_nob bb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ unfold verified_schedule_nob. intros H;
+ monadInv H. destruct x4.
+ intros; eapply verified_par_checks_alls_bundles; eauto.
+Qed.
+
+Lemma verify_par_bblock_PExpand bb i:
+ exit bb = Some (PExpand i) -> verify_par_bblock bb = OK tt.
+Proof.
+ destruct bb as [h bdy ext H]; simpl.
+ intros; subst. destruct i.
+ generalize H.
+ rewrite <- wf_bblock_refl in H.
+ destruct H as [H H0].
+ unfold builtin_alone in H0. erewrite H0; eauto.
+Qed.
+
+Local Hint Resolve verified_schedule_nob_checks_alls_bundles: core.
+
+Lemma verified_schedule_checks_alls_bundles bb lb bundle:
+ verified_schedule bb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ unfold verified_schedule. remember (exit bb) as exb.
+ destruct exb as [c|]; eauto.
+ destruct c as [i|]; eauto.
+ destruct i; intros H. inversion_clear H; simpl.
+ intuition subst.
+ intros; eapply verify_par_bblock_PExpand; eauto.
+Qed.
+
+Lemma transf_blocks_checks_all_bundles lbb: forall lb bundle,
+ transf_blocks lbb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ induction lbb; simpl.
+ - intros lb bundle H; inversion_clear H. simpl; try tauto.
+ - intros lb bundle H0.
+ monadInv H0.
+ rewrite in_app. destruct 1; eauto.
+ eapply verified_schedule_checks_alls_bundles; eauto.
+Qed.
+
+Lemma find_bblock_Some_in lb:
+ forall ofs b, find_bblock ofs lb = Some b -> List.In b lb.
+Proof.
+ induction lb; simpl; try congruence.
+ intros ofs b.
+ destruct (zlt ofs 0); try congruence.
+ destruct (zeq ofs 0); eauto.
+ intros X; inversion X; eauto.
+Qed.*)
+
+(*
+Section PRESERVATION_ASMVLIW.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma all_bundles_are_checked b ofs f bundle:
+ Genv.find_funct_ptr (globalenv (Asmblock.semantics tprog)) b = Some (Internal f) ->
+ find_bblock ofs (fn_blocks f) = Some bundle ->
+ verify_par_bblock bundle = OK tt.
+Proof.
+ unfold match_prog, match_program in TRANSL.
+ unfold Genv.find_funct_ptr; simpl; intros X.
+ destruct (Genv.find_def_match_2 TRANSL b) as [|f0 y H]; try congruence.
+ destruct y as [tf0|]; try congruence.
+ inversion X as [H1]. subst. clear X.
+ remember (@Gfun fundef unit (Internal f)) as f2.
+ destruct H as [ctx' f1 f2 H0|]; try congruence.
+ inversion Heqf2 as [H2]. subst; clear Heqf2.
+ unfold transf_fundef, transf_partial_fundef in H.
+ destruct f1 as [f1|f1]; try congruence.
+ unfold transf_function, transl_function in H.
+ monadInv H. monadInv EQ.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks _))); simpl in *|-; try congruence.
+ injection EQ1; intros; subst.
+ monadInv EQ0. simpl in * |-.
+ intros; exploit transf_blocks_checks_all_bundles; eauto.
+ intros; eapply find_bblock_Some_in; eauto.
+Qed.
+
+Lemma checked_bundles_are_parexec_equiv f bundle rs rs' m m':
+ exec_bblock (globalenv (Asmblock.semantics tprog)) f bundle rs m = Next rs' m' ->
+ verify_par_bblock bundle = OK tt ->
+ det_parexec (globalenv (semantics tprog)) f bundle rs m rs' m'.
+Proof.
+ intros. unfold verify_par_bblock in H0. destruct (Asmblockdeps.bblock_para_check _) eqn:BPC; try discriminate. clear H0.
+ simpl in H.
+ eapply Asmblockdeps.bblock_para_check_correct; eauto.
+Qed.
+
+Lemma seqexec_parexec_equiv b ofs f bundle rs rs' m m':
+ Genv.find_funct_ptr (globalenv (Asmblock.semantics tprog)) b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bundle ->
+ exec_bblock (globalenv (Asmblock.semantics tprog)) f bundle rs m = Next rs' m' ->
+ det_parexec (globalenv (semantics tprog)) f bundle rs m rs' m'.
+Proof.
+ intros; eapply checked_bundles_are_parexec_equiv; eauto.
+ eapply all_bundles_are_checked; eauto.
+Qed.
+
+Theorem transf_program_correct_Asmvliw:
+ forward_simulation (Asmblock.semantics tprog) (Asmvliw.semantics tprog).
+Proof.
+ eapply forward_simulation_step with (match_states:=fun (s1:Asmvliw.state) s2 => s1=s2); eauto.
+ - intros; subst; auto.
+ - intros s1 t s1' H s2 H0; subst; inversion H; clear H; subst; eexists; split; eauto.
+ + eapply exec_step_internal; eauto.
+ intros; eapply seqexec_parexec_equiv; eauto.
+ + eapply exec_step_builtin; eauto.
+ + eapply exec_step_external; eauto.
+Qed.
+
+End PRESERVATION_ASMVLIW.*)
+
+Section PRESERVATION.
+
+Variable lk: aarch64_linker.
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Theorem transf_program_correct:
+ forward_simulation (Asmblock.semantics lk prog) (Asmblock.semantics lk tprog).
+Proof.
+ eapply transf_program_correct_Asmblock; eauto.
+Qed.
+
+End PRESERVATION.
diff --git a/aarch64/PrepassSchedulingOracle.ml b/aarch64/PrepassSchedulingOracle.ml
new file mode 100644
index 00000000..d7e80cd9
--- /dev/null
+++ b/aarch64/PrepassSchedulingOracle.ml
@@ -0,0 +1,297 @@
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* Nicolas Nardino ENS-Lyon, VERIMAG *)
+(* *)
+(* *)
+(* *************************************************************)
+
+open AST
+open BTL
+open Maps
+open InstructionScheduler
+open Registers
+open PrepassSchedulingOracleDeps
+open PrintBTL
+open DebugPrint
+
+let use_alias_analysis () = false
+
+let build_constraints_and_resources (opweights : opweights) seqa btl =
+ let last_reg_reads : int list PTree.t ref = ref PTree.empty
+ and last_reg_write : (int * int) PTree.t ref = ref PTree.empty
+ and last_mem_reads : int list ref = ref []
+ and last_mem_write : int option ref = ref None
+ and last_branch : int option ref = ref None
+ and last_non_pipelined_op : int array =
+ Array.make opweights.nr_non_pipelined_units (-1)
+ and latency_constraints : latency_constraint list ref = ref [] in
+ let add_constraint instr_from instr_to latency =
+ assert (instr_from <= instr_to);
+ assert (latency >= 0);
+ if instr_from = instr_to then
+ if latency = 0 then ()
+ else
+ failwith "PrepassSchedulingOracle.get_dependencies: negative self-loop"
+ else
+ latency_constraints :=
+ { instr_from; instr_to; latency } :: !latency_constraints
+ and get_last_reads reg =
+ match PTree.get reg !last_reg_reads with Some l -> l | None -> []
+ in
+ let add_input_mem i =
+ if not (use_alias_analysis ()) then (
+ (* Read after write *)
+ (match !last_mem_write with None -> () | Some j -> add_constraint j i 1);
+ last_mem_reads := i :: !last_mem_reads)
+ and add_output_mem i =
+ if not (use_alias_analysis ()) then (
+ (* Write after write *)
+ (match !last_mem_write with None -> () | Some j -> add_constraint j i 1);
+ (* Write after read *)
+ List.iter (fun j -> add_constraint j i 0) !last_mem_reads;
+ last_mem_write := Some i;
+ last_mem_reads := [])
+ and add_input_reg i reg =
+ (* Read after write *)
+ (match PTree.get reg !last_reg_write with
+ | None -> ()
+ | Some (j, latency) -> add_constraint j i latency);
+ last_reg_reads := PTree.set reg (i :: get_last_reads reg) !last_reg_reads
+ and add_output_reg i latency reg =
+ (* Write after write *)
+ (match PTree.get reg !last_reg_write with
+ | None -> ()
+ | Some (j, _) -> add_constraint j i 1);
+ (* Write after read *)
+ List.iter (fun j -> add_constraint j i 0) (get_last_reads reg);
+ last_reg_write := PTree.set reg (i, latency) !last_reg_write;
+ last_reg_reads := PTree.remove reg !last_reg_reads
+ in
+ let add_input_regs i regs = List.iter (add_input_reg i) regs in
+ let rec add_builtin_res i (res : reg builtin_res) =
+ match res with
+ | BR r -> add_output_reg i 10 r
+ | BR_none -> ()
+ | BR_splitlong (hi, lo) ->
+ add_builtin_res i hi;
+ add_builtin_res i lo
+ in
+ let rec add_builtin_arg i (ba : reg builtin_arg) =
+ match ba with
+ | BA r -> add_input_reg i r
+ | BA_int _ | BA_long _ | BA_float _ | BA_single _ -> ()
+ | BA_loadstack (_, _) -> add_input_mem i
+ | BA_addrstack _ -> ()
+ | BA_loadglobal (_, _, _) -> add_input_mem i
+ | BA_addrglobal _ -> ()
+ | BA_splitlong (hi, lo) ->
+ add_builtin_arg i hi;
+ add_builtin_arg i lo
+ | BA_addptr (a1, a2) ->
+ add_builtin_arg i a1;
+ add_builtin_arg i a2
+ and irreversible_action i =
+ match !last_branch with None -> () | Some j -> add_constraint j i 1
+ in
+ let set_branch i =
+ irreversible_action i;
+ last_branch := Some i
+ and add_non_pipelined_resources i resources =
+ Array.iter2
+ (fun latency last ->
+ if latency >= 0 && last >= 0 then add_constraint last i latency)
+ resources last_non_pipelined_op;
+ Array.iteri
+ (fun rsc latency -> if latency >= 0 then last_non_pipelined_op.(rsc) <- i)
+ resources
+ in
+ Array.iteri
+ (fun i (inst, other_uses) ->
+ List.iter (fun use -> add_input_reg i use) (Regset.elements other_uses);
+ match inst with
+ | Bnop _ -> ()
+ | Bop (op, lr, rd, _) ->
+ add_non_pipelined_resources i
+ (opweights.non_pipelined_resources_of_op op (List.length lr));
+ if Op.is_trapping_op op then irreversible_action i;
+ add_input_regs i lr;
+ add_output_reg i (opweights.latency_of_op op (List.length lr)) rd
+ | Bload (trap, chk, addr, lr, rd, _) ->
+ if trap = TRAP then irreversible_action i;
+ add_input_mem i;
+ add_input_regs i lr;
+ add_output_reg i
+ (opweights.latency_of_load trap chk addr (List.length lr))
+ rd
+ | Bstore (chk, addr, lr, src, _) ->
+ irreversible_action i;
+ add_input_regs i lr;
+ add_input_reg i src;
+ add_output_mem i
+ | Bcond (cond, lr, BF (Bgoto s, _), ibnot, _) ->
+ set_branch i;
+ add_input_mem i;
+ add_input_regs i lr
+ | Bcond (_, _, _, _, _) ->
+ failwith "build_constraints_and_resources: invalid Bcond"
+ | BF (Bcall (signature, ef, lr, rd, _), _) ->
+ set_branch i;
+ (match ef with
+ | Datatypes.Coq_inl r -> add_input_reg i r
+ | Datatypes.Coq_inr symbol -> ());
+ add_input_mem i;
+ add_input_regs i lr;
+ add_output_reg i (opweights.latency_of_call signature ef) rd;
+ add_output_mem i;
+ failwith "build_constraints_and_resources: invalid Bcall"
+ | BF (Btailcall (signature, ef, lr), _) ->
+ set_branch i;
+ (match ef with
+ | Datatypes.Coq_inl r -> add_input_reg i r
+ | Datatypes.Coq_inr symbol -> ());
+ add_input_mem i;
+ add_input_regs i lr;
+ failwith "build_constraints_and_resources: invalid Btailcall"
+ | BF (Bbuiltin (ef, lr, rd, _), _) ->
+ set_branch i;
+ add_input_mem i;
+ List.iter (add_builtin_arg i) lr;
+ add_builtin_res i rd;
+ add_output_mem i;
+ failwith "build_constraints_and_resources: invalid Bbuiltin"
+ | BF (Bjumptable (lr, _), _) ->
+ set_branch i;
+ add_input_reg i lr;
+ failwith "build_constraints_and_resources: invalid Bjumptable"
+ | BF (Breturn (Some r), _) ->
+ set_branch i;
+ add_input_reg i r;
+ failwith "build_constraints_and_resources: invalid Breturn Some"
+ | BF (Breturn None, _) ->
+ set_branch i;
+ failwith "build_constraints_and_resources: invalid Breturn None"
+ | BF (Bgoto _, _) ->
+ failwith "build_constraints_and_resources: invalid Bgoto"
+ | Bseq (_, _) -> failwith "build_constraints_and_resources: Bseq")
+ seqa;
+ !latency_constraints
+
+let resources_of_instruction (opweights : opweights) = function
+ | Bnop _ -> Array.map (fun _ -> 0) opweights.pipelined_resource_bounds
+ | Bop (op, inputs, output, _) ->
+ opweights.resources_of_op op (List.length inputs)
+ | Bload (trap, chunk, addressing, addr_regs, output, _) ->
+ opweights.resources_of_load trap chunk addressing (List.length addr_regs)
+ | Bstore (chunk, addressing, addr_regs, input, _) ->
+ opweights.resources_of_store chunk addressing (List.length addr_regs)
+ | BF (Bcall (signature, ef, inputs, output, _), _) ->
+ opweights.resources_of_call signature ef
+ | BF (Bbuiltin (ef, builtin_inputs, builtin_output, _), _) ->
+ opweights.resources_of_builtin ef
+ | Bcond (cond, args, _, _, _) ->
+ opweights.resources_of_cond cond (List.length args)
+ | BF (Btailcall _, _) | BF (Bjumptable _, _) | BF (Breturn _, _) ->
+ opweights.pipelined_resource_bounds
+ | BF (Bgoto _, _) | Bseq (_, _) ->
+ failwith "resources_of_instruction: invalid btl instruction"
+
+let print_sequence pp seqa =
+ Array.iteri
+ (fun i (inst, other_uses) ->
+ debug "i=%d\n inst = " i;
+ print_btl_inst pp inst;
+ debug "\n other_uses=";
+ print_regset other_uses;
+ debug "\n")
+ seqa
+
+let length_of_chunk = function
+ | Mint8signed | Mint8unsigned -> 1
+ | Mint16signed | Mint16unsigned -> 2
+ | Mint32 | Mfloat32 | Many32 -> 4
+ | Mint64 | Mfloat64 | Many64 -> 8
+
+let define_problem (opweights : opweights) (live_entry_regs : Regset.t)
+ (typing : RTLtyping.regenv) reference_counting seqa btl =
+ let simple_deps = build_constraints_and_resources opweights seqa btl in
+ {
+ max_latency = -1;
+ resource_bounds = opweights.pipelined_resource_bounds;
+ live_regs_entry = live_entry_regs;
+ typing;
+ reference_counting = Some reference_counting;
+ instruction_usages =
+ Array.map (resources_of_instruction opweights) (Array.map fst seqa);
+ latency_constraints = simple_deps;
+ }
+
+let zigzag_scheduler problem early_ones =
+ let nr_instructions = get_nr_instructions problem in
+ assert (nr_instructions = Array.length early_ones);
+ match list_scheduler problem with
+ | Some fwd_schedule ->
+ let fwd_makespan = fwd_schedule.(Array.length fwd_schedule - 1) in
+ let constraints' = ref problem.latency_constraints in
+ Array.iteri
+ (fun i is_early ->
+ if is_early then
+ constraints' :=
+ {
+ instr_from = i;
+ instr_to = nr_instructions;
+ latency = fwd_makespan - fwd_schedule.(i);
+ }
+ :: !constraints')
+ early_ones;
+ validated_scheduler reverse_list_scheduler
+ { problem with latency_constraints = !constraints' }
+ | None -> None
+
+let prepass_scheduler_by_name name problem seqa =
+ match name with
+ | "zigzag" ->
+ let early_ones =
+ Array.map
+ (fun (inst, _) ->
+ match inst with Bcond (_, _, _, _, _) -> true | _ -> false)
+ seqa
+ in
+ zigzag_scheduler problem early_ones
+ | _ -> scheduler_by_name name problem
+
+let schedule_sequence seqa btl (live_regs_entry : Registers.Regset.t)
+ (typing : RTLtyping.regenv) reference =
+ let opweights = OpWeights.get_opweights () in
+ try
+ if Array.length seqa <= 1 then None
+ else
+ let nr_instructions = Array.length seqa in
+ if !Clflags.option_debug_compcert > 6 then
+ Printf.printf "prepass scheduling length = %d\n" nr_instructions;
+ let problem =
+ define_problem opweights live_regs_entry typing reference seqa btl
+ in
+ if !Clflags.option_debug_compcert > 7 then (
+ print_sequence stdout seqa;
+ print_problem stdout problem);
+ match
+ prepass_scheduler_by_name !Clflags.option_fprepass_sched problem seqa
+ with
+ | None ->
+ Printf.printf "no solution in prepass scheduling\n";
+ None
+ | Some solution ->
+ let positions = Array.init nr_instructions (fun i -> i) in
+ Array.sort
+ (fun i j ->
+ let si = solution.(i) and sj = solution.(j) in
+ if si < sj then -1 else if si > sj then 1 else i - j)
+ positions;
+ Some positions
+ with Failure s ->
+ Printf.printf "failure in prepass scheduling: %s\n" s;
+ None
diff --git a/aarch64/PrepassSchedulingOracleDeps.ml b/aarch64/PrepassSchedulingOracleDeps.ml
new file mode 100644
index 00000000..8d10d406
--- /dev/null
+++ b/aarch64/PrepassSchedulingOracleDeps.ml
@@ -0,0 +1,17 @@
+type called_function = (Registers.reg, AST.ident) Datatypes.sum
+
+type opweights =
+ {
+ pipelined_resource_bounds : int array;
+ nr_non_pipelined_units : int;
+ latency_of_op : Op.operation -> int -> int;
+ resources_of_op : Op.operation -> int -> int array;
+ non_pipelined_resources_of_op : Op.operation -> int -> int array;
+ latency_of_load : AST.trapping_mode -> AST.memory_chunk -> Op.addressing -> int -> int;
+ resources_of_load : AST.trapping_mode -> AST.memory_chunk -> Op.addressing -> int -> int array;
+ resources_of_store : AST.memory_chunk -> Op.addressing -> int -> int array;
+ resources_of_cond : Op.condition -> int -> int array;
+ latency_of_call : AST.signature -> called_function -> int;
+ resources_of_call : AST.signature -> called_function -> int array;
+ resources_of_builtin : AST.external_function -> int array
+ };;
diff --git a/aarch64/SelectLongproof.v b/aarch64/SelectLongproof.v
index aee09b12..0984943c 100644
--- a/aarch64/SelectLongproof.v
+++ b/aarch64/SelectLongproof.v
@@ -16,6 +16,7 @@ Require Import Coqlib Zbits.
Require Import AST Integers Floats Values Memory Globalenvs.
Require Import Cminor Op CminorSel.
Require Import SelectOp SelectLong SelectOpproof.
+Require Import OpHelpers OpHelpersproof.
Local Open Scope cminorsel_scope.
Local Transparent Archi.ptr64.
@@ -23,8 +24,10 @@ Local Transparent Archi.ptr64.
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -556,25 +559,29 @@ Qed.
Theorem eval_divls_base: partial_binary_constructor_sound divls_base Val.divls.
Proof.
red; intros; unfold divls_base; TrivialExists.
+ cbn. rewrite H1. reflexivity.
Qed.
Theorem eval_modls_base: partial_binary_constructor_sound modls_base Val.modls.
Proof.
red; intros; unfold modls_base, modl_aux.
exploit Val.modls_divls; eauto. intros (q & A & B). subst z.
- TrivialExists. repeat (econstructor; eauto with evalexpr). exact A.
+ TrivialExists. repeat (econstructor; eauto with evalexpr).
+ rewrite A. reflexivity.
Qed.
Theorem eval_divlu_base: partial_binary_constructor_sound divlu_base Val.divlu.
Proof.
red; intros; unfold divlu_base; TrivialExists.
+ cbn. rewrite H1. reflexivity.
Qed.
Theorem eval_modlu_base: partial_binary_constructor_sound modlu_base Val.modlu.
Proof.
red; intros; unfold modlu_base, modl_aux.
exploit Val.modlu_divlu; eauto. intros (q & A & B). subst z.
- TrivialExists. repeat (econstructor; eauto with evalexpr). exact A.
+ TrivialExists. repeat (econstructor; eauto with evalexpr).
+ rewrite A. reflexivity.
Qed.
Theorem eval_shrxlimm:
@@ -589,7 +596,7 @@ Proof.
destruct x; simpl in H0; try discriminate.
change (Int.ltu Int.zero (Int.repr 63)) with true in H0; inv H0.
rewrite Int64.shrx'_zero. auto.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
(** General shifts *)
@@ -723,42 +730,42 @@ Qed.
Theorem eval_longoffloat: partial_unary_constructor_sound longoffloat Val.longoffloat.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_longuoffloat: partial_unary_constructor_sound longuoffloat Val.longuoffloat.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatoflong: partial_unary_constructor_sound floatoflong Val.floatoflong.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatoflongu: partial_unary_constructor_sound floatoflongu Val.floatoflongu.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_longofsingle: partial_unary_constructor_sound longofsingle Val.longofsingle.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_longuofsingle: partial_unary_constructor_sound longuofsingle Val.longuofsingle.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleoflong: partial_unary_constructor_sound singleoflong Val.singleoflong.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleoflongu: partial_unary_constructor_sound singleoflongu Val.singleoflongu.
Proof.
- red; intros; TrivialExists.
+ red; intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
End CMCONSTR.
diff --git a/aarch64/SelectOp.vp b/aarch64/SelectOp.vp
index b5a03989..7f73d592 100644
--- a/aarch64/SelectOp.vp
+++ b/aarch64/SelectOp.vp
@@ -56,9 +56,13 @@ Nondetfunction add (e1: expr) (e2: expr) :=
| t1, Eop (Oshift s a) (t2:::Enil) ?? arith_shift s =>
Eop (Oaddshift s a) (t1 ::: t2 ::: Enil)
| Eop Omul (t1:::t2:::Enil), t3 =>
- Eop Omuladd (t3:::t1:::t2:::Enil)
+ if Compopts.optim_madd tt
+ then Eop Omuladd (t3:::t1:::t2:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
| t1, Eop Omul (t2:::t3:::Enil) =>
- Eop Omuladd (t1:::t2:::t3:::Enil)
+ if Compopts.optim_madd tt
+ then Eop Omuladd (t1:::t2:::t3:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
| _, _ => Eop Oadd (e1:::e2:::Enil)
end.
@@ -555,6 +559,13 @@ Nondetfunction addressing (chunk: memory_chunk) (e: expr) :=
| _ => (Aindexed Int64.zero, e:::Enil)
end.
+(* floats *)
+Definition divf_base (e1: expr) (e2: expr) :=
+ Eop Odivf (e1 ::: e2 ::: Enil).
+
+Definition divfs_base (e1: expr) (e2: expr) :=
+ Eop Odivfs (e1 ::: e2 ::: Enil).
+
(** ** Arguments of builtins *)
Nondetfunction builtin_arg (e: expr) :=
diff --git a/aarch64/SelectOpproof.v b/aarch64/SelectOpproof.v
index ccc4c0f1..dfa4c598 100644
--- a/aarch64/SelectOpproof.v
+++ b/aarch64/SelectOpproof.v
@@ -16,6 +16,7 @@ Require Import Coqlib Zbits.
Require Import AST Integers Floats Values Memory Builtins Globalenvs.
Require Import Cminor Op CminorSel.
Require Import SelectOp.
+Require Import OpHelpers OpHelpersproof.
Local Open Scope cminorsel_scope.
Local Transparent Archi.ptr64.
@@ -74,8 +75,10 @@ Ltac TrivialExists :=
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -158,8 +161,10 @@ Proof.
- rewrite <- Val.add_assoc. apply eval_addimm. EvalOp.
- rewrite Val.add_commut. TrivialExists.
- TrivialExists.
-- rewrite Val.add_commut. TrivialExists.
-- TrivialExists.
+- destruct (Compopts.optim_madd tt).
+ + rewrite Val.add_commut. TrivialExists.
+ + TrivialExists.
+- destruct (Compopts.optim_madd tt); TrivialExists.
- TrivialExists.
Qed.
@@ -661,7 +666,8 @@ Theorem eval_divs_base:
Val.divs x y = Some z ->
exists v, eval_expr ge sp e m le (divs_base a b) v /\ Val.lessdef z v.
Proof.
- intros; unfold divs_base; TrivialExists.
+ intros; unfold divs_base; TrivialExists; cbn.
+ rewrite H1. reflexivity.
Qed.
Theorem eval_mods_base:
@@ -673,7 +679,8 @@ Theorem eval_mods_base:
Proof.
intros; unfold mods_base, mod_aux.
exploit Val.mods_divs; eauto. intros (q & A & B). subst z.
- TrivialExists. repeat (econstructor; eauto with evalexpr). exact A.
+ TrivialExists. repeat (econstructor; eauto with evalexpr).
+ cbn. rewrite A. reflexivity.
Qed.
Theorem eval_divu_base:
@@ -684,6 +691,7 @@ Theorem eval_divu_base:
exists v, eval_expr ge sp e m le (divu_base a b) v /\ Val.lessdef z v.
Proof.
intros; unfold divu_base; TrivialExists.
+ cbn. rewrite H1. reflexivity.
Qed.
Theorem eval_modu_base:
@@ -695,7 +703,8 @@ Theorem eval_modu_base:
Proof.
intros; unfold modu_base, mod_aux.
exploit Val.modu_divu; eauto. intros (q & A & B). subst z.
- TrivialExists. repeat (econstructor; eauto with evalexpr). exact A.
+ TrivialExists. repeat (econstructor; eauto with evalexpr).
+ rewrite A. reflexivity.
Qed.
Theorem eval_shrximm:
@@ -710,7 +719,7 @@ Proof.
destruct x; simpl in H0; try discriminate.
change (Int.ltu Int.zero (Int.repr 31)) with true in H0; inv H0.
rewrite Int.shrx_zero by (compute; auto). auto.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
(** General shifts *)
@@ -923,7 +932,7 @@ Theorem eval_intoffloat:
Val.intoffloat x = Some y ->
exists v, eval_expr ge sp e m le (intoffloat a) v /\ Val.lessdef y v.
Proof.
- intros; TrivialExists.
+ intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatofint:
@@ -934,7 +943,7 @@ Theorem eval_floatofint:
Proof.
intros until y; unfold floatofint. case (floatofint_match a); intros; InvEval.
- TrivialExists.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intuoffloat:
@@ -943,7 +952,7 @@ Theorem eval_intuoffloat:
Val.intuoffloat x = Some y ->
exists v, eval_expr ge sp e m le (intuoffloat a) v /\ Val.lessdef y v.
Proof.
- intros; TrivialExists.
+ intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatofintu:
@@ -954,7 +963,7 @@ Theorem eval_floatofintu:
Proof.
intros until y; unfold floatofintu. case (floatofintu_match a); intros; InvEval.
- TrivialExists.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intofsingle:
@@ -963,7 +972,7 @@ Theorem eval_intofsingle:
Val.intofsingle x = Some y ->
exists v, eval_expr ge sp e m le (intofsingle a) v /\ Val.lessdef y v.
Proof.
- intros; TrivialExists.
+ intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleofint:
@@ -974,7 +983,7 @@ Theorem eval_singleofint:
Proof.
intros until y; unfold singleofint. case (singleofint_match a); intros; InvEval.
- TrivialExists.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intuofsingle:
@@ -983,7 +992,7 @@ Theorem eval_intuofsingle:
Val.intuofsingle x = Some y ->
exists v, eval_expr ge sp e m le (intuofsingle a) v /\ Val.lessdef y v.
Proof.
- intros; TrivialExists.
+ intros; TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleofintu:
@@ -994,7 +1003,7 @@ Theorem eval_singleofintu:
Proof.
intros until y; unfold singleofintu. case (singleofintu_match a); intros; InvEval.
- TrivialExists.
-- TrivialExists.
+- TrivialExists. cbn. rewrite H0. reflexivity.
Qed.
(** Selection *)
@@ -1061,6 +1070,26 @@ Proof.
- constructor; auto.
Qed.
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ TrivialExists.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ TrivialExists.
+Qed.
+
(** Platform-specific known builtins *)
Theorem eval_platform_builtin:
diff --git a/aarch64/TargetPrinter.ml b/aarch64/TargetPrinter.ml
index a9d47bdd..1ca3be16 100644
--- a/aarch64/TargetPrinter.ml
+++ b/aarch64/TargetPrinter.ml
@@ -21,23 +21,6 @@ open AisAnnot
open PrintAsmaux
open Fileinfo
-(* Recognition of FP numbers that are supported by the fmov #imm instructions:
- "a normalized binary floating point encoding with 1 sign bit,
- 4 bits of fraction and a 3-bit exponent"
-*)
-
-let is_immediate_float64 bits =
- let exp = (Int64.(to_int (shift_right_logical bits 52)) land 0x7FF) - 1023 in
- let mant = Int64.logand bits 0xF_FFFF_FFFF_FFFFL in
- exp >= -3 && exp <= 4 && Int64.logand mant 0xF_0000_0000_0000L = mant
-
-let is_immediate_float32 bits =
- let exp = (Int32.(to_int (shift_right_logical bits 23)) land 0xFF) - 127 in
- let mant = Int32.logand bits 0x7F_FFFFl in
- exp >= -3 && exp <= 4 && Int32.logand mant 0x78_0000l = mant
-
-(* Naming and printing registers *)
-
let intsz oc (sz, n) =
match sz with X -> coqint64 oc n | W -> coqint oc n
@@ -107,14 +90,14 @@ let freg oc (sz, r) =
output_string oc (match sz with D -> dreg_name r | S -> sreg_name r)
let preg_asm oc ty = function
- | IR r -> if ty = Tint then wreg oc r else xreg oc r
- | FR r -> if ty = Tsingle then sreg oc r else dreg oc r
- | _ -> assert false
+ | DR (IR (RR1 r)) -> if ty = Tint then wreg oc r else xreg oc r
+ | DR (FR r) -> if ty = Tsingle then sreg oc r else dreg oc r
+| _ -> assert false
let preg_annot = function
- | IR r -> xreg_name r
- | FR r -> dreg_name r
- | _ -> assert false
+ | DR (IR (RR1 r)) -> xreg_name r
+ | DR (FR r) -> dreg_name r
+ | _ -> assert false
(* Base-2 log of a Caml integer *)
let rec log2 n =
@@ -156,12 +139,16 @@ module ELF_System : SYSTEM =
fprintf oc "#:lo12:%a" elf_label lbl
let load_symbol_address oc rd id =
- fprintf oc " adrp %a, :got:%a\n" xreg rd symbol id;
- fprintf oc " ldr %a, [%a, #:got_lo12:%a]\n" xreg rd xreg rd symbol id
+ fprintf oc " adrp %a, :got:%a\n" xreg rd symbol id;
+ fprintf oc " ldr %a, [%a, #:got_lo12:%a]\n" xreg rd xreg rd symbol id
+
+ (* Names of sections *)
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".section .rodata" i
@@ -223,7 +210,9 @@ module MacOS_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".const" ~reloc:".const_data" i
@@ -343,6 +332,28 @@ module Target(System: SYSTEM): TARGET =
| (r, EOuxtw n) -> fprintf oc "%a, uxtw #%a" wreg r coqint n
| (r, EOuxtx n) -> fprintf oc "%a, uxtx #%a" xreg r coqint n
+ let next_profiling_label =
+ let atomic_incr_counter = ref 0 in
+ fun () ->
+ let r = sprintf ".Lcompcert_atomic_incr%d" !atomic_incr_counter in
+ incr atomic_incr_counter; r;;
+
+ let print_profiling_logger oc id kind =
+ assert (kind >= 0);
+ assert (kind <= 1);
+ fprintf oc "%s begin profiling %a %d: atomic increment\n" comment
+ Profilingaux.pp_id id kind;
+ let ofs = profiling_offset id kind and lbl = next_profiling_label () in
+ fprintf oc " adrp x15, %s+%d\n" profiling_counter_table_name ofs;
+ fprintf oc " add x15, x15, :lo12:(%s+%d)\n" profiling_counter_table_name ofs;
+ fprintf oc "%s:\n" lbl;
+ fprintf oc " ldaxr x17, [x15]\n";
+ fprintf oc " add x17, x17, 1\n";
+ fprintf oc " stlxr w29, x17, [x15]\n";
+ fprintf oc " cbnz w29, %s\n" lbl;
+ fprintf oc "%s end profiling %a %d\n" comment
+ Profilingaux.pp_id id kind;;
+
(* Printing of instructions *)
let print_instruction oc = function
(* Branches *)
@@ -386,7 +397,9 @@ module Target(System: SYSTEM): TARGET =
(* the upper 32 bits of Xrd are set to 0, performing zero-extension *)
| Pldrsw(rd, a) ->
fprintf oc " ldrsw %a, %a\n" xreg rd addressing a
- | Pldp(rd1, rd2, a) ->
+ | Pldpw(rd1, rd2, _, _, a) ->
+ fprintf oc " ldp %a, %a, %a\n" wreg rd1 wreg rd2 addressing a
+ | Pldpx(rd1, rd2, _, _, a) ->
fprintf oc " ldp %a, %a, %a\n" xreg rd1 xreg rd2 addressing a
| Pstrw(rs, a) | Pstrw_a(rs, a) ->
fprintf oc " str %a, %a\n" wreg rs addressing a
@@ -396,7 +409,9 @@ module Target(System: SYSTEM): TARGET =
fprintf oc " strb %a, %a\n" wreg rs addressing a
| Pstrh(rs, a) ->
fprintf oc " strh %a, %a\n" wreg rs addressing a
- | Pstp(rs1, rs2, a) ->
+ | Pstpw(rs1, rs2, _, _, a) ->
+ fprintf oc " stp %a, %a, %a\n" wreg rs1 wreg rs2 addressing a
+ | Pstpx(rs1, rs2, _, _, a) ->
fprintf oc " stp %a, %a, %a\n" xreg rs1 xreg rs2 addressing a
(* Integer arithmetic, immediate *)
| Paddimm(sz, rd, r1, n) ->
@@ -520,12 +535,20 @@ module Target(System: SYSTEM): TARGET =
fprintf oc " str %a, %a\n" sreg rd addressing a
| Pstrd(rd, a) | Pstrd_a(rd, a) ->
fprintf oc " str %a, %a\n" dreg rd addressing a
+ | Pldps(rd1, rd2, _, _, a) ->
+ fprintf oc " ldp %a, %a, %a\n" sreg rd1 sreg rd2 addressing a
+ | Pldpd(rd1, rd2, _, _, a) ->
+ fprintf oc " ldp %a, %a, %a\n" dreg rd1 dreg rd2 addressing a
+ | Pstps(rd1, rd2, _, _, a) ->
+ fprintf oc " stp %a, %a, %a\n" sreg rd1 sreg rd2 addressing a
+ | Pstpd(rd1, rd2, _, _, a) ->
+ fprintf oc " stp %a, %a, %a\n" dreg rd1 dreg rd2 addressing a
(* Floating-point move *)
| Pfmov(rd, r1) ->
fprintf oc " fmov %a, %a\n" dreg rd dreg r1
| Pfmovimmd(rd, f) ->
let d = camlint64_of_coqint (Floats.Float.to_bits f) in
- if is_immediate_float64 d then
+ if is_immediate_float64 f then
fprintf oc " fmov %a, #%.7f\n" dreg rd (Int64.float_of_bits d)
else begin
let lbl = label_literal64 d in
@@ -534,7 +557,7 @@ module Target(System: SYSTEM): TARGET =
end
| Pfmovimms(rd, f) ->
let d = camlint_of_coqint (Floats.Float32.to_bits f) in
- if is_immediate_float32 d then
+ if is_immediate_float32 f then
fprintf oc " fmov %a, #%.7f\n" sreg rd (Int32.float_of_bits d)
else begin
let lbl = label_literal32 d in
@@ -596,8 +619,8 @@ module Target(System: SYSTEM): TARGET =
| Pfsel(rd, r1, r2, c) ->
fprintf oc " fcsel %a, %a, %a, %s\n" dreg rd dreg r1 dreg r2 (condition_name c)
(* No-op *)
- | Pnop ->
- fprintf oc " nop\n"
+ | Pnop -> ()
+ (*fprintf oc " nop\n"*)
(* Pseudo-instructions expanded in Asmexpand *)
| Pallocframe(sz, linkofs) -> assert false
| Pfreeframe(sz, linkofs) -> assert false
@@ -640,6 +663,8 @@ module Target(System: SYSTEM): TARGET =
fprintf oc "%s begin inline assembly\n\t" comment;
print_inline_asm preg_asm oc (camlstring_of_coqstring txt) sg args res;
fprintf oc "%s end inline assembly\n" comment
+ | EF_profiling (id, coq_kind) ->
+ print_profiling_logger oc id (Z.to_int coq_kind)
| _ ->
assert false
end
@@ -689,7 +714,24 @@ module Target(System: SYSTEM): TARGET =
section oc Section_text;
end
+ let aarch64_profiling_stub oc nr_items
+ profiling_id_table_name
+ profiling_counter_table_name =
+ fprintf oc " adrp x2, %s\n" profiling_counter_table_name;
+ fprintf oc " adrp x1, %s\n" profiling_id_table_name;
+ fprintf oc " add x2, x2, :lo12:%s\n" profiling_counter_table_name;
+ fprintf oc " add x1, x1, :lo12:%s\n" profiling_id_table_name;
+ fprintf oc " mov w0, %d\n" nr_items;
+ fprintf oc " b %s\n" profiling_write_table_helper ;;
+
+ let print_atexit oc to_be_called =
+ fprintf oc " adrp x0, %s\n" to_be_called;
+ fprintf oc " add x0, x0, :lo12:%s\n" to_be_called;
+ fprintf oc " b atexit\n";;
+
+
let print_epilogue oc =
+ print_profiling_epilogue elf_text_print_fun_info (Init_atexit print_atexit) aarch64_profiling_stub oc;
if !Clflags.option_g then begin
Debug.compute_gnu_file_enum (fun f -> ignore (print_file oc f));
section oc Section_text;
diff --git a/aarch64/ValueAOp.v b/aarch64/ValueAOp.v
index e0d98c85..e6a60d4e 100644
--- a/aarch64/ValueAOp.v
+++ b/aarch64/ValueAOp.v
@@ -96,8 +96,8 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Omul, v1::v2::nil => mul v1 v2
| Omuladd, v1::v2::v3::nil => add v1 (mul v2 v3)
| Omulsub, v1::v2::v3::nil => sub v1 (mul v2 v3)
- | Odiv, v1::v2::nil => divs v1 v2
- | Odivu, v1::v2::nil => divu v1 v2
+ | Odiv, v1::v2::nil => divs_total v1 v2
+ | Odivu, v1::v2::nil => divu_total v1 v2
| Oand, v1::v2::nil => and v1 v2
| Oandshift s a, v1::v2::nil => and v1 (eval_static_shift s v2 a)
| Oandimm n, v1::nil => and v1 (I n)
@@ -145,8 +145,8 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Omullsub, v1::v2::v3::nil => subl v1 (mull v2 v3)
| Omullhs, v1::v2::nil => mullhs v1 v2
| Omullhu, v1::v2::nil => mullhu v1 v2
- | Odivl, v1::v2::nil => divls v1 v2
- | Odivlu, v1::v2::nil => divlu v1 v2
+ | Odivl, v1::v2::nil => divls_total v1 v2
+ | Odivlu, v1::v2::nil => divlu_total v1 v2
| Oandl, v1::v2::nil => andl v1 v2
| Oandlshift s a, v1::v2::nil => andl v1 (eval_static_shiftl s v2 a)
| Oandlimm n, v1::nil => andl v1 (L n)
@@ -191,20 +191,20 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Osingleoffloat, v1::nil => singleoffloat v1
| Ofloatofsingle, v1::nil => floatofsingle v1
- | Ointoffloat, v1::nil => intoffloat v1
- | Ointuoffloat, v1::nil => intuoffloat v1
+ | Ointoffloat, v1::nil => intoffloat_total v1
+ | Ointuoffloat, v1::nil => intuoffloat_total v1
| Ofloatofint, v1::nil => floatofint v1
| Ofloatofintu, v1::nil => floatofintu v1
- | Ointofsingle, v1::nil => intofsingle v1
- | Ointuofsingle, v1::nil => intuofsingle v1
+ | Ointofsingle, v1::nil => intofsingle_total v1
+ | Ointuofsingle, v1::nil => intuofsingle_total v1
| Osingleofint, v1::nil => singleofint v1
| Osingleofintu, v1::nil => singleofintu v1
- | Olongoffloat, v1::nil => longoffloat v1
- | Olonguoffloat, v1::nil => longuoffloat v1
+ | Olongoffloat, v1::nil => longoffloat_total v1
+ | Olonguoffloat, v1::nil => longuoffloat_total v1
| Ofloatoflong, v1::nil => floatoflong v1
| Ofloatoflongu, v1::nil => floatoflongu v1
- | Olongofsingle, v1::nil => longofsingle v1
- | Olonguofsingle, v1::nil => longuofsingle v1
+ | Olongofsingle, v1::nil => longofsingle_total v1
+ | Olonguofsingle, v1::nil => longuofsingle_total v1
| Osingleoflong, v1::nil => singleoflong v1
| Osingleoflongu, v1::nil => singleoflongu v1
diff --git a/aarch64/extractionMachdep.v b/aarch64/extractionMachdep.v
index ae0006bc..0401d0fa 100644
--- a/aarch64/extractionMachdep.v
+++ b/aarch64/extractionMachdep.v
@@ -20,6 +20,9 @@ Require Archi Asm Asmgen SelectOp.
(* Archi *)
+
+Extract Constant Archi.pic_code => "fun () -> false". (* for the time being *)
+
Extract Constant Archi.abi =>
"match Configuration.abi with
| ""apple"" -> Apple
@@ -36,7 +39,4 @@ Extract Constant SelectOp.symbol_is_relocatable =>
Extract Constant Asm.symbol_low => "fun _ _ _ -> assert false".
Extract Constant Asm.symbol_high => "fun _ _ _ -> assert false".
-
-(* Asmgen *)
-
-Extract Constant Asmgen.symbol_is_aligned => "C2C.atom_is_aligned".
+Extract Constant Asmblockgen.symbol_is_aligned => "C2C.atom_is_aligned".
diff --git a/arm/Archi.v b/arm/Archi.v
index c4cb5496..ce96b2b4 100644
--- a/arm/Archi.v
+++ b/arm/Archi.v
@@ -97,3 +97,5 @@ Parameter abi: abi_kind.
(** Whether instructions added with Thumb2 are supported. True for ARMv6T2
and above. *)
Parameter thumb2_support: bool.
+
+Definition has_notrap_loads := false.
diff --git a/arm/AsmToJSON.ml b/arm/AsmToJSON.ml
index e850fed6..669d8c0c 100644
--- a/arm/AsmToJSON.ml
+++ b/arm/AsmToJSON.ml
@@ -177,6 +177,7 @@ let pp_instructions pp ic =
| EF_annot_val _
| EF_builtin _
| EF_debug _
+ | EF_profiling _
| EF_external _
| EF_free
| EF_malloc
diff --git a/arm/Asmexpand.ml b/arm/Asmexpand.ml
index 223cf7fc..b81ae74b 100644
--- a/arm/Asmexpand.ml
+++ b/arm/Asmexpand.ml
@@ -621,7 +621,7 @@ let expand_instruction instr =
| EF_memcpy(sz, al) ->
expand_builtin_memcpy (Int32.to_int (camlint_of_coqint sz))
(Int32.to_int (camlint_of_coqint al)) args
- | EF_annot _ | EF_debug _ | EF_inline_asm _ ->
+ | EF_annot _ | EF_debug _ | EF_inline_asm _ | EF_profiling _ ->
emit instr
| _ ->
assert false
diff --git a/arm/Asmgen.v b/arm/Asmgen.v
index 1a1e7f2f..f428feea 100644
--- a/arm/Asmgen.v
+++ b/arm/Asmgen.v
@@ -481,6 +481,9 @@ Definition transl_op
do r <- ireg_of res; do r1 <- ireg_of a1;
if Int.eq n Int.zero then
OK (Pmov r (SOreg r1) :: k)
+ else if Int.eq n Int.one then
+ OK (Padd IR14 r1 (SOlsr r1 (Int.repr 31)) ::
+ Pmov r (SOasr IR14 n) :: k)
else
OK (Pmov IR14 (SOasr r1 (Int.repr 31)) ::
Padd IR14 r1 (SOlsr IR14 (Int.sub Int.iwordsize n)) ::
@@ -689,8 +692,12 @@ Definition transl_memory_access_float
None
mk_immed addr args k.
-Definition transl_load (chunk: memory_chunk) (addr: addressing)
- (args: list mreg) (dst: mreg) (k: code) :=
+Definition transl_load (trap : trapping_mode)
+ (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (dst: mreg) (k: code) :=
+ match trap with
+ | NOTRAP => Error (msg "Asmgen.transl_load non-trapping loads unsupported on Arm")
+ | TRAP =>
match chunk with
| Mint8signed =>
transl_memory_access_int Pldrsb mk_immed_mem_small dst addr args k
@@ -708,6 +715,7 @@ Definition transl_load (chunk: memory_chunk) (addr: addressing)
transl_memory_access_float Pfldd mk_immed_mem_float dst addr args k
| _ =>
Error (msg "Asmgen.transl_load")
+ end
end.
Definition transl_store (chunk: memory_chunk) (addr: addressing)
@@ -747,8 +755,8 @@ Definition transl_instr (f: Mach.function) (i: Mach.instruction)
else loadind_int IR13 f.(fn_link_ofs) IR12 c)
| Mop op args res =>
transl_op op args res k
- | Mload chunk addr args dst =>
- transl_load chunk addr args dst k
+ | Mload trap chunk addr args dst =>
+ transl_load trap chunk addr args dst k
| Mstore chunk addr args src =>
transl_store chunk addr args src k
| Mcall sig (inl arg) =>
diff --git a/arm/Asmgenproof.v b/arm/Asmgenproof.v
index 93e0c6c2..67cfe0ae 100644
--- a/arm/Asmgenproof.v
+++ b/arm/Asmgenproof.v
@@ -303,6 +303,7 @@ Proof.
eapply tail_nolabel_trans. 2: eapply loadind_label; eauto. unfold loadind_int; TailNoLabel.
eapply transl_op_label; eauto.
unfold transl_load, transl_memory_access_int, transl_memory_access_float in H.
+ destruct t; try discriminate.
destruct m; monadInv H; eapply transl_memory_access_label; eauto; simpl; auto.
unfold transl_store, transl_memory_access_int, transl_memory_access_float in H.
destruct m; monadInv H; eapply transl_memory_access_label; eauto; simpl; auto.
@@ -618,6 +619,12 @@ Opaque loadind.
split. eapply agree_set_undef_mreg; eauto. congruence.
simpl; congruence.
+- (* Mload notrap1 *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
+- (* Mload notrap *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
- (* Mstore *)
assert (eval_addressing tge sp addr rs##args = Some a).
rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
diff --git a/arm/Asmgenproof1.v b/arm/Asmgenproof1.v
index 6f0482dc..729ddb4f 100644
--- a/arm/Asmgenproof1.v
+++ b/arm/Asmgenproof1.v
@@ -1264,15 +1264,32 @@ Local Transparent destroyed_by_op.
destruct (rs x0) eqn: X0; simpl in H0; try discriminate.
destruct (Int.ltu i (Int.repr 31)) eqn: LTU; inv H0.
revert EQ2. predSpec Int.eq Int.eq_spec i Int.zero; intros EQ2.
+ {
(* i = 0 *)
inv EQ2. econstructor.
split. apply exec_straight_one. simpl. reflexivity. auto.
split. Simpl. unfold Int.shrx. rewrite Int.shl_zero. unfold Int.divs.
change (Int.signed Int.one) with 1. rewrite Z.quot_1_r. rewrite Int.repr_signed. auto.
intros. Simpl.
- (* i <> 0 *)
- inv EQ2.
- assert (LTU': Int.ltu (Int.sub Int.iwordsize i) Int.iwordsize = true).
+ }
+ { (* i <> 0 *)
+ revert EQ2. predSpec Int.eq Int.eq_spec i Int.one; intros EQ2.
+ {
+ inv EQ2.
+ econstructor; split.
+ eapply exec_straight_two; simpl; reflexivity.
+ split.
+ { rewrite X0.
+ rewrite Int.shrx1_shr by reflexivity.
+ Simpl.
+ }
+ { intros.
+ Simpl.
+ }
+ }
+ clear H0.
+ inv EQ2.
+ assert (LTU': Int.ltu (Int.sub Int.iwordsize i) Int.iwordsize = true).
{
generalize (Int.ltu_inv _ _ LTU). intros.
unfold Int.sub, Int.ltu. rewrite Int.unsigned_repr_wordsize.
@@ -1306,6 +1323,7 @@ Local Transparent destroyed_by_op.
rewrite LTU'; simpl. rewrite LTU''; simpl.
f_equal. symmetry. apply Int.shrx_shr_2. assumption.
intros. unfold rs3; Simpl. unfold rs2; Simpl. unfold rs1; Simpl.
+ }
(* intoffloat *)
econstructor; split. apply exec_straight_one; simpl. rewrite H0; simpl. eauto. auto.
Transparent destroyed_by_op.
@@ -1540,8 +1558,8 @@ Proof.
Qed.
Lemma transl_load_correct:
- forall chunk addr args dst k c (rs: regset) a m v,
- transl_load chunk addr args dst k = OK c ->
+ forall trap chunk addr args dst k c (rs: regset) a m v,
+ transl_load trap chunk addr args dst k = OK c ->
eval_addressing ge (rs#SP) addr (map rs (map preg_of args)) = Some a ->
Mem.loadv chunk m a = Some v ->
exists rs',
@@ -1549,7 +1567,9 @@ Lemma transl_load_correct:
/\ rs'#(preg_of dst) = v
/\ forall r, data_preg r = true -> r <> preg_of dst -> rs'#r = rs#r.
Proof.
- intros. destruct chunk; simpl in H.
+ intros.
+ destruct trap; try (simpl in *; discriminate).
+ destruct chunk; simpl in H.
eapply transl_load_int_correct; eauto.
eapply transl_load_int_correct; eauto.
eapply transl_load_int_correct; eauto.
diff --git a/arm/BTL_SEsimplify.v b/arm/BTL_SEsimplify.v
new file mode 120000
index 00000000..f190e6d5
--- /dev/null
+++ b/arm/BTL_SEsimplify.v
@@ -0,0 +1 @@
+../aarch64/BTL_SEsimplify.v \ No newline at end of file
diff --git a/arm/CSE2deps.v b/arm/CSE2deps.v
new file mode 100644
index 00000000..4592f408
--- /dev/null
+++ b/arm/CSE2deps.v
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs (Int.unsigned ofs') chunk' (Int.unsigned ofs) chunk)
+ else true
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/arm/CSE2depsproof.v b/arm/CSE2depsproof.v
new file mode 100644
index 00000000..7dd0914e
--- /dev/null
+++ b/arm/CSE2depsproof.v
@@ -0,0 +1,211 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_size :
+ Ptrofs.wordsize = 32%nat.
+Proof.
+ unfold Ptrofs.wordsize.
+ unfold Wordsize_Ptrofs.wordsize.
+ trivial.
+Qed.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = 4294967296.
+Proof.
+ unfold Ptrofs.modulus.
+ rewrite ptrofs_size.
+ destruct Archi.ptr64; reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : int.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= Int.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Int.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Int.unsigned ofsw + size_chunk chunkw <= Int.unsigned ofsr
+ \/ Int.unsigned ofsr + size_chunk chunkr <= Int.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+ destruct base; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.of_int ofsr)) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.of_int ofsw)) as [OFSW | OFSW];
+ rewrite OFSW).
+
+ all: try rewrite ptrofs_modulus in *.
+
+ all: unfold Ptrofs.of_int.
+
+ all: repeat rewrite Ptrofs.unsigned_repr by (change Ptrofs.max_unsigned with 4294967295; lia).
+ all: intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs (Int.unsigned ofsr) chunkr (Int.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End MEMORY_WRITE.
+
+Section STACK_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Ainstack ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Ainstack ofsr) nil = Some addrr.
+
+ Lemma stack_load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+
+ destruct sp; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+
+ all: try rewrite ptrofs_modulus in *.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem stack_load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply stack_load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End STACK_WRITE.
+End SOUNDNESS.
+
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+- (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ simpl in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs (Int.unsigned i0) chunk' (Int.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ simpl in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+
+- (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply stack_load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/arm/Constantexpand.ml b/arm/Constantexpand.ml
index 408b291e..8cc32c1f 100644
--- a/arm/Constantexpand.ml
+++ b/arm/Constantexpand.ml
@@ -106,6 +106,7 @@ let estimate_size = function
| Pbuiltin (ef,_,_) ->
begin match ef with
| EF_inline_asm _ -> 256
+ | EF_profiling _ -> 40
| _ -> 0 end
| Pcfi_adjust _
| Pcfi_rel_offset _
diff --git a/arm/DuplicateOpcodeHeuristic.ml b/arm/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..41996028
--- /dev/null
+++ b/arm/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,36 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c | Ccompfs c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c | Cnotcompfs c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
+
diff --git a/arm/ExpansionOracle.ml b/arm/ExpansionOracle.ml
new file mode 120000
index 00000000..ee2674bf
--- /dev/null
+++ b/arm/ExpansionOracle.ml
@@ -0,0 +1 @@
+../aarch64/ExpansionOracle.ml \ No newline at end of file
diff --git a/arm/Machregs.v b/arm/Machregs.v
index ae0ff6bf..1ec8f0a1 100644
--- a/arm/Machregs.v
+++ b/arm/Machregs.v
@@ -153,6 +153,7 @@ Definition destroyed_by_builtin (ef: external_function): list mreg :=
match ef with
| EF_memcpy sz al => R2 :: R3 :: R12 :: F7 :: nil
| EF_inline_asm txt sg clob => destroyed_by_clobber clob
+ | EF_profiling _ _ => R2 :: R3 :: R12 :: nil
| _ => nil
end.
diff --git a/arm/Machregsaux.ml b/arm/Machregsaux.ml
index a4624a9d..24a33e9e 100644
--- a/arm/Machregsaux.ml
+++ b/arm/Machregsaux.ml
@@ -13,3 +13,8 @@
(** Auxiliary functions on machine registers *)
let is_scratch_register s = s = "R14" || s = "r14"
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong -> 0
+ | AST.Tfloat | AST.Tsingle -> 1
+ | AST.Tany32 | AST.Tany64 -> assert false
diff --git a/arm/Machregsaux.mli b/arm/Machregsaux.mli
index f3d52849..01b0f9fd 100644
--- a/arm/Machregsaux.mli
+++ b/arm/Machregsaux.mli
@@ -13,3 +13,5 @@
(** Auxiliary functions on machine registers *)
val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
diff --git a/arm/Op.v b/arm/Op.v
index 4739ef2e..68f6662d 100644
--- a/arm/Op.v
+++ b/arm/Op.v
@@ -518,6 +518,37 @@ Proof with (try exact I; try reflexivity).
unfold Val.select. destruct (eval_condition c vl m). apply Val.normalize_type. exact I.
Qed.
+
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Odiv | Odivu
+ | Oshrximm _
+ | Ointoffloat | Ointuoffloat
+ | Ointofsingle | Ointuofsingle
+ | Ofloatofint | Ofloatofintu
+ | Osingleofint | Osingleofintu => true
+ | _ => false
+ end.
+
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op; destruct eq_operation; intros; simpl in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+Qed.
End SOUNDNESS.
(** * Manipulating and transforming operations *)
@@ -687,7 +718,7 @@ Definition is_trivial_op (op: operation) : bool :=
(** Operations that depend on the memory state. *)
-Definition condition_depends_on_memory (c: condition) : bool :=
+Definition cond_depends_on_memory (c: condition) : bool :=
match c with
| Ccompu _ | Ccompushift _ _| Ccompuimm _ _ => true
| _ => false
@@ -695,14 +726,14 @@ Definition condition_depends_on_memory (c: condition) : bool :=
Definition op_depends_on_memory (op: operation) : bool :=
match op with
- | Ocmp c => condition_depends_on_memory c
- | Osel c ty => condition_depends_on_memory c
+ | Ocmp c => cond_depends_on_memory c
+ | Osel c ty => cond_depends_on_memory c
| _ => false
end.
-Lemma condition_depends_on_memory_correct:
+Lemma cond_depends_on_memory_correct:
forall c args m1 m2,
- condition_depends_on_memory c = false ->
+ cond_depends_on_memory c = false ->
eval_condition c args m1 = eval_condition c args m2.
Proof.
intros. destruct c; simpl; auto; discriminate.
@@ -714,12 +745,36 @@ Lemma op_depends_on_memory_correct:
eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
Proof.
intros until m2. destruct op; simpl; try congruence; intros C.
-- f_equal; f_equal; apply condition_depends_on_memory_correct; auto.
+- f_equal; f_equal; apply cond_depends_on_memory_correct; auto.
- destruct args; auto. destruct args; auto.
- rewrite (condition_depends_on_memory_correct c args m1 m2 C).
+ rewrite (cond_depends_on_memory_correct c args m1 m2 C).
auto.
Qed.
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op eqn:OP; simpl; try congruence.
+ - intros MEM; destruct c; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intro MEM; destruct c; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
(** Global variables mentioned in an operation or addressing mode *)
Definition globals_operation (op: operation) : list ident :=
@@ -975,6 +1030,20 @@ Proof.
apply Val.offset_ptr_inject; auto.
Qed.
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; simpl in *;
+ inv Hinjvl; trivial; try discriminate; inv H0; trivial; try discriminate; inv H2; trivial; try discriminate.
+Qed.
End EVAL_COMPAT.
(** Compatibility of the evaluation functions with the ``is less defined'' relation over values. *)
@@ -1080,6 +1149,19 @@ Proof.
destruct H1 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
Qed.
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros. rewrite val_inject_list_lessdef in H.
+ eapply eval_addressing_inj_none with (sp1 := sp).
+ intros. rewrite <- val_inject_lessdef; auto.
+ rewrite <- val_inject_lessdef; auto.
+ eauto. auto.
+Qed.
+
End EVAL_LESSDEF.
(** Compatibility of the evaluation functions with memory injections. *)
@@ -1132,6 +1214,19 @@ Proof.
econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
Qed.
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
Lemma eval_operation_inject:
forall op vl1 vl2 v1 m1 m2,
Val.inject_list f vl1 vl2 ->
diff --git a/arm/PrepassSchedulingOracle.ml b/arm/PrepassSchedulingOracle.ml
new file mode 120000
index 00000000..9885fd52
--- /dev/null
+++ b/arm/PrepassSchedulingOracle.ml
@@ -0,0 +1 @@
+../x86/PrepassSchedulingOracle.ml \ No newline at end of file
diff --git a/arm/SelectLong.vp b/arm/SelectLong.vp
index cc7a38f6..b4cdd0e3 100644
--- a/arm/SelectLong.vp
+++ b/arm/SelectLong.vp
@@ -16,6 +16,6 @@ Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats.
Require Import Op CminorSel.
-Require Import SelectOp SplitLong.
+Require Import OpHelpers SelectOp SplitLong.
(** This file is empty because we use the default implementation provided in [SplitLong]. *)
diff --git a/arm/SelectLongproof.v b/arm/SelectLongproof.v
index a82c082c..a65a38d4 100644
--- a/arm/SelectLongproof.v
+++ b/arm/SelectLongproof.v
@@ -16,6 +16,7 @@ Require Import String Coqlib Maps Integers Floats Errors.
Require Archi.
Require Import AST Values Memory Globalenvs Events.
Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectOpproof SplitLong SplitLongproof.
Require Import SelectLong.
diff --git a/arm/SelectOp.vp b/arm/SelectOp.vp
index 1220abc4..5506157c 100644
--- a/arm/SelectOp.vp
+++ b/arm/SelectOp.vp
@@ -39,7 +39,7 @@
Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats Builtins.
-Require Import Op CminorSel.
+Require Import Op OpHelpers CminorSel.
Local Open Scope cminorsel_scope.
@@ -516,6 +516,13 @@ Nondetfunction builtin_arg (e: expr) :=
| _ => BA e
end.
+(* floats *)
+Definition divf_base (e1: expr) (e2: expr) :=
+ Eop Odivf (e1 ::: e2 ::: Enil).
+
+Definition divfs_base (e1: expr) (e2: expr) :=
+ Eop Odivfs (e1 ::: e2 ::: Enil).
+
(** Platform-specific known builtins *)
Definition platform_builtin (b: platform_builtin) (args: exprlist) : option expr :=
diff --git a/arm/SelectOpproof.v b/arm/SelectOpproof.v
index bd9f01b1..e4e606bc 100644
--- a/arm/SelectOpproof.v
+++ b/arm/SelectOpproof.v
@@ -17,6 +17,7 @@ Require Import AST Integers Floats.
Require Import Values Memory Builtins Globalenvs.
Require Import Cminor Op CminorSel.
Require Import SelectOp.
+Require Import OpHelpers OpHelpersproof.
Local Open Scope cminorsel_scope.
Local Transparent Archi.ptr64.
@@ -69,8 +70,10 @@ Ltac TrivialExists :=
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -902,6 +905,27 @@ Proof.
- constructor; auto.
Qed.
+(* floating-point division without HELPERS *)
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ TrivialExists.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ TrivialExists.
+Qed.
+
(** Platform-specific known builtins *)
Theorem eval_platform_builtin:
diff --git a/arm/TargetPrinter.ml b/arm/TargetPrinter.ml
index 43dac44a..9269dd29 100644
--- a/arm/TargetPrinter.ml
+++ b/arm/TargetPrinter.ml
@@ -147,7 +147,9 @@ struct
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data(i) ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".section .rodata" i
@@ -202,6 +204,38 @@ struct
| SOasr(r, n) -> fprintf oc "%a, asr #%a" ireg r coqint n
| SOror(r, n) -> fprintf oc "%a, ror #%a" ireg r coqint n
+
+ let next_profiling_label =
+ let profiling_label_counter = ref 0 in
+ fun () ->
+ let r = sprintf ".Lprofiling_label%d" !profiling_label_counter in
+ incr profiling_label_counter; r;;
+
+ let print_profiling_logger oc id kind =
+ assert (kind >= 0);
+ assert (kind <= 1);
+ let ofs = profiling_offset id kind and olbl = next_profiling_label () in
+ fprintf oc "%s begin profiling %a %d: non-atomic increment\n" comment
+ Profilingaux.pp_id id kind;
+ fprintf oc " ldr r2, %s\n" olbl;
+ fprintf oc " ldr r3, [r2, #%d]\n"
+ (if Configuration.is_big_endian then 8 else 0);
+ fprintf oc " ldr r12, [r2, #%d]\n"
+ (if Configuration.is_big_endian then 0 else 8);
+ fprintf oc " adds r3, r3, #1\n";
+ fprintf oc " adc r12, r12, #0\n";
+ fprintf oc " str r3, [r2, #%d]\n"
+ (if Configuration.is_big_endian then 8 else 0);
+ fprintf oc " str r12, [r2, #%d]\n"
+ (if Configuration.is_big_endian then 0 else 8);
+ let jlbl = next_profiling_label () in
+ fprintf oc " b %s\n" jlbl;
+ fprintf oc "%s:\n" olbl;
+ fprintf oc " .word %s + %d\n" profiling_counter_table_name ofs;
+ fprintf oc "%s:\n" jlbl;
+ fprintf oc "%s end profiling %a %d\n" comment
+ Profilingaux.pp_id id kind;;
+
let print_instruction oc = function
(* Core instructions *)
| Padc (r1,r2,so) ->
@@ -482,6 +516,7 @@ struct
fprintf oc "%s begin inline assembly\n\t" comment;
print_inline_asm preg_asm oc (camlstring_of_coqstring txt) sg args res;
fprintf oc "%s end inline assembly\n" comment
+ | EF_profiling(id, coq_kind) -> print_profiling_logger oc id (Z.to_int coq_kind)
| _ ->
assert false
end
@@ -549,6 +584,11 @@ struct
if !Clflags.option_mthumb then
fprintf oc " .thumb_func\n"
+
+ let text_print_fun_info oc name =
+ fprintf oc " .type %s, %%function\n" name;
+ fprintf oc " .size %s, . - %s\n" name name
+
let print_fun_info oc name =
fprintf oc " .type %a, %%function\n" symbol name;
fprintf oc " .size %a, . - %a\n" symbol name symbol name
@@ -596,9 +636,22 @@ struct
if !Clflags.option_g then begin
section oc Section_text;
cfi_section oc
- end
+ end
+
+ let arm_profiling_stub oc nr_items
+ profiling_id_table_name
+ profiling_counter_table_name =
+ fprintf oc " ldr r2, = %s\n" profiling_counter_table_name;
+ fprintf oc " ldr r1, = %s\n" profiling_id_table_name;
+ fprintf oc " mov r0, #%d\n" nr_items;
+ fprintf oc " b %s\n" profiling_write_table_helper;;
+
+ let print_atexit oc to_be_called =
+ fprintf oc " ldr r0, = %s\n" to_be_called;
+ fprintf oc " b atexit\n";;
let print_epilogue oc =
+ print_profiling_epilogue text_print_fun_info (Init_atexit print_atexit) arm_profiling_stub oc;
if !Clflags.option_g then begin
Debug.compute_gnu_file_enum (fun f -> ignore (print_file oc f));
section oc Section_text;
diff --git a/backend/Allnontrap.v b/backend/Allnontrap.v
new file mode 100644
index 00000000..fedf14f7
--- /dev/null
+++ b/backend/Allnontrap.v
@@ -0,0 +1,38 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+
+
+Definition transf_ros (ros: reg + ident) : reg + ident := ros.
+
+Definition transf_instr (pc: node) (instr: instruction) :=
+ match instr with
+ | Iload trap chunk addr args dst s => Iload NOTRAP chunk addr args dst s
+ | _ => instr
+ end.
+
+Definition transf_function (f: function) : function :=
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map transf_instr f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
+
diff --git a/backend/Allnontrapproof.v b/backend/Allnontrapproof.v
new file mode 100644
index 00000000..157c5de2
--- /dev/null
+++ b/backend/Allnontrapproof.v
@@ -0,0 +1,227 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import FunInd.
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import Allnontrap.
+
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; reflexivity.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i,
+ f.(fn_code)!pc = Some i ->
+ (transf_function f).(fn_code)!pc = Some(transf_instr pc i).
+Proof.
+ intros until i. intro Hcode.
+ unfold transf_function; simpl.
+ rewrite PTree.gmap.
+ unfold option_map.
+ rewrite Hcode.
+ reflexivity.
+Qed.
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+ | match_frames_intro: forall res f sp pc rs,
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS; try TR_AT.
+- (* nop *)
+ econstructor; split. eapply exec_Inop; eauto.
+ constructor; auto.
+- (* op *)
+ econstructor; split.
+ eapply exec_Iop with (v := v); eauto.
+ rewrite <- H0. apply eval_operation_preserved. exact symbols_preserved.
+ constructor; auto.
+(* load *)
+- econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload; eauto.
+ constructor; auto.
+- (* load notrap1 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = None).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap1; eauto.
+ constructor; auto.
+- (* load notrap2 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap2; eauto.
+ constructor; auto.
+- (* store *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Istore; eauto.
+ constructor; auto.
+(* call *)
+- econstructor; split.
+ eapply exec_Icall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ constructor. constructor; auto. constructor.
+(* tailcall *)
+- econstructor; split.
+ eapply exec_Itailcall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ constructor. auto.
+(* builtin *)
+- econstructor; split.
+ eapply exec_Ibuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+(* cond *)
+- econstructor; split.
+ eapply exec_Icond; eauto.
+ constructor; auto.
+(* jumptbl *)
+- econstructor; split.
+ eapply exec_Ijumptable; eauto.
+ constructor; auto.
+(* return *)
+- econstructor; split.
+ eapply exec_Ireturn; eauto.
+ constructor; auto.
+(* internal function *)
+- simpl. econstructor; split.
+ eapply exec_function_internal; eauto.
+ constructor; auto.
+(* external function *)
+- econstructor; split.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+(* return *)
+- inv STACKS. inv H1.
+ econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/Allocation.v b/backend/Allocation.v
index 08e0a4f4..2323c050 100644
--- a/backend/Allocation.v
+++ b/backend/Allocation.v
@@ -58,7 +58,7 @@ Inductive block_shape: Type :=
(mv2: moves) (s: node)
| BSopdead (op: operation) (args: list reg) (res: reg)
(mv: moves) (s: node)
- | BSload (chunk: memory_chunk) (addr: addressing) (args: list reg) (dst: reg)
+ | BSload (trap : trapping_mode) (chunk: memory_chunk) (addr: addressing) (args: list reg) (dst: reg)
(mv1: moves) (args': list mreg) (dst': mreg)
(mv2: moves) (s: node)
| BSloaddead (chunk: memory_chunk) (addr: addressing) (args: list reg) (dst: reg)
@@ -226,15 +226,19 @@ Definition pair_instr_block
| operation_other _ _ =>
pair_Iop_block op args res s b
end
- | Iload chunk addr args dst s =>
+ | Iload trap chunk addr args dst s =>
let (mv1, b1) := extract_moves nil b in
match b1 with
- | Lload chunk' addr' args' dst' :: b2 =>
+ | Lload trap' chunk' addr' args' dst' :: b2 =>
+ assertion (trapping_mode_eq trap' trap);
if chunk_eq chunk Mint64 && Archi.splitlong then
+ (* TODO: do not support non trapping split loads *)
+ assertion (trapping_mode_eq trap TRAP);
assertion (chunk_eq chunk' Mint32);
let (mv2, b3) := extract_moves nil b2 in
match b3 with
- | Lload chunk'' addr'' args'' dst'' :: b4 =>
+ | Lload trap'' chunk'' addr'' args'' dst'' :: b4 =>
+ assertion (trapping_mode_eq trap'' TRAP);
let (mv3, b5) := extract_moves nil b4 in
assertion (chunk_eq chunk'' Mint32);
assertion (eq_addressing addr addr');
@@ -254,7 +258,7 @@ Definition pair_instr_block
assertion (chunk_eq chunk chunk');
assertion (eq_addressing addr addr');
assertion (check_succ s b3);
- Some(BSload chunk addr args dst mv1 args' dst' mv2 s))
+ Some(BSload trap chunk addr args dst mv1 args' dst' mv2 s))
| _ =>
assertion (check_succ s b1);
Some(BSloaddead chunk addr args dst mv1 s)
@@ -310,10 +314,10 @@ Definition pair_instr_block
Some(BSbuiltin ef args res mv1 args' res' mv2 s)
| _ => None
end
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 i =>
let (mv1, b1) := extract_moves nil b in
match b1 with
- | Lcond cond' args' s1' s2' :: b2 =>
+ | Lcond cond' args' s1' s2' i' :: b2 =>
assertion (eq_condition cond cond');
assertion (peq s1 s1');
assertion (peq s2 s2');
@@ -1023,7 +1027,7 @@ Definition transfer_aux (f: RTL.function) (env: regenv)
| BSopdead op args res mv s =>
assertion (reg_unconstrained res e);
track_moves env mv e
- | BSload chunk addr args dst mv1 args' dst' mv2 s =>
+ | BSload trap chunk addr args dst mv1 args' dst' mv2 s =>
do e1 <- track_moves env mv2 e;
do e2 <- transfer_use_def args dst args' dst' (destroyed_by_load chunk addr) e1;
track_moves env mv1 e2
@@ -1263,7 +1267,7 @@ Definition successors_block_shape (bsh: block_shape) : list node :=
| BShighlong src dst mv s => s :: nil
| BSop op args res mv1 args' res' mv2 s => s :: nil
| BSopdead op args res mv s => s :: nil
- | BSload chunk addr args dst mv1 args' dst' mv2 s => s :: nil
+ | BSload trap chunk addr args dst mv1 args' dst' mv2 s => s :: nil
| BSload2 addr addr' args dst mv1 args1' dst1' mv2 args2' dst2' mv3 s => s :: nil
| BSload2_1 addr args dst mv1 args' dst' mv2 s => s :: nil
| BSload2_2 addr addr' args dst mv1 args' dst' mv2 s => s :: nil
diff --git a/backend/Allocproof.v b/backend/Allocationproof.v
index 3fdbacbe..15cbdcdc 100644
--- a/backend/Allocproof.v
+++ b/backend/Allocationproof.v
@@ -96,44 +96,44 @@ Inductive expand_block_shape: block_shape -> RTL.instruction -> LTL.bblock -> Pr
expand_block_shape (BSopdead op args res mv s)
(Iop op args res s)
(expand_moves mv (Lbranch s :: k))
- | ebs_load: forall chunk addr args dst mv1 args' dst' mv2 s k,
+ | ebs_load: forall trap chunk addr args dst mv1 args' dst' mv2 s k,
wf_moves mv1 -> wf_moves mv2 ->
- expand_block_shape (BSload chunk addr args dst mv1 args' dst' mv2 s)
- (Iload chunk addr args dst s)
+ expand_block_shape (BSload trap chunk addr args dst mv1 args' dst' mv2 s)
+ (Iload trap chunk addr args dst s)
(expand_moves mv1
- (Lload chunk addr args' dst' :: expand_moves mv2 (Lbranch s :: k)))
+ (Lload trap chunk addr args' dst' :: expand_moves mv2 (Lbranch s :: k)))
| ebs_load2: forall addr addr2 args dst mv1 args1' dst1' mv2 args2' dst2' mv3 s k,
wf_moves mv1 -> wf_moves mv2 -> wf_moves mv3 ->
Archi.splitlong = true ->
offset_addressing addr 4 = Some addr2 ->
expand_block_shape (BSload2 addr addr2 args dst mv1 args1' dst1' mv2 args2' dst2' mv3 s)
- (Iload Mint64 addr args dst s)
+ (Iload TRAP Mint64 addr args dst s)
(expand_moves mv1
- (Lload Mint32 addr args1' dst1' ::
+ (Lload TRAP Mint32 addr args1' dst1' ::
expand_moves mv2
- (Lload Mint32 addr2 args2' dst2' ::
+ (Lload TRAP Mint32 addr2 args2' dst2' ::
expand_moves mv3 (Lbranch s :: k))))
| ebs_load2_1: forall addr args dst mv1 args' dst' mv2 s k,
wf_moves mv1 -> wf_moves mv2 ->
Archi.splitlong = true ->
expand_block_shape (BSload2_1 addr args dst mv1 args' dst' mv2 s)
- (Iload Mint64 addr args dst s)
+ (Iload TRAP Mint64 addr args dst s)
(expand_moves mv1
- (Lload Mint32 addr args' dst' ::
+ (Lload TRAP Mint32 addr args' dst' ::
expand_moves mv2 (Lbranch s :: k)))
| ebs_load2_2: forall addr addr2 args dst mv1 args' dst' mv2 s k,
wf_moves mv1 -> wf_moves mv2 ->
Archi.splitlong = true ->
offset_addressing addr 4 = Some addr2 ->
expand_block_shape (BSload2_2 addr addr2 args dst mv1 args' dst' mv2 s)
- (Iload Mint64 addr args dst s)
+ (Iload TRAP Mint64 addr args dst s)
(expand_moves mv1
- (Lload Mint32 addr2 args' dst' ::
+ (Lload TRAP Mint32 addr2 args' dst' ::
expand_moves mv2 (Lbranch s :: k)))
- | ebs_load_dead: forall chunk addr args dst mv s k,
+ | ebs_load_dead: forall trap chunk addr args dst mv s k,
wf_moves mv ->
expand_block_shape (BSloaddead chunk addr args dst mv s)
- (Iload chunk addr args dst s)
+ (Iload trap chunk addr args dst s)
(expand_moves mv (Lbranch s :: k))
| ebs_store: forall chunk addr args src mv1 args' src' s k,
wf_moves mv1 ->
@@ -169,11 +169,11 @@ Inductive expand_block_shape: block_shape -> RTL.instruction -> LTL.bblock -> Pr
(Ibuiltin ef args res s)
(expand_moves mv1
(Lbuiltin ef args' res' :: expand_moves mv2 (Lbranch s :: k)))
- | ebs_cond: forall cond args mv args' s1 s2 k,
+ | ebs_cond: forall cond args mv args' s1 s2 k i i',
wf_moves mv ->
expand_block_shape (BScond cond args mv args' s1 s2)
- (Icond cond args s1 s2)
- (expand_moves mv (Lcond cond args' s1 s2 :: k))
+ (Icond cond args s1 s2 i)
+ (expand_moves mv (Lcond cond args' s1 s2 i' :: k))
| ebs_jumptable: forall arg mv arg' tbl k,
wf_moves mv ->
expand_block_shape (BSjumptable arg mv arg' tbl)
@@ -1970,8 +1970,8 @@ Ltac UseShape :=
end.
Remark addressing_not_long:
- forall env f addr args dst s r,
- wt_instr f env (Iload Mint64 addr args dst s) -> Archi.splitlong = true ->
+ forall trap env f addr args dst s r,
+ wt_instr f env (Iload trap Mint64 addr args dst s) -> Archi.splitlong = true ->
In r args -> r <> dst.
Proof.
intros. inv H.
@@ -1981,7 +1981,7 @@ Proof.
{ rewrite <- H5. apply in_map; auto. }
assert (C: env r = Tint).
{ apply A in B. rewrite B. unfold Tptr. rewrite Archi.splitlong_ptr32 by auto. auto. }
- red; intros; subst r. rewrite C in H8; discriminate.
+ red; intros; subst r. rewrite C in H9; discriminate.
Qed.
(** The proof of semantic preservation is a simulation argument of the
@@ -2082,8 +2082,8 @@ Proof.
econstructor; eauto.
eapply wt_exec_Iop; eauto.
-(* load regular *)
-- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
+(* load regular TRAP *)
+- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
exploit (exec_moves mv1); eauto. intros [ls1 [A1 B1]].
exploit transfer_use_def_satisf; eauto. intros [X Y].
exploit eval_addressing_lessdef; eauto. intros [a' [F G]].
@@ -2100,7 +2100,7 @@ Proof.
econstructor; eauto.
(* load pair *)
-- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
+- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
exploit loadv_int64_split; eauto. intros (v1 & v2 & LOAD1 & LOAD2 & V1 & V2).
set (v2' := if Archi.big_endian then v2 else v1) in *.
set (v1' := if Archi.big_endian then v1 else v2) in *.
@@ -2155,7 +2155,7 @@ Proof.
econstructor; eauto.
(* load first word of a pair *)
-- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
+- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
exploit loadv_int64_split; eauto. intros (v1 & v2 & LOAD1 & LOAD2 & V1 & V2).
set (v2' := if Archi.big_endian then v2 else v1) in *.
set (v1' := if Archi.big_endian then v1 else v2) in *.
@@ -2185,7 +2185,7 @@ Proof.
econstructor; eauto.
(* load second word of a pair *)
-- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
+- generalize (wt_exec_Iload _ _ _ _ _ _ _ _ _ _ _ _ WTI H1 WTRS). intros WTRS'.
exploit loadv_int64_split; eauto. intros (v1 & v2 & LOAD1 & LOAD2 & V1 & V2).
set (v2' := if Archi.big_endian then v2 else v1) in *.
set (v1' := if Archi.big_endian then v1 else v2) in *.
@@ -2229,6 +2229,79 @@ Proof.
econstructor; eauto.
eapply wt_exec_Iload; eauto.
+- (* load notrap1 *)
+ generalize (wt_exec_Iload_notrap _ _ _ _ _ _ _ _ WTI WTRS).
+ intro WTRS'.
+ exploit (exec_moves mv1); eauto. intros [ls1 [A1 B1]].
+ exploit transfer_use_def_satisf; eauto. intros [X Y].
+ exploit eval_addressing_lessdef_none; eauto. intro Haddr.
+ exploit (exec_moves mv2); eauto. intros [ls2 [A2 B2]].
+ econstructor; split.
+ eapply plus_left. econstructor; eauto.
+ eapply star_trans. eexact A1.
+ eapply star_left. eapply exec_Lload_notrap1. rewrite <- Haddr.
+ apply eval_addressing_preserved. exact symbols_preserved. eauto.
+
+ eapply star_right. eexact A2. constructor.
+ eauto. eauto. eauto. traceEq.
+ exploit satisf_successors; eauto. simpl; eauto. intros [enext [U V]].
+ econstructor; eauto.
+
+(* load notrap1 dead *)
+- exploit exec_moves; eauto. intros [ls1 [X Y]].
+ econstructor; split.
+ eapply plus_left. econstructor; eauto.
+ eapply star_right. eexact X. econstructor; eauto.
+ eauto. traceEq.
+ exploit satisf_successors. eauto. eauto. simpl; eauto. eauto.
+ eapply reg_unconstrained_satisf; eauto.
+ intros [enext [U V]].
+ econstructor; eauto.
+ eapply wt_exec_Iload_notrap; eauto.
+
+(* load regular notrap2 *)
+- generalize (wt_exec_Iload_notrap _ _ _ _ _ _ _ _ WTI WTRS).
+ intro WTRS'.
+ exploit (exec_moves mv1); eauto. intros [ls1 [A1 B1]].
+ exploit transfer_use_def_satisf; eauto. intros [X Y].
+ exploit eval_addressing_lessdef; eauto. intros [a' [F G]].
+ destruct (Mem.loadv chunk m' a') as [v' |] eqn:Hload.
+ { exploit (exec_moves mv2 env (rs # dst <- Vundef)); eauto. intros [ls2 [A2 B2]].
+ econstructor; split.
+ eapply plus_left. econstructor; eauto.
+ eapply star_trans. eexact A1.
+ eapply star_left. econstructor. instantiate (1 := a'). rewrite <- F.
+ apply eval_addressing_preserved. exact symbols_preserved. eauto. eauto.
+ eapply star_right. eexact A2. constructor.
+ eauto. eauto. eauto. traceEq.
+ exploit satisf_successors; eauto. simpl; eauto. intros [enext [U V]].
+ econstructor; eauto.
+ }
+ { exploit (exec_moves mv2 env (rs # dst <- Vundef)); eauto. intros [ls2 [A2 B2]].
+ econstructor; split.
+ eapply plus_left. econstructor; eauto.
+ eapply star_trans. eexact A1.
+ eapply star_left. eapply exec_Lload_notrap2. rewrite <- F.
+ apply eval_addressing_preserved. exact symbols_preserved. assumption.
+ eauto.
+ eapply star_right. eexact A2. constructor.
+ eauto. eauto. eauto. traceEq.
+ exploit satisf_successors; eauto. simpl; eauto. intros [enext [U V]].
+ econstructor; eauto.
+ }
+
+- (* load notrap2 dead *)
+ exploit exec_moves; eauto. intros [ls1 [X Y]].
+ econstructor; split.
+ eapply plus_left. econstructor; eauto.
+ eapply star_right. eexact X. econstructor; eauto.
+ eauto. traceEq.
+ exploit satisf_successors. eauto. eauto. simpl; eauto. eauto.
+ eapply reg_unconstrained_satisf; eauto.
+ intros [enext [U V]].
+ econstructor; eauto.
+ eapply wt_exec_Iload_notrap; eauto.
+
(* store *)
- exploit exec_moves; eauto. intros [ls1 [X Y]].
exploit add_equations_lessdef; eauto. intros LD. simpl in LD. inv LD.
diff --git a/backend/Asmaux.v b/backend/Asmaux.v
new file mode 100644
index 00000000..1167c34c
--- /dev/null
+++ b/backend/Asmaux.v
@@ -0,0 +1,19 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Asm.
+Require Import AST.
+
+(* Constant only needed by Asmexpandaux.ml *)
+Definition dummy_function := {| fn_code := nil; fn_sig := signature_main |}.
diff --git a/backend/Asmexpandaux.ml b/backend/Asmexpandaux.ml
index f7feb303..1017ce26 100644
--- a/backend/Asmexpandaux.ml
+++ b/backend/Asmexpandaux.ml
@@ -15,6 +15,7 @@
pseudo-instructions *)
open Asm
+open Asmaux
open AST
open Camlcoq
@@ -26,7 +27,10 @@ let emit i = current_code := i :: !current_code
(* Generation of fresh labels *)
+(* now imported from Asmaux.ml
let dummy_function = { fn_code = []; fn_sig = signature_main }
+*)
+
let current_function = ref dummy_function
let next_label = ref (None: label option)
diff --git a/backend/Bounds.v b/backend/Bounds.v
index 4231d861..d6b67a02 100644
--- a/backend/Bounds.v
+++ b/backend/Bounds.v
@@ -67,7 +67,7 @@ Definition instr_within_bounds (i: instruction) :=
| Lgetstack sl ofs ty r => slot_within_bounds sl ofs ty /\ mreg_within_bounds r
| Lsetstack r sl ofs ty => slot_within_bounds sl ofs ty
| Lop op args res => mreg_within_bounds res
- | Lload chunk addr args dst => mreg_within_bounds dst
+ | Lload trap chunk addr args dst => mreg_within_bounds dst
| Lcall sig ros => size_arguments sig <= bound_outgoing b
| Lbuiltin ef args res =>
(forall r, In r (params_of_builtin_res res) \/ In r (destroyed_by_builtin ef) -> mreg_within_bounds r)
@@ -104,7 +104,7 @@ Definition record_regs_of_instr (u: RegSet.t) (i: instruction) : RegSet.t :=
| Lgetstack sl ofs ty r => record_reg u r
| Lsetstack r sl ofs ty => record_reg u r
| Lop op args res => record_reg u res
- | Lload chunk addr args dst => record_reg u dst
+ | Lload trap chunk addr args dst => record_reg u dst
| Lstore chunk addr args src => u
| Lcall sig ros => u
| Ltailcall sig ros => u
@@ -280,7 +280,7 @@ Definition defined_by_instr (r': mreg) (i: instruction) :=
match i with
| Lgetstack sl ofs ty r => r' = r
| Lop op args res => r' = res
- | Lload chunk addr args dst => r' = dst
+ | Lload trap chunk addr args dst => r' = dst
| Lbuiltin ef args res => In r' (params_of_builtin_res res) \/ In r' (destroyed_by_builtin ef)
| _ => False
end.
diff --git a/backend/CSE.v b/backend/CSE.v
index ecfa1f9e..838d96a6 100644
--- a/backend/CSE.v
+++ b/backend/CSE.v
@@ -459,8 +459,10 @@ Definition transfer (f: function) (approx: PMap.t VA.t) (pc: node) (before: numb
before
| Iop op args res s =>
add_op before res op args
- | Iload chunk addr args dst s =>
- add_load before dst chunk addr args
+ | Iload TRAP chunk addr args dst s =>
+ add_load before dst chunk addr args
+ | Iload NOTRAP _ _ _ dst _ =>
+ set_unknown before dst
| Istore chunk addr args src s =>
let app := approx!!pc in
let n := kill_loads_after_store app before chunk addr args in
@@ -491,10 +493,10 @@ Definition transfer (f: function) (approx: PMap.t VA.t) (pc: node) (before: numb
| _ =>
empty_numbering
end
- | EF_vload _ | EF_annot _ _ _ | EF_annot_val _ _ _ | EF_debug _ _ _ =>
+ | EF_vload _ | EF_annot _ _ _ | EF_annot_val _ _ _ | EF_debug _ _ _ | EF_profiling _ _ =>
set_res_unknown before res
end
- | Icond cond args ifso ifnot =>
+ | Icond cond args ifso ifnot _ =>
before
| Ijumptable arg tbl =>
before
@@ -534,23 +536,23 @@ Definition transf_instr (n: numbering) (instr: instruction) :=
let (op', args') := reduce _ combine_op n1 op args vl in
Iop op' args' res s
end
- | Iload chunk addr args dst s =>
+ | Iload TRAP chunk addr args dst s =>
let (n1, vl) := valnum_regs n args in
match find_rhs n1 (Load chunk addr vl) with
| Some r =>
Iop Omove (r :: nil) dst s
| None =>
let (addr', args') := reduce _ combine_addr n1 addr args vl in
- Iload chunk addr' args' dst s
+ Iload TRAP chunk addr' args' dst s
end
| Istore chunk addr args src s =>
let (n1, vl) := valnum_regs n args in
let (addr', args') := reduce _ combine_addr n1 addr args vl in
Istore chunk addr' args' src s
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 i =>
let (n1, vl) := valnum_regs n args in
let (cond', args') := reduce _ combine_cond n1 cond args vl in
- Icond cond' args' s1 s2
+ Icond cond' args' s1 s2 i
| _ =>
instr
end.
diff --git a/backend/CSE2.v b/backend/CSE2.v
new file mode 100644
index 00000000..3042645e
--- /dev/null
+++ b/backend/CSE2.v
@@ -0,0 +1,413 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+Replace available expressions by the register containing their value.
+
+David Monniaux, CNRS, VERIMAG
+ *)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps CSE2deps.
+
+(* Static analysis *)
+
+Inductive sym_val : Type :=
+| SMove (src : reg)
+| SOp (op : operation) (args : list reg)
+| SLoad (chunk : memory_chunk) (addr : addressing) (args : list reg).
+
+Definition eq_args (x y : list reg) : { x = y } + { x <> y } :=
+ list_eq_dec peq x y.
+
+Definition eq_sym_val : forall x y : sym_val,
+ {x = y} + { x <> y }.
+Proof.
+ generalize eq_operation.
+ generalize eq_args.
+ generalize peq.
+ generalize eq_addressing.
+ generalize chunk_eq.
+ decide equality.
+Defined.
+
+Module RELATION <: SEMILATTICE_WITHOUT_BOTTOM.
+
+Definition t := (PTree.t sym_val).
+Definition eq (r1 r2 : t) :=
+ forall x, (PTree.get x r1) = (PTree.get x r2).
+
+Definition top : t := PTree.empty sym_val.
+
+Lemma eq_refl: forall x, eq x x.
+Proof.
+ unfold eq.
+ intros; reflexivity.
+Qed.
+
+Lemma eq_sym: forall x y, eq x y -> eq y x.
+Proof.
+ unfold eq.
+ intros; eauto.
+Qed.
+
+Lemma eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+Proof.
+ unfold eq.
+ intros; congruence.
+Qed.
+
+Definition sym_val_beq (x y : sym_val) :=
+ if eq_sym_val x y then true else false.
+
+Definition beq (r1 r2 : t) := PTree.beq sym_val_beq r1 r2.
+
+Lemma beq_correct: forall r1 r2, beq r1 r2 = true -> eq r1 r2.
+Proof.
+ unfold beq, eq. intros r1 r2 EQ x.
+ pose proof (PTree.beq_correct sym_val_beq r1 r2) as CORRECT.
+ destruct CORRECT as [CORRECTF CORRECTB].
+ pose proof (CORRECTF EQ x) as EQx.
+ clear CORRECTF CORRECTB EQ.
+ unfold sym_val_beq in *.
+ destruct (r1 ! x) as [R1x | ] in *;
+ destruct (r2 ! x) as [R2x | ] in *;
+ trivial; try contradiction.
+ destruct (eq_sym_val R1x R2x) in *; congruence.
+Qed.
+
+Definition ge (r1 r2 : t) :=
+ forall x,
+ match PTree.get x r1 with
+ | None => True
+ | Some v => (PTree.get x r2) = Some v
+ end.
+
+Lemma ge_refl: forall r1 r2, eq r1 r2 -> ge r1 r2.
+Proof.
+ unfold eq, ge.
+ intros r1 r2 EQ x.
+ pose proof (EQ x) as EQx.
+ clear EQ.
+ destruct (r1 ! x).
+ - congruence.
+ - trivial.
+Qed.
+
+Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+Proof.
+ unfold ge.
+ intros r1 r2 r3 GE12 GE23 x.
+ pose proof (GE12 x) as GE12x; clear GE12.
+ pose proof (GE23 x) as GE23x; clear GE23.
+ destruct (r1 ! x); trivial.
+ destruct (r2 ! x); congruence.
+Qed.
+
+Definition lub (r1 r2 : t) :=
+ PTree.combine
+ (fun ov1 ov2 =>
+ match ov1, ov2 with
+ | (Some v1), (Some v2) =>
+ if eq_sym_val v1 v2
+ then ov1
+ else None
+ | None, _
+ | _, None => None
+ end)
+ r1 r2.
+
+Lemma ge_lub_left: forall x y, ge (lub x y) x.
+Proof.
+ unfold ge, lub.
+ intros r1 r2 x.
+ rewrite PTree.gcombine by reflexivity.
+ destruct (_ ! _); trivial.
+ destruct (_ ! _); trivial.
+ destruct (eq_sym_val _ _); trivial.
+Qed.
+
+Lemma ge_lub_right: forall x y, ge (lub x y) y.
+Proof.
+ unfold ge, lub.
+ intros r1 r2 x.
+ rewrite PTree.gcombine by reflexivity.
+ destruct (_ ! _); trivial.
+ destruct (_ ! _); trivial.
+ destruct (eq_sym_val _ _); trivial.
+ congruence.
+Qed.
+
+End RELATION.
+
+
+Module RB := ADD_BOTTOM(RELATION).
+Module DS := Dataflow_Solver(RB)(NodeSetForward).
+
+Definition kill_sym_val (dst : reg) (sv : sym_val) :=
+ match sv with
+ | SMove src => if peq dst src then true else false
+ | SOp op args => List.existsb (peq dst) args
+ | SLoad chunk addr args => List.existsb (peq dst) args
+ end.
+
+Definition kill_reg (dst : reg) (rel : RELATION.t) :=
+ PTree.filter1 (fun x => negb (kill_sym_val dst x))
+ (PTree.remove dst rel).
+
+Definition kill_sym_val_mem (sv: sym_val) :=
+ match sv with
+ | SMove _ => false
+ | SOp op _ => op_depends_on_memory op
+ | SLoad _ _ _ => true
+ end.
+
+Definition kill_sym_val_store chunk addr args (sv: sym_val) :=
+ match sv with
+ | SMove _ => false
+ | SOp op _ => op_depends_on_memory op
+ | SLoad chunk' addr' args' => may_overlap chunk addr args chunk' addr' args'
+ end.
+
+Definition kill_mem (rel : RELATION.t) :=
+ PTree.filter1 (fun x => negb (kill_sym_val_mem x)) rel.
+
+Definition forward_move (rel : RELATION.t) (x : reg) : reg :=
+ match rel ! x with
+ | Some (SMove org) => org
+ | _ => x
+ end.
+
+Definition kill_store1 chunk addr args rel :=
+ PTree.filter1 (fun x => negb (kill_sym_val_store chunk addr args x)) rel.
+
+Definition kill_store chunk addr args rel :=
+ kill_store1 chunk addr (List.map (forward_move rel) args) rel.
+
+Definition move (src dst : reg) (rel : RELATION.t) :=
+ PTree.set dst (SMove (forward_move rel src)) (kill_reg dst rel).
+
+Definition find_op_fold op args (already : option reg) x sv :=
+ match already with
+ | Some found => already
+ | None =>
+ match sv with
+ | (SOp op' args') =>
+ if (eq_operation op op') && (eq_args args args')
+ then Some x
+ else None
+ | _ => None
+ end
+ end.
+
+Definition find_op (rel : RELATION.t) (op : operation) (args : list reg) :=
+ PTree.fold (find_op_fold op args) rel None.
+
+Definition find_load_fold chunk addr args (already : option reg) x sv :=
+ match already with
+ | Some found => already
+ | None =>
+ match sv with
+ | (SLoad chunk' addr' args') =>
+ if (chunk_eq chunk chunk') &&
+ (eq_addressing addr addr') &&
+ (eq_args args args')
+ then Some x
+ else None
+ | _ => None
+ end
+ end.
+
+Definition find_load (rel : RELATION.t) (chunk : memory_chunk) (addr : addressing) (args : list reg) :=
+ PTree.fold (find_load_fold chunk addr args) rel None.
+
+Definition oper2 (op: operation) (dst : reg) (args : list reg)
+ (rel : RELATION.t) :=
+ let rel' := kill_reg dst rel in
+ PTree.set dst (SOp op (List.map (forward_move rel') args)) rel'.
+
+Definition oper1 (op: operation) (dst : reg) (args : list reg)
+ (rel : RELATION.t) :=
+ if List.in_dec peq dst args
+ then kill_reg dst rel
+ else oper2 op dst args rel.
+
+Definition oper (op: operation) (dst : reg) (args : list reg)
+ (rel : RELATION.t) :=
+ match find_op rel op (List.map (forward_move rel) args) with
+ | Some r => move r dst rel
+ | None => oper1 op dst args rel
+ end.
+
+Definition gen_oper (op: operation) (dst : reg) (args : list reg)
+ (rel : RELATION.t) :=
+ match op, args with
+ | Omove, src::nil => move src dst rel
+ | _, _ => oper op dst args rel
+ end.
+
+Definition load2 (chunk: memory_chunk) (addr : addressing)
+ (dst : reg) (args : list reg) (rel : RELATION.t) :=
+ let rel' := kill_reg dst rel in
+ PTree.set dst (SLoad chunk addr (List.map (forward_move rel') args)) rel'.
+
+Definition load1 (chunk: memory_chunk) (addr : addressing)
+ (dst : reg) (args : list reg) (rel : RELATION.t) :=
+ if List.in_dec peq dst args
+ then kill_reg dst rel
+ else load2 chunk addr dst args rel.
+
+Definition load (chunk: memory_chunk) (addr : addressing)
+ (dst : reg) (args : list reg) (rel : RELATION.t) :=
+ match find_load rel chunk addr (List.map (forward_move rel) args) with
+ | Some r => move r dst rel
+ | None => load1 chunk addr dst args rel
+ end.
+
+Definition kill_builtin_res res rel :=
+ match res with
+ | BR r => kill_reg r rel
+ | _ => rel
+ end.
+
+Definition apply_external_call ef (rel : RELATION.t) : RELATION.t :=
+ match ef with
+ | EF_builtin name sg
+ | EF_runtime name sg =>
+ match Builtins.lookup_builtin_function name sg with
+ | Some bf => rel
+ | None => kill_mem rel
+ end
+ | EF_malloc (* would need lessdef *)
+ | EF_external _ _
+ | EF_vstore _
+ | EF_free (* would need lessdef? *)
+ | EF_memcpy _ _ (* FIXME *)
+ | EF_inline_asm _ _ _ => kill_mem rel
+ | _ => rel
+ end.
+
+Definition apply_instr instr (rel : RELATION.t) : RB.t :=
+ match instr with
+ | Inop _
+ | Icond _ _ _ _ _
+ | Ijumptable _ _ => Some rel
+ | Istore chunk addr args _ _ => Some (kill_store chunk addr args rel)
+ | Iop op args dst _ => Some (gen_oper op dst args rel)
+ | Iload trap chunk addr args dst _ => Some (load chunk addr dst args rel)
+ | Icall _ _ _ dst _ => Some (kill_reg dst (kill_mem rel))
+ | Ibuiltin ef _ res _ => Some (kill_builtin_res res (apply_external_call ef rel))
+ | Itailcall _ _ _ | Ireturn _ => RB.bot
+ end.
+
+Definition apply_instr' code (pc : node) (ro : RB.t) : RB.t :=
+ match ro with
+ | None => None
+ | Some x =>
+ match code ! pc with
+ | None => RB.bot
+ | Some instr => apply_instr instr x
+ end
+ end.
+
+Definition forward_map (f : RTL.function) := DS.fixpoint
+ (RTL.fn_code f) RTL.successors_instr
+ (apply_instr' (RTL.fn_code f)) (RTL.fn_entrypoint f) (Some RELATION.top).
+
+Definition forward_move_b (rb : RB.t) (x : reg) :=
+ match rb with
+ | None => x
+ | Some rel => forward_move rel x
+ end.
+
+Definition subst_arg (fmap : option (PMap.t RB.t)) (pc : node) (x : reg) : reg :=
+ match fmap with
+ | None => x
+ | Some inv => forward_move_b (PMap.get pc inv) x
+ end.
+
+Definition subst_args fmap pc := List.map (subst_arg fmap pc).
+
+(* Transform *)
+Definition find_op_in_fmap fmap pc op args :=
+ match fmap with
+ | None => None
+ | Some map =>
+ match PMap.get pc map with
+ | Some rel => find_op rel op args
+ | None => None
+ end
+ end.
+
+Definition find_load_in_fmap fmap pc chunk addr args :=
+ match fmap with
+ | None => None
+ | Some map =>
+ match PMap.get pc map with
+ | Some rel => find_load rel chunk addr args
+ | None => None
+ end
+ end.
+
+Definition transf_instr (fmap : option (PMap.t RB.t))
+ (pc: node) (instr: instruction) :=
+ match instr with
+ | Iop op args dst s =>
+ let args' := subst_args fmap pc args in
+ match (if is_trivial_op op then None else find_op_in_fmap fmap pc op args') with
+ | None => Iop op args' dst s
+ | Some src => Iop Omove (src::nil) dst s
+ end
+ | Iload trap chunk addr args dst s =>
+ let args' := subst_args fmap pc args in
+ match find_load_in_fmap fmap pc chunk addr args' with
+ | None => Iload trap chunk addr args' dst s
+ | Some src => Iop Omove (src::nil) dst s
+ end
+ | Istore chunk addr args src s =>
+ Istore chunk addr (subst_args fmap pc args) (subst_arg fmap pc src) s
+ | Icall sig ros args dst s =>
+ Icall sig ros (subst_args fmap pc args) dst s
+ | Itailcall sig ros args =>
+ Itailcall sig ros (subst_args fmap pc args)
+ | Icond cond args s1 s2 i =>
+ Icond cond (subst_args fmap pc args) s1 s2 i
+ | Ijumptable arg tbl =>
+ Ijumptable (subst_arg fmap pc arg) tbl
+ | Ireturn (Some arg) =>
+ Ireturn (Some (subst_arg fmap pc arg))
+ | _ => instr
+ end.
+
+Definition transf_function (f: function) : function :=
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map (transf_instr (forward_map f)) f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
diff --git a/backend/CSE2proof.v b/backend/CSE2proof.v
new file mode 100644
index 00000000..252240c9
--- /dev/null
+++ b/backend/CSE2proof.v
@@ -0,0 +1,1755 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+Replace available expressions by the register containing their value.
+
+Proofs.
+
+David Monniaux, CNRS, VERIMAG
+ *)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps CSE2depsproof.
+Require Import Lia.
+
+Lemma args_unaffected:
+ forall rs : regset,
+ forall dst : reg,
+ forall v,
+ forall args : list reg,
+ existsb (fun y : reg => peq dst y) args = false ->
+ (rs # dst <- v ## args) = (rs ## args).
+Proof.
+ induction args; simpl; trivial.
+ destruct (peq dst a) as [EQ | NEQ]; simpl.
+ { discriminate.
+ }
+ intro EXIST.
+ f_equal.
+ {
+ apply Regmap.gso.
+ congruence.
+ }
+ apply IHargs.
+ assumption.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section SAME_MEMORY.
+ Variable m : mem.
+
+Definition sem_sym_val sym rs (v : option val) : Prop :=
+ match sym with
+ | SMove src => v = Some (rs # src)
+ | SOp op args =>
+ v = (eval_operation genv sp op (rs ## args) m)
+ | SLoad chunk addr args =>
+ match eval_addressing genv sp addr rs##args with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => v = Some dat
+ | None => v = None \/ v = Some Vundef
+ end
+ | None => v = None \/ v = Some Vundef
+ end
+ end.
+
+Definition sem_reg (rel : RELATION.t) (x : reg) (rs : regset) (v : val) : Prop :=
+ match rel ! x with
+ | None => True
+ | Some sym => sem_sym_val sym rs (Some (rs # x))
+ end.
+
+Definition sem_rel (rel : RELATION.t) (rs : regset) :=
+ forall x : reg, (sem_reg rel x rs (rs # x)).
+
+Definition sem_rel_b (relb : RB.t) (rs : regset) :=
+ match relb with
+ | Some rel => sem_rel rel rs
+ | None => False
+ end.
+
+Definition fmap_sem (fmap : option (PMap.t RB.t))
+ (pc : node) (rs : regset) :=
+ match fmap with
+ | None => True
+ | Some m => sem_rel_b (PMap.get pc m) rs
+ end.
+
+Lemma subst_arg_ok:
+ forall f,
+ forall pc,
+ forall rs,
+ forall arg,
+ fmap_sem (forward_map f) pc rs ->
+ rs # (subst_arg (forward_map f) pc arg) = rs # arg.
+Proof.
+ intros until arg.
+ intro SEM.
+ unfold fmap_sem in SEM.
+ destruct (forward_map f) as [map |]in *; trivial.
+ simpl.
+ unfold sem_rel_b, sem_rel, sem_reg in *.
+ destruct (map # pc).
+ 2: contradiction.
+ pose proof (SEM arg) as SEMarg.
+ simpl. unfold forward_move.
+ unfold sem_sym_val in *.
+ destruct (t ! arg); trivial.
+ destruct s; congruence.
+Qed.
+
+Lemma subst_args_ok:
+ forall f,
+ forall pc,
+ forall rs,
+ fmap_sem (forward_map f) pc rs ->
+ forall args,
+ rs ## (subst_args (forward_map f) pc args) = rs ## args.
+Proof.
+ induction args; trivial.
+ simpl.
+ f_equal.
+ apply subst_arg_ok; assumption.
+ assumption.
+Qed.
+
+Lemma kill_reg_sound :
+ forall rel : RELATION.t,
+ forall dst : reg,
+ forall rs,
+ forall v,
+ sem_rel rel rs ->
+ sem_rel (kill_reg dst rel) (rs # dst <- v).
+Proof.
+ unfold sem_rel, kill_reg, sem_reg, sem_sym_val.
+ intros until v.
+ intros REL x.
+ rewrite PTree.gfilter1.
+ destruct (Pos.eq_dec dst x).
+ {
+ subst x.
+ rewrite PTree.grs.
+ trivial.
+ }
+ rewrite PTree.gro by congruence.
+ rewrite Regmap.gso by congruence.
+ destruct (rel ! x) as [relx | ] eqn:RELx; trivial.
+ unfold kill_sym_val.
+ pose proof (REL x) as RELinstx.
+ rewrite RELx in RELinstx.
+ destruct relx eqn:SYMVAL.
+ {
+ destruct (peq dst src); simpl.
+ { reflexivity. }
+ rewrite Regmap.gso by congruence.
+ assumption.
+ }
+ { destruct existsb eqn:EXISTS; simpl.
+ { reflexivity. }
+ rewrite args_unaffected by exact EXISTS.
+ assumption.
+ }
+ { destruct existsb eqn:EXISTS; simpl.
+ { reflexivity. }
+ rewrite args_unaffected by exact EXISTS.
+ assumption.
+ }
+Qed.
+
+Lemma write_same:
+ forall rs : regset,
+ forall src dst : reg,
+ (rs # dst <- (rs # src)) # src = rs # src.
+Proof.
+ intros.
+ destruct (peq src dst).
+ {
+ subst dst.
+ apply Regmap.gss.
+ }
+ rewrite Regmap.gso by congruence.
+ reflexivity.
+Qed.
+
+Lemma move_sound :
+ forall rel : RELATION.t,
+ forall src dst : reg,
+ forall rs,
+ sem_rel rel rs ->
+ sem_rel (move src dst rel) (rs # dst <- (rs # src)).
+Proof.
+ intros until rs. intros REL x.
+ pose proof (kill_reg_sound rel dst rs (rs # src) REL x) as KILL.
+ pose proof (REL src) as RELsrc.
+ unfold move.
+ destruct (peq x dst).
+ {
+ subst x.
+ unfold sem_reg.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ unfold sem_reg in *.
+ simpl.
+ unfold forward_move.
+ destruct (rel ! src) as [ sv |]; simpl.
+ destruct sv eqn:SV; simpl in *.
+ {
+ destruct (peq dst src0).
+ {
+ subst src0.
+ rewrite Regmap.gss.
+ reflexivity.
+ }
+ rewrite Regmap.gso by congruence.
+ assumption.
+ }
+ all: f_equal; symmetry; apply write_same.
+ }
+ rewrite Regmap.gso by congruence.
+ unfold sem_reg.
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso in KILL by congruence.
+ exact KILL.
+Qed.
+
+Lemma move_cases_neq:
+ forall dst rel a,
+ a <> dst ->
+ (forward_move (kill_reg dst rel) a) <> dst.
+Proof.
+ intros until a. intro NEQ.
+ unfold kill_reg, forward_move.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by congruence.
+ destruct (rel ! a); simpl.
+ 2: congruence.
+ destruct s.
+ {
+ unfold kill_sym_val.
+ destruct peq; simpl; congruence.
+ }
+ all: simpl;
+ destruct negb; simpl; congruence.
+Qed.
+
+Lemma args_replace_dst :
+ forall rel,
+ forall args : list reg,
+ forall dst : reg,
+ forall rs : regset,
+ forall v,
+ (sem_rel rel rs) ->
+ not (In dst args) ->
+ (rs # dst <- v)
+ ## (map
+ (forward_move (kill_reg dst rel)) args) = rs ## args.
+Proof.
+ induction args; simpl.
+ 1: reflexivity.
+ intros until v.
+ intros REL NOT_IN.
+ rewrite IHargs by auto.
+ f_equal.
+ pose proof (REL a) as RELa.
+ rewrite Regmap.gso by (apply move_cases_neq; auto).
+ unfold kill_reg.
+ unfold sem_reg in RELa.
+ unfold forward_move.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by auto.
+ destruct (rel ! a); simpl; trivial.
+ destruct s; simpl in *; destruct negb; simpl; congruence.
+Qed.
+
+Lemma oper2_sound :
+ forall rel : RELATION.t,
+ forall op : operation,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall v,
+ sem_rel rel rs ->
+ not (In dst args) ->
+ eval_operation genv sp op (rs ## args) m = Some v ->
+ sem_rel (oper2 op dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL NOT_IN EVAL x.
+ pose proof (kill_reg_sound rel dst rs v REL x) as KILL.
+ unfold oper2.
+ destruct (peq x dst).
+ {
+ subst x.
+ unfold sem_reg.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ simpl.
+ rewrite args_replace_dst by auto.
+ symmetry.
+ assumption.
+ }
+ rewrite Regmap.gso by congruence.
+ unfold sem_reg.
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso in KILL by congruence.
+ exact KILL.
+Qed.
+
+Lemma oper1_sound :
+ forall rel : RELATION.t,
+ forall op : operation,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall v,
+ sem_rel rel rs ->
+ eval_operation genv sp op (rs ## args) m = Some v ->
+ sem_rel (oper1 op dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL EVAL.
+ unfold oper1.
+ destruct in_dec.
+ {
+ apply kill_reg_sound; auto.
+ }
+ apply oper2_sound; auto.
+Qed.
+
+Lemma find_op_sound :
+ forall rel : RELATION.t,
+ forall op : operation,
+ forall src : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ find_op rel op args = Some src ->
+ (eval_operation genv sp op (rs ## args) m) = Some (rs # src).
+Proof.
+ intros until rs.
+ unfold find_op.
+ rewrite PTree.fold_spec.
+ intro REL.
+ assert (
+ forall start,
+ match start with
+ | None => True
+ | Some src => eval_operation genv sp op rs ## args m = Some rs # src
+ end -> fold_left
+ (fun (a : option reg) (p : positive * sym_val) =>
+ find_op_fold op args a (fst p) (snd p)) (PTree.elements rel) start =
+ Some src ->
+ eval_operation genv sp op rs ## args m = Some rs # src) as REC.
+ {
+ unfold sem_rel, sem_reg in REL.
+ generalize (PTree.elements_complete rel).
+ generalize (PTree.elements rel).
+ induction l; simpl.
+ {
+ intros.
+ subst start.
+ assumption.
+ }
+ destruct a as [r sv]; simpl.
+ intros COMPLETE start GEN.
+ apply IHl.
+ {
+ intros.
+ apply COMPLETE.
+ right.
+ assumption.
+ }
+ unfold find_op_fold.
+ destruct start.
+ assumption.
+ destruct sv; trivial.
+ destruct eq_operation; trivial.
+ subst op0.
+ destruct eq_args; trivial.
+ subst args0.
+ simpl.
+ assert ((rel ! r) = Some (SOp op args)) as RELatr.
+ {
+ apply COMPLETE.
+ left.
+ reflexivity.
+ }
+ pose proof (REL r) as RELr.
+ rewrite RELatr in RELr.
+ simpl in RELr.
+ symmetry.
+ assumption.
+ }
+ apply REC; auto.
+Qed.
+
+
+Lemma find_load_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall src : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ find_load rel chunk addr args = Some src ->
+ match eval_addressing genv sp addr rs##args with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end.
+Proof.
+ intros until rs.
+ unfold find_load.
+ rewrite PTree.fold_spec.
+ intro REL.
+ assert (
+ forall start,
+ match start with
+ | None => True
+ | Some src =>
+ match eval_addressing genv sp addr rs##args with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end
+ end ->
+ fold_left
+ (fun (a : option reg) (p : positive * sym_val) =>
+ find_load_fold chunk addr args a (fst p) (snd p)) (PTree.elements rel) start =
+ Some src ->
+ match eval_addressing genv sp addr rs##args with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end) as REC.
+
+ {
+ unfold sem_rel, sem_reg in REL.
+ generalize (PTree.elements_complete rel).
+ generalize (PTree.elements rel).
+ induction l; simpl.
+ {
+ intros.
+ subst start.
+ assumption.
+ }
+ destruct a as [r sv]; simpl.
+ intros COMPLETE start GEN.
+ apply IHl.
+ {
+ intros.
+ apply COMPLETE.
+ right.
+ assumption.
+ }
+ unfold find_load_fold.
+ destruct start.
+ assumption.
+ destruct sv; trivial.
+ destruct chunk_eq; trivial.
+ subst chunk0.
+ destruct eq_addressing; trivial.
+ subst addr0.
+ destruct eq_args; trivial.
+ subst args0.
+ simpl.
+ assert ((rel ! r) = Some (SLoad chunk addr args)) as RELatr.
+ {
+ apply COMPLETE.
+ left.
+ reflexivity.
+ }
+ pose proof (REL r) as RELr.
+ rewrite RELatr in RELr.
+ simpl in RELr.
+ destruct eval_addressing.
+ { destruct Mem.loadv.
+ congruence.
+ destruct RELr; congruence.
+ }
+ destruct RELr; congruence.
+ }
+ apply REC; auto.
+Qed.
+
+
+Lemma find_load_sound' :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall src : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ forall v,
+ sem_rel rel rs ->
+ find_load rel chunk addr args = Some src ->
+ eval_addressing genv sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ v = rs # src.
+Proof.
+ intros until v. intros REL FINDLOAD ADDR LOAD.
+ pose proof (find_load_sound rel chunk addr src args rs REL FINDLOAD) as Z.
+ destruct eval_addressing in *.
+ {
+ replace a with v0 in * by congruence.
+ destruct Mem.loadv in * ; congruence.
+ }
+ discriminate.
+Qed.
+
+Lemma find_load_notrap1_sound' :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall src : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ find_load rel chunk addr args = Some src ->
+ eval_addressing genv sp addr rs##args = None ->
+ rs # src = Vundef.
+Proof.
+ intros until rs. intros REL FINDLOAD ADDR.
+ pose proof (find_load_sound rel chunk addr src args rs REL FINDLOAD) as Z.
+ rewrite ADDR in Z.
+ assumption.
+Qed.
+
+Lemma find_load_notrap2_sound' :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall src : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ sem_rel rel rs ->
+ find_load rel chunk addr args = Some src ->
+ eval_addressing genv sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = None ->
+ rs # src = Vundef.
+Proof.
+ intros until a. intros REL FINDLOAD ADDR LOAD.
+ pose proof (find_load_sound rel chunk addr src args rs REL FINDLOAD) as Z.
+ rewrite ADDR in Z.
+ destruct Mem.loadv.
+ discriminate.
+ assumption.
+Qed.
+
+Lemma forward_move_map:
+ forall rel args rs,
+ sem_rel rel rs ->
+ rs ## (map (forward_move rel) args) = rs ## args.
+Proof.
+ induction args; simpl; trivial.
+ intros rs REL.
+ f_equal.
+ 2: (apply IHargs; assumption).
+ unfold forward_move, sem_rel, sem_reg, sem_sym_val in *.
+ pose proof (REL a) as RELa.
+ destruct (rel ! a); trivial.
+ destruct s; congruence.
+Qed.
+
+
+Lemma forward_move_rs:
+ forall rel arg rs,
+ sem_rel rel rs ->
+ rs # (forward_move rel arg) = rs # arg.
+Proof.
+ unfold forward_move, sem_rel, sem_reg, sem_sym_val in *.
+ intros until rs.
+ intro REL.
+ pose proof (REL arg) as RELarg.
+ destruct (rel ! arg); trivial.
+ destruct s; congruence.
+Qed.
+
+Lemma oper_sound :
+ forall rel : RELATION.t,
+ forall op : operation,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall v,
+ sem_rel rel rs ->
+ eval_operation genv sp op (rs ## args) m = Some v ->
+ sem_rel (oper op dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL EVAL.
+ unfold oper.
+ destruct find_op eqn:FIND.
+ {
+ assert (eval_operation genv sp op rs ## (map (forward_move rel) args) m = Some rs # r) as FIND_OP.
+ {
+ apply (find_op_sound rel); trivial.
+ }
+ rewrite forward_move_map in FIND_OP by assumption.
+ replace v with (rs # r) by congruence.
+ apply move_sound; auto.
+ }
+ apply oper1_sound; trivial.
+Qed.
+
+Lemma gen_oper_sound :
+ forall rel : RELATION.t,
+ forall op : operation,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall v,
+ sem_rel rel rs ->
+ eval_operation genv sp op (rs ## args) m = Some v ->
+ sem_rel (gen_oper op dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL EVAL.
+ unfold gen_oper.
+ destruct op.
+ { destruct args as [ | h0 t0].
+ apply oper_sound; auto.
+ destruct t0.
+ {
+ simpl in *.
+ replace v with (rs # h0) by congruence.
+ apply move_sound; auto.
+ }
+ apply oper_sound; auto.
+ }
+ all: apply oper_sound; auto.
+Qed.
+
+
+Lemma load2_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ forall v,
+ sem_rel rel rs ->
+ not (In dst args) ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ sem_rel (load2 chunk addr dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL NOT_IN ADDR LOAD x.
+ pose proof (kill_reg_sound rel dst rs v REL x) as KILL.
+ unfold load2.
+ destruct (peq x dst).
+ {
+ subst x.
+ unfold sem_reg.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ simpl.
+ rewrite args_replace_dst by auto.
+ destruct eval_addressing.
+ {
+ replace a with v0 in * by congruence.
+ destruct Mem.loadv; congruence.
+ }
+ discriminate.
+ }
+ rewrite Regmap.gso by congruence.
+ unfold sem_reg.
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso in KILL by congruence.
+ exact KILL.
+Qed.
+
+Lemma load2_notrap1_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ not (In dst args) ->
+ eval_addressing genv sp addr (rs ## args) = None ->
+ sem_rel (load2 chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until rs.
+ intros REL NOT_IN ADDR x.
+ pose proof (kill_reg_sound rel dst rs Vundef REL x) as KILL.
+ unfold load2.
+ destruct (peq x dst).
+ {
+ subst x.
+ unfold sem_reg.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ simpl.
+ rewrite args_replace_dst by auto.
+ rewrite ADDR.
+ right.
+ trivial.
+ }
+ rewrite Regmap.gso by congruence.
+ unfold sem_reg.
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso in KILL by congruence.
+ exact KILL.
+Qed.
+
+Lemma load2_notrap2_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ sem_rel rel rs ->
+ not (In dst args) ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = None ->
+ sem_rel (load2 chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until a.
+ intros REL NOT_IN ADDR LOAD x.
+ pose proof (kill_reg_sound rel dst rs Vundef REL x) as KILL.
+ unfold load2.
+ destruct (peq x dst).
+ {
+ subst x.
+ unfold sem_reg.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ simpl.
+ rewrite args_replace_dst by auto.
+ rewrite ADDR.
+ rewrite LOAD.
+ right; trivial.
+ }
+ rewrite Regmap.gso by congruence.
+ unfold sem_reg.
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso in KILL by congruence.
+ exact KILL.
+Qed.
+
+Lemma load1_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ forall v,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ sem_rel (load1 chunk addr dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL ADDR LOAD.
+ unfold load1.
+ destruct in_dec.
+ {
+ apply kill_reg_sound; auto.
+ }
+ apply load2_sound with (a := a); auto.
+Qed.
+
+Lemma load1_notrap1_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = None ->
+ sem_rel (load1 chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until rs.
+ intros REL ADDR LOAD.
+ unfold load1.
+ destruct in_dec.
+ {
+ apply kill_reg_sound; auto.
+ }
+ apply load2_notrap1_sound; auto.
+Qed.
+
+Lemma load1_notrap2_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = None ->
+ sem_rel (load1 chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until a.
+ intros REL ADDR LOAD.
+ unfold load1.
+ destruct in_dec.
+ {
+ apply kill_reg_sound; auto.
+ }
+ apply load2_notrap2_sound with (a := a); auto.
+Qed.
+
+Lemma load_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ forall v,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ sem_rel (load chunk addr dst args rel) (rs # dst <- v).
+Proof.
+ intros until v.
+ intros REL ADDR LOAD.
+ unfold load.
+ destruct find_load as [src | ] eqn:FIND.
+ {
+ assert (match eval_addressing genv sp addr rs## (map (forward_move rel) args) with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end) as FIND_LOAD.
+ {
+ apply (find_load_sound rel); trivial.
+ }
+ rewrite forward_move_map in FIND_LOAD by assumption.
+ destruct eval_addressing in *.
+ 2: discriminate.
+ replace v0 with a in * by congruence.
+ destruct Mem.loadv in *.
+ 2: discriminate.
+ replace v with (rs # src) by congruence.
+ apply move_sound; auto.
+ }
+ apply load1_sound with (a := a); trivial.
+Qed.
+
+Lemma load_notrap1_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = None ->
+ sem_rel (load chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until rs.
+ intros REL ADDR.
+ unfold load.
+ destruct find_load as [src | ] eqn:FIND.
+ {
+ assert (match eval_addressing genv sp addr rs## (map (forward_move rel) args) with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end) as FIND_LOAD.
+ {
+ apply (find_load_sound rel); trivial.
+ }
+ rewrite forward_move_map in FIND_LOAD by assumption.
+ destruct eval_addressing in *.
+ discriminate.
+ rewrite <- FIND_LOAD.
+ apply move_sound; auto.
+ }
+ apply load1_notrap1_sound; trivial.
+Qed.
+
+Lemma load_notrap2_sound :
+ forall rel : RELATION.t,
+ forall chunk : memory_chunk,
+ forall addr : addressing,
+ forall dst : reg,
+ forall args: list reg,
+ forall rs : regset,
+ forall a,
+ sem_rel rel rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.loadv chunk m a = None ->
+ sem_rel (load chunk addr dst args rel) (rs # dst <- Vundef).
+Proof.
+ intros until a.
+ intros REL ADDR.
+ unfold load.
+ destruct find_load as [src | ] eqn:FIND.
+ {
+ assert (match eval_addressing genv sp addr rs## (map (forward_move rel) args) with
+ | Some a => match Mem.loadv chunk m a with
+ | Some dat => rs#src = dat
+ | None => rs#src = Vundef
+ end
+ | None => rs#src = Vundef
+ end) as FIND_LOAD.
+ {
+ apply (find_load_sound rel); trivial.
+ }
+ rewrite forward_move_map in FIND_LOAD by assumption.
+ rewrite ADDR in FIND_LOAD.
+ destruct Mem.loadv; intro.
+ discriminate.
+ rewrite <- FIND_LOAD.
+ apply move_sound; auto.
+ }
+ apply load1_notrap2_sound; trivial.
+Qed.
+
+Lemma kill_reg_weaken:
+ forall res mpc rs,
+ sem_rel mpc rs ->
+ sem_rel (kill_reg res mpc) rs.
+Proof.
+ intros until rs.
+ intros REL x.
+ pose proof (REL x) as RELx.
+ unfold kill_reg, sem_reg in *.
+ rewrite PTree.gfilter1.
+ destruct (peq res x).
+ { subst x.
+ rewrite PTree.grs.
+ reflexivity.
+ }
+ rewrite PTree.gro by congruence.
+ destruct (mpc ! x) as [sv | ]; trivial.
+ destruct negb; trivial.
+Qed.
+
+Lemma top_ok:
+ forall rs, sem_rel RELATION.top rs.
+Proof.
+ unfold sem_rel, sem_reg, RELATION.top.
+ intros.
+ rewrite PTree.gempty.
+ reflexivity.
+Qed.
+
+Lemma sem_rel_ge:
+ forall r1 r2 : RELATION.t,
+ (RELATION.ge r1 r2) ->
+ forall rs : regset,
+ (sem_rel r2 rs) -> (sem_rel r1 rs).
+Proof.
+ intros r1 r2 GE rs RE x.
+ pose proof (RE x) as REx.
+ pose proof (GE x) as GEx.
+ unfold sem_reg in *.
+ destruct (r1 ! x) as [r1x | ] in *;
+ destruct (r2 ! x) as [r2x | ] in *;
+ congruence.
+Qed.
+End SAME_MEMORY.
+
+Lemma kill_mem_sound :
+ forall m m' : mem,
+ forall rel : RELATION.t,
+ forall rs,
+ sem_rel m rel rs -> sem_rel m' (kill_mem rel) rs.
+Proof.
+ unfold sem_rel, sem_reg.
+ intros until rs.
+ intros SEM x.
+ pose proof (SEM x) as SEMx.
+ unfold kill_mem.
+ rewrite PTree.gfilter1.
+ unfold kill_sym_val_mem.
+ destruct (rel ! x) as [ sv | ].
+ 2: reflexivity.
+ destruct sv; simpl in *; trivial.
+ {
+ destruct op_depends_on_memory eqn:DEPENDS; simpl; trivial.
+ rewrite SEMx.
+ apply op_depends_on_memory_correct; auto.
+ }
+Qed.
+
+Lemma kill_store_sound :
+ forall m m' : mem,
+ forall rel : RELATION.t,
+ forall chunk addr args a v rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (Mem.storev chunk m a v) = Some m' ->
+ sem_rel m rel rs -> sem_rel m' (kill_store chunk addr args rel) rs.
+Proof.
+ unfold sem_rel, sem_reg.
+ intros until rs.
+ intros ADDR STORE SEM x.
+ pose proof (SEM x) as SEMx.
+ unfold kill_store, kill_store1.
+ rewrite PTree.gfilter1.
+ destruct (rel ! x) as [ sv | ].
+ 2: reflexivity.
+ destruct sv; simpl in *; trivial.
+ {
+ destruct op_depends_on_memory eqn:DEPENDS; simpl; trivial.
+ rewrite SEMx.
+ apply op_depends_on_memory_correct; auto.
+ }
+ destruct may_overlap eqn:OVERLAP; simpl; trivial.
+ destruct (eval_addressing genv sp addr0 rs ## args0) eqn:ADDR0.
+ {
+ erewrite may_overlap_sound with (args := (map (forward_move rel) args)).
+ all: try eassumption.
+
+ erewrite forward_move_map by eassumption.
+ assumption.
+ }
+ intuition congruence.
+Qed.
+
+Lemma kill_builtin_res_sound:
+ forall res (m : mem) (rs : regset) vres (rel : RELATION.t)
+ (REL : sem_rel m rel rs),
+ (sem_rel m (kill_builtin_res res rel) (regmap_setres res vres rs)).
+Proof.
+ destruct res; simpl; intros; trivial.
+ apply kill_reg_sound; trivial.
+Qed.
+End SOUNDNESS.
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun cu f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. apply match_transform_program; auto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; trivial.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall (f : function) (pc : node) (i : instruction),
+ (fn_code f)!pc = Some i ->
+ (fn_code (transf_function f))!pc =
+ Some(transf_instr (forward_map f) pc i).
+Proof.
+ intros until i. intro CODE.
+ unfold transf_function; simpl.
+ rewrite PTree.gmap.
+ unfold option_map.
+ rewrite CODE.
+ reflexivity.
+Qed.
+
+Definition is_killed_in_map (map : PMap.t RB.t) pc res :=
+ match PMap.get pc map with
+ | None => True
+ | Some rel => exists rel', RELATION.ge rel (kill_reg res rel')
+ end.
+
+Definition is_killed_in_fmap fmap pc res :=
+ match fmap with
+ | None => True
+ | Some map => is_killed_in_map map pc res
+ end.
+
+Lemma external_call_sound:
+ forall ef (rel : RELATION.t) sp (m m' : mem) (rs : regset) vargs t vres
+ (REL : sem_rel fundef unit ge sp m rel rs)
+ (CALL : external_call ef ge vargs m t vres m'),
+ sem_rel fundef unit ge sp m' (apply_external_call ef rel) rs.
+Proof.
+ destruct ef; intros; simpl in *.
+ all: eauto using kill_mem_sound.
+ all: unfold builtin_or_external_sem in *.
+ 1, 2: destruct (Builtins.lookup_builtin_function name sg);
+ eauto using kill_mem_sound;
+ inv CALL; eauto using kill_mem_sound.
+ all: inv CALL.
+ all: eauto using kill_mem_sound.
+Qed.
+
+Definition sem_rel_b' := sem_rel_b fundef unit ge.
+Definition fmap_sem' := fmap_sem fundef unit ge.
+Definition subst_arg_ok' := subst_arg_ok fundef unit ge.
+Definition subst_args_ok' := subst_args_ok fundef unit ge.
+Definition kill_mem_sound' := kill_mem_sound fundef unit ge.
+Definition kill_store_sound' := kill_store_sound fundef unit ge.
+
+Lemma sem_rel_b_ge:
+ forall rb1 rb2 : RB.t,
+ (RB.ge rb1 rb2) ->
+ forall sp m,
+ forall rs : regset,
+ (sem_rel_b' sp m rb2 rs) -> (sem_rel_b' sp m rb1 rs).
+Proof.
+ unfold sem_rel_b', sem_rel_b.
+ destruct rb1 as [r1 | ];
+ destruct rb2 as [r2 | ]; simpl;
+ intros GE sp m rs RE; try contradiction.
+ apply sem_rel_ge with (r2 := r2); assumption.
+Qed.
+
+Lemma apply_instr'_bot :
+ forall code,
+ forall pc,
+ RB.eq (apply_instr' code pc RB.bot) RB.bot.
+Proof.
+ reflexivity.
+Qed.
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+| match_frames_intro: forall res f sp pc rs,
+ (forall m : mem,
+ forall vres, (fmap_sem' sp m (forward_map f) pc rs # res <- vres)) ->
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ (fmap_sem' sp m (forward_map f) pc rs) ->
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS; try TR_AT.
+- (* nop *)
+ econstructor; split. eapply exec_Inop; eauto.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply sem_rel_b_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ unfold sem_rel_b in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+- (* op *)
+ unfold transf_instr in *.
+ destruct (if is_trivial_op op then None else find_op_in_fmap (forward_map f) pc op
+ (subst_args (forward_map f) pc args)) eqn:FIND_OP.
+ {
+ destruct (is_trivial_op op).
+ discriminate.
+ unfold find_op_in_fmap, fmap_sem', fmap_sem in *.
+ destruct (forward_map f) as [map |] eqn:MAP.
+ 2: discriminate.
+ change (@PMap.get (option RELATION.t) pc map) with (map # pc) in *.
+ destruct (map # pc) as [mpc | ] eqn:MPC.
+ 2: discriminate.
+ econstructor; split.
+ {
+ eapply exec_Iop with (v := v); eauto.
+ simpl.
+ rewrite <- subst_args_ok with (genv := ge) (f := f) (pc := pc) (sp := sp) (m := m) in H0.
+ {
+ rewrite MAP in H0.
+ rewrite find_op_sound with (rel := mpc) (src := r) in H0 by assumption.
+ assumption.
+ }
+ unfold fmap_sem. rewrite MAP. rewrite MPC. assumption.
+ }
+ constructor; eauto.
+ unfold fmap_sem', fmap_sem in *.
+ rewrite MAP.
+ apply sem_rel_b_ge with (rb2 := Some (gen_oper op res args mpc)).
+ {
+ replace (Some (gen_oper op res args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ unfold sem_rel_b', sem_rel_b.
+ apply gen_oper_sound; auto.
+ }
+ {
+ econstructor; split.
+ {
+ eapply exec_Iop with (v := v); eauto.
+ rewrite (subst_args_ok' sp m) by assumption.
+ rewrite <- H0.
+ apply eval_operation_preserved. exact symbols_preserved.
+ }
+ constructor; eauto.
+ unfold fmap_sem', fmap_sem in *.
+ unfold find_op_in_fmap, fmap_sem', fmap_sem in *.
+ destruct (forward_map f) as [map |] eqn:MAP.
+ 2: constructor.
+ change (@PMap.get (option RELATION.t) pc map) with (map # pc) in *.
+ destruct (map # pc) as [mpc | ] eqn:MPC.
+ 2: contradiction.
+
+ apply sem_rel_b_ge with (rb2 := Some (gen_oper op res args mpc)).
+ {
+ replace (Some (gen_oper op res args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ unfold sem_rel_b', sem_rel_b.
+ apply gen_oper_sound; auto.
+ }
+
+(* load *)
+- unfold transf_instr in *.
+ destruct find_load_in_fmap eqn:FIND_LOAD.
+ {
+ unfold find_load_in_fmap, fmap_sem', fmap_sem in *.
+ destruct (forward_map f) as [map |] eqn:MAP.
+ 2: discriminate.
+ change (@PMap.get (option RELATION.t) pc map) with (map # pc) in *.
+ destruct (map # pc) as [mpc | ] eqn:MPC.
+ 2: discriminate.
+ econstructor; split.
+ {
+ eapply exec_Iop with (v := v); eauto.
+ simpl.
+ rewrite <- subst_args_ok with (genv := ge) (f := f) (pc := pc) (sp := sp) (m := m) in H0.
+ {
+ f_equal.
+ symmetry.
+ rewrite MAP in H0.
+ eapply find_load_sound' with (genv := ge) (sp := sp) (addr := addr) (args := subst_args (Some map) pc args) (rel := mpc) (src := r) (rs := rs).
+ all: eassumption.
+ }
+ unfold fmap_sem. rewrite MAP. rewrite MPC. assumption.
+ }
+ constructor; eauto.
+ unfold fmap_sem', fmap_sem in *.
+ rewrite MAP.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ unfold sem_rel_b', sem_rel_b.
+ apply load_sound with (a := a); auto.
+ }
+ {
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0.
+ apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload; eauto.
+ rewrite (subst_args_ok' sp m); assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ apply load_sound with (a := a); assumption.
+ }
+
+- (* load notrap1 *)
+ unfold transf_instr in *.
+ destruct find_load_in_fmap eqn:FIND_LOAD.
+ {
+ unfold find_load_in_fmap, fmap_sem', fmap_sem in *.
+ destruct (forward_map f) as [map |] eqn:MAP.
+ 2: discriminate.
+ change (@PMap.get (option RELATION.t) pc map) with (map # pc) in *.
+ destruct (map # pc) as [mpc | ] eqn:MPC.
+ 2: discriminate.
+ econstructor; split.
+ {
+ eapply exec_Iop with (v := Vundef); eauto.
+ simpl.
+ rewrite <- subst_args_ok with (genv := ge) (f := f) (pc := pc) (sp := sp) (m := m) in H0.
+ {
+ f_equal.
+ rewrite MAP in H0.
+ eapply find_load_notrap1_sound' with (genv := ge) (sp := sp) (addr := addr) (args := subst_args (Some map) pc args) (rel := mpc) (src := r) (rs := rs).
+ all: eassumption.
+ }
+ unfold fmap_sem. rewrite MAP. rewrite MPC. assumption.
+ }
+ constructor; eauto.
+ unfold fmap_sem', fmap_sem in *.
+ rewrite MAP.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ unfold sem_rel_b', sem_rel_b.
+ apply load_notrap1_sound; auto.
+ }
+ {
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = None).
+ rewrite <- H0.
+ apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap1; eauto.
+ rewrite (subst_args_ok' sp m); assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ apply load_notrap1_sound; assumption.
+ }
+
+(* load notrap2 *)
+- unfold transf_instr in *.
+ destruct find_load_in_fmap eqn:FIND_LOAD.
+ {
+ unfold find_load_in_fmap, fmap_sem', fmap_sem in *.
+ destruct (forward_map f) as [map |] eqn:MAP.
+ 2: discriminate.
+ change (@PMap.get (option RELATION.t) pc map) with (map # pc) in *.
+ destruct (map # pc) as [mpc | ] eqn:MPC.
+ 2: discriminate.
+ econstructor; split.
+ {
+ eapply exec_Iop with (v := Vundef); eauto.
+ simpl.
+ rewrite <- subst_args_ok with (genv := ge) (f := f) (pc := pc) (sp := sp) (m := m) in H0.
+ {
+ f_equal.
+ rewrite MAP in H0.
+ eapply find_load_notrap2_sound' with (genv := ge) (sp := sp) (addr := addr) (args := subst_args (Some map) pc args) (rel := mpc) (src := r) (rs := rs).
+ all: try eassumption.
+ }
+ unfold fmap_sem. rewrite MAP. rewrite MPC. assumption.
+ }
+ constructor; eauto.
+ unfold fmap_sem', fmap_sem in *.
+ rewrite MAP.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ unfold sem_rel_b', sem_rel_b.
+ apply load_notrap2_sound with (a := a); auto.
+ }
+ {
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0.
+ apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap2; eauto.
+ rewrite (subst_args_ok' sp m); assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply sem_rel_b_ge with (rb2 := Some (load chunk addr dst args mpc)).
+ {
+ replace (Some (load chunk addr dst args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ simpl.
+ reflexivity.
+ }
+ apply load_notrap2_sound with (a := a); assumption.
+ }
+
+- (* store *)
+ econstructor. split.
+ {
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Istore; eauto.
+ - rewrite (subst_args_ok' sp m) by assumption.
+ eassumption.
+ - rewrite (subst_arg_ok' sp m) by assumption.
+ eassumption.
+ }
+
+ constructor; auto.
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply sem_rel_b_ge with (rb2 := Some (kill_store chunk addr args mpc)); trivial.
+ {
+ replace (Some (kill_store chunk addr args mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ unfold sem_rel_b in *.
+ rewrite MPC.
+ rewrite H.
+ reflexivity.
+ }
+ eapply (kill_store_sound' sp m); eassumption.
+
+(* call *)
+- econstructor; split.
+ eapply exec_Icall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ rewrite (subst_args_ok' sp m) by assumption.
+ constructor. constructor; auto.
+
+ constructor.
+ {
+ intros m' vres.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply sem_rel_b_ge with (rb2 := Some (kill_reg res (kill_mem mpc))).
+ {
+ replace (Some (kill_reg res (kill_mem mpc))) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_reg_sound.
+ apply (kill_mem_sound' sp m).
+ assumption.
+ }
+
+(* tailcall *)
+- econstructor; split.
+ eapply exec_Itailcall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ rewrite (subst_args_ok' (Vptr stk Ptrofs.zero) m) by assumption.
+ constructor. auto.
+
+(* builtin *)
+- econstructor; split.
+ eapply exec_Ibuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+
+ apply sem_rel_b_ge with (rb2 := Some (kill_builtin_res res (apply_external_call ef mpc))).
+ {
+ replace (Some (kill_builtin_res res (apply_external_call ef mpc))) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_builtin_res_sound.
+ eapply external_call_sound with (m := m); eassumption.
+
+(* cond *)
+- econstructor; split.
+ eapply exec_Icond; eauto.
+ rewrite (subst_args_ok' sp m); eassumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply sem_rel_b_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl.
+ destruct b; tauto.
+ }
+ unfold apply_instr'.
+ unfold sem_rel_b in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+
+(* jumptbl *)
+- econstructor; split.
+ eapply exec_Ijumptable; eauto.
+ rewrite (subst_arg_ok' sp m); eassumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply sem_rel_b_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl.
+ apply list_nth_z_in with (n := Int.unsigned n).
+ assumption.
+ }
+ unfold apply_instr'.
+ unfold sem_rel_b in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+
+(* return *)
+- destruct or as [arg | ].
+ {
+ econstructor; split.
+ eapply exec_Ireturn; eauto.
+ unfold regmap_optget.
+ rewrite (subst_arg_ok' (Vptr stk Ptrofs.zero) m) by eassumption.
+ constructor; auto.
+ }
+ econstructor; split.
+ eapply exec_Ireturn; eauto.
+ constructor; auto.
+
+
+(* internal function *)
+- simpl. econstructor; split.
+ eapply exec_function_internal; eauto.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem', fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply sem_rel_b_ge with (rb2 := Some RELATION.top).
+ {
+ eapply DS.fixpoint_entry with (code := fn_code f) (successors := successors_instr); try eassumption.
+ }
+ apply top_ok.
+
+(* external function *)
+- econstructor; split.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+
+(* return *)
+- inv STACKS. inv H1.
+ econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto.
+Qed.
+
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/CSE3.v b/backend/CSE3.v
new file mode 100644
index 00000000..2f73a1a7
--- /dev/null
+++ b/backend/CSE3.v
@@ -0,0 +1,151 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps CSE2deps.
+Require Import CSE3analysis HashedSet.
+Require Import RTLtyping.
+Require Compopts.
+
+Local Open Scope error_monad_scope.
+
+Axiom preanalysis : typing_env -> RTL.function -> invariants * analysis_hints.
+
+Record cse3params : Type :=
+ mkcse3params
+ { cse3_conditions : bool;
+ cse3_operations : bool;
+ cse3_trivial_ops: bool;
+ }.
+
+Section PARAMS.
+ Variable params : cse3params.
+
+Section REWRITE.
+ Context {ctx : eq_context}.
+
+Definition find_op_in_fmap fmap pc op args :=
+ match PMap.get pc fmap with
+ | Some rel => rhs_find (ctx:=ctx) pc (SOp op) args rel
+ | None => None
+ end.
+
+Definition find_load_in_fmap fmap pc chunk addr args :=
+ match PMap.get pc fmap with
+ | Some rel => rhs_find (ctx:=ctx) pc (SLoad chunk addr) args rel
+ | None => None
+ end.
+
+Definition forward_move_b (rb : RB.t) (x : reg) :=
+ match rb with
+ | None => x
+ | Some rel => forward_move (ctx := ctx) rel x
+ end.
+
+Definition subst_arg (fmap : PMap.t RB.t) (pc : node) (x : reg) : reg :=
+ forward_move_b (PMap.get pc fmap) x.
+
+Definition forward_move_l_b (rb : RB.t) (xl : list reg) :=
+ match rb with
+ | None => xl
+ | Some rel => forward_move_l (ctx := ctx) rel xl
+ end.
+
+Definition subst_args fmap pc xl :=
+ forward_move_l_b (PMap.get pc fmap) xl.
+
+Definition find_cond_in_fmap fmap pc cond args :=
+ if params.(cse3_conditions)
+ then
+ match PMap.get pc fmap with
+ | Some rel =>
+ if is_condition_present (ctx:=ctx) pc rel cond args
+ then Some true
+ else
+ let ncond := negate_condition cond in
+ if is_condition_present (ctx:=ctx) pc rel ncond args
+ then Some false
+ else let args' := subst_args fmap pc args in
+ if is_condition_present (ctx:=ctx) pc rel cond args'
+ then Some true
+ else if is_condition_present (ctx:=ctx) pc rel ncond args'
+ then Some false
+ else None
+ | None => None
+ end
+ else None.
+
+Definition param_transf_instr (fmap : PMap.t RB.t)
+ (pc: node) (instr: instruction) :=
+ match instr with
+ | Iop op args dst s =>
+ let args' := subst_args fmap pc args in
+ match (if (negb params.(cse3_operations) || ((negb params.(cse3_trivial_ops)) && (is_trivial_op op)))
+ then None else find_op_in_fmap fmap pc op args') with
+ | None => Iop op args' dst s
+ | Some src => Iop Omove (src::nil) dst s
+ end
+ | Iload trap chunk addr args dst s =>
+ let args' := subst_args fmap pc args in
+ match find_load_in_fmap fmap pc chunk addr args' with
+ | None => Iload trap chunk addr args' dst s
+ | Some src => Iop Omove (src::nil) dst s
+ end
+ | Istore chunk addr args src s =>
+ Istore chunk addr (subst_args fmap pc args) (subst_arg fmap pc src) s
+ | Icall sig ros args dst s =>
+ Icall sig ros (subst_args fmap pc args) dst s
+ | Itailcall sig ros args =>
+ Itailcall sig ros (subst_args fmap pc args)
+ | Icond cond args s1 s2 expected =>
+ let args' := subst_args fmap pc args in
+ match find_cond_in_fmap fmap pc cond args with
+ | None => Icond cond args' s1 s2 expected
+ | Some b => Inop (if b then s1 else s2)
+ end
+ | Ijumptable arg tbl =>
+ Ijumptable (subst_arg fmap pc arg) tbl
+ | Ireturn (Some arg) =>
+ Ireturn (Some (subst_arg fmap pc arg))
+ | _ => instr
+ end.
+End REWRITE.
+
+Definition param_transf_function (f: function) : res function :=
+ do tenv <- type_function f;
+ let (invariants, hints) := preanalysis tenv f in
+ let ctx := context_from_hints hints in
+ if check_inductiveness (ctx:=ctx) f tenv invariants
+ then
+ OK {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map (param_transf_instr (ctx := ctx) invariants)
+ f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}
+ else Error (msg "cse3: not inductive").
+
+Definition param_transf_fundef (fd: fundef) : res fundef :=
+ AST.transf_partial_fundef param_transf_function fd.
+
+Definition param_transf_program (p: program) : res program :=
+ transform_partial_program param_transf_fundef p.
+
+End PARAMS.
+
+Definition cmdline_params (_ : unit) :=
+ {| cse3_conditions := Compopts.optim_CSE3_conditions tt;
+ cse3_operations := true;
+ cse3_trivial_ops:= Compopts.optim_CSE3_trivial_ops tt |}.
+
+Definition transf_program p := param_transf_program (cmdline_params tt) p.
diff --git a/backend/CSE3analysis.v b/backend/CSE3analysis.v
new file mode 100644
index 00000000..9a6c9c0d
--- /dev/null
+++ b/backend/CSE3analysis.v
@@ -0,0 +1,566 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps CSE2deps.
+Require Import HashedSet.
+Require List Compopts.
+
+Definition typing_env := reg -> typ.
+
+Definition loadv_storev_compatible_type
+ (chunk : memory_chunk) (ty : typ) : bool :=
+ match chunk, ty with
+ | Mint32, Tint
+ | Mint64, Tlong
+ | Mfloat32, Tsingle
+ | Mfloat64, Tfloat => true
+ | _, _ => false
+ end.
+
+Module RELATION <: SEMILATTICE_WITHOUT_BOTTOM.
+ Definition t := PSet.t.
+ Definition eq (x : t) (y : t) := x = y.
+
+ Lemma eq_refl: forall x, eq x x.
+ Proof.
+ unfold eq. trivial.
+ Qed.
+
+ Lemma eq_sym: forall x y, eq x y -> eq y x.
+ Proof.
+ unfold eq. congruence.
+ Qed.
+
+ Lemma eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+ Proof.
+ unfold eq. congruence.
+ Qed.
+
+ Definition beq (x y : t) := if PSet.eq x y then true else false.
+
+ Lemma beq_correct: forall x y, beq x y = true -> eq x y.
+ Proof.
+ unfold beq.
+ intros.
+ destruct PSet.eq; congruence.
+ Qed.
+
+ Definition ge (x y : t) := (PSet.is_subset x y) = true.
+
+ Lemma ge_refl: forall x y, eq x y -> ge x y.
+ Proof.
+ unfold eq, ge.
+ intros.
+ subst y.
+ apply PSet.is_subset_spec.
+ trivial.
+ Qed.
+
+ Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+ Proof.
+ unfold ge.
+ intros.
+ rewrite PSet.is_subset_spec in *.
+ intuition.
+ Qed.
+
+ Definition lub x y :=
+ if Compopts.optim_CSE3_across_merges tt
+ then PSet.inter x y
+ else
+ if PSet.eq x y
+ then x
+ else PSet.empty.
+
+ Definition glb := PSet.union.
+
+ Lemma ge_lub_left: forall x y, ge (lub x y) x.
+ Proof.
+ unfold ge, lub.
+ intros.
+ destruct (Compopts.optim_CSE3_across_merges tt).
+ - apply PSet.is_subset_spec.
+ intro.
+ rewrite PSet.ginter.
+ rewrite andb_true_iff.
+ intuition.
+ - apply PSet.is_subset_spec.
+ intro.
+ destruct (PSet.eq x y).
+ + auto.
+ + rewrite PSet.gempty.
+ discriminate.
+ Qed.
+
+ Lemma ge_lub_right: forall x y, ge (lub x y) y.
+ Proof.
+ unfold ge, lub.
+ intros.
+ destruct (Compopts.optim_CSE3_across_merges tt).
+ - apply PSet.is_subset_spec.
+ intro.
+ rewrite PSet.ginter.
+ rewrite andb_true_iff.
+ intuition.
+ - apply PSet.is_subset_spec.
+ intro.
+ destruct (PSet.eq x y).
+ + subst. auto.
+ + rewrite PSet.gempty.
+ discriminate.
+ Qed.
+
+ Definition top := PSet.empty.
+End RELATION.
+
+Module RB := ADD_BOTTOM(RELATION).
+Module DS := Dataflow_Solver(RB)(NodeSetForward).
+
+Inductive sym_op : Type :=
+| SOp : operation -> sym_op
+| SLoad : memory_chunk -> addressing -> sym_op.
+
+Definition eq_dec_sym_op : forall s s' : sym_op, {s = s'} + {s <> s'}.
+Proof.
+ generalize eq_operation.
+ generalize eq_addressing.
+ generalize chunk_eq.
+ decide equality.
+Defined.
+
+Definition eq_dec_args : forall l l' : list reg, { l = l' } + { l <> l' }.
+Proof.
+ apply List.list_eq_dec.
+ exact peq.
+Defined.
+
+Inductive equation_or_condition :=
+| Equ : reg -> sym_op -> list reg -> equation_or_condition
+| Cond : condition -> list reg -> equation_or_condition.
+
+Definition eq_dec_equation :
+ forall eq eq' : equation_or_condition, {eq = eq'} + {eq <> eq'}.
+Proof.
+ generalize peq.
+ generalize eq_dec_sym_op.
+ generalize eq_dec_args.
+ generalize eq_condition.
+ decide equality.
+Defined.
+
+Definition eq_id := node.
+
+Definition add_i_j (i : reg) (j : eq_id) (m : Regmap.t PSet.t) :=
+ Regmap.set i (PSet.add j (Regmap.get i m)) m.
+
+Definition add_ilist_j (ilist : list reg) (j : eq_id) (m : Regmap.t PSet.t) :=
+ List.fold_left (fun already i => add_i_j i j already) ilist m.
+
+Definition get_reg_kills (eqs : PTree.t equation_or_condition) :
+ Regmap.t PSet.t :=
+ PTree.fold (fun already (eqno : eq_id) (eq_cond : equation_or_condition) =>
+ match eq_cond with
+ | Equ lhs sop args =>
+ add_i_j lhs eqno
+ (add_ilist_j args eqno already)
+ | Cond cond args => add_ilist_j args eqno already
+ end) eqs
+ (PMap.init PSet.empty).
+
+Definition eq_cond_depends_on_mem eq_cond :=
+ match eq_cond with
+ | Equ lhs sop args =>
+ match sop with
+ | SLoad _ _ => true
+ | SOp op => op_depends_on_memory op
+ end
+ | Cond cond args => cond_depends_on_memory cond
+ end.
+
+Definition eq_cond_depends_on_store eq_cond :=
+ match eq_cond with
+ | Equ _ (SLoad _ _) _ => true
+ | _ => false
+ end.
+
+Definition get_mem_kills (eqs : PTree.t equation_or_condition) : PSet.t :=
+ PTree.fold (fun already (eqno : eq_id) (eq : equation_or_condition) =>
+ if eq_cond_depends_on_mem eq
+ then PSet.add eqno already
+ else already) eqs PSet.empty.
+
+Definition get_store_kills (eqs : PTree.t equation_or_condition) : PSet.t :=
+ PTree.fold (fun already (eqno : eq_id) (eq : equation_or_condition) =>
+ if eq_cond_depends_on_store eq
+ then PSet.add eqno already
+ else already) eqs PSet.empty.
+
+Definition is_move (op : operation) :
+ { op = Omove } + { op <> Omove }.
+Proof.
+ destruct op; try (right ; congruence).
+ left; trivial.
+Qed.
+
+Definition is_smove (sop : sym_op) :
+ { sop = SOp Omove } + { sop <> SOp Omove }.
+Proof.
+ destruct sop; try (right ; congruence).
+ destruct (is_move o).
+ - left; congruence.
+ - right; congruence.
+Qed.
+
+Definition get_moves (eqs : PTree.t equation_or_condition) :
+ Regmap.t PSet.t :=
+ PTree.fold (fun already (eqno : eq_id) (eq : equation_or_condition) =>
+ match eq with
+ | Equ lhs sop args =>
+ if is_smove sop
+ then add_i_j lhs eqno already
+ else already
+ | _ => already
+ end) eqs (PMap.init PSet.empty).
+
+Record eq_context := mkeqcontext
+ { eq_catalog : eq_id -> option equation_or_condition;
+ eq_find_oracle : node -> equation_or_condition -> option eq_id;
+ eq_rhs_oracle : node -> sym_op -> list reg -> PSet.t;
+ eq_kill_reg : reg -> PSet.t;
+ eq_kill_mem : unit -> PSet.t;
+ eq_kill_store : unit -> PSet.t;
+ eq_moves : reg -> PSet.t }.
+
+Section OPERATIONS.
+ Context {ctx : eq_context}.
+
+ Definition kill_reg (r : reg) (rel : RELATION.t) : RELATION.t :=
+ PSet.subtract rel (eq_kill_reg ctx r).
+
+ Definition kill_mem (rel : RELATION.t) : RELATION.t :=
+ PSet.subtract rel (eq_kill_mem ctx tt).
+
+ Definition pick_source (l : list reg) := (* todo: take min? *)
+ match l with
+ | h::t => Some h
+ | nil => None
+ end.
+
+ Definition forward_move (rel : RELATION.t) (x : reg) : reg :=
+ match pick_source (PSet.elements (PSet.inter rel (eq_moves ctx x))) with
+ | None => x
+ | Some eqno =>
+ match eq_catalog ctx eqno with
+ | Some (Equ lhs sop args) =>
+ if is_smove sop && peq x lhs
+ then
+ match args with
+ | src::nil => src
+ | _ => x
+ end
+ else x
+ | _ => x
+ end
+ end.
+
+ Definition forward_move_l (rel : RELATION.t) : list reg -> list reg :=
+ List.map (forward_move rel).
+
+ Section PER_NODE.
+ Variable no : node.
+
+ Definition eq_find (eq : equation_or_condition) :=
+ match eq_find_oracle ctx no eq with
+ | Some id =>
+ match eq_catalog ctx id with
+ | Some eq' => if eq_dec_equation eq eq' then Some id else None
+ | None => None
+ end
+ | None => None
+ end.
+
+ Definition is_condition_present
+ (rel : RELATION.t) (cond : condition) (args : list reg) :=
+ match eq_find (Cond cond args) with
+ | Some id => PSet.contains rel id
+ | None => false
+ end.
+
+ Definition rhs_find (sop : sym_op) (args : list reg) (rel : RELATION.t) : option reg :=
+ match pick_source (PSet.elements (PSet.inter (eq_rhs_oracle ctx no sop args) rel)) with
+ | None => None
+ | Some src =>
+ match eq_catalog ctx src with
+ | Some (Equ eq_lhs eq_sop eq_args) =>
+ if eq_dec_sym_op sop eq_sop && eq_dec_args args eq_args
+ then Some eq_lhs
+ else None
+ | _ => None
+ end
+ end.
+
+ Definition oper2 (dst : reg) (op: sym_op)(args : list reg)
+ (rel : RELATION.t) : RELATION.t :=
+ match eq_find (Equ dst op args) with
+ | Some id =>
+ if PSet.contains rel id
+ then rel
+ else PSet.add id (kill_reg dst rel)
+ | None => kill_reg dst rel
+ end.
+
+ Definition oper1 (dst : reg) (op: sym_op) (args : list reg)
+ (rel : RELATION.t) : RELATION.t :=
+ if List.in_dec peq dst args
+ then kill_reg dst rel
+ else oper2 dst op args rel.
+
+
+ Definition move (src dst : reg) (rel : RELATION.t) : RELATION.t :=
+ if peq src dst
+ then rel
+ else
+ match eq_find (Equ dst (SOp Omove) (src::nil)) with
+ | Some eq_id => PSet.add eq_id (kill_reg dst rel)
+ | None => kill_reg dst rel
+ end.
+
+ Definition is_trivial_sym_op sop :=
+ match sop with
+ | SOp op => is_trivial_op op
+ | SLoad _ _ => false
+ end.
+
+ Definition oper (dst : reg) (op: sym_op) (args : list reg)
+ (rel : RELATION.t) : RELATION.t :=
+ if is_smove op
+ then
+ match args with
+ | src::nil =>
+ move (forward_move rel src) dst rel
+ | _ => kill_reg dst rel
+ end
+ else
+ let args' := forward_move_l rel args in
+ match rhs_find op args rel with
+ | Some r =>
+ if Compopts.optim_CSE3_glb tt
+ then RELATION.glb (move r dst rel)
+ (RELATION.glb
+ (oper1 dst op args rel)
+ (oper1 dst op args' rel))
+ else RELATION.glb
+ (oper1 dst op args rel)
+ (oper1 dst op args' rel)
+ | None => RELATION.glb
+ (oper1 dst op args rel)
+ (oper1 dst op args' rel)
+ end.
+
+ Definition kill_store (rel : RELATION.t) : RELATION.t :=
+ PSet.subtract rel (eq_kill_store ctx tt).
+
+ Definition clever_kill_store
+ (chunk : memory_chunk) (addr: addressing) (args : list reg)
+ (src : reg)
+ (rel : RELATION.t) : RELATION.t :=
+ PSet.subtract rel
+ (PSet.filter
+ (fun eqno =>
+ match eq_catalog ctx eqno with
+ | Some (Equ eq_lhs eq_sop eq_args) =>
+ match eq_sop with
+ | SOp op => true
+ | SLoad chunk' addr' =>
+ may_overlap chunk addr args chunk' addr' eq_args
+ end
+ | _ => false
+ end)
+ (PSet.inter rel (eq_kill_store ctx tt))).
+
+ Definition store2
+ (chunk : memory_chunk) (addr: addressing) (args : list reg)
+ (src : reg)
+ (rel : RELATION.t) : RELATION.t :=
+ if Compopts.optim_CSE3_alias_analysis tt
+ then clever_kill_store chunk addr args src rel
+ else kill_store rel.
+
+ Definition store1
+ (chunk : memory_chunk) (addr: addressing) (args : list reg)
+ (src : reg) (ty: typ)
+ (rel : RELATION.t) : RELATION.t :=
+ let rel' := store2 chunk addr args src rel in
+ if loadv_storev_compatible_type chunk ty
+ then
+ match eq_find (Equ src (SLoad chunk addr) args) with
+ | Some id => PSet.add id rel'
+ | None => rel'
+ end
+ else rel'.
+
+ Definition store (tenv : typing_env)
+ (chunk : memory_chunk) (addr: addressing) (args : list reg)
+ (src : reg)
+ (rel : RELATION.t) : RELATION.t :=
+ let args' := forward_move_l rel args in
+ let src' := forward_move rel src in
+ let tsrc := tenv src in
+ let tsrc' := tenv src' in
+ RELATION.glb
+ (RELATION.glb
+ (store1 chunk addr args src tsrc rel)
+ (store1 chunk addr args' src tsrc rel))
+ (RELATION.glb
+ (store1 chunk addr args src' tsrc' rel)
+ (store1 chunk addr args' src' tsrc' rel)).
+
+ Definition kill_builtin_res res rel :=
+ match res with
+ | BR r => kill_reg r rel
+ | _ => rel
+ end.
+
+ Definition apply_external_call ef (rel : RELATION.t) : RELATION.t :=
+ match ef with
+ | EF_builtin name sg =>
+ match Builtins.lookup_builtin_function name sg with
+ | Some bf => rel
+ | None => if Compopts.optim_CSE3_across_calls tt
+ then kill_mem rel
+ else RELATION.top
+ end
+ | EF_runtime name sg =>
+ if Compopts.optim_CSE3_across_calls tt
+ then
+ match Builtins.lookup_builtin_function name sg with
+ | Some bf => rel
+ | None => kill_mem rel
+ end
+ else RELATION.top
+ | EF_malloc
+ | EF_external _ _
+ | EF_free =>
+ if Compopts.optim_CSE3_across_calls tt
+ then kill_mem rel
+ else RELATION.top
+ | EF_vstore _
+ | EF_memcpy _ _ (* FIXME *)
+ | EF_inline_asm _ _ _ => kill_mem rel
+ | _ => rel
+ end.
+
+ Definition apply_cond1 cond args (rel : RELATION.t) : RB.t :=
+ match eq_find (Cond (negate_condition cond) args) with
+ | Some eq_id =>
+ if PSet.contains rel eq_id
+ then RB.bot
+ else Some rel
+ | None => Some rel
+ end.
+
+ Definition apply_cond0 cond args (rel : RELATION.t) : RELATION.t :=
+ match eq_find (Cond cond args) with
+ | Some eq_id => PSet.add eq_id rel
+ | None => rel
+ end.
+
+ Definition apply_cond cond args (rel : RELATION.t) : RB.t :=
+ match apply_cond1 cond args rel with
+ | Some rel => Some (apply_cond0 cond args rel)
+ | None => RB.bot
+ end.
+
+ Definition apply_instr (tenv : typing_env) (instr : RTL.instruction) (rel : RELATION.t) : list (node * RB.t) :=
+ match instr with
+ | Inop pc' => (pc', (Some rel))::nil
+ | Icond cond args ifso ifnot _ =>
+ (ifso, (apply_cond cond args rel))::
+ (ifnot, (apply_cond (negate_condition cond) args rel))::nil
+ | Ijumptable _ targets => List.map (fun pc' => (pc', (Some rel))) targets
+ | Istore chunk addr args src pc' =>
+ (pc', (Some (store tenv chunk addr args src rel)))::nil
+ | Iop op args dst pc' => (pc', (Some (oper dst (SOp op) args rel)))::nil
+ | Iload trap chunk addr args dst pc' => (pc', (Some (oper dst (SLoad chunk addr) args rel)))::nil
+ | Icall _ _ _ dst pc' => (pc', (Some (kill_reg dst (kill_mem rel))))::nil
+ | Ibuiltin ef _ res pc' => (pc', (Some (kill_builtin_res res (apply_external_call ef rel))))::nil
+ | Itailcall _ _ _ | Ireturn _ => nil
+ end.
+ End PER_NODE.
+
+Definition apply_instr' (tenv : typing_env) code (pc : node) (ro : RB.t) :
+ list (node * RB.t) :=
+ match code ! pc with
+ | None => nil
+ | Some instr =>
+ match ro with
+ | None => List.map (fun pc' => (pc', RB.bot)) (successors_instr instr)
+ | Some x => apply_instr pc tenv instr x
+ end
+ end.
+
+Definition invariants := PMap.t RB.t.
+
+Definition rel_leb (x y : RELATION.t) : bool := (PSet.is_subset y x).
+
+Definition relb_leb (x y : RB.t) : bool :=
+ match x, y with
+ | None, _ => true
+ | (Some _), None => false
+ | (Some x), (Some y) => rel_leb x y
+ end.
+
+Definition check_inductiveness (fn : RTL.function) (tenv: typing_env) (inv: invariants) :=
+ (RB.beq (Some RELATION.top) (PMap.get (fn_entrypoint fn) inv)) &&
+ PTree_Properties.for_all (fn_code fn)
+ (fun pc instr =>
+ match PMap.get pc inv with
+ | None => true
+ | Some rel =>
+ List.forallb
+ (fun szz =>
+ relb_leb (snd szz) (PMap.get (fst szz) inv))
+ (apply_instr pc tenv instr rel)
+ end).
+(* No longer used. Incompatible with transfer functions that yield a different result depending on the successor.
+
+Definition internal_analysis
+ (tenv : typing_env)
+ (f : RTL.function) : option invariants := DS.fixpoint
+ (RTL.fn_code f) RTL.successors_instr
+ (apply_instr' tenv (RTL.fn_code f)) (RTL.fn_entrypoint f) (Some RELATION.top).
+*)
+End OPERATIONS.
+
+Record analysis_hints :=
+ mkanalysis_hints
+ { hint_eq_catalog : PTree.t equation_or_condition;
+ hint_eq_find_oracle : node -> equation_or_condition -> option eq_id;
+ hint_eq_rhs_oracle : node -> sym_op -> list reg -> PSet.t }.
+
+Definition context_from_hints (hints : analysis_hints) :=
+ let eqs := hint_eq_catalog hints in
+ let reg_kills := get_reg_kills eqs in
+ let mem_kills := get_mem_kills eqs in
+ let store_kills := get_store_kills eqs in
+ let moves := get_moves eqs in
+ {|
+ eq_catalog := fun eq_id => PTree.get eq_id eqs;
+ eq_find_oracle := hint_eq_find_oracle hints ;
+ eq_rhs_oracle := hint_eq_rhs_oracle hints;
+ eq_kill_reg := fun reg => PMap.get reg reg_kills;
+ eq_kill_mem := fun _ => mem_kills;
+ eq_kill_store := fun _ => store_kills;
+ eq_moves := fun reg => PMap.get reg moves
+ |}.
diff --git a/backend/CSE3analysisaux.ml b/backend/CSE3analysisaux.ml
new file mode 100644
index 00000000..efe6b600
--- /dev/null
+++ b/backend/CSE3analysisaux.ml
@@ -0,0 +1,319 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open CSE3analysis
+open Maps
+open HashedSet
+open Camlcoq
+open Coqlib
+
+type flattened_equation_or_condition =
+ | Flat_equ of int * sym_op * int list
+ | Flat_cond of Op.condition * int list;;
+
+let flatten_eq = function
+ | Equ(lhs, sop, args) ->
+ Flat_equ((P.to_int lhs), sop, (List.map P.to_int args))
+ | Cond(cond, args) ->
+ Flat_cond(cond, (List.map P.to_int args));;
+
+let imp_add_i_j s i j =
+ s := PMap.set i (PSet.add j (PMap.get i !s)) !s;;
+
+let string_of_chunk = function
+ | AST.Mint8signed -> "int8signed"
+ | AST.Mint8unsigned -> "int8unsigned"
+ | AST.Mint16signed -> "int16signed"
+ | AST.Mint16unsigned -> "int16unsigned"
+ | AST.Mint32 -> "int32"
+ | AST.Mint64 -> "int64"
+ | AST.Mfloat32 -> "float32"
+ | AST.Mfloat64 -> "float64"
+ | AST.Many32 -> "any32"
+ | AST.Many64 -> "any64";;
+
+let print_reg channel i =
+ Printf.fprintf channel "r%d" i;;
+
+let print_eq channel (lhs, sop, args) =
+ match sop with
+ | SOp op ->
+ Printf.printf "%a = %a" print_reg lhs (PrintOp.print_operation print_reg) (op, args)
+ | SLoad(chunk, addr) ->
+ Printf.printf "%a = %s @ %a" print_reg lhs (string_of_chunk chunk)
+ (PrintOp.print_addressing print_reg) (addr, args);;
+
+let print_cond channel (cond, args) =
+ Printf.printf "cond %a" (PrintOp.print_condition print_reg) (cond, args);;
+
+let pp_intset oc s =
+ Printf.fprintf oc "{ ";
+ List.iter (fun i -> Printf.fprintf oc "%d; " (P.to_int i)) (PSet.elements s);
+ Printf.fprintf oc "}";;
+
+let pp_rhs oc (sop, args) =
+ match sop with
+ | SOp op -> PrintOp.print_operation PrintRTL.reg oc (op, args)
+ | SLoad(chunk, addr) ->
+ Printf.fprintf oc "%s[%a]"
+ (PrintAST.name_of_chunk chunk)
+ (PrintOp.print_addressing PrintRTL.reg) (addr, args);;
+
+let pp_eq oc eq_cond =
+ match eq_cond with
+ | Equ(lhs, sop, args) ->
+ Printf.fprintf oc "x%d = %a" (P.to_int lhs)
+ pp_rhs (sop, args)
+ | Cond(cond, args) ->
+ Printf.fprintf oc "cond %a"
+ (PrintOp.print_condition PrintRTL.reg) (cond, args);;
+
+let pp_P oc x = Printf.fprintf oc "%d" (P.to_int x)
+
+let pp_option pp oc = function
+ | None -> output_string oc "none"
+ | Some x -> pp oc x;;
+
+let is_trivial = function
+ | Equ(lhs, (SOp Op.Omove), [lhs']) -> lhs=lhs'
+ | _ -> false;;
+
+let rec pp_list separator pp_item chan = function
+ | [] -> ()
+ | [h] -> pp_item chan h
+ | h::t ->
+ pp_item chan h;
+ output_string chan separator;
+ pp_list separator pp_item chan t;;
+
+let pp_set separator pp_item chan s =
+ pp_list separator pp_item chan (PSet.elements s);;
+
+let pp_equation hints chan x =
+ match PTree.get x hints.hint_eq_catalog with
+ | None -> output_string chan "???"
+ | Some eq ->
+ match eq with
+ | Equ(lhs, sop, args) ->
+ print_eq chan (P.to_int lhs, sop, List.map P.to_int args)
+ | Cond(cond, args) ->
+ print_cond chan (cond, List.map P.to_int args);;
+
+let pp_relation hints chan rel =
+ pp_set "; " (pp_equation hints) chan rel;;
+
+let pp_relation_b hints chan = function
+ | None -> output_string chan "bot"
+ | Some rel -> pp_relation hints chan rel;;
+
+let pp_results f (invariants : RB.t PMap.t) hints chan =
+ let max_pc = P.to_int (RTL.max_pc_function f) in
+ for pc=max_pc downto 1
+ do
+ Printf.fprintf chan "%d: %a\n\n" pc
+ (pp_relation_b hints) (PMap.get (P.of_int pc) invariants)
+ done
+
+module IntSet=Set.Make(struct type t=int let compare = ( - ) end);;
+
+let rec union_list prev = function
+ | [] -> prev
+ | h::t -> union_list (RB.lub prev h) t;;
+
+let rb_glb (x : RB.t) (y : RB.t) : RB.t =
+ match x, y with
+ | None, _ | _, None -> None
+ | (Some x'), (Some y') -> Some (RELATION.glb x' y');;
+
+let compute_invariants
+ (nodes : RTL.node list)
+ (entrypoint : RTL.node)
+ (tfr : RTL.node -> RB.t -> (RTL.node * RB.t) list) =
+ let todo = ref IntSet.empty
+ and invariants = ref (PMap.set entrypoint (Some RELATION.top) (PMap.init RB.bot)) in
+ let add_todo (pc : RTL.node) =
+ todo := IntSet.add (P.to_int pc) !todo in
+ let update_node (pc : RTL.node) =
+ (if !Clflags.option_debug_compcert > 9
+ then Printf.printf "UP updating node %d\n" (P.to_int pc));
+ let cur = PMap.get pc !invariants in
+ List.iter (fun (next_pc, next_contrib) ->
+ let previous = PMap.get next_pc !invariants in
+ let next = RB.lub previous next_contrib in
+ if not (RB.beq previous next)
+ then (
+ invariants := PMap.set next_pc next !invariants;
+ add_todo next_pc)) (tfr pc cur) in
+ add_todo entrypoint;
+ while not (IntSet.is_empty !todo) do
+ let nxt = IntSet.max_elt !todo in
+ todo := IntSet.remove nxt !todo;
+ update_node (P.of_int nxt)
+ done;
+ !invariants;;
+
+let refine_invariants
+ (nodes : RTL.node list)
+ (entrypoint : RTL.node)
+ (successors : RTL.node -> RTL.node list)
+ (predecessors : RTL.node -> RTL.node list)
+ (tfr : RTL.node -> RB.t -> (RTL.node * RB.t) list)
+ (invariants0 : RB.t PMap.t) =
+ let todo = ref IntSet.empty
+ and invariants = ref invariants0 in
+ let add_todo (pc : RTL.node) =
+ todo := IntSet.add (P.to_int pc) !todo in
+ let update_node (pc : RTL.node) =
+ (if !Clflags.option_debug_compcert > 9
+ then Printf.printf "DOWN updating node %d\n" (P.to_int pc));
+ if not (peq pc entrypoint)
+ then
+ let cur = PMap.get pc !invariants in
+ let nxt = union_list RB.bot
+ (List.map
+ (fun pred_pc->
+ rb_glb cur
+ (List.assoc pc (tfr pred_pc (PMap.get pred_pc !invariants))))
+ (predecessors pc)) in
+ if not (RB.beq cur nxt)
+ then
+ begin
+ (if !Clflags.option_debug_compcert > 4
+ then Printf.printf "refining CSE3 node %d\n" (P.to_int pc));
+ List.iter add_todo (successors pc)
+ end in
+ (List.iter add_todo nodes);
+ while not (IntSet.is_empty !todo) do
+ let nxt = IntSet.max_elt !todo in
+ todo := IntSet.remove nxt !todo;
+ update_node (P.of_int nxt)
+ done;
+ !invariants;;
+
+let get_default default x ptree =
+ match PTree.get x ptree with
+ | None -> default
+ | Some y -> y;;
+
+let initial_analysis ctx tenv (f : RTL.coq_function) =
+ let tfr = apply_instr' ctx tenv f.RTL.fn_code in
+ compute_invariants
+ (List.map fst (PTree.elements f.RTL.fn_code))
+ f.RTL.fn_entrypoint tfr;;
+
+let refine_analysis ctx tenv
+ (f : RTL.coq_function) (invariants0 : RB.t PMap.t) =
+ let succ_map = RTL.successors_map f in
+ let succ_f x = get_default [] x succ_map in
+ let pred_map = Kildall.make_predecessors f.RTL.fn_code RTL.successors_instr in
+ let pred_f x = get_default [] x pred_map in
+ let tfr = apply_instr' ctx tenv f.RTL.fn_code in
+ refine_invariants
+ (List.map fst (PTree.elements f.RTL.fn_code))
+ f.RTL.fn_entrypoint succ_f pred_f tfr invariants0;;
+
+let add_to_set_in_table table key item =
+ Hashtbl.add table key
+ (PSet.add item
+ (match Hashtbl.find_opt table key with
+ | None -> PSet.empty
+ | Some s -> s));;
+
+let preanalysis (tenv : typing_env) (f : RTL.coq_function) =
+ let cur_eq_id = ref 0
+ and cur_catalog = ref PTree.empty
+ and eq_table = Hashtbl.create 100
+ and rhs_table = Hashtbl.create 100
+ and cur_kill_reg = ref (PMap.init PSet.empty)
+ and cur_kill_mem = ref PSet.empty
+ and cur_kill_store = ref PSet.empty
+ and cur_moves = ref (PMap.init PSet.empty) in
+ let eq_find_oracle node eq =
+ assert (not (is_trivial eq));
+ let o = Hashtbl.find_opt eq_table (flatten_eq eq) in
+ (* FIXME (if o = None then failwith "eq_find_oracle"); *)
+ (if !Clflags.option_debug_compcert > 5
+ then Printf.printf "@%d: eq_find %a -> %a\n" (P.to_int node)
+ pp_eq eq (pp_option pp_P) o);
+ o
+ and rhs_find_oracle node sop args =
+ let o =
+ match Hashtbl.find_opt rhs_table (sop, List.map P.to_int args) with
+ | None -> PSet.empty
+ | Some s -> s in
+ (if !Clflags.option_debug_compcert > 5
+ then Printf.printf "@%d: rhs_find %a = %a\n"
+ (P.to_int node) pp_rhs (sop, args) pp_intset o);
+ o in
+ let mutating_eq_find_oracle node eq : P.t option =
+ let flat_eq = flatten_eq eq in
+ let o =
+ match Hashtbl.find_opt eq_table flat_eq with
+ | Some x ->
+ Some x
+ | None ->
+ (* TODO print_eq stderr flat_eq; *)
+ incr cur_eq_id;
+ let id = !cur_eq_id in
+ let coq_id = P.of_int id in
+ begin
+ Hashtbl.add eq_table flat_eq coq_id;
+ (cur_catalog := PTree.set coq_id eq !cur_catalog);
+ (match flat_eq with
+ | Flat_equ(flat_eq_lhs, flat_eq_op, flat_eq_args) ->
+ add_to_set_in_table rhs_table
+ (flat_eq_op, flat_eq_args) coq_id
+ | Flat_cond(flat_eq_cond, flat_eq_args) -> ());
+ (match eq with
+ | Equ(lhs, sop, args) ->
+ List.iter
+ (fun reg -> imp_add_i_j cur_kill_reg reg coq_id)
+ (lhs :: args);
+ (match sop, args with
+ | (SOp Op.Omove), [rhs] -> imp_add_i_j cur_moves lhs coq_id
+ | _, _ -> ())
+ | Cond(cond, args) ->
+ List.iter
+ (fun reg -> imp_add_i_j cur_kill_reg reg coq_id) args
+ );
+ (if eq_cond_depends_on_mem eq
+ then cur_kill_mem := PSet.add coq_id !cur_kill_mem);
+ (if eq_cond_depends_on_store eq
+ then cur_kill_store := PSet.add coq_id !cur_kill_store);
+ Some coq_id
+ end
+ in
+ (if !Clflags.option_debug_compcert > 5
+ then Printf.printf "@%d: mutating_eq_find %a -> %a\n" (P.to_int node)
+ pp_eq eq (pp_option pp_P) o);
+ o
+ in
+ let ctx = { eq_catalog = (fun eq_id -> PTree.get eq_id !cur_catalog);
+ eq_find_oracle = mutating_eq_find_oracle;
+ eq_rhs_oracle = rhs_find_oracle ;
+ eq_kill_reg = (fun reg -> PMap.get reg !cur_kill_reg);
+ eq_kill_mem = (fun () -> !cur_kill_mem);
+ eq_kill_store = (fun () -> !cur_kill_store);
+ eq_moves = (fun reg -> PMap.get reg !cur_moves)
+ } in
+ let invariants = initial_analysis ctx tenv f in
+ let invariants' =
+ if ! Clflags.option_fcse3_refine
+ then refine_analysis ctx tenv f invariants
+ else invariants
+ and hints = { hint_eq_catalog = !cur_catalog;
+ hint_eq_find_oracle= eq_find_oracle;
+ hint_eq_rhs_oracle = rhs_find_oracle } in
+ (if !Clflags.option_debug_compcert > 1
+ then pp_results f invariants' hints stdout);
+ invariants', hints
+;;
diff --git a/backend/CSE3analysisproof.v b/backend/CSE3analysisproof.v
new file mode 100644
index 00000000..523b52df
--- /dev/null
+++ b/backend/CSE3analysisproof.v
@@ -0,0 +1,1421 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE3analysis CSE2deps CSE2depsproof HashedSet.
+Require Import RTLtyping.
+Require Import Lia.
+
+Lemma rel_leb_correct:
+ forall x x',
+ rel_leb x x' = true <-> RELATION.ge x' x.
+Proof.
+ unfold rel_leb, RELATION.ge.
+ split; auto.
+Qed.
+
+Hint Resolve rel_leb_correct : cse3.
+
+Lemma relb_leb_correct:
+ forall x x',
+ relb_leb x x' = true <-> RB.ge x' x.
+Proof.
+ unfold relb_leb, RB.ge.
+ destruct x; destruct x'; split; trivial; try contradiction; discriminate.
+Qed.
+
+Hint Resolve relb_leb_correct : cse3.
+
+Theorem loadv_storev_really_same:
+ forall chunk: memory_chunk,
+ forall m1: mem,
+ forall addr v: val,
+ forall m2: mem,
+ forall ty : typ,
+ forall TYPE: Val.has_type v ty,
+ forall STORE: Mem.storev chunk m1 addr v = Some m2,
+ forall COMPATIBLE: loadv_storev_compatible_type chunk ty = true,
+ Mem.loadv chunk m2 addr = Some v.
+Proof.
+ intros.
+ rewrite Mem.loadv_storev_same with (m1:=m1) (v:=v) by assumption.
+ f_equal.
+ destruct chunk; destruct ty; try discriminate.
+ all: destruct v; trivial; try contradiction.
+ all: unfold Val.load_result, Val.has_type in *.
+ all: destruct Archi.ptr64; trivial; discriminate.
+Qed.
+
+Lemma subst_args_notin :
+ forall (rs : regset) dst v args,
+ ~ In dst args ->
+ (rs # dst <- v) ## args = rs ## args.
+Proof.
+ induction args; simpl; trivial.
+ intro NOTIN.
+ destruct (peq a dst).
+ {
+ subst a.
+ intuition congruence.
+ }
+ rewrite Regmap.gso by congruence.
+ f_equal.
+ apply IHargs.
+ intuition congruence.
+Qed.
+
+Lemma add_i_j_adds : forall i j m,
+ PSet.contains (Regmap.get i (add_i_j i j m)) j = true.
+Proof.
+ intros.
+ unfold add_i_j.
+ rewrite Regmap.gss.
+ auto with pset.
+Qed.
+Hint Resolve add_i_j_adds: cse3.
+
+Lemma add_i_j_monotone : forall i j i' j' m,
+ PSet.contains (Regmap.get i' m) j' = true ->
+ PSet.contains (Regmap.get i' (add_i_j i j m)) j' = true.
+Proof.
+ intros.
+ unfold add_i_j.
+ destruct (peq i i').
+ - subst i'.
+ rewrite Regmap.gss.
+ destruct (peq j j').
+ + subst j'.
+ apply PSet.gadds.
+ + eauto with pset.
+ - rewrite Regmap.gso.
+ assumption.
+ congruence.
+Qed.
+
+Hint Resolve add_i_j_monotone: cse3.
+
+Lemma add_ilist_j_monotone : forall ilist j i' j' m,
+ PSet.contains (Regmap.get i' m) j' = true ->
+ PSet.contains (Regmap.get i' (add_ilist_j ilist j m)) j' = true.
+Proof.
+ induction ilist; simpl; intros until m; intro CONTAINS; auto with cse3.
+Qed.
+Hint Resolve add_ilist_j_monotone: cse3.
+
+Lemma add_ilist_j_adds : forall ilist j m,
+ forall i, In i ilist ->
+ PSet.contains (Regmap.get i (add_ilist_j ilist j m)) j = true.
+Proof.
+ induction ilist; simpl; intros until i; intro IN.
+ contradiction.
+ destruct IN as [HEAD | TAIL]; subst; auto with cse3.
+Qed.
+Hint Resolve add_ilist_j_adds: cse3.
+
+Definition xlget_kills (eqs : list (eq_id * equation_or_condition))
+ (m : Regmap.t PSet.t) :
+ Regmap.t PSet.t :=
+ List.fold_left (fun already (item : eq_id * equation_or_condition) =>
+ match snd item with
+ | Equ lhs sop args =>
+ add_i_j lhs (fst item)
+ (add_ilist_j args (fst item) already)
+ | Cond cond args => add_ilist_j args (fst item) already
+ end) eqs m.
+
+Definition xlget_mem_kills (eqs : list (positive * equation_or_condition))
+ (m : PSet.t) : PSet.t :=
+(fold_left
+ (fun (a : PSet.t) (item : positive * equation_or_condition) =>
+ if eq_cond_depends_on_mem (snd item)
+ then PSet.add (fst item) a
+ else a
+ )
+ eqs m).
+
+Definition xlget_store_kills (eqs : list (positive * equation_or_condition))
+ (m : PSet.t) : PSet.t :=
+(fold_left
+ (fun (a : PSet.t) (item : positive * equation_or_condition) =>
+ if eq_cond_depends_on_store (snd item)
+ then PSet.add (fst item) a
+ else a
+ )
+ eqs m).
+
+Lemma xlget_kills_monotone :
+ forall eqs m i j,
+ PSet.contains (Regmap.get i m) j = true ->
+ PSet.contains (Regmap.get i (xlget_kills eqs m)) j = true.
+Proof.
+ induction eqs; simpl; trivial.
+ intros.
+ destruct a as [id eq_cond]; cbn.
+ destruct eq_cond as [eq_lhs eq_sop eq_args | eq_cond eq_args]; auto with cse3.
+Qed.
+
+Hint Resolve xlget_kills_monotone : cse3.
+
+Lemma xlget_mem_kills_monotone :
+ forall eqs m j,
+ PSet.contains m j = true ->
+ PSet.contains (xlget_mem_kills eqs m) j = true.
+Proof.
+ induction eqs; simpl; trivial.
+ intros.
+ destruct a as [id eq_cond]; cbn.
+ destruct eq_cond_depends_on_mem.
+ - apply IHeqs.
+ destruct (peq id j).
+ + subst j. apply PSet.gadds.
+ + rewrite PSet.gaddo by congruence.
+ trivial.
+ - auto.
+Qed.
+
+Hint Resolve xlget_mem_kills_monotone : cse3.
+
+Lemma xlget_store_kills_monotone :
+ forall eqs m j,
+ PSet.contains m j = true ->
+ PSet.contains (xlget_store_kills eqs m) j = true.
+Proof.
+ induction eqs; simpl; trivial.
+ intros.
+ destruct a as [id eq_cond]; cbn.
+ destruct eq_cond_depends_on_store.
+ - apply IHeqs.
+ destruct (peq id j).
+ + subst j. apply PSet.gadds.
+ + rewrite PSet.gaddo by congruence.
+ trivial.
+ - auto.
+Qed.
+
+Hint Resolve xlget_store_kills_monotone : cse3.
+
+Lemma xlget_kills_has_lhs :
+ forall eqs m lhs sop args j,
+ In (j, (Equ lhs sop args)) eqs ->
+ PSet.contains (Regmap.get lhs (xlget_kills eqs m)) j = true.
+Proof.
+ induction eqs; simpl.
+ contradiction.
+ intros until j.
+ intro HEAD_TAIL.
+ destruct HEAD_TAIL as [HEAD | TAIL]; subst; simpl.
+ - auto with cse3.
+ - eapply IHeqs. eassumption.
+Qed.
+Hint Resolve xlget_kills_has_lhs : cse3.
+
+Lemma xlget_kills_has_arg :
+ forall eqs m lhs sop arg args j,
+ In (j, (Equ lhs sop args)) eqs ->
+ In arg args ->
+ PSet.contains (Regmap.get arg (xlget_kills eqs m)) j = true.
+Proof.
+ induction eqs; simpl.
+ contradiction.
+ intros until j.
+ intros HEAD_TAIL ARG.
+ destruct HEAD_TAIL as [HEAD | TAIL]; subst; simpl.
+ - auto with cse3.
+ - eapply IHeqs; eassumption.
+Qed.
+
+Hint Resolve xlget_kills_has_arg : cse3.
+
+Lemma xlget_cond_kills_has_arg :
+ forall eqs m cond arg args j,
+ In (j, (Cond cond args)) eqs ->
+ In arg args ->
+ PSet.contains (Regmap.get arg (xlget_kills eqs m)) j = true.
+Proof.
+ induction eqs; simpl.
+ contradiction.
+ intros until j.
+ intros HEAD_TAIL ARG.
+ destruct HEAD_TAIL as [HEAD | TAIL]; subst; simpl.
+ - auto with cse3.
+ - eapply IHeqs; eassumption.
+Qed.
+
+Hint Resolve xlget_cond_kills_has_arg : cse3.
+
+Lemma get_kills_has_lhs :
+ forall eqs lhs sop args j,
+ PTree.get j eqs = Some (Equ lhs sop args) ->
+ PSet.contains (Regmap.get lhs (get_reg_kills eqs)) j = true.
+Proof.
+ unfold get_reg_kills.
+ intros.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : Regmap.t PSet.t) (p : positive * equation_or_condition) =>
+ match snd p with
+ | Equ lhs0 _ args0 =>
+ add_i_j lhs0 (fst p) (add_ilist_j args0 (fst p) a)
+ | Cond _ args0 => add_ilist_j args0 (fst p) a
+ end)) with xlget_kills.
+ eapply xlget_kills_has_lhs.
+ apply PTree.elements_correct.
+ eassumption.
+Qed.
+
+Hint Resolve get_kills_has_lhs : cse3.
+
+Lemma context_from_hints_get_kills_has_lhs :
+ forall hints lhs sop args j,
+ PTree.get j (hint_eq_catalog hints) = Some (Equ lhs sop args) ->
+ PSet.contains (eq_kill_reg (context_from_hints hints) lhs) j = true.
+Proof.
+ intros; simpl.
+ eapply get_kills_has_lhs.
+ eassumption.
+Qed.
+
+Hint Resolve context_from_hints_get_kills_has_lhs : cse3.
+
+Lemma get_kills_has_arg :
+ forall eqs lhs sop arg args j,
+ PTree.get j eqs = Some (Equ lhs sop args) ->
+ In arg args ->
+ PSet.contains (Regmap.get arg (get_reg_kills eqs)) j = true.
+Proof.
+ unfold get_reg_kills.
+ intros.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : Regmap.t PSet.t) (p : positive * equation_or_condition) =>
+ match snd p with
+ | Equ lhs0 _ args0 =>
+ add_i_j lhs0 (fst p) (add_ilist_j args0 (fst p) a)
+ | Cond _ args0 => add_ilist_j args0 (fst p) a
+ end)) with xlget_kills.
+ eapply xlget_kills_has_arg.
+ - apply PTree.elements_correct.
+ eassumption.
+ - assumption.
+Qed.
+
+Hint Resolve get_kills_has_arg : cse3.
+
+Lemma context_from_hints_get_kills_has_arg :
+ forall hints lhs sop arg args j,
+ PTree.get j (hint_eq_catalog hints) = Some (Equ lhs sop args) ->
+ In arg args ->
+ PSet.contains (eq_kill_reg (context_from_hints hints) arg) j = true.
+Proof.
+ intros.
+ simpl.
+ eapply get_kills_has_arg; eassumption.
+Qed.
+
+Hint Resolve context_from_hints_get_kills_has_arg : cse3.
+
+Lemma get_cond_kills_has_arg :
+ forall eqs cond arg args j,
+ PTree.get j eqs = Some (Cond cond args) ->
+ In arg args ->
+ PSet.contains (Regmap.get arg (get_reg_kills eqs)) j = true.
+Proof.
+ unfold get_reg_kills.
+ intros.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : Regmap.t PSet.t) (p : positive * equation_or_condition) =>
+ match snd p with
+ | Equ lhs0 _ args0 =>
+ add_i_j lhs0 (fst p) (add_ilist_j args0 (fst p) a)
+ | Cond _ args0 => add_ilist_j args0 (fst p) a
+ end)) with xlget_kills.
+ eapply xlget_cond_kills_has_arg.
+ - apply PTree.elements_correct.
+ eassumption.
+ - assumption.
+Qed.
+
+Hint Resolve get_cond_kills_has_arg : cse3.
+
+Lemma context_from_hints_get_cond_kills_has_arg :
+ forall hints cond arg args j,
+ PTree.get j (hint_eq_catalog hints) = Some (Cond cond args) ->
+ In arg args ->
+ PSet.contains (eq_kill_reg (context_from_hints hints) arg) j = true.
+Proof.
+ intros.
+ simpl.
+ eapply get_cond_kills_has_arg; eassumption.
+Qed.
+
+Hint Resolve context_from_hints_get_cond_kills_has_arg : cse3.
+
+Lemma xlget_kills_has_eq_depends_on_mem :
+ forall eqs eq j m,
+ In (j, eq) eqs ->
+ eq_cond_depends_on_mem eq = true ->
+ PSet.contains (xlget_mem_kills eqs m) j = true.
+Proof.
+ induction eqs; simpl.
+ contradiction.
+ intros.
+ destruct H.
+ { subst a.
+ simpl.
+ rewrite H0.
+ apply xlget_mem_kills_monotone.
+ apply PSet.gadds.
+ }
+ eauto.
+Qed.
+
+Hint Resolve xlget_kills_has_eq_depends_on_mem : cse3.
+
+Lemma get_kills_has_eq_depends_on_mem :
+ forall eqs eq j,
+ PTree.get j eqs = Some eq ->
+ eq_cond_depends_on_mem eq = true ->
+ PSet.contains (get_mem_kills eqs) j = true.
+Proof.
+ intros.
+ unfold get_mem_kills.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : PSet.t) (p : positive * equation_or_condition) =>
+ if eq_cond_depends_on_mem (snd p) then PSet.add (fst p) a else a))
+ with xlget_mem_kills.
+ eapply xlget_kills_has_eq_depends_on_mem.
+ apply PTree.elements_correct.
+ eassumption.
+ trivial.
+Qed.
+
+Lemma context_from_hints_get_kills_has_eq_depends_on_mem :
+ forall hints eq j,
+ PTree.get j (hint_eq_catalog hints) = Some eq ->
+ eq_cond_depends_on_mem eq = true ->
+ PSet.contains (eq_kill_mem (context_from_hints hints) tt) j = true.
+Proof.
+ intros.
+ simpl.
+ eapply get_kills_has_eq_depends_on_mem; eassumption.
+Qed.
+
+Hint Resolve context_from_hints_get_kills_has_eq_depends_on_mem : cse3.
+
+Lemma xlget_kills_has_eq_depends_on_store :
+ forall eqs eq j m,
+ In (j, eq) eqs ->
+ eq_cond_depends_on_store eq = true ->
+ PSet.contains (xlget_store_kills eqs m) j = true.
+Proof.
+ induction eqs; simpl.
+ contradiction.
+ intros.
+ destruct H.
+ { subst a.
+ simpl.
+ rewrite H0.
+ apply xlget_store_kills_monotone.
+ apply PSet.gadds.
+ }
+ eauto.
+Qed.
+
+Hint Resolve xlget_kills_has_eq_depends_on_store : cse3.
+
+Lemma get_kills_has_eq_depends_on_store :
+ forall eqs eq j,
+ PTree.get j eqs = Some eq ->
+ eq_cond_depends_on_store eq = true ->
+ PSet.contains (get_store_kills eqs) j = true.
+Proof.
+ intros.
+ unfold get_store_kills.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : PSet.t) (p : positive * equation_or_condition) =>
+ if eq_cond_depends_on_store (snd p) then PSet.add (fst p) a else a))
+ with xlget_store_kills.
+ eapply xlget_kills_has_eq_depends_on_store.
+ apply PTree.elements_correct.
+ eassumption.
+ trivial.
+Qed.
+
+Lemma context_from_hints_get_kills_has_eq_depends_on_store :
+ forall hints eq j,
+ PTree.get j (hint_eq_catalog hints) = Some eq ->
+ eq_cond_depends_on_store eq = true ->
+ PSet.contains (eq_kill_store (context_from_hints hints) tt) j = true.
+Proof.
+ intros.
+ simpl.
+ eapply get_kills_has_eq_depends_on_store; eassumption.
+Qed.
+
+Hint Resolve context_from_hints_get_kills_has_eq_depends_on_store : cse3.
+
+Definition eq_involves (eq : equation_or_condition) (i : reg) :=
+ match eq with
+ | Equ lhs sop args =>
+ i = lhs \/ In i args
+ | Cond cond args => In i args
+ end.
+
+Section SOUNDNESS.
+ Context {F V : Type}.
+ Context {genv: Genv.t F V}.
+ Context {sp : val}.
+
+ Context {ctx : eq_context}.
+
+ Definition sem_rhs (sop : sym_op) (args : list reg)
+ (rs : regset) (m : mem) (v' : val) :=
+ match sop with
+ | SOp op =>
+ match eval_operation genv sp op (rs ## args) m with
+ | Some v => v' = v
+ | None => False
+ end
+ | SLoad chunk addr =>
+ match
+ match eval_addressing genv sp addr (rs ## args) with
+ | Some a => Mem.loadv chunk m a
+ | None => None
+ end
+ with
+ | Some dat => v' = dat
+ | None => v' = Vundef
+ end
+ end.
+
+ Definition sem_eq (eq : equation_or_condition) (rs : regset) (m : mem) :=
+ match eq with
+ | Equ lhs sop args => sem_rhs sop args rs m (rs # lhs)
+ | Cond cond args => eval_condition cond (rs ## args) m = Some true
+ end.
+
+ Definition sem_rel (rel : RELATION.t) (rs : regset) (m : mem) :=
+ forall i eq,
+ PSet.contains rel i = true ->
+ eq_catalog ctx i = Some eq ->
+ sem_eq eq rs m.
+
+ Lemma sem_rel_glb:
+ forall rel1 rel2 rs m,
+ (sem_rel (RELATION.glb rel1 rel2) rs m) <->
+ ((sem_rel rel1 rs m) /\
+ (sem_rel rel2 rs m)).
+ Proof.
+ intros.
+ unfold sem_rel, RELATION.glb.
+ split.
+ - intro IMPLIES.
+ split;
+ intros i eq CONTAINS;
+ specialize IMPLIES with (i:=i) (eq0:=eq);
+ rewrite PSet.gunion in IMPLIES;
+ rewrite orb_true_iff in IMPLIES;
+ intuition.
+ - intros (IMPLIES1 & IMPLIES2) i eq.
+ rewrite PSet.gunion.
+ rewrite orb_true_iff.
+ specialize IMPLIES1 with (i:=i) (eq0:=eq).
+ specialize IMPLIES2 with (i:=i) (eq0:=eq).
+ intuition.
+ Qed.
+
+ Hypothesis ctx_kill_reg_has_lhs :
+ forall lhs sop args j,
+ eq_catalog ctx j = Some (Equ lhs sop args) ->
+ PSet.contains (eq_kill_reg ctx lhs) j = true.
+
+ Hypothesis ctx_kill_reg_has_arg :
+ forall lhs sop args j,
+ eq_catalog ctx j = Some (Equ lhs sop args) ->
+ forall arg,
+ In arg args ->
+ PSet.contains (eq_kill_reg ctx arg) j = true.
+
+ Hypothesis ctx_cond_kill_reg_has_arg :
+ forall cond args j,
+ eq_catalog ctx j = Some (Cond cond args) ->
+ forall arg,
+ In arg args ->
+ PSet.contains (eq_kill_reg ctx arg) j = true.
+
+ Hypothesis ctx_kill_mem_has_depends_on_mem :
+ forall eq j,
+ eq_catalog ctx j = Some eq ->
+ eq_cond_depends_on_mem eq = true ->
+ PSet.contains (eq_kill_mem ctx tt) j = true.
+
+ Hypothesis ctx_kill_store_has_depends_on_store :
+ forall eq j,
+ eq_catalog ctx j = Some eq ->
+ eq_cond_depends_on_store eq = true ->
+ PSet.contains (eq_kill_store ctx tt) j = true.
+
+ Theorem kill_reg_sound :
+ forall rel rs m dst v,
+ (sem_rel rel rs m) ->
+ (sem_rel (kill_reg (ctx:=ctx) dst rel) (rs#dst <- v) m).
+ Proof.
+ unfold sem_rel, sem_eq, sem_rhs, kill_reg.
+ intros until v.
+ intros REL i eq.
+ specialize REL with (i := i) (eq0 := eq).
+ destruct eq as [lhs sop args | cond args]; simpl.
+ * specialize ctx_kill_reg_has_lhs with (lhs := lhs) (sop := sop) (args := args) (j := i).
+ specialize ctx_kill_reg_has_arg with (lhs := lhs) (sop := sop) (args := args) (j := i) (arg := dst).
+ intuition.
+ rewrite PSet.gsubtract in H.
+ rewrite andb_true_iff in H.
+ rewrite negb_true_iff in H.
+ intuition.
+ simpl in *.
+ assert ({In dst args} + {~In dst args}) as IN_ARGS.
+ {
+ apply List.in_dec.
+ apply peq.
+ }
+ destruct IN_ARGS as [IN_ARGS | NOTIN_ARGS].
+ { intuition.
+ congruence.
+ }
+ destruct (peq dst lhs).
+ {
+ congruence.
+ }
+ rewrite subst_args_notin by assumption.
+ destruct sop.
+ - destruct (eval_operation genv sp o rs ## args m) as [ev | ]; trivial.
+ rewrite Regmap.gso by congruence.
+ assumption.
+ - rewrite Regmap.gso by congruence.
+ assumption.
+ * specialize ctx_cond_kill_reg_has_arg with (cond := cond) (args := args) (j := i) (arg := dst).
+ intuition.
+ rewrite PSet.gsubtract in H.
+ rewrite andb_true_iff in H.
+ rewrite negb_true_iff in H.
+ intuition.
+ simpl in *.
+ assert ({In dst args} + {~In dst args}) as IN_ARGS.
+ {
+ apply List.in_dec.
+ apply peq.
+ }
+ destruct IN_ARGS as [IN_ARGS | NOTIN_ARGS].
+ { intuition.
+ congruence.
+ }
+ rewrite subst_args_notin by assumption.
+ assumption.
+ Qed.
+
+ Hint Resolve kill_reg_sound : cse3.
+
+ Theorem kill_reg_sound2 :
+ forall rel rs m dst,
+ (sem_rel rel rs m) ->
+ (sem_rel (kill_reg (ctx:=ctx) dst rel) rs m).
+ Proof.
+ unfold sem_rel, sem_eq, sem_rhs, kill_reg.
+ intros until dst.
+ intros REL i eq.
+ specialize REL with (i := i) (eq0 := eq).
+ destruct eq as [lhs sop args | cond args]; simpl.
+ * specialize ctx_kill_reg_has_lhs with (lhs := lhs) (sop := sop) (args := args) (j := i).
+ specialize ctx_kill_reg_has_arg with (lhs := lhs) (sop := sop) (args := args) (j := i) (arg := dst).
+ intuition.
+ rewrite PSet.gsubtract in H.
+ rewrite andb_true_iff in H.
+ rewrite negb_true_iff in H.
+ intuition.
+ * specialize ctx_cond_kill_reg_has_arg with (cond := cond) (args := args) (j := i) (arg := dst).
+ intuition.
+ rewrite PSet.gsubtract in H.
+ rewrite andb_true_iff in H.
+ rewrite negb_true_iff in H.
+ intuition.
+ Qed.
+
+ Lemma pick_source_sound :
+ forall (l : list reg),
+ match pick_source l with
+ | Some x => In x l
+ | None => True
+ end.
+ Proof.
+ unfold pick_source.
+ destruct l; simpl; trivial.
+ left; trivial.
+ Qed.
+
+ Hint Resolve pick_source_sound : cse3.
+
+ Theorem forward_move_sound :
+ forall rel rs m x,
+ (sem_rel rel rs m) ->
+ rs # (forward_move (ctx := ctx) rel x) = rs # x.
+ Proof.
+ unfold sem_rel, forward_move.
+ intros until x.
+ intro REL.
+ pose proof (pick_source_sound (PSet.elements (PSet.inter rel (eq_moves ctx x)))) as ELEMENT.
+ destruct (pick_source (PSet.elements (PSet.inter rel (eq_moves ctx x)))).
+ 2: reflexivity.
+ destruct (eq_catalog ctx r) as [eq | ] eqn:CATALOG.
+ 2: reflexivity.
+ specialize REL with (i := r) (eq0 := eq).
+ destruct eq as [lhs sop args | cond args]; cbn in *; trivial.
+ destruct (is_smove sop) as [MOVE | ].
+ 2: reflexivity.
+ rewrite MOVE in *; cbn in *.
+ destruct (peq x lhs).
+ 2: reflexivity.
+ simpl.
+ subst x.
+ rewrite PSet.elements_spec in ELEMENT.
+ rewrite PSet.ginter in ELEMENT.
+ rewrite andb_true_iff in ELEMENT.
+ unfold sem_eq in REL.
+ simpl in REL.
+ destruct args as [ | h t].
+ reflexivity.
+ destruct t.
+ 2: reflexivity.
+ simpl in REL.
+ intuition congruence.
+ Qed.
+
+ Hint Resolve forward_move_sound : cse3.
+
+ Theorem forward_move_l_sound :
+ forall rel rs m l,
+ (sem_rel rel rs m) ->
+ rs ## (forward_move_l (ctx := ctx) rel l) = rs ## l.
+ Proof.
+ induction l; simpl; intros; trivial.
+ erewrite forward_move_sound by eassumption.
+ intuition congruence.
+ Qed.
+
+ Hint Resolve forward_move_l_sound : cse3.
+
+ Theorem kill_mem_sound :
+ forall rel rs m m',
+ (sem_rel rel rs m) ->
+ (sem_rel (kill_mem (ctx:=ctx) rel) rs m').
+ Proof.
+ unfold sem_rel, sem_eq, sem_rhs, kill_mem.
+ intros until m'.
+ intros REL i eq.
+ specialize REL with (i := i) (eq0 := eq).
+ intros SUBTRACT CATALOG.
+ rewrite PSet.gsubtract in SUBTRACT.
+ rewrite andb_true_iff in SUBTRACT.
+ intuition.
+ destruct eq as [lhs sop args | cond args] eqn:EQ.
+ * destruct sop as [op | chunk addr] eqn:OP.
+ - specialize ctx_kill_mem_has_depends_on_mem with (eq0 := eq) (j := i).
+ rewrite EQ in ctx_kill_mem_has_depends_on_mem.
+ unfold eq_cond_depends_on_mem in ctx_kill_mem_has_depends_on_mem.
+ rewrite (op_depends_on_memory_correct genv sp op) with (m2 := m).
+ assumption.
+ destruct (op_depends_on_memory op) in *; trivial.
+ rewrite ctx_kill_mem_has_depends_on_mem in H0; trivial.
+ discriminate H0.
+ - specialize ctx_kill_mem_has_depends_on_mem with (eq0 := eq) (j := i).
+ rewrite EQ in ctx_kill_mem_has_depends_on_mem.
+ rewrite negb_true_iff in H0.
+ intuition.
+ congruence.
+ * specialize ctx_kill_mem_has_depends_on_mem with (eq0 := eq) (j := i).
+ rewrite EQ in ctx_kill_mem_has_depends_on_mem.
+ unfold eq_cond_depends_on_mem in ctx_kill_mem_has_depends_on_mem.
+ rewrite (cond_depends_on_memory_correct cond) with (m2 := m).
+ assumption.
+ destruct (cond_depends_on_memory cond) in *; trivial.
+ rewrite negb_true_iff in H0.
+ intuition.
+ congruence.
+ Qed.
+
+ Hint Resolve kill_mem_sound : cse3.
+
+ (* TODO: shouldn't this already be proved somewhere else? *)
+ Lemma store_preserves_validity:
+ forall m m' wchunk a v
+ (STORE : Mem.storev wchunk m a v = Some m')
+ (b : block) (z : Z),
+ Mem.valid_pointer m' b z = Mem.valid_pointer m b z.
+ Proof.
+ unfold Mem.storev.
+ intros.
+ destruct a; try discriminate.
+ Local Transparent Mem.store.
+ unfold Mem.store in STORE.
+ destruct Mem.valid_access_dec in STORE.
+ 2: discriminate.
+ inv STORE.
+ reflexivity.
+ Qed.
+
+ Hint Resolve store_preserves_validity : cse3.
+
+ Theorem kill_store_sound :
+ forall rel rs m m' wchunk a v,
+ (sem_rel rel rs m) ->
+ (Mem.storev wchunk m a v = Some m') ->
+ (sem_rel (kill_store (ctx:=ctx) rel) rs m').
+ Proof.
+ unfold sem_rel, sem_eq, sem_rhs, kill_store.
+ intros until v.
+ intros REL STORE i eq.
+ specialize REL with (i := i) (eq0 := eq).
+ intros SUBTRACT CATALOG.
+ rewrite PSet.gsubtract in SUBTRACT.
+ rewrite andb_true_iff in SUBTRACT.
+ intuition.
+ destruct eq as [lhs sop args | cond args] eqn:EQ.
+ * destruct sop as [op | chunk addr] eqn:OP.
+ - rewrite op_valid_pointer_eq with (m2 := m).
+ assumption.
+ eapply store_preserves_validity; eauto.
+ - specialize ctx_kill_store_has_depends_on_store with (eq0 := eq) (j := i).
+ rewrite EQ in ctx_kill_store_has_depends_on_store.
+ rewrite negb_true_iff in H0.
+ intuition.
+ congruence.
+ * rewrite cond_valid_pointer_eq with (m2 := m).
+ assumption.
+ eapply store_preserves_validity; eauto.
+ Qed.
+
+ Hint Resolve kill_store_sound : cse3.
+
+ Theorem eq_find_sound:
+ forall no eq id,
+ eq_find (ctx := ctx) no eq = Some id ->
+ eq_catalog ctx id = Some eq.
+ Proof.
+ unfold eq_find.
+ intros.
+ destruct (eq_find_oracle ctx no eq) as [ id' | ].
+ 2: discriminate.
+ destruct (eq_catalog ctx id') as [eq' |] eqn:CATALOG.
+ 2: discriminate.
+ destruct (eq_dec_equation eq eq').
+ 2: discriminate.
+ congruence.
+ Qed.
+
+ Hint Resolve eq_find_sound : cse3.
+
+ Theorem is_condition_present_sound :
+ forall node rel cond args rs m
+ (REL : sem_rel rel rs m)
+ (COND : (is_condition_present (ctx := ctx) node rel cond args) = true),
+ (eval_condition cond (rs ## args) m) = Some true.
+ Proof.
+ unfold sem_rel, is_condition_present.
+ intros.
+ destruct eq_find as [i |] eqn:FIND.
+ 2: discriminate.
+ pose proof (eq_find_sound node (Cond cond args) i FIND) as CATALOG.
+ exact (REL i (Cond cond args) COND CATALOG).
+ Qed.
+
+ Hint Resolve is_condition_present_sound : cse3.
+
+ Theorem rhs_find_sound:
+ forall no sop args rel src rs m,
+ sem_rel rel rs m ->
+ rhs_find (ctx := ctx) no sop args rel = Some src ->
+ sem_rhs sop args rs m (rs # src).
+ Proof.
+ unfold rhs_find, sem_rel, sem_eq.
+ intros until m.
+ intros REL FIND.
+ pose proof (pick_source_sound (PSet.elements (PSet.inter (eq_rhs_oracle ctx no sop args) rel))) as SOURCE.
+ destruct (pick_source (PSet.elements (PSet.inter (eq_rhs_oracle ctx no sop args) rel))) as [ src' | ].
+ 2: discriminate.
+ rewrite PSet.elements_spec in SOURCE.
+ rewrite PSet.ginter in SOURCE.
+ rewrite andb_true_iff in SOURCE.
+ destruct (eq_catalog ctx src') as [eq | ] eqn:CATALOG.
+ 2: discriminate.
+ specialize REL with (i := src') (eq0 := eq).
+ destruct eq as [eq_lhs eq_sop eq_args | eq_cond eq_args] eqn:EQ.
+ 2: discriminate.
+ destruct (eq_dec_sym_op sop eq_sop).
+ 2: discriminate.
+ destruct (eq_dec_args args eq_args).
+ 2: discriminate.
+ simpl in FIND.
+ intuition congruence.
+ Qed.
+
+ Hint Resolve rhs_find_sound : cse3.
+
+ Theorem forward_move_rhs_sound :
+ forall sop args rel rs m v,
+ (sem_rel rel rs m) ->
+ (sem_rhs sop args rs m v) ->
+ (sem_rhs sop (forward_move_l (ctx := ctx) rel args) rs m v).
+ Proof.
+ intros until v.
+ intros REL RHS.
+ destruct sop; simpl in *.
+ all: erewrite forward_move_l_sound by eassumption; assumption.
+ Qed.
+
+ Hint Resolve forward_move_rhs_sound : cse3.
+
+ Lemma arg_not_replaced:
+ forall (rs : regset) dst v args,
+ ~ In dst args ->
+ (rs # dst <- v) ## args = rs ## args.
+ Proof.
+ induction args; simpl; trivial.
+ intuition.
+ f_equal; trivial.
+ apply Regmap.gso; congruence.
+ Qed.
+
+ Lemma sem_rhs_depends_on_args_only:
+ forall sop args rs dst m v,
+ sem_rhs sop args rs m v ->
+ ~ In dst args ->
+ sem_rhs sop args (rs # dst <- v) m v.
+ Proof.
+ unfold sem_rhs.
+ intros.
+ rewrite arg_not_replaced by assumption.
+ assumption.
+ Qed.
+
+ Lemma replace_sound:
+ forall no eqno dst sop args rel rs m v,
+ sem_rel rel rs m ->
+ sem_rhs sop args rs m v ->
+ ~ In dst args ->
+ eq_find (ctx := ctx) no (Equ dst sop args) = Some eqno ->
+ sem_rel (PSet.add eqno (kill_reg (ctx := ctx) dst rel)) (rs # dst <- v) m.
+ Proof.
+ intros until v.
+ intros REL RHS NOTIN FIND i eq CONTAINS CATALOG.
+ destruct (peq i eqno).
+ - subst i.
+ rewrite eq_find_sound with (no := no) (eq0 := Equ dst sop args) in CATALOG by exact FIND.
+ clear FIND.
+ inv CATALOG.
+ unfold sem_eq.
+ simpl in *.
+ rewrite Regmap.gss.
+ apply sem_rhs_depends_on_args_only; auto.
+ - rewrite PSet.gaddo in CONTAINS by congruence.
+ eapply kill_reg_sound; eauto.
+ Qed.
+
+ Lemma sem_rhs_det:
+ forall {sop} {args} {rs} {m} {v} {v'},
+ sem_rhs sop args rs m v ->
+ sem_rhs sop args rs m v' ->
+ v = v'.
+ Proof.
+ intros until v'. intro SEMv.
+ destruct sop; simpl in *.
+ - destruct eval_operation.
+ congruence.
+ contradiction.
+ - destruct eval_addressing.
+ + destruct Mem.loadv; congruence.
+ + congruence.
+ Qed.
+
+ Lemma arglist_idem_write:
+ forall { A : Type} args (rs : Regmap.t A) dst,
+ (rs # dst <- (rs # dst)) ## args = rs ## args.
+ Proof.
+ induction args; trivial.
+ intros. cbn.
+ f_equal; trivial.
+ apply Regmap.gsident.
+ Qed.
+
+ Lemma sem_rhs_idem_write:
+ forall sop args rs dst m v,
+ sem_rhs sop args rs m v ->
+ sem_rhs sop args (rs # dst <- (rs # dst)) m v.
+ Proof.
+ intros.
+ unfold sem_rhs in *.
+ rewrite arglist_idem_write.
+ assumption.
+ Qed.
+
+ Theorem oper2_sound:
+ forall no dst sop args rel rs m v,
+ sem_rel rel rs m ->
+ not (In dst args) ->
+ sem_rhs sop args rs m v ->
+ sem_rel (oper2 (ctx := ctx) no dst sop args rel) (rs # dst <- v) m.
+ Proof.
+ unfold oper2.
+ intros until v.
+ intros REL NOTIN RHS.
+ pose proof (eq_find_sound no (Equ dst sop args)) as EQ_FIND_SOUND.
+ destruct eq_find.
+ 2: auto with cse3; fail.
+ specialize EQ_FIND_SOUND with (id := e).
+ intuition.
+ intros i eq CONTAINS.
+ destruct (peq i e).
+ { subst i.
+ rewrite H.
+ clear H.
+ intro Z.
+ inv Z.
+ unfold sem_eq.
+ simpl.
+ rewrite Regmap.gss.
+ apply sem_rhs_depends_on_args_only; auto.
+ }
+ intros INi.
+ destruct (PSet.contains rel e) eqn:CONTAINSe.
+ { pose proof (REL e (Equ dst sop args) CONTAINSe H) as RELe.
+ pose proof (REL i eq CONTAINS INi) as RELi.
+ destruct eq as [eq_lhs eq_sop eq_args | eq_cond eq_args]; cbn in *.
+ - replace v with (rs # dst) by (eapply sem_rhs_det; eassumption).
+ rewrite Regmap.gsident.
+ apply sem_rhs_idem_write.
+ assumption.
+ - replace v with (rs # dst) by (eapply sem_rhs_det; eassumption).
+ rewrite arglist_idem_write.
+ assumption.
+ }
+ rewrite PSet.gaddo in CONTAINS by congruence.
+ apply (kill_reg_sound rel rs m dst v REL i eq); auto.
+ Qed.
+
+ Hint Resolve oper2_sound : cse3.
+
+ Theorem oper1_sound:
+ forall no dst sop args rel rs m v,
+ sem_rel rel rs m ->
+ sem_rhs sop args rs m v ->
+ sem_rel (oper1 (ctx := ctx) no dst sop args rel) (rs # dst <- v) m.
+ Proof.
+ intros.
+ unfold oper1.
+ destruct in_dec; auto with cse3.
+ Qed.
+
+ Hint Resolve oper1_sound : cse3.
+
+ Lemma rel_idem_replace:
+ forall rel rs r m,
+ sem_rel rel rs m ->
+ sem_rel rel rs # r <- (rs # r) m.
+ Proof.
+ intros until m.
+ intro REL.
+ unfold sem_rel, sem_eq, sem_rhs in *.
+ intros.
+ specialize REL with (i:=i) (eq0:=eq).
+ destruct eq as [lhs sop args | cond args] eqn:EQ.
+ * rewrite Regmap.gsident.
+ replace ((rs # r <- (rs # r)) ## args) with
+ (rs ## args).
+ { apply REL; auto. }
+ apply list_map_exten.
+ intros.
+ apply Regmap.gsident.
+ (* TODO simplify? *)
+ * rewrite arglist_idem_write.
+ auto.
+ Qed.
+
+ Lemma move_sound :
+ forall no : node,
+ forall rel : RELATION.t,
+ forall src dst : reg,
+ forall rs m,
+ sem_rel rel rs m ->
+ sem_rel (move (ctx:=ctx) no src dst rel) (rs # dst <- (rs # src)) m.
+ Proof.
+ unfold move.
+ intros until m.
+ intro REL.
+ destruct (peq src dst).
+ { subst dst.
+ apply rel_idem_replace; auto.
+ }
+ pose proof (eq_find_sound no (Equ dst (SOp Omove) (src::nil))) as EQ_FIND_SOUND.
+ destruct eq_find.
+ - intros i eq CONTAINS.
+ destruct (peq i e).
+ + subst i.
+ rewrite (EQ_FIND_SOUND e) by trivial.
+ intro Z.
+ inv Z.
+ unfold sem_eq.
+ simpl.
+ destruct (peq src dst).
+ * subst dst.
+ reflexivity.
+ * rewrite Regmap.gss.
+ rewrite Regmap.gso by congruence.
+ reflexivity.
+ + intros.
+ rewrite PSet.gaddo in CONTAINS by congruence.
+ apply (kill_reg_sound rel rs m dst (rs # src) REL i); auto.
+ - apply kill_reg_sound; auto.
+ Qed.
+
+ Hint Resolve move_sound : cse3.
+
+ Theorem oper_sound:
+ forall no dst sop args rel rs m v,
+ sem_rel rel rs m ->
+ sem_rhs sop args rs m v ->
+ sem_rel (oper (ctx := ctx) no dst sop args rel) (rs # dst <- v) m.
+ Proof.
+ intros until v.
+ intros REL RHS.
+ unfold oper.
+ destruct (is_smove sop).
+ - subst.
+ simpl in RHS.
+ destruct args. contradiction.
+ destruct args. 2: contradiction.
+ cbn in *.
+ subst.
+ rewrite <- (forward_move_sound rel rs m r) by auto.
+ apply move_sound; auto.
+ - destruct rhs_find as [src |] eqn:RHS_FIND.
+ + destruct (Compopts.optim_CSE3_glb tt).
+ * apply sem_rel_glb; split.
+ ** pose proof (rhs_find_sound no sop args rel src rs m REL RHS_FIND) as SOUND.
+ rewrite <- (sem_rhs_det SOUND RHS).
+ apply move_sound; auto.
+ ** apply sem_rel_glb; split.
+ *** apply oper1_sound; auto.
+ *** apply oper1_sound; auto.
+ apply forward_move_rhs_sound; auto.
+ * apply sem_rel_glb; split.
+ ** apply oper1_sound; auto.
+ ** apply oper1_sound; auto.
+ apply forward_move_rhs_sound; auto.
+ + apply sem_rel_glb; split.
+ * apply oper1_sound; auto.
+ * apply oper1_sound; auto.
+ apply forward_move_rhs_sound; auto.
+ Qed.
+
+ Hint Resolve oper_sound : cse3.
+
+
+ Theorem clever_kill_store_sound:
+ forall chunk addr args a src rel rs m m',
+ sem_rel rel rs m ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.storev chunk m a (rs # src) = Some m' ->
+ sem_rel (clever_kill_store (ctx:=ctx) chunk addr args src rel) rs m'.
+ Proof.
+ unfold clever_kill_store.
+ intros until m'. intros REL ADDR STORE i eq CONTAINS CATALOG.
+ autorewrite with pset in CONTAINS.
+ destruct (PSet.contains rel i) eqn:RELi; simpl in CONTAINS.
+ 2: discriminate.
+ rewrite CATALOG in CONTAINS.
+ unfold sem_rel in REL.
+ specialize REL with (i := i) (eq0 := eq).
+ destruct eq as [eq_lhs eq_sop eq_args | eq_cond eq_args]; simpl in *.
+ * unfold sem_eq in *.
+ simpl in *.
+ destruct eq_sop as [op' | chunk' addr']; simpl.
+ - rewrite op_valid_pointer_eq with (m2 := m).
+ + cbn in *.
+ apply REL; auto.
+ + eapply store_preserves_validity; eauto.
+ - simpl in REL.
+ erewrite ctx_kill_store_has_depends_on_store in CONTAINS by eauto.
+ simpl in CONTAINS.
+ rewrite negb_true_iff in CONTAINS.
+ destruct (eval_addressing genv sp addr' rs ## eq_args) as [a'|] eqn:ADDR'.
+ + erewrite may_overlap_sound with (chunk:=chunk) (addr:=addr) (args:=args) (chunk':=chunk') (addr':=addr') (args':=eq_args); try eassumption.
+ apply REL; auto.
+ + apply REL; auto.
+ * rewrite cond_valid_pointer_eq with (m2 := m).
+ auto.
+ eapply store_preserves_validity; eauto.
+ Qed.
+
+ Hint Resolve clever_kill_store_sound : cse3.
+
+ Theorem store2_sound:
+ forall chunk addr args a src rel rs m m',
+ sem_rel rel rs m ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.storev chunk m a (rs # src) = Some m' ->
+ sem_rel (store2 (ctx:=ctx) chunk addr args src rel) rs m'.
+ Proof.
+ unfold store2.
+ intros.
+ destruct (Compopts.optim_CSE3_alias_analysis tt); eauto with cse3.
+ Qed.
+
+ Hint Resolve store2_sound : cse3.
+
+ Theorem store1_sound:
+ forall no chunk addr args a src rel tenv rs m m',
+ sem_rel rel rs m ->
+ wt_regset tenv rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.storev chunk m a (rs#src) = Some m' ->
+ sem_rel (store1 (ctx:=ctx) no chunk addr args src (tenv src) rel) rs m'.
+ Proof.
+ unfold store1.
+ intros until m'.
+ intros REL WT ADDR STORE.
+ assert (sem_rel (store2 (ctx:=ctx) chunk addr args src rel) rs m') as REL' by eauto with cse3.
+ destruct loadv_storev_compatible_type eqn:COMPATIBLE.
+ 2: auto; fail.
+ destruct eq_find as [eq_id | ] eqn:FIND.
+ 2: auto; fail.
+ intros i eq CONTAINS CATALOG.
+ destruct (peq i eq_id).
+ { subst i.
+ rewrite eq_find_sound with (no:=no) (eq0:=Equ src (SLoad chunk addr) args) in CATALOG; trivial.
+ inv CATALOG.
+ unfold sem_eq.
+ simpl.
+ rewrite ADDR.
+ rewrite loadv_storev_really_same with (m1:=m) (v:=rs#src) (ty:=(tenv src)); trivial.
+ }
+ unfold sem_rel in REL'.
+ rewrite PSet.gaddo in CONTAINS by congruence.
+ eauto.
+ Qed.
+
+ Hint Resolve store1_sound : cse3.
+
+
+ Theorem store_sound:
+ forall no chunk addr args a src rel tenv rs m m',
+ sem_rel rel rs m ->
+ wt_regset tenv rs ->
+ eval_addressing genv sp addr (rs ## args) = Some a ->
+ Mem.storev chunk m a (rs#src) = Some m' ->
+ sem_rel (store (ctx:=ctx) no tenv chunk addr args src rel) rs m'.
+ Proof.
+ unfold store.
+ intros until m'.
+ intros REL WT ADDR STORE.
+ apply sem_rel_glb; split.
+ - apply sem_rel_glb; split.
+ * apply store1_sound with (a := a) (m := m); trivial.
+ * rewrite <- forward_move_l_sound with (rel:=rel) (m:=m) in ADDR by trivial.
+ apply store1_sound with (a := a) (m := m); trivial.
+ - rewrite <- forward_move_sound with (rel:=rel) (m:=m) in STORE by trivial.
+ apply sem_rel_glb; split.
+ * apply store1_sound with (a := a) (m := m); trivial.
+ * rewrite <- forward_move_l_sound with (rel:=rel) (m:=m) in ADDR by trivial.
+ apply store1_sound with (a := a) (m := m); trivial.
+ Qed.
+
+ Hint Resolve store_sound : cse3.
+
+ Lemma kill_builtin_res_sound:
+ forall res (m : mem) (rs : regset) vres (rel : RELATION.t)
+ (REL : sem_rel rel rs m),
+ (sem_rel (kill_builtin_res (ctx:=ctx) res rel)
+ (regmap_setres res vres rs) m).
+ Proof.
+ destruct res; simpl; intros; trivial.
+ apply kill_reg_sound; trivial.
+ Qed.
+
+ Hint Resolve kill_builtin_res_sound : cse3.
+
+ Lemma top_sound:
+ forall rs m, (sem_rel RELATION.top rs m).
+ Proof.
+ unfold RELATION.top, sem_rel.
+ intros.
+ rewrite PSet.gempty in H.
+ discriminate.
+ Qed.
+
+ Hint Resolve top_sound : cse3.
+
+ Lemma external_call_sound:
+ forall ge ef (rel : RELATION.t) (m m' : mem) (rs : regset) vargs t vres
+ (REL : sem_rel rel rs m)
+ (CALL : external_call ef ge vargs m t vres m'),
+ sem_rel (apply_external_call (ctx:=ctx) ef rel) rs m'.
+ Proof.
+ destruct ef; intros; simpl in *.
+ all: eauto using kill_mem_sound.
+ all: unfold builtin_or_external_sem in *.
+ 1, 2, 3, 5, 6: destruct (Compopts.optim_CSE3_across_calls tt).
+ all: eauto using kill_mem_sound, top_sound.
+ 1, 2, 3: destruct (Builtins.lookup_builtin_function name sg).
+ all: eauto using kill_mem_sound, top_sound.
+ all: inv CALL; eauto using kill_mem_sound.
+ Qed.
+
+ Hint Resolve external_call_sound : cse3.
+
+
+ Definition sem_rel_b (rel : RB.t) (rs : regset) (m : mem) :=
+ match rel with
+ | None => False
+ | Some rel => sem_rel rel rs m
+ end.
+
+ Lemma apply_cond1_sound :
+ forall pc cond args rel rs m
+ (COND : (eval_condition cond (rs ## args) m) = Some true)
+ (REL : (sem_rel rel rs m)),
+ (sem_rel_b (apply_cond1 (ctx:=ctx) pc cond args rel) rs m).
+ Proof.
+ intros.
+ unfold apply_cond1.
+ destruct eq_find as [eq_id | ] eqn:FIND; cbn.
+ 2: assumption.
+ destruct PSet.contains eqn:CONTAINS.
+ {
+ pose proof (eq_find_sound pc (Cond (negate_condition cond) args) eq_id FIND) as FIND_SOUND.
+ unfold sem_rel in REL.
+ pose proof (REL eq_id (Cond (negate_condition cond) args) CONTAINS FIND_SOUND) as REL_id.
+ cbn in REL_id.
+ rewrite eval_negate_condition in REL_id.
+ rewrite COND in REL_id.
+ discriminate.
+ }
+ exact REL.
+ Qed.
+
+ Lemma apply_cond0_sound :
+ forall pc cond args rel rs m
+ (COND : (eval_condition cond (rs ## args) m) = Some true)
+ (REL : (sem_rel rel rs m)),
+ (sem_rel (apply_cond0 (ctx:=ctx) pc cond args rel) rs m).
+ Proof.
+ intros.
+ unfold apply_cond0.
+ destruct eq_find as [eq_id | ] eqn:FIND; cbn.
+ 2: assumption.
+ pose proof (eq_find_sound pc (Cond cond args) eq_id FIND) as FIND_SOUND.
+ intros eq_id' eq' CONTAINS CATALOG.
+ destruct (peq eq_id eq_id').
+ { subst eq_id'.
+ unfold sem_eq.
+ rewrite FIND_SOUND in CATALOG.
+ inv CATALOG.
+ assumption.
+ }
+ rewrite PSet.gaddo in CONTAINS by assumption.
+ unfold sem_rel in REL.
+ eapply REL; eassumption.
+ Qed.
+
+ Lemma apply_cond_sound :
+ forall pc cond args rel rs m
+ (COND : (eval_condition cond (rs ## args) m) = Some true)
+ (REL : (sem_rel rel rs m)),
+ (sem_rel_b (apply_cond (ctx:=ctx) pc cond args rel) rs m).
+ Proof.
+ unfold apply_cond.
+ intros.
+ pose proof (apply_cond1_sound pc cond args rel rs m COND REL) as SOUND1.
+ destruct apply_cond1 eqn:COND1.
+ { apply apply_cond0_sound; auto. }
+ exact SOUND1.
+ Qed.
+
+ (*
+ Section INDUCTIVENESS.
+ Variable fn : RTL.function.
+ Variable tenv : typing_env.
+ Variable inv: invariants.
+
+ Definition is_inductive_step (pc pc' : node) :=
+ forall instr,
+ PTree.get pc (fn_code fn) = Some instr ->
+ In pc' (successors_instr instr) ->
+ RB.ge (PMap.get pc' inv)
+ (match apply_instr' (ctx:=ctx) tenv (fn_code fn) pc
+ (PMap.get pc inv) with
+ | Abst_same rel' => rel'
+ end).
+
+ Definition is_inductive_allstep :=
+ forall pc pc', is_inductive_step pc pc'.
+
+ Lemma checked_is_inductive_allstep:
+ (check_inductiveness (ctx:=ctx) fn tenv inv) = true ->
+ is_inductive_allstep.
+ Proof.
+ unfold check_inductiveness, is_inductive_allstep, is_inductive_step.
+ rewrite andb_true_iff.
+ rewrite PTree_Properties.for_all_correct.
+ intros (ENTRYPOINT & ALL).
+ intros until instr.
+ intros INSTR IN_SUCC.
+ specialize ALL with (x := pc) (a := instr).
+ pose proof (ALL INSTR) as AT_PC.
+ destruct (inv # pc).
+ 2: apply RB.ge_bot.
+ unfold apply_instr'.
+ rewrite INSTR.
+ destruct apply_instr.
+ { (* same *)
+ rewrite List.forallb_forall in AT_PC.
+ apply relb_leb_correct.
+ auto.
+ }
+ Qed.
+
+ Lemma checked_is_inductive_entry:
+ (check_inductiveness (ctx:=ctx) fn tenv inv) = true ->
+ inv # (fn_entrypoint fn) = Some RELATION.top.
+ Proof.
+ unfold check_inductiveness, is_inductive_allstep, is_inductive_step.
+ rewrite andb_true_iff.
+ intros (ENTRYPOINT & ALL).
+ apply RB.beq_correct in ENTRYPOINT.
+ unfold RB.eq, RELATION.eq in ENTRYPOINT.
+ destruct (inv # (fn_entrypoint fn)) as [rel | ].
+ 2: contradiction.
+ f_equal.
+ symmetry.
+ assumption.
+ Qed.
+ End INDUCTIVENESS.
+
+ Hint Resolve checked_is_inductive_allstep checked_is_inductive_entry : cse3.
+ *)
+End SOUNDNESS.
diff --git a/backend/CSE3proof.v b/backend/CSE3proof.v
new file mode 100644
index 00000000..a601d5d5
--- /dev/null
+++ b/backend/CSE3proof.v
@@ -0,0 +1,1221 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+Replace available expressions by the register containing their value.
+
+Proofs.
+
+David Monniaux, CNRS, VERIMAG
+ *)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE3 CSE3analysis CSE3analysisproof.
+Require Import RTLtyping.
+
+Section PARAMS.
+ Variable params : cse3params.
+
+Definition param_match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => param_transf_fundef params f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, param_transf_program params p = OK tp -> param_match_prog p tp.
+Proof.
+ intros. eapply match_transform_partial_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSF: param_match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Section SOUNDNESS.
+Variable sp : val.
+Variable ctx : eq_context.
+
+Definition sem_rel_b (rel : RB.t) (rs : regset) (m : mem) :=
+ match rel with
+ | None => False
+ | Some rel => sem_rel (ctx:=ctx) (genv:=ge) (sp:=sp) rel rs m
+ end.
+
+Lemma forward_move_b_sound :
+ forall rel rs m x,
+ (sem_rel_b rel rs m) ->
+ rs # (forward_move_b (ctx := ctx) rel x) = rs # x.
+Proof.
+ destruct rel as [rel | ]; simpl; intros.
+ 2: contradiction.
+ eapply forward_move_sound; eauto.
+ Qed.
+
+ Lemma forward_move_l_b_sound :
+ forall rel rs m x,
+ (sem_rel_b rel rs m) ->
+ rs ## (forward_move_l_b (ctx := ctx) rel x) = rs ## x.
+ Proof.
+ destruct rel as [rel | ]; simpl; intros.
+ 2: contradiction.
+ eapply forward_move_l_sound; eauto.
+ Qed.
+
+ Definition fmap_sem (fmap : PMap.t RB.t) (pc : node) (rs : regset) (m : mem) :=
+ sem_rel_b (PMap.get pc fmap) rs m.
+
+ Lemma subst_arg_ok:
+ forall invariants,
+ forall pc,
+ forall rs,
+ forall m,
+ forall arg,
+ forall (SEM : fmap_sem invariants pc rs m),
+ rs # (subst_arg (ctx:=ctx) invariants pc arg) = rs # arg.
+ Proof.
+ intros.
+ apply forward_move_b_sound with (m:=m).
+ assumption.
+ Qed.
+
+ Lemma subst_args_ok:
+ forall invariants,
+ forall pc,
+ forall rs,
+ forall m,
+ forall args,
+ forall (SEM : fmap_sem invariants pc rs m),
+ rs ## (subst_args (ctx:=ctx) invariants pc args) = rs ## args.
+ Proof.
+ intros.
+ apply forward_move_l_b_sound with (m:=m).
+ assumption.
+ Qed.
+End SOUNDNESS.
+
+Lemma functions_translated:
+ forall (v: val) (f: RTL.fundef),
+ Genv.find_funct ge v = Some f ->
+ exists tf,
+ Genv.find_funct tge v = Some tf /\ param_transf_fundef params f = OK tf.
+Proof.
+ apply (Genv.find_funct_transf_partial TRANSF).
+Qed.
+
+Lemma function_ptr_translated:
+ forall (b: block) (f: RTL.fundef),
+ Genv.find_funct_ptr ge b = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge b = Some tf /\ param_transf_fundef params f = OK tf.
+Proof.
+ apply (Genv.find_funct_ptr_transf_partial TRANSF).
+Qed.
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof.
+ apply (Genv.find_symbol_match TRANSF).
+Qed.
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof.
+ apply (Genv.senv_match TRANSF).
+Qed.
+
+Lemma sig_preserved:
+ forall f tf, param_transf_fundef params f = OK tf -> funsig tf = funsig f.
+Proof.
+ destruct f; simpl; intros.
+ - monadInv H.
+ monadInv EQ.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ1.
+ reflexivity.
+ - monadInv H.
+ reflexivity.
+Qed.
+
+Lemma stacksize_preserved:
+ forall f tf, param_transf_function params f = OK tf -> fn_stacksize tf = fn_stacksize f.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ0.
+ reflexivity.
+Qed.
+
+Lemma params_preserved:
+ forall f tf, param_transf_function params f = OK tf -> fn_params tf = fn_params f.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ0.
+ reflexivity.
+Qed.
+
+Lemma entrypoint_preserved:
+ forall f tf, param_transf_function params f = OK tf -> fn_entrypoint tf = fn_entrypoint f.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ0.
+ reflexivity.
+Qed.
+
+Lemma sig_preserved2:
+ forall f tf, param_transf_function params f = OK tf -> fn_sig tf = fn_sig f.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ0.
+ reflexivity.
+Qed.
+
+Lemma transf_function_is_typable:
+ forall f tf, param_transf_function params f = OK tf ->
+ exists tenv, type_function f = OK tenv.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ exists x.
+ assumption.
+Qed.
+Lemma transf_function_invariants_inductive:
+ forall f tf tenv, param_transf_function params f = OK tf ->
+ type_function f = OK tenv ->
+ check_inductiveness (ctx:=(context_from_hints (snd (preanalysis tenv f))))
+ f tenv (fst (preanalysis tenv f)) = true.
+Proof.
+ unfold transf_function; destruct f; simpl; intros.
+ monadInv H.
+ replace x with tenv in * by congruence.
+ clear x.
+ destruct preanalysis as [invariants hints].
+ destruct check_inductiveness; trivial; discriminate.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ exists tfd,
+ find_function tge ros rs = Some tfd /\ param_transf_fundef params fd = OK tfd.
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Inductive match_stackframes: list stackframe -> list stackframe -> signature -> Prop :=
+ | match_stackframes_nil: forall sg,
+ sg.(sig_res) = Tint ->
+ match_stackframes nil nil sg
+ | match_stackframes_cons:
+ forall res f sp pc rs s tf ts sg tenv
+ (STACKS: match_stackframes s ts (fn_sig tf))
+ (FUN: param_transf_function params f = OK tf)
+ (WTF: type_function f = OK tenv)
+ (WTRS: wt_regset tenv rs)
+ (WTRES: tenv res = proj_sig_res sg)
+ (REL: forall m vres,
+ sem_rel_b sp (context_from_hints (snd (preanalysis tenv f)))
+ ((fst (preanalysis tenv f))#pc) (rs#res <- vres) m),
+
+ match_stackframes
+ (Stackframe res f sp pc rs :: s)
+ (Stackframe res tf sp pc rs :: ts)
+ sg.
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s f sp pc rs m ts tf tenv
+ (STACKS: match_stackframes s ts (fn_sig tf))
+ (FUN: param_transf_function params f = OK tf)
+ (WTF: type_function f = OK tenv)
+ (WTRS: wt_regset tenv rs)
+ (REL: sem_rel_b sp (context_from_hints (snd (preanalysis tenv f))) ((fst (preanalysis tenv f))#pc) rs m),
+ match_states (State s f sp pc rs m)
+ (State ts tf sp pc rs m)
+ | match_states_call:
+ forall s f args m ts tf
+ (STACKS: match_stackframes s ts (funsig tf))
+ (FUN: param_transf_fundef params f = OK tf)
+ (WTARGS: Val.has_type_list args (sig_args (funsig tf))),
+ match_states (Callstate s f args m)
+ (Callstate ts tf args m)
+ | match_states_return:
+ forall s res m ts sg
+ (STACKS: match_stackframes s ts sg)
+ (WTRES: Val.has_type res (proj_sig_res sg)),
+ match_states (Returnstate s res m)
+ (Returnstate ts res m).
+
+Lemma match_stackframes_change_sig:
+ forall s ts sg sg',
+ match_stackframes s ts sg ->
+ sg'.(sig_res) = sg.(sig_res) ->
+ match_stackframes s ts sg'.
+Proof.
+ intros. inv H.
+ constructor. congruence.
+ econstructor; eauto.
+ unfold proj_sig_res in *. rewrite H0; auto.
+Qed.
+
+Lemma transf_function_at:
+ forall f tf pc tenv instr
+ (TF : param_transf_function params f = OK tf)
+ (TYPE : type_function f = OK tenv)
+ (PC : (fn_code f) ! pc = Some instr),
+ (fn_code tf) ! pc = Some (param_transf_instr
+ (ctx := (context_from_hints (snd (preanalysis tenv f))))
+ params (fst (preanalysis tenv f))
+ pc instr).
+Proof.
+ intros.
+ unfold transf_function in TF.
+ monadInv TF.
+ replace x with tenv in * by congruence.
+ clear EQ.
+ destruct (preanalysis tenv f) as [invariants hints].
+ destruct check_inductiveness.
+ 2: discriminate.
+ inv EQ0.
+ simpl.
+ rewrite PTree.gmap.
+ rewrite PC.
+ reflexivity.
+Qed.
+
+Ltac TR_AT := erewrite transf_function_at by eauto.
+
+Hint Resolve wt_instrs type_function_correct : wt.
+
+Lemma wt_undef :
+ forall tenv rs dst,
+ wt_regset tenv rs ->
+ wt_regset tenv rs # dst <- Vundef.
+Proof.
+ unfold wt_regset.
+ intros.
+ destruct (peq r dst).
+ { subst dst.
+ rewrite Regmap.gss.
+ constructor.
+ }
+ rewrite Regmap.gso by congruence.
+ auto.
+Qed.
+
+Lemma rel_ge:
+ forall inv inv'
+ (GE : RELATION.ge inv' inv)
+ ctx sp rs m
+ (REL: sem_rel (genv:=ge) (sp:=sp) (ctx:=ctx) inv rs m),
+ sem_rel (genv:=ge) (sp:=sp) (ctx:=ctx) inv' rs m.
+Proof.
+ unfold sem_rel, RELATION.ge.
+ intros.
+ apply (REL i); trivial.
+ eapply HashedSet.PSet.is_subset_spec1; eassumption.
+Qed.
+
+Hint Resolve rel_ge : cse3.
+
+Lemma relb_ge:
+ forall inv inv'
+ (GE : RB.ge inv' inv)
+ ctx sp rs m
+ (REL: sem_rel_b sp ctx inv rs m),
+ sem_rel_b sp ctx inv' rs m.
+Proof.
+ intros.
+ destruct inv; cbn in *.
+ 2: contradiction.
+ destruct inv'; cbn in *.
+ 2: assumption.
+ eapply rel_ge; eassumption.
+Qed.
+
+Hint Resolve relb_ge : cse3.
+
+Lemma sem_rhs_sop :
+ forall sp op rs args m v,
+ eval_operation ge sp op rs ## args m = Some v ->
+ sem_rhs (genv:=ge) (sp:=sp) (SOp op) args rs m v.
+Proof.
+ intros. simpl.
+ rewrite H.
+ reflexivity.
+Qed.
+
+Hint Resolve sem_rhs_sop : cse3.
+
+Lemma sem_rhs_sload :
+ forall sp chunk addr rs args m a v,
+ eval_addressing ge sp addr rs ## args = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ sem_rhs (genv:=ge) (sp:=sp) (SLoad chunk addr) args rs m v.
+Proof.
+ intros. simpl.
+ rewrite H. rewrite H0.
+ reflexivity.
+Qed.
+
+Hint Resolve sem_rhs_sload : cse3.
+
+Lemma sem_rhs_sload_notrap1 :
+ forall sp chunk addr rs args m,
+ eval_addressing ge sp addr rs ## args = None ->
+ sem_rhs (genv:=ge) (sp:=sp) (SLoad chunk addr) args rs m Vundef.
+Proof.
+ intros. simpl.
+ rewrite H.
+ reflexivity.
+Qed.
+
+Hint Resolve sem_rhs_sload_notrap1 : cse3.
+
+Lemma sem_rhs_sload_notrap2 :
+ forall sp chunk addr rs args m a,
+ eval_addressing ge sp addr rs ## args = Some a ->
+ Mem.loadv chunk m a = None ->
+ sem_rhs (genv:=ge) (sp:=sp) (SLoad chunk addr) args rs m Vundef.
+Proof.
+ intros. simpl.
+ rewrite H. rewrite H0.
+ reflexivity.
+Qed.
+
+Hint Resolve sem_rhs_sload_notrap2 : cse3.
+
+Lemma sem_rel_top:
+ forall ctx sp rs m, sem_rel (genv:=ge) (sp:=sp) (ctx:=ctx) RELATION.top rs m.
+Proof.
+ unfold sem_rel, RELATION.top.
+ intros.
+ rewrite HashedSet.PSet.gempty in *.
+ discriminate.
+Qed.
+
+Hint Resolve sem_rel_top : cse3.
+
+Lemma sem_rel_b_top:
+ forall ctx sp rs m, sem_rel_b sp ctx (Some RELATION.top) rs m.
+Proof.
+ intros. simpl.
+ apply sem_rel_top.
+Qed.
+
+Hint Resolve sem_rel_b_top : cse3.
+
+(*
+Ltac IND_STEP :=
+ match goal with
+ REW: (fn_code ?fn) ! ?mpc = Some ?minstr
+ |-
+ sem_rel_b ?sp (context_from_hints (snd (preanalysis ?tenv ?fn))) ((fst (preanalysis ?tenv ?fn)) # ?mpc') ?rs ?m =>
+ assert (is_inductive_allstep (ctx:= (context_from_hints (snd (preanalysis tenv fn)))) fn tenv (fst (preanalysis tenv fn))) as IND by
+ (apply checked_is_inductive_allstep;
+ eapply transf_function_invariants_inductive; eassumption);
+ unfold is_inductive_allstep, is_inductive_step, apply_instr' in IND;
+ specialize IND with (pc:=mpc) (pc':=mpc') (instr:=minstr);
+ simpl in IND;
+ rewrite REW in IND;
+ simpl in IND;
+ destruct ((fst (preanalysis tenv fn)) # mpc') as [zinv' | ];
+ destruct ((fst (preanalysis tenv fn)) # mpc) as [zinv | ];
+ simpl in *;
+ intuition;
+ eapply rel_ge; eauto with cse3 (* ; for printing
+ idtac mpc mpc' fn minstr *)
+ end.
+ *)
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS.
+ all: try set (ctx := (context_from_hints (snd (preanalysis tenv f)))) in *.
+ all: try set (invs := (fst (preanalysis tenv f))) in *.
+ - (* Inop *)
+ exists (State ts tf sp pc' rs m). split.
+ + apply exec_Inop; auto.
+ TR_AT. reflexivity.
+ + econstructor; eauto.
+
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ unfold sem_rel_b.
+ apply (rel_ge inv_pc inv_pc'); auto.
+ (* END INVARIANT *)
+
+ - (* Iop *)
+ exists (State ts tf sp pc' (rs # res <- v) m). split.
+ + pose (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iop op args res pc')) as instr'.
+ assert (instr' = (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iop op args res pc'))) by reflexivity.
+ unfold param_transf_instr, find_op_in_fmap in instr'.
+ destruct (@PMap.get (option RELATION.t) pc) eqn:INV_PC.
+ pose proof (rhs_find_sound (sp:=sp) (genv:=ge) (ctx:=(context_from_hints (snd (preanalysis tenv f)))) pc (SOp op)
+ (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) t) as FIND_SOUND.
+ * destruct (if (negb params.(cse3_operations) || (negb (params.(cse3_trivial_ops))) && (is_trivial_op op))
+ then None
+ else
+ rhs_find pc (SOp op)
+ (subst_args (fst (preanalysis tenv f)) pc args) t) eqn:FIND.
+ ** destruct (negb params.(cse3_operations) || ((negb (params.(cse3_trivial_ops))) && (is_trivial_op op))). discriminate.
+ apply exec_Iop with (op := Omove) (args := r :: nil).
+ TR_AT.
+ subst instr'.
+ congruence.
+ simpl.
+ specialize FIND_SOUND with (src := r) (rs := rs) (m := m).
+ simpl in FIND_SOUND.
+ rewrite subst_args_ok with (sp:=sp) (m:=m) in FIND_SOUND.
+ rewrite H0 in FIND_SOUND.
+ rewrite FIND_SOUND; auto.
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ ** apply exec_Iop with (op := op) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)).
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_operation_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ * apply exec_Iop with (op := op) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)).
+ TR_AT.
+ { subst instr'.
+ rewrite if_same in H1.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_operation_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ + econstructor; eauto.
+ * eapply wt_exec_Iop with (f:=f); try eassumption.
+ eauto with wt.
+ *
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ apply oper_sound; unfold ctx; eauto with cse3.
+ (* END INVARIANT *)
+ - (* Iload *)
+ exists (State ts tf sp pc' (rs # dst <- v) m). split.
+ + pose (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload trap chunk addr args dst pc')) as instr'.
+ assert (instr' = (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload trap chunk addr args dst pc'))) by reflexivity.
+ unfold param_transf_instr, find_load_in_fmap in instr'.
+ destruct (@PMap.get (option RELATION.t) pc) eqn:INV_PC.
+ pose proof (rhs_find_sound (sp:=sp) (genv:=ge) (ctx:=(context_from_hints (snd (preanalysis tenv f)))) pc (SLoad chunk addr)
+ (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) t) as FIND_SOUND.
+ * destruct rhs_find eqn:FIND.
+ ** apply exec_Iop with (op := Omove) (args := r :: nil).
+ TR_AT.
+ subst instr'.
+ congruence.
+ simpl.
+ specialize FIND_SOUND with (src := r) (rs := rs) (m := m).
+ simpl in FIND_SOUND.
+ rewrite subst_args_ok with (sp:=sp) (m:=m) in FIND_SOUND.
+ rewrite H0 in FIND_SOUND. (* ADDR *)
+ rewrite H1 in FIND_SOUND. (* LOAD *)
+ rewrite FIND_SOUND; auto.
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ ** apply exec_Iload with (trap := trap) (chunk := chunk) (a := a) (addr := addr) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ * apply exec_Iload with (chunk := chunk) (trap := trap) (addr := addr) (a := a) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ + econstructor; eauto.
+ * eapply wt_exec_Iload with (f:=f); try eassumption.
+ eauto with wt.
+ * (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ apply oper_sound; unfold ctx; eauto with cse3.
+ (* END INVARIANT *)
+
+ - (* Iload notrap1 *)
+ exists (State ts tf sp pc' (rs # dst <- Vundef) m). split.
+ + pose (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload NOTRAP chunk addr args dst pc')) as instr'.
+ assert (instr' = (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload NOTRAP chunk addr args dst pc'))) by reflexivity.
+ unfold param_transf_instr, find_load_in_fmap in instr'.
+ destruct (@PMap.get (option RELATION.t) pc) eqn:INV_PC.
+ pose proof (rhs_find_sound (sp:=sp) (genv:=ge) (ctx:=(context_from_hints (snd (preanalysis tenv f)))) pc (SLoad chunk addr)
+ (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) t) as FIND_SOUND.
+ * destruct rhs_find eqn:FIND.
+ ** apply exec_Iop with (op := Omove) (args := r :: nil).
+ TR_AT.
+ subst instr'.
+ congruence.
+ simpl.
+ specialize FIND_SOUND with (src := r) (rs := rs) (m := m).
+ simpl in FIND_SOUND.
+ rewrite subst_args_ok with (sp:=sp) (m:=m) in FIND_SOUND.
+ rewrite H0 in FIND_SOUND. (* ADDR *)
+ rewrite FIND_SOUND; auto.
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ ** apply exec_Iload_notrap1 with (chunk := chunk) (addr := addr) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ * apply exec_Iload_notrap1 with (chunk := chunk) (addr := addr) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ + econstructor; eauto.
+ * apply wt_undef; assumption.
+ * (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ apply oper_sound; unfold ctx; eauto with cse3.
+ (* END INVARIANT *)
+
+ - (* Iload notrap2 *)
+ exists (State ts tf sp pc' (rs # dst <- Vundef) m). split.
+ + pose (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload NOTRAP chunk addr args dst pc')) as instr'.
+ assert (instr' = (param_transf_instr (ctx:=(context_from_hints (snd (preanalysis tenv f)))) params (fst (preanalysis tenv f)) pc (Iload NOTRAP chunk addr args dst pc'))) by reflexivity.
+ unfold param_transf_instr, find_load_in_fmap in instr'.
+ destruct (@PMap.get (option RELATION.t) pc) eqn:INV_PC.
+ pose proof (rhs_find_sound (sp:=sp) (genv:=ge) (ctx:=(context_from_hints (snd (preanalysis tenv f)))) pc (SLoad chunk addr)
+ (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) t) as FIND_SOUND.
+ * destruct rhs_find eqn:FIND.
+ ** apply exec_Iop with (op := Omove) (args := r :: nil).
+ TR_AT.
+ subst instr'.
+ congruence.
+ simpl.
+ specialize FIND_SOUND with (src := r) (rs := rs) (m := m).
+ simpl in FIND_SOUND.
+ rewrite subst_args_ok with (sp:=sp) (m:=m) in FIND_SOUND.
+ rewrite H0 in FIND_SOUND. (* ADDR *)
+ rewrite H1 in FIND_SOUND. (* LOAD *)
+ rewrite FIND_SOUND; auto.
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ ** apply exec_Iload_notrap2 with (chunk := chunk) (a := a) (addr := addr) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ * apply exec_Iload_notrap2 with (chunk := chunk) (addr := addr) (a := a) (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); trivial.
+ TR_AT.
+ { subst instr'.
+ congruence. }
+ rewrite subst_args_ok with (sp:=sp) (m:=m).
+ {
+ rewrite eval_addressing_preserved with (ge1:=ge) by exact symbols_preserved.
+ assumption.
+ }
+ unfold fmap_sem.
+ change ((fst (preanalysis tenv f)) # pc)
+ with (@PMap.get (option RELATION.t) pc (@fst invariants analysis_hints (preanalysis tenv f))).
+ rewrite INV_PC.
+ assumption.
+ + econstructor; eauto.
+ * apply wt_undef; assumption.
+ * (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ apply oper_sound; unfold ctx; eauto with cse3.
+ (* END INVARIANT *)
+
+ - (* Istore *)
+ exists (State ts tf sp pc' rs m'). split.
+ + eapply exec_Istore with (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args))
+ (src := (subst_arg (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc src)) ; try eassumption.
+ * TR_AT. reflexivity.
+ * rewrite subst_args_ok with (sp:=sp) (m:=m) by trivial.
+ rewrite eval_addressing_preserved with (ge1 := ge) by exact symbols_preserved.
+ eassumption.
+ * rewrite subst_arg_ok with (sp:=sp) (m:=m) by trivial.
+ assumption.
+ + econstructor; eauto.
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ apply store_sound with (a0:=a) (m0:=m); unfold ctx; eauto with cse3.
+ (* END INVARIANT *)
+
+ - (* Icall *)
+ destruct (find_function_translated ros rs fd H0) as [tfd [HTFD1 HTFD2]].
+ econstructor. split.
+ + eapply exec_Icall; try eassumption.
+ * TR_AT. reflexivity.
+ * apply sig_preserved; auto.
+ + rewrite subst_args_ok with (sp:=sp) (m:=m) by trivial.
+ assert (wt_instr f tenv (Icall (funsig fd) ros args res pc')) as WTcall by eauto with wt.
+ inv WTcall.
+ constructor; trivial.
+ * econstructor; eauto.
+ ** rewrite sig_preserved with (f:=fd); assumption.
+ ** intros.
+
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ (* END INVARIANT *)
+ { apply kill_reg_sound; unfold ctx; eauto with cse3.
+ eapply kill_mem_sound; unfold ctx; eauto with cse3. }
+ * rewrite sig_preserved with (f:=fd) by trivial.
+ rewrite <- H7.
+ apply wt_regset_list; auto.
+ - (* Itailcall *)
+ destruct (find_function_translated ros rs fd H0) as [tfd [HTFD1 HTFD2]].
+ econstructor. split.
+ + eapply exec_Itailcall; try eassumption.
+ * TR_AT. reflexivity.
+ * apply sig_preserved; auto.
+ * rewrite stacksize_preserved with (f:=f); eauto.
+ + rewrite subst_args_ok with (m:=m) (sp := (Vptr stk Ptrofs.zero)) by trivial.
+ assert (wt_instr f tenv (Itailcall (funsig fd) ros args)) as WTcall by eauto with wt.
+ inv WTcall.
+ constructor; trivial.
+ * rewrite sig_preserved with (f:=fd) by trivial.
+ inv STACKS.
+ ** econstructor; eauto.
+ rewrite H7.
+ rewrite <- sig_preserved2 with (tf:=tf) by trivial.
+ assumption.
+ ** econstructor; eauto.
+ unfold proj_sig_res in *.
+ rewrite H7.
+ rewrite WTRES.
+ rewrite sig_preserved2 with (f:=f) by trivial.
+ reflexivity.
+ * rewrite sig_preserved with (f:=fd) by trivial.
+ rewrite <- H6.
+ apply wt_regset_list; auto.
+ - (* Ibuiltin *)
+ econstructor. split.
+ + eapply exec_Ibuiltin; try eassumption.
+ * TR_AT. reflexivity.
+ * eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ * eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + econstructor; eauto.
+ * eapply wt_exec_Ibuiltin with (f:=f); eauto with wt.
+ * (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ destruct (invs # pc') as [inv_pc' | ] eqn:INV_pc'; cbn in *.
+ 2: discriminate.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me.
+ rewrite rel_leb_correct in *.
+ eapply rel_ge.
+ eassumption.
+ (* END INVARIANT *)
+
+ apply kill_builtin_res_sound; unfold ctx; eauto with cse3.
+ eapply external_call_sound; unfold ctx; eauto with cse3.
+
+ - (* Icond *)
+ destruct (find_cond_in_fmap (ctx := ctx) params invs pc cond args) as [bfound | ] eqn:FIND_COND.
+ + econstructor; split.
+ * eapply exec_Inop; try eassumption.
+ TR_AT. unfold param_transf_instr. fold invs. fold ctx. rewrite FIND_COND. reflexivity.
+ * replace bfound with b.
+ { econstructor; eauto.
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ rewrite andb_true_iff in IND_step_me.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me as [IND_so [IND_not ZOT]].
+ clear ZOT.
+ rewrite relb_leb_correct in IND_so.
+ rewrite relb_leb_correct in IND_not.
+
+ destruct b.
+ { eapply relb_ge. eassumption. apply apply_cond_sound; auto. }
+ eapply relb_ge. eassumption. apply apply_cond_sound; trivial.
+ rewrite eval_negate_condition.
+ rewrite H0.
+ reflexivity.
+ (* END INVARIANT *)
+ }
+ unfold sem_rel_b in REL.
+ destruct (invs # pc) as [rel | ] eqn:FIND_REL.
+ 2: contradiction.
+ pose proof (is_condition_present_sound pc rel cond args rs m REL) as COND_PRESENT_TRUE.
+ pose proof (is_condition_present_sound pc rel (negate_condition cond) args rs m REL) as COND_PRESENT_FALSE.
+ rewrite eval_negate_condition in COND_PRESENT_FALSE.
+ unfold find_cond_in_fmap in FIND_COND.
+ change (@PMap.get (option RELATION.t)) with (@Regmap.get RB.t) in FIND_COND.
+ rewrite FIND_REL in FIND_COND.
+ destruct (params.(cse3_conditions)).
+ 2: discriminate.
+ destruct (is_condition_present pc rel cond args).
+ { rewrite COND_PRESENT_TRUE in H0 by trivial.
+ congruence.
+ }
+ destruct (is_condition_present pc rel (negate_condition cond) args).
+ { destruct (eval_condition cond rs ## args m) as [b0 | ].
+ 2: discriminate.
+ inv H0.
+ cbn in COND_PRESENT_FALSE.
+ intuition.
+ inv H0.
+ inv FIND_COND.
+ destruct b; trivial; cbn in H2; discriminate.
+ }
+ clear COND_PRESENT_TRUE COND_PRESENT_FALSE.
+ pose proof (is_condition_present_sound pc rel cond (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) rs m REL) as COND_PRESENT_TRUE.
+ pose proof (is_condition_present_sound pc rel (negate_condition cond) (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args) rs m REL) as COND_PRESENT_FALSE.
+ rewrite eval_negate_condition in COND_PRESENT_FALSE.
+
+ destruct is_condition_present.
+ { rewrite subst_args_ok with (sp:=sp) (m:=m) in COND_PRESENT_TRUE.
+ { rewrite COND_PRESENT_TRUE in H0 by trivial.
+ congruence.
+ }
+ unfold fmap_sem.
+ unfold sem_rel_b.
+ fold invs.
+ rewrite FIND_REL.
+ exact REL.
+ }
+ destruct is_condition_present.
+ { rewrite subst_args_ok with (sp:=sp) (m:=m) in COND_PRESENT_FALSE.
+ { destruct (eval_condition cond rs ## args m) as [b0 | ].
+ 2: discriminate.
+ inv H0.
+ cbn in COND_PRESENT_FALSE.
+ intuition.
+ inv H0.
+ inv FIND_COND.
+ destruct b; trivial; cbn in H2; discriminate.
+ }
+ unfold fmap_sem.
+ unfold sem_rel_b.
+ fold invs.
+ rewrite FIND_REL.
+ exact REL.
+ }
+ discriminate.
+ + econstructor; split.
+ * eapply exec_Icond with (args := (subst_args (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc args)); try eassumption.
+ ** TR_AT. unfold param_transf_instr. fold invs. fold ctx.
+ rewrite FIND_COND.
+ reflexivity.
+ ** rewrite subst_args_ok with (sp:=sp) (m:=m) by trivial.
+ eassumption.
+ ** reflexivity.
+ * econstructor; eauto.
+
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ rewrite andb_true_iff in IND_step_me.
+ rewrite andb_true_iff in IND_step_me.
+ destruct IND_step_me as [IND_so [IND_not ZOT]].
+ clear ZOT.
+ rewrite relb_leb_correct in IND_so.
+ rewrite relb_leb_correct in IND_not.
+
+ destruct b.
+ { eapply relb_ge. eassumption. apply apply_cond_sound; auto. }
+ eapply relb_ge. eassumption. apply apply_cond_sound; trivial.
+ rewrite eval_negate_condition.
+ rewrite H0.
+ reflexivity.
+ (* END INVARIANT *)
+
+ - (* Ijumptable *)
+ econstructor. split.
+ + eapply exec_Ijumptable with (arg := (subst_arg (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc arg)); try eassumption.
+ * TR_AT. reflexivity.
+ * rewrite subst_arg_ok with (sp:=sp) (m:=m) by trivial.
+ assumption.
+ + econstructor; eauto.
+
+ (* BEGIN INVARIANT *)
+ fold ctx. fold invs.
+ assert ((check_inductiveness f tenv invs)=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ rewrite PTree_Properties.for_all_correct in IND_step.
+ pose proof (IND_step pc _ H) as IND_step_me.
+ clear IND_entry IND_step.
+ destruct (invs # pc) as [inv_pc | ] eqn:INV_pc; cbn in REL.
+ 2: contradiction.
+ cbn in IND_step_me.
+ rewrite forallb_forall in IND_step_me.
+ assert (RB.ge (invs # pc') (Some inv_pc)) as GE.
+ {
+ apply relb_leb_correct.
+ specialize IND_step_me with (pc', Some inv_pc).
+ apply IND_step_me.
+ apply (in_map (fun pc'0 : node => (pc'0, Some inv_pc))).
+ eapply list_nth_z_in.
+ eassumption.
+ }
+ destruct (invs # pc'); cbn in *.
+ 2: contradiction.
+ eapply rel_ge; eauto.
+ (* END INVARIANT *)
+
+ - (* Ireturn *)
+ destruct or as [arg | ].
+ -- econstructor. split.
+ + eapply exec_Ireturn with (or := Some (subst_arg (ctx:=(context_from_hints (snd (preanalysis tenv f)))) (fst (preanalysis tenv f)) pc arg)).
+ * TR_AT. reflexivity.
+ * rewrite stacksize_preserved with (f:=f); eauto.
+ + simpl.
+ rewrite subst_arg_ok with (sp:=(Vptr stk Ptrofs.zero)) (m:=m) by trivial.
+ econstructor; eauto.
+ apply type_function_correct in WTF.
+ apply wt_instrs with (pc:=pc) (instr:=(Ireturn (Some arg))) in WTF.
+ 2: assumption.
+ inv WTF.
+ rewrite sig_preserved2 with (f:=f) by assumption.
+ rewrite <- H3.
+ unfold wt_regset in WTRS.
+ apply WTRS.
+ -- econstructor. split.
+ + eapply exec_Ireturn; try eassumption.
+ * TR_AT; reflexivity.
+ * rewrite stacksize_preserved with (f:=f); eauto.
+ + econstructor; eauto.
+ simpl. trivial.
+ - (* Callstate internal *)
+ monadInv FUN.
+ rename x into tf.
+ destruct (transf_function_is_typable f tf EQ) as [tenv TENV].
+ econstructor; split.
+ + apply exec_function_internal.
+ rewrite stacksize_preserved with (f:=f); eauto.
+ + rewrite params_preserved with (tf:=tf) (f:=f) by assumption.
+ rewrite entrypoint_preserved with (tf:=tf) (f:=f) by assumption.
+ econstructor; eauto.
+ * apply type_function_correct in TENV.
+ inv TENV.
+ simpl in WTARGS.
+ rewrite sig_preserved2 with (f:=f) in WTARGS by assumption.
+ apply wt_init_regs.
+ rewrite <- wt_params in WTARGS.
+ assumption.
+ * assert ((check_inductiveness f tenv (fst (preanalysis tenv f)))=true) as IND by (eapply transf_function_invariants_inductive; eauto).
+ unfold check_inductiveness in IND.
+ rewrite andb_true_iff in IND.
+ destruct IND as [IND_entry IND_step].
+ clear IND_step.
+ apply RB.beq_correct in IND_entry.
+ unfold RB.eq in *.
+ destruct ((fst (preanalysis tenv f)) # (fn_entrypoint f)).
+ 2: contradiction.
+ cbn.
+ rewrite <- IND_entry.
+ apply sem_rel_top.
+
+ - (* external *)
+ simpl in FUN.
+ inv FUN.
+ econstructor. split.
+ + eapply exec_function_external.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + econstructor; eauto.
+ eapply external_call_well_typed; eauto.
+ - (* return *)
+ inv STACKS.
+ econstructor. split.
+ + eapply exec_return.
+ + econstructor; eauto.
+ apply wt_regset_assign; trivial.
+ rewrite WTRES0.
+ exact WTRES.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inversion H.
+ exploit function_ptr_translated; eauto.
+ intros (tf & A & B).
+ exists (Callstate nil tf nil m0); split.
+ - econstructor; eauto.
+ + eapply (Genv.init_mem_match TRANSF); eauto.
+ + replace (prog_main tprog) with (prog_main prog).
+ rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main; eauto.
+ + rewrite <- H3. eapply sig_preserved; eauto.
+ - constructor; trivial.
+ + constructor. rewrite sig_preserved with (f:=f) by assumption.
+ rewrite H3. reflexivity.
+ + rewrite sig_preserved with (f:=f) by assumption.
+ rewrite H3. reflexivity.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> final_state S1 r -> final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ - apply senv_preserved.
+ - eexact transf_initial_states.
+ - eexact transf_final_states.
+ - intros. eapply step_simulation; eauto.
+Qed.
+
+End PRESERVATION.
+End PARAMS.
+
+Definition match_prog := param_match_prog (cmdline_params tt).
diff --git a/backend/CSEdomain.v b/backend/CSEdomain.v
index e96c4cd4..9641d012 100644
--- a/backend/CSEdomain.v
+++ b/backend/CSEdomain.v
@@ -43,7 +43,7 @@ Definition eq_list_valnum: forall (x y: list valnum), {x=y}+{x<>y} := list_eq_de
Definition eq_rhs (x y: rhs) : {x=y}+{x<>y}.
Proof.
- generalize chunk_eq eq_operation eq_addressing eq_valnum eq_list_valnum.
+ generalize trapping_mode_eq chunk_eq eq_operation eq_addressing eq_valnum eq_list_valnum.
decide equality.
Defined.
@@ -109,7 +109,16 @@ Inductive rhs_eval_to (valu: valuation) (ge: genv) (sp: val) (m: mem):
| load_eval_to: forall chunk addr vl a v,
eval_addressing ge sp addr (map valu vl) = Some a ->
Mem.loadv chunk m a = Some v ->
- rhs_eval_to valu ge sp m (Load chunk addr vl) v.
+ rhs_eval_to valu ge sp m (Load chunk addr vl) v
+(* | load_notrap1_eval_to: forall chunk addr vl,
+ eval_addressing ge sp addr (map valu vl) = None ->
+ rhs_eval_to valu ge sp m (Load NOTRAP chunk addr vl)
+ Vundef
+ | load_notrap2_eval_to: forall chunk addr vl a,
+ eval_addressing ge sp addr (map valu vl) = Some a ->
+ Mem.loadv chunk m a = None ->
+ rhs_eval_to valu ge sp m (Load NOTRAP chunk addr vl)
+ Vundef *).
Inductive equation_holds (valu: valuation) (ge: genv) (sp: val) (m: mem):
equation -> Prop :=
diff --git a/backend/CSEproof.v b/backend/CSEproof.v
index a2a1b461..556b44b3 100644
--- a/backend/CSEproof.v
+++ b/backend/CSEproof.v
@@ -71,7 +71,11 @@ Lemma rhs_eval_to_exten:
Proof.
intros. inv H; simpl in *.
- constructor. rewrite valnums_val_exten by assumption. auto.
-- econstructor; eauto. rewrite valnums_val_exten by assumption. auto.
+- eapply load_eval_to; eauto. rewrite valnums_val_exten by assumption. auto.
+(*
+- apply load_notrap1_eval_to; auto. rewrite valnums_val_exten by assumption. assumption.
+- eapply load_notrap2_eval_to; eauto. rewrite valnums_val_exten by assumption. assumption.
+*)
Qed.
Lemma equation_holds_exten:
@@ -393,6 +397,39 @@ Proof.
+ intros. apply Regmap.gso; auto.
Qed.
+(*
+Lemma add_load_holds_none1:
+ forall valu1 ge sp rs m n addr (args: list reg) chunk dst,
+ numbering_holds valu1 ge sp rs m n ->
+ eval_addressing ge sp addr rs##args = None ->
+ exists valu2, numbering_holds valu2 ge sp (rs#dst <- Vundef) m (add_load n dst chunk addr args).
+Proof.
+ unfold add_load; intros.
+ destruct (valnum_regs n args) as [n1 vl] eqn:VN.
+ exploit valnum_regs_holds; eauto.
+ intros (valu2 & A & B & C & D & E).
+ eapply add_rhs_holds; eauto.
++ rewrite Regmap.gss; auto. eapply load_notrap1_eval_to. rewrite <- B; eauto.
++ intros. apply Regmap.gso; auto.
+Qed.
+
+Lemma add_load_holds_none2:
+ forall valu1 ge sp rs m n addr (args: list reg) a chunk dst,
+ numbering_holds valu1 ge sp rs m n ->
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = None ->
+ exists valu2, numbering_holds valu2 ge sp (rs#dst <- Vundef) m (add_load n dst NOTRAP chunk addr args).
+Proof.
+ unfold add_load; intros.
+ destruct (valnum_regs n args) as [n1 vl] eqn:VN.
+ exploit valnum_regs_holds; eauto.
+ intros (valu2 & A & B & C & D & E).
+ eapply add_rhs_holds; eauto.
++ rewrite Regmap.gss; auto. eapply load_notrap2_eval_to. rewrite <- B; eauto. assumption.
++ intros. apply Regmap.gso; auto.
+Qed.
+ *)
+
Lemma set_unknown_holds:
forall valu ge sp rs m n r v,
numbering_holds valu ge sp rs m n ->
@@ -456,8 +493,8 @@ Lemma kill_all_loads_hold:
Proof.
intros. eapply kill_equations_hold; eauto.
unfold filter_loads; intros. inv H1.
- constructor. rewrite <- H2. apply op_depends_on_memory_correct; auto.
- discriminate.
+ 1: constructor; rewrite <- H2; apply op_depends_on_memory_correct; auto.
+ all: discriminate.
Qed.
Lemma kill_loads_after_store_holds:
@@ -486,6 +523,20 @@ Proof.
apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto.
erewrite <- regs_valnums_sound by eauto. eauto with va.
apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto with va.
+(*
+- eapply load_notrap1_eval_to; assumption.
+- destruct (regs_valnums n vl) as [rl|] eqn:RV; try discriminate.
+ eapply load_notrap2_eval_to; eauto.
+ rewrite <- H9.
+ destruct a; simpl in H1; try discriminate.
+ destruct a0; simpl in H9; try discriminate; simpl; trivial.
+ rewrite negb_false_iff in H6. unfold aaddressing in H6.
+ eapply Mem.load_store_other. eauto.
+ eapply pdisjoint_sound; eauto.
+ apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto.
+ erewrite <- regs_valnums_sound by eauto. eauto with va.
+ apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto with va.
+*)
Qed.
Lemma store_normalized_range_sound:
@@ -562,6 +613,19 @@ Proof.
unfold aaddressing. apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto.
erewrite <- regs_valnums_sound by eauto. eauto with va.
auto.
+(*
+- eapply load_notrap1_eval_to; assumption.
+- destruct (regs_valnums n vl) as [rl|] eqn:RV; try discriminate.
+ eapply load_notrap2_eval_to; eauto. rewrite <- H11.
+ destruct a; simpl in H10; try discriminate; simpl; trivial.
+ rewrite negb_false_iff in H8.
+ eapply Mem.load_storebytes_other. eauto.
+ rewrite H6. rewrite Z2Nat.id by lia.
+ eapply pdisjoint_sound. eauto.
+ unfold aaddressing. apply match_aptr_of_aval. eapply eval_static_addressing_sound; eauto.
+ erewrite <- regs_valnums_sound by eauto. eauto with va.
+ auto.
+*)
Qed.
Lemma load_memcpy:
@@ -1034,6 +1098,10 @@ Proof.
destruct (valnum_regs approx!!pc args) as [n1 vl] eqn:?.
destruct SAT as [valu1 NH1].
exploit valnum_regs_holds; eauto. intros (valu2 & NH2 & EQ & AG & P & Q).
+ destruct trap.
+
+ (* TRAP *)
+ {
destruct (find_rhs n1 (Load chunk addr vl)) as [r|] eqn:?.
+ (* replaced by move *)
exploit find_rhs_sound; eauto. intros (v' & EV & LD).
@@ -1063,7 +1131,102 @@ Proof.
unfold transfer; rewrite H.
eapply add_load_holds; eauto.
apply set_reg_lessdef; auto.
+ }
+
+ (* NOTRAP *)
+ {
+ assert (exists a' : val,
+ eval_addressing ge sp addr rs' ## args = Some a' /\ Val.lessdef a a')
+ as Haa'.
+ apply eval_addressing_lessdef with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ destruct Haa' as [a' [Ha'1 Ha'2]].
+
+ assert (
+ exists v' : val,
+ Mem.loadv chunk m' a' = Some v' /\ Val.lessdef v v') as Hload' by
+ (apply Mem.loadv_extends with (m1 := m) (addr1 := a); assumption).
+ destruct Hload' as [v' [Hv'1 Hv'2]].
+
+ econstructor. split.
+ eapply exec_Iload; eauto.
+ try (rewrite eval_addressing_preserved with (ge1 := ge); auto; exact symbols_preserved).
+
+ econstructor; eauto.
+ eapply analysis_correct_1; eauto. simpl; eauto.
+ unfold transfer. rewrite H.
+ exists valu1.
+ apply set_unknown_holds.
+ assumption.
+ apply set_reg_lessdef; assumption.
+ }
+- (* Iload notrap 1*)
+ destruct (valnum_regs approx!!pc args) as [n1 vl] eqn:?.
+ destruct SAT as [valu1 NH1].
+ exploit valnum_regs_holds; eauto. intros (valu2 & NH2 & EQ & AG & P & Q).
+
+ econstructor. split.
+ eapply exec_Iload_notrap1; eauto.
+ rewrite eval_addressing_preserved with (ge1 := ge).
+ apply eval_addressing_lessdef_none with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ exact symbols_preserved.
+
+ econstructor; eauto.
+ eapply analysis_correct_1; eauto. simpl; eauto.
+ unfold transfer. rewrite H.
+ exists valu1.
+ apply set_unknown_holds.
+ assumption.
+ apply set_reg_lessdef.
+ constructor. assumption.
+
+- (* Iload notrap 2*)
+ destruct (valnum_regs approx!!pc args) as [n1 vl] eqn:?.
+ destruct SAT as [valu1 NH1].
+ exploit valnum_regs_holds; eauto. intros (valu2 & NH2 & EQ & AG & P & Q).
+
+ assert (exists a' : val,
+ eval_addressing ge sp addr rs' ## args = Some a' /\ Val.lessdef a a')
+ as Haa'.
+ apply eval_addressing_lessdef with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ destruct Haa' as [a' [Ha'1 Ha'2]].
+
+ destruct (Mem.loadv chunk m' a') eqn:Hload'.
+
+ {
+ econstructor. split.
+ eapply exec_Iload; eauto.
+ try (rewrite eval_addressing_preserved with (ge1 := ge); auto; exact symbols_preserved).
+
+ econstructor; eauto.
+ eapply analysis_correct_1; eauto. simpl; eauto.
+ unfold transfer. rewrite H.
+ exists valu1.
+ apply set_unknown_holds.
+ assumption.
+ apply set_reg_lessdef; eauto.
+ }
+ {
+ econstructor. split.
+ eapply exec_Iload_notrap2; eauto.
+ try (rewrite eval_addressing_preserved with (ge1 := ge); auto; exact symbols_preserved).
+
+ econstructor; eauto.
+ eapply analysis_correct_1; eauto. simpl; eauto.
+ unfold transfer. rewrite H.
+ exists valu1.
+ apply set_unknown_holds.
+ assumption.
+ apply set_reg_lessdef.
+ constructor. assumption.
+ }
+
- (* Istore *)
destruct (valnum_regs approx!!pc args) as [n1 vl] eqn:?.
destruct SAT as [valu1 NH1].
@@ -1154,6 +1317,7 @@ Proof.
+ apply CASE2; inv H1; auto.
+ apply CASE1.
+ apply CASE2; inv H1; auto.
+ + apply CASE2; inv H1; auto.
* apply set_res_lessdef; auto.
- (* Icond *)
diff --git a/backend/CleanupLabelsproof.v b/backend/CleanupLabelsproof.v
index fb8e57b7..39c3919f 100644
--- a/backend/CleanupLabelsproof.v
+++ b/backend/CleanupLabelsproof.v
@@ -255,6 +255,18 @@ Proof.
left; econstructor; split.
econstructor; eauto.
econstructor; eauto with coqlib.
+(* Lload notrap1 *)
+ assert (eval_addressing tge sp addr (LTL.reglist rs args) = None).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ left; econstructor; split.
+ eapply exec_Lload_notrap1; eauto.
+ econstructor; eauto with coqlib.
+(* Lload notrap2 *)
+ assert (eval_addressing tge sp addr (LTL.reglist rs args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ left; econstructor; split.
+ eapply exec_Lload_notrap2; eauto.
+ econstructor; eauto with coqlib.
(* Lstore *)
assert (eval_addressing tge sp addr (LTL.reglist rs args) = Some a).
rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
diff --git a/backend/Cminor.v b/backend/Cminor.v
index 1618866e..829adca0 100644
--- a/backend/Cminor.v
+++ b/backend/Cminor.v
@@ -78,6 +78,7 @@ Inductive unary_operation : Type :=
| Osingleoflongu: unary_operation. (**r unsigned long to float32 *)
Inductive binary_operation : Type :=
+ | Oexpect: typ -> binary_operation (**r first value, second is expected*)
| Oadd: binary_operation (**r integer addition *)
| Osub: binary_operation (**r integer subtraction *)
| Omul: binary_operation (**r integer multiplication *)
@@ -302,6 +303,7 @@ Definition eval_unop (op: unary_operation) (arg: val) : option val :=
Definition eval_binop
(op: binary_operation) (arg1 arg2: val) (m: mem): option val :=
match op with
+ | Oexpect ty => Some (Val.normalize arg1 ty)
| Oadd => Some (Val.add arg1 arg2)
| Osub => Some (Val.sub arg1 arg2)
| Omul => Some (Val.mul arg1 arg2)
diff --git a/backend/CminorSel.v b/backend/CminorSel.v
index f6f6e34d..cedd2bed 100644
--- a/backend/CminorSel.v
+++ b/backend/CminorSel.v
@@ -50,7 +50,7 @@ with exprlist : Type :=
| Econs: expr -> exprlist -> exprlist
with condexpr : Type :=
- | CEcond : condition -> exprlist -> condexpr
+ | CEcond : condition -> option bool -> exprlist -> condexpr
| CEcondition : condexpr -> condexpr -> condexpr -> condexpr
| CElet: expr -> condexpr -> condexpr.
@@ -207,10 +207,10 @@ with eval_exprlist: letenv -> exprlist -> list val -> Prop :=
eval_exprlist le (Econs a1 al) (v1 :: vl)
with eval_condexpr: letenv -> condexpr -> bool -> Prop :=
- | eval_CEcond: forall le cond al vl vb,
+ | eval_CEcond: forall le cond expected al vl vb,
eval_exprlist le al vl ->
eval_condition cond vl m = Some vb ->
- eval_condexpr le (CEcond cond al) vb
+ eval_condexpr le (CEcond cond expected al) vb
| eval_CEcondition: forall le a b c va v,
eval_condexpr le a va ->
eval_condexpr le (if va then b else c) v ->
@@ -495,7 +495,7 @@ with lift_exprlist (p: nat) (a: exprlist) {struct a}: exprlist :=
with lift_condexpr (p: nat) (a: condexpr) {struct a}: condexpr :=
match a with
- | CEcond c al => CEcond c (lift_exprlist p al)
+ | CEcond c expected al => CEcond c expected (lift_exprlist p al)
| CEcondition a b c => CEcondition (lift_condexpr p a) (lift_condexpr p b) (lift_condexpr p c)
| CElet a b => CElet (lift_expr p a) (lift_condexpr (S p) b)
end.
diff --git a/backend/Cminortyping.v b/backend/Cminortyping.v
index 9f35fe35..d9e99122 100644
--- a/backend/Cminortyping.v
+++ b/backend/Cminortyping.v
@@ -64,6 +64,7 @@ Definition type_binop (op: binary_operation) : typ * typ * typ :=
| Ocmpf _ => (Tfloat, Tfloat, Tint)
| Ocmpfs _ => (Tsingle, Tsingle, Tint)
| Ocmpl _ | Ocmplu _ => (Tlong, Tlong, Tint)
+ | Oexpect ty => (ty, ty, ty)
end.
Module RTLtypes <: TYPE_ALGEBRA.
diff --git a/backend/Constprop.v b/backend/Constprop.v
index 4aab7677..0be9438c 100644
--- a/backend/Constprop.v
+++ b/backend/Constprop.v
@@ -69,7 +69,7 @@ Fixpoint successor_rec (n: nat) (f: function) (ae: AE.t) (pc: node) : node :=
match f.(fn_code)!pc with
| Some (Inop s) =>
successor_rec n' f ae s
- | Some (Icond cond args s1 s2) =>
+ | Some (Icond cond args s1 s2 _) =>
match resolve_branch (eval_static_condition cond (aregs ae args)) with
| Some b => successor_rec n' f ae (if b then s1 else s2)
| None => pc
@@ -181,7 +181,7 @@ Definition transf_instr (f: function) (an: PMap.t VA.t) (rm: romem)
let (op', args') := op_strength_reduction op args aargs in
Iop op' args' res s'
end
- | Iload chunk addr args dst s =>
+ | Iload TRAP chunk addr args dst s =>
let aargs := aregs ae args in
let a := ValueDomain.loadv chunk rm am (eval_static_addressing addr aargs) in
match const_for_result a with
@@ -189,7 +189,7 @@ Definition transf_instr (f: function) (an: PMap.t VA.t) (rm: romem)
Iop cop nil dst s
| None =>
let (addr', args') := addr_strength_reduction addr args aargs in
- Iload chunk addr' args' dst s
+ Iload TRAP chunk addr' args' dst s
end
| Istore chunk addr args src s =>
let aargs := aregs ae args in
@@ -217,14 +217,14 @@ Definition transf_instr (f: function) (an: PMap.t VA.t) (rm: romem)
end
| _, _ => dfl
end
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 i =>
let aargs := aregs ae args in
match resolve_branch (eval_static_condition cond aargs) with
| Some b =>
if b then Inop s1 else Inop s2
| None =>
let (cond', args') := cond_strength_reduction cond args aargs in
- Icond cond' args' s1 s2
+ Icond cond' args' s1 s2 i
end
| Ijumptable arg tbl =>
match areg ae arg with
diff --git a/backend/Constpropproof.v b/backend/Constpropproof.v
index a3592c4d..b59ee8b4 100644
--- a/backend/Constpropproof.v
+++ b/backend/Constpropproof.v
@@ -142,8 +142,8 @@ Inductive match_pc (f: function) (rs: regset) (m: mem): nat -> node -> node -> P
f.(fn_code)!pc = Some (Inop s) ->
match_pc f rs m n s pcx ->
match_pc f rs m (S n) pc pcx
- | match_pc_cond: forall n pc cond args s1 s2 pcx,
- f.(fn_code)!pc = Some (Icond cond args s1 s2) ->
+ | match_pc_cond: forall n pc cond args s1 s2 pcx i,
+ f.(fn_code)!pc = Some (Icond cond args s1 s2 i) ->
(forall b,
eval_condition cond rs##args m = Some b ->
match_pc f rs m n (if b then s1 else s2) pcx) ->
@@ -406,6 +406,8 @@ Proof.
assert (VM1: vmatch bc a aa) by (eapply eval_static_addressing_sound; eauto with va).
set (av := loadv chunk (romem_for cu) am aa).
assert (VM2: vmatch bc v av) by (eapply loadv_sound; eauto).
+ destruct trap.
+ {
destruct (const_for_result av) as [cop|] eqn:?; intros.
+ (* constant-propagated *)
exploit const_for_result_correct; eauto. intros (v' & A & B).
@@ -431,6 +433,59 @@ Proof.
left; econstructor; econstructor; split.
eapply exec_Iload; eauto.
eapply match_states_succ; eauto. apply set_reg_lessdef; auto.
+ }
+ {
+ assert (exists v2 : val,
+ eval_addressing ge (Vptr sp0 Ptrofs.zero) addr (rs' ## args) = Some v2 /\ Val.lessdef a v2) as Hexist2.
+ apply eval_addressing_lessdef with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ destruct Hexist2 as [v2 [Heval2 Hlessdef2]].
+ destruct (Mem.loadv_extends chunk m m' a v2 v MEM H1 Hlessdef2) as [vX [Hvx1 Hvx2]].
+ left; econstructor; econstructor; split.
+ eapply exec_Iload with (a := v2); eauto.
+ try (erewrite eval_addressing_preserved with (ge1:=ge); auto;
+ exact symbols_preserved).
+ eapply match_states_succ; eauto. apply set_reg_lessdef; auto.
+
+ }
+
+- (* Iload notrap1 *)
+ rename pc'0 into pc. TransfInstr.
+ assert (eval_addressing tge (Vptr sp0 Ptrofs.zero) addr (rs' ## args) = None).
+ rewrite eval_addressing_preserved with (ge1 := ge); eauto.
+ apply eval_addressing_lessdef_none with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ exact symbols_preserved.
+
+ left; econstructor; econstructor; split.
+ eapply exec_Iload_notrap1; eauto.
+ eapply match_states_succ; eauto. apply set_reg_lessdef; auto.
+
+- (* Iload notrap2 *)
+ rename pc'0 into pc. TransfInstr.
+ assert (exists v2 : val,
+ eval_addressing ge (Vptr sp0 Ptrofs.zero) addr (rs' ## args) = Some v2 /\ Val.lessdef a v2) as Hexist2.
+ apply eval_addressing_lessdef with (vl1 := rs ## args).
+ apply regs_lessdef_regs; assumption.
+ assumption.
+ destruct Hexist2 as [a' [Heval' Hlessdef']].
+ destruct (Mem.loadv chunk m' a') eqn:Hload'.
+ {
+ left; econstructor; econstructor; split.
+ eapply exec_Iload; eauto.
+
+ try (rewrite eval_addressing_preserved with (ge1 := ge); auto; exact symbols_preserved).
+ eapply match_states_succ; eauto. apply set_reg_lessdef; auto.
+ }
+ {
+ left; econstructor; econstructor; split.
+ eapply exec_Iload_notrap2; eauto.
+
+ try (rewrite eval_addressing_preserved with (ge1 := ge); auto; exact symbols_preserved).
+ eapply match_states_succ; eauto. apply set_reg_lessdef; auto.
+ }
- (* Istore *)
rename pc'0 into pc. TransfInstr.
diff --git a/backend/Deadcode.v b/backend/Deadcode.v
index 2286876e..3412a6fa 100644
--- a/backend/Deadcode.v
+++ b/backend/Deadcode.v
@@ -123,7 +123,7 @@ Definition transfer (f: function) (approx: PMap.t VA.t)
if is_dead nres then after
else if is_int_zero nres then (kill res ne, nm)
else (add_needs args (needs_of_operation op nres) (kill res ne), nm)
- | Some (Iload chunk addr args dst s) =>
+ | Some (Iload trap chunk addr args dst s) =>
let ndst := nreg ne dst in
if is_dead ndst then after
else if is_int_zero ndst then (kill dst ne, nm)
@@ -142,7 +142,7 @@ Definition transfer (f: function) (approx: PMap.t VA.t)
nmem_dead_stack f.(fn_stacksize))
| Some(Ibuiltin ef args res s) =>
transfer_builtin approx!!pc ef args res ne nm
- | Some(Icond cond args s1 s2) =>
+ | Some(Icond cond args s1 s2 _) =>
if peq s1 s2 then after else
(add_needs args (needs_of_condition cond) ne, nm)
| Some(Ijumptable arg tbl) =>
@@ -175,7 +175,7 @@ Definition transf_instr (approx: PMap.t VA.t) (an: PMap.t NA.t)
end
else
instr
- | Iload chunk addr args dst s =>
+ | Iload trap chunk addr args dst s =>
let ndst := nreg (fst an!!pc) dst in
if is_dead ndst then
Inop s
@@ -192,7 +192,7 @@ Definition transf_instr (approx: PMap.t VA.t) (an: PMap.t NA.t)
if nmem_contains (snd an!!pc) (aaddr_arg approx!!pc dst) sz
then instr
else Inop s
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 _ =>
if peq s1 s2 then Inop s1 else instr
| _ =>
instr
diff --git a/backend/Deadcodeproof.v b/backend/Deadcodeproof.v
index 7be12c69..be20af0b 100644
--- a/backend/Deadcodeproof.v
+++ b/backend/Deadcodeproof.v
@@ -829,6 +829,81 @@ Ltac UseTransfer :=
apply eagree_update; eauto 2 with na.
eapply magree_monotone; eauto. intros. apply incl_nmem_add; auto.
+- (* load notrap1 *)
+ TransfInstr; UseTransfer.
+ destruct (is_dead (nreg ne dst)) eqn:DEAD;
+ [idtac|destruct (is_int_zero (nreg ne dst)) eqn:INTZERO];
+ simpl in *.
++ (* dead instruction, turned into a nop *)
+ econstructor; split.
+ eapply exec_Inop; eauto.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update_dead; auto with na.
++ (* instruction with needs = [I Int.zero], turned into a load immediate of zero. *)
+ econstructor; split.
+ eapply exec_Iop with (v := Vint Int.zero); eauto.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update; auto.
+ rewrite is_int_zero_sound by auto.
+ constructor.
++ (* preserved *)
+ exploit eval_addressing_lessdef_none. eapply add_needs_all_lessdef; eauto. eassumption.
+ intro Hnone'.
+ assert (eval_addressing tge (Vptr sp0 Ptrofs.zero) addr te ## args = None) as Hnone2'.
+ erewrite eval_addressing_preserved with (ge1 := ge).
+ assumption.
+ exact symbols_preserved.
+
+ econstructor; split.
+ eapply exec_Iload_notrap1; eauto.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update; eauto 2 with na.
+ eapply magree_monotone; eauto. intros. apply incl_nmem_add; auto.
+
+- (* load notrap2 *)
+ TransfInstr; UseTransfer.
+
+ destruct (is_dead (nreg ne dst)) eqn:DEAD;
+ [idtac|destruct (is_int_zero (nreg ne dst)) eqn:INTZERO];
+ simpl in *.
++ (* dead instruction, turned into a nop *)
+ econstructor; split.
+ eapply exec_Inop; eauto.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update_dead; auto with na.
++ (* instruction with needs = [I Int.zero], turned into a load immediate of zero. *)
+ econstructor; split.
+ eapply exec_Iop with (v := Vint Int.zero); eauto.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update; auto.
+ rewrite is_int_zero_sound by auto.
+ constructor.
++ (* preserved *)
+ exploit eval_addressing_lessdef. eapply add_needs_all_lessdef; eauto. eauto.
+ intros (ta & U & V).
+ destruct (Mem.loadv chunk tm ta) eqn:Hchunk2.
+ {
+ econstructor; split.
+ eapply exec_Iload. eauto.
+ erewrite eval_addressing_preserved with (ge1 := ge).
+ eassumption.
+ exact symbols_preserved.
+ eassumption.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update; eauto 2 with na.
+ eapply magree_monotone; eauto. intros. apply incl_nmem_add; auto.
+ }
+ {
+ econstructor; split.
+ eapply exec_Iload_notrap2. eauto.
+ erewrite eval_addressing_preserved with (ge1 := ge).
+ eassumption.
+ exact symbols_preserved.
+ eassumption.
+ eapply match_succ_states; eauto. simpl; auto.
+ apply eagree_update; eauto 2 with na.
+ eapply magree_monotone; eauto. intros. apply incl_nmem_add; auto.
+ }
- (* store *)
TransfInstr; UseTransfer.
destruct (nmem_contains nm (aaddressing (vanalyze cu f) # pc addr args)
diff --git a/backend/Debugvar.v b/backend/Debugvar.v
index 3204dae5..7806984a 100644
--- a/backend/Debugvar.v
+++ b/backend/Debugvar.v
@@ -233,7 +233,7 @@ Definition transfer (lm: labelmap) (before: option avail) (i: instruction):
(lm, Some (kill (S sl ofs ty) s))
| Lop op args dst =>
(lm, Some (kill (R dst) s))
- | Lload chunk addr args dst =>
+ | Lload trap chunk addr args dst =>
(lm, Some (kill (R dst) s))
| Lstore chunk addr args src =>
(lm, before)
diff --git a/backend/Debugvarproof.v b/backend/Debugvarproof.v
index d31c63ec..95020637 100644
--- a/backend/Debugvarproof.v
+++ b/backend/Debugvarproof.v
@@ -449,6 +449,22 @@ Proof.
eauto. eauto.
apply eval_add_delta_ranges. traceEq.
constructor; auto.
+- (* load notrap1 *)
+ econstructor; split.
+ eapply plus_left.
+ eapply exec_Lload_notrap1.
+ rewrite <- H; apply eval_addressing_preserved; exact symbols_preserved.
+ eauto. eauto.
+ apply eval_add_delta_ranges. traceEq.
+ constructor; auto.
+- (* load notrap2 *)
+ econstructor; split.
+ eapply plus_left.
+ eapply exec_Lload_notrap2.
+ rewrite <- H; apply eval_addressing_preserved; exact symbols_preserved.
+ eauto. eauto.
+ apply eval_add_delta_ranges. traceEq.
+ constructor; auto.
- (* store *)
econstructor; split.
eapply plus_left.
diff --git a/backend/Duplicate.v b/backend/Duplicate.v
new file mode 100644
index 00000000..3fd86728
--- /dev/null
+++ b/backend/Duplicate.v
@@ -0,0 +1,232 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** RTL node duplication using external oracle. Used to form superblock
+ structures *)
+
+Require Import AST RTL Maps Globalenvs.
+Require Import Coqlib Errors Op.
+
+
+
+Module Type DuplicateOracle.
+
+ (** External oracle returning the new RTL code (entry point unchanged),
+ along with the new entrypoint, and a mapping of new nodes to old nodes *)
+ Parameter duplicate_aux: function -> code * node * (PTree.t node).
+
+End DuplicateOracle.
+
+
+
+Module Duplicate (D: DuplicateOracle).
+
+Export D.
+
+Definition duplicate_aux := duplicate_aux.
+
+(* Extract Constant duplicate_aux => "Duplicateaux.duplicate_aux". *)
+
+Local Open Scope error_monad_scope.
+Local Open Scope positive_scope.
+
+(** * Verification of node duplications *)
+
+Definition verify_is_copy dupmap n n' :=
+ match dupmap!n' with
+ | None => Error(msg "verify_is_copy None")
+ | Some revn => match (Pos.compare n revn) with Eq => OK tt | _ => Error(msg "verify_is_copy invalid map") end
+ end.
+
+Fixpoint verify_is_copy_list dupmap ln ln' :=
+ match ln with
+ | n::ln => match ln' with
+ | n'::ln' => do u <- verify_is_copy dupmap n n';
+ verify_is_copy_list dupmap ln ln'
+ | nil => Error (msg "verify_is_copy_list: ln' bigger than ln") end
+ | nil => match ln' with
+ | n :: ln' => Error (msg "verify_is_copy_list: ln bigger than ln'")
+ | nil => OK tt end
+ end.
+
+Definition verify_mapping_entrypoint dupmap (f f': function): res unit :=
+ verify_is_copy dupmap (fn_entrypoint f) (fn_entrypoint f').
+
+Lemma product_eq {A B: Type} :
+ (forall (a b: A), {a=b} + {a<>b}) ->
+ (forall (c d: B), {c=d} + {c<>d}) ->
+ forall (x y: A+B), {x=y} + {x<>y}.
+Proof.
+ intros H H'. intros. decide equality.
+Qed.
+
+(** FIXME Ideally i would like to put this in AST.v but i get an "illegal application"
+ * error when doing so *)
+Remark builtin_arg_eq_pos: forall (a b: builtin_arg positive), {a=b} + {a<>b}.
+Proof.
+ intros.
+ apply (builtin_arg_eq Pos.eq_dec).
+Defined.
+Global Opaque builtin_arg_eq_pos.
+
+Remark builtin_res_eq_pos: forall (a b: builtin_res positive), {a=b} + {a<>b}.
+Proof. intros. apply (builtin_res_eq Pos.eq_dec). Qed.
+Global Opaque builtin_res_eq_pos.
+
+Definition verify_match_inst dupmap inst tinst :=
+ match inst with
+ | Inop n => match tinst with Inop n' => verify_is_copy dupmap n n' | _ => Error(msg "verify_match_inst Inop") end
+
+ | Iop op lr r n => match tinst with
+ Iop op' lr' r' n' =>
+ do u <- verify_is_copy dupmap n n';
+ if (eq_operation op op') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then
+ OK tt
+ else Error (msg "Different r in Iop")
+ else Error (msg "Different lr in Iop")
+ else Error(msg "Different operations in Iop")
+ | _ => Error(msg "verify_match_inst Inop") end
+
+ | Iload tm m a lr r n => match tinst with
+ | Iload tm' m' a' lr' r' n' =>
+ do u <- verify_is_copy dupmap n n';
+ if (trapping_mode_eq tm tm') then
+ if (chunk_eq m m') then
+ if (eq_addressing a a') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then OK tt
+ else Error (msg "Different r in Iload")
+ else Error (msg "Different lr in Iload")
+ else Error (msg "Different addressing in Iload")
+ else Error (msg "Different mchunk in Iload")
+ else Error (msg "Different trapping_mode in Iload")
+ | _ => Error (msg "verify_match_inst Iload") end
+
+ | Istore m a lr r n => match tinst with
+ | Istore m' a' lr' r' n' =>
+ do u <- verify_is_copy dupmap n n';
+ if (chunk_eq m m') then
+ if (eq_addressing a a') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then OK tt
+ else Error (msg "Different r in Istore")
+ else Error (msg "Different lr in Istore")
+ else Error (msg "Different addressing in Istore")
+ else Error (msg "Different mchunk in Istore")
+ | _ => Error (msg "verify_match_inst Istore") end
+
+ | Icall s ri lr r n => match tinst with
+ | Icall s' ri' lr' r' n' =>
+ do u <- verify_is_copy dupmap n n';
+ if (signature_eq s s') then
+ if (product_eq Pos.eq_dec ident_eq ri ri') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then OK tt
+ else Error (msg "Different r r' in Icall")
+ else Error (msg "Different lr in Icall")
+ else Error (msg "Different ri in Icall")
+ else Error (msg "Different signatures in Icall")
+ | _ => Error (msg "verify_match_inst Icall") end
+
+ | Itailcall s ri lr => match tinst with
+ | Itailcall s' ri' lr' =>
+ if (signature_eq s s') then
+ if (product_eq Pos.eq_dec ident_eq ri ri') then
+ if (list_eq_dec Pos.eq_dec lr lr') then OK tt
+ else Error (msg "Different lr in Itailcall")
+ else Error (msg "Different ri in Itailcall")
+ else Error (msg "Different signatures in Itailcall")
+ | _ => Error (msg "verify_match_inst Itailcall") end
+
+ | Ibuiltin ef lbar brr n => match tinst with
+ | Ibuiltin ef' lbar' brr' n' =>
+ do u <- verify_is_copy dupmap n n';
+ if (external_function_eq ef ef') then
+ if (list_eq_dec builtin_arg_eq_pos lbar lbar') then
+ if (builtin_res_eq_pos brr brr') then OK tt
+ else Error (msg "Different brr in Ibuiltin")
+ else Error (msg "Different lbar in Ibuiltin")
+ else Error (msg "Different ef in Ibuiltin")
+ | _ => Error (msg "verify_match_inst Ibuiltin") end
+
+ | Icond cond lr n1 n2 i => match tinst with
+ | Icond cond' lr' n1' n2' i' =>
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (eq_condition cond cond') then
+ do u1 <- verify_is_copy dupmap n1 n1';
+ verify_is_copy dupmap n2 n2'
+ else if (eq_condition (negate_condition cond) cond') then
+ do u1 <- verify_is_copy dupmap n1 n2';
+ verify_is_copy dupmap n2 n1'
+ else Error (msg "Incompatible conditions in Icond")
+ else Error (msg "Different lr in Icond")
+ | _ => Error (msg "verify_match_inst Icond") end
+
+ | Ijumptable r ln => match tinst with
+ | Ijumptable r' ln' =>
+ do u <- verify_is_copy_list dupmap ln ln';
+ if (Pos.eq_dec r r') then OK tt
+ else Error (msg "Different r in Ijumptable")
+ | _ => Error (msg "verify_match_inst Ijumptable") end
+
+ | Ireturn or => match tinst with
+ | Ireturn or' =>
+ if (option_eq Pos.eq_dec or or') then OK tt
+ else Error (msg "Different or in Ireturn")
+ | _ => Error (msg "verify_match_inst Ireturn") end
+ end.
+
+Definition verify_mapping_mn dupmap f f' (m: positive*positive) :=
+ let (tn, n) := m in
+ match (fn_code f)!n with
+ | None => Error (msg "verify_mapping_mn: Could not get an instruction at (fn_code f)!n")
+ | Some inst => match (fn_code f')!tn with
+ | None => Error (msg "verify_mapping_mn: Could not get an instruction at (fn_code xf)!tn")
+ | Some tinst => verify_match_inst dupmap inst tinst
+ end
+ end.
+
+Fixpoint verify_mapping_mn_rec dupmap f f' lm :=
+ match lm with
+ | nil => OK tt
+ | m :: lm => do u <- verify_mapping_mn dupmap f f' m;
+ verify_mapping_mn_rec dupmap f f' lm
+ end.
+
+Definition verify_mapping_match_nodes dupmap (f f': function): res unit :=
+ verify_mapping_mn_rec dupmap f f' (PTree.elements dupmap).
+
+(** Verifies that the [dupmap] of the translated function [f'] is giving correct information in regards to [f] *)
+Definition verify_mapping dupmap (f f': function) : res unit :=
+ do u <- verify_mapping_entrypoint dupmap f f';
+ verify_mapping_match_nodes dupmap f f'.
+
+(** * Entry points *)
+
+Definition transf_function (f: function) : res function :=
+ let (tcte, dupmap) := duplicate_aux f in
+ let (tc, te) := tcte in
+ let f' := mkfunction (fn_sig f) (fn_params f) (fn_stacksize f) tc te in
+ do u <- verify_mapping dupmap f f';
+ OK f'.
+
+Definition transf_fundef (f: fundef) : res fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program transf_fundef p.
+
+End Duplicate.
diff --git a/backend/Duplicateaux.ml b/backend/Duplicateaux.ml
new file mode 100644
index 00000000..22bee067
--- /dev/null
+++ b/backend/Duplicateaux.ml
@@ -0,0 +1,1137 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* Oracle for Duplicate pass.
+ * - Add static prediction information to Icond nodes
+ * - Performs tail duplication on interesting traces to form superblocks
+ * - Unrolls a single iteration of innermost loops
+ * - (TODO: perform partial loop unrolling inside innermost loops)
+ *)
+
+open RTL
+open Maps
+open Camlcoq
+open DebugPrint
+open RTLcommonaux
+
+let stats_oc = ref None
+
+let set_stats_oc () =
+ try
+ let name = Sys.getenv "COMPCERT_PREDICT_STATS" in
+ let oc = open_out_gen [Open_append; Open_creat; Open_text] 0o666 name in
+ stats_oc := Some oc
+ with Not_found -> ()
+
+(* number of total CBs *)
+let stats_nb_total = ref 0
+(* we predicted the same thing as the profiling *)
+let stats_nb_correct_predicts = ref 0
+(* we predicted something (say Some true), but the profiling predicted the opposite (say Some false) *)
+let stats_nb_mispredicts = ref 0
+(* we did not predict anything (None) even though the profiling did predict something *)
+let stats_nb_missed_opportunities = ref 0
+(* we predicted something (say Some true) but the profiling preferred not to predict anything (None) *)
+let stats_nb_overpredict = ref 0
+
+(* heuristic specific counters *)
+let wrong_opcode = ref 0
+let wrong_return = ref 0
+let wrong_loop2 = ref 0
+let wrong_call = ref 0
+
+let right_opcode = ref 0
+let right_return = ref 0
+let right_loop2 = ref 0
+let right_call = ref 0
+
+let reset_stats () = begin
+ stats_nb_total := 0;
+ stats_nb_correct_predicts := 0;
+ stats_nb_mispredicts := 0;
+ stats_nb_missed_opportunities := 0;
+ stats_nb_overpredict := 0;
+ wrong_opcode := 0;
+ wrong_return := 0;
+ wrong_loop2 := 0;
+ wrong_call := 0;
+ right_opcode := 0;
+ right_return := 0;
+ right_loop2 := 0;
+ right_call := 0;
+end
+
+let incr theref = theref := !theref + 1
+
+let has_some o = match o with Some _ -> true | None -> false
+
+let stats_oc_recording () = has_some !stats_oc
+
+let write_stats_oc () =
+ match !stats_oc with
+ | None -> ()
+ | Some oc -> begin
+ Printf.fprintf oc "%d %d %d %d %d %d %d %d %d %d %d %d %d\n" !stats_nb_total
+ !stats_nb_correct_predicts !stats_nb_mispredicts !stats_nb_missed_opportunities
+ !stats_nb_overpredict
+ !wrong_opcode !wrong_return !wrong_loop2 !wrong_call
+ !right_opcode !right_return !right_loop2 !right_call
+ ;
+ close_out oc
+ end
+
+let get_loop_headers = LICMaux.get_loop_headers
+let rtl_successors = LICMaux.rtl_successors
+
+(* Get list of nodes following a BFS of the code *)
+(* Stops when predicate is reached
+ * Excludes any node given in excluded function *)
+let bfs_until code entrypoint (predicate: node->bool) (excluded: node->bool) = begin
+ debug "bfs\n";
+ let visited = ref (PTree.map (fun n i -> false) code)
+ and bfs_list = ref []
+ and to_visit = Queue.create ()
+ and node = ref entrypoint
+ in begin
+ Queue.add entrypoint to_visit;
+ while not (Queue.is_empty to_visit) do
+ node := Queue.pop to_visit;
+ if (not (get_some @@ PTree.get !node !visited)) then begin
+ visited := PTree.set !node true !visited;
+ if not (excluded !node) then begin
+ match PTree.get !node code with
+ | None -> failwith "No such node"
+ | Some i ->
+ bfs_list := !node :: !bfs_list;
+ if not (predicate !node) then
+ let succ = rtl_successors i in List.iter (fun n -> Queue.add n to_visit) succ
+ end
+ end
+ done;
+ List.rev !bfs_list
+ end
+end
+
+let bfs code entrypoint = bfs_until code entrypoint (fun _ -> false) (fun _ -> false)
+
+let optbool o = match o with Some _ -> true | None -> false
+
+let ptree_get_some n ptree = get_some @@ PTree.get n ptree
+
+(* Returns a PTree: node -> list of the predecessors of that node *)
+let get_predecessors_rtl code = begin
+ debug "get_predecessors_rtl\n";
+ let preds = ref (PTree.map (fun n i -> []) code) in
+ let process_inst (node, i) =
+ let succ = rtl_successors i
+ in List.iter (fun s ->
+ let previous_preds = ptree_get_some s !preds in
+ if optbool @@ List.find_opt (fun e -> e == node) previous_preds then ()
+ else preds := PTree.set s (node::previous_preds) !preds) succ
+ in begin
+ List.iter process_inst (PTree.elements code);
+ !preds
+ end
+end
+
+module PInt = struct
+ type t = P.t
+ let compare x y = compare (P.to_int x) (P.to_int y)
+end
+
+module PSet = Set.Make(PInt)
+
+let print_intset s =
+ let seq = PSet.to_seq s
+ in begin
+ if !debug_flag then begin
+ Printf.printf "{";
+ Seq.iter (fun n ->
+ Printf.printf "%d " (P.to_int n)
+ ) seq;
+ Printf.printf "}"
+ end
+ end
+
+(* Looks ahead (until a branch) to see if a node further down verifies
+ * the given predicate *)
+let rec look_ahead_gen (successors: RTL.instruction -> P.t list) code node is_loop_header predicate =
+ if (predicate node) then true
+ else match (successors @@ get_some @@ PTree.get node code) with
+ | [n] -> if (predicate n) then true
+ else (
+ if (get_some @@ PTree.get n is_loop_header) then false
+ else look_ahead_gen successors code n is_loop_header predicate
+ )
+ | _ -> false
+
+let look_ahead = look_ahead_gen rtl_successors
+
+(**
+ * Heuristics mostly based on the paper Branch Prediction for Free
+ *)
+
+let do_call_heuristic code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tCall heuristic..\n";
+ let predicate n = (function
+ | Icall _ -> true
+ | _ -> false) @@ get_some @@ PTree.get n code
+ in let ifso_call = look_ahead code ifso is_loop_header predicate
+ in let ifnot_call = look_ahead code ifnot is_loop_header predicate
+ in if ifso_call && ifnot_call then None
+ else if ifso_call then Some false
+ else if ifnot_call then Some true
+ else None
+ end
+
+let do_opcode_heuristic code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tOpcode heuristic..\n";
+ DuplicateOpcodeHeuristic.opcode_heuristic code cond ifso ifnot is_loop_header
+ end
+
+let do_return_heuristic code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tReturn heuristic..\n";
+ let predicate n = (function
+ | Ireturn _ -> true
+ | _ -> false) @@ get_some @@ PTree.get n code
+ in let ifso_return = look_ahead code ifso is_loop_header predicate
+ in let ifnot_return = look_ahead code ifnot is_loop_header predicate
+ in if ifso_return && ifnot_return then None
+ else if ifso_return then Some false
+ else if ifnot_return then Some true
+ else None
+ end
+
+let do_store_heuristic code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tStore heuristic..\n";
+ let predicate n = (function
+ | Istore _ -> true
+ | _ -> false) @@ get_some @@ PTree.get n code
+ in let ifso_store = look_ahead code ifso is_loop_header predicate
+ in let ifnot_store = look_ahead code ifnot is_loop_header predicate
+ in if ifso_store && ifnot_store then None
+ else if ifso_store then Some false
+ else if ifnot_store then Some true
+ else None
+ end
+
+let do_loop_heuristic code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tLoop heuristic..\n";
+ let predicate n = get_some @@ PTree.get n is_loop_header in
+ let ifso_loop = look_ahead code ifso is_loop_header predicate in
+ let ifnot_loop = look_ahead code ifnot is_loop_header predicate in
+ if ifso_loop && ifnot_loop then (debug "\t\tLOOP but can't choose which\n"; None) (* TODO - take the innermost loop ? *)
+ else if ifso_loop then Some true
+ else if ifnot_loop then Some false
+ else None
+ end
+
+let do_loop2_heuristic loop_info n code cond ifso ifnot is_loop_header =
+ begin
+ debug "\tLoop2 heuristic..\n";
+ match get_some @@ PTree.get n loop_info with
+ | None -> None
+ | Some b -> Some b
+ end
+
+(** Innermost loop detection *)
+
+type innerLoop = {
+ preds: P.t list;
+ body: P.t list;
+ head: P.t; (* head of the loop *)
+ finals: P.t list; (* the final instructions, which loops back to the head *)
+ (* There may be more than one ; for instance if there is an if inside the loop with both
+ * branches leading to a goto backedge
+ * Such cases usually happen after a tail-duplication *)
+ sb_final: P.t option; (* if the innerloop wraps a superblock, this is its final instruction *)
+ (* may be None if we predict that we do not loop *)
+}
+
+let print_pset = LICMaux.pp_pset
+
+let rtl_successors_pref = function
+| Itailcall _ | Ireturn _ -> []
+| Icall(_,_,_,_,n) | Ibuiltin(_,_,_,n) | Inop n | Iop (_,_,_,n)
+| Iload (_,_,_,_,_,n) | Istore (_,_,_,_,n) -> [n]
+| Icond (_,_,n1,n2,p) -> (match p with
+ | Some true -> [n1]
+ | Some false -> [n2]
+ | None -> [n1; n2])
+| Ijumptable (_,ln) -> ln
+
+(* Find the last node of a trace (starting at "node"), until a loop is encountered.
+ * If a non-predicted branch is encountered, returns None *)
+let rec find_last_node_before_loop code node trace is_loop_header =
+ let rtl_succ = rtl_successors @@ get_some @@ PTree.get node code in
+ let headers = List.filter (fun n ->
+ get_some @@ PTree.get n is_loop_header && HashedSet.PSet.contains trace n) rtl_succ in
+ match headers with
+ | [] -> (
+ let next_nodes = rtl_successors_pref @@ get_some @@ PTree.get node code in
+ match next_nodes with
+ | [n] -> (
+ (* To prevent getting out of the superblock and loop infinitely when the prediction is false *)
+ if HashedSet.PSet.contains trace n then
+ find_last_node_before_loop code n trace is_loop_header
+ else None
+ )
+ | _ -> None (* May happen when we predict that a loop is not taken *)
+ )
+ | [h] -> Some node
+ | _ -> failwith "Multiple branches leading to a loop"
+
+(* The computation of sb_final requires to already have branch prediction *)
+let get_inner_loops f code is_loop_header =
+ let fake_f = { fn_sig = f.fn_sig; fn_params = f.fn_params;
+ fn_stacksize = f.fn_stacksize; fn_code = code; fn_entrypoint = f.fn_entrypoint } in
+ let (_, predmap, loopmap) = LICMaux.inner_loops fake_f in
+ begin
+ debug "PREDMAP: "; print_ptree print_intlist predmap;
+ debug "LOOPMAP: "; print_ptree print_pset loopmap;
+ List.map (fun (n, body) ->
+ let preds = List.filter (fun p -> not @@ HashedSet.PSet.contains body p)
+ @@ get_some @@ PTree.get n predmap in
+ let head = (* the instruction from body which is a loop header *)
+ let heads = HashedSet.PSet.elements @@ HashedSet.PSet.filter
+ (fun n -> ptree_get_some n is_loop_header) body in
+ begin
+ assert (List.length heads == 1);
+ List.hd heads
+ end in
+ let finals = (* the predecessors from head that are in the body *)
+ let head_preds = ptree_get_some head predmap in
+ let filtered = List.filter (fun n -> HashedSet.PSet.contains body n) head_preds in
+ begin
+ debug "HEAD: %d\n" (P.to_int head);
+ debug "BODY: %a\n" print_pset body;
+ debug "HEADPREDS: %a\n" print_intlist head_preds;
+ filtered
+ end in
+ let sb_final = find_last_node_before_loop code head body is_loop_header in
+ let body = HashedSet.PSet.elements body in
+ { preds = preds; body = body; head = head; finals = finals;
+ sb_final = sb_final; }
+ )
+ (* LICMaux.inner_loops also returns non-inner loops, but with a body of 1 instruction
+ * We remove those to get just the inner loops *)
+ @@ List.filter (fun (n, body) ->
+ let count = List.length @@ HashedSet.PSet.elements body in count != 1
+ ) (PTree.elements loopmap)
+ end
+
+let get_loop_bodies code entrypoint =
+ let predecessors = get_predecessors_rtl code in
+ (* Algorithm from Muchnik, Compiler Design & Implementation, Figure 7.21 page 192 *)
+ let natural_loop n m =
+ debug "Natural Loop from %d to %d\n" (P.to_int n) (P.to_int m);
+ let in_body = ref (PTree.map (fun n b -> false) code) in
+ let body = ref [] in
+ let add_to_body n = begin
+ in_body := PTree.set n true !in_body;
+ body := n :: !body
+ end
+ in let rec process_node p =
+ debug " Processing node %d\n" (P.to_int p);
+ List.iter (fun pred ->
+ debug " Looking at predecessor of %d: %d\n" (P.to_int p) (P.to_int pred);
+ let is_in_body = get_some @@ PTree.get pred !in_body in
+ if (not @@ is_in_body) then begin
+ debug " --> adding to body\n";
+ add_to_body pred;
+ process_node pred
+ end
+ ) (get_some @@ PTree.get p predecessors)
+ in begin
+ add_to_body m;
+ add_to_body n;
+ (if (m != n) then process_node m);
+ !body
+ end
+ in let option_natural_loop n = function
+ | None -> None
+ | Some m -> Some (natural_loop n m)
+ in PTree.map option_natural_loop (LICMaux.get_loop_backedges code entrypoint)
+
+(* Returns a PTree of either None or Some b where b determines the node in the loop body, for a cb instruction *)
+let get_loop_info f is_loop_header bfs_order code =
+ let loop_info = ref (PTree.map (fun n i -> None) code) in
+ let mark_body body =
+ List.iter (fun n ->
+ match get_some @@ PTree.get n code with
+ | Icond (_, _, ifso, ifnot, _) -> begin
+ match PTree.get n !loop_info with
+ | None -> ()
+ | Some _ ->
+ let b1 = List.mem ifso body in
+ let b2 = List.mem ifnot body in
+ if (b1 && b2) then ()
+ else if (b1 || b2) then begin
+ if b1 then loop_info := PTree.set n (Some true) !loop_info
+ else if b2 then loop_info := PTree.set n (Some false) !loop_info
+ end
+ end
+ | _ -> ()
+ ) body
+ in let bodymap = get_loop_bodies code f.fn_entrypoint in
+ List.iter (fun (_,obody) ->
+ match obody with
+ | None -> ()
+ | Some body -> mark_body body
+ ) (PTree.elements bodymap);
+ !loop_info
+
+(* Remark - compared to the original Branch Prediction for Free paper, we don't use the store heuristic *)
+let get_directions f code entrypoint = begin
+ debug "get_directions\n";
+ let bfs_order = bfs code entrypoint in
+ let is_loop_header = get_loop_headers code entrypoint in
+ let loop_info = get_loop_info f is_loop_header bfs_order code in
+ let directions = ref (PTree.map (fun n i -> None) code) in (* None <=> no predicted direction *)
+ begin
+ (* ptree_printbool is_loop_header; *)
+ (* debug "\n"; *)
+ List.iter (fun n ->
+ match (get_some @@ PTree.get n code) with
+ | Icond (cond, lr, ifso, ifnot, pred) -> begin
+ if stats_oc_recording () || not @@ has_some pred then
+ (* debug "Analyzing %d.." (P.to_int n); *)
+ let heuristics = [ do_opcode_heuristic;
+ do_return_heuristic; do_loop2_heuristic loop_info n; (* do_loop_heuristic; *) do_call_heuristic;
+ (* do_store_heuristic *) ] in
+ let preferred = ref None in
+ let current_heuristic = ref 0 in
+ begin
+ debug "Deciding condition for RTL node %d\n" (P.to_int n);
+ List.iter (fun do_heur ->
+ match !preferred with
+ | None -> begin
+ preferred := do_heur code cond ifso ifnot is_loop_header;
+ if stats_oc_recording () then begin
+ (* Getting stats about mispredictions from each heuristic *)
+ (match !preferred, pred with
+ | Some false, Some true
+ | Some true, Some false
+ (* | Some _, None *) (* Uncomment for overpredicts *)
+ -> begin
+ match !current_heuristic with
+ | 0 -> incr wrong_opcode
+ | 1 -> incr wrong_return
+ | 2 -> incr wrong_loop2
+ | 3 -> incr wrong_call
+ | _ -> failwith "Shouldn't happen"
+ end
+ | Some false, Some false
+ | Some true, Some true -> begin
+ match !current_heuristic with
+ | 0 -> incr right_opcode
+ | 1 -> incr right_return
+ | 2 -> incr right_loop2
+ | 3 -> incr right_call
+ | _ -> failwith "Shouldn't happen"
+ end
+ | _ -> ()
+ );
+ incr current_heuristic
+ end
+ end
+ | Some _ -> ()
+ ) heuristics;
+ directions := PTree.set n !preferred !directions;
+ (match !preferred with | Some false -> debug "\tFALLTHROUGH\n"
+ | Some true -> debug "\tBRANCH\n"
+ | None -> debug "\tUNSURE\n");
+ debug "---------------------------------------\n"
+ end
+ end
+ | _ -> ()
+ ) bfs_order;
+ !directions
+ end
+end
+
+let update_direction direction = function
+| Icond (cond, lr, n, n', pred) -> begin
+ (* Counting stats from profiling *)
+ if stats_oc_recording () then begin
+ incr stats_nb_total;
+ match pred, direction with
+ | None, None -> incr stats_nb_correct_predicts
+ | None, Some _ -> incr stats_nb_overpredict
+ | Some _, None -> incr stats_nb_missed_opportunities
+ | Some false, Some false -> incr stats_nb_correct_predicts
+ | Some false, Some true -> incr stats_nb_mispredicts
+ | Some true, Some false -> incr stats_nb_mispredicts
+ | Some true, Some true -> incr stats_nb_correct_predicts
+ end;
+
+ (* only update if there is no prior existing branch prediction *)
+ (match pred with
+ | None -> Icond (cond, lr, n, n', direction)
+ | Some _ -> begin
+ Icond (cond, lr, n, n', pred)
+ end
+ )
+ end
+| i -> i
+
+(* Uses branch prediction to write prediction annotations in Icond *)
+let update_directions f code entrypoint = begin
+ debug "Update_directions\n";
+ let directions = get_directions f code entrypoint in
+ let code' = ref code in
+ begin
+ debug "Get Directions done, now proceeding to update all direction information..\n";
+ (* debug "Ifso directions: ";
+ ptree_printbool directions;
+ debug "\n"; *)
+ List.iter (fun (n, i) ->
+ let direction = get_some @@ PTree.get n directions in
+ code' := PTree.set n (update_direction direction i) !code'
+ ) (PTree.elements code);
+ !code'
+ end
+end
+
+(** Trace selection *)
+
+let rec exists_false_rec = function
+ | [] -> false
+ | m::lm -> let (_, b) = m in if b then exists_false_rec lm else true
+
+let exists_false boolmap = exists_false_rec (PTree.elements boolmap)
+
+(* DFS using prediction info to guide the exploration *)
+let dfs code entrypoint = begin
+ debug "dfs\n";
+ let visited = ref (PTree.map (fun n i -> false) code) in
+ let rec dfs_list code = function
+ | [] -> []
+ | node :: ln ->
+ if get_some @@ PTree.get node !visited then dfs_list code ln
+ else begin
+ visited := PTree.set node true !visited;
+ let next_nodes = (match get_some @@ PTree.get node code with
+ | Icall(_, _, _, _, n) | Ibuiltin (_, _, _, n) | Iop (_, _, _, n)
+ | Iload (_, _, _, _, _, n) | Istore (_, _, _, _, n) | Inop n -> [n]
+ | Ijumptable (_, ln) -> ln
+ | Itailcall _ | Ireturn _ -> []
+ | Icond (_, _, n1, n2, info) -> (match info with
+ | Some false -> [n2; n1]
+ | _ -> [n1; n2]
+ )
+ ) in node :: dfs_list code (next_nodes @ ln)
+ end
+ in dfs_list code [entrypoint]
+end
+
+let rec select_unvisited_node is_visited = function
+| [] -> failwith "Empty list"
+| n :: ln -> if not (ptree_get_some n is_visited) then n else select_unvisited_node is_visited ln
+
+let best_successor_of node code is_visited =
+ match (PTree.get node code) with
+ | None -> failwith "No such node in the code"
+ | Some i ->
+ let next_node = match i with
+ | Inop n | Iop (_,_,_,n) | Iload (_,_,_,_,_,n) | Istore(_,_,_,_,n)
+ | Icall (_,_,_,_,n) | Ibuiltin (_,_,_,n) -> Some n
+ | Icond (_, _, n1, n2, ob) -> (match ob with None -> None | Some false -> Some n2 | Some true -> Some n1)
+ | _ -> None
+ in match next_node with
+ | None -> None
+ | Some n -> if not (ptree_get_some n is_visited) then Some n else None
+
+(* FIXME - could be improved by selecting in priority the predicted paths *)
+let best_predecessor_of node predecessors code order is_visited =
+ match (PTree.get node predecessors) with
+ | None -> failwith "No predecessor list found"
+ | Some lp ->
+ try Some (List.find (fun n ->
+ if (List.mem n lp) && (not (ptree_get_some n is_visited)) then
+ match ptree_get_some n code with
+ | Icond (_, _, n1, n2, ob) -> (match ob with
+ | None -> false
+ | Some false -> n == n2
+ | Some true -> n == n1
+ )
+ | _ -> true
+ else false
+ ) order)
+ with Not_found -> None
+
+let print_trace = print_intlist
+
+let print_traces oc traces =
+ let rec f oc = function
+ | [] -> ()
+ | t::lt -> Printf.fprintf oc "\n\t%a,\n%a" print_trace t f lt
+ in begin
+ if !debug_flag then
+ Printf.fprintf oc "Traces: {%a}\n" f traces
+ end
+
+(* Dumb (but linear) trace selection *)
+let select_traces_linear code entrypoint =
+ let is_visited = ref (PTree.map (fun n i -> false) code) in
+ let bfs_order = bfs code entrypoint in
+ let rec go_through node = begin
+ is_visited := PTree.set node true !is_visited;
+ let next_node = match (get_some @@ PTree.get node code) with
+ | Icall(_, _, _, _, n) | Ibuiltin (_, _, _, n) | Iop (_, _, _, n)
+ | Iload (_, _, _, _, _, n) | Istore (_, _, _, _, n) | Inop n -> Some n
+ | Ijumptable _ | Itailcall _ | Ireturn _ -> None
+ | Icond (_, _, n1, n2, info) -> (match info with
+ | Some false -> Some n2
+ | Some true -> Some n1
+ | None -> None
+ )
+ in match next_node with
+ | None -> [node]
+ | Some n ->
+ if not (get_some @@ PTree.get n !is_visited) then node :: go_through n
+ else [node]
+ end
+ in let traces = ref [] in begin
+ List.iter (fun n ->
+ if not (get_some @@ PTree.get n !is_visited) then
+ traces := (go_through n) :: !traces
+ ) bfs_order;
+ !traces
+ end
+
+
+(* Algorithm mostly inspired from Chang and Hwu 1988
+ * "Trace Selection for Compiling Large C Application Programs to Microcode" *)
+let select_traces_chang code entrypoint = begin
+ debug "select_traces\n";
+ let order = dfs code entrypoint in
+ let predecessors = get_predecessors_rtl code in
+ let traces = ref [] in
+ let is_visited = ref (PTree.map (fun n i -> false) code) in begin (* mark all nodes visited *)
+ debug "Length: %d\n" (List.length order);
+ while exists_false !is_visited do (* while (there are unvisited nodes) *)
+ let seed = select_unvisited_node !is_visited order in
+ let trace = ref [seed] in
+ let current = ref seed in begin
+ is_visited := PTree.set seed true !is_visited; (* mark seed visited *)
+ let quit_loop = ref false in begin
+ while not !quit_loop do
+ let s = best_successor_of !current code !is_visited in
+ match s with
+ | None -> quit_loop := true (* if (s==0) exit loop *)
+ | Some succ -> begin
+ trace := !trace @ [succ];
+ is_visited := PTree.set succ true !is_visited; (* mark s visited *)
+ current := succ
+ end
+ done;
+ current := seed;
+ quit_loop := false;
+ while not !quit_loop do
+ let s = best_predecessor_of !current predecessors code order !is_visited in
+ match s with
+ | None -> quit_loop := true (* if (s==0) exit loop *)
+ | Some pred -> begin
+ trace := pred :: !trace;
+ is_visited := PTree.set pred true !is_visited; (* mark s visited *)
+ current := pred
+ end
+ done;
+ traces := !trace :: !traces;
+ end
+ end
+ done;
+ (* debug "DFS: \t"; print_intlist order; debug "\n"; *)
+ debug "Traces: %a" print_traces !traces;
+ !traces
+ end
+end
+
+let select_traces code entrypoint =
+ let length = List.length @@ PTree.elements code in
+ if (length < 5000) then select_traces_chang code entrypoint
+ else select_traces_linear code entrypoint
+
+let rec make_identity_ptree_rec = function
+| [] -> PTree.empty
+| m::lm -> let (n, _) = m in PTree.set n n (make_identity_ptree_rec lm)
+
+let make_identity_ptree code = make_identity_ptree_rec (PTree.elements code)
+
+(* Change the pointers of nodes to point to n' instead of n *)
+let rec change_pointers code n n' = function
+ | [] -> code
+ | node :: nodes ->
+ let new_pred_inst = match ptree_get_some node code with
+ | Icall(a, b, c, d, n0) -> assert (n0 = n); Icall(a, b, c, d, n')
+ | Ibuiltin(a, b, c, n0) -> assert (n0 = n); Ibuiltin(a, b, c, n')
+ | Ijumptable(a, ln) -> assert (optbool @@ List.find_opt (fun e -> e = n) ln);
+ Ijumptable(a, List.map (fun e -> if (e = n) then n' else e) ln)
+ | Icond(a, b, n1, n2, i) -> assert (n1 = n || n2 = n);
+ let n1' = if (n1 = n) then n' else n1
+ in let n2' = if (n2 = n) then n' else n2
+ in Icond(a, b, n1', n2', i)
+ | Inop n0 -> assert (n0 = n); Inop n'
+ | Iop (a, b, c, n0) -> assert (n0 = n); Iop (a, b, c, n')
+ | Iload (a, b, c, d, e, n0) -> assert (n0 = n); Iload (a, b, c, d, e, n')
+ | Istore (a, b, c, d, n0) -> assert (n0 = n); Istore (a, b, c, d, n')
+ | Itailcall _ | Ireturn _ -> failwith "That instruction cannot be a predecessor"
+ in let new_code = PTree.set node new_pred_inst code
+ in change_pointers new_code n n' nodes
+
+(* parent: parent of n to keep as parent
+ * preds: all the other parents of n
+ * n': the integer which should contain the duplicate of n
+ * returns: new code, new ptree *)
+let duplicate code ptree parent n preds n' =
+ debug "Duplicating node %d into %d..\n" (P.to_int n) (P.to_int n');
+ match PTree.get n' code with
+ | Some _ -> failwith "The PTree already has a node n'"
+ | None ->
+ let c' = change_pointers code n n' preds
+ in let new_code = PTree.set n' (ptree_get_some n code) c'
+ and new_ptree = PTree.set n' n ptree
+ in (new_code, new_ptree)
+
+let rec maxint = function
+ | [] -> 0
+ | i :: l -> assert (i >= 0); let m = maxint l in if i > m then i else m
+
+let is_empty = function
+ | [] -> true
+ | _ -> false
+
+let next_free_pc code = maxint (List.map (fun e -> let (n, _) = e in P.to_int n) (PTree.elements code)) + 1
+
+let is_a_nop code n =
+ match get_some @@ PTree.get n code with
+ | Inop _ -> true
+ | _ -> false
+
+(* code: RTL code
+ * preds: mapping node -> predecessors
+ * ptree: the revmap
+ * trace: the trace to follow tail duplication on *)
+let tail_duplicate code preds is_loop_header ptree trace =
+ debug "Tail_duplicate on that trace: %a\n" print_trace trace;
+ (* next_int: unused integer that can be used for the next duplication *)
+ let next_int = ref (next_free_pc code)
+ (* last_node and last_duplicate store resp. the last processed node of the trace, and its duplication *)
+ in let last_node = ref None
+ in let last_duplicate = ref None
+ in let nb_duplicated = ref 0
+ (* recursive function on a trace *)
+ in let rec f code ptree is_first = function
+ | [] -> (code, ptree)
+ | n :: t ->
+ let (new_code, new_ptree) =
+ if is_first then (code, ptree) (* first node is never duplicated regardless of its inputs *)
+ else
+ let node_preds = ptree_get_some n preds
+ in let node_preds_nolast =
+ (* We traverse loop headers without initiating tail duplication
+ * (see case of two imbricated loops) *)
+ if (get_some @@ PTree.get n is_loop_header) then []
+ else List.filter (fun e -> e <> get_some !last_node) node_preds
+ (* in let node_preds_nolast = List.filter (fun e -> not @@ List.mem e t) node_preds_nolast *)
+ in let final_node_preds = match !last_duplicate with
+ | None -> node_preds_nolast
+ | Some n' -> n' :: node_preds_nolast
+ in if not (is_empty final_node_preds) then
+ let n' = !next_int
+ in let (newc, newp) = duplicate code ptree !last_node n final_node_preds (P.of_int n')
+ in begin
+ next_int := !next_int + 1;
+ (if not @@ is_a_nop code n then nb_duplicated := !nb_duplicated + 1);
+ last_duplicate := Some (P.of_int n');
+ (newc, newp)
+ end
+ else (code, ptree)
+ in begin
+ last_node := Some n;
+ f new_code new_ptree false t
+ end
+ in let new_code, new_ptree = f code ptree true trace
+ in (new_code, new_ptree, !nb_duplicated)
+
+let superblockify_traces code preds is_loop_header traces ptree =
+ let max_nb_duplicated = !Clflags.option_ftailduplicate (* FIXME - should be architecture dependent *)
+ in let rec f code ptree = function
+ | [] -> (code, ptree, 0)
+ | trace :: traces ->
+ let new_code, new_ptree, nb_duplicated = tail_duplicate code preds is_loop_header ptree trace
+ in if (nb_duplicated < max_nb_duplicated)
+ then (debug "End duplication\n"; f new_code new_ptree traces)
+ else (debug "Too many duplicated nodes, aborting tail duplication\n"; (code, ptree, 0))
+ in let new_code, new_ptree, _ = f code ptree traces
+ in (new_code, new_ptree)
+
+let invert_iconds code =
+ PTree.map1 (fun i -> match i with
+ | Icond (c, lr, ifso, ifnot, info) -> (match info with
+ | Some true -> begin
+ (* debug "Reversing ifso/ifnot for node %d\n" (P.to_int n); *)
+ Icond (Op.negate_condition c, lr, ifnot, ifso, Some false)
+ end
+ | _ -> i)
+ | _ -> i
+ ) code
+
+(** Partial loop unrolling
+ *
+ * The following code seeks innermost loops, and unfolds the first iteration
+ * Most of the code has been moved from LICMaux.ml to Duplicateaux.ml to solve
+ * cyclic dependencies between LICMaux and Duplicateaux
+ *)
+
+let print_inner_loop iloop =
+ debug "{preds: %a, body: %a, head: %d, finals: %a, sb_final: %a}\n"
+ print_intlist iloop.preds
+ print_intlist iloop.body
+ (P.to_int iloop.head)
+ print_intlist iloop.finals
+ print_option_pint iloop.sb_final
+
+let rec print_inner_loops = function
+| [] -> ()
+| iloop :: iloops -> begin
+ print_inner_loop iloop;
+ debug "\n";
+ print_inner_loops iloops
+ end
+
+let cb_exit_node = function
+ | Icond (_,_,n1,n2,p) -> begin match p with
+ | Some true -> Some n2
+ | Some false -> Some n1
+ | None -> None
+ end
+ | _ -> None
+
+ (*
+(* Alternative code to get inner_loops - use it if we suspect the other function to be bugged *)
+let get_natural_loop code predmap n =
+ let is_final_node m =
+ let successors = rtl_successors @@ get_some @@ PTree.get m code in
+ List.exists (fun s -> (P.to_int s) == (P.to_int n)) successors
+ in
+ let excluded_node = cb_exit_node @@ get_some @@ PTree.get n code in
+ let is_excluded m = match excluded_node with
+ | None -> false
+ | Some ex -> P.to_int ex == P.to_int m
+ in
+ debug "get_natural_loop for %d\n" (P.to_int n);
+ let body = bfs_until code n is_final_node is_excluded in
+ debug "BODY: %a\n" print_intlist body;
+ let final = List.find is_final_node body in
+ debug "FINAL: %d\n" (P.to_int final);
+ let preds = List.filter (fun pred -> List.mem pred body) @@ get_some @@ PTree.get n predmap in
+ debug "PREDS: %a\n" print_intlist preds;
+ { preds = preds; body = body; head = n; final = final }
+
+let rec count_loop_headers is_loop_header = function
+ | [] -> 0
+ | n :: ln ->
+ let rem = count_loop_headers is_loop_header ln in
+ if (get_some @@ PTree.get n is_loop_header) then rem + 1 else rem
+
+let get_inner_loops f code is_loop_header =
+ let predmap = get_predecessors_rtl code in
+ let iloops = ref [] in
+ List.iter (fun (n, ilh) -> if ilh then begin
+ let iloop = get_natural_loop code predmap n in
+ let nb_headers = count_loop_headers is_loop_header iloop.body in
+ if nb_headers == 1 then (* innermost loop *)
+ iloops := iloop :: !iloops end
+ ) (PTree.elements is_loop_header);
+ !iloops
+ *)
+
+let rec generate_fwmap ln ln' fwmap =
+ match ln with
+ | [] -> begin
+ match ln' with
+ | [] -> fwmap
+ | _ -> failwith "ln and ln' have different lengths"
+ end
+ | n :: ln -> begin
+ match ln' with
+ | n' :: ln' -> generate_fwmap ln ln' (PTree.set n n' fwmap)
+ | _ -> failwith "ln and ln' have different lengths"
+ end
+
+let generate_revmap ln ln' revmap = generate_fwmap ln' ln revmap
+
+let apply_map fw n = P.of_int @@ ptree_get_some n fw
+
+let apply_map_list fw ln = List.map (apply_map fw) ln
+
+let apply_map_opt fw n =
+ match PTree.get n fw with
+ | Some n' -> P.of_int n'
+ | None -> n
+
+let change_nexts fwmap = function
+ | Icall (a, b, c, d, n) -> Icall (a, b, c, d, apply_map fwmap n)
+ | Ibuiltin (a, b, c, n) -> Ibuiltin (a, b, c, apply_map fwmap n)
+ | Ijumptable (a, ln) -> Ijumptable (a, List.map (apply_map_opt fwmap) ln)
+ | Icond (a, b, n1, n2, i) -> Icond (a, b, apply_map_opt fwmap n1, apply_map_opt fwmap n2, i)
+ | Inop n -> Inop (apply_map fwmap n)
+ | Iop (a, b, c, n) -> Iop (a, b, c, apply_map fwmap n)
+ | Iload (a, b, c, d, e, n) -> Iload (a, b, c, d, e, apply_map fwmap n)
+ | Istore (a, b, c, d, n) -> Istore (a, b, c, d, apply_map fwmap n)
+ | Itailcall (a, b, c) -> Itailcall (a, b, c)
+ | Ireturn o -> Ireturn o
+
+(** Clone a list of instructions into free pc indexes
+ *
+ * The list of instructions should be contiguous, and not include any loop.
+ * It is assumed that the first instruction of the list is the head.
+ * Also, the last instruction of the list should be the loop backedge.
+ *
+ * Returns: (code', revmap', ln', fwmap)
+ * code' is the updated code, after cloning
+ * revmap' is the updated revmap
+ * ln' is the list of the new indexes used to reference the cloned instructions
+ * fwmap is a map from ln to ln'
+ *)
+let clone code revmap ln = begin
+ assert (List.length ln > 0);
+ let head' = next_free_pc code in
+ (* +head' to ensure we never overlap with the existing code *)
+ let ln' = List.map (fun n -> n + head') @@ List.map P.to_int ln in
+ let fwmap = generate_fwmap ln ln' PTree.empty in
+ let revmap' = generate_revmap ln (List.map P.of_int ln') revmap in
+ let code' = ref code in
+ List.iter (fun n ->
+ let instr = get_some @@ PTree.get n code in
+ let instr' = change_nexts fwmap instr in
+ code' := PTree.set (apply_map fwmap n) instr' !code'
+ ) ln;
+ (!code', revmap', ln', fwmap)
+end
+
+let rec count_ignore_nops code = function
+ | [] -> 0
+ | n::ln ->
+ let inst = get_some @@ PTree.get n code in
+ match inst with
+ | Inop _ -> count_ignore_nops code ln
+ | _ -> 1 + count_ignore_nops code ln
+
+(* Unrolls a single interation of the inner loop
+ * 1) Clones the body into body'
+ * 2) Links the preds to the first instruction of body'
+ * 3) Links the last instruction of body' into the first instruction of body
+ *)
+let unroll_inner_loop_single code revmap iloop =
+ let body = iloop.body in
+ if count_ignore_nops code body > !Clflags.option_funrollsingle then begin
+ debug "Too many nodes in the loop body (%d > %d)" (List.length body) !Clflags.option_funrollsingle;
+ (code, revmap)
+ end else
+ let (code2, revmap2, dupbody, fwmap) = clone code revmap body in
+ let code' = ref code2 in
+ let head' = apply_map fwmap (iloop.head) in
+ let finals' = apply_map_list fwmap (iloop.finals) in
+ begin
+ debug "PREDS: %a\n" print_intlist iloop.preds;
+ debug "IHEAD: %d\n" (P.to_int iloop.head);
+ code' := change_pointers !code' (iloop.head) head' (iloop.preds);
+ code' := change_pointers !code' head' (iloop.head) finals';
+ (!code', revmap2)
+ end
+
+let unroll_inner_loops_single f code revmap =
+ let is_loop_header = get_loop_headers code (f.fn_entrypoint) in
+ let inner_loops = get_inner_loops f code is_loop_header in
+ let code' = ref code in
+ let revmap' = ref revmap in
+ begin
+ print_inner_loops inner_loops;
+ List.iter (fun iloop ->
+ let (new_code, new_revmap) = unroll_inner_loop_single !code' !revmap' iloop in
+ code' := new_code; revmap' := new_revmap
+ ) inner_loops;
+ (!code', !revmap')
+ end
+
+let is_some o = match o with Some _ -> true | None -> false
+
+let rec go_through_predicted code start final =
+ if start == final then
+ Some [start]
+ else
+ match rtl_successors_pref @@ get_some @@ PTree.get start code with
+ | [n] -> (
+ match go_through_predicted code n final with
+ | Some ln -> Some (start :: ln)
+ | None -> None
+ )
+ | _ -> None
+
+(* Unrolls the body of the inner loop once - duplicating the exit condition as well
+ * 1) Clones body into body'
+ * 2) Links the last instruction of body (sb_final) into the first of body'
+ * 3) Links the last instruction of body' into the first of body
+ *)
+let unroll_inner_loop_body code revmap iloop =
+ debug "iloop = "; print_inner_loop iloop;
+ let body = iloop.body in
+ let limit = !Clflags.option_funrollbody in
+ if count_ignore_nops code body > limit then begin
+ debug "Too many nodes in the loop body (%d > %d)\n" (List.length body) limit;
+ (code, revmap)
+ end else if not @@ is_some iloop.sb_final then begin
+ debug "The loop body does not form a superblock OR we have predicted that we do not loop\n";
+ (code, revmap)
+ end else
+ let sb_final = get_some @@ iloop.sb_final in
+ let sb_body = get_some @@ go_through_predicted code iloop.head sb_final in
+ let (code2, revmap2, dupbody, fwmap) = clone code revmap sb_body in
+ let code' = ref code2 in
+ let head' = apply_map fwmap (iloop.head) in
+ let sb_final' = apply_map fwmap sb_final in
+ begin
+ code' := change_pointers !code' iloop.head head' [sb_final];
+ code' := change_pointers !code' head' iloop.head [sb_final'];
+ (!code', revmap2)
+ end
+
+let unroll_inner_loops_body f code revmap =
+ let is_loop_header = get_loop_headers code (f.fn_entrypoint) in
+ let inner_loops = get_inner_loops f code is_loop_header in
+ debug "Number of loops found: %d\n" (List.length inner_loops);
+ let code' = ref code in
+ let revmap' = ref revmap in
+ begin
+ print_inner_loops inner_loops;
+ List.iter (fun iloop ->
+ let (new_code, new_revmap) = unroll_inner_loop_body !code' !revmap' iloop in
+ code' := new_code; revmap' := new_revmap
+ ) inner_loops;
+ (!code', !revmap')
+ end
+
+let extract_upto_icond f code head =
+ let rec extract h =
+ let inst = get_some @@ PTree.get h code in
+ match inst with
+ | Icond _ -> [h]
+ | _ -> ( match rtl_successors inst with
+ | [n] -> h :: (extract n)
+ | _ -> failwith "Found a node with more than one successor??"
+ )
+ in List.rev @@ extract head
+
+let rotate_inner_loop f code revmap iloop =
+ let header = extract_upto_icond f code iloop.head in
+ let limit = !Clflags.option_flooprotate in
+ let nb_duplicated = count_ignore_nops code header in
+ if nb_duplicated > limit then begin
+ debug "Loop Rotate: too many nodes to duplicate (%d > %d)" (List.length header) limit;
+ (code, revmap)
+ end else if nb_duplicated == count_ignore_nops code iloop.body then begin
+ debug "The conditional branch is already at the end! No need to rotate.";
+ (code, revmap)
+ end else
+ let (code2, revmap2, dupheader, fwmap) = clone code revmap header in
+ let code' = ref code2 in
+ let head' = apply_map fwmap iloop.head in
+ begin
+ code' := change_pointers !code' iloop.head head' iloop.preds;
+ (!code', revmap2)
+ end
+
+let rotate_inner_loops f code revmap =
+ let is_loop_header = get_loop_headers code (f.fn_entrypoint) in
+ let inner_loops = get_inner_loops f code is_loop_header in
+ let code' = ref code in
+ let revmap' = ref revmap in
+ begin
+ print_inner_loops inner_loops;
+ List.iter (fun iloop ->
+ let (new_code, new_revmap) = rotate_inner_loop f !code' !revmap' iloop in
+ code' := new_code; revmap' := new_revmap
+ ) inner_loops;
+ (!code', !revmap')
+ end
+
+let loop_rotate f =
+ let entrypoint = f.fn_entrypoint in
+ let code = f.fn_code in
+ let revmap = make_identity_ptree code in
+ let (code, revmap) =
+ if !Clflags.option_flooprotate > 0 then
+ rotate_inner_loops f code revmap
+ else (code, revmap) in
+ ((code, entrypoint), revmap)
+
+let static_predict f =
+ let entrypoint = f.fn_entrypoint in
+ let code = f.fn_code in
+ let revmap = make_identity_ptree code in
+ begin
+ reset_stats ();
+ set_stats_oc ();
+ let code =
+ if !Clflags.option_fpredict then
+ update_directions f code entrypoint
+ else code in
+ write_stats_oc ();
+ let code =
+ if !Clflags.option_fpredict then
+ invert_iconds code
+ else code in
+ ((code, entrypoint), revmap)
+ end
+
+let unroll_single f =
+ let entrypoint = f.fn_entrypoint in
+ let code = f.fn_code in
+ let revmap = make_identity_ptree code in
+ let (code, revmap) =
+ if !Clflags.option_funrollsingle > 0 then
+ unroll_inner_loops_single f code revmap
+ else (code, revmap) in
+ ((code, entrypoint), revmap)
+
+let unroll_body f =
+ let entrypoint = f.fn_entrypoint in
+ let code = f.fn_code in
+ let revmap = make_identity_ptree code in
+ let (code, revmap) =
+ if !Clflags.option_funrollbody > 0 then
+ unroll_inner_loops_body f code revmap
+ else (code, revmap) in
+ ((code, entrypoint), revmap)
+
+let tail_duplicate f =
+ let entrypoint = f.fn_entrypoint in
+ let code = f.fn_code in
+ let revmap = make_identity_ptree code in
+ let (code, revmap) =
+ if !Clflags.option_ftailduplicate > 0 then
+ let traces = select_traces code entrypoint in
+ let preds = get_predecessors_rtl code in
+ let is_loop_header = get_loop_headers code entrypoint in
+ superblockify_traces code preds is_loop_header traces revmap
+ else (code, revmap) in
+ ((code, entrypoint), revmap)
diff --git a/backend/Duplicatepasses.v b/backend/Duplicatepasses.v
new file mode 100644
index 00000000..7e58eedf
--- /dev/null
+++ b/backend/Duplicatepasses.v
@@ -0,0 +1,58 @@
+Require Import RTL.
+Require Import Maps.
+Require Import Duplicate.
+Require Import Duplicateproof.
+
+(** Static Prediction *)
+
+Module StaticPredictOracle <: DuplicateOracle.
+ Axiom duplicate_aux : function -> code * node * (PTree.t node).
+ Extract Constant duplicate_aux => "Duplicateaux.static_predict".
+End StaticPredictOracle.
+
+Module Staticpredictproof := DuplicateProof StaticPredictOracle.
+
+Module Staticpredict := Staticpredictproof.
+
+(** Unrolling one iteration out of the body *)
+
+Module UnrollSingleOracle <: DuplicateOracle.
+ Axiom duplicate_aux : function -> code * node * (PTree.t node).
+ Extract Constant duplicate_aux => "Duplicateaux.unroll_single".
+End UnrollSingleOracle.
+
+Module Unrollsingleproof := DuplicateProof UnrollSingleOracle.
+
+Module Unrollsingle := Unrollsingleproof.
+
+(** Unrolling the body of innermost loops *)
+
+Module UnrollBodyOracle <: DuplicateOracle.
+ Axiom duplicate_aux : function -> code * node * (PTree.t node).
+ Extract Constant duplicate_aux => "Duplicateaux.unroll_body".
+End UnrollBodyOracle.
+
+Module Unrollbodyproof := DuplicateProof UnrollBodyOracle.
+
+Module Unrollbody := Unrollbodyproof.
+
+(** Tail Duplication *)
+
+Module TailDuplicateOracle <: DuplicateOracle.
+ Axiom duplicate_aux : function -> code * node * (PTree.t node).
+ Extract Constant duplicate_aux => "Duplicateaux.tail_duplicate".
+End TailDuplicateOracle.
+
+Module Tailduplicateproof := DuplicateProof TailDuplicateOracle.
+
+Module Tailduplicate := Tailduplicateproof.
+
+(** Loop Rotate *)
+
+Module LoopRotateOracle <: DuplicateOracle.
+ Axiom duplicate_aux : function -> code * node * (PTree.t node).
+ Extract Constant duplicate_aux => "Duplicateaux.loop_rotate".
+End LoopRotateOracle.
+
+Module Looprotateproof := DuplicateProof LoopRotateOracle.
+Module Looprotate := Looprotateproof.
diff --git a/backend/Duplicateproof.v b/backend/Duplicateproof.v
new file mode 100644
index 00000000..2f3bad2f
--- /dev/null
+++ b/backend/Duplicateproof.v
@@ -0,0 +1,542 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Correctness proof for code duplication *)
+Require Import AST Linking Errors Globalenvs Smallstep.
+Require Import Coqlib Maps Events Values.
+Require Import Op RTL Duplicate.
+
+Module DuplicateProof (D: DuplicateOracle).
+Include Duplicate D.
+
+Local Open Scope positive_scope.
+
+(** * Definition of [match_states] (independently of the translation) *)
+
+(* est-ce plus simple de prendre dupmap: node -> node, avec un noeud hors CFG à la place de None ? *)
+Inductive match_inst (dupmap: PTree.t node): instruction -> instruction -> Prop :=
+ | match_inst_nop: forall n n',
+ dupmap!n' = (Some n) -> match_inst dupmap (Inop n) (Inop n')
+ | match_inst_op: forall n n' op lr r,
+ dupmap!n' = (Some n) -> match_inst dupmap (Iop op lr r n) (Iop op lr r n')
+ | match_inst_load: forall n n' tm m a lr r,
+ dupmap!n' = (Some n) -> match_inst dupmap (Iload tm m a lr r n) (Iload tm m a lr r n')
+ | match_inst_store: forall n n' m a lr r,
+ dupmap!n' = (Some n) -> match_inst dupmap (Istore m a lr r n) (Istore m a lr r n')
+ | match_inst_call: forall n n' s ri lr r,
+ dupmap!n' = (Some n) -> match_inst dupmap (Icall s ri lr r n) (Icall s ri lr r n')
+ | match_inst_tailcall: forall s ri lr,
+ match_inst dupmap (Itailcall s ri lr) (Itailcall s ri lr)
+ | match_inst_builtin: forall n n' ef la br,
+ dupmap!n' = (Some n) -> match_inst dupmap (Ibuiltin ef la br n) (Ibuiltin ef la br n')
+ | match_inst_cond: forall ifso ifso' ifnot ifnot' c lr i i',
+ dupmap!ifso' = (Some ifso) -> dupmap!ifnot' = (Some ifnot) ->
+ match_inst dupmap (Icond c lr ifso ifnot i) (Icond c lr ifso' ifnot' i')
+ | match_inst_revcond: forall ifso ifso' ifnot ifnot' c lr i i',
+ dupmap!ifso' = (Some ifso) -> dupmap!ifnot' = (Some ifnot) ->
+ match_inst dupmap (Icond c lr ifso ifnot i) (Icond (negate_condition c) lr ifnot' ifso' i')
+ | match_inst_jumptable: forall ln ln' r,
+ list_forall2 (fun n n' => (dupmap!n' = (Some n))) ln ln' ->
+ match_inst dupmap (Ijumptable r ln) (Ijumptable r ln')
+ | match_inst_return: forall or, match_inst dupmap (Ireturn or) (Ireturn or).
+
+Record match_function dupmap f f': Prop := {
+ dupmap_correct: forall n n', dupmap!n' = Some n ->
+ (forall i, (fn_code f)!n = Some i -> exists i', (fn_code f')!n' = Some i' /\ match_inst dupmap i i');
+ dupmap_entrypoint: dupmap!(fn_entrypoint f') = Some (fn_entrypoint f);
+ preserv_fnsig: fn_sig f = fn_sig f';
+ preserv_fnparams: fn_params f = fn_params f';
+ preserv_fnstacksize: fn_stacksize f = fn_stacksize f'
+}.
+
+Inductive match_fundef: RTL.fundef -> RTL.fundef -> Prop :=
+ | match_Internal dupmap f f': match_function dupmap f f' -> match_fundef (Internal f) (Internal f')
+ | match_External ef: match_fundef (External ef) (External ef).
+
+Inductive match_stackframes: stackframe -> stackframe -> Prop :=
+ | match_stackframe_intro
+ dupmap res f sp pc rs f' pc'
+ (TRANSF: match_function dupmap f f')
+ (DUPLIC: dupmap!pc' = Some pc):
+ match_stackframes (Stackframe res f sp pc rs) (Stackframe res f' sp pc' rs).
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro
+ dupmap st f sp pc rs m st' f' pc'
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_function dupmap f f')
+ (DUPLIC: dupmap!pc' = Some pc):
+ match_states (State st f sp pc rs m) (State st' f' sp pc' rs m)
+ | match_states_call:
+ forall st st' f f' args m
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_fundef f f'),
+ match_states (Callstate st f args m) (Callstate st' f' args m)
+ | match_states_return:
+ forall st st' v m
+ (STACKS: list_forall2 match_stackframes st st'),
+ match_states (Returnstate st v m) (Returnstate st' v m).
+
+(** * Auxiliary properties *)
+
+
+Theorem transf_function_preserves:
+ forall f f',
+ transf_function f = OK f' ->
+ fn_sig f = fn_sig f' /\ fn_params f = fn_params f' /\ fn_stacksize f = fn_stacksize f'.
+Proof.
+ intros. unfold transf_function in H. destruct (duplicate_aux _) as (tcte & mp). destruct tcte as (tc & te). monadInv H.
+ repeat (split; try reflexivity).
+Qed.
+
+
+Lemma verify_mapping_mn_rec_step:
+ forall dupmap lb b f f',
+ In b lb ->
+ verify_mapping_mn_rec dupmap f f' lb = OK tt ->
+ verify_mapping_mn dupmap f f' b = OK tt.
+Proof.
+ induction lb; intros.
+ - monadInv H0. inversion H.
+ - inversion H.
+ + subst. monadInv H0. destruct x. assumption.
+ + monadInv H0. destruct x. eapply IHlb; assumption.
+Qed.
+
+Lemma verify_is_copy_correct:
+ forall dupmap n n',
+ verify_is_copy dupmap n n' = OK tt ->
+ dupmap ! n' = Some n.
+Proof.
+ intros. unfold verify_is_copy in H. destruct (_ ! n') eqn:REVM; [|inversion H].
+ destruct (n ?= p) eqn:NP; try (inversion H; fail).
+ eapply Pos.compare_eq in NP. subst.
+ reflexivity.
+Qed.
+
+Lemma verify_is_copy_list_correct:
+ forall dupmap ln ln',
+ verify_is_copy_list dupmap ln ln' = OK tt ->
+ list_forall2 (fun n n' => dupmap ! n' = Some n) ln ln'.
+Proof.
+ induction ln.
+ - intros. destruct ln'; monadInv H. constructor.
+ - intros. destruct ln'; monadInv H. destruct x. apply verify_is_copy_correct in EQ.
+ eapply IHln in EQ0. constructor; assumption.
+Qed.
+
+Lemma verify_match_inst_correct:
+ forall dupmap i i',
+ verify_match_inst dupmap i i' = OK tt ->
+ match_inst dupmap i i'.
+Proof.
+ intros. unfold verify_match_inst in H.
+ destruct i; try (inversion H; fail).
+(* Inop *)
+ - destruct i'; try (inversion H; fail).
+ eapply verify_is_copy_correct in H.
+ constructor; eauto.
+(* Iop *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_correct in EQ.
+ destruct (eq_operation _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. clear EQ0. subst.
+ constructor. assumption.
+(* Iload *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_correct in EQ.
+ destruct (trapping_mode_eq _ _); try discriminate.
+ destruct (chunk_eq _ _); try discriminate.
+ destruct (eq_addressing _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. clear EQ0. subst.
+ constructor. assumption.
+(* Istore *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_correct in EQ.
+ destruct (chunk_eq _ _); try discriminate.
+ destruct (eq_addressing _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. clear EQ0. subst.
+ constructor. assumption.
+(* Icall *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_correct in EQ.
+ destruct (signature_eq _ _); try discriminate.
+ destruct (product_eq _ _ _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. subst.
+ constructor. assumption.
+(* Itailcall *)
+ - destruct i'; try (inversion H; fail).
+ destruct (signature_eq _ _); try discriminate.
+ destruct (product_eq _ _ _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate. subst. clear H.
+ constructor.
+(* Ibuiltin *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_correct in EQ.
+ destruct (external_function_eq _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (builtin_res_eq_pos _ _); try discriminate. subst.
+ constructor. assumption.
+(* Icond *)
+ - destruct i'; try (inversion H; fail).
+ destruct (list_eq_dec _ _ _); try discriminate. subst.
+ destruct (eq_condition _ _); try discriminate.
+ + monadInv H. destruct x. eapply verify_is_copy_correct in EQ.
+ eapply verify_is_copy_correct in EQ0.
+ subst; constructor; assumption.
+ + destruct (eq_condition _ _); try discriminate.
+ monadInv H. destruct x. eapply verify_is_copy_correct in EQ.
+ eapply verify_is_copy_correct in EQ0.
+ subst; constructor; assumption.
+(* Ijumptable *)
+ - destruct i'; try (inversion H; fail). monadInv H.
+ destruct x. eapply verify_is_copy_list_correct in EQ.
+ destruct (Pos.eq_dec _ _); try discriminate. subst.
+ constructor. assumption.
+(* Ireturn *)
+ - destruct i'; try (inversion H; fail).
+ destruct (option_eq _ _ _); try discriminate. subst. clear H.
+ constructor.
+Qed.
+
+
+Lemma verify_mapping_mn_correct mp n n' i f f' tc:
+ mp ! n' = Some n ->
+ (fn_code f) ! n = Some i ->
+ (fn_code f') = tc ->
+ verify_mapping_mn mp f f' (n', n) = OK tt ->
+ exists i',
+ tc ! n' = Some i'
+ /\ match_inst mp i i'.
+Proof.
+ unfold verify_mapping_mn; intros H H0 H1 H2. rewrite H0 in H2. clear H0. rewrite H1 in H2. clear H1.
+ destruct (tc ! n') eqn:TCN; [| inversion H2].
+ exists i0. split; auto.
+ eapply verify_match_inst_correct. assumption.
+Qed.
+
+
+Lemma verify_mapping_mn_rec_correct:
+ forall mp n n' i f f' tc,
+ mp ! n' = Some n ->
+ (fn_code f) ! n = Some i ->
+ (fn_code f') = tc ->
+ verify_mapping_mn_rec mp f f' (PTree.elements mp) = OK tt ->
+ exists i',
+ tc ! n' = Some i'
+ /\ match_inst mp i i'.
+Proof.
+ intros. exploit PTree.elements_correct. eapply H. intros IN.
+ eapply verify_mapping_mn_rec_step in H2; eauto.
+ eapply verify_mapping_mn_correct; eauto.
+Qed.
+
+Theorem transf_function_correct f f':
+ transf_function f = OK f' -> exists dupmap, match_function dupmap f f'.
+Proof.
+ unfold transf_function.
+ intros TRANSF.
+ destruct (duplicate_aux _) as (tcte & mp). destruct tcte as (tc & te).
+ monadInv TRANSF.
+ unfold verify_mapping in EQ. monadInv EQ.
+ exists mp; constructor 1; simpl; auto.
+ + (* correct *)
+ intros until n'. intros REVM i FNC.
+ unfold verify_mapping_match_nodes in EQ1. simpl in EQ1. destruct x.
+ eapply verify_mapping_mn_rec_correct; eauto.
+ simpl; eauto.
+ + (* entrypoint *)
+ intros. unfold verify_mapping_entrypoint in EQ0. simpl in EQ0.
+ eapply verify_is_copy_correct; eauto.
+ destruct x0; auto.
+Qed.
+
+Lemma transf_fundef_correct f f':
+ transf_fundef f = OK f' -> match_fundef f f'.
+Proof.
+ intros TRANSF; destruct f; simpl; monadInv TRANSF.
+ + exploit transf_function_correct; eauto.
+ intros (dupmap & MATCH_F).
+ eapply match_Internal; eauto.
+ + eapply match_External.
+Qed.
+
+(** * Preservation proof *)
+
+Definition match_prog (p tp: program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variable prog: program.
+Variable tprog: program.
+
+Hypothesis TRANSL: match_prog prog tprog.
+
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma symbols_preserved s: Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof.
+ rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
+Qed.
+
+(* UNUSED LEMMA ?
+Lemma senv_transitivity x y z: Senv.equiv x y -> Senv.equiv y z -> Senv.equiv x z.
+Proof.
+ unfold Senv.equiv. intuition congruence.
+Qed.
+*)
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof.
+ eapply (Genv.senv_match TRANSL).
+Qed.
+
+Lemma functions_translated:
+ forall (v: val) (f: fundef),
+ Genv.find_funct ge v = Some f ->
+ exists tf cunit, transf_fundef f = OK tf /\ Genv.find_funct tge v = Some tf /\ linkorder cunit prog.
+Proof.
+ intros. exploit (Genv.find_funct_match TRANSL); eauto.
+ intros (cu & tf & A & B & C).
+ repeat eexists; intuition eauto.
+ + unfold incl; auto.
+ + eapply linkorder_refl.
+Qed.
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ transf_fundef f = OK tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
+Qed.
+
+Lemma function_sig_translated:
+ forall f tf, transf_fundef f = OK tf -> funsig tf = funsig f.
+Proof.
+ intros. destruct f.
+ - simpl in H. monadInv H. simpl. symmetry. apply transf_function_preserves. assumption.
+ - simpl in H. monadInv H. reflexivity.
+Qed.
+
+Lemma list_nth_z_dupmap:
+ forall dupmap ln ln' (pc pc': node) val,
+ list_nth_z ln val = Some pc ->
+ list_forall2 (fun n n' => dupmap!n' = Some n) ln ln' ->
+ exists pc',
+ list_nth_z ln' val = Some pc'
+ /\ dupmap!pc' = Some pc.
+Proof.
+ induction ln; intros until val; intros LNZ LFA.
+ - inv LNZ.
+ - inv LNZ. destruct (zeq val 0) eqn:ZEQ.
+ + inv H0. destruct ln'; inv LFA.
+ simpl. exists p. split; auto.
+ + inv LFA. simpl. rewrite ZEQ. exploit IHln. 2: eapply H0. all: eauto.
+ intros (pc'1 & LNZ & REV). exists pc'1. split; auto. congruence.
+Qed.
+
+Theorem transf_initial_states:
+ forall s1, initial_state prog s1 ->
+ exists s2, initial_state tprog s2 /\ match_states s1 s2.
+Proof.
+ intros. inv H.
+ exploit function_ptr_translated; eauto. intros (tf & FIND & TRANSF).
+ eexists. split.
+ - econstructor; eauto.
+ + eapply (Genv.init_mem_transf_partial TRANSL); eauto.
+ + replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main. eauto.
+ + destruct f.
+ * monadInv TRANSF. rewrite <- H3. symmetry; eapply transf_function_preserves. assumption.
+ * monadInv TRANSF. assumption.
+ - constructor; eauto.
+ + constructor.
+ + apply transf_fundef_correct; auto.
+Qed.
+
+Theorem transf_final_states:
+ forall s1 s2 r,
+ match_states s1 s2 -> final_state s1 r -> final_state s2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem step_simulation:
+ forall s1 t s1', step ge s1 t s1' ->
+ forall s2 (MS: match_states s1 s2),
+ exists s2',
+ step tge s2 t s2'
+ /\ match_states s1' s2'.
+Proof.
+ Local Hint Resolve transf_fundef_correct: core.
+ induction 1; intros; inv MS.
+(* Inop *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3).
+ inv H3.
+ eexists. split.
+ + eapply exec_Inop; eauto.
+ + econstructor; eauto.
+(* Iop *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Iop; eauto. erewrite eval_operation_preserved; eauto.
+ + econstructor; eauto.
+(* Iload *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Iload; eauto; (* is the follow still needed?*) erewrite eval_addressing_preserved; eauto.
+ + econstructor; eauto.
+(* Iload notrap1 *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Iload_notrap1; eauto; erewrite eval_addressing_preserved; eauto.
+ + econstructor; eauto.
+(* Iload notrap2 *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Iload_notrap2; eauto; erewrite eval_addressing_preserved; eauto.
+ + econstructor; eauto.
+
+(* Istore *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Istore; eauto; erewrite eval_addressing_preserved; eauto.
+ + econstructor; eauto.
+(* Icall *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ destruct ros.
+ * simpl in H0. apply functions_translated in H0.
+ destruct H0 as (tf & cunit & TFUN & GFIND & LO).
+ eexists. split.
+ + eapply exec_Icall. eassumption. simpl. eassumption.
+ apply function_sig_translated. assumption.
+ + repeat (econstructor; eauto).
+ * simpl in H0. destruct (Genv.find_symbol _ _) eqn:GFS; try discriminate.
+ apply function_ptr_translated in H0. destruct H0 as (tf & GFF & TF).
+ eexists. split.
+ + eapply exec_Icall. eassumption. simpl. rewrite symbols_preserved. rewrite GFS.
+ eassumption. apply function_sig_translated. assumption.
+ + repeat (econstructor; eauto).
+(* Itailcall *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H10 & H11). inv H11.
+ pose symbols_preserved as SYMPRES.
+ destruct ros.
+ * simpl in H0. apply functions_translated in H0.
+ destruct H0 as (tf & cunit & TFUN & GFIND & LO).
+ eexists. split.
+ + eapply exec_Itailcall. eassumption. simpl. eassumption.
+ apply function_sig_translated. assumption.
+ erewrite <- preserv_fnstacksize; eauto.
+ + repeat (constructor; auto).
+ * simpl in H0. destruct (Genv.find_symbol _ _) eqn:GFS; try discriminate.
+ apply function_ptr_translated in H0. destruct H0 as (tf & GFF & TF).
+ eexists. split.
+ + eapply exec_Itailcall. eassumption. simpl. rewrite symbols_preserved. rewrite GFS.
+ eassumption. apply function_sig_translated. assumption.
+ erewrite <- preserv_fnstacksize; eauto.
+ + repeat (constructor; auto).
+(* Ibuiltin *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Ibuiltin; eauto. eapply eval_builtin_args_preserved; eauto.
+ eapply external_call_symbols_preserved; eauto. eapply senv_preserved.
+ + econstructor; eauto.
+(* Icond *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ * (* match_inst_cond *)
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Icond; eauto.
+ + econstructor; eauto. destruct b; auto.
+ * (* match_inst_revcond *)
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Icond; eauto. rewrite eval_negate_condition. rewrite H0. simpl. eauto.
+ + econstructor; eauto. destruct b; auto.
+(* Ijumptable *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ exploit list_nth_z_dupmap; eauto. intros (pc'1 & LNZ & REVM).
+ eexists. split.
+ + eapply exec_Ijumptable; eauto.
+ + econstructor; eauto.
+(* Ireturn *)
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (i' & H2 & H3). inv H3.
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ + eapply exec_Ireturn; eauto. erewrite <- preserv_fnstacksize; eauto.
+ + econstructor; eauto.
+(* exec_function_internal *)
+ - inversion TRANSF as [dupmap f0 f0' MATCHF|]; subst. eexists. split.
+ + eapply exec_function_internal. erewrite <- preserv_fnstacksize; eauto.
+ + erewrite preserv_fnparams; eauto.
+ econstructor; eauto. apply dupmap_entrypoint. assumption.
+(* exec_function_external *)
+ - inversion TRANSF as [|]; subst. eexists. split.
+ + econstructor. eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + constructor. assumption.
+(* exec_return *)
+ - inv STACKS. destruct b1 as [res' f' sp' pc' rs']. eexists. split.
+ + constructor.
+ + inv H1. econstructor; eauto.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (semantics prog) (semantics tprog).
+Proof.
+ eapply forward_simulation_step with match_states.
+ - eapply senv_preserved.
+ - eapply transf_initial_states.
+ - eapply transf_final_states.
+ - eapply step_simulation.
+Qed.
+
+End PRESERVATION.
+
+End DuplicateProof.
diff --git a/backend/FirstNop.v b/backend/FirstNop.v
new file mode 100644
index 00000000..b3c765e4
--- /dev/null
+++ b/backend/FirstNop.v
@@ -0,0 +1,30 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+
+Definition transf_function (f: function) : function :=
+ let start_pc := Pos.succ (max_pc_function f) in
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.set start_pc (Inop f.(fn_entrypoint)) f.(fn_code);
+ fn_entrypoint := start_pc |}.
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
+
diff --git a/backend/FirstNopproof.v b/backend/FirstNopproof.v
new file mode 100644
index 00000000..5a1c5acf
--- /dev/null
+++ b/backend/FirstNopproof.v
@@ -0,0 +1,285 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import FirstNop.
+Require Import Lia.
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; reflexivity.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i,
+ f.(fn_code)!pc = Some i ->
+ (transf_function f).(fn_code)!pc = Some i.
+Proof.
+ intros until i. intro Hcode.
+ unfold transf_function; simpl.
+ destruct (peq pc (Pos.succ (max_pc_function f))) as [EQ | NEQ].
+ { assert (pc <= (max_pc_function f))%positive as LE by (eapply max_pc_function_sound; eassumption).
+ subst pc.
+ lia.
+ }
+ rewrite PTree.gso by congruence.
+ assumption.
+Qed.
+
+Hint Resolve transf_function_at : firstnop.
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+| match_frames_intro: forall res f sp pc rs,
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+(*
+Lemma match_pc_refl : forall f pc, match_pc f pc pc.
+Proof.
+ unfold match_pc.
+ left.
+ trivial.
+Qed.
+
+Hint Resolve match_pc_refl : firstnop.
+
+Lemma initial_jump:
+ forall f,
+ (fn_code (transf_function f)) ! (Pos.succ (max_pc_function f)) =
+ Some (Inop (fn_entrypoint f)).
+Proof.
+ intros. unfold transf_function. simpl.
+ apply PTree.gss.
+Qed.
+
+Hint Resolve initial_jump : firstnop.
+ *)
+
+Lemma match_pc_same :
+ forall f pc i,
+ PTree.get pc (fn_code f) = Some i ->
+ PTree.get pc (fn_code (transf_function f)) = Some i.
+Proof.
+ intros.
+ unfold transf_function. simpl.
+ rewrite <- H.
+ apply PTree.gso.
+ pose proof (max_pc_function_sound f pc i H) as LE.
+ unfold Ple in LE.
+ lia.
+Qed.
+
+Hint Resolve match_pc_same : firstnop.
+
+
+Definition measure (S: RTL.state) : nat :=
+ match S with
+ | State _ _ _ _ _ _ => 0%nat
+ | Callstate _ _ _ _ => 1%nat
+ | Returnstate _ _ _ => 0%nat
+ end.
+
+Lemma step_simulation:
+ forall S1 t S2,
+ step ge S1 t S2 ->
+ forall S1' (MS: match_states S1 S1'),
+ (exists S2', plus step tge S1' t S2' /\ match_states S2 S2')
+ \/ (measure S2 < measure S1 /\ t = E0 /\ match_states S2 S1')%nat.
+Proof.
+ induction 1; intros; inv MS.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Inop; eauto with firstnop.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Iop with (v:=v); eauto with firstnop.
+ rewrite <- H0.
+ apply eval_operation_preserved.
+ apply symbols_preserved.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Iload with (v:=v); eauto with firstnop.
+ all: rewrite <- H0.
+ all: auto using eval_addressing_preserved, symbols_preserved.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Iload_notrap1; eauto with firstnop.
+ all: rewrite <- H0;
+ apply eval_addressing_preserved;
+ apply symbols_preserved.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Iload_notrap2; eauto with firstnop.
+ all: rewrite <- H0;
+ apply eval_addressing_preserved;
+ apply symbols_preserved.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Istore; eauto with firstnop.
+ all: rewrite <- H0;
+ apply eval_addressing_preserved;
+ apply symbols_preserved.
+ + constructor; auto with firstnop.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Icall.
+ apply match_pc_same. exact H.
+ apply find_function_translated.
+ exact H0.
+ apply sig_preserved.
+ + constructor.
+ constructor; auto.
+ constructor.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Itailcall.
+ apply match_pc_same. exact H.
+ apply find_function_translated.
+ exact H0.
+ apply sig_preserved.
+ unfold transf_function; simpl.
+ eassumption.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Ibuiltin; eauto with firstnop.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Icond; eauto with firstnop.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Ijumptable; eauto with firstnop.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_Ireturn; eauto with firstnop.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_two.
+ * eapply exec_function_internal; eauto with firstnop.
+ * eapply exec_Inop.
+ unfold transf_function; simpl.
+ rewrite PTree.gss.
+ reflexivity.
+ * auto.
+ + constructor; auto.
+ - left. econstructor. split.
+ + eapply plus_one. eapply exec_function_external; eauto with firstnop.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + constructor; auto.
+ - left.
+ inv STACKS. inv H1.
+ econstructor; split.
+ + eapply plus_one. eapply exec_return; eauto.
+ + constructor; auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_star.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/ForwardMoves.v b/backend/ForwardMoves.v
new file mode 100644
index 00000000..1b375532
--- /dev/null
+++ b/backend/ForwardMoves.v
@@ -0,0 +1,345 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+(* Static analysis *)
+
+Module RELATION.
+
+Definition t := (PTree.t reg).
+Definition eq (r1 r2 : t) :=
+ forall x, (PTree.get x r1) = (PTree.get x r2).
+
+Definition top : t := PTree.empty reg.
+
+Lemma eq_refl: forall x, eq x x.
+Proof.
+ unfold eq.
+ intros; reflexivity.
+Qed.
+
+Lemma eq_sym: forall x y, eq x y -> eq y x.
+Proof.
+ unfold eq.
+ intros; eauto.
+Qed.
+
+Lemma eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+Proof.
+ unfold eq.
+ intros; congruence.
+Qed.
+
+Definition reg_beq (x y : reg) :=
+ if Pos.eq_dec x y then true else false.
+
+Definition beq (r1 r2 : t) := PTree.beq reg_beq r1 r2.
+
+Lemma beq_correct: forall r1 r2, beq r1 r2 = true -> eq r1 r2.
+Proof.
+ unfold beq, eq. intros r1 r2 EQ x.
+ pose proof (PTree.beq_correct reg_beq r1 r2) as CORRECT.
+ destruct CORRECT as [CORRECTF CORRECTB].
+ pose proof (CORRECTF EQ x) as EQx.
+ clear CORRECTF CORRECTB EQ.
+ unfold reg_beq in *.
+ destruct (r1 ! x) as [R1x | ] in *;
+ destruct (r2 ! x) as [R2x | ] in *;
+ trivial; try contradiction.
+ destruct (Pos.eq_dec R1x R2x) in *; congruence.
+Qed.
+
+Definition ge (r1 r2 : t) :=
+ forall x,
+ match PTree.get x r1 with
+ | None => True
+ | Some v => (PTree.get x r2) = Some v
+ end.
+
+Lemma ge_refl: forall r1 r2, eq r1 r2 -> ge r1 r2.
+Proof.
+ unfold eq, ge.
+ intros r1 r2 EQ x.
+ pose proof (EQ x) as EQx.
+ clear EQ.
+ destruct (r1 ! x).
+ - congruence.
+ - trivial.
+Qed.
+
+Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+Proof.
+ unfold ge.
+ intros r1 r2 r3 GE12 GE23 x.
+ pose proof (GE12 x) as GE12x; clear GE12.
+ pose proof (GE23 x) as GE23x; clear GE23.
+ destruct (r1 ! x); trivial.
+ destruct (r2 ! x); congruence.
+Qed.
+
+Definition lub (r1 r2 : t) :=
+ PTree.combine
+ (fun ov1 ov2 =>
+ match ov1, ov2 with
+ | (Some v1), (Some v2) =>
+ if Pos.eq_dec v1 v2
+ then ov1
+ else None
+ | None, _
+ | _, None => None
+ end)
+ r1 r2.
+
+Lemma ge_lub_left: forall x y, ge (lub x y) x.
+Proof.
+ unfold ge, lub.
+ intros r1 r2 x.
+ rewrite PTree.gcombine by reflexivity.
+ destruct (_ ! _); trivial.
+ destruct (_ ! _); trivial.
+ destruct (Pos.eq_dec _ _); trivial.
+Qed.
+
+Lemma ge_lub_right: forall x y, ge (lub x y) y.
+Proof.
+ unfold ge, lub.
+ intros r1 r2 x.
+ rewrite PTree.gcombine by reflexivity.
+ destruct (_ ! _); trivial.
+ destruct (_ ! _); trivial.
+ destruct (Pos.eq_dec _ _); trivial.
+ congruence.
+Qed.
+
+End RELATION.
+
+Module Type SEMILATTICE_WITHOUT_BOTTOM.
+
+ Parameter t: Type.
+ Parameter eq: t -> t -> Prop.
+ Axiom eq_refl: forall x, eq x x.
+ Axiom eq_sym: forall x y, eq x y -> eq y x.
+ Axiom eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+ Parameter beq: t -> t -> bool.
+ Axiom beq_correct: forall x y, beq x y = true -> eq x y.
+ Parameter ge: t -> t -> Prop.
+ Axiom ge_refl: forall x y, eq x y -> ge x y.
+ Axiom ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+ Parameter lub: t -> t -> t.
+ Axiom ge_lub_left: forall x y, ge (lub x y) x.
+ Axiom ge_lub_right: forall x y, ge (lub x y) y.
+
+End SEMILATTICE_WITHOUT_BOTTOM.
+
+Module ADD_BOTTOM(L : SEMILATTICE_WITHOUT_BOTTOM).
+ Definition t := option L.t.
+ Definition eq (a b : t) :=
+ match a, b with
+ | None, None => True
+ | Some x, Some y => L.eq x y
+ | Some _, None | None, Some _ => False
+ end.
+
+ Lemma eq_refl: forall x, eq x x.
+ Proof.
+ unfold eq; destruct x; trivial.
+ apply L.eq_refl.
+ Qed.
+
+ Lemma eq_sym: forall x y, eq x y -> eq y x.
+ Proof.
+ unfold eq; destruct x; destruct y; trivial.
+ apply L.eq_sym.
+ Qed.
+
+ Lemma eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+ Proof.
+ unfold eq; destruct x; destruct y; destruct z; trivial.
+ - apply L.eq_trans.
+ - contradiction.
+ Qed.
+
+ Definition beq (x y : t) :=
+ match x, y with
+ | None, None => true
+ | Some x, Some y => L.beq x y
+ | Some _, None | None, Some _ => false
+ end.
+
+ Lemma beq_correct: forall x y, beq x y = true -> eq x y.
+ Proof.
+ unfold beq, eq.
+ destruct x; destruct y; trivial; try congruence.
+ apply L.beq_correct.
+ Qed.
+
+ Definition ge (x y : t) :=
+ match x, y with
+ | None, Some _ => False
+ | _, None => True
+ | Some a, Some b => L.ge a b
+ end.
+
+ Lemma ge_refl: forall x y, eq x y -> ge x y.
+ Proof.
+ unfold eq, ge.
+ destruct x; destruct y; trivial.
+ apply L.ge_refl.
+ Qed.
+
+ Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+ Proof.
+ unfold ge.
+ destruct x; destruct y; destruct z; trivial; try contradiction.
+ apply L.ge_trans.
+ Qed.
+
+ Definition bot: t := None.
+ Lemma ge_bot: forall x, ge x bot.
+ Proof.
+ unfold ge, bot.
+ destruct x; trivial.
+ Qed.
+
+ Definition lub (a b : t) :=
+ match a, b with
+ | None, _ => b
+ | _, None => a
+ | (Some x), (Some y) => Some (L.lub x y)
+ end.
+
+ Lemma ge_lub_left: forall x y, ge (lub x y) x.
+ Proof.
+ unfold ge, lub.
+ destruct x; destruct y; trivial.
+ - apply L.ge_lub_left.
+ - apply L.ge_refl.
+ apply L.eq_refl.
+ Qed.
+
+ Lemma ge_lub_right: forall x y, ge (lub x y) y.
+ Proof.
+ unfold ge, lub.
+ destruct x; destruct y; trivial.
+ - apply L.ge_lub_right.
+ - apply L.ge_refl.
+ apply L.eq_refl.
+ Qed.
+End ADD_BOTTOM.
+
+Module RB := ADD_BOTTOM(RELATION).
+Module DS := Dataflow_Solver(RB)(NodeSetForward).
+
+Definition kill (dst : reg) (rel : RELATION.t) :=
+ PTree.filter1 (fun x => if Pos.eq_dec dst x then false else true)
+ (PTree.remove dst rel).
+
+Definition move (src dst : reg) (rel : RELATION.t) :=
+ PTree.set dst (match PTree.get src rel with
+ | Some src' => src'
+ | None => src
+ end) (kill dst rel).
+
+Fixpoint kill_builtin_res (res : builtin_res reg) (rel : RELATION.t) :=
+ match res with
+ | BR z => kill z rel
+ | BR_none => rel
+ | BR_splitlong hi lo => kill_builtin_res hi (kill_builtin_res lo rel)
+ end.
+
+Definition apply_instr instr x :=
+ match instr with
+ | Inop _
+ | Icond _ _ _ _ _
+ | Ijumptable _ _
+ | Istore _ _ _ _ _ => Some x
+ | Iop Omove (src :: nil) dst _ => Some (move src dst x)
+ | Iop _ _ dst _
+ | Iload _ _ _ _ dst _
+ | Icall _ _ _ dst _ => Some (kill dst x)
+ | Ibuiltin _ _ res _ => Some (RELATION.top) (* TODO (kill_builtin_res res x) *)
+ | Itailcall _ _ _ | Ireturn _ => RB.bot
+ end.
+
+Definition apply_instr' code (pc : node) (ro : RB.t) : RB.t :=
+ match ro with
+ | None => None
+ | Some x =>
+ match code ! pc with
+ | None => RB.bot
+ | Some instr => apply_instr instr x
+ end
+ end.
+
+Definition forward_map (f : RTL.function) := DS.fixpoint
+ (RTL.fn_code f) RTL.successors_instr
+ (apply_instr' (RTL.fn_code f)) (RTL.fn_entrypoint f) (Some RELATION.top).
+
+Definition get_r (rel : RELATION.t) (x : reg) :=
+ match PTree.get x rel with
+ | None => x
+ | Some src => src
+ end.
+
+Definition get_rb (rb : RB.t) (x : reg) :=
+ match rb with
+ | None => x
+ | Some rel => get_r rel x
+ end.
+
+Definition subst_arg (fmap : option (PMap.t RB.t)) (pc : node) (x : reg) : reg :=
+ match fmap with
+ | None => x
+ | Some inv => get_rb (PMap.get pc inv) x
+ end.
+
+Definition subst_args fmap pc := List.map (subst_arg fmap pc).
+
+(* Transform *)
+Definition transf_instr (fmap : option (PMap.t RB.t))
+ (pc: node) (instr: instruction) :=
+ match instr with
+ | Iop op args dst s =>
+ Iop op (subst_args fmap pc args) dst s
+ | Iload trap chunk addr args dst s =>
+ Iload trap chunk addr (subst_args fmap pc args) dst s
+ | Istore chunk addr args src s =>
+ Istore chunk addr (subst_args fmap pc args) src s
+ | Icall sig ros args dst s =>
+ Icall sig ros (subst_args fmap pc args) dst s
+ | Itailcall sig ros args =>
+ Itailcall sig ros (subst_args fmap pc args)
+ | Icond cond args s1 s2 i =>
+ Icond cond (subst_args fmap pc args) s1 s2 i
+ | Ijumptable arg tbl =>
+ Ijumptable (subst_arg fmap pc arg) tbl
+ | Ireturn (Some arg) =>
+ Ireturn (Some (subst_arg fmap pc arg))
+ | _ => instr
+ end.
+
+Definition transf_function (f: function) : function :=
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map (transf_instr (forward_map f)) f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
diff --git a/backend/ForwardMovesproof.v b/backend/ForwardMovesproof.v
new file mode 100644
index 00000000..f3e572e0
--- /dev/null
+++ b/backend/ForwardMovesproof.v
@@ -0,0 +1,813 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import FunInd.
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import ForwardMoves.
+
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; trivial.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i,
+ f.(fn_code)!pc = Some i ->
+ (transf_function f).(fn_code)!pc =
+ Some(transf_instr (forward_map f) pc i).
+Proof.
+ intros until i. intro CODE.
+ unfold transf_function; simpl.
+ rewrite PTree.gmap.
+ unfold option_map.
+ rewrite CODE.
+ reflexivity.
+Qed.
+
+(*
+Definition fmap_sem (fmap : option (PMap.t RB.t)) (pc : node) (rs : regset) :=
+ forall x : reg,
+ (rs # (subst_arg fmap pc x)) = (rs # x).
+ *)
+
+Lemma apply_instr'_bot :
+ forall code,
+ forall pc,
+ RB.eq (apply_instr' code pc RB.bot) RB.bot.
+Proof.
+ reflexivity.
+Qed.
+
+Definition get_rb_sem (rb : RB.t) (rs : regset) :=
+ match rb with
+ | None => False
+ | Some rel =>
+ forall x : reg,
+ (rs # (get_r rel x)) = (rs # x)
+ end.
+
+Lemma get_rb_sem_ge:
+ forall rb1 rb2 : RB.t,
+ (RB.ge rb1 rb2) ->
+ forall rs : regset,
+ (get_rb_sem rb2 rs) -> (get_rb_sem rb1 rs).
+Proof.
+ destruct rb1 as [r1 | ];
+ destruct rb2 as [r2 | ];
+ unfold get_rb_sem;
+ simpl;
+ intros GE rs RB2RS;
+ try contradiction.
+ unfold RELATION.ge in GE.
+ unfold get_r in *.
+ intro x.
+ pose proof (GE x) as GEx.
+ pose proof (RB2RS x) as RB2RSx.
+ destruct (r1 ! x) as [r1x | ] in *;
+ destruct (r2 ! x) as [r2x | ] in *;
+ congruence.
+Qed.
+
+Definition fmap_sem (fmap : option (PMap.t RB.t))
+ (pc : node) (rs : regset) :=
+ match fmap with
+ | None => True
+ | Some m => get_rb_sem (PMap.get pc m) rs
+ end.
+
+Lemma subst_arg_ok:
+ forall f,
+ forall pc,
+ forall rs,
+ forall arg,
+ fmap_sem (forward_map f) pc rs ->
+ rs # (subst_arg (forward_map f) pc arg) = rs # arg.
+Proof.
+ intros until arg.
+ intro SEM.
+ unfold fmap_sem in SEM.
+ destruct (forward_map f) as [map |]in *; trivial.
+ simpl.
+ unfold get_rb_sem in *.
+ destruct (map # pc).
+ 2: contradiction.
+ apply SEM.
+Qed.
+
+Lemma subst_args_ok:
+ forall f,
+ forall pc,
+ forall rs,
+ fmap_sem (forward_map f) pc rs ->
+ forall args,
+ rs ## (subst_args (forward_map f) pc args) = rs ## args.
+Proof.
+ induction args; trivial.
+ simpl.
+ f_equal.
+ apply subst_arg_ok; assumption.
+ assumption.
+Qed.
+
+Lemma kill_ok:
+ forall dst,
+ forall mpc,
+ forall rs,
+ forall v,
+ get_rb_sem (Some mpc) rs ->
+ get_rb_sem (Some (kill dst mpc)) rs # dst <- v.
+Proof.
+ unfold get_rb_sem.
+ intros until v.
+ intros SEM x.
+ destruct (Pos.eq_dec x dst) as [EQ | NEQ].
+ {
+ subst dst.
+ rewrite Regmap.gss.
+ unfold kill, get_r.
+ rewrite PTree.gfilter1.
+ rewrite PTree.grs.
+ apply Regmap.gss.
+ }
+ rewrite (Regmap.gso v rs NEQ).
+ unfold kill, get_r in *.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by assumption.
+ pose proof (SEM x) as SEMx.
+ destruct (mpc ! x).
+ {
+ destruct (Pos.eq_dec dst r).
+ {
+ subst dst.
+ rewrite Regmap.gso by assumption.
+ reflexivity.
+ }
+ rewrite Regmap.gso by congruence.
+ assumption.
+ }
+ rewrite Regmap.gso by assumption.
+ reflexivity.
+Qed.
+
+Lemma kill_weaken:
+ forall dst,
+ forall mpc,
+ forall rs,
+ get_rb_sem (Some mpc) rs ->
+ get_rb_sem (Some (kill dst mpc)) rs.
+Proof.
+ unfold get_rb_sem.
+ intros until rs.
+ intros SEM x.
+ destruct (Pos.eq_dec x dst) as [EQ | NEQ].
+ {
+ subst dst.
+ unfold kill, get_r.
+ rewrite PTree.gfilter1.
+ rewrite PTree.grs.
+ reflexivity.
+ }
+ unfold kill, get_r in *.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by assumption.
+ pose proof (SEM x) as SEMx.
+ destruct (mpc ! x).
+ {
+ destruct (Pos.eq_dec dst r).
+ {
+ reflexivity.
+ }
+ assumption.
+ }
+ reflexivity.
+Qed.
+
+Lemma top_ok :
+ forall rs, get_rb_sem (Some RELATION.top) rs.
+Proof.
+ unfold get_rb_sem, RELATION.top.
+ intros.
+ unfold get_r.
+ rewrite PTree.gempty.
+ reflexivity.
+Qed.
+
+Lemma move_ok:
+ forall mpc : RELATION.t,
+ forall src res : reg,
+ forall rs : regset,
+ get_rb_sem (Some mpc) rs ->
+ get_rb_sem (Some (move src res mpc)) (rs # res <- (rs # src)).
+Proof.
+ unfold get_rb_sem, move.
+ intros until rs.
+ intros SEM x.
+ unfold get_r in *.
+ destruct (Pos.eq_dec res x).
+ {
+ subst res.
+ rewrite PTree.gss.
+ rewrite Regmap.gss.
+ pose proof (SEM src) as SEMsrc.
+ destruct (mpc ! src) as [mpcsrc | ] in *.
+ {
+ destruct (Pos.eq_dec x mpcsrc).
+ {
+ subst mpcsrc.
+ rewrite Regmap.gss.
+ reflexivity.
+ }
+ rewrite Regmap.gso by congruence.
+ assumption.
+ }
+ destruct (Pos.eq_dec x src).
+ {
+ subst src.
+ rewrite Regmap.gss.
+ reflexivity.
+ }
+ rewrite Regmap.gso by congruence.
+ reflexivity.
+ }
+ rewrite PTree.gso by congruence.
+ rewrite Regmap.gso with (i := x) by congruence.
+ unfold kill.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by congruence.
+ pose proof (SEM x) as SEMx.
+ destruct (mpc ! x) as [ r |].
+ {
+ destruct (Pos.eq_dec res r).
+ {
+ subst r.
+ rewrite Regmap.gso by congruence.
+ trivial.
+ }
+ rewrite Regmap.gso by congruence.
+ assumption.
+ }
+ rewrite Regmap.gso by congruence.
+ reflexivity.
+Qed.
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+Definition is_killed_in_map (map : PMap.t RB.t) pc res :=
+ match PMap.get pc map with
+ | None => True
+ | Some rel => exists rel', RELATION.ge rel (kill res rel')
+ end.
+
+Definition is_killed_in_fmap fmap pc res :=
+ match fmap with
+ | None => True
+ | Some map => is_killed_in_map map pc res
+ end.
+
+Definition killed_twice:
+ forall rel : RELATION.t,
+ forall res,
+ RELATION.eq (kill res rel) (kill res (kill res rel)).
+Proof.
+ unfold kill, RELATION.eq.
+ intros.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gfilter1.
+ destruct (Pos.eq_dec res x).
+ {
+ subst res.
+ rewrite PTree.grs.
+ rewrite PTree.grs.
+ reflexivity.
+ }
+ rewrite PTree.gro by congruence.
+ rewrite PTree.gro by congruence.
+ rewrite PTree.gfilter1.
+ rewrite PTree.gro by congruence.
+ destruct (rel ! x) as [relx | ]; trivial.
+ destruct (Pos.eq_dec res relx); trivial.
+ destruct (Pos.eq_dec res relx); congruence.
+Qed.
+
+Lemma get_rb_killed:
+ forall mpc,
+ forall rs,
+ forall rel,
+ forall res,
+ forall vres,
+ (get_rb_sem (Some mpc) rs) ->
+ (RELATION.ge mpc (kill res rel)) ->
+ (get_rb_sem (Some mpc) rs # res <- vres).
+Proof.
+ simpl.
+ intros until vres.
+ intros SEM GE x.
+ pose proof (GE x) as GEx.
+ pose proof (SEM x) as SEMx.
+ unfold get_r in *.
+ destruct (mpc ! x) as [mpcx | ] in *; trivial.
+ unfold kill in GEx.
+ rewrite PTree.gfilter1 in GEx.
+ destruct (Pos.eq_dec res x) as [ | res_NE_x].
+ {
+ subst res.
+ rewrite PTree.grs in GEx.
+ discriminate.
+ }
+ rewrite PTree.gro in GEx by congruence.
+ rewrite Regmap.gso with (i := x) by congruence.
+ destruct (rel ! x) as [relx | ]; try discriminate.
+ destruct (Pos.eq_dec res relx) as [ res_EQ_relx | res_NE_relx] in *; try discriminate.
+ rewrite Regmap.gso by congruence.
+ congruence.
+Qed.
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+| match_frames_intro: forall res f sp pc rs,
+ (fmap_sem (forward_map f) pc rs) ->
+ (is_killed_in_fmap (forward_map f) pc res) ->
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ (fmap_sem (forward_map f) pc rs) ->
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Lemma op_cases:
+ forall op,
+ forall args,
+ forall dst,
+ forall s,
+ forall x,
+ (exists src, op=Omove /\ args = src :: nil /\
+ (apply_instr (Iop op args dst s) x) = Some (move src dst x))
+ \/
+ (apply_instr (Iop op args dst s) x) = Some (kill dst x).
+Proof.
+ destruct op; try (right; simpl; reflexivity).
+ destruct args as [| arg0 args0t]; try (right; simpl; reflexivity).
+ destruct args0t as [| arg1 args1t]; try (right; simpl; reflexivity).
+ left.
+ eauto.
+Qed.
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS; try TR_AT.
+- (* nop *)
+ econstructor; split. eapply exec_Inop; eauto.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply get_rb_sem_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ unfold get_rb_sem in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+- (* op *)
+ econstructor; split.
+ eapply exec_Iop with (v := v); eauto.
+ rewrite <- H0.
+ rewrite subst_args_ok by assumption.
+ apply eval_operation_preserved. exact symbols_preserved.
+ constructor; auto.
+
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ assert (RB.ge (map # pc') (apply_instr' (fn_code f) pc (map # pc))) as GE.
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr' in GE.
+ rewrite MPC in GE.
+ rewrite H in GE.
+
+ destruct (op_cases op args res pc' mpc) as [[src [OP [ARGS MOVE]]] | KILL].
+ {
+ subst op.
+ subst args.
+ rewrite MOVE in GE.
+ simpl in H0.
+ simpl in GE.
+ apply get_rb_sem_ge with (rb2 := Some (move src res mpc)).
+ assumption.
+ replace v with (rs # src) by congruence.
+ apply move_ok.
+ assumption.
+ }
+ rewrite KILL in GE.
+ apply get_rb_sem_ge with (rb2 := Some (kill res mpc)).
+ assumption.
+ apply kill_ok.
+ assumption.
+
+(* load *)
+- econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0.
+ apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload; eauto.
+ rewrite subst_args_ok; assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply get_rb_sem_ge with (rb2 := Some (kill dst mpc)).
+ {
+ replace (Some (kill dst mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_ok.
+ assumption.
+
+- (* load notrap1 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = None).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap1; eauto.
+ rewrite subst_args_ok; assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply get_rb_sem_ge with (rb2 := Some (kill dst mpc)).
+ {
+ replace (Some (kill dst mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_ok.
+ assumption.
+
+- (* load notrap2 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap2; eauto.
+ rewrite subst_args_ok; assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply get_rb_sem_ge with (rb2 := Some (kill dst mpc)).
+ {
+ replace (Some (kill dst mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_ok.
+ assumption.
+
+- (* store *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Istore; eauto.
+ rewrite subst_args_ok; assumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply get_rb_sem_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ unfold get_rb_sem in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+
+(* call *)
+- econstructor; split.
+ eapply exec_Icall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ rewrite subst_args_ok by assumption.
+ constructor. constructor; auto. constructor.
+
+ {
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+ apply get_rb_sem_ge with (rb2 := Some (kill res mpc)).
+ {
+ replace (Some (kill res mpc)) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply kill_weaken.
+ assumption.
+ }
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ assert (RB.ge (map # pc') (apply_instr' (fn_code f) pc (map # pc))) as GE.
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr' in GE.
+ unfold fmap_sem in *.
+ destruct (map # pc) as [mpc |] in *; try contradiction.
+ rewrite H in GE.
+ simpl in GE.
+ unfold is_killed_in_fmap, is_killed_in_map.
+ unfold RB.ge in GE.
+ destruct (map # pc') as [mpc'|] eqn:MPC' in *; trivial.
+ eauto.
+
+(* tailcall *)
+- econstructor; split.
+ eapply exec_Itailcall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ rewrite subst_args_ok by assumption.
+ constructor. auto.
+
+(* builtin *)
+- econstructor; split.
+ eapply exec_Ibuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ destruct (map # pc) as [mpc |] eqn:MPC in *; try contradiction.
+
+ apply get_rb_sem_ge with (rb2 := Some RELATION.top).
+ {
+ replace (Some RELATION.top) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl. tauto.
+ }
+ unfold apply_instr'.
+ rewrite H.
+ rewrite MPC.
+ reflexivity.
+ }
+ apply top_ok.
+
+(* cond *)
+- econstructor; split.
+ eapply exec_Icond; eauto.
+ rewrite subst_args_ok; eassumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply get_rb_sem_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl.
+ destruct b; tauto.
+ }
+ unfold apply_instr'.
+ unfold get_rb_sem in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+
+(* jumptbl *)
+- econstructor; split.
+ eapply exec_Ijumptable; eauto.
+ rewrite subst_arg_ok; eassumption.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply get_rb_sem_ge with (rb2 := map # pc); trivial.
+ replace (map # pc) with (apply_instr' (fn_code f) pc (map # pc)).
+ {
+ eapply DS.fixpoint_solution with (code := fn_code f) (successors := successors_instr); try eassumption.
+ 2: apply apply_instr'_bot.
+ simpl.
+ apply list_nth_z_in with (n := Int.unsigned n).
+ assumption.
+ }
+ unfold apply_instr'.
+ unfold get_rb_sem in *.
+ destruct (map # pc) in *; try contradiction.
+ rewrite H.
+ reflexivity.
+
+(* return *)
+- destruct or as [arg | ].
+ {
+ econstructor; split.
+ eapply exec_Ireturn; eauto.
+ unfold regmap_optget.
+ rewrite subst_arg_ok by eassumption.
+ constructor; auto.
+ }
+ econstructor; split.
+ eapply exec_Ireturn; eauto.
+ constructor; auto.
+
+
+(* internal function *)
+- simpl. econstructor; split.
+ eapply exec_function_internal; eauto.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ apply get_rb_sem_ge with (rb2 := Some RELATION.top).
+ {
+ eapply DS.fixpoint_entry with (code := fn_code f) (successors := successors_instr); try eassumption.
+ }
+ apply top_ok.
+
+(* external function *)
+- econstructor; split.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+
+(* return *)
+- inv STACKS. inv H1.
+ econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto.
+
+ simpl in *.
+ unfold fmap_sem in *.
+ destruct (forward_map _) as [map |] eqn:MAP in *; trivial.
+ unfold is_killed_in_fmap in H8.
+ unfold is_killed_in_map in H8.
+ destruct (map # pc) as [mpc |] in *; try contradiction.
+ destruct H8 as [rel' RGE].
+ eapply get_rb_killed; eauto.
+Qed.
+
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/IRC.ml b/backend/IRC.ml
index 6f4bbe29..d9e4651e 100644
--- a/backend/IRC.ml
+++ b/backend/IRC.ml
@@ -15,6 +15,7 @@ open Camlcoq
open AST
open Registers
open Machregs
+open Machregsaux
open Locations
open Conventions1
open XTL
@@ -237,12 +238,6 @@ type graph = {
according to their types. A variable can be forced into class 2
by giving it a negative spill cost. *)
-let class_of_type = function
- | Tint | Tlong -> 0
- | Tfloat | Tsingle -> 1
- | Tany32 -> 0
- | Tany64 -> if Archi.ptr64 then 0 else 1
-
let class_of_reg r =
if Conventions1.is_float_reg r then 1 else 0
diff --git a/backend/IRC.mli b/backend/IRC.mli
index 59471329..254f27ff 100644
--- a/backend/IRC.mli
+++ b/backend/IRC.mli
@@ -39,5 +39,4 @@ val add_pref: graph -> var -> var -> unit
val coloring: graph -> (var -> loc)
(* Auxiliaries to deal with register classes *)
-val class_of_type: AST.typ -> int
val class_of_loc: loc -> int
diff --git a/backend/Inject.v b/backend/Inject.v
new file mode 100644
index 00000000..a24fef50
--- /dev/null
+++ b/backend/Inject.v
@@ -0,0 +1,134 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+
+Local Open Scope positive.
+
+Inductive inj_instr : Type :=
+ | INJnop
+ | INJop: operation -> list reg -> reg -> inj_instr
+ | INJload: memory_chunk -> addressing -> list reg -> reg -> inj_instr.
+
+Definition inject_instr (i : inj_instr) (pc' : node) : instruction :=
+ match i with
+ | INJnop => Inop pc'
+ | INJop op args dst => Iop op args dst pc'
+ | INJload chunk addr args dst => Iload NOTRAP chunk addr args dst pc'
+ end.
+
+Fixpoint inject_list (prog : code) (pc : node) (dst : node)
+ (l : list inj_instr) : node * code :=
+ let pc' := Pos.succ pc in
+ match l with
+ | nil => (pc', PTree.set pc (Inop dst) prog)
+ | h::t =>
+ inject_list (PTree.set pc (inject_instr h pc') prog)
+ pc' dst t
+ end.
+
+Definition successor (i : instruction) : node :=
+ match i with
+ | Inop pc' => pc'
+ | Iop _ _ _ pc' => pc'
+ | Iload _ _ _ _ _ pc' => pc'
+ | Istore _ _ _ _ pc' => pc'
+ | Icall _ _ _ _ pc' => pc'
+ | Ibuiltin _ _ _ pc' => pc'
+ | Icond _ _ pc' _ _ => pc'
+ | Itailcall _ _ _
+ | Ijumptable _ _
+ | Ireturn _ => 1
+ end.
+
+Definition alter_successor (i : instruction) (pc' : node) : instruction :=
+ match i with
+ | Inop _ => Inop pc'
+ | Iop op args dst _ => Iop op args dst pc'
+ | Iload trap chunk addr args dst _ => Iload trap chunk addr args dst pc'
+ | Istore chunk addr args src _ => Istore chunk addr args src pc'
+ | Ibuiltin ef args res _ => Ibuiltin ef args res pc'
+ | Icond cond args _ pc2 expected => Icond cond args pc' pc2 expected
+ | Icall sig ros args res _ => Icall sig ros args res pc'
+ | Itailcall _ _ _
+ | Ijumptable _ _
+ | Ireturn _ => i
+ end.
+
+Definition inject_at (prog : code) (pc extra_pc : node)
+ (l : list inj_instr) : node * code :=
+ match PTree.get pc prog with
+ | Some i =>
+ inject_list (PTree.set pc (alter_successor i extra_pc) prog)
+ extra_pc (successor i) l
+ | None => inject_list prog extra_pc 1 l (* does not happen *)
+ end.
+
+Definition inject_at' (already : node * code) pc l :=
+ let (extra_pc, prog) := already in
+ inject_at prog pc extra_pc l.
+
+Definition inject_l (prog : code) extra_pc injections :=
+ List.fold_left (fun already (injection : node * (list inj_instr)) =>
+ inject_at' already (fst injection) (snd injection))
+ injections
+ (extra_pc, prog).
+(*
+Definition inject' (prog : code) (extra_pc : node) (injections : PTree.t (list inj_instr)) :=
+ PTree.fold inject_at' injections (extra_pc, prog).
+
+Definition inject prog extra_pc injections : code :=
+ snd (inject' prog extra_pc injections).
+*)
+
+Section INJECTOR.
+ Variable gen_injections : function -> node -> reg -> PTree.t (list inj_instr).
+
+ Definition valid_injection_instr (max_reg : reg) (i : inj_instr) :=
+ match i with
+ | INJnop => true
+ | INJop op args res => (max_reg <? res) && (negb (is_trapping_op op)
+ && (Datatypes.length args =? args_of_operation op)%nat)
+ | INJload _ _ _ res => max_reg <? res
+ end.
+
+ Definition valid_injections1 max_pc max_reg :=
+ List.forallb
+ (fun injection =>
+ ((fst injection) <=? max_pc) &&
+ (List.forallb (valid_injection_instr max_reg) (snd injection))
+ ).
+
+ Definition valid_injections f :=
+ valid_injections1 (max_pc_function f) (max_reg_function f).
+
+ Definition transf_function (f : function) : res function :=
+ let max_pc := max_pc_function f in
+ let max_reg := max_reg_function f in
+ let injections := PTree.elements (gen_injections f max_pc max_reg) in
+ if valid_injections1 max_pc max_reg injections
+ then
+ OK {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := snd (inject_l (fn_code f) (Pos.succ max_pc) injections);
+ fn_entrypoint := f.(fn_entrypoint) |}
+ else Error (msg "Inject.transf_function: injections at bad locations").
+
+Definition transf_fundef (fd: fundef) : res fundef :=
+ AST.transf_partial_fundef transf_function fd.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program transf_fundef p.
+End INJECTOR.
diff --git a/backend/Injectproof.v b/backend/Injectproof.v
new file mode 100644
index 00000000..dd5e72f8
--- /dev/null
+++ b/backend/Injectproof.v
@@ -0,0 +1,1806 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Globalenvs Values Events.
+Require Import Inject.
+Require Import Lia.
+
+Local Open Scope positive.
+
+Lemma inject_list_preserves:
+ forall l prog pc dst pc0,
+ pc0 < pc ->
+ PTree.get pc0 (snd (inject_list prog pc dst l)) = PTree.get pc0 prog.
+Proof.
+ induction l; intros; simpl.
+ - apply PTree.gso. lia.
+ - rewrite IHl by lia.
+ apply PTree.gso. lia.
+Qed.
+
+Fixpoint pos_add_nat x n :=
+ match n with
+ | O => x
+ | S n' => Pos.succ (pos_add_nat x n')
+ end.
+
+Lemma pos_add_nat_increases : forall x n, x <= (pos_add_nat x n).
+Proof.
+ induction n; simpl; lia.
+Qed.
+
+Lemma pos_add_nat_succ : forall n x,
+ Pos.succ (pos_add_nat x n) = pos_add_nat (Pos.succ x) n.
+Proof.
+ induction n; simpl; intros; trivial.
+ rewrite IHn.
+ reflexivity.
+Qed.
+
+Lemma pos_add_nat_monotone : forall x n1 n2,
+ (n1 < n2) % nat ->
+ (pos_add_nat x n1) < (pos_add_nat x n2).
+Proof.
+ induction n1; destruct n2; intros.
+ - lia.
+ - simpl.
+ pose proof (pos_add_nat_increases x n2).
+ lia.
+ - lia.
+ - simpl.
+ specialize IHn1 with n2.
+ lia.
+Qed.
+
+Lemma inject_list_increases:
+ forall l prog pc dst,
+ (fst (inject_list prog pc dst l)) = pos_add_nat pc (S (List.length l)).
+Proof.
+ induction l; simpl; intros; trivial.
+ rewrite IHl.
+ simpl.
+ rewrite <- pos_add_nat_succ.
+ reflexivity.
+Qed.
+
+Program Fixpoint bounded_nth
+ {T : Type} (k : nat) (l : list T) (BOUND : (k < List.length l)%nat) : T :=
+ match k, l with
+ | O, h::_ => h
+ | (S k'), _::l' => bounded_nth k' l' _
+ | _, nil => _
+ end.
+Obligation 1.
+Proof.
+ simpl in BOUND.
+ lia.
+Qed.
+Obligation 2.
+Proof.
+ simpl in BOUND.
+ lia.
+Qed.
+
+Program Definition bounded_nth_S_statement : Prop :=
+ forall (T : Type) (k : nat) (h : T) (l : list T) (BOUND : (k < List.length l)%nat),
+ bounded_nth (S k) (h::l) _ = bounded_nth k l BOUND.
+Obligation 1.
+lia.
+Qed.
+
+Lemma bounded_nth_proof_irr :
+ forall {T : Type} (k : nat) (l : list T)
+ (BOUND1 BOUND2 : (k < List.length l)%nat),
+ (bounded_nth k l BOUND1) = (bounded_nth k l BOUND2).
+Proof.
+ induction k; destruct l; simpl; intros; trivial; lia.
+Qed.
+
+Lemma bounded_nth_S : bounded_nth_S_statement.
+Proof.
+ unfold bounded_nth_S_statement.
+ induction k; destruct l; simpl; intros; trivial.
+ 1, 2: lia.
+ apply bounded_nth_proof_irr.
+Qed.
+
+Lemma inject_list_injected:
+ forall l prog pc dst k (BOUND : (k < (List.length l))%nat),
+ PTree.get (pos_add_nat pc k) (snd (inject_list prog pc dst l)) =
+ Some (inject_instr (bounded_nth k l BOUND) (Pos.succ (pos_add_nat pc k))).
+Proof.
+ induction l; simpl; intros.
+ - lia.
+ - simpl.
+ destruct k as [ | k]; simpl pos_add_nat.
+ + simpl bounded_nth.
+ rewrite inject_list_preserves by lia.
+ apply PTree.gss.
+ + rewrite pos_add_nat_succ.
+ erewrite IHl.
+ f_equal. f_equal.
+ simpl.
+ apply bounded_nth_proof_irr.
+ Unshelve.
+ lia.
+Qed.
+
+Lemma inject_list_injected_end:
+ forall l prog pc dst,
+ PTree.get (pos_add_nat pc (List.length l))
+ (snd (inject_list prog pc dst l)) =
+ Some (Inop dst).
+Proof.
+ induction l; simpl; intros.
+ - apply PTree.gss.
+ - rewrite pos_add_nat_succ.
+ apply IHl.
+Qed.
+
+Lemma inject_at_preserves :
+ forall prog pc extra_pc l pc0,
+ pc0 < extra_pc ->
+ pc0 <> pc ->
+ PTree.get pc0 (snd (inject_at prog pc extra_pc l)) = PTree.get pc0 prog.
+Proof.
+ intros. unfold inject_at.
+ destruct (PTree.get pc prog) eqn:GET.
+ - rewrite inject_list_preserves; trivial.
+ apply PTree.gso; lia.
+ - apply inject_list_preserves; trivial.
+Qed.
+
+Lemma inject_at_redirects:
+ forall prog pc extra_pc l i,
+ pc < extra_pc ->
+ PTree.get pc prog = Some i ->
+ PTree.get pc (snd (inject_at prog pc extra_pc l)) =
+ Some (alter_successor i extra_pc).
+Proof.
+ intros until i. intros BEFORE GET. unfold inject_at.
+ rewrite GET.
+ rewrite inject_list_preserves by trivial.
+ apply PTree.gss.
+Qed.
+
+Lemma inject_at_redirects_none:
+ forall prog pc extra_pc l,
+ pc < extra_pc ->
+ PTree.get pc prog = None ->
+ PTree.get pc (snd (inject_at prog pc extra_pc l)) = None.
+Proof.
+ intros until l. intros BEFORE GET. unfold inject_at.
+ rewrite GET.
+ rewrite inject_list_preserves by trivial.
+ assumption.
+Qed.
+
+Lemma inject_at_increases:
+ forall prog pc extra_pc l,
+ (fst (inject_at prog pc extra_pc l)) = pos_add_nat extra_pc (S (List.length l)).
+Proof.
+ intros. unfold inject_at.
+ destruct (PTree.get pc prog).
+ all: apply inject_list_increases.
+Qed.
+
+Lemma inject_at_injected:
+ forall l prog pc extra_pc k (BOUND : (k < (List.length l))%nat),
+ PTree.get (pos_add_nat extra_pc k) (snd (inject_at prog pc extra_pc l)) =
+ Some (inject_instr (bounded_nth k l BOUND) (Pos.succ (pos_add_nat extra_pc k))).
+Proof.
+ intros. unfold inject_at.
+ destruct (prog ! pc); apply inject_list_injected.
+Qed.
+
+Lemma inject_at_injected_end:
+ forall l prog pc extra_pc i,
+ PTree.get pc prog = Some i ->
+ PTree.get (pos_add_nat extra_pc (List.length l))
+ (snd (inject_at prog pc extra_pc l)) =
+ Some (Inop (successor i)).
+Proof.
+ intros until i. intro REW. unfold inject_at.
+ rewrite REW.
+ apply inject_list_injected_end.
+Qed.
+
+Lemma pair_expand:
+ forall { A B : Type } (p : A*B),
+ p = ((fst p), (snd p)).
+Proof.
+ destruct p; simpl; trivial.
+Qed.
+
+Fixpoint inject_l_position extra_pc
+ (injections : list (node * (list inj_instr)))
+ (k : nat) {struct injections} : node :=
+ match injections with
+ | nil => extra_pc
+ | (pc,l)::l' =>
+ match k with
+ | O => extra_pc
+ | S k' =>
+ inject_l_position
+ (Pos.succ (pos_add_nat extra_pc (List.length l))) l' k'
+ end
+ end.
+
+Lemma inject_l_position_increases : forall injections pc k,
+ pc <= inject_l_position pc injections k.
+Proof.
+ induction injections; simpl; intros.
+ lia.
+ destruct a as [_ l].
+ destruct k.
+ lia.
+ specialize IHinjections with (pc := (Pos.succ (pos_add_nat pc (Datatypes.length l)))) (k := k).
+ assert (pc <= (pos_add_nat pc (Datatypes.length l))) by apply pos_add_nat_increases.
+ lia.
+Qed.
+
+Definition inject_l (prog : code) extra_pc injections :=
+ List.fold_left (fun already (injection : node * (list inj_instr)) =>
+ inject_at' already (fst injection) (snd injection))
+ injections
+ (extra_pc, prog).
+
+Lemma inject_l_preserves :
+ forall injections prog extra_pc pc0,
+ pc0 < extra_pc ->
+ List.forallb (fun injection => if peq (fst injection) pc0 then false else true) injections = true ->
+ PTree.get pc0 (snd (inject_l prog extra_pc injections)) = PTree.get pc0 prog.
+Proof.
+ induction injections;
+ intros until pc0; intros BEFORE ALL; simpl; trivial.
+ unfold inject_l.
+ destruct a as [pc l]. simpl.
+ simpl in ALL.
+ rewrite andb_true_iff in ALL.
+ destruct ALL as [NEQ ALL].
+ rewrite pair_expand with (p := inject_at prog pc extra_pc l).
+ fold (inject_l (snd (inject_at prog pc extra_pc l))
+ (fst (inject_at prog pc extra_pc l))
+ injections).
+ rewrite IHinjections; trivial.
+ - apply inject_at_preserves; trivial.
+ destruct (peq pc pc0); congruence.
+ - rewrite inject_at_increases.
+ pose proof (pos_add_nat_increases extra_pc (S (Datatypes.length l))).
+ lia.
+Qed.
+
+Lemma nth_error_nil : forall { T : Type} k,
+ nth_error (@nil T) k = None.
+Proof.
+ destruct k; simpl; trivial.
+Qed.
+
+Lemma inject_l_injected:
+ forall injections prog injnum pc l extra_pc k
+ (BELOW : forallb (fun injection => (fst injection) <? extra_pc) injections = true)
+ (NUMBER : nth_error injections injnum = Some (pc, l))
+ (BOUND : (k < (List.length l))%nat),
+ PTree.get (pos_add_nat (inject_l_position extra_pc injections injnum) k)
+ (snd (inject_l prog extra_pc injections)) =
+ Some (inject_instr (bounded_nth k l BOUND)
+ (Pos.succ (pos_add_nat (inject_l_position extra_pc injections injnum) k))).
+Proof.
+ induction injections; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER.
+ }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ unfold inject_l.
+ destruct a as [pc' l'].
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at prog pc' extra_pc l').
+ progress fold (inject_l (snd (inject_at prog pc' extra_pc l'))
+ (fst (inject_at prog pc' extra_pc l'))
+ injections).
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - apply inject_at_injected.
+ - rewrite inject_at_increases.
+ apply pos_add_nat_monotone.
+ lia.
+ - rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ destruct peq as [EQ | ]; trivial.
+ rewrite EQ in IN.
+ rewrite Pos.ltb_lt in IN.
+ pose proof (pos_add_nat_increases extra_pc k).
+ lia.
+ }
+ simpl.
+ rewrite inject_at_increases.
+ apply IHinjections with (pc := pc); trivial.
+ rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ pose proof (pos_add_nat_increases extra_pc (Datatypes.length l')).
+ rewrite Pos.ltb_lt.
+ rewrite Pos.ltb_lt in IN.
+ lia.
+Qed.
+
+Lemma inject_l_injected_end:
+ forall injections prog injnum pc i l extra_pc
+ (BEFORE : PTree.get pc prog = Some i)
+ (DISTINCT : list_norepet (map fst injections))
+ (BELOW : forallb (fun injection => (fst injection) <? extra_pc) injections = true)
+ (NUMBER : nth_error injections injnum = Some (pc, l)),
+ PTree.get (pos_add_nat (inject_l_position extra_pc injections injnum)
+ (List.length l))
+ (snd (inject_l prog extra_pc injections)) =
+ Some (Inop (successor i)).
+Proof.
+ induction injections; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER.
+ }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ unfold inject_l.
+ destruct a as [pc' l'].
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at prog pc' extra_pc l').
+ progress fold (inject_l (snd (inject_at prog pc' extra_pc l'))
+ (fst (inject_at prog pc' extra_pc l'))
+ injections).
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - apply inject_at_injected_end; trivial.
+ - rewrite inject_at_increases.
+ apply pos_add_nat_monotone.
+ lia.
+ - rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ destruct peq as [EQ | ]; trivial.
+ rewrite EQ in IN.
+ rewrite Pos.ltb_lt in IN.
+ pose proof (pos_add_nat_increases extra_pc (Datatypes.length l)).
+ lia.
+ }
+ simpl.
+ rewrite inject_at_increases.
+ apply IHinjections with (pc := pc); trivial.
+ {
+ rewrite <- BEFORE.
+ apply inject_at_preserves.
+ {
+ apply nth_error_In in NUMBER.
+ rewrite forallb_forall in BELOW2.
+ specialize BELOW2 with (pc, l).
+ apply BELOW2 in NUMBER.
+ apply Pos.ltb_lt in NUMBER.
+ simpl in NUMBER.
+ assumption.
+ }
+ simpl in DISTINCT.
+ inv DISTINCT.
+ intro SAME.
+ subst pc'.
+ apply nth_error_in in NUMBER.
+ assert (In (fst (pc, l)) (map fst injections)) as Z.
+ { apply in_map. assumption.
+ }
+ simpl in Z.
+ auto.
+ }
+ { inv DISTINCT.
+ assumption.
+ }
+ {
+ rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ pose proof (pos_add_nat_increases extra_pc (Datatypes.length l')).
+ rewrite Pos.ltb_lt.
+ rewrite Pos.ltb_lt in IN.
+ assert (pos_add_nat extra_pc (Datatypes.length l') <
+ pos_add_nat extra_pc (S (Datatypes.length l'))).
+ { apply pos_add_nat_monotone.
+ lia.
+ }
+ lia.
+ }
+Qed.
+
+
+Lemma inject_l_redirects:
+ forall injections prog injnum pc i l extra_pc
+ (BEFORE : PTree.get pc prog = Some i)
+ (DISTINCT : list_norepet (map fst injections))
+ (BELOW : forallb (fun injection => (fst injection) <? extra_pc) injections = true)
+ (NUMBER : nth_error injections injnum = Some (pc, l)),
+ PTree.get pc (snd (inject_l prog extra_pc injections)) =
+ Some (alter_successor i (inject_l_position extra_pc injections injnum)).
+Proof.
+ induction injections; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER.
+ }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ unfold inject_l.
+ destruct a as [pc' l'].
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at prog pc' extra_pc l').
+ progress fold (inject_l (snd (inject_at prog pc' extra_pc l'))
+ (fst (inject_at prog pc' extra_pc l'))
+ injections).
+ simpl in BELOW1.
+ apply Pos.ltb_lt in BELOW1.
+ inv DISTINCT.
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - apply inject_at_redirects; trivial.
+ - rewrite inject_at_increases.
+ pose proof (pos_add_nat_increases extra_pc (S (Datatypes.length l))).
+ lia.
+ - rewrite forallb_forall.
+ intros loc IN.
+ destruct loc as [pc' l'].
+ simpl in *.
+ destruct peq; trivial.
+ subst pc'.
+ apply in_map with (f := fst) in IN.
+ simpl in IN.
+ exfalso.
+ auto.
+ }
+ simpl.
+ rewrite inject_at_increases.
+ apply IHinjections with (pc := pc) (l := l); trivial.
+ {
+ rewrite <- BEFORE.
+ apply nth_error_In in NUMBER.
+ rewrite forallb_forall in BELOW2.
+ specialize BELOW2 with (pc, l).
+ simpl in BELOW2.
+ rewrite Pos.ltb_lt in BELOW2.
+ apply inject_at_preserves; auto.
+ assert (In (fst (pc, l)) (map fst injections)) as Z.
+ { apply in_map. assumption.
+ }
+ simpl in Z.
+ intro EQ.
+ subst pc'.
+ auto.
+ }
+ {
+ rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ pose proof (pos_add_nat_increases extra_pc (Datatypes.length l')).
+ rewrite Pos.ltb_lt.
+ rewrite Pos.ltb_lt in IN.
+ assert (pos_add_nat extra_pc (Datatypes.length l') <
+ pos_add_nat extra_pc (S (Datatypes.length l'))).
+ { apply pos_add_nat_monotone.
+ lia.
+ }
+ lia.
+ }
+Qed.
+
+(*
+Lemma inject'_preserves :
+ forall injections prog extra_pc pc0,
+ pc0 < extra_pc ->
+ PTree.get pc0 injections = None ->
+ PTree.get pc0 (snd (inject' prog extra_pc injections)) = PTree.get pc0 prog.
+Proof.
+ intros. unfold inject'.
+ rewrite PTree.fold_spec.
+ change (fold_left
+ (fun (a : node * code) (p : positive * list inj_instr) =>
+ inject_at' a (fst p) (snd p)) (PTree.elements injections)
+ (extra_pc, prog)) with (inject_l prog extra_pc (PTree.elements injections)).
+ apply inject_l_preserves; trivial.
+ rewrite List.forallb_forall.
+ intros injection IN.
+ destruct injection as [pc l].
+ simpl.
+ apply PTree.elements_complete in IN.
+ destruct (peq pc pc0); trivial.
+ congruence.
+Qed.
+
+Lemma inject_preserves :
+ forall injections prog extra_pc pc0,
+ pc0 < extra_pc ->
+ PTree.get pc0 injections = None ->
+ PTree.get pc0 (inject prog extra_pc injections) = PTree.get pc0 prog.
+Proof.
+ unfold inject'.
+ apply inject'_preserves.
+Qed.
+*)
+
+Section INJECTOR.
+ Variable gen_injections : function -> node -> reg -> PTree.t (list inj_instr).
+
+ Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => transf_fundef gen_injections f = OK tf) eq p tp.
+
+ Lemma transf_program_match:
+ forall p tp, transf_program gen_injections p = OK tp -> match_prog p tp.
+ Proof.
+ intros. eapply match_transform_partial_program; eauto.
+ Qed.
+
+ Section PRESERVATION.
+
+ Variables prog tprog: program.
+ Hypothesis TRANSF: match_prog prog tprog.
+ Let ge := Genv.globalenv prog.
+ Let tge := Genv.globalenv tprog.
+
+ Definition match_regs (f : function) (rs rs' : regset) :=
+ forall r, r <= max_reg_function f -> (rs'#r = rs#r).
+
+ Lemma match_regs_refl : forall f rs, match_regs f rs rs.
+ Proof.
+ unfold match_regs. intros. trivial.
+ Qed.
+
+ Lemma match_regs_trans : forall f rs1 rs2 rs3,
+ match_regs f rs1 rs2 -> match_regs f rs2 rs3 -> match_regs f rs1 rs3.
+ Proof.
+ unfold match_regs. intros until rs3. intros M12 M23 r.
+ specialize M12 with r.
+ specialize M23 with r.
+ intuition congruence.
+ Qed.
+
+ Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+ | match_frames_intro: forall res f tf sp pc pc' rs trs
+ (FUN : transf_function gen_injections f = OK tf)
+ (REGS : match_regs f rs trs)
+ (STAR:
+ forall ts m trs1,
+ exists trs2,
+ (match_regs f trs1 trs2) /\
+ Smallstep.star RTL.step tge
+ (State ts tf sp pc' trs1 m) E0
+ (State ts tf sp pc trs2 m)),
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res tf sp pc' trs).
+
+ Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s f tf sp pc rs trs m ts
+ (FUN : transf_function gen_injections f = OK tf)
+ (STACKS: list_forall2 match_frames s ts)
+ (REGS: match_regs f rs trs),
+ match_states (State s f sp pc rs m) (State ts tf sp pc trs m)
+ | match_states_call:
+ forall s fd tfd args m ts
+ (FUN : transf_fundef gen_injections fd = OK tfd)
+ (STACKS: list_forall2 match_frames s ts),
+ match_states (Callstate s fd args m) (Callstate ts tfd args m)
+ | match_states_return:
+ forall s res m ts
+ (STACKS: list_forall2 match_frames s ts),
+ match_states (Returnstate s res m)
+ (Returnstate ts res m).
+
+ Lemma functions_translated:
+ forall (v: val) (f: RTL.fundef),
+ Genv.find_funct ge v = Some f ->
+ exists tf,
+ Genv.find_funct tge v = Some tf /\
+ transf_fundef gen_injections f = OK tf.
+ Proof.
+ apply (Genv.find_funct_transf_partial TRANSF).
+ Qed.
+
+ Lemma function_ptr_translated:
+ forall (b: block) (f: RTL.fundef),
+ Genv.find_funct_ptr ge b = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge b = Some tf /\
+ transf_fundef gen_injections f = OK tf.
+ Proof.
+ apply (Genv.find_funct_ptr_transf_partial TRANSF).
+ Qed.
+
+ Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+ Proof.
+ apply (Genv.find_symbol_match TRANSF).
+ Qed.
+
+ Lemma senv_preserved:
+ Senv.equiv ge tge.
+ Proof.
+ apply (Genv.senv_match TRANSF).
+ Qed.
+
+ Lemma sig_preserved:
+ forall f tf, transf_fundef gen_injections f = OK tf
+ -> funsig tf = funsig f.
+ Proof.
+ destruct f; simpl; intros; monadInv H; trivial.
+ unfold transf_function in *.
+ destruct valid_injections1 in EQ.
+ 2: discriminate.
+ inv EQ.
+ reflexivity.
+ Qed.
+
+ Lemma stacksize_preserved:
+ forall f tf, transf_function gen_injections f = OK tf ->
+ fn_stacksize tf = fn_stacksize f.
+ Proof.
+ destruct f.
+ unfold transf_function.
+ intros.
+ destruct valid_injections1 in H.
+ 2: discriminate.
+ inv H.
+ reflexivity.
+ Qed.
+
+ Lemma params_preserved:
+ forall f tf, transf_function gen_injections f = OK tf ->
+ fn_params tf = fn_params f.
+ Proof.
+ destruct f.
+ unfold transf_function.
+ intros.
+ destruct valid_injections1 in H.
+ 2: discriminate.
+ inv H.
+ reflexivity.
+ Qed.
+
+ Lemma entrypoint_preserved:
+ forall f tf, transf_function gen_injections f = OK tf ->
+ fn_entrypoint tf = fn_entrypoint f.
+ Proof.
+ destruct f.
+ unfold transf_function.
+ intros.
+ destruct valid_injections1 in H.
+ 2: discriminate.
+ inv H.
+ reflexivity.
+ Qed.
+
+ Lemma sig_preserved2:
+ forall f tf, transf_function gen_injections f = OK tf ->
+ fn_sig tf = fn_sig f.
+ Proof.
+ destruct f.
+ unfold transf_function.
+ intros.
+ destruct valid_injections1 in H.
+ 2: discriminate.
+ inv H.
+ reflexivity.
+ Qed.
+
+ Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+ Proof.
+ intros. inversion H.
+ exploit function_ptr_translated; eauto.
+ intros (tf & A & B).
+ exists (Callstate nil tf nil m0); split.
+ - econstructor; eauto.
+ + eapply (Genv.init_mem_match TRANSF); eauto.
+ + replace (prog_main tprog) with (prog_main prog).
+ rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main; eauto.
+ + rewrite <- H3. eapply sig_preserved; eauto.
+ - constructor; trivial.
+ constructor.
+ Qed.
+
+ Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 ->
+ final_state S1 r -> final_state S2 r.
+ Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+ Qed.
+
+ Lemma assign_above:
+ forall f trs res v,
+ (max_reg_function f) < res ->
+ match_regs f trs trs # res <- v.
+ Proof.
+ unfold match_regs.
+ intros.
+ apply Regmap.gso.
+ lia.
+ Qed.
+
+ Lemma transf_function_inj_step:
+ forall ts f tf sp pc trs m ii
+ (FUN : transf_function gen_injections f = OK tf)
+ (GET : (fn_code tf) ! pc = Some (inject_instr ii (Pos.succ pc)))
+ (VALID : valid_injection_instr (max_reg_function f) ii = true),
+ exists trs',
+ RTL.step tge
+ (State ts tf sp pc trs m) E0
+ (State ts tf sp (Pos.succ pc) trs' m) /\
+ match_regs (f : function) trs trs'.
+ Proof.
+ destruct ii as [ |op args res | chunk addr args res]; simpl; intros.
+ - exists trs.
+ split.
+ * apply exec_Inop; assumption.
+ * apply match_regs_refl.
+ - repeat rewrite andb_true_iff in VALID.
+ rewrite negb_true_iff in VALID.
+ destruct VALID as (MAX_REG & NOTRAP & LENGTH).
+ rewrite Pos.ltb_lt in MAX_REG.
+ rewrite Nat.eqb_eq in LENGTH.
+ destruct (eval_operation ge sp op trs ## args m) as [v | ] eqn:EVAL.
+ + exists (trs # res <- v).
+ split.
+ * apply exec_Iop with (op := op) (args := args) (res := res); trivial.
+ rewrite eval_operation_preserved with (ge1 := ge).
+ assumption.
+ exact symbols_preserved.
+ * apply assign_above; auto.
+ + exfalso.
+ generalize EVAL.
+ apply is_trapping_op_sound; trivial.
+ rewrite map_length.
+ assumption.
+ - rewrite Pos.ltb_lt in VALID.
+ destruct (eval_addressing ge sp addr trs ## args) as [a | ] eqn:ADDR.
+ + destruct (Mem.loadv chunk m a) as [v | ] eqn:LOAD.
+ * exists (trs # res <- v).
+ split.
+ ** apply exec_Iload with (trap := NOTRAP) (chunk := chunk) (addr := addr) (args := args) (dst := res) (a := a); trivial.
+ all: try rewrite eval_addressing_preserved with (ge1 := ge).
+ all: auto using symbols_preserved.
+ ** apply assign_above; auto.
+ * exists (trs # res <- Vundef).
+ split.
+ ** apply exec_Iload_notrap2 with (chunk := chunk) (addr := addr) (args := args) (dst := res) (a := a); trivial.
+ all: rewrite eval_addressing_preserved with (ge1 := ge).
+ all: auto using symbols_preserved.
+ ** apply assign_above; auto.
+ + exists (trs # res <- Vundef).
+ split.
+ * apply exec_Iload_notrap1 with (chunk := chunk) (addr := addr) (args := args) (dst := res); trivial.
+ all: rewrite eval_addressing_preserved with (ge1 := ge).
+ all: auto using symbols_preserved.
+ * apply assign_above; auto.
+ Qed.
+
+ Lemma bounded_nth_In: forall {T : Type} (l : list T) k LESS,
+ In (bounded_nth k l LESS) l.
+ Proof.
+ induction l; simpl; intros.
+ lia.
+ destruct k; simpl.
+ - left; trivial.
+ - right. apply IHl.
+ Qed.
+
+ Lemma transf_function_inj_starstep_rec :
+ forall ts f tf sp m inj_n src_pc inj_pc inj_code
+ (FUN : transf_function gen_injections f = OK tf)
+ (INJ : nth_error (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n =
+ Some (src_pc, inj_code))
+ (POSITION : inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n = inj_pc)
+ (k : nat)
+ (CUR : (k <= (List.length inj_code))%nat)
+ (trs : regset),
+ exists trs',
+ match_regs (f : function) trs trs' /\
+ Smallstep.star RTL.step tge
+ (State ts tf sp (pos_add_nat inj_pc
+ ((List.length inj_code) - k)%nat) trs m) E0
+ (State ts tf sp (pos_add_nat inj_pc (List.length inj_code)) trs' m).
+ Proof.
+ induction k; simpl; intros.
+ { rewrite Nat.sub_0_r.
+ exists trs.
+ split.
+ - apply match_regs_refl.
+ - constructor.
+ }
+ assert (k <= Datatypes.length inj_code)%nat as KK by lia.
+ pose proof (IHk KK) as IH.
+ clear IHk KK.
+ pose proof FUN as VALIDATE.
+ unfold transf_function, valid_injections1 in VALIDATE.
+ destruct forallb eqn:FORALL in VALIDATE.
+ 2: discriminate.
+ injection VALIDATE.
+ intro TF.
+ symmetry in TF.
+ pose proof (inject_l_injected (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) (fn_code f) inj_n src_pc inj_code (Pos.succ (max_pc_function f)) ((List.length inj_code) - (S k))%nat) as INJECTED.
+ lapply INJECTED.
+ { clear INJECTED.
+ intro INJECTED.
+ assert ((Datatypes.length inj_code - S k <
+ Datatypes.length inj_code)%nat) as LESS by lia.
+ pose proof (INJECTED INJ LESS) as INJ'.
+ replace (snd
+ (inject_l (fn_code f) (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))))) with (fn_code tf) in INJ'.
+ 2: rewrite TF; simpl; reflexivity. apply transf_function_inj_step with (f:=f) (ts:=ts) (sp:=sp) (trs:=trs) (m := m) in INJ'.
+ 2: assumption.
+ {
+ destruct INJ' as [trs'' [STEP STEPMATCH]].
+ destruct (IH trs'') as [trs' [STARSTEPMATCH STARSTEP]].
+ exists trs'.
+ split.
+ { apply match_regs_trans with (rs2 := trs''); assumption. }
+ eapply Smallstep.star_step with (t1:=E0) (t2:=E0).
+ {
+ rewrite POSITION in STEP.
+ exact STEP.
+ }
+ {
+ replace (Datatypes.length inj_code - k)%nat
+ with (S (Datatypes.length inj_code - (S k)))%nat in STARSTEP by lia.
+ simpl pos_add_nat in STARSTEP.
+ exact STARSTEP.
+ }
+ constructor.
+ }
+ rewrite forallb_forall in FORALL.
+ specialize FORALL with (src_pc, inj_code).
+ lapply FORALL.
+ {
+ simpl.
+ rewrite andb_true_iff.
+ intros (SRC & ALL_VALID).
+ rewrite forallb_forall in ALL_VALID.
+ apply ALL_VALID.
+ apply bounded_nth_In.
+ }
+ apply nth_error_In with (n := inj_n).
+ assumption.
+ }
+ rewrite forallb_forall in FORALL.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite Pos.ltb_lt.
+ pose proof (FORALL x INx) as ALLx.
+ rewrite andb_true_iff in ALLx.
+ destruct ALLx as [ALLx1 ALLx2].
+ rewrite Pos.leb_le in ALLx1.
+ lia.
+ Qed.
+
+ Lemma transf_function_inj_starstep :
+ forall ts f tf sp m inj_n src_pc inj_pc inj_code
+ (FUN : transf_function gen_injections f = OK tf)
+ (INJ : nth_error (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n =
+ Some (src_pc, inj_code))
+ (POSITION : inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n = inj_pc)
+ (trs : regset),
+ exists trs',
+ match_regs (f : function) trs trs' /\
+ Smallstep.star RTL.step tge
+ (State ts tf sp inj_pc trs m) E0
+ (State ts tf sp (pos_add_nat inj_pc (List.length inj_code)) trs' m).
+ Proof.
+ intros.
+ replace (State ts tf sp inj_pc trs m) with (State ts tf sp (pos_add_nat inj_pc ((List.length inj_code) - (List.length inj_code))%nat) trs m).
+ eapply transf_function_inj_starstep_rec; eauto.
+ f_equal.
+ rewrite <- minus_n_n.
+ reflexivity.
+ Qed.
+
+ Lemma transf_function_inj_end :
+ forall ts f tf sp m inj_n src_pc inj_pc inj_code i
+ (FUN : transf_function gen_injections f = OK tf)
+ (INJ : nth_error (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n =
+ Some (src_pc, inj_code))
+ (SRC: (fn_code f) ! src_pc = Some i)
+ (POSITION : inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n = inj_pc)
+ (trs : regset),
+ RTL.step tge
+ (State ts tf sp (pos_add_nat inj_pc (List.length inj_code)) trs m) E0
+ (State ts tf sp (successor i) trs m).
+ Proof.
+ intros.
+ pose proof FUN as VALIDATE.
+ unfold transf_function, valid_injections1 in VALIDATE.
+ destruct forallb eqn:FORALL in VALIDATE.
+ 2: discriminate.
+ injection VALIDATE.
+ intro TF.
+ symmetry in TF.
+ pose proof (inject_l_injected_end (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) (fn_code f) inj_n src_pc i inj_code (Pos.succ (max_pc_function f))) as INJECTED.
+ lapply INJECTED.
+ 2: assumption.
+ clear INJECTED.
+ intro INJECTED.
+ lapply INJECTED.
+ 2: apply (PTree.elements_keys_norepet (gen_injections f (max_pc_function f) (max_reg_function f))); fail.
+ clear INJECTED.
+ intro INJECTED.
+ lapply INJECTED.
+ { clear INJECTED.
+ intro INJECTED.
+ pose proof (INJECTED INJ) as INJ'.
+ clear INJECTED.
+ replace (snd
+ (inject_l (fn_code f) (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))))) with (fn_code tf) in INJ'.
+ 2: rewrite TF; simpl; reflexivity.
+ rewrite POSITION in INJ'.
+ apply exec_Inop.
+ assumption.
+ }
+ clear INJECTED.
+ rewrite forallb_forall in FORALL.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite Pos.ltb_lt.
+ pose proof (FORALL x INx) as ALLx.
+ rewrite andb_true_iff in ALLx.
+ destruct ALLx as [ALLx1 ALLx2].
+ rewrite Pos.leb_le in ALLx1.
+ lia.
+ Qed.
+
+ Lemma transf_function_inj_plusstep :
+ forall ts f tf sp m inj_n src_pc inj_pc inj_code i
+ (FUN : transf_function gen_injections f = OK tf)
+ (INJ : nth_error (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n =
+ Some (src_pc, inj_code))
+ (SRC: (fn_code f) ! src_pc = Some i)
+ (POSITION : inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) inj_n = inj_pc)
+ (trs : regset),
+ exists trs',
+ match_regs (f : function) trs trs' /\
+ Smallstep.plus RTL.step tge
+ (State ts tf sp inj_pc trs m) E0
+ (State ts tf sp (successor i) trs' m).
+ Proof.
+ intros.
+ destruct (transf_function_inj_starstep ts f tf sp m inj_n src_pc inj_pc inj_code FUN INJ POSITION trs) as [trs' [MATCH PLUS]].
+ exists trs'.
+ split. assumption.
+ eapply Smallstep.plus_right.
+ exact PLUS.
+ eapply transf_function_inj_end; eassumption.
+ reflexivity.
+ Qed.
+
+ Lemma transf_function_preserves:
+ forall f tf pc
+ (FUN : transf_function gen_injections f = OK tf)
+ (LESS : pc <= max_pc_function f)
+ (NOCHANGE : (gen_injections f (max_pc_function f) (max_reg_function f)) ! pc = None),
+ (fn_code tf) ! pc = (fn_code f) ! pc.
+ Proof.
+ intros.
+ unfold transf_function in FUN.
+ destruct valid_injections1 in FUN.
+ 2: discriminate.
+ inv FUN.
+ simpl.
+ apply inject_l_preserves.
+ lia.
+ rewrite forallb_forall.
+ intros x INx.
+ destruct peq; trivial.
+ subst pc.
+ exfalso.
+ destruct x as [pc ii].
+ simpl in *.
+ apply PTree.elements_complete in INx.
+ congruence.
+ Qed.
+
+ Lemma transf_function_redirects:
+ forall f tf pc injl ii
+ (FUN : transf_function gen_injections f = OK tf)
+ (LESS : pc <= max_pc_function f)
+ (INJECTION : (gen_injections f (max_pc_function f) (max_reg_function f)) ! pc = Some injl)
+ (INSTR: (fn_code f) ! pc = Some ii),
+ exists pc' : node,
+ (fn_code tf) ! pc = Some (alter_successor ii pc') /\
+ (forall ts sp m trs,
+ exists trs',
+ match_regs f trs trs' /\
+ Smallstep.plus RTL.step tge
+ (State ts tf sp pc' trs m) E0
+ (State ts tf sp (successor ii) trs' m)).
+ Proof.
+ intros.
+ apply PTree.elements_correct in INJECTION.
+ apply In_nth_error in INJECTION.
+ destruct INJECTION as [injn INJECTION].
+ exists (inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) injn).
+ split.
+ { unfold transf_function in FUN.
+ destruct (valid_injections1) eqn:VALID in FUN.
+ 2: discriminate.
+ inv FUN.
+ simpl.
+ apply inject_l_redirects with (l := injl); auto.
+ apply PTree.elements_keys_norepet.
+ unfold valid_injections1 in VALID.
+ rewrite forallb_forall in VALID.
+ rewrite forallb_forall.
+ intros x INx.
+ pose proof (VALID x INx) as VALIDx.
+ clear VALID.
+ rewrite andb_true_iff in VALIDx.
+ rewrite Pos.leb_le in VALIDx.
+ destruct VALIDx as [VALIDx1 VALIDx2].
+ rewrite Pos.ltb_lt.
+ lia.
+ }
+ intros.
+ pose proof (transf_function_inj_plusstep ts f tf sp m injn pc
+ (inject_l_position (Pos.succ (max_pc_function f))
+ (PTree.elements (gen_injections f (max_pc_function f) (max_reg_function f))) injn)
+ injl ii FUN INJECTION INSTR) as TRANS.
+ lapply TRANS.
+ 2: reflexivity.
+ clear TRANS.
+ intro TRANS.
+ exact (TRANS trs).
+ Qed.
+
+ Lemma transf_function_preserves_uses:
+ forall f tf pc rs trs ii
+ (FUN : transf_function gen_injections f = OK tf)
+ (MATCH : match_regs f rs trs)
+ (INSTR : (fn_code f) ! pc = Some ii),
+ trs ## (instr_uses ii) = rs ## (instr_uses ii).
+ Proof.
+ intros.
+ assert (forall r, In r (instr_uses ii) ->
+ trs # r = rs # r) as SAME.
+ {
+ intros r INr.
+ apply MATCH.
+ apply (max_reg_function_use f pc ii); auto.
+ }
+ induction (instr_uses ii); simpl; trivial.
+ f_equal.
+ - apply SAME. constructor; trivial.
+ - apply IHl. intros.
+ apply SAME. right. assumption.
+ Qed.
+
+ (*
+ Lemma transf_function_preserves_builtin_arg:
+ forall rs trs ef res sp m pc'
+ (arg : builtin_arg reg)
+ (SAME : (forall r,
+ In r (instr_uses (Ibuiltin ef args res pc')) ->
+ trs # r = rs # r) )
+ varg
+ (EVAL : eval_builtin_arg ge (fun r => rs#r) sp m arg varg),
+ eval_builtin_arg ge (fun r => trs#r) sp m arg varg.
+ Proof.
+ *)
+
+ Lemma transf_function_preserves_builtin_args_rec:
+ forall rs trs ef res sp m pc'
+ (args : list (builtin_arg reg))
+ (SAME : (forall r,
+ In r (instr_uses (Ibuiltin ef args res pc')) ->
+ trs # r = rs # r) )
+ (vargs : list val)
+ (EVAL : eval_builtin_args ge (fun r => rs#r) sp m args vargs),
+ eval_builtin_args ge (fun r => trs#r) sp m args vargs.
+ Proof.
+ unfold eval_builtin_args.
+ induction args; intros; inv EVAL.
+ - constructor.
+ - constructor.
+ + induction H1.
+ all: try (constructor; auto; fail).
+ * rewrite <- SAME.
+ apply eval_BA.
+ simpl.
+ left. reflexivity.
+ * constructor.
+ ** apply IHeval_builtin_arg1.
+ intros r INr.
+ apply SAME.
+ simpl.
+ simpl in INr.
+ rewrite in_app in INr.
+ rewrite in_app.
+ rewrite in_app.
+ tauto.
+ ** apply IHeval_builtin_arg2.
+ intros r INr.
+ apply SAME.
+ simpl.
+ simpl in INr.
+ rewrite in_app in INr.
+ rewrite in_app.
+ rewrite in_app.
+ tauto.
+ * constructor.
+ ** apply IHeval_builtin_arg1.
+ intros r INr.
+ apply SAME.
+ simpl.
+ simpl in INr.
+ rewrite in_app in INr.
+ rewrite in_app.
+ rewrite in_app.
+ tauto.
+ ** apply IHeval_builtin_arg2.
+ intros r INr.
+ apply SAME.
+ simpl.
+ simpl in INr.
+ rewrite in_app in INr.
+ rewrite in_app.
+ rewrite in_app.
+ tauto.
+ + apply IHargs.
+ 2: assumption.
+ intros r INr.
+ apply SAME.
+ simpl.
+ apply in_or_app.
+ right.
+ exact INr.
+ Qed.
+
+ Lemma transf_function_preserves_builtin_args:
+ forall f tf pc rs trs ef res sp m pc'
+ (args : list (builtin_arg reg))
+ (FUN : transf_function gen_injections f = OK tf)
+ (MATCH : match_regs f rs trs)
+ (INSTR : (fn_code f) ! pc = Some (Ibuiltin ef args res pc'))
+ (vargs : list val)
+ (EVAL : eval_builtin_args ge (fun r => rs#r) sp m args vargs),
+ eval_builtin_args ge (fun r => trs#r) sp m args vargs.
+ Proof.
+ intros.
+ apply transf_function_preserves_builtin_args_rec with (rs := rs) (ef := ef) (res := res) (pc' := pc').
+ intros r INr.
+ apply MATCH.
+ apply (max_reg_function_use f pc (Ibuiltin ef args res pc')).
+ all: auto.
+ Qed.
+
+ Lemma match_regs_write:
+ forall f rs trs res v
+ (MATCH : match_regs f rs trs),
+ match_regs f (rs # res <- v) (trs # res <- v).
+ Proof.
+ intros.
+ intros r LESS.
+ destruct (peq r res).
+ {
+ subst r.
+ rewrite Regmap.gss.
+ symmetry.
+ apply Regmap.gss.
+ }
+ rewrite Regmap.gso.
+ rewrite Regmap.gso.
+ all: trivial.
+ apply MATCH.
+ trivial.
+ Qed.
+
+ Lemma match_regs_setres:
+ forall f res rs trs vres
+ (MATCH : match_regs f rs trs),
+ match_regs f (regmap_setres res vres rs) (regmap_setres res vres trs).
+ Proof.
+ induction res; simpl; intros; trivial.
+ apply match_regs_write; auto.
+ Qed.
+
+ Lemma transf_function_preserves_ros:
+ forall f tf pc rs trs ros args res fd pc' sig
+ (FUN : transf_function gen_injections f = OK tf)
+ (MATCH : match_regs f rs trs)
+ (INSTR : (fn_code f) ! pc = Some (Icall sig ros args res pc'))
+ (FIND : find_function ge ros rs = Some fd),
+ exists tfd, find_function tge ros trs = Some tfd
+ /\ transf_fundef gen_injections fd = OK tfd.
+ Proof.
+ intros; destruct ros as [r|id].
+ - apply functions_translated; auto.
+ replace (trs # r) with (hd Vundef (trs ## (instr_uses (Icall sig (inl r) args res pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ - simpl. rewrite symbols_preserved.
+ simpl in FIND.
+ destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+ Qed.
+
+ Lemma transf_function_preserves_ros_tail:
+ forall f tf pc rs trs ros args fd sig
+ (FUN : transf_function gen_injections f = OK tf)
+ (MATCH : match_regs f rs trs)
+ (INSTR : (fn_code f) ! pc = Some (Itailcall sig ros args))
+ (FIND : find_function ge ros rs = Some fd),
+ exists tfd, find_function tge ros trs = Some tfd
+ /\ transf_fundef gen_injections fd = OK tfd.
+ Proof.
+ intros; destruct ros as [r|id].
+ - apply functions_translated; auto.
+ replace (trs # r) with (hd Vundef (trs ## (instr_uses (Itailcall sig (inl r) args)))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ - simpl. rewrite symbols_preserved.
+ simpl in FIND.
+ destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+ Qed.
+
+ Theorem transf_step_correct:
+ forall s1 t s2, step ge s1 t s2 ->
+ forall ts1 (MS: match_states s1 ts1),
+ exists ts2, Smallstep.plus step tge ts1 t ts2 /\ match_states s2 ts2.
+ Proof.
+ induction 1; intros ts1 MS; inv MS; try (inv TRC).
+ - (* nop *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m) (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Inop.
+ exact ALTER.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs); assumption.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Inop.
+ rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ * constructor; trivial.
+
+ - (* op *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m)
+ (trs := trs # res <- v).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Iop with (op := op) (args := args).
+ exact ALTER.
+ rewrite eval_operation_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iop op args res pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl.
+ eassumption.
+ }
+ exact symbols_preserved.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs # res <- v); trivial.
+ apply match_regs_write.
+ assumption.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Iop with (op := op) (args := args).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** rewrite eval_operation_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iop op args res pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl.
+ eassumption.
+ }
+ exact symbols_preserved.
+ * constructor; trivial.
+ apply match_regs_write.
+ assumption.
+
+ - (* load *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m)
+ (trs := trs # dst <- v).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Iload with (trap := trap) (chunk := chunk) (addr := addr) (args := args) (a := a).
+ exact ALTER.
+ rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload trap chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ eassumption.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs # dst <- v); trivial.
+ apply match_regs_write.
+ assumption.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Iload with (trap := trap) (chunk := chunk) (addr := addr) (args := args) (a := a).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload trap chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ ** eassumption.
+ * constructor; trivial.
+ apply match_regs_write.
+ assumption.
+
+ - (* load notrap1 *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m)
+ (trs := trs # dst <- Vundef).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Iload_notrap1 with (chunk := chunk) (addr := addr) (args := args).
+ exact ALTER.
+ rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload NOTRAP chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs # dst <- Vundef); trivial.
+ apply match_regs_write.
+ assumption.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Iload_notrap1 with (chunk := chunk) (addr := addr) (args := args).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload NOTRAP chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ * constructor; trivial.
+ apply match_regs_write.
+ assumption.
+
+ - (* load notrap2 *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m)
+ (trs := trs # dst <- Vundef).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Iload_notrap2 with (chunk := chunk) (addr := addr) (args := args) (a := a).
+ exact ALTER.
+ rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload NOTRAP chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ eassumption.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs # dst <- Vundef); trivial.
+ apply match_regs_write.
+ assumption.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Iload_notrap2 with (chunk := chunk) (addr := addr) (args := args) (a := a).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace args with (instr_uses (Iload NOTRAP chunk addr args dst pc')) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ ** eassumption.
+ * constructor; trivial.
+ apply match_regs_write.
+ assumption.
+
+ - (* store *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m') (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Istore with (chunk := chunk) (addr := addr) (args := args) (a := a) (src := src).
+ exact ALTER.
+ rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace (trs ## args) with (tl (trs ## (instr_uses (Istore chunk addr args src pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ replace (trs # src) with (hd Vundef (trs ## (instr_uses (Istore chunk addr args src pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl.
+ eassumption.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := trs); trivial.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Istore with (chunk := chunk) (addr := addr) (args := args) (a := a) (src := src).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** rewrite eval_addressing_preserved with (ge1 := ge).
+ {
+ replace (trs ## args) with (tl (trs ## (instr_uses (Istore chunk addr args src pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ }
+ exact symbols_preserved.
+ ** replace (trs # src) with (hd Vundef (trs ## (instr_uses (Istore chunk addr args src pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl.
+ eassumption.
+ * constructor; trivial.
+ - (* call *)
+ destruct (transf_function_preserves_ros f tf pc rs trs ros args res fd pc' (funsig fd) FUN REGS H H0) as [tfd [TFD1 TFD2]].
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ simpl in ALTER.
+ econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Icall with (args := args) (sig := (funsig fd)) (ros := ros).
+ exact ALTER.
+ exact TFD1.
+ apply sig_preserved; auto.
+ * destruct ros as [r | id].
+ ** replace (trs ## args) with (tl (trs ## (instr_uses (Icall (funsig fd) (inl r) args res pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ constructor; auto.
+ constructor; auto.
+
+ intros.
+ destruct (SKIP ts0 sp m0 trs1) as [trs2 [MATCH PLUS]].
+ exists trs2. split. assumption.
+ apply Smallstep.plus_star. exact PLUS.
+
+ ** replace (trs ## args) with (trs ## (instr_uses (Icall (funsig fd) (inr id) args res pc'))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ constructor; auto.
+ constructor; auto.
+
+ intros.
+ destruct (SKIP ts0 sp m0 trs1) as [trs2 [MATCH PLUS]].
+ exists trs2. split. assumption.
+ apply Smallstep.plus_star. exact PLUS.
+
+ + econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Icall with (args := args) (sig := (funsig fd)) (ros := ros).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** exact TFD1.
+ ** apply sig_preserved; auto.
+ * destruct ros as [r | id].
+ ** replace (trs ## args) with (tl (trs ## (instr_uses (Icall (funsig fd) (inl r) args res pc')))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ constructor; auto.
+ constructor; auto.
+
+ intros. exists trs1. split.
+ apply match_regs_refl. constructor.
+
+ ** replace (trs ## args) with (trs ## (instr_uses (Icall (funsig fd) (inr id) args res pc'))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ constructor; auto.
+ constructor; auto.
+
+ intros. exists trs1. split.
+ apply match_regs_refl. constructor.
+
+ - (* tailcall *)
+ destruct (transf_function_preserves_ros_tail f tf pc rs trs ros args fd (funsig fd) FUN REGS H H0) as [tfd [TFD1 TFD2]].
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ simpl in ALTER.
+ econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Itailcall with (args := args) (sig := (funsig fd)) (ros := ros).
+ exact ALTER.
+ exact TFD1.
+ apply sig_preserved; auto.
+ rewrite stacksize_preserved with (f:=f) by trivial.
+ eassumption.
+ * destruct ros as [r | id].
+ ** replace (trs ## args) with (tl (trs ## (instr_uses (Itailcall (funsig fd) (inl r) args)))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ ** replace (trs ## args) with (trs ## (instr_uses (Itailcall (funsig fd) (inr id) args))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ + econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Itailcall with (args := args) (sig := (funsig fd)) (ros := ros).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** exact TFD1.
+ ** apply sig_preserved; auto.
+ ** rewrite stacksize_preserved with (f:=f) by trivial.
+ eassumption.
+ * destruct ros as [r | id].
+ ** replace (trs ## args) with (tl (trs ## (instr_uses (Itailcall (funsig fd) (inl r) args)))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+ ** replace (trs ## args) with (trs ## (instr_uses (Itailcall (funsig fd) (inr id) args))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ apply match_states_call; auto.
+
+ - (* builtin *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m')
+ (trs := (regmap_setres res vres trs)).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Ibuiltin with (ef := ef) (args := args) (res := res) (vargs := vargs).
+ *** exact ALTER.
+ *** apply eval_builtin_args_preserved with (ge1 := ge); eauto.
+ exact symbols_preserved.
+ apply transf_function_preserves_builtin_args with (f:=f) (tf:=tf) (pc:=pc) (rs:=rs) (ef:=ef) (res0:=res) (pc':=pc'); auto.
+ *** eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** symmetry. apply E0_right.
+ * constructor; trivial.
+ apply match_regs_trans with (rs2 := (regmap_setres res vres trs)); trivial.
+ apply match_regs_setres.
+ assumption.
+ + econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Ibuiltin with (ef := ef) (args := args) (res := res) (vargs := vargs).
+ ** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ ** apply eval_builtin_args_preserved with (ge1 := ge); eauto.
+ exact symbols_preserved.
+ apply transf_function_preserves_builtin_args with (f:=f) (tf:=tf) (pc:=pc) (rs:=rs) (ef:=ef) (res0:=res) (pc':=pc'); auto.
+ ** eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ * constructor; auto.
+ apply match_regs_setres.
+ assumption.
+
+ - (* cond *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + destruct b eqn:B.
+ ++ exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m) (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_left.
+ ** apply exec_Icond with (b := true) (cond := cond) (args := args) (ifso := pc_inj) (ifnot := ifnot) (predb := predb).
+ exact ALTER.
+ replace args with (instr_uses (Icond cond args ifso ifnot predb)) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl. reflexivity.
+ ** apply Smallstep.plus_star.
+ exact PLUS.
+ ** reflexivity.
+ * simpl. constructor; auto.
+ apply match_regs_trans with (rs2:=trs); auto.
+
+ ++ exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m) (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * eapply Smallstep.plus_one.
+ apply exec_Icond with (b := false) (cond := cond) (args := args) (ifso := pc_inj) (ifnot := ifnot) (predb := predb).
+ exact ALTER.
+ replace args with (instr_uses (Icond cond args ifso ifnot predb)) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ simpl. reflexivity.
+ * simpl. constructor; auto.
+ + destruct b eqn:B.
+ * econstructor; split.
+ ** eapply Smallstep.plus_one.
+ apply exec_Icond with (b := true) (cond := cond) (args := args) (ifso := ifso) (ifnot := ifnot) (predb := predb).
+ *** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ *** replace args with (instr_uses (Icond cond args ifso ifnot predb)) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ *** reflexivity.
+ ** constructor; auto.
+ * econstructor; split.
+ ** eapply Smallstep.plus_one.
+ apply exec_Icond with (b := false) (cond := cond) (args := args) (ifso := ifso) (ifnot := ifnot) (predb := predb).
+ *** rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ *** replace args with (instr_uses (Icond cond args ifso ifnot predb)) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ *** reflexivity.
+ ** constructor; auto.
+
+ - destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := sp) (m := m) (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Ijumptable with (arg := arg) (tbl := tbl) (n := n); trivial.
+ replace (trs # arg) with (hd Vundef (trs ## (instr_uses (Ijumptable arg tbl)))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ eassumption.
+ * constructor; trivial.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Ijumptable with (arg := arg) (tbl := tbl) (n := n); trivial.
+ rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ replace (trs # arg) with (hd Vundef (trs ## (instr_uses (Ijumptable arg tbl)))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ eassumption.
+ * constructor; trivial.
+ - (* return *)
+ destruct ((gen_injections f (max_pc_function f) (max_reg_function f)) ! pc) eqn:INJECTION.
+ + exploit transf_function_redirects; eauto.
+ { eapply max_pc_function_sound; eauto. }
+ intros [pc_inj [ALTER SKIP]].
+ specialize SKIP with (ts := ts) (sp := (Vptr stk Ptrofs.zero)) (m := m) (trs := trs).
+ destruct SKIP as [trs' [MATCH PLUS]].
+ econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Ireturn.
+ exact ALTER.
+ rewrite stacksize_preserved with (f:=f); eassumption.
+ * destruct or as [r | ]; simpl.
+ ** replace (trs # r) with (hd Vundef (trs ## (instr_uses (Ireturn (Some r))))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ constructor; auto.
+ ** constructor; auto.
+ + econstructor; split.
+ * apply Smallstep.plus_one.
+ apply exec_Ireturn.
+ rewrite transf_function_preserves with (f:=f); eauto.
+ eapply max_pc_function_sound; eauto.
+ rewrite stacksize_preserved with (f:=f); eassumption.
+ * destruct or as [r | ]; simpl.
+ ** replace (trs # r) with (hd Vundef (trs ## (instr_uses (Ireturn (Some r))))) by reflexivity.
+ rewrite transf_function_preserves_uses with (f := f) (tf := tf) (pc := pc) (rs := rs); trivial.
+ constructor; auto.
+ ** constructor; auto.
+
+ - (* internal call *)
+ monadInv FUN.
+ econstructor; split.
+ + apply Smallstep.plus_one.
+ apply exec_function_internal.
+ rewrite stacksize_preserved with (f:=f) by assumption.
+ eassumption.
+ + rewrite entrypoint_preserved with (f:=f)(tf:=x) by assumption.
+ constructor; auto.
+ rewrite params_preserved with (f:=f)(tf:=x) by assumption.
+ apply match_regs_refl.
+ - (* external call *)
+ monadInv FUN.
+ econstructor; split.
+ + apply Smallstep.plus_one.
+ apply exec_function_external.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + constructor; auto.
+
+ - (* return *)
+ inv STACKS. inv H1.
+ destruct (STAR bl m (trs # res <- vres)) as [trs2 [MATCH' STAR']].
+ econstructor; split.
+ + eapply Smallstep.plus_left.
+ * apply exec_return.
+ * exact STAR'.
+ * reflexivity.
+ + constructor; trivial.
+ apply match_regs_trans with (rs2 := (trs # res <- vres)).
+ apply match_regs_write.
+ assumption.
+ assumption.
+ Qed.
+
+ Theorem transf_program_correct:
+ Smallstep.forward_simulation (semantics prog) (semantics tprog).
+ Proof.
+ eapply Smallstep.forward_simulation_plus.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ eexact transf_step_correct.
+ Qed.
+
+End PRESERVATION.
+End INJECTOR.
diff --git a/backend/Inlining.v b/backend/Inlining.v
index d66d2586..317c288c 100644
--- a/backend/Inlining.v
+++ b/backend/Inlining.v
@@ -367,9 +367,9 @@ Definition expand_instr (ctx: context) (pc: node) (i: instruction): mon unit :=
| Iop op args res s =>
set_instr (spc ctx pc)
(Iop (sop ctx op) (sregs ctx args) (sreg ctx res) (spc ctx s))
- | Iload chunk addr args dst s =>
+ | Iload trap chunk addr args dst s =>
set_instr (spc ctx pc)
- (Iload chunk (saddr ctx addr) (sregs ctx args) (sreg ctx dst) (spc ctx s))
+ (Iload trap chunk (saddr ctx addr) (sregs ctx args) (sreg ctx dst) (spc ctx s))
| Istore chunk addr args src s =>
set_instr (spc ctx pc)
(Istore chunk (saddr ctx addr) (sregs ctx args) (sreg ctx src) (spc ctx s))
@@ -400,9 +400,9 @@ Definition expand_instr (ctx: context) (pc: node) (i: instruction): mon unit :=
| Ibuiltin ef args res s =>
set_instr (spc ctx pc)
(Ibuiltin ef (map (sbuiltinarg ctx) args) (sbuiltinres ctx res) (spc ctx s))
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 info =>
set_instr (spc ctx pc)
- (Icond cond (sregs ctx args) (spc ctx s1) (spc ctx s2))
+ (Icond cond (sregs ctx args) (spc ctx s1) (spc ctx s2) info)
| Ijumptable r tbl =>
set_instr (spc ctx pc)
(Ijumptable (sreg ctx r) (List.map (spc ctx) tbl))
diff --git a/backend/Inliningaux.ml b/backend/Inliningaux.ml
index 2e83eb0c..cf308962 100644
--- a/backend/Inliningaux.ml
+++ b/backend/Inliningaux.ml
@@ -17,7 +17,8 @@ open Maps
open Op
open Ordered
open! RTL
-
+open Camlcoq
+
module PSet = Make(OrderedPositive)
type inlining_info = {
@@ -57,7 +58,7 @@ let used_in_globvar io gv =
let fun_inline_analysis id io fn =
let inst io nid = function
| Iop (op, args, dest, succ) -> used_id io (globals_operation op)
- | Iload (chunk, addr, args, dest, succ)
+ | Iload (_, chunk, addr, args, dest, succ)
| Istore (chunk, addr, args, dest, succ) -> used_id io (globals_addressing addr)
| Ibuiltin (ef, args, dest, succ) -> used_id io (globals_of_builtin_args args)
| Icall (_, Coq_inr cid, _, _, _)
@@ -83,13 +84,15 @@ let static_called_once id io =
else
false
-(* To be considered: heuristics based on size of function? *)
+(* D. Monniaux: attempt at heuristic based on size *)
+let small_enough (f : coq_function) =
+ P.to_int (RTL.max_pc_function f) <= !Clflags.option_inline_auto_threshold
let should_inline (io: inlining_info) (id: ident) (f: coq_function) =
if !Clflags.option_finline then begin
match C2C.atom_inline id with
| C2C.Inline -> true
| C2C.Noinline -> false
- | C2C.No_specifier -> static_called_once id io
+ | C2C.No_specifier -> static_called_once id io || small_enough f
end else
false
diff --git a/backend/Inliningproof.v b/backend/Inliningproof.v
index 0434a4a4..eb30732b 100644
--- a/backend/Inliningproof.v
+++ b/backend/Inliningproof.v
@@ -929,6 +929,15 @@ Proof.
intros. inv H. eauto.
Qed.
+Lemma eval_addressing_none:
+ forall sp' ctx addr rs,
+ eval_addressing ge (Vptr sp' (Ptrofs.repr (dstk ctx))) addr rs = None ->
+ eval_addressing ge (Vptr sp' Ptrofs.zero) (saddr ctx addr) rs = None.
+Proof.
+ intros until rs; intro Heval.
+ destruct addr; destruct rs as [| r0 rs1]; simpl in *; trivial; discriminate.
+Qed.
+
Theorem step_simulation:
forall S1 t S2,
step ge S1 t S2 ->
@@ -976,6 +985,51 @@ Proof.
apply match_stacks_inside_set_reg; auto.
apply agree_set_reg; auto.
+- (* load notrap1 *)
+ exploit tr_funbody_inv; eauto. intros TR; inv TR.
+ left; econstructor; split.
+ eapply plus_one. eapply exec_Iload_notrap1. eassumption.
+ rewrite eval_addressing_preserved with (ge1:=ge) (ge2:=tge).
+ exploit eval_addressing_inj_none.
+ 4: eassumption.
+ intros. eapply symbol_address_inject.
+ eapply match_stacks_inside_globals; eauto.
+ eauto.
+ instantiate (1 := rs'##(sregs ctx args)). eapply agree_val_regs; eauto.
+ rewrite Ptrofs.add_zero_l.
+ apply eval_addressing_none.
+ exact symbols_preserved.
+ econstructor; eauto.
+ apply match_stacks_inside_set_reg; auto.
+ apply agree_set_reg; auto.
+
+- (* load notrap2 *)
+ exploit tr_funbody_inv; eauto. intros TR; inv TR.
+
+ exploit eval_addressing_inject.
+ eapply match_stacks_inside_globals; eauto.
+ eexact SP.
+ instantiate (2 := rs##args). instantiate (1 := rs'##(sregs ctx args)). eapply agree_val_regs; eauto.
+ eauto.
+ fold (saddr ctx addr). intros [a' [P Q]].
+
+ destruct (Mem.loadv chunk m' a') eqn:Hload'.
+ + left; econstructor; split.
+ eapply plus_one.
+ eapply exec_Iload; eauto.
+ try (rewrite <- P; apply eval_addressing_preserved; exact symbols_preserved).
+ econstructor; eauto.
+ apply match_stacks_inside_set_reg; auto.
+ apply agree_set_reg; auto.
+
+ + left; econstructor; split.
+ eapply plus_one.
+ eapply exec_Iload_notrap2; eauto.
+ try (rewrite <- P; apply eval_addressing_preserved; exact symbols_preserved).
+ econstructor; eauto.
+ apply match_stacks_inside_set_reg; auto.
+ apply agree_set_reg; auto.
+
- (* store *)
exploit tr_funbody_inv; eauto. intros TR; inv TR.
exploit eval_addressing_inject.
diff --git a/backend/Inliningspec.v b/backend/Inliningspec.v
index 477f883a..e846e0fd 100644
--- a/backend/Inliningspec.v
+++ b/backend/Inliningspec.v
@@ -270,10 +270,10 @@ Inductive tr_instr: context -> node -> instruction -> code -> Prop :=
Ple res ctx.(mreg) ->
c!(spc ctx pc) = Some (Iop (sop ctx op) (sregs ctx args) (sreg ctx res) (spc ctx s)) ->
tr_instr ctx pc (Iop op args res s) c
- | tr_load: forall ctx pc c chunk addr args res s,
+ | tr_load: forall ctx pc c trap chunk addr args res s,
Ple res ctx.(mreg) ->
- c!(spc ctx pc) = Some (Iload chunk (saddr ctx addr) (sregs ctx args) (sreg ctx res) (spc ctx s)) ->
- tr_instr ctx pc (Iload chunk addr args res s) c
+ c!(spc ctx pc) = Some (Iload trap chunk (saddr ctx addr) (sregs ctx args) (sreg ctx res) (spc ctx s)) ->
+ tr_instr ctx pc (Iload trap chunk addr args res s) c
| tr_store: forall ctx pc c chunk addr args src s,
c!(spc ctx pc) = Some (Istore chunk (saddr ctx addr) (sregs ctx args) (sreg ctx src) (spc ctx s)) ->
tr_instr ctx pc (Istore chunk addr args src s) c
@@ -312,9 +312,9 @@ Inductive tr_instr: context -> node -> instruction -> code -> Prop :=
match res with BR r => Ple r ctx.(mreg) | _ => True end ->
c!(spc ctx pc) = Some (Ibuiltin ef (map (sbuiltinarg ctx) args) (sbuiltinres ctx res) (spc ctx s)) ->
tr_instr ctx pc (Ibuiltin ef args res s) c
- | tr_cond: forall ctx pc cond args s1 s2 c,
- c!(spc ctx pc) = Some (Icond cond (sregs ctx args) (spc ctx s1) (spc ctx s2)) ->
- tr_instr ctx pc (Icond cond args s1 s2) c
+ | tr_cond: forall ctx pc cond args s1 s2 c i,
+ c!(spc ctx pc) = Some (Icond cond (sregs ctx args) (spc ctx s1) (spc ctx s2) i) ->
+ tr_instr ctx pc (Icond cond args s1 s2 i) c
| tr_jumptable: forall ctx pc r tbl c,
c!(spc ctx pc) = Some (Ijumptable (sreg ctx r) (List.map (spc ctx) tbl)) ->
tr_instr ctx pc (Ijumptable r tbl) c
diff --git a/backend/JsonAST.ml b/backend/JsonAST.ml
index 8ab874b1..2e70aae7 100644
--- a/backend/JsonAST.ml
+++ b/backend/JsonAST.ml
@@ -39,7 +39,7 @@ let pp_section pp sec =
match sec with
| Section_text -> pp_simple "Text"
- | Section_data init -> pp_complex "Data" init
+ | Section_data(init, thread_local) -> pp_complex "Data" init (* FIXME *)
| Section_small_data init -> pp_complex "Small Data" init
| Section_const init -> pp_complex "Const" init
| Section_small_const init -> pp_complex "Small Const" init
diff --git a/backend/KillUselessMoves.v b/backend/KillUselessMoves.v
new file mode 100644
index 00000000..bdd7ec60
--- /dev/null
+++ b/backend/KillUselessMoves.v
@@ -0,0 +1,40 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+Require List.
+
+Definition transf_ros (ros: reg + ident) : reg + ident := ros.
+
+Definition transf_instr (pc: node) (instr: instruction) :=
+ match instr with
+ | Iop op args res s =>
+ if (eq_operation op Omove) && (List.list_eq_dec peq args (res :: nil))
+ then Inop s
+ else instr
+ | _ => instr
+ end.
+
+Definition transf_function (f: function) : function :=
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map transf_instr f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
diff --git a/backend/KillUselessMovesproof.v b/backend/KillUselessMovesproof.v
new file mode 100644
index 00000000..629aa6aa
--- /dev/null
+++ b/backend/KillUselessMovesproof.v
@@ -0,0 +1,361 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Axioms.
+Require Import FunInd.
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import KillUselessMoves.
+
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; reflexivity.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i,
+ f.(fn_code)!pc = Some i ->
+ (transf_function f).(fn_code)!pc = Some(transf_instr pc i).
+Proof.
+ intros until i. intro Hcode.
+ unfold transf_function; simpl.
+ rewrite PTree.gmap.
+ unfold option_map.
+ rewrite Hcode.
+ reflexivity.
+Qed.
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+Section SAME_RS.
+ Context {A : Type}.
+
+ Definition same_rs (rs rs' : Regmap.t A) :=
+ forall x, rs # x = rs' # x.
+
+ Lemma same_rs_refl : forall rs, same_rs rs rs.
+ Proof.
+ unfold same_rs.
+ reflexivity.
+ Qed.
+
+ Lemma same_rs_comm : forall rs rs', (same_rs rs rs') -> (same_rs rs' rs).
+ Proof.
+ unfold same_rs.
+ congruence.
+ Qed.
+
+ Lemma same_rs_trans : forall rs1 rs2 rs3,
+ (same_rs rs1 rs2) -> (same_rs rs2 rs3) -> (same_rs rs1 rs3).
+ Proof.
+ unfold same_rs.
+ congruence.
+ Qed.
+
+ Lemma same_rs_idem_write : forall rs r,
+ (same_rs rs (rs # r <- (rs # r))).
+ Proof.
+ unfold same_rs.
+ intros.
+ rewrite Regmap.gsident.
+ reflexivity.
+ Qed.
+
+ Lemma same_rs_read:
+ forall rs rs' r, (same_rs rs rs') -> rs # r = rs' # r.
+ Proof.
+ unfold same_rs.
+ auto.
+ Qed.
+
+ Lemma same_rs_subst:
+ forall rs rs' l, (same_rs rs rs') -> rs ## l = rs' ## l.
+ Proof.
+ induction l; cbn; intuition congruence.
+ Qed.
+
+ Lemma same_rs_write: forall rs rs' r x,
+ (same_rs rs rs') -> (same_rs (rs # r <- x) (rs' # r <- x)).
+ Proof.
+ unfold same_rs.
+ intros.
+ destruct (peq r x0).
+ { subst x0.
+ rewrite Regmap.gss. rewrite Regmap.gss.
+ reflexivity.
+ }
+ rewrite Regmap.gso by congruence.
+ rewrite Regmap.gso by congruence.
+ auto.
+ Qed.
+
+ Lemma same_rs_setres:
+ forall rs rs' (SAME: same_rs rs rs') res vres,
+ same_rs (regmap_setres res vres rs) (regmap_setres res vres rs').
+ Proof.
+ induction res; cbn; auto using same_rs_write.
+ Qed.
+End SAME_RS.
+
+Lemma same_find_function: forall tge rs rs' (SAME: same_rs rs rs') ros,
+ find_function tge ros rs = find_function tge ros rs'.
+Proof.
+ destruct ros; cbn.
+ { rewrite (same_rs_read rs rs' r SAME).
+ reflexivity. }
+ reflexivity.
+Qed.
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+| match_frames_intro: forall res f sp pc rs rs' (SAME : same_rs rs rs'),
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs').
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs rs' m stk'
+ (SAME: same_rs rs rs')
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs' m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS; try TR_AT.
+- (* nop *)
+ econstructor; split. eapply exec_Inop; eauto.
+ constructor; auto.
+- (* op *)
+ cbn in H1.
+ destruct (_ && _) eqn:IS_MOVE in H1.
+ {
+ destruct eq_operation in IS_MOVE. 2: discriminate.
+ destruct list_eq_dec in IS_MOVE. 2: discriminate.
+ subst op. subst args.
+ clear IS_MOVE.
+ cbn in H0.
+ inv H0.
+ econstructor; split.
+ { eapply exec_Inop; eauto. }
+ constructor.
+ 2: assumption.
+ eapply same_rs_trans.
+ { apply same_rs_comm.
+ apply same_rs_idem_write.
+ }
+ assumption.
+ }
+ econstructor; split.
+ eapply exec_Iop with (v := v); eauto.
+ rewrite <- H0.
+ rewrite (same_rs_subst rs rs' args SAME).
+ apply eval_operation_preserved. exact symbols_preserved.
+ constructor; auto using same_rs_write.
+(* load *)
+- econstructor; split.
+ assert (eval_addressing tge sp addr rs' ## args = Some a).
+ { rewrite <- H0.
+ rewrite (same_rs_subst rs rs' args SAME).
+ apply eval_addressing_preserved. exact symbols_preserved.
+ }
+ eapply exec_Iload; eauto.
+ constructor; auto using same_rs_write.
+- (* load notrap1 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs' ## args = None).
+ { rewrite <- H0.
+ rewrite (same_rs_subst rs rs' args SAME).
+ apply eval_addressing_preserved. exact symbols_preserved.
+ }
+ eapply exec_Iload_notrap1; eauto.
+ constructor; auto using same_rs_write.
+- (* load notrap2 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs' ## args = Some a).
+ { rewrite <- H0.
+ rewrite (same_rs_subst rs rs' args SAME).
+ apply eval_addressing_preserved. exact symbols_preserved.
+ }
+ eapply exec_Iload_notrap2; eauto.
+ constructor; auto using same_rs_write.
+- (* store *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs' ## args = Some a).
+ { rewrite <- H0.
+ rewrite (same_rs_subst rs rs' args SAME).
+ apply eval_addressing_preserved. exact symbols_preserved.
+ }
+ rewrite (same_rs_read rs rs' src SAME) in H1.
+ eapply exec_Istore; eauto.
+ constructor; auto.
+(* call *)
+- econstructor; split.
+ eapply exec_Icall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ { rewrite <- (same_find_function ge rs rs') by assumption.
+ assumption. }
+ apply sig_preserved.
+ rewrite (same_rs_subst rs rs' args SAME).
+ constructor. constructor; auto. constructor; auto.
+(* tailcall *)
+- econstructor; split.
+ eapply exec_Itailcall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ { rewrite <- (same_find_function ge rs rs') by assumption.
+ assumption. }
+ apply sig_preserved.
+ rewrite (same_rs_subst rs rs' args SAME).
+ constructor. auto.
+(* builtin *)
+- econstructor; split.
+ eapply exec_Ibuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ {
+ replace (fun r : positive => rs' # r) with (fun r : positive => rs # r).
+ eassumption.
+ apply functional_extensionality.
+ auto using same_rs_read.
+ }
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+ auto using same_rs_setres.
+(* cond *)
+- econstructor; split.
+ eapply exec_Icond; eauto.
+ rewrite <- (same_rs_subst rs rs' args SAME); eassumption.
+ constructor; auto.
+(* jumptbl *)
+- econstructor; split.
+ eapply exec_Ijumptable; eauto.
+ rewrite <- (same_rs_read rs rs' arg SAME); eassumption.
+ constructor; auto.
+(* return *)
+- econstructor; split.
+ eapply exec_Ireturn; eauto.
+ destruct or; cbn.
+ + rewrite <- (same_rs_read rs rs' r SAME) by auto.
+ constructor; auto.
+ + constructor; auto.
+(* internal function *)
+- simpl. econstructor; split.
+ eapply exec_function_internal; eauto.
+ constructor; auto.
+ cbn.
+ apply same_rs_refl.
+(* external function *)
+- econstructor; split.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+(* return *)
+- inv STACKS. inv H1.
+ econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto using same_rs_write.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/LICM.v b/backend/LICM.v
new file mode 100644
index 00000000..787ce256
--- /dev/null
+++ b/backend/LICM.v
@@ -0,0 +1,21 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+Require Inject.
+
+Axiom gen_injections : function -> node -> reg -> PTree.t (list Inject.inj_instr).
+
+Definition transf_program : program -> res program :=
+ Inject.transf_program gen_injections.
diff --git a/backend/LICMaux.ml b/backend/LICMaux.ml
new file mode 100644
index 00000000..82e4629f
--- /dev/null
+++ b/backend/LICMaux.ml
@@ -0,0 +1,332 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open RTL;;
+open Camlcoq;;
+open Maps;;
+open Kildall;;
+open HashedSet;;
+open Inject;;
+open DebugPrint;;
+open RTLcommonaux;;
+
+type reg = P.t;;
+
+(** get_loop_headers moved from Duplicateaux.ml to LICMaux.ml to prevent cycle dependencies *)
+type vstate = Unvisited | Processed | Visited
+
+let rtl_successors = function
+| Itailcall _ | Ireturn _ -> []
+| Icall(_,_,_,_,n) | Ibuiltin(_,_,_,n) | Inop n | Iop (_,_,_,n)
+| Iload (_,_,_,_,_,n) | Istore (_,_,_,_,n) -> [n]
+| Icond (_,_,n1,n2,_) -> [n1; n2]
+| Ijumptable (_,ln) -> ln
+
+(** Getting loop branches with a DFS visit :
+ * Each node is either Unvisited, Visited, or Processed
+ * pre-order: node becomes Processed
+ * post-order: node becomes Visited
+ *
+ * If we come accross an edge to a Processed node, it's a loop!
+ *)
+let get_loop_backedges code entrypoint = begin
+ debug "get_loop_backedges\n";
+ let visited = ref (PTree.map (fun n i -> Unvisited) code)
+ and loop_backedge = ref (PTree.map (fun n i -> None) code)
+ in let rec dfs_visit code origin = function
+ | [] -> ()
+ | node :: ln ->
+ debug "ENTERING node %d, REM are %a\n" (P.to_int node) print_intlist ln;
+ match (get_some @@ PTree.get node !visited) with
+ | Visited -> begin
+ debug "\tNode %d is already Visited, skipping\n" (P.to_int node);
+ dfs_visit code origin ln
+ end
+ | Processed -> begin
+ debug "Node %d is a loop header\n" (P.to_int node);
+ debug "The backedge is from %d\n" (P.to_int @@ get_some origin);
+ loop_backedge := PTree.set node origin !loop_backedge;
+ visited := PTree.set node Visited !visited;
+ dfs_visit code origin ln
+ end
+ | Unvisited -> begin
+ visited := PTree.set node Processed !visited;
+ debug "Node %d is Processed\n" (P.to_int node);
+ (match PTree.get node code with
+ | None -> failwith "No such node"
+ | Some i -> let next_visits = rtl_successors i in begin
+ debug "About to visit: %a\n" print_intlist next_visits;
+ dfs_visit code (Some node) next_visits
+ end);
+ debug "Node %d is Visited!\n" (P.to_int node);
+ visited := PTree.set node Visited !visited;
+ dfs_visit code origin ln
+ end
+ in begin
+ dfs_visit code None [entrypoint];
+ debug "LOOP BACKEDGES: %a\n" print_ptree_opint !loop_backedge;
+ !loop_backedge
+ end
+end
+
+let get_loop_headers code entrypoint =
+ let backedges = get_loop_backedges code entrypoint in
+ PTree.map (fun _ ob ->
+ match ob with
+ | None -> false
+ | Some _ -> true
+ ) backedges
+
+module Dominator =
+ struct
+ type t = Unreachable | Dominated of int | Multiple
+ let bot = Unreachable and top = Multiple
+ let beq a b =
+ match a, b with
+ | Unreachable, Unreachable
+ | Multiple, Multiple -> true
+ | (Dominated x), (Dominated y) -> x = y
+ | _ -> false
+ let lub a b =
+ match a, b with
+ | Multiple, _
+ | _, Multiple -> Multiple
+ | Unreachable, x
+ | x, Unreachable -> x
+ | (Dominated x), (Dominated y) when x=y -> a
+ | (Dominated _), (Dominated _) -> Multiple
+
+ let pp oc = function
+ | Unreachable -> output_string oc "unreachable"
+ | Multiple -> output_string oc "multiple"
+ | Dominated x -> Printf.fprintf oc "%d" x;;
+ end
+
+module Dominator_Solver = Dataflow_Solver(Dominator)(NodeSetForward)
+
+let apply_dominator (is_marked : node -> bool) (pc : node)
+ (before : Dominator.t) : Dominator.t =
+ match before with
+ | Dominator.Unreachable -> before
+ | _ ->
+ if is_marked pc
+ then Dominator.Dominated (P.to_int pc)
+ else before;;
+
+let dominated_parts1 (f : coq_function) :
+ (bool PTree.t) * (Dominator.t PMap.t option) =
+ let headers = get_loop_headers f.fn_code f.fn_entrypoint in
+ let dominated = Dominator_Solver.fixpoint f.fn_code RTL.successors_instr
+ (apply_dominator (fun pc -> match PTree.get pc headers with
+ | Some x -> x
+ | None -> false)) f.fn_entrypoint
+ Dominator.top in
+ (headers, dominated);;
+
+let dominated_parts (f : coq_function) : Dominator.t PMap.t * PSet.t PTree.t =
+ let (headers, dominated) = dominated_parts1 f in
+ match dominated with
+ | None -> failwith "dominated_parts 1"
+ | Some dominated ->
+ let singletons =
+ PTree.fold (fun before pc flag ->
+ if flag
+ then PTree.set pc (PSet.add pc PSet.empty) before
+ else before) headers PTree.empty in
+ (dominated,
+ PTree.fold (fun before pc ii ->
+ match PMap.get pc dominated with
+ | Dominator.Dominated x ->
+ let px = P.of_int x in
+ (match PTree.get px before with
+ | None -> failwith "dominated_parts 2"
+ | Some old ->
+ PTree.set px (PSet.add pc old) before)
+ | _ -> before) f.fn_code singletons);;
+
+let graph_traversal (initial_node : P.t)
+ (successor_iterator : P.t -> (P.t -> unit) -> unit) : PSet.t =
+ let seen = ref PSet.empty
+ and stack = Stack.create () in
+ Stack.push initial_node stack;
+ while not (Stack.is_empty stack)
+ do
+ let vertex = Stack.pop stack in
+ if not (PSet.contains !seen vertex)
+ then
+ begin
+ seen := PSet.add vertex !seen;
+ successor_iterator vertex (fun x -> Stack.push x stack)
+ end
+ done;
+ !seen;;
+
+let filter_dominated_part (predecessors : P.t list PTree.t)
+ (header : P.t) (dominated_part : PSet.t) =
+ graph_traversal header
+ (fun (vertex : P.t) (f : P.t -> unit) ->
+ match PTree.get vertex predecessors with
+ | None -> ()
+ | Some l ->
+ List.iter
+ (fun x ->
+ if PSet.contains dominated_part x
+ then f x) l
+ );;
+
+let inner_loops (f : coq_function) =
+ let (dominated, parts) = dominated_parts f
+ and predecessors = Kildall.make_predecessors f.fn_code RTL.successors_instr in
+ (dominated, predecessors, PTree.map (filter_dominated_part predecessors) parts);;
+
+let map_reg mapper r =
+ match PTree.get r mapper with
+ | None -> r
+ | Some x -> x;;
+
+let rewrite_loop_body (last_alloc : reg ref)
+ (insns : RTL.code) (header : P.t) (loop_body : PSet.t) =
+ let seen = ref PSet.empty
+ and stack = Stack.create ()
+ and rewritten = ref [] in
+ let add_inj ii = rewritten := ii::!rewritten in
+ Stack.push (header, PTree.empty) stack;
+ while not (Stack.is_empty stack)
+ do
+ let (pc, mapper) = Stack.pop stack in
+ if not (PSet.contains !seen pc)
+ then
+ begin
+ seen := PSet.add pc !seen;
+ match PTree.get pc insns with
+ | None -> ()
+ | Some ii ->
+ let mapper' =
+ match ii with
+ | Iop(op, args, res, pc') when not (Op.is_trapping_op op) ->
+ let new_res = P.succ !last_alloc in
+ last_alloc := new_res;
+ add_inj (INJop(op,
+ (List.map (map_reg mapper) args),
+ new_res));
+ PTree.set res new_res mapper
+ | Iload(_, chunk, addr, args, v, pc')
+ | Istore(chunk, addr, args, v, pc')
+ when Archi.has_notrap_loads &&
+ !Clflags.option_fnontrap_loads ->
+ let new_res = P.succ !last_alloc in
+ last_alloc := new_res;
+ add_inj (INJload(chunk, addr,
+ (List.map (map_reg mapper) args),
+ new_res));
+ PTree.set v new_res mapper
+ | _ -> mapper in
+ List.iter (fun x ->
+ if PSet.contains loop_body x
+ then Stack.push (x, mapper') stack)
+ (successors_instr ii)
+ end
+ done;
+ List.rev !rewritten;;
+
+let pp_inj_instr (oc : out_channel) (ii : inj_instr) =
+ match ii with
+ | INJnop -> output_string oc "nop"
+ | INJop(op, args, res) ->
+ Printf.fprintf oc "%a = %a"
+ PrintRTL.reg res (PrintOp.print_operation PrintRTL.reg) (op, args)
+ | INJload(chunk, addr, args, dst) ->
+ Printf.fprintf oc "%a = %s[%a]"
+ PrintRTL.reg dst (PrintAST.name_of_chunk chunk)
+ (PrintOp.print_addressing PrintRTL.reg) (addr, args);;
+
+let pp_inj_list (oc : out_channel) (l : inj_instr list) =
+ List.iter (Printf.fprintf oc "%a; " pp_inj_instr) l;;
+
+let pp_injections (oc : out_channel) (injections : inj_instr list PTree.t) =
+ List.iter
+ (fun (pc, injl) ->
+ Printf.fprintf oc "%d : %a\n" (P.to_int pc) pp_inj_list injl)
+ (PTree.elements injections);;
+
+let compute_injections1 (f : coq_function) =
+ let (dominated, predecessors, loop_bodies) = inner_loops f
+ and last_alloc = ref (max_reg_function f) in
+ (dominated, predecessors,
+ PTree.map (fun header body ->
+ (body, rewrite_loop_body last_alloc f.fn_code header body)) loop_bodies);;
+
+let compute_injections (f : coq_function) : inj_instr list PTree.t =
+ let (dominated, predecessors, injections) = compute_injections1 f in
+ let output_map = ref PTree.empty in
+ List.iter
+ (fun (header, (body, inj)) ->
+ match PTree.get header predecessors with
+ | None -> failwith "compute_injections"
+ | Some l ->
+ List.iter (fun predecessor ->
+ if (PMap.get predecessor dominated)<>Dominator.Unreachable &&
+ not (PSet.contains body predecessor)
+ then output_map := PTree.set predecessor inj !output_map) l)
+ (PTree.elements injections);
+ !output_map;;
+
+let pp_list pp_item oc l =
+ output_string oc "{ ";
+ let first = ref true in
+ List.iter (fun x ->
+ (if !first
+ then first := false
+ else output_string oc ", ");
+ pp_item oc x) l;
+ output_string oc " }";;
+
+let pp_pset oc s =
+ pp_list (fun oc -> Printf.fprintf oc "%d") oc
+ (List.sort (fun x y -> y - x) (List.map P.to_int (PSet.elements s)));;
+
+let print_dominated_parts oc f =
+ List.iter (fun (header, nodes) ->
+ Printf.fprintf oc "%d : %a\n" (P.to_int header) pp_pset nodes)
+ (PTree.elements (snd (dominated_parts f)));;
+
+let print_inner_loops oc f =
+ List.iter (fun (header, nodes) ->
+ Printf.fprintf oc "%d : %a\n" (P.to_int header) pp_pset nodes)
+ (PTree.elements (let (_,_,l) = (inner_loops f) in l));;
+
+let print_dominated_parts1 oc f =
+ match snd (dominated_parts1 f) with
+ | None -> output_string oc "error\n"
+ | Some parts ->
+ List.iter
+ (fun (pc, instr) ->
+ Printf.fprintf oc "%d : %a\n" (P.to_int pc) Dominator.pp
+ (PMap.get pc parts)
+ )
+ (PTree.elements f.fn_code);;
+
+let loop_headers (f : coq_function) : RTL.node list =
+ List.map fst (List.filter snd (PTree.elements (get_loop_headers f.fn_code f.fn_entrypoint)));;
+
+let print_loop_headers f =
+ print_endline "Loop headers";
+ List.iter
+ (fun i -> Printf.printf "%d " (P.to_int i))
+ (loop_headers f);
+ print_newline ();;
+
+let gen_injections (f : coq_function) (coq_max_pc : node) (coq_max_reg : reg):
+ (Inject.inj_instr list) PTree.t =
+ let injections = compute_injections f in
+ (* let () = pp_injections stdout injections in *)
+ injections;;
diff --git a/backend/LICMproof.v b/backend/LICMproof.v
new file mode 100644
index 00000000..e3f0c2b8
--- /dev/null
+++ b/backend/LICMproof.v
@@ -0,0 +1,39 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+Require Import LICM.
+Require Injectproof.
+
+Definition match_prog : program -> program -> Prop :=
+ Injectproof.match_prog gen_injections.
+
+Section PRESERVATION.
+
+ Variables prog tprog: program.
+ Hypothesis TRANSF: match_prog prog tprog.
+
+ Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+ Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+ Qed.
+
+ Theorem transf_program_correct :
+ Smallstep.forward_simulation (semantics prog) (semantics tprog).
+ Proof.
+ apply Injectproof.transf_program_correct with (gen_injections := gen_injections).
+ exact TRANSF.
+ Qed.
+End PRESERVATION.
diff --git a/backend/LTL.v b/backend/LTL.v
index 5e7eec8c..a382ef0e 100644
--- a/backend/LTL.v
+++ b/backend/LTL.v
@@ -29,7 +29,7 @@ Definition node := positive.
Inductive instruction: Type :=
| Lop (op: operation) (args: list mreg) (res: mreg)
- | Lload (chunk: memory_chunk) (addr: addressing) (args: list mreg) (dst: mreg)
+ | Lload (trap : trapping_mode) (chunk: memory_chunk) (addr: addressing) (args: list mreg) (dst: mreg)
| Lgetstack (sl: slot) (ofs: Z) (ty: typ) (dst: mreg)
| Lsetstack (src: mreg) (sl: slot) (ofs: Z) (ty: typ)
| Lstore (chunk: memory_chunk) (addr: addressing) (args: list mreg) (src: mreg)
@@ -37,7 +37,7 @@ Inductive instruction: Type :=
| Ltailcall (sg: signature) (ros: mreg + ident)
| Lbuiltin (ef: external_function) (args: list (builtin_arg loc)) (res: builtin_res mreg)
| Lbranch (s: node)
- | Lcond (cond: condition) (args: list mreg) (s1 s2: node)
+ | Lcond (cond: condition) (args: list mreg) (s1 s2: node) (info: option bool)
| Ljumptable (arg: mreg) (tbl: list node)
| Lreturn.
@@ -209,11 +209,24 @@ Inductive step: state -> trace -> state -> Prop :=
rs' = Locmap.set (R res) v (undef_regs (destroyed_by_op op) rs) ->
step (Block s f sp (Lop op args res :: bb) rs m)
E0 (Block s f sp bb rs' m)
- | exec_Lload: forall s f sp chunk addr args dst bb rs m a v rs',
+ | exec_Lload: forall s f sp trap chunk addr args dst bb rs m a v rs',
eval_addressing ge sp addr (reglist rs args) = Some a ->
Mem.loadv chunk m a = Some v ->
rs' = Locmap.set (R dst) v (undef_regs (destroyed_by_load chunk addr) rs) ->
- step (Block s f sp (Lload chunk addr args dst :: bb) rs m)
+ step (Block s f sp (Lload trap chunk addr args dst :: bb) rs m)
+ E0 (Block s f sp bb rs' m)
+ | exec_Lload_notrap1: forall s f sp chunk addr args dst bb rs m rs',
+ eval_addressing ge sp addr (reglist rs args) = None ->
+ rs' = Locmap.set (R dst) Vundef
+ (undef_regs (destroyed_by_load chunk addr) rs) ->
+ step (Block s f sp (Lload NOTRAP chunk addr args dst :: bb) rs m)
+ E0 (Block s f sp bb rs' m)
+ | exec_Lload_notrap2: forall s f sp chunk addr args dst bb rs m a rs',
+ eval_addressing ge sp addr (reglist rs args) = Some a ->
+ Mem.loadv chunk m a = None ->
+ rs' = Locmap.set (R dst) Vundef
+ (undef_regs (destroyed_by_load chunk addr) rs) ->
+ step (Block s f sp (Lload NOTRAP chunk addr args dst :: bb) rs m)
E0 (Block s f sp bb rs' m)
| exec_Lgetstack: forall s f sp sl ofs ty dst bb rs m rs',
rs' = Locmap.set (R dst) (rs (S sl ofs ty)) (undef_regs (destroyed_by_getstack sl) rs) ->
@@ -250,11 +263,11 @@ Inductive step: state -> trace -> state -> Prop :=
| exec_Lbranch: forall s f sp pc bb rs m,
step (Block s f sp (Lbranch pc :: bb) rs m)
E0 (State s f sp pc rs m)
- | exec_Lcond: forall s f sp cond args pc1 pc2 bb rs b pc rs' m,
+ | exec_Lcond: forall s f sp cond args pc1 pc2 bb rs b pc rs' m i,
eval_condition cond (reglist rs args) m = Some b ->
pc = (if b then pc1 else pc2) ->
rs' = undef_regs (destroyed_by_cond cond) rs ->
- step (Block s f sp (Lcond cond args pc1 pc2 :: bb) rs m)
+ step (Block s f sp (Lcond cond args pc1 pc2 i :: bb) rs m)
E0 (State s f sp pc rs' m)
| exec_Ljumptable: forall s f sp arg tbl bb rs m n pc rs',
rs (R arg) = Vint n ->
@@ -315,7 +328,7 @@ Fixpoint successors_block (b: bblock) : list node :=
| nil => nil (**r should never happen *)
| Ltailcall _ _ :: _ => nil
| Lbranch s :: _ => s :: nil
- | Lcond _ _ s1 s2 :: _ => s1 :: s2 :: nil
+ | Lcond _ _ s1 s2 _ :: _ => s1 :: s2 :: nil
| Ljumptable _ tbl :: _ => tbl
| Lreturn :: _ => nil
| instr :: b' => successors_block b'
diff --git a/backend/LTLTunneling.v b/backend/LTLTunneling.v
new file mode 100644
index 00000000..4b404724
--- /dev/null
+++ b/backend/LTLTunneling.v
@@ -0,0 +1,167 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Branch tunneling (optimization of branches to branches). *)
+
+Require Import Coqlib Maps Errors.
+Require Import AST.
+Require Import LTL.
+
+(** Branch tunneling shortens sequences of branches (with no intervening
+ computations) by rewriting the branch and conditional branch instructions
+ so that they jump directly to the end of the branch sequence.
+ For example:
+<<
+ L1: if (cond) nop L2; L1: nop L3;
+ L2: nop L3; becomes L2: nop L3;
+ L3: instr; L3: instr;
+ L4: if (cond) goto L1; L4: if (cond) nop L1;
+>>
+ This optimization can be applied to several of our intermediate
+ languages. We choose to perform it on the [LTL] language,
+ after register allocation but before code linearization.
+ Register allocation can delete instructions (such as dead
+ computations or useless moves), therefore there are more
+ opportunities for tunneling after allocation than before.
+ Symmetrically, prior tunneling helps linearization to produce
+ better code, e.g. by revealing that some [branch] instructions are
+ dead code (as the "branch L3" in the example above).
+*)
+
+(** The implementation consists in two passes: the first pass
+ records the branch t of each "nop"
+ and the second pass replace any "nop" node to [pc]
+ by a branch to a "nop" at [branch_t f pc]
+
+Naively, we may define [branch_t f pc] as follows:
+<<
+ branch_t f pc = branch_t f pc' if f(pc) = nop pc'
+ = pc otherwise
+>>
+ However, this definition can fail to terminate if
+ the program can contain loops consisting only of branches, as in
+<<
+ L1: branch L1;
+>>
+ or
+<<
+ L1: nop L2;
+ L2: nop L1;
+>>
+ Coq warns us of this fact by not accepting the definition
+ of [branch_t] above.
+
+ To handle this problem, we use a union-find data structure, adding equalities [pc = pc']
+ for every instruction [pc: nop pc'] in the function.
+
+ Moreover, because the elimination of "useless" [Lcond] depends on the current [uf] datastructure,
+ we need to iterate until we reach a fixpoint.
+
+ Actually, it is simpler and more efficient to perform this in an external oracle, that also returns a measure
+ in order to help the proof.
+
+ A verifier checks that this data-structure is correct.
+*)
+
+Definition UF := PTree.t (node * Z).
+
+(* The oracle returns a map of "nop" node to their target with a distance (ie the number of the "nop" node on the path) to the target. *)
+Axiom branch_target: LTL.function -> UF.
+Extract Constant branch_target => "LTLTunnelingaux.branch_target".
+
+Local Open Scope error_monad_scope.
+
+Definition get (td: UF) pc:node*Z :=
+ match td!pc with
+ | Some (t,d) => (t,Z.abs d)
+ | _ => (pc,0)
+ end.
+
+Definition target (td: UF) (pc:node): node := fst (get td pc).
+Coercion target: UF >-> Funclass.
+
+(* we check that the domain of [td] is included in the domain of [c] *)
+Definition check_included (td: UF) (c: code): option bblock
+ := PTree.fold (fun (ok:option bblock) pc _ => if ok then c!pc else None) td (Some nil).
+
+(* we check the validity of targets and their bound:
+ the distance of a "nop" node (w.r.t to the target) must be greater than the one of its parents.
+*)
+Definition check_bblock (td: UF) (pc:node) (bb: bblock): res unit
+ := match td!pc with
+ | None => OK tt
+ | Some (tpc, dpc) =>
+ let dpc := Z.abs dpc in
+ match bb with
+ | Lbranch s ::_ =>
+ let (ts, ds) := get td s in
+ if peq tpc ts then
+ if zlt ds dpc then OK tt
+ else Error (msg "bad distance in Lbranch")
+ else Error (msg "invalid skip of Lbranch")
+ | Lcond _ _ s1 s2 _ :: _ =>
+ let (ts1, ds1) := get td s1 in
+ let (ts2, ds2) := get td s2 in
+ if peq tpc ts1 then
+ if peq tpc ts2 then
+ if zlt ds1 dpc then
+ if zlt ds2 dpc then OK tt
+ else Error (msg "bad distance on else branch")
+ else Error (msg "bad distance on then branch")
+ else Error (msg "invalid skip of else branch")
+ else Error (msg "invalid skip of then branch")
+ | _ => Error (msg "cannot skip this block")
+ end
+ end.
+
+Definition check_code (td: UF) (c:code): res unit
+ := PTree.fold (fun ok pc bb => do _ <- ok; check_bblock td pc bb) c (OK tt).
+
+(** The second pass rewrites all LTL instructions, replacing every
+ successor [s] of every instruction by [t s], the canonical representative
+ of its equivalence class in the union-find data structure. *)
+
+Definition tunnel_instr (t: node -> node) (i: instruction) : instruction :=
+ match i with
+ | Lbranch s => Lbranch (t s)
+ | Lcond cond args s1 s2 info =>
+ let s1' := t s1 in let s2' := t s2 in
+ if peq s1' s2'
+ then Lbranch s1'
+ else Lcond cond args s1' s2' info
+ | Ljumptable arg tbl => Ljumptable arg (List.map t tbl)
+ | _ => i
+ end.
+
+Definition tunnel_block (t: node -> node) (b: bblock) : bblock :=
+ List.map (tunnel_instr t) b.
+
+Definition tunnel_function (f: LTL.function) : res LTL.function :=
+ let td := branch_target f in
+ let c := (fn_code f) in
+ if check_included td c then
+ do _ <- check_code td c ; OK
+ (mkfunction
+ (fn_sig f)
+ (fn_stacksize f)
+ (PTree.map1 (tunnel_block td) c)
+ (td (fn_entrypoint f)))
+ else
+ Error (msg "Some node of the union-find is not in the CFG")
+ .
+
+Definition tunnel_fundef (f: fundef) : res fundef :=
+ transf_partial_fundef tunnel_function f.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program tunnel_fundef p.
diff --git a/backend/LTLTunnelingaux.ml b/backend/LTLTunnelingaux.ml
new file mode 100644
index 00000000..66540bc1
--- /dev/null
+++ b/backend/LTLTunnelingaux.ml
@@ -0,0 +1,109 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Pierre Goutagny ENS-Lyon, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+
+This file implements the [branch_target] oracle that identifies "nop" branches in a LTL function,
+and computes their target node with the distance (ie the number of cummulated nops) toward this target.
+
+See [LTLTunneling.v]
+
+*)
+
+open Coqlib
+open LTL
+open Maps
+open Camlcoq
+open Tunnelinglibs
+
+module LANG = struct
+ type code_unit = LTL.bblock
+ type funct = LTL.coq_function
+end
+
+module OPT = struct
+ let langname = "LTL"
+ let limit_tunneling = None
+ let debug_flag = ref false
+ let final_dump = false
+end
+
+module Partial = Tunnelinglibs.Tunneling(LANG)(OPT)
+
+module FUNS = struct
+ let build_simplified_cfg c acc pc bb =
+ match bb with
+ | Lbranch s :: _ ->
+ let ns = get_node c s in
+ set_branch c pc ns;
+ acc
+ | Lcond (_, _, s1, s2, _) :: _ ->
+ c.num_rems <- c.num_rems + 1;
+ let ns1 = get_node c s1 in
+ let ns2 = get_node c s2 in
+ let npc = get_node c pc in
+ npc.inst <- COND(ns1, ns2);
+ npc::acc
+ | _ -> acc
+
+ let print_code_unit c println (pc, bb) =
+ match bb with
+ | Lbranch s::_ -> (if println then Partial.debug "\n"); Partial.debug "%d:Lbranch %d %s\n" pc (P.to_int s) (string_of_labeli c.nodes pc); false
+ | Lcond (_, _, s1, s2, _)::_ -> (if println then Partial.debug "\n"); Partial.debug "%d:Lcond (%d,%d) %s\n" pc (P.to_int s1) (P.to_int s2) (string_of_labeli c.nodes pc); false
+ | _ -> Partial.debug "%d " pc; true
+
+ let fn_code f = f.fn_code
+ let fn_entrypoint f = f.fn_entrypoint
+
+
+ (*************************************************************)
+ (* Copy-paste of the extracted code of the verifier *)
+ (* with [raise (BugOnPC (P.to_int pc))] instead of [Error.*] *)
+
+ let check_code_unit td pc bb =
+ match PTree.get pc td with
+ | Some p ->
+ let (tpc, dpc) = p in
+ let dpc0 = dpc in
+ (match bb with
+ | [] ->
+ raise (BugOnPC (P.to_int pc))
+ | i :: _ ->
+ (match i with
+ | Lbranch s ->
+ let (ts, ds) = get td s in
+ if peq tpc ts
+ then if zlt ds dpc0
+ then ()
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ | Lcond (_, _, s1, s2, _) ->
+ let (ts1, ds1) = get td s1 in
+ let (ts2, ds2) = get td s2 in
+ if peq tpc ts1
+ then if peq tpc ts2
+ then if zlt ds1 dpc0
+ then if zlt ds2 dpc0
+ then ()
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ | _ ->
+ raise (BugOnPC (P.to_int pc))))
+ | None -> ()
+end
+
+module T = Partial.T(FUNS)
+let branch_target = T.branch_target
+
diff --git a/backend/LTLTunnelingproof.v b/backend/LTLTunnelingproof.v
new file mode 100644
index 00000000..d36d3c76
--- /dev/null
+++ b/backend/LTLTunnelingproof.v
@@ -0,0 +1,666 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Correctness proof for the branch tunneling optimization. *)
+
+Require Import Coqlib Maps Errors.
+Require Import AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations LTL.
+Require Import LTLTunneling.
+
+Local Open Scope nat.
+
+
+(** * Properties of the branch_target, when the verifier succeeds *)
+
+Definition check_included_spec (c:code) (td:UF) (ok: option bblock) :=
+ ok <> None -> forall pc, c!pc = None -> td!pc = None.
+
+Lemma check_included_correct (td: UF) (c: code):
+ check_included_spec c td (check_included td c).
+Proof.
+ apply PTree_Properties.fold_rec with (P := check_included_spec c).
+- (* extensionality *)
+ unfold check_included_spec. intros m m' a EQ IND X pc. rewrite <- EQ; auto.
+- (* base case *)
+ intros _ pc. rewrite PTree.gempty; try congruence.
+- (* inductive case *)
+ unfold check_included_spec.
+ intros m [|] pc bb NEW ATPC IND; simpl; try congruence.
+ intros H pc0. rewrite PTree.gsspec; destruct (peq _ _); subst; simpl; try congruence.
+ intros; eapply IND; try congruence.
+Qed.
+
+Inductive target_bounds (target: node -> node) (bound: node -> nat) (pc: node): (option bblock) -> Prop :=
+ | TB_default (TB: target pc = pc) ob
+ : target_bounds target bound pc ob
+ | TB_branch s bb
+ (EQ: target pc = target s)
+ (DECREASE: bound s < bound pc)
+ : target_bounds target bound pc (Some (Lbranch s::bb))
+ | TB_cond cond args s1 s2 info bb
+ (EQ1: target pc = target s1)
+ (EQ2: target pc = target s2)
+ (DEC1: bound s1 < bound pc)
+ (DEC2: bound s2 < bound pc)
+ : target_bounds target bound pc (Some (Lcond cond args s1 s2 info::bb))
+ .
+Local Hint Resolve TB_default: core.
+
+Lemma target_None (td:UF) (pc: node): td!pc = None -> td pc = pc.
+Proof.
+ unfold target, get. intros H; rewrite H; auto.
+Qed.
+Local Hint Resolve target_None Z.abs_nonneg: core.
+
+Lemma get_nonneg td pc t d: get td pc = (t, d) -> (0 <= d)%Z.
+Proof.
+ unfold get. destruct (td!_) as [(t0&d0)|]; intros H; inversion H; subst; simpl; lia || auto.
+Qed.
+Local Hint Resolve get_nonneg: core.
+
+Definition bound (td: UF) (pc: node) := Z.to_nat (snd (get td pc)).
+
+Lemma check_bblock_correct (td:UF) (pc:node) (bb: bblock):
+ check_bblock td pc bb = OK tt ->
+ target_bounds (target td) (bound td) pc (Some bb).
+Proof.
+ unfold check_bblock, bound.
+ destruct (td!pc) as [(tpc&dpc)|] eqn:Hpc; auto.
+ assert (Tpc: td pc = tpc). { unfold target, get; rewrite Hpc; simpl; auto. }
+ assert (Dpc: snd (get td pc) = Z.abs dpc). { unfold get; rewrite Hpc; simpl; auto. }
+ destruct bb as [|[ ] bb]; simpl; try congruence.
+ + destruct (get td s) as (ts, ds) eqn:Hs.
+ repeat (destruct (peq _ _) || destruct (zlt _ _)); simpl; try congruence.
+ intros; apply TB_branch.
+ * rewrite Tpc. unfold target; rewrite Hs; simpl; auto.
+ * rewrite Dpc, Hs; simpl. apply Z2Nat.inj_lt; eauto.
+ + destruct (get td s1) as (ts1, ds1) eqn:Hs1.
+ destruct (get td s2) as (ts2, ds2) eqn:Hs2.
+ repeat (destruct (peq _ _) || destruct (zlt _ _)); simpl; try congruence.
+ intros; apply TB_cond.
+ * rewrite Tpc. unfold target; rewrite Hs1; simpl; auto.
+ * rewrite Tpc. unfold target; rewrite Hs2; simpl; auto.
+ * rewrite Dpc, Hs1; simpl. apply Z2Nat.inj_lt; eauto.
+ * rewrite Dpc, Hs2; simpl. apply Z2Nat.inj_lt; eauto.
+Qed.
+
+Definition check_code_spec (td:UF) (c:code) (ok: res unit) :=
+ ok = OK tt -> forall pc bb, c!pc = Some bb -> target_bounds (target td) (bound td) pc (Some bb).
+
+Lemma check_code_correct (td:UF) c:
+ check_code_spec td c (check_code td c).
+Proof.
+ apply PTree_Properties.fold_rec with (P := check_code_spec td).
+- (* extensionality *)
+ unfold check_code_spec. intros m m' a EQ IND X pc bb; subst. rewrite <- ! EQ; eauto.
+- (* base case *)
+ intros _ pc. rewrite PTree.gempty; try congruence.
+- (* inductive case *)
+ unfold check_code_spec.
+ intros m [[]|] pc bb NEW ATPC IND; simpl; try congruence.
+ intros H pc0 bb0. rewrite PTree.gsspec; destruct (peq _ _); subst; simpl; auto.
+ intros X; inversion X; subst.
+ apply check_bblock_correct; auto.
+Qed.
+
+Theorem branch_target_bounds:
+ forall f tf pc,
+ tunnel_function f = OK tf ->
+ target_bounds (branch_target f) (bound (branch_target f)) pc (f.(fn_code)!pc).
+Proof.
+ unfold tunnel_function; intros f f' pc.
+ destruct (check_included _ _) eqn:H1; try congruence.
+ destruct (check_code _ _) as [[]|] eqn:H2; simpl; try congruence.
+ intros _.
+ destruct ((fn_code f)!pc) eqn:X.
+ - exploit check_code_correct; eauto.
+ - exploit check_included_correct; eauto.
+ congruence.
+Qed.
+
+Lemma tunnel_function_unfold:
+ forall f tf pc,
+ tunnel_function f = OK tf ->
+ (fn_code tf)!pc = option_map (tunnel_block (branch_target f)) (fn_code f)!pc.
+Proof.
+ unfold tunnel_function; intros f f' pc.
+ destruct (check_included _ _) eqn:H1; try congruence.
+ destruct (check_code _ _) as [[]|] eqn:H2; simpl; try congruence.
+ intros X; inversion X; clear X; subst.
+ simpl. rewrite PTree.gmap1. auto.
+Qed.
+
+Lemma tunnel_fundef_Internal:
+ forall f tf, tunnel_fundef (Internal f) = OK tf
+ -> exists tf', tunnel_function f = OK tf' /\ tf = Internal tf'.
+Proof.
+ intros f tf; simpl.
+ destruct (tunnel_function f) eqn:X; simpl; try congruence.
+ intros EQ; inversion EQ.
+ eexists; split; eauto.
+Qed.
+
+Lemma tunnel_fundef_External:
+ forall tf ef, tunnel_fundef (External ef) = OK tf
+ -> tf = External ef.
+Proof.
+ intros tf ef; simpl. intros H; inversion H; auto.
+Qed.
+
+(** * Preservation of semantics *)
+
+Definition match_prog (p tp: program) :=
+ match_program (fun _ f tf => tunnel_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall (v: val) (f: fundef),
+ Genv.find_funct ge v = Some f ->
+ exists tf, tunnel_fundef f = OK tf /\ Genv.find_funct tge v = Some tf.
+Proof.
+ intros. exploit (Genv.find_funct_match TRANSL); eauto.
+ intros (cu & tf & A & B & C).
+ repeat eexists; intuition eauto.
+Qed.
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ tunnel_fundef f = OK tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
+Qed.
+
+Lemma symbols_preserved s: Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof.
+ rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
+Qed.
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof.
+ eapply (Genv.senv_match TRANSL).
+Qed.
+
+Lemma sig_preserved:
+ forall f tf, tunnel_fundef f = OK tf -> funsig tf = funsig f.
+Proof.
+ intros. destruct f.
+ - simpl in H. monadInv H. unfold tunnel_function in EQ.
+ destruct (check_included _ _); try congruence.
+ monadInv EQ. simpl; auto.
+ - simpl in H. monadInv H. reflexivity.
+Qed.
+
+Lemma fn_stacksize_preserved:
+ forall f tf, tunnel_function f = OK tf -> fn_stacksize tf = fn_stacksize f.
+Proof.
+ intros f tf; unfold tunnel_function.
+ destruct (check_included _ _); try congruence.
+ destruct (check_code _ _); simpl; try congruence.
+ intros H; inversion H; simpl; auto.
+Qed.
+
+Lemma fn_entrypoint_preserved:
+ forall f tf, tunnel_function f = OK tf -> fn_entrypoint tf = branch_target f (fn_entrypoint f).
+Proof.
+ intros f tf; unfold tunnel_function.
+ destruct (check_included _ _); try congruence.
+ destruct (check_code _ _); simpl; try congruence.
+ intros H; inversion H; simpl; auto.
+Qed.
+
+
+(** The proof of semantic preservation is a simulation argument
+ based on diagrams of the following form:
+<<
+ st1 --------------- st2
+ | |
+ t| ?|t
+ | |
+ v v
+ st1'--------------- st2'
+>>
+ The [match_states] predicate, defined below, captures the precondition
+ between states [st1] and [st2], as well as the postcondition between
+ [st1'] and [st2']. One transition in the source code (left) can correspond
+ to zero or one transition in the transformed code (right). The
+ "zero transition" case occurs when executing a [Lnop] instruction
+ in the source code that has been removed by tunneling.
+
+ In the definition of [match_states], what changes between the original and
+ transformed codes is mainly the control-flow
+ (in particular, the current program point [pc]), but also some values
+ and memory states, since some [Vundef] values can become more defined
+ as a consequence of eliminating useless [Lcond] instructions. *)
+
+Definition locmap_lessdef (ls1 ls2: locset) : Prop :=
+ forall l, Val.lessdef (ls1 l) (ls2 l).
+
+Inductive match_stackframes: stackframe -> stackframe -> Prop :=
+ | match_stackframes_intro:
+ forall f tf sp ls0 bb tls0,
+ locmap_lessdef ls0 tls0 ->
+ tunnel_function f = OK tf ->
+ match_stackframes
+ (Stackframe f sp ls0 bb)
+ (Stackframe tf sp tls0 (tunnel_block (branch_target f) bb)).
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s f tf sp pc ls m ts tls tm
+ (STK: list_forall2 match_stackframes s ts)
+ (LS: locmap_lessdef ls tls)
+ (MEM: Mem.extends m tm)
+ (TF: tunnel_function f = OK tf),
+ match_states (State s f sp pc ls m)
+ (State ts tf sp (branch_target f pc) tls tm)
+ | match_states_block:
+ forall s f tf sp bb ls m ts tls tm
+ (STK: list_forall2 match_stackframes s ts)
+ (LS: locmap_lessdef ls tls)
+ (MEM: Mem.extends m tm)
+ (TF: tunnel_function f = OK tf),
+ match_states (Block s f sp bb ls m)
+ (Block ts tf sp (tunnel_block (branch_target f) bb) tls tm)
+ | match_states_interm:
+ forall s f tf sp pc i bb ls m ts tls tm
+ (STK: list_forall2 match_stackframes s ts)
+ (LS: locmap_lessdef ls tls)
+ (MEM: Mem.extends m tm)
+ (IBRANCH: tunnel_instr (branch_target f) i = Lbranch pc)
+ (TF: tunnel_function f = OK tf),
+ match_states (Block s f sp (i :: bb) ls m)
+ (State ts tf sp pc tls tm)
+ | match_states_call:
+ forall s f tf ls m ts tls tm
+ (STK: list_forall2 match_stackframes s ts)
+ (LS: locmap_lessdef ls tls)
+ (MEM: Mem.extends m tm)
+ (TF: tunnel_fundef f = OK tf),
+ match_states (Callstate s f ls m)
+ (Callstate ts tf tls tm)
+ | match_states_return:
+ forall s ls m ts tls tm
+ (STK: list_forall2 match_stackframes s ts)
+ (LS: locmap_lessdef ls tls)
+ (MEM: Mem.extends m tm),
+ match_states (Returnstate s ls m)
+ (Returnstate ts tls tm).
+
+(** Properties of [locmap_lessdef] *)
+
+Lemma reglist_lessdef:
+ forall rl ls1 ls2,
+ locmap_lessdef ls1 ls2 -> Val.lessdef_list (reglist ls1 rl) (reglist ls2 rl).
+Proof.
+ induction rl; simpl; intros; auto.
+Qed.
+
+Lemma locmap_set_lessdef:
+ forall ls1 ls2 v1 v2 l,
+ locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.set l v1 ls1) (Locmap.set l v2 ls2).
+Proof.
+ intros; red; intros l'. unfold Locmap.set. destruct (Loc.eq l l').
+- destruct l; auto using Val.load_result_lessdef.
+- destruct (Loc.diff_dec l l'); auto.
+Qed.
+
+Lemma locmap_set_undef_lessdef:
+ forall ls1 ls2 l,
+ locmap_lessdef ls1 ls2 -> locmap_lessdef (Locmap.set l Vundef ls1) ls2.
+Proof.
+ intros; red; intros l'. unfold Locmap.set. destruct (Loc.eq l l').
+- destruct l; auto. destruct ty; auto.
+- destruct (Loc.diff_dec l l'); auto.
+Qed.
+
+Lemma locmap_undef_regs_lessdef:
+ forall rl ls1 ls2,
+ locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_regs rl ls1) (undef_regs rl ls2).
+Proof.
+ induction rl as [ | r rl]; intros; simpl. auto. apply locmap_set_lessdef; auto.
+Qed.
+
+Lemma locmap_undef_regs_lessdef_1:
+ forall rl ls1 ls2,
+ locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_regs rl ls1) ls2.
+Proof.
+ induction rl as [ | r rl]; intros; simpl. auto. apply locmap_set_undef_lessdef; auto.
+Qed.
+
+Lemma locmap_getpair_lessdef:
+ forall p ls1 ls2,
+ locmap_lessdef ls1 ls2 -> Val.lessdef (Locmap.getpair p ls1) (Locmap.getpair p ls2).
+Proof.
+ intros; destruct p; simpl; auto using Val.longofwords_lessdef.
+Qed.
+
+Lemma locmap_getpairs_lessdef:
+ forall pl ls1 ls2,
+ locmap_lessdef ls1 ls2 ->
+ Val.lessdef_list (map (fun p => Locmap.getpair p ls1) pl) (map (fun p => Locmap.getpair p ls2) pl).
+Proof.
+ intros. induction pl; simpl; auto using locmap_getpair_lessdef.
+Qed.
+
+Lemma locmap_setpair_lessdef:
+ forall p ls1 ls2 v1 v2,
+ locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.setpair p v1 ls1) (Locmap.setpair p v2 ls2).
+Proof.
+ intros; destruct p; simpl; auto using locmap_set_lessdef, Val.loword_lessdef, Val.hiword_lessdef.
+Qed.
+
+Lemma locmap_setres_lessdef:
+ forall res ls1 ls2 v1 v2,
+ locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.setres res v1 ls1) (Locmap.setres res v2 ls2).
+Proof.
+ induction res; intros; simpl; auto using locmap_set_lessdef, Val.loword_lessdef, Val.hiword_lessdef.
+Qed.
+
+Lemma locmap_undef_caller_save_regs_lessdef:
+ forall ls1 ls2,
+ locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_caller_save_regs ls1) (undef_caller_save_regs ls2).
+Proof.
+ intros; red; intros. unfold undef_caller_save_regs.
+ destruct l.
+- destruct (Conventions1.is_callee_save r); auto.
+- destruct sl; auto.
+Qed.
+
+Lemma find_function_translated:
+ forall ros ls tls fd,
+ locmap_lessdef ls tls ->
+ find_function ge ros ls = Some fd ->
+ exists tfd, tunnel_fundef fd = OK tfd /\ find_function tge ros tls = Some tfd.
+Proof.
+ intros. destruct ros; simpl in *.
+- assert (E: tls (R m) = ls (R m)).
+ { exploit Genv.find_funct_inv; eauto. intros (b & EQ).
+ generalize (H (R m)). rewrite EQ. intros LD; inv LD. auto. }
+ rewrite E. exploit functions_translated; eauto.
+- rewrite symbols_preserved. destruct (Genv.find_symbol ge i); inv H0.
+ exploit function_ptr_translated; eauto.
+ intros (tf & X1 & X2). exists tf; intuition.
+Qed.
+
+Lemma call_regs_lessdef:
+ forall ls1 ls2, locmap_lessdef ls1 ls2 -> locmap_lessdef (call_regs ls1) (call_regs ls2).
+Proof.
+ intros; red; intros. destruct l as [r | [] ofs ty]; simpl; auto.
+Qed.
+
+Lemma return_regs_lessdef:
+ forall caller1 callee1 caller2 callee2,
+ locmap_lessdef caller1 caller2 ->
+ locmap_lessdef callee1 callee2 ->
+ locmap_lessdef (return_regs caller1 callee1) (return_regs caller2 callee2).
+Proof.
+ intros; red; intros. destruct l; simpl.
+- destruct (Conventions1.is_callee_save r); auto.
+- destruct sl; auto.
+Qed.
+
+(** To preserve non-terminating behaviours, we show that the transformed
+ code cannot take an infinity of "zero transition" cases.
+ We use the following [measure] function over source states,
+ which decreases strictly in the "zero transition" case. *)
+
+Definition measure (st: state) : nat :=
+ match st with
+ | State s f sp pc ls m => (bound (branch_target f) pc) * 2
+ | Block s f sp (Lbranch pc :: _) ls m => (bound (branch_target f) pc) * 2 + 1
+ | Block s f sp (Lcond _ _ pc1 pc2 _ :: _) ls m => (max (bound (branch_target f) pc1) (bound (branch_target f) pc2)) * 2 + 1
+ | Block s f sp bb ls m => 0
+ | Callstate s f ls m => 0
+ | Returnstate s ls m => 0
+ end.
+
+Lemma match_parent_locset:
+ forall s ts,
+ list_forall2 match_stackframes s ts ->
+ locmap_lessdef (parent_locset s) (parent_locset ts).
+Proof.
+ induction 1; simpl.
+- red; auto.
+- inv H; auto.
+Qed.
+
+Lemma tunnel_step_correct:
+ forall st1 t st2, step ge st1 t st2 ->
+ forall st1' (MS: match_states st1 st1'),
+ (exists st2', step tge st1' t st2' /\ match_states st2 st2')
+ \/ (measure st2 < measure st1 /\ t = E0 /\ match_states st2 st1')%nat.
+Proof.
+ induction 1; intros; try inv MS; try (simpl in IBRANCH; inv IBRANCH).
+
+- (* entering a block *)
+ exploit (branch_target_bounds f tf pc); eauto.
+ rewrite H. intros X; inversion X.
+ + (* TB_default *)
+ rewrite TB; left. econstructor; split.
+ * econstructor. simpl. erewrite tunnel_function_unfold, H ; simpl; eauto.
+ * econstructor; eauto.
+ + (* FT_branch *)
+ simpl; right.
+ rewrite EQ; repeat (econstructor; lia || eauto).
+ + (* FT_cond *)
+ simpl; right.
+ repeat (econstructor; lia || eauto); simpl.
+ destruct (peq _ _); try congruence.
+- (* Lop *)
+ exploit eval_operation_lessdef. apply reglist_lessdef; eauto. eauto. eauto.
+ intros (tv & EV & LD).
+ left; simpl; econstructor; split.
+ eapply exec_Lop with (v := tv); eauto.
+ rewrite <- EV. apply eval_operation_preserved. exact symbols_preserved.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+- (* Lload *)
+ exploit eval_addressing_lessdef. apply reglist_lessdef; eauto. eauto.
+ intros (ta & EV & LD).
+ exploit Mem.loadv_extends. eauto. eauto. eexact LD.
+ intros (tv & LOAD & LD').
+ left; simpl; econstructor; split.
+ eapply exec_Lload with (a := ta).
+ rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
+ eauto. eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+- (* Lload notrap1 *)
+ exploit eval_addressing_lessdef_none. apply reglist_lessdef; eauto. eassumption.
+ left; simpl; econstructor; split.
+ eapply exec_Lload_notrap1.
+ rewrite <- H0.
+ apply eval_addressing_preserved. exact symbols_preserved. eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+- (* Lload notrap2 *)
+ exploit eval_addressing_lessdef. apply reglist_lessdef; eauto. eauto.
+ intros (ta & EV & LD).
+ destruct (Mem.loadv chunk tm ta) eqn:Htload.
+ {
+ left; simpl; econstructor; split.
+ eapply exec_Lload.
+ rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
+ exact Htload. eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+ }
+ {
+ left; simpl; econstructor; split.
+ eapply exec_Lload_notrap2.
+ rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
+ exact Htload. eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+ }
+- (* Lgetstack *)
+ left; simpl; econstructor; split.
+ econstructor; eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+- (* Lsetstack *)
+ left; simpl; econstructor; split.
+ econstructor; eauto.
+ econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
+- (* Lstore *)
+ exploit eval_addressing_lessdef. apply reglist_lessdef; eauto. eauto.
+ intros (ta & EV & LD).
+ exploit Mem.storev_extends. eauto. eauto. eexact LD. apply LS.
+ intros (tm' & STORE & MEM').
+ left; simpl; econstructor; split.
+ eapply exec_Lstore with (a := ta).
+ rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
+ eauto. eauto.
+ econstructor; eauto using locmap_undef_regs_lessdef.
+- (* Lcall *)
+ left; simpl.
+ exploit find_function_translated; eauto.
+ intros (tfd & Htfd & FIND).
+ econstructor; split.
+ + eapply exec_Lcall; eauto.
+ erewrite sig_preserved; eauto.
+ + econstructor; eauto.
+ constructor; auto.
+ constructor; auto.
+- (* Ltailcall *)
+ exploit find_function_translated. 2: eauto.
+ { eauto using return_regs_lessdef, match_parent_locset. }
+ intros (tfd & Htfd & FIND).
+ exploit Mem.free_parallel_extends. eauto. eauto. intros (tm' & FREE & MEM').
+ left; simpl; econstructor; split.
+ + eapply exec_Ltailcall; eauto.
+ * eapply sig_preserved; eauto.
+ * erewrite fn_stacksize_preserved; eauto.
+ + econstructor; eauto using return_regs_lessdef, match_parent_locset.
+- (* Lbuiltin *)
+ exploit eval_builtin_args_lessdef. eexact LS. eauto. eauto. intros (tvargs & EVA & LDA).
+ exploit external_call_mem_extends; eauto. intros (tvres & tm' & A & B & C & D).
+ left; simpl; econstructor; split.
+ eapply exec_Lbuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved. apply senv_preserved. eauto.
+ econstructor; eauto using locmap_setres_lessdef, locmap_undef_regs_lessdef.
+- (* Lbranch (preserved) *)
+ left; simpl; econstructor; split.
+ eapply exec_Lbranch; eauto.
+ fold (branch_target f pc). econstructor; eauto.
+- (* Lbranch (eliminated) *)
+ right; split. simpl. lia. split. auto. constructor; auto.
+- (* Lcond (preserved) *)
+ simpl; left; destruct (peq _ _) eqn: EQ.
+ + econstructor; split.
+ eapply exec_Lbranch.
+ destruct b.
+ * constructor; eauto using locmap_undef_regs_lessdef_1.
+ * rewrite e. constructor; eauto using locmap_undef_regs_lessdef_1.
+ + econstructor; split.
+ eapply exec_Lcond; eauto. eapply eval_condition_lessdef; eauto using reglist_lessdef.
+ destruct b; econstructor; eauto using locmap_undef_regs_lessdef.
+- (* Lcond (eliminated) *)
+ destruct (peq _ _) eqn: EQ; try inv H1.
+ right; split; simpl.
+ + destruct b.
+ generalize (Nat.le_max_l (bound (branch_target f) pc1) (bound (branch_target f) pc2)); lia.
+ generalize (Nat.le_max_r (bound (branch_target f) pc1) (bound (branch_target f) pc2)); lia.
+ + destruct b.
+ -- repeat (constructor; auto).
+ -- rewrite e; repeat (constructor; auto).
+- (* Ljumptable *)
+ assert (tls (R arg) = Vint n).
+ { generalize (LS (R arg)); rewrite H; intros LD; inv LD; auto. }
+ left; simpl; econstructor; split.
+ eapply exec_Ljumptable.
+ eauto. rewrite list_nth_z_map, H0; simpl; eauto. eauto.
+ econstructor; eauto using locmap_undef_regs_lessdef.
+- (* Lreturn *)
+ exploit Mem.free_parallel_extends. eauto. eauto. intros (tm' & FREE & MEM').
+ left; simpl; econstructor; split.
+ + eapply exec_Lreturn; eauto.
+ erewrite fn_stacksize_preserved; eauto.
+ + constructor; eauto using return_regs_lessdef, match_parent_locset.
+- (* internal function *)
+ exploit tunnel_fundef_Internal; eauto.
+ intros (tf' & TF' & ITF). subst.
+ exploit Mem.alloc_extends. eauto. eauto. apply Z.le_refl. apply Z.le_refl.
+ intros (tm' & ALLOC & MEM').
+ left; simpl.
+ econstructor; split.
+ + eapply exec_function_internal; eauto.
+ erewrite fn_stacksize_preserved; eauto.
+ + simpl.
+ erewrite (fn_entrypoint_preserved f tf'); auto.
+ econstructor; eauto using locmap_undef_regs_lessdef, call_regs_lessdef.
+- (* external function *)
+ exploit external_call_mem_extends; eauto using locmap_getpairs_lessdef.
+ intros (tvres & tm' & A & B & C & D).
+ left; simpl; econstructor; split.
+ + erewrite (tunnel_fundef_External tf ef); eauto.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + simpl. econstructor; eauto using locmap_setpair_lessdef, locmap_undef_caller_save_regs_lessdef.
+- (* return *)
+ inv STK. inv H1.
+ left; econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall st1, initial_state prog st1 ->
+ exists st2, initial_state tprog st2 /\ match_states st1 st2.
+Proof.
+ intros. inversion H.
+ exploit function_ptr_translated; eauto.
+ intros (tf & Htf & Hf).
+ exists (Callstate nil tf (Locmap.init Vundef) m0); split.
+ econstructor; eauto.
+ apply (Genv.init_mem_transf_partial TRANSL); auto.
+ rewrite (match_program_main TRANSL).
+ rewrite symbols_preserved. eauto.
+ rewrite <- H3. apply sig_preserved. auto.
+ constructor. constructor. red; simpl; auto. apply Mem.extends_refl. auto.
+Qed.
+
+Lemma transf_final_states:
+ forall st1 st2 r,
+ match_states st1 st2 -> final_state st1 r -> final_state st2 r.
+Proof.
+ intros. inv H0. inv H. inv STK.
+ set (p := map_rpair R (Conventions1.loc_result signature_main)) in *.
+ generalize (locmap_getpair_lessdef p _ _ LS). rewrite H1; intros LD; inv LD.
+ econstructor; eauto.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (LTL.semantics prog) (LTL.semantics tprog).
+Proof.
+ eapply forward_simulation_opt.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ eexact tunnel_step_correct.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/Linear.v b/backend/Linear.v
index 447c6ba6..cb11f7dc 100644
--- a/backend/Linear.v
+++ b/backend/Linear.v
@@ -28,7 +28,7 @@ Inductive instruction: Type :=
| Lgetstack: slot -> Z -> typ -> mreg -> instruction
| Lsetstack: mreg -> slot -> Z -> typ -> instruction
| Lop: operation -> list mreg -> mreg -> instruction
- | Lload: memory_chunk -> addressing -> list mreg -> mreg -> instruction
+ | Lload: trapping_mode -> memory_chunk -> addressing -> list mreg -> mreg -> instruction
| Lstore: memory_chunk -> addressing -> list mreg -> mreg -> instruction
| Lcall: signature -> mreg + ident -> instruction
| Ltailcall: signature -> mreg + ident -> instruction
@@ -160,11 +160,28 @@ Inductive step: state -> trace -> state -> Prop :=
step (State s f sp (Lop op args res :: b) rs m)
E0 (State s f sp b rs' m)
| exec_Lload:
- forall s f sp chunk addr args dst b rs m a v rs',
+ forall s f sp trap chunk addr args dst b rs m a v rs',
eval_addressing ge sp addr (reglist rs args) = Some a ->
Mem.loadv chunk m a = Some v ->
rs' = Locmap.set (R dst) v (undef_regs (destroyed_by_load chunk addr) rs) ->
- step (State s f sp (Lload chunk addr args dst :: b) rs m)
+ step (State s f sp (Lload trap chunk addr args dst :: b) rs m)
+ E0 (State s f sp b rs' m)
+ | exec_Lload_notrap1:
+ forall s f sp chunk addr args dst b rs m rs',
+ eval_addressing ge sp addr (reglist rs args) = None ->
+ rs' = Locmap.set (R dst)
+ Vundef
+ (undef_regs (destroyed_by_load chunk addr) rs) ->
+ step (State s f sp (Lload NOTRAP chunk addr args dst :: b) rs m)
+ E0 (State s f sp b rs' m)
+ | exec_Lload_notrap2:
+ forall s f sp chunk addr args dst b rs m a rs',
+ eval_addressing ge sp addr (reglist rs args) = Some a ->
+ Mem.loadv chunk m a = None ->
+ rs' = Locmap.set (R dst)
+ Vundef
+ (undef_regs (destroyed_by_load chunk addr) rs) ->
+ step (State s f sp (Lload NOTRAP chunk addr args dst :: b) rs m)
E0 (State s f sp b rs' m)
| exec_Lstore:
forall s f sp chunk addr args src b rs m m' a rs',
diff --git a/backend/Linearize.v b/backend/Linearize.v
index 2cfa4d3c..66b36428 100644
--- a/backend/Linearize.v
+++ b/backend/Linearize.v
@@ -163,8 +163,8 @@ Fixpoint linearize_block (b: LTL.bblock) (k: code) : code :=
| nil => k
| LTL.Lop op args res :: b' =>
Lop op args res :: linearize_block b' k
- | LTL.Lload chunk addr args dst :: b' =>
- Lload chunk addr args dst :: linearize_block b' k
+ | LTL.Lload trap chunk addr args dst :: b' =>
+ Lload trap chunk addr args dst :: linearize_block b' k
| LTL.Lgetstack sl ofs ty dst :: b' =>
Lgetstack sl ofs ty dst :: linearize_block b' k
| LTL.Lsetstack src sl ofs ty :: b' =>
@@ -179,7 +179,7 @@ Fixpoint linearize_block (b: LTL.bblock) (k: code) : code :=
Lbuiltin ef args res :: linearize_block b' k
| LTL.Lbranch s :: b' =>
add_branch s k
- | LTL.Lcond cond args s1 s2 :: b' =>
+ | LTL.Lcond cond args s1 s2 _ :: b' =>
if starts_with s1 k then
Lcond (negate_condition cond) args s2 :: add_branch s1 k
else
diff --git a/backend/Linearizeaux.ml b/backend/Linearizeaux.ml
index 902724e0..5914f6a3 100644
--- a/backend/Linearizeaux.ml
+++ b/backend/Linearizeaux.ml
@@ -12,7 +12,12 @@
open LTL
open Maps
-open Camlcoq
+
+let debug_flag = ref false
+
+let debug fmt =
+ if !debug_flag then Printf.eprintf fmt
+ else Printf.ifprintf stderr fmt
(* Trivial enumeration, in decreasing order of PC *)
@@ -29,6 +34,8 @@ let enumerate_aux f reach =
(* More clever enumeration that flattens basic blocks *)
+open Camlcoq
+
module IntSet = Set.Make(struct type t = int let compare = compare end)
(* Determine join points: reachable nodes that have > 1 predecessor *)
@@ -80,7 +87,7 @@ let basic_blocks f joins =
| [] -> assert false
| Lbranch s :: _ -> next_in_block blk minpc s
| Ltailcall (sig0, ros) :: _ -> end_block blk minpc
- | Lcond (cond, args, ifso, ifnot) :: _ ->
+ | Lcond (cond, args, ifso, ifnot, _) :: _ ->
end_block blk minpc; start_block ifso; start_block ifnot
| Ljumptable(arg, tbl) :: _ ->
end_block blk minpc; List.iter start_block tbl
@@ -110,5 +117,73 @@ let flatten_blocks blks =
(* Build the enumeration *)
-let enumerate_aux f reach =
+let enumerate_aux_flat f reach =
flatten_blocks (basic_blocks f (join_points f))
+
+(**
+ * Alternate enumeration based on traces as identified by Duplicate.v
+ *
+ * This is a slight alteration to the above heuristic, ensuring that any
+ * superblock will be contiguous in memory, while still following the original
+ * heuristic
+ *
+ * Slight change: instead of taking the minimum pc of the superblock, we just take
+ * the pc of the first block.
+ * (experimentally this leads to slightly better performance..)
+ *)
+
+let super_blocks f joins =
+ let blocks = ref [] in
+ let visited = ref IntSet.empty in
+ (* start_block:
+ pc is the function entry point
+ or a join point
+ or the successor of a conditional test *)
+ let rec start_block pc =
+ let npc = P.to_int pc in
+ if not (IntSet.mem npc !visited) then begin
+ visited := IntSet.add npc !visited;
+ in_block [] npc pc
+ end
+ (* in_block: add pc to block and check successors *)
+ and in_block blk minpc pc =
+ let blk = pc :: blk in
+ match PTree.get pc f.fn_code with
+ | None -> assert false
+ | Some b ->
+ let rec do_instr_list = function
+ | [] -> assert false
+ | Lbranch s :: _ -> next_in_block blk minpc s
+ | Ltailcall (sig0, ros) :: _ -> end_block blk minpc
+ | Lcond (cond, args, ifso, ifnot, pred) :: _ -> begin
+ match pred with
+ | None -> (end_block blk minpc; start_block ifso; start_block ifnot)
+ | Some true -> (next_in_block blk minpc ifso; start_block ifnot)
+ | Some false -> (next_in_block blk minpc ifnot; start_block ifso)
+ end
+ | Ljumptable(arg, tbl) :: _ ->
+ end_block blk minpc; List.iter start_block tbl
+ | Lreturn :: _ -> end_block blk minpc
+ | instr :: b' -> do_instr_list b' in
+ do_instr_list b
+ (* next_in_block: check if join point and either extend block
+ or start block *)
+ and next_in_block blk minpc pc =
+ let npc = P.to_int pc in
+ if IntSet.mem npc joins
+ then (end_block blk minpc; start_block pc)
+ else in_block blk minpc pc
+ (* end_block: record block that we just discovered *)
+ and end_block blk minpc =
+ blocks := (minpc, List.rev blk) :: !blocks
+ in
+ start_block f.fn_entrypoint; !blocks
+
+(* Build the enumeration *)
+
+let enumerate_aux_sb f reach =
+ flatten_blocks (super_blocks f (join_points f))
+
+let enumerate_aux f reach =
+ if !Clflags.option_ftracelinearize then enumerate_aux_sb f reach
+ else enumerate_aux_flat f reach
diff --git a/backend/Linearizeproof.v b/backend/Linearizeproof.v
index b065238c..c12eab6e 100644
--- a/backend/Linearizeproof.v
+++ b/backend/Linearizeproof.v
@@ -585,45 +585,61 @@ Proof.
intros; eapply reachable_successors; eauto.
eapply is_tail_lin_block; eauto. eapply is_tail_find_label; eauto.
- (* Lop *)
+- (* Lop *)
left; econstructor; split. simpl.
apply plus_one. econstructor; eauto.
instantiate (1 := v); rewrite <- H; apply eval_operation_preserved.
exact symbols_preserved.
econstructor; eauto.
- (* Lload *)
+- (* Lload *)
left; econstructor; split. simpl.
- apply plus_one. econstructor.
+ apply plus_one. eapply exec_Lload.
instantiate (1 := a). rewrite <- H; apply eval_addressing_preserved.
exact symbols_preserved. eauto. eauto.
econstructor; eauto.
- (* Lgetstack *)
+- (* Lload notrap1 *)
+ left; econstructor; split. simpl.
+ apply plus_one. eapply exec_Lload_notrap1.
+ rewrite <- H.
+ apply eval_addressing_preserved.
+ exact symbols_preserved. eauto.
+ econstructor; eauto.
+
+- (* Lload notrap2 *)
+ left; econstructor; split. simpl.
+ apply plus_one. eapply exec_Lload_notrap2.
+ rewrite <- H.
+ apply eval_addressing_preserved.
+ exact symbols_preserved. eauto. eauto.
+ econstructor; eauto.
+
+- (* Lgetstack *)
left; econstructor; split. simpl.
apply plus_one. econstructor; eauto.
econstructor; eauto.
- (* Lsetstack *)
+- (* Lsetstack *)
left; econstructor; split. simpl.
apply plus_one. econstructor; eauto.
econstructor; eauto.
- (* Lstore *)
+- (* Lstore *)
left; econstructor; split. simpl.
apply plus_one. econstructor.
instantiate (1 := a). rewrite <- H; apply eval_addressing_preserved.
exact symbols_preserved. eauto. eauto.
econstructor; eauto.
- (* Lcall *)
+- (* Lcall *)
exploit find_function_translated; eauto. intros [tfd [A B]].
left; econstructor; split. simpl.
apply plus_one. econstructor; eauto.
symmetry; eapply sig_preserved; eauto.
econstructor; eauto. constructor; auto. econstructor; eauto.
- (* Ltailcall *)
+- (* Ltailcall *)
exploit find_function_translated; eauto. intros [tfd [A B]].
left; econstructor; split. simpl.
apply plus_one. econstructor; eauto.
@@ -633,18 +649,18 @@ Proof.
rewrite (match_parent_locset _ _ STACKS).
econstructor; eauto.
- (* Lbuiltin *)
+- (* Lbuiltin *)
left; econstructor; split. simpl.
apply plus_one. eapply exec_Lbuiltin; eauto.
eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
eapply external_call_symbols_preserved; eauto. apply senv_preserved.
econstructor; eauto.
- (* Lbranch *)
+- (* Lbranch *)
assert ((reachable f)!!pc = true). apply REACH; simpl; auto.
right; split. simpl; lia. split. auto. simpl. econstructor; eauto.
- (* Lcond *)
+- (* Lcond *)
assert (REACH1: (reachable f)!!pc1 = true) by (apply REACH; simpl; auto).
assert (REACH2: (reachable f)!!pc2 = true) by (apply REACH; simpl; auto).
simpl linearize_block.
@@ -670,18 +686,18 @@ Proof.
apply plus_one. eapply exec_Lcond_false. eauto. eauto.
econstructor; eauto.
- (* Ljumptable *)
+- (* Ljumptable *)
assert (REACH': (reachable f)!!pc = true).
apply REACH. simpl. eapply list_nth_z_in; eauto.
right; split. simpl; lia. split. auto. econstructor; eauto.
- (* Lreturn *)
+- (* Lreturn *)
left; econstructor; split.
simpl. apply plus_one. econstructor; eauto.
rewrite (stacksize_preserved _ _ TRF). eauto.
rewrite (match_parent_locset _ _ STACKS). econstructor; eauto.
- (* internal functions *)
+- (* internal functions *)
assert (REACH: (reachable f)!!(LTL.fn_entrypoint f) = true).
apply reachable_entrypoint.
monadInv H7.
@@ -691,13 +707,13 @@ Proof.
generalize EQ; intro EQ'; monadInv EQ'. simpl.
econstructor; eauto. simpl. eapply is_tail_add_branch. constructor.
- (* external function *)
+- (* external function *)
monadInv H8. left; econstructor; split.
apply plus_one. eapply exec_function_external; eauto.
eapply external_call_symbols_preserved; eauto. apply senv_preserved.
econstructor; eauto.
- (* return *)
+- (* return *)
inv H3. inv H1.
left; econstructor; split.
apply plus_one. econstructor.
diff --git a/backend/Lineartyping.v b/backend/Lineartyping.v
index 0e3b7c8e..cf903aad 100644
--- a/backend/Lineartyping.v
+++ b/backend/Lineartyping.v
@@ -76,7 +76,7 @@ Definition wt_instr (i: instruction) : bool :=
let (targs, tres) := type_of_operation op in
subtype tres (mreg_type res)
end
- | Lload chunk addr args dst =>
+ | Lload trap chunk addr args dst =>
subtype (type_of_chunk chunk) (mreg_type dst)
| Ltailcall sg ros =>
zeq (size_arguments sg) 0
@@ -321,17 +321,31 @@ Local Opaque mreg_type.
+ (* other ops *)
destruct (type_of_operation op) as [ty_args ty_res] eqn:TYOP. InvBooleans.
econstructor; eauto.
- apply wt_setreg. eapply Val.has_subtype; eauto.
+
+ apply wt_setreg; auto; try (apply wt_undef_regs; auto).
+ eapply Val.has_subtype; eauto.
change ty_res with (snd (ty_args, ty_res)). rewrite <- TYOP. eapply type_of_operation_sound; eauto.
red; intros; subst op. simpl in ISMOVE.
destruct args; try discriminate. destruct args; discriminate.
- apply wt_undef_regs; auto.
+ (* no longer needed apply wt_undef_regs; auto. *)
- (* load *)
simpl in *; InvBooleans.
econstructor; eauto.
apply wt_setreg. eapply Val.has_subtype; eauto.
destruct a; simpl in H0; try discriminate. eapply Mem.load_type; eauto.
apply wt_undef_regs; auto.
+- (* load notrap1 *)
+ simpl in *; InvBooleans.
+ econstructor; eauto.
+ apply wt_setreg. eapply Val.has_subtype; eauto.
+ constructor.
+ apply wt_undef_regs; auto.
+- (* load notrap2 *)
+ simpl in *; InvBooleans.
+ econstructor; eauto.
+ apply wt_setreg. eapply Val.has_subtype; eauto.
+ constructor.
+ apply wt_undef_regs; auto.
- (* store *)
simpl in *; InvBooleans.
econstructor. eauto. eauto. eauto.
diff --git a/backend/Liveness.v b/backend/Liveness.v
index 16533158..9652b363 100644
--- a/backend/Liveness.v
+++ b/backend/Liveness.v
@@ -79,7 +79,7 @@ Definition transfer
reg_list_live args (reg_dead res after)
else
after
- | Iload chunk addr args dst s =>
+ | Iload trap chunk addr args dst s =>
if Regset.mem dst after then
reg_list_live args (reg_dead dst after)
else
@@ -94,7 +94,7 @@ Definition transfer
| Ibuiltin ef args res s =>
reg_list_live (params_of_builtin_args args)
(reg_list_dead (params_of_builtin_res res) after)
- | Icond cond args ifso ifnot =>
+ | Icond cond args ifso ifnot _ =>
reg_list_live args after
| Ijumptable arg tbl =>
reg_live arg after
diff --git a/backend/Mach.v b/backend/Mach.v
index 9fdee9eb..2cfd738d 100644
--- a/backend/Mach.v
+++ b/backend/Mach.v
@@ -56,7 +56,7 @@ Inductive instruction: Type :=
| Msetstack: mreg -> ptrofs -> typ -> instruction
| Mgetparam: ptrofs -> typ -> mreg -> instruction
| Mop: operation -> list mreg -> mreg -> instruction
- | Mload: memory_chunk -> addressing -> list mreg -> mreg -> instruction
+ | Mload: trapping_mode -> memory_chunk -> addressing -> list mreg -> mreg -> instruction
| Mstore: memory_chunk -> addressing -> list mreg -> mreg -> instruction
| Mcall: signature -> mreg + ident -> instruction
| Mtailcall: signature -> mreg + ident -> instruction
@@ -321,11 +321,24 @@ Inductive step: state -> trace -> state -> Prop :=
step (State s f sp (Mop op args res :: c) rs m)
E0 (State s f sp c rs' m)
| exec_Mload:
- forall s f sp chunk addr args dst c rs m a v rs',
+ forall s f sp trap chunk addr args dst c rs m a v rs',
eval_addressing ge sp addr rs##args = Some a ->
Mem.loadv chunk m a = Some v ->
rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- v) ->
- step (State s f sp (Mload chunk addr args dst :: c) rs m)
+ step (State s f sp (Mload trap chunk addr args dst :: c) rs m)
+ E0 (State s f sp c rs' m)
+ | exec_Mload_notrap1:
+ forall s f sp chunk addr args dst c rs m rs',
+ eval_addressing ge sp addr rs##args = None ->
+ rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- Vundef) ->
+ step (State s f sp (Mload NOTRAP chunk addr args dst :: c) rs m)
+ E0 (State s f sp c rs' m)
+ | exec_Mload_notrap2:
+ forall s f sp chunk addr args dst c rs m a rs',
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = None ->
+ rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- Vundef) ->
+ step (State s f sp (Mload NOTRAP chunk addr args dst :: c) rs m)
E0 (State s f sp c rs' m)
| exec_Mstore:
forall s f sp chunk addr args src c rs m m' a rs',
diff --git a/backend/OpHelpers.v b/backend/OpHelpers.v
new file mode 100644
index 00000000..7f8af39b
--- /dev/null
+++ b/backend/OpHelpers.v
@@ -0,0 +1,54 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import AST Integers Floats.
+Require Import Op CminorSel.
+
+(** Some arithmetic operations are transformed into calls to
+ runtime library functions. The following type class collects
+ the names of these functions. *)
+
+Definition sig_l_l := mksignature (Tlong :: nil) Tlong cc_default.
+Definition sig_l_f := mksignature (Tlong :: nil) Tfloat cc_default.
+Definition sig_l_s := mksignature (Tlong :: nil) Tsingle cc_default.
+Definition sig_f_l := mksignature (Tfloat :: nil) Tlong cc_default.
+Definition sig_ll_l := mksignature (Tlong :: Tlong :: nil) Tlong cc_default.
+Definition sig_li_l := mksignature (Tlong :: Tint :: nil) Tlong cc_default.
+Definition sig_ii_l := mksignature (Tint :: Tint :: nil) Tlong cc_default.
+Definition sig_ii_i := mksignature (Tint :: Tint :: nil) Tint cc_default.
+Definition sig_ff_f := mksignature (Tfloat :: Tfloat :: nil) Tfloat cc_default.
+Definition sig_ss_s := mksignature (Tsingle :: Tsingle :: nil) Tsingle cc_default.
+
+Class helper_functions := mk_helper_functions {
+ i64_dtos: ident; (**r float64 -> signed long *)
+ i64_dtou: ident; (**r float64 -> unsigned long *)
+ i64_stod: ident; (**r signed long -> float64 *)
+ i64_utod: ident; (**r unsigned long -> float64 *)
+ i64_stof: ident; (**r signed long -> float32 *)
+ i64_utof: ident; (**r unsigned long -> float32 *)
+ i64_sdiv: ident; (**r signed division *)
+ i64_udiv: ident; (**r unsigned division *)
+ i64_smod: ident; (**r signed remainder *)
+ i64_umod: ident; (**r unsigned remainder *)
+ i64_shl: ident; (**r shift left *)
+ i64_shr: ident; (**r shift right unsigned *)
+ i64_sar: ident; (**r shift right signed *)
+ i64_umulh: ident; (**r unsigned multiply high *)
+ i64_smulh: ident; (**r signed multiply high *)
+ i32_sdiv: ident; (**r signed division *)
+ i32_udiv: ident; (**r unsigned division *)
+ i32_smod: ident; (**r signed remainder *)
+ i32_umod: ident; (**r unsigned remainder *)
+ f64_div: ident; (**float division*)
+ f32_div: ident; (**float division*)
+}.
diff --git a/backend/OpHelpersproof.v b/backend/OpHelpersproof.v
new file mode 100644
index 00000000..63199520
--- /dev/null
+++ b/backend/OpHelpersproof.v
@@ -0,0 +1,90 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Cminor.
+Require Import Op.
+Require Import CminorSel.
+Require Import Events.
+Require Import OpHelpers.
+
+(** * Axiomatization of the helper functions *)
+
+Definition external_implements (name: string) (sg: signature) (vargs: list val) (vres: val) : Prop :=
+ forall F V (ge: Genv.t F V) m,
+ external_call (EF_runtime name sg) ge vargs m E0 vres m.
+
+Definition builtin_implements (name: string) (sg: signature) (vargs: list val) (vres: val) : Prop :=
+ forall F V (ge: Genv.t F V) m,
+ external_call (EF_builtin name sg) ge vargs m E0 vres m.
+
+Axiom arith_helpers_correct :
+ (forall x z, Val.longoffloat x = Some z -> external_implements "__compcert_i64_dtos" sig_f_l (x::nil) z)
+ /\ (forall x z, Val.longuoffloat x = Some z -> external_implements "__compcert_i64_dtou" sig_f_l (x::nil) z)
+ /\ (forall x z, Val.floatoflong x = Some z -> external_implements "__compcert_i64_stod" sig_l_f (x::nil) z)
+ /\ (forall x z, Val.floatoflongu x = Some z -> external_implements "__compcert_i64_utod" sig_l_f (x::nil) z)
+ /\ (forall x z, Val.singleoflong x = Some z -> external_implements "__compcert_i64_stof" sig_l_s (x::nil) z)
+ /\ (forall x z, Val.singleoflongu x = Some z -> external_implements "__compcert_i64_utof" sig_l_s (x::nil) z)
+ /\ (forall x, builtin_implements "__builtin_negl" sig_l_l (x::nil) (Val.negl x))
+ /\ (forall x y, builtin_implements "__builtin_addl" sig_ll_l (x::y::nil) (Val.addl x y))
+ /\ (forall x y, builtin_implements "__builtin_subl" sig_ll_l (x::y::nil) (Val.subl x y))
+ /\ (forall x y, builtin_implements "__builtin_mull" sig_ii_l (x::y::nil) (Val.mull' x y))
+ /\ (forall x y z, Val.divls x y = Some z -> external_implements "__compcert_i64_sdiv" sig_ll_l (x::y::nil) z)
+ /\ (forall x y z, Val.divlu x y = Some z -> external_implements "__compcert_i64_udiv" sig_ll_l (x::y::nil) z)
+ /\ (forall x y z, Val.modls x y = Some z -> external_implements "__compcert_i64_smod" sig_ll_l (x::y::nil) z)
+ /\ (forall x y z, Val.modlu x y = Some z -> external_implements "__compcert_i64_umod" sig_ll_l (x::y::nil) z)
+ /\ (forall x y, external_implements "__compcert_i64_shl" sig_li_l (x::y::nil) (Val.shll x y))
+ /\ (forall x y, external_implements "__compcert_i64_shr" sig_li_l (x::y::nil) (Val.shrlu x y))
+ /\ (forall x y, external_implements "__compcert_i64_sar" sig_li_l (x::y::nil) (Val.shrl x y))
+ /\ (forall x y, external_implements "__compcert_i64_umulh" sig_ll_l (x::y::nil) (Val.mullhu x y))
+ /\ (forall x y, external_implements "__compcert_i64_smulh" sig_ll_l (x::y::nil) (Val.mullhs x y))
+ /\ (forall x y z, Val.divs x y = Some z -> external_implements "__compcert_i32_sdiv" sig_ii_i (x::y::nil) z)
+ /\ (forall x y z, Val.divu x y = Some z -> external_implements "__compcert_i32_udiv" sig_ii_i (x::y::nil) z)
+ /\ (forall x y z, Val.mods x y = Some z -> external_implements "__compcert_i32_smod" sig_ii_i (x::y::nil) z)
+ /\ (forall x y z, Val.modu x y = Some z -> external_implements "__compcert_i32_umod" sig_ii_i (x::y::nil) z)
+ /\ (forall x y z, Val.divfs x y = z -> external_implements "__compcert_f32_div" sig_ss_s (x::y::nil) z)
+ /\ (forall x y z, Val.divf x y = z -> external_implements "__compcert_f64_div" sig_ff_f (x::y::nil) z)
+.
+
+Definition helper_declared {F V: Type} (p: AST.program (AST.fundef F) V) (id: ident) (name: string) (sg: signature) : Prop :=
+ (prog_defmap p)!id = Some (Gfun (External (EF_runtime name sg))).
+
+Definition helper_functions_declared {F V: Type} (p: AST.program (AST.fundef F) V) (hf: helper_functions) : Prop :=
+ helper_declared p i64_dtos "__compcert_i64_dtos" sig_f_l
+ /\ helper_declared p i64_dtou "__compcert_i64_dtou" sig_f_l
+ /\ helper_declared p i64_stod "__compcert_i64_stod" sig_l_f
+ /\ helper_declared p i64_utod "__compcert_i64_utod" sig_l_f
+ /\ helper_declared p i64_stof "__compcert_i64_stof" sig_l_s
+ /\ helper_declared p i64_utof "__compcert_i64_utof" sig_l_s
+ /\ helper_declared p i64_sdiv "__compcert_i64_sdiv" sig_ll_l
+ /\ helper_declared p i64_udiv "__compcert_i64_udiv" sig_ll_l
+ /\ helper_declared p i64_smod "__compcert_i64_smod" sig_ll_l
+ /\ helper_declared p i64_umod "__compcert_i64_umod" sig_ll_l
+ /\ helper_declared p i64_shl "__compcert_i64_shl" sig_li_l
+ /\ helper_declared p i64_shr "__compcert_i64_shr" sig_li_l
+ /\ helper_declared p i64_sar "__compcert_i64_sar" sig_li_l
+ /\ helper_declared p i64_umulh "__compcert_i64_umulh" sig_ll_l
+ /\ helper_declared p i64_smulh "__compcert_i64_smulh" sig_ll_l
+ /\ helper_declared p i32_sdiv "__compcert_i32_sdiv" sig_ii_i
+ /\ helper_declared p i32_udiv "__compcert_i32_udiv" sig_ii_i
+ /\ helper_declared p i32_smod "__compcert_i32_smod" sig_ii_i
+ /\ helper_declared p i32_umod "__compcert_i32_umod" sig_ii_i
+ /\ helper_declared p f32_div "__compcert_f32_div" sig_ss_s
+ /\ helper_declared p f64_div "__compcert_f64_div" sig_ff_f
+.
diff --git a/backend/PrintAsm.ml b/backend/PrintAsm.ml
index 22df68ae..7cc386ed 100644
--- a/backend/PrintAsm.ml
+++ b/backend/PrintAsm.ml
@@ -121,7 +121,7 @@ module Printer(Target:TARGET) =
let sec =
match C2C.atom_sections name with
| [s] -> s
- | _ -> Section_data Init
+ | _ -> Section_data (Init, false) (* FIX Sylvain: not sure of this fix *)
and align =
match C2C.atom_alignof name with
| Some a -> a
diff --git a/backend/PrintAsmaux.ml b/backend/PrintAsmaux.ml
index e39ba8aa..f1978ad2 100644
--- a/backend/PrintAsmaux.ml
+++ b/backend/PrintAsmaux.ml
@@ -111,6 +111,10 @@ let elf_symbol_offset oc (symb, ofs) =
if ofs <> 0L then fprintf oc " + %Ld" ofs
(* Functions for fun and var info *)
+let elf_text_print_fun_info oc name =
+ fprintf oc " .type %s, @function\n" name;
+ fprintf oc " .size %s, . - %s\n" name name
+
let elf_print_fun_info oc name =
fprintf oc " .type %a, @function\n" elf_symbol name;
fprintf oc " .size %a, . - %a\n" elf_symbol name elf_symbol name
@@ -328,3 +332,84 @@ let variable_section ~sec ?bss ?reloc ?(common = !Clflags.option_fcommon) i =
| Init -> sec
| Init_reloc ->
begin match reloc with Some s -> s | None -> sec end
+
+
+(* Profiling *)
+let profiling_table : (Digest.t, int) Hashtbl.t = Hashtbl.create 1000;;
+let next_profiling_position = ref 0;;
+let profiling_position (x : Digest.t) : int =
+ match Hashtbl.find_opt profiling_table x with
+ | None -> let y = !next_profiling_position in
+ next_profiling_position := succ y;
+ Hashtbl.replace profiling_table x y;
+ y
+ | Some y -> y;;
+
+let profiling_ids () =
+ let nr_items = !next_profiling_position in
+ let ar = Array.make nr_items "" in
+ Hashtbl.iter
+ (fun x y -> ar.(y) <- x)
+ profiling_table;
+ ar;;
+
+let print_profiling_id oc id =
+ assert (String.length id = 16);
+ output_string oc " .byte";
+ for i=0 to 15 do
+ fprintf oc " 0x%02x" (Char.code (String.get id i));
+ if i < 15 then output_char oc ','
+ done;
+ output_char oc '\n';;
+
+let profiling_counter_table_name = ".compcert_profiling_counters"
+and profiling_id_table_name = ".compcert_profiling_ids"
+and profiling_write_table = ".compcert_profiling_save_for_this_object"
+and profiling_init = ".compcert_profiling_init"
+and profiling_write_table_helper = "_compcert_write_profiling_table"
+and dtor_section = ".dtors.65435,\"aw\",@progbits"
+(* and fini_section = ".fini_array_00100,\"aw\"" *)
+and init_section = ".init_array,\"aw\"";;
+
+type finalizer_call_method =
+ | Dtors
+ | Init_atexit of (out_channel -> string -> unit);;
+
+let write_symbol_pointer oc sym =
+ if Archi.ptr64
+ then fprintf oc " .8byte %s\n" sym
+ else fprintf oc " .4byte %s\n" sym;;
+
+let print_profiling_epilogue declare_function finalizer_call_method print_profiling_stub oc =
+ if !Clflags.option_profile_arcs
+ then
+ let nr_items = !next_profiling_position in
+ if nr_items > 0
+ then
+ begin
+ fprintf oc " .lcomm %s, %d\n"
+ profiling_counter_table_name (nr_items * 16);
+ fprintf oc " .section .rodata\n";
+ fprintf oc "%s:\n" profiling_id_table_name;
+ Array.iter (print_profiling_id oc) (profiling_ids ());
+ fprintf oc " .text\n";
+ fprintf oc "%s:\n" profiling_write_table;
+ print_profiling_stub oc nr_items
+ profiling_id_table_name
+ profiling_counter_table_name;
+ declare_function oc profiling_write_table;
+ match finalizer_call_method with
+ | Dtors ->
+ fprintf oc " .section %s\n" dtor_section;
+ write_symbol_pointer oc profiling_write_table
+ | Init_atexit(atexit_call) ->
+ fprintf oc " .section %s\n" init_section;
+ write_symbol_pointer oc profiling_init;
+ fprintf oc " .text\n";
+ fprintf oc "%s:\n" profiling_init;
+ atexit_call oc profiling_write_table;
+ declare_function oc profiling_init
+ end;;
+
+let profiling_offset id kind =
+ ((profiling_position id)*2 + kind)*8;;
diff --git a/backend/PrintCminor.ml b/backend/PrintCminor.ml
index 59b340f7..9ca0e3a0 100644
--- a/backend/PrintCminor.ml
+++ b/backend/PrintCminor.ml
@@ -35,6 +35,7 @@ let precedence = function
| Ebinop((Oadd|Osub|Oaddf|Osubf|Oaddfs|Osubfs|Oaddl|Osubl), _, _) -> (12, LtoR)
| Ebinop((Oshl|Oshr|Oshru|Oshll|Oshrl|Oshrlu), _, _) -> (11, LtoR)
| Ebinop((Ocmp _|Ocmpu _|Ocmpf _|Ocmpfs _|Ocmpl _|Ocmplu _), _, _) -> (10, LtoR)
+ | Ebinop((Oexpect _), _, _) -> (9, LtoR)
| Ebinop((Oand|Oandl), _, _) -> (8, LtoR)
| Ebinop((Oxor|Oxorl), _, _) -> (7, LtoR)
| Ebinop((Oor|Oorl), _, _) -> (6, LtoR)
@@ -90,6 +91,7 @@ let comparison_name = function
| Cge -> ">="
let name_of_binop = function
+ | Oexpect _ -> "expect"
| Oadd -> "+"
| Osub -> "-"
| Omul -> "*"
diff --git a/backend/PrintLTL.ml b/backend/PrintLTL.ml
index d75ba19c..87e8a1fc 100644
--- a/backend/PrintLTL.ml
+++ b/backend/PrintLTL.ml
@@ -61,9 +61,10 @@ let print_succ pp s dfl =
let print_instruction pp succ = function
| Lop(op, args, res) ->
fprintf pp "%a = %a" mreg res (print_operation mreg) (op, args)
- | Lload(chunk, addr, args, dst) ->
- fprintf pp "%a = %s[%a]"
- mreg dst (name_of_chunk chunk) (print_addressing mreg) (addr, args)
+ | Lload(trap,chunk, addr, args, dst) ->
+ fprintf pp "%a = %s[%a]%a"
+ mreg dst (name_of_chunk chunk) (print_addressing mreg) (addr, args)
+ print_trapping_mode trap
| Lgetstack(sl, ofs, ty, dst) ->
fprintf pp "%a = %a" mreg dst slot (sl, ofs, ty)
| Lsetstack(src, sl, ofs, ty) ->
@@ -82,10 +83,11 @@ let print_instruction pp succ = function
(print_builtin_args loc) args
| Lbranch s ->
print_succ pp s succ
- | Lcond(cond, args, s1, s2) ->
- fprintf pp "if (%a) goto %d else goto %d"
+ | Lcond(cond, args, s1, s2, info) ->
+ fprintf pp "if (%a) goto %d else goto %d (prediction: %s)"
(print_condition mreg) (cond, args)
(P.to_int s1) (P.to_int s2)
+ (match info with None -> "none" | Some true -> "branch" | Some false -> "fallthrough")
| Ljumptable(arg, tbl) ->
let tbl = Array.of_list tbl in
fprintf pp "jumptable (%a)" mreg arg;
@@ -131,10 +133,10 @@ let print_program pp (prog: LTL.program) =
let destination : string option ref = ref None
-let print_if prog =
+let print_if passno prog =
match !destination with
| None -> ()
| Some f ->
- let oc = open_out f in
+ let oc = open_out (f ^ "." ^ Z.to_string passno) in
print_program oc prog;
close_out oc
diff --git a/backend/PrintMach.ml b/backend/PrintMach.ml
index 8a5f9a7c..3481421b 100644
--- a/backend/PrintMach.ml
+++ b/backend/PrintMach.ml
@@ -47,10 +47,11 @@ let print_instruction pp i =
| Mop(op, args, res) ->
fprintf pp "\t%a = %a\n"
reg res (PrintOp.print_operation reg) (op, args)
- | Mload(chunk, addr, args, dst) ->
- fprintf pp "\t%a = %s[%a]\n"
+ | Mload(trap, chunk, addr, args, dst) ->
+ fprintf pp "\t%a = %s[%a]%a\n"
reg dst (name_of_chunk chunk)
(PrintOp.print_addressing reg) (addr, args)
+ print_trapping_mode trap
| Mstore(chunk, addr, args, src) ->
fprintf pp "\t%s[%a] = %a\n"
(name_of_chunk chunk)
diff --git a/backend/PrintRTL.ml b/backend/PrintRTL.ml
index 841540b6..b2ef05ca 100644
--- a/backend/PrintRTL.ml
+++ b/backend/PrintRTL.ml
@@ -50,10 +50,11 @@ let print_instruction pp (pc, i) =
fprintf pp "%a = %a\n"
reg res (PrintOp.print_operation reg) (op, args);
print_succ pp s (pc - 1)
- | Iload(chunk, addr, args, dst, s) ->
- fprintf pp "%a = %s[%a]\n"
+ | Iload(trap, chunk, addr, args, dst, s) ->
+ fprintf pp "%a = %s[%a]%a\n"
reg dst (name_of_chunk chunk)
- (PrintOp.print_addressing reg) (addr, args);
+ (PrintOp.print_addressing reg) (addr, args)
+ print_trapping_mode trap;
print_succ pp s (pc - 1)
| Istore(chunk, addr, args, src, s) ->
fprintf pp "%s[%a] = %a\n"
@@ -74,10 +75,11 @@ let print_instruction pp (pc, i) =
(name_of_external ef)
(print_builtin_args reg) args;
print_succ pp s (pc - 1)
- | Icond(cond, args, s1, s2) ->
- fprintf pp "if (%a) goto %d else goto %d\n"
+ | Icond(cond, args, s1, s2, info) ->
+ fprintf pp "if (%a) goto %d else goto %d (prediction: %s)\n"
(PrintOp.print_condition reg) (cond, args)
(P.to_int s1) (P.to_int s2)
+ (match info with None -> "none" | Some true -> "branch" | Some false -> "fallthrough")
| Ijumptable(arg, tbl) ->
let tbl = Array.of_list tbl in
fprintf pp "jumptable (%a)\n" reg arg;
diff --git a/backend/PrintXTL.ml b/backend/PrintXTL.ml
index aeaef25e..6f2b1df9 100644
--- a/backend/PrintXTL.ml
+++ b/backend/PrintXTL.ml
@@ -86,9 +86,10 @@ let print_instruction pp succ = function
fprintf pp "(%a) = (%a) using %a, %a" vars dsts vars srcs var t1 var t2
| Xop(op, args, res) ->
fprintf pp "%a = %a" var res (print_operation var) (op, args)
- | Xload(chunk, addr, args, dst) ->
- fprintf pp "%a = %s[%a]"
- var dst (name_of_chunk chunk) (print_addressing var) (addr, args)
+ | Xload(trap, chunk, addr, args, dst) ->
+ fprintf pp "%a = %s[%a]%a"
+ var dst (name_of_chunk chunk) (print_addressing var) (addr, args)
+ print_trapping_mode trap
| Xstore(chunk, addr, args, src) ->
fprintf pp "%s[%a] = %a"
(name_of_chunk chunk) (print_addressing var) (addr, args) var src
@@ -103,7 +104,7 @@ let print_instruction pp succ = function
(print_builtin_args var) args
| Xbranch s ->
print_succ pp s succ
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, _) ->
fprintf pp "if (%a) goto %d else goto %d"
(print_condition var) (cond, args)
(P.to_int s1) (P.to_int s2)
diff --git a/backend/Profiling.v b/backend/Profiling.v
new file mode 100644
index 00000000..83e96311
--- /dev/null
+++ b/backend/Profiling.v
@@ -0,0 +1,77 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+
+Local Open Scope positive.
+
+Parameter function_id : function -> AST.profiling_id.
+Parameter branch_id : AST.profiling_id -> node -> AST.profiling_id.
+
+Section PER_FUNCTION_ID.
+ Variable f_id : AST.profiling_id.
+
+ Definition inject_profiling_call (prog : code)
+ (pc extra_pc ifso ifnot : node) : node * code :=
+ let id := branch_id f_id pc in
+ let extra_pc' := Pos.succ extra_pc in
+ let prog' := PTree.set extra_pc
+ (Ibuiltin (EF_profiling id 0%Z) nil BR_none ifnot) prog in
+ let prog'':= PTree.set extra_pc'
+ (Ibuiltin (EF_profiling id 1%Z) nil BR_none ifso) prog' in
+ (Pos.succ extra_pc', prog'').
+
+ Definition inject_at (prog : code) (pc extra_pc : node) : node * code :=
+ match PTree.get pc prog with
+ | Some (Icond cond args ifso ifnot expected) =>
+ inject_profiling_call
+ (PTree.set pc
+ (Icond cond args (Pos.succ extra_pc) extra_pc expected) prog)
+ pc extra_pc ifso ifnot
+ | _ => inject_profiling_call prog pc extra_pc 1 1 (* does not happen *)
+ end.
+
+ Definition inject_at' (already : node * code) pc :=
+ let (extra_pc, prog) := already in
+ inject_at prog pc extra_pc.
+
+ Definition inject_l (prog : code) extra_pc injections :=
+ List.fold_left (fun already (inject_pc : node) =>
+ inject_at' already inject_pc)
+ injections
+ (extra_pc, prog).
+
+ Definition gen_conditions (prog : code) :=
+ List.map fst (PTree.elements (PTree.filter1
+ (fun instr =>
+ match instr with
+ | Icond cond args ifso ifnot expected => true
+ | _ => false
+ end) prog)).
+End PER_FUNCTION_ID.
+
+Definition transf_function (f : function) : function :=
+ let max_pc := max_pc_function f in
+ let conditions := gen_conditions (fn_code f) in
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := snd (inject_l (function_id f) (fn_code f) (Pos.succ max_pc) conditions);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
diff --git a/backend/ProfilingExploit.v b/backend/ProfilingExploit.v
new file mode 100644
index 00000000..2325f582
--- /dev/null
+++ b/backend/ProfilingExploit.v
@@ -0,0 +1,42 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL.
+
+Local Open Scope positive.
+
+Parameter function_id : function -> AST.profiling_id.
+Parameter branch_id : AST.profiling_id -> node -> AST.profiling_id.
+Parameter condition_oracle : AST.profiling_id -> option bool.
+
+Definition transf_instr (f_id : AST.profiling_id)
+ (pc : node) (i : instruction) : instruction :=
+ match i with
+ | Icond cond args ifso ifnot None =>
+ Icond cond args ifso ifnot (condition_oracle (branch_id f_id pc))
+ | _ => i
+ end.
+
+Definition transf_function (f : function) : function :=
+ {| fn_sig := f.(fn_sig);
+ fn_params := f.(fn_params);
+ fn_stacksize := f.(fn_stacksize);
+ fn_code := PTree.map (transf_instr (function_id f)) f.(fn_code);
+ fn_entrypoint := f.(fn_entrypoint) |}.
+
+Definition transf_fundef (fd: fundef) : fundef :=
+ AST.transf_fundef transf_function fd.
+
+Definition transf_program (p: program) : program :=
+ transform_program transf_fundef p.
diff --git a/backend/ProfilingExploitproof.v b/backend/ProfilingExploitproof.v
new file mode 100644
index 00000000..78de09af
--- /dev/null
+++ b/backend/ProfilingExploitproof.v
@@ -0,0 +1,236 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import FunInd.
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import ProfilingExploit.
+
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; reflexivity.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i,
+ f.(fn_code)!pc = Some i ->
+ (transf_function f).(fn_code)!pc = Some(transf_instr (function_id f) pc i).
+Proof.
+ intros until i. intro Hcode.
+ unfold transf_function; simpl.
+ rewrite PTree.gmap.
+ unfold option_map.
+ rewrite Hcode.
+ reflexivity.
+Qed.
+
+Ltac TR_AT :=
+ match goal with
+ | [ A: (fn_code _)!_ = Some _ |- _ ] =>
+ generalize (transf_function_at _ _ _ A); intros
+ end.
+
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+ | match_frames_intro: forall res f sp pc rs,
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Lemma step_simulation:
+ forall S1 t S2, RTL.step ge S1 t S2 ->
+ forall S1', match_states S1 S1' ->
+ exists S2', RTL.step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ induction 1; intros S1' MS; inv MS; try TR_AT.
+- (* nop *)
+ econstructor; split. eapply exec_Inop; eauto.
+ constructor; auto.
+- (* op *)
+ econstructor; split.
+ eapply exec_Iop with (v := v); eauto.
+ rewrite <- H0. apply eval_operation_preserved. exact symbols_preserved.
+ constructor; auto.
+(* load *)
+- econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload; eauto.
+ constructor; auto.
+- (* load notrap1 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = None).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap1; eauto.
+ constructor; auto.
+- (* load notrap2 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap2; eauto.
+ constructor; auto.
+- (* store *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Istore; eauto.
+ constructor; auto.
+(* call *)
+- econstructor; split.
+ eapply exec_Icall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ constructor. constructor; auto. constructor.
+(* tailcall *)
+- econstructor; split.
+ eapply exec_Itailcall with (fd := transf_fundef fd); eauto.
+ eapply find_function_translated; eauto.
+ apply sig_preserved.
+ constructor. auto.
+(* builtin *)
+- econstructor; split.
+ eapply exec_Ibuiltin; eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+(* cond *)
+- destruct predb.
+ + econstructor; split.
+ eapply exec_Icond; eauto.
+ constructor; auto.
+ + simpl transf_instr in H1.
+ destruct condition_oracle in H1.
+ * econstructor; split.
+ eapply exec_Icond; eauto.
+ constructor; auto.
+ * econstructor; split.
+ eapply exec_Icond; eauto.
+ constructor; auto.
+(* jumptbl *)
+- econstructor; split.
+ eapply exec_Ijumptable; eauto.
+ constructor; auto.
+(* return *)
+- econstructor; split.
+ eapply exec_Ireturn; eauto.
+ constructor; auto.
+(* internal function *)
+- simpl. econstructor; split.
+ eapply exec_function_internal; eauto.
+ constructor; auto.
+(* external function *)
+- econstructor; split.
+ eapply exec_function_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ constructor; auto.
+(* return *)
+- inv STACKS. inv H1.
+ econstructor; split.
+ eapply exec_return; eauto.
+ constructor; auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_step.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/Profilingaux.ml b/backend/Profilingaux.ml
new file mode 100644
index 00000000..6ecea9e6
--- /dev/null
+++ b/backend/Profilingaux.ml
@@ -0,0 +1,85 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Camlcoq
+open RTL
+open Maps
+
+type identifier = Digest.t
+
+let pp_id channel (x : identifier) =
+ assert(String.length x = 16);
+ for i=0 to 15 do
+ Printf.fprintf channel "%02x" (Char.code (String.get x i))
+ done
+
+let print_anonymous_function pp f =
+ let instrs =
+ List.sort
+ (fun (pc1, _) (pc2, _) -> compare pc2 pc1)
+ (List.rev_map
+ (fun (pc, i) -> (P.to_int pc, i))
+ (PTree.elements f.fn_code)) in
+ PrintRTL.print_succ pp f.fn_entrypoint
+ (match instrs with (pc1, _) :: _ -> pc1 | [] -> -1);
+ List.iter (PrintRTL.print_instruction pp) instrs;
+ Printf.fprintf pp "}\n\n"
+
+let function_id (f : coq_function) : identifier =
+ let digest = Digest.string (Marshal.to_string f []) in
+ (*
+ Printf.fprintf stderr "FUNCTION hash = %a\n" pp_id digest;
+ print_anonymous_function stderr f;
+ *)
+ digest
+
+let branch_id (f_id : identifier) (node : P.t) : identifier =
+ Digest.string (f_id ^ (Int64.to_string (P.to_int64 node)));;
+
+let profiling_counts : (identifier, (Int64.t*Int64.t)) Hashtbl.t = Hashtbl.create 1000;;
+
+let get_counts id =
+ match Hashtbl.find_opt profiling_counts id with
+ | Some x -> x
+ | None -> (0L, 0L);;
+
+let add_profiling_counts id counter0 counter1 =
+ let (old0, old1) = get_counts id in
+ Hashtbl.replace profiling_counts id (Int64.add old0 counter0,
+ Int64.add old1 counter1);;
+
+let input_counter (ic : in_channel) : Int64.t =
+ let r = ref Int64.zero in
+ for i=0 to 7
+ do
+ r := Int64.add !r (Int64.shift_left (Int64.of_int (input_byte ic)) (8*i))
+ done;
+ !r;;
+
+let load_profiling_info (filename : string) : unit =
+ let ic = open_in filename in
+ try
+ while true do
+ let id : identifier = really_input_string ic 16 in
+ let counter0 = input_counter ic in
+ let counter1 = input_counter ic in
+ (* Printf.fprintf stderr "%a : %Ld %Ld\n" pp_id id counter0 counter1 *)
+ add_profiling_counts id counter0 counter1
+ done
+ with End_of_file -> close_in ic;;
+
+let condition_oracle (id : identifier) : bool option =
+ let (count0, count1) = get_counts id in
+ (* (if count0 <> 0L || count1 <> 0L then
+ Printf.fprintf stderr "%a : %Ld %Ld\n" pp_id id count0 count1); *)
+ if count0 = count1 then None
+ else Some(count1 > count0);;
diff --git a/backend/Profilingproof.v b/backend/Profilingproof.v
new file mode 100644
index 00000000..0cebc601
--- /dev/null
+++ b/backend/Profilingproof.v
@@ -0,0 +1,704 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import Profiling.
+Require Import Lia.
+
+Local Open Scope positive.
+
+Definition match_prog (p tp: RTL.program) :=
+ match_program (fun ctx f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p, match_prog p (transf_program p).
+Proof.
+ intros. eapply match_transform_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ Genv.find_funct tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_transf TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ Genv.find_funct_ptr tge v = Some (transf_fundef f).
+Proof (Genv.find_funct_ptr_transf TRANSL).
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_transf TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_transf TRANSL).
+
+Lemma sig_preserved:
+ forall f, funsig (transf_fundef f) = funsig f.
+Proof.
+ destruct f; reflexivity.
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs fd,
+ find_function ge ros rs = Some fd ->
+ find_function tge ros rs = Some (transf_fundef fd).
+Proof.
+ unfold find_function; intros. destruct ros as [r|id].
+ eapply functions_translated; eauto.
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try congruence.
+ eapply function_ptr_translated; eauto.
+Qed.
+
+Lemma pair_expand:
+ forall { A B : Type } (p : A*B),
+ p = ((fst p), (snd p)).
+Proof.
+ destruct p; simpl; trivial.
+Qed.
+
+Lemma inject_profiling_call_preserves:
+ forall id body pc extra_pc ifso ifnot pc0,
+ pc0 < extra_pc ->
+ PTree.get pc0 (snd (inject_profiling_call id body pc extra_pc ifso ifnot)) = PTree.get pc0 body.
+Proof.
+ intros. simpl.
+ rewrite PTree.gso by lia.
+ apply PTree.gso.
+ lia.
+Qed.
+
+Lemma inject_at_preserves :
+ forall id body pc extra_pc pc0,
+ pc0 < extra_pc ->
+ pc0 <> pc ->
+ PTree.get pc0 (snd (inject_at id body pc extra_pc)) = PTree.get pc0 body.
+Proof.
+ intros. unfold inject_at.
+ destruct (PTree.get pc body) eqn:GET.
+ - destruct i.
+ all: try (rewrite inject_profiling_call_preserves; trivial; fail).
+ rewrite inject_profiling_call_preserves by trivial.
+ apply PTree.gso; lia.
+ - apply inject_profiling_call_preserves; trivial.
+Qed.
+
+Lemma inject_profiling_call_increases:
+ forall id body pc extra_pc ifso ifnot,
+ fst (inject_profiling_call id body pc extra_pc ifso ifnot) = extra_pc + 2.
+Proof.
+ intros.
+ simpl.
+ rewrite <- (Pos2Nat.id (Pos.succ (Pos.succ extra_pc))).
+ rewrite <- (Pos2Nat.id (extra_pc + 2)).
+ rewrite !Pos2Nat.inj_succ.
+ rewrite !Pos2Nat.inj_add.
+ apply f_equal.
+ lia.
+Qed.
+
+Lemma inject_at_increases:
+ forall id body pc extra_pc,
+ (fst (inject_at id body pc extra_pc)) = extra_pc + 2.
+Proof.
+ intros. unfold inject_at.
+ destruct (PTree.get pc body).
+ - destruct i; apply inject_profiling_call_increases.
+ - apply inject_profiling_call_increases.
+Qed.
+
+Lemma inject_l_preserves :
+ forall id injections body extra_pc pc0,
+ pc0 < extra_pc ->
+ List.forallb (fun injection => if peq injection pc0 then false else true) injections = true ->
+ PTree.get pc0 (snd (inject_l id body extra_pc injections)) = PTree.get pc0 body.
+Proof.
+ induction injections;
+ intros until pc0; intros BEFORE ALL; simpl; trivial.
+ unfold inject_l.
+ simpl in ALL.
+ rewrite andb_true_iff in ALL.
+ destruct ALL as [NEQ ALL].
+ simpl.
+ rewrite pair_expand with (p := inject_at id body a extra_pc).
+ progress fold (inject_l id (snd (inject_at id body a extra_pc))
+ (fst (inject_at id body a extra_pc))
+ injections).
+ rewrite IHinjections; trivial.
+ - apply inject_at_preserves; trivial.
+ destruct (peq a pc0); congruence.
+ - rewrite inject_at_increases.
+ lia.
+Qed.
+
+Fixpoint inject_l_position extra_pc
+ (injections : list node)
+ (k : nat) {struct injections} : node :=
+ match injections with
+ | nil => extra_pc
+ | pc::l' =>
+ match k with
+ | O => extra_pc
+ | S k' => inject_l_position (extra_pc + 2) l' k'
+ end
+ end.
+
+Lemma inject_l_position_increases : forall injections pc k,
+ pc <= inject_l_position pc injections k.
+Proof.
+ induction injections; simpl; intros.
+ lia.
+ destruct k.
+ lia.
+ specialize IHinjections with (pc := pc + 2) (k := k).
+ lia.
+Qed.
+
+Lemma inject_l_injected_pc:
+ forall f_id injections cond args ifso ifnot expected body injnum pc extra_pc
+ (INSTR : body ! pc = Some (Icond cond args ifso ifnot expected))
+ (BELOW : forallb (fun pc => pc <? extra_pc) injections = true)
+ (NOREPET : list_norepet injections)
+ (NUMBER : nth_error injections injnum = Some pc),
+ PTree.get pc (snd (inject_l f_id body extra_pc injections)) =
+ Some (Icond cond args
+ (Pos.succ (inject_l_position extra_pc injections injnum))
+ (inject_l_position extra_pc injections injnum) expected).
+Proof.
+ induction injections; simpl; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER. }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ rewrite Pos.ltb_lt in BELOW1.
+ unfold inject_l.
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at f_id body a extra_pc).
+ progress fold (inject_l f_id (snd (inject_at f_id body a extra_pc))
+ (fst (inject_at f_id body a extra_pc))
+ injections).
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - unfold inject_at.
+ rewrite INSTR.
+ unfold inject_profiling_call. simpl.
+ rewrite PTree.gso by lia.
+ rewrite PTree.gso by lia.
+ apply PTree.gss.
+ - rewrite inject_at_increases.
+ lia.
+ - inv NOREPET.
+ rewrite forallb_forall.
+ intros x IN.
+ destruct peq as [EQ | ]; trivial.
+ subst x.
+ contradiction.
+ }
+ simpl.
+ rewrite inject_at_increases.
+ apply IHinjections with (ifso := ifso) (ifnot := ifnot).
+ - rewrite inject_at_preserves; trivial.
+ + rewrite forallb_forall in BELOW2.
+ rewrite <- Pos.ltb_lt.
+ apply nth_error_In in NUMBER.
+ auto.
+ + inv NOREPET.
+ intro ZZZ.
+ subst a.
+ apply nth_error_In in NUMBER.
+ auto.
+
+ - rewrite forallb_forall in BELOW2.
+ rewrite forallb_forall.
+ intros.
+ specialize BELOW2 with x.
+ rewrite Pos.ltb_lt in *.
+ intuition lia.
+ - inv NOREPET. trivial.
+ - trivial.
+Qed.
+
+Lemma inject_l_injected0:
+ forall f_id cond args ifso ifnot expected injections body injnum pc extra_pc
+ (INSTR : body ! pc = Some (Icond cond args ifso ifnot expected))
+ (BELOW : forallb (fun pc => pc <? extra_pc) injections = true)
+ (NOREPET : list_norepet injections)
+ (NUMBER : nth_error injections injnum = Some pc),
+ PTree.get (inject_l_position extra_pc injections injnum)
+ (snd (inject_l f_id body extra_pc injections)) =
+ Some (Ibuiltin (EF_profiling (branch_id f_id pc) 0%Z) nil BR_none ifnot).
+Proof.
+ induction injections; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER. }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ unfold inject_l.
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at f_id body a extra_pc).
+ progress fold (inject_l f_id (snd (inject_at f_id body a extra_pc))
+ (fst (inject_at f_id body a extra_pc))
+ injections).
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - unfold inject_at.
+ rewrite INSTR.
+ unfold inject_profiling_call. simpl.
+ rewrite PTree.gso by lia.
+ apply PTree.gss.
+ - rewrite inject_at_increases.
+ lia.
+ - rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ destruct peq as [EQ | ]; trivial.
+ rewrite EQ in IN.
+ rewrite Pos.ltb_lt in IN.
+ lia.
+ }
+ simpl.
+ rewrite inject_at_increases.
+
+ apply IHinjections.
+ - rewrite inject_at_preserves; trivial.
+ + rewrite forallb_forall in BELOW2.
+ rewrite <- Pos.ltb_lt.
+ apply nth_error_In in NUMBER.
+ auto.
+ + inv NOREPET.
+ intro ZZZ.
+ subst a.
+ apply nth_error_In in NUMBER.
+ auto.
+
+ - rewrite forallb_forall in BELOW2.
+ rewrite forallb_forall.
+ intros.
+ specialize BELOW2 with x.
+ rewrite Pos.ltb_lt in *.
+ intuition lia.
+ - inv NOREPET. trivial.
+ - trivial.
+Qed.
+
+Lemma inject_l_injected1:
+ forall f_id cond args ifso ifnot expected injections body injnum pc extra_pc
+ (INSTR : body ! pc = Some (Icond cond args ifso ifnot expected))
+ (BELOW : forallb (fun pc => pc <? extra_pc) injections = true)
+ (NOREPET : list_norepet injections)
+ (NUMBER : nth_error injections injnum = Some pc),
+ PTree.get (Pos.succ (inject_l_position extra_pc injections injnum))
+ (snd (inject_l f_id body extra_pc injections)) =
+ Some (Ibuiltin (EF_profiling (branch_id f_id pc) 1%Z) nil BR_none ifso).
+Proof.
+ induction injections; intros.
+ { rewrite nth_error_nil in NUMBER.
+ discriminate NUMBER. }
+ simpl in BELOW.
+ rewrite andb_true_iff in BELOW.
+ destruct BELOW as [BELOW1 BELOW2].
+ unfold inject_l.
+ simpl fold_left.
+ rewrite pair_expand with (p := inject_at f_id body a extra_pc).
+ progress fold (inject_l f_id (snd (inject_at f_id body a extra_pc))
+ (fst (inject_at f_id body a extra_pc))
+ injections).
+ destruct injnum as [ | injnum']; simpl in NUMBER.
+ { inv NUMBER.
+ rewrite inject_l_preserves; simpl.
+ - unfold inject_at.
+ rewrite INSTR.
+ unfold inject_profiling_call. simpl.
+ apply PTree.gss.
+ - rewrite inject_at_increases.
+ lia.
+ - rewrite forallb_forall.
+ rewrite forallb_forall in BELOW2.
+ intros loc IN.
+ specialize BELOW2 with loc.
+ apply BELOW2 in IN.
+ destruct peq as [EQ | ]; trivial.
+ rewrite EQ in IN.
+ rewrite Pos.ltb_lt in IN.
+ lia.
+ }
+ simpl.
+ rewrite inject_at_increases.
+
+ apply IHinjections.
+ - rewrite inject_at_preserves; trivial.
+ + rewrite forallb_forall in BELOW2.
+ rewrite <- Pos.ltb_lt.
+ apply nth_error_In in NUMBER.
+ auto.
+ + inv NOREPET.
+ intro ZZZ.
+ subst a.
+ apply nth_error_In in NUMBER.
+ auto.
+
+ - rewrite forallb_forall in BELOW2.
+ rewrite forallb_forall.
+ intros.
+ specialize BELOW2 with x.
+ rewrite Pos.ltb_lt in *.
+ intuition lia.
+ - inv NOREPET. trivial.
+ - trivial.
+Qed.
+
+Lemma transf_function_at:
+ forall f pc i
+ (CODE : f.(fn_code)!pc = Some i)
+ (INSTR : match i with
+ | Icond _ _ _ _ _ => False
+ | _ => True
+ end),
+ (transf_function f).(fn_code)!pc = Some i.
+Proof.
+ intros.
+ unfold transf_function; simpl.
+ rewrite inject_l_preserves.
+ assumption.
+ - pose proof (max_pc_function_sound f pc i CODE) as LE.
+ unfold Ple in LE.
+ lia.
+ - rewrite forallb_forall.
+ intros x IN.
+ destruct peq; trivial.
+ subst x.
+ unfold gen_conditions in IN.
+ rewrite in_map_iff in IN.
+ destruct IN as [[pc' i'] [EQ IN]].
+ simpl in EQ.
+ subst pc'.
+ apply PTree.elements_complete in IN.
+ rewrite PTree.gfilter1 in IN.
+ rewrite CODE in IN.
+ destruct i; try discriminate; contradiction.
+Qed.
+
+Inductive match_frames: RTL.stackframe -> RTL.stackframe -> Prop :=
+| match_frames_intro: forall res f sp pc rs,
+ match_frames (Stackframe res f sp pc rs)
+ (Stackframe res (transf_function f) sp pc rs).
+
+Inductive match_states: RTL.state -> RTL.state -> Prop :=
+ | match_regular_states: forall stk f sp pc rs m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (State stk f sp pc rs m)
+ (State stk' (transf_function f) sp pc rs m)
+ | match_callstates: forall stk f args m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Callstate stk f args m)
+ (Callstate stk' (transf_fundef f) args m)
+ | match_returnstates: forall stk v m stk'
+ (STACKS: list_forall2 match_frames stk stk'),
+ match_states (Returnstate stk v m)
+ (Returnstate stk' v m).
+
+Lemma funsig_preserved:
+ forall fd,
+ funsig (transf_fundef fd) = funsig fd.
+Proof.
+ destruct fd; simpl; trivial.
+Qed.
+
+Lemma stacksize_preserved:
+ forall f,
+ fn_stacksize (transf_function f) = fn_stacksize f.
+Proof.
+ destruct f; simpl; trivial.
+Qed.
+
+Hint Resolve symbols_preserved funsig_preserved external_call_symbols_preserved senv_preserved stacksize_preserved : profiling.
+
+Lemma step_simulation:
+ forall s1 t s2 (STEP : step ge s1 t s2)
+ s1' (MS: match_states s1 s1'),
+ exists s2', plus step tge s1' t s2' /\ match_states s2 s2'.
+Proof.
+ induction 1; intros; inv MS.
+ - econstructor; split.
+ + apply plus_one. apply exec_Inop.
+ erewrite transf_function_at; eauto. apply I.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Iop with (op:=op) (args:=args).
+ * erewrite transf_function_at; eauto. apply I.
+ * rewrite eval_operation_preserved with (ge1:=ge);
+ eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Iload with (trap:=trap) (chunk:=chunk)
+ (addr:=addr) (args:=args) (a:=a).
+ erewrite transf_function_at; eauto. apply I.
+ rewrite eval_addressing_preserved with (ge1:=ge).
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Iload_notrap1 with (chunk:=chunk)
+ (addr:=addr) (args:=args).
+ erewrite transf_function_at; eauto. apply I.
+ rewrite eval_addressing_preserved with (ge1:=ge).
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Iload_notrap2 with (chunk:=chunk)
+ (addr:=addr) (args:=args) (a:=a).
+ erewrite transf_function_at; eauto. apply I.
+ rewrite eval_addressing_preserved with (ge1:=ge).
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Istore with (chunk:=chunk) (src := src)
+ (addr:=addr) (args:=args) (a:=a).
+ erewrite transf_function_at; eauto. apply I.
+ rewrite eval_addressing_preserved with (ge1:=ge).
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_Icall with (sig:=(funsig fd)) (ros:=ros).
+ erewrite transf_function_at; eauto. apply I.
+ apply find_function_translated with (fd := fd).
+ all: eauto with profiling.
+ + constructor; auto.
+ constructor; auto.
+ constructor.
+ - econstructor; split.
+ + apply plus_one. apply exec_Itailcall with (sig:=(funsig fd)) (ros:=ros).
+ erewrite transf_function_at; eauto. apply I.
+ apply find_function_translated with (fd := fd).
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one.
+ apply exec_Ibuiltin with (ef:=ef) (args:=args) (vargs:=vargs).
+ erewrite transf_function_at; eauto. apply I.
+ apply eval_builtin_args_preserved with (ge1:=ge).
+ all: eauto with profiling.
+ + constructor; auto.
+ - destruct b.
+ + assert (In pc (gen_conditions (fn_code f))) as IN.
+ { unfold gen_conditions.
+ rewrite in_map_iff.
+ exists (pc, (Icond cond args ifso ifnot predb)).
+ split; simpl; trivial.
+ apply PTree.elements_correct.
+ rewrite PTree.gfilter1.
+ rewrite H.
+ reflexivity.
+ }
+ apply In_nth_error in IN.
+ destruct IN as [n IN].
+ econstructor; split.
+ * eapply plus_two.
+ ++ eapply exec_Icond with (cond := cond) (args := args) (predb := predb) (b := true).
+ unfold transf_function. simpl.
+ erewrite inject_l_injected_pc with (cond := cond) (args := args).
+ ** reflexivity.
+ ** eassumption.
+ ** unfold gen_conditions.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite in_map_iff in INx.
+ destruct INx as [[x' i'] [EQ INx]].
+ simpl in EQ.
+ subst x'.
+ apply PTree.elements_complete in INx.
+ rewrite PTree.gfilter1 in INx.
+ assert (x <= max_pc_function f) as MAX.
+ { destruct ((fn_code f) ! x) eqn:CODEx.
+ 2: discriminate.
+ apply max_pc_function_sound with (i:=i).
+ assumption.
+ }
+ rewrite Pos.ltb_lt.
+ lia.
+ ** unfold gen_conditions.
+ apply PTree.elements_keys_norepet.
+ ** exact IN.
+ ** assumption.
+ ** reflexivity.
+ ++ apply exec_Ibuiltin with (ef := (EF_profiling (branch_id (function_id f) pc) 1%Z)) (args := nil) (vargs := nil).
+ apply inject_l_injected1 with (cond := cond) (args := args) (ifso := ifso) (ifnot := ifnot) (expected := predb).
+ ** exact H.
+ ** unfold gen_conditions.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite in_map_iff in INx.
+ destruct INx as [[x' i'] [EQ INx]].
+ simpl in EQ.
+ subst x'.
+ apply PTree.elements_complete in INx.
+ rewrite PTree.gfilter1 in INx.
+ assert (x <= max_pc_function f) as MAX.
+ { destruct ((fn_code f) ! x) eqn:CODEx.
+ 2: discriminate.
+ apply max_pc_function_sound with (i:=i).
+ assumption.
+ }
+ rewrite Pos.ltb_lt.
+ lia.
+ ** unfold gen_conditions.
+ apply PTree.elements_keys_norepet.
+ ** exact IN.
+ ** constructor.
+ ** constructor.
+ ++ reflexivity.
+ * simpl. constructor; auto.
+
+ + assert (In pc (gen_conditions (fn_code f))) as IN.
+ { unfold gen_conditions.
+ rewrite in_map_iff.
+ exists (pc, (Icond cond args ifso ifnot predb)).
+ split; simpl; trivial.
+ apply PTree.elements_correct.
+ rewrite PTree.gfilter1.
+ rewrite H.
+ reflexivity.
+ }
+ apply In_nth_error in IN.
+ destruct IN as [n IN].
+ econstructor; split.
+ * eapply plus_two.
+ ++ eapply exec_Icond with (cond := cond) (args := args) (predb := predb) (b := false).
+ unfold transf_function. simpl.
+ erewrite inject_l_injected_pc with (cond := cond) (args := args).
+ ** reflexivity.
+ ** eassumption.
+ ** unfold gen_conditions.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite in_map_iff in INx.
+ destruct INx as [[x' i'] [EQ INx]].
+ simpl in EQ.
+ subst x'.
+ apply PTree.elements_complete in INx.
+ rewrite PTree.gfilter1 in INx.
+ assert (x <= max_pc_function f) as MAX.
+ { destruct ((fn_code f) ! x) eqn:CODEx.
+ 2: discriminate.
+ apply max_pc_function_sound with (i:=i).
+ assumption.
+ }
+ rewrite Pos.ltb_lt.
+ lia.
+ ** unfold gen_conditions.
+ apply PTree.elements_keys_norepet.
+ ** exact IN.
+ ** assumption.
+ ** reflexivity.
+ ++ apply exec_Ibuiltin with (ef := (EF_profiling (branch_id (function_id f) pc) 0%Z)) (args := nil) (vargs := nil).
+ apply inject_l_injected0 with (cond := cond) (args := args) (ifso := ifso) (ifnot := ifnot) (expected := predb).
+ ** exact H.
+ ** unfold gen_conditions.
+ rewrite forallb_forall.
+ intros x INx.
+ rewrite in_map_iff in INx.
+ destruct INx as [[x' i'] [EQ INx]].
+ simpl in EQ.
+ subst x'.
+ apply PTree.elements_complete in INx.
+ rewrite PTree.gfilter1 in INx.
+ assert (x <= max_pc_function f) as MAX.
+ { destruct ((fn_code f) ! x) eqn:CODEx.
+ 2: discriminate.
+ apply max_pc_function_sound with (i:=i).
+ assumption.
+ }
+ rewrite Pos.ltb_lt.
+ lia.
+ ** unfold gen_conditions.
+ apply PTree.elements_keys_norepet.
+ ** exact IN.
+ ** constructor.
+ ** constructor.
+ ++ reflexivity.
+ * simpl. constructor; auto.
+
+ - econstructor; split.
+ + apply plus_one.
+ apply exec_Ijumptable with (arg:=arg) (tbl:=tbl) (n:=n).
+ erewrite transf_function_at; eauto. apply I.
+ all: eauto with profiling.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one.
+ apply exec_Ireturn.
+ erewrite transf_function_at; eauto. apply I.
+ rewrite stacksize_preserved. eassumption.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_function_internal.
+ rewrite stacksize_preserved. eassumption.
+ + constructor; auto.
+ - econstructor; split.
+ + apply plus_one. apply exec_function_external.
+ eauto with profiling.
+ + constructor; auto.
+ - inv STACKS. inv H1.
+ econstructor; split.
+ + apply plus_one. apply exec_return.
+ + constructor; auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall S1, RTL.initial_state prog S1 ->
+ exists S2, RTL.initial_state tprog S2 /\ match_states S1 S2.
+Proof.
+ intros. inv H. econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf TRANSL); eauto.
+ rewrite symbols_preserved. rewrite (match_program_main TRANSL). eauto.
+ eapply function_ptr_translated; eauto.
+ rewrite <- H3; apply sig_preserved.
+ constructor. constructor.
+Qed.
+
+Lemma transf_final_states:
+ forall S1 S2 r, match_states S1 S2 -> RTL.final_state S1 r -> RTL.final_state S2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_plus.
+ apply senv_preserved.
+ eexact transf_initial_states.
+ eexact transf_final_states.
+ exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/RTL.v b/backend/RTL.v
index a022f55a..fe350adf 100644
--- a/backend/RTL.v
+++ b/backend/RTL.v
@@ -43,11 +43,12 @@ Inductive instruction: Type :=
(** [Iop op args dest succ] performs the arithmetic operation [op]
over the values of registers [args], stores the result in [dest],
and branches to [succ]. *)
- | Iload: memory_chunk -> addressing -> list reg -> reg -> node -> instruction
- (** [Iload chunk addr args dest succ] loads a [chunk] quantity from
+ | Iload: trapping_mode -> memory_chunk -> addressing -> list reg -> reg -> node -> instruction
+ (** [Iload trap chunk addr args dest succ] loads a [chunk] quantity from
the address determined by the addressing mode [addr] and the
values of the [args] registers, stores the quantity just read
- into [dest], and branches to [succ]. *)
+ into [dest], and branches to [succ].
+ If trap=NOTRAP, then failures lead to a default value written to [dest]. *)
| Istore: memory_chunk -> addressing -> list reg -> reg -> node -> instruction
(** [Istore chunk addr args src succ] stores the value of register
[src] in the [chunk] quantity at the
@@ -66,11 +67,12 @@ Inductive instruction: Type :=
(** [Ibuiltin ef args dest succ] calls the built-in function
identified by [ef], giving it the values of [args] as arguments.
It stores the return value in [dest] and branches to [succ]. *)
- | Icond: condition -> list reg -> node -> node -> instruction
- (** [Icond cond args ifso ifnot] evaluates the boolean condition
+ | Icond: condition -> list reg -> node -> node -> option bool -> instruction
+ (** [Icond cond args ifso ifnot info] evaluates the boolean condition
[cond] over the values of registers [args]. If the condition
is true, it transitions to [ifso]. If the condition is false,
- it transitions to [ifnot]. *)
+ it transitions to [ifnot]. [info] is a ghost field there to provide
+ information relative to branch prediction. *)
| Ijumptable: reg -> list node -> instruction
(** [Ijumptable arg tbl] transitions to the node that is the [n]-th
element of the list [tbl], where [n] is the unsigned integer
@@ -212,12 +214,25 @@ Inductive step: state -> trace -> state -> Prop :=
step (State s f sp pc rs m)
E0 (State s f sp pc' (rs#res <- v) m)
| exec_Iload:
- forall s f sp pc rs m chunk addr args dst pc' a v,
- (fn_code f)!pc = Some(Iload chunk addr args dst pc') ->
+ forall s f sp pc rs m trap chunk addr args dst pc' a v,
+ (fn_code f)!pc = Some(Iload trap chunk addr args dst pc') ->
eval_addressing ge sp addr rs##args = Some a ->
Mem.loadv chunk m a = Some v ->
step (State s f sp pc rs m)
E0 (State s f sp pc' (rs#dst <- v) m)
+ | exec_Iload_notrap1:
+ forall s f sp pc rs m chunk addr args dst pc',
+ (fn_code f)!pc = Some(Iload NOTRAP chunk addr args dst pc') ->
+ eval_addressing ge sp addr rs##args = None ->
+ step (State s f sp pc rs m)
+ E0 (State s f sp pc' (rs#dst <- Vundef) m)
+ | exec_Iload_notrap2:
+ forall s f sp pc rs m chunk addr args dst pc' a,
+ (fn_code f)!pc = Some(Iload NOTRAP chunk addr args dst pc') ->
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = None->
+ step (State s f sp pc rs m)
+ E0 (State s f sp pc' (rs#dst <- Vundef) m)
| exec_Istore:
forall s f sp pc rs m chunk addr args src pc' a m',
(fn_code f)!pc = Some(Istore chunk addr args src pc') ->
@@ -248,8 +263,8 @@ Inductive step: state -> trace -> state -> Prop :=
step (State s f sp pc rs m)
t (State s f sp pc' (regmap_setres res vres rs) m')
| exec_Icond:
- forall s f sp pc rs m cond args ifso ifnot b pc',
- (fn_code f)!pc = Some(Icond cond args ifso ifnot) ->
+ forall s f sp pc rs m cond args ifso ifnot b pc' predb,
+ (fn_code f)!pc = Some(Icond cond args ifso ifnot predb) ->
eval_condition cond rs##args m = Some b ->
pc' = (if b then ifso else ifnot) ->
step (State s f sp pc rs m)
@@ -299,8 +314,8 @@ Proof.
Qed.
Lemma exec_Iload':
- forall s f sp pc rs m chunk addr args dst pc' rs' a v,
- (fn_code f)!pc = Some(Iload chunk addr args dst pc') ->
+ forall s f sp pc rs m trap chunk addr args dst pc' rs' a v,
+ (fn_code f)!pc = Some(Iload trap chunk addr args dst pc') ->
eval_addressing ge sp addr rs##args = Some a ->
Mem.loadv chunk m a = Some v ->
rs' = (rs#dst <- v) ->
@@ -384,12 +399,12 @@ Definition successors_instr (i: instruction) : list node :=
match i with
| Inop s => s :: nil
| Iop op args res s => s :: nil
- | Iload chunk addr args dst s => s :: nil
+ | Iload trap chunk addr args dst s => s :: nil
| Istore chunk addr args src s => s :: nil
| Icall sig ros args res s => s :: nil
| Itailcall sig ros args => nil
| Ibuiltin ef args res s => s :: nil
- | Icond cond args ifso ifnot => ifso :: ifnot :: nil
+ | Icond cond args ifso ifnot _ => ifso :: ifnot :: nil
| Ijumptable arg tbl => tbl
| Ireturn optarg => nil
end.
@@ -403,14 +418,14 @@ Definition instr_uses (i: instruction) : list reg :=
match i with
| Inop s => nil
| Iop op args res s => args
- | Iload chunk addr args dst s => args
+ | Iload trap chunk addr args dst s => args
| Istore chunk addr args src s => src :: args
| Icall sig (inl r) args res s => r :: args
| Icall sig (inr id) args res s => args
| Itailcall sig (inl r) args => r :: args
| Itailcall sig (inr id) args => args
| Ibuiltin ef args res s => params_of_builtin_args args
- | Icond cond args ifso ifnot => args
+ | Icond cond args ifso ifnot _ => args
| Ijumptable arg tbl => arg :: nil
| Ireturn None => nil
| Ireturn (Some arg) => arg :: nil
@@ -422,13 +437,13 @@ Definition instr_defs (i: instruction) : option reg :=
match i with
| Inop s => None
| Iop op args res s => Some res
- | Iload chunk addr args dst s => Some dst
+ | Iload trap chunk addr args dst s => Some dst
| Istore chunk addr args src s => None
| Icall sig ros args res s => Some res
| Itailcall sig ros args => None
| Ibuiltin ef args res s =>
match res with BR r => Some r | _ => None end
- | Icond cond args ifso ifnot => None
+ | Icond cond args ifso ifnot _ => None
| Ijumptable arg tbl => None
| Ireturn optarg => None
end.
@@ -462,7 +477,7 @@ Definition max_reg_instr (m: positive) (pc: node) (i: instruction) :=
match i with
| Inop s => m
| Iop op args res s => fold_left Pos.max args (Pos.max res m)
- | Iload chunk addr args dst s => fold_left Pos.max args (Pos.max dst m)
+ | Iload trap chunk addr args dst s => fold_left Pos.max args (Pos.max dst m)
| Istore chunk addr args src s => fold_left Pos.max args (Pos.max src m)
| Icall sig (inl r) args res s => fold_left Pos.max args (Pos.max r (Pos.max res m))
| Icall sig (inr id) args res s => fold_left Pos.max args (Pos.max res m)
@@ -471,7 +486,7 @@ Definition max_reg_instr (m: positive) (pc: node) (i: instruction) :=
| Ibuiltin ef args res s =>
fold_left Pos.max (params_of_builtin_args args)
(fold_left Pos.max (params_of_builtin_res res) m)
- | Icond cond args ifso ifnot => fold_left Pos.max args m
+ | Icond cond args ifso ifnot _ => fold_left Pos.max args m
| Ijumptable arg tbl => Pos.max arg m
| Ireturn None => m
| Ireturn (Some arg) => Pos.max arg m
diff --git a/backend/RTLTunneling.v b/backend/RTLTunneling.v
new file mode 100644
index 00000000..878e079f
--- /dev/null
+++ b/backend/RTLTunneling.v
@@ -0,0 +1,121 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Pierre Goutagny ENS-Lyon, VERIMAG *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Branch tunneling for the RTL representation *)
+
+Require Import Coqlib Maps Errors.
+Require Import AST.
+Require Import RTL.
+
+(* This is a port of tunneling for LTL. See LTLTunneling.v *)
+
+Definition UF := PTree.t (node * Z).
+
+(* The oracle returns a map of "nop" node to their target with a distance (ie the number of the "nop" node on the path) to the target. *)
+Axiom branch_target: RTL.function -> UF.
+Extract Constant branch_target => "RTLTunnelingaux.branch_target".
+
+Local Open Scope error_monad_scope.
+
+Definition get (td: UF) (pc: node): node*Z :=
+ match td!pc with
+ | Some (t,d) => (t,Z.abs d)
+ | None => (pc,0)
+ end.
+
+
+Definition target (td: UF) (pc: node): node := fst (get td pc).
+Coercion target: UF >-> Funclass.
+
+(* we check that the domain of [td] is included in the domain of [c] *)
+
+Definition check_included (td: UF) (c: code): option instruction
+ := PTree.fold (fun (ok:option instruction) pc _ => if ok then c!pc else None) td (Some (Inop xH)).
+
+(* we check the validity of targets and their bound:
+* the distance of a "nop" node (w.r.t to the target) must be greater than the one of its parents.
+*)
+Definition check_instr (td: UF) (pc: node) (i: instruction): res unit :=
+ match td!pc with
+ | None => OK tt
+ | Some (tpc, dpc) =>
+ let dpc := Z.abs dpc in
+ match i with
+ | Inop s =>
+ let (ts,ds) := get td s in
+ if peq tpc ts then
+ if zlt ds dpc then OK tt
+ else Error (msg "bad distance in Inop")
+ else Error (msg "invalid skip of Inop")
+ | Icond _ _ ifso ifnot _ =>
+ let (tso,dso) := get td ifso in
+ let (tnot,dnot) := get td ifnot in
+ if peq tpc tso then
+ if peq tpc tnot then
+ if zlt dso dpc then
+ if zlt dnot dpc then OK tt
+ else Error (msg "bad distance on else branch")
+ else Error (msg "bad distance on then branch")
+ else Error (msg "invalid skip of else branch")
+ else Error (msg "invalid skip of then branch")
+ | _ => Error (msg "cannot skip this instruction")
+ end
+ end.
+
+Definition check_code (td: UF) (c: code): res unit :=
+ PTree.fold (fun ok pc i => do _ <- ok; check_instr td pc i) c (OK tt).
+
+(* The second pass rewrites all LTL instructions, replacing every
+ * successor [s] of every instruction by [t s], the canonical representative
+ * of its equivalence class in the union-find data structure.
+ *)
+
+Definition tunnel_instr (t: node -> node) (i: instruction) : instruction :=
+ match i with
+ | Inop s => Inop (t s)
+ | Iop op args res s => Iop op args res (t s)
+ | Iload trap chunk addr args dst s => Iload trap chunk addr args dst (t s)
+ | Istore chunk addr args src s => Istore chunk addr args src (t s)
+ | Icall sig ros args res s => Icall sig ros args res (t s)
+ | Ibuiltin ef args res s => Ibuiltin ef args res (t s)
+ | Icond cond args ifso ifnot info =>
+ let ifso' := t ifso in
+ let ifnot' := t ifnot in
+ if peq ifso' ifnot'
+
+ then Inop ifso'
+ else Icond cond args ifso' ifnot' info
+ | Ijumptable arg tbl => Ijumptable arg (List.map t tbl)
+ | _ => i
+ end.
+
+Definition tunnel_function (f: RTL.function): res RTL.function :=
+ let td := branch_target f in
+ let c := fn_code f in
+ if check_included td c then
+ do _ <- check_code td c ; OK
+ (mkfunction
+ (fn_sig f)
+ (fn_params f)
+ (fn_stacksize f)
+ (PTree.map1 (tunnel_instr td) c)
+ (td (fn_entrypoint f)))
+ else Error (msg "Some node of the union-find is not in the CFG").
+
+Definition tunnel_fundef (f: fundef): res fundef :=
+ transf_partial_fundef tunnel_function f.
+
+Definition transf_program (p: program): res program :=
+ transform_partial_program tunnel_fundef p.
+
diff --git a/backend/RTLTunnelingaux.ml b/backend/RTLTunnelingaux.ml
new file mode 100644
index 00000000..43d4bf9f
--- /dev/null
+++ b/backend/RTLTunnelingaux.ml
@@ -0,0 +1,112 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Pierre Goutagny ENS-Lyon, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+
+This file implements the [branch_target] oracle that identifies "nop" branches in a RTL function,
+and computes their target node with the distance (ie the number of cummulated nops) toward this target.
+
+See [RTLTunneling.v]
+
+*)
+
+open Coqlib
+open RTL
+open Maps
+open Camlcoq
+open Tunnelinglibs
+
+module LANG = struct
+ type code_unit = RTL.instruction
+ type funct = RTL.coq_function
+end
+
+module OPT = struct
+ let langname = "RTL"
+ let limit_tunneling = None
+ let debug_flag = ref false
+ let final_dump = false
+end
+
+module Partial = Tunnelinglibs.Tunneling(LANG)(OPT)
+
+module FUNS = struct
+ let build_simplified_cfg c acc pc i =
+ match i with
+ | Inop s ->
+ let ns = get_node c s in
+ set_branch c pc ns;
+ incr nopcounter;
+ acc
+ | Icond (_, _, s1, s2, _) ->
+ c.num_rems <- c.num_rems + 1;
+ let ns1 = get_node c s1 in
+ let ns2 = get_node c s2 in
+ let npc = get_node c pc in
+ npc.inst <- COND(ns1, ns2);
+ npc::acc
+ | _ -> acc
+
+ let print_code_unit c println (pc, i) =
+ match i with
+ | Inop s -> (if println then Partial.debug "\n");
+ Partial.debug "%d:Inop %d %s\n" pc (P.to_int s) (string_of_labeli c.nodes pc);
+ false
+ | Icond (_, _, s1, s2, _) -> (if println then Partial.debug "\n");
+ Partial.debug "%d:Icond (%d,%d) %s\n" pc (P.to_int s1) (P.to_int s2) (string_of_labeli c.nodes pc);
+ false
+ | _ -> Partial.debug "%d " pc;
+ true
+
+ let fn_code f = f.fn_code
+ let fn_entrypoint f = f.fn_entrypoint
+
+
+ (*************************************************************)
+ (* Copy-paste of the extracted code of the verifier *)
+ (* with [raise (BugOnPC (P.to_int pc))] instead of [Error.*] *)
+
+ let check_code_unit td pc i =
+ match PTree.get pc td with
+ | Some p ->
+ let (tpc, dpc) = p in
+ let dpc0 = dpc in begin
+ match i with
+ | Inop s ->
+ let (ts, ds) = get td s in
+ if peq tpc ts
+ then if zlt ds dpc0
+ then ()
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ | Icond (_, _, s1, s2, _) ->
+ let (ts1, ds1) = get td s1 in
+ let (ts2, ds2) = get td s2 in
+ if peq tpc ts1
+ then if peq tpc ts2
+ then if zlt ds1 dpc0
+ then if zlt ds2 dpc0
+ then ()
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ else raise (BugOnPC (P.to_int pc))
+ | _ ->
+ raise (BugOnPC (P.to_int pc)) end
+ | None -> ()
+
+end
+
+module T = Partial.T(FUNS)
+let branch_target = T.branch_target
+
diff --git a/backend/RTLTunnelingproof.v b/backend/RTLTunnelingproof.v
new file mode 100644
index 00000000..0861143b
--- /dev/null
+++ b/backend/RTLTunnelingproof.v
@@ -0,0 +1,609 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Pierre Goutagny ENS-Lyon, VERIMAG *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Correctness proof for the branch tunneling optimization for RTL. *)
+(* This is a port of Tunnelingproof.v, the same optimisation for LTL. *)
+
+Require Import Coqlib Maps Errors.
+Require Import AST Linking.
+Require Import Values Memory Registers Events Globalenvs Smallstep.
+Require Import Op Locations RTL.
+Require Import RTLTunneling.
+Require Import Conventions1.
+
+Local Open Scope nat.
+
+Definition check_included_spec (c:code) (td:UF) (ok: option instruction) :=
+ ok <> None -> forall pc, c!pc = None -> td!pc = None.
+
+Lemma check_included_correct (td:UF) (c:code):
+ check_included_spec c td (check_included td c).
+Proof.
+ apply PTree_Properties.fold_rec with (P:=check_included_spec c); unfold check_included_spec.
+ - intros m m' oi EQ IND N pc. rewrite <- EQ. apply IND. apply N.
+ - intros N pc. rewrite PTree.gempty. auto.
+ - intros m oi pc v N S IND. destruct oi.
+ + intros. rewrite PTree.gsspec. destruct (peq _ _); try congruence. apply IND. congruence. apply H0.
+ + contradiction.
+Qed.
+
+Inductive target_bounds (target: node -> node) (bound: node -> nat) (pc: node) : option instruction -> Prop :=
+ | TB_default (TB: target pc = pc) oi:
+ target_bounds target bound pc oi
+ | TB_nop s
+ (EQ: target pc = target s)
+ (DEC: bound s < bound pc):
+ target_bounds target bound pc (Some (Inop s))
+ | TB_cond cond args ifso ifnot info
+ (EQSO: target pc = target ifso)
+ (EQNOT: target pc = target ifnot)
+ (DECSO: bound ifso < bound pc)
+ (DECNOT: bound ifnot < bound pc):
+ target_bounds target bound pc (Some (Icond cond args ifso ifnot info))
+.
+Local Hint Resolve TB_default: core.
+
+Lemma target_None (td: UF) (pc: node): td!pc = None -> td pc = pc.
+Proof.
+ unfold target, get. intro EQ. rewrite EQ. auto.
+Qed.
+Local Hint Resolve target_None Z.abs_nonneg: core.
+
+Lemma get_nonneg td pc t d: get td pc = (t,d) -> (0 <= d)%Z.
+Proof.
+ unfold get. destruct td!pc as [(tpc,dpc)|]; intro H; inv H; lia.
+Qed.
+Local Hint Resolve get_nonneg: core.
+
+Definition bound (td: UF) (pc: node) := Z.to_nat (snd (get td pc)).
+
+
+(* TODO: à réécrire proprement *)
+Lemma check_instr_correct (td: UF) (pc: node) (i: instruction):
+ check_instr td pc i = OK tt ->
+ target_bounds (target td) (bound td) pc (Some i).
+Proof.
+ unfold check_instr. destruct (td!pc) as [(tpc,dpc)|] eqn:EQ.
+ assert (DPC: snd (get td pc) = Z.abs dpc). { unfold get. rewrite EQ. auto. }
+ - destruct i; try congruence.
+ + destruct (get td n) as (ts,ds) eqn:EQs.
+ destruct (peq _ _); try congruence.
+ destruct (zlt _ _); try congruence. intros _.
+ apply TB_nop. replace (td pc) with tpc.
+ unfold target. rewrite EQs. auto.
+ unfold target. unfold get. rewrite EQ. auto.
+ unfold bound. rewrite DPC. rewrite EQs; simpl. apply Z2Nat.inj_lt; try lia. apply get_nonneg with td n ts. apply EQs.
+ + destruct (get td n) as (tso,dso) eqn:EQSO.
+ destruct (get td n0) as (tnot,dnot) eqn:EQNOT.
+ intro H.
+ repeat ((destruct (peq _ _) in H || destruct (zlt _ _) in H); try congruence).
+ apply TB_cond; subst.
+ * unfold target. replace (fst (get td pc)) with tnot. rewrite EQSO. auto.
+ unfold get. rewrite EQ. auto.
+ * unfold target. replace (fst (get td pc)) with tnot. rewrite EQNOT. auto.
+ unfold get. rewrite EQ. auto.
+ * unfold bound. rewrite DPC. apply Z2Nat.inj_lt; try lia. apply get_nonneg with td n tnot. rewrite EQSO. auto. rewrite EQSO. auto.
+ * unfold bound. rewrite DPC. apply Z2Nat.inj_lt; try lia. apply get_nonneg with td n0 tnot. rewrite EQNOT; auto. rewrite EQNOT; auto.
+ - intros _. apply TB_default. unfold target. unfold get. rewrite EQ. auto.
+Qed.
+
+Definition check_code_spec (td:UF) (c:code) (ok: res unit) :=
+ ok = OK tt -> forall pc i, c!pc = Some i -> target_bounds (target td) (bound td) pc (Some i).
+
+Lemma check_code_correct (td:UF) c:
+ check_code_spec td c (check_code td c).
+Proof.
+ unfold check_code. apply PTree_Properties.fold_rec; unfold check_code_spec.
+ - intros. rewrite <- H in H2. apply H0; auto.
+ - intros. rewrite PTree.gempty in H0. congruence.
+ - intros m [[]|e] pc i N S IND; simpl; try congruence.
+ intros H pc0 i0. rewrite PTree.gsspec. destruct (peq _ _).
+ subst. intro. inv H0. apply check_instr_correct. apply H.
+ auto.
+Qed.
+
+Theorem branch_target_bounds:
+ forall f tf pc,
+ tunnel_function f = OK tf ->
+ target_bounds (branch_target f) (bound (branch_target f)) pc (f.(fn_code)!pc).
+Proof.
+ intros. unfold tunnel_function in H.
+ destruct (check_included _ _) eqn:EQinc; try congruence.
+ monadInv H. rename EQ into EQcode.
+ destruct (_ ! _) eqn:EQ.
+ - exploit check_code_correct. destruct x. apply EQcode. apply EQ. auto.
+ - exploit check_included_correct.
+ rewrite EQinc. congruence.
+ apply EQ.
+ intro. apply TB_default. apply target_None. apply H.
+Qed.
+
+(** Preservation of semantics *)
+
+Definition match_prog (p tp: program) :=
+ match_program (fun _ f tf => tunnel_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+
+Section PRESERVATION.
+
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma functions_translated:
+ forall (v: val) (f: fundef),
+ Genv.find_funct ge v = Some f ->
+ exists tf, tunnel_fundef f = OK tf /\ Genv.find_funct tge v = Some tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_match TRANSL). apply H.
+ intros (cu & tf & A & B & C).
+ eexists. eauto.
+Qed.
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ tunnel_fundef f = OK tf.
+Proof.
+ intros. exploit (Genv.find_funct_ptr_match TRANSL).
+ - apply H.
+ - intros (cu & tf & A & B & C). exists tf. split. apply A. apply B.
+Qed.
+
+Lemma symbols_preserved s: Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof.
+ apply (Genv.find_symbol_match TRANSL).
+Qed.
+
+Lemma sig_preserved:
+ forall f tf, tunnel_fundef f = OK tf -> funsig tf = funsig f.
+Proof.
+ intros. destruct f; simpl in H.
+ - monadInv H.
+ unfold tunnel_function in EQ.
+ destruct (check_included _ _) in EQ; try congruence.
+ monadInv EQ. auto.
+ - monadInv H. auto.
+Qed.
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof.
+ eapply (Genv.senv_match TRANSL). (* Il y a déjà une preuve de cette propriété très exactement, je ne vais pas réinventer la roue ici *)
+Qed.
+
+Inductive match_stackframes: stackframe -> stackframe -> Prop :=
+ | match_stackframes_intro:
+ forall res f tf sp pc rs trs
+ (TF: tunnel_function f = OK tf)
+ (RS: Registers.regs_lessdef rs trs),
+ match_stackframes
+ (Stackframe res f sp pc rs)
+ (Stackframe res tf sp (branch_target f pc) trs).
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s ts f tf sp pc rs trs m tm
+ (STK: list_forall2 match_stackframes s ts)
+ (TF: tunnel_function f = OK tf)
+ (RS: Registers.regs_lessdef rs trs)
+ (MEM: Mem.extends m tm),
+ match_states
+ (State s f sp pc rs m)
+ (State ts tf sp (branch_target f pc) trs tm)
+ | match_states_call:
+ forall s ts f tf a ta m tm
+ (STK: list_forall2 match_stackframes s ts)
+ (TF: tunnel_fundef f = OK tf)
+ (ARGS: list_forall2 Val.lessdef a ta)
+ (MEM: Mem.extends m tm),
+ match_states
+ (Callstate s f a m)
+ (Callstate ts tf ta tm)
+ | match_states_return:
+ forall s ts v tv m tm
+ (STK: list_forall2 match_stackframes s ts)
+ (VAL: Val.lessdef v tv)
+ (MEM: Mem.extends m tm),
+ match_states
+ (Returnstate s v m)
+ (Returnstate ts tv tm).
+
+Definition measure (st: state): nat :=
+ match st with
+ | State s f sp pc rs m => bound (branch_target f) pc
+ | Callstate s f v m => 0
+ | Returnstate s v m => 0
+ end.
+
+
+Lemma transf_initial_states:
+ forall s1: state, initial_state prog s1 ->
+ exists s2: state, initial_state tprog s2 /\ match_states s1 s2.
+Proof.
+ intros. inversion H as [b f m0 ge0 MEM SYM PTR SIG CALL].
+ exploit function_ptr_translated.
+ - apply PTR.
+ - intros (tf & TPTR & TUN).
+ exists (Callstate nil tf nil m0). split.
+ + apply initial_state_intro with b.
+ * apply (Genv.init_mem_match TRANSL). apply MEM.
+ * rewrite (match_program_main TRANSL).
+ rewrite symbols_preserved. apply SYM.
+ * apply TPTR.
+ * rewrite <- SIG. apply sig_preserved. apply TUN.
+ + apply match_states_call.
+ * apply list_forall2_nil.
+ * apply TUN.
+ * apply list_forall2_nil.
+ * apply Mem.extends_refl.
+Qed.
+
+Lemma transf_final_states:
+ forall (s1 : state)
+ (s2 : state) (r : Integers.Int.int),
+ match_states s1 s2 ->
+ final_state s1 r ->
+ final_state s2 r.
+Proof.
+ intros. inv H0. inv H. inv VAL. inversion STK. apply final_state_intro.
+Qed.
+
+Lemma tunnel_function_unfold:
+ forall f tf pc,
+ tunnel_function f = OK tf ->
+ (fn_code tf) ! pc =
+ option_map (tunnel_instr (branch_target f)) (fn_code f) ! pc.
+Proof.
+ intros f tf pc.
+ unfold tunnel_function.
+ destruct (check_included _ _) eqn:EQinc; try congruence.
+ destruct (check_code _ _) eqn:EQcode; simpl; try congruence.
+ intro. inv H. simpl. rewrite PTree.gmap1. reflexivity.
+Qed.
+
+Lemma reglist_lessdef:
+ forall (rs trs: Registers.Regmap.t val) (args: list Registers.reg),
+ regs_lessdef rs trs -> Val.lessdef_list (rs##args) (trs##args).
+Proof.
+ intros. induction args; simpl; constructor.
+ apply H. apply IHargs.
+Qed.
+
+Lemma instruction_type_preserved:
+ forall (f tf:function) (pc:node) (i ti:instruction)
+ (TF: tunnel_function f = OK tf)
+ (FATPC: (fn_code f) ! pc = Some i)
+ (NOTINOP: forall s, i <> Inop s)
+ (NOTICOND: forall cond args ifso ifnot info, i <> Icond cond args ifso ifnot info)
+ (TI: ti = tunnel_instr (branch_target f) i),
+ (fn_code tf) ! (branch_target f pc) = Some ti.
+Proof.
+ intros.
+ assert ((fn_code tf) ! pc = Some (tunnel_instr (branch_target f) i)) as TFATPC.
+ rewrite (tunnel_function_unfold f tf pc); eauto.
+ rewrite FATPC; eauto.
+ exploit branch_target_bounds; eauto.
+ intro TB. inversion TB as [BT|s|cond args ifso ifnot info]; try (rewrite FATPC in H; congruence).
+Qed.
+
+Lemma find_function_translated:
+ forall ros rs trs fd,
+ regs_lessdef rs trs ->
+ find_function ge ros rs = Some fd ->
+ exists tfd, tunnel_fundef fd = OK tfd /\ find_function tge ros trs = Some tfd.
+Proof.
+ intros. destruct ros; simpl in *.
+ - (* reg *)
+ assert (E: trs # r = rs # r).
+ { exploit Genv.find_funct_inv. apply H0. intros (b & EQ).
+ generalize (H r) . rewrite EQ. intro LD. inv LD. auto. }
+ rewrite E. exploit functions_translated; eauto.
+ - (* ident *)
+ rewrite symbols_preserved. destruct (Genv.find_symbol ge i); inv H0.
+ exploit function_ptr_translated; eauto.
+ intros (tf & X1 & X2). exists tf; intuition.
+Qed.
+
+Lemma list_forall2_lessdef_rs:
+ forall rs trs args,
+ regs_lessdef rs trs ->
+ list_forall2 Val.lessdef rs ## args trs ## args.
+Proof.
+ intros rs trs args LD.
+ exploit (reglist_lessdef rs trs args). apply LD.
+ induction args; simpl; intros H; try constructor; inv H.
+ apply H3. apply IHargs. apply H5.
+Qed.
+
+Lemma fn_stacksize_preserved:
+ forall f tf
+ (TF: tunnel_function f = OK tf),
+ fn_stacksize f = fn_stacksize tf.
+Proof.
+ intros f tf. unfold tunnel_function.
+ destruct (check_included _ _); try congruence.
+ intro H. monadInv H. simpl. reflexivity.
+Qed.
+
+Lemma regs_setres_lessdef:
+ forall res vres tvres rs trs,
+ regs_lessdef rs trs -> Val.lessdef vres tvres ->
+ regs_lessdef (regmap_setres res vres rs) (regmap_setres res tvres trs).
+Proof.
+ induction res; intros; simpl; try auto using set_reg_lessdef.
+Qed.
+
+Lemma regmap_optget_lessdef:
+ forall or rs trs,
+ regs_lessdef rs trs -> Val.lessdef (regmap_optget or Vundef rs) (regmap_optget or Vundef trs).
+Proof.
+ intros or rs trs RS.
+ induction or; simpl; auto using set_reg_lessdef.
+Qed.
+
+Lemma tunnel_fundef_Internal:
+ forall (f: function) (tf: fundef)
+ (TF: tunnel_fundef (Internal f) = OK tf),
+ exists (tf': function), tf = (Internal tf') /\ tunnel_function f = OK tf'.
+Proof.
+ intros f tf.
+ unfold tunnel_fundef. simpl. intro H. monadInv H. exists x.
+ split. reflexivity. apply EQ.
+Qed.
+
+Lemma tunnel_fundef_External:
+ forall (ef: external_function) (tf: fundef)
+ (TF: tunnel_fundef (External ef) = OK tf),
+ tf = (External ef).
+Proof.
+ intros f tf.
+ unfold tunnel_fundef. simpl. intro H. monadInv H. reflexivity.
+Qed.
+
+
+Lemma fn_entrypoint_preserved:
+ forall f tf
+ (TF: tunnel_function f = OK tf),
+ fn_entrypoint tf = branch_target f (fn_entrypoint f).
+Proof.
+ intros f tf.
+ unfold tunnel_function. destruct (check_included _ _); try congruence.
+ intro TF. monadInv TF. simpl. reflexivity.
+Qed.
+
+Lemma init_regs_lessdef:
+ forall f tf args targs
+ (ARGS: list_forall2 Val.lessdef args targs)
+ (TF: tunnel_function f = OK tf),
+ regs_lessdef (init_regs args (fn_params f)) (init_regs targs (fn_params tf)).
+Proof.
+ assert (regs_lessdef (Regmap.init Vundef) (Regmap.init Vundef)) as Hundef.
+ { unfold Regmap.init. unfold regs_lessdef. intro. unfold Regmap.get. rewrite PTree.gempty. apply Val.lessdef_undef. }
+
+ intros f tf args targs ARGS.
+
+ unfold tunnel_function. destruct (check_included _ _) eqn:EQinc; try congruence.
+ intro TF. monadInv TF. simpl.
+ (*
+ induction ARGS.
+ - induction (fn_params f) eqn:EQP; apply Hundef.
+ - induction (fn_params f) eqn:EQP.
+ * simpl. apply Hundef.
+ * simpl. apply set_reg_lessdef. apply H.
+ *)
+
+ generalize (fn_params f) as l. induction ARGS; induction l; try (simpl; apply Hundef).
+ simpl. apply set_reg_lessdef; try assumption. apply IHARGS.
+Qed.
+
+Lemma lessdef_forall2_list:
+ forall args ta,
+ list_forall2 Val.lessdef args ta -> Val.lessdef_list args ta.
+Proof.
+ intros args ta H. induction H. apply Val.lessdef_list_nil. apply Val.lessdef_list_cons. apply H. apply IHlist_forall2.
+Qed.
+
+Lemma tunnel_step_correct:
+ forall st1 t st2, step ge st1 t st2 ->
+ forall st1' (MS: match_states st1 st1'),
+ (exists st2', step tge st1' t st2' /\ match_states st2 st2')
+ \/ (measure st2 < measure st1 /\ t = E0 /\ match_states st2 st1')%nat.
+Proof.
+ intros st1 t st2 H. induction H; intros; try (inv MS).
+ - (* Inop *)
+ exploit branch_target_bounds. apply TF.
+ rewrite H. intro. inv H0.
+ + (* TB_default *)
+ rewrite TB. left. eexists. split.
+ * apply exec_Inop. rewrite (tunnel_function_unfold f tf pc). rewrite H. simpl. eauto. apply TF.
+ * constructor; try assumption.
+ + (* TB_nop *)
+ simpl. right. repeat split. apply DEC.
+ rewrite EQ. apply match_states_intro; assumption.
+ - (* Iop *)
+ exploit eval_operation_lessdef; try eassumption.
+ apply reglist_lessdef. apply RS.
+ intros (tv & EVAL & LD).
+ left; eexists; split.
+ + eapply exec_Iop with (v:=tv).
+ apply instruction_type_preserved with (Iop op args res pc'); (simpl; auto)||congruence.
+ rewrite <- EVAL. apply eval_operation_preserved. apply symbols_preserved.
+ + apply match_states_intro; eauto. apply set_reg_lessdef. apply LD. apply RS.
+ - (* Iload *)
+ exploit eval_addressing_lessdef; try eassumption.
+ apply reglist_lessdef. apply RS.
+ intros (ta & EVAL & LD).
+ exploit Mem.loadv_extends; try eassumption.
+ intros (tv & LOAD & LD').
+ left. eexists. split.
+ + eapply exec_Iload.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * rewrite <- EVAL. apply eval_addressing_preserved. apply symbols_preserved.
+ * apply LOAD.
+ + apply match_states_intro; try assumption. apply set_reg_lessdef. apply LD'. apply RS.
+ - (* Iload NOTRAP1 *)
+ exploit eval_addressing_lessdef_none; try eassumption.
+ apply reglist_lessdef; apply RS.
+ left. eexists. split.
+ + eapply exec_Iload_notrap1.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * rewrite <- H1. apply eval_addressing_preserved. apply symbols_preserved.
+ + apply match_states_intro; try assumption. apply set_reg_lessdef. apply Val.lessdef_undef. apply RS.
+ - (* Iload NOTRAP2 *)
+ exploit eval_addressing_lessdef; try eassumption.
+ apply reglist_lessdef; apply RS.
+ intros (ta & EVAL & LD).
+ (* TODO: on peut sans doute factoriser ça *)
+ destruct (Mem.loadv chunk tm ta) eqn:Htload.
+ + left; eexists; split.
+ eapply exec_Iload.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * rewrite <- EVAL. apply eval_addressing_preserved. apply symbols_preserved.
+ * apply Htload.
+ * apply match_states_intro; try assumption. apply set_reg_lessdef; eauto.
+ + left; eexists; split.
+ eapply exec_Iload_notrap2.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * rewrite <- EVAL. apply eval_addressing_preserved. apply symbols_preserved.
+ * apply Htload.
+ * apply match_states_intro; try assumption. apply set_reg_lessdef; eauto.
+ - (* Lstore *)
+ exploit eval_addressing_lessdef; try eassumption.
+ apply reglist_lessdef; apply RS.
+ intros (ta & EVAL & LD).
+ exploit Mem.storev_extends; try eassumption. apply RS.
+ intros (tm' & STORE & MEM').
+ left. eexists. split.
+ + eapply exec_Istore.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * rewrite <- EVAL. apply eval_addressing_preserved. apply symbols_preserved.
+ * rewrite STORE. reflexivity.
+ + apply match_states_intro; try eassumption.
+ - (* Icall *)
+ left.
+ exploit find_function_translated; try eassumption.
+ intros (tfd & TFD & FIND).
+ eexists. split.
+ + eapply exec_Icall.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * apply FIND.
+ * apply sig_preserved. apply TFD.
+ + apply match_states_call; try assumption.
+ * apply list_forall2_cons; try assumption. apply match_stackframes_intro; try assumption.
+ * apply list_forall2_lessdef_rs. apply RS.
+ - (* Itailcall *)
+ exploit find_function_translated; try eassumption.
+ intros (tfd & TFD & FIND).
+ exploit Mem.free_parallel_extends; try eassumption.
+ intros (tm' & FREE & MEM').
+ left. eexists. split.
+ + eapply exec_Itailcall.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * apply FIND.
+ * apply sig_preserved. apply TFD.
+ * erewrite <- fn_stacksize_preserved; try eassumption.
+ + apply match_states_call; try assumption.
+ apply list_forall2_lessdef_rs. apply RS.
+ - (* Ibuiltin *)
+ exploit eval_builtin_args_lessdef; try eassumption. apply RS.
+ intros (vl2 & EVAL & LD).
+ exploit external_call_mem_extends; try eassumption.
+ intros (tvres & tm' & EXT & LDRES & MEM' & UNCHGD).
+ left. eexists. split.
+ + eapply exec_Ibuiltin.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * eapply eval_builtin_args_preserved. eapply symbols_preserved. eapply EVAL.
+ * eapply external_call_symbols_preserved. eapply senv_preserved. eapply EXT.
+ + apply match_states_intro; try assumption. apply regs_setres_lessdef; try assumption.
+ - (* Icond *)
+ simpl. exploit branch_target_bounds. apply TF. rewrite H. intro. inv H1.
+ + (* TB_default *)
+ rewrite TB.
+ destruct (fn_code tf)!pc as [[]|] eqn:EQ;
+ assert (tunnel_function f = OK tf) as TF'; auto;
+ unfold tunnel_function in TF; destruct (check_included _ _) in TF; monadInv TF;
+ simpl in EQ; rewrite PTree.gmap1 in EQ; rewrite H in EQ; simpl in EQ; destruct (peq _ _) eqn: EQpeq in EQ; try congruence.
+ * left. eexists. split.
+ -- eapply exec_Inop. simpl. rewrite PTree.gmap1. rewrite H. simpl. rewrite EQpeq. reflexivity.
+ -- destruct b. apply match_states_intro; eauto. rewrite e. apply match_states_intro; eauto.
+ * left. eexists. split.
+ -- eapply exec_Icond; auto. simpl. rewrite PTree.gmap1. rewrite H. simpl. rewrite EQpeq. reflexivity. eapply eval_condition_lessdef; try eassumption. apply reglist_lessdef. apply RS.
+ -- destruct b; apply match_states_intro; auto.
+ + (* TB_cond *) right; repeat split; destruct b; try assumption.
+ * rewrite EQSO. apply match_states_intro; try assumption.
+ * rewrite EQNOT. apply match_states_intro; try assumption.
+ - (* Ijumptable *)
+ left. eexists. split.
+ + eapply exec_Ijumptable.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * generalize (RS arg). rewrite H0. intro. inv H2. reflexivity.
+ * rewrite list_nth_z_map. rewrite H1. simpl. reflexivity.
+ + apply match_states_intro; try eassumption.
+ - (* Ireturn *)
+ exploit Mem.free_parallel_extends; try eassumption.
+ intros (tm' & FREE & MEM').
+ left. eexists. split.
+ + eapply exec_Ireturn.
+ * exploit instruction_type_preserved; (simpl; eauto)||congruence.
+ * erewrite <- fn_stacksize_preserved. eapply FREE. eapply TF.
+ + apply match_states_return; try eassumption.
+ apply regmap_optget_lessdef. apply RS.
+ - (* internal function *)
+ exploit tunnel_fundef_Internal; try eassumption.
+ intros (tf' & EQ & TF'). subst.
+ exploit Mem.alloc_extends; try eassumption. reflexivity. reflexivity.
+ intros (m2' & ALLOC & EXT).
+ left. eexists. split.
+ + eapply exec_function_internal.
+ rewrite <- (fn_stacksize_preserved f tf'). eapply ALLOC. eapply TF'.
+ + rewrite (fn_entrypoint_preserved f tf'); try eassumption. apply match_states_intro; try eassumption.
+ apply init_regs_lessdef. apply ARGS. apply TF'.
+ - (* external function *)
+ exploit external_call_mem_extends. eapply H. eapply MEM. eapply lessdef_forall2_list. eapply ARGS.
+ intros (tvres & tm' & EXTCALL & LD & EXT & MEMUNCHGD).
+ left. eexists. split.
+ + erewrite (tunnel_fundef_External ef tf); try eassumption.
+ eapply exec_function_external. eapply external_call_symbols_preserved. eapply senv_preserved. eapply EXTCALL.
+ + eapply match_states_return; try assumption.
+ - (* return *)
+ inv STK. inv H1.
+ left. eexists. split.
+ + eapply exec_return.
+ + eapply match_states_intro; try assumption.
+ apply set_reg_lessdef; try assumption.
+Qed.
+
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_opt.
+ apply senv_preserved.
+ apply transf_initial_states.
+ apply transf_final_states.
+ exact tunnel_step_correct.
+Qed.
+
+End PRESERVATION.
diff --git a/backend/RTLcommonaux.ml b/backend/RTLcommonaux.ml
new file mode 100644
index 00000000..2e9dde2e
--- /dev/null
+++ b/backend/RTLcommonaux.ml
@@ -0,0 +1,105 @@
+open RTL
+open Maps
+open Camlcoq
+open Registers
+open Kildall
+open Lattice
+
+let p2i r = P.to_int r
+
+let i2p i = P.of_int i
+
+let get_some = function
+ | None -> failwith "Got None instead of Some _"
+ | Some thing -> thing
+
+let get_ok r = match r with Errors.OK x -> x | _ -> failwith "Did not get OK"
+
+let successors_inst = function
+ | Inop n
+ | Iop (_, _, _, n)
+ | Iload (_, _, _, _, _, n)
+ | Istore (_, _, _, _, n)
+ | Icall (_, _, _, _, n)
+ | Ibuiltin (_, _, _, n) ->
+ [ n ]
+ | Icond (_, _, n1, n2, _) -> [ n1; n2 ]
+ | Ijumptable (_, l) -> l
+ | Itailcall _ | Ireturn _ -> []
+
+let predicted_successor = function
+ | Inop n | Iop (_, _, _, n) | Iload (_, _, _, _, _, n) | Istore (_, _, _, _, n)
+ ->
+ Some n
+ | Icall (_, _, _, _, n) | Ibuiltin (_, _, _, n) -> None
+ | Icond (_, _, n1, n2, p) -> (
+ match p with Some true -> Some n1 | Some false -> Some n2 | None -> None)
+ | Ijumptable _ | Itailcall _ | Ireturn _ -> None
+
+let non_predicted_successors i = function
+ | None -> successors_inst i
+ | Some ps -> List.filter (fun s -> s != ps) (successors_inst i)
+
+(* adapted from Linearizeaux.get_join_points *)
+let get_join_points code entry =
+ let reached = ref (PTree.map (fun n i -> false) code) in
+ let reached_twice = ref (PTree.map (fun n i -> false) code) in
+ let rec traverse pc =
+ if get_some @@ PTree.get pc !reached then (
+ if not (get_some @@ PTree.get pc !reached_twice) then
+ reached_twice := PTree.set pc true !reached_twice)
+ else (
+ reached := PTree.set pc true !reached;
+ traverse_succs (successors_inst @@ get_some @@ PTree.get pc code))
+ and traverse_succs = function
+ | [] -> ()
+ | [ pc ] -> traverse pc
+ | pc :: l ->
+ traverse pc;
+ traverse_succs l
+ in
+ traverse entry;
+ !reached_twice
+
+let transfer f pc after =
+ let open Liveness in
+ match PTree.get pc f.fn_code with
+ | Some i -> (
+ match i with
+ | Inop _ -> after
+ | Iop (_, args, res, _) -> reg_list_live args (Regset.remove res after)
+ | Iload (_, _, _, args, dst, _) ->
+ reg_list_live args (Regset.remove dst after)
+ | Istore (_, _, args, src, _) -> reg_list_live args (Regset.add src after)
+ | Icall (_, ros, args, res, _) ->
+ reg_list_live args (reg_sum_live ros (Regset.remove res after))
+ | Itailcall (_, ros, args) ->
+ reg_list_live args (reg_sum_live ros Regset.empty)
+ | Ibuiltin (_, args, res, _) ->
+ reg_list_live
+ (AST.params_of_builtin_args args)
+ (reg_list_dead (AST.params_of_builtin_res res) after)
+ | Icond (_, args, _, _, _) -> reg_list_live args after
+ | Ijumptable (arg, _) -> Regset.add arg after
+ | Ireturn optarg -> reg_option_live optarg Regset.empty)
+ | None -> Regset.empty
+
+module RegsetLat = LFSet (Regset)
+module DS = Backward_Dataflow_Solver (RegsetLat) (NodeSetBackward)
+
+let analyze f =
+ let liveouts =
+ get_some @@ DS.fixpoint f.fn_code successors_instr (transfer f)
+ in
+ PTree.map
+ (fun n _ ->
+ let lo = PMap.get n liveouts in
+ transfer f n lo)
+ f.fn_code
+
+let get_outputs liveness n last =
+ let path_last_successors = successors_inst last in
+ let list_input_regs =
+ List.map (fun n -> get_some @@ PTree.get n liveness) path_last_successors
+ in
+ List.fold_left Regset.union Regset.empty list_input_regs
diff --git a/backend/RTLgen.v b/backend/RTLgen.v
index f7280c9e..243d7b7c 100644
--- a/backend/RTLgen.v
+++ b/backend/RTLgen.v
@@ -435,7 +435,7 @@ Fixpoint transl_expr (map: mapping) (a: expr) (rd: reg) (nd: node)
transl_exprlist map al rl no
| Eload chunk addr al =>
do rl <- alloc_regs map al;
- do no <- add_instr (Iload chunk addr rl rd nd);
+ do no <- add_instr (Iload TRAP chunk addr rl rd nd);
transl_exprlist map al rl no
| Econdition a b c =>
do nfalse <- transl_expr map c rd nd;
@@ -477,9 +477,9 @@ with transl_exprlist (map: mapping) (al: exprlist) (rl: list reg) (nd: node)
with transl_condexpr (map: mapping) (a: condexpr) (ntrue nfalse: node)
{struct a} : mon node :=
match a with
- | CEcond c al =>
+ | CEcond c expected al =>
do rl <- alloc_regs map al;
- do nt <- add_instr (Icond c rl ntrue nfalse);
+ do nt <- add_instr (Icond c rl ntrue nfalse expected);
transl_exprlist map al rl nt
| CEcondition a b c =>
do nc <- transl_condexpr map c ntrue nfalse;
diff --git a/backend/RTLgenaux.ml b/backend/RTLgenaux.ml
index e39d3b56..26688e23 100644
--- a/backend/RTLgenaux.ml
+++ b/backend/RTLgenaux.ml
@@ -41,7 +41,7 @@ and size_exprs = function
| Econs(e1, el) -> size_expr e1 + size_exprs el
and size_condexpr = function
- | CEcond(c, args) -> size_exprs args
+ | CEcond(c, expected, args) -> size_exprs args
| CEcondition(c1, c2, c3) ->
1 + size_condexpr c1 + size_condexpr c2 + size_condexpr c3
| CElet(a, c) ->
diff --git a/backend/RTLgenproof.v b/backend/RTLgenproof.v
index 1602823f..d07dc968 100644
--- a/backend/RTLgenproof.v
+++ b/backend/RTLgenproof.v
@@ -799,11 +799,11 @@ Proof.
Qed.
Lemma transl_condexpr_CEcond_correct:
- forall le cond al vl vb,
+ forall le cond expected al vl vb,
eval_exprlist ge sp e m le al vl ->
transl_exprlist_prop le al vl ->
eval_condition cond vl m = Some vb ->
- transl_condexpr_prop le (CEcond cond al) vb.
+ transl_condexpr_prop le (CEcond cond expected al) vb.
Proof.
intros; red; intros. inv TE.
exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
diff --git a/backend/RTLgenspec.v b/backend/RTLgenspec.v
index 25f9954c..0210aa5b 100644
--- a/backend/RTLgenspec.v
+++ b/backend/RTLgenspec.v
@@ -707,7 +707,7 @@ Inductive tr_expr (c: code):
tr_expr c map pr (Eop op al) ns nd rd dst
| tr_Eload: forall map pr chunk addr al ns nd rd n1 rl dst,
tr_exprlist c map pr al ns n1 rl ->
- c!n1 = Some (Iload chunk addr rl rd nd) ->
+ c!n1 = Some (Iload TRAP chunk addr rl rd nd) ->
reg_map_ok map rd dst -> ~In rd pr ->
tr_expr c map pr (Eload chunk addr al) ns nd rd dst
| tr_Econdition: forall map pr a ifso ifnot ns nd rd ntrue nfalse dst,
@@ -744,10 +744,10 @@ Inductive tr_expr (c: code):
with tr_condition (c: code):
mapping -> list reg -> condexpr -> node -> node -> node -> Prop :=
- | tr_CEcond: forall map pr cond bl ns ntrue nfalse n1 rl,
+ | tr_CEcond: forall map pr cond expected bl ns ntrue nfalse n1 rl i,
tr_exprlist c map pr bl ns n1 rl ->
- c!n1 = Some (Icond cond rl ntrue nfalse) ->
- tr_condition c map pr (CEcond cond bl) ns ntrue nfalse
+ c!n1 = Some (Icond cond rl ntrue nfalse i) ->
+ tr_condition c map pr (CEcond cond expected bl) ns ntrue nfalse
| tr_CEcondition: forall map pr a1 a2 a3 ns ntrue nfalse n2 n3,
tr_condition c map pr a1 ns n2 n3 ->
tr_condition c map pr a2 n2 ntrue nfalse ->
diff --git a/backend/RTLtyping.v b/backend/RTLtyping.v
index 5b8646ea..6048f895 100644
--- a/backend/RTLtyping.v
+++ b/backend/RTLtyping.v
@@ -104,11 +104,11 @@ Inductive wt_instr : instruction -> Prop :=
valid_successor s ->
wt_instr (Iop op args res s)
| wt_Iload:
- forall chunk addr args dst s,
+ forall trap chunk addr args dst s,
map env args = type_of_addressing addr ->
env dst = type_of_chunk chunk ->
valid_successor s ->
- wt_instr (Iload chunk addr args dst s)
+ wt_instr (Iload trap chunk addr args dst s)
| wt_Istore:
forall chunk addr args src s,
map env args = type_of_addressing addr ->
@@ -139,11 +139,11 @@ Inductive wt_instr : instruction -> Prop :=
valid_successor s ->
wt_instr (Ibuiltin ef args res s)
| wt_Icond:
- forall cond args s1 s2,
+ forall cond args s1 s2 i,
map env args = type_of_condition cond ->
valid_successor s1 ->
valid_successor s2 ->
- wt_instr (Icond cond args s1 s2)
+ wt_instr (Icond cond args s1 s2 i)
| wt_Ijumptable:
forall arg tbl,
env arg = Tint ->
@@ -283,7 +283,7 @@ Definition type_instr (e: S.typenv) (i: instruction) : res S.typenv :=
else
(let (targs, tres) := type_of_operation op in
do e1 <- S.set_list e args targs; S.set e1 res tres)
- | Iload chunk addr args dst s =>
+ | Iload trap chunk addr args dst s =>
do x <- check_successor s;
do e1 <- S.set_list e args (type_of_addressing addr);
S.set e1 dst (type_of_chunk chunk)
@@ -313,7 +313,7 @@ Definition type_instr (e: S.typenv) (i: instruction) : res S.typenv :=
| _ => type_builtin_args e args sig.(sig_args)
end;
type_builtin_res e1 res (proj_sig_res sig)
- | Icond cond args s1 s2 =>
+ | Icond cond args s1 s2 _ =>
do x1 <- check_successor s1;
do x2 <- check_successor s2;
S.set_list e args (type_of_condition cond)
@@ -844,14 +844,24 @@ Proof.
Qed.
Lemma wt_exec_Iload:
- forall env f chunk addr args dst s m a v rs,
- wt_instr f env (Iload chunk addr args dst s) ->
+ forall env f trap chunk addr args dst s m a v rs,
+ wt_instr f env (Iload trap chunk addr args dst s) ->
Mem.loadv chunk m a = Some v ->
wt_regset env rs ->
wt_regset env (rs#dst <- v).
Proof.
intros. destruct a; simpl in H0; try discriminate. inv H.
- eapply wt_regset_assign; eauto. rewrite H8; eapply Mem.load_type; eauto.
+ eapply wt_regset_assign; eauto. rewrite H9; eapply Mem.load_type; eauto.
+Qed.
+
+Lemma wt_exec_Iload_notrap:
+ forall env f chunk addr args dst s rs,
+ wt_instr f env (Iload NOTRAP chunk addr args dst s) ->
+ wt_regset env rs ->
+ wt_regset env (rs#dst <- Vundef).
+Proof.
+ intros.
+ eapply wt_regset_assign; eauto. simpl. trivial.
Qed.
Lemma wt_exec_Ibuiltin:
@@ -933,6 +943,10 @@ Proof.
econstructor; eauto. eapply wt_exec_Iop; eauto.
(* Iload *)
econstructor; eauto. eapply wt_exec_Iload; eauto.
+ (* Iload notrap1*)
+ econstructor; eauto. eapply wt_exec_Iload_notrap; eauto.
+ (* Iload notrap2*)
+ econstructor; eauto. eapply wt_exec_Iload_notrap; eauto.
(* Istore *)
econstructor; eauto.
(* Icall *)
diff --git a/backend/Regalloc.ml b/backend/Regalloc.ml
index 19aba4f6..ffe26933 100644
--- a/backend/Regalloc.ml
+++ b/backend/Regalloc.ml
@@ -249,18 +249,18 @@ let block_of_RTL_instr funsig tyenv = function
else
let t = new_temp (tyenv res) in (t :: args2', t) in
movelist args1 args3 (Xop(op, args3, res3) :: move res3 res1 [Xbranch s])
- | RTL.Iload(chunk, addr, args, dst, s) ->
+ | RTL.Iload(trap, chunk, addr, args, dst, s) ->
if Archi.splitlong && chunk = Mint64 then begin
match offset_addressing addr (coqint_of_camlint 4l) with
| None -> assert false
| Some addr' ->
- [Xload(Mint32, addr, vregs tyenv args,
+ [Xload(trap, Mint32, addr, vregs tyenv args,
V((if Archi.big_endian then dst else twin_reg dst), Tint));
- Xload(Mint32, addr', vregs tyenv args,
+ Xload(trap, Mint32, addr', vregs tyenv args,
V((if Archi.big_endian then twin_reg dst else dst), Tint));
Xbranch s]
end else
- [Xload(chunk, addr, vregs tyenv args, vreg tyenv dst); Xbranch s]
+ [Xload(trap, chunk, addr, vregs tyenv args, vreg tyenv dst); Xbranch s]
| RTL.Istore(chunk, addr, args, src, s) ->
if Archi.splitlong && chunk = Mint64 then begin
match offset_addressing addr (coqint_of_camlint 4l) with
@@ -295,8 +295,8 @@ let block_of_RTL_instr funsig tyenv = function
(Xbuiltin(ef, args2, res2) ::
movelist (params_of_builtin_res res2) (params_of_builtin_res res1)
[Xbranch s])
- | RTL.Icond(cond, args, s1, s2) ->
- [Xcond(cond, vregs tyenv args, s1, s2)]
+ | RTL.Icond(cond, args, s1, s2, i) ->
+ [Xcond(cond, vregs tyenv args, s1, s2, i)]
| RTL.Ijumptable(arg, tbl) ->
[Xjumptable(vreg tyenv arg, tbl)]
| RTL.Ireturn None ->
@@ -364,7 +364,7 @@ let live_before instr after =
if VSet.mem res after
then vset_addlist args (VSet.remove res after)
else after
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
if VSet.mem dst after
then vset_addlist args (VSet.remove dst after)
else after
@@ -380,7 +380,7 @@ let live_before instr after =
vset_addargs args (vset_removeres res after)
| Xbranch s ->
after
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, _) ->
List.fold_right VSet.add args after
| Xjumptable(arg, tbl) ->
VSet.add arg after
@@ -459,7 +459,7 @@ let dce_instr instr after k =
if VSet.mem res after
then instr :: k
else k
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
if VSet.mem dst after
then instr :: k
else k
@@ -550,7 +550,7 @@ let spill_costs f =
(* temps must not be spilled *)
| Xop(op, args, res) ->
charge_list 10 1 args; charge 10 1 res
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
charge_list 10 1 args; charge 10 1 dst
| Xstore(chunk, addr, args, src) ->
charge_list 10 1 args; charge 10 1 src
@@ -575,7 +575,7 @@ let spill_costs f =
charge_list 10 1 (params_of_builtin_res res)
end
| Xbranch _ -> ()
- | Xcond(cond, args, _, _) ->
+ | Xcond(cond, args, _, _, _) ->
charge_list 10 1 args
| Xjumptable(arg, _) ->
charge 10 1 arg
@@ -677,7 +677,7 @@ let add_interfs_instr g instr live =
(vset_addlist (res :: argl) (VSet.remove res live))
end;
add_interfs_destroyed g (VSet.remove res live) (destroyed_by_op op)
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
add_interfs_def g dst live;
add_interfs_destroyed g (VSet.remove dst live)
(destroyed_by_load chunk addr)
@@ -718,7 +718,7 @@ let add_interfs_instr g instr live =
end
| Xbranch s ->
()
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, _) ->
add_interfs_destroyed g live (destroyed_by_cond cond)
| Xjumptable(arg, tbl) ->
add_interfs_destroyed g live destroyed_by_jumptable
@@ -782,7 +782,7 @@ let tospill_instr alloc instr ts =
ts
| Xop(op, args, res) ->
addlist_tospill alloc args (add_tospill alloc res ts)
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
addlist_tospill alloc args (add_tospill alloc dst ts)
| Xstore(chunk, addr, args, src) ->
addlist_tospill alloc args (add_tospill alloc src ts)
@@ -797,7 +797,7 @@ let tospill_instr alloc instr ts =
(addlist_tospill alloc (params_of_builtin_res res) ts)
| Xbranch s ->
ts
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, _) ->
addlist_tospill alloc args ts
| Xjumptable(arg, tbl) ->
add_tospill alloc arg ts
@@ -964,10 +964,10 @@ let spill_instr tospill eqs instr =
add res tmp (kill tmp (kill res eqs2)))
end
end
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
let (args', c1, eqs1) = reload_vars tospill eqs args in
let (dst', c2, eqs2) = save_var tospill eqs1 dst in
- (c1 @ Xload(chunk, addr, args', dst') :: c2, eqs2)
+ (c1 @ Xload(trap, chunk, addr, args', dst') :: c2, eqs2)
| Xstore(chunk, addr, args, src) ->
let (args', c1, eqs1) = reload_vars tospill eqs args in
let (src', c2, eqs2) = reload_var tospill eqs1 src in
@@ -990,9 +990,9 @@ let spill_instr tospill eqs instr =
(c1 @ Xbuiltin(ef, args', res') :: c2, eqs2)
| Xbranch s ->
([instr], eqs)
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, i) ->
let (args', c1, eqs1) = reload_vars tospill eqs args in
- (c1 @ [Xcond(cond, args', s1, s2)], eqs1)
+ (c1 @ [Xcond(cond, args', s1, s2, i)], eqs1)
| Xjumptable(arg, tbl) ->
let (arg', c1, eqs1) = reload_var tospill eqs arg in
(c1 @ [Xjumptable(arg', tbl)], eqs1)
@@ -1067,7 +1067,7 @@ let make_parmove srcs dsts itmp ftmp k =
| Locations.S(sl, ofs, ty), R rd ->
code := LTL.Lgetstack(sl, ofs, ty, rd) :: !code
| Locations.S(sls, ofss, tys), Locations.S(sld, ofsd, tyd) ->
- let tmp = temp_for (class_of_type tys) in
+ let tmp = temp_for (Machregsaux.class_of_type tys) in
(* code will be reversed at the end *)
code := LTL.Lsetstack(tmp, sld, ofsd, tyd) ::
LTL.Lgetstack(sls, ofss, tys, tmp) :: !code
@@ -1115,8 +1115,8 @@ let transl_instr alloc instr k =
LTL.Lop(Omove, [rarg1], rres) ::
LTL.Lop(op, rres :: rargl, rres) :: k
end
- | Xload(chunk, addr, args, dst) ->
- LTL.Lload(chunk, addr, mregs_of alloc args, mreg_of alloc dst) :: k
+ | Xload(trap, chunk, addr, args, dst) ->
+ LTL.Lload(trap, chunk, addr, mregs_of alloc args, mreg_of alloc dst) :: k
| Xstore(chunk, addr, args, src) ->
LTL.Lstore(chunk, addr, mregs_of alloc args, mreg_of alloc src) :: k
| Xcall(sg, vos, args, res) ->
@@ -1128,8 +1128,8 @@ let transl_instr alloc instr k =
AST.map_builtin_res (mreg_of alloc) res) :: k
| Xbranch s ->
LTL.Lbranch s :: []
- | Xcond(cond, args, s1, s2) ->
- LTL.Lcond(cond, mregs_of alloc args, s1, s2) :: []
+ | Xcond(cond, args, s1, s2, i) ->
+ LTL.Lcond(cond, mregs_of alloc args, s1, s2, i) :: []
| Xjumptable(arg, tbl) ->
LTL.Ljumptable(mreg_of alloc arg, tbl) :: []
| Xreturn optarg ->
diff --git a/backend/Renumber.v b/backend/Renumber.v
index 10f58251..2727b979 100644
--- a/backend/Renumber.v
+++ b/backend/Renumber.v
@@ -43,12 +43,12 @@ Definition renum_instr (i: instruction) : instruction :=
match i with
| Inop s => Inop (renum_pc s)
| Iop op args res s => Iop op args res (renum_pc s)
- | Iload chunk addr args res s => Iload chunk addr args res (renum_pc s)
+ | Iload trap chunk addr args res s => Iload trap chunk addr args res (renum_pc s)
| Istore chunk addr args src s => Istore chunk addr args src (renum_pc s)
| Icall sg ros args res s => Icall sg ros args res (renum_pc s)
| Itailcall sg ros args => i
| Ibuiltin ef args res s => Ibuiltin ef args res (renum_pc s)
- | Icond cond args s1 s2 => Icond cond args (renum_pc s1) (renum_pc s2)
+ | Icond cond args s1 s2 info => Icond cond args (renum_pc s1) (renum_pc s2) info
| Ijumptable arg tbl => Ijumptable arg (List.map renum_pc tbl)
| Ireturn or => i
end.
diff --git a/backend/Renumberproof.v b/backend/Renumberproof.v
index 7cda9425..2e161965 100644
--- a/backend/Renumberproof.v
+++ b/backend/Renumberproof.v
@@ -175,6 +175,18 @@ Proof.
rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
eapply exec_Iload; eauto.
constructor; auto. eapply reach_succ; eauto. simpl; auto.
+ (* load notrap1 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = None).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap1; eauto.
+ constructor; auto. eapply reach_succ; eauto. simpl; auto.
+ (* load notrap2 *)
+ econstructor; split.
+ assert (eval_addressing tge sp addr rs ## args = Some a).
+ rewrite <- H0. apply eval_addressing_preserved. exact symbols_preserved.
+ eapply exec_Iload_notrap2; eauto.
+ constructor; auto. eapply reach_succ; eauto. simpl; auto.
(* store *)
econstructor; split.
assert (eval_addressing tge sp addr rs ## args = Some a).
diff --git a/backend/SelectDiv.vp b/backend/SelectDiv.vp
index d91797c5..9f852616 100644
--- a/backend/SelectDiv.vp
+++ b/backend/SelectDiv.vp
@@ -15,10 +15,13 @@
Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats.
-Require Import Op CminorSel SelectOp SplitLong SelectLong.
+Require Import Op CminorSel OpHelpers SelectOp SplitLong SelectLong.
Local Open Scope cminorsel_scope.
+Section SELECT.
+Context {hf: helper_functions}.
+
Definition is_intconst (e: expr) : option int :=
match e with
| Eop (Ointconst n) _ => Some n
@@ -221,10 +224,6 @@ Definition mods (e1: expr) (e2: expr) :=
(** 64-bit integer divisions *)
-Section SELECT.
-
-Context {hf: helper_functions}.
-
Definition modl_from_divl (equo: expr) (n: int64) :=
subl (Eletvar O) (mullimm n equo).
@@ -241,8 +240,8 @@ Definition divlu (e1 e2: expr) :=
divlu_base e1 e2
else
match divlu_mul_params (Int64.unsigned n2) with
- | None => divlu_base e1 e2
- | Some(p, m) => Elet e1 (divlu_mull p m)
+ | _ => divlu_base e1 e2
+ (* | Some(p, m) => Elet e1 (divlu_mull p m) *) (* FIXME - hack K1 *)
end
end
| _, _ => divlu_base e1 e2
@@ -258,8 +257,8 @@ Definition modlu (e1 e2: expr) :=
modlu_base e1 e2
else
match divlu_mul_params (Int64.unsigned n2) with
- | None => modlu_base e1 e2
- | Some(p, m) => Elet e1 (modl_from_divl (divlu_mull p m) n2)
+ | _ => modlu_base e1 e2
+ (* | Some(p, m) => Elet e1 (modl_from_divl (divlu_mull p m) n2) *) (* FIXME - hack K1 *)
end
end
| _, _ => modlu_base e1 e2
@@ -285,8 +284,8 @@ Definition divls (e1 e2: expr) :=
divls_base e1 e2
else
match divls_mul_params (Int64.signed n2) with
- | None => divls_base e1 e2
- | Some(p, m) => Elet e1 (divls_mull p m)
+ | _ => divls_base e1 e2
+ (* | Some(p, m) => Elet e1 (divls_mull p m) *) (* FIXME - hack K1 *)
end
end
| _, _ => divls_base e1 e2
@@ -304,40 +303,38 @@ Definition modls (e1 e2: expr) :=
modls_base e1 e2
else
match divls_mul_params (Int64.signed n2) with
- | None => modls_base e1 e2
- | Some(p, m) => Elet e1 (modl_from_divl (divls_mull p m) n2)
+ | _ => modls_base e1 e2
+ (* | Some(p, m) => Elet e1 (modl_from_divl (divls_mull p m) n2) *) (* FIXME - hack K1 *)
end
end
| _, _ => modls_base e1 e2
end.
-
-End SELECT.
-
+
(** Floating-point division by a constant can also be turned into a FP
multiplication by the inverse constant, but only for powers of 2. *)
Definition divfimm (e: expr) (n: float) :=
match Float.exact_inverse n with
| Some n' => Eop Omulf (e ::: Eop (Ofloatconst n') Enil ::: Enil)
- | None => Eop Odivf (e ::: Eop (Ofloatconst n) Enil ::: Enil)
+ | None => divf_base e (Eop (Ofloatconst n) Enil)
end.
Nondetfunction divf (e1: expr) (e2: expr) :=
match e2 with
| Eop (Ofloatconst n2) Enil => divfimm e1 n2
- | _ => Eop Odivf (e1 ::: e2 ::: Enil)
+ | _ => divf_base e1 e2
end.
Definition divfsimm (e: expr) (n: float32) :=
match Float32.exact_inverse n with
| Some n' => Eop Omulfs (e ::: Eop (Osingleconst n') Enil ::: Enil)
- | None => Eop Odivfs (e ::: Eop (Osingleconst n) Enil ::: Enil)
+ | None => divfs_base e (Eop (Osingleconst n) Enil)
end.
Nondetfunction divfs (e1: expr) (e2: expr) :=
match e2 with
| Eop (Osingleconst n2) Enil => divfsimm e1 n2
- | _ => Eop Odivfs (e1 ::: e2 ::: Enil)
+ | _ => divfs_base e1 e2
end.
-
+End SELECT. \ No newline at end of file
diff --git a/backend/SelectDivproof.v b/backend/SelectDivproof.v
index 9d581ec9..3f91b1ba 100644
--- a/backend/SelectDivproof.v
+++ b/backend/SelectDivproof.v
@@ -15,6 +15,7 @@
Require Import Zquot Coqlib Zbits.
Require Import AST Integers Floats Values Memory Globalenvs Events.
Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectOpproof SplitLong SplitLongproof SelectLong SelectLongproof SelectDiv.
Local Open Scope cminorsel_scope.
@@ -587,12 +588,12 @@ Proof.
- destruct (Compopts.optim_for_size tt).
+ eapply eval_modu_base; eauto. EvalOp.
+ destruct (divu_mul_params (Int.unsigned n2)) as [[p M] | ] eqn:PARAMS.
- * econstructor; split.
+ * econstructor; split.
econstructor; eauto. eapply eval_mod_from_div.
eapply eval_divu_mul; eauto. simpl; eauto. simpl; eauto.
rewrite Int.modu_divu. auto.
red; intros; subst n2; discriminate.
- * eapply eval_modu_base; eauto. EvalOp.
+ * eapply eval_modu_base; eauto. EvalOp.
Qed.
Theorem eval_modu:
@@ -704,7 +705,7 @@ Proof.
|| Int.eq i (Int.repr Int.min_signed) && Int.eq n2 Int.mone) eqn:Z2; inv DIV.
destruct (Int.is_power2 n2) as [l | ] eqn:P2.
- destruct (Int.ltu l (Int.repr 31)) eqn:LT31.
- + exploit (eval_shrximm ge sp e m (Vint i :: le) (Eletvar O)).
+ + exploit (eval_shrximm prog sp e m (Vint i :: le) (Eletvar O)).
constructor. simpl; eauto. eapply Val.divs_pow2; eauto.
intros [v1 [X LD]]. inv LD.
econstructor; split. econstructor. eauto.
@@ -788,10 +789,10 @@ Proof.
+ destruct (Int64.is_power2' n2) as [l|] eqn:POW.
* exploit Val.divlu_pow2; eauto. intros EQ; subst z. apply eval_shrluimm; auto.
* destruct (Compopts.optim_for_size tt). eapply eval_divlu_base; eauto.
- destruct (divlu_mul_params (Int64.unsigned n2)) as [[p M]|] eqn:PARAMS.
+ (* destruct (divlu_mul_params (Int64.unsigned n2)) as [[p M]|] eqn:PARAMS.
** destruct x; simpl in H1; try discriminate.
destruct (Int64.eq n2 Int64.zero); inv H1.
- econstructor; split; eauto. econstructor. eauto. eapply eval_divlu_mull; eauto.
+ econstructor; split; eauto. econstructor. eauto. eapply eval_divlu_mull; eauto. *) (* FIXME - K1 hack *)
** eapply eval_divlu_base; eauto.
- eapply eval_divlu_base; eauto.
Qed.
@@ -813,14 +814,14 @@ Proof.
+ destruct (Int64.is_power2 n2) as [l|] eqn:POW.
* exploit Val.modlu_pow2; eauto. intros EQ; subst z. eapply eval_andl; eauto. apply eval_longconst.
* destruct (Compopts.optim_for_size tt). eapply eval_modlu_base; eauto.
- destruct (divlu_mul_params (Int64.unsigned n2)) as [[p M]|] eqn:PARAMS.
+ (* destruct (divlu_mul_params (Int64.unsigned n2)) as [[p M]|] eqn:PARAMS.
** destruct x; simpl in H1; try discriminate.
destruct (Int64.eq n2 Int64.zero) eqn:Z; inv H1.
rewrite Int64.modu_divu.
econstructor; split; eauto. econstructor. eauto.
eapply eval_modl_from_divl; eauto.
eapply eval_divlu_mull; eauto.
- red; intros; subst n2; discriminate Z.
+ red; intros; subst n2; discriminate Z. *)
** eapply eval_modlu_base; eauto.
- eapply eval_modlu_base; eauto.
Qed.
@@ -883,12 +884,12 @@ Proof.
** exploit Val.divls_pow2; eauto. intros EQ. eapply eval_shrxlimm; eauto.
** eapply eval_divls_base; eauto.
* destruct (Compopts.optim_for_size tt). eapply eval_divls_base; eauto.
- destruct (divls_mul_params (Int64.signed n2)) as [[p M]|] eqn:PARAMS.
+ (* destruct (divls_mul_params (Int64.signed n2)) as [[p M]|] eqn:PARAMS.
** destruct x; simpl in H1; try discriminate.
destruct (Int64.eq n2 Int64.zero
|| Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq n2 Int64.mone); inv H1.
econstructor; split; eauto. econstructor. eauto.
- eapply eval_divls_mull; eauto.
+ eapply eval_divls_mull; eauto. *)
** eapply eval_divls_base; eauto.
- eapply eval_divls_base; eauto.
Qed.
@@ -925,14 +926,14 @@ Proof.
rewrite Int64.mods_divs. auto.
**eapply eval_modls_base; eauto.
* destruct (Compopts.optim_for_size tt). eapply eval_modls_base; eauto.
- destruct (divls_mul_params (Int64.signed n2)) as [[p M]|] eqn:PARAMS.
+ (* destruct (divls_mul_params (Int64.signed n2)) as [[p M]|] eqn:PARAMS.
** destruct x; simpl in H1; try discriminate.
destruct (Int64.eq n2 Int64.zero
|| Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq n2 Int64.mone); inv H1.
econstructor; split; eauto. econstructor. eauto.
rewrite Int64.mods_divs.
eapply eval_modl_from_divl; auto.
- eapply eval_divls_mull; eauto.
+ eapply eval_divls_mull; eauto. *)
** eapply eval_modls_base; eauto.
- eapply eval_modls_base; eauto.
Qed.
@@ -950,8 +951,8 @@ Proof.
+ inv H0. inv H4. simpl in H6. inv H6. econstructor; split.
repeat (econstructor; eauto).
destruct x; simpl; auto. erewrite Float.div_mul_inverse; eauto.
- + TrivialExists.
-- TrivialExists.
+ + apply eval_divf_base; trivial.
+- apply eval_divf_base; trivial.
Qed.
Theorem eval_divfs:
@@ -965,8 +966,8 @@ Proof.
+ inv H0. inv H4. simpl in H6. inv H6. econstructor; split.
repeat (econstructor; eauto).
destruct x; simpl; auto. erewrite Float32.div_mul_inverse; eauto.
- + TrivialExists.
-- TrivialExists.
+ + apply eval_divfs_base; trivial.
+- apply eval_divfs_base; trivial.
Qed.
End CMCONSTRS.
diff --git a/backend/Selection.v b/backend/Selection.v
index a5bef9ae..8667922f 100644
--- a/backend/Selection.v
+++ b/backend/Selection.v
@@ -26,7 +26,7 @@ Require String.
Require Import Coqlib Maps.
Require Import AST Errors Integers Globalenvs Builtins Switch.
Require Cminor.
-Require Import Op CminorSel Cminortyping.
+Require Import Op CminorSel OpHelpers Cminortyping.
Require Import SelectOp SplitLong SelectLong SelectDiv.
Require Machregs.
@@ -35,12 +35,13 @@ Local Open Scope error_monad_scope.
(** Conversion of conditions *)
-Function condexpr_of_expr (e: expr) : condexpr :=
+Function condexpr_of_expr (e: expr) (expected : option bool) : condexpr :=
match e with
- | Eop (Ocmp c) el => CEcond c el
- | Econdition a b c => CEcondition a (condexpr_of_expr b) (condexpr_of_expr c)
- | Elet a b => CElet a (condexpr_of_expr b)
- | _ => CEcond (Ccompuimm Cne Int.zero) (e ::: Enil)
+ | Eop (Ocmp c) el => CEcond c expected el
+ | Econdition a b c => CEcondition a (condexpr_of_expr b expected)
+ (condexpr_of_expr c expected)
+ | Elet a b => CElet a (condexpr_of_expr b expected)
+ | _ => CEcond (Ccompuimm Cne Int.zero) expected (e ::: Enil)
end.
Function condition_of_expr (e: expr) : condition * exprlist :=
@@ -120,6 +121,7 @@ Definition sel_unop (op: Cminor.unary_operation) (arg: expr) : expr :=
Definition sel_binop (op: Cminor.binary_operation) (arg1 arg2: expr) : expr :=
match op with
+ | Cminor.Oexpect ty => arg1
| Cminor.Oadd => add arg1 arg2
| Cminor.Osub => sub arg1 arg2
| Cminor.Omul => mul arg1 arg2
@@ -166,7 +168,7 @@ Definition sel_select (ty: typ) (cnd ifso ifnot: expr) : expr :=
let (cond, args) := condition_of_expr cnd in
match SelectOp.select ty cond args ifso ifnot with
| Some a => a
- | None => Econdition (condexpr_of_expr cnd) ifso ifnot
+ | None => Econdition (condexpr_of_expr cnd None) ifso ifnot
end.
(** Conversion from Cminor expression to Cminorsel expressions *)
@@ -243,7 +245,8 @@ Definition sel_builtin_res (optid: option ident) : builtin_res ident :=
Function sel_known_builtin (bf: builtin_function) (args: exprlist) :=
match bf, args with
| BI_platform b, _ =>
- SelectOp.platform_builtin b args
+ SelectOp.platform_builtin b args
+(* | BI_standard BI_expect, a1 ::: a2 ::: Enil => Some a1 *)
| BI_standard (BI_select ty), a1 ::: a2 ::: a3 ::: Enil =>
Some (sel_select ty a1 a2 a3)
| BI_standard BI_fabs, a1 ::: Enil =>
@@ -302,16 +305,16 @@ Fixpoint sel_switch (arg: nat) (t: comptree): exitexpr :=
| CTaction act =>
XEexit act
| CTifeq key act t' =>
- XEcondition (condexpr_of_expr (make_cmp_eq (Eletvar arg) key))
+ XEcondition (condexpr_of_expr (make_cmp_eq (Eletvar arg) key) None)
(XEexit act)
(sel_switch arg t')
| CTiflt key t1 t2 =>
- XEcondition (condexpr_of_expr (make_cmp_ltu (Eletvar arg) key))
+ XEcondition (condexpr_of_expr (make_cmp_ltu (Eletvar arg) key) None)
(sel_switch arg t1)
(sel_switch arg t2)
| CTjumptable ofs sz tbl t' =>
XElet (make_sub (Eletvar arg) ofs)
- (XEcondition (condexpr_of_expr (make_cmp_ltu (Eletvar O) sz))
+ (XEcondition (condexpr_of_expr (make_cmp_ltu (Eletvar O) sz) None)
(XEjumptable (make_to_int (Eletvar O)) tbl)
(sel_switch (S arg) t'))
end.
@@ -386,6 +389,22 @@ Definition if_conversion
| _, _ => None
end.
+Definition extract_expect1 (e : Cminor.expr) : option bool :=
+ match e with
+ | Cminor.Ebinop (Cminor.Oexpect ty) e1 (Cminor.Econst (Cminor.Ointconst c)) =>
+ Some (if Int.eq_dec c Int.zero then false else true)
+ | Cminor.Ebinop (Cminor.Oexpect ty) e1 (Cminor.Econst (Cminor.Olongconst c)) =>
+ Some (if Int64.eq_dec c Int64.zero then false else true)
+ | _ => None
+ end.
+
+Definition extract_expect (e : Cminor.expr) : option bool :=
+ match e with
+ | Cminor.Ebinop (Cminor.Ocmpu Cne) e1 (Cminor.Econst (Cminor.Ointconst c)) =>
+ if Int.eq_dec c Int.zero then extract_expect1 e1 else None
+ | _ => extract_expect1 e
+ end.
+
(** Conversion from Cminor statements to Cminorsel statements. *)
Fixpoint sel_stmt (ki: known_idents) (env: typenv) (s: Cminor.stmt) : res stmt :=
@@ -413,8 +432,10 @@ Fixpoint sel_stmt (ki: known_idents) (env: typenv) (s: Cminor.stmt) : res stmt :
match if_conversion ki env e ifso ifnot with
| Some s => OK s
| None =>
- do ifso' <- sel_stmt ki env ifso; do ifnot' <- sel_stmt ki env ifnot;
- OK (Sifthenelse (condexpr_of_expr (sel_expr e)) ifso' ifnot')
+ do ifso' <- sel_stmt ki env ifso;
+ do ifnot' <- sel_stmt ki env ifnot;
+ OK (Sifthenelse (condexpr_of_expr (sel_expr e)
+ (extract_expect e)) ifso' ifnot')
end
| Cminor.Sloop body =>
do body' <- sel_stmt ki env body; OK (Sloop body')
@@ -518,11 +539,19 @@ Definition get_helpers (defmap: PTree.t globdef) : res helper_functions :=
do i64_sar <- lookup_helper globs "__compcert_i64_sar" sig_li_l ;
do i64_umulh <- lookup_helper globs "__compcert_i64_umulh" sig_ll_l ;
do i64_smulh <- lookup_helper globs "__compcert_i64_smulh" sig_ll_l ;
+ do i32_sdiv <- lookup_helper globs "__compcert_i32_sdiv" sig_ii_i ;
+ do i32_udiv <- lookup_helper globs "__compcert_i32_udiv" sig_ii_i ;
+ do i32_smod <- lookup_helper globs "__compcert_i32_smod" sig_ii_i ;
+ do i32_umod <- lookup_helper globs "__compcert_i32_umod" sig_ii_i ;
+ do f64_div <- lookup_helper globs "__compcert_f64_div" sig_ff_f ;
+ do f32_div <- lookup_helper globs "__compcert_f32_div" sig_ss_s ;
OK (mk_helper_functions
i64_dtos i64_dtou i64_stod i64_utod i64_stof i64_utof
i64_sdiv i64_udiv i64_smod i64_umod
i64_shl i64_shr i64_sar
- i64_umulh i64_smulh).
+ i64_umulh i64_smulh
+ i32_sdiv i32_udiv i32_smod i32_umod
+ f64_div f32_div).
(** Conversion of programs. *)
diff --git a/backend/Selectionaux.ml b/backend/Selectionaux.ml
index 8acae8f2..fcf8e238 100644
--- a/backend/Selectionaux.ml
+++ b/backend/Selectionaux.ml
@@ -39,6 +39,7 @@ let cost_unop = function
| Osingleoflong | Osingleoflongu -> assert false
let cost_binop = function
+ | Oexpect _ -> 0
| Oadd | Osub -> 1
| Omul -> 2
| Odiv | Odivu | Omod | Omodu -> assert false
@@ -73,13 +74,13 @@ let fast_cmove ty =
| "arm", _ ->
(match ty with Tint | Tfloat | Tsingle -> true | _ -> false)
| "powerpc", "e5500" ->
- (match ty with Tint -> true | Tlong -> true | _ -> false)
+ (match ty with Tint | Tlong -> true | _ -> false)
| "powerpc", _ -> false
| "riscV", _ -> false
| "x86", _ ->
- (match ty with Tint -> true | Tlong -> Archi.ptr64 | _ -> false)
- | _, _ ->
- assert false
+ (match ty with Tint -> true | Tlong -> Archi.ptr64 | _ -> false)
+ | "kvx", _ -> true
+ | a, m -> failwith (Printf.sprintf "fast_cmove: unknown arch %s %s" a m)
(* The if-conversion heuristic depend on the
-fif-conversion and -Obranchless flags.
diff --git a/backend/Selectionproof.v b/backend/Selectionproof.v
index 4755ab79..e737ba4b 100644
--- a/backend/Selectionproof.v
+++ b/backend/Selectionproof.v
@@ -17,6 +17,7 @@ Require Import Coqlib Maps.
Require Import AST Linking Errors Integers.
Require Import Values Memory Builtins Events Globalenvs Smallstep.
Require Import Switch Cminor Op CminorSel Cminortyping.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectDiv SplitLong SelectLong Selection.
Require Import SelectOpproof SelectDivproof SplitLongproof SelectLongproof.
@@ -87,7 +88,7 @@ Lemma get_helpers_correct:
forall p hf,
get_helpers (prog_defmap p) = OK hf -> helper_functions_declared p hf.
Proof.
- intros. monadInv H. red; simpl. auto 20 using lookup_helper_correct.
+ intros. monadInv H. red; simpl. auto 22 using lookup_helper_correct.
Qed.
Theorem transf_program_match:
@@ -107,7 +108,7 @@ Proof.
{ unfold helper_declared; intros.
destruct (prog_defmap_linkorder _ _ _ _ H0 H1) as (gd & P & Q).
inv Q. inv H3. auto. }
- red in H. decompose [Logic.and] H; clear H. red; auto 20.
+ red in H. decompose [Logic.and] H; clear H. red; auto 22.
Qed.
(** * Correctness of the instruction selection functions for expressions *)
@@ -175,7 +176,7 @@ Proof.
generalize (match_program_defmap _ _ _ _ _ TRANSF id).
unfold Cminor.fundef; rewrite H; intros R; inv R. inv H2.
destruct H4 as (cu & A & B). monadInv B. auto. }
- unfold helper_functions_declared; intros. decompose [Logic.and] H; clear H. auto 20.
+ unfold helper_functions_declared; intros. decompose [Logic.and] H; clear H. auto 22.
Qed.
Section CMCONSTR.
@@ -195,12 +196,12 @@ Variable e: env.
Variable m: mem.
Lemma eval_condexpr_of_expr:
- forall a le v b,
+ forall expected a le v b,
eval_expr tge sp e m le a v ->
Val.bool_of_val v b ->
- eval_condexpr tge sp e m le (condexpr_of_expr a) b.
+ eval_condexpr tge sp e m le (condexpr_of_expr a expected) b.
Proof.
- intros until a. functional induction (condexpr_of_expr a); intros.
+ intros until a. functional induction (condexpr_of_expr a expected); intros.
(* compare *)
inv H. econstructor; eauto.
simpl in H6. inv H6. apply Val.bool_of_val_of_optbool. auto.
@@ -309,46 +310,47 @@ Lemma eval_sel_binop:
exists v', eval_expr tge sp e m le (sel_binop op a1 a2) v' /\ Val.lessdef v v'.
Proof.
destruct op; simpl; intros; FuncInv; try subst v.
- apply eval_add; auto.
- apply eval_sub; auto.
- apply eval_mul; auto.
- eapply eval_divs; eauto.
- eapply eval_divu; eauto.
- eapply eval_mods; eauto.
- eapply eval_modu; eauto.
- apply eval_and; auto.
- apply eval_or; auto.
- apply eval_xor; auto.
- apply eval_shl; auto.
- apply eval_shr; auto.
- apply eval_shru; auto.
- apply eval_addf; auto.
- apply eval_subf; auto.
- apply eval_mulf; auto.
- apply eval_divf; auto.
- apply eval_addfs; auto.
- apply eval_subfs; auto.
- apply eval_mulfs; auto.
- apply eval_divfs; auto.
- eapply eval_addl; eauto.
- eapply eval_subl; eauto.
- eapply eval_mull; eauto.
- eapply eval_divls; eauto.
- eapply eval_divlu; eauto.
- eapply eval_modls; eauto.
- eapply eval_modlu; eauto.
- eapply eval_andl; eauto.
- eapply eval_orl; eauto.
- eapply eval_xorl; eauto.
- eapply eval_shll; eauto.
- eapply eval_shrl; eauto.
- eapply eval_shrlu; eauto.
- apply eval_comp; auto.
- apply eval_compu; auto.
- apply eval_compf; auto.
- apply eval_compfs; auto.
- exists v; split; auto. eapply eval_cmpl; eauto.
- exists v; split; auto. eapply eval_cmplu; eauto.
+ - exists v1; split; trivial. apply Val.lessdef_normalize.
+ - apply eval_add; auto.
+ - apply eval_sub; auto.
+ - apply eval_mul; auto.
+ - eapply eval_divs; eauto.
+ - eapply eval_divu; eauto.
+ - eapply eval_mods; eauto.
+ - eapply eval_modu; eauto.
+ - apply eval_and; auto.
+ - apply eval_or; auto.
+ - apply eval_xor; auto.
+ - apply eval_shl; auto.
+ - apply eval_shr; auto.
+ - apply eval_shru; auto.
+ - apply eval_addf; auto.
+ - apply eval_subf; auto.
+ - apply eval_mulf; auto.
+ - apply eval_divf; auto.
+ - apply eval_addfs; auto.
+ - apply eval_subfs; auto.
+ - apply eval_mulfs; auto.
+ - apply eval_divfs; auto.
+ - eapply eval_addl; eauto.
+ - eapply eval_subl; eauto.
+ - eapply eval_mull; eauto.
+ - eapply eval_divls; eauto.
+ - eapply eval_divlu; eauto.
+ - eapply eval_modls; eauto.
+ - eapply eval_modlu; eauto.
+ - eapply eval_andl; eauto.
+ - eapply eval_orl; eauto.
+ - eapply eval_xorl; eauto.
+ - eapply eval_shll; eauto.
+ - eapply eval_shrl; eauto.
+ - eapply eval_shrlu; eauto.
+ - apply eval_comp; auto.
+ - apply eval_compu; auto.
+ - apply eval_compf; auto.
+ - apply eval_compfs; auto.
+ - exists v; split; auto. eapply eval_cmpl; eauto.
+ - exists v; split; auto. eapply eval_cmplu; eauto.
Qed.
Lemma eval_sel_select:
@@ -781,6 +783,8 @@ Lemma sel_select_opt_correct:
Cminor.eval_expr ge sp e m cond vcond ->
Cminor.eval_expr ge sp e m a1 v1 ->
Cminor.eval_expr ge sp e m a2 v2 ->
+ Val.has_type v1 ty ->
+ Val.has_type v2 ty ->
Val.bool_of_val vcond b ->
env_lessdef e e' -> Mem.extends m m' ->
exists v', eval_expr tge sp e' m' le a v' /\ Val.lessdef (Val.select (Some b) v1 v2 ty) v'.
@@ -790,7 +794,7 @@ Proof.
exploit sel_expr_correct. eexact H0. eauto. eauto. intros (vcond' & EVC & LDC).
exploit sel_expr_correct. eexact H1. eauto. eauto. intros (v1' & EV1 & LD1).
exploit sel_expr_correct. eexact H2. eauto. eauto. intros (v2' & EV2 & LD2).
- assert (Val.bool_of_val vcond' b) by (inv H3; inv LDC; constructor).
+ assert (Val.bool_of_val vcond' b) by (inv H5; inv LDC; constructor).
exploit eval_condition_of_expr. eexact EVC. eauto. rewrite C. intros (vargs' & EVARGS & EVCOND).
exploit eval_select; eauto. intros (v' & X & Y).
exists v'; split; eauto.
@@ -1194,21 +1198,21 @@ Remark find_label_commut:
Proof.
induction s; intros until k'; simpl; intros MC SE; try (monadInv SE); simpl; auto.
(* store *)
- unfold store. destruct (addressing m (sel_expr e)); simpl; auto.
+- unfold store. destruct (addressing m (sel_expr e)); simpl; auto.
(* call *)
- destruct (classify_call (prog_defmap cunit) e); simpl; auto.
+- destruct (classify_call (prog_defmap cunit) e); simpl; auto.
rewrite sel_builtin_nolabel; auto.
(* tailcall *)
- destruct (classify_call (prog_defmap cunit) e); simpl; auto.
+- destruct (classify_call (prog_defmap cunit) e); simpl; auto.
(* builtin *)
- rewrite sel_builtin_nolabel; auto.
+- rewrite sel_builtin_nolabel; auto.
(* seq *)
- exploit (IHs1 (Cminor.Kseq s2 k)). constructor; eauto. eauto.
+- exploit (IHs1 (Cminor.Kseq s2 k)). constructor; eauto. eauto.
destruct (Cminor.find_label lbl s1 (Cminor.Kseq s2 k)) as [[sx kx] | ];
destruct (find_label lbl x (Kseq x0 k')) as [[sy ky] | ];
intuition. apply IHs2; auto.
(* ifthenelse *)
- destruct (if_conversion ki env e s1 s2) as [s|] eqn:IFC.
+- destruct (if_conversion ki env e s1 s2) as [s|] eqn:IFC.
inv SE. exploit if_conversion_nolabel; eauto. intros (A & B & C).
rewrite A, B, C. auto.
monadInv SE; simpl.
@@ -1217,19 +1221,19 @@ Proof.
destruct (find_label lbl x k') as [[sy ky] | ];
intuition. apply IHs2; auto.
(* loop *)
- apply IHs. constructor; auto. simpl; rewrite EQ; auto. auto.
+- apply IHs. constructor; auto. simpl; rewrite EQ; auto. auto.
(* block *)
- apply IHs. constructor; auto. auto.
+- apply IHs. constructor; auto. auto.
(* switch *)
- destruct b.
+- destruct b.
destruct (validate_switch Int64.modulus n l (compile_switch Int64.modulus n l)); inv SE.
simpl; auto.
destruct (validate_switch Int.modulus n l (compile_switch Int.modulus n l)); inv SE.
simpl; auto.
(* return *)
- destruct o; inv SE; simpl; auto.
+- destruct o; inv SE; simpl; auto.
(* label *)
- destruct (ident_eq lbl l). auto. apply IHs; auto.
+- destruct (ident_eq lbl l). auto. apply IHs; auto.
Qed.
Definition measure (s: Cminor.state) : nat :=
diff --git a/backend/SplitLong.vp b/backend/SplitLong.vp
index 694bb0e2..0f240602 100644
--- a/backend/SplitLong.vp
+++ b/backend/SplitLong.vp
@@ -10,47 +10,19 @@
(* *)
(* *********************************************************************)
+(* FIXME: expected branching information not propagated *)
(** Instruction selection for 64-bit integer operations *)
Require String.
Require Import Coqlib.
Require Import AST Integers Floats.
Require Import Op CminorSel.
+Require Import OpHelpers.
Require Import SelectOp.
Local Open Scope cminorsel_scope.
Local Open Scope string_scope.
-(** Some operations on 64-bit integers are transformed into calls to
- runtime library functions. The following type class collects
- the names of these functions. *)
-
-Class helper_functions := mk_helper_functions {
- i64_dtos: ident; (**r float64 -> signed long *)
- i64_dtou: ident; (**r float64 -> unsigned long *)
- i64_stod: ident; (**r signed long -> float64 *)
- i64_utod: ident; (**r unsigned long -> float64 *)
- i64_stof: ident; (**r signed long -> float32 *)
- i64_utof: ident; (**r unsigned long -> float32 *)
- i64_sdiv: ident; (**r signed division *)
- i64_udiv: ident; (**r unsigned division *)
- i64_smod: ident; (**r signed remainder *)
- i64_umod: ident; (**r unsigned remainder *)
- i64_shl: ident; (**r shift left *)
- i64_shr: ident; (**r shift right unsigned *)
- i64_sar: ident; (**r shift right signed *)
- i64_umulh: ident; (**r unsigned multiply high *)
- i64_smulh: ident; (**r signed multiply high *)
-}.
-
-Definition sig_l_l := mksignature (Tlong :: nil) Tlong cc_default.
-Definition sig_l_f := mksignature (Tlong :: nil) Tfloat cc_default.
-Definition sig_l_s := mksignature (Tlong :: nil) Tsingle cc_default.
-Definition sig_f_l := mksignature (Tfloat :: nil) Tlong cc_default.
-Definition sig_ll_l := mksignature (Tlong :: Tlong :: nil) Tlong cc_default.
-Definition sig_li_l := mksignature (Tlong :: Tint :: nil) Tlong cc_default.
-Definition sig_ii_l := mksignature (Tint :: Tint :: nil) Tlong cc_default.
-
Section SELECT.
Context {hf: helper_functions}.
@@ -285,7 +257,7 @@ Definition cmpl_ne_zero (e: expr) :=
Definition cmplu_gen (ch cl: comparison) (e1 e2: expr) :=
splitlong2 e1 e2 (fun h1 l1 h2 l2 =>
- Econdition (CEcond (Ccomp Ceq) (h1:::h2:::Enil))
+ Econdition (CEcond (Ccomp Ceq) None (h1:::h2:::Enil))
(Eop (Ocmp (Ccompu cl)) (l1:::l2:::Enil))
(Eop (Ocmp (Ccompu ch)) (h1:::h2:::Enil))).
@@ -307,7 +279,7 @@ Definition cmplu (c: comparison) (e1 e2: expr) :=
Definition cmpl_gen (ch cl: comparison) (e1 e2: expr) :=
splitlong2 e1 e2 (fun h1 l1 h2 l2 =>
- Econdition (CEcond (Ccomp Ceq) (h1:::h2:::Enil))
+ Econdition (CEcond (Ccomp Ceq) None (h1:::h2:::Enil))
(Eop (Ocmp (Ccompu cl)) (l1:::l2:::Enil))
(Eop (Ocmp (Ccomp ch)) (h1:::h2:::Enil))).
diff --git a/backend/SplitLongproof.v b/backend/SplitLongproof.v
index 1e50b1c2..e45c3a34 100644
--- a/backend/SplitLongproof.v
+++ b/backend/SplitLongproof.v
@@ -16,6 +16,8 @@ Require Import String.
Require Import Coqlib Maps.
Require Import AST Errors Integers Floats.
Require Import Values Memory Globalenvs Builtins Events Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
+Require Import Values Memory Globalenvs Builtins Events Cminor Op CminorSel.
Require Import SelectOp SelectOpproof SplitLong.
Local Open Scope cminorsel_scope.
@@ -23,26 +25,6 @@ Local Open Scope string_scope.
(** * Properties of the helper functions *)
-Definition helper_declared {F V: Type} (p: AST.program (AST.fundef F) V) (id: ident) (name: string) (sg: signature) : Prop :=
- (prog_defmap p)!id = Some (Gfun (External (EF_runtime name sg))).
-
-Definition helper_functions_declared {F V: Type} (p: AST.program (AST.fundef F) V) (hf: helper_functions) : Prop :=
- helper_declared p i64_dtos "__compcert_i64_dtos" sig_f_l
- /\ helper_declared p i64_dtou "__compcert_i64_dtou" sig_f_l
- /\ helper_declared p i64_stod "__compcert_i64_stod" sig_l_f
- /\ helper_declared p i64_utod "__compcert_i64_utod" sig_l_f
- /\ helper_declared p i64_stof "__compcert_i64_stof" sig_l_s
- /\ helper_declared p i64_utof "__compcert_i64_utof" sig_l_s
- /\ helper_declared p i64_sdiv "__compcert_i64_sdiv" sig_ll_l
- /\ helper_declared p i64_udiv "__compcert_i64_udiv" sig_ll_l
- /\ helper_declared p i64_smod "__compcert_i64_smod" sig_ll_l
- /\ helper_declared p i64_umod "__compcert_i64_umod" sig_ll_l
- /\ helper_declared p i64_shl "__compcert_i64_shl" sig_li_l
- /\ helper_declared p i64_shr "__compcert_i64_shr" sig_li_l
- /\ helper_declared p i64_sar "__compcert_i64_sar" sig_li_l
- /\ helper_declared p i64_umulh "__compcert_i64_umulh" sig_ll_l
- /\ helper_declared p i64_smulh "__compcert_i64_smulh" sig_ll_l.
-
(** * Correctness of the instruction selection functions for 64-bit operators *)
Section CMCONSTR.
@@ -55,6 +37,7 @@ Variable sp: val.
Variable e: env.
Variable m: mem.
+Ltac UseHelper := decompose [Logic.and] arith_helpers_correct; eauto.
Ltac DeclHelper := red in HELPERS; decompose [Logic.and] HELPERS; eauto.
Lemma eval_helper:
@@ -342,7 +325,7 @@ Theorem eval_longofint: unary_constructor_sound longofint Val.longofint.
Proof.
red; intros. unfold longofint. destruct (longofint_match a).
- InvEval. econstructor; split. apply eval_longconst. auto.
-- exploit (eval_shrimm ge sp e m (Int.repr 31) (x :: le) (Eletvar 0)). EvalOp.
+- exploit (eval_shrimm prog sp e m (Int.repr 31) (x :: le) (Eletvar 0)). EvalOp.
intros [v1 [A B]].
econstructor; split. EvalOp.
destruct x; simpl; auto.
diff --git a/backend/Splitting.ml b/backend/Splitting.ml
index 40f09c3d..3ca45c3b 100644
--- a/backend/Splitting.ml
+++ b/backend/Splitting.ml
@@ -151,8 +151,8 @@ let ren_instr f maps pc i =
| Inop s -> Inop s
| Iop(op, args, res, s) ->
Iop(op, ren_regs before args, ren_reg after res, s)
- | Iload(chunk, addr, args, dst, s) ->
- Iload(chunk, addr, ren_regs before args, ren_reg after dst, s)
+ | Iload(trap, chunk, addr, args, dst, s) ->
+ Iload(trap, chunk, addr, ren_regs before args, ren_reg after dst, s)
| Istore(chunk, addr, args, src, s) ->
Istore(chunk, addr, ren_regs before args, ren_reg before src, s)
| Icall(sg, ros, args, res, s) ->
@@ -162,8 +162,8 @@ let ren_instr f maps pc i =
| Ibuiltin(ef, args, res, s) ->
Ibuiltin(ef, List.map (AST.map_builtin_arg (ren_reg before)) args,
AST.map_builtin_res (ren_reg after) res, s)
- | Icond(cond, args, s1, s2) ->
- Icond(cond, ren_regs before args, s1, s2)
+ | Icond(cond, args, s1, s2, i) ->
+ Icond(cond, ren_regs before args, s1, s2, i)
| Ijumptable(arg, tbl) ->
Ijumptable(ren_reg before arg, tbl)
| Ireturn optarg ->
diff --git a/backend/Stacking.v b/backend/Stacking.v
index 7b382d05..0e3f2832 100644
--- a/backend/Stacking.v
+++ b/backend/Stacking.v
@@ -133,8 +133,8 @@ Definition transl_instr
end
| Lop op args res =>
Mop (transl_op fe op) args res :: k
- | Lload chunk addr args dst =>
- Mload chunk (transl_addr fe addr) args dst :: k
+ | Lload trap chunk addr args dst =>
+ Mload trap chunk (transl_addr fe addr) args dst :: k
| Lstore chunk addr args src =>
Mstore chunk (transl_addr fe addr) args src :: k
| Lcall sig ros =>
diff --git a/backend/Stackingproof.v b/backend/Stackingproof.v
index 7724c5d6..6d793961 100644
--- a/backend/Stackingproof.v
+++ b/backend/Stackingproof.v
@@ -868,7 +868,7 @@ Qed.
Remark transl_destroyed_by_op:
forall op e, destroyed_by_op (transl_op e op) = destroyed_by_op op.
Proof.
- intros; destruct op; reflexivity.
+ intros; destruct op; try reflexivity; simpl.
Qed.
Remark transl_destroyed_by_load:
@@ -1891,12 +1891,13 @@ Proof.
apply plus_one. econstructor.
instantiate (1 := v'). rewrite <- A. apply eval_operation_preserved.
exact symbols_preserved. eauto.
- econstructor; eauto with coqlib.
- apply agree_regs_set_reg; auto.
- rewrite transl_destroyed_by_op. apply agree_regs_undef_regs; auto.
- apply agree_locs_set_reg; auto. apply agree_locs_undef_locs. auto. apply destroyed_by_op_caller_save.
- apply frame_set_reg. apply frame_undef_regs. exact SEP.
-
+ econstructor; eauto with coqlib;
+ try (apply agree_regs_set_reg; auto);
+ (* generic proof *)
+ solve [
+ (rewrite transl_destroyed_by_op; apply agree_regs_undef_regs; auto) |
+ (apply agree_locs_set_reg; auto; apply agree_locs_undef_locs; auto; apply destroyed_by_op_caller_save) |
+ (apply frame_set_reg; apply frame_undef_regs; exact SEP) ].
- (* Lload *)
assert (exists a',
eval_addressing ge (Vptr sp' Ptrofs.zero) (transl_addr (make_env (function_bounds f)) addr) rs0##args = Some a'
@@ -1917,6 +1918,46 @@ Proof.
apply agree_regs_set_reg. rewrite transl_destroyed_by_load. apply agree_regs_undef_regs; auto. auto.
apply agree_locs_set_reg. apply agree_locs_undef_locs. auto. apply destroyed_by_load_caller_save. auto.
+- (* Lload notrap1*)
+ assert (eval_addressing ge (Vptr sp' Ptrofs.zero) (transl_addr (make_env (function_bounds f)) addr) rs0##args = None) as Haddress.
+ eapply eval_addressing_inject_none; eauto.
+ eapply globalenv_inject_preserves_globals. eapply sep_proj2. eapply sep_proj2. eapply sep_proj2. eexact SEP.
+ eapply agree_reglist; eauto.
+ econstructor; split.
+ apply plus_one. apply exec_Mload_notrap1.
+ rewrite <- Haddress. apply eval_addressing_preserved. exact symbols_preserved.
+ eauto. econstructor; eauto with coqlib.
+ apply agree_regs_set_reg. rewrite transl_destroyed_by_load. apply agree_regs_undef_regs; auto. auto.
+ apply agree_locs_set_reg. apply agree_locs_undef_locs. auto. apply destroyed_by_load_caller_save. auto.
+
+- (* Lload notrap2 *)
+ assert (exists a',
+ eval_addressing ge (Vptr sp' Ptrofs.zero) (transl_addr (make_env (function_bounds f)) addr) rs0##args = Some a'
+ /\ Val.inject j a a').
+ eapply eval_addressing_inject; eauto.
+ eapply globalenv_inject_preserves_globals. eapply sep_proj2. eapply sep_proj2. eapply sep_proj2. eexact SEP.
+ eapply agree_reglist; eauto.
+ destruct H1 as [a' [A B]].
+
+ destruct ( Mem.loadv chunk m' a') as [v'|] eqn:Hloadv.
+ {
+ econstructor; split.
+ apply plus_one. apply exec_Mload with (a:=a') (v:=v'); eauto.
+ try (rewrite <- A; apply eval_addressing_preserved; auto; exact symbols_preserved).
+ econstructor; eauto with coqlib.
+ apply agree_regs_set_reg. rewrite transl_destroyed_by_load. apply agree_regs_undef_regs; auto. auto.
+ apply agree_locs_set_reg. apply agree_locs_undef_locs. auto. apply destroyed_by_load_caller_save. auto.
+ }
+ {
+ econstructor; split.
+ apply plus_one. apply exec_Mload_notrap2 with (a:=a'); eauto.
+ try (rewrite <- A; apply eval_addressing_preserved; auto; exact symbols_preserved).
+
+ econstructor; eauto with coqlib.
+ apply agree_regs_set_reg. rewrite transl_destroyed_by_load. apply agree_regs_undef_regs; auto. auto.
+ apply agree_locs_set_reg. apply agree_locs_undef_locs. auto. apply destroyed_by_load_caller_save. auto.
+ }
+
- (* Lstore *)
assert (exists a',
eval_addressing ge (Vptr sp' Ptrofs.zero) (transl_addr (make_env (function_bounds f)) addr) rs0##args = Some a'
diff --git a/backend/Tailcallproof.v b/backend/Tailcallproof.v
index 7a5be5ed..39fc10fb 100644
--- a/backend/Tailcallproof.v
+++ b/backend/Tailcallproof.v
@@ -436,6 +436,43 @@ Proof.
apply eval_addressing_preserved. exact symbols_preserved. eauto.
econstructor; eauto. apply set_reg_lessdef; auto.
+- (* load notrap1 *)
+ TransfInstr.
+ assert (Val.lessdef_list (rs##args) (rs'##args)). apply regs_lessdef_regs; auto.
+ left.
+ exists (State s' (transf_function f) (Vptr sp0 Ptrofs.zero) pc' (rs'#dst <- Vundef) m'); split.
+ eapply exec_Iload_notrap1.
+ eassumption.
+ eapply eval_addressing_lessdef_none. eassumption.
+ erewrite eval_addressing_preserved.
+ eassumption. exact symbols_preserved.
+
+ econstructor; eauto. apply set_reg_lessdef; auto.
+
+- (* load notrap2 *)
+ TransfInstr.
+ assert (Val.lessdef_list (rs##args) (rs'##args)). apply regs_lessdef_regs; auto.
+ left.
+
+ exploit eval_addressing_lessdef; eauto.
+ intros [a' [ADDR' ALD]].
+
+ destruct (Mem.loadv chunk m' a') eqn:Echunk2.
+ + exists (State s' (transf_function f) (Vptr sp0 Ptrofs.zero) pc' (rs'#dst <- v) m'); split.
+ eapply exec_Iload with (a:=a'). eassumption.
+ erewrite eval_addressing_preserved.
+ eassumption.
+ exact symbols_preserved.
+ assumption.
+ econstructor; eauto. apply set_reg_lessdef; auto.
+ + exists (State s' (transf_function f) (Vptr sp0 Ptrofs.zero) pc' (rs'#dst <- Vundef) m'); split.
+ eapply exec_Iload_notrap2. eassumption.
+ erewrite eval_addressing_preserved.
+ eassumption.
+ exact symbols_preserved.
+ assumption.
+ econstructor; eauto. apply set_reg_lessdef; auto.
+
- (* store *)
TransfInstr.
assert (Val.lessdef_list (rs##args) (rs'##args)). apply regs_lessdef_regs; auto.
diff --git a/backend/Tunneling.v b/backend/Tunneling.v
deleted file mode 100644
index 265e06ba..00000000
--- a/backend/Tunneling.v
+++ /dev/null
@@ -1,192 +0,0 @@
-(* *********************************************************************)
-(* *)
-(* The Compcert verified compiler *)
-(* *)
-(* Xavier Leroy, INRIA Paris-Rocquencourt *)
-(* *)
-(* Copyright Institut National de Recherche en Informatique et en *)
-(* Automatique. All rights reserved. This file is distributed *)
-(* under the terms of the INRIA Non-Commercial License Agreement. *)
-(* *)
-(* *********************************************************************)
-
-(** Branch tunneling (optimization of branches to branches). *)
-
-Require Import FunInd.
-Require Import Coqlib Maps UnionFind.
-Require Import AST.
-Require Import LTL.
-
-(** Branch tunneling shortens sequences of branches (with no intervening
- computations) by rewriting the branch and conditional branch instructions
- so that they jump directly to the end of the branch sequence.
- For example:
-<<
- L1: branch L2; L1: branch L3;
- L2; branch L3; becomes L2: branch L3;
- L3: instr; L3: instr;
- L4: if (cond) goto L1; L4: if (cond) goto L3;
->>
- This optimization can be applied to several of our intermediate
- languages. We choose to perform it on the [LTL] language,
- after register allocation but before code linearization.
- Register allocation can delete instructions (such as dead
- computations or useless moves), therefore there are more
- opportunities for tunneling after allocation than before.
- Symmetrically, prior tunneling helps linearization to produce
- better code, e.g. by revealing that some [branch] instructions are
- dead code (as the "branch L3" in the example above).
-*)
-
-(** The naive implementation of branch tunneling would replace
- any branch to a node [pc] by a branch to the node
- [branch_target f pc], defined as follows:
-<<
- branch_target f pc = branch_target f pc' if f(pc) = branch pc'
- = pc otherwise
->>
- However, this definition can fail to terminate if
- the program can contain loops consisting only of branches, as in
-<<
- L1: branch L1;
->>
- or
-<<
- L1: branch L2;
- L2: branch L1;
->>
- Coq warns us of this fact by not accepting the definition
- of [branch_target] above.
-
- To handle this problem, we proceed in two passes:
-
-- The first pass populates a union-find data structure, adding equalities
- between PCs of blocks that are connected by branches and no other
- computation.
-
-- The second pass rewrites the code, replacing every branch to a node [pc]
- by a branch to the canonical representative of the equivalence class of [pc].
-*)
-
-(** * Construction of the union-find data structure *)
-
-Module U := UnionFind.UF(PTree).
-
-(** We start populating the union-find data structure by adding
- equalities [pc = pc'] for every block [pc: branch pc'] in the function. *)
-
-Definition record_branch (uf: U.t) (pc: node) (b: bblock) : U.t :=
- match b with
- | Lbranch s :: _ => U.union uf pc s
- | _ => uf
- end.
-
-Definition record_branches (f: LTL.function) : U.t :=
- PTree.fold record_branch f.(fn_code) U.empty.
-
-(** An additional optimization opportunity comes from conditional branches.
- Consider a block [pc: cond ifso ifnot]. If the [ifso] case
- and the [ifnot] case jump to the same block [pc']
- (modulo intermediate branches), the block can be simplified into
- [pc: branch pc'], and the equality [pc = pc'] can be added to the
- union-find data structure. *)
-
-(** In rare cases, the extra equation [pc = pc'] introduced by the
- simplification of a conditional branch can trigger further simplifications
- of other conditional branches. We therefore iterate the analysis
- until no optimizable conditional branch remains. *)
-
-(** The code [c] (first component of the [st] triple) starts identical
- to the code [fn.(fn_code)] of the current function, but each time
- conditional branch at [pc] is optimized, we remove the block at
- [pc] from the code [c]. This guarantees termination of the
- iteration. *)
-
-Definition record_cond (st: code * U.t * bool) (pc: node) (b: bblock) : code * U.t * bool :=
- match b with
- | Lcond cond args s1 s2 :: _ =>
- let '(c, u, _) := st in
- if peq (U.repr u s1) (U.repr u s2)
- then (PTree.remove pc c, U.union u pc s1, true)
- else st
- | _ =>
- st
- end.
-
-Definition record_conds_1 (cu: code * U.t) : code * U.t * bool :=
- let (c, u) := cu in PTree.fold record_cond c (c, u, false).
-
-Definition measure_state (cu: code * U.t) : nat :=
- PTree_Properties.cardinal (fst cu).
-
-Function record_conds (cu: code * U.t) {measure measure_state cu} : U.t :=
- let (cu', changed) := record_conds_1 cu in
- if changed then record_conds cu' else snd cu.
-Proof.
- intros [c0 u0] [c1 u1].
- set (P := fun (c: code) (s: code * U.t * bool) =>
- (forall pc, c!pc = None -> (fst (fst s))!pc = c0!pc) /\
- (PTree_Properties.cardinal (fst (fst s))
- + (if snd s then 1 else 0)
- <= PTree_Properties.cardinal c0)%nat).
- assert (A: P c0 (PTree.fold record_cond c0 (c0, u0, false))).
- { apply PTree_Properties.fold_rec; unfold P.
- - intros. destruct H0; split; auto. intros. rewrite <- H in H2. auto.
- - simpl; split; intros. auto. simpl; lia.
- - intros cd [[c u] changed] pc b NONE SOME [HR1 HR2]. simpl. split.
- + intros p EQ. rewrite PTree.gsspec in EQ. destruct (peq p pc); try discriminate.
- unfold record_cond. destruct b as [ | [] b ]; auto.
- destruct (peq (U.repr u s1) (U.repr u s2)); auto.
- simpl. rewrite PTree.gro by auto. auto.
- + unfold record_cond. destruct b as [ | [] b ]; auto.
- destruct (peq (U.repr u s1) (U.repr u s2)); auto.
- simpl in *.
- assert (SOME': c!pc = Some (Lcond cond args s1 s2 :: b)).
- { rewrite HR1 by auto. auto. }
- generalize (PTree_Properties.cardinal_remove SOME').
- destruct changed; lia.
- }
- unfold record_conds_1, measure_state; intros.
- destruct A as [_ A]. rewrite teq in A. simpl in *.
- lia.
-Qed.
-
-Definition record_gotos (f: LTL.function) : U.t :=
- record_conds (f.(fn_code), record_branches f).
-
-(** * Code transformation *)
-
-(** The code transformation rewrites all LTL instruction, replacing every
- successor [s] of every instruction by the canonical representative
- of its equivalence class in the union-find data structure.
- Additionally, [Lcond] conditional branches are turned into [Lbranch]
- unconditional branches whenever possible. *)
-
-Definition tunnel_instr (u: U.t) (i: instruction) : instruction :=
- match i with
- | Lbranch s => Lbranch (U.repr u s)
- | Lcond cond args s1 s2 =>
- let s1' := U.repr u s1 in let s2' := U.repr u s2 in
- if peq s1' s2'
- then Lbranch s1'
- else Lcond cond args s1' s2'
- | Ljumptable arg tbl => Ljumptable arg (List.map (U.repr u) tbl)
- | _ => i
- end.
-
-Definition tunnel_block (u: U.t) (b: bblock) : bblock :=
- List.map (tunnel_instr u) b.
-
-Definition tunnel_function (f: LTL.function) : LTL.function :=
- let u := record_gotos f in
- mkfunction
- (fn_sig f)
- (fn_stacksize f)
- (PTree.map1 (tunnel_block u) (fn_code f))
- (U.repr u (fn_entrypoint f)).
-
-Definition tunnel_fundef (f: LTL.fundef) : LTL.fundef :=
- transf_fundef tunnel_function f.
-
-Definition tunnel_program (p: LTL.program) : LTL.program :=
- transform_program tunnel_fundef p.
diff --git a/backend/Tunnelinglibs.ml b/backend/Tunnelinglibs.ml
new file mode 100644
index 00000000..010595be
--- /dev/null
+++ b/backend/Tunnelinglibs.ml
@@ -0,0 +1,272 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Pierre Goutagny ENS-Lyon, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(*
+
+This file implements the core functions of the tunneling passes, for both RTL
+and LTL, by using a simplified CFG as a transparent interface
+
+See [LTLTunneling.v]/[LTLTunnelingaux.ml] and [RTLTunneling.v]/[RTLTunnelingaux.ml].
+
+*)
+
+open Maps
+open Camlcoq
+
+(* type of labels in the cfg *)
+type label = int * P.t
+
+(* instructions under analyzis *)
+type simple_inst = (* a simplified view of instructions *)
+ BRANCH of node
+| COND of node * node
+| OTHER
+and node = {
+ lab: label;
+ mutable inst: simple_inst;
+ mutable link: node; (* link in the union-find: itself for non "nop"-nodes, target of the "nop" otherwise *)
+ mutable dist: int;
+ mutable tag: int
+ }
+
+type positive = P.t
+type integer = Z.t
+
+(* type of the (simplified) CFG *)
+type cfg = {
+ nodes: (int, node) Hashtbl.t;
+ mutable rems: node list; (* remaining conditions that may become lbranch or not *)
+ mutable num_rems: int;
+ mutable iter_num: int (* number of iterations in elimination of conditions *)
+ }
+
+exception BugOnPC of int
+
+(* keeps track of the total number of nops seen, for debugging purposes *)
+let nopcounter = ref 0
+
+(* General functions that do not require language-specific context, and that
+ are used for building language-specific functions *)
+
+let rec target c n = (* inspired from the "find" of union-find algorithm *)
+ match n.inst with
+ | COND(s1,s2) ->
+ if n.link != n
+ then update c n
+ else if n.tag < c.iter_num then (
+ (* we try to change the condition ... *)
+ n.tag <- c.iter_num; (* ... but at most once by iteration *)
+ let ts1 = target c s1 in
+ let ts2 = target c s2 in
+ if ts1 == ts2 then (n.link <- ts1; ts1) else n
+ ) else n
+ | _ ->
+ if n.link != n
+ then update c n
+ else n
+and update c n =
+ let t = target c n.link in
+ n.link <- t; t
+
+let get_node c p =
+ let li = P.to_int p in
+ try
+ Hashtbl.find c.nodes li
+ with
+ Not_found ->
+ let rec n = { lab = (li, p); inst = OTHER; link = n ; dist = 0; tag = 0 } in
+ Hashtbl.add c.nodes li n;
+ n
+
+let set_branch c p s =
+ let li = P.to_int p in
+ try
+ let n = Hashtbl.find c.nodes li in
+ n.inst <- BRANCH s;
+ n.link <- target c s
+ with
+ Not_found ->
+ let n = { lab = (li,p); inst = BRANCH s; link = target c s; dist = 0; tag = 0 } in
+ Hashtbl.add c.nodes li n
+
+let get td pc =
+ match PTree.get pc td with
+ | Some p -> let (t0, d) = p in (t0, d)
+ | None -> (pc, Z.of_uint 0)
+
+let lab_i (n: node): int = fst n.lab
+let lab_p (n: node): P.t = snd n.lab
+
+let undef_dist = -1
+let self_dist = undef_dist-1
+let rec dist n =
+ if n.dist = undef_dist
+ then (
+ n.dist <- self_dist; (* protection against an unexpected loop in the data-structure *)
+ n.dist <-
+ (match n.inst with
+ | OTHER -> 0
+ | BRANCH p -> 1 + dist p
+ | COND (p1,p2) -> 1 + (max (dist p1) (dist p2)));
+ n.dist
+ ) else if n.dist=self_dist then raise (BugOnPC (lab_i n))
+ else n.dist
+
+let string_of_labeli nodes ipc =
+ try
+ let pc = Hashtbl.find nodes ipc in
+ if pc.link == pc
+ then Printf.sprintf "(Target@%d)" (dist pc)
+ else Printf.sprintf "(Nop %d @%d)" (lab_i pc.link) (dist pc)
+ with
+ Not_found -> ""
+
+(*
+ * When given the necessary types and options as context, and then some
+ * language-specific functions that cannot be factorised between LTL and RTL, the
+ * `Tunneling` functor returns a module containing the corresponding
+ * `branch_target` function.
+ *)
+
+module Tunneling = functor
+ (* Language-specific types *)
+ (LANG: sig
+ type code_unit (* the type of a node of the code cfg (an instruction or a bblock *)
+ type funct (* type of internal functions *)
+ end)
+
+ (* Compilation options for debugging *)
+ (OPT: sig
+ val langname: string
+ val limit_tunneling: int option (* for debugging: [Some x] limit the number of iterations *)
+ val debug_flag: bool ref
+ val final_dump: bool (* set to true to have a more verbose debugging *)
+ end)
+ -> struct
+
+ (* The `debug` function uses values from `OPT`, and is used in functions passed to `F`
+ so it must be defined between the two *)
+ let debug fmt =
+ if !OPT.debug_flag then Printf.eprintf fmt
+ else Printf.ifprintf stderr fmt
+
+ module T
+ (* Language-specific functions *)
+ (FUNS: sig
+ (* build [c.nodes] and accumulate in [acc] conditions at beginning of LTL basic-blocks *)
+ val build_simplified_cfg: cfg -> node list -> positive -> LANG.code_unit -> node list
+ val print_code_unit: cfg -> bool -> int * LANG.code_unit -> bool
+ val fn_code: LANG.funct -> LANG.code_unit PTree.t
+ val fn_entrypoint: LANG.funct -> positive
+ val check_code_unit: (positive * integer) PTree.t -> positive -> LANG.code_unit -> unit
+ end)
+ (* only export what's needed *)
+ : sig val branch_target: LANG.funct -> (positive * integer) PTree.t end
+ = struct
+
+ (* try to change a condition into a branch [acc] is the current accumulator of
+ conditions to consider in the next iteration of repeat_change_cond *)
+ let try_change_cond c acc pc =
+ match pc.inst with
+ | COND(s1,s2) ->
+ let ts1 = target c s1 in
+ let ts2 = target c s2 in
+ if ts1 == ts2 then (
+ pc.link <- ts1;
+ c.num_rems <- c.num_rems - 1;
+ acc
+ ) else
+ pc::acc
+ | _ -> raise (BugOnPC (lab_i pc)) (* COND expected *)
+
+ (* repeat [try_change_cond] until no condition is changed into a branch *)
+ let rec repeat_change_cond c =
+ c.iter_num <- c.iter_num + 1;
+ debug "++ %sTunneling.branch_target %d: remaining number of conds to consider = %d\n" OPT.langname (c.iter_num) (c.num_rems);
+ let old = c.num_rems in
+ c.rems <- List.fold_left (try_change_cond c) [] c.rems;
+ let curr = c.num_rems in
+ let continue =
+ match OPT.limit_tunneling with
+ | Some n -> curr < old && c.iter_num < n
+ | None -> curr < old
+ in
+ if continue
+ then repeat_change_cond c
+
+
+ (*********************************************)
+ (*** START: printing and debugging functions *)
+
+ let print_cfg (f: LANG.funct) c =
+ let a = Array.of_list (PTree.fold (fun acc pc cu -> (P.to_int pc,cu)::acc) (FUNS.fn_code f) []) in
+ Array.fast_sort (fun (i1,_) (i2,_) -> i2 - i1) a;
+ let ep = P.to_int (FUNS.fn_entrypoint f) in
+ debug "entrypoint: %d %s\n" ep (string_of_labeli c.nodes ep);
+ let println = Array.fold_left (FUNS.print_code_unit c) false a in
+ (if println then debug "\n");debug "remaining cond:";
+ List.iter (fun n -> debug "%d " (lab_i n)) c.rems;
+ debug "\n"
+
+
+ (*************************************************************)
+ (* Copy-paste of the extracted code of the verifier *)
+ (* with [raise (BugOnPC (P.to_int pc))] instead of [Error.*] *)
+
+ (** val check_code : coq_UF -> code -> unit res **)
+
+ let check_code td c =
+ PTree.fold (fun _ pc cu -> FUNS.check_code_unit td pc cu) c (())
+
+ (*** END: copy-paste & debugging functions *******)
+
+ (* compute the final distance of each nop nodes to its target *)
+ let final_export f c =
+ let count = ref 0 in
+ let filter_nops_init_dist _ n acc =
+ let tn = target c n in
+ if tn == n
+ then (
+ n.dist <- 0; (* force [n] to be a base case in the recursion of [dist] *)
+ acc
+ ) else (
+ n.dist <- undef_dist; (* force [dist] to compute the actual [n.dist] *)
+ count := !count+1;
+ n::acc
+ )
+ in
+ let nops = Hashtbl.fold filter_nops_init_dist c.nodes [] in
+ let res = List.fold_left (fun acc n -> PTree.set (lab_p n) (lab_p n.link, Z.of_uint (dist n)) acc) PTree.empty nops in
+ debug "* %sTunneling.branch_target: initial number of nops = %d\n" OPT.langname !nopcounter;
+ debug "* %sTunneling.branch_target: final number of eliminated nops = %d\n" OPT.langname !count;
+ res
+
+ let branch_target f =
+ debug "* %sTunneling.branch_target: starting on a new function\n" OPT.langname;
+ if OPT.limit_tunneling <> None then debug "* WARNING: limit_tunneling <> None\n";
+ let c = { nodes = Hashtbl.create 100; rems = []; num_rems = 0; iter_num = 0 } in
+ c.rems <- PTree.fold (FUNS.build_simplified_cfg c) (FUNS.fn_code f) [];
+ repeat_change_cond c;
+ let res = final_export f c in
+ if !OPT.debug_flag then (
+ try
+ check_code res (FUNS.fn_code f);
+ if OPT.final_dump then print_cfg f c;
+ with e -> (
+ print_cfg f c;
+ check_code res (FUNS.fn_code f)
+ )
+ );
+ res
+ end
+end
diff --git a/backend/Tunnelingproof.v b/backend/Tunnelingproof.v
deleted file mode 100644
index 68913fc9..00000000
--- a/backend/Tunnelingproof.v
+++ /dev/null
@@ -1,714 +0,0 @@
-(* *********************************************************************)
-(* *)
-(* The Compcert verified compiler *)
-(* *)
-(* Xavier Leroy, INRIA Paris-Rocquencourt *)
-(* *)
-(* Copyright Institut National de Recherche en Informatique et en *)
-(* Automatique. All rights reserved. This file is distributed *)
-(* under the terms of the INRIA Non-Commercial License Agreement. *)
-(* *)
-(* *********************************************************************)
-
-(** Correctness proof for the branch tunneling optimization. *)
-
-Require Import FunInd.
-Require Import Coqlib Maps UnionFind.
-Require Import AST Linking.
-Require Import Values Memory Events Globalenvs Smallstep.
-Require Import Op Locations LTL.
-Require Import Tunneling.
-
-Definition match_prog (p tp: program) :=
- match_program (fun ctx f tf => tf = tunnel_fundef f) eq p tp.
-
-Lemma transf_program_match:
- forall p, match_prog p (tunnel_program p).
-Proof.
- intros. eapply match_transform_program; eauto.
-Qed.
-
-(** * Properties of the branch map computed using union-find. *)
-
-Section BRANCH_MAP_CORRECT.
-
-Variable fn: LTL.function.
-
-Definition measure_branch (u: U.t) (pc s: node) (f: node -> nat) : node -> nat :=
- fun x => if peq (U.repr u s) pc then f x
- else if peq (U.repr u x) pc then (f x + f s + 1)%nat
- else f x.
-
-Definition measure_cond (u: U.t) (pc s1 s2: node) (f: node -> nat) : node -> nat :=
- fun x => if peq (U.repr u s1) pc then f x
- else if peq (U.repr u x) pc then (f x + Nat.max (f s1) (f s2) + 1)%nat
- else f x.
-
-Definition branch_map_correct_1 (c: code) (u: U.t) (f: node -> nat): Prop :=
- forall pc,
- match c!pc with
- | Some(Lbranch s :: b) =>
- U.repr u pc = pc \/ (U.repr u pc = U.repr u s /\ f s < f pc)%nat
- | _ =>
- U.repr u pc = pc
- end.
-
-Lemma record_branch_correct:
- forall c u f pc b,
- branch_map_correct_1 (PTree.remove pc c) u f ->
- c!pc = Some b ->
- { f' | branch_map_correct_1 c (record_branch u pc b) f' }.
-Proof.
- intros c u f pc b BMC GET1.
- assert (PC: U.repr u pc = pc).
- { specialize (BMC pc). rewrite PTree.grs in BMC. auto. }
- assert (DFL: { f | branch_map_correct_1 c u f }).
- { exists f. intros p. destruct (peq p pc).
- - subst p. rewrite GET1. destruct b as [ | [] b ]; auto.
- - specialize (BMC p). rewrite PTree.gro in BMC by auto. exact BMC.
- }
- unfold record_branch. destruct b as [ | [] b ]; auto.
- exists (measure_branch u pc s f). intros p. destruct (peq p pc).
-+ subst p. rewrite GET1. unfold measure_branch.
- rewrite (U.repr_union_2 u pc s); auto. rewrite U.repr_union_3.
- destruct (peq (U.repr u s) pc); auto. rewrite PC, peq_true. right; split; auto. lia.
-+ specialize (BMC p). rewrite PTree.gro in BMC by auto.
- assert (U.repr u p = p -> U.repr (U.union u pc s) p = p).
- { intro. rewrite <- H at 2. apply U.repr_union_1. congruence. }
- destruct (c!p) as [ [ | [] _ ] | ]; auto.
- destruct BMC as [A | [A B]]. auto.
- right; split. apply U.sameclass_union_2; auto.
- unfold measure_branch. destruct (peq (U.repr u s) pc). auto.
- rewrite A. destruct (peq (U.repr u s0) pc); lia.
-Qed.
-
-Lemma record_branches_correct:
- { f | branch_map_correct_1 fn.(fn_code) (record_branches fn) f }.
-Proof.
- unfold record_branches. apply PTree_Properties.fold_ind.
-- (* base case *)
- intros m EMPTY. exists (fun _ => O).
- red; intros. rewrite EMPTY. apply U.repr_empty.
-- (* inductive case *)
- intros m u pc bb GET1 GET2 [f BMC]. eapply record_branch_correct; eauto.
-Qed.
-
-Definition branch_map_correct_2 (c: code) (u: U.t) (f: node -> nat): Prop :=
- forall pc,
- match fn.(fn_code)!pc with
- | Some(Lbranch s :: b) =>
- U.repr u pc = pc \/ (U.repr u pc = U.repr u s /\ f s < f pc)%nat
- | Some(Lcond cond args s1 s2 :: b) =>
- U.repr u pc = pc \/ (c!pc = None /\ U.repr u pc = U.repr u s1 /\ U.repr u pc = U.repr u s2 /\ f s1 < f pc /\ f s2 < f pc)%nat
- | _ =>
- U.repr u pc = pc
- end.
-
-Lemma record_cond_correct:
- forall c u changed f pc b,
- branch_map_correct_2 c u f ->
- fn.(fn_code)!pc = Some b ->
- c!pc <> None ->
- let '(c1, u1, _) := record_cond (c, u, changed) pc b in
- { f' | branch_map_correct_2 c1 u1 f' }.
-Proof.
- intros c u changed f pc b BMC GET1 GET2.
- assert (DFL: { f' | branch_map_correct_2 c u f' }).
- { exists f; auto. }
- unfold record_cond. destruct b as [ | [] b ]; auto.
- destruct (peq (U.repr u s1) (U.repr u s2)); auto.
- exists (measure_cond u pc s1 s2 f).
- assert (PC: U.repr u pc = pc).
- { specialize (BMC pc). rewrite GET1 in BMC. intuition congruence. }
- intro p. destruct (peq p pc).
-- subst p. rewrite GET1. unfold measure_cond.
- rewrite U.repr_union_2 by auto. rewrite <- e, PC, peq_true.
- destruct (peq (U.repr u s1) pc); auto.
- right; repeat split.
- + apply PTree.grs.
- + rewrite U.repr_union_3. auto.
- + rewrite U.repr_union_1 by congruence. auto.
- + lia.
- + lia.
-- assert (P: U.repr u p = p -> U.repr (U.union u pc s1) p = p).
- { intros. rewrite U.repr_union_1 by congruence. auto. }
- specialize (BMC p). destruct (fn_code fn)!p as [ [ | [] bb ] | ]; auto.
- + destruct BMC as [A | (A & B)]; auto. right; split.
- * apply U.sameclass_union_2; auto.
- * unfold measure_cond. rewrite <- A.
- destruct (peq (U.repr u s1) pc). auto.
- destruct (peq (U.repr u p) pc); lia.
- + destruct BMC as [A | (A & B & C & D & E)]; auto. right; split; [ | split; [ | split]].
- * rewrite PTree.gro by auto. auto.
- * apply U.sameclass_union_2; auto.
- * apply U.sameclass_union_2; auto.
- * unfold measure_cond. rewrite <- B, <- C.
- destruct (peq (U.repr u s1) pc). auto.
- destruct (peq (U.repr u p) pc); lia.
-Qed.
-
-Definition code_compat (c: code) : Prop :=
- forall pc b, c!pc = Some b -> fn.(fn_code)!pc = Some b.
-
-Definition code_invariant (c0 c1 c2: code) : Prop :=
- forall pc, c0!pc = None -> c1!pc = c2!pc.
-
-Lemma record_conds_1_correct:
- forall c u f,
- branch_map_correct_2 c u f ->
- code_compat c ->
- let '(c', u', _) := record_conds_1 (c, u) in
- (code_compat c' * { f' | branch_map_correct_2 c' u' f' })%type.
-Proof.
- intros c0 u0 f0 BMC0 COMPAT0.
- unfold record_conds_1.
- set (x := PTree.fold record_cond c0 (c0, u0, false)).
- set (P := fun (cd: code) (cuc: code * U.t * bool) =>
- (code_compat (fst (fst cuc)) *
- code_invariant cd (fst (fst cuc)) c0 *
- { f | branch_map_correct_2 (fst (fst cuc)) (snd (fst cuc)) f })%type).
- assert (REC: P c0 x).
- { unfold x; apply PTree_Properties.fold_ind.
- - intros cd EMPTY. split; [split|]; simpl.
- + auto.
- + red; auto.
- + exists f0; auto.
- - intros cd [[c u] changed] pc b GET1 GET2 [[COMPAT INV] [f BMC]]. simpl in *.
- split; [split|].
- + unfold record_cond; destruct b as [ | [] b]; simpl; auto.
- destruct (peq (U.repr u s1) (U.repr u s2)); simpl; auto.
- red; intros. rewrite PTree.grspec in H. destruct (PTree.elt_eq pc0 pc). discriminate. auto.
- + assert (DFL: code_invariant cd c c0).
- { intros p GET. apply INV. rewrite PTree.gro by congruence. auto. }
- unfold record_cond; destruct b as [ | [] b]; simpl; auto.
- destruct (peq (U.repr u s1) (U.repr u s2)); simpl; auto.
- intros p GET. rewrite PTree.gro by congruence. apply INV. rewrite PTree.gro by congruence. auto.
- + assert (GET3: c!pc = Some b).
- { rewrite <- GET2. apply INV. apply PTree.grs. }
- assert (X: fn.(fn_code)!pc = Some b) by auto.
- assert (Y: c!pc <> None) by congruence.
- generalize (record_cond_correct c u changed f pc b BMC X Y).
- destruct (record_cond (c, u, changed) pc b) as [[c1 u1] changed1]; simpl.
- auto.
- }
- destruct x as [[c1 u1] changed1]; destruct REC as [[COMPAT1 INV1] BMC1]; auto.
-Qed.
-
-Definition branch_map_correct (u: U.t) (f: node -> nat): Prop :=
- forall pc,
- match fn.(fn_code)!pc with
- | Some(Lbranch s :: b) =>
- U.repr u pc = pc \/ (U.repr u pc = U.repr u s /\ f s < f pc)%nat
- | Some(Lcond cond args s1 s2 :: b) =>
- U.repr u pc = pc \/ (U.repr u pc = U.repr u s1 /\ U.repr u pc = U.repr u s2 /\ f s1 < f pc /\ f s2 < f pc)%nat
- | _ =>
- U.repr u pc = pc
- end.
-
-Lemma record_conds_correct:
- forall cu,
- { f | branch_map_correct_2 (fst cu) (snd cu) f } ->
- code_compat (fst cu) ->
- { f | branch_map_correct (record_conds cu) f }.
-Proof.
- intros cu0. functional induction (record_conds cu0); intros.
-- destruct cu as [c u], cu' as [c' u'], H as [f BMC].
- generalize (record_conds_1_correct c u f BMC H0).
- rewrite e. intros [U V]. apply IHt; auto.
-- destruct cu as [c u], H as [f BMC].
- exists f. intros pc. specialize (BMC pc); simpl in *.
- destruct (fn_code fn)!pc as [ [ | [] b ] | ]; tauto.
-Qed.
-
-Lemma record_gotos_correct_1:
- { f | branch_map_correct (record_gotos fn) f }.
-Proof.
- apply record_conds_correct; simpl.
-- destruct record_branches_correct as [f BMC].
- exists f. intros pc. specialize (BMC pc); simpl in *.
- destruct (fn_code fn)!pc as [ [ | [] b ] | ]; auto.
-- red; auto.
-Qed.
-
-Definition branch_target (pc: node) : node :=
- U.repr (record_gotos fn) pc.
-
-Definition count_gotos (pc: node) : nat :=
- proj1_sig record_gotos_correct_1 pc.
-
-Theorem record_gotos_correct:
- forall pc,
- match fn.(fn_code)!pc with
- | Some(Lbranch s :: b) =>
- branch_target pc = pc \/
- (branch_target pc = branch_target s /\ count_gotos s < count_gotos pc)%nat
- | Some(Lcond cond args s1 s2 :: b) =>
- branch_target pc = pc \/
- (branch_target pc = branch_target s1 /\ branch_target pc = branch_target s2
- /\ count_gotos s1 < count_gotos pc /\ count_gotos s2 < count_gotos pc)%nat
- | _ =>
- branch_target pc = pc
- end.
-Proof.
- intros. unfold count_gotos. destruct record_gotos_correct_1 as [f P]; simpl.
- apply P.
-Qed.
-
-End BRANCH_MAP_CORRECT.
-
-(** * Preservation of semantics *)
-
-Section PRESERVATION.
-
-Variables prog tprog: program.
-Hypothesis TRANSL: match_prog prog tprog.
-Let ge := Genv.globalenv prog.
-Let tge := Genv.globalenv tprog.
-
-Lemma functions_translated:
- forall v f,
- Genv.find_funct ge v = Some f ->
- Genv.find_funct tge v = Some (tunnel_fundef f).
-Proof (Genv.find_funct_transf TRANSL).
-
-Lemma function_ptr_translated:
- forall v f,
- Genv.find_funct_ptr ge v = Some f ->
- Genv.find_funct_ptr tge v = Some (tunnel_fundef f).
-Proof (Genv.find_funct_ptr_transf TRANSL).
-
-Lemma symbols_preserved:
- forall id,
- Genv.find_symbol tge id = Genv.find_symbol ge id.
-Proof (Genv.find_symbol_transf TRANSL).
-
-Lemma senv_preserved:
- Senv.equiv ge tge.
-Proof (Genv.senv_transf TRANSL).
-
-Lemma sig_preserved:
- forall f, funsig (tunnel_fundef f) = funsig f.
-Proof.
- destruct f; reflexivity.
-Qed.
-
-(** The proof of semantic preservation is a simulation argument
- based on diagrams of the following form:
-<<
- st1 --------------- st2
- | |
- t| ?|t
- | |
- v v
- st1'--------------- st2'
->>
- The [match_states] predicate, defined below, captures the precondition
- between states [st1] and [st2], as well as the postcondition between
- [st1'] and [st2']. One transition in the source code (left) can correspond
- to zero or one transition in the transformed code (right). The
- "zero transition" case occurs when executing a [Lgoto] instruction
- in the source code that has been removed by tunneling.
-
- In the definition of [match_states], what changes between the original and
- transformed codes is mainly the control-flow
- (in particular, the current program point [pc]), but also some values
- and memory states, since some [Vundef] values can become more defined
- as a consequence of eliminating useless [Lcond] instructions. *)
-
-Definition tunneled_block (f: function) (b: bblock) :=
- tunnel_block (record_gotos f) b.
-
-Definition tunneled_code (f: function) :=
- PTree.map1 (tunneled_block f) (fn_code f).
-
-Definition locmap_lessdef (ls1 ls2: locset) : Prop :=
- forall l, Val.lessdef (ls1 l) (ls2 l).
-
-Inductive match_stackframes: stackframe -> stackframe -> Prop :=
- | match_stackframes_intro:
- forall f sp ls0 bb tls0,
- locmap_lessdef ls0 tls0 ->
- match_stackframes
- (Stackframe f sp ls0 bb)
- (Stackframe (tunnel_function f) sp tls0 (tunneled_block f bb)).
-
-Inductive match_states: state -> state -> Prop :=
- | match_states_intro:
- forall s f sp pc ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm),
- match_states (State s f sp pc ls m)
- (State ts (tunnel_function f) sp (branch_target f pc) tls tm)
- | match_states_block:
- forall s f sp bb ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm),
- match_states (Block s f sp bb ls m)
- (Block ts (tunnel_function f) sp (tunneled_block f bb) tls tm)
- | match_states_interm_branch:
- forall s f sp pc bb ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm),
- match_states (Block s f sp (Lbranch pc :: bb) ls m)
- (State ts (tunnel_function f) sp (branch_target f pc) tls tm)
- | match_states_interm_cond:
- forall s f sp cond args pc1 pc2 bb ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm)
- (SAME: branch_target f pc1 = branch_target f pc2),
- match_states (Block s f sp (Lcond cond args pc1 pc2 :: bb) ls m)
- (State ts (tunnel_function f) sp (branch_target f pc1) tls tm)
- | match_states_call:
- forall s f ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm),
- match_states (Callstate s f ls m)
- (Callstate ts (tunnel_fundef f) tls tm)
- | match_states_return:
- forall s ls m ts tls tm
- (STK: list_forall2 match_stackframes s ts)
- (LS: locmap_lessdef ls tls)
- (MEM: Mem.extends m tm),
- match_states (Returnstate s ls m)
- (Returnstate ts tls tm).
-
-(** Properties of [locmap_lessdef] *)
-
-Lemma reglist_lessdef:
- forall rl ls1 ls2,
- locmap_lessdef ls1 ls2 -> Val.lessdef_list (reglist ls1 rl) (reglist ls2 rl).
-Proof.
- induction rl; simpl; intros; auto.
-Qed.
-
-Lemma locmap_set_lessdef:
- forall ls1 ls2 v1 v2 l,
- locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.set l v1 ls1) (Locmap.set l v2 ls2).
-Proof.
- intros; red; intros l'. unfold Locmap.set. destruct (Loc.eq l l').
-- destruct l; auto using Val.load_result_lessdef.
-- destruct (Loc.diff_dec l l'); auto.
-Qed.
-
-Lemma locmap_set_undef_lessdef:
- forall ls1 ls2 l,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (Locmap.set l Vundef ls1) ls2.
-Proof.
- intros; red; intros l'. unfold Locmap.set. destruct (Loc.eq l l').
-- destruct l; auto. destruct ty; auto.
-- destruct (Loc.diff_dec l l'); auto.
-Qed.
-
-Lemma locmap_undef_regs_lessdef:
- forall rl ls1 ls2,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_regs rl ls1) (undef_regs rl ls2).
-Proof.
- induction rl as [ | r rl]; intros; simpl. auto. apply locmap_set_lessdef; auto.
-Qed.
-
-Lemma locmap_undef_regs_lessdef_1:
- forall rl ls1 ls2,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_regs rl ls1) ls2.
-Proof.
- induction rl as [ | r rl]; intros; simpl. auto. apply locmap_set_undef_lessdef; auto.
-Qed.
-
-(*
-Lemma locmap_undef_lessdef:
- forall ll ls1 ls2,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (Locmap.undef ll ls1) (Locmap.undef ll ls2).
-Proof.
- induction ll as [ | l ll]; intros; simpl. auto. apply IHll. apply locmap_set_lessdef; auto.
-Qed.
-
-Lemma locmap_undef_lessdef_1:
- forall ll ls1 ls2,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (Locmap.undef ll ls1) ls2.
-Proof.
- induction ll as [ | l ll]; intros; simpl. auto. apply IHll. apply locmap_set_undef_lessdef; auto.
-Qed.
-*)
-
-Lemma locmap_getpair_lessdef:
- forall p ls1 ls2,
- locmap_lessdef ls1 ls2 -> Val.lessdef (Locmap.getpair p ls1) (Locmap.getpair p ls2).
-Proof.
- intros; destruct p; simpl; auto using Val.longofwords_lessdef.
-Qed.
-
-Lemma locmap_getpairs_lessdef:
- forall pl ls1 ls2,
- locmap_lessdef ls1 ls2 ->
- Val.lessdef_list (map (fun p => Locmap.getpair p ls1) pl) (map (fun p => Locmap.getpair p ls2) pl).
-Proof.
- intros. induction pl; simpl; auto using locmap_getpair_lessdef.
-Qed.
-
-Lemma locmap_setpair_lessdef:
- forall p ls1 ls2 v1 v2,
- locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.setpair p v1 ls1) (Locmap.setpair p v2 ls2).
-Proof.
- intros; destruct p; simpl; auto using locmap_set_lessdef, Val.loword_lessdef, Val.hiword_lessdef.
-Qed.
-
-Lemma locmap_setres_lessdef:
- forall res ls1 ls2 v1 v2,
- locmap_lessdef ls1 ls2 -> Val.lessdef v1 v2 -> locmap_lessdef (Locmap.setres res v1 ls1) (Locmap.setres res v2 ls2).
-Proof.
- induction res; intros; simpl; auto using locmap_set_lessdef, Val.loword_lessdef, Val.hiword_lessdef.
-Qed.
-
-Lemma locmap_undef_caller_save_regs_lessdef:
- forall ls1 ls2,
- locmap_lessdef ls1 ls2 -> locmap_lessdef (undef_caller_save_regs ls1) (undef_caller_save_regs ls2).
-Proof.
- intros; red; intros. unfold undef_caller_save_regs.
- destruct l.
-- destruct (Conventions1.is_callee_save r); auto.
-- destruct sl; auto.
-Qed.
-
-Lemma find_function_translated:
- forall ros ls tls fd,
- locmap_lessdef ls tls ->
- find_function ge ros ls = Some fd ->
- find_function tge ros tls = Some (tunnel_fundef fd).
-Proof.
- intros. destruct ros; simpl in *.
-- assert (E: tls (R m) = ls (R m)).
- { exploit Genv.find_funct_inv; eauto. intros (b & EQ).
- generalize (H (R m)). rewrite EQ. intros LD; inv LD. auto. }
- rewrite E. apply functions_translated; auto.
-- rewrite symbols_preserved. destruct (Genv.find_symbol ge i); inv H0.
- apply function_ptr_translated; auto.
-Qed.
-
-Lemma call_regs_lessdef:
- forall ls1 ls2, locmap_lessdef ls1 ls2 -> locmap_lessdef (call_regs ls1) (call_regs ls2).
-Proof.
- intros; red; intros. destruct l as [r | [] ofs ty]; simpl; auto.
-Qed.
-
-Lemma return_regs_lessdef:
- forall caller1 callee1 caller2 callee2,
- locmap_lessdef caller1 caller2 ->
- locmap_lessdef callee1 callee2 ->
- locmap_lessdef (return_regs caller1 callee1) (return_regs caller2 callee2).
-Proof.
- intros; red; intros. destruct l; simpl.
-- destruct (Conventions1.is_callee_save r); auto.
-- destruct sl; auto.
-Qed.
-
-(** To preserve non-terminating behaviours, we show that the transformed
- code cannot take an infinity of "zero transition" cases.
- We use the following [measure] function over source states,
- which decreases strictly in the "zero transition" case. *)
-
-Definition measure (st: state) : nat :=
- match st with
- | State s f sp pc ls m => (count_gotos f pc * 2)%nat
- | Block s f sp (Lbranch pc :: _) ls m => (count_gotos f pc * 2 + 1)%nat
- | Block s f sp (Lcond _ _ pc1 pc2 :: _) ls m => (Nat.max (count_gotos f pc1) (count_gotos f pc2) * 2 + 1)%nat
- | Block s f sp bb ls m => 0%nat
- | Callstate s f ls m => 0%nat
- | Returnstate s ls m => 0%nat
- end.
-
-Lemma match_parent_locset:
- forall s ts,
- list_forall2 match_stackframes s ts ->
- locmap_lessdef (parent_locset s) (parent_locset ts).
-Proof.
- induction 1; simpl.
-- red; auto.
-- inv H; auto.
-Qed.
-
-Lemma tunnel_step_correct:
- forall st1 t st2, step ge st1 t st2 ->
- forall st1' (MS: match_states st1 st1'),
- (exists st2', step tge st1' t st2' /\ match_states st2 st2')
- \/ (measure st2 < measure st1 /\ t = E0 /\ match_states st2 st1')%nat.
-Proof.
- induction 1; intros; try inv MS.
-
-- (* entering a block *)
- assert (DEFAULT: branch_target f pc = pc ->
- (exists st2' : state,
- step tge (State ts (tunnel_function f) sp (branch_target f pc) tls tm) E0 st2'
- /\ match_states (Block s f sp bb rs m) st2')).
- { intros. rewrite H0. econstructor; split.
- econstructor. simpl. rewrite PTree.gmap1. rewrite H. simpl. eauto.
- econstructor; eauto. }
-
- generalize (record_gotos_correct f pc). rewrite H.
- destruct bb; auto. destruct i; auto.
-+ (* Lbranch *)
- intros [A | [B C]]. auto.
- right. split. simpl. lia.
- split. auto.
- rewrite B. econstructor; eauto.
-+ (* Lcond *)
- intros [A | (B & C & D & E)]. auto.
- right. split. simpl. lia.
- split. auto.
- rewrite B. econstructor; eauto. congruence.
-
-- (* Lop *)
- exploit eval_operation_lessdef. apply reglist_lessdef; eauto. eauto. eauto.
- intros (tv & EV & LD).
- left; simpl; econstructor; split.
- eapply exec_Lop with (v := tv); eauto.
- rewrite <- EV. apply eval_operation_preserved. exact symbols_preserved.
- econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
-- (* Lload *)
- exploit eval_addressing_lessdef. apply reglist_lessdef; eauto. eauto.
- intros (ta & EV & LD).
- exploit Mem.loadv_extends. eauto. eauto. eexact LD.
- intros (tv & LOAD & LD').
- left; simpl; econstructor; split.
- eapply exec_Lload with (a := ta).
- rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
- eauto. eauto.
- econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
-- (* Lgetstack *)
- left; simpl; econstructor; split.
- econstructor; eauto.
- econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
-- (* Lsetstack *)
- left; simpl; econstructor; split.
- econstructor; eauto.
- econstructor; eauto using locmap_set_lessdef, locmap_undef_regs_lessdef.
-- (* Lstore *)
- exploit eval_addressing_lessdef. apply reglist_lessdef; eauto. eauto.
- intros (ta & EV & LD).
- exploit Mem.storev_extends. eauto. eauto. eexact LD. apply LS.
- intros (tm' & STORE & MEM').
- left; simpl; econstructor; split.
- eapply exec_Lstore with (a := ta).
- rewrite <- EV. apply eval_addressing_preserved. exact symbols_preserved.
- eauto. eauto.
- econstructor; eauto using locmap_undef_regs_lessdef.
-- (* Lcall *)
- left; simpl; econstructor; split.
- eapply exec_Lcall with (fd := tunnel_fundef fd); eauto.
- eapply find_function_translated; eauto.
- rewrite sig_preserved. auto.
- econstructor; eauto.
- constructor; auto.
- constructor; auto.
-- (* Ltailcall *)
- exploit Mem.free_parallel_extends. eauto. eauto. intros (tm' & FREE & MEM').
- left; simpl; econstructor; split.
- eapply exec_Ltailcall with (fd := tunnel_fundef fd); eauto.
- eapply find_function_translated; eauto using return_regs_lessdef, match_parent_locset.
- apply sig_preserved.
- econstructor; eauto using return_regs_lessdef, match_parent_locset.
-- (* Lbuiltin *)
- exploit eval_builtin_args_lessdef. eexact LS. eauto. eauto. intros (tvargs & EVA & LDA).
- exploit external_call_mem_extends; eauto. intros (tvres & tm' & A & B & C & D).
- left; simpl; econstructor; split.
- eapply exec_Lbuiltin; eauto.
- eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
- eapply external_call_symbols_preserved. apply senv_preserved. eauto.
- econstructor; eauto using locmap_setres_lessdef, locmap_undef_regs_lessdef.
-- (* Lbranch (preserved) *)
- left; simpl; econstructor; split.
- eapply exec_Lbranch; eauto.
- fold (branch_target f pc). econstructor; eauto.
-- (* Lbranch (eliminated) *)
- right; split. simpl. lia. split. auto. constructor; auto.
-
-- (* Lcond (preserved) *)
- simpl tunneled_block.
- set (s1 := U.repr (record_gotos f) pc1). set (s2 := U.repr (record_gotos f) pc2).
- destruct (peq s1 s2).
-+ left; econstructor; split.
- eapply exec_Lbranch.
- set (pc := if b then pc1 else pc2).
- replace s1 with (branch_target f pc) by (unfold pc; destruct b; auto).
- constructor; eauto using locmap_undef_regs_lessdef_1.
-+ left; econstructor; split.
- eapply exec_Lcond; eauto. eapply eval_condition_lessdef; eauto using reglist_lessdef.
- destruct b; econstructor; eauto using locmap_undef_regs_lessdef.
-- (* Lcond (eliminated) *)
- right; split. simpl. destruct b; lia.
- split. auto.
- set (pc := if b then pc1 else pc2).
- replace (branch_target f pc1) with (branch_target f pc) by (unfold pc; destruct b; auto).
- econstructor; eauto.
-
-- (* Ljumptable *)
- assert (tls (R arg) = Vint n).
- { generalize (LS (R arg)); rewrite H; intros LD; inv LD; auto. }
- left; simpl; econstructor; split.
- eapply exec_Ljumptable.
- eauto. rewrite list_nth_z_map. change U.elt with node. rewrite H0. reflexivity. eauto.
- econstructor; eauto using locmap_undef_regs_lessdef.
-- (* Lreturn *)
- exploit Mem.free_parallel_extends. eauto. eauto. intros (tm' & FREE & MEM').
- left; simpl; econstructor; split.
- eapply exec_Lreturn; eauto.
- constructor; eauto using return_regs_lessdef, match_parent_locset.
-- (* internal function *)
- exploit Mem.alloc_extends. eauto. eauto. apply Z.le_refl. apply Z.le_refl.
- intros (tm' & ALLOC & MEM').
- left; simpl; econstructor; split.
- eapply exec_function_internal; eauto.
- simpl. econstructor; eauto using locmap_undef_regs_lessdef, call_regs_lessdef.
-- (* external function *)
- exploit external_call_mem_extends; eauto using locmap_getpairs_lessdef.
- intros (tvres & tm' & A & B & C & D).
- left; simpl; econstructor; split.
- eapply exec_function_external; eauto.
- eapply external_call_symbols_preserved; eauto. apply senv_preserved.
- simpl. econstructor; eauto using locmap_setpair_lessdef, locmap_undef_caller_save_regs_lessdef.
-- (* return *)
- inv STK. inv H1.
- left; econstructor; split.
- eapply exec_return; eauto.
- constructor; auto.
-Qed.
-
-Lemma transf_initial_states:
- forall st1, initial_state prog st1 ->
- exists st2, initial_state tprog st2 /\ match_states st1 st2.
-Proof.
- intros. inversion H.
- exists (Callstate nil (tunnel_fundef f) (Locmap.init Vundef) m0); split.
- econstructor; eauto.
- apply (Genv.init_mem_transf TRANSL); auto.
- rewrite (match_program_main TRANSL).
- rewrite symbols_preserved. eauto.
- apply function_ptr_translated; auto.
- rewrite <- H3. apply sig_preserved.
- constructor. constructor. red; simpl; auto. apply Mem.extends_refl.
-Qed.
-
-Lemma transf_final_states:
- forall st1 st2 r,
- match_states st1 st2 -> final_state st1 r -> final_state st2 r.
-Proof.
- intros. inv H0. inv H. inv STK.
- set (p := map_rpair R (Conventions1.loc_result signature_main)) in *.
- generalize (locmap_getpair_lessdef p _ _ LS). rewrite H1; intros LD; inv LD.
- econstructor; eauto.
-Qed.
-
-Theorem transf_program_correct:
- forward_simulation (LTL.semantics prog) (LTL.semantics tprog).
-Proof.
- eapply forward_simulation_opt.
- apply senv_preserved.
- eexact transf_initial_states.
- eexact transf_final_states.
- eexact tunnel_step_correct.
-Qed.
-
-End PRESERVATION.
diff --git a/backend/Unusedglob.v b/backend/Unusedglob.v
index 8ac7c4ce..3b8e19ad 100644
--- a/backend/Unusedglob.v
+++ b/backend/Unusedglob.v
@@ -46,14 +46,14 @@ Definition ref_instruction (i: instruction) : list ident :=
match i with
| Inop _ => nil
| Iop op _ _ _ => globals_operation op
- | Iload _ addr _ _ _ => globals_addressing addr
+ | Iload _ _ addr _ _ _ => globals_addressing addr
| Istore _ addr _ _ _ => globals_addressing addr
| Icall _ (inl r) _ _ _ => nil
| Icall _ (inr id) _ _ _ => id :: nil
| Itailcall _ (inl r) _ => nil
| Itailcall _ (inr id) _ => id :: nil
| Ibuiltin _ args _ _ => globals_of_builtin_args args
- | Icond cond _ _ _ => nil
+ | Icond cond _ _ _ _ => nil
| Ijumptable _ _ => nil
| Ireturn _ => nil
end.
@@ -126,7 +126,7 @@ Fixpoint filter_globdefs (used: IS.t) (accu defs: list (ident * globdef fundef u
Definition global_defined (p: program) (pm: prog_map) (id: ident) : bool :=
match pm!id with Some _ => true | None => ident_eq id (prog_main p) end.
-Definition transform_program (p: program) : res program :=
+Definition transf_program (p: program) : res program :=
let pm := prog_defmap p in
match used_globals p pm with
| None => Error (msg "Unusedglob: analysis failed")
diff --git a/backend/Unusedglobproof.v b/backend/Unusedglobproof.v
index 3216ec50..aaacf9d1 100644
--- a/backend/Unusedglobproof.v
+++ b/backend/Unusedglobproof.v
@@ -428,9 +428,9 @@ Qed.
End TRANSFORMATION.
Theorem transf_program_match:
- forall p tp, transform_program p = OK tp -> match_prog p tp.
+ forall p tp, transf_program p = OK tp -> match_prog p tp.
Proof.
- unfold transform_program; intros p tp TR. set (pm := prog_defmap p) in *.
+ unfold transf_program; intros p tp TR. set (pm := prog_defmap p) in *.
destruct (used_globals p pm) as [u|] eqn:U; try discriminate.
destruct (IS.for_all (global_defined p pm) u) eqn:DEF; inv TR.
exists u; split.
@@ -915,7 +915,7 @@ Proof.
/\ Val.inject j a ta).
{ apply eval_addressing_inj with (ge1 := ge) (sp1 := Vptr sp0 Ptrofs.zero) (vl1 := rs##args).
intros. apply symbol_address_inject. eapply match_stacks_preserves_globals; eauto.
- apply KEPT. red. exists pc, (Iload chunk addr args dst pc'); auto.
+ apply KEPT. red. exists pc, (Iload trap chunk addr args dst pc'); auto.
econstructor; eauto.
apply regs_inject; auto.
assumption. }
@@ -924,6 +924,36 @@ Proof.
econstructor; split. eapply exec_Iload; eauto.
econstructor; eauto. apply set_reg_inject; auto.
+- (* load notrap1 *)
+ assert (eval_addressing tge (Vptr tsp Ptrofs.zero) addr trs##args = None).
+ { eapply eval_addressing_inj_none.
+ intros. apply symbol_address_inject. eapply match_stacks_preserves_globals; eauto.
+ apply KEPT. red. exists pc, (Iload NOTRAP chunk addr args dst pc'); auto.
+ econstructor; eauto.
+ rewrite Ptrofs.add_zero; reflexivity.
+ apply regs_inject; auto.
+ eassumption.
+ assumption. }
+
+ econstructor; split. eapply exec_Iload_notrap1; eauto.
+ econstructor; eauto. apply set_reg_inject; auto.
+
+- (* load notrap2 *)
+ assert (A: exists ta,
+ eval_addressing tge (Vptr tsp Ptrofs.zero) addr trs##args = Some ta
+ /\ Val.inject j a ta).
+ { apply eval_addressing_inj with (ge1 := ge) (sp1 := Vptr sp0 Ptrofs.zero) (vl1 := rs##args).
+ intros. apply symbol_address_inject. eapply match_stacks_preserves_globals; eauto.
+ apply KEPT. red. exists pc, (Iload NOTRAP chunk addr args dst pc'); auto.
+ econstructor; eauto.
+ apply regs_inject; auto.
+ assumption. }
+ destruct A as (ta & B & C).
+ destruct (Mem.loadv chunk tm ta) eqn:Echunk2.
+ + econstructor; split. eapply exec_Iload; eauto.
+ econstructor; eauto. apply set_reg_inject; auto.
+ + econstructor; split. eapply exec_Iload_notrap2; eauto.
+ econstructor; eauto. apply set_reg_inject; auto.
- (* store *)
assert (A: exists ta,
eval_addressing tge (Vptr tsp Ptrofs.zero) addr trs##args = Some ta
diff --git a/backend/ValueAnalysis.v b/backend/ValueAnalysis.v
index ebf2c5ea..e20edff7 100644
--- a/backend/ValueAnalysis.v
+++ b/backend/ValueAnalysis.v
@@ -139,9 +139,14 @@ Definition transfer (f: function) (rm: romem) (pc: node) (ae: aenv) (am: amem) :
| Some(Iop op args res s) =>
let a := eval_static_operation op (aregs ae args) in
VA.State (AE.set res a ae) am
- | Some(Iload chunk addr args dst s) =>
+ | Some(Iload TRAP chunk addr args dst s) =>
let a := loadv chunk rm am (eval_static_addressing addr (aregs ae args)) in
VA.State (AE.set dst a ae) am
+
+ (* TODO: maybe a case analysis on the results of loadv? *)
+
+ | Some(Iload NOTRAP chunk addr args dst s) =>
+ VA.State (AE.set dst Vtop ae) am
| Some(Istore chunk addr args src s) =>
let am' := storev chunk am (eval_static_addressing addr (aregs ae args)) (areg ae src) in
VA.State ae am'
@@ -151,7 +156,7 @@ Definition transfer (f: function) (rm: romem) (pc: node) (ae: aenv) (am: amem) :
VA.Bot
| Some(Ibuiltin ef args res s) =>
transfer_builtin ae am rm ef args res
- | Some(Icond cond args s1 s2) =>
+ | Some(Icond cond args s1 s2 _) =>
VA.State ae am
| Some(Ijumptable arg tbl) =>
VA.State ae am
@@ -1267,11 +1272,27 @@ Proof.
apply ematch_update; auto. eapply eval_static_operation_sound; eauto with va.
- (* load *)
+ destruct trap.
+ + eapply sound_succ_state; eauto. simpl; auto.
+ unfold transfer; rewrite H. eauto.
+ apply ematch_update; auto. eapply loadv_sound; eauto with va.
+ eapply eval_static_addressing_sound; eauto with va.
+ + eapply sound_succ_state; eauto. simpl; auto.
+ unfold transfer; rewrite H. eauto.
+ apply ematch_update; auto.
+ eapply vmatch_top.
+ eapply loadv_sound; try eassumption.
+ eapply eval_static_addressing_sound; eauto with va.
+- (* load notrap1 *)
eapply sound_succ_state; eauto. simpl; auto.
unfold transfer; rewrite H. eauto.
- apply ematch_update; auto. eapply loadv_sound; eauto with va.
- eapply eval_static_addressing_sound; eauto with va.
-
+ apply ematch_update; auto.
+ constructor.
+- (* load notrap2 *)
+ eapply sound_succ_state; eauto. simpl; auto.
+ unfold transfer; rewrite H. eauto.
+ apply ematch_update; auto.
+ constructor.
- (* store *)
exploit eval_static_addressing_sound; eauto with va. intros VMADDR.
eapply sound_succ_state; eauto. simpl; auto.
diff --git a/backend/ValueDomain.v b/backend/ValueDomain.v
index 01f080ff..5a7cfc12 100644
--- a/backend/ValueDomain.v
+++ b/backend/ValueDomain.v
@@ -15,6 +15,7 @@ Require Import Zwf Coqlib Maps Zbits Integers Floats Lattice.
Require Import Compopts AST.
Require Import Values Memory Globalenvs Builtins Events.
Require Import Registers RTL.
+Require Import Lia.
(** The abstract domains for value analysis *)
@@ -2069,7 +2070,6 @@ Definition divfs := binop_single Float32.div.
Lemma divfs_sound:
forall v x w y, vmatch v x -> vmatch w y -> vmatch (Val.divfs v w) (divfs x y).
Proof (binop_single_sound Float32.div).
-
(** Conversions *)
Definition zero_ext (nbits: Z) (v: aval) :=
@@ -2483,6 +2483,468 @@ Proof.
destruct 1; simpl; auto with va.
Qed.
+
+(* Extensions for KVX and Risc-V *)
+
+Definition intoffloat_total (x: aval) :=
+ match x with
+ | F f =>
+ match Float.to_int f with
+ | Some i => I i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition intuoffloat_total (x: aval) :=
+ match x with
+ | F f =>
+ match Float.to_intu f with
+ | Some i => I i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition intofsingle_total (x: aval) :=
+ match x with
+ | FS f =>
+ match Float32.to_int f with
+ | Some i => I i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition intuofsingle_total (x: aval) :=
+ match x with
+ | FS f =>
+ match Float32.to_intu f with
+ | Some i => I i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition longoffloat_total (x: aval) :=
+ match x with
+ | F f =>
+ match Float.to_long f with
+ | Some i => L i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition longuoffloat_total (x: aval) :=
+ match x with
+ | F f =>
+ match Float.to_longu f with
+ | Some i => L i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition longofsingle_total (x: aval) :=
+ match x with
+ | FS f =>
+ match Float32.to_long f with
+ | Some i => L i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Definition longuofsingle_total (x: aval) :=
+ match x with
+ | FS f =>
+ match Float32.to_longu f with
+ | Some i => L i
+ | None => ntop
+ end
+ | _ => ntop1 x
+ end.
+
+Lemma intoffloat_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.intoffloat v)) (intoffloat_total x).
+Proof.
+ unfold Val.intoffloat, intoffloat_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float.to_int f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma intuoffloat_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.intuoffloat v)) (intuoffloat_total x).
+Proof.
+ unfold Val.intoffloat, intoffloat_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float.to_intu f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma intofsingle_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.intofsingle v)) (intofsingle_total x).
+Proof.
+ unfold Val.intofsingle, intofsingle_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float32.to_int f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma intuofsingle_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.intuofsingle v)) (intuofsingle_total x).
+Proof.
+ unfold Val.intofsingle, intofsingle_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float32.to_intu f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma singleofint_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.singleofint v)) (singleofint x).
+Proof.
+ unfold Val.singleofint, singleofint; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma singleofintu_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.singleofintu v)) (singleofintu x).
+Proof.
+ unfold Val.singleofintu, singleofintu; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma longoffloat_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.longoffloat v)) (longoffloat_total x).
+Proof.
+ unfold Val.longoffloat, longoffloat_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float.to_long f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma longuoffloat_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.longuoffloat v)) (longuoffloat_total x).
+Proof.
+ unfold Val.longoffloat, longoffloat_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float.to_longu f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma longofsingle_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.longofsingle v)) (longofsingle_total x).
+Proof.
+ unfold Val.longofsingle, longofsingle_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float32.to_long f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma longuofsingle_total_sound:
+ forall v x
+ (MATCH : vmatch v x),
+ vmatch (Val.maketotal (Val.longuofsingle v)) (longuofsingle_total x).
+Proof.
+ unfold Val.longofsingle, longofsingle_total. intros.
+ inv MATCH; simpl in *; try constructor.
+ all: destruct (Float32.to_longu f) as [i|] eqn:E; simpl; [auto with va | constructor].
+Qed.
+
+Lemma singleoflong_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.singleoflong v)) (singleoflong x).
+Proof.
+ unfold Val.singleoflong, singleoflong; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma singleoflongu_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.singleoflongu v)) (singleoflongu x).
+Proof.
+ unfold Val.singleoflongu, singleoflongu; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma floatoflong_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.floatoflong v)) (floatoflong x).
+Proof.
+ unfold Val.floatoflong, floatoflong; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma floatoflongu_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.floatoflongu v)) (floatoflongu x).
+Proof.
+ unfold Val.floatoflongu, floatoflongu; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma floatofint_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.floatofint v)) (floatofint x).
+Proof.
+ unfold Val.floatofint, floatofint; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+Lemma floatofintu_total_sound:
+ forall v x, vmatch v x ->
+ vmatch (Val.maketotal (Val.floatofintu v)) (floatofintu x).
+Proof.
+ unfold Val.floatofintu, floatofintu; intros.
+ inv H; simpl.
+ all: auto with va.
+ all: unfold ntop1, provenance.
+ all: try constructor.
+Qed.
+
+
+Definition divs_total (v w: aval) :=
+ match w, v with
+ | I i2, I i1 =>
+ if Int.eq i2 Int.zero
+ || Int.eq i1 (Int.repr Int.min_signed) && Int.eq i2 Int.mone
+ then ntop
+ else I (Int.divs i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma divs_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.divs v w)) (divs_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ { destruct (_ || _) eqn:E; cbn; unfold ntop; auto with va.
+ }
+ all: unfold ntop2; auto with va.
+ all: destruct (_ || _) eqn:E; unfold ntop2; cbn; auto with va.
+Qed.
+
+Definition divu_total (v w: aval) :=
+ match w, v with
+ | I i2, I i1 =>
+ if Int.eq i2 Int.zero
+ then ntop
+ else I (Int.divu i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma divu_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.divu v w)) (divu_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ { destruct Int.eq eqn:E; cbn; unfold ntop; auto with va.
+ }
+ all: unfold ntop2; auto with va.
+ all: destruct Int.eq eqn:E; unfold ntop2; cbn; auto with va.
+Qed.
+
+Definition mods_total (v w: aval) :=
+ match w, v with
+ | I i2, I i1 =>
+ if Int.eq i2 Int.zero
+ || Int.eq i1 (Int.repr Int.min_signed) && Int.eq i2 Int.mone
+ then ntop
+ else I (Int.mods i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma mods_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.mods v w)) (mods_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ { destruct (_ || _) eqn:E; cbn; unfold ntop; auto with va.
+ }
+ all: unfold ntop2; auto with va.
+ all: destruct (_ || _) eqn:E; unfold ntop2; cbn; auto with va.
+Qed.
+
+Definition modu_total (v w: aval) :=
+ match w, v with
+ | I i2, I i1 =>
+ if Int.eq i2 Int.zero
+ then ntop
+ else I (Int.modu i1 i2)
+ | I i2, _ => uns (provenance v) (usize i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma modu_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.modu v w)) (modu_total x y).
+Proof.
+ assert (UNS: forall i j, j <> Int.zero -> is_uns (usize j) (Int.modu i j)).
+ {
+ intros. apply is_uns_mon with (usize (Int.modu i j)).
+ { apply is_uns_usize.
+ }
+ unfold usize, Int.size.
+ apply Zsize_monotone.
+ generalize (Int.unsigned_range_2 j); intros RANGE.
+ assert (Int.unsigned j <> 0).
+ { red; intros; elim H. rewrite <- (Int.repr_unsigned j). rewrite H0. auto. }
+ exploit (Z_mod_lt (Int.unsigned i) (Int.unsigned j)). lia. intros MOD.
+ unfold Int.modu. rewrite Int.unsigned_repr. lia. lia.
+ }
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ { destruct Int.eq eqn:E; unfold ntop; cbn; auto with va.
+ }
+ all: try discriminate.
+ all: unfold ntop2; auto with va.
+ all: try (destruct Int.eq eqn:E; cbn; unfold ntop2; auto with va; fail).
+ all: try apply vmatch_uns_undef.
+
+ all:
+ generalize (Int.eq_spec i0 Int.zero);
+ destruct (Int.eq i0 Int.zero);
+ cbn;
+ intro.
+ all: try apply vmatch_uns_undef.
+ all: apply vmatch_uns; auto.
+Qed.
+
+
+Lemma shrx_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.shrx v w)) (shrx x y).
+Proof.
+ intros until y. intros HX HY.
+ inv HX; inv HY; cbn.
+ all: unfold ntop1; auto with va.
+ all: destruct Int.ltu eqn:LTU; cbn; unfold ntop; auto with va.
+Qed.
+
+
+Definition divls_total (v w: aval) :=
+ match w, v with
+ | L i2, L i1 =>
+ if Int64.eq i2 Int64.zero
+ || Int64.eq i1 (Int64.repr Int64.min_signed) && Int64.eq i2 Int64.mone
+ then ntop
+ else L (Int64.divs i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma divls_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.divls v w)) (divls_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ all: unfold ntop2; auto with va.
+ all: destruct (_ || _) eqn:E; unfold ntop2, ntop; cbn; auto with va.
+Qed.
+
+Definition divlu_total (v w: aval) :=
+ match w, v with
+ | L i2, L i1 =>
+ if Int64.eq i2 Int64.zero
+ then ntop
+ else L (Int64.divu i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma divlu_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.divlu v w)) (divlu_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ all: unfold ntop2; auto with va.
+ all: destruct Int64.eq eqn:E; unfold ntop2, ntop; cbn; auto with va.
+Qed.
+
+
+Definition modls_total (v w: aval) :=
+ match w, v with
+ | L i2, L i1 =>
+ if Int64.eq i2 Int64.zero
+ || Int64.eq i1 (Int64.repr Int64.min_signed) && Int64.eq i2 Int64.mone
+ then ntop
+ else L (Int64.mods i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma modls_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.modls v w)) (modls_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ all: unfold ntop2; auto with va.
+ all: destruct (_ || _) eqn:E; unfold ntop2, ntop; cbn; auto with va.
+Qed.
+
+
+Definition modlu_total (v w: aval) :=
+ match w, v with
+ | L i2, L i1 =>
+ if Int64.eq i2 Int64.zero
+ then ntop
+ else L (Int64.modu i1 i2)
+ | _, _ => ntop2 v w
+ end.
+
+Lemma modlu_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.modlu v w)) (modlu_total x y).
+Proof.
+ intros until y.
+ intros HX HY.
+ inv HX; inv HY; cbn in *.
+ all: unfold ntop2; auto with va.
+ all: destruct Int64.eq eqn:E; cbn; unfold ntop2, ntop; auto with va.
+Qed.
+
+Lemma shrxl_total_sound:
+ forall v w x y, vmatch v x -> vmatch w y -> vmatch (Val.maketotal (Val.shrxl v w)) (shrxl x y).
+Proof.
+ intros until y. intros HX HY.
+ inv HX; inv HY; cbn.
+ all: unfold ntop1; auto with va.
+ all: destruct Int.ltu eqn:LTU; cbn; unfold ntop; auto with va.
+Qed.
+
(** Comparisons and variation intervals *)
Definition cmp_intv (c: comparison) (i: Z * Z) (n: Z) : abool :=
@@ -4739,6 +5201,26 @@ Global Hint Resolve cnot_sound symbol_address_sound
longoffloat_sound longuoffloat_sound floatoflong_sound floatoflongu_sound
longofsingle_sound longuofsingle_sound singleoflong_sound singleoflongu_sound
longofwords_sound loword_sound hiword_sound
+ intoffloat_total_sound
+ intuoffloat_total_sound
+ intofsingle_total_sound
+ intuofsingle_total_sound
+ singleofint_total_sound
+ singleofintu_total_sound
+ longoffloat_total_sound
+ longuoffloat_total_sound
+ longofsingle_total_sound
+ longuofsingle_total_sound
+ singleoflong_total_sound
+ singleoflongu_total_sound
+ floatoflong_total_sound
+ floatoflongu_total_sound
+ floatofint_total_sound
+ floatofintu_total_sound
+ divu_total_sound divs_total_sound
+ modu_total_sound mods_total_sound shrx_total_sound
+ divlu_total_sound divls_total_sound
+ modlu_total_sound modls_total_sound shrxl_total_sound
cmpu_bool_sound cmp_bool_sound cmplu_bool_sound cmpl_bool_sound
cmpf_bool_sound cmpfs_bool_sound
maskzero_sound : va.
diff --git a/backend/XTL.ml b/backend/XTL.ml
index f10efeed..1d8e89c0 100644
--- a/backend/XTL.ml
+++ b/backend/XTL.ml
@@ -30,13 +30,13 @@ type instruction =
| Xspill of var * var
| Xparmove of var list * var list * var * var
| Xop of operation * var list * var
- | Xload of memory_chunk * addressing * var list * var
+ | Xload of trapping_mode * memory_chunk * addressing * var list * var
| Xstore of memory_chunk * addressing * var list * var
| Xcall of signature * (var, ident) sum * var list * var list
| Xtailcall of signature * (var, ident) sum * var list
| Xbuiltin of external_function * var builtin_arg list * var builtin_res
| Xbranch of node
- | Xcond of condition * var list * node * node
+ | Xcond of condition * var list * node * node * bool option
| Xjumptable of var * node list
| Xreturn of var list
@@ -105,7 +105,7 @@ let twin_reg r =
let rec successors_block = function
| Xbranch s :: _ -> [s]
| Xtailcall(sg, vos, args) :: _ -> []
- | Xcond(cond, args, s1, s2) :: _ -> [s1; s2]
+ | Xcond(cond, args, s1, s2, _) :: _ -> [s1; s2]
| Xjumptable(arg, tbl) :: _ -> tbl
| Xreturn _:: _ -> []
| instr :: blk -> successors_block blk
@@ -159,7 +159,7 @@ let type_instr = function
let (targs, tres) = type_of_operation op in
set_vars_type args targs;
set_var_type res tres
- | Xload(chunk, addr, args, dst) ->
+ | Xload(trap, chunk, addr, args, dst) ->
set_vars_type args (type_of_addressing addr);
set_var_type dst (type_of_chunk chunk)
| Xstore(chunk, addr, args, src) ->
@@ -179,7 +179,7 @@ let type_instr = function
type_builtin_res res (proj_sig_res sg)
| Xbranch s ->
()
- | Xcond(cond, args, s1, s2) ->
+ | Xcond(cond, args, s1, s2, _) ->
set_vars_type args (type_of_condition cond)
| Xjumptable(arg, tbl) ->
set_var_type arg Tint
diff --git a/backend/XTL.mli b/backend/XTL.mli
index 54988d4b..7b7f7186 100644
--- a/backend/XTL.mli
+++ b/backend/XTL.mli
@@ -31,13 +31,13 @@ type instruction =
| Xspill of var * var
| Xparmove of var list * var list * var * var
| Xop of operation * var list * var
- | Xload of memory_chunk * addressing * var list * var
+ | Xload of trapping_mode * memory_chunk * addressing * var list * var
| Xstore of memory_chunk * addressing * var list * var
| Xcall of signature * (var, ident) sum * var list * var list
| Xtailcall of signature * (var, ident) sum * var list
| Xbuiltin of external_function * var builtin_arg list * var builtin_res
| Xbranch of node
- | Xcond of condition * var list * node * node
+ | Xcond of condition * var list * node * node * bool option
| Xjumptable of var * node list
| Xreturn of var list
diff --git a/cfrontend/C2C.ml b/cfrontend/C2C.ml
index 3c71718c..502108f8 100644
--- a/cfrontend/C2C.ml
+++ b/cfrontend/C2C.ml
@@ -47,16 +47,29 @@ let decl_atom : (AST.ident, atom_info) Hashtbl.t = Hashtbl.create 103
let atom_is_static a =
try
- (Hashtbl.find decl_atom a).a_storage = C.Storage_static
+ match (Hashtbl.find decl_atom a).a_storage with
+ | C.Storage_static | C.Storage_thread_local_static -> true
+ | _ -> false
with Not_found ->
false
let atom_is_extern a =
try
- (Hashtbl.find decl_atom a).a_storage = C.Storage_extern
+ match (Hashtbl.find decl_atom a).a_storage with
+ | C.Storage_extern| C.Storage_thread_local_extern -> true
+ | _ -> false
with Not_found ->
false
+let atom_is_thread_local a =
+ try
+ match (Hashtbl.find decl_atom a).a_storage with
+ | C.Storage_thread_local_extern| C.Storage_thread_local_static
+ | C.Storage_thread_local -> true
+ | _ -> false
+ with Not_found ->
+ false
+
let atom_alignof a =
try
(Hashtbl.find decl_atom a).a_alignment
@@ -165,13 +178,14 @@ let ais_annot_functions =
true);]
else
[]
-
+
let builtins_generic = {
builtin_typedefs = [];
builtin_functions =
- ais_annot_functions
- @
+ ais_annot_functions @
[
+ "__builtin_expect",
+ (TInt(ILong, []), [TInt(ILong, []); TInt(ILong, [])], false);
(* Integer arithmetic *)
"__builtin_bswap64",
(TInt(IULongLong, []), [TInt(IULongLong, [])], false);
@@ -257,7 +271,92 @@ let builtins_generic = {
"__builtin_unreachable",
(TVoid [], [], false);
"__builtin_expect",
- (TInt(ILong, []), [TInt(ILong, []); TInt(ILong, [])], false)
+ (TInt(ILong, []), [TInt(ILong, []); TInt(ILong, [])], false);
+ (* Helper functions for int64 arithmetic *)
+ "__compcert_i64_dtos",
+ (TInt(ILongLong, []),
+ [TFloat(FDouble, [])],
+ false);
+ "__compcert_i64_dtou",
+ (TInt(IULongLong, []),
+ [TFloat(FDouble, [])],
+ false);
+ "__compcert_i64_stod",
+ (TFloat(FDouble, []),
+ [TInt(ILongLong, [])],
+ false);
+ "__compcert_i64_utod",
+ (TFloat(FDouble, []),
+ [TInt(IULongLong, [])],
+ false);
+ "__compcert_i64_stof",
+ (TFloat(FFloat, []),
+ [TInt(ILongLong, [])],
+ false);
+ "__compcert_i64_utof",
+ (TFloat(FFloat, []),
+ [TInt(IULongLong, [])],
+ false);
+ "__compcert_i64_sdiv",
+ (TInt(ILongLong, []),
+ [TInt(ILongLong, []); TInt(ILongLong, [])],
+ false);
+ "__compcert_i64_udiv",
+ (TInt(IULongLong, []),
+ [TInt(IULongLong, []); TInt(IULongLong, [])],
+ false);
+ "__compcert_i64_smod",
+ (TInt(ILongLong, []),
+ [TInt(ILongLong, []); TInt(ILongLong, [])],
+ false);
+ "__compcert_i64_umod",
+ (TInt(IULongLong, []),
+ [TInt(IULongLong, []); TInt(IULongLong, [])],
+ false);
+ "__compcert_i64_shl",
+ (TInt(ILongLong, []),
+ [TInt(ILongLong, []); TInt(IInt, [])],
+ false);
+ "__compcert_i64_shr",
+ (TInt(IULongLong, []),
+ [TInt(IULongLong, []); TInt(IInt, [])],
+ false);
+ "__compcert_i64_sar",
+ (TInt(ILongLong, []),
+ [TInt(ILongLong, []); TInt(IInt, [])],
+ false);
+ "__compcert_i64_smulh",
+ (TInt(ILongLong, []),
+ [TInt(ILongLong, []); TInt(ILongLong, [])],
+ false);
+ "__compcert_i64_umulh",
+ (TInt(IULongLong, []),
+ [TInt(IULongLong, []); TInt(IULongLong, [])],
+ false);
+ "__compcert_i32_sdiv",
+ (TInt(IInt, []),
+ [TInt(IInt, []); TInt(IInt, [])],
+ false);
+ "__compcert_i32_udiv",
+ (TInt(IUInt, []),
+ [TInt(IUInt, []); TInt(IUInt, [])],
+ false);
+ "__compcert_i32_smod",
+ (TInt(IInt, []),
+ [TInt(IInt, []); TInt(IInt, [])],
+ false);
+ "__compcert_i32_umod",
+ (TInt(IUInt, []),
+ [TInt(IUInt, []); TInt(IUInt, [])],
+ false);
+ "__compcert_f32_div",
+ (TFloat(FFloat,[]),
+ [TFloat(FFloat,[]); TFloat(FFloat,[])],
+ false);
+ "__compcert_f64_div",
+ (TFloat(FDouble,[]),
+ [TFloat(FDouble,[]); TFloat(FDouble,[])],
+ false);
]
}
@@ -855,6 +954,14 @@ let rec convertExpr env e =
| C.ECompound(ty1, ie) ->
unsupported "compound literals"; ezero
+ | C.ECall({edesc = C.EVar {name = "__builtin_expect"}}, args) ->
+ (match args with
+ | [e1; e2] ->
+ ewrap (Ctyping.ebinop Cop.Oexpect (convertExpr env e1) (convertExpr env e2))
+ | _ ->
+ error "__builtin_expect wants two arguments";
+ ezero)
+
| C.ECall({edesc = C.EVar {name = "__builtin_debug"}}, args) when List.length args < 2 ->
error "too few arguments to function call, expected at least 2, have 0";
ezero
@@ -947,8 +1054,8 @@ let rec convertExpr env e =
ewrap (Ctyping.eselection (convertExpr env arg1)
(convertExpr env arg2) (convertExpr env arg3))
- | C.ECall({edesc = C.EVar {name = "__builtin_expect"}}, [arg1; arg2]) ->
- convertExpr env arg1
+ (*| C.ECall({edesc = C.EVar {name = "__builtin_expect"}}, [arg1; arg2]) ->
+ convertExpr env arg1*)
| C.ECall({edesc = C.EVar {name = "printf"}}, args)
when !Clflags.option_interp ->
@@ -1195,7 +1302,8 @@ let convertFundef loc env fd =
let vars =
List.map
(fun (sto, id, ty, init) ->
- if sto = Storage_extern || sto = Storage_static then
+ if sto = Storage_extern || sto = Storage_thread_local_extern
+ || sto = Storage_static || sto = Storage_thread_local_static then
unsupported "'static' or 'extern' local variable";
if init <> None then
unsupported "initialized local variable";
@@ -1233,6 +1341,13 @@ let convertFundef loc env fd =
let re_builtin = Str.regexp "__builtin_"
+(* BEGIN CHECK *)
+let re_runtime64 = Str.regexp "__compcert_i64"
+let re_runtime32 = Str.regexp "__compcert_i32"
+let re_runtimef32 = Str.regexp "__compcert_f32"
+let re_runtimef64 = Str.regexp "__compcert_f64"
+(* END CHECK *)
+
let convertFundecl env (sto, id, ty, optinit) =
let (args, res, cconv) =
match convertTyp env ty with
@@ -1243,7 +1358,15 @@ let convertFundecl env (sto, id, ty, optinit) =
let sg = signature_of_type args res cconv in
let ef =
if id.name = "malloc" then AST.EF_malloc else
- if id.name = "free" then AST.EF_free else
+ if id.name = "free" then AST.EF_free else
+ (* BEGIN CHECK *)
+ if Str.string_match re_runtime64 id.name 0
+ || Str.string_match re_runtime32 id.name 0
+ || Str.string_match re_runtimef64 id.name 0
+ || Str.string_match re_runtimef32 id.name 0
+ then AST.EF_runtime(id'', sg)
+ else
+ (* END CHECK *)
if Str.string_match re_builtin id.name 0
&& List.mem_assoc id.name builtins.builtin_functions
then AST.EF_builtin(id'', sg)
@@ -1289,7 +1412,8 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
let init' =
match optinit with
| None ->
- if sto = C.Storage_extern then [] else [AST.Init_space sz]
+ if sto = C.Storage_extern || sto = C.Storage_thread_local_extern
+ then [] else [AST.Init_space sz]
| Some i ->
convertInitializer env ty i in
let initialized =
@@ -1298,11 +1422,16 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
then Sections.Init_reloc
else Sections.Init in
let (section, access) =
- Sections.for_variable env loc id' ty initialized in
+ Sections.for_variable env loc id' ty initialized
+ (match sto with
+ | Storage_thread_local | Storage_thread_local_extern
+ | Storage_thread_local_static -> true
+ | _ -> false) in
if Z.gt sz (Z.of_uint64 0xFFFF_FFFFL) then
error "'%s' is too big (%s bytes)"
id.name (Z.to_string sz);
- if sto <> C.Storage_extern && Cutil.incomplete_type env ty then
+ if sto <> C.Storage_extern && sto <> C.Storage_thread_local_extern
+ && Cutil.incomplete_type env ty then
error "'%s' has incomplete type" id.name;
Hashtbl.add decl_atom id'
{ a_storage = sto;
@@ -1476,7 +1605,7 @@ let cleanupGlobals p =
if IdentSet.mem fd.fd_name !strong then
error "multiple definitions of %s" fd.fd_name.name;
strong := IdentSet.add fd.fd_name !strong
- | C.Gdecl(Storage_extern, id, ty, init) ->
+ | C.Gdecl((Storage_extern|Storage_thread_local_extern), id, ty, init) ->
extern := IdentSet.add id !extern
| C.Gdecl(sto, id, ty, Some i) ->
if IdentSet.mem id !strong then
@@ -1495,7 +1624,7 @@ let cleanupGlobals p =
match g.gdesc with
| C.Gdecl(sto, id, ty, init) ->
let better_def_exists =
- if sto = Storage_extern then
+ if sto = Storage_extern || sto = Storage_thread_local_extern then
IdentSet.mem id !strong || IdentSet.mem id !weak
else if init = None then
IdentSet.mem id !strong
diff --git a/cfrontend/Cexec.v b/cfrontend/Cexec.v
index 52037ac0..cc3058a9 100644
--- a/cfrontend/Cexec.v
+++ b/cfrontend/Cexec.v
@@ -556,6 +556,10 @@ Definition do_ef_debug (kind: positive) (text: ident) (targs: list typ)
(w: world) (vargs: list val) (m: mem) : option (world * trace * val * mem) :=
Some(w, E0, Vundef, m).
+Definition do_ef_profiling (id : profiling_id)
+ (w: world) (vargs: list val) (m: mem) : option (world * trace * val * mem) :=
+ Some(w, E0, Vundef, m).
+
Definition do_builtin_or_external (name: string) (sg: signature)
(w: world) (vargs: list val) (m: mem) : option (world * trace * val * mem) :=
match lookup_builtin_function name sg with
@@ -578,6 +582,7 @@ Definition do_external (ef: external_function):
| EF_annot_val kind text targ => do_ef_annot_val text targ
| EF_inline_asm text sg clob => do_inline_assembly text sg ge
| EF_debug kind text targs => do_ef_debug kind text targs
+ | EF_profiling id kind => do_ef_profiling id
end.
Lemma do_ef_external_sound:
@@ -645,6 +650,8 @@ Proof with try congruence.
eapply do_inline_assembly_sound; eauto.
- (* EF_debug *)
unfold do_ef_debug. mydestr. split; constructor.
+- (* EF_profiling *)
+ unfold do_ef_profiling. mydestr. split; constructor.
Qed.
Lemma do_ef_external_complete:
@@ -699,6 +706,8 @@ Proof.
eapply do_inline_assembly_complete; eauto.
- (* EF_debug *)
inv H. inv H0. reflexivity.
+- (* EF_profiling *)
+ inv H. inv H0. reflexivity.
Qed.
(** * Reduction of expressions *)
diff --git a/cfrontend/Cminorgenproof.v b/cfrontend/Cminorgenproof.v
index bc1c92ca..4c97011e 100644
--- a/cfrontend/Cminorgenproof.v
+++ b/cfrontend/Cminorgenproof.v
@@ -1335,6 +1335,7 @@ Lemma eval_binop_compat:
/\ Val.inject f v tv.
Proof.
destruct op; simpl; intros; inv H.
+- TrivialExists. apply Val.normalize_inject; auto.
- TrivialExists. apply Val.add_inject; auto.
- TrivialExists. apply Val.sub_inject; auto.
- TrivialExists. inv H0; inv H1; constructor.
diff --git a/cfrontend/Cop.v b/cfrontend/Cop.v
index 0d7bcc3a..2f8b5e7c 100644
--- a/cfrontend/Cop.v
+++ b/cfrontend/Cop.v
@@ -34,6 +34,7 @@ Inductive unary_operation : Type :=
| Oabsfloat : unary_operation. (**r floating-point absolute value *)
Inductive binary_operation : Type :=
+ | Oexpect : binary_operation (**r return first argument *)
| Oadd : binary_operation (**r addition (binary [+]) *)
| Osub : binary_operation (**r subtraction (binary [-]) *)
| Omul : binary_operation (**r multiplication (binary [*]) *)
@@ -764,6 +765,14 @@ Definition sem_mul (v1:val) (t1:type) (v2: val) (t2:type) (m:mem) : option val :
(fun n1 n2 => Some(Vsingle(Float32.mul n1 n2)))
v1 t1 v2 t2 m.
+Definition sem_expect (v1:val) (t1:type) (v2: val) (t2:type) (m:mem) : option val :=
+ sem_binarith
+ (fun sg n1 n2 => Some(Vint n1))
+ (fun sg n1 n2 => Some(Vlong n1))
+ (fun n1 n2 => Some(Vfloat n1))
+ (fun n1 n2 => Some(Vsingle n1))
+ v1 t1 v2 t2 m.
+
Definition sem_div (v1:val) (t1:type) (v2: val) (t2:type) (m:mem) : option val :=
sem_binarith
(fun sg n1 n2 =>
@@ -1051,6 +1060,7 @@ Definition sem_binary_operation
(v1: val) (t1: type) (v2: val) (t2:type)
(m: mem): option val :=
match op with
+ | Oexpect => sem_expect v1 t1 v2 t2 m
| Oadd => sem_add cenv v1 t1 v2 t2 m
| Osub => sem_sub cenv v1 t1 v2 t2 m
| Omul => sem_mul v1 t1 v2 t2 m
@@ -1345,6 +1355,9 @@ Lemma sem_binary_operation_inj:
exists tv, sem_binary_operation cenv op tv1 ty1 tv2 ty2 m' = Some tv /\ Val.inject f v tv.
Proof.
unfold sem_binary_operation; intros; destruct op.
+- (* expect *)
+ unfold sem_expect in *.
+ eapply sem_binarith_inject; eauto; intros; exact I.
- (* add *)
assert (A: forall cenv ty si v1' v2' tv1' tv2',
Val.inject f v1' tv1' -> Val.inject f v2' tv2' ->
diff --git a/cfrontend/Cshmgen.v b/cfrontend/Cshmgen.v
index 9e804176..173b8c8c 100644
--- a/cfrontend/Cshmgen.v
+++ b/cfrontend/Cshmgen.v
@@ -259,6 +259,11 @@ Definition make_add_ptr_long (ce: composite_env) (ty: type) (e1 e2: expr) :=
let n := make_intconst (Int.repr sz) in
OK (Ebinop Oadd e1 (Ebinop Omul n (Eunop Ointoflong e2))).
+Definition make_expect (e1: expr) (ty1: type) (e2: expr) (ty2: type) :=
+ make_binarith (Oexpect AST.Tint) (Oexpect AST.Tint)
+ (Oexpect AST.Tfloat) (Oexpect AST.Tsingle)
+ (Oexpect AST.Tlong) (Oexpect AST.Tlong) e1 ty1 e2 ty2.
+
Definition make_add (ce: composite_env) (e1: expr) (ty1: type) (e2: expr) (ty2: type) :=
match classify_add ty1 ty2 with
| add_case_pi ty si => make_add_ptr_int ce ty si e1 e2
@@ -463,6 +468,7 @@ Definition transl_binop (ce: composite_env)
(a: expr) (ta: type)
(b: expr) (tb: type) : res expr :=
match op with
+ | Cop.Oexpect => make_expect a ta b tb
| Cop.Oadd => make_add ce a ta b tb
| Cop.Osub => make_sub ce a ta b tb
| Cop.Omul => make_mul a ta b tb
diff --git a/cfrontend/Cshmgenproof.v b/cfrontend/Cshmgenproof.v
index 8e396e2a..e373d4e5 100644
--- a/cfrontend/Cshmgenproof.v
+++ b/cfrontend/Cshmgenproof.v
@@ -607,6 +607,11 @@ End MAKE_BIN.
Hint Extern 2 (@eq (option val) _ _) => (simpl; reflexivity) : cshm.
+Lemma make_expect_correct: binary_constructor_correct make_expect sem_expect.
+Proof.
+ apply make_binarith_correct; intros; auto.
+Qed.
+
Lemma make_add_correct: binary_constructor_correct (make_add cunit.(prog_comp_env)) (sem_add prog.(prog_comp_env)).
Proof.
assert (A: forall ty si a b c e le m va vb v,
@@ -910,22 +915,23 @@ Lemma transl_binop_correct:
eval_expr ge e le m c v.
Proof.
intros. destruct op; simpl in *.
- eapply make_add_correct; eauto.
- eapply make_sub_correct; eauto.
- eapply make_mul_correct; eauto.
- eapply make_div_correct; eauto.
- eapply make_mod_correct; eauto.
- eapply make_and_correct; eauto.
- eapply make_or_correct; eauto.
- eapply make_xor_correct; eauto.
- eapply make_shl_correct; eauto.
- eapply make_shr_correct; eauto.
- eapply make_cmp_correct; eauto.
- eapply make_cmp_correct; eauto.
- eapply make_cmp_correct; eauto.
- eapply make_cmp_correct; eauto.
- eapply make_cmp_correct; eauto.
- eapply make_cmp_correct; eauto.
+- eapply make_expect_correct; eauto.
+- eapply make_add_correct; eauto.
+- eapply make_sub_correct; eauto.
+- eapply make_mul_correct; eauto.
+- eapply make_div_correct; eauto.
+- eapply make_mod_correct; eauto.
+- eapply make_and_correct; eauto.
+- eapply make_or_correct; eauto.
+- eapply make_xor_correct; eauto.
+- eapply make_shl_correct; eauto.
+- eapply make_shr_correct; eauto.
+- eapply make_cmp_correct; eauto.
+- eapply make_cmp_correct; eauto.
+- eapply make_cmp_correct; eauto.
+- eapply make_cmp_correct; eauto.
+- eapply make_cmp_correct; eauto.
+- eapply make_cmp_correct; eauto.
Qed.
Remark int_ltu_true:
diff --git a/cfrontend/Ctyping.v b/cfrontend/Ctyping.v
index 97ed1ed3..834b1931 100644
--- a/cfrontend/Ctyping.v
+++ b/cfrontend/Ctyping.v
@@ -112,6 +112,7 @@ Definition comparison_type (ty1 ty2: type) (m: string): res type :=
Definition type_binop (op: binary_operation) (ty1 ty2: type) : res type :=
match op with
+ | Oexpect => binarith_type ty1 ty2 "__builtin_expect"
| Oadd =>
match classify_add ty1 ty2 with
| add_case_pi ty _ | add_case_ip _ ty
@@ -1548,6 +1549,8 @@ Lemma pres_sem_binop:
Proof.
intros until m; intros TY SEM WT1 WT2.
destruct op; simpl in TY; simpl in SEM.
+- (* expect *)
+ unfold sem_expect in SEM. eapply pres_sem_binarith; eauto; intros; exact I.
- (* add *)
unfold sem_add, sem_add_ptr_int, sem_add_ptr_long in SEM; DestructCases; auto with ty.
eapply pres_sem_binarith; eauto; intros; exact I.
diff --git a/cfrontend/PrintClight.ml b/cfrontend/PrintClight.ml
index a9ecb342..7ca64741 100644
--- a/cfrontend/PrintClight.ml
+++ b/cfrontend/PrintClight.ml
@@ -63,6 +63,7 @@ let precedence = function
| Ebinop(Oand, _, _, _) -> (8, LtoR)
| Ebinop(Oxor, _, _, _) -> (7, LtoR)
| Ebinop(Oor, _, _, _) -> (6, LtoR)
+ | Ebinop(Oexpect, _, _, _) -> (5, LtoR)
(* Expressions *)
diff --git a/cfrontend/PrintCsyntax.ml b/cfrontend/PrintCsyntax.ml
index 16cdfc41..5fa9ea17 100644
--- a/cfrontend/PrintCsyntax.ml
+++ b/cfrontend/PrintCsyntax.ml
@@ -31,6 +31,7 @@ let name_unop = function
| Oabsfloat -> "__builtin_fabs"
let name_binop = function
+ | Oexpect -> "expect"
| Oadd -> "+"
| Osub -> "-"
| Omul -> "*"
@@ -159,6 +160,7 @@ let rec precedence = function
| Ebinop(Oand, _, _, _) -> (8, LtoR)
| Ebinop(Oxor, _, _, _) -> (7, LtoR)
| Ebinop(Oor, _, _, _) -> (6, LtoR)
+ | Ebinop(Oexpect, _, _, _) -> (5, LtoR) (* fixme *)
| Eseqand _ -> (5, LtoR)
| Eseqor _ -> (4, LtoR)
| Econdition _ -> (3, RtoL)
diff --git a/common/AST.v b/common/AST.v
index 2259d74c..868364cd 100644
--- a/common/AST.v
+++ b/common/AST.v
@@ -18,7 +18,7 @@
the abstract syntax trees of many of the intermediate languages. *)
Require Import String.
-Require Import Coqlib Maps Errors Integers Floats.
+Require Import Coqlib Maps Errors Integers Floats BinPos.
Require Archi.
Set Implicit Arguments.
@@ -233,6 +233,16 @@ Definition chunk_of_type (ty: typ) :=
Lemma chunk_of_Tptr: chunk_of_type Tptr = Mptr.
Proof. unfold Mptr, Tptr; destruct Archi.ptr64; auto. Qed.
+(** Trapping mode: does undefined behavior result in a trap or an undefined value (e.g. for loads) *)
+Inductive trapping_mode : Type := TRAP | NOTRAP.
+
+Definition trapping_mode_eq : forall x y : trapping_mode,
+ { x=y } + { x <> y}.
+Proof.
+ decide equality.
+Defined.
+
+
(** Initialization data for global variables. *)
Inductive init_data: Type :=
@@ -455,6 +465,11 @@ Qed.
(** * External functions *)
+(* Identifiers for profiling information *)
+Parameter profiling_id : Type.
+Axiom profiling_id_eq : forall (x y : profiling_id), {x=y} + {x<>y}.
+Definition profiling_kind := Z.t.
+
(** For most languages, the functions composing the program are either
internal functions, defined within the language, or external functions,
defined outside. External functions include system calls but also
@@ -505,10 +520,13 @@ Inductive external_function : Type :=
used with caution, as it can invalidate the semantic
preservation theorem. Generated only if [-finline-asm] is
given. *)
- | EF_debug (kind: positive) (text: ident) (targs: list typ).
+ | EF_debug (kind: positive) (text: ident) (targs: list typ)
(** Transport debugging information from the front-end to the generated
assembly. Takes zero, one or several arguments like [EF_annot].
Unlike [EF_annot], produces no observable event. *)
+ | EF_profiling (id: profiling_id) (kind : profiling_kind).
+ (** Count one profiling event for this identifier and kind.
+ Takes no argument. Produces no observable event. *)
(** The type signature of an external function. *)
@@ -526,6 +544,7 @@ Definition ef_sig (ef: external_function): signature :=
| EF_annot_val kind text targ => mksignature (targ :: nil) targ cc_default
| EF_inline_asm text sg clob => sg
| EF_debug kind text targs => mksignature targs Tvoid cc_default
+ | EF_profiling id kind => mksignature nil Tvoid cc_default
end.
(** Whether an external function should be inlined by the compiler. *)
@@ -544,6 +563,7 @@ Definition ef_inline (ef: external_function) : bool :=
| EF_annot_val kind Text rg => true
| EF_inline_asm text sg clob => true
| EF_debug kind text targs => true
+ | EF_profiling id kind => true
end.
(** Whether an external function must reload its arguments. *)
@@ -559,7 +579,7 @@ Definition ef_reloads (ef: external_function) : bool :=
Definition external_function_eq: forall (ef1 ef2: external_function), {ef1=ef2} + {ef1<>ef2}.
Proof.
- generalize ident_eq string_dec signature_eq chunk_eq typ_eq list_eq_dec zeq Int.eq_dec; intros.
+ generalize profiling_id_eq ident_eq string_dec signature_eq chunk_eq typ_eq list_eq_dec zeq Int.eq_dec; intros.
decide equality.
Defined.
Global Opaque external_function_eq.
@@ -670,11 +690,28 @@ Inductive builtin_arg (A: Type) : Type :=
| BA_splitlong (hi lo: builtin_arg A)
| BA_addptr (a1 a2: builtin_arg A).
+Definition builtin_arg_eq {A: Type}:
+ (forall x y : A, {x = y} + {x <> y}) ->
+ forall (ba1 ba2: (builtin_arg A)), {ba1=ba2} + {ba1<>ba2}.
+Proof.
+ intro. generalize Integers.int_eq int64_eq float_eq float32_eq chunk_eq ptrofs_eq ident_eq.
+ decide equality.
+Defined.
+Global Opaque builtin_arg_eq.
+
Inductive builtin_res (A: Type) : Type :=
| BR (x: A)
| BR_none
| BR_splitlong (hi lo: builtin_res A).
+Definition builtin_res_eq {A: Type}:
+ (forall x y : A, {x = y} + {x <> y}) ->
+ forall (a b: builtin_res A), {a=b} + {a<>b}.
+Proof.
+ intro. decide equality.
+Defined.
+Global Opaque builtin_res_eq.
+
Fixpoint globals_of_builtin_arg (A: Type) (a: builtin_arg A) : list ident :=
match a with
| BA_loadglobal chunk id ofs => id :: nil
diff --git a/common/DebugPrint.ml b/common/DebugPrint.ml
new file mode 100644
index 00000000..83a485b0
--- /dev/null
+++ b/common/DebugPrint.ml
@@ -0,0 +1,144 @@
+open Maps
+open Camlcoq
+open Registers
+open RTLcommonaux
+
+let debug_flag = ref false
+
+let debug fmt =
+ if !debug_flag then (flush stderr; flush stdout; Printf.eprintf fmt)
+ else Printf.ifprintf stderr fmt
+
+let print_ptree_bool oc pt =
+ if !debug_flag then
+ let elements = PTree.elements pt in
+ begin
+ Printf.fprintf oc "[";
+ List.iter (fun (n, b) ->
+ if b then Printf.fprintf oc "%d, " (P.to_int n)
+ ) elements;
+ Printf.fprintf oc "]\n"
+ end
+ else ()
+
+let print_ptree_opint oc pt =
+ if !debug_flag then
+ let elements = PTree.elements pt in
+ begin
+ Printf.fprintf oc "[";
+ List.iter (fun (n, op) ->
+ match op with
+ | None -> ()
+ | Some p -> Printf.fprintf oc "%d -> %d, " (P.to_int n) (P.to_int p)
+ ) elements;
+ Printf.fprintf oc "]\n"
+ end
+ else ()
+
+let print_intlist oc l =
+ let rec f oc = function
+ | [] -> ()
+ | n::ln -> (Printf.fprintf oc "%d %a" (P.to_int n) f ln)
+ in begin
+ if !debug_flag then begin
+ Printf.fprintf oc "[%a]" f l
+ end
+ end
+
+let print_ptree_oplist oc pt =
+ if !debug_flag then
+ let elements = PTree.elements pt in
+ begin
+ Printf.fprintf oc "[";
+ List.iter (fun (n, ol) ->
+ match ol with
+ | None -> ()
+ | Some l -> Printf.fprintf oc "%d -> %a,\n" (P.to_int n) print_intlist l
+ ) elements;
+ Printf.fprintf oc "]\n"
+ end
+ else ()
+
+(* Adapted from backend/PrintRTL.ml: print_function *)
+let print_code code = let open PrintRTL in let open Printf in
+ if (!debug_flag) then begin
+ fprintf stdout "{\n";
+ let instrs =
+ List.sort
+ (fun (pc1, _) (pc2, _) -> compare pc2 pc1)
+ (List.rev_map
+ (fun (pc, i) -> (P.to_int pc, i))
+ (PTree.elements code)) in
+ List.iter (print_instruction stdout) instrs;
+ fprintf stdout "}"
+ end
+
+let ptree_printbool pt =
+ let elements = PTree.elements pt
+ in begin
+ if !debug_flag then begin
+ Printf.printf "[";
+ List.iter (fun (n, b) ->
+ if b then Printf.printf "%d, " (P.to_int n) else ()
+ ) elements;
+ Printf.printf "]"
+ end
+ end
+
+let print_ptree printer pt =
+ let elements = PTree.elements pt in
+ begin
+ debug "[\n";
+ List.iter (fun (n, elt) ->
+ debug "\t%d: %a\n" (P.to_int n) printer elt
+ ) elements;
+ debug "]\n"
+ end
+
+let print_option_pint oc o =
+ if !debug_flag then
+ match o with
+ | None -> Printf.fprintf oc "None"
+ | Some n -> Printf.fprintf oc "Some %d" (P.to_int n)
+
+let print_pint oc i = if !debug_flag then Printf.fprintf oc "%d" (P.to_int i) else ()
+
+let print_regset rs = begin
+ debug "[";
+ List.iter (fun n -> debug "%d " (P.to_int n)) (Regset.elements rs);
+ debug "]"
+end
+
+let print_ptree_regset pt = begin
+ debug "[";
+ List.iter (fun (n, rs) ->
+ debug "\n\t";
+ debug "%d: " (P.to_int n);
+ print_regset rs
+ ) (PTree.elements pt);
+ debug "]"
+end
+
+let print_true_nodes booltree = begin
+ debug "[";
+ List.iter (fun (n,b) ->
+ if b then debug "%d " (P.to_int n)
+ ) (PTree.elements booltree);
+ debug "]";
+end
+
+
+let print_instructions insts code =
+ if (!debug_flag) then begin
+ debug "[ ";
+ List.iter (
+ fun n -> (PrintRTL.print_instruction stdout (P.to_int n, get_some @@ PTree.get n code))
+ ) insts; debug " ]"
+ end
+
+let print_arrayp arr = begin
+ debug "[| ";
+ Array.iter (fun n -> debug "%d, " (P.to_int n)) arr;
+ debug "|]"
+end
+
diff --git a/common/Events.v b/common/Events.v
index c4a6e7f9..360da52f 100644
--- a/common/Events.v
+++ b/common/Events.v
@@ -26,6 +26,7 @@ Require Import Values.
Require Import Memory.
Require Import Globalenvs.
Require Import Builtins.
+Require Import Lia.
(** * Events and traces *)
@@ -1379,6 +1380,11 @@ Inductive extcall_debug_sem (ge: Senv.t):
| extcall_debug_sem_intro: forall vargs m,
extcall_debug_sem ge vargs m E0 Vundef m.
+Inductive extcall_profiling_sem (ge: Senv.t):
+ list val -> mem -> trace -> val -> mem -> Prop :=
+ | extcall_profiling_sem_intro: forall vargs m,
+ extcall_profiling_sem ge vargs m E0 Vundef m.
+
Lemma extcall_debug_ok:
forall targs,
extcall_properties extcall_debug_sem
@@ -1413,6 +1419,40 @@ Proof.
split. constructor. auto.
Qed.
+Lemma extcall_profiling_ok:
+ forall targs,
+ extcall_properties extcall_profiling_sem
+ (mksignature targs Tvoid cc_default).
+Proof.
+ intros; constructor; intros.
+(* well typed *)
+- inv H. simpl. auto.
+(* symbols *)
+- inv H0. econstructor; eauto.
+(* valid blocks *)
+- inv H; auto.
+(* perms *)
+- inv H; auto.
+(* readonly *)
+- inv H; auto.
+(* mem extends *)
+- inv H.
+ exists Vundef; exists m1'; intuition.
+ econstructor; eauto.
+(* mem injects *)
+- inv H0.
+ exists f; exists Vundef; exists m1'; intuition.
+ econstructor; eauto.
+ red; intros; congruence.
+(* trace length *)
+- inv H; simpl; lia.
+(* receptive *)
+- inv H; inv H0. exists Vundef, m1; constructor.
+(* determ *)
+- inv H; inv H0.
+ split. constructor. auto.
+Qed.
+
(** ** Semantics of known built-in functions. *)
(** Some built-in functions and runtime support functions have known semantics
@@ -1531,6 +1571,7 @@ Definition external_call (ef: external_function): extcall_sem :=
| EF_annot_val kind txt targ => extcall_annot_val_sem txt targ
| EF_inline_asm txt sg clb => inline_assembly_sem txt sg
| EF_debug kind txt targs => extcall_debug_sem
+ | EF_profiling id kind => extcall_profiling_sem
end.
Theorem external_call_spec:
@@ -1538,18 +1579,19 @@ Theorem external_call_spec:
extcall_properties (external_call ef) (ef_sig ef).
Proof.
intros. unfold external_call, ef_sig; destruct ef.
- apply external_functions_properties.
- apply builtin_or_external_sem_ok.
- apply builtin_or_external_sem_ok.
- apply volatile_load_ok.
- apply volatile_store_ok.
- apply extcall_malloc_ok.
- apply extcall_free_ok.
- apply extcall_memcpy_ok.
- apply extcall_annot_ok.
- apply extcall_annot_val_ok.
- apply inline_assembly_properties.
- apply extcall_debug_ok.
+- apply external_functions_properties.
+- apply builtin_or_external_sem_ok.
+- apply builtin_or_external_sem_ok.
+- apply volatile_load_ok.
+- apply volatile_store_ok.
+- apply extcall_malloc_ok.
+- apply extcall_free_ok.
+- apply extcall_memcpy_ok.
+- apply extcall_annot_ok.
+- apply extcall_annot_val_ok.
+- apply inline_assembly_properties.
+- apply extcall_debug_ok.
+- apply extcall_profiling_ok.
Qed.
Definition external_call_well_typed_gen ef := ec_well_typed (external_call_spec ef).
diff --git a/common/Memdata.v b/common/Memdata.v
index 1bd87169..c80b3754 100644
--- a/common/Memdata.v
+++ b/common/Memdata.v
@@ -24,6 +24,7 @@ Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
+Require Import Lia.
(** * Properties of memory chunks *)
@@ -45,6 +46,13 @@ Definition size_chunk (chunk: memory_chunk) : Z :=
| Many64 => 8
end.
+Definition largest_size_chunk := 8.
+
+Lemma max_size_chunk: forall chunk, size_chunk chunk <= 8.
+Proof.
+ destruct chunk; simpl; lia.
+Qed.
+
Lemma size_chunk_pos:
forall chunk, size_chunk chunk > 0.
Proof.
diff --git a/common/Memory.v b/common/Memory.v
index fa60455b..ff17efb0 100644
--- a/common/Memory.v
+++ b/common/Memory.v
@@ -39,6 +39,8 @@ Require Import Floats.
Require Import Values.
Require Export Memdata.
Require Export Memtype.
+Require Import Lia.
+
(* To avoid useless definitions of inductors in extracted code. *)
Local Unset Elimination Schemes.
@@ -539,6 +541,48 @@ Proof.
induction vl; simpl; intros. auto. rewrite IHvl. auto.
Qed.
+Remark set_setN_swap_disjoint:
+ forall vl: list memval,
+ forall v: memval,
+ forall m : ZMap.t memval,
+ forall p pl: Z,
+ ~ (Intv.In p (pl, pl + Z.of_nat (length vl))) ->
+ (setN vl pl (ZMap.set p v m)) = (ZMap.set p v (setN vl pl m)).
+Proof.
+ induction vl; simpl; trivial.
+ intros.
+ unfold Intv.In in *; simpl in *.
+ rewrite ZMap.set_disjoint by lia.
+ apply IHvl.
+ lia.
+Qed.
+
+Lemma setN_swap_disjoint:
+ forall vl1 vl2: list memval,
+ forall m : ZMap.t memval,
+ forall p1 p2: Z,
+ Intv.disjoint (p1, p1 + Z.of_nat (length vl1))
+ (p2, p2 + Z.of_nat (length vl2)) ->
+ (setN vl1 p1 (setN vl2 p2 m)) = (setN vl2 p2 (setN vl1 p1 m)).
+Proof.
+ induction vl1; simpl; trivial.
+ intros until p2. intro DISJOINT.
+ rewrite <- set_setN_swap_disjoint.
+ { rewrite IHvl1.
+ reflexivity.
+ unfold Intv.disjoint, Intv.In in *.
+ simpl in *.
+ intro.
+ intro BOUNDS.
+ apply DISJOINT.
+ lia.
+ }
+ unfold Intv.disjoint, Intv.In in *.
+ simpl in *.
+ apply DISJOINT.
+ lia.
+Qed.
+
(** [store chunk m b ofs v] perform a write in memory state [m].
Value [v] is stored at address [b] and offset [ofs].
Return the updated memory store, or [None] if the accessed bytes
@@ -1179,6 +1223,118 @@ Local Hint Resolve store_valid_block_1 store_valid_block_2: mem.
Local Hint Resolve store_valid_access_1 store_valid_access_2
store_valid_access_3: mem.
+Remark mem_same_proof_irr :
+ forall m1 m2 : mem,
+ (mem_contents m1) = (mem_contents m2) ->
+ (mem_access m1) = (mem_access m2) ->
+ (nextblock m1) = (nextblock m2) ->
+ m1 = m2.
+Proof.
+ destruct m1 as [contents1 access1 nextblock1 access_max1 nextblock_noaccess1 default1].
+ destruct m2 as [contents2 access2 nextblock2 access_max2 nextblock_noaccess2 default2].
+ simpl.
+ intros.
+ subst contents2.
+ subst access2.
+ subst nextblock2.
+ f_equal; apply proof_irr.
+Qed.
+
+Theorem store_store_other:
+ forall chunk b ofs v chunk' b' ofs' v' m0 m1 m1',
+ b' <> b
+ \/ ofs' + size_chunk chunk' <= ofs
+ \/ ofs + size_chunk chunk <= ofs' ->
+ store chunk m0 b ofs v = Some m1 ->
+ store chunk' m0 b' ofs' v' = Some m1' ->
+ store chunk' m1 b' ofs' v' =
+ store chunk m1' b ofs v.
+Proof.
+ intros until m1'.
+ intro DISJOINT.
+ intros W0 W0'.
+ assert (valid_access m1' chunk b ofs Writable) as WRITEABLE1' by eauto with mem.
+ (* {
+ eapply store_valid_access_1.
+ apply W0'.
+ eapply store_valid_access_3.
+ apply W0.
+ } *)
+ assert (valid_access m1 chunk' b' ofs' Writable) as WRITABLE1 by eauto with mem.
+ (* {
+ eapply store_valid_access_1.
+ apply W0.
+ eapply store_valid_access_3.
+ apply W0'.
+ } *)
+ unfold store in *.
+ destruct (valid_access_dec m0 chunk b ofs Writable).
+ 2: congruence.
+ destruct (valid_access_dec m1 chunk' b' ofs' Writable).
+ 2: contradiction.
+ destruct (valid_access_dec m0 chunk' b' ofs' Writable).
+ 2: congruence.
+ destruct (valid_access_dec m1' chunk b ofs Writable).
+ 2: contradiction.
+ f_equal.
+ inv W0; simpl in *.
+ inv W0'; simpl in *.
+ apply mem_same_proof_irr; simpl; trivial.
+ destruct (eq_block b b').
+ { subst b'.
+ rewrite PMap.gss.
+ rewrite PMap.gss.
+ rewrite PMap.set2.
+ rewrite PMap.set2.
+ f_equal.
+ apply setN_swap_disjoint.
+ unfold Intv.disjoint.
+ rewrite encode_val_length.
+ rewrite <- size_chunk_conv.
+ rewrite encode_val_length.
+ rewrite <- size_chunk_conv.
+ unfold Intv.In; simpl.
+ intros.
+ destruct DISJOINT. contradiction.
+ lia.
+ }
+ {
+ rewrite PMap.set_disjoint by congruence.
+ rewrite PMap.gso by congruence.
+ rewrite PMap.gso by congruence.
+ reflexivity.
+ }
+Qed.
+
+Section STOREV.
+Variable chunk: memory_chunk.
+Variable m1: mem.
+Variables addr v: val.
+Variable m2: mem.
+Hypothesis STORE: storev chunk m1 addr v = Some m2.
+
+
+Theorem loadv_storev_same:
+ loadv chunk m2 addr = Some (Val.load_result chunk v).
+Proof.
+ destruct addr; simpl in *; try discriminate.
+ eapply load_store_same.
+ eassumption.
+Qed.
+
+Theorem storev_preserv_valid (b : block) (ofs: Z): valid_pointer m1 b ofs = valid_pointer m2 b ofs.
+Proof.
+ unfold storev in STORE.
+ cut (valid_pointer m1 b ofs = true <-> valid_pointer m2 b ofs = true).
+ { destruct (valid_pointer _ _ _), (valid_pointer _ _ _); intuition congruence. }
+ destruct addr; try congruence.
+ rewrite! valid_pointer_valid_access. split.
+ - intros; eapply store_valid_access_1; eauto.
+ - intros; eapply store_valid_access_2; eauto.
+Qed.
+
+End STOREV.
+
Lemma load_store_overlap:
forall chunk m1 b ofs v m2 chunk' ofs' v',
store chunk m1 b ofs v = Some m2 ->
diff --git a/common/PrintAST.ml b/common/PrintAST.ml
index 61c76c91..c33cb2dc 100644
--- a/common/PrintAST.ml
+++ b/common/PrintAST.ml
@@ -48,6 +48,13 @@ let name_of_chunk = function
| Many32 -> "any32"
| Many64 -> "any64"
+let spp_profiling_id () (x : Digest.t) : string =
+ let s = Buffer.create 32 in
+ for i=0 to 15 do
+ Printf.bprintf s "%02x" (Char.code (String.get x i))
+ done;
+ Buffer.contents s;;
+
let name_of_external = function
| EF_external(name, sg) -> sprintf "extern %S" (camlstring_of_coqstring name)
| EF_builtin(name, sg) -> sprintf "builtin %S" (camlstring_of_coqstring name)
@@ -62,7 +69,9 @@ let name_of_external = function
| EF_annot_val(kind,text, targ) -> sprintf "annot_val %S" (camlstring_of_coqstring text)
| EF_inline_asm(text, sg, clob) -> sprintf "inline_asm %S" (camlstring_of_coqstring text)
| EF_debug(kind, text, targs) ->
- sprintf "debug%d %S" (P.to_int kind) (extern_atom text)
+ sprintf "debug%d %S" (P.to_int kind) (extern_atom text)
+ | EF_profiling(id, kind) ->
+ sprintf "profiling %a %d" spp_profiling_id id (Z.to_int kind)
let rec print_builtin_arg px oc = function
| BA x -> px oc x
@@ -99,3 +108,7 @@ let rec print_builtin_res px oc = function
fprintf oc "splitlong(%a, %a)"
(print_builtin_res px) hi (print_builtin_res px) lo
+let print_trapping_mode oc = function
+ | TRAP -> ()
+ | NOTRAP -> output_string oc " [notrap]"
+
diff --git a/common/Sections.ml b/common/Sections.ml
index 794b8470..c256628e 100644
--- a/common/Sections.ml
+++ b/common/Sections.ml
@@ -23,7 +23,7 @@ type initialized =
type section_name =
| Section_text
- | Section_data of initialized
+ | Section_data of initialized * bool (* true = thread local ? *)
| Section_small_data of initialized
| Section_const of initialized
| Section_small_const of initialized
@@ -54,9 +54,9 @@ type section_info = {
}
let default_section_info = {
- sec_name_init = Section_data Init;
- sec_name_init_reloc = Section_data Init_reloc;
- sec_name_uninit = Section_data Uninit;
+ sec_name_init = Section_data (Init, false);
+ sec_name_init_reloc = Section_data (Init_reloc, false);
+ sec_name_uninit = Section_data (Uninit, false);
sec_writable = true;
sec_executable = false;
sec_access = Access_default
@@ -72,9 +72,15 @@ let builtin_sections = [
sec_writable = false; sec_executable = true;
sec_access = Access_default};
"DATA",
- {sec_name_init = Section_data Init;
- sec_name_init_reloc = Section_data Init_reloc;
- sec_name_uninit = Section_data Uninit;
+ {sec_name_init = Section_data (Init, false);
+ sec_name_init_reloc = Section_data (Init_reloc, false);
+ sec_name_uninit = Section_data (Uninit, false);
+ sec_writable = true; sec_executable = false;
+ sec_access = Access_default};
+ "TDATA",
+ {sec_name_init = Section_data (Init, true);
+ sec_name_init_reloc = Section_data (Init_reloc, true);
+ sec_name_uninit = Section_data (Uninit, true);
sec_writable = true; sec_executable = false;
sec_access = Access_default};
"SDATA",
@@ -195,7 +201,7 @@ let get_attr_section loc attr =
(* Determine section for a variable definition *)
-let for_variable env loc id ty init =
+let for_variable env loc id ty init thrl =
let attr = Cutil.attributes_of_type env ty in
let readonly = List.mem C.AConst attr && not(List.mem C.AVolatile attr) in
let si =
@@ -214,7 +220,8 @@ let for_variable env loc id ty init =
let name =
if readonly
then if size <= !Clflags.option_small_const then "SCONST" else "CONST"
- else if size <= !Clflags.option_small_data then "SDATA" else "DATA" in
+ else if size <= !Clflags.option_small_data then "SDATA" else
+ if thrl then "TDATA" else "DATA" in
try
Hashtbl.find current_section_table name
with Not_found ->
diff --git a/common/Sections.mli b/common/Sections.mli
index 8ec98e40..6d1d9c69 100644
--- a/common/Sections.mli
+++ b/common/Sections.mli
@@ -24,7 +24,7 @@ type initialized =
type section_name =
| Section_text
- | Section_data of initialized
+ | Section_data of initialized * bool (* true = thread local? *)
| Section_small_data of initialized
| Section_const of initialized
| Section_small_const of initialized
@@ -52,7 +52,7 @@ val define_section:
-> ?writable:bool -> ?executable:bool -> ?access:access_mode -> unit -> unit
val use_section_for: AST.ident -> string -> bool
-val for_variable: Env.t -> C.location -> AST.ident -> C.typ -> initialized ->
+val for_variable: Env.t -> C.location -> AST.ident -> C.typ -> initialized -> bool ->
section_name * access_mode
val for_function: Env.t -> C.location -> AST.ident -> C.attributes -> section_name list
val for_stringlit: unit -> section_name
diff --git a/common/Switchaux.ml b/common/Switchaux.ml
index 47ded8ee..eb1ab8bc 100644
--- a/common/Switchaux.ml
+++ b/common/Switchaux.ml
@@ -81,6 +81,7 @@ let compile_switch_as_jumptable default cases minkey maxkey =
CTaction default)
let dense_enough (numcases: int) (minkey: Z.t) (maxkey: Z.t) =
+ (* DM Settings this to constant false disables jump tables *)
let span = Z.sub maxkey minkey in
assert (Z.ge span Z.zero);
let tree_size = Z.mul (Z.of_uint 4) (Z.of_uint numcases)
@@ -88,7 +89,7 @@ let dense_enough (numcases: int) (minkey: Z.t) (maxkey: Z.t) =
numcases >= 7 (* small jump tables are always less efficient *)
&& Z.le table_size tree_size
&& Z.lt span (Z.of_uint Sys.max_array_length)
-
+
let compile_switch modulus default table =
let (tbl, keys) = normalize_table table in
if ZSet.is_empty keys then CTaction default else begin
diff --git a/common/Values.v b/common/Values.v
index 891c9a88..87ebea00 100644
--- a/common/Values.v
+++ b/common/Values.v
@@ -21,6 +21,7 @@ Require Import Coqlib.
Require Import AST.
Require Import Integers.
Require Import Floats.
+Require Import Lia.
Definition block : Type := positive.
Definition eq_block := peq.
@@ -90,6 +91,27 @@ Definition has_type (v: val) (t: typ) : Prop :=
| _, _ => False
end.
+Definition has_type_b (v: val) (t: typ) :=
+ match v, t with
+ | Vundef, _ => true
+ | Vint _, Tint => true
+ | Vlong _, Tlong => true
+ | Vfloat _, Tfloat => true
+ | Vsingle _, Tsingle => true
+ | Vptr _ _, Tint => negb Archi.ptr64
+ | Vptr _ _, Tlong => Archi.ptr64
+ | (Vint _ | Vsingle _), Tany32 => true
+ | Vptr _ _, Tany32 => negb Archi.ptr64
+ | _, Tany64 => true
+ | _, _ => false
+ end.
+
+Lemma has_type_b_correct: forall v t,
+ has_type_b v t = true <-> has_type v t.
+Proof.
+ destruct v; destruct t; cbn; destruct Archi.ptr64; cbn; split; intros; auto; discriminate.
+Qed.
+
Fixpoint has_type_list (vl: list val) (tl: list typ) {struct vl} : Prop :=
match vl, tl with
| nil, nil => True
@@ -1471,6 +1493,60 @@ Proof.
assert (32 < Int.max_unsigned) by reflexivity. lia.
Qed.
+Theorem shrx1_shr:
+ forall x z,
+ shrx x (Vint (Int.repr 1)) = Some z ->
+ z = shr (add x (shru x (Vint (Int.repr 31)))) (Vint (Int.repr 1)).
+Proof.
+ intros. destruct x; simpl in H; try discriminate.
+ change (Int.ltu (Int.repr 1) (Int.repr 31)) with true in H; simpl in H.
+ inversion_clear H.
+ simpl.
+ change (Int.ltu (Int.repr 31) Int.iwordsize) with true; simpl.
+ change (Int.ltu (Int.repr 1) Int.iwordsize) with true; simpl.
+ f_equal.
+ rewrite Int.shrx1_shr by reflexivity.
+ reflexivity.
+Qed.
+
+Theorem shrx_shr_3:
+ forall n x z,
+ shrx x (Vint n) = Some z ->
+ z = (if Int.eq n Int.zero then x else
+ if Int.eq n Int.one
+ then shr (add x (shru x (Vint (Int.repr 31)))) (Vint Int.one)
+ else shr (add x (shru (shr x (Vint (Int.repr 31)))
+ (Vint (Int.sub (Int.repr 32) n))))
+ (Vint n)).
+Proof.
+ intros. destruct x; simpl in H; try discriminate.
+ destruct (Int.ltu n (Int.repr 31)) eqn:LT; inv H.
+ exploit Int.ltu_inv; eauto. change (Int.unsigned (Int.repr 31)) with 31; intros LT'.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+- subst n. unfold Int.shrx. rewrite Int.shl_zero. unfold Int.divs. change (Int.signed Int.one) with 1.
+ rewrite Z.quot_1_r. rewrite Int.repr_signed; auto.
+- predSpec Int.eq Int.eq_spec n Int.one.
+ * subst n. simpl.
+ change (Int.ltu (Int.repr 31) Int.iwordsize) with true. simpl.
+ change (Int.ltu Int.one Int.iwordsize) with true. simpl.
+ f_equal.
+ apply Int.shrx1_shr.
+ reflexivity.
+ * clear H0.
+ simpl. change (Int.ltu (Int.repr 31) Int.iwordsize) with true. simpl.
+ replace (Int.ltu (Int.sub (Int.repr 32) n) Int.iwordsize) with true. simpl.
+ replace (Int.ltu n Int.iwordsize) with true.
+ f_equal; apply Int.shrx_shr_2; assumption.
+ symmetry; apply zlt_true. change (Int.unsigned n < 32); lia.
+ symmetry; apply zlt_true. unfold Int.sub. change (Int.unsigned (Int.repr 32)) with 32.
+ assert (Int.unsigned n <> 0).
+ { red; intros; elim H.
+ rewrite <- (Int.repr_unsigned n), H0. auto. }
+ rewrite Int.unsigned_repr.
+ change (Int.unsigned Int.iwordsize) with 32; lia.
+ assert (32 < Int.max_unsigned) by reflexivity. lia.
+Qed.
+
Theorem or_rolm:
forall x n m1 m2,
or (rolm x n m1) (rolm x n m2) = rolm x n (Int.or m1 m2).
@@ -1730,6 +1806,58 @@ Proof.
assert (64 < Int.max_unsigned) by reflexivity. lia.
Qed.
+Theorem shrxl1_shrl:
+ forall x z,
+ shrxl x (Vint (Int.repr 1)) = Some z ->
+ z = shrl (addl x (shrlu x (Vint (Int.repr 63)))) (Vint (Int.repr 1)).
+Proof.
+ intros. destruct x; simpl in H; try discriminate.
+ change (Int.ltu (Int.repr 1) (Int.repr 63)) with true in H; simpl in H.
+ inversion_clear H.
+ simpl.
+ change (Int.ltu (Int.repr 63) Int64.iwordsize') with true; simpl.
+ change (Int.ltu (Int.repr 1) Int64.iwordsize') with true; simpl.
+ f_equal.
+ rewrite Int64.shrx'1_shr' by reflexivity.
+ reflexivity.
+Qed.
+
+Theorem shrxl_shrl_3:
+ forall n x z,
+ shrxl x (Vint n) = Some z ->
+ z = (if Int.eq n Int.zero then x else
+ if Int.eq n Int.one
+ then shrl (addl x (shrlu x (Vint (Int.repr 63)))) (Vint Int.one)
+ else shrl (addl x (shrlu (shrl x (Vint (Int.repr 63)))
+ (Vint (Int.sub (Int.repr 64) n))))
+ (Vint n)).
+Proof.
+ intros. destruct x; simpl in H; try discriminate.
+ destruct (Int.ltu n (Int.repr 63)) eqn:LT; inv H.
+ exploit Int.ltu_inv; eauto. change (Int.unsigned (Int.repr 63)) with 63; intros LT'.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+- subst n. unfold Int64.shrx'. rewrite Int64.shl'_zero. unfold Int64.divs. change (Int64.signed Int64.one) with 1.
+ rewrite Z.quot_1_r. rewrite Int64.repr_signed; auto.
+- predSpec Int.eq Int.eq_spec n Int.one.
+ * subst n. simpl.
+ change (Int.ltu (Int.repr 63) Int64.iwordsize') with true. simpl.
+ change (Int.ltu Int.one Int64.iwordsize') with true. simpl.
+ f_equal.
+ apply Int64.shrx'1_shr'.
+ reflexivity.
+ * clear H0.
+simpl. change (Int.ltu (Int.repr 63) Int64.iwordsize') with true. simpl.
+ replace (Int.ltu (Int.sub (Int.repr 64) n) Int64.iwordsize') with true. simpl.
+ replace (Int.ltu n Int64.iwordsize') with true.
+ f_equal; apply Int64.shrx'_shr_2; assumption.
+ symmetry; apply zlt_true. change (Int.unsigned n < 64); lia.
+ symmetry; apply zlt_true. unfold Int.sub. change (Int.unsigned (Int.repr 64)) with 64.
+ assert (Int.unsigned n <> 0). { red; intros; elim H. rewrite <- (Int.repr_unsigned n), H0. auto. }
+ rewrite Int.unsigned_repr.
+ change (Int.unsigned Int64.iwordsize') with 64; lia.
+ assert (64 < Int.max_unsigned) by reflexivity. lia.
+Qed.
+
Theorem negate_cmp_bool:
forall c x y, cmp_bool (negate_comparison c) x y = option_map negb (cmp_bool c x y).
Proof.
@@ -1888,6 +2016,20 @@ Proof.
destruct (Float.cmp Cgt f f0); destruct (Float.cmp Ceq f f0); auto.
Qed.
+Theorem swap_cmpf_bool:
+ forall c x y,
+ Val.cmpf_bool (swap_comparison c) x y = Val.cmpf_bool c y x.
+Proof.
+ destruct x; destruct y; simpl; auto. rewrite Float.cmp_swap. auto.
+Qed.
+
+Theorem swap_cmpfs_bool:
+ forall c x y,
+ Val.cmpfs_bool (swap_comparison c) x y = Val.cmpfs_bool c y x.
+Proof.
+ destruct x; destruct y; simpl; auto. rewrite Float32.cmp_swap. auto.
+Qed.
+
Theorem cmp_ne_0_optbool:
forall ob, cmp Cne (of_optbool ob) (Vint Int.zero) = of_optbool ob.
Proof.
@@ -2508,6 +2650,55 @@ Qed.
End VAL_INJ_OPS.
+(* Specializations of cmpu_bool, cmpu, cmplu_bool, and cmplu for maximal pointer validity *)
+
+Definition mxcmpu_bool cmp v1 v2: option bool :=
+ cmpu_bool (fun _ _ => true) cmp v1 v2.
+
+Lemma mxcmpu_bool_correct vptr (cmp: comparison) (v1 v2: val) b:
+ cmpu_bool vptr cmp v1 v2 = Some b
+ -> mxcmpu_bool cmp v1 v2 = Some b.
+Proof.
+ intros; eapply cmpu_bool_lessdef; (econstructor 1 || eauto).
+Qed.
+
+Definition mxcmpu cmp v1 v2 := of_optbool (mxcmpu_bool cmp v1 v2).
+
+Lemma mxcmpu_correct vptr (cmp: comparison) (v1 v2: val):
+ lessdef (cmpu vptr cmp v1 v2) (mxcmpu cmp v1 v2).
+Proof.
+ unfold cmpu, mxcmpu.
+ remember (cmpu_bool _ cmp v1 v2) as ob.
+ destruct ob; simpl.
+ - erewrite mxcmpu_bool_correct; eauto.
+ econstructor.
+ - econstructor.
+Qed.
+
+Definition mxcmplu_bool (cmp: comparison) (v1 v2: val)
+ := (cmplu_bool (fun _ _ => true) cmp v1 v2).
+
+Lemma mxcmplu_bool_correct vptr (cmp: comparison) (v1 v2: val) b:
+ (cmplu_bool vptr cmp v1 v2) = Some b
+ -> (mxcmplu_bool cmp v1 v2) = Some b.
+Proof.
+ intros; eapply cmplu_bool_lessdef; (econstructor 1 || eauto).
+Qed.
+
+Definition mxcmplu cmp v1 v2 := of_optbool (mxcmplu_bool cmp v1 v2).
+
+Lemma mxcmplu_correct vptr (cmp: comparison) (v1 v2: val):
+ lessdef (maketotal (cmplu vptr cmp v1 v2))
+ (mxcmplu cmp v1 v2).
+Proof.
+ unfold cmplu, mxcmplu.
+ remember (cmplu_bool _ cmp v1 v2) as ob.
+ destruct ob as [b|]; simpl.
+ - erewrite mxcmplu_bool_correct; eauto.
+ simpl. econstructor.
+ - econstructor.
+Qed.
+
End Val.
Notation meminj := Val.meminj.
@@ -2601,3 +2792,24 @@ Proof.
unfold compose_meminj; rewrite H1; rewrite H3; eauto.
rewrite Ptrofs.add_assoc. decEq. unfold Ptrofs.add. apply Ptrofs.eqm_samerepr. auto with ints.
Qed.
+
+
+(** Particular cases of extensionality lemma *)
+
+Lemma cmpu_bool_valid_pointer_eq vptr1 vptr2 c v1 v2:
+ (forall (b : block) (z : Z), vptr1 b z = vptr2 b z) ->
+ Val.cmpu_bool vptr1 c v1 v2 = Val.cmpu_bool vptr2 c v1 v2.
+Proof.
+ intros EQ; unfold Val.cmpu_bool; destruct v1; try congruence;
+ destruct v2; try congruence;
+ rewrite !EQ; auto.
+Qed.
+
+Lemma cmplu_bool_valid_pointer_eq vptr1 vptr2 c v1 v2:
+ (forall (b : block) (z : Z), vptr1 b z = vptr2 b z) ->
+ Val.cmplu_bool vptr1 c v1 v2 = Val.cmplu_bool vptr2 c v1 v2.
+Proof.
+ intros EQ; unfold Val.cmplu_bool; destruct v1; try congruence;
+ destruct v2; try congruence;
+ rewrite !EQ; auto.
+Qed.
diff --git a/compcert_build_env.dockerfile b/compcert_build_env.dockerfile
new file mode 100644
index 00000000..de339e55
--- /dev/null
+++ b/compcert_build_env.dockerfile
@@ -0,0 +1,6 @@
+FROM debian:stable-20210408
+LABEL maintainer="David.Monniaux@univ-grenoble-alpes.fr"
+RUN apt-get update && apt-get upgrade -y && apt-get -y install gcc-powerpc-linux-gnu gcc-powerpc64-linux-gnu gcc-riscv64-linux-gnu gcc-arm-linux-gnueabi gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu qemu-user opam
+RUN adduser --gecos "Application user" appuser
+USER appuser
+RUN opam init --disable-sandboxing && opam switch create 4.11.2+flambda && eval $(opam config env) && opam pin -y add -n coq 8.12.2 && opam install -y menhir ocamlbuild coq
diff --git a/compcert_kvx.dockerfile b/compcert_kvx.dockerfile
new file mode 100644
index 00000000..21b15308
--- /dev/null
+++ b/compcert_kvx.dockerfile
@@ -0,0 +1,19 @@
+FROM compcert_build_env
+USER root
+RUN mkdir /opt/CompCert && chown appuser:appuser /opt/CompCert
+COPY --chown=appuser:appuser . CompCert
+USER appuser
+
+RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_aarch64.sh && make && make install
+
+RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_arm.sh && make && make install
+
+RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_armhf.sh && make && make install
+
+# RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_ia32.sh && make && make install
+
+RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_x86_64.sh && make && make install
+
+# RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_rv32.sh && make && make install
+
+RUN eval $(opam config env) && cd CompCert && git clean -dfx && git reset --hard && ./config_rv64.sh && make && make install
diff --git a/compcert_kvx_pruned.dockerfile b/compcert_kvx_pruned.dockerfile
new file mode 100644
index 00000000..c122cb83
--- /dev/null
+++ b/compcert_kvx_pruned.dockerfile
@@ -0,0 +1,5 @@
+FROM compcert_kvx
+USER root
+RUN apt-get -y install gcc && apt-get -y remove opam && apt-get -y autoremove
+RUN rm -rf /home/appuser/.opam /home/appuser/CompCert
+USER appuser
diff --git a/config_aarch64.sh b/config_aarch64.sh
new file mode 100755
index 00000000..ded267bf
--- /dev/null
+++ b/config_aarch64.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh aarch64-linux --toolprefix aarch64-linux-gnu- "$@"
diff --git a/config_arm.sh b/config_arm.sh
new file mode 100755
index 00000000..1861e029
--- /dev/null
+++ b/config_arm.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh arm-linux --toolprefix arm-linux-gnueabi- "$@"
diff --git a/config_armhf.sh b/config_armhf.sh
new file mode 100755
index 00000000..8a1302bd
--- /dev/null
+++ b/config_armhf.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh arm-eabihf --toolprefix arm-linux-gnueabihf- "$@"
diff --git a/config_ia32.sh b/config_ia32.sh
new file mode 100755
index 00000000..b40f2b39
--- /dev/null
+++ b/config_ia32.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh ia32-linux "$@"
diff --git a/config_kvx.sh b/config_kvx.sh
new file mode 100755
index 00000000..9040c23b
--- /dev/null
+++ b/config_kvx.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh kvx-cos "$@"
diff --git a/config_kvx_elf.sh b/config_kvx_elf.sh
new file mode 100755
index 00000000..f1430417
--- /dev/null
+++ b/config_kvx_elf.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh kvx-elf "$@"
diff --git a/config_macos_x86_64.sh b/config_macos_x86_64.sh
new file mode 100755
index 00000000..9d5b3f5e
--- /dev/null
+++ b/config_macos_x86_64.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh x86_64-macos "$@"
diff --git a/config_ppc.sh b/config_ppc.sh
new file mode 100755
index 00000000..d597cda5
--- /dev/null
+++ b/config_ppc.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh ppc-linux --toolprefix powerpc-linux-gnu- "$@"
diff --git a/config_ppc64.sh b/config_ppc64.sh
new file mode 100755
index 00000000..df31c18f
--- /dev/null
+++ b/config_ppc64.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh ppc64-linux --toolprefix powerpc64-linux-gnu- "$@"
diff --git a/config_rv32.sh b/config_rv32.sh
new file mode 100755
index 00000000..654cacfa
--- /dev/null
+++ b/config_rv32.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh rv32-linux --toolprefix riscv64-unknown-elf- "$@"
diff --git a/config_rv64.sh b/config_rv64.sh
new file mode 100755
index 00000000..0698c2ff
--- /dev/null
+++ b/config_rv64.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh rv64-linux --toolprefix riscv64-linux-gnu- "$@"
diff --git a/config_simple.sh b/config_simple.sh
new file mode 100755
index 00000000..52b7d1a6
--- /dev/null
+++ b/config_simple.sh
@@ -0,0 +1,11 @@
+arch=$1
+shift
+version=`git rev-parse --short HEAD`
+branch=`git rev-parse --abbrev-ref HEAD`
+date=`date +%Y-%m-%d`
+
+if test "x$CCOMP_INSTALL_PREFIX" = "x" ;
+then CCOMP_INSTALL_PREFIX=/opt/CompCert ;
+fi
+
+./configure --prefix ${CCOMP_INSTALL_PREFIX}/${branch}/${date}_${version}/$arch "$@" $arch
diff --git a/config_x86_64.sh b/config_x86_64.sh
new file mode 100755
index 00000000..b18ec95b
--- /dev/null
+++ b/config_x86_64.sh
@@ -0,0 +1 @@
+exec ./config_simple.sh x86_64-linux "$@"
diff --git a/configure b/configure
index 10f60262..76c46343 100755
--- a/configure
+++ b/configure
@@ -58,6 +58,9 @@ Supported targets:
x86_64-cygwin (x86 64 bits, Cygwin environment under Windows)
rv32-linux (RISC-V 32 bits, Linux)
rv64-linux (RISC-V 64 bits, Linux)
+ kvx-mbr (Kalray KV3, bare runtime)
+ kvx-elf (Kalray KV3, ELF)
+ kvx-cos (Kalray KV3, ClusterOS)
aarch64-linux (AArch64, i.e. ARMv8 in 64-bit mode, Linux)
aarch64-macos (AArch64, i.e. Apple silicon, MacOS)
manual (edit configuration file by hand)
@@ -190,6 +193,8 @@ case "$target" in
arch="riscV"; model="32"; endianness="little"; bitsize=32;;
rv64-*)
arch="riscV"; model="64"; endianness="little"; bitsize=64;;
+ kvx-*)
+ arch="kvx"; model="64"; endianness="little"; bitsize=64;;
aarch64-*|arm64-*)
arch="aarch64"; model="default"; endianness="little"; bitsize=64;;
manual)
@@ -402,6 +407,38 @@ if test "$arch" = "riscV"; then
fi
#
+# KVX Target Configuration
+#
+if test "$arch" = "kvx"; then
+ #model_options="-march=rv64imafd -mabi=lp64d"
+ # FIXME - maybe later add it for NodeOS & cie
+ #model_options=-m64
+ model_options=
+ abi="standard"
+ if test "$target" = "mbr";
+ then os="mbr";
+ elif test "$target" = "cos";
+ then os="cos";
+ elif test "$target" = "elf";
+ then os="elf";
+ else
+ echo "Unknown KVX backend"
+ exit 1
+ fi
+ osupper=`echo $os|tr a-z A-Z`
+ k1base="kvx-$os"
+ casm="$k1base-as"
+ casm_options="$model_options"
+ cc="$k1base-gcc $model_options"
+ clinker="$k1base-gcc"
+ clinker_options="$model_options -L$libdir -Wl,-rpath=$libdir"
+ cprepro="$k1base-gcc"
+ cprepro_options="$model_options -D __KVX_${osupper}__ -std=c99 -E -include ccomp_kvx_fixes.h"
+ libmath="-lm"
+ system="linux"
+fi
+
+#
# AArch64 (ARMv8 64 bits) Target Configuration
#
if test "$arch" = "aarch64"; then
@@ -523,9 +560,9 @@ esac
echo "Testing OCaml... " | tr -d '\n'
ocaml_ver=`ocamlc -version 2>/dev/null`
case "$ocaml_ver" in
- 4.00.*|4.01.*| 4.02.*|4.03.*|4.04.*)
+ 4.00.*|4.01.*| 4.02.*|4.03.*|4.04.*|4.05.*)
echo "version $ocaml_ver -- UNSUPPORTED"
- echo "Error: CompCert requires OCaml version 4.05 or later."
+ echo "Error: CompCert requires OCaml version 4.06 or later."
missingtools=true;;
4.*)
echo "version $ocaml_ver -- good!";;
@@ -650,6 +687,7 @@ HAS_STANDARD_HEADERS=$has_standard_headers
INSTALL_COQDEV=$install_coqdev
LIBMATH=$libmath
MODEL=$model
+OS=${os:-unspecified}
SYSTEM=$system
RESPONSEFILE=$responsefile
LIBRARY_FLOCQ=$library_Flocq
@@ -759,6 +797,42 @@ LIBRARY_MENHIRLIB=local # external
EOF
fi
+if [ "$arch" = "aarch64" ]; then # for aarch64 scheduling
+cat >> Makefile.config <<EOF
+ARCHDIRS=$arch scheduling/abstractbb scheduling/postpass_lib
+BACKENDLIB=Machblock.v Machblockgen.v Machblockgenproof.v IterList.v \\
+ Asmblock.v Asmblockgen.v Asmblockgenproof0.v Asmblockgenproof1.v Asmblockgenproof.v Asm.v Asmblockprops.v\\
+ ForwardSimulationBlock.v PostpassScheduling.v PostpassSchedulingproof.v\\
+ Asmblockdeps.v\\
+ AbstractBasicBlocksDef.v SeqSimuTheory.v ImpSimuTest.v Parallelizability.v\\
+ ImpConfig.v ImpCore.v ImpExtern.v ImpHCons.v ImpIO.v ImpLoops.v ImpMonads.v ImpPrelude.v
+ # TODO: UPDATE THIS
+ # DecBoolOps.v Chunks.v Peephole.v ExtValues.v ExtFloats.v
+EXTRA_EXTRACTION= Asmgen.Asmgen_expand.loadimm32 Asmgen.Asmgen_expand.addimm64 Asmgen.Asmgen_expand.storeptr
+EOF
+fi
+
+if [ "$arch" = "kvx" ]; then
+cat >> Makefile.config <<EOF
+ARCHDIRS=$arch scheduling/abstractbb scheduling/postpass_lib
+EXECUTE=kvx-cluster --syscall=libstd_scalls.so --
+CFLAGS= -D __KVX_COS__
+SIMU=kvx-cluster --
+BACKENDLIB=Machblock.v Machblockgen.v Machblockgenproof.v\\
+ Asmblock.v Asmblockgen.v Asmblockgenproof0.v Asmblockgenproof1.v Asmblockgenproof.v Asmvliw.v Asmblockprops.v\\
+ ForwardSimulationBlock.v PostpassScheduling.v PostpassSchedulingproof.v\\
+ Asmblockdeps.v DecBoolOps.v Chunks.v Peephole.v ExtValues.v ExtFloats.v\\
+ AbstractBasicBlocksDef.v SeqSimuTheory.v ImpSimuTest.v Parallelizability.v
+EOF
+fi
+
+if [ "$arch" = "riscV" ] ; then
+cat >> Makefile.config <<EOF
+EXTRA_EXTRACTION=Asm.ireg_eq Asm.ireg0_eq
+BACKENDLIB=Asmgenproof0.v Asmgenproof1.v ExtValues.v
+EOF
+fi
+
#
# Generate Merlin and CoqProject files to simplify development
#
@@ -845,5 +919,4 @@ cat <<EOF
Coq development will not be installed
EOF
fi
-
fi
diff --git a/cparser/C.mli b/cparser/C.mli
index 763a9277..cccf744b 100644
--- a/cparser/C.mli
+++ b/cparser/C.mli
@@ -87,8 +87,11 @@ type attributes = attribute list
type storage =
| Storage_default (* used for toplevel names without explicit storage *)
+ | Storage_thread_local
| Storage_extern
| Storage_static
+ | Storage_thread_local_extern
+ | Storage_thread_local_static
| Storage_auto (* used for block-scoped names without explicit storage *)
| Storage_register
diff --git a/cparser/Cabs.v b/cparser/Cabs.v
index accb95a0..ab908be3 100644
--- a/cparser/Cabs.v
+++ b/cparser/Cabs.v
@@ -55,7 +55,7 @@ Inductive typeSpecifier := (* Merge all specifiers into one type *)
| Tenum : option string -> option (list (string * option expression * loc)) -> list attribute -> typeSpecifier
with storage :=
- AUTO | STATIC | EXTERN | REGISTER | TYPEDEF
+ AUTO | STATIC | EXTERN | REGISTER | TYPEDEF | THREAD_LOCAL
with cvspec :=
| CV_CONST | CV_VOLATILE | CV_RESTRICT
diff --git a/cparser/Ceval.ml b/cparser/Ceval.ml
index 14f61e06..b216ebc8 100644
--- a/cparser/Ceval.ml
+++ b/cparser/Ceval.ml
@@ -355,7 +355,9 @@ and is_constant_lval env e =
begin match Env.find_ident env id with
| Env.II_ident(sto, _) ->
begin match sto with
- | Storage_default | Storage_extern | Storage_static -> true
+ | Storage_default | Storage_extern | Storage_static
+ | Storage_thread_local | Storage_thread_local_extern | Storage_thread_local_static
+ -> true
| Storage_auto | Storage_register -> false
end
| Env.II_enum _ -> false (* should not happen *)
diff --git a/cparser/Cleanup.ml b/cparser/Cleanup.ml
index 62b00e04..74959cbb 100644
--- a/cparser/Cleanup.ml
+++ b/cparser/Cleanup.ml
@@ -127,14 +127,14 @@ let add_enum e =
*)
let visible_decl (sto, id, ty, init) =
- sto = Storage_default &&
+ (sto = Storage_default || sto = Storage_thread_local) &&
match ty with TFun _ -> false | _ -> true
let visible_fundef f =
match f.fd_storage with
- | Storage_default -> not f.fd_inline
- | Storage_extern -> true
- | Storage_static -> false
+ | Storage_default | Storage_thread_local -> not f.fd_inline
+ | Storage_extern | Storage_thread_local_extern -> true
+ | Storage_static | Storage_thread_local_static -> false
| Storage_auto | Storage_register -> assert false
let rec add_init_globdecls accu = function
diff --git a/cparser/Cprint.ml b/cparser/Cprint.ml
index 93377989..dddc8f73 100644
--- a/cparser/Cprint.ml
+++ b/cparser/Cprint.ml
@@ -362,6 +362,9 @@ let storage pp = function
| Storage_default -> ()
| Storage_extern -> fprintf pp "extern "
| Storage_static -> fprintf pp "static "
+ | Storage_thread_local -> fprintf pp "_Thread_local"
+ | Storage_thread_local_extern -> fprintf pp "extern _Thread_local"
+ | Storage_thread_local_static -> fprintf pp "static _Thread_local"
| Storage_auto -> () (* used only in blocks, where it can be omitted *)
| Storage_register -> fprintf pp "register "
diff --git a/cparser/Elab.ml b/cparser/Elab.ml
index c2c7f943..4fae584e 100644
--- a/cparser/Elab.ml
+++ b/cparser/Elab.ml
@@ -154,6 +154,9 @@ let name_of_storage_class = function
| Storage_default -> "<default>"
| Storage_extern -> "'extern'"
| Storage_static -> "'static'"
+ | Storage_thread_local -> "'_Thread_local'"
+ | Storage_thread_local_extern -> "'_Thread_local extern'"
+ | Storage_thread_local_static -> "'_Thread_local static'"
| Storage_auto -> "'auto'"
| Storage_register -> "'register'"
@@ -179,15 +182,29 @@ let combine_toplevel_definitions loc env s old_sto old_ty sto ty =
| Storage_static,Storage_static
| Storage_extern,Storage_extern
| Storage_default,Storage_default -> sto
- | _,Storage_static ->
+ | Storage_thread_local_static,Storage_thread_local_static
+ | Storage_thread_local_extern,Storage_thread_local_extern
+ | Storage_thread_local,Storage_thread_local -> sto
+ | _,Storage_static | _,Storage_thread_local_static ->
error loc "static declaration of '%s' follows non-static declaration" s;
sto
| Storage_static,_ -> Storage_static (* Static stays static *)
- | Storage_extern,_ -> if is_function_type env new_ty then Storage_extern else sto
+ | Storage_thread_local_static,_ -> Storage_thread_local_static (* Thread-local static stays static *)
+ | (Storage_extern|Storage_thread_local_extern),_ -> if is_function_type env new_ty then Storage_extern else sto
| Storage_default,Storage_extern ->
if is_global_defined s && is_function_type env ty then
warning loc Extern_after_definition "this extern declaration follows a non-extern definition and is ignored";
Storage_extern
+ | Storage_thread_local,Storage_thread_local_extern ->
+ if is_global_defined s && is_function_type env ty then
+ warning loc Extern_after_definition "this extern declaration follows a non-extern definition and is ignored";
+ Storage_extern
+ | Storage_thread_local, Storage_default ->
+ error loc "Non thread-local declaration follows thread-local";
+ sto
+ | Storage_default, (Storage_thread_local|Storage_thread_local_extern) ->
+ error loc "Thread-local declaration follows non thread-local";
+ sto
| _,Storage_extern -> old_sto
(* "auto" and "register" don't appear in toplevel definitions.
Normally this was checked earlier. Generate error message
@@ -655,13 +672,26 @@ let rec elab_specifier ?(only = false) loc env specifier =
restrict := cv = CV_RESTRICT;
attr := add_attributes (elab_cvspec env cv) !attr
| SpecStorage st ->
- if !sto <> Storage_default && st <> TYPEDEF then
+ if !sto <> Storage_default && st <> TYPEDEF && st <> THREAD_LOCAL then
error loc "multiple storage classes in declaration specifier";
begin match st with
| AUTO -> sto := Storage_auto
| STATIC -> sto := Storage_static
| EXTERN -> sto := Storage_extern
| REGISTER -> sto := Storage_register
+ | THREAD_LOCAL ->
+ sto := (match !sto with
+ | Storage_static | Storage_thread_local_static ->
+ Storage_thread_local_static
+ | Storage_extern | Storage_thread_local_extern ->
+ Storage_thread_local_extern
+ | Storage_default | Storage_thread_local ->
+ Storage_thread_local
+ | Storage_auto|Storage_register ->
+ error loc "_Thread_local on auto or register variable";
+ !sto
+ )
+
| TYPEDEF ->
if !typedef then
error loc "multiple uses of 'typedef'";
diff --git a/cparser/Lexer.mll b/cparser/Lexer.mll
index 42980d30..7a2974eb 100644
--- a/cparser/Lexer.mll
+++ b/cparser/Lexer.mll
@@ -74,6 +74,7 @@ let () =
("goto", fun loc -> GOTO loc);
("if", fun loc -> IF loc);
("inline", fun loc -> INLINE loc);
+ ("_Thread_local", fun loc -> THREAD_LOCAL loc);
("_Noreturn", fun loc -> NORETURN loc);
("int", fun loc -> INT loc);
("long", fun loc -> LONG loc);
@@ -545,6 +546,7 @@ and singleline_comment = parse
| Pre_parser.IF loc -> loop (Parser.IF_ loc)
| Pre_parser.INC loc -> loop (Parser.INC loc)
| Pre_parser.INLINE loc -> loop (Parser.INLINE loc)
+ | Pre_parser.THREAD_LOCAL loc -> loop (Parser.THREAD_LOCAL loc)
| Pre_parser.INT loc -> loop (Parser.INT loc)
| Pre_parser.LBRACE loc -> loop (Parser.LBRACE loc)
| Pre_parser.LBRACK loc -> loop (Parser.LBRACK loc)
diff --git a/cparser/Machine.ml b/cparser/Machine.ml
index c47ec594..4ce63970 100644
--- a/cparser/Machine.ml
+++ b/cparser/Machine.ml
@@ -19,6 +19,7 @@
type struct_passing_style =
| SP_ref_callee (* by reference, callee takes copy *)
| SP_ref_caller (* by reference, caller takes copy *)
+ | SP_value32_ref_callee (* by value if <= 32 bits, by ref_callee otherwise *)
| SP_split_args (* by value, as a sequence of ints *)
type struct_return_style =
@@ -61,6 +62,7 @@ type t = {
supports_unaligned_accesses: bool;
struct_passing_style: struct_passing_style;
struct_return_style : struct_return_style;
+ has_non_trapping_loads : bool;
}
let ilp32ll64 = {
@@ -96,6 +98,7 @@ let ilp32ll64 = {
supports_unaligned_accesses = false;
struct_passing_style = SP_ref_callee;
struct_return_style = SR_ref;
+ has_non_trapping_loads = false;
}
let i32lpll64 = {
@@ -131,6 +134,7 @@ let i32lpll64 = {
supports_unaligned_accesses = false;
struct_passing_style = SP_ref_callee;
struct_return_style = SR_ref;
+ has_non_trapping_loads = false;
}
let il32pll64 = {
@@ -166,6 +170,7 @@ let il32pll64 = {
supports_unaligned_accesses = false;
struct_passing_style = SP_ref_callee;
struct_return_style = SR_ref;
+ has_non_trapping_loads = false;
}
(* Canned configurations for some ABIs *)
@@ -238,6 +243,52 @@ let rv64 =
struct_passing_style = SP_ref_callee; (* Wrong *)
struct_return_style = SR_ref } (* to check *)
+let kvxbase =
+ { name = "kvx";
+ char_signed = true;
+ wchar_signed = true;
+ sizeof_ptr = 8;
+ sizeof_short = 2;
+ sizeof_int = 4;
+ sizeof_long = 8;
+ sizeof_longlong = 8;
+ sizeof_float = 4;
+ sizeof_double = 8;
+ sizeof_longdouble = 8;
+ sizeof_void = None; (* What is this for ? *)
+ sizeof_fun = None; (* What is this for ? *)
+ sizeof_wchar = 4;
+ sizeof_size_t = 8;
+ sizeof_ptrdiff_t = 8;
+ sizeof_intreg = 8; (* What is this for ? *)
+ alignof_ptr = 8;
+ alignof_short = 2;
+ alignof_int = 4;
+ alignof_long = 8;
+ alignof_longlong = 8;
+ alignof_float = 4;
+ alignof_double = 8;
+ alignof_longdouble = 8;
+ alignof_void = None; (* what is this for ? *)
+ alignof_fun = None; (* what is this for ? *)
+ bigendian = false;
+ bitfields_msb_first = false; (* TO CHECK *)
+ supports_unaligned_accesses = true;
+ struct_passing_style = SP_value32_ref_callee;
+ struct_return_style = SR_int1to4;
+ has_non_trapping_loads = false;
+}
+
+let kvxcos =
+ { kvxbase with has_non_trapping_loads = true;
+}
+
+let kvxmbr =
+ { kvxbase with has_non_trapping_loads = true;
+}
+
+let kvxelf = kvxmbr
+
let aarch64 =
{ i32lpll64 with name = "aarch64";
struct_passing_style = SP_ref_callee; (* Wrong *)
@@ -292,6 +343,7 @@ let undef = {
supports_unaligned_accesses = false;
struct_passing_style = SP_ref_callee;
struct_return_style = SR_ref;
+ has_non_trapping_loads = false;
}
(* The current configuration. Must be initialized before use. *)
diff --git a/cparser/Machine.mli b/cparser/Machine.mli
index f9d347b9..9e6063ba 100644
--- a/cparser/Machine.mli
+++ b/cparser/Machine.mli
@@ -18,6 +18,7 @@
type struct_passing_style =
| SP_ref_callee (* by reference, callee takes copy *)
| SP_ref_caller (* by reference, caller takes copy *)
+ | SP_value32_ref_callee (* by value if <= 32 bits, by ref_callee otherwise *)
| SP_split_args (* by value, as a sequence of ints *)
type struct_return_style =
@@ -60,6 +61,7 @@ type t = {
supports_unaligned_accesses: bool;
struct_passing_style: struct_passing_style;
struct_return_style: struct_return_style;
+ has_non_trapping_loads: bool;
}
(* The current configuration *)
@@ -87,6 +89,9 @@ val arm_littleendian : t
val arm_bigendian : t
val rv32 : t
val rv64 : t
+val kvxmbr : t
+val kvxcos : t
+val kvxelf : t
val aarch64 : t
val aarch64_apple : t
diff --git a/cparser/Parser.vy b/cparser/Parser.vy
index f1abe3d9..6c39719c 100644
--- a/cparser/Parser.vy
+++ b/cparser/Parser.vy
@@ -33,7 +33,7 @@ Require Cabs.
LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
%token<Cabs.loc> LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE DOT COMMA
- SEMICOLON ELLIPSIS TYPEDEF EXTERN STATIC RESTRICT AUTO REGISTER INLINE
+ SEMICOLON ELLIPSIS TYPEDEF EXTERN STATIC RESTRICT AUTO REGISTER INLINE THREAD_LOCAL
NORETURN CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
STRUCT UNION ENUM UNDERSCORE_BOOL PACKED ALIGNAS ATTRIBUTE ASM
@@ -403,6 +403,8 @@ storage_class_specifier:
{ (Cabs.AUTO, loc) }
| loc = REGISTER
{ (Cabs.REGISTER, loc) }
+| loc = THREAD_LOCAL
+ { (Cabs.THREAD_LOCAL, loc) }
(* 6.7.2 *)
type_specifier:
diff --git a/cparser/Rename.ml b/cparser/Rename.ml
index 96424bf8..e1b3537e 100644
--- a/cparser/Rename.ml
+++ b/cparser/Rename.ml
@@ -258,13 +258,16 @@ let rec reserve_public env = function
match dcl.gdesc with
| Gdecl(sto, id, _, _) ->
begin match sto with
- | Storage_default | Storage_extern -> enter_public env id
+ | Storage_default | Storage_thread_local
+ | Storage_extern | Storage_thread_local_extern ->
+ enter_public env id
| Storage_static -> env
| _ -> assert false
end
| Gfundef f ->
begin match f.fd_storage with
- | Storage_default | Storage_extern -> enter_public env f.fd_name
+ | Storage_default | Storage_extern
+ -> enter_public env f.fd_name
| Storage_static -> env
| _ -> assert false
end
diff --git a/cparser/StructPassing.ml b/cparser/StructPassing.ml
index f1f481d0..629d7bc3 100644
--- a/cparser/StructPassing.ml
+++ b/cparser/StructPassing.ml
@@ -69,7 +69,16 @@ let classify_param env ty =
match !struct_passing_style with
| SP_ref_callee -> Param_unchanged
| SP_ref_caller -> Param_ref_caller
- | _ ->
+ | SP_value32_ref_callee ->
+ (match sizeof env ty, alignof env ty with
+ | Some sz, Some al ->
+ if (sz <= 4) then
+ Param_flattened ((sz+3)/4, sz, al) (* FIXME - why (sz+3)/4 ? *)
+ else
+ Param_unchanged
+ | _, _ -> Param_unchanged (* when parsing prototype with incomplete structure definition *)
+ )
+ | SP_split_args ->
match sizeof env ty, alignof env ty with
| Some sz, Some al ->
Param_flattened ((sz + 3) / 4, sz, al)
diff --git a/cparser/deLexer.ml b/cparser/deLexer.ml
index e3ab3291..3f84d847 100644
--- a/cparser/deLexer.ml
+++ b/cparser/deLexer.ml
@@ -31,6 +31,7 @@ let delex (symbol : string) : string =
| "BUILTIN_VA_ARG" -> "__builtin_va_arg"
| "CONST" -> "const"
| "INLINE" -> "inline"
+ | "THREAD_LOCAL" -> "_Thread_local"
| "PACKED" -> "__packed__"
| "RESTRICT" -> "restrict"
| "SIGNED" -> "signed"
diff --git a/cparser/pre_parser.mly b/cparser/pre_parser.mly
index cffbd192..f99fef62 100644
--- a/cparser/pre_parser.mly
+++ b/cparser/pre_parser.mly
@@ -55,7 +55,7 @@
COLON AND MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN SUB_ASSIGN LEFT_ASSIGN
RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN LPAREN RPAREN LBRACK RBRACK
LBRACE RBRACE DOT COMMA SEMICOLON ELLIPSIS TYPEDEF EXTERN STATIC RESTRICT
- AUTO REGISTER INLINE NORETURN CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE
+ AUTO REGISTER INLINE THREAD_LOCAL NORETURN CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE
UNDERSCORE_BOOL CONST VOLATILE VOID STRUCT UNION ENUM CASE DEFAULT IF ELSE
SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN BUILTIN_VA_ARG ALIGNOF
ATTRIBUTE ALIGNAS PACKED ASM BUILTIN_OFFSETOF STATIC_ASSERT
@@ -432,6 +432,7 @@ storage_class_specifier_no_typedef:
| STATIC
| AUTO
| REGISTER
+| THREAD_LOCAL
{}
(* [declaration_specifier_no_type] matches declaration specifiers
diff --git a/doc/index-kvx.html b/doc/index-kvx.html
new file mode 100644
index 00000000..daa4cdc4
--- /dev/null
+++ b/doc/index-kvx.html
@@ -0,0 +1,429 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<HTML>
+<HEAD>
+<TITLE>The CompCert verified compiler</TITLE>
+<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
+
+<style type="text/css">
+body {
+ color: black; background: white;
+ margin-left: 5%; margin-right: 5%;
+}
+h2 { margin-left: -5%;}
+h3 { margin-left: -3%; }
+h1,h2,h3 { font-family: sans-serif; }
+hr { margin-left: -5%; margin-right:-5%; }
+a:visited {color : #416DFF; text-decoration : none; font-weight : bold}
+a:link {color : #416DFF; text-decoration : none; font-weight : bold}
+a:hover {color : Red; text-decoration : underline; }
+a:active {color : Red; text-decoration : underline; }
+</style>
+
+</HEAD>
+<BODY>
+
+<font color=gray>
+<H1 align="center">The CompCert verified compiler</H1>
+<H2 align="center">Commented Coq development</H2>
+<H3 align="center">Version 3.8, 2020-11-16</H3>
+</font>
+<H3 align="center">PATCHED for the Kalray MPPA-KVX VLIW CORE<!--@DATE@--></H3>
+
+<H2>Introduction</H2>
+
+<p>This web page is a patched version of the table of contents of the official CompCert sources documentation,
+ as given on <A HREF="http://compcert.org/doc/index.html">the CompCert Web site</A>.
+ The unmodified parts of this table appear in <font color=gray>gray</font>.
+ <br>
+ <br>
+ A high-level view of this CompCert backend is provided by this OOPSLA'20 paper (of Six, Boulm&eacute; and Monniaux):
+ <div><a href=https://hal.archives-ouvertes.fr/hal-02185883>Certified and Efficient Instruction Scheduling. Application to Interlocked VLIW Processors.</a></div>
+ <br>
+ See also the <tt>README.md</tt> of our <a href=https://gricad-gitlab.univ-grenoble-alpes.fr/certicompil/compcert-kvx>GitLab public repository</a>.
+</p>
+
+<font color=gray>
+<H2>Table of contents</H2>
+
+<H3>General-purpose libraries, data structures and algorithms</H3>
+
+<UL>
+<LI> <A HREF="html/compcert.lib.Coqlib.html">Coqlib</A>: addendum to the Coq standard library.
+<LI> <A HREF="html/compcert.lib.Maps.html">Maps</A>: finite maps.
+<LI> <A HREF="html/compcert.lib.Integers.html">Integers</A>: machine integers.
+<LI> <A HREF="html/compcert.lib.Floats.html">Floats</A>: machine floating-point numbers.
+<LI> <A HREF="html/compcert.lib.Iteration.html">Iteration</A>: various forms of "while" loops.
+<LI> <A HREF="html/compcert.lib.Ordered.html">Ordered</A>: construction of
+ordered types.
+<LI> <A HREF="html/compcert.lib.Lattice.html">Lattice</A>: construction of
+semi-lattices.
+<LI> <A HREF="html/compcert.backend.Kildall.html">Kildall</A>: resolution of dataflow
+inequations by fixpoint iteration.
+<LI> <A HREF="html/compcert.lib.UnionFind.html">UnionFind</A>: a persistent union-find data structure.
+<LI> <A HREF="html/compcert.lib.Postorder.html">Postorder</A>: postorder numbering of a directed graph.
+</UL></font>
+
+<H4>The <tt>abstractbb</tt> library, introduced for KVX core</H4>
+<UL>
+<LI> <A HREF="html/compcert.scheduling.abstractbb.AbstractBasicBlocksDef.html">AbstractBasicBlocksDef</A>: an IR for verifying some semantic properties on basic-blocks.
+<LI> <A HREF="html/compcert.scheduling.abstractbb.Parallelizability.html">Parallelizability</A>: verifying that sequential and parallel semantics are equivalent for a given abstract basic-block.
+<LI> <A HREF="html/compcert.scheduling.abstractbb.ImpSimuTest.html">ImpSimuTest</A>: verifying that a given abstract basic-block is simulated by another one for sequential semantics. This module refines <A HREF="html/compcert.scheduling.abstractbb.SeqSimuTheory.html">SeqSimuTheory</A> with hash-consing and uses <A HREF=https://github.com/boulme/ImpureDemo>the Impure library</A> to reason on physical equality and handling of imperative code in Coq.
+</UL>
+
+<font color=gray>
+<H3>Definitions and theorems used in many parts of the development</H3>
+
+<UL>
+<LI> <A HREF="html/compcert.common.Errors.html">Errors</A>: the Error monad.
+<LI> <A HREF="html/compcert.common.AST.html">AST</A>: identifiers, whole programs and other
+common elements of abstract syntaxes.
+<LI> <A HREF="html/compcert.common.Linking.html">Linking</A>: generic framework to define syntactic linking over the CompCert languages.
+<LI> <A HREF="html/compcert.common.Values.html">Values</A>: run-time values.
+<LI> <A HREF="html/compcert.common.Events.html">Events</A>: observable events and traces.
+<LI> <A HREF="html/compcert.common.Memory.html">Memory</A>: memory model. <BR>
+See also: <A HREF="html/compcert.common.Memdata.html">Memdata</A> (in-memory representation of data).
+<LI> <A HREF="html/compcert.common.Globalenvs.html">Globalenvs</A>: global execution environments.
+<LI> <A HREF="html/compcert.common.Smallstep.html">Smallstep</A>: tools for small-step semantics.
+<LI> <A HREF="html/compcert.common.Behaviors.html">Behaviors</A>: from small-step semantics to observable behaviors of programs.
+<LI> <A HREF="html/compcert.common.Determinism.html">Determinism</A>: determinism properties of small-step semantics.
+<LI> <A HREF="html/compcert.kvx.Op.html"><I>Op</I></A>: operators, addressing modes and their
+semantics.
+<LI> <A HREF="html/compcert.common.Builtins.html">Builtins</A>: semantics of built-in functions. <BR>
+See also: <A HREF="html/compcert.common.Builtins0.html">Builtins0</A> (target-independent part), <A HREF="html/compcert.kvx.Builtins1.html"><I>Builtins1</I></A> (target-dependent part).
+<LI> <A HREF="html/compcert.common.Unityping.html">Unityping</A>: a solver for atomic unification constraints.
+</UL>
+
+<H3>Source, intermediate and target languages: syntax and semantics</H3>
+
+<UL>
+<LI> The CompCert C source language:
+<A HREF="html/compcert.cfrontend.Csyntax.html">syntax</A> and
+<A HREF="html/compcert.cfrontend.Csem.html">semantics</A> and
+<A HREF="html/compcert.cfrontend.Cstrategy.html">determinized semantics</A> and
+<A HREF="html/compcert.cfrontend.Ctyping.html">type system</A>.<BR>
+See also: <A HREF="html/compcert.cfrontend.Ctypes.html">type expressions</A> and
+<A HREF="html/compcert.cfrontend.Cop.html">operators (syntax and semantics)</A>.<BR>
+See also: <A HREF="html/compcert.cfrontend.Cexec.html">reference interpreter</A>.
+<LI> <A HREF="html/compcert.cfrontend.Clight.html">Clight</A>: a simpler version of CompCert C where expressions contain no side-effects.
+<LI> <A HREF="html/compcert.cfrontend.Csharpminor.html">Csharpminor</A>: low-level
+ structured language.
+<LI> <A HREF="html/compcert.backend.Cminor.html">Cminor</A>: low-level structured
+language, with explicit stack allocation of certain local variables.
+<LI> <A HREF="html/compcert.backend.CminorSel.html">CminorSel</A>: like Cminor,
+with machine-specific operators and addressing modes.
+<LI> <A HREF="html/compcert.backend.RTL.html">RTL</A>: register transfer language (3-address
+code, control-flow graph, infinitely many pseudo-registers). <BR>
+See also: <A HREF="html/compcert.backend.Registers.html">Registers</A> (representation of
+pseudo-registers).
+<LI> <A HREF="html/compcert.backend.LTL.html">LTL</A>: location transfer language (3-address
+code, control-flow graph of basic blocks, finitely many physical registers, infinitely
+many stack slots). <BR>
+See also: <A HREF="html/compcert.backend.Locations.html">Locations</A> (representation of
+locations) and <A HREF="html/compcert.kvx.Machregs.html"><I>Machregs</I></A> (description of processor registers).
+<LI> <A HREF="html/compcert.backend.Linear.html">Linear</A>: like LTL, but the CFG is
+replaced by a linear list of instructions with explicit branches and labels.
+<LI> <A HREF="html/compcert.backend.Mach.html">Mach</A>: like Linear, with a more concrete
+view of the activation record.
+</UL>
+</font>
+<H4>Languages introduced for KVX core</H4>
+<UL>
+ <LI> <A HREF="html/compcert.scheduling.RTLpath.html">RTLpath</A>: extends RTL with annotations for delimitating superblocks (with possible liveness information).
+ This IR is generic over the processor, and used for prepass scheduling.
+<LI> <A HREF="html/compcert.scheduling.postpass_lib.Machblock.html">Machblock</A>: a variant of Mach, with a syntax for basic-blocks, and a block-step semantics (execute one basic-block in one step).
+This IR is generic over the processor, even if currently, only used for KVX.
+<LI> <A HREF="html/compcert.kvx.Asmvliw.html"><I>Asmvliw</I></A>: abstract syntax and semantics for KVX VLIW assembly: atomic instructions are grouped by "bundles". These bundles are executed sequentially, but execution is parallel within bundles.
+<LI> <A HREF="html/compcert.kvx.Asmblock.html"><I>Asmblock</I></A>: a variant of Asmvliw, with a sequential semantics within bundles, which make them corresponds here to usual basic-blocks.
+ This IR is an intermediate step between Machblock and Asmvliw.
+<LI> <A HREF="html/compcert.kvx.Asm.html"><I>Asm</I></A>: a variant of Asmvliw with a flat syntax for bundles, instead of a structured one (bundle termination is encoded as a pseudo-instruction). This IR is mainly a wrapper of <I>Asmvliw</I> for a smooth integration in CompCert (and an easier pretty-printing of the abstract syntax).
+</UL>
+
+<H3>Compiler passes</H3>
+
+<TABLE cellpadding="5%" style="color:#808080">
+<TR valign="top">
+ <TH align=left>Pass</TH>
+ <TH align=left>Source &amp; target</TH>
+ <TH align=left>Compiler&nbsp;code</TH>
+ <TH align=left>Correctness&nbsp;proof</TH>
+</TR>
+
+<TR valign="top">
+ <TD>Pulling side-effects out of expressions;<br>
+ fixing an evaluation order</TD>
+ <TD>CompCert C to Clight</TD>
+ <TD><A HREF="html/compcert.cfrontend.SimplExpr.html">SimplExpr</A></TD>
+ <TD><A HREF="html/compcert.cfrontend.SimplExprspec.html">SimplExprspec</A><br>
+ <A HREF="html/compcert.cfrontend.SimplExprproof.html">SimplExprproof</A></TD>
+</TR>
+<TR valign="top">
+ <TD>Pulling non-adressable scalar local variables out of memory</TD>
+ <TD>Clight to Clight</TD>
+ <TD><A HREF="html/compcert.cfrontend.SimplLocals.html">SimplLocals</A></TD>
+ <TD><A HREF="html/compcert.cfrontend.SimplLocalsproof.html">SimplLocalsproof</A></TD>
+</TR>
+<TR valign="top">
+ <TD>Simplification of control structures; <br>
+ explication of type-dependent computations</TD>
+ <TD>Clight to Csharpminor</TD>
+ <TD><A HREF="html/compcert.cfrontend.Cshmgen.html">Cshmgen</A></TD>
+ <TD><A HREF="html/compcert.cfrontend.Cshmgenproof.html">Cshmgenproof</A></TD>
+</TR>
+<TR valign="top">
+ <TD>Stack allocation of local variables<br>
+ whose address is taken;<br>
+ simplification of switch statements</TD>
+ <TD>Csharpminor to Cminor</TD>
+ <TD><A HREF="html/compcert.cfrontend.Cminorgen.html">Cminorgen</A></TD>
+ <TD><A HREF="html/compcert.cfrontend.Cminorgenproof.html">Cminorgenproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Recognition of operators<br>and addressing modes;<br>
+ if-conversion</TD>
+ <TD>Cminor to CminorSel</TD>
+ <TD><A HREF="html/compcert.backend.Selection.html">Selection</A><br>
+ <A HREF="html/compcert.kvx.SelectOp.html"><I>SelectOp</I></A><br>
+ <A HREF="html/compcert.kvx.SelectLong.html"><I>SelectLong</I></A><br>
+ <A HREF="html/compcert.backend.SelectDiv.html">SelectDiv</A><br>
+ <A HREF="html/compcert.backend.SplitLong.html">SplitLong</A></TD>
+ <TD><A HREF="html/compcert.backend.Selectionproof.html">Selectionproof</A><br>
+ <A HREF="html/compcert.kvx.SelectOpproof.html"><I>SelectOpproof</I></A><br>
+ <A HREF="html/compcert.kvx.SelectLongproof.html"><I>SelectLongproof</I></A><br>
+ <A HREF="html/compcert.backend.SelectDivproof.html">SelectDivproof</A><br>
+ <A HREF="html/compcert.backend.SplitLongproof.html">SplitLongproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Construction of the CFG, <br>3-address code generation</TD>
+ <TD>CminorSel to RTL</TD>
+ <TD><A HREF="html/compcert.backend.RTLgen.html">RTLgen</A></TD>
+ <TD><A HREF="html/compcert.backend.RTLgenspec.html">RTLgenspec</A><BR>
+ <A HREF="html/compcert.backend.RTLgenproof.html">RTLgenproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Recognition of tail calls</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Tailcall.html">Tailcall</A></TD>
+ <TD><A HREF="html/compcert.backend.Tailcallproof.html">Tailcallproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Function inlining</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Inlining.html">Inlining</A></TD>
+ <TD><A HREF="html/compcert.backend.Inliningspec.html">Inliningspec</A><BR>
+ <A HREF="html/compcert.backend.Inliningproof.html">Inliningproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Postorder renumbering of the CFG</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Renumber.html">Renumber</A></TD>
+ <TD><A HREF="html/compcert.backend.Renumberproof.html">Renumberproof</A></TD>
+</TR>
+<TR valign="top">
+ <TD>Common subexpression elimination</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.CSE.html">CSE</A><BR>
+ <A HREF="html/compcert.kvx.CombineOp.html"><I>CombineOp</I></A></TD>
+ <TD><A HREF="html/compcert.backend.CSEproof.html">CSEproof</A><BR>
+ <A HREF="html/compcert.kvx.CombineOpproof.html"><I>CombineOpproof</I></A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Constant propagation</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Constprop.html">Constprop</A><br>
+ <A HREF="html/compcert.kvx.ConstpropOp.html"><I>ConstpropOp</I></A></TD>
+ <TD><A HREF="html/compcert.backend.Constpropproof.html">Constpropproof</A><br>
+ <A HREF="html/compcert.kvx.ConstpropOpproof.html"><I>ConstproppOproof</I></A></TD>
+</TR>
+<TR valign="top">
+ <TD>Redundancy elimination</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Deadcode.html">Deadcode</A></TD>
+ <TD><A HREF="html/compcert.backend.Deadcodeproof.html">Deadcodeproof</A></TD>
+</TR>
+<TR valign="top">
+ <TD>Removal of unused static globals</TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Unusedglob.html">Unusedglob</A></TD>
+ <TD><A HREF="html/compcert.backend.Unusedglobproof.html">Unusedglobproof</A></TD>
+</TR>
+
+<TR valign="top" style="color:#000000">
+ <TD colspan="4"><b>Passes introduced for profiling (for later use in trace selection)</b></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Insert profiling annotations (for recording experiments -- see PROFILE.md).
+ </TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Profiling.html">Profiling</A></TD>
+ <TD><A HREF="html/compcert.backend.Profilingproof.html">Profilingproof</A></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Update ICond nodes (from recorded experiments -- see PROFILE.md).
+ </TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.ProfilingExploit.html">ProfilingExploit</A></TD>
+ <TD><A HREF="html/compcert.backend.ProfilingExploitproof.html">ProfilingExploitproof</A></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD colspan="4"><b>Passes introduced for superblock prepass scheduling</b></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Code duplications (trace selection, loop unrollings, etc)
+ </TD>
+ <TD>RTL to RTL</TD>
+ <TD><A HREF="html/compcert.backend.Duplicate.html">Duplicate</A> (generic checker)</TD>
+ <TD><A HREF="html/compcert.backend.Duplicateproof.html">Duplicateproof</A> (generic proof)<BR>
+ <a href="html/compcert.backend.Duplicatepasses.html">Duplicatepasses</a> (several passes from several oracles)</TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Superblock selection (with Liveness information)</TD>
+ <TD>RTL to RTLPath</TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpathLivegen.html">RTLpathLivegen</A></TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpathLivegenproof.html">RTLpathLivegenproof</A></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Superblock prepass scheduling</TD>
+ <TD>RTLPath to RTLPath</TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpathScheduler.html">RTLpathScheduler</A></TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpathSchedulerproof.html">RTLpathSchedulerproof</A><BR>
+ with <A HREF="html/compcert.scheduling.RTLpathSE_theory.html">RTLpathSE_theory</A> (the theory of symbolic execution on RTLpath)<BR>
+ and <A HREF="html/compcert.scheduling.RTLpathSE_simu_specs.html">RTLpathSE_simu_specs</A> (the low-level specifications of the simulation checker)<BR>
+ and <A HREF="html/compcert.scheduling.RTLpathSE_impl.html">RTLpathSE_impl</A> (the simulation checker with hash-consing)</TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Forgeting superblocks</TD>
+ <TD>RTLPath to RTL</TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpath.html#transf_program">RTLpath.transf_program</A></TD>
+ <TD><A HREF="html/compcert.scheduling.RTLpathproof.html">RTLpathproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD colspan="4"><b>Passes from register allocation</b></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Register allocation (validation a posteriori)</TD>
+ <TD>RTL to LTL</TD>
+ <TD><A HREF="html/compcert.backend.Allocation.html">Allocation</A></TD>
+ <TD><A HREF="html/compcert.backend.Allocproof.html">Allocproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>(LTL) Branch tunneling</TD>
+ <TD>LTL to LTL</TD>
+ <TD><A HREF="html/compcert.backend.LTLTunneling.html">LTLTunneling</A></TD>
+ <TD><A HREF="html/compcert.backend.LTLTunnelingproof.html">LTLTunnelingproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Linearization of the CFG</TD>
+ <TD>LTL to Linear</TD>
+ <TD><A HREF="html/compcert.backend.Linearize.html">Linearize</A></TD>
+ <TD><A HREF="html/compcert.backend.Linearizeproof.html">Linearizeproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Removal of unreferenced labels</TD>
+ <TD>Linear to Linear</TD>
+ <TD><A HREF="html/compcert.backend.CleanupLabels.html">CleanupLabels</A></TD>
+ <TD><A HREF="html/compcert.backend.CleanupLabelsproof.html">CleanupLabelsproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Synthesis of debugging information</TD>
+ <TD>Linear to Linear</TD>
+ <TD><A HREF="html/compcert.backend.Debugvar.html">Debugvar</A></TD>
+ <TD><A HREF="html/compcert.backend.Debugvarproof.html">Debugvarproof</A></TD>
+</TR>
+
+<TR valign="top">
+ <TD>Laying out the activation records</TD>
+ <TD>Linear to Mach</TD>
+ <TD><A HREF="html/compcert.backend.Stacking.html">Stacking</A><BR>
+ <A HREF="html/compcert.backend.Bounds.html">Bounds</A><BR>
+ <A HREF="html/compcert.kvx.Stacklayout.html"><I>Stacklayout</I></A></TD>
+ <TD><A HREF="html/compcert.backend.Stackingproof.html">Stackingproof</A><br>
+ <A HREF="html/compcert.common.Separation.html">Separation</A></TD>
+</TR>
+
+<TR valign="top" style="color:#000000">
+ <TD colspan="4"><b>Passes introduced for KVX VLIW</b></TD>
+</TR>
+<TR valign="top" style="color:#000000">
+ <TD>Reconstruction of basic-blocks at Mach level</TD>
+ <TD>Mach to Machblock</TD>
+ <TD><A HREF="html/compcert.scheduling.postpass_lib.Machblockgen.html">Machblockgen</A></TD>
+ <TD><A HREF="html/compcert.scheduling.postpass_lib.ForwardSimulationBlock.html">ForwardSimulationBlock</A><BR>
+ <A HREF="html/compcert.scheduling.postpass_lib.Machblockgenproof.html">Machblockgenproof</A></TD>
+</TR>
+
+<TR valign="top" style="color:#000000">
+ <TD>Emission of purely sequential assembly code</TD>
+ <TD>Machblock to Asmblock</TD>
+ <TD><A HREF="html/compcert.kvx.Asmblockgen.html"><I>Asmblockgen</I></A></TD>
+ <TD><A HREF="html/compcert.kvx.Asmblockgenproof0.html"><I>Asmblockgenproof0</I></A><BR>
+ <A HREF="html/compcert.kvx.Asmblockgenproof1.html"><I>Asmblockgenproof1</I></A><BR>
+ <A HREF="html/compcert.kvx.Asmblockgenproof.html"><I>Asmblockgenproof</I></A></TD>
+</TR>
+
+<TR valign="top" style="color:#000000">
+ <TD>Bundling (and basic-block scheduling)</TD>
+ <TD>Asmblock to Asmvliw</TD>
+ <TD><A HREF="html/compcert.kvx.PostpassScheduling.html"><I>PostpassScheduling</I></A><BR>
+ using <A HREF="html/compcert.kvx.Asmblockdeps.html"><I>Asmblockdeps</I></A><BR>
+ and the <tt>abstractbb</tt> library</TD>
+ <TD><A HREF="html/compcert.kvx.PostpassSchedulingproof.html"><I>PostpassSchedulingproof</I></A></TD>
+</TR>
+
+<TR valign="top" style="color:#000000">
+ <TD>Flattening bundles (only a bureaucratic operation)</TD>
+ <TD>Asmvliw to Asm</TD>
+ <TD><A HREF="html/compcert.kvx.Asmgen.html"><I>Asmgen</I></A></TD>
+ <TD><A HREF="html/compcert.kvx.Asmgenproof.html"><I>Asmgenproof</I></A> (whole simulation proof from <tt>Mach</tt> to <tt>Asm</tt>)</TD>
+</TR>
+</TABLE>
+
+<H3>All together (there are many more RTL passes than on vanilla CompCert: their order is specified in Compiler)</H3>
+
+<UL>
+</font>
+<LI> <A HREF="html/compcert.driver.Compiler.html">Compiler</A>: composing the passes together;
+whole-compiler semantic preservation theorems.
+<font color=gray>
+<LI> <A HREF="html/compcert.driver.Complements.html">Complements</A>: interesting consequences of the semantic preservation theorems.
+</UL>
+
+<H3>Static analyses</H3>
+
+The following static analyses are performed over the RTL intermediate
+representation to support optimizations such as constant propagation,
+CSE, and dead code elimination.
+<UL>
+<LI> <A HREF="html/compcert.backend.Liveness.html">Liveness</A>: liveness analysis</A>.
+<LI> <A HREF="html/compcert.backend.ValueAnalysis.html">ValueAnalysis</A>: value and alias analysis</A> <BR>
+See also: <A HREF="html/compcert.backend.ValueDomain.html">ValueDomain</A>: the abstract domain for value analysis.<BR>
+See also: <A HREF="html/compcert.kvx.ValueAOp.html"><I>ValueAOp</I></A>: processor-dependent parts of value analysis.
+<LI> <A HREF="html/compcert.backend.Deadcode.html">Deadcode</A>: neededness analysis</A> <BR>
+See also: <A HREF="html/compcert.backend.NeedDomain.html">NeedDomain</A>: the abstract domain for neededness analysis.<BR>
+See also: <A HREF="html/compcert.kvx.NeedOp.html"><I>NeedOp</I></A>: processor-dependent parts of neededness analysis.
+</UL>
+
+<H3>Type systems</H3>
+
+The <A HREF="html/compcert.cfrontend.Ctyping.html">type system of CompCert C</A> is fully formalized. For some intermediate languages of the back-end, simpler type systems are used to statically capture well-formedness conditions.
+<UL>
+<LI> <A HREF="html/compcert.cfrontend.Ctyping.html">Ctyping</A>: typing for CompCert C + type-checking functions.
+<LI> <A HREF="html/compcert.backend.RTLtyping.html">RTLtyping</A>: typing for RTL + type
+reconstruction.
+<LI> <A HREF="html/compcert.backend.Lineartyping.html">Lineartyping</A>: typing for Linear.
+</UL>
+</font>
+
+</BODY>
+</HTML>
diff --git a/doc/index.html b/doc/index.html
index c3912cb2..34b87924 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -270,10 +270,10 @@ code.
</TR>
<TR valign="top">
- <TD>Branch tunneling</TD>
+ <TD>(LTL) Branch tunneling</TD>
<TD>LTL to LTL</TD>
- <TD><A HREF="html/compcert.backend.Tunneling.html">Tunneling</A></TD>
- <TD><A HREF="html/compcert.backend.Tunnelingproof.html">Tunnelingproof</A></TD>
+ <TD><A HREF="html/compcert.backend.LTLTunneling.html">LTLTunneling</A></TD>
+ <TD><A HREF="html/compcert.backend.LTLTunnelingproof.html">LTLTunnelingproof</A></TD>
</TR>
<TR valign="top">
diff --git a/driver/Clflags.ml b/driver/Clflags.ml
index 25c3e1dd..6511087b 100644
--- a/driver/Clflags.ml
+++ b/driver/Clflags.ml
@@ -15,7 +15,7 @@
let prepro_options = ref ([]: string list)
let linker_options = ref ([]: string list)
let assembler_options = ref ([]: string list)
-let option_flongdouble = ref false
+let option_flongdouble = ref (Configuration.arch = "kvx")
let option_fstruct_passing = ref false
let option_fvararg_calls = ref true
let option_funprototyped = ref true
@@ -25,7 +25,36 @@ let option_ffloatconstprop = ref 2
let option_ftailcalls = ref true
let option_fconstprop = ref true
let option_fcse = ref true
+let option_fcse2 = ref false
+
+let option_fcse3 = ref true
+let option_fcse3_alias_analysis = ref true
+let option_fcse3_across_calls = ref false
+let option_fcse3_across_merges = ref true
+let option_fcse3_glb = ref true
+let option_fcse3_trivial_ops = ref false
+let option_fcse3_refine = ref true
+let option_fcse3_conditions = ref true
+
let option_fredundancy = ref true
+
+(** Options relative to superblock scheduling *)
+let option_fpredict = ref true (* insert static branch prediction information, and swaps ifso/ifnot branches accordingly *)
+let option_ftailduplicate = ref 0 (* perform tail duplication for blocks of size n *)
+let option_ftracelinearize = ref true (* uses branch prediction information to improve the linearization *)
+let option_funrollsingle = ref 0 (* unroll a single iteration of innermost loops of size n *)
+let option_funrollbody = ref 0 (* unroll the body of innermost loops of size n *)
+let option_flooprotate = ref 0 (* rotate the innermost loops to have the condition inside the loop body *)
+
+(* Scheduling *)
+let option_mtune = ref ""
+
+let option_fprepass = ref true
+let option_fprepass_sched = ref "regpres"
+
+let option_fpostpass = ref true
+let option_fpostpass_sched = ref "list"
+
let option_fifconversion = ref true
let option_Obranchless = ref false
let option_falignfunctions = ref (None: int option)
@@ -66,4 +95,25 @@ let option_small_const = ref (!option_small_data)
let option_timings = ref false
let stdlib_path = ref Configuration.stdlib_path
let use_standard_headers = ref Configuration.has_standard_headers
+
+let option_fglobaladdrtmp = ref false
+let option_fglobaladdroffset = ref false
+let option_fxsaddr = ref true
+let option_faddx = ref false
+let option_fmadd = ref true
+let option_div_i32 = ref "stsud"
+let option_div_i64 = ref "stsud"
+let option_fcoalesce_mem = ref true
+let option_fexpanse_rtlcond = ref false
+let option_fexpanse_others = ref false
+let option_fforward_moves = ref false
+let option_fmove_loop_invariants = ref false
+let option_fnontrap_loads = ref true
+let option_all_loads_nontrap = ref false
+let option_inline_auto_threshold = ref 0
+let option_profile_arcs = ref false
+let option_fbranch_probabilities = ref true
+let option_debug_compcert = ref 0
+let option_regpres_threshold = ref 2
+let option_regpres_wait_window = ref false
let main_function_name = ref "main"
diff --git a/driver/Compiler.v b/driver/Compiler.vexpand
index 75247f71..9673267d 100644
--- a/driver/Compiler.v
+++ b/driver/Compiler.vexpand
@@ -35,19 +35,8 @@ Require Cshmgen.
Require Cminorgen.
Require Selection.
Require RTLgen.
-Require Tailcall.
-Require Inlining.
-Require Renumber.
-Require Constprop.
-Require CSE.
-Require Deadcode.
-Require Unusedglob.
-Require Allocation.
-Require Tunneling.
-Require Linearize.
-Require CleanupLabels.
-Require Debugvar.
-Require Stacking.
+Require Import Duplicatepasses.
+EXPAND_RTL_REQUIRE
Require Asmgen.
(** Proofs of semantic preservation. *)
Require SimplExprproof.
@@ -56,20 +45,8 @@ Require Cshmgenproof.
Require Cminorgenproof.
Require Selectionproof.
Require RTLgenproof.
-Require Tailcallproof.
-Require Inliningproof.
-Require Renumberproof.
-Require Constpropproof.
-Require CSEproof.
-Require Deadcodeproof.
-Require Unusedglobproof.
-Require Allocproof.
-Require Tunnelingproof.
-Require Linearizeproof.
-Require CleanupLabelsproof.
-Require Debugvarproof.
-Require Stackingproof.
-Require Asmgenproof.
+EXPAND_RTL_REQUIRE_PROOF
+Require Import Asmgenproof.
(** Command-line flags. *)
Require Import Compopts.
@@ -77,7 +54,7 @@ Require Import Compopts.
Parameter print_Clight: Clight.program -> unit.
Parameter print_Cminor: Cminor.program -> unit.
Parameter print_RTL: Z -> RTL.program -> unit.
-Parameter print_LTL: LTL.program -> unit.
+Parameter print_LTL: Z -> LTL.program -> unit.
Parameter print_Mach: Mach.program -> unit.
Local Open Scope string_scope.
@@ -120,32 +97,9 @@ Definition partial_if {A: Type}
Definition transf_rtl_program (f: RTL.program) : res Asm.program :=
OK f
@@ print (print_RTL 0)
- @@ total_if Compopts.optim_tailcalls (time "Tail calls" Tailcall.transf_program)
- @@ print (print_RTL 1)
- @@@ time "Inlining" Inlining.transf_program
- @@ print (print_RTL 2)
- @@ time "Renumbering" Renumber.transf_program
- @@ print (print_RTL 3)
- @@ total_if Compopts.optim_constprop (time "Constant propagation" Constprop.transf_program)
- @@ print (print_RTL 4)
- @@ total_if Compopts.optim_constprop (time "Renumbering" Renumber.transf_program)
- @@ print (print_RTL 5)
- @@@ partial_if Compopts.optim_CSE (time "CSE" CSE.transf_program)
- @@ print (print_RTL 6)
- @@@ partial_if Compopts.optim_redundancy (time "Redundancy elimination" Deadcode.transf_program)
- @@ print (print_RTL 7)
- @@@ time "Unused globals" Unusedglob.transform_program
- @@ print (print_RTL 8)
- @@@ time "Register allocation" Allocation.transf_program
- @@ print print_LTL
- @@ time "Branch tunneling" Tunneling.tunnel_program
- @@@ time "CFG linearization" Linearize.transf_program
- @@ time "Label cleanup" CleanupLabels.transf_program
- @@@ partial_if Compopts.debug (time "Debugging info for local variables" Debugvar.transf_program)
- @@@ time "Mach generation" Stacking.transf_program
- @@ print print_Mach
- @@@ time "Asm generation" Asmgen.transf_program.
-
+EXPAND_RTL_TRANSF_PROGRAM
+ @@@ time "Total Mach->Asm generation" Asmgen.transf_program.
+
Definition transf_cminor_program (p: Cminor.program) : res Asm.program :=
OK p
@@ print print_Cminor
@@ -235,20 +189,7 @@ Definition CompCert's_passes :=
::: mkpass Cminorgenproof.match_prog
::: mkpass Selectionproof.match_prog
::: mkpass RTLgenproof.match_prog
- ::: mkpass (match_if Compopts.optim_tailcalls Tailcallproof.match_prog)
- ::: mkpass Inliningproof.match_prog
- ::: mkpass Renumberproof.match_prog
- ::: mkpass (match_if Compopts.optim_constprop Constpropproof.match_prog)
- ::: mkpass (match_if Compopts.optim_constprop Renumberproof.match_prog)
- ::: mkpass (match_if Compopts.optim_CSE CSEproof.match_prog)
- ::: mkpass (match_if Compopts.optim_redundancy Deadcodeproof.match_prog)
- ::: mkpass Unusedglobproof.match_prog
- ::: mkpass Allocproof.match_prog
- ::: mkpass Tunnelingproof.match_prog
- ::: mkpass Linearizeproof.match_prog
- ::: mkpass CleanupLabelsproof.match_prog
- ::: mkpass (match_if Compopts.debug Debugvarproof.match_prog)
- ::: mkpass Stackingproof.match_prog
+EXPAND_RTL_MKPASS
::: mkpass Asmgenproof.match_prog
::: pass_nil _.
@@ -268,30 +209,18 @@ Theorem transf_c_program_match:
match_prog p tp.
Proof.
intros p tp T.
- unfold transf_c_program, time in T. simpl in T.
- destruct (SimplExpr.transl_program p) as [p1|e] eqn:P1; simpl in T; try discriminate.
- unfold transf_clight_program, time in T. rewrite ! compose_print_identity in T. simpl in T.
- destruct (SimplLocals.transf_program p1) as [p2|e] eqn:P2; simpl in T; try discriminate.
- destruct (Cshmgen.transl_program p2) as [p3|e] eqn:P3; simpl in T; try discriminate.
- destruct (Cminorgen.transl_program p3) as [p4|e] eqn:P4; simpl in T; try discriminate.
- unfold transf_cminor_program, time in T. rewrite ! compose_print_identity in T. simpl in T.
- destruct (Selection.sel_program p4) as [p5|e] eqn:P5; simpl in T; try discriminate.
- destruct (RTLgen.transl_program p5) as [p6|e] eqn:P6; simpl in T; try discriminate.
- unfold transf_rtl_program, time in T. rewrite ! compose_print_identity in T. simpl in T.
- set (p7 := total_if optim_tailcalls Tailcall.transf_program p6) in *.
- destruct (Inlining.transf_program p7) as [p8|e] eqn:P8; simpl in T; try discriminate.
- set (p9 := Renumber.transf_program p8) in *.
- set (p10 := total_if optim_constprop Constprop.transf_program p9) in *.
- set (p11 := total_if optim_constprop Renumber.transf_program p10) in *.
- destruct (partial_if optim_CSE CSE.transf_program p11) as [p12|e] eqn:P12; simpl in T; try discriminate.
- destruct (partial_if optim_redundancy Deadcode.transf_program p12) as [p13|e] eqn:P13; simpl in T; try discriminate.
- destruct (Unusedglob.transform_program p13) as [p14|e] eqn:P14; simpl in T; try discriminate.
- destruct (Allocation.transf_program p14) as [p15|e] eqn:P15; simpl in T; try discriminate.
- set (p16 := Tunneling.tunnel_program p15) in *.
- destruct (Linearize.transf_program p16) as [p17|e] eqn:P17; simpl in T; try discriminate.
- set (p18 := CleanupLabels.transf_program p17) in *.
- destruct (partial_if debug Debugvar.transf_program p18) as [p19|e] eqn:P19; simpl in T; try discriminate.
- destruct (Stacking.transf_program p19) as [p20|e] eqn:P20; simpl in T; try discriminate.
+ unfold transf_c_program, time in T. cbn in T.
+ destruct (SimplExpr.transl_program p) as [p1|e] eqn:P1; cbn in T; try discriminate.
+ unfold transf_clight_program, time in T. rewrite ! compose_print_identity in T. cbn in T.
+ destruct (SimplLocals.transf_program p1) as [p2|e] eqn:P2; cbn in T; try discriminate.
+ destruct (Cshmgen.transl_program p2) as [p3|e] eqn:P3; cbn in T; try discriminate.
+ destruct (Cminorgen.transl_program p3) as [p4|e] eqn:P4; cbn in T; try discriminate.
+ unfold transf_cminor_program, time in T. rewrite ! compose_print_identity in T. cbn in T.
+ destruct (Selection.sel_program p4) as [p5|e] eqn:P5; cbn in T; try discriminate.
+ destruct (RTLgen.transl_program p5) as [p6|e] eqn:P6; cbn in T; try discriminate.
+ unfold transf_rtl_program, time in T. rewrite ! compose_print_identity in T.
+ cbn in T.
+EXPAND_RTL_PROOF
unfold match_prog; simpl.
exists p1; split. apply SimplExprproof.transf_program_match; auto.
exists p2; split. apply SimplLocalsproof.match_transf_program; auto.
@@ -299,20 +228,7 @@ Proof.
exists p4; split. apply Cminorgenproof.transf_program_match; auto.
exists p5; split. apply Selectionproof.transf_program_match; auto.
exists p6; split. apply RTLgenproof.transf_program_match; auto.
- exists p7; split. apply total_if_match. apply Tailcallproof.transf_program_match.
- exists p8; split. apply Inliningproof.transf_program_match; auto.
- exists p9; split. apply Renumberproof.transf_program_match; auto.
- exists p10; split. apply total_if_match. apply Constpropproof.transf_program_match.
- exists p11; split. apply total_if_match. apply Renumberproof.transf_program_match.
- exists p12; split. eapply partial_if_match; eauto. apply CSEproof.transf_program_match.
- exists p13; split. eapply partial_if_match; eauto. apply Deadcodeproof.transf_program_match.
- exists p14; split. apply Unusedglobproof.transf_program_match; auto.
- exists p15; split. apply Allocproof.transf_program_match; auto.
- exists p16; split. apply Tunnelingproof.transf_program_match.
- exists p17; split. apply Linearizeproof.transf_program_match; auto.
- exists p18; split. apply CleanupLabelsproof.transf_program_match; auto.
- exists p19; split. eapply partial_if_match; eauto. apply Debugvarproof.transf_program_match.
- exists p20; split. apply Stackingproof.transf_program_match; auto.
+EXPAND_RTL_PROOF2
exists tp; split. apply Asmgenproof.transf_program_match; auto.
reflexivity.
Qed.
@@ -364,7 +280,9 @@ Ltac DestructM :=
destruct H as (p & M & MM); clear H
end.
repeat DestructM. subst tp.
- assert (F: forward_simulation (Cstrategy.semantics p) (Asm.semantics p21)).
+ assert (F: forward_simulation (Cstrategy.semantics p)
+EXPAND_ASM_SEMANTICS
+ ).
{
eapply compose_forward_simulations.
eapply SimplExprproof.transl_program_correct; eassumption.
@@ -378,25 +296,17 @@ Ltac DestructM :=
eapply Selectionproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
eapply RTLgenproof.transf_program_correct; eassumption.
+EXPAND_RTL_FORWARD_SIMULATIONS
eapply compose_forward_simulations.
- eapply match_if_simulation. eassumption. exact Tailcallproof.transf_program_correct.
- eapply compose_forward_simulations.
- eapply Inliningproof.transf_program_correct; eassumption.
- eapply compose_forward_simulations. eapply Renumberproof.transf_program_correct; eassumption.
- eapply compose_forward_simulations.
- eapply match_if_simulation. eassumption. exact Constpropproof.transf_program_correct.
- eapply compose_forward_simulations.
- eapply match_if_simulation. eassumption. exact Renumberproof.transf_program_correct.
- eapply compose_forward_simulations.
- eapply match_if_simulation. eassumption. exact CSEproof.transf_program_correct.
+ eapply RTLtoBTLproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
- eapply match_if_simulation. eassumption. exact Deadcodeproof.transf_program_correct; eassumption.
+ eapply BTL_Schedulerproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
- eapply Unusedglobproof.transf_program_correct; eassumption.
+ eapply BTLtoRTLproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
- eapply Allocproof.transf_program_correct; eassumption.
+ eapply Allocationproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
- eapply Tunnelingproof.transf_program_correct; eassumption.
+ eapply LTLTunnelingproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
eapply Linearizeproof.transf_program_correct; eassumption.
eapply compose_forward_simulations.
diff --git a/driver/Compopts.v b/driver/Compopts.v
index 2a213350..65264124 100644
--- a/driver/Compopts.v
+++ b/driver/Compopts.v
@@ -36,11 +36,75 @@ Parameter optim_constprop: unit -> bool.
(** Flag -fcse. For common subexpression elimination. *)
Parameter optim_CSE: unit -> bool.
+(** Flag -fcse2. For DMonniaux's common subexpression elimination. *)
+Parameter optim_CSE2: unit -> bool.
+
+(** Flag -fcse3. For DMonniaux's common subexpression elimination. *)
+Parameter optim_CSE3: unit -> bool.
+
+(** Flag -fcse3-alias-analysis. For DMonniaux's common subexpression elimination. Perform a simple alias analysis. *)
+Parameter optim_CSE3_alias_analysis: unit -> bool.
+
+(** Flag -fcse3-across-calls. For DMonniaux's common subexpression elimination. Propagate information across function calls (may increase register pressure). *)
+Parameter optim_CSE3_across_calls: unit -> bool.
+
+(** Flag -fcse3-across-calls. For DMonniaux's common subexpression elimination. Propagate information across control-flow merges (may increase register pressure). *)
+Parameter optim_CSE3_across_merges: unit -> bool.
+
+(** Flag -fcse3-glb *)
+Parameter optim_CSE3_glb: unit -> bool.
+
+(** Flag -fcse3-trivial-ops. For DMonniaux's common subexpression elimination, simplify trivial operations as well. *)
+Parameter optim_CSE3_trivial_ops: unit -> bool.
+
+(** Flag -fcse3-conditions. For DMonniaux's common subexpression elimination: remove redundant conditional branches. *)
+Parameter optim_CSE3_conditions: unit -> bool.
+
+(** Flag -fmove-loop-invariants. *)
+Parameter optim_move_loop_invariants: unit -> bool.
+
(** Flag -fredundancy. For dead code elimination. *)
Parameter optim_redundancy: unit -> bool.
+(** Flag -fpostpass. Postpass scheduling for K1 architecture *)
+Parameter optim_postpass: unit -> bool.
+
+(** FIXME TEMPORARY Flag -fglobaladdrtmp. Use a temporary register for loading the address of global variables (default false) *)
+Parameter optim_globaladdrtmp: unit -> bool.
+
+(** FIXME TEMPORARY Flag -fglobaladdroffset. Fold offsets into global addresses (default false) *)
+Parameter optim_globaladdroffset: unit -> bool.
+
+(** FIXME TEMPORARY Flag -fxsaddr. Use .xs addressing mode (default true) *)
+Parameter optim_xsaddr: unit -> bool.
+
+(** FIXME TEMPORARY Flag -fcoaelesce-mem. Fuse (default true) *)
+Parameter optim_coalesce_mem: unit -> bool.
+
+(* FIXME TEMPORARY Flag -faddx. Fuse (default true) *)
+Parameter optim_madd: unit -> bool.
+
+(** FIXME TEMPORARY Flag -faddx. Fuse (default false) *)
+Parameter optim_addx: unit -> bool.
+
(** Flag -fthumb. For the ARM back-end. *)
Parameter thumb: unit -> bool.
(** Flag -g. For insertion of debugging information. *)
Parameter debug: unit -> bool.
+
+(** Flag -fall-loads-nontrap. Turn user loads into non trapping. *)
+Parameter all_loads_nontrap: unit -> bool.
+
+(** Flag -fforward-moves. Forward moves after CSE. *)
+Parameter optim_forward_moves: unit -> bool.
+
+(** Flag -fprofile-arcs. Add profiling logger. *)
+Parameter profile_arcs : unit -> bool.
+
+(** Flag -fbranch_probabilities. Use profiling information if available *)
+Parameter branch_probabilities : unit -> bool.
+
+(* TODO is there a more appropriate place? *)
+Require Import Coqlib.
+Definition time {A B: Type} (name: string) (f: A -> B) : A -> B := f.
diff --git a/driver/Configuration.ml b/driver/Configuration.ml
index 4b0c116e..deca85f2 100644
--- a/driver/Configuration.ml
+++ b/driver/Configuration.ml
@@ -123,9 +123,10 @@ let get_bool_config key =
let arch =
match get_config_string "arch" with
- | "powerpc"|"arm"|"x86"|"riscV"|"aarch64" as a -> a
+ | "powerpc"|"arm"|"x86"|"riscV"|"kvx"|"aarch64" as a -> a
| v -> bad_config "arch" [v]
let model = get_config_string "model"
+let os = get_config_string "os"
let abi = get_config_string "abi"
let is_big_endian =
match get_config_string "endianness" with
diff --git a/driver/Configuration.mli b/driver/Configuration.mli
index a71da72d..75e547ff 100644
--- a/driver/Configuration.mli
+++ b/driver/Configuration.mli
@@ -19,6 +19,9 @@ val model: string
val abi: string
(** ABI to use *)
+val os: string
+ (** ABI to use *)
+
val is_big_endian: bool
(** Endianness to use *)
diff --git a/driver/Driver.ml b/driver/Driver.ml
index 2b34d538..3f5a4bd9 100644
--- a/driver/Driver.ml
+++ b/driver/Driver.ml
@@ -185,16 +185,46 @@ Processing options:
{|Optimization options: (use -fno-<opt> to turn off -f<opt>)
-O Optimize the compiled code [on by default]
-O0 Do not optimize the compiled code
- -O1 -O2 -O3 Synonymous for -O
+ -O1 Perform all optimization passes except scheduling
+ -O2 -O3 Synonymous for -O
-Os Optimize for code size in preference to code speed
-Obranchless Optimize to generate fewer conditional branches; try to produce
branch-free instruction sequences as much as possible
+ -finline-auto-threshold n Inline functions under size n
-ftailcalls Optimize function calls in tail position [on]
-fconst-prop Perform global constant propagation [on]
-ffloat-const-prop <n> Control constant propagation of floats
(<n>=0: none, <n>=1: limited, <n>=2: full; default is full)
-fcse Perform common subexpression elimination [on]
+ -fcse2 Perform inter-loop common subexpression elimination [off]
+ -fcse3 Perform inter-loop common subexpression elimination [on]
+ -fcse3-alias-analysis Perform inter-loop common subexpression elimination with alias analysis [on]
+ -fcse3-across-calls Propagate CSE3 information across function calls [off]
+ -fcse3-across-merges Propagate CSE3 information across control-flow merges [on]
+ -fcse3-glb Refine CSE3 information using greatest lower bounds [on]
+ -fcse3-trivial-ops Replace trivial operations as well using CSE3 [off]
+ -fcse3-refine Refine CSE3 invariants by descending iteration [on]
+ -fcse3-conditions Remove redundant conditions using CSE3 [on]
+ -fmove-loop-invariants Perform loop-invariant code motion [off]
-fredundancy Perform redundancy elimination [on]
+ -mtune= Type of CPU (for scheduling on some architectures)
+ -fprepass Perform prepass scheduling (only on some architectures) [on]
+ -fprepass= <optim> Perform postpass scheduling with the specified optimization [list]
+ (<optim>=list: list scheduling, <optim>=revlist: reverse list scheduling, <optim>=regpres: list scheduling aware of register pressure, <optim>=regpres_bis: variant of regpres, <optim>=zigzag: zigzag scheduling, <optim>=ilp: ILP, <optim>=greedy: just packing bundles)
+ -regpres-threshold n With `-fprepass= regpres`, set threshold value for number of free registers before trying to decrease register pressure
+ -fregpres-wait-window When register pressure is high, use a 5-cycle waiting window instead of scheduling short paths first (default no)
+ -fpostpass Perform postpass scheduling (only for K1 architecture) [on]
+ -fpostpass= <optim> Perform postpass scheduling with the specified optimization [list]
+ (<optim>=list: list scheduling, <optim>=ilp: ILP, <optim>=greedy: just packing bundles)
+ -fpredict Insert static branch prediction information [on]
+ Also swaps ifso/ifnot branches accordingly at RTL level
+ -ftailduplicate n Perform tail duplication for RTL code blocks of size n (not counting Inops) [0]
+ -ftracelinearize Uses branch prediction information to improve the Linearize [on]
+ -funrollsingle n Unrolls a single iteration of innermost loops of size n (not counting Inops) [0]
+ -funrollbody n Unrolls once the body of innermost loops of size n (not counting Inops) [0]
+ -flooprotate n Duplicates the header (condition computation part) of innermost loops to perform a loop rotate [0]
+ Doesn't duplicate if the size of that header is strictly greater than n
+ -fforward-moves Forward moves after CSE
-finline Perform inlining of functions [on]
-finline-functions-called-once Integrate functions only required by their
single caller [on]
@@ -206,7 +236,10 @@ Code generation options: (use -fno-<opt> to turn off -f<opt>)
-falign-functions <n> Set alignment (in bytes) of function entry points
-falign-branch-targets <n> Set alignment (in bytes) of branch targets
-falign-cond-branches <n> Set alignment (in bytes) of conditional branches
- -fcommon Put uninitialized globals in the common section [on].
+ -fcommon Put uninitialized globals in the common section [on]
+ -fprofile-arcs Profile branches [off].
+ -fprofile-use= filename Use profiling information in filename
+ -fbranch-probabilities Use profiling information (if available) for branches [on]
|} ^
target_help ^
toolchain_help ^
@@ -248,8 +281,11 @@ let dump_mnemonics destfile =
exit 0
let optimization_options = [
- option_ftailcalls; option_fifconversion; option_fconstprop; option_fcse;
- option_fredundancy; option_finline; option_finline_functions_called_once;
+ option_ftailcalls; option_fifconversion; option_fconstprop;
+ option_fcse; option_fcse2; option_fcse3;
+ option_fpredict; option_ftracelinearize;
+ option_fpostpass;
+ option_fredundancy; option_finline; option_finline_functions_called_once;
]
let set_all opts () = List.iter (fun r -> r := true) opts
@@ -262,6 +298,14 @@ let num_input_files = ref 0
let cmdline_actions =
let f_opt name ref =
[Exact("-f" ^ name), Set ref; Exact("-fno-" ^ name), Unset ref] in
+ let f_opt_str name default ref strref =
+ [Exact("-f" ^ name ^ "="), String
+ (fun s -> (strref := (if s == "" then default else s)); ref := true)
+ ] in
+ let f_str name strref default =
+ [Exact("-f" ^ name ^ "="), String
+ (fun s -> (strref := (if s == "" then default else s)))
+ ] in
let check_align n =
if n <= 0 || ((n land (n - 1)) <> 0) then
error no_loc "requested alignment %d is not a power of 2" n
@@ -293,9 +337,14 @@ let cmdline_actions =
[
Exact "-O0", Unit (unset_all optimization_options);
Exact "-O", Unit (set_all optimization_options);
+ _Regexp "-O1", Self (fun _ -> set_all optimization_options (); option_fpostpass := false);
_Regexp "-O[123]$", Unit (set_all optimization_options);
Exact "-Os", Set option_Osize;
Exact "-Obranchless", Set option_Obranchless;
+ Exact "-fprofile-use=", String (fun s -> Profilingaux.load_profiling_info s);
+ Exact "-finline-auto-threshold", Integer (fun n -> option_inline_auto_threshold := n);
+ Exact "-debug-compcert", Integer (fun n -> option_debug_compcert := n);
+ Exact "-regpres-threshold", Integer (fun n -> option_regpres_threshold := n);
Exact "-fsmall-data", Integer(fun n -> option_small_data := n);
Exact "-fsmall-const", Integer(fun n -> option_small_const := n);
Exact "-ffloat-const-prop", Integer(fun n -> option_ffloatconstprop := n);
@@ -365,10 +414,47 @@ let cmdline_actions =
@ f_opt "if-conversion" option_fifconversion
@ f_opt "const-prop" option_fconstprop
@ f_opt "cse" option_fcse
+ @ f_opt "cse2" option_fcse2
+ @ f_opt "cse3" option_fcse3
+ @ f_opt "cse3-alias-analysis" option_fcse3_alias_analysis
+ @ f_opt "cse3-across-calls" option_fcse3_across_calls
+ @ f_opt "cse3-across-merges" option_fcse3_across_merges
+ @ f_opt "cse3-glb" option_fcse3_glb
+ @ f_opt "cse3-trivial-ops" option_fcse3_trivial_ops
+ @ f_opt "cse3-refine" option_fcse3_refine
+ @ f_opt "cse3-conditions" option_fcse3_conditions
+ @ f_opt "move-loop-invariants" option_fmove_loop_invariants
@ f_opt "redundancy" option_fredundancy
+ @ [ Exact "-mtune", String (fun s -> option_mtune := s) ]
+ @ f_opt "prepass" option_fprepass
+ @ f_opt "regpres-wait-window" option_regpres_wait_window
+ @ f_opt "postpass" option_fpostpass
+ @ [ Exact "-ftailduplicate", Integer (fun n -> option_ftailduplicate := n) ]
+ @ f_opt "predict" option_fpredict
+ @ [ Exact "-funrollsingle", Integer (fun n -> option_funrollsingle := n) ]
+ @ [ Exact "-funrollbody", Integer (fun n -> option_funrollbody := n) ]
+ @ [ Exact "-flooprotate", Integer (fun n -> option_flooprotate := n) ]
+ @ f_opt "tracelinearize" option_ftracelinearize
+ @ f_opt_str "prepass" "regpress" option_fprepass option_fprepass_sched
+ @ f_opt_str "postpass" "list" option_fpostpass option_fpostpass_sched
@ f_opt "inline" option_finline
@ f_opt "inline-functions-called-once" option_finline_functions_called_once
-(* Code generation options *)
+ @ f_opt "globaladdrtmp" option_fglobaladdrtmp
+ @ f_opt "globaladdroffset" option_fglobaladdroffset
+ @ f_opt "xsaddr" option_fxsaddr
+ @ f_str "div-i32" option_div_i32 "stsud"
+ @ f_str "div-i64" option_div_i64 "stsud"
+ @ f_opt "addx" option_faddx
+ @ f_opt "madd" option_fmadd
+ @ f_opt "nontrap-loads" option_fnontrap_loads
+ @ f_opt "coalesce-mem" option_fcoalesce_mem
+ @ f_opt "expanse-rtlcond" option_fexpanse_rtlcond
+ @ f_opt "expanse-others" option_fexpanse_others
+ @ f_opt "all-loads-nontrap" option_all_loads_nontrap
+ @ f_opt "forward-moves" option_fforward_moves
+ (* Code generation options *)
+ @ f_opt "profile-arcs" option_profile_arcs
+ @ f_opt "branch-probabilities" option_fbranch_probabilities
@ f_opt "fpu" option_ffpu
@ f_opt "sse" option_ffpu (* backward compatibility *)
@ [
diff --git a/driver/Frontend.ml b/driver/Frontend.ml
index 6133291e..ecf3d6a5 100644
--- a/driver/Frontend.ml
+++ b/driver/Frontend.ml
@@ -116,6 +116,11 @@ let init () =
| "riscV" -> if Configuration.model = "64"
then Machine.rv64
else Machine.rv32
+ | "kvx" -> if Configuration.os = "cos" then Machine.kvxcos
+ else if Configuration.os = "mbr" then Machine.kvxmbr
+ else if Configuration.os = "elf" then Machine.kvxelf
+ else (Printf.eprintf "Configuration OS = %s\n" Configuration.os;
+ failwith "Wrong OS configuration for KVX")
| "aarch64" -> if Configuration.abi = "apple"
then Machine.aarch64_apple
else Machine.aarch64
diff --git a/export/ExportBase.ml b/export/ExportBase.ml
index 4b93d8a9..b6388768 100644
--- a/export/ExportBase.ml
+++ b/export/ExportBase.ml
@@ -219,6 +219,7 @@ let external_function p = function
coqstring text
signatur sg
(print_list coqstring) clob
+ | EF_profiling(_ ,_) -> failwith "EF_profiling in export"
(* Variables *)
diff --git a/export/ExportClight.ml b/export/ExportClight.ml
index e3b00986..959c4f5c 100644
--- a/export/ExportClight.ml
+++ b/export/ExportClight.ml
@@ -50,6 +50,7 @@ let name_binop = function
| Ogt -> "Ogt"
| Ole -> "Ole"
| Oge -> "Oge"
+ | Oexpect -> "Oexpect"
let rec expr p = function
| Evar(id, t) ->
diff --git a/export/ExportCsyntax.ml b/export/ExportCsyntax.ml
index 1d91cba5..25d8526b 100644
--- a/export/ExportCsyntax.ml
+++ b/export/ExportCsyntax.ml
@@ -54,6 +54,7 @@ let name_binop = function
| Ogt -> "Ogt"
| Ole -> "Ole"
| Oge -> "Oge"
+ | Oexpect -> "Oexpect"
let name_incr_or_decr = function
| Incr -> "Incr"
diff --git a/exportclight/ExportClight.ml b/exportclight/ExportClight.ml
new file mode 100644
index 00000000..e6d08d4f
--- /dev/null
+++ b/exportclight/ExportClight.ml
@@ -0,0 +1,559 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the GNU Lesser General Public License as *)
+(* published by the Free Software Foundation, either version 2.1 of *)
+(* the License, or (at your option) any later version. *)
+(* This file is also distributed under the terms of the *)
+(* INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Export Clight as a Coq file *)
+
+open Format
+open Camlcoq
+open AST
+open! Ctypes
+open Cop
+open Clight
+
+(* Options, lists, pairs *)
+
+let print_option fn p = function
+ | None -> fprintf p "None"
+ | Some x -> fprintf p "(Some %a)" fn x
+
+let print_pair fn1 fn2 p (x1, x2) =
+ fprintf p "@[<hov 1>(%a,@ %a)@]" fn1 x1 fn2 x2
+
+let print_list fn p l =
+ match l with
+ | [] ->
+ fprintf p "nil"
+ | hd :: tl ->
+ fprintf p "@[<hov 1>(";
+ let rec plist = function
+ | [] -> fprintf p "nil"
+ | hd :: tl -> fprintf p "%a ::@ " fn hd; plist tl
+ in plist l;
+ fprintf p ")@]"
+
+(* Numbers *)
+
+let coqint p n =
+ let n = camlint_of_coqint n in
+ if n >= 0l
+ then fprintf p "(Int.repr %ld)" n
+ else fprintf p "(Int.repr (%ld))" n
+
+let coqptrofs p n =
+ let s = Z.to_string n in
+ if Z.ge n Z.zero
+ then fprintf p "(Ptrofs.repr %s)" s
+ else fprintf p "(Ptrofs.repr (%s))" s
+
+let coqint64 p n =
+ let n = camlint64_of_coqint n in
+ if n >= 0L
+ then fprintf p "(Int64.repr %Ld)" n
+ else fprintf p "(Int64.repr (%Ld))" n
+
+let coqfloat p n =
+ fprintf p "(Float.of_bits %a)" coqint64 (Floats.Float.to_bits n)
+
+let coqsingle p n =
+ fprintf p "(Float32.of_bits %a)" coqint (Floats.Float32.to_bits n)
+
+let positive p n =
+ fprintf p "%s%%positive" (Z.to_string (Z.Zpos n))
+
+let coqN p n =
+ fprintf p "%s%%N" (Z.to_string (Z.of_N n))
+
+let coqZ p n =
+ if Z.ge n Z.zero
+ then fprintf p "%s" (Z.to_string n)
+ else fprintf p "(%s)" (Z.to_string n)
+
+(* Coq strings *)
+
+let coqstring p s =
+ fprintf p "\"%s\"" (camlstring_of_coqstring s)
+
+(* Identifiers *)
+
+exception Not_an_identifier
+
+let sanitize s =
+ let s' = Bytes.create (String.length s) in
+ for i = 0 to String.length s - 1 do
+ Bytes.set s' i
+ (match String.get s i with
+ | 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' as c -> c
+ | ' ' | '$' -> '_'
+ | _ -> raise Not_an_identifier)
+ done;
+ Bytes.to_string s'
+
+let temp_names : (ident, string) Hashtbl.t = Hashtbl.create 17
+
+let ident p id =
+ try
+ let s = Hashtbl.find string_of_atom id in
+ fprintf p "_%s" (sanitize s)
+ with Not_found | Not_an_identifier ->
+ try
+ let s = Hashtbl.find temp_names id in
+ fprintf p "%s" s
+ with Not_found ->
+ positive p id
+
+let iter_hashtbl_sorted (h: ('a, string) Hashtbl.t) (f: 'a * string -> unit) =
+ List.iter f
+ (List.fast_sort (fun (k1, d1) (k2, d2) -> String.compare d1 d2)
+ (Hashtbl.fold (fun k d accu -> (k, d) :: accu) h []))
+
+let define_idents p =
+ iter_hashtbl_sorted
+ string_of_atom
+ (fun (id, name) ->
+ try
+ if !use_canonical_atoms && id = pos_of_string name then
+ fprintf p "Definition _%s : ident := $\"%s\".@ "
+ (sanitize name) name
+ else
+ fprintf p "Definition _%s : ident := %a.@ "
+ (sanitize name) positive id
+ with Not_an_identifier ->
+ ());
+ iter_hashtbl_sorted
+ temp_names
+ (fun (id, name) ->
+ fprintf p "Definition %s : ident := %a.@ "
+ name positive id);
+ fprintf p "@ "
+
+let name_temporary t =
+ if not (Hashtbl.mem string_of_atom t) && not (Hashtbl.mem temp_names t)
+ then begin
+ let t0 = first_unused_ident () in
+ let d = Z.succ (Z.sub (Z.Zpos t) (Z.Zpos t0)) in
+ Hashtbl.add temp_names t ("_t'" ^ Z.to_string d)
+ end
+
+let name_opt_temporary = function
+ | None -> ()
+ | Some id -> name_temporary id
+
+(* Raw attributes *)
+
+let attribute p a =
+ if a = noattr then
+ fprintf p "noattr"
+ else
+ fprintf p "{| attr_volatile := %B; attr_alignas := %a |}"
+ a.attr_volatile
+ (print_option coqN) a.attr_alignas
+
+(* Types *)
+
+let rec typ p t =
+ match attr_of_type t with
+ | { attr_volatile = false; attr_alignas = None} ->
+ rtyp p t
+ | { attr_volatile = true; attr_alignas = None} ->
+ fprintf p "(tvolatile %a)" rtyp t
+ | { attr_volatile = false; attr_alignas = Some n} ->
+ fprintf p "(talignas %a %a)" coqN n rtyp t
+ | { attr_volatile = true; attr_alignas = Some n} ->
+ fprintf p "(tvolatile_alignas %a %a)" coqN n rtyp t
+
+and rtyp p = function
+ | Tvoid -> fprintf p "tvoid"
+ | Ctypes.Tint(sz, sg, _) ->
+ fprintf p "%s" (
+ match sz, sg with
+ | I8, Signed -> "tschar"
+ | I8, Unsigned -> "tuchar"
+ | I16, Signed -> "tshort"
+ | I16, Unsigned -> "tushort"
+ | I32, Signed -> "tint"
+ | I32, Unsigned -> "tuint"
+ | IBool, _ -> "tbool")
+ | Ctypes.Tlong(sg, _) ->
+ fprintf p "%s" (
+ match sg with
+ | Signed -> "tlong"
+ | Unsigned -> "tulong")
+ | Ctypes.Tfloat(sz, _) ->
+ fprintf p "%s" (
+ match sz with
+ | F32 -> "tfloat"
+ | F64 -> "tdouble")
+ | Tpointer(t, _) ->
+ fprintf p "(tptr %a)" typ t
+ | Tarray(t, sz, _) ->
+ fprintf p "(tarray %a %ld)" typ t (Z.to_int32 sz)
+ | Tfunction(targs, tres, cc) ->
+ fprintf p "@[<hov 2>(Tfunction@ %a@ %a@ %a)@]"
+ typlist targs typ tres callconv cc
+ | Tstruct(id, _) ->
+ fprintf p "(Tstruct %a noattr)" ident id
+ | Tunion(id, _) ->
+ fprintf p "(Tunion %a noattr)" ident id
+
+and typlist p = function
+ | Tnil ->
+ fprintf p "Tnil"
+ | Tcons(t, tl) ->
+ fprintf p "@[<hov 2>(Tcons@ %a@ %a)@]" typ t typlist tl
+
+and callconv p cc =
+ if cc = cc_default
+ then fprintf p "cc_default"
+ else fprintf p "{|cc_vararg:=%a; cc_unproto:=%b; cc_structret:=%b|}"
+ (print_option coqZ) cc.cc_vararg cc.cc_unproto cc.cc_structret
+
+(* External functions *)
+
+let asttype p t =
+ fprintf p "%s"
+ (match t with
+ | AST.Tint -> "AST.Tint"
+ | AST.Tfloat -> "AST.Tfloat"
+ | AST.Tlong -> "AST.Tlong"
+ | AST.Tsingle -> "AST.Tsingle"
+ | AST.Tany32 -> "AST.Tany32"
+ | AST.Tany64 -> "AST.Tany64")
+
+let astrettype p = function
+ | AST.Tret t -> asttype p t
+ | AST.Tvoid -> fprintf p "AST.Tvoid"
+ | AST.Tint8signed -> fprintf p "AST.Tint8signed"
+ | AST.Tint8unsigned -> fprintf p "AST.Tint8unsigned"
+ | AST.Tint16signed -> fprintf p "AST.Tint16signed"
+ | AST.Tint16unsigned -> fprintf p "AST.Tint16unsigned"
+
+let name_of_chunk = function
+ | Mint8signed -> "Mint8signed"
+ | Mint8unsigned -> "Mint8unsigned"
+ | Mint16signed -> "Mint16signed"
+ | Mint16unsigned -> "Mint16unsigned"
+ | Mint32 -> "Mint32"
+ | Mint64 -> "Mint64"
+ | Mfloat32 -> "Mfloat32"
+ | Mfloat64 -> "Mfloat64"
+ | Many32 -> "Many32"
+ | Many64 -> "Many64"
+
+let signatur p sg =
+ fprintf p "@[<hov 2>(mksignature@ %a@ %a@ %a)@]"
+ (print_list asttype) sg.sig_args
+ astrettype sg.sig_res
+ callconv sg.sig_cc
+
+let external_function p = function
+ | EF_external(name, sg) ->
+ fprintf p "@[<hov 2>(EF_external %a@ %a)@]" coqstring name signatur sg
+ | EF_builtin(name, sg) ->
+ fprintf p "@[<hov 2>(EF_builtin %a@ %a)@]" coqstring name signatur sg
+ | EF_runtime(name, sg) ->
+ fprintf p "@[<hov 2>(EF_runtime %a@ %a)@]" coqstring name signatur sg
+ | EF_vload chunk ->
+ fprintf p "(EF_vload %s)" (name_of_chunk chunk)
+ | EF_vstore chunk ->
+ fprintf p "(EF_vstore %s)" (name_of_chunk chunk)
+ | EF_malloc -> fprintf p "EF_malloc"
+ | EF_free -> fprintf p "EF_free"
+ | EF_memcpy(sz, al) ->
+ fprintf p "(EF_memcpy %ld %ld)" (Z.to_int32 sz) (Z.to_int32 al)
+ | EF_annot(kind, text, targs) ->
+ fprintf p "(EF_annot %a %a %a)"
+ positive kind coqstring text (print_list asttype) targs
+ | EF_annot_val(kind, text, targ) ->
+ fprintf p "(EF_annot_val %a %a %a)"
+ positive kind coqstring text asttype targ
+ | EF_debug(kind, text, targs) ->
+ fprintf p "(EF_debug %a %a %a)"
+ positive kind positive text (print_list asttype) targs
+ | EF_inline_asm(text, sg, clob) ->
+ fprintf p "@[<hov 2>(EF_inline_asm %a@ %a@ %a)@]"
+ coqstring text
+ signatur sg
+ (print_list coqstring) clob
+ | EF_profiling(id, kind) ->
+ failwith "EF_profiling in ExportClight" (* should not happen *)
+
+(* Expressions *)
+
+let name_unop = function
+ | Onotbool -> "Onotbool"
+ | Onotint -> "Onotint"
+ | Oneg -> "Oneg"
+ | Oabsfloat -> "Oabsfloat"
+
+let name_binop = function
+ | Oadd -> "Oadd"
+ | Osub -> "Osub"
+ | Omul -> "Omul"
+ | Odiv -> "Odiv"
+ | Omod -> "Omod"
+ | Oand -> "Oand"
+ | Oor -> "Oor"
+ | Oxor -> "Oxor"
+ | Oshl -> "Oshl"
+ | Oshr -> "Oshr"
+ | Oeq -> "Oeq"
+ | Cop.One -> "One"
+ | Olt -> "Olt"
+ | Ogt -> "Ogt"
+ | Ole -> "Ole"
+ | Oge -> "Oge"
+ | Oexpect -> "Oexpect"
+
+let rec expr p = function
+ | Evar(id, t) ->
+ fprintf p "(Evar %a %a)" ident id typ t
+ | Etempvar(id, t) ->
+ fprintf p "(Etempvar %a %a)" ident id typ t
+ | Ederef(a1, t) ->
+ fprintf p "@[<hov 2>(Ederef@ %a@ %a)@]" expr a1 typ t
+ | Efield(a1, f, t) ->
+ fprintf p "@[<hov 2>(Efield@ %a@ %a@ %a)@]" expr a1 ident f typ t
+ | Econst_int(n, t) ->
+ fprintf p "(Econst_int %a %a)" coqint n typ t
+ | Econst_float(n, t) ->
+ fprintf p "(Econst_float %a %a)" coqfloat n typ t
+ | Econst_long(n, t) ->
+ fprintf p "(Econst_long %a %a)" coqint64 n typ t
+ | Econst_single(n, t) ->
+ fprintf p "(Econst_single %a %a)" coqsingle n typ t
+ | Eunop(op, a1, t) ->
+ fprintf p "@[<hov 2>(Eunop %s@ %a@ %a)@]"
+ (name_unop op) expr a1 typ t
+ | Eaddrof(a1, t) ->
+ fprintf p "@[<hov 2>(Eaddrof@ %a@ %a)@]" expr a1 typ t
+ | Ebinop(op, a1, a2, t) ->
+ fprintf p "@[<hov 2>(Ebinop %s@ %a@ %a@ %a)@]"
+ (name_binop op) expr a1 expr a2 typ t
+ | Ecast(a1, t) ->
+ fprintf p "@[<hov 2>(Ecast@ %a@ %a)@]" expr a1 typ t
+ | Esizeof(t1, t) ->
+ fprintf p "(Esizeof %a %a)" typ t1 typ t
+ | Ealignof(t1, t) ->
+ fprintf p "(Ealignof %a %a)" typ t1 typ t
+
+(* Statements *)
+
+let rec stmt p = function
+ | Sskip ->
+ fprintf p "Sskip"
+ | Sassign(e1, e2) ->
+ fprintf p "@[<hov 2>(Sassign@ %a@ %a)@]" expr e1 expr e2
+ | Sset(id, e2) ->
+ fprintf p "@[<hov 2>(Sset %a@ %a)@]" ident id expr e2
+ | Scall(optid, e1, el) ->
+ fprintf p "@[<hov 2>(Scall %a@ %a@ %a)@]"
+ (print_option ident) optid expr e1 (print_list expr) el
+ | Sbuiltin(optid, ef, tyl, el) ->
+ fprintf p "@[<hov 2>(Sbuiltin %a@ %a@ %a@ %a)@]"
+ (print_option ident) optid
+ external_function ef
+ typlist tyl
+ (print_list expr) el
+ | Ssequence(Sskip, s2) ->
+ stmt p s2
+ | Ssequence(s1, Sskip) ->
+ stmt p s1
+ | Ssequence(s1, s2) ->
+ fprintf p "@[<hv 2>(Ssequence@ %a@ %a)@]" stmt s1 stmt s2
+ | Sifthenelse(e, s1, s2) ->
+ fprintf p "@[<hv 2>(Sifthenelse %a@ %a@ %a)@]" expr e stmt s1 stmt s2
+ | Sloop (Ssequence (Sifthenelse(e, Sskip, Sbreak), s), Sskip) ->
+ fprintf p "@[<hv 2>(Swhile@ %a@ %a)@]" expr e stmt s
+ | Sloop (Ssequence (Ssequence(Sskip, Sifthenelse(e, Sskip, Sbreak)), s), Sskip) ->
+ fprintf p "@[<hv 2>(Swhile@ %a@ %a)@]" expr e stmt s
+ | Sloop(s1, s2) ->
+ fprintf p "@[<hv 2>(Sloop@ %a@ %a)@]" stmt s1 stmt s2
+ | Sbreak ->
+ fprintf p "Sbreak"
+ | Scontinue ->
+ fprintf p "Scontinue"
+ | Sswitch(e, cases) ->
+ fprintf p "@[<hv 2>(Sswitch %a@ %a)@]" expr e lblstmts cases
+ | Sreturn e ->
+ fprintf p "@[<hv 2>(Sreturn %a)@]" (print_option expr) e
+ | Slabel(lbl, s1) ->
+ fprintf p "@[<hv 2>(Slabel %a@ %a)@]" ident lbl stmt s1
+ | Sgoto lbl ->
+ fprintf p "(Sgoto %a)" ident lbl
+
+and lblstmts p = function
+ | LSnil ->
+ (fprintf p "LSnil")
+ | LScons(lbl, s, ls) ->
+ fprintf p "@[<hv 2>(LScons %a@ %a@ %a)@]"
+ (print_option coqZ) lbl stmt s lblstmts ls
+
+let print_function p (id, f) =
+ fprintf p "Definition f_%s := {|@ " (sanitize (extern_atom id));
+ fprintf p " fn_return := %a;@ " typ f.fn_return;
+ fprintf p " fn_callconv := %a;@ " callconv f.fn_callconv;
+ fprintf p " fn_params := %a;@ " (print_list (print_pair ident typ)) f.fn_params;
+ fprintf p " fn_vars := %a;@ " (print_list (print_pair ident typ)) f.fn_vars;
+ fprintf p " fn_temps := %a;@ " (print_list (print_pair ident typ)) f.fn_temps;
+ fprintf p " fn_body :=@ ";
+ stmt p f.fn_body;
+ fprintf p "@ |}.@ @ "
+
+let init_data p = function
+ | Init_int8 n -> fprintf p "Init_int8 %a" coqint n
+ | Init_int16 n -> fprintf p "Init_int16 %a" coqint n
+ | Init_int32 n -> fprintf p "Init_int32 %a" coqint n
+ | Init_int64 n -> fprintf p "Init_int64 %a" coqint64 n
+ | Init_float32 n -> fprintf p "Init_float32 %a" coqsingle n
+ | Init_float64 n -> fprintf p "Init_float64 %a" coqfloat n
+ | Init_space n -> fprintf p "Init_space %a" coqZ n
+ | Init_addrof(id,ofs) -> fprintf p "Init_addrof %a %a" ident id coqptrofs ofs
+
+let print_variable p (id, v) =
+ fprintf p "Definition v_%s := {|@ " (sanitize (extern_atom id));
+ fprintf p " gvar_info := %a;@ " typ v.gvar_info;
+ fprintf p " gvar_init := %a;@ " (print_list init_data) v.gvar_init;
+ fprintf p " gvar_readonly := %B;@ " v.gvar_readonly;
+ fprintf p " gvar_volatile := %B@ " v.gvar_volatile;
+ fprintf p "|}.@ @ "
+
+let print_globdef p (id, gd) =
+ match gd with
+ | Gfun(Ctypes.Internal f) -> print_function p (id, f)
+ | Gfun(Ctypes.External _) -> ()
+ | Gvar v -> print_variable p (id, v)
+
+let print_ident_globdef p = function
+ | (id, Gfun(Ctypes.Internal f)) ->
+ fprintf p "(%a, Gfun(Internal f_%s))" ident id (sanitize (extern_atom id))
+ | (id, Gfun(Ctypes.External(ef, targs, tres, cc))) ->
+ fprintf p "@[<hov 2>(%a,@ @[<hov 2>Gfun(External %a@ %a@ %a@ %a))@]@]"
+ ident id external_function ef typlist targs typ tres callconv cc
+ | (id, Gvar v) ->
+ fprintf p "(%a, Gvar v_%s)" ident id (sanitize (extern_atom id))
+
+(* Composite definitions *)
+
+let print_composite_definition p (Composite(id, su, m, a)) =
+ fprintf p "@[<hv 2>Composite %a %s@ %a@ %a@]"
+ ident id
+ (match su with Struct -> "Struct" | Union -> "Union")
+ (print_list (print_pair ident typ)) m
+ attribute a
+
+(* The prologue *)
+
+let prologue = "\
+From Coq Require Import String List ZArith.\n\
+From compcert Require Import Coqlib Integers Floats AST Ctypes Cop Clight Clightdefs.\n\
+Import Clightdefs.ClightNotations.\n\
+Local Open Scope Z_scope.\n\
+Local Open Scope string_scope.\n\
+Local Open Scope clight_scope.\n"
+
+(* Naming the compiler-generated temporaries occurring in the program *)
+
+let rec name_expr = function
+ | Evar(id, t) -> ()
+ | Etempvar(id, t) -> name_temporary id
+ | Ederef(a1, t) -> name_expr a1
+ | Efield(a1, f, t) -> name_expr a1
+ | Econst_int(n, t) -> ()
+ | Econst_float(n, t) -> ()
+ | Econst_long(n, t) -> ()
+ | Econst_single(n, t) -> ()
+ | Eunop(op, a1, t) -> name_expr a1
+ | Eaddrof(a1, t) -> name_expr a1
+ | Ebinop(op, a1, a2, t) -> name_expr a1; name_expr a2
+ | Ecast(a1, t) -> name_expr a1
+ | Esizeof(t1, t) -> ()
+ | Ealignof(t1, t) -> ()
+
+let rec name_stmt = function
+ | Sskip -> ()
+ | Sassign(e1, e2) -> name_expr e1; name_expr e2
+ | Sset(id, e2) -> name_temporary id; name_expr e2
+ | Scall(optid, e1, el) ->
+ name_opt_temporary optid; name_expr e1; List.iter name_expr el
+ | Sbuiltin(optid, ef, tyl, el) ->
+ name_opt_temporary optid; List.iter name_expr el
+ | Ssequence(s1, s2) -> name_stmt s1; name_stmt s2
+ | Sifthenelse(e, s1, s2) -> name_expr e; name_stmt s1; name_stmt s2
+ | Sloop(s1, s2) -> name_stmt s1; name_stmt s2
+ | Sbreak -> ()
+ | Scontinue -> ()
+ | Sswitch(e, cases) -> name_expr e; name_lblstmts cases
+ | Sreturn (Some e) -> name_expr e
+ | Sreturn None -> ()
+ | Slabel(lbl, s1) -> name_stmt s1
+ | Sgoto lbl -> ()
+
+and name_lblstmts = function
+ | LSnil -> ()
+ | LScons(lbl, s, ls) -> name_stmt s; name_lblstmts ls
+
+let name_function f =
+ List.iter (fun (id, ty) -> name_temporary id) f.fn_temps;
+ name_stmt f.fn_body
+
+let name_globdef (id, g) =
+ match g with
+ | Gfun(Ctypes.Internal f) -> name_function f
+ | _ -> ()
+
+let name_program p =
+ List.iter name_globdef p.Ctypes.prog_defs
+
+(* Information about this run of clightgen *)
+
+let print_clightgen_info p sourcefile normalized =
+ fprintf p "@[<v 2>Module Info.";
+ fprintf p "@ Definition version := %S." Version.version;
+ fprintf p "@ Definition build_number := %S." Version.buildnr;
+ fprintf p "@ Definition build_tag := %S." Version.tag;
+ fprintf p "@ Definition build_branch := %S." Version.branch;
+ fprintf p "@ Definition arch := %S." Configuration.arch;
+ fprintf p "@ Definition model := %S." Configuration.model;
+ fprintf p "@ Definition abi := %S." Configuration.abi;
+ fprintf p "@ Definition bitsize := %d." (if Archi.ptr64 then 64 else 32);
+ fprintf p "@ Definition big_endian := %B." Archi.big_endian;
+ fprintf p "@ Definition source_file := %S." sourcefile;
+ fprintf p "@ Definition normalized := %B." normalized;
+ fprintf p "@]@ End Info.@ @ "
+
+(* All together *)
+
+let print_program p prog sourcefile normalized =
+ Hashtbl.clear temp_names;
+ name_program prog;
+ fprintf p "@[<v 0>";
+ fprintf p "%s" prologue;
+ print_clightgen_info p sourcefile normalized;
+ define_idents p;
+ List.iter (print_globdef p) prog.Ctypes.prog_defs;
+ fprintf p "Definition composites : list composite_definition :=@ ";
+ print_list print_composite_definition p prog.prog_types;
+ fprintf p ".@ @ ";
+ fprintf p "Definition global_definitions : list (ident * globdef fundef type) :=@ ";
+ print_list print_ident_globdef p prog.Ctypes.prog_defs;
+ fprintf p ".@ @ ";
+ fprintf p "Definition public_idents : list ident :=@ ";
+ print_list ident p prog.Ctypes.prog_public;
+ fprintf p ".@ @ ";
+ fprintf p "Definition prog : Clight.program := @ ";
+ fprintf p " mkprogram composites global_definitions public_idents %a Logic.I.@ @ "
+ ident prog.Ctypes.prog_main;
+ fprintf p "@]@."
diff --git a/extraction/debug/Asmgen.ml b/extraction/debug/Asmgen.ml
new file mode 100644
index 00000000..e8dfde13
--- /dev/null
+++ b/extraction/debug/Asmgen.ml
@@ -0,0 +1,126 @@
+(* Replace transl_op by this wrapper to print the op *)
+
+let transl_op op args res0 k =
+ match op with
+ | Omove -> (Printf.eprintf "Omove\n"; thereal_transl_op op args res0 k)
+ | Ointconst _ -> (Printf.eprintf "Ointconst _\n"; thereal_transl_op op args res0 k)
+ | Olongconst _ -> (Printf.eprintf "Olongconst _\n"; thereal_transl_op op args res0 k)
+ | Ofloatconst _ -> (Printf.eprintf "Ofloatconst _\n"; thereal_transl_op op args res0 k)
+ | Osingleconst _ -> (Printf.eprintf "Osingleconst _\n"; thereal_transl_op op args res0 k)
+ | Oaddrsymbol _ -> (Printf.eprintf "Oaddrsymbol _ _\n"; thereal_transl_op op args res0 k)
+ | Oaddrstack _ -> (Printf.eprintf "Oaddrstack _\n"; thereal_transl_op op args res0 k)
+ | Ocast8signed -> (Printf.eprintf "Ocast8signed\n"; thereal_transl_op op args res0 k)
+ | Ocast16signed -> (Printf.eprintf "Ocast16signed\n"; thereal_transl_op op args res0 k)
+ | Oadd -> (Printf.eprintf "Oadd\n"; thereal_transl_op op args res0 k)
+ | Oaddimm _ -> (Printf.eprintf "Oaddimm _\n"; thereal_transl_op op args res0 k)
+ | Oneg -> (Printf.eprintf "Oneg\n"; thereal_transl_op op args res0 k)
+ | Osub -> (Printf.eprintf "Osub\n"; thereal_transl_op op args res0 k)
+ | Omul -> (Printf.eprintf "Omul\n"; thereal_transl_op op args res0 k)
+ | Omulhs -> (Printf.eprintf "Omulhs\n"; thereal_transl_op op args res0 k)
+ | Omulhu -> (Printf.eprintf "Omulhu\n"; thereal_transl_op op args res0 k)
+ | Odiv -> (Printf.eprintf "Odiv\n"; thereal_transl_op op args res0 k)
+ | Odivu -> (Printf.eprintf "Odivu\n"; thereal_transl_op op args res0 k)
+ | Omod -> (Printf.eprintf "Omod\n"; thereal_transl_op op args res0 k)
+ | Omodu -> (Printf.eprintf "Omodu\n"; thereal_transl_op op args res0 k)
+ | Oand -> (Printf.eprintf "Oand\n"; thereal_transl_op op args res0 k)
+ | Oandimm _ -> (Printf.eprintf "Oandimm _\n"; thereal_transl_op op args res0 k)
+ | Oor -> (Printf.eprintf "Oor\n"; thereal_transl_op op args res0 k)
+ | Oorimm _ -> (Printf.eprintf "Oorimm _\n"; thereal_transl_op op args res0 k)
+ | Oxor -> (Printf.eprintf "Oxor\n"; thereal_transl_op op args res0 k)
+ | Oxorimm _ -> (Printf.eprintf "Oxorimm _\n"; thereal_transl_op op args res0 k)
+ | Oshl -> (Printf.eprintf "Oshl\n"; thereal_transl_op op args res0 k)
+ | Oshlimm _ -> (Printf.eprintf "Oshlimm _\n"; thereal_transl_op op args res0 k)
+ | Oshr -> (Printf.eprintf "Oshr\n"; thereal_transl_op op args res0 k)
+ | Oshrimm _ -> (Printf.eprintf "Oshrimm _\n"; thereal_transl_op op args res0 k)
+ | Oshruimm _ -> (Printf.eprintf "Oshruimm _\n"; thereal_transl_op op args res0 k)
+ | Oshrximm _ -> (Printf.eprintf "Oshrximm _\n"; thereal_transl_op op args res0 k)
+ | Olowlong -> (Printf.eprintf "Olowlong\n"; thereal_transl_op op args res0 k)
+ | Ocast32signed -> (Printf.eprintf "Ocast32signed\n"; thereal_transl_op op args res0 k)
+ | Ocast32unsigned -> (Printf.eprintf "Ocast32unsigned\n"; thereal_transl_op op args res0 k)
+ | Oaddl -> (Printf.eprintf "Oaddl\n"; thereal_transl_op op args res0 k)
+ | Oaddlimm _ -> (Printf.eprintf "Oaddlimm _\n"; thereal_transl_op op args res0 k)
+ | Onegl -> (Printf.eprintf "Onegl\n"; thereal_transl_op op args res0 k)
+ | Osubl -> (Printf.eprintf "Osubl\n"; thereal_transl_op op args res0 k)
+ | Omull -> (Printf.eprintf "Omull\n"; thereal_transl_op op args res0 k)
+ | Omullhs -> (Printf.eprintf "Omullhs\n"; thereal_transl_op op args res0 k)
+ | Omullhu -> (Printf.eprintf "Omullhu\n"; thereal_transl_op op args res0 k)
+ | Odivl -> (Printf.eprintf "Odivl\n"; thereal_transl_op op args res0 k)
+ | Odivlu -> (Printf.eprintf "Odivlu\n"; thereal_transl_op op args res0 k)
+ | Omodl -> (Printf.eprintf "Omodl\n"; thereal_transl_op op args res0 k)
+ | Omodlu -> (Printf.eprintf "Omodlu\n"; thereal_transl_op op args res0 k)
+ | Oandl -> (Printf.eprintf "Oandl\n"; thereal_transl_op op args res0 k)
+ | Oandlimm _ -> (Printf.eprintf "Oandlimm _\n"; thereal_transl_op op args res0 k)
+ | Oorl -> (Printf.eprintf "Oorl\n"; thereal_transl_op op args res0 k)
+ | Oorlimm _ -> (Printf.eprintf "Oorlimm _\n"; thereal_transl_op op args res0 k)
+ | Oxorl -> (Printf.eprintf "Oxorl\n"; thereal_transl_op op args res0 k)
+ | Oxorlimm _ -> (Printf.eprintf "Oxorlimm _\n"; thereal_transl_op op args res0 k)
+ | Oshll -> (Printf.eprintf "Oshll\n"; thereal_transl_op op args res0 k)
+ | Oshllimm _ -> (Printf.eprintf "Oshllimm _\n"; thereal_transl_op op args res0 k)
+ | Oshrlu -> (Printf.eprintf "Oshrlu\n"; thereal_transl_op op args res0 k)
+ | Oshrluimm _ -> (Printf.eprintf "Oshrluimm\n"; thereal_transl_op op args res0 k)
+ | Oshrxlimm _ -> (Printf.eprintf "Oshrxlimm\n"; thereal_transl_op op args res0 k)
+ | Onegf -> (Printf.eprintf "Onegf\n"; thereal_transl_op op args res0 k)
+ | Oabsf -> (Printf.eprintf "Oabsf\n"; thereal_transl_op op args res0 k)
+ | Oaddf -> (Printf.eprintf "Oaddf\n"; thereal_transl_op op args res0 k)
+ | Osubf -> (Printf.eprintf "Osubf\n"; thereal_transl_op op args res0 k)
+ | Omulf -> (Printf.eprintf "Omulf\n"; thereal_transl_op op args res0 k)
+ | Odivf -> (Printf.eprintf "Odivf\n"; thereal_transl_op op args res0 k)
+ | Onegfs -> (Printf.eprintf "Onegfs\n"; thereal_transl_op op args res0 k)
+ | Oabsfs -> (Printf.eprintf "Oabsfs\n"; thereal_transl_op op args res0 k)
+ | Oaddfs -> (Printf.eprintf "Oaddfs\n"; thereal_transl_op op args res0 k)
+ | Osubfs -> (Printf.eprintf "Osubfs\n"; thereal_transl_op op args res0 k)
+ | Omulfs -> (Printf.eprintf "Omulfs\n"; thereal_transl_op op args res0 k)
+ | Odivfs -> (Printf.eprintf "Odivfs\n"; thereal_transl_op op args res0 k)
+ | Osingleoffloat -> (Printf.eprintf "Osingleoffloat\n"; thereal_transl_op op args res0 k)
+ | Ofloatofsingle -> (Printf.eprintf "Ofloatofsingle\n"; thereal_transl_op op args res0 k)
+ | Ointoffloat -> (Printf.eprintf "Ointoffloat\n"; thereal_transl_op op args res0 k)
+ | Ointuoffloat -> (Printf.eprintf "Ointuoffloat\n"; thereal_transl_op op args res0 k)
+ | Ofloatofint -> (Printf.eprintf "Ofloatofint\n"; thereal_transl_op op args res0 k)
+ | Ofloatofintu -> (Printf.eprintf "Ofloatofintu\n"; thereal_transl_op op args res0 k)
+ | Ointofsingle -> (Printf.eprintf "Ointofsingle\n"; thereal_transl_op op args res0 k)
+ | Ointuofsingle -> (Printf.eprintf "Ointuofsingle\n"; thereal_transl_op op args res0 k)
+ | Osingleofint -> (Printf.eprintf "Osingleofint\n"; thereal_transl_op op args res0 k)
+ | Osingleofintu -> (Printf.eprintf "Osingleofintu\n"; thereal_transl_op op args res0 k)
+ | Olongoffloat -> (Printf.eprintf "Olongoffloat\n"; thereal_transl_op op args res0 k)
+ | Olonguoffloat -> (Printf.eprintf "Olonguoffloat\n"; thereal_transl_op op args res0 k)
+ | Ofloatoflong -> (Printf.eprintf "Ofloatoflong\n"; thereal_transl_op op args res0 k)
+ | Ofloatoflongu -> (Printf.eprintf "Ofloatoflongu\n"; thereal_transl_op op args res0 k)
+ | Olongofsingle -> (Printf.eprintf "Olongofsingle\n"; thereal_transl_op op args res0 k)
+ | Olonguofsingle -> (Printf.eprintf "Olonguofsingle\n"; thereal_transl_op op args res0 k)
+ | Osingleoflong -> (Printf.eprintf "Osingleoflong\n"; thereal_transl_op op args res0 k)
+ | Osingleoflongu -> (Printf.eprintf "Osingleoflongu\n"; thereal_transl_op op args res0 k)
+ | Ocmp _ -> (Printf.eprintf "Ocmp _\n"; thereal_transl_op op args res0 k)
+ | _ -> (Printf.eprintf "_\n"; thereal_transl_op op args res0 k)
+
+let transl_instr f i x k =
+ match i with
+ | Mgetstack _ -> (Printf.eprintf "Mgetstack\n"; thereal_transl_instr f i x k)
+ | Msetstack _ -> (Printf.eprintf "Msetstack\n"; thereal_transl_instr f i x k)
+ | Mgetparam _ -> (Printf.eprintf "Mgetparam\n"; thereal_transl_instr f i x k)
+ | Mop _ -> (Printf.eprintf "Mop\n"; thereal_transl_instr f i x k)
+ | Mload _ -> (Printf.eprintf "Mload\n"; thereal_transl_instr f i x k)
+ | Mstore _ -> (Printf.eprintf "Mstore\n"; thereal_transl_instr f i x k)
+ | Mcall _ -> (Printf.eprintf "Mcall\n"; thereal_transl_instr f i x k)
+ | Mtailcall _ -> (Printf.eprintf "Mtailcall\n"; thereal_transl_instr f i x k)
+ | Mbuiltin _ -> (Printf.eprintf "Mbuiltin\n"; thereal_transl_instr f i x k)
+ | Mlabel _ -> (Printf.eprintf "Mlabel\n"; thereal_transl_instr f i x k)
+ | Mgoto _ -> (Printf.eprintf "Mgoto\n"; thereal_transl_instr f i x k)
+ | Mcond _ -> (Printf.eprintf "Mcond\n"; thereal_transl_instr f i x k)
+ | Mjumptable _ -> (Printf.eprintf "Mjumptable\n"; thereal_transl_instr f i x k)
+ | Mreturn -> (Printf.eprintf "Mreturn\n"; thereal_transl_instr f i x k)
+
+let transl_cbranch c a l k =
+ match c, a with
+ | Ccomp _, _ :: _ :: [] -> (Printf.eprintf "Ccomp\n"; thereal_transl_cbranch c a l k)
+ | Ccompu _, _ :: _ :: [] -> (Printf.eprintf "Ccompu\n"; thereal_transl_cbranch c a l k)
+ | Ccompimm (_, _), _ :: [] -> (Printf.eprintf "Ccompimm\n"; thereal_transl_cbranch c a l k)
+ | Ccompuimm (_, _), _ :: [] -> (Printf.eprintf "Ccompuimm\n"; thereal_transl_cbranch c a l k)
+ | Ccompl _, _ :: _ :: [] -> (Printf.eprintf "Ccompl\n"; thereal_transl_cbranch c a l k)
+ | Ccomplu _, _ :: _ :: [] -> (Printf.eprintf "Ccomplu\n"; thereal_transl_cbranch c a l k)
+ | Ccomplimm (_, _), _ :: [] -> (Printf.eprintf "Ccomplimm\n"; thereal_transl_cbranch c a l k)
+ | Ccompluimm (_, _), _ :: [] -> (Printf.eprintf "Ccompulimm\n"; thereal_transl_cbranch c a l k)
+ | Ccompf _, _ :: _ :: [] -> (Printf.eprintf "Ccompf\n"; thereal_transl_cbranch c a l k)
+ | Cnotcompf _, _ :: _ :: [] -> (Printf.eprintf "Cnotcompf\n"; thereal_transl_cbranch c a l k)
+ | Ccompfs _, _ :: _ :: [] -> (Printf.eprintf "Ccomps\n"; thereal_transl_cbranch c a l k)
+ | Cnotcompfs _, _ :: _ :: [] -> (Printf.eprintf "Cnotcomps\n"; thereal_transl_cbranch c a l k)
+ | _ -> (Printf.eprintf "OOPS\n"; thereal_transl_cbranch c a l k)
diff --git a/extraction/extraction.v b/extraction/extraction.vexpand
index 8c2a52a2..b61a97d7 100644
--- a/extraction/extraction.v
+++ b/extraction/extraction.vexpand
@@ -14,6 +14,7 @@
(* *)
(* *********************************************************************)
+Require Import ZArith PeanoNat.
Require Coqlib.
Require Wfsimpl.
Require DecidableClass Decidableplus.
@@ -35,6 +36,10 @@ Require Clight.
Require Compiler.
Require Parser.
Require Initializers.
+Require Asmaux.
+
+Require CSE3.
+Require CSE3analysis.
(* Standard lib *)
Require Import ExtrOcamlBasic.
@@ -84,6 +89,9 @@ Extract Inlined Constant Inlining.inlining_info => "Inliningaux.inlining_info".
Extract Inlined Constant Inlining.inlining_analysis => "Inliningaux.inlining_analysis".
Extraction Inline Inlining.ret Inlining.bind.
+(* Loop invariant code motion *)
+Extract Inlined Constant LICM.gen_injections => "LICMaux.gen_injections".
+
(* Allocation *)
Extract Constant Allocation.regalloc => "Regalloc.regalloc".
@@ -109,12 +117,55 @@ Extract Constant Compopts.optim_constprop =>
"fun _ -> !Clflags.option_fconstprop".
Extract Constant Compopts.optim_CSE =>
"fun _ -> !Clflags.option_fcse".
+Extract Constant Compopts.optim_CSE2 =>
+ "fun _ -> !Clflags.option_fcse2".
+Extract Constant Compopts.optim_CSE3 =>
+ "fun _ -> !Clflags.option_fcse3".
+Extract Constant Compopts.optim_CSE3_alias_analysis =>
+ "fun _ -> !Clflags.option_fcse3_alias_analysis".
+Extract Constant Compopts.optim_CSE3_across_calls =>
+ "fun _ -> !Clflags.option_fcse3_across_calls".
+Extract Constant Compopts.optim_CSE3_across_merges =>
+ "fun _ -> !Clflags.option_fcse3_across_merges".
+Extract Constant Compopts.optim_CSE3_glb =>
+ "fun _ -> !Clflags.option_fcse3_glb".
+Extract Constant Compopts.optim_CSE3_trivial_ops =>
+ "fun _ -> !Clflags.option_fcse3_trivial_ops".
+Extract Constant Compopts.optim_CSE3_conditions =>
+ "fun _ -> !Clflags.option_fcse3_conditions".
+Extract Constant Compopts.optim_move_loop_invariants =>
+ "fun _ -> !Clflags.option_fmove_loop_invariants".
+
Extract Constant Compopts.optim_redundancy =>
"fun _ -> !Clflags.option_fredundancy".
+Extract Constant Compopts.optim_postpass =>
+ "fun _ -> !Clflags.option_fpostpass".
Extract Constant Compopts.thumb =>
"fun _ -> !Clflags.option_mthumb".
Extract Constant Compopts.debug =>
"fun _ -> !Clflags.option_g".
+Extract Constant Compopts.optim_globaladdrtmp =>
+ "fun _ -> !Clflags.option_fglobaladdrtmp".
+Extract Constant Compopts.optim_globaladdroffset =>
+ "fun _ -> !Clflags.option_fglobaladdroffset".
+Extract Constant Compopts.optim_xsaddr =>
+ "fun _ -> !Clflags.option_fxsaddr".
+Extract Constant Compopts.optim_addx =>
+ "fun _ -> !Clflags.option_faddx".
+Extract Constant Compopts.optim_madd =>
+ "fun _ -> !Clflags.option_fmadd".
+Extract Constant Compopts.optim_coalesce_mem =>
+ "fun _ -> !Clflags.option_fcoalesce_mem".
+Extract Constant Compopts.optim_forward_moves =>
+ "fun _ -> !Clflags.option_fforward_moves".
+Extract Constant Compopts.va_strict =>
+ "fun _ -> false".
+Extract Constant Compopts.all_loads_nontrap =>
+ "fun _ -> !Clflags.option_all_loads_nontrap".
+Extract Constant Compopts.profile_arcs =>
+"fun _ -> !Clflags.option_profile_arcs".
+Extract Constant Compopts.branch_probabilities =>
+ "fun _ -> !Clflags.option_fbranch_probabilities".
(* Compiler *)
Extract Constant Compiler.print_Clight => "PrintClight.print_if".
@@ -124,9 +175,18 @@ Extract Constant Compiler.print_LTL => "PrintLTL.print_if".
Extract Constant Compiler.print_Mach => "PrintMach.print_if".
Extract Constant Compiler.print => "fun (f: 'a -> unit) (x: 'a) -> f x; x".
Extract Constant Compiler.time => "Timing.time_coq".
-
+Extract Constant Compopts.time => "Timing.time_coq".
(*Extraction Inline Compiler.apply_total Compiler.apply_partial.*)
+(* Profiling *)
+Extract Constant AST.profiling_id => "Digest.t".
+Extract Constant AST.profiling_id_eq => "Digest.equal".
+Extract Constant Profiling.function_id => "Profilingaux.function_id".
+Extract Constant Profiling.branch_id => "Profilingaux.branch_id".
+Extract Constant ProfilingExploit.function_id => "Profilingaux.function_id".
+Extract Constant ProfilingExploit.branch_id => "Profilingaux.branch_id".
+Extract Constant ProfilingExploit.condition_oracle => "Profilingaux.condition_oracle".
+
(* Cabs *)
Extract Constant Cabs.loc =>
"{ lineno : int;
@@ -137,6 +197,12 @@ Extract Constant Cabs.loc =>
Extract Inlined Constant Cabs.string => "String.t".
Extract Constant Cabs.char_code => "int64".
+Extract Inlined Constant CSE3.preanalysis => "CSE3analysisaux.preanalysis".
+
+Extract Inductive HashedSet.PSet_internals.pset => "HashedSetaux.pset" [ "HashedSetaux.empty" "HashedSetaux.node" ] "HashedSetaux.pset_match".
+
+Extract Inlined Constant HashedSet.PSet_internals.pset_eq => "(==)" (* "HashedSetaux.eq" *).
+
(* Processor-specific extraction directives *)
Load extractionMachdep.
@@ -158,7 +224,9 @@ Set Extraction AccessOpaque.
Cd "extraction".
-Separate Extraction
+Separate Extraction
+ Z.ldiff Z.lnot Nat.leb
+ CSE3analysis.eq_cond_depends_on_mem CSE3analysis.apply_instr'
Compiler.transf_c_program Compiler.transf_cminor_program
Cexec.do_initial_state Cexec.do_step Cexec.at_final_state
Ctypes.merge_attributes Ctypes.remove_attributes Ctypes.build_composite_env
@@ -177,7 +245,9 @@ Separate Extraction
Machregs.mregs_for_operation Machregs.mregs_for_builtin
Machregs.two_address_op Machregs.is_stack_reg
Machregs.destroyed_at_indirect_call
- AST.signature_main
+ AST.signature_main Asmaux
Floats.Float32.from_parsed Floats.Float.from_parsed
Globalenvs.Senv.invert_symbol
- Parser.translation_unit_file.
+ Parser.translation_unit_file
+ Compopts.optim_postpass
+ Archi.has_notrap_loads
diff --git a/kvx/Archi.v b/kvx/Archi.v
new file mode 100644
index 00000000..6d59a3d1
--- /dev/null
+++ b/kvx/Archi.v
@@ -0,0 +1,80 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Architecture-dependent parameters for MPPA KVX. Mostly copied from the Risc-V backend *)
+
+Require Import ZArith List.
+Require Import Binary Bits.
+
+Definition ptr64 := true.
+
+Definition big_endian := false.
+
+Definition align_int64 := 8%Z.
+Definition align_float64 := 8%Z.
+
+Definition splitlong := false.
+
+Lemma splitlong_ptr32: splitlong = true -> ptr64 = false.
+Proof.
+ unfold splitlong. congruence.
+Qed.
+
+(** FIXME - Check the properties below *)
+
+(** Section 7.3: "Except when otherwise stated, if the result of a
+ floating-point operation is NaN, it is the canonical NaN. The
+ canonical NaN has a positive sign and all significand bits clear
+ except the MSB, a.k.a. the quiet bit."
+ We need to extend the [choose_binop_pl] functions to account for
+ this case. *)
+
+Definition default_nan_64 := (false, iter_nat 51 _ xO xH).
+Definition default_nan_32 := (false, iter_nat 22 _ xO xH).
+
+(* Always choose the first NaN argument, if any *)
+
+Definition choose_nan_64 (l: list (bool * positive)) : bool * positive :=
+ match l with nil => default_nan_64 | n :: _ => n end.
+
+Definition choose_nan_32 (l: list (bool * positive)) : bool * positive :=
+ match l with nil => default_nan_32 | n :: _ => n end.
+
+Definition fpu_returns_default_qNaN := false.
+
+Lemma choose_nan_64_idem: forall n,
+ choose_nan_64 (n :: n :: nil) = choose_nan_64 (n :: nil).
+Proof. auto. Qed.
+
+Lemma choose_nan_32_idem: forall n,
+ choose_nan_32 (n :: n :: nil) = choose_nan_32 (n :: nil).
+Proof. auto. Qed.
+
+Definition fma_order {A: Type} (x y z: A) := (x, z, y).
+
+Definition fma_invalid_mul_is_nan := false.
+Definition float_of_single_preserves_sNaN := false.
+
+Global Opaque ptr64 big_endian splitlong
+ default_nan_64 choose_nan_64
+ default_nan_32 choose_nan_32
+ fma_order fma_invalid_mul_is_nan
+ float_of_single_preserves_sNaN.
+
+(** Whether to generate position-independent code or not *)
+
+Parameter pic_code: unit -> bool.
+
+Definition has_notrap_loads := true.
diff --git a/kvx/Asm.v b/kvx/Asm.v
new file mode 100644
index 00000000..fd20316c
--- /dev/null
+++ b/kvx/Asm.v
@@ -0,0 +1,758 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Abstract syntax for KVX textual assembly language.
+
+ Each emittable instruction is defined here. ';;' is also defined as an instruction.
+ The goal of this representation is to stay compatible with the rest of the generic backend of CompCert
+ We define [unfold : list bblock -> list instruction]
+ An Asm function is then defined as : [fn_sig], [fn_blocks], [fn_code], and a proof of [unfold fn_blocks = fn_code]
+ [fn_code] has no semantic. Instead, the semantic of Asm is given by using the AsmVLIW semantic on [fn_blocks] *)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import ExtValues.
+Require Import Memory.
+Require Import Events.
+Require Import Globalenvs.
+Require Import Smallstep.
+Require Import Locations.
+Require Stacklayout.
+Require Import Conventions.
+Require Export Asmvliw.
+Require Import Linking.
+Require Import Errors.
+
+(** Definitions for OCaml code *)
+Definition label := positive.
+
+(* Necessary definition for Asmexpandaux.mli *)
+Definition preg := preg.
+
+Inductive addressing : Type :=
+ | AOff (ofs: offset)
+ | AReg (ro: ireg)
+ | ARegXS (ro: ireg)
+.
+
+(** * Syntax *)
+Inductive instruction : Type :=
+ (** pseudo instructions *)
+ | Pallocframe (sz: Z) (pos: ptrofs) (**r allocate new stack frame *)
+ | Pfreeframe (sz: Z) (pos: ptrofs) (**r deallocate stack frame and restore previous frame *)
+ | Plabel (lbl: label) (**r define a code label *)
+ | Ploadsymbol (rd: ireg) (id: ident) (ofs: ptrofs) (**r load the address of a symbol *)
+ | Pbuiltin: external_function -> list (builtin_arg preg)
+ -> builtin_res preg -> instruction (**r built-in function (pseudo) *)
+ | Psemi (**r semi colon separating bundles *)
+ | Pnop (**r instruction that does nothing *)
+
+ (** Control flow instructions *)
+ | Pget (rd: ireg) (rs: preg) (**r get system register *)
+ | Pset (rd: preg) (rs: ireg) (**r set system register *)
+ | Pret (**r return *)
+ | Pcall (l: label) (**r function call *)
+ | Picall (rs: ireg) (**r function call on register *)
+ (* Pgoto is for tailcalls, Pj_l is for jumping to a particular label *)
+ | Pgoto (l: label) (**r goto *)
+ | Pigoto (rs: ireg) (**r goto from register *)
+ | Pj_l (l: label) (**r jump to label *)
+ | Pcb (bt: btest) (r: ireg) (l: label) (**r branch based on btest *)
+ | Pcbu (bt: btest) (r: ireg) (l: label) (**r branch based on btest with unsigned semantics *)
+ | Pjumptable (r: ireg) (labels: list label)
+
+ (* For builtins *)
+ | Ploopdo (count: ireg) (loopend: label)
+ | Pgetn (n: int) (dst: ireg)
+ | Psetn (n: int) (src: ireg)
+ | Pwfxl (n: int) (src: ireg)
+ | Pwfxm (n: int) (src: ireg)
+ | Pldu (dst: ireg) (addr: ireg)
+ | Plbzu (dst: ireg) (addr: ireg)
+ | Plhzu (dst: ireg) (addr: ireg)
+ | Plwzu (dst: ireg) (addr: ireg)
+ | Pawait
+ | Psleep
+ | Pstop
+ | Pbarrier
+ | Pfence
+ | Pdinval
+ | Pdinvall (addr: ireg)
+ | Pdtouchl (addr: ireg)
+ | Piinval
+ | Piinvals (addr: ireg)
+ | Pitouchl (addr: ireg)
+ | Pdzerol (addr: ireg)
+(*| Pafaddd (addr: ireg) (incr_res: ireg)
+ | Pafaddw (addr: ireg) (incr_res: ireg) *) (* see #157 *)
+ | Palclrd (dst: ireg) (addr: ireg)
+ | Palclrw (dst: ireg) (addr: ireg)
+ | Pclzll (rd rs: ireg)
+ | Pclzw (rd rs: ireg)
+ | Pctzll (rd rs: ireg)
+ | Pctzw (rd rs: ireg)
+ | Pstsud (rd rs1 rs2: ireg)
+
+ (** Loads *)
+ | Plb (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load byte *)
+ | Plbu (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load byte unsigned *)
+ | Plh (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load half word *)
+ | Plhu (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load half word unsigned *)
+ | Plw (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load int32 *)
+ | Plw_a (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load any32 *)
+ | Pld (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load int64 *)
+ | Pld_a (trap: trapping_mode) (rd: ireg) (ra: ireg) (ofs: addressing) (**r load any64 *)
+ | Pfls (trap: trapping_mode) (rd: freg) (ra: ireg) (ofs: addressing) (**r load float *)
+ | Pfld (trap: trapping_mode) (rd: freg) (ra: ireg) (ofs: addressing) (**r load 64-bit float *)
+ | Plq (rs: gpreg_q) (ra: ireg) (ofs: addressing) (**r load 2*64-bit *)
+ | Plo (rs: gpreg_o) (ra: ireg) (ofs: addressing) (**r load 4*64-bit *)
+
+ (** Stores *)
+ | Psb (rs: ireg) (ra: ireg) (ofs: addressing) (**r store byte *)
+ | Psh (rs: ireg) (ra: ireg) (ofs: addressing) (**r store half byte *)
+ | Psw (rs: ireg) (ra: ireg) (ofs: addressing) (**r store int32 *)
+ | Psw_a (rs: ireg) (ra: ireg) (ofs: addressing) (**r store any32 *)
+ | Psd (rs: ireg) (ra: ireg) (ofs: addressing) (**r store int64 *)
+ | Psd_a (rs: ireg) (ra: ireg) (ofs: addressing) (**r store any64 *)
+ | Pfss (rs: freg) (ra: ireg) (ofs: addressing) (**r store float *)
+ | Pfsd (rs: freg) (ra: ireg) (ofs: addressing) (**r store 64-bit float *)
+
+ | Psq (rs: gpreg_q) (ra: ireg) (ofs: addressing) (**r store 2*64-bit *)
+ | Pso (rs: gpreg_o) (ra: ireg) (ofs: addressing) (**r store 2*64-bit *)
+
+ (** Arith RR *)
+ | Pmv (rd rs: ireg) (**r register move *)
+ | Pnegw (rd rs: ireg) (**r negate word *)
+ | Pnegl (rd rs: ireg) (**r negate long *)
+ | Pcvtl2w (rd rs: ireg) (**r Convert Long to Word *)
+ | Psxwd (rd rs: ireg) (**r Sign Extend Word to Double Word *)
+ | Pzxwd (rd rs: ireg) (**r Zero Extend Word to Double Word *)
+
+ | Pextfz (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r extract bitfields unsigned *)
+ | Pextfs (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r extract bitfields signed *)
+
+ | Pextfzl (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r extract bitfields unsigned *)
+ | Pextfsl (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r extract bitfields signed *)
+
+ | Pinsf (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r insert bitfield *)
+ | Pinsfl (rd : ireg) (rs : ireg) (stop : Z) (start : Z) (**r insert bitfield *)
+
+ | Pfabsd (rd rs: ireg) (**r float absolute double *)
+ | Pfabsw (rd rs: ireg) (**r float absolute word *)
+ | Pfnegd (rd rs: ireg) (**r float negate double *)
+ | Pfnegw (rd rs: ireg) (**r float negate word *)
+ | Pfnarrowdw (rd rs: ireg) (**r float narrow 64 -> 32 bits *)
+ | Pfwidenlwd (rd rs: ireg) (**r float widen 32 -> 64 bits *)
+ | Pfloatwrnsz (rd rs: ireg) (**r Floating Point Conversion from integer (32 -> 32) *)
+ | Pfloatuwrnsz (rd rs: ireg) (**r Floating Point Conversion from integer (u32 -> 32) *)
+ | Pfloatudrnsz (rd rs: ireg) (**r Floating Point Conversion from unsigned integer (64 bits) *)
+ | Pfloatdrnsz (rd rs: ireg) (**r Floating Point Conversion from integer (64 bits) *)
+ | Pfixedwrzz (rd rs: ireg) (**r Integer conversion from floating point *)
+ | Pfixeduwrzz (rd rs: ireg) (**r Integer conversion from floating point (f32 -> 32 bits unsigned *)
+ | Pfixeddrzz (rd rs: ireg) (**r Integer conversion from floating point (i64 -> 64 bits) *)
+ | Pfixeddrzz_i32 (rd rs: ireg) (**r Integer conversion from floating point (i32 -> f64) *)
+ | Pfixedudrzz (rd rs: ireg) (**r unsigned Integer conversion from floating point (u64 -> 64 bits) *)
+ | Pfixedudrzz_i32 (rd rs: ireg) (**r unsigned Integer conversion from floating point (u32 -> 64 bits) *)
+
+ (** Arith RI32 *)
+ | Pmake (rd: ireg) (imm: int) (**r load immediate *)
+
+ (** Arith RI64 *)
+ | Pmakel (rd: ireg) (imm: int64) (**r load immediate long *)
+
+ (** Arith RF32 *)
+ | Pmakefs (rd: ireg) (imm: float32)
+
+ (** Arith RF64 *)
+ | Pmakef (rd: ireg) (imm: float)
+
+ (** Arith RRR *)
+ | Pcompw (it: itest) (rd rs1 rs2: ireg) (**r comparison word *)
+ | Pcompl (it: itest) (rd rs1 rs2: ireg) (**r comparison long *)
+ | Pfcompw (ft: ftest) (rd rs1 rs2: ireg) (**r comparison float *)
+ | Pfcompl (ft: ftest) (rd rs1 rs2: ireg) (**r comparison float64 *)
+
+ | Paddw (rd rs1 rs2: ireg) (**r add word *)
+ | Paddxw (shift : shift1_4) (rd rs1 rs2: ireg) (**r add word *)
+ | Psubw (rd rs1 rs2: ireg) (**r sub word *)
+ | Prevsubxw (shift : shift1_4) (rd rs1 rs2: ireg) (**r add word *)
+ | Pmulw (rd rs1 rs2: ireg) (**r mul word *)
+ | Pandw (rd rs1 rs2: ireg) (**r and word *)
+ | Pnandw (rd rs1 rs2: ireg) (**r nand word *)
+ | Porw (rd rs1 rs2: ireg) (**r or word *)
+ | Pnorw (rd rs1 rs2: ireg) (**r nor word *)
+ | Pxorw (rd rs1 rs2: ireg) (**r xor word *)
+ | Pnxorw (rd rs1 rs2: ireg) (**r xor word *)
+ | Pandnw (rd rs1 rs2: ireg) (**r andn word *)
+ | Pornw (rd rs1 rs2: ireg) (**r orn word *)
+ | Psraw (rd rs1 rs2: ireg) (**r shift right arithmetic word *)
+ | Psrxw (rd rs1 rs2: ireg) (**r shift right arithmetic word round to 0*)
+ | Psrlw (rd rs1 rs2: ireg) (**r shift right logical word *)
+ | Psllw (rd rs1 rs2: ireg) (**r shift left logical word *)
+ | Pmaddw (rd rs1 rs2: ireg) (**r multiply-add words *)
+ | Pmsubw (rd rs1 rs2: ireg) (**r multiply-add words *)
+ | Pfmaddfw (rd rs1 rs2: ireg) (**r float fused multiply-add words *)
+ | Pfmsubfw (rd rs1 rs2: ireg) (**r float fused multiply-subtract words *)
+ | Pfmaddfl (rd rs1 rs2: ireg) (**r float fused multiply-add longs *)
+ | Pfmsubfl (rd rs1 rs2: ireg) (**r float fused multiply-subtract longs *)
+
+ | Paddl (rd rs1 rs2: ireg) (**r add long *)
+ | Paddxl (shift : shift1_4) (rd rs1 rs2: ireg) (**r add long shift *)
+ | Psubl (rd rs1 rs2: ireg) (**r sub long *)
+ | Prevsubxl (shift : shift1_4) (rd rs1 rs2: ireg) (**r sub long shift *)
+ | Pandl (rd rs1 rs2: ireg) (**r and long *)
+ | Pnandl (rd rs1 rs2: ireg) (**r nand long *)
+ | Porl (rd rs1 rs2: ireg) (**r or long *)
+ | Pnorl (rd rs1 rs2: ireg) (**r nor long *)
+ | Pxorl (rd rs1 rs2: ireg) (**r xor long *)
+ | Pnxorl (rd rs1 rs2: ireg) (**r nxor long *)
+ | Pandnl (rd rs1 rs2: ireg) (**r andn long *)
+ | Pornl (rd rs1 rs2: ireg) (**r orn long *)
+ | Pmull (rd rs1 rs2: ireg) (**r mul long (low part) *)
+ | Pslll (rd rs1 rs2: ireg) (**r shift left logical long *)
+ | Psrll (rd rs1 rs2: ireg) (**r shift right logical long *)
+ | Psral (rd rs1 rs2: ireg) (**r shift right arithmetic long *)
+ | Psrxl (rd rs1 rs2: ireg) (**r shift right arithmetic long round to 0*)
+ | Pmaddl (rd rs1 rs2: ireg) (**r multiply-add long *)
+ | Pmsubl (rd rs1 rs2: ireg) (**r multiply-add long *)
+
+ | Pfaddd (rd rs1 rs2: ireg) (**r Float addition double *)
+ | Pfaddw (rd rs1 rs2: ireg) (**r Float addition word *)
+ | Pfsbfd (rd rs1 rs2: ireg) (**r Float sub double *)
+ | Pfsbfw (rd rs1 rs2: ireg) (**r Float sub word *)
+ | Pfmuld (rd rs1 rs2: ireg) (**r Float mul double *)
+ | Pfmulw (rd rs1 rs2: ireg) (**r Float mul word *)
+ | Pfmind (rd rs1 rs2: ireg) (**r Float min double *)
+ | Pfminw (rd rs1 rs2: ireg) (**r Float min word *)
+ | Pfmaxd (rd rs1 rs2: ireg) (**r Float max double *)
+ | Pfmaxw (rd rs1 rs2: ireg) (**r Float max word *)
+ | Pfinvw (rd rs1: ireg) (**r Float invert word *)
+
+ (** Arith RRI32 *)
+ | Pcompiw (it: itest) (rd rs: ireg) (imm: int) (**r comparison imm word *)
+
+ | Paddiw (rd rs: ireg) (imm: int) (**r add imm word *)
+ | Paddxiw (shift : shift1_4) (rd rs: ireg) (imm: int) (**r add imm word *)
+ | Prevsubiw (rd rs: ireg) (imm: int) (**r subtract imm word *)
+ | Prevsubxiw (shift : shift1_4) (rd rs: ireg) (imm: int) (**r subtract imm word *)
+ | Pmuliw (rd rs: ireg) (imm: int) (**r mul imm word *)
+ | Pandiw (rd rs: ireg) (imm: int) (**r and imm word *)
+ | Pnandiw (rd rs: ireg) (imm: int) (**r nand imm word *)
+ | Poriw (rd rs: ireg) (imm: int) (**r or imm word *)
+ | Pnoriw (rd rs: ireg) (imm: int) (**r nor imm word *)
+ | Pxoriw (rd rs: ireg) (imm: int) (**r xor imm word *)
+ | Pnxoriw (rd rs: ireg) (imm: int) (**r nxor imm word *)
+ | Pandniw (rd rs: ireg) (imm: int) (**r andn imm word *)
+ | Porniw (rd rs: ireg) (imm: int) (**r orn imm word *)
+ | Psraiw (rd rs: ireg) (imm: int) (**r shift right arithmetic imm word *)
+ | Psrxiw (rd rs: ireg) (imm: int) (**r shift right arithmetic imm word round to 0*)
+ | Psrliw (rd rs: ireg) (imm: int) (**r shift right logical imm word *)
+ | Pslliw (rd rs: ireg) (imm: int) (**r shift left logical imm word *)
+ | Proriw (rd rs: ireg) (imm: int) (**r rotate right imm word *)
+ | Pmaddiw (rd rs: ireg) (imm: int) (**r multiply add imm word *)
+ | Psllil (rd rs: ireg) (imm: int) (**r shift left logical immediate long *)
+ | Psrxil (rd rs: ireg) (imm: int) (**r shift right arithmetic imm word round to 0*)
+ | Psrlil (rd rs: ireg) (imm: int) (**r shift right logical immediate long *)
+ | Psrail (rd rs: ireg) (imm: int) (**r shift right arithmetic immediate long *)
+
+ (** Arith RRI64 *)
+ | Pcompil (it: itest) (rd rs: ireg) (imm: int64) (**r comparison imm long *)
+ | Paddil (rd rs: ireg) (imm: int64) (**r add immediate long *)
+ | Paddxil (shift : shift1_4) (rd rs: ireg) (imm: int64) (**r add immediate long *)
+ | Prevsubil (rd rs: ireg) (imm: int64) (**r subtract imm long *)
+ | Prevsubxil (shift : shift1_4) (rd rs: ireg) (imm: int64) (**r subtract imm long *)
+ | Pmulil (rd rs: ireg) (imm: int64) (**r add immediate long *)
+ | Pandil (rd rs: ireg) (imm: int64) (**r and immediate long *)
+ | Pnandil (rd rs: ireg) (imm: int64) (**r and immediate long *)
+ | Poril (rd rs: ireg) (imm: int64) (**r or immediate long *)
+ | Pnoril (rd rs: ireg) (imm: int64) (**r and immediate long *)
+ | Pxoril (rd rs: ireg) (imm: int64) (**r xor immediate long *)
+ | Pnxoril (rd rs: ireg) (imm: int64) (**r xor immediate long *)
+ | Pandnil (rd rs: ireg) (imm: int64) (**r andn long *)
+ | Pornil (rd rs: ireg) (imm: int64) (**r orn long *)
+ | Pmaddil (rd rs: ireg) (imm: int64) (**r multiply add imm long *)
+ | Pcmove (bt: btest) (rcond rd rs : ireg) (** conditional move *)
+ | Pcmoveu (bt: btest) (rcond rd rs : ireg) (** conditional move, unsigned semantics *)
+ | Pcmoveiw (bt: btest) (rcond rd : ireg) (imm: int) (** conditional move *)
+ | Pcmoveuiw (bt: btest) (rcond rd : ireg) (imm: int) (** conditional move, unsigned semantics *)
+ | Pcmoveil (bt: btest) (rcond rd : ireg) (imm: int64) (** conditional move *)
+ | Pcmoveuil (bt: btest) (rcond rd : ireg) (imm: int64) (** conditional move, unsigned semantics *)
+.
+
+(** Correspondance between Asmblock and Asm *)
+
+Definition control_to_instruction (c: control) :=
+ match c with
+ | PExpand (Asmvliw.Pbuiltin ef args res) => Pbuiltin ef args res
+ | PCtlFlow Asmvliw.Pret => Pret
+ | PCtlFlow (Asmvliw.Pcall l) => Pcall l
+ | PCtlFlow (Asmvliw.Picall r) => Picall r
+ | PCtlFlow (Asmvliw.Pgoto l) => Pgoto l
+ | PCtlFlow (Asmvliw.Pigoto l) => Pigoto l
+ | PCtlFlow (Asmvliw.Pj_l l) => Pj_l l
+ | PCtlFlow (Asmvliw.Pcb bt r l) => Pcb bt r l
+ | PCtlFlow (Asmvliw.Pcbu bt r l) => Pcbu bt r l
+ | PCtlFlow (Asmvliw.Pjumptable r label) => Pjumptable r label
+ end.
+
+Definition basic_to_instruction (b: basic) :=
+ match b with
+ (** Special basics *)
+ | Asmvliw.Pget rd rs => Pget rd rs
+ | Asmvliw.Pset rd rs => Pset rd rs
+ | Asmvliw.Pnop => Pnop
+ | Asmvliw.Pallocframe sz pos => Pallocframe sz pos
+ | Asmvliw.Pfreeframe sz pos => Pfreeframe sz pos
+
+ (** PArith basics *)
+ (* R *)
+ | PArithR (Asmvliw.Ploadsymbol id ofs) r => Ploadsymbol r id ofs
+
+ (* RR *)
+ | PArithRR Asmvliw.Pmv rd rs => Pmv rd rs
+ | PArithRR Asmvliw.Pnegw rd rs => Pnegw rd rs
+ | PArithRR Asmvliw.Pnegl rd rs => Pnegl rd rs
+ | PArithRR Asmvliw.Pcvtl2w rd rs => Pcvtl2w rd rs
+ | PArithRR Asmvliw.Psxwd rd rs => Psxwd rd rs
+ | PArithRR Asmvliw.Pzxwd rd rs => Pzxwd rd rs
+ | PArithRR (Asmvliw.Pextfz stop start) rd rs => Pextfz rd rs stop start
+ | PArithRR (Asmvliw.Pextfs stop start) rd rs => Pextfs rd rs stop start
+ | PArithRR (Asmvliw.Pextfzl stop start) rd rs => Pextfzl rd rs stop start
+ | PArithRR (Asmvliw.Pextfsl stop start) rd rs => Pextfsl rd rs stop start
+ | PArithRR Asmvliw.Pfabsd rd rs => Pfabsd rd rs
+ | PArithRR Asmvliw.Pfabsw rd rs => Pfabsw rd rs
+ | PArithRR Asmvliw.Pfnegd rd rs => Pfnegd rd rs
+ | PArithRR Asmvliw.Pfnegw rd rs => Pfnegw rd rs
+ | PArithRR Asmvliw.Pfinvw rd rs => Pfinvw rd rs
+ | PArithRR Asmvliw.Pfnarrowdw rd rs => Pfnarrowdw rd rs
+ | PArithRR Asmvliw.Pfwidenlwd rd rs => Pfwidenlwd rd rs
+ | PArithRR Asmvliw.Pfloatuwrnsz rd rs => Pfloatuwrnsz rd rs
+ | PArithRR Asmvliw.Pfloatwrnsz rd rs => Pfloatwrnsz rd rs
+ | PArithRR Asmvliw.Pfloatudrnsz rd rs => Pfloatudrnsz rd rs
+ | PArithRR Asmvliw.Pfloatdrnsz rd rs => Pfloatdrnsz rd rs
+ | PArithRR Asmvliw.Pfixedwrzz rd rs => Pfixedwrzz rd rs
+ | PArithRR Asmvliw.Pfixeduwrzz rd rs => Pfixeduwrzz rd rs
+ | PArithRR Asmvliw.Pfixeddrzz rd rs => Pfixeddrzz rd rs
+ | PArithRR Asmvliw.Pfixedudrzz rd rs => Pfixedudrzz rd rs
+ | PArithRR Asmvliw.Pfixeddrzz_i32 rd rs => Pfixeddrzz_i32 rd rs
+ | PArithRR Asmvliw.Pfixedudrzz_i32 rd rs => Pfixedudrzz_i32 rd rs
+
+ (* RI32 *)
+ | PArithRI32 Asmvliw.Pmake rd imm => Pmake rd imm
+
+ (* RI64 *)
+ | PArithRI64 Asmvliw.Pmakel rd imm => Pmakel rd imm
+
+ (* RF32 *)
+ | PArithRF32 Asmvliw.Pmakefs rd imm => Pmakefs rd imm
+
+ (* RF64 *)
+ | PArithRF64 Asmvliw.Pmakef rd imm => Pmakef rd imm
+
+ (* RRR *)
+ | PArithRRR (Asmvliw.Pcompw it) rd rs1 rs2 => Pcompw it rd rs1 rs2
+ | PArithRRR (Asmvliw.Pcompl it) rd rs1 rs2 => Pcompl it rd rs1 rs2
+ | PArithRRR (Asmvliw.Pfcompw ft) rd rs1 rs2 => Pfcompw ft rd rs1 rs2
+ | PArithRRR (Asmvliw.Pfcompl ft) rd rs1 rs2 => Pfcompl ft rd rs1 rs2
+ | PArithRRR Asmvliw.Paddw rd rs1 rs2 => Paddw rd rs1 rs2
+ | PArithRRR (Asmvliw.Paddxw shift) rd rs1 rs2 => Paddxw shift rd rs1 rs2
+ | PArithRRR Asmvliw.Psubw rd rs1 rs2 => Psubw rd rs1 rs2
+ | PArithRRR (Asmvliw.Prevsubxw shift) rd rs1 rs2 => Prevsubxw shift rd rs1 rs2
+ | PArithRRR Asmvliw.Pmulw rd rs1 rs2 => Pmulw rd rs1 rs2
+ | PArithRRR Asmvliw.Pandw rd rs1 rs2 => Pandw rd rs1 rs2
+ | PArithRRR Asmvliw.Pnandw rd rs1 rs2 => Pnandw rd rs1 rs2
+ | PArithRRR Asmvliw.Porw rd rs1 rs2 => Porw rd rs1 rs2
+ | PArithRRR Asmvliw.Pnorw rd rs1 rs2 => Pnorw rd rs1 rs2
+ | PArithRRR Asmvliw.Pxorw rd rs1 rs2 => Pxorw rd rs1 rs2
+ | PArithRRR Asmvliw.Pnxorw rd rs1 rs2 => Pnxorw rd rs1 rs2
+ | PArithRRR Asmvliw.Pandnw rd rs1 rs2 => Pandnw rd rs1 rs2
+ | PArithRRR Asmvliw.Pornw rd rs1 rs2 => Pornw rd rs1 rs2
+ | PArithRRR Asmvliw.Psraw rd rs1 rs2 => Psraw rd rs1 rs2
+ | PArithRRR Asmvliw.Psrxw rd rs1 rs2 => Psrxw rd rs1 rs2
+ | PArithRRR Asmvliw.Psrlw rd rs1 rs2 => Psrlw rd rs1 rs2
+ | PArithRRR Asmvliw.Psllw rd rs1 rs2 => Psllw rd rs1 rs2
+
+ | PArithRRR Asmvliw.Paddl rd rs1 rs2 => Paddl rd rs1 rs2
+ | PArithRRR (Asmvliw.Paddxl shift) rd rs1 rs2 => Paddxl shift rd rs1 rs2
+ | PArithRRR Asmvliw.Psubl rd rs1 rs2 => Psubl rd rs1 rs2
+ | PArithRRR (Asmvliw.Prevsubxl shift) rd rs1 rs2 => Prevsubxl shift rd rs1 rs2
+ | PArithRRR Asmvliw.Pandl rd rs1 rs2 => Pandl rd rs1 rs2
+ | PArithRRR Asmvliw.Pnandl rd rs1 rs2 => Pnandl rd rs1 rs2
+ | PArithRRR Asmvliw.Porl rd rs1 rs2 => Porl rd rs1 rs2
+ | PArithRRR Asmvliw.Pnorl rd rs1 rs2 => Pnorl rd rs1 rs2
+ | PArithRRR Asmvliw.Pxorl rd rs1 rs2 => Pxorl rd rs1 rs2
+ | PArithRRR Asmvliw.Pnxorl rd rs1 rs2 => Pnxorl rd rs1 rs2
+ | PArithRRR Asmvliw.Pandnl rd rs1 rs2 => Pandnl rd rs1 rs2
+ | PArithRRR Asmvliw.Pornl rd rs1 rs2 => Pornl rd rs1 rs2
+ | PArithRRR Asmvliw.Pmull rd rs1 rs2 => Pmull rd rs1 rs2
+ | PArithRRR Asmvliw.Pslll rd rs1 rs2 => Pslll rd rs1 rs2
+ | PArithRRR Asmvliw.Psrll rd rs1 rs2 => Psrll rd rs1 rs2
+ | PArithRRR Asmvliw.Psral rd rs1 rs2 => Psral rd rs1 rs2
+ | PArithRRR Asmvliw.Psrxl rd rs1 rs2 => Psrxl rd rs1 rs2
+
+ | PArithRRR Asmvliw.Pfaddd rd rs1 rs2 => Pfaddd rd rs1 rs2
+ | PArithRRR Asmvliw.Pfaddw rd rs1 rs2 => Pfaddw rd rs1 rs2
+ | PArithRRR Asmvliw.Pfsbfd rd rs1 rs2 => Pfsbfd rd rs1 rs2
+ | PArithRRR Asmvliw.Pfsbfw rd rs1 rs2 => Pfsbfw rd rs1 rs2
+ | PArithRRR Asmvliw.Pfmuld rd rs1 rs2 => Pfmuld rd rs1 rs2
+ | PArithRRR Asmvliw.Pfmulw rd rs1 rs2 => Pfmulw rd rs1 rs2
+ | PArithRRR Asmvliw.Pfmind rd rs1 rs2 => Pfmind rd rs1 rs2
+ | PArithRRR Asmvliw.Pfminw rd rs1 rs2 => Pfminw rd rs1 rs2
+ | PArithRRR Asmvliw.Pfmaxd rd rs1 rs2 => Pfmaxd rd rs1 rs2
+ | PArithRRR Asmvliw.Pfmaxw rd rs1 rs2 => Pfmaxw rd rs1 rs2
+
+ (* RRI32 *)
+ | PArithRRI32 (Asmvliw.Pcompiw it) rd rs imm => Pcompiw it rd rs imm
+ | PArithRRI32 Asmvliw.Paddiw rd rs imm => Paddiw rd rs imm
+ | PArithRRI32 (Asmvliw.Paddxiw shift) rd rs imm => Paddxiw shift rd rs imm
+ | PArithRRI32 Asmvliw.Prevsubiw rd rs imm => Prevsubiw rd rs imm
+ | PArithRRI32 (Asmvliw.Prevsubxiw shift) rd rs imm => Prevsubxiw shift rd rs imm
+ | PArithRRI32 Asmvliw.Pmuliw rd rs imm => Pmuliw rd rs imm
+ | PArithRRI32 Asmvliw.Pandiw rd rs imm => Pandiw rd rs imm
+ | PArithRRI32 Asmvliw.Pnandiw rd rs imm => Pnandiw rd rs imm
+ | PArithRRI32 Asmvliw.Poriw rd rs imm => Poriw rd rs imm
+ | PArithRRI32 Asmvliw.Pnoriw rd rs imm => Pnoriw rd rs imm
+ | PArithRRI32 Asmvliw.Pxoriw rd rs imm => Pxoriw rd rs imm
+ | PArithRRI32 Asmvliw.Pnxoriw rd rs imm => Pnxoriw rd rs imm
+ | PArithRRI32 Asmvliw.Pandniw rd rs imm => Pandniw rd rs imm
+ | PArithRRI32 Asmvliw.Porniw rd rs imm => Porniw rd rs imm
+ | PArithRRI32 Asmvliw.Psraiw rd rs imm => Psraiw rd rs imm
+ | PArithRRI32 Asmvliw.Psrxiw rd rs imm => Psrxiw rd rs imm
+ | PArithRRI32 Asmvliw.Psrliw rd rs imm => Psrliw rd rs imm
+ | PArithRRI32 Asmvliw.Pslliw rd rs imm => Pslliw rd rs imm
+ | PArithRRI32 Asmvliw.Proriw rd rs imm => Proriw rd rs imm
+ | PArithRRI32 Asmvliw.Psllil rd rs imm => Psllil rd rs imm
+ | PArithRRI32 Asmvliw.Psrlil rd rs imm => Psrlil rd rs imm
+ | PArithRRI32 Asmvliw.Psrxil rd rs imm => Psrxil rd rs imm
+ | PArithRRI32 Asmvliw.Psrail rd rs imm => Psrail rd rs imm
+
+ (* RRI64 *)
+ | PArithRRI64 (Asmvliw.Pcompil it) rd rs imm => Pcompil it rd rs imm
+ | PArithRRI64 Asmvliw.Paddil rd rs imm => Paddil rd rs imm
+ | PArithRRI64 (Asmvliw.Paddxil shift) rd rs imm => Paddxil shift rd rs imm
+ | PArithRRI64 Asmvliw.Prevsubil rd rs imm => Prevsubil rd rs imm
+ | PArithRRI64 (Asmvliw.Prevsubxil shift) rd rs imm => Prevsubxil shift rd rs imm
+ | PArithRRI64 Asmvliw.Pmulil rd rs imm => Pmulil rd rs imm
+ | PArithRRI64 Asmvliw.Pandil rd rs imm => Pandil rd rs imm
+ | PArithRRI64 Asmvliw.Pnandil rd rs imm => Pnandil rd rs imm
+ | PArithRRI64 Asmvliw.Poril rd rs imm => Poril rd rs imm
+ | PArithRRI64 Asmvliw.Pnoril rd rs imm => Pnoril rd rs imm
+ | PArithRRI64 Asmvliw.Pxoril rd rs imm => Pxoril rd rs imm
+ | PArithRRI64 Asmvliw.Pnxoril rd rs imm => Pnxoril rd rs imm
+ | PArithRRI64 Asmvliw.Pandnil rd rs imm => Pandnil rd rs imm
+ | PArithRRI64 Asmvliw.Pornil rd rs imm => Pornil rd rs imm
+
+ (** ARRR *)
+ | PArithARRR Asmvliw.Pmaddw rd rs1 rs2 => Pmaddw rd rs1 rs2
+ | PArithARRR Asmvliw.Pmaddl rd rs1 rs2 => Pmaddl rd rs1 rs2
+ | PArithARRR Asmvliw.Pmsubw rd rs1 rs2 => Pmsubw rd rs1 rs2
+ | PArithARRR Asmvliw.Pmsubl rd rs1 rs2 => Pmsubl rd rs1 rs2
+ | PArithARRR Asmvliw.Pfmaddfw rd rs1 rs2 => Pfmaddfw rd rs1 rs2
+ | PArithARRR Asmvliw.Pfmaddfl rd rs1 rs2 => Pfmaddfl rd rs1 rs2
+ | PArithARRR Asmvliw.Pfmsubfw rd rs1 rs2 => Pfmsubfw rd rs1 rs2
+ | PArithARRR Asmvliw.Pfmsubfl rd rs1 rs2 => Pfmsubfl rd rs1 rs2
+ | PArithARRR (Asmvliw.Pcmove cond) rd rs1 rs2=> Pcmove cond rd rs1 rs2
+ | PArithARRR (Asmvliw.Pcmoveu cond) rd rs1 rs2=> Pcmoveu cond rd rs1 rs2
+
+ (** ARR *)
+ | PArithARR (Asmvliw.Pinsf stop start) rd rs => Pinsf rd rs stop start
+ | PArithARR (Asmvliw.Pinsfl stop start) rd rs => Pinsfl rd rs stop start
+
+ (** ARRI32 *)
+ | PArithARRI32 Asmvliw.Pmaddiw rd rs1 imm => Pmaddiw rd rs1 imm
+ | PArithARRI32 (Asmvliw.Pcmoveiw cond) rd rs1 imm => Pcmoveiw cond rd rs1 imm
+ | PArithARRI32 (Asmvliw.Pcmoveuiw cond) rd rs1 imm => Pcmoveuiw cond rd rs1 imm
+
+ (** ARRI64 *)
+ | PArithARRI64 Asmvliw.Pmaddil rd rs1 imm => Pmaddil rd rs1 imm
+ | PArithARRI64 (Asmvliw.Pcmoveil cond) rd rs1 imm => Pcmoveil cond rd rs1 imm
+ | PArithARRI64 (Asmvliw.Pcmoveuil cond) rd rs1 imm => Pcmoveuil cond rd rs1 imm
+ (** Load *)
+ | PLoadRRO trap Asmvliw.Plb rd ra ofs => Plb trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Plbu rd ra ofs => Plbu trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Plh rd ra ofs => Plh trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Plhu rd ra ofs => Plhu trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Plw rd ra ofs => Plw trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Plw_a rd ra ofs => Plw_a trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Pld rd ra ofs => Pld trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Pld_a rd ra ofs => Pld_a trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Pfls rd ra ofs => Pfls trap rd ra (AOff ofs)
+ | PLoadRRO trap Asmvliw.Pfld rd ra ofs => Pfld trap rd ra (AOff ofs)
+
+ | PLoadQRRO qrs ra ofs => Plq qrs ra (AOff ofs)
+ | PLoadORRO qrs ra ofs => Plo qrs ra (AOff ofs)
+
+ | PLoadRRR trap Asmvliw.Plb rd ra ro => Plb trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Plbu rd ra ro => Plbu trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Plh rd ra ro => Plh trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Plhu rd ra ro => Plhu trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Plw rd ra ro => Plw trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Plw_a rd ra ro => Plw_a trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Pld rd ra ro => Pld trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Pld_a rd ra ro => Pld_a trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Pfls rd ra ro => Pfls trap rd ra (AReg ro)
+ | PLoadRRR trap Asmvliw.Pfld rd ra ro => Pfld trap rd ra (AReg ro)
+
+ | PLoadRRRXS trap Asmvliw.Plb rd ra ro => Plb trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Plbu rd ra ro => Plbu trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Plh rd ra ro => Plh trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Plhu rd ra ro => Plhu trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Plw rd ra ro => Plw trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Plw_a rd ra ro => Plw_a trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Pld rd ra ro => Pld trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Pld_a rd ra ro => Pld_a trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Pfls rd ra ro => Pfls trap rd ra (ARegXS ro)
+ | PLoadRRRXS trap Asmvliw.Pfld rd ra ro => Pfld trap rd ra (ARegXS ro)
+
+ (** Store *)
+ | PStoreRRO Asmvliw.Psb rd ra ofs => Psb rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Psh rd ra ofs => Psh rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Psw rd ra ofs => Psw rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Psw_a rd ra ofs => Psw_a rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Psd rd ra ofs => Psd rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Psd_a rd ra ofs => Psd_a rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Pfss rd ra ofs => Pfss rd ra (AOff ofs)
+ | PStoreRRO Asmvliw.Pfsd rd ra ofs => Pfsd rd ra (AOff ofs)
+
+ | PStoreRRR Asmvliw.Psb rd ra ro => Psb rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Psh rd ra ro => Psh rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Psw rd ra ro => Psw rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Psw_a rd ra ro => Psw_a rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Psd rd ra ro => Psd rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Psd_a rd ra ro => Psd_a rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Pfss rd ra ro => Pfss rd ra (AReg ro)
+ | PStoreRRR Asmvliw.Pfsd rd ra ro => Pfsd rd ra (AReg ro)
+
+ | PStoreRRRXS Asmvliw.Psb rd ra ro => Psb rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Psh rd ra ro => Psh rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Psw rd ra ro => Psw rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Psw_a rd ra ro => Psw_a rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Psd rd ra ro => Psd rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Psd_a rd ra ro => Psd_a rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Pfss rd ra ro => Pfss rd ra (ARegXS ro)
+ | PStoreRRRXS Asmvliw.Pfsd rd ra ro => Pfsd rd ra (ARegXS ro)
+
+ | PStoreQRRO qrs ra ofs => Psq qrs ra (AOff ofs)
+ | PStoreORRO qrs ra ofs => Pso qrs ra (AOff ofs)
+ end.
+
+(** * Semantics (given through the existence of well-formed VLIW program) *)
+
+Section RELSEM.
+
+Definition code := list instruction.
+
+Fixpoint unfold_label (ll: list label) :=
+ match ll with
+ | nil => nil
+ | l :: ll => Plabel l :: unfold_label ll
+ end.
+
+Fixpoint unfold_body (lb: list basic) :=
+ match lb with
+ | nil => nil
+ | b :: lb => basic_to_instruction b :: unfold_body lb
+ end.
+
+Definition unfold_exit (oc: option control) :=
+ match oc with
+ | None => nil
+ | Some c => control_to_instruction c :: nil
+ end.
+
+Definition unfold_bblock (b: bblock) := unfold_label (header b) ++
+ (match (body b), (exit b) with
+ | (((Asmvliw.Pfreeframe _ _ | Asmvliw.Pallocframe _ _)::nil) as bo), None =>
+ unfold_body bo
+ | bo, ex => unfold_body bo ++ unfold_exit ex ++ Psemi :: nil
+ end).
+
+Fixpoint unfold (lb: bblocks) :=
+ match lb with
+ | nil => nil
+ | b :: lb => (unfold_bblock b) ++ unfold lb
+ end.
+
+Record function : Type := mkfunction { fn_sig: signature; fn_blocks: bblocks; fn_code: code;
+ correct: unfold fn_blocks = fn_code }.
+
+Definition fundef := AST.fundef function.
+Definition program := AST.program fundef unit.
+Definition genv := Genv.t fundef unit.
+
+Definition function_proj (f: function) := Asmvliw.mkfunction (fn_sig f) (fn_blocks f).
+
+Definition fundef_proj (fu: fundef) : Asmvliw.fundef :=
+ match fu with
+ | Internal f => Internal (function_proj f)
+ | External ef => External ef
+ end.
+
+Definition globdef_proj (gd: globdef fundef unit) : globdef Asmvliw.fundef unit :=
+ match gd with
+ | Gfun f => Gfun (fundef_proj f)
+ | Gvar gu => Gvar gu
+ end.
+
+Program Definition genv_trans (ge: genv) : Asmvliw.genv :=
+ {| Genv.genv_public := Genv.genv_public ge;
+ Genv.genv_symb := Genv.genv_symb ge;
+ Genv.genv_defs := PTree.map1 globdef_proj (Genv.genv_defs ge);
+ Genv.genv_next := Genv.genv_next ge |}.
+Next Obligation.
+ destruct ge. cbn in *. eauto.
+Qed. Next Obligation.
+ destruct ge; cbn in *.
+ rewrite PTree.gmap1 in H.
+ destruct (genv_defs ! b) eqn:GEN.
+ - eauto.
+ - discriminate.
+Qed. Next Obligation.
+ destruct ge; cbn in *.
+ eauto.
+Qed.
+
+Fixpoint prog_defs_proj (l: list (ident * globdef fundef unit))
+ : list (ident * globdef Asmvliw.fundef unit) :=
+ match l with
+ | nil => nil
+ | (i, gd) :: l => (i, globdef_proj gd) :: prog_defs_proj l
+ end.
+
+Definition program_proj (p: program) : Asmvliw.program :=
+ {| prog_defs := prog_defs_proj (prog_defs p);
+ prog_public := prog_public p;
+ prog_main := prog_main p
+ |}.
+
+End RELSEM.
+
+Definition semantics (p: program) := Asmvliw.semantics (program_proj p).
+
+(** Determinacy of the [Asm] semantics. *)
+
+Lemma semantics_determinate: forall p, determinate (semantics p).
+Proof.
+ intros. apply semantics_determinate.
+Qed.
+
+(** transf_program *)
+
+Program Definition transf_function (f: Asmvliw.function) : function :=
+ {| fn_sig := Asmvliw.fn_sig f; fn_blocks := Asmvliw.fn_blocks f;
+ fn_code := unfold (Asmvliw.fn_blocks f) |}.
+
+Lemma transf_function_proj: forall f, function_proj (transf_function f) = f.
+Proof.
+ intros f. destruct f as [sig blks]. unfold function_proj. cbn. auto.
+Qed.
+
+Definition transf_fundef : Asmvliw.fundef -> fundef := AST.transf_fundef transf_function.
+
+Lemma transf_fundef_proj: forall f, fundef_proj (transf_fundef f) = f.
+Proof.
+ intros f. destruct f as [f|e]; cbn; auto.
+ rewrite transf_function_proj. auto.
+Qed.
+
+Definition transf_program : Asmvliw.program -> program := transform_program transf_fundef.
+
+Lemma program_equals {A B: Type} : forall (p1 p2: AST.program A B),
+ prog_defs p1 = prog_defs p2 ->
+ prog_public p1 = prog_public p2 ->
+ prog_main p1 = prog_main p2 ->
+ p1 = p2.
+Proof.
+ intros. destruct p1. destruct p2. cbn in *. subst. auto.
+Qed.
+
+Lemma transf_program_proj: forall p, program_proj (transf_program p) = p.
+Proof.
+ intros p. destruct p as [defs pub main]. unfold program_proj. cbn.
+ apply program_equals; cbn; auto.
+ induction defs.
+ - cbn; auto.
+ - cbn. rewrite IHdefs.
+ destruct a as [id gd]; cbn.
+ destruct gd as [f|v]; cbn; auto.
+ rewrite transf_fundef_proj. auto.
+Qed.
+
+Definition match_prog (p: Asmvliw.program) (tp: program) :=
+ match_program (fun _ f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, transf_program p = tp -> match_prog p tp.
+Proof.
+ intros. rewrite <- H. eapply match_transform_program; eauto.
+Qed.
+
+Lemma cons_extract {A: Type} : forall (l: list A) a b, a = b -> a::l = b::l.
+Proof.
+ intros. congruence.
+Qed.
+
+Lemma match_program_transf:
+ forall p tp, match_prog p tp -> transf_program p = tp.
+Proof.
+ intros p tp H. inversion_clear H. inv H1.
+ destruct p as [defs pub main]. destruct tp as [tdefs tpub tmain]. cbn in *.
+ subst. unfold transf_program. unfold transform_program. cbn.
+ apply program_equals; cbn; auto.
+ induction H0; cbn; auto.
+ rewrite IHlist_forall2. apply cons_extract.
+ destruct a1 as [ida gda]. destruct b1 as [idb gdb].
+ cbn in *.
+ inv H. inv H2.
+ - cbn in *. subst. auto.
+ - cbn in *. subst. inv H. auto.
+Qed.
+
+Section PRESERVATION.
+
+Variable prog: Asmvliw.program.
+Variable tprog: program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Definition match_states (s1 s2: state) := s1 = s2.
+
+Lemma symbols_preserved:
+ forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof (Genv.find_symbol_match TRANSF).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSF).
+
+
+Theorem transf_program_correct:
+ forward_simulation (Asmvliw.semantics prog) (semantics tprog).
+Proof.
+ pose proof (match_program_transf prog tprog TRANSF) as TR.
+ subst. unfold semantics. rewrite transf_program_proj.
+
+ eapply forward_simulation_step with (match_states := match_states); cbn; auto.
+ - intros. exists s1. split; auto. congruence.
+ - intros. inv H. auto.
+ - intros. exists s1'. inv H0. split; auto. congruence.
+Qed.
+
+End PRESERVATION.
diff --git a/kvx/AsmToJSON.ml b/kvx/AsmToJSON.ml
new file mode 100644
index 00000000..8a6a97a7
--- /dev/null
+++ b/kvx/AsmToJSON.ml
@@ -0,0 +1,23 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Bernhard Schommer, AbsInt Angewandte Informatik GmbH *)
+(* *)
+(* AbsInt Angewandte Informatik GmbH. All rights reserved. This file *)
+(* is distributed under the terms of the INRIA Non-Commercial *)
+(* License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(* Simple functions to serialize RISC-V Asm to JSON *)
+
+(* Dummy function *)
+let destination: string option ref = ref None
+
+let sdump_folder = ref ""
+
+let print_if prog sourcename =
+ ()
+
+let pp_mnemonics pp = ()
diff --git a/kvx/Asmaux.v b/kvx/Asmaux.v
new file mode 100644
index 00000000..2abd445e
--- /dev/null
+++ b/kvx/Asmaux.v
@@ -0,0 +1,19 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Asm.
+Require Import AST.
+
+(** Constant only needed by Asmexpandaux.ml *)
+Program Definition dummy_function := {| fn_code := nil; fn_sig := signature_main; fn_blocks := nil |}.
diff --git a/kvx/Asmblock.v b/kvx/Asmblock.v
new file mode 100644
index 00000000..17ebac32
--- /dev/null
+++ b/kvx/Asmblock.v
@@ -0,0 +1,394 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Sequential block semantics for KVX assembly. The syntax is given in AsmVLIW *)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import Memory.
+Require Import Events.
+Require Import Globalenvs.
+Require Import Smallstep.
+Require Import Locations.
+Require Stacklayout.
+Require Import Conventions.
+Require Import Errors.
+Require Export Asmvliw.
+Require Import Lia.
+
+(* Notations necessary to hook Asmvliw definitions *)
+Notation undef_caller_save_regs := Asmvliw.undef_caller_save_regs.
+Notation regset := Asmvliw.regset.
+Notation extcall_arg := Asmvliw.extcall_arg.
+Notation extcall_arg_pair := Asmvliw.extcall_arg_pair.
+Notation extcall_arguments := Asmvliw.extcall_arguments.
+Notation set_res := Asmvliw.set_res.
+Notation function := Asmvliw.function.
+Notation bblocks := Asmvliw.bblocks.
+Notation header := Asmvliw.header.
+Notation body := Asmvliw.body.
+Notation exit := Asmvliw.exit.
+Notation correct := Asmvliw.correct.
+
+(** * Auxiliary utilies on basic blocks *)
+
+(** ** A unified view of Kalray instructions *)
+
+Inductive instruction : Type :=
+ | PBasic (i: basic)
+ | PControl (i: control)
+.
+
+Coercion PBasic: basic >-> instruction.
+Coercion PControl: control >-> instruction.
+
+Definition code := list instruction.
+Definition bcode := list basic.
+
+Fixpoint basics_to_code (l: list basic) :=
+ match l with
+ | nil => nil
+ | bi::l => (PBasic bi)::(basics_to_code l)
+ end.
+
+Fixpoint code_to_basics (c: code) :=
+ match c with
+ | (PBasic i)::c =>
+ match code_to_basics c with
+ | None => None
+ | Some l => Some (i::l)
+ end
+ | _::c => None
+ | nil => Some nil
+ end.
+
+Lemma code_to_basics_id: forall c, code_to_basics (basics_to_code c) = Some c.
+Proof.
+ intros. induction c as [|i c]; cbn; auto.
+ rewrite IHc. auto.
+Qed.
+
+Lemma code_to_basics_dist:
+ forall c c' l l',
+ code_to_basics c = Some l ->
+ code_to_basics c' = Some l' ->
+ code_to_basics (c ++ c') = Some (l ++ l').
+Proof.
+ induction c as [|i c]; cbn; auto.
+ - intros. inv H. cbn. auto.
+ - intros. destruct i; try discriminate. destruct (code_to_basics c) eqn:CTB; try discriminate.
+ inv H. erewrite IHc; eauto. auto.
+Qed.
+
+(**
+ Asmblockgen will have to translate a Mach control into a list of instructions of the form
+ i1 :: i2 :: i3 :: ctl :: nil ; where i1..i3 are basic instructions, ctl is a control instruction
+ These functions provide way to extract the basic / control instructions
+*)
+
+Fixpoint extract_basic (c: code) :=
+ match c with
+ | nil => nil
+ | PBasic i :: c => i :: (extract_basic c)
+ | PControl i :: c => nil
+ end.
+
+Fixpoint extract_ctl (c: code) :=
+ match c with
+ | nil => None
+ | PBasic i :: c => extract_ctl c
+ | PControl i :: nil => Some i
+ | PControl i :: _ => None (* if the first found control instruction isn't the last *)
+ end.
+
+(** ** Wellformness of basic blocks *)
+
+Ltac exploreInst :=
+ repeat match goal with
+ | [ H : match ?var with | _ => _ end = _ |- _ ] => destruct var
+ | [ H : OK _ = OK _ |- _ ] => monadInv H
+ | [ |- context[if ?b then _ else _] ] => destruct b
+ | [ |- context[match ?m with | _ => _ end] ] => destruct m
+ | [ |- context[match ?m as _ return _ with | _ => _ end]] => destruct m
+ | [ H : bind _ _ = OK _ |- _ ] => monadInv H
+ | [ H : Error _ = OK _ |- _ ] => inversion H
+ end.
+
+Definition non_empty_bblock (body: list basic) (exit: option control): Prop
+ := body <> nil \/ exit <> None.
+
+Lemma non_empty_bblock_refl:
+ forall body exit,
+ non_empty_bblock body exit <->
+ Is_true (non_empty_bblockb body exit).
+Proof.
+ intros. split.
+ - destruct body; destruct exit.
+ all: cbn; auto. intros. inversion H; contradiction.
+ - destruct body; destruct exit.
+ all: cbn; auto.
+ all: intros; try (right; discriminate); try (left; discriminate).
+ contradiction.
+Qed.
+
+Definition builtin_alone (body: list basic) (exit: option control) := forall ef args res,
+ exit = Some (PExpand (Pbuiltin ef args res)) -> body = nil.
+
+
+Lemma builtin_alone_refl:
+ forall body exit,
+ builtin_alone body exit <-> Is_true (builtin_aloneb body exit).
+Proof.
+ intros. split.
+ - destruct body; destruct exit.
+ all: cbn; auto.
+ all: exploreInst; cbn; auto.
+ unfold builtin_alone. intros. assert (Some (Pbuiltin e l b0) = Some (Pbuiltin e l b0)); auto.
+ assert (b :: body = nil). eapply H; eauto. discriminate.
+ - destruct body; destruct exit.
+ all: cbn; auto; try constructor.
+ + exploreInst; try discriminate.
+ cbn. contradiction.
+ + intros. discriminate.
+Qed.
+
+Definition wf_bblock (body: list basic) (exit: option control) :=
+ non_empty_bblock body exit /\ builtin_alone body exit.
+
+Lemma wf_bblock_refl:
+ forall body exit,
+ wf_bblock body exit <-> Is_true (wf_bblockb body exit).
+Proof.
+ intros. split.
+ - intros. inv H. apply non_empty_bblock_refl in H0. apply builtin_alone_refl in H1.
+ apply andb_prop_intro. auto.
+ - intros. apply andb_prop_elim in H. inv H.
+ apply non_empty_bblock_refl in H0. apply builtin_alone_refl in H1.
+ unfold wf_bblock. split; auto.
+Qed.
+
+Ltac bblock_auto_correct := (apply non_empty_bblock_refl; try discriminate; try (left; discriminate); try (right; discriminate)).
+
+Lemma Istrue_proof_irrelevant (b: bool): forall (p1 p2:Is_true b), p1=p2.
+Proof.
+ destruct b; cbn; auto.
+ - destruct p1, p2; auto.
+ - destruct p1.
+Qed.
+
+Lemma bblock_equality bb1 bb2: header bb1=header bb2 -> body bb1 = body bb2 -> exit bb1 = exit bb2 -> bb1 = bb2.
+Proof.
+ destruct bb1 as [h1 b1 e1 c1], bb2 as [h2 b2 e2 c2]; cbn.
+ intros; subst.
+ rewrite (Istrue_proof_irrelevant _ c1 c2).
+ auto.
+Qed.
+
+Program Definition bblock_single_inst (i: instruction) :=
+ match i with
+ | PBasic b => {| header:=nil; body:=(b::nil); exit:=None |}
+ | PControl ctl => {| header:=nil; body:=nil; exit:=(Some ctl) |}
+ end.
+Next Obligation.
+ apply wf_bblock_refl. constructor.
+ right. discriminate.
+ constructor.
+Qed.
+
+Lemma length_nonil {A: Type} : forall l:(list A), l <> nil -> (length l > 0)%nat.
+Proof.
+ intros. destruct l; try (contradict H; auto; fail).
+ cbn. lia.
+Qed.
+
+Lemma to_nat_pos : forall z:Z, (Z.to_nat z > 0)%nat -> z > 0.
+Proof.
+ intros. destruct z; auto.
+ - contradict H. cbn. apply gt_irrefl.
+ - apply Zgt_pos_0.
+ - contradict H. cbn. apply gt_irrefl.
+Qed.
+
+Lemma size_positive (b:bblock): size b > 0.
+Proof.
+ unfold size. destruct b as [hd bdy ex cor]. cbn.
+ destruct ex; destruct bdy; try (apply to_nat_pos; rewrite Nat2Z.id; cbn; lia).
+ inversion cor; contradict H; cbn; auto.
+Qed.
+
+
+Program Definition no_header (bb : bblock) := {| header := nil; body := body bb; exit := exit bb |}.
+Next Obligation.
+ destruct bb; cbn. assumption.
+Defined.
+
+Lemma no_header_size:
+ forall bb, size (no_header bb) = size bb.
+Proof.
+ intros. destruct bb as [hd bdy ex COR]. unfold no_header. cbn. reflexivity.
+Qed.
+
+Program Definition stick_header (h : list label) (bb : bblock) := {| header := h; body := body bb; exit := exit bb |}.
+Next Obligation.
+ destruct bb; cbn. assumption.
+Defined.
+
+Lemma stick_header_size:
+ forall h bb, size (stick_header h bb) = size bb.
+Proof.
+ intros. destruct bb. unfold stick_header. cbn. reflexivity.
+Qed.
+
+Lemma stick_header_no_header:
+ forall bb, stick_header (header bb) (no_header bb) = bb.
+Proof.
+ intros. destruct bb as [hd bdy ex COR]. cbn. unfold no_header; unfold stick_header; cbn. reflexivity.
+Qed.
+
+(** * Sequential Semantics of basic blocks *)
+Section RELSEM.
+
+(** Execution of arith instructions *)
+
+Variable ge: genv.
+
+Definition exec_arith_instr (ai: ar_instruction) (rs: regset): regset := parexec_arith_instr ge ai rs rs.
+
+(** Auxiliaries for memory accesses *)
+
+Definition exec_load_offset (trap: trapping_mode) (chunk: memory_chunk) (rs: regset) (m: mem) (d a: ireg) (ofs: offset) := parexec_load_offset trap chunk rs rs m m d a ofs.
+
+Definition exec_load_reg (trap: trapping_mode) (chunk: memory_chunk) (rs: regset) (m: mem) (d a ro: ireg) := parexec_load_reg trap chunk rs rs m m d a ro.
+
+Definition exec_load_regxs (trap: trapping_mode) (chunk: memory_chunk) (rs: regset) (m: mem) (d a ro: ireg) := parexec_load_regxs trap chunk rs rs m m d a ro.
+
+Definition exec_load_q_offset (rs: regset) (m: mem) (d : gpreg_q) (a: ireg) (ofs: offset) := parexec_load_q_offset rs rs m m d a ofs.
+
+Definition exec_load_o_offset (rs: regset) (m: mem) (d : gpreg_o) (a: ireg) (ofs: offset) := parexec_load_o_offset rs rs m m d a ofs.
+
+Definition exec_store_offset (chunk: memory_chunk) (rs: regset) (m: mem) (s a: ireg) (ofs: offset) := parexec_store_offset chunk rs rs m m s a ofs.
+
+Definition exec_store_q_offset (rs: regset) (m: mem) (s : gpreg_q) (a: ireg) (ofs: offset) := parexec_store_q_offset rs rs m m s a ofs.
+
+Definition exec_store_o_offset (rs: regset) (m: mem) (s : gpreg_o) (a: ireg) (ofs: offset) := parexec_store_o_offset rs rs m m s a ofs.
+
+Definition exec_store_reg (chunk: memory_chunk) (rs: regset) (m: mem) (s a ro: ireg) := parexec_store_reg chunk rs rs m m s a ro.
+
+Definition exec_store_regxs (chunk: memory_chunk) (rs: regset) (m: mem) (s a ro: ireg) := parexec_store_regxs chunk rs rs m m s a ro.
+
+(** * basic instructions *)
+
+Definition exec_basic_instr (bi: basic) (rs: regset) (m: mem) : outcome := bstep ge bi rs rs m m.
+
+Fixpoint exec_body (body: list basic) (rs: regset) (m: mem): outcome :=
+ match body with
+ | nil => Next rs m
+ | bi::body' =>
+ match exec_basic_instr bi rs m with
+ | Next rs' m' => exec_body body' rs' m'
+ | Stuck => Stuck
+ end
+ end.
+
+
+Theorem builtin_body_nil:
+ forall bb ef args res, exit bb = Some (PExpand (Pbuiltin ef args res)) -> body bb = nil.
+Proof.
+ intros. destruct bb as [hd bdy ex WF]. cbn in *.
+ apply wf_bblock_refl in WF. inv WF. unfold builtin_alone in H1.
+ eapply H1; eauto.
+Qed.
+
+Theorem exec_body_app:
+ forall l l' rs m rs'' m'',
+ exec_body (l ++ l') rs m = Next rs'' m'' ->
+ exists rs' m',
+ exec_body l rs m = Next rs' m'
+ /\ exec_body l' rs' m' = Next rs'' m''.
+Proof.
+ induction l.
+ - intros. cbn in H. repeat eexists. auto.
+ - intros. rewrite <- app_comm_cons in H. cbn in H.
+ destruct (exec_basic_instr a rs m) eqn:EXEBI.
+ + apply IHl in H. destruct H as (rs1 & m1 & EXEB1 & EXEB2).
+ repeat eexists. cbn. rewrite EXEBI. eauto. auto.
+ + discriminate.
+Qed.
+
+(** Position corresponding to a label *)
+
+Definition goto_label (f: function) (lbl: label) (rs: regset) (m: mem) : outcome := par_goto_label f lbl rs rs m.
+
+Definition eval_branch (f: function) (l: label) (rs: regset) (m: mem) (res: option bool) : outcome := par_eval_branch f l rs rs m res.
+
+Definition exec_control (f: function) (oc: option control) (rs: regset) (m: mem) : outcome := parexec_control ge f oc rs rs m.
+
+Definition exec_bblock (f: function) (b: bblock) (rs0: regset) (m: mem) : outcome :=
+ match exec_body (body b) rs0 m with
+ | Next rs' m' =>
+ let rs1 := nextblock b rs' in exec_control f (exit b) rs1 m'
+ | Stuck => Stuck
+ end.
+
+
+(** Execution of the instruction at [rs PC]. *)
+
+Inductive step: state -> trace -> state -> Prop :=
+ | exec_step_internal:
+ forall b ofs f bi rs m rs' m',
+ rs PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bi ->
+ exec_bblock f bi rs m = Next rs' m' ->
+ step (State rs m) E0 (State rs' m')
+ | exec_step_builtin:
+ forall b ofs f ef args res rs m vargs t vres rs' m' bi,
+ rs PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) f.(fn_blocks) = Some bi ->
+ exit bi = Some (PExpand (Pbuiltin ef args res)) ->
+ eval_builtin_args ge rs (rs SP) m args vargs ->
+ external_call ef ge vargs m t vres m' ->
+ rs' = nextblock bi
+ (set_res res vres
+ (undef_regs (map preg_of (destroyed_by_builtin ef))
+ (rs#RTMP <- Vundef))) ->
+ step (State rs m) t (State rs' m')
+ | exec_step_external:
+ forall b ef args res rs m t rs' m',
+ rs PC = Vptr b Ptrofs.zero ->
+ Genv.find_funct_ptr ge b = Some (External ef) ->
+ external_call ef ge args m t res m' ->
+ extcall_arguments rs m (ef_sig ef) args ->
+ rs' = (set_pair (loc_external_result (ef_sig ef) ) res (undef_caller_save_regs rs))#PC <- (rs RA) ->
+ step (State rs m) t (State rs' m')
+ .
+
+End RELSEM.
+
+Definition semantics (p: program) :=
+ Semantics step (initial_state p) final_state (Genv.globalenv p).
+
+Definition data_preg (r: preg) : bool :=
+ match r with
+ | RA => false
+ | IR GPRA => false
+ | IR RTMP => false
+ | IR _ => true
+ | PC => false
+ end.
diff --git a/kvx/Asmblockdeps.v b/kvx/Asmblockdeps.v
new file mode 100644
index 00000000..a9786e0a
--- /dev/null
+++ b/kvx/Asmblockdeps.v
@@ -0,0 +1,1845 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Translation from [Asmvliw] to [AbstractBB] *)
+
+(** We define a specific instance [L] of [AbstractBB] and translate [bblocks] from [Asmvliw] into [L].
+ [AbstractBB] will then define two semantics for [L]: a sequential and a parallel one.
+ We prove a bisimulation between the parallel semantics of [L] and [AsmVLIW].
+ We also prove a bisimulation between the sequential semantics of [L] and [Asmblock].
+ Then, the checkers on [Asmblock] and [Asmvliw] are deduced from those of [L].
+ *)
+
+Require Import AST.
+Require Import Asmblock.
+Require Import Asmblockgenproof0 Asmblockprops.
+Require Import Values.
+Require Import Globalenvs.
+Require Import Memory.
+Require Import Errors.
+Require Import Integers.
+Require Import Floats.
+Require Import ZArith.
+Require Import Coqlib.
+Require Import ImpSimuTest.
+Require Import Axioms.
+Require Import Parallelizability.
+Require Import Asmvliw Permutation.
+Require Import Chunks.
+
+Require Import Lia.
+
+
+Import ListNotations.
+Local Open Scope list_scope.
+
+Open Scope impure.
+
+(** Definition of [L] *)
+
+Module P<: ImpParam.
+Module R := Pos.
+
+Section IMPPARAM.
+
+Definition env := Genv.t fundef unit.
+
+Inductive genv_wrap := Genv (ge: env) (fn: function).
+Definition genv := genv_wrap.
+
+Variable Ge: genv.
+
+Inductive value_wrap :=
+ | Val (v: val)
+ | Memstate (m: mem)
+.
+
+Definition value := value_wrap.
+
+Inductive control_op :=
+ | Oj_l (l: label)
+ | Ocb (bt: btest) (l: label)
+ | Ocbu (bt: btest) (l: label)
+ | Odiv
+ | Odivu
+ | OError
+ | OIncremPC (sz: Z)
+ | Ojumptable (l: list label)
+.
+
+Inductive arith_op :=
+ | OArithR (n: arith_name_r)
+ | OArithRR (n: arith_name_rr)
+ | OArithRI32 (n: arith_name_ri32) (imm: int)
+ | OArithRI64 (n: arith_name_ri64) (imm: int64)
+ | OArithRF32 (n: arith_name_rf32) (imm: float32)
+ | OArithRF64 (n: arith_name_rf64) (imm: float)
+ | OArithRRR (n: arith_name_rrr)
+ | OArithRRI32 (n: arith_name_rri32) (imm: int)
+ | OArithRRI64 (n: arith_name_rri64) (imm: int64)
+ | OArithARRR (n: arith_name_arrr)
+ | OArithARR (n: arith_name_arr)
+ | OArithARRI32 (n: arith_name_arri32) (imm: int)
+ | OArithARRI64 (n: arith_name_arri64) (imm: int64)
+.
+
+Coercion OArithR: arith_name_r >-> arith_op.
+Coercion OArithRR: arith_name_rr >-> arith_op.
+Coercion OArithRI32: arith_name_ri32 >-> Funclass.
+Coercion OArithRI64: arith_name_ri64 >-> Funclass.
+Coercion OArithRF32: arith_name_rf32 >-> Funclass.
+Coercion OArithRF64: arith_name_rf64 >-> Funclass.
+Coercion OArithRRR: arith_name_rrr >-> arith_op.
+Coercion OArithRRI32: arith_name_rri32 >-> Funclass.
+Coercion OArithRRI64: arith_name_rri64 >-> Funclass.
+
+Inductive load_op :=
+ | OLoadRRO (n: load_name) (trap: trapping_mode) (ofs: offset)
+ | OLoadRRR (n: load_name) (trap: trapping_mode)
+ | OLoadRRRXS (n: load_name) (trap: trapping_mode)
+.
+
+Coercion OLoadRRO: load_name >-> Funclass.
+
+Inductive store_op :=
+ | OStoreRRO (n: store_name) (ofs: offset)
+ | OStoreRRR (n: store_name)
+ | OStoreRRRXS (n: store_name)
+.
+
+Coercion OStoreRRO: store_name >-> Funclass.
+
+Inductive op_wrap :=
+ | Arith (ao: arith_op)
+ | Load (lo: load_op)
+ | Store (so: store_op)
+ | Control (co: control_op)
+ | Allocframe (sz: Z) (pos: ptrofs)
+ | Allocframe2 (sz: Z) (pos: ptrofs)
+ | Freeframe (sz: Z) (pos: ptrofs)
+ | Freeframe2 (sz: Z) (pos: ptrofs)
+ | Constant (v: val)
+ | Fail
+.
+
+Coercion Arith: arith_op >-> op_wrap.
+Coercion Load: load_op >-> op_wrap.
+Coercion Store: store_op >-> op_wrap.
+Coercion Control: control_op >-> op_wrap.
+
+Definition op := op_wrap.
+
+Definition arith_eval (ao: arith_op) (l: list value) :=
+ let (ge, fn) := Ge in
+ match ao, l with
+ | OArithR n, [] => Some (Val (arith_eval_r ge n))
+
+ | OArithRR n, [Val v] => Some (Val (arith_eval_rr n v))
+
+ | OArithRI32 n i, [] => Some (Val (arith_eval_ri32 n i))
+ | OArithRI64 n i, [] => Some (Val (arith_eval_ri64 n i))
+ | OArithRF32 n i, [] => Some (Val (arith_eval_rf32 n i))
+ | OArithRF64 n i, [] => Some (Val (arith_eval_rf64 n i))
+
+ | OArithRRR n, [Val v1; Val v2] => Some (Val (arith_eval_rrr n v1 v2))
+ | OArithRRI32 n i, [Val v] => Some (Val (arith_eval_rri32 n v i))
+ | OArithRRI64 n i, [Val v] => Some (Val (arith_eval_rri64 n v i))
+
+ | OArithARR n, [Val v1; Val v2] => Some (Val (arith_eval_arr n v1 v2))
+ | OArithARRR n, [Val v1; Val v2; Val v3] => Some (Val (arith_eval_arrr n v1 v2 v3))
+ | OArithARRI32 n i, [Val v1; Val v2] => Some (Val (arith_eval_arri32 n v1 v2 i))
+ | OArithARRI64 n i, [Val v1; Val v2] => Some (Val (arith_eval_arri64 n v1 v2 i))
+
+ | _, _ => None
+ end.
+
+Definition exec_incorrect_load trap :=
+ match trap with
+ | TRAP => None
+ | NOTRAP => Some (Val Vundef)
+ end.
+
+Definition exec_load_deps_offset (trap: trapping_mode) (chunk: memory_chunk) (m: mem) (v: val) (ofs: offset) :=
+ let (ge, fn) := Ge in
+ match (eval_offset ofs) with
+ | OK ptr => match Mem.loadv chunk m (Val.offset_ptr v ptr) with
+ | None => exec_incorrect_load trap
+ | Some vl => Some (Val vl)
+ end
+ | _ => None
+ end.
+
+Definition exec_load_deps_reg (trap: trapping_mode) (chunk: memory_chunk) (m: mem) (v vo: val) :=
+ match Mem.loadv chunk m (Val.addl v vo) with
+ | None => exec_incorrect_load trap
+ | Some vl => Some (Val vl)
+ end.
+
+Definition exec_load_deps_regxs (trap: trapping_mode) (chunk: memory_chunk) (m: mem) (v vo: val) :=
+ match Mem.loadv chunk m (Val.addl v (Val.shll vo (scale_of_chunk chunk))) with
+ | None => exec_incorrect_load trap
+ | Some vl => Some (Val vl)
+ end.
+
+Definition load_eval (lo: load_op) (l: list value) :=
+ match lo, l with
+ | OLoadRRO n trap ofs, [Val v; Memstate m] => exec_load_deps_offset trap (load_chunk n) m v ofs
+ | OLoadRRR n trap, [Val v; Val vo; Memstate m] => exec_load_deps_reg trap (load_chunk n) m v vo
+ | OLoadRRRXS n trap, [Val v; Val vo; Memstate m] => exec_load_deps_regxs trap (load_chunk n) m v vo
+ | _, _ => None
+ end.
+
+Definition exec_store_deps_offset (chunk: memory_chunk) (m: mem) (vs va: val) (ofs: offset) :=
+ let (ge, fn) := Ge in
+ match (eval_offset ofs) with
+ | OK ptr => match Mem.storev chunk m (Val.offset_ptr va ptr) vs with
+ | None => None
+ | Some m' => Some (Memstate m')
+ end
+ | _ => None
+ end.
+
+Definition exec_store_deps_reg (chunk: memory_chunk) (m: mem) (vs va vo: val) :=
+ match Mem.storev chunk m (Val.addl va vo) vs with
+ | None => None
+ | Some m' => Some (Memstate m')
+ end.
+
+Definition exec_store_deps_regxs (chunk: memory_chunk) (m: mem) (vs va vo: val) :=
+ match Mem.storev chunk m (Val.addl va (Val.shll vo (scale_of_chunk chunk))) vs with
+ | None => None
+ | Some m' => Some (Memstate m')
+ end.
+
+Definition store_eval (so: store_op) (l: list value) :=
+ match so, l with
+ | OStoreRRO n ofs, [Val vs; Val va; Memstate m] => exec_store_deps_offset (store_chunk n) m vs va ofs
+ | OStoreRRR n, [Val vs; Val va; Val vo; Memstate m] => exec_store_deps_reg (store_chunk n) m vs va vo
+ | OStoreRRRXS n, [Val vs; Val va; Val vo; Memstate m] => exec_store_deps_regxs (store_chunk n) m vs va vo
+ | _, _ => None
+ end.
+
+Local Open Scope Z.
+
+Remark size_chunk_positive: forall chunk,
+ (size_chunk chunk) > 0.
+Proof.
+ destruct chunk; simpl; lia.
+Qed.
+
+Remark size_chunk_small: forall chunk,
+ (size_chunk chunk) <= 8.
+Proof.
+ destruct chunk; simpl; lia.
+Qed.
+
+Definition disjoint_chunks
+ (ofs1 : offset) (chunk1 : memory_chunk)
+ (ofs2 : offset) (chunk2 : memory_chunk) :=
+ Intv.disjoint ((Ptrofs.unsigned ofs1),
+ ((Ptrofs.unsigned ofs1) + (size_chunk chunk1)))
+ ((Ptrofs.unsigned ofs2),
+ ((Ptrofs.unsigned ofs2) + (size_chunk chunk2))).
+
+Definition small_offset_threshold := 18446744073709551608.
+
+Lemma store_store_disjoint_offsets :
+ forall n1 n2 ofs1 ofs2 vs1 vs2 va m0 m1 m2 m1' m2',
+ (disjoint_chunks ofs1 (store_chunk n1) ofs2 (store_chunk n2)) ->
+ (Ptrofs.unsigned ofs1) < small_offset_threshold ->
+ (Ptrofs.unsigned ofs2) < small_offset_threshold ->
+ store_eval (OStoreRRO n1 ofs1) [vs1; va; Memstate m0] = Some (Memstate m1) ->
+ store_eval (OStoreRRO n2 ofs2) [vs2; va; Memstate m1] = Some (Memstate m2) ->
+ store_eval (OStoreRRO n2 ofs2) [vs2; va; Memstate m0] = Some (Memstate m1') ->
+ store_eval (OStoreRRO n1 ofs1) [vs1; va; Memstate m1'] = Some (Memstate m2') ->
+ m2 = m2'.
+Proof.
+ intros until m2'.
+ intros DISJOINT SMALL1 SMALL2 STORE0 STORE1 STORE0' STORE1'.
+ unfold disjoint_chunks in DISJOINT.
+ destruct vs1 as [v1 | ]; simpl in STORE0, STORE1'; try congruence.
+ destruct vs2 as [v2 | ]; simpl in STORE1, STORE0'; try congruence.
+ destruct va as [base | ]; try congruence.
+ unfold exec_store_deps_offset in *.
+ destruct Ge.
+ unfold eval_offset in *; simpl in *.
+ unfold Mem.storev in *.
+ unfold Val.offset_ptr in *.
+ destruct base as [ | | | | | wblock wpofs] in * ; try congruence.
+ destruct (Mem.store _ _ _ _ _) eqn:E0; try congruence.
+ inv STORE0.
+ destruct (Mem.store (store_chunk n2) _ _ _ _) eqn:E1; try congruence.
+ inv STORE1.
+ destruct (Mem.store (store_chunk n2) m0 _ _ _) eqn:E0'; try congruence.
+ inv STORE0'.
+ destruct (Mem.store _ m1' _ _ _) eqn:E1'; try congruence.
+ inv STORE1'.
+ assert (Some m2 = Some m2').
+ 2: congruence.
+ rewrite <- E1.
+ rewrite <- E1'.
+ eapply Mem.store_store_other.
+ 2, 3: eassumption.
+
+ right.
+ pose proof (size_chunk_positive (store_chunk n1)).
+ pose proof (size_chunk_positive (store_chunk n2)).
+ pose proof (size_chunk_small (store_chunk n1)).
+ pose proof (size_chunk_small (store_chunk n2)).
+ destruct (Intv.range_disjoint _ _ DISJOINT) as [DIS | [DIS | DIS]];
+ unfold Intv.empty in DIS; simpl in DIS.
+ 1, 2: lia.
+ pose proof (Ptrofs.unsigned_range ofs1).
+ pose proof (Ptrofs.unsigned_range ofs2).
+ unfold small_offset_threshold in *.
+ destruct (Ptrofs.unsigned_add_either wpofs ofs1) as [R1 | R1]; rewrite R1;
+ destruct (Ptrofs.unsigned_add_either wpofs ofs2) as [R2 | R2]; rewrite R2;
+ change Ptrofs.modulus with 18446744073709551616 in *;
+ lia.
+Qed.
+
+Lemma load_store_disjoint_offsets :
+ forall n1 n2 tm ofs1 ofs2 vs va m0 m1,
+ (disjoint_chunks ofs1 (store_chunk n1) ofs2 (load_chunk n2)) ->
+ (Ptrofs.unsigned ofs1) < small_offset_threshold ->
+ (Ptrofs.unsigned ofs2) < small_offset_threshold ->
+ store_eval (OStoreRRO n1 ofs1) [vs; va; Memstate m0] = Some (Memstate m1) ->
+ load_eval (OLoadRRO n2 tm ofs2) [va; Memstate m1] =
+ load_eval (OLoadRRO n2 tm ofs2) [va; Memstate m0].
+Proof.
+ intros until m1.
+ intros DISJOINT SMALL1 SMALL2 STORE0.
+ destruct vs as [v | ]; simpl in STORE0; try congruence.
+ destruct va as [base | ]; try congruence.
+ unfold exec_store_deps_offset in *.
+ unfold eval_offset in *; simpl in *.
+ unfold exec_load_deps_offset.
+ unfold Mem.storev, Mem.loadv in *.
+ destruct Ge in *.
+ unfold eval_offset in *.
+ unfold Val.offset_ptr in *.
+ destruct base as [ | | | | | wblock wpofs] in * ; try congruence.
+ destruct (Mem.store _ _ _ _) eqn:E0; try congruence.
+ inv STORE0.
+ assert (
+ (Mem.load (load_chunk n2) m1 wblock
+ (Ptrofs.unsigned (Ptrofs.add wpofs ofs2))) =
+ (Mem.load (load_chunk n2) m0 wblock
+ (Ptrofs.unsigned (Ptrofs.add wpofs ofs2))) ) as LOADS.
+ {
+ eapply Mem.load_store_other.
+ eassumption.
+ right.
+ pose proof (size_chunk_positive (store_chunk n1)).
+ pose proof (size_chunk_positive (load_chunk n2)).
+ pose proof (size_chunk_small (store_chunk n1)).
+ pose proof (size_chunk_small (load_chunk n2)).
+ destruct (Intv.range_disjoint _ _ DISJOINT) as [DIS | [DIS | DIS]];
+ unfold Intv.empty in DIS; simpl in DIS.
+ 1,2: lia.
+
+ pose proof (Ptrofs.unsigned_range ofs1).
+ pose proof (Ptrofs.unsigned_range ofs2).
+ unfold small_offset_threshold in *.
+ destruct (Ptrofs.unsigned_add_either wpofs ofs1) as [R1 | R1]; rewrite R1;
+ destruct (Ptrofs.unsigned_add_either wpofs ofs2) as [R2 | R2]; rewrite R2;
+ change Ptrofs.modulus with 18446744073709551616 in *;
+ lia.
+ }
+ destruct (Mem.load _ m1 _ _) in *; destruct (Mem.load _ m0 _ _) in *; congruence.
+Qed.
+
+Definition goto_label_deps (f: function) (lbl: label) (vpc: val) :=
+ match label_pos lbl 0 (fn_blocks f) with
+ | None => None
+ | Some pos =>
+ match vpc with
+ | Vptr b ofs => Some (Val (Vptr b (Ptrofs.repr pos)))
+ | _ => None
+ end
+ end.
+
+Definition eval_branch_deps (f: function) (l: label) (vpc: val) (res: option bool) :=
+ match res with
+ | Some true => goto_label_deps f l vpc
+ | Some false => Some (Val vpc)
+ | None => None
+ end.
+
+Definition control_eval (o: control_op) (l: list value) :=
+ let (ge, fn) := Ge in
+ match o, l with
+ | (Ojumptable tbl), [Val index; Val vpc] =>
+ match index with
+ | Vint n =>
+ match list_nth_z tbl (Int.unsigned n) with
+ | None => None
+ | Some lbl => goto_label_deps fn lbl vpc
+ end
+ | _ => None
+ end
+ | Oj_l l, [Val vpc] => goto_label_deps fn l vpc
+ | Ocb bt l, [Val v; Val vpc] =>
+ match cmp_for_btest bt with
+ | (Some c, Int) => eval_branch_deps fn l vpc (Val.cmp_bool c v (Vint (Int.repr 0)))
+ | (Some c, Long) => eval_branch_deps fn l vpc (Val.cmpl_bool c v (Vlong (Int64.repr 0)))
+ | (None, _) => None
+ end
+ | Ocbu bt l, [Val v; Val vpc] =>
+ match cmpu_for_btest bt with
+ | (Some c, Int) => eval_branch_deps fn l vpc (Val.mxcmpu_bool c v (Vint (Int.repr 0)))
+ | (Some c, Long) => eval_branch_deps fn l vpc (Val.mxcmplu_bool c v (Vlong (Int64.repr 0)))
+ | (None, _) => None
+ end
+ | Odiv, [Val v1; Val v2] =>
+ match Val.divs v1 v2 with
+ | Some v => Some (Val v)
+ | None => None
+ end
+ | Odivu, [Val v1; Val v2] =>
+ match Val.divu v1 v2 with
+ | Some v => Some (Val v)
+ | None => None
+ end
+ | OIncremPC sz, [Val vpc] => Some (Val (Val.offset_ptr vpc (Ptrofs.repr sz)))
+ | OError, _ => None
+ | _, _ => None
+ end.
+
+Definition op_eval (o: op) (l: list value) :=
+ match o, l with
+ | Arith o, l => arith_eval o l
+ | Load o, l => load_eval o l
+ | Store o, l => store_eval o l
+ | Control o, l => control_eval o l
+ | Allocframe sz pos, [Val spv; Memstate m] =>
+ let (m1, stk) := Mem.alloc m 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ match Mem.storev Mptr m1 (Val.offset_ptr sp pos) spv with
+ | None => None
+ | Some m => Some (Memstate m)
+ end
+ | Allocframe2 sz pos, [Val spv; Memstate m] =>
+ let (m1, stk) := Mem.alloc m 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ match Mem.storev Mptr m1 (Val.offset_ptr sp pos) spv with
+ | None => None
+ | Some m => Some (Val sp)
+ end
+ | Freeframe sz pos, [Val spv; Memstate m] =>
+ match Mem.loadv Mptr m (Val.offset_ptr spv pos) with
+ | None => None
+ | Some v =>
+ match spv with
+ | Vptr stk ofs =>
+ match Mem.free m stk 0 sz with
+ | None => None
+ | Some m' => Some (Memstate m')
+ end
+ | _ => None
+ end
+ end
+ | Freeframe2 sz pos, [Val spv; Memstate m] =>
+ match Mem.loadv Mptr m (Val.offset_ptr spv pos) with
+ | None => None
+ | Some v =>
+ match spv with
+ | Vptr stk ofs =>
+ match Mem.free m stk 0 sz with
+ | None => None
+ | Some m' => Some (Val v)
+ end
+ | _ => None
+ end
+ end
+ | Constant v, [] => Some (Val v)
+ | Fail, _ => None
+ | _, _ => None
+ end.
+
+
+Definition arith_op_eq (o1 o2: arith_op): ?? bool :=
+ match o1 with
+ | OArithR n1 =>
+ match o2 with OArithR n2 => struct_eq n1 n2 | _ => RET false end
+ | OArithRR n1 =>
+ match o2 with OArithRR n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithRI32 n1 i1 =>
+ match o2 with OArithRI32 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithRI64 n1 i1 =>
+ match o2 with OArithRI64 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithRF32 n1 i1 =>
+ match o2 with OArithRF32 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithRF64 n1 i1 =>
+ match o2 with OArithRF64 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithRRR n1 =>
+ match o2 with OArithRRR n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithRRI32 n1 i1 =>
+ match o2 with OArithRRI32 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithRRI64 n1 i1 =>
+ match o2 with OArithRRI64 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithARRR n1 =>
+ match o2 with OArithARRR n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithARR n1 =>
+ match o2 with OArithARR n2 => phys_eq n1 n2 | _ => RET false end
+ | OArithARRI32 n1 i1 =>
+ match o2 with OArithARRI32 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ | OArithARRI64 n1 i1 =>
+ match o2 with OArithARRI64 n2 i2 => iandb (phys_eq n1 n2) (phys_eq i1 i2) | _ => RET false end
+ end.
+
+Ltac my_wlp_simplify := wlp_xsimplify ltac:(intros; subst; simpl in * |- *; congruence || intuition eauto with wlp).
+
+Lemma arith_op_eq_correct o1 o2:
+ WHEN arith_op_eq o1 o2 ~> b THEN b = true -> o1 = o2.
+Proof.
+ destruct o1, o2; my_wlp_simplify; try congruence.
+Qed.
+Hint Resolve arith_op_eq_correct: wlp.
+Opaque arith_op_eq_correct.
+
+Definition offset_eq (ofs1 ofs2 : offset): ?? bool :=
+ RET (Ptrofs.eq ofs1 ofs2).
+
+Lemma offset_eq_correct ofs1 ofs2:
+ WHEN offset_eq ofs1 ofs2 ~> b THEN b = true -> ofs1 = ofs2.
+Proof.
+ wlp_simplify.
+ pose (Ptrofs.eq_spec ofs1 ofs2).
+ rewrite H in *.
+ trivial.
+Qed.
+Hint Resolve offset_eq_correct: wlp.
+
+Definition trapping_mode_eq trap1 trap2 :=
+ RET (match trap1, trap2 with
+ | TRAP, TRAP | NOTRAP, NOTRAP => true
+ | TRAP, NOTRAP | NOTRAP, TRAP => false
+ end).
+Lemma trapping_mode_eq_correct t1 t2:
+ WHEN trapping_mode_eq t1 t2 ~> b THEN b = true -> t1 = t2.
+Proof.
+ wlp_simplify.
+ destruct t1; destruct t2; trivial; discriminate.
+Qed.
+Hint Resolve trapping_mode_eq_correct: wlp.
+
+Definition load_op_eq (o1 o2: load_op): ?? bool :=
+ match o1 with
+ | OLoadRRO n1 trap ofs1 =>
+ match o2 with
+ | OLoadRRO n2 trap2 ofs2 => iandb (phys_eq n1 n2) (iandb (offset_eq ofs1 ofs2) (trapping_mode_eq trap trap2))
+ | _ => RET false
+ end
+ | OLoadRRR n1 trap =>
+ match o2 with
+ | OLoadRRR n2 trap2 => iandb (phys_eq n1 n2) (trapping_mode_eq trap trap2)
+ | _ => RET false
+ end
+ | OLoadRRRXS n1 trap =>
+ match o2 with
+ | OLoadRRRXS n2 trap2 => iandb (phys_eq n1 n2) (trapping_mode_eq trap trap2)
+ | _ => RET false
+ end
+ end.
+
+Lemma load_op_eq_correct o1 o2:
+ WHEN load_op_eq o1 o2 ~> b THEN b = true -> o1 = o2.
+Proof.
+ destruct o1, o2; wlp_simplify; try discriminate.
+ { f_equal.
+ destruct trap, trap0; simpl in *; trivial; discriminate.
+ pose (Ptrofs.eq_spec ofs ofs0).
+ rewrite H in *. trivial. }
+ all: destruct trap, trap0; simpl in *; trivial; discriminate.
+Qed.
+Hint Resolve load_op_eq_correct: wlp.
+Opaque load_op_eq_correct.
+
+Definition store_op_eq (o1 o2: store_op): ?? bool :=
+ match o1 with
+ | OStoreRRO n1 ofs1 =>
+ match o2 with OStoreRRO n2 ofs2 => iandb (phys_eq n1 n2) (offset_eq ofs1 ofs2) | _ => RET false end
+ | OStoreRRR n1 =>
+ match o2 with OStoreRRR n2 => phys_eq n1 n2 | _ => RET false end
+ | OStoreRRRXS n1 =>
+ match o2 with OStoreRRRXS n2 => phys_eq n1 n2 | _ => RET false end
+ end.
+
+Lemma store_op_eq_correct o1 o2:
+ WHEN store_op_eq o1 o2 ~> b THEN b = true -> o1 = o2.
+Proof.
+ destruct o1, o2; wlp_simplify; try discriminate.
+ - f_equal. pose (Ptrofs.eq_spec ofs ofs0).
+ rewrite H in *. trivial.
+ - congruence.
+ - congruence.
+Qed.
+Hint Resolve store_op_eq_correct: wlp.
+Opaque store_op_eq_correct.
+
+Definition control_op_eq (c1 c2: control_op): ?? bool :=
+ match c1 with
+ | Oj_l l1 =>
+ match c2 with Oj_l l2 => phys_eq l1 l2 | _ => RET false end
+ | Ocb bt1 l1 =>
+ match c2 with Ocb bt2 l2 => iandb (phys_eq bt1 bt2) (phys_eq l1 l2) | _ => RET false end
+ | Ocbu bt1 l1 =>
+ match c2 with Ocbu bt2 l2 => iandb (phys_eq bt1 bt2) (phys_eq l1 l2) | _ => RET false end
+ | Ojumptable tbl1 =>
+ match c2 with Ojumptable tbl2 => phys_eq tbl1 tbl2 | _ => RET false end
+ | Odiv =>
+ match c2 with Odiv => RET true | _ => RET false end
+ | Odivu =>
+ match c2 with Odivu => RET true | _ => RET false end
+ | OIncremPC sz1 =>
+ match c2 with OIncremPC sz2 => RET (Z.eqb sz1 sz2) | _ => RET false end
+ | OError =>
+ match c2 with OError => RET true | _ => RET false end
+ end.
+
+Lemma control_op_eq_correct c1 c2:
+ WHEN control_op_eq c1 c2 ~> b THEN b = true -> c1 = c2.
+Proof.
+ destruct c1, c2; wlp_simplify; try rewrite Z.eqb_eq in * |-; try congruence.
+Qed.
+Hint Resolve control_op_eq_correct: wlp.
+Opaque control_op_eq_correct.
+
+Definition op_eq (o1 o2: op): ?? bool :=
+ match o1 with
+ | Arith i1 =>
+ match o2 with Arith i2 => arith_op_eq i1 i2 | _ => RET false end
+ | Load i1 =>
+ match o2 with Load i2 => load_op_eq i1 i2 | _ => RET false end
+ | Store i1 =>
+ match o2 with Store i2 => store_op_eq i1 i2 | _ => RET false end
+ | Control i1 =>
+ match o2 with Control i2 => control_op_eq i1 i2 | _ => RET false end
+ | Allocframe sz1 pos1 =>
+ match o2 with Allocframe sz2 pos2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq pos1 pos2) | _ => RET false end
+ | Allocframe2 sz1 pos1 =>
+ match o2 with Allocframe2 sz2 pos2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq pos1 pos2) | _ => RET false end
+ | Freeframe sz1 pos1 =>
+ match o2 with Freeframe sz2 pos2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq pos1 pos2) | _ => RET false end
+ | Freeframe2 sz1 pos1 =>
+ match o2 with Freeframe2 sz2 pos2 => iandb (RET (Z.eqb sz1 sz2)) (phys_eq pos1 pos2) | _ => RET false end
+ | Constant c1 =>
+ match o2 with Constant c2 => phys_eq c1 c2 | _ => RET false end
+ | Fail =>
+ match o2 with Fail => RET true | _ => RET false end
+ end.
+
+Theorem op_eq_correct o1 o2:
+ WHEN op_eq o1 o2 ~> b THEN b=true -> o1 = o2.
+Proof.
+ destruct o1, o2; wlp_simplify; try rewrite Z.eqb_eq in * |- ; try congruence.
+Qed.
+Hint Resolve op_eq_correct: wlp.
+Global Opaque op_eq_correct.
+
+End IMPPARAM.
+
+End P.
+
+Module L <: ISeqLanguage with Module LP:=P.
+
+Module LP:=P.
+
+Include MkSeqLanguage P.
+
+End L.
+
+Module IST := ImpSimu L ImpPosDict.
+
+Import L.
+Import P.
+
+(** Compilation from [Asmvliw] to [L] *)
+
+Local Open Scope positive_scope.
+
+Definition pmem : R.t := 1.
+
+Definition ireg_to_pos (ir: ireg) : R.t :=
+ match ir with
+ | GPR0 => 1 | GPR1 => 2 | GPR2 => 3 | GPR3 => 4 | GPR4 => 5 | GPR5 => 6 | GPR6 => 7 | GPR7 => 8 | GPR8 => 9 | GPR9 => 10
+ | GPR10 => 11 | GPR11 => 12 | GPR12 => 13 | GPR13 => 14 | GPR14 => 15 | GPR15 => 16 | GPR16 => 17 | GPR17 => 18 | GPR18 => 19 | GPR19 => 20
+ | GPR20 => 21 | GPR21 => 22 | GPR22 => 23 | GPR23 => 24 | GPR24 => 25 | GPR25 => 26 | GPR26 => 27 | GPR27 => 28 | GPR28 => 29 | GPR29 => 30
+ | GPR30 => 31 | GPR31 => 32 | GPR32 => 33 | GPR33 => 34 | GPR34 => 35 | GPR35 => 36 | GPR36 => 37 | GPR37 => 38 | GPR38 => 39 | GPR39 => 40
+ | GPR40 => 41 | GPR41 => 42 | GPR42 => 43 | GPR43 => 44 | GPR44 => 45 | GPR45 => 46 | GPR46 => 47 | GPR47 => 48 | GPR48 => 49 | GPR49 => 50
+ | GPR50 => 51 | GPR51 => 52 | GPR52 => 53 | GPR53 => 54 | GPR54 => 55 | GPR55 => 56 | GPR56 => 57 | GPR57 => 58 | GPR58 => 59 | GPR59 => 60
+ | GPR60 => 61 | GPR61 => 62 | GPR62 => 63 | GPR63 => 64
+ end
+.
+
+Lemma ireg_to_pos_discr: forall r r', r <> r' -> ireg_to_pos r <> ireg_to_pos r'.
+Proof.
+ destruct r; destruct r'; try contradiction; discriminate.
+Qed.
+
+Definition ppos (r: preg) : R.t :=
+ match r with
+ | RA => 2
+ | PC => 3
+ | IR ir => 3 + ireg_to_pos ir
+ end
+.
+
+Notation "# r" := (ppos r) (at level 100, right associativity).
+
+Lemma not_eq_add:
+ forall k n n', n <> n' -> k + n <> k + n'.
+Proof.
+ intros k n n' H1 H2. apply H1; clear H1. eapply Pos.add_reg_l; eauto.
+Qed.
+
+Lemma ppos_discr: forall r r', r <> r' -> ppos r <> ppos r'.
+Proof.
+ destruct r; destruct r'.
+ all: try discriminate; try contradiction.
+ - intros. apply not_eq_add. apply ireg_to_pos_discr. congruence.
+ - intros. unfold ppos. replace (3 + ireg_to_pos g) with ((1 + ireg_to_pos g) + 2).
+ apply Pos.add_no_neutral.
+ rewrite Pos.add_comm, Pos.add_assoc. reflexivity.
+ - intros. unfold ppos. rewrite Pos.add_comm. apply Pos.add_no_neutral.
+ - intros. unfold ppos. apply not_eq_sym.
+ replace (3 + ireg_to_pos g) with ((1 + ireg_to_pos g) + 2). apply Pos.add_no_neutral.
+ rewrite Pos.add_comm, Pos.add_assoc. reflexivity.
+ - intros. unfold ppos. apply not_eq_sym. rewrite Pos.add_comm. apply Pos.add_no_neutral.
+Qed.
+
+Lemma ppos_pmem_discr: forall r, pmem <> ppos r.
+Proof.
+ intros. destruct r.
+ - unfold ppos. unfold pmem. apply not_eq_sym. rewrite Pos.add_comm. replace 3 with (2 + 1). rewrite Pos.add_assoc. apply Pos.add_no_neutral.
+ reflexivity.
+ - unfold ppos. unfold pmem. discriminate.
+ - unfold ppos. unfold pmem. discriminate.
+Qed.
+
+(** Inversion functions, used for debug traces *)
+
+Definition pos_to_ireg (p: R.t) : option gpreg :=
+ match p with
+ | 1 => Some GPR0 | 2 => Some GPR1 | 3 => Some GPR2 | 4 => Some GPR3 | 5 => Some GPR4 | 6 => Some GPR5 | 7 => Some GPR6 | 8 => Some GPR7 | 9 => Some GPR8 | 10 => Some GPR9
+ | 11 => Some GPR10 | 12 => Some GPR11 | 13 => Some GPR12 | 14 => Some GPR13 | 15 => Some GPR14 | 16 => Some GPR15 | 17 => Some GPR16 | 18 => Some GPR17 | 19 => Some GPR18 | 20 => Some GPR19
+ | 21 => Some GPR20 | 22 => Some GPR21 | 23 => Some GPR22 | 24 => Some GPR23 | 25 => Some GPR24 | 26 => Some GPR25 | 27 => Some GPR26 | 28 => Some GPR27 | 29 => Some GPR28 | 30 => Some GPR29
+ | 31 => Some GPR30 | 32 => Some GPR31 | 33 => Some GPR32 | 34 => Some GPR33 | 35 => Some GPR34 | 36 => Some GPR35 | 37 => Some GPR36 | 38 => Some GPR37 | 39 => Some GPR38 | 40 => Some GPR39
+ | 41 => Some GPR40 | 42 => Some GPR41 | 43 => Some GPR42 | 44 => Some GPR43 | 45 => Some GPR44 | 46 => Some GPR45 | 47 => Some GPR46 | 48 => Some GPR47 | 49 => Some GPR48 | 50 => Some GPR49
+ | 51 => Some GPR50 | 52 => Some GPR51 | 53 => Some GPR52 | 54 => Some GPR53 | 55 => Some GPR54 | 56 => Some GPR55 | 57 => Some GPR56 | 58 => Some GPR57 | 59 => Some GPR58 | 60 => Some GPR59
+ | 61 => Some GPR60 | 62 => Some GPR61 | 63 => Some GPR62 | 64 => Some GPR63
+ | _ => None
+ end.
+
+Definition inv_ppos (p: R.t) : option preg :=
+ match p with
+ | 1 => None
+ | 2 => Some RA | 3 => Some PC
+ | n => match pos_to_ireg (n-3) with
+ | None => None
+ | Some gpr => Some (IR gpr)
+ end
+ end.
+
+Notation "a @ b" := (Econs a b) (at level 102, right associativity).
+
+(** Translations of instructions *)
+
+Definition trans_control (ctl: control) : inst :=
+ match ctl with
+ | Pret => [(#PC, PReg(#RA))]
+ | Pcall s => [(#RA, PReg(#PC)); (#PC, Op (Arith (OArithR (Ploadsymbol s Ptrofs.zero))) Enil)]
+ | Picall r => [(#RA, PReg(#PC)); (#PC, PReg(#r))]
+ | Pgoto s => [(#PC, Op (Arith (OArithR (Ploadsymbol s Ptrofs.zero))) Enil)]
+ | Pigoto r => [(#PC, PReg(#r))]
+ | Pj_l l => [(#PC, Op (Control (Oj_l l)) (PReg(#PC) @ Enil))]
+ | Pcb bt r l => [(#PC, Op (Control (Ocb bt l)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Pcbu bt r l => [(#PC, Op (Control (Ocbu bt l)) (PReg(#r) @ PReg(#PC) @ Enil))]
+ | Pjumptable r labels => [(#PC, Op (Control (Ojumptable labels)) (PReg(#r) @ PReg(#PC) @ Enil));
+ (#GPR62, Op (Constant Vundef) Enil);
+ (#GPR63, Op (Constant Vundef) Enil) ]
+ | Pbuiltin ef args res => [(#PC, Op (Control (OError)) Enil)]
+ end.
+
+Definition trans_exit (ex: option control) : L.inst :=
+ match ex with
+ | None => []
+ | Some ctl => trans_control ctl
+ end
+.
+
+Definition trans_arith (ai: ar_instruction) : inst :=
+ match ai with
+ | PArithR n d => [(#d, Op (Arith (OArithR n)) Enil)]
+ | PArithRR n d s => [(#d, Op (Arith (OArithRR n)) (PReg(#s) @ Enil))]
+ | PArithRI32 n d i => [(#d, Op (Arith (OArithRI32 n i)) Enil)]
+ | PArithRI64 n d i => [(#d, Op (Arith (OArithRI64 n i)) Enil)]
+ | PArithRF32 n d i => [(#d, Op (Arith (OArithRF32 n i)) Enil)]
+ | PArithRF64 n d i => [(#d, Op (Arith (OArithRF64 n i)) Enil)]
+ | PArithRRR n d s1 s2 => [(#d, Op (Arith (OArithRRR n)) (PReg(#s1) @ PReg(#s2) @ Enil))]
+ | PArithRRI32 n d s i => [(#d, Op (Arith (OArithRRI32 n i)) (PReg(#s) @ Enil))]
+ | PArithRRI64 n d s i => [(#d, Op (Arith (OArithRRI64 n i)) (PReg(#s) @ Enil))]
+ | PArithARRR n d s1 s2 => [(#d, Op (Arith (OArithARRR n)) (PReg(#d) @ PReg(#s1) @ PReg(#s2) @ Enil))]
+ | PArithARR n d s => [(#d, Op (Arith (OArithARR n)) (PReg(#d) @ PReg(#s) @ Enil))]
+ | PArithARRI32 n d s i => [(#d, Op (Arith (OArithARRI32 n i)) (PReg(#d) @ PReg(#s) @ Enil))]
+ | PArithARRI64 n d s i => [(#d, Op (Arith (OArithARRI64 n i)) (PReg(#d) @ PReg(#s) @ Enil))]
+ end.
+
+
+Definition trans_basic (b: basic) : inst :=
+ match b with
+ | PArith ai => trans_arith ai
+ | PLoadRRO trap n d a ofs => [(#d, Op (Load (OLoadRRO n trap ofs)) (PReg (#a) @ PReg pmem @ Enil))]
+ | PLoadRRR trap n d a ro => [(#d, Op (Load (OLoadRRR n trap)) (PReg (#a) @ PReg (#ro) @ PReg pmem @ Enil))]
+ | PLoadRRRXS trap n d a ro => [(#d, Op (Load (OLoadRRRXS n trap)) (PReg (#a) @ PReg (#ro) @ PReg pmem @ Enil))]
+ | PStoreRRO n s a ofs => [(pmem, Op (Store (OStoreRRO n ofs)) (PReg (#s) @ PReg (#a) @ PReg pmem @ Enil))]
+ | PLoadQRRO qd a ofs =>
+ let (d0, d1) := gpreg_q_expand qd in
+ [(#d0, Op (Load (OLoadRRO Pld_a TRAP ofs)) (PReg (#a) @ PReg pmem @ Enil));
+ (#d1, Op (Load (OLoadRRO Pld_a TRAP (Ptrofs.add ofs (Ptrofs.repr 8)))) (Old(PReg (#a)) @ PReg pmem @ Enil))]
+ | PLoadORRO od a ofs =>
+ match gpreg_o_expand od with
+ | (d0, d1, d2, d3) =>
+ [(#d0, Op (Load (OLoadRRO Pld_a TRAP ofs)) (PReg (#a) @ PReg pmem @ Enil));
+ (#d1, Op (Load (OLoadRRO Pld_a TRAP (Ptrofs.add ofs (Ptrofs.repr 8)))) (Old(PReg (#a)) @ PReg pmem @ Enil));
+ (#d2, Op (Load (OLoadRRO Pld_a TRAP (Ptrofs.add ofs (Ptrofs.repr 16)))) (Old(PReg (#a)) @ PReg pmem @ Enil));
+ (#d3, Op (Load (OLoadRRO Pld_a TRAP (Ptrofs.add ofs (Ptrofs.repr 24)))) (Old(PReg (#a)) @ PReg pmem @ Enil))]
+ end
+ | PStoreRRR n s a ro => [(pmem, Op (Store (OStoreRRR n)) (PReg (#s) @ PReg (#a) @ PReg (#ro) @ PReg pmem @ Enil))]
+ | PStoreRRRXS n s a ro => [(pmem, Op (Store (OStoreRRRXS n)) (PReg (#s) @ PReg (#a) @ PReg (#ro) @ PReg pmem @ Enil))]
+ | PStoreQRRO qs a ofs =>
+ let (s0, s1) := gpreg_q_expand qs in
+ [(pmem, Op (Store (OStoreRRO Psd_a ofs)) (PReg (#s0) @ PReg (#a) @ PReg pmem @ Enil));
+ (pmem, Op (Store (OStoreRRO Psd_a (Ptrofs.add ofs (Ptrofs.repr 8)))) (PReg (#s1) @ PReg (#a) @ PReg pmem @ Enil))]
+ | PStoreORRO os a ofs =>
+ match gpreg_o_expand os with
+ | (s0, s1, s2, s3) =>
+ [(pmem, Op (Store (OStoreRRO Psd_a ofs)) (PReg (#s0) @ PReg (#a) @ PReg pmem @ Enil));
+ (pmem, Op (Store (OStoreRRO Psd_a (Ptrofs.add ofs (Ptrofs.repr 8)))) (PReg (#s1) @ PReg (#a) @ PReg pmem @ Enil));
+ (pmem, Op (Store (OStoreRRO Psd_a (Ptrofs.add ofs (Ptrofs.repr 16)))) (PReg (#s2) @ PReg (#a) @ PReg pmem @ Enil));
+ (pmem, Op (Store (OStoreRRO Psd_a (Ptrofs.add ofs (Ptrofs.repr 24)))) (PReg (#s3) @ PReg (#a) @ PReg pmem @ Enil))]
+ end
+ | Pallocframe sz pos => [(#FP, PReg (#SP)); (#SP, Op (Allocframe2 sz pos) (PReg (#SP) @ PReg pmem @ Enil)); (#RTMP, Op (Constant Vundef) Enil);
+ (pmem, Op (Allocframe sz pos) (Old (PReg (#SP)) @ PReg pmem @ Enil))]
+ | Pfreeframe sz pos => [(pmem, Op (Freeframe sz pos) (PReg (#SP) @ PReg pmem @ Enil));
+ (#SP, Op (Freeframe2 sz pos) (PReg (#SP) @ Old (PReg pmem) @ Enil));
+ (#RTMP, Op (Constant Vundef) Enil)]
+ | Pget rd ra => match ra with
+ | RA => [(#rd, PReg(#ra))]
+ | _ => [(#rd, Op Fail Enil)]
+ end
+ | Pset ra rd => match ra with
+ | RA => [(#ra, PReg(#rd))]
+ | _ => [(#rd, Op Fail Enil)]
+ end
+ | Pnop => []
+ end.
+
+Fixpoint trans_body (b: list basic) : list L.inst :=
+ match b with
+ | nil => nil
+ | b :: lb => (trans_basic b) :: (trans_body lb)
+ end.
+
+Definition trans_pcincr (sz: Z) (k: L.inst) := (#PC, Op (Control (OIncremPC sz)) (PReg(#PC) @ Enil)) :: k.
+
+Definition trans_block (b: Asmvliw.bblock) : L.bblock :=
+ trans_body (body b) ++ (trans_pcincr (size b) (trans_exit (exit b)) :: nil).
+
+Theorem trans_block_noheader_inv: forall bb, trans_block (no_header bb) = trans_block bb.
+Proof.
+ intros. destruct bb as [hd bdy ex COR]; unfold no_header; simpl. unfold trans_block. simpl. reflexivity.
+Qed.
+
+Theorem trans_block_header_inv: forall bb hd, trans_block (stick_header hd bb) = trans_block bb.
+Proof.
+ intros. destruct bb as [hdr bdy ex COR]; unfold no_header; simpl. unfold trans_block. simpl. reflexivity.
+Qed.
+
+(** Lemmas on the translation *)
+
+Definition state := L.mem.
+Definition exec := L.run.
+
+Definition match_states (s: Asmvliw.state) (s': state) :=
+ let (rs, m) := s in
+ s' pmem = Memstate m
+ /\ forall r, s' (#r) = Val (rs r).
+
+Definition match_outcome (o:outcome) (s: option state) :=
+ match o with
+ | Next rs m => exists s', s=Some s' /\ match_states (State rs m) s'
+ | Stuck => s=None
+ end.
+
+Notation "a <[ b <- c ]>" := (assign a b c) (at level 102, right associativity).
+
+Definition trans_state (s: Asmvliw.state) : state :=
+ let (rs, m) := s in
+ fun x => if (Pos.eq_dec x pmem) then Memstate m
+ else match (inv_ppos x) with
+ | Some r => Val (rs r)
+ | None => Val Vundef
+ end.
+
+Lemma not_eq_IR:
+ forall r r', r <> r' -> IR r <> IR r'.
+Proof.
+ intros. congruence.
+Qed.
+
+(** Parallelizability test of a bblock (bundle), and bisimulation of the Asmblock and L parallel semantics *)
+
+Module PChk := ParallelChecks L PosPseudoRegSet.
+
+Definition bblock_para_check (p: Asmvliw.bblock) : bool :=
+ PChk.is_parallelizable (trans_block p).
+
+Section SECT_PAR.
+
+Import PChk.
+
+Ltac Simplif :=
+ ((rewrite nextblock_inv by eauto with asmgen)
+ || (rewrite nextblock_inv1 by eauto with asmgen)
+ || (rewrite Pregmap.gss)
+ || (rewrite nextblock_pc)
+ || (rewrite Pregmap.gso by eauto with asmgen)
+ || (rewrite assign_diff by (auto; try discriminate; try (apply ppos_discr; try discriminate; congruence); try (apply ppos_pmem_discr);
+ try (apply not_eq_sym; apply ppos_discr; try discriminate; congruence); try (apply not_eq_sym; apply ppos_pmem_discr); auto))
+ || (rewrite assign_eq)
+ ); auto with asmgen.
+
+Ltac Simpl := repeat Simplif.
+
+Arguments Pos.add: simpl never.
+Arguments ppos: simpl never.
+
+Variable Ge: genv.
+
+Lemma trans_arith_par_correct ge fn rsr mr sr rsw mw sw rsw' i:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_states (State rsw mw) sw ->
+ parexec_arith_instr ge i rsr rsw = rsw' ->
+ exists sw',
+ inst_prun Ge (trans_arith i) sw sr sr = Some sw'
+ /\ match_states (State rsw' mw) sw'.
+Proof.
+ intros GENV MSR MSW PARARITH. subst. inv MSR. inv MSW.
+ unfold parexec_arith_instr. destruct i.
+(* Ploadsymbol *)
+ - destruct i. eexists; split; [| split].
+ * simpl. reflexivity.
+ * Simpl.
+ * simpl. intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRR *)
+ - eexists; split; [| split].
+ * simpl. rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRI32 *)
+ - eexists; split; [|split].
+ * simpl. reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRI64 *)
+ - eexists; split; [|split].
+ * simpl. reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRF32 *)
+ - eexists; split; [|split].
+ * simpl. reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRF64 *)
+ - eexists; split; [|split].
+ * simpl. reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRRR *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rs1). rewrite (H0 rs2). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRRI32 *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithRRI64 *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithARRR *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rd). rewrite (H0 rs1). rewrite (H0 rs2). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithARR *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rd). rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithARRI32 *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rd). rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* PArithARRI64 *)
+ - eexists; split; [|split].
+ * simpl. rewrite (H0 rd). rewrite (H0 rs). reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+Qed.
+
+
+
+Theorem bisimu_par_wio_basic ge fn rsr rsw mr mw sr sw bi:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_states (State rsw mw) sw ->
+ match_outcome (bstep ge bi rsr rsw mr mw) (inst_prun Ge (trans_basic bi) sw sr sr).
+Proof.
+
+(* a little tactic to automate reasoning on preg_eq *)
+Local Hint Resolve not_eq_sym ppos_pmem_discr ppos_discr: core.
+Local Ltac preg_eq_discr r rd :=
+ destruct (preg_eq r rd); try (subst r; rewrite assign_eq, Pregmap.gss; auto);
+ rewrite (assign_diff _ (#rd) (#r) _); auto;
+ rewrite Pregmap.gso; auto.
+
+ intros GENV MSR MSW; inversion MSR as (H & H0); inversion MSW as (H1 & H2).
+ destruct bi; simpl.
+(* Arith *)
+ - exploit trans_arith_par_correct. 5: eauto. all: eauto.
+(* Load *)
+ - destruct i.
+ (* Load Offset *)
+ + destruct i; simpl load_chunk. all:
+ unfold parexec_load_offset; simpl; unfold exec_load_deps_offset; erewrite GENV, H, H0;
+ unfold eval_offset;
+ simpl; auto;
+ destruct (Mem.loadv _ _ _) eqn:MEML; destruct trap; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl; destruct (ireg_eq g rd); subst; Simpl.
+
+ (* Load Reg *)
+ + destruct i; simpl load_chunk. all:
+ unfold parexec_load_reg; simpl; unfold exec_load_deps_reg; rewrite H, H0; rewrite (H0 rofs);
+ destruct (Mem.loadv _ _ _) eqn:MEML; destruct trap; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl; destruct (ireg_eq g rd); subst; Simpl.
+
+ (* Load Reg XS *)
+ + destruct i; simpl load_chunk. all:
+ unfold parexec_load_regxs; simpl; unfold exec_load_deps_regxs; rewrite H, H0; rewrite (H0 rofs);
+ destruct (Mem.loadv _ _ _) eqn:MEML; destruct trap; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl; destruct (ireg_eq g rd); subst; Simpl.
+
+ (* Load Quad word *)
+ + unfold parexec_load_q_offset.
+ destruct (gpreg_q_expand rd) as [rd0 rd1]; destruct Ge; simpl.
+ rewrite H0, H.
+ destruct (Mem.loadv Many64 mr _) as [load0 | ]; simpl; auto.
+ rewrite !(assign_diff _ _ pmem), H; auto.
+ destruct (Mem.loadv Many64 mr (_ _ (Ptrofs.add ofs (Ptrofs.repr 8)))) as [load1| ]; simpl; auto.
+ eexists; intuition eauto.
+ { rewrite !(assign_diff _ _ pmem); auto. }
+ { preg_eq_discr r rd1.
+ preg_eq_discr r rd0. }
+
+ (* Load Octuple word *)
+ + Local Hint Resolve not_eq_sym ppos_pmem_discr ppos_discr: core.
+ unfold parexec_load_o_offset.
+ destruct (gpreg_o_expand rd) as [[[rd0 rd1] rd2] rd3]; destruct Ge; simpl.
+ rewrite H0, H.
+ destruct (Mem.loadv Many64 mr (Val.offset_ptr (rsr ra) ofs)) as [load0 | ]; simpl; auto.
+ rewrite !(assign_diff _ _ pmem), !H; auto.
+ destruct (Mem.loadv Many64 mr (_ _ (Ptrofs.add ofs (Ptrofs.repr 8)))) as [load1| ]; simpl; auto.
+ rewrite !(assign_diff _ _ pmem), !H; auto.
+ destruct (Mem.loadv Many64 mr (_ _ (Ptrofs.add ofs (Ptrofs.repr 16)))) as [load2| ]; simpl; auto.
+ rewrite !(assign_diff _ _ pmem), !H; auto.
+ destruct (Mem.loadv Many64 mr (_ _ (Ptrofs.add ofs (Ptrofs.repr 24)))) as [load3| ]; simpl; auto.
+ eexists; intuition eauto.
+ { rewrite !(assign_diff _ _ pmem); auto. }
+ { preg_eq_discr r rd3.
+ preg_eq_discr r rd2.
+ preg_eq_discr r rd1.
+ preg_eq_discr r rd0. }
+
+(* Store *)
+ - destruct i.
+ (* Store Offset *)
+ + destruct i; simpl store_chunk. all:
+ unfold parexec_store_offset; simpl; unfold exec_store_deps_offset; erewrite GENV, H, H0; rewrite (H0 ra);
+ unfold eval_offset; simpl; auto;
+ destruct (Mem.storev _ _ _ _) eqn:MEML; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl.
+
+ (* Store Reg *)
+ + destruct i; simpl store_chunk. all:
+ unfold parexec_store_reg; simpl; unfold exec_store_deps_reg; rewrite H, H0; rewrite (H0 ra); rewrite (H0 rofs);
+ destruct (Mem.storev _ _ _ _) eqn:MEML; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl.
+
+ (* Store Reg XS *)
+ + destruct i; simpl store_chunk. all:
+ unfold parexec_store_regxs; simpl; unfold exec_store_deps_regxs; rewrite H, H0; rewrite (H0 ra); rewrite (H0 rofs);
+ destruct (Mem.storev _ _ _ _) eqn:MEML; simpl; auto;
+ eexists; split; try split; Simpl;
+ intros rr; destruct rr; Simpl.
+
+ (* Store Quad Word *)
+ + unfold parexec_store_q_offset.
+ destruct (gpreg_q_expand rs) as [s0 s1]; destruct Ge; simpl.
+ rewrite !H0, !H.
+ destruct (Mem.storev _ _ _ (rsr s0)) as [mem0 | ]; simpl; auto.
+ rewrite !assign_diff, !H0; auto.
+ destruct (Mem.storev _ _ _ (rsr s1)) as [mem1 | ]; simpl; auto.
+ eexists; intuition eauto.
+ rewrite !assign_diff; auto.
+
+ (* Store Ocuple Word *)
+ + unfold parexec_store_o_offset.
+ destruct (gpreg_o_expand rs) as [[[s0 s1] s2] s3]; destruct Ge; simpl.
+ rewrite !H0, !H.
+ destruct (Mem.storev _ _ _ (rsr s0)) as [store0 | ]; simpl; auto.
+ rewrite !assign_diff, !H0; auto.
+ destruct (Mem.storev _ _ _ (rsr s1)) as [store1 | ]; simpl; auto.
+ rewrite !assign_diff, !H0; auto.
+ destruct (Mem.storev _ _ _ (rsr s2)) as [store2 | ]; simpl; auto.
+ rewrite !assign_diff, !H0; auto.
+ destruct (Mem.storev _ _ _ (rsr s3)) as [store3 | ]; simpl; auto.
+ eexists; intuition eauto.
+ rewrite !assign_diff; auto.
+
+ (* Allocframe *)
+ - destruct (Mem.alloc _ _ _) eqn:MEMAL. destruct (Mem.store _ _ _ _) eqn:MEMS.
+ * eexists; repeat split.
+ { Simpl. erewrite !H0, H, MEMAL, MEMS. Simpl.
+ rewrite H, MEMAL. rewrite MEMS. reflexivity. }
+ { Simpl. }
+ { intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g GPR32); [| destruct (ireg_eq g GPR12); [| destruct (ireg_eq g GPR17)]]; subst; Simpl. }
+ * simpl; Simpl; erewrite !H0, H, MEMAL, MEMS; auto.
+ (* Freeframe *)
+ - erewrite !H0, H.
+ destruct (Mem.loadv _ _ _) eqn:MLOAD; simpl; auto.
+ destruct (rsr GPR12) eqn:SPeq; simpl; auto.
+ destruct (Mem.free _ _ _ _) eqn:MFREE; simpl; auto.
+ eexists; repeat split.
+ * simpl. Simpl. erewrite H0, SPeq, MLOAD, MFREE. reflexivity.
+ * Simpl.
+ * intros rr; destruct rr; Simpl. destruct (ireg_eq g GPR32); [| destruct (ireg_eq g GPR12); [| destruct (ireg_eq g GPR17)]]; subst; Simpl.
+(* Pget *)
+ - destruct rs eqn:rseq; simpl; auto.
+ eexists. repeat split. Simpl. intros rr; destruct rr; Simpl.
+ destruct (ireg_eq g rd); subst; Simpl.
+(* Pset *)
+ - destruct rd eqn:rdeq; simpl; auto.
+ eexists. repeat split. Simpl. intros rr; destruct rr; Simpl.
+(* Pnop *)
+ - eexists. repeat split; assumption.
+Qed.
+
+
+Theorem bisimu_par_body:
+ forall bdy ge fn rsr mr sr rsw mw sw,
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_states (State rsw mw) sw ->
+ match_outcome (parexec_wio_body ge bdy rsr rsw mr mw) (prun_iw Ge (trans_body bdy) sw sr).
+Proof.
+ induction bdy as [|i bdy]; simpl; eauto.
+ intros.
+ exploit (bisimu_par_wio_basic ge fn rsr rsw mr mw sr sw i); eauto.
+ destruct (bstep _ _ _ _ _ _); simpl.
+ - intros (s' & X1 & X2). rewrite X1; simpl; eauto.
+ - intros X; rewrite X; simpl; auto.
+Qed.
+
+Theorem bisimu_par_control ex sz aux ge fn rsr rsw mr mw sr sw:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_states (State rsw mw) sw ->
+ match_outcome (parexec_control ge fn ex (incrPC (Ptrofs.repr sz) rsr) (rsw#PC <- aux) mw) (inst_prun Ge (trans_pcincr sz (trans_exit ex)) sw sr sr).
+Proof.
+ intros GENV MSR MSW; unfold estep.
+ simpl in *. inv MSR. inv MSW.
+ destruct ex.
+ - destruct c; destruct i; try discriminate; simpl.
+ all: try (rewrite (H0 PC); eexists; split; try split; Simpl; intros rr; destruct rr; unfold incrPC; Simpl).
+
+ (* Pjumptable *)
+ + rewrite (H0 PC). Simpl. rewrite (H0 r). unfold incrPC. Simpl.
+ destruct (rsr r); simpl; auto. destruct (list_nth_z _ _); simpl; auto.
+ unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto. Simpl.
+ destruct (Val.offset_ptr _ _); simpl; auto.
+ eexists; split; try split; Simpl. intros rr; destruct rr; unfold incrPC; Simpl.
+ destruct (preg_eq g GPR62). rewrite e. Simpl.
+ destruct (preg_eq g GPR63). rewrite e. Simpl. Simpl.
+
+ (* Pj_l *)
+ + rewrite (H0 PC). Simpl. unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto.
+ unfold incrPC. Simpl. destruct (Val.offset_ptr _ _); simpl; auto.
+ eexists; split; try split; Simpl. intros rr; destruct rr; unfold incrPC; Simpl.
+
+ (* Pcb *)
+ + rewrite (H0 PC). Simpl. rewrite (H0 r). destruct (cmp_for_btest _); simpl; auto. destruct o; simpl; auto.
+ unfold par_eval_branch. unfold eval_branch_deps. unfold incrPC. Simpl. destruct i.
+ ++ destruct (Val.cmp_bool _ _ _); simpl; auto. destruct b.
+ +++ unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto. Simpl.
+ destruct (Val.offset_ptr _ _); simpl; auto. eexists; split; try split; Simpl.
+ intros rr; destruct rr; Simpl.
+ +++ repeat (econstructor; eauto). intros rr; destruct rr; Simpl.
+ ++ destruct (Val.cmpl_bool _ _ _); simpl; auto. destruct b.
+ +++ unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto. Simpl.
+ destruct (Val.offset_ptr _ _); simpl; auto. eexists; split; try split; Simpl.
+ intros rr; destruct rr; Simpl.
+ +++ repeat (econstructor; eauto). intros rr; destruct rr; Simpl.
+
+ (* Pcbu *)
+ + rewrite (H0 PC). Simpl. rewrite (H0 r). destruct (cmpu_for_btest _); simpl; auto. destruct o; simpl; auto.
+ unfold par_eval_branch. unfold eval_branch_deps. unfold incrPC. Simpl. destruct i.
+ ++ destruct (Val.mxcmpu_bool _ _ _); simpl; auto. destruct b.
+ +++ unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto. Simpl.
+ destruct (Val.offset_ptr _ _); simpl; auto. eexists; split; try split; Simpl.
+ intros rr; destruct rr; Simpl.
+ +++ repeat (econstructor; eauto). intros rr; destruct rr; Simpl.
+ ++ destruct (Val.mxcmplu_bool _ _ _); simpl; auto. destruct b.
+ +++ unfold par_goto_label. unfold goto_label_deps. destruct (label_pos _ _ _); simpl; auto. Simpl.
+ destruct (Val.offset_ptr _ _); simpl; auto. eexists; split; try split; Simpl.
+ intros rr; destruct rr; Simpl.
+ +++ repeat (econstructor; eauto). intros rr; destruct rr; Simpl.
+
+ - simpl in *. rewrite (H0 PC). eexists; split; try split; Simpl.
+ intros rr; destruct rr; unfold incrPC; Simpl.
+Qed.
+
+Theorem bisimu_par_exit ex sz ge fn rsr rsw mr mw sr sw:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_states (State rsw mw) sw ->
+ match_outcome (estep ge fn ex (Ptrofs.repr sz) rsr rsw mw) (inst_prun Ge (trans_pcincr sz (trans_exit ex)) sw sr sr).
+Proof.
+ intros; unfold estep.
+ exploit (bisimu_par_control ex sz rsw#PC ge fn rsr rsw mr mw sr sw); eauto.
+ replace (rsw # PC <- (rsw PC)) with rsw; auto.
+ apply extensionality. intros; destruct x; simpl; auto.
+Qed.
+
+Definition trans_block_aux bdy sz ex := (trans_body bdy) ++ (trans_pcincr sz (trans_exit ex) :: nil).
+
+Theorem bisimu_par_wio ge fn rsr mr sr bdy ex sz:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_outcome (parexec_wio ge fn bdy ex (Ptrofs.repr sz) rsr mr) (prun_iw Ge (trans_block_aux bdy sz ex) sr sr).
+Proof.
+ intros GENV MSR. unfold parexec_wio, trans_block_aux.
+ exploit (bisimu_par_body bdy ge fn rsr mr sr rsr mr sr); eauto.
+ destruct (parexec_wio_body _ _ _ _ _ _); simpl.
+ - intros (s' & X1 & X2).
+ erewrite prun_iw_app_Some; eauto.
+ exploit (bisimu_par_exit ex sz ge fn rsr rs mr m sr s'); eauto.
+ subst Ge; simpl. destruct MSR as (Y1 & Y2). erewrite Y2; simpl.
+ destruct (inst_prun _ _ _ _ _); simpl; auto.
+ - intros X; erewrite prun_iw_app_None; eauto.
+Qed.
+
+Theorem bisimu_par_wio_bblock ge fn rsr mr sr bdy1 bdy2 ex sz:
+ Ge = Genv ge fn ->
+ match_states (State rsr mr) sr ->
+ match_outcome
+ match parexec_wio ge fn bdy1 ex (Ptrofs.repr sz) rsr mr with
+ | Next rs' m' => parexec_wio_body ge bdy2 rsr rs' mr m'
+ | Stuck => Stuck
+ end
+ (prun_iw Ge ((trans_block_aux bdy1 sz ex)++(trans_body bdy2)) sr sr).
+Proof.
+ intros.
+ exploit (bisimu_par_wio ge fn rsr mr sr bdy1 ex sz); eauto.
+ destruct (parexec_wio _ _ _ _ _ _); simpl.
+ - intros (s' & X1 & X2).
+ erewrite prun_iw_app_Some; eauto.
+ eapply bisimu_par_body; eauto.
+ - intros; erewrite prun_iw_app_None; eauto.
+Qed.
+
+Lemma trans_body_perserves_permutation bdy1 bdy2:
+ Permutation bdy1 bdy2 ->
+ Permutation (trans_body bdy1) (trans_body bdy2).
+Proof.
+ induction 1; simpl; econstructor; eauto.
+Qed.
+
+Lemma trans_body_app bdy1: forall bdy2,
+ trans_body (bdy1++bdy2) = (trans_body bdy1) ++ (trans_body bdy2).
+Proof.
+ induction bdy1; simpl; congruence.
+Qed.
+
+Theorem trans_block_perserves_permutation bdy1 bdy2 b:
+ Permutation (bdy1 ++ bdy2) (body b) ->
+ Permutation (trans_block b) ((trans_block_aux bdy1 (size b) (exit b))++(trans_body bdy2)).
+Proof.
+ intro H; unfold trans_block, trans_block_aux.
+ eapply perm_trans.
+ - eapply Permutation_app_tail.
+ apply trans_body_perserves_permutation.
+ apply Permutation_sym; eapply H.
+ - rewrite trans_body_app. rewrite <-! app_assoc.
+ apply Permutation_app_head.
+ apply Permutation_app_comm.
+Qed.
+
+Theorem bisimu_par rs1 m1 s1' b ge fn o2:
+ Ge = Genv ge fn ->
+ match_states (State rs1 m1) s1' ->
+ parexec_bblock ge fn b rs1 m1 o2 ->
+ exists o2',
+ prun Ge (trans_block b) s1' o2'
+ /\ match_outcome o2 o2'.
+Proof.
+ intros GENV MS PAREXEC.
+ inversion PAREXEC as (bdy1 & bdy2 & PERM & WIO).
+ exploit trans_block_perserves_permutation; eauto.
+ intros Perm.
+ exploit (bisimu_par_wio_bblock ge fn rs1 m1 s1' bdy1 bdy2 (exit b) (size b)); eauto.
+ rewrite <- WIO. clear WIO.
+ intros H; eexists; split. 2: eapply H.
+ unfold prun; eexists; split; eauto.
+ destruct (prun_iw _ _ _ _); simpl; eauto.
+Qed.
+
+(** sequential execution *)
+Theorem bisimu_basic ge fn bi rs m s:
+ Ge = Genv ge fn ->
+ match_states (State rs m) s ->
+ match_outcome (exec_basic_instr ge bi rs m) (inst_run Ge (trans_basic bi) s s).
+Proof.
+ intros; unfold exec_basic_instr. rewrite inst_run_prun.
+ eapply bisimu_par_wio_basic; eauto.
+Qed.
+
+Lemma bisimu_body:
+ forall bdy ge fn rs m s,
+ Ge = Genv ge fn ->
+ match_states (State rs m) s ->
+ match_outcome (exec_body ge bdy rs m) (exec Ge (trans_body bdy) s).
+Proof.
+ induction bdy as [|i bdy]; simpl; eauto.
+ intros.
+ exploit (bisimu_basic ge fn i rs m s); eauto.
+ destruct (exec_basic_instr _ _ _ _); simpl.
+ - intros (s' & X1 & X2). rewrite X1; simpl; eauto.
+ - intros X; rewrite X; simpl; auto.
+Qed.
+
+Theorem bisimu_exit ge fn b rs m s:
+ Ge = Genv ge fn ->
+ match_states (State rs m) s ->
+ match_outcome (exec_control ge fn (exit b) (nextblock b rs) m) (inst_run Ge (trans_pcincr (size b) (trans_exit (exit b))) s s).
+Proof.
+ intros; unfold exec_control, nextblock. rewrite inst_run_prun.
+ apply (bisimu_par_control (exit b) (size b) (Val.offset_ptr (rs PC) (Ptrofs.repr (size b))) ge fn rs rs m m s s); auto.
+Qed.
+
+Theorem bisimu rs m b ge fn s:
+ Ge = Genv ge fn ->
+ match_states (State rs m) s ->
+ match_outcome (exec_bblock ge fn b rs m) (exec Ge (trans_block b) s).
+Proof.
+ intros GENV MS. unfold exec_bblock.
+ exploit (bisimu_body (body b) ge fn rs m s); eauto.
+ unfold exec, trans_block; simpl.
+ destruct (exec_body _ _ _ _); simpl.
+ - intros (s' & X1 & X2).
+ erewrite run_app_Some; eauto.
+ exploit (bisimu_exit ge fn b rs0 m0 s'); eauto.
+ subst Ge; simpl. destruct X2 as (Y1 & Y2). erewrite Y2; simpl.
+ destruct (inst_run _ _ _); simpl; auto.
+ - intros X; erewrite run_app_None; eauto.
+Qed.
+
+
+Theorem trans_state_match: forall S, match_states S (trans_state S).
+Proof.
+ intros. destruct S as (rs & m). simpl.
+ split. reflexivity.
+ intro. destruct r; try reflexivity.
+ destruct g; reflexivity.
+Qed.
+
+
+Lemma state_eq_decomp:
+ forall rs1 m1 rs2 m2, rs1 = rs2 -> m1 = m2 -> State rs1 m1 = State rs2 m2.
+Proof.
+ intros. congruence.
+Qed.
+
+Theorem state_equiv S1 S2 S': match_states S1 S' -> match_states S2 S' -> S1 = S2.
+Proof.
+ unfold match_states; intros H0 H1. destruct S1 as (rs1 & m1). destruct S2 as (rs2 & m2). inv H0. inv H1.
+ apply state_eq_decomp.
+ - apply functional_extensionality. intros. assert (Val (rs1 x) = Val (rs2 x)) by congruence. congruence.
+ - congruence.
+Qed.
+
+Lemma bblock_para_check_correct ge fn bb rs m rs' m':
+ Ge = Genv ge fn ->
+ exec_bblock ge fn bb rs m = Next rs' m' ->
+ bblock_para_check bb = true ->
+ det_parexec ge fn bb rs m rs' m'.
+Proof.
+ intros H H0 H1 o H2. unfold bblock_para_check in H1.
+ exploit (bisimu rs m bb ge fn); eauto. eapply trans_state_match.
+ rewrite H0; simpl.
+ intros (s2' & EXEC & MS).
+ exploit bisimu_par. 2: apply (trans_state_match (State rs m)). all: eauto.
+ intros (o2' & PRUN & MO).
+ exploit parallelizable_correct. apply is_para_correct_aux. eassumption.
+ intro. eapply H3 in PRUN. clear H3. destruct o2'.
+ - inv PRUN. inv H3. unfold exec in EXEC; unfold trans_state in H.
+ assert (x = s2') by congruence. subst. clear H.
+ assert (m0 = s2') by (apply functional_extensionality; auto). subst. clear H4.
+ destruct o; try discriminate. inv MO. inv H. assert (s2' = x) by congruence. subst.
+ exploit (state_equiv (State rs' m') (State rs0 m0)).
+ 2: eapply H4. eapply MS. intro H. inv H. reflexivity.
+ - unfold match_outcome in MO. destruct o.
+ + inv MO. inv H3. discriminate.
+ + clear MO. unfold exec in EXEC.
+ unfold trans_state in PRUN; rewrite EXEC in PRUN. discriminate.
+Qed.
+
+End SECT_PAR.
+
+Section SECT_BBLOCK_EQUIV.
+
+Variable Ge: genv.
+
+Local Hint Resolve trans_state_match: core.
+
+Lemma bblock_simu_reduce:
+ forall p1 p2 ge fn,
+ Ge = Genv ge fn ->
+ L.bblock_simu Ge (trans_block p1) (trans_block p2) ->
+ Asmblockprops.bblock_simu ge fn p1 p2.
+Proof.
+ unfold bblock_simu, res_eq; intros p1 p2 ge fn H1 H2 rs m DONTSTUCK.
+ generalize (H2 (trans_state (State rs m))); clear H2.
+ intro H2.
+ exploit (bisimu Ge rs m p1 ge fn (trans_state (State rs m))); eauto.
+ exploit (bisimu Ge rs m p2 ge fn (trans_state (State rs m))); eauto.
+ destruct (exec_bblock ge fn p1 rs m); try congruence.
+ intros H3 (s2' & exp2 & MS'). unfold exec in exp2, H3. rewrite exp2 in H2.
+ destruct H2 as (m2' & H2 & H4). discriminate. rewrite H2 in H3.
+ destruct (exec_bblock ge fn p2 rs m); simpl in H3.
+ * destruct H3 as (s' & H3 & H5 & H6). inv H3. inv MS'.
+ replace rs0 with rs1.
+ - replace m0 with m1; auto. congruence.
+ - apply functional_extensionality. intros r.
+ generalize (H0 r). intros Hr. congruence.
+ * discriminate.
+Qed.
+
+(** Used for debug traces *)
+
+Definition gpreg_name (gpr: gpreg) :=
+ match gpr with
+ | GPR0 => Str ("GPR0") | GPR1 => Str ("GPR1") | GPR2 => Str ("GPR2") | GPR3 => Str ("GPR3") | GPR4 => Str ("GPR4")
+ | GPR5 => Str ("GPR5") | GPR6 => Str ("GPR6") | GPR7 => Str ("GPR7") | GPR8 => Str ("GPR8") | GPR9 => Str ("GPR9")
+ | GPR10 => Str ("GPR10") | GPR11 => Str ("GPR11") | GPR12 => Str ("GPR12") | GPR13 => Str ("GPR13") | GPR14 => Str ("GPR14")
+ | GPR15 => Str ("GPR15") | GPR16 => Str ("GPR16") | GPR17 => Str ("GPR17") | GPR18 => Str ("GPR18") | GPR19 => Str ("GPR19")
+ | GPR20 => Str ("GPR20") | GPR21 => Str ("GPR21") | GPR22 => Str ("GPR22") | GPR23 => Str ("GPR23") | GPR24 => Str ("GPR24")
+ | GPR25 => Str ("GPR25") | GPR26 => Str ("GPR26") | GPR27 => Str ("GPR27") | GPR28 => Str ("GPR28") | GPR29 => Str ("GPR29")
+ | GPR30 => Str ("GPR30") | GPR31 => Str ("GPR31") | GPR32 => Str ("GPR32") | GPR33 => Str ("GPR33") | GPR34 => Str ("GPR34")
+ | GPR35 => Str ("GPR35") | GPR36 => Str ("GPR36") | GPR37 => Str ("GPR37") | GPR38 => Str ("GPR38") | GPR39 => Str ("GPR39")
+ | GPR40 => Str ("GPR40") | GPR41 => Str ("GPR41") | GPR42 => Str ("GPR42") | GPR43 => Str ("GPR43") | GPR44 => Str ("GPR44")
+ | GPR45 => Str ("GPR45") | GPR46 => Str ("GPR46") | GPR47 => Str ("GPR47") | GPR48 => Str ("GPR48") | GPR49 => Str ("GPR49")
+ | GPR50 => Str ("GPR50") | GPR51 => Str ("GPR51") | GPR52 => Str ("GPR52") | GPR53 => Str ("GPR53") | GPR54 => Str ("GPR54")
+ | GPR55 => Str ("GPR55") | GPR56 => Str ("GPR56") | GPR57 => Str ("GPR57") | GPR58 => Str ("GPR58") | GPR59 => Str ("GPR59")
+ | GPR60 => Str ("GPR60") | GPR61 => Str ("GPR61") | GPR62 => Str ("GPR62") | GPR63 => Str ("GPR63")
+ end.
+
+Definition string_of_name (x: P.R.t): ?? pstring :=
+ if (Pos.eqb x pmem) then
+ RET (Str "MEM")
+ else
+ match inv_ppos x with
+ | Some RA => RET (Str ("RA"))
+ | Some PC => RET (Str ("PC"))
+ | Some (IR gpr) => RET (gpreg_name gpr)
+ | _ => RET (Str ("UNDEFINED"))
+ end.
+
+Definition string_of_name_r (n: arith_name_r): pstring :=
+ match n with
+ | Ploadsymbol _ _ => "Ploadsymbol"
+ end.
+
+Definition string_of_name_rr (n: arith_name_rr): pstring :=
+ match n with
+ Pmv => "Pmv"
+ | Pnegw => "Pnegw"
+ | Pnegl => "Pnegl"
+ | Pcvtl2w => "Pcvtl2w"
+ | Psxwd => "Psxwd"
+ | Pzxwd => "Pzxwd"
+ | Pextfz _ _ => "Pextfz"
+ | Pextfs _ _ => "Pextfs"
+ | Pextfzl _ _ => "Pextfzl"
+ | Pextfsl _ _ => "Pextfsl"
+ | Pfabsd => "Pfabsd"
+ | Pfabsw => "Pfabsw"
+ | Pfnegd => "Pfnegd"
+ | Pfnegw => "Pfnegw"
+ | Pfinvw => "Pfinvw"
+ | Pfnarrowdw => "Pfnarrowdw"
+ | Pfwidenlwd => "Pfwidenlwd"
+ | Pfloatwrnsz => "Pfloatwrnsz"
+ | Pfloatuwrnsz => "Pfloatuwrnsz"
+ | Pfloatudrnsz => "Pfloatudrnsz"
+ | Pfloatdrnsz => "Pfloatdrnsz"
+ | Pfixedwrzz => "Pfixedwrzz"
+ | Pfixeduwrzz => "Pfixeduwrzz"
+ | Pfixeddrzz => "Pfixeddrzz"
+ | Pfixedudrzz => "Pfixedudrzz"
+ | Pfixeddrzz_i32 => "Pfixeddrzz_i32"
+ | Pfixedudrzz_i32 => "Pfixedudrzz_i32"
+ end.
+
+Definition string_of_name_ri32 (n: arith_name_ri32): pstring :=
+ match n with
+ | Pmake => "Pmake"
+ end.
+
+Definition string_of_name_ri64 (n: arith_name_ri64): pstring :=
+ match n with
+ | Pmakel => "Pmakel"
+ end.
+
+Definition string_of_name_rf32 (n: arith_name_rf32): pstring :=
+ match n with
+ | Pmakefs => "Pmakefs"
+ end.
+
+Definition string_of_name_rf64 (n: arith_name_rf64): pstring :=
+ match n with
+ | Pmakef => "Pmakef"
+ end.
+
+Definition string_of_name_rrr (n: arith_name_rrr): pstring :=
+ match n with
+ | Pcompw _ => "Pcompw"
+ | Pcompl _ => "Pcompl"
+ | Pfcompw _ => "Pfcompw"
+ | Pfcompl _ => "Pfcompl"
+ | Paddw => "Paddw"
+ | Paddxw _ => "Paddxw"
+ | Psubw => "Psubw"
+ | Prevsubxw _ => "Prevsubxw"
+ | Pmulw => "Pmulw"
+ | Pandw => "Pandw"
+ | Pnandw => "Pnandw"
+ | Porw => "Porw"
+ | Pnorw => "Pnorw"
+ | Pxorw => "Pxorw"
+ | Pnxorw => "Pnxorw"
+ | Pandnw => "Pandnw"
+ | Pornw => "Pornw"
+ | Psraw => "Psraw"
+ | Psrlw => "Psrlw"
+ | Psrxw => "Psrxw"
+ | Psllw => "Psllw"
+ | Paddl => "Paddl"
+ | Paddxl _ => "Paddxl"
+ | Psubl => "Psubl"
+ | Prevsubxl _ => "Prevsubxl"
+ | Pandl => "Pandl"
+ | Pnandl => "Pnandl"
+ | Porl => "Porl"
+ | Pnorl => "Pnorl"
+ | Pxorl => "Pxorl"
+ | Pnxorl => "Pnxorl"
+ | Pandnl => "Pandnl"
+ | Pornl => "Pornl"
+ | Pmull => "Pmull"
+ | Pslll => "Pslll"
+ | Psrll => "Psrll"
+ | Psrxl => "Psrxl"
+ | Psral => "Psral"
+ | Pfaddd => "Pfaddd"
+ | Pfaddw => "Pfaddw"
+ | Pfsbfd => "Pfsbfd"
+ | Pfsbfw => "Pfsbfw"
+ | Pfmuld => "Pfmuld"
+ | Pfmulw => "Pfmulw"
+ | Pfmind => "Pfmind"
+ | Pfminw => "Pfminw"
+ | Pfmaxd => "Pfmaxd"
+ | Pfmaxw => "Pfmaxw"
+ end.
+
+Definition string_of_name_rri32 (n: arith_name_rri32): pstring :=
+ match n with
+ Pcompiw _ => "Pcompiw"
+ | Paddiw => "Paddiw"
+ | Paddxiw _ => "Paddxiw"
+ | Prevsubiw => "Prevsubiw"
+ | Prevsubxiw _ => "Prevsubxiw"
+ | Pmuliw => "Pmuliw"
+ | Pandiw => "Pandiw"
+ | Pnandiw => "Pnandiw"
+ | Poriw => "Poriw"
+ | Pnoriw => "Pnoriw"
+ | Pxoriw => "Pxoriw"
+ | Pnxoriw => "Pnxoriw"
+ | Pandniw => "Pandniw"
+ | Porniw => "Porniw"
+ | Psraiw => "Psraiw"
+ | Psrliw => "Psrliw"
+ | Psrxiw => "Psrxiw"
+ | Pslliw => "Pslliw"
+ | Proriw => "Proriw"
+ | Psllil => "Psllil"
+ | Psrlil => "Psrlil"
+ | Psrail => "Psrail"
+ | Psrxil => "Psrxil"
+ end.
+
+Definition string_of_name_rri64 (n: arith_name_rri64): pstring :=
+ match n with
+ Pcompil _ => "Pcompil"
+ | Paddil => "Paddil"
+ | Prevsubil => "Prevsubil"
+ | Paddxil _ => "Paddxil"
+ | Prevsubxil _ => "Prevsubxil"
+ | Pmulil => "Pmulil"
+ | Pandil => "Pandil"
+ | Pnandil => "Pnandil"
+ | Poril => "Poril"
+ | Pnoril => "Pnoril"
+ | Pxoril => "Pxoril"
+ | Pnxoril => "Pnxoril"
+ | Pandnil => "Pandnil"
+ | Pornil => "Pornil"
+ end.
+
+Definition string_of_name_arrr (n: arith_name_arrr): pstring :=
+ match n with
+ | Pmaddw => "Pmaddw"
+ | Pmaddl => "Pmaddl"
+ | Pmsubw => "Pmsubw"
+ | Pmsubl => "Pmsubl"
+ | Pcmove _ => "Pcmove"
+ | Pcmoveu _ => "Pcmoveu"
+ | Pfmaddfw => "Pfmaddfw"
+ | Pfmaddfl => "Pfmaddfl"
+ | Pfmsubfw => "Pfmsubfw"
+ | Pfmsubfl => "Pfmsubfl"
+ end.
+
+Definition string_of_name_arr (n: arith_name_arr): pstring :=
+ match n with
+ | Pinsf _ _ => "Pinsf"
+ | Pinsfl _ _ => "Pinsfl"
+ end.
+
+Definition string_of_name_arri32 (n: arith_name_arri32): pstring :=
+ match n with
+ | Pmaddiw => "Pmaddw"
+ | Pcmoveiw _ => "Pcmoveiw"
+ | Pcmoveuiw _ => "Pcmoveuiw"
+ end.
+
+Definition string_of_name_arri64 (n: arith_name_arri64): pstring :=
+ match n with
+ | Pmaddil => "Pmaddl"
+ | Pcmoveil _ => "Pcmoveil"
+ | Pcmoveuil _ => "Pcmoveuil"
+ end.
+
+Definition string_of_arith (op: arith_op): pstring :=
+ match op with
+ | OArithR n => string_of_name_r n
+ | OArithRR n => string_of_name_rr n
+ | OArithRI32 n _ => string_of_name_ri32 n
+ | OArithRI64 n _ => string_of_name_ri64 n
+ | OArithRF32 n _ => string_of_name_rf32 n
+ | OArithRF64 n _ => string_of_name_rf64 n
+ | OArithRRR n => string_of_name_rrr n
+ | OArithRRI32 n _ => string_of_name_rri32 n
+ | OArithRRI64 n _ => string_of_name_rri64 n
+ | OArithARRR n => string_of_name_arrr n
+ | OArithARR n => string_of_name_arr n
+ | OArithARRI32 n _ => string_of_name_arri32 n
+ | OArithARRI64 n _ => string_of_name_arri64 n
+ end.
+
+Definition string_of_load_name (n: load_name) : pstring :=
+ match n with
+ Plb => "Plb"
+ | Plbu => "Plbu"
+ | Plh => "Plh"
+ | Plhu => "Plhu"
+ | Plw => "Plw"
+ | Plw_a => "Plw_a"
+ | Pld => "Pld"
+ | Pld_a => "Pld_a"
+ | Pfls => "Pfls"
+ | Pfld => "Pfld"
+ end.
+
+Definition string_of_load (op: load_op): pstring :=
+ match op with
+ | OLoadRRO n _ _ => string_of_load_name n
+ | OLoadRRR n _ => string_of_load_name n
+ | OLoadRRRXS n _ => string_of_load_name n
+ end.
+
+Definition string_of_store_name (n: store_name) : pstring :=
+ match n with
+ Psb => "Psb"
+ | Psh => "Psh"
+ | Psw => "Psw"
+ | Psw_a => "Psw_a"
+ | Psd => "Psd"
+ | Psd_a => "Psd_a"
+ | Pfss => "Pfss"
+ | Pfsd => "Pfsd"
+ end.
+
+Definition string_of_store (op: store_op) : pstring :=
+ match op with
+ | OStoreRRO n _ => string_of_store_name n
+ | OStoreRRR n => string_of_store_name n
+ | OStoreRRRXS n => string_of_store_name n
+ end.
+
+Definition string_of_control (op: control_op) : pstring :=
+ match op with
+ | Oj_l _ => "Oj_l"
+ | Ocb _ _ => "Ocb"
+ | Ocbu _ _ => "Ocbu"
+ | Odiv => "Odiv"
+ | Odivu => "Odivu"
+ | Ojumptable _ => "Ojumptable"
+ | OError => "OError"
+ | OIncremPC _ => "OIncremPC"
+ end.
+
+Definition string_of_op (op: P.op): ?? pstring :=
+ match op with
+ | Arith op => RET (string_of_arith op)
+ | Load op => RET (string_of_load op)
+ | Store op => RET (string_of_store op)
+ | Control op => RET (string_of_control op)
+ | Allocframe _ _ => RET (Str "Allocframe")
+ | Allocframe2 _ _ => RET (Str "Allocframe2")
+ | Freeframe _ _ => RET (Str "Freeframe")
+ | Freeframe2 _ _ => RET (Str "Freeframe2")
+ | Constant _ => RET (Str "Constant")
+ | Fail => RET (Str "Fail")
+ end.
+
+End SECT_BBLOCK_EQUIV.
+
+(** REWRITE RULES *)
+
+Definition is_constant (o: op): bool :=
+ match o with
+ | Constant _ | OArithR _ | OArithRI32 _ _ | OArithRI64 _ _ | OArithRF32 _ _ | OArithRF64 _ _ => true
+ | _ => false
+ end.
+
+Lemma is_constant_correct ge o: is_constant o = true -> op_eval ge o [] <> None.
+Proof.
+ destruct o; simpl in * |- *; try congruence.
+ destruct ao; simpl in * |- *; try congruence;
+ destruct n; simpl in * |- *; try congruence;
+ unfold arith_eval; destruct ge; simpl in * |- *; try congruence.
+Qed.
+
+Definition main_reduce (t: Terms.term):= RET (Terms.nofail is_constant t).
+
+Local Hint Resolve is_constant_correct: wlp.
+
+Lemma main_reduce_correct t:
+ WHEN main_reduce t ~> pt THEN Terms.match_pt t pt.
+Proof.
+ wlp_simplify.
+Qed.
+
+Definition reduce := {| Terms.result := main_reduce; Terms.result_correct := main_reduce_correct |}.
+
+Definition bblock_simu_test (verb: bool) (p1 p2: Asmvliw.bblock) : ?? bool :=
+ if verb then
+ IST.verb_bblock_simu_test reduce string_of_name string_of_op (trans_block p1) (trans_block p2)
+ else
+ IST.bblock_simu_test reduce (trans_block p1) (trans_block p2).
+
+Local Hint Resolve IST.bblock_simu_test_correct bblock_simu_reduce IST.verb_bblock_simu_test_correct: wlp.
+
+(** Main simulation (Impure) theorem *)
+Theorem bblock_simu_test_correct verb p1 p2 :
+ WHEN bblock_simu_test verb p1 p2 ~> b THEN b=true -> forall ge fn, Asmblockprops.bblock_simu ge fn p1 p2.
+Proof.
+ wlp_simplify.
+Qed.
+Hint Resolve bblock_simu_test_correct: wlp.
+
+(** ** Coerce bblock_simu_test into a pure function (this is a little unsafe like all oracles in CompCert). *)
+
+Import UnsafeImpure.
+
+Definition pure_bblock_simu_test (verb: bool) (p1 p2: Asmvliw.bblock): bool :=
+ match unsafe_coerce (bblock_simu_test verb p1 p2) with
+ | Some b => b
+ | None => false
+ end.
+
+Theorem pure_bblock_simu_test_correct verb p1 p2 ge fn: pure_bblock_simu_test verb p1 p2 = true -> Asmblockprops.bblock_simu ge fn p1 p2.
+Proof.
+ unfold pure_bblock_simu_test.
+ destruct (unsafe_coerce (bblock_simu_test verb p1 p2)) eqn: UNSAFE; try discriminate.
+ intros; subst. eapply bblock_simu_test_correct; eauto.
+ apply unsafe_coerce_not_really_correct; eauto.
+Qed.
+
+Definition bblock_simub: Asmvliw.bblock -> Asmvliw.bblock -> bool := pure_bblock_simu_test true.
+
+Lemma bblock_simub_correct p1 p2 ge fn: bblock_simub p1 p2 = true -> Asmblockprops.bblock_simu ge fn p1 p2.
+Proof.
+ eapply (pure_bblock_simu_test_correct true).
+Qed.
diff --git a/kvx/Asmblockgen.v b/kvx/Asmblockgen.v
new file mode 100644
index 00000000..ab827b1c
--- /dev/null
+++ b/kvx/Asmblockgen.v
@@ -0,0 +1,1211 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Translation from Machblock to KVX assembly language (Asmblock)
+ Inspired from the Mach->Asm pass of other backends, but adapted to the block structure *)
+
+Require Archi.
+Require Import Coqlib Errors.
+Require Import AST Integers Floats Memdata.
+Require Import Op Locations Machblock Asmvliw Asmblock.
+Require ExtValues.
+Require Import Chunks.
+
+Local Open Scope string_scope.
+Local Open Scope error_monad_scope.
+
+Import PArithCoercions.
+
+(** The code generation functions take advantage of several
+ characteristics of the [Mach] code generated by earlier passes of the
+ compiler, mostly that argument and result registers are of the correct
+ types. These properties are true by construction, but it's easier to
+ recheck them during code generation and fail if they do not hold. *)
+
+(** Extracting integer or float registers. *)
+
+Inductive immed32 : Type :=
+ | Imm32_single (imm: int).
+
+Definition make_immed32 (val: int) := Imm32_single val.
+
+Inductive immed64 : Type :=
+ | Imm64_single (imm: int64)
+.
+
+Definition make_immed64 (val: int64) := Imm64_single val.
+
+Notation "a ::g b" := (cons (A:=instruction) a b) (at level 49, right associativity).
+Notation "a ::i b" := (cons (A:=basic) a b) (at level 49, right associativity).
+Notation "a ::b lb" := ((bblock_single_inst a) :: lb) (at level 49, right associativity).
+Notation "a ++g b" := (app (A:=instruction) a b) (at level 49, right associativity).
+Notation "a @@ b" := (app a b) (at level 49, right associativity).
+
+Definition loadimm32 (r: ireg) (n: int) :=
+ match make_immed32 n with
+ | Imm32_single imm => Pmake r imm
+ end.
+
+Definition opimm32 (op: arith_name_rrr)
+ (opimm: arith_name_rri32)
+ (rd rs: ireg) (n: int) :=
+ match make_immed32 n with
+ | Imm32_single imm => opimm rd rs imm
+ end.
+
+Definition addimm32 := opimm32 Paddw Paddiw.
+Definition mulimm32 := opimm32 Pmulw Pmuliw.
+Definition andimm32 := opimm32 Pandw Pandiw.
+Definition nandimm32 := opimm32 Pnandw Pnandiw.
+Definition orimm32 := opimm32 Porw Poriw.
+Definition norimm32 := opimm32 Pnorw Pnoriw.
+Definition xorimm32 := opimm32 Pxorw Pxoriw.
+Definition nxorimm32 := opimm32 Pnxorw Pnxoriw.
+
+Definition loadimm64 (r: ireg) (n: int64) :=
+ match make_immed64 n with
+ | Imm64_single imm => Pmakel r imm
+ end.
+
+Definition opimm64 (op: arith_name_rrr)
+ (opimm: arith_name_rri64)
+ (rd rs: ireg) (n: int64) :=
+ match make_immed64 n with
+ | Imm64_single imm => opimm rd rs imm
+end.
+
+Definition addimm64 := opimm64 Paddl Paddil.
+Definition mulimm64 := opimm64 Pmull Pmulil.
+Definition orimm64 := opimm64 Porl Poril.
+Definition andimm64 := opimm64 Pandl Pandil.
+Definition xorimm64 := opimm64 Pxorl Pxoril.
+Definition norimm64 := opimm64 Pnorl Pnoril.
+Definition nandimm64 := opimm64 Pnandl Pnandil.
+Definition nxorimm64 := opimm64 Pnxorl Pnxoril.
+
+Definition addptrofs (rd rs: ireg) (n: ptrofs) :=
+ if Ptrofs.eq_dec n Ptrofs.zero then
+ Pmv rd rs
+ else
+ addimm64 rd rs (Ptrofs.to_int64 n).
+
+(** Translation of conditional branches. *)
+
+Definition transl_comp
+ (c: comparison) (s: signedness) (r1 r2: ireg) (lbl: label) (k: code) : list instruction :=
+ Pcompw (itest_for_cmp c s) RTMP r1 r2 ::g Pcb BTwnez RTMP lbl ::g k.
+
+Definition transl_compi
+ (c: comparison) (s: signedness) (r: ireg) (imm: int) (lbl: label) (k: code) : list instruction :=
+ Pcompiw (itest_for_cmp c s) RTMP r imm ::g Pcb BTwnez RTMP lbl ::g k.
+
+Definition transl_compl
+ (c: comparison) (s: signedness) (r1 r2: ireg) (lbl: label) (k: code) : list instruction :=
+ Pcompl (itest_for_cmp c s) RTMP r1 r2 ::g Pcb BTwnez RTMP lbl ::g k.
+
+Definition transl_compil
+ (c: comparison) (s: signedness) (r: ireg) (imm: int64) (lbl: label) (k: code) : list instruction :=
+ Pcompil (itest_for_cmp c s) RTMP r imm ::g Pcb BTwnez RTMP lbl ::g k.
+
+Definition select_comp (n: int) (c: comparison) : option comparison :=
+ if Int.eq n Int.zero then
+ match c with
+ | Ceq => Some Ceq
+ | Cne => Some Cne
+ | _ => None
+ end
+ else
+ None
+ .
+
+Definition transl_opt_compuimm
+ (n: int) (c: comparison) (r1: ireg) (lbl: label) (k: code) : list instruction :=
+ if Int.eq n Int.zero then
+ match c with
+ | Ceq => Pcbu BTweqz r1 lbl ::g k
+ | Cne => Pcbu BTwnez r1 lbl ::g k
+ | _ => transl_compi c Unsigned r1 n lbl k
+ end
+ else
+ transl_compi c Unsigned r1 n lbl k
+ .
+
+Definition select_compl (n: int64) (c: comparison) : option comparison :=
+ if Int64.eq n Int64.zero then
+ match c with
+ | Ceq => Some Ceq
+ | Cne => Some Cne
+ | _ => None
+ end
+ else
+ None
+ .
+
+Definition transl_opt_compluimm
+ (n: int64) (c: comparison) (r1: ireg) (lbl: label) (k: code) : list instruction :=
+ if Int64.eq n Int64.zero then
+ match c with
+ | Ceq => Pcbu BTdeqz r1 lbl ::g k
+ | Cne => Pcbu BTdnez r1 lbl ::g k
+ | _ => transl_compil c Unsigned r1 n lbl k
+ end
+ else
+ transl_compil c Unsigned r1 n lbl k
+ .
+
+Definition transl_comp_float32 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
+ match ftest_for_cmp cmp with
+ | Normal ft => Pfcompw ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
+ | Reversed ft => Pfcompw ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
+ end.
+
+Definition transl_comp_notfloat32 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
+ match notftest_for_cmp cmp with
+ | Normal ft => Pfcompw ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
+ | Reversed ft => Pfcompw ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
+ end.
+
+Definition transl_comp_float64 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
+ match ftest_for_cmp cmp with
+ | Normal ft => Pfcompl ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
+ | Reversed ft => Pfcompl ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
+ end.
+
+Definition transl_comp_notfloat64 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
+ match notftest_for_cmp cmp with
+ | Normal ft => Pfcompl ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
+ | Reversed ft => Pfcompl ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
+ end.
+
+Definition transl_cbranch
+ (cond: condition) (args: list mreg) (lbl: label) (k: code) : res (list instruction ) :=
+ match cond, args with
+ | Ccompuimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_opt_compuimm n c r1 lbl k)
+ | Ccomp c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp c Signed r1 r2 lbl k)
+ | Ccompu c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp c Unsigned r1 r2 lbl k)
+ | Ccompimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if Int.eq n Int.zero then
+ Pcb (btest_for_cmpswz c) r1 lbl ::g k
+ else
+ transl_compi c Signed r1 n lbl k
+ )
+ | Ccompluimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_opt_compluimm n c r1 lbl k)
+ | Ccompl c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_compl c Signed r1 r2 lbl k)
+ | Ccomplu c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_compl c Unsigned r1 r2 lbl k)
+ | Ccomplimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (if Int64.eq n Int64.zero then
+ Pcb (btest_for_cmpsdz c) r1 lbl ::g k
+ else
+ transl_compil c Signed r1 n lbl k
+ )
+ | Ccompf c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp_float64 c r1 r2 lbl k)
+ | Cnotcompf c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp_notfloat64 c r1 r2 lbl k)
+ | Ccompfs c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp_float32 c r1 r2 lbl k)
+ | Cnotcompfs c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_comp_notfloat32 c r1 r2 lbl k)
+ | _, _ =>
+ Error(msg "Asmgenblock.transl_cbranch")
+ end.
+
+(** Translation of a condition operator. The generated code sets the
+ [rd] target register to 0 or 1 depending on the truth value of the
+ condition. *)
+
+Definition transl_cond_int32s (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ Pcompw (itest_for_cmp cmp Signed) rd r1 r2 ::i k.
+
+Definition transl_cond_int32u (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ Pcompw (itest_for_cmp cmp Unsigned) rd r1 r2 ::i k.
+
+Definition transl_cond_int64s (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ Pcompl (itest_for_cmp cmp Signed) rd r1 r2 ::i k.
+
+Definition transl_cond_int64u (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ Pcompl (itest_for_cmp cmp Unsigned) rd r1 r2 ::i k.
+
+Definition transl_condimm_int32s (cmp: comparison) (rd r1: ireg) (n: int) (k: bcode) :=
+ Pcompiw (itest_for_cmp cmp Signed) rd r1 n ::i k.
+
+Definition transl_condimm_int32u (cmp: comparison) (rd r1: ireg) (n: int) (k: bcode) :=
+ Pcompiw (itest_for_cmp cmp Unsigned) rd r1 n ::i k.
+
+Definition transl_condimm_int64s (cmp: comparison) (rd r1: ireg) (n: int64) (k: bcode) :=
+ Pcompil (itest_for_cmp cmp Signed) rd r1 n ::i k.
+
+Definition transl_condimm_int64u (cmp: comparison) (rd r1: ireg) (n: int64) (k: bcode) :=
+ Pcompil (itest_for_cmp cmp Unsigned) rd r1 n ::i k.
+
+
+Definition transl_cond_float32 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ match ftest_for_cmp cmp with
+ | Normal ft => Pfcompw ft rd r1 r2 ::i k
+ | Reversed ft => Pfcompw ft rd r2 r1 ::i k
+ end.
+
+Definition transl_cond_notfloat32 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ match notftest_for_cmp cmp with
+ | Normal ft => Pfcompw ft rd r1 r2 ::i k
+ | Reversed ft => Pfcompw ft rd r2 r1 ::i k
+ end.
+
+Definition transl_cond_float64 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ match ftest_for_cmp cmp with
+ | Normal ft => Pfcompl ft rd r1 r2 ::i k
+ | Reversed ft => Pfcompl ft rd r2 r1 ::i k
+ end.
+
+Definition transl_cond_notfloat64 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
+ match notftest_for_cmp cmp with
+ | Normal ft => Pfcompl ft rd r1 r2 ::i k
+ | Reversed ft => Pfcompl ft rd r2 r1 ::i k
+ end.
+
+
+(* CoMPare Unsigned Words to Zero *)
+Definition btest_for_cmpuwz (c: comparison) :=
+ match c with
+ | Cne => OK BTwnez
+ | Ceq => OK BTweqz
+ | Clt => Error (msg "btest_for_compuwz: Clt") (* TODO reachable *)
+ | Cge => Error (msg "btest_for_compuwz: Cge")
+ | Cle => OK BTweqz
+ | Cgt => OK BTwnez
+ end.
+
+(* CoMPare Unsigned Words to Zero *)
+Definition btest_for_cmpudz (c: comparison) :=
+ match c with
+ | Cne => OK BTdnez
+ | Ceq => OK BTdeqz
+ | Clt => Error (msg "btest_for_compudz: Clt")
+ | Cge => Error (msg "btest_for_compudz: Cge")
+ | Cle => OK BTdeqz
+ | Cgt => OK BTdnez
+ end.
+
+Definition conditional_move (cond0 : condition0) (rc rd rs : ireg) :
+ res basic :=
+ if ireg_eq rd rs
+ then OK Pnop
+ else
+ (match cond0 with
+ | Ccomp0 cmp =>
+ OK (PArith (Pcmove (btest_for_cmpswz cmp) rd rc rs))
+ | Ccompu0 cmp =>
+ do bt <- btest_for_cmpuwz cmp;
+ OK (PArith (Pcmoveu bt rd rc rs))
+ | Ccompl0 cmp =>
+ OK (PArith (Pcmove (btest_for_cmpsdz cmp) rd rc rs))
+ | Ccomplu0 cmp =>
+ do bt <- btest_for_cmpudz cmp;
+ OK (PArith (Pcmoveu bt rd rc rs))
+ end).
+
+Definition conditional_move_imm32 (cond0 : condition0) (rc rd : ireg) (imm : int) : res basic :=
+ match cond0 with
+ | Ccomp0 cmp =>
+ OK (PArith (Pcmoveiw (btest_for_cmpswz cmp) rd rc imm))
+ | Ccompu0 cmp =>
+ do bt <- btest_for_cmpuwz cmp;
+ OK (PArith (Pcmoveuiw bt rd rc imm))
+ | Ccompl0 cmp =>
+ OK (PArith (Pcmoveiw (btest_for_cmpsdz cmp) rd rc imm))
+ | Ccomplu0 cmp =>
+ do bt <- btest_for_cmpudz cmp;
+ OK (PArith (Pcmoveuiw bt rd rc imm))
+ end.
+
+Definition conditional_move_imm64 (cond0 : condition0) (rc rd : ireg) (imm : int64) : res basic :=
+ match cond0 with
+ | Ccomp0 cmp =>
+ OK (PArith (Pcmoveil (btest_for_cmpswz cmp) rd rc imm))
+ | Ccompu0 cmp =>
+ do bt <- btest_for_cmpuwz cmp;
+ OK (PArith (Pcmoveuil bt rd rc imm))
+ | Ccompl0 cmp =>
+ OK (PArith (Pcmoveil (btest_for_cmpsdz cmp) rd rc imm))
+ | Ccomplu0 cmp =>
+ do bt <- btest_for_cmpudz cmp;
+ OK (PArith (Pcmoveuil bt rd rc imm))
+ end.
+
+Definition transl_cond_op
+ (cond: condition) (rd: ireg) (args: list mreg) (k: bcode) :=
+ match cond, args with
+ | Ccomp c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_int32s c rd r1 r2 k)
+ | Ccompu c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_int32u c rd r1 r2 k)
+ | Ccompimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_condimm_int32s c rd r1 n k)
+ | Ccompuimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_condimm_int32u c rd r1 n k)
+ | Ccompl c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_int64s c rd r1 r2 k)
+ | Ccomplu c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_int64u c rd r1 r2 k)
+ | Ccomplimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_condimm_int64s c rd r1 n k)
+ | Ccompluimm c n, a1 :: nil =>
+ do r1 <- ireg_of a1;
+ OK (transl_condimm_int64u c rd r1 n k)
+ | Ccompfs c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_float32 c rd r1 r2 k)
+ | Cnotcompfs c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_notfloat32 c rd r1 r2 k)
+ | Ccompf c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_float64 c rd r1 r2 k)
+ | Cnotcompf c, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ OK (transl_cond_notfloat64 c rd r1 r2 k)
+ | _, _ =>
+ Error(msg "Asmblockgen.transl_cond_op")
+end.
+
+(** Translation of the arithmetic operation [r <- op(args)].
+ The corresponding instructions are prepended to [k]. *)
+
+Definition transl_op
+ (op: operation) (args: list mreg) (res: mreg) (k: bcode) :=
+ match op, args with
+ | Omove, a1 :: nil =>
+ match preg_of res, preg_of a1 with
+ | IR r, IR a => OK (Pmv r a ::i k)
+ | _ , _ => Error(msg "Asmgenblock.transl_op: Omove")
+ end
+ | Ointconst n, nil =>
+ do rd <- ireg_of res;
+ OK (loadimm32 rd n ::i k)
+ | Olongconst n, nil =>
+ do rd <- ireg_of res;
+ OK (loadimm64 rd n ::i k)
+ | Ofloatconst f, nil =>
+ do rd <- freg_of res;
+ OK (Pmakef rd f ::i k)
+ | Osingleconst f, nil =>
+ do rd <- freg_of res;
+ OK (Pmakefs rd f ::i k)
+ | Oaddrsymbol s ofs, nil =>
+ do rd <- ireg_of res;
+ OK (if Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)
+ then Ploadsymbol s Ptrofs.zero rd ::i addptrofs rd rd ofs ::i k
+ else Ploadsymbol s ofs rd ::i k)
+ | Oaddrstack n, nil =>
+ do rd <- ireg_of res;
+ OK (addptrofs rd SP n ::i k)
+
+ | Ocast8signed, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pslliw rd rs (Int.repr 24) ::i Psraiw rd rd (Int.repr 24) ::i k)
+ | Ocast16signed, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pslliw rd rs (Int.repr 16) ::i Psraiw rd rd (Int.repr 16) ::i k)
+ | Oadd, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Paddw rd rs1 rs2 ::i k)
+ | Oaddimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (addimm32 rd rs n ::i k)
+ | Oaddx shift, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Paddxw shift rd rs1 rs2 ::i k)
+ | Oaddximm shift n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Paddxiw shift rd rs n ::i k)
+ | Oaddxl shift, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Paddxl shift rd rs1 rs2 ::i k)
+ | Oaddxlimm shift n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Paddxil shift rd rs n ::i k)
+ | Oneg, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pnegw rd rs ::i k)
+ | Osub, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psubw rd rs1 rs2 ::i k)
+ | Orevsubimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Prevsubiw rd rs n ::i k)
+ | Orevsubx shift, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Prevsubxw shift rd rs1 rs2 ::i k)
+ | Orevsubximm shift n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Prevsubxiw shift rd rs n ::i k)
+ | Omul, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pmulw rd rs1 rs2 ::i k)
+ | Omulimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1;
+ OK (mulimm32 rd rs1 n ::i k)
+ | Omulhs, _ => Error(msg "Asmblockgen.transl_op: Omulhs") (* Normalement pas émis *)
+ | Omulhu, _ => Error(msg "Asmblockgen.transl_op: Omulhu") (* Normalement pas émis *)
+ | Oand, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pandw rd rs1 rs2 ::i k)
+ | Oandimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (andimm32 rd rs n ::i k)
+ | Onand, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnandw rd rs1 rs2 ::i k)
+ | Onandimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (nandimm32 rd rs n ::i k)
+ | Oor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Porw rd rs1 rs2 ::i k)
+ | Onor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnorw rd rs1 rs2 ::i k)
+ | Oorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (orimm32 rd rs n ::i k)
+ | Onorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (norimm32 rd rs n ::i k)
+ | Oxor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pxorw rd rs1 rs2 ::i k)
+ | Oxorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (xorimm32 rd rs n ::i k)
+ | Onxor, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnxorw rd rs1 rs2 ::i k)
+ | Onxorimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (nxorimm32 rd rs n ::i k)
+ | Onot, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (xorimm32 rd rs Int.mone ::i k)
+ | Oandn, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pandnw rd rs1 rs2 ::i k)
+ | Oandnimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pandniw rd rs n ::i k)
+ | Oorn, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pornw rd rs1 rs2 ::i k)
+ | Oornimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Porniw rd rs n ::i k)
+ | Oshl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psllw rd rs1 rs2 ::i k)
+ | Oshlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pslliw rd rs n ::i k)
+ | Oshr, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psraw rd rs1 rs2 ::i k)
+ | Oshrimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psraiw rd rs n ::i k)
+ | Oshru, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psrlw rd rs1 rs2 ::i k)
+ | Oshruimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psrliw rd rs n ::i k)
+ | Oshrximm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psrxiw rd rs n ::i k)
+ | Ororimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Proriw rd rs n ::i k)
+ | Omadd, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ do r3 <- ireg_of a3;
+ OK (Pmaddw r1 r2 r3 ::i k)
+ | Omaddimm n, a1 :: a2 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ OK (Pmaddiw r1 r2 n ::i k)
+ | Omsub, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ do r3 <- ireg_of a3;
+ OK (Pmsubw r1 r2 r3 ::i k)
+ (* [Omakelong], [Ohighlong] should not occur *)
+ | Olowlong, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pcvtl2w rd rs ::i k)
+ | Ocast32signed, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psxwd rd rs ::i k)
+ | Ocast32unsigned, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pzxwd rd rs ::i k)
+(* assertion (ireg_eq rd rs);
+ OK (Pcvtw2l rd ::i Psllil rd rd (Int.repr 32) ::i Psrlil rd rd (Int.repr 32) ::i k) *)
+ | Oaddl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Paddl rd rs1 rs2 ::i k)
+ | Oaddlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (addimm64 rd rs n ::i k)
+ | Onegl, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pnegl rd rs ::i k)
+ | Osubl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psubl rd rs1 rs2 ::i k)
+ | Orevsubxl shift, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Prevsubxl shift rd rs1 rs2 ::i k)
+ | Orevsublimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Prevsubil rd rs n ::i k)
+ | Orevsubxlimm shift n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Prevsubxil shift rd rs n ::i k)
+ | Omull, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pmull rd rs1 rs2 ::i k)
+ | Omullimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1;
+ OK (mulimm64 rd rs1 n ::i k)
+ | Omullhs, _ => Error (msg "Asmblockgen.transl_op: Omullhs") (* Normalement pas émis *)
+ | Omullhu, _ => Error (msg "Asmblockgen.transl_op: Omullhu") (* Normalement pas émis *)
+ | Odivl, _ => Error (msg "Asmblockgen.transl_op: Odivl") (* Géré par fonction externe *)
+ | Odivlu, _ => Error (msg "Asmblockgen.transl_op: Odivlu") (* Géré par fonction externe *)
+ | Omodl, _ => Error (msg "Asmblockgen.transl_op: Omodl") (* Géré par fonction externe *)
+ | Omodlu, _ => Error (msg "Asmblockgen.transl_op: Omodlu") (* Géré par fonction externe *)
+ | Onotl, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (xorimm64 rd rs Int64.mone ::i k)
+ | Oandl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pandl rd rs1 rs2 ::i k)
+ | Oandlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (andimm64 rd rs n ::i k)
+ | Onandl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnandl rd rs1 rs2 ::i k)
+ | Onandlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (nandimm64 rd rs n ::i k)
+ | Oorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Porl rd rs1 rs2 ::i k)
+ | Oorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (orimm64 rd rs n ::i k)
+ | Onorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnorl rd rs1 rs2 ::i k)
+ | Onorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (norimm64 rd rs n ::i k)
+ | Oxorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pxorl rd rs1 rs2 ::i k)
+ | Oxorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (xorimm64 rd rs n ::i k)
+ | Onxorl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pnxorl rd rs1 rs2 ::i k)
+ | Onxorlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (nxorimm64 rd rs n ::i k)
+ | Oandnl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pandnl rd rs1 rs2 ::i k)
+ | Oandnlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pandnil rd rs n ::i k)
+ | Oornl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pornl rd rs1 rs2 ::i k)
+ | Oornlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pornil rd rs n ::i k)
+ | Oshll, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Pslll rd rs1 rs2 ::i k)
+ | Oshllimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psllil rd rs n ::i k)
+ | Oshrl, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psral rd rs1 rs2 ::i k)
+ | Oshrlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psrail rd rs n ::i k)
+ | Oshrlu, a1 :: a2 :: nil =>
+ do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
+ OK (Psrll rd rs1 rs2 ::i k)
+ | Oshrluimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psrlil rd rs n ::i k)
+ | Oshrxlimm n, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Psrxil rd rs n ::i k)
+ | Omaddl, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ do r3 <- ireg_of a3;
+ OK (Pmaddl r1 r2 r3 ::i k)
+ | Omaddlimm n, a1 :: a2 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ OK (Pmaddil r1 r2 n ::i k)
+ | Omsubl, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do r1 <- ireg_of a1;
+ do r2 <- ireg_of a2;
+ do r3 <- ireg_of a3;
+ OK (Pmsubl r1 r2 r3 ::i k)
+ | Oabsf, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfabsd rd rs ::i k)
+ | Oabsfs, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfabsw rd rs ::i k)
+ | Oaddf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfaddd rd rs1 rs2 ::i k)
+ | Oaddfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfaddw rd rs1 rs2 ::i k)
+ | Osubf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfsbfd rd rs1 rs2 ::i k)
+ | Osubfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfsbfw rd rs1 rs2 ::i k)
+ | Omulf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmuld rd rs1 rs2 ::i k)
+ | Omulfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmulw rd rs1 rs2 ::i k)
+ | Ominf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmind rd rs1 rs2 ::i k)
+ | Ominfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfminw rd rs1 rs2 ::i k)
+ | Omaxf, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmaxd rd rs1 rs2 ::i k)
+ | Omaxfs, a1 :: a2 :: nil =>
+ do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
+ OK (Pfmaxw rd rs1 rs2 ::i k)
+ | Onegf, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfnegd rd rs ::i k)
+ | Onegfs, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfnegw rd rs ::i k)
+ | Oinvfs, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfinvw rd rs ::i k)
+
+ | Ofmaddf, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do rs1 <- freg_of a1;
+ do rs2 <- freg_of a2;
+ do rs3 <- freg_of a3;
+ OK (Pfmaddfl rs1 rs2 rs3 ::i k)
+ | Ofmaddfs, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do rs1 <- freg_of a1;
+ do rs2 <- freg_of a2;
+ do rs3 <- freg_of a3;
+ OK (Pfmaddfw rs1 rs2 rs3 ::i k)
+ | Ofmsubf, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do rs1 <- freg_of a1;
+ do rs2 <- freg_of a2;
+ do rs3 <- freg_of a3;
+ OK (Pfmsubfl rs1 rs2 rs3 ::i k)
+ | Ofmsubfs, a1 :: a2 :: a3 :: nil =>
+ assertion (mreg_eq a1 res);
+ do rs1 <- freg_of a1;
+ do rs2 <- freg_of a2;
+ do rs3 <- freg_of a3;
+ OK (Pfmsubfw rs1 rs2 rs3 ::i k)
+
+ | Osingleofint, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfloatwrnsz rd rs ::i k)
+ | Osingleofintu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfloatuwrnsz rd rs ::i k)
+ | Ofloatoflong, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfloatdrnsz rd rs ::i k)
+ | Ofloatoflongu, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfloatudrnsz rd rs ::i k)
+ | Ointofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixedwrzz rd rs ::i k)
+ | Ointuofsingle, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixeduwrzz rd rs ::i k)
+ | Olongoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixeddrzz rd rs ::i k)
+ | Ointoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixeddrzz_i32 rd rs ::i k)
+ | Ointuoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixedudrzz_i32 rd rs ::i k)
+ | Olonguoffloat, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfixedudrzz rd rs ::i k)
+
+ | Ofloatofsingle, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfwidenlwd rd rs ::i k)
+ | Osingleoffloat, a1 :: nil =>
+ do rd <- freg_of res; do rs <- freg_of a1;
+ OK (Pfnarrowdw rd rs ::i k)
+
+
+ | Odivf , _ => Error (msg "Asmblockgen.transl_op: Odivf")
+ | Odivfs, _ => Error (msg "Asmblockgen.transl_op: Odivfs")
+
+ (* We use the Splitlong instead for these four conversions *)
+ | Osingleoflong , _ => Error (msg "Asmblockgen.transl_op: Osingleoflong")
+ | Osingleoflongu , _ => Error (msg "Asmblockgen.transl_op: Osingleoflongu")
+ | Olongofsingle , _ => Error (msg "Asmblockgen.transl_op: Olongofsingle")
+ | Olonguofsingle , _ => Error (msg "Asmblockgen.transl_op: Olonguofsingle")
+
+
+ | Ocmp cmp, _ =>
+ do rd <- ireg_of res;
+ transl_cond_op cmp rd args k
+
+
+ | Oextfz stop start, a1 :: nil =>
+ assertion (ExtValues.is_bitfield stop start);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pextfz stop start rd rs ::i k)
+
+ | Oextfs stop start, a1 :: nil =>
+ assertion (ExtValues.is_bitfield stop start);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pextfs stop start rd rs ::i k)
+
+ | Oextfzl stop start, a1 :: nil =>
+ assertion (ExtValues.is_bitfieldl stop start);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pextfzl stop start rd rs ::i k)
+
+ | Oextfsl stop start, a1 :: nil =>
+ assertion (ExtValues.is_bitfieldl stop start);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pextfsl stop start rd rs ::i k)
+
+ | Oinsf stop start, a0 :: a1 :: nil =>
+ assertion (ExtValues.is_bitfield stop start);
+ assertion (mreg_eq a0 res);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pinsf stop start rd rs ::i k)
+
+ | Oinsfl stop start, a0 :: a1 :: nil =>
+ assertion (ExtValues.is_bitfieldl stop start);
+ assertion (mreg_eq a0 res);
+ do rd <- ireg_of res; do rs <- ireg_of a1;
+ OK (Pinsfl stop start rd rs ::i k)
+
+ | Osel cond0 ty, aT :: aF :: aC :: nil =>
+ assertion (mreg_eq aT res);
+ do rT <- ireg_of aT;
+ do rF <- ireg_of aF;
+ do rC <- ireg_of aC;
+ do op <- conditional_move (negate_condition0 cond0) rC rT rF;
+ OK (op ::i k)
+
+ | Oselimm cond0 imm, aT :: aC :: nil =>
+ assertion (mreg_eq aT res);
+ do rT <- ireg_of aT;
+ do rC <- ireg_of aC;
+ do op <- conditional_move_imm32 (negate_condition0 cond0) rC rT imm;
+ OK (op ::i k)
+
+
+ | Osellimm cond0 imm, aT :: aC :: nil =>
+ assertion (mreg_eq aT res);
+ do rT <- ireg_of aT;
+ do rC <- ireg_of aC;
+ do op <- conditional_move_imm64 (negate_condition0 cond0) rC rT imm;
+ OK (op ::i k)
+
+ | _, _ =>
+ Error(msg "Asmgenblock.transl_op")
+ end.
+
+(** Accessing data in the stack frame. *)
+
+Definition indexed_memory_access
+ (mk_instr: ireg -> offset -> basic)
+ (base: ireg) (ofs: ptrofs) :=
+ match make_immed64 (Ptrofs.to_int64 ofs) with
+ | Imm64_single imm =>
+ mk_instr base (Ptrofs.of_int64 imm)
+end.
+
+Definition loadind (base: ireg) (ofs: ptrofs) (ty: typ) (dst: mreg) (k: bcode) :=
+ match ty, preg_of dst with
+ | Tint, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Plw rd) base ofs ::i k)
+ | Tlong, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pld rd) base ofs ::i k)
+ | Tsingle, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pfls rd) base ofs ::i k)
+ | Tfloat, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pfld rd) base ofs ::i k)
+ | Tany32, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Plw_a rd) base ofs ::i k)
+ | Tany64, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pld_a rd) base ofs ::i k)
+ | _, _ => Error (msg "Asmblockgen.loadind")
+ end.
+
+Definition storeind (src: mreg) (base: ireg) (ofs: ptrofs) (ty: typ) (k: bcode) :=
+ match ty, preg_of src with
+ | Tint, IR rd => OK (indexed_memory_access (PStoreRRO Psw rd) base ofs ::i k)
+ | Tlong, IR rd => OK (indexed_memory_access (PStoreRRO Psd rd) base ofs ::i k)
+ | Tsingle, IR rd => OK (indexed_memory_access (PStoreRRO Pfss rd) base ofs ::i k)
+ | Tfloat, IR rd => OK (indexed_memory_access (PStoreRRO Pfsd rd) base ofs ::i k)
+ | Tany32, IR rd => OK (indexed_memory_access (PStoreRRO Psw_a rd) base ofs ::i k)
+ | Tany64, IR rd => OK (indexed_memory_access (PStoreRRO Psd_a rd) base ofs ::i k)
+ | _, _ => Error (msg "Asmblockgen.storeind")
+ end.
+
+Definition loadind_ptr (base: ireg) (ofs: ptrofs) (dst: ireg) :=
+ indexed_memory_access (PLoadRRO TRAP Pld dst) base ofs.
+
+Definition storeind_ptr (src: ireg) (base: ireg) (ofs: ptrofs) :=
+ indexed_memory_access (PStoreRRO Psd src) base ofs.
+
+(** Translation of memory accesses: loads, and stores. *)
+
+Definition transl_memory_access2
+ (mk_instr: ireg -> ireg -> basic)
+ (addr: addressing) (args: list mreg) (k: bcode) : res bcode :=
+ match addr, args with
+ | Aindexed2, a1 :: a2 :: nil =>
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ OK (mk_instr rs1 rs2 ::i k)
+ | _, _ => Error (msg "Asmblockgen.transl_memory_access2")
+ end.
+
+Definition transl_memory_access2XS
+ (chunk: memory_chunk)
+ (mk_instr: ireg -> ireg -> basic)
+ scale (args: list mreg) (k: bcode) : res bcode :=
+ match args with
+ | (a1 :: a2 :: nil) =>
+ assertion (Z.eqb (zscale_of_chunk chunk) scale);
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ OK (mk_instr rs1 rs2 ::i k)
+ | _ => Error (msg "Asmblockgen.transl_memory_access2XS")
+ end.
+
+Definition transl_memory_access
+ (mk_instr: ireg -> offset -> basic)
+ (addr: addressing) (args: list mreg) (k: bcode) : res bcode :=
+ match addr, args with
+ | Aindexed ofs, a1 :: nil =>
+ do rs <- ireg_of a1;
+ OK (indexed_memory_access mk_instr rs ofs ::i k)
+ | Aglobal id ofs, nil =>
+ OK (Ploadsymbol id ofs RTMP ::i (mk_instr RTMP Ptrofs.zero ::i k))
+ | Ainstack ofs, nil =>
+ OK (indexed_memory_access mk_instr SP ofs ::i k)
+ | _, _ =>
+ Error(msg "Asmblockgen.transl_memory_access")
+ end.
+
+Definition chunk2load (chunk: memory_chunk) :=
+ match chunk with
+ | Mint8signed => Plb
+ | Mint8unsigned => Plbu
+ | Mint16signed => Plh
+ | Mint16unsigned => Plhu
+ | Mint32 => Plw
+ | Mint64 => Pld
+ | Mfloat32 => Pfls
+ | Mfloat64 => Pfld
+ | Many32 => Plw_a
+ | Many64 => Pld_a
+ end.
+
+Definition transl_load_rro (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of dst;
+ transl_memory_access (PLoadRRO trap (chunk2load chunk) r) addr args k.
+
+Definition transl_load_rrr (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of dst;
+ transl_memory_access2 (PLoadRRR trap (chunk2load chunk) r) addr args k.
+
+Definition transl_load_rrrXS (trap: trapping_mode) (chunk: memory_chunk) (scale : Z)
+ (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of dst;
+ transl_memory_access2XS chunk (PLoadRRRXS trap (chunk2load chunk) r) scale args k.
+
+Definition transl_load (trap : trapping_mode)
+ (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
+ match addr with
+ | Aindexed2XS scale => transl_load_rrrXS trap chunk scale args dst k
+ | Aindexed2 => transl_load_rrr trap chunk addr args dst k
+ | _ => transl_load_rro trap chunk addr args dst k
+ end.
+
+Definition chunk2store (chunk: memory_chunk) :=
+ match chunk with
+ | Mint8signed | Mint8unsigned => Psb
+ | Mint16signed | Mint16unsigned => Psh
+ | Mint32 => Psw
+ | Mint64 => Psd
+ | Mfloat32 => Pfss
+ | Mfloat64 => Pfsd
+ | Many32 => Psw_a
+ | Many64 => Psd_a
+ end.
+
+Definition transl_store_rro (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of src;
+ transl_memory_access (PStoreRRO (chunk2store chunk) r) addr args k.
+
+Definition transl_store_rrr (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of src;
+ transl_memory_access2 (PStoreRRR (chunk2store chunk) r) addr args k.
+
+Definition transl_store_rrrxs (chunk: memory_chunk) (scale: Z)
+ (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
+ do r <- ireg_of src;
+ transl_memory_access2XS chunk (PStoreRRRXS (chunk2store chunk) r) scale args k.
+
+Definition transl_store (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
+ match addr with
+ | Aindexed2 => transl_store_rrr chunk addr args src k
+ | Aindexed2XS scale => transl_store_rrrxs chunk scale args src k
+ | _ => transl_store_rro chunk addr args src k
+ end.
+
+(** Function epilogue *)
+
+Definition make_epilogue (f: Machblock.function) (k: code) :=
+ (loadind_ptr SP f.(fn_retaddr_ofs) GPRA)
+ ::g Pset RA GPRA ::g Pfreeframe f.(fn_stacksize) f.(fn_link_ofs) ::g k.
+
+(** Translation of a Machblock instruction. *)
+
+Definition transl_instr_basic (f: Machblock.function) (i: Machblock.basic_inst)
+ (ep: bool) (k: bcode) :=
+ match i with
+ | MBgetstack ofs ty dst =>
+ loadind SP ofs ty dst k
+ | MBsetstack src ofs ty =>
+ storeind src SP ofs ty k
+ | MBgetparam ofs ty dst =>
+ (* load via the frame pointer if it is valid *)
+ do c <- loadind FP ofs ty dst k;
+ OK (if ep then c
+ else (loadind_ptr SP f.(fn_link_ofs) FP) ::i c)
+ | MBop op args res =>
+ transl_op op args res k
+ | MBload trap chunk addr args dst =>
+ transl_load trap chunk addr args dst k
+ | MBstore chunk addr args src =>
+ transl_store chunk addr args src k
+ end.
+
+Definition transl_instr_control (f: Machblock.function) (oi: option Machblock.control_flow_inst)
+ : res code :=
+ match oi with
+ | None => OK nil
+ | Some i =>
+ match i with
+ | MBcall sig (inl r) =>
+ do r1 <- ireg_of r; OK ((Picall r1) ::g nil)
+ | MBcall sig (inr symb) =>
+ OK ((Pcall symb) ::g nil)
+ | MBtailcall sig (inr symb) =>
+ OK (make_epilogue f ((Pgoto symb) ::g nil))
+ | MBtailcall sig (inl r) =>
+ do r1 <- ireg_of r; OK (make_epilogue f ((Pigoto r1) ::g nil))
+ | MBbuiltin ef args res =>
+ OK (Pbuiltin ef (List.map (map_builtin_arg preg_of) args) (map_builtin_res preg_of res) ::g nil)
+ | MBgoto lbl =>
+ OK (Pj_l lbl ::g nil)
+ | MBcond cond args lbl =>
+ transl_cbranch cond args lbl nil
+ | MBreturn =>
+ OK (make_epilogue f (Pret ::g nil))
+ | MBjumptable arg tbl =>
+ do r <- ireg_of arg;
+ OK (Pjumptable r tbl ::g nil)
+ end
+ end.
+
+(** Translation of a code sequence *)
+
+Definition fp_is_parent (before: bool) (i: Machblock.basic_inst) : bool :=
+ match i with
+ | MBgetstack ofs ty dst => before && negb (mreg_eq dst MFP)
+ | MBsetstack src ofs ty => before
+ | MBgetparam ofs ty dst => negb (mreg_eq dst MFP)
+ | MBop op args res => before && negb (mreg_eq res MFP)
+ | MBload trapping_mode chunk addr args dst => before && negb (mreg_eq dst MFP)
+ | MBstore chunk addr args res => before
+ end.
+
+(** This is the naive definition, which is not tail-recursive unlike the other backends *)
+
+Fixpoint transl_basic_code (f: Machblock.function) (il: list Machblock.basic_inst) (it1p: bool) :=
+ match il with
+ | nil => OK nil
+ | i1 :: il' =>
+ do k <- transl_basic_code f il' (fp_is_parent it1p i1);
+ transl_instr_basic f i1 it1p k
+ end.
+
+(* (** This is an equivalent definition in continuation-passing style
+ that runs in constant stack space. *)
+
+Fixpoint transl_basic_rec (f: Machblock.function) (il: list Machblock.basic_inst)
+ (it1p: bool) (k: bcode -> res bcode) :=
+ match il with
+ | nil => k nil
+ | i1 :: il' =>
+ transl_basic_rec f il' (fp_is_parent it1p i1)
+ (fun c1 => do c2 <- transl_instr_basic f i1 it1p c1; k c2)
+ end.
+
+Definition transl_basic_code' (f: Machblock.function) (il: list Machblock.basic_inst) (it1p: bool) :=
+ transl_basic_rec f il it1p (fun c => OK c). *)
+
+(** Translation of a whole function. Note that we must check
+ that the generated code contains less than [2^64] instructions,
+ otherwise the offset part of the [PC] code pointer could wrap
+ around, leading to incorrect executions. *)
+
+(* gen_bblocks can generate two bblocks if the ctl is a PExpand (since the PExpand must be alone in its block) *)
+Program Definition gen_bblocks (hd: list label) (c: list basic) (ctl: list instruction) :=
+ match (extract_ctl ctl) with
+ | None =>
+ match c with
+ | nil => {| header := hd; body := Pnop::nil; exit := None |} :: nil
+ | i::c => {| header := hd; body := ((i::c) ++ extract_basic ctl); exit := None |} :: nil
+ end
+ | Some (PExpand (Pbuiltin ef args res)) =>
+ match c with
+ | nil => {| header := hd; body := nil; exit := Some (PExpand (Pbuiltin ef args res)) |} :: nil
+ | _ => {| header := hd; body := c; exit := None |}
+ :: {| header := nil; body := nil; exit := Some (PExpand (Pbuiltin ef args res)) |} :: nil
+ end
+ | Some ex => {| header := hd; body := (c ++ extract_basic ctl); exit := Some ex |} :: nil
+ end
+.
+Next Obligation.
+ apply wf_bblock_refl. constructor.
+ left. auto.
+ discriminate.
+Qed. Next Obligation.
+ apply wf_bblock_refl. constructor.
+ right. discriminate.
+ unfold builtin_alone. intros. pose (H ef args res). rewrite H0 in n. contradiction.
+Qed.
+
+Definition transl_block (f: Machblock.function) (fb: Machblock.bblock) (ep: bool) : res (list bblock) :=
+ do c <- transl_basic_code f fb.(Machblock.body) ep;
+ do ctl <- transl_instr_control f fb.(Machblock.exit);
+ OK (gen_bblocks fb.(Machblock.header) c ctl)
+.
+
+Fixpoint transl_blocks (f: Machblock.function) (lmb: list Machblock.bblock) (ep: bool) :=
+ match lmb with
+ | nil => OK nil
+ | mb :: lmb =>
+ do lb <- transl_block f mb (if Machblock.header mb then ep else false);
+ do lb' <- transl_blocks f lmb false;
+ OK (lb @@ lb')
+ end
+.
+
+Program Definition make_prologue (f: Machblock.function) lb :=
+ ({| header := nil; body := Pallocframe f.(fn_stacksize) f.(fn_link_ofs) ::i
+ Pget GPRA RA ::i
+ storeind_ptr GPRA SP f.(fn_retaddr_ofs) ::i nil;
+ exit := None |} :: lb).
+
+Definition transl_function (f: Machblock.function) :=
+ do lb <- transl_blocks f f.(Machblock.fn_code) true;
+ OK (mkfunction f.(Machblock.fn_sig)
+ (make_prologue f lb)).
+
+Definition transf_function (f: Machblock.function) : res Asmvliw.function :=
+ do tf <- transl_function f;
+ if zlt Ptrofs.max_unsigned (size_blocks tf.(fn_blocks))
+ then Error (msg "code size exceeded")
+ else OK tf.
+
+Definition transf_fundef (f: Machblock.fundef) : res Asmvliw.fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: Machblock.program) : res Asmvliw.program :=
+ transform_partial_program transf_fundef p.
diff --git a/kvx/Asmblockgenproof.v b/kvx/Asmblockgenproof.v
new file mode 100644
index 00000000..6e3029d8
--- /dev/null
+++ b/kvx/Asmblockgenproof.v
@@ -0,0 +1,1808 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Correctness proof for kvx/Asmblock generation: main proof. *)
+
+Require Import Coqlib Errors.
+Require Import Integers Floats AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations Machblock Conventions Asmblock.
+Require Import Asmblockgen Asmblockgenproof0 Asmblockgenproof1 Asmblockprops.
+Require Import Axioms.
+Require Import Lia.
+
+Module MB := Machblock.
+Module AB := Asmvliw.
+
+Definition match_prog (p: Machblock.program) (tp: Asmvliw.program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros. eapply match_transform_partial_program; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variable prog: Machblock.program.
+Variable tprog: Asmvliw.program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma symbols_preserved:
+ forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof (Genv.find_symbol_match TRANSF).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSF).
+
+Lemma functions_translated:
+ forall b f,
+ Genv.find_funct_ptr ge b = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge b = Some tf /\ transf_fundef f = OK tf.
+Proof (Genv.find_funct_ptr_transf_partial TRANSF).
+
+Lemma functions_transl:
+ forall fb f tf,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transf_function f = OK tf ->
+ Genv.find_funct_ptr tge fb = Some (Internal tf).
+Proof.
+ intros. exploit functions_translated; eauto. intros [tf' [A B]].
+ monadInv B. rewrite H0 in EQ; inv EQ; auto.
+Qed.
+
+Lemma transf_function_no_overflow:
+ forall f tf,
+ transf_function f = OK tf -> size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned.
+Proof.
+ intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (size_blocks x.(fn_blocks))); inv EQ0.
+ lia.
+Qed.
+
+Section TRANSL_LABEL. (* Lemmas on translation of MB.is_label into AB.is_label *)
+
+Lemma gen_bblocks_label:
+ forall hd bdy ex tbb tc,
+ gen_bblocks hd bdy ex = tbb::tc ->
+ header tbb = hd.
+Proof.
+ intros until tc. intros GENB. unfold gen_bblocks in GENB.
+ destruct (extract_ctl ex); try destruct c; try destruct i; try destruct bdy.
+ all: inv GENB; simpl; auto.
+Qed.
+
+Lemma gen_bblocks_label2:
+ forall hd bdy ex tbb1 tbb2,
+ gen_bblocks hd bdy ex = tbb1::tbb2::nil ->
+ header tbb2 = nil.
+Proof.
+ intros until tbb2. intros GENB. unfold gen_bblocks in GENB.
+ destruct (extract_ctl ex); try destruct c; try destruct i; try destruct bdy.
+ all: inv GENB; simpl; auto.
+Qed.
+
+Remark in_dec_transl:
+ forall lbl hd,
+ (if in_dec lbl hd then true else false) = (if MB.in_dec lbl hd then true else false).
+Proof.
+ intros. destruct (in_dec lbl hd), (MB.in_dec lbl hd). all: tauto.
+Qed.
+
+Lemma transl_is_label:
+ forall lbl bb tbb f ep tc,
+ transl_block f bb ep = OK (tbb::tc) ->
+ is_label lbl tbb = MB.is_label lbl bb.
+Proof.
+ intros until tc. intros TLB.
+ destruct tbb as [thd tbdy tex]; simpl in *.
+ monadInv TLB.
+ unfold is_label. simpl.
+ apply gen_bblocks_label in H0. simpl in H0. subst.
+ rewrite in_dec_transl. auto.
+Qed.
+
+Lemma transl_is_label_false2:
+ forall lbl bb f ep tbb1 tbb2,
+ transl_block f bb ep = OK (tbb1::tbb2::nil) ->
+ is_label lbl tbb2 = false.
+Proof.
+ intros until tbb2. intros TLB.
+ destruct tbb2 as [thd tbdy tex]; simpl in *.
+ monadInv TLB. apply gen_bblocks_label2 in H0. simpl in H0. subst.
+ apply is_label_correct_false. simpl. auto.
+Qed.
+
+Lemma transl_is_label2:
+ forall f bb ep tbb1 tbb2 lbl,
+ transl_block f bb ep = OK (tbb1::tbb2::nil) ->
+ is_label lbl tbb1 = MB.is_label lbl bb
+ /\ is_label lbl tbb2 = false.
+Proof.
+ intros. split. eapply transl_is_label; eauto. eapply transl_is_label_false2; eauto.
+Qed.
+
+Lemma transl_block_nonil:
+ forall f c ep tc,
+ transl_block f c ep = OK tc ->
+ tc <> nil.
+Proof.
+ intros. monadInv H. unfold gen_bblocks.
+ destruct (extract_ctl x0); try destruct c0; try destruct x; try destruct i.
+ all: discriminate.
+Qed.
+
+Lemma transl_block_limit: forall f bb ep tbb1 tbb2 tbb3 tc,
+ ~transl_block f bb ep = OK (tbb1 :: tbb2 :: tbb3 :: tc).
+Proof.
+ intros. intro. monadInv H.
+ unfold gen_bblocks in H0.
+ destruct (extract_ctl x0); try destruct x; try destruct c; try destruct i.
+ all: discriminate.
+Qed.
+
+Lemma find_label_transl_false:
+ forall x f lbl bb ep x',
+ transl_block f bb ep = OK x ->
+ MB.is_label lbl bb = false ->
+ find_label lbl (x++x') = find_label lbl x'.
+Proof.
+ intros until x'. intros TLB MBis; simpl; auto.
+ destruct x as [|x0 x1]; simpl; auto.
+ destruct x1 as [|x1 x2]; simpl; auto.
+ - erewrite <- transl_is_label in MBis; eauto. rewrite MBis. auto.
+ - destruct x2 as [|x2 x3]; simpl; auto.
+ + erewrite <- transl_is_label in MBis; eauto. rewrite MBis.
+ erewrite transl_is_label_false2; eauto.
+ + apply transl_block_limit in TLB. destruct TLB.
+Qed.
+
+Lemma transl_blocks_label:
+ forall lbl f c tc ep,
+ transl_blocks f c ep = OK tc ->
+ match MB.find_label lbl c with
+ | None => find_label lbl tc = None
+ | Some c' => exists tc', find_label lbl tc = Some tc' /\ transl_blocks f c' false = OK tc'
+ end.
+Proof.
+ induction c; simpl; intros.
+ inv H. auto.
+ monadInv H.
+ destruct (MB.is_label lbl a) eqn:MBis.
+ - destruct x as [|tbb tc]. { apply transl_block_nonil in EQ. contradiction. }
+ simpl find_label. exploit transl_is_label; eauto. intros ABis. rewrite MBis in ABis.
+ rewrite ABis.
+ eexists. eexists. split; eauto. simpl transl_blocks.
+ assert (MB.header a <> nil).
+ { apply MB.is_label_correct_true in MBis.
+ destruct (MB.header a). contradiction. discriminate. }
+ destruct (MB.header a); try contradiction.
+ rewrite EQ. simpl. rewrite EQ1. simpl. auto.
+ - apply IHc in EQ1. destruct (MB.find_label lbl c).
+ + destruct EQ1 as (tc' & FIND & TLBS). exists tc'; eexists; auto.
+ erewrite find_label_transl_false; eauto.
+ + erewrite find_label_transl_false; eauto.
+Qed.
+
+Lemma find_label_nil:
+ forall bb lbl c,
+ header bb = nil ->
+ find_label lbl (bb::c) = find_label lbl c.
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *. subst.
+ assert (is_label lbl {| AB.header := nil; AB.body := bdy; AB.exit := ex; AB.correct := correct |} = false).
+ { erewrite <- is_label_correct_false. simpl. auto. }
+ rewrite H. auto.
+Qed.
+
+Theorem transl_find_label:
+ forall lbl f tf,
+ transf_function f = OK tf ->
+ match MB.find_label lbl f.(MB.fn_code) with
+ | None => find_label lbl tf.(fn_blocks) = None
+ | Some c => exists tc, find_label lbl tf.(fn_blocks) = Some tc /\ transl_blocks f c false = OK tc
+ end.
+Proof.
+ intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks x))); inv EQ0. clear g.
+ monadInv EQ. unfold make_prologue. simpl fn_blocks. repeat (rewrite find_label_nil); simpl; auto.
+ eapply transl_blocks_label; eauto.
+Qed.
+
+End TRANSL_LABEL.
+
+(** A valid branch in a piece of Machblock code translates to a valid ``go to''
+ transition in the generated Asmblock code. *)
+
+Lemma find_label_goto_label:
+ forall f tf lbl rs m c' b ofs,
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ transf_function f = OK tf ->
+ rs PC = Vptr b ofs ->
+ MB.find_label lbl f.(MB.fn_code) = Some c' ->
+ exists tc', exists rs',
+ goto_label tf lbl rs m = Next rs' m
+ /\ transl_code_at_pc ge (rs' PC) b f c' false tf tc'
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ intros. exploit (transl_find_label lbl f tf); eauto. rewrite H2.
+ intros (tc & A & B).
+ exploit label_pos_code_tail; eauto. instantiate (1 := 0).
+ intros [pos' [P [Q R]]].
+ exists tc; exists (rs#PC <- (Vptr b (Ptrofs.repr pos'))).
+ split. unfold goto_label. unfold par_goto_label. rewrite P. rewrite H1. auto.
+ split. rewrite Pregmap.gss. constructor; auto.
+ rewrite Ptrofs.unsigned_repr. replace (pos' - 0) with pos' in Q.
+ auto. lia.
+ generalize (transf_function_no_overflow _ _ H0). lia.
+ intros. apply Pregmap.gso; auto.
+Qed.
+
+(** Existence of return addresses *)
+
+Lemma return_address_exists:
+ forall b f c, is_tail (b :: c) f.(MB.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros. eapply Asmblockgenproof0.return_address_exists; eauto.
+
+- intros. monadInv H0.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks x.(fn_blocks))); inv EQ0. monadInv EQ. simpl.
+ exists x; exists true; split; auto.
+ repeat constructor.
+- exact transf_function_no_overflow.
+Qed.
+
+(** * Proof of semantic preservation *)
+
+(** Semantic preservation is proved using a complex simulation diagram
+ of the following form.
+<<
+ MB.step
+ ---------------------------------------->
+ header body exit
+ st1 -----> st2 -----> st3 ------------------> st4
+ | | | |
+ | (A) | (B) | (C) |
+ match_codestate | | | |
+ | header | body1 | body2 | match_states
+ cs1 -----> cs2 -----> cs3 ------> cs4 |
+ | / \ exit |
+ match_asmstate | --------------- --->--- |
+ | / match_asmstate \ |
+ st'1 ---------------------------------------> st'2
+ AB.step *
+>>
+ The invariant between each MB.step/AB.step is the [match_states] predicate below.
+ However, we also need to introduce an intermediary state [Codestate] which allows
+ us to reason on a finer grain, executing header, body and exit separately.
+
+ This [Codestate] consists in a state like [Asmblock.State], except that the
+ code is directly stored in the state, much like [Machblock.State]. It also features
+ additional useful elements to keep track of while executing a bblock.
+*)
+
+Remark preg_of_not_FP: forall r, negb (mreg_eq r MFP) = true -> IR FP <> preg_of r.
+Proof.
+ intros. change (IR FP) with (preg_of MFP). red; intros.
+ exploit preg_of_injective; eauto. intros; subst r; discriminate.
+Qed.
+
+Inductive match_states: Machblock.state -> Asmvliw.state -> Prop :=
+ | match_states_intro:
+ forall s fb sp c ep ms m m' rs f tf tc
+ (STACKS: match_stack ge s)
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (MEXT: Mem.extends m m')
+ (AT: transl_code_at_pc ge (rs PC) fb f c ep tf tc)
+ (AG: agree ms sp rs)
+ (DXP: ep = true -> rs#FP = parent_sp s),
+ match_states (Machblock.State s fb sp c ms m)
+ (Asmvliw.State rs m')
+ | match_states_call:
+ forall s fb ms m m' rs
+ (STACKS: match_stack ge s)
+ (MEXT: Mem.extends m m')
+ (AG: agree ms (parent_sp s) rs)
+ (ATPC: rs PC = Vptr fb Ptrofs.zero)
+ (ATLR: rs RA = parent_ra s),
+ match_states (Machblock.Callstate s fb ms m)
+ (Asmvliw.State rs m')
+ | match_states_return:
+ forall s ms m m' rs
+ (STACKS: match_stack ge s)
+ (MEXT: Mem.extends m m')
+ (AG: agree ms (parent_sp s) rs)
+ (ATPC: rs PC = parent_ra s),
+ match_states (Machblock.Returnstate s ms m)
+ (Asmvliw.State rs m').
+
+Record codestate :=
+ Codestate { pstate: state; (**r projection to Asmblock.state *)
+ pheader: list label;
+ pbody1: list basic; (**r list of basic instructions coming from the translation of the Machblock body *)
+ pbody2: list basic; (**r list of basic instructions coming from the translation of the Machblock exit *)
+ pctl: option control; (**r exit instruction, coming from the translation of the Machblock exit *)
+ ep: bool; (**r reflects the [ep] variable used in the translation *)
+ rem: list AB.bblock; (**r remaining bblocks to execute *)
+ cur: bblock (**r current bblock to execute - to keep track of its size when incrementing PC *)
+ }.
+
+(* The part that deals with Machblock <-> Codestate agreement
+ * Note about DXP: the property of [ep] only matters if the current block doesn't have a header, hence the condition *)
+Inductive match_codestate fb: Machblock.state -> codestate -> Prop :=
+ | match_codestate_intro:
+ forall s sp ms m rs0 m0 f tc ep c bb tbb tbc tbi
+ (STACKS: match_stack ge s)
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (MEXT: Mem.extends m m0)
+ (TBC: transl_basic_code f (MB.body bb) (if MB.header bb then ep else false) = OK tbc)
+ (TIC: transl_instr_control f (MB.exit bb) = OK tbi)
+ (TBLS: transl_blocks f c false = OK tc)
+ (AG: agree ms sp rs0)
+ (DXP: (if MB.header bb then ep else false) = true -> rs0#FP = parent_sp s)
+ ,
+ match_codestate fb (Machblock.State s fb sp (bb::c) ms m)
+ {| pstate := (Asmvliw.State rs0 m0);
+ pheader := (MB.header bb);
+ pbody1 := tbc;
+ pbody2 := extract_basic tbi;
+ pctl := extract_ctl tbi;
+ ep := ep;
+ rem := tc;
+ cur := tbb
+ |}
+.
+
+(* The part ensuring that the code in Codestate actually resides at [rs PC] *)
+Inductive match_asmstate fb: codestate -> Asmvliw.state -> Prop :=
+ | match_asmstate_some:
+ forall rs f tf tc m tbb ofs ep tbdy tex lhd
+ (FIND: Genv.find_funct_ptr ge fb = Some (Internal f))
+ (TRANSF: transf_function f = OK tf)
+ (PCeq: rs PC = Vptr fb ofs)
+ (TAIL: code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) (tbb::tc))
+ ,
+ match_asmstate fb
+ {| pstate := (Asmvliw.State rs m);
+ pheader := lhd;
+ pbody1 := tbdy;
+ pbody2 := extract_basic tex;
+ pctl := extract_ctl tex;
+ ep := ep;
+ rem := tc;
+ cur := tbb |}
+ (Asmvliw.State rs m)
+.
+
+(* Useful for dealing with the many cases in some proofs *)
+Ltac exploreInst :=
+ repeat match goal with
+ | [ H : match ?var with | _ => _ end = _ |- _ ] => destruct var
+ | [ H : OK _ = OK _ |- _ ] => monadInv H
+ | [ |- context[if ?b then _ else _] ] => destruct b
+ | [ |- context[match ?m with | _ => _ end] ] => destruct m
+ | [ |- context[match ?m as _ return _ with | _ => _ end]] => destruct m
+ | [ H : bind _ _ = OK _ |- _ ] => monadInv H
+ | [ H : Error _ = OK _ |- _ ] => inversion H
+ end.
+
+(** Some translation properties *)
+
+Lemma transl_blocks_nonil:
+ forall f bb c tc ep,
+ transl_blocks f (bb::c) ep = OK tc ->
+ exists tbb tc', tc = tbb :: tc'.
+Proof.
+ intros until ep0. intros TLBS. monadInv TLBS. monadInv EQ. unfold gen_bblocks.
+ destruct (extract_ctl x2).
+ - destruct c0; destruct i; simpl; eauto. destruct x1; simpl; eauto.
+ - destruct x1; simpl; eauto.
+Qed.
+
+Lemma no_builtin_preserved:
+ forall f ex x2,
+ (forall ef args res, ex <> Some (MBbuiltin ef args res)) ->
+ transl_instr_control f ex = OK x2 ->
+ (exists i, extract_ctl x2 = Some (PCtlFlow i))
+ \/ extract_ctl x2 = None.
+Proof.
+ intros until x2. intros Hbuiltin TIC.
+ destruct ex.
+ - destruct c.
+ (* MBcall *)
+ + simpl in TIC. exploreInst; simpl; eauto.
+ (* MBtailcall *)
+ + simpl in TIC. exploreInst; simpl; eauto.
+ (* MBbuiltin *)
+ + assert (H: Some (MBbuiltin e l b) <> Some (MBbuiltin e l b)).
+ apply Hbuiltin. contradict H; auto.
+ (* MBgoto *)
+ + simpl in TIC. exploreInst; simpl; eauto.
+ (* MBcond *)
+ + simpl in TIC. unfold transl_cbranch in TIC. exploreInst; simpl; eauto.
+ * unfold transl_opt_compuimm. exploreInst; simpl; eauto.
+ * unfold transl_opt_compluimm. exploreInst; simpl; eauto.
+ * unfold transl_comp_float64. exploreInst; simpl; eauto.
+ * unfold transl_comp_notfloat64. exploreInst; simpl; eauto.
+ * unfold transl_comp_float32. exploreInst; simpl; eauto.
+ * unfold transl_comp_notfloat32. exploreInst; simpl; eauto.
+ (* MBjumptable *)
+ + simpl in TIC. exploreInst; simpl; eauto.
+ (* MBreturn *)
+ + simpl in TIC. monadInv TIC. simpl. eauto.
+ - monadInv TIC. simpl; auto.
+Qed.
+
+Lemma transl_blocks_distrib:
+ forall c f bb tbb tc ep,
+ transl_blocks f (bb::c) ep = OK (tbb::tc)
+ -> (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res))
+ -> transl_block f bb (if MB.header bb then ep else false) = OK (tbb :: nil)
+ /\ transl_blocks f c false = OK tc.
+Proof.
+ intros until ep0. intros TLBS Hbuiltin.
+ destruct bb as [hd bdy ex].
+ monadInv TLBS. monadInv EQ.
+ exploit no_builtin_preserved; eauto. intros Hectl. destruct Hectl.
+ - destruct H as [i Hectl].
+ unfold gen_bblocks in H0. rewrite Hectl in H0. inv H0.
+ simpl in *. unfold transl_block; simpl. rewrite EQ0. rewrite EQ. simpl.
+ unfold gen_bblocks. rewrite Hectl. auto.
+ - unfold gen_bblocks in H0. rewrite H in H0.
+ destruct x1 as [|bi x1].
+ + simpl in H0. inv H0. simpl in *. unfold transl_block; simpl. rewrite EQ0. rewrite EQ. simpl.
+ unfold gen_bblocks. rewrite H. auto.
+ + simpl in H0. inv H0. simpl in *. unfold transl_block; simpl. rewrite EQ0. rewrite EQ. simpl.
+ unfold gen_bblocks. rewrite H. auto.
+Qed.
+
+Lemma gen_bblocks_nobuiltin:
+ forall thd tbdy tex tbb,
+ (tbdy <> nil \/ extract_ctl tex <> None) ->
+ (forall ef args res, extract_ctl tex <> Some (PExpand (Pbuiltin ef args res))) ->
+ gen_bblocks thd tbdy tex = tbb :: nil ->
+ header tbb = thd
+ /\ body tbb = tbdy ++ extract_basic tex
+ /\ exit tbb = extract_ctl tex.
+Proof.
+ intros until tbb. intros Hnonil Hnobuiltin GENB. unfold gen_bblocks in GENB.
+ destruct (extract_ctl tex) eqn:ECTL.
+ - destruct c.
+ + destruct i; try (inv GENB; simpl; auto; fail).
+ assert False. eapply Hnobuiltin. eauto. destruct H.
+ + inv GENB. simpl. auto.
+ - inversion Hnonil.
+ + destruct tbdy as [|bi tbdy]; try (contradict H; simpl; auto; fail). inv GENB. auto.
+ + contradict H; simpl; auto.
+Qed.
+
+Lemma transl_instr_basic_nonil:
+ forall k f bi ep x,
+ transl_instr_basic f bi ep k = OK x ->
+ x <> nil.
+Proof.
+ intros until x. intros TIB.
+ destruct bi.
+ - simpl in TIB. unfold loadind in TIB. exploreInst; try discriminate.
+ - simpl in TIB. unfold storeind in TIB. exploreInst; try discriminate.
+ - simpl in TIB. monadInv TIB. unfold loadind in EQ. exploreInst; try discriminate.
+ - simpl in TIB. unfold transl_op in TIB. exploreInst; try discriminate.
+ unfold transl_cond_op in EQ0. exploreInst; try discriminate.
+ unfold transl_cond_float64. exploreInst; try discriminate.
+ unfold transl_cond_notfloat64. exploreInst; try discriminate.
+ unfold transl_cond_float32. exploreInst; try discriminate.
+ unfold transl_cond_notfloat32. exploreInst; try discriminate.
+ - simpl in TIB. unfold transl_load in TIB. exploreInst; try discriminate.
+ all: monadInv TIB; unfold transl_memory_access in EQ0; unfold transl_memory_access2 in EQ0; unfold transl_memory_access2XS in EQ0; exploreInst; try discriminate.
+ - simpl in TIB. unfold transl_store in TIB. exploreInst; try discriminate.
+ all: monadInv TIB; unfold transl_memory_access in EQ0; unfold transl_memory_access2 in EQ0; unfold transl_memory_access2XS in EQ0; exploreInst; try discriminate.
+Qed.
+
+Lemma transl_basic_code_nonil:
+ forall bdy f x ep,
+ bdy <> nil ->
+ transl_basic_code f bdy ep = OK x ->
+ x <> nil.
+Proof.
+ induction bdy as [|bi bdy].
+ intros. contradict H0; auto.
+ destruct bdy as [|bi2 bdy].
+ - clear IHbdy. intros f x b _ TBC. simpl in TBC. eapply transl_instr_basic_nonil; eauto.
+ - intros f x b Hnonil TBC. remember (bi2 :: bdy) as bdy'.
+ monadInv TBC.
+ assert (x0 <> nil).
+ eapply IHbdy; eauto. subst bdy'. discriminate.
+ eapply transl_instr_basic_nonil; eauto.
+Qed.
+
+Lemma transl_instr_control_nonil:
+ forall ex f x,
+ ex <> None ->
+ transl_instr_control f ex = OK x ->
+ extract_ctl x <> None.
+Proof.
+ intros ex f x Hnonil TIC.
+ destruct ex as [ex|].
+ - clear Hnonil. destruct ex.
+ all: try (simpl in TIC; exploreInst; discriminate).
+ + simpl in TIC. unfold transl_cbranch in TIC. exploreInst; try discriminate.
+ * unfold transl_opt_compuimm. exploreInst; try discriminate.
+ * unfold transl_opt_compluimm. exploreInst; try discriminate.
+ * unfold transl_comp_float64. exploreInst; try discriminate.
+ * unfold transl_comp_notfloat64. exploreInst; try discriminate.
+ * unfold transl_comp_float32. exploreInst; try discriminate.
+ * unfold transl_comp_notfloat32. exploreInst; try discriminate.
+ - contradict Hnonil; auto.
+Qed.
+
+Lemma transl_instr_control_nobuiltin:
+ forall f ex x,
+ (forall ef args res, ex <> Some (MBbuiltin ef args res)) ->
+ transl_instr_control f ex = OK x ->
+ (forall ef args res, extract_ctl x <> Some (PExpand (Pbuiltin ef args res))).
+Proof.
+ intros until x. intros Hnobuiltin TIC. intros until res.
+ unfold transl_instr_control in TIC. exploreInst.
+ all: try discriminate.
+ - assert False. eapply Hnobuiltin; eauto. destruct H.
+ - unfold transl_cbranch in TIC. exploreInst.
+ all: try discriminate.
+ * unfold transl_opt_compuimm. exploreInst. all: try discriminate.
+ * unfold transl_opt_compluimm. exploreInst. all: try discriminate.
+ * unfold transl_comp_float64. exploreInst; try discriminate.
+ * unfold transl_comp_notfloat64. exploreInst; try discriminate.
+ * unfold transl_comp_float32. exploreInst; try discriminate.
+ * unfold transl_comp_notfloat32. exploreInst; try discriminate.
+Qed.
+
+(* Proving that one can decompose a [match_state] relation into a [match_codestate]
+ and a [match_asmstate], along with some helpful properties tying both relations together *)
+
+Theorem match_state_codestate:
+ forall mbs abs s fb sp bb c ms m,
+ (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res)) ->
+ (MB.body bb <> nil \/ MB.exit bb <> None) ->
+ mbs = (Machblock.State s fb sp (bb::c) ms m) ->
+ match_states mbs abs ->
+ exists cs fb f tbb tc ep,
+ match_codestate fb mbs cs /\ match_asmstate fb cs abs
+ /\ Genv.find_funct_ptr ge fb = Some (Internal f)
+ /\ transl_blocks f (bb::c) ep = OK (tbb::tc)
+ /\ body tbb = pbody1 cs ++ pbody2 cs
+ /\ exit tbb = pctl cs
+ /\ cur cs = tbb /\ rem cs = tc
+ /\ pstate cs = abs.
+Proof.
+ intros until m. intros Hnobuiltin Hnotempty Hmbs MS. subst. inv MS.
+ inv AT. clear H0. exploit transl_blocks_nonil; eauto. intros (tbb & tc' & Htc). subst.
+ exploit transl_blocks_distrib; eauto. intros (TLB & TLBS). clear H2.
+ monadInv TLB. exploit gen_bblocks_nobuiltin; eauto.
+ { inversion Hnotempty.
+ - destruct (MB.body bb) as [|bi bdy]; try (contradict H0; simpl; auto; fail).
+ left. eapply transl_basic_code_nonil; eauto.
+ - destruct (MB.exit bb) as [ei|]; try (contradict H0; simpl; auto; fail).
+ right. eapply transl_instr_control_nonil; eauto. }
+ eapply transl_instr_control_nobuiltin; eauto.
+ intros (Hth & Htbdy & Htexit).
+ exists {| pstate := (State rs m'); pheader := (Machblock.header bb); pbody1 := x; pbody2 := extract_basic x0;
+ pctl := extract_ctl x0; ep := ep0; rem := tc'; cur := tbb |}, fb, f, tbb, tc', ep0.
+ repeat split. 1-2: econstructor; eauto.
+ { destruct (MB.header bb). eauto. discriminate. } eauto.
+ unfold transl_blocks. fold transl_blocks. unfold transl_block. rewrite EQ. simpl. rewrite EQ1; simpl.
+ rewrite TLBS. simpl. rewrite H2.
+ all: simpl; auto.
+Qed.
+
+Definition mb_remove_body (bb: MB.bblock) :=
+ {| MB.header := MB.header bb; MB.body := nil; MB.exit := MB.exit bb |}.
+
+Lemma exec_straight_pnil:
+ forall c rs1 m1 rs2 m2,
+ exec_straight tge c rs1 m1 (Pnop ::g nil) rs2 m2 ->
+ exec_straight tge c rs1 m1 nil rs2 m2.
+Proof.
+ intros. eapply exec_straight_trans. eapply H. econstructor; eauto.
+Qed.
+
+Lemma transl_block_nobuiltin:
+ forall f bb ep tbb,
+ (MB.body bb <> nil \/ MB.exit bb <> None) ->
+ (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res)) ->
+ transl_block f bb ep = OK (tbb :: nil) ->
+ exists c c',
+ transl_basic_code f (MB.body bb) ep = OK c
+ /\ transl_instr_control f (MB.exit bb) = OK c'
+ /\ body tbb = c ++ extract_basic c'
+ /\ exit tbb = extract_ctl c'.
+Proof.
+ intros until tbb. intros Hnonil Hnobuiltin TLB. monadInv TLB. destruct Hnonil.
+ - eexists. eexists. split; eauto. split; eauto. eapply gen_bblocks_nobuiltin; eauto.
+ left. eapply transl_basic_code_nonil; eauto. eapply transl_instr_control_nobuiltin; eauto.
+ - eexists. eexists. split; eauto. split; eauto. eapply gen_bblocks_nobuiltin; eauto.
+ right. eapply transl_instr_control_nonil; eauto. eapply transl_instr_control_nobuiltin; eauto.
+Qed.
+
+Lemma nextblock_preserves:
+ forall rs rs' bb r,
+ rs' = nextblock bb rs ->
+ data_preg r = true ->
+ rs r = rs' r.
+Proof.
+ intros. destruct r; try discriminate.
+ subst. Simpl.
+Qed.
+
+Remark cons3_app {A: Type}:
+ forall a b c (l: list A),
+ a :: b :: c :: l = (a :: b :: c :: nil) ++ l.
+Proof.
+ intros. simpl. auto.
+Qed.
+
+Lemma exec_straight_opt_body2:
+ forall c rs1 m1 c' rs2 m2,
+ exec_straight_opt tge c rs1 m1 c' rs2 m2 ->
+ exists body,
+ exec_body tge body rs1 m1 = Next rs2 m2
+ /\ (basics_to_code body) ++g c' = c.
+Proof.
+ intros until m2. intros EXES.
+ inv EXES.
+ - exists nil. split; auto.
+ - eapply exec_straight_body2. auto.
+Qed.
+
+Lemma extract_basics_to_code:
+ forall lb c,
+ extract_basic (basics_to_code lb ++ c) = lb ++ extract_basic c.
+Proof.
+ induction lb; intros; simpl; congruence.
+Qed.
+
+Lemma extract_ctl_basics_to_code:
+ forall lb c,
+ extract_ctl (basics_to_code lb ++ c) = extract_ctl c.
+Proof.
+ induction lb; intros; simpl; congruence.
+Qed.
+
+(* See (C) in the diagram. The proofs are mostly adapted from the previous Mach->Asm proofs, but are
+ unfortunately quite cumbersome. To reproduce them, it's best to have a Coq IDE with you and see by
+ yourself the steps *)
+Theorem step_simu_control:
+ forall bb' fb fn s sp c ms' m' rs2 m2 t S'' rs1 m1 tbb tbdy2 tex cs2,
+ MB.body bb' = nil ->
+ (forall ef args res, MB.exit bb' <> Some (MBbuiltin ef args res)) ->
+ Genv.find_funct_ptr tge fb = Some (Internal fn) ->
+ pstate cs2 = (Asmvliw.State rs2 m2) ->
+ pbody1 cs2 = nil -> pbody2 cs2 = tbdy2 -> pctl cs2 = tex ->
+ cur cs2 = tbb ->
+ match_codestate fb (MB.State s fb sp (bb'::c) ms' m') cs2 ->
+ match_asmstate fb cs2 (Asmvliw.State rs1 m1) ->
+ exit_step return_address_offset ge (MB.exit bb') (MB.State s fb sp (bb'::c) ms' m') t S'' ->
+ (exists rs3 m3 rs4 m4,
+ exec_body tge tbdy2 rs2 m2 = Next rs3 m3
+ /\ exec_control_rel tge fn tex tbb rs3 m3 rs4 m4
+ /\ match_states S'' (State rs4 m4)).
+Proof.
+ intros until cs2. intros Hbody Hbuiltin FIND Hpstate Hpbody1 Hpbody2 Hpctl Hcur MCS MAS ESTEP.
+ inv ESTEP.
+ - inv MCS. inv MAS. simpl in *.
+ inv Hpstate.
+ destruct ctl.
+ + (* MBcall *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ destruct s1 as [rf|fid]; simpl in H7.
+ * (* Indirect call *)
+ monadInv H1.
+ assert (ms' rf = Vptr f' Ptrofs.zero).
+ { unfold find_function_ptr in H14. destruct (ms' rf); try discriminate.
+ revert H14; predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
+ assert (rs2 x = Vptr f' Ptrofs.zero).
+ { exploit ireg_val; eauto. rewrite H; intros LD; inv LD; auto. }
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+ remember (Ptrofs.add _ _) as ofs'.
+ assert (TCA: transl_code_at_pc ge (Vptr fb ofs') fb f c false tf tc).
+ { econstructor; eauto. }
+ assert (f1 = f) by congruence. subst f1.
+ exploit return_address_offset_correct; eauto. intros; subst ra.
+
+ repeat eexists.
+ rewrite H6. econstructor; eauto.
+ rewrite H7. econstructor; eauto.
+ econstructor; eauto.
+ econstructor; eauto. eapply agree_sp_def; eauto. simpl. eapply agree_exten; eauto. intros. Simpl.
+ simpl. Simpl. rewrite PCeq. rewrite Heqofs'. simpl. auto.
+
+ * (* Direct call *)
+ monadInv H1.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+ remember (Ptrofs.add _ _) as ofs'.
+ assert (TCA: transl_code_at_pc ge (Vptr fb ofs') fb f c false tf tc).
+ econstructor; eauto.
+ assert (f1 = f) by congruence. subst f1.
+ exploit return_address_offset_correct; eauto. intros; subst ra.
+ repeat eexists.
+ rewrite H6. econstructor; eauto.
+ rewrite H7. econstructor; eauto.
+ econstructor; eauto.
+ econstructor; eauto. eapply agree_sp_def; eauto. simpl. eapply agree_exten; eauto. intros. Simpl.
+ Simpl. unfold Genv.symbol_address. rewrite symbols_preserved. simpl in H14. rewrite H14. auto.
+ Simpl. simpl. subst. Simpl. simpl. unfold Val.offset_ptr. rewrite PCeq. auto.
+ + (* MBtailcall *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ exploit Mem.loadv_extends. eauto. eexact H15. auto. simpl. intros [parent' [A B]].
+ destruct s1 as [rf|fid]; simpl in H13.
+ * monadInv H1.
+ assert (ms' rf = Vptr f' Ptrofs.zero).
+ { destruct (ms' rf); try discriminate. revert H13. predSpec Ptrofs.eq Ptrofs.eq_spec i Ptrofs.zero; intros; congruence. }
+ assert (rs2 x = Vptr f' Ptrofs.zero).
+ { exploit ireg_val; eauto. rewrite H; intros LD; inv LD; auto. }
+
+ assert (f = f1) by congruence. subst f1. clear FIND1. clear H14.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ { simpl. eauto. }
+ intros EXEB.
+ repeat eexists.
+ rewrite H6. simpl extract_basic. eauto.
+ rewrite H7. simpl extract_ctl. simpl. reflexivity.
+ econstructor; eauto.
+ { apply agree_set_other.
+ - econstructor; auto with asmgen.
+ + apply V.
+ + intro r. destruct r; apply V; auto.
+ - eauto with asmgen. }
+ assert (IR x <> IR GPR12 /\ IR x <> IR GPR32 /\ IR x <> IR GPR16).
+ { clear - EQ. destruct x; repeat split; try discriminate.
+ all: unfold ireg_of in EQ; destruct rf; try discriminate. }
+ Simpl. inv H1. inv H3. rewrite Z; auto; try discriminate.
+ * monadInv H1. assert (f = f1) by congruence. subst f1. clear FIND1. clear H14.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ simpl. eauto.
+ intros EXEB.
+ repeat eexists.
+ rewrite H6. simpl extract_basic. eauto.
+ rewrite H7. simpl extract_ctl. simpl. reflexivity.
+ econstructor; eauto.
+ { apply agree_set_other.
+ - econstructor; auto with asmgen.
+ + apply V.
+ + intro r. destruct r; apply V; auto.
+ - eauto with asmgen. }
+ { Simpl. unfold Genv.symbol_address. rewrite symbols_preserved. rewrite H13. auto. }
+ + (* MBbuiltin (contradiction) *)
+ assert (MB.exit bb' <> Some (MBbuiltin e l b)) by (apply Hbuiltin).
+ rewrite <- H in H1. contradict H1; auto.
+ + (* MBgoto *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0. assert (f1 = f) by congruence. subst f1. clear H11.
+ remember (nextblock tbb rs2) as rs2'.
+ exploit functions_transl. eapply FIND0. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+ exploit find_label_goto_label.
+ eauto. eauto.
+ instantiate (2 := rs2').
+ { subst. unfold nextblock, incrPC. Simpl. unfold Val.offset_ptr. rewrite PCeq. eauto. }
+ eauto.
+ intros (tc' & rs' & GOTO & AT2 & INV).
+
+ eexists. eexists. repeat eexists. repeat split.
+ rewrite H6. simpl extract_basic. simpl. eauto.
+ rewrite H7. simpl extract_ctl. simpl. rewrite <- Heqrs2'. eauto.
+ econstructor; eauto.
+ rewrite Heqrs2' in INV. unfold nextblock, incrPC in INV.
+ eapply agree_exten; eauto with asmgen.
+ assert (forall r : preg, r <> PC -> rs' r = rs2 r).
+ { intros. destruct r.
+ - destruct g. all: rewrite INV; Simpl; auto.
+ - rewrite INV; Simpl; auto.
+ - contradiction. }
+ eauto with asmgen.
+ congruence.
+ + (* MBcond *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ * (* MBcond true *)
+ assert (f0 = f) by congruence. subst f0.
+ exploit eval_condition_lessdef.
+ eapply preg_vals; eauto.
+ all: eauto.
+ intros EC.
+ exploit transl_cbranch_correct_true; eauto. intros (rs' & jmp & A & B & C).
+ exploit exec_straight_opt_body2. eauto. intros (bdy & EXEB & BTC).
+ assert (PCeq': rs2 PC = rs' PC). { inv A; auto. erewrite <- exec_straight_pc. 2: eapply H. eauto. }
+ rewrite PCeq' in PCeq.
+ assert (f1 = f) by congruence. subst f1.
+ exploit find_label_goto_label.
+ 4: eapply H16. 1-2: eauto. instantiate (2 := (nextblock tbb rs')). rewrite nextblock_pc.
+ unfold Val.offset_ptr. rewrite PCeq. eauto.
+ intros (tc' & rs3 & GOTOL & TLPC & Hrs3).
+ exploit functions_transl. eapply FIND1. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+
+ repeat eexists.
+ rewrite H6. rewrite <- BTC. rewrite extract_basics_to_code. simpl. rewrite app_nil_r. eauto.
+ rewrite H7. rewrite <- BTC. rewrite extract_ctl_basics_to_code. simpl extract_ctl. rewrite B. eauto.
+
+ econstructor; eauto.
+ eapply agree_exten with rs2; eauto with asmgen.
+ { intros. destruct r; try destruct g; try discriminate.
+ all: rewrite Hrs3; try discriminate; unfold nextblock, incrPC; Simpl. }
+ intros. discriminate.
+
+ * (* MBcond false *)
+ assert (f0 = f) by congruence. subst f0.
+ exploit eval_condition_lessdef.
+ eapply preg_vals; eauto.
+ all: eauto.
+ intros EC.
+
+ exploit transl_cbranch_correct_false; eauto. intros (rs' & jmp & A & B & C).
+ exploit exec_straight_opt_body2. eauto. intros (bdy & EXEB & BTC).
+ assert (PCeq': rs2 PC = rs' PC). { inv A; auto. erewrite <- exec_straight_pc. 2: eapply H. eauto. }
+ rewrite PCeq' in PCeq.
+ exploit functions_transl. eapply FIND1. eapply TRANSF0. intros FIND'.
+ assert (tf = fn) by congruence. subst tf.
+
+ assert (NOOV: size_blocks fn.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1.
+
+ repeat eexists.
+ rewrite H6. rewrite <- BTC. rewrite extract_basics_to_code. simpl. rewrite app_nil_r. eauto.
+ rewrite H7. rewrite <- BTC. rewrite extract_ctl_basics_to_code. simpl extract_ctl. rewrite B. eauto.
+
+ econstructor; eauto.
+ unfold nextblock, incrPC. Simpl. unfold Val.offset_ptr. rewrite PCeq. econstructor; eauto.
+ eapply agree_exten with rs2; eauto with asmgen.
+ { intros. destruct r; try destruct g; try discriminate.
+ all: rewrite <- C; try discriminate; unfold nextblock, incrPC; Simpl. }
+ intros. discriminate.
+ + (* MBjumptable *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ monadInv H1.
+ generalize (transf_function_no_overflow _ _ TRANSF0); intro NOOV.
+ assert (f1 = f) by congruence. subst f1.
+ exploit find_label_goto_label. 4: eapply H16. 1-2: eauto. instantiate (2 := (nextblock tbb rs2) # GPR62 <- Vundef # GPR63 <- Vundef).
+ unfold nextblock, incrPC. Simpl. unfold Val.offset_ptr. rewrite PCeq. reflexivity.
+ exploit functions_transl. eapply FIND0. eapply TRANSF0. intros FIND3. assert (fn = tf) by congruence. subst fn.
+
+ intros [tc' [rs' [A [B C]]]].
+ exploit ireg_val; eauto. rewrite H13. intros LD; inv LD.
+
+ repeat eexists.
+ rewrite H6. simpl extract_basic. simpl. eauto.
+ rewrite H7. simpl extract_ctl. simpl. Simpl. rewrite <- H1. unfold Mach.label in H14. unfold label. rewrite H14. eapply A.
+ econstructor; eauto.
+ eapply agree_undef_regs; eauto. intros. rewrite C; auto with asmgen.
+ { assert (destroyed_by_jumptable = R62 :: R63 :: nil) by auto. rewrite H2 in H0. simpl in H0. inv H0.
+ destruct (preg_eq r' GPR63). subst. contradiction.
+ destruct (preg_eq r' GPR62). subst. contradiction.
+ destruct r'; Simpl. }
+ discriminate.
+ + (* MBreturn *)
+ destruct bb' as [mhd' mbdy' mex']; simpl in *. subst.
+ inv TBC. inv TIC. inv H0.
+
+ assert (f0 = f) by congruence. subst f0.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ exploit make_epilogue_correct; eauto. intros (rs1 & m1 & U & V & W & X & Y & Z).
+ exploit exec_straight_body; eauto.
+ simpl. eauto.
+ intros EXEB.
+ assert (f1 = f) by congruence. subst f1.
+
+ repeat eexists.
+ rewrite H6. simpl extract_basic. eauto.
+ rewrite H7. simpl extract_ctl. simpl. reflexivity.
+ econstructor; eauto.
+ unfold nextblock, incrPC. repeat apply agree_set_other; auto with asmgen.
+
+ - inv MCS. inv MAS. simpl in *. subst. inv Hpstate.
+ destruct bb' as [hd' bdy' ex']; simpl in *. subst.
+ monadInv TBC. monadInv TIC. simpl in *. rewrite H5. rewrite H6.
+ simpl. repeat eexists.
+ econstructor. 4: instantiate (3 := false). all:eauto.
+ unfold nextblock, incrPC. Simpl. unfold Val.offset_ptr. rewrite PCeq.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ assert (f = f0) by congruence. subst f0. econstructor; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV TAIL). intro CT1. eauto.
+ eapply agree_exten; eauto. intros. Simpl.
+ discriminate.
+Qed.
+
+Definition mb_remove_first (bb: MB.bblock) :=
+ {| MB.header := MB.header bb; MB.body := tail (MB.body bb); MB.exit := MB.exit bb |}.
+
+Lemma exec_straight_body:
+ forall c c' lc rs1 m1 rs2 m2,
+ exec_straight tge c rs1 m1 c' rs2 m2 ->
+ code_to_basics c = Some lc ->
+ exists l ll,
+ c = l ++ c'
+ /\ code_to_basics l = Some ll
+ /\ exec_body tge ll rs1 m1 = Next rs2 m2.
+Proof.
+ induction c; try (intros; inv H; fail).
+ intros until m2. intros EXES CTB. inv EXES.
+ - exists (i1 ::g nil),(i1::nil). repeat (split; simpl; auto). rewrite H6. auto.
+ - inv CTB. destruct (code_to_basics c); try discriminate. inv H0.
+ eapply IHc in H7; eauto. destruct H7 as (l' & ll & Hc & CTB & EXECB). subst.
+ exists (i ::g l'),(i::ll). repeat (split; simpl; auto).
+ rewrite CTB. auto.
+ rewrite H1. auto.
+Qed.
+
+Lemma basics_to_code_app:
+ forall c l x ll,
+ basics_to_code c = l ++ basics_to_code x ->
+ code_to_basics l = Some ll ->
+ c = ll ++ x.
+Proof.
+ intros. apply (f_equal code_to_basics) in H.
+ erewrite code_to_basics_dist in H; eauto. 2: eapply code_to_basics_id.
+ rewrite code_to_basics_id in H. inv H. auto.
+Qed.
+
+Lemma basics_to_code_app2:
+ forall i c l x ll,
+ (PBasic i) :: basics_to_code c = l ++ basics_to_code x ->
+ code_to_basics l = Some ll ->
+ i :: c = ll ++ x.
+Proof.
+ intros until ll. intros.
+ exploit basics_to_code_app. instantiate (3 := (i::c)). simpl.
+ all: eauto.
+Qed.
+
+(* Handling the individual instructions of theorem (B) in the above diagram. A bit less cumbersome, but still tough *)
+Theorem step_simu_basic:
+ forall bb bb' s fb sp c ms m rs1 m1 ms' m' bi cs1 tbdy bdy,
+ MB.header bb = nil -> MB.body bb = bi::(bdy) -> (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res)) ->
+ bb' = {| MB.header := nil; MB.body := bdy; MB.exit := MB.exit bb |} ->
+ basic_step ge s fb sp ms m bi ms' m' ->
+ pstate cs1 = (State rs1 m1) -> pbody1 cs1 = tbdy ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists rs2 m2 l cs2 tbdy',
+ cs2 = {| pstate := (State rs2 m2); pheader := nil; pbody1 := tbdy'; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := fp_is_parent (ep cs1) bi; rem := rem cs1; cur := cur cs1 |}
+ /\ tbdy = l ++ tbdy'
+ /\ exec_body tge l rs1 m1 = Next rs2 m2
+ /\ match_codestate fb (MB.State s fb sp (bb'::c) ms' m') cs2).
+Proof.
+ intros until bdy. intros Hheader Hbody Hnobuiltin (* Hnotempty *) Hbb' BSTEP Hpstate Hpbody1 MCS. inv MCS.
+ simpl in *. inv Hpstate.
+ rewrite Hbody in TBC. monadInv TBC.
+ inv BSTEP.
+
+ - (* MBgetstack *)
+ simpl in EQ0.
+ unfold Mach.load_stack in H.
+ exploit Mem.loadv_extends; eauto. intros [v' [A B]].
+ rewrite (sp_val _ _ _ AG) in A.
+ exploit loadind_correct; eauto with asmgen.
+ intros (rs2 & EXECS & Hrs'1 & Hrs'2).
+ eapply exec_straight_body in EXECS.
+ 2: eapply code_to_basics_id; eauto.
+ destruct EXECS as (l & Hlbi & BTC & CTB & EXECB).
+ exists rs2, m1, Hlbi.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. simpl. auto. }
+ subst. simpl in Hheadereq.
+
+ eapply match_codestate_intro; eauto.
+ { simpl. simpl in EQ. rewrite <- Hheadereq in EQ. assumption. }
+ eapply agree_set_mreg; eauto with asmgen.
+ intro Hep. simpl in Hep.
+ destruct (andb_prop _ _ Hep). clear Hep.
+ rewrite <- Hheadereq in DXP. subst. rewrite <- DXP. rewrite Hrs'2. reflexivity.
+ discriminate. apply preg_of_not_FP; assumption. reflexivity.
+
+ - (* MBsetstack *)
+ simpl in EQ0.
+ unfold Mach.store_stack in H.
+ assert (Val.lessdef (ms src) (rs1 (preg_of src))). { eapply preg_val; eauto. }
+ exploit Mem.storev_extends; eauto. intros [m2' [A B]].
+ exploit storeind_correct; eauto with asmgen.
+ rewrite (sp_val _ _ _ AG) in A. eauto. intros [rs' [P Q]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs', m2', ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ. rewrite Hheader in EQ. auto.
+
+ eapply agree_undef_regs; eauto with asmgen.
+ simpl; intros. rewrite Q; auto with asmgen. rewrite Hheader in DXP. auto.
+ - (* MBgetparam *)
+ simpl in EQ0.
+
+ assert (f0 = f) by congruence; subst f0.
+ unfold Mach.load_stack in *.
+ exploit Mem.loadv_extends. eauto. eexact H0. auto.
+ intros [parent' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit lessdef_parent_sp; eauto. clear B; intros B; subst parent'.
+ exploit Mem.loadv_extends. eauto. eexact H1. auto.
+ intros [v' [C D]].
+
+ monadInv EQ0. rewrite Hheader. rewrite Hheader in DXP.
+ destruct ep0 eqn:EPeq.
+
+ (* RTMP contains parent *)
+ + exploit loadind_correct. eexact EQ1.
+ instantiate (2 := rs1). rewrite DXP; eauto.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & BTC & CTB & EXECB).
+ exists rs2, m1, ll. eexists.
+ eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ { eapply basics_to_code_app; eauto. }
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. simpl. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+
+ eapply agree_set_mreg. eapply agree_set_mreg; eauto. congruence. auto with asmgen.
+ simpl; intros. rewrite R; auto with asmgen.
+ apply preg_of_not_FP; auto.
+
+ (* RTMP does not contain parent *)
+ + rewrite chunk_of_Tptr in A.
+ exploit loadind_ptr_correct. eexact A. intros [rs2 [P [Q R]]].
+ exploit loadind_correct. eexact EQ1. instantiate (2 := rs2). rewrite Q. eauto.
+ intros [rs3 [S [T U]]].
+
+ exploit exec_straight_trans.
+ eapply P.
+ eapply S.
+ intros EXES.
+
+ eapply exec_straight_body in EXES.
+ 2: simpl. 2: erewrite code_to_basics_id; eauto.
+ destruct EXES as (l & ll & BTC & CTB & EXECB).
+ exists rs3, m1, ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app2; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto.
+ eapply agree_set_mreg. eapply agree_set_mreg. eauto. eauto.
+ instantiate (1 := rs2#FP <- (rs3#FP)). intros.
+ rewrite Pregmap.gso; auto with asmgen.
+ congruence.
+ intros. unfold Pregmap.set. destruct (PregEq.eq r' FP). congruence. auto with asmgen.
+ simpl; intros. rewrite U; auto with asmgen.
+ apply preg_of_not_FP; auto.
+ - (* MBop *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (eval_operation tge sp op (map ms args) m' = Some v).
+ rewrite <- H. apply eval_operation_preserved. exact symbols_preserved.
+ exploit eval_operation_lessdef.
+ eapply preg_vals; eauto.
+ 2: eexact H0.
+ all: eauto.
+ intros [v' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit transl_op_correct; eauto. intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs2, m1, ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ. rewrite Hheader in EQ. auto.
+ apply agree_set_undef_mreg with rs1; auto.
+ apply Val.lessdef_trans with v'; auto.
+ simpl; intros. destruct (andb_prop _ _ H1); clear H1.
+ rewrite R; auto. apply preg_of_not_FP; auto.
+Local Transparent destroyed_by_op.
+ destruct op; simpl; auto; congruence.
+ - (* MBload *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (eval_addressing tge sp addr (map ms args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
+ intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ exploit Mem.loadv_extends; eauto. intros [v' [C D]].
+ exploit transl_load_correct; eauto.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs2, m1, ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ.
+ rewrite <- Hheadereq in EQ. assumption.
+ eapply agree_set_mreg; eauto with asmgen.
+ intro Hep. simpl in Hep.
+ destruct (andb_prop _ _ Hep). clear Hep.
+ subst. rewrite <- DXP. rewrite R; try discriminate. reflexivity.
+ apply preg_of_not_FP; assumption. reflexivity.
+
+ - (* notrap1 cannot happen *)
+ simpl in EQ0. unfold transl_load in EQ0.
+ destruct addr; simpl in H.
+ all: unfold transl_load_rrrXS, transl_load_rrr, transl_load_rro in EQ0;
+ monadInv EQ0; unfold transl_memory_access2XS, transl_memory_access2, transl_memory_access in EQ2;
+ destruct args as [|h0 t0]; try discriminate;
+ destruct t0 as [|h1 t1]; try discriminate;
+ destruct t1 as [|h2 t2]; try discriminate.
+
+ - (* MBload notrap2 TODO *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (eval_addressing tge sp addr (map ms args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
+ intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+
+ destruct (Mem.loadv chunk m1 a') as [v' | ] eqn:Hload.
+ {
+ exploit transl_load_correct; eauto.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs2, m1, ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ eapply match_codestate_intro; eauto. simpl. rewrite Hheader in *.
+ simpl in EQ. assumption.
+
+ eapply agree_set_undef_mreg; eauto. intros; auto with asmgen.
+
+ simpl. intro.
+ rewrite R; try congruence.
+ apply DXP.
+ destruct ep0; simpl in *; congruence.
+ apply preg_of_not_FP.
+ destruct ep0; simpl in *; congruence.
+ }
+ {
+ exploit transl_load_correct_notrap2; eauto.
+ intros [rs2 [P [Q R]]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs2, m1, ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+(* assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ rewrite <- Hheadereq. *) subst.
+ eapply match_codestate_intro; eauto. simpl. rewrite Hheader in *. simpl in EQ. assumption.
+
+ eapply agree_set_undef_mreg; eauto. intros; auto with asmgen.
+ simpl. intro.
+ rewrite R; try congruence.
+ apply DXP.
+ destruct ep0; simpl in *; congruence.
+ apply preg_of_not_FP.
+ destruct ep0; simpl in *; congruence.
+ }
+ - (* MBstore *)
+ simpl in EQ0. rewrite Hheader in DXP.
+
+ assert (eval_addressing tge sp addr (map ms args) = Some a).
+ rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
+ exploit eval_addressing_lessdef. eapply preg_vals; eauto. eexact H1.
+ intros [a' [A B]]. rewrite (sp_val _ _ _ AG) in A.
+ assert (Val.lessdef (ms src) (rs1 (preg_of src))). eapply preg_val; eauto.
+ exploit Mem.storev_extends; eauto. intros [m2' [C D]].
+ exploit transl_store_correct; eauto. intros [rs2 [P Q]].
+
+ eapply exec_straight_body in P.
+ 2: eapply code_to_basics_id; eauto.
+ destruct P as (l & ll & TBC & CTB & EXECB).
+ exists rs2, m2', ll.
+ eexists. eexists. split. instantiate (1 := x). eauto.
+ repeat (split; auto).
+ eapply basics_to_code_app; eauto.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb'.
+ assert (Hheadereq: MB.header bb' = MB.header bb). { subst. auto. }
+ subst.
+ eapply match_codestate_intro; eauto. simpl. simpl in EQ.
+ rewrite <- Hheadereq in EQ. assumption.
+ eapply agree_undef_regs; eauto with asmgen.
+ intro Hep. simpl in Hep.
+ subst. rewrite <- DXP. rewrite Q; try discriminate. reflexivity. reflexivity.
+Qed.
+
+Lemma exec_body_trans:
+ forall l l' rs0 m0 rs1 m1 rs2 m2,
+ exec_body tge l rs0 m0 = Next rs1 m1 ->
+ exec_body tge l' rs1 m1 = Next rs2 m2 ->
+ exec_body tge (l++l') rs0 m0 = Next rs2 m2.
+Proof.
+ induction l.
+ - simpl. congruence.
+ - intros until m2. intros EXEB1 EXEB2.
+ inv EXEB1. destruct (exec_basic_instr _) eqn:EBI; try discriminate.
+ simpl. rewrite EBI. eapply IHl; eauto.
+Qed.
+
+Definition mb_remove_header bb := {| MB.header := nil; MB.body := MB.body bb; MB.exit := MB.exit bb |}.
+
+Program Definition remove_header tbb := {| header := nil; body := body tbb; exit := exit tbb |}.
+Next Obligation.
+ destruct tbb. simpl. auto.
+Qed.
+
+Inductive exec_header: codestate -> codestate -> Prop :=
+ | exec_header_cons: forall cs1,
+ exec_header cs1 {| pstate := pstate cs1; pheader := nil; pbody1 := pbody1 cs1; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := (if pheader cs1 then ep cs1 else false); rem := rem cs1;
+ cur := cur cs1 |}.
+
+(* Theorem (A) in the diagram, the easiest of all *)
+Theorem step_simu_header:
+ forall bb s fb sp c ms m rs1 m1 cs1,
+ pstate cs1 = (State rs1 m1) ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists cs1',
+ exec_header cs1 cs1'
+ /\ match_codestate fb (MB.State s fb sp (mb_remove_header bb::c) ms m) cs1').
+Proof.
+ intros until cs1. intros Hpstate MCS.
+ eexists. split; eauto.
+ econstructor; eauto.
+ inv MCS. simpl in *. inv Hpstate.
+ econstructor; eauto.
+Qed.
+
+Lemma step_matchasm_header:
+ forall fb cs1 cs1' s1,
+ match_asmstate fb cs1 s1 ->
+ exec_header cs1 cs1' ->
+ match_asmstate fb cs1' s1.
+Proof.
+ intros until s1. intros MAS EXH.
+ inv MAS. inv EXH.
+ simpl. econstructor; eauto.
+Qed.
+
+(* Theorem (B) in the diagram, using step_simu_basic + induction on the Machblock body *)
+Theorem step_simu_body:
+ forall bb s fb sp c ms m rs1 m1 ms' cs1 m',
+ MB.header bb = nil ->
+ (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res)) ->
+ body_step ge s fb sp (MB.body bb) ms m ms' m' ->
+ pstate cs1 = (State rs1 m1) ->
+ match_codestate fb (MB.State s fb sp (bb::c) ms m) cs1 ->
+ (exists rs2 m2 cs2 ep,
+ cs2 = {| pstate := (State rs2 m2); pheader := nil; pbody1 := nil; pbody2 := pbody2 cs1;
+ pctl := pctl cs1; ep := ep; rem := rem cs1; cur := cur cs1 |}
+ /\ exec_body tge (pbody1 cs1) rs1 m1 = Next rs2 m2
+ /\ match_codestate fb (MB.State s fb sp ({| MB.header := nil; MB.body := nil; MB.exit := MB.exit bb |}::c) ms' m') cs2).
+Proof.
+ intros bb. destruct bb as [hd bdy ex]; simpl; auto. induction bdy as [|bi bdy].
+ - intros until m'. intros Hheader Hnobuiltin BSTEP Hpstate MCS.
+ inv BSTEP.
+ exists rs1, m1, cs1, (ep cs1).
+ inv MCS. inv Hpstate. simpl in *. monadInv TBC. repeat (split; simpl; auto).
+ econstructor; eauto.
+ - intros until m'. intros Hheader Hnobuiltin BSTEP Hpstate MCS. inv BSTEP.
+ rename ms' into ms''. rename m' into m''. rename rs' into ms'. rename m'0 into m'.
+ exploit (step_simu_basic); eauto. simpl. eauto. simpl; auto. simpl; auto.
+ intros (rs2 & m2 & l & cs2 & tbdy' & Hcs2 & Happ & EXEB & MCS').
+ simpl in *.
+ exploit IHbdy. auto. 2: eapply H6. 3: eapply MCS'. all: eauto. subst; eauto. simpl; auto.
+ intros (rs3 & m3 & cs3 & ep & Hcs3 & EXEB' & MCS'').
+ exists rs3, m3, cs3, ep.
+ repeat (split; simpl; auto). subst. simpl in *. auto.
+ rewrite Happ. eapply exec_body_trans; eauto. rewrite Hcs2 in EXEB'; simpl in EXEB'. auto.
+Qed.
+
+Lemma exec_body_control:
+ forall b rs1 m1 rs2 m2 rs3 m3 fn,
+ exec_body tge (body b) rs1 m1 = Next rs2 m2 ->
+ exec_control_rel tge fn (exit b) b rs2 m2 rs3 m3 ->
+ exec_bblock_rel tge fn b rs1 m1 rs3 m3.
+Proof.
+ intros until fn. intros EXEB EXECTL.
+ econstructor; eauto. inv EXECTL.
+ unfold exec_bblock. rewrite EXEB. auto.
+Qed.
+
+Definition mbsize (bb: MB.bblock) := (length (MB.body bb) + length_opt (MB.exit bb))%nat.
+
+Lemma mbsize_eqz:
+ forall bb, mbsize bb = 0%nat -> MB.body bb = nil /\ MB.exit bb = None.
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *. unfold mbsize in H.
+ remember (length _) as a. remember (length_opt _) as b.
+ assert (a = 0%nat) by lia. assert (b = 0%nat) by lia. subst. clear H.
+ inv H0. inv H1. destruct bdy; destruct ex; auto.
+ all: try discriminate.
+Qed.
+
+Lemma mbsize_neqz:
+ forall bb, mbsize bb <> 0%nat -> (MB.body bb <> nil \/ MB.exit bb <> None).
+Proof.
+ intros. destruct bb as [hd bdy ex]; simpl in *.
+ destruct bdy; destruct ex; try (right; discriminate); try (left; discriminate).
+ contradict H. unfold mbsize. simpl. auto.
+Qed.
+
+(* Bringing theorems (A), (B) and (C) together, for the case of the absence of builtin instruction *)
+(* This more general form is easier to prove, but the actual theorem is step_simulation_bblock further below *)
+Lemma step_simulation_bblock':
+ forall sf f sp bb bb' bb'' rs m rs' m' s'' c S1,
+ bb' = mb_remove_header bb ->
+ body_step ge sf f sp (Machblock.body bb') rs m rs' m' ->
+ bb'' = mb_remove_body bb' ->
+ (forall ef args res, MB.exit bb'' <> Some (MBbuiltin ef args res)) ->
+ exit_step return_address_offset ge (Machblock.exit bb'') (Machblock.State sf f sp (bb'' :: c) rs' m') E0 s'' ->
+ match_states (Machblock.State sf f sp (bb :: c) rs m) S1 ->
+ exists S2 : state, plus step tge S1 E0 S2 /\ match_states s'' S2.
+Proof.
+ intros until S1. intros Hbb' BSTEP Hbb'' Hbuiltin ESTEP MS.
+ destruct (mbsize bb) eqn:SIZE.
+ - apply mbsize_eqz in SIZE. destruct SIZE as (Hbody & Hexit).
+ destruct bb as [hd bdy ex]; simpl in *; subst.
+ inv MS. inv AT. exploit transl_blocks_nonil; eauto. intros (tbb & tc' & Htc). subst. rename tc' into tc.
+ monadInv H2. simpl in *. inv ESTEP. inv BSTEP.
+ eexists. split. eapply plus_one.
+ exploit functions_translated; eauto. intros (tf0 & FIND' & TRANSF'). monadInv TRANSF'.
+ assert (x = tf) by congruence. subst x.
+ eapply exec_step_internal; eauto. eapply find_bblock_tail; eauto.
+ unfold exec_bblock. simpl. eauto.
+ econstructor. eauto. eauto. eauto.
+ unfold nextblock, incrPC. Simpl. unfold Val.offset_ptr. rewrite <- H.
+ assert (NOOV: size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+ econstructor; eauto.
+ generalize (code_tail_next_int _ _ _ _ NOOV H3). intro CT1. eauto.
+ eapply agree_exten; eauto. intros. Simpl.
+ intros. discriminate.
+ - subst. exploit mbsize_neqz. { instantiate (1 := bb). rewrite SIZE. discriminate. }
+ intros Hnotempty.
+
+ (* initial setting *)
+ exploit match_state_codestate.
+ 2: eapply Hnotempty.
+ all: eauto.
+ intros (cs1 & fb & f0 & tbb & tc & ep & MCS & MAS & FIND & TLBS & Hbody & Hexit & Hcur & Hrem & Hpstate).
+
+ (* step_simu_header part *)
+ assert (exists rs1 m1, pstate cs1 = State rs1 m1). { inv MAS. simpl. eauto. }
+ destruct H as (rs1 & m1 & Hpstate2). subst.
+ assert (f = fb). { inv MCS. auto. } subst fb.
+ exploit step_simu_header.
+ 2: eapply MCS.
+ all: eauto.
+ intros (cs1' & EXEH & MCS2).
+
+ (* step_simu_body part *)
+ assert (Hpstate': pstate cs1' = pstate cs1). { inv EXEH; auto. }
+ exploit step_simu_body.
+ 3: eapply BSTEP.
+ 4: eapply MCS2.
+ all: eauto. rewrite Hpstate'. eauto.
+ intros (rs2 & m2 & cs2 & ep' & Hcs2 & EXEB & MCS').
+
+ (* step_simu_control part *)
+ assert (exists tf, Genv.find_funct_ptr tge f = Some (Internal tf)).
+ { exploit functions_translated; eauto. intros (tf & FIND' & TRANSF'). monadInv TRANSF'. eauto. }
+ destruct H as (tf & FIND').
+ assert (exists tex, pbody2 cs1 = extract_basic tex /\ pctl cs1 = extract_ctl tex).
+ { inv MAS. simpl in *. eauto. }
+ destruct H as (tex & Hpbody2 & Hpctl).
+ inv EXEH. simpl in *.
+ subst. exploit step_simu_control.
+ 9: eapply MCS'. all: simpl.
+ 10: eapply ESTEP.
+ all: simpl; eauto.
+ rewrite Hpbody2. rewrite Hpctl.
+ { inv MAS; simpl in *. inv Hpstate2. eapply match_asmstate_some; eauto.
+ erewrite exec_body_pc; eauto. }
+ intros (rs3 & m3 & rs4 & m4 & EXEB' & EXECTL' & MS').
+
+ (* bringing the pieces together *)
+ exploit exec_body_trans.
+ eapply EXEB.
+ eauto.
+ intros EXEB2.
+ exploit exec_body_control; eauto.
+ rewrite <- Hpbody2 in EXEB2. rewrite <- Hbody in EXEB2. eauto.
+ rewrite Hexit. rewrite Hpctl. eauto.
+ intros EXECB. inv EXECB.
+ exists (State rs4 m4).
+ split; auto. eapply plus_one. rewrite Hpstate2.
+ assert (exists ofs, rs1 PC = Vptr f ofs).
+ { rewrite Hpstate2 in MAS. inv MAS. simpl in *. eauto. }
+ destruct H0 as (ofs & Hrs1pc).
+ eapply exec_step_internal; eauto.
+
+ (* proving the initial find_bblock *)
+ rewrite Hpstate2 in MAS. inv MAS. simpl in *.
+ assert (f1 = f0) by congruence. subst f0.
+ rewrite PCeq in Hrs1pc. inv Hrs1pc.
+ exploit functions_translated; eauto. intros (tf1 & FIND'' & TRANS''). rewrite FIND' in FIND''.
+ inv FIND''. monadInv TRANS''. rewrite TRANSF0 in EQ. inv EQ.
+ eapply find_bblock_tail; eauto.
+Qed.
+
+Theorem step_simulation_bblock:
+ forall sf f sp bb ms m ms' m' S2 c,
+ body_step ge sf f sp (Machblock.body bb) ms m ms' m' ->
+ (forall ef args res, MB.exit bb <> Some (MBbuiltin ef args res)) ->
+ exit_step return_address_offset ge (Machblock.exit bb) (Machblock.State sf f sp (bb :: c) ms' m') E0 S2 ->
+ forall S1', match_states (Machblock.State sf f sp (bb :: c) ms m) S1' ->
+ exists S2' : state, plus step tge S1' E0 S2' /\ match_states S2 S2'.
+Proof.
+ intros until c. intros BSTEP Hbuiltin ESTEP S1' MS.
+ eapply step_simulation_bblock'; eauto.
+ all: destruct bb as [hd bdy ex]; simpl in *; eauto.
+ inv ESTEP.
+ - econstructor. inv H; try (econstructor; eauto; fail).
+ - econstructor.
+Qed.
+
+(** Dealing now with the builtin case *)
+
+Definition split (c: MB.code) :=
+ match c with
+ | nil => nil
+ | bb::c => {| MB.header := MB.header bb; MB.body := MB.body bb; MB.exit := None |}
+ :: {| MB.header := nil; MB.body := nil; MB.exit := MB.exit bb |} :: c
+ end.
+
+Lemma cons_ok_eq3 {A: Type} :
+ forall (x:A) y z x' y' z',
+ x = x' -> y = y' -> z = z' ->
+ OK (x::y::z) = OK (x'::y'::z').
+Proof.
+ intros. subst. auto.
+Qed.
+
+Lemma transl_blocks_split_builtin:
+ forall bb c ep f ef args res,
+ MB.exit bb = Some (MBbuiltin ef args res) -> MB.body bb <> nil ->
+ transl_blocks f (split (bb::c)) ep = transl_blocks f (bb::c) ep.
+Proof.
+ intros until res. intros Hexit Hbody. simpl split.
+ unfold transl_blocks. fold transl_blocks. unfold transl_block.
+ simpl. remember (transl_basic_code _ _ _) as tbc. remember (transl_instr_control _ _) as tbi.
+ remember (transl_blocks _ _ _) as tlbs.
+ destruct tbc; destruct tbi; destruct tlbs.
+ all: try simpl; auto.
+ - simpl. rewrite Hexit in Heqtbi. simpl in Heqtbi. monadInv Heqtbi. simpl.
+ unfold gen_bblocks. simpl. destruct l.
+ + exploit transl_basic_code_nonil; eauto. intro. destruct H.
+ + simpl. rewrite app_nil_r. apply cons_ok_eq3. all: try eapply bblock_equality. all: simpl; auto.
+Qed.
+
+Lemma transl_code_at_pc_split_builtin:
+ forall rs f f0 bb c ep tf tc ef args res,
+ MB.body bb <> nil -> MB.exit bb = Some (MBbuiltin ef args res) ->
+ transl_code_at_pc ge (rs PC) f f0 (bb :: c) ep tf tc ->
+ transl_code_at_pc ge (rs PC) f f0 (split (bb :: c)) ep tf tc.
+Proof.
+ intros until res. intros Hbody Hexit AT. inv AT.
+ econstructor; eauto. erewrite transl_blocks_split_builtin; eauto.
+Qed.
+
+Theorem match_states_split_builtin:
+ forall sf f sp bb c rs m ef args res S1,
+ MB.body bb <> nil -> MB.exit bb = Some (MBbuiltin ef args res) ->
+ match_states (Machblock.State sf f sp (bb :: c) rs m) S1 ->
+ match_states (Machblock.State sf f sp (split (bb::c)) rs m) S1.
+Proof.
+ intros until S1. intros Hbody Hexit MS.
+ inv MS.
+ econstructor; eauto.
+ eapply transl_code_at_pc_split_builtin; eauto.
+Qed.
+
+Theorem step_simulation_builtin:
+ forall ef args res bb sf f sp c ms m t S2,
+ MB.body bb = nil -> MB.exit bb = Some (MBbuiltin ef args res) ->
+ exit_step return_address_offset ge (MB.exit bb) (Machblock.State sf f sp (bb :: c) ms m) t S2 ->
+ forall S1', match_states (Machblock.State sf f sp (bb :: c) ms m) S1' ->
+ exists S2' : state, plus step tge S1' t S2' /\ match_states S2 S2'.
+Proof.
+ intros until S2. intros Hbody Hexit ESTEP S1' MS.
+ inv MS. inv AT. monadInv H2. monadInv EQ.
+ rewrite Hbody in EQ0. monadInv EQ0.
+ rewrite Hexit in EQ. monadInv EQ.
+ rewrite Hexit in ESTEP. inv ESTEP. inv H4.
+
+ exploit functions_transl; eauto. intro FN.
+ generalize (transf_function_no_overflow _ _ H1); intro NOOV.
+ exploit builtin_args_match; eauto. intros [vargs' [P Q]].
+ exploit external_call_mem_extends; eauto.
+ intros [vres' [m2' [A [B [C D]]]]].
+ econstructor; split. apply plus_one.
+ simpl in H3.
+ eapply exec_step_builtin. eauto. eauto.
+ eapply find_bblock_tail; eauto.
+ simpl. eauto.
+ erewrite <- sp_val by eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ eauto.
+ econstructor; eauto.
+ instantiate (2 := tf); instantiate (1 := x0).
+ unfold nextblock, incrPC. rewrite Pregmap.gss.
+ rewrite set_res_other. rewrite undef_regs_other_2. rewrite Pregmap.gso by congruence.
+ rewrite <- H. simpl. econstructor; eauto.
+ eapply code_tail_next_int; eauto.
+ rewrite preg_notin_charact. intros. auto with asmgen.
+ auto with asmgen.
+ apply agree_nextblock. eapply agree_set_res; auto.
+ eapply agree_undef_regs; eauto. intros. rewrite undef_regs_other_2; auto.
+ apply Pregmap.gso; auto with asmgen.
+ congruence.
+Qed.
+
+Lemma next_sep:
+ forall rs m rs' m', rs = rs' -> m = m' -> Next rs m = Next rs' m'.
+Proof.
+ congruence.
+Qed.
+
+(* Measure to prove finite stuttering, see the other backends *)
+Definition measure (s: MB.state) : nat :=
+ match s with
+ | MB.State _ _ _ _ _ _ => 0%nat
+ | MB.Callstate _ _ _ _ => 0%nat
+ | MB.Returnstate _ _ _ => 1%nat
+ end.
+
+(* The actual MB.step/AB.step simulation, using the above theorems, plus extra proofs
+ for the internal and external function cases *)
+Theorem step_simulation:
+ forall S1 t S2, MB.step return_address_offset ge S1 t S2 ->
+ forall S1' (MS: match_states S1 S1'),
+ (exists S2', plus step tge S1' t S2' /\ match_states S2 S2')
+ \/ (measure S2 < measure S1 /\ t = E0 /\ match_states S2 S1')%nat.
+Proof.
+ induction 1; intros.
+
+- (* bblock *)
+ left. destruct (Machblock.exit bb) eqn:MBE; try destruct c0.
+ all: try(inversion H0; subst; inv H2; eapply step_simulation_bblock;
+ try (rewrite MBE; try discriminate); eauto).
+ + (* MBbuiltin *)
+ destruct (MB.body bb) eqn:MBB.
+ * inv H. eapply step_simulation_builtin; eauto. rewrite MBE. eauto.
+ * eapply match_states_split_builtin in MS; eauto.
+ 2: rewrite MBB; discriminate.
+ simpl split in MS.
+ rewrite <- MBB in H.
+ remember {| MB.header := _; MB.body := _; MB.exit := _ |} as bb1.
+ assert (MB.body bb = MB.body bb1). { subst. simpl. auto. }
+ rewrite H1 in H. subst.
+ exploit step_simulation_bblock. eapply H.
+ discriminate.
+ simpl. constructor.
+ eauto.
+ intros (S2' & PLUS1 & MS').
+ rewrite MBE in MS'.
+ assert (exit_step return_address_offset ge (Some (MBbuiltin e l b))
+ (MB.State sf f sp ({| MB.header := nil; MB.body := nil; MB.exit := Some (MBbuiltin e l b) |}::c)
+ rs' m') t s').
+ { inv H0. inv H3. econstructor. econstructor; eauto. }
+ exploit step_simulation_builtin.
+ 4: eapply MS'.
+ all: simpl; eauto.
+ intros (S3' & PLUS'' & MS'').
+ exists S3'. split; eauto.
+ eapply plus_trans. eapply PLUS1. eapply PLUS''. eauto.
+ + inversion H0. subst. eapply step_simulation_bblock; try (rewrite MBE; try discriminate); eauto.
+
+- (* internal function *)
+ inv MS.
+ exploit functions_translated; eauto. intros [tf [A B]]. monadInv B.
+ generalize EQ; intros EQ'. monadInv EQ'.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks x0.(fn_blocks))); inversion EQ1. clear EQ1. subst x0.
+ unfold Mach.store_stack in *.
+ exploit Mem.alloc_extends. eauto. eauto. apply Z.le_refl. apply Z.le_refl.
+ intros [m1' [C D]].
+ exploit Mem.storev_extends. eexact D. eexact H1. eauto. eauto.
+ intros [m2' [F G]].
+ simpl chunk_of_type in F.
+ exploit Mem.storev_extends. eexact G. eexact H2. eauto. eauto.
+ intros [m3' [P Q]].
+ (* Execution of function prologue *)
+ monadInv EQ0.
+ set (tfbody := make_prologue f x0) in *.
+ set (tf := {| fn_sig := MB.fn_sig f; fn_blocks := tfbody |}) in *.
+ set (rs2 := rs0#FP <- (parent_sp s) #SP <- sp #RTMP <- Vundef).
+ exploit (Pget_correct tge GPRA RA nil rs2 m2'); auto.
+ intros (rs' & U' & V').
+ exploit (storeind_ptr_correct tge SP (fn_retaddr_ofs f) GPRA nil rs' m2').
+ { rewrite chunk_of_Tptr in P.
+ assert (rs' GPRA = rs0 RA). { apply V'. }
+ assert (rs' SP = rs2 SP). { apply V'; discriminate. }
+ rewrite H4. rewrite H3.
+ rewrite ATLR.
+ change (rs2 SP) with sp. eexact P. }
+ intros (rs3 & U & V).
+ assert (EXEC_PROLOGUE: exists rs3',
+ exec_straight_blocks tge tf
+ tf.(fn_blocks) rs0 m'
+ x0 rs3' m3'
+ /\ forall r, r <> PC -> rs3' r = rs3 r).
+ { eexists. split.
+ - change (fn_blocks tf) with tfbody; unfold tfbody.
+ econstructor; eauto. unfold exec_bblock. simpl exec_body.
+ rewrite C. fold sp. rewrite <- (sp_val _ _ _ AG). rewrite chunk_of_Tptr in F. simpl in F. rewrite F.
+ Simpl. unfold parexec_store_offset. rewrite Ptrofs.of_int64_to_int64. unfold eval_offset.
+ rewrite chunk_of_Tptr in P. Simpl. rewrite ATLR. unfold Mptr in P. assert (Archi.ptr64 = true) by auto. 2: auto. rewrite H3 in P. rewrite P.
+ simpl. apply next_sep; eauto. reflexivity.
+ - intros. destruct V' as (V'' & V'). destruct r.
+ + Simpl.
+ destruct (gpreg_eq g0 GPR16). { subst. Simpl. rewrite V; try discriminate. rewrite V''. subst rs2. Simpl. }
+ destruct (gpreg_eq g0 GPR32). { subst. Simpl. rewrite V; try discriminate. rewrite V'; try discriminate. subst rs2. Simpl. }
+ destruct (gpreg_eq g0 GPR12). { subst. Simpl. rewrite V; try discriminate. rewrite V'; try discriminate. subst rs2. Simpl. }
+ destruct (gpreg_eq g0 GPR17). { subst. Simpl. rewrite V; try discriminate. rewrite V'; try discriminate. subst rs2. Simpl. }
+ Simpl. rewrite V; try discriminate. rewrite V'; try discriminate. subst rs2. Simpl. { destruct g0; try discriminate. contradiction. }
+ + Simpl. rewrite V; try discriminate. rewrite V'; try discriminate. subst rs2. Simpl.
+ + contradiction.
+ } destruct EXEC_PROLOGUE as (rs3' & EXEC_PROLOGUE & Heqrs3').
+ exploit exec_straight_steps_2; eauto using functions_transl.
+ simpl fn_blocks. simpl fn_blocks in g. lia. constructor.
+ intros (ofs' & X & Y).
+ left; exists (State rs3' m3'); split.
+ eapply exec_straight_steps_1; eauto.
+ simpl fn_blocks. simpl fn_blocks in g. lia.
+ constructor.
+ econstructor; eauto.
+ rewrite X; econstructor; eauto.
+ apply agree_exten with rs2; eauto with asmgen.
+ unfold rs2.
+ apply agree_set_other; auto with asmgen.
+ apply agree_change_sp with (parent_sp s).
+ apply agree_undef_regs with rs0. auto.
+Local Transparent destroyed_at_function_entry.
+ simpl; intros; Simpl.
+ unfold sp; congruence.
+
+ intros.
+ assert (r <> RTMP). { contradict H3; rewrite H3; unfold data_preg; auto. }
+ rewrite Heqrs3'. Simpl. rewrite V. inversion V'. rewrite H6. auto.
+ assert (r <> GPRA). { contradict H3; rewrite H3; unfold data_preg; auto. }
+ assert (forall r : preg, r <> PC -> r <> GPRA -> rs' r = rs2 r). { apply V'. }
+ contradict H3; rewrite H3; unfold data_preg; auto.
+ contradict H3; rewrite H3; unfold data_preg; auto.
+ contradict H3; rewrite H3; unfold data_preg; auto.
+ contradict H3; rewrite H3; unfold data_preg; auto.
+ intros. rewrite Heqrs3'. rewrite V by auto with asmgen.
+ assert (forall r : preg, r <> PC -> r <> GPRA -> rs' r = rs2 r). { apply V'. }
+ rewrite H4 by auto with asmgen. reflexivity. discriminate.
+
+- (* external function *)
+ inv MS.
+ exploit functions_translated; eauto.
+ intros [tf [A B]]. simpl in B. inv B.
+ exploit extcall_arguments_match; eauto.
+ intros [args' [C D]].
+ exploit external_call_mem_extends; eauto.
+ intros [res' [m2' [P [Q [R S]]]]].
+ left; econstructor; split.
+ apply plus_one. eapply exec_step_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ econstructor; eauto.
+ unfold loc_external_result.
+ apply agree_set_other; auto.
+ apply agree_set_pair; auto.
+ apply agree_undef_caller_save_regs; auto.
+
+- (* return *)
+ inv MS.
+ inv STACKS. simpl in *.
+ right. split. lia. split. auto.
+ rewrite <- ATPC in H5.
+ econstructor; eauto. congruence.
+Qed.
+
+Lemma transf_initial_states:
+ forall st1, MB.initial_state prog st1 ->
+ exists st2, AB.initial_state tprog st2 /\ match_states st1 st2.
+Proof.
+ intros. inversion H. unfold ge0 in *.
+ econstructor; split.
+ econstructor.
+ eapply (Genv.init_mem_transf_partial TRANSF); eauto.
+ replace (Genv.symbol_address (Genv.globalenv tprog) (prog_main tprog) Ptrofs.zero)
+ with (Vptr fb Ptrofs.zero).
+ econstructor; eauto.
+ constructor.
+ apply Mem.extends_refl.
+ split. auto. simpl. unfold Vnullptr; destruct Archi.ptr64; congruence.
+ intros. rewrite Mach.Regmap.gi. auto.
+ unfold Genv.symbol_address.
+ rewrite (match_program_main TRANSF).
+ rewrite symbols_preserved.
+ unfold ge; rewrite H1. auto.
+Qed.
+
+Lemma transf_final_states:
+ forall st1 st2 r,
+ match_states st1 st2 -> MB.final_state st1 r -> AB.final_state st2 r.
+Proof.
+ intros. inv H0. inv H. constructor. assumption.
+ compute in H1. inv H1.
+ generalize (preg_val _ _ _ R0 AG). rewrite H2. intros LD; inv LD. auto.
+Qed.
+
+Definition return_address_offset : Machblock.function -> Machblock.code -> ptrofs -> Prop :=
+ Asmblockgenproof0.return_address_offset.
+
+Theorem transf_program_correct:
+ forward_simulation (MB.semantics return_address_offset prog) (Asmblock.semantics tprog).
+Proof.
+ eapply forward_simulation_star with (measure := measure).
+ - apply senv_preserved.
+ - eexact transf_initial_states.
+ - eexact transf_final_states.
+ - exact step_simulation.
+Qed.
+
+End PRESERVATION.
diff --git a/kvx/Asmblockgenproof0.v b/kvx/Asmblockgenproof0.v
new file mode 100644
index 00000000..83b574e7
--- /dev/null
+++ b/kvx/Asmblockgenproof0.v
@@ -0,0 +1,982 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * "block" version of Asmgenproof0
+
+ This module is largely adapted from Asmgenproof0.v of the other backends
+ It needs to stand apart because of the block structure, and the distinction control/basic that there isn't in the other backends
+ It has similar definitions than Asmgenproof0, but adapted to this new structure *)
+
+Require Import Coqlib.
+Require Intv.
+Require Import AST.
+Require Import Errors.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Events.
+Require Import Smallstep.
+Require Import Locations.
+Require Import Machblock.
+Require Import Asmblock.
+Require Import Asmblockgen.
+Require Import Conventions1.
+Require Import Axioms.
+Require Import Asmblockprops.
+Require Import Lia.
+
+Module MB:=Machblock.
+Module AB:=Asmblock.
+
+Lemma ireg_of_eq:
+ forall r r', ireg_of r = OK r' -> preg_of r = IR r'.
+Proof.
+ unfold ireg_of; intros. destruct (preg_of r); inv H; auto.
+Qed.
+
+Lemma freg_of_eq:
+ forall r r', freg_of r = OK r' -> preg_of r = IR r'.
+Proof.
+ unfold freg_of; intros. destruct (preg_of r); inv H; auto.
+Qed.
+
+Lemma preg_of_injective:
+ forall r1 r2, preg_of r1 = preg_of r2 -> r1 = r2.
+Proof.
+ destruct r1; destruct r2; simpl; intros; reflexivity || discriminate.
+Qed.
+
+Lemma undef_regs_other:
+ forall r rl rs,
+ (forall r', In r' rl -> r <> r') ->
+ undef_regs rl rs r = rs r.
+Proof.
+ induction rl; simpl; intros. auto.
+ rewrite IHrl by auto. rewrite Pregmap.gso; auto.
+Qed.
+
+Fixpoint preg_notin (r: preg) (rl: list mreg) : Prop :=
+ match rl with
+ | nil => True
+ | r1 :: nil => r <> preg_of r1
+ | r1 :: rl => r <> preg_of r1 /\ preg_notin r rl
+ end.
+
+Remark preg_notin_charact:
+ forall r rl,
+ preg_notin r rl <-> (forall mr, In mr rl -> r <> preg_of mr).
+Proof.
+ induction rl; simpl; intros.
+ tauto.
+ destruct rl.
+ simpl. split. intros. intuition congruence. auto.
+ rewrite IHrl. split.
+ intros [A B]. intros. destruct H. congruence. auto.
+ auto.
+Qed.
+
+Lemma undef_regs_other_2:
+ forall r rl rs,
+ preg_notin r rl ->
+ undef_regs (map preg_of rl) rs r = rs r.
+Proof.
+ intros. apply undef_regs_other. intros.
+ exploit list_in_map_inv; eauto. intros [mr [A B]]. subst.
+ rewrite preg_notin_charact in H. auto.
+Qed.
+
+(** * Agreement between Mach registers and processor registers *)
+
+Record agree (ms: Mach.regset) (sp: val) (rs: AB.regset) : Prop := mkagree {
+ agree_sp: rs#SP = sp;
+ agree_sp_def: sp <> Vundef;
+ agree_mregs: forall r: mreg, Val.lessdef (ms r) (rs#(preg_of r))
+}.
+
+Lemma preg_val:
+ forall ms sp rs r, agree ms sp rs -> Val.lessdef (ms r) rs#(preg_of r).
+Proof.
+ intros. destruct H. auto.
+Qed.
+
+Lemma preg_vals:
+ forall ms sp rs, agree ms sp rs ->
+ forall l, Val.lessdef_list (map ms l) (map rs (map preg_of l)).
+Proof.
+ induction l; simpl. constructor. constructor. eapply preg_val; eauto. auto.
+Qed.
+
+Lemma sp_val:
+ forall ms sp rs, agree ms sp rs -> sp = rs#SP.
+Proof.
+ intros. destruct H; auto.
+Qed.
+
+Lemma ireg_val:
+ forall ms sp rs r r',
+ agree ms sp rs ->
+ ireg_of r = OK r' ->
+ Val.lessdef (ms r) rs#r'.
+Proof.
+ intros. rewrite <- (ireg_of_eq _ _ H0). eapply preg_val; eauto.
+Qed.
+
+Lemma freg_val:
+ forall ms sp rs r r',
+ agree ms sp rs ->
+ freg_of r = OK r' ->
+ Val.lessdef (ms r) (rs#r').
+Proof.
+ intros. rewrite <- (freg_of_eq _ _ H0). eapply preg_val; eauto.
+Qed.
+
+Lemma agree_exten:
+ forall ms sp rs rs',
+ agree ms sp rs ->
+ (forall r, data_preg r = true -> rs'#r = rs#r) ->
+ agree ms sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite H0; auto. auto.
+ intros. rewrite H0; auto. apply preg_of_data.
+Qed.
+
+(** Preservation of register agreement under various assignments. *)
+
+Lemma agree_set_mreg:
+ forall ms sp rs r v rs',
+ agree ms sp rs ->
+ Val.lessdef v (rs'#(preg_of r)) ->
+ (forall r', data_preg r' = true -> r' <> preg_of r -> rs'#r' = rs#r') ->
+ agree (Mach.Regmap.set r v ms) sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite H1; auto. apply not_eq_sym. apply preg_of_not_SP.
+ intros. unfold Mach.Regmap.set. destruct (Mach.RegEq.eq r0 r). congruence.
+ rewrite H1. auto. apply preg_of_data.
+ red; intros; elim n. eapply preg_of_injective; eauto.
+Qed.
+
+Corollary agree_set_mreg_parallel:
+ forall ms sp rs r v v',
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.Regmap.set r v ms) sp (Pregmap.set (preg_of r) v' rs).
+Proof.
+ intros. eapply agree_set_mreg; eauto. rewrite Pregmap.gss; auto. intros; apply Pregmap.gso; auto.
+Qed.
+
+Lemma agree_set_other:
+ forall ms sp rs r v,
+ agree ms sp rs ->
+ data_preg r = false ->
+ agree ms sp (rs#r <- v).
+Proof.
+ intros. apply agree_exten with rs. auto.
+ intros. apply Pregmap.gso. congruence.
+Qed.
+
+Lemma agree_nextblock:
+ forall ms sp rs b,
+ agree ms sp rs -> agree ms sp (nextblock b rs).
+Proof.
+ intros. unfold nextblock. apply agree_set_other. auto. auto.
+Qed.
+
+Lemma agree_set_pair:
+ forall sp p v v' ms rs,
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.set_pair p v ms) sp (set_pair (map_rpair preg_of p) v' rs).
+Proof.
+ intros. destruct p; simpl.
+- apply agree_set_mreg_parallel; auto.
+- apply agree_set_mreg_parallel. apply agree_set_mreg_parallel; auto.
+ apply Val.hiword_lessdef; auto. apply Val.loword_lessdef; auto.
+Qed.
+
+Lemma agree_undef_nondata_regs:
+ forall ms sp rl rs,
+ agree ms sp rs ->
+ (forall r, In r rl -> data_preg r = false) ->
+ agree ms sp (undef_regs rl rs).
+Proof.
+ induction rl; simpl; intros. auto.
+ apply IHrl. apply agree_exten with rs; auto.
+ intros. apply Pregmap.gso. red; intros; subst.
+ assert (data_preg a = false) by auto. congruence.
+ intros. apply H0; auto.
+Qed.
+
+Lemma agree_undef_regs:
+ forall ms sp rl rs rs',
+ agree ms sp rs ->
+ (forall r', data_preg r' = true -> preg_notin r' rl -> rs'#r' = rs#r') ->
+ agree (Mach.undef_regs rl ms) sp rs'.
+Proof.
+ intros. destruct H. split; auto.
+ rewrite <- agree_sp0. apply H0; auto.
+ rewrite preg_notin_charact. intros. apply not_eq_sym. apply preg_of_not_SP.
+ intros. destruct (In_dec mreg_eq r rl).
+ rewrite Mach.undef_regs_same; auto.
+ rewrite Mach.undef_regs_other; auto. rewrite H0; auto.
+ apply preg_of_data.
+ rewrite preg_notin_charact. intros; red; intros. elim n.
+ exploit preg_of_injective; eauto. congruence.
+Qed.
+
+Lemma agree_set_undef_mreg:
+ forall ms sp rs r v rl rs',
+ agree ms sp rs ->
+ Val.lessdef v (rs'#(preg_of r)) ->
+ (forall r', data_preg r' = true -> r' <> preg_of r -> preg_notin r' rl -> rs'#r' = rs#r') ->
+ agree (Mach.Regmap.set r v (Mach.undef_regs rl ms)) sp rs'.
+Proof.
+ intros. apply agree_set_mreg with (rs'#(preg_of r) <- (rs#(preg_of r))); auto.
+ apply agree_undef_regs with rs; auto.
+ intros. unfold Pregmap.set. destruct (PregEq.eq r' (preg_of r)).
+ congruence. auto.
+ intros. rewrite Pregmap.gso; auto.
+Qed.
+
+Lemma agree_undef_caller_save_regs:
+ forall ms sp rs,
+ agree ms sp rs ->
+ agree (Mach.undef_caller_save_regs ms) sp (undef_caller_save_regs rs).
+Proof.
+ intros. destruct H. unfold Mach.undef_caller_save_regs, undef_caller_save_regs; split.
+- unfold proj_sumbool; rewrite dec_eq_true. auto.
+- auto.
+- intros. unfold proj_sumbool. rewrite dec_eq_false by (apply preg_of_not_SP).
+ destruct (List.in_dec preg_eq (preg_of r) (List.map preg_of (List.filter is_callee_save all_mregs))); simpl.
++ apply list_in_map_inv in i. destruct i as (mr & A & B).
+ assert (r = mr) by (apply preg_of_injective; auto). subst mr; clear A.
+ apply List.filter_In in B. destruct B as [C D]. rewrite D. auto.
++ destruct (is_callee_save r) eqn:CS; auto.
+ elim n. apply List.in_map. apply List.filter_In. auto using all_mregs_complete.
+Qed.
+
+Lemma agree_change_sp:
+ forall ms sp rs sp',
+ agree ms sp rs -> sp' <> Vundef ->
+ agree ms sp' (rs#SP <- sp').
+Proof.
+ intros. inv H. split; auto.
+ intros. rewrite Pregmap.gso; auto with asmgen.
+Qed.
+
+(** Connection between Mach and Asm calling conventions for external
+ functions. *)
+
+Lemma extcall_arg_match:
+ forall ms sp rs m m' l v,
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ Mach.extcall_arg ms m sp l v ->
+ exists v', AB.extcall_arg rs m' l v' /\ Val.lessdef v v'.
+Proof.
+ intros. inv H1.
+ exists (rs#(preg_of r)); split. constructor. eapply preg_val; eauto.
+ unfold Mach.load_stack in H2.
+ exploit Mem.loadv_extends; eauto. intros [v' [A B]].
+ rewrite (sp_val _ _ _ H) in A.
+ exists v'; split; auto.
+ econstructor. eauto. assumption.
+Qed.
+
+Lemma extcall_arg_pair_match:
+ forall ms sp rs m m' p v,
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ Mach.extcall_arg_pair ms m sp p v ->
+ exists v', AB.extcall_arg_pair rs m' p v' /\ Val.lessdef v v'.
+Proof.
+ intros. inv H1.
+- exploit extcall_arg_match; eauto. intros (v' & A & B). exists v'; split; auto. constructor; auto.
+- exploit extcall_arg_match. eauto. eauto. eexact H2. intros (v1 & A1 & B1).
+ exploit extcall_arg_match. eauto. eauto. eexact H3. intros (v2 & A2 & B2).
+ exists (Val.longofwords v1 v2); split. constructor; auto. apply Val.longofwords_lessdef; auto.
+Qed.
+
+
+Lemma extcall_args_match:
+ forall ms sp rs m m', agree ms sp rs -> Mem.extends m m' ->
+ forall ll vl,
+ list_forall2 (Mach.extcall_arg_pair ms m sp) ll vl ->
+ exists vl', list_forall2 (AB.extcall_arg_pair rs m') ll vl' /\ Val.lessdef_list vl vl'.
+Proof.
+ induction 3; intros.
+ exists (@nil val); split. constructor. constructor.
+ exploit extcall_arg_pair_match; eauto. intros [v1' [A B]].
+ destruct IHlist_forall2 as [vl' [C D]].
+ exists (v1' :: vl'); split; constructor; auto.
+Qed.
+
+Lemma extcall_arguments_match:
+ forall ms m m' sp rs sg args,
+ agree ms sp rs -> Mem.extends m m' ->
+ Mach.extcall_arguments ms m sp sg args ->
+ exists args', AB.extcall_arguments rs m' sg args' /\ Val.lessdef_list args args'.
+Proof.
+ unfold Mach.extcall_arguments, AB.extcall_arguments; intros.
+ eapply extcall_args_match; eauto.
+Qed.
+
+Remark builtin_arg_match:
+ forall ge (rs: regset) sp m a v,
+ eval_builtin_arg ge (fun r => rs (preg_of r)) sp m a v ->
+ eval_builtin_arg ge rs sp m (map_builtin_arg preg_of a) v.
+Proof.
+ induction 1; simpl; eauto with barg.
+Qed.
+
+Lemma builtin_args_match:
+ forall ge ms sp rs m m', agree ms sp rs -> Mem.extends m m' ->
+ forall al vl, eval_builtin_args ge ms sp m al vl ->
+ exists vl', eval_builtin_args ge rs sp m' (map (map_builtin_arg preg_of) al) vl'
+ /\ Val.lessdef_list vl vl'.
+Proof.
+ induction 3; intros; simpl.
+ exists (@nil val); split; constructor.
+ exploit (@eval_builtin_arg_lessdef _ ge ms (fun r => rs (preg_of r))); eauto.
+ intros; eapply preg_val; eauto.
+ intros (v1' & A & B).
+ destruct IHlist_forall2 as [vl' [C D]].
+ exists (v1' :: vl'); split; constructor; auto. apply builtin_arg_match; auto.
+Qed.
+
+Lemma agree_set_res:
+ forall res ms sp rs v v',
+ agree ms sp rs ->
+ Val.lessdef v v' ->
+ agree (Mach.set_res res v ms) sp (AB.set_res (map_builtin_res preg_of res) v' rs).
+Proof.
+ induction res; simpl; intros.
+- eapply agree_set_mreg; eauto. rewrite Pregmap.gss. auto.
+ intros. apply Pregmap.gso; auto.
+- auto.
+- apply IHres2. apply IHres1. auto.
+ apply Val.hiword_lessdef; auto.
+ apply Val.loword_lessdef; auto.
+Qed.
+
+Lemma set_res_other:
+ forall r res v rs,
+ data_preg r = false ->
+ set_res (map_builtin_res preg_of res) v rs r = rs r.
+Proof.
+ induction res; simpl; intros.
+- apply Pregmap.gso. red; intros; subst r. rewrite preg_of_data in H; discriminate.
+- auto.
+- rewrite IHres2, IHres1; auto.
+Qed.
+
+(* inspired from Mach *)
+
+Lemma find_label_tail:
+ forall lbl c c', MB.find_label lbl c = Some c' -> is_tail c' c.
+Proof.
+ induction c; simpl; intros. discriminate.
+ destruct (MB.is_label lbl a). inv H. auto with coqlib. eauto with coqlib.
+Qed.
+
+(* inspired from Asmgenproof0 *)
+
+(* ... skip ... *)
+
+(** The ``code tail'' of an instruction list [c] is the list of instructions
+ starting at PC [pos]. *)
+
+Inductive code_tail: Z -> bblocks -> bblocks -> Prop :=
+ | code_tail_0: forall c,
+ code_tail 0 c c
+ | code_tail_S: forall pos bi c1 c2,
+ code_tail pos c1 c2 ->
+ code_tail (pos + (size bi)) (bi :: c1) c2.
+
+Lemma code_tail_pos:
+ forall pos c1 c2, code_tail pos c1 c2 -> pos >= 0.
+Proof.
+ induction 1. lia. generalize (size_positive bi); intros; lia.
+Qed.
+
+Lemma find_bblock_tail:
+ forall c1 bi c2 pos,
+ code_tail pos c1 (bi :: c2) ->
+ find_bblock pos c1 = Some bi.
+Proof.
+ induction c1; simpl; intros.
+ inversion H.
+ destruct (zlt pos 0). generalize (code_tail_pos _ _ _ H); intro; lia.
+ destruct (zeq pos 0). subst pos.
+ inv H. auto. generalize (size_positive a) (code_tail_pos _ _ _ H4). intro; lia.
+ inv H. congruence. replace (pos0 + size a - size a) with pos0 by lia.
+ eauto.
+Qed.
+
+
+Local Hint Resolve code_tail_0 code_tail_S: core.
+
+Lemma code_tail_next:
+ forall fn ofs c0,
+ code_tail ofs fn c0 ->
+ forall bi c1, c0 = bi :: c1 -> code_tail (ofs + (size bi)) fn c1.
+Proof.
+ induction 1; intros.
+ - subst; eauto.
+ - replace (pos + size bi + size bi0) with ((pos + size bi0) + size bi); eauto.
+ lia.
+Qed.
+
+Lemma size_blocks_pos c: 0 <= size_blocks c.
+Proof.
+ induction c as [| a l ]; simpl; try lia.
+ generalize (size_positive a); lia.
+Qed.
+
+Remark code_tail_positive:
+ forall fn ofs c,
+ code_tail ofs fn c -> 0 <= ofs.
+Proof.
+ induction 1; intros; simpl.
+ - lia.
+ - generalize (size_positive bi). lia.
+Qed.
+
+Remark code_tail_size:
+ forall fn ofs c,
+ code_tail ofs fn c -> size_blocks fn = ofs + size_blocks c.
+Proof.
+ induction 1; intros; simpl; try lia.
+Qed.
+
+Remark code_tail_bounds fn ofs c:
+ code_tail ofs fn c -> 0 <= ofs <= size_blocks fn.
+Proof.
+ intro H;
+ exploit code_tail_size; eauto.
+ generalize (code_tail_positive _ _ _ H), (size_blocks_pos c).
+ lia.
+Qed.
+
+Local Hint Resolve code_tail_next: core.
+
+Lemma code_tail_next_int:
+ forall fn ofs bi c,
+ size_blocks fn <= Ptrofs.max_unsigned ->
+ code_tail (Ptrofs.unsigned ofs) fn (bi :: c) ->
+ code_tail (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr (size bi)))) fn c.
+Proof.
+ intros.
+ exploit code_tail_size; eauto.
+ simpl; generalize (code_tail_positive _ _ _ H0), (size_positive bi), (size_blocks_pos c).
+ intros.
+ rewrite Ptrofs.add_unsigned, Ptrofs.unsigned_repr.
+ - rewrite Ptrofs.unsigned_repr; eauto.
+ lia.
+ - rewrite Ptrofs.unsigned_repr; lia.
+Qed.
+
+(** Predictor for return addresses in generated Asm code.
+
+ The [return_address_offset] predicate defined here is used in the
+ semantics for Mach to determine the return addresses that are
+ stored in activation records. *)
+
+(** Consider a Mach function [f] and a sequence [c] of Mach instructions
+ representing the Mach code that remains to be executed after a
+ function call returns. The predicate [return_address_offset f c ofs]
+ holds if [ofs] is the integer offset of the PPC instruction
+ following the call in the Asm code obtained by translating the
+ code of [f]. Graphically:
+<<
+ Mach function f |--------- Mcall ---------|
+ Mach code c | |--------|
+ | \ \
+ | \ \
+ | \ \
+ Asm code | |--------|
+ Asm function |------------- Pcall ---------|
+
+ <-------- ofs ------->
+>>
+*)
+
+Definition return_address_offset (f: MB.function) (c: MB.code) (ofs: ptrofs) : Prop :=
+ forall tf tc,
+ transf_function f = OK tf ->
+ transl_blocks f c false = OK tc ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) tc.
+
+Lemma transl_blocks_tail:
+ forall f c1 c2, is_tail c1 c2 ->
+ forall tc2 ep2, transl_blocks f c2 ep2 = OK tc2 ->
+ exists tc1, exists ep1, transl_blocks f c1 ep1 = OK tc1 /\ is_tail tc1 tc2.
+Proof.
+ induction 1; simpl; intros.
+ exists tc2; exists ep2; split; auto with coqlib.
+ monadInv H0. exploit IHis_tail; eauto. intros (tc1 & ep1 & A & B).
+ exists tc1; exists ep1; split. auto.
+ eapply is_tail_trans with x0; eauto with coqlib.
+Qed.
+
+Lemma is_tail_code_tail:
+ forall c1 c2, is_tail c1 c2 -> exists ofs, code_tail ofs c2 c1.
+Proof.
+ induction 1; eauto.
+ destruct IHis_tail; eauto.
+Qed.
+
+Section RETADDR_EXISTS.
+
+Hypothesis transf_function_inv:
+ forall f tf, transf_function f = OK tf ->
+ exists tc ep, transl_blocks f (Machblock.fn_code f) ep = OK tc /\ is_tail tc (fn_blocks tf).
+
+Hypothesis transf_function_len:
+ forall f tf, transf_function f = OK tf -> size_blocks (fn_blocks tf) <= Ptrofs.max_unsigned.
+
+
+Lemma return_address_exists:
+ forall b f c, is_tail (b :: c) f.(MB.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros. destruct (transf_function f) as [tf|] eqn:TF.
+ + exploit transf_function_inv; eauto. intros (tc1 & ep1 & TR1 & TL1).
+ exploit transl_blocks_tail; eauto. intros (tc2 & ep2 & TR2 & TL2).
+ monadInv TR2.
+ assert (TL3: is_tail x0 (fn_blocks tf)).
+ { apply is_tail_trans with tc1; auto.
+ apply is_tail_trans with (x++x0); auto. eapply is_tail_app.
+ }
+ exploit is_tail_code_tail. eexact TL3. intros [ofs CT].
+ exists (Ptrofs.repr ofs). red; intros.
+ rewrite Ptrofs.unsigned_repr. congruence.
+ exploit code_tail_bounds; eauto.
+ intros; apply transf_function_len in TF. lia.
+ + exists Ptrofs.zero; red; intros. congruence.
+Qed.
+
+End RETADDR_EXISTS.
+
+(** [transl_code_at_pc pc fb f c ep tf tc] holds if the code pointer [pc] points
+ within the Asmblock code generated by translating Machblock function [f],
+ and [tc] is the tail of the generated code at the position corresponding
+ to the code pointer [pc]. *)
+
+Inductive transl_code_at_pc (ge: MB.genv):
+ val -> block -> MB.function -> MB.code -> bool -> AB.function -> AB.bblocks -> Prop :=
+ transl_code_at_pc_intro:
+ forall b ofs f c ep tf tc,
+ Genv.find_funct_ptr ge b = Some(Internal f) ->
+ transf_function f = Errors.OK tf ->
+ transl_blocks f c ep = OK tc ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) tc ->
+ transl_code_at_pc ge (Vptr b ofs) b f c ep tf tc.
+
+Remark code_tail_no_bigger:
+ forall pos c1 c2, code_tail pos c1 c2 -> (length c2 <= length c1)%nat.
+Proof.
+ induction 1; simpl; lia.
+Qed.
+
+Remark code_tail_unique:
+ forall fn c pos pos',
+ code_tail pos fn c -> code_tail pos' fn c -> pos = pos'.
+Proof.
+ induction fn; intros until pos'; intros ITA CT; inv ITA; inv CT; auto.
+ generalize (code_tail_no_bigger _ _ _ H3); simpl; intro; lia.
+ generalize (code_tail_no_bigger _ _ _ H3); simpl; intro; lia.
+ f_equal. eauto.
+Qed.
+
+Lemma return_address_offset_correct:
+ forall ge b ofs fb f c tf tc ofs',
+ transl_code_at_pc ge (Vptr b ofs) fb f c false tf tc ->
+ return_address_offset f c ofs' ->
+ ofs' = ofs.
+Proof.
+ intros. inv H. red in H0.
+ exploit code_tail_unique. eexact H12. eapply H0; eauto. intro.
+ rewrite <- (Ptrofs.repr_unsigned ofs).
+ rewrite <- (Ptrofs.repr_unsigned ofs').
+ congruence.
+Qed.
+
+(** The [find_label] function returns the code tail starting at the
+ given label. A connection with [code_tail] is then established. *)
+
+Fixpoint find_label (lbl: label) (c: bblocks) {struct c} : option bblocks :=
+ match c with
+ | nil => None
+ | bb1 :: bbl => if is_label lbl bb1 then Some c else find_label lbl bbl
+ end.
+
+Lemma label_pos_code_tail:
+ forall lbl c pos c',
+ find_label lbl c = Some c' ->
+ exists pos',
+ label_pos lbl pos c = Some pos'
+ /\ code_tail (pos' - pos) c c'
+ /\ pos <= pos' <= pos + size_blocks c.
+Proof.
+ induction c.
+ simpl; intros. discriminate.
+ simpl; intros until c'.
+ case (is_label lbl a).
+ - intros. inv H. exists pos. split; auto. split.
+ replace (pos - pos) with 0 by lia. constructor. constructor; try lia.
+ generalize (size_blocks_pos c). generalize (size_positive a). lia.
+ - intros. generalize (IHc (pos+size a) c' H). intros [pos' [A [B C]]].
+ exists pos'. split. auto. split.
+ replace (pos' - pos) with ((pos' - (pos + (size a))) + (size a)) by lia.
+ constructor. auto. generalize (size_positive a). lia.
+Qed.
+
+(** Helper lemmas to reason about
+- the "code is tail of" property
+- correct translation of labels. *)
+
+Definition tail_nolabel (k c: bblocks) : Prop :=
+ is_tail k c /\ forall lbl, find_label lbl c = find_label lbl k.
+
+Lemma tail_nolabel_refl:
+ forall c, tail_nolabel c c.
+Proof.
+ intros; split. apply is_tail_refl. auto.
+Qed.
+
+Lemma tail_nolabel_trans:
+ forall c1 c2 c3, tail_nolabel c2 c3 -> tail_nolabel c1 c2 -> tail_nolabel c1 c3.
+Proof.
+ intros. destruct H; destruct H0; split.
+ eapply is_tail_trans; eauto.
+ intros. rewrite H1; auto.
+Qed.
+
+Definition nolabel (b: bblock) :=
+ match (header b) with nil => True | _ => False end.
+
+Hint Extern 1 (nolabel _) => exact I : labels.
+
+Lemma tail_nolabel_cons:
+ forall b c k,
+ nolabel b -> tail_nolabel k c -> tail_nolabel k (b :: c).
+Proof.
+ intros. destruct H0. split.
+ constructor; auto.
+ intros. simpl. rewrite <- H1. destruct b as [hd bdy ex]; simpl in *.
+ destruct hd as [|l hd]; simpl in *.
+ - assert (is_label lbl {| AB.header := nil; AB.body := bdy; AB.exit := ex; AB.correct := correct |} = false).
+ { apply is_label_correct_false. simpl header. apply in_nil. }
+ rewrite H2. auto.
+ - contradiction.
+Qed.
+
+Hint Resolve tail_nolabel_refl: labels.
+
+Ltac TailNoLabel :=
+ eauto with labels;
+ match goal with
+ | [ |- tail_nolabel _ (_ :: _) ] => apply tail_nolabel_cons; [auto; exact I | TailNoLabel]
+ | [ H: Error _ = OK _ |- _ ] => discriminate
+ | [ H: assertion_failed = OK _ |- _ ] => discriminate
+ | [ H: OK _ = OK _ |- _ ] => inv H; TailNoLabel
+ | [ H: bind _ _ = OK _ |- _ ] => monadInv H; TailNoLabel
+ | [ H: (if ?x then _ else _) = OK _ |- _ ] => destruct x; TailNoLabel
+ | [ H: match ?x with nil => _ | _ :: _ => _ end = OK _ |- _ ] => destruct x; TailNoLabel
+ | _ => idtac
+ end.
+
+Remark tail_nolabel_find_label:
+ forall lbl k c, tail_nolabel k c -> find_label lbl c = find_label lbl k.
+Proof.
+ intros. destruct H. auto.
+Qed.
+
+Remark tail_nolabel_is_tail:
+ forall k c, tail_nolabel k c -> is_tail k c.
+Proof.
+ intros. destruct H. auto.
+Qed.
+
+Lemma exec_body_pc:
+ forall ge l rs1 m1 rs2 m2,
+ exec_body ge l rs1 m1 = Next rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ induction l.
+ - intros. inv H. auto.
+ - intros until m2. intro EXEB.
+ inv EXEB. destruct (exec_basic_instr _ _ _ _) eqn:EBI; try discriminate.
+ eapply IHl in H0. rewrite H0.
+ erewrite exec_basic_instr_pc; eauto.
+Qed.
+
+Section STRAIGHTLINE.
+
+Variable ge: genv.
+Variable fn: function.
+
+(** Straight-line code is composed of processor instructions that execute
+ in sequence (no branches, no function calls and returns).
+ The following inductive predicate relates the machine states
+ before and after executing a straight-line sequence of instructions.
+ Instructions are taken from the first list instead of being fetched
+ from memory. *)
+
+Inductive exec_straight: list instruction -> regset -> mem ->
+ list instruction -> regset -> mem -> Prop :=
+ | exec_straight_one:
+ forall i1 c rs1 m1 rs2 m2,
+ exec_basic_instr ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_straight ((PBasic i1) ::g c) rs1 m1 c rs2 m2
+ | exec_straight_step:
+ forall i c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_basic_instr ge i rs1 m1 = Next rs2 m2 ->
+ exec_straight c rs2 m2 c' rs3 m3 ->
+ exec_straight ((PBasic i) :: c) rs1 m1 c' rs3 m3.
+
+Inductive exec_control_rel: option control -> bblock -> regset -> mem ->
+ regset -> mem -> Prop :=
+ | exec_control_rel_intro:
+ forall rs1 m1 b rs1' ctl rs2 m2,
+ rs1' = nextblock b rs1 ->
+ exec_control ge fn ctl rs1' m1 = Next rs2 m2 ->
+ exec_control_rel ctl b rs1 m1 rs2 m2.
+
+Inductive exec_bblock_rel: bblock -> regset -> mem -> regset -> mem -> Prop :=
+ | exec_bblock_rel_intro:
+ forall rs1 m1 b rs2 m2,
+ exec_bblock ge fn b rs1 m1 = Next rs2 m2 ->
+ exec_bblock_rel b rs1 m1 rs2 m2.
+
+Lemma exec_straight_body:
+ forall c l rs1 m1 rs2 m2,
+ exec_straight c rs1 m1 nil rs2 m2 ->
+ code_to_basics c = Some l ->
+ exec_body ge l rs1 m1 = Next rs2 m2.
+Proof.
+ induction c as [|i c].
+ - intros until m2. intros EXES CTB. inv EXES.
+ - intros until m2. intros EXES CTB. inv EXES.
+ + inv CTB. simpl. rewrite H6. auto.
+ + inv CTB. destruct (code_to_basics c); try discriminate. inv H0. eapply IHc in H7; eauto.
+ rewrite <- H7. simpl. rewrite H1. auto.
+Qed.
+
+Lemma exec_straight_body2:
+ forall c rs1 m1 c' rs2 m2,
+ exec_straight c rs1 m1 c' rs2 m2 ->
+ exists body,
+ exec_body ge body rs1 m1 = Next rs2 m2
+ /\ (basics_to_code body) ++g c' = c.
+Proof.
+ intros until m2. induction 1.
+ - exists (i1::nil). split; auto. simpl. rewrite H. auto.
+ - destruct IHexec_straight as (bdy & EXEB & BTC).
+ exists (i:: bdy). split; simpl.
+ + rewrite H. auto.
+ + congruence.
+Qed.
+
+Lemma exec_straight_trans:
+ forall c1 rs1 m1 c2 rs2 m2 c3 rs3 m3,
+ exec_straight c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight c1 rs1 m1 c3 rs3 m3.
+Proof.
+ induction 1; intros.
+ apply exec_straight_step with rs2 m2; auto.
+ apply exec_straight_step with rs2 m2; auto.
+Qed.
+
+Lemma exec_straight_two:
+ forall i1 i2 c rs1 m1 rs2 m2 rs3 m3,
+ exec_basic_instr ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_basic_instr ge i2 rs2 m2 = Next rs3 m3 ->
+ exec_straight (i1 ::g i2 ::g c) rs1 m1 c rs3 m3.
+Proof.
+ intros. apply exec_straight_step with rs2 m2; auto.
+ apply exec_straight_one; auto.
+Qed.
+
+Lemma exec_straight_three:
+ forall i1 i2 i3 c rs1 m1 rs2 m2 rs3 m3 rs4 m4,
+ exec_basic_instr ge i1 rs1 m1 = Next rs2 m2 ->
+ exec_basic_instr ge i2 rs2 m2 = Next rs3 m3 ->
+ exec_basic_instr ge i3 rs3 m3 = Next rs4 m4 ->
+ exec_straight (i1 ::g i2 ::g i3 ::g c) rs1 m1 c rs4 m4.
+Proof.
+ intros. apply exec_straight_step with rs2 m2; auto.
+ eapply exec_straight_two; eauto.
+Qed.
+
+(** Like exec_straight predicate, but on blocks *)
+
+Inductive exec_straight_blocks: bblocks -> regset -> mem ->
+ bblocks -> regset -> mem -> Prop :=
+ | exec_straight_blocks_one:
+ forall b1 c rs1 m1 rs2 m2,
+ exec_bblock ge fn b1 rs1 m1 = Next rs2 m2 ->
+ rs2#PC = Val.offset_ptr rs1#PC (Ptrofs.repr (size b1)) ->
+ exec_straight_blocks (b1 :: c) rs1 m1 c rs2 m2
+ | exec_straight_blocks_step:
+ forall b c rs1 m1 rs2 m2 c' rs3 m3,
+ exec_bblock ge fn b rs1 m1 = Next rs2 m2 ->
+ rs2#PC = Val.offset_ptr rs1#PC (Ptrofs.repr (size b)) ->
+ exec_straight_blocks c rs2 m2 c' rs3 m3 ->
+ exec_straight_blocks (b :: c) rs1 m1 c' rs3 m3.
+
+Lemma exec_straight_blocks_trans:
+ forall c1 rs1 m1 c2 rs2 m2 c3 rs3 m3,
+ exec_straight_blocks c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight_blocks c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight_blocks c1 rs1 m1 c3 rs3 m3.
+Proof.
+ induction 1; intros.
+ apply exec_straight_blocks_step with rs2 m2; auto.
+ apply exec_straight_blocks_step with rs2 m2; auto.
+Qed.
+
+(** Linking exec_straight with exec_straight_blocks *)
+
+Lemma exec_straight_pc:
+ forall c c' rs1 m1 rs2 m2,
+ exec_straight c rs1 m1 c' rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ induction c; intros; try (inv H; fail).
+ inv H.
+ - eapply exec_basic_instr_pc; eauto.
+ - rewrite (IHc c' rs3 m3 rs2 m2); auto.
+ erewrite exec_basic_instr_pc; eauto.
+Qed.
+
+Lemma regset_same_assign (rs: regset) r:
+ rs # r <- (rs r) = rs.
+Proof.
+ apply functional_extensionality. intros x. destruct (preg_eq x r); subst; Simpl.
+Qed.
+
+Lemma exec_straight_through_singleinst:
+ forall a b rs1 m1 rs2 m2 rs2' m2' lb,
+ bblock_single_inst (PBasic a) = b ->
+ exec_straight (a ::g nil) rs1 m1 nil rs2 m2 ->
+ nextblock b rs2 = rs2' -> m2 = m2' ->
+ exec_straight_blocks (b::lb) rs1 m1 lb rs2' m2'.
+Proof.
+ intros. subst. constructor 1. unfold exec_bblock. simpl body. erewrite exec_straight_body; eauto.
+ simpl. rewrite regset_same_assign. auto.
+ simpl; auto. unfold nextblock, incrPC; simpl. Simpl. erewrite exec_straight_pc; eauto.
+Qed.
+
+(** The following lemmas show that straight-line executions
+ (predicate [exec_straight_blocks]) correspond to correct Asm executions. *)
+
+Lemma exec_straight_steps_1:
+ forall c rs m c' rs' m',
+ exec_straight_blocks c rs m c' rs' m' ->
+ size_blocks (fn_blocks fn) <= Ptrofs.max_unsigned ->
+ forall b ofs,
+ rs#PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal fn) ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks fn) c ->
+ plus step ge (State rs m) E0 (State rs' m').
+Proof.
+ induction 1; intros.
+ apply plus_one.
+ econstructor; eauto.
+ eapply find_bblock_tail. eauto.
+ eapply plus_left'.
+ econstructor; eauto.
+ eapply find_bblock_tail. eauto.
+ apply IHexec_straight_blocks with b0 (Ptrofs.add ofs (Ptrofs.repr (size b))).
+ auto. rewrite H0. rewrite H3. reflexivity.
+ auto.
+ apply code_tail_next_int; auto.
+ traceEq.
+Qed.
+
+Lemma exec_straight_steps_2:
+ forall c rs m c' rs' m',
+ exec_straight_blocks c rs m c' rs' m' ->
+ size_blocks (fn_blocks fn) <= Ptrofs.max_unsigned ->
+ forall b ofs,
+ rs#PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal fn) ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks fn) c ->
+ exists ofs',
+ rs'#PC = Vptr b ofs'
+ /\ code_tail (Ptrofs.unsigned ofs') (fn_blocks fn) c'.
+Proof.
+ induction 1; intros.
+ exists (Ptrofs.add ofs (Ptrofs.repr (size b1))). split.
+ rewrite H0. rewrite H2. auto.
+ apply code_tail_next_int; auto.
+ apply IHexec_straight_blocks with (Ptrofs.add ofs (Ptrofs.repr (size b))).
+ auto. rewrite H0. rewrite H3. reflexivity. auto.
+ apply code_tail_next_int; auto.
+Qed.
+
+End STRAIGHTLINE.
+
+(** * Properties of the Machblock call stack *)
+
+Section MATCH_STACK.
+
+Variable ge: MB.genv.
+
+Inductive match_stack: list MB.stackframe -> Prop :=
+ | match_stack_nil:
+ match_stack nil
+ | match_stack_cons: forall fb sp ra c s f tf tc,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transl_code_at_pc ge ra fb f c false tf tc ->
+ sp <> Vundef ->
+ match_stack s ->
+ match_stack (Stackframe fb sp ra c :: s).
+
+Lemma parent_sp_def: forall s, match_stack s -> parent_sp s <> Vundef.
+Proof.
+ induction 1; simpl.
+ unfold Vnullptr; destruct Archi.ptr64; congruence.
+ auto.
+Qed.
+
+Lemma parent_ra_def: forall s, match_stack s -> parent_ra s <> Vundef.
+Proof.
+ induction 1; simpl.
+ unfold Vnullptr; destruct Archi.ptr64; congruence.
+ inv H0. congruence.
+Qed.
+
+Lemma lessdef_parent_sp:
+ forall s v,
+ match_stack s -> Val.lessdef (parent_sp s) v -> v = parent_sp s.
+Proof.
+ intros. inv H0. auto. exploit parent_sp_def; eauto. tauto.
+Qed.
+
+Lemma lessdef_parent_ra:
+ forall s v,
+ match_stack s -> Val.lessdef (parent_ra s) v -> v = parent_ra s.
+Proof.
+ intros. inv H0. auto. exploit parent_ra_def; eauto. tauto.
+Qed.
+
+End MATCH_STACK.
diff --git a/kvx/Asmblockgenproof1.v b/kvx/Asmblockgenproof1.v
new file mode 100644
index 00000000..a18afec8
--- /dev/null
+++ b/kvx/Asmblockgenproof1.v
@@ -0,0 +1,2500 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** * Proof of correctness for individual instructions *)
+
+Require Import Coqlib Errors Maps.
+Require Import AST Integers Floats Values Memory Globalenvs.
+Require Import Op Locations Machblock Conventions.
+Require Import Asmblock Asmblockgen Asmblockgenproof0 Asmblockprops.
+Require Import Chunks.
+Require Import Lia.
+
+Import PArithCoercions.
+
+(** Decomposition of integer constants. *)
+
+Lemma make_immed32_sound:
+ forall n,
+ match make_immed32 n with
+ | Imm32_single imm => n = imm
+ end.
+Proof.
+ intros; unfold make_immed32. set (lo := Int.sign_ext 12 n).
+ predSpec Int.eq Int.eq_spec n lo; auto.
+Qed.
+
+Lemma make_immed64_sound:
+ forall n,
+ match make_immed64 n with
+ | Imm64_single imm => n = imm
+ end.
+Proof.
+ intros; unfold make_immed64. set (lo := Int64.sign_ext 12 n).
+ predSpec Int64.eq Int64.eq_spec n lo.
+- auto.
+- set (m := Int64.sub n lo).
+ set (p := Int64.zero_ext 20 (Int64.shru m (Int64.repr 12))).
+ predSpec Int64.eq Int64.eq_spec n (Int64.add (Int64.sign_ext 32 (Int64.shl p (Int64.repr 12))) lo).
+ auto.
+ auto.
+Qed.
+
+
+(** Properties of registers *)
+
+Lemma ireg_of_not_RTMP:
+ forall m r, ireg_of m = OK r -> IR r <> IR RTMP.
+Proof.
+ intros. erewrite <- ireg_of_eq; eauto with asmgen.
+Qed.
+
+Lemma ireg_of_not_RTMP':
+ forall m r, ireg_of m = OK r -> r <> RTMP.
+Proof.
+ intros. apply ireg_of_not_RTMP in H. congruence.
+Qed.
+
+Hint Resolve ireg_of_not_RTMP ireg_of_not_RTMP': asmgen.
+
+
+(** Useful simplification tactic *)
+
+Ltac Simplif :=
+ ((rewrite nextblock_inv by eauto with asmgen)
+ || (rewrite nextblock_inv1 by eauto with asmgen)
+ || (rewrite Pregmap.gss)
+ || (rewrite nextblock_pc)
+ || (rewrite Pregmap.gso by eauto with asmgen)
+ ); auto with asmgen.
+
+Ltac Simpl := repeat Simplif.
+
+(** * Correctness of RISC-V constructor functions *)
+
+Section CONSTRUCTORS.
+
+Variable ge: genv.
+Variable fn: function.
+
+Lemma loadimm32_correct:
+ forall rd n k rs m,
+ exists rs',
+ exec_straight ge (loadimm32 rd n ::g k) rs m k rs' m
+ /\ rs'#rd = Vint n
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ unfold loadimm32; intros. generalize (make_immed32_sound n); intros E.
+ destruct (make_immed32 n).
+- subst imm. econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+Qed.
+
+Lemma loadimm64_correct:
+ forall rd n k rs m,
+ exists rs',
+ exec_straight ge (loadimm64 rd n ::g k) rs m k rs' m
+ /\ rs'#rd = Vlong n
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ unfold loadimm64; intros. generalize (make_immed64_sound n); intros E.
+ destruct (make_immed64 n).
+- subst imm. econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl.
+ intros; Simpl.
+Qed.
+
+Lemma opimm64_correct:
+ forall (op: arith_name_rrr)
+ (opi: arith_name_rri64)
+ (sem: val -> val -> val) m,
+ (forall d s1 s2 rs,
+ exec_basic_instr ge (op d s1 s2) rs m = Next ((rs#d <- (sem rs#s1 rs#s2))) m) ->
+ (forall d s n rs,
+ exec_basic_instr ge (opi d s n) rs m = Next ((rs#d <- (sem rs#s (Vlong n)))) m) ->
+ forall rd r1 n k rs,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (opimm64 op opi rd r1 n ::g k) rs m k rs' m
+ /\ rs'#rd = sem rs#r1 (Vlong n)
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. unfold opimm64. generalize (make_immed64_sound n); intros E.
+ destruct (make_immed64 n).
+- subst imm. econstructor; split.
+ apply exec_straight_one. rewrite H0. simpl; eauto. auto.
+ split. Simpl. intros; Simpl.
+Qed.
+
+(** Add offset to pointer *)
+
+Lemma addptrofs_correct:
+ forall rd r1 n k rs m,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (addptrofs rd r1 n ::g k) rs m k rs' m
+ /\ Val.lessdef (Val.offset_ptr rs#r1 n) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ unfold addptrofs; intros.
+ destruct (Ptrofs.eq_dec n Ptrofs.zero).
+- subst n. econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split. Simpl. destruct (rs r1); simpl; auto. rewrite Ptrofs.add_zero; auto.
+ intros; Simpl.
+- unfold addimm64.
+ exploit (opimm64_correct Paddl Paddil Val.addl); eauto. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. split; auto.
+ rewrite B. destruct (rs r1); simpl; auto.
+ rewrite Ptrofs.of_int64_to_int64 by auto. auto.
+Qed.
+
+Ltac ArgsInv :=
+ repeat (match goal with
+ | [ H: Error _ = OK _ |- _ ] => discriminate
+ | [ H: match ?args with nil => _ | _ :: _ => _ end = OK _ |- _ ] => destruct args
+ | [ H: bind _ _ = OK _ |- _ ] => monadInv H
+ | [ H: match _ with left _ => _ | right _ => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
+ | [ H: match _ with true => _ | false => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
+ end);
+ subst;
+ repeat (match goal with
+ | [ H: ireg_of _ = OK _ |- _ ] => simpl in *; rewrite (ireg_of_eq _ _ H) in *
+ | [ H: freg_of _ = OK _ |- _ ] => simpl in *; rewrite (freg_of_eq _ _ H) in *
+ end).
+
+Inductive exec_straight_opt: list instruction -> regset -> mem -> list instruction -> regset -> mem -> Prop :=
+ | exec_straight_opt_refl: forall c rs m,
+ exec_straight_opt c rs m c rs m
+ | exec_straight_opt_intro: forall c1 rs1 m1 c2 rs2 m2,
+ exec_straight ge c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight_opt c1 rs1 m1 c2 rs2 m2.
+
+Remark exec_straight_opt_right:
+ forall c3 rs3 m3 c1 rs1 m1 c2 rs2 m2,
+ exec_straight_opt c1 rs1 m1 c2 rs2 m2 ->
+ exec_straight ge c2 rs2 m2 c3 rs3 m3 ->
+ exec_straight ge c1 rs1 m1 c3 rs3 m3.
+Proof.
+ destruct 1; intros. auto. eapply exec_straight_trans; eauto.
+Qed.
+
+Lemma transl_comp_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp cmp Signed r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmp_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_comp. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_int (itest_for_cmp cmp Signed) rs # r1 rs # r2)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_int (itest_for_cmp cmp Signed) rs # r1 rs # r2)).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmp_bool cmp rs#r1 rs#r2) as cmpbool.
+ destruct cmp; simpl;
+ unfold Val.cmp; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compi_correct:
+ forall cmp r1 n lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compi cmp Signed r1 n lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmp_bool cmp rs#r1 (Vint n) = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compi. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_int (itest_for_cmp cmp Signed) rs # r1 (Vint n))) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_int (itest_for_cmp cmp Signed) rs # r1 (Vint n))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmp_bool cmp rs#r1 (Vint n)) as cmpbool.
+ destruct cmp; simpl;
+ unfold Val.cmp; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compu_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp cmp Unsigned r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ (Val.mxcmpu_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow ((Pcb BTwnez RTMP lbl)))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_comp. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_int (itest_for_cmp cmp Unsigned) rs # r1 rs # r2)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_int (itest_for_cmp cmp Unsigned) rs # r1 rs # r2)).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.mxcmpu_bool cmp rs#r1 rs#r2) as cmpubool.
+ destruct cmp; simpl; unfold Val.mxcmpu;
+ rewrite <- Heqcmpubool; destruct cmpubool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compui_correct:
+ forall cmp r1 n lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compi cmp Unsigned r1 n lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ (Val.mxcmpu_bool cmp rs#r1 (Vint n) = Some b ->
+ exec_control ge fn (Some (PCtlFlow ((Pcb BTwnez RTMP lbl)))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compi. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_int (itest_for_cmp cmp Unsigned) rs # r1 (Vint n))) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_int (itest_for_cmp cmp Unsigned) rs # r1 (Vint n))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.mxcmpu_bool cmp rs#r1 (Vint n)) as cmpubool.
+ destruct cmp; simpl; unfold Val.mxcmpu;
+ rewrite <- Heqcmpubool; destruct cmpubool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compl_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compl cmp Signed r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmpl_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compl. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_long (itest_for_cmp cmp Signed) rs # r1 rs # r2)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_long (itest_for_cmp cmp Signed) rs # r1 rs # r2)).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpl_bool cmp rs#r1 rs#r2) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_long, Val.cmpl;
+ rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compil_correct:
+ forall cmp r1 n lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compil cmp Signed r1 n lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmpl_bool cmp rs#r1 (Vlong n) = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compil. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_long (itest_for_cmp cmp Signed) rs # r1 (Vlong n))) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_long (itest_for_cmp cmp Signed) rs # r1 (Vlong n))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpl_bool cmp rs#r1 (Vlong n)) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_long, Val.cmpl;
+ rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma swap_comparison_cmpf_eq:
+ forall v1 v2 cmp,
+ (Val.cmpf cmp v1 v2) = (Val.cmpf (swap_comparison cmp) v2 v1).
+Proof.
+ intros. unfold Val.cmpf. unfold Val.cmpf_bool. destruct v1; destruct v2; auto.
+ rewrite Float.cmp_swap. auto.
+Qed.
+
+Lemma swap_comparison_cmpf_bool:
+ forall cmp ft v1 v2,
+ ftest_for_cmp cmp = Reversed ft ->
+ Val.cmpf_bool cmp v1 v2 = Val.cmpf_bool (swap_comparison cmp) v2 v1.
+Proof.
+ intros. unfold Val.cmpf_bool. destruct v1; destruct v2; auto. rewrite Float.cmp_swap. reflexivity.
+Qed.
+
+Lemma transl_compf_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp_float64 cmp r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmpf_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. unfold transl_comp_float64. destruct (ftest_for_cmp cmp) eqn:FT.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_float _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_float ft (rs r1) (rs r2))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpf_bool cmp rs#r1 rs#r2) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_float;
+ unfold Val.cmpf; simpl in FT; inversion FT; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_float _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_float ft (rs r2) (rs r1))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpf_bool cmp rs#r1 rs#r2) as cmpbool.
+ erewrite swap_comparison_cmpf_bool in Heqcmpbool; eauto.
+ destruct cmp; simpl;
+ unfold compare_float;
+ unfold Val.cmpf; simpl in FT; inversion FT; simpl in Heqcmpbool; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma cmpf_bool_ne_eq:
+ forall v1 v2,
+ Val.cmpf_bool Cne v1 v2 = option_map negb (Val.cmpf_bool Ceq v1 v2).
+Proof.
+ intros. unfold Val.cmpf_bool. destruct v1; destruct v2; auto. rewrite Float.cmp_ne_eq. simpl. reflexivity.
+Qed.
+
+Lemma cmpf_bool_ne_eq_rev:
+ forall v1 v2,
+ Val.cmpf_bool Ceq v1 v2 = option_map negb (Val.cmpf_bool Cne v1 v2).
+Proof.
+ intros. unfold Val.cmpf_bool. destruct v1; destruct v2; auto. rewrite Float.cmp_ne_eq. simpl. rewrite negb_involutive. reflexivity.
+Qed.
+
+Lemma option_map_negb_negb:
+ forall v,
+ option_map negb (option_map negb v) = v.
+Proof.
+ destruct v; simpl; auto. rewrite negb_involutive. reflexivity.
+Qed.
+
+Lemma notbool_option_map_negb:
+ forall v, Val.notbool (Val.of_optbool v) = Val.of_optbool (option_map negb v).
+Proof.
+ unfold Val.notbool. unfold Val.of_optbool.
+ destruct v; auto. destruct b; auto.
+Qed.
+
+Lemma swap_comparison_cmpf_bool_notftest:
+ forall cmp ft v1 v2,
+ notftest_for_cmp cmp = Reversed ft ->
+ Val.cmpf_bool cmp v1 v2 = Val.cmpf_bool (swap_comparison cmp) v2 v1.
+Proof.
+ intros. unfold Val.cmpf_bool. destruct v1; destruct v2; auto. rewrite Float.cmp_swap. reflexivity.
+Qed.
+
+Lemma transl_compnotf_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp_notfloat64 cmp r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ (option_map negb (Val.cmpf_bool cmp rs#r1 rs#r2) = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. unfold transl_comp_notfloat64. destruct (notftest_for_cmp cmp) eqn:FT.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_float _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_float ft (rs r1) (rs r2))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (option_map negb (Val.cmpf_bool cmp rs#r1 rs#r2)) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_float;
+ unfold Val.cmpf; simpl in FT; inversion FT.
+ * rewrite cmpf_bool_ne_eq; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite cmpf_bool_ne_eq_rev. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_float _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_float ft (rs r2) (rs r1))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpf_bool cmp rs#r1 rs#r2) as cmpbool.
+ erewrite swap_comparison_cmpf_bool_notftest in Heqcmpbool; eauto.
+ destruct cmp; simpl;
+ unfold compare_float;
+ unfold Val.cmpf; simpl in FT; inversion FT; simpl in Heqcmpbool.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma swap_comparison_cmpfs_bool:
+ forall cmp ft v1 v2,
+ ftest_for_cmp cmp = Reversed ft ->
+ Val.cmpfs_bool cmp v1 v2 = Val.cmpfs_bool (swap_comparison cmp) v2 v1.
+Proof.
+ intros. unfold Val.cmpfs_bool. destruct v1; destruct v2; auto. rewrite Float32.cmp_swap. reflexivity.
+Qed.
+
+Lemma transl_compfs_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp_float32 cmp r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.cmpfs_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. unfold transl_comp_float32. destruct (ftest_for_cmp cmp) eqn:FT.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_single _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_single ft (rs r1) (rs r2))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpfs_bool cmp rs#r1 rs#r2) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_single;
+ unfold Val.cmpfs; simpl in FT; inversion FT; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_single _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_single ft (rs r2) (rs r1))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpfs_bool cmp rs#r1 rs#r2) as cmpbool.
+ erewrite swap_comparison_cmpfs_bool in Heqcmpbool; eauto.
+ destruct cmp; simpl;
+ unfold compare_single;
+ unfold Val.cmpfs; simpl in FT; inversion FT; simpl in Heqcmpbool; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma swap_comparison_cmpfs_bool_notftest:
+ forall cmp ft v1 v2,
+ notftest_for_cmp cmp = Reversed ft ->
+ Val.cmpfs_bool cmp v1 v2 = Val.cmpfs_bool (swap_comparison cmp) v2 v1.
+Proof.
+ intros. unfold Val.cmpfs_bool. destruct v1; destruct v2; auto. rewrite Float32.cmp_swap. reflexivity.
+Qed.
+
+Lemma cmpfs_bool_ne_eq:
+ forall v1 v2,
+ Val.cmpfs_bool Cne v1 v2 = option_map negb (Val.cmpfs_bool Ceq v1 v2).
+Proof.
+ intros. unfold Val.cmpfs_bool. destruct v1; destruct v2; auto. rewrite Float32.cmp_ne_eq. simpl. reflexivity.
+Qed.
+
+Lemma cmpfs_bool_ne_eq_rev:
+ forall v1 v2,
+ Val.cmpfs_bool Ceq v1 v2 = option_map negb (Val.cmpfs_bool Cne v1 v2).
+Proof.
+ intros. unfold Val.cmpfs_bool. destruct v1; destruct v2; auto. rewrite Float32.cmp_ne_eq. simpl. rewrite negb_involutive. reflexivity.
+Qed.
+
+Lemma transl_compnotfs_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_comp_notfloat32 cmp r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ (option_map negb (Val.cmpfs_bool cmp rs#r1 rs#r2) = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. unfold transl_comp_notfloat32. destruct (notftest_for_cmp cmp) eqn:FT.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_single _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_single ft (rs r1) (rs r2))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (option_map negb (Val.cmpfs_bool cmp rs#r1 rs#r2)) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_single;
+ unfold Val.cmpfs; simpl in FT; inversion FT.
+ * rewrite cmpfs_bool_ne_eq; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite cmpfs_bool_ne_eq_rev. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+ * esplit. split.
+ - apply exec_straight_one; simpl; eauto.
+ - split.
+ + intros; Simpl.
+ + intros. remember (rs # RTMP <- (compare_single _ _ _)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_single ft (rs r2) (rs r1))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.cmpfs_bool cmp rs#r1 rs#r2) as cmpbool.
+ erewrite swap_comparison_cmpfs_bool_notftest in Heqcmpbool; eauto.
+ destruct cmp; simpl;
+ unfold compare_single;
+ unfold Val.cmpfs; simpl in FT; inversion FT; simpl in Heqcmpbool.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ * rewrite notbool_option_map_negb. rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto; destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_complu_correct:
+ forall cmp r1 r2 lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compl cmp Unsigned r1 r2 lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.mxcmplu_bool cmp rs#r1 rs#r2 = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compl. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_long (itest_for_cmp cmp Unsigned) rs # r1 rs # r2)) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_long (itest_for_cmp cmp Unsigned) rs # r1 rs # r2)).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.mxcmplu_bool cmp rs#r1 rs#r2) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_long, Val.mxcmplu; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_compilu_correct:
+ forall cmp r1 n lbl k rs m tbb b,
+ exists rs',
+ exec_straight ge (transl_compil cmp Unsigned r1 n lbl k) rs m (Pcb BTwnez RTMP lbl ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.mxcmplu_bool cmp rs#r1 (Vlong n) = Some b ->
+ exec_control ge fn (Some (PCtlFlow (Pcb BTwnez RTMP lbl))) (nextblock tbb rs') m
+ = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros. esplit. split.
+- unfold transl_compil. apply exec_straight_one; simpl; eauto.
+- split.
+ + intros; Simpl.
+ + intros.
+ remember (rs # RTMP <- (compare_long (itest_for_cmp cmp Unsigned) rs # r1 (Vlong n))) as rs'.
+ simpl. assert (Val.cmp_bool Cne (nextblock tbb rs') # RTMP (Vint (Int.repr 0)) = Some b).
+ {
+ assert ((nextblock tbb rs') # RTMP = (compare_long (itest_for_cmp cmp Unsigned) rs # r1 (Vlong n))).
+ { rewrite Heqrs'. auto. }
+ rewrite H0. rewrite <- H.
+ remember (Val.mxcmplu_bool cmp rs#r1 (Vlong n)) as cmpbool.
+ destruct cmp; simpl;
+ unfold compare_long, Val.mxcmplu; rewrite <- Heqcmpbool; destruct cmpbool; simpl; auto;
+ destruct b0; simpl; auto.
+ }
+ rewrite H0. simpl; auto.
+Qed.
+
+Lemma transl_opt_compuimm_correct:
+ forall n cmp r1 lbl k rs m b tbb c,
+ select_comp n cmp = Some c ->
+ exists rs', exists insn,
+ exec_straight_opt (transl_opt_compuimm n cmp r1 lbl k) rs m ((PControl insn) ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.mxcmpu_bool cmp rs#r1 (Vint n) = Some b ->
+ exec_control ge fn (Some insn) (nextblock tbb rs') m = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros.
+(* unfold transl_opt_compuimm. unfold select_comp in H. rewrite H; simpl. *)
+ remember c as c'.
+ destruct c'.
+ - (* c = Ceq *)
+ assert (Int.eq n Int.zero = true) as H'.
+ { remember (Int.eq n Int.zero) as termz. destruct termz; auto.
+ generalize H. unfold select_comp; rewrite <- Heqtermz; simpl.
+ discriminate. }
+ assert (n = (Int.repr 0)) as H0. {
+ destruct (Int.eq_dec n (Int.repr 0)) as [Ha|Ha]; auto.
+ generalize (Int.eq_false _ _ Ha). unfold Int.zero in H'.
+ rewrite H'. discriminate.
+ }
+ assert (Ceq = cmp). {
+ remember cmp as c0'. destruct c0'; auto; generalize H; unfold select_comp;
+ rewrite H'; simpl; auto;
+ intros; contradict H; discriminate.
+ }
+ unfold transl_opt_compuimm. subst. rewrite H'.
+
+ exists rs, (Pcbu BTweqz r1 lbl).
+ split.
+ * constructor.
+ * split; auto. simpl. intros.
+ assert (rs r1 = (nextblock tbb rs) r1).
+ unfold nextblock, incrPC. Simpl. rewrite H1 in H0.
+ (*assert (Val.cmp_bool Ceq (rs r1) (Vint (Int.repr 0)) = Some b) as EVAL'S.
+ { rewrite <- H2. rewrite <- H0. rewrite <- H1. auto. }*)
+ auto;
+ unfold eval_branch. rewrite H0; auto.
+ - (* c = Cne *)
+ assert (Int.eq n Int.zero = true) as H'.
+ { remember (Int.eq n Int.zero) as termz. destruct termz; auto.
+ generalize H. unfold select_comp; rewrite <- Heqtermz; simpl.
+ discriminate. }
+ assert (n = (Int.repr 0)) as H0. {
+ destruct (Int.eq_dec n (Int.repr 0)) as [Ha|Ha]; auto.
+ generalize (Int.eq_false _ _ Ha). unfold Int.zero in H'.
+ rewrite H'. discriminate.
+ }
+ assert (Cne = cmp). {
+ remember cmp as c0'. destruct c0'; auto; generalize H; unfold select_comp;
+ rewrite H'; simpl; auto;
+ intros; contradict H; discriminate.
+ }
+ unfold transl_opt_compuimm. subst. rewrite H'.
+
+ exists rs, (Pcbu BTwnez r1 lbl).
+ split.
+ * constructor.
+ * split; auto. simpl. intros.
+ assert (rs r1 = (nextblock tbb rs) r1).
+ unfold nextblock, incrPC. Simpl. rewrite H1 in H0.
+ auto;
+ unfold eval_branch. rewrite H0. auto.
+ - (* c = Clt *) contradict H; unfold select_comp; destruct (Int.eq n Int.zero);
+ destruct cmp; discriminate.
+ - (* c = Cle *) contradict H; unfold select_comp; destruct (Int.eq n Int.zero);
+ destruct cmp; discriminate.
+ - (* c = Cgt *) contradict H; unfold select_comp; destruct (Int.eq n Int.zero);
+ destruct cmp; discriminate.
+ - (* c = Cge *) contradict H; unfold select_comp; destruct (Int.eq n Int.zero);
+ destruct cmp; discriminate.
+Qed.
+
+Lemma transl_opt_compluimm_correct:
+ forall n cmp r1 lbl k rs m b tbb c,
+ select_compl n cmp = Some c ->
+ exists rs', exists insn,
+ exec_straight_opt (transl_opt_compluimm n cmp r1 lbl k) rs m ((PControl insn) ::g k) rs' m
+ /\ (forall r : preg, r <> PC -> r <> RTMP -> rs' r = rs r)
+ /\ ( Val.mxcmplu_bool cmp rs#r1 (Vlong n) = Some b ->
+ exec_control ge fn (Some insn) (nextblock tbb rs') m = eval_branch fn lbl (nextblock tbb rs') m (Some b))
+ .
+Proof.
+ intros.
+(* unfold transl_opt_compluimm; rewrite H; simpl. *)
+ remember c as c'.
+ destruct c'.
+ - (* c = Ceq *)
+ assert (Int64.eq n Int64.zero = true) as H'.
+ { remember (Int64.eq n Int64.zero) as termz. destruct termz; auto.
+ generalize H. unfold select_compl; rewrite <- Heqtermz; simpl.
+ discriminate. }
+ assert (n = (Int64.repr 0)) as H0. {
+ destruct (Int64.eq_dec n (Int64.repr 0)) as [Ha|Ha]; auto.
+ generalize (Int64.eq_false _ _ Ha). unfold Int64.zero in H'.
+ rewrite H'. discriminate.
+ }
+ assert (Ceq = cmp). {
+ remember cmp as c0'. destruct c0'; auto; generalize H; unfold select_compl;
+ rewrite H'; simpl; auto;
+ intros; contradict H; discriminate.
+ }
+ unfold transl_opt_compluimm; subst; rewrite H'.
+
+ exists rs, (Pcbu BTdeqz r1 lbl).
+ split.
+ * constructor.
+ * split; auto. simpl. intros.
+ assert (rs r1 = (nextblock tbb rs) r1).
+ unfold nextblock, incrPC. Simpl. rewrite H1 in H0.
+ auto;
+ unfold eval_branch. rewrite H0; auto.
+ - (* c = Cne *)
+ assert (Int64.eq n Int64.zero = true) as H'.
+ { remember (Int64.eq n Int64.zero) as termz. destruct termz; auto.
+ generalize H. unfold select_compl; rewrite <- Heqtermz; simpl.
+ discriminate. }
+ assert (n = (Int64.repr 0)) as H0. {
+ destruct (Int64.eq_dec n (Int64.repr 0)) as [Ha|Ha]; auto.
+ generalize (Int64.eq_false _ _ Ha). unfold Int64.zero in H'.
+ rewrite H'. discriminate.
+ }
+ assert (Cne = cmp). {
+ remember cmp as c0'. destruct c0'; auto; generalize H; unfold select_compl;
+ rewrite H'; simpl; auto;
+ intros; contradict H; discriminate.
+ }
+ unfold transl_opt_compluimm; subst; rewrite H'.
+
+ exists rs, (Pcbu BTdnez r1 lbl).
+ split.
+ * constructor.
+ * split; auto. simpl. intros.
+ assert (rs r1 = (nextblock tbb rs) r1).
+ unfold nextblock, incrPC. Simpl. rewrite H1 in H0.
+ auto;
+ unfold eval_branch. rewrite H0; auto.
+ - (* c = Clt *) contradict H; unfold select_compl; destruct (Int64.eq n Int64.zero);
+ destruct cmp; discriminate.
+ - (* c = Cle *) contradict H; unfold select_compl; destruct (Int64.eq n Int64.zero);
+ destruct cmp; discriminate.
+ - (* c = Cgt *) contradict H; unfold select_compl; destruct (Int64.eq n Int64.zero);
+ destruct cmp; discriminate.
+ - (* c = Cge *) contradict H; unfold select_compl; destruct (Int64.eq n Int64.zero);
+ destruct cmp; discriminate.
+Qed.
+
+Local Hint Resolve Val.mxcmpu_bool_correct Val.mxcmplu_bool_correct: core.
+
+Lemma transl_cbranch_correct_1:
+ forall cond args lbl k c m ms b sp rs m' tbb,
+ transl_cbranch cond args lbl k = OK c ->
+ eval_condition cond (List.map ms args) m = Some b ->
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ exists rs', exists insn,
+ exec_straight_opt c rs m' ((PControl insn) ::g k) rs' m'
+ /\ exec_control ge fn (Some insn) (nextblock tbb rs') m' = eval_branch fn lbl (nextblock tbb rs') m' (Some b)
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until tbb; intros TRANSL EVAL AG MEXT.
+ set (vl' := map rs (map preg_of args)).
+ assert (EVAL': eval_condition cond vl' m' = Some b).
+ { apply eval_condition_lessdef with (map ms args) m; auto. eapply preg_vals; eauto. }
+ clear EVAL MEXT AG.
+ destruct cond; simpl in TRANSL; ArgsInv.
+(* Ccomp *)
+- exploit (transl_comp_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+(* Ccompu *)
+- exploit (transl_compu_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; eauto.
+(* Ccompimm *)
+- remember (Int.eq n Int.zero) as eqz.
+ destruct eqz.
+ + assert (n = (Int.repr 0)). {
+ destruct (Int.eq_dec n (Int.repr 0)) as [H|H]; auto.
+ generalize (Int.eq_false _ _ H). unfold Int.zero in Heqeqz.
+ rewrite <- Heqeqz. discriminate.
+ }
+ exists rs, (Pcb (btest_for_cmpswz c0) x lbl).
+ split.
+ * constructor.
+ * split; auto.
+ assert (rs x = (nextblock tbb rs) x).
+ unfold nextblock, incrPC. Simpl. rewrite H0 in EVAL'. clear H0.
+ destruct c0; simpl; auto;
+ unfold eval_branch; rewrite <- H; rewrite EVAL'; auto.
+ + exploit (transl_compi_correct c0 x n lbl); eauto. intros (rs'2 & A' & B' & C').
+ exists rs'2, (Pcb BTwnez RTMP lbl).
+ split.
+ * constructor. eexact A'.
+ * split; auto.
+ { apply C'; auto. }
+(* Ccompuimm *)
+- remember (select_comp n c0) as selcomp.
+ destruct selcomp.
+ + exploit (transl_opt_compuimm_correct n c0 x lbl k). apply eq_sym. apply Heqselcomp.
+ intros (rs' & i & A & B & C).
+ exists rs', i.
+ split.
+ * apply A.
+ * split; auto. apply C. apply EVAL'.
+ + assert (transl_opt_compuimm n c0 x lbl k = transl_compi c0 Unsigned x n lbl k).
+ { unfold transl_opt_compuimm.
+ destruct (Int.eq n Int.zero) eqn:EQN.
+ all: unfold select_comp in Heqselcomp; rewrite EQN in Heqselcomp; destruct c0; simpl in *; auto.
+ all: discriminate. }
+ rewrite H. clear H.
+ exploit (transl_compui_correct c0 x n lbl); eauto. intros (rs'2 & A' & B' & C').
+ exists rs'2, (Pcb BTwnez RTMP lbl).
+ split.
+ * constructor. eexact A'.
+ * split; auto.
+ { apply C'; auto. }
+(* Ccompl *)
+- exploit (transl_compl_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+(* Ccomplu *)
+- exploit (transl_complu_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; eauto.
+(* Ccomplimm *)
+- remember (Int64.eq n Int64.zero) as eqz.
+ destruct eqz.
+ + assert (n = (Int64.repr 0)). {
+ destruct (Int64.eq_dec n (Int64.repr 0)) as [H|H]; auto.
+ generalize (Int64.eq_false _ _ H). unfold Int64.zero in Heqeqz.
+ rewrite <- Heqeqz. discriminate.
+ }
+ exists rs, (Pcb (btest_for_cmpsdz c0) x lbl).
+ split.
+ * constructor.
+ * split; auto.
+ assert (rs x = (nextblock tbb rs) x).
+ unfold nextblock, incrPC. Simpl. rewrite H0 in EVAL'. clear H0.
+ destruct c0; simpl; auto;
+ unfold eval_branch; rewrite <- H; rewrite EVAL'; auto.
+ + exploit (transl_compil_correct c0 x n lbl); eauto. intros (rs'2 & A' & B' & C').
+ exists rs'2, (Pcb BTwnez RTMP lbl).
+ split.
+ * constructor. eexact A'.
+ * split; auto.
+ { apply C'; auto. }
+
+(* Ccompluimm *)
+- remember (select_compl n c0) as selcomp.
+ destruct selcomp.
+ + exploit (transl_opt_compluimm_correct n c0 x lbl k). apply eq_sym. apply Heqselcomp.
+ intros (rs' & i & A & B & C).
+ exists rs', i.
+ split.
+ * apply A.
+ * split; eauto. (* apply C. apply EVAL'. *)
+ + assert (transl_opt_compluimm n c0 x lbl k = transl_compil c0 Unsigned x n lbl k).
+ { unfold transl_opt_compluimm.
+ destruct (Int64.eq n Int64.zero) eqn:EQN.
+ all: unfold select_compl in Heqselcomp; rewrite EQN in Heqselcomp; destruct c0; simpl in *; auto.
+ all: discriminate. }
+ rewrite H. clear H.
+ exploit (transl_compilu_correct c0 x n lbl); eauto. intros (rs'2 & A' & B' & C').
+ exists rs'2, (Pcb BTwnez RTMP lbl).
+ split.
+ * constructor. eexact A'.
+ * split; auto.
+ { apply C'; auto. eapply Val.mxcmplu_bool_correct; eauto. }
+
+(* Ccompf *)
+- exploit (transl_compf_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+
+(* Cnotcompf *)
+- exploit (transl_compnotf_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+
+(* Ccompfs *)
+- exploit (transl_compfs_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+
+(* Cnotcompfs *)
+- exploit (transl_compnotfs_correct c0 x x0 lbl); eauto. intros (rs' & A & B & C).
+ exists rs', (Pcb BTwnez RTMP lbl).
+ split.
+ + constructor. eexact A.
+ + split; auto. apply C; auto.
+Qed.
+
+Lemma transl_cbranch_correct_true:
+ forall cond args lbl k c m ms sp rs m' tbb,
+ transl_cbranch cond args lbl k = OK c ->
+ eval_condition cond (List.map ms args) m = Some true ->
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ exists rs', exists insn,
+ exec_straight_opt c rs m' ((PControl insn) ::g k) rs' m'
+ /\ exec_control ge fn (Some insn) (nextblock tbb rs') m' = goto_label fn lbl (nextblock tbb rs') m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. eapply transl_cbranch_correct_1 with (b := true); eauto.
+Qed.
+
+Lemma transl_cbranch_correct_false:
+ forall cond args lbl k c m ms sp rs tbb m',
+ transl_cbranch cond args lbl k = OK c ->
+ eval_condition cond (List.map ms args) m = Some false ->
+ agree ms sp rs ->
+ Mem.extends m m' ->
+ exists rs', exists insn,
+ exec_straight_opt c rs m' ((PControl insn) ::g k) rs' m'
+ /\ exec_control ge fn (Some insn) (nextblock tbb rs') m' = Next (nextblock tbb rs') m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. exploit transl_cbranch_correct_1. all: eauto. simpl eval_branch. instantiate (1 := tbb).
+ intros (rs' & insn & A & B & C). rewrite regset_same_assign in B.
+ eexists; eexists. split; try split. all: eassumption.
+Qed.
+
+(** Translation of condition operators *)
+
+Lemma transl_cond_int32s_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_int32s cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.cmp cmp rs#r1 rs#r2) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+
+Lemma transl_cond_int32u_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_int32u cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = Val.mxcmpu cmp rs#r1 rs#r2
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_cond_int64s_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_int64s cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.maketotal (Val.cmpl cmp rs#r1 rs#r2)) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_cond_int64u_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_int64u cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = Val.mxcmplu cmp rs#r1 rs#r2
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_condimm_int32s_correct:
+ forall cmp rd r1 n k rs m,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code (transl_condimm_int32s cmp rd r1 n k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.cmp cmp rs#r1 (Vint n)) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Local Hint Resolve Val.mxcmpu_correct Val.mxcmplu_correct: core.
+
+Lemma transl_condimm_int32u_correct:
+ forall cmp rd r1 n k rs m,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code (transl_condimm_int32u cmp rd r1 n k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.cmpu (Mem.valid_pointer m) cmp rs#r1 (Vint n)) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_condimm_int64s_correct:
+ forall cmp rd r1 n k rs m,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code (transl_condimm_int64s cmp rd r1 n k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.maketotal (Val.cmpl cmp rs#r1 (Vlong n))) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_condimm_int64u_correct:
+ forall cmp rd r1 n k rs m,
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code (transl_condimm_int64u cmp rd r1 n k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.maketotal (Val.cmplu (Mem.valid_pointer m) cmp rs#r1 (Vlong n))) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl;
+ (econstructor; split;
+ [ apply exec_straight_one; [simpl; eauto] |
+ split; intros; Simpl; unfold compare_long; eauto]).
+Qed.
+
+Lemma swap_comparison_cmpfs:
+ forall v1 v2 cmp,
+ Val.lessdef (Val.cmpfs cmp v1 v2) (Val.cmpfs (swap_comparison cmp) v2 v1).
+Proof.
+ intros. unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct v1; destruct v2; auto.
+ rewrite Float32.cmp_swap. auto.
+Qed.
+
+Lemma transl_cond_float32_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_float32 cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.cmpfs cmp rs#r1 rs#r2) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl. apply swap_comparison_cmpfs.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl. apply swap_comparison_cmpfs.
+- econstructor; split. apply exec_straight_one; [simpl;
+ eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_cond_nofloat32_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_notfloat32 cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.of_optbool (option_map negb (Val.cmpfs_bool cmp (rs r1) (rs r2)))) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto.
+ rewrite Float32.cmp_ne_eq. auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto.
+ rewrite Float32.cmp_ne_eq. simpl. destruct (Float32.cmp Ceq f f0); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ destruct (Float32.cmp Clt f f0); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ replace (Cge) with (swap_comparison Cle); auto. rewrite Float32.cmp_swap.
+ destruct (Float32.cmp _ _ _); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ replace (Clt) with (swap_comparison Cgt); auto. rewrite Float32.cmp_swap.
+ destruct (Float32.cmp _ _ _); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpfs. unfold Val.cmpfs_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ destruct (Float32.cmp _ _ _); auto.
+Qed.
+
+Lemma swap_comparison_cmpf:
+ forall v1 v2 cmp,
+ Val.lessdef (Val.cmpf cmp v1 v2) (Val.cmpf (swap_comparison cmp) v2 v1).
+Proof.
+ intros. unfold Val.cmpf. unfold Val.cmpf_bool. destruct v1; destruct v2; auto.
+ rewrite Float.cmp_swap. auto.
+Qed.
+
+Lemma transl_cond_float64_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_float64 cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.cmpf cmp rs#r1 rs#r2) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl. apply swap_comparison_cmpf.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl. apply swap_comparison_cmpf.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+Qed.
+
+Lemma transl_cond_nofloat64_correct:
+ forall cmp rd r1 r2 k rs m,
+ exists rs',
+ exec_straight ge (basics_to_code (transl_cond_notfloat64 cmp rd r1 r2 k)) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.of_optbool (option_map negb (Val.cmpf_bool cmp (rs r1) (rs r2)))) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros. destruct cmp; simpl.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto.
+ rewrite Float.cmp_ne_eq. auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto.
+ rewrite Float.cmp_ne_eq. simpl. destruct (Float.cmp Ceq f f0); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ destruct (Float.cmp Clt f f0); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ replace (Cge) with (swap_comparison Cle); auto. rewrite Float.cmp_swap.
+ destruct (Float.cmp _ _ _); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ replace (Clt) with (swap_comparison Cgt); auto. rewrite Float.cmp_swap.
+ destruct (Float.cmp _ _ _); auto.
+- econstructor; split. apply exec_straight_one; [simpl; eauto].
+ split; intros; Simpl.
+ unfold Val.cmpf. unfold Val.cmpf_bool. destruct (rs r1); auto. destruct (rs r2); auto. simpl.
+ destruct (Float.cmp _ _ _); auto.
+Qed.
+
+Lemma transl_cond_op_correct:
+ forall cond rd args k c rs m,
+ transl_cond_op cond rd args k = OK c ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef (Val.of_optbool (eval_condition cond (map rs (map preg_of args)) m)) rs'#rd
+ /\ forall r, r <> PC -> r <> rd -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ assert (MKTOT: forall ob, Val.of_optbool ob = Val.maketotal (option_map Val.of_bool ob)).
+ { destruct ob as [[]|]; reflexivity. }
+ intros until m; intros TR.
+ destruct cond; simpl in TR; ArgsInv.
++ (* cmp *)
+ exploit transl_cond_int32s_correct; eauto. simpl. intros (rs' & A & B & C). exists rs'; eauto.
++ (* cmpu *)
+ exploit transl_cond_int32u_correct; eauto. simpl. intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto. rewrite B; eapply Val.mxcmpu_correct.
++ (* cmpimm *)
+ apply transl_condimm_int32s_correct; eauto with asmgen.
++ (* cmpuimm *)
+ apply transl_condimm_int32u_correct; eauto with asmgen.
++ (* cmpl *)
+ exploit transl_cond_int64s_correct; eauto. simpl. intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
++ (* cmplu *)
+ exploit transl_cond_int64u_correct; eauto. simpl. intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto. rewrite B, MKTOT; eauto.
+ eapply Val.mxcmplu_correct.
++ (* cmplimm *)
+ exploit transl_condimm_int64s_correct; eauto. instantiate (1 := x); eauto with asmgen. simpl.
+ intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
++ (* cmpluimm *)
+ exploit transl_condimm_int64u_correct; eauto. instantiate (1 := x); eauto with asmgen. simpl.
+ intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
++ (* cmpfloat *)
+ exploit transl_cond_float64_correct; eauto. intros (rs' & A & B & C). exists rs'; eauto.
++ (* cmpnosingle *)
+ exploit transl_cond_nofloat64_correct; eauto. intros (rs' & A & B & C). exists rs'; eauto.
++ (* cmpsingle *)
+ exploit transl_cond_float32_correct; eauto. intros (rs' & A & B & C). exists rs'; eauto.
++ (* cmpnosingle *)
+ exploit transl_cond_nofloat32_correct; eauto. intros (rs' & A & B & C). exists rs'; eauto.
+Qed.
+
+(* Translation of arithmetic operations *)
+
+Ltac SimplEval H :=
+ match type of H with
+ | Some _ = None _ => discriminate
+ | Some _ = Some _ => inv H
+ | ?a = Some ?b => let A := fresh in assert (A: Val.maketotal a = b) by (rewrite H; reflexivity)
+end.
+
+Ltac TranslOpSimpl :=
+ econstructor; split;
+ [ apply exec_straight_one; reflexivity
+ | split; [ apply Val.lessdef_same; simpl; Simpl; fail | intros; simpl; Simpl; fail ] ].
+
+Lemma int_eq_comm:
+ forall (x y: int),
+ (Int.eq x y) = (Int.eq y x).
+Proof.
+ intros.
+ unfold Int.eq.
+ unfold zeq.
+ destruct (Z.eq_dec _ _); destruct (Z.eq_dec _ _); congruence.
+Qed.
+
+Lemma int64_eq_comm:
+ forall (x y: int64),
+ (Int64.eq x y) = (Int64.eq y x).
+Proof.
+ intros.
+ unfold Int64.eq.
+ unfold zeq.
+ destruct (Z.eq_dec _ _); destruct (Z.eq_dec _ _); congruence.
+Qed.
+
+Lemma select_same_lessdef:
+ forall ty c v,
+ Val.lessdef (Val.select c v v ty) v.
+Proof.
+ intros.
+ unfold Val.select.
+ destruct c; try econstructor.
+ replace (if b then v else v) with v by (destruct b ; trivial).
+ destruct v; destruct ty; simpl; econstructor.
+Qed.
+
+Lemma if_neg : forall X,
+ forall a,
+ forall b c : X,
+ (if (negb a) then b else c) = (if a then c else b).
+Proof.
+ destruct a; reflexivity.
+Qed.
+
+Lemma int_ltu_to_neq:
+ forall x,
+ Int.ltu Int.zero x = negb (Int.eq x Int.zero).
+Proof.
+ intros.
+ unfold Int.ltu, Int.eq.
+ change (Int.unsigned Int.zero) with 0.
+ pose proof (Int.unsigned_range x) as RANGE.
+ unfold zlt, zeq.
+ destruct (Z_lt_dec _ _); destruct (Z.eq_dec _ _); trivial; lia.
+Qed.
+
+Lemma int64_ltu_to_neq:
+ forall x,
+ Int64.ltu Int64.zero x = negb (Int64.eq x Int64.zero).
+Proof.
+ intros.
+ unfold Int64.ltu, Int64.eq.
+ change (Int64.unsigned Int64.zero) with 0.
+ pose proof (Int64.unsigned_range x) as RANGE.
+ unfold zlt, zeq.
+ destruct (Z_lt_dec _ _); destruct (Z.eq_dec _ _); trivial; lia.
+Qed.
+
+Ltac splitall := repeat match goal with |- _ /\ _ => split end.
+
+Lemma transl_op_correct:
+ forall op args res k (rs: regset) m v c,
+ transl_op op args res k = OK c ->
+ eval_operation ge (rs#SP) op (map rs (map preg_of args)) m = Some v ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ Val.lessdef v rs'#(preg_of res)
+ /\ forall r, data_preg r = true -> r <> preg_of res -> preg_notin r (destroyed_by_op op) -> rs' r = rs r.
+Proof.
+ assert (SAME: forall v1 v2, v1 = v2 -> Val.lessdef v2 v1). { intros; subst; auto. }
+Opaque Int.eq.
+ intros until c; intros TR EV.
+ unfold transl_op in TR; destruct op; ArgsInv; simpl in EV; SimplEval EV; try TranslOpSimpl.
+- (* Omove *)
+ destruct (preg_of res), (preg_of m0); inv TR; TranslOpSimpl.
+- (* Oaddrsymbol *)
+ destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
++ set (rs1 := (rs#x <- (Genv.symbol_address ge id Ptrofs.zero))).
+ exploit (addptrofs_correct x x ofs (basics_to_code k) rs1 m); eauto with asmgen.
+ intros (rs2 & A & B & C).
+ exists rs2; split.
+ apply exec_straight_step with rs1 m; auto.
+ split. replace ofs with (Ptrofs.add Ptrofs.zero ofs) by (apply Ptrofs.add_zero_l).
+ rewrite Genv.shift_symbol_address.
+ replace (rs1 x) with (Genv.symbol_address ge id Ptrofs.zero) in B by (unfold rs1; Simpl).
+ exact B.
+ intros. rewrite C by eauto with asmgen. unfold rs1; Simpl.
++ TranslOpSimpl.
+- (* Oaddrstack *)
+ exploit addptrofs_correct. instantiate (1 := SP); auto with asmgen. intros (rs' & A & B & C).
+ exists rs'; split; eauto. auto with asmgen.
+- (* Ocast8signed *)
+ econstructor; split.
+ eapply exec_straight_two. simpl;eauto. simpl;eauto.
+ repeat split; intros; simpl; Simpl.
+ assert (A: Int.ltu (Int.repr 24) Int.iwordsize = true) by auto.
+ destruct (rs x0); auto; simpl. rewrite A; simpl. Simpl. unfold Val.shr. rewrite A.
+ apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence.
+- (* Ocast16signed *)
+ econstructor; split.
+ eapply exec_straight_two. simpl;eauto. simpl;eauto.
+ repeat split; intros; Simpl.
+ assert (A: Int.ltu (Int.repr 16) Int.iwordsize = true) by auto.
+ destruct (rs x0); auto; simpl. rewrite A; simpl. Simpl. unfold Val.shr. rewrite A.
+ apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence.
+- (* Oshrximm *)
+ econstructor; split.
+ + apply exec_straight_one. simpl. eauto.
+ + repeat split.
+ * rewrite Pregmap.gss.
+ destruct (rs x0); simpl; trivial.
+ unfold Val.maketotal.
+ destruct (Int.ltu _ _); simpl; trivial.
+ * intros.
+ rewrite Pregmap.gso; trivial.
+- (* Oshrxlimm *)
+ econstructor; split.
+ + apply exec_straight_one. simpl. eauto.
+ + repeat split.
+ * rewrite Pregmap.gss.
+ destruct (rs x0); simpl; trivial.
+ unfold Val.maketotal.
+ destruct (Int.ltu _ _); simpl; trivial.
+ * intros.
+ rewrite Pregmap.gso; trivial.
+
+- (* Ocmp *)
+ exploit transl_cond_op_correct; eauto. intros (rs' & A & B & C).
+ exists rs'; repeat split; eauto with asmgen.
+
+- (* Osel *)
+ unfold conditional_move in *.
+ destruct (ireg_eq _ _).
+ {
+ subst x. inv EQ2.
+ econstructor; split.
+ {
+ apply exec_straight_one.
+ simpl. reflexivity.
+ }
+ split.
+ { apply select_same_lessdef. }
+ intros; trivial.
+ }
+
+ destruct c0; simpl in *.
+
+ all: destruct c.
+ all: simpl in *.
+ all: inv EQ2.
+ all: econstructor; splitall.
+ all: try apply exec_straight_one.
+ all: intros; simpl; trivial.
+ all: unfold Val.select, cmove, cmoveu; simpl.
+ all: destruct (rs x1); simpl; trivial.
+ all: try rewrite int_ltu_to_neq.
+ all: try rewrite int64_ltu_to_neq.
+ all: try change (Int64.eq Int64.zero Int64.zero) with true.
+ all: try destruct Archi.ptr64.
+ all: try rewrite Pregmap.gss.
+ all: repeat rewrite if_neg.
+ all: simpl.
+ all: try destruct (_ || _).
+ all: try apply Val.lessdef_normalize.
+ all: trivial. (* no more lessdef *)
+ all: apply Pregmap.gso; congruence.
+
+- (* Oselimm *)
+ unfold conditional_move_imm32 in *.
+ destruct c0; simpl in *.
+
+ all: destruct c.
+ all: simpl in *.
+ all: inv EQ0.
+ all: econstructor; splitall.
+ all: try apply exec_straight_one.
+ all: intros; simpl; trivial.
+ all: unfold Val.select, cmove, cmoveu; simpl.
+ all: destruct (rs x0); simpl; trivial.
+ all: try rewrite int_ltu_to_neq.
+ all: try rewrite int64_ltu_to_neq.
+ all: try change (Int64.eq Int64.zero Int64.zero) with true.
+ all: try destruct Archi.ptr64.
+ all: try rewrite Pregmap.gss.
+ all: repeat rewrite if_neg.
+ all: simpl.
+ all: try destruct (_ || _).
+ all: try apply Val.lessdef_normalize.
+ all: trivial. (* no more lessdef *)
+ all: apply Pregmap.gso; congruence.
+
+- (* Osellimm *)
+ unfold conditional_move_imm64 in *.
+ destruct c0; simpl in *.
+
+ all: destruct c.
+ all: simpl in *.
+ all: inv EQ0.
+ all: econstructor; splitall.
+ all: try apply exec_straight_one.
+ all: intros; simpl; trivial.
+ all: unfold Val.select, cmove, cmoveu; simpl.
+ all: destruct (rs x0); simpl; trivial.
+ all: try rewrite int_ltu_to_neq.
+ all: try rewrite int64_ltu_to_neq.
+ all: try change (Int64.eq Int64.zero Int64.zero) with true.
+ all: try destruct Archi.ptr64.
+ all: try rewrite Pregmap.gss.
+ all: repeat rewrite if_neg.
+ all: simpl.
+ all: try destruct (_ || _).
+ all: try apply Val.lessdef_normalize.
+ all: trivial. (* no more lessdef *)
+ all: apply Pregmap.gso; congruence.
+Qed.
+
+(** Memory accesses *)
+
+Lemma indexed_memory_access_correct:
+ forall mk_instr base ofs k rs m,
+ exists base' ofs' rs' ptr',
+ exec_straight_opt (indexed_memory_access mk_instr base ofs ::g k) rs m
+ (mk_instr base' ofs' ::g k) rs' m
+ /\ eval_offset ofs' = OK ptr'
+ /\ Val.offset_ptr rs'#base' ptr' = Val.offset_ptr rs#base ofs
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ unfold indexed_memory_access; intros.
+ (* destruct Archi.ptr64 eqn:SF. *)
+ assert (Archi.ptr64 = true) as SF; auto.
+- generalize (make_immed64_sound (Ptrofs.to_int64 ofs)); intros EQ.
+ destruct (make_immed64 (Ptrofs.to_int64 ofs)).
++ econstructor; econstructor; econstructor; econstructor; split.
+ apply exec_straight_opt_refl.
+ split; auto. simpl. subst imm. rewrite Ptrofs.of_int64_to_int64 by auto. auto.
+Qed.
+
+
+Lemma indexed_load_access_correct:
+ forall trap chunk (mk_instr: ireg -> offset -> basic) rd m,
+ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_load_offset trap chunk rs m rd base ofs) ->
+ forall (base: ireg) ofs k (rs: regset) v,
+ Mem.loadv chunk m (Val.offset_ptr rs#base ofs) = Some v ->
+ exists rs',
+ exec_straight ge (indexed_memory_access mk_instr base ofs ::g k) rs m k rs' m
+ /\ rs'#rd = v
+ /\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until m; intros EXEC; intros until v; intros LOAD.
+ exploit indexed_memory_access_correct; eauto.
+ intros (base' & ofs' & rs' & ptr' & A & PtrEq & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. rewrite EXEC.
+ unfold exec_load_offset. unfold parexec_load_offset. rewrite PtrEq. rewrite B, LOAD. eauto. Simpl.
+ split; intros; Simpl. auto.
+Qed.
+
+Lemma indexed_store_access_correct:
+ forall chunk (mk_instr: ireg -> offset -> basic) r1 m,
+ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_store_offset chunk rs m r1 base ofs) ->
+ forall (base: ireg) ofs k (rs: regset) m',
+ Mem.storev chunk m (Val.offset_ptr rs#base ofs) (rs#r1) = Some m' ->
+ exists rs',
+ exec_straight ge (indexed_memory_access mk_instr base ofs ::g k) rs m k rs' m'
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ intros until m; intros EXEC; intros until m'; intros STORE.
+ exploit indexed_memory_access_correct. (* instantiate (1 := base). eauto. *)
+ intros (base' & ofs' & rs' & ptr' & A & PtrEq & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eapply A. apply exec_straight_one. rewrite EXEC.
+ unfold exec_store_offset. unfold parexec_store_offset. rewrite PtrEq. rewrite B, C, STORE.
+ eauto.
+ discriminate.
+ auto.
+Qed.
+
+Lemma loadind_correct:
+ forall (base: ireg) ofs ty dst k c (rs: regset) m v,
+ loadind base ofs ty dst k = OK c ->
+ Mem.loadv (chunk_of_type ty) m (Val.offset_ptr rs#base ofs) = Some v ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#(preg_of dst) = v
+ /\ forall r, r <> PC -> r <> preg_of dst -> rs'#r = rs#r.
+Proof.
+ intros until v; intros TR LOAD.
+ assert (A: exists mk_instr rd,
+ preg_of dst = IR rd
+ /\ c = indexed_memory_access mk_instr base ofs :: k
+ /\ forall base' ofs' rs',
+ exec_basic_instr ge (mk_instr base' ofs') rs' m =
+ exec_load_offset TRAP (chunk_of_type ty) rs' m rd base' ofs').
+ { unfold loadind in TR.
+ destruct ty, (preg_of dst); inv TR; econstructor; esplit; eauto. }
+ destruct A as (mk_instr & rd & rdEq & B & C). subst c. rewrite rdEq.
+ eapply indexed_load_access_correct; eauto with asmgen.
+Qed.
+
+Lemma storeind_correct:
+ forall (base: ireg) ofs ty src k c (rs: regset) m m',
+ storeind src base ofs ty k = OK c ->
+ Mem.storev (chunk_of_type ty) m (Val.offset_ptr rs#base ofs) rs#(preg_of src) = Some m' ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m'
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ intros until m'; intros TR STORE.
+ assert (A: exists mk_instr rr,
+ preg_of src = IR rr
+ /\ c = indexed_memory_access mk_instr base ofs :: k
+ /\ forall base' ofs' rs',
+ exec_basic_instr ge (mk_instr base' ofs') rs' m =
+ exec_store_offset (chunk_of_type ty) rs' m rr base' ofs').
+ { unfold storeind in TR. destruct ty, (preg_of src); inv TR; econstructor; esplit; eauto. }
+ destruct A as (mk_instr & rr & rsEq & B & C). subst c.
+ eapply indexed_store_access_correct; eauto with asmgen.
+ congruence.
+Qed.
+
+Ltac bsimpl := unfold exec_bblock; simpl.
+
+Lemma Pget_correct:
+ forall (dst: gpreg) (src: preg) k (rs: regset) m,
+ src = RA ->
+ exists rs',
+ exec_straight ge (Pget dst src ::g k) rs m k rs' m
+ /\ rs'#dst = rs#src
+ /\ forall r, r <> PC -> r <> dst -> rs'#r = rs#r.
+Proof.
+ intros. econstructor; econstructor; econstructor.
+- rewrite H. bsimpl. auto.
+- Simpl.
+- intros. Simpl.
+Qed.
+
+Lemma Pset_correct:
+ forall (dst: preg) (src: gpreg) k (rs: regset) m,
+ dst = RA ->
+ exists rs',
+ exec_straight ge (Pset dst src ::g k) rs m k rs' m
+ /\ rs'#dst = rs#src
+ /\ forall r, r <> PC -> r <> dst -> rs'#r = rs#r.
+Proof.
+ intros. econstructor; econstructor; econstructor; simpl.
+ rewrite H. auto.
+ Simpl.
+ Simpl.
+ intros. rewrite H. Simpl.
+Qed.
+
+Lemma loadind_ptr_correct:
+ forall (base: ireg) ofs (dst: ireg) k (rs: regset) m v,
+ Mem.loadv Mptr m (Val.offset_ptr rs#base ofs) = Some v ->
+ exists rs',
+ exec_straight ge (loadind_ptr base ofs dst ::g k) rs m k rs' m
+ /\ rs'#dst = v
+ /\ forall r, r <> PC -> r <> dst -> rs'#r = rs#r.
+Proof.
+ intros. eapply indexed_load_access_correct; eauto with asmgen.
+ intros. unfold Mptr. assert (Archi.ptr64 = true). auto. rewrite H0.
+ instantiate (1 := TRAP).
+ auto.
+Qed.
+
+Lemma storeind_ptr_correct:
+ forall (base: ireg) ofs (src: ireg) k (rs: regset) m m',
+ Mem.storev Mptr m (Val.offset_ptr rs#base ofs) rs#src = Some m' ->
+ exists rs',
+ exec_straight ge (storeind_ptr src base ofs ::g k) rs m k rs' m'
+ /\ forall r, r <> PC -> rs'#r = rs#r.
+Proof.
+ intros. eapply indexed_store_access_correct with (r1 := src); eauto with asmgen.
+ intros. unfold Mptr. assert (Archi.ptr64 = true); auto.
+Qed.
+
+Lemma transl_memory_access_correct:
+ forall mk_instr addr args k c (rs: regset) m v,
+ transl_memory_access mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ exists base ofs rs' ptr,
+ exec_straight_opt (basics_to_code c) rs m (mk_instr base ofs ::g (basics_to_code k)) rs' m
+ /\ eval_offset ofs = OK ptr
+ /\ Val.offset_ptr rs'#base ptr = v
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until v; intros TR EV.
+ unfold transl_memory_access in TR; destruct addr; ArgsInv.
+- (* indexed *)
+ inv EV. exploit indexed_memory_access_correct; eauto. intros (base' & ofs' & rs' & ptr' & EOPT & EVALOFF & VALOFF & RSEQ).
+ eexists; eexists; eexists; eexists. split; try split; try split.
+ eapply EOPT. unfold eval_offset in EVALOFF. inv EVALOFF. eauto.
+ { intros. destruct r; rewrite RSEQ; auto. }
+- (* global *)
+ simpl in EV. inv EV. inv TR. econstructor; econstructor; econstructor; econstructor; split.
+ constructor. apply exec_straight_one. simpl; eauto. auto.
+ split; split; intros; Simpl.
+ assert (Val.lessdef (Val.offset_ptr (Genv.symbol_address ge i i0) Ptrofs.zero) (Genv.symbol_address ge i i0)).
+ { apply Val.offset_ptr_zero. }
+ remember (Genv.symbol_address ge i i0) as symbol.
+ destruct symbol; auto.
+ + contradict Heqsymbol; unfold Genv.symbol_address.
+ destruct (Genv.find_symbol ge i); discriminate.
+ + contradict Heqsymbol; unfold Genv.symbol_address;
+ destruct (Genv.find_symbol ge i); discriminate.
+ + contradict Heqsymbol; unfold Genv.symbol_address;
+ destruct (Genv.find_symbol ge i); discriminate.
+ + contradict Heqsymbol; unfold Genv.symbol_address;
+ destruct (Genv.find_symbol ge i); discriminate.
+ + simpl. rewrite Ptrofs.add_zero; auto.
+- (* stack *)
+ inv TR. inv EV.
+ exploit indexed_memory_access_correct; eauto. intros (base' & ofs' & rs' & ptr' & EOPT & EVALOFF & VALOFF & RSEQ).
+ eexists; eexists; eexists; eexists. split; try split; try split.
+ eapply EOPT. unfold eval_offset in EVALOFF. inv EVALOFF. eauto.
+ { intros. destruct r; rewrite RSEQ; auto. }
+Qed.
+
+Lemma transl_memory_access2_correct:
+ forall mk_instr addr args k c (rs: regset) m v,
+ transl_memory_access2 mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ exists base ro mro mr1 rs',
+ args = mr1 :: mro :: nil
+ /\ ireg_of mro = OK ro
+ /\ exec_straight_opt (basics_to_code c) rs m (mk_instr base ro ::g (basics_to_code k)) rs' m
+ /\ Val.addl rs'#base rs'#ro = v
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until v; intros TR EV.
+ unfold transl_memory_access2 in TR; destruct addr; ArgsInv.
+ inv EV. repeat eexists. eassumption. econstructor; eauto.
+Qed.
+
+Lemma transl_memory_access2XS_correct:
+ forall chunk mk_instr (scale : Z) args k c (rs: regset) m v,
+ transl_memory_access2XS chunk mk_instr scale args k = OK c ->
+ eval_addressing ge rs#SP (Aindexed2XS scale) (map rs (map preg_of args)) = Some v ->
+ exists base ro mro mr1 rs',
+ args = mr1 :: mro :: nil
+ /\ ireg_of mro = OK ro
+ /\ exec_straight_opt (basics_to_code c) rs m (mk_instr base ro ::g (basics_to_code k)) rs' m
+ /\ Val.addl rs'#base (Val.shll rs'#ro (Vint (Int.repr scale))) = v
+ /\ (forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r)
+ /\ scale = (zscale_of_chunk chunk).
+Proof.
+ intros until v; intros TR EV.
+ unfold transl_memory_access2XS in TR; ArgsInv.
+ inv EV. repeat eexists. eassumption. econstructor; eauto.
+ symmetry.
+ apply Z.eqb_eq.
+ assumption.
+Qed.
+
+Lemma transl_load_access2_correct:
+ forall trap chunk (mk_instr: ireg -> ireg -> basic) addr args k c rd (rs: regset) m v mro mr1 ro v',
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_reg trap chunk rs m rd base ro) ->
+ transl_memory_access2 mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = Some v' ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = v'
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until v'; intros ARGS IREGE INSTR TR EV LOAD.
+ exploit transl_memory_access2_correct; eauto.
+ intros (base & ro2 & mro2 & mr2 & rs' & ARGSS & IREGEQ & A & B & C). rewrite ARGSS in ARGS. inversion ARGS. subst mr2 mro2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_load_reg. unfold parexec_load_reg. rewrite B, LOAD. reflexivity. Simpl.
+ split; intros; Simpl. auto.
+Qed.
+
+Lemma transl_load_access2_correct_notrap2:
+ forall chunk (mk_instr: ireg -> ireg -> basic) addr args k c rd (rs: regset) m v mro mr1 ro,
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_reg NOTRAP chunk rs m rd base ro) ->
+ transl_memory_access2 mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = None ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = Vundef
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until ro; intros ARGS IREGE INSTR TR EV LOAD.
+ exploit transl_memory_access2_correct; eauto.
+ intros (base & ro2 & mro2 & mr2 & rs' & ARGSS & IREGEQ & A & B & C). rewrite ARGSS in ARGS. inversion ARGS. subst mr2 mro2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_load_reg. unfold parexec_load_reg. rewrite B, LOAD. reflexivity. Simpl.
+ split; intros; Simpl. auto.
+Qed.
+
+Lemma transl_load_access2XS_correct:
+ forall trap chunk (mk_instr: ireg -> ireg -> basic) (scale : Z) args k c rd (rs: regset) m v mro mr1 ro v',
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_regxs trap chunk rs m rd base ro) ->
+ transl_memory_access2XS chunk mk_instr scale args k = OK c ->
+ eval_addressing ge rs#SP (Aindexed2XS scale) (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = Some v' ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = v'
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until v'; intros ARGS IREGE INSTR TR EV LOAD.
+ exploit transl_memory_access2XS_correct; eauto.
+ intros (base & ro2 & mro2 & mr2 & rs' & ARGSS & IREGEQ & A & B & C & D). rewrite ARGSS in ARGS. inversion ARGS. subst mr2 mro2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_load_regxs. unfold parexec_load_regxs.
+ unfold scale_of_chunk.
+ subst scale.
+ rewrite B, LOAD. reflexivity. Simpl.
+ split. trivial. intros. Simpl.
+Qed.
+
+Lemma transl_load_access2XS_correct_notrap2:
+ forall chunk (mk_instr: ireg -> ireg -> basic) (scale : Z) args k c rd (rs: regset) m v mro mr1 ro,
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_regxs NOTRAP chunk rs m rd base ro) ->
+ transl_memory_access2XS chunk mk_instr scale args k = OK c ->
+ eval_addressing ge rs#SP (Aindexed2XS scale) (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = None ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = Vundef
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until ro; intros ARGS IREGE INSTR TR EV LOAD.
+ exploit transl_memory_access2XS_correct; eauto.
+ intros (base & ro2 & mro2 & mr2 & rs' & ARGSS & IREGEQ & A & B & C & D). rewrite ARGSS in ARGS. inversion ARGS. subst mr2 mro2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_load_regxs. unfold parexec_load_regxs.
+ unfold scale_of_chunk.
+ subst scale.
+ rewrite B, LOAD. reflexivity. Simpl.
+ split. trivial. intros. Simpl.
+Qed.
+
+Lemma transl_load_access_correct:
+ forall trap chunk (mk_instr: ireg -> offset -> basic) addr args k c rd (rs: regset) m v v',
+ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_load_offset trap chunk rs m rd base ofs) ->
+ transl_memory_access mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = Some v' ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = v'
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until v'; intros INSTR TR EV LOAD.
+ exploit transl_memory_access_correct; eauto.
+ intros (base & ofs & rs' & ptr & A & PtrEq & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one.
+ rewrite INSTR. unfold exec_load_offset. unfold parexec_load_offset. rewrite PtrEq, B, LOAD. reflexivity. Simpl.
+ split; intros; Simpl. auto.
+Qed.
+
+Lemma transl_load_access_correct_notrap2:
+ forall chunk (mk_instr: ireg -> offset -> basic) addr args k c rd (rs: regset) m v,
+ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_load_offset NOTRAP chunk rs m rd base ofs) ->
+ transl_memory_access mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.loadv chunk m v = None ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#rd = Vundef
+ /\ forall r, r <> PC -> r <> RTMP -> r <> rd -> rs'#r = rs#r.
+Proof.
+ intros until v; intros INSTR TR EV LOAD.
+ exploit transl_memory_access_correct; eauto.
+ intros (base & ofs & rs' & ptr & A & PtrEq & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one.
+ rewrite INSTR. unfold exec_load_offset. unfold parexec_load_offset. rewrite PtrEq, B, LOAD. reflexivity. Simpl.
+ split. trivial. intros. Simpl.
+Qed.
+
+Lemma transl_load_memory_access_ok:
+ forall addr trap chunk args dst k c rs a v m,
+ (match addr with Aindexed2XS _ | Aindexed2 => False | _ => True end) ->
+ transl_load trap chunk addr args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) addr (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ exists mk_instr rd,
+ preg_of dst = IR rd
+ /\ transl_memory_access mk_instr addr args k = OK c
+ /\ forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_load_offset trap chunk rs m rd base ofs.
+Proof.
+ intros until m. intros ADDR TR ? ?.
+ unfold transl_load in TR. destruct addr; try contradiction.
+ - monadInv TR. destruct chunk; ArgsInv; econstructor; (esplit; eauto).
+ - monadInv TR. destruct chunk. all: ArgsInv; destruct args; try discriminate; monadInv EQ0; eexists; eexists; split; try split;
+ [ instantiate (1 := (PLoadRRO _ _ x)); simpl; reflexivity
+ | eauto ].
+ - monadInv TR. destruct chunk. all: ArgsInv; destruct args; try discriminate; monadInv EQ0; eexists; eexists; split; try split;
+ [ instantiate (1 := (PLoadRRO _ _ x)); simpl; reflexivity
+ | eauto ].
+Qed.
+
+Lemma transl_load_memory_access_ok_notrap2:
+ forall addr chunk args dst k c rs a m,
+ (match addr with Aindexed2XS _ | Aindexed2 => False | _ => True end) ->
+ transl_load NOTRAP chunk addr args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) addr (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = None ->
+ exists mk_instr rd,
+ preg_of dst = IR rd
+ /\ transl_memory_access mk_instr addr args k = OK c
+ /\ forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_load_offset NOTRAP chunk rs m rd base ofs.
+Proof.
+ intros until m. intros ADDR TR ? ?.
+ unfold transl_load in TR. destruct addr; try contradiction.
+ - monadInv TR. destruct chunk; ArgsInv; econstructor; (esplit; eauto).
+ - monadInv TR. destruct chunk. all: ArgsInv; destruct args; try discriminate; monadInv EQ0; eexists; eexists; split; try split;
+ [ instantiate (1 := (PLoadRRO _ _ x)); simpl; reflexivity
+ | eauto ].
+ - monadInv TR. destruct chunk. all: ArgsInv; destruct args; try discriminate; monadInv EQ0; eexists; eexists; split; try split;
+ [ instantiate (1 := (PLoadRRO _ _ x)); simpl; reflexivity
+ | eauto ].
+Qed.
+
+Lemma transl_load_memory_access2_ok:
+ forall trap chunk args dst k c rs a v m,
+ transl_load trap chunk Aindexed2 args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) Aindexed2 (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ exists mk_instr mr0 mro rd ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of dst = IR rd
+ /\ preg_of mro = IR ro
+ /\ transl_memory_access2 mk_instr Aindexed2 args k = OK c
+ /\ forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_reg trap chunk rs m rd base ro.
+Proof.
+ intros until m. intros TR ? ?.
+ unfold transl_load in TR. subst. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2 in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ unfold ireg_of in EQ0; destruct (preg_of m1); eauto; try discriminate; monadInv EQ0; reflexivity
+ | rewrite EQ1; rewrite EQ0; simpl; instantiate (1 := (PLoadRRR _ _ x)); simpl; reflexivity
+ | eauto].
+Qed.
+
+
+Lemma transl_load_memory_access2_ok_notrap2:
+ forall chunk args dst k c rs a m,
+ transl_load NOTRAP chunk Aindexed2 args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) Aindexed2 (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = None ->
+ exists mk_instr mr0 mro rd ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of dst = IR rd
+ /\ preg_of mro = IR ro
+ /\ transl_memory_access2 mk_instr Aindexed2 args k = OK c
+ /\ forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_reg NOTRAP chunk rs m rd base ro.
+Proof.
+ intros until m. intros TR ? ?.
+ unfold transl_load in TR. subst. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2 in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ unfold ireg_of in EQ0; destruct (preg_of m1); eauto; try discriminate; monadInv EQ0; reflexivity
+ | rewrite EQ1; rewrite EQ0; simpl; instantiate (1 := (PLoadRRR _ _ x)); simpl; reflexivity
+ | eauto].
+Qed.
+
+Lemma transl_load_memory_access2XS_ok:
+ forall scale trap chunk args dst k c rs a v m,
+ transl_load trap chunk (Aindexed2XS scale) args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) (Aindexed2XS scale) (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ exists mk_instr mr0 mro rd ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of dst = IR rd
+ /\ preg_of mro = IR ro
+ /\ transl_memory_access2XS chunk mk_instr scale args k = OK c
+ /\ forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_regxs trap chunk rs m rd base ro.
+Proof.
+ intros until m. intros TR ? ?.
+ unfold transl_load in TR. subst. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2XS in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ unfold ireg_of in EQ0; destruct (preg_of m1); eauto; try discriminate; monadInv EQ0; reflexivity
+ | rewrite EQ1; rewrite EQ0; simpl; instantiate (1 := (PLoadRRRXS _ _ x)); simpl; rewrite Heqb; eauto
+ | eauto].
+Qed.
+
+
+Lemma transl_load_memory_access2XS_ok_notrap2:
+ forall scale chunk args dst k c rs a m,
+ transl_load NOTRAP chunk (Aindexed2XS scale) args dst k = OK c ->
+ eval_addressing ge (rs (IR SP)) (Aindexed2XS scale) (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = None ->
+ exists mk_instr mr0 mro rd ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of dst = IR rd
+ /\ preg_of mro = IR ro
+ /\ transl_memory_access2XS chunk mk_instr scale args k = OK c
+ /\ forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_load_regxs NOTRAP chunk rs m rd base ro.
+Proof.
+ intros until m. intros TR ? ?.
+ unfold transl_load in TR. subst. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2XS in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ unfold ireg_of in EQ0; destruct (preg_of m1); eauto; try discriminate; monadInv EQ0; reflexivity
+ | rewrite EQ1; rewrite EQ0; simpl; instantiate (1 := (PLoadRRRXS _ _ x)); simpl; rewrite Heqb; eauto
+ | eauto].
+Qed.
+
+Lemma transl_load_correct:
+ forall trap chunk addr args dst k c (rs: regset) m a v,
+ transl_load trap chunk addr args dst k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#(preg_of dst) = v
+ /\ forall r, r <> PC -> r <> RTMP -> r <> preg_of dst -> rs'#r = rs#r.
+Proof.
+ intros until v; intros TR EV LOAD. destruct addr.
+ - exploit transl_load_memory_access2XS_ok; eauto. intros (mk_instr & mr0 & mro & rd & ro & argsEq & rdEq & roEq & B & C).
+ rewrite rdEq. eapply transl_load_access2XS_correct; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity.
+ - exploit transl_load_memory_access2_ok; eauto. intros (mk_instr & mr0 & mro & rd & ro & argsEq & rdEq & roEq & B & C).
+ rewrite rdEq. eapply transl_load_access2_correct; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity.
+ - exploit transl_load_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct; eauto with asmgen.
+ - exploit transl_load_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct; eauto with asmgen.
+ - exploit transl_load_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct; eauto with asmgen.
+Qed.
+
+Lemma transl_load_correct_notrap2:
+ forall chunk addr args dst k c (rs: regset) m a,
+ transl_load NOTRAP chunk addr args dst k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some a ->
+ Mem.loadv chunk m a = None ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m
+ /\ rs'#(preg_of dst) = Vundef
+ /\ forall r, r <> PC -> r <> RTMP -> r <> preg_of dst -> rs'#r = rs#r.
+Proof.
+ intros until a; intros TR EV LOAD. destruct addr.
+ - exploit transl_load_memory_access2XS_ok_notrap2; eauto. intros (mk_instr & mr0 & mro & rd & ro & argsEq & rdEq & roEq & B & C).
+ rewrite rdEq. eapply transl_load_access2XS_correct_notrap2; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity.
+ - exploit transl_load_memory_access2_ok_notrap2; eauto. intros (mk_instr & mr0 & mro & rd & ro & argsEq & rdEq & roEq & B & C).
+ rewrite rdEq. eapply transl_load_access2_correct_notrap2; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity.
+ - exploit transl_load_memory_access_ok_notrap2; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct_notrap2; eauto with asmgen.
+ - exploit transl_load_memory_access_ok_notrap2; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct_notrap2; eauto with asmgen.
+ - exploit transl_load_memory_access_ok_notrap2; eauto; try discriminate; try (simpl; reflexivity).
+ intros A; destruct A as (mk_instr & rd & rdEq & B & C); rewrite rdEq;
+ eapply transl_load_access_correct_notrap2; eauto with asmgen.
+Qed.
+
+Lemma transl_store_access2_correct:
+ forall chunk (mk_instr: ireg -> ireg -> basic) addr args k c r1 (rs: regset) m v mr1 mro ro m',
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_store_reg chunk rs m r1 base ro) ->
+ transl_memory_access2 mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.storev chunk m v rs#r1 = Some m' ->
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until m'; intros ARGS IREG INSTR TR EV STORE NOT31.
+ exploit transl_memory_access2_correct; eauto.
+ intros (base & ro2 & mr2 & mro2 & rs' & ARGSS & IREGG & A & B & C). rewrite ARGSS in ARGS. inversion ARGS. subst mro2 mr2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_store_reg. unfold parexec_store_reg. rewrite B. rewrite C; try discriminate. rewrite STORE. auto.
+ intro. inv H. contradiction.
+ auto.
+Qed.
+
+Lemma transl_store_access2XS_correct:
+ forall chunk (mk_instr: ireg -> ireg -> basic) scale args k c r1 (rs: regset) m v mr1 mro ro m',
+ args = mr1 :: mro :: nil ->
+ ireg_of mro = OK ro ->
+ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_store_regxs chunk rs m r1 base ro) ->
+ transl_memory_access2XS chunk mk_instr scale args k = OK c ->
+ eval_addressing ge rs#SP (Aindexed2XS scale) (map rs (map preg_of args)) = Some v ->
+ Mem.storev chunk m v rs#r1 = Some m' ->
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until m'; intros ARGS IREG INSTR TR EV STORE NOT31.
+ exploit transl_memory_access2XS_correct; eauto.
+ intros (base & ro2 & mr2 & mro2 & rs' & ARGSS & IREGG & A & B & C & D). rewrite ARGSS in ARGS. inversion ARGS. subst mro2 mr2. clear ARGS.
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one. assert (ro = ro2) by congruence. subst ro2.
+ rewrite INSTR. unfold exec_store_regxs. unfold parexec_store_regxs.
+ unfold scale_of_chunk.
+ subst scale.
+ rewrite B. rewrite C; try discriminate. rewrite STORE. auto.
+ intro. inv H. contradiction.
+ auto.
+Qed.
+
+Lemma transl_store_access_correct:
+ forall chunk (mk_instr: ireg -> offset -> basic) addr args k c r1 (rs: regset) m v m',
+ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_store_offset chunk rs m r1 base ofs) ->
+ transl_memory_access mk_instr addr args k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some v ->
+ Mem.storev chunk m v rs#r1 = Some m' ->
+ r1 <> RTMP ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until m'; intros INSTR TR EV STORE NOT31.
+ exploit transl_memory_access_correct; eauto.
+ intros (base & ofs & rs' & ptr & A & PtrEq & B & C).
+ econstructor; split.
+ eapply exec_straight_opt_right. eexact A. apply exec_straight_one.
+ rewrite INSTR. unfold exec_store_offset. unfold parexec_store_offset. rewrite PtrEq, B. rewrite C; try discriminate. rewrite STORE. auto.
+ intro. inv H. contradiction.
+ auto.
+Qed.
+
+
+Remark exec_store_offset_8_sign rs m x base ofs:
+ exec_store_offset Mint8unsigned rs m x base ofs = exec_store_offset Mint8signed rs m x base ofs.
+Proof.
+ unfold exec_store_offset. unfold parexec_store_offset. unfold eval_offset; auto. unfold Mem.storev.
+ destruct (Val.offset_ptr _ _); auto. erewrite <- Mem.store_signed_unsigned_8. reflexivity.
+Qed.
+
+Remark exec_store_offset_16_sign rs m x base ofs:
+ exec_store_offset Mint16unsigned rs m x base ofs = exec_store_offset Mint16signed rs m x base ofs.
+Proof.
+ unfold exec_store_offset. unfold parexec_store_offset. unfold eval_offset; auto. unfold Mem.storev.
+ destruct (Val.offset_ptr _ _); auto. erewrite <- Mem.store_signed_unsigned_16. reflexivity.
+Qed.
+
+Lemma transl_store_memory_access_ok:
+ forall addr chunk args src k c rs a m m',
+ (match addr with Aindexed2XS _ | Aindexed2 => False | _ => True end) ->
+ transl_store chunk addr args src k = OK c ->
+ eval_addressing ge (rs (IR SP)) addr (map rs (map preg_of args)) = Some a ->
+ Mem.storev chunk m a (rs (preg_of src)) = Some m' ->
+ exists mk_instr chunk' rr,
+ preg_of src = IR rr
+ /\ transl_memory_access mk_instr addr args k = OK c
+ /\ (forall base ofs rs,
+ exec_basic_instr ge (mk_instr base ofs) rs m = exec_store_offset chunk' rs m rr base ofs)
+ /\ Mem.storev chunk m a rs#(preg_of src) = Mem.storev chunk' m a rs#(preg_of src).
+Proof.
+ intros until m'. intros ? TR ? ?.
+ unfold transl_store in TR. destruct addr; try contradiction.
+ - monadInv TR. destruct chunk. all:
+ ArgsInv; eexists; eexists; eexists; split; try split; [
+ repeat (destruct args; try discriminate); eassumption
+ | split; eauto; intros; simpl; try reflexivity].
+ eapply exec_store_offset_8_sign.
+ eapply exec_store_offset_16_sign.
+ - monadInv TR. destruct chunk. all:
+ ArgsInv; eexists; eexists; eexists; split; try split;
+ [ repeat (destruct args; try discriminate); instantiate (1 := PStoreRRO _ x); simpl; eassumption
+ | split; eauto; intros; simpl; try reflexivity].
+ eapply exec_store_offset_8_sign.
+ eapply exec_store_offset_16_sign.
+ - monadInv TR. destruct chunk. all:
+ ArgsInv; eexists; eexists; eexists; split; try split;
+ [ repeat (destruct args; try discriminate); instantiate (1 := PStoreRRO _ x); simpl; eassumption
+ | split; eauto; intros; simpl; try reflexivity].
+ eapply exec_store_offset_8_sign.
+ eapply exec_store_offset_16_sign.
+Qed.
+
+Remark exec_store_reg_8_sign rs m x base ofs:
+ exec_store_reg Mint8unsigned rs m x base ofs = exec_store_reg Mint8signed rs m x base ofs.
+Proof.
+ unfold exec_store_reg. unfold parexec_store_reg. unfold Mem.storev. destruct (Val.addl _ _); auto.
+ erewrite <- Mem.store_signed_unsigned_8. reflexivity.
+Qed.
+
+Remark exec_store_reg_16_sign rs m x base ofs:
+ exec_store_reg Mint16unsigned rs m x base ofs = exec_store_reg Mint16signed rs m x base ofs.
+Proof.
+ unfold exec_store_reg. unfold parexec_store_reg. unfold Mem.storev. destruct (Val.addl _ _); auto.
+ erewrite <- Mem.store_signed_unsigned_16. reflexivity.
+Qed.
+
+Remark exec_store_regxs_8_sign rs m x base ofs:
+ exec_store_regxs Mint8unsigned rs m x base ofs = exec_store_regxs Mint8signed rs m x base ofs.
+Proof.
+ unfold exec_store_regxs. unfold parexec_store_regxs. unfold Mem.storev. destruct (Val.addl _ _); auto.
+ erewrite <- Mem.store_signed_unsigned_8. reflexivity.
+Qed.
+
+Remark exec_store_regxs_16_sign rs m x base ofs:
+ exec_store_regxs Mint16unsigned rs m x base ofs = exec_store_regxs Mint16signed rs m x base ofs.
+Proof.
+ unfold exec_store_regxs. unfold parexec_store_regxs. unfold Mem.storev. destruct (Val.addl _ _); auto.
+ erewrite <- Mem.store_signed_unsigned_16. reflexivity.
+Qed.
+
+Lemma transl_store_memory_access2_ok:
+ forall addr chunk args src k c rs a m m',
+ addr = Aindexed2 ->
+ transl_store chunk addr args src k = OK c ->
+ eval_addressing ge (rs (IR SP)) addr (map rs (map preg_of args)) = Some a ->
+ Mem.storev chunk m a (rs (preg_of src)) = Some m' ->
+ exists mk_instr chunk' rr mr0 mro ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of mro = IR ro
+ /\ preg_of src = IR rr
+ /\ transl_memory_access2 mk_instr addr args k = OK c
+ /\ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_store_reg chunk' rs m rr base ro)
+ /\ Mem.storev chunk m a rs#(preg_of src) = Mem.storev chunk' m a rs#(preg_of src).
+Proof.
+ intros until m'. intros ? TR ? ?.
+ unfold transl_store in TR. subst addr. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2 in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ ArgsInv; reflexivity
+ | rewrite EQ1; rewrite EQ0; instantiate (1 := (PStoreRRR _ x)); simpl; reflexivity
+ | eauto ].
+ - simpl. intros. eapply exec_store_reg_8_sign.
+ - simpl. intros. eapply exec_store_reg_16_sign.
+Qed.
+
+Lemma transl_store_memory_access2XS_ok:
+ forall scale chunk args src k c rs a m m',
+ transl_store chunk (Aindexed2XS scale) args src k = OK c ->
+ eval_addressing ge (rs (IR SP)) (Aindexed2XS scale) (map rs (map preg_of args)) = Some a ->
+ Mem.storev chunk m a (rs (preg_of src)) = Some m' ->
+ exists mk_instr chunk' rr mr0 mro ro,
+ args = mr0 :: mro :: nil
+ /\ preg_of mro = IR ro
+ /\ preg_of src = IR rr
+ /\ transl_memory_access2XS chunk' mk_instr scale args k = OK c
+ /\ (forall base rs,
+ exec_basic_instr ge (mk_instr base ro) rs m = exec_store_regxs chunk' rs m rr base ro)
+ /\ Mem.storev chunk m a rs#(preg_of src) = Mem.storev chunk' m a rs#(preg_of src).
+Proof.
+ intros until m'. intros TR ? ?.
+ unfold transl_store in TR. monadInv TR. destruct chunk. all:
+ unfold transl_memory_access2XS in EQ0; repeat (destruct args; try discriminate); monadInv EQ0; ArgsInv; repeat eexists;
+ [ ArgsInv; reflexivity
+ | rewrite EQ1; rewrite EQ0; instantiate (1 := (PStoreRRRXS _ x)); simpl; rewrite Heqb; eauto
+ | eauto ].
+ - simpl. intros. eapply exec_store_regxs_8_sign.
+ - simpl. intros. eapply exec_store_regxs_16_sign.
+Qed.
+
+Lemma transl_store_correct:
+ forall chunk addr args src k c (rs: regset) m a m',
+ transl_store chunk addr args src k = OK c ->
+ eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some a ->
+ Mem.storev chunk m a rs#(preg_of src) = Some m' ->
+ exists rs',
+ exec_straight ge (basics_to_code c) rs m (basics_to_code k) rs' m'
+ /\ forall r, r <> PC -> r <> RTMP -> rs'#r = rs#r.
+Proof.
+ intros until m'; intros TR EV STORE. destruct addr.
+ - exploit transl_store_memory_access2XS_ok; eauto. intros (mk_instr & chunk' & rr & mr0 & mro & ro & argsEq & roEq & srcEq & A & B & C).
+ eapply transl_store_access2XS_correct; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity. congruence.
+ destruct rr; try discriminate. destruct src; simpl in srcEq; try discriminate.
+ - exploit transl_store_memory_access2_ok; eauto. intros (mk_instr & chunk' & rr & mr0 & mro & ro & argsEq & roEq & srcEq & A & B & C).
+ eapply transl_store_access2_correct; eauto with asmgen. unfold ireg_of. rewrite roEq. reflexivity. congruence.
+ destruct rr; try discriminate. destruct src; simpl in srcEq; try discriminate.
+ - exploit transl_store_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intro A;
+ destruct A as (mk_instr & chunk' & rr & rrEq & B & C & D);
+ rewrite D in STORE; clear D;
+ eapply transl_store_access_correct; eauto with asmgen; try congruence;
+ destruct rr; try discriminate; destruct src; try discriminate.
+ - exploit transl_store_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intro A;
+ destruct A as (mk_instr & chunk' & rr & rrEq & B & C & D);
+ rewrite D in STORE; clear D;
+ eapply transl_store_access_correct; eauto with asmgen; try congruence;
+ destruct rr; try discriminate; destruct src; try discriminate.
+ - exploit transl_store_memory_access_ok; eauto; try discriminate; try (simpl; reflexivity).
+ intro A;
+ destruct A as (mk_instr & chunk' & rr & rrEq & B & C & D);
+ rewrite D in STORE; clear D;
+ eapply transl_store_access_correct; eauto with asmgen; try congruence;
+ destruct rr; try discriminate; destruct src; try discriminate.
+Qed.
+
+Lemma make_epilogue_correct:
+ forall ge0 f m stk soff cs m' ms rs k tm,
+ Mach.load_stack m (Vptr stk soff) Tptr f.(fn_link_ofs) = Some (parent_sp cs) ->
+ Mach.load_stack m (Vptr stk soff) Tptr f.(fn_retaddr_ofs) = Some (parent_ra cs) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ agree ms (Vptr stk soff) rs ->
+ Mem.extends m tm ->
+ match_stack ge0 cs ->
+ exists rs', exists tm',
+ exec_straight ge (make_epilogue f k) rs tm k rs' tm'
+ /\ agree ms (parent_sp cs) rs'
+ /\ Mem.extends m' tm'
+ /\ rs'#RA = parent_ra cs
+ /\ rs'#SP = parent_sp cs
+ /\ (forall r, r <> PC -> r <> RA -> r <> SP -> r <> RTMP -> r <> GPRA -> rs'#r = rs#r).
+Proof.
+ intros until tm; intros LP LRA FREE AG MEXT MCS.
+ exploit Mem.loadv_extends. eauto. eexact LP. auto. simpl. intros (parent' & LP' & LDP').
+ exploit Mem.loadv_extends. eauto. eexact LRA. auto. simpl. intros (ra' & LRA' & LDRA').
+ exploit lessdef_parent_sp; eauto. intros EQ; subst parent'; clear LDP'.
+ exploit lessdef_parent_ra; eauto. intros EQ; subst ra'; clear LDRA'.
+ exploit Mem.free_parallel_extends; eauto. intros (tm' & FREE' & MEXT').
+ unfold make_epilogue.
+ rewrite chunk_of_Tptr in *.
+
+ exploit ((loadind_ptr_correct SP (fn_retaddr_ofs f) GPRA (Pset RA GPRA ::g Pfreeframe (fn_stacksize f) (fn_link_ofs f) ::g k))
+ rs tm).
+ - rewrite <- (sp_val _ _ rs AG). simpl. eexact LRA'.
+ - intros (rs1 & A1 & B1 & C1).
+ assert (agree ms (Vptr stk soff) rs1) as AG1.
+ + destruct AG.
+ apply mkagree; auto.
+ rewrite C1; discriminate || auto.
+ intro. rewrite C1; auto; destruct r; simpl; try discriminate.
+ + exploit (Pset_correct RA GPRA (Pfreeframe (fn_stacksize f) (fn_link_ofs f) ::g k) rs1 tm). auto.
+ intros (rs2 & A2 & B2 & C2).
+ econstructor; econstructor; split.
+ * eapply exec_straight_trans.
+ { eexact A1. }
+ { eapply exec_straight_trans.
+ { eapply A2. }
+ { apply exec_straight_one. simpl.
+ rewrite (C2 SP) by auto with asmgen. rewrite <- (sp_val _ _ rs1 AG1). simpl; rewrite LP'.
+ rewrite FREE'. eauto. } }
+ * split. apply agree_set_other; auto with asmgen.
+ apply agree_change_sp with (Vptr stk soff).
+ apply agree_exten with rs; auto. intros; rewrite C2; auto with asmgen.
+ eapply parent_sp_def; eauto.
+ split. auto.
+ split. Simpl. rewrite B2. auto.
+ split. Simpl.
+ intros. Simpl.
+ rewrite C2; auto.
+Qed.
+
+End CONSTRUCTORS.
+
+
diff --git a/kvx/Asmblockprops.v b/kvx/Asmblockprops.v
new file mode 100644
index 00000000..a732d29b
--- /dev/null
+++ b/kvx/Asmblockprops.v
@@ -0,0 +1,357 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Common definition and proofs on Asmblock required by various modules *)
+
+Require Import Coqlib.
+Require Import Integers.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Values.
+Require Import Asmblock.
+Require Import Axioms.
+
+Definition bblock_simu (ge: Genv.t fundef unit) (f: function) (bb bb': bblock) :=
+ forall rs m,
+ exec_bblock ge f bb rs m <> Stuck ->
+ exec_bblock ge f bb rs m = exec_bblock ge f bb' rs m.
+
+Hint Extern 2 (_ <> _) => congruence: asmgen.
+
+Lemma preg_of_data:
+ forall r, data_preg (preg_of r) = true.
+Proof.
+ intros. destruct r; reflexivity.
+Qed.
+Hint Resolve preg_of_data: asmgen.
+
+Lemma data_diff:
+ forall r r',
+ data_preg r = true -> data_preg r' = false -> r <> r'.
+Proof.
+ congruence.
+Qed.
+Hint Resolve data_diff: asmgen.
+
+Lemma preg_of_not_PC:
+ forall r, preg_of r <> PC.
+Proof.
+ intros. apply data_diff; auto with asmgen.
+Qed.
+
+Lemma preg_of_not_SP:
+ forall r, preg_of r <> SP.
+Proof.
+ intros. unfold preg_of; destruct r; cbn; congruence.
+Qed.
+
+Hint Resolve preg_of_not_SP preg_of_not_PC: asmgen.
+
+
+Lemma nextblock_pc:
+ forall b rs, (nextblock b rs)#PC = Val.offset_ptr rs#PC (Ptrofs.repr (size b)).
+Proof.
+ intros. apply Pregmap.gss.
+Qed.
+
+Lemma nextblock_inv:
+ forall b r rs, r <> PC -> (nextblock b rs)#r = rs#r.
+Proof.
+ intros. unfold nextblock. apply Pregmap.gso. red; intro; subst. auto.
+Qed.
+
+Lemma nextblock_inv1:
+ forall b r rs, data_preg r = true -> (nextblock b rs)#r = rs#r.
+Proof.
+ intros. apply nextblock_inv. red; intro; subst; discriminate.
+Qed.
+
+Ltac Simplif :=
+ ((rewrite nextblock_inv by eauto with asmgen)
+ || (rewrite nextblock_inv1 by eauto with asmgen)
+ || (rewrite Pregmap.gss)
+ || (rewrite nextblock_pc)
+ || (rewrite Pregmap.gso by eauto with asmgen)
+ ); auto with asmgen.
+
+Ltac Simpl := repeat Simplif.
+
+(* For Asmblockgenproof0 *)
+
+Theorem exec_basic_instr_pc:
+ forall ge b rs1 m1 rs2 m2,
+ exec_basic_instr ge b rs1 m1 = Next rs2 m2 ->
+ rs2 PC = rs1 PC.
+Proof.
+ intros. destruct b; try destruct i; try destruct i.
+ all: try (inv H; Simpl).
+ 1-10: unfold parexec_load_offset in H1; destruct (eval_offset ofs); try discriminate; destruct (Mem.loadv _ _ _); unfold parexec_incorrect_load in *; destruct trap; try discriminate; inv H1; Simpl; fail.
+
+ 1-20: unfold parexec_load_reg, parexec_load_regxs in H1; destruct (Mem.loadv _ _ _); unfold parexec_incorrect_load in *; destruct trap; try discriminate; inv H1; Simpl; fail.
+
+ { (* PLoadQRRO *)
+ unfold parexec_load_q_offset in H1.
+ destruct (gpreg_q_expand _) as [r0 r1] in H1.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ inv H1. Simpl. }
+ { (* PLoadORRO *)
+ unfold parexec_load_o_offset in H1.
+ destruct (gpreg_o_expand _) as [[[r0 r1] r2] r3] in H1.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ destruct (Mem.loadv _ _ _) in H1; try discriminate.
+ inv H1. Simpl. }
+ 1-8: unfold parexec_store_offset in H1; destruct (eval_offset ofs); try discriminate; destruct (Mem.storev _ _ _); [inv H1; auto | discriminate]; fail.
+ 1-8: unfold parexec_store_reg in H1; destruct (Mem.storev _ _ _); [inv H1; Simpl | discriminate]; auto; fail.
+ 1-8: unfold parexec_store_regxs in H1; destruct (Mem.storev _ _ _); [inv H1; Simpl | discriminate]; auto; fail.
+
+ { (* PStoreQRRO *)
+ unfold parexec_store_q_offset in H1.
+ destruct (gpreg_q_expand _) as [r0 r1] in H1.
+ unfold eval_offset in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ inv H1. Simpl. reflexivity. }
+ { (* PStoreORRO *)
+ unfold parexec_store_o_offset in H1.
+ destruct (gpreg_o_expand _) as [[[r0 r1] r2] r3] in H1.
+ unfold eval_offset in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ destruct (Mem.storev _ _ _) in H1; try discriminate.
+ inv H1. Simpl. reflexivity. }
+ - destruct (Mem.alloc _ _ _). destruct (Mem.store _ _ _ _ _). inv H1. Simpl. discriminate.
+ - destruct (Mem.loadv _ _ _); try discriminate. destruct (rs1 _); try discriminate.
+ destruct (Mem.free _ _ _ _). inv H1. Simpl. discriminate.
+ - destruct rs; try discriminate. inv H1. Simpl.
+ - destruct rd; try discriminate. inv H1; Simpl.
+ - reflexivity.
+Qed.
+
+(* For PostpassSchedulingproof *)
+
+Lemma regset_double_set:
+ forall r1 r2 (rs: regset) v1 v2,
+ r1 <> r2 ->
+ (rs # r1 <- v1 # r2 <- v2) = (rs # r2 <- v2 # r1 <- v1).
+Proof.
+ intros. apply functional_extensionality. intros r. destruct (preg_eq r r1).
+ - subst. rewrite Pregmap.gso; auto. repeat (rewrite Pregmap.gss). auto.
+ - destruct (preg_eq r r2).
+ + subst. rewrite Pregmap.gss. rewrite Pregmap.gso; auto. rewrite Pregmap.gss. auto.
+ + repeat (rewrite Pregmap.gso; auto).
+Qed.
+
+Lemma next_eq:
+ forall (rs rs': regset) m m',
+ rs = rs' -> m = m' -> Next rs m = Next rs' m'.
+Proof.
+ intros; apply f_equal2; auto.
+Qed.
+
+Lemma exec_load_offset_pc_var:
+ forall trap t rs m rd ra ofs rs' m' v,
+ exec_load_offset trap t rs m rd ra ofs = Next rs' m' ->
+ exec_load_offset trap t rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_load_offset in *. unfold parexec_load_offset in *. rewrite Pregmap.gso; try discriminate. destruct (eval_offset ofs); try discriminate.
+ destruct (Mem.loadv _ _ _).
+ - inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+ - unfold parexec_incorrect_load in *.
+ destruct trap; try discriminate.
+ inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+Qed.
+
+Lemma exec_load_reg_pc_var:
+ forall trap t rs m rd ra ro rs' m' v,
+ exec_load_reg trap t rs m rd ra ro = Next rs' m' ->
+ exec_load_reg trap t rs # PC <- v m rd ra ro = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_load_reg in *. unfold parexec_load_reg in *. rewrite Pregmap.gso; try discriminate.
+ destruct (Mem.loadv _ _ _).
+ - inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+ - unfold parexec_incorrect_load in *.
+ destruct trap; try discriminate.
+ inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+Qed.
+
+Lemma exec_load_regxs_pc_var:
+ forall trap t rs m rd ra ro rs' m' v,
+ exec_load_regxs trap t rs m rd ra ro = Next rs' m' ->
+ exec_load_regxs trap t rs # PC <- v m rd ra ro = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_load_regxs in *. unfold parexec_load_regxs in *. rewrite Pregmap.gso; try discriminate.
+ destruct (Mem.loadv _ _ _).
+ - inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+ - unfold parexec_incorrect_load in *.
+ destruct trap; try discriminate.
+ inv H. apply next_eq; auto. apply functional_extensionality. intros. rewrite regset_double_set; auto. discriminate.
+Qed.
+
+Lemma exec_load_offset_q_pc_var:
+ forall rs m rd ra ofs rs' m' v,
+ exec_load_q_offset rs m rd ra ofs = Next rs' m' ->
+ exec_load_q_offset rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_load_q_offset in *. unfold parexec_load_q_offset in *.
+ destruct (gpreg_q_expand rd) as [rd0 rd1].
+ (* destruct (ireg_eq rd0 ra); try discriminate. *)
+ rewrite Pregmap.gso; try discriminate.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ inv H.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ inv H1. f_equal.
+ rewrite (regset_double_set PC rd0) by discriminate.
+ rewrite (regset_double_set PC rd1) by discriminate.
+ reflexivity.
+Qed.
+
+Lemma exec_load_offset_o_pc_var:
+ forall rs m rd ra ofs rs' m' v,
+ exec_load_o_offset rs m rd ra ofs = Next rs' m' ->
+ exec_load_o_offset rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_load_o_offset in *. unfold parexec_load_o_offset in *.
+ destruct (gpreg_o_expand rd) as [[[rd0 rd1] rd2] rd3].
+(*
+ destruct (ireg_eq rd0 ra); try discriminate.
+ destruct (ireg_eq rd1 ra); try discriminate.
+ destruct (ireg_eq rd2 ra); try discriminate.
+*)
+ rewrite Pregmap.gso; try discriminate.
+ cbn in *.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ destruct (Mem.loadv _ _ _); try discriminate.
+ rewrite (regset_double_set PC rd0) by discriminate.
+ rewrite (regset_double_set PC rd1) by discriminate.
+ rewrite (regset_double_set PC rd2) by discriminate.
+ rewrite (regset_double_set PC rd3) by discriminate.
+ inv H.
+ trivial.
+Qed.
+
+Lemma exec_store_offset_pc_var:
+ forall t rs m rd ra ofs rs' m' v,
+ exec_store_offset t rs m rd ra ofs = Next rs' m' ->
+ exec_store_offset t rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_store_offset in *. unfold parexec_store_offset in *. rewrite Pregmap.gso; try discriminate.
+ destruct (eval_offset ofs); try discriminate.
+ destruct (Mem.storev _ _ _).
+ - inv H. apply next_eq; auto.
+ - discriminate.
+Qed.
+
+Lemma exec_store_q_offset_pc_var:
+ forall rs m rd ra ofs rs' m' v,
+ exec_store_q_offset rs m rd ra ofs = Next rs' m' ->
+ exec_store_q_offset rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_store_q_offset in *. unfold parexec_store_q_offset in *. rewrite Pregmap.gso; try discriminate.
+ cbn in *.
+ destruct (gpreg_q_expand _) as [s0 s1].
+ destruct (Mem.storev _ _ _); try discriminate.
+ destruct (Mem.storev _ _ _); try discriminate.
+ inv H. apply next_eq; auto.
+Qed.
+
+Lemma exec_store_o_offset_pc_var:
+ forall rs m rd ra ofs rs' m' v,
+ exec_store_o_offset rs m rd ra ofs = Next rs' m' ->
+ exec_store_o_offset rs # PC <- v m rd ra ofs = Next rs' # PC <- v m'.
+Proof.
+ intros.
+ unfold exec_store_o_offset in *. unfold parexec_store_o_offset in *.
+ destruct (gpreg_o_expand _) as [[[s0 s1] s2] s3].
+ destruct (Mem.storev _ _ _); try discriminate.
+ destruct (Mem.storev _ _ _); try discriminate.
+ destruct (Mem.storev _ _ _); try discriminate.
+ destruct (Mem.storev _ _ _); try discriminate.
+ inv H.
+ trivial.
+Qed.
+
+Lemma exec_store_reg_pc_var:
+ forall t rs m rd ra ro rs' m' v,
+ exec_store_reg t rs m rd ra ro = Next rs' m' ->
+ exec_store_reg t rs # PC <- v m rd ra ro = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_store_reg in *. unfold parexec_store_reg in *. rewrite Pregmap.gso; try discriminate.
+ destruct (Mem.storev _ _ _).
+ - inv H. apply next_eq; auto.
+ - discriminate.
+Qed.
+
+Lemma exec_store_regxs_pc_var:
+ forall t rs m rd ra ro rs' m' v,
+ exec_store_regxs t rs m rd ra ro = Next rs' m' ->
+ exec_store_regxs t rs # PC <- v m rd ra ro = Next rs' # PC <- v m'.
+Proof.
+ intros. unfold exec_store_regxs in *. unfold parexec_store_regxs in *. rewrite Pregmap.gso; try discriminate.
+ destruct (Mem.storev _ _ _).
+ - inv H. apply next_eq; auto.
+ - discriminate.
+Qed.
+
+Theorem exec_basic_instr_pc_var:
+ forall ge i rs m rs' m' v,
+ exec_basic_instr ge i rs m = Next rs' m' ->
+ exec_basic_instr ge i (rs # PC <- v) m = Next (rs' # PC <- v) m'.
+Proof.
+ intros. unfold exec_basic_instr in *. unfold bstep in *. destruct i.
+ - unfold exec_arith_instr in *. destruct i; destruct i.
+ all: try (exploreInst; inv H; apply next_eq; auto;
+ apply functional_extensionality; intros; rewrite regset_double_set; auto; discriminate).
+(*
+ (* Some cases treated seperately because exploreInst destructs too much *)
+ all: try (inv H; apply next_eq; auto; apply functional_extensionality; intros; rewrite regset_double_set; auto; discriminate). *)
+ - destruct i.
+ + exploreInst; apply exec_load_offset_pc_var; auto.
+ + exploreInst; apply exec_load_reg_pc_var; auto.
+ + exploreInst; apply exec_load_regxs_pc_var; auto.
+ + apply exec_load_offset_q_pc_var; auto.
+ + apply exec_load_offset_o_pc_var; auto.
+ - destruct i.
+ + exploreInst; apply exec_store_offset_pc_var; auto.
+ + exploreInst; apply exec_store_reg_pc_var; auto.
+ + exploreInst; apply exec_store_regxs_pc_var; auto.
+ + apply exec_store_q_offset_pc_var; auto.
+ + apply exec_store_o_offset_pc_var; auto.
+ - destruct (Mem.alloc _ _ _) as (m1 & stk). repeat (rewrite Pregmap.gso; try discriminate).
+ destruct (Mem.storev _ _ _ _); try discriminate.
+ inv H. apply next_eq; auto. apply functional_extensionality. intros.
+ rewrite (regset_double_set GPR32 PC); try discriminate.
+ rewrite (regset_double_set GPR12 PC); try discriminate.
+ rewrite (regset_double_set FP PC); try discriminate. reflexivity.
+ - repeat (rewrite Pregmap.gso; try discriminate).
+ destruct (Mem.loadv _ _ _); try discriminate.
+ destruct (rs GPR12); try discriminate.
+ destruct (Mem.free _ _ _ _); try discriminate.
+ inv H. apply next_eq; auto.
+ rewrite (regset_double_set GPR32 PC).
+ rewrite (regset_double_set GPR12 PC). reflexivity.
+ all: discriminate.
+ - destruct rs0; try discriminate. inv H. apply next_eq; auto.
+ repeat (rewrite Pregmap.gso; try discriminate). apply regset_double_set; discriminate.
+ - destruct rd; try discriminate. inv H. apply next_eq; auto.
+ repeat (rewrite Pregmap.gso; try discriminate). apply regset_double_set; discriminate.
+ - inv H. apply next_eq; auto.
+Qed.
+
+
diff --git a/kvx/Asmexpand.ml b/kvx/Asmexpand.ml
new file mode 100644
index 00000000..f84cf22d
--- /dev/null
+++ b/kvx/Asmexpand.ml
@@ -0,0 +1,642 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* Expanding built-ins and some pseudo-instructions by rewriting
+ of the RISC-V assembly code. *)
+
+open Asm
+open Asmexpandaux
+open AST
+open Camlcoq
+
+exception Error of string
+
+(* Useful constants and helper functions *)
+
+let _0 = Integers.Int.zero
+let _1 = Integers.Int.one
+let _2 = coqint_of_camlint 2l
+let _4 = coqint_of_camlint 4l
+let _8 = coqint_of_camlint 8l
+let _16 = coqint_of_camlint 16l
+let _m1 = coqint_of_camlint (-1l)
+
+let wordsize = if Archi.ptr64 then 8 else 4
+
+let align n a = (n + a - 1) land (-a)
+
+let stack_pointer = Asmvliw.GPR12
+
+(* Emit instruction sequences that set or offset a register by a constant. *)
+(*
+ let expand_loadimm32 dst n =
+ List.iter emit (Asmgen.loadimm32 dst n [])
+*)
+let expand_addptrofs dst src n =
+ List.iter emit (basic_to_instruction (Asmvliw.PArith (Asmblockgen.addptrofs dst src n)) :: [])
+let expand_storeind_ptr src base ofs =
+ List.iter emit (basic_to_instruction (Asmblockgen.storeind_ptr src base ofs) :: [])
+let expand_loadind_ptr dst base ofs =
+ List.iter emit (basic_to_instruction (Asmblockgen.loadind_ptr base ofs dst) :: [])
+
+(* Built-ins. They come in two flavors:
+ - annotation statements: take their arguments in registers or stack
+ locations; generate no code;
+ - inlined by the compiler: take their arguments in arbitrary
+ registers.
+*)
+
+(* Fix-up code around calls to variadic functions. Floating-point arguments
+ residing in FP registers need to be moved to integer registers. *)
+
+let int_param_regs = let open Asmvliw in [| GPR0; GPR1; GPR2; GPR3; GPR4; GPR5; GPR6; GPR7; GPR8; GPR9; GPR10; GPR11 |]
+(* let float_param_regs = [| F10; F11; F12; F13; F14; F15; F16; F17 |] *)
+let float_param_regs = [| |]
+
+let fixup_variadic_call pos tyl = assert false
+(*if pos < 8 then
+ match tyl with
+ | [] ->
+ ()
+ | (Tint | Tany32) :: tyl ->
+ fixup_variadic_call (pos + 1) tyl
+ | Tsingle :: tyl ->
+ let rs =float_param_regs.(pos)
+ and rd = int_param_regs.(pos) in
+ emit (Pfmvxs(rd, rs));
+ fixup_variadic_call (pos + 1) tyl
+ | Tlong :: tyl ->
+ let pos' = if Archi.ptr64 then pos + 1 else align pos 2 + 2 in
+ fixup_variadic_call pos' tyl
+ | (Tfloat | Tany64) :: tyl ->
+ if Archi.ptr64 then begin
+ let rs = float_param_regs.(pos)
+ and rd = int_param_regs.(pos) in
+ emit (Pfmvxd(rd, rs));
+ fixup_variadic_call (pos + 1) tyl
+ end else begin
+ let pos = align pos 2 in
+ if pos < 8 then begin
+ let rs = float_param_regs.(pos)
+ and rd1 = int_param_regs.(pos)
+ and rd2 = int_param_regs.(pos + 1) in
+ emit (Paddiw(X2, X X2, Integers.Int.neg _16));
+ emit (Pfsd(rs, X2, Ofsimm _0));
+ emit (Plw(rd1, X2, Ofsimm _0));
+ emit (Plw(rd2, X2, Ofsimm _4));
+ emit (Paddiw(X2, X X2, _16));
+ fixup_variadic_call (pos + 2) tyl
+ end
+ end
+*)
+
+let fixup_call sg =
+ if sg.sig_cc.cc_vararg <> None then fixup_variadic_call 0 sg.sig_args
+
+(* Handling of annotations *)
+
+let expand_annot_val kind txt targ args res =
+ emit (Pbuiltin (EF_annot(kind,txt,[targ]), args, BR_none));
+ match args, res with
+ | [BA(Asmvliw.IR src)], BR(Asmvliw.IR dst) ->
+ if dst <> src then emit (Pmv (dst, src))
+ | _, _ ->
+ raise (Error "ill-formed __builtin_annot_val")
+
+(* Handling of memcpy *)
+
+let emit_move dst r =
+ if dst <> r
+ then emit (Paddil(dst, r, Z.zero));;
+
+(* FIXME DMonniaux this is probably not complete *)
+let get_builtin_arg dst arg =
+ match arg with
+ | BA (Asmvliw.IR reg) -> emit_move dst reg
+ | BA (ireg) -> failwith "get_builtin_arg: BA_int(not ireg)"
+ | BA_int _ -> failwith "get_builtin_arg: BA_int"
+ | BA_long _ -> failwith "get_builtin_arg: BA_long"
+ | BA_float _ -> failwith "get_builtin_arg: BA_float"
+ | BA_single _ -> failwith "get_builtin_arg: BA_single"
+ | BA_loadstack _ -> failwith "get_builtin_arg: BA_loadstack"
+ | BA_addrstack ofs -> emit (Paddil(dst, stack_pointer, ofs))
+ | BA_loadglobal _ -> failwith "get_builtin_arg: BA_loadglobal"
+ | BA_addrglobal _ -> failwith "get_builtin_arg: BA_addrglobal"
+ | BA_splitlong _ -> failwith "get_builtin_arg: BA_splitlong"
+ | BA_addptr _ -> failwith "get_builtin_arg: BA_addptr";;
+
+let smart_memcpy = true
+
+(* FIXME DMonniaux this is really suboptimal (byte per byte) *)
+let expand_builtin_memcpy_big sz al src dst =
+ assert (sz > Z.zero);
+ let dstptr = Asmvliw.GPR62
+ and srcptr = Asmvliw.GPR63
+ and tmpbuf = Asmvliw.GPR61
+ and tmpbuf2 = Asmvliw.R60R61
+ and caml_sz = camlint64_of_coqint sz in
+ get_builtin_arg dstptr dst;
+ get_builtin_arg srcptr src;
+ let caml_sz_div16 = Int64.shift_right caml_sz 4
+ and sixteen = coqint_of_camlint64 16L in
+ if smart_memcpy
+ then
+ let remaining = ref caml_sz
+ and offset = ref 0L in
+ let cpy buf size load store =
+ (if !remaining >= size
+ then
+ let zofs = coqint_of_camlint64 !offset in
+ begin
+ emit Psemi;
+ emit (load buf srcptr (AOff zofs));
+ emit Psemi;
+ emit (store buf dstptr (AOff zofs));
+ remaining := Int64.sub !remaining size;
+ offset := Int64.add !offset size
+ end) in
+ begin
+ (if caml_sz_div16 >= 2L
+ then
+ begin
+ emit (Pmake (tmpbuf, (coqint_of_camlint64 caml_sz_div16)));
+ emit Psemi;
+ let lbl = new_label() in
+ emit (Ploopdo (tmpbuf, lbl));
+ emit Psemi;
+ emit (Plq (tmpbuf2, srcptr, AOff Z.zero));
+ emit (Paddil (srcptr, srcptr, sixteen));
+ emit Psemi;
+ emit (Psq (tmpbuf2, dstptr, AOff Z.zero));
+ emit (Paddil (dstptr, dstptr, sixteen));
+ emit Psemi;
+ emit (Plabel lbl);
+ remaining := Int64.sub !remaining (Int64.shift_left caml_sz_div16 4)
+ end);
+
+ cpy tmpbuf2 16L (fun x y z -> Plq(x, y, z)) (fun x y z -> Psq(x, y, z));
+ cpy tmpbuf 8L (fun x y z -> Pld(TRAP, x, y, z)) (fun x y z -> Psd(x, y, z));
+ cpy tmpbuf 4L (fun x y z -> Plw(TRAP, x, y, z)) (fun x y z -> Psw(x, y, z));
+ cpy tmpbuf 2L (fun x y z -> Plh(TRAP, x, y, z)) (fun x y z -> Psh(x, y, z));
+ cpy tmpbuf 1L (fun x y z -> Plb(TRAP, x, y, z)) (fun x y z -> Psb(x, y, z));
+ assert (!remaining = 0L)
+ end
+ else
+ begin
+ emit (Pmake (tmpbuf, sz));
+ emit Psemi;
+ let lbl = new_label() in
+ emit (Ploopdo (tmpbuf, lbl));
+ emit Psemi;
+ emit (Plb (TRAP, tmpbuf, srcptr, AOff Z.zero));
+ emit (Paddil (srcptr, srcptr, Z.one));
+ emit Psemi;
+ emit (Psb (tmpbuf, dstptr, AOff Z.zero));
+ emit (Paddil (dstptr, dstptr, Z.one));
+ emit Psemi;
+ emit (Plabel lbl);
+ end;;
+
+let expand_builtin_memcpy sz al args =
+ match args with
+ | [dst; src] ->
+ expand_builtin_memcpy_big sz al src dst
+ | _ -> assert false;;
+
+(* Handling of volatile reads and writes *)
+(* FIXME probably need to check for size of displacement *)
+let expand_builtin_vload_common chunk base ofs res =
+ match chunk, res with
+ | Mint8unsigned, BR(Asmvliw.IR res) ->
+ emit (Plbu (TRAP, res, base, AOff ofs))
+ | Mint8signed, BR(Asmvliw.IR res) ->
+ emit (Plb (TRAP, res, base, AOff ofs))
+ | Mint16unsigned, BR(Asmvliw.IR res) ->
+ emit (Plhu (TRAP, res, base, AOff ofs))
+ | Mint16signed, BR(Asmvliw.IR res) ->
+ emit (Plh (TRAP, res, base, AOff ofs))
+ | Mint32, BR(Asmvliw.IR res) ->
+ emit (Plw (TRAP, res, base, AOff ofs))
+ | Mint64, BR(Asmvliw.IR res) ->
+ emit (Pld (TRAP, res, base, AOff ofs))
+ | Mint64, BR_splitlong(BR(Asmvliw.IR res1), BR(Asmvliw.IR res2)) ->
+ let ofs' = Integers.Ptrofs.add ofs _4 in
+ if base <> res2 then begin
+ emit (Plw (TRAP, res2, base, AOff ofs));
+ emit (Plw (TRAP, res1, base, AOff ofs'))
+ end else begin
+ emit (Plw (TRAP, res1, base, AOff ofs'));
+ emit (Plw (TRAP, res2, base, AOff ofs))
+ end
+ | Mfloat32, BR(Asmvliw.IR res) ->
+ emit (Pfls (TRAP, res, base, AOff ofs))
+ | Mfloat64, BR(Asmvliw.IR res) ->
+ emit (Pfld (TRAP, res, base, AOff ofs))
+ | _ ->
+ assert false
+
+let expand_builtin_vload chunk args res =
+ match args with
+ | [BA(Asmvliw.IR addr)] ->
+ expand_builtin_vload_common chunk addr _0 res
+ | [BA_addrstack ofs] ->
+ expand_builtin_vload_common chunk stack_pointer ofs res
+ | [BA_addptr(BA(Asmvliw.IR addr), (BA_int ofs | BA_long ofs))] ->
+ expand_builtin_vload_common chunk addr ofs res
+ | _ ->
+ assert false
+
+
+let expand_builtin_vstore_common chunk base ofs src =
+ match chunk, src with
+ | (Mint8signed | Mint8unsigned), BA(Asmvliw.IR src) ->
+ emit (Psb (src, base, AOff ofs))
+ | (Mint16signed | Mint16unsigned), BA(Asmvliw.IR src) ->
+ emit (Psh (src, base, AOff ofs))
+ | Mint32, BA(Asmvliw.IR src) ->
+ emit (Psw (src, base, AOff ofs))
+ | Mint64, BA(Asmvliw.IR src) ->
+ emit (Psd (src, base, AOff ofs))
+ | Mint64, BA_splitlong(BA(Asmvliw.IR src1), BA(Asmvliw.IR src2)) ->
+ let ofs' = Integers.Ptrofs.add ofs _4 in
+ emit (Psw (src2, base, AOff ofs));
+ emit (Psw (src1, base, AOff ofs'))
+ | Mfloat32, BA(Asmvliw.IR src) ->
+ emit (Pfss (src, base, AOff ofs))
+ | Mfloat64, BA(Asmvliw.IR src) ->
+ emit (Pfsd (src, base, AOff ofs))
+ | _ ->
+ assert false
+
+let expand_builtin_vstore chunk args =
+ match args with
+ | [BA(Asmvliw.IR addr); src] ->
+ expand_builtin_vstore_common chunk addr _0 src
+ | [BA_addrstack ofs; src] ->
+ expand_builtin_vstore_common chunk stack_pointer ofs src
+ | [BA_addptr(BA(Asmvliw.IR addr), (BA_int ofs | BA_long ofs)); src] ->
+ expand_builtin_vstore_common chunk addr ofs src
+ | _ ->
+ assert false
+
+(* Handling of varargs *)
+
+(* Size in words of the arguments to a function. This includes both
+ arguments passed in registers and arguments passed on stack. *)
+
+let rec args_size sz = function
+ | [] -> sz
+ | (Tint | Tsingle | Tany32) :: l ->
+ args_size (sz + 1) l
+ | (Tlong | Tfloat | Tany64) :: l ->
+ args_size (if Archi.ptr64 then sz + 1 else align sz 2 + 2) l
+
+let arguments_size sg =
+ args_size 0 sg.sig_args
+
+let _nbregargs_ = 12
+let _alignment_ = 8
+
+let save_arguments first_reg base_ofs = let open Asmvliw in
+ for i = first_reg to (_nbregargs_ - 1) do begin
+ expand_storeind_ptr
+ int_param_regs.(i)
+ GPR12
+ (Integers.Ptrofs.repr (Z.add base_ofs (Z.of_uint ((i - first_reg) * wordsize))));
+ emit Psemi
+ end done
+
+let vararg_start_ofs : Z.t option ref = ref None
+
+let expand_builtin_va_start r = (* assert false *)
+match !vararg_start_ofs with
+ | None ->
+ invalid_arg "Fatal error: va_start used in non-vararg function"
+ | Some ofs ->
+ expand_addptrofs Asmvliw.GPR32 stack_pointer (Integers.Ptrofs.repr ofs);
+ emit Psemi;
+ expand_storeind_ptr Asmvliw.GPR32 r Integers.Ptrofs.zero
+
+(* Auxiliary for 64-bit integer arithmetic built-ins. They expand to
+ two instructions, one computing the low 32 bits of the result,
+ followed by another computing the high 32 bits. In cases where
+ the first instruction would overwrite arguments to the second
+ instruction, we must go through X31 to hold the low 32 bits of the result.
+*)
+
+let expand_int64_arith conflict rl fn = assert false
+(*if conflict then (fn X31; emit (Pmv(rl, X31))) else fn rl *)
+
+(* Byte swaps. There are no specific instructions, so we use standard,
+ not-very-efficient formulas. *)
+
+let expand_bswap16 d s = let open Asmvliw in
+ (* d = (s & 0xFF) << 8 | (s >> 8) & 0xFF *)
+ emit (Pandiw(GPR32, s, coqint_of_camlint 0xFFl)); emit Psemi;
+ emit (Pslliw(GPR32, GPR32, _8)); emit Psemi;
+ emit (Psrliw(d, s, _8)); emit Psemi;
+ emit (Pandiw(d, d, coqint_of_camlint 0xFFl));
+ emit (Porw(d, GPR32, d)); emit Psemi
+
+let expand_bswap32 d s = let open Asmvliw in
+ (* d = (s << 24)
+ | (((s >> 8) & 0xFF) << 16)
+ | (((s >> 16) & 0xFF) << 8)
+ | (s >> 24) *)
+ emit (Pslliw(GPR16, s, coqint_of_camlint 24l)); emit Psemi;
+ emit (Psrliw(GPR32, s, _8)); emit Psemi;
+ emit (Pandiw(GPR32, GPR32, coqint_of_camlint 0xFFl)); emit Psemi;
+ emit (Pslliw(GPR32, GPR32, _16)); emit Psemi;
+ emit (Porw(GPR16, GPR16, GPR32)); emit Psemi;
+ emit (Psrliw(GPR32, s, _16)); emit Psemi;
+ emit (Pandiw(GPR32, GPR32, coqint_of_camlint 0xFFl)); emit Psemi;
+ emit (Pslliw(GPR32, GPR32, _8)); emit Psemi;
+ emit (Porw(GPR16, GPR16, GPR32)); emit Psemi;
+ emit (Psrliw(GPR32, s, coqint_of_camlint 24l)); emit Psemi;
+ emit (Porw(d, GPR16, GPR32)); emit Psemi
+
+let expand_bswap64 d s = let open Asmvliw in
+ (* d = s << 56
+ | (((s >> 8) & 0xFF) << 48)
+ | (((s >> 16) & 0xFF) << 40)
+ | (((s >> 24) & 0xFF) << 32)
+ | (((s >> 32) & 0xFF) << 24)
+ | (((s >> 40) & 0xFF) << 16)
+ | (((s >> 48) & 0xFF) << 8)
+ | s >> 56 *)
+ emit (Psllil(GPR16, s, coqint_of_camlint 56l)); emit Psemi;
+ List.iter
+ (fun (n1, n2) ->
+ emit (Psrlil(GPR32, s, coqint_of_camlint n1)); emit Psemi;
+ emit (Pandil(GPR32, GPR32, coqint_of_camlint 0xFFl)); emit Psemi;
+ emit (Psllil(GPR32, GPR32, coqint_of_camlint n2)); emit Psemi;
+ emit (Porl(GPR16, GPR16, GPR32)); emit Psemi;)
+ [(8l,48l); (16l,40l); (24l,32l); (32l,24l); (40l,16l); (48l,8l)];
+ emit (Psrlil(GPR32, s, coqint_of_camlint 56l)); emit Psemi;
+ emit (Porl(d, GPR16, GPR32)); emit Psemi
+
+(* Handling of compiler-inlined builtins *)
+let last_system_register = 511l
+let not_system_register cn =cn<0l || cn>last_system_register
+
+let expand_builtin_inline name args res = let open Asmvliw in
+ match name, args, res with
+ (* Synchronization *)
+ | "__builtin_membar", [], _ ->
+ ()
+ (* Vararg stuff *)
+ | "__builtin_va_start", [BA(IR a)], _ ->
+ expand_builtin_va_start a
+ | "__builtin_kvx_clzw", [BA(IR a)], BR(IR res) ->
+ emit (Pclzw(res, a))
+ | "__builtin_clzll", [BA(IR a)], BR(IR res) ->
+ emit (Pclzll(res, a))
+ | "__builtin_kvx_ctzw", [BA(IR a)], BR(IR res) ->
+ emit (Pctzw(res, a))
+ | "__builtin_ctzll", [BA(IR a)], BR(IR res) ->
+ emit (Pctzll(res, a))
+ | "__builtin_kvx_stsud", [BA(IR a1); BA(IR a2)], BR(IR res) ->
+ emit (Pstsud(res, a1, a2))
+ | "__builtin_kvx_get", [BA_int(n)], BR(IR res) ->
+ let cn = camlint_of_coqint n in
+ (if not_system_register cn
+ then failwith (Printf.sprintf "__builtin_kvx_get(n): n must be between 0 and %ld, was %ld" last_system_register cn)
+ else emit (Pgetn(n, res)))
+ | "__builtin_kvx_set", [BA_int(n); BA(IR src)], _ ->
+ let cn = camlint_of_coqint n in
+ (if not_system_register cn
+ then failwith (Printf.sprintf "__builtin_kvx_set(n, val): n must be between 0 and %ld, was %ld" last_system_register cn)
+ else emit (Psetn(n, src)))
+ | "__builtin_kvx_wfxl", [BA_int(n); BA(IR src)], _ ->
+ let cn = camlint_of_coqint n in
+ (if not_system_register cn
+ then failwith (Printf.sprintf "__builtin_kvx_wfxl(n, val): n must be between 0 and %ld, was %ld" last_system_register cn)
+ else emit (Pwfxl(n, src)))
+ | "__builtin_kvx_wfxm", [BA_int(n); BA(IR src)], _ ->
+ let cn = camlint_of_coqint n in
+ (if not_system_register cn
+ then failwith (Printf.sprintf "__builtin_kvx_wfxm(n, val): n must be between 0 and %ld, was %ld" last_system_register cn)
+ else emit (Pwfxm(n, src)))
+ | "__builtin_kvx_ldu", [BA(IR addr)], BR(IR res) ->
+ emit (Pldu(res, addr))
+ | "__builtin_kvx_lbzu", [BA(IR addr)], BR(IR res) ->
+ emit (Plbzu(res, addr))
+ | "__builtin_kvx_lhzu", [BA(IR addr)], BR(IR res) ->
+ emit (Plhzu(res, addr))
+ | "__builtin_kvx_lwzu", [BA(IR addr)], BR(IR res) ->
+ emit (Plwzu(res, addr))
+ | "__builtin_kvx_alclrd", [BA(IR addr)], BR(IR res) ->
+ emit (Palclrd(res, addr))
+ | "__builtin_kvx_alclrw", [BA(IR addr)], BR(IR res) ->
+ emit (Palclrw(res, addr))
+ | "__builtin_kvx_await", [], _ ->
+ emit Pawait
+ | "__builtin_kvx_sleep", [], _ ->
+ emit Psleep
+ | "__builtin_kvx_stop", [], _ ->
+ emit Pstop
+ | "__builtin_kvx_barrier", [], _ ->
+ emit Pbarrier
+ | "__builtin_kvx_fence", [], _ ->
+ emit Pfence
+ | "__builtin_kvx_dinval", [], _ ->
+ emit Pdinval
+ | "__builtin_kvx_dinvall", [BA(IR addr)], _ ->
+ emit (Pdinvall addr)
+ | "__builtin_kvx_dtouchl", [BA(IR addr)], _ ->
+ emit (Pdtouchl addr)
+ | "__builtin_kvx_iinval", [], _ ->
+ emit Piinval
+ | "__builtin_kvx_iinvals", [BA(IR addr)], _ ->
+ emit (Piinvals addr)
+ | "__builtin_kvx_itouchl", [BA(IR addr)], _ ->
+ emit (Pitouchl addr)
+ | "__builtin_kvx_dzerol", [BA(IR addr)], _ ->
+ emit (Pdzerol addr)
+(*| "__builtin_kvx_afaddd", [BA(IR addr); BA (IR incr_res)], BR(IR res) ->
+ (if res <> incr_res
+ then (emit (Asm.Pmv(res, incr_res)); emit Psemi));
+ emit (Pafaddd(addr, res))
+ | "__builtin_kvx_afaddw", [BA(IR addr); BA (IR incr_res)], BR(IR res) ->
+ (if res <> incr_res
+ then (emit (Asm.Pmv(res, incr_res)); emit Psemi));
+ emit (Pafaddw(addr, res)) *) (* see #157 *)
+ | "__builtin_alclrd", [BA(IR addr)], BR(IR res) ->
+ emit (Palclrd(res, addr))
+ | "__builtin_alclrw", [BA(IR addr)], BR(IR res) ->
+ emit (Palclrw(res, addr))
+ | "__builtin_bswap16", [BA(IR a1)], BR(IR res) ->
+ expand_bswap16 res a1
+ | ("__builtin_bswap"| "__builtin_bswap32"), [BA(IR a1)], BR(IR res) ->
+ expand_bswap32 res a1
+ | "__builtin_bswap64", [BA(IR src)], BR(IR res) ->
+ expand_bswap64 res src
+
+ (* Byte swaps *)
+(*| "__builtin_bswap16", [BA(IR a1)], BR(IR res) ->
+ expand_bswap16 res a1
+ | "__builtin_fabs", [BA(FR a1)], BR(FR res) ->
+ emit (Pfabsd(res, a1))
+*)
+ (* Catch-all *)
+ | _ ->
+ raise (Error ("unrecognized builtin " ^ name))
+
+(* Expansion of instructions *)
+
+let expand_instruction instr =
+ match instr with
+ | Pallocframe (sz, ofs) ->
+ let sg = get_current_function_sig() in
+ emit (Pmv (Asmvliw.GPR17, stack_pointer));
+ if sg.sig_cc.cc_vararg <> None then begin
+ let n = arguments_size sg in
+ let extra_sz = if n >= _nbregargs_ then 0 else (* align _alignment_ *) ((_nbregargs_ - n) * wordsize) in
+ let full_sz = Z.add sz (Z.of_uint extra_sz) in
+ expand_addptrofs stack_pointer stack_pointer (Integers.Ptrofs.repr (Z.neg full_sz));
+ emit Psemi;
+ expand_storeind_ptr Asmvliw.GPR17 stack_pointer ofs;
+ emit Psemi;
+ let va_ofs =
+ let extra_ofs = if n <= _nbregargs_ then 0 else ((n - _nbregargs_) * wordsize) in
+ Z.add sz (Z.of_sint extra_ofs) in
+ vararg_start_ofs := Some va_ofs;
+ save_arguments n va_ofs
+ end else begin
+ let below = Integers.Ptrofs.repr (Z.neg sz) in
+ expand_addptrofs stack_pointer stack_pointer below;
+ expand_storeind_ptr stack_pointer stack_pointer (Integers.Ptrofs.add ofs below);
+ emit Psemi; (* Psemi required to fit in resource constraints *)
+ vararg_start_ofs := None
+ end
+ | Pfreeframe (sz, ofs) ->
+ let sg = get_current_function_sig() in
+ let extra_sz =
+ if sg.sig_cc.cc_vararg <> None then begin
+ let n = arguments_size sg in
+ if n >= _nbregargs_ then 0 else (* align _alignment_ *) ((_nbregargs_ - n) * wordsize)
+ end else 0 in
+ expand_addptrofs stack_pointer stack_pointer (Integers.Ptrofs.repr (Z.add sz (Z.of_uint extra_sz)))
+
+(*| Pseqw(rd, rs1, rs2) ->
+ (* emulate based on the fact that x == 0 iff x <u 1 (unsigned cmp) *)
+ if rs2 = X0 then begin
+ emit (Psltiuw(rd, rs1, Int.one))
+ end else begin
+ emit (Pxorw(rd, rs1, rs2)); emit (Psltiuw(rd, X rd, Int.one))
+ end
+ | Psnew(rd, rs1, rs2) ->
+ (* emulate based on the fact that x != 0 iff 0 <u x (unsigned cmp) *)
+ if rs2 = X0 then begin
+ emit (Psltuw(rd, X0, rs1))
+ end else begin
+ emit (Pxorw(rd, rs1, rs2)); emit (Psltuw(rd, X0, X rd))
+ end
+ | Pseql(rd, rs1, rs2) ->
+ (* emulate based on the fact that x == 0 iff x <u 1 (unsigned cmp) *)
+ if rs2 = X0 then begin
+ emit (Psltiul(rd, rs1, Int64.one))
+ end else begin
+ emit (Pxorl(rd, rs1, rs2)); emit (Psltiul(rd, X rd, Int64.one))
+ end
+ | Psnel(rd, rs1, rs2) ->
+ (* emulate based on the fact that x != 0 iff 0 <u x (unsigned cmp) *)
+ if rs2 = X0 then begin
+ emit (Psltul(rd, X0, rs1))
+ end else begin
+ emit (Pxorl(rd, rs1, rs2)); emit (Psltul(rd, X0, X rd))
+ end
+*)| Pcvtl2w (rd, rs) ->
+ assert Archi.ptr64;
+ emit (Paddiw (rd, rs, Integers.Int.zero)) (* 32-bit sign extension *)
+
+(*| Pjal_r(r, sg) ->
+ fixup_call sg; emit instr
+ | Pjal_s(symb, sg) ->
+ fixup_call sg; emit instr
+ | Pj_r(r, sg) when r <> X1 ->
+ fixup_call sg; emit instr
+ | Pj_s(symb, sg) ->
+ fixup_call sg; emit instr
+
+*)| Pbuiltin (ef,args,res) ->
+ begin match ef with
+ | EF_builtin (name,sg) ->
+ expand_builtin_inline (camlstring_of_coqstring name) args res
+ | EF_vload chunk ->
+ expand_builtin_vload chunk args res
+ | EF_vstore chunk ->
+ expand_builtin_vstore chunk args
+(* | EF_annot_val (kind,txt,targ) ->
+ expand_annot_val kind txt targ args res *)
+ | EF_memcpy(sz, al) ->
+ expand_builtin_memcpy sz al args
+ (* | EF_annot _ | EF_debug _ | EF_inline_asm _ ->
+ emit instr
+ *)
+ | EF_malloc -> failwith "asmexpand: malloc"
+ | EF_free -> failwith "asmexpand: free"
+ | EF_debug _ -> failwith "asmexpand: debug"
+ | EF_annot _ -> emit instr
+ | EF_annot_val (kind, txt, targ) -> expand_annot_val kind txt targ args res
+ | EF_external _ -> failwith "asmexpand: external"
+ | EF_inline_asm _ -> emit instr
+ | EF_runtime _ -> failwith "asmexpand: runtime"
+ | EF_profiling _ -> emit instr
+ end
+ | _ ->
+ emit instr
+
+(* NOTE: Dwarf register maps for RV32G are not yet specified
+ officially. This is just a placeholder. *)
+let int_reg_to_dwarf = let open Asmvliw in function
+ | GPR0 -> 1 | GPR1 -> 2 | GPR2 -> 3 | GPR3 -> 4 | GPR4 -> 5
+ | GPR5 -> 6 | GPR6 -> 7 | GPR7 -> 8 | GPR8 -> 9 | GPR9 -> 10
+ | GPR10 -> 11 | GPR11 -> 12 | GPR12 -> 13 | GPR13 -> 14 | GPR14 -> 15
+ | GPR15 -> 16 | GPR16 -> 17 | GPR17 -> 18 | GPR18 -> 19 | GPR19 -> 20
+ | GPR20 -> 21 | GPR21 -> 22 | GPR22 -> 23 | GPR23 -> 24 | GPR24 -> 25
+ | GPR25 -> 26 | GPR26 -> 27 | GPR27 -> 28 | GPR28 -> 29 | GPR29 -> 30
+ | GPR30 -> 31 | GPR31 -> 32 | GPR32 -> 33 | GPR33 -> 34 | GPR34 -> 35
+ | GPR35 -> 36 | GPR36 -> 37 | GPR37 -> 38 | GPR38 -> 39 | GPR39 -> 40
+ | GPR40 -> 41 | GPR41 -> 42 | GPR42 -> 43 | GPR43 -> 44 | GPR44 -> 45
+ | GPR45 -> 46 | GPR46 -> 47 | GPR47 -> 48 | GPR48 -> 49 | GPR49 -> 50
+ | GPR50 -> 51 | GPR51 -> 52 | GPR52 -> 53 | GPR53 -> 54 | GPR54 -> 55
+ | GPR55 -> 56 | GPR56 -> 57 | GPR57 -> 58 | GPR58 -> 59 | GPR59 -> 60
+ | GPR60 -> 61 | GPR61 -> 62 | GPR62 -> 63 | GPR63 -> 64
+
+let preg_to_dwarf = let open Asmvliw in function
+ | IR r -> int_reg_to_dwarf r
+ | RA -> 65 (* FIXME - No idea what is $ra DWARF number in k1-gdb *)
+ | _ -> assert false
+
+let expand_function id fn =
+ try
+ set_current_function fn;
+ expand id (* sp= *) 2 preg_to_dwarf expand_instruction fn.fn_code;
+ Errors.OK (get_current_function ())
+ with Error s ->
+ Errors.Error (Errors.msg (coqstring_of_camlstring s))
+
+let expand_fundef id = function
+ | Internal f ->
+ begin match expand_function id f with
+ | Errors.OK tf -> Errors.OK (Internal tf)
+ | Errors.Error msg -> Errors.Error msg
+ end
+ | External ef ->
+ Errors.OK (External ef)
+
+let expand_program (p: Asm.program) : Asm.program Errors.res =
+ AST.transform_partial_program2 expand_fundef (fun id v -> Errors.OK v) p
diff --git a/kvx/Asmgen.v b/kvx/Asmgen.v
new file mode 100644
index 00000000..61856acf
--- /dev/null
+++ b/kvx/Asmgen.v
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Integers.
+Require Import Mach Asm Asmblock Asmblockgen Machblockgen.
+Require Import PostpassScheduling.
+Require Import Errors String.
+Require Compopts.
+
+Local Open Scope error_monad_scope.
+
+Definition time {A B: Type} (name: string) (f: A -> B) : A -> B := Compopts.time name f.
+
+Definition transf_program (p: Mach.program) : res Asm.program :=
+ let mbp := (time "Machblock generation" Machblockgen.transf_program) p in
+ do abp <- (time "Asmblock generation" Asmblockgen.transf_program) mbp;
+ do abp' <- (time "PostpassScheduling total oracle+verification" PostpassScheduling.transf_program) abp;
+ OK ((time "Asm generation" Asm.transf_program) abp').
+
+Definition transf_function (f: Mach.function) : res Asm.function :=
+ let mbf := Machblockgen.transf_function f in
+ do abf <- Asmblockgen.transf_function mbf;
+ OK (Asm.transf_function abf).
+
+Definition transl_code (f: Mach.function) (l: Mach.code) : res (list Asm.instruction) :=
+ let mbf := Machblockgen.transf_function f in
+ let mbc := Machblockgen.trans_code l in
+ do abc <- transl_blocks mbf mbc true;
+ OK (unfold abc).
diff --git a/kvx/Asmgenproof.v b/kvx/Asmgenproof.v
new file mode 100644
index 00000000..636c105f
--- /dev/null
+++ b/kvx/Asmgenproof.v
@@ -0,0 +1,96 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Composing all passes from Mach to KVX Asm *)
+
+Require Import Coqlib Errors.
+Require Import Integers Floats AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations Mach Conventions Asm Asmgen Machblockgen Asmblockgen.
+Require Import Machblockgenproof Asmblockgenproof PostpassSchedulingproof.
+
+Local Open Scope linking_scope.
+
+Definition block_passes :=
+ mkpass Machblockgenproof.match_prog
+ ::: mkpass Asmblockgenproof.match_prog
+ ::: mkpass PostpassSchedulingproof.match_prog
+ ::: mkpass Asm.match_prog
+ ::: pass_nil _.
+
+Definition match_prog := pass_match (compose_passes block_passes).
+
+Lemma transf_program_match:
+ forall p tp, Asmgen.transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros p tp H.
+ unfold Asmgen.transf_program in H. apply bind_inversion in H. destruct H.
+ inversion_clear H. apply bind_inversion in H1. destruct H1.
+ inversion_clear H. inversion H2. unfold time, Compopts.time in *. remember (Machblockgen.transf_program p) as mbp.
+ unfold match_prog; cbn.
+ exists mbp; split. apply Machblockgenproof.transf_program_match; auto.
+ exists x; split. apply Asmblockgenproof.transf_program_match; auto.
+ exists x0; split. apply PostpassSchedulingproof.transf_program_match; auto.
+ exists tp; split. apply Asm.transf_program_match; auto. auto.
+Qed.
+
+(** Return Address Offset for Mach *)
+
+Definition return_address_offset: Mach.function -> Mach.code -> ptrofs -> Prop :=
+ Mach_return_address_offset Asmblockgenproof.return_address_offset.
+
+Lemma return_address_exists:
+ forall f sg ros c, is_tail (Mcall sg ros :: c) f.(Mach.fn_code) ->
+ exists ra, return_address_offset f c ra.
+Proof.
+ intros; unfold return_address_offset; eapply Mach_return_address_exists; eauto.
+ intros; eapply Asmblockgenproof.return_address_exists; eauto.
+Qed.
+
+(** Main preservation theorem: from Mach to KVX Asm *)
+
+Section PRESERVATION.
+
+Variable prog: Mach.program.
+Variable tprog: program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Theorem transf_program_correct:
+ forward_simulation (Mach.semantics return_address_offset prog) (Asm.semantics tprog).
+Proof.
+ unfold match_prog in TRANSF. cbn in TRANSF.
+ inv TRANSF. inv H. inv H1. inv H. inv H2. inv H. inv H3. inv H.
+ eapply compose_forward_simulations.
+ exploit Machblockgenproof.transf_program_correct; eauto.
+ unfold Machblockgenproof.inv_trans_rao.
+ eapply compose_forward_simulations. apply Asmblockgenproof.transf_program_correct; eauto.
+ eapply compose_forward_simulations. apply PostpassSchedulingproof.transf_program_correct; eauto.
+ apply Asm.transf_program_correct. eauto.
+Qed.
+
+End PRESERVATION.
+
+Instance TransfAsm: TransfLink match_prog := pass_match_link (compose_passes block_passes).
+
+(*******************************************)
+(** Stub actually needed by driver/Compiler *)
+
+Module Asmgenproof0.
+
+Definition return_address_offset := return_address_offset.
+
+End Asmgenproof0.
diff --git a/kvx/Asmvliw.v b/kvx/Asmvliw.v
new file mode 100644
index 00000000..45b230e6
--- /dev/null
+++ b/kvx/Asmvliw.v
@@ -0,0 +1,1729 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Abstract syntax and semantics for VLIW semantics of KVX assembly language. *)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import ExtValues.
+Require Import Memory.
+Require Import Events.
+Require Import Globalenvs.
+Require Import Smallstep.
+Require Import Locations.
+Require Stacklayout.
+Require Import Conventions.
+Require Import Errors.
+Require Import Sorting.Permutation.
+Require Import Chunks.
+Require Import Lia.
+
+(** * Abstract syntax *)
+
+(** A KVX program is syntactically given as a list of functions.
+ Each function is associated to a list of bundles of type [bblock] below.
+ Hence, syntactically, we view each bundle as a basic block:
+ this view induces our sequential semantics of bundles defined in [Asmblock].
+*)
+
+(** ** General Purpose registers. *)
+
+Inductive gpreg: Type :=
+ | GPR0: gpreg | GPR1: gpreg | GPR2: gpreg | GPR3: gpreg | GPR4: gpreg
+ | GPR5: gpreg | GPR6: gpreg | GPR7: gpreg | GPR8: gpreg | GPR9: gpreg
+ | GPR10: gpreg | GPR11: gpreg | GPR12: gpreg | GPR13: gpreg | GPR14: gpreg
+ | GPR15: gpreg | GPR16: gpreg | GPR17: gpreg | GPR18: gpreg | GPR19: gpreg
+ | GPR20: gpreg | GPR21: gpreg | GPR22: gpreg | GPR23: gpreg | GPR24: gpreg
+ | GPR25: gpreg | GPR26: gpreg | GPR27: gpreg | GPR28: gpreg | GPR29: gpreg
+ | GPR30: gpreg | GPR31: gpreg | GPR32: gpreg | GPR33: gpreg | GPR34: gpreg
+ | GPR35: gpreg | GPR36: gpreg | GPR37: gpreg | GPR38: gpreg | GPR39: gpreg
+ | GPR40: gpreg | GPR41: gpreg | GPR42: gpreg | GPR43: gpreg | GPR44: gpreg
+ | GPR45: gpreg | GPR46: gpreg | GPR47: gpreg | GPR48: gpreg | GPR49: gpreg
+ | GPR50: gpreg | GPR51: gpreg | GPR52: gpreg | GPR53: gpreg | GPR54: gpreg
+ | GPR55: gpreg | GPR56: gpreg | GPR57: gpreg | GPR58: gpreg | GPR59: gpreg
+ | GPR60: gpreg | GPR61: gpreg | GPR62: gpreg | GPR63: gpreg.
+
+Definition ireg := gpreg.
+Definition freg := gpreg.
+
+Lemma gpreg_eq: forall (x y: gpreg), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
+Lemma ireg_eq: forall (x y: ireg), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
+Lemma freg_eq: forall (x y: freg), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
+Inductive gpreg_q : Type :=
+| R0R1 | R2R3 | R4R5 | R6R7 | R8R9
+| R10R11 | R12R13 | R14R15 | R16R17 | R18R19
+| R20R21 | R22R23 | R24R25 | R26R27 | R28R29
+| R30R31 | R32R33 | R34R35 | R36R37 | R38R39
+| R40R41 | R42R43 | R44R45 | R46R47 | R48R49
+| R50R51 | R52R53 | R54R55 | R56R57 | R58R59
+| R60R61 | R62R63.
+
+Lemma gpreg_q_eq : forall (x y : gpreg_q), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
+Definition gpreg_q_expand (x : gpreg_q) : gpreg * gpreg :=
+ match x with
+ | R0R1 => (GPR0, GPR1)
+ | R2R3 => (GPR2, GPR3)
+ | R4R5 => (GPR4, GPR5)
+ | R6R7 => (GPR6, GPR7)
+ | R8R9 => (GPR8, GPR9)
+ | R10R11 => (GPR10, GPR11)
+ | R12R13 => (GPR12, GPR13)
+ | R14R15 => (GPR14, GPR15)
+ | R16R17 => (GPR16, GPR17)
+ | R18R19 => (GPR18, GPR19)
+ | R20R21 => (GPR20, GPR21)
+ | R22R23 => (GPR22, GPR23)
+ | R24R25 => (GPR24, GPR25)
+ | R26R27 => (GPR26, GPR27)
+ | R28R29 => (GPR28, GPR29)
+ | R30R31 => (GPR30, GPR31)
+ | R32R33 => (GPR32, GPR33)
+ | R34R35 => (GPR34, GPR35)
+ | R36R37 => (GPR36, GPR37)
+ | R38R39 => (GPR38, GPR39)
+ | R40R41 => (GPR40, GPR41)
+ | R42R43 => (GPR42, GPR43)
+ | R44R45 => (GPR44, GPR45)
+ | R46R47 => (GPR46, GPR47)
+ | R48R49 => (GPR48, GPR49)
+ | R50R51 => (GPR50, GPR51)
+ | R52R53 => (GPR52, GPR53)
+ | R54R55 => (GPR54, GPR55)
+ | R56R57 => (GPR56, GPR57)
+ | R58R59 => (GPR58, GPR59)
+ | R60R61 => (GPR60, GPR61)
+ | R62R63 => (GPR62, GPR63)
+ end.
+
+Inductive gpreg_o : Type :=
+| R0R1R2R3 | R4R5R6R7 | R8R9R10R11 | R12R13R14R15
+| R16R17R18R19 | R20R21R22R23 | R24R25R26R27 | R28R29R30R31
+| R32R33R34R35 | R36R37R38R39 | R40R41R42R43 | R44R45R46R47
+| R48R49R50R51 | R52R53R54R55 | R56R57R58R59 | R60R61R62R63.
+
+Definition gpreg_o_expand (x : gpreg_o) : gpreg * gpreg * gpreg * gpreg :=
+ match x with
+ | R0R1R2R3 => (GPR0, GPR1, GPR2, GPR3)
+ | R4R5R6R7 => (GPR4, GPR5, GPR6, GPR7)
+ | R8R9R10R11 => (GPR8, GPR9, GPR10, GPR11)
+ | R12R13R14R15 => (GPR12, GPR13, GPR14, GPR15)
+ | R16R17R18R19 => (GPR16, GPR17, GPR18, GPR19)
+ | R20R21R22R23 => (GPR20, GPR21, GPR22, GPR23)
+ | R24R25R26R27 => (GPR24, GPR25, GPR26, GPR27)
+ | R28R29R30R31 => (GPR28, GPR29, GPR30, GPR31)
+ | R32R33R34R35 => (GPR32, GPR33, GPR34, GPR35)
+ | R36R37R38R39 => (GPR36, GPR37, GPR38, GPR39)
+ | R40R41R42R43 => (GPR40, GPR41, GPR42, GPR43)
+ | R44R45R46R47 => (GPR44, GPR45, GPR46, GPR47)
+ | R48R49R50R51 => (GPR48, GPR49, GPR50, GPR51)
+ | R52R53R54R55 => (GPR52, GPR53, GPR54, GPR55)
+ | R56R57R58R59 => (GPR56, GPR57, GPR58, GPR59)
+ | R60R61R62R63 => (GPR60, GPR61, GPR62, GPR63)
+ end.
+
+Lemma gpreg_o_eq : forall (x y : gpreg_o), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
+Inductive preg: Type :=
+ | IR: gpreg -> preg (**r integer general purpose registers *)
+ | RA: preg
+ | PC: preg
+ .
+
+Coercion IR: gpreg >-> preg.
+
+Lemma preg_eq: forall (x y: preg), {x=y} + {x<>y}.
+Proof. decide equality. apply ireg_eq. Defined.
+
+Module PregEq.
+ Definition t := preg.
+ Definition eq := preg_eq.
+End PregEq.
+
+(* FIXME - R16 and R32 are excluded *)
+Definition preg_of (r: mreg) : preg :=
+ match r with
+ | R0 => GPR0 | R1 => GPR1 | R2 => GPR2 | R3 => GPR3 | R4 => GPR4
+ | R5 => GPR5 | R6 => GPR6 | R7 => GPR7 | R8 => GPR8 | R9 => GPR9
+ | R10 => GPR10 | R11 => GPR11 (* | R12 => GPR12 | R13 => GPR13 | R14 => GPR14 *)
+ | R15 => GPR15 (* | R16 => GPR16 *) | R17 => GPR17 | R18 => GPR18 | R19 => GPR19
+ | R20 => GPR20 | R21 => GPR21 | R22 => GPR22 | R23 => GPR23 | R24 => GPR24
+ | R25 => GPR25 | R26 => GPR26 | R27 => GPR27 | R28 => GPR28 | R29 => GPR29
+ | R30 => GPR30 | R31 => GPR31 (* | R32 => GPR32 *) | R33 => GPR33 | R34 => GPR34
+ | R35 => GPR35 | R36 => GPR36 | R37 => GPR37 | R38 => GPR38 | R39 => GPR39
+ | R40 => GPR40 | R41 => GPR41 | R42 => GPR42 | R43 => GPR43 | R44 => GPR44
+ | R45 => GPR45 | R46 => GPR46 | R47 => GPR47 | R48 => GPR48 | R49 => GPR49
+ | R50 => GPR50 | R51 => GPR51 | R52 => GPR52 | R53 => GPR53 | R54 => GPR54
+ | R55 => GPR55 | R56 => GPR56 | R57 => GPR57 | R58 => GPR58 | R59 => GPR59
+ | R60 => GPR60 | R61 => GPR61 | R62 => GPR62 | R63 => GPR63
+ end.
+
+Definition ireg_of (r: mreg) : res ireg :=
+ match preg_of r with IR mr => OK mr | _ => Error(msg "Asmgenblock.ireg_of") end.
+
+Definition freg_of (r: mreg) : res freg :=
+ match preg_of r with IR mr => OK mr | _ => Error(msg "Asmgenblock.freg_of") end.
+
+Module Pregmap := EMap(PregEq).
+
+(** ** Conventional names for stack pointer ([SP]), return address ([RA]), frame pointer ([FP]) and other temporaries used *)
+
+Notation "'SP'" := GPR12 (only parsing) : asm.
+Notation "'FP'" := GPR17 (only parsing) : asm.
+Notation "'MFP'" := R17 (only parsing) : asm.
+Notation "'GPRA'" := GPR16 (only parsing) : asm.
+Notation "'RTMP'" := GPR32 (only parsing) : asm.
+
+(** ** Names of tests in comparisons *)
+
+Inductive btest: Type :=
+ | BTdnez (**r Double Not Equal to Zero *)
+ | BTdeqz (**r Double Equal to Zero *)
+ | BTdltz (**r Double Less Than Zero *)
+ | BTdgez (**r Double Greater Than or Equal to Zero *)
+ | BTdlez (**r Double Less Than or Equal to Zero *)
+ | BTdgtz (**r Double Greater Than Zero *)
+ | BTwnez (**r Word Not Equal to Zero *)
+ | BTweqz (**r Word Equal to Zero *)
+ | BTwltz (**r Word Less Than Zero *)
+ | BTwgez (**r Word Greater Than or Equal to Zero *)
+ | BTwlez (**r Word Less Than or Equal to Zero *)
+ | BTwgtz (**r Word Greater Than Zero *)
+ .
+
+Inductive itest: Type :=
+ | ITne (**r Not Equal *)
+ | ITeq (**r Equal *)
+ | ITlt (**r Less Than *)
+ | ITge (**r Greater Than or Equal *)
+ | ITle (**r Less Than or Equal *)
+ | ITgt (**r Greater Than *)
+ | ITneu (**r Unsigned Not Equal *)
+ | ITequ (**r Unsigned Equal *)
+ | ITltu (**r Less Than Unsigned *)
+ | ITgeu (**r Greater Than or Equal Unsigned *)
+ | ITleu (**r Less Than or Equal Unsigned *)
+ | ITgtu (**r Greater Than Unsigned *)
+ .
+
+Inductive ftest: Type :=
+ | FTone (**r Ordered and Not Equal *)
+ | FTueq (**r Unordered or Equal *)
+ | FToeq (**r Ordered and Equal *)
+ | FTune (**r Unordered or Not Equal *)
+ | FTolt (**r Ordered and Less Than *)
+ | FTuge (**r Unordered or Greater Than or Equal *)
+ | FToge (**r Ordered and Greater Than or Equal *)
+ | FTult (**r Unordered or Less Than *)
+ .
+
+(** *** Offsets for load and store instructions. *)
+
+Definition offset : Type := ptrofs.
+
+(** *** Labels for goto (in the current function) *)
+
+Definition label := positive.
+
+(** ** Instructions *)
+
+(** We model a subset of the KVX instruction set.
+
+- Although it is possible to use the 32-bits mode, for now we don't support it. When mapping to actual instructions, the OCaml code in TargetPrinter.ml
+ throws an error if we are not in 64-bits mode.
+
+- We follow a design close to the one used for the Risc-V port: one set of
+ pseudo-instructions for 32-bit integer arithmetic, with suffix W, another
+ set for 64-bit integer arithmetic, with suffix L.
+
+- With respect to other CompCert assemblies, we define a type hierarchy of instructions (instead of a flat type).
+ This helps us to factorize similar cases for the scheduling verifier.
+
+*)
+
+(** *** Instructions to be expanded in control-flow *)
+Inductive ex_instruction : Type :=
+ (* Pseudo-instructions *)
+ | Pbuiltin: external_function -> list (builtin_arg preg)
+ -> builtin_res preg -> ex_instruction (**r built-in function (pseudo) *)
+.
+
+(** Similarly to other CompCert assembly languages, the pseudo-instructions are the following:
+
+- [Ploadsymbol]: load the address of a symbol in an integer register.
+
+- [Pallocframe sz pos]: in the formal semantics, this
+ pseudo-instruction allocates a memory block with bounds [0] and
+ [sz], stores the value of the stack pointer at offset [pos] in this
+ block, and sets the stack pointer to the address of the bottom of
+ this block.
+
+ This cannot be expressed in our memory model, which does not reflect
+ the fact that stack frames are adjacent and allocated/freed
+ following a stack discipline.
+
+- [Pfreeframe sz pos]: in the formal semantics, this pseudo-instruction
+ reads the word at [pos] of the block pointed by the stack pointer,
+ frees this block, and sets the stack pointer to the value read.
+ Again, our memory model cannot comprehend that this operation
+ frees (logically) the current stack frame.
+
+- [Pbtbl reg table]: this is a N-way branch, implemented via a jump table
+*)
+
+(** *** Control Flow instructions *)
+Inductive cf_instruction : Type :=
+ | Pret (**r return *)
+ | Pcall (l: label) (**r function call *)
+ | Picall (r: ireg) (**r function call on register value *)
+ | Pjumptable (r: ireg) (labels: list label) (**r N-way branch through a jump table (pseudo) *)
+
+ (* Pgoto is for tailcalls, Pj_l is for jumping to a particular label *)
+ | Pgoto (l: label) (**r goto *)
+ | Pigoto (r: ireg) (**r goto from register *)
+ | Pj_l (l: label) (**r jump to label *)
+
+ (* Conditional branches *)
+ | Pcb (bt: btest) (r: ireg) (l: label) (**r branch based on btest *)
+ | Pcbu (bt: btest) (r: ireg) (l: label) (**r branch based on btest with unsigned semantics *)
+.
+
+(** *** Loads *)
+
+(* What follows was the original spec, but is subtly incorrect.
+ Our definition of the assembly-level memory model is already an abstraction of the real world.
+ In particular, we consider that a load is incorrect when it points outside of CompCert's visible memory, whereas this memory could be correct at the assembly level.
+ This means that CompCert would believe an incorrect load would yield 0 whereas it would yield another value.
+ match chunk with
+ | Mint8signed | Mint8unsigned | Mint16signed | Mint16unsigned
+ | Mint32 => Vint Int.zero
+ | Mint64 => Vlong Int64.zero
+ | Many32 | Many64 => Vundef
+ | Mfloat32 => Vsingle Float32.zero
+ | Mfloat64 => Vfloat Float.zero
+ end. *)
+
+Inductive load_name : Type :=
+ | Plb (**r load byte *)
+ | Plbu (**r load byte unsigned *)
+ | Plh (**r load half word *)
+ | Plhu (**r load half word unsigned *)
+ | Plw (**r load int32 *)
+ | Plw_a (**r load any32 *)
+ | Pld (**r load int64 *)
+ | Pld_a (**r load any64 *)
+ | Pfls (**r load float *)
+ | Pfld (**r load 64-bit float *)
+.
+
+Inductive ld_instruction : Type :=
+ | PLoadRRO (trap: trapping_mode) (i: load_name) (rd: ireg) (ra: ireg) (ofs: offset)
+ | PLoadRRR (trap: trapping_mode) (i: load_name) (rd: ireg) (ra: ireg) (rofs: ireg)
+ | PLoadRRRXS (trap: trapping_mode) (i: load_name) (rd: ireg) (ra: ireg) (rofs: ireg)
+ | PLoadQRRO (rd: gpreg_q) (ra: ireg) (ofs: offset)
+ | PLoadORRO (rd: gpreg_o) (ra: ireg) (ofs: offset)
+.
+
+(** *** Stores *)
+Inductive store_name : Type :=
+ | Psb (**r store byte *)
+ | Psh (**r store half byte *)
+ | Psw (**r store int32 *)
+ | Psw_a (**r store any32 *)
+ | Psd (**r store int64 *)
+ | Psd_a (**r store any64 *)
+ | Pfss (**r store float *)
+ | Pfsd (**r store 64-bit float *)
+.
+
+Inductive st_instruction : Type :=
+ | PStoreRRO (i: store_name) (rs: ireg) (ra: ireg) (ofs: offset)
+ | PStoreRRR (i: store_name) (rs: ireg) (ra: ireg) (rofs: ireg)
+ | PStoreRRRXS(i: store_name) (rs: ireg) (ra: ireg) (rofs: ireg)
+ | PStoreQRRO (rs: gpreg_q) (ra: ireg) (ofs: offset)
+ | PStoreORRO (rs: gpreg_o) (ra: ireg) (ofs: offset)
+.
+
+(** *** Arithmetic instructions *)
+Inductive arith_name_r : Type :=
+ | Ploadsymbol (id: ident) (ofs: ptrofs) (**r load the address of a symbol *)
+.
+
+Inductive arith_name_rr : Type :=
+ | Pmv (**r register move *)
+ | Pnegw (**r negate word *)
+ | Pnegl (**r negate long *)
+ | Pcvtl2w (**r Convert Long to Word *)
+ | Psxwd (**r Sign Extend Word to Double Word *)
+ | Pzxwd (**r Zero Extend Word to Double Word *)
+ | Pextfz (stop : Z) (start : Z) (**r extract bit field, unsigned *)
+ | Pextfs (stop : Z) (start : Z) (**r extract bit field, signed *)
+ | Pextfzl (stop : Z) (start : Z) (**r extract bit field, unsigned *)
+ | Pextfsl (stop : Z) (start : Z) (**r extract bit field, signed *)
+
+ | Pfabsd (**r float absolute double *)
+ | Pfabsw (**r float absolute word *)
+ | Pfnegd (**r float negate double *)
+ | Pfnegw (**r float negate word *)
+ | Pfinvw (**r float invert word *)
+ | Pfnarrowdw (**r float narrow 64 -> 32 bits *)
+ | Pfwidenlwd (**r Floating Point widen from 32 bits to 64 bits *)
+ | Pfloatwrnsz (**r Floating Point conversion from integer (int -> SINGLE) *)
+ | Pfloatuwrnsz (**r Floating Point conversion from integer (unsigned int -> SINGLE) *)
+ | Pfloatudrnsz (**r Floating Point Conversion from integer (unsigned long -> float) *)
+ | Pfloatdrnsz (**r Floating Point Conversion from integer (long -> float) *)
+ | Pfixedwrzz (**r Integer conversion from floating point (single -> int) *)
+ | Pfixeduwrzz (**r Integer conversion from floating point (single -> unsigned int) *)
+ | Pfixeddrzz (**r Integer conversion from floating point (float -> long) *)
+ | Pfixedudrzz (**r Integer conversion from floating point (float -> unsigned long) *)
+ | Pfixeddrzz_i32 (**r Integer conversion from floating point (float -> int) *)
+ | Pfixedudrzz_i32 (**r Integer conversion from floating point (float -> unsigned int) *)
+.
+
+Inductive arith_name_ri32 : Type :=
+ | Pmake (**r load immediate *)
+.
+
+Inductive arith_name_ri64 : Type :=
+ | Pmakel (**r load immediate long *)
+.
+
+Inductive arith_name_rf32 : Type :=
+ | Pmakefs (**r load immediate single *)
+.
+
+Inductive arith_name_rf64 : Type :=
+ | Pmakef (**r load immediate float *)
+.
+
+Inductive arith_name_rrr : Type :=
+ | Pcompw (it: itest) (**r comparison word *)
+ | Pcompl (it: itest) (**r comparison long *)
+ | Pfcompw (ft: ftest) (**r comparison float32 *)
+ | Pfcompl (ft: ftest) (**r comparison float64 *)
+
+ | Paddw (**r add word *)
+ | Paddxw (shift : shift1_4) (**r add shift *)
+ | Psubw (**r sub word word *)
+ | Prevsubxw (shift : shift1_4) (**r sub shift word *)
+ | Pmulw (**r mul word *)
+ | Pandw (**r and word *)
+ | Pnandw (**r nand word *)
+ | Porw (**r or word *)
+ | Pnorw (**r nor word *)
+ | Pxorw (**r xor word *)
+ | Pnxorw (**r nxor word *)
+ | Pandnw (**r andn word *)
+ | Pornw (**r orn word *)
+ | Psraw (**r shift right arithmetic word *)
+ | Psrxw (**r shift right arithmetic word round to 0*)
+ | Psrlw (**r shift right logical word *)
+ | Psllw (**r shift left logical word *)
+
+ | Paddl (**r add long *)
+ | Paddxl (shift : shift1_4) (**r add shift long *)
+ | Psubl (**r sub long *)
+ | Prevsubxl (shift : shift1_4) (**r sub shift long *)
+ | Pandl (**r and long *)
+ | Pnandl (**r nand long *)
+ | Porl (**r or long *)
+ | Pnorl (**r nor long *)
+ | Pxorl (**r xor long *)
+ | Pnxorl (**r nxor long *)
+ | Pandnl (**r andn long *)
+ | Pornl (**r orn long *)
+ | Pmull (**r mul long (low part) *)
+ | Pslll (**r shift left logical long *)
+ | Psrll (**r shift right logical long *)
+ | Psrxl (**r shift right logical long round to 0*)
+ | Psral (**r shift right arithmetic long *)
+
+ | Pfaddd (**r float add double *)
+ | Pfaddw (**r float add word *)
+ | Pfsbfd (**r float sub double *)
+ | Pfsbfw (**r float sub word *)
+ | Pfmuld (**r float multiply double *)
+ | Pfmulw (**r float multiply word *)
+ | Pfmind (**r float min double *)
+ | Pfminw (**r float min word *)
+ | Pfmaxd (**r float max double *)
+ | Pfmaxw (**r float max word *)
+.
+
+Inductive arith_name_rri32 : Type :=
+ | Pcompiw (it: itest) (**r comparison imm word *)
+
+ | Paddiw (**r add imm word *)
+ | Paddxiw (shift : shift1_4)
+ | Prevsubiw (**r add imm word *)
+ | Prevsubxiw (shift : shift1_4)
+ | Pmuliw (**r add imm word *)
+ | Pandiw (**r and imm word *)
+ | Pnandiw (**r nand imm word *)
+ | Poriw (**r or imm word *)
+ | Pnoriw (**r nor imm word *)
+ | Pxoriw (**r xor imm word *)
+ | Pnxoriw (**r nxor imm word *)
+ | Pandniw (**r andn word *)
+ | Porniw (**r orn word *)
+ | Psraiw (**r shift right arithmetic imm word *)
+ | Psrxiw (**r shift right arithmetic imm word round to 0*)
+ | Psrliw (**r shift right logical imm word *)
+ | Pslliw (**r shift left logical imm word *)
+ | Proriw (**r rotate right imm word *)
+ | Psllil (**r shift left logical immediate long *)
+ | Psrlil (**r shift right logical immediate long *)
+ | Psrail (**r shift right arithmetic immediate long *)
+ | Psrxil (**r shift right arithmetic immediate long round to 0*)
+.
+
+Inductive arith_name_rri64 : Type :=
+ | Pcompil (it: itest) (**r comparison imm long *)
+ | Paddil (**r add immediate long *)
+ | Paddxil (shift : shift1_4)
+ | Prevsubil
+ | Prevsubxil (shift : shift1_4)
+ | Pmulil (**r mul immediate long *)
+ | Pandil (**r and immediate long *)
+ | Pnandil (**r nand immediate long *)
+ | Poril (**r or immediate long *)
+ | Pnoril (**r nor immediate long *)
+ | Pxoril (**r xor immediate long *)
+ | Pnxoril (**r nxor immediate long *)
+ | Pandnil (**r andn immediate long *)
+ | Pornil (**r orn immediate long *)
+.
+
+Inductive arith_name_arrr : Type :=
+ | Pmaddw (**r multiply add word *)
+ | Pmaddl (**r multiply add long *)
+ | Pmsubw (**r multiply subtract word *)
+ | Pmsubl (**r multiply subtract long *)
+ | Pcmove (bt: btest) (**r conditional move *)
+ | Pcmoveu (bt: btest) (**r conditional move, test on unsigned semantics *)
+ | Pfmaddfw (**r float fused multiply add word *)
+ | Pfmaddfl (**r float fused multiply add long *)
+ | Pfmsubfw (**r float fused multiply subtract word *)
+ | Pfmsubfl (**r float fused multiply subtract long *)
+.
+
+Inductive arith_name_arri32 : Type :=
+ | Pmaddiw (**r multiply add word *)
+ | Pcmoveiw (bt: btest)
+ | Pcmoveuiw (bt: btest)
+.
+
+Inductive arith_name_arri64 : Type :=
+ | Pmaddil (**r multiply add long *)
+ | Pcmoveil (bt: btest)
+ | Pcmoveuil (bt: btest)
+.
+
+Inductive arith_name_arr : Type :=
+ | Pinsf (stop : Z) (start : Z) (**r insert bit field *)
+ | Pinsfl (stop : Z) (start : Z) (**r insert bit field *)
+.
+
+Inductive ar_instruction : Type :=
+ | PArithR (i: arith_name_r) (rd: ireg)
+ | PArithRR (i: arith_name_rr) (rd rs: ireg)
+ | PArithRI32 (i: arith_name_ri32) (rd: ireg) (imm: int)
+ | PArithRI64 (i: arith_name_ri64) (rd: ireg) (imm: int64)
+ | PArithRF32 (i: arith_name_rf32) (rd: ireg) (imm: float32)
+ | PArithRF64 (i: arith_name_rf64) (rd: ireg) (imm: float)
+ | PArithRRR (i: arith_name_rrr) (rd rs1 rs2: ireg)
+ | PArithRRI32 (i: arith_name_rri32) (rd rs: ireg) (imm: int)
+ | PArithRRI64 (i: arith_name_rri64) (rd rs: ireg) (imm: int64)
+ | PArithARRR (i: arith_name_arrr) (rd rs1 rs2: ireg)
+ | PArithARR (i: arith_name_arr) (rd rs: ireg)
+ | PArithARRI32 (i: arith_name_arri32) (rd rs: ireg) (imm: int)
+ | PArithARRI64 (i: arith_name_arri64) (rd rs: ireg) (imm: int64)
+.
+
+Module PArithCoercions.
+
+Coercion PArithR: arith_name_r >-> Funclass.
+Coercion PArithRR: arith_name_rr >-> Funclass.
+Coercion PArithRI32: arith_name_ri32 >-> Funclass.
+Coercion PArithRI64: arith_name_ri64 >-> Funclass.
+Coercion PArithRF32: arith_name_rf32 >-> Funclass.
+Coercion PArithRF64: arith_name_rf64 >-> Funclass.
+Coercion PArithRRR: arith_name_rrr >-> Funclass.
+Coercion PArithRRI32: arith_name_rri32 >-> Funclass.
+Coercion PArithRRI64: arith_name_rri64 >-> Funclass.
+Coercion PArithARRR: arith_name_arrr >-> Funclass.
+Coercion PArithARR: arith_name_arr >-> Funclass.
+Coercion PArithARRI32: arith_name_arri32 >-> Funclass.
+Coercion PArithARRI64: arith_name_arri64 >-> Funclass.
+
+End PArithCoercions.
+
+(** ** Basic instructions *)
+
+Inductive basic : Type :=
+ | PArith (i: ar_instruction)
+ | PLoad (i: ld_instruction)
+ | PStore (i: st_instruction)
+ | Pallocframe (sz: Z) (pos: ptrofs) (**r allocate new stack frame *)
+ | Pfreeframe (sz: Z) (pos: ptrofs) (**r deallocate stack frame and restore previous frame *)
+ | Pget (rd: ireg) (rs: preg) (**r get system register *)
+ | Pset (rd: preg) (rs: ireg) (**r set system register *)
+ | Pnop (**r virtual instruction that does nothing *)
+.
+
+Coercion PLoad: ld_instruction >-> basic.
+Coercion PStore: st_instruction >-> basic.
+Coercion PArith: ar_instruction >-> basic.
+
+(** ** Control-flow instructions *)
+
+Inductive control : Type :=
+ | PExpand (i: ex_instruction)
+ | PCtlFlow (i: cf_instruction)
+.
+
+Coercion PExpand: ex_instruction >-> control.
+Coercion PCtlFlow: cf_instruction >-> control.
+
+
+(** * Definition of a bblock (ie a bundle) *)
+
+(** A bundle/bblock must contain at least one instruction.
+
+This choice simplifies the definition of [find_bblock] below:
+indeed, each address of a code block identifies at most one bundle
+(which depends on the number of instructions in the bundles of lower addresses).
+
+*)
+
+Definition non_empty_body (body: list basic): bool :=
+ match body with
+ | nil => false
+ | _ => true
+ end.
+
+Definition non_empty_exit (exit: option control): bool :=
+ match exit with
+ | None => false
+ | _ => true
+ end.
+
+Definition non_empty_bblockb (body: list basic) (exit: option control): bool := non_empty_body body || non_empty_exit exit.
+
+
+(** For now, we consider a builtin is alone in a bundle (and a basic block).
+ Is there a way to avoid that ? (TODO)
+ *)
+Definition builtin_aloneb (body: list basic) (exit: option control) :=
+ match exit with
+ | Some (PExpand (Pbuiltin _ _ _)) =>
+ match body with
+ | nil => true
+ | _ => false
+ end
+ | _ => true
+ end.
+
+Definition wf_bblockb (body: list basic) (exit: option control) :=
+ (non_empty_bblockb body exit) && (builtin_aloneb body exit).
+
+(** A bblock is well-formed if he contains at least one instruction,
+ and if there is a builtin then it must be alone in this bblock. *)
+
+Record bblock := mk_bblock {
+ header: list label;
+ body: list basic;
+ exit: option control;
+ correct: Is_true (wf_bblockb body exit)
+}.
+
+(* FIXME? redundant with definition in Machblock *)
+Definition length_opt {A} (o: option A) : nat :=
+ match o with
+ | Some o => 1
+ | None => 0
+ end.
+
+(** The notion of size induces the notion of "valid" code address given by [find_bblock]
+ The result is in Z to be compatible with operations on PC.
+
+ WARNING: this notion of size is not the same than in Machblock !
+ We ignore labels here...
+
+*)
+Definition size (b:bblock): Z := Z.of_nat (length (body b) + length_opt (exit b)).
+
+Definition bblocks := list bblock.
+
+Fixpoint size_blocks (l: bblocks): Z :=
+ match l with
+ | nil => 0
+ | b :: l =>
+ (size b) + (size_blocks l)
+ end
+ .
+
+Record function : Type := mkfunction { fn_sig: signature; fn_blocks: bblocks }.
+Definition fundef := AST.fundef function.
+Definition program := AST.program fundef unit.
+
+(** * Parallel Semantics of bundles *)
+
+(** The semantics operates over a single mapping from registers
+ (type [preg]) to values. We maintain
+ the convention that integer registers are mapped to values of
+ type [Tint] or [Tlong] (in 64 bit mode),
+ and float registers to values of type [Tsingle] or [Tfloat]. *)
+
+Definition regset := Pregmap.t val.
+
+Definition genv := Genv.t fundef unit.
+
+Notation "a # b" := (a b) (at level 1, only parsing) : asm.
+Notation "a # b <- c" := (Pregmap.set b c a) (at level 1, b at next level) : asm.
+
+Open Scope asm.
+
+(** *** Undefining some registers *)
+
+Fixpoint undef_regs (l: list preg) (rs: regset) : regset :=
+ match l with
+ | nil => rs
+ | r :: l' => undef_regs l' (rs#r <- Vundef)
+ end.
+
+
+(** *** Assigning a register pair *)
+Definition set_pair (p: rpair preg) (v: val) (rs: regset) : regset :=
+ match p with
+ | One r => rs#r <- v
+ | Twolong rhi rlo => rs#rhi <- (Val.hiword v) #rlo <- (Val.loword v)
+ end.
+
+
+(** *** Assigning the result of a builtin *)
+
+Fixpoint set_res (res: builtin_res preg) (v: val) (rs: regset) : regset :=
+ match res with
+ | BR r => rs#r <- v
+ | BR_none => rs
+ | BR_splitlong hi lo => set_res lo (Val.loword v) (set_res hi (Val.hiword v) rs)
+ end.
+
+Local Open Scope asm.
+
+Section RELSEM.
+
+Variable ge: genv.
+
+(** The parallel semantics on bundles is purely small-step and defined as a relation
+ from the current state (a register set + a memory state) to either [Next rs' m']
+ where [rs'] and [m'] are the updated register set and memory state after execution
+ of the instruction at [rs#PC], or [Stuck] if the processor is stuck.
+
+ The parallel semantics of each instructions handles two states in input:
+ - the actual input state of the bundle which is only read
+ - and the other on which every "write" is performed:
+ it represents a temporary "writes" buffer, from which the final state
+ of the bundle is computed.
+
+ NB: the sequential semantics defined in [Asmblock] is derived
+ from the parallel semantics of each instruction by identifying
+ the read state and the write state.
+
+*)
+
+Inductive outcome: Type :=
+ | Next (rs:regset) (m:mem)
+ | Stuck
+.
+
+(** *** Arithmetic Expressions (including comparisons) *)
+
+Inductive signedness: Type := Signed | Unsigned.
+
+Inductive intsize: Type := Int | Long.
+
+Definition itest_for_cmp (c: comparison) (s: signedness) :=
+ match c, s with
+ | Cne, Signed => ITne
+ | Ceq, Signed => ITeq
+ | Clt, Signed => ITlt
+ | Cge, Signed => ITge
+ | Cle, Signed => ITle
+ | Cgt, Signed => ITgt
+ | Cne, Unsigned => ITneu
+ | Ceq, Unsigned => ITequ
+ | Clt, Unsigned => ITltu
+ | Cge, Unsigned => ITgeu
+ | Cle, Unsigned => ITleu
+ | Cgt, Unsigned => ITgtu
+ end.
+
+Inductive oporder_ftest :=
+ | Normal (ft: ftest)
+ | Reversed (ft: ftest)
+.
+
+Definition ftest_for_cmp (c: comparison) :=
+ match c with
+ | Ceq => Normal FToeq
+ | Cne => Normal FTune
+ | Clt => Normal FTolt
+ | Cle => Reversed FToge
+ | Cgt => Reversed FTolt
+ | Cge => Normal FToge
+ end.
+
+Definition notftest_for_cmp (c: comparison) :=
+ match c with
+ | Ceq => Normal FTune
+ | Cne => Normal FToeq
+ | Clt => Normal FTuge
+ | Cle => Reversed FTult
+ | Cgt => Reversed FTuge
+ | Cge => Normal FTult
+ end.
+
+(* **** CoMPare Signed Words to Zero *)
+Definition btest_for_cmpswz (c: comparison) :=
+ match c with
+ | Cne => BTwnez
+ | Ceq => BTweqz
+ | Clt => BTwltz
+ | Cge => BTwgez
+ | Cle => BTwlez
+ | Cgt => BTwgtz
+ end.
+
+(* **** CoMPare Signed Doubles to Zero *)
+Definition btest_for_cmpsdz (c: comparison) :=
+ match c with
+ | Cne => BTdnez
+ | Ceq => BTdeqz
+ | Clt => BTdltz
+ | Cge => BTdgez
+ | Cle => BTdlez
+ | Cgt => BTdgtz
+ end.
+
+Definition cmp_for_btest (bt: btest) :=
+ match bt with
+ | BTwnez => (Some Cne, Int)
+ | BTweqz => (Some Ceq, Int)
+ | BTwltz => (Some Clt, Int)
+ | BTwgez => (Some Cge, Int)
+ | BTwlez => (Some Cle, Int)
+ | BTwgtz => (Some Cgt, Int)
+
+ | BTdnez => (Some Cne, Long)
+ | BTdeqz => (Some Ceq, Long)
+ | BTdltz => (Some Clt, Long)
+ | BTdgez => (Some Cge, Long)
+ | BTdlez => (Some Cle, Long)
+ | BTdgtz => (Some Cgt, Long)
+ end.
+
+Definition cmpu_for_btest (bt: btest) :=
+ match bt with
+ | BTwnez => (Some Cne, Int)
+ | BTweqz => (Some Ceq, Int)
+ | BTdnez => (Some Cne, Long)
+ | BTdeqz => (Some Ceq, Long)
+ | _ => (None, Int)
+ end.
+
+
+(** **** Comparing integers *)
+Definition compare_int (t: itest) (v1 v2: val): val :=
+ match t with
+ | ITne => Val.cmp Cne v1 v2
+ | ITeq => Val.cmp Ceq v1 v2
+ | ITlt => Val.cmp Clt v1 v2
+ | ITge => Val.cmp Cge v1 v2
+ | ITle => Val.cmp Cle v1 v2
+ | ITgt => Val.cmp Cgt v1 v2
+ | ITneu => Val.mxcmpu Cne v1 v2
+ | ITequ => Val.mxcmpu Ceq v1 v2
+ | ITltu => Val.mxcmpu Clt v1 v2
+ | ITgeu => Val.mxcmpu Cge v1 v2
+ | ITleu => Val.mxcmpu Cle v1 v2
+ | ITgtu => Val.mxcmpu Cgt v1 v2
+ end.
+
+Definition compare_long (t: itest) (v1 v2: val): val :=
+ let res := match t with
+ | ITne => Val.cmpl Cne v1 v2
+ | ITeq => Val.cmpl Ceq v1 v2
+ | ITlt => Val.cmpl Clt v1 v2
+ | ITge => Val.cmpl Cge v1 v2
+ | ITle => Val.cmpl Cle v1 v2
+ | ITgt => Val.cmpl Cgt v1 v2
+ | ITneu => Some (Val.mxcmplu Cne v1 v2)
+ | ITequ => Some (Val.mxcmplu Ceq v1 v2)
+ | ITltu => Some (Val.mxcmplu Clt v1 v2)
+ | ITgeu => Some (Val.mxcmplu Cge v1 v2)
+ | ITleu => Some (Val.mxcmplu Cle v1 v2)
+ | ITgtu => Some (Val.mxcmplu Cgt v1 v2)
+ end in
+ match res with
+ | Some v => v
+ | None => Vundef
+ end
+ .
+
+Definition compare_single (t: ftest) (v1 v2: val): val :=
+ match t with
+ | FTone | FTueq => Vundef (* unused *)
+ | FToeq => Val.cmpfs Ceq v1 v2
+ | FTune => Val.cmpfs Cne v1 v2
+ | FTolt => Val.cmpfs Clt v1 v2
+ | FTuge => Val.notbool (Val.cmpfs Clt v1 v2)
+ | FToge => Val.cmpfs Cge v1 v2
+ | FTult => Val.notbool (Val.cmpfs Cge v1 v2)
+ end.
+
+Definition compare_float (t: ftest) (v1 v2: val): val :=
+ match t with
+ | FTone | FTueq => Vundef (* unused *)
+ | FToeq => Val.cmpf Ceq v1 v2
+ | FTune => Val.cmpf Cne v1 v2
+ | FTolt => Val.cmpf Clt v1 v2
+ | FTuge => Val.notbool (Val.cmpf Clt v1 v2)
+ | FToge => Val.cmpf Cge v1 v2
+ | FTult => Val.notbool (Val.cmpf Cge v1 v2)
+ end.
+
+(** **** Arithmetic evaluators *)
+
+Definition arith_eval_r n :=
+ match n with
+ | Ploadsymbol s ofs => Genv.symbol_address ge s ofs
+ end
+.
+
+Definition arith_eval_rr n v :=
+ match n with
+ | Pmv => v
+ | Pnegw => Val.neg v
+ | Pnegl => Val.negl v
+ | Pcvtl2w => Val.loword v
+ | Psxwd => Val.longofint v
+ | Pzxwd => Val.longofintu v
+ | Pextfz stop start => extfz stop start v
+ | Pextfs stop start => extfs stop start v
+ | Pextfzl stop start => extfzl stop start v
+ | Pextfsl stop start => extfsl stop start v
+ | Pfnegd => Val.negf v
+ | Pfnegw => Val.negfs v
+ | Pfabsd => Val.absf v
+ | Pfabsw => Val.absfs v
+ | Pfinvw => ExtValues.invfs v
+ | Pfnarrowdw => Val.singleoffloat v
+ | Pfwidenlwd => Val.floatofsingle v
+ | Pfloatwrnsz => Val.maketotal (Val.singleofint v)
+ | Pfloatuwrnsz => Val.maketotal (Val.singleofintu v)
+ | Pfloatudrnsz => Val.maketotal (Val.floatoflongu v)
+ | Pfloatdrnsz => Val.maketotal (Val.floatoflong v)
+ | Pfixedwrzz => Val.maketotal (Val.intofsingle v)
+ | Pfixeduwrzz => Val.maketotal (Val.intuofsingle v)
+ | Pfixeddrzz => Val.maketotal (Val.longoffloat v)
+ | Pfixedudrzz => Val.maketotal (Val.longuoffloat v)
+ | Pfixeddrzz_i32 => Val.maketotal (Val.intoffloat v)
+ | Pfixedudrzz_i32 => Val.maketotal (Val.intuoffloat v)
+ end.
+
+Definition arith_eval_ri32 n i :=
+ match n with
+ | Pmake => Vint i
+ end.
+
+Definition arith_eval_ri64 n i :=
+ match n with
+ | Pmakel => Vlong i
+ end.
+
+Definition arith_eval_rf32 n i :=
+ match n with
+ | Pmakefs => Vsingle i
+ end.
+
+Definition arith_eval_rf64 n i :=
+ match n with
+ | Pmakef => Vfloat i
+ end.
+
+Definition arith_eval_rrr n v1 v2 :=
+ match n with
+ | Pcompw c => compare_int c v1 v2
+ | Pcompl c => compare_long c v1 v2
+ | Pfcompw c => compare_single c v1 v2
+ | Pfcompl c => compare_float c v1 v2
+
+ | Paddw => Val.add v1 v2
+ | Psubw => Val.sub v1 v2
+ | Pmulw => Val.mul v1 v2
+ | Pandw => Val.and v1 v2
+ | Pnandw => Val.notint (Val.and v1 v2)
+ | Porw => Val.or v1 v2
+ | Pnorw => Val.notint (Val.or v1 v2)
+ | Pxorw => Val.xor v1 v2
+ | Pnxorw => Val.notint (Val.xor v1 v2)
+ | Pandnw => Val.and (Val.notint v1) v2
+ | Pornw => Val.or (Val.notint v1) v2
+ | Psrlw => Val.shru v1 v2
+ | Psraw => Val.shr v1 v2
+ | Psllw => Val.shl v1 v2
+ | Psrxw => ExtValues.val_shrx v1 v2
+
+ | Paddl => Val.addl v1 v2
+ | Psubl => Val.subl v1 v2
+ | Pandl => Val.andl v1 v2
+ | Pnandl => Val.notl (Val.andl v1 v2)
+ | Porl => Val.orl v1 v2
+ | Pnorl => Val.notl (Val.orl v1 v2)
+ | Pxorl => Val.xorl v1 v2
+ | Pnxorl => Val.notl (Val.xorl v1 v2)
+ | Pandnl => Val.andl (Val.notl v1) v2
+ | Pornl => Val.orl (Val.notl v1) v2
+ | Pmull => Val.mull v1 v2
+ | Pslll => Val.shll v1 v2
+ | Psrll => Val.shrlu v1 v2
+ | Psral => Val.shrl v1 v2
+ | Psrxl => ExtValues.val_shrxl v1 v2
+
+ | Pfaddd => Val.addf v1 v2
+ | Pfaddw => Val.addfs v1 v2
+ | Pfsbfd => Val.subf v1 v2
+ | Pfsbfw => Val.subfs v1 v2
+ | Pfmuld => Val.mulf v1 v2
+ | Pfmulw => Val.mulfs v1 v2
+
+ | Pfmind => ExtValues.minf v1 v2
+ | Pfminw => ExtValues.minfs v1 v2
+ | Pfmaxd => ExtValues.maxf v1 v2
+ | Pfmaxw => ExtValues.maxfs v1 v2
+
+ | Paddxw shift => ExtValues.addx (int_of_shift1_4 shift) v1 v2
+ | Paddxl shift => ExtValues.addxl (int_of_shift1_4 shift) v1 v2
+
+ | Prevsubxw shift => ExtValues.revsubx (int_of_shift1_4 shift) v1 v2
+ | Prevsubxl shift => ExtValues.revsubxl (int_of_shift1_4 shift) v1 v2
+ end.
+
+Definition arith_eval_rri32 n v i :=
+ match n with
+ | Pcompiw c => compare_int c v (Vint i)
+ | Paddiw => Val.add v (Vint i)
+ | Prevsubiw => Val.sub (Vint i) v
+ | Pmuliw => Val.mul v (Vint i)
+ | Pandiw => Val.and v (Vint i)
+ | Pnandiw => Val.notint (Val.and v (Vint i))
+ | Poriw => Val.or v (Vint i)
+ | Pnoriw => Val.notint (Val.or v (Vint i))
+ | Pxoriw => Val.xor v (Vint i)
+ | Pnxoriw => Val.notint (Val.xor v (Vint i))
+ | Pandniw => Val.and (Val.notint v) (Vint i)
+ | Porniw => Val.or (Val.notint v) (Vint i)
+ | Psraiw => Val.shr v (Vint i)
+ | Psrxiw => ExtValues.val_shrx v (Vint i)
+ | Psrliw => Val.shru v (Vint i)
+ | Pslliw => Val.shl v (Vint i)
+ | Proriw => Val.ror v (Vint i)
+ | Psllil => Val.shll v (Vint i)
+ | Psrxil => ExtValues.val_shrxl v (Vint i)
+ | Psrlil => Val.shrlu v (Vint i)
+ | Psrail => Val.shrl v (Vint i)
+ | Paddxiw shift => ExtValues.addx (int_of_shift1_4 shift) v (Vint i)
+ | Prevsubxiw shift => ExtValues.revsubx (int_of_shift1_4 shift) v (Vint i)
+ end.
+
+Definition arith_eval_rri64 n v i :=
+ match n with
+ | Pcompil c => compare_long c v (Vlong i)
+ | Paddil => Val.addl v (Vlong i)
+ | Prevsubil => Val.subl (Vlong i) v
+ | Pmulil => Val.mull v (Vlong i)
+ | Pandil => Val.andl v (Vlong i)
+ | Pnandil => Val.notl (Val.andl v (Vlong i))
+ | Poril => Val.orl v (Vlong i)
+ | Pnoril => Val.notl (Val.orl v (Vlong i))
+ | Pxoril => Val.xorl v (Vlong i)
+ | Pnxoril => Val.notl (Val.xorl v (Vlong i))
+ | Pandnil => Val.andl (Val.notl v) (Vlong i)
+ | Pornil => Val.orl (Val.notl v) (Vlong i)
+ | Paddxil shift => ExtValues.addxl (int_of_shift1_4 shift) v (Vlong i)
+ | Prevsubxil shift => ExtValues.revsubxl (int_of_shift1_4 shift) v (Vlong i)
+ end.
+
+Definition cmove bt v1 v2 v3 :=
+ match cmp_for_btest bt with
+ | (Some c, Int) =>
+ match Val.cmp_bool c v2 (Vint Int.zero) with
+ | None => Vundef
+ | Some true => v3
+ | Some false => v1
+ end
+ | (Some c, Long) =>
+ match Val.cmpl_bool c v2 (Vlong Int64.zero) with
+ | None => Vundef
+ | Some true => v3
+ | Some false => v1
+ end
+ | (None, _) => Vundef
+ end.
+
+Definition cmoveu bt v1 v2 v3 :=
+ match cmpu_for_btest bt with
+ | (Some c, Int) =>
+ match Val.mxcmpu_bool c v2 (Vint Int.zero) with
+ | None => Vundef
+ | Some true => v3
+ | Some false => v1
+ end
+ | (Some c, Long) =>
+ match Val.mxcmplu_bool c v2 (Vlong Int64.zero) with
+ | None => Vundef
+ | Some true => v3
+ | Some false => v1
+ end
+ | (None, _) => Vundef
+ end.
+
+Definition arith_eval_arrr n v1 v2 v3 :=
+ match n with
+ | Pmaddw => Val.add v1 (Val.mul v2 v3)
+ | Pmaddl => Val.addl v1 (Val.mull v2 v3)
+ | Pmsubw => Val.sub v1 (Val.mul v2 v3)
+ | Pmsubl => Val.subl v1 (Val.mull v2 v3)
+ | Pcmove bt => cmove bt v1 v2 v3
+ | Pcmoveu bt => cmoveu bt v1 v2 v3
+ | Pfmaddfw => ExtValues.fmaddfs v1 v2 v3
+ | Pfmaddfl => ExtValues.fmaddf v1 v2 v3
+ | Pfmsubfw => ExtValues.fmsubfs v1 v2 v3
+ | Pfmsubfl => ExtValues.fmsubf v1 v2 v3
+ end.
+
+Definition arith_eval_arr n v1 v2 :=
+ match n with
+ | Pinsf stop start => ExtValues.insf stop start v1 v2
+ | Pinsfl stop start => ExtValues.insfl stop start v1 v2
+ end.
+
+Definition arith_eval_arri32 n v1 v2 v3 :=
+ match n with
+ | Pmaddiw => Val.add v1 (Val.mul v2 (Vint v3))
+ | Pcmoveiw bt => cmove bt v1 v2 (Vint v3)
+ | Pcmoveuiw bt => cmoveu bt v1 v2 (Vint v3)
+ end.
+
+Definition arith_eval_arri64 n v1 v2 v3 :=
+ match n with
+ | Pmaddil => Val.addl v1 (Val.mull v2 (Vlong v3))
+ | Pcmoveil bt => cmove bt v1 v2 (Vlong v3)
+ | Pcmoveuil bt => cmoveu bt v1 v2 (Vlong v3)
+ end.
+
+Definition parexec_arith_instr (ai: ar_instruction) (rsr rsw: regset): regset :=
+ match ai with
+ | PArithR n d => rsw#d <- (arith_eval_r n)
+
+ | PArithRR n d s => rsw#d <- (arith_eval_rr n rsr#s)
+
+ | PArithRI32 n d i => rsw#d <- (arith_eval_ri32 n i)
+ | PArithRI64 n d i => rsw#d <- (arith_eval_ri64 n i)
+ | PArithRF32 n d i => rsw#d <- (arith_eval_rf32 n i)
+ | PArithRF64 n d i => rsw#d <- (arith_eval_rf64 n i)
+
+ | PArithRRR n d s1 s2 => rsw#d <- (arith_eval_rrr n rsr#s1 rsr#s2)
+ | PArithRRI32 n d s i => rsw#d <- (arith_eval_rri32 n rsr#s i)
+ | PArithRRI64 n d s i => rsw#d <- (arith_eval_rri64 n rsr#s i)
+
+ | PArithARRR n d s1 s2 => rsw#d <- (arith_eval_arrr n rsr#d rsr#s1 rsr#s2)
+ | PArithARR n d s => rsw#d <- (arith_eval_arr n rsr#d rsr#s)
+ | PArithARRI32 n d s i => rsw#d <- (arith_eval_arri32 n rsr#d rsr#s i)
+ | PArithARRI64 n d s i => rsw#d <- (arith_eval_arri64 n rsr#d rsr#s i)
+ end.
+
+Definition eval_offset (ofs: offset) : res ptrofs := OK ofs.
+
+(** *** load/store instructions *)
+
+Definition parexec_incorrect_load trap d rsw mw :=
+ match trap with
+ | TRAP => Stuck
+ | NOTRAP => Next (rsw#d <- Vundef) mw
+ end.
+
+Definition parexec_load_offset (trap: trapping_mode) (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (d a: ireg) (ofs: offset) :=
+ match (eval_offset ofs) with
+ | OK ptr => match Mem.loadv chunk mr (Val.offset_ptr (rsr a) ptr) with
+ | None => parexec_incorrect_load trap d rsw mw
+ | Some v => Next (rsw#d <- v) mw
+ end
+ | _ => Stuck
+ end.
+
+Definition parexec_load_q_offset (rsr rsw: regset) (mr mw: mem) (d : gpreg_q) (a: ireg) (ofs: offset) :=
+ let (rd0, rd1) := gpreg_q_expand d in
+(* NB: By construction of [gpreg_q], register rd0 and rd1 are distinct, thus, the register writes cannot overlap.
+ But we do not need to express/prove this in the semantics.
+*)
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) ofs) with
+ | None => Stuck
+ | Some v0 =>
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 8))) with
+ | None => Stuck
+ | Some v1 => Next (rsw#rd0 <- v0 #rd1 <- v1) mw
+ end
+ end.
+
+Definition parexec_load_o_offset (rsr rsw: regset) (mr mw: mem) (d : gpreg_o) (a: ireg) (ofs: offset) :=
+ match gpreg_o_expand d with
+ | (rd0, rd1, rd2, rd3) =>
+(* NB: By construction of [gpreg_o], the four destination registers are pairwise distinct, thus, the register writes cannot overlap.
+ But we do not need to express/prove this in the semantics.
+*)
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) ofs) with
+ | None => Stuck
+ | Some v0 =>
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 8))) with
+ | None => Stuck
+ | Some v1 =>
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 16))) with
+ | None => Stuck
+ | Some v2 =>
+ match Mem.loadv Many64 mr (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 24))) with
+ | None => Stuck
+ | Some v3 =>
+ Next (rsw#rd0 <- v0 #rd1 <- v1 #rd2 <- v2 #rd3 <- v3) mw
+ end
+ end
+ end
+ end
+ end.
+
+Definition parexec_load_reg (trap: trapping_mode) (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (d a ro: ireg) :=
+ match Mem.loadv chunk mr (Val.addl (rsr a) (rsr ro)) with
+ | None => parexec_incorrect_load trap d rsw mw
+ | Some v => Next (rsw#d <- v) mw
+ end.
+
+Definition parexec_load_regxs (trap: trapping_mode) (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (d a ro: ireg) :=
+ match Mem.loadv chunk mr (Val.addl (rsr a) (Val.shll (rsr ro) (scale_of_chunk chunk))) with
+ | None => parexec_incorrect_load trap d rsw mw
+ | Some v => Next (rsw#d <- v) mw
+ end.
+
+Definition parexec_store_offset (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (s a: ireg) (ofs: offset) :=
+ match (eval_offset ofs) with
+ | OK ptr => match Mem.storev chunk mr (Val.offset_ptr (rsr a) ptr) (rsr s) with
+ | None => Stuck
+ | Some m' => Next rsw m'
+ end
+ | _ => Stuck
+ end.
+
+Definition parexec_store_reg
+ (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (s a ro: ireg) :=
+ match Mem.storev chunk mr (Val.addl (rsr a) (rsr ro)) (rsr s) with
+ | None => Stuck
+ | Some m' => Next rsw m'
+ end.
+
+Definition parexec_store_regxs (chunk: memory_chunk) (rsr rsw: regset) (mr mw: mem) (s a ro: ireg) :=
+ match Mem.storev chunk mr (Val.addl (rsr a) (Val.shll (rsr ro) (scale_of_chunk chunk))) (rsr s) with
+ | None => Stuck
+ | Some m' => Next rsw m'
+ end.
+
+Definition parexec_store_q_offset (rsr rsw: regset) (mr mw: mem) (s : gpreg_q) (a: ireg) (ofs: offset) :=
+ let (s0, s1) := gpreg_q_expand s in
+ match Mem.storev Many64 mr (Val.offset_ptr (rsr a) ofs) (rsr s0) with
+ | None => Stuck
+ | Some m1 =>
+ match Mem.storev Many64 m1 (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 8))) (rsr s1) with
+ | None => Stuck
+ | Some m2 => Next rsw m2
+ end
+ end.
+
+Definition parexec_store_o_offset (rsr rsw: regset) (mr mw: mem) (s : gpreg_o) (a: ireg) (ofs: offset) :=
+ match gpreg_o_expand s with
+ | (s0, s1, s2, s3) =>
+ match Mem.storev Many64 mr (Val.offset_ptr (rsr a) ofs) (rsr s0) with
+ | None => Stuck
+ | Some m1 =>
+ match Mem.storev Many64 m1 (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 8))) (rsr s1) with
+ | None => Stuck
+ | Some m2 =>
+ match Mem.storev Many64 m2 (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 16))) (rsr s2) with
+ | None => Stuck
+ | Some m3 =>
+ match Mem.storev Many64 m3 (Val.offset_ptr (rsr a) (Ptrofs.add ofs (Ptrofs.repr 24))) (rsr s3) with
+ | None => Stuck
+ | Some m4 => Next rsw m4
+ end
+ end
+ end
+ end
+ end.
+
+
+Definition load_chunk n :=
+ match n with
+ | Plb => Mint8signed
+ | Plbu => Mint8unsigned
+ | Plh => Mint16signed
+ | Plhu => Mint16unsigned
+ | Plw => Mint32
+ | Plw_a => Many32
+ | Pld => Mint64
+ | Pld_a => Many64
+ | Pfls => Mfloat32
+ | Pfld => Mfloat64
+ end.
+
+Definition store_chunk n :=
+ match n with
+ | Psb => Mint8unsigned
+ | Psh => Mint16unsigned
+ | Psw => Mint32
+ | Psw_a => Many32
+ | Psd => Mint64
+ | Psd_a => Many64
+ | Pfss => Mfloat32
+ | Pfsd => Mfloat64
+ end.
+
+(** ** Basic (instruction) step *)
+
+Definition bstep (bi: basic) (rsr rsw: regset) (mr mw: mem) :=
+ match bi with
+ | PArith ai => Next (parexec_arith_instr ai rsr rsw) mw
+
+ | PLoad (PLoadRRO trap n d a ofs) => parexec_load_offset trap (load_chunk n) rsr rsw mr mw d a ofs
+ | PLoad (PLoadRRR trap n d a ro) => parexec_load_reg trap (load_chunk n) rsr rsw mr mw d a ro
+ | PLoad (PLoadRRRXS trap n d a ro) => parexec_load_regxs trap (load_chunk n) rsr rsw mr mw d a ro
+ | PLoad (PLoadQRRO d a ofs) =>
+ parexec_load_q_offset rsr rsw mr mw d a ofs
+ | PLoad (PLoadORRO d a ofs) =>
+ parexec_load_o_offset rsr rsw mr mw d a ofs
+
+ | PStoreRRO n s a ofs => parexec_store_offset (store_chunk n) rsr rsw mr mw s a ofs
+ | PStoreRRR n s a ro => parexec_store_reg (store_chunk n) rsr rsw mr mw s a ro
+ | PStoreRRRXS n s a ro => parexec_store_regxs (store_chunk n) rsr rsw mr mw s a ro
+ | PStoreQRRO s a ofs =>
+ parexec_store_q_offset rsr rsw mr mw s a ofs
+ | PStoreORRO s a ofs =>
+ parexec_store_o_offset rsr rsw mr mw s a ofs
+
+ | Pallocframe sz pos =>
+ let (mw, stk) := Mem.alloc mr 0 sz in
+ let sp := (Vptr stk Ptrofs.zero) in
+ match Mem.storev Mptr mw (Val.offset_ptr sp pos) rsr#SP with
+ | None => Stuck
+ | Some mw => Next (rsw #FP <- (rsr SP) #SP <- sp #RTMP <- Vundef) mw
+ end
+
+ | Pfreeframe sz pos =>
+ match Mem.loadv Mptr mr (Val.offset_ptr rsr#SP pos) with
+ | None => Stuck
+ | Some v =>
+ match rsr SP with
+ | Vptr stk ofs =>
+ match Mem.free mr stk 0 sz with
+ | None => Stuck
+ | Some mw => Next (rsw#SP <- v #RTMP <- Vundef) mw
+ end
+ | _ => Stuck
+ end
+ end
+ | Pget rd ra =>
+ match ra with
+ | RA => Next (rsw#rd <- (rsr#ra)) mw
+ | _ => Stuck
+ end
+ | Pset ra rd =>
+ match ra with
+ | RA => Next (rsw#ra <- (rsr#rd)) mw
+ | _ => Stuck
+ end
+ | Pnop => Next rsw mw
+end.
+
+(** *** parexec with writes-in-order *)
+Fixpoint parexec_wio_body (body: list basic) (rsr rsw: regset) (mr mw: mem) :=
+ match body with
+ | nil => Next rsw mw
+ | bi::body' =>
+ match bstep bi rsr rsw mr mw with
+ | Next rsw mw => parexec_wio_body body' rsr rsw mr mw
+ | Stuck => Stuck
+ end
+ end.
+
+(* TODO: redundant w.r.t Machblock ?? *)
+Lemma in_dec (lbl: label) (l: list label): { List.In lbl l } + { ~(List.In lbl l) }.
+Proof.
+ apply List.in_dec.
+ apply Pos.eq_dec.
+Qed.
+
+
+
+(* Note: copy-paste from Machblock *)
+Definition is_label (lbl: label) (bb: bblock) : bool :=
+ if in_dec lbl (header bb) then true else false.
+
+Lemma is_label_correct_true lbl bb:
+ List.In lbl (header bb) <-> is_label lbl bb = true.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); cbn; intuition.
+Qed.
+
+Lemma is_label_correct_false lbl bb:
+ ~(List.In lbl (header bb)) <-> is_label lbl bb = false.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); cbn; intuition.
+Qed.
+
+
+
+(** **** convert a label into a position in the code *)
+Fixpoint label_pos (lbl: label) (pos: Z) (lb: bblocks) {struct lb} : option Z :=
+ match lb with
+ | nil => None
+ | b :: lb' => if is_label lbl b then Some pos else label_pos lbl (pos + (size b)) lb'
+ end.
+
+Definition par_goto_label (f: function) (lbl: label) (rsr rsw: regset) (mw: mem) :=
+ match label_pos lbl 0 (fn_blocks f) with
+ | None => Stuck
+ | Some pos =>
+ match rsr#PC with
+ | Vptr b ofs => Next (rsw#PC <- (Vptr b (Ptrofs.repr pos))) mw
+ | _ => Stuck
+ end
+ end.
+
+(** **** Parallel Evaluation of a branch *)
+
+(** Warning: PC is assumed to be already pointing on the next bundle ! *)
+
+Definition par_eval_branch (f: function) (l: label) (rsr rsw: regset) (mw: mem) (res: option bool) :=
+ match res with
+ | Some true => par_goto_label f l rsr rsw mw
+ | Some false => Next (rsw # PC <- (rsr PC)) mw
+ | None => Stuck
+ end.
+
+
+(** **** Parallel execution of a control-flow instruction *)
+
+(** As above: PC is assumed to be incremented on the next block before the control-flow instruction
+*)
+
+Definition parexec_control (f: function) (oc: option control) (rsr rsw: regset) (mw: mem) :=
+ match oc with
+ | None => Next (rsw#PC <- (rsr#PC)) mw
+ | Some ic => (**r Branch Control Unit instructions *)
+ match ic with
+ | Pret =>
+ Next (rsw#PC <- (rsr#RA)) mw
+ | Pcall s =>
+ Next (rsw#RA <- (rsr#PC) #PC <- (Genv.symbol_address ge s Ptrofs.zero)) mw
+ | Picall r =>
+ Next (rsw#RA <- (rsr#PC) #PC <- (rsr#r)) mw
+ | Pjumptable r tbl =>
+ match rsr#r with
+ | Vint n =>
+ match list_nth_z tbl (Int.unsigned n) with
+ | None => Stuck
+ | Some lbl => par_goto_label f lbl rsr (rsw #GPR62 <- Vundef #GPR63 <- Vundef) mw
+ end
+ | _ => Stuck
+ end
+ | Pgoto s =>
+ Next (rsw#PC <- (Genv.symbol_address ge s Ptrofs.zero)) mw
+ | Pigoto r =>
+ Next (rsw#PC <- (rsr#r)) mw
+ | Pj_l l =>
+ par_goto_label f l rsr rsw mw
+ | Pcb bt r l =>
+ match cmp_for_btest bt with
+ | (Some c, Int) => par_eval_branch f l rsr rsw mw (Val.cmp_bool c rsr#r (Vint (Int.repr 0)))
+ | (Some c, Long) => par_eval_branch f l rsr rsw mw (Val.cmpl_bool c rsr#r (Vlong (Int64.repr 0)))
+ | (None, _) => Stuck
+ end
+ | Pcbu bt r l =>
+ match cmpu_for_btest bt with
+ | (Some c, Int) => par_eval_branch f l rsr rsw mw (Val.mxcmpu_bool c rsr#r (Vint (Int.repr 0)))
+ | (Some c, Long) => par_eval_branch f l rsr rsw mw (Val.mxcmplu_bool c rsr#r (Vlong (Int64.repr 0)))
+ | (None, _) => Stuck
+ end
+ (**r Pseudo-instructions *)
+ | Pbuiltin ef args res =>
+ Stuck (**r treated specially below *)
+ end
+ end.
+
+
+Definition incrPC size_b (rs: regset) :=
+ rs#PC <- (Val.offset_ptr rs#PC size_b).
+
+(** parallel execution of the exit instruction of a bundle *)
+Definition estep (f: function) ext size_b (rsr rsw: regset) (mw: mem)
+ := parexec_control f ext (incrPC size_b rsr) rsw mw.
+
+Definition parexec_wio f bdy ext size_b (rs: regset) (m: mem): outcome :=
+ match parexec_wio_body bdy rs rs m m with
+ | Next rsw mw => estep f ext size_b rs rsw mw
+ | Stuck => Stuck
+ end.
+
+(** *** non-deterministic (out-of-order writes) parallel execution of bundles *)
+Definition parexec_bblock (f: function) (bundle: bblock) (rs: regset) (m: mem) (o: outcome): Prop :=
+ exists bdy1 bdy2, Permutation (bdy1++bdy2) (body bundle) /\
+ o=match parexec_wio f bdy1 (exit bundle) (Ptrofs.repr (size bundle)) rs m with
+ | Next rsw mw => parexec_wio_body bdy2 rs rsw m mw
+ | Stuck => Stuck
+ end.
+
+(** *** deterministic parallel (out-of-order writes) execution of bundles *)
+Definition det_parexec (f: function) (bundle: bblock) (rs: regset) (m: mem) rs' m': Prop :=
+ forall o, parexec_bblock f bundle rs m o -> o = Next rs' m'.
+
+
+(** *** Translation of the LTL/Linear/Mach view of machine registers to
+ the assembly view. Note that no LTL register maps to [X31]. This
+ register is reserved as temporary, to be used by the generated RV32G
+ code. *)
+
+
+(** **** Undefine all registers except SP and callee-save registers *)
+
+Definition undef_caller_save_regs (rs: regset) : regset :=
+ fun r =>
+ if preg_eq r SP
+ || In_dec preg_eq r (List.map preg_of (List.filter is_callee_save all_mregs))
+ then rs r
+ else Vundef.
+
+(** **** Extract the values of the arguments of an external call.
+ We exploit the calling conventions from module [Conventions], except that
+ we use assembly registers instead of locations. *)
+
+Inductive extcall_arg (rs: regset) (m: mem): loc -> val -> Prop :=
+ | extcall_arg_reg: forall r,
+ extcall_arg rs m (R r) (rs (preg_of r))
+ | extcall_arg_stack: forall ofs ty bofs v,
+ bofs = Stacklayout.fe_ofs_arg + 4 * ofs ->
+ Mem.loadv (chunk_of_type ty) m
+ (Val.offset_ptr rs#SP (Ptrofs.repr bofs)) = Some v ->
+ extcall_arg rs m (S Outgoing ofs ty) v.
+
+Inductive extcall_arg_pair (rs: regset) (m: mem): rpair loc -> val -> Prop :=
+ | extcall_arg_one: forall l v,
+ extcall_arg rs m l v ->
+ extcall_arg_pair rs m (One l) v
+ | extcall_arg_twolong: forall hi lo vhi vlo,
+ extcall_arg rs m hi vhi ->
+ extcall_arg rs m lo vlo ->
+ extcall_arg_pair rs m (Twolong hi lo) (Val.longofwords vhi vlo).
+
+Definition extcall_arguments
+ (rs: regset) (m: mem) (sg: signature) (args: list val) : Prop :=
+ list_forall2 (extcall_arg_pair rs m) (loc_arguments sg) args.
+
+
+Definition loc_external_result (sg: signature) : rpair preg :=
+ map_rpair preg_of (loc_result sg).
+
+
+(** ** Looking up bblocks in a code sequence by position. *)
+Fixpoint find_bblock (pos: Z) (lb: bblocks) {struct lb} : option bblock :=
+ match lb with
+ | nil => None
+ | b :: il =>
+ if zlt pos 0 then None (*r NOTE: It is impossible to branch inside a block *)
+ else if zeq pos 0 then Some b
+ else find_bblock (pos - (size b)) il
+ end.
+
+
+Inductive state: Type :=
+ | State: regset -> mem -> state.
+
+Definition nextblock (b:bblock) (rs: regset) :=
+ incrPC (Ptrofs.repr (size b)) rs.
+
+Inductive step: state -> trace -> state -> Prop :=
+ | exec_step_internal:
+ forall b ofs f bundle rs m rs' m',
+ rs PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bundle ->
+ det_parexec f bundle rs m rs' m' ->
+ step (State rs m) E0 (State rs' m')
+ | exec_step_builtin:
+ forall b ofs f ef args res rs m vargs t vres rs' m' bi,
+ rs PC = Vptr b ofs ->
+ Genv.find_funct_ptr ge b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) f.(fn_blocks) = Some bi ->
+ exit bi = Some (PExpand (Pbuiltin ef args res)) ->
+ eval_builtin_args ge rs (rs SP) m args vargs ->
+ external_call ef ge vargs m t vres m' ->
+ rs' = nextblock bi
+ (set_res res vres
+ (undef_regs (map preg_of (destroyed_by_builtin ef))
+ (rs#RTMP <- Vundef))) ->
+ step (State rs m) t (State rs' m')
+ | exec_step_external:
+ forall b ef args res rs m t rs' m',
+ rs PC = Vptr b Ptrofs.zero ->
+ Genv.find_funct_ptr ge b = Some (External ef) ->
+ external_call ef ge args m t res m' ->
+ extcall_arguments rs m (ef_sig ef) args ->
+ rs' = (set_pair (loc_external_result (ef_sig ef) ) res (undef_caller_save_regs rs))#PC <- (rs RA) ->
+ step (State rs m) t (State rs' m')
+ .
+
+
+(** parallel in-order writes execution of bundles *)
+Definition parexec_wio_bblock (f: function) (b: bblock) (rs: regset) (m: mem): outcome :=
+ parexec_wio f (body b) (exit b) (Ptrofs.repr (size b)) rs m.
+
+
+Lemma parexec_bblock_write_in_order f b rs m:
+ parexec_bblock f b rs m (parexec_wio_bblock f b rs m).
+Proof.
+ exists (body b). exists nil.
+ constructor 1.
+ - rewrite app_nil_r; auto.
+ - unfold parexec_wio_bblock.
+ destruct (parexec_wio f _ _ _); cbn; auto.
+Qed.
+
+
+Local Hint Resolve parexec_bblock_write_in_order: core.
+
+Lemma det_parexec_write_in_order f b rs m rs' m':
+ det_parexec f b rs m rs' m' -> parexec_wio_bblock f b rs m = Next rs' m'.
+Proof.
+ unfold det_parexec; auto.
+Qed.
+
+End RELSEM.
+
+(** ** Execution of whole programs. *)
+
+Inductive initial_state (p: program): state -> Prop :=
+ | initial_state_intro: forall m0,
+ let ge := Genv.globalenv p in
+ let rs0 :=
+ (Pregmap.init Vundef)
+ # PC <- (Genv.symbol_address ge p.(prog_main) Ptrofs.zero)
+ # SP <- Vnullptr
+ # RA <- Vnullptr in
+ Genv.init_mem p = Some m0 ->
+ initial_state p (State rs0 m0).
+
+Inductive final_state: state -> int -> Prop :=
+ | final_state_intro: forall rs m r,
+ rs PC = Vnullptr ->
+ rs GPR0 = Vint r ->
+ final_state (State rs m) r.
+
+Definition semantics (p: program) :=
+ Semantics step (initial_state p) final_state (Genv.globalenv p).
+
+Remark extcall_arguments_determ:
+ forall rs m sg args1 args2,
+ extcall_arguments rs m sg args1 -> extcall_arguments rs m sg args2 -> args1 = args2.
+Proof.
+ intros until m.
+ assert (A: forall l v1 v2,
+ extcall_arg rs m l v1 -> extcall_arg rs m l v2 -> v1 = v2).
+ { intros. inv H; inv H0; congruence. }
+ assert (B: forall p v1 v2,
+ extcall_arg_pair rs m p v1 -> extcall_arg_pair rs m p v2 -> v1 = v2).
+ { intros. inv H; inv H0.
+ eapply A; eauto.
+ f_equal; eapply A; eauto. }
+ assert (C: forall ll vl1, list_forall2 (extcall_arg_pair rs m) ll vl1 ->
+ forall vl2, list_forall2 (extcall_arg_pair rs m) ll vl2 -> vl1 = vl2).
+ {
+ induction 1; intros vl2 EA; inv EA.
+ auto.
+ f_equal; eauto. }
+ intros. eapply C; eauto.
+Qed.
+
+Lemma semantics_determinate p: determinate (semantics p).
+Proof.
+Ltac Equalities :=
+ match goal with
+ | [ H1: ?a = ?b, H2: ?a = ?c |- _ ] =>
+ rewrite H1 in H2; inv H2; Equalities
+ | _ => idtac
+ end.
+Ltac Det_WIO X :=
+ match goal with
+ | [ H: det_parexec _ _ _ _ _ _ _ |- _ ] =>
+ exploit det_parexec_write_in_order; [ eapply H | idtac]; clear H; intro X
+ | _ => idtac
+ end.
+ intros; constructor; cbn.
+- (* determ *) intros s t1 s1 t2 s2 H H0. inv H; Det_WIO X1;
+ inv H0; Det_WIO X2; Equalities.
+ + split. constructor. auto.
+ + unfold parexec_wio_bblock, parexec_wio in X1. destruct (parexec_wio_body _ _ _ _ _ _); try discriminate.
+ rewrite H8 in X1. discriminate.
+ + unfold parexec_wio_bblock, parexec_wio in X2. destruct (parexec_wio_body _ _ _ _ _ _); try discriminate.
+ rewrite H4 in X2. discriminate.
+ + assert (vargs0 = vargs) by (eapply eval_builtin_args_determ; eauto). subst vargs0.
+ exploit external_call_determ. eexact H6. eexact H13. intros [A B].
+ split. auto. intros. destruct B; auto. subst. auto.
+ + assert (args0 = args) by (eapply extcall_arguments_determ; eauto). subst args0.
+ exploit external_call_determ. eexact H3. eexact H8. intros [A B].
+ split. auto. intros. destruct B; auto. subst. auto.
+- (* trace length *)
+ red; intros. inv H; cbn.
+ lia.
+ eapply external_call_trace_length; eauto.
+ eapply external_call_trace_length; eauto.
+- (* initial states *)
+ intros s1 s2 H H0; inv H; inv H0; f_equal; congruence.
+- (* final no step *)
+ intros s r H; assert (NOTNULL: forall b ofs, Vnullptr <> Vptr b ofs).
+ { intros; unfold Vnullptr; destruct Archi.ptr64; congruence. }
+ inv H. red; intros; red; intros.
+ inv H; rewrite H0 in *; eelim NOTNULL; eauto.
+- (* final states *)
+ intros s r1 r2 H H0; inv H; inv H0. congruence.
+Qed.
diff --git a/kvx/BTL_SEsimplify.v b/kvx/BTL_SEsimplify.v
new file mode 120000
index 00000000..f190e6d5
--- /dev/null
+++ b/kvx/BTL_SEsimplify.v
@@ -0,0 +1 @@
+../aarch64/BTL_SEsimplify.v \ No newline at end of file
diff --git a/kvx/Builtins1.v b/kvx/Builtins1.v
new file mode 100644
index 00000000..441345bf
--- /dev/null
+++ b/kvx/Builtins1.v
@@ -0,0 +1,61 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Platform-specific built-in functions *)
+
+Require Import String Coqlib.
+Require Import AST Integers Floats Values ExtFloats.
+Require Import Builtins0.
+
+Inductive platform_builtin : Type :=
+| BI_fmin
+| BI_fmax
+| BI_fminf
+| BI_fmaxf
+| BI_fma
+| BI_fmaf.
+
+Local Open Scope string_scope.
+
+Definition platform_builtin_table : list (string * platform_builtin) :=
+ ("__builtin_fmin", BI_fmin)
+ :: ("__builtin_fmax", BI_fmax)
+ :: ("__builtin_fminf", BI_fminf)
+ :: ("__builtin_fmaxf", BI_fmaxf)
+ :: ("__builtin_fma", BI_fma)
+ :: ("__builtin_fmaf", BI_fmaf)
+ :: nil.
+
+Definition platform_builtin_sig (b: platform_builtin) : signature :=
+ match b with
+ | BI_fmin | BI_fmax =>
+ mksignature (Tfloat :: Tfloat :: nil) Tfloat cc_default
+ | BI_fminf | BI_fmaxf =>
+ mksignature (Tsingle :: Tsingle :: nil) Tsingle cc_default
+ | BI_fma =>
+ mksignature (Tfloat :: Tfloat :: Tfloat :: nil) Tfloat cc_default
+ | BI_fmaf =>
+ mksignature (Tsingle :: Tsingle :: Tsingle :: nil) Tsingle cc_default
+ end.
+
+Definition platform_builtin_sem (b: platform_builtin) : builtin_sem (sig_res (platform_builtin_sig b)) :=
+ match b with
+ | BI_fmin => mkbuiltin_n2t Tfloat Tfloat Tfloat ExtFloat.min
+ | BI_fmax => mkbuiltin_n2t Tfloat Tfloat Tfloat ExtFloat.max
+ | BI_fminf => mkbuiltin_n2t Tsingle Tsingle Tsingle ExtFloat32.min
+ | BI_fmaxf => mkbuiltin_n2t Tsingle Tsingle Tsingle ExtFloat32.max
+ | BI_fma => mkbuiltin_n3t Tfloat Tfloat Tfloat Tfloat Float.fma
+ | BI_fmaf => mkbuiltin_n3t Tsingle Tsingle Tsingle Tsingle Float32.fma
+ end.
diff --git a/kvx/CBuiltins.ml b/kvx/CBuiltins.ml
new file mode 100644
index 00000000..7398e0f4
--- /dev/null
+++ b/kvx/CBuiltins.ml
@@ -0,0 +1,145 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* Processor-dependent builtin C functions *)
+
+open C
+
+let builtins = {
+ builtin_typedefs = [
+ "__builtin_va_list", TPtr(TVoid [], [])
+ ];
+ (* The builtin list is inspired from the GCC file builtin_kvx.h *)
+ builtin_functions = [ (* Some builtins are commented out because their opcode is not present (yet?) *)
+ (* BCU Instructions *)
+ "__builtin_kvx_await", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_barrier", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_doze", (TVoid [], [], false); (* opcode not supported in assembly, not in documentation *)
+ "__builtin_kvx_wfxl", (TVoid [], [TInt(IUChar, []); TInt(ILongLong, [])], false); (* DONE *)
+ "__builtin_kvx_wfxm", (TVoid [], [TInt(IUChar, []); TInt(ILongLong, [])], false); (* DONE *)
+ "__builtin_kvx_sleep", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_stop", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_syncgroup", (TVoid [], [TInt(IULongLong, [])], false);
+ "__builtin_kvx_tlbread", (TVoid [], [], false);
+ "__builtin_kvx_tlbwrite", (TVoid [], [], false);
+ "__builtin_kvx_tlbprobe", (TVoid [], [], false);
+ "__builtin_kvx_tlbdinval", (TVoid [], [], false);
+ "__builtin_kvx_tlbiinval", (TVoid [], [], false);
+
+ "__builtin_kvx_get", (TInt(IULongLong, []), [TInt(IInt, [])], false); (* DONE *)
+ "__builtin_kvx_set", (TVoid [], [TInt(IInt, []); TInt(IULongLong, [])], false); (* DONE *)
+
+ (* LSU Instructions *)
+ (* acswapd and acswapw done using headers and assembly *)
+(* "__builtin_kvx_afaddd", (TInt(IULongLong, []), [TPtr(TVoid [], []); TInt(ILongLong, [])], false);
+ "__builtin_kvx_afaddw", (TInt(IUInt, []), [TPtr(TVoid [], []); TInt(IInt, [])], false); *) (* see #157 *)
+ "__builtin_kvx_alclrd", (TInt(IULongLong, []), [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_alclrw", (TInt(IUInt, []), [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_dinval", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_dinvall", (TVoid [], [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_dtouchl", (TVoid [], [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_dzerol", (TVoid [], [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_fence", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_iinval", (TVoid [], [], false); (* DONE *)
+ "__builtin_kvx_iinvals", (TVoid [], [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_itouchl", (TVoid [], [TPtr(TVoid [], [])], false); (* DONE [not supported by assembler but in documentation] *)
+ "__builtin_kvx_lbsu", (TInt(IChar, []), [TPtr(TVoid [], [])], false);
+ "__builtin_kvx_lbzu", (TInt(IUChar, []), [TPtr(TVoid [], [])], false);
+ "__builtin_kvx_ldu", (TInt(IULongLong, []), [TPtr(TVoid [], [])], false); (* DONE *)
+ "__builtin_kvx_lhsu", (TInt(IShort, []), [TPtr(TVoid [], [])], false);
+ "__builtin_kvx_lhzu", (TInt(IUShort, []), [TPtr(TVoid [], [])], false);
+ "__builtin_kvx_lwzu", (TInt(IUInt, []), [TPtr(TVoid [], [])], false);
+
+ (* ALU Instructions *)
+ (* "__builtin_kvx_addhp", (TInt(IInt, []), [TInt(IInt, []); TInt(IInt, [])], false); *)
+ (* "__builtin_kvx_adds", (TInt(IInt, []), [TInt(IInt, []); TInt(IInt, [])], false); *)
+ (* "__builtin_kvx_bwlu", (TInt(IUInt, []),
+ [TInt(IUInt, []); TInt(IUInt, []); TInt(IUInt, []); TInt(IUInt, []); TInt(IUShort, [])], false); *)
+ (* "__builtin_kvx_bwluhp", (TInt(IUInt, []), [TInt(IUInt, []); TInt(IUInt, []); TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_bwluwp", (TInt(IULongLong, []),
+ [TInt(IULongLong, []); TInt(IULongLong, []); TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_cbs", (TInt(IInt, []), [TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_cbsdl", (TInt(ILongLong, []), [TInt(IULongLong, [])], false); *)
+ (* "__builtin_kvx_clz", (TInt(IInt, []), [TInt(IUInt, [])], false); *)
+ "__builtin_kvx_clzw", (TInt(IInt, []), [TInt(IUInt, [])], false);
+ "__builtin_clzll", (TInt(ILongLong, []), [TInt(IULongLong, [])], false);
+ "__builtin_kvx_ctzw", (TInt(IInt, []), [TInt(IUInt, [])], false);
+ "__builtin_ctzll", (TInt(ILongLong, []), [TInt(IULongLong, [])], false);
+ (* "__builtin_kvx_clzdl", (TInt(ILongLong, []), [TInt(IULongLong, [])], false); *)
+ (* "__builtin_kvx_cmove", (TInt(IInt, []), [TInt(IInt, []); TInt(IInt, []); TInt(IInt, [])], false); *)
+ (* "__builtin_kvx_ctz", (TInt(IInt, []), [TInt(IUInt, [])], false); *)
+ "__builtin_kvx_ctzw", (TInt(IInt, []), [TInt(IUInt, [])], false);
+ "__builtin_kvx_ctzd", (TInt(ILongLong, []), [TInt(IULongLong, [])], false);
+ (* "__builtin_kvx_ctzdl", (TInt(ILongLong, []), [TInt(IULongLong, [])], false); *)
+ (* "__builtin_kvx_extfz", (TInt(IUInt, []), [TInt(IUInt, []); TInt(IUInt, []); TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_landhp", (TInt(IInt, []), [TInt(IInt, []); TInt(IInt, []); TInt(IInt, [])], false); *)
+ (* "__builtin_kvx_sat", (TInt(IInt, []), [TInt(IInt, []); TInt(IUChar, [])], false); *)
+ "__builtin_kvx_satd", (TInt(ILongLong, []), [TInt(ILongLong, []); TInt(IUChar, [])], false);
+ (* "__builtin_kvx_sbfhp", (TInt(IInt, []), [TInt(IInt, []); TInt(IInt, [])], false); *)
+ "__builtin_kvx_sbmm8", (TInt(IULongLong, []), [TInt(IULongLong, []); TInt(IULongLong, [])], false);
+ "__builtin_kvx_sbmmt8", (TInt(IULongLong, []), [TInt(IULongLong, []); TInt(IULongLong, [])], false);
+ (* "__builtin_kvx_sllhps", (TInt(IUInt, []), [TInt(IUInt, []); TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_srahps", (TInt(IUInt, []), [TInt(IUInt, []); TInt(IUInt, [])], false); *)
+ (* "__builtin_kvx_stsu", (TInt(IUInt, []), [TInt(IUInt, []); TInt(IUInt, [])], false); *)
+ "__builtin_kvx_stsud", (TInt(IULongLong, []), [TInt(IULongLong, []); TInt(IULongLong, [])], false);
+
+
+ (* Synchronization *)
+(* "__builtin_fence",
+ (TVoid [], [], false); *)
+(* (* Float arithmetic *)
+ "__builtin_fmadd",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fmsub",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fnmadd",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fnmsub",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, []); TFloat(FDouble, [])], false); *)
+ "__builtin_fabsf",
+ (TFloat(FFloat, []),
+ [TFloat(FFloat, [])], false);
+ "__builtin_fmax",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fmin",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fmaxf",
+ (TFloat(FFloat, []),
+ [TFloat(FFloat, []); TFloat(FFloat, [])], false);
+ "__builtin_fminf",
+ (TFloat(FFloat, []),
+ [TFloat(FFloat, []); TFloat(FFloat, [])], false);
+ "__builtin_fma",
+ (TFloat(FDouble, []),
+ [TFloat(FDouble, []); TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_fmaf",
+ (TFloat(FFloat, []),
+ [TFloat(FFloat, []); TFloat(FFloat, []); TFloat(FFloat, [])], false);
+]
+}
+
+let va_list_type = TPtr(TVoid [], []) (* to check! *)
+let size_va_list = if Archi.ptr64 then 8 else 4
+let va_list_scalar = true
+
+(* Expand memory references inside extended asm statements. Used in C2C. *)
+
+let asm_mem_argument arg = Printf.sprintf "0(%s)" arg
diff --git a/kvx/CSE2deps.v b/kvx/CSE2deps.v
new file mode 100644
index 00000000..c0deacf0
--- /dev/null
+++ b/kvx/CSE2deps.v
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ else true
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/kvx/CSE2depsproof.v b/kvx/CSE2depsproof.v
new file mode 100644
index 00000000..a5f7b317
--- /dev/null
+++ b/kvx/CSE2depsproof.v
@@ -0,0 +1,146 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_size :
+ Ptrofs.wordsize = (if Archi.ptr64 then 64 else 32)%nat.
+Proof.
+ unfold Ptrofs.wordsize.
+ unfold Wordsize_Ptrofs.wordsize.
+ trivial.
+Qed.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = if Archi.ptr64 then 18446744073709551616 else 4294967296.
+Proof.
+ unfold Ptrofs.modulus.
+ rewrite ptrofs_size.
+ destruct Archi.ptr64; reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ cbn in *.
+ inv ADDRR.
+ inv ADDRW.
+ destruct base; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: try rewrite ptrofs_modulus in *.
+ all: destruct Archi.ptr64.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End MEMORY_WRITE.
+End SOUNDNESS.
+
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+ - (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ cbn in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+ - (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/kvx/Chunks.v b/kvx/Chunks.v
new file mode 100644
index 00000000..86d4f0ac
--- /dev/null
+++ b/kvx/Chunks.v
@@ -0,0 +1,36 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import AST.
+Require Import Values.
+Require Import Integers.
+Require Import Coq.ZArith.BinIntDef.
+Require Import BinNums.
+
+Local Open Scope Z_scope.
+
+Definition zscale_of_chunk (chunk: memory_chunk) : Z :=
+ match chunk with
+ | Mint8signed => 0
+ | Mint8unsigned => 0
+ | Mint16signed => 1
+ | Mint16unsigned => 1
+ | Mint32 => 2
+ | Mint64 => 3
+ | Mfloat32 => 2
+ | Mfloat64 => 3
+ | Many32 => 2
+ | Many64 => 3
+ end.
+Definition scale_of_chunk chunk := Vint (Int.repr (zscale_of_chunk chunk)).
diff --git a/kvx/CombineOp.v b/kvx/CombineOp.v
new file mode 100644
index 00000000..ff1db3cd
--- /dev/null
+++ b/kvx/CombineOp.v
@@ -0,0 +1,141 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Recognition of combined operations, addressing modes and conditions
+ during the [CSE] phase. *)
+
+Require Import Coqlib.
+Require Import AST.
+Require Import Integers.
+Require Import Op.
+Require Import CSEdomain.
+
+Section COMBINE.
+
+Variable get: valnum -> option rhs.
+
+Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
+ match get x with
+ | Some(Op (Ocmp c) ys) => Some (c, ys)
+ | _ => None
+ end.
+
+Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
+ match get x with
+ | Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
+ | _ => None
+ end.
+
+Function combine_compimm_eq_1 (x: valnum) : option(condition * list valnum) :=
+ match get x with
+ | Some(Op (Ocmp c) ys) => Some (c, ys)
+ | _ => None
+ end.
+
+Function combine_compimm_ne_1 (x: valnum) : option(condition * list valnum) :=
+ match get x with
+ | Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
+ | _ => None
+ end.
+
+Function combine_cond (cond: condition) (args: list valnum) : option(condition * list valnum) :=
+ match cond, args with
+ | Ccompimm Cne n, x::nil =>
+ if Int.eq_dec n Int.zero then combine_compimm_ne_0 x
+ else if Int.eq_dec n Int.one then combine_compimm_ne_1 x
+ else None
+ | Ccompimm Ceq n, x::nil =>
+ if Int.eq_dec n Int.zero then combine_compimm_eq_0 x
+ else if Int.eq_dec n Int.one then combine_compimm_eq_1 x
+ else None
+ | Ccompuimm Cne n, x::nil =>
+ if Int.eq_dec n Int.zero then combine_compimm_ne_0 x
+ else if Int.eq_dec n Int.one then combine_compimm_ne_1 x
+ else None
+ | Ccompuimm Ceq n, x::nil =>
+ if Int.eq_dec n Int.zero then combine_compimm_eq_0 x
+ else if Int.eq_dec n Int.one then combine_compimm_eq_1 x
+ else None
+ | _, _ => None
+ end.
+
+Function combine_addr (addr: addressing) (args: list valnum) : option(addressing * list valnum) :=
+ match addr, args with
+ | Aindexed n, x::nil =>
+ match get x with
+ | Some(Op (Oaddimm m) ys) =>
+ if Archi.ptr64 then None else Some(Aindexed (Ptrofs.add (Ptrofs.of_int m) n), ys)
+ | Some(Op (Oaddlimm m) ys) =>
+ if Archi.ptr64 then Some(Aindexed (Ptrofs.add (Ptrofs.of_int64 m) n), ys) else None
+ | _ => None
+ end
+ | _, _ => None
+ end.
+
+Function combine_op (op: operation) (args: list valnum) : option(operation * list valnum) :=
+ match op, args with
+ | Oaddimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oaddimm m) ys) => Some(Oaddimm (Int.add m n), ys)
+ | _ => None
+ end
+ | Oandimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oandimm m) ys) =>
+ Some(let p := Int.and m n in
+ if Int.eq p m then (Omove, x :: nil) else (Oandimm p, ys))
+ | _ => None
+ end
+ | Oorimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oorimm m) ys) => Some(Oorimm (Int.or m n), ys)
+ | _ => None
+ end
+ | Oxorimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oxorimm m) ys) => Some(Oxorimm (Int.xor m n), ys)
+ | _ => None
+ end
+ | Oaddlimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oaddlimm m) ys) => Some(Oaddlimm (Int64.add m n), ys)
+ | _ => None
+ end
+ | Oandlimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oandlimm m) ys) =>
+ Some(let p := Int64.and m n in
+ if Int64.eq p m then (Omove, x :: nil) else (Oandlimm p, ys))
+ | _ => None
+ end
+ | Oorlimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oorlimm m) ys) => Some(Oorlimm (Int64.or m n), ys)
+ | _ => None
+ end
+ | Oxorlimm n, x :: nil =>
+ match get x with
+ | Some(Op (Oxorlimm m) ys) => Some(Oxorlimm (Int64.xor m n), ys)
+ | _ => None
+ end
+ | Ocmp cond, _ =>
+ match combine_cond cond args with
+ | Some(cond', args') => Some(Ocmp cond', args')
+ | None => None
+ end
+ | _, _ => None
+ end.
+
+End COMBINE.
diff --git a/kvx/CombineOpproof.v b/kvx/CombineOpproof.v
new file mode 100644
index 00000000..5dffc565
--- /dev/null
+++ b/kvx/CombineOpproof.v
@@ -0,0 +1,176 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Recognition of combined operations, addressing modes and conditions
+ during the [CSE] phase. *)
+
+Require Import FunInd.
+Require Import Coqlib.
+Require Import AST.
+Require Import Integers.
+Require Import Values.
+Require Import Memory.
+Require Import Op.
+Require Import Registers.
+Require Import RTL.
+Require Import CSEdomain.
+Require Import CombineOp.
+
+Section COMBINE.
+
+Variable ge: genv.
+Variable sp: val.
+Variable m: mem.
+Variable get: valnum -> option rhs.
+Variable valu: valnum -> val.
+Hypothesis get_sound: forall v rhs, get v = Some rhs -> rhs_eval_to valu ge sp m rhs (valu v).
+
+Lemma get_op_sound:
+ forall v op vl, get v = Some (Op op vl) -> eval_operation ge sp op (map valu vl) m = Some (valu v).
+Proof.
+ intros. exploit get_sound; eauto. intros REV; inv REV; auto.
+Qed.
+
+Ltac UseGetSound :=
+ match goal with
+ | [ H: get _ = Some _ |- _ ] =>
+ let x := fresh "EQ" in (generalize (get_op_sound _ _ _ H); intros x; cbn in x; FuncInv)
+ end.
+
+Lemma combine_compimm_ne_0_sound:
+ forall x cond args,
+ combine_compimm_ne_0 get x = Some(cond, args) ->
+ eval_condition cond (map valu args) m = Val.cmp_bool Cne (valu x) (Vint Int.zero) /\
+ eval_condition cond (map valu args) m = Val.cmpu_bool (Mem.valid_pointer m) Cne (valu x) (Vint Int.zero).
+Proof.
+ intros until args. functional induction (combine_compimm_ne_0 get x); intros EQ; inv EQ.
+ (* of cmp *)
+ UseGetSound. rewrite <- H.
+ destruct (eval_condition cond (map valu args) m); cbn; auto. destruct b; auto.
+Qed.
+
+Lemma combine_compimm_eq_0_sound:
+ forall x cond args,
+ combine_compimm_eq_0 get x = Some(cond, args) ->
+ eval_condition cond (map valu args) m = Val.cmp_bool Ceq (valu x) (Vint Int.zero) /\
+ eval_condition cond (map valu args) m = Val.cmpu_bool (Mem.valid_pointer m) Ceq (valu x) (Vint Int.zero).
+Proof.
+ intros until args. functional induction (combine_compimm_eq_0 get x); intros EQ; inv EQ.
+ (* of cmp *)
+ UseGetSound. rewrite <- H.
+ rewrite eval_negate_condition.
+ destruct (eval_condition c (map valu args) m); cbn; auto. destruct b; auto.
+Qed.
+
+Lemma combine_compimm_eq_1_sound:
+ forall x cond args,
+ combine_compimm_eq_1 get x = Some(cond, args) ->
+ eval_condition cond (map valu args) m = Val.cmp_bool Ceq (valu x) (Vint Int.one) /\
+ eval_condition cond (map valu args) m = Val.cmpu_bool (Mem.valid_pointer m) Ceq (valu x) (Vint Int.one).
+Proof.
+ intros until args. functional induction (combine_compimm_eq_1 get x); intros EQ; inv EQ.
+ (* of cmp *)
+ UseGetSound. rewrite <- H.
+ destruct (eval_condition cond (map valu args) m); cbn; auto. destruct b; auto.
+Qed.
+
+Lemma combine_compimm_ne_1_sound:
+ forall x cond args,
+ combine_compimm_ne_1 get x = Some(cond, args) ->
+ eval_condition cond (map valu args) m = Val.cmp_bool Cne (valu x) (Vint Int.one) /\
+ eval_condition cond (map valu args) m = Val.cmpu_bool (Mem.valid_pointer m) Cne (valu x) (Vint Int.one).
+Proof.
+ intros until args. functional induction (combine_compimm_ne_1 get x); intros EQ; inv EQ.
+ (* of cmp *)
+ UseGetSound. rewrite <- H.
+ rewrite eval_negate_condition.
+ destruct (eval_condition c (map valu args) m); cbn; auto. destruct b; auto.
+Qed.
+
+Theorem combine_cond_sound:
+ forall cond args cond' args',
+ combine_cond get cond args = Some(cond', args') ->
+ eval_condition cond' (map valu args') m = eval_condition cond (map valu args) m.
+Proof.
+ intros. functional inversion H; subst.
+ (* compimm ne zero *)
+ - cbn; eapply combine_compimm_ne_0_sound; eauto.
+ (* compimm ne one *)
+ - cbn; eapply combine_compimm_ne_1_sound; eauto.
+ (* compimm eq zero *)
+ - cbn; eapply combine_compimm_eq_0_sound; eauto.
+ (* compimm eq one *)
+ - cbn; eapply combine_compimm_eq_1_sound; eauto.
+ (* compuimm ne zero *)
+ - cbn; eapply combine_compimm_ne_0_sound; eauto.
+ (* compuimm ne one *)
+ - cbn; eapply combine_compimm_ne_1_sound; eauto.
+ (* compuimm eq zero *)
+ - cbn; eapply combine_compimm_eq_0_sound; eauto.
+ (* compuimm eq one *)
+ - cbn; eapply combine_compimm_eq_1_sound; eauto.
+Qed.
+
+Theorem combine_addr_sound:
+ forall addr args addr' args',
+ combine_addr get addr args = Some(addr', args') ->
+ eval_addressing ge sp addr' (map valu args') = eval_addressing ge sp addr (map valu args).
+Proof.
+ intros. functional inversion H; subst.
+- (* indexed - addimm *)
+ UseGetSound. cbn. rewrite <- H0. destruct v; auto. cbn; rewrite H7; cbn.
+ rewrite Ptrofs.add_assoc. auto.
+- (* indexed - addimml *)
+ UseGetSound. cbn. rewrite <- H0. destruct v; auto. cbn; rewrite H7; cbn.
+ rewrite Ptrofs.add_assoc. auto.
+Qed.
+
+Theorem combine_op_sound:
+ forall op args op' args',
+ combine_op get op args = Some(op', args') ->
+ eval_operation ge sp op' (map valu args') m = eval_operation ge sp op (map valu args) m.
+Proof.
+ intros. functional inversion H; subst.
+ (* addimm - addimm *)
+ - UseGetSound. FuncInv. cbn.
+ rewrite <- H0. rewrite Val.add_assoc. auto.
+ (* andimm - andimm *)
+ - UseGetSound; cbn.
+ generalize (Int.eq_spec p m0); rewrite H7; intros.
+ rewrite <- H0. rewrite Val.and_assoc. cbn. fold p. rewrite H1. auto.
+ - UseGetSound; cbn.
+ rewrite <- H0. rewrite Val.and_assoc. auto.
+ (* orimm - orimm *)
+ - UseGetSound. cbn. rewrite <- H0. rewrite Val.or_assoc. auto.
+ (* xorimm - xorimm *)
+ - UseGetSound. cbn. rewrite <- H0. rewrite Val.xor_assoc. auto.
+ (* addlimm - addlimm *)
+ - UseGetSound. FuncInv. cbn.
+ rewrite <- H0. rewrite Val.addl_assoc. auto.
+ (* andlimm - andlimm *)
+ - UseGetSound; cbn.
+ generalize (Int64.eq_spec p m0); rewrite H7; intros.
+ rewrite <- H0. rewrite Val.andl_assoc. cbn. fold p. rewrite H1. auto.
+ - UseGetSound; cbn.
+ rewrite <- H0. rewrite Val.andl_assoc. auto.
+ (* orlimm - orlimm *)
+ - UseGetSound. cbn. rewrite <- H0. rewrite Val.orl_assoc. auto.
+ (* xorlimm - xorlimm *)
+ - UseGetSound. cbn. rewrite <- H0. rewrite Val.xorl_assoc. auto.
+ (* cmp *)
+ - cbn. decEq; decEq. eapply combine_cond_sound; eauto.
+Qed.
+
+End COMBINE.
diff --git a/kvx/ConstpropOp.vp b/kvx/ConstpropOp.vp
new file mode 100644
index 00000000..2a428020
--- /dev/null
+++ b/kvx/ConstpropOp.vp
@@ -0,0 +1,312 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Strength reduction for operators and conditions.
+ This is the machine-dependent part of [Constprop]. *)
+
+Require Archi.
+Require Import Coqlib Compopts.
+Require Import AST Integers Floats.
+Require Import Op Registers.
+Require Import ValueDomain.
+
+(** * Converting known values to constants *)
+
+Definition const_for_result (a: aval) : option operation :=
+ match a with
+ | I n => Some(Ointconst n)
+ | L n => if Archi.ptr64 then Some(Olongconst n) else None
+ | F n => if Compopts.generate_float_constants tt then Some(Ofloatconst n) else None
+ | FS n => if Compopts.generate_float_constants tt then Some(Osingleconst n) else None
+ | Ptr(Gl id ofs) => Some(Oaddrsymbol id ofs)
+ | Ptr(Stk ofs) => Some(Oaddrstack ofs)
+ | _ => None
+ end.
+
+(** * Operator strength reduction *)
+
+(** We now define auxiliary functions for strength reduction of
+ operators and addressing modes: replacing an operator with a cheaper
+ one if some of its arguments are statically known. These are again
+ large pattern-matchings expressed in indirect style. *)
+
+Nondetfunction cond_strength_reduction
+ (cond: condition) (args: list reg) (vl: list aval) :=
+ match cond, args, vl with
+ | Ccomp c, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
+ (Ccompimm (swap_comparison c) n1, r2 :: nil)
+ | Ccomp c, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
+ (Ccompimm c n2, r1 :: nil)
+ | Ccompu c, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
+ (Ccompuimm (swap_comparison c) n1, r2 :: nil)
+ | Ccompu c, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
+ (Ccompuimm c n2, r1 :: nil)
+ | Ccompl c, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
+ (Ccomplimm (swap_comparison c) n1, r2 :: nil)
+ | Ccompl c, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
+ (Ccomplimm c n2, r1 :: nil)
+ | Ccomplu c, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
+ (Ccompluimm (swap_comparison c) n1, r2 :: nil)
+ | Ccomplu c, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
+ (Ccompluimm c n2, r1 :: nil)
+ | _, _, _ =>
+ (cond, args)
+ end.
+
+Definition make_cmp_base (c: condition) (args: list reg) (vl: list aval) :=
+ let (c', args') := cond_strength_reduction c args vl in (Ocmp c', args').
+
+Definition make_cmp_imm_eq (c: condition) (args: list reg) (vl: list aval)
+ (n: int) (r1: reg) (v1: aval) :=
+ if Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1) then (Omove, r1 :: nil)
+ else if Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1) then (Oxorimm Int.one, r1 :: nil)
+ else make_cmp_base c args vl.
+
+Definition make_cmp_imm_ne (c: condition) (args: list reg) (vl: list aval)
+ (n: int) (r1: reg) (v1: aval) :=
+ if Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1) then (Omove, r1 :: nil)
+ else if Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1) then (Oxorimm Int.one, r1 :: nil)
+ else make_cmp_base c args vl.
+
+Nondetfunction make_cmp (c: condition) (args: list reg) (vl: list aval) :=
+ match c, args, vl with
+ | Ccompimm Ceq n, r1 :: nil, v1 :: nil =>
+ make_cmp_imm_eq c args vl n r1 v1
+ | Ccompimm Cne n, r1 :: nil, v1 :: nil =>
+ make_cmp_imm_ne c args vl n r1 v1
+ | Ccompuimm Ceq n, r1 :: nil, v1 :: nil =>
+ make_cmp_imm_eq c args vl n r1 v1
+ | Ccompuimm Cne n, r1 :: nil, v1 :: nil =>
+ make_cmp_imm_ne c args vl n r1 v1
+ | _, _, _ =>
+ make_cmp_base c args vl
+ end.
+
+Definition make_addimm (n: int) (r: reg) :=
+ if Int.eq n Int.zero
+ then (Omove, r :: nil)
+ else (Oaddimm n, r :: nil).
+
+Definition make_shlimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int.iwordsize then (Oshlimm n, r1 :: nil)
+ else (Oshl, r1 :: r2 :: nil).
+
+Definition make_shrimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int.iwordsize then (Oshrimm n, r1 :: nil)
+ else (Oshr, r1 :: r2 :: nil).
+
+Definition make_shruimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int.iwordsize then (Oshruimm n, r1 :: nil)
+ else (Oshru, r1 :: r2 :: nil).
+
+Definition make_mulimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then
+ (Ointconst Int.zero, nil)
+ else if Int.eq n Int.one then
+ (Omove, r1 :: nil)
+ else
+ match Int.is_power2 n with
+ | Some l => (Oshlimm l, r1 :: nil)
+ | None => (Omul, r1 :: r2 :: nil)
+ end.
+
+Definition make_andimm (n: int) (r: reg) (a: aval) :=
+ if Int.eq n Int.zero then (Ointconst Int.zero, nil)
+ else if Int.eq n Int.mone then (Omove, r :: nil)
+ else if match a with Uns _ m => Int.eq (Int.zero_ext m (Int.not n)) Int.zero
+ | _ => false end
+ then (Omove, r :: nil)
+ else (Oandimm n, r :: nil).
+
+Definition make_orimm (n: int) (r: reg) :=
+ if Int.eq n Int.zero then (Omove, r :: nil)
+ else if Int.eq n Int.mone then (Ointconst Int.mone, nil)
+ else (Oorimm n, r :: nil).
+
+Definition make_xorimm (n: int) (r: reg) :=
+ if Int.eq n Int.zero then (Omove, r :: nil)
+ else (Oxorimm n, r :: nil).
+
+Definition make_divimm n (r1 r2: reg) :=
+ if Int.eq n Int.one then
+ (Omove, r1 :: nil)
+ else
+ match Int.is_power2 n with
+ | Some l => if Int.ltu l (Int.repr 31)
+ then (Oshrximm l, r1 :: nil)
+ else (Odiv, r1 :: r2 :: nil)
+ | None => (Odiv, r1 :: r2 :: nil)
+ end.
+
+Definition make_divuimm n (r1 r2: reg) :=
+ if Int.eq n Int.one then
+ (Omove, r1 :: nil)
+ else
+ match Int.is_power2 n with
+ | Some l => (Oshruimm l, r1 :: nil)
+ | None => (Odivu, r1 :: r2 :: nil)
+ end.
+
+Definition make_moduimm n (r1 r2: reg) :=
+ match Int.is_power2 n with
+ | Some l => (Oandimm (Int.sub n Int.one), r1 :: nil)
+ | None => (Omodu, r1 :: r2 :: nil)
+ end.
+
+Definition make_addlimm (n: int64) (r: reg) :=
+ if Int64.eq n Int64.zero
+ then (Omove, r :: nil)
+ else (Oaddlimm n, r :: nil).
+
+Definition make_shllimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int64.iwordsize' then (Oshllimm n, r1 :: nil)
+ else (Oshll, r1 :: r2 :: nil).
+
+Definition make_shrlimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int64.iwordsize' then (Oshrlimm n, r1 :: nil)
+ else (Oshrl, r1 :: r2 :: nil).
+
+Definition make_shrluimm (n: int) (r1 r2: reg) :=
+ if Int.eq n Int.zero then (Omove, r1 :: nil)
+ else if Int.ltu n Int64.iwordsize' then (Oshrluimm n, r1 :: nil)
+ else (Oshrlu, r1 :: r2 :: nil).
+
+Definition make_mullimm (n: int64) (r1 r2: reg) :=
+ if Int64.eq n Int64.zero then
+ (Olongconst Int64.zero, nil)
+ else if Int64.eq n Int64.one then
+ (Omove, r1 :: nil)
+ else
+ match Int64.is_power2' n with
+ | Some l => (Oshllimm l, r1 :: nil)
+ | None => (Omull, r1 :: r2 :: nil)
+ end.
+
+Definition make_andlimm (n: int64) (r: reg) (a: aval) :=
+ if Int64.eq n Int64.zero then (Olongconst Int64.zero, nil)
+ else if Int64.eq n Int64.mone then (Omove, r :: nil)
+ else (Oandlimm n, r :: nil).
+
+Definition make_orlimm (n: int64) (r: reg) :=
+ if Int64.eq n Int64.zero then (Omove, r :: nil)
+ else if Int64.eq n Int64.mone then (Olongconst Int64.mone, nil)
+ else (Oorlimm n, r :: nil).
+
+Definition make_xorlimm (n: int64) (r: reg) :=
+ if Int64.eq n Int64.zero then (Omove, r :: nil)
+ else (Oxorlimm n, r :: nil).
+
+Definition make_divlimm n (r1 r2: reg) :=
+ match Int64.is_power2' n with
+ | Some l => if Int.ltu l (Int.repr 63)
+ then (Oshrxlimm l, r1 :: nil)
+ else (Odivl, r1 :: r2 :: nil)
+ | None => (Odivl, r1 :: r2 :: nil)
+ end.
+
+Definition make_divluimm n (r1 r2: reg) :=
+ match Int64.is_power2' n with
+ | Some l => (Oshrluimm l, r1 :: nil)
+ | None => (Odivlu, r1 :: r2 :: nil)
+ end.
+
+Definition make_modluimm n (r1 r2: reg) :=
+ match Int64.is_power2 n with
+ | Some l => (Oandlimm (Int64.sub n Int64.one), r1 :: nil)
+ | None => (Omodlu, r1 :: r2 :: nil)
+ end.
+
+Definition make_mulfimm (n: float) (r r1 r2: reg) :=
+ if Float.eq_dec n (Float.of_int (Int.repr 2))
+ then (Oaddf, r :: r :: nil)
+ else (Omulf, r1 :: r2 :: nil).
+
+Definition make_mulfsimm (n: float32) (r r1 r2: reg) :=
+ if Float32.eq_dec n (Float32.of_int (Int.repr 2))
+ then (Oaddfs, r :: r :: nil)
+ else (Omulfs, r1 :: r2 :: nil).
+
+Definition make_cast8signed (r: reg) (a: aval) :=
+ if vincl a (Sgn Ptop 8) then (Omove, r :: nil) else (Ocast8signed, r :: nil).
+Definition make_cast16signed (r: reg) (a: aval) :=
+ if vincl a (Sgn Ptop 16) then (Omove, r :: nil) else (Ocast16signed, r :: nil).
+
+Nondetfunction op_strength_reduction
+ (op: operation) (args: list reg) (vl: list aval) :=
+ match op, args, vl with
+ | Ocast8signed, r1 :: nil, v1 :: nil => make_cast8signed r1 v1
+ | Ocast16signed, r1 :: nil, v1 :: nil => make_cast16signed r1 v1
+ | Oadd, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_addimm n1 r2
+ | Oadd, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_addimm n2 r1
+ | Osub, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_addimm (Int.neg n2) r1
+ | Omul, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_mulimm n1 r2 r1
+ | Omul, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_mulimm n2 r1 r2
+ | Odiv, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_divimm n2 r1 r2
+ | Odivu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_divuimm n2 r1 r2
+ | Omodu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_moduimm n2 r1 r2
+ | Oand, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_andimm n1 r2 v2
+ | Oand, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_andimm n2 r1 v1
+ | Oandimm n, r1 :: nil, v1 :: nil => make_andimm n r1 v1
+ | Oor, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_orimm n1 r2
+ | Oor, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_orimm n2 r1
+ | Oxor, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_xorimm n1 r2
+ | Oxor, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_xorimm n2 r1
+ | Oshl, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shlimm n2 r1 r2
+ | Oshr, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrimm n2 r1 r2
+ | Oshru, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shruimm n2 r1 r2
+ | Oaddl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_addlimm n1 r2
+ | Oaddl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_addlimm n2 r1
+ | Osubl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_addlimm (Int64.neg n2) r1
+ | Omull, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_mullimm n1 r2 r1
+ | Omull, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_mullimm n2 r1 r2
+ | Odivl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_divlimm n2 r1 r2
+ | Odivlu, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_divluimm n2 r1 r2
+ | Omodlu, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_modluimm n2 r1 r2
+ | Oandl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_andlimm n1 r2 v2
+ | Oandl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_andlimm n2 r1 v1
+ | Oandlimm n, r1 :: nil, v1 :: nil => make_andlimm n r1 v1
+ | Oorl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_orlimm n1 r2
+ | Oorl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_orlimm n2 r1
+ | Oxorl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_xorlimm n1 r2
+ | Oxorl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_xorlimm n2 r1
+ | Oshll, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shllimm n2 r1 r2
+ | Oshrl, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrlimm n2 r1 r2
+ | Oshrlu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrluimm n2 r1 r2
+ | Ocmp c, args, vl => make_cmp c args vl
+ | Omulf, r1 :: r2 :: nil, v1 :: F n2 :: nil => make_mulfimm n2 r1 r1 r2
+ | Omulf, r1 :: r2 :: nil, F n1 :: v2 :: nil => make_mulfimm n1 r2 r1 r2
+ | Omulfs, r1 :: r2 :: nil, v1 :: FS n2 :: nil => make_mulfsimm n2 r1 r1 r2
+ | Omulfs, r1 :: r2 :: nil, FS n1 :: v2 :: nil => make_mulfsimm n1 r2 r1 r2
+ | _, _, _ => (op, args)
+ end.
+
+Nondetfunction addr_strength_reduction
+ (addr: addressing) (args: list reg) (vl: list aval) :=
+ match addr, args, vl with
+ | Aindexed n, r1 :: nil, Ptr(Gl symb n1) :: nil =>
+ if (orb (Archi.pic_code tt) (negb (Compopts.optim_globaladdrtmp tt)))
+ then (addr, args)
+ else (Aglobal symb (Ptrofs.add n1 n), nil)
+ | Aindexed n, r1 :: nil, Ptr(Stk n1) :: nil =>
+ (Ainstack (Ptrofs.add n1 n), nil)
+ | _, _, _ =>
+ (addr, args)
+ end.
+
diff --git a/kvx/ConstpropOpproof.v b/kvx/ConstpropOpproof.v
new file mode 100644
index 00000000..f67b8a4e
--- /dev/null
+++ b/kvx/ConstpropOpproof.v
@@ -0,0 +1,749 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Correctness proof for operator strength reduction. *)
+
+Require Import Coqlib Compopts.
+Require Import Integers Floats Values Memory Globalenvs Events.
+Require Import Op Registers RTL ValueDomain.
+Require Import ConstpropOp.
+Require Import Lia.
+
+Section STRENGTH_REDUCTION.
+
+Variable bc: block_classification.
+Variable ge: genv.
+Hypothesis GENV: genv_match bc ge.
+Variable sp: block.
+Hypothesis STACK: bc sp = BCstack.
+Variable ae: AE.t.
+Variable e: regset.
+Variable m: mem.
+Hypothesis MATCH: ematch bc e ae.
+
+Lemma match_G:
+ forall r id ofs,
+ AE.get r ae = Ptr(Gl id ofs) -> Val.lessdef e#r (Genv.symbol_address ge id ofs).
+Proof.
+ intros. apply vmatch_ptr_gl with bc; auto. rewrite <- H. apply MATCH.
+Qed.
+
+Lemma match_S:
+ forall r ofs,
+ AE.get r ae = Ptr(Stk ofs) -> Val.lessdef e#r (Vptr sp ofs).
+Proof.
+ intros. apply vmatch_ptr_stk with bc; auto. rewrite <- H. apply MATCH.
+Qed.
+
+Ltac InvApproxRegs :=
+ match goal with
+ | [ H: _ :: _ = _ :: _ |- _ ] =>
+ injection H; clear H; intros; InvApproxRegs
+ | [ H: ?v = AE.get ?r ae |- _ ] =>
+ generalize (MATCH r); rewrite <- H; clear H; intro; InvApproxRegs
+ | _ => idtac
+ end.
+
+Ltac SimplVM :=
+ match goal with
+ | [ H: vmatch _ ?v (I ?n) |- _ ] =>
+ let E := fresh in
+ assert (E: v = Vint n) by (inversion H; auto);
+ rewrite E in *; clear H; SimplVM
+ | [ H: vmatch _ ?v (L ?n) |- _ ] =>
+ let E := fresh in
+ assert (E: v = Vlong n) by (inversion H; auto);
+ rewrite E in *; clear H; SimplVM
+ | [ H: vmatch _ ?v (F ?n) |- _ ] =>
+ let E := fresh in
+ assert (E: v = Vfloat n) by (inversion H; auto);
+ rewrite E in *; clear H; SimplVM
+ | [ H: vmatch _ ?v (FS ?n) |- _ ] =>
+ let E := fresh in
+ assert (E: v = Vsingle n) by (inversion H; auto);
+ rewrite E in *; clear H; SimplVM
+ | [ H: vmatch _ ?v (Ptr(Gl ?id ?ofs)) |- _ ] =>
+ let E := fresh in
+ assert (E: Val.lessdef v (Genv.symbol_address ge id ofs)) by (eapply vmatch_ptr_gl; eauto);
+ clear H; SimplVM
+ | [ H: vmatch _ ?v (Ptr(Stk ?ofs)) |- _ ] =>
+ let E := fresh in
+ assert (E: Val.lessdef v (Vptr sp ofs)) by (eapply vmatch_ptr_stk; eauto);
+ clear H; SimplVM
+ | _ => idtac
+ end.
+
+Lemma const_for_result_correct:
+ forall a op v,
+ const_for_result a = Some op ->
+ vmatch bc v a ->
+ exists v', eval_operation ge (Vptr sp Ptrofs.zero) op nil m = Some v' /\ Val.lessdef v v'.
+Proof.
+ unfold const_for_result. generalize Archi.ptr64; intros ptr64; intros.
+ destruct a; inv H; SimplVM.
+- (* integer *)
+ exists (Vint n); auto.
+- (* long *)
+ destruct ptr64; inv H2. exists (Vlong n); auto.
+- (* float *)
+ destruct (Compopts.generate_float_constants tt); inv H2. exists (Vfloat f); auto.
+- (* single *)
+ destruct (Compopts.generate_float_constants tt); inv H2. exists (Vsingle f); auto.
+- (* pointer *)
+ destruct p; try discriminate; SimplVM.
+ + (* global *)
+ inv H2. exists (Genv.symbol_address ge id ofs); auto.
+ + (* stack *)
+ inv H2. exists (Vptr sp ofs); split; auto. cbn. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
+Lemma cond_strength_reduction_correct:
+ forall cond args vl,
+ vl = map (fun r => AE.get r ae) args ->
+ let (cond', args') := cond_strength_reduction cond args vl in
+ eval_condition cond' e##args' m = eval_condition cond e##args m.
+Proof.
+ intros until vl. unfold cond_strength_reduction.
+ case (cond_strength_reduction_match cond args vl); cbn; intros; InvApproxRegs; SimplVM.
+- apply Val.swap_cmp_bool.
+- auto.
+- apply Val.swap_cmpu_bool.
+- auto.
+- apply Val.swap_cmpl_bool.
+- auto.
+- apply Val.swap_cmplu_bool.
+- auto.
+- auto.
+Qed.
+
+Lemma make_cmp_base_correct:
+ forall c args vl,
+ vl = map (fun r => AE.get r ae) args ->
+ let (op', args') := make_cmp_base c args vl in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op' e##args' m = Some v
+ /\ Val.lessdef (Val.of_optbool (eval_condition c e##args m)) v.
+Proof.
+ intros. unfold make_cmp_base.
+ generalize (cond_strength_reduction_correct c args vl H).
+ destruct (cond_strength_reduction c args vl) as [c' args']. intros EQ.
+ econstructor; split. cbn; eauto. rewrite EQ. auto.
+Qed.
+
+Lemma make_cmp_correct:
+ forall c args vl,
+ vl = map (fun r => AE.get r ae) args ->
+ let (op', args') := make_cmp c args vl in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op' e##args' m = Some v
+ /\ Val.lessdef (Val.of_optbool (eval_condition c e##args m)) v.
+Proof.
+ intros c args vl.
+ assert (Y: forall r, vincl (AE.get r ae) (Uns Ptop 1) = true ->
+ e#r = Vundef \/ e#r = Vint Int.zero \/ e#r = Vint Int.one).
+ { intros. apply vmatch_Uns_1 with bc Ptop. eapply vmatch_ge. eapply vincl_ge; eauto. apply MATCH. }
+ unfold make_cmp. case (make_cmp_match c args vl); intros.
+- unfold make_cmp_imm_eq.
+ destruct (Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1)) eqn:E1.
++ cbn in H; inv H. InvBooleans. subst n.
+ exists (e#r1); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
++ destruct (Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1)) eqn:E0.
+* cbn in H; inv H. InvBooleans. subst n.
+ exists (Val.xor e#r1 (Vint Int.one)); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
+* apply make_cmp_base_correct; auto.
+- unfold make_cmp_imm_ne.
+ destruct (Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1)) eqn:E0.
++ cbn in H; inv H. InvBooleans. subst n.
+ exists (e#r1); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
++ destruct (Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1)) eqn:E1.
+* cbn in H; inv H. InvBooleans. subst n.
+ exists (Val.xor e#r1 (Vint Int.one)); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
+* apply make_cmp_base_correct; auto.
+- unfold make_cmp_imm_eq.
+ destruct (Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1)) eqn:E1.
++ cbn in H; inv H. InvBooleans. subst n.
+ exists (e#r1); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
++ destruct (Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1)) eqn:E0.
+* cbn in H; inv H. InvBooleans. subst n.
+ exists (Val.xor e#r1 (Vint Int.one)); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
+* apply make_cmp_base_correct; auto.
+- unfold make_cmp_imm_ne.
+ destruct (Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1)) eqn:E0.
++ cbn in H; inv H. InvBooleans. subst n.
+ exists (e#r1); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
++ destruct (Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1)) eqn:E1.
+* cbn in H; inv H. InvBooleans. subst n.
+ exists (Val.xor e#r1 (Vint Int.one)); split; auto. cbn.
+ exploit Y; eauto. intros [A | [A | A]]; rewrite A; cbn; auto.
+* apply make_cmp_base_correct; auto.
+- apply make_cmp_base_correct; auto.
+Qed.
+
+Lemma make_addimm_correct:
+ forall n r,
+ let (op, args) := make_addimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.add e#r (Vint n)) v.
+Proof.
+ intros. unfold make_addimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros.
+ subst. exists (e#r); split; auto.
+ destruct (e#r); cbn; auto; rewrite ?Int.add_zero, ?Ptrofs.add_zero; auto.
+ exists (Val.add e#r (Vint n)); split; auto.
+Qed.
+
+Lemma make_shlimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shlimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shl e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shlimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto. rewrite Int.shl_zero. auto.
+ destruct (Int.ltu n Int.iwordsize).
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_shrimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shrimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shr e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shrimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto. rewrite Int.shr_zero. auto.
+ destruct (Int.ltu n Int.iwordsize).
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_shruimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shruimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shru e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shruimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto. rewrite Int.shru_zero. auto.
+ destruct (Int.ltu n Int.iwordsize).
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_mulimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_mulimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mul e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_mulimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (Vint Int.zero); split; auto. destruct (e#r1); cbn; auto. rewrite Int.mul_zero; auto.
+ predSpec Int.eq Int.eq_spec n Int.one; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto. rewrite Int.mul_one; auto.
+ destruct (Int.is_power2 n) eqn:?; intros.
+ rewrite (Val.mul_pow2 e#r1 _ _ Heqo). econstructor; split. cbn; eauto. auto.
+ econstructor; split; eauto. cbn. rewrite H; auto.
+Qed.
+
+Lemma make_divimm_correct:
+ forall n r1 r2 v,
+ Val.divs e#r1 e#r2 = Some v ->
+ e#r2 = Vint n ->
+ let (op, args) := make_divimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_divimm.
+ predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
+ destruct (e#r1) eqn:?;
+ try (rewrite Val.divs_one in H; exists (Vint i); split; cbn; try rewrite Heqv0; auto);
+ inv H; auto.
+ destruct (Int.is_power2 n) eqn:?.
+ destruct (Int.ltu i (Int.repr 31)) eqn:?.
+ exists v; split; auto. cbn.
+ erewrite Val.divs_pow2; eauto. reflexivity. congruence.
+ exists v; auto.
+ exists v; auto.
+Qed.
+
+Lemma make_divuimm_correct:
+ forall n r1 r2 v,
+ Val.divu e#r1 e#r2 = Some v ->
+ e#r2 = Vint n ->
+ let (op, args) := make_divuimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_divuimm.
+ predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
+ destruct (e#r1) eqn:?;
+ try (rewrite Val.divu_one in H; exists (Vint i); split; cbn; try rewrite Heqv0; auto);
+ inv H; auto.
+ destruct (Int.is_power2 n) eqn:?.
+ econstructor; split. cbn; eauto.
+ rewrite H0 in H. erewrite Val.divu_pow2 by eauto. auto.
+ exists v; auto.
+Qed.
+
+Lemma make_moduimm_correct:
+ forall n r1 r2 v,
+ Val.modu e#r1 e#r2 = Some v ->
+ e#r2 = Vint n ->
+ let (op, args) := make_moduimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_moduimm.
+ destruct (Int.is_power2 n) eqn:?.
+ exists v; split; auto. cbn. decEq. eapply Val.modu_pow2; eauto. congruence.
+ exists v; auto.
+Qed.
+
+Lemma make_andimm_correct:
+ forall n r x,
+ vmatch bc e#r x ->
+ let (op, args) := make_andimm n r x in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.and e#r (Vint n)) v.
+Proof.
+ intros; unfold make_andimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros.
+ subst n. exists (Vint Int.zero); split; auto. destruct (e#r); cbn; auto. rewrite Int.and_zero; auto.
+ predSpec Int.eq Int.eq_spec n Int.mone; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int.and_mone; auto.
+ destruct (match x with Uns _ k => Int.eq (Int.zero_ext k (Int.not n)) Int.zero
+ | _ => false end) eqn:UNS.
+ destruct x; try congruence.
+ exists (e#r); split; auto.
+ inv H; auto. cbn. replace (Int.and i n) with i; auto.
+ generalize (Int.eq_spec (Int.zero_ext n0 (Int.not n)) Int.zero); rewrite UNS; intro EQ.
+ Int.bit_solve. destruct (zlt i0 n0).
+ replace (Int.testbit n i0) with (negb (Int.testbit Int.zero i0)).
+ rewrite Int.bits_zero. cbn. rewrite andb_true_r. auto.
+ rewrite <- EQ. rewrite Int.bits_zero_ext by lia. rewrite zlt_true by auto.
+ rewrite Int.bits_not by auto. apply negb_involutive.
+ rewrite H6 by auto. auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_orimm_correct:
+ forall n r,
+ let (op, args) := make_orimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.or e#r (Vint n)) v.
+Proof.
+ intros; unfold make_orimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int.or_zero; auto.
+ predSpec Int.eq Int.eq_spec n Int.mone; intros.
+ subst n. exists (Vint Int.mone); split; auto. destruct (e#r); cbn; auto. rewrite Int.or_mone; auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_xorimm_correct:
+ forall n r,
+ let (op, args) := make_xorimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.xor e#r (Vint n)) v.
+Proof.
+ intros; unfold make_xorimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int.xor_zero; auto.
+ predSpec Int.eq Int.eq_spec n Int.mone; intros.
+ subst n. exists (Val.notint e#r); split; auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_addlimm_correct:
+ forall n r,
+ let (op, args) := make_addlimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.addl e#r (Vlong n)) v.
+Proof.
+ intros. unfold make_addlimm.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero; intros.
+ subst. exists (e#r); split; auto.
+ destruct (e#r); cbn; auto; rewrite ? Int64.add_zero, ? Ptrofs.add_zero; auto.
+ exists (Val.addl e#r (Vlong n)); split; auto.
+Qed.
+
+Lemma make_shllimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shllimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shll e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shllimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto.
+ unfold Int64.shl'. rewrite Z.shiftl_0_r, Int64.repr_unsigned. auto.
+ destruct (Int.ltu n Int64.iwordsize').
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_shrlimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shrlimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shrl e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shrlimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto.
+ unfold Int64.shr'. rewrite Z.shiftr_0_r, Int64.repr_signed. auto.
+ destruct (Int.ltu n Int64.iwordsize').
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_shrluimm_correct:
+ forall n r1 r2,
+ e#r2 = Vint n ->
+ let (op, args) := make_shrluimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.shrlu e#r1 (Vint n)) v.
+Proof.
+ intros; unfold make_shrluimm.
+ predSpec Int.eq Int.eq_spec n Int.zero; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto.
+ unfold Int64.shru'. rewrite Z.shiftr_0_r, Int64.repr_unsigned. auto.
+ destruct (Int.ltu n Int64.iwordsize').
+ econstructor; split. cbn. eauto. auto.
+ econstructor; split. cbn. eauto. rewrite H; auto.
+Qed.
+
+Lemma make_mullimm_correct:
+ forall n r1 r2,
+ e#r2 = Vlong n ->
+ let (op, args) := make_mullimm n r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mull e#r1 (Vlong n)) v.
+Proof.
+ intros; unfold make_mullimm.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero; intros. subst.
+ exists (Vlong Int64.zero); split; auto. destruct (e#r1); cbn; auto. rewrite Int64.mul_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.one; intros. subst.
+ exists (e#r1); split; auto. destruct (e#r1); cbn; auto. rewrite Int64.mul_one; auto.
+ destruct (Int64.is_power2' n) eqn:?; intros.
+ exists (Val.shll e#r1 (Vint i)); split; auto.
+ destruct (e#r1); cbn; auto.
+ erewrite Int64.is_power2'_range by eauto.
+ erewrite Int64.mul_pow2' by eauto. auto.
+ econstructor; split; eauto. cbn; rewrite H; auto.
+Qed.
+
+Lemma make_divlimm_correct:
+ forall n r1 r2 v,
+ Val.divls e#r1 e#r2 = Some v ->
+ e#r2 = Vlong n ->
+ let (op, args) := make_divlimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_divlimm.
+ destruct (Int64.is_power2' n) eqn:?. destruct (Int.ltu i (Int.repr 63)) eqn:?.
+ rewrite H0 in H. econstructor; split. cbn; eauto.
+ erewrite Val.divls_pow2; eauto. auto.
+ exists v; auto.
+ exists v; auto.
+Qed.
+
+Lemma make_divluimm_correct:
+ forall n r1 r2 v,
+ Val.divlu e#r1 e#r2 = Some v ->
+ e#r2 = Vlong n ->
+ let (op, args) := make_divluimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_divluimm.
+ destruct (Int64.is_power2' n) eqn:?.
+ econstructor; split. cbn; eauto.
+ rewrite H0 in H. destruct (e#r1); inv H. destruct (Int64.eq n Int64.zero); inv H2.
+ cbn.
+ erewrite Int64.is_power2'_range by eauto.
+ erewrite Int64.divu_pow2' by eauto. auto.
+ exists v; auto.
+Qed.
+
+Lemma make_modluimm_correct:
+ forall n r1 r2 v,
+ Val.modlu e#r1 e#r2 = Some v ->
+ e#r2 = Vlong n ->
+ let (op, args) := make_modluimm n r1 r2 in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
+Proof.
+ intros; unfold make_modluimm.
+ destruct (Int64.is_power2 n) eqn:?.
+ exists v; split; auto. cbn. decEq.
+ rewrite H0 in H. destruct (e#r1); inv H. destruct (Int64.eq n Int64.zero); inv H2.
+ cbn. erewrite Int64.modu_and by eauto. auto.
+ exists v; auto.
+Qed.
+
+Lemma make_andlimm_correct:
+ forall n r x,
+ let (op, args) := make_andlimm n r x in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.andl e#r (Vlong n)) v.
+Proof.
+ intros; unfold make_andlimm.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero; intros.
+ subst n. exists (Vlong Int64.zero); split; auto. destruct (e#r); cbn; auto. rewrite Int64.and_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.mone; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int64.and_mone; auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_orlimm_correct:
+ forall n r,
+ let (op, args) := make_orlimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.orl e#r (Vlong n)) v.
+Proof.
+ intros; unfold make_orlimm.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int64.or_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.mone; intros.
+ subst n. exists (Vlong Int64.mone); split; auto. destruct (e#r); cbn; auto. rewrite Int64.or_mone; auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_xorlimm_correct:
+ forall n r,
+ let (op, args) := make_xorlimm n r in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.xorl e#r (Vlong n)) v.
+Proof.
+ intros; unfold make_xorlimm.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero; intros.
+ subst n. exists (e#r); split; auto. destruct (e#r); cbn; auto. rewrite Int64.xor_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.mone; intros.
+ subst n. exists (Val.notl e#r); split; auto.
+ econstructor; split; eauto. auto.
+Qed.
+
+Lemma make_mulfimm_correct:
+ forall n r1 r2,
+ e#r2 = Vfloat n ->
+ let (op, args) := make_mulfimm n r1 r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mulf e#r1 e#r2) v.
+Proof.
+ intros; unfold make_mulfimm.
+ destruct (Float.eq_dec n (Float.of_int (Int.repr 2))); intros.
+ cbn. econstructor; split. eauto. rewrite H; subst n.
+ destruct (e#r1); cbn; auto. rewrite Float.mul2_add; auto.
+ cbn. econstructor; split; eauto.
+Qed.
+
+Lemma make_mulfimm_correct_2:
+ forall n r1 r2,
+ e#r1 = Vfloat n ->
+ let (op, args) := make_mulfimm n r2 r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mulf e#r1 e#r2) v.
+Proof.
+ intros; unfold make_mulfimm.
+ destruct (Float.eq_dec n (Float.of_int (Int.repr 2))); intros.
+ cbn. econstructor; split. eauto. rewrite H; subst n.
+ destruct (e#r2); cbn; auto. rewrite Float.mul2_add; auto.
+ rewrite Float.mul_commut; auto.
+ cbn. econstructor; split; eauto.
+Qed.
+
+Lemma make_mulfsimm_correct:
+ forall n r1 r2,
+ e#r2 = Vsingle n ->
+ let (op, args) := make_mulfsimm n r1 r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mulfs e#r1 e#r2) v.
+Proof.
+ intros; unfold make_mulfsimm.
+ destruct (Float32.eq_dec n (Float32.of_int (Int.repr 2))); intros.
+ cbn. econstructor; split. eauto. rewrite H; subst n.
+ destruct (e#r1); cbn; auto. rewrite Float32.mul2_add; auto.
+ cbn. econstructor; split; eauto.
+Qed.
+
+Lemma make_mulfsimm_correct_2:
+ forall n r1 r2,
+ e#r1 = Vsingle n ->
+ let (op, args) := make_mulfsimm n r2 r1 r2 in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.mulfs e#r1 e#r2) v.
+Proof.
+ intros; unfold make_mulfsimm.
+ destruct (Float32.eq_dec n (Float32.of_int (Int.repr 2))); intros.
+ cbn. econstructor; split. eauto. rewrite H; subst n.
+ destruct (e#r2); cbn; auto. rewrite Float32.mul2_add; auto.
+ rewrite Float32.mul_commut; auto.
+ cbn. econstructor; split; eauto.
+Qed.
+
+Lemma make_cast8signed_correct:
+ forall r x,
+ vmatch bc e#r x ->
+ let (op, args) := make_cast8signed r x in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.sign_ext 8 e#r) v.
+Proof.
+ intros; unfold make_cast8signed. destruct (vincl x (Sgn Ptop 8)) eqn:INCL.
+ exists e#r; split; auto.
+ assert (V: vmatch bc e#r (Sgn Ptop 8)).
+ { eapply vmatch_ge; eauto. apply vincl_ge; auto. }
+ inv V; cbn; auto. rewrite is_sgn_sign_ext in H4 by auto. rewrite H4; auto.
+ econstructor; split; cbn; eauto.
+Qed.
+
+Lemma make_cast16signed_correct:
+ forall r x,
+ vmatch bc e#r x ->
+ let (op, args) := make_cast16signed r x in
+ exists v, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v /\ Val.lessdef (Val.sign_ext 16 e#r) v.
+Proof.
+ intros; unfold make_cast16signed. destruct (vincl x (Sgn Ptop 16)) eqn:INCL.
+ exists e#r; split; auto.
+ assert (V: vmatch bc e#r (Sgn Ptop 16)).
+ { eapply vmatch_ge; eauto. apply vincl_ge; auto. }
+ inv V; cbn; auto. rewrite is_sgn_sign_ext in H4 by auto. rewrite H4; auto.
+ econstructor; split; cbn; eauto.
+Qed.
+
+Lemma op_strength_reduction_correct:
+ forall op args vl v,
+ vl = map (fun r => AE.get r ae) args ->
+ eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some v ->
+ let (op', args') := op_strength_reduction op args vl in
+ exists w, eval_operation ge (Vptr sp Ptrofs.zero) op' e##args' m = Some w /\ Val.lessdef v w.
+Proof.
+ intros until v; unfold op_strength_reduction;
+ case (op_strength_reduction_match op args vl); cbn; intros.
+- (* cast8signed *)
+ InvApproxRegs; SimplVM; inv H0. apply make_cast8signed_correct; auto.
+- (* cast16signed *)
+ InvApproxRegs; SimplVM; inv H0. apply make_cast16signed_correct; auto.
+- (* add 1 *)
+ rewrite Val.add_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_addimm_correct; auto.
+- (* add 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_addimm_correct; auto.
+- (* sub *)
+ InvApproxRegs; SimplVM; inv H0. rewrite Val.sub_add_opp. apply make_addimm_correct; auto.
+- (* mul 1 *)
+ rewrite Val.mul_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_mulimm_correct; auto.
+- (* mul 2*)
+ InvApproxRegs; SimplVM; inv H0. apply make_mulimm_correct; auto.
+- (* divs *)
+ assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_divimm_correct; auto.
+- (* divu *)
+ assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_divuimm_correct; auto.
+- (* modu *)
+ assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_moduimm_correct; auto.
+- (* and 1 *)
+ rewrite Val.and_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andimm_correct; auto.
+- (* and 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_andimm_correct; auto.
+- (* andimm *)
+ inv H; inv H0. apply make_andimm_correct; auto.
+- (* or 1 *)
+ rewrite Val.or_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_orimm_correct; auto.
+- (* or 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_orimm_correct; auto.
+- (* xor 1 *)
+ rewrite Val.xor_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_xorimm_correct; auto.
+- (* xor 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_xorimm_correct; auto.
+- (* shl *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shlimm_correct; auto.
+- (* shr *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shrimm_correct; auto.
+- (* shru *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shruimm_correct; auto.
+- (* addl 1 *)
+ rewrite Val.addl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_addlimm_correct; auto.
+- (* addl 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_addlimm_correct; auto.
+- (* subl *)
+ InvApproxRegs; SimplVM; inv H0.
+ replace (Val.subl e#r1 (Vlong n2)) with (Val.addl e#r1 (Vlong (Int64.neg n2))).
+ apply make_addlimm_correct; auto.
+ unfold Val.addl, Val.subl. destruct Archi.ptr64 eqn:SF, e#r1; auto.
+ rewrite Int64.sub_add_opp; auto.
+ rewrite Ptrofs.sub_add_opp. do 2 f_equal. auto with ptrofs.
+ rewrite Int64.sub_add_opp; auto.
+- (* mull 1 *)
+ rewrite Val.mull_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_mullimm_correct; auto.
+- (* mull 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_mullimm_correct; auto.
+- (* divl *)
+ assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_divlimm_correct; auto.
+- (* divlu *)
+ assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_divluimm_correct; auto.
+- (* modlu *)
+ assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
+ apply make_modluimm_correct; auto.
+- (* andl 1 *)
+ rewrite Val.andl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andlimm_correct; auto.
+- (* andl 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_andlimm_correct; auto.
+- (* andlimm *)
+ inv H; inv H0. apply make_andlimm_correct; auto.
+- (* orl 1 *)
+ rewrite Val.orl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_orlimm_correct; auto.
+- (* orl 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_orlimm_correct; auto.
+- (* xorl 1 *)
+ rewrite Val.xorl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_xorlimm_correct; auto.
+- (* xorl 2 *)
+ InvApproxRegs; SimplVM; inv H0. apply make_xorlimm_correct; auto.
+- (* shll *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shllimm_correct; auto.
+- (* shrl *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shrlimm_correct; auto.
+- (* shrlu *)
+ InvApproxRegs; SimplVM; inv H0. apply make_shrluimm_correct; auto.
+- (* cond *)
+ inv H0. apply make_cmp_correct; auto.
+- (* mulf 1 *)
+ InvApproxRegs; SimplVM; inv H0. rewrite <- H2. apply make_mulfimm_correct; auto.
+- (* mulf 2 *)
+ InvApproxRegs; SimplVM; inv H0. fold (Val.mulf (Vfloat n1) e#r2).
+ rewrite <- H2. apply make_mulfimm_correct_2; auto.
+- (* mulfs 1 *)
+ InvApproxRegs; SimplVM; inv H0. rewrite <- H2. apply make_mulfsimm_correct; auto.
+- (* mulfs 2 *)
+ InvApproxRegs; SimplVM; inv H0. fold (Val.mulfs (Vsingle n1) e#r2).
+ rewrite <- H2. apply make_mulfsimm_correct_2; auto.
+- (* default *)
+ exists v; auto.
+Qed.
+
+Lemma addr_strength_reduction_correct:
+ forall addr args vl res,
+ vl = map (fun r => AE.get r ae) args ->
+ eval_addressing ge (Vptr sp Ptrofs.zero) addr e##args = Some res ->
+ let (addr', args') := addr_strength_reduction addr args vl in
+ exists res', eval_addressing ge (Vptr sp Ptrofs.zero) addr' e##args' = Some res' /\ Val.lessdef res res'.
+Proof.
+ intros until res. unfold addr_strength_reduction.
+ destruct (addr_strength_reduction_match addr args vl); cbn;
+ intros VL EA; InvApproxRegs; SimplVM; try (inv EA).
+- destruct (orb _ _).
++ exists (Val.offset_ptr e#r1 n); auto.
++ cbn. rewrite Genv.shift_symbol_address. econstructor; split; eauto.
+ inv H0; cbn; auto.
+- rewrite Ptrofs.add_zero_l. econstructor; split; eauto.
+ change (Vptr sp (Ptrofs.add n1 n)) with (Val.offset_ptr (Vptr sp n1) n).
+ inv H0; cbn; auto.
+- exists res; auto.
+Qed.
+
+End STRENGTH_REDUCTION.
diff --git a/kvx/Conventions1.v b/kvx/Conventions1.v
new file mode 100644
index 00000000..d8eff34e
--- /dev/null
+++ b/kvx/Conventions1.v
@@ -0,0 +1,431 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Function calling conventions and other conventions regarding the use of
+ machine registers and stack slots. *)
+
+Require Import Coqlib Decidableplus.
+Require Import AST Machregs Locations.
+
+(** * Classification of machine registers *)
+
+(** Machine registers (type [mreg] in module [Locations]) are divided in
+ the following groups:
+- Callee-save registers, whose value is preserved across a function call.
+- Caller-save registers that can be modified during a function call.
+
+ We follow the RISC-V application binary interface (ABI) in our choice
+ of callee- and caller-save registers.
+*)
+
+Definition is_callee_save (r: mreg) : bool :=
+ match r with
+ (* | R15 | R16 | R17 *) | R18 | R19 | R20 | R21 | R22
+ | R23 | R24 | R25 | R26 | R27 | R28 | R29 | R30 | R31 => true
+ | _ => false
+ end.
+
+Definition int_caller_save_regs :=
+ R0 :: R1 :: R2 :: R3 :: R4 :: R5 :: R6 :: R7 :: R8 :: R9
+ :: R10 :: R11 :: R15 (* :: R16 *) :: R17
+ (* :: R32 *) :: R33 :: R34 :: R35 :: R36 :: R37 :: R38 :: R39 :: R40 :: R41
+ :: R42 :: R43 :: R44 :: R45 :: R46 :: R47 :: R48 :: R49 :: R50 :: R51
+ :: R52 :: R53 :: R54 :: R55 :: R56 :: R57 :: R58 :: R59 :: R60 :: R61
+ :: R62 :: R63 :: nil.
+
+Definition float_caller_save_regs : list mreg := nil.
+
+Definition int_callee_save_regs :=
+ (* R15 :: R16 :: R17 :: *)R18 :: R19 :: R20 :: R21 :: R22
+ :: R23 :: R24 :: R25 :: R26 :: R27 :: R28 :: R29 :: R30 :: R31 :: nil.
+
+Definition float_callee_save_regs : list mreg := nil.
+
+Definition destroyed_at_call :=
+ List.filter (fun r => negb (is_callee_save r)) all_mregs.
+
+Definition dummy_int_reg := R63. (**r Used in [Coloring]. *)
+Definition dummy_float_reg := R62. (**r Used in [Coloring]. *)
+
+Definition callee_save_type := mreg_type.
+
+Definition is_float_reg (r: mreg) := false.
+
+(** * Function calling conventions *)
+
+(** The functions in this section determine the locations (machine registers
+ and stack slots) used to communicate arguments and results between the
+ caller and the callee during function calls. These locations are functions
+ of the signature of the function and of the call instruction.
+ Agreement between the caller and the callee on the locations to use
+ is guaranteed by our dynamic semantics for Cminor and RTL, which demand
+ that the signature of the call instruction is identical to that of the
+ called function.
+
+ Calling conventions are largely arbitrary: they must respect the properties
+ proved in this section (such as no overlapping between the locations
+ of function arguments), but this leaves much liberty in choosing actual
+ locations. To ensure binary interoperability of code generated by our
+ compiler with libraries compiled by another compiler, we
+ implement the standard RISC-V conventions. *)
+
+(** ** Location of function result *)
+
+(** The result value of a function is passed back to the caller in
+ registers [R10] or [F10] or [R10,R11], depending on the type of the
+ returned value. We treat a function without result as a function
+ with one integer result. *)
+
+
+Definition loc_result (s: signature) : rpair mreg :=
+ match s.(sig_res) with
+ | Tvoid => One R0
+ | Tint8signed => One R0
+ | Tint8unsigned => One R0
+ | Tint16signed => One R0
+ | Tint16unsigned => One R0
+ | Tint | Tany32 => One R0
+ | Tfloat | Tsingle | Tany64 => One R0
+ | Tlong => if Archi.ptr64 then One R0 else One R0
+ end.
+
+(** The result registers have types compatible with that given in the signature. *)
+
+Lemma loc_result_type:
+ forall sig,
+ subtype (proj_sig_res sig) (typ_rpair mreg_type (loc_result sig)) = true.
+Proof.
+ intros. unfold proj_sig_res, loc_result, mreg_type.
+ destruct (sig_res sig); try destruct Archi.ptr64; cbn; trivial; destruct t; trivial.
+Qed.
+
+(** The result locations are caller-save registers *)
+
+Lemma loc_result_caller_save:
+ forall (s: signature),
+ forall_rpair (fun r => is_callee_save r = false) (loc_result s).
+Proof.
+ intros. unfold loc_result, is_callee_save;
+ destruct (sig_res s); cbn; auto; try destruct Archi.ptr64; cbn; auto; try destruct t; cbn; auto.
+Qed.
+
+(** If the result is in a pair of registers, those registers are distinct and have type [Tint] at least. *)
+
+Lemma loc_result_pair:
+ forall sg,
+ match loc_result sg with
+ | One _ => True
+ | Twolong r1 r2 =>
+ r1 <> r2 /\ proj_sig_res sg = Tlong
+ /\ subtype Tint (mreg_type r1) = true /\ subtype Tint (mreg_type r2) = true
+ /\ Archi.ptr64 = false
+ end.
+Proof.
+ intros.
+ unfold loc_result; destruct (sig_res sg); auto;
+ unfold mreg_type; try destruct Archi.ptr64; auto;
+ destruct t; auto.
+Qed.
+
+(** The location of the result depends only on the result part of the signature *)
+
+Lemma loc_result_exten:
+ forall s1 s2, s1.(sig_res) = s2.(sig_res) -> loc_result s1 = loc_result s2.
+Proof.
+ intros. unfold loc_result. rewrite H; auto.
+Qed.
+
+(** ** Location of function arguments *)
+
+(** The RISC-V ABI states the following convention for passing arguments
+ to a function:
+
+- Arguments are passed in registers when possible.
+
+- Up to eight integer registers (ai: int_param_regs) and up to eight
+ floating-point registers (fai: float_param_regs) are used for this
+ purpose.
+
+- If the arguments to a function are conceptualized as fields of a C
+ struct, each with pointer alignment, the argument registers are a
+ shadow of the first eight pointer-words of that struct. If argument
+ i < 8 is a floating-point type, it is passed in floating-point
+ register fa_i; otherwise, it is passed in integer register a_i.
+
+- When primitive arguments twice the size of a pointer-word are passed
+ on the stack, they are naturally aligned. When they are passed in the
+ integer registers, they reside in an aligned even-odd register pair,
+ with the even register holding the least-significant bits.
+
+- Floating-point arguments to variadic functions (except those that
+ are explicitly named in the parameter list) are passed in integer
+ registers.
+
+- The portion of the conceptual struct that is not passed in argument
+ registers is passed on the stack. The stack pointer sp points to the
+ first argument not passed in a register.
+
+The bit about variadic functions doesn't quite fit CompCert's model.
+We do our best by passing the FP arguments in registers, as usual,
+and reserving the corresponding integer registers, so that fixup
+code can be introduced in the Asmexpand pass.
+*)
+
+Definition param_regs :=
+ R0 :: R1 :: R2 :: R3 :: R4 :: R5 :: R6 :: R7 :: R8 :: R9 :: R10 :: R11 :: nil.
+
+Definition one_arg (regs: list mreg) (rn: Z) (ofs: Z) (ty: typ)
+ (rec: Z -> Z -> list (rpair loc)) :=
+ match list_nth_z regs rn with
+ | Some r =>
+ One(R r) :: rec (rn + 1) ofs
+ | None =>
+ let ofs := align ofs (typealign ty) in
+ One(S Outgoing ofs ty) :: rec rn (ofs + (if Archi.ptr64 then 2 else typesize ty))
+ end.
+
+Definition two_args (regs: list mreg) (rn: Z) (ofs: Z)
+ (rec: Z -> Z -> list (rpair loc)) :=
+ let rn := align rn 2 in
+ match list_nth_z regs rn, list_nth_z regs (rn + 1) with
+ | Some r1, Some r2 =>
+ Twolong (R r2) (R r1) :: rec (rn + 2) ofs
+ | _, _ =>
+ let ofs := align ofs 2 in
+ Twolong (S Outgoing (ofs + 1) Tint) (S Outgoing ofs Tint) ::
+ rec rn (ofs + 2)
+ end.
+
+Definition hybrid_arg (regs: list mreg) (rn: Z) (ofs: Z) (ty: typ)
+ (rec: Z -> Z -> list (rpair loc)) :=
+ let rn := align rn 2 in
+ match list_nth_z regs rn with
+ | Some r =>
+ One (R r) :: rec (rn + 2) ofs
+ | None =>
+ let ofs := align ofs 2 in
+ One (S Outgoing ofs ty) :: rec rn (ofs + 2)
+ end.
+
+Fixpoint loc_arguments_rec (va: bool)
+ (tyl: list typ) (r ofs: Z) {struct tyl} : list (rpair loc) :=
+ match tyl with
+ | nil => nil
+ | ty :: tys => one_arg param_regs r ofs ty (loc_arguments_rec va tys)
+(*
+ | (Tint | Tany32) as ty :: tys =>
+ one_arg int_param_regs r ofs ty (loc_arguments_rec va tys)
+ | Tsingle as ty :: tys =>
+ one_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
+ | Tlong as ty :: tys =>
+ if Archi.ptr64
+ then one_arg int_param_regs r ofs ty (loc_arguments_rec va tys)
+ else two_args int_param_regs r ofs (loc_arguments_rec va tys)
+ | (Tfloat | Tany64) as ty :: tys =>
+ if va && negb Archi.ptr64
+ then hybrid_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
+ else one_arg float_param_regs r ofs ty (loc_arguments_rec va tys)
+*)
+ end.
+
+(* FIX Sylvain: not sure to understand what I have done... *)
+Definition has_va (s: signature) : bool :=
+ match s.(sig_cc).(cc_vararg) with
+ | Some n => true
+ | None => false
+ end.
+
+(** [loc_arguments s] returns the list of locations where to store arguments
+ when calling a function with signature [s]. *)
+
+Definition loc_arguments (s: signature) : list (rpair loc) :=
+ loc_arguments_rec (has_va s) s.(sig_args) 0 0.
+
+(** [size_arguments s] returns the number of [Outgoing] slots used
+ to call a function with signature [s]. *)
+
+Definition max_outgoing_1 (accu: Z) (l: loc) : Z :=
+ match l with
+ | S Outgoing ofs ty => Z.max accu (ofs + typesize ty)
+ | _ => accu
+ end.
+
+Definition max_outgoing_2 (accu: Z) (rl: rpair loc) : Z :=
+ match rl with
+ | One l => max_outgoing_1 accu l
+ | Twolong l1 l2 => max_outgoing_1 (max_outgoing_1 accu l1) l2
+ end.
+
+Definition size_arguments (s: signature) : Z :=
+ List.fold_left max_outgoing_2 (loc_arguments s) 0.
+
+(** Argument locations are either non-temporary registers or [Outgoing]
+ stack slots at nonnegative offsets. *)
+
+Definition loc_argument_acceptable (l: loc) : Prop :=
+ match l with
+ | R r => is_callee_save r = false
+ | S Outgoing ofs ty => ofs >= 0 /\ (typealign ty | ofs)
+ | _ => False
+ end.
+
+Lemma loc_arguments_rec_charact:
+ forall va tyl rn ofs p,
+ ofs >= 0 ->
+ In p (loc_arguments_rec va tyl rn ofs) -> forall_rpair loc_argument_acceptable p.
+Proof.
+ set (OK := fun (l: list (rpair loc)) =>
+ forall p, In p l -> forall_rpair loc_argument_acceptable p).
+ set (OKF := fun (f: Z -> Z -> list (rpair loc)) =>
+ forall rn ofs, ofs >= 0 -> OK (f rn ofs)).
+ set (OKREGS := fun (l: list mreg) => forall r, In r l -> is_callee_save r = false).
+ assert (AL: forall ofs ty, ofs >= 0 -> align ofs (typealign ty) >= 0).
+ { intros.
+ assert (ofs <= align ofs (typealign ty)) by (apply align_le; apply typealign_pos).
+ lia. }
+ assert (SK: (if Archi.ptr64 then 2 else 1) > 0).
+ { destruct Archi.ptr64; lia. }
+ assert (SKK: forall ty, (if Archi.ptr64 then 2 else typesize ty) > 0).
+ { intros. destruct Archi.ptr64. lia. apply typesize_pos. }
+ assert (A: forall regs rn ofs ty f,
+ OKREGS regs -> OKF f -> ofs >= 0 -> OK (one_arg regs rn ofs ty f)).
+ { intros until f; intros OR OF OO; red; unfold one_arg; intros.
+ destruct (list_nth_z regs rn) as [r|] eqn:NTH; destruct H.
+ - subst p; cbn. apply OR. eapply list_nth_z_in; eauto.
+ - eapply OF; eauto.
+ - subst p; cbn. auto using align_divides, typealign_pos.
+ - eapply OF; [idtac|eauto].
+ generalize (AL ofs ty OO) (SKK ty); lia.
+ }
+ assert (B: forall regs rn ofs f,
+ OKREGS regs -> OKF f -> ofs >= 0 -> OK (two_args regs rn ofs f)).
+ { intros until f; intros OR OF OO; unfold two_args.
+ set (rn' := align rn 2).
+ set (ofs' := align ofs 2).
+ assert (OO': ofs' >= 0) by (apply (AL ofs Tlong); auto).
+ assert (DFL: OK (Twolong (S Outgoing (ofs' + 1) Tint) (S Outgoing ofs' Tint)
+ :: f rn' (ofs' + 2))).
+ { red; cbn; intros. destruct H.
+ - subst p; cbn.
+ repeat split; auto using Z.divide_1_l. lia.
+ - eapply OF; [idtac|eauto]. lia.
+ }
+ destruct (list_nth_z regs rn') as [r1|] eqn:NTH1;
+ destruct (list_nth_z regs (rn' + 1)) as [r2|] eqn:NTH2;
+ try apply DFL.
+ red; cbn; intros; destruct H.
+ - subst p; cbn. split; apply OR; eauto using list_nth_z_in.
+ - eapply OF; [idtac|eauto]. auto.
+ }
+ assert (C: forall regs rn ofs ty f,
+ OKREGS regs -> OKF f -> ofs >= 0 -> typealign ty = 1 -> OK (hybrid_arg regs rn ofs ty f)).
+ { intros until f; intros OR OF OO OTY; unfold hybrid_arg; red; intros.
+ set (rn' := align rn 2) in *.
+ destruct (list_nth_z regs rn') as [r|] eqn:NTH; destruct H.
+ - subst p; cbn. apply OR. eapply list_nth_z_in; eauto.
+ - eapply OF; eauto.
+ - subst p; cbn. rewrite OTY. split. apply (AL ofs Tlong OO). apply Z.divide_1_l.
+ - eapply OF; [idtac|eauto]. generalize (AL ofs Tlong OO); cbn; lia.
+ }
+ assert (D: OKREGS param_regs).
+ { red. decide_goal. }
+ assert (E: OKREGS param_regs).
+ { red. decide_goal. }
+
+ cut (forall va tyl rn ofs, ofs >= 0 -> OK (loc_arguments_rec va tyl rn ofs)).
+ unfold OK. eauto.
+ induction tyl as [ | ty1 tyl]; intros until ofs; intros OO; cbn.
+ - red; cbn; tauto.
+ - destruct ty1.
++ (* int *) apply A; auto.
++ (* float *)
+ apply A; auto.
++ (* long *)
+ apply A; auto.
++ (* single *)
+ apply A; auto.
++ (* any32 *)
+ apply A; auto.
++ (* any64 *)
+ apply A; auto.
+Qed.
+
+Lemma loc_arguments_acceptable:
+ forall (s: signature) (p: rpair loc),
+ In p (loc_arguments s) -> forall_rpair loc_argument_acceptable p.
+Proof.
+ unfold loc_arguments; intros. eapply loc_arguments_rec_charact; eauto. lia.
+Qed.
+
+(** The offsets of [Outgoing] arguments are below [size_arguments s]. *)
+
+Remark fold_max_outgoing_above:
+ forall l n, fold_left max_outgoing_2 l n >= n.
+Proof.
+ assert (A: forall n l, max_outgoing_1 n l >= n).
+ { intros; unfold max_outgoing_1. destruct l as [_ | []]; lia. }
+ induction l; cbn; intros.
+ - lia.
+ - eapply Zge_trans. eauto.
+ destruct a; cbn. apply A. eapply Zge_trans; eauto.
+Qed.
+
+Lemma size_arguments_above:
+ forall s, size_arguments s >= 0.
+Proof.
+ intros. apply fold_max_outgoing_above.
+Qed.
+
+Lemma loc_arguments_bounded:
+ forall (s: signature) (ofs: Z) (ty: typ),
+ In (S Outgoing ofs ty) (regs_of_rpairs (loc_arguments s)) ->
+ ofs + typesize ty <= size_arguments s.
+Proof.
+ intros until ty.
+ assert (A: forall n l, n <= max_outgoing_1 n l).
+ { intros; unfold max_outgoing_1. destruct l as [_ | []]; lia. }
+ assert (B: forall p n,
+ In (S Outgoing ofs ty) (regs_of_rpair p) ->
+ ofs + typesize ty <= max_outgoing_2 n p).
+ { intros. destruct p; cbn in H; intuition; subst; cbn.
+ - lia.
+ - eapply Z.le_trans. 2: apply A. lia.
+ - lia. }
+ assert (C: forall l n,
+ In (S Outgoing ofs ty) (regs_of_rpairs l) ->
+ ofs + typesize ty <= fold_left max_outgoing_2 l n).
+ { induction l; cbn; intros.
+ - contradiction.
+ - rewrite in_app_iff in H. destruct H.
+ + eapply Z.le_trans. eapply B; eauto. apply Z.ge_le. apply fold_max_outgoing_above.
+ + apply IHl; auto.
+ }
+ apply C.
+Qed.
+
+Lemma loc_arguments_main:
+ loc_arguments signature_main = nil.
+Proof.
+ reflexivity.
+Qed.
+
+
+(** ** Normalization of function results and parameters *)
+
+(** No normalization needed. *)
+
+Definition return_value_needs_normalization (t: rettype): bool := false.
+Definition parameter_needs_normalization (t: rettype): bool := false.
+
diff --git a/kvx/DecBoolOps.v b/kvx/DecBoolOps.v
new file mode 100644
index 00000000..1e0a6187
--- /dev/null
+++ b/kvx/DecBoolOps.v
@@ -0,0 +1,30 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Set Implicit Arguments.
+
+Theorem and_dec : forall A B C D : Prop,
+ { A } + { B } -> { C } + { D } ->
+ { A /\ C } + { (B /\ C) \/ (B /\ D) \/ (A /\ D) }.
+Proof.
+ intros A B C D AB CD.
+ destruct AB; destruct CD.
+ - left. tauto.
+ - right. tauto.
+ - right. tauto.
+ - right. tauto.
+Qed.
+
+
diff --git a/kvx/DuplicateOpcodeHeuristic.ml b/kvx/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..38702e1b
--- /dev/null
+++ b/kvx/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* open Camlcoq *)
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccomplimm (c, n) | Ccompluimm (c, n) -> if n == Integers.Int64.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c | Ccompfs c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c | Cnotcompfs c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
diff --git a/kvx/ExpansionOracle.ml b/kvx/ExpansionOracle.ml
new file mode 120000
index 00000000..ee2674bf
--- /dev/null
+++ b/kvx/ExpansionOracle.ml
@@ -0,0 +1 @@
+../aarch64/ExpansionOracle.ml \ No newline at end of file
diff --git a/kvx/ExtFloats.v b/kvx/ExtFloats.v
new file mode 100644
index 00000000..b08503a5
--- /dev/null
+++ b/kvx/ExtFloats.v
@@ -0,0 +1,54 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Floats Integers ZArith.
+
+Module ExtFloat.
+(** TODO check with the actual KVX;
+ this is what happens on x86 and may be inappropriate. *)
+
+Definition min (x : float) (y : float) : float :=
+ match Float.compare x y with
+ | Some Eq | Some Lt => x
+ | Some Gt | None => y
+ end.
+
+Definition max (x : float) (y : float) : float :=
+ match Float.compare x y with
+ | Some Eq | Some Gt => x
+ | Some Lt | None => y
+ end.
+End ExtFloat.
+
+Module ExtFloat32.
+(** TODO check with the actual KVX *)
+
+Definition min (x : float32) (y : float32) : float32 :=
+ match Float32.compare x y with
+ | Some Eq | Some Lt => x
+ | Some Gt | None => y
+ end.
+
+Definition max (x : float32) (y : float32) : float32 :=
+ match Float32.compare x y with
+ | Some Eq | Some Gt => x
+ | Some Lt | None => y
+ end.
+
+Definition one := Float32.of_int (Int.repr (1%Z)).
+Definition inv (x : float32) : float32 :=
+ Float32.div one x.
+
+End ExtFloat32.
diff --git a/kvx/ExtValues.v b/kvx/ExtValues.v
new file mode 100644
index 00000000..b4e14898
--- /dev/null
+++ b/kvx/ExtValues.v
@@ -0,0 +1,756 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import Integers.
+Require Import Values.
+Require Import Floats ExtFloats.
+Require Import Lia.
+
+Open Scope Z_scope.
+
+Definition abs_diff (x y : Z) := Z.abs (x - y).
+Definition abs_diff2 (x y : Z) :=
+ if x <=? y then y - x else x - y.
+Lemma abs_diff2_correct :
+ forall x y : Z, (abs_diff x y) = (abs_diff2 x y).
+Proof.
+ intros.
+ unfold abs_diff, abs_diff2.
+ unfold Z.leb.
+ pose proof (Z.compare_spec x y) as Hspec.
+ inv Hspec.
+ - rewrite Z.abs_eq; lia.
+ - rewrite Z.abs_neq; lia.
+ - rewrite Z.abs_eq; lia.
+Qed.
+
+Inductive shift1_4 : Type :=
+| SHIFT1 | SHIFT2 | SHIFT3 | SHIFT4.
+
+Definition z_of_shift1_4 (x : shift1_4) :=
+ match x with
+ | SHIFT1 => 1
+ | SHIFT2 => 2
+ | SHIFT3 => 3
+ | SHIFT4 => 4
+ end.
+
+Definition shift1_4_of_z (x : Z) :=
+ if Z.eq_dec x 1 then Some SHIFT1
+ else if Z.eq_dec x 2 then Some SHIFT2
+ else if Z.eq_dec x 3 then Some SHIFT3
+ else if Z.eq_dec x 4 then Some SHIFT4
+ else None.
+
+Lemma shift1_4_of_z_correct :
+ forall z,
+ match shift1_4_of_z z with
+ | Some x => z_of_shift1_4 x = z
+ | None => True
+ end.
+Proof.
+ intro. unfold shift1_4_of_z.
+ destruct (Z.eq_dec _ _); cbn; try congruence.
+ destruct (Z.eq_dec _ _); cbn; try congruence.
+ destruct (Z.eq_dec _ _); cbn; try congruence.
+ destruct (Z.eq_dec _ _); cbn; try congruence.
+ trivial.
+Qed.
+
+Definition int_of_shift1_4 (x : shift1_4) :=
+ Int.repr (z_of_shift1_4 x).
+
+Definition is_bitfield stop start :=
+ (Z.leb start stop)
+ && (Z.geb start Z.zero)
+ && (Z.ltb stop Int.zwordsize).
+
+Definition extfz stop start v :=
+ if is_bitfield stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | Vint w =>
+ Vint (Int.shru (Int.shl w (Int.repr (Z.sub Int.zwordsize stop'))) (Int.repr (Z.sub Int.zwordsize (Z.sub stop' start))))
+ | _ => Vundef
+ end
+ else Vundef.
+
+
+Definition extfs stop start v :=
+ if is_bitfield stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | Vint w =>
+ Vint (Int.shr (Int.shl w (Int.repr (Z.sub Int.zwordsize stop'))) (Int.repr (Z.sub Int.zwordsize (Z.sub stop' start))))
+ | _ => Vundef
+ end
+ else Vundef.
+
+Definition zbitfield_mask stop start :=
+ (Z.shiftl 1 (Z.succ stop)) - (Z.shiftl 1 start).
+
+Definition bitfield_mask stop start :=
+ Vint(Int.repr (zbitfield_mask stop start)).
+
+Definition bitfield_maskl stop start :=
+ Vlong(Int64.repr (zbitfield_mask stop start)).
+
+Definition insf stop start prev fld :=
+ let mask := bitfield_mask stop start in
+ if is_bitfield stop start
+ then
+ Val.or (Val.and prev (Val.notint mask))
+ (Val.and (Val.shl fld (Vint (Int.repr start))) mask)
+ else Vundef.
+
+Definition is_bitfieldl stop start :=
+ (Z.leb start stop)
+ && (Z.geb start Z.zero)
+ && (Z.ltb stop Int64.zwordsize).
+
+Definition extfzl stop start v :=
+ if is_bitfieldl stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | Vlong w =>
+ Vlong (Int64.shru' (Int64.shl' w (Int.repr (Z.sub Int64.zwordsize stop'))) (Int.repr (Z.sub Int64.zwordsize (Z.sub stop' start))))
+ | _ => Vundef
+ end
+ else Vundef.
+
+
+Definition extfsl stop start v :=
+ if is_bitfieldl stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | Vlong w =>
+ Vlong (Int64.shr' (Int64.shl' w (Int.repr (Z.sub Int64.zwordsize stop'))) (Int.repr (Z.sub Int64.zwordsize (Z.sub stop' start))))
+ | _ => Vundef
+ end
+ else Vundef.
+
+Definition insfl stop start prev fld :=
+ let mask := bitfield_maskl stop start in
+ if is_bitfieldl stop start
+ then
+ Val.orl (Val.andl prev (Val.notl mask))
+ (Val.andl (Val.shll fld (Vint (Int.repr start))) mask)
+ else Vundef.
+
+Fixpoint highest_bit (x : Z) (n : nat) : Z :=
+ match n with
+ | O => 0
+ | S n1 =>
+ let n' := Z.of_N (N_of_nat n) in
+ if Z.testbit x n'
+ then n'
+ else highest_bit x n1
+ end.
+
+Definition int_highest_bit (x : int) : Z :=
+ highest_bit (Int.unsigned x) (31%nat).
+
+
+Definition int64_highest_bit (x : int64) : Z :=
+ highest_bit (Int64.unsigned x) (63%nat).
+
+Definition val_shrx (v1 v2: val): val :=
+ match v1, v2 with
+ | Vint n1, Vint n2 =>
+ if Int.ltu n2 (Int.repr 31)
+ then Vint(Int.shrx n1 n2)
+ else Vundef
+ | _, _ => Vundef
+ end.
+
+Definition val_shrxl (v1 v2: val): val :=
+ match v1, v2 with
+ | Vlong n1, Vint n2 =>
+ if Int.ltu n2 (Int.repr 63)
+ then Vlong(Int64.shrx' n1 n2)
+ else Vundef
+ | _, _ => Vundef
+ end.
+
+Remark modulus_fits_64: Int.modulus < Int64.max_unsigned.
+Proof.
+ compute.
+ trivial.
+Qed.
+
+Remark unsigned64_repr :
+ forall i,
+ -1 < i < Int.modulus ->
+ Int64.unsigned (Int64.repr i) = i.
+Proof.
+ intros i H.
+ destruct H as [Hlow Hhigh].
+ apply Int64.unsigned_repr.
+ split. { lia. }
+ pose proof modulus_fits_64.
+ lia.
+Qed.
+
+Theorem divu_is_divlu: forall v1 v2 : val,
+ Val.divu v1 v2 =
+ match Val.divlu (Val.longofintu v1) (Val.longofintu v2) with
+ | None => None
+ | Some q => Some (Val.loword q)
+ end.
+Proof.
+ intros.
+ destruct v1; cbn; trivial.
+ destruct v2; cbn; trivial.
+ destruct i as [i_val i_range].
+ destruct i0 as [i0_val i0_range].
+ cbn.
+ unfold Int.eq, Int64.eq, Int.zero, Int64.zero.
+ cbn.
+ rewrite Int.unsigned_repr by (compute; split; discriminate).
+ rewrite (Int64.unsigned_repr 0) by (compute; split; discriminate).
+ rewrite (unsigned64_repr i0_val) by assumption.
+ destruct (zeq i0_val 0) as [ | Hnot0]; cbn; trivial.
+ f_equal. f_equal.
+ unfold Int.divu, Int64.divu. cbn.
+ rewrite (unsigned64_repr i_val) by assumption.
+ rewrite (unsigned64_repr i0_val) by assumption.
+ unfold Int64.loword.
+ rewrite Int64.unsigned_repr.
+ reflexivity.
+ destruct (Z.eq_dec i0_val 1).
+ {subst i0_val.
+ pose proof modulus_fits_64.
+ rewrite Zdiv_1_r.
+ lia.
+ }
+ destruct (Z.eq_dec i_val 0).
+ { subst i_val. compute.
+ split;
+ intro ABSURD;
+ discriminate ABSURD. }
+ assert ((i_val / i0_val) < i_val).
+ { apply Z_div_lt; lia. }
+ split.
+ { apply Z_div_pos; lia. }
+ pose proof modulus_fits_64.
+ lia.
+Qed.
+
+Theorem modu_is_modlu: forall v1 v2 : val,
+ Val.modu v1 v2 =
+ match Val.modlu (Val.longofintu v1) (Val.longofintu v2) with
+ | None => None
+ | Some q => Some (Val.loword q)
+ end.
+Proof.
+ intros.
+ destruct v1; cbn; trivial.
+ destruct v2; cbn; trivial.
+ destruct i as [i_val i_range].
+ destruct i0 as [i0_val i0_range].
+ cbn.
+ unfold Int.eq, Int64.eq, Int.zero, Int64.zero.
+ cbn.
+ rewrite Int.unsigned_repr by (compute; split; discriminate).
+ rewrite (Int64.unsigned_repr 0) by (compute; split; discriminate).
+ rewrite (unsigned64_repr i0_val) by assumption.
+ destruct (zeq i0_val 0) as [ | Hnot0]; cbn; trivial.
+ f_equal. f_equal.
+ unfold Int.modu, Int64.modu. cbn.
+ rewrite (unsigned64_repr i_val) by assumption.
+ rewrite (unsigned64_repr i0_val) by assumption.
+ unfold Int64.loword.
+ rewrite Int64.unsigned_repr.
+ reflexivity.
+ assert((i_val mod i0_val) < i0_val).
+ apply Z_mod_lt.
+ lia.
+ split.
+ { apply Z_mod_lt.
+ lia. }
+ pose proof modulus_fits_64.
+ lia.
+Qed.
+
+Remark if_zlt_0_half_modulus :
+ forall T : Type,
+ forall x y: T,
+ (if (zlt 0 Int.half_modulus) then x else y) = x.
+Proof.
+ reflexivity.
+Qed.
+
+Remark if_zlt_mone_half_modulus :
+ forall T : Type,
+ forall x y: T,
+ (if (zlt (Int.unsigned Int.mone) Int.half_modulus) then x else y) = y.
+Proof.
+ reflexivity.
+Qed.
+
+Remark if_zlt_min_signed_half_modulus :
+ forall T : Type,
+ forall x y: T,
+ (if (zlt (Int.unsigned (Int.repr Int.min_signed))
+ Int.half_modulus)
+ then x
+ else y) = y.
+Proof.
+ reflexivity.
+Qed.
+
+Lemma repr_unsigned64_repr:
+ forall x, Int.repr (Int64.unsigned (Int64.repr x)) = Int.repr x.
+Proof.
+ intros.
+ apply Int.eqm_samerepr.
+ unfold Int.eqm.
+ unfold Zbits.eqmod.
+ pose proof (Int64.eqm_unsigned_repr x) as H64.
+ unfold Int64.eqm in H64.
+ unfold Zbits.eqmod in H64.
+ destruct H64 as [k64 H64].
+ change Int64.modulus with 18446744073709551616 in *.
+ change Int.modulus with 4294967296.
+ exists (-4294967296 * k64).
+ set (y := Int64.unsigned (Int64.repr x)) in *.
+ rewrite H64.
+ clear H64.
+ lia.
+Qed.
+
+(*
+Theorem divs_is_divls: forall v1 v2 : val,
+ match Val.divs v1 v2 with
+ | Some q =>
+ match Val.divls (Val.longofint v1) (Val.longofint v2) with
+ | None => False
+ | Some q' => q = Val.loword q'
+ end
+ | None => True
+ end.
+Proof.
+ intros.
+ destruct v1; cbn; trivial.
+ destruct v2; cbn; trivial.
+ destruct i as [i_val i_range].
+ destruct i0 as [i0_val i0_range].
+ cbn.
+ unfold Int.eq, Int64.eq, Int.zero, Int64.zero.
+ cbn.
+ replace (Int.unsigned (Int.repr 0)) with 0 in * by reflexivity.
+ destruct (zeq _ _) as [H0' | Hnot0]; cbn; trivial.
+ destruct (zeq i_val (Int.unsigned (Int.repr Int.min_signed))) as [Hmin | Hnotmin]; cbn.
+ { subst.
+ destruct (zeq i0_val (Int.unsigned Int.mone)) as [Hmone | Hnotmone]; trivial.
+ unfold Int.signed. cbn.
+ replace (Int64.unsigned (Int64.repr 0)) with 0 in * by reflexivity.
+ rewrite if_zlt_min_signed_half_modulus.
+ replace (if
+ zeq
+ (Int64.unsigned
+ (Int64.repr
+ (Int.unsigned (Int.repr Int.min_signed) - Int.modulus)))
+ (Int64.unsigned (Int64.repr Int64.min_signed))
+ then true
+ else false) with false by reflexivity.
+ cbn.
+ rewrite orb_false_r.
+ destruct (zlt i0_val Int.half_modulus) as [Hlt_half | Hge_half].
+ {
+ replace Int.half_modulus with 2147483648 in * by reflexivity.
+ rewrite Int64.unsigned_repr by (change Int64.max_unsigned with 18446744073709551615; lia).
+ destruct (zeq _ _) as [ | Hneq0]; try lia. clear Hneq0.
+ unfold Val.loword.
+ f_equal.
+ unfold Int64.divs, Int.divs, Int64.loword.
+ unfold Int.signed, Int64.signed. cbn.
+ rewrite if_zlt_min_signed_half_modulus.
+ change Int.half_modulus with 2147483648 in *.
+ destruct (zlt _ _) as [discard|]; try lia. clear discard.
+ change (Int64.unsigned
+ (Int64.repr
+ (Int.unsigned (Int.repr Int.min_signed) - Int.modulus)))
+ with 18446744071562067968.
+ change Int64.half_modulus with 9223372036854775808.
+ change Int64.modulus with 18446744073709551616.
+ cbn.
+ rewrite (Int64.unsigned_repr i0_val) by (change Int64.max_unsigned with 18446744073709551615; lia).
+ destruct (zlt i0_val 9223372036854775808) as [discard |]; try lia.
+ clear discard.
+ change (Int.unsigned (Int.repr Int.min_signed) - Int.modulus) with (-2147483648).
+ destruct (Z.eq_dec i0_val 1) as [H1 | Hnot1].
+ { subst.
+ rewrite Z.quot_1_r.
+ apply Int.eqm_samerepr.
+ unfold Int.eqm.
+ change (Int64.unsigned (Int64.repr (-2147483648))) with 18446744071562067968.
+ unfold Zbits.eqmod.
+ change Int.modulus with 4294967296.
+ exists (-4294967296).
+ compute.
+ reflexivity.
+ }
+ change (-2147483648) with (-(2147483648)).
+ rewrite Z.quot_opp_l by assumption.
+ rewrite repr_unsigned64_repr.
+ reflexivity.
+ }
+ destruct (zeq _ _) as [Hmod|Hnmod].
+ {
+ rewrite Int64.unsigned_repr_eq in Hmod.
+ set (delta := (i0_val - Int.modulus)) in *.
+ assert (delta = Int64.modulus*(delta/Int64.modulus)) as Hdelta.
+ { apply Z_div_exact_full_2.
+ compute. lia.
+ assumption. }
+ set (k := (delta / Int64.modulus)) in *.
+ change Int64.modulus with 18446744073709551616 in *.
+ change Int.modulus with 4294967296 in *.
+ change Int.half_modulus with 2147483648 in *.
+ change (Int.unsigned Int.mone) with 4294967295 in *.
+ lia.
+ }
+ unfold Int.divs, Int64.divs, Val.loword, Int64.loword.
+ change (Int.unsigned (Int.repr Int.min_signed)) with 2147483648.
+ change Int.modulus with 4294967296.
+ change (Int64.signed (Int64.repr (2147483648 - 4294967296))) with (-2147483648).
+ f_equal.
+ change (Int.signed {| Int.intval := 2147483648; Int.intrange := i_range |})
+ with (-2147483648).
+ rewrite Int64.signed_repr.
+ {
+ replace (Int.signed {| Int.intval := i0_val; Int.intrange := i0_range |}) with (i0_val - 4294967296).
+ { rewrite repr_unsigned64_repr.
+ reflexivity.
+ }
+ *)
+
+Lemma big_unsigned_signed:
+ forall x,
+ (Int.unsigned x >= Int.half_modulus) ->
+ (Int.signed x) = (Int.unsigned x) - Int.modulus.
+Proof.
+ destruct x as [xval xrange].
+ intro BIG.
+ unfold Int.signed, Int.unsigned in *. cbn in *.
+ destruct (zlt _ _).
+ lia.
+ trivial.
+Qed.
+
+(*
+Lemma signed_0_eqb :
+ forall x, (Z.eqb (Int.signed x) 0) = Int.eq x Int.zero.
+Qed.
+ *)
+
+Lemma Z_quot_le: forall a b,
+ 0 <= a -> 1 <= b -> Z.quot a b <= a.
+Proof.
+ intros a b Ha Hb.
+ destruct (Z.eq_dec b 1) as [Hb1 | Hb1].
+ { (* b=1 *)
+ subst.
+ rewrite Z.quot_1_r.
+ auto with zarith.
+ }
+ destruct (Z.eq_dec a 0) as [Ha0 | Ha0].
+ { (* a=0 *)
+ subst.
+ rewrite Z.quot_0_l.
+ auto with zarith.
+ lia.
+ }
+ assert ((Z.quot a b) < a).
+ {
+ apply Z.quot_lt; lia.
+ }
+ auto with zarith.
+Qed.
+
+(*
+Lemma divs_is_quot: forall v1 v2 : val,
+ Val.divs v1 v2 =
+ match v1, v2 with
+ | (Vint w1), (Vint w2) =>
+ let q := Z.quot (Int.signed w1) (Int.signed w2) in
+ if (negb (Z.eqb (Int.signed w2) 0))
+ && (Z.geb q Int.min_signed) && (Z.leb q Int.max_signed)
+ then Some (Vint (Int.repr q))
+ else None
+ | _, _ => None
+ end.
+
+Proof.
+ destruct v1; destruct v2; cbn; trivial.
+ unfold Int.divs.
+ rewrite signed_0_eqb.
+ destruct (Int.eq i0 Int.zero) eqn:Eeq0; cbn; trivial.
+ destruct (Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone) eqn:EXCEPTION.
+ { replace (Int.signed i0) with (-1).
+ replace (Int.signed i) with Int.min_signed.
+ change Int.min_signed with (-2147483648).
+ change Int.max_signed with (2147483647).
+ compute.
+ reflexivity.
+ { unfold Int.eq in EXCEPTION.
+ destruct (zeq _ _) as [Hmin | ] in EXCEPTION; try discriminate.
+ change Int.min_signed with (-2147483648).
+ change (Int.unsigned (Int.repr Int.min_signed)) with (2147483648) in *.
+ rewrite big_unsigned_signed.
+ change Int.modulus with 4294967296.
+ lia.
+ change Int.half_modulus with 2147483648.
+ lia.
+ }
+ unfold Int.eq in EXCEPTION.
+ destruct (zeq _ _) in EXCEPTION; try discriminate.
+ destruct (zeq _ _) as [Hmone | ] in EXCEPTION; try discriminate.
+ destruct i0 as [i0val i0range]; unfold Int.signed in *; cbn in *.
+ rewrite Hmone.
+ reflexivity.
+ }
+ replace (Int.signed i ÷ Int.signed i0 >=? Int.min_signed) with true.
+ replace (Int.signed i ÷ Int.signed i0 <=? Int.max_signed) with true.
+ reflexivity.
+ { assert (Int.signed i ÷ Int.signed i0 <= Int.max_signed).
+ {
+ destruct (Z_lt_le_dec (Int.signed i) 0).
+ {
+ apply Z.le_trans with (m:=0).
+ rewrite <- (Z.quot_0_l (Int.signed i0)).
+ Require Import Coq.ZArith.Zquot.
+ apply Z_quot_monotone.
+ }
+ assert ( Int.signed i ÷ Int.signed i0 <= Int.signed i).
+ apply Z_quot_le.
+ }
+ }
+
+ *)
+
+Require Import Coq.ZArith.Zquot.
+Lemma Z_quot_pos_pos_bound: forall a b m,
+ 0 <= a <= m -> 1 <= b -> 0 <= Z.quot a b <= m.
+Proof.
+ intros.
+ split.
+ { rewrite <- (Z.quot_0_l b) by lia.
+ apply Z_quot_monotone; lia.
+ }
+ apply Z.le_trans with (m := a).
+ {
+ apply Z_quot_le; tauto.
+ }
+ tauto.
+Qed.
+Lemma Z_quot_neg_pos_bound: forall a b m,
+ m <= a <= 0 -> 1 <= b -> m <= Z.quot a b <= 0.
+ intros.
+ assert (0 <= - (a ÷ b) <= -m).
+ {
+ rewrite <- Z.quot_opp_l by lia.
+ apply Z_quot_pos_pos_bound; lia.
+ }
+ lia.
+Qed.
+
+Lemma Z_quot_signed_pos_bound: forall a b,
+ Int.min_signed <= a <= Int.max_signed -> 1 <= b ->
+ Int.min_signed <= Z.quot a b <= Int.max_signed.
+Proof.
+ intros.
+ destruct (Z_lt_ge_dec a 0).
+ {
+ split.
+ { apply Z_quot_neg_pos_bound; lia. }
+ { eapply Z.le_trans with (m := 0).
+ { apply Z_quot_neg_pos_bound with (m := Int.min_signed); trivial.
+ split. tauto. auto with zarith.
+ }
+ discriminate.
+ }
+ }
+ { split.
+ { eapply Z.le_trans with (m := 0).
+ discriminate.
+ apply Z_quot_pos_pos_bound with (m := Int.max_signed); trivial.
+ split. lia. tauto.
+ }
+ { apply Z_quot_pos_pos_bound; lia.
+ }
+ }
+Qed.
+
+Lemma Z_quot_signed_neg_bound: forall a b,
+ Int.min_signed <= a <= Int.max_signed -> b < -1 ->
+ Int.min_signed <= Z.quot a b <= Int.max_signed.
+Proof.
+ change Int.min_signed with (-2147483648).
+ change Int.max_signed with 2147483647.
+ intros.
+
+ replace b with (-(-b)) by auto with zarith.
+ rewrite Z.quot_opp_r by lia.
+ assert (-2147483647 <= (a ÷ - b) <= 2147483648).
+ 2: lia.
+
+ destruct (Z_lt_ge_dec a 0).
+ {
+ replace a with (-(-a)) by auto with zarith.
+ rewrite Z.quot_opp_l by lia.
+ assert (-2147483648 <= - a ÷ - b <= 2147483647).
+ 2: lia.
+ split.
+ {
+ rewrite Z.quot_opp_l by lia.
+ assert (a ÷ - b <= 2147483648).
+ 2: lia.
+ {
+ apply Z.le_trans with (m := 0).
+ rewrite <- (Z.quot_0_l (-b)) by lia.
+ apply Z_quot_monotone; lia.
+ discriminate.
+ }
+ }
+ assert (- a ÷ - b < -a ).
+ 2: lia.
+ apply Z_quot_lt; lia.
+ }
+ {
+ split.
+ { apply Z.le_trans with (m := 0).
+ discriminate.
+ rewrite <- (Z.quot_0_l (-b)) by lia.
+ apply Z_quot_monotone; lia.
+ }
+ { apply Z.le_trans with (m := a).
+ apply Z_quot_le.
+ all: lia.
+ }
+ }
+Qed.
+
+Lemma sub_add_neg :
+ forall x y, Val.sub x y = Val.add x (Val.neg y).
+Proof.
+ destruct x; destruct y; cbn; trivial.
+ f_equal.
+ apply Int.sub_add_opp.
+Qed.
+
+Lemma neg_mul_distr_r :
+ forall x y, Val.neg (Val.mul x y) = Val.mul x (Val.neg y).
+Proof.
+ destruct x; destruct y; cbn; trivial.
+ f_equal.
+ apply Int.neg_mul_distr_r.
+Qed.
+
+(* pointer diff
+Lemma sub_addl_negl :
+ forall x y, Val.subl x y = Val.addl x (Val.negl y).
+Proof.
+ destruct x; destruct y; cbn; trivial.
+ + f_equal. apply Int64.sub_add_opp.
+ + destruct (Archi.ptr64) eqn:ARCHI64; trivial.
+ f_equal. rewrite Ptrofs.sub_add_opp.
+ pose (Ptrofs.agree64_neg ARCHI64 (Ptrofs.of_int64 i0) i0) as Hagree.
+ unfold Ptrofs.agree64 in Hagree.
+ unfold Ptrofs.add.
+ f_equal. f_equal.
+ rewrite Hagree.
+ pose (Ptrofs.agree64_of_int ARCHI64 (Int64.neg i0)) as Hagree2.
+ rewrite Hagree2.
+ reflexivity.
+ exact (Ptrofs.agree64_of_int ARCHI64 i0).
+ + destruct (Archi.ptr64) eqn:ARCHI64; cbn; trivial.
+ destruct (eq_block _ _); cbn; trivial.
+Qed.
+ *)
+
+Lemma negl_mull_distr_r :
+ forall x y, Val.negl (Val.mull x y) = Val.mull x (Val.negl y).
+Proof.
+ destruct x; destruct y; cbn; trivial.
+ f_equal.
+ apply Int64.neg_mul_distr_r.
+Qed.
+
+Definition addx sh v1 v2 :=
+ Val.add v2 (Val.shl v1 (Vint sh)).
+
+Definition addxl sh v1 v2 :=
+ Val.addl v2 (Val.shll v1 (Vint sh)).
+
+Definition revsubx sh v1 v2 :=
+ Val.sub v2 (Val.shl v1 (Vint sh)).
+
+Definition revsubxl sh v1 v2 :=
+ Val.subl v2 (Val.shll v1 (Vint sh)).
+
+Definition minf v1 v2 :=
+ match v1, v2 with
+ | (Vfloat f1), (Vfloat f2) => Vfloat (ExtFloat.min f1 f2)
+ | _, _ => Vundef
+ end.
+
+Definition maxf v1 v2 :=
+ match v1, v2 with
+ | (Vfloat f1), (Vfloat f2) => Vfloat (ExtFloat.max f1 f2)
+ | _, _ => Vundef
+ end.
+
+Definition minfs v1 v2 :=
+ match v1, v2 with
+ | (Vsingle f1), (Vsingle f2) => Vsingle (ExtFloat32.min f1 f2)
+ | _, _ => Vundef
+ end.
+
+Definition maxfs v1 v2 :=
+ match v1, v2 with
+ | (Vsingle f1), (Vsingle f2) => Vsingle (ExtFloat32.max f1 f2)
+ | _, _ => Vundef
+ end.
+
+Definition invfs v1 :=
+ match v1 with
+ | (Vsingle f1) => Vsingle (ExtFloat32.inv f1)
+ | _ => Vundef
+ end.
+
+Definition triple_op_float f v1 v2 v3 :=
+ match v1, v2, v3 with
+ | (Vfloat f1), (Vfloat f2), (Vfloat f3) => Vfloat (f f1 f2 f3)
+ | _, _, _ => Vundef
+ end.
+
+Definition triple_op_single f v1 v2 v3 :=
+ match v1, v2, v3 with
+ | (Vsingle f1), (Vsingle f2), (Vsingle f3) => Vsingle (f f1 f2 f3)
+ | _, _, _ => Vundef
+ end.
+
+Definition fmaddf := triple_op_float (fun f1 f2 f3 => Float.fma f2 f3 f1).
+Definition fmaddfs := triple_op_single (fun f1 f2 f3 => Float32.fma f2 f3 f1).
+
+Definition fmsubf := triple_op_float (fun f1 f2 f3 => Float.fma (Float.neg f2) f3 f1).
+Definition fmsubfs := triple_op_single (fun f1 f2 f3 => Float32.fma (Float32.neg f2) f3 f1).
diff --git a/kvx/Machregs.v b/kvx/Machregs.v
new file mode 100644
index 00000000..02fa4e6b
--- /dev/null
+++ b/kvx/Machregs.v
@@ -0,0 +1,245 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import String.
+Require Import Coqlib.
+Require Import Decidableplus.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Op.
+
+(** ** Machine registers *)
+
+(** The following type defines the machine registers that can be referenced
+ as locations. These include:
+- Integer registers that can be allocated to RTL pseudo-registers ([Rxx]).
+- Floating-point registers that can be allocated to RTL pseudo-registers
+ ([Fxx]).
+
+ The type [mreg] does not include reserved machine registers such as
+ the zero register (x0), the link register (x1), the stack pointer
+ (x2), the global pointer (x3), and the thread pointer (x4).
+ Finally, register x31 is reserved for use as a temporary by the
+ assembly-code generator [Asmgen].
+*)
+
+Inductive mreg: Type :=
+ (* Allocatable General Purpose regs. *)
+ | R0 | R1 | R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9
+ | R10 | R11 (* | R12 | R13 | R14 *) | R15 (* | R16 *) | R17 | R18 | R19
+ | R20 | R21 | R22 | R23 | R24 | R25 | R26 | R27 | R28 | R29
+ | R30 | R31 (* | R32 *) | R33 | R34 | R35 | R36 | R37 | R38 | R39
+ | R40 | R41 | R42 | R43 | R44 | R45 | R46 | R47 | R48 | R49
+ | R50 | R51 | R52 | R53 | R54 | R55 | R56 | R57 | R58 | R59
+ | R60 | R61 | R62 | R63.
+
+Lemma mreg_eq: forall (r1 r2: mreg), {r1 = r2} + {r1 <> r2}.
+Proof. decide equality. Defined.
+Global Opaque mreg_eq.
+
+Definition all_mregs :=
+ R0 :: R1 :: R2 :: R3 :: R4 :: R5 :: R6 :: R7 :: R8 :: R9
+ :: R10 :: R11 (* :: R12 :: R13 :: R14 *) :: R15 (* :: R16 *) :: R17 :: R18 :: R19
+ :: R20 :: R21 :: R22 :: R23 :: R24 :: R25 :: R26 :: R27 :: R28 :: R29
+ :: R30 :: R31 (* :: R32 *) :: R33 :: R34 :: R35 :: R36 :: R37 :: R38 :: R39
+ :: R40 :: R41 :: R42 :: R43 :: R44 :: R45 :: R46 :: R47 :: R48 :: R49
+ :: R50 :: R51 :: R52 :: R53 :: R54 :: R55 :: R56 :: R57 :: R58 :: R59
+ :: R60 :: R61 :: R62 :: R63 :: nil.
+
+Lemma all_mregs_complete:
+ forall (r: mreg), In r all_mregs.
+Proof.
+ assert (forall r, proj_sumbool (In_dec mreg_eq r all_mregs) = true) by (destruct r; reflexivity).
+ intros. specialize (H r). InvBooleans. auto.
+Qed.
+
+Instance Decidable_eq_mreg : forall (x y: mreg), Decidable (eq x y) := Decidable_eq mreg_eq.
+
+Instance Finite_mreg : Finite mreg := {
+ Finite_elements := all_mregs;
+ Finite_elements_spec := all_mregs_complete
+}.
+
+Definition mreg_type (r: mreg): typ := Tany64.
+
+Open Scope positive_scope.
+
+Module IndexedMreg <: INDEXED_TYPE.
+ Definition t := mreg.
+ Definition eq := mreg_eq.
+ Definition index (r: mreg): positive :=
+ match r with
+ | R0 => 1 | R1 => 2 | R2 => 3 | R3 => 4 | R4 => 5
+ | R5 => 6 | R6 => 7 | R7 => 8 | R8 => 9 | R9 => 10
+ | R10 => 11 | R11 => 12 (* | R12 => 13 | R13 => 14 | R14 => 15 *)
+ | R15 => 16 (* | R16 => 17 *) | R17 => 18 | R18 => 19 | R19 => 20
+ | R20 => 21 | R21 => 22 | R22 => 23 | R23 => 24 | R24 => 25
+ | R25 => 26 | R26 => 27 | R27 => 28 | R28 => 29 | R29 => 30
+ | R30 => 31 | R31 => 32 (* | R32 => 33 *) | R33 => 34 | R34 => 35
+ | R35 => 36 | R36 => 37 | R37 => 38 | R38 => 39 | R39 => 40
+ | R40 => 41 | R41 => 42 | R42 => 43 | R43 => 44 | R44 => 45
+ | R45 => 46 | R46 => 47 | R47 => 48 | R48 => 49 | R49 => 50
+ | R50 => 51 | R51 => 52 | R52 => 53 | R53 => 54 | R54 => 55
+ | R55 => 56 | R56 => 57 | R57 => 58 | R58 => 59 | R59 => 60
+ | R60 => 61 | R61 => 62 | R62 => 63 | R63 => 64
+ end.
+
+ Lemma index_inj:
+ forall r1 r2, index r1 = index r2 -> r1 = r2.
+ Proof.
+ decide_goal.
+ Qed.
+End IndexedMreg.
+
+Definition is_stack_reg (r: mreg) : bool := false.
+
+(** ** Names of registers *)
+
+Local Open Scope string_scope.
+
+Definition register_names :=
+ ("R0" , R0) :: ("R1" , R1) :: ("R2" , R2) :: ("R3" , R3) :: ("R4" , R4)
+ :: ("R5" , R5) :: ("R6" , R6) :: ("R7" , R7) :: ("R8" , R8) :: ("R9" , R9)
+ :: ("R10", R10) :: ("R11", R11) (* :: ("R12", R12) :: ("R13", R13) :: ("R14", R14) *)
+ :: ("R15", R15) (* :: ("R16", R16) *) :: ("R17", R17) :: ("R18", R18) :: ("R19", R19)
+ :: ("R20", R20) :: ("R21", R21) :: ("R22", R22) :: ("R23", R23) :: ("R24", R24)
+ :: ("R25", R25) :: ("R26", R26) :: ("R27", R27) :: ("R28", R28) :: ("R29", R29)
+ :: ("R30", R30) :: ("R31", R31) (* :: ("R32", R32) *) :: ("R33", R33) :: ("R34", R34)
+ :: ("R35", R35) :: ("R36", R36) :: ("R37", R37) :: ("R38", R38) :: ("R39", R39)
+ :: ("R40", R40) :: ("R41", R41) :: ("R42", R42) :: ("R43", R43) :: ("R44", R44)
+ :: ("R45", R45) :: ("R46", R46) :: ("R47", R47) :: ("R48", R48) :: ("R49", R49)
+ :: ("R50", R50) :: ("R51", R51) :: ("R52", R52) :: ("R53", R53) :: ("R54", R54)
+ :: ("R55", R55) :: ("R56", R56) :: ("R57", R57) :: ("R58", R58) :: ("R59", R59)
+ :: ("R60", R60) :: ("R61", R61) :: ("R62", R62) :: ("R63", R63) :: nil.
+
+Definition register_by_name (s: string) : option mreg :=
+ let fix assoc (l: list (string * mreg)) : option mreg :=
+ match l with
+ | nil => None
+ | (s1, r1) :: l' => if string_dec s s1 then Some r1 else assoc l'
+ end
+ in assoc register_names.
+
+(** ** Destroyed registers, preferred registers *)
+
+Definition destroyed_by_op (op: operation): list mreg := nil.
+(*match op with
+ | Ointoffloat | Ointuoffloat | Ointofsingle | Ointuofsingle
+ | Olongoffloat | Olonguoffloat | Olongofsingle | Olonguofsingle
+ => F6 :: nil
+ | _ => nil
+ end.
+*)
+
+Definition destroyed_by_load (chunk: memory_chunk) (addr: addressing): list mreg := nil.
+
+Definition destroyed_by_store (chunk: memory_chunk) (addr: addressing): list mreg := nil.
+
+Definition destroyed_by_cond (cond: condition): list mreg := nil.
+
+Definition destroyed_by_jumptable: list mreg := R62 :: R63 :: nil.
+
+Fixpoint destroyed_by_clobber (cl: list string): list mreg :=
+ match cl with
+ | nil => nil
+ | c1 :: cl =>
+ match register_by_name c1 with
+ | Some r => r :: destroyed_by_clobber cl
+ | None => destroyed_by_clobber cl
+ end
+ end.
+
+Definition destroyed_by_builtin (ef: external_function): list mreg :=
+ match ef with
+ | EF_inline_asm txt sg clob => destroyed_by_clobber clob
+ | EF_memcpy sz al =>
+ if Z.leb sz 15
+ then R62 :: R63 :: R61 :: nil
+ else R62 :: R63 :: R61 :: R60 :: nil
+ | EF_profiling _ _ => R62 :: R63 ::nil
+ | _ => nil
+ end.
+
+Definition destroyed_by_setstack (ty: typ): list mreg := nil.
+
+Definition destroyed_at_function_entry: list mreg := R17 :: nil.
+
+Definition temp_for_parent_frame: mreg := R17. (* Temporary used to store the parent frame, where the arguments are *)
+
+Definition destroyed_at_indirect_call: list mreg := nil.
+ (* R10 :: R11 :: R12 :: R13 :: R14 :: R15 :: R16 :: R17 :: nil. *)
+
+Definition mregs_for_operation (op: operation): list (option mreg) * option mreg := (nil, None).
+
+(* FIXME DMonniaux this seems to be the place for preferred registers for arguments *)
+Definition mregs_for_builtin (ef: external_function): list (option mreg) * list(option mreg) := (nil, nil).
+
+ (* match ef with
+ | EF_builtin name sg =>
+ if (negb Archi.ptr64) && string_dec name "__builtin_bswap64" then
+ (Some R6 :: Some R5 :: nil, Some R5 :: Some R6 :: nil)
+ else
+ (nil, nil)
+ | _ =>
+ (nil, nil)
+ end. *)
+
+Global Opaque
+ destroyed_by_op destroyed_by_load destroyed_by_store
+ destroyed_by_cond destroyed_by_jumptable destroyed_by_builtin
+ destroyed_by_setstack destroyed_at_function_entry temp_for_parent_frame
+ mregs_for_operation mregs_for_builtin.
+
+(** Two-address operations. Return [true] if the first argument and
+ the result must be in the same location *and* are unconstrained
+ by [mregs_for_operation]. There are two: the pseudo [Ocast32signed],
+ because it expands to a no-op owing to the representation of 32-bit
+ integers as their 64-bit sign extension; and [Ocast32unsigned],
+ because it builds on the same magic no-op. *)
+
+Definition two_address_op (op: operation) : bool :=
+ match op with
+ | Ofmaddf | Ofmaddfs
+ | Ofmsubf | Ofmsubfs
+ | Omadd | Omaddimm _
+ | Omaddl | Omaddlimm _
+ | Omsub | Omsubl
+ | Osel _ _ | Oselimm _ _ | Osellimm _ _
+ | Oinsf _ _ | Oinsfl _ _ => true
+ | _ => false
+ end.
+
+(** Constraints on constant propagation for builtins *)
+
+Definition builtin_constraints (ef: external_function) :
+ list builtin_arg_constraint :=
+ match ef with
+ | EF_builtin id sg =>
+ if string_dec id "__builtin_kvx_get" then OK_const :: nil
+ else if string_dec id "__builtin_kvx_set"
+ then OK_const :: OK_default :: nil
+ else if string_dec id "__builtin_kvx_wfxl"
+ then OK_const :: OK_default :: nil
+ else if string_dec id "__builtin_kvx_wfxm"
+ then OK_const :: OK_default :: nil
+ else nil
+ | EF_vload _ => OK_addressing :: nil
+ | EF_vstore _ => OK_addressing :: OK_default :: nil
+ | EF_memcpy _ _ => OK_addrstack :: OK_addrstack :: nil
+ | EF_annot kind txt targs => map (fun _ => OK_all) targs
+ | EF_debug kind txt targs => map (fun _ => OK_all) targs
+ | _ => nil
+ end.
diff --git a/kvx/Machregsaux.ml b/kvx/Machregsaux.ml
new file mode 100644
index 00000000..dbb89727
--- /dev/null
+++ b/kvx/Machregsaux.ml
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Auxiliary functions on machine registers *)
+
+open Camlcoq
+open Machregs
+
+let register_names : (mreg, string) Hashtbl.t = Hashtbl.create 31
+
+let _ =
+ List.iter
+ (fun (s, r) -> Hashtbl.add register_names r (camlstring_of_coqstring s))
+ Machregs.register_names
+
+let is_scratch_register r = false
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong
+ | AST.Tfloat | AST.Tsingle -> 0
+ | AST.Tany32 | AST.Tany64 -> assert false
+
+let nr_regs = [| 59 |]
diff --git a/kvx/Machregsaux.mli b/kvx/Machregsaux.mli
new file mode 100644
index 00000000..23ac1c9a
--- /dev/null
+++ b/kvx/Machregsaux.mli
@@ -0,0 +1,20 @@
+(* *********************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Xavier Leroy, INRIA Paris-Rocquencourt *)
+(* *)
+(* Copyright Institut National de Recherche en Informatique et en *)
+(* Automatique. All rights reserved. This file is distributed *)
+(* under the terms of the INRIA Non-Commercial License Agreement. *)
+(* *)
+(* *********************************************************************)
+
+(** Auxiliary functions on machine registers *)
+
+val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
+
+(* Number of registers in each class *)
+val nr_regs : int array
diff --git a/kvx/NeedOp.v b/kvx/NeedOp.v
new file mode 100644
index 00000000..4578b4e8
--- /dev/null
+++ b/kvx/NeedOp.v
@@ -0,0 +1,415 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs.
+Require Import Op RTL.
+Require Import NeedDomain.
+Require Import Lia.
+
+(** Neededness analysis for RISC-V operators *)
+
+Definition op1 (nv: nval) := nv :: nil.
+Definition op2 (nv: nval) := nv :: nv :: nil.
+Definition op3 (nv: nval) := nv :: nv :: nv :: nil.
+
+Definition needs_of_condition (cond: condition): list nval := nil.
+Definition needs_of_condition0 (cond0: condition0): list nval := nil.
+
+Definition needs_of_operation (op: operation) (nv: nval): list nval :=
+ match op with
+ | Omove => op1 nv
+ | Ointconst n => nil
+ | Olongconst n => nil
+ | Ofloatconst n => nil
+ | Osingleconst n => nil
+ | Oaddrsymbol id ofs => nil
+ | Oaddrstack ofs => nil
+ | Ocast8signed => op1 (sign_ext 8 nv)
+ | Ocast16signed => op1 (sign_ext 16 nv)
+ | Oadd => op2 (modarith nv)
+ | Oaddimm n => op1 (modarith nv)
+ | Oaddx _ => op2 (default nv)
+ | Oaddximm _ _ => op1 (default nv)
+ | Oneg => op1 (modarith nv)
+ | Osub => op2 (default nv)
+ | Orevsubimm _ => op1 (default nv)
+ | Orevsubx _ => op2 (default nv)
+ | Orevsubximm _ _ => op1 (default nv)
+ | Omul => op2 (modarith nv)
+ | Omulimm _ => op1 (modarith nv)
+ | Omulhs | Omulhu | Odiv | Odivu | Omod | Omodu => op2 (default nv)
+ | Oand => op2 (bitwise nv)
+ | Oandimm n => op1 (andimm nv n)
+ | Onand => op2 (bitwise nv)
+ | Onandimm n => op1 (andimm nv n)
+ | Oor => op2 (bitwise nv)
+ | Oorimm n => op1 (orimm nv n)
+ | Onor => op2 (bitwise nv)
+ | Onorimm n => op1 (orimm nv n)
+ | Oxor => op2 (bitwise nv)
+ | Oxorimm n => op1 (bitwise nv)
+ | Onxor => op2 (bitwise nv)
+ | Onxorimm n => op1 (bitwise nv)
+ | Onot => op1 (bitwise nv)
+ | Oandn => op2 (bitwise nv)
+ | Oandnimm n => op1 (andimm nv n)
+ | Oorn => op2 (bitwise nv)
+ | Oornimm n => op1 (orimm nv n)
+ | Oshl | Oshr | Oshru => op2 (default nv)
+ | Oshlimm n => op1 (shlimm nv n)
+ | Oshrimm n => op1 (shrimm nv n)
+ | Ororimm n => op1 (ror nv n)
+ | Oshruimm n => op1 (shruimm nv n)
+ | Oshrximm n => op1 (default nv)
+ | Omadd => op3 (modarith nv)
+ | Omaddimm n => op2 (modarith nv)
+ | Omsub => op3 (modarith nv)
+ | Omakelong => op2 (default nv)
+ | Olowlong | Ohighlong => op1 (default nv)
+ | Ocast32signed => op1 (default nv)
+ | Ocast32unsigned => op1 (default nv)
+ | Oaddl => op2 (default nv)
+ | Oaddlimm n => op1 (default nv)
+ | Oaddxl _ => op2 (default nv)
+ | Oaddxlimm _ _ => op1 (default nv)
+ | Orevsublimm _ => op1 (default nv)
+ | Orevsubxl _ => op2 (default nv)
+ | Orevsubxlimm _ _ => op1 (default nv)
+ | Onegl => op1 (default nv)
+ | Osubl => op2 (default nv)
+ | Omull => op2 (default nv)
+ | Omullimm _ => op1 (default nv)
+ | Omullhs | Omullhu | Odivl | Odivlu | Omodl | Omodlu => op2 (default nv)
+ | Oandl => op2 (default nv)
+ | Oandlimm n => op1 (default nv)
+ | Onandl => op2 (default nv)
+ | Onandlimm n => op1 (default nv)
+ | Oorl => op2 (default nv)
+ | Oorlimm n => op1 (default nv)
+ | Onorl => op2 (default nv)
+ | Onorlimm n => op1 (default nv)
+ | Oxorl => op2 (default nv)
+ | Oxorlimm n => op1 (default nv)
+ | Onxorl => op2 (default nv)
+ | Onxorlimm n => op1 (default nv)
+ | Onotl => op1 (default nv)
+ | Oandnl => op2 (default nv)
+ | Oandnlimm n => op1 (default nv)
+ | Oornl => op2 (default nv)
+ | Oornlimm n => op1 (default nv)
+ | Oshll | Oshrl | Oshrlu => op2 (default nv)
+ | Oshllimm n => op1 (default nv)
+ | Oshrlimm n => op1 (default nv)
+ | Oshrluimm n => op1 (default nv)
+ | Oshrxlimm n => op1 (default nv)
+ | Omaddl => op3 (default nv)
+ | Omaddlimm n => op2 (default nv)
+ | Omsubl => op3 (default nv)
+ | Onegf | Oabsf => op1 (default nv)
+ | Oaddf | Osubf | Omulf | Odivf | Ominf | Omaxf => op2 (default nv)
+ | Ofmaddf | Ofmsubf => op3 (default nv)
+ | Onegfs | Oabsfs => op1 (default nv)
+ | Oaddfs | Osubfs | Omulfs | Odivfs | Ominfs | Omaxfs => op2 (default nv)
+ | Oinvfs => op1 (default nv)
+ | Ofmaddfs | Ofmsubfs => op3 (default nv)
+ | Ofloatofsingle | Osingleoffloat => op1 (default nv)
+ | Ointoffloat | Ointuoffloat => op1 (default nv)
+ | Olongoffloat | Olonguoffloat | Ofloatoflong | Ofloatoflongu => op1 (default nv)
+ | Ointofsingle | Ointuofsingle | Osingleofint | Osingleofintu => op1 (default nv)
+ | Olongofsingle | Olonguofsingle | Osingleoflong | Osingleoflongu => op1 (default nv)
+ | Ocmp c => needs_of_condition c
+ | Oextfz _ _ | Oextfs _ _ | Oextfzl _ _ | Oextfsl _ _ => op1 (default nv)
+ | Oinsf _ _ | Oinsfl _ _ => op2 (default nv)
+ | Osel c ty => nv :: nv :: needs_of_condition0 c
+ | Oselimm c imm
+ | Osellimm c imm => nv :: needs_of_condition0 c
+ end.
+
+Definition operation_is_redundant (op: operation) (nv: nval): bool :=
+ match op with
+ | Ocast8signed => sign_ext_redundant 8 nv
+ | Ocast16signed => sign_ext_redundant 16 nv
+ | Oandimm n => andimm_redundant nv n
+ | Oorimm n => orimm_redundant nv n
+ | _ => false
+ end.
+
+Ltac InvAgree :=
+ match goal with
+ | [H: vagree_list nil _ _ |- _ ] => inv H; InvAgree
+ | [H: vagree_list (_::_) _ _ |- _ ] => inv H; InvAgree
+ | _ => idtac
+ end.
+
+Ltac TrivialExists :=
+ match goal with
+ | [ |- exists v, Some ?x = Some v /\ _ ] => exists x; split; auto
+ | _ => idtac
+ end.
+
+Section SOUNDNESS.
+
+Variable ge: genv.
+Variable sp: block.
+Variables m1 m2: mem.
+Hypothesis PERM: forall b ofs k p, Mem.perm m1 b ofs k p -> Mem.perm m2 b ofs k p.
+
+Lemma needs_of_condition_sound:
+ forall cond args b args',
+ eval_condition cond args m1 = Some b ->
+ vagree_list args args' (needs_of_condition cond) ->
+ eval_condition cond args' m2 = Some b.
+Proof.
+ intros. unfold needs_of_condition in H0.
+ eapply default_needs_of_condition_sound; eauto.
+Qed.
+
+Let valid_pointer_inj:
+ forall b1 ofs b2 delta,
+ inject_id b1 = Some(b2, delta) ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+Proof.
+ unfold inject_id; intros. inv H. rewrite Ptrofs.add_zero.
+ rewrite Mem.valid_pointer_nonempty_perm in *. eauto.
+Qed.
+
+Let weak_valid_pointer_inj:
+ forall b1 ofs b2 delta,
+ inject_id b1 = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.weak_valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+Proof.
+ unfold inject_id; intros. inv H. rewrite Ptrofs.add_zero.
+ rewrite Mem.weak_valid_pointer_spec in *.
+ rewrite ! Mem.valid_pointer_nonempty_perm in *.
+ destruct H0; [left|right]; eauto.
+Qed.
+
+Let weak_valid_pointer_no_overflow:
+ forall b1 ofs b2 delta,
+ inject_id b1 = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ 0 <= Ptrofs.unsigned ofs + Ptrofs.unsigned (Ptrofs.repr delta) <= Ptrofs.max_unsigned.
+Proof.
+ unfold inject_id; intros. inv H. rewrite Z.add_0_r. apply Ptrofs.unsigned_range_2.
+Qed.
+
+Let valid_different_pointers_inj:
+ forall b1 ofs1 b2 ofs2 b1' delta1 b2' delta2,
+ b1 <> b2 ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs1) = true ->
+ Mem.valid_pointer m1 b2 (Ptrofs.unsigned ofs2) = true ->
+ inject_id b1 = Some (b1', delta1) ->
+ inject_id b2 = Some (b2', delta2) ->
+ b1' <> b2' \/
+ Ptrofs.unsigned (Ptrofs.add ofs1 (Ptrofs.repr delta1)) <> Ptrofs.unsigned (Ptrofs.add ofs2 (Ptrofs.repr delta2)).
+Proof.
+ unfold inject_id; intros. left; congruence.
+Qed.
+
+Lemma needs_of_condition0_sound:
+ forall cond arg1 b arg2,
+ eval_condition0 cond arg1 m1 = Some b ->
+ vagree arg1 arg2 All ->
+ eval_condition0 cond arg2 m2 = Some b.
+Proof.
+ intros until arg2.
+ intros Hcond Hagree.
+ apply eval_condition0_inj with (f := inject_id) (m1 := m1) (v1 := arg1); cbn; auto.
+ apply val_inject_lessdef. apply lessdef_vagree. assumption.
+Qed.
+
+Lemma addl_sound:
+ forall v1 w1 v2 w2 x,
+ vagree v1 w1 (default x) -> vagree v2 w2 (default x) ->
+ vagree (Val.addl v1 v2) (Val.addl w1 w2) x.
+Proof.
+ unfold default; intros.
+ destruct x; cbn in *; trivial.
+ - unfold Val.addl.
+ destruct v1; destruct v2; trivial; destruct Archi.ptr64; trivial.
+ - apply Val.addl_lessdef; trivial.
+Qed.
+
+Lemma subl_lessdef:
+ forall v1 v1' v2 v2',
+ Val.lessdef v1 v1' -> Val.lessdef v2 v2' -> Val.lessdef (Val.subl v1 v2) (Val.subl v1' v2').
+Proof.
+ intros. inv H. inv H0. auto. destruct v1'; cbn; auto. cbn; auto.
+Qed.
+
+Lemma subl_sound:
+ forall v1 w1 v2 w2 x,
+ vagree v1 w1 (default x) -> vagree v2 w2 (default x) ->
+ vagree (Val.subl v1 v2) (Val.subl w1 w2) x.
+Proof.
+ unfold default; intros.
+ destruct x; cbn in *; trivial.
+ - unfold Val.subl.
+ destruct v1; destruct v2; trivial; destruct Archi.ptr64; cbn; trivial.
+ destruct (eq_block _ _) ; cbn; trivial.
+ - apply subl_lessdef; trivial.
+Qed.
+
+
+Lemma mull_sound:
+ forall v1 w1 v2 w2 x,
+ vagree v1 w1 (default x) -> vagree v2 w2 (default x) ->
+ vagree (Val.mull v1 v2) (Val.mull w1 w2) x.
+Proof.
+ unfold default; intros.
+ destruct x; cbn in *; trivial.
+ - unfold Val.mull.
+ destruct v1; destruct v2; trivial.
+ - unfold Val.mull.
+ destruct v1; destruct v2; trivial.
+ inv H. inv H0.
+ trivial.
+Qed.
+
+
+Remark default_idem: forall nv, default (default nv) = default nv.
+Proof.
+ destruct nv; cbn; trivial.
+Qed.
+
+Lemma vagree_triple_op_float :
+ forall f a b c x y z nv,
+ (vagree a x (default nv)) ->
+ (vagree b y (default nv)) ->
+ (vagree c z (default nv)) ->
+ (vagree (ExtValues.triple_op_float f a b c)
+ (ExtValues.triple_op_float f x y z) nv).
+Proof.
+ induction nv;
+ intros Hax Hby Hcz.
+ - trivial.
+ - cbn in *. destruct a; cbn; trivial.
+ destruct b; cbn; trivial.
+ destruct c; cbn; trivial.
+ - cbn in *. destruct a; cbn; trivial.
+ destruct b; cbn; trivial.
+ destruct c; cbn; trivial.
+ inv Hax. inv Hby. inv Hcz.
+ cbn.
+ constructor.
+Qed.
+
+Lemma vagree_triple_op_single :
+ forall f a b c x y z nv,
+ (vagree a x (default nv)) ->
+ (vagree b y (default nv)) ->
+ (vagree c z (default nv)) ->
+ (vagree (ExtValues.triple_op_single f a b c)
+ (ExtValues.triple_op_single f x y z) nv).
+Proof.
+ induction nv;
+ intros Hax Hby Hcz.
+ - trivial.
+ - cbn in *. destruct a; cbn; trivial.
+ destruct b; cbn; trivial.
+ destruct c; cbn; trivial.
+ - cbn in *. destruct a; cbn; trivial.
+ destruct b; cbn; trivial.
+ destruct c; cbn; trivial.
+ inv Hax. inv Hby. inv Hcz.
+ cbn.
+ constructor.
+Qed.
+
+Hint Resolve vagree_triple_op_float vagree_triple_op_single : na.
+
+Lemma needs_of_operation_sound:
+ forall op args v nv args',
+ eval_operation ge (Vptr sp Ptrofs.zero) op args m1 = Some v ->
+ vagree_list args args' (needs_of_operation op nv) ->
+ nv <> Nothing ->
+ exists v',
+ eval_operation ge (Vptr sp Ptrofs.zero) op args' m2 = Some v'
+ /\ vagree v v' nv.
+Proof.
+ unfold needs_of_operation; intros; destruct op; try (eapply default_needs_of_operation_sound; eauto; fail);
+ cbn in *; FuncInv; InvAgree; TrivialExists.
+- apply sign_ext_sound; auto. compute; auto.
+- apply sign_ext_sound; auto. compute; auto.
+- apply add_sound; auto.
+- apply add_sound; auto with na.
+- apply neg_sound; auto.
+- apply mul_sound; auto.
+- apply mul_sound; auto with na.
+- apply and_sound; auto.
+- apply andimm_sound; auto.
+- apply notint_sound; apply and_sound; auto.
+- apply notint_sound; apply andimm_sound; auto.
+- apply or_sound; auto.
+- apply orimm_sound; auto.
+- apply notint_sound; apply or_sound; auto.
+- apply notint_sound; apply orimm_sound; auto.
+- apply xor_sound; auto.
+- apply xor_sound; auto with na.
+- apply notint_sound; apply xor_sound; auto.
+- apply notint_sound; apply xor_sound; auto with na.
+- apply notint_sound; auto.
+- apply and_sound; try apply notint_sound; auto with na.
+- apply andimm_sound; try apply notint_sound; auto with na.
+- apply or_sound; try apply notint_sound; auto with na.
+- apply orimm_sound; try apply notint_sound; auto with na.
+- apply shlimm_sound; auto.
+- apply shrimm_sound; auto.
+- apply shruimm_sound; auto.
+- apply ror_sound; auto.
+ (* madd *)
+- apply add_sound; try apply mul_sound; auto with na; rewrite modarith_idem; assumption.
+- apply add_sound; try apply mul_sound; auto with na; rewrite modarith_idem; assumption.
+- repeat rewrite ExtValues.sub_add_neg.
+ apply add_sound; trivial.
+ apply neg_sound; trivial.
+ rewrite modarith_idem.
+ apply mul_sound;
+ rewrite modarith_idem; trivial.
+- destruct (eval_condition0 _ _ _) as [b|] eqn:EC.
+ erewrite needs_of_condition0_sound by eauto.
+ apply select_sound; auto.
+ cbn; auto with na.
+ (* select imm *)
+- destruct (eval_condition0 _ _ _) as [b|] eqn:EC.
+ { erewrite needs_of_condition0_sound by eauto.
+ apply select_sound; auto with na. }
+ cbn; auto with na.
+ (* select long imm *)
+- destruct (eval_condition0 _ _ _) as [b|] eqn:EC.
+ { erewrite needs_of_condition0_sound by eauto.
+ apply select_sound; auto with na. }
+ cbn; auto with na.
+Qed.
+
+Lemma operation_is_redundant_sound:
+ forall op nv arg1 args v arg1' args',
+ operation_is_redundant op nv = true ->
+ eval_operation ge (Vptr sp Ptrofs.zero) op (arg1 :: args) m1 = Some v ->
+ vagree_list (arg1 :: args) (arg1' :: args') (needs_of_operation op nv) ->
+ vagree v arg1' nv.
+Proof.
+ intros. destruct op; cbn in *; try discriminate; inv H1; FuncInv; subst.
+- apply sign_ext_redundant_sound; auto. lia.
+- apply sign_ext_redundant_sound; auto. lia.
+- apply andimm_redundant_sound; auto.
+- apply orimm_redundant_sound; auto.
+Qed.
+
+End SOUNDNESS.
diff --git a/kvx/Op.v b/kvx/Op.v
new file mode 100644
index 00000000..4458adb3
--- /dev/null
+++ b/kvx/Op.v
@@ -0,0 +1,2014 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Operators and addressing modes. The abstract syntax and dynamic
+ semantics for the CminorSel, RTL, LTL and Mach languages depend on the
+ following types, defined in this library:
+- [condition]: boolean conditions for conditional branches;
+- [operation]: arithmetic and logical operations;
+- [addressing]: addressing modes for load and store operations.
+
+ These types are processor-specific and correspond roughly to what the
+ processor can compute in one instruction. In other terms, these
+ types reflect the state of the program after instruction selection.
+ For a processor-independent set of operations, see the abstract
+ syntax and dynamic semantics of the Cminor language.
+*)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values ExtValues Memory Globalenvs Events.
+
+Set Implicit Arguments.
+
+(** Conditions (boolean-valued operators). *)
+
+Inductive condition : Type :=
+ | Ccomp (c: comparison) (**r signed integer comparison *)
+ | Ccompu (c: comparison) (**r unsigned integer comparison *)
+ | Ccompimm (c: comparison) (n: int) (**r signed integer comparison with a constant *)
+ | Ccompuimm (c: comparison) (n: int) (**r unsigned integer comparison with a constant *)
+ | Ccompl (c: comparison) (**r signed 64-bit integer comparison *)
+ | Ccomplu (c: comparison) (**r unsigned 64-bit integer comparison *)
+ | Ccomplimm (c: comparison) (n: int64) (**r signed 64-bit integer comparison with a constant *)
+ | Ccompluimm (c: comparison) (n: int64) (**r unsigned 64-bit integer comparison with a constant *)
+ | Ccompf (c: comparison) (**r 64-bit floating-point comparison *)
+ | Cnotcompf (c: comparison) (**r negation of a floating-point comparison *)
+ | Ccompfs (c: comparison) (**r 32-bit floating-point comparison *)
+ | Cnotcompfs (c: comparison). (**r negation of a floating-point comparison *)
+
+Inductive condition0 : Type :=
+ | Ccomp0 (c: comparison) (**r signed integer comparison with 0 *)
+ | Ccompu0 (c: comparison) (**r unsigned integer comparison with 0 *)
+ | Ccompl0 (c: comparison) (**r signed 64-bit integer comparison with 0 *)
+ | Ccomplu0 (c: comparison). (**r unsigned 64-bit integer comparison with 0 *)
+
+Definition arg_type_of_condition0 (cond: condition0) :=
+ match cond with
+ | Ccomp0 _ | Ccompu0 _ => Tint
+ | Ccompl0 _ | Ccomplu0 _ => Tlong
+ end.
+
+(** Arithmetic and logical operations. In the descriptions, [rd] is the
+ result of the operation and [r1], [r2], etc, are the arguments. *)
+
+Inductive operation : Type :=
+ | Omove (**r [rd = r1] *)
+ | Ointconst (n: int) (**r [rd] is set to the given integer constant *)
+ | Olongconst (n: int64) (**r [rd] is set to the given integer constant *)
+ | Ofloatconst (n: float) (**r [rd] is set to the given float constant *)
+ | Osingleconst (n: float32)(**r [rd] is set to the given float constant *)
+ | Oaddrsymbol (id: ident) (ofs: ptrofs) (**r [rd] is set to the address of the symbol plus the given offset *)
+ | Oaddrstack (ofs: ptrofs) (**r [rd] is set to the stack pointer plus the given offset *)
+(*c 32-bit integer arithmetic: *)
+ | Ocast8signed (**r [rd] is 8-bit sign extension of [r1] *)
+ | Ocast16signed (**r [rd] is 16-bit sign extension of [r1] *)
+ | Oadd (**r [rd = r1 + r2] *)
+ | Oaddimm (n: int) (**r [rd = r1 + n] *)
+ | Oaddx (shift: shift1_4) (**r [rd = r1 << shift + r2] *)
+ | Oaddximm (shift: shift1_4) (n: int) (**r [rd = r1 << shift + n] *)
+ | Oneg (**r [rd = - r1] *)
+ | Osub (**r [rd = r1 - r2] *)
+ | Orevsubimm (n: int) (**r [rd = n - r1] *)
+ | Orevsubx (shift: shift1_4) (**r [rd = r2 -r1 << shift] *)
+ | Orevsubximm (shift: shift1_4) (n: int) (**r [rd = n -r1 << shift] *)
+ | Omul (**r [rd = r1 * r2] *)
+ | Omulimm (n: int) (**r [rd = r1 * n] *)
+ | Omulhs (**r [rd = high part of r1 * r2, signed] *)
+ | Omulhu (**r [rd = high part of r1 * r2, unsigned] *)
+ | Odiv (**r [rd = r1 / r2] (signed) *)
+ | Odivu (**r [rd = r1 / r2] (unsigned) *)
+ | Omod (**r [rd = r1 % r2] (signed) *)
+ | Omodu (**r [rd = r1 % r2] (unsigned) *)
+ | Oand (**r [rd = r1 & r2] *)
+ | Oandimm (n: int) (**r [rd = r1 & n] *)
+ | Onand (**r [rd = ~(r1 & r2)] *)
+ | Onandimm (n: int) (**r [rd = ~(r1 & n)] *)
+ | Oor (**r [rd = r1 | r2] *)
+ | Oorimm (n: int) (**r [rd = r1 | n] *)
+ | Onor (**r [rd = ~(r1 | r2)] *)
+ | Onorimm (n: int) (**r [rd = ~(r1 | n)] *)
+ | Oxor (**r [rd = r1 ^ r2] *)
+ | Oxorimm (n: int) (**r [rd = r1 ^ n] *)
+ | Onxor (**r [rd = ~(r1 ^ r2)] *)
+ | Onxorimm (n: int) (**r [rd = ~(r1 ^ n)] *)
+ | Onot (**r [rd = ~r1] *)
+ | Oandn (**r [rd = (~r1) & r2] *)
+ | Oandnimm (n: int) (**r [rd = (~r1) & n] *)
+ | Oorn (**r [rd = (~r1) | r2] *)
+ | Oornimm (n: int) (**r [rd = (~r1) | n] *)
+ | Oshl (**r [rd = r1 << r2] *)
+ | Oshlimm (n: int) (**r [rd = r1 << n] *)
+ | Oshr (**r [rd = r1 >>s r2] (signed) *)
+ | Oshrimm (n: int) (**r [rd = r1 >>s n] (signed) *)
+ | Oshru (**r [rd = r1 >>u r2] (unsigned) *)
+ | Oshruimm (n: int) (**r [rd = r1 >>x n] (unsigned) *)
+ | Oshrximm (n: int) (**r [rd = r1 / 2^n] (signed) *)
+ | Ororimm (n: int) (**r rotate right immediate *)
+ | Omadd (**r [rd = rd + r1 * r2] *)
+ | Omaddimm (n: int) (**r [rd = rd + r1 * imm] *)
+ | Omsub (**r [rd = rd - r1 * r2] *)
+(*c 64-bit integer arithmetic: *)
+ | Omakelong (**r [rd = r1 << 32 | r2] *)
+ | Olowlong (**r [rd = low-word(r1)] *)
+ | Ohighlong (**r [rd = high-word(r1)] *)
+ | Ocast32signed (**r [rd] is 32-bit sign extension of [r1] *)
+ | Ocast32unsigned (**r [rd] is 32-bit zero extension of [r1] *)
+ | Oaddl (**r [rd = r1 + r2] *)
+ | Oaddlimm (n: int64) (**r [rd = r1 + n] *)
+ | Oaddxl (shift: shift1_4) (**r [rd = r1 << shift + r2] *)
+ | Oaddxlimm (shift: shift1_4) (n: int64) (**r [rd = r1 << shift + n] *)
+ | Orevsublimm (n: int64) (**r [rd = n - r1] *)
+ | Orevsubxl (shift: shift1_4) (**r [rd = r2 -r1 << shift] *)
+ | Orevsubxlimm (shift: shift1_4) (n: int64) (**r [rd = n -r1 << shift] *)
+ | Onegl (**r [rd = - r1] *)
+ | Osubl (**r [rd = r1 - r2] *)
+ | Omull (**r [rd = r1 * r2] *)
+ | Omullimm (n: int64) (**r [rd = r1 * n] *)
+ | Omullhs (**r [rd = high part of r1 * r2, signed] *)
+ | Omullhu (**r [rd = high part of r1 * r2, unsigned] *)
+ | Odivl (**r [rd = r1 / r2] (signed) *)
+ | Odivlu (**r [rd = r1 / r2] (unsigned) *)
+ | Omodl (**r [rd = r1 % r2] (signed) *)
+ | Omodlu (**r [rd = r1 % r2] (unsigned) *)
+ | Oandl (**r [rd = r1 & r2] *)
+ | Oandlimm (n: int64) (**r [rd = r1 & n] *)
+ | Onandl (**r [rd = ~(r1 & r2)] *)
+ | Onandlimm (n: int64) (**r [rd = ~(r1 & n)] *)
+ | Oorl (**r [rd = r1 | r2] *)
+ | Oorlimm (n: int64) (**r [rd = r1 | n] *)
+ | Onorl (**r [rd = ~(r1 | r2)] *)
+ | Onorlimm (n: int64) (**r [rd = ~(r1 | n)] *)
+ | Oxorl (**r [rd = r1 ^ r2] *)
+ | Oxorlimm (n: int64) (**r [rd = r1 ^ n] *)
+ | Onxorl (**r [rd = ~(r1 ^ r2)] *)
+ | Onxorlimm (n: int64) (**r [rd = ~(r1 ^ n)] *)
+ | Onotl (**r [rd = ~r1] *)
+ | Oandnl (**r [rd = (~r1) & r2] *)
+ | Oandnlimm (n: int64) (**r [rd = (~r1) & n] *)
+ | Oornl (**r [rd = (~r1) | r2] *)
+ | Oornlimm (n: int64) (**r [rd = (~r1) | n] *)
+ | Oshll (**r [rd = r1 << r2] *)
+ | Oshllimm (n: int) (**r [rd = r1 << n] *)
+ | Oshrl (**r [rd = r1 >> r2] (signed) *)
+ | Oshrlimm (n: int) (**r [rd = r1 >> n] (signed) *)
+ | Oshrlu (**r [rd = r1 >> r2] (unsigned) *)
+ | Oshrluimm (n: int) (**r [rd = r1 >> n] (unsigned) *)
+ | Oshrxlimm (n: int) (**r [rd = r1 / 2^n] (signed) *)
+ | Omaddl (**r [rd = rd + r1 * r2] *)
+ | Omaddlimm (n: int64) (**r [rd = rd + r1 * imm] *)
+ | Omsubl (**r [rd = rd - r1 * r2] *)
+(*c Floating-point arithmetic: *)
+ | Onegf (**r [rd = - r1] *)
+ | Oabsf (**r [rd = abs(r1)] *)
+ | Oaddf (**r [rd = r1 + r2] *)
+ | Osubf (**r [rd = r1 - r2] *)
+ | Omulf (**r [rd = r1 * r2] *)
+ | Odivf (**r [rd = r1 / r2] *)
+ | Ominf
+ | Omaxf
+ | Ofmaddf
+ | Ofmsubf
+ | Onegfs (**r [rd = - r1] *)
+ | Oabsfs (**r [rd = abs(r1)] *)
+ | Oaddfs (**r [rd = r1 + r2] *)
+ | Osubfs (**r [rd = r1 - r2] *)
+ | Omulfs (**r [rd = r1 * r2] *)
+ | Odivfs (**r [rd = r1 / r2] *)
+ | Ominfs
+ | Omaxfs
+ | Oinvfs
+ | Ofmaddfs
+ | Ofmsubfs
+ | Osingleoffloat (**r [rd] is [r1] truncated to single-precision float *)
+ | Ofloatofsingle (**r [rd] is [r1] extended to double-precision float *)
+(*c Conversions between int and float: *)
+ | Ointoffloat (**r [rd = signed_int_of_float64(r1)] *)
+ | Ointuoffloat (**r [rd = unsigned_int_of_float64(r1)] *)
+ | Ointofsingle (**r [rd = signed_int_of_float32(r1)] *)
+ | Ointuofsingle (**r [rd = unsigned_int_of_float32(r1)] *)
+ | Osingleofint (**r [rd = float32_of_signed_int(r1)] *)
+ | Osingleofintu (**r [rd = float32_of_unsigned_int(r1)] *)
+ | Olongoffloat (**r [rd = signed_long_of_float64(r1)] *)
+ | Olonguoffloat (**r [rd = unsigned_long_of_float64(r1)] *)
+ | Ofloatoflong (**r [rd = float64_of_signed_long(r1)] *)
+ | Ofloatoflongu (**r [rd = float64_of_unsigned_long(r1)] *)
+ | Olongofsingle (**r [rd = signed_long_of_float32(r1)] *)
+ | Olonguofsingle (**r [rd = unsigned_long_of_float32(r1)] *)
+ | Osingleoflong (**r [rd = float32_of_signed_long(r1)] *)
+ | Osingleoflongu (**r [rd = float32_of_unsigned_int(r1)] *)
+(*c Boolean tests: *)
+ | Ocmp (cond: condition) (**r [rd = 1] if condition holds, [rd = 0] otherwise. *)
+ | Oextfz (stop : Z) (start : Z)
+ | Oextfs (stop : Z) (start : Z)
+ | Oextfzl (stop : Z) (start : Z)
+ | Oextfsl (stop : Z) (start : Z)
+ | Oinsf (stop : Z) (start : Z)
+ | Oinsfl (stop : Z) (start : Z)
+ | Osel (c0 : condition0) (ty : typ)
+ | Oselimm (c0 : condition0) (imm: int)
+ | Osellimm (c0 : condition0) (imm: int64).
+
+(** Addressing modes. [r1], [r2], etc, are the arguments to the
+ addressing. *)
+
+Inductive addressing: Type :=
+ | Aindexed2XS (scale : Z) : addressing (**r Address is [r1 + r2 << scale] *)
+ | Aindexed2 : addressing (**r Address is [r1 + r2] *)
+ | Aindexed: ptrofs -> addressing (**r Address is [r1 + offset] *)
+ | Aglobal: ident -> ptrofs -> addressing (**r Address is global plus offset *)
+ | Ainstack: ptrofs -> addressing. (**r Address is [stack_pointer + offset] *)
+
+(** Comparison functions (used in modules [CSE] and [Allocation]). *)
+
+Definition eq_condition (x y: condition) : {x=y} + {x<>y}.
+Proof.
+ generalize Int.eq_dec Int64.eq_dec; intro.
+ assert (forall (x y: comparison), {x=y}+{x<>y}). decide equality.
+ decide equality.
+Defined.
+
+Definition eq_condition0 (x y: condition0) : {x=y} + {x<>y}.
+Proof.
+ generalize Int.eq_dec Int64.eq_dec; intro.
+ assert (forall (x y: comparison), {x=y}+{x<>y}). decide equality.
+ decide equality.
+Defined.
+
+Definition eq_addressing (x y: addressing) : {x=y} + {x<>y}.
+Proof.
+ generalize ident_eq Ptrofs.eq_dec Z.eq_dec; intros.
+ decide equality.
+Defined.
+
+Definition eq_shift1_4 (x y : shift1_4): {x=y} + {x<>y}.
+Proof.
+ decide equality.
+Defined.
+
+Definition eq_operation: forall (x y: operation), {x=y} + {x<>y}.
+Proof.
+ generalize typ_eq Int.eq_dec Int64.eq_dec Ptrofs.eq_dec Float.eq_dec Float32.eq_dec ident_eq eq_condition eq_condition0 Z.eq_dec eq_shift1_4; intros.
+ decide equality.
+Defined.
+
+(* Alternate definition:
+Definition beq_operation: forall (x y: operation), bool.
+Proof.
+ generalize Int.eq_dec Int64.eq_dec Ptrofs.eq_dec Float.eq_dec Float32.eq_dec ident_eq eq_condition; boolean_equality.
+Defined.
+
+Definition eq_operation: forall (x y: operation), {x=y} + {x<>y}.
+Proof.
+ decidable_equality_from beq_operation.
+Defined.
+*)
+
+Global Opaque eq_condition eq_addressing eq_operation.
+
+(** * Evaluation functions *)
+
+(** Evaluation of conditions, operators and addressing modes applied
+ to lists of values. Return [None] when the computation can trigger an
+ error, e.g. integer division by zero. [eval_condition] returns a boolean,
+ [eval_operation] and [eval_addressing] return a value. *)
+
+Definition eval_condition (cond: condition) (vl: list val) (m: mem): option bool :=
+ match cond, vl with
+ | Ccomp c, v1 :: v2 :: nil => Val.cmp_bool c v1 v2
+ | Ccompu c, v1 :: v2 :: nil => Val.cmpu_bool (Mem.valid_pointer m) c v1 v2
+ | Ccompimm c n, v1 :: nil => Val.cmp_bool c v1 (Vint n)
+ | Ccompuimm c n, v1 :: nil => Val.cmpu_bool (Mem.valid_pointer m) c v1 (Vint n)
+ | Ccompl c, v1 :: v2 :: nil => Val.cmpl_bool c v1 v2
+ | Ccomplu c, v1 :: v2 :: nil => Val.cmplu_bool (Mem.valid_pointer m) c v1 v2
+ | Ccomplimm c n, v1 :: nil => Val.cmpl_bool c v1 (Vlong n)
+ | Ccompluimm c n, v1 :: nil => Val.cmplu_bool (Mem.valid_pointer m) c v1 (Vlong n)
+ | Ccompf c, v1 :: v2 :: nil => Val.cmpf_bool c v1 v2
+ | Cnotcompf c, v1 :: v2 :: nil => option_map negb (Val.cmpf_bool c v1 v2)
+ | Ccompfs c, v1 :: v2 :: nil => Val.cmpfs_bool c v1 v2
+ | Cnotcompfs c, v1 :: v2 :: nil => option_map negb (Val.cmpfs_bool c v1 v2)
+ | _, _ => None
+ end.
+
+Definition eval_condition0 (cond: condition0) (v1: val) (m: mem): option bool :=
+ match cond with
+ | Ccomp0 c => Val.cmp_bool c v1 (Vint Int.zero)
+ | Ccompu0 c => Val.cmpu_bool (Mem.valid_pointer m) c v1 (Vint Int.zero)
+ | Ccompl0 c => Val.cmpl_bool c v1 (Vlong Int64.zero)
+ | Ccomplu0 c => Val.cmplu_bool (Mem.valid_pointer m) c v1 (Vlong Int64.zero)
+ end.
+
+Definition negate_condition0 (cond0 : condition0) : condition0 :=
+ match cond0 with
+ | Ccomp0 c => Ccomp0 (negate_comparison c)
+ | Ccompu0 c => Ccompu0 (negate_comparison c)
+ | Ccompl0 c => Ccompl0 (negate_comparison c)
+ | Ccomplu0 c => Ccomplu0 (negate_comparison c)
+ end.
+
+Definition eval_operation
+ (F V: Type) (genv: Genv.t F V) (sp: val)
+ (op: operation) (vl: list val) (m: mem): option val :=
+ match op, vl with
+ | Omove, v1::nil => Some v1
+ | Ointconst n, nil => Some (Vint n)
+ | Olongconst n, nil => Some (Vlong n)
+ | Ofloatconst n, nil => Some (Vfloat n)
+ | Osingleconst n, nil => Some (Vsingle n)
+ | Oaddrsymbol s ofs, nil => Some (Genv.symbol_address genv s ofs)
+ | Oaddrstack ofs, nil => Some (Val.offset_ptr sp ofs)
+ | Ocast8signed, v1 :: nil => Some (Val.sign_ext 8 v1)
+ | Ocast16signed, v1 :: nil => Some (Val.sign_ext 16 v1)
+ | Oadd, v1 :: v2 :: nil => Some (Val.add v1 v2)
+ | Oaddimm n, v1 :: nil => Some (Val.add v1 (Vint n))
+ | Oaddx s14, v1 :: v2 :: nil => Some (addx (int_of_shift1_4 s14) v1 v2)
+ | Oaddximm s14 n, v1 :: nil => Some (addx (int_of_shift1_4 s14) v1 (Vint n))
+ | Oneg, v1 :: nil => Some (Val.neg v1)
+ | Osub, v1 :: v2 :: nil => Some (Val.sub v1 v2)
+ | Orevsubimm n, v1 :: nil => Some (Val.sub (Vint n) v1)
+ | Orevsubx shift, v1 :: v2 :: nil => Some (ExtValues.revsubx (int_of_shift1_4 shift) v1 v2)
+ | Orevsubximm shift n, v1 :: nil => Some (ExtValues.revsubx (int_of_shift1_4 shift) v1 (Vint n))
+ | Omul, v1 :: v2 :: nil => Some (Val.mul v1 v2)
+ | Omulimm n, v1 :: nil => Some (Val.mul v1 (Vint n))
+ | Omulhs, v1::v2::nil => Some (Val.mulhs v1 v2)
+ | Omulhu, v1::v2::nil => Some (Val.mulhu v1 v2)
+ | Odiv, v1 :: v2 :: nil => Val.divs v1 v2
+ | Odivu, v1 :: v2 :: nil => Val.divu v1 v2
+ | Omod, v1 :: v2 :: nil => Val.mods v1 v2
+ | Omodu, v1 :: v2 :: nil => Val.modu v1 v2
+ | Oand, v1 :: v2 :: nil => Some (Val.and v1 v2)
+ | Oandimm n, v1 :: nil => Some (Val.and v1 (Vint n))
+ | Onand, v1 :: v2 :: nil => Some (Val.notint (Val.and v1 v2))
+ | Onandimm n, v1 :: nil => Some (Val.notint (Val.and v1 (Vint n)))
+ | Oor, v1 :: v2 :: nil => Some (Val.or v1 v2)
+ | Oorimm n, v1 :: nil => Some (Val.or v1 (Vint n))
+ | Onor, v1 :: v2 :: nil => Some (Val.notint (Val.or v1 v2))
+ | Onorimm n, v1 :: nil => Some (Val.notint (Val.or v1 (Vint n)))
+ | Oxor, v1 :: v2 :: nil => Some (Val.xor v1 v2)
+ | Oxorimm n, v1 :: nil => Some (Val.xor v1 (Vint n))
+ | Onxor, v1 :: v2 :: nil => Some (Val.notint (Val.xor v1 v2))
+ | Onxorimm n, v1 :: nil => Some (Val.notint (Val.xor v1 (Vint n)))
+ | Onot, v1 :: nil => Some (Val.notint v1)
+ | Oandn, v1 :: v2 :: nil => Some (Val.and (Val.notint v1) v2)
+ | Oandnimm n, v1 :: nil => Some (Val.and (Val.notint v1) (Vint n))
+ | Oorn, v1 :: v2 :: nil => Some (Val.or (Val.notint v1) v2)
+ | Oornimm n, v1 :: nil => Some (Val.or (Val.notint v1) (Vint n))
+ | Oshl, v1 :: v2 :: nil => Some (Val.shl v1 v2)
+ | Oshlimm n, v1 :: nil => Some (Val.shl v1 (Vint n))
+ | Oshr, v1 :: v2 :: nil => Some (Val.shr v1 v2)
+ | Oshrimm n, v1 :: nil => Some (Val.shr v1 (Vint n))
+ | Ororimm n, v1 :: nil => Some (Val.ror v1 (Vint n))
+ | Oshru, v1 :: v2 :: nil => Some (Val.shru v1 v2)
+ | Oshruimm n, v1 :: nil => Some (Val.shru v1 (Vint n))
+ | Oshrximm n, v1::nil => Some (Val.maketotal (Val.shrx v1 (Vint n)))
+ | Omadd, v1::v2::v3::nil => Some (Val.add v1 (Val.mul v2 v3))
+ | (Omaddimm n), v1::v2::nil => Some (Val.add v1 (Val.mul v2 (Vint n)))
+ | Omsub, v1::v2::v3::nil => Some (Val.sub v1 (Val.mul v2 v3))
+
+ | Omakelong, v1::v2::nil => Some (Val.longofwords v1 v2)
+ | Olowlong, v1::nil => Some (Val.loword v1)
+ | Ohighlong, v1::nil => Some (Val.hiword v1)
+ | Ocast32signed, v1 :: nil => Some (Val.longofint v1)
+ | Ocast32unsigned, v1 :: nil => Some (Val.longofintu v1)
+ | Oaddl, v1 :: v2 :: nil => Some (Val.addl v1 v2)
+ | Oaddlimm n, v1::nil => Some (Val.addl v1 (Vlong n))
+ | Oaddxl s14, v1 :: v2 :: nil => Some (addxl (int_of_shift1_4 s14) v1 v2)
+ | Oaddxlimm s14 n, v1 :: nil => Some (addxl (int_of_shift1_4 s14) v1 (Vlong n))
+ | Onegl, v1::nil => Some (Val.negl v1)
+ | Osubl, v1::v2::nil => Some (Val.subl v1 v2)
+ | Orevsublimm n, v1 :: nil => Some (Val.subl (Vlong n) v1)
+ | Orevsubxl shift, v1 :: v2 :: nil => Some (ExtValues.revsubxl (int_of_shift1_4 shift) v1 v2)
+ | Orevsubxlimm shift n, v1 :: nil => Some (ExtValues.revsubxl (int_of_shift1_4 shift) v1 (Vlong n))
+ | Omull, v1::v2::nil => Some (Val.mull v1 v2)
+ | Omullimm n, v1::nil => Some (Val.mull v1 (Vlong n))
+ | Omullhs, v1::v2::nil => Some (Val.mullhs v1 v2)
+ | Omullhu, v1::v2::nil => Some (Val.mullhu v1 v2)
+ | Odivl, v1::v2::nil => Val.divls v1 v2
+ | Odivlu, v1::v2::nil => Val.divlu v1 v2
+ | Omodl, v1::v2::nil => Val.modls v1 v2
+ | Omodlu, v1::v2::nil => Val.modlu v1 v2
+ | Oandl, v1::v2::nil => Some(Val.andl v1 v2)
+ | Oandlimm n, v1::nil => Some (Val.andl v1 (Vlong n))
+ | Onandl, v1::v2::nil => Some(Val.notl (Val.andl v1 v2))
+ | Onandlimm n, v1::nil => Some(Val.notl (Val.andl v1 (Vlong n)))
+ | Oorl, v1::v2::nil => Some(Val.orl v1 v2)
+ | Oorlimm n, v1::nil => Some (Val.orl v1 (Vlong n))
+ | Onorl, v1::v2::nil => Some(Val.notl (Val.orl v1 v2))
+ | Onorlimm n, v1::nil => Some(Val.notl (Val.orl v1 (Vlong n)))
+ | Oxorl, v1::v2::nil => Some(Val.xorl v1 v2)
+ | Oxorlimm n, v1::nil => Some (Val.xorl v1 (Vlong n))
+ | Onxorl, v1::v2::nil => Some(Val.notl (Val.xorl v1 v2))
+ | Onxorlimm n, v1::nil => Some(Val.notl (Val.xorl v1 (Vlong n)))
+ | Onotl, v1 :: nil => Some (Val.notl v1)
+ | Oandnl, v1 :: v2 :: nil => Some (Val.andl (Val.notl v1) v2)
+ | Oandnlimm n, v1 :: nil => Some (Val.andl (Val.notl v1) (Vlong n))
+ | Oornl, v1 :: v2 :: nil => Some (Val.orl (Val.notl v1) v2)
+ | Oornlimm n, v1 :: nil => Some (Val.orl (Val.notl v1) (Vlong n))
+ | Oshll, v1::v2::nil => Some (Val.shll v1 v2)
+ | Oshllimm n, v1::nil => Some (Val.shll v1 (Vint n))
+ | Oshrl, v1::v2::nil => Some (Val.shrl v1 v2)
+ | Oshrlimm n, v1::nil => Some (Val.shrl v1 (Vint n))
+ | Oshrlu, v1::v2::nil => Some (Val.shrlu v1 v2)
+ | Oshrluimm n, v1::nil => Some (Val.shrlu v1 (Vint n))
+ | Oshrxlimm n, v1::nil => Some (Val.maketotal (Val.shrxl v1 (Vint n)))
+ | Omaddl, v1::v2::v3::nil => Some (Val.addl v1 (Val.mull v2 v3))
+ | (Omaddlimm n), v1::v2::nil => Some (Val.addl v1 (Val.mull v2 (Vlong n)))
+ | Omsubl, v1::v2::v3::nil => Some (Val.subl v1 (Val.mull v2 v3))
+
+ | Onegf, v1::nil => Some (Val.negf v1)
+ | Oabsf, v1::nil => Some (Val.absf v1)
+ | Oaddf, v1::v2::nil => Some (Val.addf v1 v2)
+ | Osubf, v1::v2::nil => Some (Val.subf v1 v2)
+ | Omulf, v1::v2::nil => Some (Val.mulf v1 v2)
+ | Odivf, v1::v2::nil => Some (Val.divf v1 v2)
+ | Ominf, v1::v2::nil => Some (ExtValues.minf v1 v2)
+ | Omaxf, v1::v2::nil => Some (ExtValues.maxf v1 v2)
+ | Ofmaddf, v1::v2::v3::nil => Some (ExtValues.fmaddf v1 v2 v3)
+ | Ofmsubf, v1::v2::v3::nil => Some (ExtValues.fmsubf v1 v2 v3)
+
+ | Onegfs, v1::nil => Some (Val.negfs v1)
+ | Oabsfs, v1::nil => Some (Val.absfs v1)
+ | Oaddfs, v1::v2::nil => Some (Val.addfs v1 v2)
+ | Osubfs, v1::v2::nil => Some (Val.subfs v1 v2)
+ | Omulfs, v1::v2::nil => Some (Val.mulfs v1 v2)
+ | Odivfs, v1::v2::nil => Some (Val.divfs v1 v2)
+ | Ominfs, v1::v2::nil => Some (ExtValues.minfs v1 v2)
+ | Omaxfs, v1::v2::nil => Some (ExtValues.maxfs v1 v2)
+ | Oinvfs, v1::nil => Some (ExtValues.invfs v1)
+ | Ofmaddfs, v1::v2::v3::nil => Some (ExtValues.fmaddfs v1 v2 v3)
+ | Ofmsubfs, v1::v2::v3::nil => Some (ExtValues.fmsubfs v1 v2 v3)
+
+ | Osingleoffloat, v1::nil => Some (Val.singleoffloat v1)
+ | Ofloatofsingle, v1::nil => Some (Val.floatofsingle v1)
+ | Ointoffloat, v1::nil => Some (Val.maketotal (Val.intoffloat v1))
+ | Ointuoffloat, v1::nil => Some (Val.maketotal (Val.intuoffloat v1))
+ | Ointofsingle, v1::nil => Some (Val.maketotal (Val.intofsingle v1))
+ | Ointuofsingle, v1::nil => Some (Val.maketotal (Val.intuofsingle v1))
+ | Osingleofint, v1::nil => Some (Val.maketotal (Val.singleofint v1))
+ | Osingleofintu, v1::nil => Some (Val.maketotal (Val.singleofintu v1))
+ | Olongoffloat, v1::nil => Some (Val.maketotal (Val.longoffloat v1))
+ | Olonguoffloat, v1::nil => Some (Val.maketotal (Val.longuoffloat v1))
+ | Ofloatoflong, v1::nil => Some (Val.maketotal (Val.floatoflong v1))
+ | Ofloatoflongu, v1::nil => Some (Val.maketotal (Val.floatoflongu v1))
+ | Olongofsingle, v1::nil => Some (Val.maketotal (Val.longofsingle v1))
+ | Olonguofsingle, v1::nil => Some (Val.maketotal (Val.longuofsingle v1))
+ | Osingleoflong, v1::nil => Some (Val.maketotal (Val.singleoflong v1))
+ | Osingleoflongu, v1::nil => Some (Val.maketotal (Val.singleoflongu v1))
+ | Ocmp c, _ => Some (Val.of_optbool (eval_condition c vl m))
+ | (Oextfz stop start), v0::nil => Some (extfz stop start v0)
+ | (Oextfs stop start), v0::nil => Some (extfs stop start v0)
+ | (Oextfzl stop start), v0::nil => Some (extfzl stop start v0)
+ | (Oextfsl stop start), v0::nil => Some (extfsl stop start v0)
+ | (Oinsf stop start), v0::v1::nil => Some (insf stop start v0 v1)
+ | (Oinsfl stop start), v0::v1::nil => Some (insfl stop start v0 v1)
+ | Osel c ty, v1::v2::vc::nil => Some(Val.select (eval_condition0 c vc m) v1 v2 ty)
+ | Oselimm c imm, v1::vc::nil => Some(Val.select (eval_condition0 c vc m) v1 (Vint imm) Tint)
+ | Osellimm c imm, v1::vc::nil => Some(Val.select (eval_condition0 c vc m) v1 (Vlong imm) Tlong)
+ | _, _ => None
+ end.
+
+Definition eval_addressing
+ (F V: Type) (genv: Genv.t F V) (sp: val)
+ (addr: addressing) (vl: list val) : option val :=
+ match addr, vl with
+ | Aindexed2XS scale, v1 :: v2 :: nil => Some (Val.addl v1 (Val.shll v2 (Vint (Int.repr scale))))
+ | Aindexed2, v1 :: v2 :: nil => Some (Val.addl v1 v2)
+ | Aindexed n, v1 :: nil => Some (Val.offset_ptr v1 n)
+ | Aglobal s ofs, nil => Some (Genv.symbol_address genv s ofs)
+ | Ainstack n, nil => Some (Val.offset_ptr sp n)
+ | _, _ => None
+ end.
+
+Remark eval_addressing_Ainstack:
+ forall (F V: Type) (genv: Genv.t F V) sp ofs,
+ eval_addressing genv sp (Ainstack ofs) nil = Some (Val.offset_ptr sp ofs).
+Proof.
+ intros. reflexivity.
+Qed.
+
+Remark eval_addressing_Ainstack_inv:
+ forall (F V: Type) (genv: Genv.t F V) sp ofs vl v,
+ eval_addressing genv sp (Ainstack ofs) vl = Some v -> vl = nil /\ v = Val.offset_ptr sp ofs.
+Proof.
+ unfold eval_addressing; intros; destruct vl; inv H; auto.
+Qed.
+
+Ltac FuncInv :=
+ match goal with
+ | H: (match ?x with nil => _ | _ :: _ => _ end = Some _) |- _ =>
+ destruct x; cbn in H; FuncInv
+ | H: (match ?v with Vundef => _ | Vint _ => _ | Vfloat _ => _ | Vptr _ _ => _ end = Some _) |- _ =>
+ destruct v; cbn in H; FuncInv
+ | H: (if Archi.ptr64 then _ else _) = Some _ |- _ =>
+ destruct Archi.ptr64 eqn:?; FuncInv
+ | H: (Some _ = Some _) |- _ =>
+ injection H; intros; clear H; FuncInv
+ | H: (None = Some _) |- _ =>
+ discriminate H
+ | _ =>
+ idtac
+ end.
+
+(** * Static typing of conditions, operators and addressing modes. *)
+
+Definition type_of_condition (c: condition) : list typ :=
+ match c with
+ | Ccomp _ => Tint :: Tint :: nil
+ | Ccompu _ => Tint :: Tint :: nil
+ | Ccompimm _ _ => Tint :: nil
+ | Ccompuimm _ _ => Tint :: nil
+ | Ccompl _ => Tlong :: Tlong :: nil
+ | Ccomplu _ => Tlong :: Tlong :: nil
+ | Ccomplimm _ _ => Tlong :: nil
+ | Ccompluimm _ _ => Tlong :: nil
+ | Ccompf _ => Tfloat :: Tfloat :: nil
+ | Cnotcompf _ => Tfloat :: Tfloat :: nil
+ | Ccompfs _ => Tsingle :: Tsingle :: nil
+ | Cnotcompfs _ => Tsingle :: Tsingle :: nil
+ end.
+
+Definition type_of_operation (op: operation) : list typ * typ :=
+ match op with
+ | Omove => (nil, Tint) (* treated specially *)
+ | Ointconst _ => (nil, Tint)
+ | Olongconst _ => (nil, Tlong)
+ | Ofloatconst f => (nil, Tfloat)
+ | Osingleconst f => (nil, Tsingle)
+ | Oaddrsymbol _ _ => (nil, Tptr)
+ | Oaddrstack _ => (nil, Tptr)
+ | Ocast8signed => (Tint :: nil, Tint)
+ | Ocast16signed => (Tint :: nil, Tint)
+ | Oadd => (Tint :: Tint :: nil, Tint)
+ | Oaddimm _ => (Tint :: nil, Tint)
+ | Oaddx _ => (Tint :: Tint :: nil, Tint)
+ | Oaddximm _ _ => (Tint :: nil, Tint)
+ | Oneg => (Tint :: nil, Tint)
+ | Osub => (Tint :: Tint :: nil, Tint)
+ | Orevsubimm _ => (Tint :: nil, Tint)
+ | Orevsubx _ => (Tint :: Tint :: nil, Tint)
+ | Orevsubximm _ _ => (Tint :: nil, Tint)
+ | Omul => (Tint :: Tint :: nil, Tint)
+ | Omulimm _ => (Tint :: nil, Tint)
+ | Omulhs => (Tint :: Tint :: nil, Tint)
+ | Omulhu => (Tint :: Tint :: nil, Tint)
+ | Odiv => (Tint :: Tint :: nil, Tint)
+ | Odivu => (Tint :: Tint :: nil, Tint)
+ | Omod => (Tint :: Tint :: nil, Tint)
+ | Omodu => (Tint :: Tint :: nil, Tint)
+ | Oand => (Tint :: Tint :: nil, Tint)
+ | Oandimm _ => (Tint :: nil, Tint)
+ | Onand => (Tint :: Tint :: nil, Tint)
+ | Onandimm _ => (Tint :: nil, Tint)
+ | Oor => (Tint :: Tint :: nil, Tint)
+ | Oorimm _ => (Tint :: nil, Tint)
+ | Onor => (Tint :: Tint :: nil, Tint)
+ | Onorimm _ => (Tint :: nil, Tint)
+ | Oxor => (Tint :: Tint :: nil, Tint)
+ | Oxorimm _ => (Tint :: nil, Tint)
+ | Onxor => (Tint :: Tint :: nil, Tint)
+ | Onxorimm _ => (Tint :: nil, Tint)
+ | Onot => (Tint :: nil, Tint)
+ | Oandn => (Tint :: Tint :: nil, Tint)
+ | Oandnimm _ => (Tint :: nil, Tint)
+ | Oorn => (Tint :: Tint :: nil, Tint)
+ | Oornimm _ => (Tint :: nil, Tint)
+ | Oshl => (Tint :: Tint :: nil, Tint)
+ | Oshlimm _ => (Tint :: nil, Tint)
+ | Oshr => (Tint :: Tint :: nil, Tint)
+ | Oshrimm _ => (Tint :: nil, Tint)
+ | Oshru => (Tint :: Tint :: nil, Tint)
+ | Oshruimm _ => (Tint :: nil, Tint)
+ | Oshrximm _ => (Tint :: nil, Tint)
+ | Ororimm _ => (Tint :: nil, Tint)
+ | Omadd => (Tint :: Tint :: Tint :: nil, Tint)
+ | Omaddimm _ => (Tint :: Tint :: nil, Tint)
+ | Omsub => (Tint :: Tint :: Tint :: nil, Tint)
+
+ | Omakelong => (Tint :: Tint :: nil, Tlong)
+ | Olowlong => (Tlong :: nil, Tint)
+ | Ohighlong => (Tlong :: nil, Tint)
+ | Ocast32signed => (Tint :: nil, Tlong)
+ | Ocast32unsigned => (Tint :: nil, Tlong)
+ | Oaddl => (Tlong :: Tlong :: nil, Tlong)
+ | Oaddlimm _ => (Tlong :: nil, Tlong)
+ | Oaddxl _ => (Tlong :: Tlong :: nil, Tlong)
+ | Oaddxlimm _ _ => (Tlong :: nil, Tlong)
+ | Orevsublimm _ => (Tlong :: nil, Tlong)
+ | Orevsubxl _ => (Tlong :: Tlong :: nil, Tlong)
+ | Orevsubxlimm _ _ => (Tlong :: nil, Tlong)
+ | Onegl => (Tlong :: nil, Tlong)
+ | Osubl => (Tlong :: Tlong :: nil, Tlong)
+ | Omull => (Tlong :: Tlong :: nil, Tlong)
+ | Omullimm _ => (Tlong :: nil, Tlong)
+ | Omullhs => (Tlong :: Tlong :: nil, Tlong)
+ | Omullhu => (Tlong :: Tlong :: nil, Tlong)
+ | Odivl => (Tlong :: Tlong :: nil, Tlong)
+ | Odivlu => (Tlong :: Tlong :: nil, Tlong)
+ | Omodl => (Tlong :: Tlong :: nil, Tlong)
+ | Omodlu => (Tlong :: Tlong :: nil, Tlong)
+ | Oandl => (Tlong :: Tlong :: nil, Tlong)
+ | Oandlimm _ => (Tlong :: nil, Tlong)
+ | Onandl => (Tlong :: Tlong :: nil, Tlong)
+ | Onandlimm _ => (Tlong :: nil, Tlong)
+ | Oorl => (Tlong :: Tlong :: nil, Tlong)
+ | Oorlimm _ => (Tlong :: nil, Tlong)
+ | Onorl => (Tlong :: Tlong :: nil, Tlong)
+ | Onorlimm _ => (Tlong :: nil, Tlong)
+ | Oxorl => (Tlong :: Tlong :: nil, Tlong)
+ | Oxorlimm _ => (Tlong :: nil, Tlong)
+ | Onxorl => (Tlong :: Tlong :: nil, Tlong)
+ | Onxorlimm _ => (Tlong :: nil, Tlong)
+ | Onotl => (Tlong :: nil, Tlong)
+ | Oandnl => (Tlong :: Tlong :: nil, Tlong)
+ | Oandnlimm _ => (Tlong :: nil, Tlong)
+ | Oornl => (Tlong :: Tlong :: nil, Tlong)
+ | Oornlimm _ => (Tlong :: nil, Tlong)
+ | Oshll => (Tlong :: Tint :: nil, Tlong)
+ | Oshllimm _ => (Tlong :: nil, Tlong)
+ | Oshrl => (Tlong :: Tint :: nil, Tlong)
+ | Oshrlimm _ => (Tlong :: nil, Tlong)
+ | Oshrlu => (Tlong :: Tint :: nil, Tlong)
+ | Oshrluimm _ => (Tlong :: nil, Tlong)
+ | Oshrxlimm _ => (Tlong :: nil, Tlong)
+ | Omaddl => (Tlong :: Tlong :: Tlong :: nil, Tlong)
+ | Omaddlimm _ => (Tlong :: Tlong :: nil, Tlong)
+ | Omsubl => (Tlong :: Tlong :: Tlong :: nil, Tlong)
+
+ | Onegf => (Tfloat :: nil, Tfloat)
+ | Oabsf => (Tfloat :: nil, Tfloat)
+ | Oaddf
+ | Osubf
+ | Omulf
+ | Odivf
+ | Ominf
+ | Omaxf => (Tfloat :: Tfloat :: nil, Tfloat)
+ | Ofmaddf | Ofmsubf => (Tfloat :: Tfloat :: Tfloat :: nil, Tfloat)
+
+ | Onegfs => (Tsingle :: nil, Tsingle)
+ | Oabsfs => (Tsingle :: nil, Tsingle)
+ | Oaddfs
+ | Osubfs
+ | Omulfs
+ | Odivfs
+ | Ominfs
+ | Omaxfs => (Tsingle :: Tsingle :: nil, Tsingle)
+ | Oinvfs => (Tsingle :: nil, Tsingle)
+ | Ofmaddfs | Ofmsubfs => (Tsingle :: Tsingle :: Tsingle :: nil, Tsingle)
+
+ | Osingleoffloat => (Tfloat :: nil, Tsingle)
+ | Ofloatofsingle => (Tsingle :: nil, Tfloat)
+ | Ointoffloat => (Tfloat :: nil, Tint)
+ | Ointuoffloat => (Tfloat :: nil, Tint)
+ | Ointofsingle => (Tsingle :: nil, Tint)
+ | Ointuofsingle => (Tsingle :: nil, Tint)
+ | Osingleofint => (Tint :: nil, Tsingle)
+ | Osingleofintu => (Tint :: nil, Tsingle)
+ | Olongoffloat => (Tfloat :: nil, Tlong)
+ | Olonguoffloat => (Tfloat :: nil, Tlong)
+ | Ofloatoflong => (Tlong :: nil, Tfloat)
+ | Ofloatoflongu => (Tlong :: nil, Tfloat)
+ | Olongofsingle => (Tsingle :: nil, Tlong)
+ | Olonguofsingle => (Tsingle :: nil, Tlong)
+ | Osingleoflong => (Tlong :: nil, Tsingle)
+ | Osingleoflongu => (Tlong :: nil, Tsingle)
+ | Ocmp c => (type_of_condition c, Tint)
+ | Oextfz _ _ | Oextfs _ _ => (Tint :: nil, Tint)
+ | Oextfzl _ _ | Oextfsl _ _ => (Tlong :: nil, Tlong)
+ | Oinsf _ _ => (Tint :: Tint :: nil, Tint)
+ | Oinsfl _ _ => (Tlong :: Tlong :: nil, Tlong)
+ | Osel c ty => (ty :: ty :: arg_type_of_condition0 c :: nil, ty)
+ | Oselimm c ty => (Tint :: arg_type_of_condition0 c :: nil, Tint)
+ | Osellimm c ty => (Tlong :: arg_type_of_condition0 c :: nil, Tlong)
+ end.
+
+(* FIXME: two Tptr ?! *)
+Definition type_of_addressing (addr: addressing) : list typ :=
+ match addr with
+ | Aindexed2XS _ => Tptr :: Tptr :: nil
+ | Aindexed2 => Tptr :: Tptr :: nil
+ | Aindexed _ => Tptr :: nil
+ | Aglobal _ _ => nil
+ | Ainstack _ => nil
+ end.
+
+(** Weak type soundness results for [eval_operation]:
+ the result values, when defined, are always of the type predicted
+ by [type_of_operation]. *)
+
+Section SOUNDNESS.
+
+Variable A V: Type.
+Variable genv: Genv.t A V.
+
+Remark type_add:
+ forall v1 v2, Val.has_type (Val.add v1 v2) Tint.
+Proof.
+ intros. unfold Val.has_type, Val.add. destruct Archi.ptr64, v1, v2; auto.
+Qed.
+
+Remark type_addl:
+ forall v1 v2, Val.has_type (Val.addl v1 v2) Tlong.
+Proof.
+ intros. unfold Val.has_type, Val.addl. destruct Archi.ptr64, v1, v2; auto.
+Qed.
+
+Remark type_sub:
+ forall v1 v2, Val.has_type (Val.sub v1 v2) Tint.
+Proof.
+ intros. unfold Val.has_type, Val.sub. destruct Archi.ptr64, v1, v2; cbn; auto.
+ destruct (eq_block _ _); auto.
+Qed.
+
+Remark type_subl:
+ forall v1 v2, Val.has_type (Val.subl v1 v2) Tlong.
+Proof.
+ intros. unfold Val.has_type, Val.subl. destruct Archi.ptr64, v1, v2; cbn; auto.
+ destruct (eq_block _ _); auto.
+Qed.
+
+Remark type_shl:
+ forall v1 v2, Val.has_type (Val.shl v1 v2) Tint.
+Proof.
+ destruct v1, v2; cbn; trivial; destruct (Int.ltu _ _); cbn; trivial.
+Qed.
+
+Remark type_shll:
+ forall v1 v2, Val.has_type (Val.shll v1 v2) Tlong.
+Proof.
+ destruct v1, v2; cbn; trivial; destruct (Int.ltu _ _); cbn; trivial.
+Qed.
+
+Lemma type_of_operation_sound:
+ forall op vl sp v m,
+ op <> Omove ->
+ eval_operation genv sp op vl m = Some v ->
+ Val.has_type v (snd (type_of_operation op)).
+Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
+ intros.
+ destruct op; cbn; cbn in H0; FuncInv; subst; cbn.
+ (* move *)
+ - congruence.
+ (* intconst, longconst, floatconst, singleconst *)
+ - exact I.
+ - exact I.
+ - exact I.
+ - exact I.
+ (* addrsymbol *)
+ - unfold Genv.symbol_address. destruct (Genv.find_symbol genv id)...
+ (* addrstack *)
+ - destruct sp...
+ (* castsigned *)
+ - destruct v0...
+ - destruct v0...
+ (* add, addimm *)
+ - apply type_add.
+ - apply type_add.
+ (* addx, addximm *)
+ - apply type_add.
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ (* neg, sub *)
+ - destruct v0...
+ - apply type_sub.
+ (* revsubimm, revsubx, revsubximm *)
+ - destruct v0...
+ - apply type_sub.
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ (* mul, mulimm, mulhs, mulhu *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* div, divu *)
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (_ || _); inv H2...
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (Int.eq i0 Int.zero); inv H2...
+ (* mod, modu *)
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (_ || _); inv H2...
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (Int.eq i0 Int.zero); inv H2...
+ (* and, andimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* nand, nandimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* or, orimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* nor, norimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* xor, xorimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* nxor, nxorimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* not *)
+ - destruct v0...
+ (* andn, andnimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* orn, ornimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* shl, shlimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int.iwordsize)...
+ - destruct v0; cbn... destruct (Int.ltu n Int.iwordsize)...
+ (* shr, shrimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int.iwordsize)...
+ - destruct v0; cbn... destruct (Int.ltu n Int.iwordsize)...
+ (* shru, shruimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int.iwordsize)...
+ - destruct v0; cbn... destruct (Int.ltu n Int.iwordsize)...
+ (* shrx *)
+ - destruct v0; cbn... destruct (Int.ltu n (Int.repr 31)); cbn; trivial.
+ (* shrimm *)
+ - destruct v0; cbn...
+ (* madd *)
+ - apply type_add.
+ - apply type_add.
+ (* msub *)
+ - apply type_sub.
+ (* makelong, lowlong, highlong *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ - destruct v0...
+ (* cast32 *)
+ - destruct v0...
+ - destruct v0...
+ (* addl, addlimm *)
+ - apply type_addl.
+ - apply type_addl.
+ (* addxl addxlimm *)
+ - apply type_addl.
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ (* negl, subl *)
+ - destruct v0...
+ - apply type_subl.
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ - destruct v0...
+ - apply type_subl.
+ (* mull, mullhs, mullhu *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* divl, divlu *)
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (_ || _); inv H2...
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (Int64.eq i0 Int64.zero); inv H2...
+ (* modl, modlu *)
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (_ || _); inv H2...
+ - destruct v0; destruct v1; cbn in *; inv H0.
+ destruct (Int64.eq i0 Int64.zero); inv H2...
+ (* andl, andlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* nandl, nandlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* orl, orlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* norl, norlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* xorl, xorlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* nxorl, nxorlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* notl *)
+ - destruct v0...
+ (* andnl, andnlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* ornl, ornlimm *)
+ - destruct v0; destruct v1...
+ - destruct v0...
+ (* shll, shllimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int64.iwordsize')...
+ - destruct v0; cbn... destruct (Int.ltu n Int64.iwordsize')...
+ (* shr, shrimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int64.iwordsize')...
+ - destruct v0; cbn... destruct (Int.ltu n Int64.iwordsize')...
+ (* shru, shruimm *)
+ - destruct v0; destruct v1; cbn... destruct (Int.ltu i0 Int64.iwordsize')...
+ - destruct v0; cbn... destruct (Int.ltu n Int64.iwordsize')...
+ (* shrxl *)
+ - destruct v0; cbn... destruct (Int.ltu n (Int.repr 63)); cbn; trivial.
+ (* maddl, maddlim *)
+ - apply type_addl.
+ - apply type_addl.
+ (* msubl *)
+ - apply type_subl.
+ (* negf, absf *)
+ - destruct v0...
+ - destruct v0...
+ (* addf, subf *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* mulf, divf *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* minf, maxf *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* fmaddf, fmsubf *)
+ - destruct v0; destruct v1; destruct v2...
+ - destruct v0; destruct v1; destruct v2...
+ (* negfs, absfs *)
+ - destruct v0...
+ - destruct v0...
+ (* addfs, subfs *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* mulfs, divfs *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* minfs, maxfs *)
+ - destruct v0; destruct v1...
+ - destruct v0; destruct v1...
+ (* invfs *)
+ - destruct v0...
+ (* fmaddfs, fmsubfs *)
+ - destruct v0; destruct v1; destruct v2...
+ - destruct v0; destruct v1; destruct v2...
+ (* singleoffloat, floatofsingle *)
+ - destruct v0...
+ - destruct v0...
+ (* intoffloat, intuoffloat *)
+ - destruct v0; cbn... destruct (Float.to_int f); cbn; trivial.
+ - destruct v0; cbn... destruct (Float.to_intu f); cbn; trivial.
+ (* intofsingle, intuofsingle *)
+ - destruct v0; cbn... destruct (Float32.to_int f); cbn; trivial.
+ - destruct v0; cbn... destruct (Float32.to_intu f); cbn; trivial.
+ (* singleofint, singleofintu *)
+ - destruct v0; cbn...
+ - destruct v0; cbn...
+ (* longoffloat, longuoffloat *)
+ - destruct v0; cbn... destruct (Float.to_long f); cbn; trivial.
+ - destruct v0; cbn... destruct (Float.to_longu f); cbn; trivial.
+ (* floatoflong, floatoflongu *)
+ - destruct v0; cbn...
+ - destruct v0; cbn...
+ (* longofsingle, longuofsingle *)
+ - destruct v0; cbn... destruct (Float32.to_long f); cbn; trivial.
+ - destruct v0; cbn... destruct (Float32.to_longu f); cbn; trivial.
+ (* singleoflong, singleoflongu *)
+ - destruct v0; cbn...
+ - destruct v0; cbn...
+ (* cmp *)
+ - destruct (eval_condition cond vl m)... destruct b...
+ (* extfz *)
+ - unfold extfz.
+ destruct (is_bitfield _ _).
+ + destruct v0; cbn; trivial.
+ + constructor.
+ (* extfs *)
+ - unfold extfs.
+ destruct (is_bitfield _ _).
+ + destruct v0; cbn; trivial.
+ + constructor.
+ (* extfzl *)
+ - unfold extfzl.
+ destruct (is_bitfieldl _ _).
+ + destruct v0; cbn; trivial.
+ + constructor.
+ (* extfsl *)
+ - unfold extfsl.
+ destruct (is_bitfieldl _ _).
+ + destruct v0; cbn; trivial.
+ + constructor.
+ (* insf *)
+ - unfold insf, bitfield_mask.
+ destruct (is_bitfield _ _).
+ + destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ + constructor.
+ (* insf *)
+ - unfold insfl, bitfield_mask.
+ destruct (is_bitfieldl _ _).
+ + destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ + constructor.
+ (* Osel *)
+ - unfold Val.select. destruct (eval_condition0 _ _ m).
+ + apply Val.normalize_type.
+ + constructor.
+ (* Oselimm *)
+ - unfold Val.select. destruct (eval_condition0 _ _ m).
+ + apply Val.normalize_type.
+ + constructor.
+ (* Osellimm *)
+ - unfold Val.select. destruct (eval_condition0 _ _ m).
+ + apply Val.normalize_type.
+ + constructor.
+Qed.
+
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Odiv | Odivl | Odivu | Odivlu
+ | Omod | Omodl | Omodu | Omodlu => true
+ | _ => false
+ end.
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op; destruct eq_operation; intros; cbn in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+Qed.
+End SOUNDNESS.
+
+(** * Manipulating and transforming operations *)
+
+(** Recognition of move operations. *)
+
+Definition is_move_operation
+ (A: Type) (op: operation) (args: list A) : option A :=
+ match op, args with
+ | Omove, arg :: nil => Some arg
+ | _, _ => None
+ end.
+
+Lemma is_move_operation_correct:
+ forall (A: Type) (op: operation) (args: list A) (a: A),
+ is_move_operation op args = Some a ->
+ op = Omove /\ args = a :: nil.
+Proof.
+ intros until a. unfold is_move_operation; destruct op;
+ try (intros; discriminate).
+ destruct args. intros; discriminate.
+ destruct args. intros. intuition congruence.
+ intros; discriminate.
+Qed.
+
+(** [negate_condition cond] returns a condition that is logically
+ equivalent to the negation of [cond]. *)
+
+Definition negate_condition (cond: condition): condition :=
+ match cond with
+ | Ccomp c => Ccomp(negate_comparison c)
+ | Ccompu c => Ccompu(negate_comparison c)
+ | Ccompimm c n => Ccompimm (negate_comparison c) n
+ | Ccompuimm c n => Ccompuimm (negate_comparison c) n
+ | Ccompl c => Ccompl(negate_comparison c)
+ | Ccomplu c => Ccomplu(negate_comparison c)
+ | Ccomplimm c n => Ccomplimm (negate_comparison c) n
+ | Ccompluimm c n => Ccompluimm (negate_comparison c) n
+ | Ccompf c => Cnotcompf c
+ | Cnotcompf c => Ccompf c
+ | Ccompfs c => Cnotcompfs c
+ | Cnotcompfs c => Ccompfs c
+ end.
+
+Lemma eval_negate_condition:
+ forall cond vl m,
+ eval_condition (negate_condition cond) vl m = option_map negb (eval_condition cond vl m).
+Proof.
+ intros. destruct cond; cbn.
+ repeat (destruct vl; auto). apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmplu_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto). apply Val.negate_cmplu_bool.
+ repeat (destruct vl; auto).
+ repeat (destruct vl; auto). destruct (Val.cmpf_bool c v v0) as [[]|]; auto.
+ repeat (destruct vl; auto).
+ repeat (destruct vl; auto). destruct (Val.cmpfs_bool c v v0) as [[]|]; auto.
+Qed.
+
+(** Shifting stack-relative references. This is used in [Stacking]. *)
+
+Definition shift_stack_addressing (delta: Z) (addr: addressing) :=
+ match addr with
+ | Ainstack ofs => Ainstack (Ptrofs.add ofs (Ptrofs.repr delta))
+ | _ => addr
+ end.
+
+Definition shift_stack_operation (delta: Z) (op: operation) :=
+ match op with
+ | Oaddrstack ofs => Oaddrstack (Ptrofs.add ofs (Ptrofs.repr delta))
+ | _ => op
+ end.
+
+Lemma type_shift_stack_addressing:
+ forall delta addr, type_of_addressing (shift_stack_addressing delta addr) = type_of_addressing addr.
+Proof.
+ intros. destruct addr; auto.
+Qed.
+
+Lemma type_shift_stack_operation:
+ forall delta op, type_of_operation (shift_stack_operation delta op) = type_of_operation op.
+Proof.
+ intros. destruct op; auto.
+Qed.
+
+Lemma eval_shift_stack_addressing:
+ forall F V (ge: Genv.t F V) sp addr vl delta,
+ eval_addressing ge (Vptr sp Ptrofs.zero) (shift_stack_addressing delta addr) vl =
+ eval_addressing ge (Vptr sp (Ptrofs.repr delta)) addr vl.
+Proof.
+ intros. destruct addr; cbn; auto. destruct vl; auto.
+ rewrite Ptrofs.add_zero_l, Ptrofs.add_commut; auto.
+Qed.
+
+Lemma eval_shift_stack_operation:
+ forall F V (ge: Genv.t F V) sp op vl m delta,
+ eval_operation ge (Vptr sp Ptrofs.zero) (shift_stack_operation delta op) vl m =
+ eval_operation ge (Vptr sp (Ptrofs.repr delta)) op vl m.
+Proof.
+ intros. destruct op; cbn; auto. destruct vl; auto.
+ rewrite Ptrofs.add_zero_l, Ptrofs.add_commut; auto.
+Qed.
+
+(** Offset an addressing mode [addr] by a quantity [delta], so that
+ it designates the pointer [delta] bytes past the pointer designated
+ by [addr]. May be undefined, in which case [None] is returned. *)
+
+Definition offset_addressing (addr: addressing) (delta: Z) : option addressing :=
+ match addr with
+ | Aindexed2 | Aindexed2XS _ => None
+ | Aindexed n => Some(Aindexed (Ptrofs.add n (Ptrofs.repr delta)))
+ | Aglobal id n => Some(Aglobal id (Ptrofs.add n (Ptrofs.repr delta)))
+ | Ainstack n => Some(Ainstack (Ptrofs.add n (Ptrofs.repr delta)))
+ end.
+
+Lemma eval_offset_addressing:
+ forall (F V: Type) (ge: Genv.t F V) sp addr args delta addr' v,
+ offset_addressing addr delta = Some addr' ->
+ eval_addressing ge sp addr args = Some v ->
+ Archi.ptr64 = false ->
+ eval_addressing ge sp addr' args = Some(Val.add v (Vint (Int.repr delta))).
+Proof.
+ intros.
+ assert (A: forall x n,
+ Val.offset_ptr x (Ptrofs.add n (Ptrofs.repr delta)) =
+ Val.add (Val.offset_ptr x n) (Vint (Int.repr delta))).
+ { intros; destruct x; cbn; auto. rewrite H1.
+ rewrite Ptrofs.add_assoc. f_equal; f_equal; f_equal. symmetry; auto with ptrofs. }
+ destruct addr; cbn in H; inv H; cbn in *; FuncInv; subst.
+- rewrite A; auto.
+- unfold Genv.symbol_address. destruct (Genv.find_symbol ge i); auto.
+ cbn. rewrite H1. f_equal; f_equal; f_equal. symmetry; auto with ptrofs.
+- rewrite A; auto.
+Qed.
+
+(** Operations that are so cheap to recompute that CSE should not factor them out. *)
+
+Definition is_trivial_op (op: operation) : bool :=
+ match op with
+ | Omove => true
+ | Ointconst n => Int.eq (Int.sign_ext 12 n) n
+ | Olongconst n => Int64.eq (Int64.sign_ext 12 n) n
+ | Oaddrstack _ => true
+ | _ => false
+ end.
+
+(** Operations that depend on the memory state. *)
+
+Definition cond_depends_on_memory (c: condition) : bool :=
+ match c with
+ | Ccompu _ | Ccompuimm _ _ => negb Archi.ptr64
+ | Ccomplu _ | Ccompluimm _ _ => Archi.ptr64
+ | _ => false
+ end.
+
+Lemma cond_depends_on_memory_correct:
+ forall c args m1 m2,
+ cond_depends_on_memory c = false ->
+ eval_condition c args m1 = eval_condition c args m2.
+Proof.
+ intros; destruct c; cbn; discriminate || reflexivity.
+Qed.
+
+Definition op_depends_on_memory (op: operation) : bool :=
+ match op with
+ | Ocmp (Ccompu _ | Ccompuimm _ _) => negb Archi.ptr64
+ | Ocmp (Ccomplu _ | Ccompluimm _ _) => Archi.ptr64
+
+ | Osel (Ccompu0 _) _ | Oselimm (Ccompu0 _) _ | Osellimm (Ccompu0 _) _ => negb Archi.ptr64
+ | Osel (Ccomplu0 _) _ | Oselimm (Ccomplu0 _) _ | Osellimm (Ccomplu0 _) _ => Archi.ptr64
+
+ | _ => false
+ end.
+
+Lemma op_depends_on_memory_correct:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ op_depends_on_memory op = false ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op; cbn; try congruence.
+ - destruct cond; cbn; try congruence;
+ intros SF; auto; rewrite ? negb_false_iff in SF;
+ unfold Val.cmpu_bool, Val.cmplu_bool; rewrite SF; reflexivity.
+ - destruct c0; cbn; try congruence;
+ intros SF; auto; rewrite ? negb_false_iff in SF;
+ unfold Val.cmpu_bool, Val.cmplu_bool; rewrite SF; reflexivity.
+ - destruct c0; cbn; try congruence;
+ intros SF; auto; rewrite ? negb_false_iff in SF;
+ unfold Val.cmpu_bool, Val.cmplu_bool; rewrite SF; reflexivity.
+ - destruct c0; cbn; try congruence;
+ intros SF; auto; rewrite ? negb_false_iff in SF;
+ unfold Val.cmpu_bool, Val.cmplu_bool; rewrite SF; reflexivity.
+Qed.
+
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op; cbn; try congruence.
+ - intros MEM; destruct cond; cbn; try congruence;
+ repeat (destruct args; cbn; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intros MEM; destruct c0; cbn; try congruence;
+ repeat (destruct args; cbn; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intros MEM; destruct c0; cbn; try congruence;
+ repeat (destruct args; cbn; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intros MEM; destruct c0; cbn; try congruence;
+ repeat (destruct args; cbn; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+(** Global variables mentioned in an operation or addressing mode *)
+
+Definition globals_addressing (addr: addressing) : list ident :=
+ match addr with
+ | Aglobal s ofs => s :: nil
+ | _ => nil
+ end.
+
+Definition globals_operation (op: operation) : list ident :=
+ match op with
+ | Oaddrsymbol s ofs => s :: nil
+ | _ => nil
+ end.
+
+(** * Invariance and compatibility properties. *)
+
+(** [eval_operation] and [eval_addressing] depend on a global environment
+ for resolving references to global symbols. We show that they give
+ the same results if a global environment is replaced by another that
+ assigns the same addresses to the same symbols. *)
+
+Section GENV_TRANSF.
+
+Variable F1 F2 V1 V2: Type.
+Variable ge1: Genv.t F1 V1.
+Variable ge2: Genv.t F2 V2.
+Hypothesis agree_on_symbols:
+ forall (s: ident), Genv.find_symbol ge2 s = Genv.find_symbol ge1 s.
+
+Lemma eval_addressing_preserved:
+ forall sp addr vl,
+ eval_addressing ge2 sp addr vl = eval_addressing ge1 sp addr vl.
+Proof.
+ intros.
+ unfold eval_addressing; destruct addr; auto. destruct vl; auto.
+ unfold Genv.symbol_address. rewrite agree_on_symbols; auto.
+Qed.
+
+Lemma eval_operation_preserved:
+ forall sp op vl m,
+ eval_operation ge2 sp op vl m = eval_operation ge1 sp op vl m.
+Proof.
+ intros.
+ unfold eval_operation; destruct op; auto. destruct vl; auto.
+ unfold Genv.symbol_address. rewrite agree_on_symbols; auto.
+Qed.
+
+End GENV_TRANSF.
+
+(** Compatibility of the evaluation functions with value injections. *)
+
+Section EVAL_COMPAT.
+
+Variable F1 F2 V1 V2: Type.
+Variable ge1: Genv.t F1 V1.
+Variable ge2: Genv.t F2 V2.
+Variable f: meminj.
+
+Variable m1: mem.
+Variable m2: mem.
+
+Hypothesis valid_pointer_inj:
+ forall b1 ofs b2 delta,
+ f b1 = Some(b2, delta) ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+
+Hypothesis weak_valid_pointer_inj:
+ forall b1 ofs b2 delta,
+ f b1 = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.weak_valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+
+Hypothesis weak_valid_pointer_no_overflow:
+ forall b1 ofs b2 delta,
+ f b1 = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ 0 <= Ptrofs.unsigned ofs + Ptrofs.unsigned (Ptrofs.repr delta) <= Ptrofs.max_unsigned.
+
+Hypothesis valid_different_pointers_inj:
+ forall b1 ofs1 b2 ofs2 b1' delta1 b2' delta2,
+ b1 <> b2 ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs1) = true ->
+ Mem.valid_pointer m1 b2 (Ptrofs.unsigned ofs2) = true ->
+ f b1 = Some (b1', delta1) ->
+ f b2 = Some (b2', delta2) ->
+ b1' <> b2' \/
+ Ptrofs.unsigned (Ptrofs.add ofs1 (Ptrofs.repr delta1)) <> Ptrofs.unsigned (Ptrofs.add ofs2 (Ptrofs.repr delta2)).
+
+Ltac InvInject :=
+ match goal with
+ | [ H: Val.inject _ (Vint _) _ |- _ ] =>
+ inv H; InvInject
+ | [ H: Val.inject _ (Vfloat _) _ |- _ ] =>
+ inv H; InvInject
+ | [ H: Val.inject _ (Vptr _ _) _ |- _ ] =>
+ inv H; InvInject
+ | [ H: Val.inject_list _ nil _ |- _ ] =>
+ inv H; InvInject
+ | [ H: Val.inject_list _ (_ :: _) _ |- _ ] =>
+ inv H; InvInject
+ | _ => idtac
+ end.
+
+Lemma eval_condition_inj:
+ forall cond vl1 vl2 b,
+ Val.inject_list f vl1 vl2 ->
+ eval_condition cond vl1 m1 = Some b ->
+ eval_condition cond vl2 m2 = Some b.
+Proof.
+ intros. destruct cond; cbn in H0; FuncInv; InvInject; cbn; auto.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+- eauto 3 using Val.cmpu_bool_inject, Mem.valid_pointer_implies.
+- inv H3; cbn in H0; inv H0; auto.
+- eauto 3 using Val.cmpu_bool_inject, Mem.valid_pointer_implies.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+- eauto 3 using Val.cmplu_bool_inject, Mem.valid_pointer_implies.
+- inv H3; cbn in H0; inv H0; auto.
+- eauto 3 using Val.cmplu_bool_inject, Mem.valid_pointer_implies.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+- inv H3; inv H2; cbn in H0; inv H0; auto.
+Qed.
+
+Lemma eval_condition0_inj:
+ forall cond v1 v2 b,
+ Val.inject f v1 v2 ->
+ eval_condition0 cond v1 m1 = Some b ->
+ eval_condition0 cond v2 m2 = Some b.
+Proof.
+ intros. destruct cond; cbn in H0; FuncInv; InvInject; cbn; auto.
+ - inv H; cbn in *; congruence.
+ - eauto 3 using Val.cmpu_bool_inject, Mem.valid_pointer_implies.
+ - inv H; cbn in *; congruence.
+ - eauto 3 using Val.cmplu_bool_inject, Mem.valid_pointer_implies.
+Qed.
+
+Ltac TrivialExists :=
+ match goal with
+ | [ |- exists v2, Some ?v1 = Some v2 /\ Val.inject _ _ v2 ] =>
+ exists v1; split; auto
+ | _ => idtac
+ end.
+
+Lemma eval_operation_inj:
+ forall op sp1 vl1 sp2 vl2 v1,
+ (forall id ofs,
+ In id (globals_operation op) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_operation ge1 sp1 op vl1 m1 = Some v1 ->
+ exists v2, eval_operation ge2 sp2 op vl2 m2 = Some v2 /\ Val.inject f v1 v2.
+Proof.
+ intros until v1; intros GL; intros. destruct op; cbn in H1; cbn; FuncInv; InvInject; TrivialExists.
+ (* addrsymbol *)
+ - apply GL; cbn; auto.
+ (* addrstack *)
+ - apply Val.offset_ptr_inject; auto.
+ (* castsigned *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* add, addimm *)
+ - apply Val.add_inject; auto.
+ - apply Val.add_inject; auto.
+ (* addx, addximm *)
+ - apply Val.add_inject; trivial.
+ inv H4; inv H2; cbn; try destruct (Int.ltu _ _); cbn; auto.
+ - inv H4; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ (* neg, sub *)
+ - inv H4; cbn; auto.
+ - apply Val.sub_inject; auto.
+ (* revsubimm, revsubx, revsubximm *)
+ - inv H4; cbn; trivial.
+ - apply Val.sub_inject; trivial.
+ inv H4; inv H2; cbn; try destruct (Int.ltu _ _); cbn; auto.
+ - inv H4; cbn; try destruct (Int.ltu _ _); cbn; auto.
+ (* mul, mulimm, mulhs, mulhu *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* div, divu *)
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (_ || _); inv H2.
+ TrivialExists.
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (Int.eq i0 Int.zero); inv H2. TrivialExists.
+ (* mod, modu *)
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (_ || _); inv H2.
+ TrivialExists.
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (Int.eq i0 Int.zero); inv H2. TrivialExists.
+ (* and, andimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* nand, nandimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* or, orimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* nor, norimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* xor, xorimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* nxor, nxorimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* not *)
+ - inv H4; cbn; auto.
+ (* andn, andnimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* orn, ornimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* shl, shlimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int.iwordsize); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int.iwordsize); auto.
+ (* shr, shrimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int.iwordsize); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int.iwordsize); auto.
+ (* shru, shruimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int.iwordsize); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int.iwordsize); auto.
+ (* shrx *)
+ - inv H4; cbn; auto.
+ destruct (Int.ltu n (Int.repr 31)); inv H; cbn; auto.
+ (* rorimm *)
+ - inv H4; cbn; auto.
+ (* madd, maddim *)
+ - inv H2; inv H3; inv H4; cbn; auto.
+ - inv H2; inv H4; cbn; auto.
+ (* msub *)
+ - apply Val.sub_inject; auto.
+ inv H3; inv H2; cbn; auto.
+ (* makelong, highlong, lowlong *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* cast32 *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* addl, addlimm *)
+ - apply Val.addl_inject; auto.
+ - apply Val.addl_inject; auto.
+ (* addxl, addxlimm *)
+ - apply Val.addl_inject; auto.
+ inv H4; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ - inv H4; cbn; trivial.
+ destruct (Int.ltu _ _); cbn; trivial.
+ (* negl, subl *)
+ - inv H4; cbn; auto.
+ - apply Val.subl_inject; auto.
+ inv H4; inv H2; cbn; trivial;
+ destruct (Int.ltu _ _); cbn; trivial.
+ - inv H4; cbn; trivial;
+ destruct (Int.ltu _ _); cbn; trivial.
+ - inv H4; cbn; auto.
+ - apply Val.subl_inject; auto.
+ (* mull, mullhs, mullhu *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* divl, divlu *)
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (_ || _); inv H2.
+ TrivialExists.
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (Int64.eq i0 Int64.zero); inv H2. TrivialExists.
+ (* modl, modlu *)
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (_ || _); inv H2.
+ TrivialExists.
+ - inv H4; inv H3; cbn in H1; inv H1. cbn.
+ destruct (Int64.eq i0 Int64.zero); inv H2. TrivialExists.
+ (* andl, andlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* nandl, nandlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* orl, orlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* norl, norlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* xorl, xorlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* nxorl, nxorlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* notl *)
+ - inv H4; cbn; auto.
+ (* andnl, andnlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* ornl, ornlimm *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; cbn; auto.
+ (* shll, shllimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int64.iwordsize'); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int64.iwordsize'); auto.
+ (* shr, shrimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int64.iwordsize'); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int64.iwordsize'); auto.
+ (* shru, shruimm *)
+ - inv H4; inv H2; cbn; auto. destruct (Int.ltu i0 Int64.iwordsize'); auto.
+ - inv H4; cbn; auto. destruct (Int.ltu n Int64.iwordsize'); auto.
+ (* shrx *)
+ - inv H4; cbn; auto.
+ destruct (Int.ltu n (Int.repr 63)); cbn; auto.
+
+ (* maddl, maddlimm *)
+ - apply Val.addl_inject; auto.
+ inv H2; inv H3; inv H4; cbn; auto.
+ - apply Val.addl_inject; auto.
+ inv H4; inv H2; cbn; auto.
+ (* msubl, msublimm *)
+ - apply Val.subl_inject; auto.
+ inv H2; inv H3; inv H4; cbn; auto.
+
+ (* negf, absf *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* addf, subf *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* mulf, divf *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* minf, maxf *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* fmaddf, fmsubf *)
+ - inv H4; inv H3; inv H2; cbn; auto.
+ - inv H4; inv H3; inv H2; cbn; auto.
+ (* negfs, absfs *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* addfs, subfs *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* mulfs, divfs *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* minfs, maxfs *)
+ - inv H4; inv H2; cbn; auto.
+ - inv H4; inv H2; cbn; auto.
+ (* invfs *)
+ - inv H4; cbn; auto.
+ (* fmaddfs, fmsubfs *)
+ - inv H4; inv H3; inv H2; cbn; auto.
+ - inv H4; inv H3; inv H2; cbn; auto.
+ (* singleoffloat, floatofsingle *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* intoffloat, intuoffloat *)
+ - inv H4; cbn; auto. destruct (Float.to_int f0); cbn; auto.
+ - inv H4; cbn; auto. destruct (Float.to_intu f0); cbn; auto.
+ (* intofsingle, intuofsingle *)
+ - inv H4; cbn; auto. destruct (Float32.to_int f0); cbn; auto.
+ - inv H4; cbn; auto. destruct (Float32.to_intu f0); cbn; auto.
+ (* singleofint, singleofintu *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* longoffloat, longuoffloat *)
+ - inv H4; cbn; auto. destruct (Float.to_long f0); cbn; auto.
+ - inv H4; cbn; auto. destruct (Float.to_longu f0); cbn; auto.
+ (* floatoflong, floatoflongu *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* longofsingle, longuofsingle *)
+ - inv H4; cbn; auto. destruct (Float32.to_long f0); cbn; auto.
+ - inv H4; cbn; auto. destruct (Float32.to_longu f0); cbn; auto.
+ (* singleoflong, singleoflongu *)
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
+ (* cmp *)
+ - subst v1. destruct (eval_condition cond vl1 m1) eqn:?.
+ exploit eval_condition_inj; eauto. intros EQ; rewrite EQ.
+ destruct b; cbn; constructor.
+ cbn; constructor.
+
+ (* extfz *)
+ - unfold extfz.
+ destruct (is_bitfield _ _).
+ + inv H4; trivial.
+ + trivial.
+
+ (* extfs *)
+ - unfold extfs.
+ destruct (is_bitfield _ _).
+ + inv H4; trivial.
+ + trivial.
+
+ (* extfzl *)
+ - unfold extfzl.
+ destruct (is_bitfieldl _ _).
+ + inv H4; trivial.
+ + trivial.
+
+ (* extfsl *)
+ - unfold extfsl.
+ destruct (is_bitfieldl _ _).
+ + inv H4; trivial.
+ + trivial.
+
+ (* insf *)
+ - unfold insf.
+ destruct (is_bitfield _ _).
+ + inv H4; inv H2; trivial.
+ cbn. destruct (Int.ltu _ _); trivial.
+ cbn. trivial.
+ + trivial.
+
+ (* insfl *)
+ - unfold insfl.
+ destruct (is_bitfieldl _ _).
+ + inv H4; inv H2; trivial.
+ cbn. destruct (Int.ltu _ _); trivial.
+ cbn. trivial.
+ + trivial.
+
+ (* Osel *)
+ - apply Val.select_inject; trivial.
+ destruct (eval_condition0 c0 v2 m1) eqn:Hcond.
+ + right.
+ symmetry.
+ eapply eval_condition0_inj; eassumption.
+ + left. trivial.
+
+ (* Oselimm *)
+ - apply Val.select_inject; trivial.
+ destruct (eval_condition0 _ _ _) eqn:Hcond.
+ + right.
+ symmetry.
+ eapply eval_condition0_inj; eassumption.
+ + left. trivial.
+
+ (* Osellimm *)
+ - apply Val.select_inject; trivial.
+ destruct (eval_condition0 _ _ _) eqn:Hcond.
+ + right.
+ symmetry.
+ eapply eval_condition0_inj; eassumption.
+ + left. trivial.
+Qed.
+
+Lemma eval_addressing_inj:
+ forall addr sp1 vl1 sp2 vl2 v1,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = Some v1 ->
+ exists v2, eval_addressing ge2 sp2 addr vl2 = Some v2 /\ Val.inject f v1 v2.
+Proof.
+ intros. destruct addr; cbn in H2; cbn; FuncInv; InvInject; TrivialExists.
+ - apply Val.addl_inject; trivial.
+ destruct v0; destruct v'0; cbn; trivial; destruct (Int.ltu _ _); cbn; trivial; inv H3.
+ apply Val.inject_long.
+ - apply Val.addl_inject; auto.
+ - apply Val.offset_ptr_inject; auto.
+ - apply H; cbn; auto.
+ - apply Val.offset_ptr_inject; auto.
+Qed.
+
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; cbn in *.
+ 1,2: inv Hinjvl; trivial;
+ inv H0; trivial;
+ inv H2; trivial;
+ discriminate.
+ 2,3: inv Hinjvl; trivial; discriminate.
+ inv Hinjvl; trivial; inv H0; trivial;
+ inv H; trivial; discriminate.
+Qed.
+
+End EVAL_COMPAT.
+
+(** Compatibility of the evaluation functions with the ``is less defined'' relation over values. *)
+
+Section EVAL_LESSDEF.
+
+Variable F V: Type.
+Variable genv: Genv.t F V.
+
+Remark valid_pointer_extends:
+ forall m1 m2, Mem.extends m1 m2 ->
+ forall b1 ofs b2 delta,
+ Some(b1, 0) = Some(b2, delta) ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+Proof.
+ intros. inv H0. rewrite Ptrofs.add_zero. eapply Mem.valid_pointer_extends; eauto.
+Qed.
+
+Remark weak_valid_pointer_extends:
+ forall m1 m2, Mem.extends m1 m2 ->
+ forall b1 ofs b2 delta,
+ Some(b1, 0) = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ Mem.weak_valid_pointer m2 b2 (Ptrofs.unsigned (Ptrofs.add ofs (Ptrofs.repr delta))) = true.
+Proof.
+ intros. inv H0. rewrite Ptrofs.add_zero. eapply Mem.weak_valid_pointer_extends; eauto.
+Qed.
+
+Remark weak_valid_pointer_no_overflow_extends:
+ forall m1 b1 ofs b2 delta,
+ Some(b1, 0) = Some(b2, delta) ->
+ Mem.weak_valid_pointer m1 b1 (Ptrofs.unsigned ofs) = true ->
+ 0 <= Ptrofs.unsigned ofs + Ptrofs.unsigned (Ptrofs.repr delta) <= Ptrofs.max_unsigned.
+Proof.
+ intros. inv H. rewrite Z.add_0_r. apply Ptrofs.unsigned_range_2.
+Qed.
+
+Remark valid_different_pointers_extends:
+ forall m1 b1 ofs1 b2 ofs2 b1' delta1 b2' delta2,
+ b1 <> b2 ->
+ Mem.valid_pointer m1 b1 (Ptrofs.unsigned ofs1) = true ->
+ Mem.valid_pointer m1 b2 (Ptrofs.unsigned ofs2) = true ->
+ Some(b1, 0) = Some (b1', delta1) ->
+ Some(b2, 0) = Some (b2', delta2) ->
+ b1' <> b2' \/
+ Ptrofs.unsigned(Ptrofs.add ofs1 (Ptrofs.repr delta1)) <> Ptrofs.unsigned(Ptrofs.add ofs2 (Ptrofs.repr delta2)).
+Proof.
+ intros. inv H2; inv H3. auto.
+Qed.
+
+Lemma eval_condition_lessdef:
+ forall cond vl1 vl2 b m1 m2,
+ Val.lessdef_list vl1 vl2 ->
+ Mem.extends m1 m2 ->
+ eval_condition cond vl1 m1 = Some b ->
+ eval_condition cond vl2 m2 = Some b.
+Proof.
+ intros. eapply eval_condition_inj with (f := fun b => Some(b, 0)) (m1 := m1).
+ apply valid_pointer_extends; auto.
+ apply weak_valid_pointer_extends; auto.
+ apply weak_valid_pointer_no_overflow_extends.
+ apply valid_different_pointers_extends; auto.
+ rewrite <- val_inject_list_lessdef. eauto. auto.
+Qed.
+
+Lemma eval_operation_lessdef:
+ forall sp op vl1 vl2 v1 m1 m2,
+ Val.lessdef_list vl1 vl2 ->
+ Mem.extends m1 m2 ->
+ eval_operation genv sp op vl1 m1 = Some v1 ->
+ exists v2, eval_operation genv sp op vl2 m2 = Some v2 /\ Val.lessdef v1 v2.
+Proof.
+ intros. rewrite val_inject_list_lessdef in H.
+ assert (exists v2 : val,
+ eval_operation genv sp op vl2 m2 = Some v2
+ /\ Val.inject (fun b => Some(b, 0)) v1 v2).
+ eapply eval_operation_inj with (m1 := m1) (sp1 := sp).
+ apply valid_pointer_extends; auto.
+ apply weak_valid_pointer_extends; auto.
+ apply weak_valid_pointer_no_overflow_extends.
+ apply valid_different_pointers_extends; auto.
+ intros. apply val_inject_lessdef. auto.
+ apply val_inject_lessdef; auto.
+ eauto.
+ auto.
+ destruct H2 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
+Qed.
+
+Lemma eval_addressing_lessdef:
+ forall sp addr vl1 vl2 v1,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = Some v1 ->
+ exists v2, eval_addressing genv sp addr vl2 = Some v2 /\ Val.lessdef v1 v2.
+Proof.
+ intros. rewrite val_inject_list_lessdef in H.
+ assert (exists v2 : val,
+ eval_addressing genv sp addr vl2 = Some v2
+ /\ Val.inject (fun b => Some(b, 0)) v1 v2).
+ eapply eval_addressing_inj with (sp1 := sp).
+ intros. rewrite <- val_inject_lessdef; auto.
+ rewrite <- val_inject_lessdef; auto.
+ eauto. auto.
+ destruct H1 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
+Qed.
+
+
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros until vl2. intros Hlessdef Heval1.
+ destruct addr; cbn in *.
+ 1, 2, 4, 5: inv Hlessdef; trivial;
+ inv H0; trivial;
+ inv H2; trivial;
+ discriminate.
+ inv Hlessdef; trivial.
+ inv H0; trivial.
+ discriminate.
+Qed.
+
+End EVAL_LESSDEF.
+
+(** Compatibility of the evaluation functions with memory injections. *)
+
+Section EVAL_INJECT.
+
+Variable F V: Type.
+Variable genv: Genv.t F V.
+Variable f: meminj.
+Hypothesis globals: meminj_preserves_globals genv f.
+Variable sp1: block.
+Variable sp2: block.
+Variable delta: Z.
+Hypothesis sp_inj: f sp1 = Some(sp2, delta).
+
+Remark symbol_address_inject:
+ forall id ofs, Val.inject f (Genv.symbol_address genv id ofs) (Genv.symbol_address genv id ofs).
+Proof.
+ intros. unfold Genv.symbol_address. destruct (Genv.find_symbol genv id) eqn:?; auto.
+ exploit (proj1 globals); eauto. intros.
+ econstructor; eauto. rewrite Ptrofs.add_zero; auto.
+Qed.
+
+Lemma eval_condition_inject:
+ forall cond vl1 vl2 b m1 m2,
+ Val.inject_list f vl1 vl2 ->
+ Mem.inject f m1 m2 ->
+ eval_condition cond vl1 m1 = Some b ->
+ eval_condition cond vl2 m2 = Some b.
+Proof.
+ intros. eapply eval_condition_inj with (f := f) (m1 := m1); eauto.
+ intros; eapply Mem.valid_pointer_inject_val; eauto.
+ intros; eapply Mem.weak_valid_pointer_inject_val; eauto.
+ intros; eapply Mem.weak_valid_pointer_inject_no_overflow; eauto.
+ intros; eapply Mem.different_pointers_inject; eauto.
+Qed.
+
+Lemma eval_addressing_inject:
+ forall addr vl1 vl2 v1,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = Some v1 ->
+ exists v2,
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = Some v2
+ /\ Val.inject f v1 v2.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
+Lemma eval_operation_inject:
+ forall op vl1 vl2 v1 m1 m2,
+ Val.inject_list f vl1 vl2 ->
+ Mem.inject f m1 m2 ->
+ eval_operation genv (Vptr sp1 Ptrofs.zero) op vl1 m1 = Some v1 ->
+ exists v2,
+ eval_operation genv (Vptr sp2 Ptrofs.zero) (shift_stack_operation delta op) vl2 m2 = Some v2
+ /\ Val.inject f v1 v2.
+Proof.
+ intros.
+ rewrite eval_shift_stack_operation. cbn.
+ eapply eval_operation_inj with (sp1 := Vptr sp1 Ptrofs.zero) (m1 := m1); eauto.
+ intros; eapply Mem.valid_pointer_inject_val; eauto.
+ intros; eapply Mem.weak_valid_pointer_inject_val; eauto.
+ intros; eapply Mem.weak_valid_pointer_inject_no_overflow; eauto.
+ intros; eapply Mem.different_pointers_inject; eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
+End EVAL_INJECT.
+
+(** * Handling of builtin arguments *)
+
+Definition builtin_arg_ok_1
+ (A: Type) (ba: builtin_arg A) (c: builtin_arg_constraint) :=
+ match c, ba with
+ | OK_all, _ => true
+ | OK_const, (BA_int _ | BA_long _ | BA_float _ | BA_single _) => true
+ | OK_addrstack, BA_addrstack _ => true
+ | OK_addressing, BA_addrstack _ => true
+ | OK_addressing, BA_addptr (BA _) (BA_int _) => true
+ | OK_addressing, BA_addptr (BA _) (BA_long _) => true
+ | _, _ => false
+ end.
+
+Definition builtin_arg_ok
+ (A: Type) (ba: builtin_arg A) (c: builtin_arg_constraint) :=
+ match ba with
+ | (BA _ | BA_splitlong (BA _) (BA _)) => true
+ | _ => builtin_arg_ok_1 ba c
+ end.
diff --git a/kvx/OpWeights.ml b/kvx/OpWeights.ml
new file mode 100644
index 00000000..23c2e5d3
--- /dev/null
+++ b/kvx/OpWeights.ml
@@ -0,0 +1,115 @@
+open Op;;
+open PostpassSchedulingOracle;;
+open PrepassSchedulingOracleDeps;;
+
+module KV3 =
+ struct
+let resource_bounds = PostpassSchedulingOracle.resource_bounds;;
+let nr_non_pipelined_units = 0;;
+
+let rec nlist_rec x l = function
+ | 0 -> l
+ | n when n > 0 -> nlist_rec x (x :: l) (n-1)
+ | _ -> failwith "nlist_rec";;
+let nlist x n = nlist_rec x [] n;;
+
+let bogus_register = Machregs.R0;;
+let bogus_inputs n = nlist bogus_register n;;
+
+let insns_of_op (op : operation) (nargs : int) =
+ match Asmblockgen.transl_op op
+ (bogus_inputs nargs) bogus_register [] with
+ | Errors.Error msg -> failwith "OpWeights.insns_of_op"
+ | Errors.OK insns -> insns;;
+
+let insn_of_op op nargs =
+ match insns_of_op op nargs with
+ | [] -> failwith "OpWeights.insn_of_op"
+ | h::_ -> h;;
+
+let insns_of_cond (cond : condition) (nargs : int) =
+ match Asmblockgen.transl_cond_op cond
+ Asmvliw.GPR0 (bogus_inputs nargs) [] with
+ | Errors.Error msg -> failwith "OpWeights.insns_of_cond"
+ | Errors.OK insns -> insns;;
+
+let insn_of_cond cond nargs =
+ match insns_of_cond cond nargs with
+ | [] -> failwith "OpWeights.insn_of_cond"
+ | h::_ -> h;;
+
+let insns_of_load trap chunk addressing (nargs : int) =
+ match Asmblockgen.transl_load trap chunk addressing
+ (bogus_inputs nargs) bogus_register [] with
+ | Errors.Error msg -> failwith "OpWeights.insns_of_load"
+ | Errors.OK insns -> insns;;
+
+let insn_of_load trap chunk addressing nargs =
+ match insns_of_load trap chunk addressing nargs with
+ | [] -> failwith "OpWeights.insn_of_load"
+ | h::_ -> h;;
+
+let insns_of_store chunk addressing (nargs : int) =
+ match Asmblockgen.transl_store chunk addressing
+ (bogus_inputs nargs) bogus_register [] with
+ | Errors.Error msg -> failwith "OpWeights.insns_of_store"
+ | Errors.OK insns -> insns;;
+
+let insn_of_store chunk addressing nargs =
+ match insns_of_store chunk addressing nargs with
+ | [] -> failwith "OpWeights.insn_of_store"
+ | h::_ -> h;;
+
+let latency_of_op (op : operation) (nargs : int) =
+ let insn = insn_of_op op nargs in
+ let record = basic_rec insn in
+ let latency = real_inst_to_latency record.inst in
+ latency;;
+
+let resources_of_op (op : operation) (nargs : int) =
+ let insn = insn_of_op op nargs in
+ let record = basic_rec insn in
+ rec_to_usage record;;
+
+let non_pipelined_resources_of_op (op : operation) (nargs : int) = [| |]
+
+let resources_of_cond (cond : condition) (nargs : int) =
+ let insn = insn_of_cond cond nargs in
+ let record = basic_rec insn in
+ rec_to_usage record;;
+
+let latency_of_load trap chunk (addr : addressing) (nargs : int) = 3;;
+let latency_of_call _ _ = 6;;
+
+let resources_of_load trap chunk addressing nargs =
+ let insn = insn_of_load trap chunk addressing nargs in
+ let record = basic_rec insn in
+ rec_to_usage record;;
+
+let resources_of_store chunk addressing nargs =
+ let insn = insn_of_store chunk addressing nargs in
+ let record = basic_rec insn in
+ rec_to_usage record;;
+
+let resources_of_call _ _ = resource_bounds;;
+let resources_of_builtin _ = resource_bounds;;
+ end;;
+
+let get_opweights () : opweights =
+ match !Clflags.option_mtune with
+ | "kv3" | "" ->
+ {
+ pipelined_resource_bounds = KV3.resource_bounds;
+ nr_non_pipelined_units = KV3.nr_non_pipelined_units;
+ latency_of_op = KV3.latency_of_op;
+ resources_of_op = KV3.resources_of_op;
+ non_pipelined_resources_of_op = KV3.non_pipelined_resources_of_op;
+ latency_of_load = KV3.latency_of_load;
+ resources_of_load = KV3.resources_of_load;
+ resources_of_store = KV3.resources_of_store;
+ resources_of_cond = KV3.resources_of_cond;
+ latency_of_call = KV3.latency_of_call;
+ resources_of_call = KV3.resources_of_call;
+ resources_of_builtin = KV3.resources_of_builtin
+ }
+ | xxx -> failwith (Printf.sprintf "unknown -mtune: %s" xxx);;
diff --git a/kvx/Peephole.v b/kvx/Peephole.v
new file mode 100644
index 00000000..5adb823b
--- /dev/null
+++ b/kvx/Peephole.v
@@ -0,0 +1,158 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import Asmvliw.
+Require Import Values.
+Require Import Integers.
+Require Import AST.
+Require Compopts.
+
+Definition gpreg_q_list : list gpreg_q :=
+R0R1 :: R2R3 :: R4R5 :: R6R7 :: R8R9
+:: R10R11 :: R12R13 :: R14R15 :: R16R17 :: R18R19
+:: R20R21 :: R22R23 :: R24R25 :: R26R27 :: R28R29
+:: R30R31 :: R32R33 :: R34R35 :: R36R37 :: R38R39
+:: R40R41 :: R42R43 :: R44R45 :: R46R47 :: R48R49
+:: R50R51 :: R52R53 :: R54R55 :: R56R57 :: R58R59
+:: R60R61 :: R62R63 :: nil.
+
+Definition gpreg_o_list : list gpreg_o :=
+R0R1R2R3 :: R4R5R6R7 :: R8R9R10R11 :: R12R13R14R15
+:: R16R17R18R19 :: R20R21R22R23 :: R24R25R26R27 :: R28R29R30R31
+:: R32R33R34R35 :: R36R37R38R39 :: R40R41R42R43 :: R44R45R46R47
+:: R48R49R50R51 :: R52R53R54R55 :: R56R57R58R59 :: R60R61R62R63 :: nil.
+
+Fixpoint gpreg_q_search_rec r0 r1 l :=
+ match l with
+ | h :: t =>
+ let (s0, s1) := gpreg_q_expand h in
+ if (gpreg_eq r0 s0) && (gpreg_eq r1 s1)
+ then Some h
+ else gpreg_q_search_rec r0 r1 t
+ | nil => None
+ end.
+
+Fixpoint gpreg_o_search_rec r0 r1 r2 r3 l :=
+ match l with
+ | h :: t =>
+ match gpreg_o_expand h with
+ | (((s0, s1), s2), s3) =>
+ if (gpreg_eq r0 s0) && (gpreg_eq r1 s1) &&
+ (gpreg_eq r2 s2) && (gpreg_eq r3 s3)
+ then Some h
+ else gpreg_o_search_rec r0 r1 r2 r3 t
+ end
+ | nil => None
+ end.
+
+Definition gpreg_q_search (r0 : gpreg) (r1 : gpreg) : option gpreg_q :=
+ gpreg_q_search_rec r0 r1 gpreg_q_list.
+
+Definition gpreg_o_search r0 r1 r2 r3 : option gpreg_o :=
+ gpreg_o_search_rec r0 r1 r2 r3 gpreg_o_list.
+
+Parameter print_found_store: forall A, Z -> A -> A.
+
+Definition coalesce_octuples := true.
+
+Fixpoint coalesce_mem (insns : list basic) : list basic :=
+ match insns with
+ | nil => nil
+ | h0 :: t0 =>
+ match t0 with
+ | h1 :: t1 =>
+ match h0, h1 with
+ | (PStoreRRO Psd_a rs0 ra0 ofs0),
+ (PStoreRRO Psd_a rs1 ra1 ofs1) =>
+ match gpreg_q_search rs0 rs1 with
+ | Some rs0rs1 =>
+ let zofs0 := Ptrofs.signed ofs0 in
+ let zofs1 := Ptrofs.signed ofs1 in
+ if (zofs1 =? zofs0 + 8) && (ireg_eq ra0 ra1)
+ then
+ if coalesce_octuples
+ then
+ match t1 with
+ | (PStoreRRO Psd_a rs2 ra2 ofs2) ::
+ (PStoreRRO Psd_a rs3 ra3 ofs3) :: t3 =>
+ match gpreg_o_search rs0 rs1 rs2 rs3 with
+ | Some octuple =>
+ let zofs2 := Ptrofs.signed ofs2 in
+ let zofs3 := Ptrofs.signed ofs3 in
+ if (zofs2 =? zofs0 + 16) && (ireg_eq ra0 ra2) &&
+ (zofs3 =? zofs0 + 24) && (ireg_eq ra0 ra3)
+ then (PStore (PStoreORRO octuple ra0 ofs0)) :: Pnop :: Pnop :: Pnop :: (coalesce_mem t3)
+ else (PStore (PStoreQRRO rs0rs1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ | None => (PStore (PStoreQRRO rs0rs1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ end
+ | _ => (PStore (PStoreQRRO rs0rs1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ end
+ else (PStore (PStoreQRRO rs0rs1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ else h0 :: (coalesce_mem t0)
+ | None => h0 :: (coalesce_mem t0)
+ end
+
+ | (PLoad (PLoadRRO TRAP Pld_a rd0 ra0 ofs0)),
+ (PLoad (PLoadRRO TRAP Pld_a rd1 ra1 ofs1)) =>
+ match gpreg_q_search rd0 rd1 with
+ | Some rd0rd1 =>
+ let zofs0 := Ptrofs.signed ofs0 in
+ let zofs1 := Ptrofs.signed ofs1 in
+ if (zofs1 =? zofs0 + 8) && (ireg_eq ra0 ra1) && negb (ireg_eq ra0 rd0)
+ then
+ if coalesce_octuples
+ then
+ match t1 with
+ | (PLoad (PLoadRRO TRAP Pld_a rd2 ra2 ofs2)) ::
+ (PLoad (PLoadRRO TRAP Pld_a rd3 ra3 ofs3)) :: t3 =>
+ match gpreg_o_search rd0 rd1 rd2 rd3 with
+ | Some octuple =>
+ let zofs2 := Ptrofs.signed ofs2 in
+ let zofs3 := Ptrofs.signed ofs3 in
+ if (zofs2 =? zofs0 + 16) && (ireg_eq ra0 ra2) &&
+ (zofs3 =? zofs0 + 24) && (ireg_eq ra0 ra3) &&
+ negb (ireg_eq ra0 rd1) && negb (ireg_eq ra0 rd2)
+ then (PLoad (PLoadORRO octuple ra0 ofs0)) :: Pnop :: Pnop :: Pnop :: (coalesce_mem t3)
+ else (PLoad (PLoadQRRO rd0rd1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ | None => (PLoad (PLoadQRRO rd0rd1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ end
+ | _ => (PLoad (PLoadQRRO rd0rd1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ end
+ else (PLoad (PLoadQRRO rd0rd1 ra0 ofs0)) :: Pnop :: (coalesce_mem t1)
+ else h0 :: (coalesce_mem t0)
+ | None => h0 :: (coalesce_mem t0)
+ end
+ | _, _ => h0 :: (coalesce_mem t0)
+ end
+ | nil => h0 :: nil
+ end
+ end.
+
+Definition optimize_body (insns : list basic) :=
+ if Compopts.optim_coalesce_mem tt
+ then coalesce_mem insns
+ else insns.
+
+Program Definition optimize_bblock (bb : bblock) :=
+ let optimized := optimize_body (body bb) in
+ let wf_ok := wf_bblockb optimized (exit bb) in
+ {| header := header bb;
+ body := if wf_ok then optimized else (body bb);
+ exit := exit bb |}.
+Next Obligation.
+ destruct (wf_bblockb (optimize_body (body bb))) eqn:Rwf.
+ - rewrite Rwf. cbn. trivial.
+ - exact (correct bb).
+Qed.
diff --git a/kvx/PostpassScheduling.v b/kvx/PostpassScheduling.v
new file mode 100644
index 00000000..08e640c6
--- /dev/null
+++ b/kvx/PostpassScheduling.v
@@ -0,0 +1,526 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Implementation (and basic properties) of the verified postpass scheduler *)
+
+Require Import Coqlib Errors AST Integers.
+Require Import Asmblock Axioms Memory Globalenvs.
+Require Import Asmblockdeps Asmblockgenproof0 Asmblockprops.
+Require Peephole.
+Require Import Lia.
+
+Local Open Scope error_monad_scope.
+
+(** * Oracle taking as input a basic block,
+ returns a scheduled list of bundles *)
+Axiom schedule: bblock -> (list (list basic)) * option control.
+
+Extract Constant schedule => "PostpassSchedulingOracle.schedule".
+
+(** * Concat all bundles into one big basic block *)
+
+(* Lemmas necessary for defining concat_all *)
+Lemma app_nonil {A: Type} (l l': list A) : l <> nil -> l ++ l' <> nil.
+Proof.
+ intros. destruct l; simpl.
+ - contradiction.
+ - discriminate.
+Qed.
+
+Lemma app_nonil2 {A: Type} : forall (l l': list A), l' <> nil -> l ++ l' <> nil.
+Proof.
+ destruct l.
+ - intros. simpl; auto.
+ - intros. rewrite <- app_comm_cons. discriminate.
+Qed.
+
+Definition check_size bb :=
+ if zlt Ptrofs.max_unsigned (size bb)
+ then Error (msg "PostpassSchedulingproof.check_size")
+ else OK tt.
+
+Program Definition concat2 (bb bb': bblock) : res bblock :=
+ do ch <- check_size bb;
+ do ch' <- check_size bb';
+ match (exit bb) with
+ | None =>
+ match (header bb') with
+ | nil =>
+ match (exit bb') with
+ | Some (PExpand (Pbuiltin _ _ _)) => Error (msg "PostpassSchedulingproof.concat2: builtin not alone")
+ | _ => OK {| header := header bb; body := body bb ++ body bb'; exit := exit bb' |}
+ end
+ | _ => Error (msg "PostpassSchedulingproof.concat2")
+ end
+ | _ => Error (msg "PostpassSchedulingproof.concat2")
+ end.
+Next Obligation.
+ apply wf_bblock_refl. constructor.
+ - destruct bb' as [hd' bdy' ex' WF']. destruct bb as [hd bdy ex WF]. simpl in *.
+ apply wf_bblock_refl in WF'. apply wf_bblock_refl in WF.
+ inversion_clear WF'. inversion_clear WF. clear H1 H3.
+ inversion H2; inversion H0.
+ + left. apply app_nonil. auto.
+ + right. auto.
+ + left. apply app_nonil2. auto.
+ + right. auto.
+ - unfold builtin_alone. intros. rewrite H0 in H.
+ assert (Some (PExpand (Pbuiltin ef args res)) <> Some (PExpand (Pbuiltin ef args res))).
+ apply (H ef args res). contradict H1. auto.
+Defined.
+
+Lemma concat2_zlt_size:
+ forall a b bb,
+ concat2 a b = OK bb ->
+ size a <= Ptrofs.max_unsigned
+ /\ size b <= Ptrofs.max_unsigned.
+Proof.
+ intros. monadInv H.
+ split.
+ - unfold check_size in EQ. destruct (zlt Ptrofs.max_unsigned (size a)); monadInv EQ. lia.
+ - unfold check_size in EQ1. destruct (zlt Ptrofs.max_unsigned (size b)); monadInv EQ1. lia.
+Qed.
+
+Lemma concat2_noexit:
+ forall a b bb,
+ concat2 a b = OK bb ->
+ exit a = None.
+Proof.
+ intros. destruct a as [hd bdy ex WF]; simpl in *.
+ destruct ex as [e|]; simpl in *; auto.
+ unfold concat2 in H. simpl in H. monadInv H.
+Qed.
+
+Lemma concat2_decomp:
+ forall a b bb,
+ concat2 a b = OK bb ->
+ body bb = body a ++ body b
+ /\ exit bb = exit b.
+Proof.
+ intros. exploit concat2_noexit; eauto. intros.
+ destruct a as [hda bda exa WFa]; destruct b as [hdb bdb exb WFb]; destruct bb as [hd bd ex WF]; simpl in *.
+ subst exa.
+ unfold concat2 in H; simpl in H.
+ destruct hdb.
+ - destruct exb.
+ + destruct c.
+ * destruct i; monadInv H; split; auto.
+ * monadInv H. split; auto.
+ + monadInv H. split; auto.
+ - monadInv H.
+Qed.
+
+Lemma concat2_size:
+ forall a b bb, concat2 a b = OK bb -> size bb = size a + size b.
+Proof.
+ intros. unfold concat2 in H.
+ destruct a as [hda bda exa WFa]; destruct b as [hdb bdb exb WFb]; destruct bb as [hd bdy ex WF]; simpl in *.
+ destruct exa; monadInv H. destruct hdb; try (monadInv EQ2). destruct exb; try (monadInv EQ2).
+ - destruct c.
+ + destruct i; monadInv EQ2;
+ unfold size; simpl; rewrite app_length; rewrite Nat.add_0_r; rewrite <- Nat2Z.inj_add; rewrite Nat.add_assoc; reflexivity.
+ + monadInv EQ2. unfold size; simpl. rewrite app_length. rewrite Nat.add_0_r. rewrite <- Nat2Z.inj_add. rewrite Nat.add_assoc. reflexivity.
+ - unfold size; simpl. rewrite app_length. repeat (rewrite Nat.add_0_r). rewrite <- Nat2Z.inj_add. reflexivity.
+Qed.
+
+Lemma concat2_header:
+ forall bb bb' tbb,
+ concat2 bb bb' = OK tbb -> header bb = header tbb.
+Proof.
+ intros. destruct bb as [hd bdy ex COR]; destruct bb' as [hd' bdy' ex' COR']; destruct tbb as [thd tbdy tex tCOR]; simpl in *.
+ unfold concat2 in H. simpl in H. monadInv H.
+ destruct ex; try discriminate. destruct hd'; try discriminate. destruct ex'.
+ - destruct c.
+ + destruct i; try discriminate; congruence.
+ + congruence.
+ - congruence.
+Qed.
+
+Lemma concat2_no_header_in_middle:
+ forall bb bb' tbb,
+ concat2 bb bb' = OK tbb ->
+ header bb' = nil.
+Proof.
+ intros. destruct bb as [hd bdy ex COR]; destruct bb' as [hd' bdy' ex' COR']; destruct tbb as [thd tbdy tex tCOR]; simpl in *.
+ unfold concat2 in H. simpl in H. monadInv H.
+ destruct ex; try discriminate. destruct hd'; try discriminate. reflexivity.
+Qed.
+
+
+
+Fixpoint concat_all (lbb: list bblock) : res bblock :=
+ match lbb with
+ | nil => Error (msg "PostpassSchedulingproof.concatenate: empty list")
+ | bb::nil => OK bb
+ | bb::lbb =>
+ do bb' <- concat_all lbb;
+ concat2 bb bb'
+ end.
+
+Lemma concat_all_size :
+ forall lbb a bb bb',
+ concat_all (a :: lbb) = OK bb ->
+ concat_all lbb = OK bb' ->
+ size bb = size a + size bb'.
+Proof.
+ intros. unfold concat_all in H. fold concat_all in H.
+ destruct lbb; try discriminate.
+ monadInv H. rewrite H0 in EQ. inv EQ.
+ apply concat2_size. assumption.
+Qed.
+
+Lemma concat_all_header:
+ forall lbb bb tbb,
+ concat_all (bb::lbb) = OK tbb -> header bb = header tbb.
+Proof.
+ destruct lbb.
+ - intros. simpl in H. congruence.
+ - intros. simpl in H. destruct lbb.
+ + inv H. eapply concat2_header; eassumption.
+ + monadInv H. eapply concat2_header; eassumption.
+Qed.
+
+Lemma concat_all_no_header_in_middle:
+ forall lbb tbb,
+ concat_all lbb = OK tbb ->
+ Forall (fun b => header b = nil) (tail lbb).
+Proof.
+ induction lbb; intros; try constructor.
+ simpl. simpl in H. destruct lbb.
+ - constructor.
+ - monadInv H. simpl tl in IHlbb. constructor.
+ + apply concat2_no_header_in_middle in EQ0. apply concat_all_header in EQ. congruence.
+ + apply IHlbb in EQ. assumption.
+Qed.
+
+Inductive is_concat : bblock -> list bblock -> Prop :=
+ | mk_is_concat: forall tbb lbb, concat_all lbb = OK tbb -> is_concat tbb lbb.
+
+(** * Remainder of the verified scheduler *)
+
+Definition verify_schedule (bb bb' : bblock) : res unit :=
+ match bblock_simub bb bb' with
+ | true => OK tt
+ | false => Error (msg "PostpassScheduling.verify_schedule")
+ end.
+
+
+Definition verify_size bb lbb := if (Z.eqb (size bb) (size_blocks lbb)) then OK tt else Error (msg "PostpassScheduling:verify_size: wrong size").
+
+Lemma verify_size_size:
+ forall bb lbb, verify_size bb lbb = OK tt -> size bb = size_blocks lbb.
+Proof.
+ intros. unfold verify_size in H. destruct (size bb =? size_blocks lbb) eqn:SIZE; try discriminate.
+ apply Z.eqb_eq. assumption.
+Qed.
+
+Lemma verify_schedule_no_header:
+ forall bb bb',
+ verify_schedule (no_header bb) bb' = verify_schedule bb bb'.
+Proof.
+ intros. unfold verify_schedule. unfold bblock_simub. unfold pure_bblock_simu_test, bblock_simu_test. rewrite trans_block_noheader_inv.
+ reflexivity.
+Qed.
+
+
+Lemma stick_header_verify_schedule:
+ forall hd bb' hbb' bb,
+ stick_header hd bb' = hbb' ->
+ verify_schedule bb bb' = verify_schedule bb hbb'.
+Proof.
+ intros. unfold verify_schedule. unfold bblock_simub, pure_bblock_simu_test, bblock_simu_test.
+ rewrite <- H. rewrite trans_block_header_inv. reflexivity.
+Qed.
+
+Lemma check_size_stick_header:
+ forall bb hd,
+ check_size bb = check_size (stick_header hd bb).
+Proof.
+ intros. unfold check_size. rewrite stick_header_size. reflexivity.
+Qed.
+
+Lemma stick_header_concat2:
+ forall bb bb' hd tbb,
+ concat2 bb bb' = OK tbb ->
+ concat2 (stick_header hd bb) bb' = OK (stick_header hd tbb).
+Proof.
+ intros. monadInv H. erewrite check_size_stick_header in EQ.
+ unfold concat2. rewrite EQ. rewrite EQ1. simpl.
+ destruct bb as [hdr bdy ex COR]; destruct bb' as [hdr' bdy' ex' COR']; simpl in *.
+ destruct ex; try discriminate. destruct hdr'; try discriminate. destruct ex'.
+ - destruct c.
+ + destruct i; try discriminate; inv EQ2; unfold stick_header; simpl; reflexivity.
+ + inv EQ2. unfold stick_header; simpl. reflexivity.
+ - inv EQ2. unfold stick_header; simpl. reflexivity.
+Qed.
+
+Lemma stick_header_concat_all:
+ forall bb c tbb hd,
+ concat_all (bb :: c) = OK tbb ->
+ concat_all (stick_header hd bb :: c) = OK (stick_header hd tbb).
+Proof.
+ intros. simpl in *. destruct c; try congruence.
+ monadInv H. rewrite EQ. simpl.
+ apply stick_header_concat2. assumption.
+Qed.
+
+
+
+Definition stick_header_code (h : list label) (lbb : list bblock) :=
+ match (head lbb) with
+ | None => Error (msg "PostpassScheduling.stick_header: empty schedule")
+ | Some fst => OK ((stick_header h fst) :: tail lbb)
+ end.
+
+Lemma stick_header_code_no_header:
+ forall bb c,
+ stick_header_code (header bb) (no_header bb :: c) = OK (bb :: c).
+Proof.
+ intros. unfold stick_header_code. simpl. rewrite stick_header_no_header. reflexivity.
+Qed.
+
+Lemma hd_tl_size:
+ forall lbb bb, hd_error lbb = Some bb -> size_blocks lbb = size bb + size_blocks (tl lbb).
+Proof.
+ destruct lbb.
+ - intros. simpl in H. discriminate.
+ - intros. simpl in H. inv H. simpl. reflexivity.
+Qed.
+
+Lemma stick_header_code_size:
+ forall h lbb lbb', stick_header_code h lbb = OK lbb' -> size_blocks lbb = size_blocks lbb'.
+Proof.
+ intros. unfold stick_header_code in H. destruct (hd_error lbb) eqn:HD; try discriminate.
+ inv H. simpl. rewrite stick_header_size. erewrite hd_tl_size; eauto.
+Qed.
+
+Lemma stick_header_code_no_header_in_middle:
+ forall c h lbb,
+ stick_header_code h c = OK lbb ->
+ Forall (fun b => header b = nil) (tl c) ->
+ Forall (fun b => header b = nil) (tl lbb).
+Proof.
+ destruct c; intros.
+ - unfold stick_header_code in H. simpl in H. discriminate.
+ - unfold stick_header_code in H. simpl in H. inv H. simpl in H0.
+ simpl. assumption.
+Qed.
+
+Lemma stick_header_code_concat_all:
+ forall hd lbb hlbb tbb,
+ stick_header_code hd lbb = OK hlbb ->
+ concat_all lbb = OK tbb ->
+ exists htbb,
+ concat_all hlbb = OK htbb
+ /\ stick_header hd tbb = htbb.
+Proof.
+ intros. exists (stick_header hd tbb). split; auto.
+ destruct lbb.
+ - unfold stick_header_code in H. simpl in H. discriminate.
+ - unfold stick_header_code in H. simpl in H. inv H.
+ apply stick_header_concat_all. assumption.
+Qed.
+
+Program Definition make_bblock_from_basics lb :=
+ match lb with
+ | nil => Error (msg "PostpassScheduling.make_bblock_from_basics")
+ | b :: lb => OK {| header := nil; body := b::lb; exit := None |}
+ end.
+
+Fixpoint schedule_to_bblocks_nocontrol llb :=
+ match llb with
+ | nil => OK nil
+ | lb :: llb => do bb <- make_bblock_from_basics lb;
+ do lbb <- schedule_to_bblocks_nocontrol llb;
+ OK (bb :: lbb)
+ end.
+
+Program Definition make_bblock_from_basics_and_control lb c :=
+ match c with
+ | PExpand (Pbuiltin _ _ _) => Error (msg "PostpassScheduling.make_bblock_from_basics_and_control")
+ | PCtlFlow cf => OK {| header := nil; body := lb; exit := Some (PCtlFlow cf) |}
+ end.
+Next Obligation.
+ apply wf_bblock_refl. constructor.
+ - right. discriminate.
+ - discriminate.
+Qed.
+
+Fixpoint schedule_to_bblocks_wcontrol llb c :=
+ match llb with
+ | nil => OK ((bblock_single_inst (PControl c)) :: nil)
+ | lb :: nil => do bb <- make_bblock_from_basics_and_control lb c; OK (bb :: nil)
+ | lb :: llb => do bb <- make_bblock_from_basics lb;
+ do lbb <- schedule_to_bblocks_wcontrol llb c;
+ OK (bb :: lbb)
+ end.
+
+Definition schedule_to_bblocks (llb: list (list basic)) (oc: option control) : res (list bblock) :=
+ match oc with
+ | None => schedule_to_bblocks_nocontrol llb
+ | Some c => schedule_to_bblocks_wcontrol llb c
+ end.
+
+Definition do_schedule (bb: bblock) : res (list bblock) :=
+ if (Z.eqb (size bb) 1) then OK (bb::nil)
+ else match (schedule bb) with (llb, oc) => schedule_to_bblocks llb oc end.
+
+Definition verify_par_bblock (bb: bblock) : res unit :=
+ if (bblock_para_check bb) then OK tt else Error (msg "PostpassScheduling.verify_par_bblock").
+
+Fixpoint verify_par (lbb: list bblock) :=
+ match lbb with
+ | nil => OK tt
+ | bb :: lbb => do res <- verify_par_bblock bb; verify_par lbb
+ end.
+
+Definition verified_schedule_nob (bb : bblock) : res (list bblock) :=
+ let bb' := no_header bb in
+ let bb'' := Peephole.optimize_bblock bb' in
+ do lbb <- do_schedule bb'';
+ do tbb <- concat_all lbb;
+ do sizecheck <- verify_size bb lbb;
+ do schedcheck <- verify_schedule bb' tbb;
+ do res <- stick_header_code (header bb) lbb;
+ do parcheck <- verify_par res;
+ OK res.
+
+Lemma verified_schedule_nob_size:
+ forall bb lbb, verified_schedule_nob bb = OK lbb -> size bb = size_blocks lbb.
+Proof.
+ intros. monadInv H. erewrite <- stick_header_code_size; eauto.
+ apply verify_size_size.
+ destruct x1; try discriminate. assumption.
+Qed.
+
+Lemma verified_schedule_nob_no_header_in_middle:
+ forall lbb bb,
+ verified_schedule_nob bb = OK lbb ->
+ Forall (fun b => header b = nil) (tail lbb).
+Proof.
+ intros. monadInv H. eapply stick_header_code_no_header_in_middle; eauto.
+ eapply concat_all_no_header_in_middle. eassumption.
+Qed.
+
+Lemma verified_schedule_nob_header:
+ forall bb tbb lbb,
+ verified_schedule_nob bb = OK (tbb :: lbb) ->
+ header bb = header tbb
+ /\ Forall (fun b => header b = nil) lbb.
+Proof.
+ intros. split.
+ - monadInv H. unfold stick_header_code in EQ3. destruct (hd_error _); try discriminate. inv EQ3.
+ simpl. reflexivity.
+ - apply verified_schedule_nob_no_header_in_middle in H. assumption.
+Qed.
+
+
+Definition verified_schedule (bb : bblock) : res (list bblock) :=
+ match exit bb with
+ | Some (PExpand (Pbuiltin ef args res)) => OK (bb::nil) (* Special case for ensuring the lemma verified_schedule_builtin_idem *)
+ | _ => verified_schedule_nob bb
+ end.
+
+Lemma verified_schedule_size:
+ forall bb lbb, verified_schedule bb = OK lbb -> size bb = size_blocks lbb.
+Proof.
+ intros. unfold verified_schedule in H. destruct (exit bb). destruct c. destruct i.
+ all: try (apply verified_schedule_nob_size; auto; fail).
+ inv H. simpl. lia.
+Qed.
+
+Lemma verified_schedule_no_header_in_middle:
+ forall lbb bb,
+ verified_schedule bb = OK lbb ->
+ Forall (fun b => header b = nil) (tail lbb).
+Proof.
+ intros. unfold verified_schedule in H. destruct (exit bb). destruct c. destruct i.
+ all: try (eapply verified_schedule_nob_no_header_in_middle; eauto; fail).
+ inv H. simpl. auto.
+Qed.
+
+Lemma verified_schedule_header:
+ forall bb tbb lbb,
+ verified_schedule bb = OK (tbb :: lbb) ->
+ header bb = header tbb
+ /\ Forall (fun b => header b = nil) lbb.
+Proof.
+ intros. unfold verified_schedule in H. destruct (exit bb). destruct c. destruct i.
+ all: try (eapply verified_schedule_nob_header; eauto; fail).
+ inv H. split; simpl; auto.
+Qed.
+
+
+Lemma verified_schedule_nob_correct:
+ forall ge f bb lbb,
+ verified_schedule_nob bb = OK lbb ->
+ exists tbb,
+ is_concat tbb lbb
+ /\ bblock_simu ge f bb tbb.
+Proof.
+ intros. monadInv H.
+ exploit stick_header_code_concat_all; eauto.
+ intros (tbb & CONC & STH).
+ exists tbb. split; auto. constructor; auto.
+ rewrite verify_schedule_no_header in EQ2. erewrite stick_header_verify_schedule in EQ2; eauto.
+ eapply bblock_simub_correct; eauto. unfold verify_schedule in EQ2.
+ destruct (bblock_simub _ _); auto; try discriminate.
+Qed.
+
+Theorem verified_schedule_correct:
+ forall ge f bb lbb,
+ verified_schedule bb = OK lbb ->
+ exists tbb,
+ is_concat tbb lbb
+ /\ bblock_simu ge f bb tbb.
+Proof.
+ intros. unfold verified_schedule in H. destruct (exit bb). destruct c. destruct i.
+ all: try (eapply verified_schedule_nob_correct; eauto; fail).
+ inv H. eexists. split; simpl; auto. constructor; auto. simpl; auto. constructor; auto.
+Qed.
+
+Lemma verified_schedule_builtin_idem:
+ forall bb ef args res lbb,
+ exit bb = Some (PExpand (Pbuiltin ef args res)) ->
+ verified_schedule bb = OK lbb ->
+ lbb = bb :: nil.
+Proof.
+ intros. unfold verified_schedule in H0. rewrite H in H0. inv H0. reflexivity.
+Qed.
+
+
+Fixpoint transf_blocks (lbb : list bblock) : res (list bblock) :=
+ match lbb with
+ | nil => OK nil
+ | (cons bb lbb) =>
+ do tlbb <- transf_blocks lbb;
+ do tbb <- verified_schedule bb;
+ OK (tbb ++ tlbb)
+ end.
+
+Definition transl_function (f: function) : res function :=
+ do lb <- transf_blocks (fn_blocks f);
+ OK (mkfunction (fn_sig f) lb).
+
+Definition transf_function (f: function) : res function :=
+ do tf <- transl_function f;
+ if zlt Ptrofs.max_unsigned (size_blocks tf.(fn_blocks))
+ then Error (msg "code size exceeded")
+ else OK tf.
+
+Definition transf_fundef (f: fundef) : res fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program transf_fundef p.
diff --git a/kvx/PostpassSchedulingOracle.ml b/kvx/PostpassSchedulingOracle.ml
new file mode 100644
index 00000000..5ebad421
--- /dev/null
+++ b/kvx/PostpassSchedulingOracle.ml
@@ -0,0 +1,1035 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open Asmvliw
+open Asmblock
+open Printf
+open Camlcoq
+open InstructionScheduler
+open TargetPrinter.Target
+
+let debug = false
+
+(**
+ * Extracting infos from Asmvliw instructions
+ *)
+
+type immediate = I32 of Integers.Int.int | I64 of Integers.Int64.int | Off of offset
+
+type location = Reg of preg | Mem
+
+type real_instruction =
+ (* ALU *)
+ | Addw | Andw | Compw | Mulw | Orw | Sbfw | Sbfxw | Sraw | Srlw | Sllw | Srsw | Rorw | Xorw
+ | Addd | Andd | Compd | Muld | Ord | Sbfd | Sbfxd | Srad | Srld | Slld | Srsd | Xord
+ | Nandw | Norw | Nxorw | Nandd | Nord | Nxord | Andnw | Ornw | Andnd | Ornd
+ | Maddw | Maddd | Msbfw | Msbfd | Cmoved
+ | Make | Nop | Extfz | Extfs | Insf
+ | Addxw | Addxd
+ (* LSU *)
+ | Lbs | Lbz | Lhs | Lhz | Lws | Ld | Lq | Lo
+ | Sb | Sh | Sw | Sd | Sq | So
+ (* BCU *)
+ | Icall | Call | Cb | Igoto | Goto | Ret | Get | Set
+ (* FPU *)
+ | Fabsd | Fabsw | Fnegw | Fnegd
+ | Faddd | Faddw | Fsbfd | Fsbfw | Fmuld | Fmulw
+ | Fmind | Fminw | Fmaxd | Fmaxw | Finvw
+ | Ffmaw | Ffmad | Ffmsw | Ffmsd
+ | Fnarrowdw | Fwidenlwd | Floatwz | Floatuwz | Floatdz | Floatudz | Fixedwz | Fixeduwz | Fixeddz | Fixedudz
+ | Fcompw | Fcompd
+
+type ab_inst_rec = {
+ inst: real_instruction;
+ write_locs : location list;
+ read_locs : location list;
+ read_at_id : location list; (* Must be contained in read_locs *)
+ read_at_e1 : location list; (* idem *)
+ imm : immediate option;
+ is_control : bool;
+}
+
+(** Asmvliw constructor to real instructions *)
+
+exception OpaqueInstruction
+
+let arith_rr_real = function
+ | Pcvtl2w -> Addw
+ | Pmv -> Addd
+ | Pnegw -> Sbfw
+ | Pnegl -> Sbfd
+ | Psxwd -> Extfs
+ | Pzxwd -> Extfz
+ | Pextfz(_,_) -> Extfz
+ | Pextfs(_,_) -> Extfs
+ | Pextfzl(_,_) -> Extfz
+ | Pextfsl(_,_) -> Extfs
+ | Pfabsw -> Fabsw
+ | Pfabsd -> Fabsd
+ | Pfnegw -> Fnegw
+ | Pfnegd -> Fnegd
+ | Pfinvw -> Finvw
+ | Pfnarrowdw -> Fnarrowdw
+ | Pfwidenlwd -> Fwidenlwd
+ | Pfloatwrnsz -> Floatwz
+ | Pfloatuwrnsz -> Floatuwz
+ | Pfloatudrnsz -> Floatudz
+ | Pfloatdrnsz -> Floatdz
+ | Pfixedwrzz -> Fixedwz
+ | Pfixeduwrzz -> Fixeduwz
+ | Pfixeddrzz -> Fixeddz
+ | Pfixedudrzz -> Fixedudz
+ | Pfixeddrzz_i32 -> Fixeddz
+ | Pfixedudrzz_i32 -> Fixedudz
+
+let arith_rrr_real = function
+ | Pcompw it -> Compw
+ | Pcompl it -> Compd
+ | Pfcompw ft -> Fcompw
+ | Pfcompl ft -> Fcompd
+ | Paddw -> Addw
+ | Paddxw _ -> Addxw
+ | Psubw -> Sbfw
+ | Prevsubxw _ -> Sbfxw
+ | Pmulw -> Mulw
+ | Pandw -> Andw
+ | Pnandw -> Nandw
+ | Porw -> Orw
+ | Pnorw -> Norw
+ | Pxorw -> Xorw
+ | Pnxorw -> Nxorw
+ | Pandnw -> Andnw
+ | Pornw -> Ornw
+ | Psraw -> Sraw
+ | Psrlw -> Srlw
+ | Psrxw -> Srsw
+ | Psllw -> Sllw
+ | Paddl -> Addd
+ | Paddxl _ -> Addxd
+ | Psubl -> Sbfd
+ | Prevsubxl _ -> Sbfxd
+ | Pandl -> Andd
+ | Pnandl -> Nandd
+ | Porl -> Ord
+ | Pnorl -> Nord
+ | Pxorl -> Xord
+ | Pnxorl -> Nxord
+ | Pandnl -> Andnd
+ | Pornl -> Ornd
+ | Pmull -> Muld
+ | Pslll -> Slld
+ | Psrll -> Srld
+ | Psrxl -> Srsd
+ | Psral -> Srad
+ | Pfaddd -> Faddd
+ | Pfaddw -> Faddw
+ | Pfsbfd -> Fsbfd
+ | Pfsbfw -> Fsbfw
+ | Pfmuld -> Fmuld
+ | Pfmulw -> Fmulw
+ | Pfmind -> Fmind
+ | Pfminw -> Fminw
+ | Pfmaxd -> Fmaxd
+ | Pfmaxw -> Fmaxw
+
+let arith_rri32_real = function
+ | Pcompiw it -> Compw
+ | Paddiw -> Addw
+ | Paddxiw _ -> Addxw
+ | Prevsubiw -> Sbfw
+ | Prevsubxiw _ -> Sbfxw
+ | Pmuliw -> Mulw
+ | Pandiw -> Andw
+ | Pnandiw -> Nandw
+ | Poriw -> Orw
+ | Pnoriw -> Norw
+ | Pxoriw -> Xorw
+ | Pnxoriw -> Nxorw
+ | Pandniw -> Andnw
+ | Porniw -> Ornw
+ | Psraiw -> Sraw
+ | Psrxiw -> Srsw
+ | Psrliw -> Srlw
+ | Pslliw -> Sllw
+ | Proriw -> Rorw
+ | Psllil -> Slld
+ | Psrlil -> Srld
+ | Psrail -> Srad
+ | Psrxil -> Srsd
+
+let arith_rri64_real = function
+ | Pcompil it -> Compd
+ | Paddil -> Addd
+ | Prevsubil -> Sbfd
+ | Paddxil _ -> Addxd
+ | Prevsubxil _ -> Sbfxd
+ | Pmulil -> Muld
+ | Pandil -> Andd
+ | Pnandil -> Nandd
+ | Poril -> Ord
+ | Pnoril -> Nord
+ | Pxoril -> Xord
+ | Pnxoril -> Nxord
+ | Pandnil -> Andnd
+ | Pornil -> Ornd
+
+
+let arith_arr_real = function
+ | Pinsf (_, _) -> Insf
+ | Pinsfl (_, _) -> Insf
+
+let arith_arrr_real = function
+ | Pfmaddfw -> Ffmaw
+ | Pfmaddfl -> Ffmad
+ | Pfmsubfw -> Ffmsw
+ | Pfmsubfl -> Ffmsd
+ | Pmaddw -> Maddw
+ | Pmaddl -> Maddd
+ | Pmsubw -> Msbfw
+ | Pmsubl -> Msbfd
+ | Pcmove _ -> Cmoved
+ | Pcmoveu _ -> Cmoved
+
+let arith_arri32_real = function
+ | Pmaddiw -> Maddw
+ | Pcmoveiw _ -> Cmoved
+ | Pcmoveuiw _ -> Cmoved
+
+let arith_arri64_real = function
+ | Pmaddil -> Maddd
+ | Pcmoveil _ -> Cmoved
+ | Pcmoveuil _ -> Cmoved
+
+let arith_ri32_real = Make
+
+let arith_ri64_real = Make
+
+let arith_rf32_real = Make
+
+let arith_rf64_real = Make
+
+let store_real = function
+ | Psb -> Sb
+ | Psh -> Sh
+ | Psw -> Sw
+ | Psw_a -> Sw
+ | Psd -> Sd
+ | Psd_a -> Sd
+ | Pfss -> Sw
+ | Pfsd -> Sd
+
+let load_real = function
+ | Plb -> Lbs
+ | Plbu -> Lbz
+ | Plh -> Lhs
+ | Plhu -> Lhz
+ | Plw -> Lws
+ | Plw_a -> Lws
+ | Pld -> Ld
+ | Pld_a -> Ld
+ | Pfls -> Lws
+ | Pfld -> Ld
+
+let set_real = Set
+let get_real = Get
+let nop_real = Nop
+let loadsymbol_real = Make
+let loadqrro_real = Lq
+let loadorro_real = Lo
+let storeqrro_real = Sq
+let storeorro_real = So
+
+let ret_real = Ret
+let call_real = Call
+let icall_real = Icall
+let goto_real = Goto
+let igoto_real = Igoto
+let jl_real = Goto
+let cb_real = Cb
+let cbu_real = Cb
+
+let arith_rri32_rec i rd rs imm32 = { inst = arith_rri32_real i; write_locs = [Reg rd]; read_locs = [Reg rs]; imm = imm32; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let arith_rri64_rec i rd rs imm64 = { inst = arith_rri64_real i; write_locs = [Reg rd]; read_locs = [Reg rs]; imm = imm64; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let arith_rrr_rec i rd rs1 rs2 = { inst = arith_rrr_real i; write_locs = [Reg rd]; read_locs = [Reg rs1; Reg rs2]; imm = None; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let arith_arri32_rec i rd rs imm32 =
+ let rae1 = match i with Pmaddiw -> [Reg rd] | _ -> []
+ in { inst = arith_arri32_real i; write_locs = [Reg rd]; read_locs = [Reg rd; Reg rs]; imm = imm32; is_control = false;
+ read_at_id = [] ; read_at_e1 = rae1 }
+
+let arith_arri64_rec i rd rs imm64 =
+ let rae1 = match i with Pmaddil -> [Reg rd] | _ -> []
+ in { inst = arith_arri64_real i; write_locs = [Reg rd]; read_locs = [Reg rd; Reg rs]; imm = imm64; is_control = false;
+ read_at_id = []; read_at_e1 = rae1 }
+
+let arith_arr_rec i rd rs = { inst = arith_arr_real i; write_locs = [Reg rd]; read_locs = [Reg rd; Reg rs]; imm = None; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let arith_arrr_rec i rd rs1 rs2 =
+ let rae1 = match i with Pmaddl | Pmaddw | Pmsubl | Pmsubw -> [Reg rd] | _ -> []
+ in { inst = arith_arrr_real i; write_locs = [Reg rd]; read_locs = [Reg rd; Reg rs1; Reg rs2]; imm = None; is_control = false;
+ read_at_id = []; read_at_e1 = rae1 }
+
+let arith_rr_rec i rd rs = { inst = arith_rr_real i; write_locs = [Reg rd]; read_locs = [Reg rs]; imm = None; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let arith_r_rec i rd = match i with
+ (* For Ploadsymbol, writing the highest integer since we do not know how many bits does a symbol have *)
+ | Ploadsymbol (id, ofs) -> { inst = loadsymbol_real; write_locs = [Reg rd]; read_locs = []; imm = Some (I64 Integers.Int64.max_signed);
+ is_control = false; read_at_id = []; read_at_e1 = [] }
+
+let arith_rec i =
+ match i with
+ | PArithRRI32 (i, rd, rs, imm32) -> arith_rri32_rec i (IR rd) (IR rs) (Some (I32 imm32))
+ | PArithRRI64 (i, rd, rs, imm64) -> arith_rri64_rec i (IR rd) (IR rs) (Some (I64 imm64))
+ | PArithRRR (i, rd, rs1, rs2) -> arith_rrr_rec i (IR rd) (IR rs1) (IR rs2)
+ | PArithARR (i, rd, rs) -> arith_arr_rec i (IR rd) (IR rs)
+ (* Seems like single constant constructor types are elided *)
+ | PArithARRI32 (i, rd, rs, imm32) -> arith_arri32_rec i (IR rd) (IR rs) (Some (I32 imm32))
+ | PArithARRI64 (i, rd, rs, imm64) -> arith_arri64_rec i (IR rd) (IR rs) (Some (I64 imm64))
+ | PArithARRR (i, rd, rs1, rs2) -> arith_arrr_rec i (IR rd) (IR rs1) (IR rs2)
+ | PArithRI32 (rd, imm32) -> { inst = arith_ri32_real; write_locs = [Reg (IR rd)]; read_locs = []; imm = (Some (I32 imm32)) ; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+ | PArithRI64 (rd, imm64) -> { inst = arith_ri64_real; write_locs = [Reg (IR rd)]; read_locs = []; imm = (Some (I64 imm64)) ; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+ | PArithRF32 (rd, f) -> { inst = arith_rf32_real; write_locs = [Reg (IR rd)]; read_locs = [];
+ imm = (Some (I32 (Floats.Float32.to_bits f))); is_control = false; read_at_id = []; read_at_e1 = []}
+ | PArithRF64 (rd, f) -> { inst = arith_rf64_real; write_locs = [Reg (IR rd)]; read_locs = [];
+ imm = (Some (I64 (Floats.Float.to_bits f))); is_control = false; read_at_id = []; read_at_e1 = []}
+ | PArithRR (i, rd, rs) -> arith_rr_rec i (IR rd) (IR rs)
+ | PArithR (i, rd) -> arith_r_rec i (IR rd)
+
+let load_rec i = match i with
+ | PLoadRRO (trap, i, rs1, rs2, imm) ->
+ { inst = load_real i; write_locs = [Reg (IR rs1)]; read_locs = [Mem; Reg (IR rs2)]; imm = (Some (Off imm)) ; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+ | PLoadQRRO(rs, ra, imm) ->
+ let (rs0, rs1) = gpreg_q_expand rs in
+ { inst = loadqrro_real; write_locs = [Reg (IR rs0); Reg (IR rs1)]; read_locs = [Mem; Reg (IR ra)]; imm = (Some (Off imm)) ; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+ | PLoadORRO(rs, ra, imm) ->
+ let (((rs0, rs1), rs2), rs3) = gpreg_o_expand rs in
+ { inst = loadorro_real; write_locs = [Reg (IR rs0); Reg (IR rs1); Reg (IR rs2); Reg (IR rs3)]; read_locs = [Mem; Reg (IR ra)];
+ imm = (Some (Off imm)) ; is_control = false; read_at_id = []; read_at_e1 = []}
+ | PLoadRRR (trap, i, rs1, rs2, rs3) | PLoadRRRXS (trap, i, rs1, rs2, rs3) ->
+ { inst = load_real i; write_locs = [Reg (IR rs1)]; read_locs = [Mem; Reg (IR rs2); Reg (IR rs3)]; imm = None ; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let store_rec i = match i with
+ | PStoreRRO (i, rs, ra, imm) ->
+ { inst = store_real i; write_locs = [Mem]; read_locs = [Reg (IR rs); Reg (IR ra)]; imm = (Some (Off imm));
+ read_at_id = []; read_at_e1 = [Reg (IR rs)] ; is_control = false}
+ | PStoreQRRO (rs, ra, imm) ->
+ let (rs0, rs1) = gpreg_q_expand rs in
+ { inst = storeqrro_real; write_locs = [Mem]; read_locs = [Reg (IR rs0); Reg (IR rs1); Reg (IR ra)]; imm = (Some (Off imm));
+ read_at_id = []; read_at_e1 = [Reg (IR rs0); Reg (IR rs1)] ; is_control = false}
+ | PStoreORRO (rs, ra, imm) ->
+ let (((rs0, rs1), rs2), rs3) = gpreg_o_expand rs in
+ { inst = storeorro_real; write_locs = [Mem]; read_locs = [Reg (IR rs0); Reg (IR rs1); Reg (IR rs2); Reg (IR rs3); Reg (IR ra)];
+ imm = (Some (Off imm)); read_at_id = []; read_at_e1 = [Reg (IR rs0); Reg (IR rs1); Reg (IR rs2); Reg (IR rs3)]; is_control = false}
+ | PStoreRRR (i, rs, ra1, ra2) | PStoreRRRXS (i, rs, ra1, ra2) ->
+ { inst = store_real i; write_locs = [Mem]; read_locs = [Reg (IR rs); Reg (IR ra1); Reg (IR ra2)]; imm = None;
+ read_at_id = []; read_at_e1 = [Reg (IR rs)]; is_control = false}
+
+let get_rec (rd:gpreg) rs = { inst = get_real; write_locs = [Reg (IR rd)]; read_locs = [Reg rs]; imm = None; is_control = false;
+ read_at_id = []; read_at_e1 = [] }
+
+let set_rec rd (rs:gpreg) = { inst = set_real; write_locs = [Reg rd]; read_locs = [Reg (IR rs)]; imm = None; is_control = false;
+ read_at_id = [Reg (IR rs)]; read_at_e1 = [] }
+
+let basic_rec i =
+ match i with
+ | PArith i -> arith_rec i
+ | PLoad i -> load_rec i
+ | PStore i -> store_rec i
+ | Pallocframe (_, _) -> raise OpaqueInstruction
+ | Pfreeframe (_, _) -> raise OpaqueInstruction
+ | Pget (rd, rs) -> get_rec rd rs
+ | Pset (rd, rs) -> set_rec rd rs
+ | Pnop -> { inst = nop_real; write_locs = []; read_locs = []; imm = None ; is_control = false; read_at_id = []; read_at_e1 = []}
+
+let expand_rec = function
+ | Pbuiltin _ -> raise OpaqueInstruction
+
+let ctl_flow_rec = function
+ | Pret -> { inst = ret_real; write_locs = []; read_locs = [Reg RA]; imm = None ; is_control = true; read_at_id = [Reg RA]; read_at_e1 = []}
+ | Pcall lbl -> { inst = call_real; write_locs = [Reg RA]; read_locs = []; imm = None ; is_control = true; read_at_id = []; read_at_e1 = []}
+ | Picall r -> { inst = icall_real; write_locs = [Reg RA]; read_locs = [Reg (IR r)]; imm = None; is_control = true;
+ read_at_id = [Reg (IR r)]; read_at_e1 = [] }
+ | Pgoto lbl -> { inst = goto_real; write_locs = []; read_locs = []; imm = None ; is_control = true; read_at_id = []; read_at_e1 = []}
+ | Pigoto r -> { inst = igoto_real; write_locs = []; read_locs = [Reg (IR r)]; imm = None ; is_control = true;
+ read_at_id = [Reg (IR r)]; read_at_e1 = [] }
+ | Pj_l lbl -> { inst = goto_real; write_locs = []; read_locs = []; imm = None ; is_control = true; read_at_id = []; read_at_e1 = []}
+ | Pcb (bt, rs, lbl) -> { inst = cb_real; write_locs = []; read_locs = [Reg (IR rs)]; imm = None ; is_control = true;
+ read_at_id = [Reg (IR rs)]; read_at_e1 = [] }
+ | Pcbu (bt, rs, lbl) -> { inst = cbu_real; write_locs = []; read_locs = [Reg (IR rs)]; imm = None ; is_control = true;
+ read_at_id = [Reg (IR rs)]; read_at_e1 = [] }
+ | Pjumptable (r, _) -> raise OpaqueInstruction (* { inst = "Pjumptable"; write_locs = [Reg (IR GPR62); Reg (IR GPR63)]; read_locs = [Reg (IR r)]; imm = None ; is_control = true} *)
+
+let control_rec i =
+ match i with
+ | PExpand i -> expand_rec i
+ | PCtlFlow i -> ctl_flow_rec i
+
+let rec basic_recs body = match body with
+ | [] -> []
+ | bi :: body -> (basic_rec bi) :: (basic_recs body)
+
+let exit_rec exit = match exit with
+ | None -> []
+ | Some ex -> [control_rec ex]
+
+let instruction_recs bb = (basic_recs bb.body) @ (exit_rec bb.exit)
+
+(**
+ * Providing informations relative to the real instructions
+ *)
+
+(** Abstraction providing all the necessary informations for solving the scheduling problem *)
+type inst_info = {
+ write_locs : location list;
+ read_locs : location list;
+ reads_at_id : bool;
+ reads_at_e1 : bool;
+ is_control : bool;
+ usage: int array; (* resources consumed by the instruction *)
+ latency: int;
+}
+
+(** Figuring out whether an immediate is s10, u27l10 or e27u27l10 *)
+type imm_encoding = U6 | S10 | U27L5 | U27L10 | E27U27L10
+
+let rec pow a = function
+ | 0 -> Int64.one
+ | 1 -> Int64.of_int a
+ | n -> let b = pow a (n/2) in
+ Int64.mul b (Int64.mul b (if n mod 2 = 0 then Int64.one else Int64.of_int a))
+
+let signed_interval n : (int64 * int64) = begin
+ assert (n > 0);
+ let min = Int64.neg @@ pow 2 (n-1)
+ and max = Int64.sub (pow 2 (n-1)) Int64.one
+ in (min, max)
+end
+
+let within i interv = match interv with (min, max) -> (i >= min && i <= max)
+
+let signed_length (i:int64) =
+ let rec f (i:int64) n =
+ let interv = signed_interval n
+ in if (within i interv) then n else f i (n+1)
+ in f i 1
+
+let unsigned_length (i:int64) = (signed_length i) - 1
+
+let encode_imm (imm:int64) =
+ if (Int64.compare imm Int64.zero < 0) then
+ let length = signed_length imm
+ in if length <= 10 then S10
+ else if length <= 32 then U27L5
+ else if length <= 37 then U27L10
+ else if length <= 64 then E27U27L10
+ else failwith @@ sprintf "encode_imm: integer too big! (%Ld)" imm
+ else
+ let length = unsigned_length imm
+ in if length <= 6 then U6
+ else if length <= 9 then S10 (* Special case for S10 - stay signed no matter what *)
+ else if length <= 32 then U27L5
+ else if length <= 37 then U27L10
+ else if length <= 64 then E27U27L10
+ else failwith @@ sprintf "encode_imm: integer too big! (%Ld)" imm
+
+(** Resources *)
+type rname = Rissue | Rtiny | Rlite | Rfull | Rlsu | Rmau | Rbcu | Rtca | Rauxr | Rauxw | Rcrrp | Rcrwl | Rcrwh | Rnop
+
+let resource_names = [Rissue; Rtiny; Rlite; Rfull; Rlsu; Rmau; Rbcu; Rtca; Rauxr; Rauxw; Rcrrp; Rcrwl; Rcrwh; Rnop]
+
+let rec find_index elt l =
+ match l with
+ | [] -> raise Not_found
+ | e::l -> if (e == elt) then 0
+ else 1 + find_index elt l
+
+let resource_id resource : int = find_index resource resource_names
+
+let resource_bound resource : int =
+ match resource with
+ | Rissue -> 8
+ | Rtiny -> 4
+ | Rlite -> 2
+ | Rfull -> 1
+ | Rlsu -> 1
+ | Rmau -> 1
+ | Rbcu -> 1
+ | Rtca -> 1
+ | Rauxr -> 1
+ | Rauxw -> 1
+ | Rcrrp -> 1
+ | Rcrwl -> 1
+ | Rcrwh -> 1
+ | Rnop -> 4
+
+let resource_bounds : int array = Array.of_list (List.map resource_bound resource_names)
+
+(** Reservation tables *)
+let alu_full : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rlite -> 1 | Rfull -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_lite : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rlite -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_lite_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | Rlite -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_lite_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | Rlite -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_nop : int array = let resmap = fun r -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_tiny : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_tiny_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let alu_tiny_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let bcu : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rbcu -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let bcu_tiny_tiny_mau_xnop : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 2 | Rmau -> 1 | Rbcu -> 1 | Rnop -> 4 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxr : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rlsu -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxr_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | Rlsu -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxr_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | Rlsu -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxw : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rlsu -> 1 | Rauxw -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxw_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | Rlsu -> 1 | Rauxw -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let lsu_auxw_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | Rlsu -> 1 | Rauxw -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rmau -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | Rmau -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | Rmau -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau_auxr : int array = let resmap = fun r -> match r with
+ | Rissue -> 1 | Rtiny -> 1 | Rmau -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau_auxr_x : int array = let resmap = fun r -> match r with
+ | Rissue -> 2 | Rtiny -> 1 | Rmau -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+let mau_auxr_y : int array = let resmap = fun r -> match r with
+ | Rissue -> 3 | Rtiny -> 1 | Rmau -> 1 | Rauxr -> 1 | _ -> 0
+ in Array.of_list (List.map resmap resource_names)
+
+(** Real instructions *)
+
+exception InvalidEncoding
+
+let rec_to_usage r =
+ let encoding = match r.imm with None -> None | Some (I32 i) | Some (I64 i) -> Some (encode_imm @@ Z.to_int64 i)
+ | Some (Off ptr) -> Some (encode_imm @@ camlint64_of_ptrofs ptr)
+
+ in match r.inst with
+ | Addw | Andw | Nandw | Orw | Norw | Sbfw | Xorw
+ | Nxorw | Andnw | Ornw ->
+ (match encoding with None | Some U6 | Some S10 -> alu_tiny
+ | Some U27L5 | Some U27L10 -> alu_tiny_x
+ | _ -> raise InvalidEncoding)
+ | Sbfxw | Sbfxd ->
+ (match encoding with None -> alu_lite
+ | Some U6 | Some S10 | Some U27L5 -> alu_lite_x
+ | _ -> raise InvalidEncoding)
+ | Addd | Andd | Nandd | Ord | Nord | Sbfd | Xord
+ | Nxord | Andnd | Ornd ->
+ (match encoding with None | Some U6 | Some S10 -> alu_tiny
+ | Some U27L5 | Some U27L10 -> alu_tiny_x
+ | Some E27U27L10 -> alu_tiny_y)
+ |Cmoved ->
+ (match encoding with None | Some U6 | Some S10 -> alu_lite
+ | Some U27L5 | Some U27L10 -> alu_lite_x
+ | Some E27U27L10 -> alu_lite_y)
+ | Addxw ->
+ (match encoding with None | Some U6 | Some S10 -> alu_lite
+ | Some U27L5 | Some U27L10 -> alu_lite_x
+ | _ -> raise InvalidEncoding)
+ | Addxd ->
+ (match encoding with None | Some U6 | Some S10 -> alu_lite
+ | Some U27L5 | Some U27L10 -> alu_lite_x
+ | Some E27U27L10 -> alu_lite_y)
+ | Compw -> (match encoding with None -> alu_tiny
+ | Some U6 | Some S10 | Some U27L5 -> alu_tiny_x
+ | _ -> raise InvalidEncoding)
+ | Compd -> (match encoding with None | Some U6 | Some S10 -> alu_tiny
+ | Some U27L5 | Some U27L10 -> alu_tiny_x
+ | Some E27U27L10 -> alu_tiny_y)
+ | Fcompw -> (match encoding with None -> alu_lite
+ | Some U6 | Some S10 | Some U27L5 -> alu_lite_x
+ | _ -> raise InvalidEncoding)
+ | Fcompd -> (match encoding with None -> alu_lite
+ | Some U6 | Some S10 | Some U27L5 -> alu_lite_x
+ | _ -> raise InvalidEncoding)
+ | Make -> (match encoding with Some U6 | Some S10 -> alu_tiny
+ | Some U27L5 | Some U27L10 -> alu_tiny_x
+ | Some E27U27L10 -> alu_tiny_y
+ | _ -> raise InvalidEncoding)
+ | Maddw | Msbfw -> (match encoding with None -> mau_auxr
+ | Some U6 | Some S10 | Some U27L5 -> mau_auxr_x
+ | _ -> raise InvalidEncoding)
+ | Maddd | Msbfd -> (match encoding with None | Some U6 | Some S10 -> mau_auxr
+ | Some U27L5 | Some U27L10 -> mau_auxr_x
+ | Some E27U27L10 -> mau_auxr_y)
+ | Mulw -> (match encoding with None -> mau
+ | Some U6 | Some S10 | Some U27L5 -> mau_x
+ | _ -> raise InvalidEncoding)
+ | Muld -> (match encoding with None | Some U6 | Some S10 -> mau
+ | Some U27L5 | Some U27L10 -> mau_x
+ | Some E27U27L10 -> mau_y)
+ | Nop -> alu_nop
+ | Sraw | Srlw | Sllw | Srad | Srld | Slld -> (match encoding with None | Some U6 -> alu_tiny | _ -> raise InvalidEncoding)
+ (* TODO: check *)
+ | Srsw | Srsd | Rorw -> (match encoding with None | Some U6 -> alu_lite | _ -> raise InvalidEncoding)
+ | Extfz | Extfs | Insf -> (match encoding with None -> alu_lite | _ -> raise InvalidEncoding)
+ | Fixeduwz | Fixedwz | Floatwz | Floatuwz | Fixeddz | Fixedudz | Floatdz | Floatudz -> mau
+ | Lbs | Lbz | Lhs | Lhz | Lws | Ld | Lq | Lo ->
+ (match encoding with None | Some U6 | Some S10 -> lsu_auxw
+ | Some U27L5 | Some U27L10 -> lsu_auxw_x
+ | Some E27U27L10 -> lsu_auxw_y)
+ | Sb | Sh | Sw | Sd | Sq | So ->
+ (match encoding with None | Some U6 | Some S10 -> lsu_auxr
+ | Some U27L5 | Some U27L10 -> lsu_auxr_x
+ | Some E27U27L10 -> lsu_auxr_y)
+ | Icall | Call | Cb | Igoto | Goto | Ret | Set -> bcu
+ | Get -> bcu_tiny_tiny_mau_xnop
+ | Fnegd | Fnegw | Fabsd | Fabsw | Fwidenlwd
+ | Fmind | Fmaxd | Fminw | Fmaxw -> alu_lite
+ | Fnarrowdw -> alu_full
+ | Faddd | Faddw | Fsbfd | Fsbfw | Fmuld | Fmulw | Finvw
+ | Ffmad | Ffmaw | Ffmsd | Ffmsw -> mau
+
+
+let inst_info_to_dlatency i =
+ begin
+ assert (not (i.reads_at_id && i.reads_at_e1));
+ match i.reads_at_id with
+ | true -> +1
+ | false -> (match i.reads_at_e1 with
+ | true -> -1
+ | false -> 0)
+ end
+
+let real_inst_to_latency = function
+ | Nop -> 0 (* Only goes through ID *)
+ | Addw | Andw | Compw | Orw | Sbfw | Sbfxw | Sraw | Srsw | Srlw | Sllw | Xorw
+ (* TODO check rorw *)
+ | Rorw | Nandw | Norw | Nxorw | Ornw | Andnw
+ | Nandd | Nord | Nxord | Ornd | Andnd
+ | Addd | Andd | Compd | Ord | Sbfd | Sbfxd | Srad | Srsd | Srld | Slld | Xord | Make
+ | Extfs | Extfz | Insf | Fcompw | Fcompd | Cmoved | Addxw | Addxd
+ | Fmind | Fmaxd | Fminw | Fmaxw
+ -> 1
+ | Floatwz | Floatuwz | Fixeduwz | Fixedwz | Floatdz | Floatudz | Fixeddz | Fixedudz -> 4
+ | Mulw | Muld | Maddw | Maddd | Msbfw | Msbfd -> 2 (* FIXME - WORST CASE. If it's S10 then it's only 1 *)
+ | Lbs | Lbz | Lhs | Lhz | Lws | Ld | Lq | Lo -> 3
+ | Sb | Sh | Sw | Sd | Sq | So -> 1 (* See kvx-Optimization.pdf page 19 *)
+ | Get -> 1
+ | Set -> 4 (* According to the manual should be 3, but I measured 4 *)
+ | Icall | Call | Cb | Igoto | Goto | Ret -> 42 (* Should not matter since it's the final instruction of the basic block *)
+ | Fnegd | Fnegw | Fabsd | Fabsw | Fwidenlwd | Fnarrowdw -> 1
+ | Faddd | Faddw | Fsbfd | Fsbfw | Fmuld | Fmulw | Finvw
+ | Ffmaw | Ffmad | Ffmsw | Ffmsd -> 4
+
+let rec empty_inter la = function
+ | [] -> true
+ | b::lb -> if (List.mem b la) then false else empty_inter la lb
+
+let rec_to_info r : inst_info =
+ let usage = rec_to_usage r
+ and latency = real_inst_to_latency r.inst
+ and reads_at_id = not (empty_inter r.read_locs r.read_at_id)
+ and reads_at_e1 = not (empty_inter r.read_locs r.read_at_e1)
+ in { write_locs = r.write_locs; read_locs = r.read_locs; usage=usage; latency=latency; is_control=r.is_control;
+ reads_at_id = reads_at_id; reads_at_e1 = reads_at_e1 }
+
+let instruction_infos bb = List.map rec_to_info (instruction_recs bb)
+
+let instruction_usages bb =
+ let usages = List.map (fun info -> info.usage) (instruction_infos bb)
+ in Array.of_list usages
+
+(**
+ * Latency constraints building
+ *)
+
+(* type access = { inst: int; loc: location } *)
+
+let preg2int pr = Camlcoq.P.to_int @@ Asmblockdeps.ppos pr
+
+let loc2int = function
+ | Mem -> 1
+ | Reg pr -> preg2int pr
+
+(* module HashedLoc = struct
+ type t = { loc: location; key: int }
+ let equal l1 l2 = (l1.key = l2.key)
+ let hash l = l.key
+ let create (l:location) : t = { loc=l; key = loc2int l }
+end *)
+
+(* module LocHash = Hashtbl.Make(HashedLoc) *)
+module LocHash = Hashtbl
+
+(* Hash table : location => list of instruction ids *)
+
+let rec intlist n =
+ if n < 0 then failwith "intlist: n < 0"
+ else if n = 0 then []
+ else (n-1) :: (intlist (n-1))
+
+let find_in_hash hashloc loc =
+ match LocHash.find_opt hashloc loc with
+ | Some idl -> idl
+ | None -> []
+
+(* Returns a list of instruction ids *)
+let rec get_accesses hashloc (ll: location list) = match ll with
+ | [] -> []
+ | loc :: llocs -> (find_in_hash hashloc loc) @ (get_accesses hashloc llocs)
+
+let compute_latency (ifrom: inst_info) (ito: inst_info) =
+ let dlat = inst_info_to_dlatency ito
+ in let lat = ifrom.latency + dlat
+ in assert (lat >= 0); if (lat == 0) then 1 else lat
+
+let latency_constraints bb =
+ let written = LocHash.create 70
+ and read = LocHash.create 70
+ and count = ref 0
+ and constraints = ref []
+ and instr_infos = instruction_infos bb
+ in let step (i: inst_info) =
+ let raw = get_accesses written i.read_locs
+ and waw = get_accesses written i.write_locs
+ and war = get_accesses read i.write_locs
+ in begin
+ List.iter (fun i -> constraints := {instr_from = i; instr_to = !count;
+ latency = compute_latency (List.nth instr_infos i) (List.nth instr_infos !count)} :: !constraints) raw;
+ List.iter (fun i -> constraints := {instr_from = i; instr_to = !count;
+ latency = compute_latency (List.nth instr_infos i) (List.nth instr_infos !count)} :: !constraints) waw;
+ List.iter (fun i -> constraints := {instr_from = i; instr_to = !count; latency = 0} :: !constraints) war;
+ if i.is_control then List.iter (fun n -> constraints := {instr_from = n; instr_to = !count; latency = 0} :: !constraints) (intlist !count);
+ (* Updating "read" and "written" hashmaps *)
+ List.iter (fun loc ->
+ begin
+ LocHash.replace written loc [!count];
+ LocHash.replace read loc []; (* Clearing all the entries of "read" hashmap when a register is written *)
+ end) i.write_locs;
+ List.iter (fun loc -> LocHash.replace read loc ((!count) :: (find_in_hash read loc))) i.read_locs;
+ count := !count + 1
+ end
+ in (List.iter step instr_infos; !constraints)
+
+(**
+ * Using the InstructionScheduler
+ *)
+
+let build_problem bb =
+{ max_latency = -1;
+ resource_bounds = resource_bounds;
+ instruction_usages = instruction_usages bb;
+ latency_constraints = latency_constraints bb;
+ live_regs_entry = Registers.Regset.empty; (* unused here *)
+ typing = (fun x -> AST.Tint); (* unused here *)
+ reference_counting = None
+}
+
+let rec find_min_opt (l: int option list) =
+ match l with
+ | [] -> None
+ | e :: l ->
+ begin match find_min_opt l with
+ | None -> e
+ | Some m ->
+ begin match e with
+ | None -> Some m
+ | Some n -> if n < m then Some n else Some m
+ end
+ end
+
+let rec filter_indexes predicate = function
+ | [] -> []
+ | e :: l -> if (predicate e) then e :: (filter_indexes predicate l) else filter_indexes predicate l
+
+let get_from_indexes indexes l = List.map (List.nth l) indexes
+
+let is_basic = function PBasic _ -> true | _ -> false
+let is_control = function PControl _ -> true | _ -> false
+let to_basic = function PBasic i -> i | _ -> failwith "to_basic: control instruction found"
+let to_control = function PControl i -> i | _ -> failwith "to_control: basic instruction found"
+
+let bundlize li hd =
+ let last = List.nth li (List.length li - 1)
+ in if is_control last then
+ let cut_li = Array.to_list @@ Array.sub (Array.of_list li) 0 (List.length li - 1)
+ in let bli = List.map to_basic cut_li
+ in { header = hd; body = bli; exit = Some (to_control last) }
+ else
+ let bli = List.map to_basic li
+ in { header = hd; body = bli; exit = None }
+
+let apply_pbasic b = PBasic b
+let extract_some o = match o with Some e -> e | None -> failwith "extract_some: None found"
+
+let rec find_min = function
+ | [] -> None
+ | e :: l ->
+ match find_min l with
+ | None -> Some e
+ | Some m -> if (e < m) then Some e else Some m
+
+let rec remove_all m = function
+ | [] -> []
+ | e :: l -> if m=e then remove_all m l
+ else e :: (remove_all m l)
+
+let rec find_mins l = match find_min l with
+ | None -> []
+ | Some m -> m :: find_mins (remove_all m l)
+
+let find_all_indices m l =
+ let rec find m off = function
+ | [] -> []
+ | e :: l -> if m=e then off :: find m (off+1) l
+ else find m (off+1) l
+ in find m 0 l
+
+module TimeHash = Hashtbl
+
+(* Hash table : time => list of instruction ids *)
+
+let hashtbl2list h maxint =
+ let rec f i = match TimeHash.find_opt h i with
+ | None -> if (i > maxint) then [] else (f (i+1))
+ | Some bund -> bund :: (f (i+1))
+ in f 0
+
+let find_max l =
+ let rec f = function
+ | [] -> None
+ | e :: l -> match f l with
+ | None -> Some e
+ | Some m -> if (e > m) then Some e else Some m
+ in match (f l) with
+ | None -> raise Not_found
+ | Some m -> m
+
+(* [0, 2, 3, 1, 1, 2, 4, 5] -> [[0], [3, 4], [1, 5], [2], [6], [7]] *)
+let minpack_list (l: int list) =
+ let timehash = TimeHash.create (List.length l)
+ in let rec f i = function
+ | [] -> ()
+ | t::l -> begin
+ (match TimeHash.find_opt timehash t with
+ | None -> TimeHash.add timehash t [i]
+ | Some bund -> TimeHash.replace timehash t (bund @ [i]));
+ f (i+1) l
+ end
+ in begin
+ f 0 l;
+ hashtbl2list timehash (find_max l)
+ end;;
+
+(* let minpack_list l =
+ let mins = find_mins l
+ in List.map (fun m -> find_all_indices m l) mins
+ *)
+
+let bb_to_instrs bb = (List.map apply_pbasic bb.body) @ (match bb.exit with None -> [] | Some e -> [PControl e])
+
+let bundlize_solution bb sol =
+ let tmp = (Array.to_list @@ Array.sub sol 0 (Array.length sol - 1))
+ in let packs = minpack_list tmp
+ and instrs = bb_to_instrs bb
+ in let rec bund hd = function
+ | [] -> []
+ | pack :: packs -> bundlize (get_from_indexes pack instrs) hd :: (bund [] packs)
+ in bund bb.header packs
+
+let print_inst oc = function
+ | Asm.Pallocframe(sz, ofs) -> fprintf oc " Pallocframe\n"
+ | Asm.Pfreeframe(sz, ofs) -> fprintf oc " Pfreeframe\n"
+ | Asm.Pbuiltin(ef, args, res) -> fprintf oc " Pbuiltin\n"
+ | Asm.Pcvtl2w(rd, rs) -> fprintf oc " Pcvtl2w %a = %a\n" ireg rd ireg rs
+ | i -> print_instruction oc i
+
+let print_bb oc bb =
+ let asm_instructions = Asm.unfold_bblock bb
+ in List.iter (print_inst oc) asm_instructions
+
+let print_schedule sched =
+ print_string "[ ";
+ Array.iter (fun x -> Printf.printf "%d; " x) sched;
+ print_endline "]";;
+
+let do_schedule bb =
+ let problem = build_problem bb in
+ (if debug then print_problem stdout problem);
+ let solution = scheduler_by_name (!Clflags.option_fpostpass_sched) problem
+ in match solution with
+ | None -> failwith "Could not find a valid schedule"
+ | Some sol ->
+ ((if debug then print_schedule sol);
+ let bundles = bundlize_solution bb sol in
+ (if debug then
+ begin
+ Printf.eprintf "Scheduling the following group of instructions:\n";
+ print_bb stderr bb;
+ Printf.eprintf "Gave the following solution:\n";
+ List.iter (print_bb stderr) bundles;
+ Printf.eprintf "--------------------------------\n"
+ end;
+ bundles))
+
+(**
+ * Dumb schedule if the above doesn't work
+ *)
+
+let bundlize_label l =
+ match l with
+ | [] -> []
+ | l -> [{ header = l; body = []; exit = None }]
+
+let rec bundlize_basic l =
+ match l with
+ | [] -> []
+ | b :: l -> { header = []; body = [b]; exit = None } :: bundlize_basic l
+
+let bundlize_exit e =
+ match e with
+ | Some e -> [{ header = []; body = []; exit = Some e }]
+ | None -> []
+
+let dumb_schedule (bb : bblock) : bblock list = bundlize_label bb.header @ bundlize_basic bb.body @ bundlize_exit bb.exit
+
+(**
+ * Separates the opaque instructions such as Pfreeframe and Pallocframe
+ *)
+
+let is_opaque = function
+ | PBasic (Pallocframe _) | PBasic (Pfreeframe _) | PControl (PExpand (Pbuiltin _)) -> true
+ | _ -> false
+
+(* Returns : (accumulated instructions, remaining instructions, opaque instruction if found) *)
+let rec biggest_wo_opaque = function
+ | [] -> ([], [], None)
+ | i :: li -> if is_opaque i then ([], li, Some i)
+ else let big, rem, opaque = biggest_wo_opaque li in (i :: big, rem, opaque);;
+
+let separate_opaque bb =
+ let instrs = bb_to_instrs bb
+ in let rec f hd li =
+ match li with
+ | [] -> []
+ | li -> let big, rem, opaque = biggest_wo_opaque li in
+ match opaque with
+ | Some i ->
+ (match big with
+ | [] -> (bundlize [i] hd) :: (f [] rem)
+ | big -> (bundlize big hd) :: (bundlize [i] []) :: (f [] rem)
+ )
+ | None -> (bundlize big hd) :: (f [] rem)
+ in f bb.header instrs
+
+let smart_schedule bb =
+ let lbb = separate_opaque bb
+ in let rec f = function
+ | [] -> []
+ | bb :: lbb ->
+ let bundles =
+ try do_schedule bb
+ with OpaqueInstruction -> dumb_schedule bb
+ | e ->
+ let msg = Printexc.to_string e
+ and stack = Printexc.get_backtrace ()
+ in begin
+ Printf.eprintf "In regards to this group of instructions:\n";
+ print_bb stderr bb;
+ Printf.eprintf "Postpass scheduling could not complete: %s\n%s" msg stack;
+ failwith "Invalid schedule"
+ (*
+ Printf.eprintf "Issuing one instruction per bundle instead\n\n";
+ dumb_schedule bb
+ *)
+ end
+ in bundles @ (f lbb)
+ in f lbb
+
+let bblock_to_bundles bb =
+ if debug then (eprintf "###############################\n"; Printf.eprintf "SCHEDULING\n"; print_bb stderr bb);
+ (* print_problem (build_problem bb); *)
+ if Compopts.optim_postpass () then smart_schedule bb else dumb_schedule bb
+
+(** To deal with the Coq Axiom schedule : bblock -> (list (list basic)) * option control *)
+
+let rec bundles_to_coq_schedule = function
+ | [] -> ([], None)
+ | bb :: [] -> ([bb.body], bb.exit)
+ | bb :: lbb -> let (llb, oc) = bundles_to_coq_schedule lbb in (bb.body :: llb, oc)
+
+(** Called schedule function from Coq *)
+
+let schedule_notime bb = let toto = bundles_to_coq_schedule @@ bblock_to_bundles bb in toto
+let schedule bb = Timing.time_coq ('P'::('o'::('s'::('t'::('p'::('a'::('s'::('s'::('S'::('c'::('h'::('e'::('d'::('u'::('l'::('i'::('n'::('g'::(' '::('o'::('r'::('a'::('c'::('l'::('e'::([])))))))))))))))))))))))))) schedule_notime bb
diff --git a/kvx/PostpassSchedulingproof.v b/kvx/PostpassSchedulingproof.v
new file mode 100644
index 00000000..937b3be6
--- /dev/null
+++ b/kvx/PostpassSchedulingproof.v
@@ -0,0 +1,690 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Errors.
+Require Import Integers Floats AST Linking.
+Require Import Values Memory Events Globalenvs Smallstep.
+Require Import Op Locations Machblock Conventions Asmblock.
+Require Import Asmblockgenproof0 Asmblockprops.
+Require Import PostpassScheduling.
+Require Import Asmblockgenproof.
+Require Import Axioms.
+Require Import Lia.
+
+Local Open Scope error_monad_scope.
+
+Definition match_prog (p tp: Asmvliw.program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall p tp, transf_program p = OK tp -> match_prog p tp.
+Proof.
+ intros. eapply match_transform_partial_program; eauto.
+Qed.
+
+Lemma regset_double_set_id:
+ forall r (rs: regset) v1 v2,
+ (rs # r <- v1 # r <- v2) = (rs # r <- v2).
+Proof.
+ intros. apply functional_extensionality. intros. destruct (preg_eq r x).
+ - subst r. repeat (rewrite Pregmap.gss; auto).
+ - repeat (rewrite Pregmap.gso); auto.
+Qed.
+
+Lemma exec_body_pc_var:
+ forall l ge rs m rs' m' v,
+ exec_body ge l rs m = Next rs' m' ->
+ exec_body ge l (rs # PC <- v) m = Next (rs' # PC <- v) m'.
+Proof.
+ induction l.
+ - intros. simpl. simpl in H. inv H. auto.
+ - intros. simpl in *.
+ destruct (exec_basic_instr ge a rs m) eqn:EXEBI; try discriminate.
+ erewrite exec_basic_instr_pc_var; eauto.
+Qed.
+
+Lemma pc_set_add:
+ forall rs v r x y,
+ 0 <= x <= Ptrofs.max_unsigned ->
+ 0 <= y <= Ptrofs.max_unsigned ->
+ rs # r <- (Val.offset_ptr v (Ptrofs.repr (x + y))) = rs # r <- (Val.offset_ptr (rs # r <- (Val.offset_ptr v (Ptrofs.repr x)) r) (Ptrofs.repr y)).
+Proof.
+ intros. apply functional_extensionality. intros r0. destruct (preg_eq r r0).
+ - subst. repeat (rewrite Pregmap.gss); auto.
+ destruct v; simpl; auto.
+ rewrite Ptrofs.add_assoc.
+ enough (Ptrofs.repr (x + y) = Ptrofs.add (Ptrofs.repr x) (Ptrofs.repr y)) as ->; auto.
+ unfold Ptrofs.add.
+ enough (x + y = Ptrofs.unsigned (Ptrofs.repr x) + Ptrofs.unsigned (Ptrofs.repr y)) as ->; auto.
+ repeat (rewrite Ptrofs.unsigned_repr); auto.
+ - repeat (rewrite Pregmap.gso; auto).
+Qed.
+
+Lemma concat2_straight:
+ forall a b bb rs m rs'' m'' f ge,
+ concat2 a b = OK bb ->
+ exec_bblock ge f bb rs m = Next rs'' m'' ->
+ exists rs' m',
+ exec_bblock ge f a rs m = Next rs' m'
+ /\ rs' PC = Val.offset_ptr (rs PC) (Ptrofs.repr (size a))
+ /\ exec_bblock ge f b rs' m' = Next rs'' m''.
+Proof.
+ intros until ge. intros CONC2 EXEB.
+ exploit concat2_zlt_size; eauto. intros (LTA & LTB).
+ exploit concat2_noexit; eauto. intros EXA.
+ exploit concat2_decomp; eauto. intros. inv H.
+ unfold exec_bblock in EXEB. destruct (exec_body ge (body bb) rs m) eqn:EXEB'; try discriminate.
+ rewrite H0 in EXEB'. apply exec_body_app in EXEB'. destruct EXEB' as (rs1 & m1 & EXEB1 & EXEB2).
+ eexists; eexists. split.
+ unfold exec_bblock. rewrite EXEB1. rewrite EXA. simpl. eauto.
+ split.
+ exploit exec_body_pc. eapply EXEB1. intros. rewrite <- H. auto.
+ unfold exec_bblock. unfold nextblock, incrPC. rewrite regset_same_assign. erewrite exec_body_pc_var; eauto.
+ rewrite <- H1. unfold nextblock in EXEB. rewrite regset_double_set_id.
+ assert (size bb = size a + size b).
+ { unfold size. rewrite H0. rewrite H1. rewrite app_length. rewrite EXA. simpl. rewrite Nat.add_0_r.
+ repeat (rewrite Nat2Z.inj_add). lia. }
+ clear EXA H0 H1. rewrite H in EXEB.
+ assert (rs1 PC = rs0 PC). { apply exec_body_pc in EXEB2. auto. }
+ rewrite H0. rewrite <- pc_set_add; auto.
+ exploit size_positive. instantiate (1 := a). intro. lia.
+ exploit size_positive. instantiate (1 := b). intro. lia.
+Qed.
+
+Lemma concat_all_exec_bblock (ge: Genv.t fundef unit) (f: function) :
+ forall a bb rs m lbb rs'' m'',
+ lbb <> nil ->
+ concat_all (a :: lbb) = OK bb ->
+ exec_bblock ge f bb rs m = Next rs'' m'' ->
+ exists bb' rs' m',
+ concat_all lbb = OK bb'
+ /\ exec_bblock ge f a rs m = Next rs' m'
+ /\ rs' PC = Val.offset_ptr (rs PC) (Ptrofs.repr (size a))
+ /\ exec_bblock ge f bb' rs' m' = Next rs'' m''.
+Proof.
+ intros until m''. intros Hnonil CONC EXEB.
+ simpl in CONC.
+ destruct lbb as [|b lbb]; try contradiction. clear Hnonil.
+ monadInv CONC. exploit concat2_straight; eauto. intros (rs' & m' & EXEB1 & PCeq & EXEB2).
+ exists x. repeat econstructor. all: eauto.
+Qed.
+
+Lemma ptrofs_add_repr :
+ forall a b,
+ Ptrofs.unsigned (Ptrofs.add (Ptrofs.repr a) (Ptrofs.repr b)) = Ptrofs.unsigned (Ptrofs.repr (a + b)).
+Proof.
+ intros a b.
+ rewrite Ptrofs.add_unsigned. repeat (rewrite Ptrofs.unsigned_repr_eq).
+ rewrite <- Zplus_mod. auto.
+Qed.
+
+Section PRESERVATION_ASMBLOCK.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma transf_function_no_overflow:
+ forall f tf,
+ transf_function f = OK tf -> size_blocks tf.(fn_blocks) <= Ptrofs.max_unsigned.
+Proof.
+ intros. monadInv H. destruct (zlt Ptrofs.max_unsigned (size_blocks x.(fn_blocks))); inv EQ0.
+ lia.
+Qed.
+
+Lemma symbols_preserved:
+ forall id,
+ Genv.find_symbol tge id = Genv.find_symbol ge id.
+Proof (Genv.find_symbol_match TRANSL).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSL).
+
+Lemma functions_translated:
+ forall v f,
+ Genv.find_funct ge v = Some f ->
+ exists tf,
+ Genv.find_funct tge v = Some tf /\ transf_fundef f = OK tf.
+Proof (Genv.find_funct_transf_partial TRANSL).
+
+Lemma function_ptr_translated:
+ forall v f,
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ transf_fundef f = OK tf.
+Proof (Genv.find_funct_ptr_transf_partial TRANSL).
+
+Lemma functions_transl:
+ forall fb f tf,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ transf_function f = OK tf ->
+ Genv.find_funct_ptr tge fb = Some (Internal tf).
+Proof.
+ intros. exploit function_ptr_translated; eauto.
+ intros (tf' & A & B). monadInv B. rewrite H0 in EQ. inv EQ. auto.
+Qed.
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro:
+ forall s1 s2, s1 = s2 -> match_states s1 s2.
+
+Lemma prog_main_preserved:
+ prog_main tprog = prog_main prog.
+Proof (match_program_main TRANSL).
+
+Lemma prog_main_address_preserved:
+ (Genv.symbol_address (Genv.globalenv prog) (prog_main prog) Ptrofs.zero) =
+ (Genv.symbol_address (Genv.globalenv tprog) (prog_main tprog) Ptrofs.zero).
+Proof.
+ unfold Genv.symbol_address. rewrite symbols_preserved.
+ rewrite prog_main_preserved. auto.
+Qed.
+
+Lemma transf_initial_states:
+ forall st1, initial_state prog st1 ->
+ exists st2, initial_state tprog st2 /\ match_states st1 st2.
+Proof.
+ intros. inv H.
+ econstructor; split.
+ - eapply initial_state_intro.
+ eapply (Genv.init_mem_transf_partial TRANSL); eauto.
+ - econstructor; eauto. subst ge0. subst rs0. rewrite prog_main_address_preserved. auto.
+Qed.
+
+Lemma transf_final_states:
+ forall st1 st2 r,
+ match_states st1 st2 -> final_state st1 r -> final_state st2 r.
+Proof.
+ intros. inv H0. inv H. econstructor; eauto.
+Qed.
+
+Lemma tail_find_bblock:
+ forall lbb pos bb,
+ find_bblock pos lbb = Some bb ->
+ exists c, code_tail pos lbb (bb::c).
+Proof.
+ induction lbb.
+ - intros. simpl in H. inv H.
+ - intros. simpl in H.
+ destruct (zlt pos 0); try (inv H; fail).
+ destruct (zeq pos 0).
+ + inv H. exists lbb. constructor; auto.
+ + apply IHlbb in H. destruct H as (c & TAIL). exists c.
+ enough (pos = pos - size a + size a) as ->.
+ apply code_tail_S; auto.
+ lia.
+Qed.
+
+Lemma code_tail_head_app:
+ forall l pos c1 c2,
+ code_tail pos c1 c2 ->
+ code_tail (pos + size_blocks l) (l++c1) c2.
+Proof.
+ induction l.
+ - intros. simpl. rewrite Z.add_0_r. auto.
+ - intros. apply IHl in H. simpl. rewrite (Z.add_comm (size a)). rewrite Z.add_assoc. apply code_tail_S. assumption.
+Qed.
+
+Lemma transf_blocks_verified:
+ forall c tc pos bb c',
+ transf_blocks c = OK tc ->
+ code_tail pos c (bb::c') ->
+ exists lbb,
+ verified_schedule bb = OK lbb
+ /\ exists tc', code_tail pos tc (lbb ++ tc').
+Proof.
+ induction c; intros.
+ - simpl in H. inv H. inv H0.
+ - inv H0.
+ + monadInv H. exists x0.
+ split; simpl; auto. eexists; eauto. econstructor; eauto.
+ + unfold transf_blocks in H. fold transf_blocks in H. monadInv H.
+ exploit IHc; eauto.
+ intros (lbb & TRANS & tc' & TAIL).
+(* monadInv TRANS. *)
+ repeat eexists; eauto.
+ erewrite verified_schedule_size; eauto.
+ apply code_tail_head_app.
+ eauto.
+Qed.
+
+Lemma transf_find_bblock:
+ forall ofs f bb tf,
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bb ->
+ transf_function f = OK tf ->
+ exists lbb,
+ verified_schedule bb = OK lbb
+ /\ exists c, code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) (lbb ++ c).
+Proof.
+ intros.
+ monadInv H0. destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks x))); try (inv EQ0; fail). inv EQ0.
+ monadInv EQ. apply tail_find_bblock in H. destruct H as (c & TAIL).
+ eapply transf_blocks_verified; eauto.
+Qed.
+
+Lemma symbol_address_preserved:
+ forall l ofs, Genv.symbol_address ge l ofs = Genv.symbol_address tge l ofs.
+Proof.
+ intros. unfold Genv.symbol_address. repeat (rewrite symbols_preserved). reflexivity.
+Qed.
+
+Lemma head_tail {A: Type}:
+ forall (l: list A) hd, hd::l = hd :: (tail (hd::l)).
+Proof.
+ intros. simpl. auto.
+Qed.
+
+Lemma verified_schedule_not_empty:
+ forall bb lbb,
+ verified_schedule bb = OK lbb -> lbb <> nil.
+Proof.
+ intros. apply verified_schedule_size in H.
+ pose (size_positive bb). assert (size_blocks lbb > 0) by lia. clear H g.
+ destruct lbb; simpl in *; discriminate.
+Qed.
+
+Lemma header_nil_label_pos_none:
+ forall lbb l p,
+ Forall (fun b => header b = nil) lbb -> label_pos l p lbb = None.
+Proof.
+ induction lbb.
+ - intros. simpl. auto.
+ - intros. inv H. simpl. unfold is_label. rewrite H2. destruct (in_dec l nil). { inv i. }
+ auto.
+Qed.
+
+Lemma verified_schedule_label:
+ forall bb tbb lbb l,
+ verified_schedule bb = OK (tbb :: lbb) ->
+ is_label l bb = is_label l tbb
+ /\ label_pos l 0 lbb = None.
+Proof.
+ intros. exploit verified_schedule_header; eauto.
+ intros (HdrEq & HdrNil).
+ split.
+ - unfold is_label. rewrite HdrEq. reflexivity.
+ - apply header_nil_label_pos_none. assumption.
+Qed.
+
+Lemma label_pos_app_none:
+ forall c c' l p p',
+ label_pos l p c = None ->
+ label_pos l (p' + size_blocks c) c' = label_pos l p' (c ++ c').
+Proof.
+ induction c.
+ - intros. simpl in *. rewrite Z.add_0_r. reflexivity.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLABEL.
+ + discriminate.
+ + eapply IHc in H. rewrite Z.add_assoc. eauto.
+Qed.
+
+Remark label_pos_pvar_none_add:
+ forall tc l p p' k,
+ label_pos l (p+k) tc = None -> label_pos l (p'+k) tc = None.
+Proof.
+ induction tc.
+ - intros. simpl. auto.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + discriminate.
+ + pose (IHtc l p p' (k + size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar_none:
+ forall tc l p p',
+ label_pos l p tc = None -> label_pos l p' tc = None.
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_none_add; eauto.
+Qed.
+
+Remark label_pos_pvar_some_add_add:
+ forall tc l p p' k k',
+ label_pos l (p+k') tc = Some (p+k) -> label_pos l (p'+k') tc = Some (p'+k).
+Proof.
+ induction tc.
+ - intros. simpl in H. discriminate.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + inv H. assert (k = k') by lia. subst. reflexivity.
+ + pose (IHtc l p p' k (k' + size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar_some_add:
+ forall tc l p p' k,
+ label_pos l p tc = Some (p+k) -> label_pos l p' tc = Some (p'+k).
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_some_add_add; eauto.
+Qed.
+
+Remark label_pos_pvar_add:
+ forall c tc l p p' k,
+ label_pos l (p+k) c = label_pos l p tc ->
+ label_pos l (p'+k) c = label_pos l p' tc.
+Proof.
+ induction c.
+ - intros. simpl in *.
+ exploit label_pos_pvar_none; eauto.
+ - intros. simpl in *. destruct (is_label _ _) eqn:ISLBL.
+ + exploit label_pos_pvar_some_add; eauto.
+ + pose (IHc tc l p p' (k+size a)). repeat (rewrite Z.add_assoc in e). auto.
+Qed.
+
+Lemma label_pos_pvar:
+ forall c tc l p p',
+ label_pos l p c = label_pos l p tc ->
+ label_pos l p' c = label_pos l p' tc.
+Proof.
+ intros. rewrite (Zplus_0_r_reverse p') at 1. rewrite (Zplus_0_r_reverse p) in H at 1.
+ eapply label_pos_pvar_add; eauto.
+Qed.
+
+Lemma label_pos_head_app:
+ forall c bb lbb l tc p,
+ verified_schedule bb = OK lbb ->
+ label_pos l p c = label_pos l p tc ->
+ label_pos l p (bb :: c) = label_pos l p (lbb ++ tc).
+Proof.
+ intros. simpl. destruct lbb as [|tbb lbb].
+ - apply verified_schedule_not_empty in H. contradiction.
+ - simpl. exploit verified_schedule_label; eauto. intros (ISLBL & LBLPOS).
+ rewrite ISLBL.
+ destruct (is_label l tbb) eqn:ISLBL'; simpl; auto.
+ eapply label_pos_pvar in H0. erewrite H0.
+ erewrite verified_schedule_size; eauto. simpl size_blocks. rewrite Z.add_assoc.
+ erewrite label_pos_app_none; eauto.
+Qed.
+
+Lemma label_pos_preserved:
+ forall c tc l,
+ transf_blocks c = OK tc -> label_pos l 0 c = label_pos l 0 tc.
+Proof.
+ induction c.
+ - intros. simpl in *. inv H. reflexivity.
+ - intros. unfold transf_blocks in H; fold transf_blocks in H. monadInv H. eapply IHc in EQ.
+ eapply label_pos_head_app; eauto.
+Qed.
+
+Lemma label_pos_preserved_blocks:
+ forall l f tf,
+ transf_function f = OK tf ->
+ label_pos l 0 (fn_blocks f) = label_pos l 0 (fn_blocks tf).
+Proof.
+ intros. monadInv H. monadInv EQ.
+ destruct (zlt Ptrofs.max_unsigned _); try discriminate.
+ monadInv EQ0. simpl. eapply label_pos_preserved; eauto.
+Qed.
+
+Lemma transf_exec_control:
+ forall f tf ex rs m,
+ transf_function f = OK tf ->
+ exec_control ge f ex rs m = exec_control tge tf ex rs m.
+Proof.
+ intros. destruct ex; simpl; auto.
+ assert (ge = Genv.globalenv prog). auto.
+ assert (tge = Genv.globalenv tprog). auto.
+ pose symbol_address_preserved.
+ exploreInst; simpl; auto; try congruence;
+ unfold par_goto_label; unfold par_eval_branch; unfold par_goto_label; erewrite label_pos_preserved_blocks; eauto.
+Qed.
+
+Lemma transf_exec_basic_instr:
+ forall i rs m, exec_basic_instr ge i rs m = exec_basic_instr tge i rs m.
+Proof.
+ intros. pose symbol_address_preserved.
+ unfold exec_basic_instr. unfold bstep. exploreInst; simpl; auto; try congruence.
+ unfold parexec_arith_instr; unfold arith_eval_r; exploreInst; simpl; auto; try congruence.
+Qed.
+
+Lemma transf_exec_body:
+ forall bdy rs m, exec_body ge bdy rs m = exec_body tge bdy rs m.
+Proof.
+ induction bdy; intros.
+ - simpl. reflexivity.
+ - simpl. rewrite transf_exec_basic_instr.
+ destruct (exec_basic_instr _ _ _); auto.
+Qed.
+
+Lemma transf_exec_bblock:
+ forall f tf bb rs m,
+ transf_function f = OK tf ->
+ exec_bblock ge f bb rs m = exec_bblock tge tf bb rs m.
+Proof.
+ intros. unfold exec_bblock. rewrite transf_exec_body. destruct (exec_body _ _ _ _); auto.
+ eapply transf_exec_control; eauto.
+Qed.
+
+Lemma transf_step_simu:
+ forall tf b lbb ofs c tbb rs m rs' m',
+ Genv.find_funct_ptr tge b = Some (Internal tf) ->
+ size_blocks (fn_blocks tf) <= Ptrofs.max_unsigned ->
+ rs PC = Vptr b ofs ->
+ code_tail (Ptrofs.unsigned ofs) (fn_blocks tf) (lbb ++ c) ->
+ concat_all lbb = OK tbb ->
+ exec_bblock tge tf tbb rs m = Next rs' m' ->
+ plus step tge (State rs m) E0 (State rs' m').
+Proof.
+ induction lbb.
+ - intros until m'. simpl. intros. discriminate.
+ - intros until m'. intros GFIND SIZE PCeq TAIL CONC EXEB.
+ destruct lbb.
+ + simpl in *. clear IHlbb. inv CONC. eapply plus_one. econstructor; eauto. eapply find_bblock_tail; eauto.
+ + exploit concat_all_exec_bblock; eauto; try discriminate.
+ intros (tbb0 & rs0 & m0 & CONC0 & EXEB0 & PCeq' & EXEB1).
+ eapply plus_left.
+ econstructor.
+ 3: eapply find_bblock_tail. rewrite <- app_comm_cons in TAIL. 3: eauto.
+ all: eauto.
+ eapply plus_star. eapply IHlbb; eauto. rewrite PCeq in PCeq'. simpl in PCeq'. all: eauto.
+ eapply code_tail_next_int; eauto.
+Qed.
+
+Theorem transf_step_correct:
+ forall s1 t s2, step ge s1 t s2 ->
+ forall s1' (MS: match_states s1 s1'),
+ (exists s2', plus step tge s1' t s2' /\ match_states s2 s2').
+Proof.
+ induction 1; intros; inv MS.
+ - exploit function_ptr_translated; eauto. intros (tf & FFP & TRANSF). monadInv TRANSF.
+ exploit transf_find_bblock; eauto. intros (lbb & VES & c & TAIL).
+ exploit verified_schedule_correct; eauto. intros (tbb & CONC & BBEQ). inv CONC. rename H3 into CONC.
+ assert (NOOV: size_blocks x.(fn_blocks) <= Ptrofs.max_unsigned).
+ eapply transf_function_no_overflow; eauto.
+
+ erewrite transf_exec_bblock in H2; eauto.
+ unfold bblock_simu in BBEQ. rewrite BBEQ in H2; try congruence.
+ exists (State rs' m'). split; try (constructor; auto).
+ eapply transf_step_simu; eauto.
+
+ - exploit function_ptr_translated; eauto. intros (tf & FFP & TRANSF). monadInv TRANSF.
+ exploit transf_find_bblock; eauto. intros (lbb & VES & c & TAIL).
+ exploit verified_schedule_builtin_idem; eauto. intros. subst lbb.
+
+ remember (State (nextblock _ _) _) as s'. exists s'.
+ split; try constructor; auto.
+ eapply plus_one. subst s'.
+ eapply exec_step_builtin.
+ 3: eapply find_bblock_tail. simpl in TAIL. 3: eauto.
+ all: eauto.
+ eapply eval_builtin_args_preserved with (ge1 := ge). exact symbols_preserved. eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+
+ - exploit function_ptr_translated; eauto. intros (tf & FFP & TRANSF). monadInv TRANSF.
+ remember (State _ m') as s'. exists s'. split; try constructor; auto.
+ subst s'. eapply plus_one. eapply exec_step_external; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+Qed.
+
+Theorem transf_program_correct_Asmblock:
+ forward_simulation (Asmblock.semantics prog) (Asmblock.semantics tprog).
+Proof.
+ eapply forward_simulation_plus.
+ - apply senv_preserved.
+ - apply transf_initial_states.
+ - apply transf_final_states.
+ - apply transf_step_correct.
+Qed.
+
+End PRESERVATION_ASMBLOCK.
+
+Require Import Asmvliw.
+
+Lemma verified_par_checks_alls_bundles lb x: forall bundle,
+ verify_par lb = OK x ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ induction lb; simpl; try tauto.
+ intros bundle H; monadInv H.
+ destruct 1; subst; eauto.
+ destruct x0; auto.
+Qed.
+
+Lemma verified_schedule_nob_checks_alls_bundles bb lb bundle:
+ verified_schedule_nob bb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ unfold verified_schedule_nob. intros H;
+ monadInv H. destruct x4.
+ intros; eapply verified_par_checks_alls_bundles; eauto.
+Qed.
+
+Lemma verify_par_bblock_PExpand bb i:
+ exit bb = Some (PExpand i) -> verify_par_bblock bb = OK tt.
+Proof.
+ destruct bb as [h bdy ext H]; simpl.
+ intros; subst. destruct i.
+ generalize H.
+ rewrite <- wf_bblock_refl in H.
+ destruct H as [H H0].
+ unfold builtin_alone in H0. erewrite H0; eauto.
+Qed.
+
+Local Hint Resolve verified_schedule_nob_checks_alls_bundles: core.
+
+Lemma verified_schedule_checks_alls_bundles bb lb bundle:
+ verified_schedule bb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ unfold verified_schedule. remember (exit bb) as exb.
+ destruct exb as [c|]; eauto.
+ destruct c as [i|]; eauto.
+ destruct i; intros H. inversion_clear H; simpl.
+ intuition subst.
+ intros; eapply verify_par_bblock_PExpand; eauto.
+Qed.
+
+Lemma transf_blocks_checks_all_bundles lbb: forall lb bundle,
+ transf_blocks lbb = OK lb ->
+ List.In bundle lb -> verify_par_bblock bundle = OK tt.
+Proof.
+ induction lbb; simpl.
+ - intros lb bundle H; inversion_clear H. simpl; try tauto.
+ - intros lb bundle H0.
+ monadInv H0.
+ rewrite in_app. destruct 1; eauto.
+ eapply verified_schedule_checks_alls_bundles; eauto.
+Qed.
+
+Lemma find_bblock_Some_in lb:
+ forall ofs b, find_bblock ofs lb = Some b -> List.In b lb.
+Proof.
+ induction lb; simpl; try congruence.
+ intros ofs b.
+ destruct (zlt ofs 0); try congruence.
+ destruct (zeq ofs 0); eauto.
+ intros X; inversion X; eauto.
+Qed.
+
+Section PRESERVATION_ASMVLIW.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma all_bundles_are_checked b ofs f bundle:
+ Genv.find_funct_ptr (globalenv (Asmblock.semantics tprog)) b = Some (Internal f) ->
+ find_bblock ofs (fn_blocks f) = Some bundle ->
+ verify_par_bblock bundle = OK tt.
+Proof.
+ unfold match_prog, match_program in TRANSL.
+ unfold Genv.find_funct_ptr; simpl; intros X.
+ destruct (Genv.find_def_match_2 TRANSL b) as [|f0 y H]; try congruence.
+ destruct y as [tf0|]; try congruence.
+ inversion X as [H1]. subst. clear X.
+ remember (@Gfun fundef unit (Internal f)) as f2.
+ destruct H as [ctx' f1 f2 H0|]; try congruence.
+ inversion Heqf2 as [H2]. subst; clear Heqf2.
+ unfold transf_fundef, transf_partial_fundef in H.
+ destruct f1 as [f1|f1]; try congruence.
+ unfold transf_function, transl_function in H.
+ monadInv H. monadInv EQ.
+ destruct (zlt Ptrofs.max_unsigned (size_blocks (fn_blocks _))); simpl in *|-; try congruence.
+ injection EQ1; intros; subst.
+ monadInv EQ0. simpl in * |-.
+ intros; exploit transf_blocks_checks_all_bundles; eauto.
+ intros; eapply find_bblock_Some_in; eauto.
+Qed.
+
+Lemma checked_bundles_are_parexec_equiv f bundle rs rs' m m':
+ exec_bblock (globalenv (Asmblock.semantics tprog)) f bundle rs m = Next rs' m' ->
+ verify_par_bblock bundle = OK tt ->
+ det_parexec (globalenv (semantics tprog)) f bundle rs m rs' m'.
+Proof.
+ intros. unfold verify_par_bblock in H0. destruct (Asmblockdeps.bblock_para_check _) eqn:BPC; try discriminate. clear H0.
+ simpl in H.
+ eapply Asmblockdeps.bblock_para_check_correct; eauto.
+Qed.
+
+Lemma seqexec_parexec_equiv b ofs f bundle rs rs' m m':
+ Genv.find_funct_ptr (globalenv (Asmblock.semantics tprog)) b = Some (Internal f) ->
+ find_bblock (Ptrofs.unsigned ofs) (fn_blocks f) = Some bundle ->
+ exec_bblock (globalenv (Asmblock.semantics tprog)) f bundle rs m = Next rs' m' ->
+ det_parexec (globalenv (semantics tprog)) f bundle rs m rs' m'.
+Proof.
+ intros; eapply checked_bundles_are_parexec_equiv; eauto.
+ eapply all_bundles_are_checked; eauto.
+Qed.
+
+Theorem transf_program_correct_Asmvliw:
+ forward_simulation (Asmblock.semantics tprog) (Asmvliw.semantics tprog).
+Proof.
+ eapply forward_simulation_step with (match_states:=fun (s1:Asmvliw.state) s2 => s1=s2); eauto.
+ - intros; subst; auto.
+ - intros s1 t s1' H s2 H0; subst; inversion H; clear H; subst; eexists; split; eauto.
+ + eapply exec_step_internal; eauto.
+ intros; eapply seqexec_parexec_equiv; eauto.
+ + eapply exec_step_builtin; eauto.
+ + eapply exec_step_external; eauto.
+Qed.
+
+End PRESERVATION_ASMVLIW.
+
+Section PRESERVATION.
+
+Variables prog tprog: program.
+Hypothesis TRANSL: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Theorem transf_program_correct:
+ forward_simulation (Asmblock.semantics prog) (Asmvliw.semantics tprog).
+Proof.
+ eapply compose_forward_simulations.
+ eapply transf_program_correct_Asmblock; eauto.
+ eapply transf_program_correct_Asmvliw; eauto.
+Qed.
+
+End PRESERVATION.
diff --git a/kvx/PrepassSchedulingOracle.ml b/kvx/PrepassSchedulingOracle.ml
new file mode 120000
index 00000000..912e9ffa
--- /dev/null
+++ b/kvx/PrepassSchedulingOracle.ml
@@ -0,0 +1 @@
+../aarch64/PrepassSchedulingOracle.ml \ No newline at end of file
diff --git a/kvx/PrepassSchedulingOracleDeps.ml b/kvx/PrepassSchedulingOracleDeps.ml
new file mode 120000
index 00000000..1e955b85
--- /dev/null
+++ b/kvx/PrepassSchedulingOracleDeps.ml
@@ -0,0 +1 @@
+../aarch64/PrepassSchedulingOracleDeps.ml \ No newline at end of file
diff --git a/kvx/PrintOp.ml b/kvx/PrintOp.ml
new file mode 100644
index 00000000..da7d6c32
--- /dev/null
+++ b/kvx/PrintOp.ml
@@ -0,0 +1,229 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Pretty-printing of operators, conditions, addressing modes *)
+
+open Printf
+open Camlcoq
+open Integers
+open Op
+open ExtValues
+
+let comparison_name = function
+ | Ceq -> "=="
+ | Cne -> "!="
+ | Clt -> "<"
+ | Cle -> "<="
+ | Cgt -> ">"
+ | Cge -> ">="
+
+let print_condition reg pp = function
+ | (Ccomp c, [r1;r2]) ->
+ fprintf pp "%a %ss %a" reg r1 (comparison_name c) reg r2
+ | (Ccompu c, [r1;r2]) ->
+ fprintf pp "%a %su %a" reg r1 (comparison_name c) reg r2
+ | (Ccompimm(c, n), [r1]) ->
+ fprintf pp "%a %ss %ld" reg r1 (comparison_name c) (camlint_of_coqint n)
+ | (Ccompuimm(c, n), [r1]) ->
+ fprintf pp "%a %su %ld" reg r1 (comparison_name c) (camlint_of_coqint n)
+ | (Ccompf c, [r1;r2]) ->
+ fprintf pp "%a %sf %a" reg r1 (comparison_name c) reg r2
+ | (Ccompl c, [r1;r2]) ->
+ fprintf pp "%a %sls %a" reg r1 (comparison_name c) reg r2
+ | (Ccomplu c, [r1;r2]) ->
+ fprintf pp "%a %slu %a" reg r1 (comparison_name c) reg r2
+ | (Ccomplimm(c, n), [r1]) ->
+ fprintf pp "%a %sls %Ld" reg r1 (comparison_name c) (camlint64_of_coqint n)
+ | (Ccompluimm(c, n), [r1]) ->
+ fprintf pp "%a %slu %Lu" reg r1 (comparison_name c) (camlint64_of_coqint n)
+ | (Cnotcompf c, [r1;r2]) ->
+ fprintf pp "%a not(%sf) %a" reg r1 (comparison_name c) reg r2
+ | (Ccompfs c, [r1;r2]) ->
+ fprintf pp "%a %sfs %a" reg r1 (comparison_name c) reg r2
+ | (Cnotcompfs c, [r1;r2]) ->
+ fprintf pp "%a not(%sfs) %a" reg r1 (comparison_name c) reg r2
+ | _ ->
+ fprintf pp "<bad condition>"
+
+let print_condition0 reg pp cond0 rc =
+ match cond0 with
+ | Ccomp0 c -> fprintf pp "%a %ss 0" reg rc (comparison_name c)
+ | Ccompu0 c -> fprintf pp "%a %su 0" reg rc (comparison_name c)
+ | Ccompl0 c -> fprintf pp "%a %ss 0" reg rc (comparison_name c)
+ | Ccomplu0 c -> fprintf pp "%a %su 0" reg rc (comparison_name c)
+
+let int_of_s14 = function
+ | SHIFT1 -> 1
+ | SHIFT2 -> 2
+ | SHIFT3 -> 3
+ | SHIFT4 -> 4
+
+let print_operation reg pp op = match op with
+ | Omove, [r1] -> reg pp r1
+ | Ointconst n, [] -> fprintf pp "%ld" (camlint_of_coqint n)
+ | Olongconst n, [] -> fprintf pp "%LdL" (camlint64_of_coqint n)
+ | Ofloatconst n, [] -> fprintf pp "%F" (camlfloat_of_coqfloat n)
+ | Osingleconst n, [] -> fprintf pp "%Ff" (camlfloat_of_coqfloat32 n)
+ | Oaddrsymbol(id, ofs), [] ->
+ fprintf pp "\"%s\" + %Ld" (extern_atom id) (camlint64_of_ptrofs ofs)
+ | Oaddrstack ofs, [] ->
+ fprintf pp "stack(%Ld)" (camlint64_of_ptrofs ofs)
+ | Ocast8signed, [r1] -> fprintf pp "int8signed(%a)" reg r1
+ | Ocast16signed, [r1] -> fprintf pp "int16signed(%a)" reg r1
+ | Oadd, [r1;r2] -> fprintf pp "%a + %a" reg r1 reg r2
+ | Oaddimm n, [r1] -> fprintf pp "%a + %ld" reg r1 (camlint_of_coqint n)
+ | Oaddx(s14), [r1; r2] -> fprintf pp "(%a << %d) + %a" reg r1 (int_of_s14 s14) reg r2
+ | Oaddximm(s14, imm), [r1] -> fprintf pp "(%a << %d) + %ld" reg r1 (int_of_s14 s14) (camlint_of_coqint imm)
+ | Oneg, [r1] -> fprintf pp "-(%a)" reg r1
+ | Osub, [r1;r2] -> fprintf pp "%a - %a" reg r1 reg r2
+ | Orevsubimm(imm), [r1] -> fprintf pp "%ld - %a" (camlint_of_coqint imm) reg r1
+ | Orevsubx(s14), [r1; r2] -> fprintf pp "%a - (%a << %d)" reg r2 reg r1 (int_of_s14 s14)
+ | Orevsubximm(s14, imm), [r1] -> fprintf pp "%ld - (%a << %d)" (camlint_of_coqint imm) reg r1 (int_of_s14 s14)
+ | Omul, [r1;r2] -> fprintf pp "%a * %a" reg r1 reg r2
+ | Omulimm(imm), [r1] -> fprintf pp "%a * %ld" reg r1 (camlint_of_coqint imm)
+ | Omulhs, [r1;r2] -> fprintf pp "%a *hs %a" reg r1 reg r2
+ | Omulhu, [r1;r2] -> fprintf pp "%a *hu %a" reg r1 reg r2
+ | Odiv, [r1;r2] -> fprintf pp "%a /s %a" reg r1 reg r2
+ | Odivu, [r1;r2] -> fprintf pp "%a /u %a" reg r1 reg r2
+ | Omod, [r1;r2] -> fprintf pp "%a %%s %a" reg r1 reg r2
+ | Omodu, [r1;r2] -> fprintf pp "%a %%u %a" reg r1 reg r2
+ | Oand, [r1;r2] -> fprintf pp "%a & %a" reg r1 reg r2
+ | Oandimm n, [r1] -> fprintf pp "%a & %ld" reg r1 (camlint_of_coqint n)
+ | Oor, [r1;r2] -> fprintf pp "%a | %a" reg r1 reg r2
+ | Oorimm n, [r1] -> fprintf pp "%a | %ld" reg r1 (camlint_of_coqint n)
+ | Oxor, [r1;r2] -> fprintf pp "%a ^ %a" reg r1 reg r2
+ | Oxorimm n, [r1] -> fprintf pp "%a ^ %ld" reg r1 (camlint_of_coqint n)
+ | Onxor, [r1;r2] -> fprintf pp "~(%a ^ %a)" reg r1 reg r2
+ | Onxorimm n, [r1] -> fprintf pp "~(%a ^ %ld)" reg r1 (camlint_of_coqint n)
+ | Onot, [r1] -> fprintf pp "~%a" reg r1
+ | Oandn, [r1; r2] -> fprintf pp "(~%a) & %a" reg r1 reg r2
+ | Oandnimm n, [r1] -> fprintf pp "(~%a) & %ld" reg r1 (camlint_of_coqint n)
+ | Oorn, [r1;r2] -> fprintf pp "(~%a) | %a" reg r1 reg r2
+ | Oornimm n, [r1] -> fprintf pp "(~%a) | %ld" reg r1 (camlint_of_coqint n)
+ | Oshl, [r1;r2] -> fprintf pp "%a << %a" reg r1 reg r2
+ | Oshlimm n, [r1] -> fprintf pp "%a << %ld" reg r1 (camlint_of_coqint n)
+ | Oshr, [r1;r2] -> fprintf pp "%a >>s %a" reg r1 reg r2
+ | Oshrimm n, [r1] -> fprintf pp "%a >>s %ld" reg r1 (camlint_of_coqint n)
+ | Oshru, [r1;r2] -> fprintf pp "%a >>u %a" reg r1 reg r2
+ | Oshruimm n, [r1] -> fprintf pp "%a >>u %ld" reg r1 (camlint_of_coqint n)
+ | Oshrximm n, [r1] -> fprintf pp "%a >>x %ld" reg r1 (camlint_of_coqint n)
+ | Ororimm n, [r1] -> fprintf pp "(%a ror %ld)" reg r1 (camlint_of_coqint n)
+ | Omadd, [r1; r2; r3] -> fprintf pp "%a + %a * %a" reg r1 reg r2 reg r3
+ | Omaddimm imm, [r1; r2] -> fprintf pp "%a + %a * %ld" reg r1 reg r2 (camlint_of_coqint imm)
+ | Omsub, [r1; r2; r3] -> fprintf pp "%a - %a * %a" reg r1 reg r2 reg r3
+
+ | Omakelong, [r1;r2] -> fprintf pp "makelong(%a,%a)" reg r1 reg r2
+ | Olowlong, [r1] -> fprintf pp "lowlong(%a)" reg r1
+ | Ohighlong, [r1] -> fprintf pp "highlong(%a)" reg r1
+ | Ocast32signed, [r1] -> fprintf pp "long32signed(%a)" reg r1
+ | Ocast32unsigned, [r1] -> fprintf pp "long32unsigned(%a)" reg r1
+ | Oaddl, [r1;r2] -> fprintf pp "%a +l %a" reg r1 reg r2
+ | Oaddlimm n, [r1] -> fprintf pp "%a +l %Ld" reg r1 (camlint64_of_coqint n)
+ | Oaddxl(s14), [r1; r2] -> fprintf pp "(%a <<l %d) +l %a" reg r1 (int_of_s14 s14) reg r2
+ | Oaddxlimm(s14, imm), [r1] -> fprintf pp "(%a <<l %d) +l %Ld" reg r1 (int_of_s14 s14) (camlint64_of_coqint imm)
+ | Orevsublimm(imm), [r1] -> fprintf pp "%Ld -l %a" (camlint64_of_coqint imm) reg r1
+ | Orevsubxl(s14), [r1; r2] -> fprintf pp "%a -l (%a <<l %d)" reg r2 reg r1 (int_of_s14 s14)
+ | Orevsubxlimm(s14, imm), [r1] -> fprintf pp "%Ld -l (%a <<l %d)" (camlint64_of_coqint imm) reg r1 (int_of_s14 s14)
+ | Onegl, [r1] -> fprintf pp "-l (%a)" reg r1
+ | Osubl, [r1;r2] -> fprintf pp "%a -l %a" reg r1 reg r2
+ | Omull, [r1;r2] -> fprintf pp "%a *l %a" reg r1 reg r2
+ | Omullimm(imm), [r1] -> fprintf pp "%a *l %Ld" reg r1 (camlint64_of_coqint imm)
+ | Omullhs, [r1;r2] -> fprintf pp "%a *lhs %a" reg r1 reg r2
+ | Omullhu, [r1;r2] -> fprintf pp "%a *lhu %a" reg r1 reg r2
+ | Odivl, [r1;r2] -> fprintf pp "%a /ls %a" reg r1 reg r2
+ | Odivlu, [r1;r2] -> fprintf pp "%a /lu %a" reg r1 reg r2
+ | Omodl, [r1;r2] -> fprintf pp "%a %%ls %a" reg r1 reg r2
+ | Omodlu, [r1;r2] -> fprintf pp "%a %%lu %a" reg r1 reg r2
+ | Oandl, [r1;r2] -> fprintf pp "%a &l %a" reg r1 reg r2
+ | Oandlimm n, [r1] -> fprintf pp "%a &l %Ld" reg r1 (camlint64_of_coqint n)
+ | Oorl, [r1;r2] -> fprintf pp "%a |l %a" reg r1 reg r2
+ | Oorlimm n, [r1] -> fprintf pp "%a |l %Ld" reg r1 (camlint64_of_coqint n)
+ | Onorl, [r1; r2] -> fprintf pp "~(%a |l %a)" reg r1 reg r2
+ | Onorlimm n, [r1] -> fprintf pp "~(%a |l %Ld)" reg r1 (camlint64_of_coqint n)
+ | Oxorl, [r1;r2] -> fprintf pp "%a ^l %a" reg r1 reg r2
+ | Oxorlimm n, [r1] -> fprintf pp "%a ^l %Ld" reg r1 (camlint64_of_coqint n)
+ | Onxorl, [r1;r2] -> fprintf pp "~(%a ^l %a)" reg r1 reg r2
+ | Onxorlimm n, [r1] -> fprintf pp "~(%a ^l %Ld)" reg r1 (camlint64_of_coqint n)
+ | Onotl, [r1] -> fprintf pp "~%a" reg r1
+ | Oandnl, [r1;r2] -> fprintf pp "(~%a) &l %a" reg r1 reg r2
+ | Oandnlimm n, [r1] -> fprintf pp "(~%a) &l %Ld" reg r1 (camlint64_of_coqint n)
+ | Oornl, [r1;r2] -> fprintf pp "(~%a) |l %a" reg r1 reg r2
+ | Oornlimm n, [r1;r2] -> fprintf pp "(~%a) |l %Ld" reg r1 (camlint64_of_coqint n)
+ | Oshll, [r1;r2] -> fprintf pp "%a <<l %a" reg r1 reg r2
+ | Oshllimm n, [r1] -> fprintf pp "%a <<l %Ld" reg r1 (camlint64_of_coqint n)
+ | Oshrl, [r1;r2] -> fprintf pp "%a >>ls %a" reg r1 reg r2
+ | Oshrlimm n, [r1] -> fprintf pp "%a >>ls %ld" reg r1 (camlint_of_coqint n)
+ | Oshrlu, [r1;r2] -> fprintf pp "%a >>lu %a" reg r1 reg r2
+ | Oshrluimm n, [r1] -> fprintf pp "%a >>lu %ld" reg r1 (camlint_of_coqint n)
+ | Oshrxlimm n, [r1] -> fprintf pp "%a >>lx %ld" reg r1 (camlint_of_coqint n)
+ | Omaddl, [r1; r2; r3] -> fprintf pp "%a +l %a *l %a" reg r1 reg r2 reg r3
+ | Omaddlimm imm, [r1; r2] -> fprintf pp "%a +l %a *l %Ld" reg r1 reg r2 (camlint64_of_coqint imm)
+ | Omsubl, [r1; r2; r3] -> fprintf pp "%a -l %a *l %a" reg r1 reg r2 reg r3
+
+ | Onegf, [r1] -> fprintf pp "negf(%a)" reg r1
+ | Oabsf, [r1] -> fprintf pp "absf(%a)" reg r1
+ | Oaddf, [r1;r2] -> fprintf pp "%a +f %a" reg r1 reg r2
+ | Osubf, [r1;r2] -> fprintf pp "%a -f %a" reg r1 reg r2
+ | Omulf, [r1;r2] -> fprintf pp "%a *f %a" reg r1 reg r2
+ | Odivf, [r1;r2] -> fprintf pp "%a /f %a" reg r1 reg r2
+ | Onegfs, [r1] -> fprintf pp "negfs(%a)" reg r1
+ | Oabsfs, [r1] -> fprintf pp "absfs(%a)" reg r1
+ | Oaddfs, [r1;r2] -> fprintf pp "%a +fs %a" reg r1 reg r2
+ | Osubfs, [r1;r2] -> fprintf pp "%a -fs %a" reg r1 reg r2
+ | Omulfs, [r1;r2] -> fprintf pp "%a *fs %a" reg r1 reg r2
+ | Odivfs, [r1;r2] -> fprintf pp "%a /fs %a" reg r1 reg r2
+ | Osingleoffloat, [r1] -> fprintf pp "singleoffloat(%a)" reg r1
+ | Ofloatofsingle, [r1] -> fprintf pp "floatofsingle(%a)" reg r1
+ | Ointoffloat, [r1] -> fprintf pp "intoffloat(%a)" reg r1
+ | Ointuoffloat, [r1] -> fprintf pp "intuoffloat(%a)" reg r1
+ | Ointofsingle, [r1] -> fprintf pp "intofsingle(%a)" reg r1
+ | Ointuofsingle, [r1] -> fprintf pp "intuofsingle(%a)" reg r1
+ | Osingleofint, [r1] -> fprintf pp "singleofint(%a)" reg r1
+ | Osingleofintu, [r1] -> fprintf pp "singleofintu(%a)" reg r1
+ | Olongoffloat, [r1] -> fprintf pp "longoffloat(%a)" reg r1
+ | Olonguoffloat, [r1] -> fprintf pp "longuoffloat(%a)" reg r1
+ | Ofloatoflong, [r1] -> fprintf pp "floatoflong(%a)" reg r1
+ | Ofloatoflongu, [r1] -> fprintf pp "floatoflongu(%a)" reg r1
+ | Olongofsingle, [r1] -> fprintf pp "longofsingle(%a)" reg r1
+ | Olonguofsingle, [r1] -> fprintf pp "longuofsingle(%a)" reg r1
+ | Osingleoflong, [r1] -> fprintf pp "singleoflong(%a)" reg r1
+ | Osingleoflongu, [r1] -> fprintf pp "singleoflongu(%a)" reg r1
+ | Ocmp c, args -> print_condition reg pp (c, args)
+
+ | Oextfz(stop, start), [r1] -> fprintf pp "extfz(%ld, %ld, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1
+ | Oextfs(stop, start), [r1] -> fprintf pp "extfs(%ld, %ld, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1
+ | Oextfzl(stop, start), [r1] -> fprintf pp "extfzl(%ld, %ld, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1
+ | Oextfsl(stop, start), [r1] -> fprintf pp "extfsl(%ld, %ld, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1
+ | Oinsf(stop, start), [r1; r2] -> fprintf pp "insf(%ld, %ld, %a, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1 reg r2
+ | Oinsfl(stop, start), [r1; r2] -> fprintf pp "insfl(%ld, %ld, %a, %a)" (camlint_of_coqint stop) (camlint_of_coqint start) reg r1 reg r2
+ | Osel(cond0, ty), [r1; r2; rc] ->
+ print_condition0 reg pp cond0 rc;
+ fprintf pp " ? %a : %a" reg r1 reg r2
+ | Oselimm(cond0, imm), [r1; rc] ->
+ print_condition0 reg pp cond0 rc;
+ fprintf pp " ? %a : %ld" reg r1 (camlint_of_coqint imm)
+ | Osellimm(cond0, imm), [r1; rc] ->
+ print_condition0 reg pp cond0 rc;
+ fprintf pp " ? %a :l %Ld" reg r1 (camlint64_of_coqint imm)
+ | _, _ -> fprintf pp "<bad operator>"
+
+let print_addressing reg pp = function
+ | Aindexed2XS scale, [r1;r2] -> fprintf pp "%a + (%a << %ld)" reg r1 reg r2 (camlint_of_coqint scale)
+ | Aindexed2, [r1;r2] -> fprintf pp "%a + %a" reg r1 reg r2
+ | Aindexed n, [r1] -> fprintf pp "%a + %Ld" reg r1 (camlint64_of_ptrofs n)
+ | Aglobal(id, ofs), [] ->
+ fprintf pp "\"%s\" + %Ld" (extern_atom id) (camlint64_of_ptrofs ofs)
+ | Ainstack ofs, [] -> fprintf pp "stack(%Ld)" (camlint64_of_ptrofs ofs)
+ | _ -> fprintf pp "<bad addressing>"
diff --git a/kvx/SelectLong.vp b/kvx/SelectLong.vp
new file mode 100644
index 00000000..b3638eca
--- /dev/null
+++ b/kvx/SelectLong.vp
@@ -0,0 +1,463 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Instruction selection for 64-bit integer operations *)
+
+Require Import Coqlib.
+Require Import Compopts.
+Require Import AST Integers Floats.
+Require Import Op CminorSel.
+Require Import OpHelpers.
+Require Import SelectOp SplitLong.
+Require Import ExtValues.
+Require Import DecBoolOps.
+
+Local Open Scope cminorsel_scope.
+Local Open Scope string_scope.
+
+Section SELECT.
+
+Context {hf: helper_functions}.
+
+Definition longconst (n: int64) : expr :=
+ if Archi.splitlong then SplitLong.longconst n else Eop (Olongconst n) Enil.
+
+Definition is_longconst (e: expr) :=
+ if Archi.splitlong then SplitLong.is_longconst e else
+ match e with
+ | Eop (Olongconst n) Enil => Some n
+ | _ => None
+ end.
+
+Definition intoflong (e: expr) :=
+ if Archi.splitlong then SplitLong.intoflong e else
+ match is_longconst e with
+ | Some n => Eop (Ointconst (Int.repr (Int64.unsigned n))) Enil
+ | None => Eop Olowlong (e ::: Enil)
+ end.
+
+Definition longofint (e: expr) :=
+ if Archi.splitlong then SplitLong.longofint e else
+ match is_intconst e with
+ | Some n => longconst (Int64.repr (Int.signed n))
+ | None => Eop Ocast32signed (e ::: Enil)
+ end.
+
+Definition longofintu (e: expr) :=
+ if Archi.splitlong then SplitLong.longofintu e else
+ match is_intconst e with
+ | Some n => longconst (Int64.repr (Int.unsigned n))
+ | None => Eop Ocast32unsigned (e ::: Enil)
+ end.
+
+(** ** Integer addition and pointer addition *)
+
+Definition addlimm_shllimm sh k2 e1 :=
+ if Compopts.optim_addx tt
+ then
+ match shift1_4_of_z (Int.unsigned sh) with
+ | Some s14 => Eop (Oaddxlimm s14 k2) (e1:::Enil)
+ | None => Eop (Oaddlimm k2) ((Eop (Oshllimm sh) (e1:::Enil)):::Enil)
+ end
+ else Eop (Oaddlimm k2) ((Eop (Oshllimm sh) (e1:::Enil)):::Enil).
+
+Nondetfunction addlimm (n: int64) (e: expr) :=
+ if Int64.eq n Int64.zero then e else
+ match e with
+ | Eop (Olongconst m) Enil => longconst (Int64.add n m)
+ | Eop (Oaddrsymbol s m) Enil =>
+ (if Compopts.optim_globaladdroffset tt
+ then Eop (Oaddrsymbol s (Ptrofs.add (Ptrofs.of_int64 n) m)) Enil
+ else Eop (Oaddlimm n) (e ::: Enil))
+ | Eop (Oaddrstack m) Enil => Eop (Oaddrstack (Ptrofs.add (Ptrofs.of_int64 n) m)) Enil
+ | Eop (Oaddlimm m) (t ::: Enil) => Eop (Oaddlimm(Int64.add n m)) (t ::: Enil)
+ | Eop (Oaddxlimm sh m) (t ::: Enil) => Eop (Oaddxlimm sh (Int64.add n m)) (t ::: Enil)
+ | Eop (Oshllimm sh) (t1:::Enil) => addlimm_shllimm sh n t1
+ | _ => Eop (Oaddlimm n) (e ::: Enil)
+ end.
+
+Definition addl_shllimm n e1 e2 :=
+ if Compopts.optim_addx tt
+ then
+ match shift1_4_of_z (Int.unsigned n) with
+ | Some s14 => Eop (Oaddxl s14) (e1:::e2:::Enil)
+ | None => Eop Oaddl (e2:::(Eop (Oshllimm n) (e1:::Enil)):::Enil)
+ end
+ else Eop Oaddl (e2:::(Eop (Oshllimm n) (e1:::Enil)):::Enil).
+
+Nondetfunction addl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.addl e1 e2 else
+ match e1, e2 with
+ | Eop (Olongconst n1) Enil, t2 => addlimm n1 t2
+ | t1, Eop (Olongconst n2) Enil => addlimm n2 t1
+ | Eop (Oaddlimm n1) (t1:::Enil), Eop (Oaddlimm n2) (t2:::Enil) =>
+ addlimm (Int64.add n1 n2) (Eop Oaddl (t1:::t2:::Enil))
+ | Eop (Oaddlimm n1) (t1:::Enil), Eop (Oaddrstack n2) Enil =>
+ Eop Oaddl (Eop (Oaddrstack (Ptrofs.add (Ptrofs.of_int64 n1) n2)) Enil ::: t1 ::: Enil)
+ | Eop (Oaddrstack n1) Enil, Eop (Oaddlimm n2) (t2:::Enil) =>
+ Eop Oaddl (Eop (Oaddrstack (Ptrofs.add n1 (Ptrofs.of_int64 n2))) Enil ::: t2 ::: Enil)
+ | Eop (Oaddlimm n1) (t1:::Enil), t2 =>
+ addlimm n1 (Eop Oaddl (t1:::t2:::Enil))
+ | t1, Eop (Oaddlimm n2) (t2:::Enil) =>
+ addlimm n2 (Eop Oaddl (t1:::t2:::Enil))
+ | t1, (Eop Omull (t2:::t3:::Enil)) =>
+ Eop Omaddl (t1:::t2:::t3:::Enil)
+ | (Eop Omull (t2:::t3:::Enil)), t1 =>
+ Eop Omaddl (t1:::t2:::t3:::Enil)
+ | t1, (Eop (Omullimm n) (t2:::Enil)) =>
+ Eop (Omaddlimm n) (t1:::t2:::Enil)
+ | (Eop (Omullimm n) (t2:::Enil)), t1 =>
+ Eop (Omaddlimm n) (t1:::t2:::Enil)
+ | (Eop (Oshllimm n) (t1:::Enil)), t2 =>
+ addl_shllimm n t1 t2
+ | t2, (Eop (Oshllimm n) (t1:::Enil)) =>
+ addl_shllimm n t1 t2
+ | _, _ => Eop Oaddl (e1:::e2:::Enil)
+ end.
+
+(** ** Integer and pointer subtraction *)
+
+Nondetfunction subl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.subl e1 e2 else
+ match e1, e2 with
+ | t1, Eop (Olongconst n2) Enil =>
+ addlimm (Int64.neg n2) t1
+ | Eop (Oaddlimm n1) (t1:::Enil), Eop (Oaddlimm n2) (t2:::Enil) =>
+ addlimm (Int64.sub n1 n2) (Eop Osubl (t1:::t2:::Enil))
+ | Eop (Oaddlimm n1) (t1:::Enil), t2 =>
+ addlimm n1 (Eop Osubl (t1:::t2:::Enil))
+ | t1, Eop (Oaddlimm n2) (t2:::Enil) =>
+ addlimm (Int64.neg n2) (Eop Osubl (t1:::t2:::Enil))
+ | t1, (Eop Omull (t2:::t3:::Enil)) =>
+ Eop Omsubl (t1:::t2:::t3:::Enil)
+ | t1, (Eop (Omullimm n) (t2:::Enil)) =>
+ Eop (Omaddlimm (Int64.neg n)) (t1:::t2:::Enil)
+ | _, _ => Eop Osubl (e1:::e2:::Enil)
+ end.
+
+Definition negl (e: expr) :=
+ if Archi.splitlong then SplitLong.negl e else
+ match is_longconst e with
+ | Some n => longconst (Int64.neg n)
+ | None => Eop Onegl (e ::: Enil)
+ end.
+
+(** ** Immediate shifts *)
+
+Nondetfunction shllimm (e1: expr) (n: int) :=
+ if Archi.splitlong then SplitLong.shllimm e1 n else
+ if Int.eq n Int.zero then
+ e1
+ else if negb (Int.ltu n Int64.iwordsize') then
+ Eop Oshll (e1 ::: Eop (Ointconst n) Enil ::: Enil)
+ else match e1 with
+ | Eop (Olongconst n1) Enil =>
+ longconst (Int64.shl' n1 n)
+ | Eop (Oshllimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int64.iwordsize'
+ then Eop (Oshllimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshllimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshllimm n) (e1:::Enil)
+ end.
+
+Nondetfunction shrluimm (e1: expr) (n: int) :=
+ if Archi.splitlong then SplitLong.shrluimm e1 n else
+ if Int.eq n Int.zero then e1 else
+ if negb (Int.ltu n Int64.iwordsize') then
+ Eop Oshrlu (e1:::Eop (Ointconst n) Enil:::Enil)
+ else
+ match e1 with
+ | Eop (Olongconst n1) Enil =>
+ longconst (Int64.shru' n1 n)
+ | Eop (Oshrluimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int64.iwordsize'
+ then Eop (Oshrluimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshrluimm n) (e1:::Enil)
+ | Eop (Oshllimm n1) (t1:::Enil) =>
+ let stop := Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one) in
+ let start := Z.sub (Z.add (Z.add (Int.unsigned n) stop) Z.one) Int64.zwordsize in
+ if is_bitfieldl stop start
+ then Eop (Oextfzl stop start) (t1:::Enil)
+ else Eop (Oshrluimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshrluimm n) (e1:::Enil)
+ end.
+
+Nondetfunction shrlimm (e1: expr) (n: int) :=
+ if Archi.splitlong then SplitLong.shrlimm e1 n else
+ if Int.eq n Int.zero then e1 else
+ if negb (Int.ltu n Int64.iwordsize') then
+ Eop Oshrl (e1:::Eop (Ointconst n) Enil:::Enil)
+ else
+ match e1 with
+ | Eop (Olongconst n1) Enil =>
+ longconst (Int64.shr' n1 n)
+ | Eop (Oshrlimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int64.iwordsize'
+ then Eop (Oshrlimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshrlimm n) (e1:::Enil)
+ | Eop (Oshllimm n1) (t1:::Enil) =>
+ let stop := Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one) in
+ let start := Z.sub (Z.add (Z.add (Int.unsigned n) stop) Z.one) Int64.zwordsize in
+ if is_bitfieldl stop start
+ then Eop (Oextfsl stop start) (t1:::Enil)
+ else Eop (Oshrlimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshrlimm n) (e1:::Enil)
+ end.
+
+(** ** General shifts *)
+
+Definition shll (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.shll e1 e2 else
+ match is_intconst e2 with
+ | Some n2 => shllimm e1 n2
+ | None => Eop Oshll (e1:::e2:::Enil)
+ end.
+
+Definition shrl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.shrl e1 e2 else
+ match is_intconst e2 with
+ | Some n2 => shrlimm e1 n2
+ | None => Eop Oshrl (e1:::e2:::Enil)
+ end.
+
+Definition shrlu (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.shrlu e1 e2 else
+ match is_intconst e2 with
+ | Some n2 => shrluimm e1 n2
+ | _ => Eop Oshrlu (e1:::e2:::Enil)
+ end.
+
+(** ** Integer multiply *)
+
+Definition mullimm_base (n1: int64) (e2: expr) :=
+ match Int64.one_bits' n1 with
+ | i :: nil =>
+ shllimm e2 i
+ | i :: j :: nil =>
+ Elet e2 (addl (shllimm (Eletvar 0) i) (shllimm (Eletvar 0) j))
+ | _ =>
+ Eop (Omullimm n1) (e2 ::: Enil)
+ end.
+
+Nondetfunction mullimm (n1: int64) (e2: expr) :=
+ if Archi.splitlong then SplitLong.mullimm n1 e2
+ else if Int64.eq n1 Int64.zero then longconst Int64.zero
+ else if Int64.eq n1 Int64.one then e2
+ else match e2 with
+ | Eop (Olongconst n2) Enil => longconst (Int64.mul n1 n2)
+ | Eop (Oaddlimm n2) (t2:::Enil) => addlimm (Int64.mul n1 n2) (mullimm_base n1 t2)
+ | _ => mullimm_base n1 e2
+ end.
+
+Nondetfunction mull (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.mull e1 e2 else
+ match e1, e2 with
+ | Eop (Olongconst n1) Enil, t2 => mullimm n1 t2
+ | t1, Eop (Olongconst n2) Enil => mullimm n2 t1
+ | _, _ => Eop Omull (e1:::e2:::Enil)
+ end.
+
+Definition mullhu (e1: expr) (n2: int64) :=
+ if Archi.splitlong then SplitLong.mullhu e1 n2 else
+ Eop Omullhu (e1 ::: longconst n2 ::: Enil).
+
+Definition mullhs (e1: expr) (n2: int64) :=
+ if Archi.splitlong then SplitLong.mullhs e1 n2 else
+ Eop Omullhs (e1 ::: longconst n2 ::: Enil).
+
+(** ** Bitwise and, or, xor *)
+
+Nondetfunction andlimm (n1: int64) (e2: expr) :=
+ if Int64.eq n1 Int64.zero then longconst Int64.zero else
+ if Int64.eq n1 Int64.mone then e2 else
+ match e2 with
+ | Eop (Olongconst n2) Enil =>
+ longconst (Int64.and n1 n2)
+ | Eop (Oandlimm n2) (t2:::Enil) =>
+ Eop (Oandlimm (Int64.and n1 n2)) (t2:::Enil)
+ | Eop Onotl (t2:::Enil) => Eop (Oandnlimm n1) (t2:::Enil)
+ | _ =>
+ Eop (Oandlimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction andl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.andl e1 e2 else
+ match e1, e2 with
+ | Eop (Olongconst n1) Enil, t2 => andlimm n1 t2
+ | t1, Eop (Olongconst n2) Enil => andlimm n2 t1
+ | (Eop Onotl (t1:::Enil)), t2 => Eop Oandnl (t1:::t2:::Enil)
+ | t1, (Eop Onotl (t2:::Enil)) => Eop Oandnl (t2:::t1:::Enil)
+ | _, _ => Eop Oandl (e1:::e2:::Enil)
+ end.
+
+Nondetfunction orlimm (n1: int64) (e2: expr) :=
+ if Int64.eq n1 Int64.zero then e2 else
+ if Int64.eq n1 Int64.mone then longconst Int64.mone else
+ match e2 with
+ | Eop (Olongconst n2) Enil => longconst (Int64.or n1 n2)
+ | Eop (Oorlimm n2) (t2:::Enil) => Eop (Oorlimm (Int64.or n1 n2)) (t2:::Enil)
+ | Eop Onotl (t2:::Enil) => Eop (Oornlimm n1) (t2:::Enil)
+ | _ => Eop (Oorlimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction orl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.orl e1 e2 else
+ match e1, e2 with
+ | Eop (Olongconst n1) Enil, t2 => orlimm n1 t2
+ | t1, Eop (Olongconst n2) Enil => orlimm n2 t1
+ | (Eop Onotl (t1:::Enil)), t2 => Eop Oornl (t1:::t2:::Enil)
+ | t1, (Eop Onotl (t2:::Enil)) => Eop Oornl (t2:::t1:::Enil)
+ | (Eop (Oandlimm nmask) (prev:::Enil)),
+ (Eop (Oandlimm mask)
+ ((Eop (Oshllimm start) (fld:::Enil)):::Enil)) =>
+ let zstart := Int.unsigned start in
+ let zstop := int64_highest_bit mask in
+ if is_bitfieldl zstop zstart
+ then
+ let mask' := Int64.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int64.eq_dec mask mask')
+ (Int64.eq_dec nmask (Int64.not mask'))
+ then Eop (Oinsfl zstop zstart) (prev:::fld:::Enil)
+ else Eop Oorl (e1:::e2:::Enil)
+ else Eop Oorl (e1:::e2:::Enil)
+ | (Eop (Oandlimm nmask) (prev:::Enil)),
+ (Eop (Oandlimm mask) (fld:::Enil)) =>
+ let zstart := 0 in
+ let zstop := int64_highest_bit mask in
+ if is_bitfieldl zstop zstart
+ then
+ let mask' := Int64.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int64.eq_dec mask mask')
+ (Int64.eq_dec nmask (Int64.not mask'))
+ then Eop (Oinsfl zstop zstart) (prev:::fld:::Enil)
+ else Eop Oorl (e1:::e2:::Enil)
+ else Eop Oorl (e1:::e2:::Enil)
+ | _, _ => Eop Oorl (e1:::e2:::Enil)
+ end.
+
+Nondetfunction xorlimm (n1: int64) (e2: expr) :=
+ if Int64.eq n1 Int64.zero then e2 else
+ if Int64.eq n1 Int64.mone
+ then Eop Onotl (e2:::Enil)
+ else
+ match e2 with
+ | Eop (Olongconst n2) Enil => longconst (Int64.xor n1 n2)
+ | Eop (Oxorlimm n2) (t2:::Enil) =>
+ let n := Int64.xor n1 n2 in
+ if Int64.eq n Int64.zero then t2 else Eop (Oxorlimm n) (t2:::Enil)
+ | _ => Eop (Oxorlimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction xorl (e1: expr) (e2: expr) :=
+ if Archi.splitlong then SplitLong.xorl e1 e2 else
+ match e1, e2 with
+ | Eop (Olongconst n1) Enil, t2 => xorlimm n1 t2
+ | t1, Eop (Olongconst n2) Enil => xorlimm n2 t1
+ | _, _ => Eop Oxorl (e1:::e2:::Enil)
+ end.
+
+(** ** Integer logical negation *)
+
+Nondetfunction notl (e: expr) :=
+ match e with
+ | Eop Oandl (e1:::e2:::Enil) => Eop Onandl (e1:::e2:::Enil)
+ | Eop (Oandlimm n) (e1:::Enil) => Eop (Onandlimm n) (e1:::Enil)
+ | Eop Oorl (e1:::e2:::Enil) => Eop Onorl (e1:::e2:::Enil)
+ | Eop (Oorlimm n) (e1:::Enil) => Eop (Onorlimm n) (e1:::Enil)
+ | Eop Oxorl (e1:::e2:::Enil) => Eop Onxorl (e1:::e2:::Enil)
+ | Eop (Oxorlimm n) (e1:::Enil) => Eop (Onxorlimm n) (e1:::Enil)
+ | Eop Onandl (e1:::e2:::Enil) => Eop Oandl (e1:::e2:::Enil)
+ | Eop (Onandlimm n) (e1:::Enil) => Eop (Oandlimm n) (e1:::Enil)
+ | Eop Onorl (e1:::e2:::Enil) => Eop Oorl (e1:::e2:::Enil)
+ | Eop (Onorlimm n) (e1:::Enil) => Eop (Oorlimm n) (e1:::Enil)
+ | Eop Onxorl (e1:::e2:::Enil) => Eop Oxorl (e1:::e2:::Enil)
+ | Eop (Onxorlimm n) (e1:::Enil) => Eop (Oxorlimm n) (e1:::Enil)
+ | Eop Oandnl (e1:::e2:::Enil) => Eop Oornl (e2:::e1:::Enil)
+ | Eop (Oandnlimm n) (e1:::Enil) => Eop (Oorlimm (Int64.not n)) (e1:::Enil)
+ | Eop Oornl (e1:::e2:::Enil) => Eop Oandnl (e2:::e1:::Enil)
+ | Eop (Oornlimm n) (e1:::Enil) => Eop (Oandlimm (Int64.not n)) (e1:::Enil)
+ | Eop Onotl (e1:::Enil) => e1
+ | Eop (Olongconst k) Enil => Eop (Olongconst (Int64.not k)) Enil
+ | _ => Eop Onotl (e:::Enil)
+ end.
+(* old: if Archi.splitlong then SplitLong.notl e else xorlimm Int64.mone e. *)
+
+(** ** Integer division and modulus *)
+
+Definition divlu_base (e1: expr) (e2: expr) := SplitLong.divlu_base e1 e2.
+Definition modlu_base (e1: expr) (e2: expr) := SplitLong.modlu_base e1 e2.
+Definition divls_base (e1: expr) (e2: expr) := SplitLong.divls_base e1 e2.
+Definition modls_base (e1: expr) (e2: expr) := SplitLong.modls_base e1 e2.
+
+Definition shrxlimm (e: expr) (n: int) :=
+ if Archi.splitlong then SplitLong.shrxlimm e n else
+ if Int.eq n Int.zero then e else Eop (Oshrxlimm n) (e ::: Enil).
+
+(** ** Comparisons *)
+
+Definition cmplu (c: comparison) (e1 e2: expr) :=
+ if Archi.splitlong then SplitLong.cmplu c e1 e2 else
+ match is_longconst e1, is_longconst e2 with
+ | Some n1, Some n2 =>
+ Eop (Ointconst (if Int64.cmpu c n1 n2 then Int.one else Int.zero)) Enil
+ | Some n1, None => Eop (Ocmp (Ccompluimm (swap_comparison c) n1)) (e2:::Enil)
+ | None, Some n2 => Eop (Ocmp (Ccompluimm c n2)) (e1:::Enil)
+ | None, None => Eop (Ocmp (Ccomplu c)) (e1:::e2:::Enil)
+ end.
+
+Definition cmpl (c: comparison) (e1 e2: expr) :=
+ if Archi.splitlong then SplitLong.cmpl c e1 e2 else
+ match is_longconst e1, is_longconst e2 with
+ | Some n1, Some n2 =>
+ Eop (Ointconst (if Int64.cmp c n1 n2 then Int.one else Int.zero)) Enil
+ | Some n1, None => Eop (Ocmp (Ccomplimm (swap_comparison c) n1)) (e2:::Enil)
+ | None, Some n2 => Eop (Ocmp (Ccomplimm c n2)) (e1:::Enil)
+ | None, None => Eop (Ocmp (Ccompl c)) (e1:::e2:::Enil)
+ end.
+
+(** ** Floating-point conversions *)
+
+Definition longoffloat (e: expr) :=
+ if Archi.splitlong then SplitLong.longoffloat e else
+ Eop Olongoffloat (e:::Enil).
+
+Definition longuoffloat (e: expr) :=
+ if Archi.splitlong then SplitLong.longuoffloat e else
+ Eop Olonguoffloat (e:::Enil).
+
+Definition floatoflong (e: expr) :=
+ if Archi.splitlong then SplitLong.floatoflong e else
+ Eop Ofloatoflong (e:::Enil).
+
+Definition floatoflongu (e: expr) :=
+ if Archi.splitlong then SplitLong.floatoflongu e else
+ Eop Ofloatoflongu (e:::Enil).
+
+Definition longofsingle (e: expr) := longoffloat (floatofsingle e).
+
+Definition longuofsingle (e: expr) := longuoffloat (floatofsingle e).
+
+Definition singleoflong (e: expr) := SplitLong.singleoflong e.
+
+Definition singleoflongu (e: expr) := SplitLong.singleoflongu e.
+
+End SELECT.
+
+(* Local Variables: *)
+(* mode: coq *)
+(* End: *)
diff --git a/kvx/SelectLongproof.v b/kvx/SelectLongproof.v
new file mode 100644
index 00000000..c3abdbc7
--- /dev/null
+++ b/kvx/SelectLongproof.v
@@ -0,0 +1,951 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Correctness of instruction selection for 64-bit integer operations *)
+
+Require Import String Coqlib Maps Integers Floats Errors.
+Require Archi.
+Require Import AST Values ExtValues Memory Globalenvs Events.
+Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
+Require Import SelectOp SelectOpproof SplitLong SplitLongproof.
+Require Import SelectLong.
+Require Import DecBoolOps.
+Require Import Lia.
+
+Local Open Scope cminorsel_scope.
+Local Open Scope string_scope.
+
+(** * Correctness of the instruction selection functions for 64-bit operators *)
+
+Section CMCONSTR.
+
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
+Variable sp: val.
+Variable e: env.
+Variable m: mem.
+
+Definition unary_constructor_sound (cstr: expr -> expr) (sem: val -> val) : Prop :=
+ forall le a x,
+ eval_expr ge sp e m le a x ->
+ exists v, eval_expr ge sp e m le (cstr a) v /\ Val.lessdef (sem x) v.
+
+Definition binary_constructor_sound (cstr: expr -> expr -> expr) (sem: val -> val -> val) : Prop :=
+ forall le a x b y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (cstr a b) v /\ Val.lessdef (sem x y) v.
+
+Definition partial_unary_constructor_sound (cstr: expr -> expr) (sem: val -> option val) : Prop :=
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ sem x = Some y ->
+ exists v, eval_expr ge sp e m le (cstr a) v /\ Val.lessdef y v.
+
+Definition partial_binary_constructor_sound (cstr: expr -> expr -> expr) (sem: val -> val -> option val) : Prop :=
+ forall le a x b y z,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ sem x y = Some z ->
+ exists v, eval_expr ge sp e m le (cstr a b) v /\ Val.lessdef z v.
+
+Theorem eval_longconst:
+ forall le n, eval_expr ge sp e m le (longconst n) (Vlong n).
+Proof.
+ unfold longconst; intros; destruct Archi.splitlong.
+ apply SplitLongproof.eval_longconst.
+ EvalOp.
+Qed.
+
+Lemma is_longconst_sound:
+ forall v a n le,
+ is_longconst a = Some n -> eval_expr ge sp e m le a v -> v = Vlong n.
+Proof with (try discriminate).
+ intros. unfold is_longconst in *. destruct Archi.splitlong.
+ eapply SplitLongproof.is_longconst_sound; eauto.
+ assert (a = Eop (Olongconst n) Enil).
+ { destruct a... destruct o... destruct e0... congruence. }
+ subst a. InvEval. auto.
+Qed.
+
+Theorem eval_intoflong: unary_constructor_sound intoflong Val.loword.
+Proof.
+ unfold intoflong; destruct Archi.splitlong. apply SplitLongproof.eval_intoflong.
+ red; intros. destruct (is_longconst a) as [n|] eqn:C.
+- TrivialExists. simpl. erewrite (is_longconst_sound x) by eauto. auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_longofintu: unary_constructor_sound longofintu Val.longofintu.
+Proof.
+ unfold longofintu; destruct Archi.splitlong. apply SplitLongproof.eval_longofintu.
+ red; intros. destruct (is_intconst a) as [n|] eqn:C.
+- econstructor; split. apply eval_longconst.
+ exploit is_intconst_sound; eauto. intros; subst x. auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_longofint: unary_constructor_sound longofint Val.longofint.
+Proof.
+ unfold longofint; destruct Archi.splitlong. apply SplitLongproof.eval_longofint.
+ red; intros. destruct (is_intconst a) as [n|] eqn:C.
+- econstructor; split. apply eval_longconst.
+ exploit is_intconst_sound; eauto. intros; subst x. auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_negl: unary_constructor_sound negl Val.negl.
+Proof.
+ unfold negl. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_negl; auto.
+ red; intros. destruct (is_longconst a) as [n|] eqn:C.
+- exploit is_longconst_sound; eauto. intros EQ; subst x.
+ econstructor; split. apply eval_longconst. auto.
+- TrivialExists.
+Qed.
+
+
+Theorem eval_addlimm_shllimm:
+ forall sh k2, unary_constructor_sound (addlimm_shllimm sh k2) (fun x => ExtValues.addxl sh x (Vlong k2)).
+Proof.
+ red; unfold addlimm_shllimm; intros.
+ destruct (Compopts.optim_addx tt).
+ {
+ destruct (shift1_4_of_z (Int.unsigned sh)) as [s14 |] eqn:SHIFT.
+ - TrivialExists. simpl.
+ f_equal.
+ unfold shift1_4_of_z, int_of_shift1_4, z_of_shift1_4 in *.
+ destruct (Z.eq_dec _ _) as [e1|].
+ { replace s14 with SHIFT1 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e1.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e1.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e2|].
+ { replace s14 with SHIFT2 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e2.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e2.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e3|].
+ { replace s14 with SHIFT3 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e3.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e3.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e4|].
+ { replace s14 with SHIFT4 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e4.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e4.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ discriminate.
+ - unfold addxl. rewrite Val.addl_commut.
+ TrivialExists.
+ repeat (try eassumption; try econstructor).
+ simpl.
+ reflexivity.
+ }
+ { unfold addxl. rewrite Val.addl_commut.
+ TrivialExists.
+ repeat (try eassumption; try econstructor).
+ simpl.
+ reflexivity.
+ }
+Qed.
+
+Theorem eval_addlimm: forall n, unary_constructor_sound (addlimm n) (fun v => Val.addl v (Vlong n)).
+Proof.
+ unfold addlimm; intros; red; intros.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ subst. exists x; split; auto.
+ destruct x; simpl; rewrite ?Int64.add_zero, ?Ptrofs.add_zero; auto.
+ destruct (addlimm_match a); InvEval.
+- econstructor; split. apply eval_longconst. rewrite Int64.add_commut; auto.
+- destruct (Compopts.optim_globaladdroffset _).
+ + econstructor; split. EvalOp. simpl; eauto.
+ unfold Genv.symbol_address. destruct (Genv.find_symbol ge s); simpl; auto.
+ destruct Archi.ptr64; auto. rewrite Ptrofs.add_commut; auto.
+ + TrivialExists. repeat econstructor. simpl. trivial.
+- econstructor; split. EvalOp. simpl; eauto.
+ destruct sp; simpl; auto. destruct Archi.ptr64; auto.
+ rewrite Ptrofs.add_assoc, (Ptrofs.add_commut m0). auto.
+- subst x. rewrite Val.addl_assoc. rewrite Int64.add_commut. TrivialExists.
+- TrivialExists; simpl. subst x.
+ destruct v1; simpl; trivial.
+ destruct (Int.ltu _ _); simpl; trivial.
+ rewrite Int64.add_assoc. rewrite Int64.add_commut.
+ reflexivity.
+- pose proof eval_addlimm_shllimm as ADDXL.
+ unfold unary_constructor_sound in ADDXL.
+ unfold addxl in ADDXL.
+ rewrite Val.addl_commut.
+ subst x.
+ apply ADDXL; assumption.
+- TrivialExists.
+Qed.
+
+Lemma eval_addxl: forall n, binary_constructor_sound (addl_shllimm n) (ExtValues.addxl n).
+Proof.
+ red.
+ intros.
+ unfold addl_shllimm.
+ destruct (Compopts.optim_addx tt).
+ {
+ destruct (shift1_4_of_z (Int.unsigned n)) as [s14 |] eqn:SHIFT.
+ - TrivialExists.
+ simpl.
+ f_equal. f_equal.
+ unfold shift1_4_of_z, int_of_shift1_4, z_of_shift1_4 in *.
+ destruct (Z.eq_dec _ _) as [e1|].
+ { replace s14 with SHIFT1 by congruence.
+ rewrite <- e1.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e2|].
+ { replace s14 with SHIFT2 by congruence.
+ rewrite <- e2.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e3|].
+ { replace s14 with SHIFT3 by congruence.
+ rewrite <- e3.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e4|].
+ { replace s14 with SHIFT4 by congruence.
+ rewrite <- e4.
+ apply Int.repr_unsigned. }
+ discriminate.
+ (* Oaddxl *)
+ - TrivialExists;
+ repeat econstructor; eassumption.
+ }
+ { TrivialExists;
+ repeat econstructor; eassumption.
+ }
+Qed.
+
+Theorem eval_addl: binary_constructor_sound addl Val.addl.
+Proof.
+ unfold addl. destruct Archi.splitlong eqn:SL.
+ apply SplitLongproof.eval_addl. apply Archi.splitlong_ptr32; auto.
+(*
+ assert (SF: Archi.ptr64 = true).
+ { Local Transparent Archi.splitlong. unfold Archi.splitlong in SL.
+ destruct Archi.ptr64; simpl in *; congruence. }
+*)
+(*
+ assert (B: forall id ofs n,
+ Genv.symbol_address ge id (Ptrofs.add ofs (Ptrofs.repr n)) =
+ Val.addl (Genv.symbol_address ge id ofs) (Vlong (Int64.repr n))).
+ { intros. replace (Ptrofs.repr n) with (Ptrofs.of_int64 (Int64.repr n)) by auto with ptrofs.
+ apply Genv.shift_symbol_address_64; auto. }
+
+*)
+ red; intros until y.
+ case (addl_match a b); intros; InvEval.
+ - rewrite Val.addl_commut. apply eval_addlimm; auto.
+ - apply eval_addlimm; auto.
+ - subst.
+ replace (Val.addl (Val.addl v1 (Vlong n1)) (Val.addl v0 (Vlong n2)))
+ with (Val.addl (Val.addl v1 v0) (Val.addl (Vlong n1) (Vlong n2))).
+ apply eval_addlimm. EvalOp.
+ repeat rewrite Val.addl_assoc. decEq. apply Val.addl_permut.
+ - subst. econstructor; split.
+ EvalOp. constructor. EvalOp. simpl; eauto. constructor. eauto. constructor. simpl; eauto.
+ rewrite Val.addl_commut. destruct sp; simpl; auto.
+ destruct v1; simpl; auto.
+ destruct Archi.ptr64 eqn:SF; auto.
+ apply Val.lessdef_same. f_equal. rewrite ! Ptrofs.add_assoc. f_equal.
+ rewrite (Ptrofs.add_commut (Ptrofs.of_int64 n1)), Ptrofs.add_assoc. f_equal. auto with ptrofs.
+ - subst. econstructor; split.
+ EvalOp. constructor. EvalOp. simpl; eauto. constructor. eauto. constructor. simpl; eauto.
+ destruct sp; simpl; auto.
+ destruct v1; simpl; auto.
+ destruct Archi.ptr64 eqn:SF; auto.
+ apply Val.lessdef_same. f_equal. rewrite ! Ptrofs.add_assoc. f_equal. f_equal.
+ rewrite Ptrofs.add_commut. auto with ptrofs.
+ - subst.
+ replace (Val.addl (Val.addl v1 (Vlong n1)) y)
+ with (Val.addl (Val.addl v1 y) (Vlong n1)).
+ apply eval_addlimm. EvalOp.
+ repeat rewrite Val.addl_assoc. decEq. apply Val.addl_commut.
+ - subst.
+ replace (Val.addl x (Val.addl v1 (Vlong n2)))
+ with (Val.addl (Val.addl x v1) (Vlong n2)).
+ apply eval_addlimm. EvalOp.
+ repeat rewrite Val.addl_assoc. reflexivity.
+ - subst. TrivialExists.
+ - subst. rewrite Val.addl_commut. TrivialExists.
+ - subst. TrivialExists.
+ - subst. rewrite Val.addl_commut. TrivialExists.
+ - subst. pose proof eval_addxl as ADDXL.
+ unfold binary_constructor_sound in ADDXL.
+ rewrite Val.addl_commut.
+ apply ADDXL; assumption.
+ (* Oaddxl *)
+ - subst. pose proof eval_addxl as ADDXL.
+ unfold binary_constructor_sound in ADDXL.
+ apply ADDXL; assumption.
+ - TrivialExists.
+Qed.
+
+Theorem eval_subl: binary_constructor_sound subl Val.subl.
+Proof.
+ unfold subl. destruct Archi.splitlong eqn:SL.
+ apply SplitLongproof.eval_subl. apply Archi.splitlong_ptr32; auto.
+ red; intros; destruct (subl_match a b); InvEval.
+- rewrite Val.subl_addl_opp. apply eval_addlimm; auto.
+- subst. rewrite Val.subl_addl_l. rewrite Val.subl_addl_r.
+ rewrite Val.addl_assoc. simpl. rewrite Int64.add_commut. rewrite <- Int64.sub_add_opp.
+ apply eval_addlimm; EvalOp.
+- subst. rewrite Val.subl_addl_l. apply eval_addlimm; EvalOp.
+- subst. rewrite Val.subl_addl_r.
+ apply eval_addlimm; EvalOp.
+- TrivialExists. simpl. subst. reflexivity.
+- TrivialExists. simpl. subst.
+ destruct v1; destruct x; simpl; trivial.
+ + f_equal. f_equal.
+ rewrite <- Int64.neg_mul_distr_r.
+ rewrite Int64.sub_add_opp.
+ reflexivity.
+ + destruct (Archi.ptr64) eqn:ARCHI64; simpl; trivial.
+ f_equal. f_equal.
+ rewrite <- Int64.neg_mul_distr_r.
+ rewrite Ptrofs.sub_add_opp.
+ unfold Ptrofs.add.
+ f_equal. f_equal.
+ rewrite (Ptrofs.agree64_neg ARCHI64 (Ptrofs.of_int64 (Int64.mul i n)) (Int64.mul i n)).
+ rewrite (Ptrofs.agree64_of_int ARCHI64 (Int64.neg (Int64.mul i n))).
+ reflexivity.
+ apply (Ptrofs.agree64_of_int ARCHI64).
+- TrivialExists.
+Qed.
+
+Theorem eval_shllimm: forall n, unary_constructor_sound (fun e => shllimm e n) (fun v => Val.shll v (Vint n)).
+Proof.
+ intros; unfold shllimm. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shllimm; auto.
+ red; intros.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ exists x; split; auto. subst n; destruct x; simpl; auto.
+ destruct (Int.ltu Int.zero Int64.iwordsize'); auto.
+ change (Int64.shl' i Int.zero) with (Int64.shl i Int64.zero). rewrite Int64.shl_zero; auto.
+ destruct (Int.ltu n Int64.iwordsize') eqn:LT; simpl.
+ assert (DEFAULT: exists v, eval_expr ge sp e m le (Eop (Oshllimm n) (a:::Enil)) v
+ /\ Val.lessdef (Val.shll x (Vint n)) v) by TrivialExists.
+ destruct (shllimm_match a); InvEval.
+- econstructor; split. apply eval_longconst. simpl; rewrite LT; auto.
+- destruct (Int.ltu (Int.add n n1) Int64.iwordsize') eqn:LT'; auto.
+ subst. econstructor; split. EvalOp. simpl; eauto.
+ destruct v1; simpl; auto. rewrite LT'.
+ destruct (Int.ltu n1 Int64.iwordsize') eqn:LT1; auto.
+ simpl; rewrite LT. rewrite Int.add_commut, Int64.shl'_shl'; auto. rewrite Int.add_commut; auto.
+- apply DEFAULT.
+- TrivialExists. constructor; eauto. constructor. EvalOp. simpl; eauto. constructor. auto.
+Qed.
+
+Theorem eval_shrluimm: forall n, unary_constructor_sound (fun e => shrluimm e n) (fun v => Val.shrlu v (Vint n)).
+Proof.
+ intros; unfold shrluimm. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shrluimm; auto.
+ red; intros.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ exists x; split; auto. subst n; destruct x; simpl; auto.
+ destruct (Int.ltu Int.zero Int64.iwordsize'); auto.
+ change (Int64.shru' i Int.zero) with (Int64.shru i Int64.zero). rewrite Int64.shru_zero; auto.
+ destruct (Int.ltu n Int64.iwordsize') eqn:LT.
+ assert (DEFAULT: exists v, eval_expr ge sp e m le (Eop (Oshrluimm n) (a:::Enil)) v
+ /\ Val.lessdef (Val.shrlu x (Vint n)) v) by TrivialExists.
+ destruct (shrluimm_match a); InvEval.
+- econstructor; split. apply eval_longconst. simpl; rewrite LT; auto.
+- destruct (Int.ltu (Int.add n n1) Int64.iwordsize') eqn:LT'; auto.
+ subst. econstructor; split. EvalOp. simpl; eauto.
+ destruct v1; simpl; auto. rewrite LT'.
+ destruct (Int.ltu n1 Int64.iwordsize') eqn:LT1; auto.
+ simpl; rewrite LT. rewrite Int.add_commut, Int64.shru'_shru'; auto. rewrite Int.add_commut; auto.
+- subst x.
+ simpl negb.
+ cbn iota.
+ destruct (is_bitfieldl _ _) eqn:BOUNDS.
+ + exists (extfzl (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one))
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int64.zwordsize) v1).
+ split.
+ ++ EvalOp.
+ ++ unfold extfzl.
+ rewrite BOUNDS.
+ destruct v1; try (simpl; apply Val.lessdef_undef).
+ replace (Z.sub Int64.zwordsize
+ (Z.add (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)) with (Int.unsigned n1) by lia.
+ replace (Z.sub Int64.zwordsize
+ (Z.sub
+ (Z.add (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int64.zwordsize))) with (Int.unsigned n) by lia.
+ simpl.
+ destruct (Int.ltu n1 Int64.iwordsize') eqn:Hltu_n1; simpl; trivial.
+ destruct (Int.ltu n Int64.iwordsize') eqn:Hltu_n; simpl; trivial.
+ rewrite Int.repr_unsigned.
+ rewrite Int.repr_unsigned.
+ constructor.
+ + TrivialExists. constructor. econstructor. constructor. eassumption. constructor. simpl. reflexivity. constructor. simpl. reflexivity.
+- apply DEFAULT.
+- TrivialExists. constructor; eauto. constructor. EvalOp. simpl; eauto. constructor. auto.
+Qed.
+
+Theorem eval_shrlimm: forall n, unary_constructor_sound (fun e => shrlimm e n) (fun v => Val.shrl v (Vint n)).
+Proof.
+ intros; unfold shrlimm. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shrlimm; auto.
+ red; intros.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ exists x; split; auto. subst n; destruct x; simpl; auto.
+ destruct (Int.ltu Int.zero Int64.iwordsize'); auto.
+ change (Int64.shr' i Int.zero) with (Int64.shr i Int64.zero). rewrite Int64.shr_zero; auto.
+ destruct (Int.ltu n Int64.iwordsize') eqn:LT.
+ assert (DEFAULT: exists v, eval_expr ge sp e m le (Eop (Oshrlimm n) (a:::Enil)) v
+ /\ Val.lessdef (Val.shrl x (Vint n)) v) by TrivialExists.
+ destruct (shrlimm_match a); InvEval.
+- econstructor; split. apply eval_longconst. simpl; rewrite LT; auto.
+- destruct (Int.ltu (Int.add n n1) Int64.iwordsize') eqn:LT'; auto.
+ subst. econstructor; split. EvalOp. simpl; eauto.
+ destruct v1; simpl; auto. rewrite LT'.
+ destruct (Int.ltu n1 Int64.iwordsize') eqn:LT1; auto.
+ simpl; rewrite LT. rewrite Int.add_commut, Int64.shr'_shr'; auto. rewrite Int.add_commut; auto.
+- subst x.
+ simpl negb.
+ cbn iota.
+ destruct (is_bitfieldl _ _) eqn:BOUNDS.
+ + exists (extfsl (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one))
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int64.zwordsize) v1).
+ split.
+ ++ EvalOp.
+ ++ unfold extfsl.
+ rewrite BOUNDS.
+ destruct v1; try (simpl; apply Val.lessdef_undef).
+ replace (Z.sub Int64.zwordsize
+ (Z.add (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)) with (Int.unsigned n1) by lia.
+ replace (Z.sub Int64.zwordsize
+ (Z.sub
+ (Z.add (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int64.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int64.zwordsize))) with (Int.unsigned n) by lia.
+ simpl.
+ destruct (Int.ltu n1 Int64.iwordsize') eqn:Hltu_n1; simpl; trivial.
+ destruct (Int.ltu n Int64.iwordsize') eqn:Hltu_n; simpl; trivial.
+ rewrite Int.repr_unsigned.
+ rewrite Int.repr_unsigned.
+ constructor.
+ + TrivialExists. constructor. econstructor. constructor. eassumption. constructor. simpl. reflexivity. constructor. simpl. reflexivity.
+- apply DEFAULT.
+- TrivialExists. constructor; eauto. constructor. EvalOp. simpl; eauto. constructor. auto.
+Qed.
+
+Theorem eval_shll: binary_constructor_sound shll Val.shll.
+Proof.
+ unfold shll. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shll; auto.
+ red; intros. destruct (is_intconst b) as [n2|] eqn:C.
+- exploit is_intconst_sound; eauto. intros EQ; subst y. apply eval_shllimm; auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_shrlu: binary_constructor_sound shrlu Val.shrlu.
+Proof.
+ unfold shrlu. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shrlu; auto.
+ red; intros. destruct (is_intconst b) as [n2|] eqn:C.
+- exploit is_intconst_sound; eauto. intros EQ; subst y. apply eval_shrluimm; auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_shrl: binary_constructor_sound shrl Val.shrl.
+Proof.
+ unfold shrl. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_shrl; auto.
+ red; intros. destruct (is_intconst b) as [n2|] eqn:C.
+- exploit is_intconst_sound; eauto. intros EQ; subst y. apply eval_shrlimm; auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_mullimm_base: forall n, unary_constructor_sound (mullimm_base n) (fun v => Val.mull v (Vlong n)).
+Proof.
+ intros; unfold mullimm_base. red; intros.
+ assert (DEFAULT: exists v,
+ eval_expr ge sp e m le (Eop Omull (a ::: longconst n ::: Enil)) v
+ /\ Val.lessdef (Val.mull x (Vlong n)) v).
+ { econstructor; split. EvalOp. constructor. eauto. constructor. apply eval_longconst. constructor. simpl; eauto.
+ auto. }
+ generalize (Int64.one_bits'_decomp n); intros D.
+ destruct (Int64.one_bits' n) as [ | i [ | j [ | ? ? ]]] eqn:B.
+- TrivialExists.
+- replace (Val.mull x (Vlong n)) with (Val.shll x (Vint i)).
+ apply eval_shllimm; auto.
+ simpl in D. rewrite D, Int64.add_zero. destruct x; simpl; auto.
+ rewrite (Int64.one_bits'_range n) by (rewrite B; auto with coqlib).
+ rewrite Int64.shl'_mul; auto.
+- set (le' := x :: le).
+ assert (A0: eval_expr ge sp e m le' (Eletvar O) x) by (constructor; reflexivity).
+ exploit (eval_shllimm i). eexact A0. intros (v1 & A1 & B1).
+ exploit (eval_shllimm j). eexact A0. intros (v2 & A2 & B2).
+ exploit (eval_addl). eexact A1. eexact A2. intros (v3 & A3 & B3).
+ exists v3; split. econstructor; eauto.
+ rewrite D. simpl. rewrite Int64.add_zero. destruct x; auto.
+ simpl in *.
+ rewrite (Int64.one_bits'_range n) in B1 by (rewrite B; auto with coqlib).
+ rewrite (Int64.one_bits'_range n) in B2 by (rewrite B; auto with coqlib).
+ inv B1; inv B2. simpl in B3; inv B3.
+ rewrite Int64.mul_add_distr_r. rewrite <- ! Int64.shl'_mul. auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_mullimm: forall n, unary_constructor_sound (mullimm n) (fun v => Val.mull v (Vlong n)).
+Proof.
+ unfold mullimm. intros; red; intros.
+ destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_mullimm; eauto.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ exists (Vlong Int64.zero); split. apply eval_longconst.
+ destruct x; simpl; auto. subst n; rewrite Int64.mul_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.one.
+ exists x; split; auto.
+ destruct x; simpl; auto. subst n; rewrite Int64.mul_one; auto.
+ destruct (mullimm_match a); InvEval.
+- econstructor; split. apply eval_longconst. rewrite Int64.mul_commut; auto.
+- exploit (eval_mullimm_base n); eauto. intros (v2 & A2 & B2).
+ exploit (eval_addlimm (Int64.mul n n2)). eexact A2. intros (v3 & A3 & B3).
+ exists v3; split; auto.
+ subst x. destruct v1; simpl; auto.
+ simpl in B2; inv B2. simpl in B3; inv B3. rewrite Int64.mul_add_distr_l.
+ rewrite (Int64.mul_commut n). auto.
+- apply eval_mullimm_base; auto.
+Qed.
+
+Theorem eval_mull: binary_constructor_sound mull Val.mull.
+Proof.
+ unfold mull. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_mull; auto.
+ red; intros; destruct (mull_match a b); InvEval.
+- rewrite Val.mull_commut. apply eval_mullimm; auto.
+- apply eval_mullimm; auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_mullhu:
+ forall n, unary_constructor_sound (fun a => mullhu a n) (fun v => Val.mullhu v (Vlong n)).
+Proof.
+ unfold mullhu; intros. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_mullhu; auto.
+ red; intros. TrivialExists. constructor. eauto. constructor. apply eval_longconst. constructor. auto.
+Qed.
+
+Theorem eval_mullhs:
+ forall n, unary_constructor_sound (fun a => mullhs a n) (fun v => Val.mullhs v (Vlong n)).
+Proof.
+ unfold mullhs; intros. destruct Archi.splitlong eqn:SL. apply SplitLongproof.eval_mullhs; auto.
+ red; intros. TrivialExists. constructor. eauto. constructor. apply eval_longconst. constructor. auto.
+Qed.
+
+Theorem eval_andlimm: forall n, unary_constructor_sound (andlimm n) (fun v => Val.andl v (Vlong n)).
+Proof.
+ unfold andlimm; intros; red; intros.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ exists (Vlong Int64.zero); split. apply eval_longconst.
+ subst. destruct x; simpl; auto. rewrite Int64.and_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.mone.
+ exists x; split. assumption.
+ subst. destruct x; simpl; auto. rewrite Int64.and_mone; auto.
+ destruct (andlimm_match a); InvEval; subst.
+- econstructor; split. apply eval_longconst. simpl. rewrite Int64.and_commut; auto.
+- TrivialExists. simpl. rewrite Val.andl_assoc. rewrite Int64.and_commut; auto.
+- TrivialExists.
+- TrivialExists.
+Qed.
+
+Lemma int64_eq_commut: forall x y : int64,
+ (Int64.eq x y) = (Int64.eq y x).
+Proof.
+ intros.
+ predSpec Int64.eq Int64.eq_spec x y;
+ predSpec Int64.eq Int64.eq_spec y x;
+ congruence.
+Qed.
+
+Theorem eval_andl: binary_constructor_sound andl Val.andl.
+Proof.
+ unfold andl; destruct Archi.splitlong. apply SplitLongproof.eval_andl.
+ red; intros. destruct (andl_match a b).
+- InvEval. rewrite Val.andl_commut. apply eval_andlimm; auto.
+- InvEval. apply eval_andlimm; auto.
+- (*andn*) InvEval. TrivialExists. simpl. congruence.
+- (*andn reverse*) InvEval. rewrite Val.andl_commut. TrivialExists; simpl. congruence.
+ (*
+- (* selectl *)
+ InvEval.
+ predSpec Int64.eq Int64.eq_spec zero1 Int64.zero; simpl; TrivialExists.
+ + constructor. econstructor; constructor.
+ constructor; try constructor; try constructor; try eassumption.
+ + simpl in *. f_equal. inv H6.
+ unfold selectl.
+ simpl.
+ destruct v3; simpl; trivial.
+ rewrite int64_eq_commut.
+ destruct (Int64.eq i Int64.zero); simpl.
+ * replace (Int64.repr (Int.signed (Int.neg Int.zero))) with Int64.zero by Int64.bit_solve.
+ destruct y; simpl; trivial.
+ * replace (Int64.repr (Int.signed (Int.neg Int.one))) with Int64.mone by Int64.bit_solve.
+ destruct y; simpl; trivial.
+ rewrite Int64.and_commut. rewrite Int64.and_mone. reflexivity.
+ + constructor. econstructor. constructor. econstructor. constructor. econstructor. constructor. eassumption. constructor. simpl. f_equal. constructor. simpl. f_equal. constructor. simpl. f_equal. constructor. eassumption. constructor.
+ + simpl in *. congruence. *)
+- TrivialExists.
+Qed.
+
+Theorem eval_orlimm: forall n, unary_constructor_sound (orlimm n) (fun v => Val.orl v (Vlong n)).
+Proof.
+ unfold orlimm; intros; red; intros.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ exists x; split; auto. subst. destruct x; simpl; auto. rewrite Int64.or_zero; auto.
+ predSpec Int64.eq Int64.eq_spec n Int64.mone.
+ econstructor; split. apply eval_longconst. subst. destruct x; simpl; auto. rewrite Int64.or_mone; auto.
+ destruct (orlimm_match a); InvEval; subst.
+- econstructor; split. apply eval_longconst. simpl. rewrite Int64.or_commut; auto.
+- TrivialExists. simpl. rewrite Val.orl_assoc. rewrite Int64.or_commut; auto.
+- InvEval. TrivialExists.
+- TrivialExists.
+Qed.
+
+
+Theorem eval_orl: binary_constructor_sound orl Val.orl.
+Proof.
+ unfold orl; destruct Archi.splitlong. apply SplitLongproof.eval_orl.
+ red; intros.
+ destruct (orl_match a b).
+- InvEval. rewrite Val.orl_commut. apply eval_orlimm; auto.
+- InvEval. apply eval_orlimm; auto.
+- (*orn*) InvEval. TrivialExists; simpl; congruence.
+- (*orn reversed*) InvEval. rewrite Val.orl_commut. TrivialExists; simpl; congruence.
+
+ - (*insfl first case*)
+ destruct (is_bitfieldl _ _) eqn:Risbitfield.
+ + destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ * rewrite Rnmask in *.
+ inv H. inv H0. inv H4. inv H3. inv H9. inv H8.
+ simpl in H6, H7.
+ inv H6. inv H7.
+ inv H4. inv H3. inv H7.
+ simpl in H6.
+ inv H6.
+ set (zstop := (int64_highest_bit mask)) in *.
+ set (zstart := (Int.unsigned start)) in *.
+
+ TrivialExists.
+ simpl. f_equal.
+
+ unfold insfl.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ simpl.
+ unfold bitfield_maskl.
+ subst zstart.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ * TrivialExists.
+ + TrivialExists.
+ - destruct (is_bitfieldl _ _) eqn:Risbitfield.
+ + destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ * rewrite Rnmask in *.
+ inv H. inv H0. inv H4. inv H6. inv H8. inv H3. inv H8.
+ inv H0. simpl in H7. inv H7.
+ set (zstop := (int64_highest_bit mask)) in *.
+ set (zstart := 0) in *.
+
+ TrivialExists. simpl. f_equal.
+ unfold insfl.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ simpl.
+ subst zstart.
+ f_equal.
+ destruct v0; simpl; trivial.
+ unfold Int.ltu, Int64.iwordsize', Int64.zwordsize, Int64.wordsize.
+ rewrite Int.unsigned_repr.
+ ** rewrite Int.unsigned_repr.
+ *** simpl.
+ rewrite Int64.shl'_zero.
+ reflexivity.
+ *** simpl. unfold Int.max_unsigned. unfold Int.modulus.
+ simpl. lia.
+ ** unfold Int.max_unsigned. unfold Int.modulus.
+ simpl. lia.
+ * TrivialExists.
+ + TrivialExists.
+- TrivialExists.
+Qed.
+
+Theorem eval_xorlimm: forall n, unary_constructor_sound (xorlimm n) (fun v => Val.xorl v (Vlong n)).
+Proof.
+ unfold xorlimm; intros; red; intros.
+ predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ - exists x; split; auto. subst. destruct x; simpl; auto. rewrite Int64.xor_zero; auto.
+ - predSpec Int64.eq Int64.eq_spec n Int64.mone.
+ -- subst n. intros. rewrite <- Val.notl_xorl. TrivialExists.
+ -- destruct (xorlimm_match a); InvEval; subst.
+ + econstructor; split. apply eval_longconst. simpl. rewrite Int64.xor_commut; auto.
+ + rewrite Val.xorl_assoc. simpl. rewrite (Int64.xor_commut n2).
+ predSpec Int64.eq Int64.eq_spec (Int64.xor n n2) Int64.zero.
+ * rewrite H. exists v1; split; auto. destruct v1; simpl; auto. rewrite Int64.xor_zero; auto.
+ * TrivialExists.
+ + TrivialExists.
+Qed.
+
+Theorem eval_xorl: binary_constructor_sound xorl Val.xorl.
+Proof.
+ unfold xorl; destruct Archi.splitlong. apply SplitLongproof.eval_xorl.
+ red; intros. destruct (xorl_match a b).
+- InvEval. rewrite Val.xorl_commut. apply eval_xorlimm; auto.
+- InvEval. apply eval_xorlimm; auto.
+- TrivialExists.
+Qed.
+
+Theorem eval_notl: unary_constructor_sound notl Val.notl.
+Proof.
+ assert (forall v, Val.lessdef (Val.notl (Val.notl v)) v).
+ destruct v; simpl; auto. rewrite Int64.not_involutive; auto.
+ unfold notl; red; intros until x; case (notl_match a); intros; InvEval.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - subst x. exists (Val.andl v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.andl v1 (Vlong n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ - subst x. exists (Val.orl v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.orl v1 (Vlong n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ - subst x. exists (Val.xorl v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.xorl v1 (Vlong n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ (* andn *)
+ - subst x. TrivialExists. simpl.
+ destruct v0; destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int64.not_and_or_not.
+ rewrite Int64.not_involutive.
+ apply Int64.or_commut.
+ - subst x. TrivialExists. simpl.
+ destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int64.not_and_or_not.
+ rewrite Int64.not_involutive.
+ reflexivity.
+ (* orn *)
+ - subst x. TrivialExists. simpl.
+ destruct v0; destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int64.not_or_and_not.
+ rewrite Int64.not_involutive.
+ apply Int64.and_commut.
+ - subst x. TrivialExists. simpl.
+ destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int64.not_or_and_not.
+ rewrite Int64.not_involutive.
+ reflexivity.
+ - subst x. exists v1; split; trivial.
+ - TrivialExists.
+ - TrivialExists.
+Qed.
+
+Theorem eval_divls_base: partial_binary_constructor_sound divls_base Val.divls.
+Proof.
+ unfold divls_base; red; intros.
+ eapply SplitLongproof.eval_divls_base; eauto.
+Qed.
+
+Theorem eval_modls_base: partial_binary_constructor_sound modls_base Val.modls.
+Proof.
+ unfold modls_base; red; intros.
+ eapply SplitLongproof.eval_modls_base; eauto.
+Qed.
+
+Theorem eval_divlu_base: partial_binary_constructor_sound divlu_base Val.divlu.
+Proof.
+ unfold divlu_base; red; intros.
+ eapply SplitLongproof.eval_divlu_base; eauto.
+Qed.
+
+Theorem eval_modlu_base: partial_binary_constructor_sound modlu_base Val.modlu.
+Proof.
+ unfold modlu_base; red; intros.
+ eapply SplitLongproof.eval_modlu_base; eauto.
+Qed.
+
+Theorem eval_shrxlimm:
+ forall le a n x z,
+ eval_expr ge sp e m le a x ->
+ Val.shrxl x (Vint n) = Some z ->
+ exists v, eval_expr ge sp e m le (shrxlimm a n) v /\ Val.lessdef z v.
+Proof.
+ unfold shrxlimm; intros. destruct Archi.splitlong eqn:SL.
++ eapply SplitLongproof.eval_shrxlimm; eauto using Archi.splitlong_ptr32.
++ predSpec Int.eq Int.eq_spec n Int.zero.
+- subst n. destruct x; simpl in H0; inv H0. econstructor; split; eauto.
+ change (Int.ltu Int.zero (Int.repr 63)) with true. simpl. rewrite Int64.shrx'_zero; auto.
+- TrivialExists. simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_cmplu:
+ forall c le a x b y v,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.cmplu (Mem.valid_pointer m) c x y = Some v ->
+ eval_expr ge sp e m le (cmplu c a b) v.
+Proof.
+ unfold cmplu; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_cmplu; eauto using Archi.splitlong_ptr32.
+ unfold Val.cmplu in H1.
+ destruct (Val.cmplu_bool (Mem.valid_pointer m) c x y) as [vb|] eqn:C; simpl in H1; inv H1.
+ destruct (is_longconst a) as [n1|] eqn:LC1; destruct (is_longconst b) as [n2|] eqn:LC2;
+ try (assert (x = Vlong n1) by (eapply is_longconst_sound; eauto));
+ try (assert (y = Vlong n2) by (eapply is_longconst_sound; eauto));
+ subst.
+- simpl in C; inv C. EvalOp. destruct (Int64.cmpu c n1 n2); reflexivity.
+- EvalOp. simpl. rewrite Val.swap_cmplu_bool. rewrite C; auto.
+- EvalOp. simpl; rewrite C; auto.
+- EvalOp. simpl; rewrite C; auto.
+Qed.
+
+Theorem eval_cmpl:
+ forall c le a x b y v,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.cmpl c x y = Some v ->
+ eval_expr ge sp e m le (cmpl c a b) v.
+Proof.
+ unfold cmpl; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_cmpl; eauto.
+ unfold Val.cmpl in H1.
+ destruct (Val.cmpl_bool c x y) as [vb|] eqn:C; simpl in H1; inv H1.
+ destruct (is_longconst a) as [n1|] eqn:LC1; destruct (is_longconst b) as [n2|] eqn:LC2;
+ try (assert (x = Vlong n1) by (eapply is_longconst_sound; eauto));
+ try (assert (y = Vlong n2) by (eapply is_longconst_sound; eauto));
+ subst.
+- simpl in C; inv C. EvalOp. destruct (Int64.cmp c n1 n2); reflexivity.
+- EvalOp. simpl. rewrite Val.swap_cmpl_bool. rewrite C; auto.
+- EvalOp. simpl; rewrite C; auto.
+- EvalOp. simpl; rewrite C; auto.
+Qed.
+
+Theorem eval_longoffloat: partial_unary_constructor_sound longoffloat Val.longoffloat.
+Proof.
+ unfold longoffloat; red; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_longoffloat; eauto.
+ TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_longuoffloat: partial_unary_constructor_sound longuoffloat Val.longuoffloat.
+Proof.
+ unfold longuoffloat; red; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_longuoffloat; eauto.
+ TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_floatoflong: partial_unary_constructor_sound floatoflong Val.floatoflong.
+Proof.
+ unfold floatoflong; red; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_floatoflong; eauto.
+ TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_floatoflongu: partial_unary_constructor_sound floatoflongu Val.floatoflongu.
+Proof.
+ unfold floatoflongu; red; intros. destruct Archi.splitlong eqn:SL.
+ eapply SplitLongproof.eval_floatoflongu; eauto.
+ TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_longofsingle: partial_unary_constructor_sound longofsingle Val.longofsingle.
+Proof.
+ unfold longofsingle; red; intros.
+ destruct x; simpl in H0; inv H0. destruct (Float32.to_long f) as [n|] eqn:EQ; simpl in H2; inv H2.
+ exploit eval_floatofsingle; eauto. intros (v & A & B). simpl in B. inv B.
+ apply Float32.to_long_double in EQ.
+ eapply eval_longoffloat; eauto. simpl.
+ change (Float.of_single f) with (Float32.to_double f); rewrite EQ; auto.
+Qed.
+
+Theorem eval_longuofsingle: partial_unary_constructor_sound longuofsingle Val.longuofsingle.
+Proof.
+ unfold longuofsingle; red; intros. (* destruct Archi.splitlong eqn:SL. *)
+ destruct x; simpl in H0; inv H0. destruct (Float32.to_longu f) as [n|] eqn:EQ; simpl in H2; inv H2.
+ exploit eval_floatofsingle; eauto. intros (v & A & B). simpl in B. inv B.
+ apply Float32.to_longu_double in EQ.
+ eapply eval_longuoffloat; eauto. simpl.
+ change (Float.of_single f) with (Float32.to_double f); rewrite EQ; auto.
+Qed.
+
+Theorem eval_singleoflong: partial_unary_constructor_sound singleoflong Val.singleoflong.
+Proof.
+ unfold singleoflong; red; intros. (* destruct Archi.splitlong eqn:SL. *)
+ eapply SplitLongproof.eval_singleoflong; eauto.
+(* TrivialExists. *)
+Qed.
+
+Theorem eval_singleoflongu: partial_unary_constructor_sound singleoflongu Val.singleoflongu.
+Proof.
+ unfold singleoflongu; red; intros. (* destruct Archi.splitlong eqn:SL. *)
+ eapply SplitLongproof.eval_singleoflongu; eauto.
+(* TrivialExists. *)
+Qed.
+
+End CMCONSTR.
diff --git a/kvx/SelectOp.vp b/kvx/SelectOp.vp
new file mode 100644
index 00000000..4e1087f9
--- /dev/null
+++ b/kvx/SelectOp.vp
@@ -0,0 +1,758 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+
+(** Instruction selection for operators *)
+
+(** The instruction selection pass recognizes opportunities for using
+ combined arithmetic and logical operations and addressing modes
+ offered by the target processor. For instance, the expression [x + 1]
+ can take advantage of the "immediate add" instruction of the processor,
+ and on the PowerPC, the expression [(x >> 6) & 0xFF] can be turned
+ into a "rotate and mask" instruction.
+
+ This file defines functions for building CminorSel expressions and
+ statements, especially expressions consisting of operator
+ applications. These functions examine their arguments to choose
+ cheaper forms of operators whenever possible.
+
+ For instance, [add e1 e2] will return a CminorSel expression semantically
+ equivalent to [Eop Oadd (e1 ::: e2 ::: Enil)], but will use a
+ [Oaddimm] operator if one of the arguments is an integer constant,
+ or suppress the addition altogether if one of the arguments is the
+ null integer. In passing, we perform operator reassociation
+ ([(e + c1) * c2] becomes [(e * c2) + (c1 * c2)]) and a small amount
+ of constant propagation.
+
+ On top of the "smart constructor" functions defined below,
+ module [Selection] implements the actual instruction selection pass.
+*)
+
+Require Archi.
+Require Import Coqlib.
+Require Import Compopts.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Op.
+Require Import CminorSel.
+Require Import OpHelpers.
+Require Import ExtValues ExtFloats.
+Require Import DecBoolOps.
+Require Import Chunks.
+Require Import Builtins.
+Require Compopts.
+
+Local Open Scope cminorsel_scope.
+
+Local Open Scope string_scope.
+Local Open Scope error_monad_scope.
+
+Section SELECT.
+
+Context {hf: helper_functions}.
+
+Inductive to_cond0 :=
+| Cond0_some : condition0 -> expr -> to_cond0
+| Cond0_none : to_cond0
+| Cond0_true : to_cond0
+| Cond0_false : to_cond0.
+
+Definition compu0 c e1 :=
+ match c with
+ | Clt => Cond0_false
+ | Cge => Cond0_true
+ | _ => Cond0_some (Ccompu0 c) e1
+ end.
+
+Definition complu0 c e1 :=
+ match c with
+ | Clt => Cond0_false
+ | Cge => Cond0_true
+ | _ => Cond0_some (Ccomplu0 c) e1
+ end.
+
+Nondetfunction cond_to_condition0 (cond : condition) (args : exprlist) :=
+ match cond, args with
+ | (Ccompimm c x), (e1 ::: Enil) =>
+ if Int.eq_dec x Int.zero
+ then Cond0_some (Ccomp0 c) e1
+ else Cond0_none
+
+ | (Ccompuimm c x), (e1 ::: Enil) =>
+ if Int.eq_dec x Int.zero
+ then compu0 c e1
+ else Cond0_none
+
+ | (Ccomplimm c x), (e1 ::: Enil) =>
+ if Int64.eq_dec x Int64.zero
+ then Cond0_some (Ccompl0 c) e1
+ else Cond0_none
+
+ | (Ccompluimm c x), (e1 ::: Enil) =>
+ if Int64.eq_dec x Int64.zero
+ then complu0 c e1
+ else Cond0_none
+
+ | _, _ => Cond0_none
+ end.
+
+(** Ternary operator *)
+Nondetfunction select0 (ty : typ) (cond0 : condition0) (e1 e2 e3: expr) :=
+ match ty, cond0, e1, e2, e3 with
+ | Tint, cond0, e1, (Eop (Ointconst imm) Enil), e3 =>
+ (Eop (Oselimm cond0 imm) (e1 ::: e3 ::: Enil))
+ | Tint, cond0, (Eop (Ointconst imm) Enil), e2, e3 =>
+ (Eop (Oselimm (negate_condition0 cond0) imm) (e2 ::: e3 ::: Enil))
+ | Tlong, cond0, e1, (Eop (Olongconst imm) Enil), e3 =>
+ (Eop (Osellimm cond0 imm) (e1 ::: e3 ::: Enil))
+ | Tlong, cond0, (Eop (Olongconst imm) Enil), e2, e3 =>
+ (Eop (Osellimm (negate_condition0 cond0) imm) (e2 ::: e3 ::: Enil))
+ | _, _, _ => (Eop (Osel cond0 ty) (e1 ::: e2 ::: e3 ::: Enil))
+ end.
+
+Definition same_expr_pure (e1 e2: expr) :=
+ match e1, e2 with
+ | Evar v1, Evar v2 => if ident_eq v1 v2 then true else false
+ | _, _ => false
+ end.
+
+Definition select (ty : typ) (cond : condition) (args : exprlist) (e1 e2: expr) : option expr :=
+ Some (if same_expr_pure e1 e2 then e1 else
+ match cond_to_condition0 cond args with
+ | Cond0_none => select0 ty (Ccomp0 Cne) e1 e2 (Eop (Ocmp cond) args)
+ | Cond0_some cond0 ec => select0 ty cond0 e1 e2 ec
+ | Cond0_true => e1
+ | Cond0_false => e2
+ end).
+
+
+(** ** Constants **)
+
+Definition addrsymbol (id: ident) (ofs: ptrofs) :=
+ Eop (Oaddrsymbol id ofs) Enil.
+
+Definition addrstack (ofs: ptrofs) :=
+ Eop (Oaddrstack ofs) Enil.
+
+(** ** Integer addition and pointer addition *)
+
+Definition addimm_shlimm sh k2 e1 :=
+ if Compopts.optim_addx tt
+ then
+ match shift1_4_of_z (Int.unsigned sh) with
+ | Some s14 => Eop (Oaddximm s14 k2) (e1:::Enil)
+ | None => Eop (Oaddimm k2) ((Eop (Oshlimm sh) (e1:::Enil)):::Enil)
+ end
+ else Eop (Oaddimm k2) ((Eop (Oshlimm sh) (e1:::Enil)):::Enil).
+
+Nondetfunction addimm (n: int) (e: expr) :=
+ if Int.eq n Int.zero then e else
+ match e with
+ | Eop (Ointconst m) Enil => Eop (Ointconst (Int.add n m)) Enil
+ | Eop (Oaddrsymbol s m) Enil => Eop (Oaddrsymbol s (Ptrofs.add (Ptrofs.of_int n) m)) Enil
+ | Eop (Oaddrstack m) Enil => Eop (Oaddrstack (Ptrofs.add (Ptrofs.of_int n) m)) Enil
+ | Eop (Oaddimm m) (t ::: Enil) => Eop (Oaddimm(Int.add n m)) (t ::: Enil)
+ | Eop (Oaddximm sh m) (t ::: Enil) => Eop (Oaddximm sh (Int.add n m)) (t ::: Enil)
+ | Eop (Oshlimm sh) (t1:::Enil) => addimm_shlimm sh n t1
+ | _ => Eop (Oaddimm n) (e ::: Enil)
+ end.
+
+Definition add_shlimm n e1 e2 :=
+ if Compopts.optim_addx tt
+ then
+ match shift1_4_of_z (Int.unsigned n) with
+ | Some s14 => Eop (Oaddx s14) (e1:::e2:::Enil)
+ | None => Eop Oadd (e2:::(Eop (Oshlimm n) (e1:::Enil)):::Enil)
+ end
+ else Eop Oadd (e2:::(Eop (Oshlimm n) (e1:::Enil)):::Enil).
+
+Nondetfunction add (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 => addimm n1 t2
+ | t1, Eop (Ointconst n2) Enil => addimm n2 t1
+ | Eop (Oaddimm n1) (t1:::Enil), Eop (Oaddimm n2) (t2:::Enil) =>
+ addimm (Int.add n1 n2) (Eop Oadd (t1:::t2:::Enil))
+ | Eop (Oaddimm n1) (t1:::Enil), Eop (Oaddrstack n2) Enil =>
+ Eop Oadd (Eop (Oaddrstack (Ptrofs.add (Ptrofs.of_int n1) n2)) Enil ::: t1 ::: Enil)
+ | Eop (Oaddrstack n1) Enil, Eop (Oaddimm n2) (t2:::Enil) =>
+ Eop Oadd (Eop (Oaddrstack (Ptrofs.add n1 (Ptrofs.of_int n2))) Enil ::: t2 ::: Enil)
+ | Eop (Oaddimm n1) (t1:::Enil), t2 =>
+ addimm n1 (Eop Oadd (t1:::t2:::Enil))
+ | t1, Eop (Oaddimm n2) (t2:::Enil) =>
+ addimm n2 (Eop Oadd (t1:::t2:::Enil))
+ | t1, (Eop Omul (t2:::t3:::Enil)) =>
+ if Compopts.optim_madd tt
+ then Eop Omadd (t1:::t2:::t3:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
+ | (Eop Omul (t2:::t3:::Enil)), t1 =>
+ if Compopts.optim_madd tt
+ then Eop Omadd (t1:::t2:::t3:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
+ | t1, (Eop (Omulimm n) (t2:::Enil)) =>
+ if Compopts.optim_madd tt
+ then Eop (Omaddimm n) (t1:::t2:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
+ | (Eop (Omulimm n) (t2:::Enil)), t1 =>
+ if Compopts.optim_madd tt
+ then Eop (Omaddimm n) (t1:::t2:::Enil)
+ else Eop Oadd (e1:::e2:::Enil)
+ | (Eop (Oshlimm n) (t1:::Enil)), t2 =>
+ add_shlimm n t1 t2
+ | t2, (Eop (Oshlimm n) (t1:::Enil)) =>
+ add_shlimm n t1 t2
+ | _, _ => Eop Oadd (e1:::e2:::Enil)
+ end.
+
+(** ** Integer and pointer subtraction *)
+
+Nondetfunction sub (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | t1, Eop (Ointconst n2) Enil =>
+ addimm (Int.neg n2) t1
+ | Eop (Oaddimm n1) (t1:::Enil), Eop (Oaddimm n2) (t2:::Enil) =>
+ addimm (Int.sub n1 n2) (Eop Osub (t1:::t2:::Enil))
+ | Eop (Oaddimm n1) (t1:::Enil), t2 =>
+ addimm n1 (Eop Osub (t1:::t2:::Enil))
+ | t1, Eop (Oaddimm n2) (t2:::Enil) =>
+ addimm (Int.neg n2) (Eop Osub (t1:::t2:::Enil))
+ | t1, (Eop Omul (t2:::t3:::Enil)) =>
+ Eop Omsub (t1:::t2:::t3:::Enil)
+ | t1, (Eop (Omulimm n) (t2:::Enil)) =>
+ if Compopts.optim_madd tt
+ then Eop (Omaddimm (Int.neg n)) (t1:::t2:::Enil)
+ else Eop Osub (e1:::e2:::Enil)
+ | _, _ => Eop Osub (e1:::e2:::Enil)
+ end.
+
+Nondetfunction negint (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => Eop (Ointconst (Int.neg n)) Enil
+ | _ => Eop Oneg (e ::: Enil)
+ end.
+
+(** ** Immediate shifts *)
+
+Nondetfunction shlimm (e1: expr) (n: int) :=
+ if Int.eq n Int.zero then
+ e1
+ else if negb (Int.ltu n Int.iwordsize) then
+ Eop Oshl (e1 ::: Eop (Ointconst n) Enil ::: Enil)
+ else match e1 with
+ | Eop (Ointconst n1) Enil =>
+ Eop (Ointconst (Int.shl n1 n)) Enil
+ | Eop (Oshlimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int.iwordsize
+ then Eop (Oshlimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshlimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshlimm n) (e1:::Enil)
+ end.
+
+Nondetfunction shruimm (e1: expr) (n: int) :=
+ if Int.eq n Int.zero then
+ e1
+ else if negb (Int.ltu n Int.iwordsize) then
+ Eop Oshru (e1 ::: Eop (Ointconst n) Enil ::: Enil)
+ else match e1 with
+ | Eop (Ointconst n1) Enil =>
+ Eop (Ointconst (Int.shru n1 n)) Enil
+ | Eop (Oshruimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int.iwordsize
+ then Eop (Oshruimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshruimm n) (e1:::Enil)
+ | Eop (Oshlimm n1) (t1:::Enil) =>
+ let stop := Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one) in
+ let start := Z.sub (Z.add (Z.add (Int.unsigned n) stop) Z.one) Int.zwordsize in
+ if is_bitfield stop start
+ then Eop (Oextfz stop start) (t1:::Enil)
+ else Eop (Oshruimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshruimm n) (e1:::Enil)
+ end.
+
+Nondetfunction shrimm (e1: expr) (n: int) :=
+ if Int.eq n Int.zero then
+ e1
+ else if negb (Int.ltu n Int.iwordsize) then
+ Eop Oshr (e1 ::: Eop (Ointconst n) Enil ::: Enil)
+ else match e1 with
+ | Eop (Ointconst n1) Enil =>
+ Eop (Ointconst (Int.shr n1 n)) Enil
+ | Eop (Oshrimm n1) (t1:::Enil) =>
+ if Int.ltu (Int.add n n1) Int.iwordsize
+ then Eop (Oshrimm (Int.add n n1)) (t1:::Enil)
+ else Eop (Oshrimm n) (e1:::Enil)
+ | Eop (Oshlimm n1) (t1:::Enil) =>
+ let stop := Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one) in
+ let start := Z.sub (Z.add (Z.add (Int.unsigned n) stop) Z.one) Int.zwordsize in
+ if is_bitfield stop start
+ then Eop (Oextfs stop start) (t1:::Enil)
+ else Eop (Oshrimm n) (e1:::Enil)
+ | _ =>
+ Eop (Oshrimm n) (e1:::Enil)
+ end.
+
+(** ** Integer multiply *)
+
+Definition mulimm_base (n1: int) (e2: expr) :=
+ match Int.one_bits n1 with
+ | i :: nil =>
+ shlimm e2 i
+ | i :: j :: nil =>
+ Elet e2 (add (shlimm (Eletvar 0) i) (shlimm (Eletvar 0) j))
+ | _ =>
+ Eop (Omulimm n1) (e2 ::: Enil)
+ end.
+
+Nondetfunction mulimm (n1: int) (e2: expr) :=
+ if Int.eq n1 Int.zero then Eop (Ointconst Int.zero) Enil
+ else if Int.eq n1 Int.one then e2
+ else match e2 with
+ | Eop (Ointconst n2) Enil => Eop (Ointconst (Int.mul n1 n2)) Enil
+ | Eop (Oaddimm n2) (t2:::Enil) => addimm (Int.mul n1 n2) (mulimm_base n1 t2)
+ | _ => mulimm_base n1 e2
+ end.
+
+Nondetfunction mul (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 => mulimm n1 t2
+ | t1, Eop (Ointconst n2) Enil => mulimm n2 t1
+ | _, _ => Eop Omul (e1:::e2:::Enil)
+ end.
+
+Definition mulhs (e1: expr) (e2: expr) :=
+ if Archi.ptr64 then
+ Eop Olowlong
+ (Eop (Oshrlimm (Int.repr 32))
+ (Eop Omull (Eop Ocast32signed (e1 ::: Enil) :::
+ Eop Ocast32signed (e2 ::: Enil) ::: Enil) ::: Enil)
+ ::: Enil)
+ else
+ Eop Omulhs (e1 ::: e2 ::: Enil).
+
+Definition mulhu (e1: expr) (e2: expr) :=
+ if Archi.ptr64 then
+ Eop Olowlong
+ (Eop (Oshrluimm (Int.repr 32))
+ (Eop Omull (Eop Ocast32unsigned (e1 ::: Enil) :::
+ Eop Ocast32unsigned (e2 ::: Enil) ::: Enil) ::: Enil)
+ ::: Enil)
+ else
+ Eop Omulhu (e1 ::: e2 ::: Enil).
+
+(** ** Bitwise and, or, xor *)
+
+Nondetfunction andimm (n1: int) (e2: expr) :=
+ if Int.eq n1 Int.zero then Eop (Ointconst Int.zero) Enil
+ else if Int.eq n1 Int.mone then e2
+ else match e2 with
+ | Eop (Ointconst n2) Enil => Eop (Ointconst (Int.and n1 n2)) Enil
+ | Eop (Oandimm n2) (t2:::Enil) => Eop (Oandimm (Int.and n1 n2)) (t2:::Enil)
+ | Eop Onot (t2:::Enil) => Eop (Oandnimm n1) (t2:::Enil)
+ | _ => Eop (Oandimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction and (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 => andimm n1 t2
+ | t1, Eop (Ointconst n2) Enil => andimm n2 t1
+ | (Eop Onot (t1:::Enil)), t2 => Eop Oandn (t1:::t2:::Enil)
+ | t1, (Eop Onot (t2:::Enil)) => Eop Oandn (t2:::t1:::Enil)
+ | _, _ => Eop Oand (e1:::e2:::Enil)
+ end.
+
+Nondetfunction orimm (n1: int) (e2: expr) :=
+ if Int.eq n1 Int.zero then e2
+ else if Int.eq n1 Int.mone then Eop (Ointconst Int.mone) Enil
+ else match e2 with
+ | Eop (Ointconst n2) Enil => Eop (Ointconst (Int.or n1 n2)) Enil
+ | Eop (Oorimm n2) (t2:::Enil) => Eop (Oorimm (Int.or n1 n2)) (t2:::Enil)
+ | Eop Onot (t2:::Enil) => Eop (Oornimm n1) (t2:::Enil)
+ | _ => Eop (Oorimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction or (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 => orimm n1 t2
+ | t1, Eop (Ointconst n2) Enil => orimm n2 t1
+ | Eop (Oshlimm n1) (t1:::Enil), Eop (Oshruimm n2) (t2:::Enil) =>
+ if Int.eq (Int.add n1 n2) Int.iwordsize && same_expr_pure t1 t2
+ then Eop (Ororimm n2) (t1:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ | Eop (Oshruimm n2) (t2:::Enil), Eop (Oshlimm n1) (t1:::Enil) =>
+ if Int.eq (Int.add n1 n2) Int.iwordsize && same_expr_pure t1 t2
+ then Eop (Ororimm n2) (t1:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ | (Eop Onot (t1:::Enil)), t2 => Eop Oorn (t1:::t2:::Enil)
+ | t1, (Eop Onot (t2:::Enil)) => Eop Oorn (t2:::t1:::Enil)
+ | (Eop (Oandimm nmask) (prev:::Enil)),
+ (Eop (Oandimm mask)
+ ((Eop (Oshlimm start) (fld:::Enil)):::Enil)) =>
+ let zstart := Int.unsigned start in
+ let zstop := int_highest_bit mask in
+ if is_bitfield zstop zstart
+ then
+ let mask' := Int.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int.eq_dec mask mask')
+ (Int.eq_dec nmask (Int.not mask'))
+ then Eop (Oinsf zstop zstart) (prev:::fld:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ | (Eop (Oandimm mask)
+ ((Eop (Oshlimm start) (fld:::Enil)):::Enil)),
+ (Eop (Oandimm nmask) (prev:::Enil)) =>
+ let zstart := Int.unsigned start in
+ let zstop := int_highest_bit mask in
+ if is_bitfield zstop zstart
+ then
+ let mask' := Int.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int.eq_dec mask mask')
+ (Int.eq_dec nmask (Int.not mask'))
+ then Eop (Oinsf zstop zstart) (prev:::fld:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ | (Eop (Oandimm nmask) (prev:::Enil)),
+ (Eop (Oandimm mask) (fld:::Enil)) =>
+ let zstart := 0 in
+ let zstop := int_highest_bit mask in
+ if is_bitfield zstop zstart
+ then
+ let mask' := Int.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int.eq_dec mask mask')
+ (Int.eq_dec nmask (Int.not mask'))
+ then Eop (Oinsf zstop zstart) (prev:::fld:::Enil)
+ else let zstart := 0 in
+ let zstop := int_highest_bit nmask in
+ if is_bitfield zstop zstart
+ then
+ let mask' := Int.repr (zbitfield_mask zstop zstart) in
+ if and_dec (Int.eq_dec nmask mask')
+ (Int.eq_dec mask (Int.not mask'))
+ then Eop (Oinsf zstop zstart) (fld:::prev:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ else Eop Oor (e1:::e2:::Enil)
+ | _, _ => Eop Oor (e1:::e2:::Enil)
+ end.
+
+Nondetfunction xorimm (n1: int) (e2: expr) :=
+ if Int.eq n1 Int.zero
+ then e2
+ else
+ if Int.eq n1 Int.mone
+ then Eop Onot (e2:::Enil)
+ else
+ match e2 with
+ | Eop (Ointconst n2) Enil => Eop (Ointconst (Int.xor n1 n2)) Enil
+ | Eop (Oxorimm n2) (t2:::Enil) =>
+ let n := Int.xor n1 n2 in
+ if Int.eq n Int.zero then t2 else Eop (Oxorimm n) (t2:::Enil)
+ | _ => Eop (Oxorimm n1) (e2:::Enil)
+ end.
+
+Nondetfunction xor (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 => xorimm n1 t2
+ | t1, Eop (Ointconst n2) Enil => xorimm n2 t1
+ | _, _ => Eop Oxor (e1:::e2:::Enil)
+ end.
+
+(** ** Integer logical negation *)
+
+Nondetfunction notint (e: expr) :=
+ match e with
+ | Eop Oand (e1:::e2:::Enil) => Eop Onand (e1:::e2:::Enil)
+ | Eop (Oandimm n) (e1:::Enil) => Eop (Onandimm n) (e1:::Enil)
+ | Eop Oor (e1:::e2:::Enil) => Eop Onor (e1:::e2:::Enil)
+ | Eop (Oorimm n) (e1:::Enil) => Eop (Onorimm n) (e1:::Enil)
+ | Eop Oxor (e1:::e2:::Enil) => Eop Onxor (e1:::e2:::Enil)
+ | Eop (Oxorimm n) (e1:::Enil) => Eop (Onxorimm n) (e1:::Enil)
+ | Eop Onand (e1:::e2:::Enil) => Eop Oand (e1:::e2:::Enil)
+ | Eop (Onandimm n) (e1:::Enil) => Eop (Oandimm n) (e1:::Enil)
+ | Eop Onor (e1:::e2:::Enil) => Eop Oor (e1:::e2:::Enil)
+ | Eop (Onorimm n) (e1:::Enil) => Eop (Oorimm n) (e1:::Enil)
+ | Eop Onxor (e1:::e2:::Enil) => Eop Oxor (e1:::e2:::Enil)
+ | Eop (Onxorimm n) (e1:::Enil) => Eop (Oxorimm n) (e1:::Enil)
+ | Eop Oandn (e1:::e2:::Enil) => Eop Oorn (e2:::e1:::Enil)
+ | Eop (Oandnimm n) (e1:::Enil) => Eop (Oorimm (Int.not n)) (e1:::Enil)
+ | Eop Oorn (e1:::e2:::Enil) => Eop Oandn (e2:::e1:::Enil)
+ | Eop (Oornimm n) (e1:::Enil) => Eop (Oandimm (Int.not n)) (e1:::Enil)
+ | Eop Onot (e1:::Enil) => e1
+ | Eop (Ointconst k) Enil => Eop (Ointconst (Int.not k)) Enil
+ | _ => Eop Onot (e:::Enil)
+ end.
+
+(** ** Integer division and modulus *)
+
+Definition divs_base (e1: expr) (e2: expr) :=
+ Eexternal i32_sdiv sig_ii_i (e1 ::: e2 ::: Enil).
+
+Definition mods_base (e1: expr) (e2: expr) :=
+ Eexternal i32_smod sig_ii_i (e1 ::: e2 ::: Enil).
+
+Definition divu_base (e1: expr) (e2: expr) :=
+ Eexternal i32_udiv sig_ii_i (e1 ::: e2 ::: Enil).
+
+Definition modu_base (e1: expr) (e2: expr) :=
+ Eexternal i32_umod sig_ii_i (e1 ::: e2 ::: Enil).
+
+Definition shrximm (e1: expr) (n2: int) :=
+ if Int.eq n2 Int.zero then e1 else Eop (Oshrximm n2) (e1:::Enil).
+
+(* Alternate definition, not convenient for strength reduction during constant propagation *)
+(*
+(* n2 will be less than 31. *)
+
+Definition shrximm_inner (e1: expr) (n2: int) :=
+ Eop (Oshruimm (Int.sub Int.iwordsize n2))
+ ((Eop (Oshrimm (Int.repr (Int.zwordsize - 1)))
+ (e1 ::: Enil))
+ ::: Enil).
+
+Definition shrximm (e1: expr) (n2: int) :=
+ if Int.eq n2 Int.zero then e1
+ else Eop (Oshrimm n2)
+ ((Eop Oadd (e1 ::: shrximm_inner e1 n2 ::: Enil))
+ ::: Enil).
+*)
+
+(** ** General shifts *)
+
+Nondetfunction shl (e1: expr) (e2: expr) :=
+ match e2 with
+ | Eop (Ointconst n2) Enil => shlimm e1 n2
+ | _ => Eop Oshl (e1:::e2:::Enil)
+ end.
+
+Nondetfunction shr (e1: expr) (e2: expr) :=
+ match e2 with
+ | Eop (Ointconst n2) Enil => shrimm e1 n2
+ | _ => Eop Oshr (e1:::e2:::Enil)
+ end.
+
+Nondetfunction shru (e1: expr) (e2: expr) :=
+ match e2 with
+ | Eop (Ointconst n2) Enil => shruimm e1 n2
+ | _ => Eop Oshru (e1:::e2:::Enil)
+ end.
+
+(** ** Floating-point arithmetic *)
+
+Definition negf (e: expr) := Eop Onegf (e ::: Enil).
+Definition absf (e: expr) := Eop Oabsf (e ::: Enil).
+Definition addf (e1 e2: expr) := Eop Oaddf (e1 ::: e2 ::: Enil).
+Definition subf (e1 e2: expr) := Eop Osubf (e1 ::: e2 ::: Enil).
+Definition mulf (e1 e2: expr) := Eop Omulf (e1 ::: e2 ::: Enil).
+
+Definition negfs (e: expr) := Eop Onegfs (e ::: Enil).
+Definition absfs (e: expr) := Eop Oabsfs (e ::: Enil).
+Definition addfs (e1 e2: expr) := Eop Oaddfs (e1 ::: e2 ::: Enil).
+Definition subfs (e1 e2: expr) := Eop Osubfs (e1 ::: e2 ::: Enil).
+Definition mulfs (e1 e2: expr) := Eop Omulfs (e1 ::: e2 ::: Enil).
+
+(** ** Comparisons *)
+
+Nondetfunction compimm (default: comparison -> int -> condition)
+ (sem: comparison -> int -> int -> bool)
+ (c: comparison) (e1: expr) (n2: int) :=
+ match c, e1 with
+ | c, Eop (Ointconst n1) Enil =>
+ Eop (Ointconst (if sem c n1 n2 then Int.one else Int.zero)) Enil
+ | Ceq, Eop (Ocmp c) el =>
+ if Int.eq_dec n2 Int.zero then
+ Eop (Ocmp (negate_condition c)) el
+ else if Int.eq_dec n2 Int.one then
+ Eop (Ocmp c) el
+ else
+ Eop (Ointconst Int.zero) Enil
+ | Cne, Eop (Ocmp c) el =>
+ if Int.eq_dec n2 Int.zero then
+ Eop (Ocmp c) el
+ else if Int.eq_dec n2 Int.one then
+ Eop (Ocmp (negate_condition c)) el
+ else
+ Eop (Ointconst Int.one) Enil
+ | _, _ =>
+ Eop (Ocmp (default c n2)) (e1 ::: Enil)
+ end.
+
+Nondetfunction comp (c: comparison) (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 =>
+ compimm Ccompimm Int.cmp (swap_comparison c) t2 n1
+ | t1, Eop (Ointconst n2) Enil =>
+ compimm Ccompimm Int.cmp c t1 n2
+ | _, _ =>
+ Eop (Ocmp (Ccomp c)) (e1 ::: e2 ::: Enil)
+ end.
+
+Nondetfunction compu (c: comparison) (e1: expr) (e2: expr) :=
+ match e1, e2 with
+ | Eop (Ointconst n1) Enil, t2 =>
+ compimm Ccompuimm Int.cmpu (swap_comparison c) t2 n1
+ | t1, Eop (Ointconst n2) Enil =>
+ compimm Ccompuimm Int.cmpu c t1 n2
+ | _, _ =>
+ Eop (Ocmp (Ccompu c)) (e1 ::: e2 ::: Enil)
+ end.
+
+Definition compf (c: comparison) (e1: expr) (e2: expr) :=
+ Eop (Ocmp (Ccompf c)) (e1 ::: e2 ::: Enil).
+
+Definition compfs (c: comparison) (e1: expr) (e2: expr) :=
+ Eop (Ocmp (Ccompfs c)) (e1 ::: e2 ::: Enil).
+
+(** ** Integer conversions *)
+
+Definition cast8unsigned (e: expr) := andimm (Int.repr 255) e.
+
+Nondetfunction cast8signed (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => Eop (Ointconst (Int.sign_ext 8 n)) Enil
+ | _ => Eop Ocast8signed (e ::: Enil)
+ end.
+
+Definition cast16unsigned (e: expr) := andimm (Int.repr 65535) e.
+
+Nondetfunction cast16signed (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => Eop (Ointconst (Int.sign_ext 16 n)) Enil
+ | _ => Eop Ocast16signed (e ::: Enil)
+ end.
+
+(** ** Floating-point conversions *)
+
+Definition intoffloat (e: expr) := Eop Ointoffloat (e ::: Enil).
+Definition intuoffloat (e: expr) := Eop Ointuoffloat (e ::: Enil).
+
+Nondetfunction floatofintu (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => Eop (Ofloatconst (Float.of_intu n)) Enil
+ | _ => Eop Ofloatoflongu ((Eop Ocast32unsigned (e ::: Enil)) ::: Enil)
+ end.
+
+Nondetfunction floatofint (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => Eop (Ofloatconst (Float.of_int n)) Enil
+ | _ => Eop Ofloatoflong ((Eop Ocast32signed (e ::: Enil)) ::: Enil)
+ end.
+
+Definition intofsingle (e: expr) := Eop Ointofsingle (e ::: Enil).
+Definition singleofint (e: expr) := Eop Osingleofint (e ::: Enil).
+
+Definition intuofsingle (e: expr) := Eop Ointuofsingle (e ::: Enil).
+Definition singleofintu (e: expr) := Eop Osingleofintu (e ::: Enil).
+
+Definition singleoffloat (e: expr) := Eop Osingleoffloat (e ::: Enil).
+Definition floatofsingle (e: expr) := Eop Ofloatofsingle (e ::: Enil).
+
+(** ** Recognition of addressing modes for load and store operations *)
+
+Nondetfunction addressing (chunk: memory_chunk) (e: expr) :=
+ match e with
+ | Eop (Oaddrstack n) Enil => (Ainstack n, Enil)
+ | Eop (Oaddrsymbol id ofs) Enil =>
+ (if (orb (Archi.pic_code tt) (negb (Compopts.optim_globaladdrtmp tt)))
+ then (Aindexed Ptrofs.zero, e:::Enil)
+ else (Aglobal id ofs, Enil))
+ | Eop (Oaddimm n) (e1:::Enil) => (Aindexed (Ptrofs.of_int n), e1:::Enil)
+ | Eop (Oaddlimm n) (e1:::Enil) => (Aindexed (Ptrofs.of_int64 n), e1:::Enil)
+ | Eop Oaddl (e1:::(Eop (Oshllimm scale) (e2:::Enil)):::Enil) =>
+ (if Compopts.optim_xsaddr tt
+ then let zscale := Int.unsigned scale in
+ if Z.eq_dec zscale (zscale_of_chunk chunk)
+ then (Aindexed2XS zscale, e1:::e2:::Enil)
+ else (Aindexed2, e1:::(Eop (Oshllimm scale) (e2:::Enil)):::Enil)
+ else (Aindexed2, e1:::(Eop (Oshllimm scale) (e2:::Enil)):::Enil))
+ | Eop (Oaddxl sh) (e1:::e2:::Enil) =>
+ let zscale := ExtValues.z_of_shift1_4 sh in
+ let scale := Int.repr zscale in
+ (if Compopts.optim_xsaddr tt
+ then if Z.eq_dec zscale (zscale_of_chunk chunk)
+ then (Aindexed2XS zscale, e2:::e1:::Enil)
+ else (Aindexed2, e2:::(Eop (Oshllimm scale) (e1:::Enil)):::Enil)
+ else (Aindexed2, e2:::(Eop (Oshllimm scale) (e1:::Enil)):::Enil))
+ | Eop Oaddl (e1:::e2:::Enil) => (Aindexed2, e1:::e2:::Enil)
+ | _ => (Aindexed Ptrofs.zero, e:::Enil)
+ end.
+
+(** ** Arguments of builtins *)
+
+Nondetfunction builtin_arg (e: expr) :=
+ match e with
+ | Eop (Ointconst n) Enil => BA_int n
+ | Eop (Oaddrsymbol id ofs) Enil => BA_addrglobal id ofs
+ | Eop (Oaddrstack ofs) Enil => BA_addrstack ofs
+ | Eop Omakelong (Eop (Ointconst h) Enil ::: Eop (Ointconst l) Enil ::: Enil) =>
+ BA_long (Int64.ofwords h l)
+ | Eop Omakelong (h ::: l ::: Enil) => BA_splitlong (BA h) (BA l)
+ | Eload chunk (Ainstack ofs) Enil => BA_loadstack chunk ofs
+ | Eop (Oaddimm n) (e1:::Enil) =>
+ if Archi.ptr64 then BA e else BA_addptr (BA e1) (BA_int n)
+ | Eop (Oaddlimm n) (e1:::Enil) =>
+ if Archi.ptr64 then BA_addptr (BA e1) (BA_long n) else BA e
+ | _ => BA e
+ end.
+
+(* float division *)
+
+Definition divf_base (e1: expr) (e2: expr) :=
+ (* Eop Odivf (e1 ::: e2 ::: Enil). *)
+ Eexternal f64_div sig_ff_f (e1 ::: e2 ::: Enil).
+
+Definition divfs_base1 (e2 : expr) :=
+ Eop Oinvfs (e2 ::: Enil).
+Definition divfs_baseX (e1 : expr) (e2 : expr) :=
+ (* Eop Odivf (e1 ::: e2 ::: Enil). *)
+ Eexternal f32_div sig_ss_s (e1 ::: e2 ::: Enil).
+
+Nondetfunction divfs_base (e1: expr) :=
+ match e1 with
+ | Eop (Osingleconst f) Enil =>
+ (if Float32.eq_dec f ExtFloat32.one
+ then divfs_base1
+ else divfs_baseX e1)
+ | _ => divfs_baseX e1
+ end.
+
+Nondetfunction gen_fma args :=
+ match args with
+ | (Eop Onegf (e1:::Enil)):::e2:::e3:::Enil => Some (Eop Ofmsubf (e3:::e1:::e2:::Enil))
+ | e1:::e2:::e3:::Enil => Some (Eop Ofmaddf (e3:::e1:::e2:::Enil))
+ | _ => None
+ end.
+
+Nondetfunction gen_fmaf args :=
+ match args with
+ | (Eop Onegfs (e1:::Enil)):::e2:::e3:::Enil => Some (Eop Ofmsubfs (e3:::e1:::e2:::Enil))
+ | e1:::e2:::e3:::Enil => Some (Eop Ofmaddfs (e3:::e1:::e2:::Enil))
+ | _ => None
+ end.
+
+Definition platform_builtin (b: platform_builtin) (args: exprlist) : option expr :=
+ match b with
+ | BI_fmin => Some (Eop Ominf args)
+ | BI_fmax => Some (Eop Omaxf args)
+ | BI_fminf => Some (Eop Ominfs args)
+ | BI_fmaxf => Some (Eop Omaxfs args)
+ | BI_fma => gen_fma args
+ | BI_fmaf => gen_fmaf args
+ end.
+End SELECT.
+
+(* Local Variables: *)
+(* mode: coq *)
+(* End: *)
diff --git a/kvx/SelectOpproof.v b/kvx/SelectOpproof.v
new file mode 100644
index 00000000..0ede1e2d
--- /dev/null
+++ b/kvx/SelectOpproof.v
@@ -0,0 +1,1901 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Correctness of instruction selection for operators *)
+
+Require Import Builtins.
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Floats.
+Require Import Values.
+Require Import ExtValues.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Cminor.
+Require Import Op.
+Require Import CminorSel.
+Require Import Builtins1.
+Require Import SelectOp.
+Require Import Events.
+Require Import OpHelpers.
+Require Import OpHelpersproof.
+Require Import DecBoolOps.
+Require Import Lia.
+
+Local Open Scope cminorsel_scope.
+Local Open Scope string_scope.
+
+
+(** * Useful lemmas and tactics *)
+
+(** The following are trivial lemmas and custom tactics that help
+ perform backward (inversion) and forward reasoning over the evaluation
+ of operator applications. *)
+
+Ltac EvalOp := eapply eval_Eop; eauto with evalexpr.
+
+Ltac InvEval1 :=
+ match goal with
+ | [ H: (eval_expr _ _ _ _ _ (Eop _ Enil) _) |- _ ] =>
+ inv H; InvEval1
+ | [ H: (eval_expr _ _ _ _ _ (Eop _ (_ ::: Enil)) _) |- _ ] =>
+ inv H; InvEval1
+ | [ H: (eval_expr _ _ _ _ _ (Eop _ (_ ::: _ ::: Enil)) _) |- _ ] =>
+ inv H; InvEval1
+ | [ H: (eval_exprlist _ _ _ _ _ Enil _) |- _ ] =>
+ inv H; InvEval1
+ | [ H: (eval_exprlist _ _ _ _ _ (_ ::: _) _) |- _ ] =>
+ inv H; InvEval1
+ | _ =>
+ idtac
+ end.
+
+Ltac InvEval2 :=
+ match goal with
+ | [ H: (eval_operation _ _ _ nil _ = Some _) |- _ ] =>
+ simpl in H; inv H
+ | [ H: (eval_operation _ _ _ (_ :: nil) _ = Some _) |- _ ] =>
+ simpl in H; FuncInv
+ | [ H: (eval_operation _ _ _ (_ :: _ :: nil) _ = Some _) |- _ ] =>
+ simpl in H; FuncInv
+ | [ H: (eval_operation _ _ _ (_ :: _ :: _ :: nil) _ = Some _) |- _ ] =>
+ simpl in H; FuncInv
+ | _ =>
+ idtac
+ end.
+
+Ltac InvEval := InvEval1; InvEval2; InvEval2.
+
+Ltac TrivialExists :=
+ match goal with
+ | [ |- exists v, _ /\ Val.lessdef ?a v ] => exists a; split; [EvalOp | auto]
+ end.
+
+(** * Correctness of the smart constructors *)
+
+Section CMCONSTR.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
+Variable sp: val.
+Variable e: env.
+Variable m: mem.
+
+(* Helper lemmas - from SplitLongproof.v *)
+
+Ltac UseHelper := decompose [Logic.and] arith_helpers_correct; eauto.
+Ltac DeclHelper := red in HELPERS; decompose [Logic.and] HELPERS; eauto.
+
+Lemma eval_helper:
+ forall le id name sg args vargs vres,
+ eval_exprlist ge sp e m le args vargs ->
+ helper_declared prog id name sg ->
+ external_implements name sg vargs vres ->
+ eval_expr ge sp e m le (Eexternal id sg args) vres.
+Proof.
+ intros.
+ red in H0. apply Genv.find_def_symbol in H0. destruct H0 as (b & P & Q).
+ rewrite <- Genv.find_funct_ptr_iff in Q.
+ econstructor; eauto.
+Qed.
+
+Corollary eval_helper_1:
+ forall le id name sg arg1 varg1 vres,
+ eval_expr ge sp e m le arg1 varg1 ->
+ helper_declared prog id name sg ->
+ external_implements name sg (varg1::nil) vres ->
+ eval_expr ge sp e m le (Eexternal id sg (arg1 ::: Enil)) vres.
+Proof.
+ intros. eapply eval_helper; eauto. constructor; auto. constructor.
+Qed.
+
+Corollary eval_helper_2:
+ forall le id name sg arg1 arg2 varg1 varg2 vres,
+ eval_expr ge sp e m le arg1 varg1 ->
+ eval_expr ge sp e m le arg2 varg2 ->
+ helper_declared prog id name sg ->
+ external_implements name sg (varg1::varg2::nil) vres ->
+ eval_expr ge sp e m le (Eexternal id sg (arg1 ::: arg2 ::: Enil)) vres.
+Proof.
+ intros. eapply eval_helper; eauto. constructor; auto. constructor; auto. constructor.
+Qed.
+
+(** We now show that the code generated by "smart constructor" functions
+ such as [Selection.notint] behaves as expected. Continuing the
+ [notint] example, we show that if the expression [e]
+ evaluates to some integer value [Vint n], then [Selection.notint e]
+ evaluates to a value [Vint (Int.not n)] which is indeed the integer
+ negation of the value of [e].
+
+ All proofs follow a common pattern:
+- Reasoning by case over the result of the classification functions
+ (such as [add_match] for integer addition), gathering additional
+ information on the shape of the argument expressions in the non-default
+ cases.
+- Inversion of the evaluations of the arguments, exploiting the additional
+ information thus gathered.
+- Equational reasoning over the arithmetic operations performed,
+ using the lemmas from the [Int] and [Float] modules.
+- Construction of an evaluation derivation for the expression returned
+ by the smart constructor.
+*)
+
+Definition unary_constructor_sound (cstr: expr -> expr) (sem: val -> val) : Prop :=
+ forall le a x,
+ eval_expr ge sp e m le a x ->
+ exists v, eval_expr ge sp e m le (cstr a) v /\ Val.lessdef (sem x) v.
+
+Definition binary_constructor_sound (cstr: expr -> expr -> expr) (sem: val -> val -> val) : Prop :=
+ forall le a x b y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (cstr a b) v /\ Val.lessdef (sem x y) v.
+
+Theorem eval_addrsymbol:
+ forall le id ofs,
+ exists v, eval_expr ge sp e m le (addrsymbol id ofs) v /\ Val.lessdef (Genv.symbol_address ge id ofs) v.
+Proof.
+ intros. unfold addrsymbol. econstructor; split.
+ EvalOp. simpl; eauto.
+ auto.
+Qed.
+
+Theorem eval_addrstack:
+ forall le ofs,
+ exists v, eval_expr ge sp e m le (addrstack ofs) v /\ Val.lessdef (Val.offset_ptr sp ofs) v.
+Proof.
+ intros. unfold addrstack. econstructor; split.
+ EvalOp. simpl; eauto.
+ auto.
+Qed.
+
+Theorem eval_addimm_shlimm:
+ forall sh k2, unary_constructor_sound (addimm_shlimm sh k2) (fun x => ExtValues.addx sh x (Vint k2)).
+Proof.
+ red; unfold addimm_shlimm; intros.
+ destruct (Compopts.optim_addx tt).
+ {
+ destruct (shift1_4_of_z (Int.unsigned sh)) as [s14 |] eqn:SHIFT.
+ - TrivialExists. simpl.
+ f_equal.
+ unfold shift1_4_of_z, int_of_shift1_4, z_of_shift1_4 in *.
+ destruct (Z.eq_dec _ _) as [e1|].
+ { replace s14 with SHIFT1 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e1.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e1.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e2|].
+ { replace s14 with SHIFT2 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e2.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e2.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e3|].
+ { replace s14 with SHIFT3 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e3.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e3.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ destruct (Z.eq_dec _ _) as [e4|].
+ { replace s14 with SHIFT4 by congruence.
+ destruct x; simpl; trivial.
+ replace (Int.ltu _ _) with true by reflexivity.
+ unfold Int.ltu.
+ rewrite e4.
+ replace (if zlt _ _ then true else false) with true by reflexivity.
+ rewrite <- e4.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ }
+ discriminate.
+ - unfold addx. rewrite Val.add_commut.
+ TrivialExists.
+ repeat (try eassumption; try econstructor).
+ simpl.
+ reflexivity.
+ }
+ { unfold addx. rewrite Val.add_commut.
+ TrivialExists.
+ repeat (try eassumption; try econstructor).
+ simpl.
+ reflexivity.
+ }
+Qed.
+
+Theorem eval_addimm:
+ forall n, unary_constructor_sound (addimm n) (fun x => Val.add x (Vint n)).
+Proof.
+ red; unfold addimm; intros until x.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ - subst n. intros. exists x; split; auto.
+ destruct x; simpl; auto.
+ rewrite Int.add_zero; auto.
+ - case (addimm_match a); intros; InvEval; simpl.
+ + TrivialExists; simpl. rewrite Int.add_commut. auto.
+ + econstructor; split. EvalOp. simpl; eauto.
+ unfold Genv.symbol_address. destruct (Genv.find_symbol ge s); simpl; auto.
+ + econstructor; split. EvalOp. simpl; eauto.
+ destruct sp; simpl; auto.
+ + TrivialExists; simpl. subst x. rewrite Val.add_assoc. rewrite Int.add_commut. auto.
+ + TrivialExists; simpl. subst x.
+ destruct v1; simpl; trivial.
+ destruct (Int.ltu _ _); simpl; trivial.
+ rewrite Int.add_assoc. rewrite Int.add_commut.
+ reflexivity.
+ + pose proof eval_addimm_shlimm as ADDX.
+ unfold unary_constructor_sound in ADDX.
+ unfold addx in ADDX.
+ rewrite Val.add_commut.
+ subst x.
+ apply ADDX; assumption.
+ + TrivialExists.
+Qed.
+
+Lemma eval_addx: forall n, binary_constructor_sound (add_shlimm n) (ExtValues.addx n).
+Proof.
+ red.
+ intros.
+ unfold add_shlimm.
+ destruct (Compopts.optim_addx tt).
+ {
+ destruct (shift1_4_of_z (Int.unsigned n)) as [s14 |] eqn:SHIFT.
+ - TrivialExists.
+ simpl.
+ f_equal. f_equal.
+ unfold shift1_4_of_z, int_of_shift1_4, z_of_shift1_4 in *.
+ destruct (Z.eq_dec _ _) as [e1|].
+ { replace s14 with SHIFT1 by congruence.
+ rewrite <- e1.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e2|].
+ { replace s14 with SHIFT2 by congruence.
+ rewrite <- e2.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e3|].
+ { replace s14 with SHIFT3 by congruence.
+ rewrite <- e3.
+ apply Int.repr_unsigned. }
+ destruct (Z.eq_dec _ _) as [e4|].
+ { replace s14 with SHIFT4 by congruence.
+ rewrite <- e4.
+ apply Int.repr_unsigned. }
+ discriminate.
+ - TrivialExists;
+ repeat econstructor; eassumption.
+ }
+ { TrivialExists;
+ repeat econstructor; eassumption.
+ }
+Qed.
+
+Theorem eval_add: binary_constructor_sound add Val.add.
+Proof.
+ red; intros until y.
+ unfold add; case (add_match a b); intros; InvEval.
+ - rewrite Val.add_commut. apply eval_addimm; auto.
+ - apply eval_addimm; auto.
+ - subst.
+ replace (Val.add (Val.add v1 (Vint n1)) (Val.add v0 (Vint n2)))
+ with (Val.add (Val.add v1 v0) (Val.add (Vint n1) (Vint n2))).
+ apply eval_addimm. EvalOp.
+ repeat rewrite Val.add_assoc. decEq. apply Val.add_permut.
+ - subst. econstructor; split.
+ EvalOp. constructor. EvalOp. simpl; eauto. constructor. eauto. constructor. simpl; eauto.
+ rewrite Val.add_commut. destruct sp; simpl; auto.
+ destruct v1; simpl; auto.
+ - subst. econstructor; split.
+ EvalOp. constructor. EvalOp. simpl; eauto. constructor. eauto. constructor. simpl; eauto.
+ destruct sp; simpl; auto.
+ destruct v1; simpl; auto.
+ - subst.
+ replace (Val.add (Val.add v1 (Vint n1)) y)
+ with (Val.add (Val.add v1 y) (Vint n1)).
+ apply eval_addimm. EvalOp.
+ repeat rewrite Val.add_assoc. decEq. apply Val.add_commut.
+ - subst.
+ replace (Val.add x (Val.add v1 (Vint n2)))
+ with (Val.add (Val.add x v1) (Vint n2)).
+ apply eval_addimm. EvalOp.
+ repeat rewrite Val.add_assoc. reflexivity.
+ - (* Omadd *)
+ subst. destruct (Compopts.optim_madd tt); TrivialExists;
+ repeat (eauto; econstructor; simpl).
+ - (* Omadd rev *)
+ subst. destruct (Compopts.optim_madd tt); TrivialExists;
+ repeat (eauto; econstructor; simpl).
+ simpl. rewrite Val.add_commut. reflexivity.
+ - (* Omaddimm *)
+ subst. destruct (Compopts.optim_madd tt); TrivialExists;
+ repeat (eauto; econstructor; simpl).
+ - (* Omaddimm rev *)
+ subst. destruct (Compopts.optim_madd tt); TrivialExists;
+ repeat (eauto; econstructor; simpl).
+ simpl. rewrite Val.add_commut. reflexivity.
+ (* Oaddx *)
+ - subst. pose proof eval_addx as ADDX.
+ unfold binary_constructor_sound in ADDX.
+ rewrite Val.add_commut.
+ apply ADDX; assumption.
+ (* Oaddx *)
+ - subst. pose proof eval_addx as ADDX.
+ unfold binary_constructor_sound in ADDX.
+ apply ADDX; assumption.
+ - TrivialExists.
+Qed.
+
+Theorem eval_sub: binary_constructor_sound sub Val.sub.
+Proof.
+ red; intros until y.
+ unfold sub; case (sub_match a b); intros; InvEval.
+ - rewrite Val.sub_add_opp. apply eval_addimm; auto.
+ - subst. rewrite Val.sub_add_l. rewrite Val.sub_add_r.
+ rewrite Val.add_assoc. simpl. rewrite Int.add_commut. rewrite <- Int.sub_add_opp.
+ apply eval_addimm; EvalOp.
+ - subst. rewrite Val.sub_add_l. apply eval_addimm; EvalOp.
+ - subst. rewrite Val.sub_add_r. apply eval_addimm; EvalOp.
+ - TrivialExists. simpl. subst. reflexivity.
+ - destruct (Compopts.optim_madd tt).
+ + TrivialExists. simpl. subst.
+ rewrite sub_add_neg.
+ rewrite neg_mul_distr_r.
+ unfold Val.neg.
+ reflexivity.
+ + TrivialExists. repeat (eauto; econstructor).
+ simpl. subst. reflexivity.
+ - TrivialExists.
+Qed.
+
+Theorem eval_negint: unary_constructor_sound negint (fun v => Val.sub Vzero v).
+Proof.
+ red; intros until x. unfold negint. case (negint_match a); intros; InvEval.
+ TrivialExists.
+ TrivialExists.
+Qed.
+
+Theorem eval_shlimm:
+ forall n, unary_constructor_sound (fun a => shlimm a n)
+ (fun x => Val.shl x (Vint n)).
+Proof.
+ red; intros until x. unfold shlimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros; subst. exists x; split; auto. destruct x; simpl; auto. rewrite Int.shl_zero; auto.
+
+ destruct (Int.ltu n Int.iwordsize) eqn:LT; simpl.
+ destruct (shlimm_match a); intros; InvEval.
+ - exists (Vint (Int.shl n1 n)); split. EvalOp.
+ simpl. rewrite LT. auto.
+ - destruct (Int.ltu (Int.add n n1) Int.iwordsize) eqn:?.
+ + exists (Val.shl v1 (Vint (Int.add n n1))); split. EvalOp.
+ subst. destruct v1; simpl; auto.
+ rewrite Heqb.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:?; simpl; auto.
+ destruct (Int.ltu n Int.iwordsize) eqn:?; simpl; auto.
+ rewrite Int.add_commut. rewrite Int.shl_shl; auto. rewrite Int.add_commut; auto.
+ + subst. TrivialExists. econstructor. EvalOp. simpl; eauto. constructor.
+ simpl. auto.
+ - TrivialExists.
+ - intros; TrivialExists. constructor. eauto. constructor. EvalOp. simpl; eauto. constructor.
+ auto.
+Qed.
+
+Theorem eval_shruimm:
+ forall n, unary_constructor_sound (fun a => shruimm a n)
+ (fun x => Val.shru x (Vint n)).
+Proof.
+ red; intros until x. unfold shruimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros; subst. exists x; split; auto. destruct x; simpl; auto. rewrite Int.shru_zero; auto.
+
+ destruct (Int.ltu n Int.iwordsize) eqn:LT.
+ destruct (shruimm_match a); intros; InvEval.
+ - exists (Vint (Int.shru n1 n)); split. EvalOp.
+ simpl. rewrite LT; auto.
+ - destruct (Int.ltu (Int.add n n1) Int.iwordsize) eqn:?.
+ exists (Val.shru v1 (Vint (Int.add n n1))); split. EvalOp.
+ subst. destruct v1; simpl; auto.
+ rewrite Heqb.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:?; simpl; auto.
+ rewrite LT. rewrite Int.add_commut. rewrite Int.shru_shru; auto. rewrite Int.add_commut; auto.
+ subst. TrivialExists. econstructor. EvalOp. simpl; eauto. constructor.
+ simpl. auto.
+ - subst x.
+ simpl negb.
+ cbn iota.
+ destruct (is_bitfield _ _) eqn:BOUNDS.
+ + exists (extfz (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one))
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int.zwordsize) v1).
+ split.
+ ++ EvalOp.
+ ++ unfold extfz.
+ rewrite BOUNDS.
+ destruct v1; try (simpl; apply Val.lessdef_undef).
+ replace (Z.sub Int.zwordsize
+ (Z.add (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)) with (Int.unsigned n1) by lia.
+ replace (Z.sub Int.zwordsize
+ (Z.sub
+ (Z.add (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int.zwordsize))) with (Int.unsigned n) by lia.
+ rewrite Int.repr_unsigned.
+ rewrite Int.repr_unsigned.
+ simpl.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:Hltu_n1; simpl; trivial.
+ simpl.
+ destruct (Int.ltu n Int.iwordsize) eqn:Hltu_n; simpl; trivial.
+ + TrivialExists. constructor. econstructor. constructor. eassumption. constructor. simpl. reflexivity. constructor. simpl. reflexivity.
+ - TrivialExists.
+ - intros; TrivialExists. constructor. eauto. constructor. EvalOp. simpl; eauto. constructor.
+ auto.
+Qed.
+
+Theorem eval_shrimm:
+ forall n, unary_constructor_sound (fun a => shrimm a n)
+ (fun x => Val.shr x (Vint n)).
+Proof.
+ red; intros until x. unfold shrimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros; subst. exists x; split; auto. destruct x; simpl; auto. rewrite Int.shr_zero; auto.
+
+ destruct (Int.ltu n Int.iwordsize) eqn:LT.
+ destruct (shrimm_match a); intros; InvEval.
+ - exists (Vint (Int.shr n1 n)); split. EvalOp.
+ simpl. rewrite LT; auto.
+ - destruct (Int.ltu (Int.add n n1) Int.iwordsize) eqn:?.
+ exists (Val.shr v1 (Vint (Int.add n n1))); split. EvalOp.
+ subst. destruct v1; simpl; auto.
+ rewrite Heqb.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:?; simpl; auto.
+ rewrite LT.
+ rewrite Int.add_commut. rewrite Int.shr_shr; auto. rewrite Int.add_commut; auto.
+ subst. TrivialExists. econstructor. EvalOp. simpl; eauto. constructor.
+ simpl. auto.
+ - subst x.
+ simpl negb.
+ cbn iota.
+ destruct (is_bitfield _ _) eqn:BOUNDS.
+ + exists (extfs (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one))
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int.zwordsize) v1).
+ split.
+ ++ EvalOp.
+ ++ unfold extfs.
+ rewrite BOUNDS.
+ destruct v1; try (simpl; apply Val.lessdef_undef).
+ replace (Z.sub Int.zwordsize
+ (Z.add (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)) with (Int.unsigned n1) by lia.
+ replace (Z.sub Int.zwordsize
+ (Z.sub
+ (Z.add (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)) Z.one)
+ (Z.sub
+ (Z.add
+ (Z.add (Int.unsigned n) (Z.sub Int.zwordsize (Z.add (Int.unsigned n1) Z.one)))
+ Z.one) Int.zwordsize))) with (Int.unsigned n) by lia.
+ rewrite Int.repr_unsigned.
+ rewrite Int.repr_unsigned.
+ simpl.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:Hltu_n1; simpl; trivial.
+ simpl.
+ destruct (Int.ltu n Int.iwordsize) eqn:Hltu_n; simpl; trivial.
+ + TrivialExists. constructor. econstructor. constructor. eassumption. constructor. simpl. reflexivity. constructor. simpl. reflexivity.
+ - TrivialExists.
+ - intros; TrivialExists. constructor. eauto. constructor. EvalOp. simpl; eauto. constructor.
+ auto.
+Qed.
+
+Lemma eval_mulimm_base:
+ forall n, unary_constructor_sound (mulimm_base n) (fun x => Val.mul x (Vint n)).
+Proof.
+ intros; red; intros; unfold mulimm_base.
+
+ assert (DFL: exists v, eval_expr ge sp e m le (Eop Omul (Eop (Ointconst n) Enil ::: a ::: Enil)) v /\ Val.lessdef (Val.mul x (Vint n)) v).
+ TrivialExists. econstructor. EvalOp. simpl; eauto. econstructor. eauto. constructor.
+ rewrite Val.mul_commut. auto.
+
+ generalize (Int.one_bits_decomp n).
+ generalize (Int.one_bits_range n).
+ destruct (Int.one_bits n).
+ - intros. TrivialExists.
+ - destruct l.
+ + intros. rewrite H1. simpl.
+ rewrite Int.add_zero.
+ replace (Vint (Int.shl Int.one i)) with (Val.shl Vone (Vint i)). rewrite Val.shl_mul.
+ apply eval_shlimm. auto. simpl. rewrite H0; auto with coqlib.
+ + destruct l.
+ intros. rewrite H1. simpl.
+ exploit (eval_shlimm i (x :: le) (Eletvar 0) x). constructor; auto. intros [v1 [A1 B1]].
+ exploit (eval_shlimm i0 (x :: le) (Eletvar 0) x). constructor; auto. intros [v2 [A2 B2]].
+ exploit (eval_add (x :: le)). eexact A1. eexact A2. intros [v [A B]].
+ exists v; split. econstructor; eauto.
+ rewrite Int.add_zero.
+ replace (Vint (Int.add (Int.shl Int.one i) (Int.shl Int.one i0)))
+ with (Val.add (Val.shl Vone (Vint i)) (Val.shl Vone (Vint i0))).
+ rewrite Val.mul_add_distr_r.
+ repeat rewrite Val.shl_mul. eapply Val.lessdef_trans. 2: eauto. apply Val.add_lessdef; auto.
+ simpl. repeat rewrite H0; auto with coqlib.
+ intros. TrivialExists.
+Qed.
+
+Theorem eval_mulimm:
+ forall n, unary_constructor_sound (mulimm n) (fun x => Val.mul x (Vint n)).
+Proof.
+ intros; red; intros until x; unfold mulimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros. exists (Vint Int.zero); split. EvalOp.
+ destruct x; simpl; auto. subst n. rewrite Int.mul_zero. auto.
+
+ predSpec Int.eq Int.eq_spec n Int.one.
+ intros. exists x; split; auto.
+ destruct x; simpl; auto. subst n. rewrite Int.mul_one. auto.
+
+ case (mulimm_match a); intros; InvEval.
+ - TrivialExists. simpl. rewrite Int.mul_commut; auto.
+ - subst. rewrite Val.mul_add_distr_l.
+ exploit eval_mulimm_base; eauto. instantiate (1 := n). intros [v' [A1 B1]].
+ exploit (eval_addimm (Int.mul n n2) le (mulimm_base n t2) v'). auto. intros [v'' [A2 B2]].
+ exists v''; split; auto. eapply Val.lessdef_trans. eapply Val.add_lessdef; eauto.
+ rewrite Val.mul_commut; auto.
+ - apply eval_mulimm_base; auto.
+Qed.
+
+Theorem eval_mul: binary_constructor_sound mul Val.mul.
+Proof.
+ red; intros until y.
+ unfold mul; case (mul_match a b); intros; InvEval.
+ rewrite Val.mul_commut. apply eval_mulimm. auto.
+ apply eval_mulimm. auto.
+ TrivialExists.
+Qed.
+
+Theorem eval_mulhs: binary_constructor_sound mulhs Val.mulhs.
+Proof.
+ red; intros. unfold mulhs; destruct Archi.ptr64 eqn:SF.
+- econstructor; split.
+ EvalOp. constructor. EvalOp. constructor. EvalOp. constructor. EvalOp. simpl; eauto.
+ constructor. EvalOp. simpl; eauto. constructor.
+ simpl; eauto. constructor. simpl; eauto. constructor. simpl; eauto.
+ destruct x; simpl; auto. destruct y; simpl; auto.
+ change (Int.ltu (Int.repr 32) Int64.iwordsize') with true; simpl.
+ apply Val.lessdef_same. f_equal.
+ transitivity (Int.repr (Z.shiftr (Int.signed i * Int.signed i0) 32)).
+ unfold Int.mulhs; f_equal. rewrite Zbits.Zshiftr_div_two_p by lia. reflexivity.
+ apply Int.same_bits_eq; intros n N.
+ change Int.zwordsize with 32 in *.
+ assert (N1: 0 <= n < 64) by lia.
+ rewrite Int64.bits_loword by auto.
+ rewrite Int64.bits_shr' by auto.
+ change (Int.unsigned (Int.repr 32)) with 32. change Int64.zwordsize with 64.
+ rewrite zlt_true by lia.
+ rewrite Int.testbit_repr by auto.
+ unfold Int64.mul. rewrite Int64.testbit_repr by (change Int64.zwordsize with 64; lia).
+ transitivity (Z.testbit (Int.signed i * Int.signed i0) (n + 32)).
+ rewrite Z.shiftr_spec by lia. auto.
+ apply Int64.same_bits_eqm. apply Int64.eqm_mult; apply Int64.eqm_unsigned_repr.
+ change Int64.zwordsize with 64; lia.
+- TrivialExists.
+Qed.
+
+Theorem eval_mulhu: binary_constructor_sound mulhu Val.mulhu.
+Proof.
+ red; intros. unfold mulhu; destruct Archi.ptr64 eqn:SF.
+- econstructor; split.
+ EvalOp. constructor. EvalOp. constructor. EvalOp. constructor. EvalOp. simpl; eauto.
+ constructor. EvalOp. simpl; eauto. constructor.
+ simpl; eauto. constructor. simpl; eauto. constructor. simpl; eauto.
+ destruct x; simpl; auto. destruct y; simpl; auto.
+ change (Int.ltu (Int.repr 32) Int64.iwordsize') with true; simpl.
+ apply Val.lessdef_same. f_equal.
+ transitivity (Int.repr (Z.shiftr (Int.unsigned i * Int.unsigned i0) 32)).
+ unfold Int.mulhu; f_equal. rewrite Zbits.Zshiftr_div_two_p by lia. reflexivity.
+ apply Int.same_bits_eq; intros n N.
+ change Int.zwordsize with 32 in *.
+ assert (N1: 0 <= n < 64) by lia.
+ rewrite Int64.bits_loword by auto.
+ rewrite Int64.bits_shru' by auto.
+ change (Int.unsigned (Int.repr 32)) with 32. change Int64.zwordsize with 64.
+ rewrite zlt_true by lia.
+ rewrite Int.testbit_repr by auto.
+ unfold Int64.mul. rewrite Int64.testbit_repr by (change Int64.zwordsize with 64; lia).
+ transitivity (Z.testbit (Int.unsigned i * Int.unsigned i0) (n + 32)).
+ rewrite Z.shiftr_spec by lia. auto.
+ apply Int64.same_bits_eqm. apply Int64.eqm_mult; apply Int64.eqm_unsigned_repr.
+ change Int64.zwordsize with 64; lia.
+- TrivialExists.
+Qed.
+
+Theorem eval_andimm:
+ forall n, unary_constructor_sound (andimm n) (fun x => Val.and x (Vint n)).
+Proof.
+ intros; red; intros until x. unfold andimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros. exists (Vint Int.zero); split. EvalOp.
+ destruct x; simpl; auto. subst n. rewrite Int.and_zero. auto.
+
+ predSpec Int.eq Int.eq_spec n Int.mone.
+ intros. exists x; split; auto.
+ subst. destruct x; simpl; auto. rewrite Int.and_mone; auto.
+
+ case (andimm_match a); intros.
+ - InvEval. TrivialExists. simpl. rewrite Int.and_commut; auto.
+ - InvEval. subst. rewrite Val.and_assoc. simpl. rewrite Int.and_commut. TrivialExists.
+ - InvEval. TrivialExists. simpl; congruence.
+ - TrivialExists.
+Qed.
+
+Theorem eval_and: binary_constructor_sound and Val.and.
+Proof.
+ red; intros until y; unfold and; case (and_match a b); intros; InvEval.
+ - rewrite Val.and_commut. apply eval_andimm; auto.
+ - apply eval_andimm; auto.
+ - (*andn*) TrivialExists; simpl; congruence.
+ - (*andn reverse*) rewrite Val.and_commut. TrivialExists; simpl; congruence.
+ - TrivialExists.
+Qed.
+
+Theorem eval_orimm:
+ forall n, unary_constructor_sound (orimm n) (fun x => Val.or x (Vint n)).
+Proof.
+ intros; red; intros until x. unfold orimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ intros. subst. exists x; split; auto.
+ destruct x; simpl; auto. rewrite Int.or_zero; auto.
+
+ predSpec Int.eq Int.eq_spec n Int.mone.
+ intros. exists (Vint Int.mone); split. EvalOp.
+ destruct x; simpl; auto. subst n. rewrite Int.or_mone. auto.
+
+ destruct (orimm_match a); intros; InvEval.
+ - TrivialExists. simpl. rewrite Int.or_commut; auto.
+ - subst. rewrite Val.or_assoc. simpl. rewrite Int.or_commut. TrivialExists.
+ - InvEval. TrivialExists. simpl; congruence.
+ - TrivialExists.
+Qed.
+
+
+Remark eval_same_expr:
+ forall a1 a2 le v1 v2,
+ same_expr_pure a1 a2 = true ->
+ eval_expr ge sp e m le a1 v1 ->
+ eval_expr ge sp e m le a2 v2 ->
+ a1 = a2 /\ v1 = v2.
+Proof.
+ intros until v2.
+ destruct a1; simpl; try (intros; discriminate).
+ destruct a2; simpl; try (intros; discriminate).
+ case (ident_eq i i0); intros.
+ subst i0. inversion H0. inversion H1. split. auto. congruence.
+ discriminate.
+Qed.
+
+Lemma int_eq_commut: forall x y : int,
+ (Int.eq x y) = (Int.eq y x).
+Proof.
+ intros.
+ predSpec Int.eq Int.eq_spec x y;
+ predSpec Int.eq Int.eq_spec y x;
+ congruence.
+Qed.
+
+Theorem eval_or: binary_constructor_sound or Val.or.
+Proof.
+ unfold or; red; intros.
+ assert (DEFAULT: exists v, eval_expr ge sp e m le (Eop Oor (a:::b:::Enil)) v /\ Val.lessdef (Val.or x y) v) by TrivialExists.
+ assert (ROR: forall v n1 n2,
+ Int.add n1 n2 = Int.iwordsize ->
+ Val.lessdef (Val.or (Val.shl v (Vint n1)) (Val.shru v (Vint n2)))
+ (Val.ror v (Vint n2))).
+ { intros. destruct v; simpl; auto.
+ destruct (Int.ltu n1 Int.iwordsize) eqn:N1; auto.
+ destruct (Int.ltu n2 Int.iwordsize) eqn:N2; auto.
+ simpl. rewrite <- Int.or_ror; auto. }
+
+ destruct (or_match a b); InvEval.
+
+ - rewrite Val.or_commut. apply eval_orimm; auto.
+ - apply eval_orimm; auto.
+ - predSpec Int.eq Int.eq_spec (Int.add n1 n2) Int.iwordsize; auto.
+ destruct (same_expr_pure t1 t2) eqn:?; auto.
+ InvEval. exploit eval_same_expr; eauto. intros [EQ1 EQ2]; subst.
+ exists (Val.ror v0 (Vint n2)); split. EvalOp. apply ROR; auto.
+ - predSpec Int.eq Int.eq_spec (Int.add n1 n2) Int.iwordsize; auto.
+ destruct (same_expr_pure t1 t2) eqn:?; auto.
+ InvEval. exploit eval_same_expr; eauto. intros [EQ1 EQ2]; subst.
+ exists (Val.ror v1 (Vint n2)); split. EvalOp. rewrite Val.or_commut. apply ROR; auto.
+ - (*orn*) TrivialExists; simpl; congruence.
+ - (*orn reversed*) rewrite Val.or_commut. TrivialExists; simpl; congruence.
+ - set (zstop := (int_highest_bit mask)).
+ set (zstart := (Int.unsigned start)).
+ destruct (is_bitfield _ _) eqn:Risbitfield.
+ + destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ * simpl in H6.
+ injection H6.
+ clear H6.
+ intro. subst y. subst x.
+ TrivialExists. simpl. f_equal.
+ unfold insf.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ rewrite Rnmask.
+ simpl.
+ unfold bitfield_mask.
+ subst v0.
+ subst zstart.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ * apply DEFAULT.
+ + apply DEFAULT.
+ - set (zstop := (int_highest_bit mask)).
+ set (zstart := (Int.unsigned start)).
+ destruct (is_bitfield _ _) eqn:Risbitfield.
+ + destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ * simpl in H6.
+ injection H6.
+ clear H6.
+ intro. subst y. subst x.
+ TrivialExists.
+ rewrite Val.or_commut.
+ simpl. f_equal.
+ unfold insf.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ rewrite Rnmask.
+ simpl.
+ unfold bitfield_mask.
+ subst v1.
+ subst zstart.
+ rewrite Int.repr_unsigned.
+ reflexivity.
+ * apply DEFAULT.
+ + apply DEFAULT.
+ - set (zstop := (int_highest_bit mask)).
+ set (zstart := 0).
+ destruct (is_bitfield _ _) eqn:Risbitfield.
+ + destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ * subst y. subst x.
+ TrivialExists. simpl. f_equal.
+ unfold insf.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ rewrite Rnmask.
+ simpl.
+ unfold bitfield_mask.
+ subst zstart.
+ rewrite (Val.or_commut (Val.and v1 _)).
+ rewrite (Val.or_commut (Val.and v1 _)).
+ destruct v0; simpl; trivial.
+ unfold Int.ltu, Int.iwordsize, Int.zwordsize.
+ rewrite Int.unsigned_repr.
+ ** rewrite Int.unsigned_repr.
+ *** simpl.
+ rewrite Int.shl_zero.
+ reflexivity.
+ *** simpl.
+ unfold Int.max_unsigned, Int.modulus.
+ simpl.
+ lia.
+ ** unfold Int.max_unsigned, Int.modulus.
+ simpl.
+ lia.
+ * clear Risbitfield. clear o.
+ clear zstop.
+ set (zstop := (int_highest_bit nmask)).
+ destruct (is_bitfield _ _) eqn:Risbitfield.
+ ++ destruct (and_dec _ _) as [[Rmask Rnmask] | ].
+ ** subst y. subst x.
+ TrivialExists. simpl. f_equal.
+ rewrite Val.or_commut.
+ unfold insf.
+ rewrite Risbitfield.
+ rewrite Rmask.
+ rewrite Rnmask.
+ simpl.
+ unfold bitfield_mask.
+ subst zstart.
+ rewrite (Val.or_commut (Val.and v0 _)).
+ rewrite (Val.or_commut (Val.and v0 _)).
+ destruct v1; simpl; trivial.
+ unfold Int.ltu, Int.iwordsize, Int.zwordsize.
+ rewrite Int.unsigned_repr.
+ *** rewrite Int.unsigned_repr.
+ **** simpl.
+ rewrite Int.shl_zero.
+ reflexivity.
+ **** simpl.
+ unfold Int.max_unsigned, Int.modulus.
+ simpl.
+ lia.
+ *** unfold Int.max_unsigned, Int.modulus.
+ simpl.
+ lia.
+ ** apply DEFAULT.
+ ++ apply DEFAULT.
+ + apply DEFAULT.
+ - apply DEFAULT.
+Qed.
+
+Theorem eval_xorimm:
+ forall n, unary_constructor_sound (xorimm n) (fun x => Val.xor x (Vint n)).
+Proof.
+ intros; red; intros until x. unfold xorimm.
+
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ - intros. exists x; split. auto.
+ destruct x; simpl; auto. subst n. rewrite Int.xor_zero. auto.
+ - predSpec Int.eq Int.eq_spec n Int.mone.
+ -- subst n. intros. rewrite <- Val.not_xor. TrivialExists.
+ -- intros. destruct (xorimm_match a); intros; InvEval.
+ + TrivialExists. simpl. rewrite Int.xor_commut; auto.
+ + subst. rewrite Val.xor_assoc. simpl. rewrite Int.xor_commut.
+ predSpec Int.eq Int.eq_spec (Int.xor n2 n) Int.zero.
+ * exists v1; split; auto. destruct v1; simpl; auto. rewrite H1, Int.xor_zero; auto.
+ * TrivialExists.
+ + TrivialExists.
+Qed.
+
+Theorem eval_xor: binary_constructor_sound xor Val.xor.
+Proof.
+ red; intros until y; unfold xor; case (xor_match a b); intros; InvEval.
+ - rewrite Val.xor_commut. apply eval_xorimm; auto.
+ - apply eval_xorimm; auto.
+ - TrivialExists.
+Qed.
+
+Theorem eval_notint: unary_constructor_sound notint Val.notint.
+Proof.
+ assert (forall v, Val.lessdef (Val.notint (Val.notint v)) v).
+ destruct v; simpl; auto. rewrite Int.not_involutive; auto.
+ unfold notint; red; intros until x; case (notint_match a); intros; InvEval.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - TrivialExists; simpl; congruence.
+ - subst x. exists (Val.and v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.and v1 (Vint n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ - subst x. exists (Val.or v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.or v1 (Vint n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ - subst x. exists (Val.xor v1 v0); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ eassumption. constructor. simpl. reflexivity.
+ - subst x. exists (Val.xor v1 (Vint n)); split; trivial.
+ econstructor. constructor. eassumption. constructor.
+ simpl. reflexivity.
+ (* andn *)
+ - subst x. TrivialExists. simpl.
+ destruct v0; destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int.not_and_or_not.
+ rewrite Int.not_involutive.
+ apply Int.or_commut.
+ - subst x. TrivialExists. simpl.
+ destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int.not_and_or_not.
+ rewrite Int.not_involutive.
+ reflexivity.
+ (* orn *)
+ - subst x. TrivialExists. simpl.
+ destruct v0; destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int.not_or_and_not.
+ rewrite Int.not_involutive.
+ apply Int.and_commut.
+ - subst x. TrivialExists. simpl.
+ destruct v1; simpl; trivial.
+ f_equal. f_equal.
+ rewrite Int.not_or_and_not.
+ rewrite Int.not_involutive.
+ reflexivity.
+ - subst x. exists v1; split; trivial.
+ - TrivialExists.
+ - TrivialExists.
+Qed.
+
+Theorem eval_divs_base:
+ forall le a b x y z,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.divs x y = Some z ->
+ exists v, eval_expr ge sp e m le (divs_base a b) v /\ Val.lessdef z v.
+Proof.
+ intros; unfold divs_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+Theorem eval_mods_base:
+ forall le a b x y z,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.mods x y = Some z ->
+ exists v, eval_expr ge sp e m le (mods_base a b) v /\ Val.lessdef z v.
+Proof.
+ intros; unfold mods_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+Theorem eval_divu_base:
+ forall le a b x y z,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.divu x y = Some z ->
+ exists v, eval_expr ge sp e m le (divu_base a b) v /\ Val.lessdef z v.
+Proof.
+ intros; unfold divu_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+(* For using 64-bit unsigned division for 32-bit
+
+ intros until z.
+ intros Hax Hby Hdiv. unfold divu_base.
+ pose proof (divu_is_divlu x y) as DIVU.
+ destruct (Val.divlu (Val.longofintu x) (Val.longofintu y))
+ as [ ql | ] eqn:Ediv.
+ { TrivialExists.
+ { econstructor. eapply eval_helper_2; eauto.
+ { econstructor. econstructor. eassumption.
+ constructor. simpl. reflexivity. }
+ { econstructor. econstructor. eassumption.
+ constructor. simpl. reflexivity. }
+ { DeclHelper. }
+ { UseHelper. }
+ constructor. }
+ simpl.
+ congruence.
+ }
+ congruence.
+Qed.
+ *)
+
+Theorem eval_modu_base:
+ forall le a b x y z,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ Val.modu x y = Some z ->
+ exists v, eval_expr ge sp e m le (modu_base a b) v /\ Val.lessdef z v.
+Proof.
+ intros; unfold modu_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+(* for using 64-bit unsigned modulo for 32-bit
+
+ intros until z.
+ intros Hax Hby Hmod. unfold modu_base.
+ pose proof (modu_is_modlu x y) as MODU.
+ destruct (Val.modlu (Val.longofintu x) (Val.longofintu y))
+ as [ ql | ] eqn:Emod.
+ { TrivialExists.
+ { econstructor. eapply eval_helper_2; eauto.
+ { econstructor. econstructor. eassumption.
+ constructor. simpl. reflexivity. }
+ { econstructor. econstructor. eassumption.
+ constructor. simpl. reflexivity. }
+ { DeclHelper. }
+ { UseHelper. }
+ constructor. }
+ simpl.
+ congruence.
+ }
+ congruence.
+Qed.
+ *)
+
+Theorem eval_shrximm:
+ forall le a n x z,
+ eval_expr ge sp e m le a x ->
+ Val.shrx x (Vint n) = Some z ->
+ exists v, eval_expr ge sp e m le (shrximm a n) v /\ Val.lessdef z v.
+Proof.
+ intros. unfold shrximm.
+ predSpec Int.eq Int.eq_spec n Int.zero.
+ subst n. exists x; split; auto.
+ destruct x; simpl in H0; try discriminate.
+ destruct (Int.ltu Int.zero (Int.repr 31)); inv H0.
+ replace (Int.shrx i Int.zero) with i. auto.
+ unfold Int.shrx, Int.divs. rewrite Int.shl_zero.
+ change (Int.signed Int.one) with 1. rewrite Z.quot_1_r. rewrite Int.repr_signed; auto.
+ econstructor; split. EvalOp.
+ simpl. rewrite H0. simpl. reflexivity. auto.
+Qed.
+
+Theorem eval_shl: binary_constructor_sound shl Val.shl.
+Proof.
+ red; intros until y; unfold shl; case (shl_match b); intros.
+ InvEval. apply eval_shlimm; auto.
+ TrivialExists.
+Qed.
+
+Theorem eval_shr: binary_constructor_sound shr Val.shr.
+Proof.
+ red; intros until y; unfold shr; case (shr_match b); intros.
+ InvEval. apply eval_shrimm; auto.
+ TrivialExists.
+Qed.
+
+Theorem eval_shru: binary_constructor_sound shru Val.shru.
+Proof.
+ red; intros until y; unfold shru; case (shru_match b); intros.
+ InvEval. apply eval_shruimm; auto.
+ TrivialExists.
+Qed.
+
+Theorem eval_negf: unary_constructor_sound negf Val.negf.
+Proof.
+ red; intros. TrivialExists.
+Qed.
+
+Theorem eval_absf: unary_constructor_sound absf Val.absf.
+Proof.
+ red; intros. TrivialExists.
+Qed.
+
+Theorem eval_addf: binary_constructor_sound addf Val.addf.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Theorem eval_subf: binary_constructor_sound subf Val.subf.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Theorem eval_mulf: binary_constructor_sound mulf Val.mulf.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Theorem eval_negfs: unary_constructor_sound negfs Val.negfs.
+Proof.
+ red; intros. TrivialExists.
+Qed.
+
+Theorem eval_absfs: unary_constructor_sound absfs Val.absfs.
+Proof.
+ red; intros. TrivialExists.
+Qed.
+
+Theorem eval_addfs: binary_constructor_sound addfs Val.addfs.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Theorem eval_subfs: binary_constructor_sound subfs Val.subfs.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Theorem eval_mulfs: binary_constructor_sound mulfs Val.mulfs.
+Proof.
+ red; intros; TrivialExists.
+Qed.
+
+Section COMP_IMM.
+
+Variable default: comparison -> int -> condition.
+Variable intsem: comparison -> int -> int -> bool.
+Variable sem: comparison -> val -> val -> val.
+
+Hypothesis sem_int: forall c x y, sem c (Vint x) (Vint y) = Val.of_bool (intsem c x y).
+Hypothesis sem_undef: forall c v, sem c Vundef v = Vundef.
+Hypothesis sem_eq: forall x y, sem Ceq (Vint x) (Vint y) = Val.of_bool (Int.eq x y).
+Hypothesis sem_ne: forall x y, sem Cne (Vint x) (Vint y) = Val.of_bool (negb (Int.eq x y)).
+Hypothesis sem_default: forall c v n, sem c v (Vint n) = Val.of_optbool (eval_condition (default c n) (v :: nil) m).
+
+Lemma eval_compimm:
+ forall le c a n2 x,
+ eval_expr ge sp e m le a x ->
+ exists v, eval_expr ge sp e m le (compimm default intsem c a n2) v
+ /\ Val.lessdef (sem c x (Vint n2)) v.
+Proof.
+ intros until x.
+ unfold compimm; case (compimm_match c a); intros.
+(* constant *)
+ - InvEval. rewrite sem_int. TrivialExists. simpl. destruct (intsem c0 n1 n2); auto.
+(* eq cmp *)
+ - InvEval. inv H. simpl in H5. inv H5.
+ destruct (Int.eq_dec n2 Int.zero).
+ + subst n2. TrivialExists.
+ simpl. rewrite eval_negate_condition.
+ destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; simpl; rewrite sem_eq; auto.
+ rewrite sem_undef; auto.
+ + destruct (Int.eq_dec n2 Int.one). subst n2. TrivialExists.
+ simpl. destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; simpl; rewrite sem_eq; auto.
+ rewrite sem_undef; auto.
+ exists (Vint Int.zero); split. EvalOp.
+ destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; rewrite sem_eq; rewrite Int.eq_false; auto.
+ rewrite sem_undef; auto.
+(* ne cmp *)
+ - InvEval. inv H. simpl in H5. inv H5.
+ destruct (Int.eq_dec n2 Int.zero).
+ + subst n2. TrivialExists.
+ simpl. destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; simpl; rewrite sem_ne; auto.
+ rewrite sem_undef; auto.
+ + destruct (Int.eq_dec n2 Int.one). subst n2. TrivialExists.
+ simpl. rewrite eval_negate_condition. destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; simpl; rewrite sem_ne; auto.
+ rewrite sem_undef; auto.
+ exists (Vint Int.one); split. EvalOp.
+ destruct (eval_condition c0 vl m); simpl.
+ unfold Vtrue, Vfalse. destruct b; rewrite sem_ne; rewrite Int.eq_false; auto.
+ rewrite sem_undef; auto.
+(* default *)
+ - TrivialExists. simpl. rewrite sem_default. auto.
+Qed.
+
+Hypothesis sem_swap:
+ forall c x y, sem (swap_comparison c) x y = sem c y x.
+
+Lemma eval_compimm_swap:
+ forall le c a n2 x,
+ eval_expr ge sp e m le a x ->
+ exists v, eval_expr ge sp e m le (compimm default intsem (swap_comparison c) a n2) v
+ /\ Val.lessdef (sem c (Vint n2) x) v.
+Proof.
+ intros. rewrite <- sem_swap. eapply eval_compimm; eauto.
+Qed.
+
+End COMP_IMM.
+
+Theorem eval_comp:
+ forall c, binary_constructor_sound (comp c) (Val.cmp c).
+Proof.
+ intros; red; intros until y. unfold comp; case (comp_match a b); intros; InvEval.
+ eapply eval_compimm_swap; eauto.
+ intros. unfold Val.cmp. rewrite Val.swap_cmp_bool; auto.
+ eapply eval_compimm; eauto.
+ TrivialExists.
+Qed.
+
+Theorem eval_compu:
+ forall c, binary_constructor_sound (compu c) (Val.cmpu (Mem.valid_pointer m) c).
+Proof.
+ intros; red; intros until y. unfold compu; case (compu_match a b); intros; InvEval.
+ eapply eval_compimm_swap; eauto.
+ intros. unfold Val.cmpu. rewrite Val.swap_cmpu_bool; auto.
+ eapply eval_compimm; eauto.
+ TrivialExists.
+Qed.
+
+Theorem eval_compf:
+ forall c, binary_constructor_sound (compf c) (Val.cmpf c).
+Proof.
+ intros; red; intros. unfold compf. TrivialExists.
+Qed.
+
+Theorem eval_compfs:
+ forall c, binary_constructor_sound (compfs c) (Val.cmpfs c).
+Proof.
+ intros; red; intros. unfold compfs. TrivialExists.
+Qed.
+
+Theorem eval_cast8signed: unary_constructor_sound cast8signed (Val.sign_ext 8).
+Proof.
+ red; intros until x. unfold cast8signed. case (cast8signed_match a); intros; InvEval.
+ TrivialExists.
+ TrivialExists.
+Qed.
+
+Theorem eval_cast8unsigned: unary_constructor_sound cast8unsigned (Val.zero_ext 8).
+Proof.
+ red; intros until x. unfold cast8unsigned.
+ rewrite Val.zero_ext_and. apply eval_andimm. compute; auto. discriminate.
+Qed.
+
+Theorem eval_cast16signed: unary_constructor_sound cast16signed (Val.sign_ext 16).
+Proof.
+ red; intros until x. unfold cast16signed. case (cast16signed_match a); intros; InvEval.
+ TrivialExists.
+ TrivialExists.
+Qed.
+
+Theorem eval_cast16unsigned: unary_constructor_sound cast16unsigned (Val.zero_ext 16).
+Proof.
+ red; intros until x. unfold cast8unsigned.
+ rewrite Val.zero_ext_and. apply eval_andimm. compute; auto. discriminate.
+Qed.
+
+Theorem eval_intoffloat:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.intoffloat x = Some y ->
+ exists v, eval_expr ge sp e m le (intoffloat a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold intoffloat. TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_intuoffloat:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.intuoffloat x = Some y ->
+ exists v, eval_expr ge sp e m le (intuoffloat a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold intuoffloat. TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_floatofintu:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.floatofintu x = Some y ->
+ exists v, eval_expr ge sp e m le (floatofintu a) v /\ Val.lessdef y v.
+Proof.
+ intros.
+ unfold Val.floatofintu in *.
+ unfold floatofintu.
+ destruct (floatofintu_match a).
+ - InvEval.
+ TrivialExists.
+ - InvEval.
+ TrivialExists.
+ constructor. econstructor. constructor. eassumption. constructor.
+ simpl. f_equal. constructor.
+ simpl.
+ destruct x; simpl; trivial; try discriminate.
+ f_equal.
+ inv H0.
+ f_equal.
+ rewrite Float.of_intu_of_longu.
+ reflexivity.
+Qed.
+
+Theorem eval_floatofint:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.floatofint x = Some y ->
+ exists v, eval_expr ge sp e m le (floatofint a) v /\ Val.lessdef y v.
+Proof.
+ intros.
+ unfold floatofint.
+ destruct (floatofint_match a).
+ - InvEval.
+ TrivialExists.
+ - InvEval.
+ TrivialExists.
+ constructor. econstructor. constructor. eassumption. constructor.
+ simpl. f_equal. constructor.
+ simpl.
+ destruct x; simpl; trivial; try discriminate.
+ f_equal.
+ inv H0.
+ f_equal.
+ rewrite Float.of_int_of_long.
+ reflexivity.
+Qed.
+
+Theorem eval_intofsingle:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.intofsingle x = Some y ->
+ exists v, eval_expr ge sp e m le (intofsingle a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold intofsingle. TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_singleofint:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.singleofint x = Some y ->
+ exists v, eval_expr ge sp e m le (singleofint a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold singleofint; TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_intuofsingle:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.intuofsingle x = Some y ->
+ exists v, eval_expr ge sp e m le (intuofsingle a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold intuofsingle. TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_singleofintu:
+ forall le a x y,
+ eval_expr ge sp e m le a x ->
+ Val.singleofintu x = Some y ->
+ exists v, eval_expr ge sp e m le (singleofintu a) v /\ Val.lessdef y v.
+Proof.
+ intros; unfold intuofsingle. TrivialExists.
+ simpl. rewrite H0. reflexivity.
+Qed.
+
+Theorem eval_singleoffloat: unary_constructor_sound singleoffloat Val.singleoffloat.
+Proof.
+ red; intros. unfold singleoffloat. TrivialExists.
+Qed.
+
+Theorem eval_floatofsingle: unary_constructor_sound floatofsingle Val.floatofsingle.
+Proof.
+ red; intros. unfold floatofsingle. TrivialExists.
+Qed.
+
+Theorem eval_addressing:
+ forall le chunk a v b ofs,
+ eval_expr ge sp e m le a v ->
+ v = Vptr b ofs ->
+ match addressing chunk a with (mode, args) =>
+ exists vl,
+ eval_exprlist ge sp e m le args vl /\
+ eval_addressing ge sp mode vl = Some v
+ end.
+Proof.
+ intros until v. unfold addressing; case (addressing_match a); intros; InvEval.
+ - exists (@nil val); split. eauto with evalexpr. simpl. auto.
+ - destruct (orb _ _).
+ + exists (Vptr b ofs0 :: nil); split.
+ constructor. EvalOp. simpl. congruence. constructor. simpl. rewrite Ptrofs.add_zero. congruence.
+ + exists (@nil val); split. constructor. simpl; auto.
+ - exists (v1 :: nil); split. eauto with evalexpr. simpl.
+ destruct v1; simpl in H; try discriminate.
+ - exists (v1 :: nil); split. eauto with evalexpr. simpl.
+ destruct v1; simpl in H; try discriminate. destruct Archi.ptr64 eqn:SF; inv H.
+ simpl. auto.
+ - destruct (Compopts.optim_xsaddr tt).
+ + destruct (Z.eq_dec _ _).
+ * exists (v1 :: v2 :: nil); split.
+ repeat (constructor; auto). simpl. rewrite Int.repr_unsigned. destruct v2; simpl in *; congruence.
+ * exists (v1 :: v0 :: nil); split.
+ repeat (constructor; auto). econstructor.
+ repeat (constructor; auto). eassumption. simpl. congruence.
+ simpl. congruence.
+ + exists (v1 :: v0 :: nil); split.
+ repeat (constructor; auto). econstructor.
+ repeat (constructor; auto). eassumption. simpl. congruence.
+ simpl. congruence.
+ - unfold addxl in *.
+ destruct (Compopts.optim_xsaddr tt).
+ + unfold int_of_shift1_4 in *.
+ destruct (Z.eq_dec _ _).
+ * exists (v0 :: v1 :: nil); split.
+ repeat (constructor; auto). simpl.
+ congruence.
+ * eexists; split.
+ repeat (constructor; auto). eassumption.
+ econstructor.
+ repeat (constructor; auto). eassumption. simpl.
+ reflexivity.
+ simpl. congruence.
+ + eexists; split.
+ repeat (constructor; auto). eassumption.
+ econstructor.
+ repeat (constructor; auto). eassumption. simpl.
+ reflexivity.
+ simpl. unfold int_of_shift1_4 in *. congruence.
+ - exists (v1 :: v0 :: nil); split. repeat (constructor; auto). simpl. congruence.
+ - exists (v :: nil); split. eauto with evalexpr. subst. simpl. rewrite Ptrofs.add_zero; auto.
+Qed.
+
+Theorem eval_builtin_arg:
+ forall a v,
+ eval_expr ge sp e m nil a v ->
+ CminorSel.eval_builtin_arg ge sp e m (builtin_arg a) v.
+Proof.
+ intros until v. unfold builtin_arg; case (builtin_arg_match a); intros.
+- InvEval. constructor.
+- InvEval. constructor.
+- InvEval. constructor.
+- InvEval. simpl in H5. inv H5. constructor.
+- InvEval. subst v. constructor; auto.
+- inv H. InvEval. simpl in H6; inv H6. constructor; auto.
+- destruct Archi.ptr64 eqn:SF.
++ constructor; auto.
++ InvEval. replace v with (if Archi.ptr64 then Val.addl v1 (Vint n) else Val.add v1 (Vint n)).
+ repeat constructor; auto.
+ rewrite SF; auto.
+- destruct Archi.ptr64 eqn:SF.
++ InvEval. replace v with (if Archi.ptr64 then Val.addl v1 (Vlong n) else Val.add v1 (Vlong n)).
+ repeat constructor; auto.
++ constructor; auto.
+- constructor; auto.
+Qed.
+
+(* ternary *)
+(* does not work due to possible nondeterminism
+Lemma cond_to_condition0_correct :
+ forall cond : condition,
+ forall al : exprlist,
+ match (cond_to_condition0 cond al) with
+ | None => True
+ | Some(cond0, e1) =>
+ forall le vl v1,
+ eval_expr ge sp e m le e1 v1 ->
+ eval_exprlist ge sp e m le al vl ->
+ (eval_condition0 cond0 v1 m) = (eval_condition cond vl m)
+ end.
+Proof.
+ intros.
+ unfold cond_to_condition0.
+ case (cond_to_condition0_match cond al); trivial.
+ {
+ intros.
+ destruct (Int.eq_dec _ _); trivial.
+ intros until v1.
+ intros He1 Hel.
+ InvEval.
+ simpl.
+ f_equal.
+ eapply eval_expr_determ. eassumption.
+ }
+Qed.
+*)
+
+Lemma eval_neg_condition0:
+ forall cond0: condition0,
+ forall v1: val,
+ forall m: mem,
+ (eval_condition0 (negate_condition0 cond0) v1 m) =
+ option_map negb (eval_condition0 cond0 v1 m).
+Proof.
+ intros.
+ destruct cond0; simpl;
+ try rewrite Val.negate_cmp_bool;
+ try rewrite Val.negate_cmpu_bool;
+ try rewrite Val.negate_cmpl_bool;
+ try rewrite Val.negate_cmplu_bool;
+ reflexivity.
+Qed.
+
+Lemma select_neg:
+ forall a b c,
+ Val.select (option_map negb a) b c =
+ Val.select a c b.
+Proof.
+ destruct a; simpl; trivial.
+ destruct b; simpl; trivial.
+Qed.
+
+Lemma eval_select0:
+ forall le ty cond0 ac vc a1 v1 a2 v2,
+ eval_expr ge sp e m le ac vc ->
+ eval_expr ge sp e m le a1 v1 ->
+ eval_expr ge sp e m le a2 v2 ->
+ exists v,
+ eval_expr ge sp e m le (select0 ty cond0 a1 a2 ac) v
+ /\ Val.lessdef (Val.select (eval_condition0 cond0 vc m) v1 v2 ty) v.
+Proof.
+ intros.
+ unfold select0.
+ destruct (select0_match ty cond0 a1 a2 ac).
+ all: InvEval; econstructor; split;
+ try repeat (try econstructor; try eassumption).
+ all: rewrite eval_neg_condition0; rewrite select_neg; constructor.
+Qed.
+
+Lemma bool_cond0_ne:
+ forall ob : option bool,
+ forall m,
+ (eval_condition0 (Ccomp0 Cne) (Val.of_optbool ob) m) = ob.
+Proof.
+ destruct ob; simpl; trivial.
+ intro.
+ destruct b; reflexivity.
+Qed.
+
+Lemma eval_condition_ccomp_swap :
+ forall c x y m,
+ eval_condition (Ccomp (swap_comparison c)) (x :: y :: nil) m=
+ eval_condition (Ccomp c) (y :: x :: nil) m.
+Proof.
+ intros; unfold eval_condition;
+ apply Val.swap_cmp_bool.
+Qed.
+
+Lemma eval_condition_ccompu_swap :
+ forall c x y m,
+ eval_condition (Ccompu (swap_comparison c)) (x :: y :: nil) m=
+ eval_condition (Ccompu c) (y :: x :: nil) m.
+Proof.
+ intros; unfold eval_condition;
+ apply Val.swap_cmpu_bool.
+Qed.
+
+Lemma eval_condition_ccompl_swap :
+ forall c x y m,
+ eval_condition (Ccompl (swap_comparison c)) (x :: y :: nil) m=
+ eval_condition (Ccompl c) (y :: x :: nil) m.
+Proof.
+ intros; unfold eval_condition;
+ apply Val.swap_cmpl_bool.
+Qed.
+
+Lemma eval_condition_ccomplu_swap :
+ forall c x y m,
+ eval_condition (Ccomplu (swap_comparison c)) (x :: y :: nil) m=
+ eval_condition (Ccomplu c) (y :: x :: nil) m.
+Proof.
+ intros; unfold eval_condition;
+ apply Val.swap_cmplu_bool.
+Qed.
+
+Lemma int_ltu_zero : forall i, Int.ltu i Int.zero = false.
+Proof.
+ intro.
+ unfold Int.ltu.
+ apply zlt_false.
+ rewrite Int.unsigned_zero.
+ pose proof (Int.unsigned_range i).
+ lia.
+Qed.
+
+Lemma cmpu_bool_Clt : forall pred v0 b,
+ Val.cmpu_bool pred Clt v0 (Vint Int.zero) = Some b -> b = false.
+Proof.
+ intros until b. intro CMP.
+ destruct v0; cbn in CMP; try discriminate.
+ inv CMP.
+ apply int_ltu_zero.
+Qed.
+
+Lemma cmpu_bool_Cge : forall pred v0 b,
+ Val.cmpu_bool pred Cge v0 (Vint Int.zero) = Some b -> b = true.
+Proof.
+ intros until b. intro CMP.
+ destruct v0; cbn in CMP; try discriminate.
+ inv CMP.
+ rewrite int_ltu_zero.
+ reflexivity.
+Qed.
+
+Lemma int64_ltu_zero : forall i, Int64.ltu i Int64.zero = false.
+Proof.
+ intro.
+ unfold Int64.ltu.
+ apply zlt_false.
+ rewrite Int64.unsigned_zero.
+ pose proof (Int64.unsigned_range i).
+ lia.
+Qed.
+
+Lemma cmplu_bool_Clt : forall pred v0 b,
+ Val.cmplu_bool pred Clt v0 (Vlong Int64.zero) = Some b -> b = false.
+Proof.
+ intros until b. intro CMP.
+ destruct v0; cbn in CMP; try discriminate.
+ { inv CMP.
+ apply int64_ltu_zero.
+ }
+ repeat rewrite if_same in CMP.
+ discriminate.
+Qed.
+
+Lemma cmplu_bool_Cge : forall pred v0 b,
+ Val.cmplu_bool pred Cge v0 (Vlong Int64.zero) = Some b -> b = true.
+Proof.
+ intros until b. intro CMP.
+ destruct v0; cbn in CMP; try discriminate.
+ { inv CMP.
+ rewrite int64_ltu_zero.
+ reflexivity.
+ }
+ repeat rewrite if_same in CMP.
+ discriminate.
+Qed.
+
+Theorem eval_select:
+ forall le ty cond al vl a1 v1 a2 v2 a b,
+ select ty cond al a1 a2 = Some a ->
+ eval_exprlist ge sp e m le al vl ->
+ eval_expr ge sp e m le a1 v1 ->
+ eval_expr ge sp e m le a2 v2 ->
+ eval_condition cond vl m = Some b ->
+ exists v,
+ eval_expr ge sp e m le a v
+ /\ Val.lessdef (Val.select (Some b) v1 v2 ty) v.
+Proof.
+ unfold select.
+ intros until b.
+ intro Hop; injection Hop; clear Hop; intro; subst a.
+ intros HeL He1 He2 HeC.
+ destruct same_expr_pure eqn:SAME.
+ {
+ destruct (eval_same_expr a1 a2 le v1 v2 SAME He1 He2) as [EQ1 EQ2].
+ subst a2. subst v2.
+ exists v1; split; trivial.
+ cbn.
+ rewrite if_same.
+ apply Val.lessdef_normalize.
+ }
+ unfold cond_to_condition0.
+ destruct (cond_to_condition0_match cond al).
+ {
+ InvEval.
+ rewrite <- HeC.
+ destruct (Int.eq_dec x Int.zero).
+ { subst x.
+ simpl.
+ change (Val.cmp_bool c v0 (Vint Int.zero))
+ with (eval_condition0 (Ccomp0 c) v0 m).
+ eapply eval_select0; eassumption.
+ }
+ simpl.
+ erewrite <- (bool_cond0_ne (Val.cmp_bool c v0 (Vint x))).
+ eapply eval_select0; repeat (try econstructor; try eassumption).
+ }
+ {
+ InvEval.
+ rewrite <- HeC.
+ destruct (Int.eq_dec x Int.zero).
+ { subst x.
+ simpl.
+ change (Val.cmpu_bool (Mem.valid_pointer m) c v0 (Vint Int.zero))
+ with (eval_condition0 (Ccompu0 c) v0 m).
+ destruct c.
+ all: try (eapply eval_select0; eassumption).
+ all: cbn.
+ all: cbn in HeC.
+ {
+ destruct (Val.cmpu_bool (Mem.valid_pointer m) Clt v0 (Vint Int.zero)) eqn:CMP; try discriminate.
+ inv HeC.
+ rewrite (cmpu_bool_Clt (Mem.valid_pointer m) v0 b CMP).
+ cbn.
+ exists v2.
+ split; auto using Val.lessdef_normalize.
+ }
+ {
+ destruct (Val.cmpu_bool (Mem.valid_pointer m) Cge v0 (Vint Int.zero)) eqn:CMP; try discriminate.
+ inv HeC.
+ rewrite (cmpu_bool_Cge (Mem.valid_pointer m) v0 b CMP).
+ cbn.
+ exists v1.
+ split; auto using Val.lessdef_normalize.
+ }
+ }
+ simpl.
+ erewrite <- (bool_cond0_ne (Val.cmpu_bool (Mem.valid_pointer m) c v0 (Vint x))).
+ eapply eval_select0; repeat (try econstructor; try eassumption).
+ }
+ {
+ InvEval.
+ rewrite <- HeC.
+ destruct (Int64.eq_dec x Int64.zero).
+ { subst x.
+ simpl.
+ change (Val.cmpl_bool c v0 (Vlong Int64.zero))
+ with (eval_condition0 (Ccompl0 c) v0 m).
+ eapply eval_select0; eassumption.
+ }
+ simpl.
+ erewrite <- (bool_cond0_ne (Val.cmpl_bool c v0 (Vlong x))).
+ eapply eval_select0; repeat (try econstructor; try eassumption).
+ }
+ {
+ InvEval.
+ rewrite <- HeC.
+ destruct (Int64.eq_dec x Int64.zero).
+ { subst x.
+ simpl.
+ change (Val.cmplu_bool (Mem.valid_pointer m) c v0 (Vlong Int64.zero))
+ with (eval_condition0 (Ccomplu0 c) v0 m).
+ destruct c.
+ all: try (eapply eval_select0; eassumption).
+ all: cbn.
+ all: cbn in HeC.
+ {
+ destruct (Val.cmplu_bool (Mem.valid_pointer m) Clt v0 (Vlong Int64.zero)) eqn:CMP; try discriminate.
+ inv HeC.
+ rewrite (cmplu_bool_Clt (Mem.valid_pointer m) v0 b CMP).
+ cbn.
+ exists v2.
+ split; auto using Val.lessdef_normalize.
+ }
+ {
+ destruct (Val.cmplu_bool (Mem.valid_pointer m) Cge v0 (Vlong Int64.zero)) eqn:CMP; try discriminate.
+ inv HeC.
+ rewrite (cmplu_bool_Cge (Mem.valid_pointer m) v0 b CMP).
+ cbn.
+ exists v1.
+ split; auto using Val.lessdef_normalize.
+ }
+ }
+ simpl.
+ erewrite <- (bool_cond0_ne (Val.cmplu_bool (Mem.valid_pointer m) c v0 (Vlong x))).
+ eapply eval_select0; repeat (try econstructor; try eassumption).
+ }
+ erewrite <- (bool_cond0_ne (Some b)).
+ eapply eval_select0; repeat (try econstructor; try eassumption).
+ rewrite <- HeC.
+ simpl.
+ reflexivity.
+Qed.
+
+(* floating-point division *)
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+
+Lemma eval_divfs_base1:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base1 b) v /\ Val.lessdef (ExtValues.invfs y) v.
+Proof.
+ intros; unfold divfs_base1.
+ econstructor; split.
+ repeat (try econstructor; try eassumption).
+ trivial.
+Qed.
+
+Lemma eval_divfs_baseX:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_baseX a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ econstructor; split. eapply eval_helper_2; eauto. DeclHelper. UseHelper. auto.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ destruct (divfs_base_match _).
+ - destruct (Float32.eq_dec _ _).
+ + exists (Val.divfs x y).
+ split; trivial. repeat (try econstructor; try eassumption).
+ simpl. InvEval. reflexivity.
+ + apply eval_divfs_baseX; assumption.
+ - apply eval_divfs_baseX; assumption.
+Qed.
+
+(** Platform-specific known builtins *)
+
+Lemma eval_fma:
+ forall al a vl v le,
+ gen_fma al = Some a ->
+ eval_exprlist ge sp e m le al vl ->
+ platform_builtin_sem BI_fma vl = Some v ->
+ exists v', eval_expr ge sp e m le a v' /\ Val.lessdef v v'.
+Proof.
+ unfold gen_fma.
+ intros until le.
+ intro Heval.
+ destruct (gen_fma_match _) in *; try discriminate.
+ all: inversion Heval; subst a; clear Heval; intro; InvEval.
+ - subst v1.
+ TrivialExists.
+ destruct v0; simpl; trivial;
+ destruct v2; simpl; trivial;
+ destruct v3; simpl; trivial.
+ - intro Heval.
+ simpl in Heval.
+ inv Heval.
+ TrivialExists.
+ destruct v0; simpl; trivial;
+ destruct v1; simpl; trivial;
+ destruct v2; simpl; trivial.
+Qed.
+
+Lemma eval_fmaf:
+ forall al a vl v le,
+ gen_fmaf al = Some a ->
+ eval_exprlist ge sp e m le al vl ->
+ platform_builtin_sem BI_fmaf vl = Some v ->
+ exists v', eval_expr ge sp e m le a v' /\ Val.lessdef v v'.
+Proof.
+ unfold gen_fmaf.
+ intros until le.
+ intro Heval.
+ destruct (gen_fmaf_match _) in *; try discriminate.
+ all: inversion Heval; subst a; clear Heval; intro; InvEval.
+ - subst v1.
+ TrivialExists.
+ destruct v0; simpl; trivial;
+ destruct v2; simpl; trivial;
+ destruct v3; simpl; trivial.
+ - intro Heval.
+ simpl in Heval.
+ inv Heval.
+ TrivialExists.
+ destruct v0; simpl; trivial;
+ destruct v1; simpl; trivial;
+ destruct v2; simpl; trivial.
+Qed.
+
+Theorem eval_platform_builtin:
+ forall bf al a vl v le,
+ platform_builtin bf al = Some a ->
+ eval_exprlist ge sp e m le al vl ->
+ platform_builtin_sem bf vl = Some v ->
+ exists v', eval_expr ge sp e m le a v' /\ Val.lessdef v v'.
+Proof.
+ destruct bf; intros until le; intro Heval.
+ all: try (inversion Heval; subst a; clear Heval;
+ exists v; split; trivial;
+ repeat (try econstructor; try eassumption)).
+ - apply eval_fma; assumption.
+ - apply eval_fmaf; assumption.
+Qed.
+
+End CMCONSTR.
diff --git a/kvx/Stacklayout.v b/kvx/Stacklayout.v
new file mode 100644
index 00000000..05cfa1d7
--- /dev/null
+++ b/kvx/Stacklayout.v
@@ -0,0 +1,151 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Machine- and ABI-dependent layout information for activation records. *)
+
+Require Import Coqlib.
+Require Import AST Memory Separation.
+Require Import Bounds.
+Require Import Lia.
+
+Local Open Scope sep_scope.
+
+(** The general shape of activation records is as follows,
+ from bottom (lowest offsets) to top:
+- Space for outgoing arguments to function calls.
+- Back link to parent frame
+- Return address
+- Saved values of callee-save registers used by the function.
+- Local stack slots.
+- Space for the stack-allocated data declared in Cminor.
+
+The stack pointer is kept 16-aligned.
+*)
+
+Definition fe_ofs_arg := 0.
+
+Definition make_env (b: bounds) : frame_env :=
+ let w := if Archi.ptr64 then 8 else 4 in
+ let olink := align (4 * b.(bound_outgoing)) w in (* back link *)
+ let oretaddr := olink + w in (* return address *)
+ let ocs := oretaddr + w in (* callee-saves *)
+ let ol := align (size_callee_save_area b ocs) 8 in (* locals *)
+ let ostkdata := align (ol + 4 * b.(bound_local)) 8 in (* stack data *)
+ let sz := align (ostkdata + b.(bound_stack_data)) 16 in
+ {| fe_size := sz;
+ fe_ofs_link := olink;
+ fe_ofs_retaddr := oretaddr;
+ fe_ofs_local := ol;
+ fe_ofs_callee_save := ocs;
+ fe_stack_data := ostkdata;
+ fe_used_callee_save := b.(used_callee_save) |}.
+
+Lemma frame_env_separated:
+ forall b sp m P,
+ let fe := make_env b in
+ m |= range sp 0 (fe_stack_data fe) ** range sp (fe_stack_data fe + bound_stack_data b) (fe_size fe) ** P ->
+ m |= range sp (fe_ofs_local fe) (fe_ofs_local fe + 4 * bound_local b)
+ ** range sp fe_ofs_arg (fe_ofs_arg + 4 * bound_outgoing b)
+ ** range sp (fe_ofs_link fe) (fe_ofs_link fe + size_chunk Mptr)
+ ** range sp (fe_ofs_retaddr fe) (fe_ofs_retaddr fe + size_chunk Mptr)
+ ** range sp (fe_ofs_callee_save fe) (size_callee_save_area b (fe_ofs_callee_save fe))
+ ** P.
+Proof.
+Local Opaque Z.add Z.mul sepconj range.
+ intros; cbn.
+ set (w := if Archi.ptr64 then 8 else 4).
+ set (olink := align (4 * b.(bound_outgoing)) w).
+ set (oretaddr := olink + w).
+ set (ocs := oretaddr + w).
+ set (ol := align (size_callee_save_area b ocs) 8).
+ set (ostkdata := align (ol + 4 * b.(bound_local)) 8).
+ replace (size_chunk Mptr) with w by (rewrite size_chunk_Mptr; auto).
+ assert (0 < w) by (unfold w; destruct Archi.ptr64; lia).
+ generalize b.(bound_local_pos) b.(bound_outgoing_pos) b.(bound_stack_data_pos); intros.
+ assert (0 <= 4 * b.(bound_outgoing)) by lia.
+ assert (4 * b.(bound_outgoing) <= olink) by (apply align_le; lia).
+ assert (olink + w <= oretaddr) by (unfold oretaddr; lia).
+ assert (oretaddr + w <= ocs) by (unfold ocs; lia).
+ assert (ocs <= size_callee_save_area b ocs) by (apply size_callee_save_area_incr).
+ assert (size_callee_save_area b ocs <= ol) by (apply align_le; lia).
+ assert (ol + 4 * b.(bound_local) <= ostkdata) by (apply align_le; lia).
+(* Reorder as:
+ outgoing
+ back link
+ retaddr
+ callee-save
+ local *)
+ rewrite sep_swap12.
+ rewrite sep_swap23.
+ rewrite sep_swap34.
+ rewrite sep_swap45.
+(* Apply range_split and range_split2 repeatedly *)
+ unfold fe_ofs_arg.
+ apply range_split_2. fold olink; lia. lia.
+ apply range_split. lia.
+ apply range_split. lia.
+ apply range_split_2. fold ol. lia. lia.
+ apply range_drop_right with ostkdata. lia.
+ eapply sep_drop2. eexact H.
+Qed.
+
+Lemma frame_env_range:
+ forall b,
+ let fe := make_env b in
+ 0 <= fe_stack_data fe /\ fe_stack_data fe + bound_stack_data b <= fe_size fe.
+Proof.
+ intros; cbn.
+ set (w := if Archi.ptr64 then 8 else 4).
+ set (olink := align (4 * b.(bound_outgoing)) w).
+ set (oretaddr := olink + w).
+ set (ocs := oretaddr + w).
+ set (ol := align (size_callee_save_area b ocs) 8).
+ set (ostkdata := align (ol + 4 * b.(bound_local)) 8).
+ assert (0 < w) by (unfold w; destruct Archi.ptr64; lia).
+ generalize b.(bound_local_pos) b.(bound_outgoing_pos) b.(bound_stack_data_pos); intros.
+ assert (0 <= 4 * b.(bound_outgoing)) by lia.
+ assert (4 * b.(bound_outgoing) <= olink) by (apply align_le; lia).
+ assert (olink + w <= oretaddr) by (unfold oretaddr; lia).
+ assert (oretaddr + w <= ocs) by (unfold ocs; lia).
+ assert (ocs <= size_callee_save_area b ocs) by (apply size_callee_save_area_incr).
+ assert (size_callee_save_area b ocs <= ol) by (apply align_le; lia).
+ assert (ol + 4 * b.(bound_local) <= ostkdata) by (apply align_le; lia).
+ split. lia. apply align_le. lia.
+Qed.
+
+Lemma frame_env_aligned:
+ forall b,
+ let fe := make_env b in
+ (8 | fe_ofs_arg)
+ /\ (8 | fe_ofs_local fe)
+ /\ (8 | fe_stack_data fe)
+ /\ (align_chunk Mptr | fe_ofs_link fe)
+ /\ (align_chunk Mptr | fe_ofs_retaddr fe).
+Proof.
+ intros; cbn.
+ set (w := if Archi.ptr64 then 8 else 4).
+ set (olink := align (4 * b.(bound_outgoing)) w).
+ set (oretaddr := olink + w).
+ set (ocs := oretaddr + w).
+ set (ol := align (size_callee_save_area b ocs) 8).
+ set (ostkdata := align (ol + 4 * b.(bound_local)) 8).
+ assert (0 < w) by (unfold w; destruct Archi.ptr64; lia).
+ replace (align_chunk Mptr) with w by (rewrite align_chunk_Mptr; auto).
+ split. apply Z.divide_0_r.
+ split. apply align_divides; lia.
+ split. apply align_divides; lia.
+ split. apply align_divides; lia.
+ apply Z.divide_add_r. apply align_divides; lia. apply Z.divide_refl.
+Qed.
diff --git a/kvx/TargetPrinter.ml b/kvx/TargetPrinter.ml
new file mode 100644
index 00000000..9e2e3776
--- /dev/null
+++ b/kvx/TargetPrinter.ml
@@ -0,0 +1,892 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* Printing RISC-V assembly code in asm syntax *)
+
+open Printf
+open Camlcoq
+open Sections
+open AST
+open Asm
+open PrintAsmaux
+open Fileinfo
+
+(* Module containing the printing functions *)
+
+module Target (*: TARGET*) =
+ struct
+
+(* Basic printing functions *)
+
+ let comment = "#"
+
+ type idiv_function_kind =
+ | Idiv_system
+ | Idiv_stsud
+ | Idiv_fp;;
+
+ let idiv_function_kind = function
+ "stsud" -> Idiv_stsud
+ | "system" -> Idiv_system
+ | "fp" -> Idiv_fp
+ | _ -> failwith "unknown integer division kind";;
+
+ let idiv_function_kind_32bit () = idiv_function_kind !Clflags.option_div_i32;;
+ let idiv_function_kind_64bit () = idiv_function_kind !Clflags.option_div_i64;;
+
+ let subst_symbol = function
+ "__compcert_i64_udiv" ->
+ (match idiv_function_kind_64bit () with
+ | Idiv_system | Idiv_fp -> "__udivdi3"
+ | Idiv_stsud -> "__compcert_i64_udiv_stsud")
+ | "__compcert_i64_sdiv" ->
+ (match idiv_function_kind_64bit() with
+ | Idiv_system | Idiv_fp -> "__divdi3"
+ | Idiv_stsud -> "__compcert_i64_sdiv_stsud")
+ | "__compcert_i64_umod" ->
+ (match idiv_function_kind_64bit() with
+ | Idiv_system | Idiv_fp -> "__umoddi3"
+ | Idiv_stsud -> "__compcert_i64_umod_stsud")
+ | "__compcert_i64_smod" ->
+ (match idiv_function_kind_64bit() with
+ | Idiv_system | Idiv_fp -> "__moddi3"
+ | Idiv_stsud -> "__compcert_i64_smod_stsud")
+ | "__compcert_i32_sdiv" as s ->
+ (match idiv_function_kind_32bit() with
+ | Idiv_system -> s
+ | Idiv_fp -> "__compcert_i32_sdiv_fp"
+ | Idiv_stsud -> "__compcert_i32_sdiv_stsud")
+ | "__compcert_i32_udiv" as s ->
+ (match idiv_function_kind_32bit() with
+ | Idiv_system -> s
+ | Idiv_fp -> "__compcert_i32_udiv_fp"
+ | Idiv_stsud -> "__compcert_i32_udiv_stsud")
+ | "__compcert_i32_smod" as s ->
+ (match idiv_function_kind_32bit() with
+ | Idiv_system -> s
+ | Idiv_fp -> "__compcert_i32_smod_fp"
+ | Idiv_stsud -> "__compcert_i32_smod_stsud")
+ | "__compcert_i32_umod" as s ->
+ (match idiv_function_kind_32bit() with
+ | Idiv_system -> s
+ | Idiv_fp -> "__compcert_i32_umod_fp"
+ | Idiv_stsud -> "__compcert_i32_umod_stsud")
+ | "__compcert_f64_div" -> "__divdf3"
+ | "__compcert_f32_div" -> "__divsf3"
+ | x -> x;;
+
+ let symbol oc symb =
+ fprintf oc "%s" (subst_symbol (extern_atom symb))
+
+ let symbol_offset oc (symb, ofs) =
+ symbol oc symb;
+ let ofs = camlint64_of_ptrofs ofs in
+ if ofs <> 0L then fprintf oc " + %Ld" ofs
+
+ let label = elf_label
+
+ let print_label oc lbl = label oc (transl_label lbl)
+
+ let int_reg_name = let open Asmvliw in function
+
+ | GPR0 -> "$r0" | GPR1 -> "$r1" | GPR2 -> "$r2" | GPR3 -> "$r3"
+ | GPR4 -> "$r4" | GPR5 -> "$r5" | GPR6 -> "$r6" | GPR7 -> "$r7"
+ | GPR8 -> "$r8" | GPR9 -> "$r9" | GPR10 -> "$r10" | GPR11 -> "$r11"
+ | GPR12 -> "$r12" | GPR13 -> "$r13" | GPR14 -> "$r14" | GPR15 -> "$r15"
+ | GPR16 -> "$r16" | GPR17 -> "$r17" | GPR18 -> "$r18" | GPR19 -> "$r19"
+ | GPR20 -> "$r20" | GPR21 -> "$r21" | GPR22 -> "$r22" | GPR23 -> "$r23"
+ | GPR24 -> "$r24" | GPR25 -> "$r25" | GPR26 -> "$r26" | GPR27 -> "$r27"
+ | GPR28 -> "$r28" | GPR29 -> "$r29" | GPR30 -> "$r30" | GPR31 -> "$r31"
+ | GPR32 -> "$r32" | GPR33 -> "$r33" | GPR34 -> "$r34" | GPR35 -> "$r35"
+ | GPR36 -> "$r36" | GPR37 -> "$r37" | GPR38 -> "$r38" | GPR39 -> "$r39"
+ | GPR40 -> "$r40" | GPR41 -> "$r41" | GPR42 -> "$r42" | GPR43 -> "$r43"
+ | GPR44 -> "$r44" | GPR45 -> "$r45" | GPR46 -> "$r46" | GPR47 -> "$r47"
+ | GPR48 -> "$r48" | GPR49 -> "$r49" | GPR50 -> "$r50" | GPR51 -> "$r51"
+ | GPR52 -> "$r52" | GPR53 -> "$r53" | GPR54 -> "$r54" | GPR55 -> "$r55"
+ | GPR56 -> "$r56" | GPR57 -> "$r57" | GPR58 -> "$r58" | GPR59 -> "$r59"
+ | GPR60 -> "$r60" | GPR61 -> "$r61" | GPR62 -> "$r62" | GPR63 -> "$r63"
+
+ let ireg oc r = output_string oc (int_reg_name r)
+
+ let int_gpreg_q_name =
+ let open Asmvliw in
+ function
+ | R0R1 -> "$r0r1"
+ | R2R3 -> "$r2r3"
+ | R4R5 -> "$r4r5"
+ | R6R7 -> "$r6r7"
+ | R8R9 -> "$r8r9"
+ | R10R11 -> "$r10r11"
+ | R12R13 -> "$r12r13"
+ | R14R15 -> "$r14r15"
+ | R16R17 -> "$r16r17"
+ | R18R19 -> "$r18r19"
+ | R20R21 -> "$r20r21"
+ | R22R23 -> "$r22r23"
+ | R24R25 -> "$r24r25"
+ | R26R27 -> "$r26r27"
+ | R28R29 -> "$r28r29"
+ | R30R31 -> "$r30r31"
+ | R32R33 -> "$r32r33"
+ | R34R35 -> "$r34r35"
+ | R36R37 -> "$r36r37"
+ | R38R39 -> "$r38r39"
+ | R40R41 -> "$r40r41"
+ | R42R43 -> "$r42r43"
+ | R44R45 -> "$r44r45"
+ | R46R47 -> "$r46r47"
+ | R48R49 -> "$r48r49"
+ | R50R51 -> "$r50r51"
+ | R52R53 -> "$r52r53"
+ | R54R55 -> "$r54r55"
+ | R56R57 -> "$r56r57"
+ | R58R59 -> "$r58r59"
+ | R60R61 -> "$r60r61"
+ | R62R63 -> "$r62r63"
+
+ let int_gpreg_o_name =
+ let open Asmvliw in
+ function
+ | R0R1R2R3 -> "$r0r1r2r3"
+ | R4R5R6R7 -> "$r4r5r6r7"
+ | R8R9R10R11 -> "$r8r9r10r11"
+ | R12R13R14R15 -> "$r12r13r14r15"
+ | R16R17R18R19 -> "$r16r17r18r19"
+ | R20R21R22R23 -> "$r20r21r22r23"
+ | R24R25R26R27 -> "$r24r25r26r27"
+ | R28R29R30R31 -> "$r28r29r30r31"
+ | R32R33R34R35 -> "$r32r33r34r35"
+ | R36R37R38R39 -> "$r36r37r38r39"
+ | R40R41R42R43 -> "$r40r41r42r43"
+ | R44R45R46R47 -> "$r44r45r46r47"
+ | R48R49R50R51 -> "$r48r49r50r51"
+ | R52R53R54R55 -> "$r52r53r54r55"
+ | R56R57R58R59 -> "$r56r57r58r59"
+ | R60R61R62R63 -> "$r60r61r62r63";;
+
+ let gpreg_q oc r = output_string oc (int_gpreg_q_name r)
+ let gpreg_o oc r = output_string oc (int_gpreg_o_name r)
+
+ let preg oc = let open Asmvliw in function
+ | IR r -> ireg oc r
+ | RA -> output_string oc "$ra"
+ | _ -> assert false
+
+ let preg_asm oc ty = preg oc
+
+ let preg_annot = let open Asmvliw in function
+ | IR r -> int_reg_name r
+ | RA -> "$ra"
+ | _ -> assert false
+
+ let scale_of_shift1_4 = let open ExtValues in function
+ | SHIFT1 -> 2
+ | SHIFT2 -> 4
+ | SHIFT3 -> 8
+ | SHIFT4 -> 16;;
+
+(* Names of sections *)
+
+ let name_of_section = function
+ | Section_text -> ".text"
+ | Section_data(Init, true) ->
+ ".section .tdata,\"awT\",@progbits"
+ | Section_data(Uninit, true) ->
+ ".section .tbss,\"awT\",@nobits"
+ | Section_data(Init_reloc, true) ->
+ failwith "Sylvain does not how to fix this"
+ | Section_data(i, false) | Section_small_data(i) ->
+ variable_section ~sec:".data" ~bss:".bss" i
+ | Section_const i | Section_small_const i ->
+ variable_section ~sec:".section .rodata" i
+ | Section_string -> ".section .rodata"
+ | Section_literal -> ".section .rodata"
+ | Section_jumptable -> ".section .rodata"
+ | Section_debug_info _ -> ".section .debug_info,\"\",%progbits"
+ | Section_debug_loc -> ".section .debug_loc,\"\",%progbits"
+ | Section_debug_abbrev -> ".section .debug_abbrev,\"\",%progbits"
+ | Section_debug_line _ -> ".section .debug_line,\"\",%progbits"
+ | Section_debug_ranges -> ".section .debug_ranges,\"\",%progbits"
+ | Section_debug_str -> ".section .debug_str,\"MS\",%progbits,1"
+ | Section_user(s, wr, ex) ->
+ sprintf ".section \"%s\",\"a%s%s\",%%progbits"
+ s (if wr then "w" else "") (if ex then "x" else "")
+ | Section_ais_annotation -> sprintf ".section \"__compcert_ais_annotations\",\"\",@note"
+
+ let section oc sec =
+ fprintf oc " %s\n" (name_of_section sec)
+
+(* Associate labels to floating-point constants and to symbols. *)
+
+ let print_tbl oc (lbl, tbl) =
+ fprintf oc " .balign 8\n";
+ fprintf oc "%a:\n" label lbl;
+ List.iter
+ (fun l -> fprintf oc " .8byte %a\n"
+ print_label l)
+ tbl
+
+ let emit_constants oc lit =
+ if exists_constants () then begin
+ section oc lit;
+ if Hashtbl.length literal64_labels > 0 then
+ begin
+ fprintf oc " .align 3\n";
+ Hashtbl.iter
+ (fun bf lbl -> fprintf oc "%a: .quad 0x%Lx\n" label lbl bf)
+ literal64_labels
+ end;
+ if Hashtbl.length literal32_labels > 0 then
+ begin
+ fprintf oc " .align 2\n";
+ Hashtbl.iter
+ (fun bf lbl ->
+ fprintf oc "%a: .long 0x%lx\n" label lbl bf)
+ literal32_labels
+ end;
+ reset_literals ()
+ end
+
+(* Generate code to load the address of id + ofs in register r *)
+
+ let loadsymbol oc r id ofs =
+ if Archi.pic_code () then begin
+ assert (ofs = Integers.Ptrofs.zero);
+ if C2C.atom_is_thread_local id then begin
+ (* fprintf oc " addd %a = $r13, @tprel(%s)\n" ireg r (extern_atom id) *)
+ fprintf oc " addd %a = $r13, @tlsle(%s)\n" ireg r (extern_atom id)
+ end else begin
+ fprintf oc " make %a = %s\n" ireg r (extern_atom id)
+ end
+ end else
+ begin
+ if C2C.atom_is_thread_local id then begin
+ (* fprintf oc " addd %a = $r13, @tprel(%a)\n" ireg r symbol_offset (id, ofs) *)
+ fprintf oc " addd %a = $r13, @tlsle(%a)\n" ireg r symbol_offset (id, ofs)
+ end else begin
+ fprintf oc " make %a = %a\n" ireg r symbol_offset (id, ofs)
+ end
+ end
+
+(* Emit .file / .loc debugging directives *)
+
+ let print_file_line oc file line =
+ print_file_line oc comment file line
+
+(*
+ let print_location oc loc =
+ if loc <> Cutil.no_loc then print_file_line oc (fst loc) (snd loc)
+*)
+
+(* Add "w" suffix to 32-bit instructions if we are in 64-bit mode *)
+
+ (*let w oc =
+ if Archi.ptr64 then output_string oc "w"
+ *)
+
+ (* Profiling *)
+
+
+ let kvx_profiling_stub oc nr_items
+ profiling_id_table_name
+ profiling_counter_table_name =
+ fprintf oc " make $r0 = %d\n" nr_items;
+ fprintf oc " make $r1 = %s\n" profiling_id_table_name;
+ fprintf oc " make $r2 = %s\n" profiling_counter_table_name;
+ fprintf oc " goto %s\n" profiling_write_table_helper;
+ fprintf oc " ;;\n";;
+
+ (* Offset part of a load or store *)
+
+ let offset oc n = ptrofs oc n
+
+ let addressing oc = function
+ | AOff ofs -> offset oc ofs
+ | AReg ro | ARegXS ro -> ireg oc ro
+
+ let xscale oc = function
+ | ARegXS _ -> fprintf oc ".xs"
+ | _ -> ()
+
+ let lsvariant oc = function
+ | TRAP -> ()
+ | NOTRAP -> output_string oc ".s"
+
+ let icond_name = let open Asmvliw in function
+ | ITne | ITneu -> "ne"
+ | ITeq | ITequ -> "eq"
+ | ITlt -> "lt"
+ | ITge -> "ge"
+ | ITle -> "le"
+ | ITgt -> "gt"
+ | ITltu -> "ltu"
+ | ITgeu -> "geu"
+ | ITleu -> "leu"
+ | ITgtu -> "gtu"
+
+ let icond oc c = fprintf oc "%s" (icond_name c)
+
+ let fcond_name = let open Asmvliw in function
+ | FTone -> "one"
+ | FTueq -> "ueq"
+ | FToeq -> "oeq"
+ | FTune -> "une"
+ | FTolt -> "olt"
+ | FTuge -> "uge"
+ | FToge -> "oge"
+ | FTult -> "ult"
+
+ let fcond oc c = fprintf oc "%s" (fcond_name c)
+
+ let bcond_name = let open Asmvliw in function
+ | BTwnez -> "wnez"
+ | BTweqz -> "weqz"
+ | BTwltz -> "wltz"
+ | BTwgez -> "wgez"
+ | BTwlez -> "wlez"
+ | BTwgtz -> "wgtz"
+ | BTdnez -> "dnez"
+ | BTdeqz -> "deqz"
+ | BTdltz -> "dltz"
+ | BTdgez -> "dgez"
+ | BTdlez -> "dlez"
+ | BTdgtz -> "dgtz"
+
+ let bcond oc c = fprintf oc "%s" (bcond_name c)
+
+(* Printing of instructions *)
+ exception ShouldBeExpanded
+
+ let print_instruction oc = function
+ (* Pseudo-instructions expanded in Asmexpand *)
+ | Pallocframe(sz, ofs) -> assert false
+ | Pfreeframe(sz, ofs) -> assert false
+
+ (* Pseudo-instructions that remain *)
+ | Plabel lbl ->
+ fprintf oc "%a:\n" print_label lbl
+ | Ploadsymbol(rd, id, ofs) ->
+ loadsymbol oc rd id ofs
+ | Pbuiltin(ef, args, res) ->
+ begin match ef with
+ | EF_annot(kind,txt, targs) ->
+ begin match (P.to_int kind) with
+ | 1 -> let annot = annot_text preg_annot "x2" (camlstring_of_coqstring txt) args in
+ fprintf oc "%s annotation: %S\n" comment annot
+ (*| 2 -> let lbl = new_label () in
+ fprintf oc "%a: " label lbl;
+ add_ais_annot lbl preg_annot "x2" (camlstring_of_coqstring txt) args
+ *)| _ -> assert false
+ end
+ | EF_debug(kind, txt, targs) ->
+ print_debug_info comment print_file_line preg_annot "sp" oc
+ (P.to_int kind) (extern_atom txt) args
+ | EF_inline_asm(txt, sg, clob) ->
+ fprintf oc "%s begin inline assembly\n\t" comment;
+ print_inline_asm preg_asm oc (camlstring_of_coqstring txt) sg args res;
+ fprintf oc "%s end inline assembly\n" comment
+ | EF_profiling(id, coq_kind) ->
+ let kind = Z.to_int coq_kind in
+ assert (kind >= 0);
+ assert (kind <= 1);
+ fprintf oc "%s profiling %a %d\n" comment
+ Profilingaux.pp_id id kind;
+ fprintf oc " make $r63 = %s\n" profiling_counter_table_name;
+ fprintf oc " make $r62 = 1\n";
+ fprintf oc " ;;\n";
+ fprintf oc " afaddd %d[$r63] = $r62\n"
+ (profiling_offset id kind);
+ fprintf oc " ;;\n"
+ | _ ->
+ assert false
+ end
+ | Pnop -> (* FIXME fprintf oc " nop\n" *) ()
+ | Psemi -> fprintf oc ";;\n"
+
+ | Pclzll (rd, rs) -> fprintf oc " clzd %a = %a\n" ireg rd ireg rs
+ | Pclzw (rd, rs) -> fprintf oc " clzw %a = %a\n" ireg rd ireg rs
+ | Pctzll (rd, rs) -> fprintf oc " ctzd %a = %a\n" ireg rd ireg rs
+ | Pctzw (rd, rs) -> fprintf oc " ctzw %a = %a\n" ireg rd ireg rs
+ | Pstsud (rd, rs1, rs2) -> fprintf oc " stsud %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+
+
+ (* Control flow instructions *)
+ | Pget (rd, rs) ->
+ fprintf oc " get %a = %a\n" ireg rd preg rs
+ | Pset (rd, rs) ->
+ fprintf oc " set %a = %a\n" preg rd ireg rs
+ | Pret ->
+ fprintf oc " ret \n"
+ | Pcall(s) ->
+ fprintf oc " call %a\n" symbol s
+ | Picall(rs) ->
+ fprintf oc " icall %a\n" ireg rs
+ | Pgoto(s) ->
+ fprintf oc " goto %a\n" symbol s
+ | Pigoto(rs) ->
+ fprintf oc " igoto %a\n" ireg rs
+ | Pj_l(s) ->
+ fprintf oc " goto %a\n" print_label s
+ | Pcb (bt, r, lbl) | Pcbu (bt, r, lbl) ->
+ fprintf oc " cb.%a %a? %a\n" bcond bt ireg r print_label lbl
+
+ (* For builtins *)
+ | Ploopdo (r, lbl) ->
+ fprintf oc " loopdo %a, %a\n" ireg r print_label lbl
+ | Pgetn(n, dst) ->
+ fprintf oc " get %a = $s%ld\n" ireg dst (camlint_of_coqint n)
+ | Psetn(n, dst) ->
+ fprintf oc " set $s%ld = %a\n" (camlint_of_coqint n) ireg dst
+ | Pwfxl(n, dst) ->
+ fprintf oc " wfxl $s%ld = %a\n" (camlint_of_coqint n) ireg dst
+ | Pwfxm(n, dst) ->
+ fprintf oc " wfxm $s%ld = %a\n" (camlint_of_coqint n) ireg dst
+ | Pldu(dst, addr) ->
+ fprintf oc " ld.u %a = 0[%a]\n" ireg dst ireg addr
+ | Plbzu(dst, addr) ->
+ fprintf oc " lbz.u %a = 0[%a]\n" ireg dst ireg addr
+ | Plhzu(dst, addr) ->
+ fprintf oc " lhz.u %a = 0[%a]\n" ireg dst ireg addr
+ | Plwzu(dst, addr) ->
+ fprintf oc " lwz.u %a = 0[%a]\n" ireg dst ireg addr
+ | Pawait ->
+ fprintf oc " await\n"
+ | Psleep ->
+ fprintf oc " sleep\n"
+ | Pstop ->
+ fprintf oc " stop\n"
+ | Pbarrier ->
+ fprintf oc " barrier\n"
+ | Pfence ->
+ fprintf oc " fence\n"
+ | Pdinval ->
+ fprintf oc " dinval\n"
+ | Pdinvall addr ->
+ fprintf oc " dinvall 0[%a]\n" ireg addr
+ | Pdtouchl addr ->
+ fprintf oc " dtouchl 0[%a]\n" ireg addr
+ | Piinval ->
+ fprintf oc " iinval\n"
+ | Piinvals addr ->
+ fprintf oc " iinvals 0[%a]\n" ireg addr
+ | Pitouchl addr ->
+ fprintf oc " itouchl 0[%a]\n" ireg addr
+ | Pdzerol addr ->
+ fprintf oc " dzerol 0[%a]\n" ireg addr
+(* | Pafaddd(addr, incr_res) ->
+ fprintfoc " afaddd 0[%a] = %a\n" ireg addr ireg incr_res
+ | Pafaddw(addr, incr_res) ->
+ fprintfoc " afaddw 0[%a] = %a\n" ireg addr ireg incr_res *) (* see #157 *)
+ | Palclrd(res, addr) ->
+ fprintf oc " alclrd %a = 0[%a]\n" ireg res ireg addr
+ | Palclrw(res, addr) ->
+ fprintf oc " alclrw %a = 0[%a]\n" ireg res ireg addr
+ | Pjumptable (idx_reg, tbl) ->
+ let lbl = new_label() in
+ (* jumptables := (lbl, tbl) :: !jumptables; *)
+ let base_reg = if idx_reg=Asmvliw.GPR63 then Asmvliw.GPR62 else Asmvliw.GPR63 in
+ fprintf oc "%s jumptable [ " comment;
+ List.iter (fun l -> fprintf oc "%a " print_label l) tbl;
+ fprintf oc "]\n";
+ fprintf oc " make %a = %a\n ;;\n" ireg base_reg label lbl;
+ fprintf oc " ld.xs %a = %a[%a]\n ;;\n" ireg base_reg ireg idx_reg ireg base_reg;
+ fprintf oc " igoto %a\n ;;\n" ireg base_reg;
+ section oc Section_jumptable;
+ print_tbl oc (lbl, tbl);
+ section oc Section_text
+
+ (* Load/Store instructions *)
+ | Plb(trap, rd, ra, adr) ->
+ fprintf oc " lbs%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Plbu(trap, rd, ra, adr) ->
+ fprintf oc " lbz%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Plh(trap, rd, ra, adr) ->
+ fprintf oc " lhs%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Plhu(trap, rd, ra, adr) ->
+ fprintf oc " lhz%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Plw(trap, rd, ra, adr) | Plw_a(trap, rd, ra, adr) | Pfls(trap, rd, ra, adr) ->
+ fprintf oc " lws%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Pld(trap, rd, ra, adr) | Pfld(trap, rd, ra, adr) | Pld_a(trap, rd, ra, adr) -> assert Archi.ptr64;
+ fprintf oc " ld%a%a %a = %a[%a]\n" lsvariant trap xscale adr ireg rd addressing adr ireg ra
+ | Plq(rd, ra, adr) ->
+ fprintf oc " lq%a %a = %a[%a]\n" xscale adr gpreg_q rd addressing adr ireg ra
+ | Plo(rd, ra, adr) ->
+ fprintf oc " lo%a %a = %a[%a]\n" xscale adr gpreg_o rd addressing adr ireg ra
+
+ | Psb(rd, ra, adr) ->
+ fprintf oc " sb%a %a[%a] = %a\n" xscale adr addressing adr ireg ra ireg rd
+ | Psh(rd, ra, adr) ->
+ fprintf oc " sh%a %a[%a] = %a\n" xscale adr addressing adr ireg ra ireg rd
+ | Psw(rd, ra, adr) | Psw_a(rd, ra, adr) | Pfss(rd, ra, adr) ->
+ fprintf oc " sw%a %a[%a] = %a\n" xscale adr addressing adr ireg ra ireg rd
+ | Psd(rd, ra, adr) | Psd_a(rd, ra, adr) | Pfsd(rd, ra, adr) -> assert Archi.ptr64;
+ fprintf oc " sd%a %a[%a] = %a\n" xscale adr addressing adr ireg ra ireg rd
+ | Psq(rd, ra, adr) ->
+ fprintf oc " sq%a %a[%a] = %a\n" xscale adr addressing adr ireg ra gpreg_q rd
+ | Pso(rd, ra, adr) ->
+ fprintf oc " so%a %a[%a] = %a\n" xscale adr addressing adr ireg ra gpreg_o rd
+
+ (* Arith R instructions *)
+
+ (* Arith RR instructions *)
+ | Pmv(rd, rs) ->
+ fprintf oc " addd %a = %a, 0\n" ireg rd ireg rs
+ | Pcvtl2w(rd, rs) -> assert false
+ | Pnegl(rd, rs) -> assert Archi.ptr64;
+ fprintf oc " negd %a = %a\n" ireg rd ireg rs
+ | Pnegw(rd, rs) ->
+ fprintf oc " negw %a = %a\n" ireg rd ireg rs
+ | Psxwd(rd, rs) ->
+ fprintf oc " sxwd %a = %a\n" ireg rd ireg rs
+ | Pzxwd(rd, rs) ->
+ fprintf oc " zxwd %a = %a\n" ireg rd ireg rs
+ | Pextfz(rd, rs, stop, start) | Pextfzl(rd, rs, stop, start) ->
+ fprintf oc " extfz %a = %a, %ld, %ld\n" ireg rd ireg rs (camlint_of_coqint stop) (camlint_of_coqint start)
+ | Pextfs(rd, rs, stop, start) | Pextfsl(rd, rs, stop, start) ->
+ fprintf oc " extfs %a = %a, %ld, %ld\n" ireg rd ireg rs (camlint_of_coqint stop) (camlint_of_coqint start)
+ | Pinsf(rd, rs, stop, start) | Pinsfl(rd, rs, stop, start) ->
+ fprintf oc " insf %a = %a, %ld, %ld\n" ireg rd ireg rs (camlint_of_coqint stop) (camlint_of_coqint start)
+ | Pfabsd(rd, rs) ->
+ fprintf oc " fabsd %a = %a\n" ireg rd ireg rs
+ | Pfabsw(rd, rs) ->
+ fprintf oc " fabsw %a = %a\n" ireg rd ireg rs
+ | Pfnegd(rd, rs) ->
+ fprintf oc " fnegd %a = %a\n" ireg rd ireg rs
+ | Pfnegw(rd, rs) ->
+ fprintf oc " fnegw %a = %a\n" ireg rd ireg rs
+ | Pfnarrowdw(rd, rs) ->
+ fprintf oc " fnarrowdw %a = %a\n" ireg rd ireg rs
+ | Pfwidenlwd(rd, rs) ->
+ fprintf oc " fwidenlwd %a = %a\n" ireg rd ireg rs
+ | Pfloatuwrnsz(rd, rs) ->
+ fprintf oc " floatuw.rn.s %a = %a, 0\n" ireg rd ireg rs
+ | Pfloatwrnsz(rd, rs) ->
+ fprintf oc " floatw.rn.s %a = %a, 0\n" ireg rd ireg rs
+ | Pfloatudrnsz(rd, rs) ->
+ fprintf oc " floatud.rn.s %a = %a, 0\n" ireg rd ireg rs
+ | Pfloatdrnsz(rd, rs) ->
+ fprintf oc " floatd.rn.s %a = %a, 0\n" ireg rd ireg rs
+ | Pfixedwrzz(rd, rs) ->
+ fprintf oc " fixedw.rz %a = %a, 0\n" ireg rd ireg rs
+ | Pfixeduwrzz(rd, rs) ->
+ fprintf oc " fixeduw.rz %a = %a, 0\n" ireg rd ireg rs
+ | Pfixeddrzz(rd, rs) | Pfixeddrzz_i32(rd, rs) ->
+ fprintf oc " fixedd.rz %a = %a, 0\n" ireg rd ireg rs
+ | Pfixedudrzz(rd, rs) | Pfixedudrzz_i32(rd, rs) ->
+ fprintf oc " fixedud.rz %a = %a, 0\n" ireg rd ireg rs
+
+ (* Arith RI32 instructions *)
+ | Pmake (rd, imm) ->
+ fprintf oc " make %a, %a\n" ireg rd coqint imm
+
+ (* Arith RI64 instructions *)
+ | Pmakel (rd, imm) ->
+ fprintf oc " make %a, %a\n" ireg rd coqint64 imm
+
+ (* Arith RF32 instructions *)
+ | Pmakefs (rd, f) ->
+ let d = Floats.Float32.to_bits f in
+ fprintf oc " make %a, %a %s %.18g\n"
+ ireg rd coqint d comment (camlfloat_of_coqfloat32 f)
+
+ (* Arith RF64 instructions *)
+ | Pmakef (rd, f) ->
+ let d = Floats.Float.to_bits f in
+ fprintf oc " make %a, %a %s %.18g\n"
+ ireg rd coqint64 d comment (camlfloat_of_coqfloat f)
+
+ (* Arith RRR instructions *)
+ | Pcompw (it, rd, rs1, rs2) ->
+ fprintf oc " compw.%a %a = %a, %a\n" icond it ireg rd ireg rs1 ireg rs2
+ | Pcompl (it, rd, rs1, rs2) ->
+ fprintf oc " compd.%a %a = %a, %a\n" icond it ireg rd ireg rs1 ireg rs2
+
+ | Pfcompw (ft, rd, rs1, rs2) ->
+ fprintf oc " fcompw.%a %a = %a, %a\n" fcond ft ireg rd ireg rs1 ireg rs2
+ | Pfcompl (ft, rd, rs1, rs2) ->
+ fprintf oc " fcompd.%a %a = %a, %a\n" fcond ft ireg rd ireg rs1 ireg rs2
+
+ | Paddw (rd, rs1, rs2) ->
+ fprintf oc " addw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Paddxw (s14, rd, rs1, rs2) ->
+ fprintf oc " addx%dw %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs1 ireg rs2
+ | Psubw (rd, rs1, rs2) ->
+ fprintf oc " sbfw %a = %a, %a\n" ireg rd ireg rs2 ireg rs1
+ | Prevsubxw (s14, rd, rs1, rs2) ->
+ fprintf oc " sbfx%dw %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs1 ireg rs2
+ | Pmulw (rd, rs1, rs2) ->
+ fprintf oc " mulw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pandw (rd, rs1, rs2) ->
+ fprintf oc " andw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnandw (rd, rs1, rs2) ->
+ fprintf oc " nandw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Porw (rd, rs1, rs2) ->
+ fprintf oc " orw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnorw (rd, rs1, rs2) ->
+ fprintf oc " norw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pxorw (rd, rs1, rs2) ->
+ fprintf oc " xorw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnxorw (rd, rs1, rs2) ->
+ fprintf oc " nxorw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pandnw (rd, rs1, rs2) ->
+ fprintf oc " andnw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pornw (rd, rs1, rs2) ->
+ fprintf oc " ornw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psraw (rd, rs1, rs2) ->
+ fprintf oc " sraw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psrxw (rd, rs1, rs2) ->
+ fprintf oc " srsw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psrlw (rd, rs1, rs2) ->
+ fprintf oc " srlw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psllw (rd, rs1, rs2) ->
+ fprintf oc " sllw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pmaddw (rd, rs1, rs2) ->
+ fprintf oc " maddw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pmsubw (rd, rs1, rs2) ->
+ fprintf oc " msbfw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmaddfw (rd, rs1, rs2) ->
+ fprintf oc " ffmaw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmsubfw (rd, rs1, rs2) ->
+ fprintf oc " ffmsw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+
+ | Paddl (rd, rs1, rs2) ->
+ fprintf oc " addd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Paddxl (s14, rd, rs1, rs2) ->
+ fprintf oc " addx%dd %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs1 ireg rs2
+ | Psubl (rd, rs1, rs2) ->
+ fprintf oc " sbfd %a = %a, %a\n" ireg rd ireg rs2 ireg rs1
+ | Prevsubxl (s14, rd, rs1, rs2) ->
+ fprintf oc " sbfx%dd %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs1 ireg rs2
+ | Pandl (rd, rs1, rs2) ->
+ fprintf oc " andd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnandl (rd, rs1, rs2) ->
+ fprintf oc " nandd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Porl (rd, rs1, rs2) ->
+ fprintf oc " ord %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnorl (rd, rs1, rs2) ->
+ fprintf oc " nord %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pxorl (rd, rs1, rs2) ->
+ fprintf oc " xord %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pnxorl (rd, rs1, rs2) ->
+ fprintf oc " nxord %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pandnl (rd, rs1, rs2) ->
+ fprintf oc " andnd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pornl (rd, rs1, rs2) ->
+ fprintf oc " ornd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pmull (rd, rs1, rs2) ->
+ fprintf oc " muld %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pslll (rd, rs1, rs2) ->
+ fprintf oc " slld %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psrll (rd, rs1, rs2) ->
+ fprintf oc " srld %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psrxl (rd, rs1, rs2) ->
+ fprintf oc " srsd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Psral (rd, rs1, rs2) ->
+ fprintf oc " srad %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pmaddl (rd, rs1, rs2) ->
+ fprintf oc " maddd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pmsubl (rd, rs1, rs2) ->
+ fprintf oc " msbfd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmaddfl (rd, rs1, rs2) ->
+ fprintf oc " ffmad %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmsubfl (rd, rs1, rs2) ->
+ fprintf oc " ffmsd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+
+ | Pfaddd (rd, rs1, rs2) ->
+ fprintf oc " faddd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfaddw (rd, rs1, rs2) ->
+ fprintf oc " faddw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfsbfd (rd, rs1, rs2) ->
+ fprintf oc " fsbfd %a = %a, %a\n" ireg rd ireg rs2 ireg rs1
+ | Pfsbfw (rd, rs1, rs2) ->
+ fprintf oc " fsbfw %a = %a, %a\n" ireg rd ireg rs2 ireg rs1
+ | Pfmuld (rd, rs1, rs2) ->
+ fprintf oc " fmuld %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmulw (rd, rs1, rs2) ->
+ fprintf oc " fmulw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmind (rd, rs1, rs2) ->
+ fprintf oc " fmind %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfminw (rd, rs1, rs2) ->
+ fprintf oc " fminw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmaxd (rd, rs1, rs2) ->
+ fprintf oc " fmaxd %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfmaxw (rd, rs1, rs2) ->
+ fprintf oc " fmaxw %a = %a, %a\n" ireg rd ireg rs1 ireg rs2
+ | Pfinvw (rd, rs1) ->
+ fprintf oc " finvw %a = %a\n" ireg rd ireg rs1
+
+ (* Arith RRI32 instructions *)
+ | Pcompiw (it, rd, rs, imm) ->
+ fprintf oc " compw.%a %a = %a, %a\n" icond it ireg rd ireg rs coqint imm
+ | Paddiw (rd, rs, imm) ->
+ fprintf oc " addw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Paddxiw (s14, rd, rs, imm) ->
+ fprintf oc " addx%dw %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs coqint imm
+ | Prevsubiw (rd, rs, imm) ->
+ fprintf oc " sbfw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Prevsubxiw (s14, rd, rs, imm) ->
+ fprintf oc " sbfx%dw %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs coqint imm
+ | Pmuliw (rd, rs, imm) ->
+ fprintf oc " mulw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pandiw (rd, rs, imm) ->
+ fprintf oc " andw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pnandiw (rd, rs, imm) ->
+ fprintf oc " nandw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Poriw (rd, rs, imm) ->
+ fprintf oc " orw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pnoriw (rd, rs, imm) ->
+ fprintf oc " norw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pxoriw (rd, rs, imm) ->
+ fprintf oc " xorw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pnxoriw (rd, rs, imm) ->
+ fprintf oc " nxorw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pandniw (rd, rs, imm) ->
+ fprintf oc " andnw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Porniw (rd, rs, imm) ->
+ fprintf oc " ornw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Psraiw (rd, rs, imm) ->
+ fprintf oc " sraw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Psrxiw (rd, rs, imm) ->
+ fprintf oc " srsw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Psrliw (rd, rs, imm) ->
+ fprintf oc " srlw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pslliw (rd, rs, imm) ->
+ fprintf oc " sllw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Proriw (rd, rs, imm) ->
+ fprintf oc " rorw %a = %a, %a\n" ireg rd ireg rs coqint imm
+ | Pmaddiw (rd, rs, imm) ->
+ fprintf oc " maddw %a = %a, %a\n" ireg rd ireg rs coqint imm
+
+ | Psllil (rd, rs, imm) ->
+ fprintf oc " slld %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Psrlil (rd, rs, imm) ->
+ fprintf oc " srld %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Psrail (rd, rs, imm) ->
+ fprintf oc " srad %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Psrxil (rd, rs, imm) ->
+ fprintf oc " srsd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+
+ (* Arith RRI64 instructions *)
+ | Pcompil (it, rd, rs, imm) ->
+ fprintf oc " compd.%a %a = %a, %a\n" icond it ireg rd ireg rs coqint64 imm
+ | Paddil (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " addd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Paddxil (s14, rd, rs, imm) ->
+ fprintf oc " addx%dd %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs coqint imm
+ | Prevsubil (rd, rs, imm) ->
+ fprintf oc " sbfd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Prevsubxil (s14, rd, rs, imm) ->
+ fprintf oc " sbfx%dd %a = %a, %a\n" (scale_of_shift1_4 s14)
+ ireg rd ireg rs coqint64 imm
+ | Pmulil (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " muld %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pandil (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " andd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pnandil (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " nandd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Poril (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " ord %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pnoril (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " nord %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pxoril (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " xord %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pnxoril (rd, rs, imm) -> assert Archi.ptr64;
+ fprintf oc " nxord %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pandnil (rd, rs, imm) ->
+ fprintf oc " andnd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pornil (rd, rs, imm) ->
+ fprintf oc " ornd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+ | Pmaddil (rd, rs, imm) ->
+ fprintf oc " maddd %a = %a, %a\n" ireg rd ireg rs coqint64 imm
+
+ | Pcmove (bt, rd, rcond, rs) | Pcmoveu (bt, rd, rcond, rs) ->
+ fprintf oc " cmoved.%a %a? %a = %a\n"
+ bcond bt ireg rcond ireg rd ireg rs
+ | Pcmoveiw (bt, rd, rcond, imm) | Pcmoveuiw (bt, rd, rcond, imm) ->
+ fprintf oc " cmoved.%a %a? %a = %a\n"
+ bcond bt ireg rcond ireg rd coqint imm
+ | Pcmoveil (bt, rd, rcond, imm) | Pcmoveuil (bt, rd, rcond, imm) ->
+ fprintf oc " cmoved.%a %a? %a = %a\n"
+ bcond bt ireg rcond ireg rd coqint64 imm
+
+ let get_section_names name =
+ let (text, lit) =
+ match C2C.atom_sections name with
+ | t :: l :: _ -> (t, l)
+ | _ -> (Section_text, Section_literal) in
+ text,lit,Section_jumptable
+
+ let print_align oc alignment =
+ fprintf oc " .balign %d\n" alignment
+
+ let print_jumptable oc jmptbl = ()
+ (* if !jumptables <> [] then
+ begin
+ section oc jmptbl;
+ List.iter (print_tbl oc) !jumptables;
+ jumptables := []
+ end *)
+
+ let print_fun_info = elf_print_fun_info
+
+ let print_optional_fun_info _ = ()
+
+ let print_var_info = elf_print_var_info
+
+ let print_comm_symb oc sz name align =
+ if C2C.atom_is_static name then
+ fprintf oc " .local %a\n" symbol name;
+ fprintf oc " .comm %a, %s, %d\n"
+ symbol name
+ (Z.to_string sz)
+ align
+
+ let print_instructions oc fn =
+ current_function_sig := fn.fn_sig;
+ List.iter (print_instruction oc) fn.fn_code
+
+(* Data *)
+
+ let address = if Archi.ptr64 then ".quad" else ".long"
+
+ let print_prologue oc =
+ (* fprintf oc " .option %s\n" (if Archi.pic_code() then "pic" else "nopic"); *)
+ if !Clflags.option_g then begin
+ section oc Section_text;
+ end
+
+ let print_epilogue oc =
+ print_profiling_epilogue elf_text_print_fun_info Dtors kvx_profiling_stub oc;
+ if !Clflags.option_g then begin
+ Debug.compute_gnu_file_enum (fun f -> ignore (print_file oc f));
+ section oc Section_text;
+ end
+
+ let default_falignment = 2
+
+ let cfi_startproc oc = ()
+ let cfi_endproc oc = ()
+
+ end
+
+let sel_target () =
+ (module Target:TARGET)
diff --git a/kvx/ValueAOp.v b/kvx/ValueAOp.v
new file mode 100644
index 00000000..87554258
--- /dev/null
+++ b/kvx/ValueAOp.v
@@ -0,0 +1,599 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Compopts.
+Require Import AST Integers Floats Values Memory Globalenvs.
+Require Import Op ExtValues ExtFloats RTL ValueDomain.
+Definition minf := binop_float ExtFloat.min.
+Definition maxf := binop_float ExtFloat.max.
+Definition minfs := binop_single ExtFloat32.min.
+Definition maxfs := binop_single ExtFloat32.max.
+
+Definition ntop3 (x y z: aval) : aval := Ifptr (plub (provenance x) (plub (provenance y) (provenance z))).
+
+Definition triple_op_float (sem: float -> float -> float -> float) (x y z: aval) :=
+ match x, y, z with
+ | F a, F b, F c => F (sem a b c)
+ | _, _, _ => ntop3 x y z
+ end.
+
+Definition triple_op_single (sem: float32 -> float32 -> float32 -> float32) (x y z: aval) :=
+ match x, y, z with
+ | FS a, FS b, FS c => FS (sem a b c)
+ | _, _, _ => ntop3 x y z
+ end.
+
+Definition fmaddf := triple_op_float (fun x y z => Float.fma y z x).
+Definition fmsubf := triple_op_float (fun x y z => Float.fma (Float.neg y) z x).
+Definition fmaddfs := triple_op_single (fun x y z => Float32.fma y z x).
+Definition fmsubfs := triple_op_single (fun x y z => Float32.fma (Float32.neg y) z x).
+
+Definition invfs (y : aval) :=
+ match y with
+ | FS f => FS (ExtFloat32.inv f)
+ | _ => ntop1 y
+ end.
+
+(** Value analysis for RISC V operators *)
+
+Definition eval_static_condition (cond: condition) (vl: list aval): abool :=
+ match cond, vl with
+ | Ccomp c, v1 :: v2 :: nil => cmp_bool c v1 v2
+ | Ccompu c, v1 :: v2 :: nil => cmpu_bool c v1 v2
+ | Ccompimm c n, v1 :: nil => cmp_bool c v1 (I n)
+ | Ccompuimm c n, v1 :: nil => cmpu_bool c v1 (I n)
+ | Ccompl c, v1 :: v2 :: nil => cmpl_bool c v1 v2
+ | Ccomplu c, v1 :: v2 :: nil => cmplu_bool c v1 v2
+ | Ccomplimm c n, v1 :: nil => cmpl_bool c v1 (L n)
+ | Ccompluimm c n, v1 :: nil => cmplu_bool c v1 (L n)
+ | Ccompf c, v1 :: v2 :: nil => cmpf_bool c v1 v2
+ | Cnotcompf c, v1 :: v2 :: nil => cnot (cmpf_bool c v1 v2)
+ | Ccompfs c, v1 :: v2 :: nil => cmpfs_bool c v1 v2
+ | Cnotcompfs c, v1 :: v2 :: nil => cnot (cmpfs_bool c v1 v2)
+ | _, _ => Bnone
+ end.
+
+Definition eval_static_addressing (addr: addressing) (vl: list aval): aval :=
+ match addr, vl with
+ | Aindexed n, v1::nil => offset_ptr v1 n
+ | Aindexed2, v1::v2::nil => addl v1 v2
+ | Aindexed2XS scale, v1::v2::nil => addl v1 (shll v2 (I (Int.repr scale)))
+ | Aglobal s ofs, nil => Ptr (Gl s ofs)
+ | Ainstack ofs, nil => Ptr (Stk ofs)
+ | _, _ => Vbot
+ end.
+
+Definition eval_static_condition0 (cond : condition0) (v : aval) : abool :=
+ match cond with
+ | Ccomp0 c => cmp_bool c v (I Int.zero)
+ | Ccompu0 c => cmpu_bool c v (I Int.zero)
+ | Ccompl0 c => cmpl_bool c v (L Int64.zero)
+ | Ccomplu0 c => cmplu_bool c v (L Int64.zero)
+ end.
+
+
+Definition eval_static_extfs (stop : Z) (start : Z) (v : aval) :=
+ if is_bitfield stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | I w =>
+ I (Int.shr (Int.shl w (Int.repr (Z.sub Int.zwordsize stop'))) (Int.repr (Z.sub Int.zwordsize (Z.sub stop' start))))
+ | _ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_extfz (stop : Z) (start : Z) (v : aval) :=
+ if is_bitfield stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | I w =>
+ I (Int.shru (Int.shl w (Int.repr (Z.sub Int.zwordsize stop'))) (Int.repr (Z.sub Int.zwordsize (Z.sub stop' start))))
+ | _ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_extfsl (stop : Z) (start : Z) (v : aval) :=
+ if is_bitfieldl stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | L w =>
+ L (Int64.shr' (Int64.shl' w (Int.repr (Z.sub Int64.zwordsize stop'))) (Int.repr (Z.sub Int64.zwordsize (Z.sub stop' start))))
+ | _ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_extfzl (stop : Z) (start : Z) (v : aval) :=
+ if is_bitfieldl stop start
+ then
+ let stop' := Z.add stop Z.one in
+ match v with
+ | L w =>
+ L (Int64.shru' (Int64.shl' w (Int.repr (Z.sub Int64.zwordsize stop'))) (Int.repr (Z.sub Int64.zwordsize (Z.sub stop' start))))
+ | _ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_insf stop start prev fld :=
+ let mask := Int.repr (zbitfield_mask stop start) in
+ if is_bitfield stop start
+ then
+ match prev, fld with
+ | (I prevI), (I fldI) =>
+ if Int.ltu (Int.repr start) Int.iwordsize
+ then I (Int.or (Int.and prevI (Int.not mask))
+ (Int.and (Int.shl fldI (Int.repr start)) mask))
+ else Vtop
+ | _, _ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_insfl stop start prev fld :=
+ let mask := Int64.repr (zbitfield_mask stop start) in
+ if is_bitfieldl stop start
+ then
+ match prev, fld with
+ | (L prevL), (L fldL) =>
+ if Int.ltu (Int.repr start) Int64.iwordsize'
+ then L (Int64.or (Int64.and prevL (Int64.not mask))
+ (Int64.and (Int64.shl' fldL (Int.repr start)) mask))
+ else Vtop
+ | _,_ => Vtop
+ end
+ else Vtop.
+
+Definition eval_static_operation (op: operation) (vl: list aval): aval :=
+ match op, vl with
+ | Omove, v1::nil => v1
+ | Ointconst n, nil => I n
+ | Olongconst n, nil => L n
+ | Ofloatconst n, nil => if propagate_float_constants tt then F n else ntop
+ | Osingleconst n, nil => if propagate_float_constants tt then FS n else ntop
+ | Oaddrsymbol id ofs, nil => Ptr (Gl id ofs)
+ | Oaddrstack ofs, nil => Ptr (Stk ofs)
+ | Ocast8signed, v1 :: nil => sign_ext 8 v1
+ | Ocast16signed, v1 :: nil => sign_ext 16 v1
+ | Oadd, v1::v2::nil => add v1 v2
+ | Oaddimm n, v1::nil => add v1 (I n)
+ | Oaddx shift, v1::v2::nil => add v2 (shl v1 (I (int_of_shift1_4 shift)))
+ | Oaddximm shift n, v1::nil => add (I n) (shl v1 (I (int_of_shift1_4 shift)))
+ | Oneg, v1::nil => neg v1
+ | Osub, v1::v2::nil => sub v1 v2
+ | Orevsubx shift, v1::v2::nil => sub v2 (shl v1 (I (int_of_shift1_4 shift)))
+ | Orevsubimm n, v1::nil => sub (I n) v1
+ | Orevsubximm shift n, v1::nil => sub (I n) (shl v1 (I (int_of_shift1_4 shift)))
+ | Omul, v1::v2::nil => mul v1 v2
+ | Omulimm n, v1::nil => mul v1 (I n)
+ | Omulhs, v1::v2::nil => mulhs v1 v2
+ | Omulhu, v1::v2::nil => mulhu v1 v2
+ | Odiv, v1::v2::nil => divs v1 v2
+ | Odivu, v1::v2::nil => divu v1 v2
+ | Omod, v1::v2::nil => mods v1 v2
+ | Omodu, v1::v2::nil => modu v1 v2
+ | Oand, v1::v2::nil => and v1 v2
+ | Oandimm n, v1::nil => and v1 (I n)
+ | Onand, v1::v2::nil => notint (and v1 v2)
+ | Onandimm n, v1::nil => notint (and v1 (I n))
+ | Oor, v1::v2::nil => or v1 v2
+ | Oorimm n, v1::nil => or v1 (I n)
+ | Onor, v1::v2::nil => notint (or v1 v2)
+ | Onorimm n, v1::nil => notint (or v1 (I n))
+ | Oxor, v1::v2::nil => xor v1 v2
+ | Oxorimm n, v1::nil => xor v1 (I n)
+ | Onxor, v1::v2::nil => notint (xor v1 v2)
+ | Onxorimm n, v1::nil => notint (xor v1 (I n))
+ | Onot, v1::nil => notint v1
+ | Oandn, v1::v2::nil => and (notint v1) v2
+ | Oandnimm n, v1::nil => and (notint v1) (I n)
+ | Oorn, v1::v2::nil => or (notint v1) v2
+ | Oornimm n, v1::nil => or (notint v1) (I n)
+ | Oshl, v1::v2::nil => shl v1 v2
+ | Oshlimm n, v1::nil => shl v1 (I n)
+ | Oshr, v1::v2::nil => shr v1 v2
+ | Oshrimm n, v1::nil => shr v1 (I n)
+ | Ororimm n, v1::nil => ror v1 (I n)
+ | Oshru, v1::v2::nil => shru v1 v2
+ | Oshruimm n, v1::nil => shru v1 (I n)
+ | Oshrximm n, v1::nil => shrx v1 (I n)
+ | Omadd, v1::v2::v3::nil => add v1 (mul v2 v3)
+ | Omaddimm n, v1::v2::nil => add v1 (mul v2 (I n))
+ | Omsub, v1::v2::v3::nil => sub v1 (mul v2 v3)
+ | Omakelong, v1::v2::nil => longofwords v1 v2
+ | Olowlong, v1::nil => loword v1
+ | Ohighlong, v1::nil => hiword v1
+ | Ocast32signed, v1::nil => longofint v1
+ | Ocast32unsigned, v1::nil => longofintu v1
+ | Oaddl, v1::v2::nil => addl v1 v2
+ | Oaddlimm n, v1::nil => addl v1 (L n)
+ | Oaddxl shift, v1::v2::nil => addl v2 (shll v1 (I (int_of_shift1_4 shift)))
+ | Oaddxlimm shift n, v1::nil => addl (L n) (shll v1 (I (int_of_shift1_4 shift)))
+ | Onegl, v1::nil => negl v1
+ | Osubl, v1::v2::nil => subl v1 v2
+ | Orevsubxl shift, v1::v2::nil => subl v2 (shll v1 (I (int_of_shift1_4 shift)))
+ | Orevsublimm n, v1::nil => subl (L n) v1
+ | Orevsubxlimm shift n, v1::nil => subl (L n) (shll v1 (I (int_of_shift1_4 shift)))
+ | Omull, v1::v2::nil => mull v1 v2
+ | Omullimm n, v1::nil => mull v1 (L n)
+ | Omullhs, v1::v2::nil => mullhs v1 v2
+ | Omullhu, v1::v2::nil => mullhu v1 v2
+ | Odivl, v1::v2::nil => divls v1 v2
+ | Odivlu, v1::v2::nil => divlu v1 v2
+ | Omodl, v1::v2::nil => modls v1 v2
+ | Omodlu, v1::v2::nil => modlu v1 v2
+ | Oandl, v1::v2::nil => andl v1 v2
+ | Oandlimm n, v1::nil => andl v1 (L n)
+ | Onandl, v1::v2::nil => notl (andl v1 v2)
+ | Onandlimm n, v1::nil => notl (andl v1 (L n))
+ | Oorl, v1::v2::nil => orl v1 v2
+ | Oorlimm n, v1::nil => orl v1 (L n)
+ | Onorl, v1::v2::nil => notl (orl v1 v2)
+ | Onorlimm n, v1::nil => notl (orl v1 (L n))
+ | Oxorl, v1::v2::nil => xorl v1 v2
+ | Oxorlimm n, v1::nil => xorl v1 (L n)
+ | Onxorl, v1::v2::nil => notl (xorl v1 v2)
+ | Onxorlimm n, v1::nil => notl (xorl v1 (L n))
+ | Onotl, v1::nil => notl v1
+ | Oandnl, v1::v2::nil => andl (notl v1) v2
+ | Oandnlimm n, v1::nil => andl (notl v1) (L n)
+ | Oornl, v1::v2::nil => orl (notl v1) v2
+ | Oornlimm n, v1::nil => orl (notl v1) (L n)
+ | Oshll, v1::v2::nil => shll v1 v2
+ | Oshllimm n, v1::nil => shll v1 (I n)
+ | Oshrl, v1::v2::nil => shrl v1 v2
+ | Oshrlimm n, v1::nil => shrl v1 (I n)
+ | Oshrlu, v1::v2::nil => shrlu v1 v2
+ | Oshrluimm n, v1::nil => shrlu v1 (I n)
+ | Oshrxlimm n, v1::nil => shrxl v1 (I n)
+ | Omaddl, v1::v2::v3::nil => addl v1 (mull v2 v3)
+ | Omaddlimm n, v1::v2::nil => addl v1 (mull v2 (L n))
+ | Omsubl, v1::v2::v3::nil => subl v1 (mull v2 v3)
+ | Onegf, v1::nil => negf v1
+ | Oabsf, v1::nil => absf v1
+ | Oaddf, v1::v2::nil => addf v1 v2
+ | Osubf, v1::v2::nil => subf v1 v2
+ | Omulf, v1::v2::nil => mulf v1 v2
+ | Odivf, v1::v2::nil => divf v1 v2
+ | Ominf, v1::v2::nil => minf v1 v2
+ | Omaxf, v1::v2::nil => maxf v1 v2
+ | Ofmaddf, v1::v2::v3::nil => fmaddf v1 v2 v3
+ | Ofmsubf, v1::v2::v3::nil => fmsubf v1 v2 v3
+ | Onegfs, v1::nil => negfs v1
+ | Oabsfs, v1::nil => absfs v1
+ | Oaddfs, v1::v2::nil => addfs v1 v2
+ | Osubfs, v1::v2::nil => subfs v1 v2
+ | Omulfs, v1::v2::nil => mulfs v1 v2
+ | Odivfs, v1::v2::nil => divfs v1 v2
+ | Ominfs, v1::v2::nil => minfs v1 v2
+ | Omaxfs, v1::v2::nil => maxfs v1 v2
+ | Oinvfs, v1::nil => invfs v1
+ | Ofmaddfs, v1::v2::v3::nil => fmaddfs v1 v2 v3
+ | Ofmsubfs, v1::v2::v3::nil => fmsubfs v1 v2 v3
+ | Osingleoffloat, v1::nil => singleoffloat v1
+ | Ofloatofsingle, v1::nil => floatofsingle v1
+ | Ointoffloat, v1::nil => intoffloat_total v1
+ | Ointuoffloat, v1::nil => intuoffloat_total v1
+ | Ointofsingle, v1::nil => intofsingle_total v1
+ | Ointuofsingle, v1::nil => intuofsingle_total v1
+ | Osingleofint, v1::nil => singleofint v1
+ | Osingleofintu, v1::nil => singleofintu v1
+ | Olongoffloat, v1::nil => longoffloat_total v1
+ | Olonguoffloat, v1::nil => longuoffloat_total v1
+ | Ofloatoflong, v1::nil => floatoflong v1
+ | Ofloatoflongu, v1::nil => floatoflongu v1
+ | Olongofsingle, v1::nil => longofsingle_total v1
+ | Olonguofsingle, v1::nil => longuofsingle_total v1
+ | Osingleoflong, v1::nil => singleoflong v1
+ | Osingleoflongu, v1::nil => singleoflongu v1
+ | Ocmp c, _ => of_optbool (eval_static_condition c vl)
+ | (Oextfz stop start), v0::nil => eval_static_extfz stop start v0
+ | (Oextfs stop start), v0::nil => eval_static_extfs stop start v0
+ | (Oextfzl stop start), v0::nil => eval_static_extfzl stop start v0
+ | (Oextfsl stop start), v0::nil => eval_static_extfsl stop start v0
+ | (Oinsf stop start), v0::v1::nil => eval_static_insf stop start v0 v1
+ | (Oinsfl stop start), v0::v1::nil => eval_static_insfl stop start v0 v1
+ | Osel c ty, v1::v2::vc::nil => select (eval_static_condition0 c vc) v1 v2
+ | Oselimm c imm, v1::vc::nil => select (eval_static_condition0 c vc) v1 (I imm)
+ | Osellimm c imm, v1::vc::nil => select (eval_static_condition0 c vc) v1 (L imm)
+ | _, _ => Vbot
+ end.
+
+Section SOUNDNESS.
+
+Variable bc: block_classification.
+Variable ge: genv.
+Hypothesis GENV: genv_match bc ge.
+Variable sp: block.
+Hypothesis STACK: bc sp = BCstack.
+
+Lemma minf_sound:
+ forall v x w y, vmatch bc v x -> vmatch bc w y -> vmatch bc (ExtValues.minf v w) (minf x y).
+Proof.
+ apply (binop_float_sound bc ExtFloat.min); assumption.
+Qed.
+
+Lemma maxf_sound:
+ forall v x w y, vmatch bc v x -> vmatch bc w y -> vmatch bc (ExtValues.maxf v w) (maxf x y).
+Proof.
+ apply (binop_float_sound bc ExtFloat.max); assumption.
+Qed.
+
+Lemma minfs_sound:
+ forall v x w y, vmatch bc v x -> vmatch bc w y -> vmatch bc (ExtValues.minfs v w) (minfs x y).
+Proof.
+ apply (binop_single_sound bc ExtFloat32.min); assumption.
+Qed.
+
+Lemma maxfs_sound:
+ forall v x w y, vmatch bc v x -> vmatch bc w y -> vmatch bc (ExtValues.maxfs v w) (maxfs x y).
+Proof.
+ apply (binop_single_sound bc ExtFloat32.max); assumption.
+Qed.
+
+Lemma invfs_sound:
+ forall v x, vmatch bc v x -> vmatch bc (ExtValues.invfs v) (invfs x).
+Proof.
+ intros v x;
+ intro MATCH;
+ inversion MATCH;
+ cbn;
+ constructor.
+Qed.
+
+Lemma triple_op_float_sound:
+ forall f a x b y c z,
+ vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.triple_op_float f a b c)
+ (triple_op_float f x y z).
+Proof.
+ intros until z.
+ intros Hax Hby Hcz.
+ inv Hax; cbn; try constructor;
+ inv Hby; cbn; try constructor;
+ inv Hcz; cbn; try constructor.
+Qed.
+
+Lemma triple_op_single_sound:
+ forall f a x b y c z,
+ vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.triple_op_single f a b c)
+ (triple_op_single f x y z).
+Proof.
+ intros until z.
+ intros Hax Hby Hcz.
+ inv Hax; cbn; try constructor;
+ inv Hby; cbn; try constructor;
+ inv Hcz; cbn; try constructor.
+Qed.
+
+Lemma fmaddf_sound :
+ forall a x b y c z, vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.fmaddf a b c) (fmaddf x y z).
+Proof.
+ intros. unfold ExtValues.fmaddf, fmaddf.
+ apply triple_op_float_sound; assumption.
+Qed.
+
+Lemma fmaddfs_sound :
+ forall a x b y c z, vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.fmaddfs a b c) (fmaddfs x y z).
+Proof.
+ intros. unfold ExtValues.fmaddfs, fmaddfs.
+ apply triple_op_single_sound; assumption.
+Qed.
+
+Lemma fmsubf_sound :
+ forall a x b y c z, vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.fmsubf a b c) (fmsubf x y z).
+Proof.
+ intros. unfold ExtValues.fmsubf, fmsubf.
+ apply triple_op_float_sound; assumption.
+Qed.
+
+Lemma fmsubfs_sound :
+ forall a x b y c z, vmatch bc a x -> vmatch bc b y -> vmatch bc c z ->
+ vmatch bc (ExtValues.fmsubfs a b c) (fmsubfs x y z).
+Proof.
+ intros. unfold ExtValues.fmsubfs, fmsubfs.
+ apply triple_op_single_sound; assumption.
+Qed.
+Hint Resolve minf_sound maxf_sound minfs_sound maxfs_sound invfs_sound fmaddf_sound fmaddfs_sound fmsubf_sound fmsubfs_sound : va.
+
+Theorem eval_static_condition_sound:
+ forall cond vargs m aargs,
+ list_forall2 (vmatch bc) vargs aargs ->
+ cmatch (eval_condition cond vargs m) (eval_static_condition cond aargs).
+Proof.
+ intros until aargs; intros VM. inv VM.
+ destruct cond; auto with va.
+ inv H0.
+ destruct cond; cbn; eauto with va.
+ inv H2.
+ destruct cond; cbn; eauto with va.
+ destruct cond; auto with va.
+Qed.
+
+Theorem eval_static_condition0_sound:
+ forall cond varg m aarg,
+ vmatch bc varg aarg ->
+ cmatch (eval_condition0 cond varg m) (eval_static_condition0 cond aarg).
+Proof.
+ intros until aarg; intro VM.
+ destruct cond; cbn; eauto with va.
+Qed.
+
+Lemma symbol_address_sound:
+ forall id ofs,
+ vmatch bc (Genv.symbol_address ge id ofs) (Ptr (Gl id ofs)).
+Proof.
+ intros; apply symbol_address_sound; apply GENV.
+Qed.
+
+Lemma symbol_address_sound_2:
+ forall id ofs,
+ vmatch bc (Genv.symbol_address ge id ofs) (Ifptr (Gl id ofs)).
+Proof.
+ intros. unfold Genv.symbol_address. destruct (Genv.find_symbol ge id) as [b|] eqn:F.
+ constructor. constructor. apply GENV; auto.
+ constructor.
+Qed.
+
+Hint Resolve symbol_address_sound symbol_address_sound_2: va.
+
+Ltac InvHyps :=
+ match goal with
+ | [H: None = Some _ |- _ ] => discriminate
+ | [H: Some _ = Some _ |- _] => inv H
+ | [H1: match ?vl with nil => _ | _ :: _ => _ end = Some _ ,
+ H2: list_forall2 _ ?vl _ |- _ ] => inv H2; InvHyps
+ | [H: (if Archi.ptr64 then _ else _) = Some _ |- _] => destruct Archi.ptr64 eqn:?; InvHyps
+ | _ => idtac
+ end.
+
+Theorem eval_static_addressing_sound:
+ forall addr vargs vres aargs,
+ eval_addressing ge (Vptr sp Ptrofs.zero) addr vargs = Some vres ->
+ list_forall2 (vmatch bc) vargs aargs ->
+ vmatch bc vres (eval_static_addressing addr aargs).
+Proof.
+ unfold eval_addressing, eval_static_addressing; intros;
+ destruct addr; InvHyps; eauto with va.
+ rewrite Ptrofs.add_zero_l; eauto with va.
+Qed.
+
+Theorem eval_static_addressing_sound_none:
+ forall addr vargs aargs,
+ eval_addressing ge (Vptr sp Ptrofs.zero) addr vargs = None ->
+ list_forall2 (vmatch bc) vargs aargs ->
+ (eval_static_addressing addr aargs) = Vbot.
+Proof.
+ unfold eval_addressing, eval_static_addressing.
+ intros until aargs. intros Heval_none Hlist.
+ inv Hlist.
+ destruct addr; trivial; discriminate.
+ inv H0.
+ destruct addr; trivial; discriminate.
+ inv H2.
+ destruct addr; trivial; discriminate.
+ inv H3;
+ destruct addr; trivial; discriminate.
+Qed.
+
+Lemma vmatch_vint_ntop1:
+ forall x y, vmatch bc (Vint x) (ntop1 y).
+Proof.
+ intro. unfold ntop1, provenance.
+ destruct y;
+ destruct (va_strict tt);
+ constructor.
+Qed.
+
+Lemma vmatch_vlong_ntop1:
+ forall x y, vmatch bc (Vlong x) (ntop1 y).
+Proof.
+ intro. unfold ntop1, provenance.
+ destruct y;
+ destruct (va_strict tt);
+ constructor.
+Qed.
+
+Hint Resolve vmatch_vint_ntop1 vmatch_vlong_ntop1: va.
+
+Theorem eval_static_operation_sound:
+ forall op vargs m vres aargs,
+ eval_operation ge (Vptr sp Ptrofs.zero) op vargs m = Some vres ->
+ list_forall2 (vmatch bc) vargs aargs ->
+ vmatch bc vres (eval_static_operation op aargs).
+Proof.
+ unfold eval_operation, eval_static_operation, addx, revsubx, addxl, revsubxl; intros.
+ destruct op; InvHyps; eauto with va.
+ - destruct (propagate_float_constants tt); constructor.
+ - destruct (propagate_float_constants tt); constructor.
+ - rewrite Ptrofs.add_zero_l; eauto with va.
+ - replace(match Val.shl a1 (Vint (int_of_shift1_4 shift)) with
+ | Vint n2 => Vint (Int.add n n2)
+ | Vptr b2 ofs2 =>
+ if Archi.ptr64
+ then Vundef
+ else Vptr b2 (Ptrofs.add ofs2 (Ptrofs.of_int n))
+ | _ => Vundef
+ end) with (Val.add (Vint n) (Val.shl a1 (Vint (int_of_shift1_4 shift)))).
+ + eauto with va.
+ + destruct a1; destruct shift; reflexivity.
+ - (*revsubimm*) inv H1; constructor.
+ - replace (match Val.shl a1 (Vint (int_of_shift1_4 shift)) with
+ | Vint n2 => Vint (Int.sub n n2)
+ | _ => Vundef
+ end) with (Val.sub (Vint n) (Val.shl a1 (Vint (int_of_shift1_4 shift)))).
+ + eauto with va.
+ + destruct n; destruct shift; reflexivity.
+ - (* shrx *)
+ inv H1; cbn; try constructor.
+ all: destruct Int.ltu; [cbn | constructor; fail].
+ all: auto with va.
+ - inv H1; constructor.
+ - replace (match Val.shll a1 (Vint (int_of_shift1_4 shift)) with
+ | Vlong n2 => Vlong (Int64.sub n n2)
+ | _ => Vundef
+ end) with (Val.subl (Vlong n) (Val.shll a1 (Vint (int_of_shift1_4 shift)))).
+ + eauto with va.
+ + destruct a1; destruct shift; reflexivity.
+ - apply of_optbool_sound. eapply eval_static_condition_sound; eauto.
+
+ (* extfz *)
+ - unfold extfz, eval_static_extfz.
+ destruct (is_bitfield _ _).
+ + inv H1; constructor.
+ + constructor.
+
+ (* extfs *)
+ - unfold extfs, eval_static_extfs.
+ destruct (is_bitfield _ _).
+ + inv H1; constructor.
+ + constructor.
+
+ (* extfzl *)
+ - unfold extfzl, eval_static_extfzl.
+ destruct (is_bitfieldl _ _).
+ + inv H1; constructor.
+ + constructor.
+
+ (* extfsl *)
+ - unfold extfsl, eval_static_extfsl.
+ destruct (is_bitfieldl _ _).
+ + inv H1; constructor.
+ + constructor.
+
+ (* insf *)
+ - unfold insf, eval_static_insf.
+ destruct (is_bitfield _ _).
+ + inv H1; inv H0; cbn; try constructor; destruct (Int.ltu _ _); cbn; constructor.
+ + constructor.
+ (* insfl *)
+ - unfold insfl, eval_static_insfl.
+ destruct (is_bitfieldl _ _).
+ + inv H1; inv H0; cbn; try constructor; destruct (Int.ltu _ _); cbn; constructor.
+ + constructor.
+ (* select *)
+ - apply select_sound; auto. eapply eval_static_condition0_sound; eauto.
+ (* select imm *)
+ - apply select_sound; auto with va. eapply eval_static_condition0_sound; eauto.
+ (* select long imm *)
+ - apply select_sound; auto with va. eapply eval_static_condition0_sound; eauto.
+Qed.
+
+End SOUNDNESS.
+
diff --git a/kvx/bitmasks.py b/kvx/bitmasks.py
new file mode 100755
index 00000000..9f6987d6
--- /dev/null
+++ b/kvx/bitmasks.py
@@ -0,0 +1,12 @@
+#!/usr/bin/env python3
+def bitmask(to, fr):
+ bit_to = 1<<to
+ return (bit_to | (bit_to - 1)) & ~((1<<fr)-1)
+
+def bitmask2(to, fr):
+ bit_to = 1<<to
+ return bit_to + (bit_to - (1 << fr))
+
+for stop in range(32):
+ for start in range(stop+1):
+ assert(bitmask(stop, start) == bitmask2(stop, start))
diff --git a/kvx/extractionMachdep.v b/kvx/extractionMachdep.v
new file mode 100644
index 00000000..2e409931
--- /dev/null
+++ b/kvx/extractionMachdep.v
@@ -0,0 +1,32 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* Xavier Leroy INRIA Paris-Rocquencourt *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* Additional extraction directives specific to the RISC-V port *)
+
+Require Archi Asm.
+
+(* Archi *)
+
+Extract Constant Archi.ptr64 => " Configuration.model = ""64"" ".
+Extract Constant Archi.pic_code => "fun () -> false". (* for the time being *)
+
+Extract Constant Peephole.print_found_store =>
+"fun offset x -> Printf.printf ""found offset = %ld\n"" (Camlcoq.camlint_of_coqint offset); x".
+
+(* Asm *)
+(*
+Extract Constant Asm.low_half => "fun _ _ _ -> assert false".
+Extract Constant Asm.high_half => "fun _ _ _ -> assert false".
+*)
diff --git a/kvx/unittest/Makefile b/kvx/unittest/Makefile
new file mode 100644
index 00000000..fcbede2d
--- /dev/null
+++ b/kvx/unittest/Makefile
@@ -0,0 +1,13 @@
+# Needs to be called from CompCert root directory
+# $ make -f kvx/unittest/Makefile postpass_test
+
+include Makefile.extr
+
+TEST_CMX=kvx/unittest/postpass_test.cmx
+
+UNITTEST_OBJS:=$(shell $(MODORDER) $(TEST_CMX))
+
+postpass_test: $(UNITTEST_OBJS)
+ @echo "Linking $@ $(UNITTEST_OBJS)"
+ @$(OCAMLOPT) -o $@ $(LIBS) $(LINK_OPT) $+
+
diff --git a/kvx/unittest/postpass_test.ml b/kvx/unittest/postpass_test.ml
new file mode 100644
index 00000000..434bfaf7
--- /dev/null
+++ b/kvx/unittest/postpass_test.ml
@@ -0,0 +1,12 @@
+open Printf
+open Asmblock
+open Integers
+open PostpassSchedulingOracle
+open BinNums
+
+let test_schedule_sd =
+ let sd_inst = PStore (PStoreRRO (Psd, GPR12, GPR16, (Ofsimm (Ptrofs.of_int @@ Int.intval Z0))))
+ in let bb = { header = []; body = [sd_inst]; exit = None }
+ in List.iter print_bb (smart_schedule bb)
+
+let _ = test_schedule_sd; printf "Done\n"
diff --git a/lib/Coqlib.v b/lib/Coqlib.v
index 045fb03a..19c641e3 100644
--- a/lib/Coqlib.v
+++ b/lib/Coqlib.v
@@ -1136,6 +1136,41 @@ Proof.
induction 1; intros. auto. apply IHis_tail. eapply is_tail_cons_left; eauto.
Qed.
+Lemma is_tail_app A (l1: list A): forall l2, is_tail l2 (l1 ++ l2).
+Proof.
+ induction l1; cbn; auto with coqlib.
+Qed.
+Hint Resolve is_tail_app: coqlib.
+
+Lemma is_tail_app_inv A (l1: list A): forall l2 l3, is_tail (l1 ++ l2) l3 -> is_tail l2 l3.
+Proof.
+ induction l1; cbn; auto with coqlib.
+ intros l2 l3 H; inversion H; eauto with coqlib.
+Qed.
+Global Hint Resolve is_tail_app_inv: coqlib.
+
+Lemma is_tail_app_right A (l2 l1: list A): is_tail l1 (l2++l1).
+Proof.
+ intros; eauto with coqlib.
+Qed.
+
+Lemma is_tail_app_def A (l1 l2: list A):
+ is_tail l1 l2 -> exists l3, l2 = l3 ++ l1.
+Proof.
+ induction 1 as [|x l1 l2]; simpl.
+ - exists nil; simpl; auto.
+ - destruct IHis_tail as (l3 & EQ); rewrite EQ.
+ exists (x::l3); simpl; auto.
+Qed.
+
+Lemma is_tail_bound A (l1 l2: list A):
+ is_tail l1 l2 -> (length l1 <= length l2)%nat.
+Proof.
+ intros H; destruct (is_tail_app_def H) as (l3 & EQ).
+ subst; rewrite app_length.
+ lia.
+Qed.
+
(** [list_forall2 P [x1 ... xN] [y1 ... yM]] holds iff [N = M] and
[P xi yi] holds for all [i]. *)
@@ -1388,3 +1423,9 @@ Lemma nlist_forall2_imply:
Proof.
induction 1; simpl; intros; constructor; auto.
Qed.
+
+Lemma if_same : forall {T : Type} (b : bool) (x : T),
+ (if b then x else x) = x.
+Proof.
+ destruct b; trivial.
+Qed.
diff --git a/lib/Floats.v b/lib/Floats.v
index 43caebb0..9ee5302d 100644
--- a/lib/Floats.v
+++ b/lib/Floats.v
@@ -17,7 +17,7 @@
(** Formalization of floating-point numbers, using the Flocq library. *)
-Require Import Coqlib Zbits Integers.
+Require Import Coqlib Zbits Integers Axioms.
From Flocq Require Import Binary Bits Core.
Require Import IEEE754_extra.
Require Import Program.
@@ -29,8 +29,69 @@ Open Scope Z_scope.
Set Asymmetric Patterns.
Definition float := binary64. (**r the type of IEE754 double-precision FP numbers *)
+
+Definition float_eq: forall (i1 i2: float), {i1=i2} + {i1<>i2}.
+Proof.
+ intros. destruct i1.
+(* B754_zero *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + apply eqb_prop in BEQ. subst. left. reflexivity.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_infinity *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + apply eqb_prop in BEQ. subst. left. reflexivity.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_nan *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + generalize (Pos.eq_dec pl pl0). intro. inv H.
+ ++ left. apply eqb_prop in BEQ. subst.
+ assert (e = e0) by (apply proof_irr). congruence.
+ ++ right. intro. inv H. contradiction.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_finite *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ; [apply eqb_prop in BEQ | apply eqb_false_iff in BEQ].
+ generalize (Pos.eq_dec m m0). intro. inv H.
+ generalize (Z.eq_dec e e1). intro. inv H.
+ 1: { left. assert (e0 = e2) by (apply proof_irr). congruence. }
+ all: right; intro; inv H; contradiction.
+Qed.
+
Definition float32 := binary32. (**r the type of IEE754 single-precision FP numbers *)
+Definition float32_eq: forall (i1 i2: float32), {i1=i2} + {i1<>i2}.
+Proof.
+ intros. destruct i1.
+(* B754_zero *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + apply eqb_prop in BEQ. subst. left. reflexivity.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_infinity *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + apply eqb_prop in BEQ. subst. left. reflexivity.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_nan *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ.
+ + generalize (Pos.eq_dec pl pl0). intro. inv H.
+ ++ left. apply eqb_prop in BEQ. subst.
+ assert (e = e0) by (apply proof_irr). congruence.
+ ++ right. intro. inv H. contradiction.
+ + apply eqb_false_iff in BEQ. right. intro. inv H. contradiction.
+(* B754_finite *)
+ - destruct i2; try (right; discriminate).
+ destruct (eqb s s0) eqn:BEQ; [apply eqb_prop in BEQ | apply eqb_false_iff in BEQ].
+ generalize (Pos.eq_dec m m0). intro. inv H.
+ generalize (Z.eq_dec e e1). intro. inv H.
+ 1: { left. assert (e0 = e2) by (apply proof_irr). congruence. }
+ all: right; intro; inv H; contradiction.
+Qed.
+
(** Boolean-valued comparisons *)
Definition cmp_of_comparison (c: comparison) (x: option Datatypes.comparison) : bool :=
diff --git a/lib/HashedSet.v b/lib/HashedSet.v
new file mode 100644
index 00000000..48798a1b
--- /dev/null
+++ b/lib/HashedSet.v
@@ -0,0 +1,1414 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import ZArith.
+Require Import Bool.
+Require Import List.
+Require Coq.Logic.Eqdep_dec.
+
+(* begin from Maps *)
+Fixpoint prev_append (i j: positive) {struct i} : positive :=
+ match i with
+ | xH => j
+ | xI i' => prev_append i' (xI j)
+ | xO i' => prev_append i' (xO j)
+ end.
+
+Definition prev (i: positive) : positive :=
+ prev_append i xH.
+
+Lemma prev_append_prev i j:
+ prev (prev_append i j) = prev_append j i.
+Proof.
+ revert j. unfold prev.
+ induction i as [i IH|i IH|]. 3: reflexivity.
+ intros j. simpl. rewrite IH. reflexivity.
+ intros j. simpl. rewrite IH. reflexivity.
+Qed.
+
+Lemma prev_involutive i :
+ prev (prev i) = i.
+Proof (prev_append_prev i xH).
+
+Lemma prev_append_inj i j j' :
+ prev_append i j = prev_append i j' -> j = j'.
+Proof.
+ revert j j'.
+ induction i as [i Hi|i Hi|]; intros j j' H; auto;
+ specialize (Hi _ _ H); congruence.
+Qed.
+
+(* end from Maps *)
+
+Lemma orb_idem: forall b, orb b b = b.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Lemma andb_idem: forall b, andb b b = b.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Lemma andb_negb_false: forall b, andb b (negb b) = false.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Hint Rewrite orb_false_r andb_false_r andb_true_r orb_true_r orb_idem andb_idem andb_negb_false : pset.
+
+Module PSet_internals.
+Inductive pset : Type :=
+| Empty : pset
+| Node : pset -> bool -> pset -> pset.
+
+Definition empty := Empty.
+
+Definition is_empty x :=
+ match x with
+ | Empty => true
+ | Node _ _ _ => false
+ end.
+
+Fixpoint wf x :=
+ match x with
+ | Empty => true
+ | Node b0 f b1 =>
+ (wf b0) && (wf b1) &&
+ ((negb (is_empty b0)) || f || (negb (is_empty b1)))
+ end.
+
+Definition iswf x := (wf x)=true.
+
+Lemma empty_wf : iswf empty.
+Proof.
+ reflexivity.
+Qed.
+
+Definition pset_eq :
+ forall s s': pset, { s=s' } + { s <> s' }.
+Proof.
+ induction s; destruct s'; repeat decide equality.
+Qed.
+
+Fixpoint contains (s : pset) (i : positive) {struct i} : bool :=
+ match s with
+ | Empty => false
+ | Node b0 f b1 =>
+ match i with
+ | xH => f
+ | xO ii => contains b0 ii
+ | xI ii => contains b1 ii
+ end
+ end.
+
+Lemma gempty :
+ forall i : positive,
+ contains Empty i = false.
+Proof.
+ destruct i; simpl; reflexivity.
+Qed.
+
+Global Hint Resolve gempty : pset.
+Hint Rewrite gempty : pset.
+
+Definition node (b0 : pset) (f : bool) (b1 : pset) : pset :=
+ match b0, f, b1 with
+ | Empty, false, Empty => Empty
+ | _, _, _ => Node b0 f b1
+ end.
+
+Lemma wf_node :
+ forall b0 f b1,
+ iswf b0 -> iswf b1 -> iswf (node b0 f b1).
+Proof.
+ destruct b0; destruct f; destruct b1; simpl.
+ all: unfold iswf; simpl; intros; trivial.
+ all: autorewrite with pset; trivial.
+ all: rewrite H.
+ all: rewrite H0.
+ all: reflexivity.
+Qed.
+
+Global Hint Resolve wf_node: pset.
+
+Lemma gnode :
+ forall b0 f b1 i,
+ contains (node b0 f b1) i =
+ contains (Node b0 f b1) i.
+Proof.
+ destruct b0; simpl; trivial.
+ destruct f; simpl; trivial.
+ destruct b1; simpl; trivial.
+ intro.
+ rewrite gempty.
+ destruct i; simpl; trivial.
+ all: symmetry; apply gempty.
+Qed.
+
+Hint Rewrite gnode : pset.
+
+Fixpoint add (i : positive) (s : pset) {struct i} : pset :=
+ match s with
+ | Empty =>
+ match i with
+ | xH => Node Empty true Empty
+ | xO ii => Node (add ii Empty) false Empty
+ | xI ii => Node Empty false (add ii Empty)
+ end
+ | Node b0 f b1 =>
+ match i with
+ | xH => Node b0 true b1
+ | xO ii => Node (add ii b0) f b1
+ | xI ii => Node b0 f (add ii b1)
+ end
+ end.
+
+Lemma add_nonempty:
+ forall i s, is_empty (add i s) = false.
+Proof.
+ induction i; destruct s; simpl; trivial.
+Qed.
+
+Hint Rewrite add_nonempty : pset.
+Global Hint Resolve add_nonempty : pset.
+
+Lemma wf_add:
+ forall i s, (iswf s) -> (iswf (add i s)).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: unfold iswf in *; simpl.
+ all: autorewrite with pset; simpl; trivial.
+ 1,3: auto with pset.
+ all: intro Z.
+ all: repeat rewrite andb_true_iff in Z.
+ all: intuition.
+Qed.
+
+Global Hint Resolve wf_add : pset.
+
+Theorem gadds :
+ forall i : positive,
+ forall s : pset,
+ contains (add i s) i = true.
+Proof.
+ induction i; destruct s; simpl; auto.
+Qed.
+
+Global Hint Resolve gadds : pset.
+Hint Rewrite gadds : pset.
+
+Theorem gaddo :
+ forall i j : positive,
+ forall s : pset,
+ i <> j ->
+ contains (add i s) j = contains s j.
+Proof.
+ induction i; destruct j; destruct s; simpl; intro; auto with pset.
+ 5, 6: congruence.
+ all: rewrite IHi by congruence.
+ all: trivial.
+ all: apply gempty.
+Qed.
+
+Global Hint Resolve gaddo : pset.
+
+Fixpoint remove (i : positive) (s : pset) { struct i } : pset :=
+ match i with
+ | xH =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node b0 false b1
+ end
+ | xO ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node (remove ii b0) f b1
+ end
+ | xI ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node b0 f (remove ii b1)
+ end
+ end.
+
+Lemma wf_remove :
+ forall i s, (iswf s) -> (iswf (remove i s)).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: unfold iswf in *; simpl.
+ all: intro Z.
+ all: repeat rewrite andb_true_iff in Z.
+ all: apply wf_node.
+ all: intuition.
+ all: apply IHi.
+ all: assumption.
+Qed.
+
+
+Fixpoint remove_noncanon (i : positive) (s : pset) { struct i } : pset :=
+ match i with
+ | xH =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node b0 false b1
+ end
+ | xO ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node (remove_noncanon ii b0) f b1
+ end
+ | xI ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node b0 f (remove_noncanon ii b1)
+ end
+ end.
+
+Lemma remove_noncanon_same:
+ forall i j s, (contains (remove i s) j) = (contains (remove_noncanon i s) j).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: rewrite gnode.
+ 3: reflexivity.
+ all: destruct j; simpl; trivial.
+Qed.
+
+Lemma remove_empty :
+ forall i, remove i Empty = Empty.
+Proof.
+ induction i; simpl; trivial.
+Qed.
+
+Hint Rewrite remove_empty : pset.
+Global Hint Resolve remove_empty : pset.
+
+Lemma gremove_noncanon_s :
+ forall i : positive,
+ forall s : pset,
+ contains (remove_noncanon i s) i = false.
+Proof.
+ induction i; destruct s; simpl; trivial.
+Qed.
+
+Theorem gremoves :
+ forall i : positive,
+ forall s : pset,
+ contains (remove i s) i = false.
+Proof.
+ intros.
+ rewrite remove_noncanon_same.
+ apply gremove_noncanon_s.
+Qed.
+
+Global Hint Resolve gremoves : pset.
+Hint Rewrite gremoves : pset.
+
+Lemma gremove_noncanon_o :
+ forall i j : positive,
+ forall s : pset,
+ i<>j ->
+ contains (remove_noncanon i s) j = contains s j.
+Proof.
+ induction i; destruct j; destruct s; simpl; intro; trivial.
+ 1, 2: rewrite IHi by congruence.
+ 1, 2: reflexivity.
+ congruence.
+Qed.
+
+Theorem gremoveo :
+ forall i j : positive,
+ forall s : pset,
+ i<>j ->
+ contains (remove i s) j = contains s j.
+Proof.
+ intros.
+ rewrite remove_noncanon_same.
+ apply gremove_noncanon_o.
+ assumption.
+Qed.
+
+Global Hint Resolve gremoveo : pset.
+
+Fixpoint union_nonopt (s s' : pset) : pset :=
+ match s, s' with
+ | Empty, _ => s'
+ | _, Empty => s
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ Node (union_nonopt b0 b0') (orb f f') (union_nonopt b1 b1')
+ end.
+
+Theorem gunion_nonopt:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (union_nonopt s s')) j = orb (contains s j) (contains s' j).
+Proof.
+ induction s; destruct s'; intro; simpl; autorewrite with pset; simpl; trivial.
+ destruct j; simpl; trivial.
+Qed.
+
+
+Fixpoint union (s s' : pset) : pset :=
+ if pset_eq s s' then s else
+ match s, s' with
+ | Empty, _ => s'
+ | _, Empty => s
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ Node (union b0 b0') (orb f f') (union b1 b1')
+ end.
+
+Lemma union_nonempty1:
+ forall s s',
+ (is_empty s) = false -> is_empty (union s s')= false.
+Proof.
+ induction s; destruct s'; simpl; try discriminate.
+ all: destruct pset_eq; simpl; trivial.
+Qed.
+
+Lemma union_nonempty2:
+ forall s s',
+ (is_empty s') = false -> is_empty (union s s')= false.
+Proof.
+ induction s; destruct s'; simpl; try discriminate.
+ all: destruct pset_eq; simpl; trivial; discriminate.
+Qed.
+
+Global Hint Resolve union_nonempty1 union_nonempty2 : pset.
+
+Lemma wf_union :
+ forall s s', (iswf s) -> (iswf s') -> (iswf (union s s')).
+Proof.
+ induction s; destruct s'; intros; simpl.
+ all: destruct pset_eq; trivial.
+ unfold iswf in *. simpl in *.
+ repeat rewrite andb_true_iff in H.
+ repeat rewrite andb_true_iff in H0.
+ rewrite IHs1.
+ rewrite IHs2.
+ simpl.
+ all: intuition.
+ repeat rewrite orb_true_iff in H2, H3.
+ repeat rewrite negb_true_iff in H2, H3.
+ repeat rewrite orb_true_iff.
+ repeat rewrite negb_true_iff.
+ intuition auto with pset.
+Qed.
+
+Global Hint Resolve wf_union : pset.
+
+Theorem gunion:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (union s s')) j = orb (contains s j) (contains s' j).
+Proof.
+ induction s; destruct s'; intro; simpl.
+ all: destruct pset_eq as [EQ | NEQ]; try congruence.
+ all: autorewrite with pset; simpl; trivial.
+ - rewrite <- EQ.
+ symmetry.
+ apply orb_idem.
+ - destruct j; simpl; trivial.
+Qed.
+
+Fixpoint inter_noncanon (s s' : pset) : pset :=
+ if pset_eq s s' then s else
+ match s, s' with
+ | Empty, _ | _, Empty => Empty
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ Node (inter_noncanon b0 b0') (andb f f') (inter_noncanon b1 b1')
+ end.
+
+Lemma ginter_noncanon:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (inter_noncanon s s')) j = andb (contains s j) (contains s' j).
+Proof.
+ induction s; destruct s'; intro; simpl.
+ all: destruct pset_eq as [EQ | NEQ]; try congruence.
+ all: autorewrite with pset; simpl; trivial.
+ - rewrite <- EQ.
+ symmetry.
+ apply andb_idem.
+ - destruct j; simpl; trivial.
+Qed.
+
+Fixpoint inter (s s' : pset) : pset :=
+ if pset_eq s s' then s else
+ match s, s' with
+ | Empty, _ | _, Empty => Empty
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ node (inter b0 b0') (andb f f') (inter b1 b1')
+ end.
+
+Lemma wf_inter :
+ forall s s', (iswf s) -> (iswf s') -> (iswf (inter s s')).
+Proof.
+ induction s; destruct s'; intros; simpl.
+ all: destruct pset_eq; trivial.
+ unfold iswf in H, H0.
+ simpl in H, H0.
+ repeat rewrite andb_true_iff in H.
+ repeat rewrite andb_true_iff in H0.
+ fold (iswf s1) in *.
+ fold (iswf s2) in *.
+ intuition.
+Qed.
+
+Global Hint Resolve wf_inter : pset.
+
+Lemma inter_noncanon_same:
+ forall s s' j, (contains (inter s s') j) = (contains (inter_noncanon s s') j).
+Proof.
+ induction s; destruct s'; simpl; trivial.
+ destruct pset_eq; trivial.
+ destruct j; rewrite gnode; simpl; auto.
+Qed.
+
+Theorem ginter:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (inter s s')) j = andb (contains s j) (contains s' j).
+Proof.
+ intros.
+ rewrite inter_noncanon_same.
+ apply ginter_noncanon.
+Qed.
+
+Global Hint Resolve ginter gunion : pset.
+Hint Rewrite ginter gunion : pset.
+
+Fixpoint subtract_noncanon (s s' : pset) : pset :=
+ if pset_eq s s' then Empty else
+ match s, s' with
+ | Empty, _ => Empty
+ | _, Empty => s
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ Node (subtract_noncanon b0 b0') (andb f (negb f')) (subtract_noncanon b1 b1')
+ end.
+
+Lemma gsubtract_noncanon:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (subtract_noncanon s s')) j = andb (contains s j) (negb (contains s' j)).
+Proof.
+ induction s; destruct s'; intro; simpl.
+ all: destruct pset_eq as [EQ | NEQ]; try congruence.
+ all: autorewrite with pset; simpl; trivial.
+ - rewrite <- EQ.
+ symmetry.
+ apply andb_negb_false.
+ - destruct j; simpl; trivial.
+Qed.
+
+Fixpoint subtract (s s' : pset) : pset :=
+ if pset_eq s s' then Empty else
+ match s, s' with
+ | Empty, _ => Empty
+ | _, Empty => s
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ node (subtract b0 b0') (andb f (negb f')) (subtract b1 b1')
+ end.
+
+Lemma wf_subtract :
+ forall s s', (iswf s) -> (iswf s') -> (iswf (subtract s s')).
+Proof.
+ induction s; destruct s'; intros; simpl.
+ all: destruct pset_eq; trivial.
+ reflexivity.
+
+ unfold iswf in H, H0.
+ simpl in H, H0.
+
+ repeat rewrite andb_true_iff in H.
+ repeat rewrite andb_true_iff in H0.
+ fold (iswf s1) in *.
+ fold (iswf s2) in *.
+ intuition.
+Qed.
+
+Global Hint Resolve wf_subtract : pset.
+
+Lemma subtract_noncanon_same:
+ forall s s' j, (contains (subtract s s') j) = (contains (subtract_noncanon s s') j).
+Proof.
+ induction s; destruct s'; simpl; trivial.
+ destruct pset_eq; trivial.
+ destruct j; rewrite gnode; simpl; auto.
+Qed.
+
+Theorem gsubtract:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (subtract s s')) j = andb (contains s j) (negb (contains s' j)).
+Proof.
+ intros.
+ rewrite subtract_noncanon_same.
+ apply gsubtract_noncanon.
+Qed.
+
+Global Hint Resolve gsubtract : pset.
+Hint Rewrite gsubtract : pset.
+
+Lemma wf_is_nonempty :
+ forall s, iswf s -> is_empty s = false -> exists i, contains s i = true.
+Proof.
+ induction s; simpl; trivial.
+ discriminate.
+ intro WF.
+ unfold iswf in WF.
+ simpl in WF.
+ repeat rewrite andb_true_iff in WF.
+ repeat rewrite orb_true_iff in WF.
+ repeat rewrite negb_true_iff in WF.
+ fold (iswf s1) in WF.
+ fold (iswf s2) in WF.
+ intuition.
+ - destruct H5 as [i K].
+ exists (xO i).
+ simpl.
+ assumption.
+ - exists xH.
+ simpl.
+ assumption.
+ - destruct H5 as [i K].
+ exists (xI i).
+ simpl.
+ assumption.
+Qed.
+
+Global Hint Resolve wf_is_nonempty : pset.
+
+Lemma wf_is_empty1 :
+ forall s, iswf s -> (forall i, (contains s i) = false) -> is_empty s = true.
+Proof.
+ induction s; trivial.
+ intro WF.
+ unfold iswf in WF.
+ simpl in WF.
+ repeat rewrite andb_true_iff in WF.
+ fold (iswf s1) in WF.
+ fold (iswf s2) in WF.
+ intro ALL.
+ intuition.
+ exfalso.
+ repeat rewrite orb_true_iff in H0.
+ repeat rewrite negb_true_iff in H0.
+ intuition.
+ - rewrite H in H0. discriminate.
+ intro i.
+ specialize ALL with (xO i).
+ simpl in ALL.
+ assumption.
+ - specialize ALL with xH.
+ simpl in ALL.
+ congruence.
+ - rewrite H3 in H4. discriminate.
+ intro i.
+ specialize ALL with (xI i).
+ simpl in ALL.
+ assumption.
+Qed.
+
+Global Hint Resolve wf_is_empty1 : pset.
+
+Lemma wf_eq :
+ forall s s', iswf s -> iswf s' -> s <> s' ->
+ exists i, (contains s i) <> (contains s' i).
+Proof.
+ induction s; destruct s'; intros WF WF' DIFF; simpl.
+ - congruence.
+ - assert (exists i, (contains (Node s'1 b s'2) i)= true) as K by auto with pset.
+ destruct K as [i Z].
+ exists i.
+ rewrite Z.
+ rewrite gempty.
+ discriminate.
+ - assert (exists i, (contains (Node s1 b s2) i)= true) as K by auto with pset.
+ destruct K as [i Z].
+ exists i.
+ rewrite Z.
+ rewrite gempty.
+ discriminate.
+ - destruct (pset_eq s1 s'1).
+ + subst s'1.
+ destruct (pset_eq s2 s'2).
+ * subst s'2.
+ exists xH.
+ simpl.
+ congruence.
+ * specialize IHs2 with s'2.
+ unfold iswf in WF.
+ simpl in WF.
+ repeat rewrite andb_true_iff in WF.
+ fold (iswf s1) in WF.
+ fold (iswf s2) in WF.
+ unfold iswf in WF'.
+ simpl in WF'.
+ repeat rewrite andb_true_iff in WF'.
+ fold (iswf s'2) in WF'.
+ intuition.
+ destruct H1 as [i K].
+ exists (xI i).
+ simpl.
+ assumption.
+ + specialize IHs1 with s'1.
+ unfold iswf in WF.
+ simpl in WF.
+ repeat rewrite andb_true_iff in WF.
+ fold (iswf s1) in WF.
+ fold (iswf s2) in WF.
+ unfold iswf in WF'.
+ simpl in WF'.
+ repeat rewrite andb_true_iff in WF'.
+ fold (iswf s'1) in WF'.
+ fold (iswf s'2) in WF'.
+ intuition.
+ destruct H1 as [i K].
+ exists (xO i).
+ simpl.
+ assumption.
+Qed.
+
+Theorem eq_correct:
+ forall s s',
+ (iswf s) -> (iswf s') ->
+ (forall i, (contains s i) = (contains s' i)) <-> s = s'.
+Proof.
+ intros s s' WF WF'.
+ split.
+ {
+ intro ALL.
+ destruct (pset_eq s s') as [ | INEQ]; trivial.
+ exfalso.
+ destruct (wf_eq s s' WF WF' INEQ) as [i K].
+ specialize ALL with i.
+ congruence.
+ }
+ intro EQ.
+ subst s'.
+ trivial.
+Qed.
+
+Lemma wf_irrelevant:
+ forall s,
+ forall WF WF' : iswf s, WF = WF'.
+Proof.
+ unfold iswf.
+ intros.
+ apply Coq.Logic.Eqdep_dec.eq_proofs_unicity_on.
+ decide equality.
+Qed.
+
+Fixpoint xelements (s : pset) (i : positive)
+ (k: list positive) {struct s}
+ : list positive :=
+ match s with
+ | Empty => k
+ | Node b0 false b1 =>
+ xelements b0 (xO i) (xelements b1 (xI i) k)
+ | Node b0 true b1 =>
+ xelements b0 (xO i) ((prev i) :: xelements b1 (xI i) k)
+ end.
+
+Definition elements (m : pset) := xelements m xH nil.
+
+ Remark xelements_append:
+ forall (m: pset) i k1 k2,
+ xelements m i (k1 ++ k2) = xelements m i k1 ++ k2.
+ Proof.
+ induction m; intros; simpl.
+ - auto.
+ - destruct b; rewrite IHm2; rewrite <- IHm1; auto.
+ Qed.
+
+ Remark xelements_empty:
+ forall i, xelements Empty i nil = nil.
+ Proof.
+ intros; reflexivity.
+ Qed.
+
+ Remark xelements_node:
+ forall (m1: pset) o (m2: pset) i,
+ xelements (Node m1 o m2) i nil =
+ xelements m1 (xO i) nil
+ ++ (if o then (prev i) :: nil else nil)
+ ++ xelements m2 (xI i) nil.
+ Proof.
+ intros. simpl. destruct o; simpl;
+ rewrite <- xelements_append; trivial.
+ Qed.
+
+ Lemma xelements_incl:
+ forall (m: pset) (i : positive) k x,
+ In x k -> In x (xelements m i k).
+ Proof.
+ induction m; intros; simpl.
+ auto.
+ destruct b.
+ apply IHm1. simpl; right; auto.
+ auto.
+ Qed.
+
+ Lemma xelements_correct:
+ forall (m: pset) (i j : positive) k,
+ contains m i=true -> In (prev (prev_append i j)) (xelements m j k).
+ Proof.
+ induction m; intros; simpl.
+ - rewrite gempty in H. discriminate.
+ - destruct b; destruct i; simpl; simpl in H; auto.
+ + apply xelements_incl. simpl.
+ right. auto.
+ + apply xelements_incl. simpl.
+ left. trivial.
+ + apply xelements_incl. auto.
+ + discriminate.
+ Qed.
+
+ Theorem elements_correct:
+ forall (m: pset) (i: positive),
+ contains m i = true -> In i (elements m).
+ Proof.
+ intros m i H.
+ generalize (xelements_correct m i xH nil H). rewrite prev_append_prev. exact id.
+ Qed.
+
+ Lemma in_xelements:
+ forall (m: pset) (i k: positive),
+ In k (xelements m i nil) ->
+ exists j, k = prev (prev_append j i) /\ contains m j = true.
+ Proof.
+ induction m; intros.
+ - rewrite xelements_empty in H. contradiction.
+ - rewrite xelements_node in H. rewrite ! in_app_iff in H. destruct H as [P | [P | P]].
+ + specialize IHm1 with (k := k) (i := xO i).
+ intuition.
+ destruct H as [j [Q R]].
+ exists (xO j).
+ auto.
+ + destruct b; simpl in P; intuition auto. subst k. exists xH; auto.
+ + specialize IHm2 with (k := k) (i := xI i).
+ intuition.
+ destruct H as [j [Q R]].
+ exists (xI j).
+ auto.
+ Qed.
+
+ Theorem elements_complete:
+ forall (m: pset) (i: positive),
+ In i (elements m) -> contains m i = true.
+ Proof.
+ unfold elements. intros m i H.
+ destruct (in_xelements m 1 i H) as [j [P Q]].
+ rewrite prev_append_prev in P. change i with (prev_append 1 i) in P.
+ replace j with i in * by (apply prev_append_inj; auto).
+ assumption.
+ Qed.
+
+
+ Fixpoint xfold {B: Type} (f: B -> positive -> B)
+ (i: positive) (m: pset) (v: B) {struct m} : B :=
+ match m with
+ | Empty => v
+ | Node l false r =>
+ let v1 := xfold f (xO i) l v in
+ xfold f (xI i) r v1
+ | Node l true r =>
+ let v1 := xfold f (xO i) l v in
+ let v2 := f v1 (prev i) in
+ xfold f (xI i) r v2
+ end.
+
+ Definition fold {B : Type} (f: B -> positive -> B) (m: pset) (v: B) :=
+ xfold f xH m v.
+
+
+ Lemma xfold_xelements:
+ forall {B: Type} (f: B -> positive -> B) m i v l,
+ List.fold_left f l (xfold f i m v) =
+ List.fold_left f (xelements m i l) v.
+ Proof.
+ induction m; intros; simpl; trivial.
+ destruct b; simpl.
+ all: rewrite <- IHm1; simpl; rewrite <- IHm2; trivial.
+ Qed.
+
+ Theorem fold_spec:
+ forall {B: Type} (f: B -> positive -> B) (v: B) (m: pset),
+ fold f m v =
+ List.fold_left f (elements m) v.
+ Proof.
+ intros. unfold fold, elements. rewrite <- xfold_xelements. auto.
+ Qed.
+
+ Fixpoint is_subset (s s' : pset) {struct s} :=
+ if pset_eq s s' then true else
+ match s, s' with
+ | Empty, _ => true
+ | _, Empty => false
+ | (Node b0 f b1), (Node b0' f' b1') =>
+ ((negb f) || f') &&
+ (is_subset b0 b0') &&
+ (is_subset b1 b1')
+ end.
+
+ Theorem is_subset_spec1:
+ forall s s',
+ is_subset s s' = true ->
+ (forall i, contains s i = true -> contains s' i = true).
+ Proof.
+ induction s; destruct s'; simpl; intros; trivial.
+ all: destruct pset_eq.
+ all: try discriminate.
+ all: try rewrite gempty in *.
+ all: try discriminate.
+ { congruence.
+ }
+ repeat rewrite andb_true_iff in H.
+ repeat rewrite orb_true_iff in H.
+ repeat rewrite negb_true_iff in H.
+ specialize IHs1 with (s' := s'1).
+ specialize IHs2 with (s' := s'2).
+ intuition.
+ - destruct i; simpl in *; auto. congruence.
+ - destruct i; simpl in *; auto.
+ Qed.
+
+ Theorem is_subset_spec2:
+ forall s s',
+ iswf s ->
+ (forall i, contains s i = true -> contains s' i = true) ->
+ is_subset s s' = true.
+ Proof.
+ induction s; destruct s'; simpl.
+ all: intro WF.
+ all: unfold iswf in WF.
+ all: simpl in WF.
+ all: repeat rewrite andb_true_iff in WF.
+ all: destruct pset_eq; trivial.
+ all: fold (iswf s1) in WF.
+ all: fold (iswf s2) in WF.
+ - repeat rewrite orb_true_iff in WF.
+ repeat rewrite negb_true_iff in WF.
+ intuition.
+ + destruct (wf_is_nonempty s1 H2 H1) as [i K].
+ specialize H with (xO i).
+ simpl in H.
+ auto.
+ + specialize H with xH.
+ simpl in H.
+ auto.
+ + destruct (wf_is_nonempty s2 H3 H0) as [i K].
+ specialize H with (xI i).
+ simpl in H.
+ auto.
+ - intro CONTAINS.
+ repeat rewrite andb_true_iff.
+ specialize IHs1 with (s' := s'1).
+ specialize IHs2 with (s' := s'2).
+ intuition.
+ + specialize CONTAINS with xH.
+ simpl in CONTAINS.
+ destruct b; destruct b0; intuition congruence.
+ + apply H.
+ intros.
+ specialize CONTAINS with (xO i).
+ simpl in CONTAINS.
+ auto.
+ + apply H3.
+ intros.
+ specialize CONTAINS with (xI i).
+ simpl in CONTAINS.
+ auto.
+ Qed.
+
+ Fixpoint xfilter (fn : positive -> bool)
+ (s : pset) (i : positive) {struct s} : pset :=
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 =>
+ node (xfilter fn b0 (xO i))
+ (f && (fn (prev i)))
+ (xfilter fn b1 (xI i))
+ end.
+
+ Lemma gxfilter:
+ forall fn s j i,
+ contains (xfilter fn s i) j =
+ contains s j &&
+ (fn (prev (prev_append j i))).
+ Proof.
+ induction s; simpl; intros; trivial.
+ {
+ rewrite gempty.
+ trivial.
+ }
+ rewrite gnode.
+ destruct j; simpl; auto.
+ Qed.
+
+ Definition filter (fn : positive -> bool) m := xfilter fn m xH.
+
+ Lemma gfilter:
+ forall fn s j,
+ contains (filter fn s) j =
+ contains s j && (fn j).
+ Proof.
+ intros.
+ unfold filter.
+ rewrite gxfilter.
+ rewrite prev_append_prev.
+ reflexivity.
+ Qed.
+
+ Lemma wf_xfilter:
+ forall fn s j,
+ iswf s -> iswf (xfilter fn s j).
+ Proof.
+ induction s; intros; trivial.
+ simpl.
+ unfold iswf in H.
+ simpl in H.
+ repeat rewrite andb_true_iff in H.
+ fold (iswf s1) in H.
+ fold (iswf s2) in H.
+ intuition.
+ Qed.
+
+ Lemma wf_filter:
+ forall fn s,
+ iswf s -> iswf (filter fn s).
+ Proof.
+ intros.
+ apply wf_xfilter; auto.
+ Qed.
+End PSet_internals.
+
+Module Type POSITIVE_SET.
+Parameter t : Type.
+Parameter empty : t.
+
+Parameter contains: t -> positive -> bool.
+
+Axiom gempty :
+ forall i : positive,
+ contains empty i = false.
+
+Parameter add : positive -> t -> t.
+
+Axiom gaddo :
+ forall i j : positive,
+ forall s : t,
+ i <> j ->
+ contains (add i s) j = contains s j.
+
+Axiom gadds :
+ forall i : positive,
+ forall s : t,
+ contains (add i s) i = true.
+
+Parameter remove : positive -> t -> t.
+
+Axiom gremoves :
+ forall i : positive,
+ forall s : t,
+ contains (remove i s) i = false.
+
+Axiom gremoveo :
+ forall i j : positive,
+ forall s : t,
+ i<>j ->
+ contains (remove i s) j = contains s j.
+
+Parameter union : t -> t -> t.
+
+Axiom gunion:
+ forall s s' : t,
+ forall j : positive,
+ (contains (union s s')) j = orb (contains s j) (contains s' j).
+
+Parameter inter : t -> t -> t.
+
+Axiom ginter:
+ forall s s' : t,
+ forall j : positive,
+ (contains (inter s s')) j = andb (contains s j) (contains s' j).
+
+Parameter subtract : t -> t -> t.
+
+Axiom gsubtract:
+ forall s s' : t,
+ forall j : positive,
+ (contains (subtract s s')) j = andb (contains s j) (negb (contains s' j)).
+
+Axiom uneq_exists :
+ forall s s', s <> s' ->
+ exists i, (contains s i) <> (contains s' i).
+
+Parameter eq:
+ forall s s' : t, {s = s'} + {s <> s'}.
+
+Axiom eq_spec :
+ forall s s',
+ (forall i, (contains s i) = (contains s' i)) <-> s = s'.
+
+Parameter elements : t -> list positive.
+
+Axiom elements_correct:
+ forall (m: t) (i: positive),
+ contains m i = true -> In i (elements m).
+
+Axiom elements_complete:
+ forall (m: t) (i: positive),
+ In i (elements m) -> contains m i = true.
+
+Axiom elements_spec:
+ forall (m: t) (i: positive),
+ In i (elements m) <-> contains m i = true.
+
+Parameter fold:
+ forall {B : Type},
+ (B -> positive -> B) -> t -> B -> B.
+
+Axiom fold_spec:
+ forall {B: Type} (f: B -> positive -> B) (v: B) (m: t),
+ fold f m v =
+ List.fold_left f (elements m) v.
+
+Parameter is_subset : t -> t -> bool.
+
+Axiom is_subset_spec1:
+ forall s s',
+ is_subset s s' = true ->
+ (forall i, contains s i = true -> contains s' i = true).
+
+Axiom is_subset_spec2:
+ forall s s',
+ (forall i, contains s i = true -> contains s' i = true) ->
+ is_subset s s' = true.
+
+Axiom is_subset_spec:
+ forall s s',
+ is_subset s s' = true <->
+ (forall i, contains s i = true -> contains s' i = true).
+
+Parameter filter: (positive -> bool) -> t -> t.
+
+Axiom gfilter:
+ forall fn s j,
+ contains (filter fn s) j =
+ contains s j && (fn j).
+
+End POSITIVE_SET.
+
+Module PSet : POSITIVE_SET.
+
+Record pset : Type := mkpset
+{
+ pset_x : PSet_internals.pset ;
+ pset_wf : PSet_internals.wf pset_x = true
+}.
+
+Definition t := pset.
+
+Program Definition empty : t := mkpset PSet_internals.empty _.
+
+Definition contains (s : t) (i : positive) :=
+ PSet_internals.contains (pset_x s) i.
+
+Theorem gempty :
+ forall i : positive,
+ contains empty i = false.
+Proof.
+ intro.
+ unfold empty, contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Program Definition add (i : positive) (s : t) :=
+ mkpset (PSet_internals.add i (pset_x s)) _.
+Obligation 1.
+ destruct s.
+ apply PSet_internals.wf_add.
+ simpl.
+ assumption.
+Qed.
+
+Theorem gaddo :
+ forall i j : positive,
+ forall s : t,
+ i <> j ->
+ contains (add i s) j = contains s j.
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Theorem gadds :
+ forall i : positive,
+ forall s : pset,
+ contains (add i s) i = true.
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Program Definition remove (i : positive) (s : t) :=
+ mkpset (PSet_internals.remove i (pset_x s)) _.
+Obligation 1.
+ destruct s.
+ apply PSet_internals.wf_remove.
+ simpl.
+ assumption.
+Qed.
+
+Theorem gremoves :
+ forall i : positive,
+ forall s : pset,
+ contains (remove i s) i = false.
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Theorem gremoveo :
+ forall i j : positive,
+ forall s : pset,
+ i<>j ->
+ contains (remove i s) j = contains s j.
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Program Definition union (s s' : t) :=
+ mkpset (PSet_internals.union (pset_x s) (pset_x s')) _.
+Obligation 1.
+ destruct s; destruct s'.
+ apply PSet_internals.wf_union; simpl; assumption.
+Qed.
+
+Theorem gunion:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (union s s')) j = orb (contains s j) (contains s' j).
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Program Definition inter (s s' : t) :=
+ mkpset (PSet_internals.inter (pset_x s) (pset_x s')) _.
+Obligation 1.
+ destruct s; destruct s'.
+ apply PSet_internals.wf_inter; simpl; assumption.
+Qed.
+
+Theorem ginter:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (inter s s')) j = andb (contains s j) (contains s' j).
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Program Definition subtract (s s' : t) :=
+ mkpset (PSet_internals.subtract (pset_x s) (pset_x s')) _.
+Obligation 1.
+ destruct s; destruct s'.
+ apply PSet_internals.wf_subtract; simpl; assumption.
+Qed.
+
+Theorem gsubtract:
+ forall s s' : pset,
+ forall j : positive,
+ (contains (subtract s s')) j = andb (contains s j) (negb (contains s' j)).
+Proof.
+ intros.
+ unfold contains.
+ simpl.
+ auto with pset.
+Qed.
+
+Theorem uneq_exists :
+ forall s s', s <> s' ->
+ exists i, (contains s i) <> (contains s' i).
+Proof.
+ destruct s as [s WF]; destruct s' as [s' WF']; simpl.
+ intro UNEQ.
+ destruct (PSet_internals.pset_eq s s').
+ { subst s'.
+ pose proof (PSet_internals.wf_irrelevant s WF WF').
+ subst WF'.
+ congruence.
+ }
+ unfold contains; simpl.
+ apply PSet_internals.wf_eq; trivial.
+Qed.
+
+Definition eq:
+ forall s s' : t, {s = s'} + {s <> s'}.
+Proof.
+ destruct s as [s WF].
+ destruct s' as [s' WF'].
+ destruct (PSet_internals.pset_eq s s'); simpl.
+ {
+ subst s'.
+ left.
+ pose proof (PSet_internals.wf_irrelevant s WF WF').
+ subst WF'.
+ reflexivity.
+ }
+ right.
+ congruence.
+Qed.
+
+Theorem eq_spec :
+ forall s s',
+ (forall i, (contains s i) = (contains s' i)) <-> s = s'.
+Proof.
+ intros.
+ split; intro K.
+ 2: subst s'; reflexivity.
+ destruct s as [s WF].
+ destruct s' as [s' WF'].
+ unfold contains in K.
+ simpl in K.
+ fold (PSet_internals.iswf s) in WF.
+ fold (PSet_internals.iswf s') in WF'.
+ assert (s = s').
+ {
+ apply PSet_internals.eq_correct; assumption.
+ }
+ subst s'.
+ pose proof (PSet_internals.wf_irrelevant s WF WF').
+ subst WF'.
+ reflexivity.
+Qed.
+
+
+Definition elements (m : t) := PSet_internals.elements (pset_x m).
+
+
+Theorem elements_correct:
+ forall (m: pset) (i: positive),
+ contains m i = true -> In i (elements m).
+Proof.
+ destruct m; unfold elements, contains; simpl.
+ apply PSet_internals.elements_correct.
+Qed.
+
+Theorem elements_complete:
+ forall (m: pset) (i: positive),
+ In i (elements m) -> contains m i = true.
+Proof.
+ destruct m; unfold elements, contains; simpl.
+ apply PSet_internals.elements_complete.
+Qed.
+
+
+Theorem elements_spec:
+ forall (m: pset) (i: positive),
+ In i (elements m) <-> contains m i = true.
+Proof.
+ intros. split.
+ - apply elements_complete.
+ - apply elements_correct.
+Qed.
+
+Definition fold {B : Type} (f : B -> positive -> B) (m : t) (v : B) : B :=
+ PSet_internals.fold f (pset_x m) v.
+
+Theorem fold_spec:
+ forall {B: Type} (f: B -> positive -> B) (v: B) (m: pset),
+ fold f m v =
+ List.fold_left f (elements m) v.
+Proof.
+ destruct m; unfold fold, elements; simpl.
+ apply PSet_internals.fold_spec.
+Qed.
+
+Definition is_subset (s s' : t) := PSet_internals.is_subset (pset_x s) (pset_x s').
+
+Theorem is_subset_spec1:
+ forall s s',
+ is_subset s s' = true ->
+ (forall i, contains s i = true -> contains s' i = true).
+Proof.
+ unfold is_subset, contains.
+ intros s s' H.
+ apply PSet_internals.is_subset_spec1.
+ assumption.
+Qed.
+
+Theorem is_subset_spec2:
+ forall s s',
+ (forall i, contains s i = true -> contains s' i = true) ->
+ is_subset s s' = true.
+Proof.
+ destruct s; destruct s';
+ unfold is_subset, contains;
+ intros.
+ apply PSet_internals.is_subset_spec2.
+ all: simpl.
+ all: assumption.
+Qed.
+
+Global Hint Resolve is_subset_spec1 is_subset_spec2 : pset.
+
+Theorem is_subset_spec:
+ forall s s',
+ is_subset s s' = true <->
+ (forall i, contains s i = true -> contains s' i = true).
+Proof.
+ intros.
+ split;
+ eauto with pset.
+Qed.
+
+Program Definition filter (fn : positive -> bool) (m : t) : t :=
+ (mkpset (PSet_internals.filter fn (pset_x m)) _).
+Obligation 1.
+ apply PSet_internals.wf_filter.
+ unfold PSet_internals.iswf.
+ destruct m.
+ assumption.
+Qed.
+
+Theorem gfilter:
+ forall fn s j,
+ contains (filter fn s) j =
+ contains s j && (fn j).
+Proof.
+ unfold contains, filter.
+ simpl.
+ intros.
+ apply PSet_internals.gfilter.
+Qed.
+End PSet.
+
+Global Hint Resolve PSet.gaddo PSet.gadds PSet.gremoveo PSet.gremoves PSet.gunion PSet.ginter PSet.gsubtract PSet.gfilter PSet.is_subset_spec1 PSet.is_subset_spec2 : pset.
+
+Hint Rewrite PSet.gadds PSet.gremoves PSet.gunion PSet.ginter PSet.gsubtract PSet.gfilter : pset.
diff --git a/lib/HashedSetaux.ml b/lib/HashedSetaux.ml
new file mode 100644
index 00000000..501475d6
--- /dev/null
+++ b/lib/HashedSetaux.ml
@@ -0,0 +1,67 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+type uid = int
+
+let uid_base = min_int
+let next_uid = ref (uid_base+1)
+
+let incr_uid () =
+ let r = !next_uid in
+ if r = max_int
+ then failwith "HashedSet: no more uid"
+ else next_uid := succ r;;
+
+let cur_uid () = !next_uid;;
+
+type pset =
+ | Empty
+ | Node of uid * pset * bool * pset;;
+
+let get_uid = function
+ | Empty -> uid_base
+ | Node(uid, _, _, _) -> uid;;
+
+module HashedPSet =
+ struct
+ type t = pset
+
+ let hash = function
+ | Empty -> 0
+ | Node(_, b0, f, b1) -> Hashtbl.hash (get_uid b0, f, get_uid b1);;
+
+ let equal x x' = match x, x' with
+ | Empty, Empty -> true
+ | Node(_, b0, f, b1), Node(_, b0', f', b1') ->
+ b0 == b0' && f == f' && b1 == b1'
+ | _, _ -> false
+ end;;
+
+module PSetHash = Weak.Make(HashedPSet);;
+
+let htable = PSetHash.create 1000;;
+
+let qnode b0 f b1 =
+ let x = Node(cur_uid(), b0, f, b1) in
+ match PSetHash.find_opt htable x with
+ | None -> PSetHash.add htable x; incr_uid(); x
+ | Some y -> y;;
+
+let node (b0, f, b1) = qnode b0 f b1;;
+
+let empty = Empty;;
+
+let pset_match empty_case node_case = function
+ | Empty -> empty_case ()
+ | Node(_, b0, f, b1) -> node_case b0 f b1;;
+
+let eq (x : pset) (y : pset) = (x==y);;
diff --git a/lib/HashedSetaux.mli b/lib/HashedSetaux.mli
new file mode 100644
index 00000000..e3426eb4
--- /dev/null
+++ b/lib/HashedSetaux.mli
@@ -0,0 +1,18 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+type pset
+val qnode : pset -> bool -> pset -> pset
+val node : pset * bool * pset -> pset
+val empty : pset
+val pset_match : (unit -> 'a) -> (pset -> bool -> pset -> 'a) -> pset -> 'a
+val eq : pset -> pset -> bool
diff --git a/lib/Impure/ImpConfig.v b/lib/Impure/ImpConfig.v
new file mode 100644
index 00000000..dd9785b5
--- /dev/null
+++ b/lib/Impure/ImpConfig.v
@@ -0,0 +1,85 @@
+(** Impure Config for UNTRUSTED backend !!! *)
+
+Require Import ImpMonads.
+Require Extraction.
+(** Pure computations (used for extraction !)
+
+We keep module [Impure] opaque in order to check that Coq proof do not depend on
+the implementation of [Impure].
+
+*)
+
+Module Type ImpureView.
+
+ Include MayReturnMonad.
+
+(* WARNING: THIS IS REALLY UNSAFE TO DECOMMENT THE "UnsafeImpure" module !
+
+ unsafe_coerce coerces an impure computation into a pure one !
+
+*)
+
+(* START COMMENT *)
+ Module UnsafeImpure.
+
+ Parameter unsafe_coerce: forall {A}, t A -> option A.
+
+ Parameter unsafe_coerce_not_really_correct: forall A (k: t A) (x:A), (unsafe_coerce k)=Some x -> mayRet k x.
+
+ Extraction Inline unsafe_coerce.
+
+ End UnsafeImpure.
+(* END COMMENT *)
+
+
+End ImpureView.
+
+
+Module Impure: ImpureView.
+
+ Include IdentityMonad.
+
+ Module UnsafeImpure.
+
+ Definition unsafe_coerce {A} (x:t A) := Some x.
+
+ Lemma unsafe_coerce_not_really_correct: forall A (k: t A) x, (unsafe_coerce k)=Some x -> mayRet k x.
+ Proof.
+ unfold unsafe_coerce, mayRet; congruence.
+ Qed.
+
+ End UnsafeImpure.
+
+End Impure.
+
+
+(** Comment the above code and decomment this to test that coq proofs still work with an impure monad !
+
+- this should fail only on extraction or if unsafe_coerce is used !
+
+*)
+(*
+Module Impure: MayReturnMonad := PowerSetMonad.
+*)
+
+Export Impure.
+
+Extraction Inline ret mk_annot.
+
+
+(* WARNING. The following directive is unsound.
+
+ Extraction Inline bind
+
+For example, it may lead to extract the following code as "true" (instead of an error raising code)
+ failwith "foo";;true
+
+*)
+
+Extract Inlined Constant bind => "(|>)".
+
+
+Extract Constant t "" => "". (* This weird directive extracts [t] as "'a" instead of "'a t" *)
+Extraction Inline t.
+
+Global Opaque t.
diff --git a/lib/Impure/ImpCore.v b/lib/Impure/ImpCore.v
new file mode 100644
index 00000000..508b3f19
--- /dev/null
+++ b/lib/Impure/ImpCore.v
@@ -0,0 +1,196 @@
+(** Impure monad for interface with impure code
+
+*)
+
+Require Export Program.
+Require Export ImpConfig.
+
+(* Theory: bind + embed => dbind
+
+Program Definition dbind {A B} (k1: t A) (k2: forall (a:A), (mayRet k1 a) -> t B) : t B
+ := bind (mk_annot k1) (fun a => k2 a _).
+
+Lemma mayRet_dbind: forall (A B:Type) k1 k2 (b:B),
+ mayRet (dbind k1 k2) b -> exists a:A, exists H: (mayRet k1 a), mayRet (k2 a H) b.
+Proof.
+ intros A B k1 k2 b H; decompose [ex and] (mayRet_bind _ _ _ _ _ H).
+ eapply ex_intro.
+ eapply ex_intro.
+ eauto.
+Qed.
+
+*)
+
+Definition wlp {A:Type} (k: t A) (P: A -> Prop): Prop
+ := forall a, mayRet k a -> P a.
+
+(* Notations *)
+
+(* Print Grammar constr. *)
+
+Module Notations.
+
+ Bind Scope impure_scope with t.
+ Delimit Scope impure_scope with impure.
+
+ Notation "?? A" := (t A) (at level 0, A at level 95): impure_scope.
+
+ Notation "k '~~>' a" := (mayRet k a) (at level 75, no associativity): impure_scope.
+
+ Notation "'RET' a" := (ret a) (at level 0): impure_scope.
+
+ Notation "'DO' x '<~' k1 ';;' k2" := (bind k1 (fun x => k2))
+ (at level 55, k1 at level 53, x at level 99, right associativity): impure_scope.
+
+ Notation "k1 ';;' k2" := (bind k1 (fun _ => k2))
+ (at level 55, right associativity): impure_scope.
+
+ Notation "'WHEN' k '~>' a 'THEN' R" := (wlp k (fun a => R))
+ (at level 73, R at level 100, right associativity): impure_scope.
+
+ Notation "'ASSERT' P" := (ret (A:=P) _) (at level 0, only parsing): impure_scope.
+
+End Notations.
+
+Import Notations.
+Local Open Scope impure.
+
+Goal ((?? list nat * ??nat -> nat) = ((?? ((list nat) * ?? nat) -> nat)))%type.
+Proof.
+ apply refl_equal.
+Qed.
+
+
+(* wlp lemmas for tactics *)
+
+Lemma wlp_unfold A (k:??A)(P: A -> Prop):
+ (forall a, k ~~> a -> P a)
+ -> wlp k P.
+Proof.
+ auto.
+Qed.
+
+Lemma wlp_monotone A (k:?? A) (P1 P2: A -> Prop):
+ wlp k P1
+ -> (forall a, k ~~> a -> P1 a -> P2 a)
+ -> wlp k P2.
+Proof.
+ unfold wlp; eauto.
+Qed.
+
+Lemma wlp_forall A B (k:?? A) (P: B -> A -> Prop):
+ (forall x, wlp k (P x))
+ -> wlp k (fun a => forall x, P x a).
+Proof.
+ unfold wlp; auto.
+Qed.
+
+Lemma wlp_ret A (P: A -> Prop) a:
+ P a -> wlp (ret a) P.
+Proof.
+ unfold wlp.
+ intros H b H0.
+ rewrite <- (mayRet_ret _ a b H0).
+ auto.
+Qed.
+
+Lemma wlp_bind A B (k1:??A) (k2: A -> ??B) (P: B -> Prop):
+ wlp k1 (fun a => wlp (k2 a) P) -> wlp (bind k1 k2) P.
+Proof.
+ unfold wlp.
+ intros H a H0.
+ case (mayRet_bind _ _ _ _ _ H0); clear H0.
+ intuition eauto.
+Qed.
+
+Lemma wlp_ifbool A (cond: bool) (k1 k2: ?? A) (P: A -> Prop):
+ (cond=true -> wlp k1 P) -> (cond=false -> wlp k2 P) -> wlp (if cond then k1 else k2) P.
+Proof.
+ destruct cond; auto.
+Qed.
+
+Lemma wlp_letprod (A B C: Type) (p: A*B) (k: A -> B -> ??C) (P: C -> Prop):
+ (wlp (k (fst p) (snd p)) P)
+ -> (wlp (let (x,y):=p in (k x y)) P).
+Proof.
+ destruct p; simpl; auto.
+Qed.
+
+Lemma wlp_sum (A B C: Type) (x: A+B) (k1: A -> ??C) (k2: B -> ??C) (P: C -> Prop):
+ (forall a, x=inl a -> wlp (k1 a) P) ->
+ (forall b, x=inr b -> wlp (k2 b) P) ->
+ (wlp (match x with inl a => k1 a | inr b => k2 b end) P).
+Proof.
+ destruct x; simpl; auto.
+Qed.
+
+Lemma wlp_sumbool (A B:Prop) (C: Type) (x: {A}+{B}) (k1: A -> ??C) (k2: B -> ??C) (P: C -> Prop):
+ (forall a, x=left a -> wlp (k1 a) P) ->
+ (forall b, x=right b -> wlp (k2 b) P) ->
+ (wlp (match x with left a => k1 a | right b => k2 b end) P).
+Proof.
+ destruct x; simpl; auto.
+Qed.
+
+Lemma wlp_option (A B: Type) (x: option A) (k1: A -> ??B) (k2: ??B) (P: B -> Prop):
+ (forall a, x=Some a -> wlp (k1 a) P) ->
+ (x=None -> wlp k2 P) ->
+ (wlp (match x with Some a => k1 a | None => k2 end) P).
+Proof.
+ destruct x; simpl; auto.
+Qed.
+
+(* Tactics
+
+MAIN tactics:
+ - xtsimplify "base": simplification using from hints in "base" database (in particular "wlp" lemmas).
+ - xtstep "base": only one step of simplification.
+
+For good performance, it is recommanded to have several databases.
+
+*)
+
+Ltac introcomp :=
+ let a:= fresh "exta" in
+ let H:= fresh "Hexta" in
+ intros a H.
+
+(* decompose the current wlp goal using "introduction" rules *)
+Ltac wlp_decompose :=
+ apply wlp_ret
+ || apply wlp_bind
+ || apply wlp_ifbool
+ || apply wlp_letprod
+ || apply wlp_sum
+ || apply wlp_sumbool
+ || apply wlp_option
+ .
+
+(* this tactic simplifies the current "wlp" goal using any hint found via tactic "hint". *)
+Ltac apply_wlp_hint hint :=
+ eapply wlp_monotone;
+ [ hint; fail | idtac ] ;
+ simpl; introcomp.
+
+(* one step of wlp_xsimplify
+*)
+Ltac wlp_step hint :=
+ match goal with
+ | |- (wlp _ _) =>
+ wlp_decompose
+ || apply_wlp_hint hint
+ || (apply wlp_unfold; introcomp)
+ end.
+
+(* main general tactic
+WARNING: for the good behavior of "wlp_xsimplify", "hint" must at least perform a "eauto".
+
+Example of use:
+ wlp_xsimplify (intuition eauto with base).
+*)
+Ltac wlp_xsimplify hint :=
+ repeat (intros; subst; wlp_step hint; simpl; (tauto || hint)).
+
+Create HintDb wlp discriminated.
+
+Ltac wlp_simplify := wlp_xsimplify ltac:(intuition eauto with wlp).
diff --git a/lib/Impure/ImpExtern.v b/lib/Impure/ImpExtern.v
new file mode 100644
index 00000000..8fb3cf3b
--- /dev/null
+++ b/lib/Impure/ImpExtern.v
@@ -0,0 +1,7 @@
+(** Exporting Extern functions
+*)
+
+Require Export ImpPrelude.
+Require Export ImpIO.
+Require Export ImpLoops.
+Require Export ImpHCons.
diff --git a/lib/Impure/ImpHCons.v b/lib/Impure/ImpHCons.v
new file mode 100644
index 00000000..637116cc
--- /dev/null
+++ b/lib/Impure/ImpHCons.v
@@ -0,0 +1,199 @@
+Require Export ImpIO.
+
+Import Notations.
+Local Open Scope impure.
+
+
+Axiom string_of_hashcode: hashcode -> ?? caml_string.
+Extract Constant string_of_hashcode => "string_of_int".
+
+Axiom hash: forall {A}, A -> ?? hashcode.
+Extract Constant hash => "Hashtbl.hash".
+
+(**************************)
+(* (Weak) Sets *)
+
+
+Import Dict.
+
+Axiom make_dict: forall {A B}, (hash_params A) -> ?? Dict.t A B.
+Extract Constant make_dict => "ImpHConsOracles.make_dict".
+
+
+Module Sets.
+
+Definition t {A} (mod: A -> Prop) := Dict.t A {x | mod x}.
+
+Definition empty {A} (hp: hash_params A) {mod:A -> Prop}: ?? t mod :=
+ make_dict hp.
+
+Program Fixpoint add {A} (l: list A) {mod: A -> Prop} (d: t mod): forall {H:forall x, List.In x l -> mod x}, ?? unit :=
+ match l with
+ | nil => fun H => RET ()
+ | x::l' => fun H =>
+ d.(set)(x,x);;
+ add l' d
+ end.
+
+Program Definition create {A} (hp: hash_params A) (l:list A): ?? t (fun x => List.In x l) :=
+ DO d <~ empty hp (mod:=fun x => List.In x l);;
+ add l (mod:=fun x => List.In x l) d (H:=_);;
+ RET d.
+Global Opaque create.
+
+Definition is_present {A} (hp: hash_params A) (x:A) {mod} (d:t mod): ?? bool :=
+ DO oy <~ (d.(get)) x;;
+ match oy with
+ | Some y => hp.(test_eq) x (`y)
+ | None => RET false
+ end.
+
+Local Hint Resolve test_eq_correct: wlp.
+
+Lemma is_present_correct A (hp: hash_params A) x mod (d:t mod):
+ WHEN is_present hp x d ~> b THEN b=true -> mod x.
+Proof.
+ wlp_simplify; subst; eauto.
+ - apply proj2_sig.
+ - discriminate.
+Qed.
+Hint Resolve is_present_correct: wlp.
+Global Opaque is_present.
+
+Definition msg_assert_incl: pstring := "Sets.assert_incl".
+
+Fixpoint assert_incl {A} (hp: hash_params A) (l: list A) {mod} (d:t mod): ?? unit :=
+ match l with
+ | nil => RET ()
+ | x::l' =>
+ DO b <~ is_present hp x d;;
+ if b then
+ assert_incl hp l' d
+ else (
+ hp.(log) x;;
+ FAILWITH msg_assert_incl
+ )
+ end.
+
+Lemma assert_incl_correct A (hp: hash_params A) l mod (d:t mod):
+ WHEN assert_incl hp l d ~> _ THEN forall x, List.In x l -> mod x.
+Proof.
+ induction l; wlp_simplify; subst; eauto.
+Qed.
+Hint Resolve assert_incl_correct: wlp.
+Global Opaque assert_incl.
+
+Definition assert_list_incl {A} (hp: hash_params A) (l1 l2: list A): ?? unit :=
+ (* println "";;print("dict_create ");;*)
+ DO d <~ create hp l2;;
+ (*print("assert_incl ");;*)
+ assert_incl hp l1 d.
+
+Lemma assert_list_incl_correct A (hp: hash_params A) l1 l2:
+ WHEN assert_list_incl hp l1 l2 ~> _ THEN List.incl l1 l2.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque assert_list_incl.
+Hint Resolve assert_list_incl_correct: wlp.
+
+End Sets.
+
+
+
+
+(********************************)
+(* (Weak) HConsing *)
+
+Module HConsing.
+
+Export HConsingDefs.
+
+(* NB: this axiom is NOT intended to be called directly, but only through [hCons...] functions below. *)
+Axiom xhCons: forall {A}, (hashP A) -> ?? hashConsing A.
+Extract Constant xhCons => "ImpHConsOracles.xhCons".
+
+Definition hCons_eq_msg: pstring := "xhCons: hash eq differs".
+
+Definition hCons {A} (hp: hashP A): ?? (hashConsing A) :=
+ DO hco <~ xhCons hp ;;
+ RET {|
+ hC := (fun x =>
+ DO x' <~ hC hco x ;;
+ DO b0 <~ hash_eq hp x.(hdata) x' ;;
+ assert_b b0 hCons_eq_msg;;
+ RET x');
+ next_hid := hco.(next_hid);
+ next_log := hco.(next_log);
+ export := hco.(export);
+ remove := hco.(remove)
+ |}.
+
+
+Lemma hCons_correct A (hp: hashP A):
+ WHEN hCons hp ~> hco THEN
+ (forall x y, WHEN hp.(hash_eq) x y ~> b THEN b=true -> (ignore_hid hp x)=(ignore_hid hp y)) ->
+ forall x, WHEN hco.(hC) x ~> x' THEN ignore_hid hp x.(hdata)=ignore_hid hp x'.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hCons.
+Hint Resolve hCons_correct: wlp.
+
+
+
+(* hashV: extending a given type with hash-consing *)
+Record hashV {A:Type}:= {
+ data: A;
+ hid: hashcode
+}.
+Arguments hashV: clear implicits.
+
+Definition hashV_C {A} (test_eq: A -> A -> ?? bool) : hashP (hashV A) := {|
+ hash_eq := fun v1 v2 => test_eq v1.(data) v2.(data);
+ get_hid := hid;
+ set_hid := fun v id => {| data := v.(data); hid := id |}
+|}.
+
+Definition liftHV (x:nat) := {| data := x; hid := unknown_hid |}.
+
+Definition hConsV {A} (hasheq: A -> A -> ?? bool): ?? (hashConsing (hashV A)) :=
+ hCons (hashV_C hasheq).
+
+Lemma hConsV_correct A (hasheq: A -> A -> ?? bool):
+ WHEN hConsV hasheq ~> hco THEN
+ (forall x y, WHEN hasheq x y ~> b THEN b=true -> x=y) ->
+ forall x, WHEN hco.(hC) x ~> x' THEN x.(hdata).(data)=x'.(data).
+Proof.
+ Local Hint Resolve f_equal2: core.
+ wlp_simplify.
+ exploit H; eauto.
+ + wlp_simplify.
+ + intros; congruence.
+Qed.
+Global Opaque hConsV.
+Hint Resolve hConsV_correct: wlp.
+
+Definition hC_known {A} (hco:hashConsing (hashV A)) (unknownHash_msg: hashinfo (hashV A) -> ?? pstring) (x:hashinfo (hashV A)): ?? hashV A :=
+ DO clock <~ hco.(next_hid)();;
+ DO x' <~ hco.(hC) x;;
+ DO ok <~ hash_older x'.(hid) clock;;
+ if ok
+ then RET x'
+ else
+ hco.(remove) x;;
+ DO msg <~ unknownHash_msg x;;
+ FAILWITH msg.
+
+Lemma hC_known_correct A (hco:hashConsing (hashV A)) msg x:
+ WHEN hC_known hco msg x ~> x' THEN
+ (forall x, WHEN hco.(hC) x ~> x' THEN x.(hdata).(data)=x'.(data)) ->
+ x.(hdata).(data)=x'.(data).
+Proof.
+ wlp_simplify.
+ unfold wlp in * |- ; eauto.
+Qed.
+Global Opaque hC_known.
+Hint Resolve hC_known_correct: wlp.
+
+End HConsing.
diff --git a/lib/Impure/ImpIO.v b/lib/Impure/ImpIO.v
new file mode 100644
index 00000000..6c02c395
--- /dev/null
+++ b/lib/Impure/ImpIO.v
@@ -0,0 +1,159 @@
+(** Extension of Coq language with some IO and exception-handling operators.
+
+TODO: integration with http://coq.io/ ?
+
+*)
+
+Require Export ImpPrelude.
+
+Import Notations.
+Local Open Scope impure.
+
+(** Printing functions *)
+
+Axiom print: pstring -> ?? unit.
+Extract Constant print => "ImpIOOracles.print".
+
+Axiom println: pstring -> ?? unit.
+Extract Constant println => "ImpIOOracles.println".
+
+Axiom read_line: unit -> ?? pstring.
+Extract Constant read_line => "ImpIOOracles.read_line".
+
+Require Import ZArith.
+Axiom string_of_Z: Z -> ?? pstring.
+Extract Constant string_of_Z => "ImpIOOracles.string_of_Z".
+
+(** timer *)
+
+Axiom timer: forall {A B}, (A -> ?? B)*A -> ?? B.
+Extract Constant timer => "ImpIOOracles.timer".
+
+(** Exception Handling *)
+
+Axiom exit_observer: Type.
+Extract Constant exit_observer => "((unit -> unit) ref)".
+
+Axiom new_exit_observer: (unit -> ??unit) -> ??exit_observer.
+Extract Constant new_exit_observer => "ImpIOOracles.new_exit_observer".
+
+Axiom set_exit_observer: exit_observer * (unit -> ??unit) -> ??unit.
+Extract Constant set_exit_observer => "ImpIOOracles.set_exit_observer".
+
+Axiom exn: Type.
+Extract Inlined Constant exn => "exn".
+
+Axiom raise: forall {A}, exn -> ?? A.
+Extract Constant raise => "raise".
+
+Axiom exn2string: exn -> ?? pstring.
+Extract Constant exn2string => "ImpIOOracles.exn2string".
+
+Axiom fail: forall {A}, pstring -> ?? A.
+Extract Constant fail => "ImpIOOracles.fail".
+
+Axiom try_with_fail: forall {A}, (unit -> ?? A) * (pstring -> exn -> ??A) -> ??A.
+Extract Constant try_with_fail => "ImpIOOracles.try_with_fail".
+
+Axiom try_with_any: forall {A}, (unit -> ?? A) * (exn -> ??A) -> ??A.
+Extract Constant try_with_any => "ImpIOOracles.try_with_any".
+
+Notation "'RAISE' e" := (DO r <~ raise (A:=False) e ;; RET (match r with end)) (at level 0): impure_scope.
+Notation "'FAILWITH' msg" := (DO r <~ fail (A:=False) msg ;; RET (match r with end)) (at level 0): impure_scope.
+
+Definition _FAILWITH {A:Type} msg: ?? A := FAILWITH msg.
+
+Example _FAILWITH_correct A msg (P: A -> Prop):
+ WHEN _FAILWITH msg ~> r THEN P r.
+Proof.
+ wlp_simplify.
+Qed.
+
+Notation "'TRY' k1 'WITH_FAIL' s ',' e '=>' k2" := (try_with_fail (fun _ => k1, fun s e => k2))
+ (at level 55, k1 at level 53, right associativity): impure_scope.
+
+Notation "'TRY' k1 'WITH_ANY' e '=>' k2" := (try_with_any (fun _ => k1, fun e => k2))
+ (at level 55, k1 at level 53, right associativity): impure_scope.
+
+
+Program Definition assert_b (b: bool) (msg: pstring): ?? b=true :=
+ match b with
+ | true => RET _
+ | false => FAILWITH msg
+ end.
+
+Lemma assert_wlp_true msg b: WHEN assert_b b msg ~> _ THEN b=true.
+Proof.
+ wlp_simplify.
+Qed.
+
+Lemma assert_false_wlp msg (P: Prop): WHEN assert_b false msg ~> _ THEN P.
+Proof.
+ simpl; wlp_simplify.
+Qed.
+
+Program Definition try_catch_fail_ensure {A} (k1: unit -> ?? A) (k2: pstring -> exn -> ??A) (P: A -> Prop | wlp (k1 tt) P /\ (forall s e, wlp (k2 s e) P)): ?? { r | P r }
+ := TRY
+ DO r <~ mk_annot (k1 tt);;
+ RET (exist P r _)
+ WITH_FAIL s, e =>
+ DO r <~ mk_annot (k2 s e);;
+ RET (exist P r _).
+Obligation 2.
+ unfold wlp in * |- *; eauto.
+Qed.
+
+Notation "'TRY' k1 'CATCH_FAIL' s ',' e '=>' k2 'ENSURE' P" := (try_catch_fail_ensure (fun _ => k1) (fun s e => k2) (exist _ P _))
+ (at level 55, k1 at level 53, right associativity): impure_scope.
+
+Definition is_try_post {A} (P: A -> Prop) k1 k2 : Prop :=
+ wlp (k1 ()) P /\ forall (e:exn), wlp (k2 e) P.
+
+Program Definition try_catch_ensure {A} k1 k2 (P:A->Prop|is_try_post P k1 k2): ?? { r | P r }
+ := TRY
+ DO r <~ mk_annot (k1 ());;
+ RET (exist P r _)
+ WITH_ANY e =>
+ DO r <~ mk_annot (k2 e);;
+ RET (exist P r _).
+Obligation 1.
+ unfold is_try_post, wlp in * |- *; intuition eauto.
+Qed.
+Obligation 2.
+ unfold is_try_post, wlp in * |- *; intuition eauto.
+Qed.
+
+Notation "'TRY' k1 'CATCH' e '=>' k2 'ENSURE' P" := (try_catch_ensure (fun _ => k1) (fun e => k2) (exist _ P _))
+ (at level 55, k1 at level 53, right associativity): impure_scope.
+
+
+Program Example tryex {A} (x y:A) :=
+ TRY (RET x)
+ CATCH _ => (RET y)
+ ENSURE (fun r => r = x \/ r = y).
+Obligation 1.
+ split; wlp_simplify.
+Qed.
+
+Program Example tryex_test {A} (x y:A):
+ WHEN tryex x y ~> r THEN `r <> x -> `r = y.
+Proof.
+ wlp_simplify. destruct exta as [r [X|X]]; intuition.
+Qed.
+
+
+Program Example try_branch1 {A} (x:A): ?? { r | r = x} :=
+ TRY (RET x)
+ CATCH e => (FAILWITH "!")
+ ENSURE _.
+Obligation 1.
+ split; wlp_simplify.
+Qed.
+
+Program Example try_branch2 {A} (x:A): ?? { r | r = x} :=
+ TRY (FAILWITH "!")
+ CATCH e => (RET x)
+ ENSURE _.
+Obligation 1.
+ split; wlp_simplify.
+Qed.
diff --git a/lib/Impure/ImpLoops.v b/lib/Impure/ImpLoops.v
new file mode 100644
index 00000000..33376c19
--- /dev/null
+++ b/lib/Impure/ImpLoops.v
@@ -0,0 +1,123 @@
+(** Extension of Coq language with generic loops. *)
+
+Require Export ImpIO.
+
+Import Notations.
+Local Open Scope impure.
+
+
+(** While-loop iterations *)
+
+Axiom loop: forall {A B}, A * (A -> ?? (A+B)) -> ?? B.
+Extract Constant loop => "ImpLoopOracles.loop".
+
+
+Section While_Loop.
+
+(** Local Definition of "while-loop-invariant" *)
+Let wli {S} cond body (I: S -> Prop) := forall s, I s -> cond s = true -> WHEN (body s) ~> s' THEN I s'.
+
+Program Definition while {S} cond body (I: S -> Prop | wli cond body I) s0: ?? {s | (I s0 -> I s) /\ cond s = false}
+ := loop (A:={s | I s0 -> I s})
+ (s0,
+ fun s =>
+ match (cond s) with
+ | true =>
+ DO s' <~ mk_annot (body s) ;;
+ RET (inl (A:={s | I s0 -> I s }) s')
+ | false =>
+ RET (inr (B:={s | (I s0 -> I s) /\ cond s = false}) s)
+ end).
+Obligation 2.
+ unfold wli, wlp in * |-; eauto.
+Qed.
+Extraction Inline while.
+
+End While_Loop.
+
+
+Section Loop_Until_None.
+(** useful to demonstrate a UNSAT property *)
+
+(** Local Definition of "loop-until-None-invariant" *)
+Let luni {S} (body: S -> ?? (option S)) (I: S -> Prop) := forall s, I s -> WHEN (body s) ~> s' THEN match s' with Some s1 => I s1 | None => False end.
+
+Program Definition loop_until_None {S} body (I: S -> Prop | luni body I) s0: ?? ~(I s0)
+ := loop (A:={s | I s0 -> I s})
+ (s0,
+ fun s =>
+ DO s' <~ mk_annot (body s) ;;
+ match s' with
+ | Some s1 => RET (inl (A:={s | I s0 -> I s }) s1)
+ | None => RET (inr (B:=~(I s0)) _)
+ end).
+Obligation 2.
+ refine (H2 s _ _ H0). auto.
+Qed.
+Obligation 3.
+ intros X; refine (H1 s _ _ H). auto.
+Qed.
+Extraction Inline loop_until_None.
+
+End Loop_Until_None.
+
+
+(*********************************************)
+(* A generic fixpoint from an equality test *)
+
+Record answ {A B: Type} {R: A -> B -> Prop} := {
+ input: A ;
+ output: B ;
+ correct: R input output
+}.
+Arguments answ {A B}.
+
+Definition msg: pstring := "wapply fails".
+
+Definition beq_correct {A} (beq: A -> A -> ?? bool) :=
+ forall x y, WHEN beq x y ~> b THEN b=true -> x=y.
+
+Definition wapply {A B} {R: A -> B -> Prop} (beq: A -> A -> ?? bool) (k: A -> ?? answ R) (x:A): ?? B :=
+ DO a <~ k x;;
+ DO b <~ beq x (input a) ;;
+ assert_b b msg;;
+ RET (output a).
+
+Lemma wapply_correct A B (R: A -> B -> Prop) (beq: A -> A -> ?? bool) (k: A -> ?? answ R) x:
+ beq_correct beq
+ -> WHEN wapply beq k x ~> y THEN R x y.
+Proof.
+ unfold beq_correct; wlp_simplify.
+ destruct exta; simpl; auto.
+Qed.
+Local Hint Resolve wapply_correct: wlp.
+Global Opaque wapply.
+
+Axiom xrec_set_option: recMode -> ?? unit.
+Extract Constant xrec_set_option => "ImpLoopOracles.xrec_set_option".
+
+(* TODO: generalizaton to get beq (and a Hash function ?) in parameters ? *)
+Axiom xrec: forall {A B}, ((A -> ?? B) -> A -> ?? B) -> ?? (A -> ?? B).
+Extract Constant xrec => "ImpLoopOracles.xrec".
+
+Definition rec_preserv {A B} (recF: (A -> ?? B) -> A -> ?? B) (R: A -> B -> Prop) :=
+ forall f x, WHEN recF f x ~> z THEN (forall x', WHEN f x' ~> y THEN R x' y) -> R x z.
+
+
+Program Definition rec {A B} beq recF (R: A -> B -> Prop) (H1: rec_preserv recF R) (H2: beq_correct beq): ?? (A -> ?? B) :=
+ DO f <~ xrec (B:=answ R) (fun f x =>
+ DO y <~ mk_annot (recF (wapply beq f) x) ;;
+ RET {| input := x; output := `y |});;
+ RET (wapply beq f).
+Obligation 1.
+ eapply H1; eauto. clear H H1.
+ wlp_simplify.
+Qed.
+
+Lemma rec_correct A B beq recF (R: A -> B -> Prop) (H1: rec_preserv recF R) (H2: beq_correct beq):
+ WHEN rec beq recF R H1 H2 ~> f THEN forall x, WHEN f x ~> y THEN R x y.
+Proof.
+ wlp_simplify.
+Qed.
+Hint Resolve rec_correct: wlp.
+Global Opaque rec.
diff --git a/lib/Impure/ImpMonads.v b/lib/Impure/ImpMonads.v
new file mode 100644
index 00000000..f01a2755
--- /dev/null
+++ b/lib/Impure/ImpMonads.v
@@ -0,0 +1,148 @@
+(** Impure monad for interface with impure code
+*)
+
+
+Require Import Program.
+
+
+Module Type MayReturnMonad.
+
+ Axiom t: Type -> Type.
+
+ Axiom mayRet: forall {A:Type}, t A -> A -> Prop.
+
+ Axiom ret: forall {A}, A -> t A.
+
+ Axiom bind: forall {A B}, (t A) -> (A -> t B) -> t B.
+
+ Axiom mk_annot: forall {A} (k: t A), t { a: A | mayRet k a }.
+
+ Axiom mayRet_ret: forall A (a b:A),
+ mayRet (ret a) b -> a=b.
+
+ Axiom mayRet_bind: forall A B k1 k2 (b:B),
+ mayRet (bind k1 k2) b -> exists a:A, mayRet k1 a /\ mayRet (k2 a) b.
+
+End MayReturnMonad.
+
+
+
+(** Model of impure computation as predicate *)
+Module PowerSetMonad<: MayReturnMonad.
+
+ Definition t (A:Type) := A -> Prop.
+
+ Definition mayRet {A:Type} (k: t A) a: Prop := k a.
+
+ Definition ret {A:Type} (a:A) := eq a.
+
+ Definition bind {A B:Type} (k1: t A) (k2: A -> t B) :=
+ fun b => exists a, k1 a /\ k2 a b.
+
+ Definition mk_annot {A} (k: t A) : t { a | mayRet k a } := fun _ => True.
+
+ Lemma mayRet_ret A (a b:A): mayRet (ret a) b -> a=b.
+ Proof.
+ unfold mayRet, ret. firstorder.
+ Qed.
+
+ Lemma mayRet_bind A B k1 k2 (b:B):
+ mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
+ Proof.
+ unfold mayRet, bind.
+ firstorder.
+ Qed.
+
+End PowerSetMonad.
+
+
+(** The identity interpretation *)
+Module IdentityMonad<: MayReturnMonad.
+
+ Definition t (A:Type) := A.
+
+ (* may-return semantics of computations *)
+ Definition mayRet {A:Type} (a b:A): Prop := a=b.
+
+ Definition ret {A:Type} (a:A) := a.
+
+ Definition bind {A B:Type} (k1: A) (k2: A -> B) := k2 k1.
+
+ Definition mk_annot {A} (k: t A) : t { a: A | mayRet k a }
+ := exist _ k (eq_refl k) .
+
+ Lemma mayRet_ret (A:Type) (a b:A): mayRet (ret a) b -> a=b.
+ Proof.
+ intuition.
+ Qed.
+
+ Lemma mayRet_bind (A B:Type) (k1:t A) k2 (b:B):
+ mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
+ Proof.
+ firstorder.
+ Qed.
+
+End IdentityMonad.
+
+
+(** Model of impure computation as state-transformers *)
+Module StateMonad<: MayReturnMonad.
+
+ Parameter St: Type. (* A global state *)
+
+ Definition t (A:Type) := St -> A * St.
+
+ Definition mayRet {A:Type} (k: t A) a: Prop :=
+ exists s, fst (k s)=a.
+
+ Definition ret {A:Type} (a:A) := fun (s:St) => (a,s).
+
+ Definition bind {A B:Type} (k1: t A) (k2: A -> t B) :=
+ fun s0 => let r := k1 s0 in k2 (fst r) (snd r).
+
+ Program Definition mk_annot {A} (k: t A) : t { a | mayRet k a } :=
+ fun s0 => let r := k s0 in (exist _ (fst r) _, snd r).
+ Obligation 1.
+ unfold mayRet; eauto.
+ Qed.
+
+ Lemma mayRet_ret {A:Type} (a b:A): mayRet (ret a) b -> a=b.
+ Proof.
+ unfold mayRet, ret. firstorder.
+ Qed.
+
+ Lemma mayRet_bind {A B:Type} k1 k2 (b:B):
+ mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
+ Proof.
+ unfold mayRet, bind. firstorder eauto.
+ Qed.
+
+End StateMonad.
+
+(** The deferred interpretation *)
+Module DeferredMonad<: MayReturnMonad.
+
+ Definition t (A:Type) := unit -> A.
+
+ (* may-return semantics of computations *)
+ Definition mayRet {A:Type} (a: t A) (b:A): Prop := a tt=b.
+
+ Definition ret {A:Type} (a:A) : t A := fun _ => a.
+
+ Definition bind {A B:Type} (k1: t A) (k2: A -> t B) : t B := fun _ => k2 (k1 tt) tt.
+
+ Definition mk_annot {A} (k: t A) : t { a: A | mayRet k a }
+ := fun _ => exist _ (k tt) (eq_refl (k tt)).
+
+ Lemma mayRet_ret (A:Type) (a b: A): mayRet (ret a) b -> a=b.
+ Proof.
+ intuition.
+ Qed.
+
+ Lemma mayRet_bind (A B:Type) (k1:t A) k2 (b:B):
+ mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
+ Proof.
+ firstorder.
+ Qed.
+
+End DeferredMonad.
diff --git a/lib/Impure/ImpPrelude.v b/lib/Impure/ImpPrelude.v
new file mode 100644
index 00000000..de4c7973
--- /dev/null
+++ b/lib/Impure/ImpPrelude.v
@@ -0,0 +1,206 @@
+Require Export String.
+Require Export List.
+Require Extraction.
+Require Import Ascii.
+Require Import BinNums.
+Require Export ImpCore.
+Require Export PArith.
+
+
+Import Notations.
+Local Open Scope impure.
+
+(** Impure lazy andb of booleans *)
+Definition iandb (k1 k2: ??bool): ?? bool :=
+ DO r1 <~ k1 ;;
+ if r1 then k2 else RET false.
+
+Extraction Inline iandb. (* Juste pour l'efficacité à l'extraction ! *)
+
+(** Strings for pretty-printing *)
+
+Axiom caml_string: Type.
+Extract Constant caml_string => "string".
+
+(* New line *)
+Definition nl: string := String (ascii_of_pos 10%positive) EmptyString.
+
+Inductive pstring: Type :=
+ | Str: string -> pstring
+ | CamlStr: caml_string -> pstring
+ | Concat: pstring -> pstring -> pstring.
+
+Coercion Str: string >-> pstring.
+Bind Scope string_scope with pstring.
+
+Notation "x +; y" := (Concat x y)
+ (at level 65, left associativity): string_scope.
+
+(** Coq references *)
+
+Record cref {A} := {
+ set: A -> ?? unit;
+ get: unit -> ?? A
+}.
+Arguments cref: clear implicits.
+
+Axiom make_cref: forall {A}, A -> ?? cref A.
+Extract Constant make_cref => "(fun x -> let r = ref x in { set = (fun y -> r:=y); get = (fun () -> !r) })".
+
+
+(** Data-structure for a logger *)
+
+Record logger {A:Type} := {
+ log_insert: A -> ?? unit;
+ log_info: unit -> ?? pstring;
+}.
+Arguments logger: clear implicits.
+
+Axiom count_logger: unit -> ?? logger unit.
+Extract Constant count_logger => "(fun () -> let count = ref 0 in { log_insert = (fun () -> count := !count + 1); log_info = (fun () -> (CamlStr (string_of_int !count))) })".
+
+
+(** Axioms of Physical equality *)
+
+Axiom phys_eq: forall {A}, A -> A -> ?? bool.
+
+Axiom phys_eq_correct: forall A (x y:A), WHEN phys_eq x y ~> b THEN b=true -> x=y.
+
+
+(* We only check here that above axioms are not trivially inconsistent...
+ (but this does not prove the correctness of the extraction directive below).
+ *)
+Module PhysEqModel.
+
+Definition phys_eq {A} (x y: A) := ret false.
+
+Lemma phys_eq_correct: forall A (x y:A), WHEN phys_eq x y ~> b THEN b=true -> x=y.
+Proof.
+ wlp_simplify. discriminate.
+Qed.
+
+End PhysEqModel.
+
+Extract Inlined Constant phys_eq => "(==)".
+Hint Resolve phys_eq_correct: wlp.
+
+
+Axiom struct_eq: forall {A}, A -> A -> ?? bool.
+Axiom struct_eq_correct: forall A (x y:A), WHEN struct_eq x y ~> b THEN if b then x=y else x<>y.
+Extract Inlined Constant struct_eq => "(=)".
+Hint Resolve struct_eq_correct: wlp.
+
+
+(** Data-structure for generic hash-consing *)
+
+Axiom hashcode: Type.
+Extract Constant hashcode => "int".
+
+(* NB: hashConsing is assumed to generate hash-code in ascending order.
+ This gives a way to check that a hash-consed value is older than an other one.
+*)
+Axiom hash_older: hashcode -> hashcode -> ?? bool.
+Extract Inlined Constant hash_older => "(<)".
+
+Module Dict.
+
+Record hash_params {A:Type} := {
+ test_eq: A -> A -> ??bool;
+ test_eq_correct: forall x y, WHEN test_eq x y ~> r THEN r=true -> x=y;
+ hashing: A -> ??hashcode;
+ log: A -> ??unit (* for debugging only *)
+}.
+Arguments hash_params: clear implicits.
+
+
+Record t {A B:Type} := {
+ set: A * B -> ?? unit;
+ get: A -> ?? option B
+}.
+Arguments t: clear implicits.
+
+End Dict.
+
+Module HConsingDefs.
+
+Record hashinfo {A: Type} := {
+ hdata: A;
+ hcodes: list hashcode;
+}.
+Arguments hashinfo: clear implicits.
+
+(* for inductive types with intrinsic hash-consing *)
+Record hashP {A:Type}:= {
+ hash_eq: A -> A -> ?? bool;
+ get_hid: A -> hashcode;
+ set_hid: A -> hashcode -> A; (* WARNING: should only be used by hash-consing machinery *)
+}.
+Arguments hashP: clear implicits.
+
+Axiom unknown_hid: hashcode.
+Extract Constant unknown_hid => "-1".
+
+Definition ignore_hid {A} (hp: hashP A) (hv:A) := set_hid hp hv unknown_hid.
+
+Record hashExport {A:Type}:= {
+ get_info: hashcode -> ?? hashinfo A;
+ iterall: ((list pstring) -> hashcode -> hashinfo A -> ?? unit) -> ?? unit; (* iter on all elements in the hashtbl, by order of creation *)
+}.
+Arguments hashExport: clear implicits.
+
+Record hashConsing {A:Type}:= {
+ hC: hashinfo A -> ?? A;
+ (**** below: debugging or internal functions ****)
+ next_hid: unit -> ?? hashcode; (* should be strictly less old than ignore_hid *)
+ remove: hashinfo A -> ??unit; (* SHOULD NOT BE USED ! *)
+ next_log: pstring -> ?? unit; (* insert a log info (for the next introduced element) -- regiven by [iterall export] below *)
+ export: unit -> ?? hashExport A ;
+}.
+Arguments hashConsing: clear implicits.
+
+End HConsingDefs.
+
+(** recMode: this is mainly for Tests ! *)
+Inductive recMode:= StdRec | MemoRec | BareRec | BuggyRec.
+
+
+(* This a copy-paste from definitions in CompCert/Lib/CoqLib.v *)
+Lemma modusponens: forall (P Q: Prop), P -> (P -> Q) -> Q.
+Proof. auto. Qed.
+
+Ltac exploit x :=
+ refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _ _) _)
+ || refine (modusponens _ _ (x _ _ _) _)
+ || refine (modusponens _ _ (x _ _) _)
+ || refine (modusponens _ _ (x _) _).
diff --git a/lib/Impure/LICENSE b/lib/Impure/LICENSE
new file mode 100644
index 00000000..65c5ca88
--- /dev/null
+++ b/lib/Impure/LICENSE
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
diff --git a/lib/Impure/README.md b/lib/Impure/README.md
new file mode 100644
index 00000000..2b19d14a
--- /dev/null
+++ b/lib/Impure/README.md
@@ -0,0 +1,31 @@
+# `Impure`: importing OCaml functions as non-deterministic ones.
+
+The principle of this library is to encode the type `A -> B` of an
+OCaml function as a type `A -> ?? B` in Coq, where `?? B` is the type
+of an axiomatized monad that can be interpreted as `B -> Prop`. In
+other word, this encoding abstracts an OCaml function as a function
+returning a postcondition on its possible results (ie a relation between its
+parameter and its result). Side-effects are simply ignored. And
+reasoning on such a function is only possible in partial correctness.
+
+See further explanations and examples on [ImpureDemo](https://github.com/boulme/ImpureDemo).
+
+## Credits
+
+[Sylvain Boulmé](mailto:Sylvain.Boulme@univ-grenoble-alpes.fr).
+
+## Code Overview
+
+- [ImpMonads](ImpMonads.v) axioms of "impure computations" and some Coq models of these axioms.
+
+- [ImpConfig](ImpConfig.v) declares the `Impure` monad and defines its extraction.
+
+- [ImpCore](ImpCore.v) defines notations for the `Impure` monad and a `wlp_simplify` tactic (to reason about `Impure` functions in a Hoare-logic style).
+
+- [ImpPrelude](ImpPrelude.v) declares the data types exchanged with `Impure` oracles.
+
+- [ImpIO](ImpIO.v), [ImpLoops](ImpLoops.v), [ImpHCons](ImpHCons.v) declare `Impure` oracles and define operators from these oracles.
+ [ImpExtern](ImpExtern.v) exports all these impure operators.
+
+- [ocaml/](ocaml/) subdirectory containing the OCaml implementations of `Impure` oracles.
+
diff --git a/lib/Impure/ocaml/ImpHConsOracles.ml b/lib/Impure/ocaml/ImpHConsOracles.ml
new file mode 100644
index 00000000..68a33a91
--- /dev/null
+++ b/lib/Impure/ocaml/ImpHConsOracles.ml
@@ -0,0 +1,72 @@
+open ImpPrelude
+open HConsingDefs
+
+let make_dict (type key) (p: key Dict.hash_params) =
+ let module MyHashedType = struct
+ type t = key
+ let equal = p.Dict.test_eq
+ let hash = p.Dict.hashing
+ end in
+ let module MyHashtbl = Hashtbl.Make(MyHashedType) in
+ let dict = MyHashtbl.create 1000 in
+ {
+ Dict.set = (fun (k,d) -> MyHashtbl.replace dict k d);
+ Dict.get = (fun k -> MyHashtbl.find_opt dict k)
+ }
+
+
+exception Stop;;
+
+let xhCons (type a) (hp:a hashP) =
+ (* We use a hash-table, but a hash-set would be sufficient ! *)
+ (* Thus, we could use a weak hash-set, but prefer avoid it for easier debugging *)
+ (* Ideally, a parameter would allow to select between the weak or full version *)
+ let module MyHashedType = struct
+ type t = a hashinfo
+ let equal x y = hp.hash_eq x.hdata y.hdata
+ let hash x = Hashtbl.hash x.hcodes
+ end in
+ let module MyHashtbl = Hashtbl.Make(MyHashedType) in
+ let pick t =
+ let res = ref None in
+ try
+ MyHashtbl.iter (fun k d -> res:=Some (k,d); raise Stop) t;
+ None
+ with
+ | Stop -> !res
+ in
+ let t = MyHashtbl.create 1000 in
+ let logs = ref [] in
+ {
+ hC = (fun (k:a hashinfo) ->
+ (* DEBUG:
+ Printf.printf "*in %d -- look for hcodes= " (Obj.magic t);
+ List.iter (fun i -> Printf.printf "%d " i) k.hcodes;
+ print_newline();
+ *)
+ match MyHashtbl.find_opt t k with
+ | Some d -> d
+ | None ->
+ (* DEBUG: Printf.printf "*in %d -- new hid:%d" (Obj.magic t) (MyHashtbl.length t); print_newline(); *)
+ let d = hp.set_hid k.hdata (MyHashtbl.length t) in
+ MyHashtbl.add t {k with hdata = d } d; d);
+ next_log = (fun info -> logs := (MyHashtbl.length t, info)::(!logs));
+ next_hid = (fun () -> MyHashtbl.length t);
+ remove = (fun (x:a hashinfo) -> MyHashtbl.remove t x);
+ export = fun () ->
+ match pick t with
+ | None -> { get_info = (fun _ -> raise Not_found); iterall = (fun _ -> ()) }
+ | Some (k,_) ->
+ (* the state is fully copied at export ! *)
+ let logs = ref (List.rev_append (!logs) []) in
+ let rec step_log i =
+ match !logs with
+ | (j, info)::l' when i>=j -> logs:=l'; info::(step_log i)
+ | _ -> []
+ in let a = Array.make (MyHashtbl.length t) k in
+ MyHashtbl.iter (fun k d -> a.(hp.get_hid d) <- k) t;
+ {
+ get_info = (fun i -> a.(i));
+ iterall = (fun iter_node -> Array.iteri (fun i k -> iter_node (step_log i) i k) a)
+ }
+ }
diff --git a/lib/Impure/ocaml/ImpHConsOracles.mli b/lib/Impure/ocaml/ImpHConsOracles.mli
new file mode 100644
index 00000000..5075d176
--- /dev/null
+++ b/lib/Impure/ocaml/ImpHConsOracles.mli
@@ -0,0 +1,5 @@
+open ImpPrelude
+open HConsingDefs
+
+val make_dict : 'a Dict.hash_params -> ('a, 'b) Dict.t
+val xhCons: 'a hashP -> 'a hashConsing
diff --git a/lib/Impure/ocaml/ImpIOOracles.ml b/lib/Impure/ocaml/ImpIOOracles.ml
new file mode 100644
index 00000000..9e63c12d
--- /dev/null
+++ b/lib/Impure/ocaml/ImpIOOracles.ml
@@ -0,0 +1,142 @@
+(* Warning
+
+These oracles assumes the following extraction directives:
+ "Require Import ExtrOcamlString."
+
+*)
+
+open ImpPrelude
+(*
+open BinNums
+open Datatypes
+*)
+
+(* two auxiliary functions, for efficient mapping of "int" to "BinNums.positive" *)
+exception Overflow
+
+let aux_add: ('a, 'b) Hashtbl.t -> 'b Queue.t -> 'a -> 'b -> unit
+ = fun t q i p ->
+ if i < 1 then (* protection against wrap around *)
+ raise Overflow;
+ Queue.add p q;
+ Hashtbl.add t i p
+
+let memo_int2pos: int -> int -> BinNums.positive
+ = fun n ->
+ (* init of the Hashtbl *)
+ let n = max n 1 in
+ let t = Hashtbl.create n in
+ let q = Queue.create () in
+ aux_add t q 1 BinNums.Coq_xH ;
+ for i = 1 to (n-1)/2 do
+ let last = Queue.take q in
+ let ni = 2*i in
+ aux_add t q ni (BinNums.Coq_xO last);
+ aux_add t q (ni+1) (BinNums.Coq_xI last)
+ done;
+ if n mod 2 = 0 then (
+ let last = Queue.take q in
+ Hashtbl.add t n (BinNums.Coq_xO last)
+ );
+ (* memoized translation of i *)
+ let rec find i =
+ try
+ (* Printf.printf "-> %d\n" i; *)
+ Hashtbl.find t i
+ with Not_found ->
+ (* Printf.printf "<- %d\n" i; *)
+ if i <= 0 then
+ invalid_arg "non-positive integer"
+ else
+ let p = find (i/2) in
+ let pi = if i mod 2 = 0 then BinNums.Coq_xO p else BinNums.Coq_xI p in
+ Hashtbl.add t i pi;
+ pi
+ in find;;
+
+let new_exit_observer: (unit -> unit) -> (unit -> unit) ref
+ = fun f ->
+ let o = ref f in
+ at_exit (fun () -> !o());
+ o;;
+
+let set_exit_observer: (unit -> unit) ref * (unit -> unit) -> unit
+ = fun (r, f) -> r := f
+
+let rec print: pstring -> unit
+ = fun ps ->
+ match ps with
+ | Str l -> List.iter print_char l
+ | CamlStr s -> print_string s
+ | Concat(ps1,ps2) -> (print ps1; print ps2);;
+
+let println: pstring -> unit
+ = fun l -> print l; print_newline()
+
+let read_line () =
+ CamlStr (Stdlib.read_line());;
+
+exception ImpureFail of pstring;;
+
+let exn2string: exn -> pstring
+ = fun e -> CamlStr (Printexc.to_string e)
+
+let fail: pstring -> 'a
+ = fun s -> raise (ImpureFail s);;
+
+let try_with_fail: (unit -> 'a) * (pstring -> exn -> 'a) -> 'a
+ = fun (k1, k2) ->
+ try
+ k1()
+ with
+ | (ImpureFail s) as e -> k2 s e
+
+let try_with_any: (unit -> 'a) * (exn -> 'a) -> 'a
+ = fun (k1, k2) ->
+ try
+ k1()
+ with
+ | e -> k2 e
+
+(** MISC **)
+
+let rec posTr: BinNums.positive -> int
+= function
+ | BinNums.Coq_xH -> 1
+ | BinNums.Coq_xO p -> (posTr p)*2
+ | BinNums.Coq_xI p -> (posTr p)*2+1;;
+
+let zTr: BinNums.coq_Z -> int
+= function
+ | BinNums.Z0 -> 0
+ | BinNums.Zpos p -> posTr p
+ | BinNums.Zneg p -> - (posTr p)
+
+let ten = BinNums.Zpos (BinNums.Coq_xO (BinNums.Coq_xI (BinNums.Coq_xO BinNums.Coq_xH)))
+
+let rec string_of_pos (p:BinNums.positive) (acc: pstring): pstring
+= let (q,r) = BinInt.Z.pos_div_eucl p ten in
+ let acc0 = Concat (CamlStr (string_of_int (zTr r)), acc) in
+ match q with
+ | BinNums.Z0 -> acc0
+ | BinNums.Zpos p0 -> string_of_pos p0 acc0
+ | _ -> assert false
+
+(*
+let string_of_Z_debug: BinNums.coq_Z -> pstring
+= fun p -> CamlStr (string_of_int (zTr p))
+*)
+
+let string_of_Z: BinNums.coq_Z -> pstring
+= function
+ | BinNums.Z0 -> CamlStr "0"
+ | BinNums.Zpos p -> string_of_pos p (CamlStr "")
+ | BinNums.Zneg p -> Concat (CamlStr "-", string_of_pos p (CamlStr ""))
+
+let timer ((f:'a -> 'b), (x:'a)) : 'b =
+ Gc.compact();
+ let itime = (Unix.times()).Unix.tms_utime in
+ let r = f x in
+ let rt = (Unix.times()).Unix.tms_utime -. itime in
+ Printf.printf "time = %f\n" rt;
+ r
diff --git a/lib/Impure/ocaml/ImpIOOracles.mli b/lib/Impure/ocaml/ImpIOOracles.mli
new file mode 100644
index 00000000..6064286a
--- /dev/null
+++ b/lib/Impure/ocaml/ImpIOOracles.mli
@@ -0,0 +1,33 @@
+open ImpPrelude
+
+
+(*
+Memoized version of translation from int -> BinNums.positive.
+The first arg is an indicative bound on the max int translated:
+it pre-computes all positives lower or equal to this bound.
+*)
+val memo_int2pos: int -> int -> BinNums.positive
+
+val read_line: unit -> pstring
+
+val print: pstring -> unit
+
+val println: pstring -> unit
+
+val string_of_Z: BinNums.coq_Z -> pstring
+
+val timer : (('a -> 'b ) * 'a) -> 'b
+
+val new_exit_observer: (unit -> unit) -> (unit -> unit) ref
+
+val set_exit_observer: (unit -> unit) ref * (unit -> unit) -> unit
+
+val exn2string: exn -> pstring
+
+val fail: pstring -> 'a
+
+exception ImpureFail of pstring;;
+
+val try_with_fail: (unit -> 'a) * (pstring -> exn -> 'a) -> 'a
+
+val try_with_any: (unit -> 'a) * (exn -> 'a) -> 'a
diff --git a/lib/Impure/ocaml/ImpLoopOracles.ml b/lib/Impure/ocaml/ImpLoopOracles.ml
new file mode 100644
index 00000000..cb7625e5
--- /dev/null
+++ b/lib/Impure/ocaml/ImpLoopOracles.ml
@@ -0,0 +1,78 @@
+open ImpPrelude
+open Datatypes
+
+(** GENERIC ITERATIVE LOOP **)
+
+(* a simple version of loop *)
+let simple_loop: ('a * ('a -> ('a, 'b) sum)) -> 'b
+ = fun (a0, f) ->
+ let rec iter: 'a -> 'b
+ = fun a ->
+ match f a with
+ | Coq_inl a' -> iter a'
+ | Coq_inr b -> b
+ in
+ iter a0;;
+
+(* loop from while *)
+let while_loop: ('a * ('a -> ('a, 'b) sum)) -> 'b
+ = fun (a0, f) ->
+ let s = ref (f a0) in
+ while (match !s with Coq_inl _ -> true | _ -> false) do
+ match !s with
+ | Coq_inl a -> s:=f a
+ | _ -> assert false
+ done;
+ match !s with
+ | Coq_inr b -> b
+ | _ -> assert false;;
+
+let loop = simple_loop
+
+
+(** GENERIC FIXPOINTS **)
+
+let std_rec (recf: ('a -> 'b ) -> 'a -> 'b): 'a -> 'b =
+ let rec f x = recf f x in
+ f
+
+let memo_rec (recf: ('a -> 'b ) -> 'a -> 'b): 'a -> 'b =
+ let memo = Hashtbl.create 10 in
+ let rec f x =
+ try
+ Hashtbl.find memo x
+ with
+ Not_found ->
+ let r = recf f x in
+ Hashtbl.replace memo x r;
+ r
+ in f
+
+let bare_rec (recf: ('a -> 'b ) -> 'a -> 'b): 'a -> 'b =
+ let fix = ref (fun x -> failwith "init") in
+ fix := (fun x -> recf !fix x);
+ !fix;;
+
+let buggy_rec (recf: ('a -> 'b ) -> 'a -> 'b): 'a -> 'b =
+ let memo = ref None in
+ let rec f x =
+ match !memo with
+ | Some y -> y
+ | None ->
+ let r = recf f x in
+ memo := Some r;
+ r
+ in f
+
+let xrec_mode = ref MemoRec
+
+let xrec_set_option : recMode -> unit
+= fun m -> xrec_mode := m
+
+let xrec : (('a -> 'b ) -> 'a -> 'b ) -> ('a -> 'b )
+ = fun recf ->
+ match !xrec_mode with
+ | StdRec -> std_rec recf
+ | MemoRec -> memo_rec recf
+ | BareRec -> bare_rec recf
+ | BuggyRec -> buggy_rec recf
diff --git a/lib/Impure/ocaml/ImpLoopOracles.mli b/lib/Impure/ocaml/ImpLoopOracles.mli
new file mode 100644
index 00000000..194696a1
--- /dev/null
+++ b/lib/Impure/ocaml/ImpLoopOracles.mli
@@ -0,0 +1,8 @@
+open ImpPrelude
+open Datatypes
+
+val loop: ('a * ('a -> ('a, 'b) sum)) -> 'b
+
+val xrec_set_option: recMode -> unit
+
+val xrec: (('a -> 'b ) -> 'a -> 'b ) -> ('a -> 'b )
diff --git a/lib/Integers.v b/lib/Integers.v
index 68bff3a0..b6c41d8d 100644
--- a/lib/Integers.v
+++ b/lib/Integers.v
@@ -4,7 +4,7 @@
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
-(* Copyright Institut National de Recherche en Informatique et en *)
+(* Copyright Institut National de Recherstestche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Lesser General Public License as *)
(* published by the Free Software Foundation, either version 2.1 of *)
@@ -17,8 +17,9 @@
(** Formalizations of machine integers modulo $2^N$ #2<sup>N</sup>#. *)
Require Import Eqdep_dec Zquot Zwf.
-Require Import Coqlib Zbits.
+Require Import Coqlib Zbits Axioms.
Require Archi.
+Require Import Lia.
(** * Comparisons *)
@@ -30,6 +31,11 @@ Inductive comparison : Type :=
| Cgt : comparison (**r greater than *)
| Cge : comparison. (**r greater than or equal *)
+Definition comparison_eq: forall (x y: comparison), {x = y} + {x <> y}.
+Proof.
+ decide equality.
+Defined.
+
Definition negate_comparison (c: comparison): comparison :=
match c with
| Ceq => Cne
@@ -1190,6 +1196,34 @@ Proof.
rewrite <- half_modulus_modulus. apply unsigned_range.
Qed.
+Local Transparent repr.
+Lemma sign_bit_of_signed: forall x,
+ (testbit x (zwordsize - 1)) = lt x zero.
+Proof.
+ intro.
+ rewrite sign_bit_of_unsigned.
+ unfold lt.
+ unfold signed, unsigned.
+ simpl.
+ pose proof half_modulus_pos as HMOD.
+ destruct (zlt 0 half_modulus) as [HMOD' | HMOD'].
+ 2: lia.
+ clear HMOD'.
+ destruct (zlt (intval x) half_modulus) as [ LOW | HIGH].
+ {
+ destruct x as [ix RANGE].
+ simpl in *.
+ destruct (zlt ix 0). lia.
+ reflexivity.
+ }
+ destruct (zlt _ _) as [LOW' | HIGH']; trivial.
+ destruct x as [ix RANGE].
+ simpl in *.
+ rewrite half_modulus_modulus in *.
+ lia.
+Qed.
+Local Opaque repr.
+
Lemma bits_signed:
forall x i, 0 <= i ->
Z.testbit (signed x) i = testbit x (if zlt i zwordsize then i else zwordsize - 1).
@@ -2423,6 +2457,57 @@ Proof.
bit_solve. destruct (zlt (i + unsigned (sub iwordsize y)) zwordsize); auto.
Qed.
+Theorem shrx1_shr:
+ forall x,
+ ltu one (repr (zwordsize - 1)) = true ->
+ shrx x (repr 1) = shr (add x (shru x (repr (zwordsize - 1)))) (repr 1).
+Proof.
+ intros.
+ rewrite shrx_shr by assumption.
+ rewrite shl_mul_two_p.
+ rewrite mul_commut. rewrite mul_one.
+ change (repr 1) with one.
+ rewrite unsigned_one.
+ change (two_p 1) with 2.
+ unfold sub.
+ rewrite unsigned_one.
+ assert (0 <= 2 <= max_unsigned).
+ {
+ unfold max_unsigned, modulus.
+ unfold zwordsize in *.
+ unfold ltu in *.
+ rewrite unsigned_one in H.
+ rewrite unsigned_repr in H.
+ {
+ destruct (zlt 1 (Z.of_nat wordsize - 1)) as [ LT | NONE].
+ 2: discriminate.
+ clear H.
+ rewrite two_power_nat_two_p.
+ split.
+ lia.
+ set (w := (Z.of_nat wordsize)) in *.
+ assert ((two_p 2) <= (two_p w)) as MONO.
+ {
+ apply two_p_monotone.
+ lia.
+ }
+ change (two_p 2) with 4 in MONO.
+ lia.
+ }
+ generalize wordsize_max_unsigned.
+ fold zwordsize.
+ generalize wordsize_pos.
+ lia.
+ }
+ rewrite unsigned_repr by assumption.
+ simpl.
+ rewrite shru_lt_zero.
+ destruct (lt x zero).
+ reflexivity.
+ rewrite add_zero.
+ reflexivity.
+Qed.
+
Theorem shrx_carry:
forall x y,
ltu y (repr (zwordsize - 1)) = true ->
@@ -3701,6 +3786,103 @@ Proof.
unfold ltu. apply zlt_true. change (unsigned z < 63). rewrite A; lia.
Qed.
+Lemma shr'63:
+ forall x, (shr' x (Int.repr 63)) = if lt x zero then mone else zero.
+Proof.
+ intro.
+ unfold shr', mone, zero.
+ rewrite Int.unsigned_repr by (change Int.max_unsigned with 4294967295; lia).
+ apply same_bits_eq.
+ intros i BIT.
+ rewrite testbit_repr by assumption.
+ rewrite Z.shiftr_spec by lia.
+ rewrite bits_signed by lia.
+ simpl.
+ change zwordsize with 64 in *.
+ destruct (zlt _ _) as [LT | GE].
+ {
+ replace i with 0 in * by lia.
+ change (0 + 63) with (zwordsize - 1).
+ rewrite sign_bit_of_signed.
+ destruct (lt x _).
+ all: rewrite testbit_repr by (change zwordsize with 64 in *; lia).
+ all: simpl; reflexivity.
+ }
+ change (64 - 1) with (zwordsize - 1).
+ rewrite sign_bit_of_signed.
+ destruct (lt x _).
+ all: rewrite testbit_repr by (change zwordsize with 64 in *; lia).
+ { symmetry.
+ apply Ztestbit_m1.
+ tauto.
+ }
+ symmetry.
+ apply Ztestbit_0.
+Qed.
+
+Lemma shru'63:
+ forall x, (shru' x (Int.repr 63)) = if lt x zero then one else zero.
+Proof.
+ intro.
+ unfold shru'.
+ rewrite Int.unsigned_repr by (change Int.max_unsigned with 4294967295; lia).
+ apply same_bits_eq.
+ intros i BIT.
+ rewrite testbit_repr by assumption.
+ rewrite Z.shiftr_spec by lia.
+ unfold lt.
+ rewrite signed_zero.
+ unfold one, zero.
+ destruct (zlt _ 0) as [LT | GE].
+ {
+ rewrite testbit_repr by assumption.
+ destruct (zeq i 0) as [IZERO | INONZERO].
+ { subst i.
+ change (Z.testbit (unsigned x) (0 + 63)) with (testbit x (zwordsize - 1)).
+ rewrite sign_bit_of_signed.
+ unfold lt.
+ rewrite signed_zero.
+ destruct (zlt _ _); try lia.
+ reflexivity.
+ }
+ change (Z.testbit (unsigned x) (i + 63)) with (testbit x (i+63)).
+ rewrite bits_above by (change zwordsize with 64; lia).
+ rewrite Ztestbit_1.
+ destruct (zeq i 0); trivial.
+ subst i.
+ lia.
+ }
+ destruct (zeq i 0) as [IZERO | INONZERO].
+ { subst i.
+ change (Z.testbit (unsigned x) (0 + 63)) with (testbit x (zwordsize - 1)).
+ rewrite sign_bit_of_signed.
+ unfold lt.
+ rewrite signed_zero.
+ rewrite bits_zero.
+ destruct (zlt _ _); try lia; reflexivity.
+ }
+ change (Z.testbit (unsigned x) (i + 63)) with (testbit x (i + 63)).
+ rewrite bits_zero.
+ apply bits_above.
+ change zwordsize with 64.
+ lia.
+Qed.
+
+Theorem shrx'1_shr':
+ forall x,
+ Int.ltu Int.one (Int.repr (zwordsize - 1)) = true ->
+ shrx' x (Int.repr 1) = shr' (add x (shru' x (Int.repr (Int64.zwordsize - 1)))) (Int.repr 1).
+Proof.
+ intros.
+ rewrite shrx'_shr_2 by reflexivity.
+ change (Int.sub (Int.repr 64) (Int.repr 1)) with (Int.repr 63).
+ f_equal. f_equal.
+ rewrite shr'63.
+ rewrite shru'63.
+ rewrite shru'63.
+ destruct (lt x zero); reflexivity.
+Qed.
+
Remark int_ltu_2_inv:
forall y z,
Int.ltu y iwordsize' = true ->
@@ -4649,8 +4831,26 @@ End Int64.
Strategy 0 [Wordsize_64.wordsize].
+Definition int_eq: forall (i1 i2: int), {i1=i2} + {i1<>i2}.
+Proof.
+ generalize Z.eq_dec. intros.
+ destruct i1. destruct i2. generalize (H intval intval0). intro.
+ inversion H0.
+ - subst. left. assert (intrange = intrange0) by (apply proof_irr). congruence.
+ - right. intro. inversion H2. contradiction.
+Qed.
+
Notation int64 := Int64.int.
+Definition int64_eq: forall (i1 i2: int64), {i1=i2} + {i1<>i2}.
+Proof.
+ generalize Z.eq_dec. intros.
+ destruct i1. destruct i2. generalize (H intval intval0). intro.
+ inversion H0.
+ - subst. left. assert (intrange = intrange0) by (apply proof_irr). congruence.
+ - right. intro. inversion H2. contradiction.
+Qed.
+
Global Opaque Int.repr Int64.repr Byte.repr.
(** * Specialization to offsets in pointer values *)
@@ -4927,6 +5127,15 @@ Strategy 0 [Wordsize_Ptrofs.wordsize].
Notation ptrofs := Ptrofs.int.
+Definition ptrofs_eq: forall (i1 i2: ptrofs), {i1=i2} + {i1<>i2}.
+Proof.
+ generalize Z.eq_dec. intros.
+ destruct i1. destruct i2. generalize (H intval intval0). intro.
+ inversion H0.
+ - subst. left. assert (intrange = intrange0) by (apply proof_irr). congruence.
+ - right. intro. inversion H2. contradiction.
+Qed.
+
Global Opaque Ptrofs.repr.
Global Hint Resolve
diff --git a/lib/IterList.v b/lib/IterList.v
new file mode 100644
index 00000000..d28124c7
--- /dev/null
+++ b/lib/IterList.v
@@ -0,0 +1,112 @@
+Require Import Coqlib.
+Require Import Lia.
+
+(** TODO: are these def and lemma already defined in the standard library ?
+
+In this case, it should be better to reuse those of the standard library !
+
+*)
+
+Fixpoint iter {A} (n:nat) (f: A -> A) (x: A) {struct n}: A :=
+ match n with
+ | O => x
+ | S n0 => iter n0 f (f x)
+ end.
+
+Lemma iter_S A (n:nat) (f: A -> A): forall x, iter (S n) f x = f (iter n f x).
+Proof.
+ induction n; simpl; auto.
+ intros; erewrite <- IHn; simpl; auto.
+Qed.
+
+Lemma iter_plus A (n m:nat) (f: A -> A): forall x, iter (n+m) f x = iter m f (iter n f x).
+Proof.
+ induction n; simpl; auto.
+Qed.
+
+Definition iter_tail {A} (n:nat) (l: list A) := iter n (@tl A) l.
+
+Lemma iter_tail_S {A} (n:nat) (l: list A): iter_tail (S n) l = tl (iter_tail n l).
+Proof.
+ apply iter_S.
+Qed.
+
+Lemma iter_tail_plus A (n m:nat) (l: list A): iter_tail (n+m) l = iter_tail m (iter_tail n l).
+Proof.
+ apply iter_plus.
+Qed.
+
+Lemma iter_tail_length A l1: forall (l2: list A), iter_tail (length l1) (l1 ++ l2) = l2.
+Proof.
+ induction l1; auto.
+Qed.
+
+Lemma iter_tail_nil A n: @iter_tail A n nil = nil.
+Proof.
+ unfold iter_tail; induction n; simpl; auto.
+Qed.
+
+Lemma iter_tail_reach_nil A (l: list A): iter_tail (length l) l = nil.
+Proof.
+ rewrite (app_nil_end l) at 2.
+ rewrite iter_tail_length.
+ auto.
+Qed.
+
+Lemma length_iter_tail {A} (n:nat): forall (l: list A), (n <= List.length l)%nat -> (List.length l = n + List.length (iter_tail n l))%nat.
+Proof.
+ unfold iter_tail; induction n; auto.
+ intros l; destruct l. { simpl; lia. }
+ intros; simpl. erewrite IHn; eauto.
+ simpl in *; lia.
+Qed.
+
+Lemma iter_tail_S_ex {A} (n:nat): forall (l: list A), (n < length l)%nat -> exists x, iter_tail n l = x::(iter_tail (S n) l).
+Proof.
+ unfold iter_tail; induction n; simpl.
+ - intros l; destruct l; simpl; lia || eauto.
+ - intros l H; destruct (IHn (tl l)) as (x & H1).
+ + destruct l; simpl in *; try lia.
+ + rewrite H1; eauto.
+Qed.
+
+Lemma iter_tail_inject1 {A} (n1 n2:nat) (l: list A): (n1 <= List.length l)%nat -> (n2 <= List.length l)%nat -> iter_tail n1 l = iter_tail n2 l -> n1=n2.
+Proof.
+ intros H1 H2 EQ; exploit (length_iter_tail n1 l); eauto.
+ rewrite EQ.
+ rewrite (length_iter_tail n2 l); eauto.
+ lia.
+Qed.
+
+Lemma iter_tail_nil_inject {A} (n:nat) (l: list A): iter_tail n l = nil -> (List.length l <= n)%nat.
+Proof.
+ destruct (le_lt_dec n (List.length l)); try lia.
+ intros; exploit (iter_tail_inject1 n (length l) l); try lia.
+ rewrite iter_tail_reach_nil. auto.
+Qed.
+
+Lemma list_length_z_nat (A: Type) (l: list A): list_length_z l = Z.of_nat (length l).
+Proof.
+ induction l; auto.
+ rewrite list_length_z_cons. simpl. rewrite Zpos_P_of_succ_nat. lia.
+Qed.
+
+Lemma list_length_nat_z (A: Type) (l: list A): length l = Z.to_nat (list_length_z l).
+Proof.
+ intros; rewrite list_length_z_nat, Nat2Z.id. auto.
+Qed.
+
+Lemma is_tail_list_nth_z A (l1 l2: list A):
+ is_tail l1 l2 -> list_nth_z l2 ((list_length_z l2) - (list_length_z l1)) = list_nth_z l1 0.
+Proof.
+ induction 1; simpl.
+ - replace (list_length_z c - list_length_z c) with 0; lia || auto.
+ - assert (X: list_length_z (i :: c2) > list_length_z c1).
+ { rewrite !list_length_z_nat, <- Nat2Z.inj_gt.
+ exploit is_tail_bound; simpl; eauto.
+ lia. }
+ destruct (zeq (list_length_z (i :: c2) - list_length_z c1) 0) as [Y|Y]; try lia.
+ replace (Z.pred (list_length_z (i :: c2) - list_length_z c1)) with (list_length_z c2 - list_length_z c1); auto.
+ rewrite list_length_z_cons.
+ lia.
+Qed.
diff --git a/lib/Lattice.v b/lib/Lattice.v
index 6fed3f21..016dad75 100644
--- a/lib/Lattice.v
+++ b/lib/Lattice.v
@@ -31,7 +31,8 @@ Local Unset Case Analysis Schemes.
[bot], and an upper bound operation [lub].
Note that we do not demand that [lub] computes the least upper bound. *)
-Module Type SEMILATTICE.
+
+Module Type SEMILATTICE_WITHOUT_BOTTOM.
Parameter t: Type.
Parameter eq: t -> t -> Prop.
@@ -43,25 +44,124 @@ Module Type SEMILATTICE.
Parameter ge: t -> t -> Prop.
Axiom ge_refl: forall x y, eq x y -> ge x y.
Axiom ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
- Parameter bot: t.
- Axiom ge_bot: forall x, ge x bot.
Parameter lub: t -> t -> t.
Axiom ge_lub_left: forall x y, ge (lub x y) x.
Axiom ge_lub_right: forall x y, ge (lub x y) y.
+End SEMILATTICE_WITHOUT_BOTTOM.
+
+Module Type SEMILATTICE.
+ Include SEMILATTICE_WITHOUT_BOTTOM.
+ Parameter bot: t.
+ Axiom ge_bot: forall x, ge x bot.
End SEMILATTICE.
(** A semi-lattice ``with top'' is similar, but also has a greatest
element [top]. *)
Module Type SEMILATTICE_WITH_TOP.
-
Include SEMILATTICE.
Parameter top: t.
Axiom ge_top: forall x, ge top x.
-
End SEMILATTICE_WITH_TOP.
+
+Module ADD_BOTTOM(L : SEMILATTICE_WITHOUT_BOTTOM) <: SEMILATTICE.
+ Definition t := option L.t.
+ Definition eq (a b : t) :=
+ match a, b with
+ | None, None => True
+ | Some x, Some y => L.eq x y
+ | Some _, None | None, Some _ => False
+ end.
+
+ Lemma eq_refl: forall x, eq x x.
+ Proof.
+ unfold eq; destruct x; trivial.
+ apply L.eq_refl.
+ Qed.
+
+ Lemma eq_sym: forall x y, eq x y -> eq y x.
+ Proof.
+ unfold eq; destruct x; destruct y; trivial.
+ apply L.eq_sym.
+ Qed.
+
+ Lemma eq_trans: forall x y z, eq x y -> eq y z -> eq x z.
+ Proof.
+ unfold eq; destruct x; destruct y; destruct z; trivial.
+ - apply L.eq_trans.
+ - contradiction.
+ Qed.
+
+ Definition beq (x y : t) :=
+ match x, y with
+ | None, None => true
+ | Some x, Some y => L.beq x y
+ | Some _, None | None, Some _ => false
+ end.
+
+ Lemma beq_correct: forall x y, beq x y = true -> eq x y.
+ Proof.
+ unfold beq, eq.
+ destruct x; destruct y; trivial; try congruence.
+ apply L.beq_correct.
+ Qed.
+
+ Definition ge (x y : t) :=
+ match x, y with
+ | None, Some _ => False
+ | _, None => True
+ | Some a, Some b => L.ge a b
+ end.
+
+ Lemma ge_refl: forall x y, eq x y -> ge x y.
+ Proof.
+ unfold eq, ge.
+ destruct x; destruct y; trivial.
+ apply L.ge_refl.
+ Qed.
+
+ Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
+ Proof.
+ unfold ge.
+ destruct x; destruct y; destruct z; trivial; try contradiction.
+ apply L.ge_trans.
+ Qed.
+
+ Definition bot: t := None.
+ Lemma ge_bot: forall x, ge x bot.
+ Proof.
+ unfold ge, bot.
+ destruct x; trivial.
+ Qed.
+
+ Definition lub (a b : t) :=
+ match a, b with
+ | None, _ => b
+ | _, None => a
+ | (Some x), (Some y) => Some (L.lub x y)
+ end.
+
+ Lemma ge_lub_left: forall x y, ge (lub x y) x.
+ Proof.
+ unfold ge, lub.
+ destruct x; destruct y; trivial.
+ - apply L.ge_lub_left.
+ - apply L.ge_refl.
+ apply L.eq_refl.
+ Qed.
+
+ Lemma ge_lub_right: forall x y, ge (lub x y) y.
+ Proof.
+ unfold ge, lub.
+ destruct x; destruct y; trivial.
+ - apply L.ge_lub_right.
+ - apply L.ge_refl.
+ apply L.eq_refl.
+ Qed.
+End ADD_BOTTOM.
+
(** * Semi-lattice over maps *)
Set Implicit Arguments.
diff --git a/lib/Maps.v b/lib/Maps.v
index 6bc6e14b..456a1a9a 100644
--- a/lib/Maps.v
+++ b/lib/Maps.v
@@ -117,6 +117,19 @@ Module Type TREE.
forall (m1: t A) (m2: t B) (i: elt),
get i (combine f m1 m2) = f (get i m1) (get i m2).
+ Parameter combine_null :
+ forall (A B C: Type) (f: A -> B -> option C),
+ t A -> t B -> t C.
+
+ Axiom gcombine_null:
+ forall (A B C: Type) (f: A -> B -> option C),
+ forall (m1: t A) (m2: t B) (i: elt),
+ get i (combine_null f m1 m2) =
+ match (get i m1), (get i m2) with
+ | (Some x1), (Some x2) => f x1 x2
+ | _, _ => None
+ end.
+
(** Enumerating the bindings of a tree. *)
Parameter elements:
forall (A: Type), t A -> list (elt * A).
@@ -152,6 +165,12 @@ Module Type TREE.
forall (A B: Type) (f: B -> A -> B) (v: B) (m: t A),
fold1 f m v =
List.fold_left (fun a p => f a (snd p)) (elements m) v.
+
+ Parameter bempty_canon :
+ forall (A : Type), t A -> bool.
+ Axiom bempty_canon_correct:
+ forall (A : Type) (tr : t A) (i : elt),
+ bempty_canon tr = true -> get i tr = None.
End TREE.
(** * The abstract signatures of maps *)
@@ -262,6 +281,12 @@ Module PTree <: TREE.
induction i; simpl; auto.
Qed.
+ Definition bempty_canon (A : Type) (tr : t A) : bool :=
+ match tr with
+ | Leaf => true
+ | _ => false
+ end.
+
Theorem gss:
forall (A: Type) (i: positive) (x: A) (m: t A), get i (set i x m) = Some x.
Proof.
@@ -270,7 +295,16 @@ Module PTree <: TREE.
Lemma gleaf : forall (A : Type) (i : positive), get i (Leaf : t A) = None.
Proof. exact gempty. Qed.
-
+
+ Lemma bempty_canon_correct:
+ forall (A : Type) (tr : t A) (i : elt),
+ bempty_canon tr = true -> get i tr = None.
+ Proof.
+ destruct tr; intros.
+ - rewrite gleaf; trivial.
+ - discriminate.
+ Qed.
+
Theorem gso:
forall (A: Type) (i j: positive) (x: A) (m: t A),
i <> j -> get i (set j x m) = get i m.
@@ -626,7 +660,81 @@ Module PTree <: TREE.
auto.
Qed.
- Fixpoint xelements (A : Type) (m : t A) (i : positive)
+ Section COMBINE_NULL.
+
+ Variables A B C: Type.
+ Variable f: A -> B -> option C.
+
+
+ Fixpoint combine_null (m1: t A) (m2: t B) {struct m1} : t C :=
+ match m1, m2 with
+ | (Node l1 o1 r1), (Node l2 o2 r2) =>
+ Node' (combine_null l1 l2)
+ (match o1, o2 with
+ | (Some x1), (Some x2) => f x1 x2
+ | _, _ => None
+ end)
+ (combine_null r1 r2)
+ | _, _ => Leaf
+ end.
+
+ Theorem gcombine_null:
+ forall (m1: t A) (m2: t B) (i: positive),
+ get i (combine_null m1 m2) =
+ match (get i m1), (get i m2) with
+ | (Some x1), (Some x2) => f x1 x2
+ | _, _ => None
+ end.
+ Proof.
+ induction m1; intros; simpl.
+ - rewrite gleaf. rewrite gleaf.
+ reflexivity.
+ - destruct m2; simpl.
+ + rewrite gleaf. rewrite gleaf.
+ destruct get; reflexivity.
+ + rewrite gnode'.
+ destruct i; simpl; try rewrite IHm1_1; try rewrite IHm1; trivial.
+ Qed.
+
+ End COMBINE_NULL.
+
+ Section REMOVE_TREE.
+
+ Variables A B: Type.
+
+ Fixpoint remove_t (m1: t A) (m2: t B) {struct m1} : t A :=
+ match m1, m2 with
+ | Leaf, _ | _, Leaf => m1
+ | (Node l1 o1 r1), (Node l2 o2 r2) =>
+ Node' (remove_t l1 l2)
+ (match o2 with
+ | Some _ => None
+ | None => o1
+ end)
+ (remove_t r1 r2)
+ end.
+
+ Theorem gremove_t:
+ forall m1 : t A,
+ forall m2 : t B,
+ forall i : positive,
+ get i (remove_t m1 m2) = match get i m2 with
+ | None => get i m1
+ | Some _ => None
+ end.
+ Proof.
+ induction m1; intros; simpl.
+ - rewrite gleaf.
+ destruct get; reflexivity.
+ - destruct m2; simpl.
+ + rewrite gleaf.
+ reflexivity.
+ + rewrite gnode'.
+ destruct i; simpl; try rewrite IHm1_1; try rewrite IHm1; trivial.
+ Qed.
+ End REMOVE_TREE.
+
+ Fixpoint xelements (A : Type) (m : t A) (i : positive)
(k: list (positive * A)) {struct m}
: list (positive * A) :=
match m with
@@ -959,6 +1067,36 @@ Module PTree <: TREE.
intros. apply fold1_xelements with (l := @nil (positive * A)).
Qed.
+ Local Open Scope positive.
+ Lemma set_disjoint1:
+ forall (A: Type)(i d : elt) (m: t A) (x y: A),
+ set (i + d) y (set i x m) = set i x (set (i + d) y m).
+ Proof.
+ induction i; destruct d; destruct m; intro; simpl; trivial;
+ intro; congruence.
+ Qed.
+
+ Local Open Scope positive.
+ Lemma set_disjoint:
+ forall (A: Type)(i j : elt) (m: t A) (x y: A),
+ i <> j ->
+ set j y (set i x m) = set i x (set j y m).
+ Proof.
+ intros.
+ destruct (Pos.compare_spec i j) as [Heq | Hlt | Hlt].
+ { congruence. }
+ {
+ rewrite (Pos.lt_iff_add i j) in Hlt.
+ destruct Hlt as [d Hd].
+ subst j.
+ apply set_disjoint1.
+ }
+ rewrite (Pos.lt_iff_add j i) in Hlt.
+ destruct Hlt as [d Hd].
+ subst i.
+ symmetry.
+ apply set_disjoint1.
+ Qed.
End PTree.
(** * An implementation of maps over type [positive] *)
@@ -1036,6 +1174,15 @@ Module PMap <: MAP.
intros. unfold set. simpl. decEq. apply PTree.set2.
Qed.
+ Local Open Scope positive.
+ Lemma set_disjoint:
+ forall (A: Type) (i j : elt) (x y: A) (m: t A),
+ i <> j ->
+ set j y (set i x m) = set i x (set j y m).
+ Proof.
+ intros. unfold set. decEq. apply PTree.set_disjoint. assumption.
+ Qed.
+
End PMap.
(** * An implementation of maps over any type that injects into type [positive] *)
@@ -1103,6 +1250,16 @@ Module IMap(X: INDEXED_TYPE).
intros. unfold set. apply PMap.set2.
Qed.
+ Lemma set_disjoint:
+ forall (A: Type) (i j : elt) (x y: A) (m: t A),
+ i <> j ->
+ set j y (set i x m) = set i x (set j y m).
+ Proof.
+ intros. unfold set. apply PMap.set_disjoint.
+ intro INEQ.
+ assert (i = j) by (apply X.index_inj; auto).
+ auto.
+ Qed.
End IMap.
Module ZIndexed.
diff --git a/lib/OptionMonad.v b/lib/OptionMonad.v
new file mode 100644
index 00000000..18430e04
--- /dev/null
+++ b/lib/OptionMonad.v
@@ -0,0 +1,70 @@
+(* Declare Scope option_monad_scope. *)
+
+Notation "'SOME' X <- A 'IN' B" := (match A with Some X => B | None => None end)
+ (at level 200, X ident, A at level 100, B at level 200)
+ : option_monad_scope.
+
+Notation "'ASSERT' A 'IN' B" := (if A then B else None)
+ (at level 200, A at level 100, B at level 200)
+ : option_monad_scope.
+
+Local Open Scope option_monad_scope.
+
+
+(** Simple tactics for option-monad *)
+
+Ltac deepest_match exp :=
+ match exp with
+ | context f [match ?expr with | _ => _ end] => ltac: (deepest_match expr)
+ | _ => exp
+ end.
+
+Ltac autodestruct :=
+ let EQ := fresh "EQ" in
+ match goal with
+ | |- context f [match ?expr with | _ => _ end] =>
+ let t := ltac: (deepest_match expr) in
+ destruct t eqn:EQ; generalize EQ; clear EQ; congruence || trivial
+ end.
+
+(* deprecated version of "autodestruct". the new one seems a better replacement *)
+Ltac dummy_autodestruct :=
+ let EQ := fresh "EQ" in
+ match goal with
+ | |- context f [match ?expr with | _ => _ end] => destruct expr eqn:EQ; generalize EQ; clear EQ; congruence || trivial
+ end.
+
+Lemma destruct_SOME A B (P: option B -> Prop) (e: option A) (f: A -> option B):
+ (forall x, e = Some x -> P (f x)) -> (e = None -> P None) -> (P (SOME x <- e IN f x)).
+Proof.
+ intros; destruct e; simpl; auto.
+Qed.
+
+Lemma destruct_ASSERT B (P: option B -> Prop) (e: bool) (x: option B):
+ (e = true -> P x) -> (e = false -> P None) -> (P (ASSERT e IN x)).
+Proof.
+ intros; destruct e; simpl; auto.
+Qed.
+
+Ltac inversion_SOME x :=
+ try (eapply destruct_SOME; [ let x := fresh x in intro x | simpl; try congruence ]).
+
+Ltac inversion_ASSERT :=
+ try (eapply destruct_ASSERT; [ idtac | simpl; try congruence ]).
+
+Ltac simplify_someHyp :=
+ match goal with
+ | H: None = Some _ |- _ => inversion H; clear H; subst
+ | H: Some _ = None |- _ => inversion H; clear H; subst
+ | H: ?t = ?t |- _ => clear H
+ | H: Some _ = Some _ |- _ => inversion H; clear H; subst
+ | H: Some _ <> None |- _ => clear H
+ | H: None <> Some _ |- _ => clear H
+ | H: _ = Some _ |- _ => (try rewrite !H in * |- *); generalize H; clear H
+ end.
+
+Ltac simplify_someHyps :=
+ repeat (simplify_someHyp; simpl in * |- *).
+
+Ltac try_simplify_someHyps :=
+ try (intros; simplify_someHyps; eauto).
diff --git a/lib/UnionFind.v b/lib/UnionFind.v
index 1bc2f657..1c37a35b 100644
--- a/lib/UnionFind.v
+++ b/lib/UnionFind.v
@@ -126,6 +126,15 @@ Module Type UNIONFIND.
pathlen uf x + pathlen uf b + 1
else
pathlen uf x.
+ Axiom pathlen_union:
+ forall uf a b x,
+ pathlen (union uf a b) x =
+ if elt_eq (repr uf a) (repr uf b) then
+ pathlen uf x
+ else if elt_eq (repr uf x) (repr uf a) then
+ (pathlen uf x)+1
+ else
+ (pathlen uf x).
Axiom pathlen_gt_merge:
forall uf a b x y,
repr uf x = repr uf y ->
@@ -533,6 +542,7 @@ Qed.
End PATHLEN.
+
(* Path length and merge *)
Lemma pathlen_merge:
@@ -551,16 +561,49 @@ Proof.
set (uf' := identify uf (repr uf a) b (repr_res_none uf a) (not_eq_sym n)).
pattern x. apply (well_founded_ind (mwf uf')); intros.
rewrite (pathlen_unroll uf'). destruct (M.get x0 (m uf')) as [x'|] eqn:G.
- rewrite H; auto. simpl in G. rewrite M.gsspec in G.
- destruct (M.elt_eq x0 (repr uf a)). rewrite e. rewrite repr_canonical. rewrite dec_eq_true.
- inversion G. subst x'. rewrite dec_eq_false; auto.
- replace (pathlen uf (repr uf a)) with 0. lia.
- symmetry. apply pathlen_none. apply repr_res_none.
- rewrite (repr_unroll uf x0), (pathlen_unroll uf x0); rewrite G.
- destruct (M.elt_eq (repr uf x') (repr uf a)); lia.
- simpl in G. rewrite M.gsspec in G. destruct (M.elt_eq x0 (repr uf a)); try discriminate.
- rewrite (repr_none uf x0) by auto. rewrite dec_eq_false; auto.
- symmetry. apply pathlen_zero; auto. apply repr_none; auto.
+ + rewrite H; auto. clear H. simpl in G. rewrite M.gsspec in G.
+ destruct (M.elt_eq x0 (repr uf a)).
+ - rewrite e, repr_canonical, dec_eq_true.
+ inversion G. subst x'. rewrite dec_eq_false; auto.
+ replace (pathlen uf (repr uf a)) with 0; try lia.
+ symmetry. apply pathlen_none. apply repr_res_none.
+ - rewrite (repr_unroll uf x0), (pathlen_unroll uf x0), G.
+ destruct (M.elt_eq (repr uf x') (repr uf a)); lia.
+ + clear H; simpl in G. rewrite M.gsspec in G. destruct (M.elt_eq x0 (repr uf a)); try discriminate.
+ rewrite (repr_none uf x0) by auto. rewrite dec_eq_false; auto.
+ symmetry. apply pathlen_zero; auto. apply repr_none; auto.
+Qed.
+
+Lemma pathlen_union:
+ forall uf a b x,
+ pathlen (union uf a b) x =
+ if M.elt_eq (repr uf a) (repr uf b) then
+ pathlen uf x
+ else if M.elt_eq (repr uf x) (repr uf a) then
+ (pathlen uf x)+1
+ else
+ (pathlen uf x).
+Proof.
+ intros. unfold union.
+ destruct (M.elt_eq (repr uf a) (repr uf b)).
+ auto.
+ set (uf' := identify uf _ _ _ _).
+ assert (LENa: pathlen uf (repr uf a) = 0).
+ { apply pathlen_none. apply repr_res_none. }
+ pattern x. apply (well_founded_ind (mwf uf')); intros.
+ rewrite (pathlen_unroll uf'). destruct (M.get x0 (m uf')) as [x'|] eqn:G.
+ + rewrite H; auto. clear H. simpl in G. rewrite M.gsspec in G.
+ destruct (M.elt_eq x0 (repr uf a)).
+ - inversion G; clear G. subst.
+ rewrite !repr_canonical, dec_eq_true.
+ rewrite dec_eq_false; auto.
+ rewrite LENa. rewrite (pathlen_none uf (repr uf b)); try lia.
+ apply repr_res_none.
+ - rewrite (repr_unroll uf x0), G, ! (pathlen_some _ _ _ G).
+ destruct (M.elt_eq _ _); auto.
+ + clear H. simpl in G. rewrite M.gsspec in G.
+ destruct (M.elt_eq _ (repr uf a)); try discriminate.
+ rewrite (repr_none _ _ G), !(pathlen_none _ _ G), dec_eq_false; auto.
Qed.
Lemma pathlen_gt_merge:
diff --git a/lib/extra/HashedMap.v b/lib/extra/HashedMap.v
new file mode 100644
index 00000000..a7d6f589
--- /dev/null
+++ b/lib/extra/HashedMap.v
@@ -0,0 +1,460 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import ZArith.
+Require Import Bool.
+Require Import List.
+Require Coq.Logic.Eqdep_dec.
+
+(* begin from Maps *)
+Fixpoint prev_append (i j: positive) {struct i} : positive :=
+ match i with
+ | xH => j
+ | xI i' => prev_append i' (xI j)
+ | xO i' => prev_append i' (xO j)
+ end.
+
+Definition prev (i: positive) : positive :=
+ prev_append i xH.
+
+Lemma prev_append_prev i j:
+ prev (prev_append i j) = prev_append j i.
+Proof.
+ revert j. unfold prev.
+ induction i as [i IH|i IH|]. 3: reflexivity.
+ intros j. simpl. rewrite IH. reflexivity.
+ intros j. simpl. rewrite IH. reflexivity.
+Qed.
+
+Lemma prev_involutive i :
+ prev (prev i) = i.
+Proof (prev_append_prev i xH).
+
+Lemma prev_append_inj i j j' :
+ prev_append i j = prev_append i j' -> j = j'.
+Proof.
+ revert j j'.
+ induction i as [i Hi|i Hi|]; intros j j' H; auto;
+ specialize (Hi _ _ H); congruence.
+Qed.
+
+(* end from Maps *)
+
+Lemma orb_idem: forall b, orb b b = b.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Lemma andb_idem: forall b, andb b b = b.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Lemma andb_negb_false: forall b, andb b (negb b) = false.
+Proof.
+ destruct b; reflexivity.
+Qed.
+
+Hint Rewrite orb_false_r andb_false_r andb_true_r orb_true_r orb_idem andb_idem andb_negb_false : pmap.
+
+Parameter T : Type.
+Parameter T_eq_dec : forall (x y : T), {x = y} + {x <> y}.
+
+Inductive pmap : Type :=
+| Empty : pmap
+| Node : pmap -> option T -> pmap -> pmap.
+Definition empty := Empty.
+
+Definition is_empty x :=
+ match x with
+ | Empty => true
+ | Node _ _ _ => false
+ end.
+
+Definition is_some (x : option T) :=
+ match x with
+ | Some _ => true
+ | None => false
+ end.
+
+Fixpoint wf x :=
+ match x with
+ | Empty => true
+ | Node b0 f b1 =>
+ (wf b0) && (wf b1) &&
+ ((negb (is_empty b0)) || (is_some f) || (negb (is_empty b1)))
+ end.
+
+Definition iswf x := (wf x)=true.
+
+Lemma empty_wf : iswf empty.
+Proof.
+ reflexivity.
+Qed.
+
+Definition pmap_eq :
+ forall s s': pmap, { s=s' } + { s <> s' }.
+Proof.
+ generalize T_eq_dec.
+ induction s; destruct s'; repeat decide equality.
+Qed.
+
+Fixpoint get (i : positive) (s : pmap) {struct i} : option T :=
+ match s with
+ | Empty => None
+ | Node b0 f b1 =>
+ match i with
+ | xH => f
+ | xO ii => get ii b0
+ | xI ii => get ii b1
+ end
+ end.
+
+Lemma gempty :
+ forall i : positive,
+ get i Empty = None.
+Proof.
+ destruct i; simpl; reflexivity.
+Qed.
+
+Hint Resolve gempty : pmap.
+Hint Rewrite gempty : pmap.
+
+Definition node (b0 : pmap) (f : option T) (b1 : pmap) : pmap :=
+ match b0, f, b1 with
+ | Empty, None, Empty => Empty
+ | _, _, _ => Node b0 f b1
+ end.
+
+Lemma wf_node :
+ forall b0 f b1,
+ iswf b0 -> iswf b1 -> iswf (node b0 f b1).
+Proof.
+ destruct b0; destruct f; destruct b1; simpl.
+ all: unfold iswf; simpl; intros; trivial.
+ all: autorewrite with pmap; trivial.
+ all: rewrite H.
+ all: rewrite H0.
+ all: reflexivity.
+Qed.
+
+Hint Resolve wf_node: pmap.
+
+Lemma gnode :
+ forall b0 f b1 i,
+ get i (node b0 f b1) =
+ get i (Node b0 f b1).
+Proof.
+ destruct b0; simpl; trivial.
+ destruct f; simpl; trivial.
+ destruct b1; simpl; trivial.
+ intro.
+ rewrite gempty.
+ destruct i; simpl; trivial.
+ all: symmetry; apply gempty.
+Qed.
+
+Hint Rewrite gnode : pmap.
+
+Fixpoint set (i : positive) (j : T) (s : pmap) {struct i} : pmap :=
+ match s with
+ | Empty =>
+ match i with
+ | xH => Node Empty (Some j) Empty
+ | xO ii => Node (set ii j Empty) None Empty
+ | xI ii => Node Empty None (set ii j Empty)
+ end
+ | Node b0 f b1 =>
+ match i with
+ | xH => Node b0 (Some j) b1
+ | xO ii => Node (set ii j b0) f b1
+ | xI ii => Node b0 f (set ii j b1)
+ end
+ end.
+
+Lemma set_nonempty:
+ forall i j s, is_empty (set i j s) = false.
+Proof.
+ induction i; destruct s; simpl; trivial.
+Qed.
+
+Hint Rewrite set_nonempty : pmap.
+Hint Resolve set_nonempty : pmap.
+
+Lemma wf_set:
+ forall i j s, (iswf s) -> (iswf (set i j s)).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: unfold iswf in *; simpl.
+ all: autorewrite with pmap; simpl; trivial.
+ 1,3: auto with pmap.
+ all: intro Z.
+ all: repeat rewrite andb_true_iff in Z.
+ all: intuition.
+Qed.
+
+Hint Resolve wf_set : pset.
+
+Theorem gss :
+ forall (i : positive) (j : T) (s : pmap),
+ get i (set i j s) = Some j.
+Proof.
+ induction i; destruct s; simpl; auto.
+Qed.
+
+Hint Resolve gss : pmap.
+Hint Rewrite gss : pmap.
+
+Theorem gso :
+ forall (i j : positive) (k : T) (s : pmap),
+ i <> j ->
+ get j (set i k s) = get j s.
+Proof.
+ induction i; destruct j; destruct s; simpl; intro; auto with pmap.
+ 5, 6: congruence.
+ all: rewrite IHi by congruence.
+ all: trivial.
+ all: apply gempty.
+Qed.
+
+Hint Resolve gso : pmap.
+
+Fixpoint remove (i : positive) (s : pmap) { struct i } : pmap :=
+ match i with
+ | xH =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node b0 None b1
+ end
+ | xO ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node (remove ii b0) f b1
+ end
+ | xI ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => node b0 f (remove ii b1)
+ end
+ end.
+
+Lemma wf_remove :
+ forall i s, (iswf s) -> (iswf (remove i s)).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: unfold iswf in *; simpl.
+ all: intro Z.
+ all: repeat rewrite andb_true_iff in Z.
+ all: apply wf_node.
+ all: intuition.
+ all: apply IHi.
+ all: assumption.
+Qed.
+
+Fixpoint remove_noncanon (i : positive) (s : pmap) { struct i } : pmap :=
+ match i with
+ | xH =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node b0 None b1
+ end
+ | xO ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node (remove_noncanon ii b0) f b1
+ end
+ | xI ii =>
+ match s with
+ | Empty => Empty
+ | Node b0 f b1 => Node b0 f (remove_noncanon ii b1)
+ end
+ end.
+
+Lemma remove_noncanon_same:
+ forall i j s, (get j (remove i s)) = (get j (remove_noncanon i s)).
+Proof.
+ induction i; destruct s; simpl; trivial.
+ all: rewrite gnode.
+ 3: reflexivity.
+ all: destruct j; simpl; trivial.
+Qed.
+
+Lemma remove_empty :
+ forall i, remove i Empty = Empty.
+Proof.
+ induction i; simpl; trivial.
+Qed.
+
+Hint Rewrite remove_empty : pmap.
+Hint Resolve remove_empty : pmap.
+
+Lemma gremove_noncanon_s :
+ forall i : positive,
+ forall s : pmap,
+ get i (remove_noncanon i s) = None.
+Proof.
+ induction i; destruct s; simpl; trivial.
+Qed.
+
+Theorem grs :
+ forall i : positive,
+ forall s : pmap,
+ get i (remove i s) = None.
+Proof.
+ intros.
+ rewrite remove_noncanon_same.
+ apply gremove_noncanon_s.
+Qed.
+
+Hint Resolve grs : pmap.
+Hint Rewrite grs : pmap.
+
+Lemma gremove_noncanon_o :
+ forall i j : positive,
+ forall s : pmap,
+ i<>j ->
+ get j (remove_noncanon i s) = get j s.
+Proof.
+ induction i; destruct j; destruct s; simpl; intro; trivial.
+ 1, 2: rewrite IHi by congruence.
+ 1, 2: reflexivity.
+ congruence.
+Qed.
+
+Theorem gro :
+ forall (i j : positive) (s : pmap),
+ i<>j ->
+ get j (remove i s) = get j s.
+Proof.
+ intros.
+ rewrite remove_noncanon_same.
+ apply gremove_noncanon_o.
+ assumption.
+Qed.
+
+Hint Resolve gro : pmap.
+
+Section MAP2.
+
+ Variable f : option T -> option T -> option T.
+
+ Section NONE_NONE.
+ Hypothesis f_none_none : f None None = None.
+
+ Fixpoint map2_Empty (b : pmap) :=
+ match b with
+ | Empty => Empty
+ | Node b0 bf b1 =>
+ node (map2_Empty b0) (f None bf) (map2_Empty b1)
+ end.
+
+ Lemma gmap2_Empty: forall i b,
+ get i (map2_Empty b) = f None (get i b).
+ Proof.
+ induction i; destruct b as [ | b0 bf b1]; intros; simpl in *.
+ all: try congruence.
+ - replace
+ (match node (map2_Empty b0) (f None bf) (map2_Empty b1) with
+ | Empty => None
+ | Node _ _ c1 => get i c1
+ end)
+ with (get (xI i) (node (map2_Empty b0) (f None bf) (map2_Empty b1))).
+ + rewrite gnode.
+ simpl. apply IHi.
+ + destruct node; auto with pmap.
+ - replace
+ (match node (map2_Empty b0) (f None bf) (map2_Empty b1) with
+ | Empty => None
+ | Node c0 _ _ => get i c0
+ end)
+ with (get (xO i) (node (map2_Empty b0) (f None bf) (map2_Empty b1))).
+ + rewrite gnode.
+ simpl. apply IHi.
+ + destruct node; auto with pmap.
+ - change (match node (map2_Empty b0) (f None bf) (map2_Empty b1) with
+ | Empty => None
+ | Node _ cf _ => cf
+ end) with (get xH (node (map2_Empty b0) (f None bf) (map2_Empty b1))).
+ rewrite gnode. reflexivity.
+ Qed.
+
+ Fixpoint map2 (a b : pmap) :=
+ match a with
+ | Empty => map2_Empty b
+ | (Node a0 af a1) =>
+ match b with
+ | (Node b0 bf b1) =>
+ node (map2 a0 b0) (f af bf) (map2 a1 b1)
+ | Empty =>
+ node (map2 a0 Empty) (f af None) (map2 a1 Empty)
+ end
+ end.
+
+ Lemma gmap2: forall a b i,
+ get i (map2 a b) = f (get i a) (get i b).
+ Proof.
+ induction a as [ | a0 IHa0 af a1 IHa1]; intros; simpl.
+ { rewrite gmap2_Empty.
+ rewrite gempty.
+ reflexivity. }
+ destruct b as [ | b0 bf b1 ]; simpl; rewrite gnode.
+ - destruct i; simpl.
+ + rewrite IHa1. rewrite gempty.
+ reflexivity.
+ + rewrite IHa0. rewrite gempty.
+ reflexivity.
+ + reflexivity.
+ - destruct i; simpl; congruence.
+ Qed.
+ End NONE_NONE.
+
+ Section IDEM.
+ Hypothesis f_idem : forall x, f x x = x.
+
+ Fixpoint map2_idem (a b : pmap) :=
+ if pmap_eq a b then a else
+ match a with
+ | Empty => map2_Empty b
+ | (Node a0 af a1) =>
+ match b with
+ | (Node b0 bf b1) =>
+ node (map2_idem a0 b0) (f af bf) (map2_idem a1 b1)
+ | Empty =>
+ node (map2_idem a0 Empty) (f af None) (map2_idem a1 Empty)
+ end
+ end.
+
+ Lemma gmap2_idem: forall a b i,
+ get i (map2_idem a b) = f (get i a) (get i b).
+ Proof.
+ induction a as [ | a0 IHa0 af a1 IHa1]; intros; simpl.
+ { destruct pmap_eq.
+ - subst b. rewrite gempty. congruence.
+ - rewrite gempty.
+ rewrite gmap2_Empty by congruence.
+ reflexivity.
+ }
+ destruct pmap_eq.
+ { subst b.
+ congruence.
+ }
+ destruct b as [ | b0 bf b1 ]; simpl; rewrite gnode.
+ - destruct i; simpl.
+ + rewrite IHa1. rewrite gempty.
+ reflexivity.
+ + rewrite IHa0. rewrite gempty.
+ reflexivity.
+ + reflexivity.
+ - destruct i; simpl; congruence.
+ Qed.
+ End IDEM.
+End MAP2.
diff --git a/make_docker.sh b/make_docker.sh
new file mode 100755
index 00000000..9665be4d
--- /dev/null
+++ b/make_docker.sh
@@ -0,0 +1,3 @@
+docker build -t compcert_build_env -f compcert_build_env.dockerfile .
+docker build -t compcert_kvx -f compcert_kvx.dockerfile .
+docker build -t compcert_kvx_pruned -f compcert_kvx_pruned.dockerfile .
diff --git a/powerpc/Archi.v b/powerpc/Archi.v
index 28859051..5b0af3b6 100644
--- a/powerpc/Archi.v
+++ b/powerpc/Archi.v
@@ -71,3 +71,5 @@ Global Opaque ptr64 big_endian splitlong
default_nan_32 choose_nan_32
fma_order fma_invalid_mul_is_nan
float_of_single_preserves_sNaN.
+
+Definition has_notrap_loads := false.
diff --git a/powerpc/AsmToJSON.ml b/powerpc/AsmToJSON.ml
index 09cfc28d..1f32dd62 100644
--- a/powerpc/AsmToJSON.ml
+++ b/powerpc/AsmToJSON.ml
@@ -362,6 +362,7 @@ let pp_instructions pp ic =
| EF_annot_val _
| EF_builtin _
| EF_debug _
+ | EF_profiling _
| EF_external _
| EF_free
| EF_malloc
diff --git a/powerpc/Asmgen.v b/powerpc/Asmgen.v
index 7b6ac9af..924773e7 100644
--- a/powerpc/Asmgen.v
+++ b/powerpc/Asmgen.v
@@ -831,8 +831,13 @@ Definition transl_memory_access
Error(msg "Asmgen.transl_memory_access")
end.
-Definition transl_load (chunk: memory_chunk) (addr: addressing)
- (args: list mreg) (dst: mreg) (k: code) :=
+Definition transl_load
+ (trap : trapping_mode)
+ (chunk: memory_chunk) (addr: addressing)
+ (args: list mreg) (dst: mreg) (k: code) :=
+ match trap with
+ | NOTRAP => Error (msg "Asmgen.transl_load non-trapping loads unsupported on PPC")
+ | TRAP =>
match chunk with
| Mint8signed =>
do r <- ireg_of dst;
@@ -860,6 +865,7 @@ Definition transl_load (chunk: memory_chunk) (addr: addressing)
transl_memory_access (Plfd r) (Plfdx r) true addr args GPR12 k
| _ =>
Error (msg "Asmgen.transl_load")
+ end
end.
Definition transl_store (chunk: memory_chunk) (addr: addressing)
@@ -917,8 +923,8 @@ Definition transl_instr (f: Mach.function) (i: Mach.instruction)
loadind GPR1 f.(fn_link_ofs) Tint R11 k1)
| Mop op args res =>
transl_op op args res k
- | Mload chunk addr args dst =>
- transl_load chunk addr args dst k
+ | Mload trap chunk addr args dst =>
+ transl_load trap chunk addr args dst k
| Mstore chunk addr args src =>
transl_store chunk addr args src k
| Mcall sig (inl r) =>
diff --git a/powerpc/Asmgenproof.v b/powerpc/Asmgenproof.v
index 85541118..e25ae583 100644
--- a/powerpc/Asmgenproof.v
+++ b/powerpc/Asmgenproof.v
@@ -339,6 +339,7 @@ Proof.
eapply loadind_label; eauto.
eapply tail_nolabel_trans; eapply loadind_label; eauto.
eapply transl_op_label; eauto.
+ destruct t; try discriminate.
destruct m; monadInv H; (eapply tail_nolabel_trans; [eapply transl_memory_access_label; TailNoLabel|TailNoLabel]).
destruct m; monadInv H; eapply transl_memory_access_label; TailNoLabel.
destruct s0; monadInv H; TailNoLabel.
@@ -668,6 +669,13 @@ Opaque loadind.
split. simpl; congruence.
apply R; auto with asmgen.
+
+- (* Mload notrap *) (* isn't there a nicer way? *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
+- (* Mload notrap *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
- (* Mstore *)
assert (eval_addressing tge sp addr rs##args = Some a).
rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
diff --git a/powerpc/Asmgenproof1.v b/powerpc/Asmgenproof1.v
index 6ae520ef..cc9fc1a7 100644
--- a/powerpc/Asmgenproof1.v
+++ b/powerpc/Asmgenproof1.v
@@ -1738,8 +1738,8 @@ Qed.
(** Translation of loads *)
Lemma transl_load_correct:
- forall chunk addr args dst k c (rs: regset) m a v,
- transl_load chunk addr args dst k = OK c ->
+ forall trap chunk addr args dst k c (rs: regset) m a v,
+ transl_load trap chunk addr args dst k = OK c ->
eval_addressing ge (rs#GPR1) addr (map rs (map preg_of args)) = Some a ->
Mem.loadv chunk m a = Some v ->
exists rs',
@@ -1748,6 +1748,7 @@ Lemma transl_load_correct:
/\ forall r, r <> PC -> r <> GPR12 -> r <> GPR0 -> r <> preg_of dst -> rs' r = rs r.
Proof.
intros.
+ destruct trap; try discriminate.
assert (LD: forall v, Val.lessdef a v -> v = a).
{ intros. inv H2; auto. discriminate H1. }
assert (BASE: forall mk1 mk2 unaligned k' chunk' v',
diff --git a/powerpc/BTL_SEsimplify.v b/powerpc/BTL_SEsimplify.v
new file mode 120000
index 00000000..f190e6d5
--- /dev/null
+++ b/powerpc/BTL_SEsimplify.v
@@ -0,0 +1 @@
+../aarch64/BTL_SEsimplify.v \ No newline at end of file
diff --git a/powerpc/CSE2deps.v b/powerpc/CSE2deps.v
new file mode 100644
index 00000000..4592f408
--- /dev/null
+++ b/powerpc/CSE2deps.v
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs (Int.unsigned ofs') chunk' (Int.unsigned ofs) chunk)
+ else true
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/powerpc/CSE2depsproof.v b/powerpc/CSE2depsproof.v
new file mode 100644
index 00000000..ede09dd6
--- /dev/null
+++ b/powerpc/CSE2depsproof.v
@@ -0,0 +1,214 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_size :
+ Ptrofs.wordsize = if Archi.ptr64 then 64%nat else 32%nat.
+Proof.
+ unfold Ptrofs.wordsize.
+ unfold Wordsize_Ptrofs.wordsize.
+ trivial.
+Qed.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = if Archi.ptr64 then 18446744073709551616 else 4294967296.
+Proof.
+ unfold Ptrofs.modulus.
+ rewrite ptrofs_size.
+ destruct Archi.ptr64; reflexivity.
+Qed.
+
+Lemma ptrofs_max_unsigned :
+ Ptrofs.max_unsigned = if Archi.ptr64 then 18446744073709551615 else 4294967295.
+Proof.
+ unfold Ptrofs.max_unsigned.
+ rewrite ptrofs_modulus.
+ destruct Archi.ptr64; reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : int.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= Int.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Int.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Int.unsigned ofsw + size_chunk chunkw <= Int.unsigned ofsr
+ \/ Int.unsigned ofsr + size_chunk chunkr <= Int.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+ destruct base; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.of_int ofsr)) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i (Ptrofs.of_int ofsw)) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: unfold Ptrofs.of_int.
+
+ all: repeat rewrite Ptrofs.unsigned_repr by (unfold Ptrofs.max_unsigned; rewrite ptrofs_modulus; destruct Archi.ptr64; lia).
+ all: repeat rewrite ptrofs_modulus.
+ all: destruct Archi.ptr64; intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs (Int.unsigned ofsr) chunkr (Int.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End MEMORY_WRITE.
+
+Section STACK_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Ainstack ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Ainstack ofsr) nil = Some addrr.
+
+ Lemma stack_load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ inv ADDRR.
+ inv ADDRW.
+
+ destruct sp; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: try rewrite ptrofs_modulus in *.
+ all: destruct Archi.ptr64.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem stack_load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply stack_load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End STACK_WRITE.
+End SOUNDNESS.
+
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+- (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ simpl in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs (Int.unsigned i0) chunk' (Int.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ simpl in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+- (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply stack_load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/powerpc/DuplicateOpcodeHeuristic.ml b/powerpc/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..c48fdfba
--- /dev/null
+++ b/powerpc/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* open Camlcoq *)
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccomplimm (c, n) | Ccompluimm (c, n) -> if n == Integers.Int64.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
diff --git a/powerpc/ExpansionOracle.ml b/powerpc/ExpansionOracle.ml
new file mode 120000
index 00000000..ee2674bf
--- /dev/null
+++ b/powerpc/ExpansionOracle.ml
@@ -0,0 +1 @@
+../aarch64/ExpansionOracle.ml \ No newline at end of file
diff --git a/powerpc/Machregsaux.ml b/powerpc/Machregsaux.ml
index 9d3a2243..d17382ad 100644
--- a/powerpc/Machregsaux.ml
+++ b/powerpc/Machregsaux.ml
@@ -13,3 +13,8 @@
(** Auxiliary functions on machine registers *)
let is_scratch_register s = s = "R0" || s = "r0"
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong -> 0
+ | AST.Tfloat | AST.Tsingle -> 1
+ | AST.Tany32 | AST.Tany64 -> assert false
diff --git a/powerpc/Machregsaux.mli b/powerpc/Machregsaux.mli
index f3d52849..01b0f9fd 100644
--- a/powerpc/Machregsaux.mli
+++ b/powerpc/Machregsaux.mli
@@ -13,3 +13,5 @@
(** Auxiliary functions on machine registers *)
val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
diff --git a/powerpc/Op.v b/powerpc/Op.v
index a96a0439..505b7545 100644
--- a/powerpc/Op.v
+++ b/powerpc/Op.v
@@ -569,6 +569,35 @@ Proof with (try exact I; try reflexivity).
unfold Val.select. destruct (eval_condition c vl m). apply Val.normalize_type. exact I.
Qed.
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Odiv | Odivl | Odivu | Odivlu
+ | Oshrximm _ | Oshrxlimm _
+ | Ointoffloat
+ | Olongoffloat
+ | Ofloatoflong => true
+ | _ => false
+ end.
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op; destruct eq_operation; intros; simpl in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+Qed.
End SOUNDNESS.
(** * Manipulating and transforming operations *)
@@ -719,7 +748,7 @@ Definition is_trivial_op (op: operation) : bool :=
(** Operations that depend on the memory state. *)
-Definition condition_depends_on_memory (c: condition) : bool :=
+Definition cond_depends_on_memory (c: condition) : bool :=
match c with
| Ccompu _ => true
| Ccompuimm _ _ => true
@@ -730,14 +759,14 @@ Definition condition_depends_on_memory (c: condition) : bool :=
Definition op_depends_on_memory (op: operation) : bool :=
match op with
- | Ocmp c => condition_depends_on_memory c
- | Osel c ty => condition_depends_on_memory c
+ | Ocmp c => cond_depends_on_memory c
+ | Osel c ty => cond_depends_on_memory c
| _ => false
end.
-Lemma condition_depends_on_memory_correct:
+Lemma cond_depends_on_memory_correct:
forall c args m1 m2,
- condition_depends_on_memory c = false ->
+ cond_depends_on_memory c = false ->
eval_condition c args m1 = eval_condition c args m2.
Proof.
intros. destruct c; simpl; auto; discriminate.
@@ -749,12 +778,36 @@ Lemma op_depends_on_memory_correct:
eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
Proof.
intros until m2. destruct op; simpl; try congruence; intros C.
-- f_equal; f_equal; apply condition_depends_on_memory_correct; auto.
+- f_equal; f_equal; apply cond_depends_on_memory_correct; auto.
- destruct args; auto. destruct args; auto.
- rewrite (condition_depends_on_memory_correct c args m1 m2 C).
+ rewrite (cond_depends_on_memory_correct c args m1 m2 C).
auto.
Qed.
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op eqn:OP; simpl; try congruence.
+ - intros MEM; destruct c; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intro MEM; destruct c; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
(** Global variables mentioned in an operation or addressing mode *)
Definition globals_operation (op: operation) : list ident :=
@@ -1016,6 +1069,21 @@ Proof.
apply Val.add_inject; auto. apply H; simpl; auto.
Qed.
+
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; simpl in *;
+ inv Hinjvl; trivial; try discriminate; inv H0; trivial; try discriminate; inv H2; trivial; try discriminate.
+Qed.
End EVAL_COMPAT.
(** Compatibility of the evaluation functions with the ``is less defined'' relation over values. *)
@@ -1082,6 +1150,20 @@ Proof.
rewrite <- val_inject_list_lessdef. eauto. auto.
Qed.
+
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros until vl2. intros Hlessdef Heval1.
+ destruct addr; simpl in *;
+ inv Hlessdef; trivial; try discriminate;
+ inv H0; trivial; try discriminate;
+ inv H2; trivial; try discriminate.
+Qed.
+
Lemma eval_operation_lessdef:
forall sp op vl1 vl2 v1 m1 m2,
Val.lessdef_list vl1 vl2 ->
@@ -1173,6 +1255,19 @@ Proof.
econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
Qed.
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
Lemma eval_operation_inject:
forall op vl1 vl2 v1 m1 m2,
Val.inject_list f vl1 vl2 ->
diff --git a/powerpc/PrepassSchedulingOracle.ml b/powerpc/PrepassSchedulingOracle.ml
new file mode 120000
index 00000000..9885fd52
--- /dev/null
+++ b/powerpc/PrepassSchedulingOracle.ml
@@ -0,0 +1 @@
+../x86/PrepassSchedulingOracle.ml \ No newline at end of file
diff --git a/powerpc/SelectLong.vp b/powerpc/SelectLong.vp
index 5f13774b..e4274ba5 100644
--- a/powerpc/SelectLong.vp
+++ b/powerpc/SelectLong.vp
@@ -16,7 +16,7 @@ Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats.
Require Import Op CminorSel.
-Require Import SelectOp SplitLong.
+Require Import OpHelpers SelectOp SplitLong.
Local Open Scope cminorsel_scope.
Local Open Scope string_scope.
diff --git a/powerpc/SelectLongproof.v b/powerpc/SelectLongproof.v
index ea14668f..2264451d 100644
--- a/powerpc/SelectLongproof.v
+++ b/powerpc/SelectLongproof.v
@@ -16,6 +16,7 @@ Require Import String Coqlib Maps Zbits Integers Floats Errors.
Require Archi.
Require Import AST Values Memory Globalenvs Events.
Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectOpproof SplitLong SplitLongproof.
Require Import SelectLong.
diff --git a/powerpc/SelectOp.vp b/powerpc/SelectOp.vp
index cd9a65df..fe8b5453 100644
--- a/powerpc/SelectOp.vp
+++ b/powerpc/SelectOp.vp
@@ -39,7 +39,7 @@
Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats Builtins.
-Require Import Op CminorSel.
+Require Import Op OpHelpers CminorSel.
Require Archi.
Local Open Scope cminorsel_scope.
@@ -472,7 +472,7 @@ Definition intuoffloat (e: expr) :=
else
Elet e
(Elet (Eop (Ofloatconst (Float.of_intu Float.ox8000_0000)) Enil)
- (Econdition (CEcond (Ccompf Clt) (Eletvar 1 ::: Eletvar 0 ::: Enil))
+ (Econdition (CEcond (Ccompf Clt) None (Eletvar 1 ::: Eletvar 0 ::: Enil))
(intoffloat (Eletvar 1))
(addimm Float.ox8000_0000 (intoffloat (subf (Eletvar 1) (Eletvar 0))))))%nat.
@@ -566,6 +566,13 @@ Nondetfunction builtin_arg (e: expr) :=
| _ => BA e
end.
+(* floats *)
+Definition divf_base (e1: expr) (e2: expr) :=
+ Eop Odivf (e1 ::: e2 ::: Enil).
+
+Definition divfs_base (e1: expr) (e2: expr) :=
+ Eop Odivfs (e1 ::: e2 ::: Enil).
+
(** Platform-specific known builtins *)
Definition platform_builtin (b: platform_builtin) (args: exprlist) : option expr :=
diff --git a/powerpc/SelectOpproof.v b/powerpc/SelectOpproof.v
index adac6c34..edc935d4 100644
--- a/powerpc/SelectOpproof.v
+++ b/powerpc/SelectOpproof.v
@@ -18,6 +18,8 @@ Require Import Values Memory Builtins Globalenvs.
Require Import Cminor Op CminorSel.
Require Import Compopts.
Require Import SelectOp.
+Require Import OpHelpers.
+Require Import OpHelpersproof.
Local Open Scope cminorsel_scope.
Local Transparent Archi.ptr64.
@@ -70,8 +72,10 @@ Ltac TrivialExists :=
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -1064,6 +1068,27 @@ Proof.
- constructor; auto.
Qed.
+(* floating-point division without HELPERS *)
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ TrivialExists.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ TrivialExists.
+Qed.
+
(** Platform-specific known builtins *)
Theorem eval_platform_builtin:
diff --git a/powerpc/TargetPrinter.ml b/powerpc/TargetPrinter.ml
index 52d30e33..a82fa5d9 100644
--- a/powerpc/TargetPrinter.ml
+++ b/powerpc/TargetPrinter.ml
@@ -117,7 +117,9 @@ module Linux_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) ->
variable_section ~sec:".data" ~bss:".section .bss" i
| Section_small_data i ->
variable_section
@@ -212,7 +214,9 @@ module Diab_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data (i, false) ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_small_data i ->
variable_section ~sec:".sdata" ~bss:".sbss" ~common:false i
diff --git a/riscV/Archi.v b/riscV/Archi.v
index 9e561ca8..96f34276 100644
--- a/riscV/Archi.v
+++ b/riscV/Archi.v
@@ -72,3 +72,5 @@ Global Opaque ptr64 big_endian splitlong
(** Whether to generate position-independent code or not *)
Parameter pic_code: unit -> bool.
+
+Definition has_notrap_loads := false.
diff --git a/riscV/Asm.v b/riscV/Asm.v
index a47573a2..c80c6cc2 100644
--- a/riscV/Asm.v
+++ b/riscV/Asm.v
@@ -30,6 +30,7 @@ Require Import Smallstep.
Require Import Locations.
Require Stacklayout.
Require Import Conventions.
+Require ExtValues.
(** * Abstract syntax *)
@@ -62,10 +63,10 @@ Inductive freg: Type :=
| F24: freg | F25: freg | F26: freg | F27: freg
| F28: freg | F29: freg | F30: freg | F31: freg.
-Lemma ireg_eq: forall (x y: ireg), {x=y} + {x<>y}.
+Definition ireg_eq: forall (x y: ireg), {x=y} + {x<>y}.
Proof. decide equality. Defined.
-Lemma ireg0_eq: forall (x y: ireg0), {x=y} + {x<>y}.
+Definition ireg0_eq: forall (x y: ireg0), {x=y} + {x<>y}.
Proof. decide equality. apply ireg_eq. Defined.
Lemma freg_eq: forall (x y: freg), {x=y} + {x<>y}.
@@ -255,10 +256,10 @@ Inductive instruction : Type :=
(* floating point register move *)
| Pfmv (rd: freg) (rs: freg) (**r move *)
- | Pfmvxs (rd: ireg) (rs: freg) (**r move FP single to integer register *)
- | Pfmvsx (rd: freg) (rs: ireg) (**r move integer register to FP single *)
- | Pfmvxd (rd: ireg) (rs: freg) (**r move FP double to integer register *)
- | Pfmvdx (rd: freg) (rs: ireg) (**r move integer register to FP double *)
+ | Pfmvxs (rd: ireg) (rs: freg) (**r bitwise move FP single to integer register *)
+ | Pfmvxd (rd: ireg) (rs: freg) (**r bitwise move FP double to integer register *)
+ | Pfmvsx (rd: freg) (rs: ireg) (**r bitwise move integer register to FP single *)
+ | Pfmvdx (rd: freg) (rs: ireg) (**r bitwise move integer register to FP double*)
(* 32-bit (single-precision) floating point *)
| Pfls (rd: freg) (ra: ireg) (ofs: offset) (**r load float *)
@@ -347,6 +348,7 @@ Inductive instruction : Type :=
| Pbtbl (r: ireg) (tbl: list label) (**r N-way branch through a jump table *)
| Pbuiltin: external_function -> list (builtin_arg preg)
-> builtin_res preg -> instruction (**r built-in function (pseudo) *)
+ | Pselectl (rd: ireg) (rb: ireg0) (rt: ireg0) (rf: ireg0)
| Pnop : instruction. (**r nop instruction *)
@@ -920,6 +922,17 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
Next (nextinstr (rs#d <- (Val.floatofsingle rs#s))) m
| Pfcvtsd d s =>
Next (nextinstr (rs#d <- (Val.singleoffloat rs#s))) m
+
+ | Pfmvxs d s =>
+ Next (nextinstr (rs#d <- (ExtValues.bits_of_single rs#s))) m
+ | Pfmvxd d s =>
+ Next (nextinstr (rs#d <- (ExtValues.bits_of_float rs#s))) m
+
+ | Pfmvsx d s =>
+ Next (nextinstr (rs#d <- (ExtValues.single_of_bits rs#s))) m
+ | Pfmvdx d s =>
+ Next (nextinstr (rs#d <- (ExtValues.float_of_bits rs#s))) m
+
(** Pseudo-instructions *)
| Pallocframe sz pos =>
@@ -942,6 +955,10 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| _ => Stuck
end
end
+ | Pselectl rd rb rt rf =>
+ Next (nextinstr (rs#rd <- (ExtValues.select01_long
+ (rs###rb) (rs###rt) (rs###rf)))
+ #X31 <- Vundef) m
| Plabel lbl =>
Next (nextinstr rs) m
| Ploadsymbol rd s ofs =>
@@ -965,16 +982,12 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
end
| Pbuiltin ef args res =>
Stuck (**r treated specially below *)
+ | Pnop => Next (nextinstr rs) m (**r Pnop is used by an oracle during expansion *)
(** The following instructions and directives are not generated directly by Asmgen,
so we do not model them. *)
| Pfence
- | Pfmvxs _ _
- | Pfmvsx _ _
- | Pfmvxd _ _
- | Pfmvdx _ _
-
| Pfmins _ _ _
| Pfmaxs _ _ _
| Pfsqrts _ _
@@ -990,7 +1003,6 @@ Definition exec_instr (f: function) (i: instruction) (rs: regset) (m: mem) : out
| Pfmsubd _ _ _ _
| Pfnmaddd _ _ _ _
| Pfnmsubd _ _ _ _
- | Pnop
=> Stuck
end.
diff --git a/riscV/Asmexpand.ml b/riscV/Asmexpand.ml
index ab0e6fee..329dd34c 100644
--- a/riscV/Asmexpand.ml
+++ b/riscV/Asmexpand.ml
@@ -657,9 +657,49 @@ let expand_builtin_inline name args res =
raise (Error ("unrecognized builtin " ^ name))
(* Expansion of instructions *)
-
+
let expand_instruction instr =
match instr with
+ | Pselectl(rd, rb, rt, rf) ->
+ if not Archi.ptr64
+ then failwith "Pselectl not available on RV32, only on RV64"
+ else
+ if ireg0_eq rt rf then
+ begin
+ if ireg0_eq (X rd) rt then
+ begin
+ end
+ else
+ begin
+ emit (Paddl(rd, X0, rt))
+ end
+ end
+ else
+ if (ireg0_eq (X rd) rt) then
+ begin
+ emit (Psubl(X31, X0, rb));
+ emit (Pandl(X31, X X31, rt));
+ emit (Paddil(rd, rb, Int64.mone));
+ emit (Pandl(rd, X rd, rf));
+ emit (Porl(rd, X rd, X X31))
+ end
+ else
+ if (ireg0_eq (X rd) rf) then
+ begin
+ emit (Paddil(X31, rb, Int64.mone));
+ emit (Pandl(X31, X X31, rf));
+ emit (Psubl(rd, X0, rb));
+ emit (Pandl(rd, X rd, rt));
+ emit (Porl(rd, X rd, X X31))
+ end
+ else
+ begin
+ emit (Psubl(X31, X0, rb));
+ emit (Paddil(rd, rb, Int64.mone));
+ emit (Pandl(X31, X X31, rt));
+ emit (Pandl(rd, X rd, rf));
+ emit (Porl(rd, X rd, X X31))
+ end
| Pallocframe (sz, ofs) ->
let sg = get_current_function_sig() in
emit (Pmv (X30, X2));
diff --git a/riscV/Asmgen.v b/riscV/Asmgen.v
index a704ed74..3e84e950 100644
--- a/riscV/Asmgen.v
+++ b/riscV/Asmgen.v
@@ -25,6 +25,8 @@ Require Import Op Locations Mach Asm.
Local Open Scope string_scope.
Local Open Scope error_monad_scope.
+Definition time {A B: Type} (name: string) (f: A -> B) : A -> B := f.
+
(** The code generation functions take advantage of several
characteristics of the [Mach] code generated by earlier passes of the
compiler, mostly that argument and result registers are of the correct
@@ -201,8 +203,23 @@ Definition transl_cond_single (cmp: comparison) (rd: ireg) (fs1 fs2: freg) :=
| Cle => (Pfles rd fs1 fs2, true)
| Cgt => (Pflts rd fs2 fs1, true)
| Cge => (Pfles rd fs2 fs1, true)
- end.
-
+ end.
+
+(** Functions to select a special register according to the op "oreg" argument from RTL *)
+
+Definition apply_bin_oreg_ireg0 (optR: option oreg) (r1 r2: ireg0): (ireg0 * ireg0) :=
+ match optR with
+ | None => (r1, r2)
+ | Some X0_L => (X0, r1)
+ | Some X0_R => (r1, X0)
+ end.
+
+Definition get_oreg (optR: option oreg) (r: ireg0) :=
+ match optR with
+ | Some X0_L | Some X0_R => X0
+ | _ => r
+ end.
+
Definition transl_cbranch
(cond: condition) (args: list mreg) (lbl: label) (k: code) :=
match cond, args with
@@ -257,7 +274,72 @@ Definition transl_cbranch
| Cnotcompfs c, f1 :: f2 :: nil =>
do r1 <- freg_of f1; do r2 <- freg_of f2;
let (insn, normal) := transl_cond_single c X31 r1 r2 in
- OK (insn :: (if normal then Pbeqw X31 X0 lbl else Pbnew X31 X0 lbl) :: k)
+ OK (insn :: (if normal then Pbeqw X31 X0 lbl else Pbnew X31 X0 lbl) :: k)
+
+ | CEbeqw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbeqw r1' r2' lbl :: k)
+ | CEbnew optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbnew r1' r2' lbl :: k)
+ | CEbequw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbeqw r1' r2' lbl :: k)
+ | CEbneuw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbnew r1' r2' lbl :: k)
+ | CEbltw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbltw r1' r2' lbl :: k)
+ | CEbltuw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbltuw r1' r2' lbl :: k)
+ | CEbgew optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbgew r1' r2' lbl :: k)
+ | CEbgeuw optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbgeuw r1' r2' lbl :: k)
+ | CEbeql optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbeql r1' r2' lbl :: k)
+ | CEbnel optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbnel r1' r2' lbl :: k)
+ | CEbequl optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbeql r1' r2' lbl :: k)
+ | CEbneul optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbnel r1' r2' lbl :: k)
+ | CEbltl optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbltl r1' r2' lbl :: k)
+ | CEbltul optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbltul r1' r2' lbl :: k)
+ | CEbgel optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbgel r1' r2' lbl :: k)
+ | CEbgeul optR, a1 :: a2 :: nil =>
+ do r1 <- ireg_of a1; do r2 <- ireg_of a2;
+ let (r1', r2') := apply_bin_oreg_ireg0 optR r1 r2 in
+ OK (Pbgeul r1' r2' lbl :: k)
| _, _ =>
Error(msg "Asmgen.transl_cond_branch")
end.
@@ -340,7 +422,7 @@ Definition transl_condimm_int64u (cmp: comparison) (rd: ireg) (r1: ireg) (n: int
match cmp with
| Clt => sltuimm64 rd r1 n k
| _ => loadimm64 X31 n (transl_cond_int64u cmp rd r1 X31 k)
- end.
+ end.
Definition transl_cond_op
(cond: condition) (rd: ireg) (args: list mreg) (k: code) :=
@@ -362,13 +444,13 @@ Definition transl_cond_op
OK (transl_cond_int64s c rd r1 r2 k)
| Ccomplu c, a1 :: a2 :: nil =>
do r1 <- ireg_of a1; do r2 <- ireg_of a2;
- OK (transl_cond_int64u c rd r1 r2 k)
+ OK (transl_cond_int64u c rd r1 r2 k)
| Ccomplimm c n, a1 :: nil =>
do r1 <- ireg_of a1;
OK (transl_condimm_int64s c rd r1 n k)
| Ccompluimm c n, a1 :: nil =>
do r1 <- ireg_of a1;
- OK (transl_condimm_int64u c rd r1 n k)
+ OK (transl_condimm_int64u c rd r1 n k)
| Ccompf c, f1 :: f2 :: nil =>
do r1 <- freg_of f1; do r2 <- freg_of f2;
let (insn, normal) := transl_cond_float c rd r1 r2 in
@@ -384,14 +466,14 @@ Definition transl_cond_op
| Cnotcompfs c, f1 :: f2 :: nil =>
do r1 <- freg_of f1; do r2 <- freg_of f2;
let (insn, normal) := transl_cond_single c rd r1 r2 in
- OK (insn :: if normal then Pxoriw rd rd Int.one :: k else k)
+ OK (insn :: if normal then Pxoriw rd rd Int.one :: k else k)
| _, _ =>
Error(msg "Asmgen.transl_cond_op")
- end.
+ end.
(** Translation of the arithmetic operation [r <- op(args)].
The corresponding instructions are prepended to [k]. *)
-
+
Definition transl_op
(op: operation) (args: list mreg) (res: mreg) (k: code) :=
match op, args with
@@ -503,11 +585,16 @@ Definition transl_op
OK (Psrliw rd rs n :: k)
| Oshrximm n, a1 :: nil =>
do rd <- ireg_of res; do rs <- ireg_of a1;
- OK (if Int.eq n Int.zero then Pmv rd rs :: k else
- Psraiw X31 rs (Int.repr 31) ::
- Psrliw X31 X31 (Int.sub Int.iwordsize n) ::
- Paddw X31 rs X31 ::
- Psraiw rd X31 n :: k)
+ OK (if Int.eq n Int.zero
+ then Pmv rd rs :: k
+ else if Int.eq n Int.one
+ then Psrliw X31 rs (Int.repr 31) ::
+ Paddw X31 rs X31 ::
+ Psraiw rd X31 Int.one :: k
+ else Psraiw X31 rs (Int.repr 31) ::
+ Psrliw X31 X31 (Int.sub Int.iwordsize n) ::
+ Paddw X31 rs X31 ::
+ Psraiw rd X31 n :: k)
(* [Omakelong], [Ohighlong] should not occur *)
| Olowlong, a1 :: nil =>
@@ -592,11 +679,16 @@ Definition transl_op
OK (Psrlil rd rs n :: k)
| Oshrxlimm n, a1 :: nil =>
do rd <- ireg_of res; do rs <- ireg_of a1;
- OK (if Int.eq n Int.zero then Pmv rd rs :: k else
- Psrail X31 rs (Int.repr 63) ::
- Psrlil X31 X31 (Int.sub Int64.iwordsize' n) ::
- Paddl X31 rs X31 ::
- Psrail rd X31 n :: k)
+ OK (if Int.eq n Int.zero
+ then Pmv rd rs :: k
+ else if Int.eq n Int.one
+ then Psrlil X31 rs (Int.repr 63) ::
+ Paddl X31 rs X31 ::
+ Psrail rd X31 Int.one :: k
+ else Psrail X31 rs (Int.repr 63) ::
+ Psrlil X31 X31 (Int.sub Int64.iwordsize' n) ::
+ Paddl X31 rs X31 ::
+ Psrail rd X31 n :: k)
| Onegf, a1 :: nil =>
do rd <- freg_of res; do rs <- freg_of a1;
@@ -692,11 +784,206 @@ Definition transl_op
| Osingleoflongu, a1 :: nil =>
do rd <- freg_of res; do rs <- ireg_of a1;
OK (Pfcvtslu rd rs :: k)
-
| Ocmp cmp, _ =>
do rd <- ireg_of res;
- transl_cond_op cmp rd args k
+ transl_cond_op cmp rd args k
+ (* Instructions expanded in RTL *)
+ | OEseqw optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Pseqw rd rs1' rs2' :: k)
+ | OEsnew optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psnew rd rs1' rs2' :: k)
+ | OEsequw optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Pseqw rd rs1' rs2' :: k)
+ | OEsneuw optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psnew rd rs1' rs2' :: k)
+ | OEsltw optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psltw rd rs1' rs2' :: k)
+ | OEsltuw optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psltuw rd rs1' rs2' :: k)
+ | OEsltiw n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Psltiw rd rs n :: k)
+ | OEsltiuw n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Psltiuw rd rs n :: k)
+ | OExoriw n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Pxoriw rd rs n :: k)
+ | OEluiw n, nil =>
+ do rd <- ireg_of res;
+ OK (Pluiw rd n :: k)
+ | OEaddiw optR n, nil =>
+ do rd <- ireg_of res;
+ let rs := get_oreg optR X0 in
+ OK (Paddiw rd rs n :: k)
+ | OEaddiw optR n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ let rs' := get_oreg optR rs in
+ OK (Paddiw rd rs' n :: k)
+ | OEandiw n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Pandiw rd rs n :: k)
+ | OEoriw n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Poriw rd rs n :: k)
+ | OEseql optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Pseql rd rs1' rs2' :: k)
+ | OEsnel optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psnel rd rs1' rs2' :: k)
+ | OEsequl optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Pseql rd rs1' rs2' :: k)
+ | OEsneul optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psnel rd rs1' rs2' :: k)
+ | OEsltl optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psltl rd rs1' rs2' :: k)
+ | OEsltul optR, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do rs1 <- ireg_of a1;
+ do rs2 <- ireg_of a2;
+ let (rs1', rs2') := apply_bin_oreg_ireg0 optR rs1 rs2 in
+ OK (Psltul rd rs1' rs2' :: k)
+ | OEsltil n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Psltil rd rs n :: k)
+ | OEsltiul n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Psltiul rd rs n :: k)
+ | OExoril n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Pxoril rd rs n :: k)
+ | OEluil n, nil =>
+ do rd <- ireg_of res;
+ OK (Pluil rd n :: k)
+ | OEaddil optR n, nil =>
+ do rd <- ireg_of res;
+ let rs := get_oreg optR X0 in
+ OK (Paddil rd rs n :: k)
+ | OEaddil optR n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ let rs' := get_oreg optR rs in
+ OK (Paddil rd rs' n :: k)
+ | OEandil n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Pandil rd rs n :: k)
+ | OEoril n, a1 :: nil =>
+ do rd <- ireg_of res;
+ do rs <- ireg_of a1;
+ OK (Poril rd rs n :: k)
+ | OEloadli n, nil =>
+ do rd <- ireg_of res;
+ OK (Ploadli rd n :: k)
+ | OEfeqd, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pfeqd rd r1 r2 :: k)
+ | OEfltd, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pfltd rd r1 r2 :: k)
+ | OEfled, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pfled rd r1 r2 :: k)
+ | OEfeqs, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pfeqs rd r1 r2 :: k)
+ | OEflts, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pflts rd r1 r2 :: k)
+ | OEfles, f1 :: f2 :: nil =>
+ do rd <- ireg_of res;
+ do r1 <- freg_of f1;
+ do r2 <- freg_of f2;
+ OK (Pfles rd r1 r2 :: k)
+ | OEmayundef _, a1 :: a2 :: nil =>
+ do rd <- ireg_of res;
+ do r2 <- ireg_of a2;
+ if ireg_eq rd r2 then
+ OK (Pnop :: k)
+ else
+ OK (Pmv rd r2 :: k)
+
+ | Obits_of_single, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfmvxs rd rs :: k)
+ | Obits_of_float, a1 :: nil =>
+ do rd <- ireg_of res; do rs <- freg_of a1;
+ OK (Pfmvxd rd rs :: k)
+ | Osingle_of_bits, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfmvsx rd rs :: k)
+ | Ofloat_of_bits, a1 :: nil =>
+ do rd <- freg_of res; do rs <- ireg_of a1;
+ OK (Pfmvdx rd rs :: k)
+ | Oselectl, b::t::f::nil =>
+ do rd <- ireg_of res;
+ do rb <- ireg_of b;
+ do rt <- ireg_of t;
+ do rf <- ireg_of f;
+ OK (Pselectl rd rb rt rf :: k)
| _, _ =>
Error(msg "Asmgen.transl_op")
end.
@@ -770,9 +1057,13 @@ Definition transl_memory_access
Error(msg "Asmgen.transl_memory_access")
end.
-Definition transl_load (chunk: memory_chunk) (addr: addressing)
+Definition transl_load (trap : trapping_mode)
+ (chunk: memory_chunk) (addr: addressing)
(args: list mreg) (dst: mreg) (k: code) :=
- match chunk with
+ match trap with
+ | NOTRAP => Error (msg "Asmgen.transl_load non-trapping loads unsupported on Arm")
+ | TRAP =>
+ match chunk with
| Mint8signed =>
do r <- ireg_of dst;
transl_memory_access (Plb r) addr args k
@@ -799,6 +1090,7 @@ Definition transl_load (chunk: memory_chunk) (addr: addressing)
transl_memory_access (Pfld r) addr args k
| _ =>
Error (msg "Asmgen.transl_load")
+ end
end.
Definition transl_store (chunk: memory_chunk) (addr: addressing)
@@ -848,8 +1140,8 @@ Definition transl_instr (f: Mach.function) (i: Mach.instruction)
else loadind_ptr SP f.(fn_link_ofs) X30 c)
| Mop op args res =>
transl_op op args res k
- | Mload chunk addr args dst =>
- transl_load chunk addr args dst k
+ | Mload trap chunk addr args dst =>
+ transl_load trap chunk addr args dst k
| Mstore chunk addr args src =>
transl_store chunk addr args src k
| Mcall sig (inl r) =>
diff --git a/riscV/Asmgenproof.v b/riscV/Asmgenproof.v
index 798dad9f..e59c4535 100644
--- a/riscV/Asmgenproof.v
+++ b/riscV/Asmgenproof.v
@@ -173,7 +173,7 @@ Remark transl_cond_single_nolabel:
transl_cond_single c r1 r2 r3 = (insn, normal) -> nolabel insn.
Proof.
unfold transl_cond_single; intros. destruct c; inv H; exact I.
-Qed.
+ Qed.
Remark transl_cbranch_label:
forall cond args lbl k c,
@@ -211,7 +211,23 @@ Proof.
destruct normal; TailNoLabel.
- destruct (transl_cond_single c0 X31 x x0) as [insn normal] eqn:F; inv EQ2.
apply tail_nolabel_cons. eapply transl_cond_single_nolabel; eauto.
- destruct normal; TailNoLabel.
+ destruct normal; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
Qed.
Remark transl_cond_op_label:
@@ -238,7 +254,7 @@ Proof.
try (eapply tail_nolabel_trans; [apply loadimm32_label | TailNoLabel]).
apply opimm32_label; intros; exact I.
- destruct c0; simpl; TailNoLabel.
-- destruct c0; simpl; TailNoLabel.
+ - destruct c0; simpl; TailNoLabel.
- unfold transl_condimm_int64s.
destruct (Int64.eq n Int64.zero).
+ destruct c0; simpl; TailNoLabel.
@@ -254,7 +270,7 @@ Proof.
+ destruct c0; simpl; TailNoLabel.
+ destruct c0; simpl;
try (eapply tail_nolabel_trans; [apply loadimm64_label | TailNoLabel]).
- apply opimm64_label; intros; exact I.
+ apply opimm64_label; intros; exact I.
- destruct (transl_cond_float c0 r x x0) as [insn normal] eqn:F; inv EQ2.
apply tail_nolabel_cons. eapply transl_cond_float_nolabel; eauto.
destruct normal; TailNoLabel.
@@ -267,7 +283,7 @@ Proof.
- destruct (transl_cond_single c0 r x x0) as [insn normal] eqn:F; inv EQ2.
apply tail_nolabel_cons. eapply transl_cond_single_nolabel; eauto.
destruct normal; TailNoLabel.
-Qed.
+ Qed.
Remark transl_op_label:
forall op args r k c,
@@ -285,13 +301,25 @@ Opaque Int.eq.
- apply opimm32_label; intros; exact I.
- apply opimm32_label; intros; exact I.
- apply opimm32_label; intros; exact I.
-- destruct (Int.eq n Int.zero); TailNoLabel.
+- destruct (Int.eq n Int.zero); try destruct (Int.eq n Int.one); TailNoLabel.
- apply opimm64_label; intros; exact I.
- apply opimm64_label; intros; exact I.
- apply opimm64_label; intros; exact I.
- apply opimm64_label; intros; exact I.
-- destruct (Int.eq n Int.zero); TailNoLabel.
+- destruct (Int.eq n Int.zero); try destruct (Int.eq n Int.one); TailNoLabel.
- eapply transl_cond_op_label; eauto.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
+- destruct optR as [[]|]; simpl in *; TailNoLabel.
Qed.
Remark indexed_memory_access_label:
@@ -359,7 +387,7 @@ Proof.
- destruct ep. eapply loadind_label; eauto.
eapply tail_nolabel_trans. apply loadind_ptr_label. eapply loadind_label; eauto.
- eapply transl_op_label; eauto.
-- destruct m; monadInv H; eapply transl_memory_access_label; eauto; intros; exact I.
+- destruct t; (try discriminate); destruct m; monadInv H; eapply transl_memory_access_label; eauto; intros; exact I.
- destruct m; monadInv H; eapply transl_memory_access_label; eauto; intros; exact I.
- destruct s0; monadInv H; TailNoLabel.
- destruct s0; monadInv H; (eapply tail_nolabel_trans; [eapply make_epilogue_label|TailNoLabel]).
@@ -725,6 +753,12 @@ Local Transparent destroyed_by_op.
intros; auto with asmgen.
simpl; congruence.
+- (* Mload notrap *) (* isn't there a nicer way? *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
+- (* Mload notrap *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
- (* Mstore *)
assert (eval_addressing tge sp addr (map rs args) = Some a).
rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
diff --git a/riscV/Asmgenproof1.v b/riscV/Asmgenproof1.v
index af53754e..89a48aee 100644
--- a/riscV/Asmgenproof1.v
+++ b/riscV/Asmgenproof1.v
@@ -375,16 +375,16 @@ Proof.
rewrite <- Float32.cmp_swap. auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpfs, Val.cmpfs_bool.
rewrite <- Float32.cmp_swap. auto.
-Qed.
+ Qed.
-Remark branch_on_X31:
+(* TODO gourdinl UNUSUED ? Remark branch_on_X31:
forall normal lbl (rs: regset) m b,
rs#X31 = Val.of_bool (eqb normal b) ->
exec_instr ge fn (if normal then Pbnew X31 X0 lbl else Pbeqw X31 X0 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct normal; simpl; rewrite H; simpl; destruct b; reflexivity.
-Qed.
+ Qed.*)
Ltac ArgsInv :=
repeat (match goal with
@@ -417,7 +417,7 @@ Proof.
{ apply eval_condition_lessdef with (map ms args) m; auto. eapply preg_vals; eauto. }
clear EVAL MEXT AG.
destruct cond; simpl in TRANSL; ArgsInv.
-- exists rs, (transl_cbranch_int32s c0 x x0 lbl).
+ - exists rs, (transl_cbranch_int32s c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int32s_correct; auto.
- exists rs, (transl_cbranch_int32u c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int32u_correct; auto.
@@ -492,7 +492,144 @@ Proof.
econstructor; econstructor.
split. constructor. apply exec_straight_one. eapply transl_cond_single_correct with (v := v); eauto. auto.
split. rewrite V; destruct normal, b; reflexivity.
- intros; Simpl.
+ intros; Simpl.
+
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *;
+ destruct (rs x) eqn:EQRS; simpl in *; try congruence;
+ inv EQ2; eexists; eexists; eauto; split; constructor; auto;
+ simpl in *.
+ + rewrite EQRS;
+ assert (HB: (Int.eq Int.zero i) = b) by congruence.
+ rewrite HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ assert (HB: (Int.eq i Int.zero) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ destruct (rs x0); try congruence.
+ assert (HB: (Int.eq i i0) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *;
+ destruct (rs x) eqn:EQRS; simpl in *; try congruence;
+ inv EQ2; eexists; eexists; eauto; split; constructor; auto;
+ simpl in *.
+ + rewrite EQRS;
+ assert (HB: negb (Int.eq Int.zero i) = b) by congruence.
+ rewrite HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ assert (HB: negb (Int.eq i Int.zero) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ destruct (rs x0); try congruence.
+ assert (HB: negb (Int.eq i i0) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ destruct (rs x) eqn:EQRS; simpl in *; try congruence;
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; auto.
+ + rewrite EQRS;
+ assert (HB: (Int64.eq Int64.zero i) = b) by congruence.
+ rewrite HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ assert (HB: (Int64.eq i Int64.zero) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ destruct (rs x0); try congruence.
+ assert (HB: (Int64.eq i i0) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32 in *; inv EQ2;
+ destruct (rs x) eqn:EQRS; simpl in *; try congruence;
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; auto.
+ + rewrite EQRS;
+ assert (HB: negb (Int64.eq Int64.zero i) = b) by congruence.
+ rewrite HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ assert (HB: negb (Int64.eq i Int64.zero) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+ + rewrite EQRS;
+ destruct (rs x0); try congruence.
+ assert (HB: negb (Int64.eq i i0) = b) by congruence.
+ rewrite <- HB; destruct b; simpl; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
+- destruct optR as [[]|];
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *;
+ unfold zero32, Op.zero32, zero64, Op.zero64 in *; inv EQ2;
+ try (destruct (rs x); simpl in EVAL'; discriminate; fail);
+ eexists; eexists; eauto; split; constructor;
+ simpl in *; try rewrite EVAL'; auto.
Qed.
Lemma transl_cbranch_correct_true:
@@ -830,7 +967,7 @@ Proof.
+ apply DFL.
+ apply DFL.
+ apply DFL.
-Qed.
+ Qed.
Lemma transl_cond_op_correct:
forall cond rd args k c rs m,
@@ -858,7 +995,7 @@ Proof.
exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ (* cmplu *)
exploit transl_cond_int64u_correct; eauto. intros (rs' & A & B & C).
- exists rs'; repeat split; eauto. rewrite B, MKTOT; eauto.
+ exists rs'; repeat split; eauto. rewrite B, MKTOT; eauto.
+ (* cmplimm *)
exploit transl_condimm_int64s_correct; eauto. instantiate (1 := x); eauto with asmgen.
intros (rs' & A & B & C).
@@ -866,7 +1003,7 @@ Proof.
+ (* cmpluimm *)
exploit transl_condimm_int64u_correct; eauto. instantiate (1 := x); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ (* cmpf *)
destruct (transl_cond_float c0 rd x x0) as [insn normal] eqn:TR.
fold (Val.cmpf c0 (rs x) (rs x0)).
@@ -923,7 +1060,7 @@ Proof.
* econstructor; split.
apply exec_straight_one. eapply transl_cond_single_correct with (v := Val.notbool v); eauto. auto.
split; intros; Simpl.
-Qed.
+ Qed.
(** Some arithmetic properties. *)
@@ -964,126 +1101,226 @@ Proof.
Opaque Int.eq.
intros until c; intros TR EV.
unfold transl_op in TR; destruct op; ArgsInv; simpl in EV; SimplEval EV; try TranslOpSimpl.
-- (* move *)
- destruct (preg_of res), (preg_of m0); inv TR; TranslOpSimpl.
-- (* intconst *)
- exploit loadimm32_correct; eauto. intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* longconst *)
- exploit loadimm64_correct; eauto. intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* floatconst *)
- destruct (Float.eq_dec n Float.zero).
-+ subst n. econstructor; split.
- apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-+ econstructor; split.
- apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-- (* singleconst *)
- destruct (Float32.eq_dec n Float32.zero).
-+ subst n. econstructor; split.
- apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-+ econstructor; split.
- apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-- (* addrsymbol *)
- destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
-+ set (rs1 := nextinstr (rs#x <- (Genv.symbol_address ge id Ptrofs.zero))).
- exploit (addptrofs_correct x x ofs k rs1 m); eauto with asmgen.
- intros (rs2 & A & B & C).
- exists rs2; split.
- apply exec_straight_step with rs1 m; auto.
- split. replace ofs with (Ptrofs.add Ptrofs.zero ofs) by (apply Ptrofs.add_zero_l).
- rewrite Genv.shift_symbol_address.
- replace (rs1 x) with (Genv.symbol_address ge id Ptrofs.zero) in B by (unfold rs1; Simpl).
- exact B.
- intros. rewrite C by eauto with asmgen. unfold rs1; Simpl.
-+ TranslOpSimpl.
-- (* stackoffset *)
- exploit addptrofs_correct. instantiate (1 := X2); auto with asmgen. intros (rs' & A & B & C).
- exists rs'; split; eauto. auto with asmgen.
-- (* cast8signed *)
- econstructor; split.
+ (* move *)
+ { destruct (preg_of res), (preg_of m0); inv TR; TranslOpSimpl. }
+ (* intconst *)
+ { exploit loadimm32_correct; eauto. intros (rs' & A & B & C).
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* longconst *)
+ { exploit loadimm64_correct; eauto. intros (rs' & A & B & C).
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* floatconst *)
+ { destruct (Float.eq_dec n Float.zero).
+ + subst n. econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl. }
+ (* singleconst *)
+ { destruct (Float32.eq_dec n Float32.zero).
+ + subst n. econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + econstructor; split.
+ apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl. }
+ (* addrsymbol *)
+ { destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
+ + set (rs1 := nextinstr (rs#x <- (Genv.symbol_address ge id Ptrofs.zero))).
+ exploit (addptrofs_correct x x ofs k rs1 m); eauto with asmgen.
+ intros (rs2 & A & B & C).
+ exists rs2; split.
+ apply exec_straight_step with rs1 m; auto.
+ split. replace ofs with (Ptrofs.add Ptrofs.zero ofs) by (apply Ptrofs.add_zero_l).
+ rewrite Genv.shift_symbol_address.
+ replace (rs1 x) with (Genv.symbol_address ge id Ptrofs.zero) in B by (unfold rs1; Simpl).
+ exact B.
+ intros. rewrite C by eauto with asmgen. unfold rs1; Simpl.
+ + TranslOpSimpl. }
+ (* stackoffset *)
+ { exploit addptrofs_correct. instantiate (1 := X2); auto with asmgen. intros (rs' & A & B & C).
+ exists rs'; split; eauto. auto with asmgen. }
+ (* cast8signed *)
+ { econstructor; split.
eapply exec_straight_two. simpl;eauto. simpl;eauto. auto. auto.
split; intros; Simpl.
assert (A: Int.ltu (Int.repr 24) Int.iwordsize = true) by auto.
destruct (rs x0); auto; simpl. rewrite A; simpl. rewrite A.
- apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence.
-- (* cast16signed *)
- econstructor; split.
+ apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence. }
+ (* cast16signed *)
+ { econstructor; split.
eapply exec_straight_two. simpl;eauto. simpl;eauto. auto. auto.
split; intros; Simpl.
assert (A: Int.ltu (Int.repr 16) Int.iwordsize = true) by auto.
destruct (rs x0); auto; simpl. rewrite A; simpl. rewrite A.
- apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence.
-- (* addimm *)
- exploit (opimm32_correct Paddw Paddiw Val.add); auto. instantiate (1 := x0); eauto with asmgen.
+ apply Val.lessdef_same. f_equal. apply Int.sign_ext_shr_shl. compute; intuition congruence. }
+ (* addimm *)
+ { exploit (opimm32_correct Paddw Paddiw Val.add); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* andimm *)
- exploit (opimm32_correct Pandw Pandiw Val.and); auto. instantiate (1 := x0); eauto with asmgen.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* andimm *)
+ { exploit (opimm32_correct Pandw Pandiw Val.and); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* orimm *)
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* orimm *)
exploit (opimm32_correct Porw Poriw Val.or); auto. instantiate (1 := x0); eauto with asmgen.
+ { intros (rs' & A & B & C).
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* xorimm *)
+ { exploit (opimm32_correct Pxorw Pxoriw Val.xor); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* xorimm *)
- exploit (opimm32_correct Pxorw Pxoriw Val.xor); auto. instantiate (1 := x0); eauto with asmgen.
- intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* shrximm *)
- clear H. exploit Val.shrx_shr_2; eauto. intros E; subst v; clear EV.
- destruct (Int.eq n Int.zero).
-+ econstructor; split. apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-+ change (Int.repr 32) with Int.iwordsize. set (n' := Int.sub Int.iwordsize n).
- econstructor; split.
- eapply exec_straight_step. simpl; reflexivity. auto.
- eapply exec_straight_step. simpl; reflexivity. auto.
- eapply exec_straight_step. simpl; reflexivity. auto.
- apply exec_straight_one. simpl; reflexivity. auto.
- split; intros; Simpl.
-- (* longofintu *)
- econstructor; split.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* shrximm *)
+ { destruct (Val.shrx (rs x0) (Vint n)) eqn:TOTAL; cbn.
+ {
+ exploit Val.shrx_shr_3; eauto. intros E; subst v.
+ destruct (Int.eq n Int.zero).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + destruct (Int.eq n Int.one).
+ * econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+ * change (Int.repr 32) with Int.iwordsize. set (n' := Int.sub Int.iwordsize n).
+ econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+ }
+ destruct (Int.eq n Int.zero).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + destruct (Int.eq n Int.one).
+ * econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+ * change (Int.repr 32) with Int.iwordsize. set (n' := Int.sub Int.iwordsize n).
+ econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl. }
+ (* longofintu *)
+ { econstructor; split.
eapply exec_straight_three. simpl; eauto. simpl; eauto. simpl; eauto. auto. auto. auto.
split; intros; Simpl. destruct (rs x0); auto. simpl.
assert (A: Int.ltu (Int.repr 32) Int64.iwordsize' = true) by auto.
rewrite A; simpl. rewrite A. apply Val.lessdef_same. f_equal.
- rewrite cast32unsigned_from_cast32signed. apply Int64.zero_ext_shru_shl. compute; auto.
-- (* addlimm *)
- exploit (opimm64_correct Paddl Paddil Val.addl); auto. instantiate (1 := x0); eauto with asmgen.
+ rewrite cast32unsigned_from_cast32signed. apply Int64.zero_ext_shru_shl. compute; auto. }
+ (* addlimm *)
+ { exploit (opimm64_correct Paddl Paddil Val.addl); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* andimm *)
- exploit (opimm64_correct Pandl Pandil Val.andl); auto. instantiate (1 := x0); eauto with asmgen.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* andimm *)
+ { exploit (opimm64_correct Pandl Pandil Val.andl); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* orimm *)
- exploit (opimm64_correct Porl Poril Val.orl); auto. instantiate (1 := x0); eauto with asmgen.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* orimm *)
+ { exploit (opimm64_correct Porl Poril Val.orl); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* xorimm *)
- exploit (opimm64_correct Pxorl Pxoril Val.xorl); auto. instantiate (1 := x0); eauto with asmgen.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* xorimm *)
+ { exploit (opimm64_correct Pxorl Pxoril Val.xorl); auto. instantiate (1 := x0); eauto with asmgen.
intros (rs' & A & B & C).
- exists rs'; split; eauto. rewrite B; auto with asmgen.
-- (* shrxlimm *)
- clear H. exploit Val.shrxl_shrl_2; eauto. intros E; subst v; clear EV.
- destruct (Int.eq n Int.zero).
-+ econstructor; split. apply exec_straight_one. simpl; eauto. auto.
- split; intros; Simpl.
-+ change (Int.repr 64) with Int64.iwordsize'. set (n' := Int.sub Int64.iwordsize' n).
- econstructor; split.
- eapply exec_straight_step. simpl; reflexivity. auto.
- eapply exec_straight_step. simpl; reflexivity. auto.
- eapply exec_straight_step. simpl; reflexivity. auto.
- apply exec_straight_one. simpl; reflexivity. auto.
+ exists rs'; split; eauto. rewrite B; auto with asmgen. }
+ (* shrxlimm *)
+ { destruct (Val.shrxl (rs x0) (Vint n)) eqn:TOTAL.
+ {
+ exploit Val.shrxl_shrl_3; eauto. intros E; subst v.
+ destruct (Int.eq n Int.zero).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + destruct (Int.eq n Int.one).
+ * econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+
+ * change (Int.repr 64) with Int64.iwordsize'. set (n' := Int.sub Int64.iwordsize' n).
+ econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+ }
+ destruct (Int.eq n Int.zero).
+ + econstructor; split. apply exec_straight_one. simpl; eauto. auto.
+ split; intros; Simpl.
+ + destruct (Int.eq n Int.one).
+ * econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl.
+
+ * change (Int.repr 64) with Int64.iwordsize'. set (n' := Int.sub Int64.iwordsize' n).
+ econstructor; split.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ eapply exec_straight_step. simpl; reflexivity. auto.
+ apply exec_straight_one. simpl; reflexivity. auto.
+ split; intros; Simpl. }
+ (* cond *)
+ { exploit transl_cond_op_correct; eauto. intros (rs' & A & B & C).
+ exists rs'; split. eexact A. eauto with asmgen. }
+ (* Expanded instructions from RTL *)
+ 9,10,19,20:
+ econstructor; split; try apply exec_straight_one; simpl; eauto;
+ split; intros; Simpl; try destruct (rs x0);
+ try rewrite Int64.add_commut;
+ try rewrite Int.add_commut; auto;
+ try rewrite Int64.and_commut;
+ try rewrite Int.and_commut; auto;
+ try rewrite Int64.or_commut;
+ try rewrite Int.or_commut; auto.
+ 1-16:
+ destruct optR as [[]|]; try discriminate;
+ unfold apply_bin_oreg_ireg0, apply_bin_oreg in *; try inv EQ3; try inv EQ2;
+ try destruct (Int.eq _ _) eqn:A; try inv H0;
+ try destruct (Int64.eq _ _) eqn:A; try inv H1;
+ econstructor; split; try apply exec_straight_one; simpl; eauto;
+ split; intros; Simpl;
+ try apply Int.same_if_eq in A; subst;
+ try apply Int64.same_if_eq in A; subst;
+ unfold get_sp;
+ try destruct (rs x0); auto;
+ try destruct (rs x1); auto;
+ try destruct (rs X2); auto;
+ try destruct Archi.ptr64 eqn:B;
+ try fold (Val.add (Vint Int.zero) (get_sp (rs X2)));
+ try fold (Val.addl (Vlong Int64.zero) (get_sp (rs X2)));
+ try rewrite Val.add_commut; auto;
+ try rewrite Val.addl_commut; auto;
+ try rewrite Int.add_commut; auto;
+ try rewrite Int64.add_commut; auto;
+ replace (Ptrofs.of_int Int.zero) with (Ptrofs.zero) by auto;
+ replace (Ptrofs.of_int64 Int64.zero) with (Ptrofs.zero) by auto;
+ try rewrite Ptrofs.add_zero; auto.
+ (* mayundef *)
+ { destruct (ireg_eq x x0); inv EQ2;
+ econstructor; split;
+ try apply exec_straight_one; simpl; eauto;
+ split; unfold eval_may_undef;
+ destruct mu eqn:EQMU; simpl; intros; Simpl; auto.
+ all:
+ destruct (rs (preg_of m0)) eqn:EQM0; simpl; auto;
+ destruct (rs x0); simpl; auto; Simpl;
+ try destruct (Int.ltu _ _); simpl;
+ Simpl; auto. }
+ (* select *)
+ { econstructor; split. apply exec_straight_one. simpl; eauto. auto.
split; intros; Simpl.
-- (* cond *)
- exploit transl_cond_op_correct; eauto. intros (rs' & A & B & C).
- exists rs'; split. eexact A. eauto with asmgen.
+ apply Val.lessdef_normalize. }
Qed.
(** Memory accesses *)
@@ -1302,8 +1539,8 @@ Proof.
Qed.
Lemma transl_load_correct:
- forall chunk addr args dst k c (rs: regset) m a v,
- transl_load chunk addr args dst k = OK c ->
+ forall trap chunk addr args dst k c (rs: regset) m a v,
+ transl_load trap chunk addr args dst k = OK c ->
eval_addressing ge rs#SP addr (map rs (map preg_of args)) = Some a ->
Mem.loadv chunk m a = Some v ->
exists rs',
@@ -1311,7 +1548,8 @@ Lemma transl_load_correct:
/\ rs'#(preg_of dst) = v
/\ forall r, r <> PC -> r <> X31 -> r <> preg_of dst -> rs'#r = rs#r.
Proof.
- intros until v; intros TR EV LOAD.
+ intros until v; intros TR EV LOAD.
+ destruct trap; try (simpl in *; discriminate).
assert (A: exists mk_instr,
transl_memory_access mk_instr addr args k = OK c
/\ forall base ofs rs,
@@ -1390,6 +1628,3 @@ Proof.
Qed.
End CONSTRUCTORS.
-
-
-
diff --git a/riscV/BTL_SEsimplify.v b/riscV/BTL_SEsimplify.v
new file mode 100644
index 00000000..ab01f7ac
--- /dev/null
+++ b/riscV/BTL_SEsimplify.v
@@ -0,0 +1,1923 @@
+Require Import Coqlib Floats Values Memory.
+Require Import Integers.
+Require Import Op Registers.
+Require Import BTL_SEtheory.
+Require Import BTL_SEsimuref.
+Require Import Asmgen Asmgenproof1.
+
+(** Useful functions for conditions/branches expansion *)
+
+Definition is_inv_cmp_int (cmp: comparison) : bool :=
+ match cmp with | Cle | Cgt => true | _ => false end.
+
+Definition is_inv_cmp_float (cmp: comparison) : bool :=
+ match cmp with | Cge | Cgt => true | _ => false end.
+
+Definition make_optR (is_x0 is_inv: bool) : option oreg :=
+ if is_x0 then
+ (if is_inv then Some (X0_L)
+ else Some (X0_R))
+ else None.
+
+(** Functions to manage lists of "fake" values *)
+
+Definition make_lfsv_cmp (is_inv: bool) (fsv1 fsv2: sval) : list_sval :=
+ let (fsvfirst, fsvsec) := if is_inv then (fsv1, fsv2) else (fsv2, fsv1) in
+ let lfsv := fScons fsvfirst fSnil in
+ fScons fsvsec lfsv.
+
+Definition make_lfsv_single (fsv: sval) : list_sval :=
+ fScons fsv fSnil.
+
+(** * Expansion functions *)
+
+(** ** Immediate loads *)
+
+Definition load_hilo32 (hi lo: int) :=
+ if Int.eq lo Int.zero then
+ fSop (OEluiw hi) fSnil
+ else
+ let fsv := fSop (OEluiw hi) fSnil in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OEaddiw None lo) lfsv.
+
+Definition load_hilo64 (hi lo: int64) :=
+ if Int64.eq lo Int64.zero then
+ fSop (OEluil hi) fSnil
+ else
+ let fsv := fSop (OEluil hi) fSnil in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OEaddil None lo) lfsv.
+
+Definition loadimm32 (n: int) :=
+ match make_immed32 n with
+ | Imm32_single imm =>
+ fSop (OEaddiw (Some X0_R) imm) fSnil
+ | Imm32_pair hi lo => load_hilo32 hi lo
+ end.
+
+Definition loadimm64 (n: int64) :=
+ match make_immed64 n with
+ | Imm64_single imm =>
+ fSop (OEaddil (Some X0_R) imm) fSnil
+ | Imm64_pair hi lo => load_hilo64 hi lo
+ | Imm64_large imm => fSop (OEloadli imm) fSnil
+ end.
+
+Definition opimm32 (fsv1: sval) (n: int) (op: operation) (opimm: int -> operation) :=
+ match make_immed32 n with
+ | Imm32_single imm =>
+ let lfsv := make_lfsv_single fsv1 in
+ fSop (opimm imm) lfsv
+ | Imm32_pair hi lo =>
+ let fsv := load_hilo32 hi lo in
+ let lfsv := make_lfsv_cmp false fsv1 fsv in
+ fSop op lfsv
+ end.
+
+Definition opimm64 (fsv1: sval) (n: int64) (op: operation) (opimm: int64 -> operation) :=
+ match make_immed64 n with
+ | Imm64_single imm =>
+ let lfsv := make_lfsv_single fsv1 in
+ fSop (opimm imm) lfsv
+ | Imm64_pair hi lo =>
+ let fsv := load_hilo64 hi lo in
+ let lfsv := make_lfsv_cmp false fsv1 fsv in
+ fSop op lfsv
+ | Imm64_large imm =>
+ let fsv := fSop (OEloadli imm) fSnil in
+ let lfsv := make_lfsv_cmp false fsv1 fsv in
+ fSop op lfsv
+ end.
+
+Definition addimm32 (fsv1: sval) (n: int) (or: option oreg) := opimm32 fsv1 n Oadd (OEaddiw or).
+Definition andimm32 (fsv1: sval) (n: int) := opimm32 fsv1 n Oand OEandiw.
+Definition orimm32 (fsv1: sval) (n: int) := opimm32 fsv1 n Oor OEoriw.
+Definition xorimm32 (fsv1: sval) (n: int) := opimm32 fsv1 n Oxor OExoriw.
+Definition sltimm32 (fsv1: sval) (n: int) := opimm32 fsv1 n (OEsltw None) OEsltiw.
+Definition sltuimm32 (fsv1: sval) (n: int) := opimm32 fsv1 n (OEsltuw None) OEsltiuw.
+Definition addimm64 (fsv1: sval) (n: int64) (or: option oreg) := opimm64 fsv1 n Oaddl (OEaddil or).
+Definition andimm64 (fsv1: sval) (n: int64) := opimm64 fsv1 n Oandl OEandil.
+Definition orimm64 (fsv1: sval) (n: int64) := opimm64 fsv1 n Oorl OEoril.
+Definition xorimm64 (fsv1: sval) (n: int64) := opimm64 fsv1 n Oxorl OExoril.
+Definition sltimm64 (fsv1: sval) (n: int64) := opimm64 fsv1 n (OEsltl None) OEsltil.
+Definition sltuimm64 (fsv1: sval) (n: int64) := opimm64 fsv1 n (OEsltul None) OEsltiul.
+(** ** Comparisons intructions *)
+
+Definition cond_int32s (cmp: comparison) (lsv: list_sval) (optR: option oreg) :=
+ match cmp with
+ | Ceq => fSop (OEseqw optR) lsv
+ | Cne => fSop (OEsnew optR) lsv
+ | Clt | Cgt => fSop (OEsltw optR) lsv
+ | Cle | Cge =>
+ let fsv := (fSop (OEsltw optR) lsv) in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OExoriw Int.one) lfsv
+ end.
+
+Definition cond_int32u (cmp: comparison) (lsv: list_sval) (optR: option oreg) :=
+ match cmp with
+ | Ceq => fSop (OEsequw optR) lsv
+ | Cne => fSop (OEsneuw optR) lsv
+ | Clt | Cgt => fSop (OEsltuw optR) lsv
+ | Cle | Cge =>
+ let fsv := (fSop (OEsltuw optR) lsv) in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OExoriw Int.one) lfsv
+ end.
+
+Definition cond_int64s (cmp: comparison) (lsv: list_sval) (optR: option oreg) :=
+ match cmp with
+ | Ceq => fSop (OEseql optR) lsv
+ | Cne => fSop (OEsnel optR) lsv
+ | Clt | Cgt => fSop (OEsltl optR) lsv
+ | Cle | Cge =>
+ let fsv := (fSop (OEsltl optR) lsv) in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OExoriw Int.one) lfsv
+ end.
+
+Definition cond_int64u (cmp: comparison) (lsv: list_sval) (optR: option oreg) :=
+ match cmp with
+ | Ceq => fSop (OEsequl optR) lsv
+ | Cne => fSop (OEsneul optR) lsv
+ | Clt | Cgt => fSop (OEsltul optR) lsv
+ | Cle | Cge =>
+ let fsv := (fSop (OEsltul optR) lsv) in
+ let lfsv := make_lfsv_single fsv in
+ fSop (OExoriw Int.one) lfsv
+ end.
+
+Definition expanse_condimm_int32s (cmp: comparison) (fsv1: sval) (n: int) :=
+ let is_inv := is_inv_cmp_int cmp in
+ if Int.eq n Int.zero then
+ let optR := make_optR true is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ cond_int32s cmp lfsv optR
+ else
+ match cmp with
+ | Ceq | Cne =>
+ let optR := make_optR true is_inv in
+ let fsv := xorimm32 fsv1 n in
+ let lfsv := make_lfsv_cmp false fsv fsv in
+ cond_int32s cmp lfsv optR
+ | Clt => sltimm32 fsv1 n
+ | Cle =>
+ if Int.eq n (Int.repr Int.max_signed) then
+ let fsv := loadimm32 Int.one in
+ let lfsv := make_lfsv_cmp false fsv1 fsv in
+ fSop (OEmayundef MUint) lfsv
+ else sltimm32 fsv1 (Int.add n Int.one)
+ | _ =>
+ let optR := make_optR false is_inv in
+ let fsv := loadimm32 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ cond_int32s cmp lfsv optR
+ end.
+
+Definition expanse_condimm_int32u (cmp: comparison) (fsv1: sval) (n: int) :=
+ let is_inv := is_inv_cmp_int cmp in
+ if Int.eq n Int.zero then
+ let optR := make_optR true is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ cond_int32u cmp lfsv optR
+ else
+ match cmp with
+ | Clt => sltuimm32 fsv1 n
+ | _ =>
+ let optR := make_optR false is_inv in
+ let fsv := loadimm32 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ cond_int32u cmp lfsv optR
+ end.
+
+Definition expanse_condimm_int64s (cmp: comparison) (fsv1: sval) (n: int64) :=
+ let is_inv := is_inv_cmp_int cmp in
+ if Int64.eq n Int64.zero then
+ let optR := make_optR true is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ cond_int64s cmp lfsv optR
+ else
+ match cmp with
+ | Ceq | Cne =>
+ let optR := make_optR true is_inv in
+ let fsv := xorimm64 fsv1 n in
+ let lfsv := make_lfsv_cmp false fsv fsv in
+ cond_int64s cmp lfsv optR
+ | Clt => sltimm64 fsv1 n
+ | Cle =>
+ if Int64.eq n (Int64.repr Int64.max_signed) then
+ let fsv := loadimm32 Int.one in
+ let lfsv := make_lfsv_cmp false fsv1 fsv in
+ fSop (OEmayundef MUlong) lfsv
+ else sltimm64 fsv1 (Int64.add n Int64.one)
+ | _ =>
+ let optR := make_optR false is_inv in
+ let fsv := loadimm64 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ cond_int64s cmp lfsv optR
+ end.
+
+Definition expanse_condimm_int64u (cmp: comparison) (fsv1: sval) (n: int64) :=
+ let is_inv := is_inv_cmp_int cmp in
+ if Int64.eq n Int64.zero then
+ let optR := make_optR true is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ cond_int64u cmp lfsv optR
+ else
+ match cmp with
+ | Clt => sltuimm64 fsv1 n
+ | _ =>
+ let optR := make_optR false is_inv in
+ let fsv := loadimm64 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ cond_int64u cmp lfsv optR
+ end.
+
+Definition cond_float (cmp: comparison) (lsv: list_sval) :=
+ match cmp with
+ | Ceq | Cne => fSop OEfeqd lsv
+ | Clt | Cgt => fSop OEfltd lsv
+ | Cle | Cge => fSop OEfled lsv
+ end.
+
+Definition cond_single (cmp: comparison) (lsv: list_sval) :=
+ match cmp with
+ | Ceq | Cne => fSop OEfeqs lsv
+ | Clt | Cgt => fSop OEflts lsv
+ | Cle | Cge => fSop OEfles lsv
+ end.
+
+Definition is_normal_cmp cmp :=
+ match cmp with | Cne => false | _ => true end.
+
+Definition expanse_cond_fp (cnot: bool) fn_cond cmp (lsv: list_sval) :=
+ let normal := is_normal_cmp cmp in
+ let normal' := if cnot then negb normal else normal in
+ let fsv := fn_cond cmp lsv in
+ let lfsv := make_lfsv_single fsv in
+ if normal' then fsv else fSop (OExoriw Int.one) lfsv.
+
+(** ** Branches instructions *)
+
+Definition transl_cbranch_int32s (cmp: comparison) (optR: option oreg) :=
+ match cmp with
+ | Ceq => CEbeqw optR
+ | Cne => CEbnew optR
+ | Clt => CEbltw optR
+ | Cle => CEbgew optR
+ | Cgt => CEbltw optR
+ | Cge => CEbgew optR
+ end.
+
+Definition transl_cbranch_int32u (cmp: comparison) (optR: option oreg) :=
+ match cmp with
+ | Ceq => CEbequw optR
+ | Cne => CEbneuw optR
+ | Clt => CEbltuw optR
+ | Cle => CEbgeuw optR
+ | Cgt => CEbltuw optR
+ | Cge => CEbgeuw optR
+ end.
+
+Definition transl_cbranch_int64s (cmp: comparison) (optR: option oreg) :=
+ match cmp with
+ | Ceq => CEbeql optR
+ | Cne => CEbnel optR
+ | Clt => CEbltl optR
+ | Cle => CEbgel optR
+ | Cgt => CEbltl optR
+ | Cge => CEbgel optR
+ end.
+
+Definition transl_cbranch_int64u (cmp: comparison) (optR: option oreg) :=
+ match cmp with
+ | Ceq => CEbequl optR
+ | Cne => CEbneul optR
+ | Clt => CEbltul optR
+ | Cle => CEbgeul optR
+ | Cgt => CEbltul optR
+ | Cge => CEbgeul optR
+ end.
+
+Definition expanse_cbranch_fp (cnot: bool) fn_cond cmp (lfsv: list_sval) : (condition * list_sval) :=
+ let normal := is_normal_cmp cmp in
+ let normal' := if cnot then negb normal else normal in
+ let fsv := fn_cond cmp lfsv in
+ let lfsv' := make_lfsv_cmp false fsv fsv in
+ if normal' then ((CEbnew (Some X0_R)), lfsv') else ((CEbeqw (Some X0_R)), lfsv').
+
+(** Target op simplifications using "fake" values *)
+
+Definition target_op_simplify (op: operation) (lr: list reg) (hrs: ristate): option sval :=
+ match op, lr with
+ | Ocmp (Ccomp c), a1 :: a2 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let fsv2 := ris_sreg_get hrs a2 in
+ let is_inv := is_inv_cmp_int c in
+ let optR := make_optR false is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond_int32s c lfsv optR)
+ | Ocmp (Ccompu c), a1 :: a2 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let fsv2 := ris_sreg_get hrs a2 in
+ let is_inv := is_inv_cmp_int c in
+ let optR := make_optR false is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond_int32u c lfsv optR)
+ | Ocmp (Ccompimm c imm), a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (expanse_condimm_int32s c fsv1 imm)
+ | Ocmp (Ccompuimm c imm), a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (expanse_condimm_int32u c fsv1 imm)
+ | Ocmp (Ccompl c), a1 :: a2 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let fsv2 := ris_sreg_get hrs a2 in
+ let is_inv := is_inv_cmp_int c in
+ let optR := make_optR false is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond_int64s c lfsv optR)
+ | Ocmp (Ccomplu c), a1 :: a2 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let fsv2 := ris_sreg_get hrs a2 in
+ let is_inv := is_inv_cmp_int c in
+ let optR := make_optR false is_inv in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond_int64u c lfsv optR)
+ | Ocmp (Ccomplimm c imm), a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (expanse_condimm_int64s c fsv1 imm)
+ | Ocmp (Ccompluimm c imm), a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (expanse_condimm_int64u c fsv1 imm)
+ | Ocmp (Ccompf c), f1 :: f2 :: nil =>
+ let fsv1 := ris_sreg_get hrs f1 in
+ let fsv2 := ris_sreg_get hrs f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cond_fp false cond_float c lfsv)
+ | Ocmp (Cnotcompf c), f1 :: f2 :: nil =>
+ let fsv1 := ris_sreg_get hrs f1 in
+ let fsv2 := ris_sreg_get hrs f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cond_fp true cond_float c lfsv)
+ | Ocmp (Ccompfs c), f1 :: f2 :: nil =>
+ let fsv1 := ris_sreg_get hrs f1 in
+ let fsv2 := ris_sreg_get hrs f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cond_fp false cond_single c lfsv)
+ | Ocmp (Cnotcompfs c), f1 :: f2 :: nil =>
+ let fsv1 := ris_sreg_get hrs f1 in
+ let fsv2 := ris_sreg_get hrs f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cond_fp true cond_single c lfsv)
+ | Ofloatconst f, nil =>
+ let fsv := loadimm64 (Float.to_bits f) in
+ let lfsv := make_lfsv_single fsv in
+ Some (fSop (Ofloat_of_bits) lfsv)
+ | Osingleconst f, nil =>
+ let fsv := loadimm32 (Float32.to_bits f) in
+ let lfsv := make_lfsv_single fsv in
+ Some (fSop (Osingle_of_bits) lfsv)
+ | Ointconst n, nil =>
+ Some (loadimm32 n)
+ | Olongconst n, nil =>
+ Some (loadimm64 n)
+ | Oaddimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (addimm32 fsv1 n None)
+ | Oaddlimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (addimm64 fsv1 n None)
+ | Oandimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (andimm32 fsv1 n)
+ | Oandlimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (andimm64 fsv1 n)
+ | Oorimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (orimm32 fsv1 n)
+ | Oorlimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (orimm64 fsv1 n)
+ | Oxorimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (xorimm32 fsv1 n)
+ | Oxorlimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ Some (xorimm64 fsv1 n)
+ | Ocast8signed, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let lfsv := make_lfsv_single fsv1 in
+ let fsv := fSop (Oshlimm (Int.repr 24)) lfsv in
+ let hl' := make_lfsv_single fsv in
+ Some (fSop (Oshrimm (Int.repr 24)) hl')
+ | Ocast16signed, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let lfsv := make_lfsv_single fsv1 in
+ let fsv := fSop (Oshlimm (Int.repr 16)) lfsv in
+ let hl' := make_lfsv_single fsv in
+ Some (fSop (Oshrimm (Int.repr 16)) hl')
+ | Ocast32unsigned, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let lfsv := make_lfsv_single fsv1 in
+ let cast32s_s := fSop Ocast32signed lfsv in
+ let cast32s_l := make_lfsv_single cast32s_s in
+ let sllil_s := fSop (Oshllimm (Int.repr 32)) cast32s_l in
+ let sllil_l := make_lfsv_single sllil_s in
+ Some (fSop (Oshrluimm (Int.repr 32)) sllil_l)
+ | Oshrximm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let lfsv := make_lfsv_single fsv1 in
+ if Int.eq n Int.zero then
+ let lhl := make_lfsv_cmp false fsv1 fsv1 in
+ Some (fSop (OEmayundef (MUshrx n)) lhl)
+ else
+ if Int.eq n Int.one then
+ let srliw_s := fSop (Oshruimm (Int.repr 31)) lfsv in
+ let srliw_l := make_lfsv_cmp false fsv1 srliw_s in
+ let addw_s := fSop Oadd srliw_l in
+ let addw_l := make_lfsv_single addw_s in
+ let sraiw_s := fSop (Oshrimm Int.one) addw_l in
+ let sraiw_l := make_lfsv_cmp false sraiw_s sraiw_s in
+ Some (fSop (OEmayundef (MUshrx n)) sraiw_l)
+ else
+ let sraiw_s := fSop (Oshrimm (Int.repr 31)) lfsv in
+ let sraiw_l := make_lfsv_single sraiw_s in
+ let srliw_s := fSop (Oshruimm (Int.sub Int.iwordsize n)) sraiw_l in
+ let srliw_l := make_lfsv_cmp false fsv1 srliw_s in
+ let addw_s := fSop Oadd srliw_l in
+ let addw_l := make_lfsv_single addw_s in
+ let sraiw_s' := fSop (Oshrimm n) addw_l in
+ let sraiw_l' := make_lfsv_cmp false sraiw_s' sraiw_s' in
+ Some (fSop (OEmayundef (MUshrx n)) sraiw_l')
+ | Oshrxlimm n, a1 :: nil =>
+ let fsv1 := ris_sreg_get hrs a1 in
+ let lfsv := make_lfsv_single fsv1 in
+ if Int.eq n Int.zero then
+ let lhl := make_lfsv_cmp false fsv1 fsv1 in
+ Some (fSop (OEmayundef (MUshrxl n)) lhl)
+ else
+ if Int.eq n Int.one then
+ let srlil_s := fSop (Oshrluimm (Int.repr 63)) lfsv in
+ let srlil_l := make_lfsv_cmp false fsv1 srlil_s in
+ let addl_s := fSop Oaddl srlil_l in
+ let addl_l := make_lfsv_single addl_s in
+ let srail_s := fSop (Oshrlimm Int.one) addl_l in
+ let srail_l := make_lfsv_cmp false srail_s srail_s in
+ Some (fSop (OEmayundef (MUshrxl n)) srail_l)
+ else
+ let srail_s := fSop (Oshrlimm (Int.repr 63)) lfsv in
+ let srail_l := make_lfsv_single srail_s in
+ let srlil_s := fSop (Oshrluimm (Int.sub Int64.iwordsize' n)) srail_l in
+ let srlil_l := make_lfsv_cmp false fsv1 srlil_s in
+ let addl_s := fSop Oaddl srlil_l in
+ let addl_l := make_lfsv_single addl_s in
+ let srail_s' := fSop (Oshrlimm n) addl_l in
+ let srail_l' := make_lfsv_cmp false srail_s' srail_s' in
+ Some (fSop (OEmayundef (MUshrxl n)) srail_l')
+
+ | _, _ => None
+ end.
+
+Definition target_cbranch_expanse (prev: ristate) (cond: condition) (args: list reg) : option (condition * list_sval) :=
+ match cond, args with
+ | (Ccomp c), (a1 :: a2 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let cond := transl_cbranch_int32s c (make_optR false is_inv) in
+ let fsv1 := ris_sreg_get prev a1 in
+ let fsv2 := ris_sreg_get prev a2 in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond, lfsv)
+ | (Ccompu c), (a1 :: a2 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let cond := transl_cbranch_int32u c (make_optR false is_inv) in
+ let fsv1 := ris_sreg_get prev a1 in
+ let fsv2 := ris_sreg_get prev a2 in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond, lfsv)
+ | (Ccompimm c n), (a1 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let fsv1 := ris_sreg_get prev a1 in
+ (if Int.eq n Int.zero then
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ let cond := transl_cbranch_int32s c (make_optR true is_inv) in
+ Some (cond, lfsv)
+ else
+ let fsv := loadimm32 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ let cond := transl_cbranch_int32s c (make_optR false is_inv) in
+ Some (cond, lfsv))
+ | (Ccompuimm c n), (a1 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let fsv1 := ris_sreg_get prev a1 in
+ (if Int.eq n Int.zero then
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ let cond := transl_cbranch_int32u c (make_optR true is_inv) in
+ Some (cond, lfsv)
+ else
+ let fsv := loadimm32 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ let cond := transl_cbranch_int32u c (make_optR false is_inv) in
+ Some (cond, lfsv))
+ | (Ccompl c), (a1 :: a2 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let cond := transl_cbranch_int64s c (make_optR false is_inv) in
+ let fsv1 := ris_sreg_get prev a1 in
+ let fsv2 := ris_sreg_get prev a2 in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond, lfsv)
+ | (Ccomplu c), (a1 :: a2 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let cond := transl_cbranch_int64u c (make_optR false is_inv) in
+ let fsv1 := ris_sreg_get prev a1 in
+ let fsv2 := ris_sreg_get prev a2 in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (cond, lfsv)
+ | (Ccomplimm c n), (a1 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let fsv1 := ris_sreg_get prev a1 in
+ (if Int64.eq n Int64.zero then
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ let cond := transl_cbranch_int64s c (make_optR true is_inv) in
+ Some (cond, lfsv)
+ else
+ let fsv := loadimm64 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ let cond := transl_cbranch_int64s c (make_optR false is_inv) in
+ Some (cond, lfsv))
+ | (Ccompluimm c n), (a1 :: nil) =>
+ let is_inv := is_inv_cmp_int c in
+ let fsv1 := ris_sreg_get prev a1 in
+ (if Int64.eq n Int64.zero then
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv1 in
+ let cond := transl_cbranch_int64u c (make_optR true is_inv) in
+ Some (cond, lfsv)
+ else
+ let fsv := loadimm64 n in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv in
+ let cond := transl_cbranch_int64u c (make_optR false is_inv) in
+ Some (cond, lfsv))
+ | (Ccompf c), (f1 :: f2 :: nil) =>
+ let fsv1 := ris_sreg_get prev f1 in
+ let fsv2 := ris_sreg_get prev f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cbranch_fp false cond_float c lfsv)
+ | (Cnotcompf c), (f1 :: f2 :: nil) =>
+ let fsv1 := ris_sreg_get prev f1 in
+ let fsv2 := ris_sreg_get prev f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cbranch_fp true cond_float c lfsv)
+ | (Ccompfs c), (f1 :: f2 :: nil) =>
+ let fsv1 := ris_sreg_get prev f1 in
+ let fsv2 := ris_sreg_get prev f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cbranch_fp false cond_single c lfsv)
+ | (Cnotcompfs c), (f1 :: f2 :: nil) =>
+ let fsv1 := ris_sreg_get prev f1 in
+ let fsv2 := ris_sreg_get prev f2 in
+ let is_inv := is_inv_cmp_float c in
+ let lfsv := make_lfsv_cmp is_inv fsv1 fsv2 in
+ Some (expanse_cbranch_fp true cond_single c lfsv)
+ | _, _ => None
+ end.
+
+(** * Auxiliary lemmas on comparisons *)
+
+(** ** Signed ints *)
+
+Lemma xor_neg_ltle_cmp: forall v1 v2,
+ Some (Val.xor (Val.cmp Clt v1 v2) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmp_bool Cle v2 v1)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ unfold Val.cmp; simpl;
+ try rewrite Int.eq_sym;
+ try destruct (Int.eq _ _); try destruct (Int.lt _ _) eqn:ELT ; simpl;
+ try rewrite Int.xor_one_one; try rewrite Int.xor_zero_one;
+ auto.
+Qed.
+Local Hint Resolve xor_neg_ltle_cmp: core.
+
+(** ** Unsigned ints *)
+
+Lemma xor_neg_ltle_cmpu: forall mptr v1 v2,
+ Some (Val.xor (Val.cmpu (Mem.valid_pointer mptr) Clt v1 v2) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmpu_bool (Mem.valid_pointer mptr) Cle v2 v1)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ unfold Val.cmpu; simpl;
+ try rewrite Int.eq_sym;
+ try destruct (Int.eq _ _); try destruct (Int.ltu _ _) eqn:ELT ; simpl;
+ try rewrite Int.xor_one_one; try rewrite Int.xor_zero_one;
+ auto.
+ 1,2:
+ unfold Val.cmpu, Val.cmpu_bool;
+ destruct Archi.ptr64; try destruct (_ && _); try destruct (_ || _);
+ try destruct (eq_block _ _); auto.
+ unfold Val.cmpu, Val.cmpu_bool; simpl;
+ destruct Archi.ptr64; try destruct (_ || _); simpl; auto;
+ destruct (eq_block b b0); destruct (eq_block b0 b);
+ try congruence;
+ try destruct (_ || _); simpl; try destruct (Ptrofs.ltu _ _);
+ simpl; auto;
+ repeat destruct (_ && _); simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_ltle_cmpu: core.
+
+Remark ltu_12_wordsize:
+ Int.ltu (Int.repr 12) Int.iwordsize = true.
+Proof.
+ unfold Int.iwordsize, Int.zwordsize. simpl.
+ unfold Int.ltu. apply zlt_true.
+ rewrite !Int.unsigned_repr; try cbn; try lia.
+Qed.
+Local Hint Resolve ltu_12_wordsize: core.
+
+(** ** Signed longs *)
+
+Lemma xor_neg_ltle_cmpl: forall v1 v2,
+ Some (Val.xor (Val.maketotal (Val.cmpl Clt v1 v2)) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmpl_bool Cle v2 v1)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ destruct (Int64.lt _ _); auto.
+Qed.
+Local Hint Resolve xor_neg_ltle_cmpl: core.
+
+Lemma xor_neg_ltge_cmpl: forall v1 v2,
+ Some (Val.xor (Val.maketotal (Val.cmpl Clt v1 v2)) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmpl_bool Cge v1 v2)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ destruct (Int64.lt _ _); auto.
+Qed.
+Local Hint Resolve xor_neg_ltge_cmpl: core.
+
+Lemma xorl_zero_eq_cmpl: forall c v1 v2,
+ c = Ceq \/ c = Cne ->
+ Some
+ (Val.maketotal
+ (option_map Val.of_bool
+ (Val.cmpl_bool c (Val.xorl v1 v2) (Vlong Int64.zero)))) =
+ Some (Val.of_optbool (Val.cmpl_bool c v1 v2)).
+Proof.
+ intros. destruct c; inv H; try discriminate;
+ destruct v1, v2; simpl; auto;
+ destruct (Int64.eq i i0) eqn:EQ0.
+ 1,3:
+ apply Int64.same_if_eq in EQ0; subst;
+ rewrite Int64.xor_idem;
+ rewrite Int64.eq_true; trivial.
+ 1,2:
+ destruct (Int64.eq (Int64.xor i i0) Int64.zero) eqn:EQ1; simpl; try congruence;
+ rewrite Int64.xor_is_zero in EQ1; congruence.
+Qed.
+Local Hint Resolve xorl_zero_eq_cmpl: core.
+
+Lemma cmp_ltle_add_one: forall v n,
+ Int.eq n (Int.repr Int.max_signed) = false ->
+ Some (Val.of_optbool (Val.cmp_bool Clt v (Vint (Int.add n Int.one)))) =
+ Some (Val.of_optbool (Val.cmp_bool Cle v (Vint n))).
+Proof.
+ intros v n EQMAX. unfold Val.cmp_bool; destruct v; simpl; auto.
+ unfold Int.lt. replace (Int.signed (Int.add n Int.one)) with (Int.signed n + 1).
+ destruct (zlt (Int.signed n) (Int.signed i)).
+ rewrite zlt_false by lia. auto.
+ rewrite zlt_true by lia. auto.
+ rewrite Int.add_signed. symmetry; apply Int.signed_repr.
+ specialize (Int.eq_spec n (Int.repr Int.max_signed)).
+ rewrite EQMAX; simpl; intros.
+ assert (Int.signed n <> Int.max_signed).
+ { red; intros E. elim H. rewrite <- (Int.repr_signed n). rewrite E. auto. }
+ generalize (Int.signed_range n); lia.
+Qed.
+Local Hint Resolve cmp_ltle_add_one: core.
+
+Lemma cmpl_ltle_add_one: forall v n,
+ Int64.eq n (Int64.repr Int64.max_signed) = false ->
+ Some (Val.of_optbool (Val.cmpl_bool Clt v (Vlong (Int64.add n Int64.one)))) =
+ Some (Val.of_optbool (Val.cmpl_bool Cle v (Vlong n))).
+Proof.
+ intros v n EQMAX. unfold Val.cmpl_bool; destruct v; simpl; auto.
+ unfold Int64.lt. replace (Int64.signed (Int64.add n Int64.one)) with (Int64.signed n + 1).
+ destruct (zlt (Int64.signed n) (Int64.signed i)).
+ rewrite zlt_false by lia. auto.
+ rewrite zlt_true by lia. auto.
+ rewrite Int64.add_signed. symmetry; apply Int64.signed_repr.
+ specialize (Int64.eq_spec n (Int64.repr Int64.max_signed)).
+ rewrite EQMAX; simpl; intros.
+ assert (Int64.signed n <> Int64.max_signed).
+ { red; intros E. elim H. rewrite <- (Int64.repr_signed n). rewrite E. auto. }
+ generalize (Int64.signed_range n); lia.
+Qed.
+Local Hint Resolve cmpl_ltle_add_one: core.
+
+Remark lt_maxsgn_false_int: forall i,
+ Int.lt (Int.repr Int.max_signed) i = false.
+Proof.
+ intros; unfold Int.lt.
+ specialize Int.signed_range with i; intros.
+ rewrite zlt_false; auto. destruct H.
+ rewrite Int.signed_repr; try (cbn; lia).
+ apply Z.le_ge. trivial.
+Qed.
+Local Hint Resolve lt_maxsgn_false_int: core.
+
+Remark lt_maxsgn_false_long: forall i,
+ Int64.lt (Int64.repr Int64.max_signed) i = false.
+Proof.
+ intros; unfold Int64.lt.
+ specialize Int64.signed_range with i; intros.
+ rewrite zlt_false; auto. destruct H.
+ rewrite Int64.signed_repr; try (cbn; lia).
+ apply Z.le_ge. trivial.
+Qed.
+Local Hint Resolve lt_maxsgn_false_long: core.
+
+(** ** Unsigned longs *)
+
+Lemma xor_neg_ltle_cmplu: forall mptr v1 v2,
+ Some (Val.xor (Val.maketotal (Val.cmplu (Mem.valid_pointer mptr) Clt v1 v2)) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmplu_bool (Mem.valid_pointer mptr) Cle v2 v1)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ destruct (Int64.ltu _ _); auto.
+ 1,2: unfold Val.cmplu; simpl; auto;
+ destruct (Archi.ptr64); simpl;
+ try destruct (eq_block _ _); simpl;
+ try destruct (_ && _); simpl;
+ try destruct (Ptrofs.cmpu _ _);
+ try destruct cmp; simpl; auto.
+ unfold Val.cmplu; simpl;
+ destruct Archi.ptr64; try destruct (_ || _); simpl; auto;
+ destruct (eq_block b b0); destruct (eq_block b0 b);
+ try congruence;
+ try destruct (_ || _); simpl; try destruct (Ptrofs.ltu _ _);
+ simpl; auto;
+ repeat destruct (_ && _); simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_ltle_cmplu: core.
+
+Lemma xor_neg_ltge_cmplu: forall mptr v1 v2,
+ Some (Val.xor (Val.maketotal (Val.cmplu (Mem.valid_pointer mptr) Clt v1 v2)) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmplu_bool (Mem.valid_pointer mptr) Cge v1 v2)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence.
+ destruct (Int64.ltu _ _); auto.
+ 1,2: unfold Val.cmplu; simpl; auto;
+ destruct (Archi.ptr64); simpl;
+ try destruct (eq_block _ _); simpl;
+ try destruct (_ && _); simpl;
+ try destruct (Ptrofs.cmpu _ _);
+ try destruct cmp; simpl; auto.
+ unfold Val.cmplu; simpl;
+ destruct Archi.ptr64; try destruct (_ || _); simpl; auto;
+ destruct (eq_block b b0); destruct (eq_block b0 b);
+ try congruence;
+ try destruct (_ || _); simpl; try destruct (Ptrofs.ltu _ _);
+ simpl; auto;
+ repeat destruct (_ && _); simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_ltge_cmplu: core.
+
+(** ** Floats *)
+
+Lemma xor_neg_eqne_cmpf: forall v1 v2,
+ Some (Val.xor (Val.cmpf Ceq v1 v2) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmpf_bool Cne v1 v2)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence;
+ unfold Val.cmpf; simpl.
+ rewrite Float.cmp_ne_eq.
+ destruct (Float.cmp _ _ _); simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_eqne_cmpf: core.
+
+(** ** Singles *)
+
+Lemma xor_neg_eqne_cmpfs: forall v1 v2,
+ Some (Val.xor (Val.cmpfs Ceq v1 v2) (Vint Int.one)) =
+ Some (Val.of_optbool (Val.cmpfs_bool Cne v1 v2)).
+Proof.
+ intros. eapply f_equal.
+ destruct v1, v2; simpl; try congruence;
+ unfold Val.cmpfs; simpl.
+ rewrite Float32.cmp_ne_eq.
+ destruct (Float32.cmp _ _ _); simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_eqne_cmpfs: core.
+
+(** ** More useful lemmas *)
+
+Lemma xor_neg_optb: forall v,
+ Some (Val.xor (Val.of_optbool (option_map negb v))
+ (Vint Int.one)) = Some (Val.of_optbool v).
+Proof.
+ intros.
+ destruct v; simpl; trivial.
+ destruct b; simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_optb: core.
+
+Lemma xor_neg_optb': forall v,
+ Some (Val.xor (Val.of_optbool v) (Vint Int.one)) =
+ Some (Val.of_optbool (option_map negb v)).
+Proof.
+ intros.
+ destruct v; simpl; trivial.
+ destruct b; simpl; auto.
+Qed.
+Local Hint Resolve xor_neg_optb': core.
+
+Lemma optbool_mktotal: forall v,
+ Val.maketotal (option_map Val.of_bool v) =
+ Val.of_optbool v.
+Proof.
+ intros.
+ destruct v; simpl; auto.
+Qed.
+Local Hint Resolve optbool_mktotal: core.
+
+(** * Intermediates lemmas on each expanded instruction *)
+
+Lemma simplify_ccomp_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (cond_int32s c (make_lfsv_cmp (is_inv_cmp_int c) (hrs r) (hrs r0)) None) =
+ Some (Val.of_optbool (Val.cmp_bool c v v0)).
+Proof.
+ intros.
+ unfold cond_int32s in *; destruct c; simpl;
+ erewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmp. eauto.
+ - replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmp_bool; trivial.
+ - replace (Clt) with (negate_comparison Cge) by auto;
+ rewrite Val.negate_cmp_bool; eauto.
+Qed.
+
+Lemma simplify_ccompu_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (cond_int32u c (make_lfsv_cmp (is_inv_cmp_int c) (hrs r) (hrs r0)) None) =
+ Some (Val.of_optbool (Val.cmpu_bool (Mem.valid_pointer (cm0 ctx)) c v v0)).
+Proof.
+ intros.
+ unfold cond_int32u in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpu.
+ - replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmpu_bool; trivial.
+ - replace (Clt) with (negate_comparison Cge) by auto;
+ rewrite Val.negate_cmpu_bool; eauto.
+Qed.
+
+Lemma simplify_ccompimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r v n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v),
+ eval_sval ctx (expanse_condimm_int32s c (hrs r) n) =
+ Some (Val.of_optbool (Val.cmp_bool c v (Vint n))).
+Proof.
+ intros.
+ unfold expanse_condimm_int32s, cond_int32s in *; destruct c;
+ intros; destruct (Int.eq n Int.zero) eqn:EQIMM; simpl;
+ try apply Int.same_if_eq in EQIMM; subst;
+ unfold loadimm32, sltimm32, xorimm32, opimm32, load_hilo32;
+ try rewrite !REG_EQ, OKv1;
+ unfold Val.cmp, zero32.
+ all:
+ try apply xor_neg_ltle_cmp;
+ try apply xor_neg_ltge_cmp; trivial.
+ 4:
+ try destruct (Int.eq n (Int.repr Int.max_signed)) eqn:EQMAX; subst;
+ try apply Int.same_if_eq in EQMAX; subst; simpl.
+ 4:
+ intros; try (specialize make_immed32_sound with (Int.one);
+ destruct (make_immed32 Int.one) eqn:EQMKI_A1); intros; simpl.
+ 6:
+ intros; try (specialize make_immed32_sound with (Int.add n Int.one);
+ destruct (make_immed32 (Int.add n Int.one)) eqn:EQMKI_A2); intros; simpl.
+ 1,2,3,8,9:
+ intros; try (specialize make_immed32_sound with (n);
+ destruct (make_immed32 n) eqn:EQMKI); intros; simpl.
+ all:
+ try destruct (Int.eq lo Int.zero) eqn:EQLO32;
+ try apply Int.same_if_eq in EQLO32; subst;
+ try rewrite !REG_EQ, OKv1;
+ try rewrite (Int.add_commut _ Int.zero), Int.add_zero_l in H; subst; simpl;
+ unfold Val.cmp, eval_may_undef, zero32, Val.add; simpl;
+ destruct v; auto.
+ all:
+ try rewrite ltu_12_wordsize;
+ try rewrite <- H;
+ try (apply cmp_ltle_add_one; auto);
+ try rewrite Int.add_commut, Int.add_zero_l in *;
+ try (
+ simpl; trivial;
+ try rewrite Int.xor_is_zero;
+ try destruct (Int.lt _ _) eqn:EQLT; trivial;
+ try rewrite lt_maxsgn_false_int in EQLT;
+ simpl; trivial; try discriminate; fail).
+Qed.
+
+Lemma simplify_ccompuimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r v n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v),
+ eval_sval ctx (expanse_condimm_int32u c (hrs r) n) =
+ Some (Val.of_optbool (Val.cmpu_bool (Mem.valid_pointer (cm0 ctx)) c v (Vint n))).
+Proof.
+ intros.
+ unfold expanse_condimm_int32u, cond_int32u in *; destruct c;
+ intros; destruct (Int.eq n Int.zero) eqn:EQIMM; simpl;
+ try apply Int.same_if_eq in EQIMM; subst;
+ unfold loadimm32, sltuimm32, opimm32, load_hilo32;
+ try rewrite !REG_EQ, OKv1; trivial;
+ try rewrite xor_neg_ltle_cmpu;
+ unfold Val.cmpu, zero32.
+ all:
+ try (specialize make_immed32_sound with n;
+ destruct (make_immed32 n) eqn:EQMKI);
+ try destruct (Int.eq lo Int.zero) eqn:EQLO;
+ try apply Int.same_if_eq in EQLO; subst;
+ intros; subst; simpl;
+ try rewrite !REG_EQ, OKv1;
+ unfold eval_may_undef, Val.cmpu;
+ destruct v; simpl; auto;
+ try rewrite EQIMM; try destruct (Archi.ptr64) eqn:EQARCH; simpl;
+ try rewrite ltu_12_wordsize; trivial;
+ try rewrite Int.add_commut, Int.add_zero_l in *;
+ try destruct (Int.ltu _ _) eqn:EQLTU; simpl;
+ try rewrite EQLTU; simpl; try rewrite EQIMM;
+ try rewrite EQARCH; trivial.
+Qed.
+
+Lemma simplify_ccompl_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (cond_int64s c (make_lfsv_cmp (is_inv_cmp_int c) (hrs r) (hrs r0)) None) =
+ Some (Val.of_optbool (Val.cmpl_bool c v v0)).
+Proof.
+ intros.
+ unfold cond_int64s in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpl.
+ 1,2,3: rewrite optbool_mktotal; trivial.
+ replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmpl_bool; trivial.
+ rewrite optbool_mktotal; trivial.
+Qed.
+
+Lemma simplify_ccomplu_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (cond_int64u c (make_lfsv_cmp (is_inv_cmp_int c) (hrs r) (hrs r0)) None) =
+ Some (Val.of_optbool (Val.cmplu_bool (Mem.valid_pointer (cm0 ctx)) c v v0)).
+Proof.
+ intros.
+ unfold cond_int64u in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmplu.
+ 1,2,3: rewrite optbool_mktotal; trivial; eauto.
+ replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmplu_bool; trivial.
+ rewrite optbool_mktotal; trivial.
+Qed.
+
+Lemma simplify_ccomplimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r v n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v),
+ eval_sval ctx (expanse_condimm_int64s c (hrs r) n) =
+ Some (Val.of_optbool (Val.cmpl_bool c v (Vlong n))).
+Proof.
+ intros.
+ unfold expanse_condimm_int64s, cond_int64s in *; destruct c;
+ intros; destruct (Int64.eq n Int64.zero) eqn:EQIMM; simpl;
+ try apply Int64.same_if_eq in EQIMM; subst;
+ unfold loadimm32, loadimm64, sltimm64, xorimm64, opimm64, load_hilo32, load_hilo64;
+ try rewrite !REG_EQ, OKv1;
+ unfold Val.cmpl, zero64.
+ all:
+ try apply xor_neg_ltle_cmpl;
+ try apply xor_neg_ltge_cmpl;
+ try rewrite optbool_mktotal; trivial.
+ 4:
+ try destruct (Int64.eq n (Int64.repr Int64.max_signed)) eqn:EQMAX; subst;
+ try apply Int64.same_if_eq in EQMAX; subst; simpl.
+ 4:
+ intros; try (specialize make_immed32_sound with (Int.one);
+ destruct (make_immed32 Int.one) eqn:EQMKI_A1); intros; simpl.
+ 6:
+ intros; try (specialize make_immed64_sound with (Int64.add n Int64.one);
+ destruct (make_immed64 (Int64.add n Int64.one)) eqn:EQMKI_A2); intros; simpl.
+ 1,2,3,9,10:
+ intros; try (specialize make_immed64_sound with (n);
+ destruct (make_immed64 n) eqn:EQMKI); intros; simpl.
+ all:
+ try destruct (Int.eq lo Int.zero) eqn:EQLO32;
+ try apply Int.same_if_eq in EQLO32; subst;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO64;
+ try apply Int64.same_if_eq in EQLO64; subst; simpl;
+ try rewrite !REG_EQ, OKv1;
+ try rewrite (Int64.add_commut _ Int64.zero), Int64.add_zero_l in H; subst;
+ unfold Val.cmpl, Val.addl;
+ try rewrite optbool_mktotal; trivial;
+ destruct v; auto.
+ all:
+ try rewrite <- optbool_mktotal; trivial;
+ try rewrite Int64.add_commut, Int64.add_zero_l in *;
+ try fold (Val.cmpl Clt (Vlong i) (Vlong imm));
+ try fold (Val.cmpl Clt (Vlong i) (Vlong (Int64.sign_ext 32 (Int64.shl hi (Int64.repr 12)))));
+ try fold (Val.cmpl Clt (Vlong i) (Vlong (Int64.add (Int64.sign_ext 32 (Int64.shl hi (Int64.repr 12))) lo))).
+ all:
+ try rewrite <- cmpl_ltle_add_one; auto;
+ try rewrite ltu_12_wordsize;
+ try rewrite Int.add_commut, Int.add_zero_l in *;
+ simpl; try rewrite lt_maxsgn_false_long;
+ try (rewrite <- H; trivial; fail);
+ simpl; trivial.
+Qed.
+
+Lemma simplify_ccompluimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r v n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v),
+ eval_sval ctx (expanse_condimm_int64u c (hrs r) n) =
+ Some (Val.of_optbool
+ (Val.cmplu_bool (Mem.valid_pointer (cm0 ctx)) c v (Vlong n))).
+Proof.
+ intros.
+ unfold expanse_condimm_int64u, cond_int64u in *; destruct c;
+ intros; destruct (Int64.eq n Int64.zero) eqn:EQIMM; simpl;
+ unfold loadimm64, sltuimm64, opimm64, load_hilo64;
+ try rewrite !REG_EQ, OKv1;
+ unfold Val.cmplu, zero64.
+ (* Simplify make immediate and decompose subcases *)
+ all:
+ try (specialize make_immed64_sound with n;
+ destruct (make_immed64 n) eqn:EQMKI);
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ try rewrite !REG_EQ, OKv1.
+ (* Ceq, Cne, Clt = itself *)
+ all: intros; try apply Int64.same_if_eq in EQIMM; subst; trivial.
+ (* Cle = xor (Clt) *)
+ all: try apply xor_neg_ltle_cmplu; trivial.
+ (* Others subcases with swap/negation *)
+ all:
+ unfold Val.cmplu, eval_may_undef, zero64, Val.addl;
+ try apply Int64.same_if_eq in EQLO; subst;
+ try rewrite Int64.add_commut, Int64.add_zero_l in *; trivial;
+ try (rewrite <- xor_neg_ltle_cmplu; unfold Val.cmplu;
+ trivial; fail);
+ try rewrite optbool_mktotal; trivial.
+ all:
+ try destruct v; simpl; auto;
+ try destruct (Archi.ptr64); simpl;
+ try rewrite EQIMM;
+ try destruct (Int64.ltu _ _);
+ try rewrite <- optbool_mktotal; trivial.
+Qed.
+
+Lemma simplify_ccompf_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (expanse_cond_fp false cond_float c
+ (make_lfsv_cmp (is_inv_cmp_float c) (hrs r) (hrs r0))) =
+ Some (Val.of_optbool (Val.cmpf_bool c v v0)).
+Proof.
+ intros.
+ unfold expanse_cond_fp in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpf.
+ - replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmpf_bool; trivial.
+ - replace (Cle) with (swap_comparison Cge) by auto;
+ rewrite Val.swap_cmpf_bool; trivial.
+Qed.
+
+Lemma simplify_cnotcompf_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (expanse_cond_fp true cond_float c
+ (make_lfsv_cmp (is_inv_cmp_float c) (hrs r) (hrs r0))) =
+ Some (Val.of_optbool (option_map negb (Val.cmpf_bool c v v0))).
+Proof.
+ intros.
+ unfold expanse_cond_fp in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpf.
+ 1,3,4: apply xor_neg_optb'.
+ all: destruct v, v0; simpl; trivial.
+ rewrite Float.cmp_ne_eq; rewrite negb_involutive; trivial.
+ 1: replace (Clt) with (swap_comparison Cgt) by auto; rewrite <- Float.cmp_swap; simpl.
+ 2: replace (Cle) with (swap_comparison Cge) by auto; rewrite <- Float.cmp_swap; simpl.
+ all: destruct (Float.cmp _ _ _); trivial.
+Qed.
+
+Lemma simplify_ccompfs_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0), eval_sval ctx
+ (expanse_cond_fp false cond_single c
+ (make_lfsv_cmp (is_inv_cmp_float c) (hrs r) (hrs r0))) =
+ Some (Val.of_optbool (Val.cmpfs_bool c v v0)).
+Proof.
+ intros.
+ unfold expanse_cond_fp in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpfs.
+ - replace (Clt) with (swap_comparison Cgt) by auto;
+ rewrite Val.swap_cmpfs_bool; trivial.
+ - replace (Cle) with (swap_comparison Cge) by auto;
+ rewrite Val.swap_cmpfs_bool; trivial.
+Qed.
+
+Lemma simplify_cnotcompfs_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ c r r0 v v0: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (OKv1 : eval_sval ctx (st r) = Some v)
+ (OKv2 : eval_sval ctx (st r0) = Some v0),
+ eval_sval ctx
+ (expanse_cond_fp true cond_single c
+ (make_lfsv_cmp (is_inv_cmp_float c) (hrs r) (hrs r0))) =
+ Some (Val.of_optbool (option_map negb (Val.cmpfs_bool c v v0))).
+Proof.
+ intros.
+ unfold expanse_cond_fp in *; destruct c; simpl;
+ rewrite !REG_EQ, OKv1, OKv2; trivial;
+ unfold Val.cmpfs.
+ 1,3,4: apply xor_neg_optb'.
+ all: destruct v, v0; simpl; trivial.
+ rewrite Float32.cmp_ne_eq; rewrite negb_involutive; trivial.
+ 1: replace (Clt) with (swap_comparison Cgt) by auto; rewrite <- Float32.cmp_swap; simpl.
+ 2: replace (Cle) with (swap_comparison Cge) by auto; rewrite <- Float32.cmp_swap; simpl.
+ all: destruct (Float32.cmp _ _ _); trivial.
+Qed.
+
+Lemma simplify_intconst_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (H : match lr with
+ | nil => Some (loadimm32 n)
+ | _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Ointconst n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv OK1; inv H; simpl;
+ unfold loadimm32, load_hilo32, make_lfsv_single; simpl;
+ specialize make_immed32_sound with (n);
+ destruct (make_immed32 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO; simpl;
+ try apply Int.same_if_eq in EQLO; subst;
+ try rewrite Int.add_commut, Int.add_zero_l;
+ try rewrite ltu_12_wordsize; try rewrite H; trivial.
+Qed.
+
+Lemma simplify_longconst_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (H : match lr with
+ | nil => Some (loadimm64 n)
+ | _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Olongconst n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv OK1; inv H; simpl;
+ unfold loadimm64, load_hilo64, make_lfsv_single; simpl;
+ specialize make_immed64_sound with (n);
+ destruct (make_immed64 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ try apply Int64.same_if_eq in EQLO; subst;
+ try rewrite Int64.add_commut, Int64.add_zero_l;
+ try rewrite Int64.add_commut;
+ try rewrite ltu_12_wordsize; try rewrite H; trivial.
+Qed.
+
+Lemma simplify_floatconst_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (H : match lr with
+ | nil =>
+ Some
+ (fSop Ofloat_of_bits
+ (make_lfsv_single (loadimm64 (Float.to_bits n))))
+ | _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Ofloatconst n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv OK1; inv H; simpl;
+ unfold loadimm64, load_hilo64; simpl;
+ specialize make_immed64_sound with (Float.to_bits n);
+ destruct (make_immed64 (Float.to_bits n)) eqn:EQMKI; intros;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO;
+ simpl.
+ - try rewrite Int64.add_commut, Int64.add_zero_l; inv H;
+ try rewrite Float.of_to_bits; trivial.
+ - apply Int64.same_if_eq in EQLO; subst.
+ try rewrite Int64.add_commut, Int64.add_zero_l in H.
+ rewrite <- H; try rewrite Float.of_to_bits; trivial.
+ - rewrite <- H; try rewrite Float.of_to_bits; trivial.
+ - rewrite <- H; try rewrite Float.of_to_bits; trivial.
+Qed.
+
+Lemma simplify_singleconst_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (H : match lr with
+ | nil =>
+ Some
+ (fSop Osingle_of_bits
+ (make_lfsv_single (loadimm32 (Float32.to_bits n))))
+ | _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Osingleconst n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv OK1; inv H; simpl;
+ unfold loadimm32, load_hilo32; simpl;
+ specialize make_immed32_sound with (Float32.to_bits n);
+ destruct (make_immed32 (Float32.to_bits n)) eqn:EQMKI; intros;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO;
+ simpl.
+ { try rewrite Int.add_commut, Int.add_zero_l; inv H;
+ try rewrite Float32.of_to_bits; trivial. }
+ all:
+ try apply Int.same_if_eq in EQLO; subst;
+ try rewrite Int.add_commut, Int.add_zero_l in H; simpl;
+ rewrite ltu_12_wordsize; simpl; try rewrite <- H;
+ try rewrite Float32.of_to_bits; trivial.
+Qed.
+
+Lemma simplify_cast8signed_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil =>
+ Some
+ (fSop (Oshrimm (Int.repr 24))
+ (make_lfsv_single
+ (fSop (Oshlimm (Int.repr 24)) (make_lfsv_single (hrs a1)))))
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) Ocast8signed args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ rewrite !REG_EQ.
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1.
+ unfold Val.shr, Val.shl, Val.sign_ext;
+ destruct v; simpl; auto.
+ assert (A: Int.ltu (Int.repr 24) Int.iwordsize = true) by auto.
+ rewrite A. rewrite Int.sign_ext_shr_shl; simpl; trivial. cbn; lia.
+Qed.
+
+Lemma simplify_cast16signed_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil =>
+ Some
+ (fSop (Oshrimm (Int.repr 16))
+ (make_lfsv_single
+ (fSop (Oshlimm (Int.repr 16)) (make_lfsv_single (hrs a1)))))
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) Ocast16signed args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ rewrite !REG_EQ.
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1.
+ unfold Val.shr, Val.shl, Val.sign_ext;
+ destruct v; simpl; auto.
+ assert (A: Int.ltu (Int.repr 16) Int.iwordsize = true) by auto.
+ rewrite A. rewrite Int.sign_ext_shr_shl; simpl; trivial. cbn; lia.
+Qed.
+
+Lemma simplify_addimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (addimm32 (hrs a1) n None)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oaddimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl.
+ unfold addimm32, opimm32, load_hilo32, make_lfsv_cmp; simpl;
+ specialize make_immed32_sound with (n);
+ destruct (make_immed32 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ apply Int.same_if_eq in EQLO; subst;
+ rewrite Int.add_commut, Int.add_zero_l;
+ rewrite ltu_12_wordsize; trivial.
+Qed.
+
+Lemma simplify_andimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (andimm32 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oandimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl.
+ unfold andimm32, opimm32, load_hilo32, make_lfsv_cmp; simpl;
+ specialize make_immed32_sound with (n);
+ destruct (make_immed32 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ fold (Val.and (Vint imm) v); rewrite Val.and_commut; trivial.
+ apply Int.same_if_eq in EQLO; subst;
+ rewrite Int.add_commut, Int.add_zero_l;
+ rewrite ltu_12_wordsize; trivial.
+Qed.
+
+Lemma simplify_orimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (orimm32 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oorimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl.
+ unfold orimm32, opimm32, load_hilo32, make_lfsv_cmp; simpl;
+ specialize make_immed32_sound with (n);
+ destruct (make_immed32 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ fold (Val.or (Vint imm) v); rewrite Val.or_commut; trivial.
+ apply Int.same_if_eq in EQLO; subst;
+ rewrite Int.add_commut, Int.add_zero_l;
+ rewrite ltu_12_wordsize; trivial.
+Qed.
+
+Lemma simplify_xorimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (xorimm32 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oxorimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl.
+ unfold xorimm32, opimm32, load_hilo32, make_lfsv_cmp; simpl;
+ specialize make_immed32_sound with (n);
+ destruct (make_immed32 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ apply Int.same_if_eq in EQLO; subst;
+ rewrite Int.add_commut, Int.add_zero_l;
+ rewrite ltu_12_wordsize; trivial.
+Qed.
+
+Lemma simplify_shrximm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil =>
+ if Int.eq n Int.zero
+ then
+ Some
+ (fSop (OEmayundef (MUshrx n))
+ (make_lfsv_cmp false (hrs a1) (hrs a1)))
+ else
+ if Int.eq n Int.one
+ then
+ Some
+ (fSop (OEmayundef (MUshrx n))
+ (make_lfsv_cmp false
+ (fSop (Oshrimm Int.one)
+ (make_lfsv_single
+ (fSop Oadd
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshruimm (Int.repr 31))
+ (make_lfsv_single (hrs a1)))))))
+ (fSop (Oshrimm Int.one)
+ (make_lfsv_single
+ (fSop Oadd
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshruimm (Int.repr 31))
+ (make_lfsv_single (hrs a1)))))))))
+ else
+ Some
+ (fSop (OEmayundef (MUshrx n))
+ (make_lfsv_cmp false
+ (fSop (Oshrimm n)
+ (make_lfsv_single
+ (fSop Oadd
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshruimm (Int.sub Int.iwordsize n))
+ (make_lfsv_single
+ (fSop (Oshrimm (Int.repr 31))
+ (make_lfsv_single (hrs a1)))))))))
+ (fSop (Oshrimm n)
+ (make_lfsv_single
+ (fSop Oadd
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshruimm (Int.sub Int.iwordsize n))
+ (make_lfsv_single
+ (fSop (Oshrimm (Int.repr 31))
+ (make_lfsv_single (hrs a1)))))))))))
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oshrximm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence).
+ assert (A: Int.ltu Int.zero (Int.repr 31) = true) by auto.
+ assert (B: Int.ltu (Int.repr 31) Int.iwordsize = true) by auto.
+ assert (C: Int.ltu Int.one Int.iwordsize = true) by auto.
+ destruct (Int.eq n Int.zero) eqn:EQ0;
+ destruct (Int.eq n Int.one) eqn:EQ1.
+ { apply Int.same_if_eq in EQ0.
+ apply Int.same_if_eq in EQ1; subst. discriminate. }
+ all:
+ simpl in OK1; inv H; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1;
+ destruct (Val.shrx v (Vint n)) eqn:TOTAL; cbn;
+ unfold eval_may_undef.
+ 2,4,6:
+ unfold Val.shrx in TOTAL;
+ destruct v; simpl in TOTAL; simpl; try congruence;
+ try rewrite B; simpl; try rewrite C; simpl;
+ try destruct (Val.shr _ _);
+ destruct (Int.ltu n (Int.repr 31)); try congruence.
+ - destruct v; simpl in TOTAL; try congruence;
+ apply Int.same_if_eq in EQ0; subst;
+ rewrite A, Int.shrx_zero in TOTAL;
+ [auto | cbn; lia].
+ - apply Int.same_if_eq in EQ1; subst;
+ unfold Val.shr, Val.shru, Val.shrx, Val.add; simpl;
+ destruct v; simpl in *; try discriminate; trivial.
+ rewrite B, C.
+ rewrite Int.shrx1_shr in TOTAL; auto.
+ - exploit Val.shrx_shr_2; eauto. rewrite EQ0.
+ intros; subst.
+ destruct v; simpl in *; try discriminate; trivial.
+ rewrite B in *.
+ destruct Int.ltu eqn:EQN0 in TOTAL; try discriminate.
+ simpl in *.
+ destruct Int.ltu eqn:EQN1 in TOTAL; try discriminate.
+ replace Int.iwordsize with (Int.repr 32) in * by auto.
+ rewrite !EQN1. simpl in *.
+ destruct Int.ltu eqn:EQN2 in TOTAL; try discriminate.
+ rewrite !EQN2. rewrite EQN0.
+ reflexivity.
+Qed.
+
+Lemma simplify_cast32unsigned_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil =>
+ Some
+ (fSop (Oshrluimm (Int.repr 32))
+ (make_lfsv_single
+ (fSop (Oshllimm (Int.repr 32))
+ (make_lfsv_single
+ (fSop Ocast32signed (make_lfsv_single (hrs a1)))))))
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) Ocast32unsigned args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl.
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1.
+ unfold Val.shrlu, Val.shll, Val.longofint, Val.longofintu.
+ destruct v; simpl; auto.
+ assert (A: Int.ltu (Int.repr 32) Int64.iwordsize' = true) by auto.
+ rewrite A. rewrite Int64.shru'_shl'; auto.
+ replace (Int.ltu (Int.repr 32) (Int.repr 32)) with (false) by auto.
+ rewrite cast32unsigned_from_cast32signed.
+ replace Int64.zwordsize with 64 by auto.
+ rewrite Int.unsigned_repr; cbn; try lia.
+ replace (Int.sub (Int.repr 32) (Int.repr 32)) with (Int.zero) by auto.
+ rewrite Int64.shru'_zero. reflexivity.
+Qed.
+
+Lemma simplify_addlimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (addimm64 (hrs a1) n None)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oaddlimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ unfold addimm64, opimm64, load_hilo64, make_lfsv_cmp; simpl;
+ specialize make_immed64_sound with (n);
+ destruct (make_immed64 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ apply Int64.same_if_eq in EQLO; subst.
+ rewrite Int64.add_commut, Int64.add_zero_l; trivial.
+Qed.
+
+Lemma simplify_andlimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (andimm64 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oandlimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ unfold andimm64, opimm64, load_hilo64, make_lfsv_cmp; simpl;
+ specialize make_immed64_sound with (n);
+ destruct (make_immed64 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ fold (Val.andl (Vlong imm) v); rewrite Val.andl_commut; trivial.
+ apply Int64.same_if_eq in EQLO; subst;
+ rewrite Int64.add_commut, Int64.add_zero_l; trivial.
+Qed.
+
+Lemma simplify_orlimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (orimm64 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oorlimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ unfold orimm64, opimm64, load_hilo64, make_lfsv_cmp; simpl;
+ specialize make_immed64_sound with (n);
+ destruct (make_immed64 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ fold (Val.orl (Vlong imm) v); rewrite Val.orl_commut; trivial.
+ apply Int64.same_if_eq in EQLO; subst;
+ rewrite Int64.add_commut, Int64.add_zero_l; trivial.
+Qed.
+
+Lemma simplify_xorlimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil => Some (xorimm64 (hrs a1) n)
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oxorlimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence);
+ simpl in OK1; inv H; simpl;
+ unfold xorimm64, opimm64, load_hilo64, make_lfsv_cmp; simpl;
+ specialize make_immed64_sound with (n);
+ destruct (make_immed64 (n)) eqn:EQMKI; intros; simpl;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1; trivial.
+ apply Int64.same_if_eq in EQLO; subst;
+ rewrite Int64.add_commut, Int64.add_zero_l; trivial.
+Qed.
+
+Lemma simplify_shrxlimm_correct (ctx: iblock_exec_context) (hrs: ristate) (st: sistate)
+ args fsv lr n: forall
+ (REG_EQ : forall r : positive, eval_sval ctx (hrs r) = eval_sval ctx (st r))
+ (H : match lr with
+ | nil => None
+ | a1 :: nil =>
+ if Int.eq n Int.zero
+ then
+ Some
+ (fSop (OEmayundef (MUshrxl n))
+ (make_lfsv_cmp false (hrs a1) (hrs a1)))
+ else
+ if Int.eq n Int.one
+ then
+ Some
+ (fSop (OEmayundef (MUshrxl n))
+ (make_lfsv_cmp false
+ (fSop (Oshrlimm Int.one)
+ (make_lfsv_single
+ (fSop Oaddl
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshrluimm (Int.repr 63))
+ (make_lfsv_single (hrs a1)))))))
+ (fSop (Oshrlimm Int.one)
+ (make_lfsv_single
+ (fSop Oaddl
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshrluimm (Int.repr 63))
+ (make_lfsv_single (hrs a1)))))))))
+ else
+ Some
+ (fSop (OEmayundef (MUshrxl n))
+ (make_lfsv_cmp false
+ (fSop (Oshrlimm n)
+ (make_lfsv_single
+ (fSop Oaddl
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshrluimm (Int.sub Int64.iwordsize' n))
+ (make_lfsv_single
+ (fSop (Oshrlimm (Int.repr 63))
+ (make_lfsv_single (hrs a1)))))))))
+ (fSop (Oshrlimm n)
+ (make_lfsv_single
+ (fSop Oaddl
+ (make_lfsv_cmp false (hrs a1)
+ (fSop (Oshrluimm (Int.sub Int64.iwordsize' n))
+ (make_lfsv_single
+ (fSop (Oshrlimm (Int.repr 63))
+ (make_lfsv_single (hrs a1)))))))))))
+ | a1 :: _ :: _ => None
+ end = Some fsv)
+ (OK1 : eval_list_sval ctx (list_sval_inj (map st lr)) = Some args),
+ eval_sval ctx fsv =
+ eval_operation (cge ctx) (csp ctx) (Oshrxlimm n) args (cm0 ctx).
+Proof.
+ intros.
+ repeat (destruct lr; simpl; try congruence).
+ assert (A: Int.ltu Int.zero (Int.repr 63) = true) by auto.
+ assert (B: Int.ltu (Int.repr 63) Int64.iwordsize' = true) by auto.
+ assert (C: Int.ltu Int.one Int64.iwordsize' = true) by auto.
+ destruct (Int.eq n Int.zero) eqn:EQ0;
+ destruct (Int.eq n Int.one) eqn:EQ1.
+ { apply Int.same_if_eq in EQ0.
+ apply Int.same_if_eq in EQ1; subst. discriminate. }
+ all:
+ simpl in OK1; inv H; simpl;
+ rewrite !REG_EQ;
+ destruct (eval_sval ctx (st p)) eqn:OKv1; try congruence; inv OK1;
+ destruct (Val.shrxl v (Vint n)) eqn:TOTAL; cbn;
+ unfold eval_may_undef.
+ 2,4,6:
+ unfold Val.shrxl in TOTAL;
+ destruct v; simpl in TOTAL; simpl; try congruence;
+ try rewrite B; simpl; try rewrite C; simpl;
+ try destruct (Val.shrl _ _);
+ destruct (Int.ltu n (Int.repr 63)); try congruence.
+ - destruct v; simpl in TOTAL; try congruence;
+ apply Int.same_if_eq in EQ0; subst;
+ rewrite A, Int64.shrx'_zero in *.
+ assumption.
+ - apply Int.same_if_eq in EQ1; subst;
+ unfold Val.shrl, Val.shrlu, Val.shrxl, Val.addl; simpl;
+ destruct v; simpl in *; try discriminate; trivial.
+ rewrite B, C.
+ rewrite Int64.shrx'1_shr' in TOTAL; auto.
+ - exploit Val.shrxl_shrl_2; eauto. rewrite EQ0.
+ intros; subst.
+ destruct v; simpl in *; try discriminate; trivial.
+ rewrite B in *.
+ destruct Int.ltu eqn:EQN0 in TOTAL; try discriminate.
+ simpl in *.
+ destruct Int.ltu eqn:EQN1 in TOTAL; try discriminate.
+ replace Int64.iwordsize' with (Int.repr 64) in * by auto.
+ rewrite !EQN1. simpl in *.
+ destruct Int.ltu eqn:EQN2 in TOTAL; try discriminate.
+ rewrite !EQN2. rewrite EQN0.
+ reflexivity.
+Qed.
+
+(* Main proof of simplification *)
+
+Lemma target_op_simplify_correct ctx op lr hrs fsv st args: forall
+ (H: target_op_simplify op lr hrs = Some fsv)
+ (REF: ris_refines ctx hrs st)
+ (OK0: ris_ok ctx hrs)
+ (OK1: eval_list_sval ctx (list_sval_inj (map (si_sreg st) lr)) = Some args),
+ eval_sval ctx fsv = eval_operation (cge ctx) (csp ctx) op args (cm0 ctx).
+Proof.
+ unfold target_op_simplify; simpl.
+ intros H ? ? ?; inv REF.
+ destruct op; try congruence.
+ eapply simplify_intconst_correct; eauto.
+ eapply simplify_longconst_correct; eauto.
+ eapply simplify_floatconst_correct; eauto.
+ eapply simplify_singleconst_correct; eauto.
+ eapply simplify_cast8signed_correct; eauto.
+ eapply simplify_cast16signed_correct; eauto.
+ eapply simplify_addimm_correct; eauto.
+ eapply simplify_andimm_correct; eauto.
+ eapply simplify_orimm_correct; eauto.
+ eapply simplify_xorimm_correct; eauto.
+ eapply simplify_shrximm_correct; eauto.
+ eapply simplify_cast32unsigned_correct; eauto.
+ eapply simplify_addlimm_correct; eauto.
+ eapply simplify_andlimm_correct; eauto.
+ eapply simplify_orlimm_correct; eauto.
+ eapply simplify_xorlimm_correct; eauto.
+ eapply simplify_shrxlimm_correct; eauto.
+ (* Ocmp expansions *)
+ destruct cond; repeat (destruct lr; simpl; try congruence);
+ simpl in OK1;
+ try (destruct (eval_sval ctx (si_sreg st r)) eqn:OKv1; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r0)) eqn:OKv2; try congruence);
+ inv H; inv OK1.
+ - eapply simplify_ccomp_correct; eauto.
+ - eapply simplify_ccompu_correct; eauto.
+ - eapply simplify_ccompimm_correct; eauto.
+ - eapply simplify_ccompuimm_correct; eauto.
+ - eapply simplify_ccompl_correct; eauto.
+ - eapply simplify_ccomplu_correct; eauto.
+ - eapply simplify_ccomplimm_correct; eauto.
+ - eapply simplify_ccompluimm_correct; eauto.
+ - eapply simplify_ccompf_correct; eauto.
+ - eapply simplify_cnotcompf_correct; eauto.
+ - eapply simplify_ccompfs_correct; eauto.
+ - eapply simplify_cnotcompfs_correct; eauto.
+Qed.
+
+Lemma target_cbranch_expanse_correct hrs c l ctx st c' l': forall
+ (TARGET: target_cbranch_expanse hrs c l = Some (c', l'))
+ (REF: ris_refines ctx hrs st)
+ (OK: ris_ok ctx hrs),
+ eval_scondition ctx c' l' =
+ eval_scondition ctx c (list_sval_inj (map (si_sreg st) l)).
+Proof.
+ unfold target_cbranch_expanse, eval_scondition; simpl.
+ intros H ? ?. inversion REF.
+ destruct c; try congruence;
+ repeat (destruct l; simpl in H; try congruence).
+ 1,2,5,6:
+ destruct c; inv H; simpl;
+ rewrite !REG_EQ;
+ try (destruct (eval_smem ctx (si_smem st)) eqn:OKmem; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r)) eqn:OKv1; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r0)) eqn:OKv2; try congruence);
+ try replace (Cle) with (swap_comparison Cge) by auto;
+ try replace (Clt) with (swap_comparison Cgt) by auto;
+ try rewrite Val.swap_cmp_bool; trivial;
+ try rewrite Val.swap_cmpu_bool; trivial;
+ try rewrite Val.swap_cmpl_bool; trivial;
+ try rewrite Val.swap_cmplu_bool; trivial.
+ 1,2,3,4:
+ try destruct (Int.eq n Int.zero) eqn: EQIMM;
+ try apply Int.same_if_eq in EQIMM;
+ try destruct (Int64.eq n Int64.zero) eqn: EQIMM;
+ try apply Int64.same_if_eq in EQIMM;
+ destruct c; inv H; simpl;
+ rewrite !REG_EQ;
+ try (destruct (eval_smem ctx (si_smem st)) eqn:OKmem; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r)) eqn:OKv1; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r0)) eqn:OKv2; try congruence);
+ unfold loadimm32, load_hilo32, Val.cmp, Val.cmpu, zero32;
+ unfold loadimm64, load_hilo64, Val.cmpl, Val.cmplu, zero64;
+ intros; try (specialize make_immed32_sound with (n);
+ destruct (make_immed32 n) eqn:EQMKI); intros; simpl;
+ intros; try (specialize make_immed64_sound with (n);
+ destruct (make_immed64 n) eqn:EQMKI); intros; simpl;
+ try rewrite EQLO; simpl;
+ try destruct (Int.eq lo Int.zero) eqn:EQLO;
+ try destruct (Int64.eq lo Int64.zero) eqn:EQLO;
+ try apply Int.same_if_eq in EQLO; simpl; trivial;
+ try apply Int64.same_if_eq in EQLO; simpl; trivial;
+ unfold eval_may_undef;
+ try erewrite !fsi_sreg_get_correct; eauto;
+ try rewrite OKv1; simpl; trivial;
+ try destruct v; try rewrite H;
+ try rewrite ltu_12_wordsize; try rewrite EQLO;
+ try rewrite Int.add_commut, Int.add_zero_l;
+ try rewrite Int64.add_commut, Int64.add_zero_l;
+ try rewrite Int64.add_commut;
+ try rewrite Int.add_zero_l; try rewrite Int64.add_zero_l;
+ auto; simpl;
+ try rewrite H in EQIMM;
+ try rewrite EQLO in EQIMM;
+ try rewrite Int.add_commut, Int.add_zero_l in EQIMM;
+ try rewrite Int64.add_commut, Int64.add_zero_l in EQIMM;
+ try rewrite EQIMM; simpl;
+ try destruct (Archi.ptr64); trivial.
+
+ 1,2,3,4:
+ destruct c; inv H; simpl;
+ rewrite !REG_EQ;
+ try (destruct (eval_smem ctx (si_smem st)) eqn:OKmem; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r)) eqn:OKv1; try congruence);
+ try (destruct (eval_sval ctx (si_sreg st r0)) eqn:OKv2; try congruence);
+ unfold zero32, zero64, Val.cmpf, Val.cmpfs;
+ destruct v, v0; simpl; trivial;
+ try rewrite Float.cmp_ne_eq;
+ try rewrite Float32.cmp_ne_eq;
+ try rewrite <- Float.cmp_swap; simpl;
+ try rewrite <- Float32.cmp_swap; simpl;
+ try destruct (Float.cmp _ _); simpl;
+ try destruct (Float32.cmp _ _); simpl;
+ try rewrite Int.eq_true; simpl;
+ try rewrite Int.eq_false; try apply Int.one_not_zero;
+ simpl; trivial.
+Qed.
+Global Opaque target_op_simplify.
+Global Opaque target_cbranch_expanse.
diff --git a/riscV/Builtins1.v b/riscV/Builtins1.v
index cd6f8cc4..6691d15c 100644
--- a/riscV/Builtins1.v
+++ b/riscV/Builtins1.v
@@ -19,16 +19,35 @@
Require Import String Coqlib.
Require Import AST Integers Floats Values.
Require Import Builtins0.
+Require ExtValues.
-Inductive platform_builtin : Type := .
+Inductive platform_builtin : Type :=
+| BI_bits_of_float
+| BI_bits_of_double
+| BI_float_of_bits
+| BI_double_of_bits.
Local Open Scope string_scope.
Definition platform_builtin_table : list (string * platform_builtin) :=
- nil.
+ ("__builtin_bits_of_float", BI_bits_of_float)
+ :: ("__builtin_bits_of_double", BI_bits_of_double)
+ :: ("__builtin_float_of_bits", BI_float_of_bits)
+ :: ("__builtin_double_of_bits", BI_double_of_bits)
+ :: nil.
Definition platform_builtin_sig (b: platform_builtin) : signature :=
- match b with end.
+ match b with
+ | BI_bits_of_float => mksignature (Tsingle :: nil) Tint cc_default
+ | BI_bits_of_double => mksignature (Tfloat :: nil) Tlong cc_default
+ | BI_float_of_bits => mksignature (Tint :: nil) Tsingle cc_default
+ | BI_double_of_bits => mksignature (Tlong :: nil) Tfloat cc_default
+ end.
Definition platform_builtin_sem (b: platform_builtin) : builtin_sem (sig_res (platform_builtin_sig b)) :=
- match b with end.
+ match b with
+ | BI_bits_of_float => mkbuiltin_n1t Tsingle Tint Float32.to_bits
+ | BI_bits_of_double => mkbuiltin_n1t Tfloat Tlong Float.to_bits
+ | BI_float_of_bits => mkbuiltin_n1t Tint Tsingle Float32.of_bits
+ | BI_double_of_bits => mkbuiltin_n1t Tlong Tfloat Float.of_bits
+ end.
diff --git a/riscV/CBuiltins.ml b/riscV/CBuiltins.ml
index 9ff4e029..ca0dbc6d 100644
--- a/riscV/CBuiltins.ml
+++ b/riscV/CBuiltins.ml
@@ -47,6 +47,14 @@ let builtins = {
(TFloat(FDouble, []), [TFloat(FDouble, []); TFloat(FDouble, [])], false);
"__builtin_fmin",
(TFloat(FDouble, []), [TFloat(FDouble, []); TFloat(FDouble, [])], false);
+ "__builtin_bits_of_double",
+ (TInt(IULong, []), [TFloat(FDouble, [])], false);
+ "__builtin_bits_of_float",
+ (TInt(IUInt, []), [TFloat(FFloat, [])], false);
+ "__builtin_double_of_bits",
+ (TFloat(FDouble, []), [TInt(IULong, [])], false);
+ "__builtin_float_of_bits",
+ (TFloat(FFloat, []), [TInt(IUInt, [])], false);
]
}
diff --git a/riscV/CSE2deps.v b/riscV/CSE2deps.v
new file mode 100644
index 00000000..c0deacf0
--- /dev/null
+++ b/riscV/CSE2deps.v
@@ -0,0 +1,35 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ else true
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/riscV/CSE2depsproof.v b/riscV/CSE2depsproof.v
new file mode 100644
index 00000000..cf9e62b1
--- /dev/null
+++ b/riscV/CSE2depsproof.v
@@ -0,0 +1,147 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_size :
+ Ptrofs.wordsize = (if Archi.ptr64 then 64 else 32)%nat.
+Proof.
+ unfold Ptrofs.wordsize.
+ unfold Wordsize_Ptrofs.wordsize.
+ trivial.
+Qed.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = if Archi.ptr64 then 18446744073709551616 else 4294967296.
+Proof.
+ unfold Ptrofs.modulus.
+ rewrite ptrofs_size.
+ destruct Archi.ptr64; reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ rewrite ptrofs_modulus in *.
+ simpl in *.
+ inv ADDRR.
+ inv ADDRW.
+ destruct base; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: try rewrite ptrofs_modulus in *.
+ all: destruct Archi.ptr64.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End MEMORY_WRITE.
+End SOUNDNESS.
+
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+- (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ simpl in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ simpl in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+
+- (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/riscV/ConstpropOpproof.v b/riscV/ConstpropOpproof.v
index 8445e55f..74dc4a05 100644
--- a/riscV/ConstpropOpproof.v
+++ b/riscV/ConstpropOpproof.v
@@ -265,52 +265,84 @@ Qed.
Lemma make_divimm_correct:
forall n r1 r2 v,
- Val.divs e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divs e#r1 e#r2) = v ->
e#r2 = Vint n ->
let (op, args) := make_divimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divimm.
- predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
- destruct (e#r1) eqn:?;
- try (rewrite Val.divs_one in H; exists (Vint i); split; simpl; try rewrite Heqv0; auto);
- inv H; auto.
- destruct (Int.is_power2 n) eqn:?.
- destruct (Int.ltu i (Int.repr 31)) eqn:?.
- exists v; split; auto. simpl. eapply Val.divs_pow2; eauto. congruence.
- exists v; auto.
- exists v; auto.
+ predSpec Int.eq Int.eq_spec n Int.one; intros; subst; rewrite H0.
+ { destruct (e # r1) eqn:Er1.
+ all: try (cbn; exists (e # r1); split; auto; fail).
+ rewrite Val.divs_one.
+ cbn.
+ rewrite Er1.
+ exists (Vint i); split; auto.
+ }
+ destruct (Int.is_power2 n) eqn:Power2.
+ {
+ destruct (Int.ltu i (Int.repr 31)) eqn:iLT31.
+ {
+ cbn.
+ exists (Val.maketotal (Val.shrx e # r1 (Vint i))); split; auto.
+ destruct (Val.divs e # r1 (Vint n)) eqn:DIVS; cbn; auto.
+ rewrite Val.divs_pow2 with (y:=v) (n:=n).
+ cbn.
+ all: auto.
+ }
+ exists (Val.maketotal (Val.divs e # r1 (Vint n))); split; cbn; auto; congruence.
+ }
+ exists (Val.maketotal (Val.divs e # r1 (Vint n))); split; cbn; auto; congruence.
Qed.
Lemma make_divuimm_correct:
forall n r1 r2 v,
- Val.divu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divu e#r1 e#r2) = v ->
e#r2 = Vint n ->
let (op, args) := make_divuimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divuimm.
- predSpec Int.eq Int.eq_spec n Int.one; intros. subst. rewrite H0 in H.
- destruct (e#r1) eqn:?;
- try (rewrite Val.divu_one in H; exists (Vint i); split; simpl; try rewrite Heqv0; auto);
- inv H; auto.
- destruct (Int.is_power2 n) eqn:?.
- econstructor; split. simpl; eauto.
- rewrite H0 in H. erewrite Val.divu_pow2 by eauto. auto.
- exists v; auto.
+ predSpec Int.eq Int.eq_spec n Int.one; intros; subst; rewrite H0.
+ { destruct (e # r1) eqn:Er1.
+ all: try (cbn; exists (e # r1); split; auto; fail).
+ rewrite Val.divu_one.
+ cbn.
+ rewrite Er1.
+ exists (Vint i); split; auto.
+ }
+ destruct (Int.is_power2 n) eqn:Power2.
+ {
+ cbn.
+ exists (Val.shru e # r1 (Vint i)); split; auto.
+ destruct (Val.divu e # r1 (Vint n)) eqn:DIVU; cbn; auto.
+ rewrite Val.divu_pow2 with (y:=v) (n:=n).
+ all: auto.
+ }
+ exists (Val.maketotal (Val.divu e # r1 (Vint n))); split; cbn; auto; congruence.
Qed.
Lemma make_moduimm_correct:
forall n r1 r2 v,
- Val.modu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.modu e#r1 e#r2) = v ->
e#r2 = Vint n ->
let (op, args) := make_moduimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_moduimm.
destruct (Int.is_power2 n) eqn:?.
- exists v; split; auto. simpl. decEq. eapply Val.modu_pow2; eauto. congruence.
- exists v; auto.
+ { destruct (Val.modu e # r1 e # r2) eqn:MODU; cbn in H.
+ { subst v0.
+ exists v; split; auto.
+ cbn. decEq. eapply Val.modu_pow2; eauto. congruence.
+ }
+ subst v.
+ eexists; split; auto.
+ cbn. reflexivity.
+ }
+ exists v; split; auto.
+ cbn.
+ congruence.
Qed.
Lemma make_andimm_correct:
@@ -444,48 +476,82 @@ Qed.
Lemma make_divlimm_correct:
forall n r1 r2 v,
- Val.divls e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divls e#r1 e#r2) = v ->
e#r2 = Vlong n ->
let (op, args) := make_divlimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divlimm.
- destruct (Int64.is_power2' n) eqn:?. destruct (Int.ltu i (Int.repr 63)) eqn:?.
- rewrite H0 in H. econstructor; split. simpl; eauto. eapply Val.divls_pow2; eauto. auto.
- exists v; auto.
- exists v; auto.
+ destruct (Int64.is_power2' n) eqn:Power2.
+ {
+ destruct (Int.ltu i (Int.repr 63)) eqn:iLT63.
+ {
+ cbn.
+ exists (Val.maketotal (Val.shrxl e # r1 (Vint i))); split; auto.
+ rewrite H0 in H.
+ destruct (Val.divls e # r1 (Vlong n)) eqn:DIVS; cbn in H; auto.
+ {
+ subst v0.
+ rewrite Val.divls_pow2 with (y:=v) (n:=n).
+ cbn.
+ all: auto.
+ }
+ subst. auto.
+ }
+ cbn. subst. rewrite H0.
+ exists (Val.maketotal (Val.divls e # r1 (Vlong n))); split; auto.
+ }
+ cbn. subst. rewrite H0.
+ exists (Val.maketotal (Val.divls e # r1 (Vlong n))); split; auto.
Qed.
Lemma make_divluimm_correct:
forall n r1 r2 v,
- Val.divlu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.divlu e#r1 e#r2) = v ->
e#r2 = Vlong n ->
let (op, args) := make_divluimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_divluimm.
destruct (Int64.is_power2' n) eqn:?.
+ {
econstructor; split. simpl; eauto.
- rewrite H0 in H. destruct (e#r1); inv H. destruct (Int64.eq n Int64.zero); inv H2.
- simpl.
- erewrite Int64.is_power2'_range by eauto.
- erewrite Int64.divu_pow2' by eauto. auto.
- exists v; auto.
+ rewrite H0 in H. destruct (e#r1); inv H.
+ all: cbn; auto.
+ {
+ destruct (Int64.eq n Int64.zero); cbn; auto.
+ erewrite Int64.is_power2'_range by eauto.
+ erewrite Int64.divu_pow2' by eauto. auto.
+ }
+ }
+ exists v; split; auto.
+ cbn.
+ rewrite H.
+ reflexivity.
Qed.
Lemma make_modluimm_correct:
forall n r1 r2 v,
- Val.modlu e#r1 e#r2 = Some v ->
+ Val.maketotal (Val.modlu e#r1 e#r2) = v ->
e#r2 = Vlong n ->
let (op, args) := make_modluimm n r1 r2 in
exists w, eval_operation ge (Vptr sp Ptrofs.zero) op e##args m = Some w /\ Val.lessdef v w.
Proof.
intros; unfold make_modluimm.
destruct (Int64.is_power2 n) eqn:?.
- exists v; split; auto. simpl. decEq.
- rewrite H0 in H. destruct (e#r1); inv H. destruct (Int64.eq n Int64.zero); inv H2.
- simpl. erewrite Int64.modu_and by eauto. auto.
- exists v; auto.
+ {
+ econstructor; split. simpl; eauto.
+ rewrite H0 in H. destruct (e#r1); inv H.
+ all: cbn; auto.
+ {
+ destruct (Int64.eq n Int64.zero); cbn; auto.
+ erewrite Int64.modu_and by eauto. auto.
+ }
+ }
+ exists v; split; auto.
+ cbn.
+ rewrite H.
+ reflexivity.
Qed.
Lemma make_andlimm_correct:
@@ -633,14 +699,17 @@ Proof.
- (* mul 2*)
InvApproxRegs; SimplVM; inv H0. apply make_mulimm_correct; auto.
- (* divs *)
- assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
- apply make_divimm_correct; auto.
+ assert (e#r2 = Vint n2). { clear H0. InvApproxRegs; SimplVM; auto. }
+ apply make_divimm_correct; auto.
+ congruence.
- (* divu *)
assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
apply make_divuimm_correct; auto.
+ congruence.
- (* modu *)
assert (e#r2 = Vint n2). clear H0. InvApproxRegs; SimplVM; auto.
apply make_moduimm_correct; auto.
+ congruence.
- (* and 1 *)
rewrite Val.and_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andimm_correct; auto.
- (* and 2 *)
@@ -680,12 +749,15 @@ Proof.
- (* divl *)
assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
apply make_divlimm_correct; auto.
+ congruence.
- (* divlu *)
assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
apply make_divluimm_correct; auto.
+ congruence.
- (* modlu *)
assert (e#r2 = Vlong n2). clear H0. InvApproxRegs; SimplVM; auto.
apply make_modluimm_correct; auto.
+ congruence.
- (* andl 1 *)
rewrite Val.andl_commut in H0. InvApproxRegs; SimplVM; inv H0. apply make_andlimm_correct; auto.
- (* andl 2 *)
diff --git a/riscV/DuplicateOpcodeHeuristic.ml b/riscV/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..38702e1b
--- /dev/null
+++ b/riscV/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* open Camlcoq *)
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccomplimm (c, n) | Ccompluimm (c, n) -> if n == Integers.Int64.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c | Ccompfs c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c | Cnotcompfs c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
diff --git a/riscV/ExpansionOracle.ml b/riscV/ExpansionOracle.ml
new file mode 100644
index 00000000..49ca7e96
--- /dev/null
+++ b/riscV/ExpansionOracle.ml
@@ -0,0 +1,1019 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Léo Gourdin UGA, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+open BTL
+open Op
+open! Integers
+open Camlcoq
+open DebugPrint
+open RTLcommonaux
+open BTLcommonaux
+open AST
+open Datatypes
+open Maps
+open Asmgen
+
+(** Tools *)
+
+let rec iblock_to_list ib =
+ match ib with
+ | Bseq (ib1, ib2) -> iblock_to_list ib1 @ iblock_to_list ib2
+ | _ -> [ ib ]
+
+let rec list_to_iblock lib =
+ match lib with
+ | i1 :: k -> if List.length lib > 1 then Bseq (i1, list_to_iblock k) else i1
+ | [] -> failwith "list_to_iblock: called on empty list"
+
+(** Mini CSE (a dynamic numbering is applied during expansion.
+ The CSE algorithm is inspired by the "static" one used in backend/CSE.v *)
+
+(** Managing virtual registers and node index *)
+
+let reg = ref 1
+
+let r2p () = P.of_int !reg
+
+let r2pi () =
+ reg := !reg + 1;
+ r2p ()
+
+(** Below are the types for rhs and equations *)
+
+type rhs = Sop of operation * int list | Smove
+
+type seq = Seq of int * rhs
+
+(** This is a mini abstraction to have a simpler representation during expansion
+ - (Sr r) is inserted if the value was found in register r
+ - (Sexp dest rhs args iinfo) represent an instruction
+ - (Scond cond args ib1 ib2 iinfo) represents a condition
+*)
+
+type expl =
+ | Sr of P.t
+ | Sexp of P.t * rhs * P.t list * inst_info
+ | Scond of condition * P.t list * iblock * iblock * inst_info
+
+(** Record used during the "dynamic" value numbering *)
+
+type numb = {
+ mutable nnext : int; (** Next unusued value number *)
+ mutable seqs : seq list; (** equations *)
+ mutable nreg : (P.t, int) Hashtbl.t; (** mapping registers to values *)
+ mutable nval : (int, P.t list) Hashtbl.t;
+ (** reverse mapping values to registers containing it *)
+}
+
+let print_list_pos l =
+ debug "[";
+ List.iter (fun i -> debug "%d;" (p2i i)) l;
+ debug "]\n"
+
+let empty_numbering () =
+ { nnext = 1; seqs = []; nreg = Hashtbl.create 100; nval = Hashtbl.create 100 }
+
+let rec get_nvalues vn = function
+ | [] -> []
+ | r :: rs ->
+ let v =
+ match Hashtbl.find_opt !vn.nreg r with
+ | Some v ->
+ debug "getnval r=%d |-> v=%d\n" (p2i r) v;
+ v
+ | None ->
+ let n = !vn.nnext in
+ debug "getnval r=%d |-> v=%d\n" (p2i r) n;
+ !vn.nnext <- !vn.nnext + 1;
+ Hashtbl.replace !vn.nreg r n;
+ Hashtbl.replace !vn.nval n [ r ];
+ n
+ in
+ let vs = get_nvalues vn rs in
+ v :: vs
+
+let get_nval_ornil vn v =
+ match Hashtbl.find_opt !vn.nval v with None -> [] | Some l -> l
+
+let forget_reg vn rd =
+ match Hashtbl.find_opt !vn.nreg rd with
+ | Some v ->
+ debug "forget_reg: r=%d |-> v=%d\n" (p2i rd) v;
+ let old_regs = get_nval_ornil vn v in
+ debug "forget_reg: old_regs are:\n";
+ print_list_pos old_regs;
+ Hashtbl.replace !vn.nval v
+ (List.filter (fun n -> not (P.eq n rd)) old_regs)
+ | None -> debug "forget_reg: no mapping for r=%d\n" (p2i rd)
+
+let update_reg vn rd v =
+ debug "update_reg: update v=%d with r=%d\n" v (p2i rd);
+ forget_reg vn rd;
+ let old_regs = get_nval_ornil vn v in
+ Hashtbl.replace !vn.nval v (rd :: old_regs)
+
+let rec find_valnum_rhs rh = function
+ | [] -> None
+ | Seq (v, rh') :: tl -> if rh = rh' then Some v else find_valnum_rhs rh tl
+
+let set_unknown vn rd =
+ debug "set_unknown: rd=%d\n" (p2i rd);
+ forget_reg vn rd;
+ Hashtbl.remove !vn.nreg rd
+
+let set_res_unknown vn res = match res with BR r -> set_unknown vn r | _ -> ()
+
+let addrhs vn rd rh =
+ match find_valnum_rhs rh !vn.seqs with
+ | Some vres ->
+ debug "addrhs: Some v=%d\n" vres;
+ Hashtbl.replace !vn.nreg rd vres;
+ update_reg vn rd vres
+ | None ->
+ let n = !vn.nnext in
+ debug "addrhs: None v=%d\n" n;
+ !vn.nnext <- !vn.nnext + 1;
+ !vn.seqs <- Seq (n, rh) :: !vn.seqs;
+ update_reg vn rd n;
+ Hashtbl.replace !vn.nreg rd n
+
+let addsop vn v op rd =
+ debug "addsop\n";
+ if op = Omove then (
+ update_reg vn rd (List.hd v);
+ Hashtbl.replace !vn.nreg rd (List.hd v))
+ else addrhs vn rd (Sop (op, v))
+
+let rec kill_mem_operations = function
+ | (Seq (v, Sop (op, vl)) as eq) :: tl ->
+ if op_depends_on_memory op then kill_mem_operations tl
+ else eq :: kill_mem_operations tl
+ | [] -> []
+ | eq :: tl -> eq :: kill_mem_operations tl
+
+let reg_valnum vn v =
+ debug "reg_valnum: trying to find a mapping for v=%d\n" v;
+ match Hashtbl.find !vn.nval v with
+ | [] -> None
+ | r :: rs ->
+ debug "reg_valnum: found a mapping r=%d\n" (p2i r);
+ Some r
+
+let rec reg_valnums vn = function
+ | [] -> Some []
+ | v :: vs -> (
+ match (reg_valnum vn v, reg_valnums vn vs) with
+ | Some r, Some rs -> Some (r :: rs)
+ | _, _ -> None)
+
+let find_rhs vn rh =
+ match find_valnum_rhs rh !vn.seqs with
+ | None -> None
+ | Some vres -> reg_valnum vn vres
+
+(** Functions to perform the dynamic reduction during CSE *)
+
+let extract_arg l =
+ if List.length l > 0 then
+ match List.hd l with
+ | Sr r -> (r, List.tl l)
+ | Sexp (rd, _, _, _) -> (rd, l)
+ | _ -> failwith "extract_arg: final instruction arg can not be extracted"
+ else failwith "extract_arg: trying to extract on an empty list"
+
+let extract_final vn fl fdest =
+ if List.length fl > 0 then
+ match List.hd fl with
+ | Sr r ->
+ if not (P.eq r fdest) then (
+ let v = get_nvalues vn [ r ] in
+ addsop vn v Omove fdest;
+ Sexp (fdest, Smove, [ r ], def_iinfo ()) :: List.tl fl)
+ else List.tl fl
+ | _ -> fl
+ else failwith "extract_final: trying to extract on an empty list"
+
+let addinst vn op args rd =
+ let v = get_nvalues vn args in
+ let rh = Sop (op, v) in
+ match find_rhs vn rh with
+ | Some r ->
+ debug "addinst: rhs found with r=%d\n" (p2i r);
+ Sr r
+ | None ->
+ addsop vn v op rd;
+ Sexp (rd, rh, args, def_iinfo ())
+
+(** Expansion functions *)
+
+type immt =
+ | Addiw
+ | Addil
+ | Andiw
+ | Andil
+ | Oriw
+ | Oril
+ | Xoriw
+ | Xoril
+ | Sltiw
+ | Sltiuw
+ | Sltil
+ | Sltiul
+
+let load_hilo32 vn dest hi lo =
+ let op1 = OEluiw hi in
+ if Int.eq lo Int.zero then [ addinst vn op1 [] dest ]
+ else
+ let r = r2pi () in
+ let op2 = OEaddiw (None, lo) in
+ let i1 = addinst vn op1 [] r in
+ let r', l = extract_arg [ i1 ] in
+ let i2 = addinst vn op2 [ r' ] dest in
+ i2 :: l
+
+let load_hilo64 vn dest hi lo =
+ let op1 = OEluil hi in
+ if Int64.eq lo Int64.zero then [ addinst vn op1 [] dest ]
+ else
+ let r = r2pi () in
+ let op2 = OEaddil (None, lo) in
+ let i1 = addinst vn op1 [] r in
+ let r', l = extract_arg [ i1 ] in
+ let i2 = addinst vn op2 [ r' ] dest in
+ i2 :: l
+
+let loadimm32 vn dest n =
+ match make_immed32 n with
+ | Imm32_single imm ->
+ let op1 = OEaddiw (Some X0_R, imm) in
+ [ addinst vn op1 [] dest ]
+ | Imm32_pair (hi, lo) -> load_hilo32 vn dest hi lo
+
+let loadimm64 vn dest n =
+ match make_immed64 n with
+ | Imm64_single imm ->
+ let op1 = OEaddil (Some X0_R, imm) in
+ [ addinst vn op1 [] dest ]
+ | Imm64_pair (hi, lo) -> load_hilo64 vn dest hi lo
+ | Imm64_large imm ->
+ let op1 = OEloadli imm in
+ [ addinst vn op1 [] dest ]
+
+let get_opimm optR imm = function
+ | Addiw -> OEaddiw (optR, imm)
+ | Andiw -> OEandiw imm
+ | Oriw -> OEoriw imm
+ | Xoriw -> OExoriw imm
+ | Sltiw -> OEsltiw imm
+ | Sltiuw -> OEsltiuw imm
+ | Addil -> OEaddil (optR, imm)
+ | Andil -> OEandil imm
+ | Oril -> OEoril imm
+ | Xoril -> OExoril imm
+ | Sltil -> OEsltil imm
+ | Sltiul -> OEsltiul imm
+
+let opimm32 vn a1 dest n optR op opimm =
+ match make_immed32 n with
+ | Imm32_single imm -> [ addinst vn (get_opimm optR imm opimm) [ a1 ] dest ]
+ | Imm32_pair (hi, lo) ->
+ let r = r2pi () in
+ let l = load_hilo32 vn r hi lo in
+ let r', l' = extract_arg l in
+ let i = addinst vn op [ a1; r' ] dest in
+ i :: l'
+
+let opimm64 vn a1 dest n optR op opimm =
+ match make_immed64 n with
+ | Imm64_single imm -> [ addinst vn (get_opimm optR imm opimm) [ a1 ] dest ]
+ | Imm64_pair (hi, lo) ->
+ let r = r2pi () in
+ let l = load_hilo64 vn r hi lo in
+ let r', l' = extract_arg l in
+ let i = addinst vn op [ a1; r' ] dest in
+ i :: l'
+ | Imm64_large imm ->
+ let r = r2pi () in
+ let op1 = OEloadli imm in
+ let i1 = addinst vn op1 [] r in
+ let r', l' = extract_arg [ i1 ] in
+ let i2 = addinst vn op [ a1; r' ] dest in
+ i2 :: l'
+
+let addimm32 vn a1 dest n optR = opimm32 vn a1 dest n optR Oadd Addiw
+
+let andimm32 vn a1 dest n = opimm32 vn a1 dest n None Oand Andiw
+
+let orimm32 vn a1 dest n = opimm32 vn a1 dest n None Oor Oriw
+
+let xorimm32 vn a1 dest n = opimm32 vn a1 dest n None Oxor Xoriw
+
+let sltimm32 vn a1 dest n = opimm32 vn a1 dest n None (OEsltw None) Sltiw
+
+let sltuimm32 vn a1 dest n = opimm32 vn a1 dest n None (OEsltuw None) Sltiuw
+
+let addimm64 vn a1 dest n optR = opimm64 vn a1 dest n optR Oaddl Addil
+
+let andimm64 vn a1 dest n = opimm64 vn a1 dest n None Oandl Andil
+
+let orimm64 vn a1 dest n = opimm64 vn a1 dest n None Oorl Oril
+
+let xorimm64 vn a1 dest n = opimm64 vn a1 dest n None Oxorl Xoril
+
+let sltimm64 vn a1 dest n = opimm64 vn a1 dest n None (OEsltl None) Sltil
+
+let sltuimm64 vn a1 dest n = opimm64 vn a1 dest n None (OEsltul None) Sltiul
+
+let is_inv_cmp = function Cle | Cgt -> true | _ -> false
+
+let make_optR is_x0 is_inv =
+ if is_x0 then if is_inv then Some X0_L else Some X0_R else None
+
+let cbranch_int32s is_x0 cmp a1 a2 iinfo succ1 succ2 k =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> Scond (CEbeqw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cne -> Scond (CEbnew optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Clt -> Scond (CEbltw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cle -> Scond (CEbgew optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cgt -> Scond (CEbltw optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cge -> Scond (CEbgew optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+
+let cbranch_int32u is_x0 cmp a1 a2 iinfo succ1 succ2 k =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> Scond (CEbequw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cne -> Scond (CEbneuw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Clt -> Scond (CEbltuw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cle -> Scond (CEbgeuw optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cgt -> Scond (CEbltuw optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cge -> Scond (CEbgeuw optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+
+let cbranch_int64s is_x0 cmp a1 a2 iinfo succ1 succ2 k =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> Scond (CEbeql optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cne -> Scond (CEbnel optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Clt -> Scond (CEbltl optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cle -> Scond (CEbgel optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cgt -> Scond (CEbltl optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cge -> Scond (CEbgel optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+
+let cbranch_int64u is_x0 cmp a1 a2 iinfo succ1 succ2 k =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> Scond (CEbequl optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cne -> Scond (CEbneul optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Clt -> Scond (CEbltul optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+ | Cle -> Scond (CEbgeul optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cgt -> Scond (CEbltul optR, [ a2; a1 ], succ1, succ2, iinfo) :: k
+ | Cge -> Scond (CEbgeul optR, [ a1; a2 ], succ1, succ2, iinfo) :: k
+
+let cond_int32s vn is_x0 cmp a1 a2 dest =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> [ addinst vn (OEseqw optR) [ a1; a2 ] dest ]
+ | Cne -> [ addinst vn (OEsnew optR) [ a1; a2 ] dest ]
+ | Clt -> [ addinst vn (OEsltw optR) [ a1; a2 ] dest ]
+ | Cle ->
+ let r = r2pi () in
+ let op = OEsltw optR in
+ let i1 = addinst vn op [ a2; a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+ | Cgt -> [ addinst vn (OEsltw optR) [ a2; a1 ] dest ]
+ | Cge ->
+ let r = r2pi () in
+ let op = OEsltw optR in
+ let i1 = addinst vn op [ a1; a2 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+
+let cond_int32u vn is_x0 cmp a1 a2 dest =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> [ addinst vn (OEsequw optR) [ a1; a2 ] dest ]
+ | Cne -> [ addinst vn (OEsneuw optR) [ a1; a2 ] dest ]
+ | Clt -> [ addinst vn (OEsltuw optR) [ a1; a2 ] dest ]
+ | Cle ->
+ let r = r2pi () in
+ let op = OEsltuw optR in
+ let i1 = addinst vn op [ a2; a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+ | Cgt -> [ addinst vn (OEsltuw optR) [ a2; a1 ] dest ]
+ | Cge ->
+ let r = r2pi () in
+ let op = OEsltuw optR in
+ let i1 = addinst vn op [ a1; a2 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+
+let cond_int64s vn is_x0 cmp a1 a2 dest =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> [ addinst vn (OEseql optR) [ a1; a2 ] dest ]
+ | Cne -> [ addinst vn (OEsnel optR) [ a1; a2 ] dest ]
+ | Clt -> [ addinst vn (OEsltl optR) [ a1; a2 ] dest ]
+ | Cle ->
+ let r = r2pi () in
+ let op = OEsltl optR in
+ let i1 = addinst vn op [ a2; a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+ | Cgt -> [ addinst vn (OEsltl optR) [ a2; a1 ] dest ]
+ | Cge ->
+ let r = r2pi () in
+ let op = OEsltl optR in
+ let i1 = addinst vn op [ a1; a2 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+
+let cond_int64u vn is_x0 cmp a1 a2 dest =
+ let optR = make_optR is_x0 (is_inv_cmp cmp) in
+ match cmp with
+ | Ceq -> [ addinst vn (OEsequl optR) [ a1; a2 ] dest ]
+ | Cne -> [ addinst vn (OEsneul optR) [ a1; a2 ] dest ]
+ | Clt -> [ addinst vn (OEsltul optR) [ a1; a2 ] dest ]
+ | Cle ->
+ let r = r2pi () in
+ let op = OEsltul optR in
+ let i1 = addinst vn op [ a2; a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+ | Cgt -> [ addinst vn (OEsltul optR) [ a2; a1 ] dest ]
+ | Cge ->
+ let r = r2pi () in
+ let op = OEsltul optR in
+ let i1 = addinst vn op [ a1; a2 ] r in
+ let r', l = extract_arg [ i1 ] in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+
+let is_normal_cmp = function Cne -> false | _ -> true
+
+let cond_float vn cmp f1 f2 dest =
+ match cmp with
+ | Ceq -> [ addinst vn OEfeqd [ f1; f2 ] dest ]
+ | Cne -> [ addinst vn OEfeqd [ f1; f2 ] dest ]
+ | Clt -> [ addinst vn OEfltd [ f1; f2 ] dest ]
+ | Cle -> [ addinst vn OEfled [ f1; f2 ] dest ]
+ | Cgt -> [ addinst vn OEfltd [ f2; f1 ] dest ]
+ | Cge -> [ addinst vn OEfled [ f2; f1 ] dest ]
+
+let cond_single vn cmp f1 f2 dest =
+ match cmp with
+ | Ceq -> [ addinst vn OEfeqs [ f1; f2 ] dest ]
+ | Cne -> [ addinst vn OEfeqs [ f1; f2 ] dest ]
+ | Clt -> [ addinst vn OEflts [ f1; f2 ] dest ]
+ | Cle -> [ addinst vn OEfles [ f1; f2 ] dest ]
+ | Cgt -> [ addinst vn OEflts [ f2; f1 ] dest ]
+ | Cge -> [ addinst vn OEfles [ f2; f1 ] dest ]
+
+let expanse_cbranchimm_int32s vn cmp a1 n iinfo succ1 succ2 =
+ if Int.eq n Int.zero then cbranch_int32s true cmp a1 a1 iinfo succ1 succ2 []
+ else
+ let r = r2pi () in
+ let l = loadimm32 vn r n in
+ let r', l' = extract_arg l in
+ cbranch_int32s false cmp a1 r' iinfo succ1 succ2 l'
+
+let expanse_cbranchimm_int32u vn cmp a1 n iinfo succ1 succ2 =
+ if Int.eq n Int.zero then cbranch_int32u true cmp a1 a1 iinfo succ1 succ2 []
+ else
+ let r = r2pi () in
+ let l = loadimm32 vn r n in
+ let r', l' = extract_arg l in
+ cbranch_int32u false cmp a1 r' iinfo succ1 succ2 l'
+
+let expanse_cbranchimm_int64s vn cmp a1 n iinfo succ1 succ2 =
+ if Int64.eq n Int64.zero then
+ cbranch_int64s true cmp a1 a1 iinfo succ1 succ2 []
+ else
+ let r = r2pi () in
+ let l = loadimm64 vn r n in
+ let r', l' = extract_arg l in
+ cbranch_int64s false cmp a1 r' iinfo succ1 succ2 l'
+
+let expanse_cbranchimm_int64u vn cmp a1 n iinfo succ1 succ2 =
+ if Int64.eq n Int64.zero then
+ cbranch_int64u true cmp a1 a1 iinfo succ1 succ2 []
+ else
+ let r = r2pi () in
+ let l = loadimm64 vn r n in
+ let r', l' = extract_arg l in
+ cbranch_int64u false cmp a1 r' iinfo succ1 succ2 l'
+
+let expanse_condimm_int32s vn cmp a1 n dest =
+ if Int.eq n Int.zero then cond_int32s vn true cmp a1 a1 dest
+ else
+ match cmp with
+ | Ceq | Cne ->
+ let r = r2pi () in
+ let l = xorimm32 vn a1 r n in
+ let r', l' = extract_arg l in
+ cond_int32s vn true cmp r' r' dest @ l'
+ | Clt -> sltimm32 vn a1 dest n
+ | Cle ->
+ if Int.eq n (Int.repr Int.max_signed) then
+ let l = loadimm32 vn dest Int.one in
+ let r, l' = extract_arg l in
+ addinst vn (OEmayundef MUint) [ a1; r ] dest :: l'
+ else sltimm32 vn a1 dest (Int.add n Int.one)
+ | _ ->
+ let r = r2pi () in
+ let l = loadimm32 vn r n in
+ let r', l' = extract_arg l in
+ cond_int32s vn false cmp a1 r' dest @ l'
+
+let expanse_condimm_int32u vn cmp a1 n dest =
+ if Int.eq n Int.zero then cond_int32u vn true cmp a1 a1 dest
+ else
+ match cmp with
+ | Clt -> sltuimm32 vn a1 dest n
+ | _ ->
+ let r = r2pi () in
+ let l = loadimm32 vn r n in
+ let r', l' = extract_arg l in
+ cond_int32u vn false cmp a1 r' dest @ l'
+
+let expanse_condimm_int64s vn cmp a1 n dest =
+ if Int64.eq n Int64.zero then cond_int64s vn true cmp a1 a1 dest
+ else
+ match cmp with
+ | Ceq | Cne ->
+ let r = r2pi () in
+ let l = xorimm64 vn a1 r n in
+ let r', l' = extract_arg l in
+ cond_int64s vn true cmp r' r' dest @ l'
+ | Clt -> sltimm64 vn a1 dest n
+ | Cle ->
+ if Int64.eq n (Int64.repr Int64.max_signed) then
+ let l = loadimm32 vn dest Int.one in
+ let r, l' = extract_arg l in
+ addinst vn (OEmayundef MUlong) [ a1; r ] dest :: l'
+ else sltimm64 vn a1 dest (Int64.add n Int64.one)
+ | _ ->
+ let r = r2pi () in
+ let l = loadimm64 vn r n in
+ let r', l' = extract_arg l in
+ cond_int64s vn false cmp a1 r' dest @ l'
+
+let expanse_condimm_int64u vn cmp a1 n dest =
+ if Int64.eq n Int64.zero then cond_int64u vn true cmp a1 a1 dest
+ else
+ match cmp with
+ | Clt -> sltuimm64 vn a1 dest n
+ | _ ->
+ let r = r2pi () in
+ let l = loadimm64 vn r n in
+ let r', l' = extract_arg l in
+ cond_int64u vn false cmp a1 r' dest @ l'
+
+let expanse_cond_fp vn cnot fn_cond cmp f1 f2 dest =
+ let normal = is_normal_cmp cmp in
+ let normal' = if cnot then not normal else normal in
+ let insn = fn_cond vn cmp f1 f2 dest in
+ if normal' then insn
+ else
+ let r', l = extract_arg insn in
+ addinst vn (OExoriw Int.one) [ r' ] dest :: l
+
+let expanse_cbranch_fp vn cnot fn_cond cmp f1 f2 iinfo succ1 succ2 =
+ let r = r2pi () in
+ let normal = is_normal_cmp cmp in
+ let normal' = if cnot then not normal else normal in
+ let insn = fn_cond vn cmp f1 f2 r in
+ let r', l = extract_arg insn in
+ if normal' then
+ Scond (CEbnew (Some X0_R), [ r'; r' ], succ1, succ2, iinfo) :: l
+ else Scond (CEbeqw (Some X0_R), [ r'; r' ], succ1, succ2, iinfo) :: l
+
+(** Return olds args if the CSE numbering is empty *)
+
+let get_arguments vn vals args =
+ match reg_valnums vn vals with Some args' -> args' | None -> args
+
+let rec gen_btl_list vn exp =
+ match exp with
+ | Sr r :: _ ->
+ failwith "write_tree: there are still some symbolic values in the list"
+ | Sexp (rd, Sop (op, vals), args, iinfo) :: k ->
+ let args = get_arguments vn vals args in
+ let inst = Bop (op, args, rd, iinfo) in
+ inst :: gen_btl_list vn k
+ | [ Sexp (rd, Smove, args, iinfo) ] -> [ Bop (Omove, args, rd, iinfo) ]
+ | [ Scond (cond, args, succ1, succ2, iinfo) ] ->
+ let ib = Bcond (cond, args, succ1, succ2, iinfo) in
+ [ ib ]
+ | [] -> []
+ | _ -> failwith "write_tree: invalid list"
+
+let expanse_list li =
+ debug "#### New block for expansion oracle\n";
+ let exp = ref [] in
+ let was_branch = ref false in
+ let was_exp = ref false in
+ let vn = ref (empty_numbering ()) in
+ let rec expanse_list_rec li =
+ match li with
+ | [] -> li
+ | i :: li' ->
+ was_branch := false;
+ was_exp := false;
+ (if !Clflags.option_fexpanse_rtlcond then
+ match i with
+ (* Expansion of conditions - Ocmp *)
+ | Bop (Ocmp (Ccomp c), a1 :: a2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccomp\n";
+ exp := cond_int32s vn false c a1 a2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompu c), a1 :: a2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompu\n";
+ exp := cond_int32u vn false c a1 a2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompimm (c, imm)), a1 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompimm\n";
+ exp := expanse_condimm_int32s vn c a1 imm dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompuimm (c, imm)), a1 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompuimm\n";
+ exp := expanse_condimm_int32u vn c a1 imm dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompl c), a1 :: a2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompl\n";
+ exp := cond_int64s vn false c a1 a2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccomplu c), a1 :: a2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccomplu\n";
+ exp := cond_int64u vn false c a1 a2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccomplimm (c, imm)), a1 :: nil, dest, iinfo) ->
+ debug "Bop/Ccomplimm\n";
+ exp := expanse_condimm_int64s vn c a1 imm dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompluimm (c, imm)), a1 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompluimm\n";
+ exp := expanse_condimm_int64u vn c a1 imm dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompf c), f1 :: f2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompf\n";
+ exp := expanse_cond_fp vn false cond_float c f1 f2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Cnotcompf c), f1 :: f2 :: nil, dest, iinfo) ->
+ debug "Bop/Cnotcompf\n";
+ exp := expanse_cond_fp vn true cond_float c f1 f2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Ccompfs c), f1 :: f2 :: nil, dest, iinfo) ->
+ debug "Bop/Ccompfs\n";
+ exp := expanse_cond_fp vn false cond_single c f1 f2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocmp (Cnotcompfs c), f1 :: f2 :: nil, dest, iinfo) ->
+ debug "Bop/Cnotcompfs\n";
+ exp := expanse_cond_fp vn true cond_single c f1 f2 dest;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ (* Expansion of branches - Ccomp *)
+ | Bcond (Ccomp c, a1 :: a2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccomp\n";
+ exp := cbranch_int32s false c a1 a2 iinfo succ1 succ2 [];
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompu c, a1 :: a2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompu\n";
+ exp := cbranch_int32u false c a1 a2 iinfo succ1 succ2 [];
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompimm (c, imm), a1 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompimm\n";
+ exp := expanse_cbranchimm_int32s vn c a1 imm iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompuimm (c, imm), a1 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompuimm\n";
+ exp := expanse_cbranchimm_int32u vn c a1 imm iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompl c, a1 :: a2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompl\n";
+ exp := cbranch_int64s false c a1 a2 iinfo succ1 succ2 [];
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccomplu c, a1 :: a2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccomplu\n";
+ exp := cbranch_int64u false c a1 a2 iinfo succ1 succ2 [];
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccomplimm (c, imm), a1 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccomplimm\n";
+ exp := expanse_cbranchimm_int64s vn c a1 imm iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompluimm (c, imm), a1 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompluimm\n";
+ exp := expanse_cbranchimm_int64u vn c a1 imm iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompf c, f1 :: f2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompf\n";
+ exp :=
+ expanse_cbranch_fp vn false cond_float c f1 f2 iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Cnotcompf c, f1 :: f2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Cnotcompf\n";
+ exp :=
+ expanse_cbranch_fp vn true cond_float c f1 f2 iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Ccompfs c, f1 :: f2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Ccompfs\n";
+ exp :=
+ expanse_cbranch_fp vn false cond_single c f1 f2 iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | Bcond (Cnotcompfs c, f1 :: f2 :: nil, succ1, succ2, iinfo) ->
+ debug "Bcond/Cnotcompfs\n";
+ exp :=
+ expanse_cbranch_fp vn true cond_single c f1 f2 iinfo succ1 succ2;
+ was_branch := true;
+ was_exp := true
+ | _ -> ());
+ (if !Clflags.option_fexpanse_others && not !was_exp then
+ match i with
+ (* Others expansions *)
+ | Bop (Ofloatconst f, nil, dest, iinfo) -> (
+ match make_immed64 (Floats.Float.to_bits f) with
+ | Imm64_single _ | Imm64_large _ -> ()
+ | Imm64_pair (hi, lo) ->
+ debug "Bop/Ofloatconst\n";
+ let r = r2pi () in
+ let l = load_hilo64 vn r hi lo in
+ let r', l' = extract_arg l in
+ exp := addinst vn Ofloat_of_bits [ r' ] dest :: l';
+ exp := extract_final vn !exp dest;
+ was_exp := true)
+ | Bop (Osingleconst f, nil, dest, iinfo) -> (
+ match make_immed32 (Floats.Float32.to_bits f) with
+ | Imm32_single imm -> ()
+ | Imm32_pair (hi, lo) ->
+ debug "Bop/Osingleconst\n";
+ let r = r2pi () in
+ let l = load_hilo32 vn r hi lo in
+ let r', l' = extract_arg l in
+ exp := addinst vn Osingle_of_bits [ r' ] dest :: l';
+ exp := extract_final vn !exp dest;
+ was_exp := true)
+ | Bop (Ointconst n, nil, dest, iinfo) ->
+ debug "Bop/Ointconst\n";
+ exp := loadimm32 vn dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Olongconst n, nil, dest, iinfo) ->
+ debug "Bop/Olongconst\n";
+ exp := loadimm64 vn dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oaddimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oaddimm\n";
+ exp := addimm32 vn a1 dest n None;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oaddlimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oaddlimm\n";
+ exp := addimm64 vn a1 dest n None;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oandimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oandimm\n";
+ exp := andimm32 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oandlimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oandlimm\n";
+ exp := andimm64 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oorimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oorimm\n";
+ exp := orimm32 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oorlimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oorlimm\n";
+ exp := orimm64 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oxorimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oxorimm\n";
+ exp := xorimm32 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oxorlimm n, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Oxorlimm\n";
+ exp := xorimm64 vn a1 dest n;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocast8signed, a1 :: nil, dest, iinfo) ->
+ debug "Bop/cast8signed\n";
+ let op = Oshlimm (Int.repr (Z.of_sint 24)) in
+ let r = r2pi () in
+ let i1 = addinst vn op [ a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ exp :=
+ addinst vn (Oshrimm (Int.repr (Z.of_sint 24))) [ r' ] dest :: l;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocast16signed, a1 :: nil, dest, iinfo) ->
+ debug "Bop/cast16signed\n";
+ let op = Oshlimm (Int.repr (Z.of_sint 16)) in
+ let r = r2pi () in
+ let i1 = addinst vn op [ a1 ] r in
+ let r', l = extract_arg [ i1 ] in
+ exp :=
+ addinst vn (Oshrimm (Int.repr (Z.of_sint 16))) [ r' ] dest :: l;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Ocast32unsigned, a1 :: nil, dest, iinfo) ->
+ debug "Bop/Ocast32unsigned\n";
+ let r1 = r2pi () in
+ let r2 = r2pi () in
+ let op1 = Ocast32signed in
+ let i1 = addinst vn op1 [ a1 ] r1 in
+ let r1', l1 = extract_arg [ i1 ] in
+
+ let op2 = Oshllimm (Int.repr (Z.of_sint 32)) in
+ let i2 = addinst vn op2 [ r1' ] r2 in
+ let r2', l2 = extract_arg (i2 :: l1) in
+
+ let op3 = Oshrluimm (Int.repr (Z.of_sint 32)) in
+ exp := addinst vn op3 [ r2' ] dest :: l2;
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oshrximm n, a1 :: nil, dest, iinfo) ->
+ if Int.eq n Int.zero then (
+ debug "Bop/Oshrximm1\n";
+ exp := [ addinst vn (OEmayundef (MUshrx n)) [ a1; a1 ] dest ])
+ else if Int.eq n Int.one then (
+ debug "Bop/Oshrximm2\n";
+ let r1 = r2pi () in
+ let r2 = r2pi () in
+ let op1 = Oshruimm (Int.repr (Z.of_sint 31)) in
+ let i1 = addinst vn op1 [ a1 ] r1 in
+ let r1', l1 = extract_arg [ i1 ] in
+
+ let op2 = Oadd in
+ let i2 = addinst vn op2 [ a1; r1' ] r2 in
+ let r2', l2 = extract_arg (i2 :: l1) in
+
+ let op3 = Oshrimm Int.one in
+ let i3 = addinst vn op3 [ r2' ] dest in
+ let r3, l3 = extract_arg (i3 :: l2) in
+ exp := addinst vn (OEmayundef (MUshrx n)) [ r3; r3 ] dest :: l3)
+ else (
+ debug "Bop/Oshrximm3\n";
+ let r1 = r2pi () in
+ let r2 = r2pi () in
+ let r3 = r2pi () in
+ let op1 = Oshrimm (Int.repr (Z.of_sint 31)) in
+ let i1 = addinst vn op1 [ a1 ] r1 in
+ let r1', l1 = extract_arg [ i1 ] in
+
+ let op2 = Oshruimm (Int.sub Int.iwordsize n) in
+ let i2 = addinst vn op2 [ r1' ] r2 in
+ let r2', l2 = extract_arg (i2 :: l1) in
+
+ let op3 = Oadd in
+ let i3 = addinst vn op3 [ a1; r2' ] r3 in
+ let r3', l3 = extract_arg (i3 :: l2) in
+
+ let op4 = Oshrimm n in
+ let i4 = addinst vn op4 [ r3' ] dest in
+ let r4, l4 = extract_arg (i4 :: l3) in
+ exp := addinst vn (OEmayundef (MUshrx n)) [ r4; r4 ] dest :: l4);
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | Bop (Oshrxlimm n, a1 :: nil, dest, iinfo) ->
+ if Int.eq n Int.zero then (
+ debug "Bop/Oshrxlimm1\n";
+ exp := [ addinst vn (OEmayundef (MUshrxl n)) [ a1; a1 ] dest ])
+ else if Int.eq n Int.one then (
+ debug "Bop/Oshrxlimm2\n";
+ let r1 = r2pi () in
+ let r2 = r2pi () in
+ let op1 = Oshrluimm (Int.repr (Z.of_sint 63)) in
+ let i1 = addinst vn op1 [ a1 ] r1 in
+ let r1', l1 = extract_arg [ i1 ] in
+
+ let op2 = Oaddl in
+ let i2 = addinst vn op2 [ a1; r1' ] r2 in
+ let r2', l2 = extract_arg (i2 :: l1) in
+
+ let op3 = Oshrlimm Int.one in
+ let i3 = addinst vn op3 [ r2' ] dest in
+ let r3, l3 = extract_arg (i3 :: l2) in
+ exp := addinst vn (OEmayundef (MUshrxl n)) [ r3; r3 ] dest :: l3)
+ else (
+ debug "Bop/Oshrxlimm3\n";
+ let r1 = r2pi () in
+ let r2 = r2pi () in
+ let r3 = r2pi () in
+ let op1 = Oshrlimm (Int.repr (Z.of_sint 63)) in
+ let i1 = addinst vn op1 [ a1 ] r1 in
+ let r1', l1 = extract_arg [ i1 ] in
+
+ let op2 = Oshrluimm (Int.sub Int64.iwordsize' n) in
+ let i2 = addinst vn op2 [ r1' ] r2 in
+ let r2', l2 = extract_arg (i2 :: l1) in
+
+ let op3 = Oaddl in
+ let i3 = addinst vn op3 [ a1; r2' ] r3 in
+ let r3', l3 = extract_arg (i3 :: l2) in
+
+ let op4 = Oshrlimm n in
+ let i4 = addinst vn op4 [ r3' ] dest in
+ let r4, l4 = extract_arg (i4 :: l3) in
+ exp := addinst vn (OEmayundef (MUshrxl n)) [ r4; r4 ] dest :: l4);
+ exp := extract_final vn !exp dest;
+ was_exp := true
+ | _ -> ());
+ if not !was_exp then (
+ (match i with
+ | Bop (op, args, dest, iinfo) ->
+ let v = get_nvalues vn args in
+ addsop vn v op dest
+ | Bload (_, _, _, _, dst, _) -> set_unknown vn dst
+ | Bstore (_, _, _, _, _) ->
+ !vn.seqs <- kill_mem_operations !vn.seqs
+ (* TODO gourdinl empty numb BF? vn := empty_numbering ()*)
+ | _ -> ());
+ i :: expanse_list_rec li')
+ else
+ let hd = gen_btl_list vn (List.rev !exp) in
+ hd @ expanse_list_rec li'
+ in
+ expanse_list_rec li
+
+let expanse n ibf btl =
+ (*debug_flag := true;*)
+ let lib = iblock_to_list ibf.entry in
+ let new_lib = expanse_list lib in
+ let ibf' =
+ {
+ entry = list_to_iblock new_lib;
+ input_regs = ibf.input_regs;
+ binfo = ibf.binfo;
+ }
+ in
+ (*debug_flag := false;*)
+ PTree.set n ibf' btl
+
+(** Form a list containing both sources and destination regs of a block *)
+let get_regindent = function Coq_inr _ -> [] | Coq_inl r -> [ r ]
+
+let rec get_regs_ib = function
+ | Bnop _ -> []
+ | Bop (_, args, dest, _) -> dest :: args
+ | Bload (_, _, _, args, dest, _) -> dest :: args
+ | Bstore (_, _, args, src, _) -> src :: args
+ | Bcond (_, args, ib1, ib2, _) -> get_regs_ib ib1 @ get_regs_ib ib2 @ args
+ | Bseq (ib1, ib2) -> get_regs_ib ib1 @ get_regs_ib ib2
+ | BF (Breturn (Some r), _) -> [ r ]
+ | BF (Bcall (_, t, args, dest, _), _) -> dest :: (get_regindent t @ args)
+ | BF (Btailcall (_, t, args), _) -> get_regindent t @ args
+ | BF (Bbuiltin (_, args, dest, _), _) ->
+ AST.params_of_builtin_res dest @ AST.params_of_builtin_args args
+ | BF (Bjumptable (arg, _), _) -> [ arg ]
+ | _ -> []
+
+let rec find_last_reg = function
+ | [] -> ()
+ | (pc, ibf) :: k ->
+ let rec traverse_list var = function
+ | [] -> ()
+ | e :: t ->
+ let e' = p2i e in
+ if e' > !var then var := e';
+ traverse_list var t
+ in
+ traverse_list reg (get_regs_ib ibf.entry);
+ find_last_reg k
diff --git a/riscV/ExtValues.v b/riscV/ExtValues.v
new file mode 100644
index 00000000..edf359ef
--- /dev/null
+++ b/riscV/ExtValues.v
@@ -0,0 +1,123 @@
+Require Import Coqlib.
+Require Import Integers.
+Require Import Values.
+Require Import Floats.
+Require Import Memory.
+Require Import Lia.
+
+Definition bits_of_float x :=
+ match x with
+ | Vfloat f => Vlong (Float.to_bits f)
+ | _ => Vundef
+ end.
+
+Definition bits_of_single x :=
+ match x with
+ | Vsingle f => Vint (Float32.to_bits f)
+ | _ => Vundef
+ end.
+
+Definition float_of_bits x :=
+ match x with
+ | Vlong f => Vfloat (Float.of_bits f)
+ | _ => Vundef
+ end.
+
+Definition single_of_bits x :=
+ match x with
+ | Vint f => Vsingle (Float32.of_bits f)
+ | _ => Vundef
+ end.
+
+Definition bitwise_select_long b vtrue vfalse :=
+ Int64.or (Int64.and (Int64.neg b) vtrue)
+ (Int64.and (Int64.sub b Int64.one) vfalse).
+
+Lemma bitwise_select_long_true :
+ forall vtrue vfalse,
+ bitwise_select_long Int64.one vtrue vfalse = vtrue.
+Proof.
+ intros. unfold bitwise_select_long. cbn.
+ change (Int64.neg Int64.one) with Int64.mone.
+ rewrite Int64.and_commut.
+ rewrite Int64.and_mone.
+ rewrite Int64.sub_idem.
+ rewrite Int64.and_commut.
+ rewrite Int64.and_zero.
+ apply Int64.or_zero.
+Qed.
+
+Lemma bitwise_select_long_false :
+ forall vtrue vfalse,
+ bitwise_select_long Int64.zero vtrue vfalse = vfalse.
+Proof.
+ intros. unfold bitwise_select_long. cbn.
+ rewrite Int64.neg_zero.
+ rewrite Int64.and_commut.
+ rewrite Int64.and_zero.
+ rewrite Int64.sub_zero_r.
+ change (Int64.neg Int64.one) with Int64.mone.
+ rewrite Int64.and_commut.
+ rewrite Int64.and_mone.
+ rewrite Int64.or_commut.
+ apply Int64.or_zero.
+Qed.
+
+Definition select01_long (vb : val) (vtrue : val) (vfalse : val) : val :=
+ match vb with
+ | (Vint b) =>
+ if Int.eq b Int.one
+ then vtrue
+ else if Int.eq b Int.zero
+ then vfalse
+ else Vundef
+ | _ => Vundef
+ end.
+
+Lemma normalize_select01:
+ forall x y z, Val.normalize (select01_long x y z) AST.Tlong = select01_long x (Val.normalize y AST.Tlong) (Val.normalize z AST.Tlong).
+Proof.
+ unfold select01_long.
+ intros.
+ destruct x; cbn; trivial.
+ destruct (Int.eq i Int.one); trivial.
+ destruct (Int.eq i Int.zero); trivial.
+Qed.
+
+Lemma select01_long_true:
+ forall vt vf,
+ select01_long Vtrue vt vf = vt.
+Proof.
+ intros. unfold select01_long. cbn.
+ rewrite Int.eq_true. reflexivity.
+Qed.
+
+Lemma select01_long_false:
+ forall vt vf,
+ select01_long Vfalse vt vf = vf.
+Proof.
+ intros. unfold select01_long. cbn.
+ rewrite Int.eq_true.
+ rewrite Int.eq_false. reflexivity.
+ cbv. discriminate.
+Qed.
+
+Lemma float_bits_normalize:
+ forall v1,
+ ExtValues.float_of_bits (Val.normalize (ExtValues.bits_of_float v1) AST.Tlong) =
+ Val.normalize v1 AST.Tfloat.
+Proof.
+ destruct v1; cbn; trivial.
+ f_equal.
+ apply Float.of_to_bits.
+Qed.
+
+Lemma single_bits_normalize:
+ forall v1,
+ ExtValues.single_of_bits (Val.normalize (ExtValues.bits_of_single v1) AST.Tint) =
+ Val.normalize v1 AST.Tsingle.
+Proof.
+ destruct v1; cbn; trivial.
+ f_equal.
+ apply Float32.of_to_bits.
+Qed.
diff --git a/riscV/Machregsaux.ml b/riscV/Machregsaux.ml
index a48749a5..e3e47946 100644
--- a/riscV/Machregsaux.ml
+++ b/riscV/Machregsaux.ml
@@ -13,3 +13,10 @@
(** Auxiliary functions on machine registers *)
let is_scratch_register r = false
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong -> 0
+ | AST.Tfloat | AST.Tsingle -> 1
+ | AST.Tany32 | AST.Tany64 -> assert false
+
+let nr_regs = [| 26; 32|]
diff --git a/riscV/Machregsaux.mli b/riscV/Machregsaux.mli
index f3d52849..bb3777bf 100644
--- a/riscV/Machregsaux.mli
+++ b/riscV/Machregsaux.mli
@@ -13,3 +13,8 @@
(** Auxiliary functions on machine registers *)
val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
+
+(* Number of registers in each class *)
+val nr_regs : int array
diff --git a/riscV/NeedOp.v b/riscV/NeedOp.v
index 4070431a..6041a34d 100644
--- a/riscV/NeedOp.v
+++ b/riscV/NeedOp.v
@@ -87,6 +87,45 @@ Definition needs_of_operation (op: operation) (nv: nval): list nval :=
| Ointofsingle | Ointuofsingle | Osingleofint | Osingleofintu => op1 (default nv)
| Olongofsingle | Olonguofsingle | Osingleoflong | Osingleoflongu => op1 (default nv)
| Ocmp c => needs_of_condition c
+ | OEseqw _ => op2 (default nv)
+ | OEsnew _ => op2 (default nv)
+ | OEsequw _ => op2 (default nv)
+ | OEsneuw _ => op2 (default nv)
+ | OEsltw _ => op2 (default nv)
+ | OEsltuw _ => op2 (default nv)
+ | OEsltiw _ => op1 (default nv)
+ | OEsltiuw _ => op1 (default nv)
+ | OExoriw _ => op1 (bitwise nv)
+ | OEluiw _ => op1 (default nv)
+ | OEaddiw _ _ => op1 (default nv)
+ | OEandiw n => op1 (andimm nv n)
+ | OEoriw n => op1 (orimm nv n)
+ | OEseql _ => op2 (default nv)
+ | OEsnel _ => op2 (default nv)
+ | OEsequl _ => op2 (default nv)
+ | OEsneul _ => op2 (default nv)
+ | OEsltl _ => op2 (default nv)
+ | OEsltul _ => op2 (default nv)
+ | OEsltil _ => op1 (default nv)
+ | OEsltiul _ => op1 (default nv)
+ | OExoril _ => op1 (default nv)
+ | OEluil _ => op1 (default nv)
+ | OEaddil _ _ => op1 (default nv)
+ | OEandil _ => op1 (default nv)
+ | OEoril _ => op1 (default nv)
+ | OEloadli _ => op1 (default nv)
+ | OEmayundef _ => op2 (default nv)
+ | OEfeqd => op2 (default nv)
+ | OEfltd => op2 (default nv)
+ | OEfled => op2 (default nv)
+ | OEfeqs => op2 (default nv)
+ | OEflts => op2 (default nv)
+ | OEfles => op2 (default nv)
+ | Obits_of_single => op1 (default nv)
+ | Obits_of_float => op1 (default nv)
+ | Osingle_of_bits => op1 (default nv)
+ | Ofloat_of_bits => op1 (default nv)
+ | Oselectl => All :: nv :: nv :: nil
end.
Definition operation_is_redundant (op: operation) (nv: nval): bool :=
@@ -154,6 +193,27 @@ Proof.
- apply shlimm_sound; auto.
- apply shrimm_sound; auto.
- apply shruimm_sound; auto.
+- fold (Val.and (Vint n) v0);
+ fold (Val.and (Vint n) v2);
+ rewrite (Val.and_commut (Vint n) v0);
+ rewrite (Val.and_commut (Vint n) v2);
+ apply andimm_sound; auto.
+- fold (Val.or (Vint n) v0);
+ fold (Val.or (Vint n) v2);
+ rewrite (Val.or_commut (Vint n) v0);
+ rewrite (Val.or_commut (Vint n) v2);
+ apply orimm_sound; auto.
+- apply xor_sound; auto with na.
+- (* selectl *)
+ unfold ExtValues.select01_long.
+ destruct v0; auto with na.
+ assert (Val.lessdef (Vint i) v4) as LESSDEF by auto with na.
+ inv LESSDEF.
+ destruct (Int.eq i Int.one).
+ { apply normalize_sound; auto. }
+ destruct (Int.eq i Int.zero).
+ { apply normalize_sound; auto. }
+ cbn. auto with na.
Qed.
Lemma operation_is_redundant_sound:
diff --git a/riscV/Op.v b/riscV/Op.v
index bb04f786..9f94828f 100644
--- a/riscV/Op.v
+++ b/riscV/Op.v
@@ -32,11 +32,18 @@
Require Import BoolEqual Coqlib.
Require Import AST Integers Floats.
Require Import Values Memory Globalenvs Events.
+Require ExtValues.
Set Implicit Arguments.
(** Conditions (boolean-valued operators). *)
+(** Type to modelize the use of a special register in arith operations *)
+
+Inductive oreg: Type :=
+ | X0_L: oreg
+ | X0_R: oreg.
+
Inductive condition : Type :=
| Ccomp (c: comparison) (**r signed integer comparison *)
| Ccompu (c: comparison) (**r unsigned integer comparison *)
@@ -49,7 +56,32 @@ Inductive condition : Type :=
| Ccompf (c: comparison) (**r 64-bit floating-point comparison *)
| Cnotcompf (c: comparison) (**r negation of a floating-point comparison *)
| Ccompfs (c: comparison) (**r 32-bit floating-point comparison *)
- | Cnotcompfs (c: comparison). (**r negation of a floating-point comparison *)
+ | Cnotcompfs (c: comparison) (**r negation of a floating-point comparison *)
+ (* Expansed branches *)
+ | CEbeqw (optR: option oreg) (**r branch-if-equal signed *)
+ | CEbnew (optR: option oreg) (**r branch-if-not-equal signed *)
+ | CEbequw (optR: option oreg) (**r branch-if-equal unsigned *)
+ | CEbneuw (optR: option oreg) (**r branch-if-not-equal unsigned *)
+ | CEbltw (optR: option oreg) (**r branch-if-less signed *)
+ | CEbltuw (optR: option oreg) (**r branch-if-less unsigned *)
+ | CEbgew (optR: option oreg) (**r branch-if-greater-or-equal signed *)
+ | CEbgeuw (optR: option oreg) (**r branch-if-greater-or-equal unsigned *)
+ | CEbeql (optR: option oreg) (**r branch-if-equal signed *)
+ | CEbnel (optR: option oreg) (**r branch-if-not-equal signed *)
+ | CEbequl (optR: option oreg) (**r branch-if-equal unsigned *)
+ | CEbneul (optR: option oreg) (**r branch-if-not-equal unsigned *)
+ | CEbltl (optR: option oreg) (**r branch-if-less signed *)
+ | CEbltul (optR: option oreg) (**r branch-if-less unsigned *)
+ | CEbgel (optR: option oreg) (**r branch-if-greater-or-equal signed *)
+ | CEbgeul (optR: option oreg). (**r branch-if-greater-or-equal unsigned *)
+
+(* This type will define the eval function of a OEmayundef operation. *)
+
+Inductive mayundef: Type :=
+ | MUint: mayundef
+ | MUlong: mayundef
+ | MUshrx: int -> mayundef
+ | MUshrxl: int -> mayundef.
(** Arithmetic and logical operations. In the descriptions, [rd] is the
result of the operation and [r1], [r2], etc, are the arguments. *)
@@ -152,7 +184,47 @@ Inductive operation : Type :=
| Osingleoflong (**r [rd = float32_of_signed_long(r1)] *)
| Osingleoflongu (**r [rd = float32_of_unsigned_int(r1)] *)
(*c Boolean tests: *)
- | Ocmp (cond: condition). (**r [rd = 1] if condition holds, [rd = 0] otherwise. *)
+ | Ocmp (cond: condition) (**r [rd = 1] if condition holds, [rd = 0] otherwise. *)
+ (* Expansed conditions *)
+ | OEseqw (optR: option oreg) (**r [rd <- rs1 == rs2] signed *)
+ | OEsnew (optR: option oreg) (**r [rd <- rs1 != rs2] signed *)
+ | OEsequw (optR: option oreg) (**r [rd <- rs1 == rs2] unsigned *)
+ | OEsneuw (optR: option oreg) (**r [rd <- rs1 != rs2] unsigned *)
+ | OEsltw (optR: option oreg) (**r set-less-than *)
+ | OEsltuw (optR: option oreg) (**r set-less-than unsigned *)
+ | OEsltiw (n: int) (**r set-less-than immediate *)
+ | OEsltiuw (n: int) (**r set-less-than unsigned immediate *)
+ | OEaddiw (optR: option oreg) (n: int) (**r add immediate *)
+ | OEandiw (n: int) (**r and immediate *)
+ | OEoriw (n: int) (**r or immediate *)
+ | OExoriw (n: int) (**r xor immediate *)
+ | OEluiw (n: int) (**r load upper-immediate *)
+ | OEseql (optR: option oreg) (**r [rd <- rs1 == rs2] signed *)
+ | OEsnel (optR: option oreg) (**r [rd <- rs1 != rs2] signed *)
+ | OEsequl (optR: option oreg) (**r [rd <- rs1 == rs2] unsigned *)
+ | OEsneul (optR: option oreg) (**r [rd <- rs1 != rs2] unsigned *)
+ | OEsltl (optR: option oreg) (**r set-less-than *)
+ | OEsltul (optR: option oreg) (**r set-less-than unsigned *)
+ | OEsltil (n: int64) (**r set-less-than immediate *)
+ | OEsltiul (n: int64) (**r set-less-than unsigned immediate *)
+ | OEaddil (optR: option oreg) (n: int64) (**r add immediate *)
+ | OEandil (n: int64) (**r and immediate *)
+ | OEoril (n: int64) (**r or immediate *)
+ | OExoril (n: int64) (**r xor immediate *)
+ | OEluil (n: int64) (**r load upper-immediate *)
+ | OEloadli (n: int64) (**r load an immediate int64 *)
+ | OEmayundef (mu: mayundef)
+ | OEfeqd (**r compare equal *)
+ | OEfltd (**r compare less-than *)
+ | OEfled (**r compare less-than/equal *)
+ | OEfeqs (**r compare equal *)
+ | OEflts (**r compare less-than *)
+ | OEfles (**r compare less-than/equal *)
+ | Obits_of_single
+ | Obits_of_float
+ | Osingle_of_bits
+ | Ofloat_of_bits
+ | Oselectl.
(** Addressing modes. [r1], [r2], etc, are the arguments to the
addressing. *)
@@ -164,11 +236,15 @@ Inductive addressing: Type :=
(** Comparison functions (used in modules [CSE] and [Allocation]). *)
+Definition oreg_eq: forall (x y: oreg), {x=y} + {x<>y}.
+Proof. decide equality. Defined.
+
Definition eq_condition (x y: condition) : {x=y} + {x<>y}.
Proof.
- generalize Int.eq_dec Int64.eq_dec; intro.
+ generalize Int.eq_dec Int64.eq_dec bool_dec oreg_eq; intros.
assert (forall (x y: comparison), {x=y}+{x<>y}). decide equality.
decide equality.
+ all: destruct optR, optR0; decide equality.
Defined.
Definition eq_addressing (x y: addressing) : {x=y} + {x<>y}.
@@ -179,8 +255,9 @@ Defined.
Definition eq_operation: forall (x y: operation), {x=y} + {x<>y}.
Proof.
- generalize Int.eq_dec Int64.eq_dec Ptrofs.eq_dec Float.eq_dec Float32.eq_dec ident_eq eq_condition; intros.
+ generalize Int.eq_dec Int64.eq_dec Ptrofs.eq_dec Float.eq_dec Float32.eq_dec ident_eq eq_condition bool_dec Val.eq oreg_eq; intros.
decide equality.
+ all: try destruct optR, optR0; try decide equality.
Defined.
(* Alternate definition:
@@ -197,6 +274,44 @@ Defined.
Global Opaque eq_condition eq_addressing eq_operation.
+(** Generic function to evaluate an instruction according to the given specific register *)
+
+Definition zero32 := (Vint Int.zero).
+Definition zero64 := (Vlong Int64.zero).
+
+Definition apply_bin_oreg {B} (optR: option oreg) (sem: val -> val -> B) (v1 v2 vz: val): B :=
+ match optR with
+ | None => sem v1 v2
+ | Some X0_L => sem vz v1
+ | Some X0_R => sem v1 vz
+ end.
+
+(** Mayundef evaluation according to the above defined type *)
+
+Definition eval_may_undef (mu: mayundef) (v1 v2: val): val :=
+ match mu with
+ | MUint => match v1, v2 with
+ | Vint _, Vint _ => v2
+ | _, _ => Vundef
+ end
+ | MUlong => match v1, v2 with
+ | Vlong _, Vint _ => v2
+ | _, _ => Vundef
+ end
+ | MUshrx i =>
+ match v1, v2 with
+ | Vint _, Vint _ =>
+ if Int.ltu i (Int.repr 31) then v2 else Vundef
+ | _, _ => Vundef
+ end
+ | MUshrxl i =>
+ match v1, v2 with
+ | Vlong _, Vlong _ =>
+ if Int.ltu i (Int.repr 63) then v2 else Vundef
+ | _, _ => Vundef
+ end
+ end.
+
(** * Evaluation functions *)
(** Evaluation of conditions, operators and addressing modes applied
@@ -218,9 +333,34 @@ Definition eval_condition (cond: condition) (vl: list val) (m: mem): option bool
| Cnotcompf c, v1 :: v2 :: nil => option_map negb (Val.cmpf_bool c v1 v2)
| Ccompfs c, v1 :: v2 :: nil => Val.cmpfs_bool c v1 v2
| Cnotcompfs c, v1 :: v2 :: nil => option_map negb (Val.cmpfs_bool c v1 v2)
+ (* Expansed branches *)
+ | CEbeqw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmp_bool Ceq) v1 v2 zero32
+ | CEbnew optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmp_bool Cne) v1 v2 zero32
+ | CEbequw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpu_bool (Mem.valid_pointer m) Ceq) v1 v2 zero32
+ | CEbneuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpu_bool (Mem.valid_pointer m) Cne) v1 v2 zero32
+ | CEbltw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmp_bool Clt) v1 v2 zero32
+ | CEbltuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpu_bool (Mem.valid_pointer m) Clt) v1 v2 zero32
+ | CEbgew optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmp_bool Cge) v1 v2 zero32
+ | CEbgeuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpu_bool (Mem.valid_pointer m) Cge) v1 v2 zero32
+ | CEbeql optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpl_bool Ceq) v1 v2 zero64
+ | CEbnel optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpl_bool Cne) v1 v2 zero64
+ | CEbequl optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmplu_bool (Mem.valid_pointer m) Ceq) v1 v2 zero64
+ | CEbneul optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmplu_bool (Mem.valid_pointer m) Cne) v1 v2 zero64
+ | CEbltl optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpl_bool Clt) v1 v2 zero64
+ | CEbltul optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmplu_bool (Mem.valid_pointer m) Clt) v1 v2 zero64
+ | CEbgel optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmpl_bool Cge) v1 v2 zero64
+ | CEbgeul optR, v1 :: v2 :: nil => apply_bin_oreg optR (Val.cmplu_bool (Mem.valid_pointer m) Cge) v1 v2 zero64
| _, _ => None
end.
+(** Assert sp is a pointer *)
+
+Definition get_sp sp :=
+ match sp with
+ | Vptr _ _ => sp
+ | _ => Vundef
+ end.
+
Definition eval_operation
(F V: Type) (genv: Genv.t F V) (sp: val)
(op: operation) (vl: list val) (m: mem): option val :=
@@ -241,10 +381,10 @@ Definition eval_operation
| Omul, v1 :: v2 :: nil => Some (Val.mul v1 v2)
| Omulhs, v1::v2::nil => Some (Val.mulhs v1 v2)
| Omulhu, v1::v2::nil => Some (Val.mulhu v1 v2)
- | Odiv, v1 :: v2 :: nil => Val.divs v1 v2
- | Odivu, v1 :: v2 :: nil => Val.divu v1 v2
- | Omod, v1 :: v2 :: nil => Val.mods v1 v2
- | Omodu, v1 :: v2 :: nil => Val.modu v1 v2
+ | Odiv, v1 :: v2 :: nil => Some (Val.maketotal (Val.divs v1 v2))
+ | Odivu, v1 :: v2 :: nil => Some (Val.maketotal (Val.divu v1 v2))
+ | Omod, v1 :: v2 :: nil => Some (Val.maketotal (Val.mods v1 v2))
+ | Omodu, v1 :: v2 :: nil => Some (Val.maketotal (Val.modu v1 v2))
| Oand, v1 :: v2 :: nil => Some (Val.and v1 v2)
| Oandimm n, v1 :: nil => Some (Val.and v1 (Vint n))
| Oor, v1 :: v2 :: nil => Some (Val.or v1 v2)
@@ -257,7 +397,7 @@ Definition eval_operation
| Oshrimm n, v1 :: nil => Some (Val.shr v1 (Vint n))
| Oshru, v1 :: v2 :: nil => Some (Val.shru v1 v2)
| Oshruimm n, v1 :: nil => Some (Val.shru v1 (Vint n))
- | Oshrximm n, v1::nil => Val.shrx v1 (Vint n)
+ | Oshrximm n, v1::nil => Some (Val.maketotal (Val.shrx v1 (Vint n)))
| Omakelong, v1::v2::nil => Some (Val.longofwords v1 v2)
| Olowlong, v1::nil => Some (Val.loword v1)
| Ohighlong, v1::nil => Some (Val.hiword v1)
@@ -270,10 +410,10 @@ Definition eval_operation
| Omull, v1::v2::nil => Some (Val.mull v1 v2)
| Omullhs, v1::v2::nil => Some (Val.mullhs v1 v2)
| Omullhu, v1::v2::nil => Some (Val.mullhu v1 v2)
- | Odivl, v1::v2::nil => Val.divls v1 v2
- | Odivlu, v1::v2::nil => Val.divlu v1 v2
- | Omodl, v1::v2::nil => Val.modls v1 v2
- | Omodlu, v1::v2::nil => Val.modlu v1 v2
+ | Odivl, v1::v2::nil => Some (Val.maketotal (Val.divls v1 v2))
+ | Odivlu, v1::v2::nil => Some (Val.maketotal (Val.divlu v1 v2))
+ | Omodl, v1::v2::nil => Some (Val.maketotal (Val.modls v1 v2))
+ | Omodlu, v1::v2::nil => Some (Val.maketotal (Val.modlu v1 v2))
| Oandl, v1::v2::nil => Some(Val.andl v1 v2)
| Oandlimm n, v1::nil => Some (Val.andl v1 (Vlong n))
| Oorl, v1::v2::nil => Some(Val.orl v1 v2)
@@ -286,7 +426,7 @@ Definition eval_operation
| Oshrlimm n, v1::nil => Some (Val.shrl v1 (Vint n))
| Oshrlu, v1::v2::nil => Some (Val.shrlu v1 v2)
| Oshrluimm n, v1::nil => Some (Val.shrlu v1 (Vint n))
- | Oshrxlimm n, v1::nil => Val.shrxl v1 (Vint n)
+ | Oshrxlimm n, v1::nil => Some (Val.maketotal (Val.shrxl v1 (Vint n)))
| Onegf, v1::nil => Some (Val.negf v1)
| Oabsf, v1::nil => Some (Val.absf v1)
| Oaddf, v1::v2::nil => Some (Val.addf v1 v2)
@@ -301,23 +441,65 @@ Definition eval_operation
| Odivfs, v1::v2::nil => Some (Val.divfs v1 v2)
| Osingleoffloat, v1::nil => Some (Val.singleoffloat v1)
| Ofloatofsingle, v1::nil => Some (Val.floatofsingle v1)
- | Ointoffloat, v1::nil => Val.intoffloat v1
- | Ointuoffloat, v1::nil => Val.intuoffloat v1
- | Ofloatofint, v1::nil => Val.floatofint v1
- | Ofloatofintu, v1::nil => Val.floatofintu v1
- | Ointofsingle, v1::nil => Val.intofsingle v1
- | Ointuofsingle, v1::nil => Val.intuofsingle v1
- | Osingleofint, v1::nil => Val.singleofint v1
- | Osingleofintu, v1::nil => Val.singleofintu v1
- | Olongoffloat, v1::nil => Val.longoffloat v1
- | Olonguoffloat, v1::nil => Val.longuoffloat v1
- | Ofloatoflong, v1::nil => Val.floatoflong v1
- | Ofloatoflongu, v1::nil => Val.floatoflongu v1
- | Olongofsingle, v1::nil => Val.longofsingle v1
- | Olonguofsingle, v1::nil => Val.longuofsingle v1
- | Osingleoflong, v1::nil => Val.singleoflong v1
- | Osingleoflongu, v1::nil => Val.singleoflongu v1
+ | Ointoffloat, v1::nil => Some (Val.maketotal (Val.intoffloat v1))
+ | Ointuoffloat, v1::nil => Some (Val.maketotal (Val.intuoffloat v1))
+ | Ofloatofint, v1::nil => Some (Val.maketotal (Val.floatofint v1))
+ | Ofloatofintu, v1::nil => Some (Val.maketotal (Val.floatofintu v1))
+ | Ointofsingle, v1::nil => Some (Val.maketotal (Val.intofsingle v1))
+ | Ointuofsingle, v1::nil => Some (Val.maketotal (Val.intuofsingle v1))
+ | Osingleofint, v1::nil => Some (Val.maketotal (Val.singleofint v1))
+ | Osingleofintu, v1::nil => Some (Val.maketotal (Val.singleofintu v1))
+ | Olongoffloat, v1::nil => Some (Val.maketotal (Val.longoffloat v1))
+ | Olonguoffloat, v1::nil => Some (Val.maketotal (Val.longuoffloat v1))
+ | Ofloatoflong, v1::nil => Some (Val.maketotal (Val.floatoflong v1))
+ | Ofloatoflongu, v1::nil => Some (Val.maketotal (Val.floatoflongu v1))
+ | Olongofsingle, v1::nil => Some (Val.maketotal (Val.longofsingle v1))
+ | Olonguofsingle, v1::nil => Some (Val.maketotal (Val.longuofsingle v1))
+ | Osingleoflong, v1::nil => Some (Val.maketotal (Val.singleoflong v1))
+ | Osingleoflongu, v1::nil => Some (Val.maketotal (Val.singleoflongu v1))
+ | Obits_of_single, v1::nil => Some (ExtValues.bits_of_single v1)
+ | Obits_of_float, v1::nil => Some (ExtValues.bits_of_float v1)
+ | Osingle_of_bits, v1::nil => Some (ExtValues.single_of_bits v1)
+ | Ofloat_of_bits, v1::nil => Some (ExtValues.float_of_bits v1)
| Ocmp c, _ => Some (Val.of_optbool (eval_condition c vl m))
+ (* Expansed conditions *)
+ | OEseqw optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmp Ceq) v1 v2 zero32)
+ | OEsnew optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmp Cne) v1 v2 zero32)
+ | OEsequw optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m) Ceq) v1 v2 zero32)
+ | OEsneuw optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m) Cne) v1 v2 zero32)
+ | OEsltw optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmp Clt) v1 v2 zero32)
+ | OEsltuw optR, v1::v2::nil => Some (apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m) Clt) v1 v2 zero32)
+ | OEsltiw n, v1::nil => Some (Val.cmp Clt v1 (Vint n))
+ | OEsltiuw n, v1::nil => Some (Val.cmpu (Mem.valid_pointer m) Clt v1 (Vint n))
+ | OExoriw n, v1::nil => Some (Val.xor v1 (Vint n))
+ | OEluiw n, nil => Some (Val.shl (Vint n) (Vint (Int.repr 12)))
+ | OEaddiw optR n, nil => Some (apply_bin_oreg optR Val.add (Vint n) Vundef zero32)
+ | OEaddiw optR n, v1::nil => Some (apply_bin_oreg optR Val.add v1 (Vint n) Vundef)
+ | OEandiw n, v1::nil => Some (Val.and (Vint n) v1)
+ | OEoriw n, v1::nil => Some (Val.or (Vint n) v1)
+ | OEseql optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmpl Ceq) v1 v2 zero64))
+ | OEsnel optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmpl Cne) v1 v2 zero64))
+ | OEsequl optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m) Ceq) v1 v2 zero64))
+ | OEsneul optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m) Cne) v1 v2 zero64))
+ | OEsltl optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmpl Clt) v1 v2 zero64))
+ | OEsltul optR, v1::v2::nil => Some (Val.maketotal (apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m) Clt) v1 v2 zero64))
+ | OEsltil n, v1::nil => Some (Val.maketotal (Val.cmpl Clt v1 (Vlong n)))
+ | OEsltiul n, v1::nil => Some (Val.maketotal (Val.cmplu (Mem.valid_pointer m) Clt v1 (Vlong n)))
+ | OExoril n, v1::nil => Some (Val.xorl v1 (Vlong n))
+ | OEluil n, nil => Some (Vlong (Int64.sign_ext 32 (Int64.shl n (Int64.repr 12))))
+ | OEaddil optR n, nil => Some (apply_bin_oreg optR Val.addl (Vlong n) Vundef zero64)
+ | OEaddil optR n, v1::nil => Some (apply_bin_oreg optR Val.addl v1 (Vlong n) Vundef)
+ | OEandil n, v1::nil => Some (Val.andl (Vlong n) v1)
+ | OEoril n, v1::nil => Some (Val.orl (Vlong n) v1)
+ | OEloadli n, nil => Some (Vlong n)
+ | OEmayundef mu, v1 :: v2 :: nil => Some (eval_may_undef mu v1 v2)
+ | OEfeqd, v1::v2::nil => Some (Val.cmpf Ceq v1 v2)
+ | OEfltd, v1::v2::nil => Some (Val.cmpf Clt v1 v2)
+ | OEfled, v1::v2::nil => Some (Val.cmpf Cle v1 v2)
+ | OEfeqs, v1::v2::nil => Some (Val.cmpfs Ceq v1 v2)
+ | OEflts, v1::v2::nil => Some (Val.cmpfs Clt v1 v2)
+ | OEfles, v1::v2::nil => Some (Val.cmpfs Cle v1 v2)
+ | Oselectl, vb::vt::vf::nil => Some (Val.normalize (ExtValues.select01_long vb vt vf) Tlong)
| _, _ => None
end.
@@ -348,9 +530,9 @@ Qed.
Ltac FuncInv :=
match goal with
| H: (match ?x with nil => _ | _ :: _ => _ end = Some _) |- _ =>
- destruct x; simpl in H; FuncInv
+ destruct x; cbn in H; FuncInv
| H: (match ?v with Vundef => _ | Vint _ => _ | Vfloat _ => _ | Vptr _ _ => _ end = Some _) |- _ =>
- destruct v; simpl in H; FuncInv
+ destruct v; cbn in H; FuncInv
| H: (if Archi.ptr64 then _ else _) = Some _ |- _ =>
destruct Archi.ptr64 eqn:?; FuncInv
| H: (Some _ = Some _) |- _ =>
@@ -377,6 +559,31 @@ Definition type_of_condition (c: condition) : list typ :=
| Cnotcompf _ => Tfloat :: Tfloat :: nil
| Ccompfs _ => Tsingle :: Tsingle :: nil
| Cnotcompfs _ => Tsingle :: Tsingle :: nil
+ | CEbeqw _ => Tint :: Tint :: nil
+ | CEbnew _ => Tint :: Tint :: nil
+ | CEbequw _ => Tint :: Tint :: nil
+ | CEbneuw _ => Tint :: Tint :: nil
+ | CEbltw _ => Tint :: Tint :: nil
+ | CEbltuw _ => Tint :: Tint :: nil
+ | CEbgew _ => Tint :: Tint :: nil
+ | CEbgeuw _ => Tint :: Tint :: nil
+ | CEbeql _ => Tlong :: Tlong :: nil
+ | CEbnel _ => Tlong :: Tlong :: nil
+ | CEbequl _ => Tlong :: Tlong :: nil
+ | CEbneul _ => Tlong :: Tlong :: nil
+ | CEbltl _ => Tlong :: Tlong :: nil
+ | CEbltul _ => Tlong :: Tlong :: nil
+ | CEbgel _ => Tlong :: Tlong :: nil
+ | CEbgeul _ => Tlong :: Tlong :: nil
+ end.
+
+(** The type of mayundef and addsp is dynamic *)
+
+Definition type_of_mayundef mu :=
+ match mu with
+ | MUint | MUshrx _ => (Tint :: Tint :: nil, Tint)
+ | MUlong => (Tlong :: Tint :: nil, Tint)
+ | MUshrxl _ => (Tlong :: Tlong :: nil, Tlong)
end.
Definition type_of_operation (op: operation) : list typ * typ :=
@@ -474,6 +681,47 @@ Definition type_of_operation (op: operation) : list typ * typ :=
| Osingleoflong => (Tlong :: nil, Tsingle)
| Osingleoflongu => (Tlong :: nil, Tsingle)
| Ocmp c => (type_of_condition c, Tint)
+ | OEseqw _ => (Tint :: Tint :: nil, Tint)
+ | OEsnew _ => (Tint :: Tint :: nil, Tint)
+ | OEsequw _ => (Tint :: Tint :: nil, Tint)
+ | OEsneuw _ => (Tint :: Tint :: nil, Tint)
+ | OEsltw _ => (Tint :: Tint :: nil, Tint)
+ | OEsltuw _ => (Tint :: Tint :: nil, Tint)
+ | OEsltiw _ => (Tint :: nil, Tint)
+ | OEsltiuw _ => (Tint :: nil, Tint)
+ | OExoriw _ => (Tint :: nil, Tint)
+ | OEluiw _ => (nil, Tint)
+ | OEaddiw None _ => (Tint :: nil, Tint)
+ | OEaddiw (Some _) _ => (nil, Tint)
+ | OEandiw _ => (Tint :: nil, Tint)
+ | OEoriw _ => (Tint :: nil, Tint)
+ | OEseql _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsnel _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsequl _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsneul _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsltl _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsltul _ => (Tlong :: Tlong :: nil, Tint)
+ | OEsltil _ => (Tlong :: nil, Tint)
+ | OEsltiul _ => (Tlong :: nil, Tint)
+ | OEandil _ => (Tlong :: nil, Tlong)
+ | OEoril _ => (Tlong :: nil, Tlong)
+ | OExoril _ => (Tlong :: nil, Tlong)
+ | OEluil _ => (nil, Tlong)
+ | OEaddil None _ => (Tlong :: nil, Tlong)
+ | OEaddil (Some _) _ => (nil, Tlong)
+ | OEloadli _ => (nil, Tlong)
+ | OEmayundef mu => type_of_mayundef mu
+ | OEfeqd => (Tfloat :: Tfloat :: nil, Tint)
+ | OEfltd => (Tfloat :: Tfloat :: nil, Tint)
+ | OEfled => (Tfloat :: Tfloat :: nil, Tint)
+ | OEfeqs => (Tsingle :: Tsingle :: nil, Tint)
+ | OEflts => (Tsingle :: Tsingle :: nil, Tint)
+ | OEfles => (Tsingle :: Tsingle :: nil, Tint)
+ | Obits_of_single => (Tsingle :: nil, Tint)
+ | Obits_of_float => (Tfloat :: nil, Tlong)
+ | Osingle_of_bits => (Tint :: nil, Tsingle)
+ | Ofloat_of_bits => (Tlong :: nil, Tfloat)
+ | Oselectl => (Tint :: Tlong :: Tlong :: nil, Tlong)
end.
Definition type_of_addressing (addr: addressing) : list typ :=
@@ -504,6 +752,14 @@ Proof.
intros. unfold Val.has_type, Val.addl. destruct Archi.ptr64, v1, v2; auto.
Qed.
+Remark type_mayundef:
+ forall mu v1 v2, Val.has_type (eval_may_undef mu v1 v2) (snd (type_of_mayundef mu)).
+Proof.
+ intros. unfold eval_may_undef.
+ destruct mu eqn:EQMU, v1, v2; simpl; auto.
+ all: destruct Int.ltu; simpl; auto.
+Qed.
+
Lemma type_of_operation_sound:
forall op vl sp v m,
op <> Omove ->
@@ -513,7 +769,7 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
intros.
destruct op; simpl; simpl in H0; FuncInv; subst; simpl.
(* move *)
- - congruence.
+ - simpl in H; congruence.
(* intconst, longconst, floatconst, singleconst *)
- exact I.
- exact I.
@@ -539,15 +795,17 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1...
- destruct v0; destruct v1...
(* div, divu *)
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero); inv H2...
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.eq i0 Int.zero
+ || Int.eq i (Int.repr (-2147483648)) && Int.eq i0 Int.mone); cbn; trivial.
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.eq i0 Int.zero); cbn; trivial.
(* mod, modu *)
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int.eq i0 Int.zero); inv H2...
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.eq i0 Int.zero
+ || Int.eq i (Int.repr (-2147483648)) && Int.eq i0 Int.mone); cbn; trivial.
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int.eq i0 Int.zero); cbn; trivial.
(* and, andimm *)
- destruct v0; destruct v1...
- destruct v0...
@@ -567,7 +825,8 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int.iwordsize)...
- destruct v0; simpl... destruct (Int.ltu n Int.iwordsize)...
(* shrx *)
- - destruct v0; simpl in H0; try discriminate. destruct (Int.ltu n (Int.repr 31)); inv H0...
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 31)); cbn; trivial.
(* makelong, lowlong, highlong *)
- destruct v0; destruct v1...
- destruct v0...
@@ -588,15 +847,19 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1...
- destruct v0; destruct v1...
(* divl, divlu *)
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero); inv H2...
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero
+ || Int64.eq i (Int64.repr (-9223372036854775808)) &&
+ Int64.eq i0 Int64.mone); cbn; trivial.
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero); cbn; trivial.
(* modl, modlu *)
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2...
- - destruct v0; destruct v1; simpl in *; inv H0.
- destruct (Int64.eq i0 Int64.zero); inv H2...
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero
+ || Int64.eq i (Int64.repr (-9223372036854775808)) &&
+ Int64.eq i0 Int64.mone); cbn; trivial.
+ - destruct v0; destruct v1; cbn; trivial.
+ destruct (Int64.eq i0 Int64.zero); cbn; trivial.
(* andl, andlimm *)
- destruct v0; destruct v1...
- destruct v0...
@@ -616,7 +879,8 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0; destruct v1; simpl... destruct (Int.ltu i0 Int64.iwordsize')...
- destruct v0; simpl... destruct (Int.ltu n Int64.iwordsize')...
(* shrxl *)
- - destruct v0; simpl in H0; try discriminate. destruct (Int.ltu n (Int.repr 63)); inv H0...
+ - destruct v0; cbn; trivial.
+ destruct (Int.ltu n (Int.repr 63)); cbn; trivial.
(* negf, absf *)
- destruct v0...
- destruct v0...
@@ -639,33 +903,173 @@ Proof with (try exact I; try reflexivity; auto using Val.Vptr_has_type).
- destruct v0...
- destruct v0...
(* intoffloat, intuoffloat *)
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_int f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_intu f); inv H2...
+ - destruct v0; cbn; trivial.
+ destruct (Float.to_int f); cbn; trivial.
+ - destruct v0; cbn; trivial.
+ destruct (Float.to_intu f); cbn; trivial.
(* floatofint, floatofintu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* intofsingle, intuofsingle *)
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_int f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_intu f); inv H2...
+ - destruct v0; cbn; trivial.
+ destruct (Float32.to_int f); cbn; trivial.
+ - destruct v0; cbn; trivial.
+ destruct (Float32.to_intu f); cbn; trivial.
(* singleofint, singleofintu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* longoffloat, longuoffloat *)
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_long f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float.to_longu f); inv H2...
+ - destruct v0; cbn; trivial.
+ destruct (Float.to_long f); cbn; trivial.
+ - destruct v0; cbn; trivial.
+ destruct (Float.to_longu f); cbn; trivial.
(* floatoflong, floatoflongu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* longofsingle, longuofsingle *)
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_long f); inv H2...
- - destruct v0; simpl in H0; inv H0. destruct (Float32.to_longu f); inv H2...
+ - destruct v0; cbn; trivial.
+ destruct (Float32.to_long f); cbn; trivial.
+ - destruct v0; cbn; trivial.
+ destruct (Float32.to_longu f); cbn; trivial.
(* singleoflong, singleoflongu *)
- - destruct v0; simpl in H0; inv H0...
- - destruct v0; simpl in H0; inv H0...
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
(* cmp *)
- destruct (eval_condition cond vl m)... destruct b...
+ (* OEseqw *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmp;
+ destruct Val.cmp_bool... all: destruct b...
+ (* OEsnew *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmp;
+ destruct Val.cmp_bool... all: destruct b...
+ (* OEsequw *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpu;
+ destruct Val.cmpu_bool... all: destruct b...
+ (* OEsneuw *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpu;
+ destruct Val.cmpu_bool... all: destruct b...
+ (* OEsltw *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmp;
+ destruct Val.cmp_bool... all: destruct b...
+ (* OEsltuw *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpu;
+ destruct Val.cmpu_bool... all: destruct b...
+ (* OEsltiw *)
+ - unfold Val.cmp; destruct Val.cmp_bool...
+ all: destruct b...
+ (* OEsltiuw *)
+ - unfold Val.cmpu; destruct Val.cmpu_bool... destruct b...
+ (* OEaddiw *)
+ - destruct optR as [[]|]; simpl in *; trivial.
+ - destruct optR as [[]|]; simpl in *; trivial;
+ apply type_add.
+ (* OEandiw *)
+ - destruct v0...
+ (* OEoriw *)
+ - destruct v0...
+ (* OExoriw *)
+ - destruct v0...
+ (* OEluiw *)
+ - destruct (Int.ltu _ _); cbn; trivial.
+ (* OEseql *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpl;
+ destruct Val.cmpl_bool... all: destruct b...
+ (* OEsnel *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpl;
+ destruct Val.cmpl_bool... all: destruct b...
+ (* OEsequl *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmplu;
+ destruct Val.cmplu_bool... all: destruct b...
+ (* OEsneul *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmplu;
+ destruct Val.cmplu_bool... all: destruct b...
+ (* OEsltl *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmpl;
+ destruct Val.cmpl_bool... all: destruct b...
+ (* OEsltul *)
+ - destruct optR as [[]|]; simpl; unfold Val.cmplu;
+ destruct Val.cmplu_bool... all: destruct b...
+ (* OEsltil *)
+ - unfold Val.cmpl; destruct Val.cmpl_bool...
+ all: destruct b...
+ (* OEsltiul *)
+ - unfold Val.cmplu; destruct Val.cmplu_bool... destruct b...
+ (* OEaddil *)
+ - destruct optR as [[]|]; simpl in *; trivial.
+ - destruct optR as [[]|]; simpl in *; trivial;
+ apply type_addl.
+ (* OEandil *)
+ - destruct v0...
+ (* OEoril *)
+ - destruct v0...
+ (* OExoril *)
+ - destruct v0...
+ (* OEluil *)
+ - simpl; trivial.
+ (* OEloadli *)
+ - trivial.
+ (* OEmayundef *)
+ - apply type_mayundef.
+ (* OEfeqd *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float.cmp; cbn; auto.
+ (* OEfltd *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float.cmp; cbn; auto.
+ (* OEfled *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float.cmp; cbn; auto.
+ (* OEfeqs *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float32.cmp; cbn; auto.
+ (* OEflts *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float32.cmp; cbn; auto.
+ (* OEfles *)
+ - destruct v0; destruct v1; cbn; auto.
+ destruct Float32.cmp; cbn; auto.
+ (* Bits_of_single, float *)
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
+ (* single, float of bits *)
+ - destruct v0; cbn; trivial.
+ - destruct v0; cbn; trivial.
+ (* selectl *)
+ - destruct v0; cbn; trivial.
+ destruct Int.eq; cbn.
+ apply Val.normalize_type.
+ destruct Int.eq; cbn; trivial.
+ apply Val.normalize_type.
Qed.
+(* This should not be simplified to "false" because it breaks proofs elsewhere. *)
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Omove => false
+ | _ => false
+ end.
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op eqn:E; destruct eq_operation; intros; simpl in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+ all: try destruct optR as [[]|]; simpl in H0; try discriminate.
+ all: try destruct Archi.ptr64; simpl in *; try discriminate.
+ all: try destruct mu; simpl in *; try discriminate.
+Qed.
End SOUNDNESS.
(** * Manipulating and transforming operations *)
@@ -708,6 +1112,22 @@ Definition negate_condition (cond: condition): condition :=
| Cnotcompf c => Ccompf c
| Ccompfs c => Cnotcompfs c
| Cnotcompfs c => Ccompfs c
+ | CEbeqw optR => CEbnew optR
+ | CEbnew optR => CEbeqw optR
+ | CEbequw optR => CEbneuw optR
+ | CEbneuw optR => CEbequw optR
+ | CEbltw optR => CEbgew optR
+ | CEbltuw optR => CEbgeuw optR
+ | CEbgew optR => CEbltw optR
+ | CEbgeuw optR => CEbltuw optR
+ | CEbeql optR => CEbnel optR
+ | CEbnel optR => CEbeql optR
+ | CEbequl optR => CEbneul optR
+ | CEbneul optR => CEbequl optR
+ | CEbltl optR => CEbgel optR
+ | CEbltul optR => CEbgeul optR
+ | CEbgel optR => CEbltl optR
+ | CEbgeul optR => CEbltul optR
end.
Lemma eval_negate_condition:
@@ -727,6 +1147,39 @@ Proof.
repeat (destruct vl; auto). destruct (Val.cmpf_bool c v v0) as [[]|]; auto.
repeat (destruct vl; auto).
repeat (destruct vl; auto). destruct (Val.cmpfs_bool c v v0) as [[]|]; auto.
+
+ repeat (destruct vl; auto); replace (Cne) with (negate_comparison Ceq) by auto; destruct optR as [[]|];
+ apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto); replace (Ceq) with (negate_comparison Cne) by auto; destruct optR as [[]|];
+ apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto); replace (Cne) with (negate_comparison Ceq) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto); replace (Ceq) with (negate_comparison Cne) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto); replace (Cge) with (negate_comparison Clt) by auto; destruct optR as [[]|];
+ apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto); replace (Cge) with (negate_comparison Clt) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto); replace (Clt) with (negate_comparison Cge) by auto; destruct optR as [[]|];
+ apply Val.negate_cmp_bool.
+ repeat (destruct vl; auto); replace (Clt) with (negate_comparison Cge) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpu_bool.
+ repeat (destruct vl; auto); replace (Cne) with (negate_comparison Ceq) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto); replace (Ceq) with (negate_comparison Cne) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto); replace (Cne) with (negate_comparison Ceq) by auto; destruct optR as [[]|];
+ apply Val.negate_cmplu_bool.
+ repeat (destruct vl; auto); replace (Ceq) with (negate_comparison Cne) by auto; destruct optR as [[]|];
+ apply Val.negate_cmplu_bool.
+ repeat (destruct vl; auto); replace (Cge) with (negate_comparison Clt) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto); replace (Cge) with (negate_comparison Clt) by auto; destruct optR as [[]|];
+ apply Val.negate_cmplu_bool.
+ repeat (destruct vl; auto); replace (Clt) with (negate_comparison Cge) by auto; destruct optR as [[]|];
+ apply Val.negate_cmpl_bool.
+ repeat (destruct vl; auto); replace (Clt) with (negate_comparison Cge) by auto; destruct optR as [[]|];
+ apply Val.negate_cmplu_bool.
Qed.
(** Shifting stack-relative references. This is used in [Stacking]. *)
@@ -752,7 +1205,8 @@ Qed.
Lemma type_shift_stack_operation:
forall delta op, type_of_operation (shift_stack_operation delta op) = type_of_operation op.
Proof.
- intros. destruct op; auto.
+ intros. destruct op; auto;
+ try destruct optR as [[]|]; simpl; auto.
Qed.
Lemma eval_shift_stack_addressing:
@@ -769,7 +1223,7 @@ Lemma eval_shift_stack_operation:
eval_operation ge (Vptr sp Ptrofs.zero) (shift_stack_operation delta op) vl m =
eval_operation ge (Vptr sp (Ptrofs.repr delta)) op vl m.
Proof.
- intros. destruct op; simpl; auto. destruct vl; auto.
+ intros. destruct op eqn:E; simpl; auto; destruct vl; auto.
rewrite Ptrofs.add_zero_l, Ptrofs.add_commut; auto.
Qed.
@@ -817,23 +1271,87 @@ Definition is_trivial_op (op: operation) : bool :=
(** Operations that depend on the memory state. *)
+Definition cond_depends_on_memory (cond : condition) : bool :=
+ match cond with
+ | Ccompu _ => negb Archi.ptr64
+ | Ccompuimm _ _ => negb Archi.ptr64
+ | Ccomplu _ => Archi.ptr64
+ | Ccompluimm _ _ => Archi.ptr64
+ | CEbequw _ => negb Archi.ptr64
+ | CEbneuw _ => negb Archi.ptr64
+ | CEbltuw _ => negb Archi.ptr64
+ | CEbgeuw _ => negb Archi.ptr64
+ | CEbequl _ => Archi.ptr64
+ | CEbneul _ => Archi.ptr64
+ | CEbltul _ => Archi.ptr64
+ | CEbgeul _ => Archi.ptr64
+ | _ => false
+ end.
+
Definition op_depends_on_memory (op: operation) : bool :=
match op with
- | Ocmp (Ccompu _) => negb Archi.ptr64
- | Ocmp (Ccompuimm _ _) => negb Archi.ptr64
- | Ocmp (Ccomplu _) => Archi.ptr64
- | Ocmp (Ccompluimm _ _) => Archi.ptr64
+ | Ocmp cmp => cond_depends_on_memory cmp
+ | OEsequw _ => negb Archi.ptr64
+ | OEsneuw _ => negb Archi.ptr64
+ | OEsltiuw _ => negb Archi.ptr64
+ | OEsltuw _ => negb Archi.ptr64
+ | OEsequl _ => Archi.ptr64
+ | OEsneul _ => Archi.ptr64
+ | OEsltul _ => Archi.ptr64
+ | OEsltiul _ => Archi.ptr64
| _ => false
end.
+Lemma cond_depends_on_memory_correct:
+ forall cond args m1 m2,
+ cond_depends_on_memory cond = false ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2.
+ destruct cond; cbn; try congruence.
+ all: unfold Val.cmpu_bool, Val.cmplu_bool.
+ all: destruct Archi.ptr64; cbn; intro SF; try discriminate.
+ all: reflexivity.
+Qed.
+
Lemma op_depends_on_memory_correct:
forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
op_depends_on_memory op = false ->
eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
Proof.
intros until m2. destruct op; simpl; try congruence.
- destruct cond; simpl; intros SF; auto; rewrite ? negb_false_iff in SF;
- unfold Val.cmpu_bool, Val.cmplu_bool; rewrite SF; reflexivity.
+ intro DEPEND.
+ f_equal. f_equal. apply cond_depends_on_memory_correct; trivial.
+ all: intros; repeat (destruct args; auto);
+ unfold Val.cmpu, Val.cmpu_bool, Val.cmplu, Val.cmplu_bool;
+ try destruct optR as [[]|]; simpl;
+ destruct v; try destruct v0; simpl; auto;
+ try apply negb_false_iff in H; try rewrite H; auto.
+Qed.
+
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ try destruct optR as [[]|]; simpl;
+ try destruct v, v0; try rewrite !MEM; auto;
+ try erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op; simpl; try congruence.
+ intro MEM; erewrite cond_valid_pointer_eq; eauto.
+ all: intros MEM; repeat (destruct args; simpl; try congruence);
+ try destruct optR as [[]|]; simpl; try destruct v, v0; try rewrite !MEM; auto;
+ unfold Val.cmpu, Val.cmplu;
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
Qed.
(** Global variables mentioned in an operation or addressing mode *)
@@ -940,6 +1458,90 @@ Ltac InvInject :=
| _ => idtac
end.
+Lemma eval_cmpu_bool_inj': forall b c v v' v0 v0',
+ Val.inject f v v' ->
+ Val.inject f v0 v0' ->
+ Val.cmpu_bool (Mem.valid_pointer m1) c v v0 = Some b ->
+ Val.cmpu_bool (Mem.valid_pointer m2) c v' v0' = Some b.
+Proof.
+ intros.
+ eauto 3 using Val.cmpu_bool_inject, Mem.valid_pointer_implies.
+Qed.
+
+Lemma eval_cmpu_bool_inj: forall c v v' v0 v'0,
+ Val.inject f v v' ->
+ Val.inject f v0 v'0 ->
+ Val.inject f (Val.cmpu (Mem.valid_pointer m1) c v v0)
+ (Val.cmpu (Mem.valid_pointer m2) c v' v'0).
+Proof.
+ intros until v'0. intros HV1 HV2.
+ unfold Val.cmpu;
+ destruct (Val.cmpu_bool (Mem.valid_pointer m1) c _ _) eqn:?; eauto.
+ exploit eval_cmpu_bool_inj'. eapply HV1. eapply HV2. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+Qed.
+
+Lemma eval_cmpu_bool_inj_opt: forall c v v' v0 v'0 optR,
+ Val.inject f v v' ->
+ Val.inject f v0 v'0 ->
+ Val.inject f (apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m1) c) v v0 zero32)
+ (apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m2) c) v' v'0 zero32).
+Proof.
+ intros until optR. intros HV1 HV2.
+ destruct optR as [[]|]; simpl; unfold zero32, Val.cmpu;
+ destruct (Val.cmpu_bool (Mem.valid_pointer m1) c _ _) eqn:?; eauto;
+ assert (HVI: Val.inject f (Vint Int.zero) (Vint Int.zero)) by apply Val.inject_int.
+ + exploit eval_cmpu_bool_inj'. eapply HVI. eapply HV1. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+ + exploit eval_cmpu_bool_inj'. eapply HV1. eapply HVI. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+ + exploit eval_cmpu_bool_inj'. eapply HV1. instantiate (1:=v'0).
+ eauto. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+Qed.
+
+Lemma eval_cmplu_bool_inj': forall b c v v' v0 v0',
+ Val.inject f v v' ->
+ Val.inject f v0 v0' ->
+ Val.cmplu_bool (Mem.valid_pointer m1) c v v0 = Some b ->
+ Val.cmplu_bool (Mem.valid_pointer m2) c v' v0' = Some b.
+Proof.
+ intros.
+ eauto 3 using Val.cmplu_bool_inject, Mem.valid_pointer_implies.
+Qed.
+
+Lemma eval_cmplu_bool_inj: forall c v v' v0 v'0,
+ Val.inject f v v' ->
+ Val.inject f v0 v'0 ->
+ Val.inject f (Val.maketotal (Val.cmplu (Mem.valid_pointer m1) c v v0))
+ (Val.maketotal (Val.cmplu (Mem.valid_pointer m2) c v' v'0)).
+Proof.
+ intros until v'0. intros HV1 HV2.
+ unfold Val.cmplu;
+ destruct (Val.cmplu_bool (Mem.valid_pointer m1) c _ _) eqn:?; eauto.
+ exploit eval_cmplu_bool_inj'. eapply HV1. eapply HV2. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+Qed.
+
+Lemma eval_cmplu_bool_inj_opt: forall c v v' v0 v'0 optR,
+ Val.inject f v v' ->
+ Val.inject f v0 v'0 ->
+ Val.inject f (Val.maketotal (apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m1) c) v v0 zero64))
+ (Val.maketotal (apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m2) c) v' v'0 zero64)).
+Proof.
+ intros until optR. intros HV1 HV2.
+ destruct optR as [[]|]; simpl; unfold zero64, Val.cmplu;
+ destruct (Val.cmplu_bool (Mem.valid_pointer m1) c _ _) eqn:?; eauto;
+ assert (HVI: Val.inject f (Vlong Int64.zero) (Vlong Int64.zero)) by apply Val.inject_long.
+ + exploit eval_cmplu_bool_inj'. eapply HVI. eapply HV1. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+ + exploit eval_cmplu_bool_inj'. eapply HV1. eapply HVI. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+ + exploit eval_cmplu_bool_inj'. eapply HV1. instantiate (1:=v'0).
+ eauto. eapply Heqo.
+ intros EQ; rewrite EQ; destruct b; simpl; constructor; eauto.
+Qed.
+
Lemma eval_condition_inj:
forall cond vl1 vl2 b,
Val.inject_list f vl1 vl2 ->
@@ -947,6 +1549,9 @@ Lemma eval_condition_inj:
eval_condition cond vl2 m2 = Some b.
Proof.
intros. destruct cond; simpl in H0; FuncInv; InvInject; simpl; auto.
+ all: assert (HVI32: Val.inject f (Vint Int.zero) (Vint Int.zero)) by apply Val.inject_int;
+ assert (HVI64: Val.inject f (Vlong Int64.zero) (Vlong Int64.zero)) by apply Val.inject_long;
+ try unfold zero32, zero64.
- inv H3; inv H2; simpl in H0; inv H0; auto.
- eauto 3 using Val.cmpu_bool_inject, Mem.valid_pointer_implies.
- inv H3; simpl in H0; inv H0; auto.
@@ -959,6 +1564,38 @@ Proof.
- inv H3; inv H2; simpl in H0; inv H0; auto.
- inv H3; inv H2; simpl in H0; inv H0; auto.
- inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmpu_bool_inj'; eauto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmpu_bool_inj'; eauto.
+- destruct optR as [[]|]; simpl;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmpu_bool_inj'; eauto.
+- destruct optR as [[]|]; simpl;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmpu_bool_inj'; eauto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmplu_bool_inj'; eauto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmplu_bool_inj'; eauto.
+- destruct optR as [[]|]; simpl;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmplu_bool_inj'; eauto.
+- destruct optR as [[]|]; simpl;
+ inv H3; inv H2; simpl in H0; inv H0; auto.
+- destruct optR as [[]|]; unfold apply_bin_oreg in *;
+ eapply eval_cmplu_bool_inj'; eauto.
Qed.
Ltac TrivialExists :=
@@ -997,19 +1634,29 @@ Proof.
- inv H4; inv H2; simpl; auto.
- inv H4; inv H2; simpl; auto.
(* div, divu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
destruct (Int.eq i0 Int.zero
- || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int.eq i0 Int.zero); inv H2. TrivialExists.
+ || Int.eq i (Int.repr (-2147483648)) && Int.eq i0 Int.mone); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_int.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
+ destruct (Int.eq i0 Int.zero); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_int.
(* mod, modu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
destruct (Int.eq i0 Int.zero
- || Int.eq i (Int.repr Int.min_signed) && Int.eq i0 Int.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int.eq i0 Int.zero); inv H2. TrivialExists.
+ || Int.eq i (Int.repr (-2147483648)) && Int.eq i0 Int.mone); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_int.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
+ destruct (Int.eq i0 Int.zero); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_int.
(* and, andimm *)
- inv H4; inv H2; simpl; auto.
- inv H4; simpl; auto.
@@ -1029,8 +1676,10 @@ Proof.
- inv H4; inv H2; simpl; auto. destruct (Int.ltu i0 Int.iwordsize); auto.
- inv H4; simpl; auto. destruct (Int.ltu n Int.iwordsize); auto.
(* shrx *)
- - inv H4; simpl in H1; try discriminate. simpl.
- destruct (Int.ltu n (Int.repr 31)); inv H1. TrivialExists.
+ - inv H4; cbn; try apply Val.val_inject_undef.
+ destruct (Int.ltu n (Int.repr 31)); cbn.
+ apply Val.inject_int.
+ apply Val.val_inject_undef.
(* makelong, highlong, lowlong *)
- inv H4; inv H2; simpl; auto.
- inv H4; simpl; auto.
@@ -1049,19 +1698,31 @@ Proof.
- inv H4; inv H2; simpl; auto.
- inv H4; inv H2; simpl; auto.
(* divl, divlu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
destruct (Int64.eq i0 Int64.zero
- || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int64.eq i0 Int64.zero); inv H2. TrivialExists.
+ || Int64.eq i (Int64.repr (-9223372036854775808)) &&
+ Int64.eq i0 Int64.mone); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_long.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
+ destruct (Int64.eq i0 Int64.zero); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_long.
(* modl, modlu *)
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
destruct (Int64.eq i0 Int64.zero
- || Int64.eq i (Int64.repr Int64.min_signed) && Int64.eq i0 Int64.mone); inv H2.
- TrivialExists.
- - inv H4; inv H3; simpl in H1; inv H1. simpl.
- destruct (Int64.eq i0 Int64.zero); inv H2. TrivialExists.
+ || Int64.eq i (Int64.repr (-9223372036854775808)) &&
+ Int64.eq i0 Int64.mone); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_long.
+ - inv H4; inv H2; cbn.
+ all: try apply Val.val_inject_undef.
+ destruct (Int64.eq i0 Int64.zero); cbn.
+ apply Val.val_inject_undef.
+ apply Val.inject_long.
(* andl, andlimm *)
- inv H4; inv H2; simpl; auto.
- inv H4; simpl; auto.
@@ -1081,8 +1742,10 @@ Proof.
- inv H4; inv H2; simpl; auto. destruct (Int.ltu i0 Int64.iwordsize'); auto.
- inv H4; simpl; auto. destruct (Int.ltu n Int64.iwordsize'); auto.
(* shrx *)
- - inv H4; simpl in H1; try discriminate. simpl.
- destruct (Int.ltu n (Int.repr 63)); inv H1. TrivialExists.
+ - inv H4; cbn; try apply Val.val_inject_undef.
+ destruct (Int.ltu n (Int.repr 63)); cbn.
+ apply Val.inject_long.
+ apply Val.val_inject_undef.
(* negf, absf *)
- inv H4; simpl; auto.
- inv H4; simpl; auto.
@@ -1105,42 +1768,145 @@ Proof.
- inv H4; simpl; auto.
- inv H4; simpl; auto.
(* intoffloat, intuoffloat *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_int f0); simpl in H2; inv H2.
- exists (Vint i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_intu f0); simpl in H2; inv H2.
- exists (Vint i); auto.
+ - inv H4; cbn; auto.
+ destruct (Float.to_int f0); cbn; auto.
+ - inv H4; cbn; auto.
+ destruct (Float.to_intu f0); cbn; auto.
(* floatofint, floatofintu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
(* intofsingle, intuofsingle *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_int f0); simpl in H2; inv H2.
- exists (Vint i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_intu f0); simpl in H2; inv H2.
- exists (Vint i); auto.
+ - inv H4; cbn; auto.
+ destruct (Float32.to_int f0); cbn; auto.
+ - inv H4; cbn; auto.
+ destruct (Float32.to_intu f0); cbn; auto.
(* singleofint, singleofintu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
(* longoffloat, longuoffloat *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_long f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float.to_longu f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
+ - inv H4; cbn; auto.
+ destruct (Float.to_long f0); cbn; auto.
+ - inv H4; cbn; auto.
+ destruct (Float.to_longu f0); cbn; auto.
(* floatoflong, floatoflongu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
(* longofsingle, longuofsingle *)
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_long f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
- - inv H4; simpl in H1; inv H1. simpl. destruct (Float32.to_longu f0); simpl in H2; inv H2.
- exists (Vlong i); auto.
+ - inv H4; cbn; auto.
+ destruct (Float32.to_long f0); cbn; auto.
+ - inv H4; cbn; auto.
+ destruct (Float32.to_longu f0); cbn; auto.
(* singleoflong, singleoflongu *)
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
- - inv H4; simpl in H1; inv H1. simpl. TrivialExists.
+ - inv H4; cbn; auto.
+ - inv H4; cbn; auto.
(* cmp *)
- subst v1. destruct (eval_condition cond vl1 m1) eqn:?.
exploit eval_condition_inj; eauto. intros EQ; rewrite EQ.
destruct b; simpl; constructor.
simpl; constructor.
+ (* OEseqw *)
+ - destruct optR as [[]|]; simpl; unfold zero32, Val.cmp;
+ inv H4; inv H2; simpl; try destruct (Int.eq _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsnew *)
+ - destruct optR as [[]|]; simpl; unfold zero32, Val.cmp;
+ inv H4; inv H2; simpl; try destruct (Int.eq _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsequw *)
+ - apply eval_cmpu_bool_inj_opt; auto.
+ (* OEsneuw *)
+ - apply eval_cmpu_bool_inj_opt; auto.
+ (* OEsltw *)
+ - destruct optR as [[]|]; simpl; unfold zero32, Val.cmp;
+ inv H4; inv H2; simpl; try destruct (Int.lt _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsltuw *)
+ - apply eval_cmpu_bool_inj_opt; auto.
+ (* OEsltiw *)
+ - inv H4; simpl; cbn; auto; try destruct (Int.lt _ _); apply Val.inject_int.
+ (* OEsltiuw *)
+ - apply eval_cmpu_bool_inj; auto.
+ (* OEaddiw *)
+ - destruct optR as [[]|]; auto; simpl.
+ rewrite Int.add_zero_l; auto.
+ rewrite Int.add_commut, Int.add_zero_l; auto.
+ - destruct optR as [[]|]; auto; simpl;
+ eapply Val.add_inject; auto.
+ (* OEandiw *)
+ - inv H4; cbn; auto.
+ (* OEoriw *)
+ - inv H4; cbn; auto.
+ (* OExoriw *)
+ - inv H4; simpl; auto.
+ (* OEluiw *)
+ - destruct (Int.ltu _ _); auto.
+ (* OEseql *)
+ - destruct optR as [[]|]; simpl; unfold zero64, Val.cmpl;
+ inv H4; inv H2; simpl; try destruct (Int64.eq _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsnel *)
+ - destruct optR as [[]|]; simpl; unfold zero64, Val.cmpl;
+ inv H4; inv H2; simpl; try destruct (Int64.eq _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsequl *)
+ - apply eval_cmplu_bool_inj_opt; auto.
+ (* OEsneul *)
+ - apply eval_cmplu_bool_inj_opt; auto.
+ (* OEsltl *)
+ - destruct optR as [[]|]; simpl; unfold zero64, Val.cmpl;
+ inv H4; inv H2; simpl; try destruct (Int64.lt _ _); simpl; cbn; auto;
+ try apply Val.inject_int.
+ (* OEsltul *)
+ - apply eval_cmplu_bool_inj_opt; auto.
+ (* OEsltil *)
+ - inv H4; simpl; cbn; auto; try destruct (Int64.lt _ _); apply Val.inject_int.
+ (* OEsltiul *)
+ - apply eval_cmplu_bool_inj; auto.
+ (* OEaddil *)
+ - destruct optR as [[]|]; auto; simpl.
+ rewrite Int64.add_zero_l; auto.
+ rewrite Int64.add_commut, Int64.add_zero_l; auto.
+ - destruct optR as [[]|]; auto; simpl;
+ eapply Val.addl_inject; auto.
+ (* OEandil *)
+ - inv H4; cbn; auto.
+ (* OEoril *)
+ - inv H4; cbn; auto.
+ (* OExoril *)
+ - inv H4; simpl; auto.
+ (* OEmayundef *)
+ - destruct mu; inv H4; inv H2; simpl; auto;
+ try destruct (Int.ltu _ _); simpl; auto.
+ all: eapply Val.inject_ptr; eauto.
+ (* OEfeqd *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* OEfltd *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* OEfled *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* OEfeqs *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float32.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* OEflts *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float32.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* OEfles *)
+ - inv H4; inv H2; cbn; simpl; auto.
+ destruct Float32.cmp; unfold Vtrue, Vfalse; cbn; auto.
+ (* Bits_of_single, double *)
+ - inv H4; simpl; auto.
+ - inv H4; simpl; auto.
+ (* single, double of bits *)
+ - inv H4; simpl; auto.
+ - inv H4; simpl; auto.
+ (* selectl *)
+ - inv H4; trivial. cbn.
+ destruct (Int.eq i Int.one).
+ + auto using Val.normalize_inject.
+ + destruct (Int.eq i Int.zero); cbn; auto using Val.normalize_inject.
Qed.
Lemma eval_addressing_inj:
@@ -1159,6 +1925,20 @@ Proof.
apply Val.offset_ptr_inject; auto.
Qed.
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; simpl in *;
+ inv Hinjvl; trivial; try discriminate; inv H0; trivial; try discriminate; inv H2; trivial; try discriminate.
+Qed.
End EVAL_COMPAT.
(** Compatibility of the evaluation functions with the ``is less defined'' relation over values. *)
@@ -1265,6 +2045,18 @@ Proof.
destruct H1 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
Qed.
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros until vl2. intros Hlessdef Heval1.
+ destruct addr; simpl in *;
+ inv Hlessdef; trivial; try discriminate;
+ inv H0; trivial; try discriminate;
+ inv H2; trivial; try discriminate.
+Qed.
End EVAL_LESSDEF.
(** Compatibility of the evaluation functions with memory injections. *)
@@ -1317,6 +2109,20 @@ Proof.
econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
Qed.
+
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
Lemma eval_operation_inject:
forall op vl1 vl2 v1 m1 m2,
Val.inject_list f vl1 vl2 ->
@@ -1358,4 +2164,4 @@ Definition builtin_arg_ok
match ba with
| (BA _ | BA_splitlong (BA _) (BA _)) => true
| _ => builtin_arg_ok_1 ba c
- end.
+ end.
diff --git a/riscV/OpWeights.ml b/riscV/OpWeights.ml
new file mode 100644
index 00000000..226f8b20
--- /dev/null
+++ b/riscV/OpWeights.ml
@@ -0,0 +1,306 @@
+open Op
+open PrepassSchedulingOracleDeps
+
+module FU74 = struct
+ (* Attempt at modeling the FU74 (HiFive Unmatched board) core *)
+
+ let resource_bounds = [| 2; 1; 1; 1; 1 |]
+ (* issue ; LSU ; BU ; FPU ; IMUL/IDIV *)
+
+ let nr_non_pipelined_units = 1
+
+ (* divider *)
+
+ let latency_of_op (op : operation) (nargs : int) =
+ match op with
+ | OEmayundef _ -> 0
+ | Omove | Ointconst _ | Olongconst _
+ | Oaddrsymbol (_, _)
+ | Oaddrstack _ | Ocast8signed | Ocast16signed | Oadd | Oaddimm _ | Oneg
+ | Osub | Oand | Oandimm _ | Oor | Oorimm _ | Oxor | Oxorimm _ | Oshl
+ | Oshlimm _ | Oshr | Oshrimm _ | Oshru | Oshruimm _ | Oshrximm _ | Olowlong
+ | Ocast32signed | Ocast32unsigned | Oaddl | Oaddlimm _ | Onegl | Osubl
+ | Oandl | Oandlimm _ | Oorl | Oorlimm _ | Oxorl | Oxorlimm _ | Oshll
+ | Oshllimm _ | Oshrl | Oshrlimm _ | Oshrlu | Oshrluimm _ | Oshrxlimm _
+ | Oselectl | Obits_of_single | Obits_of_float | OEseqw _ | OEsnew _
+ | OEsequw _ | OEsneuw _ | OEsltw _ | OEsltuw _ | OEsltiw _ | OEsltiuw _
+ | OEaddiw (_, _)
+ | OEandiw _ | OEoriw _ | OExoriw _ | OEluiw _ | OEseql _ | OEsnel _
+ | OEsequl _ | OEsneul _ | OEsltl _ | OEsltul _ | OEsltil _ | OEsltiul _
+ | OEaddil (_, _)
+ | OEandil _ | OEoril _ | OExoril _ | OEluil _ | OEloadli _ ->
+ 1
+ | Osingleconst _ | Ofloatconst _ | Onegf | Oabsf | Onegfs | Oabsfs
+ | Osingleoffloat | Ofloatofsingle | Ofloatofint | Ofloatofintu
+ | Osingleofint | Osingleofintu | Osingle_of_bits ->
+ 2
+ | Omul | Omulhs | Omulhu | Omull | Omullhs | Omullhu -> 3
+ | Omulf -> 7
+ | Omulfs -> 5
+ | Ointoffloat | Ointuoffloat | Ointofsingle | Ointuofsingle | Olongoffloat
+ | Olonguoffloat | Olongofsingle | Olonguofsingle | Osingleoflong
+ | Osingleoflongu | OEfeqd | OEfltd | OEfled | OEfeqs | OEflts | OEfles ->
+ 4
+ | Ofloatoflong | Ofloatoflongu | Ofloat_of_bits -> 6
+ | Oaddf | Osubf | Oaddfs | Osubfs -> 7
+ | Ocmp cond -> (
+ match cond with
+ | Ccomp _ | Ccompu _ | Ccompimm _ | Ccompuimm _ | Ccompl _ | Ccomplu _
+ | Ccomplimm _ | Ccompluimm _ | CEbeqw _ | CEbnew _ | CEbequw _
+ | CEbneuw _ | CEbltw _ | CEbltuw _ | CEbgew _ | CEbgeuw _ | CEbeql _
+ | CEbnel _ | CEbequl _ | CEbneul _ | CEbltl _ | CEbltul _ | CEbgel _
+ | CEbgeul _ ->
+ 1
+ | Ccompf _ | Cnotcompf _ | Ccompfs _ | Cnotcompfs _ -> 4)
+ | Odiv | Odivu | Omod | Omodu | Odivl | Odivlu | Omodl | Omodlu | Odivf
+ | Odivfs ->
+ 68
+ | _ -> 1
+
+ let resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | OEmayundef _ -> [| 0; 0; 0; 0; 0 |]
+ | Omove | Ointconst _ | Olongconst _
+ | Oaddrsymbol (_, _)
+ | Oaddrstack _ | Ocast8signed | Ocast16signed | Oadd | Oaddimm _ | Oneg
+ | Osub | Oand | Oandimm _ | Oor | Oorimm _ | Oxor | Oxorimm _ | Oshl
+ | Oshlimm _ | Oshr | Oshrimm _ | Oshru | Oshruimm _ | Oshrximm _ | Olowlong
+ | Ocast32signed | Ocast32unsigned | Oaddl | Oaddlimm _ | Onegl | Osubl
+ | Oandl | Oandlimm _ | Oorl | Oorlimm _ | Oxorl | Oxorlimm _ | Oshll
+ | Oshllimm _ | Oshrl | Oshrlimm _ | Oshrlu | Oshrluimm _ | Oshrxlimm _
+ | Oselectl | Obits_of_single | Obits_of_float | OEseqw _ | OEsnew _
+ | OEsequw _ | OEsneuw _ | OEsltw _ | OEsltuw _ | OEsltiw _ | OEsltiuw _
+ | OEaddiw (_, _)
+ | OEandiw _ | OEoriw _ | OExoriw _ | OEluiw _ | OEseql _ | OEsnel _
+ | OEsequl _ | OEsneul _ | OEsltl _ | OEsltul _ | OEsltil _ | OEsltiul _
+ | OEaddil (_, _)
+ | OEandil _ | OEoril _ | OExoril _ | OEluil _ | OEloadli _ ->
+ [| 1; 0; 0; 0; 0 |]
+ | Omul | Omulhs | Omulhu | Omull | Omullhs | Omullhu | Odiv | Odivu | Omod
+ | Omodu | Odivl | Odivlu | Omodl | Omodlu ->
+ [| 1; 0; 0; 0; 1 |]
+ | Omulf | Omulfs | Ointoffloat | Ointuoffloat | Ointofsingle | Ointuofsingle
+ | Olongoffloat | Olonguoffloat | Olongofsingle | Olonguofsingle
+ | Osingleoflong | Osingleoflongu | OEfeqd | OEfltd | OEfled | OEfeqs
+ | OEflts | OEfles | Ofloatoflong | Ofloatoflongu | Ofloat_of_bits | Oaddf
+ | Osubf | Oaddfs | Osubfs | Osingleconst _ | Ofloatconst _ | Onegf | Oabsf
+ | Onegfs | Oabsfs | Osingleoffloat | Ofloatofsingle | Ofloatofint
+ | Ofloatofintu | Osingleofint | Osingleofintu | Osingle_of_bits | Odivf
+ | Odivfs ->
+ [| 1; 0; 0; 1; 0 |]
+ | Ocmp cond -> (
+ match cond with
+ | Ccomp _ | Ccompu _ | Ccompimm _ | Ccompuimm _ | Ccompl _ | Ccomplu _
+ | Ccomplimm _ | Ccompluimm _ | CEbeqw _ | CEbnew _ | CEbequw _
+ | CEbneuw _ | CEbltw _ | CEbltuw _ | CEbgew _ | CEbgeuw _ | CEbeql _
+ | CEbnel _ | CEbequl _ | CEbneul _ | CEbltl _ | CEbltul _ | CEbgel _
+ | CEbgeul _ ->
+ [| 1; 0; 0; 0; 0 |]
+ | Ccompf _ | Cnotcompf _ | Ccompfs _ | Cnotcompfs _ ->
+ [| 1; 0; 0; 1; 0 |])
+ | _ -> [| 1; 0; 0; 0; 0 |]
+
+ let non_pipelined_resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Odiv | Odivu | Odivl | Odivlu | Odivf | Odivfs -> [| 68 |]
+ | _ -> [| -1 |]
+
+ let resources_of_cond (cond : condition) (nargs : int) =
+ match cond with
+ | Ccompf _ | Cnotcompf _ | Ccompfs _ | Cnotcompfs _ -> [| 1; 0; 1; 1; 0 |]
+ | _ -> [| 1; 0; 1; 0; 0 |]
+
+ let latency_of_load trap chunk (addr : addressing) (nargs : int) = 3
+
+ let latency_of_call _ _ = 6
+
+ let resources_of_load trap chunk addressing nargs = [| 1; 1; 0; 0; 0 |]
+
+ let resources_of_store chunk addressing nargs = [| 1; 1; 0; 0; 0 |]
+
+ let resources_of_call _ _ = resource_bounds
+
+ let resources_of_builtin _ = resource_bounds
+end
+
+module Rocket = struct
+ (* Attempt at modeling the Rocket core *)
+
+ let resource_bounds = [| 1 |]
+
+ let nr_non_pipelined_units = 1
+
+ (* divider *)
+
+ let latency_of_op (op : operation) (nargs : int) =
+ match op with
+ | Omul | Omulhs | Omulhu | Omull | Omullhs | Omullhu -> 4
+ | Onegf -> 1 (*r [rd = - r1] *)
+ | Oabsf (*r [rd = abs(r1)] *)
+ | Oaddf (*r [rd = r1 + r2] *)
+ | Osubf (*r [rd = r1 - r2] *)
+ | Omulf ->
+ 6 (*r [rd = r1 * r2] *)
+ | Onegfs -> 1 (*r [rd = - r1] *)
+ | Oabsfs (*r [rd = abs(r1)] *)
+ | Oaddfs (*r [rd = r1 + r2] *)
+ | Osubfs (*r [rd = r1 - r2] *)
+ | Omulfs ->
+ 4 (*r [rd = r1 * r2] *)
+ | Osingleoffloat (*r [rd] is [r1] truncated to single-precision float *)
+ | Ofloatofsingle (*r [rd] is [r1] extended to double-precision float *)
+ (*c Conversions between int and float: *)
+ | Ofloatconst _ | Osingleconst _
+ | Ointoffloat (*r [rd = signed_int_of_float64(r1)] *)
+ | Ointuoffloat (*r [rd = unsigned_int_of_float64(r1)] *)
+ | Ofloatofint (*r [rd = float64_of_signed_int(r1)] *)
+ | Ofloatofintu (*r [rd = float64_of_unsigned_int(r1)] *)
+ | Ointofsingle (*r [rd = signed_int_of_float32(r1)] *)
+ | Ointuofsingle (*r [rd = unsigned_int_of_float32(r1)] *)
+ | Osingleofint (*r [rd = float32_of_signed_int(r1)] *)
+ | Osingleofintu (*r [rd = float32_of_unsigned_int(r1)] *)
+ | Olongoffloat (*r [rd = signed_long_of_float64(r1)] *)
+ | Olonguoffloat (*r [rd = unsigned_long_of_float64(r1)] *)
+ | Ofloatoflong (*r [rd = float64_of_signed_long(r1)] *)
+ | Ofloatoflongu (*r [rd = float64_of_unsigned_long(r1)] *)
+ | Olongofsingle (*r [rd = signed_long_of_float32(r1)] *)
+ | Olonguofsingle (*r [rd = unsigned_long_of_float32(r1)] *)
+ | Osingleoflong (*r [rd = float32_of_signed_long(r1)] *)
+ | Osingleoflongu ->
+ 2 (*r [rd = float32_of_unsigned_int(r1)] *)
+ | OEfeqd | OEfltd | OEfeqs | OEflts | OEfles | OEfled | Obits_of_single
+ | Obits_of_float | Osingle_of_bits | Ofloat_of_bits ->
+ 2
+ | OEloadli _ -> 2
+ | Odiv | Odivu | Odivl | Odivlu -> 16
+ | Odivfs -> 35
+ | Odivf -> 50
+ | Ocmp cond -> (
+ match cond with
+ | Ccomp _ | Ccompu _ | Ccompimm _ | Ccompuimm _ | Ccompl _ | Ccomplu _
+ | Ccomplimm _ | Ccompluimm _ | CEbeqw _ | CEbnew _ | CEbequw _
+ | CEbneuw _ | CEbltw _ | CEbltuw _ | CEbgew _ | CEbgeuw _ | CEbeql _
+ | CEbnel _ | CEbequl _ | CEbneul _ | CEbltl _ | CEbltul _ | CEbgel _
+ | CEbgeul _ ->
+ 1
+ | Ccompf _ | Cnotcompf _ -> 2
+ | Ccompfs _ | Cnotcompfs _ -> 2)
+ | OEmayundef _ -> 0
+ | _ -> 1
+
+ let resources_of_op (op : operation) (nargs : int) =
+ match op with OEmayundef _ -> [| 0 |] | _ -> resource_bounds
+
+ let non_pipelined_resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Odiv | Odivu -> [| 29 |]
+ | Odivfs -> [| 20 |]
+ | Odivl | Odivlu | Odivf -> [| 50 |]
+ | _ -> [| -1 |]
+
+ let resources_of_cond (cond : condition) (nargs : int) = resource_bounds
+
+ let latency_of_load trap chunk (addr : addressing) (nargs : int) = 3
+
+ let latency_of_call _ _ = 6
+
+ let resources_of_load trap chunk addressing nargs = resource_bounds
+
+ let resources_of_store chunk addressing nargs = resource_bounds
+
+ let resources_of_call _ _ = resource_bounds
+
+ let resources_of_builtin _ = resource_bounds
+end
+
+module SweRV_EH1 = struct
+ (* Attempt at modeling SweRV EH1
+ [| issues ; LSU ; multiplier |] *)
+ let resource_bounds = [| 2; 1; 1 |]
+
+ let nr_non_pipelined_units = 1
+
+ (* divider *)
+
+ let latency_of_op (op : operation) (nargs : int) =
+ match op with
+ | Omul | Omulhs | Omulhu | Omull | Omullhs | Omullhu -> 3
+ | Odiv | Odivu | Odivl | Odivlu -> 16
+ | _ -> 1
+
+ let resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Omul | Omulhs | Omulhu | Omull | Omullhs | Omullhu -> [| 1; 0; 1 |]
+ | Odiv | Odivu | Odivl | Odivlu -> [| 0; 0; 0 |]
+ | _ -> [| 1; 0; 0 |]
+
+ let non_pipelined_resources_of_op (op : operation) (nargs : int) =
+ match op with
+ | Odiv | Odivu -> [| 29 |]
+ | Odivfs -> [| 20 |]
+ | Odivl | Odivlu | Odivf -> [| 50 |]
+ | _ -> [| -1 |]
+
+ let resources_of_cond (cond : condition) (nargs : int) = [| 1; 0; 0 |]
+
+ let latency_of_load trap chunk (addr : addressing) (nargs : int) = 3
+
+ let latency_of_call _ _ = 6
+
+ let resources_of_load trap chunk addressing nargs = [| 1; 1; 0 |]
+
+ let resources_of_store chunk addressing nargs = [| 1; 1; 0 |]
+
+ let resources_of_call _ _ = resource_bounds
+
+ let resources_of_builtin _ = resource_bounds
+end
+
+let get_opweights () : opweights =
+ match !Clflags.option_mtune with
+ | "rocket" | "" ->
+ {
+ pipelined_resource_bounds = Rocket.resource_bounds;
+ nr_non_pipelined_units = Rocket.nr_non_pipelined_units;
+ latency_of_op = Rocket.latency_of_op;
+ resources_of_op = Rocket.resources_of_op;
+ non_pipelined_resources_of_op = Rocket.non_pipelined_resources_of_op;
+ latency_of_load = Rocket.latency_of_load;
+ resources_of_load = Rocket.resources_of_load;
+ resources_of_store = Rocket.resources_of_store;
+ resources_of_cond = Rocket.resources_of_cond;
+ latency_of_call = Rocket.latency_of_call;
+ resources_of_call = Rocket.resources_of_call;
+ resources_of_builtin = Rocket.resources_of_builtin;
+ }
+ | "SweRV_EH1" | "EH1" ->
+ {
+ pipelined_resource_bounds = SweRV_EH1.resource_bounds;
+ nr_non_pipelined_units = SweRV_EH1.nr_non_pipelined_units;
+ latency_of_op = SweRV_EH1.latency_of_op;
+ resources_of_op = SweRV_EH1.resources_of_op;
+ non_pipelined_resources_of_op = SweRV_EH1.non_pipelined_resources_of_op;
+ latency_of_load = SweRV_EH1.latency_of_load;
+ resources_of_load = SweRV_EH1.resources_of_load;
+ resources_of_store = SweRV_EH1.resources_of_store;
+ resources_of_cond = SweRV_EH1.resources_of_cond;
+ latency_of_call = SweRV_EH1.latency_of_call;
+ resources_of_call = SweRV_EH1.resources_of_call;
+ resources_of_builtin = SweRV_EH1.resources_of_builtin;
+ }
+ | "FU74" | "sifive-u74" ->
+ {
+ pipelined_resource_bounds = FU74.resource_bounds;
+ nr_non_pipelined_units = FU74.nr_non_pipelined_units;
+ latency_of_op = FU74.latency_of_op;
+ resources_of_op = FU74.resources_of_op;
+ non_pipelined_resources_of_op = FU74.non_pipelined_resources_of_op;
+ latency_of_load = FU74.latency_of_load;
+ resources_of_load = FU74.resources_of_load;
+ resources_of_store = FU74.resources_of_store;
+ resources_of_cond = FU74.resources_of_cond;
+ latency_of_call = FU74.latency_of_call;
+ resources_of_call = FU74.resources_of_call;
+ resources_of_builtin = FU74.resources_of_builtin;
+ }
+ | xxx -> failwith (Printf.sprintf "unknown -mtune: %s" xxx)
diff --git a/riscV/PrepassSchedulingOracle.ml b/riscV/PrepassSchedulingOracle.ml
new file mode 120000
index 00000000..912e9ffa
--- /dev/null
+++ b/riscV/PrepassSchedulingOracle.ml
@@ -0,0 +1 @@
+../aarch64/PrepassSchedulingOracle.ml \ No newline at end of file
diff --git a/riscV/PrepassSchedulingOracleDeps.ml b/riscV/PrepassSchedulingOracleDeps.ml
new file mode 120000
index 00000000..1e955b85
--- /dev/null
+++ b/riscV/PrepassSchedulingOracleDeps.ml
@@ -0,0 +1 @@
+../aarch64/PrepassSchedulingOracleDeps.ml \ No newline at end of file
diff --git a/riscV/PrintOp.ml b/riscV/PrintOp.ml
index 9ec474b3..0d47192a 100644
--- a/riscV/PrintOp.ml
+++ b/riscV/PrintOp.ml
@@ -30,6 +30,21 @@ let comparison_name = function
| Cgt -> ">"
| Cge -> ">="
+let mu_name pp = function
+ | MUint -> fprintf pp "MUint"
+ | MUlong -> fprintf pp "MUlong"
+ | MUshrx i -> fprintf pp "MUshrx(%ld)" (camlint_of_coqint i)
+ | MUshrxl i -> fprintf pp "MUshrxl(%ld)" (camlint_of_coqint i)
+
+let get_optR_s c reg pp r1 r2 = function
+ | None -> fprintf pp "(%a %s %a)" reg r1 (comparison_name c) reg r2
+ | Some X0_L -> fprintf pp "(X0 %s %a)" (comparison_name c) reg r1
+ | Some X0_R -> fprintf pp "(%a %s X0)" reg r1 (comparison_name c)
+
+let get_optR_a pp = function
+ | None -> failwith "PrintOp: None in get_optR_a instruction (problem with RTL expansions?)"
+ | Some X0_L | Some X0_R -> fprintf pp "X0"
+
let print_condition reg pp = function
| (Ccomp c, [r1;r2]) ->
fprintf pp "%a %ss %a" reg r1 (comparison_name c) reg r2
@@ -55,15 +70,47 @@ let print_condition reg pp = function
fprintf pp "%a %sfs %a" reg r1 (comparison_name c) reg r2
| (Cnotcompfs c, [r1;r2]) ->
fprintf pp "%a not(%sfs) %a" reg r1 (comparison_name c) reg r2
+ | (CEbeqw optR, [r1;r2]) ->
+ fprintf pp "CEbeqw"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | (CEbnew optR, [r1;r2]) ->
+ fprintf pp "CEbnew"; (get_optR_s Cne reg pp r1 r2 optR)
+ | (CEbequw optR, [r1;r2]) ->
+ fprintf pp "CEbequw"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | (CEbneuw optR, [r1;r2]) ->
+ fprintf pp "CEbneuw"; (get_optR_s Cne reg pp r1 r2 optR)
+ | (CEbltw optR, [r1;r2]) ->
+ fprintf pp "CEbltw"; (get_optR_s Clt reg pp r1 r2 optR)
+ | (CEbltuw optR, [r1;r2]) ->
+ fprintf pp "CEbltuw"; (get_optR_s Clt reg pp r1 r2 optR)
+ | (CEbgew optR, [r1;r2]) ->
+ fprintf pp "CEbgew"; (get_optR_s Cge reg pp r1 r2 optR)
+ | (CEbgeuw optR, [r1;r2]) ->
+ fprintf pp "CEbgeuw"; (get_optR_s Cge reg pp r1 r2 optR)
+ | (CEbeql optR, [r1;r2]) ->
+ fprintf pp "CEbeql"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | (CEbnel optR, [r1;r2]) ->
+ fprintf pp "CEbnel"; (get_optR_s Cne reg pp r1 r2 optR)
+ | (CEbequl optR, [r1;r2]) ->
+ fprintf pp "CEbequl"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | (CEbneul optR, [r1;r2]) ->
+ fprintf pp "CEbneul"; (get_optR_s Cne reg pp r1 r2 optR)
+ | (CEbltl optR, [r1;r2]) ->
+ fprintf pp "CEbltl"; (get_optR_s Clt reg pp r1 r2 optR)
+ | (CEbltul optR, [r1;r2]) ->
+ fprintf pp "CEbltul"; (get_optR_s Clt reg pp r1 r2 optR)
+ | (CEbgel optR, [r1;r2]) ->
+ fprintf pp "CEbgel"; (get_optR_s Cge reg pp r1 r2 optR)
+ | (CEbgeul optR, [r1;r2]) ->
+ fprintf pp "CEbgeul"; (get_optR_s Cge reg pp r1 r2 optR)
| _ ->
fprintf pp "<bad condition>"
let print_operation reg pp = function
| Omove, [r1] -> reg pp r1
- | Ointconst n, [] -> fprintf pp "%ld" (camlint_of_coqint n)
- | Olongconst n, [] -> fprintf pp "%LdL" (camlint64_of_coqint n)
- | Ofloatconst n, [] -> fprintf pp "%F" (camlfloat_of_coqfloat n)
- | Osingleconst n, [] -> fprintf pp "%Ff" (camlfloat_of_coqfloat32 n)
+ | Ointconst n, [] -> fprintf pp "Ointconst(%ld)" (camlint_of_coqint n)
+ | Olongconst n, [] -> fprintf pp "Olongconst(%LdL)" (camlint64_of_coqint n)
+ | Ofloatconst n, [] -> fprintf pp "Ofloatconst(%F)" (camlfloat_of_coqfloat n)
+ | Osingleconst n, [] -> fprintf pp "Osingleconst(%Ff)" (camlfloat_of_coqfloat32 n)
| Oaddrsymbol(id, ofs), [] ->
fprintf pp "\"%s\" + %Ld" (extern_atom id) (camlint64_of_ptrofs ofs)
| Oaddrstack ofs, [] ->
@@ -156,6 +203,47 @@ let print_operation reg pp = function
| Osingleoflong, [r1] -> fprintf pp "singleoflong(%a)" reg r1
| Osingleoflongu, [r1] -> fprintf pp "singleoflongu(%a)" reg r1
| Ocmp c, args -> print_condition reg pp (c, args)
+ | OEseqw optR, [r1;r2] -> fprintf pp "OEseqw"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | OEsnew optR, [r1;r2] -> fprintf pp "OEsnew"; (get_optR_s Cne reg pp r1 r2 optR)
+ | OEsequw optR, [r1;r2] -> fprintf pp "OEsequw"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | OEsneuw optR, [r1;r2] -> fprintf pp "OEsneuw"; (get_optR_s Cne reg pp r1 r2 optR)
+ | OEsltw optR, [r1;r2] -> fprintf pp "OEsltw"; (get_optR_s Clt reg pp r1 r2 optR)
+ | OEsltuw optR, [r1;r2] -> fprintf pp "OEsltuw"; (get_optR_s Clt reg pp r1 r2 optR)
+ | OEsltiw n, [r1] -> fprintf pp "OEsltiw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEsltiuw n, [r1] -> fprintf pp "OEsltiuw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OExoriw n, [r1] -> fprintf pp "OExoriw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEluiw n, _ -> fprintf pp "OEluiw(%ld)" (camlint_of_coqint n)
+ | OEaddiw (optR, n), [] -> fprintf pp "OEaddiw(%a,%ld)" get_optR_a optR (camlint_of_coqint n)
+ | OEaddiw (optR, n), [r1] -> fprintf pp "OEaddiw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEandiw n, [r1] -> fprintf pp "OEandiw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEoriw n, [r1] -> fprintf pp "OEoriw(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEseql optR, [r1;r2] -> fprintf pp "OEseql"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | OEsnel optR, [r1;r2] -> fprintf pp "OEsnel"; (get_optR_s Cne reg pp r1 r2 optR)
+ | OEsequl optR, [r1;r2] -> fprintf pp "OEsequl"; (get_optR_s Ceq reg pp r1 r2 optR)
+ | OEsneul optR, [r1;r2] -> fprintf pp "OEsneul"; (get_optR_s Cne reg pp r1 r2 optR)
+ | OEsltl optR, [r1;r2] -> fprintf pp "OEsltl"; (get_optR_s Clt reg pp r1 r2 optR)
+ | OEsltul optR, [r1;r2] -> fprintf pp "OEsltul"; (get_optR_s Clt reg pp r1 r2 optR)
+ | OEsltil n, [r1] -> fprintf pp "OEsltil(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEsltiul n, [r1] -> fprintf pp "OEsltiul(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OExoril n, [r1] -> fprintf pp "OExoril(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEluil n, _ -> fprintf pp "OEluil(%ld)" (camlint_of_coqint n)
+ | OEaddil (optR, n), [] -> fprintf pp "OEaddil(%a,%ld)" get_optR_a optR (camlint_of_coqint n)
+ | OEaddil (optR, n), [r1] -> fprintf pp "OEaddil(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEandil n, [r1] -> fprintf pp "OEandil(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEoril n, [r1] -> fprintf pp "OEoril(%a,%ld)" reg r1 (camlint_of_coqint n)
+ | OEloadli n, _ -> fprintf pp "OEloadli(%ld)" (camlint_of_coqint n)
+ | OEmayundef mu, [r1;r2] -> fprintf pp "OEmayundef (%a,%a,%a)" mu_name mu reg r1 reg r2
+ | OEfeqd, [r1;r2] -> fprintf pp "OEfeqd(%a,%s,%a)" reg r1 (comparison_name Ceq) reg r2
+ | OEfltd, [r1;r2] -> fprintf pp "OEfltd(%a,%s,%a)" reg r1 (comparison_name Clt) reg r2
+ | OEfled, [r1;r2] -> fprintf pp "OEfled(%a,%s,%a)" reg r1 (comparison_name Cle) reg r2
+ | OEfeqs, [r1;r2] -> fprintf pp "OEfeqs(%a,%s,%a)" reg r1 (comparison_name Ceq) reg r2
+ | OEflts, [r1;r2] -> fprintf pp "OEflts(%a,%s,%a)" reg r1 (comparison_name Clt) reg r2
+ | OEfles, [r1;r2] -> fprintf pp "OEfles(%a,%s,%a)" reg r1 (comparison_name Cle) reg r2
+ | Obits_of_single, [r1] -> fprintf pp "bits_of_single(%a)" reg r1
+ | Obits_of_float, [r1] -> fprintf pp "bits_of_float(%a)" reg r1
+ | Osingle_of_bits, [r1] -> fprintf pp "single_of_bits(%a)" reg r1
+ | Ofloat_of_bits, [r1] -> fprintf pp "float_of_bits(%a)" reg r1
+ | Oselectl, [rb;rt;rf] -> fprintf pp "selectl(b:%a, t:%a, f:%a)" reg rb reg rt reg rf
| _ -> fprintf pp "<bad operator>"
let print_addressing reg pp = function
diff --git a/riscV/SelectLong.vp b/riscV/SelectLong.vp
index b3e07bf5..0ccc4725 100644
--- a/riscV/SelectLong.vp
+++ b/riscV/SelectLong.vp
@@ -21,7 +21,7 @@ Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats.
Require Import Op CminorSel.
-Require Import SelectOp SplitLong.
+Require Import OpHelpers SelectOp SplitLong.
Local Open Scope cminorsel_scope.
Local Open Scope string_scope.
diff --git a/riscV/SelectLongproof.v b/riscV/SelectLongproof.v
index 3794e050..0fc578bf 100644
--- a/riscV/SelectLongproof.v
+++ b/riscV/SelectLongproof.v
@@ -21,6 +21,7 @@ Require Import String Coqlib Maps Integers Floats Errors.
Require Archi.
Require Import AST Values Memory Globalenvs Events.
Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectOpproof SplitLong SplitLongproof.
Require Import SelectLong.
@@ -454,6 +455,10 @@ Proof.
unfold divls_base; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_divls_base; eauto.
TrivialExists.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_modls_base: partial_binary_constructor_sound modls_base Val.modls.
@@ -461,6 +466,10 @@ Proof.
unfold modls_base; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_modls_base; eauto.
TrivialExists.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_divlu_base: partial_binary_constructor_sound divlu_base Val.divlu.
@@ -468,6 +477,10 @@ Proof.
unfold divlu_base; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_divlu_base; eauto.
TrivialExists.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_modlu_base: partial_binary_constructor_sound modlu_base Val.modlu.
@@ -475,6 +488,10 @@ Proof.
unfold modlu_base; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_modlu_base; eauto.
TrivialExists.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_shrxlimm:
@@ -489,33 +506,9 @@ Proof.
- subst n. destruct x; simpl in H0; inv H0. econstructor; split; eauto.
change (Int.ltu Int.zero (Int.repr 63)) with true. simpl. rewrite Int64.shrx'_zero; auto.
- TrivialExists.
-(*
- intros. unfold shrxlimm. destruct Archi.splitlong eqn:SL.
-+ eapply SplitLongproof.eval_shrxlimm; eauto using Archi.splitlong_ptr32.
-+ destruct x; simpl in H0; try discriminate.
- destruct (Int.ltu n (Int.repr 63)) eqn:LTU; inv H0.
- predSpec Int.eq Int.eq_spec n Int.zero.
- - subst n. exists (Vlong i); split; auto. rewrite Int64.shrx'_zero. auto.
- - assert (NZ: Int.unsigned n <> 0).
- { intro EQ; elim H0. rewrite <- (Int.repr_unsigned n). rewrite EQ; auto. }
- assert (LT: 0 <= Int.unsigned n < 63) by (apply Int.ltu_inv in LTU; assumption).
- assert (LTU2: Int.ltu (Int.sub Int64.iwordsize' n) Int64.iwordsize' = true).
- { unfold Int.ltu; apply zlt_true.
- unfold Int.sub. change (Int.unsigned Int64.iwordsize') with 64.
- rewrite Int.unsigned_repr. lia.
- assert (64 < Int.max_unsigned) by reflexivity. lia. }
- assert (X: eval_expr ge sp e m le
- (Eop (Oshrlimm (Int.repr (Int64.zwordsize - 1))) (a ::: Enil))
- (Vlong (Int64.shr' i (Int.repr (Int64.zwordsize - 1))))).
- { EvalOp. }
- assert (Y: eval_expr ge sp e m le (shrxlimm_inner a n)
- (Vlong (Int64.shru' (Int64.shr' i (Int.repr (Int64.zwordsize - 1))) (Int.sub Int64.iwordsize' n)))).
- { EvalOp. simpl. rewrite LTU2. auto. }
- TrivialExists.
- constructor. EvalOp. simpl; eauto. constructor.
- simpl. unfold Int.ltu; rewrite zlt_true. rewrite Int64.shrx'_shr_2 by auto. reflexivity.
- change (Int.unsigned Int64.iwordsize') with 64; lia.
-*)
+ cbn.
+ rewrite H0.
+ reflexivity.
Qed.
Theorem eval_cmplu:
@@ -565,6 +558,7 @@ Proof.
unfold longoffloat; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_longoffloat; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_longuoffloat: partial_unary_constructor_sound longuoffloat Val.longuoffloat.
@@ -572,6 +566,7 @@ Proof.
unfold longuoffloat; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_longuoffloat; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_floatoflong: partial_unary_constructor_sound floatoflong Val.floatoflong.
@@ -579,6 +574,7 @@ Proof.
unfold floatoflong; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_floatoflong; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_floatoflongu: partial_unary_constructor_sound floatoflongu Val.floatoflongu.
@@ -586,6 +582,7 @@ Proof.
unfold floatoflongu; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_floatoflongu; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_longofsingle: partial_unary_constructor_sound longofsingle Val.longofsingle.
@@ -593,6 +590,7 @@ Proof.
unfold longofsingle; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_longofsingle; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_longuofsingle: partial_unary_constructor_sound longuofsingle Val.longuofsingle.
@@ -600,6 +598,7 @@ Proof.
unfold longuofsingle; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_longuofsingle; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_singleoflong: partial_unary_constructor_sound singleoflong Val.singleoflong.
@@ -607,6 +606,7 @@ Proof.
unfold singleoflong; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_singleoflong; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
Theorem eval_singleoflongu: partial_unary_constructor_sound singleoflongu Val.singleoflongu.
@@ -614,6 +614,7 @@ Proof.
unfold singleoflongu; red; intros. destruct Archi.splitlong eqn:SL.
eapply SplitLongproof.eval_singleoflongu; eauto.
TrivialExists.
+ cbn; rewrite H0; reflexivity.
Qed.
End CMCONSTR.
diff --git a/riscV/SelectOp.vp b/riscV/SelectOp.vp
index 99806006..9932aaf8 100644
--- a/riscV/SelectOp.vp
+++ b/riscV/SelectOp.vp
@@ -419,9 +419,39 @@ Definition floatofsingle (e: expr) := Eop Ofloatofsingle (e ::: Enil).
(** ** Selection *)
+Definition same_expr_pure (e1 e2: expr) :=
+ match e1, e2 with
+ | Evar v1, Evar v2 => if ident_eq v1 v2 then true else false
+ | _, _ => false
+ end.
+
Definition select (ty: typ) (cond: condition) (args: exprlist) (e1 e2: expr)
- : option expr
- := None.
+ : option expr :=
+ if same_expr_pure e1 e2
+ then Some e1
+ else
+ if Archi.ptr64 then
+ match ty with
+ | Tlong => Some (Eop Oselectl
+ ((Eop (Ocmp cond) args) ::: e1 ::: e2 ::: Enil))
+ | Tint => Some (Eop Olowlong ((Eop Oselectl
+ ((Eop (Ocmp cond) args) :::
+ (Eop Ocast32signed (e1 ::: Enil)) :::
+ (Eop Ocast32signed (e2 ::: Enil)) ::: Enil)) ::: Enil))
+ | Tfloat => Some (Eop Ofloat_of_bits ((Eop Oselectl
+ ((Eop (Ocmp cond) args) :::
+ (Eop Obits_of_float (e1 ::: Enil)) :::
+ (Eop Obits_of_float (e2 ::: Enil)) ::: Enil)) ::: Enil))
+ | Tsingle => Some
+ (Eop Osingle_of_bits
+ ((Eop Olowlong ((Eop Oselectl
+ ((Eop (Ocmp cond) args) :::
+ (Eop Ocast32signed ((Eop Obits_of_single (e1 ::: Enil)) ::: Enil)) :::
+ (Eop Ocast32signed ((Eop Obits_of_single (e2 ::: Enil)) ::: Enil))
+ ::: Enil)) ::: Enil)) ::: Enil))
+ | _ => None
+ end
+ else None.
(** ** Recognition of addressing modes for load and store operations *)
@@ -452,7 +482,19 @@ Nondetfunction builtin_arg (e: expr) :=
| _ => BA e
end.
+(* floats *)
+Definition divf_base (e1: expr) (e2: expr) :=
+ Eop Odivf (e1 ::: e2 ::: Enil).
+
+Definition divfs_base (e1: expr) (e2: expr) :=
+ Eop Odivfs (e1 ::: e2 ::: Enil).
+
(** Platform-specific known builtins *)
Definition platform_builtin (b: platform_builtin) (args: exprlist) : option expr :=
- None.
+ match b with
+ | BI_bits_of_float => Some (Eop Obits_of_single args)
+ | BI_bits_of_double => Some (Eop Obits_of_float args)
+ | BI_float_of_bits => Some (Eop Osingle_of_bits args)
+ | BI_double_of_bits => Some (Eop Ofloat_of_bits args)
+ end.
diff --git a/riscV/SelectOpproof.v b/riscV/SelectOpproof.v
index b0b4b794..f450fe6c 100644
--- a/riscV/SelectOpproof.v
+++ b/riscV/SelectOpproof.v
@@ -22,6 +22,9 @@ Require Import AST Integers Floats.
Require Import Values Memory Builtins Globalenvs.
Require Import Cminor Op CminorSel.
Require Import SelectOp.
+Require Import OpHelpers.
+Require Import OpHelpersproof.
+Require Import Lia.
Local Open Scope cminorsel_scope.
@@ -73,8 +76,10 @@ Ltac TrivialExists :=
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -502,7 +507,12 @@ Theorem eval_divs_base:
Val.divs x y = Some z ->
exists v, eval_expr ge sp e m le (divs_base a b) v /\ Val.lessdef z v.
Proof.
- intros. unfold divs_base. exists z; split. EvalOp. auto.
+ intros. unfold divs_base. exists z; split. EvalOp.
+ 2: apply Val.lessdef_refl.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_mods_base:
@@ -512,7 +522,12 @@ Theorem eval_mods_base:
Val.mods x y = Some z ->
exists v, eval_expr ge sp e m le (mods_base a b) v /\ Val.lessdef z v.
Proof.
- intros. unfold mods_base. exists z; split. EvalOp. auto.
+ intros. unfold mods_base. exists z; split. EvalOp.
+ 2: apply Val.lessdef_refl.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_divu_base:
@@ -522,7 +537,12 @@ Theorem eval_divu_base:
Val.divu x y = Some z ->
exists v, eval_expr ge sp e m le (divu_base a b) v /\ Val.lessdef z v.
Proof.
- intros. unfold divu_base. exists z; split. EvalOp. auto.
+ intros. unfold divu_base. exists z; split. EvalOp.
+ 2: apply Val.lessdef_refl.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_modu_base:
@@ -532,7 +552,12 @@ Theorem eval_modu_base:
Val.modu x y = Some z ->
exists v, eval_expr ge sp e m le (modu_base a b) v /\ Val.lessdef z v.
Proof.
- intros. unfold modu_base. exists z; split. EvalOp. auto.
+ intros. unfold modu_base. exists z; split. EvalOp.
+ 2: apply Val.lessdef_refl.
+ cbn.
+ rewrite H1.
+ cbn.
+ trivial.
Qed.
Theorem eval_shrximm:
@@ -549,34 +574,12 @@ Proof.
replace (Int.shrx i Int.zero) with i. auto.
unfold Int.shrx, Int.divs. rewrite Int.shl_zero.
change (Int.signed Int.one) with 1. rewrite Z.quot_1_r. rewrite Int.repr_signed; auto.
- econstructor; split. EvalOp. auto.
-(*
- intros. destruct x; simpl in H0; try discriminate.
- destruct (Int.ltu n (Int.repr 31)) eqn:LTU; inv H0.
- unfold shrximm.
- predSpec Int.eq Int.eq_spec n Int.zero.
- - subst n. exists (Vint i); split; auto.
- unfold Int.shrx, Int.divs. rewrite Z.quot_1_r. rewrite Int.repr_signed. auto.
- - assert (NZ: Int.unsigned n <> 0).
- { intro EQ; elim H0. rewrite <- (Int.repr_unsigned n). rewrite EQ; auto. }
- assert (LT: 0 <= Int.unsigned n < 31) by (apply Int.ltu_inv in LTU; assumption).
- assert (LTU2: Int.ltu (Int.sub Int.iwordsize n) Int.iwordsize = true).
- { unfold Int.ltu; apply zlt_true.
- unfold Int.sub. change (Int.unsigned Int.iwordsize) with 32.
- rewrite Int.unsigned_repr. lia.
- assert (32 < Int.max_unsigned) by reflexivity. lia. }
- assert (X: eval_expr ge sp e m le
- (Eop (Oshrimm (Int.repr (Int.zwordsize - 1))) (a ::: Enil))
- (Vint (Int.shr i (Int.repr (Int.zwordsize - 1))))).
- { EvalOp. }
- assert (Y: eval_expr ge sp e m le (shrximm_inner a n)
- (Vint (Int.shru (Int.shr i (Int.repr (Int.zwordsize - 1))) (Int.sub Int.iwordsize n)))).
- { EvalOp. simpl. rewrite LTU2. auto. }
- TrivialExists.
- constructor. EvalOp. simpl; eauto. constructor.
- simpl. unfold Int.ltu; rewrite zlt_true. rewrite Int.shrx_shr_2 by auto. reflexivity.
- change (Int.unsigned Int.iwordsize) with 32; lia.
-*)
+ econstructor; split. EvalOp.
+ cbn.
+ rewrite H0.
+ cbn.
+ reflexivity.
+ apply Val.lessdef_refl.
Qed.
Theorem eval_shl: binary_constructor_sound shl Val.shl.
@@ -786,6 +789,7 @@ Theorem eval_intoffloat:
exists v, eval_expr ge sp e m le (intoffloat a) v /\ Val.lessdef y v.
Proof.
intros; unfold intoffloat. TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intuoffloat:
@@ -795,6 +799,7 @@ Theorem eval_intuoffloat:
exists v, eval_expr ge sp e m le (intuoffloat a) v /\ Val.lessdef y v.
Proof.
intros; unfold intuoffloat. TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatofintu:
@@ -806,6 +811,7 @@ Proof.
intros until y; unfold floatofintu. case (floatofintu_match a); intros.
InvEval. simpl in H0. TrivialExists.
TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_floatofint:
@@ -817,6 +823,7 @@ Proof.
intros until y; unfold floatofint. case (floatofint_match a); intros.
InvEval. simpl in H0. TrivialExists.
TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intofsingle:
@@ -826,6 +833,7 @@ Theorem eval_intofsingle:
exists v, eval_expr ge sp e m le (intofsingle a) v /\ Val.lessdef y v.
Proof.
intros; unfold intofsingle. TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleofint:
@@ -835,6 +843,7 @@ Theorem eval_singleofint:
exists v, eval_expr ge sp e m le (singleofint a) v /\ Val.lessdef y v.
Proof.
intros; unfold singleofint; TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_intuofsingle:
@@ -844,6 +853,7 @@ Theorem eval_intuofsingle:
exists v, eval_expr ge sp e m le (intuofsingle a) v /\ Val.lessdef y v.
Proof.
intros; unfold intuofsingle. TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleofintu:
@@ -853,6 +863,7 @@ Theorem eval_singleofintu:
exists v, eval_expr ge sp e m le (singleofintu a) v /\ Val.lessdef y v.
Proof.
intros; unfold intuofsingle. TrivialExists.
+ cbn. rewrite H0. reflexivity.
Qed.
Theorem eval_singleoffloat: unary_constructor_sound singleoffloat Val.singleoffloat.
@@ -865,6 +876,71 @@ Proof.
red; intros. unfold floatofsingle. TrivialExists.
Qed.
+Lemma mod_small_negative:
+ forall a modulus,
+ modulus > 0 -> -modulus < a < 0 -> a mod modulus = a + modulus.
+Proof.
+ intros.
+ replace (a mod modulus) with ((a + modulus) mod modulus).
+ apply Z.mod_small.
+ lia.
+ rewrite <- Zplus_mod_idemp_r.
+ rewrite Z.mod_same by lia.
+ rewrite Z.add_0_r.
+ reflexivity.
+Qed.
+
+Remark normalize_low_long: forall
+ (PTR64 : Archi.ptr64 = true) v1,
+ Val.loword (Val.normalize (Val.longofint v1) Tlong) = Val.normalize v1 Tint.
+Proof.
+ intros.
+ destruct v1; cbn; try rewrite PTR64; trivial.
+ f_equal.
+ unfold Int64.loword.
+ unfold Int.signed.
+ destruct zlt.
+ { rewrite Int64.int_unsigned_repr.
+ apply Int.repr_unsigned.
+ }
+ pose proof (Int.unsigned_range i).
+ rewrite Int64.unsigned_repr_eq.
+ replace ((Int.unsigned i - Int.modulus) mod Int64.modulus)
+ with (Int64.modulus + Int.unsigned i - Int.modulus).
+ {
+ rewrite <- (Int.repr_unsigned i) at 2.
+ apply Int.eqm_samerepr.
+ unfold Int.eqm, eqmod.
+ change Int.modulus with 4294967296 in *.
+ change Int64.modulus with 18446744073709551616 in *.
+ exists 4294967295.
+ lia.
+ }
+ { rewrite mod_small_negative.
+ lia.
+ constructor.
+ constructor.
+ change Int.modulus with 4294967296 in *.
+ change Int.half_modulus with 2147483648 in *.
+ change Int64.modulus with 18446744073709551616 in *.
+ lia.
+ lia.
+ }
+Qed.
+
+Lemma same_expr_pure_correct:
+ forall le a1 a2 v1 v2
+ (PURE : same_expr_pure a1 a2 = true)
+ (EVAL1 : eval_expr ge sp e m le a1 v1)
+ (EVAL2 : eval_expr ge sp e m le a2 v2),
+ v1 = v2.
+Proof.
+ intros.
+ destruct a1; destruct a2; cbn in *; try discriminate.
+ inv EVAL1. inv EVAL2.
+ destruct (ident_eq i i0); congruence.
+Qed.
+
Theorem eval_select:
forall le ty cond al vl a1 v1 a2 v2 a b,
select ty cond al a1 a2 = Some a ->
@@ -876,7 +952,56 @@ Theorem eval_select:
eval_expr ge sp e m le a v
/\ Val.lessdef (Val.select (Some b) v1 v2 ty) v.
Proof.
- unfold select; intros; discriminate.
+ unfold select; intros.
+ pose proof (same_expr_pure_correct le a1 a2 v1 v2) as PURE.
+ destruct (same_expr_pure a1 a2).
+ { rewrite <- PURE by auto.
+ inv H.
+ exists v1. split. assumption.
+ unfold Val.select.
+ destruct b; apply Val.lessdef_normalize.
+ }
+ clear PURE.
+ destruct Archi.ptr64 eqn:PTR64.
+ 2: discriminate.
+ destruct ty; cbn in *; try discriminate.
+ - (* Tint *)
+ inv H. TrivialExists.
+ + cbn. repeat econstructor; eassumption.
+ + cbn. f_equal. rewrite ExtValues.normalize_select01.
+ rewrite H3. destruct b.
+ * rewrite ExtValues.select01_long_true. apply normalize_low_long; assumption.
+ * rewrite ExtValues.select01_long_false. apply normalize_low_long; assumption.
+
+ - (* Tfloat *)
+ inv H. TrivialExists.
+ + cbn. repeat econstructor; eassumption.
+ + cbn. f_equal. rewrite ExtValues.normalize_select01.
+ rewrite H3. destruct b.
+ * rewrite ExtValues.select01_long_true.
+ apply ExtValues.float_bits_normalize.
+ * rewrite ExtValues.select01_long_false.
+ apply ExtValues.float_bits_normalize.
+
+ - (* Tlong *)
+ inv H. TrivialExists.
+ + cbn. repeat econstructor; eassumption.
+ + cbn. f_equal. rewrite ExtValues.normalize_select01.
+ rewrite H3. destruct b.
+ * rewrite ExtValues.select01_long_true. reflexivity.
+ * rewrite ExtValues.select01_long_false. reflexivity.
+
+ - (* Tsingle *)
+ inv H. TrivialExists.
+ + cbn. repeat econstructor; eassumption.
+ + cbn. f_equal. rewrite ExtValues.normalize_select01.
+ rewrite H3. destruct b.
+ * rewrite ExtValues.select01_long_true.
+ rewrite normalize_low_long by assumption.
+ apply ExtValues.single_bits_normalize.
+ * rewrite ExtValues.select01_long_false.
+ rewrite normalize_low_long by assumption.
+ apply ExtValues.single_bits_normalize.
Qed.
Theorem eval_addressing:
@@ -929,6 +1054,27 @@ Proof.
- constructor; auto.
Qed.
+(* floating-point division without HELPERS *)
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ TrivialExists.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ TrivialExists.
+Qed.
+
(** Platform-specific known builtins *)
Theorem eval_platform_builtin:
@@ -938,7 +1084,10 @@ Theorem eval_platform_builtin:
platform_builtin_sem bf vl = Some v ->
exists v', eval_expr ge sp e m le a v' /\ Val.lessdef v v'.
Proof.
- intros. discriminate.
+ destruct bf; intros until le; intro Heval.
+ all: try (inversion Heval; subst a; clear Heval;
+ exists v; split; trivial;
+ repeat (try econstructor; try eassumption)).
Qed.
End CMCONSTR.
diff --git a/riscV/TargetPrinter.ml b/riscV/TargetPrinter.ml
index d8137f84..aab6b9b8 100644
--- a/riscV/TargetPrinter.ml
+++ b/riscV/TargetPrinter.ml
@@ -107,7 +107,9 @@ module Target : TARGET =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".section .rodata" i
@@ -527,6 +529,8 @@ module Target : TARGET =
fprintf oc " fcvt.s.d %a, %a\n" freg fd freg fs
(* Pseudo-instructions expanded in Asmexpand *)
+ | Pselectl(_, _, _, _) ->
+ assert false
| Pallocframe(sz, ofs) ->
assert false
| Pfreeframe(sz, ofs) ->
diff --git a/riscV/ValueAOp.v b/riscV/ValueAOp.v
index 5670b5fe..e0314c6a 100644
--- a/riscV/ValueAOp.v
+++ b/riscV/ValueAOp.v
@@ -13,9 +13,46 @@
Require Import Coqlib Compopts.
Require Import AST Integers Floats Values Memory Globalenvs.
Require Import Op RTL ValueDomain.
+Require Import Zbits Lia.
(** Value analysis for RISC V operators *)
+Definition zero32 := (I Int.zero).
+Definition zero64 := (L Int64.zero).
+
+(** Functions to select a special register (see Op.v) *)
+
+Definition apply_bin_oreg {B} (optR: option oreg) (sem: aval -> aval -> B) (v1 v2 vz: aval): B :=
+ match optR with
+ | None => sem v1 v2
+ | Some X0_L => sem vz v1
+ | Some X0_R => sem v1 vz
+ end.
+
+Definition eval_may_undef (mu: mayundef) (v1 v2: aval): aval :=
+ match mu with
+ | MUint => match v1, v2 with
+ | I _, I _ => v2
+ | _, _ => Ifptr Ptop
+ end
+ | MUlong => match v1, v2 with
+ | L _, I _ => v2
+ | _, _ => Ifptr Ptop
+ end
+ | MUshrx i =>
+ match v1, v2 with
+ | I _, I _ =>
+ if Int.ltu i (Int.repr 31) then v2 else Ifptr Ptop
+ | _, _ => Ifptr Ptop
+ end
+ | MUshrxl i =>
+ match v1, v2 with
+ | L _, L _ =>
+ if Int.ltu i (Int.repr 63) then v2 else Ifptr Ptop
+ | _, _ => Ifptr Ptop
+ end
+ end.
+
Definition eval_static_condition (cond: condition) (vl: list aval): abool :=
match cond, vl with
| Ccomp c, v1 :: v2 :: nil => cmp_bool c v1 v2
@@ -30,6 +67,22 @@ Definition eval_static_condition (cond: condition) (vl: list aval): abool :=
| Cnotcompf c, v1 :: v2 :: nil => cnot (cmpf_bool c v1 v2)
| Ccompfs c, v1 :: v2 :: nil => cmpfs_bool c v1 v2
| Cnotcompfs c, v1 :: v2 :: nil => cnot (cmpfs_bool c v1 v2)
+ | CEbeqw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmp_bool Ceq) v1 v2 zero32
+ | CEbnew optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmp_bool Cne) v1 v2 zero32
+ | CEbequw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpu_bool Ceq) v1 v2 zero32
+ | CEbneuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpu_bool Cne) v1 v2 zero32
+ | CEbltw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmp_bool Clt) v1 v2 zero32
+ | CEbltuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpu_bool Clt) v1 v2 zero32
+ | CEbgew optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmp_bool Cge) v1 v2 zero32
+ | CEbgeuw optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpu_bool Cge) v1 v2 zero32
+ | CEbeql optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpl_bool Ceq) v1 v2 zero64
+ | CEbnel optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpl_bool Cne) v1 v2 zero64
+ | CEbequl optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmplu_bool Ceq) v1 v2 zero64
+ | CEbneul optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmplu_bool Cne) v1 v2 zero64
+ | CEbltl optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpl_bool Clt) v1 v2 zero64
+ | CEbltul optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmplu_bool Clt) v1 v2 zero64
+ | CEbgel optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmpl_bool Cge) v1 v2 zero64
+ | CEbgeul optR, v1 :: v2 :: nil => apply_bin_oreg optR (cmplu_bool Cge) v1 v2 zero64
| _, _ => Bnone
end.
@@ -41,6 +94,39 @@ Definition eval_static_addressing (addr: addressing) (vl: list aval): aval :=
| _, _ => Vbot
end.
+Definition bits_of_single (v : aval) : aval :=
+ match v with
+ | FS f => I (Float32.to_bits f)
+ | _ => ntop1 v
+ end.
+
+Definition bits_of_float (v : aval) : aval :=
+ match v with
+ | F f => L (Float.to_bits f)
+ | _ => ntop1 v
+ end.
+
+Definition single_of_bits (v : aval) : aval :=
+ match v with
+ | I f => FS (Float32.of_bits f)
+ | _ => ntop1 v
+ end.
+
+Definition float_of_bits (v : aval) : aval :=
+ match v with
+ | L f => F (Float.of_bits f)
+ | _ => ntop1 v
+ end.
+
+Definition select01_long (vb : aval) (vt : aval) (vf : aval) :=
+ match vb with
+ | I b =>
+ if Int.eq b Int.one then add_undef vt
+ else if Int.eq b Int.zero then add_undef vf
+ else add_undef (vlub vt vf)
+ | _ => add_undef (vlub vt vf)
+ end.
+
Definition eval_static_operation (op: operation) (vl: list aval): aval :=
match op, vl with
| Omove, v1::nil => v1
@@ -59,10 +145,10 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Omul, v1::v2::nil => mul v1 v2
| Omulhs, v1::v2::nil => mulhs v1 v2
| Omulhu, v1::v2::nil => mulhu v1 v2
- | Odiv, v1::v2::nil => divs v1 v2
- | Odivu, v1::v2::nil => divu v1 v2
- | Omod, v1::v2::nil => mods v1 v2
- | Omodu, v1::v2::nil => modu v1 v2
+ | Odiv, v1::v2::nil => divs_total v1 v2
+ | Odivu, v1::v2::nil => divu_total v1 v2
+ | Omod, v1::v2::nil => mods_total v1 v2
+ | Omodu, v1::v2::nil => modu_total v1 v2
| Oand, v1::v2::nil => and v1 v2
| Oandimm n, v1::nil => and v1 (I n)
| Oor, v1::v2::nil => or v1 v2
@@ -88,10 +174,10 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Omull, v1::v2::nil => mull v1 v2
| Omullhs, v1::v2::nil => mullhs v1 v2
| Omullhu, v1::v2::nil => mullhu v1 v2
- | Odivl, v1::v2::nil => divls v1 v2
- | Odivlu, v1::v2::nil => divlu v1 v2
- | Omodl, v1::v2::nil => modls v1 v2
- | Omodlu, v1::v2::nil => modlu v1 v2
+ | Odivl, v1::v2::nil => divls_total v1 v2
+ | Odivlu, v1::v2::nil => divlu_total v1 v2
+ | Omodl, v1::v2::nil => modls_total v1 v2
+ | Omodlu, v1::v2::nil => modlu_total v1 v2
| Oandl, v1::v2::nil => andl v1 v2
| Oandlimm n, v1::nil => andl v1 (L n)
| Oorl, v1::v2::nil => orl v1 v2
@@ -119,23 +205,64 @@ Definition eval_static_operation (op: operation) (vl: list aval): aval :=
| Odivfs, v1::v2::nil => divfs v1 v2
| Osingleoffloat, v1::nil => singleoffloat v1
| Ofloatofsingle, v1::nil => floatofsingle v1
- | Ointoffloat, v1::nil => intoffloat v1
- | Ointuoffloat, v1::nil => intuoffloat v1
+ | Ointoffloat, v1::nil => intoffloat_total v1
+ | Ointuoffloat, v1::nil => intuoffloat_total v1
| Ofloatofint, v1::nil => floatofint v1
| Ofloatofintu, v1::nil => floatofintu v1
- | Ointofsingle, v1::nil => intofsingle v1
- | Ointuofsingle, v1::nil => intuofsingle v1
+ | Ointofsingle, v1::nil => intofsingle_total v1
+ | Ointuofsingle, v1::nil => intuofsingle_total v1
| Osingleofint, v1::nil => singleofint v1
| Osingleofintu, v1::nil => singleofintu v1
- | Olongoffloat, v1::nil => longoffloat v1
- | Olonguoffloat, v1::nil => longuoffloat v1
+ | Olongoffloat, v1::nil => longoffloat_total v1
+ | Olonguoffloat, v1::nil => longuoffloat_total v1
| Ofloatoflong, v1::nil => floatoflong v1
| Ofloatoflongu, v1::nil => floatoflongu v1
- | Olongofsingle, v1::nil => longofsingle v1
- | Olonguofsingle, v1::nil => longuofsingle v1
+ | Olongofsingle, v1::nil => longofsingle_total v1
+ | Olonguofsingle, v1::nil => longuofsingle_total v1
| Osingleoflong, v1::nil => singleoflong v1
| Osingleoflongu, v1::nil => singleoflongu v1
| Ocmp c, _ => of_optbool (eval_static_condition c vl)
+ | OEseqw optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmp_bool Ceq) v1 v2 zero32)
+ | OEsnew optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmp_bool Cne) v1 v2 zero32)
+ | OEsequw optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpu_bool Ceq) v1 v2 zero32)
+ | OEsneuw optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpu_bool Cne) v1 v2 zero32)
+ | OEsltw optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmp_bool Clt) v1 v2 zero32)
+ | OEsltuw optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpu_bool Clt) v1 v2 zero32)
+ | OEsltiw n, v1::nil => of_optbool (cmp_bool Clt v1 (I n))
+ | OEsltiuw n, v1::nil => of_optbool (cmpu_bool Clt v1 (I n))
+ | OExoriw n, v1::nil => xor v1 (I n)
+ | OEluiw n, nil => shl (I n) (I (Int.repr 12))
+ | OEaddiw optR n, nil => apply_bin_oreg optR add (I n) (Ifptr Ptop) zero32
+ | OEaddiw optR n, v1::nil => apply_bin_oreg optR add v1 (I n) (Ifptr Ptop)
+ | OEandiw n, v1::nil => and (I n) v1
+ | OEoriw n, v1::nil => or (I n) v1
+ | OEseql optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpl_bool Ceq) v1 v2 zero64)
+ | OEsnel optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpl_bool Cne) v1 v2 zero64)
+ | OEsequl optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmplu_bool Ceq) v1 v2 zero64)
+ | OEsneul optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmplu_bool Cne) v1 v2 zero64)
+ | OEsltl optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmpl_bool Clt) v1 v2 zero64)
+ | OEsltul optR, v1::v2::nil => of_optbool (apply_bin_oreg optR (cmplu_bool Clt) v1 v2 zero64)
+ | OEsltil n, v1::nil => of_optbool (cmpl_bool Clt v1 (L n))
+ | OEsltiul n, v1::nil => of_optbool (cmplu_bool Clt v1 (L n))
+ | OEandil n, v1::nil => andl (L n) v1
+ | OEoril n, v1::nil => orl (L n) v1
+ | OExoril n, v1::nil => xorl v1 (L n)
+ | OEluil n, nil => sign_ext 32 (shll (L n) (L (Int64.repr 12)))
+ | OEaddil optR n, nil => apply_bin_oreg optR addl (L n) (Ifptr Ptop) zero64
+ | OEaddil optR n, v1::nil => apply_bin_oreg optR addl v1 (L n) (Ifptr Ptop)
+ | OEloadli n, nil => L (n)
+ | OEmayundef mu, v1 :: v2 :: nil => eval_may_undef mu v1 v2
+ | OEfeqd, v1::v2::nil => of_optbool (cmpf_bool Ceq v1 v2)
+ | OEfltd, v1::v2::nil => of_optbool (cmpf_bool Clt v1 v2)
+ | OEfled, v1::v2::nil => of_optbool (cmpf_bool Cle v1 v2)
+ | OEfeqs, v1::v2::nil => of_optbool (cmpfs_bool Ceq v1 v2)
+ | OEflts, v1::v2::nil => of_optbool (cmpfs_bool Clt v1 v2)
+ | OEfles, v1::v2::nil => of_optbool (cmpfs_bool Cle v1 v2)
+ | Obits_of_single, v1::nil => bits_of_single v1
+ | Obits_of_float, v1::nil => bits_of_float v1
+ | Osingle_of_bits, v1::nil => single_of_bits v1
+ | Ofloat_of_bits, v1::nil => float_of_bits v1
+ | Oselectl, vb::vt::vf::nil => select01_long vb vt vf
| _, _ => Vbot
end.
@@ -147,6 +274,75 @@ Hypothesis GENV: genv_match bc ge.
Variable sp: block.
Hypothesis STACK: bc sp = BCstack.
+Lemma bits_of_single_sound:
+ forall v x, vmatch bc v x -> vmatch bc (ExtValues.bits_of_single v) (bits_of_single x).
+Proof.
+ unfold ExtValues.bits_of_single; intros. inv H; cbn; constructor.
+Qed.
+
+Lemma bits_of_float_sound:
+ forall v x, vmatch bc v x -> vmatch bc (ExtValues.bits_of_float v) (bits_of_float x).
+Proof.
+ unfold ExtValues.bits_of_float; intros. inv H; cbn; constructor.
+Qed.
+
+Lemma single_of_bits_sound:
+ forall v x, vmatch bc v x -> vmatch bc (ExtValues.single_of_bits v) (single_of_bits x).
+Proof.
+ unfold ExtValues.bits_of_single; intros. inv H; cbn; constructor.
+Qed.
+
+Lemma float_of_bits_sound:
+ forall v x, vmatch bc v x -> vmatch bc (ExtValues.float_of_bits v) (float_of_bits x).
+Proof.
+ unfold ExtValues.bits_of_float; intros. inv H; cbn; constructor.
+Qed.
+
+
+Lemma select01_long_sound:
+ forall vb xb vt xt vf xf
+ (MATCH_b : vmatch bc vb xb)
+ (MATCH_t : vmatch bc vt xt)
+ (MATCH_f : vmatch bc vf xf),
+ vmatch bc (Val.normalize (ExtValues.select01_long vb vt vf) Tlong)
+ (select01_long xb xt xf).
+Proof.
+ intros.
+ inv MATCH_b; cbn; try apply add_undef_undef.
+ - destruct (Int.eq i Int.one). { apply add_undef_normalize; trivial. }
+ destruct (Int.eq i Int.zero). { apply add_undef_normalize; trivial. }
+ cbn. apply add_undef_undef.
+ - destruct (Int.eq i Int.one).
+ { apply add_undef_normalize.
+ apply vmatch_lub_l.
+ trivial. }
+ destruct (Int.eq i Int.zero).
+ { apply add_undef_normalize.
+ apply vmatch_lub_r.
+ trivial. }
+ cbn. apply add_undef_undef.
+ - destruct (Int.eq i Int.one).
+ { apply add_undef_normalize.
+ apply vmatch_lub_l.
+ trivial. }
+ destruct (Int.eq i Int.zero).
+ { apply add_undef_normalize.
+ apply vmatch_lub_r.
+ trivial. }
+ cbn. apply add_undef_undef.
+ - destruct (Int.eq i Int.one).
+ { apply add_undef_normalize.
+ apply vmatch_lub_l.
+ trivial. }
+ destruct (Int.eq i Int.zero).
+ { apply add_undef_normalize.
+ apply vmatch_lub_r.
+ trivial. }
+ cbn. apply add_undef_undef.
+Qed.
+
+Hint Resolve bits_of_single_sound bits_of_float_sound single_of_bits_sound float_of_bits_sound select01_long_sound : va.
+
Theorem eval_static_condition_sound:
forall cond vargs m aargs,
list_forall2 (vmatch bc) vargs aargs ->
@@ -158,7 +354,9 @@ Proof.
destruct cond; simpl; eauto with va.
inv H2.
destruct cond; simpl; eauto with va.
- destruct cond; auto with va.
+ 17: destruct cond; simpl; eauto with va.
+ all: destruct optR as [[]|]; unfold apply_bin_oreg, Op.apply_bin_oreg;
+ unfold zero32, Op.zero32, zero64, Op.zero64; eauto with va.
Qed.
Lemma symbol_address_sound:
@@ -200,6 +398,70 @@ Proof.
rewrite Ptrofs.add_zero_l; eauto with va.
Qed.
+Lemma of_optbool_maketotal_sound:
+ forall ob ab, cmatch ob ab -> vmatch bc (Val.maketotal (option_map Val.of_bool ob)) (of_optbool ab).
+Proof.
+ intros.
+ assert (DEFAULT: vmatch bc (Val.maketotal (option_map Val.of_bool ob)) (Uns Pbot 1)).
+ {
+ destruct ob; simpl; auto with va.
+ destruct b; constructor; try lia.
+ change 1 with (usize Int.one). apply is_uns_usize.
+ red; intros. apply Int.bits_zero.
+ }
+ inv H; auto. simpl. destruct b; constructor.
+Qed.
+
+Lemma eval_cmpu_sound c: forall a1 b1 a0 b0 optR m,
+ c = Ceq \/ c = Cne \/ c = Clt->
+ vmatch bc a1 b1 ->
+ vmatch bc a0 b0 ->
+ vmatch bc (Op.apply_bin_oreg optR (Val.cmpu (Mem.valid_pointer m) c) a1 a0 Op.zero32)
+ (of_optbool (apply_bin_oreg optR (cmpu_bool c) b1 b0 zero32)).
+Proof.
+ intros.
+ destruct optR as [[]|]; unfold Op.apply_bin_oreg, apply_bin_oreg;
+ apply of_optbool_sound; unfold Op.zero32, zero32; eauto with va.
+Qed.
+
+Lemma eval_cmplu_sound c: forall a1 b1 a0 b0 optR m,
+ c = Ceq \/ c = Cne \/ c = Clt->
+ vmatch bc a1 b1 ->
+ vmatch bc a0 b0 ->
+ vmatch bc
+ (Val.maketotal
+ (Op.apply_bin_oreg optR (Val.cmplu (Mem.valid_pointer m) c) a1 a0
+ Op.zero64))
+ (of_optbool (apply_bin_oreg optR (cmplu_bool c) b1 b0 zero64)).
+Proof.
+ intros.
+ destruct optR as [[]|]; unfold Op.apply_bin_oreg, apply_bin_oreg;
+ apply of_optbool_maketotal_sound; unfold Op.zero64, zero64; eauto with va.
+Qed.
+
+Lemma eval_cmp_sound: forall a1 b1 a0 b0 optR cmp,
+ vmatch bc a1 b1 ->
+ vmatch bc a0 b0 ->
+ vmatch bc (Op.apply_bin_oreg optR (Val.cmp cmp) a1 a0 Op.zero32)
+ (of_optbool (apply_bin_oreg optR (cmp_bool cmp) b1 b0 zero32)).
+Proof.
+ intros.
+ destruct optR as [[]|]; unfold Op.apply_bin_oreg, apply_bin_oreg;
+ apply of_optbool_sound; unfold Op.zero32, zero32; eauto with va.
+Qed.
+
+Lemma eval_cmpl_sound: forall a1 b1 a0 b0 optR cmp,
+ vmatch bc a1 b1 ->
+ vmatch bc a0 b0 ->
+ vmatch bc
+ (Val.maketotal (Op.apply_bin_oreg optR (Val.cmpl cmp) a1 a0 Op.zero64))
+ (of_optbool (apply_bin_oreg optR (cmpl_bool cmp) b1 b0 zero64)).
+Proof.
+ intros.
+ destruct optR as [[]|]; unfold Op.apply_bin_oreg, apply_bin_oreg;
+ apply of_optbool_maketotal_sound; unfold Op.zero64, zero64; eauto with va.
+Qed.
+
Theorem eval_static_operation_sound:
forall op vargs m vres aargs,
eval_operation ge (Vptr sp Ptrofs.zero) op vargs m = Some vres ->
@@ -212,6 +474,39 @@ Proof.
destruct (propagate_float_constants tt); constructor.
rewrite Ptrofs.add_zero_l; eauto with va.
apply of_optbool_sound. eapply eval_static_condition_sound; eauto.
+
+ 3,4,6: apply eval_cmpu_sound; auto.
+ 1,2,3: apply eval_cmp_sound; auto.
+ unfold Val.cmp; apply of_optbool_sound; eauto with va.
+ unfold Val.cmpu; apply of_optbool_sound; eauto with va.
+
+ { destruct optR as [[]|]; simpl; eauto with va. }
+ { destruct optR as [[]|];
+ unfold apply_bin_oreg, Op.apply_bin_oreg; eauto with va. }
+ { fold (Val.and (Vint n) a1); eauto with va. }
+ { fold (Val.or (Vint n) a1); eauto with va. }
+ { simpl; try destruct (Int.ltu _ _); eauto with va; unfold ntop1;
+ try apply vmatch_ifptr_undef. }
+ 9: { destruct optR as [[]|]; simpl; eauto with va. }
+ 9: { destruct optR as [[]|];
+ unfold apply_bin_oreg, Op.apply_bin_oreg; eauto with va. }
+ 9: { fold (Val.andl (Vlong n) a1); eauto with va. }
+ 9: { fold (Val.orl (Vlong n) a1); eauto with va. }
+ 9: { simpl; unfold ntop1, sign_ext, Int64.sign_ext, sgn; simpl;
+ apply vmatch_ifptr_l. }
+
+ 1,10: simpl; eauto with va.
+ 10:
+ unfold Op.eval_may_undef, eval_may_undef; destruct mu;
+ inv H1; inv H0; eauto with va;
+ try destruct (Int.ltu _ _); simpl;
+ try eapply vmatch_ifptr_p, pmatch_top'; eauto with va.
+
+ 4,5,7: apply eval_cmplu_sound; auto.
+ 1,3,4: apply eval_cmpl_sound; auto.
+ 2: { unfold Val.cmpl; apply of_optbool_maketotal_sound; eauto with va. }
+ 2: { unfold Val.cmplu; apply of_optbool_maketotal_sound; eauto with va. }
+ all: unfold Val.cmpf; apply of_optbool_sound; eauto with va.
Qed.
End SOUNDNESS.
diff --git a/runtime/Makefile b/runtime/Makefile
index beb105a6..ff51b7d7 100644
--- a/runtime/Makefile
+++ b/runtime/Makefile
@@ -1,6 +1,8 @@
include ../Makefile.config
-CFLAGS=-O1 -g -Wall
+.PRECIOUS: .s
+
+CFLAGS=-O1 -Wall
ifeq ($(ARCH),x86)
ifeq ($(MODEL),64)
@@ -22,6 +24,12 @@ ifeq ($(ARCH),x86_64)
OBJS=i64_dtou.o i64_utod.o i64_utof.o vararg.o
else ifeq ($(ARCH),powerpc64)
OBJS=i64_dtou.o i64_stof.o i64_utod.o i64_utof.o vararg.o
+else ifeq ($(ARCH),kvx)
+OBJS=i64_umod.o i64_udiv.o i64_udivmod.o i64_sdiv.o i64_smod.o \
+ i64_udivmod_stsud.o i32_divmod.o \
+ i64_utod.o i64_utof.o i64_stod.o i64_stof.o \
+ vararg.o
+DOMAKE:=$(shell (cd kvx && make))
else ifeq ($(ARCH),aarch64)
OBJS=vararg.o
else
@@ -32,11 +40,18 @@ OBJS=i64_dtos.o i64_dtou.o i64_sar.o i64_sdiv.o i64_shl.o \
vararg.o
endif
+AR=ar
+
+OBJS+=write_profiling_table.o
+
LIB=libcompcert.a
INCLUDES=include/float.h include/stdarg.h include/stdbool.h \
include/stddef.h include/varargs.h include/stdalign.h \
include/stdnoreturn.h
+ifeq ($(ARCH),kvx)
+INCLUDES += include/ccomp_kvx_fixes.h include/math.h
+endif
VPATH=$(ARCH)
@@ -48,7 +63,7 @@ endif
$(LIB): $(OBJS)
rm -f $(LIB)
- $(ARCHIVER) $(LIB) $(OBJS)
+ $(AR) rcs $(LIB) $(OBJS)
%.o: %.s
$(CASMRUNTIME) -o $@ $^
@@ -62,10 +77,10 @@ $(LIB): $(OBJS)
# generated assembly
%.o: c/%.c c/i64.h ../ccomp
- ../ccomp -O2 -S -o $*.s -I./c c/$*.c
+ ../ccomp -g -O2 -S -o $*.s -I./c c/$*.c
sed -i -e 's/i64_/__compcert_i64_/g' $*.s
$(CASMRUNTIME) -o $*.o $*.s
- @rm -f $*.s
+ @rm $*.s
clean::
rm -f *.o $(LIB) *.tmp?.s
diff --git a/runtime/c/ccomp_kvx_fixes.h b/runtime/c/ccomp_kvx_fixes.h
new file mode 120000
index 00000000..726d0f72
--- /dev/null
+++ b/runtime/c/ccomp_kvx_fixes.h
@@ -0,0 +1 @@
+../include/ccomp_kvx_fixes.h \ No newline at end of file
diff --git a/runtime/c/write_profiling_table.c b/runtime/c/write_profiling_table.c
new file mode 100644
index 00000000..f8f46306
--- /dev/null
+++ b/runtime/c/write_profiling_table.c
@@ -0,0 +1,70 @@
+/* *************************************************************/
+/* */
+/* The Compcert verified compiler */
+/* */
+/* David Monniaux CNRS, VERIMAG */
+/* */
+/* Copyright VERIMAG. All rights reserved. */
+/* This file is distributed under the terms of the INRIA */
+/* Non-Commercial License Agreement. */
+/* */
+/* *************************************************************/
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+typedef uint8_t md5_hash[16];
+typedef uint64_t condition_counters[2];
+
+static void write_id(FILE *fp, md5_hash *hash) {
+ fwrite(hash, 16, 1, fp);
+}
+
+#define BYTE(counter, i) ((counter >> (8*i)) & 0xFF)
+static void write_counter(FILE *fp, uint64_t counter) {
+ putc(BYTE(counter, 0), fp);
+ putc(BYTE(counter, 1), fp);
+ putc(BYTE(counter, 2), fp);
+ putc(BYTE(counter, 3), fp);
+ putc(BYTE(counter, 4), fp);
+ putc(BYTE(counter, 5), fp);
+ putc(BYTE(counter, 6), fp);
+ putc(BYTE(counter, 7), fp);
+}
+
+void _compcert_write_profiling_table(unsigned int nr_items,
+ md5_hash id_table[],
+ condition_counters counter_table[]) {
+ errno = 0;
+
+ const char *filename = getenv("COMPCERT_PROFILING_DATA");
+ if (filename) {
+ if (!*filename) return;
+ } else {
+ filename = "compcert_profiling.dat";
+ }
+
+ FILE *fp = fopen(filename, "a");
+ //fprintf(stderr, "successfully opened profiling file\n");
+ if (fp == NULL) {
+ perror("open CompCert profiling data for writing");
+ return;
+ }
+
+ for(unsigned int i=0; i<nr_items; i++) {
+ write_id(fp, &id_table[i]);
+ write_counter(fp, counter_table[i][0]);
+ write_counter(fp, counter_table[i][1]);
+ }
+ //fprintf(stderr, "successfully written profiling file\n");
+
+ fclose(fp);
+ //fprintf(stderr, "successfully closed profiling file\n");
+ if (errno != 0) {
+ perror("write CompCert profiling data");
+ return;
+ }
+ // fprintf(stderr, "write CompCert profiling data: no error\n");
+}
diff --git a/runtime/include/ccomp_kvx_fixes.h b/runtime/include/ccomp_kvx_fixes.h
new file mode 100644
index 00000000..88fa22c3
--- /dev/null
+++ b/runtime/include/ccomp_kvx_fixes.h
@@ -0,0 +1,58 @@
+/* *************************************************************/
+/* */
+/* The Compcert verified compiler */
+/* */
+/* Sylvain Boulmé Grenoble-INP, VERIMAG */
+/* David Monniaux CNRS, VERIMAG */
+/* Cyril Six Kalray */
+/* */
+/* Copyright Kalray. Copyright VERIMAG. All rights reserved. */
+/* This file is distributed under the terms of the INRIA */
+/* Non-Commercial License Agreement. */
+/* */
+/* *************************************************************/
+
+
+#ifndef __CCOMP_KIC_FIXES_H
+#define __CCOMP_KIC_FIXES_H
+
+#if ! (defined(__COMPCERT__) && defined (__KVX__))
+#error This header is solely for CompCert on KVX
+#endif
+
+#undef __GNUC__
+#define __thread _Thread_local
+
+struct __int128_ccomp { long __int128_ccomp_low; long __int128_ccomp_high; };
+
+#define __int128 struct __int128_ccomp
+
+#define __builtin_kvx_acswapd __compcert_acswapd
+extern unsigned long __compcert_acswapd(void *address, unsigned long long new_value, unsigned long long old_value);
+
+#define __builtin_kvx_acswapw __compcert_acswapw
+extern unsigned int __compcert_acswapw(void *address, unsigned int new_value, unsigned int old_value);
+
+#define __builtin_kvx_aladdd __compcert_aladdd
+extern long long __compcert_aladdd(void *address, unsigned long long incr);
+
+#define __builtin_kvx_aladdw __compcert_aladdw
+extern int __compcert_aladdw(void *address, unsigned int incr);
+
+#define __builtin_kvx_afaddd __compcert_afaddd
+extern long long __compcert_afaddd(void *address, unsigned long long incr);
+
+#define __builtin_kvx_afaddw __compcert_afaddw
+extern int __compcert_afaddw(void *address, unsigned int incr);
+
+#define __builtin_kvx_ld __compcert_ld
+extern int __compcert_ld(void *address, const char *str, const int b);
+
+#define __builtin_kvx_lwz __compcert_lwz
+extern int __compcert_lwz(void *address, const char *str, const int b);
+
+/* #define __builtin_expect(x, y) (x) */
+#define __builtin_ctz(x) __builtin_kvx_ctzw(x)
+#define __builtin_clz(x) __builtin_kvx_clzw(x)
+
+#endif
diff --git a/runtime/include/math.h b/runtime/include/math.h
new file mode 100644
index 00000000..e7c9e475
--- /dev/null
+++ b/runtime/include/math.h
@@ -0,0 +1,40 @@
+/* *************************************************************/
+/* */
+/* The Compcert verified compiler */
+/* */
+/* Sylvain Boulmé Grenoble-INP, VERIMAG */
+/* David Monniaux CNRS, VERIMAG */
+/* Cyril Six Kalray */
+/* */
+/* Copyright Kalray. Copyright VERIMAG. All rights reserved. */
+/* This file is distributed under the terms of the INRIA */
+/* Non-Commercial License Agreement. */
+/* */
+/* *************************************************************/
+
+#ifndef _COMPCERT_MATH_H
+#define _COMPCERT_MATH_H
+
+#ifdef __KVX__
+
+#define isfinite(__y) (fpclassify((__y)) >= FP_ZERO)
+
+#include_next <math.h>
+
+#ifndef COMPCERT_NO_FP_MACROS
+#define fmin(x, y) __builtin_fmin((x),(y))
+#define fmax(x, y) __builtin_fmax((x),(y))
+#define fminf(x, y) __builtin_fminf((x),(y))
+#define fmaxf(x, y) __builtin_fmaxf((x),(y))
+#define fabs(x) __builtin_fabs((x))
+#define fabsf(x) __builtin_fabsf((x))
+#define fma(x, y, z) __builtin_fma((x),(y),(z))
+#define fmaf(x, y, z) __builtin_fmaf((x),(y),(z))
+#endif
+
+#else
+
+#include_next <math.h>
+
+#endif
+#endif
diff --git a/runtime/kvx/Makefile b/runtime/kvx/Makefile
new file mode 100644
index 00000000..4e47f567
--- /dev/null
+++ b/runtime/kvx/Makefile
@@ -0,0 +1,15 @@
+CCOMP ?= ../../ccomp
+CFLAGS ?= -O2 -D__K1_TINYK1__
+
+CFILES=$(wildcard *.c)
+SFILES=$(subst .c,.s,$(CFILES))
+
+CCOMPPATH=$(shell which $(CCOMP))
+
+all: $(SFILES)
+
+.SECONDARY:
+%.s: %.c $(CCOMPPATH)
+ $(CCOMP) $(CFLAGS) -S $< -o $@
+ sed -i -e 's/i64_/__compcert_i64_/g' -e 's/i32_/__compcert_i32_/g' \
+ -e 's/f64_/__compcert_f64_/g' -e 's/f32_/__compcert_f32_/g' $@
diff --git a/runtime/kvx/ccomp_kvx_fixes.h b/runtime/kvx/ccomp_kvx_fixes.h
new file mode 120000
index 00000000..62334d46
--- /dev/null
+++ b/runtime/kvx/ccomp_kvx_fixes.h
@@ -0,0 +1 @@
+../c/ccomp_kvx_fixes.h \ No newline at end of file
diff --git a/runtime/kvx/i32_divmod.s b/runtime/kvx/i32_divmod.s
new file mode 100644
index 00000000..9a6f0bce
--- /dev/null
+++ b/runtime/kvx/i32_divmod.s
@@ -0,0 +1,120 @@
+/* KVX
+32-bit unsigned/signed integer division/modulo (udiv5)
+
+D. Monniaux, CNRS, VERIMAG */
+
+
+ .globl __compcert_i32_sdiv_fp
+__compcert_i32_sdiv_fp:
+ compw.lt $r2 = $r0, 0
+ compw.lt $r3 = $r1, 0
+ absw $r0 = $r0
+ absw $r1 = $r1
+ ;;
+ xord $r2 = $r2, $r3
+ make $r3 = 0
+ goto __compcert_i32_divmod_fp
+ ;;
+
+ .globl __compcert_i32_smod_fp
+__compcert_i32_smod_fp:
+ compw.lt $r2 = $r0, 0
+ absw $r0 = $r0
+ absw $r1 = $r1
+ make $r3 = 1
+ goto __compcert_i32_divmod_fp
+ ;;
+
+ .globl __compcert_i32_umod_fp
+__compcert_i32_umod_fp:
+ make $r2 = 0
+ make $r3 = 1
+ goto __compcert_i32_divmod_fp
+ ;;
+
+ .globl __compcert_i32_udiv_fp
+__compcert_i32_udiv_fp:
+ make $r2 = 0
+ make $r3 = 0
+ ;;
+
+/*
+r0 : a
+r1 : b
+r2 : negate result?
+r3 : return mod?
+*/
+
+ .globl __compcert_i32_divmod_fp
+__compcert_i32_divmod_fp:
+ zxwd $r7 = $r1
+ zxwd $r1 = $r0
+#ifndef NO_SHORTCUT
+ compw.ltu $r8 = $r0, $r1
+ cb.weqz $r1? .ERR # return 0 if divide by 0
+#endif
+ ;;
+# a in r1, b in r7
+ floatud.rn.s $r5 = $r7, 0
+#ifndef NO_SHORTCUT
+ compd.eq $r8 = $r7, 1
+ cb.wnez $r8? .LESS # shortcut if a < b
+#endif
+ ;;
+# b (double) in r5
+ make $r6 = 0x3ff0000000000000 # 1.0
+ fnarrowdw.rn.s $r11 = $r5
+# cb.wnez $r8, .RET1 # if b=1
+ ;;
+# b (single) in r11
+ floatud.rn.s $r10 = $r1, 0
+ finvw.rn.s $r11 = $r11
+ ;;
+ fwidenlwd.s $r11 = $r11
+ ;;
+# invb0 in r11
+ copyd $r9 = $r11
+ ffmsd.rn.s $r6 = $r11, $r5
+# alpha in r6
+ ;;
+ ffmad.rn.s $r9 = $r11, $r6
+# 1/b in r9
+ ;;
+ fmuld.rn.s $r0 = $r10, $r9
+# a/b in r1
+ ;;
+ fixedud.rn.s $r0 = $r0, 0
+ ;;
+ msbfd $r1 = $r0, $r7
+ ;;
+ addd $r6 = $r0, -1
+ addd $r8 = $r1, $r7
+ ;;
+ cmoved.dltz $r1? $r0 = $r6
+ cmoved.dltz $r1? $r1 = $r8
+ ;;
+ negw $r4 = $r0
+ negw $r5 = $r1
+ ;;
+ cmoved.wnez $r2? $r0 = $r4
+ cmoved.wnez $r2? $r1 = $r5
+ ;;
+.END:
+ cmoved.wnez $r3? $r0 = $r1
+ ret
+ ;;
+#ifndef NO_SHORTCUT
+
+.LESS:
+ make $r0 = 0
+ negw $r5 = $r1
+ ;;
+ cmoved.wnez $r2? $r1 = $r5
+ goto .END
+ ;;
+
+.ERR:
+ make $r0 = 0
+ ret
+ ;;
+#endif
diff --git a/runtime/kvx/i64_sdiv.c b/runtime/kvx/i64_sdiv.c
new file mode 100644
index 00000000..a42164cc
--- /dev/null
+++ b/runtime/kvx/i64_sdiv.c
@@ -0,0 +1,23 @@
+extern long __divdi3 (long a, long b);
+
+int i32_sdiv (int a, int b)
+{
+ return __divdi3 (a, b);
+}
+
+#ifdef OUR_OWN_FE_EXCEPT
+#include <../../k1-cos/include/hal/cos_registers.h>
+
+/* DM FIXME this is for floating point */
+int fetestexcept(int excepts) {
+ int mask = (COS_SFR_CS_IO_MASK | COS_SFR_CS_DZ_MASK | COS_SFR_CS_OV_MASK | COS_SFR_CS_UN_MASK | COS_SFR_CS_IN_MASK) & excepts;
+ unsigned long long cs = __builtin_kvx_get(COS_SFR_CS);
+ return cs & mask;
+}
+
+int feclearexcept(int excepts) {
+ int mask = (COS_SFR_CS_IO_MASK | COS_SFR_CS_DZ_MASK | COS_SFR_CS_OV_MASK | COS_SFR_CS_UN_MASK | COS_SFR_CS_IN_MASK) & excepts;
+ __builtin_kvx_wfxl(COS_SFR_CS, mask);
+ return 0;
+}
+#endif
diff --git a/runtime/kvx/i64_smod.c b/runtime/kvx/i64_smod.c
new file mode 100644
index 00000000..3371eecf
--- /dev/null
+++ b/runtime/kvx/i64_smod.c
@@ -0,0 +1,5 @@
+extern long __moddi3 (long a, long b);
+int i32_smod (int a, int b)
+{
+ return __moddi3 (a, b);
+}
diff --git a/runtime/kvx/i64_udiv.c b/runtime/kvx/i64_udiv.c
new file mode 100644
index 00000000..75f4bbf5
--- /dev/null
+++ b/runtime/kvx/i64_udiv.c
@@ -0,0 +1,6 @@
+extern unsigned long __udivdi3 (unsigned long a, unsigned long b);
+
+unsigned i32_udiv (unsigned a, unsigned b)
+{
+ return __udivdi3 (a, b);
+}
diff --git a/runtime/kvx/i64_udivmod.c b/runtime/kvx/i64_udivmod.c
new file mode 100644
index 00000000..952e47e5
--- /dev/null
+++ b/runtime/kvx/i64_udivmod.c
@@ -0,0 +1,30 @@
+#if 0
+/* THIS IS THE PREVIOUS VERSION, USED ON BOSTAN AND ANDEY */
+unsigned long long
+udivmoddi4(unsigned long long num, unsigned long long den, int modwanted)
+{
+ unsigned long long r = num, q = 0;
+
+ if(den <= r) {
+ unsigned k = __builtin_clzll (den) - __builtin_clzll (r);
+ den = den << k;
+ if(r >= den) {
+ r = r - den;
+ q = 1LL << k;
+ }
+ if(k != 0) {
+ unsigned i = k;
+ den = den >> 1;
+ do {
+ r = __builtin_kvx_stsud (den, r);
+ i--;
+ } while (i!= 0);
+ q = q + r;
+ r = r >> k;
+ q = q - (r << k);
+ }
+ }
+
+ return modwanted ? r : q;
+}
+#endif
diff --git a/runtime/kvx/i64_udivmod_stsud.s b/runtime/kvx/i64_udivmod_stsud.s
new file mode 100644
index 00000000..2dd73d66
--- /dev/null
+++ b/runtime/kvx/i64_udivmod_stsud.s
@@ -0,0 +1,218 @@
+/*
+Integer division for KVX
+
+David Monniaux, CNRS / Verimag
+ */
+
+ .globl dm_udivmoddi4
+dm_udivmoddi4:
+ sxwd $r2 = $r2
+ make $r5 = 0
+ compd.ltu $r3 = $r0, $r1
+ ;;
+
+ clzd $r3 = $r1
+ clzd $r4 = $r0
+ cb.dnez $r3? .L74
+ ;;
+
+ sbfw $r4 = $r4, $r3
+ ;;
+
+ zxwd $r3 = $r4
+ slld $r1 = $r1, $r4
+ ;;
+
+ compd.ltu $r6 = $r0, $r1
+ ;;
+
+ cb.dnez $r6? .L4C
+ ;;
+
+ make $r5 = 1
+ sbfd $r0 = $r1, $r0
+ ;;
+
+ slld $r5 = $r5, $r4
+ ;;
+
+.L4C:
+ cb.deqz $r3? .L74
+ ;;
+
+ srld $r1 = $r1, 1
+ zxwd $r3 = $r4
+ ;;
+
+ loopdo $r3, .LOOP
+ ;;
+
+ stsud $r0 = $r1, $r0
+ ;;
+
+.LOOP:
+ addd $r5 = $r0, $r5
+ srld $r0 = $r0, $r4
+ ;;
+
+ slld $r4 = $r0, $r4
+ ;;
+
+ sbfd $r5 = $r4, $r5
+ ;;
+
+.L74:
+ cmoved.deqz $r2? $r0 = $r5
+ ret
+ ;;
+
+/*
+r0 : a
+r1 : b
+r2 : negate result?
+r3 : return mod?
+*/
+
+ .globl __compcert_i32_sdiv_stsud
+__compcert_i32_sdiv_stsud:
+ compw.lt $r2 = $r0, 0
+ compw.lt $r3 = $r1, 0
+ absw $r0 = $r0
+ absw $r1 = $r1
+ ;;
+ zxwd $r0 = $r0
+ zxwd $r1 = $r1
+ xord $r2 = $r2, $r3
+ make $r3 = 0
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i32_smod_stsud
+__compcert_i32_smod_stsud:
+ compw.lt $r2 = $r0, 0
+ absw $r0 = $r0
+ absw $r1 = $r1
+ make $r3 = 1
+ ;;
+ zxwd $r0 = $r0
+ zxwd $r1 = $r1
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i32_umod_stsud
+__compcert_i32_umod_stsud:
+ make $r2 = 0
+ make $r3 = 1
+ zxwd $r0 = $r0
+ zxwd $r1 = $r1
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i32_udiv_stsud
+__compcert_i32_udiv_stsud:
+ make $r2 = 0
+ make $r3 = 0
+ zxwd $r0 = $r0
+ zxwd $r1 = $r1
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i64_umod_stsud
+__compcert_i64_umod_stsud:
+ make $r2 = 0
+ make $r3 = 1
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i64_udiv_stsud
+__compcert_i64_udiv_stsud:
+ make $r2 = 0
+ make $r3 = 0
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i64_sdiv_stsud
+__compcert_i64_sdiv_stsud:
+ compd.lt $r2 = $r0, 0
+ compd.lt $r3 = $r1, 0
+ absd $r0 = $r0
+ absd $r1 = $r1
+ ;;
+ xord $r2 = $r2, $r3
+ make $r3 = 0
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i64_smod_stsud
+__compcert_i64_smod_stsud:
+ compd.lt $r2 = $r0, 0
+ absd $r0 = $r0
+ absd $r1 = $r1
+ make $r3 = 1
+ goto __compcert_i64_divmod_stsud
+ ;;
+
+ .globl __compcert_i64_divmod_stsud
+__compcert_i64_divmod_stsud:
+ make $r5 = 0
+ compd.ltu $r7 = $r0, $r1
+ ;;
+
+ clzd $r7 = $r1
+ clzd $r4 = $r0
+ cb.dnez $r7? .ZL74
+ ;;
+
+ sbfw $r4 = $r4, $r7
+ ;;
+
+ zxwd $r7 = $r4
+ slld $r1 = $r1, $r4
+ ;;
+
+ compd.ltu $r6 = $r0, $r1
+ ;;
+
+ cb.dnez $r6? .ZL4C
+ ;;
+
+ make $r5 = 1
+ sbfd $r0 = $r1, $r0
+ ;;
+
+ slld $r5 = $r5, $r4
+ ;;
+
+.ZL4C:
+ cb.deqz $r7? .ZL74
+ ;;
+
+ srld $r1 = $r1, 1
+ zxwd $r7 = $r4
+ ;;
+
+ loopdo $r7, .ZLOOP
+ ;;
+
+ stsud $r0 = $r1, $r0
+ ;;
+
+.ZLOOP:
+ addd $r5 = $r0, $r5
+ srld $r0 = $r0, $r4
+ ;;
+
+ slld $r4 = $r0, $r4
+ ;;
+
+ sbfd $r5 = $r4, $r5
+ ;;
+
+.ZL74:
+ cmoved.weqz $r3? $r0 = $r5
+ ;;
+ negd $r5 = $r0
+ ;;
+ cmoved.wnez $r2? $r0 = $r5
+ ret
+ ;;
diff --git a/runtime/kvx/i64_umod.c b/runtime/kvx/i64_umod.c
new file mode 100644
index 00000000..59e35960
--- /dev/null
+++ b/runtime/kvx/i64_umod.c
@@ -0,0 +1,6 @@
+extern unsigned long __umoddi3 (unsigned long a, unsigned long b);
+
+unsigned i32_umod (unsigned a, unsigned b)
+{
+ return __umoddi3 (a, b);
+}
diff --git a/runtime/kvx/vararg.s b/runtime/kvx/vararg.s
new file mode 100644
index 00000000..65c1eab8
--- /dev/null
+++ b/runtime/kvx/vararg.s
@@ -0,0 +1,54 @@
+
+# typedef void * va_list;
+# unsigned int __compcert_va_int32(va_list * ap);
+# unsigned long long __compcert_va_int64(va_list * ap);
+
+ .text
+ .balign 2
+ .globl __compcert_va_int32
+__compcert_va_int32:
+ ld $r32 = 0[$r0] # $r32 <- *ap
+;;
+ addd $r32 = $r32, 8 # $r32 <- $r32 + WORDSIZE
+;;
+ sd 0[$r0] = $r32 # *ap <- $r32
+;;
+ lws $r0 = -8[$r32] # retvalue <- 32-bits at *ap - WORDSIZE
+ ret
+;;
+
+ .text
+ .balign 2
+ .globl __compcert_va_int64
+ .globl __compcert_va_float64
+ .globl __compcert_va_composite
+__compcert_va_int64:
+__compcert_va_float64:
+# FIXME this assumes pass-by-reference
+__compcert_va_composite:
+# Prologue
+ ld $r32 = 0[$r0] # $r32 <- *ap
+;;
+ addd $r32 = $r32, 8 # $r32 <- $r32 + WORDSIZE
+;;
+ sd 0[$r0] = $r32 # *ap <- $r32
+;;
+ ld $r0 = -8[$r32] # retvalue <- 64-bits at *ap - WORDSIZE
+ ret
+;;
+
+# FIXME this assumes pass-by-reference
+ .globl __compcert_acswapd
+__compcert_acswapd:
+ acswapd 0[$r1] = $r2r3
+ ;;
+ sq 0[$r0] = $r2r3
+ ret
+ ;;
+ .globl __compcert_acswapw
+__compcert_acswapw:
+ acswapw 0[$r1] = $r2r3
+ ;;
+ sq 0[$r0] = $r2r3
+ ret
+ ;;
diff --git a/scheduling/BTL.v b/scheduling/BTL.v
new file mode 100644
index 00000000..f832085c
--- /dev/null
+++ b/scheduling/BTL.v
@@ -0,0 +1,758 @@
+(** The BTL intermediate language: abstract syntax and semantics.
+
+ BTL stands for "Block Transfer Language".
+
+ Informally, a block is a piece of "loop-free" code, with a single entry-point,
+ hence, such that transformation preserving locally the semantics of each block,
+ preserve also globally the semantics of the function.
+
+ a BTL function is a CFG where each node is such a block, represented by structured code.
+
+ BTL gives a structured view of RTL code.
+ It is dedicated to optimizations validated by "symbolic simulation" over blocks.
+
+
+*)
+
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad.
+Require Import Lia.
+
+Import ListNotations.
+
+(** * Abstract syntax *)
+
+Definition exit := node. (* we may generalize this with register renamings at exit,
+ like in "phi" nodes of SSA-form *)
+
+(* inst_info is a ghost record to provide instruction information through oracles *)
+Parameter inst_info: Set.
+Extract Constant inst_info => "BTLtypes.inst_info".
+
+(* block_info is a ghost record to provide block information through oracles *)
+Parameter block_info: Set.
+Extract Constant block_info => "BTLtypes.block_info".
+
+(** final instructions (that stops block execution) *)
+Inductive final: Type :=
+ | Bgoto (succ:exit) (** No operation -- just branch to [succ]. *)
+ | Breturn (res: option reg)
+ (** terminates the execution of the current function. It returns the value of the given
+ register, or [Vundef] if none is given. *)
+ | Bcall (sig: signature) (fn: reg + ident) (args:list reg) (dest:reg) (succ:exit)
+ (** invokes the function determined by [fn] (either a function pointer found in a register or a
+ function name), giving it the values of registers [args] as arguments.
+ It stores the return value in [dest] and branches to [succ]. *)
+ | Btailcall (sig:signature) (fn: reg + ident) (args: list reg)
+ (** performs a function invocation in tail-call position
+ (the current function terminates after the call, returning the result of the callee)
+ *)
+ | Bbuiltin (ef:external_function) (args:list (builtin_arg reg)) (dest:builtin_res reg) (succ:exit)
+ (** calls the built-in function identified by [ef], giving it the values of [args] as arguments.
+ It stores the return value in [dest] and branches to [succ]. *)
+ | Bjumptable (arg:reg) (tbl:list exit)
+ (** [Bjumptable arg tbl] transitions to the node that is the [n]-th
+ element of the list [tbl], where [n] is the unsigned integer value of register [arg]. *)
+ .
+
+(* instruction block *)
+Inductive iblock: Type :=
+(* final instructions that stops block execution *)
+ | BF (fi: final) (iinfo: inst_info)
+(* basic instructions that continues block execution, except when aborting *)
+ | Bnop (oiinfo: option inst_info) (* nop instruction *)
+ | Bop (op:operation) (args:list reg) (dest:reg) (iinfo: inst_info)
+ (** performs the arithmetic operation [op] over the values of registers [args], stores the result in [dest] *)
+ | Bload (trap:trapping_mode) (chunk:memory_chunk) (addr:addressing) (args:list reg) (dest:reg) (iinfo: inst_info)
+ (** loads a [chunk] quantity from the address determined by the addressing mode [addr]
+ and the values of the [args] registers, stores the quantity just read into [dest].
+ If trap=NOTRAP, then failures lead to a default value written to [dest]. *)
+ | Bstore (chunk:memory_chunk) (addr:addressing) (args:list reg) (src:reg) (iinfo: inst_info)
+ (** stores the value of register [src] in the [chunk] quantity at the
+ the address determined by the addressing mode [addr] and the
+ values of the [args] registers. *)
+(* composed instructions *)
+ | Bseq (b1 b2: iblock)
+ (** starts by running [b1] and stops here if execution of [b1] has reached a final instruction or aborted
+ or continue with [b2] otherwise *)
+ | Bcond (cond:condition) (args:list reg) (ifso ifnot: iblock) (iinfo: inst_info)
+ (** evaluates the boolean condition [cond] over the values of registers [args].
+ If the condition is true, it continues on [ifso].
+ If the condition is false, it continues on [ifnot].
+ [info] is a ghost field there to provide information relative to branch prediction. *)
+ .
+Coercion BF: final >-> Funclass.
+
+
+(** NB: - a RTL [(Inop pc)] ending a branch of block is encoded by [(Bseq Bnop (Bgoto pc))].
+ - a RTL [(Inop pc)] in the middle of a branch is simply encoded by [Bnop].
+ - the same trick appears for all "basic" instructions and [Icond].
+*)
+
+Record iblock_info := {
+ entry: iblock;
+ input_regs: Regset.t; (* extra liveness information for BTL functional semantics *)
+ binfo: block_info (* Ghost field used in oracles *)
+}.
+
+Definition code: Type := PTree.t iblock_info.
+
+Record function: Type := mkfunction {
+ fn_sig: signature;
+ fn_params: list reg;
+ fn_stacksize: Z;
+ fn_code: code;
+ fn_entrypoint: node
+}.
+
+(** A function description comprises a control-flow graph (CFG) [fn_code]
+ (a partial finite mapping from nodes to instructions). As in Cminor,
+ [fn_sig] is the function signature and [fn_stacksize] the number of bytes
+ for its stack-allocated activation record. [fn_params] is the list
+ of registers that are bound to the values of arguments at call time.
+ [fn_entrypoint] is the node of the first instruction of the function
+ in the CFG. *)
+
+Definition fundef := AST.fundef function.
+
+Definition program := AST.program fundef unit.
+
+Definition funsig (fd: fundef) :=
+ match fd with
+ | Internal f => fn_sig f
+ | External ef => ef_sig ef
+ end.
+
+(** * Operational semantics *)
+
+Definition genv := Genv.t fundef unit.
+
+(** The dynamic semantics of BTL is similar to RTL,
+ except that the step of one instruction is generalized into the run of one [iblock].
+*)
+
+Inductive stackframe : Type :=
+ | Stackframe:
+ forall (res: reg) (**r where to store the result *)
+ (f: function) (**r calling function *)
+ (sp: val) (**r stack pointer in calling function *)
+ (succ: exit) (**r program point in calling function *)
+ (rs: regset), (**r register state in calling function *)
+ stackframe.
+
+Inductive state : Type :=
+ | State:
+ forall (stack: list stackframe) (**r call stack *)
+ (f: function) (**r current function *)
+ (sp: val) (**r stack pointer *)
+ (pc: node) (**r current program point in [c] *)
+ (rs: regset) (**r register state *)
+ (m: mem), (**r memory state *)
+ state
+ | Callstate:
+ forall (stack: list stackframe) (**r call stack *)
+ (f: fundef) (**r function to call *)
+ (args: list val) (**r arguments to the call *)
+ (m: mem), (**r memory state *)
+ state
+ | Returnstate:
+ forall (stack: list stackframe) (**r call stack *)
+ (v: val) (**r return value for the call *)
+ (m: mem), (**r memory state *)
+ state.
+
+(** outcome of a block execution *)
+Record outcome := out {
+ _rs: regset;
+ _m: mem;
+ _fin: option final
+}.
+
+(* follows RTL semantics to set the result of builtin *)
+Definition reg_builtin_res (res: builtin_res reg): option reg :=
+ match res with
+ | BR r => Some r
+ | _ => None
+ end.
+
+Section RELSEM.
+
+(** [step] (and in particular [final_step]) is parametrized by function [tr_exit] to transfer registers on each iblock exit.
+
+ In particular, [tr_exit f lpc or rs] computes from [rs] the [rs'] on which the execution from iblock [pc] in [lpc] in [f] may start.
+
+ Here, [or] is an optional register that will be assigned after exiting the iblock, but before entering in [pc]: e.g. the register set by a function call
+ before entering in return address.
+
+ See [tr_inputs] implementation below.
+
+*)
+
+Variable tr_exit: function -> list exit -> option reg -> regset -> regset.
+
+Variable ge: genv.
+
+Definition find_function (ros: reg + ident) (rs: regset) : option fundef :=
+ match ros with
+ | inl r => Genv.find_funct ge rs#r
+ | inr symb =>
+ match Genv.find_symbol ge symb with
+ | None => None
+ | Some b => Genv.find_funct_ptr ge b
+ end
+ end.
+
+Local Open Scope option_monad_scope.
+
+(* TODO: a new (hopefully simpler) scheme to support "NOTRAP" wrt current scheme of RTL *)
+
+Inductive has_loaded sp rs m chunk addr args v: trapping_mode -> Prop :=
+ | has_loaded_normal a trap
+ (EVAL: eval_addressing ge sp addr rs##args = Some a)
+ (LOAD: Mem.loadv chunk m a = Some v)
+ : has_loaded sp rs m chunk addr args v trap
+ | has_loaded_default
+ (LOAD: forall a, eval_addressing ge sp addr rs##args = Some a -> Mem.loadv chunk m a = None)
+ (DEFAULT: v = Vundef)
+ : has_loaded sp rs m chunk addr args v NOTRAP
+ .
+Local Hint Constructors has_loaded: core.
+
+(* TODO: move this scheme in "Memory" module if this scheme is useful ! *)
+
+(** internal big-step execution of one iblock *)
+Inductive iblock_istep sp: regset -> mem -> iblock -> regset -> mem -> option final -> Prop :=
+ | exec_final rs m fin iinfo: iblock_istep sp rs m (BF fin iinfo) rs m (Some fin)
+| exec_nop rs m oiinfo: iblock_istep sp rs m (Bnop oiinfo) rs m None
+ | exec_op rs m op args res v iinfo
+ (EVAL: eval_operation ge sp op rs##args m = Some v)
+ : iblock_istep sp rs m (Bop op args res iinfo) (rs#res <- v) m None
+ | exec_load rs m trap chunk addr args dst v iinfo
+ (LOAD: has_loaded sp rs m chunk addr args v trap)
+ : iblock_istep sp rs m (Bload trap chunk addr args dst iinfo) (rs#dst <- v) m None
+ | exec_store rs m chunk addr args src a m' iinfo
+ (EVAL: eval_addressing ge sp addr rs##args = Some a)
+ (STORE: Mem.storev chunk m a rs#src = Some m')
+ : iblock_istep sp rs m (Bstore chunk addr args src iinfo) rs m' None
+ | exec_seq_stop rs m b1 b2 rs' m' fin
+ (EXEC: iblock_istep sp rs m b1 rs' m' (Some fin))
+ : iblock_istep sp rs m (Bseq b1 b2) rs' m' (Some fin)
+ | exec_seq_continue rs m b1 b2 rs1 m1 rs' m' ofin
+ (EXEC1: iblock_istep sp rs m b1 rs1 m1 None)
+ (EXEC2: iblock_istep sp rs1 m1 b2 rs' m' ofin)
+ : iblock_istep sp rs m (Bseq b1 b2) rs' m' ofin
+ | exec_cond rs m cond args ifso ifnot b rs' m' ofin iinfo
+ (EVAL: eval_condition cond rs##args m = Some b)
+ (EXEC: iblock_istep sp rs m (if b then ifso else ifnot) rs' m' ofin)
+ : iblock_istep sp rs m (Bcond cond args ifso ifnot iinfo) rs' m' ofin
+ .
+Local Hint Constructors iblock_istep: core.
+
+(** A functional variant of [iblock_istep_run] of [iblock_istep].
+Lemma [iblock_istep_run_equiv] below provides a proof that "relation" [iblock_istep] is a "partial function".
+*)
+Fixpoint iblock_istep_run sp ib rs m: option outcome :=
+ match ib with
+ | BF fin _ =>
+ Some {| _rs := rs; _m := m; _fin := Some fin |}
+ (* basic instructions *)
+ | Bnop _ =>
+ Some {| _rs := rs; _m:= m; _fin := None |}
+ | Bop op args res _ =>
+ SOME v <- eval_operation ge sp op rs##args m IN
+ Some {| _rs := rs#res <- v; _m:= m; _fin := None |}
+ | Bload TRAP chunk addr args dst _ =>
+ SOME a <- eval_addressing ge sp addr rs##args IN
+ SOME v <- Mem.loadv chunk m a IN
+ Some {| _rs := rs#dst <- v; _m:= m; _fin := None |}
+ | Bload NOTRAP chunk addr args dst _ =>
+ match eval_addressing ge sp addr rs##args with
+ | Some a =>
+ match Mem.loadv chunk m a with
+ | Some v => Some {| _rs := rs#dst <- v; _m:= m; _fin := None |}
+ | None =>
+ Some {| _rs := rs#dst <- Vundef; _m:= m; _fin := None |}
+ end
+ | None =>
+ Some {| _rs := rs#dst <- Vundef; _m:= m; _fin := None |}
+ end
+ | Bstore chunk addr args src _ =>
+ SOME a <- eval_addressing ge sp addr rs##args IN
+ SOME m' <- Mem.storev chunk m a rs#src IN
+ Some {| _rs := rs; _m:= m'; _fin := None |}
+ (* composed instructions *)
+ | Bseq b1 b2 =>
+ SOME out1 <- iblock_istep_run sp b1 rs m IN
+ match out1.(_fin) with
+ | None => iblock_istep_run sp b2 out1.(_rs) out1.(_m)
+ | _ => Some out1 (* stop execution on the 1st final instruction *)
+ end
+ | Bcond cond args ifso ifnot _ =>
+ SOME b <- eval_condition cond rs##args m IN
+ iblock_istep_run sp (if b then ifso else ifnot) rs m
+ end.
+
+Lemma iblock_istep_run_equiv_load sp ib v rs rs' m trap chunk addr args dst iinfo ofin:
+ ib = (Bload trap chunk addr args dst iinfo) ->
+ rs' = rs # dst <- v ->
+ has_loaded sp rs m chunk addr args v trap ->
+ iblock_istep sp rs m ib rs' m ofin <->
+ iblock_istep_run sp ib rs m = Some {| _rs := rs'; _m := m; _fin := ofin |}.
+Proof.
+ split; subst; inv H1; simpl in *; intros.
+ - destruct trap; inv H; simpl in *; repeat autodestruct.
+ - inv H; autodestruct; rewrite LOAD; auto.
+ - destruct trap; inv H; simpl in *;
+ rewrite EVAL, LOAD in H1; inv H1; repeat econstructor; eauto.
+ - generalize H; autodestruct.
+ + rewrite LOAD in *; inv H; auto; constructor.
+ eapply has_loaded_default; eauto; try_simplify_someHyps.
+ + inv H; constructor. eapply has_loaded_default; eauto; try_simplify_someHyps.
+Qed.
+Local Hint Resolve iblock_istep_run_equiv_load: core.
+
+Lemma iblock_istep_run_equiv sp rs m ib rs' m' ofin:
+ iblock_istep sp rs m ib rs' m' ofin <-> iblock_istep_run sp ib rs m = Some {| _rs := rs'; _m:= m'; _fin := ofin |}.
+Proof.
+ constructor.
+ - induction 1; try (eapply iblock_istep_run_equiv_load; eauto; fail);
+ simpl; try autodestruct; try_simplify_someHyps.
+ - generalize rs m rs' m' ofin; clear rs m rs' m' ofin.
+ induction ib; simpl; repeat (try autodestruct; try_simplify_someHyps).
+ 1,2: constructor; eapply has_loaded_default; try_simplify_someHyps.
+ destruct o; try_simplify_someHyps; subst; eauto.
+Qed.
+
+Local Open Scope list_scope.
+
+Inductive final_step stack f sp rs m: final -> trace -> state -> Prop :=
+ | exec_Bgoto pc:
+ final_step stack f sp rs m (Bgoto pc) E0
+ (State stack f sp pc (tr_exit f [pc] None rs) m)
+ | exec_Breturn or stk m':
+ sp = (Vptr stk Ptrofs.zero) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ final_step stack f sp rs m (Breturn or)
+ E0 (Returnstate stack (regmap_optget or Vundef rs) m')
+ | exec_Bcall sig ros args res pc' fd:
+ find_function ros rs = Some fd ->
+ funsig fd = sig ->
+ final_step stack f sp rs m (Bcall sig ros args res pc')
+ E0 (Callstate (Stackframe res f sp pc' (tr_exit f [pc'] (Some res) rs) :: stack) fd rs##args m)
+ | exec_Btailcall sig ros args stk m' fd:
+ find_function ros rs = Some fd ->
+ funsig fd = sig ->
+ sp = (Vptr stk Ptrofs.zero) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ final_step stack f sp rs m (Btailcall sig ros args)
+ E0 (Callstate stack fd rs##args m')
+ | exec_Bbuiltin ef args res pc' vargs t vres m':
+ eval_builtin_args ge (fun r => rs#r) sp m args vargs ->
+ external_call ef ge vargs m t vres m' ->
+ final_step stack f sp rs m (Bbuiltin ef args res pc')
+ t (State stack f sp pc' (regmap_setres res vres (tr_exit f [pc'] (reg_builtin_res res) rs)) m')
+ | exec_Bjumptable arg tbl n pc':
+ rs#arg = Vint n ->
+ list_nth_z tbl (Int.unsigned n) = Some pc' ->
+ final_step stack f sp rs m (Bjumptable arg tbl)
+ E0 (State stack f sp pc' (tr_exit f tbl None rs) m)
+.
+
+(** big-step execution of one iblock *)
+Definition iblock_step stack f sp rs m ib t s: Prop :=
+ exists rs' m' fin, iblock_istep sp rs m ib rs' m' (Some fin) /\ final_step stack f sp rs' m' fin t s.
+
+(** The transitions are presented as an inductive predicate
+ [step ge st1 t st2], where [ge] is the global environment,
+ [st1] the initial state, [st2] the final state, and [t] the trace
+ of system calls performed during this transition. *)
+
+Inductive step: state -> trace -> state -> Prop :=
+ | exec_iblock stack ib f sp pc rs m t s
+ (PC: (fn_code f)!pc = Some ib)
+ (STEP: iblock_step stack f sp rs m ib.(entry) t s)
+ :step (State stack f sp pc rs m) t s
+ | exec_function_internal stack f args m m' stk
+ (ALLOC: Mem.alloc m 0 f.(fn_stacksize) = (m', stk))
+ :step (Callstate stack (Internal f) args m)
+ E0 (State stack
+ f
+ (Vptr stk Ptrofs.zero)
+ f.(fn_entrypoint)
+ (init_regs args f.(fn_params))
+ m')
+ | exec_function_external stack ef args res t m m'
+ (EXTCALL: external_call ef ge args m t res m')
+ :step (Callstate stack (External ef) args m)
+ t (Returnstate stack res m')
+ | exec_return stack res f sp pc rs vres m
+ :step (Returnstate (Stackframe res f sp pc rs :: stack) vres m)
+ E0 (State stack f sp pc (rs#res <- vres) m)
+.
+
+End RELSEM.
+
+(** Execution of whole programs are described as sequences of transitions
+ from an initial state to a final state. An initial state is a [Callstate]
+ corresponding to the invocation of the ``main'' function of the program
+ without arguments and with an empty call stack. *)
+
+Inductive initial_state (p: program): state -> Prop :=
+ | initial_state_intro: forall b f m0,
+ let ge := Genv.globalenv p in
+ Genv.init_mem p = Some m0 ->
+ Genv.find_symbol ge p.(prog_main) = Some b ->
+ Genv.find_funct_ptr ge b = Some f ->
+ funsig f = signature_main ->
+ initial_state p (Callstate nil f nil m0).
+
+(** A final state is a [Returnstate] with an empty call stack. *)
+
+Inductive final_state: state -> int -> Prop :=
+ | final_state_intro: forall r m,
+ final_state (Returnstate nil (Vint r) m) r.
+
+(** The full "functional" small-step semantics for a BTL program.
+ at each exit, we only transfer register in "input_regs" (i.e. "alive" registers).
+*)
+Definition transfer_regs (inputs: list reg) (rs: regset): regset :=
+ init_regs (rs##inputs) inputs.
+
+Lemma transfer_regs_inputs inputs rs r:
+ List.In r inputs -> (transfer_regs inputs rs)#r = rs#r.
+Proof.
+ unfold transfer_regs; induction inputs; simpl; intuition subst.
+ - rewrite Regmap.gss; auto.
+ - destruct (Pos.eq_dec a r).
+ + subst; rewrite Regmap.gss; auto.
+ + rewrite Regmap.gso; auto.
+Qed.
+
+Lemma transfer_regs_dead inputs rs r:
+ ~List.In r inputs -> (transfer_regs inputs rs)#r = Vundef.
+Proof.
+ unfold transfer_regs; induction inputs; simpl; intuition subst.
+ - rewrite Regmap.gi; auto.
+ - rewrite Regmap.gso; auto.
+Qed.
+
+Fixpoint union_inputs (f:function) (tbl: list exit): Regset.t :=
+ match tbl with
+ | nil => Regset.empty
+ | pc::l => let rs:= union_inputs f l in
+ match f.(fn_code)!pc with
+ | None => rs
+ | Some ib => Regset.union rs ib.(input_regs)
+ end
+ end.
+
+(* TODO: lemma not yet used
+
+Lemma union_inputs_In f tbl r:
+ Regset.In r (union_inputs f tbl) -> (exists pc, List.In pc tbl /\ exists ib, f.(fn_code)!pc = Some ib /\ Regset.In r ib.(input_regs)).
+Proof.
+ induction tbl as [|pc l]; simpl; intros.
+ - exploit Regset.empty_1; eauto. intuition.
+ - destruct ((fn_code f) ! pc) eqn:ATpc.
+ + exploit Regset.union_1; eauto.
+ destruct 1 as [X1|X1].
+ * destruct IHl as (pc' & H1 & H2); eauto.
+ * eexists; intuition eauto.
+ + destruct IHl as (pc' & H1 & H2); eauto.
+Qed.
+
+Lemma union_inputs_notIn f tbl r:
+ (forall pc ib, List.In pc tbl -> f.(fn_code)!pc = Some ib -> ~Regset.In r ib.(input_regs))
+ -> ~Regset.In r (union_inputs f tbl).
+Proof.
+ induction tbl as [|pc l]; simpl; intuition eauto.
+ - exploit Regset.empty_1; eauto.
+ - destruct ((fn_code f) ! pc) eqn:ATpc; intuition eauto.
+ exploit Regset.union_1; intuition eauto.
+Qed.
+*)
+
+(* This function computes the union of the inputs associated to the exits
+ minus the optional register in [or] (typically the result of a call or a builtin).
+ i.e. similar to [pre_output_regs] in RTLpath.
+*)
+Definition pre_inputs (f:function) (tbl: list exit) (or: option reg): Regset.t :=
+ let rs := union_inputs f tbl in
+ match or with
+ | Some r => Regset.remove r rs
+ | None => rs
+ end
+ .
+
+(* TODO: lemma pre_inputs_In + pre_inputs_notIn ? *)
+
+Definition tr_inputs (f:function) (tbl: list exit) (or:option reg): regset -> regset
+ := transfer_regs (Regset.elements (pre_inputs f tbl or)).
+
+
+(* TODO: move this elsewhere *)
+Lemma SetoidList_InA_eq_equiv A (l: list A): forall x,
+ SetoidList.InA (fun x y => x = y) x l <-> List.In x l.
+Proof.
+ intros x; split.
+ - induction 1; simpl; eauto.
+ - induction l; simpl; intuition.
+Qed.
+
+Lemma tr_pre_inputs f tbl or rs r:
+ Regset.In r (pre_inputs f tbl or) -> (tr_inputs f tbl or rs)#r = rs#r.
+Proof.
+ intros; eapply transfer_regs_inputs.
+ rewrite <- SetoidList_InA_eq_equiv.
+ eapply Regset.elements_1; eauto.
+Qed.
+
+Lemma tr_inputs_dead f tbl or rs r:
+ ~(Regset.In r (pre_inputs f tbl or)) -> (tr_inputs f tbl or rs)#r = Vundef.
+Proof.
+ intros X; eapply transfer_regs_dead; intuition eauto.
+ eapply X. eapply Regset.elements_2.
+ rewrite -> SetoidList_InA_eq_equiv; eauto.
+Qed.
+
+Local Hint Resolve tr_pre_inputs Regset.mem_2 Regset.mem_1: core.
+
+Lemma tr_inputs_get f tbl or rs r:
+ (tr_inputs f tbl or rs)#r = if Regset.mem r (pre_inputs f tbl or) then rs#r else Vundef.
+Proof.
+ autodestruct; eauto.
+ intros; apply tr_inputs_dead; eauto.
+ intro X; exploit Regset.mem_1; eauto.
+ congruence.
+Qed.
+
+(* TODO: not yet used
+Lemma tr_inputs_ext f tbl or rs1 rs2:
+ (forall r, Regset.In r (pre_inputs f tbl or) -> rs1#r = rs2#r) ->
+ (forall r, (tr_inputs f tbl or rs1)#r = (tr_inputs f tbl or rs2)#r).
+Proof.
+ intros EQ r. rewrite !tr_inputs_get.
+ autodestruct; auto.
+Qed.
+*)
+
+Definition fsem (p: program) :=
+ Semantics (step tr_inputs) (initial_state p) final_state (Genv.globalenv p).
+
+Local Open Scope list_scope.
+
+Definition poly_tr {A} (tr: function -> list exit -> option reg -> A) f (i: final): A :=
+ match i with
+ | Bgoto pc => tr f [pc] None
+ | Bcall _ _ _ res pc => tr f [pc] (Some res)
+ | Btailcall _ _ args => tr f [] None
+ | Bbuiltin _ _ res pc => tr f [pc] (reg_builtin_res res)
+ | Breturn _ => tr f [] None
+ | Bjumptable _ tbl => tr f tbl None
+ end.
+
+Definition tr_regs: function -> final -> regset -> regset :=
+ poly_tr tr_inputs.
+
+(* TODO: NOT USEFUL ?
+Definition liveout: function -> final -> Regset.t :=
+ poly_tr pre_inputs.
+
+Lemma tr_regs_liveout_equiv f fi : tr_regs f fi = transfer_regs (Regset.elements (liveout f fi)).
+Proof.
+ destruct fi; simpl; auto.
+Qed.
+
+Local Hint Resolve tr_inputs_get: core.
+
+Lemma tr_regs_get f fi rs r: (tr_regs f fi rs)#r = if Regset.mem r (liveout f fi) then rs#r else Vundef.
+Proof.
+ Local Opaque pre_inputs.
+ destruct fi; simpl; auto.
+Qed.
+*)
+
+(* * Comparing BTL semantics modulo extensionality of [regset].
+
+NB: This is at least used in BTL_SEtheory (and probably in the future BTL_SchedulerProof).
+
+*)
+Inductive equiv_stackframe: stackframe -> stackframe -> Prop :=
+ | equiv_stackframe_intro res f sp pc (rs1 rs2: regset)
+ (EQUIV: forall r, rs1#r = rs2#r)
+ :equiv_stackframe (Stackframe res f sp pc rs1) (Stackframe res f sp pc rs2)
+ .
+
+Inductive equiv_state: state -> state -> Prop :=
+ | State_equiv stk1 stk2 f sp pc rs1 m rs2
+ (STACKS: list_forall2 equiv_stackframe stk1 stk2)
+ (EQUIV: forall r, rs1#r = rs2#r)
+ :equiv_state (State stk1 f sp pc rs1 m) (State stk2 f sp pc rs2 m)
+ | Call_equiv stk1 stk2 f args m
+ (STACKS: list_forall2 equiv_stackframe stk1 stk2)
+ :equiv_state (Callstate stk1 f args m) (Callstate stk2 f args m)
+ | Return_equiv stk1 stk2 v m
+ (STACKS: list_forall2 equiv_stackframe stk1 stk2)
+ :equiv_state (Returnstate stk1 v m) (Returnstate stk2 v m)
+ .
+Local Hint Constructors equiv_stackframe equiv_state: core.
+
+Lemma equiv_stackframe_refl stf: equiv_stackframe stf stf.
+Proof.
+ destruct stf; eauto.
+Qed.
+
+Lemma equiv_stack_refl stk: list_forall2 equiv_stackframe stk stk.
+Proof.
+ Local Hint Resolve equiv_stackframe_refl: core.
+ induction stk; simpl; constructor; auto.
+Qed.
+Local Hint Resolve equiv_stack_refl: core.
+
+Lemma equiv_state_refl s: equiv_state s s.
+Proof.
+ induction s; simpl; constructor; auto.
+Qed.
+
+Lemma equiv_stackframe_trans stf1 stf2 stf3:
+ equiv_stackframe stf1 stf2 -> equiv_stackframe stf2 stf3 -> equiv_stackframe stf1 stf3.
+Proof.
+ destruct 1; intros EQ; inv EQ; try econstructor; eauto.
+ intros; eapply eq_trans; eauto.
+Qed.
+Local Hint Resolve equiv_stackframe_trans: core.
+
+Lemma equiv_stack_trans stk1 stk2:
+ list_forall2 equiv_stackframe stk1 stk2 ->
+ forall stk3, list_forall2 equiv_stackframe stk2 stk3 ->
+ list_forall2 equiv_stackframe stk1 stk3.
+Proof.
+ induction 1; intros stk3 EQ; inv EQ; econstructor; eauto.
+Qed.
+
+Lemma equiv_state_trans s1 s2 s3: equiv_state s1 s2 -> equiv_state s2 s3 -> equiv_state s1 s3.
+Proof.
+ Local Hint Resolve equiv_stack_trans: core.
+ destruct 1; intros EQ; inv EQ; econstructor; eauto.
+ intros; eapply eq_trans; eauto.
+Qed.
+
+Lemma regmap_setres_eq (rs rs': regset) res vres:
+ (forall r, rs # r = rs' # r) ->
+ forall r, (regmap_setres res vres rs) # r = (regmap_setres res vres rs') # r.
+Proof.
+ intros RSEQ r. destruct res; simpl; try congruence.
+ destruct (peq x r).
+ - subst. repeat (rewrite Regmap.gss). reflexivity.
+ - repeat (rewrite Regmap.gso); auto.
+Qed.
+
+(* * Comparing BTL semantics modulo [regs_lessdef].
+
+This extends the previous [equiv_*] stuff for [Val.lessdef]. Here, we need to also compare memories
+for Mem.extends, because Vundef values are propagated in memory through stores, allocation, etc.
+
+*)
+
+Inductive lessdef_stackframe: stackframe -> stackframe -> Prop :=
+ | lessdef_stackframe_intro res f sp pc rs1 rs2
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ :lessdef_stackframe (Stackframe res f sp pc rs1) (Stackframe res f sp pc rs2)
+ .
+
+Inductive lessdef_state: state -> state -> Prop :=
+ | State_lessdef stk1 stk2 f sp pc rs1 m1 rs2 m2
+ (STACKS: list_forall2 lessdef_stackframe stk1 stk2)
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ (MEMS: Mem.extends m1 m2)
+ :lessdef_state (State stk1 f sp pc rs1 m1) (State stk2 f sp pc rs2 m2)
+ | Call_lessdef stk1 stk2 f args1 args2 m1 m2
+ (STACKS: list_forall2 lessdef_stackframe stk1 stk2)
+ (ARGS: Val.lessdef_list args1 args2)
+ (MEMS: Mem.extends m1 m2)
+ :lessdef_state (Callstate stk1 f args1 m1) (Callstate stk2 f args2 m2)
+ | Return_lessdef stk1 stk2 v1 v2 m1 m2
+ (STACKS: list_forall2 lessdef_stackframe stk1 stk2)
+ (REGS: Val.lessdef v1 v2)
+ (MEMS: Mem.extends m1 m2)
+ :lessdef_state (Returnstate stk1 v1 m1) (Returnstate stk2 v2 m2)
+ .
+Local Hint Constructors lessdef_stackframe lessdef_state: core.
+
+Lemma lessdef_stackframe_refl stf: lessdef_stackframe stf stf.
+Proof.
+ destruct stf; eauto.
+Qed.
+
+Local Hint Resolve lessdef_stackframe_refl: core.
+Lemma lessdef_stack_refl stk: list_forall2 lessdef_stackframe stk stk.
+Proof.
+ induction stk; simpl; constructor; auto.
+Qed.
+
+Local Hint Resolve lessdef_stack_refl: core.
+
+Lemma lessdef_list_refl args: Val.lessdef_list args args.
+Proof.
+ induction args; simpl; auto.
+Qed.
+
+Local Hint Resolve lessdef_list_refl Mem.extends_refl: core.
+
+Lemma lessdef_state_refl s: lessdef_state s s.
+Proof.
+ induction s; simpl; constructor; auto.
+Qed.
+
+Local Hint Resolve Val.lessdef_trans: core.
+Lemma lessdef_stackframe_trans stf1 stf2 stf3:
+ lessdef_stackframe stf1 stf2 -> lessdef_stackframe stf2 stf3 -> lessdef_stackframe stf1 stf3.
+Proof.
+ destruct 1. intros LD; inv LD; try econstructor; eauto.
+Qed.
+
+Local Hint Resolve lessdef_stackframe_trans: core.
+Lemma lessdef_stack_trans stk1 stk2:
+ list_forall2 lessdef_stackframe stk1 stk2 ->
+ forall stk3, list_forall2 lessdef_stackframe stk2 stk3 ->
+ list_forall2 lessdef_stackframe stk1 stk3.
+Proof.
+ induction 1; intros stk3 LD; inv LD; econstructor; eauto.
+Qed.
+
+Local Hint Resolve lessdef_stack_trans Mem.extends_extends_compose Val.lessdef_list_trans: core.
+Lemma lessdef_state_trans s1 s2 s3: lessdef_state s1 s2 -> lessdef_state s2 s3 -> lessdef_state s1 s3.
+Proof.
+ destruct 1; intros LD; inv LD; econstructor; eauto.
+Qed.
+
+Lemma init_regs_lessdef_preserv args1 args2
+ (ARGS : Val.lessdef_list args1 args2)
+ : forall rl r, Val.lessdef (init_regs args1 rl)#r (init_regs args2 rl)#r.
+Proof.
+ induction ARGS; simpl; auto.
+ intros rl r1; destruct rl; simpl in *; auto.
+ eapply set_reg_lessdef; eauto.
+ intros r2; eauto.
+Qed.
+
+Lemma find_function_lessdef ge ros rs1 rs2 fd
+ (FIND: find_function ge ros rs1 = Some fd)
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ : find_function ge ros rs2 = Some fd.
+Proof.
+ destruct ros; simpl in *; auto.
+ exploit Genv.find_funct_inv; eauto.
+ intros (b & EQ).
+ destruct (REGS r); try congruence.
+Qed.
+
+(** * Auxiliary general purpose functions on BTL *)
+
+Definition is_goto (ib: iblock): bool :=
+ match ib with
+ | BF (Bgoto _) _ => true
+ | _ => false
+ end.
+
diff --git a/scheduling/BTLRenumber.ml b/scheduling/BTLRenumber.ml
new file mode 100644
index 00000000..2f4f9203
--- /dev/null
+++ b/scheduling/BTLRenumber.ml
@@ -0,0 +1,112 @@
+open Maps
+open BTL
+open RTLcommonaux
+open BTLcommonaux
+open BTLtypes
+open DebugPrint
+open PrintBTL
+
+let recompute_inumbs btl entry =
+ let btl = reset_visited_ib (reset_visited_ibf btl) in
+ let last_used = ref 0 in
+ let ibf = get_some @@ PTree.get entry btl in
+ let ipos () =
+ last_used := !last_used + 1;
+ !last_used
+ in
+ let rec walk ib k =
+ (* heuristic: try to explore the lower numbered branch first *)
+ let walk_smallest_child s1 s2 ib1 ib2 =
+ if s1 < s2 then (
+ walk ib1 None;
+ walk ib2 None)
+ else (
+ walk ib2 None;
+ walk ib1 None)
+ in
+ if jump_visit ib then ()
+ else
+ match ib with
+ | BF (Bcall (_, _, _, _, s), iinfo) | BF (Bbuiltin (_, _, _, s), iinfo) ->
+ let ib' = (get_some @@ PTree.get s btl).entry in
+ walk ib' None;
+ iinfo.inumb <- ipos ()
+ | BF (Bgoto s, _) ->
+ let ib' = (get_some @@ PTree.get s btl).entry in
+ walk ib' None
+ | BF (Bjumptable (_, tbl), iinfo) ->
+ List.iter
+ (fun s ->
+ let ib' = (get_some @@ PTree.get s btl).entry in
+ walk ib' None)
+ tbl;
+ iinfo.inumb <- ipos ()
+ | BF (Btailcall (_, _, _), iinfo) | BF (Breturn _, iinfo) ->
+ iinfo.inumb <- ipos ()
+ | Bnop None ->
+ failwith
+ "recompute_inumbs: Bnop None can only be in the right child of \
+ Bcond"
+ | Bnop (Some iinfo)
+ | Bop (_, _, _, iinfo)
+ | Bload (_, _, _, _, _, iinfo)
+ | Bstore (_, _, _, _, iinfo) ->
+ let succ = get_some @@ k in
+ walk succ None;
+ iinfo.inumb <- ipos ()
+ | Bseq (ib1, ib2) -> walk ib1 (Some ib2)
+ | Bcond (_, _, BF (Bgoto s1, iinfoL), Bnop None, iinfoF) ->
+ iinfoL.visited <- true;
+ let ib1 = get_some @@ PTree.get s1 btl in
+ let ib2 = get_some @@ k in
+ walk_smallest_child (p2i s1) (get_inumb_or_next ib2) ib1.entry ib2;
+ iinfoF.inumb <- ipos ()
+ | Bcond (_, _, _, _, _) -> failwith "recompute_inumbs: unsupported Bcond"
+ in
+ walk ibf.entry None;
+ btl
+
+let regenerate_btl_tree btl entry =
+ let new_entry = ref entry in
+ let rec renumber_iblock ib =
+ let get_new_succ s =
+ let sentry = get_some @@ PTree.get s btl in
+ i2p (get_inumb_or_next sentry.entry)
+ in
+ match ib with
+ | BF (Bcall (sign, fn, lr, rd, s), iinfo) ->
+ BF (Bcall (sign, fn, lr, rd, get_new_succ s), iinfo)
+ | BF (Bbuiltin (sign, fn, lr, s), iinfo) ->
+ BF (Bbuiltin (sign, fn, lr, get_new_succ s), iinfo)
+ | BF (Bgoto s, iinfo) -> BF (Bgoto (get_new_succ s), iinfo)
+ | BF (Bjumptable (arg, tbl), iinfo) ->
+ let tbl' = List.map (fun s -> get_new_succ s) tbl in
+ BF (Bjumptable (arg, tbl'), iinfo)
+ | Bcond (cond, lr, ib1, ib2, iinfo) ->
+ Bcond (cond, lr, renumber_iblock ib1, renumber_iblock ib2, iinfo)
+ | Bseq (ib1, ib2) -> Bseq (renumber_iblock ib1, renumber_iblock ib2)
+ | _ -> ib
+ in
+ let dm = ref PTree.empty in
+ let ord_btl =
+ PTree.fold
+ (fun ord_btl old_n old_ibf ->
+ let ib = renumber_iblock old_ibf.entry in
+ let n = get_inumb_or_next ib in
+ let n_pos = i2p n in
+ let bi = mk_binfo n old_ibf.binfo.s_output_regs old_ibf.binfo.typing in
+ let ibf = { entry = ib; input_regs = old_ibf.input_regs; binfo = bi } in
+ if old_n = entry then new_entry := n_pos;
+ dm := PTree.set old_n n_pos !dm;
+ PTree.set n_pos ibf ord_btl)
+ btl PTree.empty
+ in
+ debug "Renumbered BTL with new_entry=%d:\n" (p2i !new_entry);
+ print_btl_code stderr ord_btl;
+ ((ord_btl, !new_entry), !dm)
+
+let renumber btl entry =
+ (*debug_flag := true;*)
+ let btl' = recompute_inumbs btl entry in
+ (*debug_flag := false;*)
+ regenerate_btl_tree btl' entry
diff --git a/scheduling/BTLScheduleraux.ml b/scheduling/BTLScheduleraux.ml
new file mode 100644
index 00000000..38bc685c
--- /dev/null
+++ b/scheduling/BTLScheduleraux.ml
@@ -0,0 +1,344 @@
+open Maps
+open BTL
+open BTLtypes
+open Machine
+open DebugPrint
+open PrintBTL
+open Registers
+open RTLcommonaux
+open BTLcommonaux
+open ExpansionOracle
+open PrepassSchedulingOracle
+
+let flatten_blk_basics ibf =
+ let ib = ibf.entry in
+ let last = ref None in
+ let rec traverse_blk ib =
+ match ib with
+ | BF (_, _) ->
+ last := Some ib;
+ []
+ | Bseq ((Bcond (_, _, _, _, iinfo) as ib1), ib2) -> (
+ match iinfo.opt_info with
+ | Some _ -> [ ib1 ] @ traverse_blk ib2
+ | None ->
+ last := Some ib;
+ [])
+ | Bseq (ib1, ib2) -> traverse_blk ib1 @ traverse_blk ib2
+ | _ -> [ ib ]
+ in
+ let ibl = traverse_blk ib in
+ (Array.of_list ibl, !last)
+
+let is_a_cb = function Bcond _ -> true | _ -> false
+
+let is_a_load = function Bload _ -> true | _ -> false
+
+module SI = Set.Make (Int)
+
+let find_array arr n =
+ let index = ref None in
+ (try
+ Array.iteri
+ (fun i n' ->
+ match !index with
+ | Some _ -> raise Exit
+ | None -> if n = n' then index := Some i)
+ arr
+ with Exit -> ());
+ get_some @@ !index
+
+let count_cbs bseq olast indexes =
+ let current_cbs = ref SI.empty in
+ let cbs_above = Hashtbl.create 100 in
+ let update_cbs n ib =
+ print_btl_inst stderr ib;
+ if is_a_cb ib then current_cbs := SI.add indexes.(n) !current_cbs
+ else if is_a_load ib then Hashtbl.add cbs_above indexes.(n) !current_cbs
+ in
+ Array.iteri (fun n ib -> update_cbs n ib) bseq;
+ (match olast with
+ | Some last -> update_cbs (Array.length bseq) last
+ | None -> ());
+ cbs_above
+
+let apply_schedule bseq olast positions =
+ let bseq_new = Array.map (fun i -> bseq.(i)) positions in
+ (if !config.has_non_trapping_loads then
+ let fmap n = find_array positions n in
+ let seq = Array.init (Array.length positions) (fun i -> i) in
+ let fseq = Array.map fmap seq in
+ let cbs_above_old = count_cbs bseq olast fseq in
+ let cbs_above_new = count_cbs bseq_new olast seq in
+ Array.iteri
+ (fun n ib ->
+ let n' = fseq.(n) in
+ match ib with
+ | Bload (t, a, b, c, d, e) ->
+ let set_old = Hashtbl.find cbs_above_old n' in
+ let set_new = Hashtbl.find cbs_above_new n' in
+ if SI.subset set_old set_new then
+ if get_some @@ e.opt_info then
+ bseq_new.(n') <- Bload (AST.TRAP, a, b, c, d, e)
+ else assert !config.has_non_trapping_loads
+ | _ -> ())
+ bseq);
+ let ibl = Array.to_list bseq_new in
+ let rec build_iblock = function
+ | [] -> failwith "build_iblock: empty list"
+ | [ ib ] -> ( match olast with Some last -> Bseq (ib, last) | None -> ib)
+ | ib1 :: ib2 :: k -> Bseq (ib1, build_iblock (ib2 :: k))
+ in
+ build_iblock ibl
+
+(** the useful one. Returns a hashtable with bindings of shape
+ ** [(r,(t, n)], where [r] is a pseudo-register (Registers.reg),
+ ** [t] is its class (according to [typing]), and [n] the number of
+ ** times it's referenced as an argument in instructions of [seqa] ;
+ ** and an arrray containg the list of regs referenced by each
+ ** instruction, with a boolean to know whether it's as arg or dest *)
+let reference_counting (seqa : (iblock * Regset.t) array) (out_regs : Regset.t)
+ (typing : RTLtyping.regenv) :
+ (reg, int * int) Hashtbl.t * (reg * bool) list array =
+ let retl = Hashtbl.create 42 in
+ let retr = Array.make (Array.length seqa) [] in
+ (* retr.(i) : (r, b) -> (r', b') -> ...
+ * where b = true if seen as arg, false if seen as dest
+ *)
+ List.iter
+ (fun reg ->
+ Hashtbl.add retl reg (Machregsaux.class_of_type (typing reg), 1))
+ (Regset.elements out_regs);
+ let add_reg reg =
+ match Hashtbl.find_opt retl reg with
+ | Some (t, n) -> Hashtbl.add retl reg (t, n + 1)
+ | None -> Hashtbl.add retl reg (Machregsaux.class_of_type (typing reg), 1)
+ in
+ let map_true = List.map (fun r -> (r, true)) in
+ Array.iteri
+ (fun i (ins, _) ->
+ match ins with
+ | Bop (_, args, dest, _) | Bload (_, _, _, args, dest, _) ->
+ List.iter add_reg args;
+ retr.(i) <- (dest, false) :: map_true args
+ | Bcond (_, args, _, _, _) ->
+ List.iter add_reg args;
+ retr.(i) <- map_true args
+ | Bstore (_, _, args, src, _) ->
+ List.iter add_reg args;
+ add_reg src;
+ retr.(i) <- (src, true) :: map_true args
+ | BF (Bcall (_, fn, args, dest, _), _) ->
+ List.iter add_reg args;
+ retr.(i) <-
+ (match fn with
+ | Datatypes.Coq_inl reg ->
+ add_reg reg;
+ (dest, false) :: (reg, true) :: map_true args
+ | _ -> (dest, false) :: map_true args)
+ | BF (Btailcall (_, fn, args), _) ->
+ List.iter add_reg args;
+ retr.(i) <-
+ (match fn with
+ | Datatypes.Coq_inl reg ->
+ add_reg reg;
+ (reg, true) :: map_true args
+ | _ -> map_true args)
+ | BF (Bbuiltin (_, args, dest, _), _) ->
+ let rec bar = function
+ | AST.BA r ->
+ add_reg r;
+ retr.(i) <- (r, true) :: retr.(i)
+ | AST.BA_splitlong (hi, lo) | AST.BA_addptr (hi, lo) ->
+ bar hi;
+ bar lo
+ | _ -> ()
+ in
+ List.iter bar args;
+ let rec bad = function
+ | AST.BR r -> retr.(i) <- (r, false) :: retr.(i)
+ | AST.BR_splitlong (hi, lo) ->
+ bad hi;
+ bad lo
+ | _ -> ()
+ in
+ bad dest
+ | BF (Bjumptable (reg, _), _) | BF (Breturn (Some reg), _) ->
+ add_reg reg;
+ retr.(i) <- [ (reg, true) ]
+ | _ -> ())
+ seqa;
+ (* print_string "mentions\n";
+ * Array.iteri (fun i l ->
+ * print_int i;
+ * print_string ": [";
+ * List.iter (fun (r, b) ->
+ * print_int (Camlcoq.P.to_int r);
+ * print_string ":";
+ * print_string (if b then "a:" else "d");
+ * if b then print_int (snd (Hashtbl.find retl r));
+ * print_string ", "
+ * ) l;
+ * print_string "]\n";
+ * flush stdout;
+ * ) retr; *)
+ (retl, retr)
+
+let get_live_regs_entry seqa ibf btl =
+ if !Clflags.option_debug_compcert > 6 then debug_flag := true;
+ let ret =
+ Array.fold_right
+ (fun (ins, liveins) regset_i ->
+ let regset = Regset.union liveins regset_i in
+ match ins with
+ | Bnop _ -> regset
+ | Bop (_, args, dest, _) | Bload (_, _, _, args, dest, _) ->
+ List.fold_left
+ (fun set reg -> Regset.add reg set)
+ (Regset.remove dest regset)
+ args
+ | Bstore (_, _, args, src, _) ->
+ List.fold_left
+ (fun set reg -> Regset.add reg set)
+ (Regset.add src regset) args
+ | BF (Bcall (_, fn, args, dest, _), _) ->
+ List.fold_left
+ (fun set reg -> Regset.add reg set)
+ ((match fn with
+ | Datatypes.Coq_inl reg -> Regset.add reg
+ | Datatypes.Coq_inr _ -> fun x -> x)
+ (Regset.remove dest regset))
+ args
+ | BF (Btailcall (_, fn, args), _) ->
+ List.fold_left
+ (fun set reg -> Regset.add reg set)
+ (match fn with
+ | Datatypes.Coq_inl reg -> Regset.add reg regset
+ | Datatypes.Coq_inr _ -> regset)
+ args
+ | BF (Bbuiltin (_, args, dest, _), _) ->
+ List.fold_left
+ (fun set arg ->
+ let rec add reg set =
+ match reg with
+ | AST.BA r -> Regset.add r set
+ | AST.BA_splitlong (hi, lo) | AST.BA_addptr (hi, lo) ->
+ add hi (add lo set)
+ | _ -> set
+ in
+ add arg set)
+ (let rec rem dest set =
+ match dest with
+ | AST.BR r -> Regset.remove r set
+ | AST.BR_splitlong (hi, lo) -> rem hi (rem lo set)
+ | _ -> set
+ in
+ rem dest regset)
+ args
+ | BF (Bjumptable (reg, _), _) | BF (Breturn (Some reg), _) ->
+ Regset.add reg regset
+ | Bcond (_, args, _, _, _) ->
+ List.fold_left (fun set reg -> Regset.add reg set) regset args
+ | _ -> regset)
+ seqa ibf.binfo.s_output_regs
+ in
+ debug "live in regs: ";
+ print_regset ret;
+ debug "\n";
+ debug_flag := false;
+ ret
+
+let schedule_blk n ibf btl =
+ if not !Clflags.option_fprepass then btl
+ else
+ let bseq, olast = flatten_blk_basics ibf in
+ let seqa =
+ Array.mapi
+ (fun i inst -> (inst, get_liveins inst))
+ bseq
+ in
+ let live_regs_entry = get_live_regs_entry seqa ibf btl in
+ let refcount =
+ reference_counting seqa ibf.binfo.s_output_regs ibf.binfo.typing
+ in
+ match
+ schedule_sequence seqa btl live_regs_entry ibf.binfo.typing refcount
+ with
+ | Some positions ->
+ let new_ib = apply_schedule bseq olast positions in
+ let new_ibf =
+ { entry = new_ib; binfo = ibf.binfo; input_regs = ibf.input_regs }
+ in
+ PTree.set n new_ibf btl
+ | None -> btl
+
+let turn_all_loads_nontrap n ibf btl =
+ if not !config.has_non_trapping_loads || not !Clflags.option_fnontrap_loads then btl
+ else
+ let rec traverse_rec ib =
+ match ib with
+ | Bseq (ib1, ib2) -> Bseq (traverse_rec ib1, traverse_rec ib2)
+ | Bload (t, a, b, c, d, e) ->
+ let _opt_info =
+ match t with
+ | AST.TRAP -> Some true
+ | AST.NOTRAP -> Some false in
+ e.opt_info <- _opt_info;
+ Bload (AST.NOTRAP, a, b, c, d, e)
+ | _ -> ib
+ in
+ let ib' = traverse_rec ibf.entry in
+ let ibf' =
+ { entry = ib'; input_regs = ibf.input_regs; binfo = ibf.binfo }
+ in
+ PTree.set n ibf' btl
+
+let compute_liveins n ibf btl =
+ let rec traverse_rec ib =
+ match ib with
+ | Bseq (ib1, ib2) ->
+ traverse_rec ib1;
+ traverse_rec ib2
+ | BF (Bgoto succ, iinfo)
+ | BF (Bcall (_, _, _, _, succ), iinfo)
+ | BF (Bbuiltin (_, _, _, succ), iinfo) ->
+ let lives = (get_some @@ PTree.get succ btl).input_regs in
+ iinfo.liveins <- lives
+ | BF (Bjumptable (_, tbl), iinfo) ->
+ List.iter
+ (fun ex ->
+ let lives = (get_some @@ PTree.get ex btl).input_regs in
+ iinfo.liveins <- Regset.union iinfo.liveins lives)
+ tbl
+ | Bcond (_, _, BF (Bgoto succ, _), Bnop None, iinfo) -> (
+ match iinfo.opt_info with
+ | Some predb ->
+ assert (predb = false);
+ let lives = (get_some @@ PTree.get succ btl).input_regs in
+ iinfo.liveins <- lives
+ | None -> ())
+ | _ -> ()
+ in
+ traverse_rec ibf.entry
+
+let rec do_schedule btl = function
+ | [] -> btl
+ | (n, ibf) :: blks ->
+ compute_liveins n ibf btl;
+ let code_exp = expanse n ibf btl in
+ let ibf_exp = get_some @@ PTree.get n code_exp in
+ let code_nt = turn_all_loads_nontrap n ibf_exp code_exp in
+ let ibf_nt = get_some @@ PTree.get n code_nt in
+ let btl' = schedule_blk n ibf_nt code_nt in
+ (*debug_flag := true;*)
+ print_btl_code stderr btl;
+ print_btl_code stderr btl';
+ (*debug_flag := false;*)
+ do_schedule btl' blks
+
+let btl_scheduler f =
+ let btl = f.fn_code in
+ let elts = PTree.elements btl in
+ find_last_reg elts;
+ let btl' = do_schedule btl elts in
+ btl'
diff --git a/scheduling/BTL_Livecheck.v b/scheduling/BTL_Livecheck.v
new file mode 100644
index 00000000..d200b9bd
--- /dev/null
+++ b/scheduling/BTL_Livecheck.v
@@ -0,0 +1,690 @@
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep Op Registers OptionMonad.
+Require Import Errors RTL BTL BTLmatchRTL.
+
+
+Local Open Scope lazy_bool_scope.
+
+Local Open Scope option_monad_scope.
+
+Fixpoint list_mem (rl: list reg) (alive: Regset.t) {struct rl}: bool :=
+ match rl with
+ | nil => true
+ | r1 :: rs => Regset.mem r1 alive &&& list_mem rs alive
+ end.
+
+Definition reg_option_mem (or: option reg) (alive: Regset.t) :=
+ match or with None => true | Some r => Regset.mem r alive end.
+
+Definition reg_sum_mem (ros: reg + ident) (alive: Regset.t) :=
+ match ros with inl r => Regset.mem r alive | inr s => true end.
+
+(* NB: definition following [regmap_setres] in [RTL.step] semantics *)
+Definition reg_builtin_res (res: builtin_res reg) (alive: Regset.t): Regset.t :=
+ match res with
+ | BR r => Regset.add r alive
+ | _ => alive
+ end.
+
+Definition exit_checker (btl: code) (alive: Regset.t) (s: node): option unit :=
+ SOME next <- btl!s IN
+ ASSERT Regset.subset next.(input_regs) alive IN
+ Some tt.
+
+Fixpoint exit_list_checker (btl: code) (alive: Regset.t) (l: list node): bool :=
+ match l with
+ | nil => true
+ | s :: l' => exit_checker btl alive s &&& exit_list_checker btl alive l'
+ end.
+
+Definition final_inst_checker (btl: code) (alive: Regset.t) (fin: final): option unit :=
+ match fin with
+ | Bgoto s =>
+ exit_checker btl alive s
+ | Breturn oreg =>
+ ASSERT reg_option_mem oreg alive IN Some tt
+ | Bcall _ ros args res s =>
+ ASSERT list_mem args alive IN
+ ASSERT reg_sum_mem ros alive IN
+ exit_checker btl (Regset.add res alive) s
+ | Btailcall _ ros args =>
+ ASSERT list_mem args alive IN
+ ASSERT reg_sum_mem ros alive IN Some tt
+ | Bbuiltin _ args res s =>
+ ASSERT list_mem (params_of_builtin_args args) alive IN
+ exit_checker btl (reg_builtin_res res alive) s
+ | Bjumptable arg tbl =>
+ ASSERT Regset.mem arg alive IN
+ ASSERT exit_list_checker btl alive tbl IN Some tt
+ end.
+
+(* This definition is the meet (infimum) subset of alive registers,
+ used for conditions by the below checker.
+ A None argument represents the neutral element for intersection. *)
+Definition meet (o1 o2: option Regset.t): option Regset.t :=
+ match o1, o2 with
+ | None, _ => o2
+ | _, None => o1
+ | Some alive1, Some alive2 => Some (Regset.inter alive1 alive2)
+ end.
+
+Fixpoint body_checker (btl: code) (ib: iblock) (alive: Regset.t): option (option Regset.t) :=
+ match ib with
+ | Bseq ib1 ib2 =>
+ SOME oalive1 <- body_checker btl ib1 alive IN
+ SOME alive1 <- oalive1 IN
+ body_checker btl ib2 alive1
+ | Bnop _ => Some (Some alive)
+ | Bop _ args dest _ =>
+ ASSERT list_mem args alive IN
+ Some (Some (Regset.add dest alive))
+ | Bload _ _ _ args dest _ =>
+ ASSERT list_mem args alive IN
+ Some (Some (Regset.add dest alive))
+ | Bstore _ _ args src _ =>
+ ASSERT Regset.mem src alive IN
+ ASSERT list_mem args alive IN
+ Some (Some alive)
+ | Bcond _ args ib1 ib2 _ =>
+ ASSERT list_mem args alive IN
+ SOME oalive1 <- body_checker btl ib1 alive IN
+ SOME oalive2 <- body_checker btl ib2 alive IN
+ Some (meet oalive1 oalive2)
+ | BF fin _ =>
+ SOME _ <- final_inst_checker btl alive fin IN
+ Some None
+ end.
+
+(* This definition simply convert the result in an option unit *)
+Definition iblock_checker (btl: code) (ib: iblock) (alive: Regset.t): option unit :=
+ SOME _ <- body_checker btl ib alive IN Some tt.
+
+Fixpoint list_iblock_checker (btl: code) (l: list (node*iblock_info)): bool :=
+ match l with
+ | nil => true
+ | (_, ibf) :: l' => iblock_checker btl ibf.(entry) ibf.(input_regs) &&& list_iblock_checker btl l'
+ end.
+
+Lemma lazy_and_Some_true A (o: option A) (b: bool): o &&& b = true <-> (exists v, o = Some v) /\ b = true.
+Proof.
+ destruct o; simpl; intuition.
+ - eauto.
+ - firstorder. try_simplify_someHyps.
+Qed.
+
+Lemma lazy_and_Some_tt_true (o: option unit) (b: bool): o &&& b = true <-> o = Some tt /\ b = true.
+Proof.
+ intros; rewrite lazy_and_Some_true; firstorder.
+ destruct x; auto.
+Qed.
+
+Lemma list_iblock_checker_correct btl l:
+ list_iblock_checker btl l = true ->
+ forall e, List.In e l -> iblock_checker btl (snd e).(entry) (snd e).(input_regs) = Some tt.
+Proof.
+ intros CHECKER e H; induction l as [|(n & ibf) l]; intuition.
+ simpl in * |- *. rewrite lazy_and_Some_tt_true in CHECKER. intuition (subst; auto).
+Qed.
+
+Definition liveness_checker_bool (f: BTL.function): bool :=
+ f.(fn_code)!(f.(fn_entrypoint)) &&& list_iblock_checker f.(fn_code) (PTree.elements f.(fn_code)).
+
+Definition liveness_checker (f: BTL.function): res unit :=
+ match liveness_checker_bool f with
+ | true => OK tt
+ | false => Error (msg "BTL_Livecheck: liveness_checker failed")
+ end.
+
+Lemma decomp_liveness_checker f:
+ liveness_checker f = OK tt ->
+ exists ibf, f.(fn_code)!(f.(fn_entrypoint)) = Some ibf /\
+ list_iblock_checker f.(fn_code) (PTree.elements f.(fn_code)) = true.
+Proof.
+ intros LIVE; unfold liveness_checker in LIVE.
+ destruct liveness_checker_bool eqn:EQL; try congruence.
+ clear LIVE. unfold liveness_checker_bool in EQL.
+ rewrite lazy_and_Some_true in EQL; destruct EQL as [[ibf ENTRY] LIST].
+ eexists; split; eauto.
+Qed.
+
+Lemma liveness_checker_correct f n ibf:
+ liveness_checker f = OK tt ->
+ f.(fn_code)!n = Some ibf ->
+ iblock_checker f.(fn_code) ibf.(entry) ibf.(input_regs) = Some tt.
+Proof.
+ intros LIVE PC.
+ apply decomp_liveness_checker in LIVE; destruct LIVE as [ibf' [ENTRY LIST]].
+ exploit list_iblock_checker_correct; eauto.
+ - eapply PTree.elements_correct; eauto.
+ - simpl; auto.
+Qed.
+
+Lemma liveness_checker_entrypoint f:
+ liveness_checker f = OK tt ->
+ f.(fn_code)!(f.(fn_entrypoint)) <> None.
+Proof.
+ intros LIVE; apply decomp_liveness_checker in LIVE; destruct LIVE as [ibf' [ENTRY LIST]].
+ unfold not; intros CONTRA. congruence.
+Qed.
+
+Definition liveness_ok_function (f: BTL.function): Prop := liveness_checker f = OK tt.
+
+Inductive liveness_ok_fundef: fundef -> Prop :=
+ | liveness_ok_Internal f: liveness_ok_function f -> liveness_ok_fundef (Internal f)
+ | liveness_ok_External ef: liveness_ok_fundef (External ef).
+
+
+Local Notation ext alive := (fun r => Regset.In r alive).
+
+Definition ext_opt (oalive: option Regset.t): Regset.elt -> Prop :=
+ match oalive with
+ | Some alive => ext alive
+ | None => fun _ => True
+ end.
+
+Lemma ext_opt_meet: forall r oalive1 oalive2,
+ ext_opt (meet oalive1 oalive2) r ->
+ ext_opt oalive1 r /\ ext_opt oalive2 r.
+Proof.
+ intros. destruct oalive1, oalive2;
+ simpl in *; intuition.
+ eapply Regset.inter_1; eauto.
+ eapply Regset.inter_2; eauto.
+Qed.
+
+Lemma regset_add_spec live r1 r2: Regset.In r1 (Regset.add r2 live) <-> (r1 = r2 \/ Regset.In r1 live).
+Proof.
+ destruct (Pos.eq_dec r1 r2).
+ - subst. intuition; eapply Regset.add_1; auto.
+ - intuition.
+ * right. eapply Regset.add_3; eauto.
+ * eapply Regset.add_2; auto.
+Qed.
+
+Local Hint Resolve Regset.mem_2 Regset.subset_2: core.
+
+Lemma lazy_and_true (b1 b2: bool): b1 &&& b2 = true <-> b1 = true /\ b2 = true.
+Proof.
+ destruct b1; simpl; intuition.
+Qed.
+
+Lemma list_mem_correct (rl: list reg) (alive: Regset.t):
+ list_mem rl alive = true -> forall r, List.In r rl -> ext alive r.
+Proof.
+ induction rl; simpl; try rewrite lazy_and_true; intuition subst; auto.
+Qed.
+
+Definition eqlive_reg (alive: Regset.elt -> Prop) (rs1 rs2: regset): Prop :=
+ forall r, (alive r) -> rs1#r = rs2#r.
+
+Lemma eqlive_reg_update (alive: Regset.elt -> Prop) rs1 rs2 r v: eqlive_reg (fun r1 => r1 <> r /\ alive r1) rs1 rs2 -> eqlive_reg alive (rs1 # r <- v) (rs2 # r <- v).
+Proof.
+ unfold eqlive_reg; intros EQLIVE r0 ALIVE.
+ destruct (Pos.eq_dec r r0) as [H|H].
+ - subst. rewrite! Regmap.gss. auto.
+ - rewrite! Regmap.gso; auto.
+Qed.
+
+Lemma eqlive_reg_monotonic (alive1 alive2: Regset.elt -> Prop) rs1 rs2: eqlive_reg alive2 rs1 rs2 -> (forall r, alive1 r -> alive2 r) -> eqlive_reg alive1 rs1 rs2.
+Proof.
+ unfold eqlive_reg; intuition.
+Qed.
+
+Lemma eqlive_reg_list (alive: Regset.elt -> Prop) args rs1 rs2: eqlive_reg alive rs1 rs2 -> (forall r, List.In r args -> (alive r)) -> rs1##args = rs2##args.
+Proof.
+ induction args; simpl; auto.
+ intros EQLIVE ALIVE; rewrite IHargs; auto.
+ unfold eqlive_reg in EQLIVE.
+ rewrite EQLIVE; auto.
+Qed.
+
+Lemma eqlive_reg_listmem (alive: Regset.t) args rs1 rs2: eqlive_reg (ext alive) rs1 rs2 -> list_mem args alive = true -> rs1##args = rs2##args.
+Proof.
+ intros; eapply eqlive_reg_list; eauto.
+ intros; eapply list_mem_correct; eauto.
+Qed.
+
+Inductive eqlive_stackframes: stackframe -> stackframe -> Prop :=
+ | eqlive_stackframes_intro ibf res f sp pc rs1 rs2
+ (LIVE: liveness_ok_function f)
+ (ENTRY: f.(fn_code)!pc = Some ibf)
+ (EQUIV: forall v, eqlive_reg (ext ibf.(input_regs)) (rs1 # res <- v) (rs2 # res <- v)):
+ eqlive_stackframes (Stackframe res f sp pc rs1) (Stackframe res f sp pc rs2).
+
+Inductive eqlive_states: state -> state -> Prop :=
+ | eqlive_states_intro
+ ibf st1 st2 f sp pc rs1 rs2 m
+ (STACKS: list_forall2 eqlive_stackframes st1 st2)
+ (LIVE: liveness_ok_function f)
+ (PATH: f.(fn_code)!pc = Some ibf)
+ (EQUIV: eqlive_reg (ext ibf.(input_regs)) rs1 rs2):
+ eqlive_states (State st1 f sp pc rs1 m) (State st2 f sp pc rs2 m)
+ | eqlive_states_call st1 st2 f args m
+ (LIVE: liveness_ok_fundef f)
+ (STACKS: list_forall2 eqlive_stackframes st1 st2):
+ eqlive_states (Callstate st1 f args m) (Callstate st2 f args m)
+ | eqlive_states_return st1 st2 v m
+ (STACKS: list_forall2 eqlive_stackframes st1 st2):
+ eqlive_states (Returnstate st1 v m) (Returnstate st2 v m).
+
+Section FSEM_SIMULATES_CFGSEM.
+
+Variable prog: BTL.program.
+
+Let ge := Genv.globalenv prog.
+
+Hypothesis all_fundef_liveness_ok: forall b f, Genv.find_funct_ptr ge b = Some f -> liveness_ok_fundef f.
+
+Local Hint Constructors eqlive_stackframes eqlive_states final_step list_forall2 step: core.
+
+Lemma eqlive_reg_update_gso alive rs1 rs2 res r: forall v : val,
+ eqlive_reg (ext alive) rs1 # res <- v rs2 # res <- v ->
+ res <> r -> Regset.In r alive ->
+ rs1 # r = rs2 # r.
+Proof.
+ intros v REGS NRES INR. unfold eqlive_reg in REGS.
+ specialize REGS with r. apply REGS in INR.
+ rewrite !Regmap.gso in INR; auto.
+Qed.
+
+Lemma find_funct_liveness_ok v fd:
+ Genv.find_funct ge v = Some fd -> liveness_ok_fundef fd.
+Proof.
+ unfold Genv.find_funct.
+ destruct v; try congruence.
+ destruct (Integers.Ptrofs.eq_dec _ _); try congruence.
+ eapply all_fundef_liveness_ok; eauto.
+Qed.
+
+Lemma find_function_liveness_ok ros rs f:
+ find_function ge ros rs = Some f -> liveness_ok_fundef f.
+Proof.
+ destruct ros as [r|i]; simpl.
+ - intros; eapply find_funct_liveness_ok; eauto.
+ - destruct (Genv.find_symbol ge i); try congruence.
+ eapply all_fundef_liveness_ok; eauto.
+Qed.
+
+Lemma find_function_eqlive alive ros rs1 rs2:
+ eqlive_reg (ext alive) rs1 rs2 ->
+ reg_sum_mem ros alive = true ->
+ find_function ge ros rs1 = find_function ge ros rs2.
+Proof.
+ intros EQLIVE.
+ destruct ros; simpl; auto.
+ intros H; erewrite (EQLIVE r); eauto.
+Qed.
+
+Lemma exit_checker_eqlive (btl: code) (alive: Regset.t) (pc: node) rs1 rs2:
+ exit_checker btl alive pc = Some tt ->
+ eqlive_reg (ext alive) rs1 rs2 ->
+ exists ibf, btl!pc = Some ibf /\ eqlive_reg (ext ibf.(input_regs)) rs1 rs2.
+Proof.
+ unfold exit_checker.
+ inversion_SOME next.
+ inversion_ASSERT. try_simplify_someHyps.
+ repeat (econstructor; eauto).
+ intros; eapply eqlive_reg_monotonic; eauto.
+ intros; exploit Regset.subset_2; eauto.
+Qed.
+
+Lemma exit_list_checker_eqlive (btl: code) (alive: Regset.t) (tbl: list node) rs1 rs2 pc: forall n,
+ exit_list_checker btl alive tbl = true ->
+ eqlive_reg (ext alive) rs1 rs2 ->
+ list_nth_z tbl n = Some pc ->
+ exists ibf, btl!pc = Some ibf /\ eqlive_reg (ext ibf.(input_regs)) rs1 rs2.
+Proof.
+ induction tbl; simpl.
+ - intros; try congruence.
+ - intros n; rewrite lazy_and_Some_tt_true; destruct (zeq n 0) eqn: Hn.
+ * try_simplify_someHyps; intuition.
+ exploit exit_checker_eqlive; eauto.
+ * intuition. eapply IHtbl; eauto.
+Qed.
+
+Lemma exit_checker_eqlive_update (btl: code) (alive: Regset.t) (pc: node) r rs1 rs2:
+ exit_checker btl (Regset.add r alive) pc = Some tt ->
+ eqlive_reg (ext alive) rs1 rs2 ->
+ exists ibf, btl!pc = Some ibf /\ (forall v, eqlive_reg (ext ibf.(input_regs)) (rs1 # r <- v) (rs2 # r <- v)).
+Proof.
+ unfold exit_checker.
+ inversion_SOME next.
+ inversion_ASSERT. try_simplify_someHyps.
+ repeat (econstructor; eauto).
+ intros; eapply eqlive_reg_update; eauto.
+ eapply eqlive_reg_monotonic; eauto.
+ intros r0 [X1 X2]; exploit Regset.subset_2; eauto.
+ rewrite regset_add_spec. intuition subst.
+Qed.
+
+Lemma exit_checker_eqlive_builtin_res (btl: code) (alive: Regset.t) (pc: node) rs1 rs2 (res:builtin_res reg):
+ exit_checker btl (reg_builtin_res res alive) pc = Some tt ->
+ eqlive_reg (ext alive) rs1 rs2 ->
+ exists ibf, btl!pc = Some ibf /\ (forall vres, eqlive_reg (ext ibf.(input_regs)) (regmap_setres res vres rs1) (regmap_setres res vres rs2)).
+Proof.
+ destruct res; simpl.
+ - intros; exploit exit_checker_eqlive_update; eauto.
+ - intros; exploit exit_checker_eqlive; eauto.
+ intros (ibf & PC & REGS).
+ eexists; intuition eauto.
+ - intros; exploit exit_checker_eqlive; eauto.
+ intros (ibf & PC & REGS).
+ eexists; intuition eauto.
+Qed.
+
+Local Hint Resolve in_or_app: local.
+Lemma eqlive_eval_builtin_args alive rs1 rs2 sp m args vargs:
+ eqlive_reg alive rs1 rs2 ->
+ Events.eval_builtin_args ge (fun r => rs1 # r) sp m args vargs ->
+ (forall r, List.In r (params_of_builtin_args args) -> alive r) ->
+ Events.eval_builtin_args ge (fun r => rs2 # r) sp m args vargs.
+Proof.
+ unfold Events.eval_builtin_args.
+ intros EQLIVE; induction 1 as [|a1 al b1 bl EVAL1 EVALL]; simpl.
+ { econstructor; eauto. }
+ intro X.
+ assert (X1: eqlive_reg (fun r => In r (params_of_builtin_arg a1)) rs1 rs2).
+ { eapply eqlive_reg_monotonic; eauto with local. }
+ lapply IHEVALL; eauto with local.
+ clear X IHEVALL; intro X. econstructor; eauto.
+ generalize X1; clear EVALL X1 X.
+ induction EVAL1; simpl; try (econstructor; eauto; fail).
+ - intros X1; erewrite X1; [ econstructor; eauto | eauto ].
+ - intros; econstructor.
+ + eapply IHEVAL1_1; eauto.
+ eapply eqlive_reg_monotonic; eauto.
+ simpl; intros; eauto with local.
+ + eapply IHEVAL1_2; eauto.
+ eapply eqlive_reg_monotonic; eauto.
+ simpl; intros; eauto with local.
+ - intros; econstructor.
+ + eapply IHEVAL1_1; eauto.
+ eapply eqlive_reg_monotonic; eauto.
+ simpl; intros; eauto with local.
+ + eapply IHEVAL1_2; eauto.
+ eapply eqlive_reg_monotonic; eauto.
+ simpl; intros; eauto with local.
+Qed.
+
+Lemma tr_inputs_eqlive_None f pc tbl ibf rs1 rs2
+ (PC: (fn_code f) ! pc = Some ibf)
+ (REGS: eqlive_reg (ext (input_regs ibf)) rs1 rs2)
+ :eqlive_reg (ext (input_regs ibf)) (tid f (pc :: tbl) None rs1)
+ (tr_inputs f (pc :: tbl) None rs2).
+Proof.
+ unfold eqlive_reg. intros r INR.
+ unfold tid. rewrite tr_inputs_get.
+ simpl. rewrite PC.
+ exploit Regset.union_3. eapply INR.
+ intros INRU. eapply Regset.mem_1 in INRU.
+ erewrite INRU; eauto.
+Qed.
+
+Lemma tr_inputs_eqlive_list_None tbl: forall f pc n alive ibf rs1 rs2
+ (REGS1: eqlive_reg (ext alive) rs1 rs2)
+ (EXIT_LIST: exit_list_checker (fn_code f) alive tbl = true)
+ (LIST: list_nth_z tbl n = Some pc)
+ (PC: (fn_code f) ! pc = Some ibf)
+ (REGS2: eqlive_reg (ext (input_regs ibf)) rs1 rs2),
+ eqlive_reg (ext (input_regs ibf)) (tid f tbl None rs1)
+ (tr_inputs f tbl None rs2).
+Proof.
+ induction tbl as [| pc' tbl IHtbl]; try_simplify_someHyps.
+ autodestruct; try_simplify_someHyps.
+ - intros; eapply tr_inputs_eqlive_None; eauto.
+ - rewrite lazy_and_Some_tt_true in EXIT_LIST.
+ destruct EXIT_LIST as [EXIT EXIT_REM].
+ intros. unfold eqlive_reg. intros r INR.
+ exploit (IHtbl f pc (Z.pred n) alive ibf rs1 rs2); eauto.
+ unfold tid. rewrite !tr_inputs_get.
+ exploit exit_checker_eqlive; eauto.
+ intros (ibf' & PC' & REGS3).
+ simpl; rewrite PC'. autodestruct.
+ + intro INRU. apply Regset.mem_2 in INRU.
+ intros EQR. eapply Regset.union_2 in INRU.
+ eapply Regset.mem_1 in INRU. erewrite INRU; auto.
+ + intros. autodestruct.
+ rewrite (REGS2 r); auto.
+Qed.
+
+Lemma tr_inputs_eqlive_update f pc ibf rs1 rs2 res
+ (PC: (fn_code f) ! pc = Some ibf)
+ :forall (v: val)
+ (REGS: eqlive_reg (ext (input_regs ibf)) rs1 # res <- v rs2 # res <- v),
+ eqlive_reg (ext (input_regs ibf))
+ (tid f (pc :: nil) (Some res) rs1) # res <- v
+ (tr_inputs f (pc :: nil) (Some res) rs2) # res <- v.
+Proof.
+ intros. apply eqlive_reg_update.
+ unfold eqlive_reg. intros r (NRES & INR).
+ unfold tid. rewrite tr_inputs_get.
+ simpl. rewrite PC. assert (NRES': res <> r) by auto.
+ clear NRES. exploit Regset.union_3. eapply INR.
+ intros INRU. exploit Regset.remove_2; eauto.
+ intros INRU_RES. eapply Regset.mem_1 in INRU_RES.
+ erewrite INRU_RES. eapply eqlive_reg_update_gso; eauto.
+Qed.
+
+Local Hint Resolve tr_inputs_eqlive_None tr_inputs_eqlive_update: core.
+Lemma cfgsem2fsem_finalstep_simu sp f stk1 stk2 s t fin alive rs1 m rs2
+ (FSTEP: final_step tid ge stk1 f sp rs1 m fin t s)
+ (LIVE: liveness_ok_function f)
+ (REGS: eqlive_reg (ext alive) rs1 rs2)
+ (FCHK: final_inst_checker (fn_code f) alive fin = Some tt)
+ (STACKS: list_forall2 eqlive_stackframes stk1 stk2)
+ :exists s',
+ final_step tr_inputs ge stk2 f sp rs2 m fin t s'
+ /\ eqlive_states s s'.
+Proof.
+ destruct FSTEP; try_simplify_someHyps; repeat inversion_ASSERT; intros.
+ - (* Bgoto *)
+ eexists; split.
+ + econstructor; eauto.
+ + exploit exit_checker_eqlive; eauto.
+ intros (ibf & PC & REGS').
+ econstructor; eauto.
+ - (* Breturn *)
+ eexists; split. econstructor; eauto.
+ destruct or; simpl in *;
+ try erewrite (REGS r); eauto.
+ - (* Bcall *)
+ exploit exit_checker_eqlive_update; eauto.
+ intros (ibf & PC & REGS').
+ eexists; split.
+ + econstructor; eauto.
+ erewrite <- find_function_eqlive; eauto.
+ + erewrite eqlive_reg_listmem; eauto.
+ eapply eqlive_states_call; eauto.
+ eapply find_function_liveness_ok; eauto.
+ - (* Btailcall *)
+ eexists; split.
+ + econstructor; eauto.
+ erewrite <- find_function_eqlive; eauto.
+ + erewrite eqlive_reg_listmem; eauto.
+ eapply eqlive_states_call; eauto.
+ eapply find_function_liveness_ok; eauto.
+ - (* Bbuiltin *)
+ exploit exit_checker_eqlive_builtin_res; eauto.
+ intros (ibf & PC & REGS').
+ eexists; split.
+ + econstructor; eauto.
+ eapply eqlive_eval_builtin_args; eauto.
+ intros; eapply list_mem_correct; eauto.
+ + repeat (econstructor; simpl; eauto).
+ unfold regmap_setres. destruct res; simpl in *; eauto.
+ - (* Bjumptable *)
+ exploit exit_list_checker_eqlive; eauto.
+ intros (ibf & PC & REGS').
+ eexists; split.
+ + econstructor; eauto.
+ erewrite <- REGS; eauto.
+ + repeat (econstructor; simpl; eauto).
+ apply (tr_inputs_eqlive_list_None tbl f pc' (Int.unsigned n) alive ibf rs1 rs2);
+ auto.
+Qed.
+
+Lemma cfgsem2fsem_ibistep_simu_None sp f ib: forall rs1 m rs1' m'
+ (ISTEP: iblock_istep ge sp rs1 m ib rs1' m' None)
+ alive1 oalive2 rs2 (REGS: eqlive_reg (ext alive1) rs1 rs2)
+ (BDY: body_checker (fn_code f) ib alive1 = Some (oalive2)),
+ exists rs2',
+ iblock_istep_run ge sp ib rs2 m = Some (out rs2' m' None)
+ /\ eqlive_reg (ext_opt oalive2) rs1' rs2'.
+Proof.
+ induction ib; intros; try_simplify_someHyps;
+ repeat inversion_ASSERT; intros; inv ISTEP.
+ - (* Bnop *)
+ inv BDY; eauto.
+ - (* Bop *)
+ erewrite <- eqlive_reg_listmem; eauto.
+ try_simplify_someHyps; intros.
+ repeat econstructor.
+ apply eqlive_reg_update.
+ eapply eqlive_reg_monotonic; eauto.
+ intros r0; rewrite regset_add_spec.
+ intuition.
+ - (* Bload *)
+ erewrite <- eqlive_reg_listmem; eauto.
+ try_simplify_someHyps; intros.
+ destruct trap; inv LOAD;
+ rewrite EVAL, LOAD0 || (autodestruct; try rewrite LOAD0; auto).
+ all:
+ repeat econstructor;
+ apply eqlive_reg_update;
+ eapply eqlive_reg_monotonic; eauto;
+ intros r0; rewrite regset_add_spec;
+ intuition.
+ - (* Bstore *)
+ erewrite <- eqlive_reg_listmem; eauto.
+ rewrite <- (REGS src); auto.
+ try_simplify_someHyps; intros.
+ rewrite STORE; eauto.
+ - (* Bseq continue *)
+ destruct (body_checker _ _ _) eqn:BDY1 in BDY; try discriminate.
+ generalize BDY; clear BDY.
+ inversion_SOME aliveMid; intros OALIVE BDY2. inv OALIVE.
+ exploit IHib1; eauto.
+ intros (rs2' & ISTEP1 & REGS1). rewrite ISTEP1; simpl.
+ eapply IHib2; eauto.
+ - (* Bcond *)
+ generalize BDY; clear BDY.
+ inversion_SOME oaliveSo; inversion_SOME oaliveNot; intros BDY1 BDY2 JOIN.
+ erewrite <- eqlive_reg_listmem; eauto.
+ rewrite EVAL.
+ destruct b; [ exploit IHib1; eauto | exploit IHib2; eauto].
+ all:
+ intros (rs2' & ISTEP1 & REGS1);
+ econstructor; split; eauto; inv JOIN;
+ eapply eqlive_reg_monotonic; eauto;
+ intros r EXTM; apply ext_opt_meet in EXTM; intuition.
+Qed.
+
+Lemma cfgsem2fsem_ibistep_simu_Some sp f stk1 stk2 ib: forall s t rs1 m rs1' m' fin
+ (ISTEP: iblock_istep ge sp rs1 m ib rs1' m' (Some fin))
+ (FSTEP: final_step tid ge stk1 f sp rs1' m' fin t s)
+ alive1 oalive2 rs2 (REGS: eqlive_reg (ext alive1) rs1 rs2)
+ (BDY: body_checker (fn_code f) ib alive1 = Some (oalive2))
+ (LIVE: liveness_ok_function f)
+ (STACKS: list_forall2 eqlive_stackframes stk1 stk2),
+ exists rs2' s',
+ iblock_istep_run ge sp ib rs2 m = Some (out rs2' m' (Some fin))
+ /\ final_step tr_inputs ge stk2 f sp rs2' m' fin t s'
+ /\ eqlive_states s s'.
+Proof.
+ induction ib; simpl; try_simplify_someHyps;
+ repeat inversion_ASSERT; intros; inv ISTEP.
+ - (* BF *)
+ generalize BDY; clear BDY.
+ inversion_SOME x; try_simplify_someHyps; intros FCHK.
+ destruct x; exploit cfgsem2fsem_finalstep_simu; eauto.
+ intros (s2 & FSTEP' & STATES); eauto.
+ - (* Bseq stop *)
+ destruct (body_checker _ _ _) eqn:BDY1 in BDY; try discriminate.
+ generalize BDY; clear BDY.
+ inversion_SOME aliveMid. intros OALIVE BDY2. inv OALIVE.
+ exploit IHib1; eauto. intros (rs2' & s' & ISTEP1 & FSTEP1 & STATES).
+ rewrite ISTEP1; simpl.
+ do 2 eexists; intuition eauto.
+ - (* Bseq continue *)
+ destruct (body_checker _ _ _) eqn:BDY1 in BDY; try discriminate.
+ generalize BDY; clear BDY.
+ inversion_SOME aliveMid; intros OALIVE BDY2. inv OALIVE.
+ exploit cfgsem2fsem_ibistep_simu_None; eauto.
+ intros (rs2' & ISTEP1 & REGS'). rewrite ISTEP1; simpl; eauto.
+ - (* Bcond *)
+ generalize BDY; clear BDY.
+ inversion_SOME oaliveSo; inversion_SOME oaliveNot; intros BDY1 BDY2 JOIN.
+ erewrite <- eqlive_reg_listmem; eauto. rewrite EVAL.
+ destruct b; eauto.
+Qed.
+
+Lemma cfgsem2fsem_ibstep_simu stk1 stk2 f sp rs1 m rs2 ibf pc s1 t:
+ iblock_step tid (Genv.globalenv prog) stk1 f sp rs1 m ibf.(entry) t s1 ->
+ list_forall2 eqlive_stackframes stk1 stk2 ->
+ eqlive_reg (ext (input_regs ibf)) rs1 rs2 ->
+ liveness_ok_function f ->
+ (fn_code f) ! pc = Some ibf ->
+ exists s2 : state,
+ iblock_step tr_inputs (Genv.globalenv prog) stk2 f sp rs2 m ibf.(entry) t s2 /\
+ eqlive_states s1 s2.
+Proof.
+ intros STEP STACKS EQLIVE LIVE PC.
+ assert (CHECKER: liveness_ok_function f) by auto.
+ unfold liveness_ok_function in CHECKER.
+ apply decomp_liveness_checker in CHECKER; destruct CHECKER as [ibf' [ENTRY LIST]].
+ eapply PTree.elements_correct in PC as PCIN.
+ eapply list_iblock_checker_correct in LIST as IBC; eauto.
+ unfold iblock_checker in IBC. generalize IBC; clear IBC.
+ inversion_SOME alive; intros BODY _.
+ destruct STEP as (rs1' & m1' & fin' & ISTEP & FSTEP).
+ exploit cfgsem2fsem_ibistep_simu_Some; eauto.
+ intros (rs2' & s' & ISTEP' & FSTEP' & REGS).
+ rewrite <- iblock_istep_run_equiv in ISTEP'. clear ISTEP.
+ unfold iblock_step. repeat eexists; eauto.
+Qed.
+
+Local Hint Constructors step: core.
+
+Lemma cfgsem2fsem_step_simu s1 s1' t s2:
+ step tid (Genv.globalenv prog) s1 t s1' ->
+ eqlive_states s1 s2 ->
+ exists s2' : state,
+ step tr_inputs (Genv.globalenv prog) s2 t s2' /\
+ eqlive_states s1' s2'.
+Proof.
+ destruct 1 as [stack ibf f sp n rs m t s ENTRY STEP | | | ]; intros STATES.
+ - (* iblock *)
+ inv STATES; simplify_someHyps.
+ intros.
+ exploit cfgsem2fsem_ibstep_simu; eauto.
+ intros (s2 & STEP2 & EQUIV2).
+ eexists; split; eauto.
+ - (* function internal *)
+ inv STATES; inv LIVE.
+ apply liveness_checker_entrypoint in H0 as ENTRY.
+ destruct ((fn_code f) ! (fn_entrypoint f)) eqn:EQENTRY; try congruence; eauto.
+ eexists; split; repeat econstructor; eauto.
+ - (* function external *)
+ inv STATES; inv LIVE; eexists; split; econstructor; eauto.
+ - (* return *)
+ inv STATES.
+ inversion STACKS as [|s1 st1 s' s2 STACK STACKS']; subst; clear STACKS.
+ inv STACK.
+ exists (State s2 f sp pc (rs2 # res <- vres) m); split.
+ * apply exec_return.
+ * eapply eqlive_states_intro; eauto.
+Qed.
+
+Theorem cfgsem2fsem: forward_simulation (cfgsem prog) (fsem prog).
+Proof.
+ eapply forward_simulation_step with eqlive_states; simpl; eauto.
+ - destruct 1, f; intros; eexists; intuition eauto;
+ repeat (econstructor; eauto).
+ - intros s1 s2 r STATES FINAL; destruct FINAL.
+ inv STATES; inv STACKS; constructor.
+ - intros. eapply cfgsem2fsem_step_simu; eauto.
+Qed.
+
+End FSEM_SIMULATES_CFGSEM.
+
+
diff --git a/scheduling/BTL_SEimpl.v b/scheduling/BTL_SEimpl.v
new file mode 100644
index 00000000..36f49d28
--- /dev/null
+++ b/scheduling/BTL_SEimpl.v
@@ -0,0 +1,1507 @@
+Require Import Coqlib AST Maps.
+Require Import Op Registers.
+Require Import RTL BTL Errors.
+Require Import BTL_SEsimuref BTL_SEtheory OptionMonad.
+Require Import BTL_SEsimplify.
+
+Require Import Impure.ImpHCons.
+Import Notations.
+Import HConsing.
+
+Local Open Scope option_monad_scope.
+Local Open Scope impure.
+
+Import ListNotations.
+Local Open Scope list_scope.
+
+(** Tactics *)
+
+Ltac simplify_SOME x := repeat inversion_SOME x; try_simplify_someHyps.
+
+(** Notations to make lemmas more readable *)
+Notation "'sval_refines' ctx sv1 sv2" := (eval_sval ctx sv1 = eval_sval ctx sv2)
+ (only parsing, at level 0, ctx at next level, sv1 at next level, sv2 at next level): hse.
+
+Local Open Scope hse.
+
+Notation "'list_sval_refines' ctx lsv1 lsv2" := (eval_list_sval ctx lsv1 = eval_list_sval ctx lsv2)
+ (only parsing, at level 0, ctx at next level, lsv1 at next level, lsv2 at next level): hse.
+
+Notation "'smem_refines' ctx sm1 sm2" := (eval_smem ctx sm1 = eval_smem ctx sm2)
+ (only parsing, at level 0, ctx at next level, sm1 at next level, sm2 at next level): hse.
+
+(** Debug printer *)
+Definition XDEBUG {A} (x:A) (k: A -> ?? pstring): ?? unit := RET tt. (* TO REMOVE DEBUG INFO *)
+(*Definition XDEBUG {A} (x:A) (k: A -> ?? pstring): ?? unit := DO s <~ k x;; println ("DEBUG simu_check:" +; s). (* TO INSERT DEBUG INFO *)*)
+
+Definition DEBUG (s: pstring): ?? unit := XDEBUG tt (fun _ => RET s).
+
+(** * Implementation of Data-structure use in Hash-consing *)
+
+Definition sval_get_hid (sv: sval): hashcode :=
+ match sv with
+ | Sundef hid => hid
+ | Sinput _ hid => hid
+ | Sop _ _ hid => hid
+ | Sload _ _ _ _ _ hid => hid
+ end.
+
+Definition list_sval_get_hid (lsv: list_sval): hashcode :=
+ match lsv with
+ | Snil hid => hid
+ | Scons _ _ hid => hid
+ end.
+
+Definition smem_get_hid (sm: smem): hashcode :=
+ match sm with
+ | Sinit hid => hid
+ | Sstore _ _ _ _ _ hid => hid
+ end.
+
+Definition sval_set_hid (sv: sval) (hid: hashcode): sval :=
+ match sv with
+ | Sundef _ => Sundef hid
+ | Sinput r _ => Sinput r hid
+ | Sop o lsv _ => Sop o lsv hid
+ | Sload sm trap chunk addr lsv _ => Sload sm trap chunk addr lsv hid
+ end.
+
+Definition list_sval_set_hid (lsv: list_sval) (hid: hashcode): list_sval :=
+ match lsv with
+ | Snil _ => Snil hid
+ | Scons sv lsv _ => Scons sv lsv hid
+ end.
+
+Definition smem_set_hid (sm: smem) (hid: hashcode): smem :=
+ match sm with
+ | Sinit _ => Sinit hid
+ | Sstore sm chunk addr lsv srce _ => Sstore sm chunk addr lsv srce hid
+ end.
+
+Lemma sval_set_hid_correct ctx x y:
+ sval_set_hid x unknown_hid = sval_set_hid y unknown_hid ->
+ sval_refines ctx x y.
+Proof.
+ destruct x, y; intro H; inversion H; subst; simpl; auto.
+Qed.
+Local Hint Resolve sval_set_hid_correct: core.
+
+Lemma list_sval_set_hid_correct ctx x y:
+ list_sval_set_hid x unknown_hid = list_sval_set_hid y unknown_hid ->
+ list_sval_refines ctx x y.
+Proof.
+ destruct x, y; intro H; inversion H; subst; simpl; auto.
+Qed.
+Local Hint Resolve list_sval_set_hid_correct: core.
+
+Lemma smem_set_hid_correct ctx x y:
+ smem_set_hid x unknown_hid = smem_set_hid y unknown_hid ->
+ smem_refines ctx x y.
+Proof.
+ destruct x, y; intro H; inversion H; subst; simpl; auto.
+Qed.
+Local Hint Resolve smem_set_hid_correct: core.
+
+(** Now, we build the hash-Cons value from a "hash_eq".
+
+ Informal specification:
+ [hash_eq] must be consistent with the "hashed" constructors defined above.
+
+ We expect that hashinfo values in the code of these "hashed" constructors verify:
+ (hash_eq (hdata x) (hdata y) ~> true) <-> (hcodes x)=(hcodes y)
+*)
+
+Definition sval_hash_eq (sv1 sv2: sval): ?? bool :=
+ match sv1, sv2 with
+ | Sinput r1 _, Sinput r2 _ => struct_eq r1 r2 (* NB: really need a struct_eq here ? *)
+ | Sop op1 lsv1 _, Sop op2 lsv2 _ =>
+ DO b1 <~ phys_eq lsv1 lsv2;;
+ if b1
+ then struct_eq op1 op2 (* NB: really need a struct_eq here ? *)
+ else RET false
+ | Sload sm1 trap1 chk1 addr1 lsv1 _, Sload sm2 trap2 chk2 addr2 lsv2 _ =>
+ DO b1 <~ phys_eq lsv1 lsv2;;
+ DO b2 <~ phys_eq sm1 sm2;;
+ DO b3 <~ struct_eq trap1 trap2;;
+ DO b4 <~ struct_eq chk1 chk2;;
+ if b1 && b2 && b3 && b4
+ then struct_eq addr1 addr2
+ else RET false
+ | _,_ => RET false
+ end.
+
+Lemma and_true_split a b: a && b = true <-> a = true /\ b = true.
+Proof.
+ destruct a; simpl; intuition.
+Qed.
+
+Lemma sval_hash_eq_correct x y:
+ WHEN sval_hash_eq x y ~> b THEN
+ b = true -> sval_set_hid x unknown_hid = sval_set_hid y unknown_hid.
+Proof.
+ destruct x, y; wlp_simplify; try (rewrite !and_true_split in *); intuition; subst; try congruence.
+Qed.
+Global Opaque sval_hash_eq.
+Local Hint Resolve sval_hash_eq_correct: wlp.
+
+Definition list_sval_hash_eq (lsv1 lsv2: list_sval): ?? bool :=
+ match lsv1, lsv2 with
+ | Snil _, Snil _ => RET true
+ | Scons sv1 lsv1' _, Scons sv2 lsv2' _ =>
+ DO b <~ phys_eq lsv1' lsv2';;
+ if b
+ then phys_eq sv1 sv2
+ else RET false
+ | _,_ => RET false
+ end.
+
+Lemma list_sval_hash_eq_correct x y:
+ WHEN list_sval_hash_eq x y ~> b THEN
+ b = true -> list_sval_set_hid x unknown_hid = list_sval_set_hid y unknown_hid.
+Proof.
+ destruct x, y; wlp_simplify; try (rewrite !and_true_split in *); intuition; subst; try congruence.
+Qed.
+Global Opaque list_sval_hash_eq.
+Local Hint Resolve list_sval_hash_eq_correct: wlp.
+
+Definition smem_hash_eq (sm1 sm2: smem): ?? bool :=
+ match sm1, sm2 with
+ | Sinit _, Sinit _ => RET true
+ | Sstore sm1 chk1 addr1 lsv1 sv1 _, Sstore sm2 chk2 addr2 lsv2 sv2 _ =>
+ DO b1 <~ phys_eq lsv1 lsv2;;
+ DO b2 <~ phys_eq sm1 sm2;;
+ DO b3 <~ phys_eq sv1 sv2;;
+ DO b4 <~ struct_eq chk1 chk2;;
+ if b1 && b2 && b3 && b4
+ then struct_eq addr1 addr2
+ else RET false
+ | _,_ => RET false
+ end.
+
+Lemma smem_hash_eq_correct x y:
+ WHEN smem_hash_eq x y ~> b THEN
+ b = true -> smem_set_hid x unknown_hid = smem_set_hid y unknown_hid.
+Proof.
+ destruct x, y; wlp_simplify; try (rewrite !and_true_split in *); intuition; subst; try congruence.
+Qed.
+Global Opaque smem_hash_eq.
+Local Hint Resolve smem_hash_eq_correct: wlp.
+
+Definition hSVAL: hashP sval := {| hash_eq := sval_hash_eq; get_hid:=sval_get_hid; set_hid:=sval_set_hid |}.
+Definition hLSVAL: hashP list_sval := {| hash_eq := list_sval_hash_eq; get_hid:= list_sval_get_hid; set_hid:= list_sval_set_hid |}.
+Definition hSMEM: hashP smem := {| hash_eq := smem_hash_eq; get_hid:= smem_get_hid; set_hid:= smem_set_hid |}.
+
+Program Definition mk_hash_params: Dict.hash_params sval :=
+ {|
+ Dict.test_eq := phys_eq;
+ Dict.hashing := fun (sv: sval) => RET (sval_get_hid sv);
+ Dict.log := fun sv =>
+ DO sv_name <~ string_of_hashcode (sval_get_hid sv);;
+ println ("unexpected undef behavior of hashcode:" +; (CamlStr sv_name)) |}.
+Obligation 1.
+ wlp_simplify.
+Qed.
+
+(** * Implementation of symbolic execution *)
+Section CanonBuilding.
+
+Variable hC_sval: hashinfo sval -> ?? sval.
+
+Hypothesis hC_sval_correct: forall s,
+ WHEN hC_sval s ~> s' THEN forall ctx,
+ sval_refines ctx (hdata s) s'.
+
+Variable hC_list_sval: hashinfo list_sval -> ?? list_sval.
+Hypothesis hC_list_sval_correct: forall lh,
+ WHEN hC_list_sval lh ~> lh' THEN forall ctx,
+ list_sval_refines ctx (hdata lh) lh'.
+
+Variable hC_smem: hashinfo smem -> ?? smem.
+Hypothesis hC_smem_correct: forall hm,
+ WHEN hC_smem hm ~> hm' THEN forall ctx,
+ smem_refines ctx (hdata hm) hm'.
+
+(* First, we wrap constructors for hashed values !*)
+
+Definition reg_hcode := 1.
+Definition op_hcode := 2.
+Definition load_hcode := 3.
+Definition undef_code := 4.
+
+Definition hSinput_hcodes (r: reg) :=
+ DO hc <~ hash reg_hcode;;
+ DO sv <~ hash r;;
+ RET [hc;sv].
+Extraction Inline hSinput_hcodes.
+
+Definition hSinput (r:reg): ?? sval :=
+ DO sv <~ hSinput_hcodes r;;
+ hC_sval {| hdata:=Sinput r unknown_hid; hcodes :=sv; |}.
+
+Lemma hSinput_correct r:
+ WHEN hSinput r ~> sv THEN forall ctx,
+ sval_refines ctx sv (Sinput r unknown_hid).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hSinput.
+Local Hint Resolve hSinput_correct: wlp.
+
+Definition hSop_hcodes (op:operation) (lsv: list_sval) :=
+ DO hc <~ hash op_hcode;;
+ DO sv <~ hash op;;
+ RET [hc;sv;list_sval_get_hid lsv].
+Extraction Inline hSop_hcodes.
+
+Definition hSop (op:operation) (lsv: list_sval): ?? sval :=
+ DO sv <~ hSop_hcodes op lsv;;
+ hC_sval {| hdata:=Sop op lsv unknown_hid; hcodes :=sv |}.
+
+Lemma hSop_fSop_correct op lsv:
+ WHEN hSop op lsv ~> sv THEN forall ctx,
+ sval_refines ctx sv (fSop op lsv).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hSop.
+Local Hint Resolve hSop_fSop_correct: wlp_raw.
+
+Lemma hSop_correct op lsv1:
+ WHEN hSop op lsv1 ~> sv THEN forall ctx lsv2
+ (LR: list_sval_refines ctx lsv1 lsv2),
+ sval_refines ctx sv (Sop op lsv2 unknown_hid).
+Proof.
+ wlp_xsimplify ltac:(intuition eauto with wlp wlp_raw).
+ rewrite <- LR. erewrite H; eauto.
+Qed.
+Local Hint Resolve hSop_correct: wlp.
+
+Definition hSload_hcodes (sm: smem) (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing) (lsv: list_sval):=
+ DO hc <~ hash load_hcode;;
+ DO sv1 <~ hash trap;;
+ DO sv2 <~ hash chunk;;
+ DO sv3 <~ hash addr;;
+ RET [hc; smem_get_hid sm; sv1; sv2; sv3; list_sval_get_hid lsv].
+Extraction Inline hSload_hcodes.
+
+Definition hSload (sm: smem) (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing) (lsv: list_sval): ?? sval :=
+ DO sv <~ hSload_hcodes sm trap chunk addr lsv;;
+ hC_sval {| hdata := Sload sm trap chunk addr lsv unknown_hid; hcodes := sv |}.
+
+Lemma hSload_correct sm1 trap chunk addr lsv1:
+ WHEN hSload sm1 trap chunk addr lsv1 ~> sv THEN forall ctx lsv2 sm2
+ (LR: list_sval_refines ctx lsv1 lsv2)
+ (MR: smem_refines ctx sm1 sm2),
+ sval_refines ctx sv (Sload sm2 trap chunk addr lsv2 unknown_hid).
+Proof.
+ wlp_simplify.
+ rewrite <- LR, <- MR.
+ auto.
+Qed.
+Global Opaque hSload.
+Local Hint Resolve hSload_correct: wlp.
+
+Definition hSundef_hcodes :=
+ DO hc <~ hash undef_code;;
+ RET [hc].
+Extraction Inline hSundef_hcodes.
+
+Definition hSundef : ?? sval :=
+ DO sv <~ hSundef_hcodes;;
+ hC_sval {| hdata:=Sundef unknown_hid; hcodes :=sv; |}.
+
+Lemma hSundef_correct:
+ WHEN hSundef ~> sv THEN forall ctx,
+ sval_refines ctx sv (Sundef unknown_hid).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hSundef.
+Local Hint Resolve hSundef_correct: wlp.
+
+Definition hSnil (_: unit): ?? list_sval :=
+ hC_list_sval {| hdata := Snil unknown_hid; hcodes := nil |}.
+
+Lemma hSnil_correct:
+ WHEN hSnil() ~> sv THEN forall ctx,
+ list_sval_refines ctx sv (Snil unknown_hid).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hSnil.
+Local Hint Resolve hSnil_correct: wlp.
+
+Definition hScons (sv: sval) (lsv: list_sval): ?? list_sval :=
+ hC_list_sval {| hdata := Scons sv lsv unknown_hid; hcodes := [sval_get_hid sv; list_sval_get_hid lsv] |}.
+
+Lemma hScons_correct sv1 lsv1:
+ WHEN hScons sv1 lsv1 ~> lsv1' THEN forall ctx sv2 lsv2
+ (VR: sval_refines ctx sv1 sv2)
+ (LR: list_sval_refines ctx lsv1 lsv2),
+ list_sval_refines ctx lsv1' (Scons sv2 lsv2 unknown_hid).
+Proof.
+ wlp_simplify.
+ rewrite <- VR, <- LR.
+ auto.
+Qed.
+Global Opaque hScons.
+Local Hint Resolve hScons_correct: wlp.
+
+Definition hSinit (_: unit): ?? smem :=
+ hC_smem {| hdata := Sinit unknown_hid; hcodes := nil |}.
+
+Lemma hSinit_correct:
+ WHEN hSinit() ~> hm THEN forall ctx,
+ smem_refines ctx hm (Sinit unknown_hid).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hSinit.
+Local Hint Resolve hSinit_correct: wlp.
+
+Definition hSstore_hcodes (sm: smem) (chunk: memory_chunk) (addr: addressing) (lsv: list_sval) (srce: sval):=
+ DO sv1 <~ hash chunk;;
+ DO sv2 <~ hash addr;;
+ RET [smem_get_hid sm; sv1; sv2; list_sval_get_hid lsv; sval_get_hid srce].
+Extraction Inline hSstore_hcodes.
+
+Definition hSstore (sm: smem) (chunk: memory_chunk) (addr: addressing) (lsv: list_sval) (srce: sval): ?? smem :=
+ DO sv <~ hSstore_hcodes sm chunk addr lsv srce;;
+ hC_smem {| hdata := Sstore sm chunk addr lsv srce unknown_hid; hcodes := sv |}.
+
+Lemma hSstore_correct sm1 chunk addr lsv1 sv1:
+ WHEN hSstore sm1 chunk addr lsv1 sv1 ~> sm1' THEN forall ctx lsv2 sm2 sv2
+ (LR: list_sval_refines ctx lsv1 lsv2)
+ (MR: smem_refines ctx sm1 sm2)
+ (VR: sval_refines ctx sv1 sv2),
+ smem_refines ctx sm1' (Sstore sm2 chunk addr lsv2 sv2 unknown_hid).
+Proof.
+ wlp_simplify.
+ rewrite <- LR, <- MR, <- VR.
+ auto.
+Qed.
+Global Opaque hSstore.
+Local Hint Resolve hSstore_correct: wlp.
+
+Definition hrs_sreg_get (hrs: ristate) r: ?? sval :=
+ match PTree.get r hrs with
+ | None => if ris_input_init hrs then hSinput r else hSundef
+ | Some sv => RET sv
+ end.
+
+Lemma hrs_sreg_get_correct hrs r:
+ WHEN hrs_sreg_get hrs r ~> sv THEN forall ctx (f: reg -> sval)
+ (RR: forall r, sval_refines ctx (hrs r) (f r)),
+ sval_refines ctx sv (f r).
+Proof.
+ unfold ris_sreg_get; wlp_simplify; rewrite <- RR; rewrite H; auto;
+ rewrite H0, H1; simpl; auto.
+Qed.
+Global Opaque hrs_sreg_get.
+Local Hint Resolve hrs_sreg_get_correct: wlp.
+
+Fixpoint hlist_args (hrs: ristate) (l: list reg): ?? list_sval :=
+ match l with
+ | nil => hSnil()
+ | r::l =>
+ DO v <~ hrs_sreg_get hrs r;;
+ DO lsv <~ hlist_args hrs l;;
+ hScons v lsv
+ end.
+
+Lemma hlist_args_correct hrs l:
+ WHEN hlist_args hrs l ~> lsv THEN forall ctx (f: reg -> sval)
+ (RR: forall r, sval_refines ctx (hrs r) (f r)),
+ list_sval_refines ctx lsv (list_sval_inj (List.map f l)).
+Proof.
+ induction l; wlp_simplify.
+Qed.
+Global Opaque hlist_args.
+Local Hint Resolve hlist_args_correct: wlp.
+
+(** Convert a "fake" hash-consed term into a "real" hash-consed term *)
+
+Fixpoint fsval_proj sv: ?? sval :=
+ match sv with
+ | Sundef hc =>
+ DO b <~ phys_eq hc unknown_hid;;
+ if b then hSundef else RET sv
+ | Sinput r hc =>
+ DO b <~ phys_eq hc unknown_hid;;
+ if b then hSinput r (* was not yet really hash-consed *)
+ else RET sv (* already hash-consed *)
+ | Sop op hl hc =>
+ DO b <~ phys_eq hc unknown_hid;;
+ if b then (* was not yet really hash-consed *)
+ DO hl' <~ fsval_list_proj hl;;
+ hSop op hl'
+ else RET sv (* already hash-consed *)
+ | Sload hm t chk addr hl _ => RET sv (* FIXME TODO gourdinl ? *)
+ end
+with fsval_list_proj sl: ?? list_sval :=
+ match sl with
+ | Snil hc =>
+ DO b <~ phys_eq hc unknown_hid;;
+ if b then hSnil() (* was not yet really hash-consed *)
+ else RET sl (* already hash-consed *)
+ | Scons sv hl hc =>
+ DO b <~ phys_eq hc unknown_hid;;
+ if b then (* was not yet really hash-consed *)
+ DO sv' <~ fsval_proj sv;;
+ DO hl' <~ fsval_list_proj hl;;
+ hScons sv' hl'
+ else RET sl (* already hash-consed *)
+ end.
+
+Lemma fsval_proj_correct sv:
+ WHEN fsval_proj sv ~> sv' THEN forall ctx,
+ sval_refines ctx sv sv'.
+Proof.
+ induction sv using sval_mut
+ with (P0 := fun lsv =>
+ WHEN fsval_list_proj lsv ~> lsv' THEN forall ctx,
+ eval_list_sval ctx lsv = eval_list_sval ctx lsv')
+ (P1 := fun sm => True); try (wlp_simplify; tauto).
+ - wlp_xsimplify ltac:(intuition eauto with wlp_raw wlp).
+ rewrite H, H0; auto.
+ - wlp_simplify; erewrite H0, H1; eauto.
+Qed.
+Global Opaque fsval_proj.
+Local Hint Resolve fsval_proj_correct: wlp.
+
+Lemma fsval_list_proj_correct lsv:
+ WHEN fsval_list_proj lsv ~> lsv' THEN forall ctx,
+ list_sval_refines ctx lsv lsv'.
+Proof.
+ induction lsv; wlp_simplify.
+ erewrite H0, H1; eauto.
+Qed.
+Global Opaque fsval_list_proj.
+Local Hint Resolve fsval_list_proj_correct: wlp.
+
+(** ** Assignment of memory *)
+
+Definition hrexec_store chunk addr args src hrs: ?? ristate :=
+ DO hargs <~ hlist_args hrs args;;
+ DO hsrc <~ hrs_sreg_get hrs src;;
+ DO hm <~ hSstore hrs chunk addr hargs hsrc;;
+ RET (rset_smem hm hrs).
+
+Lemma hrexec_store_correct chunk addr args src hrs:
+ WHEN hrexec_store chunk addr args src hrs ~> hrs' THEN forall ctx sis
+ (REF: ris_refines ctx hrs sis),
+ ris_refines ctx hrs' (sexec_store chunk addr args src sis).
+Proof.
+ wlp_simplify.
+ eapply rset_mem_correct; simpl; eauto.
+ - intros X; erewrite H1; eauto.
+ rewrite X. simplify_SOME z.
+ - intros X; inversion REF.
+ erewrite H1; eauto.
+Qed.
+Local Hint Resolve hrexec_store_correct: wlp.
+
+(** ** Assignment of registers *)
+
+(** locally new symbolic values during symbolic execution *)
+Inductive root_sval: Type :=
+| Rop (op: operation)
+| Rload (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing)
+.
+
+Definition root_apply (rsv: root_sval) (lr: list reg) (st: sistate): sval :=
+ let lsv := list_sval_inj (List.map (si_sreg st) lr) in
+ let sm := si_smem st in
+ match rsv with
+ | Rop op => fSop op lsv
+ | Rload trap chunk addr => fSload sm trap chunk addr lsv
+ end.
+Coercion root_apply: root_sval >-> Funclass.
+
+Definition root_happly (rsv: root_sval) (lr: list reg) (hrs: ristate): ?? sval :=
+ DO lsv <~ hlist_args hrs lr;;
+ match rsv with
+ | Rop op => hSop op lsv
+ | Rload trap chunk addr => hSload hrs trap chunk addr lsv
+ end.
+
+Lemma root_happly_correct (rsv: root_sval) lr hrs:
+ WHEN root_happly rsv lr hrs ~> sv THEN forall ctx sis
+ (REF: ris_refines ctx hrs sis)
+ (OK: ris_ok ctx hrs),
+ sval_refines ctx sv (rsv lr sis).
+Proof.
+ unfold root_apply, root_happly; destruct rsv; wlp_simplify; inv REF;
+ erewrite H0, H; eauto.
+Qed.
+Global Opaque root_happly.
+Hint Resolve root_happly_correct: wlp.
+
+Local Open Scope lazy_bool_scope.
+
+(* NB: return [false] if the rsv cannot fail *)
+Definition may_trap (rsv: root_sval) (lr: list reg): bool :=
+ match rsv with
+ | Rop op => is_trapping_op op ||| negb (Nat.eqb (length lr) (args_of_operation op)) (* cf. lemma is_trapping_op_sound *)
+ | Rload TRAP _ _ => true
+ | _ => false
+ end.
+
+Lemma lazy_orb_negb_false (b1 b2:bool):
+ (b1 ||| negb b2) = false <-> (b1 = false /\ b2 = true).
+Proof.
+ unfold negb. repeat autodestruct; simpl; intuition (try congruence).
+Qed.
+
+Lemma eval_list_sval_length ctx (f: reg -> sval) (l:list reg):
+ forall l', eval_list_sval ctx (list_sval_inj (List.map f l)) = Some l' ->
+ Datatypes.length l = Datatypes.length l'.
+Proof.
+ induction l.
+ - simpl. intros. inv H. reflexivity.
+ - simpl. intros. destruct (eval_sval _ _); [|discriminate].
+ destruct (eval_list_sval _ _) eqn:SLS; [|discriminate]. inv H. simpl.
+ erewrite IHl; eauto.
+Qed.
+
+Lemma may_trap_correct ctx (rsv: root_sval) (lr: list reg) st:
+ may_trap rsv lr = false ->
+ eval_list_sval ctx (list_sval_inj (List.map (si_sreg st) lr)) <> None ->
+ eval_smem ctx (si_smem st) <> None ->
+ eval_sval ctx (rsv lr st) <> None.
+Proof.
+ destruct rsv; simpl; try congruence.
+ - rewrite lazy_orb_negb_false. intros (TRAP1 & LEN) OK1 OK2.
+ autodestruct; try congruence. intros.
+ eapply is_trapping_op_sound; eauto.
+ erewrite <- eval_list_sval_length; eauto.
+ apply Nat.eqb_eq in LEN.
+ assumption.
+ - intros X OK1 OK2.
+ repeat autodestruct; try congruence.
+Qed.
+
+(** simplify a symbolic value before assignment to a register *)
+Definition simplify (rsv: root_sval) (lr: list reg) (hrs: ristate): ?? sval :=
+ match rsv with
+ | Rop op =>
+ match is_move_operation op lr with
+ | Some arg => hrs_sreg_get hrs arg (* optimization of Omove *)
+ | None =>
+ match target_op_simplify op lr hrs with
+ | Some fsv => fsval_proj fsv
+ | None =>
+ DO lhsv <~ hlist_args hrs lr;;
+ hSop op lhsv
+ end
+ end
+ | Rload _ chunk addr =>
+ DO lsv <~ hlist_args hrs lr;;
+ hSload hrs NOTRAP chunk addr lsv
+ end.
+
+Lemma simplify_correct rsv lr hrs:
+ WHEN simplify rsv lr hrs ~> sv THEN forall ctx sis
+ (REF: ris_refines ctx hrs sis)
+ (OK0: ris_ok ctx hrs)
+ (OK1: eval_sval ctx (rsv lr sis) <> None),
+ sval_refines ctx sv (rsv lr sis).
+Proof.
+ destruct rsv; simpl; auto.
+ - (* Rop *)
+ destruct (is_move_operation _ _) eqn: Hmove.
+ { wlp_simplify; exploit is_move_operation_correct; eauto.
+ intros (Hop & Hlsv); subst; simpl in *. inv REF.
+ simplify_SOME z; erewrite H; eauto. }
+ destruct (target_op_simplify _ _ _) eqn: Htarget_op_simp; wlp_simplify.
+ { destruct (eval_list_sval _ _) eqn: OKlist; try congruence.
+ rewrite <- H; exploit target_op_simplify_correct; eauto. }
+ inv REF; clear Htarget_op_simp.
+ intuition eauto.
+ - (* Rload *)
+ destruct trap; wlp_simplify; inv REF.
+ + erewrite H0; eauto.
+ erewrite H; [|eapply REG_EQ; eauto].
+ erewrite MEM_EQ; eauto.
+ repeat simplify_SOME z.
+ * destruct (Memory.Mem.loadv _ _ _); try congruence.
+ * rewrite H1 in OK1; congruence.
+ + erewrite H0; eauto.
+Qed.
+Global Opaque simplify.
+Local Hint Resolve simplify_correct: wlp.
+
+Definition red_PTree_set (r: reg) (sv: sval) (hrs: ristate): PTree.t sval :=
+ match (ris_input_init hrs), sv with
+ | true, Sinput r' _ =>
+ if Pos.eq_dec r r'
+ then PTree.remove r' hrs
+ else PTree.set r sv hrs
+ | false, Sundef _ =>
+ PTree.remove r hrs
+ | _, _ => PTree.set r sv hrs
+ end.
+
+Lemma red_PTree_set_correct (r r0:reg) sv (hrs: ristate) ctx:
+ sval_refines ctx ((ris_sreg_set hrs (red_PTree_set r sv hrs)) r0) ((ris_sreg_set hrs (PTree.set r sv hrs)) r0).
+Proof.
+ unfold red_PTree_set, ris_sreg_set, ris_sreg_get; simpl.
+ destruct (ris_input_init hrs) eqn:Hinit, sv; simpl; auto.
+ 1: destruct (Pos.eq_dec r r1); auto; subst;
+ rename r1 into r.
+ all: destruct (Pos.eq_dec r r0); auto;
+ [ subst; rewrite PTree.grs, PTree.gss; simpl; auto |
+ rewrite PTree.gro, PTree.gso; simpl; auto].
+Qed.
+
+Lemma red_PTree_set_refines (r r0:reg) ctx hrs sis sv1 sv2:
+ ris_refines ctx hrs sis ->
+ sval_refines ctx sv1 sv2 ->
+ ris_ok ctx hrs ->
+ sval_refines ctx (ris_sreg_set hrs (red_PTree_set r sv1 hrs) r0) (if Pos.eq_dec r r0 then sv2 else si_sreg sis r0).
+Proof.
+ intros REF SREF OK; rewrite red_PTree_set_correct.
+ unfold ris_sreg_set, ris_sreg_get.
+ destruct (Pos.eq_dec r r0).
+ - subst; simpl. rewrite PTree.gss; simpl; auto.
+ - inv REF; simpl. rewrite PTree.gso; simpl; eauto.
+Qed.
+
+Definition cbranch_expanse (prev: ristate) (cond: condition) (args: list reg): ?? (condition * list_sval) :=
+ match target_cbranch_expanse prev cond args with
+ | Some (cond', vargs) =>
+ DO vargs' <~ fsval_list_proj vargs;;
+ RET (cond', vargs')
+ | None =>
+ DO vargs <~ hlist_args prev args ;;
+ RET (cond, vargs)
+ end.
+
+Lemma cbranch_expanse_correct hrs c l:
+ WHEN cbranch_expanse hrs c l ~> r THEN forall ctx sis
+ (REF : ris_refines ctx hrs sis)
+ (OK: ris_ok ctx hrs),
+ eval_scondition ctx (fst r) (snd r) =
+ eval_scondition ctx c (list_sval_inj (map (si_sreg sis) l)).
+Proof.
+ unfold cbranch_expanse.
+ destruct (target_cbranch_expanse _ _ _) eqn: TARGET; wlp_simplify;
+ unfold eval_scondition; try erewrite <- H; inversion REF; eauto.
+ destruct p as [c' l']; simpl.
+ exploit target_cbranch_expanse_correct; eauto.
+Qed.
+Local Hint Resolve cbranch_expanse_correct: wlp.
+Global Opaque cbranch_expanse.
+
+Definition some_or_fail {A} (o: option A) (msg: pstring): ?? A :=
+ match o with
+ | Some x => RET x
+ | None => FAILWITH msg
+ end.
+
+Definition hrs_init: ?? ristate
+ := DO hm <~ hSinit ();;
+ RET {| ris_smem := hm; ris_input_init := true; ok_rsval := nil; ris_sreg := PTree.empty _ |}.
+
+Lemma hrs_init_correct:
+ WHEN hrs_init ~> hrs THEN
+ forall ctx, ris_refines ctx hrs sis_init.
+Proof.
+ unfold hrs_init, sis_init; wlp_simplify.
+ econstructor; simpl in *; eauto.
+ + split; destruct 1; econstructor; simpl in *;
+ try rewrite H; try congruence; trivial.
+ + destruct 1; simpl in *. unfold ris_sreg_get; simpl.
+ intros; rewrite PTree.gempty; eauto.
+Qed.
+Local Hint Resolve hrs_init_correct: wlp.
+Global Opaque hrs_init.
+
+Definition hrs_sreg_set r lr rsv (hrs: ristate): ?? ristate :=
+ DO ok_lsv <~
+ (if may_trap rsv lr
+ then DO sv <~ root_happly rsv lr hrs;;
+ XDEBUG sv (fun sv => DO sv_name <~ string_of_hashcode (sval_get_hid sv);; RET ("-- insert undef behavior of hashcode:" +; (CamlStr sv_name))%string);;
+ RET (sv::(ok_rsval hrs))
+ else RET (ok_rsval hrs));;
+ DO simp <~ simplify rsv lr hrs;;
+ RET {| ris_smem := hrs.(ris_smem);
+ ris_input_init := hrs.(ris_input_init);
+ ok_rsval := ok_lsv;
+ ris_sreg:= red_PTree_set r simp hrs |}.
+
+Lemma ok_hrset_sreg (rsv:root_sval) ctx (st: sistate) r lr:
+ si_ok ctx (set_sreg r (rsv lr st) st)
+ <-> (si_ok ctx st /\ eval_sval ctx (rsv lr st) <> None).
+Proof.
+ unfold set_sreg; simpl; split.
+ - intros. destruct H as [[OK_SV OK_PRE] OK_SMEM OK_SREG]; simpl in *.
+ repeat (split; try tauto).
+ + intros r0; generalize (OK_SREG r0); clear OK_SREG; destruct (Pos.eq_dec r r0); try congruence.
+ + generalize (OK_SREG r); clear OK_SREG; destruct (Pos.eq_dec r r); try congruence.
+ - intros (OK & SEVAL). inv OK.
+ repeat (split; try tauto; eauto).
+ intros r0; destruct (Pos.eq_dec r r0) eqn:Heq; simpl;
+ rewrite Heq; eauto.
+Qed.
+
+Lemma eval_list_sval_inj_not_none ctx st: forall l,
+ (forall r, List.In r l -> eval_sval ctx (si_sreg st r) = None -> False) ->
+ eval_list_sval ctx (list_sval_inj (map (si_sreg st) l)) = None -> False.
+Proof.
+ induction l.
+ - intuition discriminate.
+ - intros ALLR. simpl.
+ inversion_SOME v.
+ + intro SVAL. inversion_SOME lv; [discriminate|].
+ assert (forall r : reg, In r l -> eval_sval ctx (si_sreg st r) = None -> False).
+ { intros r INR. eapply ALLR. right. assumption. }
+ intro SVALLIST. intro. eapply IHl; eauto.
+ + intros. exploit (ALLR a); simpl; eauto.
+Qed.
+
+Lemma hrs_sreg_set_correct r lr rsv hrs:
+ WHEN hrs_sreg_set r lr rsv hrs ~> hrs' THEN forall ctx sis
+ (REF: ris_refines ctx hrs sis),
+ ris_refines ctx hrs' (set_sreg r (rsv lr sis) sis).
+Proof.
+ wlp_simplify; inversion REF.
+ - (* may_trap -> true *)
+ assert (X: si_ok ctx (set_sreg r (rsv lr sis) sis) <->
+ ris_ok ctx {| ris_smem := hrs;
+ ris_input_init := ris_input_init hrs;
+ ok_rsval := exta :: ok_rsval hrs;
+ ris_sreg := red_PTree_set r exta0 hrs |}).
+ {
+ rewrite ok_hrset_sreg, OK_EQUIV.
+ split.
+ + intros (ROK & SEVAL); inv ROK.
+ assert (ROK: ris_ok ctx hrs) by (econstructor; eauto).
+ econstructor; eauto; simpl.
+ intuition (subst; eauto).
+ erewrite H0 in *; eauto.
+ + intros (OK_RMEM & OK_RREG); simpl in *.
+ assert (ROK: ris_ok ctx hrs) by (econstructor; eauto).
+ erewrite <- H0 in *; eauto. }
+ split; auto; rewrite <- X, ok_hrset_sreg.
+ + intuition eauto.
+ + intros (SOK & SEVAL) r0.
+ rewrite ris_sreg_set_access.
+ erewrite red_PTree_set_refines; intuition eauto.
+ - (* may_trap -> false *)
+ assert (X: si_ok ctx (set_sreg r (rsv lr sis) sis) <->
+ ris_ok ctx {| ris_smem := hrs;
+ ris_input_init := ris_input_init hrs;
+ ok_rsval := ok_rsval hrs;
+ ris_sreg := red_PTree_set r exta hrs |}).
+ {
+ rewrite ok_hrset_sreg, OK_EQUIV.
+ split.
+ + intros (ROK & SEVAL); inv ROK.
+ econstructor; eauto.
+ + intros (OK_RMEM & OK_RREG).
+ assert (ROK: ris_ok ctx hrs) by (econstructor; eauto).
+ split; auto.
+ intros SNONE; exploit may_trap_correct; eauto.
+ * intros LNONE; eapply eval_list_sval_inj_not_none; eauto.
+ assert (SOK: si_ok ctx sis) by intuition.
+ inv SOK. intuition eauto.
+ * rewrite <- MEM_EQ; auto. }
+ split; auto; rewrite <- X, ok_hrset_sreg.
+ + intuition eauto.
+ + intros (SOK & SEVAL) r0.
+ rewrite ris_sreg_set_access.
+ erewrite red_PTree_set_refines; intuition eauto.
+Qed.
+
+Lemma hrs_ok_op_okpreserv ctx op args dest hrs:
+ WHEN hrs_sreg_set dest args (Rop op) hrs ~> rst THEN
+ ris_ok ctx rst -> ris_ok ctx hrs.
+Proof.
+ wlp_simplify; inv H2 || inv H1;
+ simpl in *; econstructor; eauto.
+Qed.
+
+Lemma hrs_ok_load_okpreserv ctx chunk addr args dest hrs trap:
+ WHEN hrs_sreg_set dest args (Rload trap chunk addr) hrs ~> rst THEN
+ ris_ok ctx rst -> ris_ok ctx hrs.
+Proof.
+ wlp_simplify; inv H2 || inv H1;
+ simpl in *; econstructor; eauto.
+Qed.
+
+Lemma hrs_ok_store_okpreserv ctx chunk addr args src hrs:
+ WHEN hrexec_store chunk addr args src hrs ~> rst THEN
+ ris_ok ctx rst -> ris_ok ctx hrs.
+Proof.
+ unfold hrexec_store; wlp_simplify.
+ generalize H2. erewrite ok_rset_mem; intuition.
+ specialize H1 with (sm2 := hrs).
+ erewrite H1; eauto. rewrite H3. repeat autodestruct.
+Qed.
+
+Global Opaque hrs_sreg_set hrexec_store.
+Local Hint Resolve hrs_sreg_set_correct: wlp.
+
+(* transfer *)
+
+Lemma ok_hrset_red_sreg ctx r rsv hrs:
+ ris_ok ctx (ris_sreg_set hrs (red_PTree_set r rsv hrs))
+ <-> (ris_ok ctx hrs).
+Proof.
+ split; destruct 1; econstructor; simpl in *; eauto.
+Qed.
+
+Fixpoint transfer_hrs (inputs: list reg) (hrs: ristate): ?? ristate :=
+ match inputs with
+ | nil => RET {| ris_smem := hrs.(ris_smem);
+ ris_input_init := false;
+ ok_rsval := hrs.(ok_rsval);
+ ris_sreg:= PTree.empty _
+ |}
+ | r::l =>
+ DO hrs' <~ transfer_hrs l hrs;;
+ DO sv <~ hrs_sreg_get hrs r;;
+ RET (ris_sreg_set hrs' (red_PTree_set r sv hrs'))
+ end.
+
+Lemma ok_transfer_hrs ctx inputs hrs:
+ WHEN transfer_hrs inputs hrs ~> hrs' THEN
+ ris_ok ctx hrs'
+ <-> (ris_ok ctx hrs).
+Proof.
+ induction inputs as [|r l]; simpl; wlp_simplify;
+ try (inv H; econstructor; eauto; fail).
+ + apply H0; rewrite <- ok_hrset_red_sreg; eauto.
+ + rewrite ok_hrset_red_sreg; auto.
+Qed.
+Local Hint Resolve ok_transfer_hrs: wlp.
+
+Lemma transfer_hrs_correct ctx inputs hrs sis:
+ WHEN transfer_hrs inputs hrs ~> hrs' THEN
+ ris_refines ctx hrs sis ->
+ ris_refines ctx hrs' (transfer_sis inputs sis).
+Proof.
+ unfold transfer_hrs;
+ induction inputs as [|r l]; wlp_simplify.
+ inversion H.
+ + constructor.
+ * erewrite ok_transfer_sis; split; intros X.
+ - constructor; simpl; rewrite OK_EQUIV in X; inv X; assumption.
+ - rewrite OK_EQUIV; constructor; inv X; assumption.
+ * intros X; inv X; simpl; apply MEM_EQ; constructor; auto.
+ * simpl. unfold ris_sreg_get.
+ intros;rewrite PTree.gempty. simpl; auto.
+ + constructor.
+ * erewrite ok_hrset_red_sreg; eauto.
+ inv H2. rewrite <- OK_EQUIV.
+ erewrite !ok_transfer_sis; reflexivity.
+ * intros X; inversion X; simpl in *.
+ rewrite ok_hrset_red_sreg in X.
+ inv H2; rewrite MEM_EQ; auto.
+ * simpl; intros X r0. inversion H2.
+ rewrite ok_hrset_red_sreg in X.
+ erewrite red_PTree_set_refines; eauto.
+ destruct (Pos.eq_dec); auto.
+ inv H1. rewrite ok_transfer_sis in OK_EQUIV.
+ rewrite <- OK_EQUIV, OK_EQUIV0 in X.
+ erewrite H0; eauto.
+Qed.
+
+Definition tr_hrs := poly_tr (fun f tbl or => transfer_hrs (Regset.elements (pre_inputs f tbl or))).
+
+Local Hint Resolve transfer_hrs_correct ok_transfer_hrs: core.
+Local Opaque transfer_hrs.
+
+Lemma ok_tr_hrs ctx fi hrs:
+ WHEN tr_hrs (cf ctx) fi hrs ~> hrs' THEN
+ ris_ok ctx hrs'
+ <-> (ris_ok ctx hrs).
+Proof.
+ destruct fi; simpl; eauto.
+Qed.
+
+Lemma tr_hrs_correct ctx fi hrs sis:
+ WHEN tr_hrs (cf ctx) fi hrs ~> hrs' THEN
+ ris_refines ctx hrs sis ->
+ ris_refines ctx hrs' (tr_sis (cf ctx) fi sis).
+Proof.
+ unfold tr_hrs, poly_tr; destruct fi; wlp_simplify;
+ exploit transfer_hrs_correct; eauto.
+ Unshelve. all: auto.
+Qed.
+
+Fixpoint hbuiltin_arg (hrs: ristate) (arg : builtin_arg reg): ?? builtin_arg sval :=
+ match arg with
+ | BA r =>
+ DO v <~ hrs_sreg_get hrs r;;
+ RET (BA v)
+ | BA_int n => RET (BA_int n)
+ | BA_long n => RET (BA_long n)
+ | BA_float f0 => RET (BA_float f0)
+ | BA_single s => RET (BA_single s)
+ | BA_loadstack chunk ptr => RET (BA_loadstack chunk ptr)
+ | BA_addrstack ptr => RET (BA_addrstack ptr)
+ | BA_loadglobal chunk id ptr => RET (BA_loadglobal chunk id ptr)
+ | BA_addrglobal id ptr => RET (BA_addrglobal id ptr)
+ | BA_splitlong ba1 ba2 =>
+ DO v1 <~ hbuiltin_arg hrs ba1;;
+ DO v2 <~ hbuiltin_arg hrs ba2;;
+ RET (BA_splitlong v1 v2)
+ | BA_addptr ba1 ba2 =>
+ DO v1 <~ hbuiltin_arg hrs ba1;;
+ DO v2 <~ hbuiltin_arg hrs ba2;;
+ RET (BA_addptr v1 v2)
+ end.
+
+Lemma hbuiltin_arg_correct hrs arg:
+ WHEN hbuiltin_arg hrs arg ~> hargs THEN forall ctx (f: reg -> sval)
+ (RR: forall r, sval_refines ctx (hrs r) (f r)),
+ eval_builtin_sval ctx hargs = eval_builtin_sval ctx (builtin_arg_map f arg).
+Proof.
+ induction arg; wlp_simplify.
+ + erewrite H; eauto.
+ + erewrite H; eauto.
+ erewrite H0; eauto.
+ + erewrite H; eauto.
+ erewrite H0; eauto.
+Qed.
+Global Opaque hbuiltin_arg.
+Local Hint Resolve hbuiltin_arg_correct: wlp.
+
+Fixpoint hbuiltin_args hrs (args: list (builtin_arg reg)): ?? list (builtin_arg sval) :=
+ match args with
+ | nil => RET nil
+ | a::l =>
+ DO ha <~ hbuiltin_arg hrs a;;
+ DO hl <~ hbuiltin_args hrs l;;
+ RET (ha::hl)
+ end.
+
+Lemma hbuiltin_args_correct hrs args:
+ WHEN hbuiltin_args hrs args ~> hargs THEN forall ctx (f: reg -> sval)
+ (RR: forall r, sval_refines ctx (hrs r) (f r)),
+ bargs_refines ctx hargs (List.map (builtin_arg_map f) args).
+Proof.
+ unfold bargs_refines; induction args; wlp_simplify.
+ erewrite H; eauto.
+ erewrite H0; eauto.
+Qed.
+Global Opaque hbuiltin_args.
+Local Hint Resolve hbuiltin_args_correct: wlp.
+
+Definition hsum_left hrs (ros: reg + ident): ?? (sval + ident) :=
+ match ros with
+ | inl r => DO hr <~ hrs_sreg_get hrs r;; RET (inl hr)
+ | inr s => RET (inr s)
+ end.
+
+Lemma hsum_left_correct hrs ros:
+ WHEN hsum_left hrs ros ~> hsi THEN forall ctx (f: reg -> sval)
+ (RR: forall r, sval_refines ctx (hrs r) (f r)),
+ rsvident_refines ctx hsi (sum_left_map f ros).
+Proof.
+ destruct ros; wlp_simplify;
+ econstructor; eauto.
+Qed.
+Global Opaque hsum_left.
+Local Hint Resolve hsum_left_correct: wlp.
+
+(** * Symbolic execution of final step *)
+Definition hrexec_final_sfv (i: final) hrs: ?? sfval :=
+ match i with
+ | Bgoto pc => RET (Sgoto pc)
+ | Bcall sig ros args res pc =>
+ DO svos <~ hsum_left hrs ros;;
+ DO sargs <~ hlist_args hrs args;;
+ RET (Scall sig svos sargs res pc)
+ | Btailcall sig ros args =>
+ DO svos <~ hsum_left hrs ros;;
+ DO sargs <~ hlist_args hrs args;;
+ RET (Stailcall sig svos sargs)
+ | Bbuiltin ef args res pc =>
+ DO sargs <~ hbuiltin_args hrs args;;
+ RET (Sbuiltin ef sargs res pc)
+ | Breturn or =>
+ match or with
+ | Some r => DO hr <~ hrs_sreg_get hrs r;; RET (Sreturn (Some hr))
+ | None => RET (Sreturn None)
+ end
+ | Bjumptable reg tbl =>
+ DO sv <~ hrs_sreg_get hrs reg;;
+ RET (Sjumptable sv tbl)
+ end.
+
+Lemma hrexec_final_sfv_correct ctx fi hrs sis:
+ WHEN hrexec_final_sfv fi hrs ~> hrs' THEN forall
+ (REF: ris_refines ctx hrs sis)
+ (OK: ris_ok ctx hrs),
+ rfv_refines ctx hrs' (sexec_final_sfv fi sis).
+Proof.
+ destruct fi; simpl; wlp_simplify; repeat econstructor;
+ try inversion REF; try erewrite H; eauto.
+Qed.
+
+Fixpoint hrexec_rec f ib hrs (k: ristate -> ?? rstate): ?? rstate :=
+ match ib with
+ | BF fin _ =>
+ DO sfv <~ hrexec_final_sfv fin hrs;;
+ DO hrs' <~ tr_hrs f fin hrs;;
+ RET (Rfinal hrs' sfv)
+ (* basic instructions *)
+ | Bnop _ => k hrs
+ | Bop op args dst _ =>
+ DO next <~ hrs_sreg_set dst args (Rop op) hrs;;
+ k next
+ | Bload trap chunk addr args dst _ =>
+ DO next <~ hrs_sreg_set dst args (Rload trap chunk addr) hrs;;
+ k next
+ | Bstore chunk addr args src _ =>
+ DO next <~ hrexec_store chunk addr args src hrs;;
+ k next
+ (* composed instructions *)
+ | Bseq ib1 ib2 =>
+ hrexec_rec f ib1 hrs (fun hrs2 => hrexec_rec f ib2 hrs2 k)
+ | Bcond cond args ifso ifnot _ =>
+ DO res <~ cbranch_expanse hrs cond args;;
+ let (cond, vargs) := res in
+ DO ifso <~ hrexec_rec f ifso hrs k;;
+ DO ifnot <~ hrexec_rec f ifnot hrs k;;
+ RET (Rcond cond vargs ifso ifnot)
+ end
+ .
+
+Definition hrexec f ib :=
+ DO init <~ hrs_init;;
+ hrexec_rec f ib init (fun _ => RET Rabort).
+
+Local Hint Resolve exec_final_refpreserv ok_tr_hrs: core.
+Local Hint Constructors rst_refines: core.
+
+Lemma hrexec_rec_correct1 ctx ib:
+ forall rk k
+ (CONTh: forall sis lsis sfv, get_soutcome ctx (k sis) = Some (sout lsis sfv) -> si_ok ctx lsis -> si_ok ctx sis)
+ (CONT: forall hrs sis lsis sfv st,
+ ris_refines ctx hrs sis ->
+ k sis = st ->
+ get_soutcome ctx (k sis) = Some (sout lsis sfv) ->
+ si_ok ctx lsis ->
+ WHEN rk hrs ~> rst THEN
+ rst_refines ctx rst (k sis))
+ hrs sis lsis sfv st
+ (REF: ris_refines ctx hrs sis)
+ (EXEC: sexec_rec (cf ctx) ib sis k = st)
+ (SOUT: get_soutcome ctx st = Some (sout lsis sfv))
+ (OK: si_ok ctx lsis),
+ WHEN hrexec_rec (cf ctx) ib hrs rk ~> rst THEN
+ rst_refines ctx rst st.
+Proof.
+ induction ib; try (wlp_simplify; try (eapply CONT; eauto); fail).
+ - (* BF *)
+ wlp_simplify; exploit tr_hrs_correct; eauto.
+ intros; constructor; auto.
+ intros X; apply H0 in X.
+ exploit hrexec_final_sfv_correct; eauto.
+ - (* Bseq *)
+ wlp_simplify.
+ eapply IHib1. 3-7: eauto.
+ + simpl. eapply sexec_rec_okpreserv; eauto.
+ + intros; eapply IHib2; eauto.
+ - (* Bcond *)
+ simpl; intros; wlp_simplify.
+ assert (rOK: ris_ok ctx hrs). {
+ erewrite <- OK_EQUIV. 2: eauto.
+ eapply sexec_rec_okpreserv with (ib:=(Bcond cond args ib1 ib2 iinfo)); simpl; eauto.
+ }
+ exploit (Refcond ctx (fst exta) cond (snd exta) (list_sval_inj (map sis args)) exta0 exta1
+ (sexec_rec (cf ctx) ib1 sis k) (sexec_rec (cf ctx) ib2 sis k));
+ exploit H; eauto; intros SEVAL; auto.
+ all:
+ simpl in SOUT; generalize SOUT; clear SOUT;
+ inversion_SOME b0; try_simplify_someHyps.
+ + intros; eapply IHib1; eauto.
+ + intros; eapply IHib2; eauto.
+Qed.
+
+Lemma hrexec_correct1 ctx ib sis sfv:
+ get_soutcome ctx (sexec (cf ctx) ib) = Some (sout sis sfv) ->
+ (si_ok ctx sis) ->
+ WHEN hrexec (cf ctx) ib ~> rst THEN
+ rst_refines ctx rst (sexec (cf ctx) ib).
+Proof.
+ unfold sexec; intros; wlp_simplify.
+ eapply hrexec_rec_correct1; eauto; simpl; congruence.
+Qed.
+
+Lemma hrexec_rec_okpreserv ctx ib:
+ forall k
+ (CONT: forall hrs lhrs rfv,
+ WHEN k hrs ~> rst THEN
+ get_routcome ctx rst = Some (rout lhrs rfv) ->
+ ris_ok ctx lhrs ->
+ ris_ok ctx hrs)
+ hrs lhrs rfv,
+ WHEN hrexec_rec (cf ctx) ib hrs k ~> rst THEN
+ get_routcome ctx rst = Some (rout lhrs rfv) ->
+ ris_ok ctx lhrs ->
+ ris_ok ctx hrs.
+Proof.
+ induction ib; simpl; try (wlp_simplify; try_simplify_someHyps; fail).
+ - (* Bop *)
+ wlp_simplify; exploit hrs_ok_op_okpreserv; eauto.
+ - (* Bload *)
+ destruct trap; wlp_simplify; try_simplify_someHyps;
+ exploit hrs_ok_load_okpreserv; eauto.
+ - (* Bstore *)
+ wlp_simplify; exploit hrs_ok_store_okpreserv; eauto.
+ - (* Bcond *)
+ wlp_simplify; generalize H2; clear H2; inversion_SOME b; destruct b; try_simplify_someHyps.
+Qed.
+Local Hint Resolve hrexec_rec_okpreserv: wlp.
+
+Lemma hrexec_rec_correct2 ctx ib:
+ forall rk k
+ (CONTh: forall hrs lhrs rfv,
+ WHEN rk hrs ~> rst THEN
+ get_routcome ctx rst = Some (rout lhrs rfv) ->
+ ris_ok ctx lhrs ->
+ ris_ok ctx hrs)
+ (CONT: forall hrs sis lhrs rfv,
+ ris_refines ctx hrs sis ->
+ WHEN rk hrs ~> rst THEN
+ get_routcome ctx rst = Some (rout lhrs rfv) ->
+ ris_ok ctx lhrs ->
+ rst_refines ctx rst (k sis))
+ hrs sis lhrs rfv
+ (REF: ris_refines ctx hrs sis),
+ WHEN hrexec_rec (cf ctx) ib hrs rk ~> rst THEN
+ get_routcome ctx rst = Some (rout lhrs rfv) ->
+ ris_ok ctx lhrs ->
+ rst_refines ctx rst (sexec_rec (cf ctx) ib sis k).
+Proof.
+ induction ib; try (wlp_simplify; fail).
+ - (* BF *)
+ wlp_simplify; exploit tr_hrs_correct; eauto.
+ intros; constructor; auto.
+ intros X; apply H0 in X.
+ exploit hrexec_final_sfv_correct; eauto.
+ - (* Bnop *) wlp_simplify; eapply CONT; eauto.
+ - (* Bseq *)
+ wlp_simplify.
+ eapply IHib1. 3-6: eauto.
+ + simpl; eapply hrexec_rec_okpreserv; eauto.
+ + intros; eapply IHib2; eauto.
+ - (* Bcond *)
+ simpl; intros; wlp_simplify.
+ assert (rOK: ris_ok ctx hrs). {
+ simpl in H2; generalize H2; inversion_SOME b0; destruct b0; try_simplify_someHyps.
+ }
+ exploit (Refcond ctx (fst exta) cond (snd exta) (list_sval_inj (map sis args)) exta0 exta1
+ (sexec_rec (cf ctx) ib1 sis k) (sexec_rec (cf ctx) ib2 sis k));
+ exploit H; eauto; intros SEVAL; auto.
+ all:
+ simpl in H2; generalize H2; clear H2;
+ inversion_SOME b0; try_simplify_someHyps.
+ + intros; eapply IHib1; eauto.
+ + intros; eapply IHib2; eauto.
+ Unshelve. all: auto.
+Qed.
+
+Lemma hrexec_correct2 ctx ib hrs rfv:
+ WHEN hrexec (cf ctx) ib ~> rst THEN
+ get_routcome ctx rst = Some (rout hrs rfv) ->
+ (ris_ok ctx hrs) ->
+ rst_refines ctx rst (sexec (cf ctx) ib).
+Proof.
+ unfold hrexec; intros; wlp_simplify.
+ eapply hrexec_rec_correct2; eauto; simpl;
+ wlp_simplify; congruence.
+Qed.
+
+Global Opaque hrexec.
+
+End CanonBuilding.
+
+(** * Implementing the simulation test with concrete hash-consed symbolic execution *)
+
+Definition phys_check {A} (x y:A) (msg: pstring): ?? unit :=
+ DO b <~ phys_eq x y;;
+ assert_b b msg;;
+ RET tt.
+
+Definition struct_check {A} (x y: A) (msg: pstring): ?? unit :=
+ DO b <~ struct_eq x y;;
+ assert_b b msg;;
+ RET tt.
+
+Lemma struct_check_correct {A} (a b: A) msg:
+ WHEN struct_check a b msg ~> _ THEN
+ a = b.
+Proof. wlp_simplify. Qed.
+Global Opaque struct_check.
+Hint Resolve struct_check_correct: wlp.
+
+Definition option_eq_check {A} (o1 o2: option A): ?? unit :=
+ match o1, o2 with
+ | Some x1, Some x2 => phys_check x1 x2 "option_eq_check: data physically differ"
+ | None, None => RET tt
+ | _, _ => FAILWITH "option_eq_check: structure differs"
+ end.
+
+Lemma option_eq_check_correct A (o1 o2: option A): WHEN option_eq_check o1 o2 ~> _ THEN o1=o2.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque option_eq_check.
+Hint Resolve option_eq_check_correct:wlp.
+
+Import PTree.
+
+Fixpoint PTree_eq_check {A} (d1 d2: PTree.t A): ?? unit :=
+ match d1, d2 with
+ | Leaf, Leaf => RET tt
+ | Node l1 o1 r1, Node l2 o2 r2 =>
+ option_eq_check o1 o2;;
+ PTree_eq_check l1 l2;;
+ PTree_eq_check r1 r2
+ | _, _ => FAILWITH "PTree_eq_check: some key is absent"
+ end.
+
+Lemma PTree_eq_check_correct A d1: forall (d2: t A),
+ WHEN PTree_eq_check d1 d2 ~> _ THEN forall x, PTree.get x d1 = PTree.get x d2.
+Proof.
+ induction d1 as [|l1 Hl1 o1 r1 Hr1]; destruct d2 as [|l2 o2 r2]; simpl;
+ wlp_simplify. destruct x; simpl; auto.
+Qed.
+Global Opaque PTree_eq_check.
+Local Hint Resolve PTree_eq_check_correct: wlp.
+
+Definition hrs_simu_check (hrs1 hrs2: ristate) : ?? unit :=
+ DEBUG("? hrs_simu_check");;
+ phys_check (ris_smem hrs1) (ris_smem hrs2) "hrs_simu_check: ris_smem sets aren't equiv";;
+ phys_check (ris_input_init hrs1) (ris_input_init hrs2) "hrs_simu_check: ris_input_init bools aren't equiv";;
+ PTree_eq_check (ris_sreg hrs1) (ris_sreg hrs2);;
+ Sets.assert_list_incl mk_hash_params (ok_rsval hrs2) (ok_rsval hrs1);;
+ DEBUG("=> hrs_simu_check: OK").
+
+Lemma setoid_in {A: Type} (a: A): forall l,
+ SetoidList.InA (fun x y => x = y) a l ->
+ In a l.
+Proof.
+ induction l; intros; inv H.
+ - constructor. reflexivity.
+ - right. auto.
+Qed.
+
+Lemma regset_elements_in r rs:
+ Regset.In r rs ->
+ In r (Regset.elements rs).
+Proof.
+ intros. exploit Regset.elements_1; eauto. intro SIN.
+ apply setoid_in. assumption.
+Qed.
+Local Hint Resolve regset_elements_in: core.
+
+Lemma hrs_simu_check_correct hrs1 hrs2:
+ WHEN hrs_simu_check hrs1 hrs2 ~> _ THEN
+ ris_simu hrs1 hrs2.
+Proof.
+ wlp_simplify; constructor; auto.
+ unfold ris_sreg_get; intros r; rewrite H, H1; reflexivity.
+Qed.
+Hint Resolve hrs_simu_check_correct: wlp.
+Global Opaque hrs_simu_check.
+
+Definition svos_simu_check (svos1 svos2: sval + ident) :=
+ match svos1, svos2 with
+ | inl sv1, inl sv2 => phys_check sv1 sv2 "svos_simu_check: sval mismatch"
+ | inr id1, inr id2 => phys_check id1 id2 "svos_simu_check: symbol mismatch"
+ | _, _ => FAILWITH "svos_simu_check: type mismatch"
+ end.
+
+Lemma svos_simu_check_correct svos1 svos2:
+ WHEN svos_simu_check svos1 svos2 ~> _ THEN
+ svos1 = svos2.
+Proof.
+ destruct svos1; destruct svos2; wlp_simplify.
+Qed.
+Global Opaque svos_simu_check.
+Hint Resolve svos_simu_check_correct: wlp.
+
+Fixpoint builtin_arg_simu_check (bs bs': builtin_arg sval) :=
+ match bs with
+ | BA sv =>
+ match bs' with
+ | BA sv' => phys_check sv sv' "builtin_arg_simu_check: sval mismatch"
+ | _ => FAILWITH "builtin_arg_simu_check: BA mismatch"
+ end
+ | BA_splitlong lo hi =>
+ match bs' with
+ | BA_splitlong lo' hi' =>
+ builtin_arg_simu_check lo lo';;
+ builtin_arg_simu_check hi hi'
+ | _ => FAILWITH "builtin_arg_simu_check: BA_splitlong mismatch"
+ end
+ | BA_addptr b1 b2 =>
+ match bs' with
+ | BA_addptr b1' b2' =>
+ builtin_arg_simu_check b1 b1';;
+ builtin_arg_simu_check b2 b2'
+ | _ => FAILWITH "builtin_arg_simu_check: BA_addptr mismatch"
+ end
+ | bs => struct_check bs bs' "builtin_arg_simu_check: basic mismatch"
+ end.
+
+Lemma builtin_arg_simu_check_correct: forall bs1 bs2,
+ WHEN builtin_arg_simu_check bs1 bs2 ~> _ THEN
+ bs1 = bs2.
+Proof.
+ induction bs1.
+ all: try (wlp_simplify; subst; reflexivity).
+ all: destruct bs2; wlp_simplify; congruence.
+Qed.
+Global Opaque builtin_arg_simu_check.
+Hint Resolve builtin_arg_simu_check_correct: wlp.
+
+Fixpoint list_builtin_arg_simu_check lbs1 lbs2 :=
+ match lbs1, lbs2 with
+ | nil, nil => RET tt
+ | bs1::lbs1, bs2::lbs2 =>
+ builtin_arg_simu_check bs1 bs2;;
+ list_builtin_arg_simu_check lbs1 lbs2
+ | _, _ => FAILWITH "list_builtin_arg_simu_check: length mismatch"
+ end.
+
+Lemma list_builtin_arg_simu_check_correct: forall lbs1 lbs2,
+ WHEN list_builtin_arg_simu_check lbs1 lbs2 ~> _ THEN
+ lbs1 = lbs2.
+Proof.
+ induction lbs1; destruct lbs2; wlp_simplify. congruence.
+Qed.
+Global Opaque list_builtin_arg_simu_check.
+Hint Resolve list_builtin_arg_simu_check_correct: wlp.
+
+Definition sfval_simu_check (sfv1 sfv2: sfval): ?? unit :=
+ match sfv1, sfv2 with
+ | Sgoto e1, Sgoto e2 =>
+ phys_check e1 e2 "sfval_simu_check: Sgoto successors do not match"
+ | Scall sig1 svos1 lsv1 res1 e1, Scall sig2 svos2 lsv2 res2 e2 =>
+ phys_check e1 e2 "sfval_simu_check: Scall successors do not match";;
+ phys_check sig1 sig2 "sfval_simu_check: Scall different signatures";;
+ phys_check res1 res2 "sfval_simu_check: Scall res do not match";;
+ svos_simu_check svos1 svos2;;
+ phys_check lsv1 lsv2 "sfval_simu_check: Scall args do not match"
+ | Stailcall sig1 svos1 lsv1, Stailcall sig2 svos2 lsv2 =>
+ phys_check sig1 sig2 "sfval_simu_check: Stailcall different signatures";;
+ svos_simu_check svos1 svos2;;
+ phys_check lsv1 lsv2 "sfval_simu_check: Stailcall args do not match"
+ | Sbuiltin ef1 lbs1 br1 e1, Sbuiltin ef2 lbs2 br2 e2 =>
+ phys_check e1 e2 "sfval_simu_check: Sbuiltin successors do not match";;
+ phys_check ef1 ef2 "sfval_simu_check: Sbuiltin ef do not match";;
+ phys_check br1 br2 "sfval_simu_check: Sbuiltin br do not match";;
+ list_builtin_arg_simu_check lbs1 lbs2
+ | Sjumptable sv1 le1, Sjumptable sv2 le2 =>
+ phys_check le1 le2 "sfval_simu_check: Sjumptable successors do not match";;
+ phys_check sv1 sv2 "sfval_simu_check: Sjumptable sval do not match"
+ | Sreturn osv1, Sreturn osv2 =>
+ option_eq_check osv1 osv2
+ | _, _ => FAILWITH "sfval_simu_check: structure mismatch"
+ end.
+
+Lemma sfval_simu_check_correct sfv1 sfv2:
+ WHEN sfval_simu_check sfv1 sfv2 ~> _ THEN
+ rfv_simu sfv1 sfv2.
+Proof.
+ destruct sfv1; destruct sfv2; simpl; wlp_simplify; try congruence.
+Qed.
+Hint Resolve sfval_simu_check_correct: wlp.
+Global Opaque sfval_simu_check.
+
+Fixpoint rst_simu_check (rst1 rst2: rstate) {struct rst1} :=
+ match rst1, rst2 with
+ | Rfinal hrs1 sfv1, Rfinal hrs2 sfv2 =>
+ hrs_simu_check hrs1 hrs2;;
+ sfval_simu_check sfv1 sfv2
+ | Rcond cond1 lsv1 rsL1 rsR1, Rcond cond2 lsv2 rsL2 rsR2 =>
+ struct_check cond1 cond2 "rst_simu_check: conditions do not match";;
+ phys_check lsv1 lsv2 "rst_simu_check: args do not match";;
+ rst_simu_check rsL1 rsL2;;
+ rst_simu_check rsR1 rsR2
+ | _, _ => FAILWITH "rst_simu_check: simu_check failed"
+ end.
+
+Lemma rst_simu_check_correct rst1: forall rst2,
+ WHEN rst_simu_check rst1 rst2 ~> _ THEN
+ rst_simu rst1 rst2.
+Proof.
+ induction rst1; destruct rst2;
+ wlp_simplify; constructor; auto.
+Qed.
+Hint Resolve rst_simu_check_correct: wlp.
+Global Opaque rst_simu_check.
+
+Definition simu_check_single (f1 f2: function) (ibf1 ibf2: iblock_info): ?? unit :=
+ (* creating the hash-consing tables *)
+ DO hC_sval <~ hCons hSVAL;;
+ DO hC_list_hsval <~ hCons hLSVAL;;
+ DO hC_hsmem <~ hCons hSMEM;;
+ let hrexec := hrexec hC_sval.(hC) hC_list_hsval.(hC) hC_hsmem.(hC) in
+ (* performing the hash-consed executions *)
+ DO hst1 <~ hrexec f1 ibf1.(entry);;
+ DO hst2 <~ hrexec f2 ibf2.(entry);;
+ (* comparing the executions *)
+ rst_simu_check hst1 hst2.
+
+Lemma simu_check_single_correct (f1 f2: function) (ibf1 ibf2: iblock_info):
+ WHEN simu_check_single f1 f2 ibf1 ibf2 ~> _ THEN
+ symbolic_simu f1 f2 ibf1.(entry) ibf2.(entry).
+Proof.
+ unfold symbolic_simu; wlp_simplify.
+ eapply rst_simu_correct; eauto.
+ + intros; eapply hrexec_correct1; eauto; wlp_simplify.
+ + intros; eapply hrexec_correct2; eauto; wlp_simplify.
+Qed.
+Global Opaque simu_check_single.
+Global Hint Resolve simu_check_single_correct: wlp.
+
+Program Definition aux_simu_check (f1 f2: function) (ibf1 ibf2: iblock_info): ?? bool :=
+ DO r <~
+ (TRY
+ simu_check_single f1 f2 ibf1 ibf2;;
+ RET true
+ CATCH_FAIL s, _ =>
+ println ("simu_check_failure:" +; s);;
+ RET false
+ ENSURE (fun b => b=true -> symbolic_simu f1 f2 ibf1.(entry) ibf2.(entry)));;
+ RET (`r).
+Obligation 1.
+ split; wlp_simplify. discriminate.
+Qed.
+
+Lemma aux_simu_check_correct (f1 f2: function) (ibf1 ibf2: iblock_info):
+ WHEN aux_simu_check f1 f2 ibf1 ibf2 ~> b THEN
+ b=true -> symbolic_simu f1 f2 ibf1.(entry) ibf2.(entry).
+Proof.
+ unfold aux_simu_check; wlp_simplify.
+ destruct exta; simpl; auto.
+Qed.
+
+(* Coerce aux_simu_check into a pure function (this is a little unsafe like all oracles in CompCert). *)
+
+Import UnsafeImpure.
+
+Definition simu_check (f1 f2: function) (ibf1 ibf2: iblock_info): res unit :=
+ match unsafe_coerce (aux_simu_check f1 f2 ibf1 ibf2) with
+ | Some true => OK tt
+ | _ => Error (msg "simu_check has failed")
+ end.
+
+Lemma simu_check_correct f1 f2 ibf1 ibf2:
+ simu_check f1 f2 ibf1 ibf2 = OK tt ->
+ symbolic_simu f1 f2 ibf1.(entry) ibf2.(entry).
+Proof.
+ unfold simu_check.
+ destruct (unsafe_coerce (aux_simu_check f1 f2 ibf1 ibf2)) as [[|]|] eqn:Hres; simpl; try discriminate.
+ intros; eapply aux_simu_check_correct; eauto.
+ eapply unsafe_coerce_not_really_correct; eauto.
+Qed.
diff --git a/scheduling/BTL_SEsimuref.v b/scheduling/BTL_SEsimuref.v
new file mode 100644
index 00000000..d47e53b8
--- /dev/null
+++ b/scheduling/BTL_SEsimuref.v
@@ -0,0 +1,852 @@
+(** Refinement of BTL_SEtheory data-structures
+ in order to introduce (and prove correct) a lower-level specification of the simulation test.
+*)
+
+Require Import Coqlib Maps Floats.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import Op Registers.
+Require Import RTL BTL OptionMonad BTL_SEtheory.
+
+
+Local Open Scope option_monad_scope.
+
+(** * Refinement of data-structures and of the specification of the simulation test *)
+
+Local Hint Resolve OK_PRE OK_SMEM OK_SREG: core.
+Local Hint Constructors si_ok: core.
+
+(* NB: refinement of (symbolic) internal state *)
+Record ristate :=
+ {
+ (** [ris_smem] represents the current smem symbolic evaluations.
+ (we also recover the history of smem in ris_smem) *)
+ ris_smem:> smem;
+ (** For the values in registers:
+ 1) we store a list of sval evaluations
+ 2) we encode the symbolic regset by a PTree + a boolean indicating the default sval
+ See [ris_sreg_get] below.
+ *)
+ ris_input_init: bool;
+ ok_rsval: list sval;
+ ris_sreg:> PTree.t sval
+ }.
+
+Definition ris_sreg_get (ris: ristate) r: sval :=
+ match PTree.get r ris with
+ | None => if ris_input_init ris then fSinput r else fSundef
+ | Some sv => sv
+ end.
+Coercion ris_sreg_get: ristate >-> Funclass.
+
+Definition ris_sreg_set (ris: ristate) (sreg: PTree.t sval): ristate :=
+ {| ris_smem := ris_smem ris;
+ ris_input_init := ris_input_init ris;
+ ok_rsval := ok_rsval ris;
+ ris_sreg := sreg |}.
+
+Lemma ris_sreg_set_access (ris: ristate) (sreg: PTree.t sval) r smem rsval:
+ {| ris_smem := smem;
+ ris_input_init := ris_input_init ris;
+ ok_rsval := rsval;
+ ris_sreg := sreg |} r =
+ ris_sreg_set ris sreg r.
+Proof.
+ unfold ris_sreg_set, ris_sreg_get; simpl.
+ reflexivity.
+Qed.
+
+Record ris_ok ctx (ris: ristate) : Prop := {
+ OK_RMEM: (eval_smem ctx (ris_smem ris)) <> None;
+ OK_RREG: forall sv, List.In sv (ok_rsval ris) -> eval_sval ctx sv <> None
+}.
+Local Hint Resolve OK_RMEM OK_RREG: core.
+Local Hint Constructors ris_ok: core.
+
+(**
+ NOTE that this refinement relation *cannot* be decomposed into a abstraction function of type
+ ristate -> sistate & an equivalence relation on istate.
+
+ Indeed, any [sis] satisfies [forall ctx r, si_ok ctx sis -> eval_sval ctx (sis r) <> None].
+ whereas this is generally not true for [ris] that [forall ctx r, ris_ok ctx ris -> eval_sval ctx (ris r) <> None],
+ except when, precisely, [ris_refines ris sis].
+
+ An alternative design enabling to define ris_refines as the composition of an equivalence on sistate
+ and a abstraction function would consist in constraining sistate with an additional [wf] field:
+
+ Record sistate :=
+ { si_pre: iblock_exec_context -> Prop;
+ si_sreg:> reg -> sval;
+ si_smem: smem;
+ si_wf: forall ctx, si_pre ctx -> si_smem <> None /\ forall r, si_sreg r <> None
+ }.
+
+ Such a constraint should also appear in [ristate]. This is not clear whether this alternative design
+ would be really simpler.
+
+*)
+Record ris_refines ctx (ris: ristate) (sis: sistate): Prop := {
+ OK_EQUIV: si_ok ctx sis <-> ris_ok ctx ris;
+ MEM_EQ: ris_ok ctx ris -> eval_smem ctx ris.(ris_smem) = eval_smem ctx sis.(si_smem);
+ REG_EQ: ris_ok ctx ris -> forall r, eval_sval ctx (ris r) = eval_sval ctx (sis r)
+}.
+Local Hint Resolve OK_EQUIV MEM_EQ REG_EQ: core.
+Local Hint Constructors ris_refines: core.
+
+Record ris_simu ris1 ris2: Prop := {
+ SIMU_FAILS: forall sv, List.In sv ris2.(ok_rsval) -> List.In sv ris1.(ok_rsval);
+ SIMU_MEM: ris1.(ris_smem) = ris2.(ris_smem);
+ SIMU_REG: forall r, ris1 r = ris2 r
+}.
+Local Hint Resolve SIMU_FAILS SIMU_MEM SIMU_REG: core.
+Local Hint Constructors ris_simu: core.
+Local Hint Resolve sge_match: core.
+
+Lemma ris_simu_ok_preserv f1 f2 ris1 ris2 (ctx:simu_proof_context f1 f2):
+ ris_simu ris1 ris2 -> ris_ok (bctx1 ctx) ris1 -> ris_ok (bctx2 ctx) ris2.
+Proof.
+ intros SIMU OK; econstructor; eauto.
+ - erewrite <- SIMU_MEM; eauto.
+ erewrite <- smem_eval_preserved; eauto.
+ - intros; erewrite <- eval_sval_preserved; eauto.
+Qed.
+
+Lemma ris_simu_correct f1 f2 ris1 ris2 (ctx:simu_proof_context f1 f2) sis1 sis2:
+ ris_simu ris1 ris2 ->
+ ris_refines (bctx1 ctx) ris1 sis1 ->
+ ris_refines (bctx2 ctx) ris2 sis2 ->
+ sistate_simu ctx sis1 sis2.
+Proof.
+ intros RIS REF1 REF2 rs m SEM.
+ exploit sem_si_ok; eauto.
+ erewrite OK_EQUIV; eauto.
+ intros ROK1.
+ exploit ris_simu_ok_preserv; eauto.
+ intros ROK2. generalize ROK2; erewrite <- OK_EQUIV; eauto.
+ intros SOK2.
+ destruct SEM as (PRE & SMEM & SREG).
+ unfold sem_sistate; intuition eauto.
+ + erewrite <- MEM_EQ, <- SIMU_MEM; eauto.
+ erewrite <- smem_eval_preserved; eauto.
+ erewrite MEM_EQ; eauto.
+ + erewrite <- REG_EQ, <- SIMU_REG; eauto.
+ erewrite <- eval_sval_preserved; eauto.
+ erewrite REG_EQ; eauto.
+Qed.
+
+Inductive optrsv_refines ctx: (option sval) -> (option sval) -> Prop :=
+ | RefSome rsv sv
+ (REF:eval_sval ctx rsv = eval_sval ctx sv)
+ :optrsv_refines ctx (Some rsv) (Some sv)
+ | RefNone: optrsv_refines ctx None None
+ .
+
+Inductive rsvident_refines ctx: (sval + ident) -> (sval + ident) -> Prop :=
+ | RefLeft rsv sv
+ (REF:eval_sval ctx rsv = eval_sval ctx sv)
+ :rsvident_refines ctx (inl rsv) (inl sv)
+ | RefRight id1 id2
+ (IDSIMU: id1 = id2)
+ :rsvident_refines ctx (inr id1) (inr id2)
+ .
+
+Definition bargs_refines ctx (rargs: list (builtin_arg sval)) (args: list (builtin_arg sval)): Prop :=
+ eval_list_builtin_sval ctx rargs = eval_list_builtin_sval ctx args.
+
+Inductive rfv_refines ctx: sfval -> sfval -> Prop :=
+ | RefGoto pc: rfv_refines ctx (Sgoto pc) (Sgoto pc)
+ | RefCall sig rvos ros rargs args r pc
+ (SV:rsvident_refines ctx rvos ros)
+ (LIST:eval_list_sval ctx rargs = eval_list_sval ctx args)
+ :rfv_refines ctx (Scall sig rvos rargs r pc) (Scall sig ros args r pc)
+ | RefTailcall sig rvos ros rargs args
+ (SV:rsvident_refines ctx rvos ros)
+ (LIST:eval_list_sval ctx rargs = eval_list_sval ctx args)
+ :rfv_refines ctx (Stailcall sig rvos rargs) (Stailcall sig ros args)
+ | RefBuiltin ef lbra lba br pc
+ (BARGS: bargs_refines ctx lbra lba)
+ :rfv_refines ctx (Sbuiltin ef lbra br pc) (Sbuiltin ef lba br pc)
+ | RefJumptable rsv sv lpc
+ (VAL: eval_sval ctx rsv = eval_sval ctx sv)
+ :rfv_refines ctx (Sjumptable rsv lpc) (Sjumptable sv lpc)
+ | RefReturn orsv osv
+ (OPT:optrsv_refines ctx orsv osv)
+ :rfv_refines ctx (Sreturn orsv) (Sreturn osv)
+.
+
+Definition rfv_simu (rfv1 rfv2: sfval): Prop := rfv1 = rfv2.
+
+Local Hint Resolve eval_sval_preserved list_sval_eval_preserved smem_eval_preserved eval_list_builtin_sval_preserved: core.
+
+Lemma rvf_simu_correct f1 f2 rfv1 rfv2 (ctx: simu_proof_context f1 f2) sfv1 sfv2:
+ rfv_simu rfv1 rfv2 ->
+ rfv_refines (bctx1 ctx) rfv1 sfv1 ->
+ rfv_refines (bctx2 ctx) rfv2 sfv2 ->
+ sfv_simu ctx sfv1 sfv2.
+Proof.
+ unfold rfv_simu; intros X REF1 REF2. subst.
+ unfold bctx2; destruct REF1; inv REF2; simpl; econstructor; eauto.
+ - (* call svid *)
+ inv SV; inv SV0; econstructor; eauto.
+ rewrite <- REF, <- REF0; eauto.
+ - (* call args *)
+ rewrite <- LIST, <- LIST0; eauto.
+ - (* taillcall svid *)
+ inv SV; inv SV0; econstructor; eauto.
+ rewrite <- REF, <- REF0; eauto.
+ - (* tailcall args *)
+ rewrite <- LIST, <- LIST0; eauto.
+ - (* builtin args *)
+ unfold bargs_refines, bargs_simu in *.
+ rewrite <- BARGS, <- BARGS0; eauto.
+ - rewrite <- VAL, <- VAL0; eauto.
+ - (* return *)
+ inv OPT; inv OPT0; econstructor; eauto.
+ rewrite <- REF, <- REF0; eauto.
+Qed.
+
+(* refinement of (symbolic) state *)
+Inductive rstate :=
+ | Rfinal (ris: ristate) (rfv: sfval)
+ | Rcond (cond: condition) (rargs: list_sval) (rifso rifnot: rstate)
+ | Rabort
+ .
+
+Record routcome := rout {
+ _ris: ristate;
+ _rfv: sfval;
+}.
+
+Fixpoint get_routcome ctx (rst:rstate): option routcome :=
+ match rst with
+ | Rfinal ris rfv => Some (rout ris rfv)
+ | Rcond cond args ifso ifnot =>
+ SOME b <- eval_scondition ctx cond args IN
+ get_routcome ctx (if b then ifso else ifnot)
+ | Rabort => None
+ end.
+
+Inductive rst_simu: rstate -> rstate -> Prop :=
+ | Rfinal_simu ris1 ris2 rfv1 rfv2
+ (RIS: ris_simu ris1 ris2)
+ (RFV: rfv_simu rfv1 rfv2)
+ : rst_simu (Rfinal ris1 rfv1) (Rfinal ris2 rfv2)
+ | Rcond_simu cond rargs rifso1 rifnot1 rifso2 rifnot2
+ (IFSO: rst_simu rifso1 rifso2)
+ (IFNOT: rst_simu rifnot1 rifnot2)
+ : rst_simu (Rcond cond rargs rifso1 rifnot1) (Rcond cond rargs rifso2 rifnot2)
+ | Rabort_simu: rst_simu Rabort Rabort
+(* TODO: extension à voir dans un second temps !
+ | Rcond_skip cond rargs rifso1 rifnot1 rst:
+ rst_simu rifso1 rst ->
+ rst_simu rifnot1 rst ->
+ rst_simu (Rcond cond rargs rifso1 rifnot1) rst
+*)
+ .
+
+Lemma rst_simu_lroutcome rst1 rst2:
+ rst_simu rst1 rst2 ->
+ forall f1 f2 (ctx: simu_proof_context f1 f2) ris1 rfv1,
+ get_routcome (bctx1 ctx) rst1 = Some (rout ris1 rfv1) ->
+ exists ris2 rfv2, get_routcome (bctx2 ctx) rst2 = Some (rout ris2 rfv2) /\ ris_simu ris1 ris2 /\ rfv_simu rfv1 rfv2.
+Proof.
+ induction 1; simpl; intros f1 f2 ctx lris1 lrfv1 ROUT; try_simplify_someHyps.
+ erewrite <- eval_scondition_preserved.
+ autodestruct.
+ destruct b; simpl; auto.
+Qed.
+
+Inductive rst_refines ctx: rstate -> sstate -> Prop :=
+ | Reffinal ris sis rfv sfv
+ (RIS: ris_refines ctx ris sis)
+ (RFV: ris_ok ctx ris -> rfv_refines ctx rfv sfv)
+ : rst_refines ctx (Rfinal ris rfv) (Sfinal sis sfv)
+ | Refcond rcond cond rargs args rifso rifnot ifso ifnot
+ (RCOND: eval_scondition ctx rcond rargs = eval_scondition ctx cond args)
+ (REFso: eval_scondition ctx rcond rargs = Some true -> rst_refines ctx rifso ifso)
+ (REFnot: eval_scondition ctx rcond rargs = Some false -> rst_refines ctx rifnot ifnot)
+ : rst_refines ctx (Rcond rcond rargs rifso rifnot) (Scond cond args ifso ifnot)
+ | Refabort
+ : rst_refines ctx Rabort Sabort
+ .
+
+Lemma rst_refines_outcome_up ctx rst st:
+ rst_refines ctx rst st ->
+ forall ris rfv,
+ get_routcome ctx rst = Some (rout ris rfv) ->
+ exists sis sfv, get_soutcome ctx st = Some (sout sis sfv) /\ ris_refines ctx ris sis /\ (ris_ok ctx ris -> rfv_refines ctx rfv sfv).
+Proof.
+ induction 1; simpl; intros lris lrfv ROUT; try_simplify_someHyps.
+ rewrite RCOND.
+ autodestruct.
+ destruct b; simpl; auto.
+Qed.
+
+Lemma rst_refines_outcome_okpreserv ctx rst st:
+ rst_refines ctx rst st ->
+ forall sis sfv,
+ get_soutcome ctx st = Some (sout sis sfv) ->
+ exists ris rfv, get_routcome ctx rst = Some (rout ris rfv) /\ ris_refines ctx ris sis /\ (ris_ok ctx ris -> rfv_refines ctx rfv sfv).
+Proof.
+ induction 1; simpl; intros lris lrfv ROUT; try_simplify_someHyps.
+ rewrite RCOND.
+ autodestruct.
+ destruct b; simpl; auto.
+Qed.
+
+Local Hint Resolve ris_simu_correct rvf_simu_correct: core.
+
+Lemma rst_simu_correct f1 f2 (ctx: simu_proof_context f1 f2) rst1 rst2 st1 st2
+ (SIMU: rst_simu rst1 rst2)
+ (REF1: forall sis sfv, get_soutcome (bctx1 ctx) st1 = Some (sout sis sfv) -> si_ok (bctx1 ctx) sis -> rst_refines (bctx1 ctx) rst1 st1)
+ (REF2: forall ris rfv, get_routcome (bctx2 ctx) rst2 = Some (rout ris rfv) -> ris_ok (bctx2 ctx) ris -> rst_refines (bctx2 ctx) rst2 st2)
+ :sstate_simu ctx st1 st2.
+Proof.
+ intros sis1 sfv1 SOUT OK1.
+ exploit REF1; eauto.
+ clear REF1; intros REF1.
+ exploit rst_refines_outcome_okpreserv; eauto. clear REF1 SOUT.
+ intros (ris1 & rfv1 & ROUT1 & REFI1 & REFF1).
+ rewrite OK_EQUIV in OK1; eauto.
+ exploit REFF1; eauto. clear REFF1; intros REFF1.
+ exploit rst_simu_lroutcome; eauto.
+ intros (ris2 & rfv2 & ROUT2 & SIMUI & SIMUF). clear ROUT1.
+ exploit ris_simu_ok_preserv; eauto.
+ clear OK1. intros OK2.
+ exploit REF2; eauto. clear REF2; intros REF2.
+ exploit rst_refines_outcome_up; eauto.
+ intros (sis2 & sfv2 & SOUT & REFI2 & REFF2).
+ do 2 eexists; split; eauto.
+Qed.
+
+
+(** * Properties of the (abstract) operators involved in symbolic execution *)
+
+Lemma ok_set_mem ctx sm sis:
+ si_ok ctx (set_smem sm sis)
+ <-> (si_ok ctx sis /\ eval_smem ctx sm <> None).
+Proof.
+ split; destruct 1; econstructor; simpl in *; eauto.
+ intuition eauto.
+Qed.
+
+Lemma ok_set_sreg ctx r sv sis:
+ si_ok ctx (set_sreg r sv sis)
+ <-> (si_ok ctx sis /\ eval_sval ctx sv <> None).
+Proof.
+ unfold set_sreg; split.
+ + intros [(SVAL & PRE) SMEM SREG]; simpl in *; split.
+ - econstructor; eauto.
+ intros r0; generalize (SREG r0); destruct (Pos.eq_dec r r0); try congruence.
+ - generalize (SREG r); destruct (Pos.eq_dec r r); try congruence.
+ + intros ([PRE SMEM SREG] & SVAL).
+ econstructor; simpl; eauto.
+ intros r0; destruct (Pos.eq_dec r r0); try congruence.
+Qed.
+
+Lemma si_ok_op_okpreserv ctx op args dest sis: si_ok ctx (sexec_op op args dest sis) -> si_ok ctx sis.
+Proof.
+ unfold sexec_op. rewrite ok_set_sreg. intuition.
+Qed.
+
+Lemma si_ok_load_okpreserv ctx chunk addr args dest sis trap: si_ok ctx (sexec_load trap chunk addr args dest sis) -> si_ok ctx sis.
+Proof.
+ unfold sexec_load. rewrite ok_set_sreg. intuition.
+Qed.
+
+Lemma si_ok_store_okpreserv ctx chunk addr args src sis: si_ok ctx (sexec_store chunk addr args src sis) -> si_ok ctx sis.
+Proof.
+ unfold sexec_store. rewrite ok_set_mem. intuition.
+Qed.
+
+Lemma si_ok_tr_okpreserv ctx fi sis: si_ok ctx (tr_sis (cf ctx) fi sis) -> si_ok ctx sis.
+Proof.
+ unfold tr_sis. intros OK; inv OK. simpl in *. intuition.
+Qed.
+
+(* These lemmas are very bad for eauto: we put them into a dedicated basis. *)
+Local Hint Resolve si_ok_tr_okpreserv si_ok_op_okpreserv si_ok_load_okpreserv si_ok_store_okpreserv: sis_ok.
+
+Lemma sexec_rec_okpreserv ctx ib:
+ forall k
+ (CONT: forall sis lsis sfv, get_soutcome ctx (k sis) = Some (sout lsis sfv) -> si_ok ctx lsis -> si_ok ctx sis)
+ sis lsis sfv
+ (SOUT: get_soutcome ctx (sexec_rec (cf ctx) ib sis k) = Some (sout lsis sfv))
+ (OK: si_ok ctx lsis)
+ ,si_ok ctx sis.
+Proof.
+ induction ib; simpl; try (autodestruct; simpl).
+ 1-6: try_simplify_someHyps; eauto with sis_ok.
+ - intros. eapply IHib1. 2-3: eauto.
+ eapply IHib2; eauto.
+ - intros k CONT sis lsis sfv.
+ do 2 autodestruct; eauto.
+Qed.
+
+(* an alternative tr_sis *)
+
+Definition transfer_sis (inputs: list reg) (sis:sistate): sistate :=
+ {| si_pre := fun ctx => (sis.(si_pre) ctx /\ forall r, eval_sval ctx (sis.(si_sreg) r) <> None);
+ si_sreg := transfer_sreg inputs sis;
+ si_smem := sis.(si_smem) |}.
+
+Lemma ok_transfer_sis ctx inputs sis:
+ si_ok ctx (transfer_sis inputs sis)
+ <-> (si_ok ctx sis).
+Proof.
+ unfold transfer_sis. induction inputs as [|r l]; simpl.
+ + split; destruct 1; econstructor; simpl in *; intuition eauto. congruence.
+ + split.
+ * destruct 1; econstructor; simpl in *; intuition eauto.
+ * intros X; generalize X. rewrite <- IHl in X; clear IHl.
+ intros [PRE SMEM SREG].
+ econstructor; simpl; eauto.
+ intros r0; destruct (Pos.eq_dec r r0); try congruence.
+ intros H; eapply OK_SREG; eauto.
+Qed.
+
+Definition alt_tr_sis := poly_tr (fun f tbl or => transfer_sis (Regset.elements (pre_inputs f tbl or))).
+
+Lemma tr_sis_alt_def f fi sis:
+ alt_tr_sis f fi sis = tr_sis f fi sis.
+Proof.
+ unfold tr_sis, str_inputs. destruct fi; simpl; auto.
+Qed.
+
+
+(** * Refinement of the symbolic execution (e.g. refinement of [sexec] into [rexec]).
+
+TODO: Est-ce qu'on garde cette vision purement fonctionnelle de l'implementation ?
+=> ça pourrait être pratique quand on va compliquer encore le vérificateur !
+
+Du coup, on introduirait une version avec hash-consing qui serait en correspondance
+pour une relation d'equivalence sur les ristate ?
+
+Attention toutefois, il est possible que certaines parties de l'execution soient pénibles à formuler
+en programmation fonctionnelle pure (exemple: usage de l'égalité de pointeur comme test d'égalité rapide!)
+
+
+*)
+
+Local Hint Constructors rfv_refines optrsv_refines rsvident_refines rsvident_refines: core.
+
+Lemma eval_list_sval_refpreserv ctx args ris sis:
+ ris_refines ctx ris sis ->
+ ris_ok ctx ris ->
+ eval_list_sval ctx (list_sval_inj (map ris args)) =
+ eval_list_sval ctx (list_sval_inj (map (si_sreg sis) args)).
+Proof.
+ intros REF OK.
+ induction args; simpl; eauto.
+ intros; erewrite REG_EQ, IHargs; eauto.
+Qed.
+
+Local Hint Resolve eval_list_sval_refpreserv: core.
+
+Lemma eval_builtin_sval_refpreserv ctx arg ris sis:
+ ris_refines ctx ris sis ->
+ ris_ok ctx ris ->
+ eval_builtin_sval ctx (builtin_arg_map ris arg) = eval_builtin_sval ctx (builtin_arg_map sis arg).
+Proof.
+ intros REF OK; induction arg; simpl; eauto.
+ + erewrite REG_EQ; eauto.
+ + erewrite IHarg1, IHarg2; eauto.
+ + erewrite IHarg1, IHarg2; eauto.
+Qed.
+
+Lemma bargs_refpreserv ctx args ris sis:
+ ris_refines ctx ris sis ->
+ ris_ok ctx ris ->
+ bargs_refines ctx (map (builtin_arg_map ris) args) (map (builtin_arg_map sis) args).
+Proof.
+ unfold bargs_refines. intros REF OK.
+ induction args; simpl; eauto.
+ erewrite eval_builtin_sval_refpreserv, IHargs; eauto.
+Qed.
+
+Local Hint Resolve bargs_refpreserv: core.
+
+Lemma exec_final_refpreserv ctx i ris sis:
+ ris_refines ctx ris sis ->
+ ris_ok ctx ris ->
+ rfv_refines ctx (sexec_final_sfv i ris) (sexec_final_sfv i sis).
+Proof.
+ destruct i; simpl; unfold sum_left_map; try autodestruct; eauto.
+Qed.
+
+Definition ris_init: ristate := {| ris_smem:= fSinit; ris_input_init:=true; ok_rsval := nil; ris_sreg := PTree.empty _ |}.
+
+Lemma ris_init_correct ctx:
+ ris_refines ctx ris_init sis_init.
+Proof.
+ unfold ris_init, sis_init; econstructor; simpl in *; eauto.
+ + split; destruct 1; econstructor; simpl in *; eauto.
+ congruence.
+ + destruct 1; simpl in *. unfold ris_sreg_get; simpl.
+ intros; rewrite PTree.gempty; eauto.
+Qed.
+
+Definition rset_smem rm (ris:ristate): ristate :=
+ {| ris_smem := rm;
+ ris_input_init := ris.(ris_input_init);
+ ok_rsval := ris.(ok_rsval);
+ ris_sreg:= ris.(ris_sreg)
+ |}.
+
+Lemma ok_rset_mem ctx rm (ris: ristate):
+ (eval_smem ctx ris.(ris_smem) = None -> eval_smem ctx rm = None) ->
+ ris_ok ctx (rset_smem rm ris)
+ <-> (ris_ok ctx ris /\ eval_smem ctx rm <> None).
+Proof.
+ split; destruct 1; econstructor; simpl in *; eauto.
+Qed.
+
+Lemma rset_mem_correct ctx rm sm ris sis:
+ (eval_smem ctx ris.(ris_smem) = None -> eval_smem ctx rm = None) ->
+ ris_refines ctx ris sis ->
+ (ris_ok ctx ris -> eval_smem ctx rm = eval_smem ctx sm) ->
+ ris_refines ctx (rset_smem rm ris) (set_smem sm sis).
+Proof.
+ destruct 2; intros.
+ econstructor; eauto.
+ + rewrite ok_set_mem, ok_rset_mem; intuition congruence.
+ + rewrite ok_rset_mem; intuition eauto.
+ + rewrite ok_rset_mem; intuition eauto.
+Qed.
+
+Definition rexec_store chunk addr args src ris: ristate :=
+ let args := list_sval_inj (List.map (ris_sreg_get ris) args) in
+ let src := ris_sreg_get ris src in
+ let rm := fSstore ris.(ris_smem) chunk addr args src in
+ rset_smem rm ris.
+
+Lemma rexec_store_correct ctx chunk addr args src ris sis:
+ ris_refines ctx ris sis ->
+ ris_refines ctx (rexec_store chunk addr args src ris) (sexec_store chunk addr args src sis).
+Proof.
+ intros REF; eapply rset_mem_correct; simpl; eauto.
+ + intros X; rewrite X. repeat autodestruct; eauto.
+ + intros OK; erewrite eval_list_sval_refpreserv, MEM_EQ, REG_EQ; eauto.
+Qed.
+
+(* TODO: reintroduire le "root_apply" ? *)
+
+Definition rset_sreg r rsv (ris:ristate): ristate :=
+ {| ris_smem := ris.(ris_smem);
+ ris_input_init := ris.(ris_input_init);
+ ok_rsval := rsv::ris.(ok_rsval); (* TODO: A CHANGER ? *)
+ ris_sreg:= PTree.set r rsv ris.(ris_sreg) (* TODO: A CHANGER *)
+ |}.
+
+Lemma ok_rset_sreg ctx r rsv ris:
+ ris_ok ctx (rset_sreg r rsv ris)
+ <-> (ris_ok ctx ris /\ eval_sval ctx rsv <> None).
+Proof.
+ split; destruct 1; econstructor; simpl in *; eauto.
+ intuition subst; eauto.
+ exploit OK_RREG; eauto.
+Qed.
+
+Lemma rset_reg_correct ctx r rsv sv ris sis:
+ ris_refines ctx ris sis ->
+ (ris_ok ctx ris -> eval_sval ctx rsv = eval_sval ctx sv) ->
+ ris_refines ctx (rset_sreg r rsv ris) (set_sreg r sv sis).
+Proof.
+ destruct 1; intros.
+ econstructor; eauto.
+ + rewrite ok_set_sreg, ok_rset_sreg; intuition congruence.
+ + rewrite ok_rset_sreg; intuition eauto.
+ + rewrite ok_rset_sreg. intros; unfold rset_sreg, set_sreg, ris_sreg_get; simpl. intuition eauto.
+ destruct (Pos.eq_dec _ _).
+ * subst; rewrite PTree.gss; eauto.
+ * rewrite PTree.gso; eauto.
+Qed.
+
+Definition rexec_op op args dst (ris:ristate): ristate :=
+ let args := list_sval_inj (List.map ris args) in
+ rset_sreg dst (fSop op args) ris.
+
+Lemma rexec_op_correct ctx op args dst ris sis:
+ ris_refines ctx ris sis ->
+ ris_refines ctx (rexec_op op args dst ris) (sexec_op op args dst sis).
+Proof.
+ intros REF; eapply rset_reg_correct; simpl; eauto.
+ intros OK; erewrite eval_list_sval_refpreserv; eauto.
+Qed.
+
+Definition rexec_load trap chunk addr args dst (ris:ristate): ristate :=
+ let args := list_sval_inj (List.map ris args) in
+ rset_sreg dst (fSload ris.(ris_smem) trap chunk addr args) ris.
+
+Lemma rexec_load_correct ctx trap chunk addr args dst ris sis:
+ ris_refines ctx ris sis ->
+ ris_refines ctx (rexec_load trap chunk addr args dst ris) (sexec_load trap chunk addr args dst sis).
+Proof.
+ intros REF; eapply rset_reg_correct; simpl; eauto.
+ intros OK; erewrite eval_list_sval_refpreserv, MEM_EQ; eauto.
+Qed.
+
+Lemma eval_scondition_refpreserv ctx cond args ris sis:
+ ris_refines ctx ris sis ->
+ ris_ok ctx ris ->
+ eval_scondition ctx cond (list_sval_inj (map ris args)) = eval_scondition ctx cond (list_sval_inj (map sis args)).
+Proof.
+ intros; unfold eval_scondition.
+ erewrite eval_list_sval_refpreserv; eauto.
+Qed.
+
+
+(* transfer *)
+
+Definition rseto_sreg r rsv (ris:ristate): ristate :=
+ {| ris_smem := ris.(ris_smem);
+ ris_input_init := ris.(ris_input_init);
+ ok_rsval := ris.(ok_rsval);
+ ris_sreg:= PTree.set r rsv ris.(ris_sreg) (* TODO: A CHANGER *)
+ |}.
+
+Lemma ok_rseto_sreg ctx r rsv ris:
+ ris_ok ctx (rseto_sreg r rsv ris)
+ <-> (ris_ok ctx ris).
+Proof.
+ split; destruct 1; econstructor; simpl in *; eauto.
+Qed.
+
+Lemma rseto_reg_correct ctx r rsv sv ris sis:
+ ris_refines ctx ris sis ->
+ (ris_ok ctx ris -> eval_sval ctx rsv <> None) ->
+ (ris_ok ctx ris -> eval_sval ctx rsv = eval_sval ctx sv) ->
+ ris_refines ctx (rseto_sreg r rsv ris) (set_sreg r sv sis).
+Proof.
+ destruct 1; intros.
+ econstructor; eauto.
+ + rewrite ok_set_sreg, ok_rseto_sreg; intuition congruence.
+ + rewrite ok_rseto_sreg; intuition eauto.
+ + rewrite ok_rseto_sreg. intros; unfold rseto_sreg, set_sreg, ris_sreg_get; simpl. intuition eauto.
+ destruct (Pos.eq_dec _ _).
+ * subst; rewrite PTree.gss; eauto.
+ * rewrite PTree.gso; eauto.
+Qed.
+
+Fixpoint transfer_ris (inputs: list reg) (ris:ristate): ristate :=
+ match inputs with
+ | nil => {| ris_smem := ris.(ris_smem);
+ ris_input_init := false;
+ ok_rsval := ris.(ok_rsval);
+ ris_sreg:= PTree.empty _
+ |}
+ | r::l => rseto_sreg r (ris_sreg_get ris r) (transfer_ris l ris)
+ end.
+
+Lemma ok_transfer_ris ctx inputs ris:
+ ris_ok ctx (transfer_ris inputs ris)
+ <-> (ris_ok ctx ris).
+Proof.
+ induction inputs as [|r l]; simpl.
+ + split; destruct 1; econstructor; simpl in *; intuition eauto.
+ + rewrite ok_rseto_sreg. auto.
+Qed.
+
+Lemma transfer_ris_correct ctx inputs ris sis:
+ ris_refines ctx ris sis ->
+ ris_refines ctx (transfer_ris inputs ris) (transfer_sis inputs sis).
+Proof.
+ destruct 1; intros.
+ induction inputs as [|r l].
+ + econstructor; eauto.
+ * erewrite ok_transfer_sis, ok_transfer_ris; eauto.
+ * erewrite ok_transfer_ris; eauto.
+ * erewrite ok_transfer_ris; simpl; unfold ris_sreg_get; simpl; eauto.
+ intros; rewrite PTree.gempty. simpl; auto.
+ + econstructor; eauto.
+ * erewrite ok_transfer_sis, ok_transfer_ris; eauto.
+ * erewrite ok_transfer_ris; simpl.
+ intros; erewrite MEM_EQ. 2: eauto.
+ - unfold transfer_sis; simpl; eauto.
+ - rewrite ok_transfer_ris; simpl; eauto.
+ * erewrite ok_transfer_ris; simpl.
+ intros H r0.
+ erewrite REG_EQ. 2: eapply rseto_reg_correct; eauto.
+ - unfold set_sreg; simpl; auto.
+ destruct (Pos.eq_dec _ _); simpl; auto.
+ - intros. rewrite REG_EQ0; auto. apply OK_SREG; tauto.
+ - rewrite ok_rseto_sreg, ok_transfer_ris. auto.
+Qed.
+
+Definition tr_ris := poly_tr (fun f tbl or => transfer_ris (Regset.elements (pre_inputs f tbl or))).
+
+Local Hint Resolve transfer_ris_correct ok_transfer_ris: core.
+Local Opaque transfer_ris.
+
+Lemma ok_tr_ris ctx fi ris:
+ ris_ok ctx (tr_ris (cf ctx) fi ris)
+ <-> (ris_ok ctx ris).
+Proof.
+ destruct fi; simpl; eauto.
+Qed.
+
+Lemma ris_ok_tr_okpreserv ctx fi ris: ris_ok ctx (tr_ris (cf ctx) fi ris) -> ris_ok ctx ris.
+Proof.
+ rewrite ok_tr_ris; auto.
+Qed.
+
+Lemma tr_ris_correct ctx fi ris sis:
+ ris_refines ctx ris sis ->
+ ris_refines ctx (tr_ris (cf ctx) fi ris) (tr_sis (cf ctx) fi sis).
+Proof.
+ intros REF. rewrite <- tr_sis_alt_def.
+ destruct fi; simpl; eauto.
+Qed.
+
+(** RAFFINEMENT EXEC SYMBOLIQUE **)
+
+Fixpoint rexec_rec f ib ris (k: ristate -> rstate): rstate :=
+ match ib with
+ | BF fin _ => Rfinal (tr_ris f fin ris) (sexec_final_sfv fin ris)
+ (* basic instructions *)
+ | Bnop _ => k ris
+ | Bop op args res _ => k (rexec_op op args res ris)
+ | Bload trap chunk addr args dst _ => k (rexec_load trap chunk addr args dst ris)
+ | Bstore chunk addr args src _ => k (rexec_store chunk addr args src ris)
+ (* composed instructions *)
+ | Bseq ib1 ib2 =>
+ rexec_rec f ib1 ris (fun ris2 => rexec_rec f ib2 ris2 k)
+ | Bcond cond args ifso ifnot _ =>
+ let args := list_sval_inj (List.map ris args) in
+ let ifso := rexec_rec f ifso ris k in
+ let ifnot := rexec_rec f ifnot ris k in
+ Rcond cond args ifso ifnot
+ end
+ .
+
+Definition rexec f ib := rexec_rec f ib ris_init (fun _ => Rabort).
+
+
+Local Hint Resolve ris_init_correct exec_final_refpreserv tr_ris_correct ris_ok_tr_okpreserv
+ rexec_op_correct rexec_load_correct rexec_store_correct: core.
+
+Local Hint Constructors rst_refines: core.
+
+Lemma rexec_rec_correct1 ctx ib:
+ forall rk k
+ (CONTh: forall sis lsis sfv, get_soutcome ctx (k sis) = Some (sout lsis sfv) -> si_ok ctx lsis -> si_ok ctx sis)
+ (CONT: forall ris sis lsis sfv st, ris_refines ctx ris sis -> k sis = st -> get_soutcome ctx (k sis) = Some (sout lsis sfv) -> si_ok ctx lsis -> rst_refines ctx (rk ris) (k sis))
+ ris sis lsis sfv st
+ (REF: ris_refines ctx ris sis)
+ (EXEC: sexec_rec (cf ctx) ib sis k = st)
+ (SOUT: get_soutcome ctx st = Some (sout lsis sfv))
+ (OK: si_ok ctx lsis)
+ , rst_refines ctx (rexec_rec (cf ctx) ib ris rk) st.
+Proof.
+ induction ib; simpl; try (intros; subst; eauto; fail).
+ - (* seq *)
+ intros; subst.
+ eapply IHib1. 3-6: eauto.
+ + simpl. eapply sexec_rec_okpreserv; eauto.
+ + intros; subst. eapply IHib2; eauto.
+ - (* cond *)
+ intros rk k CONTh CONT ris sis lsis sfv st REF EXEC OUT OK. subst.
+ assert (rOK: ris_ok ctx ris). {
+ erewrite <- OK_EQUIV. 2: eauto.
+ eapply sexec_rec_okpreserv with (ib:=(Bcond cond args ib1 ib2 iinfo)); simpl; eauto.
+ }
+ generalize OUT; clear OUT; simpl.
+ autodestruct.
+ intros COND; generalize COND.
+ erewrite <- eval_scondition_refpreserv; eauto.
+ econstructor; try_simplify_someHyps.
+Qed.
+
+Lemma rexec_correct1 ctx ib sis sfv:
+ get_soutcome ctx (sexec (cf ctx) ib) = Some (sout sis sfv) ->
+ (si_ok ctx sis) ->
+ rst_refines ctx (rexec (cf ctx) ib) (sexec (cf ctx) ib).
+Proof.
+ unfold sexec; intros; eapply rexec_rec_correct1; eauto; simpl; congruence.
+Qed.
+
+
+(** COPIER-COLLER ... Y a-t-il moyen de l'eviter ? **)
+
+Lemma ris_ok_op_okpreserv ctx op args dest ris: ris_ok ctx (rexec_op op args dest ris) -> ris_ok ctx ris.
+Proof.
+ unfold rexec_op. rewrite ok_rset_sreg. intuition.
+Qed.
+
+Lemma ris_ok_load_okpreserv ctx chunk addr args dest ris trap: ris_ok ctx (rexec_load trap chunk addr args dest ris) -> ris_ok ctx ris.
+Proof.
+ unfold rexec_load. rewrite ok_rset_sreg. intuition.
+Qed.
+
+Lemma ris_ok_store_okpreserv ctx chunk addr args src ris: ris_ok ctx (rexec_store chunk addr args src ris) -> ris_ok ctx ris.
+Proof.
+ unfold rexec_store. rewrite ok_rset_mem; simpl.
+ + intuition.
+ + intros X; rewrite X; simpl.
+ repeat autodestruct.
+Qed.
+
+(* These lemmas are very bad for eauto: we put them into a dedicated basis. *)
+Local Hint Resolve ris_ok_store_okpreserv ris_ok_op_okpreserv ris_ok_load_okpreserv: ris_ok.
+
+Lemma rexec_rec_okpreserv ctx ib:
+ forall k
+ (CONT: forall ris lris rfv, get_routcome ctx (k ris) = Some (rout lris rfv) -> ris_ok ctx lris -> ris_ok ctx ris)
+ ris lris rfv
+ (ROUT: get_routcome ctx (rexec_rec (cf ctx) ib ris k) = Some (rout lris rfv))
+ (OK: ris_ok ctx lris)
+ ,ris_ok ctx ris.
+Proof.
+ induction ib; simpl; try (autodestruct; simpl).
+ 1-6: try_simplify_someHyps; eauto with ris_ok.
+ - intros. eapply IHib1. 2-3: eauto.
+ eapply IHib2; eauto.
+ - intros k CONT sis lsis sfv.
+ do 2 autodestruct; eauto.
+Qed.
+
+Lemma rexec_rec_correct2 ctx ib:
+ forall rk k
+ (CONTh: forall ris lris rfv, get_routcome ctx (rk ris) = Some (rout lris rfv) -> ris_ok ctx lris -> ris_ok ctx ris)
+ (CONT: forall ris sis lris rfv st, ris_refines ctx ris sis -> rk ris = st -> get_routcome ctx (rk ris) = Some (rout lris rfv) -> ris_ok ctx lris -> rst_refines ctx (rk ris) (k sis))
+ ris sis lris rfv st
+ (REF: ris_refines ctx ris sis)
+ (EXEC: rexec_rec (cf ctx) ib ris rk = st)
+ (SOUT: get_routcome ctx st = Some (rout lris rfv))
+ (OK: ris_ok ctx lris)
+ , rst_refines ctx st (sexec_rec (cf ctx) ib sis k).
+Proof.
+ induction ib; simpl; try (intros; subst; eauto; fail).
+ - (* seq *)
+ intros; subst.
+ eapply IHib1. 3-6: eauto.
+ + simpl. eapply rexec_rec_okpreserv; eauto.
+ + intros; subst. eapply IHib2; eauto.
+ - (* cond *)
+ intros rk k CONTh CONT ris sis lsis sfv st REF EXEC OUT OK. subst.
+ assert (OK0: ris_ok ctx ris). {
+ eapply rexec_rec_okpreserv with (ib:=(Bcond cond args ib1 ib2 iinfo)); simpl; eauto.
+ }
+ generalize OUT; clear OUT; simpl.
+ autodestruct.
+ intros COND; generalize COND.
+ erewrite eval_scondition_refpreserv; eauto.
+ econstructor; try_simplify_someHyps.
+Qed.
+
+Lemma rexec_correct2 ctx ib ris rfv:
+ get_routcome ctx (rexec (cf ctx) ib) = Some (rout ris rfv) ->
+ (ris_ok ctx ris) ->
+ rst_refines ctx (rexec (cf ctx) ib) (sexec (cf ctx) ib).
+Proof.
+ unfold rexec; intros; eapply rexec_rec_correct2; eauto; simpl; congruence.
+Qed.
+
+Theorem rexec_simu_correct f1 f2 ib1 ib2:
+ rst_simu (rexec f1 ib1) (rexec f2 ib2) ->
+ symbolic_simu f1 f2 ib1 ib2.
+Proof.
+ intros SIMU ctx.
+ eapply rst_simu_correct; eauto.
+ + intros; eapply rexec_correct1; eauto.
+ + intros; eapply rexec_correct2; eauto.
+Qed.
diff --git a/scheduling/BTL_SEtheory.v b/scheduling/BTL_SEtheory.v
new file mode 100644
index 00000000..033ed843
--- /dev/null
+++ b/scheduling/BTL_SEtheory.v
@@ -0,0 +1,1336 @@
+(** A theory of symbolic simulation (i.e. simulation of symbolic executions) on BTL blocks.
+
+NB: an efficient implementation with hash-consing will be defined in another file (some day)
+
+The main theorem of this file is [symbolic_simu_correct] stating
+that the abstract definition of symbolic simulation of two BTL blocks
+implies the simulation for BTL.fsem block-steps.
+
+
+*)
+
+Require Import Coqlib Maps Floats.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import Op Registers.
+Require Import RTL BTL OptionMonad.
+Require Export Impure.ImpHCons.
+Import HConsing.
+
+(** * Syntax and semantics of symbolic values *)
+
+(** The semantics of symbolic execution is parametrized by the context of the execution of a block *)
+Record iblock_exec_context := Bctx {
+ cge: BTL.genv; (** usual environment for identifiers *)
+ cf: function; (** ambient function of the block *)
+ csp: val; (** stack pointer *)
+ crs0: regset; (** initial state of registers (at the block entry) *)
+ cm0: mem (** initial memory state *)
+}.
+
+
+(** symbolic value *)
+Inductive sval :=
+ | Sundef (hid: hashcode)
+ | Sinput (r: reg) (hid: hashcode)
+ | Sop (op:operation) (lsv: list_sval) (hid: hashcode)
+ | Sload (sm: smem) (trap: trapping_mode) (chunk:memory_chunk) (addr:addressing) (lsv:list_sval) (hid: hashcode)
+(** list of symbolic values *)
+with list_sval :=
+ | Snil (hid: hashcode)
+ | Scons (sv: sval) (lsv: list_sval) (hid: hashcode)
+(** symbolic memory *)
+with smem :=
+ | Sinit (hid: hashcode)
+ | Sstore (sm: smem) (chunk:memory_chunk) (addr:addressing) (lsv:list_sval) (srce: sval) (hid: hashcode)
+.
+
+Scheme sval_mut := Induction for sval Sort Prop
+with list_sval_mut := Induction for list_sval Sort Prop
+with smem_mut := Induction for smem Sort Prop.
+
+(** "fake" smart-constructors using an [unknown_hid] instead of the one provided by hash-consing.
+These smart-constructors are those used in the abstract model of symbolic execution.
+They will also appear in the implementation of rewriting rules (in order to avoid hash-consing handling
+in proofs of rewriting rules).
+*)
+
+Definition fSundef := Sundef unknown_hid.
+Definition fSinput (r: reg) := Sinput r unknown_hid.
+Definition fSop (op:operation) (lsv: list_sval) := Sop op lsv unknown_hid.
+Definition fSload (sm: smem) (trap: trapping_mode) (chunk:memory_chunk) (addr:addressing) (lsv:list_sval)
+ := Sload sm trap chunk addr lsv unknown_hid.
+
+Definition fSnil := Snil unknown_hid.
+Definition fScons (sv: sval) (lsv: list_sval) := Scons sv lsv unknown_hid.
+
+Definition fSinit := Sinit unknown_hid.
+Definition fSstore (sm: smem) (chunk:memory_chunk) (addr:addressing) (lsv:list_sval) (srce: sval)
+ := Sstore sm chunk addr lsv srce unknown_hid.
+
+Fixpoint list_sval_inj (l: list sval): list_sval :=
+ match l with
+ | nil => fSnil
+ | v::l => fScons v (list_sval_inj l)
+ end.
+
+Local Open Scope option_monad_scope.
+
+(** Semantics *)
+Fixpoint eval_sval ctx (sv: sval): option val :=
+ match sv with
+ | Sundef _ => Some Vundef
+ | Sinput r _ => Some ((crs0 ctx)#r)
+ | Sop op l _ =>
+ SOME args <- eval_list_sval ctx l IN
+ eval_operation (cge ctx) (csp ctx) op args (cm0 ctx)
+ | Sload sm trap chunk addr lsv _ =>
+ match trap with
+ | TRAP =>
+ SOME args <- eval_list_sval ctx lsv IN
+ SOME a <- eval_addressing (cge ctx) (csp ctx) addr args IN
+ SOME m <- eval_smem ctx sm IN
+ Mem.loadv chunk m a
+ | NOTRAP =>
+ SOME args <- eval_list_sval ctx lsv IN
+ match (eval_addressing (cge ctx) (csp ctx) addr args) with
+ | None => Some Vundef
+ | Some a =>
+ SOME m <- eval_smem ctx sm IN
+ match (Mem.loadv chunk m a) with
+ | None => Some Vundef
+ | Some val => Some val
+ end
+ end
+ end
+ end
+with eval_list_sval ctx (lsv: list_sval): option (list val) :=
+ match lsv with
+ | Snil _ => Some nil
+ | Scons sv lsv' _ =>
+ SOME v <- eval_sval ctx sv IN
+ SOME lv <- eval_list_sval ctx lsv' IN
+ Some (v::lv)
+ end
+with eval_smem ctx (sm: smem): option mem :=
+ match sm with
+ | Sinit _ => Some (cm0 ctx)
+ | Sstore sm chunk addr lsv srce _ =>
+ SOME args <- eval_list_sval ctx lsv IN
+ SOME a <- eval_addressing (cge ctx) (csp ctx) addr args IN
+ SOME m <- eval_smem ctx sm IN
+ SOME sv <- eval_sval ctx srce IN
+ Mem.storev chunk m a sv
+ end.
+
+(** The symbolic memory preserves predicate Mem.valid_pointer with respect to initial memory.
+ Hence, arithmetic operations and Boolean conditions do not depend on the current memory of the block
+ (their semantics only depends on the initial memory of the block).
+
+ The correctness of this idea is proved on lemmas [sexec_op_correct] and [eval_scondition_eq].
+*)
+Lemma valid_pointer_preserv ctx sm:
+ forall m b ofs, eval_smem ctx sm = Some m -> Mem.valid_pointer (cm0 ctx) b ofs = Mem.valid_pointer m b ofs.
+Proof.
+ induction sm; simpl; intros; try_simplify_someHyps; auto.
+ repeat autodestruct; intros; erewrite IHsm by reflexivity.
+ eapply Mem.storev_preserv_valid; eauto.
+Qed.
+Local Hint Resolve valid_pointer_preserv: core.
+
+Lemma eval_list_sval_inj ctx l (sreg: reg -> sval) rs:
+ (forall r : reg, eval_sval ctx (sreg r) = Some (rs # r)) ->
+ eval_list_sval ctx (list_sval_inj (map sreg l)) = Some (rs ## l).
+Proof.
+ intros H; induction l as [|r l]; simpl; repeat autodestruct; auto.
+Qed.
+
+Definition eval_scondition ctx (cond: condition) (lsv: list_sval): option bool :=
+ SOME args <- eval_list_sval ctx lsv IN
+ eval_condition cond args (cm0 ctx).
+
+
+(** * Auxiliary definitions on Builtins *)
+(* TODO: clean this. Some generic stuffs could be put in [AST.v] *)
+
+Section EVAL_BUILTIN_SARG. (* adapted from Events.v *)
+
+Variable ctx: iblock_exec_context.
+Variable m: mem.
+
+Inductive eval_builtin_sarg: builtin_arg sval -> val -> Prop :=
+ | seval_BA: forall x v,
+ eval_sval ctx x = Some v ->
+ eval_builtin_sarg (BA x) v
+ | seval_BA_int: forall n,
+ eval_builtin_sarg (BA_int n) (Vint n)
+ | seval_BA_long: forall n,
+ eval_builtin_sarg (BA_long n) (Vlong n)
+ | seval_BA_float: forall n,
+ eval_builtin_sarg (BA_float n) (Vfloat n)
+ | seval_BA_single: forall n,
+ eval_builtin_sarg (BA_single n) (Vsingle n)
+ | seval_BA_loadstack: forall chunk ofs v,
+ Mem.loadv chunk m (Val.offset_ptr (csp ctx) ofs) = Some v ->
+ eval_builtin_sarg (BA_loadstack chunk ofs) v
+ | seval_BA_addrstack: forall ofs,
+ eval_builtin_sarg (BA_addrstack ofs) (Val.offset_ptr (csp ctx) ofs)
+ | seval_BA_loadglobal: forall chunk id ofs v,
+ Mem.loadv chunk m (Senv.symbol_address (cge ctx) id ofs) = Some v ->
+ eval_builtin_sarg (BA_loadglobal chunk id ofs) v
+ | seval_BA_addrglobal: forall id ofs,
+ eval_builtin_sarg (BA_addrglobal id ofs) (Senv.symbol_address (cge ctx) id ofs)
+ | seval_BA_splitlong: forall hi lo vhi vlo,
+ eval_builtin_sarg hi vhi -> eval_builtin_sarg lo vlo ->
+ eval_builtin_sarg (BA_splitlong hi lo) (Val.longofwords vhi vlo)
+ | seval_BA_addptr: forall a1 a2 v1 v2,
+ eval_builtin_sarg a1 v1 -> eval_builtin_sarg a2 v2 ->
+ eval_builtin_sarg (BA_addptr a1 a2)
+ (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)
+.
+
+Definition eval_builtin_sargs (al: list (builtin_arg sval)) (vl: list val) : Prop :=
+ list_forall2 eval_builtin_sarg al vl.
+
+Lemma eval_builtin_sarg_determ:
+ forall a v, eval_builtin_sarg a v -> forall v', eval_builtin_sarg a v' -> v' = v.
+Proof.
+ induction 1; intros v' EV; inv EV; try congruence.
+ f_equal; eauto.
+ apply IHeval_builtin_sarg1 in H3. apply IHeval_builtin_sarg2 in H5. subst; auto.
+Qed.
+
+Lemma eval_builtin_sargs_determ:
+ forall al vl, eval_builtin_sargs al vl -> forall vl', eval_builtin_sargs al vl' -> vl' = vl.
+Proof.
+ induction 1; intros v' EV; inv EV; f_equal; eauto using eval_builtin_sarg_determ.
+Qed.
+
+End EVAL_BUILTIN_SARG.
+
+(* NB: generic function that could be put into [AST] file *)
+Fixpoint builtin_arg_map {A B} (f: A -> B) (arg: builtin_arg A) : builtin_arg B :=
+ match arg with
+ | BA x => BA (f x)
+ | BA_int n => BA_int n
+ | BA_long n => BA_long n
+ | BA_float f => BA_float f
+ | BA_single s => BA_single s
+ | BA_loadstack chunk ptr => BA_loadstack chunk ptr
+ | BA_addrstack ptr => BA_addrstack ptr
+ | BA_loadglobal chunk id ptr => BA_loadglobal chunk id ptr
+ | BA_addrglobal id ptr => BA_addrglobal id ptr
+ | BA_splitlong ba1 ba2 => BA_splitlong (builtin_arg_map f ba1) (builtin_arg_map f ba2)
+ | BA_addptr ba1 ba2 => BA_addptr (builtin_arg_map f ba1) (builtin_arg_map f ba2)
+ end.
+
+Lemma eval_builtin_sarg_correct ctx rs m sreg: forall arg varg,
+ (forall r, eval_sval ctx (sreg r) = Some rs # r) ->
+ eval_builtin_arg (cge ctx) (fun r => rs # r) (csp ctx) m arg varg ->
+ eval_builtin_sarg ctx m (builtin_arg_map sreg arg) varg.
+Proof.
+ induction arg.
+ all: try (intros varg SEVAL BARG; inv BARG; constructor; congruence).
+ - intros varg SEVAL BARG. inv BARG. simpl. constructor.
+ eapply IHarg1; eauto. eapply IHarg2; eauto.
+ - intros varg SEVAL BARG. inv BARG. simpl. constructor.
+ eapply IHarg1; eauto. eapply IHarg2; eauto.
+Qed.
+
+Lemma eval_builtin_sargs_correct ctx rs m sreg args vargs:
+ (forall r, eval_sval ctx (sreg r) = Some rs # r) ->
+ eval_builtin_args (cge ctx) (fun r => rs # r) (csp ctx) m args vargs ->
+ eval_builtin_sargs ctx m (map (builtin_arg_map sreg) args) vargs.
+Proof.
+ induction 2.
+ - constructor.
+ - simpl. constructor; [| assumption].
+ eapply eval_builtin_sarg_correct; eauto.
+Qed.
+
+Lemma eval_builtin_sarg_exact ctx rs m sreg: forall arg varg,
+ (forall r, eval_sval ctx (sreg r) = Some rs # r) ->
+ eval_builtin_sarg ctx m (builtin_arg_map sreg arg) varg ->
+ eval_builtin_arg (cge ctx) (fun r => rs # r) (csp ctx) m arg varg.
+Proof.
+ induction arg.
+ all: intros varg SEVAL BARG; try (inv BARG; constructor; congruence).
+ - inv BARG. rewrite SEVAL in H0. inv H0. constructor.
+ - inv BARG. simpl. constructor.
+ eapply IHarg1; eauto. eapply IHarg2; eauto.
+ - inv BARG. simpl. constructor.
+ eapply IHarg1; eauto. eapply IHarg2; eauto.
+Qed.
+
+Lemma eval_builtin_sargs_exact ctx rs m sreg: forall args vargs,
+ (forall r, eval_sval ctx (sreg r) = Some rs # r) ->
+ eval_builtin_sargs ctx m (map (builtin_arg_map sreg) args) vargs ->
+ eval_builtin_args (cge ctx) (fun r => rs # r) (csp ctx) m args vargs.
+Proof.
+ induction args.
+ - simpl. intros. inv H0. constructor.
+ - intros vargs SEVAL BARG. simpl in BARG. inv BARG.
+ constructor; [| eapply IHargs; eauto].
+ eapply eval_builtin_sarg_exact; eauto.
+Qed.
+
+Fixpoint eval_builtin_sval ctx bsv :=
+ match bsv with
+ | BA sv => SOME v <- eval_sval ctx sv IN Some (BA v)
+ | BA_splitlong sv1 sv2 =>
+ SOME v1 <- eval_builtin_sval ctx sv1 IN
+ SOME v2 <- eval_builtin_sval ctx sv2 IN
+ Some (BA_splitlong v1 v2)
+ | BA_addptr sv1 sv2 =>
+ SOME v1 <- eval_builtin_sval ctx sv1 IN
+ SOME v2 <- eval_builtin_sval ctx sv2 IN
+ Some (BA_addptr v1 v2)
+ | BA_int i => Some (BA_int i)
+ | BA_long l => Some (BA_long l)
+ | BA_float f => Some (BA_float f)
+ | BA_single s => Some (BA_single s)
+ | BA_loadstack chk ptr => Some (BA_loadstack chk ptr)
+ | BA_addrstack ptr => Some (BA_addrstack ptr)
+ | BA_loadglobal chk id ptr => Some (BA_loadglobal chk id ptr)
+ | BA_addrglobal id ptr => Some (BA_addrglobal id ptr)
+ end.
+
+Fixpoint eval_list_builtin_sval ctx lbsv :=
+ match lbsv with
+ | nil => Some nil
+ | bsv::lbsv => SOME v <- eval_builtin_sval ctx bsv IN
+ SOME lv <- eval_list_builtin_sval ctx lbsv IN
+ Some (v::lv)
+ end.
+
+Lemma eval_list_builtin_sval_nil ctx lbs2:
+ eval_list_builtin_sval ctx lbs2 = Some nil ->
+ lbs2 = nil.
+Proof.
+ destruct lbs2; simpl; repeat autodestruct; congruence.
+Qed.
+
+Lemma eval_builtin_sval_arg ctx bs:
+ forall ba m v,
+ eval_builtin_sval ctx bs = Some ba ->
+ eval_builtin_arg (cge ctx) (fun id => id) (csp ctx) m ba v ->
+ eval_builtin_sarg ctx m bs v.
+Proof.
+ induction bs; simpl;
+ try (intros ba m v H; inversion H; subst; clear H;
+ intros H; inversion H; subst;
+ econstructor; auto; fail).
+ - intros ba m v; destruct (eval_sval _ _) eqn: SV;
+ intros H; inversion H; subst; clear H.
+ intros H; inversion H; subst.
+ econstructor; auto.
+ - intros ba m v.
+ destruct (eval_builtin_sval _ bs1) eqn: SV1; try congruence.
+ destruct (eval_builtin_sval _ bs2) eqn: SV2; try congruence.
+ intros H; inversion H; subst; clear H.
+ intros H; inversion H; subst.
+ econstructor; eauto.
+ - intros ba m v.
+ destruct (eval_builtin_sval _ bs1) eqn: SV1; try congruence.
+ destruct (eval_builtin_sval _ bs2) eqn: SV2; try congruence.
+ intros H; inversion H; subst; clear H.
+ intros H; inversion H; subst.
+ econstructor; eauto.
+Qed.
+
+Lemma eval_builtin_sarg_sval ctx m v: forall bs,
+ eval_builtin_sarg ctx m bs v ->
+ exists ba,
+ eval_builtin_sval ctx bs = Some ba
+ /\ eval_builtin_arg (cge ctx) (fun id => id) (csp ctx) m ba v.
+Proof.
+ induction 1.
+ all: try (eexists; constructor; [simpl; reflexivity | constructor]).
+ 2-3: try assumption.
+ - eexists. constructor.
+ + simpl. rewrite H. reflexivity.
+ + constructor.
+ - destruct IHeval_builtin_sarg1 as (ba1 & A1 & B1).
+ destruct IHeval_builtin_sarg2 as (ba2 & A2 & B2).
+ eexists. constructor.
+ + simpl. rewrite A1. rewrite A2. reflexivity.
+ + constructor; assumption.
+ - destruct IHeval_builtin_sarg1 as (ba1 & A1 & B1).
+ destruct IHeval_builtin_sarg2 as (ba2 & A2 & B2).
+ eexists. constructor.
+ + simpl. rewrite A1. rewrite A2. reflexivity.
+ + constructor; assumption.
+Qed.
+
+Lemma eval_builtin_sval_args ctx lbs:
+ forall lba m v,
+ eval_list_builtin_sval ctx lbs = Some lba ->
+ list_forall2 (eval_builtin_arg (cge ctx) (fun id => id) (csp ctx) m) lba v ->
+ eval_builtin_sargs ctx m lbs v.
+Proof.
+ unfold eval_builtin_sargs; induction lbs; simpl; intros lba m v.
+ - intros H; inversion H; subst; clear H.
+ intros H; inversion H. econstructor.
+ - destruct (eval_builtin_sval _ _) eqn:SV; try congruence.
+ destruct (eval_list_builtin_sval _ _) eqn: SVL; try congruence.
+ intros H; inversion H; subst; clear H.
+ intros H; inversion H; subst; clear H.
+ econstructor; eauto.
+ eapply eval_builtin_sval_arg; eauto.
+Qed.
+
+Lemma eval_builtin_sargs_sval ctx m lv: forall lbs,
+ eval_builtin_sargs ctx m lbs lv ->
+ exists lba,
+ eval_list_builtin_sval ctx lbs = Some lba
+ /\ list_forall2 (eval_builtin_arg (cge ctx) (fun id => id) (csp ctx) m) lba lv.
+Proof.
+ induction 1.
+ - eexists. constructor.
+ + simpl. reflexivity.
+ + constructor.
+ - destruct IHlist_forall2 as (lba & A & B).
+ apply eval_builtin_sarg_sval in H. destruct H as (ba & A' & B').
+ eexists. constructor.
+ + simpl. rewrite A'. rewrite A. reflexivity.
+ + constructor; assumption.
+Qed.
+
+Lemma eval_builtin_sval_correct ctx m: forall bs1 v bs2,
+ eval_builtin_sarg ctx m bs1 v ->
+ (eval_builtin_sval ctx bs1) = (eval_builtin_sval ctx bs2) ->
+ eval_builtin_sarg ctx m bs2 v.
+Proof.
+ intros. exploit eval_builtin_sarg_sval; eauto.
+ intros (ba & X1 & X2).
+ eapply eval_builtin_sval_arg; eauto.
+ congruence.
+Qed.
+
+Lemma eval_list_builtin_sval_correct ctx m vargs: forall lbs1,
+ eval_builtin_sargs ctx m lbs1 vargs ->
+ forall lbs2, (eval_list_builtin_sval ctx lbs1) = (eval_list_builtin_sval ctx lbs2) ->
+ eval_builtin_sargs ctx m lbs2 vargs.
+Proof.
+ intros. exploit eval_builtin_sargs_sval; eauto.
+ intros (ba & X1 & X2).
+ eapply eval_builtin_sval_args; eauto.
+ congruence.
+Qed.
+
+(** * Symbolic (final) value of a block *)
+
+(** TODO: faut-il hash-conser les valeurs symboliques finales. Pas très utile si pas de join interne.
+Mais peut être utile dans le cas contraire. *)
+
+Inductive sfval :=
+ | Sgoto (pc: exit)
+ | Scall (sig:signature) (svos: sval + ident) (lsv:list_sval) (res:reg) (pc:exit)
+ | Stailcall: signature -> sval + ident -> list_sval -> sfval
+ | Sbuiltin (ef:external_function) (sargs: list (builtin_arg sval)) (res: builtin_res reg) (pc:exit)
+ | Sjumptable (sv: sval) (tbl: list exit)
+ | Sreturn: option sval -> sfval
+.
+
+Definition sfind_function ctx (svos : sval + ident): option fundef :=
+ match svos with
+ | inl sv => SOME v <- eval_sval ctx sv IN Genv.find_funct (cge ctx) v
+ | inr symb => SOME b <- Genv.find_symbol (cge ctx) symb IN Genv.find_funct_ptr (cge ctx) b
+ end
+.
+
+Import ListNotations.
+Local Open Scope list_scope.
+
+Inductive sem_sfval ctx stk: sfval -> regset -> mem -> trace -> state -> Prop :=
+ | exec_Sgoto pc rs m:
+ sem_sfval ctx stk (Sgoto pc) rs m E0 (State stk (cf ctx) (csp ctx) pc (tr_inputs ctx.(cf) [pc] None rs) m)
+ | exec_Sreturn pstk osv rs m m' v:
+ (csp ctx) = (Vptr pstk Ptrofs.zero) ->
+ Mem.free m pstk 0 (cf ctx).(fn_stacksize) = Some m' ->
+ match osv with Some sv => eval_sval ctx sv | None => Some Vundef end = Some v ->
+ sem_sfval ctx stk (Sreturn osv) rs m
+ E0 (Returnstate stk v m')
+ | exec_Scall rs m sig svos lsv args res pc fd:
+ sfind_function ctx svos = Some fd ->
+ funsig fd = sig ->
+ eval_list_sval ctx lsv = Some args ->
+ sem_sfval ctx stk (Scall sig svos lsv res pc) rs m
+ E0 (Callstate (Stackframe res (cf ctx) (csp ctx) pc (tr_inputs ctx.(cf) [pc] (Some res) rs)::stk) fd args m)
+ | exec_Stailcall pstk rs m sig svos args fd m' lsv:
+ sfind_function ctx svos = Some fd ->
+ funsig fd = sig ->
+ (csp ctx) = Vptr pstk Ptrofs.zero ->
+ Mem.free m pstk 0 (cf ctx).(fn_stacksize) = Some m' ->
+ eval_list_sval ctx lsv = Some args ->
+ sem_sfval ctx stk (Stailcall sig svos lsv) rs m
+ E0 (Callstate stk fd args m')
+ | exec_Sbuiltin m' rs m vres res pc t sargs ef vargs:
+ eval_builtin_sargs ctx m sargs vargs ->
+ external_call ef (cge ctx) vargs m t vres m' ->
+ sem_sfval ctx stk (Sbuiltin ef sargs res pc) rs m
+ t (State stk (cf ctx) (csp ctx) pc (regmap_setres res vres (tr_inputs (cf ctx) [pc] (reg_builtin_res res) rs)) m')
+ | exec_Sjumptable sv tbl pc' n rs m:
+ eval_sval ctx sv = Some (Vint n) ->
+ list_nth_z tbl (Int.unsigned n) = Some pc' ->
+ sem_sfval ctx stk (Sjumptable sv tbl) rs m
+ E0 (State stk (cf ctx) (csp ctx) pc' (tr_inputs ctx.(cf) tbl None rs) m)
+.
+
+(* Syntax and Semantics of symbolic internal states *)
+(* [si_pre] is a precondition on initial context *)
+Record sistate := { si_pre: iblock_exec_context -> Prop; si_sreg:> reg -> sval; si_smem: smem }.
+
+(* Predicate on which (rs, m) is a possible final state after evaluating [st] on ((crs0 ctx), (cm0 ctx)) *)
+Definition sem_sistate ctx (sis: sistate) (rs: regset) (m: mem): Prop :=
+ sis.(si_pre) ctx
+ /\ eval_smem ctx sis.(si_smem) = Some m
+ /\ forall (r:reg), eval_sval ctx (sis.(si_sreg) r) = Some (rs#r).
+
+(** * Symbolic execution of final step *)
+Definition sexec_final_sfv (i: final) (sreg: reg -> sval): sfval :=
+ match i with
+ | Bgoto pc => Sgoto pc
+ | Bcall sig ros args res pc =>
+ let svos := sum_left_map sreg ros in
+ let sargs := list_sval_inj (List.map sreg args) in
+ Scall sig svos sargs res pc
+ | Btailcall sig ros args =>
+ let svos := sum_left_map sreg ros in
+ let sargs := list_sval_inj (List.map sreg args) in
+ Stailcall sig svos sargs
+ | Bbuiltin ef args res pc =>
+ let sargs := List.map (builtin_arg_map sreg) args in
+ Sbuiltin ef sargs res pc
+ | Breturn or =>
+ let sor := SOME r <- or IN Some (sreg r) in
+ Sreturn sor
+ | Bjumptable reg tbl =>
+ let sv := sreg reg in
+ Sjumptable sv tbl
+ end.
+
+Local Hint Constructors sem_sfval: core.
+
+Lemma sexec_final_sfv_correct ctx stk i sis t rs m s:
+ sem_sistate ctx sis rs m ->
+ final_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) rs m i t s ->
+ sem_sfval ctx stk (sexec_final_sfv i sis) rs m t s.
+Proof.
+ intros (PRE&MEM&REG).
+ destruct 1; subst; try_simplify_someHyps; simpl; intros; try autodestruct; eauto.
+ + (* Bcall *) intros; eapply exec_Scall; auto.
+ - destruct ros; simpl in * |- *; auto.
+ rewrite REG; auto.
+ - erewrite eval_list_sval_inj; simpl; auto.
+ + (* Btailcall *) intros. eapply exec_Stailcall; eauto.
+ - destruct ros; simpl in * |- *; eauto.
+ rewrite REG; eauto.
+ - erewrite eval_list_sval_inj; simpl; auto.
+ + (* Bbuiltin *) intros. eapply exec_Sbuiltin; eauto.
+ eapply eval_builtin_sargs_correct; eauto.
+ + (* Bjumptable *) intros. eapply exec_Sjumptable; eauto. congruence.
+Qed.
+
+Local Hint Constructors final_step: core.
+Local Hint Resolve eval_builtin_sargs_exact: core.
+
+Lemma sexec_final_sfv_exact ctx stk i sis t rs m s:
+ sem_sistate ctx sis rs m ->
+ sem_sfval ctx stk (sexec_final_sfv i sis) rs m t s
+ -> final_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) rs m i t s.
+Proof.
+ intros (PRE&MEM&REG).
+ destruct i; simpl; intros LAST; inv LAST; eauto.
+ + (* Breturn *)
+ enough (v=regmap_optget res Vundef rs) as ->; eauto.
+ destruct res; simpl in *; congruence.
+ + (* Bcall *)
+ erewrite eval_list_sval_inj in *; try_simplify_someHyps.
+ intros; eapply exec_Bcall; eauto.
+ destruct fn; simpl in * |- *; auto.
+ rewrite REG in * |- ; auto.
+ + (* Btailcall *)
+ erewrite eval_list_sval_inj in *; try_simplify_someHyps.
+ intros; eapply exec_Btailcall; eauto.
+ destruct fn; simpl in * |- *; auto.
+ rewrite REG in * |- ; auto.
+ + (* Bjumptable *)
+ eapply exec_Bjumptable; eauto.
+ congruence.
+Qed.
+
+(** * symbolic execution of basic instructions *)
+
+Definition sis_init : sistate := {| si_pre:= fun _ => True; si_sreg:= fun r => fSinput r; si_smem:= fSinit |}.
+
+Lemma sis_init_correct ctx:
+ sem_sistate ctx sis_init (crs0 ctx) (cm0 ctx).
+Proof.
+ unfold sis_init, sem_sistate; simpl; intuition eauto.
+Qed.
+
+Definition set_sreg (r:reg) (sv:sval) (sis:sistate): sistate :=
+ {| si_pre:=(fun ctx => eval_sval ctx (sis.(si_sreg) r) <> None /\ (sis.(si_pre) ctx));
+ si_sreg:=fun y => if Pos.eq_dec r y then sv else sis.(si_sreg) y;
+ si_smem:= sis.(si_smem)|}.
+
+Lemma set_sreg_correct ctx dst sv sis (rs rs': regset) m:
+ sem_sistate ctx sis rs m ->
+ (eval_sval ctx sv = Some rs' # dst) ->
+ (forall r, r <> dst -> rs'#r = rs#r) ->
+ sem_sistate ctx (set_sreg dst sv sis) rs' m.
+Proof.
+ intros (PRE&MEM&REG) NEW OLD.
+ unfold sem_sistate; simpl.
+ intuition.
+ - rewrite REG in *; congruence.
+ - destruct (Pos.eq_dec dst r); simpl; subst; eauto.
+ rewrite REG in *. rewrite OLD; eauto.
+Qed.
+
+Definition set_smem (sm:smem) (sis:sistate): sistate :=
+ {| si_pre:=(fun ctx => eval_smem ctx sis.(si_smem) <> None /\ (sis.(si_pre) ctx));
+ si_sreg:= sis.(si_sreg);
+ si_smem:= sm |}.
+
+Lemma set_smem_correct ctx sm sis rs m m':
+ sem_sistate ctx sis rs m ->
+ eval_smem ctx sm = Some m' ->
+ sem_sistate ctx (set_smem sm sis) rs m'.
+Proof.
+ intros (PRE&MEM&REG) NEW.
+ unfold sem_sistate; simpl.
+ intuition.
+ rewrite MEM in *; congruence.
+Qed.
+
+Definition sexec_op op args dst sis: sistate :=
+ let args := list_sval_inj (List.map sis.(si_sreg) args) in
+ set_sreg dst (fSop op args) sis.
+
+Lemma sexec_op_correct ctx op args dst sis rs m v
+ (EVAL: eval_operation (cge ctx) (csp ctx) op rs ## args m = Some v)
+ (SIS: sem_sistate ctx sis rs m)
+ :(sem_sistate ctx (sexec_op op args dst sis) (rs#dst <- v) m).
+Proof.
+ eapply set_sreg_correct; eauto.
+ - simpl. destruct SIS as (PRE&MEM&REG).
+ rewrite Regmap.gss; simpl; auto.
+ erewrite eval_list_sval_inj; simpl; auto.
+ try_simplify_someHyps.
+ intros; erewrite op_valid_pointer_eq; eauto.
+ - intros; rewrite Regmap.gso; auto.
+Qed.
+
+Definition sexec_load trap chunk addr args dst sis: sistate :=
+ let args := list_sval_inj (List.map sis.(si_sreg) args) in
+ set_sreg dst (fSload sis.(si_smem) trap chunk addr args) sis.
+
+Lemma sexec_load_correct ctx chunk addr args dst sis rs m v trap
+ (HLOAD: has_loaded (cge ctx) (csp ctx) rs m chunk addr args v trap)
+ (SIS: sem_sistate ctx sis rs m)
+ :(sem_sistate ctx (sexec_load trap chunk addr args dst sis) (rs#dst <- v) m).
+Proof.
+ inv HLOAD; eapply set_sreg_correct; eauto.
+ 2,4: intros; rewrite Regmap.gso; auto.
+ - simpl. destruct SIS as (PRE&MEM&REG).
+ destruct trap; rewrite Regmap.gss; simpl; auto;
+ erewrite eval_list_sval_inj; simpl; auto;
+ try_simplify_someHyps.
+ intros. rewrite LOAD; auto.
+ - simpl. destruct SIS as (PRE&MEM&REG).
+ rewrite Regmap.gss; simpl; auto.
+ erewrite eval_list_sval_inj; simpl; auto.
+ autodestruct; rewrite MEM, LOAD; auto.
+Qed.
+
+Definition sexec_store chunk addr args src sis: sistate :=
+ let args := list_sval_inj (List.map sis.(si_sreg) args) in
+ let src := sis.(si_sreg) src in
+ let sm := fSstore sis.(si_smem) chunk addr args src in
+ set_smem sm sis.
+
+Lemma sexec_store_correct ctx chunk addr args src sis rs m m' a
+ (EVAL: eval_addressing (cge ctx) (csp ctx) addr rs ## args = Some a)
+ (STORE: Mem.storev chunk m a (rs # src) = Some m')
+ (SIS: sem_sistate ctx sis rs m)
+ :(sem_sistate ctx (sexec_store chunk addr args src sis) rs m').
+Proof.
+ eapply set_smem_correct; eauto.
+ simpl. destruct SIS as (PRE&MEM&REG).
+ erewrite eval_list_sval_inj; simpl; auto.
+ try_simplify_someHyps.
+ rewrite REG; auto.
+Qed.
+
+Lemma eval_scondition_eq ctx cond args sis rs m
+ (SIS : sem_sistate ctx sis rs m)
+ :eval_scondition ctx cond (list_sval_inj (map (si_sreg sis) args)) = eval_condition cond rs ## args m.
+Proof.
+ destruct SIS as (PRE&MEM&REG); unfold eval_scondition; simpl.
+ erewrite eval_list_sval_inj; simpl; auto.
+ eapply cond_valid_pointer_eq; eauto.
+Qed.
+
+(** * Compute sistate associated to final values *)
+Fixpoint transfer_sreg (inputs: list reg) (sreg: reg -> sval): reg -> sval :=
+ match inputs with
+ | nil => fun r => fSundef
+ | r1::l => fun r => if Pos.eq_dec r1 r then sreg r1 else transfer_sreg l sreg r
+ end.
+
+Definition str_inputs (f:function) (tbl: list exit) (or:option reg) := transfer_sreg (Regset.elements (pre_inputs f tbl or)).
+
+Lemma str_inputs_correct ctx sis rs tbl or r:
+ (forall r : reg, eval_sval ctx (si_sreg sis r) = Some rs # r) ->
+ eval_sval ctx (str_inputs (cf ctx) tbl or (si_sreg sis) r) = Some (tr_inputs (cf ctx) tbl or rs) # r.
+Proof.
+ intros H.
+ unfold str_inputs, tr_inputs, transfer_regs.
+ induction (Regset.elements _) as [|x l]; simpl.
+ + rewrite Regmap.gi; auto.
+ + autodestruct; intros; subst.
+ * rewrite Regmap.gss; auto.
+ * rewrite Regmap.gso; eauto.
+Qed.
+
+Local Hint Resolve str_inputs_correct: core.
+
+Definition tr_sis f (fi: final) (sis: sistate) :=
+ {| si_pre := fun ctx => (sis.(si_pre) ctx /\ forall r, eval_sval ctx (sis.(si_sreg) r) <> None);
+ si_sreg := poly_tr str_inputs f fi sis.(si_sreg);
+ si_smem := sis.(si_smem) |}.
+
+Lemma tr_sis_regs_correct_aux ctx fin sis rs m:
+ sem_sistate ctx sis rs m ->
+ (forall r, eval_sval ctx (tr_sis (cf ctx) fin sis r) = Some (tr_regs (cf ctx) fin rs) # r).
+Proof.
+ Local Opaque str_inputs.
+ simpl. destruct 1 as (_ & _ & REG).
+ destruct fin; simpl; eauto.
+Qed.
+
+Lemma tr_sis_regs_correct ctx fin sis rs m:
+ sem_sistate ctx sis rs m ->
+ sem_sistate ctx (tr_sis (cf ctx) fin sis) (tr_regs (cf ctx) fin rs) m.
+Proof.
+ intros H.
+ generalize (tr_sis_regs_correct_aux _ fin _ _ _ H).
+ destruct H as (PRE & MEM & REG).
+ econstructor; simpl; intuition eauto || congruence.
+Qed.
+
+Definition poly_str {A} (tr: function -> list exit -> option reg -> A) f (sfv: sfval): A :=
+ match sfv with
+ | Sgoto pc => tr f [pc] None
+ | Scall _ _ _ res pc => tr f [pc] (Some res)
+ | Stailcall _ _ args => tr f [] None
+ | Sbuiltin _ _ res pc => tr f [pc] (reg_builtin_res res)
+ | Sreturn _ => tr f [] None
+ | Sjumptable _ tbl => tr f tbl None
+ end.
+
+Definition str_regs: function -> sfval -> regset -> regset :=
+ poly_str tr_inputs.
+
+Lemma str_tr_regs_equiv f fin sis:
+ str_regs f (sexec_final_sfv fin sis) = tr_regs f fin.
+Proof.
+ destruct fin; simpl; auto.
+Qed.
+
+(** * symbolic execution of blocks *)
+
+(* symbolic state *)
+Inductive sstate :=
+ | Sfinal (sis: sistate) (sfv: sfval)
+ | Scond (cond: condition) (args: list_sval) (ifso ifnot: sstate)
+ | Sabort
+ .
+
+(* outcome of a symbolic execution path *)
+Record soutcome := sout {
+ _sis: sistate;
+ _sfv: sfval;
+}.
+
+Fixpoint get_soutcome ctx (st:sstate): option soutcome :=
+ match st with
+ | Sfinal sis sfv => Some (sout sis sfv)
+ | Scond cond args ifso ifnot =>
+ SOME b <- eval_scondition ctx cond args IN
+ get_soutcome ctx (if b then ifso else ifnot)
+ | Sabort => None
+ end.
+
+(* transition (t,s) produced by a sstate in initial context ctx *)
+Inductive sem_sstate ctx stk t s: sstate -> Prop :=
+ | sem_Sfinal sis sfv rs m
+ (SIS: sem_sistate ctx sis (str_regs (cf ctx) sfv rs) m)
+ (SFV: sem_sfval ctx stk sfv rs m t s)
+ : sem_sstate ctx stk t s (Sfinal sis sfv)
+ | sem_Scond b cond args ifso ifnot
+ (SEVAL: eval_scondition ctx cond args = Some b)
+ (SELECT: sem_sstate ctx stk t s (if b then ifso else ifnot))
+ : sem_sstate ctx stk t s (Scond cond args ifso ifnot)
+ (* NB: Sabort: fails to produce a transition *)
+ .
+
+Lemma sem_sstate_run ctx stk st t s:
+ sem_sstate ctx stk t s st ->
+ exists sis sfv rs m,
+ get_soutcome ctx st = Some (sout sis sfv)
+ /\ sem_sistate ctx sis (str_regs (cf ctx) sfv rs) m
+ /\ sem_sfval ctx stk sfv rs m t s
+ .
+Proof.
+ induction 1; simpl; try_simplify_someHyps; do 4 eexists; intuition eauto.
+Qed.
+
+Local Hint Resolve sem_Sfinal: core.
+
+Lemma run_sem_sstate ctx st sis sfv:
+ get_soutcome ctx st = Some (sout sis sfv) ->
+ forall rs m stk s t,
+ sem_sistate ctx sis (str_regs (cf ctx) sfv rs) m ->
+ sem_sfval ctx stk sfv rs m t s ->
+ sem_sstate ctx stk t s st
+ .
+Proof.
+ induction st; simpl; try_simplify_someHyps.
+ autodestruct; intros; econstructor; eauto.
+ autodestruct; eauto.
+Qed.
+
+
+(** * Model of Symbolic Execution with Continuation Passing Style
+
+Parameter [k] is the continuation, i.e. the [sstate] construction that will be applied in each execution branch.
+Its input parameter is the symbolic internal state of the branch.
+
+*)
+
+Fixpoint sexec_rec f ib sis (k: sistate -> sstate): sstate :=
+ match ib with
+ | BF fin _ => Sfinal (tr_sis f fin sis) (sexec_final_sfv fin sis)
+ (* basic instructions *)
+ | Bnop _ => k sis
+ | Bop op args res _ => k (sexec_op op args res sis)
+ | Bload trap chunk addr args dst _ => k (sexec_load trap chunk addr args dst sis)
+ | Bstore chunk addr args src _ => k (sexec_store chunk addr args src sis)
+ (* composed instructions *)
+ | Bseq ib1 ib2 =>
+ sexec_rec f ib1 sis (fun sis2 => sexec_rec f ib2 sis2 k)
+ | Bcond cond args ifso ifnot _ =>
+ let args := list_sval_inj (List.map sis.(si_sreg) args) in
+ let ifso := sexec_rec f ifso sis k in
+ let ifnot := sexec_rec f ifnot sis k in
+ Scond cond args ifso ifnot
+ end
+ .
+
+Definition sexec f ib := sexec_rec f ib sis_init (fun _ => Sabort).
+
+Local Hint Constructors sem_sstate: core.
+Local Hint Resolve sexec_op_correct sexec_final_sfv_correct tr_sis_regs_correct_aux tr_sis_regs_correct
+ sexec_load_correct sexec_store_correct sis_init_correct: core.
+
+Lemma sexec_rec_correct ctx stk t s ib rs m rs1 m1 ofin
+ (ISTEP: iblock_istep (cge ctx) (csp ctx) rs m ib rs1 m1 ofin): forall sis k
+ (SIS: sem_sistate ctx sis rs m)
+ (CONT: match ofin with
+ | None => forall sis', sem_sistate ctx sis' rs1 m1 -> sem_sstate ctx stk t s (k sis')
+ | Some fin => final_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) rs1 m1 fin t s
+ end),
+ sem_sstate ctx stk t s (sexec_rec (cf ctx) ib sis k).
+Proof.
+ induction ISTEP; simpl; try autodestruct; eauto.
+ (* final value *)
+ intros; econstructor; eauto.
+ rewrite str_tr_regs_equiv; eauto.
+ (* condition *)
+ all: intros;
+ eapply sem_Scond; eauto; [
+ erewrite eval_scondition_eq; eauto |
+ replace (if b then sexec_rec (cf ctx) ifso sis k else sexec_rec (cf ctx) ifnot sis k) with (sexec_rec (cf ctx) (if b then ifso else ifnot) sis k);
+ try autodestruct; eauto ].
+Qed.
+
+
+(* NB: each concrete execution can be executed on the symbolic state (produced from [sexec])
+ (sexec is a correct over-approximation)
+*)
+Theorem sexec_correct ctx stk ib t s:
+ iblock_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) (crs0 ctx) (cm0 ctx) ib t s ->
+ sem_sstate ctx stk t s (sexec (cf ctx) ib).
+Proof.
+ destruct 1 as (rs' & m' & fin & ISTEP & FSTEP).
+ eapply sexec_rec_correct; simpl; eauto.
+Qed.
+
+(* Remark that we want to reason modulo "extensionality" wrt Regmap.get about regsets.
+ And, nothing in their representation as (val * PTree.t val) enforces that
+ (forall r, rs1#r = rs2#r) -> rs1 = rs2
+*)
+Lemma sem_sistate_tr_sis_determ ctx sis rs1 m1 fi rs2 m2:
+ sem_sistate ctx sis rs1 m1 ->
+ sem_sistate ctx (tr_sis (cf ctx) fi sis) rs2 m2 ->
+ (forall r, rs2#r = (tr_regs (cf ctx) fi rs1)#r)
+ /\ m1 = m2.
+Proof.
+ intros H1 H2.
+ lapply (tr_sis_regs_correct_aux ctx fi sis rs1 m1); eauto.
+ intros X.
+ destruct H1 as (_&MEM1&REG1).
+ destruct H2 as (_&MEM2&REG2); simpl in *.
+ intuition try congruence.
+ cut (Some rs2 # r = Some (tr_regs (cf ctx) fi rs1)#r).
+ { congruence. }
+ rewrite <- REG2, X. auto.
+Qed.
+
+Local Hint Constructors equiv_stackframe list_forall2: core.
+Local Hint Resolve regmap_setres_eq equiv_stack_refl equiv_stack_refl: core.
+
+Lemma sem_sfval_equiv rs1 rs2 ctx stk sfv m t s:
+ sem_sfval ctx stk sfv rs1 m t s ->
+ (forall r, (str_regs (cf ctx) sfv rs1)#r = (str_regs (cf ctx) sfv rs2)#r) ->
+ exists s', sem_sfval ctx stk sfv rs2 m t s' /\ equiv_state s s'.
+Proof.
+ unfold str_regs.
+ destruct 1; simpl in *; intros; subst; eexists; split; econstructor; eauto; try congruence.
+Qed.
+
+Definition abort_sistate ctx (sis: sistate): Prop :=
+ ~(sis.(si_pre) ctx)
+ \/ eval_smem ctx sis.(si_smem) = None
+ \/ exists (r: reg), eval_sval ctx (sis.(si_sreg) r) = None.
+
+Lemma set_sreg_preserves_abort ctx sv dst sis:
+ abort_sistate ctx sis ->
+ abort_sistate ctx (set_sreg dst sv sis).
+Proof.
+ unfold abort_sistate; simpl; intros [PRE|[MEM|REG]]; try tauto.
+ destruct REG as [r REG].
+ destruct (Pos.eq_dec dst r) as [TEST|TEST] eqn: HTEST.
+ - subst; rewrite REG; tauto.
+ - right. right. eexists; rewrite HTEST. auto.
+Qed.
+
+Lemma sexec_op_preserves_abort ctx op args dest sis:
+ abort_sistate ctx sis
+ -> abort_sistate ctx (sexec_op op args dest sis).
+Proof.
+ intros; eapply set_sreg_preserves_abort; eauto.
+Qed.
+
+Lemma sexec_load_preserves_abort ctx chunk addr args dest sis trap:
+ abort_sistate ctx sis
+ -> abort_sistate ctx (sexec_load trap chunk addr args dest sis).
+Proof.
+ intros; eapply set_sreg_preserves_abort; eauto.
+Qed.
+
+Lemma set_smem_preserves_abort ctx sm sis:
+ abort_sistate ctx sis ->
+ abort_sistate ctx (set_smem sm sis).
+Proof.
+ unfold abort_sistate; simpl; try tauto.
+Qed.
+
+Lemma sexec_store_preserves_abort ctx chunk addr args src sis:
+ abort_sistate ctx sis
+ -> abort_sistate ctx (sexec_store chunk addr args src sis).
+Proof.
+ intros; eapply set_smem_preserves_abort; eauto.
+Qed.
+
+Lemma sem_sistate_tr_sis_exclude_abort ctx sis fi rs m:
+ sem_sistate ctx (tr_sis (cf ctx) fi sis) rs m ->
+ abort_sistate ctx sis ->
+ False.
+Proof.
+ intros ((PRE1 & PRE2) & MEM & REG); simpl in *.
+ intros [ABORT1 | [ABORT2 | ABORT3]]; [ | | inv ABORT3]; try congruence.
+Qed.
+
+Local Hint Resolve sexec_op_preserves_abort sexec_load_preserves_abort
+ sexec_store_preserves_abort sem_sistate_tr_sis_exclude_abort: core.
+
+Lemma sexec_exclude_abort ctx stk ib t s1: forall sis k
+ (SEXEC: sem_sstate ctx stk t s1 (sexec_rec (cf ctx) ib sis k))
+ (CONT: forall sis', sem_sstate ctx stk t s1 (k sis') -> (abort_sistate ctx sis') -> False)
+ (ABORT: abort_sistate ctx sis),
+ False.
+Proof.
+ induction ib; simpl; intros; eauto.
+ - (* final *) inversion SEXEC; subst; eauto.
+ - (* seq *)
+ eapply IHib1; eauto.
+ simpl. eauto.
+ - (* cond *)
+ inversion SEXEC; subst; eauto. clear SEXEC.
+ destruct b; eauto.
+Qed.
+
+Lemma set_sreg_abort ctx dst sv sis rs m:
+ sem_sistate ctx sis rs m ->
+ (eval_sval ctx sv = None) ->
+ abort_sistate ctx (set_sreg dst sv sis).
+Proof.
+ intros (PRE&MEM&REG) NEW.
+ unfold sem_sistate, abort_sistate; simpl.
+ right; right.
+ exists dst; destruct (Pos.eq_dec dst dst); simpl; try congruence.
+Qed.
+
+Lemma sexec_op_abort ctx sis op args dest rs m
+ (EVAL: eval_operation (cge ctx) (csp ctx) op rs ## args m = None)
+ (SIS: sem_sistate ctx sis rs m)
+ : abort_sistate ctx (sexec_op op args dest sis).
+Proof.
+ eapply set_sreg_abort; eauto.
+ simpl. destruct SIS as (PRE&MEM&REG).
+ erewrite eval_list_sval_inj; simpl; auto.
+ try_simplify_someHyps.
+ intros; erewrite op_valid_pointer_eq; eauto.
+Qed.
+
+Lemma sexec_load_TRAP_abort ctx chunk addr args dst sis rs m
+ (EVAL: forall a, eval_addressing (cge ctx) (csp ctx) addr rs ## args = Some a -> Mem.loadv chunk m a = None)
+ (SIS: sem_sistate ctx sis rs m)
+ : abort_sistate ctx (sexec_load TRAP chunk addr args dst sis).
+Proof.
+ eapply set_sreg_abort; eauto.
+ simpl. destruct SIS as (PRE&MEM&REG).
+ erewrite eval_list_sval_inj; simpl; auto.
+ intros; autodestruct; try_simplify_someHyps.
+Qed.
+
+Lemma set_smem_abort ctx sm sis rs m:
+ sem_sistate ctx sis rs m ->
+ eval_smem ctx sm = None ->
+ abort_sistate ctx (set_smem sm sis).
+Proof.
+ intros (PRE&MEM&REG) NEW.
+ unfold abort_sistate; simpl.
+ tauto.
+Qed.
+
+Lemma sexec_store_abort ctx chunk addr args src sis rs m
+ (EVAL: forall a, eval_addressing (cge ctx) (csp ctx) addr rs ## args = Some a -> Mem.storev chunk m a (rs # src) = None)
+ (SIS: sem_sistate ctx sis rs m)
+ :abort_sistate ctx (sexec_store chunk addr args src sis).
+Proof.
+ eapply set_smem_abort; eauto.
+ simpl. destruct SIS as (PRE&MEM&REG).
+ erewrite eval_list_sval_inj; simpl; auto.
+ try_simplify_someHyps.
+ intros; rewrite REG; autodestruct; try_simplify_someHyps.
+Qed.
+
+Local Hint Resolve sexec_op_abort sexec_load_TRAP_abort sexec_store_abort sexec_final_sfv_exact: core.
+
+Lemma sexec_rec_exact ctx stk ib t s1: forall sis k
+ (SEXEC: sem_sstate ctx stk t s1 (sexec_rec (cf ctx) ib sis k))
+ rs m
+ (SIS: sem_sistate ctx sis rs m)
+ (CONT: forall sis', sem_sstate ctx stk t s1 (k sis') -> (abort_sistate ctx sis') -> False)
+ ,
+ match iblock_istep_run (cge ctx) (csp ctx) ib rs m with
+ | Some (out rs' m' (Some fin)) =>
+ exists s2, final_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) rs' m' fin t s2 /\ equiv_state s1 s2
+ | Some (out rs' m' None) => exists sis', (sem_sstate ctx stk t s1 (k sis')) /\ (sem_sistate ctx sis' rs' m')
+ | None => False
+ end.
+Proof.
+ induction ib; simpl; intros; eauto.
+ - (* final *)
+ inv SEXEC.
+ exploit (sem_sistate_tr_sis_determ ctx sis rs m fi); eauto.
+ intros (REG&MEM); subst.
+ exploit (sem_sfval_equiv rs0 rs); eauto.
+ * intros; rewrite REG, str_tr_regs_equiv; auto.
+ * intros (s2 & EQUIV & SFV'); eauto.
+ - (* Bop *) autodestruct; eauto.
+ - destruct trap.
+ + repeat autodestruct.
+ { eexists; split; eauto.
+ eapply sexec_load_correct; eauto.
+ econstructor; eauto. }
+ all:
+ intros; eapply CONT; eauto;
+ eapply sexec_load_TRAP_abort; eauto;
+ intros; try_simplify_someHyps.
+ + repeat autodestruct;
+ eexists; split; eauto;
+ eapply sexec_load_correct; eauto;
+ try (econstructor; eauto; fail).
+ all: eapply has_loaded_default; auto; try_simplify_someHyps.
+ - repeat autodestruct; eauto.
+ all: intros; eapply CONT; eauto;
+ eapply sexec_store_abort; eauto;
+ intros; try_simplify_someHyps.
+ - (* Bseq *)
+ exploit IHib1; eauto. clear sis SEXEC SIS.
+ { simpl; intros; eapply sexec_exclude_abort; eauto. }
+ destruct (iblock_istep_run _ _ _ _ _) eqn: ISTEP; auto.
+ destruct o.
+ destruct _fin eqn: OFIN; simpl; eauto.
+ intros (sis1 & SEXEC1 & SIS1).
+ exploit IHib2; eauto.
+ - (* Bcond *)
+ inv SEXEC.
+ erewrite eval_scondition_eq in SEVAL; eauto.
+ rewrite SEVAL.
+ destruct b.
+ + exploit IHib1; eauto.
+ + exploit IHib2; eauto.
+Qed.
+
+
+(* NB: each execution of a symbolic state (produced from [sexec]) represents a concrete execution
+ (sexec is exact).
+*)
+Theorem sexec_exact ctx stk ib t s1:
+ sem_sstate ctx stk t s1 (sexec (cf ctx) ib) ->
+ exists s2, iblock_step tr_inputs (cge ctx) stk (cf ctx) (csp ctx) (crs0 ctx) (cm0 ctx) ib t s2
+ /\ equiv_state s1 s2.
+Proof.
+ intros; exploit sexec_rec_exact; eauto.
+ { intros sis' SEXEC; inversion SEXEC. }
+ repeat autodestruct; simpl; try tauto.
+ - intros D1 D2 ISTEP (s2 & FSTEP & EQSTEP); subst.
+ eexists; split; eauto.
+ repeat eexists; eauto.
+ erewrite iblock_istep_run_equiv; eauto.
+ - intros D1 D2 ISTEP (sis & SEXEC & _); subst.
+ inversion SEXEC.
+Qed.
+
+(** * High-Level specification of the symbolic simulation test as predicate [symbolic_simu] *)
+
+Record simu_proof_context {f1 f2: BTL.function} := Sctx {
+ sge1: BTL.genv;
+ sge2: BTL.genv;
+ sge_match: forall s, Genv.find_symbol sge1 s = Genv.find_symbol sge2 s;
+ ssp: val;
+ srs0: regset;
+ sm0: mem
+}.
+Arguments simu_proof_context: clear implicits.
+
+Definition bctx1 {f1 f2} (ctx: simu_proof_context f1 f2):= Bctx ctx.(sge1) f1 ctx.(ssp) ctx.(srs0) ctx.(sm0).
+Definition bctx2 {f1 f2} (ctx: simu_proof_context f1 f2):= Bctx ctx.(sge2) f2 ctx.(ssp) ctx.(srs0) ctx.(sm0).
+
+(* NOTE: we need to mix semantical simulation and syntactic definition on [sfval] in order to abstract
+ the [match_states] of BTL_Schedulerproof.
+
+ Indeed, the [match_states] involves [match_function] in [match_stackframe].
+ And, here, we aim to define a notion of simulation for defining [match_function].
+
+ A syntactic definition of the simulation on [sfval] avoids the circularity issue.
+
+*)
+
+Inductive optsv_simu {f1 f2: function} (ctx: simu_proof_context f1 f2): (option sval) -> (option sval) -> Prop :=
+ | Ssome_simu sv1 sv2
+ (SIMU:eval_sval (bctx1 ctx) sv1 = eval_sval (bctx2 ctx) sv2)
+ :optsv_simu ctx (Some sv1) (Some sv2)
+ | Snone_simu: optsv_simu ctx None None
+ .
+
+Inductive svident_simu {f1 f2: function} (ctx: simu_proof_context f1 f2): (sval + ident) -> (sval + ident) -> Prop :=
+ | Sleft_simu sv1 sv2
+ (SIMU:eval_sval (bctx1 ctx) sv1 = eval_sval (bctx2 ctx) sv2)
+ :svident_simu ctx (inl sv1) (inl sv2)
+ | Sright_simu id1 id2
+ (IDSIMU: id1 = id2)
+ :svident_simu ctx (inr id1) (inr id2)
+ .
+
+Definition bargs_simu {f1 f2: function} (ctx: simu_proof_context f1 f2) (args1 args2: list (builtin_arg sval)): Prop :=
+ eval_list_builtin_sval (bctx1 ctx) args1 = eval_list_builtin_sval (bctx2 ctx) args2.
+
+Inductive sfv_simu {f1 f2} (ctx: simu_proof_context f1 f2): sfval -> sfval -> Prop :=
+ | Sgoto_simu pc: sfv_simu ctx (Sgoto pc) (Sgoto pc)
+ | Scall_simu sig ros1 ros2 args1 args2 r pc
+ (SVID: svident_simu ctx ros1 ros2)
+ (ARGS:eval_list_sval (bctx1 ctx) args1 = eval_list_sval (bctx2 ctx) args2)
+ :sfv_simu ctx (Scall sig ros1 args1 r pc) (Scall sig ros2 args2 r pc)
+ | Stailcall_simu sig ros1 ros2 args1 args2
+ (SVID: svident_simu ctx ros1 ros2)
+ (ARGS:eval_list_sval (bctx1 ctx) args1 = eval_list_sval (bctx2 ctx) args2)
+ :sfv_simu ctx (Stailcall sig ros1 args1) (Stailcall sig ros2 args2)
+ | Sbuiltin_simu ef lba1 lba2 br pc
+ (BARGS: bargs_simu ctx lba1 lba2)
+ :sfv_simu ctx (Sbuiltin ef lba1 br pc) (Sbuiltin ef lba2 br pc)
+ | Sjumptable_simu sv1 sv2 lpc
+ (VAL: eval_sval (bctx1 ctx) sv1 = eval_sval (bctx2 ctx) sv2)
+ :sfv_simu ctx (Sjumptable sv1 lpc) (Sjumptable sv2 lpc)
+ | simu_Sreturn osv1 osv2
+ (OPT:optsv_simu ctx osv1 osv2)
+ :sfv_simu ctx (Sreturn osv1) (Sreturn osv2)
+.
+
+Definition sistate_simu {f1 f2} (ctx: simu_proof_context f1 f2) (sis1 sis2:sistate): Prop :=
+ forall rs m, sem_sistate (bctx1 ctx) sis1 rs m -> sem_sistate (bctx2 ctx) sis2 rs m.
+
+Record si_ok ctx (sis: sistate): Prop := {
+ OK_PRE: (sis.(si_pre) ctx);
+ OK_SMEM: eval_smem ctx sis.(si_smem) <> None;
+ OK_SREG: forall (r: reg), eval_sval ctx (si_sreg sis r) <> None
+}.
+
+Lemma sem_si_ok ctx sis rs m:
+ sem_sistate ctx sis rs m -> si_ok ctx sis.
+Proof.
+ unfold sem_sistate;
+ econstructor;
+ intuition congruence.
+Qed.
+
+Definition sstate_simu {f1 f2} (ctx: simu_proof_context f1 f2) (st1 st2:sstate): Prop :=
+ forall sis1 sfv1, get_soutcome (bctx1 ctx) st1 = Some (sout sis1 sfv1) -> si_ok (bctx1 ctx) sis1 ->
+ exists sis2 sfv2, get_soutcome (bctx2 ctx) st2 = Some (sout sis2 sfv2)
+ /\ sistate_simu ctx sis1 sis2
+ /\ (forall rs m, sem_sistate (bctx1 ctx) sis1 rs m -> sfv_simu ctx sfv1 sfv2)
+ .
+
+Definition symbolic_simu f1 f2 ib1 ib2: Prop := forall (ctx: simu_proof_context f1 f2), sstate_simu ctx (sexec f1 ib1) (sexec f2 ib2).
+
+(* REM. L'approche suivie initialement ne marche pas !!!
+*)
+(*
+Definition sstate_simu {f1 f2} (ctx: simu_proof_context f1 f2) (st1 st2: sstate) :=
+ forall t s1, sem_sstate (bctx1 ctx) t s1 st1 ->
+ exists s2, sem_sstate (bctx2 ctx) t s2 st2 /\ equiv_state s1 s2.
+
+Definition symbolic_simu f1 f2 ib1 ib2: Prop := forall (ctx: simu_proof_context f1 f2), sstate_simu ctx (sexec f1 ib1) (sexec f2 ib2).
+
+Theorem symbolic_simu_correct f1 f2 ib1 ib2:
+ symbolic_simu f1 f2 ib1 ib2 ->
+ forall (ctx: simu_proof_context f1 f2) t s1, iblock_step tr_inputs (sge1 ctx) (sstk1 ctx) f1 (ssp ctx) (srs0 ctx) (sm0 ctx) ib1 t s1 ->
+ exists s2, iblock_step tr_inputs (sge2 ctx) (sstk2 ctx) f2 (ssp ctx) (srs0 ctx) (sm0 ctx) ib2 t s2 /\ equiv_state s1 s2.
+Proof.
+ unfold symbolic_simu, sstate_simu.
+ intros SIMU ctx t s1 STEP1.
+ exploit (sexec_correct (bctx1 ctx)); simpl; eauto.
+ intros; exploit SIMU; eauto.
+ intros (s2 & SEM1 & EQ1).
+ exploit (sexec_exact (bctx2 ctx)); simpl; eauto.
+ intros (s3 & STEP2 & EQ2).
+ clear STEP1; eexists; split; eauto.
+ eapply equiv_state_trans; eauto.
+Qed.
+*)
+
+(** * Preservation properties under a [simu_proof_context] *)
+
+Section SymbValPreserved.
+
+Variable f1 f2: function.
+
+Hypothesis ctx: simu_proof_context f1 f2.
+Local Hint Resolve sge_match: core.
+
+Lemma eval_sval_preserved sv:
+ eval_sval (bctx1 ctx) sv = eval_sval (bctx2 ctx) sv.
+Proof.
+ induction sv using sval_mut with (P0 := fun lsv => eval_list_sval (bctx1 ctx) lsv = eval_list_sval (bctx2 ctx) lsv)
+ (P1 := fun sm => eval_smem (bctx1 ctx) sm = eval_smem (bctx2 ctx) sm); simpl; auto.
+ + rewrite IHsv; clear IHsv. destruct (eval_list_sval _ _); auto.
+ erewrite eval_operation_preserved; eauto.
+ + rewrite IHsv0; clear IHsv0. destruct (eval_list_sval _ _); auto.
+ erewrite eval_addressing_preserved; eauto.
+ destruct (eval_addressing _ _ _ _); auto.
+ rewrite IHsv; auto.
+ + rewrite IHsv; clear IHsv. destruct (eval_sval _ _); auto.
+ rewrite IHsv0; auto.
+ + rewrite IHsv0; clear IHsv0. destruct (eval_list_sval _ _); auto.
+ erewrite eval_addressing_preserved; eauto.
+ destruct (eval_addressing _ _ _ _); auto.
+ rewrite IHsv; clear IHsv. destruct (eval_smem _ _); auto.
+ rewrite IHsv1; auto.
+Qed.
+
+Lemma list_sval_eval_preserved lsv:
+ eval_list_sval (bctx1 ctx) lsv = eval_list_sval (bctx2 ctx) lsv.
+Proof.
+ induction lsv; simpl; auto.
+ rewrite eval_sval_preserved. destruct (eval_sval _ _); auto.
+ rewrite IHlsv; auto.
+Qed.
+
+Lemma smem_eval_preserved sm:
+ eval_smem (bctx1 ctx) sm = eval_smem (bctx2 ctx) sm.
+Proof.
+ induction sm; simpl; auto.
+ rewrite list_sval_eval_preserved. destruct (eval_list_sval _ _); auto.
+ erewrite eval_addressing_preserved; eauto.
+ destruct (eval_addressing _ _ _ _); auto.
+ rewrite IHsm; clear IHsm. destruct (eval_smem _ _); auto.
+ rewrite eval_sval_preserved; auto.
+Qed.
+
+Lemma eval_builtin_sval_preserved sv:
+ eval_builtin_sval (bctx1 ctx) sv = eval_builtin_sval (bctx2 ctx) sv.
+Proof.
+ induction sv; simpl; auto.
+ all: try (erewrite eval_sval_preserved by eauto); trivial.
+ all: erewrite IHsv1 by eauto; erewrite IHsv2 by eauto; reflexivity.
+Qed.
+
+Lemma eval_list_builtin_sval_preserved lsv:
+ eval_list_builtin_sval (bctx1 ctx) lsv = eval_list_builtin_sval (bctx2 ctx) lsv.
+Proof.
+ induction lsv; simpl; auto.
+ erewrite eval_builtin_sval_preserved by eauto.
+ erewrite IHlsv by eauto.
+ reflexivity.
+Qed.
+
+Lemma eval_scondition_preserved cond lsv:
+ eval_scondition (bctx1 ctx) cond lsv = eval_scondition (bctx2 ctx) cond lsv.
+Proof.
+ unfold eval_scondition.
+ rewrite list_sval_eval_preserved. destruct (eval_list_sval _ _); auto.
+Qed.
+
+(* additional preservation properties under this additional hypothesis *)
+Hypothesis senv_preserved_BTL: Senv.equiv (sge1 ctx) (sge2 ctx).
+
+Lemma senv_find_symbol_preserved id:
+ Senv.find_symbol (sge1 ctx) id = Senv.find_symbol (sge2 ctx) id.
+Proof.
+ destruct senv_preserved_BTL as (A & B & C). congruence.
+Qed.
+
+Lemma senv_symbol_address_preserved id ofs:
+ Senv.symbol_address (sge1 ctx) id ofs = Senv.symbol_address (sge2 ctx) id ofs.
+Proof.
+ unfold Senv.symbol_address. rewrite senv_find_symbol_preserved.
+ reflexivity.
+Qed.
+
+Lemma eval_builtin_sarg_preserved m: forall bs varg,
+ eval_builtin_sarg (bctx1 ctx) m bs varg ->
+ eval_builtin_sarg (bctx2 ctx) m bs varg.
+Proof.
+ induction 1; simpl.
+ all: try (constructor; auto).
+ - rewrite <- eval_sval_preserved. assumption.
+ - rewrite <- senv_symbol_address_preserved. assumption.
+ - rewrite senv_symbol_address_preserved. eapply seval_BA_addrglobal.
+Qed.
+
+Lemma eval_builtin_sargs_preserved m lbs vargs:
+ eval_builtin_sargs (bctx1 ctx) m lbs vargs ->
+ eval_builtin_sargs (bctx2 ctx) m lbs vargs.
+Proof.
+ induction 1; constructor; eauto.
+ eapply eval_builtin_sarg_preserved; auto.
+Qed.
+
+End SymbValPreserved.
+
diff --git a/scheduling/BTL_Scheduler.v b/scheduling/BTL_Scheduler.v
new file mode 100644
index 00000000..78c597e0
--- /dev/null
+++ b/scheduling/BTL_Scheduler.v
@@ -0,0 +1,176 @@
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad BTL.
+
+Require Import Errors Linking BTL_SEtheory BTL_SEimpl.
+
+(** External oracle *)
+Axiom scheduler: BTL.function -> BTL.code.
+
+Extract Constant scheduler => "BTLScheduleraux.btl_scheduler".
+
+Definition equiv_input_regs (f1 f2: BTL.function): Prop :=
+ (forall pc, (fn_code f1)!pc = None <-> (fn_code f2)!pc = None)
+ /\ (forall pc ib1 ib2, (fn_code f1)!pc = Some ib1 -> (fn_code f2)!pc = Some ib2 -> ib1.(input_regs) = ib2.(input_regs)).
+
+Lemma equiv_input_regs_union f1 f2:
+ equiv_input_regs f1 f2 -> forall tbl, union_inputs f1 tbl = union_inputs f2 tbl.
+Proof.
+ intros (EQNone & EQSome). induction tbl as [|pc l']; simpl; auto.
+ generalize (EQNone pc) (EQSome pc); clear EQNone EQSome; intros EQN EQS.
+ do 2 autodestruct; intuition; try_simplify_someHyps.
+ intros; exploit EQS; eauto; clear EQS. congruence.
+Qed.
+
+Lemma equiv_input_regs_pre f1 f2 tbl or:
+ equiv_input_regs f1 f2 -> pre_inputs f1 tbl or = pre_inputs f2 tbl or.
+Proof.
+ intros; unfold pre_inputs; erewrite equiv_input_regs_union; auto.
+Qed.
+
+Lemma equiv_input_regs_tr_inputs f1 f2 l oreg rs:
+ equiv_input_regs f1 f2 ->
+ tr_inputs f1 l oreg rs = tr_inputs f2 l oreg rs.
+Proof.
+ intros; unfold tr_inputs; erewrite equiv_input_regs_pre; eauto.
+Qed.
+
+(* a specification of the verification to do on each function *)
+Record match_function (f1 f2: BTL.function): Prop := {
+ preserv_fnsig: fn_sig f1 = fn_sig f2;
+ preserv_fnparams: fn_params f1 = fn_params f2;
+ preserv_fnstacksize: fn_stacksize f1 = fn_stacksize f2;
+ preserv_entrypoint: fn_entrypoint f1 = fn_entrypoint f2;
+ preserv_inputs: equiv_input_regs f1 f2;
+ symbolic_simu_ok: forall pc ib1, (fn_code f1)!pc = Some ib1 ->
+ exists ib2, (fn_code f2)!pc = Some ib2 /\ symbolic_simu f1 f2 (entry ib1) (entry ib2);
+}.
+
+Inductive match_fundef: fundef -> fundef -> Prop :=
+ | match_Internal f f': match_function f f' -> match_fundef (Internal f) (Internal f')
+ | match_External ef: match_fundef (External ef) (External ef).
+
+Inductive match_stackframes: stackframe -> stackframe -> Prop :=
+ | match_stackframe_intro
+ res f sp pc rs rs' f'
+ (EQREGS: forall r, rs # r = rs' # r)
+ (TRANSF: match_function f f')
+ : match_stackframes (BTL.Stackframe res f sp pc rs) (BTL.Stackframe res f' sp pc rs').
+
+Inductive match_states: state -> state -> Prop :=
+ | match_states_intro
+ st f sp pc rs rs' m st' f'
+ (EQREGS: forall r, rs # r = rs' # r)
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_function f f')
+ : match_states (State st f sp pc rs m) (State st' f' sp pc rs' m)
+ | match_states_call
+ st st' f f' args m
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_fundef f f')
+ : match_states (Callstate st f args m) (Callstate st' f' args m)
+ | match_states_return
+ st st' v m
+ (STACKS: list_forall2 match_stackframes st st')
+ : match_states (Returnstate st v m) (Returnstate st' v m)
+ .
+
+Lemma match_stack_equiv stk1 stk2:
+ list_forall2 match_stackframes stk1 stk2 ->
+ forall stk3, list_forall2 equiv_stackframe stk2 stk3 ->
+ list_forall2 match_stackframes stk1 stk3.
+Proof.
+ induction 1; intros stk3 EQ; inv EQ; constructor; eauto.
+ inv H3; inv H; econstructor; eauto.
+ intros; rewrite <- EQUIV; auto.
+Qed.
+
+Lemma match_states_equiv s1 s2 s3: match_states s1 s2 -> equiv_state s2 s3 -> match_states s1 s3.
+Proof.
+ Local Hint Resolve match_stack_equiv: core.
+ destruct 1; intros EQ; inv EQ; econstructor; eauto.
+ intros; rewrite <- EQUIV; auto.
+Qed.
+
+Local Open Scope error_monad_scope.
+
+Fixpoint check_symbolic_simu_rec (f1 f2: BTL.function) (lpc: list node): res unit :=
+ match lpc with
+ | nil => OK tt
+ | pc :: lpc' =>
+ match (fn_code f1)!pc, (fn_code f2)!pc with
+ | Some ibf1, Some ibf2 =>
+ do _ <- simu_check f1 f2 ibf1 ibf2;
+ check_symbolic_simu_rec f1 f2 lpc'
+ | _, _ => Error (msg "check_symbolic_simu_rec: code tree mismatch")
+ end
+ end.
+
+Definition erase_input_regs f1 f2 :=
+ let code := PTree.map (fun pc ibf =>
+ let oibf := match (fn_code f2)!pc with
+ | None => ibf
+ | Some oibf => oibf
+ end in
+ {| entry:= oibf.(entry); input_regs := ibf.(input_regs); binfo := oibf.(binfo) |}) (fn_code f1) in
+ BTL.mkfunction (fn_sig f2) (fn_params f2) (fn_stacksize f2) code (fn_entrypoint f2).
+
+Lemma erase_input_regs_correct f1 f2 f3:
+ erase_input_regs f1 f2 = f3 ->
+ equiv_input_regs f1 f3.
+Proof.
+ unfold erase_input_regs; simpl; intros.
+ unfold equiv_input_regs; intuition auto.
+ - subst; simpl. rewrite PTree.gmap, H0; simpl; auto.
+ - subst; simpl in H0. rewrite PTree.gmap in H0.
+ destruct ((fn_code f1)!pc) eqn:EQF; simpl in H0; inv H0; auto.
+ - subst; simpl in H1. rewrite PTree.gmap in H1.
+ rewrite H0 in H1; simpl in H1; inv H1; simpl; auto.
+Qed.
+Global Opaque erase_input_regs.
+
+Definition check_symbolic_simu (f1 f2: BTL.function): res unit :=
+ check_symbolic_simu_rec f1 f2 (List.map (fun elt => fst elt) (PTree.elements (fn_code f1))).
+
+Definition transf_function (f: BTL.function) :=
+ let tf := BTL.mkfunction (fn_sig f) (fn_params f) (fn_stacksize f) (scheduler f) (fn_entrypoint f) in
+ let tf' := erase_input_regs f tf in
+ do _ <- check_symbolic_simu f tf';
+ OK tf'.
+
+Lemma check_symbolic_simu_rec_correct lpc: forall f1 f2 x,
+ check_symbolic_simu_rec f1 f2 lpc = OK x ->
+ forall pc ib1, (fn_code f1)!pc = Some ib1 /\ In pc lpc ->
+ exists ib2, (fn_code f2)!pc = Some ib2 /\ symbolic_simu f1 f2 (entry ib1) (entry ib2).
+Proof.
+ induction lpc; simpl; intros f1 f2 x X pc ib1 (PC & HIN); try contradiction.
+ destruct HIN; subst.
+ - rewrite PC in X; destruct ((fn_code f2)!pc); monadInv X.
+ exists i; split; auto. destruct x0. eapply simu_check_correct; eauto.
+ - destruct ((fn_code f1)!a), ((fn_code f2)!a); monadInv X.
+ eapply IHlpc; eauto.
+Qed.
+
+Lemma check_symbolic_simu_correct x f1 f2:
+ check_symbolic_simu f1 f2 = OK x ->
+ forall pc ib1, (fn_code f1)!pc = Some ib1 ->
+ exists ib2, (fn_code f2)!pc = Some ib2 /\ symbolic_simu f1 f2 (entry ib1) (entry ib2).
+Proof.
+ unfold check_symbolic_simu; intros X pc ib1 PC.
+ eapply check_symbolic_simu_rec_correct; intuition eauto.
+ apply PTree.elements_correct in PC. eapply (in_map fst) in PC; auto.
+Qed.
+
+Lemma check_symbolic_simu_input_equiv x f1 f2:
+ transf_function f1 = OK f2 ->
+ check_symbolic_simu f1 f2 = OK x -> equiv_input_regs f1 f2.
+Proof.
+ unfold transf_function; intros TRANSF X; monadInv TRANSF.
+ exploit erase_input_regs_correct; eauto.
+Qed.
+
+Definition transf_fundef (f: fundef) : res fundef :=
+ transf_partial_fundef (fun f => transf_function f) f.
+
+Definition transf_program (p: program) : res program :=
+ transform_partial_program transf_fundef p.
diff --git a/scheduling/BTL_Schedulerproof.v b/scheduling/BTL_Schedulerproof.v
new file mode 100644
index 00000000..9a73d278
--- /dev/null
+++ b/scheduling/BTL_Schedulerproof.v
@@ -0,0 +1,434 @@
+Require Import AST Linking Values Maps Globalenvs Smallstep Registers.
+Require Import Coqlib Maps Events Errors Op OptionMonad.
+Require Import RTL BTL BTL_SEtheory.
+Require Import BTLmatchRTL BTL_Scheduler.
+
+Definition match_prog (p tp: program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+Section PRESERVATION.
+
+Variable prog: program.
+Variable tprog: program.
+
+Hypothesis TRANSL: match_prog prog tprog.
+
+Let pge := Genv.globalenv prog.
+Let tpge := Genv.globalenv tprog.
+
+Lemma symbols_preserved s: Genv.find_symbol tpge s = Genv.find_symbol pge s.
+Proof.
+ rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
+Qed.
+
+Lemma senv_preserved:
+ Senv.equiv pge tpge.
+Proof.
+ eapply (Genv.senv_match TRANSL).
+Qed.
+
+Lemma functions_preserved:
+ forall (v: val) (f: fundef),
+ Genv.find_funct pge v = Some f ->
+ exists tf cunit, transf_fundef f = OK tf /\ Genv.find_funct tpge v = Some tf /\ linkorder cunit prog.
+Proof.
+ intros. exploit (Genv.find_funct_match TRANSL); eauto.
+ intros (cu & tf & A & B & C).
+ repeat eexists; intuition eauto.
+ + unfold incl; auto.
+ + eapply linkorder_refl.
+Qed.
+
+Lemma transf_function_correct f f':
+ transf_function f = OK f' -> match_function f f'.
+Proof.
+ unfold transf_function. intros H.
+ assert (OH: transf_function f = OK f') by auto. monadInv H.
+ econstructor; eauto.
+ eapply check_symbolic_simu_input_equiv; eauto.
+ eapply check_symbolic_simu_correct; eauto.
+Qed.
+
+Lemma transf_fundef_correct f f':
+ transf_fundef f = OK f' -> match_fundef f f'.
+Proof.
+ intros TRANSF; destruct f; simpl; inv TRANSF.
+ + monadInv H0. exploit transf_function_correct; eauto.
+ constructor; auto.
+ + eapply match_External.
+Qed.
+
+Lemma function_ptr_preserved:
+ forall v f,
+ Genv.find_funct_ptr pge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tpge v = Some tf /\ transf_fundef f = OK tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
+Qed.
+
+Lemma function_sig_translated f tf: transf_fundef f = OK tf -> funsig tf = funsig f.
+Proof.
+ intros H; apply transf_fundef_correct in H; destruct H; simpl; eauto.
+ symmetry; erewrite preserv_fnsig; eauto.
+Qed.
+
+Theorem transf_initial_states:
+ forall s1, initial_state prog s1 ->
+ exists s2, initial_state tprog s2 /\ match_states s1 s2.
+Proof.
+ intros. inv H.
+ exploit function_ptr_preserved; eauto. intros (tf & FIND & TRANSF).
+ eexists. split.
+ - econstructor; eauto.
+ + intros; apply (Genv.init_mem_match TRANSL); eauto.
+ + replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main. eauto.
+ + erewrite function_sig_translated; eauto.
+ - constructor; eauto.
+ + constructor.
+ + apply transf_fundef_correct; auto.
+Qed.
+
+Lemma transf_final_states s1 s2 r:
+ match_states s1 s2 -> final_state s1 r -> final_state s2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Section SYMBOLIC_CTX.
+
+Variables f1 f2: function.
+Variable sp0: val.
+Variable rs0: regset.
+Variable m0: Memory.Mem.mem.
+
+Lemma symbols_preserved_rev s: Genv.find_symbol pge s = Genv.find_symbol tpge s.
+Proof.
+ symmetry; apply symbols_preserved.
+Qed.
+
+Let ctx := Sctx f1 f2 pge tpge symbols_preserved_rev sp0 rs0 m0.
+
+Lemma str_regs_equiv sfv1 sfv2 rs:
+ sfv_simu ctx sfv1 sfv2 ->
+ match_function f1 f2 ->
+ str_regs (cf (bctx1 ctx)) sfv1 rs = str_regs (cf (bctx2 ctx)) sfv2 rs.
+Proof.
+ intros SFV MF; inv MF.
+ inv SFV; simpl;
+ erewrite equiv_input_regs_tr_inputs; auto.
+Qed.
+
+Lemma sfind_function_preserved ros1 ros2 fd:
+ svident_simu ctx ros1 ros2 ->
+ sfind_function (bctx1 ctx) ros1 = Some fd ->
+ exists fd',
+ sfind_function (bctx2 ctx) ros2 = Some fd'
+ /\ transf_fundef fd = OK fd'.
+Proof.
+ unfold sfind_function. intros [sv1 sv2 SIMU|]; simpl in *.
+ + rewrite !SIMU. clear SIMU.
+ destruct (eval_sval _ _); try congruence.
+ intros; exploit functions_preserved; eauto.
+ intros (fd' & cunit & (X1 & X2 & X3)). eexists; eauto.
+ + subst. rewrite symbols_preserved. destruct (Genv.find_symbol _ _); try congruence.
+ intros; exploit function_ptr_preserved; eauto.
+Qed.
+
+Theorem symbolic_simu_ssem stk1 stk2 st st' s t:
+ sstate_simu ctx st st' ->
+ sem_sstate (bctx1 ctx) stk1 t s st ->
+ list_forall2 match_stackframes stk1 stk2 ->
+ match_function f1 f2 ->
+ exists s',
+ sem_sstate (bctx2 ctx) stk2 t s' st'
+ /\ match_states s s'.
+Proof.
+ intros SIMU SST1 STACKS MF. inversion MF.
+ exploit sem_sstate_run; eauto.
+ intros (sis1 & sfv1 & rs' & m' & SOUT1 & SIS1 & SF).
+ exploit sem_si_ok; eauto. intros SOK.
+ exploit SIMU; simpl; eauto.
+ intros (sis2 & sfv2 & SOUT2 & SSIMU & SFVSIM).
+ remember SIS1 as EQSFV. clear HeqEQSFV.
+ eapply SFVSIM in EQSFV.
+ exploit SSIMU; eauto. intros SIS2.
+ destruct SIS1 as (PRE1 & SMEM1 & SREGS1).
+ try_simplify_someHyps; intros.
+ set (EQSFV2:=sfv2).
+ inversion EQSFV; subst; inversion SF; subst.
+ - (* goto *)
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + erewrite <- str_regs_equiv; simpl; eauto.
+ + econstructor.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ erewrite equiv_input_regs_tr_inputs; eauto.
+ - (* call *)
+ exploit sfind_function_preserved; eauto. intros (fd' & SFIND & FUND).
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + erewrite <- str_regs_equiv; eauto.
+ + econstructor; eauto.
+ * apply function_sig_translated; auto.
+ * erewrite <- ARGS; eauto.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ * erewrite equiv_input_regs_tr_inputs; eauto.
+ repeat (econstructor; eauto).
+ * apply transf_fundef_correct; auto.
+ - (* tailcall *)
+ exploit sfind_function_preserved; eauto. intros (fd' & SFIND & FUND).
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + econstructor; eauto.
+ * apply function_sig_translated; auto.
+ * simpl; erewrite <- preserv_fnstacksize; eauto.
+ * erewrite <- ARGS; eauto.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ apply transf_fundef_correct; auto.
+ - (* builtin *)
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + erewrite <- str_regs_equiv; simpl; eauto.
+ + econstructor.
+ * eapply eval_builtin_sargs_preserved.
+ eapply senv_preserved.
+ eapply eval_list_builtin_sval_correct; eauto.
+ inv BARGS.
+ erewrite <- eval_list_builtin_sval_preserved in H0; auto.
+ * simpl in *; eapply external_call_symbols_preserved; eauto.
+ eapply senv_preserved.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ erewrite equiv_input_regs_tr_inputs; eauto.
+ - (* jumptable *)
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + erewrite <- str_regs_equiv; simpl; eauto.
+ + econstructor; eauto.
+ rewrite <- VAL; auto.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ erewrite equiv_input_regs_tr_inputs; eauto.
+ - (* return *)
+ exploit (run_sem_sstate (bctx2 ctx) st' sis2 EQSFV2); eauto.
+ + econstructor; eauto.
+ * simpl; erewrite <- preserv_fnstacksize; eauto.
+ * inv OPT; eauto.
+ rewrite <- SIMU0; auto.
+ + intros SST2. eexists; split; eauto.
+ econstructor; simpl; eauto.
+ Unshelve. all: auto.
+Qed.
+
+Theorem symbolic_simu_correct stk1 stk2 ibf1 ibf2 s t:
+ sstate_simu ctx (sexec f1 ibf1.(entry)) (sexec f2 ibf2.(entry)) ->
+ iblock_step tr_inputs (sge1 ctx) stk1 f1 (ssp ctx) (srs0 ctx) (sm0 ctx) ibf1.(entry) t s ->
+ list_forall2 match_stackframes stk1 stk2 ->
+ match_function f1 f2 ->
+ exists s',
+ iblock_step tr_inputs (sge2 ctx) stk2 f2 (ssp ctx) (srs0 ctx) (sm0 ctx) ibf2.(entry) t s'
+ /\ match_states s s'.
+Proof.
+ unfold sstate_simu.
+ intros SIMU STEP1 STACKS MF.
+ exploit (sexec_correct (bctx1 ctx)); simpl; eauto.
+ intros SST1. exploit symbolic_simu_ssem; eauto.
+ intros (s2 & SST2 & MS).
+ exploit (sexec_exact (bctx2 ctx)); simpl; eauto.
+ intros (s3 & STEP & EQUIV).
+ exists s3. intuition eauto.
+ eapply match_states_equiv; eauto.
+Qed.
+
+End SYMBOLIC_CTX.
+
+Lemma tr_inputs_eqregs_list f tbl: forall rs rs' r' ores,
+ (forall r : positive, rs # r = rs' # r) ->
+ (tr_inputs f tbl ores rs) # r' =
+ (tr_inputs f tbl ores rs') # r'.
+Proof.
+ induction tbl as [|pc tbl IHtbl];
+ intros; destruct ores; simpl;
+ rewrite !tr_inputs_get; autodestruct.
+Qed.
+
+Lemma find_function_eqregs rs rs' ros fd:
+ (forall r : positive, rs # r = rs' # r) ->
+ find_function tpge ros rs = Some fd ->
+ find_function tpge ros rs' = Some fd.
+Proof.
+ intros EQREGS; destruct ros; simpl.
+ rewrite EQREGS; auto.
+ autodestruct.
+Qed.
+
+Lemma args_eqregs (args: list reg): forall (rs rs': regset),
+ (forall r : positive, rs # r = rs' # r) ->
+ rs ## args = rs' ## args.
+Proof.
+ induction args; simpl; trivial.
+ intros rs rs' EQREGS; rewrite EQREGS.
+ apply f_equal; eauto.
+Qed.
+
+Lemma f_arg_eqregs (rs rs': regset):
+ (forall r : positive, rs # r = rs' # r) ->
+ (fun r : positive => rs # r) = (fun r : positive => rs' # r).
+Proof.
+ intros EQREGS; apply functional_extensionality; eauto.
+Qed.
+
+Lemma eqregs_update (rs rs': regset) v dest:
+ (forall r, rs # r = rs' # r) ->
+ (forall r, (rs # dest <- v) # r = (rs' # dest <- v) # r).
+Proof.
+ intros EQREGS r; destruct (Pos.eq_dec dest r); subst.
+ * rewrite !Regmap.gss; reflexivity.
+ * rewrite !Regmap.gso; auto.
+Qed.
+
+Local Hint Resolve equiv_stack_refl tr_inputs_eqregs_list find_function_eqregs eqregs_update: core.
+
+Lemma iblock_istep_eqregs_None sp ib: forall rs m rs' rs1 m1
+ (EQREGS: forall r, rs # r = rs' # r)
+ (ISTEP: iblock_istep tpge sp rs m ib rs1 m1 None),
+ exists rs1',
+ iblock_istep tpge sp rs' m ib rs1' m1 None
+ /\ (forall r, rs1 # r = rs1' # r).
+Proof.
+ induction ib; intros; inv ISTEP.
+ - repeat econstructor; eauto.
+ - repeat econstructor; eauto.
+ erewrite args_eqregs; eauto.
+ - inv LOAD; repeat econstructor; eauto;
+ erewrite args_eqregs; eauto.
+ - repeat econstructor; eauto.
+ erewrite args_eqregs; eauto.
+ rewrite <- EQREGS; eauto.
+ - exploit IHib1; eauto.
+ intros (rs1' & ISTEP1 & EQREGS1).
+ exploit IHib2; eauto.
+ intros (rs2' & ISTEP2 & EQREGS2).
+ eexists; split. econstructor; eauto.
+ eauto.
+ - destruct b.
+ 1: exploit IHib1; eauto.
+ 2: exploit IHib2; eauto.
+ all:
+ intros (rs1' & ISTEP & EQREGS');
+ eexists; split; try eapply exec_cond;
+ try erewrite args_eqregs; eauto.
+Qed.
+
+Lemma iblock_step_eqregs stk sp f ib: forall rs rs' m t s
+ (EQREGS: forall r, rs # r = rs' # r)
+ (STEP: iblock_step tr_inputs tpge stk f sp rs m ib t s),
+ exists s',
+ iblock_step tr_inputs tpge stk f sp rs' m ib t s'
+ /\ equiv_state s s'.
+Proof.
+ induction ib; intros;
+ destruct STEP as (rs2 & m2 & fin & ISTEP & FSTEP); inv ISTEP.
+ - inv FSTEP;
+ eexists; split; repeat econstructor; eauto.
+ + destruct (or); simpl; try rewrite EQREGS; constructor; eauto.
+ + erewrite args_eqregs; eauto. repeat econstructor; eauto.
+ + erewrite args_eqregs; eauto. econstructor; eauto.
+ + erewrite f_arg_eqregs; eauto.
+ + destruct res; simpl; eauto.
+ + rewrite <- EQREGS; auto.
+ - exploit IHib1; eauto.
+ + econstructor. do 2 eexists; split; eauto.
+ + intros (s' & STEP & EQUIV). clear FSTEP.
+ destruct STEP as (rs3 & m3 & fin2 & ISTEP & FSTEP).
+ repeat (econstructor; eauto).
+ - exploit iblock_istep_eqregs_None; eauto.
+ intros (rs1' & ISTEP1 & EQREGS1).
+ exploit IHib2; eauto.
+ + econstructor. do 2 eexists; split; eauto.
+ + intros (s' & STEP & EQUIV). clear FSTEP.
+ destruct STEP as (rs3 & m3 & fin2 & ISTEP & FSTEP).
+ eexists; split; eauto.
+ econstructor; do 2 eexists; split; eauto.
+ eapply exec_seq_continue; eauto.
+ - destruct b.
+ 1: exploit IHib1; eauto.
+ 3: exploit IHib2; eauto.
+ 1,3: econstructor; do 2 eexists; split; eauto.
+ all:
+ intros (s' & STEP & EQUIV); clear FSTEP;
+ destruct STEP as (rs3 & m3 & fin2 & ISTEP & FSTEP);
+ eexists; split; eauto; econstructor;
+ do 2 eexists; split; eauto;
+ eapply exec_cond; try erewrite args_eqregs; eauto.
+Qed.
+
+Lemma iblock_step_simulation stk1 stk2 f1 f2 sp rs rs' m ibf t s1 pc
+ (STEP: iblock_step tr_inputs pge stk1 f1 sp rs m ibf.(entry) t s1)
+ (PC: (fn_code f1) ! pc = Some ibf)
+ (EQREGS: forall r : positive, rs # r = rs' # r)
+ (STACKS: list_forall2 match_stackframes stk1 stk2)
+ (TRANSF: match_function f1 f2)
+ :exists ibf' s2,
+ (fn_code f2) ! pc = Some ibf'
+ /\ iblock_step tr_inputs tpge stk2 f2 sp rs' m ibf'.(entry) t s2
+ /\ match_states s1 s2.
+Proof.
+ exploit symbolic_simu_ok; eauto. intros (ib2 & PC' & SYMBS).
+ exploit symbolic_simu_correct; repeat (eauto; simpl).
+ intros (s2 & STEP2 & MS).
+ exploit iblock_step_eqregs; eauto. intros (s3 & STEP3 & EQUIV).
+ clear STEP2. exists ib2; exists s3. repeat split; auto.
+ eapply match_states_equiv; eauto.
+Qed.
+
+Local Hint Constructors step: core.
+
+Theorem step_simulation s1 s1' t s2
+ (STEP: step tr_inputs pge s1 t s1')
+ (MS: match_states s1 s2)
+ :exists s2',
+ step tr_inputs tpge s2 t s2'
+ /\ match_states s1' s2'.
+Proof.
+ destruct STEP as [stack ibf f sp n rs m t s PC STEP | | | ]; inv MS.
+ - (* iblock *)
+ simplify_someHyps. intros PC.
+ exploit iblock_step_simulation; eauto.
+ intros (ibf' & s2 & PC2 & STEP2 & MS2).
+ eexists; split; eauto.
+ - (* function internal *)
+ inversion TRANSF as [xf tf MF |]; subst.
+ eexists; split.
+ + econstructor. erewrite <- preserv_fnstacksize; eauto.
+ + erewrite preserv_fnparams; eauto.
+ erewrite preserv_entrypoint; eauto.
+ econstructor; eauto.
+ - (* function external *)
+ inv TRANSF. eexists; split; econstructor; eauto.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ - (* return *)
+ inv STACKS. destruct b1 as [res' f' sp' pc' rs'].
+ eexists; split.
+ + econstructor.
+ + inv H1. econstructor; eauto.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (fsem prog) (fsem tprog).
+Proof.
+ eapply forward_simulation_step with match_states; simpl; eauto. (* lock-step with respect to match_states *)
+ - eapply senv_preserved.
+ - eapply transf_initial_states.
+ - eapply transf_final_states.
+ - intros; eapply step_simulation; eauto.
+Qed.
+
+End PRESERVATION.
diff --git a/scheduling/BTLcommonaux.ml b/scheduling/BTLcommonaux.ml
new file mode 100644
index 00000000..11a7ee7f
--- /dev/null
+++ b/scheduling/BTLcommonaux.ml
@@ -0,0 +1,87 @@
+open Maps
+open BTL
+open Registers
+open BTLtypes
+open RTLcommonaux
+
+let undef_node = -1
+
+let mk_iinfo _inumb _opt_info = { inumb = _inumb; opt_info = _opt_info; visited = false; liveins = Regset.empty }
+
+let def_iinfo () = { inumb = undef_node; opt_info = None; visited = false; liveins = Regset.empty }
+
+let mk_binfo _bnumb _s_output_regs _typing =
+ {
+ bnumb = _bnumb;
+ visited = false;
+ s_output_regs = _s_output_regs;
+ typing = _typing;
+ }
+
+let reset_visited_ibf btl =
+ PTree.map
+ (fun n ibf ->
+ ibf.binfo.visited <- false;
+ ibf)
+ btl
+
+let reset_visited_ib btl =
+ List.iter
+ (fun (n, ibf) ->
+ let ib = ibf.entry in
+ let rec reset_visited_ib_rec ib =
+ match ib with
+ | Bseq (ib1, ib2) ->
+ reset_visited_ib_rec ib1;
+ reset_visited_ib_rec ib2
+ | Bcond (_, _, ib1, ib2, iinfo) ->
+ reset_visited_ib_rec ib1;
+ reset_visited_ib_rec ib2;
+ iinfo.visited <- false
+ | Bnop (Some iinfo)
+ | Bop (_, _, _, iinfo)
+ | Bload (_, _, _, _, _, iinfo)
+ | Bstore (_, _, _, _, iinfo)
+ | BF (_, iinfo) ->
+ iinfo.visited <- false
+ | _ -> ()
+ in
+ reset_visited_ib_rec ib)
+ (PTree.elements btl);
+ btl
+
+let jump_visit = function
+ | Bcond (_, _, _, _, iinfo)
+ | Bnop (Some iinfo)
+ | Bop (_, _, _, iinfo)
+ | Bload (_, _, _, _, _, iinfo)
+ | Bstore (_, _, _, _, iinfo)
+ | BF (_, iinfo) ->
+ if iinfo.visited then true
+ else (
+ iinfo.visited <- true;
+ false)
+ | Bseq (_, _) -> false
+ | Bnop None -> true
+
+let rec get_inumb_or_next = function
+ | BF (Bgoto s, _) -> p2i s
+ | BF (_, iinfo)
+ | Bnop (Some iinfo)
+ | Bop (_, _, _, iinfo)
+ | Bload (_, _, _, _, _, iinfo)
+ | Bstore (_, _, _, _, iinfo)
+ | Bcond (_, _, _, _, iinfo) ->
+ iinfo.inumb
+ | Bseq (ib1, _) -> get_inumb_or_next ib1
+ | _ -> failwith "get_inumb_or_next: Bnop None"
+
+let get_liveins = function
+ | BF (_, iinfo)
+ | Bnop (Some iinfo)
+ | Bop (_, _, _, iinfo)
+ | Bload (_, _, _, _, _, iinfo)
+ | Bstore (_, _, _, _, iinfo)
+ | Bcond (_, _, _, _, iinfo) ->
+ iinfo.liveins
+ | _ -> failwith "get_liveins: invalid iblock"
diff --git a/scheduling/BTLmatchRTL.v b/scheduling/BTLmatchRTL.v
new file mode 100644
index 00000000..07131893
--- /dev/null
+++ b/scheduling/BTLmatchRTL.v
@@ -0,0 +1,606 @@
+(** General notions about the bisimulation BTL <-> RTL.
+
+This bisimulation is proved on the "CFG semantics" of BTL called [cfgsem] defined below,
+such that the full state of registers is preserved when exiting blocks.
+
+*)
+
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad Lia.
+Require Export BTL.
+
+(* tid = transfer_identity *)
+Definition tid (_:function) (_:list exit) (_: option reg) (rs:regset) := rs.
+
+Definition cfgsem (p: program) :=
+ Semantics (step tid) (initial_state p) final_state (Genv.globalenv p).
+
+(* * Simulation of BTL fsem by BTL cfgsem: a lock-step less-def simulation.
+*)
+
+
+Lemma tr_inputs_lessdef f rs1 rs2 tbl or r
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ :Val.lessdef (tr_inputs f tbl or rs1)#r (tid f tbl or rs2)#r.
+Proof.
+ unfold tid; rewrite tr_inputs_get.
+ autodestruct.
+Qed.
+
+Local Hint Resolve tr_inputs_lessdef init_regs_lessdef_preserv find_function_lessdef regs_lessdef_regs
+ lessdef_state_refl: core.
+Local Hint Unfold regs_lessdef: core.
+
+Lemma fsem2cfgsem_ibistep_simu ge sp rs1 m1 rs1' m1' of ib
+ (ISTEP: iblock_istep ge sp rs1 m1 ib rs1' m1' of): forall
+ rs2 m2
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ (MEMS: Mem.extends m1 m2),
+ exists rs2' m2', iblock_istep_run ge sp ib rs2 m2 = Some (out rs2' m2' of)
+ /\ (forall r, Val.lessdef rs1'#r rs2'#r)
+ /\ (Mem.extends m1' m2').
+Proof.
+ induction ISTEP; simpl; try_simplify_someHyps; intros.
+ - (* Bop *)
+ exploit (@eval_operation_lessdef _ _ ge sp op (rs ## args)); eauto.
+ intros (v1 & EVAL' & LESSDEF).
+ do 2 eexists; rewrite EVAL'. repeat (split; eauto).
+ eapply set_reg_lessdef; eauto.
+ - (* Bload *)
+ inv LOAD.
+ + exploit (@eval_addressing_lessdef _ _ ge sp addr (rs ## args)); eauto.
+ intros (v2 & EVAL' & LESSDEF). exploit Mem.loadv_extends; eauto.
+ intros (v3 & LOAD' & LESSDEF'); autodestruct;
+ do 2 eexists; rewrite EVAL', LOAD';
+ repeat (split; eauto); eapply set_reg_lessdef; eauto.
+ + destruct (eval_addressing ge sp addr rs ## args) eqn:EQA;
+ repeat autodestruct; do 2 eexists;
+ repeat (split; eauto); eapply set_reg_lessdef; eauto.
+ - (* Bstore *)
+ exploit (@eval_addressing_lessdef _ _ ge sp addr (rs ## args)); eauto.
+ intros (v2 & EVAL' & LESSDEF). exploit Mem.storev_extends; eauto.
+ intros (v3 & STORE' & LESSDEF').
+ do 2 eexists; rewrite EVAL', STORE'. repeat (split; eauto).
+ - (* Bseq stop *)
+ exploit IHISTEP; eauto. intros (rs2' & m2' & IBIS & REGS' & MEMS').
+ destruct (iblock_istep_run _ _ _ _ _); try congruence.
+ destruct o, _fin; simpl in *; try congruence. eauto.
+ - (* Bseq continue *)
+ exploit IHISTEP1; eauto.
+ clear ISTEP1 REGS MEMS.
+ intros (rs3 & m3 & ISTEP3 & REGS3 & MEMS3).
+ rewrite ISTEP3; simpl. rewrite iblock_istep_run_equiv in ISTEP2.
+ exploit IHISTEP2; eauto.
+ - (* Bcond *)
+ erewrite (@eval_condition_lessdef cond (rs ## args)); eauto.
+Qed.
+
+Local Hint Constructors lessdef_stackframe lessdef_state final_step list_forall2 step: core.
+
+Lemma fsem2cfgsem_finalstep_simu ge stk1 f sp rs1 m1 fin t s1 stk2 rs2 m2
+ (FSTEP: final_step tr_inputs ge stk1 f sp rs1 m1 fin t s1)
+ (STACKS: list_forall2 lessdef_stackframe stk1 stk2)
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ (MEMS: Mem.extends m1 m2)
+ : exists s2, final_step tid ge stk2 f sp rs2 m2 fin t s2
+ /\ lessdef_state s1 s2.
+Proof.
+ destruct FSTEP; try (eexists; split; simpl; econstructor; eauto; fail).
+ - (* return *)
+ exploit Mem.free_parallel_extends; eauto.
+ intros (m2' & FREE & MEMS2).
+ eexists; split; simpl; econstructor; eauto.
+ destruct or; simpl; auto.
+ - (* tailcall *)
+ exploit Mem.free_parallel_extends; eauto.
+ intros (m2' & FREE & MEMS2).
+ eexists; split; simpl; econstructor; eauto.
+ - (* builtin *)
+ exploit (eval_builtin_args_lessdef (ge:=ge) (e1:=(fun r => rs1 # r)) (fun r => rs2 # r)); eauto.
+ intros (vl2' & BARGS & VARGS).
+ exploit external_call_mem_extends; eauto.
+ intros (vres' & m2' & CALL2 & REGS2 & MEMS2 & UNCHANGED).
+ eexists; split; simpl; econstructor; eauto.
+ intros; apply set_res_lessdef; eauto.
+ - (* jumptable *)
+ eexists; split; simpl; econstructor; eauto.
+ destruct (REGS arg); try congruence.
+Qed.
+
+Lemma fsem2cfgsem_ibstep_simu ge stk1 stk2 f sp rs1 m1 ib t s1 rs2 m2
+ (STEP: iblock_step tr_inputs ge stk1 f sp rs1 m1 ib t s1)
+ (STACKS : list_forall2 lessdef_stackframe stk1 stk2)
+ (REGS: forall r, Val.lessdef rs1#r rs2#r)
+ (MEMS : Mem.extends m1 m2)
+ : exists s2, iblock_step tid ge stk2 f sp rs2 m2 ib t s2
+ /\ lessdef_state s1 s2.
+Proof.
+ destruct STEP as (rs1' & m1' & fin & ISTEP & FSTEP).
+ exploit fsem2cfgsem_ibistep_simu; eauto.
+ intros (rs2' & m2' & ISTEP2 & REGS2 & MEMS2).
+ rewrite <- iblock_istep_run_equiv in ISTEP2. clear ISTEP REGS MEMS.
+ exploit fsem2cfgsem_finalstep_simu; eauto.
+ intros (s2 & FSTEP2 & LESSDEF). clear FSTEP.
+ unfold iblock_step; eexists; split; eauto.
+Qed.
+
+Local Hint Constructors step: core.
+
+Lemma fsem2cfgsem_step_simu ge s1 t s1' s2
+ (STEP: step tr_inputs ge s1 t s1')
+ (REGS: lessdef_state s1 s2)
+ :exists s2' : state,
+ step tid ge s2 t s2' /\
+ lessdef_state s1' s2'.
+Proof.
+ destruct STEP; inv REGS.
+ - (* iblock *)
+ intros; exploit fsem2cfgsem_ibstep_simu; eauto. clear STEP.
+ intros (s2 & STEP2 & REGS2).
+ eexists; split; eauto.
+ - (* internal call *)
+ exploit (Mem.alloc_extends m m2 0 (fn_stacksize f) stk m' 0 (fn_stacksize f)); eauto.
+ 1-2: lia.
+ intros (m2' & ALLOC2 & MEMS2). clear ALLOC MEMS.
+ eexists; split; econstructor; eauto.
+ - (* external call *)
+ exploit external_call_mem_extends; eauto.
+ intros (vres' & m2' & CALL2 & REGS2 & MEMS2 & UNCHANGED). clear EXTCALL MEMS.
+ eexists; split; econstructor; eauto.
+ - (* return *)
+ inversion STACKS as [|d1 d2 d3 d4 STF2 STK2]. subst.
+ inv STF2.
+ eexists; split; econstructor; eauto.
+ intros; eapply set_reg_lessdef; eauto.
+Qed.
+
+Theorem fsem2cfgsem prog:
+ forward_simulation (fsem prog) (cfgsem prog).
+Proof.
+ eapply forward_simulation_step with lessdef_state; simpl; auto.
+ - destruct 1; intros; eexists; intuition eauto. econstructor; eauto.
+ - intros s1 s2 r REGS FINAL; destruct FINAL.
+ inv REGS; inv STACKS; inv REGS0.
+ econstructor; eauto.
+ - intros; eapply fsem2cfgsem_step_simu; eauto.
+Qed.
+
+(** * Matching BTL (for cfgsem) and RTL code
+
+We define a single verifier able to prove a bisimulation between BTL and RTL code.
+
+Hence, in a sense, our verifier imitates the approach of Duplicate, where [dupmap] maps the BTL nodes to the RTL nodes.
+
+The [match_function] definition gives a "relational" specification of the verifier...
+*)
+
+Require Import Errors.
+
+Inductive match_final_inst (dupmap: PTree.t node): final -> instruction -> Prop :=
+ | mfi_return or: match_final_inst dupmap (Breturn or) (Ireturn or)
+ | mfi_call pc pc' s ri lr r:
+ dupmap!pc = (Some pc') -> match_final_inst dupmap (Bcall s ri lr r pc) (Icall s ri lr r pc')
+ | mfi_tailcall s ri lr:
+ match_final_inst dupmap (Btailcall s ri lr) (Itailcall s ri lr)
+ | mfi_builtin pc pc' ef la br:
+ dupmap!pc = (Some pc') -> match_final_inst dupmap (Bbuiltin ef la br pc) (Ibuiltin ef la br pc')
+ | mfi_jumptable ln ln' r:
+ list_forall2 (fun pc pc' => (dupmap!pc = (Some pc'))) ln ln' ->
+ match_final_inst dupmap (Bjumptable r ln) (Ijumptable r ln')
+.
+
+Inductive is_join_opt {A}: (option A) -> (option A) -> (option A) -> Prop :=
+ | ijo_None_left o: is_join_opt None o o
+ | ijo_None_right o: is_join_opt o None o
+ | ijo_Some x: is_join_opt (Some x) (Some x) (Some x)
+ .
+
+(* [match_iblock dupmap cfg isfst pc ib opc] means that [ib] match a block in a RTL code starting at [pc], with:
+ - [isfst] (in "input") indicates that no step in the surrounding block has been executed before entering [pc]
+ - if [opc] (in "ouput") is [None], this means that all branches of the block ends on a final instruction
+ - if [opc] is [Some pc'], this means that all branches of the block that do not exit, join on [pc'].
+*)
+Inductive match_iblock (dupmap: PTree.t node) (cfg: RTL.code): bool -> node -> iblock -> (option node) -> Prop :=
+ | mib_BF isfst fi pc i iinfo:
+ cfg!pc = Some i ->
+ match_final_inst dupmap fi i ->
+ match_iblock dupmap cfg isfst pc (BF fi iinfo) None
+ | mib_nop_on_rtl isfst pc pc' iinfo:
+ cfg!pc = Some (Inop pc') ->
+ match_iblock dupmap cfg isfst pc (Bnop (Some iinfo)) (Some pc')
+ | mib_nop_skip pc:
+ match_iblock dupmap cfg false pc (Bnop None) (Some pc)
+ | mib_op isfst pc pc' op lr r iinfo:
+ cfg!pc = Some (Iop op lr r pc') ->
+ match_iblock dupmap cfg isfst pc (Bop op lr r iinfo) (Some pc')
+ | mib_load isfst pc pc' trap m a lr r iinfo:
+ cfg!pc = Some (Iload trap m a lr r pc') ->
+ match_iblock dupmap cfg isfst pc (Bload trap m a lr r iinfo) (Some pc')
+ | mib_store isfst pc pc' m a lr r iinfo:
+ cfg!pc = Some (Istore m a lr r pc') ->
+ match_iblock dupmap cfg isfst pc (Bstore m a lr r iinfo) (Some pc')
+ | mib_exit pc pc' iinfo:
+ dupmap!pc = (Some pc') ->
+ match_iblock dupmap cfg false pc' (BF (Bgoto pc) iinfo) None
+ (* NB: on RTL side, we exit the block by a "basic" instruction (or Icond).
+ Thus some step should have been executed before [pc'] in the RTL code *)
+ | mib_seq_Some isfst b1 b2 pc1 pc2 opc:
+ match_iblock dupmap cfg isfst pc1 b1 (Some pc2) ->
+ match_iblock dupmap cfg false pc2 b2 opc ->
+ match_iblock dupmap cfg isfst pc1 (Bseq b1 b2) opc
+(* | mib_seq_None isfst b1 b2 pc:
+ match_iblock dupmap cfg isfst pc b1 None ->
+ match_iblock dupmap cfg isfst (Bseq b1 b2) pc None
+ (* here [b2] is dead code ! Our verifier rejects such dead code!
+ *)
+*)
+ | mib_cond isfst c lr bso bnot pcso pcnot pc opc1 opc2 opc i iinfo:
+ cfg!pc = Some (Icond c lr pcso pcnot i) ->
+ match_iblock dupmap cfg false pcso bso opc1 ->
+ match_iblock dupmap cfg false pcnot bnot opc2 ->
+ is_join_opt opc1 opc2 opc ->
+ match_iblock dupmap cfg isfst pc (Bcond c lr bso bnot iinfo) opc
+ .
+
+Definition match_cfg dupmap (cfg: BTL.code) (cfg':RTL.code): Prop :=
+ forall pc pc', dupmap!pc = Some pc' ->
+ exists ib, cfg!pc = Some ib /\ match_iblock dupmap cfg' true pc' ib.(entry) None.
+
+Record match_function dupmap f f': Prop := {
+ dupmap_correct: match_cfg dupmap (BTL.fn_code f) (RTL.fn_code f');
+ dupmap_entrypoint: dupmap!(fn_entrypoint f) = Some (RTL.fn_entrypoint f');
+ preserv_fnsig: fn_sig f = RTL.fn_sig f';
+ preserv_fnparams: fn_params f = RTL.fn_params f';
+ preserv_fnstacksize: fn_stacksize f = RTL.fn_stacksize f'
+}.
+
+(** * Shared verifier between RTL -> BTL and BTL -> RTL *)
+
+Local Open Scope error_monad_scope.
+
+Definition verify_is_copy dupmap n n' :=
+ match dupmap!n with
+ | None => Error(msg "BTL.verify_is_copy None")
+ | Some revn => match (Pos.compare n' revn) with Eq => OK tt | _ => Error(msg "BTL.verify_is_copy invalid map") end
+ end.
+
+Fixpoint verify_is_copy_list dupmap ln ln' :=
+ match ln with
+ | n::ln => match ln' with
+ | n'::ln' => do _ <- verify_is_copy dupmap n n';
+ verify_is_copy_list dupmap ln ln'
+ | nil => Error (msg "BTL.verify_is_copy_list: ln' bigger than ln") end
+ | nil => match ln' with
+ | n :: ln' => Error (msg "BTL.verify_is_copy_list: ln bigger than ln'")
+ | nil => OK tt end
+ end.
+
+Lemma verify_is_copy_correct dupmap n n' tt:
+ verify_is_copy dupmap n n' = OK tt ->
+ dupmap ! n = Some n'.
+Proof.
+ unfold verify_is_copy; repeat autodestruct.
+ intros NP H; destruct (_ ! n) eqn:REVM; [|inversion H].
+ eapply Pos.compare_eq in NP. congruence.
+Qed.
+Local Hint Resolve verify_is_copy_correct: core.
+
+Lemma verify_is_copy_list_correct dupmap ln: forall ln' tt,
+ verify_is_copy_list dupmap ln ln' = OK tt ->
+ list_forall2 (fun n n' => dupmap ! n = Some n') ln ln'.
+Proof.
+ induction ln.
+ - intros. destruct ln'; monadInv H. constructor.
+ - intros. destruct ln'; monadInv H. constructor; eauto.
+Qed.
+
+(* TODO Copied from duplicate, should we import ? *)
+Lemma product_eq {A B: Type} :
+ (forall (a b: A), {a=b} + {a<>b}) ->
+ (forall (c d: B), {c=d} + {c<>d}) ->
+ forall (x y: A+B), {x=y} + {x<>y}.
+Proof.
+ intros H H'. intros. decide equality.
+Qed.
+
+(* TODO Copied from duplicate, should we import ? *)
+(** FIXME Ideally i would like to put this in AST.v but i get an "illegal application"
+ * error when doing so *)
+Remark builtin_arg_eq_pos: forall (a b: builtin_arg positive), {a=b} + {a<>b}.
+Proof.
+ intros.
+ apply (builtin_arg_eq Pos.eq_dec).
+Defined.
+Global Opaque builtin_arg_eq_pos.
+
+(* TODO Copied from duplicate, should we import ? *)
+Remark builtin_res_eq_pos: forall (a b: builtin_res positive), {a=b} + {a<>b}.
+Proof. intros. apply (builtin_res_eq Pos.eq_dec). Qed.
+Global Opaque builtin_res_eq_pos.
+
+Fixpoint verify_block (dupmap: PTree.t node) cfg isfst pc ib : res (option node) :=
+ match ib with
+ | BF fi _ =>
+ match fi with
+ | Bgoto pc1 =>
+ do u <- verify_is_copy dupmap pc1 pc;
+ if negb isfst then
+ OK None
+ else Error (msg "BTL.verify_block: isfst is true Bgoto")
+ | Breturn or =>
+ match cfg!pc with
+ | Some (Ireturn or') =>
+ if option_eq Pos.eq_dec or or' then OK None
+ else Error (msg "BTL.verify_block: different opt reg in Breturn")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Breturn")
+ end
+ | Bcall s ri lr r pc1 =>
+ match cfg!pc with
+ | Some (Icall s' ri' lr' r' pc2) =>
+ do u <- verify_is_copy dupmap pc1 pc2;
+ if (signature_eq s s') then
+ if (product_eq Pos.eq_dec ident_eq ri ri') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then OK None
+ else Error (msg "BTL.verify_block: different r r' in Bcall")
+ else Error (msg "BTL.verify_block: different lr in Bcall")
+ else Error (msg "BTL.verify_block: different ri in Bcall")
+ else Error (msg "BTL.verify_block: different signatures in Bcall")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bcall")
+ end
+ | Btailcall s ri lr =>
+ match cfg!pc with
+ | Some (Itailcall s' ri' lr') =>
+ if (signature_eq s s') then
+ if (product_eq Pos.eq_dec ident_eq ri ri') then
+ if (list_eq_dec Pos.eq_dec lr lr') then OK None
+ else Error (msg "BTL.verify_block: different lr in Btailcall")
+ else Error (msg "BTL.verify_block: different ri in Btailcall")
+ else Error (msg "BTL.verify_block: different signatures in Btailcall")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Btailcall")
+ end
+ | Bbuiltin ef la br pc1 =>
+ match cfg!pc with
+ | Some (Ibuiltin ef' la' br' pc2) =>
+ do u <- verify_is_copy dupmap pc1 pc2;
+ if (external_function_eq ef ef') then
+ if (list_eq_dec builtin_arg_eq_pos la la') then
+ if (builtin_res_eq_pos br br') then OK None
+ else Error (msg "BTL.verify_block: different brr in Bbuiltin")
+ else Error (msg "BTL.verify_block: different lbar in Bbuiltin")
+ else Error (msg "BTL.verify_block: different ef in Bbuiltin")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bbuiltin")
+ end
+ | Bjumptable r ln =>
+ match cfg!pc with
+ | Some (Ijumptable r' ln') =>
+ do u <- verify_is_copy_list dupmap ln ln';
+ if (Pos.eq_dec r r') then OK None
+ else Error (msg "BTL.verify_block: different r in Bjumptable")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bjumptable")
+ end
+ end
+ | Bnop oiinfo =>
+ match oiinfo with
+ | Some _ =>
+ match cfg!pc with
+ | Some (Inop pc') => OK (Some pc')
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bnop")
+ end
+ | None =>
+ if negb isfst then OK (Some pc)
+ else Error (msg "BTL.verify_block: isfst is true Bnop (on_rtl is false)")
+ end
+ | Bop op lr r _ =>
+ match cfg!pc with
+ | Some (Iop op' lr' r' pc') =>
+ if (eq_operation op op') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then
+ OK (Some pc')
+ else Error (msg "BTL.verify_block: different r in Bop")
+ else Error (msg "BTL.verify_block: different lr in Bop")
+ else Error (msg "BTL.verify_block: different operations in Bop")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bop")
+ end
+ | Bload tm m a lr r _ =>
+ match cfg!pc with
+ | Some (Iload tm' m' a' lr' r' pc') =>
+ if (trapping_mode_eq tm tm') then
+ if (chunk_eq m m') then
+ if (eq_addressing a a') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then
+ OK (Some pc')
+ else Error (msg "BTL.verify_block: different r in Bload")
+ else Error (msg "BTL.verify_block: different lr in Bload")
+ else Error (msg "BTL.verify_block: different addressing in Bload")
+ else Error (msg "BTL.verify_block: different chunk in Bload")
+ else Error (msg "BTL.verify_block: different trapping_mode in Bload")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bload")
+ end
+ | Bstore m a lr r _ =>
+ match cfg!pc with
+ | Some (Istore m' a' lr' r' pc') =>
+ if (chunk_eq m m') then
+ if (eq_addressing a a') then
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (Pos.eq_dec r r') then OK (Some pc')
+ else Error (msg "BTL.verify_block: different r in Bstore")
+ else Error (msg "BTL.verify_block: different lr in Bstore")
+ else Error (msg "BTL.verify_block: different addressing in Bstore")
+ else Error (msg "BTL.verify_block: different chunk in Bstore")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bstore")
+ end
+ | Bseq b1 b2 =>
+ do opc <- verify_block dupmap cfg isfst pc b1;
+ match opc with
+ | Some pc' =>
+ verify_block dupmap cfg false pc' b2
+ | None => Error (msg "BTL.verify_block: None next pc in Bseq (deadcode)")
+ end
+ | Bcond c lr bso bnot _ =>
+ match cfg!pc with
+ | Some (Icond c' lr' pcso pcnot i') =>
+ if (list_eq_dec Pos.eq_dec lr lr') then
+ if (eq_condition c c') then
+ do opc1 <- verify_block dupmap cfg false pcso bso;
+ do opc2 <- verify_block dupmap cfg false pcnot bnot;
+ match opc1, opc2 with
+ | None, o => OK o
+ | o, None => OK o
+ | Some x, Some x' =>
+ if Pos.eq_dec x x' then OK (Some x)
+ else Error (msg "BTL.verify_block: is_join_opt incorrect for Bcond")
+ end
+ else Error (msg "BTL.verify_block: incompatible conditions in Bcond")
+ else Error (msg "BTL.verify_block: different lr in Bcond")
+ | _ => Error (msg "BTL.verify_block: incorrect cfg Bcond")
+ end
+ end.
+
+(* This property expresses that "relation" [match_iblock] is a partial function (see also [iblock_istep_run_equiv] above) *)
+Lemma verify_block_correct dupmap cfg ib: forall pc isfst fin,
+ verify_block dupmap cfg isfst pc ib = (OK fin) -> match_iblock dupmap cfg isfst pc ib fin.
+Proof.
+ induction ib; intros;
+ try (unfold verify_block in H; destruct (cfg!pc) eqn:EQCFG; [ idtac | discriminate; fail ]).
+ - (* BF *)
+ destruct fi; unfold verify_block in H.
+ + (* Bgoto *)
+ monadInv H.
+ destruct (isfst); simpl in EQ0; inv EQ0.
+ eapply verify_is_copy_correct in EQ.
+ constructor; assumption.
+ + (* Breturn *)
+ destruct (cfg!pc) eqn:EQCFG; try destruct i; try discriminate.
+ destruct (option_eq _ _ _); try discriminate. inv H.
+ eapply mib_BF; eauto. constructor.
+ + (* Bcall *)
+ destruct (cfg!pc) eqn:EQCFG; try destruct i; try discriminate.
+ monadInv H.
+ eapply verify_is_copy_correct in EQ.
+ destruct (signature_eq _ _); try discriminate.
+ destruct (product_eq _ _ _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. subst.
+ inv EQ0. eapply mib_BF; eauto. constructor; assumption.
+ + (* Btailcall *)
+ destruct (cfg!pc) eqn:EQCFG; try destruct i; try discriminate.
+ destruct (signature_eq _ _); try discriminate.
+ destruct (product_eq _ _ _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate. subst.
+ inv H. eapply mib_BF; eauto. constructor.
+ + (* Bbuiltin *)
+ destruct (cfg!pc) eqn:EQCFG; try destruct i; try discriminate.
+ monadInv H.
+ eapply verify_is_copy_correct in EQ.
+ destruct (external_function_eq _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (builtin_res_eq_pos _ _); try discriminate. subst.
+ inv EQ0. eapply mib_BF; eauto. constructor; assumption.
+ + (* Bjumptable *)
+ destruct (cfg!pc) eqn:EQCFG; try destruct i; try discriminate.
+ monadInv H.
+ eapply verify_is_copy_list_correct in EQ.
+ destruct (Pos.eq_dec _ _); try discriminate. subst.
+ inv EQ0. eapply mib_BF; eauto. constructor; assumption.
+ - (* Bnop *)
+ destruct oiinfo; simpl in *.
+ + destruct (cfg!pc) eqn:EQCFG; try discriminate.
+ destruct i0; inv H. constructor; assumption.
+ + destruct isfst; try discriminate. inv H.
+ constructor; assumption.
+ - (* Bop *)
+ destruct i; try discriminate.
+ destruct (eq_operation _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. inv H.
+ constructor; assumption.
+ - (* Bload *)
+ destruct i; try discriminate.
+ destruct (trapping_mode_eq _ _); try discriminate.
+ destruct (chunk_eq _ _); try discriminate.
+ destruct (eq_addressing _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. inv H.
+ constructor; assumption.
+ - (* Bstore *)
+ destruct i; try discriminate.
+ destruct (chunk_eq _ _); try discriminate.
+ destruct (eq_addressing _ _); try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (Pos.eq_dec _ _); try discriminate. inv H.
+ constructor; assumption.
+ - (* Bseq *)
+ monadInv H.
+ destruct x; try discriminate.
+ eapply mib_seq_Some.
+ eapply IHib1; eauto.
+ eapply IHib2; eauto.
+ - (* Bcond *)
+ destruct i; try discriminate.
+ destruct (list_eq_dec _ _ _); try discriminate.
+ destruct (eq_condition _ _); try discriminate.
+ fold (verify_block dupmap cfg false n ib1) in H.
+ fold (verify_block dupmap cfg false n0 ib2) in H.
+ monadInv H.
+ destruct x, x0; try destruct (Pos.eq_dec _ _); try discriminate.
+ all: inv EQ2; eapply mib_cond; eauto; econstructor.
+Qed.
+Local Hint Resolve verify_block_correct: core.
+
+Fixpoint verify_blocks dupmap (cfg: code) (cfg':RTL.code) l: res unit :=
+ match l with
+ | nil => OK tt
+ | (pc, pc')::l =>
+ match cfg!pc with
+ | Some ib => do o <- verify_block dupmap cfg' true pc' ib.(entry);
+ match o with
+ | None => verify_blocks dupmap cfg cfg' l
+ | _ => Error(msg "BTL.verify_blocks.end")
+ end
+ | _ => Error(msg "BTL.verify_blocks.entry")
+ end
+ end.
+
+Definition verify_cfg dupmap (cfg: code) (cfg':RTL.code): res unit :=
+ verify_blocks dupmap cfg cfg' (PTree.elements dupmap).
+
+Lemma verify_cfg_correct dupmap cfg cfg' tt:
+ verify_cfg dupmap cfg cfg' = OK tt ->
+ match_cfg dupmap cfg cfg'.
+Proof.
+ unfold verify_cfg.
+ intros X pc pc' H; generalize X; clear X.
+ exploit PTree.elements_correct; eauto.
+ generalize tt pc pc' H; clear tt pc pc' H.
+ generalize (PTree.elements dupmap).
+ induction l as [|[pc1 pc1']l]; simpl.
+ - tauto.
+ - intros pc pc' DUP u H.
+ unfold bind.
+ repeat autodestruct.
+ intros; subst.
+ destruct H as [H|H]; eauto.
+ inversion H; subst.
+ eexists; split; eauto.
+Qed.
+
+Definition verify_function dupmap f f' : res unit :=
+ do _ <- verify_is_copy dupmap (fn_entrypoint f) (RTL.fn_entrypoint f');
+ verify_cfg dupmap (fn_code f) (RTL.fn_code f').
+
+Lemma verify_function_correct dupmap f f' tt:
+ verify_function dupmap f f' = OK tt ->
+ fn_sig f = RTL.fn_sig f' ->
+ fn_params f = RTL.fn_params f' ->
+ fn_stacksize f = RTL.fn_stacksize f' ->
+ match_function dupmap f f'.
+Proof.
+ unfold verify_function; intro VERIF. monadInv VERIF.
+ constructor; eauto.
+ eapply verify_cfg_correct; eauto.
+Qed.
+
diff --git a/scheduling/BTLroadmap.md b/scheduling/BTLroadmap.md
new file mode 100644
index 00000000..1514c5d4
--- /dev/null
+++ b/scheduling/BTLroadmap.md
@@ -0,0 +1,454 @@
+# BTL Development Roadmap
+
+BTL aims to be an IR dedicated to defensive certification of middle-end optimizations (before register allocation).
+It provides a CFG of "loop-free" blocks, where each block is run in one step emitting at most a single observational event.
+The "local" optimizations (i.e. preserving "locally" the semantics of such blocks) would be checked by symbolic execution with rewriting rules.
+The main info required from oracles: a "dupmap" mapping block entries (and maybe strategies for controlling path explosion during symbolic execution -- see below).
+Moreover, the "global" optimizations would require some invariants annotations at block entry (provided by oracles).
+
+Examples of optimizations that we aim to support:
+
+ - instruction scheduling
+ - instruction rewritings (instruction selection)
+ - branch duplication, "if-lifting" (e.g. side-exit moved up in "traces").
+ - strength-reduction
+ - SSA optimizations
+
+We sketch below the various kinds of supported optimizations in development order...
+
+Remark that we may port most of the current optimizations from RTL to BTL (even maybe, register allocation).
+However, RTL will probably remain useful for "fine-tuned" duplication that crosses block boundaries (e.g. Duplicate module).
+
+## Introduction
+
+En gros la syntaxe d'un bloc BTL est définie par:
+
+ Inductive iblock: Type :=
+ | ... (* instructions basiques ou "finales" (call, return, goto, etc) *)
+ | Bseq (b1 b2: iblock) (* séquence de deux blocs *)
+ | Bcond (cond:condition) (args:list reg) (ifso ifnot: iblock) (* if-then-else *)
+
+Le modèle de base de l'exécution symbolique représente un tel bloc par un état symbolique de type:
+
+ Inductive sstate :=
+ | Sfinal (sis: sistate) (sfv: sfval)
+ | Scond (cond: condition) (args: list_sval) (ifso ifnot: sstate)
+ | Sabort
+ .
+
+où `sistate` est un PPA (preconditioned parallel assignment) des registres et `sfval` représente un branchement (call, return, goto, etc).
+
+Autrement dit, un état symbolique représente tous les chemins
+d'exécution possibles par une sorte de gros BDD ayant sur les feuilles
+un `Sfinal` (ou un `Sabort` s'il manque une instruction de branchement sur ce chemin).
+
+## Block boundaries, branch duplication or factorization
+
+Two possibility of branch duplications (e.g tail-duplication, loop unrolling, etc):
+
+- during RTL -> BTL (while "building" BTL blocks)
+- during BTL -> BTL. Typically, the "if-lifting" à la Justus could be performed/verified in a single pass here !
+
+Branch factorization should also be possible in BTL -> RTL pass. Example: revert "loop-unrolling".
+
+**IMPLEM NOTE:** a single verifier for RTL -> BTL and BTL -> RTL simulations, with a dupmap: BTL block-entries -> RTL nodes.
+
+
+**CURRENT STATUS**
+
+- verifier: implemented and proved w.r.t match_iblock specification.
+- Proof:
+ - BTL -> RTL: done.
+ - RTL -> BTL: done.
+- Oracles:
+ - BTL -> RTL: TODO.
+ - RTL -> BTL: started.
+
+## Simulation modulo liveness and "Functional" semantics of BTL blocks
+
+L'approche suivie pour réaliser la simulation modulo liveness dans
+RTLpath est compliquée à adapter sur BTL. En effet, un état
+symbolique RTLpath correspond à un état symbolique BTL très
+particulier: toutes les "feuilles" (les `Sfinal`) sont des `Sgoto`
+sauf éventuellement une. Or, dans RTLpath, le traitement de l'info de
+liveness sur cette feuille particulière est très adhoc et laborieux
+(cf. le traitement de `pre_output_regs` dans RTLpathScheduler, etc).
+On n'a pas envie de généraliser cette usine à gaz.
+
+On cherche donc une façon plus abstraite de faire... On a l'idée de
+coder la "simulation modulo liveness" dans une "simulation
+less_def". Ça peut rendre le cadre du test de simulation plus
+simple et plus général.
+
+**Idée_Informelle** à côté de la sémantique "à la RTL" pour BTL
+[BTLmatch.cfgsem], on définit une sémantique [BTL.fsem], où c'est
+juste "l'entrée dans un bloc" qui change de sémantique. Intuitivement,
+cette sémantique considère chaque bloc comme une sorte de
+fonction paramétrée par les `input_regs` et appelée uniquement en
+"tailcall" (via les "goto"). C'est ce qu'on va appeler la "functional
+semantics" de BTL (l'autre étant appelée qqchose comme "CFG semantics"
+?).
+
+Autrement dit, sur l'entrée dans un bloc pour un état (rs,m), on pourrait moralement
+commencer par mettre à Vundef tous les registres qui ne sont pas dans l'`input_regs`.
+
+**NOTE** cette idée de voir les blocs comme des "fonctions" correpond
+bien à la représentation "SSA" à la Appel/MLIR. Donc cette sémantique
+peut servir de base pour un support de formes SSA (partielles ou
+totales) dans BTL. Pour l'encodage de SSA, il suffira d'étendre le
+mécanisme d'initialisation du "rs0" d'un bloc avec un passage de
+paramètres.
+
+**REM** pour le test d'exécution symbolique, il semble plus adapté de
+mettre les Vundef juste à la fin de la transition (précédente) plutôt
+qu'au début (de la suivante): c'est d'ailleurs plus proche de la vision SSA.
+
+En fait, on pourrait faire les deux (dans le détail, ça ne ferait pas
+exactement deux fois la même chose: par exemple, quand on sort sur un
+call ou un builtin, le résultat du call/builtin n'est jamais dans le
+liveout puisqu'il va être écrasé de toute façon pendant la transition,
+mais il peut être -- ou pas -- dans le livein de la transition
+suivante dans ce même bloc).
+
+Avoir une initialisation à Vundef en début de bloc permettrait de
+faire une analyse des expressions mal initialisées - par exemple à
+partir du bloc d'entrée de la fonction. Ça semble complémentaire de
+l'analyse de "liveout". Mais utilisable uniquement dans le cadre d'une
+combinaison "exécution symbolique"+"value analysis" (donc pas tout de suite).
+
+Donc, dans un premier temps, la BTL.fsem encode/abstrait la notion de
+"liveout" pour le test d'exécution symbolique. Les définitions des
+deux sémantiques (symbolique et "functional") se font donc en
+simultanée.
+
+**STATUS**
+
+DONE.
+
+## "Basic" symbolic execution / symbolic simulation
+
+We will implement a "basic" (e.g without rewriting rules) simulation test for BTL.fsem.
+Dans un premier temps: pas besoin de "less_undef lockstep", on peut se contenter de tester l'égalité des registres de chaque côté: les registres hors liveout seront égaux à Vundef de chaque côté.
+
+**STATUS**
+
+- BTL_SEtheory: DONE
+ - model of symbolic execution in Continuation-Passing Style for BTL with "correctness" and "completness" thms wrt BTL.fsem
+ - high-level specification [sstate_simu] of "symbolic simulation" over iblocks (wrt BTL.fsem).
+
+- BTL_SEsimuref.v: DONE (raffinement de la notion d'état symbolique)
+
+- BTL_Schedulerproof: DONE (preuve que les simulations symboliques locales de blocks/blocks garantissent la simulation globale du programme pour BTL.fsem).
+
+**TODO**
+
+implem du hash-consing.
+
+## Port of RTLpath optimizations to BTL
+
+- Generalize superblock scheduling for a notion of "extended-blocks" such that each instruction of a block, except the block entry, has a single predecessor in the block.
+- Port rewriting rules of RTLpath.
+- Justus's "poor man SSA" + if-lifting.
+
+## Efficient comparison (guided by oracles) of "if-then-else" sequences.
+
+Le pb est complexe. Comparer des expressions booleennes contenant juste des variables booleennes est déjà NP-complet, avec "explosion exponentielle" dans le pire cas.
+
+Approche proposée: utiliser un mécanisme de vérification "simple", basée sur une comparaison par execution symbolique de "tous" les chemins d'execution (cf Intro ci-dessus).
+Ça pète exponentiellement dans le pire cas: mais on pourra contrôler ce risque d'explosion par les oracles...
+
+Ci-dessous, on liste quelques "techniques" de collaboration oracle/vérificateur pour contrôler l'explosion des chemins.
+Idée: les conditions comportent une liste d'annotations qui permet le guidage du vérificateur par l'oracle.
+
+**Remarque** déjà sur les superblocs, le test de simulation par
+exécution symbolique est quadratique dans le pire cas. Si on a "n"
+variables live sur les sorties et "m" sorties, la vérif est en
+"n*m". En pratique, le nombre de sorties est limité, notamment à cause
+des contraintes pour respecter la structure de superblocs. En RTLpath,
+malgré ce coût quadratique théorique dans le pire cas, en pratique, le
+vérificateur passe bien à l'échelle sur nos benchmarks.
+
+### Contrôle des "joins internes" dans le bloc.
+
+Si dans le bloc, toute condition a au plus un "predecesseur" (au sens
+du CFG RTL) dans le bloc: alors le nombre de "chemins sémantiques"
+(explorés par l'exécution symbolique) est identique au nombre de
+"branches syntaxiques" (présents dans le code). Une façon simple de
+contrôler l'explosion de l'exécution symbolique est de fabriquer (avec
+les oracles) des blocs avec un nombre borné (petit) de "joins
+internes".
+
+**Exemple d'application: généralisation des superblocks**
+
+On considère le bloc BTL ci-dessous (où les `i*` sont des séquences d'instructions basiques sans branchement):
+
+ i0;
+ if (c1) { i1 } else { i1'; goto pc1 }
+ if (c2) { i2; goto pc2 } else { i2' }
+ if (c3} { i3; goto pc3 } else { i3'; goto pc3' }
+
+Sur un tel bloc, il n'y a aucun "join interne" (l'exécution symbolique est donc linéaire et le test de simulation quadratique).
+Mais représenter en RTLpath un tel bloc nécessite au moins 4 blocks (1 superbloc et 3 basic-blocs):
+
+ i0; c1; i1; c2; i2'; i3'; goto pc3'
+ i1'; goto pc1
+ i2; goto pc2
+ i3; goto pc3
+
+La vérification BTL sur le gros bloc ne prendra à priori pas plus de
+temps que la vérification RTLpath cumulée des 4 "petits" blocs. Mais
+la vérification BTL sera plus *puissante*, puisque que quand on va
+vérifier les chemins d'exécutions correspondant à ceux des 3
+basic-blocs, on aura le `i0` en plus dans l'état symbolique (i.e. un
+"contexte d'exécution" plus précis).
+
+**Autre exemple d'application: le if-lifting à la Justus**
+
+Le superblock suivant:
+
+ y1 = e1(x)
+ x = e2(a)
+ y2 = e3(x)
+ if (c[x]) { goto pc } else { i4; goto pc' }
+
+peut être directement montré équivalent à
+
+ x' = e2(a) // x' un registre "frais" (pas dans les "liveout")
+ if (c[x']) {
+ y1 = e1(x);
+ x = x';
+ y2 = e3(x);
+ goto pc
+ } else {
+ y1 = e1(x);
+ x = x';
+ y2 = e3(x);
+ i4;
+ goto pc'
+ }
+
+Ici, la duplication de branche a donc lieu en BTL.
+
+L'exécution symbolique de ces deux blocs va produire deux BDD comparables (avec comparaison des feuilles modulo liveness).
+
+### Comparaison des BDD (modulo réordonnancement des conditions ?)
+
+On peut avoir envie de montrer que les deux blocs ci-dessous sont équivalents (si les dépendences sur les variables le permettent):
+
+ if (c1) { i1 } else { i1' }
+ if (c2) { i2 } else { i2' }
+
+et
+
+ if (c2) { i2 } else { i2' }
+ if (c1) { i1 } else { i1' }
+
+Ça revient (en gros) à comparer les BDD:
+
+ if (c1) { if (c2) {i1;i2} else {i1;i2'} } else { if (c2) {i1';i2} else {i1';i2'} }
+
+et
+
+ if (c2) { if (c1) {i2;i1} else {i2;i1'} } else { if (c1) {i2';i1} else {i2';i1'} }
+
+Pour gérer ça, on peut faire des "Ordered BDD": l'oracle du **bloc
+transformé** annote (certaines) conditions avec des numéros de façon à
+ce l'exécution symbolique du bloc transformé produise un "BDD" qui
+correspond à celui du bloc d'origine (cf. "Principe"
+ci-dessous). Cependant, il semble difficile d'appliquer complètement
+les techniques de mémoïsation des BDD ayant des booléens sur les
+feuilles. Car ici on veut effectuer une comparaison sur des feuilles
+2 à 2 qui n'est pas une égalité, mais une inclusion !
+
+**Principe du réordonnancement de BDD:** l'exécution symbolique du **bloc transformé** réordonne le BDD de
+façon à respecter la numérotation: un pére doit avoir un numéro inférieur à
+chacun de ses fils (en l'absence de numéro, on ignore les contraintes
+de numérotation entre ce noeud est ses voisins). Exemple ci-dessous
+avec trois conditions distinctes (pour order c1 < c2 < c3):
+
+ if (c3) { if (c1) {f1} else {f1'} } else { if (c2} {f2} else {f2'} }
+
+est réordonné en
+
+ if (c1) { if (c2) { if (c3) {f1} else {f2} } else { if (c3) {f1} else {f2'} } }
+ else { if (c2) { if (c3) {f1'} else {f2} } else { if (c3) {f1'} else {f2'} } }
+
+**rem:** on ajoute ici un undefined behavior potentiel à cause l'execution de c2 quand c3 est vrai.
+Mais si le **bloc d'origine** est simulé par cet état symbolique réordonné, c'est correct.
+Le bloc transformé a juste supprimé un test inutile...
+
+Bon, à voir si le principe ci-dessus est vraiment utile dans toute sa
+généralité. Pour simplifier, on peut aussi restreindre le
+réordonnancement du BDD aux cas où il n'y a pas de test redondant
+inséré (comme dans l'exemple initial).
+
+**Version simplifiée: comparaison des BDD sans réordonnancement des conditions**
+
+Dans un premier temps (jusqu'à ce qu'on trouve une optimisation où ça pourrait être utile): pas de réordonnacement des BDD.
+On autorise juste le BDD du bloc transformé à supprimer des conditions par rapport au bloc d'origine.
+Autrement dit, dans la comparaison récursive de `{if (c) BDD1 BDD2}` avec `{if (c') BDD1' BDD2}'`:
+
+- soit `c=c'` et on compare récursivement `BDD1` avec `BDD1'` et `BDD2` avec `BDD2'`.
+- soit `c<>c'` et on compare récursivement `BDD1` et `BDD2` avec `{if (c') BDD1' BDD2'}`
+
+Ce deuxième cas correspond au fait que le test sur `c` dans le bloc d'origine était inutile!
+
+### Propagation de valeurs symbolique de conditions (et élimination de condition) pendant l'execution symbolique
+
+L'exécution symbolique se propageant en "avant", on peut propager les valeurs des conditions symboliques, qu'on peut utiliser pour éliminer des conditions redondantes
+(et donc limiter l'explosion du nombre de chemin).
+
+Pour rendre ce mécanisme efficace et puissant, on peut guider ce mécanisme de propagation/élimination avec des annotations introduites par les oracles.
+
+- une annotation "bind_to x" qui indique de mémoriser la valeur (soit "true" soit "false" suivant la branche) de la condition symbolique avec le nom "x"
+- une annotation "eval_to b proof" qui indique que la condition s'évalue sur la constante "b", ce qui est prouvable avec la preuve "proof".
+
+Ici on peut imaginer un langage plus ou moins compliqué pour prouver l'évaluation des conditions. La version la plus simple:
+
+- "eq(x)" dit simplement que la condition symbolique est syntaxiquement égale celle appelée "x".
+- "eqnot(x)" dit que c'est la négation.
+
+Dans le cas général, on peut introduire tout un système de réécriture pour éliminer les conditions.
+
+En fait, il serait sans doute intéressant de mettre en place un
+"système de réécriture guidé par oracle" pour toutes les instructions
+BTL. Ça permet de concilier "puissance" de l'exécution symbolique et
+"efficacité". L'exécution symbolique va pouvoir éventuellement faire
+des réécritures compliquées, mais uniquement quand c'est nécessaire.
+
+**Exemple: une "if-conversion" généralisée**
+On aimerait montrer que le bloc d'origine:
+
+ if (c) {
+ x1 = e1
+ x2 = e2
+ y = e
+ x3 = e3
+ } else {
+ x3 = e3'
+ z = e'
+ x1 = e1'
+ x2 = e2'
+ }
+
+est simulable par le bloc transformé:
+
+ x1 = (c?e1:e1')
+ x2 = (c?e2:e2')
+ x3 = (c?e3:e3')
+ if (c) { y = e } else { z = e' }
+
+une solution: ajouter une régle de réécriture `x = (c?e:e')` en `if (c) { x=e } else {x=e'}`
+(attention, ce n'est pas une règle de réécriture sur les valeurs
+symboliques, mais sur du code BTL lui-même, avant l'exécution
+symbolique de ce code).
+
+L'exécution symbolique ouvre alors deux branches `c=true` et
+`c=false` sur `x1 = (c?e1:e1')`, puis la propagation/élimination de la
+condition symbolique `c` simplifie les conditionnelles sur `x2`, `x3` et `y`/`z`.
+Au final, on obtient deux BDD identiques sur cet exemple (sans explosion combinatoire).
+
+**Rem** les mécanismes de propagation/réécritures décrits ci-dessus peuvent être aussi très utile pour la simulation symbolique modulo invariants (cf. ci-dessous) !
+
+## Peut-on coder l'égalité modulo liveness comme un cas simple d'invariant symbolique (et faire la vérif de liveneness par execution symbolique) ?
+
+On peut peut-être simplifier le cadre actuel pour rendre l'intégration des invariants symboliques plus facile !! Une piste à creuser...
+
+Si la sémantique `BTL.fsem` a permis de rendre l'architecture un peu plus modulaire (mais sans vraiment simplifier sur le fond).
+Elle ne simplifie pas vraiment l'intégration de la liveness avec les invariants.
+Une approche qui pourrait réellement simplifier: voir le raisonnement modulo liveness comme un cas particulier d'invariant symbolique.
+
+**Idée** pour tout chemin d'exécution passant de `pc` et `pc'`, on ramène la simulation modulo liveness de `ib1` par `ib2` au test:
+
+ [ib1;str_inputs(pc')] lessdef [str_inputs(pc);ib2]
+
+**Avantages possibles**
+
+- plus de verif de liveness spécifique.
+- plus de sémantique `BTL.fsem`
+- les `str_inputs` ne seraient plus dans l'exécution symbolique, mais dans le test de simulation: ça évite les difficultés sur les jumptables.
+
+Par contre, il faut réintroduire une preuve `lessdef` modulo liveness dans `BTL_Schedulerproof`.
+
+### Plan d'implem. incrémentale (dans une branche)
+
+1. Repartir d'une version de BTL sans info de liveness pour simplifier au maximum!! (ie on supprime la semantique fsem et on repart sur cfgsem).
+2. Introduire la preuve `lessdef` modulo liveness dans `BTL_Schedulerproof` en *supposant* qu'on a le test de simulation symbolique qui nous arrange.
+ L'idée est de dégager des conditions suffisantes sur le test de simulation symbolique.
+3. Réaliser le test de simulation symbolique.
+
+## Symbolic Invariants at block entry
+
+Extends the symbolic simulation test to support invariants at block entry, including global renaming of registers. Case-study: support of strength-reduction.
+
+
+### EXAMPLE OF STRENGTH REDUCTION
+
+On veut passer du code C1:
+
+ init: // inputs: int *t, int n, int s
+ int i=0;
+ loop: // inputs: int *t, int n, int s, int i
+ if (i >= n) goto exit;
+ s += t[i];
+ i += 1;
+ goto loop;
+ exit: // inputs: int *t, int s
+
+au code C2:
+
+ init: // inputs: int *t, int n, int s
+ int *ti = t;
+ int *tn = t+n;
+ loop: // inputs: int *t, int s, int *ti, int *tn
+ if (ti >= tn) goto exit;
+ s += *ti;
+ ti += 1; // i.e. sizeof(int)
+ goto loop;
+ exit; // inputs: int *t, int s
+
+Pour donner la correspondance entre les variables des 2 blocs, pour chaque entrée "pc", on introduit une "fonction" de C1@pc.(inputs) -> C2@pc.(inputs).
+
+Typiquement, cette fonction est codable comme un map associant une valeur symbolique sur les C1@pc.(inputs) aux variables de C2@pc.(inputs). Exemple:
+
+ init: // map vide (identité)
+ loop:
+ ti = t+i
+ tn = t+n
+ exit: // map vide (identité)
+
+Si on note `TRANSFER` cette fonction, alors la vérification par
+exécution symbolique que le bloc `ib1` est simulé par `ib2` modulo
+`TRANSFER` sur l'entrée `pc` se ramène à montrer `ib1[TRANSFER]` avec
+`TRANSFER(pc); ib2` pour la simulation symbolique usuelle.
+
+Ci-dessus `ib1[TRANSFER]` dit qu'on a appliqué `TRANSFER(pc')` sur chacune des sorties `pc'` de `ib1`.
+
+**REM** pour que ce soit correct, il faut sans doute vérifier une condition du style `ok(ib1) => ok(ib1[TRANSFER])`...
+
+### Comment gérer le cas des registres résultat de call et builtin ?
+
+Rem: c'est la gestion de ces cas particuliers qui conduit à la notion de `pre_output_regs` (ou `BTL.pre_input`) dans la verif de la simulation modulo liveness.
+On va retrouver ça dans la verif de la simulation modulo invariants symboliques.
+Mais pas évident à éviter.
+
+Concrètement, on ne pourra pas mettre d'invariant qui porte sur le résultat d'un call ou d'un builtin (à part un pur renommage de registre).
+
+### GENERALISATION (What comes next ?)
+
+Est-ce qu'il ne faut pas envisager une combinaison "execution symbolique + value analysis" ? La value analysis pourrait se faire directement sur la sémantique symbolique (donc en sortie de l'exécution symbolique), avec un mécanisme de mémoïsation (pour éviter les calculs redondants dus à la duplication de termes dans l'état symbolique). Intérêts: la value analysis ne se ferait que sur les registres live. Elle serait aussi plus sans doute un peu simple (par exemple inutile d'avoir un "join": on peut se contenter d'un test d'inclusion sur chacun des chemins).
+
+En gros, on pourrait passer d'invariants purement symboliques à des invariants combinant valeurs symboliques et valeurs abstraites.
+
+## Support of SSA-optimizations
+
+Minimum feature: extends BTL with "register renamings" at exits. This should enable to represent SSA-forms in BTL IR, more or less like in MLIR.
+
+Maximum feature: add also a basic instruction that performs parallel renaming of registers.
+This simple feature would help to represent a very general notion of "partial SSA forms": since they could appear in the middle of a block.
+
+In both case, such register renamings would be forbidden in BTL<->RTL matching (SSA-optimizations could only happen within BTL passes).
+
+## Alias analysis in the symbolic simulation
+
+A REGARDER [papier pointé par Justus](https://vbpf.github.io/assets/prevail-paper.pdf)
diff --git a/scheduling/BTLtoRTL.v b/scheduling/BTLtoRTL.v
new file mode 100644
index 00000000..fc58533d
--- /dev/null
+++ b/scheduling/BTLtoRTL.v
@@ -0,0 +1,26 @@
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad BTL.
+Require Export BTLmatchRTL.
+
+Require Import Errors Linking.
+
+(** External oracle *)
+Axiom btl2rtl: BTL.function -> RTL.code * node * (PTree.t node).
+
+Extract Constant btl2rtl => "BTLtoRTLaux.btl2rtl".
+
+Local Open Scope error_monad_scope.
+
+Definition transf_function (f: BTL.function) : res RTL.function :=
+ let (tcte, dupmap) := btl2rtl f in
+ let (tc, te) := tcte in
+ let f' := RTL.mkfunction (fn_sig f) (fn_params f) (fn_stacksize f) tc te in
+ do u <- verify_function dupmap f f';
+ OK f'.
+
+Definition transf_fundef (f: fundef) : res RTL.fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: program) : res RTL.program :=
+ transform_partial_program transf_fundef p.
diff --git a/scheduling/BTLtoRTLaux.ml b/scheduling/BTLtoRTLaux.ml
new file mode 100644
index 00000000..47262b3e
--- /dev/null
+++ b/scheduling/BTLtoRTLaux.ml
@@ -0,0 +1,88 @@
+open Maps
+open BTL
+open RTL
+open Camlcoq
+open RTLcommonaux
+open DebugPrint
+open PrintBTL
+open BTLcommonaux
+open BTLtypes
+open BTLRenumber
+
+let get_inumb iinfo = P.of_int iinfo.inumb
+
+let get_ib_num ib = P.of_int (get_inumb_or_next ib)
+
+let translate_function btl entry =
+ let rtl_code = ref PTree.empty in
+ let rec translate_btl_block ib k =
+ debug "Entering translate_btl_block...\n";
+ print_btl_inst stderr ib;
+ let rtli =
+ match ib with
+ | Bcond (cond, lr, BF (Bgoto s1, _), Bnop None, iinfo) ->
+ Some
+ ( Icond (cond, lr, s1, get_ib_num (get_some k), iinfo.opt_info),
+ get_inumb iinfo )
+ | Bcond (_, _, _, _, _) ->
+ failwith "translate_function: unsupported Bcond"
+ | Bseq (ib1, ib2) ->
+ translate_btl_block ib1 (Some ib2);
+ translate_btl_block ib2 None;
+ None
+ | Bnop (Some iinfo) ->
+ Some (Inop (get_ib_num (get_some k)), get_inumb iinfo)
+ | Bnop None ->
+ failwith
+ "translate_function: Bnop None can only be in the right child of \
+ Bcond"
+ | Bop (op, lr, rd, iinfo) ->
+ Some (Iop (op, lr, rd, get_ib_num (get_some k)), get_inumb iinfo)
+ | Bload (trap, chk, addr, lr, rd, iinfo) ->
+ Some
+ ( Iload (trap, chk, addr, lr, rd, get_ib_num (get_some k)),
+ get_inumb iinfo )
+ | Bstore (chk, addr, lr, src, iinfo) ->
+ Some
+ ( Istore (chk, addr, lr, src, get_ib_num (get_some k)),
+ get_inumb iinfo )
+ | BF (Bcall (sign, fn, lr, rd, s), iinfo) ->
+ Some (Icall (sign, fn, lr, rd, s), get_inumb iinfo)
+ | BF (Btailcall (sign, fn, lr), iinfo) ->
+ Some (Itailcall (sign, fn, lr), get_inumb iinfo)
+ | BF (Bbuiltin (ef, lr, rd, s), iinfo) ->
+ Some (Ibuiltin (ef, lr, rd, s), get_inumb iinfo)
+ | BF (Bjumptable (arg, tbl), iinfo) ->
+ Some (Ijumptable (arg, tbl), get_inumb iinfo)
+ | BF (Breturn oreg, iinfo) -> Some (Ireturn oreg, get_inumb iinfo)
+ | BF (Bgoto s, iinfo) -> None
+ in
+ match rtli with
+ | Some (inst, inumb) -> rtl_code := PTree.set inumb inst !rtl_code
+ | None -> ()
+ in
+ List.iter
+ (fun (n, ibf) ->
+ ibf.binfo.visited <- true;
+ let ib = ibf.entry in
+ translate_btl_block ib None)
+ (PTree.elements btl);
+ !rtl_code
+
+let btl2rtl (f : BTL.coq_function) =
+ (*debug_flag := true;*)
+ let btl = f.fn_code in
+ let entry = f.fn_entrypoint in
+ let obne, dm = renumber btl entry in
+ let ordered_btl, new_entry = obne in
+ let rtl = translate_function ordered_btl new_entry in
+ debug "Entry %d\n" (p2i new_entry);
+ debug "BTL Code:\n";
+ print_btl_code stderr ordered_btl;
+ debug "RTL Code: ";
+ print_code rtl;
+ debug "Dupmap:\n";
+ print_ptree print_pint dm;
+ debug "\n";
+ (*debug_flag := false;*)
+ ((rtl, new_entry), dm)
diff --git a/scheduling/BTLtoRTLproof.v b/scheduling/BTLtoRTLproof.v
new file mode 100644
index 00000000..cbdc81bd
--- /dev/null
+++ b/scheduling/BTLtoRTLproof.v
@@ -0,0 +1,405 @@
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad.
+
+Require Import Errors Linking BTLtoRTL.
+
+Require Import Linking.
+
+
+Inductive match_fundef: BTL.fundef -> RTL.fundef -> Prop :=
+ | match_Internal dupmap f f': match_function dupmap f f' -> match_fundef (Internal f) (Internal f')
+ | match_External ef: match_fundef (External ef) (External ef).
+
+Inductive match_stackframes: BTL.stackframe -> RTL.stackframe -> Prop :=
+ | match_stackframe_intro
+ dupmap res f sp pc rs f' pc'
+ (TRANSF: match_function dupmap f f')
+ (DUPLIC: dupmap!pc = Some pc')
+ : match_stackframes (BTL.Stackframe res f sp pc rs) (RTL.Stackframe res f' sp pc' rs).
+
+Inductive match_states: BTL.state -> RTL.state -> Prop :=
+ | match_states_intro
+ dupmap st f sp pc rs m st' f' pc'
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_function dupmap f f')
+ (DUPLIC: dupmap!pc = Some pc')
+ : match_states (State st f sp pc rs m) (RTL.State st' f' sp pc' rs m)
+ | match_states_call
+ st st' f f' args m
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_fundef f f')
+ : match_states (Callstate st f args m) (RTL.Callstate st' f' args m)
+ | match_states_return
+ st st' v m
+ (STACKS: list_forall2 match_stackframes st st')
+ : match_states (Returnstate st v m) (RTL.Returnstate st' v m)
+ .
+
+Lemma transf_function_correct f f':
+ transf_function f = OK f' -> exists dupmap, match_function dupmap f f'.
+Proof.
+ unfold transf_function; unfold bind. repeat autodestruct.
+ intros H _ _ X. inversion X; subst; clear X.
+ eexists; eapply verify_function_correct; simpl; eauto.
+Qed.
+
+Lemma transf_fundef_correct f f':
+ transf_fundef f = OK f' -> match_fundef f f'.
+Proof.
+ intros TRANSF; destruct f; simpl; monadInv TRANSF.
+ + exploit transf_function_correct; eauto.
+ intros (dupmap & MATCH_F).
+ eapply match_Internal; eauto.
+ + eapply match_External.
+Qed.
+
+Definition match_prog (p: program) (tp: RTL.program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+Section RTL_SIMULATES_BTL.
+
+Variable prog: program.
+Variable tprog: RTL.program.
+
+Hypothesis TRANSL: match_prog prog tprog.
+
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Lemma symbols_preserved s: Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof.
+ rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
+Qed.
+
+Lemma senv_preserved: Senv.equiv ge tge.
+Proof.
+ eapply (Genv.senv_match TRANSL).
+Qed.
+
+Lemma functions_translated (v: val) (f: fundef):
+ Genv.find_funct ge v = Some f ->
+ exists tf cunit, transf_fundef f = OK tf /\ Genv.find_funct tge v = Some tf /\ linkorder cunit prog.
+Proof.
+ intros. exploit (Genv.find_funct_match TRANSL); eauto.
+ intros (cu & tf & A & B & C).
+ repeat eexists; intuition eauto.
+ + unfold incl; auto.
+ + eapply linkorder_refl.
+Qed.
+
+Lemma function_ptr_translated v f:
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ transf_fundef f = OK tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
+Qed.
+
+Lemma function_sig_translated f tf: transf_fundef f = OK tf -> RTL.funsig tf = funsig f.
+Proof.
+ intros H; apply transf_fundef_correct in H; destruct H; simpl; eauto.
+ erewrite preserv_fnsig; eauto.
+Qed.
+
+Lemma transf_initial_states s1:
+ initial_state prog s1 ->
+ exists s2, RTL.initial_state tprog s2 /\ match_states s1 s2.
+Proof.
+ intros. inv H.
+ exploit function_ptr_translated; eauto. intros (tf & FIND & TRANSF).
+ eexists. split.
+ - econstructor; eauto.
+ + eapply (Genv.init_mem_transf_partial TRANSL); eauto.
+ + replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main. eauto.
+ + erewrite function_sig_translated; eauto.
+ - constructor; eauto.
+ constructor.
+ apply transf_fundef_correct; auto.
+Qed.
+
+Lemma transf_final_states s1 s2 r:
+ match_states s1 s2 -> final_state s1 r -> RTL.final_state s2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Lemma find_function_preserved ri rs0 fd
+ (FIND : find_function ge ri rs0 = Some fd)
+ : exists fd', RTL.find_function tge ri rs0 = Some fd'
+ /\ transf_fundef fd = OK fd'.
+Proof.
+ pose symbols_preserved as SYMPRES.
+ destruct ri.
+ + simpl in FIND; apply functions_translated in FIND.
+ destruct FIND as (tf & cunit & TFUN & GFIND & LO).
+ eexists; split. eauto. assumption.
+ + simpl in FIND. destruct (Genv.find_symbol _ _) eqn:GFS; try discriminate.
+ apply function_ptr_translated in FIND. destruct FIND as (tf & GFF & TF).
+ eexists; split. simpl. rewrite symbols_preserved.
+ rewrite GFS. eassumption. assumption.
+Qed.
+
+(* Inspired from Duplicateproof.v *)
+Lemma list_nth_z_dupmap:
+ forall dupmap ln ln' (pc pc': node) val,
+ list_nth_z ln val = Some pc ->
+ list_forall2 (fun n n' => dupmap!n = Some n') ln ln' ->
+ exists (pc': node),
+ list_nth_z ln' val = Some pc'
+ /\ dupmap!pc = Some pc'.
+Proof.
+ induction ln; intros until val; intros LNZ LFA.
+ - inv LNZ.
+ - inv LNZ. destruct (zeq val 0) eqn:ZEQ.
+ + inv H0. destruct ln'; inv LFA.
+ simpl. exists n. split; auto.
+ + inv LFA. simpl. rewrite ZEQ. exploit IHln. 2: eapply H0. all: eauto.
+Qed.
+
+(* variant of [star RTL.step] but requiring proposition [P] on the [refl] (stutttering) case. *)
+Inductive cond_star_step (P: Prop): RTL.state -> trace -> RTL.state -> Prop :=
+ | css_refl s: P -> cond_star_step P s E0 s
+ | css_plus s1 t s2: plus RTL.step tge s1 t s2 -> cond_star_step P s1 t s2
+ .
+
+Lemma css_plus_trans P Q s0 s1 s2 t:
+ plus RTL.step tge s0 E0 s1 ->
+ cond_star_step P s1 t s2 ->
+ cond_star_step Q s0 t s2.
+Proof.
+ intros PLUS STAR.
+ eapply css_plus.
+ inv STAR; auto.
+ eapply plus_trans; eauto.
+Qed.
+
+Lemma css_E0_trans isfst isfst' s0 s1 s2:
+ cond_star_step (isfst=false) s0 E0 s1 ->
+ cond_star_step (false=isfst') s1 E0 s2 ->
+ cond_star_step (isfst=isfst') s0 E0 s2.
+Proof.
+ intros S1 S2.
+ inversion S1; subst; eauto.
+ inversion S2; subst; eauto.
+ eapply css_plus_trans; eauto.
+Qed.
+
+Lemma css_star P s0 s1 t:
+ cond_star_step P s0 t s1 ->
+ star RTL.step tge s0 t s1.
+Proof.
+ destruct 1.
+ - eapply star_refl; eauto.
+ - eapply plus_star; eauto.
+Qed.
+
+Local Hint Constructors RTL.step match_states: core.
+Local Hint Resolve css_refl plus_one transf_fundef_correct: core.
+
+Lemma iblock_istep_simulation sp dupmap stack' f' rs0 m0 ib rs1 m1 ofin
+ (IBIS: iblock_istep ge sp rs0 m0 ib rs1 m1 ofin):
+ forall pc0 opc isfst
+ (MIB: match_iblock dupmap (RTL.fn_code f') isfst pc0 ib opc),
+ match ofin with
+ | None => exists pc1,(opc = Some pc1) /\ cond_star_step (isfst = false) (RTL.State stack' f' sp pc0 rs0 m0) E0 (RTL.State stack' f' sp pc1 rs1 m1)
+ | Some fin =>
+ exists isfst' pc1 iinfo, match_iblock dupmap (RTL.fn_code f') isfst' pc1 (BF fin iinfo) None
+ /\ cond_star_step (isfst = isfst') (RTL.State stack' f' sp pc0 rs0 m0) E0 (RTL.State stack' f' sp pc1 rs1 m1)
+ end.
+Proof.
+ induction IBIS; simpl; intros.
+ - (* exec_final *)
+ assert (X: opc = None). { inv MIB; auto. }
+ subst.
+ repeat eexists; eauto.
+ - (* exec_nop *)
+ inv MIB; eexists; split; econstructor; eauto.
+ - (* exec_op *)
+ inv MIB. exists pc'; split; auto; constructor.
+ apply plus_one. eapply exec_Iop; eauto.
+ erewrite <- eval_operation_preserved; eauto.
+ intros; rewrite symbols_preserved; trivial.
+ - (* exec_load *)
+ inv MIB. exists pc'; split; auto; constructor.
+ apply plus_one. inv LOAD.
+ eapply exec_Iload; eauto.
+ all: try (destruct (eval_addressing _ _ _ _) eqn:EVAL;
+ [ eapply exec_Iload_notrap2 | eapply exec_Iload_notrap1]; eauto).
+ all: erewrite <- eval_addressing_preserved; eauto;
+ intros; rewrite symbols_preserved; trivial.
+ - (* exec_store *)
+ inv MIB. exists pc'; split; auto; constructor.
+ apply plus_one. eapply exec_Istore; eauto.
+ all: erewrite <- eval_addressing_preserved; eauto;
+ intros; rewrite symbols_preserved; trivial.
+ - (* exec_seq_stop *)
+ inv MIB; eauto.
+ - (* exec_seq_continue *)
+ inv MIB.
+ exploit IHIBIS1; eauto.
+ intros (pc1 & EQpc1 & STEP1); inv EQpc1.
+ exploit IHIBIS2; eauto.
+ destruct ofin; simpl.
+ + intros (ifst2 & pc2 & iinfo & M2 & STEP2).
+ repeat eexists; eauto.
+ eapply css_E0_trans; eauto.
+ + intros (pc2 & EQpc2 & STEP2); inv EQpc2.
+ eexists; split; auto.
+ eapply css_E0_trans; eauto.
+ - (* exec_cond *)
+ inv MIB.
+ rename H10 into JOIN. (* is_join_opt opc1 opc2 opc *)
+ destruct b; exploit IHIBIS; eauto.
+ + (* taking ifso *)
+ destruct ofin; simpl.
+ * (* ofin is Some final *)
+ intros (isfst' & pc1 & iinfo' & MI & STAR).
+ repeat eexists; eauto.
+ eapply css_plus_trans; eauto.
+ * (* ofin is None *)
+ intros (pc1 & OPC & PLUS). inv OPC.
+ inv JOIN; eexists; split; eauto.
+ all:
+ eapply css_plus_trans; eauto.
+ + (* taking ifnot *)
+ destruct ofin; simpl.
+ * (* ofin is Some final *)
+ intros (isfst' & pc1 & iinfo' & MI & STAR).
+ repeat eexists; eauto.
+ eapply css_plus_trans; eauto.
+ * (* ofin is None *)
+ intros (pc1 & OPC & PLUS). subst.
+ inv JOIN; eexists; split; eauto.
+ all:
+ eapply css_plus_trans; eauto.
+Qed.
+
+Lemma final_simu_except_goto sp dupmap stack stack' f f' rs1 m1 pc1 fin t s
+ (STACKS : list_forall2 match_stackframes stack stack')
+ (TRANSF : match_function dupmap f f')
+ (FS : final_step tid ge stack f sp rs1 m1 fin t s)
+ (i : instruction)
+ (ATpc1 : (RTL.fn_code f') ! pc1 = Some i)
+ (MF : match_final_inst dupmap fin i)
+ : exists s', RTL.step tge (RTL.State stack' f' sp pc1 rs1 m1) t s' /\ match_states s s'.
+Proof.
+ inv MF; inv FS.
+ - (* return *)
+ eexists; split.
+ eapply exec_Ireturn; eauto.
+ erewrite <- preserv_fnstacksize; eauto.
+ econstructor; eauto.
+ - (* call *)
+ rename H7 into FIND.
+ exploit find_function_preserved; eauto.
+ intros (fd' & FIND' & TRANSFU).
+ eexists; split. eapply exec_Icall; eauto.
+ apply function_sig_translated. assumption.
+ repeat (econstructor; eauto).
+ - (* tailcall *)
+ rename H2 into FIND.
+ exploit find_function_preserved; eauto.
+ intros (fd' & FIND' & TRANSFU).
+ eexists; split. eapply exec_Itailcall; eauto.
+ apply function_sig_translated. assumption.
+ erewrite <- preserv_fnstacksize; eauto.
+ repeat (econstructor; eauto).
+ - (* builtin *)
+ pose symbols_preserved as SYMPRES.
+ eexists. split.
+ eapply exec_Ibuiltin; eauto. eapply eval_builtin_args_preserved; eauto.
+ eapply external_call_symbols_preserved; eauto. eapply senv_preserved.
+ econstructor; eauto.
+ - (* jumptable *)
+ pose symbols_preserved as SYMPRES.
+ exploit list_nth_z_dupmap; eauto. intros (pc'1 & LNZ & REVM).
+ eexists. split.
+ eapply exec_Ijumptable; eauto.
+ econstructor; eauto.
+Qed.
+
+Lemma iblock_step_simulation sp dupmap stack stack' f f' ib rs0 m0 rs1 m1 pc0 fin t s
+ (STACKS: list_forall2 match_stackframes stack stack')
+ (TRANSF: match_function dupmap f f')
+ (IBIS: iblock_istep ge sp rs0 m0 ib rs1 m1 (Some fin))
+ (MIB : match_iblock dupmap (RTL.fn_code f') true pc0 ib None)
+ (FS : final_step tid ge stack f sp rs1 m1 fin t s)
+ : exists s', plus RTL.step tge (RTL.State stack' f' sp pc0 rs0 m0) t s' /\ match_states s s'.
+Proof.
+ intros; exploit iblock_istep_simulation; eauto.
+ simpl. intros (isfst' & pc1 & iinfo & MI & STAR). clear IBIS MIB.
+ inv MI.
+ - (* final inst except goto *)
+ exploit final_simu_except_goto; eauto.
+ intros (s' & STEP & MS). eexists; split.
+ + eapply plus_right.
+ eapply css_star; eauto.
+ eapply STEP. econstructor.
+ + eapply MS.
+ - (* goto *)
+ inv FS.
+ inv STAR; try congruence.
+ eexists; split. eauto.
+ econstructor; eauto.
+Qed.
+
+Theorem plus_simulation s1 t s1':
+ step tid ge s1 t s1' ->
+ forall s2 (MS: match_states s1 s2),
+ exists s2',
+ plus RTL.step tge s2 t s2'
+ /\ match_states s1' s2'.
+Proof.
+ destruct 1; intros; inv MS.
+ - eapply dupmap_correct in DUPLIC; eauto.
+ destruct DUPLIC as (ib' & FNC & MIB).
+ try_simplify_someHyps. destruct STEP as (rs' & m' & fin & IBIS & FS).
+ intros; exploit iblock_step_simulation; eauto.
+ (* exec_function_internal *)
+ - inversion TRANSF as [dupmap f0 f0' MATCHF|]; subst. eexists. split.
+ + eapply plus_one. apply RTL.exec_function_internal.
+ erewrite <- preserv_fnstacksize; eauto.
+ + erewrite <- preserv_fnparams; eauto.
+ eapply match_states_intro; eauto.
+ apply dupmap_entrypoint. assumption.
+ (* exec_function_external *)
+ - inversion TRANSF as [|]; subst. eexists. split.
+ + eapply plus_one. econstructor.
+ eapply external_call_symbols_preserved; eauto. apply senv_preserved.
+ + constructor. assumption.
+ (* exec_return *)
+ - inversion STACKS as [|a1 al b1 bl H1 HL]; subst.
+ destruct b1 as [res' f' sp' pc' rs'].
+ eexists. split.
+ + eapply plus_one. constructor.
+ + inv H1. econstructor; eauto.
+Qed.
+
+Theorem transf_program_correct_cfg:
+ forward_simulation (BTLmatchRTL.cfgsem prog) (RTL.semantics tprog).
+Proof.
+ eapply forward_simulation_plus with match_states.
+ - eapply senv_preserved.
+ - eapply transf_initial_states.
+ - eapply transf_final_states.
+ - eapply plus_simulation.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (BTL.fsem prog) (RTL.semantics tprog).
+Proof.
+ eapply compose_forward_simulations.
+ - eapply fsem2cfgsem.
+ - eapply transf_program_correct_cfg.
+Qed.
+
+End RTL_SIMULATES_BTL.
diff --git a/scheduling/BTLtypes.ml b/scheduling/BTLtypes.ml
new file mode 100644
index 00000000..64001adf
--- /dev/null
+++ b/scheduling/BTLtypes.ml
@@ -0,0 +1,31 @@
+open Registers
+
+type inst_info = {
+ mutable inumb : int;
+ mutable opt_info : bool option;
+ mutable visited : bool;
+ mutable liveins : Regset.t;
+}
+(** This struct is a ghost field attached to each BTL instruction
+ * inumb: int field used for remembering the original numbering of CFG
+ * opt_info: option bool used for various tests:
+ * - On Bcond, stores the prediction information
+ * - On Bload, stores the trapping information (if Some false, the load was
+ * initially non-trapping, and the opposite if some true)
+ * visited: boolean used to mark nodes
+ * liveins: set of lives registers
+ *)
+
+type block_info = {
+ mutable bnumb : int;
+ mutable visited : bool;
+ s_output_regs : Regset.t;
+ typing : RTLtyping.regenv;
+}
+(** Struct attached to each BTL iblock_info type
+ * bnumb: int representing the block id in the BTL CFG
+ * visited: boolean used to mark blocks
+ * s_output_regs: set of output registers
+ * typing: field transferring RTL typing information in BTL (used in regpres
+ * scheduling)
+ *)
diff --git a/scheduling/InstructionScheduler.ml b/scheduling/InstructionScheduler.ml
new file mode 100644
index 00000000..4c9bde64
--- /dev/null
+++ b/scheduling/InstructionScheduler.ml
@@ -0,0 +1,1753 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+let with_destructor dtor stuff f =
+ try let ret = f stuff in
+ dtor stuff;
+ ret
+ with exn -> dtor stuff;
+ raise exn;;
+
+let with_out_channel chan f = with_destructor close_out chan f;;
+let with_in_channel chan f = with_destructor close_in chan f;;
+
+(** Schedule instructions on a synchronized pipeline
+@author David Monniaux, CNRS, VERIMAG *)
+
+type latency_constraint = {
+ instr_from : int;
+ instr_to : int;
+ latency : int };;
+
+type problem = {
+ max_latency : int;
+ resource_bounds : int array;
+ live_regs_entry : Registers.Regset.t;
+ typing : RTLtyping.regenv;
+ reference_counting : ((Registers.reg, int * int) Hashtbl.t
+ * ((Registers.reg * bool) list array)) option;
+ instruction_usages : int array array;
+ latency_constraints : latency_constraint list;
+ };;
+
+let print_problem channel problem =
+ (if problem.max_latency >= 0
+ then Printf.fprintf channel "max makespan: %d\n" problem.max_latency);
+ output_string channel "resource bounds:";
+ (Array.iter (fun b -> Printf.fprintf channel " %d" b) problem.resource_bounds);
+ output_string channel ";\n";
+ (Array.iteri (fun i v ->
+ Printf.fprintf channel "instr%d:" i;
+ (Array.iter (fun b -> Printf.fprintf channel " %d" b) v);
+ output_string channel ";\n") problem.instruction_usages);
+ List.iter (fun instr ->
+ Printf.printf "t%d - t%d >= %d;\n"
+ instr.instr_to instr.instr_from instr.latency)
+ problem.latency_constraints;;
+
+let get_nr_instructions problem = Array.length problem.instruction_usages;;
+let get_nr_resources problem = Array.length problem.resource_bounds;;
+
+type solution = int array
+type scheduler = problem -> solution option
+
+(* DISABLED
+(** Schedule the problem optimally by constraint solving using the Gecode solver. *)
+external gecode_scheduler : problem -> solution option =
+ "caml_gecode_schedule_instr";;
+ *)
+
+let maximum_slot_used times =
+ let maxi = ref (-1) in
+ for i=0 to (Array.length times)-2
+ do
+ maxi := max !maxi times.(i)
+ done;
+ !maxi;;
+
+let check_schedule (problem : problem) (times : solution) =
+ let nr_instructions = get_nr_instructions problem in
+ (if Array.length times <> nr_instructions+1
+ then failwith
+ (Printf.sprintf "check_schedule: %d times expected, got %d"
+ (nr_instructions+1) (Array.length times)));
+ (if problem.max_latency >= 0 && times.(nr_instructions)> problem.max_latency
+ then failwith "check_schedule: max_latency exceeded");
+ (Array.iteri (fun i time ->
+ (if time < 0
+ then failwith (Printf.sprintf "time[%d] < 0" i))) times);
+ let slot_resources = Array.init ((maximum_slot_used times)+1)
+ (fun _ -> Array.copy problem.resource_bounds) in
+ for i=0 to nr_instructions -1
+ do
+ let remaining_resources = slot_resources.(times.(i))
+ and used_resources = problem.instruction_usages.(i) in
+ for resource=0 to (Array.length used_resources)-1
+ do
+ let after = remaining_resources.(resource) - used_resources.(resource) in
+ (if after < 0
+ then failwith (Printf.sprintf "check_schedule: instruction %d exceeds resource %d at slot %d" i resource times.(i)));
+ remaining_resources.(resource) <- after
+ done
+ done;
+ List.iter (fun ctr ->
+ if times.(ctr.instr_to) - times.(ctr.instr_from) < ctr.latency
+ then failwith (Printf.sprintf "check_schedule: time[%d]=%d - time[%d]=%d < %d"
+ ctr.instr_to times.(ctr.instr_to)
+ ctr.instr_from times.(ctr.instr_from)
+ ctr.latency)
+ ) problem.latency_constraints;;
+
+let bound_max_time problem =
+ let total = ref(Array.length problem.instruction_usages) in
+ List.iter (fun ctr -> total := !total + ctr.latency) problem.latency_constraints;
+ !total;;
+
+let vector_less_equal a b =
+ try
+ Array.iter2 (fun x y ->
+ if x>y
+ then raise Exit) a b;
+ true
+ with Exit -> false;;
+
+(* let vector_add a b =
+ * assert ((Array.length a) = (Array.length b));
+ * for i=0 to (Array.length a)-1
+ * do
+ * b.(i) <- b.(i) + a.(i)
+ * done;; *)
+
+let vector_subtract a b =
+ assert ((Array.length a) = (Array.length b));
+ for i=0 to (Array.length a)-1
+ do
+ b.(i) <- b.(i) - a.(i)
+ done;;
+
+(* The version with critical path ordering is much better! *)
+type list_scheduler_order =
+ | INSTRUCTION_ORDER
+ | CRITICAL_PATH_ORDER;;
+
+let int_max (x : int) (y : int) =
+ if x > y then x else y;;
+
+let int_min (x : int) (y : int) =
+ if x < y then x else y;;
+
+let get_predecessors problem =
+ let nr_instructions = get_nr_instructions problem in
+ let predecessors = Array.make (nr_instructions+1) [] in
+ List.iter (fun ctr ->
+ predecessors.(ctr.instr_to) <-
+ (ctr.instr_from, ctr.latency)::predecessors.(ctr.instr_to))
+ problem.latency_constraints;
+ predecessors;;
+
+let get_successors problem =
+ let nr_instructions = get_nr_instructions problem in
+ let successors = Array.make nr_instructions [] in
+ List.iter (fun ctr ->
+ successors.(ctr.instr_from) <-
+ (ctr.instr_to, ctr.latency)::successors.(ctr.instr_from))
+ problem.latency_constraints;
+ successors;;
+
+let critical_paths successors =
+ let nr_instructions = Array.length successors in
+ let path_lengths = Array.make nr_instructions (-1) in
+ let rec compute i =
+ if i=nr_instructions then 0 else
+ match path_lengths.(i) with
+ | -2 -> failwith "InstructionScheduler: the dependency graph has cycles"
+ | -1 -> path_lengths.(i) <- -2;
+ let x = List.fold_left
+ (fun cur (j, latency)-> int_max cur (latency+(compute j)))
+ 1 successors.(i)
+ in path_lengths.(i) <- x; x
+ | x -> x
+ in for i = nr_instructions-1 downto 0
+ do
+ ignore (compute i)
+ done;
+ path_lengths;;
+
+let maximum_critical_path problem =
+ let paths = critical_paths (get_successors problem) in
+ Array.fold_left int_max 0 paths;;
+
+let get_earliest_dates predecessors =
+ let nr_instructions = (Array.length predecessors)-1 in
+ let path_lengths = Array.make (nr_instructions+1) (-1) in
+ let rec compute i =
+ match path_lengths.(i) with
+ | -2 -> failwith "InstructionScheduler: the dependency graph has cycles"
+ | -1 -> path_lengths.(i) <- -2;
+ let x = List.fold_left
+ (fun cur (j, latency)-> int_max cur (latency+(compute j)))
+ 0 predecessors.(i)
+ in path_lengths.(i) <- x; x
+ | x -> x
+ in for i = 0 to nr_instructions
+ do
+ ignore (compute i)
+ done;
+ for i = 0 to nr_instructions - 1
+ do
+ path_lengths.(nr_instructions) <- int_max
+ path_lengths.(nr_instructions) (1 + path_lengths.(i))
+ done;
+ path_lengths;;
+
+exception Unschedulable
+
+let get_latest_dates deadline successors =
+ let nr_instructions = Array.length successors
+ and path_lengths = critical_paths successors in
+ Array.init (nr_instructions + 1)
+ (fun i ->
+ if i < nr_instructions then
+ let path_length = path_lengths.(i) in
+ assert (path_length >= 1);
+ (if path_length > deadline
+ then raise Unschedulable);
+ deadline - path_length
+ else deadline);;
+
+let priority_list_scheduler (order : list_scheduler_order)
+ (problem : problem) :
+ solution option =
+ let nr_instructions = get_nr_instructions problem in
+ let successors = get_successors problem
+ and predecessors = get_predecessors problem
+ and times = Array.make (nr_instructions+1) (-1) in
+
+ let priorities = match order with
+ | INSTRUCTION_ORDER -> None
+ | CRITICAL_PATH_ORDER -> Some (critical_paths successors) in
+
+ let module InstrSet =
+ Set.Make (struct type t=int
+ let compare = match priorities with
+ | None -> (fun x y -> x - y)
+ | Some p -> (fun x y ->
+ (match p.(y)-p.(x) with
+ | 0 -> x - y
+ | z -> z))
+ end) in
+
+ let max_time = bound_max_time problem in
+ let ready = Array.make max_time InstrSet.empty in
+ Array.iteri (fun i preds ->
+ if i<nr_instructions && preds=[]
+ then ready.(0) <- InstrSet.add i ready.(0)) predecessors;
+
+ let current_time = ref 0
+ and current_resources = Array.copy problem.resource_bounds
+ and earliest_time i =
+ try
+ let time = ref (-1) in
+ List.iter (fun (j, latency) ->
+ if times.(j) < 0
+ then raise Exit
+ else let t = times.(j) + latency in
+ if t > !time
+ then time := t) predecessors.(i);
+ assert(!time >= 0);
+ !time
+ with Exit -> -1
+ in
+
+ let advance_time() =
+ begin
+ (if !current_time < max_time-1
+ then
+ begin
+ Array.blit problem.resource_bounds 0 current_resources 0
+ (Array.length current_resources);
+ ready.(!current_time + 1) <-
+ InstrSet.union (ready.(!current_time))
+ (ready.(!current_time + 1));
+ ready.(!current_time) <- InstrSet.empty;
+ end);
+ incr current_time
+ end in
+
+ let attempt_scheduling ready usages =
+ let result = ref (-1) in
+ try
+ InstrSet.iter (fun i ->
+ (* Printf.printf "trying scheduling %d\n" i;
+ pr int_vector usages.(i);
+ print _vector current_resources; *)
+ if vector_less_equal usages.(i) current_resources
+ then
+ begin
+ vector_subtract usages.(i) current_resources;
+ result := i;
+ raise Exit
+ end) ready;
+ -1
+ with Exit -> !result in
+
+ while !current_time < max_time
+ do
+ if (InstrSet.is_empty ready.(!current_time))
+ then advance_time()
+ else
+ match attempt_scheduling ready.(!current_time)
+ problem.instruction_usages with
+ | -1 -> advance_time()
+ | i ->
+ begin
+ assert(times.(i) < 0);
+ times.(i) <- !current_time;
+ ready.(!current_time) <- InstrSet.remove i (ready.(!current_time));
+ List.iter (fun (instr_to, latency) ->
+ if instr_to < nr_instructions then
+ match earliest_time instr_to with
+ | -1 -> ()
+ | to_time ->
+ ready.(to_time) <- InstrSet.add instr_to ready.(to_time))
+ successors.(i);
+ successors.(i) <- []
+ end
+ done;
+ try
+ let final_time = ref (-1) in
+ for i=0 to nr_instructions-1
+ do
+ (if times.(i) < 0 then raise Exit);
+ (if !final_time < times.(i)+1 then final_time := times.(i)+1)
+ done;
+ List.iter (fun (i, latency) ->
+ let target_time = latency + times.(i) in
+ if target_time > !final_time
+ then final_time := target_time
+ ) predecessors.(nr_instructions);
+ times.(nr_instructions) <- !final_time;
+ Some times
+ with Exit -> None;;
+
+let list_scheduler = priority_list_scheduler CRITICAL_PATH_ORDER;;
+
+(* dummy code for placating ocaml's warnings *)
+let _ = fun x -> priority_list_scheduler INSTRUCTION_ORDER x;;
+
+
+(* A scheduler sensitive to register pressure *)
+let reg_pres_scheduler (problem : problem) : solution option =
+ (* DebugPrint.debug_flag := true; *)
+ let nr_instructions = get_nr_instructions problem in
+
+ if !Clflags.option_debug_compcert > 6 then
+ (Printf.eprintf "\nSCHEDULING_SUPERBLOCK %d\n" nr_instructions;
+ flush stderr);
+
+ let successors = get_successors problem
+ and predecessors = get_predecessors problem
+ and times = Array.make (nr_instructions+1) (-1) in
+ let live_regs_entry = problem.live_regs_entry in
+
+ let available_regs = Array.copy Machregsaux.nr_regs in
+
+ let nr_types_regs = Array.length available_regs in
+
+ let thres = Array.fold_left (min)
+ (max !(Clflags.option_regpres_threshold) 0)
+ Machregsaux.nr_regs
+ in
+
+
+ let regs_thresholds = Array.make nr_types_regs thres in
+ (* placeholder value *)
+
+ let class_r r =
+ Machregsaux.class_of_type (problem.typing r) in
+
+ let live_regs = Hashtbl.create 42 in
+
+ List.iter (fun r -> let classe = Machregsaux.class_of_type
+ (problem.typing r) in
+ available_regs.(classe)
+ <- available_regs.(classe) - 1;
+ Hashtbl.add live_regs r classe)
+ (Registers.Regset.elements live_regs_entry);
+
+ let csr_b = ref false in
+
+ let counts, mentions =
+ match problem.reference_counting with
+ | Some (l, r) -> l, r
+ | None -> assert false
+ in
+
+ let fold_delta i = (fun a (r, b) ->
+ a +
+ if class_r r <> i then 0 else
+ (if b then
+ if (Hashtbl.find counts r = (i, 1))
+ then 1 else 0
+ else
+ match Hashtbl.find_opt live_regs r with
+ | None -> -1
+ | Some t -> 0
+ )) in
+
+ let priorities = critical_paths successors in
+
+ let current_resources = Array.copy problem.resource_bounds in
+
+ let module InstrSet =
+ struct
+ module MSet =
+ Set.Make (struct
+ type t=int
+ let compare x y =
+ match priorities.(y) - priorities.(x) with
+ | 0 -> x - y
+ | z -> z
+ end)
+
+ let empty = MSet.empty
+ let is_empty = MSet.is_empty
+ let add = MSet.add
+ let remove = MSet.remove
+ let union = MSet.union
+ let iter = MSet.iter
+
+ let compare_regs i x y =
+ let pyi = List.fold_left (fold_delta i) 0 mentions.(y) in
+ (* print_int y;
+ * print_string " ";
+ * print_int pyi;
+ * print_newline ();
+ * flush stdout; *)
+ let pxi = List.fold_left (fold_delta i) 0 mentions.(x) in
+ match pyi - pxi with
+ | 0 -> (match priorities.(y) - priorities.(x) with
+ | 0 -> x - y
+ | z -> z)
+ | z -> z
+
+ (** t is the register class *)
+ let sched_CSR t ready usages =
+ (* print_string "looking for max delta";
+ * print_newline ();
+ * flush stdout; *)
+ let result = ref (-1) in
+ iter (fun i ->
+ if vector_less_equal usages.(i) current_resources
+ then if !result = -1 || (compare_regs t !result i > 0)
+ then result := i) ready;
+ !result
+ end
+ in
+
+ let max_time = bound_max_time problem + 5*nr_instructions in
+ let ready = Array.make max_time InstrSet.empty in
+
+ Array.iteri (fun i preds ->
+ if i < nr_instructions && preds = []
+ then ready.(0) <- InstrSet.add i ready.(0)) predecessors;
+
+ let current_time = ref 0
+ and earliest_time i =
+ try
+ let time = ref (-1) in
+ List.iter (fun (j, latency) ->
+ if times.(j) < 0
+ then raise Exit
+ else let t = times.(j) + latency in
+ if t > !time
+ then time := t) predecessors.(i);
+ assert (!time >= 0);
+ !time
+ with Exit -> -1
+ in
+
+ let advance_time () =
+ (if !current_time < max_time-1
+ then (
+ Array.blit problem.resource_bounds 0 current_resources 0
+ (Array.length current_resources);
+ ready.(!current_time + 1) <-
+ InstrSet.union (ready.(!current_time))
+ (ready.(!current_time +1));
+ ready.(!current_time) <- InstrSet.empty));
+ incr current_time
+ in
+
+ (* ALL MENTIONS TO cnt ARE PLACEHOLDERS *)
+ let cnt = ref 0 in
+
+ let attempt_scheduling ready usages =
+ let result = ref (-1) in
+ DebugPrint.debug "\n\nREADY: ";
+ InstrSet.iter (fun i -> DebugPrint.debug "%d " i) ready;
+ DebugPrint.debug "\n\n";
+ try
+ Array.iteri (fun i avlregs ->
+ DebugPrint.debug "avlregs: %d %d\nlive regs: %d\n"
+ i avlregs (Hashtbl.length live_regs);
+ if !cnt < 5 && avlregs <= regs_thresholds.(i)
+ then (
+ csr_b := true;
+ let maybe = InstrSet.sched_CSR i ready usages in
+ DebugPrint.debug "maybe %d\n" maybe;
+ (if maybe >= 0 &&
+ let delta =
+ List.fold_left (fold_delta i) 0 mentions.(maybe) in
+ DebugPrint.debug "delta %d\n" delta;
+ delta > 0
+ then
+ (vector_subtract usages.(maybe) current_resources;
+ result := maybe)
+ else
+ if not !Clflags.option_regpres_wait_window
+ then
+ (InstrSet.iter (fun ins ->
+ if vector_less_equal usages.(ins) current_resources
+ && List.fold_left (fold_delta i) 0 mentions.(maybe) >= 0
+ then (result := ins;
+ vector_subtract usages.(!result) current_resources;
+ raise Exit)
+ ) ready;
+ if !result <> -1 then
+ vector_subtract usages.(!result) current_resources
+ else incr cnt)
+ else
+ (incr cnt)
+ );
+ raise Exit)) available_regs;
+ InstrSet.iter (fun i ->
+ if vector_less_equal usages.(i) current_resources
+ then (
+ vector_subtract usages.(i) current_resources;
+ result := i;
+ raise Exit)) ready;
+ -1
+ with Exit ->
+ !result in
+
+ while !current_time < max_time
+ do
+ if (InstrSet.is_empty ready.(!current_time))
+ then advance_time ()
+ else
+ match attempt_scheduling ready.(!current_time)
+ problem.instruction_usages with
+ | -1 -> advance_time()
+ | i -> (assert(times.(i) < 0);
+ (DebugPrint.debug "INSTR ISSUED: %d\n" i;
+ if !csr_b && !Clflags.option_debug_compcert > 6 then
+ (Printf.eprintf "REGPRES: high pres class %d\n" i;
+ flush stderr);
+ csr_b := false;
+ (* if !Clflags.option_regpres_wait_window then *)
+ cnt := 0;
+ List.iter (fun (r,b) ->
+ if b then
+ (match Hashtbl.find_opt counts r with
+ | None -> assert false
+ | Some (t, n) ->
+ Hashtbl.remove counts r;
+ if n = 1 then
+ (Hashtbl.remove live_regs r;
+ available_regs.(t)
+ <- available_regs.(t) + 1))
+ else
+ let t = class_r r in
+ match Hashtbl.find_opt live_regs r with
+ | None -> (Hashtbl.add live_regs r t;
+ available_regs.(t)
+ <- available_regs.(t) - 1)
+ | Some i -> ()
+ ) mentions.(i));
+ times.(i) <- !current_time;
+ ready.(!current_time)
+ <- InstrSet.remove i (ready.(!current_time));
+ List.iter (fun (instr_to, latency) ->
+ if instr_to < nr_instructions then
+ match earliest_time instr_to with
+ | -1 -> ()
+ | to_time ->
+ ((* DebugPrint.debug "TO TIME %d : %d\n" to_time
+ * (Array.length ready); *)
+ ready.(to_time)
+ <- InstrSet.add instr_to ready.(to_time))
+ ) successors.(i);
+ successors.(i) <- []
+ )
+ done;
+
+ try
+ let final_time = ref (-1) in
+ for i = 0 to nr_instructions - 1 do
+ DebugPrint.debug "%d " i;
+ (if times.(i) < 0 then raise Exit);
+ (if !final_time < times.(i) + 1 then final_time := times.(i) + 1)
+ done;
+ List.iter (fun (i, latency) ->
+ let target_time = latency + times.(i) in
+ if target_time > !final_time then
+ final_time := target_time) predecessors.(nr_instructions);
+ times.(nr_instructions) <- !final_time;
+ (* DebugPrint.debug_flag := false; *)
+ Some times
+ with Exit ->
+ (* DebugPrint.debug_flag := true; *)
+ DebugPrint.debug "reg_pres_sched failed\n";
+ (* DebugPrint.debug_flag := false; *)
+ None
+
+;;
+
+
+(********************************************************************)
+
+let reg_pres_scheduler_bis (problem : problem) : solution option =
+ (* DebugPrint.debug_flag := true; *)
+ DebugPrint.debug "\nNEW\n\n";
+ let nr_instructions = get_nr_instructions problem in
+ let successors = get_successors problem
+ and predecessors = get_predecessors problem
+ and times = Array.make (nr_instructions+1) (-1) in
+ let live_regs_entry = problem.live_regs_entry in
+
+ (* let available_regs = Array.copy Machregsaux.nr_regs in *)
+
+ let class_r r =
+ Machregsaux.class_of_type (problem.typing r) in
+
+ let live_regs = Hashtbl.create 42 in
+
+ List.iter (fun r -> let classe = Machregsaux.class_of_type
+ (problem.typing r) in
+ (* available_regs.(classe)
+ * <- available_regs.(classe) - 1; *)
+ Hashtbl.add live_regs r classe)
+ (Registers.Regset.elements live_regs_entry);
+
+
+ let counts, mentions =
+ match problem.reference_counting with
+ | Some (l, r) -> l, r
+ | None -> assert false
+ in
+
+ let fold_delta a (r, b) =
+ a + (if b then
+ match Hashtbl.find_opt counts r with
+ | Some (_, 1) -> 1
+ | _ -> 0
+ else
+ match Hashtbl.find_opt live_regs r with
+ | None -> -1
+ | Some t -> 0
+ ) in
+
+ let priorities = critical_paths successors in
+
+ let current_resources = Array.copy problem.resource_bounds in
+
+ let compare_pres x y =
+ let pdy = List.fold_left (fold_delta) 0 mentions.(y) in
+ let pdx = List.fold_left (fold_delta) 0 mentions.(x) in
+ match pdy - pdx with
+ | 0 -> x - y
+ | z -> z
+ in
+
+ let module InstrSet =
+ Set.Make (struct
+ type t = int
+ let compare x y =
+ match priorities.(y) - priorities.(x) with
+ | 0 -> x - y
+ | z -> z
+ end) in
+
+ let max_time = bound_max_time problem (* + 5*nr_instructions *) in
+ let ready = Array.make max_time InstrSet.empty in
+
+ Array.iteri (fun i preds ->
+ if i < nr_instructions && preds = []
+ then ready.(0) <- InstrSet.add i ready.(0)) predecessors;
+
+ let current_time = ref 0
+ and earliest_time i =
+ try
+ let time = ref (-1) in
+ List.iter (fun (j, latency) ->
+ if times.(j) < 0
+ then raise Exit
+ else let t = times.(j) + latency in
+ if t > !time
+ then time := t) predecessors.(i);
+ assert (!time >= 0);
+ !time
+ with Exit -> -1
+ in
+
+ let advance_time () =
+ (* Printf.printf "ADV\n";
+ * flush stdout; *)
+ (if !current_time < max_time-1
+ then (
+ Array.blit problem.resource_bounds 0 current_resources 0
+ (Array.length current_resources);
+ ready.(!current_time + 1) <-
+ InstrSet.union (ready.(!current_time))
+ (ready.(!current_time +1));
+ ready.(!current_time) <- InstrSet.empty));
+ incr current_time
+ in
+
+
+ let attempt_scheduling ready usages =
+ let result = ref [] in
+ try
+ InstrSet.iter (fun i ->
+ if vector_less_equal usages.(i) current_resources
+ then
+ if !result = [] || priorities.(i) = priorities.(List.hd (!result))
+ then
+ result := i::(!result)
+ else raise Exit
+ ) ready;
+ if !result <> [] then raise Exit;
+ -1
+ with
+ Exit ->
+ let mini = List.fold_left (fun a b ->
+ if a = -1 || compare_pres a b > 0
+ then b else a
+ ) (-1) !result in
+ vector_subtract usages.(mini) current_resources;
+ mini
+ in
+
+ while !current_time < max_time
+ do
+ if (InstrSet.is_empty ready.(!current_time))
+ then advance_time ()
+ else
+ match attempt_scheduling ready.(!current_time)
+ problem.instruction_usages with
+ | -1 -> advance_time()
+ | i -> (
+ DebugPrint.debug "ISSUED: %d\nREADY: " i;
+ InstrSet.iter (fun i -> DebugPrint.debug "%d " i)
+ ready.(!current_time);
+ DebugPrint.debug "\nSUCC: ";
+ List.iter (fun (i, l) -> DebugPrint.debug "%d " i)
+ successors.(i);
+ DebugPrint.debug "\n\n";
+ assert(times.(i) < 0);
+ times.(i) <- !current_time;
+ ready.(!current_time)
+ <- InstrSet.remove i (ready.(!current_time));
+ (List.iter (fun (r,b) ->
+ if b then
+ (match Hashtbl.find_opt counts r with
+ | None -> assert false
+ | Some (t, n) ->
+ Hashtbl.remove counts r;
+ if n = 1 then
+ (Hashtbl.remove live_regs r;
+ (* available_regs.(t)
+ * <- available_regs.(t) + 1 *)))
+ else
+ let t = class_r r in
+ match Hashtbl.find_opt live_regs r with
+ | None -> (Hashtbl.add live_regs r t;
+ (* available_regs.(t)
+ * <- available_regs.(t) - 1 *))
+ | Some i -> ()
+ ) mentions.(i));
+ List.iter (fun (instr_to, latency) ->
+ if instr_to < nr_instructions then
+ match earliest_time instr_to with
+ | -1 -> ()
+ | to_time ->
+ ((* DebugPrint.debug "TO TIME %d : %d\n" to_time
+ * (Array.length ready); *)
+ ready.(to_time)
+ <- InstrSet.add instr_to ready.(to_time))
+ ) successors.(i);
+ successors.(i) <- []
+ )
+ done;
+
+ try
+ let final_time = ref (-1) in
+ for i = 0 to nr_instructions - 1 do
+ (* print_int i;
+ * flush stdout; *)
+ (if times.(i) < 0 then raise Exit);
+ (if !final_time < times.(i) + 1 then final_time := times.(i) + 1)
+ done;
+ List.iter (fun (i, latency) ->
+ let target_time = latency + times.(i) in
+ if target_time > !final_time then
+ final_time := target_time) predecessors.(nr_instructions);
+ times.(nr_instructions) <- !final_time;
+ (* DebugPrint.debug_flag := false; *)
+ Some times
+ with Exit ->
+ DebugPrint.debug "reg_pres_sched failed\n";
+ (* DebugPrint.debug_flag := false; *)
+ None
+
+;;
+
+(********************************************************************)
+
+type bundle = int list;;
+
+let rec extract_deps_to index = function
+ | [] -> []
+ | dep :: deps -> let extracts = extract_deps_to index deps in
+ if (dep.instr_to == index) then
+ dep :: extracts
+ else
+ extracts
+
+exception InvalidBundle;;
+
+let dependency_check problem bundle index =
+ let index_deps = extract_deps_to index problem.latency_constraints in
+ List.iter (fun i ->
+ List.iter (fun dep ->
+ if (dep.instr_from == i) then raise InvalidBundle
+ ) index_deps
+ ) bundle;;
+
+let rec make_bundle problem resources bundle index =
+ let resources_copy = Array.copy resources in
+ let nr_instructions = get_nr_instructions problem in
+ if (index >= nr_instructions) then (bundle, index+1) else
+ let inst_usage = problem.instruction_usages.(index) in
+ try match vector_less_equal inst_usage resources with
+ | false -> raise InvalidBundle
+ | true -> (
+ dependency_check problem bundle index;
+ vector_subtract problem.instruction_usages.(index) resources_copy;
+ make_bundle problem resources_copy (index::bundle) (index+1)
+ )
+ with InvalidBundle -> (bundle, index);;
+
+let rec make_bundles problem index : bundle list =
+ if index >= get_nr_instructions problem then
+ []
+ else
+ let (bundle, new_index) = make_bundle problem problem.resource_bounds [] index in
+ bundle :: (make_bundles problem new_index);;
+
+let bundles_to_schedule problem bundles : solution =
+ let nr_instructions = get_nr_instructions problem in
+ let schedule = Array.make (nr_instructions+1) (nr_instructions+4) in
+ let time = ref 0 in
+ List.iter (fun bundle ->
+ begin
+ List.iter (fun i ->
+ schedule.(i) <- !time
+ ) bundle;
+ time := !time + 1
+ end
+ ) bundles; schedule;;
+
+let greedy_scheduler (problem : problem) : solution option =
+ let bundles = make_bundles problem 0 in
+ Some (bundles_to_schedule problem bundles);;
+
+(* alternate implementation
+let swap_array_elements a i j =
+ let x = a.(i) in
+ a.(i) <- a.(j);
+ a.(j) <- x;;
+
+let array_reverse_slice a first last =
+ let i = ref first and j = ref last in
+ while i < j
+ do
+ swap_array_elements a !i !j;
+ incr i;
+ decr j
+ done;;
+
+let array_reverse a =
+ let a' = Array.copy a in
+ array_reverse_slice a' 0 ((Array.length a)-1);
+ a';;
+ *)
+
+(* unneeded
+let array_reverse a =
+ let n=Array.length a in
+ Array.init n (fun i -> a.(n-1-i));;
+ *)
+
+let reverse_constraint nr_instructions ctr =
+ { instr_to = nr_instructions -ctr.instr_from;
+ instr_from = nr_instructions - ctr.instr_to;
+ latency = ctr.latency };;
+
+(* unneeded
+let rec list_map_filter f = function
+ | [] -> []
+ | h::t ->
+ (match f h with
+ | None -> list_map_filter f t
+ | Some x -> x :: (list_map_filter f t));;
+ *)
+
+let reverse_problem problem =
+ let nr_instructions = get_nr_instructions problem in
+ {
+ max_latency = problem.max_latency;
+ resource_bounds = problem.resource_bounds;
+ live_regs_entry = Registers.Regset.empty; (* PLACEHOLDER *)
+ (* Not needed for the revlist sched, and for now we wont bother
+ with creating a reverse scheduler aware of reg press *)
+
+ typing = problem.typing;
+ reference_counting = problem.reference_counting;
+ instruction_usages = Array.init (nr_instructions + 1)
+ (fun i ->
+ if i=0
+ then Array.map (fun _ -> 0) problem.resource_bounds else problem.instruction_usages.(nr_instructions - i));
+ latency_constraints = List.map (reverse_constraint nr_instructions)
+ problem.latency_constraints
+ };;
+
+let max_scheduled_time solution =
+ let time = ref (-1) in
+ for i = 0 to ((Array.length solution) - 2)
+ do
+ time := max !time solution.(i)
+ done;
+ !time;;
+
+(*
+let recompute_makespan problem solution =
+ let n = (Array.length solution) - 1 and ms = ref 0 in
+ List.iter (fun cstr ->
+ if cstr.instr_to = n
+ then ms := max !ms (solution.(cstr.instr_from) + cstr.latency)
+ ) problem.latency_constraints;
+ !ms;;
+ *)
+
+let schedule_reversed (scheduler : problem -> solution option)
+ (problem : problem) =
+ match scheduler (reverse_problem problem) with
+ | None -> None
+ | Some solution ->
+ let nr_instructions = get_nr_instructions problem in
+ let makespan = max_scheduled_time solution in
+ let ret = Array.init (nr_instructions + 1)
+ (fun i -> makespan-solution.(nr_instructions-i)) in
+ ret.(nr_instructions) <- max ((max_scheduled_time ret) + 1)
+ (ret.(nr_instructions));
+ Some ret;;
+
+(** Schedule the problem using a greedy list scheduling algorithm, from the end. *)
+let reverse_list_scheduler = schedule_reversed list_scheduler;;
+
+let check_problem problem =
+ (if (Array.length problem.instruction_usages) < 1
+ then failwith "length(problem.instruction_usages) < 1");;
+
+let validated_scheduler (scheduler : problem -> solution option)
+ (problem : problem) =
+ check_problem problem;
+ match scheduler problem with
+ | None -> None
+ | (Some solution) as ret -> check_schedule problem solution; ret;;
+
+let get_max_latency solution =
+ solution.((Array.length solution)-1);;
+
+let show_date_ranges problem =
+ let deadline = problem.max_latency in
+ assert(deadline >= 0);
+ let successors = get_successors problem
+ and predecessors = get_predecessors problem in
+ let earliest_dates : int array = get_earliest_dates predecessors
+ and latest_dates : int array = get_latest_dates deadline successors in
+ assert ((Array.length earliest_dates) =
+ (Array.length latest_dates));
+ Array.iteri (fun i early ->
+ let late = latest_dates.(i) in
+ Printf.printf "t[%d] in %d..%d\n" i early late)
+ earliest_dates;;
+
+type pseudo_boolean_problem_type =
+ | SATISFIABILITY
+ | OPTIMIZATION;;
+
+type pseudo_boolean_mapper = {
+ mapper_pb_type : pseudo_boolean_problem_type;
+ mapper_nr_instructions : int;
+ mapper_nr_pb_variables : int;
+ mapper_earliest_dates : int array;
+ mapper_latest_dates : int array;
+ mapper_var_offsets : int array;
+ mapper_final_predecessors : (int * int) list
+};;
+
+(* Latency constraints are:
+ presence of instr-to at each t <= sum of presences of instr-from at compatible times
+
+ if reverse_encoding
+ presence of instr-from at each t <= sum of presences of instr-to at compatible times *)
+
+(* Experiments show reverse_encoding=true multiplies time by 2 in sat4j
+ without making hard instances easier *)
+let direct_encoding = false
+and reverse_encoding = false
+and delta_encoding = true
+
+let pseudo_boolean_print_problem channel problem pb_type =
+ let deadline = problem.max_latency in
+ assert (deadline > 0);
+ let nr_instructions = get_nr_instructions problem
+ and nr_resources = get_nr_resources problem
+ and successors = get_successors problem
+ and predecessors = get_predecessors problem in
+ let earliest_dates = get_earliest_dates predecessors
+ and latest_dates = get_latest_dates deadline successors in
+ let var_offsets = Array.make
+ (match pb_type with
+ | OPTIMIZATION -> nr_instructions+1
+ | SATISFIABILITY -> nr_instructions) 0 in
+ let nr_pb_variables =
+ (let nr = ref 0 in
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ var_offsets.(i) <- !nr;
+ nr := !nr + latest_dates.(i) - earliest_dates.(i) + 1
+ done;
+ !nr)
+ and nr_pb_constraints =
+ (match pb_type with
+ | OPTIMIZATION -> nr_instructions+1
+ | SATISFIABILITY -> nr_instructions) +
+
+ (let count = ref 0 in
+ for t=0 to deadline-1
+ do
+ for j=0 to nr_resources-1
+ do
+ try
+ for i=0 to nr_instructions-1
+ do
+ let usage = problem.instruction_usages.(i).(j) in
+ if t >= earliest_dates.(i) && t <= latest_dates.(i)
+ && usage > 0 then raise Exit
+ done
+ with Exit -> incr count
+ done
+ done;
+ !count) +
+
+ (let count=ref 0 in
+ List.iter
+ (fun ctr ->
+ if ctr.instr_to < nr_instructions
+ then count := !count + 1 + latest_dates.(ctr.instr_to)
+ - earliest_dates.(ctr.instr_to)
+ + (if reverse_encoding
+ then 1 + latest_dates.(ctr.instr_from)
+ - earliest_dates.(ctr.instr_from)
+ else 0)
+ )
+ problem.latency_constraints;
+ !count) +
+
+ (match pb_type with
+ | OPTIMIZATION -> (1 + deadline - earliest_dates.(nr_instructions)) * nr_instructions
+ | SATISFIABILITY -> 0)
+ and measured_nr_constraints = ref 0 in
+
+ let pb_var i t =
+ assert(t >= earliest_dates.(i));
+ assert(t <= latest_dates.(i));
+ let v = 1+var_offsets.(i)+t-earliest_dates.(i) in
+ assert(v <= nr_pb_variables);
+ Printf.sprintf "x%d" v in
+
+ let end_constraint () =
+ begin
+ output_string channel ";\n";
+ incr measured_nr_constraints
+ end in
+
+ let gen_latency_constraint i_to i_from latency t_to =
+ Printf.fprintf channel "* t[%d] - t[%d] >= %d when t[%d]=%d\n"
+ i_to i_from latency i_to t_to;
+ for t_from=earliest_dates.(i_from) to
+ int_min latest_dates.(i_from) (t_to - latency)
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i_from t_from)
+ done;
+ Printf.fprintf channel "-1 %s " (pb_var i_to t_to);
+ Printf.fprintf channel ">= 0";
+ end_constraint()
+
+ and gen_dual_latency_constraint i_to i_from latency t_from =
+ Printf.fprintf channel "* t[%d] - t[%d] >= %d when t[%d]=%d\n"
+ i_to i_from latency i_to t_from;
+ for t_to=int_max earliest_dates.(i_to) (t_from + latency)
+ to latest_dates.(i_to)
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i_to t_to)
+ done;
+ Printf.fprintf channel "-1 %s " (pb_var i_from t_from);
+ Printf.fprintf channel ">= 0";
+ end_constraint()
+ in
+
+ Printf.fprintf channel "* #variable= %d #constraint= %d\n" nr_pb_variables nr_pb_constraints;
+ Printf.fprintf channel "* nr_instructions=%d deadline=%d\n" nr_instructions deadline;
+ begin
+ match pb_type with
+ | SATISFIABILITY -> ()
+ | OPTIMIZATION ->
+ output_string channel "min:";
+ for t=earliest_dates.(nr_instructions) to deadline
+ do
+ Printf.fprintf channel " %+d %s" t (pb_var nr_instructions t)
+ done;
+ output_string channel ";\n";
+ end;
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ let early = earliest_dates.(i) and late= latest_dates.(i) in
+ Printf.fprintf channel "* t[%d] in %d..%d\n" i early late;
+ for t=early to late
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i t)
+ done;
+ Printf.fprintf channel "= 1";
+ end_constraint()
+ done;
+
+ for t=0 to deadline-1
+ do
+ for j=0 to nr_resources-1
+ do
+ let bound = problem.resource_bounds.(j)
+ and coeffs = ref [] in
+ for i=0 to nr_instructions-1
+ do
+ let usage = problem.instruction_usages.(i).(j) in
+ if t >= earliest_dates.(i) && t <= latest_dates.(i)
+ && usage > 0
+ then coeffs := (i, usage) :: !coeffs
+ done;
+ if !coeffs <> [] then
+ begin
+ Printf.fprintf channel "* resource #%d at t=%d <= %d\n" j t bound;
+ List.iter (fun (i, usage) ->
+ Printf.fprintf channel "%+d %s " (-usage) (pb_var i t)) !coeffs;
+ Printf.fprintf channel ">= %d" (-bound);
+ end_constraint();
+ end
+ done
+ done;
+
+ List.iter
+ (fun ctr ->
+ if ctr.instr_to < nr_instructions then
+ begin
+ for t_to=earliest_dates.(ctr.instr_to) to latest_dates.(ctr.instr_to)
+ do
+ gen_latency_constraint ctr.instr_to ctr.instr_from ctr.latency t_to
+ done;
+ if reverse_encoding
+ then
+ for t_from=earliest_dates.(ctr.instr_from) to latest_dates.(ctr.instr_from)
+ do
+ gen_dual_latency_constraint ctr.instr_to ctr.instr_from ctr.latency t_from
+ done
+ end
+ ) problem.latency_constraints;
+
+ begin
+ match pb_type with
+ | SATISFIABILITY -> ()
+ | OPTIMIZATION ->
+ let final_latencies = Array.make nr_instructions 1 in
+ List.iter (fun (i, latency) ->
+ final_latencies.(i) <- int_max final_latencies.(i) latency)
+ predecessors.(nr_instructions);
+ for t_to=earliest_dates.(nr_instructions) to deadline
+ do
+ for i_from = 0 to nr_instructions -1
+ do
+ gen_latency_constraint nr_instructions i_from final_latencies.(i_from) t_to
+ done
+ done
+ end;
+ assert (!measured_nr_constraints = nr_pb_constraints);
+ {
+ mapper_pb_type = pb_type;
+ mapper_nr_instructions = nr_instructions;
+ mapper_nr_pb_variables = nr_pb_variables;
+ mapper_earliest_dates = earliest_dates;
+ mapper_latest_dates = latest_dates;
+ mapper_var_offsets = var_offsets;
+ mapper_final_predecessors = predecessors.(nr_instructions)
+ };;
+
+type pb_answer =
+ | Positive
+ | Negative
+ | Unknown
+
+let line_to_pb_solution sol line nr_pb_variables =
+ let assign s v =
+ begin
+ let i = int_of_string s in
+ sol.(i-1) <- v
+ end in
+ List.iter
+ begin
+ function "" -> ()
+ | item ->
+ (match String.get item 0 with
+ | '+' ->
+ assert ((String.length item) >= 3);
+ assert ((String.get item 1) = 'x');
+ assign (String.sub item 2 ((String.length item)-2)) Positive
+ | '-' ->
+ assert ((String.length item) >= 3);
+ assert ((String.get item 1) = 'x');
+ assign (String.sub item 2 ((String.length item)-2)) Negative
+ | 'x' ->
+ assert ((String.length item) >= 2);
+ assign (String.sub item 1 ((String.length item)-1)) Positive
+ | _ -> failwith "syntax error in pseudo Boolean solution: epected + - or x"
+ )
+ end
+ (String.split_on_char ' ' (String.sub line 2 ((String.length line)-2)));;
+
+let pb_solution_to_schedule mapper pb_solution =
+ Array.mapi (fun i offset ->
+ let first = mapper.mapper_earliest_dates.(i)
+ and last = mapper.mapper_latest_dates.(i)
+ and time = ref (-1) in
+ for t=first to last
+ do
+ match pb_solution.(t - first + offset) with
+ | Positive ->
+ (if !time = -1
+ then time:=t
+ else failwith "duplicate time in pseudo boolean solution")
+ | Negative -> ()
+ | Unknown -> failwith "unknown value in pseudo boolean solution"
+ done;
+ (if !time = -1
+ then failwith "no time in pseudo boolean solution");
+ !time
+ ) mapper.mapper_var_offsets;;
+
+let pseudo_boolean_read_solution mapper channel =
+ let optimum = ref (-1)
+ and optimum_found = ref false
+ and solution = Array.make mapper.mapper_nr_pb_variables Unknown in
+ try
+ while true do
+ match input_line channel with
+ | "" -> ()
+ | line ->
+ begin
+ match String.get line 0 with
+ | 'c' -> ()
+ | 'o' ->
+ assert ((String.length line) >= 2);
+ assert ((String.get line 1) = ' ');
+ optimum := int_of_string (String.sub line 2 ((String.length line)-2))
+ | 's' -> (match line with
+ | "s OPTIMUM FOUND" -> optimum_found := true
+ | "s SATISFIABLE" -> ()
+ | "s UNSATISFIABLE" -> close_in channel;
+ raise Unschedulable
+ | _ -> failwith line)
+ | 'v' -> line_to_pb_solution solution line mapper.mapper_nr_pb_variables
+ | x -> Printf.printf "unknown: %s\n" line
+ end
+ done;
+ assert false
+ with End_of_file ->
+ close_in channel;
+ begin
+ let sol = pb_solution_to_schedule mapper solution in
+ sol
+ end;;
+
+let recompute_max_latency mapper solution =
+ let maxi = ref (-1) in
+ for i=0 to (mapper.mapper_nr_instructions-1)
+ do
+ maxi := int_max !maxi (1+solution.(i))
+ done;
+ List.iter (fun (i, latency) ->
+ maxi := int_max !maxi (solution.(i) + latency)) mapper.mapper_final_predecessors;
+ !maxi;;
+
+let adjust_check_solution mapper solution =
+ match mapper.mapper_pb_type with
+ | OPTIMIZATION ->
+ let max_latency = recompute_max_latency mapper solution in
+ assert (max_latency = solution.(mapper.mapper_nr_instructions));
+ solution
+ | SATISFIABILITY ->
+ let max_latency = recompute_max_latency mapper solution in
+ Array.init (mapper.mapper_nr_instructions+1)
+ (fun i -> if i < mapper.mapper_nr_instructions
+ then solution.(i)
+ else max_latency);;
+
+(* let pseudo_boolean_solver = ref "/local/monniaux/progs/naps/naps" *)
+(* let pseudo_boolean_solver = ref "/local/monniaux/packages/sat4j/org.sat4j.pb.jar CuttingPlanes" *)
+
+(* let pseudo_boolean_solver = ref "java -jar /usr/share/java/org.sat4j.pb.jar CuttingPlanes" *)
+(* let pseudo_boolean_solver = ref "java -jar /usr/share/java/org.sat4j.pb.jar" *)
+(* let pseudo_boolean_solver = ref "clasp" *)
+(* let pseudo_boolean_solver = ref "/home/monniaux/progs/CP/open-wbo/open-wbo_static -formula=1" *)
+(* let pseudo_boolean_solver = ref "/home/monniaux/progs/CP/naps/naps" *)
+(* let pseudo_boolean_solver = ref "/home/monniaux/progs/CP/minisatp/build/release/bin/minisatp" *)
+(* let pseudo_boolean_solver = ref "java -jar sat4j-pb.jar CuttingPlanesStar" *)
+let pseudo_boolean_solver = ref "pb_solver"
+
+let pseudo_boolean_scheduler pb_type problem =
+ try
+ let filename_in = "problem.opb" in
+ (* needed only if not using stdout and filename_out = "problem.sol" *)
+ let mapper =
+ with_out_channel (open_out filename_in)
+ (fun opb_problem ->
+ pseudo_boolean_print_problem opb_problem problem pb_type) in
+ Some (with_in_channel
+ (Unix.open_process_in (!pseudo_boolean_solver ^ " " ^ filename_in))
+ (fun opb_solution -> adjust_check_solution mapper (pseudo_boolean_read_solution mapper opb_solution)))
+ with
+ | Unschedulable -> None;;
+
+let rec reoptimizing_scheduler (scheduler : scheduler) (previous_solution : solution) (problem : problem) =
+ if (get_max_latency previous_solution)>1 then
+ begin
+ Printf.printf "reoptimizing < %d\n" (get_max_latency previous_solution);
+ flush stdout;
+ match scheduler
+ { problem with max_latency = (get_max_latency previous_solution)-1 }
+ with
+ | None -> previous_solution
+ | Some solution -> reoptimizing_scheduler scheduler solution problem
+ end
+ else previous_solution;;
+
+let smt_var i = Printf.sprintf "t%d" i
+
+let is_resource_used problem j =
+ try
+ Array.iter (fun usages ->
+ if usages.(j) > 0
+ then raise Exit) problem.instruction_usages;
+ false
+ with Exit -> true;;
+
+let smt_use_quantifiers = false
+
+let smt_print_problem channel problem =
+ let nr_instructions = get_nr_instructions problem in
+ let gen_smt_resource_constraint time j =
+ output_string channel "(<= (+";
+ Array.iteri
+ (fun i usages ->
+ let usage=usages.(j) in
+ if usage > 0
+ then Printf.fprintf channel " (ite (= %s %s) %d 0)"
+ time (smt_var i) usage)
+ problem.instruction_usages;
+ Printf.fprintf channel ") %d)" problem.resource_bounds.(j)
+ in
+ output_string channel "(set-option :produce-models true)\n";
+ for i=0 to nr_instructions
+ do
+ Printf.fprintf channel "(declare-const %s Int)\n" (smt_var i);
+ Printf.fprintf channel "(assert (>= %s 0))\n" (smt_var i)
+ done;
+ for i=0 to nr_instructions-1
+ do
+ Printf.fprintf channel "(assert (< %s %s))\n"
+ (smt_var i) (smt_var nr_instructions)
+ done;
+ (if problem.max_latency > 0
+ then Printf.fprintf channel "(assert (<= %s %d))\n"
+ (smt_var nr_instructions) problem.max_latency);
+ List.iter (fun ctr ->
+ Printf.fprintf channel "(assert (>= (- %s %s) %d))\n"
+ (smt_var ctr.instr_to)
+ (smt_var ctr.instr_from)
+ ctr.latency) problem.latency_constraints;
+ for j=0 to (Array.length problem.resource_bounds)-1
+ do
+ if is_resource_used problem j
+ then
+ begin
+ if smt_use_quantifiers
+ then
+ begin
+ Printf.fprintf channel
+ "; resource #%d <= %d\n(assert (forall ((t Int)) "
+ j problem.resource_bounds.(j);
+ gen_smt_resource_constraint "t" j;
+ output_string channel "))\n"
+ end
+ else
+ begin
+ (if problem.max_latency < 0
+ then failwith "quantifier explosion needs max latency");
+ for t=0 to problem.max_latency
+ do
+ Printf.fprintf channel
+ "; resource #%d <= %d at t=%d\n(assert "
+ j problem.resource_bounds.(j) t;
+ gen_smt_resource_constraint (string_of_int t) j;
+ output_string channel ")\n"
+ done
+ end
+ end
+ done;
+ output_string channel "(check-sat)(get-model)\n";;
+
+
+let ilp_print_problem channel problem pb_type =
+ let deadline = problem.max_latency in
+ assert (deadline > 0);
+ let nr_instructions = get_nr_instructions problem
+ and nr_resources = get_nr_resources problem
+ and successors = get_successors problem
+ and predecessors = get_predecessors problem in
+ let earliest_dates = get_earliest_dates predecessors
+ and latest_dates = get_latest_dates deadline successors in
+
+ let pb_var i t =
+ Printf.sprintf "x%d_%d" i t in
+
+ let gen_latency_constraint i_to i_from latency t_to =
+ Printf.fprintf channel "\\ t[%d] - t[%d] >= %d when t[%d]=%d\n"
+ i_to i_from latency i_to t_to;
+ Printf.fprintf channel "c_%d_%d_%d_%d: "
+ i_to i_from latency t_to;
+ for t_from=earliest_dates.(i_from) to
+ int_min latest_dates.(i_from) (t_to - latency)
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i_from t_from)
+ done;
+ Printf.fprintf channel "-1 %s " (pb_var i_to t_to);
+ output_string channel ">= 0\n"
+
+ and gen_dual_latency_constraint i_to i_from latency t_from =
+ Printf.fprintf channel "\\ t[%d] - t[%d] >= %d when t[%d]=%d\n"
+ i_to i_from latency i_to t_from;
+ Printf.fprintf channel "d_%d_%d_%d_%d: "
+ i_to i_from latency t_from;
+ for t_to=int_max earliest_dates.(i_to) (t_from + latency)
+ to latest_dates.(i_to)
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i_to t_to)
+ done;
+ Printf.fprintf channel "-1 %s " (pb_var i_from t_from);
+ Printf.fprintf channel ">= 0\n"
+
+ and gen_delta_constraint i_from i_to latency =
+ if delta_encoding
+ then Printf.fprintf channel "l_%d_%d_%d: +1 t%d -1 t%d >= %d\n"
+ i_from i_to latency i_to i_from latency
+
+ in
+
+ Printf.fprintf channel "\\ nr_instructions=%d deadline=%d\n" nr_instructions deadline;
+ begin
+ match pb_type with
+ | SATISFIABILITY -> output_string channel "Minimize dummy: 0\n"
+ | OPTIMIZATION ->
+ Printf.fprintf channel "Minimize\nmakespan: t%d\n" nr_instructions
+ end;
+ output_string channel "Subject To\n";
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ let early = earliest_dates.(i) and late= latest_dates.(i) in
+ Printf.fprintf channel "\\ t[%d] in %d..%d\ntimes%d: " i early late i;
+ for t=early to late
+ do
+ Printf.fprintf channel "+1 %s " (pb_var i t)
+ done;
+ Printf.fprintf channel "= 1\n"
+ done;
+
+ for t=0 to deadline-1
+ do
+ for j=0 to nr_resources-1
+ do
+ let bound = problem.resource_bounds.(j)
+ and coeffs = ref [] in
+ for i=0 to nr_instructions-1
+ do
+ let usage = problem.instruction_usages.(i).(j) in
+ if t >= earliest_dates.(i) && t <= latest_dates.(i)
+ && usage > 0
+ then coeffs := (i, usage) :: !coeffs
+ done;
+ if !coeffs <> [] then
+ begin
+ Printf.fprintf channel "\\ resource #%d at t=%d <= %d\nr%d_%d: " j t bound j t;
+ List.iter (fun (i, usage) ->
+ Printf.fprintf channel "%+d %s " (-usage) (pb_var i t)) !coeffs;
+ Printf.fprintf channel ">= %d\n" (-bound)
+ end
+ done
+ done;
+
+ List.iter
+ (fun ctr ->
+ if ctr.instr_to < nr_instructions then
+ begin
+ gen_delta_constraint ctr.instr_from ctr.instr_to ctr.latency;
+ begin
+ if direct_encoding
+ then
+ for t_to=earliest_dates.(ctr.instr_to) to latest_dates.(ctr.instr_to)
+ do
+ gen_latency_constraint ctr.instr_to ctr.instr_from ctr.latency t_to
+ done
+ end;
+ begin
+ if reverse_encoding
+ then
+ for t_from=earliest_dates.(ctr.instr_from) to latest_dates.(ctr.instr_from)
+ do
+ gen_dual_latency_constraint ctr.instr_to ctr.instr_from ctr.latency t_from
+ done
+ end
+ end
+ ) problem.latency_constraints;
+
+ begin
+ match pb_type with
+ | SATISFIABILITY -> ()
+ | OPTIMIZATION ->
+ let final_latencies = Array.make nr_instructions 1 in
+ List.iter (fun (i, latency) ->
+ final_latencies.(i) <- int_max final_latencies.(i) latency)
+ predecessors.(nr_instructions);
+ for i_from = 0 to nr_instructions -1
+ do
+ gen_delta_constraint i_from nr_instructions final_latencies.(i_from)
+ done;
+ for t_to=earliest_dates.(nr_instructions) to deadline
+ do
+ for i_from = 0 to nr_instructions -1
+ do
+ gen_latency_constraint nr_instructions i_from final_latencies.(i_from) t_to
+ done
+ done
+ end;
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ Printf.fprintf channel "ct%d : -1 t%d" i i;
+ let early = earliest_dates.(i) and late= latest_dates.(i) in
+ for t=early to late do
+ Printf.fprintf channel " +%d %s" t (pb_var i t)
+ done;
+ output_string channel " = 0\n"
+ done;
+ output_string channel "Bounds\n";
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ let early = earliest_dates.(i) and late= latest_dates.(i) in
+ begin
+ Printf.fprintf channel "%d <= t%d <= %d\n" early i late;
+ if true then
+ for t=early to late do
+ Printf.fprintf channel "0 <= %s <= 1\n" (pb_var i t)
+ done
+ end
+ done;
+ output_string channel "Integer\n";
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ Printf.fprintf channel "t%d " i
+ done;
+ output_string channel "\nBinary\n";
+ for i=0 to (match pb_type with
+ | OPTIMIZATION -> nr_instructions
+ | SATISFIABILITY -> nr_instructions-1)
+ do
+ let early = earliest_dates.(i) and late= latest_dates.(i) in
+ for t=early to late do
+ output_string channel (pb_var i t);
+ output_string channel " "
+ done;
+ output_string channel "\n"
+ done;
+ output_string channel "End\n";
+ {
+ mapper_pb_type = pb_type;
+ mapper_nr_instructions = nr_instructions;
+ mapper_nr_pb_variables = 0;
+ mapper_earliest_dates = earliest_dates;
+ mapper_latest_dates = latest_dates;
+ mapper_var_offsets = [| |];
+ mapper_final_predecessors = predecessors.(nr_instructions)
+ };;
+
+(* Guess what? Cplex sometimes outputs 11.000000004 instead of integer 11 *)
+
+let positive_float_round x = truncate (x +. 0.5)
+
+let float_round (x : float) : int =
+ if x > 0.0
+ then positive_float_round x
+ else - (positive_float_round (-. x))
+
+let rounded_int_of_string x = float_round (float_of_string x)
+
+let ilp_read_solution mapper channel =
+ let times = Array.make
+ (match mapper.mapper_pb_type with
+ | OPTIMIZATION -> 1+mapper.mapper_nr_instructions
+ | SATISFIABILITY -> mapper.mapper_nr_instructions) (-1) in
+ try
+ while true do
+ let line = input_line channel in
+ ( if (String.length line) < 3
+ then failwith (Printf.sprintf "bad ilp output: length(line) < 3: %s" line));
+ match String.get line 0 with
+ | 'x' -> ()
+ | 't' -> let space =
+ try String.index line ' '
+ with Not_found ->
+ failwith "bad ilp output: no t variable number"
+ in
+ let tnumber =
+ try int_of_string (String.sub line 1 (space-1))
+ with Failure _ ->
+ failwith "bad ilp output: not a variable number"
+ in
+ (if tnumber < 0 || tnumber >= (Array.length times)
+ then failwith (Printf.sprintf "bad ilp output: not a correct variable number: %d (%d)" tnumber (Array.length times)));
+ let value =
+ let s = String.sub line (space+1) ((String.length line)-space-1) in
+ try rounded_int_of_string s
+ with Failure _ ->
+ failwith (Printf.sprintf "bad ilp output: not a time number (%s)" s)
+ in
+ (if value < 0
+ then failwith "bad ilp output: negative time");
+ times.(tnumber) <- value
+ | '#' -> ()
+ | '0' -> ()
+ | _ -> failwith (Printf.sprintf "bad ilp output: bad variable initial, line = %s" line)
+ done;
+ assert false
+ with End_of_file ->
+ Array.iteri (fun i x ->
+ if i<(Array.length times)-1
+ && x<0 then raise Unschedulable) times;
+ times;;
+
+let ilp_solver = ref "ilp_solver"
+
+let problem_nr = ref 0
+
+let ilp_scheduler pb_type problem =
+ try
+ let filename_in = Printf.sprintf "problem%05d.lp" !problem_nr
+ and filename_out = Printf.sprintf "problem%05d.sol" !problem_nr in
+ incr problem_nr;
+ let mapper = with_out_channel (open_out filename_in)
+ (fun opb_problem -> ilp_print_problem opb_problem problem pb_type) in
+
+ begin
+ match Unix.system (!ilp_solver ^ " " ^ filename_in ^ " " ^ filename_out) with
+ | Unix.WEXITED 0 ->
+ Some (with_in_channel
+ (open_in filename_out)
+ (fun opb_solution ->
+ adjust_check_solution mapper
+ (ilp_read_solution mapper opb_solution)))
+ | Unix.WEXITED _ -> failwith "failed to start ilp solver"
+ | _ -> None
+ end
+ with
+ | Unschedulable -> None;;
+
+let current_utime_all () =
+ let t = Unix.times() in
+ t.Unix.tms_cutime +. t.Unix.tms_utime;;
+
+let utime_all_fn fn arg =
+ let utime_start = current_utime_all () in
+ let output = fn arg in
+ let utime_end = current_utime_all () in
+ (output, utime_end -. utime_start);;
+
+let cascaded_scheduler (problem : problem) =
+ let (some_initial_solution, list_scheduler_time) =
+ utime_all_fn (validated_scheduler list_scheduler) problem in
+ match some_initial_solution with
+ | None -> None
+ | Some initial_solution ->
+ let (solution, reoptimizing_time) = utime_all_fn (reoptimizing_scheduler (validated_scheduler (ilp_scheduler SATISFIABILITY)) initial_solution) problem in
+ begin
+ let latency2 = get_max_latency solution
+ and latency1 = get_max_latency initial_solution in
+ Printf.printf "postpass %s: %d, %d, %d, %g, %g\n"
+ (if latency2 < latency1 then "REOPTIMIZED" else "unchanged")
+ (get_nr_instructions problem)
+ latency1 latency2
+ list_scheduler_time reoptimizing_time;
+ flush stdout
+ end;
+ Some solution;;
+
+let scheduler_by_name name =
+ match name with
+ | "ilp" -> validated_scheduler cascaded_scheduler
+ | "list" -> validated_scheduler list_scheduler
+ | "revlist" -> validated_scheduler reverse_list_scheduler
+ | "regpres" -> validated_scheduler reg_pres_scheduler
+ | "regpres_bis" -> validated_scheduler reg_pres_scheduler_bis
+ | "greedy" -> greedy_scheduler
+ | s -> failwith ("unknown scheduler: " ^ s);;
diff --git a/scheduling/InstructionScheduler.mli b/scheduling/InstructionScheduler.mli
new file mode 100644
index 00000000..29b05b6c
--- /dev/null
+++ b/scheduling/InstructionScheduler.mli
@@ -0,0 +1,135 @@
+(** Schedule instructions on a synchronized pipeline
+by David Monniaux, CNRS, VERIMAG *)
+
+(** A latency constraint: instruction number [instr_to] should be scheduled at least [latency] clock ticks before [instr_from].
+
+It is possible to specify [latency]=0, meaning that [instr_to] can be scheduled at the same clock tick as [instr_from], but not before.
+
+[instr_to] can be the special value equal to the number of instructions, meaning that it refers to the final output latency. *)
+type latency_constraint = {
+ instr_from : int;
+ instr_to : int;
+ latency : int;
+ }
+
+(** A scheduling problem.
+
+In addition to the latency constraints, the resource constraints should be satisfied: at every clock tick, the sum of vectors of resources used by the instructions scheduled at that tick does not exceed the resource bounds.
+*)
+type problem = {
+ max_latency : int;
+ (** An optional maximal total latency of the problem, after which the problem is deemed not schedulable. -1 means there should be no maximum. *)
+
+ resource_bounds : int array;
+ (** An array of number of units available indexed by the kind of resources to be allocated. It can be empty, in which case the problem is scheduling without resource constraints. *)
+
+ live_regs_entry : Registers.Regset.t;
+ (** The set of live pseudo-registers at entry. *)
+
+ typing : RTLtyping.regenv;
+ (** Register type map. *)
+
+ reference_counting : ((Registers.reg, int * int) Hashtbl.t
+ * ((Registers.reg * bool) list array)) option;
+ (** See BTLScheduleraux.reference_counting. *)
+
+ instruction_usages: int array array;
+ (** At index {i i} the vector of resources used by instruction number {i i}. It must be the same length as [resource_bounds] *)
+
+ latency_constraints : latency_constraint list
+ (** The latency constraints that must be satisfied *)
+ };;
+
+(** Print problem for human readability. *)
+val print_problem : out_channel -> problem -> unit;;
+
+(** Get the number of instructions in a problem *)
+val get_nr_instructions : problem -> int;;
+
+(** Get the number of resources in a problem *)
+val get_nr_resources : problem -> int;;
+
+(** Scheduling solution. For {i n} instructions to schedule, and 0≤{i i}<{i n}, position {i i} contains the time to which instruction {i i} should be scheduled. Position {i n} contains the final output latency. *)
+type solution = int array
+
+(** A scheduling algorithm.
+The return value [Some x] is a solution [x].
+[None] means that scheduling failed. *)
+type scheduler = problem -> solution option;;
+
+(* DISABLED
+(** Schedule the problem optimally by constraint solving using the Gecode solver. *)
+external gecode_scheduler : problem -> solution option
+ = "caml_gecode_schedule_instr"
+ *)
+
+(** Get the number the last scheduling time used for an instruction in a solution.
+@return The last clock tick used *)
+val maximum_slot_used : solution -> int
+
+(** Validate that a solution is truly a solution of a scheduling problem.
+@raise Failure if validation fails *)
+val check_schedule : problem -> solution -> unit
+
+(** Schedule the problem using a greedy list scheduling algorithm, from the start.
+The first (according to instruction ordering) instruction that is ready (according to the latency constraints) is scheduled at the current clock tick.
+Once a clock tick is full go to the next.
+
+@return [Some solution] when a solution is found, [None] if not. *)
+val list_scheduler : problem -> solution option
+
+(** WIP : Same as list_scheduler, but schedules instructions which decrease
+register pressure when it gets too high. *)
+val reg_pres_scheduler : problem -> solution option
+
+val reg_pres_scheduler_bis : problem -> solution option
+
+(** Schedule the problem using the order of instructions without any reordering *)
+val greedy_scheduler : problem -> solution option
+
+(** Schedule a problem using a scheduler applied in the opposite direction, e.g. for list scheduling from the end instead of the start. BUGGY *)
+val schedule_reversed : scheduler -> problem -> int array option
+
+(** Schedule a problem from the end using a list scheduler. BUGGY *)
+val reverse_list_scheduler : problem -> int array option
+
+(** Check that a problem is well-formed.
+@raise Failure if validation fails *)
+val check_problem : problem -> unit
+
+(** Apply a scheduler and validate the result against the input problem.
+@return The solution found
+@raise Failure if validation fails *)
+val validated_scheduler : scheduler -> problem -> solution option;;
+
+(** Get max latency from solution
+@return Max latency *)
+val get_max_latency : solution -> int;;
+
+(** Get the length of a maximal critical path
+@return Max length *)
+val maximum_critical_path : problem -> int;;
+
+(** Apply line scheduler then advanced solver
+@return A solution if found *)
+val cascaded_scheduler : problem -> solution option;;
+
+val show_date_ranges : problem -> unit;;
+
+type pseudo_boolean_problem_type =
+ | SATISFIABILITY
+ | OPTIMIZATION;;
+
+type pseudo_boolean_mapper
+val pseudo_boolean_print_problem : out_channel -> problem -> pseudo_boolean_problem_type -> pseudo_boolean_mapper;;
+val pseudo_boolean_read_solution : pseudo_boolean_mapper -> in_channel -> solution;;
+val pseudo_boolean_scheduler : pseudo_boolean_problem_type -> problem -> solution option;;
+
+val smt_print_problem : out_channel -> problem -> unit;;
+
+val ilp_print_problem : out_channel -> problem -> pseudo_boolean_problem_type -> pseudo_boolean_mapper;;
+
+val ilp_scheduler : pseudo_boolean_problem_type -> problem -> solution option;;
+
+(** Schedule a problem using a scheduler given by a string name *)
+val scheduler_by_name : string -> problem -> int array option;;
diff --git a/scheduling/PrintBTL.ml b/scheduling/PrintBTL.ml
new file mode 100644
index 00000000..64e83651
--- /dev/null
+++ b/scheduling/PrintBTL.ml
@@ -0,0 +1,118 @@
+open Printf
+open Camlcoq
+open Datatypes
+open Maps
+open BTL
+open PrintAST
+open DebugPrint
+open BTLtypes
+
+(* Printing of BTL code *)
+
+let reg pp r = fprintf pp "x%d" (P.to_int r)
+
+let rec regs pp = function
+ | [] -> ()
+ | [ r ] -> reg pp r
+ | r1 :: rl -> fprintf pp "%a, %a" reg r1 regs rl
+
+let ros pp = function
+ | Coq_inl r -> reg pp r
+ | Coq_inr s -> fprintf pp "\"%s\"" (extern_atom s)
+
+let print_succ pp s = fprintf pp "\tsucc %d\n" (P.to_int s)
+
+let print_pref pp pref = fprintf pp "%s" pref
+
+let rec print_iblock pp is_rec pref ib =
+ match ib with
+ | Bnop None ->
+ print_pref pp pref;
+ fprintf pp "??: Bnop None\n"
+ | Bnop (Some iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bnop\n" iinfo.inumb
+ | Bop (op, args, res, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bop: %a = %a\n" iinfo.inumb reg res
+ (PrintOp.print_operation reg)
+ (op, args)
+ | Bload (trap, chunk, addr, args, dst, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bload: %a = %s[%a]%a\n" iinfo.inumb reg dst
+ (name_of_chunk chunk)
+ (PrintOp.print_addressing reg)
+ (addr, args) print_trapping_mode trap
+ | Bstore (chunk, addr, args, src, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bstore: %s[%a] = %a\n" iinfo.inumb (name_of_chunk chunk)
+ (PrintOp.print_addressing reg)
+ (addr, args) reg src
+ | BF (Bcall (sg, fn, args, res, s), iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bcall: %a = %a(%a)\n" iinfo.inumb reg res ros fn regs args;
+ print_succ pp s
+ | BF (Btailcall (sg, fn, args), iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Btailcall: %a(%a)\n" iinfo.inumb ros fn regs args
+ | BF (Bbuiltin (ef, args, res, s), iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bbuiltin: %a = %s(%a)\n" iinfo.inumb
+ (print_builtin_res reg) res (name_of_external ef)
+ (print_builtin_args reg) args;
+ print_succ pp s
+ | Bcond (cond, args, ib1, ib2, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bcond: (%a) (prediction: %s)\n" iinfo.inumb
+ (PrintOp.print_condition reg)
+ (cond, args)
+ (match iinfo.opt_info with
+ | None -> "none"
+ | Some true -> "branch"
+ | Some false -> "fallthrough");
+ let pref' = pref ^ " " in
+ fprintf pp "%sifso = [\n" pref;
+ if is_rec then print_iblock pp is_rec pref' ib1 else fprintf pp "...\n";
+ fprintf pp "%s]\n" pref;
+ fprintf pp "%sifnot = [\n" pref;
+ if is_rec then print_iblock pp is_rec pref' ib2 else fprintf pp "...\n";
+ fprintf pp "%s]\n" pref
+ | BF (Bjumptable (arg, tbl), iinfo) ->
+ let tbl = Array.of_list tbl in
+ print_pref pp pref;
+ fprintf pp "%d: Bjumptable: (%a)\n" iinfo.inumb reg arg;
+ for i = 0 to Array.length tbl - 1 do
+ fprintf pp "\t\tcase %d: goto %d\n" i (P.to_int tbl.(i))
+ done
+ | BF (Breturn None, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Breturn\n" iinfo.inumb
+ | BF (Breturn (Some arg), iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Breturn: %a\n" iinfo.inumb reg arg
+ | BF (Bgoto s, iinfo) ->
+ print_pref pp pref;
+ fprintf pp "%d: Bgoto: %d\n" iinfo.inumb (P.to_int s)
+ | Bseq (ib1, ib2) ->
+ if is_rec then (
+ print_iblock pp is_rec pref ib1;
+ print_iblock pp is_rec pref ib2)
+ else fprintf pp "Bseq...\n"
+
+let print_btl_inst pp ib =
+ if !debug_flag then print_iblock pp false " " ib else ()
+
+let print_btl_code pp btl =
+ if !debug_flag then (
+ fprintf pp "\n";
+ List.iter
+ (fun (n, ibf) ->
+ fprintf pp "[BTL Liveness] ";
+ print_regset ibf.input_regs;
+ fprintf pp "\n";
+ fprintf pp "[BTL block %d with inumb %d]\n" (P.to_int n) ibf.binfo.bnumb;
+ print_iblock pp true " " ibf.entry;
+ fprintf pp "\n")
+ (PTree.elements btl);
+ fprintf pp "\n")
+ else ()
diff --git a/scheduling/RTLtoBTL.v b/scheduling/RTLtoBTL.v
new file mode 100644
index 00000000..309c616e
--- /dev/null
+++ b/scheduling/RTLtoBTL.v
@@ -0,0 +1,27 @@
+Require Import Coqlib Maps.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad BTL.
+Require Export BTLmatchRTL BTL_Livecheck.
+
+Require Import Errors Linking.
+
+(** External oracle *)
+Axiom rtl2btl: RTL.function -> BTL.code * node * (PTree.t node).
+
+Extract Constant rtl2btl => "RTLtoBTLaux.rtl2btl".
+
+Local Open Scope error_monad_scope.
+
+Definition transf_function (f: RTL.function) : res BTL.function :=
+ let (tcte, dupmap) := rtl2btl f in
+ let (tc, te) := tcte in
+ let f' := BTL.mkfunction (RTL.fn_sig f) (RTL.fn_params f) (RTL.fn_stacksize f) tc te in
+ do u <- verify_function dupmap f' f;
+ do u <- liveness_checker f';
+ OK f'.
+
+Definition transf_fundef (f: RTL.fundef) : res fundef :=
+ transf_partial_fundef transf_function f.
+
+Definition transf_program (p: RTL.program) : res program :=
+ transform_partial_program transf_fundef p.
diff --git a/scheduling/RTLtoBTLaux.ml b/scheduling/RTLtoBTLaux.ml
new file mode 100644
index 00000000..0ed6ec61
--- /dev/null
+++ b/scheduling/RTLtoBTLaux.ml
@@ -0,0 +1,119 @@
+open Maps
+open RTL
+open BTL
+open PrintBTL
+open RTLcommonaux
+open DebugPrint
+open BTLtypes
+open BTLcommonaux
+
+let encaps_final inst osucc =
+ match inst with
+ | BF _ -> inst
+ | _ -> Bseq (inst, BF (Bgoto (get_some @@ osucc), def_iinfo ()))
+
+let translate_inst (iinfo : BTL.inst_info) inst is_final =
+ let osucc = ref None in
+ let btli =
+ match inst with
+ | Inop s ->
+ osucc := Some s;
+ Bnop (Some iinfo)
+ | Iop (op, lr, rd, s) ->
+ osucc := Some s;
+ Bop (op, lr, rd, iinfo)
+ | Iload (trap, chk, addr, lr, rd, s) ->
+ osucc := Some s;
+ Bload (trap, chk, addr, lr, rd, iinfo)
+ | Istore (chk, addr, lr, src, s) ->
+ osucc := Some s;
+ Bstore (chk, addr, lr, src, iinfo)
+ | Icall (sign, fn, lr, rd, s) -> BF (Bcall (sign, fn, lr, rd, s), iinfo)
+ | Itailcall (sign, fn, lr) -> BF (Btailcall (sign, fn, lr), iinfo)
+ | Ibuiltin (ef, lr, rd, s) -> BF (Bbuiltin (ef, lr, rd, s), iinfo)
+ | Icond (cond, lr, ifso, ifnot, info) ->
+ osucc := Some ifnot;
+ iinfo.opt_info <- info;
+ Bcond
+ ( cond,
+ lr,
+ BF (Bgoto ifso, def_iinfo ()),
+ Bnop None,
+ iinfo )
+ | Ijumptable (arg, tbl) -> BF (Bjumptable (arg, tbl), iinfo)
+ | Ireturn oreg -> BF (Breturn oreg, iinfo)
+ in
+ if is_final then encaps_final btli !osucc else btli
+
+let translate_function code entry join_points liveness (typing : RTLtyping.regenv) =
+ let reached = ref (PTree.map (fun n i -> false) code) in
+ let btl_code = ref PTree.empty in
+ let rec build_btl_tree e =
+ if get_some @@ PTree.get e !reached then ()
+ else (
+ reached := PTree.set e true !reached;
+ let next_nodes = ref [] in
+ let last = ref None in
+ let rec build_btl_block n =
+ let inst = get_some @@ PTree.get n code in
+ let psucc = predicted_successor inst in
+ let iinfo = mk_iinfo (p2i n) None in
+ let succ =
+ match psucc with
+ | Some ps ->
+ if get_some @@ PTree.get ps join_points then None else Some ps
+ | None -> None
+ in
+ match succ with
+ | Some s -> (
+ match inst with
+ | Icond (cond, lr, ifso, ifnot, info) ->
+ assert (s = ifnot);
+ next_nodes := !next_nodes @ non_predicted_successors inst psucc;
+ iinfo.opt_info <- info;
+ Bseq
+ ( Bcond
+ (cond, lr, BF (Bgoto ifso, def_iinfo ()), Bnop None, iinfo),
+ build_btl_block s )
+ | _ -> Bseq (translate_inst iinfo inst false, build_btl_block s))
+ | None ->
+ debug "BLOCK END.\n";
+ next_nodes := !next_nodes @ successors_inst inst;
+ last := Some inst;
+ translate_inst iinfo inst true
+ in
+ let ib = build_btl_block e in
+ let succs = !next_nodes in
+
+ let inputs = get_some @@ PTree.get e liveness in
+ let soutput = get_outputs liveness e (get_some @@ !last) in
+
+ let bi = mk_binfo (p2i e) soutput typing in
+ let ibf = { entry = ib; input_regs = inputs; binfo = bi } in
+ btl_code := PTree.set e ibf !btl_code;
+ List.iter build_btl_tree succs)
+ in
+ build_btl_tree entry;
+ !btl_code
+
+let rtl2btl (f : RTL.coq_function) =
+ let code = f.fn_code in
+ let entry = f.fn_entrypoint in
+ let join_points = get_join_points code entry in
+ let liveness = analyze f in
+ let typing = get_ok @@ RTLtyping.type_function f in
+ let btl = translate_function code entry join_points liveness typing in
+ let dm = PTree.map (fun n i -> n) btl in
+ (*debug_flag := true;*)
+ debug "Entry %d\n" (p2i entry);
+ debug "RTL Code: ";
+ print_code code;
+ debug "BTL Code:\n";
+ print_btl_code stderr btl;
+ (*debug_flag := false;*)
+ debug "Dupmap:\n";
+ print_ptree print_pint dm;
+ debug "Join points: ";
+ print_true_nodes join_points;
+ debug "\n";
+ ((btl, entry), dm)
diff --git a/scheduling/RTLtoBTLproof.v b/scheduling/RTLtoBTLproof.v
new file mode 100644
index 00000000..0ca93bce
--- /dev/null
+++ b/scheduling/RTLtoBTLproof.v
@@ -0,0 +1,758 @@
+Require Import Coqlib Maps Lia.
+Require Import AST Integers Values Events Memory Globalenvs Smallstep.
+Require Import RTL Op Registers OptionMonad BTL.
+
+Require Import Errors Linking RTLtoBTL.
+
+Require Import Linking.
+
+(** * Normalization of BTL iblock for simulation of RTL
+
+Below [normRTL] normalizes the representation of BTL blocks,
+in order to represent as sequences of RTL instructions.
+
+This eases the
+
+*)
+
+Definition is_RTLatom (ib: iblock): bool :=
+ match ib with
+ | Bseq _ _ | Bcond _ _ _ _ _ | Bnop None => false
+ | _ => true
+ end.
+
+Definition is_RTLbasic (ib: iblock): bool :=
+ match ib with
+ | Bseq _ _ | Bcond _ _ _ _ _ | Bnop None | BF _ _ => false
+ | _ => true
+ end.
+
+(** The strict [is_normRTL] property specifying the ouput of [normRTL] below *)
+Inductive is_normRTL: iblock -> Prop :=
+ | norm_Bseq ib1 ib2:
+ is_RTLbasic ib1 = true ->
+ is_normRTL ib2 ->
+ is_normRTL (Bseq ib1 ib2)
+ | norm_Bcond cond args ib1 ib2 i:
+ is_normRTL ib1 ->
+ is_normRTL ib2 ->
+ is_normRTL (Bcond cond args ib1 ib2 i)
+ | norm_others ib:
+ is_RTLatom ib = true ->
+ is_normRTL ib
+ .
+Local Hint Constructors is_normRTL: core.
+
+(** Weaker version allowing for trailing [Bnop None]. *)
+Inductive is_wnormRTL: iblock -> Prop :=
+ | wnorm_Bseq ib1 ib2:
+ is_RTLbasic ib1 = true ->
+ (ib2 <> Bnop None -> is_wnormRTL ib2) ->
+ is_wnormRTL (Bseq ib1 ib2)
+ | wnorm_Bcond cond args ib1 ib2 iinfo:
+ (ib1 <> Bnop None -> is_wnormRTL ib1) ->
+ (ib2 <> Bnop None -> is_wnormRTL ib2) ->
+ is_wnormRTL (Bcond cond args ib1 ib2 iinfo)
+ | wnorm_others ib:
+ is_RTLatom ib = true ->
+ is_wnormRTL ib
+ .
+Local Hint Constructors is_wnormRTL: core.
+
+(* NB: [k] is a "continuation" (e.g. semantically [normRTLrec ib k] is like [Bseq ib k]) *)
+Fixpoint normRTLrec (ib: iblock) (k: iblock): iblock :=
+ match ib with
+ | Bseq ib1 ib2 => normRTLrec ib1 (normRTLrec ib2 k)
+ | Bcond cond args ib1 ib2 iinfo =>
+ Bcond cond args (normRTLrec ib1 k) (normRTLrec ib2 k) iinfo
+ | BF fin iinfo => BF fin iinfo
+ | Bnop None => k
+ | ib => Bseq ib k
+ end.
+
+Definition normRTL ib := normRTLrec ib (Bnop None).
+
+Lemma normRTLrec_wcorrect ib: forall k,
+ (k <> (Bnop None) -> is_wnormRTL k) ->
+ (normRTLrec ib k) <> Bnop None ->
+ is_wnormRTL (normRTLrec ib k).
+Proof.
+ induction ib; simpl; intros; repeat autodestruct; auto.
+Qed.
+
+Lemma normRTL_wcorrect ib:
+ (normRTL ib) <> Bnop None ->
+ is_wnormRTL (normRTL ib).
+Proof.
+ intros; eapply normRTLrec_wcorrect; eauto.
+Qed.
+
+Lemma is_join_opt_None {A} (opc1 opc2: option A):
+ is_join_opt opc1 opc2 None -> opc1 = None /\ opc2 = None.
+Proof.
+ intros X. inv X; auto.
+Qed.
+
+Lemma match_iblock_None_not_Bnop dupmap cfg isfst pc ib:
+ match_iblock dupmap cfg isfst pc ib None -> ib <> Bnop None.
+Proof.
+ intros X; inv X; try congruence.
+Qed.
+Local Hint Resolve match_iblock_None_not_Bnop: core.
+
+Lemma is_wnormRTL_normRTL dupmap cfg ib:
+ is_wnormRTL ib ->
+ forall isfst pc
+ (MIB: match_iblock dupmap cfg isfst pc ib None),
+ is_normRTL ib.
+Proof.
+ induction 1; simpl; intros; auto; try (inv MIB); eauto.
+ (* Bcond *)
+ destruct (is_join_opt_None opc1 opc2); subst; eauto.
+ econstructor; eauto.
+Qed.
+
+Local Hint Constructors iblock_istep: core.
+Lemma normRTLrec_iblock_istep_correct tge sp ib rs0 m0 rs1 m1 ofin1:
+ forall (ISTEP: iblock_istep tge sp rs0 m0 ib rs1 m1 ofin1)
+ k ofin2 rs2 m2
+ (CONT: match ofin1 with
+ | None => iblock_istep tge sp rs1 m1 k rs2 m2 ofin2
+ | Some fin1 => rs2=rs1 /\ m2=m1 /\ ofin2=Some fin1
+ end),
+ iblock_istep tge sp rs0 m0 (normRTLrec ib k) rs2 m2 ofin2.
+Proof.
+ induction 1; simpl; intuition subst; eauto.
+ - (* Bnop *) autodestruct; eauto.
+ - (* Bop *) repeat econstructor; eauto.
+ - (* Bload *) inv LOAD.
+ + repeat econstructor; eauto.
+ + do 2 (econstructor; eauto).
+ eapply has_loaded_default; eauto.
+ - (* Bcond *) repeat econstructor; eauto.
+ destruct ofin; intuition subst;
+ destruct b; eapply IHISTEP; eauto.
+Qed.
+
+Lemma normRTL_iblock_istep_correct tge sp ib rs0 m0 rs1 m1 ofin:
+ iblock_istep tge sp rs0 m0 ib rs1 m1 ofin ->
+ iblock_istep tge sp rs0 m0 (normRTL ib) rs1 m1 ofin.
+Proof.
+ intros; eapply normRTLrec_iblock_istep_correct; eauto.
+ destruct ofin; simpl; auto.
+Qed.
+
+Lemma normRTLrec_iblock_istep_run_None tge sp ib:
+ forall rs0 m0 k
+ (CONT: match iblock_istep_run tge sp ib rs0 m0 with
+ | Some (out rs1 m1 ofin) =>
+ ofin = None /\
+ iblock_istep_run tge sp k rs1 m1 = None
+ | _ => True
+ end),
+ iblock_istep_run tge sp (normRTLrec ib k) rs0 m0 = None.
+Proof.
+ induction ib; simpl; intros; subst; intuition (try discriminate).
+ - (* Bnop *)
+ intros. autodestruct; auto.
+ - (* Bop *)
+ intros; repeat autodestruct; simpl; intuition congruence.
+ - (* Bload *)
+ intros; repeat autodestruct; simpl; intuition congruence.
+ - (* Bstore *)
+ intros; repeat autodestruct; simpl; intuition congruence.
+ - (* Bseq *)
+ intros.
+ eapply IHib1; eauto.
+ autodestruct; simpl in *; destruct o; simpl in *; intuition eauto.
+ + destruct _fin; intuition eauto.
+ + destruct _fin; intuition congruence || eauto.
+ - (* Bcond *)
+ intros; repeat autodestruct; simpl; intuition congruence || eauto.
+Qed.
+
+Lemma normRTL_preserves_iblock_istep_run_None tge sp ib:
+ forall rs m, iblock_istep_run tge sp ib rs m = None
+ -> iblock_istep_run tge sp (normRTL ib) rs m = None.
+Proof.
+ intros; eapply normRTLrec_iblock_istep_run_None; eauto.
+ rewrite H; simpl; auto.
+Qed.
+
+Lemma normRTL_preserves_iblock_istep_run tge sp ib:
+ forall rs m, iblock_istep_run tge sp ib rs m =
+ iblock_istep_run tge sp (normRTL ib) rs m.
+Proof.
+ intros.
+ destruct (iblock_istep_run tge sp ib rs m) eqn:ISTEP.
+ - destruct o. symmetry.
+ rewrite <- iblock_istep_run_equiv in *.
+ apply normRTL_iblock_istep_correct; auto.
+ - symmetry.
+ apply normRTL_preserves_iblock_istep_run_None; auto.
+Qed.
+
+Local Hint Constructors match_iblock: core.
+Lemma normRTLrec_matchiblock_correct dupmap cfg ib pc isfst:
+ forall opc1
+ (MIB: match_iblock dupmap cfg isfst pc ib opc1) k opc2
+ (CONT: match opc1 with
+ | Some pc' =>
+ match_iblock dupmap cfg false pc' k opc2
+ | None => opc2=opc1
+ end),
+ match_iblock dupmap cfg isfst pc (normRTLrec ib k) opc2.
+Proof.
+ induction 1; simpl; intros; subst; eauto.
+ (* Bcond *)
+ intros. inv H0;
+ econstructor; eauto; try econstructor.
+ destruct opc0; econstructor.
+Qed.
+
+Lemma normRTL_matchiblock_correct dupmap cfg ib pc isfst opc:
+ match_iblock dupmap cfg isfst pc ib opc ->
+ match_iblock dupmap cfg isfst pc (normRTL ib) opc.
+Proof.
+ intros.
+ eapply normRTLrec_matchiblock_correct; eauto.
+ destruct opc; simpl; auto.
+Qed.
+
+Lemma is_normRTL_correct dupmap cfg ib pc
+ (MI : match_iblock dupmap cfg true pc ib None):
+ is_normRTL (normRTL ib).
+Proof.
+ exploit normRTL_matchiblock_correct; eauto.
+ intros MI2.
+ eapply is_wnormRTL_normRTL; eauto.
+ apply normRTL_wcorrect; try congruence.
+ inv MI2; discriminate.
+Qed.
+
+(** * Matching relation on functions *)
+
+(* we simply switch [f] and [tf] in the usual way *)
+Record match_function dupmap (f:RTL.function) (tf: BTL.function): Prop := {
+ matchRTL:> BTLmatchRTL.match_function dupmap tf f;
+ liveness_ok: liveness_ok_function tf;
+}.
+
+Local Hint Resolve matchRTL: core.
+
+Inductive match_fundef: RTL.fundef -> BTL.fundef -> Prop :=
+ | match_Internal dupmap f f': match_function dupmap f f' -> match_fundef (Internal f) (Internal f')
+ | match_External ef: match_fundef (External ef) (External ef).
+
+Inductive match_stackframes: RTL.stackframe -> BTL.stackframe -> Prop :=
+ | match_stackframe_intro
+ dupmap res f sp pc rs f' pc'
+ (TRANSF: match_function dupmap f f')
+ (DUPLIC: dupmap!pc' = Some pc)
+ : match_stackframes (RTL.Stackframe res f sp pc rs) (BTL.Stackframe res f' sp pc' rs).
+
+Lemma transf_function_correct f f':
+ transf_function f = OK f' -> exists dupmap, match_function dupmap f f'.
+Proof.
+ unfold transf_function; unfold bind. repeat autodestruct.
+ intros LIVE VER _ _ X. inv X. eexists; econstructor.
+ - eapply verify_function_correct; simpl; eauto.
+ - unfold liveness_ok_function; destruct u0; auto.
+Qed.
+
+Lemma transf_fundef_correct f f':
+ transf_fundef f = OK f' -> match_fundef f f'.
+Proof.
+ intros TRANSF; destruct f; simpl; monadInv TRANSF.
+ + exploit transf_function_correct; eauto.
+ intros (dupmap & MATCH_F).
+ eapply match_Internal; eauto.
+ + eapply match_External.
+Qed.
+
+Definition match_prog (p: RTL.program) (tp: program) :=
+ match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.
+
+Lemma transf_program_match:
+ forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
+Proof.
+ intros. eapply match_transform_partial_program_contextual; eauto.
+Qed.
+
+Section BTL_SIMULATES_RTL.
+
+Variable prog: RTL.program.
+Variable tprog: program.
+
+Hypothesis TRANSL: match_prog prog tprog.
+
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+Local Open Scope nat_scope.
+
+(** * Match relation from a RTL state to a BTL state
+
+The "option iblock" parameter represents the current BTL execution state.
+Thus, each RTL single step is symbolized by a new BTL "option iblock"
+starting at the equivalent PC.
+
+The simulation diagram for match_states_intro is as follows:
+
+<<
+
+ RTL state match_states_intro BTL state
+ [pcR0,rs0,m0] --------------------------- [pcB0,rs0,m0]
+ | |
+ | |
+ RTL_RUN | *E0 | BTL_RUN
+ | |
+ | MIB |
+ [pcR1,rs1,m1] ------------------------------- [ib]
+
+>>
+*)
+
+Inductive match_strong_state dupmap st st' f f' sp rs1 m1 rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst: Prop :=
+ | match_strong_state_intro
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_function dupmap f f')
+ (ATpc0: (fn_code f')!pcB0 = Some ib0)
+ (DUPLIC: dupmap!pcB0 = Some pcR0)
+ (MIB: match_iblock dupmap (RTL.fn_code f) isfst pcR1 ib None)
+ (IS_EXPD: is_normRTL ib)
+ (RTL_RUN: star RTL.step ge (RTL.State st f sp pcR0 rs0 m0) E0 (RTL.State st f sp pcR1 rs1 m1))
+ (BTL_RUN: iblock_istep_run tge sp ib0.(entry) rs0 m0 = iblock_istep_run tge sp ib rs1 m1)
+ : match_strong_state dupmap st st' f f' sp rs1 m1 rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst
+ .
+
+Inductive match_states: (option iblock) -> RTL.state -> state -> Prop :=
+ | match_states_intro
+ dupmap st st' f f' sp rs1 m1 rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst
+ (MSTRONG: match_strong_state dupmap st st' f f' sp rs1 m1 rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst)
+ (NGOTO: is_goto ib = false)
+ : match_states (Some ib) (RTL.State st f sp pcR1 rs1 m1) (State st' f' sp pcB0 rs0 m0)
+ | match_states_call
+ st st' f f' args m
+ (STACKS: list_forall2 match_stackframes st st')
+ (TRANSF: match_fundef f f')
+ : match_states None (RTL.Callstate st f args m) (Callstate st' f' args m)
+ | match_states_return
+ st st' v m
+ (STACKS: list_forall2 match_stackframes st st')
+ : match_states None (RTL.Returnstate st v m) (Returnstate st' v m)
+ .
+
+Lemma symbols_preserved s: Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof.
+ rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
+Qed.
+
+Lemma senv_preserved: Senv.equiv ge tge.
+Proof.
+ eapply (Genv.senv_match TRANSL).
+Qed.
+
+Lemma functions_translated (v: val) (f: RTL.fundef):
+ Genv.find_funct ge v = Some f ->
+ exists tf cunit, transf_fundef f = OK tf /\ Genv.find_funct tge v = Some tf /\ linkorder cunit prog.
+Proof.
+ intros. exploit (Genv.find_funct_match TRANSL); eauto.
+ intros (cu & tf & A & B & C).
+ repeat eexists; intuition eauto.
+ + unfold incl; auto.
+ + eapply linkorder_refl.
+Qed.
+
+Lemma function_ptr_translated v f:
+ Genv.find_funct_ptr ge v = Some f ->
+ exists tf,
+ Genv.find_funct_ptr tge v = Some tf /\ transf_fundef f = OK tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
+Qed.
+
+Lemma function_sig_translated f tf: transf_fundef f = OK tf -> funsig tf = RTL.funsig f.
+Proof.
+ intros H; apply transf_fundef_correct in H; destruct H; simpl; eauto.
+ erewrite preserv_fnsig; eauto.
+Qed.
+
+Lemma transf_initial_states s1:
+ RTL.initial_state prog s1 ->
+ exists ib s2, initial_state tprog s2 /\ match_states ib s1 s2.
+Proof.
+ intros. inv H.
+ exploit function_ptr_translated; eauto. intros (tf & FIND & TRANSF).
+ eexists. eexists. split.
+ - econstructor; eauto.
+ + eapply (Genv.init_mem_transf_partial TRANSL); eauto.
+ + replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved. eauto.
+ symmetry. eapply match_program_main. eauto.
+ + erewrite function_sig_translated; eauto.
+ - constructor; eauto.
+ constructor.
+ apply transf_fundef_correct; auto.
+Qed.
+
+Lemma transf_final_states ib s1 s2 r:
+ match_states ib s1 s2 -> RTL.final_state s1 r -> final_state s2 r.
+Proof.
+ intros. inv H0. inv H. inv STACKS. constructor.
+Qed.
+
+Lemma find_function_preserved ri rs0 fd
+ (FIND : RTL.find_function ge ri rs0 = Some fd)
+ : exists fd', find_function tge ri rs0 = Some fd'
+ /\ transf_fundef fd = OK fd'.
+Proof.
+ pose symbols_preserved as SYMPRES.
+ destruct ri.
+ + simpl in FIND; apply functions_translated in FIND.
+ destruct FIND as (tf & cunit & TFUN & GFIND & LO).
+ eexists; split. eauto. assumption.
+ + simpl in FIND. destruct (Genv.find_symbol _ _) eqn:GFS; try discriminate.
+ apply function_ptr_translated in FIND. destruct FIND as (tf & GFF & TF).
+ eexists; split. simpl. rewrite symbols_preserved.
+ rewrite GFS. eassumption. assumption.
+Qed.
+
+(** Representing an intermediate BTL state
+
+We keep a measure of code that remains to be executed with the omeasure
+type defined below. Intuitively, each RTL step corresponds to either
+ - a single BTL step if we are on the last instruction of the block
+ - no BTL step (as we use a "big step" semantics) but a change in
+ the measure which represents the new intermediate state of the BTL code
+ *)
+Fixpoint measure ib: nat :=
+ match ib with
+ | Bseq ib1 ib2
+ | Bcond _ _ ib1 ib2 _ => measure ib1 + measure ib2
+ | ib => 1
+ end.
+
+Definition omeasure (oib: option iblock): nat :=
+ match oib with
+ | None => 0
+ | Some ib => measure ib
+ end.
+
+Remark measure_pos: forall ib,
+ measure ib > 0.
+Proof.
+ induction ib; simpl; auto; lia.
+Qed.
+
+Lemma match_iblock_true_isnt_goto dupmap cfg pc ib opc:
+ match_iblock dupmap cfg true pc ib opc ->
+ is_goto ib = false.
+Proof.
+ intros MIB; inversion MIB as [d1 d2 d3 d4 d5 H H0| | | | | | | |]; subst; simpl; try congruence.
+ inv H0; congruence.
+Qed.
+
+Local Hint Resolve match_iblock_true_isnt_goto normRTL_preserves_iblock_istep_run star_refl star_right: core.
+Local Hint Constructors match_strong_state RTL.step: core.
+
+(** At entry in a block: we init [match_states] on [normRTL] to normalize the block *)
+Lemma match_states_entry dupmap st f sp pc ib rs m st' f' pc'
+ (STACKS : list_forall2 match_stackframes st st')
+ (TRANSF : match_function dupmap f f')
+ (FN : (fn_code f') ! pc' = Some ib)
+ (MI : match_iblock dupmap (RTL.fn_code f) true pc (entry ib) None)
+ (DUP : dupmap ! pc' = Some pc):
+ match_states (Some (normRTL (entry ib))) (RTL.State st f sp pc rs m) (State st' f' sp pc' rs m).
+Proof.
+ exploit is_normRTL_correct; eauto.
+ econstructor; eauto; apply normRTL_matchiblock_correct in MI; eauto.
+Qed.
+Local Hint Resolve match_states_entry: core.
+
+Lemma list_nth_z_rev_dupmap:
+ forall dupmap ln ln' (pc pc': node) val,
+ list_nth_z ln val = Some pc ->
+ list_forall2 (fun n' n => dupmap!n' = Some n) ln' ln ->
+ exists (pc': node),
+ list_nth_z ln' val = Some pc'
+ /\ dupmap!pc' = Some pc.
+Proof.
+ induction ln; intros until val; intros LNZ LFA.
+ - inv LNZ.
+ - inv LNZ. destruct (zeq val 0) eqn:ZEQ.
+ + inv H0. destruct ln'; inv LFA.
+ simpl. exists p. split; auto.
+ + inv LFA. simpl. rewrite ZEQ. exploit IHln. 2: eapply H0. all: eauto.
+ intros (pc'1 & LNZ & REV). exists pc'1. split; auto. congruence.
+Qed.
+
+
+(** * Match strong state property
+
+Used when executing non-atomic instructions such as Bseq/Bcond(ib1,ib2).
+Two possible executions:
+
+<<
+
+ **ib2 is a Bgoto (left side):**
+
+ RTL state MSS1 BTL state
+ [pcR1,rs1,m1] -------------------------- [ib1,pcB0,rs0,m0]
+ | |
+ | |
+ | | BTL_STEP
+ | |
+ | |
+ RTL_STEP | *E0 [ib2,pc=(Bgoto succ),rs2,m2]
+ | / |
+ | MSS2 / |
+ | _________________/ | BTL_GOTO
+ | / |
+ | / GOAL: match_states |
+ [pcR2,rs2,m2] ------------------------ [ib?,pc=succ,rs2,m2]
+
+
+ **ib2 is any other instruction (right side):**
+
+See explanations of opt_simu below.
+
+>>
+*)
+
+Lemma match_strong_state_simu
+ dupmap st st' f f' sp rs2 m2 rs1 m1 rs0 m0 pcB0 pcR0 pcR1 pcR2 isfst ib1 ib2 ib0 n t s1'
+ (EQt: t=E0)
+ (EQs1': s1'=(RTL.State st f sp pcR2 rs2 m2))
+ (STEP : RTL.step ge (RTL.State st f sp pcR1 rs1 m1) t s1')
+ (MSS1 : match_strong_state dupmap st st' f f' sp rs1 m1 rs0 m0 pcB0 pcR0 pcR1 ib1 ib0 isfst)
+ (MSS2 : match_strong_state dupmap st st' f f' sp rs2 m2 rs0 m0 pcB0 pcR0 pcR2 ib2 ib0 false)
+ (MES : measure ib2 < n)
+ : exists (oib' : option iblock),
+ (exists s2', step tid tge (State st' f' sp pcB0 rs0 m0) E0 s2'
+ /\ match_states oib' s1' s2')
+ \/ (omeasure oib' < n /\ t=E0
+ /\ match_states oib' s1' (State st' f' sp pcB0 rs0 m0)).
+Proof.
+ subst.
+ destruct (is_goto ib2) eqn:GT.
+ destruct ib2; try destruct fi; try discriminate.
+ - (* Bgoto *)
+ inv MSS2. inversion MIB; subst; try inv H4.
+ remember H2 as ODUPLIC; clear HeqODUPLIC.
+ exploit dupmap_correct; eauto.
+ intros [ib [FNC MI]].
+ eexists; left; eexists; split; eauto.
+ repeat econstructor; eauto.
+ apply iblock_istep_run_equiv in BTL_RUN; eauto.
+ econstructor.
+ - (* Others *)
+ exists (Some ib2); right; split.
+ simpl; auto.
+ split; auto. econstructor; eauto.
+Qed.
+
+Lemma opt_simu_intro
+ dupmap st st' f f' sp rs m rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst s1' t
+ (STEP : RTL.step ge (RTL.State st f sp pcR1 rs m) t s1')
+ (MSTRONG : match_strong_state dupmap st st' f f' sp rs m rs0 m0 pcB0 pcR0 pcR1 ib ib0 isfst)
+ (NGOTO : is_goto ib = false)
+ : exists (oib' : option iblock),
+ (exists s2', step tid tge (State st' f' sp pcB0 rs0 m0) t s2' /\ match_states oib' s1' s2')
+ \/ (omeasure oib' < omeasure (Some ib) /\ t=E0 /\ match_states oib' s1' (State st' f' sp pcB0 rs0 m0)).
+Proof.
+ inv MSTRONG; subst. inv MIB.
+ - (* mib_BF *)
+ inv H0;
+ inversion STEP; subst; try_simplify_someHyps; intros.
+ + (* Breturn *)
+ eexists; left; eexists; split.
+ * econstructor; eauto. econstructor.
+ eexists; eexists; split.
+ eapply iblock_istep_run_equiv in BTL_RUN.
+ eapply BTL_RUN. econstructor; eauto.
+ erewrite preserv_fnstacksize; eauto.
+ * econstructor; eauto.
+ + (* Bcall *)
+ rename H10 into FIND.
+ eapply find_function_preserved in FIND.
+ destruct FIND as (fd' & FF & TRANSFUN).
+ eexists; left; eexists; split.
+ * econstructor; eauto. econstructor.
+ eexists; eexists; split.
+ eapply iblock_istep_run_equiv in BTL_RUN.
+ eapply BTL_RUN. econstructor; eauto.
+ eapply function_sig_translated; eauto.
+ * repeat (econstructor; eauto).
+ eapply transf_fundef_correct; eauto.
+ + (* Btailcall *)
+ rename H9 into FIND.
+ eapply find_function_preserved in FIND.
+ destruct FIND as (fd' & FF & TRANSFUN).
+ eexists; left; eexists; split.
+ * econstructor; eauto. econstructor.
+ eexists; eexists; split.
+ eapply iblock_istep_run_equiv in BTL_RUN.
+ eapply BTL_RUN. econstructor; eauto.
+ eapply function_sig_translated; eauto.
+ erewrite preserv_fnstacksize; eauto.
+ * repeat (econstructor; eauto).
+ eapply transf_fundef_correct; eauto.
+ + (* Bbuiltin *)
+ exploit dupmap_correct; eauto.
+ intros [ib [FNC MI]].
+ exists (Some (normRTL (entry ib))); left; eexists; split; eauto.
+ econstructor; eauto. econstructor.
+ eexists; eexists; split.
+ eapply iblock_istep_run_equiv in BTL_RUN.
+ eapply BTL_RUN. econstructor; eauto.
+ pose symbols_preserved as SYMPRES.
+ eapply eval_builtin_args_preserved; eauto.
+ eapply external_call_symbols_preserved; eauto. eapply senv_preserved.
+ + (* Bjumptable *)
+ exploit list_nth_z_rev_dupmap; eauto.
+ intros (pc'0 & LNZ & DM).
+ exploit dupmap_correct; eauto.
+ intros [ib [FNC MI]].
+ exists (Some (normRTL (entry ib))); left; eexists; split; eauto.
+ econstructor; eauto. econstructor.
+ eexists; eexists; split.
+ eapply iblock_istep_run_equiv in BTL_RUN.
+ eapply BTL_RUN. econstructor; eauto.
+ - (* mib_exit *)
+ discriminate.
+ - (* mib_seq *)
+ inv IS_EXPD; try discriminate.
+ inv H; simpl in *; try congruence;
+ inv STEP; try_simplify_someHyps;
+ intros; eapply match_strong_state_simu; eauto;
+ econstructor; eauto.
+ { (* Bop *)
+ erewrite eval_operation_preserved in H12.
+ erewrite H12 in BTL_RUN; simpl in BTL_RUN; auto.
+ intros; rewrite <- symbols_preserved; trivial. }
+ all: (* Bload / Bstore *)
+ erewrite eval_addressing_preserved in H12;
+ try erewrite H12 in BTL_RUN; try erewrite H13 in BTL_RUN;
+ simpl in BTL_RUN; try destruct trap; auto;
+ intros; rewrite <- symbols_preserved; trivial.
+ - (* mib_cond *)
+ inv IS_EXPD; try discriminate.
+ inversion STEP; subst; try_simplify_someHyps; intros.
+ destruct (is_join_opt_None opc1 opc2); eauto. subst.
+ eapply match_strong_state_simu with (ib1:=Bcond c lr bso bnot iinfo) (ib2:=(if b then bso else bnot)); eauto.
+ + intros; rewrite H14 in BTL_RUN; destruct b; econstructor; eauto.
+ + assert (measure (if b then bnot else bso) > 0) by apply measure_pos; destruct b; simpl; lia.
+ Unshelve.
+ all: eauto.
+Qed.
+
+(** * Main RTL to BTL simulation theorem
+
+Two possible executions:
+
+<<
+
+ **Last instruction (left side):**
+
+ RTL state match_states BTL state
+ s1 ------------------------------------ s2
+ | |
+ STEP | Classical lockstep simu |
+ | |
+ s1' ----------------------------------- s2'
+
+
+ **Middle instruction (right side):**
+
+ RTL state match_states [oib] BTL state
+ s1 ------------------------------------ s2
+ | _______/
+ STEP | *E0 ___________________/
+ | / match_states [oib']
+ s1' ______/
+ Where omeasure oib' < omeasure oib
+
+>>
+*)
+
+Theorem opt_simu s1 t s1' oib s2:
+ RTL.step ge s1 t s1' ->
+ match_states oib s1 s2 ->
+ exists (oib' : option iblock),
+ (exists s2', step tid tge s2 t s2' /\ match_states oib' s1' s2')
+ \/ (omeasure oib' < omeasure oib /\ t=E0 /\ match_states oib' s1' s2)
+ .
+Proof.
+ inversion 2; subst; clear H0.
+ - (* State *)
+ exploit opt_simu_intro; eauto.
+ - (* Callstate *)
+ inv H.
+ + (* Internal function *)
+ inv TRANSF.
+ rename H0 into TRANSF.
+ exploit dupmap_entrypoint; eauto. intros ENTRY.
+ exploit dupmap_correct; eauto.
+ intros [ib [CENTRY MI]].
+ exists (Some (normRTL (entry ib))); left; eexists; split.
+ * eapply exec_function_internal.
+ erewrite preserv_fnstacksize; eauto.
+ * erewrite preserv_fnparams; eauto.
+ + (* External function *)
+ inv TRANSF.
+ eexists; left; eexists; split.
+ * eapply exec_function_external.
+ eapply external_call_symbols_preserved.
+ eapply senv_preserved. eauto.
+ * econstructor; eauto.
+ - (* Returnstate *)
+ inv H. inv STACKS. inv H1.
+ exploit dupmap_correct; eauto.
+ intros [ib [FNC MI]].
+ eexists; left; eexists; split; eauto.
+ eapply exec_return.
+Qed.
+
+Local Hint Resolve plus_one star_refl: core.
+
+Theorem transf_program_correct_cfg:
+ forward_simulation (RTL.semantics prog) (BTLmatchRTL.cfgsem tprog).
+Proof.
+ eapply (Forward_simulation (L1:=RTL.semantics prog) (L2:=cfgsem tprog) (ltof _ omeasure) match_states).
+ constructor 1; simpl.
+ - apply well_founded_ltof.
+ - eapply transf_initial_states.
+ - eapply transf_final_states.
+ - intros s1 t s1' STEP i s2 MATCH. exploit opt_simu; eauto. clear MATCH STEP.
+ destruct 1 as (oib' & [ (s2' & STEP & MATCH) | (MEASURE & TRACE & MATCH) ]).
+ + repeat eexists; eauto.
+ + subst. repeat eexists; eauto.
+ - eapply senv_preserved.
+Qed.
+
+Theorem all_fundef_liveness_ok b f:
+ Genv.find_funct_ptr tge b = Some f -> liveness_ok_fundef f.
+Proof.
+ unfold match_prog, match_program in TRANSL.
+ unfold Genv.find_funct_ptr, tge; simpl; intro X.
+ destruct (Genv.find_def_match_2 TRANSL b) as [|f0 y H]; try congruence.
+ destruct y as [tf0|]; try congruence.
+ inversion X as [H1]. subst. clear X.
+ remember (@Gfun fundef unit f) as f2.
+ destruct H as [ctx' f1 f2 H0|]; try congruence.
+ inversion Heqf2 as [H2]. subst; clear Heqf2.
+ exploit transf_fundef_correct; eauto.
+ destruct f; econstructor.
+ inv H1; eapply liveness_ok; eauto.
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (RTL.semantics prog) (BTL.fsem tprog).
+Proof.
+ eapply compose_forward_simulations.
+ - eapply transf_program_correct_cfg.
+ - eapply cfgsem2fsem. apply all_fundef_liveness_ok.
+Qed.
+
+End BTL_SIMULATES_RTL.
diff --git a/scheduling/abstractbb/AbstractBasicBlocksDef.v b/scheduling/abstractbb/AbstractBasicBlocksDef.v
new file mode 100644
index 00000000..fec716c4
--- /dev/null
+++ b/scheduling/abstractbb/AbstractBasicBlocksDef.v
@@ -0,0 +1,471 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Syntax and Sequential Semantics of Abstract Basic Blocks.
+*)
+Require Import Setoid.
+Require Import ImpPrelude.
+
+Module Type PseudoRegisters.
+
+Parameter t: Type.
+
+Parameter eq_dec: forall (x y: t), { x = y } + { x<>y }.
+
+End PseudoRegisters.
+
+
+(** * Parameters of the language of Basic Blocks *)
+Module Type LangParam.
+
+Declare Module R: PseudoRegisters.
+
+Parameter value: Type.
+
+(** Declare the type of operations *)
+
+Parameter op: Type. (* type of operations *)
+
+Parameter genv: Type. (* environment to be used for evaluating an op *)
+
+Parameter op_eval: genv -> op -> list value -> option value.
+
+End LangParam.
+
+
+
+(** * Syntax and (sequential) semantics of "abstract basic blocks" *)
+Module MkSeqLanguage(P: LangParam).
+
+Export P.
+
+Local Open Scope list.
+
+Section SEQLANG.
+
+Variable ge: genv.
+
+Definition mem := R.t -> value.
+
+Definition assign (m: mem) (x:R.t) (v: value): mem
+ := fun y => if R.eq_dec x y then v else m y.
+
+
+(** Expressions *)
+
+Inductive exp :=
+ | PReg (x:R.t) (**r pseudo-register *)
+ | Op (o:op) (le: list_exp) (**r operation *)
+ | Old (e: exp) (**r evaluation of [e] in the initial state of the instruction (see [inst] below) *)
+with list_exp :=
+ | Enil
+ | Econs (e:exp) (le:list_exp)
+ | LOld (le: list_exp)
+.
+
+Fixpoint exp_eval (e: exp) (m old: mem): option value :=
+ match e with
+ | PReg x => Some (m x)
+ | Op o le =>
+ match list_exp_eval le m old with
+ | Some lv => op_eval ge o lv
+ | _ => None
+ end
+ | Old e => exp_eval e old old
+ end
+with list_exp_eval (le: list_exp) (m old: mem): option (list value) :=
+ match le with
+ | Enil => Some nil
+ | Econs e le' =>
+ match exp_eval e m old, list_exp_eval le' m old with
+ | Some v, Some lv => Some (v::lv)
+ | _, _ => None
+ end
+ | LOld le => list_exp_eval le old old
+ end.
+
+(** An instruction represents a sequence of assignments where [Old] refers to the initial state of the sequence. *)
+Definition inst := list (R.t * exp).
+
+Fixpoint inst_run (i: inst) (m old: mem): option mem :=
+ match i with
+ | nil => Some m
+ | (x, e)::i' =>
+ match exp_eval e m old with
+ | Some v' => inst_run i' (assign m x v') old
+ | None => None
+ end
+ end.
+
+(** A basic block is a sequence of instructions. *)
+Definition bblock := list inst.
+
+Fixpoint run (p: bblock) (m: mem): option mem :=
+ match p with
+ | nil => Some m
+ | i::p' =>
+ match inst_run i m m with
+ | Some m' => run p' m'
+ | None => None
+ end
+ end.
+
+(* A few useful lemma *)
+Lemma assign_eq m x v:
+ (assign m x v) x = v.
+Proof.
+ unfold assign. destruct (R.eq_dec x x); try congruence.
+Qed.
+
+Lemma assign_diff m x y v:
+ x<>y -> (assign m x v) y = m y.
+Proof.
+ unfold assign. destruct (R.eq_dec x y); try congruence.
+Qed.
+
+Lemma assign_skips m x y:
+ (assign m x (m x)) y = m y.
+Proof.
+ unfold assign. destruct (R.eq_dec x y); try congruence.
+Qed.
+
+Lemma assign_swap m x1 v1 x2 v2 y:
+ x1 <> x2 -> (assign (assign m x1 v1) x2 v2) y = (assign (assign m x2 v2) x1 v1) y.
+Proof.
+ intros; destruct (R.eq_dec x2 y).
+ - subst. rewrite assign_eq, assign_diff; auto. rewrite assign_eq; auto.
+ - rewrite assign_diff; auto.
+ destruct (R.eq_dec x1 y).
+ + subst; rewrite! assign_eq. auto.
+ + rewrite! assign_diff; auto.
+Qed.
+
+
+(** A small theory of bblock simulation *)
+
+(* equalities on bblock outputs *)
+Definition res_eq (om1 om2: option mem): Prop :=
+ match om1 with
+ | Some m1 => exists m2, om2 = Some m2 /\ forall x, m1 x = m2 x
+ | None => om2 = None
+ end.
+
+Scheme exp_mut := Induction for exp Sort Prop
+with list_exp_mut := Induction for list_exp Sort Prop.
+
+Lemma exp_equiv e old1 old2:
+ (forall x, old1 x = old2 x) ->
+ forall m1 m2, (forall x, m1 x = m2 x) ->
+ (exp_eval e m1 old1) = (exp_eval e m2 old2).
+Proof.
+ intros H1.
+ induction e using exp_mut with (P0:=fun l => forall m1 m2, (forall x, m1 x = m2 x) -> list_exp_eval l m1 old1 = list_exp_eval l m2 old2); cbn; try congruence; auto.
+ - intros; erewrite IHe; eauto.
+ - intros; erewrite IHe, IHe0; auto.
+Qed.
+
+Definition bblock_simu (p1 p2: bblock): Prop
+ := forall m, (run p1 m) <> None -> res_eq (run p1 m) (run p2 m).
+
+Lemma inst_equiv_refl i old1 old2:
+ (forall x, old1 x = old2 x) ->
+ forall m1 m2, (forall x, m1 x = m2 x) ->
+ res_eq (inst_run i m1 old1) (inst_run i m2 old2).
+Proof.
+ intro H; induction i as [ | [x e]]; cbn; eauto.
+ intros m1 m2 H1. erewrite exp_equiv; eauto.
+ destruct (exp_eval e m2 old2); cbn; auto.
+ apply IHi.
+ unfold assign; intro y. destruct (R.eq_dec x y); auto.
+Qed.
+
+Lemma bblock_equiv_refl p: forall m1 m2, (forall x, m1 x = m2 x) -> res_eq (run p m1) (run p m2).
+Proof.
+ induction p as [ | i p']; cbn; eauto.
+ intros m1 m2 H; lapply (inst_equiv_refl i m1 m2); auto.
+ intros X; lapply (X m1 m2); auto; clear X.
+ destruct (inst_run i m1 m1); cbn.
+ - intros [m3 [H1 H2]]; rewrite H1; cbn; auto.
+ - intros H1; rewrite H1; cbn; auto.
+Qed.
+
+Lemma res_eq_sym om1 om2: res_eq om1 om2 -> res_eq om2 om1.
+Proof.
+ destruct om1; cbn.
+ - intros [m2 [H1 H2]]; subst; cbn. eauto.
+ - intros; subst; cbn; eauto.
+Qed.
+
+Lemma res_eq_trans (om1 om2 om3: option mem):
+ (res_eq om1 om2) -> (res_eq om2 om3) -> (res_eq om1 om3).
+Proof.
+ destruct om1; cbn.
+ - intros [m2 [H1 H2]]; subst; cbn.
+ intros [m3 [H3 H4]]; subst; cbn.
+ eapply ex_intro; intuition eauto. rewrite H2; auto.
+ - intro; subst; cbn; auto.
+Qed.
+
+Lemma bblock_simu_alt p1 p2: bblock_simu p1 p2 <-> (forall m1 m2, (forall x, m1 x = m2 x) -> (run p1 m1)<>None -> res_eq (run p1 m1) (run p2 m2)).
+Proof.
+ unfold bblock_simu; intuition.
+ intros; eapply res_eq_trans. eauto.
+ eapply bblock_equiv_refl; eauto.
+Qed.
+
+
+Lemma run_app p1: forall m1 p2,
+ run (p1++p2) m1 =
+ match run p1 m1 with
+ | Some m2 => run p2 m2
+ | None => None
+ end.
+Proof.
+ induction p1; cbn; try congruence.
+ intros; destruct (inst_run _ _ _); cbn; auto.
+Qed.
+
+Lemma run_app_None p1 m1 p2:
+ run p1 m1 = None ->
+ run (p1++p2) m1 = None.
+Proof.
+ intro H; rewrite run_app. rewrite H; auto.
+Qed.
+
+Lemma run_app_Some p1 m1 m2 p2:
+ run p1 m1 = Some m2 ->
+ run (p1++p2) m1 = run p2 m2.
+Proof.
+ intros H; rewrite run_app. rewrite H; auto.
+Qed.
+
+End SEQLANG.
+
+
+(** * Terms in the symbolic execution *)
+
+(** Such a term represents the successive computations in one given pseudo-register.
+The [hid] has no formal semantics: it is only used by the hash-consing oracle (itself dynamically checked to behave like an identity function).
+
+*)
+
+Module Terms.
+
+Inductive term :=
+ | Input (x:R.t) (hid:hashcode)
+ | App (o: op) (l: list_term) (hid:hashcode)
+with list_term :=
+ | LTnil (hid:hashcode)
+ | LTcons (t:term) (l:list_term) (hid:hashcode)
+ .
+
+Scheme term_mut := Induction for term Sort Prop
+with list_term_mut := Induction for list_term Sort Prop.
+
+Bind Scope pattern_scope with term.
+Delimit Scope term_scope with term.
+Delimit Scope pattern_scope with pattern.
+
+Local Open Scope pattern_scope.
+
+Notation "[ ]" := (LTnil _) (format "[ ]"): pattern_scope.
+Notation "[ x ]" := (LTcons x [ ] _): pattern_scope.
+Notation "[ x ; y ; .. ; z ]" := (LTcons x (LTcons y .. (LTcons z (LTnil _) _) .. _) _): pattern_scope.
+Notation "o @ l" := (App o l _) (at level 50, no associativity): pattern_scope.
+
+Import HConsingDefs.
+
+Notation "[ ]" := (LTnil unknown_hid) (format "[ ]"): term_scope.
+Notation "[ x ]" := (LTcons x []%term unknown_hid): term_scope.
+Notation "[ x ; y ; .. ; z ]" := (LTcons x (LTcons y .. (LTcons z (LTnil unknown_hid) unknown_hid) .. unknown_hid) unknown_hid): term_scope.
+Notation "o @ l" := (App o l unknown_hid) (at level 50, no associativity): term_scope.
+
+
+Fixpoint term_eval (ge: genv) (t: term) (m: mem): option value :=
+ match t with
+ | Input x _ => Some (m x)
+ | o @ l =>
+ match list_term_eval ge l m with
+ | Some v => op_eval ge o v
+ | _ => None
+ end
+ end
+with list_term_eval ge (l: list_term) (m: mem) {struct l}: option (list value) :=
+ match l with
+ | [] => Some nil
+ | LTcons t l' _ =>
+ match term_eval ge t m, list_term_eval ge l' m with
+ | Some v, Some lv => Some (v::lv)
+ | _, _ => None
+ end
+ end.
+
+
+Definition term_get_hid (t: term): hashcode :=
+ match t with
+ | Input _ hid => hid
+ | App _ _ hid => hid
+ end.
+
+Definition list_term_get_hid (l: list_term): hashcode :=
+ match l with
+ | LTnil hid => hid
+ | LTcons _ _ hid => hid
+ end.
+
+
+Fixpoint allvalid ge (l: list term) m : Prop :=
+ match l with
+ | nil => True
+ | t::nil => term_eval ge t m <> None
+ | t::l' => term_eval ge t m <> None /\ allvalid ge l' m
+ end.
+
+Lemma allvalid_extensionality ge (l: list term) m:
+ allvalid ge l m <-> (forall t, List.In t l -> term_eval ge t m <> None).
+Proof.
+ induction l as [|t l]; cbn; try (tauto).
+ destruct l.
+ - intuition (congruence || eauto).
+ - rewrite IHl; clear IHl. intuition (congruence || eauto).
+Qed.
+
+(** * Rewriting rules in the symbolic execution *)
+
+(** The symbolic execution is parametrized by rewriting rules on pseudo-terms. *)
+
+Record pseudo_term: Type := intro_fail {
+ mayfail: list term;
+ effect: term
+}.
+
+(** Simulation relation between a term and a pseudo-term *)
+
+Definition match_pt (t: term) (pt: pseudo_term) :=
+ (forall ge m, term_eval ge t m <> None <-> allvalid ge pt.(mayfail) m)
+ /\ (forall ge m0 m1, term_eval ge t m0 = Some m1 -> term_eval ge pt.(effect) m0 = Some m1).
+
+Lemma inf_option_equivalence (A:Type) (o1 o2: option A):
+ (o1 <> None -> o1 = o2) <-> (forall m1, o1 = Some m1 -> o2 = Some m1).
+Proof.
+ destruct o1; intuition (congruence || eauto).
+ symmetry; eauto.
+Qed.
+
+Lemma intro_fail_correct (l: list term) (t: term) :
+ (forall ge m, term_eval ge t m <> None <-> allvalid ge l m) -> match_pt t (intro_fail l t).
+Proof.
+ unfold match_pt; cbn; intros; intuition congruence.
+Qed.
+Hint Resolve intro_fail_correct: wlp.
+
+(** The default reduction of a term to a pseudo-term *)
+Definition identity_fail (t: term):= intro_fail (t::nil) t.
+
+Lemma identity_fail_correct (t: term): match_pt t (identity_fail t).
+Proof.
+ eapply intro_fail_correct; cbn; tauto.
+Qed.
+Global Opaque identity_fail.
+Hint Resolve identity_fail_correct: wlp.
+
+(** The reduction for constant term *)
+Definition nofail (is_constant: op -> bool) (t: term):=
+ match t with
+ | Input x _ => intro_fail nil t
+ | o @ [] => if is_constant o then (intro_fail nil t) else (identity_fail t)
+ | _ => identity_fail t
+ end.
+
+Lemma nofail_correct (is_constant: op -> bool) t:
+ (forall ge o, is_constant o = true -> op_eval ge o nil <> None) -> match_pt t (nofail is_constant t).
+Proof.
+ destruct t; cbn.
+ + intros; eapply intro_fail_correct; cbn; intuition congruence.
+ + intros; destruct l; cbn; auto with wlp.
+ destruct (is_constant o) eqn:Heqo; cbn; intuition eauto with wlp.
+ eapply intro_fail_correct; cbn; intuition eauto with wlp.
+Qed.
+Global Opaque nofail.
+Hint Resolve nofail_correct: wlp.
+
+(** Term equivalence preserve the simulation by pseudo-terms *)
+Definition term_equiv t1 t2:= forall ge m, term_eval ge t1 m = term_eval ge t2 m.
+
+Global Instance term_equiv_Equivalence : Equivalence term_equiv.
+Proof.
+ split; intro x; unfold term_equiv; intros; eauto.
+ eapply eq_trans; eauto.
+Qed.
+
+Lemma match_pt_term_equiv t1 t2 pt: term_equiv t1 t2 -> match_pt t1 pt -> match_pt t2 pt.
+Proof.
+ unfold match_pt, term_equiv.
+ intros H. intuition; try (erewrite <- H1 in * |- *; congruence).
+ erewrite <- H2; eauto; congruence.
+Qed.
+Hint Resolve match_pt_term_equiv: wlp.
+
+(** Other generic reductions *)
+Definition app_fail (l: list term) (pt: pseudo_term): pseudo_term :=
+ {| mayfail := List.rev_append l pt.(mayfail); effect := pt.(effect) |}.
+
+Lemma app_fail_allvalid_correct l pt t1 t2: forall
+ (V1: forall (ge : genv) (m : mem), term_eval ge t1 m <> None <-> allvalid ge (mayfail pt) m)
+ (V2: forall (ge : genv) (m : mem), term_eval ge t2 m <> None <-> allvalid ge (mayfail {| mayfail := t1 :: l; effect := t1 |}) m)
+ (ge : genv) (m : mem), term_eval ge t2 m <> None <-> allvalid ge (mayfail (app_fail l pt)) m.
+Proof.
+ intros; generalize (V1 ge m) (V2 ge m); rewrite !allvalid_extensionality; cbn. clear V1 V2.
+ intuition subst.
+ + rewrite rev_append_rev, in_app_iff, <- in_rev in H3. destruct H3; eauto.
+ + eapply H3; eauto.
+ intros. intuition subst.
+ * eapply H2; eauto. intros; eapply H0; eauto. rewrite rev_append_rev, in_app_iff; auto.
+ * intros; eapply H0; eauto. rewrite rev_append_rev, in_app_iff, <- in_rev; auto.
+Qed.
+Local Hint Resolve app_fail_allvalid_correct: core.
+
+Lemma app_fail_correct l pt t1 t2:
+ match_pt t1 pt ->
+ match_pt t2 {| mayfail:=t1::l; effect:=t1 |} ->
+ match_pt t2 (app_fail l pt).
+Proof.
+ unfold match_pt in * |- *; intros (V1 & E1) (V2 & E2); split; intros ge m; try (eauto; fail).
+Qed.
+Extraction Inline app_fail.
+
+Import ImpCore.Notations.
+Local Open Scope impure_scope.
+
+(** Specification of rewriting functions in parameter of the symbolic execution: in the impure monad, because the rewriting functions produce hash-consed terms (wrapped in pseudo-terms).
+*)
+Record reduction:= {
+ result:> term -> ?? pseudo_term;
+ result_correct: forall t, WHEN result t ~> pt THEN match_pt t pt;
+}.
+Hint Resolve result_correct: wlp.
+
+End Terms.
+
+End MkSeqLanguage.
+
+
+Module Type SeqLanguage.
+
+Declare Module LP: LangParam.
+
+Include MkSeqLanguage LP.
+
+End SeqLanguage.
+
diff --git a/scheduling/abstractbb/ImpSimuTest.v b/scheduling/abstractbb/ImpSimuTest.v
new file mode 100644
index 00000000..6b64e1d8
--- /dev/null
+++ b/scheduling/abstractbb/ImpSimuTest.v
@@ -0,0 +1,1286 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Implementation of a simulation test (ie a "scheduling verifier") for the sequential semantics of Abstract Basic Blocks.
+
+It is based on a symbolic execution procedure of Abstract Basic Blocks with imperative hash-consing and rewriting.
+
+It also provides debugging information when the test fails.
+
+
+*)
+
+Require Export Impure.ImpHCons. (**r Import the Impure library. See https://github.com/boulme/ImpureDemo *)
+Export Notations.
+Import HConsing.
+
+Require Import Coq.Bool.Bool.
+Require Export SeqSimuTheory.
+Require Import PArith.
+
+
+Local Open Scope impure.
+
+Import ListNotations.
+Local Open Scope list_scope.
+
+
+Definition FULL_DEBUG_DUMP : bool := false. (* print debug traces, even if the verifier succeeds. *)
+
+(** * Interface of (impure) equality tests for operators *)
+Module Type ImpParam.
+
+Include LangParam.
+
+Parameter op_eq: op -> op -> ?? bool.
+
+Parameter op_eq_correct: forall o1 o2,
+ WHEN op_eq o1 o2 ~> b THEN
+ b=true -> o1 = o2.
+
+End ImpParam.
+
+
+Module Type ISeqLanguage.
+
+Declare Module LP: ImpParam.
+
+Include MkSeqLanguage LP.
+
+End ISeqLanguage.
+
+
+(** * A generic dictinary on PseudoRegisters with an impure equality test *)
+
+Module Type ImpDict.
+
+Declare Module R: PseudoRegisters.
+
+Parameter t: Type -> Type.
+
+Parameter get: forall {A}, t A -> R.t -> option A.
+
+Parameter set: forall {A}, t A -> R.t -> A -> t A.
+
+Parameter set_spec_eq: forall A d x (v: A),
+ get (set d x v) x = Some v.
+
+Parameter set_spec_diff: forall A d x y (v: A),
+ x <> y -> get (set d x v) y = get d y.
+
+Parameter rem: forall {A}, t A -> R.t -> t A.
+
+Parameter rem_spec_eq: forall A (d: t A) x,
+ get (rem d x) x = None.
+
+Parameter rem_spec_diff: forall A (d: t A) x y,
+ x <> y -> get (rem d x) y = get d y.
+
+Parameter empty: forall {A}, t A.
+
+Parameter empty_spec: forall A x,
+ get (empty (A:=A)) x = None.
+
+Parameter eq_test: forall {A}, t A -> t A -> ?? bool.
+
+Parameter eq_test_correct: forall A (d1 d2: t A),
+ WHEN eq_test d1 d2 ~> b THEN
+ b=true -> forall x, get d1 x = get d2 x.
+
+(* NB: we could also take an eq_test on R.t (but not really useful with "pure" dictionaries *)
+
+(** only for debugging *)
+Parameter not_eq_witness: forall {A}, t A -> t A -> ?? option R.t.
+
+End ImpDict.
+
+
+(** * Specification of the provided tests *)
+Module Type ImpSimuInterface.
+
+Declare Module CoreL: ISeqLanguage.
+Import CoreL.
+Import Terms.
+
+(** the silent test (without debugging informations) *)
+Parameter bblock_simu_test: reduction -> bblock -> bblock -> ?? bool.
+
+Parameter bblock_simu_test_correct: forall reduce (p1 p2 : bblock),
+ WHEN bblock_simu_test reduce p1 p2 ~> b
+ THEN b = true -> forall ge : genv, bblock_simu ge p1 p2.
+
+(** the verbose test extended with debugging informations *)
+Parameter verb_bblock_simu_test
+ : reduction ->
+ (R.t -> ?? pstring) ->
+ (op -> ?? pstring) -> bblock -> bblock -> ?? bool.
+
+Parameter verb_bblock_simu_test_correct:
+ forall reduce
+ (string_of_name : R.t -> ?? pstring)
+ (string_of_op : op -> ?? pstring)
+ (p1 p2 : bblock),
+ WHEN verb_bblock_simu_test reduce string_of_name string_of_op p1 p2 ~> b
+ THEN b = true -> forall ge : genv, bblock_simu ge p1 p2.
+
+End ImpSimuInterface.
+
+
+(** * Implementation of the provided tests *)
+
+Module ImpSimu (L: ISeqLanguage) (Dict: ImpDict with Module R:=L.LP.R): ImpSimuInterface with Module CoreL := L.
+
+Module CoreL:=L.
+
+Module ST := SimuTheory L.
+
+Import ST.
+Import Terms.
+
+Definition term_set_hid (t: term) (hid: hashcode): term :=
+ match t with
+ | Input x _ => Input x hid
+ | App op l _ => App op l hid
+ end.
+
+Definition list_term_set_hid (l: list_term) (hid: hashcode): list_term :=
+ match l with
+ | LTnil _ => LTnil hid
+ | LTcons t l' _ => LTcons t l' hid
+ end.
+
+Lemma term_eval_set_hid ge t hid m:
+ term_eval ge (term_set_hid t hid) m = term_eval ge t m.
+Proof.
+ destruct t; cbn; auto.
+Qed.
+
+Lemma list_term_eval_set_hid ge l hid m:
+ list_term_eval ge (list_term_set_hid l hid) m = list_term_eval ge l m.
+Proof.
+ destruct l; cbn; auto.
+Qed.
+
+(* Local nickname *)
+Module D:=ImpPrelude.Dict.
+
+Section SimuWithReduce.
+
+Variable reduce: reduction.
+
+Section CanonBuilding. (** Implementation of the symbolic execution (ie a "canonical form" representing the semantics of an abstract basic block) *)
+
+Variable hC_term: hashinfo term -> ?? term.
+Hypothesis hC_term_correct: forall t, WHEN hC_term t ~> t' THEN forall ge m, term_eval ge (hdata t) m = term_eval ge t' m.
+
+Variable hC_list_term: hashinfo list_term -> ?? list_term.
+Hypothesis hC_list_term_correct: forall t, WHEN hC_list_term t ~> t' THEN forall ge m, list_term_eval ge (hdata t) m = list_term_eval ge t' m.
+
+(* First, we wrap constructors for hashed values !*)
+
+Local Open Scope positive.
+Local Open Scope list_scope.
+
+Definition hInput_hcodes (x:R.t) :=
+ DO hc <~ hash 1;;
+ DO hv <~ hash x;;
+ RET [hc;hv].
+Extraction Inline hInput_hcodes.
+
+Definition hInput (x:R.t): ?? term :=
+ DO hv <~ hInput_hcodes x;;
+ hC_term {| hdata:=Input x unknown_hid; hcodes :=hv; |}.
+
+Lemma hInput_correct x:
+ WHEN hInput x ~> t THEN forall ge m, term_eval ge t m = Some (m x).
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hInput.
+Hint Resolve hInput_correct: wlp.
+
+Definition hApp_hcodes (o:op) (l: list_term) :=
+ DO hc <~ hash 2;;
+ DO hv <~ hash o;;
+ RET [hc;hv;list_term_get_hid l].
+Extraction Inline hApp_hcodes.
+
+Definition hApp (o:op) (l: list_term) : ?? term :=
+ DO hv <~ hApp_hcodes o l;;
+ hC_term {| hdata:=App o l unknown_hid; hcodes:=hv |}.
+
+Lemma hApp_correct o l:
+ WHEN hApp o l ~> t THEN forall ge m,
+ term_eval ge t m = match list_term_eval ge l m with
+ | Some v => op_eval ge o v
+ | None => None
+ end.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hApp.
+Hint Resolve hApp_correct: wlp.
+
+Definition hLTnil (_: unit): ?? list_term :=
+ hC_list_term {| hdata:=LTnil unknown_hid; hcodes := nil; |} .
+
+Lemma hLTnil_correct x:
+ WHEN hLTnil x ~> l THEN forall ge m, list_term_eval ge l m = Some nil.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hLTnil.
+Hint Resolve hLTnil_correct: wlp.
+
+
+Definition hLTcons (t: term) (l: list_term): ?? list_term :=
+ hC_list_term {| hdata:=LTcons t l unknown_hid; hcodes := [term_get_hid t; list_term_get_hid l]; |}.
+
+Lemma hLTcons_correct t l:
+ WHEN hLTcons t l ~> l' THEN forall ge m,
+ list_term_eval ge l' m = match term_eval ge t m, list_term_eval ge l m with
+ | Some v, Some lv => Some (v::lv)
+ | _, _ => None
+ end.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque hLTcons.
+Hint Resolve hLTcons_correct: wlp.
+
+(* Second, we use these hashed constructors ! *)
+
+Record hsmem:= {hpre: list term; hpost:> Dict.t term}.
+
+(** evaluation of the post-condition *)
+Definition hsmem_post_eval ge (hd: Dict.t term) x (m:mem) :=
+ match Dict.get hd x with
+ | None => Some (m x)
+ | Some ht => term_eval ge ht m
+ end.
+
+Definition hsmem_get (d:hsmem) x: ?? term :=
+ match Dict.get d x with
+ | None => hInput x
+ | Some t => RET t
+ end.
+
+Lemma hsmem_get_correct (d:hsmem) x:
+ WHEN hsmem_get d x ~> t THEN forall ge m, term_eval ge t m = hsmem_post_eval ge d x m.
+Proof.
+ unfold hsmem_get, hsmem_post_eval; destruct (Dict.get d x); wlp_simplify.
+Qed.
+Global Opaque hsmem_get.
+Hint Resolve hsmem_get_correct: wlp.
+
+Local Opaque allvalid.
+
+Definition smem_model ge (d: smem) (hd:hsmem): Prop :=
+ (forall m, allvalid ge hd.(hpre) m <-> smem_valid ge d m)
+ /\ (forall m x, smem_valid ge d m -> hsmem_post_eval ge hd x m = (ST.term_eval ge (d x) m)).
+
+Lemma smem_model_smem_valid_alt ge d hd: smem_model ge d hd ->
+ forall m x, smem_valid ge d m -> hsmem_post_eval ge hd x m <> None.
+Proof.
+ intros (H1 & H2) m x H. rewrite H2; auto.
+ unfold smem_valid in H. intuition eauto.
+Qed.
+
+Lemma smem_model_allvalid_alt ge d hd: smem_model ge d hd ->
+ forall m x, allvalid ge hd.(hpre) m -> hsmem_post_eval ge hd x m <> None.
+Proof.
+ intros (H1 & H2) m x H. eapply smem_model_smem_valid_alt.
+ - split; eauto.
+ - rewrite <- H1; auto.
+Qed.
+
+Definition naive_set (hd:hsmem) x (t:term) :=
+ {| hpre:= t::hd.(hpre); hpost:=Dict.set hd x t |}.
+
+Lemma naive_set_correct hd x ht ge d t:
+ smem_model ge d hd ->
+ (forall m, smem_valid ge d m -> term_eval ge ht m = ST.term_eval ge t m) ->
+ smem_model ge (smem_set d x t) (naive_set hd x ht).
+Proof.
+ unfold naive_set; intros (DM0 & DM1) EQT; split.
+ - intros m.
+ destruct (DM0 m) as (PRE & VALID0); clear DM0.
+ assert (VALID1: allvalid ge hd.(hpre) m -> pre d ge m). { unfold smem_valid in PRE; tauto. }
+ assert (VALID2: allvalid ge hd.(hpre) m -> forall x : Dict.R.t, ST.term_eval ge (d x) m <> None). { unfold smem_valid in PRE; tauto. }
+ rewrite !allvalid_extensionality in * |- *; cbn.
+ intuition (subst; eauto).
+ + eapply smem_valid_set_proof; eauto.
+ erewrite <- EQT; eauto.
+ + exploit smem_valid_set_decompose_1; eauto.
+ intros X1; exploit smem_valid_set_decompose_2; eauto.
+ rewrite <- EQT; eauto.
+ + exploit smem_valid_set_decompose_1; eauto.
+ - clear DM0. unfold hsmem_post_eval, hsmem_post_eval in * |- *; cbn.
+ Local Hint Resolve smem_valid_set_decompose_1: core.
+ intros; case (R.eq_dec x x0).
+ + intros; subst; rewrite !Dict.set_spec_eq; cbn; eauto.
+ + intros; rewrite !Dict.set_spec_diff; cbn; eauto.
+Qed.
+Local Hint Resolve naive_set_correct: core.
+
+Definition equiv_hsmem ge (hd1 hd2: hsmem) :=
+ (forall m, allvalid ge hd1.(hpre) m <-> allvalid ge hd2.(hpre) m)
+ /\ (forall m x, allvalid ge hd1.(hpre) m -> hsmem_post_eval ge hd1 x m = hsmem_post_eval ge hd2 x m).
+
+Lemma equiv_smem_symmetry ge hd1 hd2:
+ equiv_hsmem ge hd1 hd2 -> equiv_hsmem ge hd2 hd1.
+Proof.
+ intros (V1 & P1); split.
+ - intros; symmetry; auto.
+ - intros; symmetry; eapply P1. rewrite V1; auto.
+Qed.
+
+Lemma equiv_hsmem_models ge hd1 hd2 d:
+ smem_model ge d hd1 -> equiv_hsmem ge hd1 hd2 -> smem_model ge d hd2.
+Proof.
+ intros (VALID & EQUIV) (HEQUIV & PEQUIV); split.
+ - intros m; rewrite <- VALID; auto. symmetry; auto.
+ - intros m x H. rewrite <- EQUIV; auto.
+ rewrite PEQUIV; auto.
+ rewrite VALID; auto.
+Qed.
+
+Variable log_assign: R.t -> term -> ?? unit.
+
+Definition lift {A B} hid (x:A) (k: B -> ?? A) (y:B): ?? A :=
+ DO b <~ phys_eq hid unknown_hid;;
+ if b then k y else RET x.
+
+Fixpoint hterm_lift (t: term): ?? term :=
+ match t with
+ | Input x hid => lift hid t hInput x
+ | App o l hid =>
+ lift hid t
+ (fun l => DO lt <~ hlist_term_lift l;;
+ hApp o lt) l
+ end
+with hlist_term_lift (l: list_term) {struct l}: ?? list_term :=
+ match l with
+ | LTnil hid => lift hid l hLTnil ()
+ | LTcons t l' hid =>
+ lift hid l
+ (fun t => DO t <~ hterm_lift t;;
+ DO lt <~ hlist_term_lift l';;
+ hLTcons t lt) t
+ end.
+
+Lemma hterm_lift_correct t:
+ WHEN hterm_lift t ~> ht THEN forall ge m, term_eval ge ht m = term_eval ge t m.
+Proof.
+ induction t using term_mut with (P0:=fun lt =>
+ WHEN hlist_term_lift lt ~> hlt THEN forall ge m, list_term_eval ge hlt m = list_term_eval ge lt m);
+ wlp_simplify.
+ - rewrite H0, H; auto.
+ - rewrite H1, H0, H; auto.
+Qed.
+Local Hint Resolve hterm_lift_correct: wlp.
+Global Opaque hterm_lift.
+
+Variable log_new_hterm: term -> ?? unit.
+
+Fixpoint hterm_append (l: list term) (lh: list term): ?? list term :=
+ match l with
+ | nil => RET lh
+ | t::l' =>
+ DO ht <~ hterm_lift t;;
+ log_new_hterm ht;;
+ hterm_append l' (ht::lh)
+ end.
+
+Lemma hterm_append_correct l: forall lh,
+ WHEN hterm_append l lh ~> lh' THEN (forall ge m, allvalid ge lh' m <-> (allvalid ge l m /\ allvalid ge lh m)).
+Proof.
+ Local Hint Resolve eq_trans: localhint.
+ induction l as [|t l']; cbn; wlp_xsimplify ltac:(eauto with wlp).
+ - intros; rewrite! allvalid_extensionality; intuition eauto.
+ - intros REC ge m; rewrite REC; clear IHl' REC. rewrite !allvalid_extensionality.
+ cbn; intuition (subst; eauto with wlp localhint).
+Qed.
+(*Local Hint Resolve hterm_append_correct: wlp.*)
+Global Opaque hterm_append.
+
+Definition smart_set (hd:hsmem) x (ht:term) :=
+ match ht with
+ | Input y _ =>
+ if R.eq_dec x y then
+ RET (Dict.rem hd x)
+ else (
+ log_assign x ht;;
+ RET (Dict.set hd x ht)
+ )
+ | _ =>
+ log_assign x ht;;
+ RET (Dict.set hd x ht)
+ end.
+
+Lemma smart_set_correct hd x ht:
+ WHEN smart_set hd x ht ~> d THEN
+ forall ge m y, hsmem_post_eval ge d y m = hsmem_post_eval ge (Dict.set hd x ht) y m.
+Proof.
+ destruct ht; wlp_simplify.
+ unfold hsmem_post_eval; cbn. case (R.eq_dec x0 y).
+ - intros; subst. rewrite Dict.set_spec_eq, Dict.rem_spec_eq. cbn; congruence.
+ - intros; rewrite Dict.set_spec_diff, Dict.rem_spec_diff; auto.
+Qed.
+(*Local Hint Resolve smart_set_correct: wlp.*)
+Global Opaque smart_set.
+
+Definition hsmem_set (hd:hsmem) x (t:term) :=
+ DO pt <~ reduce t;;
+ DO lht <~ hterm_append pt.(mayfail) hd.(hpre);;
+ DO ht <~ hterm_lift pt.(effect);;
+ log_new_hterm ht;;
+ DO nd <~ smart_set hd x ht;;
+ RET {| hpre := lht; hpost := nd |}.
+
+Lemma hsmem_set_correct hd x ht:
+ WHEN hsmem_set hd x ht ~> nhd THEN
+ forall ge d t, smem_model ge d hd ->
+ (forall m, smem_valid ge d m -> term_eval ge ht m = ST.term_eval ge t m) ->
+ smem_model ge (smem_set d x t) nhd.
+Proof.
+ intros; wlp_simplify.
+ generalize (hterm_append_correct _ _ _ Hexta0); intro APPEND.
+ generalize (hterm_lift_correct _ _ Hexta1); intro LIFT.
+ generalize (smart_set_correct _ _ _ _ Hexta3); intro SMART.
+ eapply equiv_hsmem_models; eauto; unfold equiv_hsmem; cbn.
+ destruct H as (VALID & EFFECT); split.
+ - intros; rewrite APPEND, <- VALID.
+ rewrite !allvalid_extensionality in * |- *; cbn; intuition (subst; eauto).
+ - intros m x0 ALLVALID; rewrite SMART.
+ destruct (term_eval ge ht m) eqn: Hht.
+ * case (R.eq_dec x x0).
+ + intros; subst. unfold hsmem_post_eval; cbn. rewrite !Dict.set_spec_eq.
+ erewrite LIFT, EFFECT; eauto.
+ + intros; unfold hsmem_post_eval; cbn. rewrite !Dict.set_spec_diff; auto.
+ * rewrite allvalid_extensionality in ALLVALID; destruct (ALLVALID ht); cbn; auto.
+Qed.
+Local Hint Resolve hsmem_set_correct: wlp.
+Global Opaque hsmem_set.
+
+(* VARIANTE: we do not hash-cons the term from the expression
+Lemma exp_hterm_correct ge e hod od:
+ smem_model ge od hod ->
+ forall hd d,
+ smem_model ge d hd ->
+ forall m, smem_valid ge d m -> smem_valid ge od m -> term_eval ge (exp_term e hd hod) m = term_eval ge (exp_term e d od) m.
+Proof.
+ intro H.
+ induction e using exp_mut with (P0:=fun le => forall d hd,
+ smem_model ge d hd -> forall m, smem_valid ge d m -> smem_valid ge od m -> list_term_eval ge (list_exp_term le hd hod) m = list_term_eval ge (list_exp_term le d od) m);
+ unfold smem_model in * |- * ; cbn; intuition eauto.
+ - erewrite IHe; eauto.
+ - erewrite IHe0, IHe; eauto.
+Qed.
+Local Hint Resolve exp_hterm_correct: wlp.
+*)
+
+Fixpoint exp_hterm (e: exp) (hd hod: hsmem): ?? term :=
+ match e with
+ | PReg x => hsmem_get hd x
+ | Op o le =>
+ DO lt <~ list_exp_hterm le hd hod;;
+ hApp o lt
+ | Old e => exp_hterm e hod hod
+ end
+with list_exp_hterm (le: list_exp) (hd hod: hsmem): ?? list_term :=
+ match le with
+ | Enil => hLTnil tt
+ | Econs e le' =>
+ DO t <~ exp_hterm e hd hod;;
+ DO lt <~ list_exp_hterm le' hd hod;;
+ hLTcons t lt
+ | LOld le => list_exp_hterm le hod hod
+ end.
+
+Lemma exp_hterm_correct_x ge e hod od:
+ smem_model ge od hod ->
+ forall hd d,
+ smem_model ge d hd ->
+ WHEN exp_hterm e hd hod ~> t THEN forall m, smem_valid ge d m -> smem_valid ge od m -> term_eval ge t m = ST.term_eval ge (exp_term e d od) m.
+ Proof.
+ intro H.
+ induction e using exp_mut with (P0:=fun le => forall d hd,
+ smem_model ge d hd ->
+ WHEN list_exp_hterm le hd hod ~> lt THEN forall m, smem_valid ge d m -> smem_valid ge od m -> list_term_eval ge lt m = ST.list_term_eval ge (list_exp_term le d od) m);
+ unfold smem_model, hsmem_post_eval in * |- * ; cbn; wlp_simplify.
+ - rewrite H1, <- H4; auto.
+ - rewrite H4, <- H0; cbn; auto.
+ - rewrite H5, <- H0, <- H4; cbn; auto.
+Qed.
+Global Opaque exp_hterm.
+
+Lemma exp_hterm_correct e hd hod:
+ WHEN exp_hterm e hd hod ~> t THEN forall ge od d m, smem_model ge od hod -> smem_model ge d hd -> smem_valid ge d m -> smem_valid ge od m -> term_eval ge t m = ST.term_eval ge (exp_term e d od) m.
+Proof.
+ unfold wlp; intros; eapply exp_hterm_correct_x; eauto.
+Qed.
+Hint Resolve exp_hterm_correct: wlp.
+
+Fixpoint hinst_smem (i: inst) (hd hod: hsmem): ?? hsmem :=
+ match i with
+ | nil => RET hd
+ | (x, e)::i' =>
+ DO ht <~ exp_hterm e hd hod;;
+ DO nd <~ hsmem_set hd x ht;;
+ hinst_smem i' nd hod
+ end.
+
+Lemma hinst_smem_correct i: forall hd hod,
+ WHEN hinst_smem i hd hod ~> hd' THEN
+ forall ge od d, smem_model ge od hod -> smem_model ge d hd -> (forall m, smem_valid ge d m -> smem_valid ge od m) -> smem_model ge (inst_smem i d od) hd'.
+Proof.
+ Local Hint Resolve smem_valid_set_proof: core.
+ induction i; cbn; wlp_simplify; eauto 15 with wlp.
+Qed.
+Global Opaque hinst_smem.
+Local Hint Resolve hinst_smem_correct: wlp.
+
+(* logging info: we log the number of inst-instructions passed ! *)
+Variable log_new_inst: unit -> ?? unit.
+
+Fixpoint bblock_hsmem_rec (p: bblock) (d: hsmem): ?? hsmem :=
+ match p with
+ | nil => RET d
+ | i::p' =>
+ log_new_inst tt;;
+ DO d' <~ hinst_smem i d d;;
+ bblock_hsmem_rec p' d'
+ end.
+
+Lemma bblock_hsmem_rec_correct p: forall hd,
+ WHEN bblock_hsmem_rec p hd ~> hd' THEN forall ge d, smem_model ge d hd -> smem_model ge (bblock_smem_rec p d) hd'.
+Proof.
+ induction p; cbn; wlp_simplify.
+Qed.
+Global Opaque bblock_hsmem_rec.
+Local Hint Resolve bblock_hsmem_rec_correct: wlp.
+
+Definition hsmem_empty: hsmem := {| hpre:= nil ; hpost := Dict.empty |}.
+
+Lemma hsmem_empty_correct ge: smem_model ge smem_empty hsmem_empty.
+Proof.
+ unfold smem_model, smem_valid, hsmem_post_eval; cbn; intuition try congruence.
+ rewrite !Dict.empty_spec; cbn; auto.
+Qed.
+
+Definition bblock_hsmem: bblock -> ?? hsmem
+ := fun p => bblock_hsmem_rec p hsmem_empty.
+
+Lemma bblock_hsmem_correct p:
+ WHEN bblock_hsmem p ~> hd THEN forall ge, smem_model ge (bblock_smem p) hd.
+Proof.
+ Local Hint Resolve hsmem_empty_correct: core.
+ wlp_simplify.
+Qed.
+Global Opaque bblock_hsmem.
+
+End CanonBuilding.
+
+(* Now, we build the hash-Cons value from a "hash_eq".
+
+Informal specification:
+ [hash_eq] must be consistent with the "hashed" constructors defined above.
+
+We expect that hashinfo values in the code of these "hashed" constructors verify:
+
+ (hash_eq (hdata x) (hdata y) ~> true) <-> (hcodes x)=(hcodes y)
+
+*)
+
+Definition term_hash_eq (ta tb: term): ?? bool :=
+ match ta, tb with
+ | Input xa _, Input xb _ =>
+ if R.eq_dec xa xb (* Inefficient in some cases ? *)
+ then RET true
+ else RET false
+ | App oa lta _, App ob ltb _ =>
+ DO b <~ op_eq oa ob ;;
+ if b then phys_eq lta ltb
+ else RET false
+ | _,_ => RET false
+ end.
+
+Lemma term_hash_eq_correct: forall ta tb, WHEN term_hash_eq ta tb ~> b THEN b=true -> term_set_hid ta unknown_hid=term_set_hid tb unknown_hid.
+Proof.
+ Local Hint Resolve op_eq_correct: wlp.
+ destruct ta, tb; wlp_simplify; (discriminate || (subst; auto)).
+Qed.
+Global Opaque term_hash_eq.
+Hint Resolve term_hash_eq_correct: wlp.
+
+Definition list_term_hash_eq (lta ltb: list_term): ?? bool :=
+ match lta, ltb with
+ | LTnil _, LTnil _ => RET true
+ | LTcons ta lta _, LTcons tb ltb _ =>
+ DO b <~ phys_eq ta tb ;;
+ if b then phys_eq lta ltb
+ else RET false
+ | _,_ => RET false
+ end.
+
+Lemma list_term_hash_eq_correct: forall lta ltb, WHEN list_term_hash_eq lta ltb ~> b THEN b=true -> list_term_set_hid lta unknown_hid=list_term_set_hid ltb unknown_hid.
+Proof.
+ destruct lta, ltb; wlp_simplify; (discriminate || (subst; auto)).
+Qed.
+Global Opaque list_term_hash_eq.
+Hint Resolve list_term_hash_eq_correct: wlp.
+
+Lemma hsmem_post_eval_intro (d1 d2: hsmem):
+ (forall x, Dict.get d1 x = Dict.get d2 x) -> (forall ge x m, hsmem_post_eval ge d1 x m = hsmem_post_eval ge d2 x m).
+Proof.
+ unfold hsmem_post_eval; intros H ge x m; rewrite H. destruct (Dict.get d2 x); auto.
+Qed.
+
+Local Hint Resolve bblock_hsmem_correct Dict.eq_test_correct: wlp.
+
+Program Definition mk_hash_params (log: term -> ?? unit): Dict.hash_params term :=
+ {|
+ Dict.test_eq := phys_eq;
+ Dict.hashing := fun (ht: term) => RET (term_get_hid ht);
+ Dict.log := log |}.
+Obligation 1.
+ eauto with wlp.
+Qed.
+
+(*** A GENERIC EQ_TEST: IN ORDER TO SUPPORT SEVERAL DEBUGGING MODE !!! ***)
+Definition no_log_assign (x:R.t) (t:term): ?? unit := RET tt.
+Definition no_log_new_term (t:term): ?? unit := RET tt.
+
+Section Prog_Eq_Gen.
+
+Variable log_assign: R.t -> term -> ?? unit.
+Variable log_new_term: hashConsing term -> hashConsing list_term -> ??(term -> ?? unit).
+Variable log_inst1: unit -> ?? unit. (* log of p1 insts *)
+Variable log_inst2: unit -> ?? unit. (* log of p2 insts *)
+
+Variable hco_term: hashConsing term.
+Hypothesis hco_term_correct: forall t, WHEN hco_term.(hC) t ~> t' THEN forall ge m, term_eval ge (hdata t) m = term_eval ge t' m.
+
+Variable hco_list: hashConsing list_term.
+Hypothesis hco_list_correct: forall t, WHEN hco_list.(hC) t ~> t' THEN forall ge m, list_term_eval ge (hdata t) m = list_term_eval ge t' m.
+
+Variable print_end_error: hsmem -> hsmem -> ?? unit.
+Variable print_dump: (option pstring) -> ?? unit.
+
+Variable check_failpreserv: bool.
+Variable dbg_failpreserv: term -> ?? unit. (* info of additional failure of the output bbloc p2 wrt the input bbloc p1 *)
+
+Program Definition g_bblock_simu_test (p1 p2: bblock): ?? bool :=
+ DO failure_in_failpreserv <~ make_cref false;;
+ DO r <~ (TRY
+ DO d1 <~ bblock_hsmem hco_term.(hC) hco_list.(hC) log_assign no_log_new_term log_inst1 p1;;
+ DO log_new_term <~ log_new_term hco_term hco_list;;
+ DO d2 <~ bblock_hsmem hco_term.(hC) hco_list.(hC) no_log_assign log_new_term log_inst2 p2;;
+ DO b <~ Dict.eq_test d1 d2 ;;
+ if b then (
+ (if check_failpreserv then
+ let hp := mk_hash_params dbg_failpreserv in
+ failure_in_failpreserv.(set)(true);;
+ Sets.assert_list_incl hp d2.(hpre) d1.(hpre)
+ else RET tt);;
+ (if FULL_DEBUG_DUMP then
+ print_dump None (* not an error... *)
+ else RET tt);;
+ RET check_failpreserv
+ ) else (
+ print_end_error d1 d2 ;;
+ RET false
+ )
+ CATCH_FAIL s, _ =>
+ DO b <~ failure_in_failpreserv.(get)();;
+ if b then RET false
+ else print_dump (Some s);; RET false
+ ENSURE (fun b => b=true -> forall ge, bblock_simu ge p1 p2));;
+ RET (`r).
+Obligation 1.
+ constructor 1; wlp_simplify; try congruence.
+ destruct (H ge) as (EQPRE1&EQPOST1); destruct (H0 ge) as (EQPRE2&EQPOST2); clear H H0.
+ apply bblock_smem_simu; auto. split.
+ + intros m; rewrite <- EQPRE1, <- EQPRE2.
+ rewrite ! allvalid_extensionality.
+ unfold incl in * |- *; intuition eauto.
+ + intros m0 x VALID; rewrite <- EQPOST1, <- EQPOST2; auto.
+ erewrite hsmem_post_eval_intro; eauto.
+ erewrite <- EQPRE2; auto.
+ erewrite <- EQPRE1 in VALID.
+ rewrite ! allvalid_extensionality in * |- *.
+ unfold incl in * |- *; intuition eauto.
+Qed.
+
+Theorem g_bblock_simu_test_correct p1 p2:
+ WHEN g_bblock_simu_test p1 p2 ~> b THEN b=true -> forall ge, bblock_simu ge p1 p2.
+Proof.
+ wlp_simplify.
+ destruct exta0; cbn in * |- *; auto.
+Qed.
+Global Opaque g_bblock_simu_test.
+
+End Prog_Eq_Gen.
+
+
+
+Definition hpt: hashP term := {| hash_eq := term_hash_eq; get_hid:=term_get_hid; set_hid:=term_set_hid |}.
+Definition hplt: hashP list_term := {| hash_eq := list_term_hash_eq; get_hid:=list_term_get_hid; set_hid:=list_term_set_hid |}.
+
+Definition recover_hcodes (t:term): ??(hashinfo term) :=
+ match t with
+ | Input x _ =>
+ DO hv <~ hInput_hcodes x ;;
+ RET {| hdata := t; hcodes := hv |}
+ | App o l _ =>
+ DO hv <~ hApp_hcodes o l ;;
+ RET {| hdata := t; hcodes := hv |}
+ end.
+
+
+Definition msg_end_of_bblock: pstring :="--- unknown subterms in the graph".
+
+Definition log_new_term
+ (unknownHash_msg: term -> ?? pstring)
+ (hct:hashConsing term)
+ (hcl:hashConsing list_term)
+ : ?? (term -> ?? unit) :=
+ DO clock <~ hct.(next_hid)();;
+ hct.(next_log) msg_end_of_bblock;;
+ hcl.(next_log) msg_end_of_bblock;;
+ RET (fun t =>
+ DO ok <~ hash_older (term_get_hid t) clock;;
+ if ok
+ then
+ RET tt
+ else
+ DO ht <~ recover_hcodes t;;
+ hct.(remove) ht;;
+ DO msg <~ unknownHash_msg t;;
+ FAILWITH msg).
+
+Definition skip (_:unit): ?? unit := RET tt.
+
+Definition msg_prefix: pstring := "*** ERROR INFO from bblock_simu_test: ".
+Definition msg_error_on_end: pstring := "mismatch in final assignments !".
+Definition msg_unknow_term: pstring := "unknown term".
+Definition msg_number: pstring := "on 2nd bblock -- on inst num ".
+Definition msg_notfailpreserv: pstring := "a possible failure of 2nd bblock is absent in 1st bblock (INTERNAL ERROR: this error is expected to be detected before!!!)".
+
+Definition print_end_error (_ _: hsmem): ?? unit
+ := println (msg_prefix +; msg_error_on_end).
+
+Definition print_error (log: logger unit) (os: option pstring): ?? unit
+ := match os with
+ | Some s =>
+ DO n <~ log_info log ();;
+ println (msg_prefix +; msg_number +; n +; " -- " +; s)
+ | None => RET tt
+ end.
+
+Definition failpreserv_error (_: term): ?? unit
+ := println (msg_prefix +; msg_notfailpreserv).
+
+Lemma term_eval_set_hid_equiv ge t1 t2 hid1 hid2 m:
+ term_set_hid t1 hid1 = term_set_hid t2 hid2 -> term_eval ge t1 m = term_eval ge t2 m.
+Proof.
+ intro H; erewrite <- term_eval_set_hid; rewrite H. apply term_eval_set_hid.
+Qed.
+
+Lemma list_term_eval_set_hid_equiv ge t1 t2 hid1 hid2 m:
+ list_term_set_hid t1 hid1 = list_term_set_hid t2 hid2 -> list_term_eval ge t1 m = list_term_eval ge t2 m.
+Proof.
+ intro H; erewrite <- list_term_eval_set_hid; rewrite H. apply list_term_eval_set_hid.
+Qed.
+
+Local Hint Resolve term_eval_set_hid_equiv list_term_eval_set_hid_equiv: core.
+
+Program Definition bblock_simu_test (p1 p2: bblock): ?? bool :=
+ DO log <~ count_logger ();;
+ DO hco_term <~ mk_annot (hCons hpt);;
+ DO hco_list <~ mk_annot (hCons hplt);;
+ g_bblock_simu_test
+ no_log_assign
+ (log_new_term (fun _ => RET msg_unknow_term))
+ skip
+ (log_insert log)
+ hco_term _
+ hco_list _
+ print_end_error
+ (print_error log)
+ true (* check_failpreserv *)
+ failpreserv_error
+ p1 p2.
+Obligation 1.
+ generalize (hCons_correct _ _ _ H0); clear H0.
+ wlp_simplify.
+Qed.
+Obligation 2.
+ generalize (hCons_correct _ _ _ H); clear H.
+ wlp_simplify.
+Qed.
+
+Local Hint Resolve g_bblock_simu_test_correct: core.
+
+Theorem bblock_simu_test_correct p1 p2:
+ WHEN bblock_simu_test p1 p2 ~> b THEN b=true -> forall ge, bblock_simu ge p1 p2.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque bblock_simu_test.
+
+(** This is only to print info on each bblock_simu_test run **)
+Section Verbose_version.
+
+Variable string_of_name: R.t -> ?? pstring.
+Variable string_of_op: op -> ?? pstring.
+
+
+Local Open Scope string_scope.
+
+Definition string_term_hid (t: term): ?? pstring :=
+ DO id <~ string_of_hashcode (term_get_hid t);;
+ RET ("E" +; (CamlStr id)).
+
+Definition string_list_hid (lt: list_term): ?? pstring :=
+ DO id <~ string_of_hashcode (list_term_get_hid lt);;
+ RET ("L" +; (CamlStr id)).
+
+Definition print_raw_term (t: term): ?? unit :=
+ match t with
+ | Input x _ =>
+ DO s <~ string_of_name x;;
+ println( "init_access " +; s)
+ | App o (LTnil _) _ =>
+ DO so <~ string_of_op o;;
+ println so
+ | App o l _ =>
+ DO so <~ string_of_op o;;
+ DO sl <~ string_list_hid l;;
+ println (so +; " " +; sl)
+ end.
+
+(*
+Definition print_raw_list(lt: list_term): ?? unit :=
+ match lt with
+ | LTnil _=> println ""
+ | LTcons t l _ =>
+ DO st <~ string_term_hid t;;
+ DO sl <~ string_list_hid l;;
+ println(st +; " " +; sl)
+ end.
+*)
+
+Section PrettryPrint.
+
+Variable get_debug_info: term -> ?? option pstring.
+
+Fixpoint string_of_term (t: term): ?? pstring :=
+ match t with
+ | Input x _ => string_of_name x
+ | App o (LTnil _) _ => string_of_op o
+ | App o l _ =>
+ DO so <~ string_of_op o;;
+ DO sl <~ string_of_list_term l;;
+ RET (so +; "[" +; sl +; "]")
+ end
+with string_of_list_term (l: list_term): ?? pstring :=
+ match l with
+ | LTnil _ => RET (Str "")
+ | LTcons t (LTnil _) _ =>
+ DO dbg <~ get_debug_info t;;
+ match dbg with
+ | Some x => RET x
+ | None => string_of_term t
+ end
+ | LTcons t l' _ =>
+ DO st <~ (DO dbg <~ get_debug_info t;;
+ match dbg with
+ | Some x => RET x
+ | None => string_of_term t
+ end);;
+ DO sl <~ string_of_list_term l';;
+ RET (st +; ";" +; sl)
+ end.
+
+End PrettryPrint.
+
+
+Definition pretty_term gdi t :=
+ DO r <~ string_of_term gdi t;;
+ println(r).
+
+Fixpoint print_head (head: list pstring): ?? unit :=
+ match head with
+ | i::head' => println (i);; print_head head'
+ | _ => RET tt
+ end.
+
+Definition print_term gdi (head: list pstring) (t: term): ?? unit :=
+ print_head head;;
+ DO s <~ string_term_hid t;;
+ print (s +; ": ");;
+ print_raw_term t;;
+ DO dbg <~ gdi t;;
+ match dbg with
+ | Some x =>
+ print("// " +; x +; " <- ");;
+ pretty_term gdi t
+ | None => RET tt
+ end.
+
+Definition print_list gdi (head: list pstring) (lt: list_term): ?? unit :=
+ print_head head;;
+ DO s <~ string_list_hid lt ;;
+ print (s +; ": ");;
+ (* print_raw_list lt;; *)
+ DO ps <~ string_of_list_term gdi lt;;
+ println("[" +; ps +; "]").
+
+
+Definition print_tables gdi ext exl: ?? unit :=
+ println "-- term table --" ;;
+ iterall ext (fun head _ pt => print_term gdi head pt.(hdata));;
+ println "-- list table --" ;;
+ iterall exl (fun head _ pl => print_list gdi head pl.(hdata));;
+ println "----------------".
+
+Definition print_final_debug gdi (d1 d2: hsmem): ?? unit
+ := DO b <~ Dict.not_eq_witness d1 d2 ;;
+ match b with
+ | Some x =>
+ DO s <~ string_of_name x;;
+ println("mismatch on: " +; s);;
+ match Dict.get d1 x with
+ | None => println("=> unassigned in 1st bblock")
+ | Some t1 =>
+ print("=> node expected from 1st bblock: ");;
+ pretty_term gdi t1
+ end;;
+ match Dict.get d2 x with
+ | None => println("=> unassigned in 2nd bblock")
+ | Some t2 =>
+ print("=> node found from 2nd bblock: ");;
+ pretty_term gdi t2
+ end
+ | None => FAILWITH "bug in Dict.not_eq_witness ?"
+ end.
+
+Definition witness:= option term.
+
+Definition msg_term (cr: cref witness) t :=
+ set cr (Some t);;
+ RET msg_unknow_term.
+
+Definition print_witness gdi cr (*msg*) :=
+ DO wit <~ get cr ();;
+ match wit with
+ | Some t =>
+ println("=> unknown term node: ");;
+ pretty_term gdi t (*;;
+ println("=> encoded on " +; msg +; " graph as: ");;
+ print_raw_term t *)
+ | None => println "Unexpected failure: no witness info (hint: hash-consing bug ?)"
+ end.
+
+Definition print_end_error1 gdi hct hcl (d1 d2:hsmem): ?? unit
+ := println "- GRAPH of 1st bblock";;
+ DO ext <~ export hct ();;
+ DO exl <~ export hcl ();;
+ print_tables gdi ext exl;;
+ print_end_error d1 d2;;
+ print_final_debug gdi d1 d2.
+
+Definition print_dump1 gdi hct hcl cr log os : ?? unit
+ := println "- GRAPH of 1st bblock";;
+ DO ext <~ export hct ();;
+ DO exl <~ export hcl ();;
+ print_tables gdi ext exl;;
+ print_error log os;;
+ match os with
+ | Some _ => print_witness gdi cr (*"1st"*)
+ | None => RET tt
+ end.
+
+Definition xmsg_number: pstring := "on 1st bblock -- on inst num ".
+
+Definition print_end_error2 gdi hct hcl (d1 d2:hsmem): ?? unit
+ := println (msg_prefix +; msg_error_on_end);;
+ println "- GRAPH of 2nd bblock";;
+ DO ext <~ export hct ();;
+ DO exl <~ export hcl ();;
+ print_tables gdi ext exl.
+
+Definition print_dump2 gdi hct hcl cr (log: logger unit) (os:option pstring): ?? unit
+ := DO n <~ log_info log ();;
+ DO ext <~ export hct ();;
+ DO exl <~ export hcl ();;
+ match os with
+ | Some s =>
+ println (msg_prefix +; xmsg_number +; n +; " -- " +; s);;
+ print_witness gdi cr (*"2nd"*)
+ | None => RET tt
+ end;;
+ println "- GRAPH of 2nd bblock";;
+ print_tables gdi ext exl.
+
+(* USELESS
+Definition simple_log_assign (d: D.t term pstring) (x: R.t) (t: term): ?? unit :=
+ DO s <~ string_of_name x;;
+ d.(D.set) (t,s).
+*)
+
+Definition log_assign (d: D.t term pstring) (log: logger unit) (x: R.t) (t: term): ?? unit :=
+ DO i <~ log_info log ();;
+ DO sx <~ string_of_name x;;
+ d.(D.set) (t,(sx +; "@" +; i)).
+
+Definition msg_new_inst : pstring := "--- inst ".
+
+Definition hlog (log: logger unit) (hct: hashConsing term) (hcl: hashConsing list_term): unit -> ?? unit :=
+ (fun _ =>
+ log_insert log tt ;;
+ DO s <~ log_info log tt;;
+ let s:= msg_new_inst +; s in
+ next_log hct s;;
+ next_log hcl s
+ ).
+
+Program Definition verb_bblock_simu_test (p1 p2: bblock): ?? bool :=
+ DO dict_info <~ make_dict (mk_hash_params (fun _ => RET tt));;
+ DO log1 <~ count_logger ();;
+ DO log2 <~ count_logger ();;
+ DO cr <~ make_cref None;;
+ DO hco_term <~ mk_annot (hCons hpt);;
+ DO hco_list <~ mk_annot (hCons hplt);;
+ (if FULL_DEBUG_DUMP then
+ println("");;
+ println("-- START simu checker --")
+ else RET tt);;
+ DO result1 <~ (g_bblock_simu_test
+ (log_assign dict_info log1)
+ (log_new_term (msg_term cr))
+ (hlog log1 hco_term hco_list)
+ (log_insert log2)
+ hco_term _
+ hco_list _
+ (print_end_error1 dict_info.(D.get) hco_term hco_list)
+ (print_dump1 dict_info.(D.get) hco_term hco_list cr log2)
+ true
+ failpreserv_error
+ p1 p2);;
+ if (if FULL_DEBUG_DUMP then false else result1)
+ then RET true
+ else (
+ DO dict_info <~ make_dict (mk_hash_params (fun _ => RET tt));;
+ DO log1 <~ count_logger ();;
+ DO log2 <~ count_logger ();;
+ DO cr <~ make_cref None;;
+ DO hco_term <~ mk_annot (hCons hpt);;
+ DO hco_list <~ mk_annot (hCons hplt);;
+ DO result2 <~ g_bblock_simu_test
+ (log_assign dict_info log1)
+ (*fun _ _ => RET no_log_new_term*) (* REM: too weak !! *)
+ (log_new_term (msg_term cr)) (* REM: too strong ?? *)
+ (hlog log1 hco_term hco_list)
+ (log_insert log2)
+ hco_term _
+ hco_list _
+ (print_end_error2 dict_info.(D.get) hco_term hco_list)
+ (print_dump2 dict_info.(D.get) hco_term hco_list cr log2)
+ false
+ (fun _ => RET tt)
+ p2 p1;;
+ if FULL_DEBUG_DUMP then
+ println("-- END simu checker --");;
+ println("");;
+ RET result1
+ else if result2
+ then (
+ println (msg_prefix +; " OOops - symmetry violation in bblock_simu_test => this is a bug of bblock_simu_test ??");;
+ RET false
+ ) else RET false
+ ).
+Obligation 1.
+ generalize (hCons_correct _ _ _ H1); clear H1.
+ wlp_simplify.
+Qed.
+Obligation 2.
+ generalize (hCons_correct _ _ _ H0); clear H0.
+ wlp_simplify.
+Qed.
+Obligation 3.
+ generalize (hCons_correct _ _ _ H1); clear H1.
+ wlp_simplify.
+Qed.
+Obligation 4.
+ generalize (hCons_correct _ _ _ H0); clear H0.
+ wlp_simplify.
+Qed.
+
+Theorem verb_bblock_simu_test_correct p1 p2:
+ WHEN verb_bblock_simu_test p1 p2 ~> b THEN b=true -> forall ge, bblock_simu ge p1 p2.
+Proof.
+ wlp_simplify.
+Qed.
+Global Opaque verb_bblock_simu_test.
+
+End Verbose_version.
+
+End SimuWithReduce.
+
+(* TODO: why inlining fails here ? *)
+Transparent hterm_lift.
+Extraction Inline lift.
+
+End ImpSimu.
+
+
+(** * Implementation of the Dictionary (based on PositiveMap) *)
+Require Import FMapPositive.
+Require Import PArith.
+Require Import FMapPositive.
+
+Module ImpPosDict <: ImpDict with Module R:=Pos.
+
+Module R:=Pos.
+
+Definition t:=PositiveMap.t.
+
+Definition get {A} (d:t A) (x:R.t): option A
+ := PositiveMap.find x d.
+
+Definition set {A} (d:t A) (x:R.t) (v:A): t A
+ := PositiveMap.add x v d.
+
+Local Hint Unfold PositiveMap.E.eq: core.
+
+Lemma set_spec_eq A d x (v: A):
+ get (set d x v) x = Some v.
+Proof.
+ unfold get, set; apply PositiveMap.add_1; auto.
+Qed.
+
+Lemma set_spec_diff A d x y (v: A):
+ x <> y -> get (set d x v) y = get d y.
+Proof.
+ unfold get, set; intros; apply PositiveMap.gso; auto.
+Qed.
+
+Definition rem {A} (d:t A) (x:R.t): t A
+ := PositiveMap.remove x d.
+
+Lemma rem_spec_eq A (d: t A) x:
+ get (rem d x) x = None.
+Proof.
+ unfold get, rem; apply PositiveMap.grs; auto.
+Qed.
+
+Lemma rem_spec_diff A (d: t A) x y:
+ x <> y -> get (rem d x) y = get d y.
+Proof.
+ unfold get, rem; intros; apply PositiveMap.gro; auto.
+Qed.
+
+
+Definition empty {A}: t A := PositiveMap.empty A.
+
+Lemma empty_spec A x:
+ get (empty (A:=A)) x = None.
+Proof.
+ unfold get, empty; apply PositiveMap.gempty; auto.
+Qed.
+
+Import PositiveMap.
+
+Fixpoint eq_test {A} (d1 d2: t A): ?? bool :=
+ match d1, d2 with
+ | Leaf _, Leaf _ => RET true
+ | Node l1 (Some x1) r1, Node l2 (Some x2) r2 =>
+ DO b0 <~ phys_eq x1 x2 ;;
+ if b0 then
+ DO b1 <~ eq_test l1 l2 ;;
+ if b1 then
+ eq_test r1 r2
+ else
+ RET false
+ else
+ RET false
+ | Node l1 None r1, Node l2 None r2 =>
+ DO b1 <~ eq_test l1 l2 ;;
+ if b1 then
+ eq_test r1 r2
+ else
+ RET false
+ | _, _ => RET false
+ end.
+
+Lemma eq_test_correct A d1: forall (d2: t A),
+ WHEN eq_test d1 d2 ~> b THEN
+ b=true -> forall x, get d1 x = get d2 x.
+Proof.
+ unfold get; induction d1 as [|l1 Hl1 [x1|] r1 Hr1]; destruct d2 as [|l2 [x2|] r2]; cbn;
+ wlp_simplify; (discriminate || (subst; destruct x; cbn; auto)).
+Qed.
+Global Opaque eq_test.
+
+(** ONLY FOR DEBUGGING INFO: get some key of a non-empty d *)
+Fixpoint pick {A} (d: t A): ?? R.t :=
+ match d with
+ | Leaf _ => FAILWITH "unexpected empty dictionary"
+ | Node _ (Some _) _ => RET xH
+ | Node (Leaf _) None r =>
+ DO p <~ pick r;;
+ RET (xI p)
+ | Node l None _ =>
+ DO p <~ pick l;;
+ RET (xO p)
+ end.
+
+(** ONLY FOR DEBUGGING INFO: find one variable on which d1 and d2 differs *)
+Fixpoint not_eq_witness {A} (d1 d2: t A): ?? option R.t :=
+ match d1, d2 with
+ | Leaf _, Leaf _ => RET None
+ | Node l1 (Some x1) r1, Node l2 (Some x2) r2 =>
+ DO b0 <~ phys_eq x1 x2 ;;
+ if b0 then
+ DO b1 <~ not_eq_witness l1 l2;;
+ match b1 with
+ | None =>
+ DO b2 <~ not_eq_witness r1 r2;;
+ match b2 with
+ | None => RET None
+ | Some p => RET (Some (xI p))
+ end
+ | Some p => RET (Some (xO p))
+ end
+ else
+ RET (Some xH)
+ | Node l1 None r1, Node l2 None r2 =>
+ DO b1 <~ not_eq_witness l1 l2;;
+ match b1 with
+ | None =>
+ DO b2 <~ not_eq_witness r1 r2;;
+ match b2 with
+ | None => RET None
+ | Some p => RET (Some (xI p))
+ end
+ | Some p => RET (Some (xO p))
+ end
+ | l, Leaf _ => DO p <~ pick l;; RET (Some p)
+ | Leaf _, r => DO p <~ pick r;; RET (Some p)
+ | _, _ => RET (Some xH)
+ end.
+
+End ImpPosDict.
+
diff --git a/scheduling/abstractbb/Parallelizability.v b/scheduling/abstractbb/Parallelizability.v
new file mode 100644
index 00000000..afa4b9fd
--- /dev/null
+++ b/scheduling/abstractbb/Parallelizability.v
@@ -0,0 +1,792 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Parallel Semantics of Abstract Basic Blocks and parallelizability test.
+*)
+
+Require Setoid. (* in order to rewrite <-> *)
+Require Export AbstractBasicBlocksDef.
+
+Require Import List.
+Import ListNotations.
+Local Open Scope list_scope.
+
+Require Import Sorting.Permutation.
+Require Import Bool.
+Local Open Scope lazy_bool_scope.
+
+(** * Definition of the parallel semantics *)
+Module ParallelSemantics (L: SeqLanguage).
+
+Export L.
+Local Open Scope list.
+
+Section PARALLEL.
+Variable ge: genv.
+
+(* parallel run of a inst *)
+Fixpoint inst_prun (i: inst) (m tmp old: mem): option mem :=
+ match i with
+ | nil => Some m
+ | (x, e)::i' =>
+ match exp_eval ge e tmp old with
+ | Some v' => inst_prun i' (assign m x v') (assign tmp x v') old
+ | None => None
+ end
+ end.
+
+(* [inst_prun] is generalization of [inst_run] *)
+Lemma inst_run_prun i: forall m old,
+ inst_run ge i m old = inst_prun i m m old.
+Proof.
+ induction i as [|[y e] i']; cbn; auto.
+ intros m old; destruct (exp_eval ge e m old); cbn; auto.
+Qed.
+
+
+(* parallel run of a bblock -- with in-order writes *)
+Fixpoint prun_iw (p: bblock) m old: option mem :=
+ match p with
+ | nil => Some m
+ | i::p' =>
+ match inst_prun i m old old with
+ | Some m1 => prun_iw p' m1 old
+ | None => None
+ end
+ end.
+
+(* non-deterministic parallel run, due to arbitrary writes order *)
+Definition prun (p: bblock) m (om: option mem) := exists p', res_eq om (prun_iw p' m m) /\ Permutation p p'.
+
+
+(* a few lemma on equality *)
+
+Lemma inst_prun_equiv i old: forall m1 m2 tmp,
+ (forall x, m1 x = m2 x) ->
+ res_eq (inst_prun i m1 tmp old) (inst_prun i m2 tmp old).
+Proof.
+ induction i as [|[x e] i']; cbn; eauto.
+ intros m1 m2 tmp H; destruct (exp_eval ge e tmp old); cbn; auto.
+ eapply IHi'; unfold assign. intros; destruct (R.eq_dec x x0); auto.
+Qed.
+
+Lemma prun_iw_equiv p: forall m1 m2 old,
+ (forall x, m1 x = m2 x) ->
+ res_eq (prun_iw p m1 old) (prun_iw p m2 old).
+Proof.
+ induction p as [|i p']; cbn; eauto.
+ - intros m1 m2 old H.
+ generalize (inst_prun_equiv i old m1 m2 old H);
+ destruct (inst_prun i m1 old old); cbn.
+ + intros (m3 & H3 & H4); rewrite H3; cbn; eauto.
+ + intros H1; rewrite H1; cbn; auto.
+Qed.
+
+
+Lemma prun_iw_app p1: forall m1 old p2,
+ prun_iw (p1++p2) m1 old =
+ match prun_iw p1 m1 old with
+ | Some m2 => prun_iw p2 m2 old
+ | None => None
+ end.
+Proof.
+ induction p1; cbn; try congruence.
+ intros; destruct (inst_prun _ _ _); cbn; auto.
+Qed.
+
+Lemma prun_iw_app_None p1: forall m1 old p2,
+ prun_iw p1 m1 old = None ->
+ prun_iw (p1++p2) m1 old = None.
+Proof.
+ intros m1 old p2 H; rewrite prun_iw_app. rewrite H; auto.
+Qed.
+
+Lemma prun_iw_app_Some p1: forall m1 old m2 p2,
+ prun_iw p1 m1 old = Some m2 ->
+ prun_iw (p1++p2) m1 old = prun_iw p2 m2 old.
+Proof.
+ intros m1 old m2 p2 H; rewrite prun_iw_app. rewrite H; auto.
+Qed.
+
+End PARALLEL.
+End ParallelSemantics.
+
+
+
+Fixpoint notIn {A} (x: A) (l:list A): Prop :=
+ match l with
+ | nil => True
+ | a::l' => x <> a /\ notIn x l'
+ end.
+
+Lemma notIn_iff A (x:A) l: (~List.In x l) <-> notIn x l.
+Proof.
+ induction l; cbn; intuition.
+Qed.
+
+Lemma notIn_app A (x:A) l1: forall l2, notIn x (l1++l2) <-> (notIn x l1 /\ notIn x l2).
+Proof.
+ induction l1; cbn.
+ - intuition.
+ - intros; rewrite IHl1. intuition.
+Qed.
+
+
+Lemma In_Permutation A (l1 l2: list A): Permutation l1 l2 -> forall x, In x l1 -> In x l2.
+Proof.
+ induction 1; cbn; intuition.
+Qed.
+
+Lemma Permutation_incl A (l1 l2: list A): Permutation l1 l2 -> incl l1 l2.
+Proof.
+ unfold incl; intros; eapply In_Permutation; eauto.
+Qed.
+
+Lemma notIn_incl A (l1 l2: list A) x: incl l1 l2 -> notIn x l2 -> notIn x l1.
+Proof.
+ unfold incl; rewrite <- ! notIn_iff; intuition.
+Qed.
+
+
+Definition disjoint {A: Type} (l l':list A) : Prop := forall x, In x l -> notIn x l'.
+
+Lemma disjoint_sym_imp A (l1 l2: list A): disjoint l1 l2 -> disjoint l2 l1.
+Proof.
+ unfold disjoint. intros H x H1. generalize (H x). rewrite <- !notIn_iff. intuition.
+Qed.
+
+Lemma disjoint_sym A (l1 l2: list A): disjoint l1 l2 <-> disjoint l2 l1.
+Proof.
+ constructor 1; apply disjoint_sym_imp; auto.
+Qed.
+
+
+Lemma disjoint_cons_l A (x:A) (l1 l2: list A): disjoint (x::l1) l2 <-> (notIn x l2) /\ (disjoint l1 l2).
+Proof.
+ unfold disjoint. cbn; intuition subst; auto.
+Qed.
+
+Lemma disjoint_cons_r A (x:A) (l1 l2: list A): disjoint l1 (x::l2) <-> (notIn x l1) /\ (disjoint l1 l2).
+Proof.
+ rewrite disjoint_sym, disjoint_cons_l, disjoint_sym; intuition.
+Qed.
+
+Lemma disjoint_app_r A (l l1 l2: list A): disjoint l (l1++l2) <-> (disjoint l l1 /\ disjoint l l2).
+Proof.
+ unfold disjoint. intuition.
+ - generalize (H x H0). rewrite notIn_app; intuition.
+ - generalize (H x H0). rewrite notIn_app; intuition.
+ - rewrite notIn_app; intuition.
+Qed.
+
+Lemma disjoint_app_l A (l l1 l2: list A): disjoint (l1++l2) l <-> (disjoint l1 l /\ disjoint l2 l).
+Proof.
+ rewrite disjoint_sym, disjoint_app_r; intuition; rewrite disjoint_sym; auto.
+Qed.
+
+Lemma disjoint_incl_r A (l1 l2: list A): incl l1 l2 -> forall l, disjoint l l2 -> disjoint l l1.
+Proof.
+ unfold disjoint. intros; eapply notIn_incl; eauto.
+Qed.
+
+Lemma disjoint_incl_l A (l1 l2: list A): incl l1 l2 -> forall l, disjoint l2 l -> disjoint l1 l.
+Proof.
+ intros; rewrite disjoint_sym. eapply disjoint_incl_r; eauto. rewrite disjoint_sym; auto.
+Qed.
+
+
+Module ParallelizablityChecking (L: SeqLanguage).
+
+Include ParallelSemantics L.
+
+Section PARALLELI.
+Variable ge: genv.
+
+(** * Preliminary notions on frames *)
+
+Lemma notIn_dec (x: R.t) l : { notIn x l } + { In x l }.
+Proof.
+ destruct (In_dec R.eq_dec x l). constructor 2; auto.
+ constructor 1; rewrite <- notIn_iff. auto.
+Qed.
+
+Fixpoint frame_assign m1 (f: list R.t) m2 :=
+ match f with
+ | nil => m1
+ | x::f' => frame_assign (assign m1 x (m2 x)) f' m2
+ end.
+
+Lemma frame_assign_def f: forall m1 m2 x,
+ frame_assign m1 f m2 x = if notIn_dec x f then m1 x else m2 x.
+Proof.
+ induction f as [|y f] ; cbn; auto.
+ - intros; destruct (notIn_dec x []); cbn in *; tauto.
+ - intros; rewrite IHf; destruct (notIn_dec x (y::f)); cbn in *.
+ + destruct (notIn_dec x f); cbn in *; intuition.
+ rewrite assign_diff; auto.
+ rewrite <- notIn_iff in *; intuition.
+ + destruct (notIn_dec x f); cbn in *; intuition subst.
+ rewrite assign_eq; auto.
+ rewrite <- notIn_iff in *; intuition.
+Qed.
+
+Lemma frame_assign_In m1 f m2 x:
+ In x f -> frame_assign m1 f m2 x = m2 x.
+Proof.
+ intros; rewrite frame_assign_def; destruct (notIn_dec x f); auto.
+ rewrite <- notIn_iff in *; intuition.
+Qed.
+
+Lemma frame_assign_notIn m1 f m2 x:
+ notIn x f -> frame_assign m1 f m2 x = m1 x.
+Proof.
+ intros; rewrite frame_assign_def; destruct (notIn_dec x f); auto.
+ rewrite <- notIn_iff in *; intuition.
+Qed.
+
+Definition frame_eq (frame: R.t -> Prop) (om1 om2: option mem): Prop :=
+ match om1 with
+ | Some m1 => exists m2, om2 = Some m2 /\ forall x, (frame x) -> m1 x = m2 x
+ | None => om2 = None
+ end.
+
+Lemma frame_eq_list_split f1 (f2: R.t -> Prop) om1 om2:
+ frame_eq (fun x => In x f1) om1 om2 ->
+ (forall m1 m2 x, om1 = Some m1 -> om2 = Some m2 -> f2 x -> notIn x f1 -> m1 x = m2 x) ->
+ frame_eq f2 om1 om2.
+Proof.
+ unfold frame_eq; destruct om1 as [ m1 | ]; cbn; auto.
+ intros (m2 & H0 & H1); subst.
+ intros H.
+ eexists; intuition eauto.
+ destruct (notIn_dec x f1); auto.
+Qed.
+
+(*
+Lemma frame_eq_res_eq f om1 om2:
+ frame_eq (fun x => In x f) om1 om2 ->
+ (forall m1 m2 x, om1 = Some m1 -> om2 = Some m2 -> notIn x f -> m1 x = m2 x) ->
+ res_eq om1 om2.
+Proof.
+ intros H H0; lapply (frame_eq_list_split f (fun _ => True) om1 om2 H); eauto.
+ clear H H0; unfold frame_eq, res_eq. destruct om1; cbn; firstorder.
+Qed.
+*)
+
+(** * Writing frames *)
+
+Fixpoint inst_wframe(i:inst): list R.t :=
+ match i with
+ | nil => nil
+ | a::i' => (fst a)::(inst_wframe i')
+ end.
+
+Lemma inst_wframe_correct i m' old: forall m tmp,
+ inst_prun ge i m tmp old = Some m' ->
+ forall x, notIn x (inst_wframe i) -> m' x = m x.
+Proof.
+ induction i as [|[y e] i']; cbn.
+ - intros m tmp H x H0; inversion_clear H; auto.
+ - intros m tmp H x (H1 & H2); destruct (exp_eval ge e tmp old); cbn; try congruence.
+ replace (m x) with (assign m y v x); eauto.
+ rewrite assign_diff; auto.
+Qed.
+
+Lemma inst_prun_fequiv i old: forall m1 m2 tmp,
+ frame_eq (fun x => In x (inst_wframe i)) (inst_prun ge i m1 tmp old) (inst_prun ge i m2 tmp old).
+Proof.
+ induction i as [|[y e] i']; cbn.
+ - intros m1 m2 tmp; eexists; intuition eauto.
+ - intros m1 m2 tmp. destruct (exp_eval ge e tmp old); cbn; auto.
+ eapply frame_eq_list_split; eauto. clear IHi'.
+ intros m1' m2' x H1 H2.
+ lapply (inst_wframe_correct i' m1' old (assign m1 y v) (assign tmp y v)); eauto.
+ lapply (inst_wframe_correct i' m2' old (assign m2 y v) (assign tmp y v)); eauto.
+ intros Xm2 Xm1 H H0. destruct H.
+ + subst. rewrite Xm1, Xm2; auto. rewrite !assign_eq. auto.
+ + rewrite <- notIn_iff in H0; tauto.
+Qed.
+
+Lemma inst_prun_None i m1 m2 tmp old:
+ inst_prun ge i m1 tmp old = None ->
+ inst_prun ge i m2 tmp old = None.
+Proof.
+ intros H; generalize (inst_prun_fequiv i old m1 m2 tmp).
+ rewrite H; cbn; auto.
+Qed.
+
+Lemma inst_prun_Some i m1 m2 tmp old m1':
+ inst_prun ge i m1 tmp old = Some m1' ->
+ res_eq (Some (frame_assign m2 (inst_wframe i) m1')) (inst_prun ge i m2 tmp old).
+Proof.
+ intros H; generalize (inst_prun_fequiv i old m1 m2 tmp).
+ rewrite H; cbn.
+ intros (m2' & H1 & H2).
+ eexists; intuition eauto.
+ rewrite frame_assign_def.
+ lapply (inst_wframe_correct i m2' old m2 tmp); eauto.
+ destruct (notIn_dec x (inst_wframe i)); auto.
+ intros X; rewrite X; auto.
+Qed.
+
+Fixpoint bblock_wframe(p:bblock): list R.t :=
+ match p with
+ | nil => nil
+ | i::p' => (inst_wframe i)++(bblock_wframe p')
+ end.
+
+Local Hint Resolve Permutation_app_head Permutation_app_tail Permutation_app_comm: core.
+
+Lemma bblock_wframe_Permutation p p':
+ Permutation p p' -> Permutation (bblock_wframe p) (bblock_wframe p').
+Proof.
+ induction 1 as [|i p p'|i1 i2 p|p1 p2 p3]; cbn; auto.
+ - rewrite! app_assoc; auto.
+ - eapply Permutation_trans; eauto.
+Qed.
+
+(*
+Lemma bblock_wframe_correct p m' old: forall m,
+ prun_iw p m old = Some m' ->
+ forall x, notIn x (bblock_wframe p) -> m' x = m x.
+Proof.
+ induction p as [|i p']; cbn.
+ - intros m H; inversion_clear H; auto.
+ - intros m H x; rewrite notIn_app; intros (H1 & H2).
+ remember (inst_prun i m old old) as om.
+ destruct om as [m1|]; cbn.
+ + eapply eq_trans.
+ eapply IHp'; eauto.
+ eapply inst_wframe_correct; eauto.
+ + inversion H.
+Qed.
+
+Lemma prun_iw_fequiv p old: forall m1 m2,
+ frame_eq (fun x => In x (bblock_wframe p)) (prun_iw p m1 old) (prun_iw p m2 old).
+Proof.
+ induction p as [|i p']; cbn.
+ - intros m1 m2; eexists; intuition eauto.
+ - intros m1 m2; generalize (inst_prun_fequiv i old m1 m2 old).
+ remember (inst_prun i m1 old old) as om.
+ destruct om as [m1'|]; cbn.
+ + intros (m2' & H1 & H2). rewrite H1; cbn.
+ eapply frame_eq_list_split; eauto. clear IHp'.
+ intros m1'' m2'' x H3 H4. rewrite in_app_iff.
+ intros X X2. assert (X1: In x (inst_wframe i)). { destruct X; auto. rewrite <- notIn_iff in X2; tauto. }
+ clear X.
+ lapply (bblock_wframe_correct p' m1'' old m1'); eauto.
+ lapply (bblock_wframe_correct p' m2'' old m2'); eauto.
+ intros Xm2' Xm1'.
+ rewrite Xm1', Xm2'; auto.
+ + intro H; rewrite H; cbn; auto.
+Qed.
+
+Lemma prun_iw_equiv p m1 m2 old:
+ (forall x, notIn x (bblock_wframe p) -> m1 x = m2 x) ->
+ res_eq (prun_iw p m1 old) (prun_iw p m2 old).
+Proof.
+ intros; eapply frame_eq_res_eq.
+ eapply prun_iw_fequiv.
+ intros m1' m2' x H1 H2 H0.Require
+ lapply (bblock_wframe_correct p m1' old m1); eauto.
+ lapply (bblock_wframe_correct p m2' old m2); eauto.
+ intros X2 X1; rewrite X1, X2; auto.
+Qed.
+*)
+
+(** * Checking that parallel semantics is deterministic *)
+
+Fixpoint is_det (p: bblock): Prop :=
+ match p with
+ | nil => True
+ | i::p' =>
+ disjoint (inst_wframe i) (bblock_wframe p') (* no WRITE-AFTER-WRITE *)
+ /\ is_det p'
+ end.
+
+Lemma is_det_Permutation p p':
+ Permutation p p' -> is_det p -> is_det p'.
+Proof.
+ induction 1; cbn; auto.
+ - intros; intuition. eapply disjoint_incl_r. 2: eauto.
+ eapply Permutation_incl. eapply Permutation_sym.
+ eapply bblock_wframe_Permutation; auto.
+ - rewrite! disjoint_app_r in * |- *. intuition.
+ rewrite disjoint_sym; auto.
+Qed.
+
+Theorem is_det_correct p p':
+ Permutation p p' ->
+ is_det p ->
+ forall m old, res_eq (prun_iw ge p m old) (prun_iw ge p' m old).
+Proof.
+ induction 1 as [ | i p p' | i1 i2 p | p1 p2 p3 ]; cbn; eauto.
+ - intros [H0 H1] m old.
+ remember (inst_prun ge i m old old) as om0.
+ destruct om0 as [ m0 | ]; cbn; auto.
+ - rewrite disjoint_app_r.
+ intros ([Z1 Z2] & Z3 & Z4) m old.
+ remember (inst_prun ge i2 m old old) as om2.
+ destruct om2 as [ m2 | ]; cbn; auto.
+ + remember (inst_prun ge i1 m old old) as om1.
+ destruct om1 as [ m1 | ]; cbn; auto.
+ * lapply (inst_prun_Some i2 m m1 old old m2); cbn; auto.
+ lapply (inst_prun_Some i1 m m2 old old m1); cbn; auto.
+ intros (m1' & Hm1' & Xm1') (m2' & Hm2' & Xm2').
+ rewrite Hm1', Hm2'; cbn.
+ eapply prun_iw_equiv.
+ intros x; rewrite <- Xm1', <- Xm2'. clear Xm2' Xm1' Hm1' Hm2' m1' m2'.
+ rewrite frame_assign_def.
+ rewrite disjoint_sym in Z1; unfold disjoint in Z1.
+ destruct (notIn_dec x (inst_wframe i1)) as [ X1 | X1 ].
+ { rewrite frame_assign_def; destruct (notIn_dec x (inst_wframe i2)) as [ X2 | X2 ]; auto.
+ erewrite (inst_wframe_correct i2 m2 old m old); eauto.
+ erewrite (inst_wframe_correct i1 m1 old m old); eauto.
+ }
+ rewrite frame_assign_notIn; auto.
+ * erewrite inst_prun_None; eauto. cbn; auto.
+ + remember (inst_prun ge i1 m old old) as om1.
+ destruct om1 as [ m1 | ]; cbn; auto.
+ erewrite inst_prun_None; eauto.
+ - intros; eapply res_eq_trans.
+ eapply IHPermutation1; eauto.
+ eapply IHPermutation2; eauto.
+ eapply is_det_Permutation; eauto.
+Qed.
+
+(** * Standard Frames *)
+
+Fixpoint exp_frame (e: exp): list R.t :=
+ match e with
+ | PReg x => x::nil
+ | Op o le => list_exp_frame le
+ | Old e => exp_frame e
+ end
+with list_exp_frame (le: list_exp): list R.t :=
+ match le with
+ | Enil => nil
+ | Econs e le' => exp_frame e ++ list_exp_frame le'
+ | LOld le => list_exp_frame le
+ end.
+
+Lemma exp_frame_correct e old1 old2:
+ (forall x, In x (exp_frame e) -> old1 x = old2 x) ->
+ forall m1 m2, (forall x, In x (exp_frame e) -> m1 x = m2 x) ->
+ (exp_eval ge e m1 old1)=(exp_eval ge e m2 old2).
+Proof.
+ induction e using exp_mut with (P0:=fun l => (forall x, In x (list_exp_frame l) -> old1 x = old2 x) -> forall m1 m2, (forall x, In x (list_exp_frame l) -> m1 x = m2 x) ->
+ (list_exp_eval ge l m1 old1)=(list_exp_eval ge l m2 old2)); cbn; auto.
+ - intros H1 m1 m2 H2; rewrite H2; auto.
+ - intros H1 m1 m2 H2; erewrite IHe; eauto.
+ - intros H1 m1 m2 H2; erewrite IHe, IHe0; eauto;
+ intros; (eapply H1 || eapply H2); rewrite in_app_iff; auto.
+Qed.
+
+Fixpoint inst_frame (i: inst): list R.t :=
+ match i with
+ | nil => nil
+ | a::i' => (fst a)::(exp_frame (snd a) ++ inst_frame i')
+ end.
+
+Lemma inst_wframe_frame i x: In x (inst_wframe i) -> In x (inst_frame i).
+Proof.
+ induction i as [ | [y e] i']; cbn; intuition.
+Qed.
+
+
+Lemma inst_frame_correct i wframe old1 old2: forall m tmp1 tmp2,
+ (disjoint (inst_frame i) wframe) ->
+ (forall x, notIn x wframe -> old1 x = old2 x) ->
+ (forall x, notIn x wframe -> tmp1 x = tmp2 x) ->
+ inst_prun ge i m tmp1 old1 = inst_prun ge i m tmp2 old2.
+Proof.
+ induction i as [|[x e] i']; cbn; auto.
+ intros m tmp1 tmp2; rewrite disjoint_cons_l, disjoint_app_l.
+ intros (H1 & H2 & H3) H6 H7.
+ replace (exp_eval ge e tmp1 old1) with (exp_eval ge e tmp2 old2).
+ - destruct (exp_eval ge e tmp2 old2); auto.
+ eapply IHi'; eauto.
+ cbn; intros x0 H0; unfold assign. destruct (R.eq_dec x x0); cbn; auto.
+ - unfold disjoint in H2; apply exp_frame_correct.
+ intros;rewrite H6; auto.
+ intros;rewrite H7; auto.
+Qed.
+
+(** * Parallelizability *)
+
+Fixpoint pararec (p: bblock) (wframe: list R.t): Prop :=
+ match p with
+ | nil => True
+ | i::p' =>
+ disjoint (inst_frame i) wframe (* no USE-AFTER-WRITE *)
+ /\ pararec p' ((inst_wframe i) ++ wframe)
+ end.
+
+Lemma pararec_disjoint (p: bblock): forall wframe, pararec p wframe -> disjoint (bblock_wframe p) wframe.
+Proof.
+ induction p as [|i p']; cbn.
+ - unfold disjoint; cbn; intuition.
+ - intros wframe [H0 H1]; rewrite disjoint_app_l.
+ generalize (IHp' _ H1).
+ rewrite disjoint_app_r. intuition.
+ eapply disjoint_incl_l. 2: eapply H0.
+ unfold incl. eapply inst_wframe_frame; eauto.
+Qed.
+
+Lemma pararec_det p: forall wframe, pararec p wframe -> is_det p.
+Proof.
+ induction p as [|i p']; cbn; auto.
+ intros wframe [H0 H1]. generalize (pararec_disjoint _ _ H1). rewrite disjoint_app_r.
+ intuition.
+ - apply disjoint_sym; auto.
+ - eapply IHp'. eauto.
+Qed.
+
+Lemma pararec_correct p old: forall wframe m,
+ pararec p wframe ->
+ (forall x, notIn x wframe -> m x = old x) ->
+ run ge p m = prun_iw ge p m old.
+Proof.
+ elim p; clear p; cbn; auto.
+ intros i p' X wframe m [H H0] H1.
+ erewrite inst_run_prun, inst_frame_correct; eauto.
+ remember (inst_prun ge i m old old) as om0.
+ destruct om0 as [m0 | ]; try congruence.
+ eapply X; eauto.
+ intro x; rewrite notIn_app. intros [H3 H4].
+ rewrite <- H1; auto.
+ eapply inst_wframe_correct; eauto.
+Qed.
+
+Definition parallelizable (p: bblock) := pararec p nil.
+
+Theorem parallelizable_correct p m om':
+ parallelizable p -> (prun ge p m om' <-> res_eq om' (run ge p m)).
+Proof.
+ intros H. constructor 1.
+ - intros (p' & H0 & H1). eapply res_eq_trans; eauto.
+ erewrite pararec_correct; eauto.
+ eapply res_eq_sym.
+ eapply is_det_correct; eauto.
+ eapply pararec_det; eauto.
+ - intros; unfold prun.
+ eexists. constructor 1. 2: apply Permutation_refl.
+ erewrite pararec_correct in H0; eauto.
+Qed.
+
+End PARALLELI.
+
+End ParallelizablityChecking.
+
+
+(** * We assume a datatype [PseudoRegSet.t] refining [list R.t] *)
+
+(**
+This data-refinement is given by an abstract "invariant" match_frame below,
+preserved by the following operations.
+*)
+
+Module Type PseudoRegSet.
+
+Declare Module R: PseudoRegisters.
+
+Parameter t: Type.
+Parameter match_frame: t -> (list R.t) -> Prop.
+
+Parameter empty: t.
+Parameter empty_match_frame: match_frame empty nil.
+
+Parameter add: R.t -> t -> t.
+Parameter add_match_frame: forall s x l, match_frame s l -> match_frame (add x s) (x::l).
+
+Parameter union: t -> t -> t.
+Parameter union_match_frame: forall s1 s2 l1 l2, match_frame s1 l1 -> match_frame s2 l2 -> match_frame (union s1 s2) (l1++l2).
+
+Parameter is_disjoint: t -> t -> bool.
+Parameter is_disjoint_match_frame: forall s1 s2 l1 l2, match_frame s1 l1 -> match_frame s2 l2 -> (is_disjoint s1 s2)=true -> disjoint l1 l2.
+
+End PseudoRegSet.
+
+
+Lemma lazy_andb_bool_true (b1 b2: bool): b1 &&& b2 = true <-> b1 = true /\ b2 = true.
+Proof.
+ destruct b1, b2; intuition.
+Qed.
+
+
+
+
+Module ParallelChecks (L: SeqLanguage) (S:PseudoRegSet with Module R:=L.LP.R).
+
+Include ParallelizablityChecking L.
+
+Section PARALLEL2.
+Variable ge: genv.
+
+Local Hint Resolve S.empty_match_frame S.add_match_frame S.union_match_frame S.is_disjoint_match_frame: core.
+
+(** Now, refinement of each operation toward parallelizable *)
+
+Fixpoint inst_wsframe(i:inst): S.t :=
+ match i with
+ | nil => S.empty
+ | a::i' => S.add (fst a) (inst_wsframe i')
+ end.
+
+Lemma inst_wsframe_correct i: S.match_frame (inst_wsframe i) (inst_wframe i).
+Proof.
+ induction i; cbn; auto.
+Qed.
+
+Fixpoint exp_sframe (e: exp): S.t :=
+ match e with
+ | PReg x => S.add x S.empty
+ | Op o le => list_exp_sframe le
+ | Old e => exp_sframe e
+ end
+with list_exp_sframe (le: list_exp): S.t :=
+ match le with
+ | Enil => S.empty
+ | Econs e le' => S.union (exp_sframe e) (list_exp_sframe le')
+ | LOld le => list_exp_sframe le
+ end.
+
+Lemma exp_sframe_correct e: S.match_frame (exp_sframe e) (exp_frame e).
+Proof.
+ induction e using exp_mut with (P0:=fun l => S.match_frame (list_exp_sframe l) (list_exp_frame l)); cbn; auto.
+Qed.
+
+Fixpoint inst_sframe (i: inst): S.t :=
+ match i with
+ | nil => S.empty
+ | a::i' => S.add (fst a) (S.union (exp_sframe (snd a)) (inst_sframe i'))
+ end.
+
+Local Hint Resolve exp_sframe_correct: core.
+
+Lemma inst_sframe_correct i: S.match_frame (inst_sframe i) (inst_frame i).
+Proof.
+ induction i as [|[y e] i']; cbn; auto.
+Qed.
+
+Local Hint Resolve inst_wsframe_correct inst_sframe_correct: core.
+
+Fixpoint is_pararec (p: bblock) (wsframe: S.t): bool :=
+ match p with
+ | nil => true
+ | i::p' =>
+ S.is_disjoint (inst_sframe i) wsframe (* no USE-AFTER-WRITE *)
+ &&& is_pararec p' (S.union (inst_wsframe i) wsframe)
+ end.
+
+Lemma is_pararec_correct (p: bblock): forall s l, S.match_frame s l -> (is_pararec p s)=true -> (pararec p l).
+Proof.
+ induction p; cbn; auto.
+ intros s l H1 H2; rewrite lazy_andb_bool_true in H2. destruct H2 as [H2 H3].
+ constructor 1; eauto.
+Qed.
+
+Definition is_parallelizable (p: bblock) := is_pararec p S.empty.
+
+Lemma is_para_correct_aux p: is_parallelizable p = true -> parallelizable p.
+Proof.
+ unfold is_parallelizable, parallelizable; intros; eapply is_pararec_correct; eauto.
+Qed.
+
+Theorem is_parallelizable_correct p:
+ is_parallelizable p = true -> forall m om', (prun ge p m om' <-> res_eq om' (run ge p m)).
+Proof.
+ intros; apply parallelizable_correct.
+ apply is_para_correct_aux. auto.
+Qed.
+
+End PARALLEL2.
+End ParallelChecks.
+
+
+
+(** * Implementing the datatype [PosPseudoRegSet.t] refining [list R.t] *)
+
+(* This data-refinement is given by an abstract "invariant" match_frame below,
+preserved by the following operations.
+*)
+
+Require Import PArith.
+Require Import MSets.MSetPositive.
+
+Module PosPseudoRegSet <: PseudoRegSet with Module R:=Pos.
+
+Module R:=Pos.
+
+
+Definition t:=PositiveSet.t.
+
+Definition match_frame (s:t) (l:list R.t): Prop
+ := forall x, PositiveSet.In x s <-> In x l.
+
+Definition empty:=PositiveSet.empty.
+
+Lemma empty_match_frame: match_frame empty nil.
+Proof.
+ unfold match_frame, empty, PositiveSet.In; cbn; intuition.
+Qed.
+
+Definition add: R.t -> t -> t := PositiveSet.add.
+
+Lemma add_match_frame: forall s x l, match_frame s l -> match_frame (add x s) (x::l).
+Proof.
+ unfold match_frame, add; cbn.
+ intros s x l H y. rewrite PositiveSet.add_spec, H.
+ intuition.
+Qed.
+
+Definition union: t -> t -> t := PositiveSet.union.
+Lemma union_match_frame: forall s1 s2 l1 l2, match_frame s1 l1 -> match_frame s2 l2 -> match_frame (union s1 s2) (l1++l2).
+Proof.
+ unfold match_frame, union.
+ intros s1 s2 l1 l2 H1 H2 x. rewrite PositiveSet.union_spec, H1, H2.
+ intuition.
+Qed.
+
+Fixpoint is_disjoint (s s': PositiveSet.t) : bool :=
+ match s with
+ | PositiveSet.Leaf => true
+ | PositiveSet.Node l o r =>
+ match s' with
+ | PositiveSet.Leaf => true
+ | PositiveSet.Node l' o' r' =>
+ if (o &&& o') then false else (is_disjoint l l' &&& is_disjoint r r')
+ end
+ end.
+
+Lemma is_disjoint_spec_true s: forall s', is_disjoint s s' = true -> forall x, PositiveSet.In x s -> PositiveSet.In x s' -> False.
+Proof.
+ unfold PositiveSet.In; induction s as [ |l IHl o r IHr]; cbn; try discriminate.
+ destruct s' as [|l' o' r']; cbn; try discriminate.
+ intros X.
+ assert (H: ~(o = true /\ o'=true) /\ is_disjoint l l' = true /\ is_disjoint r r'=true).
+ { destruct o, o', (is_disjoint l l'), (is_disjoint r r'); cbn in X; intuition. }
+ clear X; destruct H as (H & H1 & H2).
+ destruct x as [i|i|]; cbn; eauto.
+Qed.
+
+Lemma is_disjoint_match_frame: forall s1 s2 l1 l2, match_frame s1 l1 -> match_frame s2 l2 -> (is_disjoint s1 s2)=true -> disjoint l1 l2.
+Proof.
+ unfold match_frame, disjoint.
+ intros s1 s2 l1 l2 H1 H2 H3 x.
+ rewrite <- notIn_iff, <- H1, <- H2.
+ intros H4 H5; eapply is_disjoint_spec_true; eauto.
+Qed.
+
+End PosPseudoRegSet.
diff --git a/scheduling/abstractbb/README.md b/scheduling/abstractbb/README.md
new file mode 100644
index 00000000..69e5defc
--- /dev/null
+++ b/scheduling/abstractbb/README.md
@@ -0,0 +1,12 @@
+# Coq sources of AbstractBasicBlocks
+
+- [AbstractBasicBlocksDef](AbstractBasicBlocksDef.v): syntax and sequential semantics of abstract basic blocks (on which we define our analyzes).
+This syntax and semantics is parametrized in order to adapt the language for different concrete basic block languages.
+
+- [Parallelizability](Parallelizability.v): define the parallel semantics and the 'is_parallelizable' function which tests whether the sequential run of a given abstract basic block is the same than a parallel run.
+
+- [DepTreeTheory](DepTreeTheory.v): defines a theory of dependency trees, such that two basic blocks with the same dependency tree have the same sequential semantics. In practice, permuting the instructions inside a basic block while perserving the dependencies of assignments should not change the dependency tree. The idea is to verify list schedulings, following ideas of [Formal verification of translation validators proposed by Tristan and Leroy](https://hal.inria.fr/inria-00289540/).
+
+- [ImpDep](ImpDep.v): adds a hash-consing mechanism to trees of [DepTreeTheory](DepTreeTheory.v), and thus provides an efficient "equality" test (a true answer ensures that the two basic blocks in input have the same sequential semantics) in order to check the correctness of list schedulings.
+
+- [DepExample](DepExample.v) defines a toy language (syntax and semantics); [DepExampleEqTest](DepExampleEqTest.v) defines a compiler of the toy language into abstract basic blocks and derives an equality test for the toy language; [DepExampleParallelTest](DepExampleParallelTest.v) derives a parallelizability test from the previous compiler; [DepExampleDemo](DepExampleDemo.v) is a test-suite for both tetsts.
diff --git a/scheduling/abstractbb/SeqSimuTheory.v b/scheduling/abstractbb/SeqSimuTheory.v
new file mode 100644
index 00000000..df6b9963
--- /dev/null
+++ b/scheduling/abstractbb/SeqSimuTheory.v
@@ -0,0 +1,397 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** A theory for checking/proving simulation by symbolic execution.
+
+*)
+
+
+Require Coq.Logic.FunctionalExtensionality. (* not really necessary -- see lemma at the end *)
+Require Setoid. (* in order to rewrite <-> *)
+Require Export AbstractBasicBlocksDef.
+Require Import List.
+Require Import ImpPrelude.
+Import HConsingDefs.
+
+
+Module SimuTheory (L: SeqLanguage).
+
+Export L.
+Export LP.
+
+Inductive term :=
+ | Input (x:R.t)
+ | App (o: op) (l: list_term)
+with list_term :=
+ | LTnil
+ | LTcons (t:term) (l:list_term)
+ .
+
+Fixpoint term_eval (ge: genv) (t: term) (m: mem): option value :=
+ match t with
+ | Input x => Some (m x)
+ | App o l =>
+ match list_term_eval ge l m with
+ | Some v => op_eval ge o v
+ | _ => None
+ end
+ end
+with list_term_eval ge (l: list_term) (m: mem) {struct l}: option (list value) :=
+ match l with
+ | LTnil => Some nil
+ | LTcons t l' =>
+ match term_eval ge t m, list_term_eval ge l' m with
+ | Some v, Some lv => Some (v::lv)
+ | _, _ => None
+ end
+ end.
+
+(** The (abstract) symbolic memory state *)
+Record smem :=
+{
+ pre: genv -> mem -> Prop; (**r pre-condition expressing that the computation has not yet abort on a None. *)
+ post:> R.t -> term (**r the output term computed on each pseudo-register *)
+}.
+
+(** Initial symbolic memory state *)
+Definition smem_empty := {| pre:=fun _ _ => True; post:=(fun x => Input x) |}.
+
+Fixpoint exp_term (e: exp) (d old: smem) : term :=
+ match e with
+ | PReg x => d x
+ | Op o le => App o (list_exp_term le d old)
+ | Old e => exp_term e old old
+ end
+with list_exp_term (le: list_exp) (d old: smem) : list_term :=
+ match le with
+ | Enil => LTnil
+ | Econs e le' => LTcons (exp_term e d old) (list_exp_term le' d old)
+ | LOld le => list_exp_term le old old
+ end.
+
+
+(** assignment of the symbolic memory state *)
+Definition smem_set (d:smem) x (t:term) :=
+ {| pre:=(fun ge m => (term_eval ge (d x) m) <> None /\ (d.(pre) ge m));
+ post:=fun y => if R.eq_dec x y then t else d y |}.
+
+(** Simulation theory: the theorem [bblock_smem_simu] ensures that the simulation between two abstract basic blocks is implied by the simulation between their symbolic execution. *)
+Section SIMU_THEORY.
+
+Variable ge: genv.
+
+Lemma set_spec_eq d x t m:
+ term_eval ge (smem_set d x t x) m = term_eval ge t m.
+Proof.
+ unfold smem_set; cbn; case (R.eq_dec x x); try congruence.
+Qed.
+
+Lemma set_spec_diff d x y t m:
+ x <> y -> term_eval ge (smem_set d x t y) m = term_eval ge (d y) m.
+Proof.
+ unfold smem_set; cbn; case (R.eq_dec x y); try congruence.
+Qed.
+
+Fixpoint inst_smem (i: inst) (d old: smem): smem :=
+ match i with
+ | nil => d
+ | (x, e)::i' =>
+ let t:=exp_term e d old in
+ inst_smem i' (smem_set d x t) old
+ end.
+
+Fixpoint bblock_smem_rec (p: bblock) (d: smem): smem :=
+ match p with
+ | nil => d
+ | i::p' =>
+ let d':=inst_smem i d d in
+ bblock_smem_rec p' d'
+ end.
+
+Definition bblock_smem: bblock -> smem
+ := fun p => bblock_smem_rec p smem_empty.
+
+Lemma inst_smem_pre_monotonic i old: forall d m,
+ (pre (inst_smem i d old) ge m) -> (pre d ge m).
+Proof.
+ induction i as [|[y e] i IHi]; cbn; auto.
+ intros d a H; generalize (IHi _ _ H); clear H IHi.
+ unfold smem_set; cbn; intuition.
+Qed.
+
+Lemma bblock_smem_pre_monotonic p: forall d m,
+ (pre (bblock_smem_rec p d) ge m) -> (pre d ge m).
+Proof.
+ induction p as [|i p' IHp']; cbn; eauto.
+ intros d a H; eapply inst_smem_pre_monotonic; eauto.
+Qed.
+
+Local Hint Resolve inst_smem_pre_monotonic bblock_smem_pre_monotonic: core.
+
+Lemma term_eval_exp e (od:smem) m0 old:
+ (forall x, term_eval ge (od x) m0 = Some (old x)) ->
+ forall (d:smem) m1,
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ term_eval ge (exp_term e d od) m0 = exp_eval ge e m1 old.
+Proof.
+ intro H.
+ induction e using exp_mut with
+ (P0:=fun l => forall (d:smem) m1, (forall x, term_eval ge (d x) m0 = Some (m1 x)) -> list_term_eval ge (list_exp_term l d od) m0 = list_exp_eval ge l m1 old);
+ cbn; auto.
+ - intros; erewrite IHe; eauto.
+ - intros. erewrite IHe, IHe0; eauto.
+Qed.
+
+Lemma inst_smem_abort i m0 x old: forall (d:smem),
+ pre (inst_smem i d old) ge m0 ->
+ term_eval ge (d x) m0 = None ->
+ term_eval ge (inst_smem i d old x) m0 = None.
+Proof.
+ induction i as [|[y e] i IHi]; cbn; auto.
+ intros d VALID H; erewrite IHi; eauto. clear IHi.
+ unfold smem_set; cbn; destruct (R.eq_dec y x); auto.
+ subst;
+ generalize (inst_smem_pre_monotonic _ _ _ _ VALID); clear VALID.
+ unfold smem_set; cbn. intuition congruence.
+Qed.
+
+Lemma block_smem_rec_abort p m0 x: forall d,
+ pre (bblock_smem_rec p d) ge m0 ->
+ term_eval ge (d x) m0 = None ->
+ term_eval ge (bblock_smem_rec p d x) m0 = None.
+Proof.
+ induction p; cbn; auto.
+ intros d VALID H; erewrite IHp; eauto. clear IHp.
+ eapply inst_smem_abort; eauto.
+Qed.
+
+Lemma inst_smem_Some_correct1 i m0 old (od:smem):
+ (forall x, term_eval ge (od x) m0 = Some (old x)) ->
+ forall (m1 m2: mem) (d: smem),
+ inst_run ge i m1 old = Some m2 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ forall x, term_eval ge (inst_smem i d od x) m0 = Some (m2 x).
+Proof.
+ intro X; induction i as [|[x e] i IHi]; cbn; intros m1 m2 d H.
+ - inversion_clear H; eauto.
+ - intros H0 x0.
+ destruct (exp_eval ge e m1 old) eqn:Heqov; try congruence.
+ refine (IHi _ _ _ _ _ _); eauto.
+ clear x0; intros x0.
+ unfold assign, smem_set; cbn. destruct (R.eq_dec x x0); auto.
+ subst; erewrite term_eval_exp; eauto.
+Qed.
+
+Lemma bblocks_smem_rec_Some_correct1 p m0: forall (m1 m2: mem) (d: smem),
+ run ge p m1 = Some m2 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ forall x, term_eval ge (bblock_smem_rec p d x) m0 = Some (m2 x).
+Proof.
+ Local Hint Resolve inst_smem_Some_correct1: core.
+ induction p as [ | i p]; cbn; intros m1 m2 d H.
+ - inversion_clear H; eauto.
+ - intros H0 x0.
+ destruct (inst_run ge i m1 m1) eqn: Heqov.
+ + refine (IHp _ _ _ _ _ _); eauto.
+ + inversion H.
+Qed.
+
+Lemma bblock_smem_Some_correct1 p m0 m1:
+ run ge p m0 = Some m1
+ -> forall x, term_eval ge (bblock_smem p x) m0 = Some (m1 x).
+Proof.
+ intros; eapply bblocks_smem_rec_Some_correct1; eauto.
+Qed.
+
+Lemma inst_smem_None_correct i m0 old (od: smem):
+ (forall x, term_eval ge (od x) m0 = Some (old x)) ->
+ forall m1 d, pre (inst_smem i d od) ge m0 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ inst_run ge i m1 old = None -> exists x, term_eval ge (inst_smem i d od x) m0 = None.
+Proof.
+ intro X; induction i as [|[x e] i IHi]; cbn; intros m1 d.
+ - discriminate.
+ - intros VALID H0.
+ destruct (exp_eval ge e m1 old) eqn: Heqov.
+ + refine (IHi _ _ _ _); eauto.
+ intros x0; unfold assign, smem_set; cbn. destruct (R.eq_dec x x0); auto.
+ subst; erewrite term_eval_exp; eauto.
+ + intuition.
+ constructor 1 with (x:=x); cbn.
+ apply inst_smem_abort; auto.
+ rewrite set_spec_eq.
+ erewrite term_eval_exp; eauto.
+Qed.
+
+Lemma inst_smem_Some_correct2 i m0 old (od: smem):
+ (forall x, term_eval ge (od x) m0 = Some (old x)) ->
+ forall (m1 m2: mem) d,
+ pre (inst_smem i d od) ge m0 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ (forall x, term_eval ge (inst_smem i d od x) m0 = Some (m2 x)) ->
+ res_eq (Some m2) (inst_run ge i m1 old).
+Proof.
+ intro X.
+ induction i as [|[x e] i IHi]; cbn; intros m1 m2 d VALID H0.
+ - intros H; eapply ex_intro; intuition eauto.
+ generalize (H0 x); rewrite H.
+ congruence.
+ - intros H.
+ destruct (exp_eval ge e m1 old) eqn: Heqov.
+ + refine (IHi _ _ _ _ _ _); eauto.
+ intros x0; unfold assign, smem_set; cbn; destruct (R.eq_dec x x0); auto.
+ subst; erewrite term_eval_exp; eauto.
+ + generalize (H x).
+ rewrite inst_smem_abort; discriminate || auto.
+ rewrite set_spec_eq.
+ erewrite term_eval_exp; eauto.
+Qed.
+
+Lemma bblocks_smem_rec_Some_correct2 p m0: forall (m1 m2: mem) d,
+ pre (bblock_smem_rec p d) ge m0 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ (forall x, term_eval ge (bblock_smem_rec p d x) m0 = Some (m2 x)) ->
+ res_eq (Some m2) (run ge p m1).
+Proof.
+ induction p as [|i p]; cbn; intros m1 m2 d VALID H0.
+ - intros H; eapply ex_intro; intuition eauto.
+ generalize (H0 x); rewrite H.
+ congruence.
+ - intros H.
+ destruct (inst_run ge i m1 m1) eqn: Heqom.
+ + refine (IHp _ _ _ _ _ _); eauto.
+ + assert (X: exists x, term_eval ge (inst_smem i d d x) m0 = None).
+ { eapply inst_smem_None_correct; eauto. }
+ destruct X as [x H1].
+ generalize (H x).
+ erewrite block_smem_rec_abort; eauto.
+ congruence.
+Qed.
+
+Lemma bblock_smem_Some_correct2 p m0 m1:
+ pre (bblock_smem p) ge m0 ->
+ (forall x, term_eval ge (bblock_smem p x) m0 = Some (m1 x))
+ -> res_eq (Some m1) (run ge p m0).
+Proof.
+ intros; eapply bblocks_smem_rec_Some_correct2; eauto.
+Qed.
+
+Lemma inst_valid i m0 old (od:smem):
+ (forall x, term_eval ge (od x) m0 = Some (old x)) ->
+ forall (m1 m2: mem) (d: smem),
+ pre d ge m0 ->
+ inst_run ge i m1 old = Some m2 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ pre (inst_smem i d od) ge m0.
+Proof.
+ induction i as [|[x e] i IHi]; cbn; auto.
+ intros Hold m1 m2 d VALID0 H Hm1.
+ destruct (exp_eval ge e m1 old) eqn: Heq; cbn; try congruence.
+ eapply IHi; eauto.
+ + unfold smem_set in * |- *; cbn.
+ rewrite Hm1; intuition congruence.
+ + intros x0. unfold assign, smem_set; cbn; destruct (R.eq_dec x x0); auto.
+ subst; erewrite term_eval_exp; eauto.
+Qed.
+
+
+Lemma block_smem_rec_valid p m0: forall (m1 m2: mem) (d:smem),
+ pre d ge m0 ->
+ run ge p m1 = Some m2 ->
+ (forall x, term_eval ge (d x) m0 = Some (m1 x)) ->
+ pre (bblock_smem_rec p d) ge m0.
+Proof.
+ Local Hint Resolve inst_valid: core.
+ induction p as [ | i p]; cbn; intros m1 d H; auto.
+ intros H0 H1.
+ destruct (inst_run ge i m1 m1) eqn: Heqov; eauto.
+ congruence.
+Qed.
+
+Lemma bblock_smem_valid p m0 m1:
+ run ge p m0 = Some m1 ->
+ pre (bblock_smem p) ge m0.
+Proof.
+ intros; eapply block_smem_rec_valid; eauto.
+ unfold smem_empty; cbn. auto.
+Qed.
+
+Definition smem_valid ge d m := pre d ge m /\ forall x, term_eval ge (d x) m <> None.
+
+Definition smem_simu (d1 d2: smem): Prop :=
+ (forall m, smem_valid ge d1 m -> smem_valid ge d2 m)
+ /\ (forall m0 x, smem_valid ge d1 m0 ->
+ term_eval ge (d1 x) m0 = term_eval ge (d2 x) m0).
+
+
+Theorem bblock_smem_simu p1 p2:
+ smem_simu (bblock_smem p1) (bblock_smem p2) ->
+ bblock_simu ge p1 p2.
+Proof.
+ Local Hint Resolve bblock_smem_valid bblock_smem_Some_correct1: core.
+ intros (INCL & EQUIV) m DONTFAIL; unfold smem_valid in * |-.
+ destruct (run ge p1 m) as [m1|] eqn: RUN1; cbn; try congruence.
+ assert (X: forall x, term_eval ge (bblock_smem p1 x) m = Some (m1 x)); eauto.
+ eapply bblock_smem_Some_correct2; eauto.
+ + destruct (INCL m); intuition eauto.
+ congruence.
+ + intro x; erewrite <- EQUIV; intuition eauto.
+ congruence.
+Qed.
+
+Lemma smem_valid_set_decompose_1 d t x m:
+ smem_valid ge (smem_set d x t) m -> smem_valid ge d m.
+Proof.
+ unfold smem_valid; intros ((PRE1 & PRE2) & VALID); split.
+ + intuition.
+ + intros x0 H. case (R.eq_dec x x0).
+ * intuition congruence.
+ * intros DIFF; eapply VALID. erewrite set_spec_diff; eauto.
+Qed.
+
+Lemma smem_valid_set_decompose_2 d t x m:
+ smem_valid ge (smem_set d x t) m -> term_eval ge t m <> None.
+Proof.
+ unfold smem_valid; intros ((PRE1 & PRE2) & VALID) H.
+ generalize (VALID x); rewrite set_spec_eq.
+ tauto.
+Qed.
+
+Lemma smem_valid_set_proof d x t m:
+ smem_valid ge d m -> term_eval ge t m <> None -> smem_valid ge (smem_set d x t) m.
+Proof.
+ unfold smem_valid; intros (PRE & VALID) PREt. split.
+ + split; auto.
+ + intros x0; unfold smem_set; cbn; case (R.eq_dec x x0); intros; subst; auto.
+Qed.
+
+
+End SIMU_THEORY.
+
+(** REMARK: this theorem reformulates the lemma above in a more abstract way (but relies on functional_extensionality axiom).
+*)
+Definition smem_correct ge (d: smem) (m: mem) (om: option mem): Prop:=
+ forall m', om=Some m' <-> (d.(pre) ge m /\ forall x, term_eval ge (d x) m = Some (m' x)).
+
+Lemma bblock_smem_correct ge p m: smem_correct ge (bblock_smem p) m (run ge p m).
+Proof.
+ unfold smem_correct; cbn; intros m'; split.
+ + intros; split.
+ * eapply bblock_smem_valid; eauto.
+ * eapply bblock_smem_Some_correct1; eauto.
+ + intros (H1 & H2).
+ destruct (bblock_smem_Some_correct2 ge p m m') as (m2 & X & Y); eauto.
+ rewrite X. f_equal.
+ apply FunctionalExtensionality.functional_extensionality; auto.
+Qed.
+
+End SimuTheory.
diff --git a/scheduling/postpass_lib/ForwardSimulationBlock.v b/scheduling/postpass_lib/ForwardSimulationBlock.v
new file mode 100644
index 00000000..cc6ecdd8
--- /dev/null
+++ b/scheduling/postpass_lib/ForwardSimulationBlock.v
@@ -0,0 +1,387 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(***
+
+Auxiliary lemmas on starN and forward_simulation
+in order to prove the forward simulation of Mach -> Machblock.
+
+***)
+
+Require Import Relations.
+Require Import Wellfounded.
+Require Import Coqlib.
+Require Import Events.
+Require Import Globalenvs.
+Require Import Smallstep.
+Require Import Lia.
+
+Local Open Scope nat_scope.
+
+
+(** Auxiliary lemma on starN *)
+Section starN_lemma.
+
+Variable L: semantics.
+
+Local Hint Resolve starN_refl starN_step Eapp_assoc: core.
+
+Lemma starN_split n s t s':
+ starN (step L) (globalenv L) n s t s' ->
+ forall m k, n=m+k ->
+ exists (t1 t2:trace) s0, starN (step L) (globalenv L) m s t1 s0 /\ starN (step L) (globalenv L) k s0 t2 s' /\ t=t1**t2.
+Proof.
+ induction 1; cbn.
+ + intros m k H; assert (X: m=0); try lia.
+ assert (X0: k=0); try lia.
+ subst; repeat (eapply ex_intro); intuition eauto.
+ + intros m; destruct m as [| m']; cbn.
+ - intros k H2; subst; repeat (eapply ex_intro); intuition eauto.
+ - intros k H2. inversion H2.
+ exploit (IHstarN m' k); eauto. intro.
+ destruct H3 as (t5 & t6 & s0 & H5 & H6 & H7).
+ repeat (eapply ex_intro).
+ instantiate (1 := t6); instantiate (1 := t1 ** t5); instantiate (1 := s0).
+ intuition eauto. subst. auto.
+Qed.
+
+Lemma starN_tailstep n s t1 s':
+ starN (step L) (globalenv L) n s t1 s' ->
+ forall (t t2:trace) s'',
+ Step L s' t2 s'' -> t = t1 ** t2 -> starN (step L) (globalenv L) (S n) s t s''.
+Proof.
+ induction 1; cbn.
+ + intros t t1 s0; autorewrite with trace_rewrite.
+ intros; subst; eapply starN_step; eauto.
+ autorewrite with trace_rewrite; auto.
+ + intros. eapply starN_step; eauto.
+ intros; subst; autorewrite with trace_rewrite; auto.
+Qed.
+
+End starN_lemma.
+
+
+
+(** General scheme from a "match_states" relation *)
+
+Section ForwardSimuBlock_REL.
+
+Variable L1 L2: semantics.
+
+
+(** Hypothèses de la preuve *)
+
+Variable dist_end_block: state L1 -> nat.
+
+Hypothesis simu_mid_block:
+ forall s1 t s1', Step L1 s1 t s1' -> (dist_end_block s1)<>0 -> t = E0 /\ dist_end_block s1=S (dist_end_block s1').
+
+Hypothesis public_preserved:
+ forall id, Senv.public_symbol (symbolenv L2) id = Senv.public_symbol (symbolenv L1) id.
+
+Variable match_states: state L1 -> state L2 -> Prop.
+
+Hypothesis match_initial_states:
+ forall s1, initial_state L1 s1 -> exists s2, match_states s1 s2 /\ initial_state L2 s2.
+
+Hypothesis match_final_states:
+ forall s1 s2 r, final_state L1 s1 r -> match_states s1 s2 -> final_state L2 s2 r.
+
+Hypothesis final_states_end_block:
+ forall s1 t s1' r, Step L1 s1 t s1' -> final_state L1 s1' r -> dist_end_block s1 = 0.
+
+Hypothesis simu_end_block:
+ forall s1 t s1' s2, starN (step L1) (globalenv L1) (S (dist_end_block s1)) s1 t s1' -> match_states s1 s2 -> exists s2', Step L2 s2 t s2' /\ match_states s1' s2'.
+
+
+(** Introduction d'une sémantique par bloc sur L1 appelée "memoL1" *)
+
+Local Hint Resolve starN_refl starN_step: core.
+
+Definition follows_in_block (head current: state L1): Prop :=
+ dist_end_block head >= dist_end_block current
+ /\ starN (step L1) (globalenv L1) (minus (dist_end_block head) (dist_end_block current)) head E0 current.
+
+Lemma follows_in_block_step (head previous next: state L1):
+ forall t, follows_in_block head previous -> Step L1 previous t next -> (dist_end_block previous)<>0 -> follows_in_block head next.
+Proof.
+ intros t [H1 H2] H3 H4.
+ destruct (simu_mid_block _ _ _ H3 H4) as [H5 H6]; subst.
+ constructor 1.
+ + lia.
+ + replace (dist_end_block head - dist_end_block next) with (S (dist_end_block head - dist_end_block previous)).
+ - eapply starN_tailstep; eauto.
+ - lia.
+Qed.
+
+Lemma follows_in_block_init (head current: state L1):
+ forall t, Step L1 head t current -> (dist_end_block head)<>0 -> follows_in_block head current.
+Proof.
+ intros t H3 H4.
+ destruct (simu_mid_block _ _ _ H3 H4) as [H5 H6]; subst.
+ constructor 1.
+ + lia.
+ + replace (dist_end_block head - dist_end_block current) with 1.
+ - eapply starN_tailstep; eauto.
+ - lia.
+Qed.
+
+
+Record memostate := {
+ real: state L1;
+ memorized: option (state L1);
+ memo_star: forall head, memorized = Some head -> follows_in_block head real;
+ memo_final: forall r, final_state L1 real r -> memorized = None
+}.
+
+Definition head (s: memostate): state L1 :=
+ match memorized s with
+ | None => real s
+ | Some s' => s'
+ end.
+
+Lemma head_followed (s: memostate): follows_in_block (head s) (real s).
+Proof.
+ destruct s as [rs ms Hs]. cbn.
+ destruct ms as [ms|]; unfold head; cbn; auto.
+ constructor 1.
+ lia.
+ replace (dist_end_block rs - dist_end_block rs)%nat with O.
+ + apply starN_refl; auto.
+ + lia.
+Qed.
+
+Inductive is_well_memorized (s s': memostate): Prop :=
+ | StartBloc:
+ dist_end_block (real s) <> O ->
+ memorized s = None ->
+ memorized s' = Some (real s) ->
+ is_well_memorized s s'
+ | MidBloc:
+ dist_end_block (real s) <> O ->
+ memorized s <> None ->
+ memorized s' = memorized s ->
+ is_well_memorized s s'
+ | ExitBloc:
+ dist_end_block (real s) = O ->
+ memorized s' = None ->
+ is_well_memorized s s'.
+
+Local Hint Resolve StartBloc MidBloc ExitBloc: core.
+
+Definition memoL1 := {|
+ state := memostate;
+ genvtype := genvtype L1;
+ step := fun ge s t s' =>
+ step L1 ge (real s) t (real s')
+ /\ is_well_memorized s s' ;
+ initial_state := fun s => initial_state L1 (real s) /\ memorized s = None;
+ final_state := fun s r => final_state L1 (real s) r;
+ globalenv:= globalenv L1;
+ symbolenv:= symbolenv L1
+|}.
+
+
+(** Preuve des 2 forward simulations: L1 -> memoL1 et memoL1 -> L2 *)
+
+Lemma discr_dist_end s:
+ {dist_end_block s = O} + {dist_end_block s <> O}.
+Proof.
+ destruct (dist_end_block s); cbn; intuition.
+Qed.
+
+Lemma memo_simulation_step:
+ forall s1 t s1', Step L1 s1 t s1' ->
+ forall s2, s1 = (real s2) -> exists s2', Step memoL1 s2 t s2' /\ s1' = (real s2').
+Proof.
+ intros s1 t s1' H1 [rs2 ms2 Hmoi] H2. cbn in H2; subst.
+ destruct (discr_dist_end rs2) as [H3 | H3].
+ + refine (ex_intro _ {|real:=s1'; memorized:=None |} _); cbn.
+ intuition.
+ + destruct ms2 as [s|].
+ - refine (ex_intro _ {|real:=s1'; memorized:=Some s |} _); cbn.
+ intuition.
+ - refine (ex_intro _ {|real:=s1'; memorized:=Some rs2 |} _); cbn.
+ intuition.
+ Unshelve.
+ * intros; discriminate.
+ * intros; auto.
+ * intros head X; injection X; clear X; intros; subst.
+ eapply follows_in_block_step; eauto.
+ * intros r X; erewrite final_states_end_block in H3; intuition eauto.
+ * intros head X; injection X; clear X; intros; subst.
+ eapply follows_in_block_init; eauto.
+ * intros r X; erewrite final_states_end_block in H3; intuition eauto.
+Qed.
+
+Lemma forward_memo_simulation_1: forward_simulation L1 memoL1.
+Proof.
+ apply forward_simulation_step with (match_states:=fun s1 s2 => s1 = (real s2)); auto.
+ + intros s1 H; eapply ex_intro with (x:={|real:=s1; memorized:=None |}); cbn.
+ intuition.
+ + intros; subst; auto.
+ + intros; exploit memo_simulation_step; eauto.
+ Unshelve.
+ * intros; discriminate.
+ * auto.
+Qed.
+
+Lemma forward_memo_simulation_2: forward_simulation memoL1 L2.
+Proof.
+ unfold memoL1; cbn.
+ apply forward_simulation_opt with (measure:=fun s => dist_end_block (real s)) (match_states:=fun s1 s2 => match_states (head s1) s2); cbn; auto.
+ + intros s1 [H0 H1]; destruct (match_initial_states (real s1) H0).
+ unfold head; rewrite H1.
+ intuition eauto.
+ + intros s1 s2 r X H0; unfold head in X.
+ erewrite memo_final in X; eauto.
+ + intros s1 t s1' [H1 H2] s2 H; subst.
+ destruct H2 as [ H0 H2 H3 | H0 H2 H3 | H0 H2].
+ - (* StartBloc *)
+ constructor 2. destruct (simu_mid_block (real s1) t (real s1')) as [H5 H4]; auto.
+ unfold head in * |- *. rewrite H2 in H. rewrite H3. rewrite H4. intuition.
+ - (* MidBloc *)
+ constructor 2. destruct (simu_mid_block (real s1) t (real s1')) as [H5 H4]; auto.
+ unfold head in * |- *. rewrite H3. rewrite H4. intuition.
+ destruct (memorized s1); cbn; auto. tauto.
+ - (* EndBloc *)
+ constructor 1.
+ destruct (simu_end_block (head s1) t (real s1') s2) as (s2' & H3 & H4); auto.
+ * destruct (head_followed s1) as [H4 H3].
+ replace (dist_end_block (head s1) - dist_end_block (real s1)) with (dist_end_block (head s1)) in H3; try lia.
+ eapply starN_tailstep; eauto.
+ * unfold head; rewrite H2; cbn. intuition eauto.
+Qed.
+
+Lemma forward_simulation_block_rel: forward_simulation L1 L2.
+Proof.
+ eapply compose_forward_simulations.
+ eapply forward_memo_simulation_1.
+ apply forward_memo_simulation_2.
+Qed.
+
+
+End ForwardSimuBlock_REL.
+
+
+
+(* An instance of the previous scheme, when there is a translation from L1 states to L2 states
+
+Here, we do not require that the sequence of S2 states does exactly match the sequence of L1 states by trans_state.
+This is because the exact matching is broken in Machblock on "goto" instruction (due to the find_label).
+
+However, the Machblock state after a goto remains "equivalent" to the trans_state of the Mach state in the sense of "equiv_on_next_step" below...
+
+*)
+
+
+Section ForwardSimuBlock_TRANS.
+
+Variable L1 L2: semantics.
+
+Variable trans_state: state L1 -> state L2.
+
+Definition equiv_on_next_step (P Q: Prop) s2_a s2_b: Prop :=
+ (P -> (forall t s', Step L2 s2_a t s' <-> Step L2 s2_b t s')) /\ (Q -> (forall r, (final_state L2 s2_a r) <-> (final_state L2 s2_b r))).
+
+Definition match_states s1 s2: Prop :=
+ equiv_on_next_step (exists t s1', Step L1 s1 t s1') (exists r, final_state L1 s1 r) s2 (trans_state s1).
+
+Lemma match_states_trans_state s1: match_states s1 (trans_state s1).
+Proof.
+ unfold match_states, equiv_on_next_step. intuition.
+Qed.
+
+Variable dist_end_block: state L1 -> nat.
+
+Hypothesis simu_mid_block:
+ forall s1 t s1', Step L1 s1 t s1' -> (dist_end_block s1)<>0 -> t = E0 /\ dist_end_block s1=S (dist_end_block s1').
+
+Hypothesis public_preserved:
+ forall id, Senv.public_symbol (symbolenv L2) id = Senv.public_symbol (symbolenv L1) id.
+
+Hypothesis match_initial_states:
+ forall s1, initial_state L1 s1 -> exists s2, match_states s1 s2 /\ initial_state L2 s2.
+
+Hypothesis match_final_states:
+ forall s1 r, final_state L1 s1 r -> final_state L2 (trans_state s1) r.
+
+Hypothesis final_states_end_block:
+ forall s1 t s1' r, Step L1 s1 t s1' -> final_state L1 s1' r -> dist_end_block s1 = 0.
+
+Hypothesis simu_end_block:
+ forall s1 t s1', starN (step L1) (globalenv L1) (S (dist_end_block s1)) s1 t s1' -> exists s2', Step L2 (trans_state s1) t s2' /\ match_states s1' s2'.
+
+Lemma forward_simulation_block_trans: forward_simulation L1 L2.
+Proof.
+ eapply forward_simulation_block_rel with (dist_end_block:=dist_end_block) (match_states:=match_states); try tauto.
+ + (* final_states *) intros s1 s2 r H1 [H2 H3]. rewrite H3; eauto.
+ + (* simu_end_block *)
+ intros s1 t s1' s2 H1 [H2a H2b]. exploit simu_end_block; eauto.
+ intros (s2' & H3 & H4); econstructor 1; intuition eauto.
+ rewrite H2a; auto.
+ inversion_clear H1. eauto.
+Qed.
+
+End ForwardSimuBlock_TRANS.
+
+
+(* another version with a relation [trans_state_R] instead of a function [trans_state] *)
+Section ForwardSimuBlock_TRANS_R.
+
+Variable L1 L2: semantics.
+
+Variable trans_state_R: state L1 -> state L2 -> Prop.
+
+Definition match_states_R s1 s2: Prop :=
+ exists s2', trans_state_R s1 s2' /\ equiv_on_next_step _ (exists t s1', Step L1 s1 t s1') (exists r, final_state L1 s1 r) s2 s2'.
+
+Lemma match_states_trans_state_R s1 s2: trans_state_R s1 s2 -> match_states_R s1 s2.
+Proof.
+ unfold match_states, equiv_on_next_step. firstorder.
+Qed.
+
+Variable dist_end_block: state L1 -> nat.
+
+Hypothesis simu_mid_block:
+ forall s1 t s1', Step L1 s1 t s1' -> (dist_end_block s1)<>0 -> t = E0 /\ dist_end_block s1=S (dist_end_block s1').
+
+Hypothesis public_preserved:
+ forall id, Senv.public_symbol (symbolenv L2) id = Senv.public_symbol (symbolenv L1) id.
+
+Hypothesis match_initial_states:
+ forall s1, initial_state L1 s1 -> exists s2, match_states_R s1 s2 /\ initial_state L2 s2.
+
+Hypothesis match_final_states:
+ forall s1 s2 r, final_state L1 s1 r -> trans_state_R s1 s2 -> final_state L2 s2 r.
+
+Hypothesis final_states_end_block:
+ forall s1 t s1' r, Step L1 s1 t s1' -> final_state L1 s1' r -> dist_end_block s1 = 0.
+
+Hypothesis simu_end_block:
+ forall s1 t s1' s2, starN (step L1) (globalenv L1) (S (dist_end_block s1)) s1 t s1' -> trans_state_R s1 s2 -> exists s2', Step L2 s2 t s2' /\ match_states_R s1' s2'.
+
+Lemma forward_simulation_block_trans_R: forward_simulation L1 L2.
+Proof.
+ eapply forward_simulation_block_rel with (dist_end_block:=dist_end_block) (match_states:=match_states_R); try tauto.
+ + (* final_states *) intros s1 s2 r H1 (s2' & H2 & H3 & H4). rewrite H4; eauto.
+ + (* simu_end_block *)
+ intros s1 t s1' s2 H1 (s2' & H2 & H2a & H2b). exploit simu_end_block; eauto.
+ intros (x & Hx & (y & H3 & H4 & H5)). repeat (econstructor; eauto).
+ rewrite H2a; eauto.
+ inversion_clear H1. eauto.
+Qed.
+
+End ForwardSimuBlock_TRANS_R.
+
diff --git a/scheduling/postpass_lib/Machblock.v b/scheduling/postpass_lib/Machblock.v
new file mode 100644
index 00000000..b588cca8
--- /dev/null
+++ b/scheduling/postpass_lib/Machblock.v
@@ -0,0 +1,388 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(** Abstract syntax and semantics of a Mach variant, structured with basic-blocks. *)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Events.
+Require Import Smallstep.
+Require Import Op.
+Require Import Locations.
+Require Import Conventions.
+Require Stacklayout.
+Require Import Mach.
+Require Import Linking.
+Require Import Lia.
+
+(** * Abstract Syntax *)
+
+(** ** basic instructions (ie no control-flow) *)
+Inductive basic_inst: Type :=
+ | MBgetstack: ptrofs -> typ -> mreg -> basic_inst
+ | MBsetstack: mreg -> ptrofs -> typ -> basic_inst
+ | MBgetparam: ptrofs -> typ -> mreg -> basic_inst
+ | MBop: operation -> list mreg -> mreg -> basic_inst
+ | MBload: trapping_mode -> memory_chunk -> addressing -> list mreg -> mreg -> basic_inst
+ | MBstore: memory_chunk -> addressing -> list mreg -> mreg -> basic_inst
+ .
+
+Definition bblock_body := list basic_inst.
+
+(** ** control flow instructions *)
+Inductive control_flow_inst: Type :=
+ | MBcall: signature -> mreg + ident -> control_flow_inst
+ | MBtailcall: signature -> mreg + ident -> control_flow_inst
+ | MBbuiltin: external_function -> list (builtin_arg mreg) -> builtin_res mreg -> control_flow_inst
+ | MBgoto: label -> control_flow_inst
+ | MBcond: condition -> list mreg -> label -> control_flow_inst
+ | MBjumptable: mreg -> list label -> control_flow_inst
+ | MBreturn: control_flow_inst
+ .
+
+(** ** basic block *)
+Record bblock := mk_bblock {
+ header: list label;
+ body: bblock_body;
+ exit: option control_flow_inst
+}.
+
+Lemma bblock_eq:
+ forall b1 b2,
+ header b1 = header b2 ->
+ body b1 = body b2 ->
+ exit b1 = exit b2 ->
+ b1 = b2.
+Proof.
+ intros. destruct b1. destruct b2.
+ cbn in *. subst. auto.
+Qed.
+
+Definition length_opt {A} (o: option A) : nat :=
+ match o with
+ | Some o => 1
+ | None => 0
+ end.
+
+Definition size (b:bblock): nat := (length (header b))+(length (body b))+(length_opt (exit b)).
+
+Lemma size_null b:
+ size b = 0%nat ->
+ header b = nil /\ body b = nil /\ exit b = None.
+Proof.
+ destruct b as [h b e]. cbn. unfold size. cbn.
+ intros H.
+ assert (length h = 0%nat) as Hh; [ lia |].
+ assert (length b = 0%nat) as Hb; [ lia |].
+ assert (length_opt e = 0%nat) as He; [ lia|].
+ repeat split.
+ destruct h; try (cbn in Hh; discriminate); auto.
+ destruct b; try (cbn in Hb; discriminate); auto.
+ destruct e; try (cbn in He; discriminate); auto.
+Qed.
+
+(** ** programs *)
+
+Definition code := list bblock.
+
+Record function: Type := mkfunction
+ { fn_sig: signature;
+ fn_code: code;
+ fn_stacksize: Z;
+ fn_link_ofs: ptrofs;
+ fn_retaddr_ofs: ptrofs }.
+
+Definition fundef := AST.fundef function.
+
+Definition program := AST.program fundef unit.
+
+Definition genv := Genv.t fundef unit.
+
+(** * Operational (blockstep) semantics ***)
+
+Lemma in_dec (lbl: label) (l: list label): { List.In lbl l } + { ~(List.In lbl l) }.
+Proof.
+ apply List.in_dec.
+ apply Pos.eq_dec.
+Qed.
+
+Definition is_label (lbl: label) (bb: bblock) : bool :=
+ if in_dec lbl (header bb) then true else false.
+
+Lemma is_label_correct_true lbl bb:
+ List.In lbl (header bb) <-> is_label lbl bb = true.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); cbn; intuition.
+Qed.
+
+Lemma is_label_correct_false lbl bb:
+ ~(List.In lbl (header bb)) <-> is_label lbl bb = false.
+Proof.
+ unfold is_label; destruct (in_dec lbl (header bb)); cbn; intuition.
+Qed.
+
+
+Local Open Scope nat_scope.
+
+Fixpoint find_label (lbl: label) (c: code) {struct c} : option code :=
+ match c with
+ | nil => None
+ | bb1 :: bbl => if is_label lbl bb1 then Some c else find_label lbl bbl
+ end.
+
+Section RELSEM.
+
+Variable rao:function -> code -> ptrofs -> Prop.
+Variable ge:genv.
+
+Definition find_function_ptr
+ (ge: genv) (ros: mreg + ident) (rs: regset) : option block :=
+ match ros with
+ | inl r =>
+ match rs r with
+ | Vptr b ofs => if Ptrofs.eq ofs Ptrofs.zero then Some b else None
+ | _ => None
+ end
+ | inr symb =>
+ Genv.find_symbol ge symb
+ end.
+
+(** ** Machblock execution states. *)
+
+Inductive stackframe: Type :=
+ | Stackframe:
+ forall (f: block) (**r pointer to calling function *)
+ (sp: val) (**r stack pointer in calling function *)
+ (retaddr: val) (**r Asm return address in calling function *)
+ (c: code), (**r program point in calling function *)
+ stackframe.
+
+Inductive state: Type :=
+ | State:
+ forall (stack: list stackframe) (**r call stack *)
+ (f: block) (**r pointer to current function *)
+ (sp: val) (**r stack pointer *)
+ (c: code) (**r current program point *)
+ (rs: regset) (**r register state *)
+ (m: mem), (**r memory state *)
+ state
+ | Callstate:
+ forall (stack: list stackframe) (**r call stack *)
+ (f: block) (**r pointer to function to call *)
+ (rs: regset) (**r register state *)
+ (m: mem), (**r memory state *)
+ state
+ | Returnstate:
+ forall (stack: list stackframe) (**r call stack *)
+ (rs: regset) (**r register state *)
+ (m: mem), (**r memory state *)
+ state.
+
+Definition parent_sp (s: list stackframe) : val :=
+ match s with
+ | nil => Vnullptr
+ | Stackframe f sp ra c :: s' => sp
+ end.
+
+Definition parent_ra (s: list stackframe) : val :=
+ match s with
+ | nil => Vnullptr
+ | Stackframe f sp ra c :: s' => ra
+ end.
+
+Inductive basic_step (s: list stackframe) (fb: block) (sp: val) (rs: regset) (m:mem): basic_inst -> regset -> mem -> Prop :=
+ | exec_MBgetstack:
+ forall ofs ty dst v,
+ load_stack m sp ty ofs = Some v ->
+ basic_step s fb sp rs m (MBgetstack ofs ty dst) (rs#dst <- v) m
+ | exec_MBsetstack:
+ forall src ofs ty m' rs',
+ store_stack m sp ty ofs (rs src) = Some m' ->
+ rs' = undef_regs (destroyed_by_setstack ty) rs ->
+ basic_step s fb sp rs m (MBsetstack src ofs ty) rs' m'
+ | exec_MBgetparam:
+ forall ofs ty dst v rs' f,
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ load_stack m sp Tptr f.(fn_link_ofs) = Some (parent_sp s) ->
+ load_stack m (parent_sp s) ty ofs = Some v ->
+ rs' = (rs # temp_for_parent_frame <- Vundef # dst <- v) ->
+ basic_step s fb sp rs m (MBgetparam ofs ty dst) rs' m
+ | exec_MBop:
+ forall op args v rs' res,
+ eval_operation ge sp op rs##args m = Some v ->
+ rs' = ((undef_regs (destroyed_by_op op) rs)#res <- v) ->
+ basic_step s fb sp rs m (MBop op args res) rs' m
+ | exec_MBload:
+ forall addr args a v rs' trap chunk dst,
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = Some v ->
+ rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- v) ->
+ basic_step s fb sp rs m (MBload trap chunk addr args dst) rs' m
+ | exec_MBload_notrap1:
+ forall addr args rs' chunk dst,
+ eval_addressing ge sp addr rs##args = None ->
+ rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- Vundef) ->
+ basic_step s fb sp rs m (MBload NOTRAP chunk addr args dst) rs' m
+ | exec_MBload_notrap2:
+ forall addr args a rs' chunk dst,
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.loadv chunk m a = None ->
+ rs' = ((undef_regs (destroyed_by_load chunk addr) rs)#dst <- Vundef) ->
+ basic_step s fb sp rs m (MBload NOTRAP chunk addr args dst) rs' m
+ | exec_MBstore:
+ forall chunk addr args src m' a rs',
+ eval_addressing ge sp addr rs##args = Some a ->
+ Mem.storev chunk m a (rs src) = Some m' ->
+ rs' = undef_regs (destroyed_by_store chunk addr) rs ->
+ basic_step s fb sp rs m (MBstore chunk addr args src) rs' m'
+ .
+
+
+Inductive body_step (s: list stackframe) (f: block) (sp: val): bblock_body -> regset -> mem -> regset -> mem -> Prop :=
+ | exec_nil_body:
+ forall rs m,
+ body_step s f sp nil rs m rs m
+ | exec_cons_body:
+ forall rs m bi p rs' m' rs'' m'',
+ basic_step s f sp rs m bi rs' m' ->
+ body_step s f sp p rs' m' rs'' m'' ->
+ body_step s f sp (bi::p) rs m rs'' m''
+ .
+
+Inductive cfi_step: control_flow_inst -> state -> trace -> state -> Prop :=
+ | exec_MBcall:
+ forall s fb sp sig ros c b rs m f f' ra,
+ find_function_ptr ge ros rs = Some f' ->
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ rao f c ra ->
+ cfi_step (MBcall sig ros) (State s fb sp (b::c) rs m)
+ E0 (Callstate (Stackframe fb sp (Vptr fb ra) c :: s)
+ f' rs m)
+ | exec_MBtailcall:
+ forall s fb stk soff sig ros c rs m f f' m',
+ find_function_ptr ge ros rs = Some f' ->
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ load_stack m (Vptr stk soff) Tptr f.(fn_link_ofs) = Some (parent_sp s) ->
+ load_stack m (Vptr stk soff) Tptr f.(fn_retaddr_ofs) = Some (parent_ra s) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ cfi_step (MBtailcall sig ros) (State s fb (Vptr stk soff) c rs m)
+ E0 (Callstate s f' rs m')
+ | exec_MBbuiltin:
+ forall s f sp rs m ef args res b c vargs t vres rs' m',
+ eval_builtin_args ge rs sp m args vargs ->
+ external_call ef ge vargs m t vres m' ->
+ rs' = set_res res vres (undef_regs (destroyed_by_builtin ef) rs) ->
+ cfi_step (MBbuiltin ef args res) (State s f sp (b :: c) rs m)
+ t (State s f sp c rs' m')
+ | exec_MBgoto:
+ forall s fb f sp lbl c rs m c',
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ find_label lbl f.(fn_code) = Some c' ->
+ cfi_step (MBgoto lbl) (State s fb sp c rs m)
+ E0 (State s fb sp c' rs m)
+ | exec_MBcond_true:
+ forall s fb f sp cond args lbl c rs m c' rs',
+ eval_condition cond rs##args m = Some true ->
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ find_label lbl f.(fn_code) = Some c' ->
+ rs' = undef_regs (destroyed_by_cond cond) rs ->
+ cfi_step (MBcond cond args lbl) (State s fb sp c rs m)
+ E0 (State s fb sp c' rs' m)
+ | exec_MBcond_false:
+ forall s f sp cond args lbl b c rs m rs',
+ eval_condition cond rs##args m = Some false ->
+ rs' = undef_regs (destroyed_by_cond cond) rs ->
+ cfi_step (MBcond cond args lbl) (State s f sp (b :: c) rs m)
+ E0 (State s f sp c rs' m)
+ | exec_MBjumptable:
+ forall s fb f sp arg tbl c rs m n lbl c' rs',
+ rs arg = Vint n ->
+ list_nth_z tbl (Int.unsigned n) = Some lbl ->
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ find_label lbl f.(fn_code) = Some c' ->
+ rs' = undef_regs destroyed_by_jumptable rs ->
+ cfi_step (MBjumptable arg tbl) (State s fb sp c rs m)
+ E0 (State s fb sp c' rs' m)
+ | exec_MBreturn:
+ forall s fb stk soff c rs m f m',
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ load_stack m (Vptr stk soff) Tptr f.(fn_link_ofs) = Some (parent_sp s) ->
+ load_stack m (Vptr stk soff) Tptr f.(fn_retaddr_ofs) = Some (parent_ra s) ->
+ Mem.free m stk 0 f.(fn_stacksize) = Some m' ->
+ cfi_step MBreturn (State s fb (Vptr stk soff) c rs m)
+ E0 (Returnstate s rs m')
+ .
+
+Inductive exit_step: option control_flow_inst -> state -> trace -> state -> Prop :=
+ | exec_Some_exit:
+ forall ctl s t s',
+ cfi_step ctl s t s' ->
+ exit_step (Some ctl) s t s'
+ | exec_None_exit:
+ forall stk f sp b lb rs m,
+ exit_step None (State stk f sp (b::lb) rs m) E0 (State stk f sp lb rs m)
+ .
+
+Inductive step: state -> trace -> state -> Prop :=
+ | exec_bblock:
+ forall sf f sp bb c rs m rs' m' t s',
+ body_step sf f sp (body bb) rs m rs' m' ->
+ exit_step (exit bb) (State sf f sp (bb::c) rs' m') t s' ->
+ step (State sf f sp (bb::c) rs m) t s'
+ | exec_function_internal:
+ forall s fb rs m f m1 m2 m3 stk rs',
+ Genv.find_funct_ptr ge fb = Some (Internal f) ->
+ Mem.alloc m 0 f.(fn_stacksize) = (m1, stk) ->
+ let sp := Vptr stk Ptrofs.zero in
+ store_stack m1 sp Tptr f.(fn_link_ofs) (parent_sp s) = Some m2 ->
+ store_stack m2 sp Tptr f.(fn_retaddr_ofs) (parent_ra s) = Some m3 ->
+ rs' = undef_regs destroyed_at_function_entry rs ->
+ step (Callstate s fb rs m)
+ E0 (State s fb sp f.(fn_code) rs' m3)
+ | exec_function_external:
+ forall s fb rs m t rs' ef args res m',
+ Genv.find_funct_ptr ge fb = Some (External ef) ->
+ extcall_arguments rs m (parent_sp s) (ef_sig ef) args ->
+ external_call ef ge args m t res m' ->
+ rs' = set_pair (loc_result (ef_sig ef)) res (undef_caller_save_regs rs) ->
+ step (Callstate s fb rs m)
+ t (Returnstate s rs' m')
+ | exec_return:
+ forall s f sp ra c rs m,
+ step (Returnstate (Stackframe f sp ra c :: s) rs m)
+ E0 (State s f sp c rs m)
+ .
+
+End RELSEM.
+
+Inductive initial_state (p: program): state -> Prop :=
+ | initial_state_intro: forall fb m0,
+ let ge := Genv.globalenv p in
+ Genv.init_mem p = Some m0 ->
+ Genv.find_symbol ge p.(prog_main) = Some fb ->
+ initial_state p (Callstate nil fb (Regmap.init Vundef) m0).
+
+Inductive final_state: state -> int -> Prop :=
+ | final_state_intro: forall rs m r retcode,
+ loc_result signature_main = One r ->
+ rs r = Vint retcode ->
+ final_state (Returnstate nil rs m) retcode.
+
+Definition semantics (rao: function -> code -> ptrofs -> Prop) (p: program) :=
+ Semantics (step rao) (initial_state p) final_state (Genv.globalenv p).
diff --git a/scheduling/postpass_lib/Machblockgen.v b/scheduling/postpass_lib/Machblockgen.v
new file mode 100644
index 00000000..5a2f2a61
--- /dev/null
+++ b/scheduling/postpass_lib/Machblockgen.v
@@ -0,0 +1,221 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Events.
+Require Import Smallstep.
+Require Import Op.
+Require Import Locations.
+Require Import Conventions.
+Require Stacklayout.
+Require Import Mach.
+Require Import Linking.
+Require Import Machblock.
+
+(** * Tail-recursive (greedy) translation from Mach code to Machblock code *)
+
+Inductive Machblock_inst: Type :=
+| MB_label (lbl: label)
+| MB_basic (bi: basic_inst)
+| MB_cfi (cfi: control_flow_inst).
+
+Definition trans_inst (i:Mach.instruction) : Machblock_inst :=
+ match i with
+ | Mcall sig ros => MB_cfi (MBcall sig ros)
+ | Mtailcall sig ros => MB_cfi (MBtailcall sig ros)
+ | Mbuiltin ef args res => MB_cfi (MBbuiltin ef args res)
+ | Mgoto lbl => MB_cfi (MBgoto lbl)
+ | Mcond cond args lbl => MB_cfi (MBcond cond args lbl)
+ | Mjumptable arg tbl => MB_cfi (MBjumptable arg tbl)
+ | Mreturn => MB_cfi (MBreturn)
+ | Mgetstack ofs ty dst => MB_basic (MBgetstack ofs ty dst)
+ | Msetstack src ofs ty => MB_basic (MBsetstack src ofs ty)
+ | Mgetparam ofs ty dst => MB_basic (MBgetparam ofs ty dst)
+ | Mop op args res => MB_basic (MBop op args res)
+ | Mload trap chunk addr args dst=> MB_basic (MBload trap chunk addr args dst)
+ | Mstore chunk addr args src => MB_basic (MBstore chunk addr args src)
+ | Mlabel l => MB_label l
+ end.
+
+Definition empty_bblock:={| header := nil; body := nil; exit := None |}.
+Extraction Inline empty_bblock.
+
+Definition add_label l bb:={| header := l::(header bb); body := (body bb); exit := (exit bb) |}.
+Extraction Inline add_label.
+
+Definition add_basic bi bb :={| header := nil; body := bi::(body bb); exit := (exit bb) |}.
+Extraction Inline add_basic.
+
+Definition cfi_bblock cfi:={| header := nil; body := nil; exit := Some cfi |}.
+Extraction Inline cfi_bblock.
+
+Definition add_to_new_bblock (i:Machblock_inst) : bblock :=
+ match i with
+ | MB_label l => add_label l empty_bblock
+ | MB_basic i => add_basic i empty_bblock
+ | MB_cfi i => cfi_bblock i
+ end.
+
+(** Adding an instruction to the beginning of a bblock list by
+
+- either adding the instruction to the head of the list,
+
+- or creating a new bblock with the instruction
+*)
+Definition add_to_code (i:Machblock_inst) (bl:code) : code :=
+ match bl with
+ | bh::bl0 => match i with
+ | MB_label l => add_label l bh::bl0
+ | MB_cfi i0 => cfi_bblock i0::bl
+ | MB_basic i0 => match header bh with
+ |_::_ => add_basic i0 empty_bblock::bl
+ | nil => add_basic i0 bh::bl0
+ end
+ end
+ | _ => add_to_new_bblock i::nil
+ end.
+
+Fixpoint trans_code_rev (c: Mach.code) (bl:code) : code :=
+ match c with
+ | nil => bl
+ | i::c0 =>
+ trans_code_rev c0 (add_to_code (trans_inst i) bl)
+ end.
+
+Function trans_code (c: Mach.code) : code :=
+ trans_code_rev (List.rev_append c nil) nil.
+
+Definition transf_function (f: Mach.function) : function :=
+ {| fn_sig:=Mach.fn_sig f;
+ fn_code:=trans_code (Mach.fn_code f);
+ fn_stacksize := Mach.fn_stacksize f;
+ fn_link_ofs := Mach.fn_link_ofs f;
+ fn_retaddr_ofs := Mach.fn_retaddr_ofs f
+ |}.
+
+Definition transf_fundef (f: Mach.fundef) : fundef :=
+ transf_fundef transf_function f.
+
+Definition transf_program (src: Mach.program) : program :=
+ transform_program transf_fundef src.
+
+
+(** * Abstracting trans_code with a simpler inductive relation *)
+
+Inductive is_end_block: Machblock_inst -> code -> Prop :=
+ | End_empty mbi: is_end_block mbi nil
+ | End_basic bi bh bl: header bh <> nil -> is_end_block (MB_basic bi) (bh::bl)
+ | End_cfi cfi bl: bl <> nil -> is_end_block (MB_cfi cfi) bl.
+
+Local Hint Resolve End_empty End_basic End_cfi: core.
+
+Inductive is_trans_code: Mach.code -> code -> Prop :=
+ | Tr_nil: is_trans_code nil nil
+ | Tr_end_block i c bl:
+ is_trans_code c bl ->
+ is_end_block (trans_inst i) bl ->
+ is_trans_code (i::c) (add_to_new_bblock (trans_inst i)::bl)
+ | Tr_add_label i l bh c bl:
+ is_trans_code c (bh::bl) ->
+ i = Mlabel l ->
+ is_trans_code (i::c) (add_label l bh::bl)
+ | Tr_add_basic i bi bh c bl:
+ is_trans_code c (bh::bl) ->
+ trans_inst i = MB_basic bi ->
+ header bh = nil ->
+ is_trans_code (i::c) (add_basic bi bh::bl).
+
+Local Hint Resolve Tr_nil Tr_end_block: core.
+
+Lemma add_to_code_is_trans_code i c bl:
+ is_trans_code c bl ->
+ is_trans_code (i::c) (add_to_code (trans_inst i) bl).
+Proof.
+ destruct bl as [|bh0 bl]; cbn.
+ - intro H. inversion H. subst. eauto.
+ - remember (trans_inst i) as ti.
+ destruct ti as [l|bi|cfi].
+ + intros; eapply Tr_add_label; eauto. destruct i; cbn in * |- *; congruence.
+ + intros. remember (header bh0) as hbh0. destruct hbh0 as [|b].
+ * eapply Tr_add_basic; eauto.
+ * replace (add_basic bi empty_bblock) with (add_to_new_bblock (MB_basic bi)); auto.
+ rewrite Heqti; eapply Tr_end_block; eauto.
+ rewrite <- Heqti. eapply End_basic. congruence.
+ + intros.
+ replace (cfi_bblock cfi) with (add_to_new_bblock (MB_cfi cfi)); auto.
+ rewrite Heqti. eapply Tr_end_block; eauto.
+ rewrite <- Heqti. eapply End_cfi. congruence.
+Qed.
+
+Local Hint Resolve add_to_code_is_trans_code: core.
+
+Lemma trans_code_is_trans_code_rev c1: forall c2 mbi,
+ is_trans_code c2 mbi ->
+ is_trans_code (rev_append c1 c2) (trans_code_rev c1 mbi).
+Proof.
+ induction c1 as [| i c1]; cbn; auto.
+Qed.
+
+Lemma trans_code_is_trans_code c: is_trans_code c (trans_code c).
+Proof.
+ unfold trans_code.
+ rewrite <- rev_alt.
+ rewrite <- (rev_involutive c) at 1.
+ rewrite rev_alt at 1.
+ apply trans_code_is_trans_code_rev; auto.
+Qed.
+
+Lemma add_to_code_is_trans_code_inv i c bl:
+ is_trans_code (i::c) bl -> exists bl0, is_trans_code c bl0 /\ bl = add_to_code (trans_inst i) bl0.
+Proof.
+ intro H; inversion H as [|H0 H1 bl0| | H0 bi bh H1 bl0]; clear H; subst; (repeat econstructor); eauto.
+ + (* case Tr_end_block *) inversion H3; subst; cbn; auto.
+ * destruct (header bh); congruence.
+ * destruct bl0; cbn; congruence.
+ + (* case Tr_add_basic *) rewrite H3. cbn. destruct (header bh); congruence.
+Qed.
+
+Lemma trans_code_is_trans_code_rev_inv c1: forall c2 mbi,
+ is_trans_code (rev_append c1 c2) mbi ->
+ exists mbi0, is_trans_code c2 mbi0 /\ mbi=trans_code_rev c1 mbi0.
+Proof.
+ induction c1 as [| i c1]; cbn; eauto.
+ intros; exploit IHc1; eauto.
+ intros (mbi0 & H1 & H2); subst.
+ exploit add_to_code_is_trans_code_inv; eauto.
+ intros. destruct H0 as [mbi1 [H2 H3]].
+ exists mbi1. split; congruence.
+Qed.
+
+Local Hint Resolve trans_code_is_trans_code: core.
+
+Theorem is_trans_code_inv c bl: is_trans_code c bl <-> bl=(trans_code c).
+Proof.
+ constructor; intros; subst; auto.
+ unfold trans_code.
+ exploit (trans_code_is_trans_code_rev_inv (rev_append c nil) nil bl); eauto.
+ * rewrite <- rev_alt.
+ rewrite <- rev_alt.
+ rewrite (rev_involutive c).
+ apply H.
+ * intros.
+ destruct H0 as [mbi [H0 H1]].
+ inversion H0. subst. reflexivity.
+Qed.
diff --git a/scheduling/postpass_lib/Machblockgenproof.v b/scheduling/postpass_lib/Machblockgenproof.v
new file mode 100644
index 00000000..1d6c6e18
--- /dev/null
+++ b/scheduling/postpass_lib/Machblockgenproof.v
@@ -0,0 +1,810 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib.
+Require Import Maps.
+Require Import AST.
+Require Import Integers.
+Require Import Values.
+Require Import Memory.
+Require Import Globalenvs.
+Require Import Events.
+Require Import Smallstep.
+Require Import Op.
+Require Import Locations.
+Require Import Conventions.
+Require Stacklayout.
+Require Import Mach.
+Require Import Linking.
+Require Import Machblock.
+Require Import Machblockgen.
+Require Import ForwardSimulationBlock.
+Require Import Lia.
+
+Ltac subst_is_trans_code H :=
+ rewrite is_trans_code_inv in H;
+ rewrite <- H in * |- *;
+ rewrite <- is_trans_code_inv in H.
+
+Definition inv_trans_rao (rao: function -> code -> ptrofs -> Prop) (f: Mach.function) (c: Mach.code) :=
+ rao (transf_function f) (trans_code c).
+
+Definition match_prog (p: Mach.program) (tp: Machblock.program) :=
+ match_program (fun _ f tf => tf = transf_fundef f) eq p tp.
+
+Lemma transf_program_match: forall p tp, transf_program p = tp -> match_prog p tp.
+Proof.
+ intros. rewrite <- H. eapply match_transform_program; eauto.
+Qed.
+
+Definition trans_stackframe (msf: Mach.stackframe) : stackframe :=
+ match msf with
+ | Mach.Stackframe f sp retaddr c => Stackframe f sp retaddr (trans_code c)
+ end.
+
+Fixpoint trans_stack (mst: list Mach.stackframe) : list stackframe :=
+ match mst with
+ | nil => nil
+ | msf :: mst0 => (trans_stackframe msf) :: (trans_stack mst0)
+ end.
+
+Definition trans_state (ms: Mach.state): state :=
+ match ms with
+ | Mach.State s f sp c rs m => State (trans_stack s) f sp (trans_code c) rs m
+ | Mach.Callstate s f rs m => Callstate (trans_stack s) f rs m
+ | Mach.Returnstate s rs m => Returnstate (trans_stack s) rs m
+ end.
+
+Section PRESERVATION.
+
+Local Open Scope nat_scope.
+
+Variable prog: Mach.program.
+Variable tprog: Machblock.program.
+Hypothesis TRANSF: match_prog prog tprog.
+Let ge := Genv.globalenv prog.
+Let tge := Genv.globalenv tprog.
+
+
+Variable rao: function -> code -> ptrofs -> Prop.
+
+Definition match_states: Mach.state -> state -> Prop
+ := ForwardSimulationBlock.match_states (Mach.semantics (inv_trans_rao rao) prog) (Machblock.semantics rao tprog) trans_state.
+
+Lemma match_states_trans_state s1: match_states s1 (trans_state s1).
+Proof.
+ apply match_states_trans_state.
+Qed.
+
+Local Hint Resolve match_states_trans_state: core.
+
+Lemma symbols_preserved:
+ forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
+Proof (Genv.find_symbol_match TRANSF).
+
+Lemma senv_preserved:
+ Senv.equiv ge tge.
+Proof (Genv.senv_match TRANSF).
+
+Lemma init_mem_preserved:
+ forall m,
+ Genv.init_mem prog = Some m ->
+ Genv.init_mem tprog = Some m.
+Proof (Genv.init_mem_transf TRANSF).
+
+Lemma prog_main_preserved:
+ prog_main tprog = prog_main prog.
+Proof (match_program_main TRANSF).
+
+Lemma functions_translated:
+ forall b f,
+ Genv.find_funct_ptr ge b = Some f ->
+ exists tf, Genv.find_funct_ptr tge b = Some tf /\ transf_fundef f = tf.
+Proof.
+ intros.
+ exploit (Genv.find_funct_ptr_match TRANSF); eauto. intro.
+ destruct H0 as (cunit & tf & A & B & C).
+ eapply ex_intro. intuition; eauto. subst. eapply A.
+Qed.
+
+Lemma find_function_ptr_same:
+ forall s rs,
+ Mach.find_function_ptr ge s rs = find_function_ptr tge s rs.
+Proof.
+ intros. unfold Mach.find_function_ptr. unfold find_function_ptr.
+ destruct s; auto.
+ rewrite symbols_preserved; auto.
+Qed.
+
+Lemma find_funct_ptr_same:
+ forall f f0,
+ Genv.find_funct_ptr ge f = Some (Internal f0) ->
+ Genv.find_funct_ptr tge f = Some (Internal (transf_function f0)).
+Proof.
+ intros. exploit (Genv.find_funct_ptr_transf TRANSF); eauto.
+Qed.
+
+Lemma find_funct_ptr_same_external:
+ forall f f0,
+ Genv.find_funct_ptr ge f = Some (External f0) ->
+ Genv.find_funct_ptr tge f = Some (External f0).
+Proof.
+ intros. exploit (Genv.find_funct_ptr_transf TRANSF); eauto.
+Qed.
+
+Lemma parent_sp_preserved:
+ forall s,
+ Mach.parent_sp s = parent_sp (trans_stack s).
+Proof.
+ unfold parent_sp. unfold Mach.parent_sp. destruct s; cbn; auto.
+ unfold trans_stackframe. destruct s; cbn; auto.
+Qed.
+
+Lemma parent_ra_preserved:
+ forall s,
+ Mach.parent_ra s = parent_ra (trans_stack s).
+Proof.
+ unfold parent_ra. unfold Mach.parent_ra. destruct s; cbn; auto.
+ unfold trans_stackframe. destruct s; cbn; auto.
+Qed.
+
+Lemma external_call_preserved:
+ forall ef args m t res m',
+ external_call ef ge args m t res m' ->
+ external_call ef tge args m t res m'.
+Proof.
+ intros. eapply external_call_symbols_preserved; eauto.
+ apply senv_preserved.
+Qed.
+
+Lemma Mach_find_label_split l i c c':
+ Mach.find_label l (i :: c) = Some c' ->
+ (i=Mlabel l /\ c' = c) \/ (i <> Mlabel l /\ Mach.find_label l c = Some c').
+Proof.
+ intros H.
+ destruct i; try (constructor 2; split; auto; discriminate ).
+ destruct (peq l0 l) as [P|P].
+ - constructor. subst l0; split; auto.
+ revert H. unfold Mach.find_label. cbn. rewrite peq_true.
+ intros H; injection H; auto.
+ - constructor 2. split.
+ + intro F. injection F. intros. contradict P; auto.
+ + revert H. unfold Mach.find_label. cbn. rewrite peq_false; auto.
+Qed.
+
+Lemma find_label_is_end_block_not_label i l c bl:
+ is_end_block (trans_inst i) bl ->
+ is_trans_code c bl ->
+ i <> Mlabel l -> find_label l (add_to_new_bblock (trans_inst i) :: bl) = find_label l bl.
+Proof.
+ intros H H0 H1.
+ unfold find_label.
+ remember (is_label l _) as b.
+ replace b with false; auto.
+ subst; unfold is_label.
+ destruct i; cbn in * |- *; try (destruct (in_dec l nil); intuition).
+ inversion H.
+ destruct (in_dec l (l0::nil)) as [H6|H6]; auto.
+ cbn in H6; intuition try congruence.
+Qed.
+
+Lemma find_label_at_begin l bh bl:
+ In l (header bh)
+ -> find_label l (bh :: bl) = Some (bh::bl).
+Proof.
+ unfold find_label; rewrite is_label_correct_true; intro H; rewrite H; cbn; auto.
+Qed.
+
+Lemma find_label_add_label_diff l bh bl:
+ ~(In l (header bh)) ->
+ find_label l (bh::bl) = find_label l bl.
+Proof.
+ unfold find_label; rewrite is_label_correct_false; intro H; rewrite H; cbn; auto.
+Qed.
+
+Definition concat (h: list label) (c: code): code :=
+ match c with
+ | nil => {| header := h; body := nil; exit := None |}::nil
+ | b::c' => {| header := h ++ (header b); body := body b; exit := exit b |}::c'
+ end.
+
+Lemma find_label_transcode_preserved:
+ forall l c c',
+ Mach.find_label l c = Some c' ->
+ exists h, In l h /\ find_label l (trans_code c) = Some (concat h (trans_code c')).
+Proof.
+ intros l c. remember (trans_code _) as bl.
+ rewrite <- is_trans_code_inv in * |-.
+ induction Heqbl.
+ + (* Tr_nil *)
+ intros; exists (l::nil); cbn in * |- *; intuition.
+ discriminate.
+ + (* Tr_end_block *)
+ intros.
+ exploit Mach_find_label_split; eauto.
+ clear H0; destruct 1 as [(H0&H2)|(H0&H2)].
+ - subst. rewrite find_label_at_begin; cbn; auto.
+ inversion H as [mbi H1 H2| | ].
+ subst.
+ inversion Heqbl.
+ subst.
+ exists (l :: nil); cbn; eauto.
+ - exploit IHHeqbl; eauto.
+ destruct 1 as (h & H3 & H4).
+ exists h.
+ split; auto.
+ erewrite find_label_is_end_block_not_label;eauto.
+ + (* Tr_add_label *)
+ intros.
+ exploit Mach_find_label_split; eauto.
+ clear H0; destruct 1 as [(H0&H2)|(H0&H2)].
+ - subst.
+ inversion H0 as [H1].
+ clear H0.
+ erewrite find_label_at_begin; cbn; eauto.
+ subst_is_trans_code Heqbl.
+ exists (l :: nil); cbn; eauto.
+ - subst; assert (H: l0 <> l); try congruence; clear H0.
+ exploit IHHeqbl; eauto.
+ clear IHHeqbl Heqbl.
+ intros (h & H3 & H4).
+ cbn; unfold is_label, add_label; cbn.
+ destruct (in_dec l (l0::header bh)) as [H5|H5]; cbn in H5.
+ * destruct H5; try congruence.
+ exists (l0::h); cbn; intuition.
+ rewrite find_label_at_begin in H4; auto.
+ apply f_equal. inversion H4 as [H5]. clear H4.
+ destruct (trans_code c'); cbn in * |- *;
+ inversion H5; subst; cbn; auto.
+ * exists h. intuition.
+ erewrite <- find_label_add_label_diff; eauto.
+ + (* Tr_add_basic *)
+ intros.
+ exploit Mach_find_label_split; eauto.
+ destruct 1 as [(H2&H3)|(H2&H3)].
+ rewrite H2 in H. unfold trans_inst in H. congruence.
+ exploit IHHeqbl; eauto.
+ clear IHHeqbl Heqbl.
+ intros (h & H4 & H5).
+ rewrite find_label_add_label_diff; auto.
+ rewrite find_label_add_label_diff in H5; eauto.
+ rewrite H0; auto.
+Qed.
+
+Lemma find_label_preserved:
+ forall l f c,
+ Mach.find_label l (Mach.fn_code f) = Some c ->
+ exists h, In l h /\ find_label l (fn_code (transf_function f)) = Some (concat h (trans_code c)).
+Proof.
+ intros. replace (fn_code (transf_function f)) with (trans_code (Mach.fn_code f)); eauto.
+ apply find_label_transcode_preserved; auto.
+Qed.
+
+Lemma mem_free_preserved:
+ forall m stk f,
+ Mem.free m stk 0 (Mach.fn_stacksize f) = Mem.free m stk 0 (fn_stacksize (transf_function f)).
+Proof.
+ intros. auto.
+Qed.
+
+Local Hint Resolve symbols_preserved senv_preserved init_mem_preserved prog_main_preserved functions_translated
+ parent_sp_preserved: core.
+
+
+Definition dist_end_block_code (c: Mach.code) :=
+ match trans_code c with
+ | nil => 0
+ | bh::_ => (size bh-1)%nat
+ end.
+
+Definition dist_end_block (s: Mach.state): nat :=
+ match s with
+ | Mach.State _ _ _ c _ _ => dist_end_block_code c
+ | _ => 0
+ end.
+
+Local Hint Resolve exec_nil_body exec_cons_body: core.
+Local Hint Resolve exec_MBgetstack exec_MBsetstack exec_MBgetparam exec_MBop exec_MBload exec_MBstore: core.
+
+Lemma size_add_label l bh: size (add_label l bh) = size bh + 1.
+Proof.
+ unfold add_label, size; cbn; lia.
+Qed.
+
+Lemma size_add_basic bi bh: header bh = nil -> size (add_basic bi bh) = size bh + 1.
+Proof.
+ intro H. unfold add_basic, size; rewrite H; cbn. lia.
+Qed.
+
+
+Lemma size_add_to_newblock i: size (add_to_new_bblock i) = 1.
+Proof.
+ destruct i; auto.
+Qed.
+
+
+Lemma dist_end_block_code_simu_mid_block i c:
+ dist_end_block_code (i::c) <> 0 ->
+ (dist_end_block_code (i::c) = Datatypes.S (dist_end_block_code c)).
+Proof.
+ unfold dist_end_block_code.
+ remember (trans_code (i::c)) as bl.
+ rewrite <- is_trans_code_inv in Heqbl.
+ inversion Heqbl as [|bl0 H| |]; subst; clear Heqbl.
+ - rewrite size_add_to_newblock; lia.
+ - rewrite size_add_label;
+ subst_is_trans_code H.
+ lia.
+ - rewrite size_add_basic; auto.
+ subst_is_trans_code H.
+ lia.
+Qed.
+
+Local Hint Resolve dist_end_block_code_simu_mid_block: core.
+
+
+Lemma size_nonzero c b bl:
+ is_trans_code c (b :: bl) -> size b <> 0.
+Proof.
+ intros H; inversion H; subst.
+ - rewrite size_add_to_newblock; lia.
+ - rewrite size_add_label; lia.
+ - rewrite size_add_basic; auto; lia.
+Qed.
+
+Inductive is_header: list label -> Mach.code -> Mach.code -> Prop :=
+ | header_empty : is_header nil nil nil
+ | header_not_label i c: (forall l, i <> Mlabel l) -> is_header nil (i::c) (i::c)
+ | header_is_label l h c c0: is_header h c c0 -> is_header (l::h) ((Mlabel l)::c) c0
+ .
+
+Inductive is_body: list basic_inst -> Mach.code -> Mach.code -> Prop :=
+ | body_empty : is_body nil nil nil
+ | body_not_bi i c: (forall bi, (trans_inst i) <> (MB_basic bi)) -> is_body nil (i::c) (i::c)
+ | body_is_bi i lbi c0 c1 bi: (trans_inst i) = MB_basic bi -> is_body lbi c0 c1 -> is_body (bi::lbi) (i::c0) c1
+ .
+
+Inductive is_exit: option control_flow_inst -> Mach.code -> Mach.code -> Prop :=
+ | exit_empty: is_exit None nil nil
+ | exit_not_cfi i c: (forall cfi, (trans_inst i) <> MB_cfi cfi) -> is_exit None (i::c) (i::c)
+ | exit_is_cfi i c cfi: (trans_inst i) = MB_cfi cfi -> is_exit (Some cfi) (i::c) c
+ .
+
+Lemma Mlabel_is_not_basic i:
+ forall bi, trans_inst i = MB_basic bi -> forall l, i <> Mlabel l.
+Proof.
+intros.
+unfold trans_inst in H.
+destruct i; congruence.
+Qed.
+
+Lemma Mlabel_is_not_cfi i:
+ forall cfi, trans_inst i = MB_cfi cfi -> forall l, i <> Mlabel l.
+Proof.
+intros.
+unfold trans_inst in H.
+destruct i; congruence.
+Qed.
+
+Lemma MBbasic_is_not_cfi i:
+ forall cfi, trans_inst i = MB_cfi cfi -> forall bi, trans_inst i <> MB_basic bi.
+Proof.
+intros.
+unfold trans_inst in H.
+unfold trans_inst.
+destruct i; congruence.
+Qed.
+
+
+Local Hint Resolve Mlabel_is_not_cfi: core.
+Local Hint Resolve MBbasic_is_not_cfi: core.
+
+Lemma add_to_new_block_is_label i:
+ header (add_to_new_bblock (trans_inst i)) <> nil -> exists l, i = Mlabel l.
+Proof.
+ intros.
+ unfold add_to_new_bblock in H.
+ destruct (trans_inst i) eqn : H1.
+ + exists lbl.
+ unfold trans_inst in H1.
+ destruct i; congruence.
+ + unfold add_basic in H; cbn in H; congruence.
+ + unfold cfi_bblock in H; cbn in H; congruence.
+Qed.
+
+Local Hint Resolve Mlabel_is_not_basic: core.
+
+Lemma trans_code_decompose c: forall b bl,
+ is_trans_code c (b::bl) ->
+ exists c0 c1 c2, is_header (header b) c c0 /\ is_body (body b) c0 c1 /\ is_exit (exit b) c1 c2 /\ is_trans_code c2 bl.
+Proof.
+ induction c as [|i c].
+ { (* nil => absurd *) intros b bl H; inversion H. }
+ intros b bl H; remember (trans_inst i) as ti.
+ destruct ti as [lbl|bi|cfi];
+ inversion H as [|d0 d1 d2 H0 H1| |]; subst;
+ try (rewrite <- Heqti in * |- *); cbn in * |- *;
+ try congruence.
+ + (* label at end block *)
+ inversion H1; subst. inversion H0; subst.
+ assert (X:i=Mlabel lbl). { destruct i; cbn in Heqti; congruence. }
+ subst. repeat econstructor; eauto.
+ + (* label at mid block *)
+ exploit IHc; eauto.
+ intros (c0 & c1 & c2 & H1 & H2 & H3 & H4).
+ repeat econstructor; eauto.
+ + (* basic at end block *)
+ inversion H1; subst.
+ lapply (Mlabel_is_not_basic i bi); auto.
+ intro H2.
+ - inversion H0; subst.
+ assert (X:(trans_inst i) = MB_basic bi ). { repeat econstructor; congruence. }
+ repeat econstructor; congruence.
+ - exists (i::c), c, c.
+ repeat econstructor; eauto; inversion H0; subst; repeat econstructor; cbn; try congruence.
+ * exploit (add_to_new_block_is_label i0); eauto.
+ intros (l & H8); subst; cbn; congruence.
+ * exploit H3; eauto.
+ * exploit (add_to_new_block_is_label i0); eauto.
+ intros (l & H8); subst; cbn; congruence.
+ + (* basic at mid block *)
+ inversion H1; subst.
+ exploit IHc; eauto.
+ intros (c0 & c1 & c2 & H3 & H4 & H5 & H6).
+ exists (i::c0), c1, c2.
+ repeat econstructor; eauto.
+ rewrite H2 in H3.
+ inversion H3; econstructor; eauto.
+ + (* cfi at end block *)
+ inversion H1; subst;
+ repeat econstructor; eauto.
+Qed.
+
+
+Lemma step_simu_header st f sp rs m s c h c' t:
+ is_header h c c' ->
+ starN (Mach.step (inv_trans_rao rao)) (Genv.globalenv prog) (length h) (Mach.State st f sp c rs m) t s ->
+ s = Mach.State st f sp c' rs m /\ t = E0.
+Proof.
+ induction 1; cbn; intros hs; try (inversion hs; tauto).
+ inversion hs as [|n1 s1 t1 t2 s2 t3 s3 H1]. inversion H1. subst. auto.
+Qed.
+
+
+
+Lemma step_simu_basic_step (i: Mach.instruction) (bi: basic_inst) (c: Mach.code) s f sp rs m (t:trace) (s':Mach.state):
+ trans_inst i = MB_basic bi ->
+ Mach.step (inv_trans_rao rao) ge (Mach.State s f sp (i::c) rs m) t s' ->
+ exists rs' m', s'=Mach.State s f sp c rs' m' /\ t=E0 /\ basic_step tge (trans_stack s) f sp rs m bi rs' m'.
+Proof.
+ destruct i; cbn in * |-;
+ (discriminate
+ || (intro H; inversion_clear H; intro X; inversion_clear X; eapply ex_intro; eapply ex_intro; intuition eauto)).
+ - eapply exec_MBgetparam; eauto. exploit (functions_translated); eauto. intro.
+ destruct H3 as (tf & A & B). subst. eapply A.
+ all: cbn; rewrite <- parent_sp_preserved; auto.
+ - eapply exec_MBop; eauto. rewrite <- H. destruct o; cbn; auto. destruct (rs ## l); cbn; auto.
+ unfold Genv.symbol_address; rewrite symbols_preserved; auto.
+ - eapply exec_MBload; eauto; rewrite <- H; destruct a; cbn; auto; destruct (rs ## l); cbn; auto;
+ unfold Genv.symbol_address; rewrite symbols_preserved; auto.
+ - eapply exec_MBload_notrap1; eauto; rewrite <- H; destruct a; cbn; auto; destruct (rs ## l); cbn; auto;
+ unfold Genv.symbol_address; rewrite symbols_preserved; auto.
+ - eapply exec_MBload_notrap2; eauto; rewrite <- H; destruct a; cbn; auto; destruct (rs ## l); cbn; auto;
+ unfold Genv.symbol_address; rewrite symbols_preserved; auto.
+ - eapply exec_MBstore; eauto; rewrite <- H; destruct a; cbn; auto; destruct (rs ## l); cbn; auto;
+ unfold Genv.symbol_address; rewrite symbols_preserved; auto.
+Qed.
+
+
+Lemma star_step_simu_body_step s f sp c bdy c':
+ is_body bdy c c' -> forall rs m t s',
+ starN (Mach.step (inv_trans_rao rao)) ge (length bdy) (Mach.State s f sp c rs m) t s' ->
+ exists rs' m', s'=Mach.State s f sp c' rs' m' /\ t=E0 /\ body_step tge (trans_stack s) f sp bdy rs m rs' m'.
+Proof.
+ induction 1; cbn.
+ + intros. inversion H. exists rs. exists m. auto.
+ + intros. inversion H0. exists rs. exists m. auto.
+ + intros. inversion H1; subst.
+ exploit (step_simu_basic_step ); eauto.
+ destruct 1 as [ rs1 [ m1 Hs]].
+ destruct Hs as [Hs1 [Hs2 Hs3]].
+ destruct (IHis_body rs1 m1 t2 s') as [rs2 Hb]. rewrite <- Hs1; eauto.
+ destruct Hb as [m2 [Hb1 [Hb2 Hb3]]].
+ exists rs2, m2.
+ rewrite Hs2, Hb2; eauto.
+ Qed.
+
+Local Hint Resolve exec_MBcall exec_MBtailcall exec_MBbuiltin exec_MBgoto exec_MBcond_true exec_MBcond_false exec_MBjumptable exec_MBreturn exec_Some_exit exec_None_exit: core.
+Local Hint Resolve eval_builtin_args_preserved external_call_symbols_preserved find_funct_ptr_same: core.
+
+
+Lemma match_states_concat_trans_code st f sp c rs m h:
+ match_states (Mach.State st f sp c rs m) (State (trans_stack st) f sp (concat h (trans_code c)) rs m).
+Proof.
+ intros; constructor 1; cbn.
+ + intros (t0 & s1' & H0) t s'.
+ remember (trans_code _) as bl.
+ destruct bl as [|bh bl].
+ { rewrite <- is_trans_code_inv in Heqbl; inversion Heqbl; inversion H0; congruence. }
+ clear H0.
+ cbn; constructor 1;
+ intros X; inversion X as [d1 d2 d3 d4 d5 d6 d7 rs' m' d10 d11 X1 X2| | | ]; subst; cbn in * |- *;
+ eapply exec_bblock; eauto; cbn;
+ inversion X2 as [cfi d1 d2 d3 H1|]; subst; eauto;
+ inversion H1; subst; eauto.
+ + intros H r; constructor 1; intro X; inversion X.
+Qed.
+
+Lemma step_simu_cfi_step (i: Mach.instruction) (cfi: control_flow_inst) (c: Mach.code) (blc:code) stk f sp rs m (t:trace) (s':Mach.state) b:
+ trans_inst i = MB_cfi cfi ->
+ is_trans_code c blc ->
+ Mach.step (inv_trans_rao rao) ge (Mach.State stk f sp (i::c) rs m) t s' ->
+ exists s2, cfi_step rao tge cfi (State (trans_stack stk) f sp (b::blc) rs m) t s2 /\ match_states s' s2.
+Proof.
+ destruct i; cbn in * |-;
+ (intro H; intro Htc;apply is_trans_code_inv in Htc;rewrite Htc;inversion_clear H;intro X; inversion_clear X).
+ * eapply ex_intro.
+ intuition auto.
+ eapply exec_MBcall;eauto.
+ rewrite <-H; exploit (find_function_ptr_same); eauto.
+ * eapply ex_intro.
+ intuition auto.
+ eapply exec_MBtailcall;eauto.
+ - rewrite <-H; exploit (find_function_ptr_same); eauto.
+ - cbn; rewrite <- parent_sp_preserved; auto.
+ - cbn; rewrite <- parent_ra_preserved; auto.
+ * eapply ex_intro.
+ intuition auto.
+ eapply exec_MBbuiltin ;eauto.
+ * exploit find_label_transcode_preserved; eauto.
+ intros (x & X1 & X2).
+ eapply ex_intro; constructor 1; [ idtac | eapply match_states_concat_trans_code ]; eauto.
+ * exploit find_label_transcode_preserved; eauto.
+ intros (x & X1 & X2).
+ eapply ex_intro; constructor 1; [ idtac | eapply match_states_concat_trans_code ]; eauto.
+ * eapply ex_intro; constructor 1; [ idtac | eapply match_states_trans_state ]; eauto.
+ eapply exec_MBcond_false; eauto.
+ * exploit find_label_transcode_preserved; eauto. intros (h & X1 & X2).
+ eapply ex_intro; constructor 1; [ idtac | eapply match_states_concat_trans_code ]; eauto.
+ * eapply ex_intro; constructor 1; [ idtac | eapply match_states_trans_state ]; eauto.
+ eapply exec_MBreturn; eauto.
+ rewrite parent_sp_preserved in H0; subst; auto.
+ rewrite parent_ra_preserved in H1; subst; auto.
+Qed.
+
+Lemma step_simu_exit_step stk f sp rs m t s1 e c c' b blc:
+ is_exit e c c' -> is_trans_code c' blc ->
+ starN (Mach.step (inv_trans_rao rao)) (Genv.globalenv prog) (length_opt e) (Mach.State stk f sp c rs m) t s1 ->
+ exists s2, exit_step rao tge e (State (trans_stack stk) f sp (b::blc) rs m) t s2 /\ match_states s1 s2.
+Proof.
+ destruct 1.
+ - (* None *)
+ intros H0 H1. inversion H1. exists (State (trans_stack stk) f sp blc rs m).
+ split; eauto.
+ apply is_trans_code_inv in H0.
+ rewrite H0.
+ apply match_states_trans_state.
+ - (* None *)
+ intros H0 H1. inversion H1. exists (State (trans_stack stk) f sp blc rs m).
+ split; eauto.
+ apply is_trans_code_inv in H0.
+ rewrite H0.
+ apply match_states_trans_state.
+ - (* Some *)
+ intros H0 H1.
+ inversion H1; subst.
+ exploit (step_simu_cfi_step); eauto.
+ intros [s2 [Hcfi1 Hcfi3]].
+ inversion H4. subst; cbn.
+ autorewrite with trace_rewrite.
+ exists s2.
+ split;eauto.
+Qed.
+
+Lemma simu_end_block:
+ forall s1 t s1',
+ starN (Mach.step (inv_trans_rao rao)) ge (Datatypes.S (dist_end_block s1)) s1 t s1' ->
+ exists s2', step rao tge (trans_state s1) t s2' /\ match_states s1' s2'.
+Proof.
+ destruct s1; cbn.
+ + (* State *)
+ remember (trans_code _) as tc.
+ rewrite <- is_trans_code_inv in Heqtc.
+ intros t s1 H.
+ destruct tc as [|b bl].
+ { (* nil => absurd *)
+ inversion Heqtc. subst.
+ unfold dist_end_block_code; cbn.
+ inversion_clear H;
+ inversion_clear H0.
+ }
+ assert (X: Datatypes.S (dist_end_block_code c) = (size b)).
+ {
+ unfold dist_end_block_code.
+ subst_is_trans_code Heqtc.
+ lapply (size_nonzero c b bl); auto.
+ lia.
+ }
+ rewrite X in H; unfold size in H.
+ (* decomposition of starN in 3 parts: header + body + exit *)
+ destruct (starN_split (Mach.semantics (inv_trans_rao rao) prog) _ _ _ _ H _ _ refl_equal) as (t3&t4&s1'&H0&H3&H4).
+ subst t; clear X H.
+ destruct (starN_split (Mach.semantics (inv_trans_rao rao) prog) _ _ _ _ H0 _ _ refl_equal) as (t1&t2&s1''&H&H1&H2).
+ subst t3; clear H0.
+ exploit trans_code_decompose; eauto. clear Heqtc.
+ intros (c0&c1&c2&Hc0&Hc1&Hc2&Heqtc).
+ (* header steps *)
+ exploit step_simu_header; eauto.
+ clear H; intros [X1 X2]; subst.
+ (* body steps *)
+ exploit (star_step_simu_body_step); eauto.
+ clear H1; intros (rs'&m'&H0&H1&H2). subst.
+ autorewrite with trace_rewrite.
+ (* exit step *)
+ exploit step_simu_exit_step; eauto.
+ clear H3; intros (s2' & H3 & H4).
+ eapply ex_intro; intuition eauto.
+ eapply exec_bblock; eauto.
+ + (* Callstate *)
+ intros t s1' H; inversion_clear H.
+ eapply ex_intro; constructor 1; eauto.
+ inversion H1; subst; clear H1.
+ inversion_clear H0; cbn.
+ - (* function_internal*)
+ replace (trans_code (Mach.fn_code f0)) with (fn_code (transf_function f0)); eauto.
+ eapply exec_function_internal; eauto.
+ rewrite <- parent_sp_preserved; eauto.
+ rewrite <- parent_ra_preserved; eauto.
+ - (* function_external *)
+ autorewrite with trace_rewrite.
+ eapply exec_function_external; eauto.
+ apply find_funct_ptr_same_external; auto.
+ rewrite <- parent_sp_preserved; eauto.
+ + (* Returnstate *)
+ intros t s1' H; inversion_clear H.
+ eapply ex_intro; constructor 1; eauto.
+ inversion H1; subst; clear H1.
+ inversion_clear H0; cbn.
+ eapply exec_return.
+Qed.
+
+
+Lemma cfi_dist_end_block i c:
+(exists cfi, trans_inst i = MB_cfi cfi) ->
+dist_end_block_code (i :: c) = 0.
+Proof.
+ unfold dist_end_block_code.
+ intro H. destruct H as [cfi H].
+ destruct i;cbn in H;try(congruence); (
+ remember (trans_code _) as bl;
+ rewrite <- is_trans_code_inv in Heqbl;
+ inversion Heqbl; subst; cbn in * |- *; try (congruence)).
+Qed.
+
+Theorem transf_program_correct:
+ forward_simulation (Mach.semantics (inv_trans_rao rao) prog) (Machblock.semantics rao tprog).
+Proof.
+ apply forward_simulation_block_trans with (dist_end_block := dist_end_block) (trans_state := trans_state).
+(* simu_mid_block *)
+ - intros s1 t s1' H1 H2.
+ destruct H1; cbn in * |- *; lia || (intuition auto);
+ destruct H2; eapply cfi_dist_end_block; cbn; eauto.
+(* public_preserved *)
+ - apply senv_preserved.
+(* match_initial_states *)
+ - intros. cbn.
+ eapply ex_intro; constructor 1.
+ eapply match_states_trans_state.
+ destruct H. split.
+ apply init_mem_preserved; auto.
+ rewrite prog_main_preserved. rewrite <- H0. apply symbols_preserved.
+(* match_final_states *)
+ - intros. cbn. destruct H. split with (r := r); auto.
+(* final_states_end_block *)
+ - intros. cbn in H0.
+ inversion H0.
+ inversion H; cbn; auto.
+ all: try (subst; discriminate).
+ apply cfi_dist_end_block; exists MBreturn; eauto.
+(* simu_end_block *)
+ - apply simu_end_block.
+Qed.
+
+End PRESERVATION.
+
+(** Auxiliary lemmas used to prove existence of a Mach return adress from a Machblock return address. *)
+
+
+
+Lemma is_trans_code_monotonic i c b l:
+ is_trans_code c (b::l) ->
+ exists l' b', is_trans_code (i::c) (l' ++ (b'::l)).
+Proof.
+ intro H; destruct c as [|i' c]. { inversion H. }
+ remember (trans_inst i) as ti.
+ destruct ti as [lbl|bi|cfi].
+ - (*i=lbl *) replace (i ) with (Mlabel lbl). 2: ( destruct i; cbn in * |- *; try congruence ).
+ exists nil; cbn; eexists. eapply Tr_add_label; eauto.
+ - (*i=basic*)
+ destruct i'.
+ 10: { exists (add_to_new_bblock (MB_basic bi)::nil). exists b.
+ replace ((add_to_new_bblock (MB_basic bi) :: nil) ++ (b::l)) with ((add_to_new_bblock (MB_basic bi) :: (b::l)));eauto.
+ rewrite Heqti.
+ eapply Tr_end_block; eauto.
+ rewrite <-Heqti.
+ eapply End_basic. inversion H; try(cbn; congruence).
+ cbn in H5; congruence. }
+ all: try(exists nil; cbn; eexists; eapply Tr_add_basic; eauto; inversion H; try(eauto || congruence)).
+ - (*i=cfi*)
+ destruct i; try(cbn in Heqti; congruence).
+ all: exists (add_to_new_bblock (MB_cfi cfi)::nil); exists b;
+ replace ((add_to_new_bblock (MB_cfi cfi) :: nil) ++ (b::l)) with ((add_to_new_bblock (MB_cfi cfi) :: (b::l)));eauto;
+ rewrite Heqti;
+ eapply Tr_end_block; eauto;
+ rewrite <-Heqti;
+ eapply End_cfi; congruence.
+Qed.
+
+Lemma trans_code_monotonic i c b l:
+ (b::l) = trans_code c ->
+ exists l' b', trans_code (i::c) = (l' ++ (b'::l)).
+Proof.
+ intro H; rewrite <- is_trans_code_inv in H.
+ destruct (is_trans_code_monotonic i c b l H) as (l' & b' & H0).
+ subst_is_trans_code H0.
+ eauto.
+Qed.
+
+Lemma Mach_Machblock_tail sg ros c c1 c2: c1=(Mcall sg ros :: c) -> is_tail c1 c2 ->
+ exists b, is_tail (b :: trans_code c) (trans_code c2).
+Proof.
+ intros H; induction 1.
+ - intros; subst.
+ remember (trans_code (Mcall _ _::c)) as tc2.
+ rewrite <- is_trans_code_inv in Heqtc2.
+ inversion Heqtc2; cbn in * |- *; subst; try congruence.
+ subst_is_trans_code H1.
+ eapply ex_intro; eauto with coqlib.
+ - intros; exploit IHis_tail; eauto. clear IHis_tail.
+ intros (b & Hb). inversion Hb; clear Hb.
+ * exploit (trans_code_monotonic i c2); eauto.
+ intros (l' & b' & Hl'); rewrite Hl'.
+ exists b'; cbn; eauto with coqlib.
+ * exploit (trans_code_monotonic i c2); eauto.
+ intros (l' & b' & Hl'); rewrite Hl'.
+ cbn; eapply ex_intro.
+ eapply is_tail_trans; eauto with coqlib.
+Qed.
+
+Section Mach_Return_Address.
+
+Variable return_address_offset: function -> code -> ptrofs -> Prop.
+
+Hypothesis ra_exists: forall (b: bblock) (f: function) (c : list bblock),
+ is_tail (b :: c) (fn_code f) -> exists ra : ptrofs, return_address_offset f c ra.
+
+Definition Mach_return_address_offset (f: Mach.function) (c: Mach.code) (ofs: ptrofs) : Prop :=
+ return_address_offset (transf_function f) (trans_code c) ofs.
+
+Lemma Mach_return_address_exists:
+ forall f sg ros c, is_tail (Mcall sg ros :: c) f.(Mach.fn_code) ->
+ exists ra, Mach_return_address_offset f c ra.
+Proof.
+ intros.
+ exploit Mach_Machblock_tail; eauto.
+ destruct 1.
+ eapply ra_exists; eauto.
+Qed.
+
+End Mach_Return_Address.
diff --git a/scripts/cplex/ilp_solver b/scripts/cplex/ilp_solver
new file mode 100755
index 00000000..1e70a652
--- /dev/null
+++ b/scripts/cplex/ilp_solver
@@ -0,0 +1,10 @@
+#!/bin/sh
+tmp=$2.tmp
+rm -f $tmp $2
+if cplex -c "read $1" "optimize" "write $tmp sol";
+then
+ grep '<variable name="' $tmp | sed -e 's@ *<variable name="@@' -e 's@" index=.* value="@ @' -e 's@"/>@@' > $2
+ rm -f $tmp
+else
+ exit $?
+fi
diff --git a/scripts/gurobi/ilp_solver b/scripts/gurobi/ilp_solver
new file mode 100755
index 00000000..7a67a942
--- /dev/null
+++ b/scripts/gurobi/ilp_solver
@@ -0,0 +1,3 @@
+#!/bin/sh
+# For using -fpostpass-ilp with Gurobi
+exec gurobi_cl DegenMoves=4 Heuristics=0 MIPFocus=2 PrePasses=2 ResultFile=$2 $1
diff --git a/scripts/scip/ilp_solver b/scripts/scip/ilp_solver
new file mode 100755
index 00000000..33b5940b
--- /dev/null
+++ b/scripts/scip/ilp_solver
@@ -0,0 +1,11 @@
+#!/bin/sh
+# does not work
+tmp=$2.tmp
+rm -f $tmp $2
+if scip -c "read $1" -c "optimize" -c "write solution $tmp" -c "quit" ;
+then
+ grep '(obj:' $tmp | sed -e 's@[[:space:]][[:space:]]*@ @' -e 's@[[:space:]]*(obj:.*)@@' > $2
+ rm -f $tmp
+else
+ exit $?
+fi
diff --git a/test/Makefile b/test/Makefile
index ccf8fcd7..b0010ea0 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,6 +1,15 @@
include ../Makefile.config
-DIRS=c compression raytracer spass regression abi
+#DIRS=c compression raytracer spass regression abi
+
+# Kalray note - removing compression, raytracer and spass that cannot be executed by the simulator in reasonable time
+# TODO: abi for Kalray ?
+ifeq ($(ARCH),kvx)
+ DIRS=c regression
+else
+ DIRS=c compression raytracer spass regression abi
+endif
+
ifeq ($(CLIGHTGEN),true)
DIRS+=export
endif
diff --git a/test/c/Makefile b/test/c/Makefile
index 4b521bb5..a728d182 100644
--- a/test/c/Makefile
+++ b/test/c/Makefile
@@ -1,18 +1,36 @@
include ../../Makefile.config
CCOMP=../../ccomp
+# TODO - temporary
+# CCOMPOPTS:=$(CCOMPOPTS) -fall-loads-nontrap -fduplicate 2 -fprepass
CCOMPFLAGS=$(CCOMPOPTS) -stdlib ../../runtime -dc -dclight -dasm
-CFLAGS=-O1 -Wall
+CFLAGS+=-O2 -Wall
+EXECUTE:=timeout --signal=SIGTERM 20s $(EXECUTE)
LIBS=$(LIBMATH)
-TIME=ocaml unix.cma ../../tools/xtime.ml -o /dev/null -mintime 2.0 -minruns 4
+TIME=time >/dev/null
+# FIXME - maybe this is better? From v3.6
+# TIME=ocaml unix.cma ../../tools/xtime.ml -o /dev/null -mintime 2.0 -minruns 4
-PROGS=fib integr qsort fft fftsp fftw sha1 sha3 aes almabench \
- lists binarytrees fannkuch knucleotide mandelbrot nbody \
+PROGS?=fib integr qsort fft fftsp fftw sha1 sha3 aes almabench \
+ lists binarytrees fannkuch mandelbrot nbody \
nsieve nsievebits spectral vmach \
- bisect chomp perlin siphash24
+ chomp perlin siphash24
+
+##
+# Kalray NOTE :
+# * removed knucleotide from PROGS, it is hard to edit the input
+# to modify its size without resulting in a seg fault, and the base input
+# takes a too long time to complete in the simulator.
+# * also removed bisect, who is exhibiting different float values on the Kalray
+# architecture than using x86 GCC (for both CompCert and GCC ports) (tested with n=10)
+##
+ifeq ($(ARCH),kvx)
+ PROGS:=$(filter-out knucleotide,$(PROGS))
+ PROGS:=$(filter-out bisect,$(PROGS))
+endif
all: $(PROGS:%=%.compcert)
@@ -29,28 +47,22 @@ all_gcc: $(PROGS:%=%.gcc)
%.gcc: %.c
$(CC) $(CFLAGS) -o $*.gcc $*.c $(LIBS)
-test:
+test: all
@for i in $(PROGS); do \
- if $(SIMU) ./$$i.compcert | cmp -s - Results/$$i; \
- then echo "$$i: passed"; \
- else echo "$$i: FAILED"; exit 2; \
- fi; \
+ SIMU='$(EXECUTE)' ./Runtest $$i ./$$i.compcert;\
done
-test_gcc:
+test_gcc: all_gcc
@for i in $(PROGS); do \
- if ./$$i.gcc | cmp -s - Results/$$i; \
- then echo "$$i: passed"; \
- else echo "$$i: FAILED"; \
- fi; \
+ SIMU='$(EXECUTE)' ./Runtest $$i ./$$i.gcc;\
done
-bench_gcc:
+bench_gcc: all_gcc
@for i in $(PROGS); do \
$(TIME) -name $$i -- ./$$i.gcc; \
done
-bench:
+bench: all
@for i in $(PROGS); do \
$(TIME) -name $$i -- ./$$i.compcert; \
done
@@ -58,3 +70,4 @@ bench:
clean:
rm -f *.compcert *.gcc
rm -f *.compcert.c *.light.c *.parsed.c *.s *.o *.sdump *~
+ rm -f *.out
diff --git a/test/c/Results/binarytrees-kvx b/test/c/Results/binarytrees-kvx
new file mode 100644
index 00000000..72654db9
--- /dev/null
+++ b/test/c/Results/binarytrees-kvx
@@ -0,0 +1,4 @@
+stretch tree of depth 7 check: -1
+128 trees of depth 4 check: -128
+32 trees of depth 6 check: -32
+long lived tree of depth 6 check: -1
diff --git a/test/c/Results/chomp-kvx b/test/c/Results/chomp-kvx
new file mode 100644
index 00000000..7898d32f
--- /dev/null
+++ b/test/c/Results/chomp-kvx
@@ -0,0 +1,9 @@
+player 0 plays at (1,1)
+player 1 plays at (3,0)
+player 0 plays at (0,3)
+player 1 plays at (2,0)
+player 0 plays at (0,2)
+player 1 plays at (1,0)
+player 0 plays at (0,1)
+player 1 plays at (0,0)
+player 1 loses
diff --git a/test/c/Results/fannkuch-kvx b/test/c/Results/fannkuch-kvx
new file mode 100644
index 00000000..09ecc715
--- /dev/null
+++ b/test/c/Results/fannkuch-kvx
@@ -0,0 +1,31 @@
+123456
+213456
+231456
+321456
+312456
+132456
+234156
+324156
+342156
+432156
+423156
+243156
+341256
+431256
+413256
+143256
+134256
+314256
+412356
+142356
+124356
+214356
+241356
+421356
+234516
+324516
+342516
+432516
+423516
+243516
+Pfannkuchen(6) = 10
diff --git a/test/c/Results/fft-kvx b/test/c/Results/fft-kvx
new file mode 100644
index 00000000..0fc1c969
--- /dev/null
+++ b/test/c/Results/fft-kvx
@@ -0,0 +1 @@
+1024 points, result OK
diff --git a/test/c/Results/fftsp-kvx b/test/c/Results/fftsp-kvx
new file mode 100644
index 00000000..2b5711a6
--- /dev/null
+++ b/test/c/Results/fftsp-kvx
@@ -0,0 +1 @@
+8 points, result OK
diff --git a/test/c/Results/fftw-kvx b/test/c/Results/fftw-kvx
new file mode 100644
index 00000000..a1b6130c
--- /dev/null
+++ b/test/c/Results/fftw-kvx
@@ -0,0 +1,16 @@
+o[0] = 2.918193e+01
+o[1] = -3.230611e+01
+o[2] = 1.271687e+01
+o[3] = -1.099040e+01
+o[4] = 5.728673e+00
+o[5] = -4.918940e+00
+o[6] = 1.880764e+00
+o[7] = -1.292782e+00
+o[8] = 1.104073e+02
+o[9] = -5.867858e+01
+o[10] = 2.768382e+01
+o[11] = -2.073843e+01
+o[12] = 1.229410e+01
+o[13] = -9.195029e+00
+o[14] = 4.307537e+00
+o[15] = -2.080713e+00
diff --git a/test/c/Results/fib-kvx b/test/c/Results/fib-kvx
new file mode 100644
index 00000000..0e0fa4d1
--- /dev/null
+++ b/test/c/Results/fib-kvx
@@ -0,0 +1 @@
+fib(15) = 987
diff --git a/test/c/Results/integr-kvx b/test/c/Results/integr-kvx
new file mode 100644
index 00000000..c61fdcc2
--- /dev/null
+++ b/test/c/Results/integr-kvx
@@ -0,0 +1 @@
+integr(square, 0.0, 1.0, 100000) = 0.333328
diff --git a/test/c/Results/knucleotide-kvx b/test/c/Results/knucleotide-kvx
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/c/Results/knucleotide-kvx
diff --git a/test/c/Results/lists-kvx b/test/c/Results/lists-kvx
new file mode 100644
index 00000000..2c94e483
--- /dev/null
+++ b/test/c/Results/lists-kvx
@@ -0,0 +1,2 @@
+OK
+OK
diff --git a/test/c/Results/mandelbrot-kvx b/test/c/Results/mandelbrot-kvx
new file mode 100644
index 00000000..55c5683a
--- /dev/null
+++ b/test/c/Results/mandelbrot-kvx
Binary files differ
diff --git a/test/c/Results/nbody-kvx b/test/c/Results/nbody-kvx
new file mode 100644
index 00000000..99ad4fd1
--- /dev/null
+++ b/test/c/Results/nbody-kvx
@@ -0,0 +1,2 @@
+-0.169075164
+-0.169050762
diff --git a/test/c/Results/nsieve-kvx b/test/c/Results/nsieve-kvx
new file mode 100644
index 00000000..95fea812
--- /dev/null
+++ b/test/c/Results/nsieve-kvx
@@ -0,0 +1,3 @@
+Primes up to 12800 1526
+Primes up to 6400 834
+Primes up to 3200 452
diff --git a/test/c/Results/nsievebits-kvx b/test/c/Results/nsievebits-kvx
new file mode 100644
index 00000000..2131804c
--- /dev/null
+++ b/test/c/Results/nsievebits-kvx
@@ -0,0 +1,3 @@
+Primes up to 40000 4203
+Primes up to 20000 2262
+Primes up to 10000 1229
diff --git a/test/c/Results/perlin-kvx b/test/c/Results/perlin-kvx
new file mode 100644
index 00000000..8438b53c
--- /dev/null
+++ b/test/c/Results/perlin-kvx
@@ -0,0 +1 @@
+6.0000e+00
diff --git a/test/c/Results/qsort-kvx b/test/c/Results/qsort-kvx
new file mode 100644
index 00000000..d86bac9d
--- /dev/null
+++ b/test/c/Results/qsort-kvx
@@ -0,0 +1 @@
+OK
diff --git a/test/c/Results/sha1-kvx b/test/c/Results/sha1-kvx
new file mode 100644
index 00000000..730d5406
--- /dev/null
+++ b/test/c/Results/sha1-kvx
@@ -0,0 +1,2 @@
+Test `abc': passed
+Test `abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq': passed
diff --git a/test/c/Results/spectral-kvx b/test/c/Results/spectral-kvx
new file mode 100644
index 00000000..b06cd560
--- /dev/null
+++ b/test/c/Results/spectral-kvx
@@ -0,0 +1 @@
+1.272359925
diff --git a/test/c/Results/vmach-kvx b/test/c/Results/vmach-kvx
new file mode 100644
index 00000000..a95237a6
--- /dev/null
+++ b/test/c/Results/vmach-kvx
@@ -0,0 +1,2 @@
+fib(15) = 987
+tak(12, 9, 6) = 9
diff --git a/test/c/Runtest b/test/c/Runtest
new file mode 100755
index 00000000..f693219a
--- /dev/null
+++ b/test/c/Runtest
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+# The name of the test
+name="$1"
+shift
+
+# The temp file for output
+out="test$$.log"
+rm -f $out
+trap "rm -f $out" 0 INT QUIT
+
+# Is the test expected to fail?
+expect_fail=false
+
+# The architecture and the bitsize
+arch=`sed -n -e 's/^ARCH=//p' ../../Makefile.config`
+bits=`sed -n -e 's/^BITSIZE=//p' ../../Makefile.config`
+
+# The reference output
+if test -f "Results/$name-$arch-$bits"; then
+ ref="Results/$name-$arch-$bits"
+elif test -f "Results/$name-$arch"; then
+ ref="Results/$name-$arch"
+elif test -f "Results/$name-$bits"; then
+ ref="Results/$name-$bits"
+elif test -f "Results/$name"; then
+ ref="Results/$name"
+else
+ ref=""
+fi
+
+# Special conditions
+
+if test -f "$name.cond"; then
+ RUN=0 SKIP=1 EXPECT_FAIL=2 sh "$name.cond"
+ case "$?" in
+ 1) echo "$name: skipped"; exit 0;;
+ 2) expect_fail=true;;
+ esac
+fi
+
+# Administer the test
+if $SIMU $* > $out
+then
+ if $expect_fail; then
+ echo "$name: ERROR (should have failed but did not)"
+ exit 2
+ elif test -n "$ref"; then
+ if cmp -s "$out" "$ref"; then
+ echo "$name: passed"
+ exit 0
+ else
+ echo "$name: WRONG OUTPUT (diff follows)"
+ diff -u "$ref" "$out"
+ exit 2
+ fi
+ else
+ echo "$name: passed"
+ exit 0
+ fi
+else
+ retcode=$?
+ if $expect_fail; then
+ echo "$name: passed (failed as expected)"
+ exit 0
+ else
+ echo "$name: EXECUTION FAILED (status $retcode)"
+ exit 2
+ fi
+fi
+
diff --git a/test/c/aes.c b/test/c/aes.c
index 16f02e47..c959a611 100644
--- a/test/c/aes.c
+++ b/test/c/aes.c
@@ -1441,6 +1441,10 @@ int main(int argc, char ** argv)
(u8 *)"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF",
(u8 *)"\x8E\xA2\xB7\xCA\x51\x67\x45\xBF\xEA\xFC\x49\x90\x4B\x49\x60\x89",
5, 6);
+#ifdef __KVX__
+ do_bench(2000);
+#else
do_bench(1000000);
+#endif
return 0;
}
diff --git a/test/c/almabench.c b/test/c/almabench.c
index 5487b062..823bc18f 100644
--- a/test/c/almabench.c
+++ b/test/c/almabench.c
@@ -42,10 +42,15 @@
#define R2D (180.0 / PI)
#define GAUSSK 0.01720209895
#define TEST_LOOPS 20
-#define TEST_LENGTH 36525
#define sineps 0.3977771559319137
#define coseps 0.9174820620691818
+#ifdef __KVX__
+#define TEST_LENGTH 12
+#else
+#define TEST_LENGTH 36525
+#endif
+
const double amas [8] = { 6023600.0, 408523.5, 328900.5, 3098710.0, 1047.355, 3498.5, 22869.0, 19314.0 };
const double a [8][3] =
diff --git a/test/c/binarytrees.c b/test/c/binarytrees.c
index b4b10232..fbcddea1 100644
--- a/test/c/binarytrees.c
+++ b/test/c/binarytrees.c
@@ -7,6 +7,7 @@
icc -O3 -ip -unroll -static binary-trees.c -lm
*/
+#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -24,6 +25,7 @@ treeNode* NewTreeNode(treeNode* left, treeNode* right, long item)
treeNode* new;
new = (treeNode*)malloc(sizeof(treeNode));
+ assert(new != NULL && "NewTreeNode: new malloc failed");
new->left = left;
new->right = right;
@@ -73,7 +75,11 @@ int main(int argc, char* argv[])
unsigned N, depth, minDepth, maxDepth, stretchDepth;
treeNode *stretchTree, *longLivedTree, *tempTree;
+#ifdef __KVX__
+ N = argc < 2 ? 6 : atol(argv[1]);
+#else
N = argc < 2 ? 12 : atol(argv[1]);
+#endif
minDepth = 4;
diff --git a/test/c/chomp.c b/test/c/chomp.c
index 728e7a01..71931b3d 100644
--- a/test/c/chomp.c
+++ b/test/c/chomp.c
@@ -338,8 +338,13 @@ int main(void)
struct _play *tree;
+#ifdef __KVX__
+ ncol = 4;
+ nrow = 4;
+#else
ncol = 7;
nrow = 7;
+#endif
tree = make_play(1); /* create entire tree structure, not just the */
player = 0; /* needed part for first move */
current = make_data(nrow,ncol); /* start play at full board */
diff --git a/test/c/fannkuch.c b/test/c/fannkuch.c
index 9cc7a693..a075c988 100644
--- a/test/c/fannkuch.c
+++ b/test/c/fannkuch.c
@@ -8,6 +8,7 @@
* $Id: fannkuch-gcc.code,v 1.33 2006/02/25 16:38:58 igouy-guest Exp $
*/
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
@@ -31,8 +32,11 @@ fannkuch( int n )
if( n < 1 ) return 0;
perm = calloc(n, sizeof(*perm ));
+ assert(perm != NULL && "fannkuch: perm malloc failed");
perm1 = calloc(n, sizeof(*perm1));
+ assert(perm != NULL && "fannkuch: perm1 malloc failed");
count = calloc(n, sizeof(*count));
+ assert(perm != NULL && "fannkuch: count malloc failed");
for( i=0 ; i<n ; ++i ) perm1[i] = i; /* initial (trivial) permu */
@@ -98,7 +102,11 @@ fannkuch( int n )
int
main( int argc, char* argv[] )
{
+#ifdef __KVX__
+ int n = (argc>1) ? atoi(argv[1]) : 6;
+#else
int n = (argc>1) ? atoi(argv[1]) : 10;
+#endif
printf("Pfannkuchen(%d) = %ld\n", n, fannkuch(n));
return 0;
diff --git a/test/c/fft.c b/test/c/fft.c
index 2bd55a18..3513319f 100644
--- a/test/c/fft.c
+++ b/test/c/fft.c
@@ -3,6 +3,7 @@
by: Dave Edelblute, edelblut@cod.nosc.mil, 05 Jan 1993
*/
+#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
@@ -151,13 +152,19 @@ int main(int argc, char ** argv)
double enp, t, y, z, zr, zi, zm, a;
double * xr, * xi, * pxr, * pxi;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 10;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 18;
+#endif
np = 1 << n;
enp = np;
npm = np / 2 - 1;
t = PI / enp;
xr = calloc(np, sizeof(double));
+ assert(xr != NULL && "xr calloc failed");
xi = calloc(np, sizeof(double));
+ assert(xi != NULL && "xi calloc failed");
pxr = xr;
pxi = xi;
for (nruns = 0; nruns < NRUNS; nruns++) {
diff --git a/test/c/fftsp.c b/test/c/fftsp.c
index 26b18b62..3215dca5 100644
--- a/test/c/fftsp.c
+++ b/test/c/fftsp.c
@@ -153,7 +153,11 @@ int main(int argc, char ** argv)
float enp, t, y, z, zr, zi, zm, a;
float * xr, * xi, * pxr, * pxi;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 3;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 12;
+#endif
np = 1 << n;
enp = np;
npm = np / 2 - 1;
diff --git a/test/c/fftw.c b/test/c/fftw.c
index 913091d9..2d50022a 100644
--- a/test/c/fftw.c
+++ b/test/c/fftw.c
@@ -74,7 +74,11 @@ const E KP1_847759065 = ((E) +1.847759065022573512256366378793576573644833252);
/* Test harness */
+#ifdef __KVX__
+#define NRUNS (10 * 10)
+#else
#define NRUNS (100 * 1000)
+#endif
int main()
{
diff --git a/test/c/fib.c b/test/c/fib.c
index e4c7d095..536038bd 100644
--- a/test/c/fib.c
+++ b/test/c/fib.c
@@ -12,7 +12,11 @@ int fib(int n)
int main(int argc, char ** argv)
{
int n, r;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 15;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 35;
+#endif
r = fib(n);
printf("fib(%d) = %d\n", n, r);
return 0;
diff --git a/test/c/integr.c b/test/c/integr.c
index 882325c3..edd87def 100644
--- a/test/c/integr.c
+++ b/test/c/integr.c
@@ -25,7 +25,11 @@ double test(int n)
int main(int argc, char ** argv)
{
int n; double r;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 100000;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 10000000;
+#endif
r = test(n);
printf("integr(square, 0.0, 1.0, %d) = %g\n", n, r);
return 0;
diff --git a/test/c/knucleotide.c b/test/c/knucleotide.c
index 3ac469be..1982834e 100644
--- a/test/c/knucleotide.c
+++ b/test/c/knucleotide.c
@@ -8,6 +8,7 @@
http://cvs.alioth.debian.org/cgi-bin/cvsweb.cgi/shootout/bench/Include/?cvsroot=shootout
*/
+#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
@@ -76,9 +77,11 @@ struct ht_node *ht_node_create(char *key) {
struct ht_ht *ht_create(int size) {
int i = 0;
struct ht_ht *ht = (struct ht_ht *)malloc(sizeof(struct ht_ht));
+ assert (ht != NULL && "ht_create: ht malloc failed");
while (ht_prime_list[i] < size) { i++; }
ht->size = ht_prime_list[i];
ht->tbl = (struct ht_node **)calloc(ht->size, sizeof(struct ht_node *));
+ assert (ht->tbl != NULL && "ht_create: ht->tbl calloc failed");
ht->iter_index = 0;
ht->iter_next = 0;
ht->items = 0;
@@ -250,6 +253,7 @@ write_frequencies (int fl, char *buffer, long buflen)
size++;
}
s = calloc (size, sizeof (sorter));
+ assert(s != NULL && "write_frequencies: s alloc failed");
i = 0;
for (nd = ht_first (ht); nd != NULL; nd = ht_next (ht))
{
@@ -293,6 +297,7 @@ main ()
FILE * f;
line = malloc (256);
+ assert (line != NULL && "line alloc failed");
if (!line)
return 2;
seqlen = 0;
@@ -308,6 +313,7 @@ main ()
buflen = 10240;
buffer = malloc (buflen + 1);
+ assert (buffer != NULL && "buffer alloc failed");
if (!buffer)
return 2;
x = buffer;
diff --git a/test/c/lists.c b/test/c/lists.c
index ced384c0..b995f6d0 100644
--- a/test/c/lists.c
+++ b/test/c/lists.c
@@ -1,5 +1,6 @@
/* List manipulations */
+#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
@@ -11,6 +12,7 @@ struct list * buildlist(int n)
struct list * r;
if (n < 0) return NULL;
r = malloc(sizeof(struct list));
+ assert(r != NULL && "buildlist: r malloc failed");
r->hd = n;
r->tl = buildlist(n - 1);
return r;
@@ -21,6 +23,7 @@ struct list * reverselist (struct list * l)
struct list * r, * r2;
for (r = NULL; l != NULL; l = l->tl) {
r2 = malloc(sizeof(struct list));
+ assert(r2 != NULL && "reverselist: r2 malloc failed");
r2->hd = l->hd;
r2->tl = r;
r = r2;
@@ -58,8 +61,13 @@ int main(int argc, char ** argv)
int n, niter, i;
struct list * l;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 500;
+ if (argc >= 3) niter = atoi(argv[1]); else niter = 100;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 1000;
if (argc >= 3) niter = atoi(argv[1]); else niter = 20000;
+#endif
l = buildlist(n);
if (checklist(n, reverselist(l))) {
printf("OK\n");
diff --git a/test/c/mandelbrot.c b/test/c/mandelbrot.c
index 032e7d75..d862b1a3 100644
--- a/test/c/mandelbrot.c
+++ b/test/c/mandelbrot.c
@@ -17,12 +17,20 @@ int main (int argc, char **argv)
{
int w, h, bit_num = 0;
char byte_acc = 0;
+#ifdef __KVX__
+ int i, iter = 30;
+#else
int i, iter = 50;
+#endif
double x, y, limit = 2.0;
double Zr, Zi, Cr, Ci, Tr, Ti;
if (argc < 2) {
+#ifdef __KVX__
+ w = h = 40;
+#else
w = h = 1000;
+#endif
} else {
w = h = atoi(argv[1]);
}
@@ -52,6 +60,9 @@ int main (int argc, char **argv)
if(bit_num == 8)
{
putc(byte_acc,stdout);
+#ifdef __KVX__ // stdout isn't flushed enough when --syscall=libstd_scalls.so is passed to the simulator k1-cluster
+ fflush(stdout);
+#endif
byte_acc = 0;
bit_num = 0;
}
@@ -59,6 +70,9 @@ int main (int argc, char **argv)
{
byte_acc <<= (8-w%8);
putc(byte_acc,stdout);
+#ifdef __KVX__ // stdout isn't flushed enough when --syscall=libstd_scalls.so is passed to the simulator k1-cluster
+ fflush(stdout);
+#endif
byte_acc = 0;
bit_num = 0;
}
diff --git a/test/c/nbody.c b/test/c/nbody.c
index 530c41fa..01b36d5a 100644
--- a/test/c/nbody.c
+++ b/test/c/nbody.c
@@ -140,7 +140,11 @@ void setup_bodies(void)
int main(int argc, char ** argv)
{
+#ifdef __KVX__
+ int n = argc < 2 ? 100 : atoi(argv[1]);
+#else
int n = argc < 2 ? 1000000 : atoi(argv[1]);
+#endif
int i;
setup_bodies();
diff --git a/test/c/nsieve.c b/test/c/nsieve.c
index 819d47f1..83e1e1f0 100644
--- a/test/c/nsieve.c
+++ b/test/c/nsieve.c
@@ -29,10 +29,18 @@ static unsigned int nsieve(int m) {
#define NITER 2
int main(int argc, char * argv[]) {
+#ifdef __KVX__
+ int m = argc < 2 ? 6 : atoi(argv[1]);
+#else
int m = argc < 2 ? 9 : atoi(argv[1]);
+#endif
int i, j;
for (i = 0; i < 3; i++) {
+#ifdef __KVX__
+ int n = 200 << (m-i);
+#else
int n = 10000 << (m-i);
+#endif
unsigned count;
for (j = 0; j < NITER; j++) { count = nsieve(n); }
printf("Primes up to %8d %8u\n", n, count);
diff --git a/test/c/nsievebits.c b/test/c/nsievebits.c
index 743a5ffd..a723d6d8 100644
--- a/test/c/nsievebits.c
+++ b/test/c/nsievebits.c
@@ -30,7 +30,11 @@ nsieve(unsigned int m)
return (count);
}
+#ifdef __KVX__
+#define NITER 1
+#else
#define NITER 2
+#endif
static void
test(unsigned int n)
@@ -48,7 +52,11 @@ main(int ac, char **av)
{
unsigned int n;
+#ifdef __KVX__
+ n = ac < 2 ? 2 : atoi(av[1]);
+#else
n = ac < 2 ? 9 : atoi(av[1]);
+#endif
test(n);
if (n >= 1)
test(n - 1);
diff --git a/test/c/perlin.c b/test/c/perlin.c
index e7bbd22d..5fa83a81 100644
--- a/test/c/perlin.c
+++ b/test/c/perlin.c
@@ -63,13 +63,22 @@ static void init(void) {
p[256+i] = p[i] = permutation[i];
}
+#ifdef __KVX__
+#define INCREMENT 0.5
+#define MIN -3.0
+#define MAX 3.0
+#else
+#define INCREMENT 0.1
+#define MIN -5.0
+#define MAX 5.0
+#endif
int main(int argc, char ** argv) {
init();
double x, y, z, sum = 0.0;
- for (x = -5.0; x < 5.0; x += 0.1)
- for (y = -5.0; y < 5.0; y += 0.1)
- for (z = -5.0; z < 5.0; z += 0.1)
+ for (x = MIN; x < MAX; x += INCREMENT)
+ for (y = MIN; y < MAX; y += INCREMENT)
+ for (z = MIN; z < MAX; z += INCREMENT)
sum += noise(x, y, z);
printf("%.4e\n", sum);
diff --git a/test/c/qsort.c b/test/c/qsort.c
index 66eef68d..298e131f 100644
--- a/test/c/qsort.c
+++ b/test/c/qsort.c
@@ -34,7 +34,11 @@ int main(int argc, char ** argv)
int n, i, j;
int * a, * b;
+#ifdef __KVX__
+ if (argc >= 2) n = atoi(argv[1]); else n = 500;
+#else
if (argc >= 2) n = atoi(argv[1]); else n = 100000;
+#endif
a = malloc(n * sizeof(int));
b = malloc(n * sizeof(int));
for (j = 0; j < NITER; j++) {
diff --git a/test/c/sha1.c b/test/c/sha1.c
index 0a6ac8fe..ce827c4a 100644
--- a/test/c/sha1.c
+++ b/test/c/sha1.c
@@ -231,6 +231,10 @@ int main(int argc, char ** argv)
}
do_test(test_input_1, test_output_1);
do_test(test_input_2, test_output_2);
+#ifdef __KVX__
+ do_bench(500);
+#else
do_bench(200000);
+#endif
return 0;
}
diff --git a/test/c/sha3.c b/test/c/sha3.c
index a0905817..796162a5 100644
--- a/test/c/sha3.c
+++ b/test/c/sha3.c
@@ -190,8 +190,13 @@ test_triplet_t testvec[4] = {
}
};
+#ifdef __KVX__
+#define DATALEN 1000
+#define NITER 7
+#else
#define DATALEN 100000
#define NITER 25
+#endif
int main()
{
diff --git a/test/c/siphash24.c b/test/c/siphash24.c
index 4a42e013..b4b4ff34 100644
--- a/test/c/siphash24.c
+++ b/test/c/siphash24.c
@@ -235,13 +235,19 @@ int test_vectors()
u8 testdata[100] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 12, 34, 56, 78, 90 };
+#ifdef __KVX__
+#define NITER 1000
+#else
+#define NITER 1000000
+#endif
+
int speed_test(void)
{
u8 out[8], k[16];
int i;
for(i = 0; i < 16; ++i ) k[i] = i;
- for(i = 0; i < 1000000; i++) {
+ for(i = 0; i < NITER; i++) {
testdata[99] = (u8) i;
crypto_auth(out, testdata, 100, k);
}
diff --git a/test/c/spectral.c b/test/c/spectral.c
index f7dc90ee..2d7604b2 100644
--- a/test/c/spectral.c
+++ b/test/c/spectral.c
@@ -43,7 +43,11 @@ void eval_AtA_times_u(int N, const double u[], double AtAu[])
int main(int argc, char *argv[])
{
int i;
+#ifdef __KVX__
+ int N = ((argc == 2) ? atoi(argv[1]) : 11);
+#else
int N = ((argc == 2) ? atoi(argv[1]) : 1000);
+#endif
double * u, * v, vBv, vv;
u = malloc(N * sizeof(double));
v = malloc(N * sizeof(double));
diff --git a/test/c/vmach.c b/test/c/vmach.c
index 815cb710..56138104 100644
--- a/test/c/vmach.c
+++ b/test/c/vmach.c
@@ -159,8 +159,14 @@ long wordcode_interp(unsigned int* code)
#define I(a,b,c,d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24))
+#ifdef __KVX__
+#define FIBSIZE 15
+#else
+#define FIBSIZE 30
+#endif
+
unsigned int wordcode_fib[] = {
-/* 0 */ I(WCONST, 30, 0, 0),
+/* 0 */ I(WCONST, FIBSIZE, 0, 0),
/* 1 */ I(WCALL1_pop1, 0, 3-1-1, 0),
/* 2 */ I(WSTOP, 0, 1, 0),
/* 3 */ I(WCONST, 2, 0, 0),
@@ -175,10 +181,21 @@ unsigned int wordcode_fib[] = {
/* 12 */ I(WCONST, 1, 0, 0),
/* 13 */ I(WRETURN, 0, 2, 0)
};
+
+#ifdef __KVX__
+#define TAKSIZE1 6
+#define TAKSIZE2 9
+#define TAKSIZE3 12
+#else
+#define TAKSIZE1 6
+#define TAKSIZE2 12
+#define TAKSIZE3 18
+#endif
+
unsigned int wordcode_tak[] = {
-/* 0 */ I(WCONST, 6, 0, 0),
-/* 1 */ I(WCONST, 12, 0, 0),
-/* 2 */ I(WCONST, 18, 0, 0),
+/* 0 */ I(WCONST, TAKSIZE1, 0, 0),
+/* 1 */ I(WCONST, TAKSIZE2, 0, 0),
+/* 2 */ I(WCONST, TAKSIZE3, 0, 0),
/* 3 */ I(WCALL3, 3, 6-3-2, 0),
/* 4 */ I(0, 1, 2, 0),
/* 5 */ I(WSTOP, 0, 1, 0),
@@ -203,8 +220,8 @@ unsigned int wordcode_tak[] = {
int main(int argc, char ** argv)
{
- printf("fib(30) = %ld\n", wordcode_interp(wordcode_fib));
- printf("tak(18, 12, 6) = %ld\n", wordcode_interp(wordcode_tak));
+ printf("fib(%d) = %ld\n", FIBSIZE, wordcode_interp(wordcode_fib));
+ printf("tak(%d, %d, %d) = %ld\n", TAKSIZE3, TAKSIZE2, TAKSIZE1, wordcode_interp(wordcode_tak));
return 0;
}
diff --git a/test/compression/Makefile b/test/compression/Makefile
index e8f3cf4d..ff7032d5 100644
--- a/test/compression/Makefile
+++ b/test/compression/Makefile
@@ -1,5 +1,7 @@
include ../../Makefile.config
+SIMU=timeout --signal=SIGKILL 20s $(EXECUTE)
+
CC=../../ccomp
CFLAGS=$(CCOMPOPTS) -U__GNUC__ -stdlib ../../runtime -dclight -dasm
LIBS=
@@ -30,15 +32,19 @@ TESTFILE:=$(firstword $(wildcard /usr/share/dict/words) ./lzss)
TESTCOMPR=/tmp/testcompr.$$$$
TESTEXPND=/tmp/testexpnd.$$$$
+LIGHTERFILEPRE:=/tmp/lighter
+LIGHTERFILE:=$(LIGHTERFILEPRE)aa
+
test:
- @rm -f $(TESTCOMPR) $(TESTEXPND); \
- echo "Test data: $(TESTFILE)"; \
+ @split -l15 $(TESTFILE) $(LIGHTERFILEPRE); \
+ rm -f $(TESTCOMPR) $(TESTEXPND); \
+ echo "Test data: $(LIGHTERFILE)"; \
for i in $(EXE); do \
echo "$$i: compression..."; \
- $(SIMU) ./$$i -c -i $(TESTFILE) -o $(TESTCOMPR); \
+ $(SIMU) ./$$i -c -i $(LIGHTERFILE) -o $(TESTCOMPR); \
echo "$$i: decompression..."; \
$(SIMU) ./$$i -d -i $(TESTCOMPR) -o $(TESTEXPND); \
- if cmp $(TESTFILE) $(TESTEXPND); \
+ if cmp $(LIGHTERFILE) $(TESTEXPND); \
then echo "$$i: passed"; \
else echo "$$i: FAILED"; exit 2; \
fi; \
diff --git a/test/cse2/globals.c b/test/cse2/globals.c
new file mode 100644
index 00000000..c6dd59cd
--- /dev/null
+++ b/test/cse2/globals.c
@@ -0,0 +1,8 @@
+int glob1, glob2;
+
+void toto() {
+ if (glob1 > 4) {
+ glob2 ++;
+ glob1 --;
+ }
+}
diff --git a/test/cse2/indexed_addr.c b/test/cse2/indexed_addr.c
new file mode 100644
index 00000000..30a7c571
--- /dev/null
+++ b/test/cse2/indexed_addr.c
@@ -0,0 +1,6 @@
+void foo(int *t) {
+ if (t[0] > 4) {
+ t[1] ++;
+ t[0] --;
+ }
+}
diff --git a/test/endian.h b/test/endian.h
index 8be2850c..204b69bc 100644
--- a/test/endian.h
+++ b/test/endian.h
@@ -1,7 +1,7 @@
#if defined(__ppc__) || defined(__PPC__) || defined(__ARMEB__)
#define ARCH_BIG_ENDIAN
#elif defined(__i386__) || defined(__x86_64__) || defined(__ARMEL__) \
- || defined(__riscv) || defined(__aarch64__)
+ || defined(__riscv) || defined(__aarch64__) || defined(__KVX__)
#undef ARCH_BIG_ENDIAN
#else
#error "unknown endianness"
diff --git a/test/gourdinl/builtin_memcpy.c b/test/gourdinl/builtin_memcpy.c
new file mode 100644
index 00000000..421f543c
--- /dev/null
+++ b/test/gourdinl/builtin_memcpy.c
@@ -0,0 +1,9 @@
+struct a b;
+struct a {
+ short;
+ int;
+ short;
+} c() {
+ struct a d[2][16];
+ b = d[1][7];
+}
diff --git a/test/gourdinl/c/add_return.c b/test/gourdinl/c/add_return.c
new file mode 100644
index 00000000..c29aeb16
--- /dev/null
+++ b/test/gourdinl/c/add_return.c
@@ -0,0 +1 @@
+int main(int r) { return r+1; }
diff --git a/test/gourdinl/c/addresses.c b/test/gourdinl/c/addresses.c
new file mode 100644
index 00000000..e3cb5201
--- /dev/null
+++ b/test/gourdinl/c/addresses.c
@@ -0,0 +1,32 @@
+/* addresses.c -- Playing with addresses of variables and their contents:
+ * what is done by C with variables, addresses, and values.
+ */
+
+#include <stdio.h>
+
+void moo(int a, int * b);
+
+int main(void) {
+ int x;
+ int *y;
+
+ x=1;
+ y=&x;
+ printf("Address of x = %d, value of x = %d\n", &x, x);
+ printf("Address of y = %d, value of y = %d, value of *y = %d\n", &y, y, *y);
+ moo(9,y);
+}
+
+void moo(int a, int *b){
+ printf("Address of a = %d, value of a = %d\n", &a, a);
+ printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
+}
+
+/* Output from running this program on my computer:
+
+Address of x = 536869640, value of x = 1
+Address of y = 536869632, value of y = 536869640, value of *y = 1
+Address of a = 536869608, value of a = 9
+Address of b = 536869600, value of b = 536869640, value of *b = 1
+
+ */
diff --git a/test/gourdinl/c/arith.c b/test/gourdinl/c/arith.c
new file mode 100644
index 00000000..02df141b
--- /dev/null
+++ b/test/gourdinl/c/arith.c
@@ -0,0 +1,16 @@
+int main(int num1, int num2)
+{
+ int sum, sub, mult, mod;
+ float div;
+
+ /*
+ * Perform all arithmetic operations
+ */
+ sum = num1 + num2;
+ sub = num1 - num2;
+ mult = num1 * num2;
+ div = (float)num1 / num2;
+ mod = num1 % num2;
+
+ return sum + sub + mult + div + mod;
+}
diff --git a/test/gourdinl/c/arith_print.c b/test/gourdinl/c/arith_print.c
new file mode 100644
index 00000000..d404a151
--- /dev/null
+++ b/test/gourdinl/c/arith_print.c
@@ -0,0 +1,19 @@
+int main()
+{
+ int num1 = 2;
+ int num2 = 4;
+ int sum, sub, mult, mod;
+ float div;
+
+ /*
+ * Perform all arithmetic operations
+ */
+ sum = num1 + num2;
+ sub = num1 - num2;
+ mult = num1 * num2;
+ div = (float)num1 / num2;
+ mod = num1 % num2;
+
+ printf("%d", sum + sub + mult + div + mod);
+ return;
+}
diff --git a/test/gourdinl/c/armstrong.c b/test/gourdinl/c/armstrong.c
new file mode 100644
index 00000000..c5d838f9
--- /dev/null
+++ b/test/gourdinl/c/armstrong.c
@@ -0,0 +1,21 @@
+int main()
+{
+ int n,sum,i,t,a,z;
+
+ for(i = 1; i <= 500; i++)
+ {
+ t = i; // as we need to retain the original number
+ sum = 0;
+ while(t != 0)
+ {
+ a = t%10;
+ sum += a*a*a;
+ t = t/10;
+ }
+
+ if(sum == i)
+ z += i;
+ }
+
+ return 0;
+}
diff --git a/test/gourdinl/c/array1.c b/test/gourdinl/c/array1.c
new file mode 100644
index 00000000..5840ca66
--- /dev/null
+++ b/test/gourdinl/c/array1.c
@@ -0,0 +1,64 @@
+/* array1.c -- Simple operations with arrays.
+ */
+
+#include <stdio.h>
+#define N 10
+
+void oneWay(void);
+void anotherWay(void);
+
+int main(void) {
+ printf("\noneWay:\n");
+ oneWay();
+ printf("\nantherWay:\n");
+ anotherWay();
+}
+
+/*Array initialized with aggregate */
+void oneWay(void) {
+ int vect[N] = {1,2,3,4,5,6,7,8,9,0};
+ int i;
+
+ for (i=0; i<N; i++)
+ printf("i = %2d vect[i] = %2d\n", i, vect[i]);
+}
+
+/*Array initialized with loop */
+void anotherWay(void) {
+ int vect[N];
+ int i;
+
+ for (i=0; i<N; i++)
+ vect[i] = i+1;
+
+ for (i=0; i<N; i++)
+ printf("i = %2d vect[i] = %2d\n", i, vect[i]);
+}
+
+/* The output of this program is
+
+oneWay:
+i = 0 vect[i] = 1
+i = 1 vect[i] = 2
+i = 2 vect[i] = 3
+i = 3 vect[i] = 4
+i = 4 vect[i] = 5
+i = 5 vect[i] = 6
+i = 6 vect[i] = 7
+i = 7 vect[i] = 8
+i = 8 vect[i] = 9
+i = 9 vect[i] = 0
+
+antherWay:
+i = 0 vect[i] = 1
+i = 1 vect[i] = 2
+i = 2 vect[i] = 3
+i = 3 vect[i] = 4
+i = 4 vect[i] = 5
+i = 5 vect[i] = 6
+i = 6 vect[i] = 7
+i = 7 vect[i] = 8
+i = 8 vect[i] = 9
+i = 9 vect[i] = 10
+
+ */
diff --git a/test/gourdinl/c/array2.c b/test/gourdinl/c/array2.c
new file mode 100644
index 00000000..389e1596
--- /dev/null
+++ b/test/gourdinl/c/array2.c
@@ -0,0 +1,74 @@
+/* array2.c -- Read/writing/reversing integer arrays
+ */
+
+#include <stdio.h>
+
+#define NMAX 10
+
+void intSwap(int *x, int *y);
+int getIntArray(int a[], int nmax, int sentinel);
+void printIntArray(int a[], int n);
+void reverseIntArray(int a[], int n);
+
+int main(void) {
+ int x[NMAX];
+ int hmny;
+
+ hmny = getIntArray(x, NMAX, 0);
+ printf("The array was: \n");
+ printIntArray(x,hmny);
+ reverseIntArray(x,hmny);
+ printf("after reverse it is:\n");
+ printIntArray(x,hmny);
+}
+
+void intSwap(int *x, int *y)
+ /* It swaps the content of x and y */
+{
+ int temp = *x;
+ *x = *y;
+ *y = temp;
+}
+
+void printIntArray(int a[], int n)
+ /* n is the number of elements in the array a.
+ * These values are printed out, five per line. */
+{
+ int i;
+
+ for (i=0; i<n; ){
+ printf("\t%d ", a[i++]);
+ if (i%5==0)
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int getIntArray(int a[], int nmax, int sentinel)
+ /* It reads up to nmax integers and stores then in a; sentinel
+ * terminates input. */
+{
+ int n = 0;
+ int temp;
+
+ do {
+ printf("Enter integer [%d to terminate] : ", sentinel);
+ scanf("%d", &temp);
+ if (temp==sentinel) break;
+ if (n==nmax)
+ printf("array is full\n");
+ else
+ a[n++] = temp;
+ }while (1);
+ return n;
+}
+
+void reverseIntArray(int a[], int n)
+ /* It reverse the order of the first n elements of a */
+{
+ int i;
+
+ for(i=0;i<n/2;i++){
+ intSwap(&a[i],&a[n-i-1]);
+ }
+}
diff --git a/test/gourdinl/c/biggest_of_3_int.c b/test/gourdinl/c/biggest_of_3_int.c
new file mode 100644
index 00000000..346908fc
--- /dev/null
+++ b/test/gourdinl/c/biggest_of_3_int.c
@@ -0,0 +1,10 @@
+int main(int a, int b, int c) {
+ if ((a > b) && (a > c)) {
+ return a;
+ } else if (b > c) {
+ return b;
+ } else {
+ return c;
+ }
+ return 0;
+}
diff --git a/test/gourdinl/c/bitwise1.c b/test/gourdinl/c/bitwise1.c
new file mode 100644
index 00000000..d20641de
--- /dev/null
+++ b/test/gourdinl/c/bitwise1.c
@@ -0,0 +1,8 @@
+int main()
+{
+ int x = 6, y = 4;
+ x = x^y;
+ y = x^876987^x | y << 42;
+
+ return ~x^y;
+}
diff --git a/test/gourdinl/c/cpintarray.c b/test/gourdinl/c/cpintarray.c
new file mode 100644
index 00000000..8049fdfb
--- /dev/null
+++ b/test/gourdinl/c/cpintarray.c
@@ -0,0 +1,108 @@
+/* cpintarray.c -- Example showing how addresses and arrays are alike
+ */
+
+#include <stdio.h>
+#define SIZE 8
+
+void cpIntArray(int *a, int *b, int n)
+/*It copies n integers starting at b into a*/
+{
+ for(;n>0;n--)
+ *a++=*b++;
+}
+
+
+void printIntArray(int a[], int n)
+ /* n is the number of elements in the array a.
+ * These values are printed out, five per line. */
+{
+ int i;
+
+ for (i=0; i<n; ){
+ printf("\t%d ", a[i++]);
+ if (i%5==0)
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int getIntArray(int a[], int nmax, int sentinel)
+ /* It reads up to nmax integers and stores then in a; sentinel
+ * terminates input. */
+{
+ int n = 0;
+ int temp;
+
+ do {
+ printf("Enter integer [%d to terminate] : ", sentinel);
+ scanf("%d", &temp);
+ if (temp==sentinel) break;
+ if (n==nmax)
+ printf("array is full\n");
+ else
+ a[n++] = temp;
+ }while (1);
+ return n;
+}
+
+int main(void){
+ int x[SIZE], nx;
+ int y[SIZE], ny;
+
+ printf("Read the x array:\n");
+ nx = getIntArray(x,SIZE,0);
+ printf("The x array is:\n");
+ printIntArray(x,nx);
+
+ printf("Read the y array:\n");
+ ny = getIntArray(y,SIZE,0);
+ printf("The y array is:\n");
+ printIntArray(y,ny);
+
+ cpIntArray(x+2,y+3,4);
+ /*Notice the expression 'x+2'. x is interpreted as the address for
+ the beginning of the x array. +2 sais to increment that address
+ by two units, in accordance with the type of x, which is
+ an integer array. Thus we move from x to two integer locations
+ past it, that is to the location of x[2]. The same reasoning applied
+ to 'y+3'.
+ */
+ printf("Printing x after having copied 4 elements\n"
+ "from y starting at y[3] into x starting at x[2]\n");
+ printIntArray(x,nx);
+}
+
+/* Here is the interaction in a run of this program:
+
+Read the x array:
+Enter integer [0 to terminate] : 1
+Enter integer [0 to terminate] : 3
+Enter integer [0 to terminate] : 5
+Enter integer [0 to terminate] : 7
+Enter integer [0 to terminate] : 9
+Enter integer [0 to terminate] : 11
+Enter integer [0 to terminate] : 13
+Enter integer [0 to terminate] : 15
+Enter integer [0 to terminate] : 0
+The x array is:
+ 1 3 5 7 9
+ 11 13 15
+Read the y array:
+Enter integer [0 to terminate] : 2
+Enter integer [0 to terminate] : 4
+Enter integer [0 to terminate] : 6
+Enter integer [0 to terminate] : 8
+Enter integer [0 to terminate] : 10
+Enter integer [0 to terminate] : 12
+Enter integer [0 to terminate] : 14
+Enter integer [0 to terminate] : 16
+Enter integer [0 to terminate] : 0
+The y array is:
+ 2 4 6 8 10
+ 12 14 16
+Printing x after having copied 4 elements
+from y starting at y[3] into x starting at x[2]
+ 1 3 8 10 12
+ 14 13 15
+
+ */
diff --git a/test/gourdinl/c/enum1.c b/test/gourdinl/c/enum1.c
new file mode 100644
index 00000000..d1f6b48d
--- /dev/null
+++ b/test/gourdinl/c/enum1.c
@@ -0,0 +1,52 @@
+/* enum1.c -- Starting to use enumerated types: Printing for each
+ * day of the week, today, yesterday, and tomorrow, both
+ * as a string and as a number.
+ */
+
+#include <stdio.h>
+
+/* Introducing an enumerated data type */
+enum days {monday,tuesday,wednesday,thursday,friday,saturday,sunday};
+typedef enum days days; // This allows us to use "days" as an abbreviation
+ // for "enum days"
+
+/* Two useful functions */
+days yesterday(days today){
+ return (today+6)%7;
+}
+days tomorrow(days today){
+ return (today+1)%7;
+}
+
+// A useful array: thedays is an array of constant (i.e you cannot
+// modify them) pointers to constant (i.e. you cannot modify them) strings
+const char * const thedays[] =
+ {"monday", "tuesday", "wednesday", "thursday",
+ "friday", "saturday", "sunday"};
+
+int main(void){
+ days today;
+
+ printf("today \tyesterday \ttomorrow\n"
+ "============================================\n");
+ for (today=monday;today<=sunday;today++)
+ printf("%s = %d \t %s = %d \t %s = %d\n",
+ thedays[today], today,
+ thedays[yesterday(today)], yesterday(today),
+ thedays[tomorrow(today)], tomorrow(today));
+}
+
+/*
+ The output is:
+
+today yesterday tomorrow
+============================================
+monday = 0 sunday = 6 tuesday = 1
+tuesday = 1 monday = 0 wednesday = 2
+wednesday = 2 tuesday = 1 thursday = 3
+thursday = 3 wednesday = 2 friday = 4
+friday = 4 thursday = 3 saturday = 5
+saturday = 5 friday = 4 sunday = 6
+sunday = 6 saturday = 5 monday = 0
+
+*/
diff --git a/test/gourdinl/c/enum2.c b/test/gourdinl/c/enum2.c
new file mode 100644
index 00000000..a18acb80
--- /dev/null
+++ b/test/gourdinl/c/enum2.c
@@ -0,0 +1,50 @@
+/* enum2.c -- Starting to use enumerated types: Printing for each
+ * day of the week, today, yesterday, and tomorrow, both
+ * as a string and as a number. We use typedef
+ */
+
+#include <stdio.h>
+
+/* Introducing an enumerated data type */
+typedef enum {monday,tuesday,wednesday,thursday,friday,saturday,sunday} days;
+
+/* Two useful functions */
+days yesterday(days today);
+days tomorrow(days today);
+
+char *thedays[] = {"monday", "tuesday", "wednesday", "thursday",
+ "friday", "saturday", "sunday"};
+
+int main(void){
+ days today;
+
+ printf("today \tyesterday \ttomorrow\n"
+ "============================================\n");
+ for (today=monday;today<=sunday;today++)
+ printf("%s = %d \t %s = %d \t %s = %d\n",
+ thedays[today], today,
+ thedays[yesterday(today)], yesterday(today),
+ thedays[tomorrow(today)], tomorrow(today));
+}
+
+days yesterday(days today){
+ return (today+6)%7;
+}
+days tomorrow(days today){
+ return (today+1)%7;
+}
+
+/*
+ The output is:
+
+today yesterday tomorrow
+============================================
+monday = 0 sunday = 6 tuesday = 1
+tuesday = 1 monday = 0 wednesday = 2
+wednesday = 2 tuesday = 1 thursday = 3
+thursday = 3 wednesday = 2 friday = 4
+friday = 4 thursday = 3 saturday = 5
+saturday = 5 friday = 4 sunday = 6
+sunday = 6 saturday = 5 monday = 0
+
+*/
diff --git a/test/gourdinl/c/floop.c b/test/gourdinl/c/floop.c
new file mode 100644
index 00000000..30270892
--- /dev/null
+++ b/test/gourdinl/c/floop.c
@@ -0,0 +1,8 @@
+int main(int x)
+{
+ int y = 4;
+ int s = 23;
+ for(int i = 0; i <= x; i++)
+ y << s;
+ return y;
+}
diff --git a/test/gourdinl/c/floor.c b/test/gourdinl/c/floor.c
new file mode 100644
index 00000000..33a57af3
--- /dev/null
+++ b/test/gourdinl/c/floor.c
@@ -0,0 +1,29 @@
+int main(int n)
+{
+ int x = 1, i;
+
+ /* for positive values */
+ if (n > 0)
+ {
+ for (; x <= n >> 1;)
+ {
+ x = x << 1;
+ }
+ n = x;
+ }
+ /* for negative values */
+ else
+ {
+ n = ~n;
+ n = n + 1;
+ for (; x <= n >> 1;)
+ {
+ x = x << 1;
+ }
+ x = x << 1;
+ x = ~x;
+ x = x + 1;
+ n = x;
+ }
+ return n;
+}
diff --git a/test/gourdinl/c/funcs.c b/test/gourdinl/c/funcs.c
new file mode 100644
index 00000000..49e610d6
--- /dev/null
+++ b/test/gourdinl/c/funcs.c
@@ -0,0 +1,36 @@
+/* funcs.c -- More examples of functions
+ */
+
+#include <stdio.h>
+
+int getint(void); /*It prompts user to enter an integer, which it returns*/
+
+int getmax(int a, int b, int c); /*It returns value of largest of a, b, c*/
+
+/* Main program: Using the various functions */
+int main (void) {
+ int x, y, z;
+
+ x = getint();
+ y = getint();
+ z = getint();
+ printf("The largest of %d, %d, and %d is %d\n", x, y, z, getmax(x,y,z));
+}
+
+int getint(void) {
+ int a;
+
+ printf("Please enter an integer > ");
+ scanf("%d", &a);
+ return(a);
+}
+
+int getmax(int a, int b, int c){
+ int m = a;
+
+ if (m<b)
+ m = b;
+ if (m<c)
+ m = c;
+ return(m);
+}
diff --git a/test/gourdinl/c/hello.c b/test/gourdinl/c/hello.c
new file mode 100644
index 00000000..0279269e
--- /dev/null
+++ b/test/gourdinl/c/hello.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(void) {
+ printf("Hello World!\n");
+ return 0;
+}
diff --git a/test/gourdinl/c/if.c b/test/gourdinl/c/if.c
new file mode 100644
index 00000000..7d2e249a
--- /dev/null
+++ b/test/gourdinl/c/if.c
@@ -0,0 +1,7 @@
+int main(int x)
+{
+ if (x > 27)
+ return 11;
+ else x--;
+ return x;
+}
diff --git a/test/gourdinl/c/msb_pos.c b/test/gourdinl/c/msb_pos.c
new file mode 100644
index 00000000..f2e7fe09
--- /dev/null
+++ b/test/gourdinl/c/msb_pos.c
@@ -0,0 +1,20 @@
+/* Function to find the MSB bit position */
+int main(int n)
+{
+ int i = 0, bit;
+ while (i < 32)
+ {
+ bit = n & 0x80000000;
+ if (bit == -0x80000000)
+ {
+ bit = 1;
+ }
+
+ if (bit == 1)
+ break;
+
+ n = n << 1;
+ i++;
+ }
+ return i;
+}
diff --git a/test/gourdinl/c/power2.c b/test/gourdinl/c/power2.c
new file mode 100644
index 00000000..64707df9
--- /dev/null
+++ b/test/gourdinl/c/power2.c
@@ -0,0 +1,42 @@
+/* power2.c -- Print out powers of 2: 1, 2, 4, 8, .. up to 2^N
+ */
+
+#include <stdio.h>
+#define N 16
+
+int main(void) {
+ int n; /* The current exponent */
+ int val = 1; /* The current power of 2 */
+
+ printf("\t n \t 2^n\n");
+ printf("\t================\n");
+ for (n=0; n<=N; n++) {
+ printf("\t%3d \t %6d\n", n, val);
+ val = 2*val;
+ }
+ return 0;
+}
+
+/* It prints out :
+
+ n 2^n
+ ================
+ 0 1
+ 1 2
+ 2 4
+ 3 8
+ 4 16
+ 5 32
+ 6 64
+ 7 128
+ 8 256
+ 9 512
+ 10 1024
+ 11 2048
+ 12 4096
+ 13 8192
+ 14 16384
+ 15 32768
+ 16 65536
+
+*/
diff --git a/test/gourdinl/c/prime.c b/test/gourdinl/c/prime.c
new file mode 100644
index 00000000..6c51db32
--- /dev/null
+++ b/test/gourdinl/c/prime.c
@@ -0,0 +1,23 @@
+/* prime1.c It prompts the user to enter an integer N. It prints out
+ * if it is a prime or not. If not, it prints out a factor of N.
+ */
+
+#include <stdio.h>
+
+int main(int n) {
+ int i;
+ int flag;
+
+ flag = 1;
+ for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
+ values of i greater than the square root of n? */
+ if ((n % i) == 0) /* If true n is divisible by i */
+ flag = 0;
+ else
+ i++;
+ }
+
+ if (flag)
+ return 1;
+ return 0;
+}
diff --git a/test/gourdinl/c/random.c b/test/gourdinl/c/random.c
new file mode 100644
index 00000000..50aa5737
--- /dev/null
+++ b/test/gourdinl/c/random.c
@@ -0,0 +1,50 @@
+/* Generating random number sequences using the formula (linear congruence)
+ x[k+1] = (a*x[k] + c)mod m
+ where a, c, and m are parameters set by the user and passed as command line
+ parameters together with a seed i.e. x[0]
+ As a simple example try a=7, c=1, m=13, and seed=5
+ A more sophisticated selection would be a=69069, c=0,
+ m=2^32=4294967296, and seed=31
+ It will print out, in a sort of random order, up to m-1 distinct values.
+ Then it loops.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+static long seed = 13;
+static long a;
+static long c;
+static long m;
+
+void random_init(long s) {
+ if (s != 0) seed = s;
+}
+
+long random() {
+ seed = (a*seed + c)%m;
+ return seed;
+ }
+
+int main(int argc, char * argv[]) {
+ if (argc != 5) {
+ printf("usage: %s a, c, m, seed\n", argv[0]);
+ return 1;
+ }
+ a = atoi(argv[1]);
+ c = atoi(argv[2]);
+ m = atoi(argv[3]);
+ long s = atoi(argv[4]);
+ random_init(s);
+ int k;
+ for (k = 0; k < m-1; k++) {
+ printf("%8ld", random());
+ if (k % 8 == 7) { // after 8 elements go to a new line
+ printf("\n");
+ sleep(1); // sleep for a second
+ }
+ }
+ printf("\n");
+ return 0;
+}
diff --git a/test/gourdinl/c/simple_op.c b/test/gourdinl/c/simple_op.c
new file mode 100644
index 00000000..7c43b081
--- /dev/null
+++ b/test/gourdinl/c/simple_op.c
@@ -0,0 +1,8 @@
+int main(int argc, char ** argv)
+{
+ int n, m;
+ n = n + 1;
+ n = n * 7;
+ n / (8 - 2);
+ return n;
+}
diff --git a/test/gourdinl/c/wloop.c b/test/gourdinl/c/wloop.c
new file mode 100644
index 00000000..5ba67419
--- /dev/null
+++ b/test/gourdinl/c/wloop.c
@@ -0,0 +1,8 @@
+int main(int x)
+{
+ int y = 4;
+ int s = 23;
+ while(s < x)
+ y << s;
+ return y;
+}
diff --git a/test/gourdinl/clause.h b/test/gourdinl/clause.h
new file mode 100644
index 00000000..3eb44402
--- /dev/null
+++ b/test/gourdinl/clause.h
@@ -0,0 +1,12 @@
+typedef struct {
+ int b;
+ int a;
+} * CLAUSE;
+__inline__ int g(CLAUSE c) { return c->b; }
+__inline__ int d(CLAUSE c) { return c->a; }
+__inline__ void clause_SetNumOfConsLits(CLAUSE c, int e) {
+ c->b = e;
+ c->a = e;
+}
+__inline__ int f(CLAUSE c) { return g(c) + d(c); }
+__inline__ int clause_LastLitIndex(c) { return f(c); }
diff --git a/test/gourdinl/clause2.c b/test/gourdinl/clause2.c
new file mode 100644
index 00000000..42cd0fa6
--- /dev/null
+++ b/test/gourdinl/clause2.c
@@ -0,0 +1,23 @@
+#include "clause.h"
+int a, b;
+void c();
+void h() {
+ int f = clause_LastLitIndex(d);
+ a = clause_LastLitIndex(0);
+ if (f)
+ if (a)
+ 1;
+}
+void i() {
+ CLAUSE e = 0;
+ int *g[] = {h, c};
+ for (; b;)
+ l(e);
+}
+void m() {
+ int k, j;
+ for (; k <= 0;)
+ ;
+ clause_SetNumOfConsLits(0, j);
+ n(0 - j);
+}
diff --git a/test/gourdinl/compare_pp.sh b/test/gourdinl/compare_pp.sh
new file mode 100755
index 00000000..09183cf9
--- /dev/null
+++ b/test/gourdinl/compare_pp.sh
@@ -0,0 +1,16 @@
+ffname=$(basename $1)
+fname=${ffname%.*}
+nopp=$fname.nopp.s
+pp=$fname.pp.s
+
+../../ccomp -fno-coalesce-mem -fno-postpass -S $1 -o $nopp
+../../ccomp -fno-coalesce-mem -fpostpass= list -S $1 -o $pp
+sed -i '1,2d' $nopp
+sed -i '1,2d' $pp
+if cmp -s $nopp $pp; then
+ echo "same!"
+else
+ echo "differents!"
+ diff -y $nopp $pp
+fi
+
diff --git a/test/gourdinl/cond_exp_mini_cse.c b/test/gourdinl/cond_exp_mini_cse.c
new file mode 100644
index 00000000..3a2ce9c3
--- /dev/null
+++ b/test/gourdinl/cond_exp_mini_cse.c
@@ -0,0 +1,6 @@
+int main(int x, int y, int* t) {
+ if (x + *t < 7)
+ if (y < 7)
+ return 421;
+ return 0;
+}
diff --git a/test/gourdinl/cscript.sh b/test/gourdinl/cscript.sh
new file mode 100755
index 00000000..8bf3a613
--- /dev/null
+++ b/test/gourdinl/cscript.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+/home/yuki/Work/VERIMAG/Compcert_neutral/ccomp -stdlib ../../runtime -dparse -dclight -S -fstruct-return -c clause2.c > log 2>&1
+
+b1=$(cat log | ack "LDP_CONSEC_PEEP_IMM_DEC_ldr64")
+sb1=$?
+b2=$(cat log | ack "LDP_BACK_SPACED_PEEP_IMM_DEC_ldr32")
+sb2=$?
+b3=$(cat log | ack "STP_FORW_SPACED_PEEP_IMM_INC_str32")
+sb3=$?
+b4=$(cat log | ack "STP_CONSEC_PEEP_IMM_INC_str64")
+sb4=$?
+
+#if [ "$sb1" == 0 ] && [ "$sb2" == 0 ] && [ "$sb3" == 0 ] && [ "$sb4" == 0 ]
+if [ "$sb1" == 0 ] && [ "$sb2" == 0 ] && [ "$sb3" == 0 ] && [ "$sb4" == 0 ]
+then
+ exit 0
+else
+ exit 1
+fi
diff --git a/test/gourdinl/fp_init.c b/test/gourdinl/fp_init.c
new file mode 100644
index 00000000..1d835994
--- /dev/null
+++ b/test/gourdinl/fp_init.c
@@ -0,0 +1,7 @@
+int main (float *x) {
+ double a = 1.0;
+ float b = 1.0f;
+ printf("%f", a);
+ *x = b;
+ return b;
+}
diff --git a/test/gourdinl/gen_asm_files.sh b/test/gourdinl/gen_asm_files.sh
new file mode 100755
index 00000000..08cd4b3d
--- /dev/null
+++ b/test/gourdinl/gen_asm_files.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+../../ccomp -S clause2.c -o clause2.nopostpass.noph.s -fno-coalesce-mem -fno-postpass
+../../ccomp -S clause2.c -o clause2.nopostpass.ph.s -fcoalesce-mem -fno-postpass
+../../ccomp -S clause2.c -o clause2.noph.s -fno-coalesce-mem
+../../ccomp -S clause2.c -o clause2.ph.s -fcoalesce-mem
diff --git a/test/gourdinl/postpass_alternate_str.c b/test/gourdinl/postpass_alternate_str.c
new file mode 100644
index 00000000..99c50d7d
--- /dev/null
+++ b/test/gourdinl/postpass_alternate_str.c
@@ -0,0 +1,11 @@
+long a;
+short *b;
+void c() {
+ long d = &a;
+ char e = 0;
+ long f[4] = {&e};
+ if (*b)
+ if (c) {
+ long g = &d;
+ }
+}
diff --git a/test/gourdinl/postpass_exp.c b/test/gourdinl/postpass_exp.c
new file mode 100644
index 00000000..522ac2a6
--- /dev/null
+++ b/test/gourdinl/postpass_exp.c
@@ -0,0 +1,5 @@
+int main(int x, int y) {
+ int z = x << 32;
+ y = y - z;
+ return x + y;
+}
diff --git a/test/kvx/.gitignore b/test/kvx/.gitignore
new file mode 100644
index 00000000..b10c40c8
--- /dev/null
+++ b/test/kvx/.gitignore
@@ -0,0 +1,20 @@
+check
+asm_coverage
+instr/Makefile
+mmult/Makefile
+prng/Makefile
+sort/Makefile
+prng/.zero
+sort/.zero
+sort/insertion-ccomp-kvx
+sort/insertion-gcc-kvx
+sort/insertion-gcc-x86
+sort/main-ccomp-kvx
+sort/main-gcc-kvx
+sort/main-gcc-x86
+sort/merge-ccomp-kvx
+sort/merge-gcc-kvx
+sort/merge-gcc-x86
+sort/selection-ccomp-kvx
+sort/selection-gcc-kvx
+sort/selection-gcc-x86
diff --git a/test/kvx/builtins/clzll.c b/test/kvx/builtins/clzll.c
new file mode 100644
index 00000000..13905cba
--- /dev/null
+++ b/test/kvx/builtins/clzll.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = __builtin_clzll(a);
+}
+END_TEST()
diff --git a/test/kvx/builtins/stsud.c b/test/kvx/builtins/stsud.c
new file mode 100644
index 00000000..fa42b001
--- /dev/null
+++ b/test/kvx/builtins/stsud.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 2)
+{
+ c = __builtin_kvx_stsud(t[0], t[1]);
+}
+END_TEST()
diff --git a/test/kvx/coverage.sh b/test/kvx/coverage.sh
new file mode 100755
index 00000000..96f6bc04
--- /dev/null
+++ b/test/kvx/coverage.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+printer=../../kvx/TargetPrinter.ml
+asmdir=instr/asm/
+to_cover_raw=/tmp/to_cover_raw
+to_cover=/tmp/to_cover
+covered_raw=/tmp/covered_raw
+covered=/tmp/covered
+
+# Stop at any error
+set -e
+# Pipes do not mask errors
+set -o pipefail
+
+sed -n "s/^.*fprintf\s\+oc\s*\"\s*\([a-z][^[:space:]]*\)\s.*/\1/p" $printer > $to_cover_raw
+python2.7 coverage_helper.py $to_cover_raw | sort -u > $to_cover
+
+rm -f $covered_raw
+for asm in $(ls $asmdir/*.ccomp.s); do
+ grep -v ":" $asm | sed -n "s/^\s*\([a-z][a-z0-9.]*\).*/\1/p" | sort -u >> $covered_raw
+done
+python2.7 coverage_helper.py $covered_raw | sort -u > $covered
+
+vimdiff $to_cover $covered
diff --git a/test/kvx/coverage_helper.py b/test/kvx/coverage_helper.py
new file mode 100644
index 00000000..e5b1907c
--- /dev/null
+++ b/test/kvx/coverage_helper.py
@@ -0,0 +1,45 @@
+import fileinput
+import sys
+
+all_loads_stores = "lbs lbz lhz lo lq ld lhs lws sb sd sh so sq sw".split(" ")
+
+all_bconds = "wnez weqz wltz wgez wlez wgtz dnez deqz dltz dgez dlez dgtz".split(" ")
+
+all_iconds = "ne eq lt ge le gt ltu geu leu gtu".split(" ")
+
+all_fconds = "one ueq oeq une olt uge oge ult".split(" ")
+
+replaces_a = [(["cb.", "cmoved."], all_bconds),
+ (["compd.", "compw."], all_iconds),
+ (["fcompd.", "fcompw."], all_fconds),
+ (all_loads_stores, [".xs", ""])]
+
+replaces_dd = [(["addx", "sbfx"], ["2d", "4d", "8d", "16d"])]
+replaces_dw = [(["addx", "sbfx"], ["2w", "4w", "8w", "16w"])]
+
+macros_binds = {"%a": replaces_a, "%dd": replaces_dd, "%dw": replaces_dw}
+
+def expand_macro(fullinst, macro, replaceTable):
+ inst = fullinst.replace(macro, "")
+ for (searchlist, mods) in replaceTable:
+ if inst in searchlist:
+ return [fullinst.replace(macro, mod) for mod in mods]
+ raise NameError
+
+insts = []
+for line in fileinput.input():
+ fullinst = line[:-1]
+ try:
+ for macro in macros_binds:
+ if macro in fullinst:
+ insts.extend(expand_macro(fullinst, macro, macros_binds[macro]))
+ break
+ else:
+ insts.append(fullinst)
+ except NameError:
+ print >> sys.stderr, fullinst + " could not be found any match for macro " + macro
+ sys.exit(1)
+
+for inst in insts:
+ print inst
+occurs = {}
diff --git a/test/kvx/delout.sh b/test/kvx/delout.sh
new file mode 100755
index 00000000..e9c72e1c
--- /dev/null
+++ b/test/kvx/delout.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+for folder in prng mmult sort instr interop; do
+ rm -f $folder/*.out
+ rm -f $folder/out/*
+done
diff --git a/test/kvx/do_test.sh b/test/kvx/do_test.sh
new file mode 100644
index 00000000..5cc23dee
--- /dev/null
+++ b/test/kvx/do_test.sh
@@ -0,0 +1,50 @@
+do_test () {
+cat << EOF
+
+##
+# PRNG tests
+##
+EOF
+(cd prng && make $1 -j$2)
+
+cat << EOF
+
+##
+# Matrix Multiplication tests
+##
+EOF
+(cd mmult && make $1 -j$2)
+
+cat << EOF
+
+##
+# List sort tests
+##
+EOF
+(cd sort && make $1 -j$2)
+
+cat << EOF
+
+##
+# Instruction unit tests
+##
+EOF
+(cd instr && make $1 -j$2)
+
+cat << EOF
+
+##
+# Interoperability with GCC
+##
+EOF
+(cd interop && make $1 -j$2)
+
+cat << EOF
+
+##
+# printf wrapper test
+##
+(cd lib && make $1 -j$2)
+EOF
+
+}
diff --git a/test/kvx/general/clzd.c b/test/kvx/general/clzd.c
new file mode 100644
index 00000000..d3e8a8ec
--- /dev/null
+++ b/test/kvx/general/clzd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 1)
+{
+ c = __builtin_kvx_clzd(t[0]);
+}
+END_TEST()
diff --git a/test/kvx/general/clzw.c b/test/kvx/general/clzw.c
new file mode 100644
index 00000000..7b5478fd
--- /dev/null
+++ b/test/kvx/general/clzw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 1)
+{
+ c = __builtin_kvx_clzw(t[0]);
+}
+END_TEST()
diff --git a/test/kvx/general/ctzd.c b/test/kvx/general/ctzd.c
new file mode 100644
index 00000000..bba869e1
--- /dev/null
+++ b/test/kvx/general/ctzd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 1)
+{
+ c = __builtin_kvx_ctzd(t[0]);
+}
+END_TEST()
diff --git a/test/kvx/general/ctzw.c b/test/kvx/general/ctzw.c
new file mode 100644
index 00000000..a7128b04
--- /dev/null
+++ b/test/kvx/general/ctzw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 1)
+{
+ c = __builtin_kvx_ctzw(t[0]);
+}
+END_TEST()
diff --git a/test/kvx/general/satd.c b/test/kvx/general/satd.c
new file mode 100644
index 00000000..9d0d1cf9
--- /dev/null
+++ b/test/kvx/general/satd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 2)
+{
+ c = __builtin_kvx_satd(t[0], t[1]);
+}
+END_TEST()
diff --git a/test/kvx/general/sbmm8.c b/test/kvx/general/sbmm8.c
new file mode 100644
index 00000000..91f13425
--- /dev/null
+++ b/test/kvx/general/sbmm8.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 2)
+{
+ c = __builtin_kvx_sbmm8(t[0], t[1]);
+}
+END_TEST()
diff --git a/test/kvx/general/sbmmt8.c b/test/kvx/general/sbmmt8.c
new file mode 100644
index 00000000..7b120dfa
--- /dev/null
+++ b/test/kvx/general/sbmmt8.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST_N(unsigned long long, 2)
+{
+ c = __builtin_kvx_sbmmt8(t[0], t[1]);
+}
+END_TEST()
diff --git a/test/kvx/hardcheck.sh b/test/kvx/hardcheck.sh
new file mode 100755
index 00000000..b6538f0e
--- /dev/null
+++ b/test/kvx/hardcheck.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+# Tests the execution of the binaries produced by CompCert, in hardware
+
+source do_test.sh
+
+do_test hardcheck 1
diff --git a/test/kvx/hardtest.sh b/test/kvx/hardtest.sh
new file mode 100755
index 00000000..6321bc7d
--- /dev/null
+++ b/test/kvx/hardtest.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+# Tests the validity of the tests, in hardware
+
+source do_test.sh
+
+do_test hardtest 1
diff --git a/test/kvx/instr/.gitignore b/test/kvx/instr/.gitignore
new file mode 100644
index 00000000..ea1472ec
--- /dev/null
+++ b/test/kvx/instr/.gitignore
@@ -0,0 +1 @@
+output/
diff --git a/test/kvx/instr/Makefile b/test/kvx/instr/Makefile
new file mode 100644
index 00000000..fce32178
--- /dev/null
+++ b/test/kvx/instr/Makefile
@@ -0,0 +1,176 @@
+SHELL := /bin/bash
+
+KVXC ?= kvx-elf-gcc
+CC ?= gcc
+CCOMP ?= ccomp
+OPTIM ?= -O2
+CFLAGS ?= $(OPTIM)
+CCOMPFLAGS ?= $(CFLAGS)
+SIMU ?= kvx-mppa
+TIMEOUT ?= --signal=SIGTERM 120s
+DIFF ?= python2.7 floatcmp.py -reltol .00001
+HARDRUN ?= kvx-jtag-runner
+
+DIR=./
+SRCDIR=$(DIR)
+OUTDIR=$(DIR)/out
+BINDIR=$(DIR)/bin
+ASMDIR=$(DIR)/asm
+LIB=../lib/system.x86-gcc.a
+K1LIB=../lib/system.gcc.a
+
+##
+# Intended flow : .c -> .gcc.s -> .gcc.bin -> .gcc.out
+# -> .ccomp.s -> .ccomp.bin -> .ccomp.out
+##
+
+KVXCPATH=$(shell which $(KVXC))
+CCPATH=$(shell which $(CC))
+CCOMPPATH=$(shell which $(CCOMP))
+SIMUPATH=$(shell which $(SIMU))
+
+TESTNAMES?=$(notdir $(subst .c,,$(wildcard $(DIR)/*.c)))
+X86_GCC_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .x86-gcc.out,$(TESTNAMES)))
+GCC_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.simu.out,$(TESTNAMES)))
+CCOMP_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.simu.out,$(TESTNAMES)))
+GCC_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.hard.out,$(TESTNAMES)))
+CCOMP_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.hard.out,$(TESTNAMES)))
+
+BIN=$(addprefix $(BINDIR)/,$(addsuffix .x86-gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .ccomp.bin,$(TESTNAMES)))
+
+##
+# Targets
+##
+
+all: $(BIN)
+
+GREEN=\033[0;32m
+RED=\033[0;31m
+YELLOW=\033[0;33m
+NC=\033[0m
+
+.PHONY:
+test: simutest
+
+.PHONY:
+check: simucheck
+
+.PHONY:
+simutest: $(X86_GCC_OUT) $(GCC_SIMUOUT)
+ @echo "Comparing x86 gcc output to k1 gcc.."
+ for test in $(TESTNAMES); do\
+ x86out=$(OUTDIR)/$$test.x86-gcc.out;\
+ gccout=$(OUTDIR)/$$test.gcc.simu.out;\
+ if grep "__KVX__" -q $$test.c; then\
+ printf "$(YELLOW)UNTESTED: $$test.c contains an \`#ifdef __KVX__\`\n$(NC)";\
+ elif $(DIFF) $$x86out $$gccout > /dev/null; test $${PIPESTATUS[0]} -ne 0; then\
+ >&2 printf "$(RED)ERROR: $$x86out and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$x86out and $$gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+simucheck: $(GCC_SIMUOUT) $(CCOMP_SIMUOUT)
+ @echo "Comparing k1 gcc output to ccomp.."
+ @for test in $(TESTNAMES); do\
+ gccout=$(OUTDIR)/$$test.gcc.simu.out;\
+ ccompout=$(OUTDIR)/$$test.ccomp.simu.out;\
+ if $(DIFF) $$ccompout $$gccout > /dev/null; test $${PIPESTATUS[0]} -ne 0; then\
+ >&2 printf "$(RED)ERROR: $$ccompout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$ccompout and $$gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+hardtest: $(X86_GCC_OUT) $(GCC_HARDOUT)
+ @echo "Comparing x86 gcc output to k1 gcc.."
+ for test in $(TESTNAMES); do\
+ x86out=$(OUTDIR)/$$test.x86-gcc.out;\
+ gccout=$(OUTDIR)/$$test.gcc.hard.out;\
+ if grep "__KVX__" -q $$test.c; then\
+ printf "$(YELLOW)UNTESTED: $$test.c contains an \`#ifdef __KVX__\`\n$(NC)";\
+ elif $(DIFF) $$x86out $$gccout > /dev/null; test $${PIPESTATUS[0]} -ne 0; then\
+ >&2 printf "$(RED)ERROR: $$x86out and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$x86out and $$gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+hardcheck: $(GCC_HARDOUT) $(CCOMP_HARDOUT)
+ @echo "Comparing k1 gcc output to ccomp.."
+ @for test in $(TESTNAMES); do\
+ gccout=$(OUTDIR)/$$test.gcc.hard.out;\
+ ccompout=$(OUTDIR)/$$test.ccomp.hard.out;\
+ if $(DIFF) $$ccompout $$gccout > /dev/null; test $${PIPESTATUS[0]} -ne 0; then\
+ >&2 printf "$(RED)ERROR: $$ccompout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$ccompout and $$gccout concur$(NC)\n";\
+ fi;\
+ done
+
+##
+# Rules
+##
+
+.SECONDARY:
+$(LIB):
+ (cd $(dir $(LIB)) && make)
+
+$(K1LIB):
+ (cd $(dir $(LIB)) && make)
+
+# Generating output
+
+## Version avec timeout
+$(OUTDIR)/%.x86-gcc.out: $(BINDIR)/%.x86-gcc.bin
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.simu.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.simu.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.hard.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.hard.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+# Assembly to binary
+
+$(BINDIR)/%.x86-gcc.bin: $(ASMDIR)/%.x86-gcc.s $(LIB) $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+$(BINDIR)/%.gcc.bin: $(ASMDIR)/%.gcc.s $(K1LIB) $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $(filter-out $(KVXCPATH),$^) -o $@
+
+$(BINDIR)/%.ccomp.bin: $(ASMDIR)/%.ccomp.s $(K1LIB) $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CCOMPFLAGS) $(filter-out $(CCOMPPATH),$^) -o $@
+
+# Source to assembly
+
+$(ASMDIR)/%.x86-gcc.s: $(SRCDIR)/%.c $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.gcc.s: $(SRCDIR)/%.c $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.ccomp.s: $(SRCDIR)/%.c $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CCOMPFLAGS) -S $< -o $@
diff --git a/test/kvx/instr/builtin32.c b/test/kvx/instr/builtin32.c
new file mode 100644
index 00000000..9efb33cd
--- /dev/null
+++ b/test/kvx/instr/builtin32.c
@@ -0,0 +1,12 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+ int *ptr = &c;
+#ifdef __KVX__
+ int d = c;
+ a = __builtin_kvx_alclrw(ptr);
+ c = d;
+
+#endif
+END_TEST32()
+
diff --git a/test/kvx/instr/builtin64.c b/test/kvx/instr/builtin64.c
new file mode 100644
index 00000000..252eb2c6
--- /dev/null
+++ b/test/kvx/instr/builtin64.c
@@ -0,0 +1,17 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+ long long *ptr = &c;
+#ifdef __KVX__
+ long long d = c;
+ a = __builtin_kvx_alclrd(ptr);
+ c = d;
+ c += a;
+
+ c += __builtin_clzll(a);
+
+ /* Removed the AFADDD builtin who was incorrect in CompCert, see #157 */
+ // a = __builtin_kvx_afaddd(ptr, a);
+ // a = __builtin_kvx_afaddd(ptr, a);
+#endif
+END_TEST64()
diff --git a/test/kvx/instr/div32.c b/test/kvx/instr/div32.c
new file mode 100644
index 00000000..83c3a0e3
--- /dev/null
+++ b/test/kvx/instr/div32.c
@@ -0,0 +1,5 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+ c = a/b;
+END_TEST32()
diff --git a/test/kvx/instr/divf32.c b/test/kvx/instr/divf32.c
new file mode 100644
index 00000000..513a3293
--- /dev/null
+++ b/test/kvx/instr/divf32.c
@@ -0,0 +1,5 @@
+#include "framework.h"
+
+BEGIN_TEST(float)
+ c = a / b;
+END_TESTF32()
diff --git a/test/kvx/instr/divf64.c b/test/kvx/instr/divf64.c
new file mode 100644
index 00000000..0dd23826
--- /dev/null
+++ b/test/kvx/instr/divf64.c
@@ -0,0 +1,5 @@
+#include "framework.h"
+
+BEGIN_TEST(double)
+ c = a / b;
+END_TESTF64()
diff --git a/test/kvx/instr/divu32.c b/test/kvx/instr/divu32.c
new file mode 100644
index 00000000..1fe196c4
--- /dev/null
+++ b/test/kvx/instr/divu32.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = a/b;
+}
+END_TEST32()
diff --git a/test/kvx/instr/f32.c b/test/kvx/instr/f32.c
new file mode 100644
index 00000000..7e304aeb
--- /dev/null
+++ b/test/kvx/instr/f32.c
@@ -0,0 +1,8 @@
+#include "framework.h"
+
+BEGIN_TEST(float)
+ c = ((float)a + (float)b);
+ c += ((float)a * (float)b);
+ c += (-(float)a);
+ c += ((float)a - (float)b);
+END_TESTF32()
diff --git a/test/kvx/instr/f64.c b/test/kvx/instr/f64.c
new file mode 100644
index 00000000..be8094c9
--- /dev/null
+++ b/test/kvx/instr/f64.c
@@ -0,0 +1,8 @@
+#include "framework.h"
+
+BEGIN_TEST(double)
+ c = ((double)a + (double)b);
+ c += ((double)a * (double)b);
+ c += (-(double)a);
+ c += ((double)a - (double)b);
+END_TESTF64()
diff --git a/test/kvx/instr/floatcmp.py b/test/kvx/instr/floatcmp.py
new file mode 100755
index 00000000..49f1bc13
--- /dev/null
+++ b/test/kvx/instr/floatcmp.py
@@ -0,0 +1,93 @@
+#!/usr/bin/python2.7
+
+import argparse as ap
+import sys
+
+parser = ap.ArgumentParser()
+parser.add_argument("file1", help="First file to compare")
+parser.add_argument("file2", help="Second file to compare")
+parser.add_argument("-reltol", help="Relative error")
+parser.add_argument("-abstol", help="Absolute error")
+parser.add_argument("-s", help="Silent output", action="store_true")
+args = parser.parse_args()
+
+reltol = float(args.reltol) if args.reltol else None
+abstol = float(args.abstol) if args.abstol else None
+silent = args.s
+
+if silent:
+ sys.stdout = open("/dev/null", "w")
+
+import re
+from math import fabs
+
+def floatcmp(f1, f2):
+ if abstol:
+ if fabs(f1 - f2) > abstol:
+ return False
+ if reltol:
+ if f2 != 0. and fabs((f1 - f2) / f2) > reltol:
+ return False
+ return True
+
+class Parsed(list):
+ def __eq__(self, other):
+ if len(self) != len(other):
+ return False
+ comps = zip(self, other)
+ for comp in comps:
+ if all(isinstance(compElt, str) for compElt in comp):
+ if comp[0] != comp[1]:
+ return False
+ elif all (isinstance(compElt, float) for compElt in comp):
+ if not floatcmp(comp[0], comp[1]):
+ return False
+ else:
+ return False
+ return True
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+parseLine = re.compile(r"\s*(\S+)")
+def readline(line):
+ words = parseLine.findall(line)
+ parsed = Parsed([])
+ for word in words:
+ try:
+ parse = float(word)
+ parsed.append(parse)
+ except ValueError:
+ parsed.append(word)
+ return parsed
+
+def readfile(filename):
+ L = []
+ try:
+ with open(filename) as f:
+ for line in f:
+ L.append(readline(line))
+ except IOError:
+ print "Unable to read {}".format(filename)
+ sys.exit(2)
+ return L
+
+L1 = readfile(args.file1)
+L2 = readfile(args.file2)
+
+if len(L1) != len(L2):
+ print "The files have different amount of lines"
+ print "\t{}: {} lines".format(args.file1, len(L1))
+ print "\t{}: {} lines".format(args.file2, len(L2))
+ sys.exit(1)
+
+cmpL = zip(L1, L2)
+for i, cmpElt in enumerate(cmpL):
+ if cmpElt[0] != cmpElt[1]:
+ print "The files differ at line {}".format(i)
+ print "\t{}: {}".format(args.file1, cmpElt[0])
+ print "\t{}: {}".format(args.file2, cmpElt[1])
+ sys.exit(1)
+
+print "Comparison succeeded"
+sys.exit(0)
diff --git a/test/kvx/instr/framework.h b/test/kvx/instr/framework.h
new file mode 100644
index 00000000..3bbfa271
--- /dev/null
+++ b/test/kvx/instr/framework.h
@@ -0,0 +1,66 @@
+#ifndef __FRAMEWORK_H__
+#define __FRAMEWORK_H__
+
+#include <stdio.h>
+#include "../prng/prng.c"
+
+#define BEGIN_TEST_N(type, N)\
+ int main(void){\
+ type t[N], c, i, j, S;\
+ srand(0);\
+ S = 0;\
+ for (i = 0 ; i < 100 ; i++){\
+ c = randlong();\
+ for (j = 0 ; j < N ; j++)\
+ t[j] = randlong();\
+ /* END BEGIN_TEST_N */
+
+#define BEGIN_TEST(type)\
+ int main(void){\
+ type a, b, c, S;\
+ int i;\
+ srand(0);\
+ S = 0;\
+ for (i = 0 ; i < 100 ; i++){\
+ c = randlong();\
+ a = randlong();\
+ b = randlong();
+ /* END BEGIN_TEST */
+
+/* In between BEGIN_TEST and END_TEST : definition of c */
+
+#define END_TEST64()\
+ printf("%llu\t%llu\t%llu\n", a, b, c);\
+ S += c;\
+ }\
+ return S;\
+ }
+ /* END END_TEST64 */
+
+#define END_TEST32()\
+ printf("%u\t%u\t%u\n", a, b, c);\
+ S += c;\
+ }\
+ return S;\
+ }
+ /* END END_TEST32 */
+
+#define END_TESTF32()\
+ printf("%e\t%e\t%e\n", a, b, c);\
+ S += c;\
+ }\
+ return 0;\
+ }
+ /* END END_TESTF32 */
+
+#define END_TESTF64()\
+ printf("%e\t%e\t%e\n", a, b, c);\
+ S += c;\
+ }\
+ return 0;\
+ }
+ /* END END_TESTF64 */
+
+#endif
+
+
diff --git a/test/kvx/instr/i32.c b/test/kvx/instr/i32.c
new file mode 100644
index 00000000..e350931c
--- /dev/null
+++ b/test/kvx/instr/i32.c
@@ -0,0 +1,149 @@
+#include "framework.h"
+
+int sum(int a, int b){
+ return a+b;
+}
+
+int make(int a){
+ return a;
+}
+
+int tailsum(int a, int b){
+ return make(a+b);
+}
+
+int fact(int a){
+ int r = 1;
+ int i;
+ for (i = 1; i < a; i++)
+ r *= i;
+ return r;
+}
+
+float int2float(int v){
+ return v;
+}
+
+BEGIN_TEST(int)
+ c = a+b;
+ c += a&b;
+
+ /* testing if, cb version */
+ if ((a & 0x1) == 1)
+ c += fact(1);
+ else
+ c += fact(2);
+
+ if (a & 0x1 == 0)
+ c += fact(4);
+ else
+ c += fact(8);
+
+ if (a & 0x1 == 0)
+ c += fact(4);
+ else
+ c += fact(8);
+
+ b = !(a & 0x01);
+ if (!b)
+ c += fact(16);
+ else
+ c += fact(32);
+
+ c += sum(make(a), make(b));
+ c += (long long) a;
+
+ if (0 > (a & 0x1) - 1)
+ c += fact(64);
+ else
+ c += fact(128);
+
+ if (0 >= (a & 0x1))
+ c += fact(256);
+ else
+ c += fact(512);
+
+ if ((a & 0x1) > 0)
+ c += fact(1024);
+ else
+ c += fact(2048);
+
+ if ((a & 0x1) - 1 >= 0)
+ c += fact(4096);
+ else
+ c += fact(8192);
+
+ /* cmoved version */
+ if ((a & 0x1) == 1)
+ c += 1;
+ else
+ c += 2;
+
+ if (a & 0x1 == 0)
+ c += 4;
+ else
+ c += 8;
+
+ if (a & 0x1 == 0)
+ c += 4;
+ else
+ c += 8;
+
+ b = !(a & 0x01);
+ if (!b)
+ c += 16;
+ else
+ c += 32;
+
+ if (0 > (a & 0x1) - 1)
+ c += 64;
+ else
+ c += 128;
+
+ if (0 >= (a & 0x1))
+ c += 256;
+ else
+ c += 512;
+
+ if ((a & 0x1) > 0)
+ c += 1024;
+ else
+ c += 2048;
+
+ if ((a & 0x1) - 1 >= 0)
+ c += 4096;
+ else
+ c += 8192;
+
+ c += ((a & 0x1) == (b & 0x1));
+ c += (a > b);
+ c += (a <= b);
+ c += (a < b);
+ c += (a + b) / 2;
+ c += (int) int2float(a) + (int) int2float(b) + (int) int2float(42.3);
+ c += (a << 4); // addx16w
+ c += (a << 3); // addx8w
+ c += (a << 2); // addx4w
+ c += (a << 1); // addx2w
+
+ c += ~a & b; // andnw
+
+ int j;
+ for (j = 0 ; j < 10 ; j++)
+ c += a;
+ int k;
+ for (k = 0 ; k < (b & 0x8) ; k++)
+ c += a;
+
+ char s[] = "Tome and Cherry at the playa\n";
+ c += s[(a & (sizeof(s)-1))];
+
+ unsigned char s2[] = "Tim is sorry at the playa\n";
+ c += s2[a & (sizeof(s) - 1)];
+
+ c += a*b;
+ c += a-b;
+ c += a << (b & 0x8);
+
+ c += sum(a, b);
+END_TEST32()
diff --git a/test/kvx/instr/i64.c b/test/kvx/instr/i64.c
new file mode 100644
index 00000000..e869d93c
--- /dev/null
+++ b/test/kvx/instr/i64.c
@@ -0,0 +1,169 @@
+#include "framework.h"
+
+long long sum(long long a, long long b){
+ return a+b;
+}
+
+long long diff(long long a, long long b){
+ return a-b;
+}
+
+long long mul(long long a, long long b){
+ return a*b;
+}
+
+long long make(long long a){
+ return a;
+}
+
+long long random_op(long long a, long long b){
+ long long d = 3;
+ long long (*op)(long long, long long);
+
+ if (a % d == 0)
+ op = sum;
+ else if (a % d == 1)
+ op = diff;
+ else
+ op = mul;
+
+ return op(a, b);
+}
+
+long fact(long a){
+ long r = 1;
+ long i;
+ for (i = 1; i < a; i++)
+ r *= i;
+ return r;
+}
+
+double long2double(long v){
+ return v;
+}
+
+BEGIN_TEST(long long)
+ c = a&b;
+ c += a*b;
+ c += -a;
+ c += a | b;
+ c += a-b;
+ c += a >> (b & 0x8LL);
+ c += a >> (b & 0x8ULL);
+ c += a % b;
+ c += (a << 4); // addx16d
+ c += (a << 3); // addx8d
+ c += (a << 2); // addx4d
+ c += (a << 1); // addx2d
+
+ c += ~a & b; // andnd
+
+ long long d = 3;
+ long long (*op)(long long, long long);
+
+ if (a % d == 0)
+ op = sum;
+ else if (a % d == 1)
+ op = diff;
+ else
+ op = mul;
+
+ c += op(make(a), make(b));
+ c += random_op(a, b);
+ c += a/b;
+ c += a^b;
+ c += (unsigned int) a;
+
+ /* Testing if, cb */
+ if (0 != (a & 0x1LL))
+ c += fact(1);
+ else
+ c += fact(2);
+
+ if (0 > (a & 0x1LL))
+ c += fact(4);
+ else
+ c += fact(8);
+
+ if (0 >= (a & 0x1LL) - 1)
+ c += fact(16);
+ else
+ c += fact(32);
+
+ if ((unsigned long long)(a & 0x1LL) >= 1)
+ c += fact(18);
+ else
+ c += fact(31);
+
+
+ if (a-41414141 > 0)
+ c += fact(13);
+ else
+ c += fact(31);
+
+ if (a & 0x1LL > 0)
+ c += fact(64);
+ else
+ c += fact(128);
+
+ if ((a & 0x1LL) - 1 >= 0)
+ c += fact(256);
+ else
+ c += fact(512);
+
+ if (0 == (a & 0x1LL))
+ c += fact(1024);
+ else
+ c += fact(2048);
+
+ /* Testing if, cmoved */
+ if (0 != (a & 0x1LL))
+ c += 1;
+ else
+ c += 2;
+
+ if (0 > (a & 0x1LL))
+ c += 4;
+ else
+ c += 8;
+
+ if (0 >= (a & 0x1LL) - 1)
+ c += 16;
+ else
+ c += 32;
+
+ if (a-41414141 > 0)
+ c += 13;
+ else
+ c += 31;
+
+ if (a & 0x1LL > 0)
+ c += 64;
+ else
+ c += 128;
+
+ if ((a & 0x1LL) - 1 >= 0)
+ c += 256;
+ else
+ c += 512;
+
+ if (0 == (a & 0x1LL))
+ c += 1024;
+ else
+ c += 2048;
+
+ c += ((a & 0x1LL) == (b & 0x1LL));
+ c += (a >= b);
+ c += (a > b);
+ c += (a <= b);
+ c += (a < b);
+ c += (long) long2double(a) + (long) long2double(b) + (long) long2double(42.3);
+
+ int j;
+
+ for (j = 0 ; j < (b & 0x8LL) ; j++)
+ c += a;
+
+ c += ((a & 0x1LL) == (b & 0x1LL));
+
+END_TEST64()
diff --git a/test/kvx/instr/individual/andw.c b/test/kvx/instr/individual/andw.c
new file mode 100644
index 00000000..799dc7fb
--- /dev/null
+++ b/test/kvx/instr/individual/andw.c
@@ -0,0 +1,5 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+ c = a&b;
+END_TEST32()
diff --git a/test/kvx/instr/individual/branch.c b/test/kvx/instr/individual/branch.c
new file mode 100644
index 00000000..c9937e31
--- /dev/null
+++ b/test/kvx/instr/individual/branch.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if ((a & 0x1) == 1)
+ c = 0;
+ else
+ c = 1;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/branchz.c b/test/kvx/instr/individual/branchz.c
new file mode 100644
index 00000000..d3e021b5
--- /dev/null
+++ b/test/kvx/instr/individual/branchz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if (a & 0x1 == 0)
+ c = 0;
+ else
+ c = 1;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/branchzu.c b/test/kvx/instr/individual/branchzu.c
new file mode 100644
index 00000000..d0169174
--- /dev/null
+++ b/test/kvx/instr/individual/branchzu.c
@@ -0,0 +1,11 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ b = !(a & 0x01);
+ if (!b)
+ c = 0;
+ else
+ c = 1;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/call.c b/test/kvx/instr/individual/call.c
new file mode 100644
index 00000000..ba2ec323
--- /dev/null
+++ b/test/kvx/instr/individual/call.c
@@ -0,0 +1,16 @@
+#include "framework.h"
+
+int sum(int a, int b){
+ return a+b;
+}
+
+int make(int a){
+ return a;
+}
+
+BEGIN_TEST(int)
+{
+ c = sum(make(a), make(b));
+}
+END_TEST32()
+/* RETURN VALUE: 60 */
diff --git a/test/kvx/instr/individual/cast_S32_S64.c b/test/kvx/instr/individual/cast_S32_S64.c
new file mode 100644
index 00000000..09c97e00
--- /dev/null
+++ b/test/kvx/instr/individual/cast_S32_S64.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = (long long) a;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/cast_S64_U32.c b/test/kvx/instr/individual/cast_S64_U32.c
new file mode 100644
index 00000000..2d9dc723
--- /dev/null
+++ b/test/kvx/instr/individual/cast_S64_U32.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = (unsigned int) a;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.deqz.c b/test/kvx/instr/individual/cb.deqz.c
new file mode 100644
index 00000000..6da2ab07
--- /dev/null
+++ b/test/kvx/instr/individual/cb.deqz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if (0 != (a & 0x1LL))
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.dgez.c b/test/kvx/instr/individual/cb.dgez.c
new file mode 100644
index 00000000..7bef25ad
--- /dev/null
+++ b/test/kvx/instr/individual/cb.dgez.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if (0 > (a & 0x1LL))
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.dgtz.c b/test/kvx/instr/individual/cb.dgtz.c
new file mode 100644
index 00000000..1a43fb1f
--- /dev/null
+++ b/test/kvx/instr/individual/cb.dgtz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if (0 >= (a & 0x1LL) - 1)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.dlez.c b/test/kvx/instr/individual/cb.dlez.c
new file mode 100644
index 00000000..2fb97939
--- /dev/null
+++ b/test/kvx/instr/individual/cb.dlez.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if (a & 0x1LL > 0)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.dltz.c b/test/kvx/instr/individual/cb.dltz.c
new file mode 100644
index 00000000..a431d5d0
--- /dev/null
+++ b/test/kvx/instr/individual/cb.dltz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if ((a & 0x1LL) - 1 >= 0)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.dnez.c b/test/kvx/instr/individual/cb.dnez.c
new file mode 100644
index 00000000..44516cbe
--- /dev/null
+++ b/test/kvx/instr/individual/cb.dnez.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ if (0 == (a & 0x1LL))
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/cb.wgez.c b/test/kvx/instr/individual/cb.wgez.c
new file mode 100644
index 00000000..5779ad92
--- /dev/null
+++ b/test/kvx/instr/individual/cb.wgez.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if (0 > (a & 0x1) - 1)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/cb.wgtz.c b/test/kvx/instr/individual/cb.wgtz.c
new file mode 100644
index 00000000..abb695bd
--- /dev/null
+++ b/test/kvx/instr/individual/cb.wgtz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if (0 >= (a & 0x1))
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/cb.wlez.c b/test/kvx/instr/individual/cb.wlez.c
new file mode 100644
index 00000000..3a2e08c1
--- /dev/null
+++ b/test/kvx/instr/individual/cb.wlez.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if ((a & 0x1) > 0)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/cb.wltz.c b/test/kvx/instr/individual/cb.wltz.c
new file mode 100644
index 00000000..5d52c72a
--- /dev/null
+++ b/test/kvx/instr/individual/cb.wltz.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ if ((a & 0x1) - 1 >= 0)
+ c = 1;
+ else
+ c = 0;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compd.eq.c b/test/kvx/instr/individual/compd.eq.c
new file mode 100644
index 00000000..4fe8de2a
--- /dev/null
+++ b/test/kvx/instr/individual/compd.eq.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = ((a & 0x1LL) == (b & 0x1LL));
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.geu.c b/test/kvx/instr/individual/compd.geu.c
new file mode 100644
index 00000000..fccf0804
--- /dev/null
+++ b/test/kvx/instr/individual/compd.geu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = (a >= b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.gt.c b/test/kvx/instr/individual/compd.gt.c
new file mode 100644
index 00000000..b9901436
--- /dev/null
+++ b/test/kvx/instr/individual/compd.gt.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = (a > b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.le.c b/test/kvx/instr/individual/compd.le.c
new file mode 100644
index 00000000..6fa0f103
--- /dev/null
+++ b/test/kvx/instr/individual/compd.le.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = (a <= b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.leu.c b/test/kvx/instr/individual/compd.leu.c
new file mode 100644
index 00000000..1ad18281
--- /dev/null
+++ b/test/kvx/instr/individual/compd.leu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = (a <= b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.lt.c b/test/kvx/instr/individual/compd.lt.c
new file mode 100644
index 00000000..c42cda56
--- /dev/null
+++ b/test/kvx/instr/individual/compd.lt.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = (a < b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.ltu.c b/test/kvx/instr/individual/compd.ltu.c
new file mode 100644
index 00000000..b03d4d53
--- /dev/null
+++ b/test/kvx/instr/individual/compd.ltu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = (a < b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compd.ne.c b/test/kvx/instr/individual/compd.ne.c
new file mode 100644
index 00000000..fd9d0b28
--- /dev/null
+++ b/test/kvx/instr/individual/compd.ne.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = ((a & 0x1ULL) != (b & 0x1ULL));
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/compw.eq.c b/test/kvx/instr/individual/compw.eq.c
new file mode 100644
index 00000000..cd93f365
--- /dev/null
+++ b/test/kvx/instr/individual/compw.eq.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = ((a & 0x1) == (b & 0x1));
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.geu.c b/test/kvx/instr/individual/compw.geu.c
new file mode 100644
index 00000000..b8fb1adf
--- /dev/null
+++ b/test/kvx/instr/individual/compw.geu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = (a >= b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.gt.c b/test/kvx/instr/individual/compw.gt.c
new file mode 100644
index 00000000..5f6bc907
--- /dev/null
+++ b/test/kvx/instr/individual/compw.gt.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = (a > b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.gtu.c b/test/kvx/instr/individual/compw.gtu.c
new file mode 100644
index 00000000..947f6a14
--- /dev/null
+++ b/test/kvx/instr/individual/compw.gtu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = (a > b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.le.c b/test/kvx/instr/individual/compw.le.c
new file mode 100644
index 00000000..35ec6b7d
--- /dev/null
+++ b/test/kvx/instr/individual/compw.le.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = (a <= b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.leu.c b/test/kvx/instr/individual/compw.leu.c
new file mode 100644
index 00000000..74ebfb42
--- /dev/null
+++ b/test/kvx/instr/individual/compw.leu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = (a <= b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.lt.c b/test/kvx/instr/individual/compw.lt.c
new file mode 100644
index 00000000..cb1f30bd
--- /dev/null
+++ b/test/kvx/instr/individual/compw.lt.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = (a < b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.ltu.c b/test/kvx/instr/individual/compw.ltu.c
new file mode 100644
index 00000000..6a0c5af1
--- /dev/null
+++ b/test/kvx/instr/individual/compw.ltu.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = (a < b);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/compw.ne.c b/test/kvx/instr/individual/compw.ne.c
new file mode 100644
index 00000000..7035e2c7
--- /dev/null
+++ b/test/kvx/instr/individual/compw.ne.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = ((a & 0x1U) != (b & 0x1U));
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/div2.c b/test/kvx/instr/individual/div2.c
new file mode 100644
index 00000000..b5dfe63a
--- /dev/null
+++ b/test/kvx/instr/individual/div2.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = (a + b) / 2;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/doubleconv.c b/test/kvx/instr/individual/doubleconv.c
new file mode 100644
index 00000000..55b1ddab
--- /dev/null
+++ b/test/kvx/instr/individual/doubleconv.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+double long2double(long v){
+ return v;
+}
+
+BEGIN_TEST(long)
+ c = (long) long2double(a) + (long) long2double(b) + (long) long2double(42.3);
+END_TEST64()
diff --git a/test/kvx/instr/individual/floatconv.c b/test/kvx/instr/individual/floatconv.c
new file mode 100644
index 00000000..32b798e1
--- /dev/null
+++ b/test/kvx/instr/individual/floatconv.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+float int2float(int v){
+ return v;
+}
+
+BEGIN_TEST(int)
+ c = (int) int2float(a) + (int) int2float(b) + (int) int2float(42.3);
+END_TEST32()
diff --git a/test/kvx/instr/individual/fmuld.c b/test/kvx/instr/individual/fmuld.c
new file mode 100644
index 00000000..03c990fa
--- /dev/null
+++ b/test/kvx/instr/individual/fmuld.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(double)
+{
+ c = ((double)a * (double)b);
+}
+END_TESTF64()
diff --git a/test/kvx/instr/individual/fmulw.c b/test/kvx/instr/individual/fmulw.c
new file mode 100644
index 00000000..f85eba64
--- /dev/null
+++ b/test/kvx/instr/individual/fmulw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(float)
+{
+ c = ((float)a * (float)b);
+}
+END_TESTF32()
diff --git a/test/kvx/instr/individual/fnegd.c b/test/kvx/instr/individual/fnegd.c
new file mode 100644
index 00000000..974eb7e8
--- /dev/null
+++ b/test/kvx/instr/individual/fnegd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(double)
+{
+ c = (-(double)a);
+}
+END_TESTF64()
diff --git a/test/kvx/instr/individual/fnegw.c b/test/kvx/instr/individual/fnegw.c
new file mode 100644
index 00000000..fbeaab8e
--- /dev/null
+++ b/test/kvx/instr/individual/fnegw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(float)
+{
+ c = (-(float)a);
+}
+END_TESTF64()
diff --git a/test/kvx/instr/individual/for.c b/test/kvx/instr/individual/for.c
new file mode 100644
index 00000000..373ab6bd
--- /dev/null
+++ b/test/kvx/instr/individual/for.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ int j;
+ for (j = 0 ; j < 10 ; j++)
+ c += a;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/forvar.c b/test/kvx/instr/individual/forvar.c
new file mode 100644
index 00000000..9e43c198
--- /dev/null
+++ b/test/kvx/instr/individual/forvar.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ int k;
+ for (k = 0 ; k < (b & 0x8) ; k++)
+ c += a;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/forvarl.c b/test/kvx/instr/individual/forvarl.c
new file mode 100644
index 00000000..c1fe90fd
--- /dev/null
+++ b/test/kvx/instr/individual/forvarl.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(long long int)
+{
+ int j;
+
+ for (j = 0 ; j < (b & 0x8LL) ; j++)
+ c += a;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/fsbfd.c b/test/kvx/instr/individual/fsbfd.c
new file mode 100644
index 00000000..f80c1efe
--- /dev/null
+++ b/test/kvx/instr/individual/fsbfd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(double)
+{
+ c = ((double)a - (double)b);
+}
+END_TESTF64()
diff --git a/test/kvx/instr/individual/fsbfw.c b/test/kvx/instr/individual/fsbfw.c
new file mode 100644
index 00000000..067c40b5
--- /dev/null
+++ b/test/kvx/instr/individual/fsbfw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(float)
+{
+ c = ((float)a - (float)b);
+}
+END_TESTF64()
diff --git a/test/kvx/instr/individual/indirect_call.c b/test/kvx/instr/individual/indirect_call.c
new file mode 100644
index 00000000..f376c00a
--- /dev/null
+++ b/test/kvx/instr/individual/indirect_call.c
@@ -0,0 +1,33 @@
+#include "framework.h"
+
+long long sum(long long a, long long b){
+ return a+b;
+}
+
+long long diff(long long a, long long b){
+ return a-b;
+}
+
+long long mul(long long a, long long b){
+ return a*b;
+}
+
+long long make(long long a){
+ return a;
+}
+
+BEGIN_TEST(long long)
+{
+ long long d = 3;
+ long long (*op)(long long, long long);
+
+ if (a % d == 0)
+ op = sum;
+ else if (a % d == 1)
+ op = diff;
+ else
+ op = mul;
+
+ c += op(make(a), make(b));
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/indirect_tailcall.c b/test/kvx/instr/individual/indirect_tailcall.c
new file mode 100644
index 00000000..e6c16ea1
--- /dev/null
+++ b/test/kvx/instr/individual/indirect_tailcall.c
@@ -0,0 +1,33 @@
+#include "framework.h"
+
+long long sum(long long a, long long b){
+ return a+b;
+}
+
+long long diff(long long a, long long b){
+ return a-b;
+}
+
+long long mul(long long a, long long b){
+ return a*b;
+}
+
+long long random_op(long long a, long long b){
+ long long d = 3;
+ long long (*op)(long long, long long);
+
+ if (a % d == 0)
+ op = sum;
+ else if (a % d == 1)
+ op = diff;
+ else
+ op = mul;
+
+ return op(a, b);
+}
+
+BEGIN_TEST(long long)
+{
+ c += random_op(a, b);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/lbs.c b/test/kvx/instr/individual/lbs.c
new file mode 100644
index 00000000..22a50632
--- /dev/null
+++ b/test/kvx/instr/individual/lbs.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ char s[] = "Tome and Cherry at the playa\n";
+
+ c = s[(a & (sizeof(s)-1))];
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/lbz.c b/test/kvx/instr/individual/lbz.c
new file mode 100644
index 00000000..04ba098d
--- /dev/null
+++ b/test/kvx/instr/individual/lbz.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ unsigned char s[] = "Tim is sorry at the playa\n";
+
+ c = s[a & (sizeof(s) - 1)];
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/muld.c b/test/kvx/instr/individual/muld.c
new file mode 100644
index 00000000..f7e23850
--- /dev/null
+++ b/test/kvx/instr/individual/muld.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = a*b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/mulw.c b/test/kvx/instr/individual/mulw.c
new file mode 100644
index 00000000..a91d966e
--- /dev/null
+++ b/test/kvx/instr/individual/mulw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = a * b;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/negd.c b/test/kvx/instr/individual/negd.c
new file mode 100644
index 00000000..837b9828
--- /dev/null
+++ b/test/kvx/instr/individual/negd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = -a;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/ord.c b/test/kvx/instr/individual/ord.c
new file mode 100644
index 00000000..cae1ae8b
--- /dev/null
+++ b/test/kvx/instr/individual/ord.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = a | b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/sbfd.c b/test/kvx/instr/individual/sbfd.c
new file mode 100644
index 00000000..77c28c77
--- /dev/null
+++ b/test/kvx/instr/individual/sbfd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = a-b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/sbfw.c b/test/kvx/instr/individual/sbfw.c
new file mode 100644
index 00000000..e38a1fff
--- /dev/null
+++ b/test/kvx/instr/individual/sbfw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = a-b;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/simple.c b/test/kvx/instr/individual/simple.c
new file mode 100644
index 00000000..944f09c9
--- /dev/null
+++ b/test/kvx/instr/individual/simple.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = a+b;
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/sllw.c b/test/kvx/instr/individual/sllw.c
new file mode 100644
index 00000000..6dd41a6c
--- /dev/null
+++ b/test/kvx/instr/individual/sllw.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+{
+ c = a << (b & 0x8);
+}
+END_TEST32()
diff --git a/test/kvx/instr/individual/srad.c b/test/kvx/instr/individual/srad.c
new file mode 100644
index 00000000..00be9d0c
--- /dev/null
+++ b/test/kvx/instr/individual/srad.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = a >> (b & 0x8LL);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/srld.c b/test/kvx/instr/individual/srld.c
new file mode 100644
index 00000000..14970efd
--- /dev/null
+++ b/test/kvx/instr/individual/srld.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = a >> (b & 0x8ULL);
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/tailcall.c b/test/kvx/instr/individual/tailcall.c
new file mode 100644
index 00000000..6c659a01
--- /dev/null
+++ b/test/kvx/instr/individual/tailcall.c
@@ -0,0 +1,16 @@
+#include "framework.h"
+
+int make(int a){
+ return a;
+}
+
+int sum(int a, int b){
+ return make(a+b);
+}
+
+BEGIN_TEST(int)
+{
+ c = sum(a, b);
+}
+END_TEST32()
+/* RETURN VALUE: 60 */
diff --git a/test/kvx/instr/individual/udivd.c b/test/kvx/instr/individual/udivd.c
new file mode 100644
index 00000000..cfb31881
--- /dev/null
+++ b/test/kvx/instr/individual/udivd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = a/b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/umodd.c b/test/kvx/instr/individual/umodd.c
new file mode 100644
index 00000000..a7f25f1c
--- /dev/null
+++ b/test/kvx/instr/individual/umodd.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = a%b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/individual/xord.c b/test/kvx/instr/individual/xord.c
new file mode 100644
index 00000000..b6a90cb0
--- /dev/null
+++ b/test/kvx/instr/individual/xord.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(long long)
+{
+ c = a^b;
+}
+END_TEST64()
diff --git a/test/kvx/instr/modi32.c b/test/kvx/instr/modi32.c
new file mode 100644
index 00000000..958ae920
--- /dev/null
+++ b/test/kvx/instr/modi32.c
@@ -0,0 +1,5 @@
+#include "framework.h"
+
+BEGIN_TEST(int)
+ c = a%b;
+END_TEST32()
diff --git a/test/kvx/instr/modui32.c b/test/kvx/instr/modui32.c
new file mode 100644
index 00000000..a39034a8
--- /dev/null
+++ b/test/kvx/instr/modui32.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = a%b;
+}
+END_TEST32()
diff --git a/test/kvx/instr/ui32.c b/test/kvx/instr/ui32.c
new file mode 100644
index 00000000..f56a9b95
--- /dev/null
+++ b/test/kvx/instr/ui32.c
@@ -0,0 +1,12 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned int)
+{
+ c = (long long) a;
+ c += (a >= b);
+ c += (a > b);
+ c += (a <= b);
+ c += (a < b);
+ c += ((a & 0x1U) != (b & 0x1U));
+}
+END_TEST32()
diff --git a/test/kvx/instr/ui64.c b/test/kvx/instr/ui64.c
new file mode 100644
index 00000000..908dec3c
--- /dev/null
+++ b/test/kvx/instr/ui64.c
@@ -0,0 +1,10 @@
+#include "framework.h"
+
+BEGIN_TEST(unsigned long long)
+{
+ c = (a > b);
+ c += (a <= b);
+ c += (a < b);
+ c += ((a & 0x1ULL) != (b & 0x1ULL));
+}
+END_TEST64()
diff --git a/test/kvx/interop/.gitignore b/test/kvx/interop/.gitignore
new file mode 100644
index 00000000..ea1472ec
--- /dev/null
+++ b/test/kvx/interop/.gitignore
@@ -0,0 +1 @@
+output/
diff --git a/test/kvx/interop/Makefile b/test/kvx/interop/Makefile
new file mode 100644
index 00000000..aa018aac
--- /dev/null
+++ b/test/kvx/interop/Makefile
@@ -0,0 +1,365 @@
+SHELL := /bin/bash
+
+KVXC ?= kvx-elf-gcc
+CC ?= gcc
+CCOMP ?= ccomp
+CFLAGS ?= -O2 -Wno-varargs
+SIMU ?= kvx-mppa
+TIMEOUT ?= --signal=SIGTERM 120s
+HARDRUN ?= kvx-jtag-runner
+
+DIR=./
+SRCDIR=$(DIR)
+OUTDIR=$(DIR)/out
+BINDIR=$(DIR)/bin
+ASMDIR=$(DIR)/asm
+OBJDIR=$(DIR)/obj
+COMMON=common
+VAARG_COMMON=vaarg_common
+
+##
+# Intended flow : .c -> .gcc.s -> .gcc.o -> .gcc.bin -> .gcc.out
+# -> .ccomp.s -> .ccomp.o -> .ccomp.bin -> .ccomp.out
+# -> .x86-gcc.s -> .x86-gcc.o -> .x86-gcc.bin -> .x86-gcc.out
+#
+# The .o -> .bin part uses $(COMMON).gcc.o or $(COMMON).x86-gcc.o depending on the architecture
+# There is also a $(VAARG_COMMON) that is the same than $(COMMON) but with va_arg
+##
+
+KVXCPATH=$(shell which $(KVXC))
+CCPATH=$(shell which $(CC))
+CCOMPPATH=$(shell which $(CCOMP))
+SIMUPATH=$(shell which $(SIMU))
+
+TESTNAMES ?= $(filter-out $(VAARG_COMMON),$(filter-out $(COMMON),$(notdir $(subst .c,,$(wildcard $(DIR)/*.c)))))
+
+X86_GCC_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .x86-gcc.out,$(TESTNAMES)))
+GCC_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.simu.out,$(TESTNAMES)))
+GCC_REV_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.rev.simu.out,$(TESTNAMES)))
+CCOMP_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.simu.out,$(TESTNAMES)))
+
+GCC_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.hard.out,$(TESTNAMES)))
+GCC_REV_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.rev.hard.out,$(TESTNAMES)))
+CCOMP_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.hard.out,$(TESTNAMES)))
+
+VAARG_X86_GCC_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .x86-gcc.vaarg.out,$(TESTNAMES)))
+VAARG_GCC_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.vaarg.simu.out,$(TESTNAMES)))
+VAARG_GCC_REV_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.rev.vaarg.simu.out,$(TESTNAMES)))
+VAARG_CCOMP_SIMUOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.vaarg.simu.out,$(TESTNAMES)))
+
+VAARG_GCC_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.vaarg.hard.out,$(TESTNAMES)))
+VAARG_GCC_REV_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.rev.vaarg.hard.out,$(TESTNAMES)))
+VAARG_CCOMP_HARDOUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.vaarg.hard.out,$(TESTNAMES)))
+
+BIN=$(addprefix $(BINDIR)/,$(addsuffix .x86-gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .ccomp.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.rev.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .x86-gcc.vaarg.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.vaarg.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .ccomp.vaarg.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.rev.vaarg.bin,$(TESTNAMES)))
+
+##
+# Targets
+##
+
+all: $(BIN)
+
+GREEN=\033[0;32m
+RED=\033[0;31m
+NC=\033[0m
+
+.PHONY:
+test: simutest
+
+.PHONY:
+simutest: $(X86_GCC_OUT) $(GCC_SIMUOUT) $(VAARG_X86_GCC_OUT) $(VAARG_GCC_SIMUOUT)
+ @echo "Comparing x86 gcc output to k1 gcc.."
+ @for test in $(TESTNAMES); do\
+ x86out=$(OUTDIR)/$$test.x86-gcc.out;\
+ gccout=$(OUTDIR)/$$test.gcc.simu.out;\
+ vaarg_x86out=$(OUTDIR)/$$test.x86-gcc.vaarg.out;\
+ vaarg_gccout=$(OUTDIR)/$$test.gcc.vaarg.simu.out;\
+ if ! diff $$x86out $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$x86out and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$x86out and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_x86out $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_x86out and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_x86out and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+check: simucheck
+
+.PHONY:
+simucheck: $(GCC_SIMUOUT) $(CCOMP_SIMUOUT) $(GCC_REV_SIMUOUT) $(VAARG_GCC_SIMUOUT) $(VAARG_CCOMP_SIMUOUT) $(VAARG_GCC_REV_SIMUOUT)
+ @echo "Comparing k1 gcc output to ccomp.."
+ @for test in $(TESTNAMES); do\
+ gccout=$(OUTDIR)/$$test.gcc.simu.out;\
+ ccompout=$(OUTDIR)/$$test.ccomp.simu.out;\
+ gccrevout=$(OUTDIR)/$$test.gcc.rev.simu.out;\
+ vaarg_gccout=$(OUTDIR)/$$test.gcc.vaarg.simu.out;\
+ vaarg_ccompout=$(OUTDIR)/$$test.ccomp.vaarg.simu.out;\
+ vaarg_gccrevout=$(OUTDIR)/$$test.gcc.rev.vaarg.simu.out;\
+ if ! diff $$ccompout $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$ccompout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$ccompout and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$gccrevout $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$gccrevout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$gccrevout and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_ccompout $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_ccompout and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_ccompout and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_gccrevout $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_gccrevout and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_gccrevout and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+hardtest: $(X86_GCC_OUT) $(GCC_HARDOUT) $(VAARG_X86_GCC_OUT) $(VAARG_GCC_HARDOUT)
+ @echo "Comparing x86 gcc output to k1 gcc.."
+ @for test in $(TESTNAMES); do\
+ x86out=$(OUTDIR)/$$test.x86-gcc.out;\
+ gccout=$(OUTDIR)/$$test.gcc.hard.out;\
+ vaarg_x86out=$(OUTDIR)/$$test.x86-gcc.vaarg.out;\
+ vaarg_gccout=$(OUTDIR)/$$test.gcc.vaarg.hard.out;\
+ if ! diff $$x86out $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$x86out and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$x86out and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_x86out $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_x86out and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_x86out and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ done
+
+.PHONY:
+hardcheck: $(GCC_HARDOUT) $(CCOMP_HARDOUT) $(GCC_REV_HARDOUT) $(VAARG_GCC_HARDOUT) $(VAARG_CCOMP_HARDOUT) $(VAARG_GCC_REV_HARDOUT)
+ @echo "Comparing k1 gcc output to ccomp.."
+ @for test in $(TESTNAMES); do\
+ gccout=$(OUTDIR)/$$test.gcc.hard.out;\
+ ccompout=$(OUTDIR)/$$test.ccomp.hard.out;\
+ gccrevout=$(OUTDIR)/$$test.gcc.rev.hard.out;\
+ vaarg_gccout=$(OUTDIR)/$$test.gcc.vaarg.hard.out;\
+ vaarg_ccompout=$(OUTDIR)/$$test.ccomp.vaarg.hard.out;\
+ vaarg_gccrevout=$(OUTDIR)/$$test.gcc.rev.vaarg.hard.out;\
+ if ! diff $$ccompout $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$ccompout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$ccompout and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$gccrevout $$gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$gccrevout and $$gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$gccrevout and $$gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_ccompout $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_ccompout and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_ccompout and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ if ! diff $$vaarg_gccrevout $$vaarg_gccout > /dev/null; then\
+ >&2 printf "$(RED)ERROR: $$vaarg_gccrevout and $$vaarg_gccout differ$(NC)\n";\
+ else\
+ printf "$(GREEN)GOOD: $$vaarg_gccrevout and $$vaarg_gccout concur$(NC)\n";\
+ fi;\
+ done
+
+##
+# Rules
+##
+
+.SECONDARY:
+
+##
+# Generating output
+##
+
+## Version sans les timeout
+#$(OUTDIR)/%.x86-gcc.out: $(BINDIR)/%.x86-gcc.bin
+# @mkdir -p $(@D)
+# ./$< > $@; echo $$? >> $@
+#
+#$(OUTDIR)/%.gcc.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+# @mkdir -p $(@D)
+# $(SIMU) -- $< > $@ ; echo $$? >> $@
+#
+#$(OUTDIR)/%.ccomp.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+# @mkdir -p $(@D)
+# $(SIMU) -- $< > $@ ; echo $$? >> $@
+
+## No vaarg
+
+$(OUTDIR)/%.x86-gcc.out: $(BINDIR)/%.x86-gcc.bin
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.simu.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.rev.simu.out: $(BINDIR)/%.gcc.rev.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.simu.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.hard.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.rev.hard.out: $(BINDIR)/%.gcc.rev.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.hard.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+## With vaarg
+
+$(OUTDIR)/%.x86-gcc.vaarg.out: $(BINDIR)/%.x86-gcc.vaarg.bin
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.vaarg.simu.out: $(BINDIR)/%.gcc.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.rev.vaarg.simu.out: $(BINDIR)/%.gcc.rev.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.vaarg.simu.out: $(BINDIR)/%.ccomp.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.vaarg.hard.out: $(BINDIR)/%.gcc.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.rev.vaarg.hard.out: $(BINDIR)/%.gcc.rev.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.vaarg.hard.out: $(BINDIR)/%.ccomp.vaarg.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(HARDRUN) --exec-file=Cluster0:$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+##
+# Object to binary
+##
+
+## common
+
+$(BINDIR)/$(COMMON).x86-gcc.bin: $(OBJDIR)/$(COMMON).x86-gcc.o $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $< -o $@
+
+$(BINDIR)/$(COMMON).gcc.bin: $(OBJDIR)/$(COMMON).gcc.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $< -o $@
+
+$(BINDIR)/$(COMMON).ccomp.bin: $(OBJDIR)/$(COMMON).ccomp.o $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $< -o $@
+
+## vaarg_common
+
+$(BINDIR)/$(VAARG_COMMON).x86-gcc.bin: $(OBJDIR)/$(VAARG_COMMON).x86-gcc.o $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $< -o $@
+
+$(BINDIR)/$(VAARG_COMMON).gcc.bin: $(OBJDIR)/$(VAARG_COMMON).gcc.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $< -o $@
+
+$(BINDIR)/$(VAARG_COMMON).ccomp.bin: $(OBJDIR)/$(VAARG_COMMON).ccomp.o $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $< -o $@
+
+## no vaarg
+
+$(BINDIR)/%.x86-gcc.bin: $(OBJDIR)/%.x86-gcc.o $(OBJDIR)/$(COMMON).x86-gcc.o $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.gcc.bin: $(OBJDIR)/%.gcc.o $(OBJDIR)/$(COMMON).gcc.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.gcc.rev.bin: $(OBJDIR)/%.gcc.o $(OBJDIR)/$(COMMON).ccomp.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.ccomp.bin: $(OBJDIR)/%.ccomp.o $(OBJDIR)/$(COMMON).gcc.o $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+## with vaarg
+
+$(BINDIR)/%.x86-gcc.vaarg.bin: $(OBJDIR)/%.x86-gcc.o $(OBJDIR)/$(VAARG_COMMON).x86-gcc.o $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.gcc.vaarg.bin: $(OBJDIR)/%.gcc.o $(OBJDIR)/$(VAARG_COMMON).gcc.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.gcc.rev.vaarg.bin: $(OBJDIR)/%.gcc.o $(OBJDIR)/$(VAARG_COMMON).ccomp.o $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+$(BINDIR)/%.ccomp.vaarg.bin: $(OBJDIR)/%.ccomp.o $(OBJDIR)/$(VAARG_COMMON).gcc.o $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $(wordlist 1,2,$^) -o $@
+
+##
+# Assembly to object
+##
+
+$(OBJDIR)/%.x86-gcc.o: $(ASMDIR)/%.x86-gcc.s $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(OBJDIR)/%.gcc.o: $(ASMDIR)/%.gcc.s $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) -c $(CFLAGS) $< -o $@
+
+$(OBJDIR)/%.ccomp.o: $(ASMDIR)/%.ccomp.s $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) -c $(CFLAGS) $< -o $@
+
+
+##
+# Source to assembly
+##
+
+$(ASMDIR)/%.x86-gcc.s: $(SRCDIR)/%.c $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.gcc.s: $(SRCDIR)/%.c $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.ccomp.s: $(SRCDIR)/%.c $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) -S $< -o $@
diff --git a/test/kvx/interop/common.c b/test/kvx/interop/common.c
new file mode 100644
index 00000000..05b49187
--- /dev/null
+++ b/test/kvx/interop/common.c
@@ -0,0 +1,257 @@
+#define STACK int a[100];\
+ a[42] = 42;
+
+#define ONEARG_OP(arg) (3*magic(arg)+2)
+
+#define MULTIARG_OP(arg1, arg2, arg3, arg4) (arg1 ^ magic(arg2) << arg3 - arg4)
+
+#define MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,\
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,\
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29)\
+ (a0 * a1 * a2 * magic(a3) * a4 * a5 * a6 * a7 * a8 * a9 *\
+ a10 * a11 * a12 * a13 * a14 * a15 * a16 * a17 * a18 * a19 *\
+ a20 * a21 * a22 * a23 * a24 * a25 * a26 * a27 * a28 * a29)
+
+int magic(long a){
+ return a*42 + 26;
+}
+
+void void_void(){
+ STACK;
+}
+
+long long ll_void(){
+ STACK;
+ return 0xdeadbeefdeadbeefULL;
+}
+
+int i_oneiarg(int arg){
+ STACK;
+ return ONEARG_OP(arg);
+}
+
+int i_multiiargs(int arg1, char arg2, char arg3, int arg4){
+ STACK;
+ return MULTIARG_OP(arg1, arg2, arg3, arg4);
+}
+
+int i_manyiargs(char a0, int a1, char a2, int a3, char a4, char a5, int a6, int a7, char a8, int a9,
+ char a10, int a11, char a12, int a13, char a14, char a15, int a16, int a17, char a18, int a19,
+ char a20, int a21, char a22, int a23, char a24, char a25, int a26, int a27, char a28, int a29)
+{
+ STACK;
+ return MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29);
+}
+
+int ll_onellarg(long long arg){
+ STACK;
+ return ONEARG_OP(arg);
+}
+
+long long ll_multillargs(long long arg1, char arg2, char arg3, long long arg4){
+ STACK;
+ return MULTIARG_OP(arg1, arg2, arg3, arg4);
+}
+
+long long ll_manyllargs(char a0, int a1, char a2, long long a3, char a4, char a5, long long a6, long long a7, char a8, long long a9,
+ char a10, long long a11, char a12, int a13, char a14, char a15, long long a16, long long a17, char a18, long long a19,
+ char a20, int a21, char a22, long long a23, char a24, char a25, long long a26, int a27, char a28, long long a29)
+{
+ STACK;
+ return MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29);
+}
+
+double stackhell(char a0, int a1, float a2, long long a3, double a4, char a5, long long a6, long long a7, float a8, long long a9,
+ double a10, long long a11, char a12, int a13, float a14, double a15, long long a16, long long a17, float a18, long long a19,
+ char a20, int a21, char a22, long long a23, float a24, char a25, long long a26, int a27, double a28, long long a29)
+{
+ long long b0 = a0;
+ long long b1 = a1 * b0;
+ long long b2 = a2 * b1;
+ float b3 = a3 * b2;
+ int b4 = a4 * b3;
+ double b5 = a5 * b4;
+ int b6 = a6 * b5;
+ float b7 = a7 * b6;
+ char b8 = a8 * b7;
+ double b9 = a9 * b8;
+ char b10 = a10 * b9;
+ float b11 = a11 * b10;
+ char b12 = a12 * b11;
+ int b13 = a13 * b12;
+ long long b14 = a14 * b13;
+ long long b15 = a15 * b14;
+ long long b16 = a16 * b15;
+ long long b17 = a17 * b16;
+ long long b18 = a18 * b17;
+ long long b19 = a19 * b18;
+ long long b20 = a20 * b19;
+ long long b21 = a21 * b20;
+ long long b22 = a22 * b21;
+ long long b23 = a23 * b22;
+ long long b24 = a24 * b23;
+ long long b25 = a25 * b24;
+ long long b26 = a26 * b25;
+ long long b27 = a27 * b26;
+ int b28 = a28 * b27;
+ double b29 = a29 * b28;
+ float b30 = b0 * b29;
+ double b31 = b1 * b30;
+ int b32 = b2 * b31;
+ char b33 = b3 * b32;
+ float b34 = b4 * b33;
+ char b35 = b5 * b34;
+ double b36 = b6 * b35;
+ float b37 = b7 * b36;
+ int b38 = b8 * b37;
+ double b39 = b9 * b38;
+ float b40 = b0 * b39;
+ int b41 = b1 * b40;
+ double b42 = b2 * b41;
+ float b43 = b3 * b42;
+ int b44 = b4 * b43;
+ double b45 = b5 * b44;
+ int b46 = b6 * b45;
+ double b47 = b7 * b46;
+ int b48 = b8 * b47;
+ long long b49 = b9 * b48;
+ long long b50 = b0 * b49;
+ long long b51 = b1 * b50;
+ long long b52 = b2 * b51;
+ long long b53 = b3 * b52;
+ long long b54 = b4 * b53;
+ long long b55 = b5 * b54;
+ long long b56 = b6 * b55;
+ long long b57 = b7 * b56;
+ int b58 = b8 * b57;
+ float b59 = b9 * b58;
+ int b60 = b0 * b59;
+ float b61 = b1 * b60;
+ float b62 = b2 * b61;
+ int b63 = b3 * b62;
+ double b64 = b4 * b63;
+ int b65 = b5 * b64;
+ int b66 = b6 * b65;
+ double b67 = b7 * b66;
+ double b68 = b8 * b67;
+ int b69 = b9 * b68;
+ char b70 = b0 * b69;
+ char b71 = b1 * b70;
+ double b72 = b2 * b71;
+ double b73 = b3 * b72;
+ char b74 = b4 * b73;
+ float b75 = b5 * b74;
+ float b76 = b6 * b75;
+ double b77 = b7 * b76;
+ char b78 = b8 * b77;
+ float b79 = b9 * b78;
+ float b80 = b0 * b79;
+ char b81 = b1 * b80;
+ char b82 = b2 * b81;
+ float b83 = b3 * b82;
+ char b84 = b4 * b83;
+ int b85 = b5 * b84;
+ int b86 = b6 * b85;
+ double b87 = b7 * b86;
+ float b88 = b8 * b87;
+ double b89 = b9 * b88;
+ int b90 = b0 * b89;
+ float b91 = b1 * b90;
+ double b92 = b2 * b91;
+ int b93 = b3 * b92;
+ int b94 = b4 * b93;
+ long long b95 = b5 * b94;
+ long long b96 = b6 * b95;
+ long long b97 = b7 * b96;
+ long long b98 = b8 * b97;
+ long long b99 = b9 * b98;
+ long long b100 = b0 * b99;
+ long long b101 = b1 * b100;
+ long long b102 = b2 * b101;
+ long long b103 = b3 * b102;
+ long long b104 = b4 * b103;
+ long long b105 = b5 * b104;
+ long long b106 = b6 * b105;
+ long long b107 = b7 * b106;
+ long long b108 = b8 * b107;
+ long long b109 = b9 * b108;
+ long long b110 = b0 * b109;
+ long long b111 = b1 * b110;
+ long long b112 = b2 * b111;
+ long long b113 = b3 * b112;
+ long long b114 = b4 * b113;
+ int b115 = b5 * b114;
+ int b116 = b6 * b115;
+ int b117 = b7 * b116;
+ float b118 = b8 * b117;
+ float b119 = b9 * b118;
+ int b120 = b0 * b119;
+ double b121 = b1 * b120;
+ float b122 = b2 * b121;
+ int b123 = b3 * b122;
+ double b124 = b4 * b123;
+ int b125 = b5 * b124;
+ char b126 = b6 * b125;
+ double b127 = b7 * b126;
+ char b128 = b8 * b127;
+ float b129 = b9 * b128;
+ char b130 = b0 * b129;
+ double b131 = b1 * b130;
+ char b132 = b2 * b131;
+ float b133 = b3 * b132;
+ char b134 = b4 * b133;
+ double b135 = b5 * b134;
+ char b136 = b6 * b135;
+ float b137 = b7 * b136;
+ char b138 = b8 * b137;
+ double b139 = b9 * b138;
+ char b140 = b0 * b139;
+ float b141 = b1 * b140;
+ char b142 = b2 * b141;
+ double b143 = b3 * b142;
+ char b144 = b4 * b143;
+ float b145 = b5 * b144;
+ char b146 = b6 * b145;
+ double b147 = b7 * b146;
+ int b148 = b8 * b147;
+ float b149 = b9 * b148;
+ int b150 = b0 * b149;
+ double b151 = b1 * b150;
+ int b152 = b2 * b151;
+ float b153 = b3 * b152;
+ int b154 = b4 * b153;
+ double b155 = b5 * b154;
+ int b156 = b6 * b155;
+ float b157 = b7 * b156;
+ int b158 = b8 * b157;
+ double b159 = b9 * b158;
+ int b160 = b0 * b159;
+ float b161 = b1 * b160;
+ int b162 = b2 * b161;
+ return MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29)
+ * b0 * b1 * b2 * b3 * b4 * b5 * b6 * b7 * b8 * b9
+ * b10 * b11 * b12 * b13 * b14 * b15 * b16 * b17 * b18 * b19
+ * b20 * b21 * b22 * b23 * b24 * b25 * b26 * b27 * b28 * b29
+ * b30 * b31 * b32 * b33 * b34 * b35 * b36 * b37 * b38 * b39
+ * b40 * b41 * b42 * b43 * b44 * b45 * b46 * b47 * b48 * b49
+ * b50 * b51 * b52 * b53 * b54 * b55 * b56 * b57 * b58 * b59
+ * b60 * b61 * b62 * b63 * b64 * b65 * b66 * b67 * b68 * b69
+ * b70 * b71 * b72 * b73 * b74 * b75 * b76 * b77 * b78 * b79
+ * b80 * b81 * b82 * b83 * b84 * b85 * b86 * b87 * b88 * b89
+ * b90 * b91 * b92 * b93 * b94 * b95 * b96 * b97 * b98 * b99
+ * b100 * b101 * b102 * b103 * b104 * b105 * b106 * b107 * b108 * b109
+ * b110 * b111 * b112 * b113 * b114 * b115 * b116 * b117 * b118 * b119
+ * b120 * b121 * b122 * b123 * b124 * b125 * b126 * b127 * b128 * b129
+ * b130 * b131 * b132 * b133 * b134 * b135 * b136 * b137 * b138 * b139
+ * b140 * b141 * b142 * b143 * b144 * b145 * b146 * b147 * b148 * b149
+ * b150 * b151 * b152 * b153 * b154 * b155 * b156 * b157 * b158 * b159
+ * b160 * b161 * b162
+ ;
+}
+
diff --git a/test/kvx/interop/common.h b/test/kvx/interop/common.h
new file mode 100644
index 00000000..055ce7ea
--- /dev/null
+++ b/test/kvx/interop/common.h
@@ -0,0 +1,28 @@
+#ifndef __COMMON_H__
+#define __COMMON_H__
+
+void void_void(void);
+
+long long ll_void(void);
+
+int i_oneiarg(int arg);
+
+int i_multiiargs(int arg1, char arg2, char arg3, int arg4);
+
+int i_manyiargs(char a0, int a1, char a2, int a3, char a4, char a5, int a6, int a7, char a8, int a9,
+ char a10, int a11, char a12, int a13, char a14, char a15, int a16, int a17, char a18, int a19,
+ char a20, int a21, char a22, int a23, char a24, char a25, int a26, int a27, char a28, int a29);
+
+int ll_onellarg(long long arg);
+
+long long ll_multillargs(long long arg1, char arg2, char arg3, long long arg4);
+
+long long ll_manyllargs(char a0, long long a1, char a2, long long a3, char a4, char a5, long long a6, long long a7, char a8, long long a9,
+ char a10, long long a11, char a12, long long a13, char a14, char a15, long long a16, long long a17, char a18, long long a19,
+ char a20, long long a21, char a22, long long a23, char a24, char a25, long long a26, long long a27, char a28, long long a29);
+
+double stackhell(char a0, long long a1, char a2, long long a3, char a4, char a5, long long a6, long long a7, char a8, long long a9,
+ char a10, long long a11, char a12, long long a13, char a14, char a15, long long a16, long long a17, char a18, long long a19,
+ char a20, long long a21, char a22, long long a23, char a24, char a25, long long a26, long long a27, char a28, long long a29);
+
+#endif
diff --git a/test/kvx/interop/framework.h b/test/kvx/interop/framework.h
new file mode 100644
index 00000000..3bbfa271
--- /dev/null
+++ b/test/kvx/interop/framework.h
@@ -0,0 +1,66 @@
+#ifndef __FRAMEWORK_H__
+#define __FRAMEWORK_H__
+
+#include <stdio.h>
+#include "../prng/prng.c"
+
+#define BEGIN_TEST_N(type, N)\
+ int main(void){\
+ type t[N], c, i, j, S;\
+ srand(0);\
+ S = 0;\
+ for (i = 0 ; i < 100 ; i++){\
+ c = randlong();\
+ for (j = 0 ; j < N ; j++)\
+ t[j] = randlong();\
+ /* END BEGIN_TEST_N */
+
+#define BEGIN_TEST(type)\
+ int main(void){\
+ type a, b, c, S;\
+ int i;\
+ srand(0);\
+ S = 0;\
+ for (i = 0 ; i < 100 ; i++){\
+ c = randlong();\
+ a = randlong();\
+ b = randlong();
+ /* END BEGIN_TEST */
+
+/* In between BEGIN_TEST and END_TEST : definition of c */
+
+#define END_TEST64()\
+ printf("%llu\t%llu\t%llu\n", a, b, c);\
+ S += c;\
+ }\
+ return S;\
+ }
+ /* END END_TEST64 */
+
+#define END_TEST32()\
+ printf("%u\t%u\t%u\n", a, b, c);\
+ S += c;\
+ }\
+ return S;\
+ }
+ /* END END_TEST32 */
+
+#define END_TESTF32()\
+ printf("%e\t%e\t%e\n", a, b, c);\
+ S += c;\
+ }\
+ return 0;\
+ }
+ /* END END_TESTF32 */
+
+#define END_TESTF64()\
+ printf("%e\t%e\t%e\n", a, b, c);\
+ S += c;\
+ }\
+ return 0;\
+ }
+ /* END END_TESTF64 */
+
+#endif
+
+
diff --git a/test/kvx/interop/i32.c b/test/kvx/interop/i32.c
new file mode 100644
index 00000000..6bc2705c
--- /dev/null
+++ b/test/kvx/interop/i32.c
@@ -0,0 +1,13 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(int)
+ c = i_manyiargs(a, b, a-b, a+b, a*2, b*2, a*2-b, a+b*2, (a-b)*2, (a+b)*2,
+ -2*a, -2*b, a-b, a+b, a*3, b*3, a*3-b, a+b*3, (a-b)*3, (a+b)*3,
+ -3*a, -3*b, a-b, a+b, a*4, b*4, a*4-b, a+b*4, (a-b)*4, (a+b)*4);
+ c += i_multiiargs(a, b, a-b, a+b);
+ c += i_oneiarg(a);
+ void_void();
+ c += a;
+END_TEST32()
+
diff --git a/test/kvx/interop/i64.c b/test/kvx/interop/i64.c
new file mode 100644
index 00000000..3e7240f7
--- /dev/null
+++ b/test/kvx/interop/i64.c
@@ -0,0 +1,14 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(long long)
+ c = ll_manyllargs(a, b, a-b, a+b, a*2, b*2, a*2-b, a+b*2, (a-b)*2, (a+b)*2,
+ -2*a, -2*b, a-b, a+b, a*3, b*3, a*3-b, a+b*3, (a-b)*3, (a+b)*3,
+ -3*a, -3*b, a-b, a+b, a*4, b*4, a*4-b, a+b*4, (a-b)*4, (a+b)*4);
+ c += ll_multillargs(a, b, a-b, a+b);
+ c += ll_onellarg(a);
+ c = ll_void();
+ c += a;
+ void_void();
+ c += a;
+END_TEST64()
diff --git a/test/kvx/interop/individual/i_multiiargs.c b/test/kvx/interop/individual/i_multiiargs.c
new file mode 100644
index 00000000..888742b5
--- /dev/null
+++ b/test/kvx/interop/individual/i_multiiargs.c
@@ -0,0 +1,6 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(int)
+ c = i_multiiargs(a, b, a-b, a+b);
+END_TEST32()
diff --git a/test/kvx/interop/individual/i_oneiarg.c b/test/kvx/interop/individual/i_oneiarg.c
new file mode 100644
index 00000000..9c969fb8
--- /dev/null
+++ b/test/kvx/interop/individual/i_oneiarg.c
@@ -0,0 +1,6 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(int)
+ c = i_oneiarg(a);
+END_TEST32()
diff --git a/test/kvx/interop/individual/ll_multillargs.c b/test/kvx/interop/individual/ll_multillargs.c
new file mode 100644
index 00000000..34b422eb
--- /dev/null
+++ b/test/kvx/interop/individual/ll_multillargs.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(long long)
+ c = ll_multillargs(a, b, a-b, a+b);
+END_TEST64()
+
diff --git a/test/kvx/interop/individual/ll_onellarg.c b/test/kvx/interop/individual/ll_onellarg.c
new file mode 100644
index 00000000..a2fbbbe9
--- /dev/null
+++ b/test/kvx/interop/individual/ll_onellarg.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(long long)
+ c = ll_onellarg(a);
+END_TEST64()
+
diff --git a/test/kvx/interop/individual/ll_void.c b/test/kvx/interop/individual/ll_void.c
new file mode 100644
index 00000000..da128fdd
--- /dev/null
+++ b/test/kvx/interop/individual/ll_void.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(long long)
+ c = ll_void();
+ c += a;
+END_TEST64()
diff --git a/test/kvx/interop/individual/void_void.c b/test/kvx/interop/individual/void_void.c
new file mode 100644
index 00000000..976a721b
--- /dev/null
+++ b/test/kvx/interop/individual/void_void.c
@@ -0,0 +1,7 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(long long)
+ void_void();
+ c = a;
+END_TEST64()
diff --git a/test/kvx/interop/stackhell.c b/test/kvx/interop/stackhell.c
new file mode 100644
index 00000000..5abaa71d
--- /dev/null
+++ b/test/kvx/interop/stackhell.c
@@ -0,0 +1,9 @@
+#include "framework.h"
+#include "common.h"
+
+BEGIN_TEST(double)
+ c = stackhell(a, b, a*b, a*b, a*2, b*2, a*2*b, a*b*2, (a*b)*2, (a*b)*2,
+ 2*a, 2*b, a*b, a*b, a*3, b*3, a*3*b, a*b*3, (a*b)*3, (a*b)*3,
+ 3*a, 3*b, a*b, a*b, a*4, b*4, a*4*b, a*b*4, (a*b)*4, (a*b)*4);
+
+END_TESTF64()
diff --git a/test/kvx/interop/vaarg_common.c b/test/kvx/interop/vaarg_common.c
new file mode 100644
index 00000000..3314959f
--- /dev/null
+++ b/test/kvx/interop/vaarg_common.c
@@ -0,0 +1,383 @@
+#include <stdarg.h>
+
+#define STACK int a[100];\
+ a[42] = 42;
+
+#define ONEARG_OP(arg) (3*magic(arg)+2)
+
+#define MULTIARG_OP(arg1, arg2, arg3, arg4) (arg1 ^ magic(arg2) << arg3 - arg4)
+
+#define MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,\
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,\
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29)\
+ (a0 + a1 * a2 + magic(a3) * a4 + a5 + a6 + a7 - a8 + a9 +\
+ a10 + a11 - a12 ^ a13 + a14 - magic(a15) + a16 ^ a17 + a18 + a19 +\
+ a20 + a21 + a22 * a23 + a24 + a25 << a26 & a27 + a28 + a29)
+
+#define VA_START(vl, arg) va_list vl; va_start(vl, arg)
+#define VA_END(vl) va_end(vl)
+
+int magic(long a){
+ return a*2 + 42;
+}
+
+void void_void(void){
+ STACK;
+}
+
+long long ll_void(void){
+ STACK;
+ return 0xdeadbeefdeadbeefULL;
+}
+
+// int i_oneiarg(int arg){
+int i_oneiarg(int arg, ...){
+ STACK;
+ VA_START(vl, arg);
+ VA_END(vl);
+ return ONEARG_OP(arg);
+}
+
+//int i_multiiargs(int arg1, char arg2, char arg3, int arg4){
+int i_multiiargs(int arg1, ...){
+ STACK;
+ VA_START(vl, arg1);
+ char arg2 = va_arg(vl, int);
+ char arg3 = va_arg(vl, int);
+ int arg4 = va_arg(vl, int);
+ VA_END(vl);
+ return MULTIARG_OP(arg1, arg2, arg3, arg4);
+}
+
+//int i_manyiargs(char a0, int a1, char a2, int a3, char a4, char a5, int a6, int a7, char a8, int a9,
+// char a10, int a11, char a12, int a13, char a14, char a15, int a16, int a17, char a18, int a19,
+// char a20, int a21, char a22, int a23, char a24, char a25, int a26, int a27, char a28, int a29)
+int i_manyiargs(char a0, ...)
+{
+ STACK;
+ VA_START(vl, a0);
+ VA_START(vl2, a0);
+ int a1 = va_arg(vl, int);
+ char a2 = va_arg(vl, int);
+ int a3 = va_arg(vl, int);
+ char a4 = va_arg(vl, int);
+ char a5 = va_arg(vl, int);
+ char b1 = va_arg(vl2, int);
+ int a6 = va_arg(vl, int);
+ int a7 = va_arg(vl, int);
+ char a8 = va_arg(vl, int);
+ char b2 = va_arg(vl2, int);
+ int a9 = va_arg(vl, int);
+ char a10 = va_arg(vl, int);
+ int a11 = va_arg(vl, int);
+ char a12 = va_arg(vl, int);
+ char b3 = va_arg(vl2, int);
+ int a13 = va_arg(vl, int);
+ char a14 = va_arg(vl, int);
+ char a15 = va_arg(vl, int);
+ int a16 = va_arg(vl, int);
+ int a17 = va_arg(vl, int);
+ char a18 = va_arg(vl, int);
+ int a19 = va_arg(vl, int);
+ char a20 = va_arg(vl, int);
+ int a21 = va_arg(vl, int);
+ char a22 = va_arg(vl, int);
+ int a23 = va_arg(vl, int);
+ char a24 = va_arg(vl, int);
+ char a25 = va_arg(vl, int);
+ int a26 = va_arg(vl, int);
+ char b4 = va_arg(vl2, int);
+ int a27 = va_arg(vl, int);
+ char a28 = va_arg(vl, int);
+ int a29 = va_arg(vl, int);
+ VA_END(vl);
+ VA_END(vl);
+ return MANYARG_OP(a0, a1, a2, a3, a4, (a5*b2), a6, a7, a8, a9,
+ (a10*b3), a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, (a21*b1), a22, a23, (a24*b3), a25, a26, a27, a28, a29);
+}
+
+//int ll_onellarg(long long arg){
+int ll_onellarg(long long arg, ...){
+ STACK;
+ VA_START(vl, arg);
+ VA_END(vl);
+ return ONEARG_OP(arg);
+}
+
+//long long ll_multillargs(long long arg1, char arg2, char arg3, long long arg4){
+long long ll_multillargs(long long arg1, ...){
+ STACK;
+ VA_START(vl, arg1);
+ char arg2 = va_arg(vl, int);
+ char arg3 = va_arg(vl, int);
+ long long arg4 = va_arg(vl, long long);
+ VA_END(vl);
+ return MULTIARG_OP(arg1, arg2, arg3, arg4);
+}
+
+//long long ll_manyllargs(char a0, int a1, char a2, long long a3, char a4, char a5, long long a6, long long a7, char a8, long long a9,
+// char a10, long long a11, char a12, int a13, char a14, char a15, long long a16, long long a17, char a18, long long a19,
+// char a20, int a21, char a22, long long a23, char a24, char a25, long long a26, int a27, char a28, long long a29)
+long long ll_manyllargs(char a0, ...)
+{
+ STACK;
+ VA_START(vl, a0);
+ int a1 = va_arg(vl, int);
+ char a2 = va_arg(vl, int);
+ long long a3 = va_arg(vl, long long);
+ char a4 = va_arg(vl, int);
+ char a5 = va_arg(vl, int);
+ long long a6 = va_arg(vl, long long);
+ long long a7 = va_arg(vl, long long);
+ char a8 = va_arg(vl, int);
+ long long a9 = va_arg(vl, long long);
+ char a10 = va_arg(vl, int);
+ long long a11 = va_arg(vl, long long);
+ char a12 = va_arg(vl, int);
+ int a13 = va_arg(vl, int);
+ char a14 = va_arg(vl, int);
+ char a15 = va_arg(vl, int);
+ long long a16 = va_arg(vl, long long);
+ long long a17 = va_arg(vl, long long);
+ char a18 = va_arg(vl, int);
+ long long a19 = va_arg(vl, long long);
+ char a20 = va_arg(vl, int);
+ int a21 = va_arg(vl, int);
+ char a22 = va_arg(vl, int);
+ long long a23 = va_arg(vl, long long);
+ char a24 = va_arg(vl, int);
+ char a25 = va_arg(vl, int);
+ long long a26 = va_arg(vl, long long);
+ int a27 = va_arg(vl, int);
+ char a28 = va_arg(vl, int);
+ long long a29 = va_arg(vl, long long);
+ VA_END(vl);
+ return MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29);
+}
+
+//long long stackhell(char a0, int a1, char a2, long long a3, char a4, char a5, long long a6, long long a7, char a8, long long a9,
+// char a10, long long a11, char a12, int a13, char a14, char a15, long long a16, long long a17, char a18, long long a19,
+// char a20, int a21, char a22, long long a23, char a24, char a25, long long a26, int a27, char a28, long long a29)
+long long stackhell(char a0, ...)
+{
+ VA_START(vl, a0);
+ int a1 = va_arg(vl, int);
+ char a2 = va_arg(vl, int);
+ long long a3 = va_arg(vl, long long);
+ char a4 = va_arg(vl, int);
+ char a5 = va_arg(vl, int);
+ long long a6 = va_arg(vl, long long);
+ long long a7 = va_arg(vl, long long);
+ char a8 = va_arg(vl, int);
+ long long a9 = va_arg(vl, long long);
+ char a10 = va_arg(vl, int);
+ long long a11 = va_arg(vl, long long);
+ char a12 = va_arg(vl, int);
+ int a13 = va_arg(vl, int);
+ char a14 = va_arg(vl, int);
+ char a15 = va_arg(vl, int);
+ long long a16 = va_arg(vl, long long);
+ long long a17 = va_arg(vl, long long);
+ char a18 = va_arg(vl, int);
+ long long a19 = va_arg(vl, long long);
+ char a20 = va_arg(vl, int);
+ int a21 = va_arg(vl, int);
+ char a22 = va_arg(vl, int);
+ long long a23 = va_arg(vl, long long);
+ char a24 = va_arg(vl, int);
+ char a25 = va_arg(vl, int);
+ long long a26 = va_arg(vl, long long);
+ int a27 = va_arg(vl, int);
+ char a28 = va_arg(vl, int);
+ long long a29 = va_arg(vl, long long);
+ VA_END(vl);
+
+ long long b0 = a0;
+ long long b1 = a1 + b0;
+ long long b2 = a2 + b1;
+ int b3 = a3 + b2;
+ int b4 = a4 + b3;
+ int b5 = a5 + b4;
+ int b6 = a6 + b5;
+ int b7 = a7 + b6;
+ char b8 = a8 + b7;
+ char b9 = a9 + b8;
+ char b10 = a10 + b9;
+ char b11 = a11 + b10;
+ char b12 = a12 + b11;
+ int b13 = a13 + b12;
+ long long b14 = a14 + b13;
+ long long b15 = a15 + b14;
+ long long b16 = a16 + b15;
+ long long b17 = a17 + b16;
+ long long b18 = a18 + b17;
+ long long b19 = a19 + b18;
+ long long b20 = a20 + b19;
+ long long b21 = a21 + b20;
+ long long b22 = a22 + b21;
+ long long b23 = a23 + b22;
+ long long b24 = a24 + b23;
+ long long b25 = a25 + b24;
+ long long b26 = a26 + b25;
+ long long b27 = a27 + b26;
+ int b28 = a28 + b27;
+ int b29 = a29 + b28;
+ int b30 = b0 + b29;
+ int b31 = b1 + b30;
+ int b32 = b2 + b31;
+ char b33 = b3 + b32;
+ char b34 = b4 + b33;
+ char b35 = b5 + b34;
+ char b36 = b6 + b35;
+ char b37 = b7 + b36;
+ int b38 = b8 + b37;
+ int b39 = b9 + b38;
+ int b40 = b0 + b39;
+ int b41 = b1 + b40;
+ int b42 = b2 + b41;
+ int b43 = b3 + b42;
+ int b44 = b4 + b43;
+ int b45 = b5 + b44;
+ int b46 = b6 + b45;
+ int b47 = b7 + b46;
+ int b48 = b8 + b47;
+ long long b49 = b9 + b48;
+ long long b50 = b0 + b49;
+ long long b51 = b1 + b50;
+ long long b52 = b2 + b51;
+ long long b53 = b3 + b52;
+ long long b54 = b4 + b53;
+ long long b55 = b5 + b54;
+ long long b56 = b6 + b55;
+ long long b57 = b7 + b56;
+ int b58 = b8 + b57;
+ int b59 = b9 + b58;
+ int b60 = b0 + b59;
+ int b61 = b1 + b60;
+ int b62 = b2 + b61;
+ int b63 = b3 + b62;
+ int b64 = b4 + b63;
+ int b65 = b5 + b64;
+ int b66 = b6 + b65;
+ int b67 = b7 + b66;
+ int b68 = b8 + b67;
+ int b69 = b9 + b68;
+ char b70 = b0 + b69;
+ char b71 = b1 + b70;
+ char b72 = b2 + b71;
+ char b73 = b3 + b72;
+ char b74 = b4 + b73;
+ char b75 = b5 + b74;
+ char b76 = b6 + b75;
+ char b77 = b7 + b76;
+ char b78 = b8 + b77;
+ char b79 = b9 + b78;
+ char b80 = b0 + b79;
+ char b81 = b1 + b80;
+ char b82 = b2 + b81;
+ char b83 = b3 + b82;
+ char b84 = b4 + b83;
+ int b85 = b5 + b84;
+ int b86 = b6 + b85;
+ int b87 = b7 + b86;
+ int b88 = b8 + b87;
+ int b89 = b9 + b88;
+ int b90 = b0 + b89;
+ int b91 = b1 + b90;
+ int b92 = b2 + b91;
+ int b93 = b3 + b92;
+ int b94 = b4 + b93;
+ long long b95 = b5 + b94;
+ long long b96 = b6 + b95;
+ long long b97 = b7 + b96;
+ long long b98 = b8 + b97;
+ long long b99 = b9 + b98;
+ long long b100 = b0 + b99;
+ long long b101 = b1 + b100;
+ long long b102 = b2 + b101;
+ long long b103 = b3 + b102;
+ long long b104 = b4 + b103;
+ long long b105 = b5 + b104;
+ long long b106 = b6 + b105;
+ long long b107 = b7 + b106;
+ long long b108 = b8 + b107;
+ long long b109 = b9 + b108;
+ long long b110 = b0 + b109;
+ long long b111 = b1 + b110;
+ long long b112 = b2 + b111;
+ long long b113 = b3 + b112;
+ long long b114 = b4 + b113;
+ int b115 = b5 + b114;
+ int b116 = b6 + b115;
+ int b117 = b7 + b116;
+ int b118 = b8 + b117;
+ int b119 = b9 + b118;
+ int b120 = b0 + b119;
+ int b121 = b1 + b120;
+ int b122 = b2 + b121;
+ int b123 = b3 + b122;
+ int b124 = b4 + b123;
+ int b125 = b5 + b124;
+ char b126 = b6 + b125;
+ char b127 = b7 + b126;
+ char b128 = b8 + b127;
+ char b129 = b9 + b128;
+ char b130 = b0 + b129;
+ char b131 = b1 + b130;
+ char b132 = b2 + b131;
+ char b133 = b3 + b132;
+ char b134 = b4 + b133;
+ char b135 = b5 + b134;
+ char b136 = b6 + b135;
+ char b137 = b7 + b136;
+ char b138 = b8 + b137;
+ char b139 = b9 + b138;
+ char b140 = b0 + b139;
+ char b141 = b1 + b140;
+ char b142 = b2 + b141;
+ char b143 = b3 + b142;
+ char b144 = b4 + b143;
+ char b145 = b5 + b144;
+ char b146 = b6 + b145;
+ char b147 = b7 + b146;
+ int b148 = b8 + b147;
+ int b149 = b9 + b148;
+ int b150 = b0 + b149;
+ int b151 = b1 + b150;
+ int b152 = b2 + b151;
+ int b153 = b3 + b152;
+ int b154 = b4 + b153;
+ int b155 = b5 + b154;
+ int b156 = b6 + b155;
+ int b157 = b7 + b156;
+ int b158 = b8 + b157;
+ int b159 = b9 + b158;
+ int b160 = b0 + b159;
+ int b161 = b1 + b160;
+ int b162 = b2 + b161;
+ return MANYARG_OP(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
+ a20, a21, a22, a23, a24, a25, a26, a27, a28, a29)
+ + b0 + b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9
+ + b10 + b11 + b12 + b13 + b14 + b15 + b16 + b17 + b18 + b19
+ + b20 + b21 + b22 + b23 + b24 + b25 + b26 + b27 + b28 + b29
+ + b30 + b31 + b32 + b33 + b34 + b35 + b36 + b37 + b38 + b39
+ + b40 + b41 + b42 + b43 + b44 + b45 + b46 + b47 + b48 + b49
+ + b50 + b51 + b52 + b53 + b54 + b55 + b56 + b57 + b58 + b59
+ + b60 + b61 + b62 + b63 + b64 + b65 + b66 + b67 + b68 + b69
+ + b70 + b71 + b72 + b73 + b74 + b75 + b76 + b77 + b78 + b79
+ + b80 + b81 + b82 + b83 + b84 + b85 + b86 + b87 + b88 + b89
+ + b90 + b91 + b92 + b93 + b94 + b95 + b96 + b97 + b98 + b99
+ + b100 + b101 + b102 + b103 + b104 + b105 + b106 + b107 + b108 + b109
+ + b110 + b111 + b112 + b113 + b114 + b115 + b116 + b117 + b118 + b119
+ + b120 + b121 + b122 + b123 + b124 + b125 + b126 + b127 + b128 + b129
+ + b130 + b131 + b132 + b133 + b134 + b135 + b136 + b137 + b138 + b139
+ + b140 + b141 + b142 + b143 + b144 + b145 + b146 + b147 + b148 + b149
+ + b150 + b151 + b152 + b153 + b154 + b155 + b156 + b157 + b158 + b159
+ + b160 + b161 + b162
+ ;
+}
+
diff --git a/test/kvx/lib/Makefile b/test/kvx/lib/Makefile
new file mode 100644
index 00000000..7df7dd16
--- /dev/null
+++ b/test/kvx/lib/Makefile
@@ -0,0 +1,133 @@
+KVXC ?= kvx-elf-gcc
+K1AR ?= kvx-elf-ar
+CC ?= gcc
+AR ?= gcc-ar
+CCOMP ?= ccomp
+CFLAGS ?= -O1 -Wl,--wrap=printf
+SIMU ?= kvx-mppa
+TIMEOUT ?= --signal=SIGTERM 60s
+
+DIR=./
+SRCDIR=$(DIR)
+OUTDIR=$(DIR)/out
+BINDIR=$(DIR)/bin
+ASMDIR=$(DIR)/asm
+OBJDIR=$(DIR)/obj
+
+KVXCPATH=$(shell which $(KVXC))
+K1ARPATH=$(shell which $(K1AR))
+CCPATH=$(shell which $(CC))
+ARPATH=$(shell which $(AR))
+SIMUPATH=$(shell which $(SIMU))
+
+TESTNAMES=printf-test
+X86_GCC_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .x86-gcc.out,$(TESTNAMES)))
+GCC_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .gcc.out,$(TESTNAMES)))
+CCOMP_OUT=$(addprefix $(OUTDIR)/,$(addsuffix .ccomp.out,$(TESTNAMES)))
+
+OUT=$(X86_GCC_OUT) $(GCC_OUT) $(CCOMP_OUT)
+BIN=$(addprefix $(BINDIR)/,$(addsuffix .x86-gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .gcc.bin,$(TESTNAMES)))\
+ $(addprefix $(BINDIR)/,$(addsuffix .ccomp.bin,$(TESTNAMES)))
+
+##
+# Targets
+##
+
+all: $(BIN) system.x86-gcc.a system.gcc.a
+
+.PHONY:
+test: $(X86_GCC_OUT) $(GCC_OUT)
+ @echo "Comparing x86 gcc output to k1 gcc.."
+ @for test in $(TESTNAMES); do\
+ x86out=$(OUTDIR)/$$test.x86-gcc.out;\
+ gccout=$(OUTDIR)/$$test.gcc.out;\
+ if ! diff $$x86out $$gccout; then\
+ >&2 echo "ERROR: $$x86out and $$gccout differ";\
+ else\
+ echo "GOOD: $$x86out and $$gccout concur";\
+ fi;\
+ done
+
+.PHONY:
+check: $(GCC_OUT) $(CCOMP_OUT)
+ @echo "Comparing k1 gcc output to ccomp.."
+ @for test in $(TESTNAMES); do\
+ gccout=$(OUTDIR)/$$test.gcc.out;\
+ ccompout=$(OUTDIR)/$$test.ccomp.out;\
+ if ! diff $$ccompout $$gccout; then\
+ >&2 echo "ERROR: $$ccompout and $$gccout differ";\
+ else\
+ echo "GOOD: $$ccompout and $$gccout concur";\
+ fi;\
+ done
+
+##
+# Rules
+##
+
+.SECONDARY:
+
+# Generating output
+
+## Version avec timeout
+$(OUTDIR)/%.x86-gcc.out: $(BINDIR)/%.x86-gcc.bin
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.gcc.out: $(BINDIR)/%.gcc.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+$(OUTDIR)/%.ccomp.out: $(BINDIR)/%.ccomp.bin $(SIMUPATH)
+ @mkdir -p $(@D)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+# Object to binary
+
+$(BINDIR)/%.x86-gcc.bin: $(OBJDIR)/%.x86-gcc.o system.x86-gcc.a $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+$(BINDIR)/%.gcc.bin: $(OBJDIR)/%.gcc.o system.gcc.a $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) $(filter-out $(KVXCPATH),$^) -o $@
+
+$(BINDIR)/%.ccomp.bin: $(OBJDIR)/%.ccomp.o system.gcc.a $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) $(filter-out $(CCOMPPATH),$^) -o $@
+
+# Generating libraries
+system.x86-gcc.a: $(OBJDIR)/printf.x86-gcc.o $(ARPATH)
+ $(AR) rcs $@ $<
+
+system.gcc.a: $(OBJDIR)/printf.gcc.o $(K1ARPATH)
+ $(K1AR) rcs $@ $<
+
+# Assembly to object
+
+$(OBJDIR)/%.x86-gcc.o: $(ASMDIR)/%.x86-gcc.s $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR)/%.gcc.o: $(ASMDIR)/%.gcc.s $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR)/%.ccomp.o: $(ASMDIR)/%.ccomp.s $(CCOMPPATH)
+ $(CCOMP) $(CFLAGS) -c $< -o $@
+
+# Source to assembly
+
+$(ASMDIR)/%.x86-gcc.s: $(SRCDIR)/%.c $(CCPATH)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.gcc.s: $(SRCDIR)/%.c $(KVXCPATH)
+ @mkdir -p $(@D)
+ $(KVXC) $(CFLAGS) -S $< -o $@
+
+$(ASMDIR)/%.ccomp.s: $(SRCDIR)/%.c $(CCOMPPATH)
+ @mkdir -p $(@D)
+ $(CCOMP) $(CFLAGS) -S $< -o $@
+
diff --git a/test/kvx/lib/printf-test.c b/test/kvx/lib/printf-test.c
new file mode 100644
index 00000000..25afd436
--- /dev/null
+++ b/test/kvx/lib/printf-test.c
@@ -0,0 +1,9 @@
+int printf(const char *, ...);
+
+int main(void){
+ int a = 42;
+ char *str = "Hi there";
+ printf("%s, I am %u\n", str, a);
+
+ return 0;
+}
diff --git a/test/kvx/lib/printf.c b/test/kvx/lib/printf.c
new file mode 100644
index 00000000..79984ef6
--- /dev/null
+++ b/test/kvx/lib/printf.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+int __wrap_printf(const char *format, ...){
+ va_list args;
+ va_start(args, format);
+ vprintf(format, args);
+ va_end(args);
+}
diff --git a/test/kvx/mmult/.gitignore b/test/kvx/mmult/.gitignore
new file mode 100644
index 00000000..b43ccc5f
--- /dev/null
+++ b/test/kvx/mmult/.gitignore
@@ -0,0 +1,4 @@
+mmult-test-ccomp-kvx
+mmult-test-gcc-kvx
+mmult-test-gcc-x86
+.zero
diff --git a/test/kvx/mmult/Makefile b/test/kvx/mmult/Makefile
new file mode 100644
index 00000000..252f8911
--- /dev/null
+++ b/test/kvx/mmult/Makefile
@@ -0,0 +1,71 @@
+KVXC ?= kvx-elf-gcc
+CC ?= gcc
+CCOMP ?= ccomp
+CFLAGS ?= -O2
+SIMU ?= kvx-mppa
+TIMEOUT ?= 10s
+
+KVXCPATH=$(shell which $(KVXC))
+CCPATH=$(shell which $(CC))
+CCOMPPATH=$(shell which $(CCOMP))
+SIMUPATH=$(shell which $(SIMU))
+
+PRNG=../prng/prng.c
+
+ALL= mmult-test-gcc-x86 mmult-test-gcc-kvx mmult-test-ccomp-kvx
+CCOMP_OUT= mmult-test-ccomp-kvx.out
+GCC_OUT= mmult-test-gcc-kvx.out
+X86_GCC_OUT= mmult-test-gcc-x86.out
+STUB_OUT=.zero
+
+all: $(ALL)
+
+mmult-test-gcc-x86: mmult.c $(PRNG) $(CCPATH)
+ $(CC) $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+mmult-test-gcc-kvx: mmult.c $(PRNG) $(KVXCPATH)
+ $(KVXC) $(CFLAGS) $(filter-out $(KVXCPATH),$^) -o $@
+
+mmult-test-ccomp-kvx: mmult.c $(PRNG) $(CCOMPPATH)
+ $(CCOMP) $(CFLAGS) $(filter-out $(CCOMPPATH),$^) -o $@
+
+.SECONDARY:
+%kvx.out: %kvx $(SIMUPATH)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+%x86.out: %x86
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+.zero:
+ @echo "0" > $@
+
+.PHONY:
+test: test-x86 test-kvx
+
+.PHONY:
+test-x86: $(X86_GCC_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR x86: $< failed";\
+ else\
+ echo "GOOD x86: $< succeeded";\
+ fi
+
+.PHONY:
+test-kvx: $(GCC_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $< failed";\
+ else\
+ echo "GOOD kvx: $< succeeded";\
+ fi
+
+.PHONY:
+check: $(CCOMP_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $< failed";\
+ else\
+ echo "GOOD kvx: $< succeeded";\
+ fi
+
+.PHONY:
+clean:
+ rm -f *.out mmult-test-ccomp-kvx mmult-test-gcc-kvx mmult-test-gcc-x86
diff --git a/test/kvx/mmult/README.md b/test/kvx/mmult/README.md
new file mode 100644
index 00000000..780603f6
--- /dev/null
+++ b/test/kvx/mmult/README.md
@@ -0,0 +1,17 @@
+MMULT
+=====
+
+Examples of matrix multiplication using different methods.
+
+We compute matrix multiplication using column-based matrix multiplication, then row-based, and finally block based.
+
+The test verifies that the result is the same on the three methods. If it is the same, 0 will be returned.
+
+The following commands can be run inside the folder:
+
+- `make`: produces the unitary test binaries
+ - `mmult-test-gcc-x86` : binary from gcc on x86
+ - `mmult-test-kvx-x86` : binary from gcc on kvx
+ - `mmult-test-ccomp-x86` : binary from ccomp on kvx
+- `make test`: tests the return value of the binaries produced by gcc.
+- `make check`: tests the return value of the binary produced by CompCert.
diff --git a/test/kvx/mmult/mmult.c b/test/kvx/mmult/mmult.c
new file mode 100644
index 00000000..aeb91d48
--- /dev/null
+++ b/test/kvx/mmult/mmult.c
@@ -0,0 +1,146 @@
+#include "../prng/types.h"
+#include "../prng/prng.h"
+
+#define __UNIT_TEST_MMULT__
+
+#ifdef __UNIT_TEST_MMULT__
+#define SIZE 10
+#else
+#include "test.h"
+#endif
+
+void mmult_row(uint64_t C[][SIZE], uint64_t A[][SIZE], uint64_t B[][SIZE]){
+ int i, j, k;
+
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++)
+ C[i][j] = 0;
+
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++)
+ for (k = 0 ; k < SIZE ; k++)
+ C[i][j] += A[i][k] * B[k][j];
+}
+
+void mmult_col(uint64_t C[][SIZE], uint64_t A[][SIZE], uint64_t B[][SIZE]){
+ int i, j, k;
+
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++)
+ C[i][j] = 0;
+
+ for (k = 0 ; k < SIZE ; k++)
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++)
+ C[i][j] += A[i][k] * B[k][j];
+}
+
+typedef struct mblock {
+ int imin, imax, jmin, jmax;
+ uint64_t *mat;
+} mblock;
+
+#define MAT_XY(mat, x, y) (mat)[(x)*SIZE + (y)]
+#define MAT_IJ(block, i, j) MAT_XY((block)->mat, (block)->imin + (i), block->jmin + (j))
+
+void divac_mul(mblock *C, const mblock *A, const mblock *B){
+ const int size = C->imax - C->imin;
+ int i, j, k;
+
+ for (i = 0 ; i < size ; i++)
+ for (j = 0 ; j < size ; j++)
+ for (k = 0 ; k < size ; k++)
+ MAT_IJ(C, i, j) += MAT_IJ(A, i, k) * MAT_IJ(B, k, j);
+}
+
+#define BLOCK_X_MID(block) ((block)->imin + (block)->imax) / 2
+#define BLOCK_Y_MID(block) ((block)->jmin + (block)->jmax) / 2
+
+#define MAKE_MBLOCK(newb, block, I, J) \
+ mblock newb = {.mat=(block)->mat};\
+ if ((I) == 0){\
+ newb.imin = (block)->imin;\
+ newb.imax = BLOCK_X_MID((block));\
+ } else {\
+ newb.imin = BLOCK_X_MID((block));\
+ newb.imax = (block)->imax;\
+ } if ((J) == 0){\
+ newb.jmin = (block)->jmin;\
+ newb.jmax = BLOCK_Y_MID((block));\
+ } else {\
+ newb.jmin = BLOCK_Y_MID((block));\
+ newb.jmax = (block)->jmax;\
+ }
+
+void divac_part(mblock *C, const mblock *A, const mblock *B);
+
+void divac_wrap(mblock *C , char IC, char JC,
+ const mblock *A, char IA, char JA,
+ const mblock *B, char IB, char JB){
+ MAKE_MBLOCK(Cb, C, IC, JC);
+ MAKE_MBLOCK(Ab, A, IA, JA);
+ MAKE_MBLOCK(Bb, B, IB, JB);
+
+ divac_part(&Cb, &Ab, &Bb);
+}
+
+
+void divac_part(mblock *C, const mblock *A, const mblock *B){
+ const int size = C->imax - C->imin;
+
+ if (size % 2 == 1)
+ divac_mul(C, A, B);
+ else{
+ /* C_00 = A_00 B_00 + A_01 B_10 */
+ divac_wrap(C, 0, 0, A, 0, 0, B, 0, 0);
+ divac_wrap(C, 0, 0, A, 0, 1, B, 1, 0);
+
+ /* C_10 = A_10 B_00 + A_11 B_10 */
+ divac_wrap(C, 1, 0, A, 1, 0, B, 0, 0);
+ divac_wrap(C, 1, 0, A, 1, 1, B, 1, 0);
+
+ /* C_01 = A_00 B_01 + A_01 B_11 */
+ divac_wrap(C, 0, 1, A, 0, 0, B, 0, 1);
+ divac_wrap(C, 0, 1, A, 0, 1, B, 1, 1);
+
+ /* C_11 = A_10 B_01 + A_11 B_11 */
+ divac_wrap(C, 1, 1, A, 1, 0, B, 0, 1);
+ divac_wrap(C, 1, 1, A, 1, 1, B, 1, 1);
+ }
+
+}
+
+void mmult_divac(uint64_t C[][SIZE], uint64_t A[][SIZE], uint64_t B[][SIZE]){
+ mblock Cb = {.mat = (uint64_t *) C, .imin = 0, .imax = SIZE, .jmin = 0, .jmax = SIZE};
+ mblock Ab = {.mat = (uint64_t *) A , .imin = 0, .imax = SIZE, .jmin = 0, .jmax = SIZE};
+ mblock Bb = {.mat = (uint64_t *) B , .imin = 0, .imax = SIZE, .jmin = 0, .jmax = SIZE};
+
+ divac_part(&Cb, &Ab, &Bb);
+}
+
+#ifdef __UNIT_TEST_MMULT__
+static uint64_t C1[SIZE][SIZE], C2[SIZE][SIZE], C3[SIZE][SIZE];
+static uint64_t A[SIZE][SIZE], B[SIZE][SIZE];
+
+int main(void){
+ srand(42);
+ int i, j;
+
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++){
+ A[i][j] = randlong();
+ B[i][j] = randlong();
+ }
+
+ mmult_row(C1, A, B);
+ mmult_col(C2, A, B);
+ mmult_divac(C3, A, B);
+
+ for (i = 0 ; i < SIZE ; i++)
+ for (j = 0 ; j < SIZE ; j++)
+ if (!(C1[i][j] == C2[i][j] && C1[i][j] == C3[i][j]))
+ return -1;
+
+ return 0;
+}
+#endif /* __UNIT_TEST_MMULT__ */
diff --git a/test/kvx/mmult/mmult.h b/test/kvx/mmult/mmult.h
new file mode 100644
index 00000000..3721784a
--- /dev/null
+++ b/test/kvx/mmult/mmult.h
@@ -0,0 +1,10 @@
+#ifndef __MMULT_H__
+#define __MMULT_H__
+
+#include "../lib/types.h"
+
+void mmult_row(uint64_t *A, const uint64_t *B, const uint64_t *C);
+void mmult_column(uint64_t *A, const uint64_t *B, const uint64_t *C);
+void mmult_strassen(uint64_t *A, const uint64_t *B, const uint64_t *C);
+
+#endif /* __MMULT_H__ */
diff --git a/test/kvx/prng/.gitignore b/test/kvx/prng/.gitignore
new file mode 100644
index 00000000..08023900
--- /dev/null
+++ b/test/kvx/prng/.gitignore
@@ -0,0 +1,3 @@
+prng-test-ccomp-kvx
+prng-test-gcc-x86
+prng-test-gcc-kvx
diff --git a/test/kvx/prng/Makefile b/test/kvx/prng/Makefile
new file mode 100644
index 00000000..b97f4aa4
--- /dev/null
+++ b/test/kvx/prng/Makefile
@@ -0,0 +1,70 @@
+KVXC ?= kvx-elf-gcc
+CC ?= gcc
+CCOMP ?= ccomp
+CFLAGS ?= -O2
+SIMU ?= kvx-mppa
+TIMEOUT ?= 10s
+
+KVXCPATH=$(shell which $(KVXC))
+CCPATH=$(shell which $(CC))
+CCOMPPATH=$(shell which $(CCOMP))
+SIMUPATH=$(shell which $(SIMU))
+
+ALL= prng-test-gcc-x86 prng-test-gcc-kvx prng-test-ccomp-kvx
+CCOMP_OUT= prng-test-ccomp-kvx.out
+GCC_OUT= prng-test-gcc-kvx.out
+X86_GCC_OUT= prng-test-gcc-x86.out
+STUB_OUT=.zero
+
+all: $(ALL)
+
+prng-test-gcc-x86: prng.c $(CCPATH)
+ $(CC) -D__UNIT_TEST_PRNG__ $(CFLAGS) $< -o $@
+
+prng-test-gcc-kvx: prng.c $(KVXCPATH)
+ $(KVXC) -D__UNIT_TEST_PRNG__ $(CFLAGS) $< -o $@
+
+prng-test-ccomp-kvx: prng.c $(CCOMPPATH)
+ $(CCOMP) -D__UNIT_TEST_PRNG__ $(CFLAGS) $< -o $@
+
+.SECONDARY:
+%kvx.out: %kvx $(SIMUPATH)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+%x86.out: %x86
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+.zero:
+ @echo "0" > $@
+
+.PHONY:
+test: test-x86 test-kvx
+
+.PHONY:
+test-x86: $(X86_GCC_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR x86: $< failed";\
+ else\
+ echo "GOOD x86: $< succeeded";\
+ fi
+
+.PHONY:
+test-kvx: $(GCC_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $< failed";\
+ else\
+ echo "GOOD kvx: $< succeeded";\
+ fi
+
+.PHONY:
+check: $(CCOMP_OUT) $(STUB_OUT)
+ @if ! diff $< $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $< failed";\
+ else\
+ echo "GOOD kvx: $< succeeded";\
+ fi
+
+.PHONY:
+clean:
+ rm -f prng-test-gcc-x86 prng-test-gcc-kvx prng-test-ccomp-kvx
+ rm -f *.out
diff --git a/test/kvx/prng/README.md b/test/kvx/prng/README.md
new file mode 100644
index 00000000..98ed539d
--- /dev/null
+++ b/test/kvx/prng/README.md
@@ -0,0 +1,17 @@
+PRNG
+====
+
+This is a simple Pseudo Random Number Generator.
+
+`prng.c` contains a simple unitary test that compares the sum of the "bytewise sum"
+of 1000 generated numbers to a hardcoded result, that is the one obtained with
+`gcc -O2` on a x86 processor, and returns 0 if the result is correct.
+
+The following commands can be run inside that folder:
+
+- `make`: produces the unitary test binaries
+ - `prng-test-gcc-x86` : binary from gcc on x86
+ - `prng-test-kvx-x86` : binary from gcc on kvx
+ - `prng-test-ccomp-x86` : binary from ccomp on kvx
+- `make test`: tests the return value of the binaries produced by gcc.
+- `make check`: tests the return value of the binary produced by CompCert.
diff --git a/test/kvx/prng/prng.c b/test/kvx/prng/prng.c
new file mode 100644
index 00000000..71de1dc3
--- /dev/null
+++ b/test/kvx/prng/prng.c
@@ -0,0 +1,41 @@
+// https://en.wikipedia.org/wiki/Linear_congruential_generator -> MMIX Donald Knuth
+// modulo 2^64 = no need to do it explicitly
+
+#include "types.h"
+
+#define MULTIPLIER 6364136223846793005LL
+#define INCREMENT 1442695040888963407LL
+
+static uint64_t current;
+
+void srand(uint64_t seed){
+ current = seed;
+}
+
+uint64_t randlong(void){
+ return (current = MULTIPLIER * current + INCREMENT);
+}
+
+#ifdef __UNIT_TEST_PRNG__
+char bytewise_sum(uint64_t to_check){
+ char sum = 0;
+ int i;
+
+ for (i = 0 ; i < 8 ; i++)
+ sum += (to_check & (uint64_t)(0xFFULL << i*8)) >> i*8;
+
+ return sum;
+}
+
+int main(void){
+ srand(42);
+ int i;
+
+ for (i = 0 ; i < 1000 ; i++)
+ randlong();
+
+ uint64_t last = randlong();
+
+ return !((unsigned char)bytewise_sum(last) == 155);
+}
+#endif // __UNIT_TEST_PRNG__
diff --git a/test/kvx/prng/prng.h b/test/kvx/prng/prng.h
new file mode 100644
index 00000000..6abdb45a
--- /dev/null
+++ b/test/kvx/prng/prng.h
@@ -0,0 +1,10 @@
+#ifndef __PRNG_H__
+#define __PRNG_H__
+
+#include "types.h"
+
+void srand(uint64_t seed);
+
+uint64_t randlong(void);
+
+#endif // __PRNG_H__
diff --git a/test/kvx/prng/types.h b/test/kvx/prng/types.h
new file mode 100644
index 00000000..584023e3
--- /dev/null
+++ b/test/kvx/prng/types.h
@@ -0,0 +1,7 @@
+#ifndef __TYPES_H__
+#define __TYPES_H__
+
+#define uint64_t unsigned long long
+#define int64_t signed long long
+
+#endif // __TYPES_H__
diff --git a/test/kvx/simucheck.sh b/test/kvx/simucheck.sh
new file mode 100755
index 00000000..48698e35
--- /dev/null
+++ b/test/kvx/simucheck.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+# Tests the execution of the binaries produced by CompCert, by simulation
+
+cores=$(grep -c ^processor /proc/cpuinfo)
+
+source do_test.sh
+
+do_test check $cores
diff --git a/test/kvx/simutest.sh b/test/kvx/simutest.sh
new file mode 100755
index 00000000..729d1ba0
--- /dev/null
+++ b/test/kvx/simutest.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+# Tests the validity of the tests, in simulator
+
+cores=$(grep -c ^processor /proc/cpuinfo)
+
+source do_test.sh
+
+do_test test $cores
diff --git a/test/kvx/sort/.gitignore b/test/kvx/sort/.gitignore
new file mode 100644
index 00000000..070b87c4
--- /dev/null
+++ b/test/kvx/sort/.gitignore
@@ -0,0 +1,9 @@
+main-test-ccomp-kvx
+main-test-gcc-kvx
+main-test-gcc-x86
+merge-test-gcc-kvx
+merge-test-gcc-x86
+selection-test-gcc-kvx
+selection-test-gcc-x86
+insertion-test-gcc-kvx
+insertion-test-gcc-x86
diff --git a/test/kvx/sort/Makefile b/test/kvx/sort/Makefile
new file mode 100644
index 00000000..46a8f025
--- /dev/null
+++ b/test/kvx/sort/Makefile
@@ -0,0 +1,98 @@
+KVXC ?= kvx-elf-gcc
+CC ?= gcc
+CCOMP ?= ccomp
+CFLAGS ?= -O2
+SIMU ?= kvx-mppa
+TIMEOUT ?= 20s
+
+KVXCPATH=$(shell which $(KVXC))
+CCPATH=$(shell which $(CC))
+CCOMPPATH=$(shell which $(CCOMP))
+SIMUPATH=$(shell which $(SIMU))
+
+PRNG=../prng/prng.c
+
+CFILES=insertion.c merge.c selection.c main.c
+
+ALL= insertion-gcc-x86 insertion-gcc-kvx insertion-ccomp-kvx\
+ selection-gcc-x86 selection-gcc-kvx selection-ccomp-kvx\
+ merge-gcc-x86 merge-gcc-kvx merge-ccomp-kvx\
+ main-gcc-x86 main-gcc-kvx main-ccomp-kvx
+
+CCOMP_OUT= insertion-ccomp-kvx.out selection-ccomp-kvx.out merge-ccomp-kvx.out\
+ main-ccomp-kvx.out
+GCC_OUT= insertion-gcc-kvx.out selection-gcc-kvx.out merge-gcc-kvx.out\
+ main-gcc-kvx.out
+X86_GCC_OUT= insertion-gcc-x86.out selection-gcc-x86.out merge-gcc-x86.out\
+ main-gcc-x86.out
+STUB_OUT= .zero
+
+all: $(ALL)
+
+main-gcc-x86: $(CFILES) $(PRNG) $(CCPATH)
+ $(CC) $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+%-gcc-x86: %.c $(PRNG) $(CCPATH)
+ $(CC) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+main-gcc-kvx: $(CFILES) $(PRNG) $(CCPATH)
+ $(KVXC) $(CFLAGS) $(filter-out $(CCPATH),$^) -o $@
+
+%-gcc-kvx: %.c $(PRNG) $(KVXCPATH)
+ $(KVXC) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $(filter-out $(KVXCPATH),$^) -o $@
+
+main-ccomp-kvx: $(CFILES) $(PRNG) $(CCOMPPATH)
+ $(CCOMP) $(CFLAGS) $(filter-out $(CCOMPPATH),$^) -o $@
+
+%-ccomp-kvx: %.c $(PRNG) $(CCOMPPATH)
+ $(CCOMP) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $(filter-out $(CCOMPPATH),$^) -o $@
+
+.SECONDARY:
+%x86.out: %x86
+ ret=0; timeout $(TIMEOUT) ./$< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+%kvx.out: %kvx $(SIMUPATH)
+ ret=0; timeout $(TIMEOUT) $(SIMU) -- $< > $@ || { ret=$$?; }; echo $$ret >> $@
+
+.zero:
+ @echo "0" > $@
+
+.PHONY:
+test-x86: $(STUB_OUT) $(X86_GCC_OUT)
+ @for test in $(wordlist 2,100,$^); do\
+ if ! diff $$test $(STUB_OUT); then\
+ >&2 echo "ERROR x86: $$test failed";\
+ else\
+ echo "GOOD x86: $$test succeeded";\
+ fi;\
+ done
+
+.PHONY:
+test-kvx: $(STUB_OUT) $(GCC_OUT)
+ @for test in $(wordlist 2,100,$^); do\
+ if ! diff $$test $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $$test failed";\
+ else\
+ echo "GOOD kvx: $$test succeeded";\
+ fi;\
+ done
+
+.PHONY:
+test: test-x86 test-kvx
+
+.PHONY:
+check: $(STUB_OUT) $(CCOMP_OUT)
+ @for test in $(wordlist 2,100,$^); do\
+ if ! diff $$test $(STUB_OUT); then\
+ >&2 echo "ERROR kvx: $$test failed";\
+ else\
+ echo "GOOD kvx: $$test succeeded";\
+ fi;\
+ done
+
+.PHONY:
+clean:
+ for test in insertion main merge selection; do\
+ rm -f $$test-ccomp-kvx $$test-gcc-kvx $$test-gcc-x86;\
+ done
+ rm -f *.out
diff --git a/test/kvx/sort/README.md b/test/kvx/sort/README.md
new file mode 100644
index 00000000..98ed539d
--- /dev/null
+++ b/test/kvx/sort/README.md
@@ -0,0 +1,17 @@
+PRNG
+====
+
+This is a simple Pseudo Random Number Generator.
+
+`prng.c` contains a simple unitary test that compares the sum of the "bytewise sum"
+of 1000 generated numbers to a hardcoded result, that is the one obtained with
+`gcc -O2` on a x86 processor, and returns 0 if the result is correct.
+
+The following commands can be run inside that folder:
+
+- `make`: produces the unitary test binaries
+ - `prng-test-gcc-x86` : binary from gcc on x86
+ - `prng-test-kvx-x86` : binary from gcc on kvx
+ - `prng-test-ccomp-x86` : binary from ccomp on kvx
+- `make test`: tests the return value of the binaries produced by gcc.
+- `make check`: tests the return value of the binary produced by CompCert.
diff --git a/test/kvx/sort/insertion.c b/test/kvx/sort/insertion.c
new file mode 100644
index 00000000..bca09599
--- /dev/null
+++ b/test/kvx/sort/insertion.c
@@ -0,0 +1,59 @@
+#include "../prng/prng.h"
+#include "../prng/types.h"
+
+#ifdef __UNIT_TEST_INSERTION__
+#define SIZE 100
+#else
+#include "test.h"
+#endif
+
+void swap_ins(uint64_t *a, uint64_t *b){
+ uint64_t tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
+
+int insert_sort(uint64_t *res, const uint64_t *T){
+ int i, j;
+
+ if (SIZE <= 0)
+ return -1;
+
+ for (i = 0 ; i < SIZE ; i++)
+ res[i] = T[i];
+
+ for (i = 0 ; i < SIZE-1 ; i++){
+ if (res[i] > res[i+1]){
+ swap_ins(&res[i], &res[i+1]);
+ for (j = i ; j > 0 ; j--)
+ if (res[j-1] > res[j])
+ swap_ins(&res[j-1], &res[j]);
+ }
+ }
+
+ return 0;
+}
+
+#ifdef __UNIT_TEST_INSERTION__
+int main(void){
+ uint64_t T[SIZE];
+ uint64_t res[SIZE];
+ int i;
+ srand(42);
+
+ for (i = 0 ; i < SIZE ; i++)
+ T[i] = randlong();
+
+ /* Sorting the table */
+ if (insert_sort(res, T) < 0) return -1;
+
+ /* Computing max(T) */
+ uint64_t max = T[0];
+ for (i = 1 ; i < SIZE ; i++)
+ if (T[i] > max)
+ max = T[i];
+
+ /* We should have: max(T) == res[SIZE] */
+ return !(max == res[SIZE-1]);
+}
+#endif // __UNIT_TEST_INSERTION__
diff --git a/test/kvx/sort/insertion.h b/test/kvx/sort/insertion.h
new file mode 100644
index 00000000..6e37c5fe
--- /dev/null
+++ b/test/kvx/sort/insertion.h
@@ -0,0 +1,6 @@
+#ifndef __INSERTION_H__
+#define __INSERTION_H__
+
+int insert_sort(uint64_t *res, const uint64_t *T);
+
+#endif // __INSERTION_H__
diff --git a/test/kvx/sort/main.c b/test/kvx/sort/main.c
new file mode 100644
index 00000000..aef419aa
--- /dev/null
+++ b/test/kvx/sort/main.c
@@ -0,0 +1,34 @@
+#include "../prng/prng.h"
+#include "../prng/types.h"
+
+#include "test.h"
+#include "insertion.h"
+#include "selection.h"
+#include "merge.h"
+
+int main(void){
+ uint64_t T[SIZE];
+ uint64_t res1[SIZE], res2[SIZE], res3[SIZE];
+ int i;
+ srand(42);
+
+ for (i = 0 ; i < SIZE ; i++)
+ T[i] = randlong();
+
+ /* insertion sort */
+ if (insert_sort(res1, T) < 0) return -1;
+
+ /* selection sort */
+ if (select_sort(res2, T) < 0) return -2;
+
+ /* merge sort */
+ if (merge_sort(res3, T) < 0) return -3;
+
+ /* We should have: res1[i] == res2[i] == res3[i] */
+ for (i = 0 ; i < SIZE ; i++){
+ if (!(res1[i] == res2[i] && res2[i] == res3[i]))
+ return -4;
+ }
+
+ return 0;
+}
diff --git a/test/kvx/sort/merge.c b/test/kvx/sort/merge.c
new file mode 100644
index 00000000..99f8ba85
--- /dev/null
+++ b/test/kvx/sort/merge.c
@@ -0,0 +1,92 @@
+#include "../prng/prng.h"
+#include "../prng/types.h"
+
+//https://en.wikipedia.org/wiki/Merge_sort
+
+#ifdef __UNIT_TEST_MERGE__
+#define SIZE 100
+#else
+#include "test.h"
+#endif
+
+int min(int a, int b){
+ return (a < b)?a:b;
+}
+
+void BottomUpMerge(const uint64_t *A, int iLeft, int iRight, int iEnd, uint64_t *B)
+{
+ int i = iLeft, j = iRight, k;
+ for (k = iLeft; k < iEnd; k++) {
+ if (i < iRight && (j >= iEnd || A[i] <= A[j])) {
+ B[k] = A[i];
+ i = i + 1;
+ } else {
+ B[k] = A[j];
+ j = j + 1;
+ }
+ }
+}
+
+void CopyArray(uint64_t *to, const uint64_t *from)
+{
+ const int n = SIZE;
+ int i;
+
+ for(i = 0; i < n; i++)
+ to[i] = from[i];
+}
+
+void BottomUpMergeSort(uint64_t *A, uint64_t *B)
+{
+ const int n = SIZE;
+ int width, i;
+
+ for (width = 1; width < n; width = 2 * width)
+ {
+ for (i = 0; i < n; i = i + 2 * width)
+ {
+ BottomUpMerge(A, i, min(i+width, n), min(i+2*width, n), B);
+ }
+ CopyArray(A, B);
+ }
+}
+
+int merge_sort(uint64_t *res, const uint64_t *T){
+ int i;
+
+ if (SIZE <= 0)
+ return -1;
+
+ uint64_t B[SIZE];
+ uint64_t *A = res;
+ for (i = 0 ; i < SIZE ; i++)
+ A[i] = T[i];
+
+ BottomUpMergeSort(A, B);
+
+ return 0;
+}
+
+#ifdef __UNIT_TEST_MERGE__
+int main(void){
+ uint64_t T[SIZE];
+ uint64_t res[SIZE];
+ int i;
+ srand(42);
+
+ for (i = 0 ; i < SIZE ; i++)
+ T[i] = randlong();
+
+ /* Sorting the table */
+ if (merge_sort(res, T) < 0) return -1;
+
+ /* Computing max(T) */
+ uint64_t max = T[0];
+ for (i = 1 ; i < SIZE ; i++)
+ if (T[i] > max)
+ max = T[i];
+
+ /* We should have: max(T) == res[SIZE] */
+ return !(max == res[SIZE-1]);
+}
+#endif // __UNIT_TEST_MERGE__
diff --git a/test/kvx/sort/merge.h b/test/kvx/sort/merge.h
new file mode 100644
index 00000000..439ce64a
--- /dev/null
+++ b/test/kvx/sort/merge.h
@@ -0,0 +1,7 @@
+#ifndef __MERGE_H__
+#define __MERGE_H__
+
+int merge_sort(uint64_t *res, const uint64_t *T);
+
+#endif // __MERGE_H__
+
diff --git a/test/kvx/sort/selection.c b/test/kvx/sort/selection.c
new file mode 100644
index 00000000..df4be04f
--- /dev/null
+++ b/test/kvx/sort/selection.c
@@ -0,0 +1,62 @@
+#include "../prng/prng.h"
+#include "../prng/types.h"
+
+#ifdef __UNIT_TEST_SELECTION__
+#define SIZE 100
+#else
+#include "test.h"
+#endif
+
+void swap_sel(uint64_t *a, uint64_t *b){
+ uint64_t tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
+
+int select_sort(uint64_t *res, const uint64_t *T){
+ int i, j, iMin;
+
+ if (SIZE <= 0)
+ return -1;
+
+ for (i = 0 ; i < SIZE ; i++)
+ res[i] = T[i];
+
+ for (j = 0 ; j < SIZE ; j++){
+ iMin = j;
+ for (i = j+1 ; i < SIZE ; i++)
+ if (res[i] < res[iMin])
+ iMin = i;
+
+ if (iMin != j)
+ swap_sel (&res[j], &res[iMin]);
+ }
+
+ return 0;
+}
+
+#ifdef __UNIT_TEST_SELECTION__
+int main(void){
+ uint64_t T[SIZE];
+ uint64_t res[SIZE];
+ uint64_t max;
+ int i;
+ srand(42);
+
+ for (i = 0 ; i < SIZE ; i++)
+ T[i] = randlong();
+
+ /* Sorting the table */
+ if (select_sort(res, T) < 0) return -1;
+
+ /* Computing max(T) */
+ max = T[0];
+ for (i = 1 ; i < SIZE ; i++)
+ if (T[i] > max)
+ max = T[i];
+
+ /* We should have: max(T) == res[SIZE] */
+ return !(max == res[SIZE-1]);
+}
+#endif // __UNIT_TEST_SELECTION__
+
diff --git a/test/kvx/sort/selection.h b/test/kvx/sort/selection.h
new file mode 100644
index 00000000..92a6b461
--- /dev/null
+++ b/test/kvx/sort/selection.h
@@ -0,0 +1,6 @@
+#ifndef __SELECTION_H__
+#define __SELECTION_H__
+
+int select_sort(uint64_t *res, const uint64_t *T);
+
+#endif // __SELECTION_H__
diff --git a/test/kvx/sort/test.h b/test/kvx/sort/test.h
new file mode 100644
index 00000000..4501ee38
--- /dev/null
+++ b/test/kvx/sort/test.h
@@ -0,0 +1,6 @@
+#ifndef __TEST_H__
+#define __TEST_H__
+
+#define SIZE 100
+
+#endif
diff --git a/test/monniaux/.gitignore b/test/monniaux/.gitignore
new file mode 100644
index 00000000..4ebc3cde
--- /dev/null
+++ b/test/monniaux/.gitignore
@@ -0,0 +1,14 @@
+**.host
+**.kvx
+**measures.csv
+
+commands.txt
+oracle_times.txt
+verifier_times.txt
+compile_times.pdf
+measure_times.host.pdf
+measure_times.kvx.pdf
+
+/.mypy_cache/
+
+mbedtls/mbedtls/
diff --git a/test/monniaux/Asmblockdeps.patch b/test/monniaux/Asmblockdeps.patch
new file mode 100644
index 00000000..a2d8c7be
--- /dev/null
+++ b/test/monniaux/Asmblockdeps.patch
@@ -0,0 +1,20 @@
+--- extraction/Asmblockdeps.ml 2019-06-13 15:06:55.493592984 +0200
++++ Asmblockdeps.mod.ml 2019-06-13 15:04:55.900122958 +0200
+@@ -2243,5 +2243,15 @@
+
+ (** val bblock_simub : bblock -> bblock -> bool **)
+
+-let bblock_simub =
+- pure_bblock_simu_test true
++let bblock_simub bb tbb =
++ let nb_instructions = Camlcoq.Z.to_int64 @@ Asmvliw.size bb
++ in let start_time = (Gc.major(); (Unix.times ()).Unix.tms_utime)
++ in let simub = pure_bblock_simu_test true bb tbb
++ in let refer = ref false
++ in begin
++ for i = 1 to 1000-1 do
++ refer := (if i > 0 then pure_bblock_simu_test true bb tbb else false); (* dumb i > 0 test to lure warning 35 *)
++ done;
++ Printf.printf "%Ld: %f\n" nb_instructions ((Unix.times ()).Unix.tms_utime -. start_time);
++ simub
++ end
diff --git a/test/monniaux/BearSSL/.gitignore b/test/monniaux/BearSSL/.gitignore
new file mode 100644
index 00000000..7da362ed
--- /dev/null
+++ b/test/monniaux/BearSSL/.gitignore
@@ -0,0 +1,6 @@
+/build/
+/libbearssl.a
+/brssl
+/testcrypto
+/testspeed
+/testx509
diff --git a/test/monniaux/BearSSL/Doxyfile b/test/monniaux/BearSSL/Doxyfile
new file mode 100644
index 00000000..088682db
--- /dev/null
+++ b/test/monniaux/BearSSL/Doxyfile
@@ -0,0 +1,2427 @@
+# Doxyfile 1.8.11
+
+# This file describes the settings to be used by the documentation system
+# doxygen (www.doxygen.org) for a project.
+#
+# All text after a double hash (##) is considered a comment and is placed in
+# front of the TAG it is preceding.
+#
+# All text after a single hash (#) is considered a comment and will be ignored.
+# The format is:
+# TAG = value [value, ...]
+# For lists, items can also be appended using:
+# TAG += value [value, ...]
+# Values that contain spaces should be placed between quotes (\" \").
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+
+# This tag specifies the encoding used for all characters in the config file
+# that follow. The default is UTF-8 which is also the encoding used for all text
+# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv
+# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv
+# for the list of possible encodings.
+# The default value is: UTF-8.
+
+DOXYFILE_ENCODING = UTF-8
+
+# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by
+# double-quotes, unless you are using Doxywizard) that should identify the
+# project for which the documentation is generated. This name is used in the
+# title of most generated pages and in a few other places.
+# The default value is: My Project.
+
+PROJECT_NAME = "BearSSL"
+
+# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
+# could be handy for archiving the generated documentation or if some version
+# control system is used.
+
+PROJECT_NUMBER =
+
+# Using the PROJECT_BRIEF tag one can provide an optional one line description
+# for a project that appears at the top of each page and should give viewer a
+# quick idea about the purpose of the project. Keep the description short.
+
+PROJECT_BRIEF =
+
+# With the PROJECT_LOGO tag one can specify a logo or an icon that is included
+# in the documentation. The maximum height of the logo should not exceed 55
+# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
+# the logo to the output directory.
+
+PROJECT_LOGO =
+
+# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
+# into which the generated documentation will be written. If a relative path is
+# entered, it will be relative to the location where doxygen was started. If
+# left blank the current directory will be used.
+
+OUTPUT_DIRECTORY = apidoc
+
+# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
+# directories (in 2 levels) under the output directory of each output format and
+# will distribute the generated files over these directories. Enabling this
+# option can be useful when feeding doxygen a huge amount of source files, where
+# putting all generated files in the same directory would otherwise causes
+# performance problems for the file system.
+# The default value is: NO.
+
+CREATE_SUBDIRS = NO
+
+# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII
+# characters to appear in the names of generated files. If set to NO, non-ASCII
+# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode
+# U+3044.
+# The default value is: NO.
+
+ALLOW_UNICODE_NAMES = NO
+
+# The OUTPUT_LANGUAGE tag is used to specify the language in which all
+# documentation generated by doxygen is written. Doxygen will use this
+# information to generate all constant output in the proper language.
+# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese,
+# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States),
+# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian,
+# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages),
+# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian,
+# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian,
+# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish,
+# Ukrainian and Vietnamese.
+# The default value is: English.
+
+OUTPUT_LANGUAGE = English
+
+# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member
+# descriptions after the members that are listed in the file and class
+# documentation (similar to Javadoc). Set to NO to disable this.
+# The default value is: YES.
+
+BRIEF_MEMBER_DESC = YES
+
+# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief
+# description of a member or function before the detailed description
+#
+# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the
+# brief descriptions will be completely suppressed.
+# The default value is: YES.
+
+REPEAT_BRIEF = YES
+
+# This tag implements a quasi-intelligent brief description abbreviator that is
+# used to form the text in various listings. Each string in this list, if found
+# as the leading text of the brief description, will be stripped from the text
+# and the result, after processing the whole list, is used as the annotated
+# text. Otherwise, the brief description is used as-is. If left blank, the
+# following values are used ($name is automatically replaced with the name of
+# the entity):The $name class, The $name widget, The $name file, is, provides,
+# specifies, contains, represents, a, an and the.
+
+ABBREVIATE_BRIEF =
+
+# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then
+# doxygen will generate a detailed section even if there is only a brief
+# description.
+# The default value is: NO.
+
+ALWAYS_DETAILED_SEC = NO
+
+# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all
+# inherited members of a class in the documentation of that class as if those
+# members were ordinary class members. Constructors, destructors and assignment
+# operators of the base classes will not be shown.
+# The default value is: NO.
+
+INLINE_INHERITED_MEMB = NO
+
+# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path
+# before files name in the file list and in the header files. If set to NO the
+# shortest path that makes the file name unique will be used
+# The default value is: YES.
+
+FULL_PATH_NAMES = NO
+
+# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path.
+# Stripping is only done if one of the specified strings matches the left-hand
+# part of the path. The tag can be used to show relative paths in the file list.
+# If left blank the directory from which doxygen is run is used as the path to
+# strip.
+#
+# Note that you can specify absolute paths here, but also relative paths, which
+# will be relative from the directory where doxygen is started.
+# This tag requires that the tag FULL_PATH_NAMES is set to YES.
+
+STRIP_FROM_PATH =
+
+# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the
+# path mentioned in the documentation of a class, which tells the reader which
+# header file to include in order to use a class. If left blank only the name of
+# the header file containing the class definition is used. Otherwise one should
+# specify the list of include paths that are normally passed to the compiler
+# using the -I flag.
+
+STRIP_FROM_INC_PATH =
+
+# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but
+# less readable) file names. This can be useful is your file systems doesn't
+# support long names like on DOS, Mac, or CD-ROM.
+# The default value is: NO.
+
+SHORT_NAMES = NO
+
+# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the
+# first line (until the first dot) of a Javadoc-style comment as the brief
+# description. If set to NO, the Javadoc-style will behave just like regular Qt-
+# style comments (thus requiring an explicit @brief command for a brief
+# description.)
+# The default value is: NO.
+
+JAVADOC_AUTOBRIEF = NO
+
+# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first
+# line (until the first dot) of a Qt-style comment as the brief description. If
+# set to NO, the Qt-style will behave just like regular Qt-style comments (thus
+# requiring an explicit \brief command for a brief description.)
+# The default value is: NO.
+
+QT_AUTOBRIEF = NO
+
+# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a
+# multi-line C++ special comment block (i.e. a block of //! or /// comments) as
+# a brief description. This used to be the default behavior. The new default is
+# to treat a multi-line C++ comment block as a detailed description. Set this
+# tag to YES if you prefer the old behavior instead.
+#
+# Note that setting this tag to YES also means that rational rose comments are
+# not recognized any more.
+# The default value is: NO.
+
+MULTILINE_CPP_IS_BRIEF = NO
+
+# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the
+# documentation from any documented member that it re-implements.
+# The default value is: YES.
+
+INHERIT_DOCS = YES
+
+# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new
+# page for each member. If set to NO, the documentation of a member will be part
+# of the file/class/namespace that contains it.
+# The default value is: NO.
+
+SEPARATE_MEMBER_PAGES = NO
+
+# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen
+# uses this value to replace tabs by spaces in code fragments.
+# Minimum value: 1, maximum value: 16, default value: 4.
+
+TAB_SIZE = 8
+
+# This tag can be used to specify a number of aliases that act as commands in
+# the documentation. An alias has the form:
+# name=value
+# For example adding
+# "sideeffect=@par Side Effects:\n"
+# will allow you to put the command \sideeffect (or @sideeffect) in the
+# documentation, which will result in a user-defined paragraph with heading
+# "Side Effects:". You can put \n's in the value part of an alias to insert
+# newlines.
+
+ALIASES =
+
+# This tag can be used to specify a number of word-keyword mappings (TCL only).
+# A mapping has the form "name=value". For example adding "class=itcl::class"
+# will allow you to use the command class in the itcl::class meaning.
+
+TCL_SUBST =
+
+# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources
+# only. Doxygen will then generate output that is more tailored for C. For
+# instance, some of the names that are used will be different. The list of all
+# members will be omitted, etc.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_FOR_C = YES
+
+# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or
+# Python sources only. Doxygen will then generate output that is more tailored
+# for that language. For instance, namespaces will be presented as packages,
+# qualified scopes will look different, etc.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_JAVA = NO
+
+# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran
+# sources. Doxygen will then generate output that is tailored for Fortran.
+# The default value is: NO.
+
+OPTIMIZE_FOR_FORTRAN = NO
+
+# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL
+# sources. Doxygen will then generate output that is tailored for VHDL.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_VHDL = NO
+
+# Doxygen selects the parser to use depending on the extension of the files it
+# parses. With this tag you can assign which parser to use for a given
+# extension. Doxygen has a built-in mapping, but you can override or extend it
+# using this tag. The format is ext=language, where ext is a file extension, and
+# language is one of the parsers supported by doxygen: IDL, Java, Javascript,
+# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran:
+# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran:
+# Fortran. In the later case the parser tries to guess whether the code is fixed
+# or free formatted code, this is the default for Fortran type files), VHDL. For
+# instance to make doxygen treat .inc files as Fortran files (default is PHP),
+# and .f files as C (default is Fortran), use: inc=Fortran f=C.
+#
+# Note: For files without extension you can use no_extension as a placeholder.
+#
+# Note that for custom extensions you also need to set FILE_PATTERNS otherwise
+# the files are not read by doxygen.
+
+EXTENSION_MAPPING =
+
+# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments
+# according to the Markdown format, which allows for more readable
+# documentation. See http://daringfireball.net/projects/markdown/ for details.
+# The output of markdown processing is further processed by doxygen, so you can
+# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in
+# case of backward compatibilities issues.
+# The default value is: YES.
+
+MARKDOWN_SUPPORT = YES
+
+# When enabled doxygen tries to link words that correspond to documented
+# classes, or namespaces to their corresponding documentation. Such a link can
+# be prevented in individual cases by putting a % sign in front of the word or
+# globally by setting AUTOLINK_SUPPORT to NO.
+# The default value is: YES.
+
+AUTOLINK_SUPPORT = YES
+
+# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want
+# to include (a tag file for) the STL sources as input, then you should set this
+# tag to YES in order to let doxygen match functions declarations and
+# definitions whose arguments contain STL classes (e.g. func(std::string);
+# versus func(std::string) {}). This also make the inheritance and collaboration
+# diagrams that involve STL classes more complete and accurate.
+# The default value is: NO.
+
+BUILTIN_STL_SUPPORT = NO
+
+# If you use Microsoft's C++/CLI language, you should set this option to YES to
+# enable parsing support.
+# The default value is: NO.
+
+CPP_CLI_SUPPORT = NO
+
+# Set the SIP_SUPPORT tag to YES if your project consists of sip (see:
+# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen
+# will parse them like normal C++ but will assume all classes use public instead
+# of private inheritance when no explicit protection keyword is present.
+# The default value is: NO.
+
+SIP_SUPPORT = NO
+
+# For Microsoft's IDL there are propget and propput attributes to indicate
+# getter and setter methods for a property. Setting this option to YES will make
+# doxygen to replace the get and set methods by a property in the documentation.
+# This will only work if the methods are indeed getting or setting a simple
+# type. If this is not the case, or you want to show the methods anyway, you
+# should set this option to NO.
+# The default value is: YES.
+
+IDL_PROPERTY_SUPPORT = YES
+
+# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC
+# tag is set to YES then doxygen will reuse the documentation of the first
+# member in the group (if any) for the other members of the group. By default
+# all members of a group must be documented explicitly.
+# The default value is: NO.
+
+DISTRIBUTE_GROUP_DOC = NO
+
+# If one adds a struct or class to a group and this option is enabled, then also
+# any nested class or struct is added to the same group. By default this option
+# is disabled and one has to add nested compounds explicitly via \ingroup.
+# The default value is: NO.
+
+GROUP_NESTED_COMPOUNDS = NO
+
+# Set the SUBGROUPING tag to YES to allow class member groups of the same type
+# (for instance a group of public functions) to be put as a subgroup of that
+# type (e.g. under the Public Functions section). Set it to NO to prevent
+# subgrouping. Alternatively, this can be done per class using the
+# \nosubgrouping command.
+# The default value is: YES.
+
+SUBGROUPING = YES
+
+# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions
+# are shown inside the group in which they are included (e.g. using \ingroup)
+# instead of on a separate page (for HTML and Man pages) or section (for LaTeX
+# and RTF).
+#
+# Note that this feature does not work in combination with
+# SEPARATE_MEMBER_PAGES.
+# The default value is: NO.
+
+INLINE_GROUPED_CLASSES = NO
+
+# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions
+# with only public data fields or simple typedef fields will be shown inline in
+# the documentation of the scope in which they are defined (i.e. file,
+# namespace, or group documentation), provided this scope is documented. If set
+# to NO, structs, classes, and unions are shown on a separate page (for HTML and
+# Man pages) or section (for LaTeX and RTF).
+# The default value is: NO.
+
+INLINE_SIMPLE_STRUCTS = NO
+
+# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or
+# enum is documented as struct, union, or enum with the name of the typedef. So
+# typedef struct TypeS {} TypeT, will appear in the documentation as a struct
+# with name TypeT. When disabled the typedef will appear as a member of a file,
+# namespace, or class. And the struct will be named TypeS. This can typically be
+# useful for C code in case the coding convention dictates that all compound
+# types are typedef'ed and only the typedef is referenced, never the tag name.
+# The default value is: NO.
+
+TYPEDEF_HIDES_STRUCT = YES
+
+# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This
+# cache is used to resolve symbols given their name and scope. Since this can be
+# an expensive process and often the same symbol appears multiple times in the
+# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small
+# doxygen will become slower. If the cache is too large, memory is wasted. The
+# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range
+# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536
+# symbols. At the end of a run doxygen will report the cache usage and suggest
+# the optimal cache size from a speed point of view.
+# Minimum value: 0, maximum value: 9, default value: 0.
+
+LOOKUP_CACHE_SIZE = 0
+
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+
+# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in
+# documentation are documented, even if no documentation was available. Private
+# class members and static file members will be hidden unless the
+# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES.
+# Note: This will also disable the warnings about undocumented members that are
+# normally produced when WARNINGS is set to YES.
+# The default value is: NO.
+
+EXTRACT_ALL = YES
+
+# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
+# be included in the documentation.
+# The default value is: NO.
+
+EXTRACT_PRIVATE = NO
+
+# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal
+# scope will be included in the documentation.
+# The default value is: NO.
+
+EXTRACT_PACKAGE = NO
+
+# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be
+# included in the documentation.
+# The default value is: NO.
+
+EXTRACT_STATIC = YES
+
+# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined
+# locally in source files will be included in the documentation. If set to NO,
+# only classes defined in header files are included. Does not have any effect
+# for Java sources.
+# The default value is: YES.
+
+EXTRACT_LOCAL_CLASSES = YES
+
+# This flag is only useful for Objective-C code. If set to YES, local methods,
+# which are defined in the implementation section but not in the interface are
+# included in the documentation. If set to NO, only methods in the interface are
+# included.
+# The default value is: NO.
+
+EXTRACT_LOCAL_METHODS = NO
+
+# If this flag is set to YES, the members of anonymous namespaces will be
+# extracted and appear in the documentation as a namespace called
+# 'anonymous_namespace{file}', where file will be replaced with the base name of
+# the file that contains the anonymous namespace. By default anonymous namespace
+# are hidden.
+# The default value is: NO.
+
+EXTRACT_ANON_NSPACES = NO
+
+# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all
+# undocumented members inside documented classes or files. If set to NO these
+# members will be included in the various overviews, but no documentation
+# section is generated. This option has no effect if EXTRACT_ALL is enabled.
+# The default value is: NO.
+
+HIDE_UNDOC_MEMBERS = NO
+
+# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
+# undocumented classes that are normally visible in the class hierarchy. If set
+# to NO, these classes will be included in the various overviews. This option
+# has no effect if EXTRACT_ALL is enabled.
+# The default value is: NO.
+
+HIDE_UNDOC_CLASSES = NO
+
+# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend
+# (class|struct|union) declarations. If set to NO, these declarations will be
+# included in the documentation.
+# The default value is: NO.
+
+HIDE_FRIEND_COMPOUNDS = NO
+
+# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any
+# documentation blocks found inside the body of a function. If set to NO, these
+# blocks will be appended to the function's detailed documentation block.
+# The default value is: NO.
+
+HIDE_IN_BODY_DOCS = NO
+
+# The INTERNAL_DOCS tag determines if documentation that is typed after a
+# \internal command is included. If the tag is set to NO then the documentation
+# will be excluded. Set it to YES to include the internal documentation.
+# The default value is: NO.
+
+INTERNAL_DOCS = NO
+
+# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file
+# names in lower-case letters. If set to YES, upper-case letters are also
+# allowed. This is useful if you have classes or files whose names only differ
+# in case and if your file system supports case sensitive file names. Windows
+# and Mac users are advised to set this option to NO.
+# The default value is: system dependent.
+
+CASE_SENSE_NAMES = YES
+
+# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with
+# their full class and namespace scopes in the documentation. If set to YES, the
+# scope will be hidden.
+# The default value is: NO.
+
+HIDE_SCOPE_NAMES = NO
+
+# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will
+# append additional text to a page's title, such as Class Reference. If set to
+# YES the compound reference will be hidden.
+# The default value is: NO.
+
+HIDE_COMPOUND_REFERENCE= NO
+
+# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of
+# the files that are included by a file in the documentation of that file.
+# The default value is: YES.
+
+SHOW_INCLUDE_FILES = NO
+
+# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each
+# grouped member an include statement to the documentation, telling the reader
+# which file to include in order to use the member.
+# The default value is: NO.
+
+SHOW_GROUPED_MEMB_INC = NO
+
+# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include
+# files with double quotes in the documentation rather than with sharp brackets.
+# The default value is: NO.
+
+FORCE_LOCAL_INCLUDES = NO
+
+# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the
+# documentation for inline members.
+# The default value is: YES.
+
+INLINE_INFO = YES
+
+# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the
+# (detailed) documentation of file and class members alphabetically by member
+# name. If set to NO, the members will appear in declaration order.
+# The default value is: YES.
+
+SORT_MEMBER_DOCS = YES
+
+# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief
+# descriptions of file, namespace and class members alphabetically by member
+# name. If set to NO, the members will appear in declaration order. Note that
+# this will also influence the order of the classes in the class list.
+# The default value is: NO.
+
+SORT_BRIEF_DOCS = NO
+
+# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the
+# (brief and detailed) documentation of class members so that constructors and
+# destructors are listed first. If set to NO the constructors will appear in the
+# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS.
+# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief
+# member documentation.
+# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting
+# detailed member documentation.
+# The default value is: NO.
+
+SORT_MEMBERS_CTORS_1ST = NO
+
+# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy
+# of group names into alphabetical order. If set to NO the group names will
+# appear in their defined order.
+# The default value is: NO.
+
+SORT_GROUP_NAMES = NO
+
+# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by
+# fully-qualified names, including namespaces. If set to NO, the class list will
+# be sorted only by class name, not including the namespace part.
+# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES.
+# Note: This option applies only to the class list, not to the alphabetical
+# list.
+# The default value is: NO.
+
+SORT_BY_SCOPE_NAME = NO
+
+# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper
+# type resolution of all parameters of a function it will reject a match between
+# the prototype and the implementation of a member function even if there is
+# only one candidate or it is obvious which candidate to choose by doing a
+# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still
+# accept a match between prototype and implementation in such cases.
+# The default value is: NO.
+
+STRICT_PROTO_MATCHING = NO
+
+# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo
+# list. This list is created by putting \todo commands in the documentation.
+# The default value is: YES.
+
+GENERATE_TODOLIST = YES
+
+# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test
+# list. This list is created by putting \test commands in the documentation.
+# The default value is: YES.
+
+GENERATE_TESTLIST = YES
+
+# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug
+# list. This list is created by putting \bug commands in the documentation.
+# The default value is: YES.
+
+GENERATE_BUGLIST = YES
+
+# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO)
+# the deprecated list. This list is created by putting \deprecated commands in
+# the documentation.
+# The default value is: YES.
+
+GENERATE_DEPRECATEDLIST= YES
+
+# The ENABLED_SECTIONS tag can be used to enable conditional documentation
+# sections, marked by \if <section_label> ... \endif and \cond <section_label>
+# ... \endcond blocks.
+
+ENABLED_SECTIONS =
+
+# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the
+# initial value of a variable or macro / define can have for it to appear in the
+# documentation. If the initializer consists of more lines than specified here
+# it will be hidden. Use a value of 0 to hide initializers completely. The
+# appearance of the value of individual variables and macros / defines can be
+# controlled using \showinitializer or \hideinitializer command in the
+# documentation regardless of this setting.
+# Minimum value: 0, maximum value: 10000, default value: 30.
+
+MAX_INITIALIZER_LINES = 30
+
+# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at
+# the bottom of the documentation of classes and structs. If set to YES, the
+# list will mention the files that were used to generate the documentation.
+# The default value is: YES.
+
+SHOW_USED_FILES = YES
+
+# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This
+# will remove the Files entry from the Quick Index and from the Folder Tree View
+# (if specified).
+# The default value is: YES.
+
+SHOW_FILES = YES
+
+# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces
+# page. This will remove the Namespaces entry from the Quick Index and from the
+# Folder Tree View (if specified).
+# The default value is: YES.
+
+SHOW_NAMESPACES = YES
+
+# The FILE_VERSION_FILTER tag can be used to specify a program or script that
+# doxygen should invoke to get the current version for each file (typically from
+# the version control system). Doxygen will invoke the program by executing (via
+# popen()) the command command input-file, where command is the value of the
+# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided
+# by doxygen. Whatever the program writes to standard output is used as the file
+# version. For an example see the documentation.
+
+FILE_VERSION_FILTER =
+
+# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed
+# by doxygen. The layout file controls the global structure of the generated
+# output files in an output format independent way. To create the layout file
+# that represents doxygen's defaults, run doxygen with the -l option. You can
+# optionally specify a file name after the option, if omitted DoxygenLayout.xml
+# will be used as the name of the layout file.
+#
+# Note that if you run doxygen from a directory containing a file called
+# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE
+# tag is left empty.
+
+LAYOUT_FILE =
+
+# The CITE_BIB_FILES tag can be used to specify one or more bib files containing
+# the reference definitions. This must be a list of .bib files. The .bib
+# extension is automatically appended if omitted. This requires the bibtex tool
+# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info.
+# For LaTeX the style of the bibliography can be controlled using
+# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the
+# search path. See also \cite for info how to create references.
+
+CITE_BIB_FILES =
+
+#---------------------------------------------------------------------------
+# Configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+
+# The QUIET tag can be used to turn on/off the messages that are generated to
+# standard output by doxygen. If QUIET is set to YES this implies that the
+# messages are off.
+# The default value is: NO.
+
+QUIET = NO
+
+# The WARNINGS tag can be used to turn on/off the warning messages that are
+# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES
+# this implies that the warnings are on.
+#
+# Tip: Turn warnings on while writing the documentation.
+# The default value is: YES.
+
+WARNINGS = YES
+
+# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate
+# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag
+# will automatically be disabled.
+# The default value is: YES.
+
+WARN_IF_UNDOCUMENTED = YES
+
+# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for
+# potential errors in the documentation, such as not documenting some parameters
+# in a documented function, or documenting parameters that don't exist or using
+# markup commands wrongly.
+# The default value is: YES.
+
+WARN_IF_DOC_ERROR = YES
+
+# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that
+# are documented, but have no documentation for their parameters or return
+# value. If set to NO, doxygen will only warn about wrong or incomplete
+# parameter documentation, but not about the absence of documentation.
+# The default value is: NO.
+
+WARN_NO_PARAMDOC = NO
+
+# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when
+# a warning is encountered.
+# The default value is: NO.
+
+WARN_AS_ERROR = NO
+
+# The WARN_FORMAT tag determines the format of the warning messages that doxygen
+# can produce. The string should contain the $file, $line, and $text tags, which
+# will be replaced by the file and line number from which the warning originated
+# and the warning text. Optionally the format may contain $version, which will
+# be replaced by the version of the file (if it could be obtained via
+# FILE_VERSION_FILTER)
+# The default value is: $file:$line: $text.
+
+WARN_FORMAT = "$file:$line: $text"
+
+# The WARN_LOGFILE tag can be used to specify a file to which warning and error
+# messages should be written. If left blank the output is written to standard
+# error (stderr).
+
+WARN_LOGFILE =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the input files
+#---------------------------------------------------------------------------
+
+# The INPUT tag is used to specify the files and/or directories that contain
+# documented source files. You may enter file names like myfile.cpp or
+# directories like /usr/src/myproject. Separate the files or directories with
+# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
+# Note: If this tag is empty the current directory is searched.
+
+INPUT = inc/bearssl.h inc/bearssl_aead.h inc/bearssl_block.h inc/bearssl_ec.h inc/bearssl_hash.h inc/bearssl_hmac.h inc/bearssl_kdf.h inc/bearssl_pem.h inc/bearssl_prf.h inc/bearssl_rand.h inc/bearssl_rsa.h inc/bearssl_ssl.h inc/bearssl_x509.h
+
+# This tag can be used to specify the character encoding of the source files
+# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
+# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
+# documentation (see: http://www.gnu.org/software/libiconv) for the list of
+# possible encodings.
+# The default value is: UTF-8.
+
+INPUT_ENCODING = UTF-8
+
+# If the value of the INPUT tag contains directories, you can use the
+# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and
+# *.h) to filter out the source-files in the directories.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# read by doxygen.
+#
+# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp,
+# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h,
+# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc,
+# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f, *.for, *.tcl,
+# *.vhd, *.vhdl, *.ucf, *.qsf, *.as and *.js.
+
+FILE_PATTERNS =
+
+# The RECURSIVE tag can be used to specify whether or not subdirectories should
+# be searched for input files as well.
+# The default value is: NO.
+
+RECURSIVE = NO
+
+# The EXCLUDE tag can be used to specify files and/or directories that should be
+# excluded from the INPUT source files. This way you can easily exclude a
+# subdirectory from a directory tree whose root is specified with the INPUT tag.
+#
+# Note that relative paths are relative to the directory from which doxygen is
+# run.
+
+EXCLUDE =
+
+# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
+# directories that are symbolic links (a Unix file system feature) are excluded
+# from the input.
+# The default value is: NO.
+
+EXCLUDE_SYMLINKS = NO
+
+# If the value of the INPUT tag contains directories, you can use the
+# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
+# certain files from those directories.
+#
+# Note that the wildcards are matched against the file with absolute path, so to
+# exclude all test directories for example use the pattern */test/*
+
+EXCLUDE_PATTERNS =
+
+# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
+# (namespaces, classes, functions, etc.) that should be excluded from the
+# output. The symbol name can be a fully qualified name, a word, or if the
+# wildcard * is used, a substring. Examples: ANamespace, AClass,
+# AClass::ANamespace, ANamespace::*Test
+#
+# Note that the wildcards are matched against the file with absolute path, so to
+# exclude all test directories use the pattern */test/*
+
+EXCLUDE_SYMBOLS =
+
+# The EXAMPLE_PATH tag can be used to specify one or more files or directories
+# that contain example code fragments that are included (see the \include
+# command).
+
+EXAMPLE_PATH =
+
+# If the value of the EXAMPLE_PATH tag contains directories, you can use the
+# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and
+# *.h) to filter out the source-files in the directories. If left blank all
+# files are included.
+
+EXAMPLE_PATTERNS =
+
+# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be
+# searched for input files to be used with the \include or \dontinclude commands
+# irrespective of the value of the RECURSIVE tag.
+# The default value is: NO.
+
+EXAMPLE_RECURSIVE = NO
+
+# The IMAGE_PATH tag can be used to specify one or more files or directories
+# that contain images that are to be included in the documentation (see the
+# \image command).
+
+IMAGE_PATH =
+
+# The INPUT_FILTER tag can be used to specify a program that doxygen should
+# invoke to filter for each input file. Doxygen will invoke the filter program
+# by executing (via popen()) the command:
+#
+# <filter> <input-file>
+#
+# where <filter> is the value of the INPUT_FILTER tag, and <input-file> is the
+# name of an input file. Doxygen will then use the output that the filter
+# program writes to standard output. If FILTER_PATTERNS is specified, this tag
+# will be ignored.
+#
+# Note that the filter must not add or remove lines; it is applied before the
+# code is scanned, but not when the output code is generated. If lines are added
+# or removed, the anchors will not be placed correctly.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# properly processed by doxygen.
+
+INPUT_FILTER =
+
+# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern
+# basis. Doxygen will compare the file name with each pattern and apply the
+# filter if there is a match. The filters are a list of the form: pattern=filter
+# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how
+# filters are used. If the FILTER_PATTERNS tag is empty or if none of the
+# patterns match the file name, INPUT_FILTER is applied.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# properly processed by doxygen.
+
+FILTER_PATTERNS =
+
+# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using
+# INPUT_FILTER) will also be used to filter the input files that are used for
+# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES).
+# The default value is: NO.
+
+FILTER_SOURCE_FILES = NO
+
+# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file
+# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and
+# it is also possible to disable source filtering for a specific pattern using
+# *.ext= (so without naming a filter).
+# This tag requires that the tag FILTER_SOURCE_FILES is set to YES.
+
+FILTER_SOURCE_PATTERNS =
+
+# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that
+# is part of the input, its contents will be placed on the main page
+# (index.html). This can be useful if you have a project on for instance GitHub
+# and want to reuse the introduction page also for the doxygen output.
+
+USE_MDFILE_AS_MAINPAGE =
+
+#---------------------------------------------------------------------------
+# Configuration options related to source browsing
+#---------------------------------------------------------------------------
+
+# If the SOURCE_BROWSER tag is set to YES then a list of source files will be
+# generated. Documented entities will be cross-referenced with these sources.
+#
+# Note: To get rid of all source code in the generated output, make sure that
+# also VERBATIM_HEADERS is set to NO.
+# The default value is: NO.
+
+SOURCE_BROWSER = NO
+
+# Setting the INLINE_SOURCES tag to YES will include the body of functions,
+# classes and enums directly into the documentation.
+# The default value is: NO.
+
+INLINE_SOURCES = NO
+
+# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any
+# special comment blocks from generated source code fragments. Normal C, C++ and
+# Fortran comments will always remain visible.
+# The default value is: YES.
+
+STRIP_CODE_COMMENTS = NO
+
+# If the REFERENCED_BY_RELATION tag is set to YES then for each documented
+# function all documented functions referencing it will be listed.
+# The default value is: NO.
+
+REFERENCED_BY_RELATION = NO
+
+# If the REFERENCES_RELATION tag is set to YES then for each documented function
+# all documented entities called/used by that function will be listed.
+# The default value is: NO.
+
+REFERENCES_RELATION = NO
+
+# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set
+# to YES then the hyperlinks from functions in REFERENCES_RELATION and
+# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will
+# link to the documentation.
+# The default value is: YES.
+
+REFERENCES_LINK_SOURCE = YES
+
+# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the
+# source code will show a tooltip with additional information such as prototype,
+# brief description and links to the definition and documentation. Since this
+# will make the HTML file larger and loading of large files a bit slower, you
+# can opt to disable this feature.
+# The default value is: YES.
+# This tag requires that the tag SOURCE_BROWSER is set to YES.
+
+SOURCE_TOOLTIPS = YES
+
+# If the USE_HTAGS tag is set to YES then the references to source code will
+# point to the HTML generated by the htags(1) tool instead of doxygen built-in
+# source browser. The htags tool is part of GNU's global source tagging system
+# (see http://www.gnu.org/software/global/global.html). You will need version
+# 4.8.6 or higher.
+#
+# To use it do the following:
+# - Install the latest version of global
+# - Enable SOURCE_BROWSER and USE_HTAGS in the config file
+# - Make sure the INPUT points to the root of the source tree
+# - Run doxygen as normal
+#
+# Doxygen will invoke htags (and that will in turn invoke gtags), so these
+# tools must be available from the command line (i.e. in the search path).
+#
+# The result: instead of the source browser generated by doxygen, the links to
+# source code will now point to the output of htags.
+# The default value is: NO.
+# This tag requires that the tag SOURCE_BROWSER is set to YES.
+
+USE_HTAGS = NO
+
+# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a
+# verbatim copy of the header file for each class for which an include is
+# specified. Set to NO to disable this.
+# See also: Section \class.
+# The default value is: YES.
+
+VERBATIM_HEADERS = YES
+
+# If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the
+# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the
+# cost of reduced performance. This can be particularly helpful with template
+# rich C++ code for which doxygen's built-in parser lacks the necessary type
+# information.
+# Note: The availability of this option depends on whether or not doxygen was
+# generated with the -Duse-libclang=ON option for CMake.
+# The default value is: NO.
+
+CLANG_ASSISTED_PARSING = NO
+
+# If clang assisted parsing is enabled you can provide the compiler with command
+# line options that you would normally use when invoking the compiler. Note that
+# the include paths will already be set by doxygen for the files and directories
+# specified with INPUT and INCLUDE_PATH.
+# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES.
+
+CLANG_OPTIONS =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+
+# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all
+# compounds will be generated. Enable this if the project contains a lot of
+# classes, structs, unions or interfaces.
+# The default value is: YES.
+
+ALPHABETICAL_INDEX = YES
+
+# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in
+# which the alphabetical index list will be split.
+# Minimum value: 1, maximum value: 20, default value: 5.
+# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.
+
+COLS_IN_ALPHA_INDEX = 5
+
+# In case all classes in a project start with a common prefix, all classes will
+# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag
+# can be used to specify a prefix (or a list of prefixes) that should be ignored
+# while generating the index headers.
+# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.
+
+IGNORE_PREFIX = br_ BR_
+
+#---------------------------------------------------------------------------
+# Configuration options related to the HTML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output
+# The default value is: YES.
+
+GENERATE_HTML = YES
+
+# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: html.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_OUTPUT = html
+
+# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each
+# generated HTML page (for example: .htm, .php, .asp).
+# The default value is: .html.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_FILE_EXTENSION = .html
+
+# The HTML_HEADER tag can be used to specify a user-defined HTML header file for
+# each generated HTML page. If the tag is left blank doxygen will generate a
+# standard header.
+#
+# To get valid HTML the header file that includes any scripts and style sheets
+# that doxygen needs, which is dependent on the configuration options used (e.g.
+# the setting GENERATE_TREEVIEW). It is highly recommended to start with a
+# default header using
+# doxygen -w html new_header.html new_footer.html new_stylesheet.css
+# YourConfigFile
+# and then modify the file new_header.html. See also section "Doxygen usage"
+# for information on how to generate the default header that doxygen normally
+# uses.
+# Note: The header is subject to change so you typically have to regenerate the
+# default header when upgrading to a newer version of doxygen. For a description
+# of the possible markers and block names see the documentation.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_HEADER =
+
+# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each
+# generated HTML page. If the tag is left blank doxygen will generate a standard
+# footer. See HTML_HEADER for more information on how to generate a default
+# footer and what special commands can be used inside the footer. See also
+# section "Doxygen usage" for information on how to generate the default footer
+# that doxygen normally uses.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_FOOTER =
+
+# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style
+# sheet that is used by each HTML page. It can be used to fine-tune the look of
+# the HTML output. If left blank doxygen will generate a default style sheet.
+# See also section "Doxygen usage" for information on how to generate the style
+# sheet that doxygen normally uses.
+# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as
+# it is more robust and this tag (HTML_STYLESHEET) will in the future become
+# obsolete.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_STYLESHEET =
+
+# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined
+# cascading style sheets that are included after the standard style sheets
+# created by doxygen. Using this option one can overrule certain style aspects.
+# This is preferred over using HTML_STYLESHEET since it does not replace the
+# standard style sheet and is therefore more robust against future updates.
+# Doxygen will copy the style sheet files to the output directory.
+# Note: The order of the extra style sheet files is of importance (e.g. the last
+# style sheet in the list overrules the setting of the previous ones in the
+# list). For an example see the documentation.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_EXTRA_STYLESHEET =
+
+# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or
+# other source files which should be copied to the HTML output directory. Note
+# that these files will be copied to the base HTML output directory. Use the
+# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these
+# files. In the HTML_STYLESHEET file, use the file name only. Also note that the
+# files will be copied as-is; there are no commands or markers available.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_EXTRA_FILES =
+
+# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen
+# will adjust the colors in the style sheet and background images according to
+# this color. Hue is specified as an angle on a colorwheel, see
+# http://en.wikipedia.org/wiki/Hue for more information. For instance the value
+# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300
+# purple, and 360 is red again.
+# Minimum value: 0, maximum value: 359, default value: 220.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_HUE = 45
+
+# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors
+# in the HTML output. For a value of 0 the output will use grayscales only. A
+# value of 255 will produce the most vivid colors.
+# Minimum value: 0, maximum value: 255, default value: 100.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_SAT = 150
+
+# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the
+# luminance component of the colors in the HTML output. Values below 100
+# gradually make the output lighter, whereas values above 100 make the output
+# darker. The value divided by 100 is the actual gamma applied, so 80 represents
+# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not
+# change the gamma.
+# Minimum value: 40, maximum value: 240, default value: 80.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_GAMMA = 80
+
+# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML
+# page will contain the date and time when the page was generated. Setting this
+# to YES can help to show when doxygen was last run and thus if the
+# documentation is up to date.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_TIMESTAMP = NO
+
+# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML
+# documentation will contain sections that can be hidden and shown after the
+# page has loaded.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_DYNAMIC_SECTIONS = NO
+
+# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries
+# shown in the various tree structured indices initially; the user can expand
+# and collapse entries dynamically later on. Doxygen will expand the tree to
+# such a level that at most the specified number of entries are visible (unless
+# a fully collapsed tree already exceeds this amount). So setting the number of
+# entries 1 will produce a full collapsed tree by default. 0 is a special value
+# representing an infinite number of entries and will result in a full expanded
+# tree by default.
+# Minimum value: 0, maximum value: 9999, default value: 100.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_INDEX_NUM_ENTRIES = 100
+
+# If the GENERATE_DOCSET tag is set to YES, additional index files will be
+# generated that can be used as input for Apple's Xcode 3 integrated development
+# environment (see: http://developer.apple.com/tools/xcode/), introduced with
+# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a
+# Makefile in the HTML output directory. Running make will produce the docset in
+# that directory and running make install will install the docset in
+# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at
+# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html
+# for more information.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_DOCSET = NO
+
+# This tag determines the name of the docset feed. A documentation feed provides
+# an umbrella under which multiple documentation sets from a single provider
+# (such as a company or product suite) can be grouped.
+# The default value is: Doxygen generated docs.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_FEEDNAME = "Doxygen generated docs"
+
+# This tag specifies a string that should uniquely identify the documentation
+# set bundle. This should be a reverse domain-name style string, e.g.
+# com.mycompany.MyDocSet. Doxygen will append .docset to the name.
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_BUNDLE_ID = org.doxygen.Project
+
+# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify
+# the documentation publisher. This should be a reverse domain-name style
+# string, e.g. com.mycompany.MyDocSet.documentation.
+# The default value is: org.doxygen.Publisher.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_PUBLISHER_ID = org.doxygen.Publisher
+
+# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher.
+# The default value is: Publisher.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_PUBLISHER_NAME = Publisher
+
+# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three
+# additional HTML index files: index.hhp, index.hhc, and index.hhk. The
+# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop
+# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on
+# Windows.
+#
+# The HTML Help Workshop contains a compiler that can convert all HTML output
+# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML
+# files are now used as the Windows 98 help format, and will replace the old
+# Windows help format (.hlp) on all Windows platforms in the future. Compressed
+# HTML files also contain an index, a table of contents, and you can search for
+# words in the documentation. The HTML workshop also contains a viewer for
+# compressed HTML files.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_HTMLHELP = NO
+
+# The CHM_FILE tag can be used to specify the file name of the resulting .chm
+# file. You can add a path in front of the file if the result should not be
+# written to the html output directory.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+CHM_FILE =
+
+# The HHC_LOCATION tag can be used to specify the location (absolute path
+# including file name) of the HTML help compiler (hhc.exe). If non-empty,
+# doxygen will try to run the HTML help compiler on the generated index.hhp.
+# The file has to be specified with full path.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+HHC_LOCATION =
+
+# The GENERATE_CHI flag controls if a separate .chi index file is generated
+# (YES) or that it should be included in the master .chm file (NO).
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+GENERATE_CHI = NO
+
+# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc)
+# and project file content.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+CHM_INDEX_ENCODING =
+
+# The BINARY_TOC flag controls whether a binary table of contents is generated
+# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it
+# enables the Previous and Next buttons.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+BINARY_TOC = NO
+
+# The TOC_EXPAND flag can be set to YES to add extra items for group members to
+# the table of contents of the HTML help documentation and to the tree view.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+TOC_EXPAND = NO
+
+# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and
+# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that
+# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help
+# (.qch) of the generated HTML documentation.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_QHP = NO
+
+# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify
+# the file name of the resulting .qch file. The path specified is relative to
+# the HTML output folder.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QCH_FILE =
+
+# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help
+# Project output. For more information please see Qt Help Project / Namespace
+# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace).
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_NAMESPACE = org.doxygen.Project
+
+# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt
+# Help Project output. For more information please see Qt Help Project / Virtual
+# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual-
+# folders).
+# The default value is: doc.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_VIRTUAL_FOLDER = doc
+
+# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom
+# filter to add. For more information please see Qt Help Project / Custom
+# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
+# filters).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_CUST_FILTER_NAME =
+
+# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the
+# custom filter to add. For more information please see Qt Help Project / Custom
+# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
+# filters).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_CUST_FILTER_ATTRS =
+
+# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this
+# project's filter section matches. Qt Help Project / Filter Attributes (see:
+# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_SECT_FILTER_ATTRS =
+
+# The QHG_LOCATION tag can be used to specify the location of Qt's
+# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the
+# generated .qhp file.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHG_LOCATION =
+
+# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be
+# generated, together with the HTML files, they form an Eclipse help plugin. To
+# install this plugin and make it available under the help contents menu in
+# Eclipse, the contents of the directory containing the HTML and XML files needs
+# to be copied into the plugins directory of eclipse. The name of the directory
+# within the plugins directory should be the same as the ECLIPSE_DOC_ID value.
+# After copying Eclipse needs to be restarted before the help appears.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_ECLIPSEHELP = NO
+
+# A unique identifier for the Eclipse help plugin. When installing the plugin
+# the directory name containing the HTML and XML files should also have this
+# name. Each documentation set should have its own identifier.
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES.
+
+ECLIPSE_DOC_ID = org.doxygen.Project
+
+# If you want full control over the layout of the generated HTML pages it might
+# be necessary to disable the index and replace it with your own. The
+# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top
+# of each HTML page. A value of NO enables the index and the value YES disables
+# it. Since the tabs in the index contain the same information as the navigation
+# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+DISABLE_INDEX = NO
+
+# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index
+# structure should be generated to display hierarchical information. If the tag
+# value is set to YES, a side panel will be generated containing a tree-like
+# index structure (just like the one that is generated for HTML Help). For this
+# to work a browser that supports JavaScript, DHTML, CSS and frames is required
+# (i.e. any modern browser). Windows users are probably better off using the
+# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can
+# further fine-tune the look of the index. As an example, the default style
+# sheet generated by doxygen has an example that shows how to put an image at
+# the root of the tree instead of the PROJECT_NAME. Since the tree basically has
+# the same information as the tab index, you could consider setting
+# DISABLE_INDEX to YES when enabling this option.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_TREEVIEW = NO
+
+# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that
+# doxygen will group on one line in the generated HTML documentation.
+#
+# Note that a value of 0 will completely suppress the enum values from appearing
+# in the overview section.
+# Minimum value: 0, maximum value: 20, default value: 4.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+ENUM_VALUES_PER_LINE = 4
+
+# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used
+# to set the initial width (in pixels) of the frame in which the tree is shown.
+# Minimum value: 0, maximum value: 1500, default value: 250.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+TREEVIEW_WIDTH = 250
+
+# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to
+# external symbols imported via tag files in a separate window.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+EXT_LINKS_IN_WINDOW = NO
+
+# Use this tag to change the font size of LaTeX formulas included as images in
+# the HTML documentation. When you change the font size after a successful
+# doxygen run you need to manually remove any form_*.png images from the HTML
+# output directory to force them to be regenerated.
+# Minimum value: 8, maximum value: 50, default value: 10.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+FORMULA_FONTSIZE = 10
+
+# Use the FORMULA_TRANPARENT tag to determine whether or not the images
+# generated for formulas are transparent PNGs. Transparent PNGs are not
+# supported properly for IE 6.0, but are supported on all modern browsers.
+#
+# Note that when changing this option you need to delete any form_*.png files in
+# the HTML output directory before the changes have effect.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+FORMULA_TRANSPARENT = YES
+
+# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see
+# http://www.mathjax.org) which uses client side Javascript for the rendering
+# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX
+# installed or if you want to formulas look prettier in the HTML output. When
+# enabled you may also need to install MathJax separately and configure the path
+# to it using the MATHJAX_RELPATH option.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+USE_MATHJAX = NO
+
+# When MathJax is enabled you can set the default output format to be used for
+# the MathJax output. See the MathJax site (see:
+# http://docs.mathjax.org/en/latest/output.html) for more details.
+# Possible values are: HTML-CSS (which is slower, but has the best
+# compatibility), NativeMML (i.e. MathML) and SVG.
+# The default value is: HTML-CSS.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_FORMAT = HTML-CSS
+
+# When MathJax is enabled you need to specify the location relative to the HTML
+# output directory using the MATHJAX_RELPATH option. The destination directory
+# should contain the MathJax.js script. For instance, if the mathjax directory
+# is located at the same level as the HTML output directory, then
+# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax
+# Content Delivery Network so you can quickly see the result without installing
+# MathJax. However, it is strongly recommended to install a local copy of
+# MathJax from http://www.mathjax.org before deployment.
+# The default value is: http://cdn.mathjax.org/mathjax/latest.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest
+
+# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax
+# extension names that should be enabled during MathJax rendering. For example
+# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_EXTENSIONS =
+
+# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces
+# of code that will be used on startup of the MathJax code. See the MathJax site
+# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an
+# example see the documentation.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_CODEFILE =
+
+# When the SEARCHENGINE tag is enabled doxygen will generate a search box for
+# the HTML output. The underlying search engine uses javascript and DHTML and
+# should work on any modern browser. Note that when using HTML help
+# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET)
+# there is already a search function so this one should typically be disabled.
+# For large projects the javascript based search engine can be slow, then
+# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to
+# search using the keyboard; to jump to the search box use <access key> + S
+# (what the <access key> is depends on the OS and browser, but it is typically
+# <CTRL>, <ALT>/<option>, or both). Inside the search box use the <cursor down
+# key> to jump into the search results window, the results can be navigated
+# using the <cursor keys>. Press <Enter> to select an item or <escape> to cancel
+# the search. The filter options can be selected when the cursor is inside the
+# search box by pressing <Shift>+<cursor down>. Also here use the <cursor keys>
+# to select a filter and <Enter> or <escape> to activate or cancel the filter
+# option.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+SEARCHENGINE = YES
+
+# When the SERVER_BASED_SEARCH tag is enabled the search engine will be
+# implemented using a web server instead of a web client using Javascript. There
+# are two flavors of web server based searching depending on the EXTERNAL_SEARCH
+# setting. When disabled, doxygen will generate a PHP script for searching and
+# an index file used by the script. When EXTERNAL_SEARCH is enabled the indexing
+# and searching needs to be provided by external tools. See the section
+# "External Indexing and Searching" for details.
+# The default value is: NO.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SERVER_BASED_SEARCH = NO
+
+# When EXTERNAL_SEARCH tag is enabled doxygen will no longer generate the PHP
+# script for searching. Instead the search results are written to an XML file
+# which needs to be processed by an external indexer. Doxygen will invoke an
+# external search engine pointed to by the SEARCHENGINE_URL option to obtain the
+# search results.
+#
+# Doxygen ships with an example indexer (doxyindexer) and search engine
+# (doxysearch.cgi) which are based on the open source search engine library
+# Xapian (see: http://xapian.org/).
+#
+# See the section "External Indexing and Searching" for details.
+# The default value is: NO.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTERNAL_SEARCH = NO
+
+# The SEARCHENGINE_URL should point to a search engine hosted by a web server
+# which will return the search results when EXTERNAL_SEARCH is enabled.
+#
+# Doxygen ships with an example indexer (doxyindexer) and search engine
+# (doxysearch.cgi) which are based on the open source search engine library
+# Xapian (see: http://xapian.org/). See the section "External Indexing and
+# Searching" for details.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SEARCHENGINE_URL =
+
+# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the unindexed
+# search data is written to a file for indexing by an external tool. With the
+# SEARCHDATA_FILE tag the name of this file can be specified.
+# The default file is: searchdata.xml.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SEARCHDATA_FILE = searchdata.xml
+
+# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the
+# EXTERNAL_SEARCH_ID tag can be used as an identifier for the project. This is
+# useful in combination with EXTRA_SEARCH_MAPPINGS to search through multiple
+# projects and redirect the results back to the right project.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTERNAL_SEARCH_ID =
+
+# The EXTRA_SEARCH_MAPPINGS tag can be used to enable searching through doxygen
+# projects other than the one defined by this configuration file, but that are
+# all added to the same external search index. Each project needs to have a
+# unique id set via EXTERNAL_SEARCH_ID. The search mapping then maps the id of
+# to a relative location where the documentation can be found. The format is:
+# EXTRA_SEARCH_MAPPINGS = tagname1=loc1 tagname2=loc2 ...
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTRA_SEARCH_MAPPINGS =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
+# The default value is: YES.
+
+GENERATE_LATEX = NO
+
+# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: latex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_OUTPUT = latex
+
+# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be
+# invoked.
+#
+# Note that when enabling USE_PDFLATEX this option is only used for generating
+# bitmaps for formulas in the HTML output, but not in the Makefile that is
+# written to the output directory.
+# The default file is: latex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_CMD_NAME = latex
+
+# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to generate
+# index for LaTeX.
+# The default file is: makeindex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+MAKEINDEX_CMD_NAME = makeindex
+
+# If the COMPACT_LATEX tag is set to YES, doxygen generates more compact LaTeX
+# documents. This may be useful for small projects and may help to save some
+# trees in general.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+COMPACT_LATEX = NO
+
+# The PAPER_TYPE tag can be used to set the paper type that is used by the
+# printer.
+# Possible values are: a4 (210 x 297 mm), letter (8.5 x 11 inches), legal (8.5 x
+# 14 inches) and executive (7.25 x 10.5 inches).
+# The default value is: a4.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+PAPER_TYPE = a4
+
+# The EXTRA_PACKAGES tag can be used to specify one or more LaTeX package names
+# that should be included in the LaTeX output. The package can be specified just
+# by its name or with the correct syntax as to be used with the LaTeX
+# \usepackage command. To get the times font for instance you can specify :
+# EXTRA_PACKAGES=times or EXTRA_PACKAGES={times}
+# To use the option intlimits with the amsmath package you can specify:
+# EXTRA_PACKAGES=[intlimits]{amsmath}
+# If left blank no extra packages will be included.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+EXTRA_PACKAGES =
+
+# The LATEX_HEADER tag can be used to specify a personal LaTeX header for the
+# generated LaTeX document. The header should contain everything until the first
+# chapter. If it is left blank doxygen will generate a standard header. See
+# section "Doxygen usage" for information on how to let doxygen write the
+# default header to a separate file.
+#
+# Note: Only use a user-defined header if you know what you are doing! The
+# following commands have a special meaning inside the header: $title,
+# $datetime, $date, $doxygenversion, $projectname, $projectnumber,
+# $projectbrief, $projectlogo. Doxygen will replace $title with the empty
+# string, for the replacement values of the other commands the user is referred
+# to HTML_HEADER.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_HEADER =
+
+# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for the
+# generated LaTeX document. The footer should contain everything after the last
+# chapter. If it is left blank doxygen will generate a standard footer. See
+# LATEX_HEADER for more information on how to generate a default footer and what
+# special commands can be used inside the footer.
+#
+# Note: Only use a user-defined footer if you know what you are doing!
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_FOOTER =
+
+# The LATEX_EXTRA_STYLESHEET tag can be used to specify additional user-defined
+# LaTeX style sheets that are included after the standard style sheets created
+# by doxygen. Using this option one can overrule certain style aspects. Doxygen
+# will copy the style sheet files to the output directory.
+# Note: The order of the extra style sheet files is of importance (e.g. the last
+# style sheet in the list overrules the setting of the previous ones in the
+# list).
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_EXTRA_STYLESHEET =
+
+# The LATEX_EXTRA_FILES tag can be used to specify one or more extra images or
+# other source files which should be copied to the LATEX_OUTPUT output
+# directory. Note that the files will be copied as-is; there are no commands or
+# markers available.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_EXTRA_FILES =
+
+# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated is
+# prepared for conversion to PDF (using ps2pdf or pdflatex). The PDF file will
+# contain links (just like the HTML output) instead of page references. This
+# makes the output suitable for online browsing using a PDF viewer.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+PDF_HYPERLINKS = YES
+
+# If the USE_PDFLATEX tag is set to YES, doxygen will use pdflatex to generate
+# the PDF file directly from the LaTeX files. Set this option to YES, to get a
+# higher quality PDF documentation.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+USE_PDFLATEX = YES
+
+# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \batchmode
+# command to the generated LaTeX files. This will instruct LaTeX to keep running
+# if errors occur, instead of asking the user for help. This option is also used
+# when generating formulas in HTML.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_BATCHMODE = NO
+
+# If the LATEX_HIDE_INDICES tag is set to YES then doxygen will not include the
+# index chapters (such as File Index, Compound Index, etc.) in the output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_HIDE_INDICES = NO
+
+# If the LATEX_SOURCE_CODE tag is set to YES then doxygen will include source
+# code with syntax highlighting in the LaTeX output.
+#
+# Note that which sources are shown also depends on other settings such as
+# SOURCE_BROWSER.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_SOURCE_CODE = NO
+
+# The LATEX_BIB_STYLE tag can be used to specify the style to use for the
+# bibliography, e.g. plainnat, or ieeetr. See
+# http://en.wikipedia.org/wiki/BibTeX and \cite for more info.
+# The default value is: plain.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_BIB_STYLE = plain
+
+# If the LATEX_TIMESTAMP tag is set to YES then the footer of each generated
+# page will contain the date and time when the page was generated. Setting this
+# to NO can help when comparing the output of multiple runs.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_TIMESTAMP = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the RTF output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_RTF tag is set to YES, doxygen will generate RTF output. The
+# RTF output is optimized for Word 97 and may not look too pretty with other RTF
+# readers/editors.
+# The default value is: NO.
+
+GENERATE_RTF = NO
+
+# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: rtf.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_OUTPUT = rtf
+
+# If the COMPACT_RTF tag is set to YES, doxygen generates more compact RTF
+# documents. This may be useful for small projects and may help to save some
+# trees in general.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+COMPACT_RTF = NO
+
+# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated will
+# contain hyperlink fields. The RTF file will contain links (just like the HTML
+# output) instead of page references. This makes the output suitable for online
+# browsing using Word or some other Word compatible readers that support those
+# fields.
+#
+# Note: WordPad (write) and others do not support links.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_HYPERLINKS = NO
+
+# Load stylesheet definitions from file. Syntax is similar to doxygen's config
+# file, i.e. a series of assignments. You only have to provide replacements,
+# missing definitions are set to their default value.
+#
+# See also section "Doxygen usage" for information on how to generate the
+# default style sheet that doxygen normally uses.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_STYLESHEET_FILE =
+
+# Set optional variables used in the generation of an RTF document. Syntax is
+# similar to doxygen's config file. A template extensions file can be generated
+# using doxygen -e rtf extensionFile.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_EXTENSIONS_FILE =
+
+# If the RTF_SOURCE_CODE tag is set to YES then doxygen will include source code
+# with syntax highlighting in the RTF output.
+#
+# Note that which sources are shown also depends on other settings such as
+# SOURCE_BROWSER.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_SOURCE_CODE = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the man page output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_MAN tag is set to YES, doxygen will generate man pages for
+# classes and files.
+# The default value is: NO.
+
+GENERATE_MAN = NO
+
+# The MAN_OUTPUT tag is used to specify where the man pages will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it. A directory man3 will be created inside the directory specified by
+# MAN_OUTPUT.
+# The default directory is: man.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_OUTPUT = man
+
+# The MAN_EXTENSION tag determines the extension that is added to the generated
+# man pages. In case the manual section does not start with a number, the number
+# 3 is prepended. The dot (.) at the beginning of the MAN_EXTENSION tag is
+# optional.
+# The default value is: .3.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_EXTENSION = .3
+
+# The MAN_SUBDIR tag determines the name of the directory created within
+# MAN_OUTPUT in which the man pages are placed. If defaults to man followed by
+# MAN_EXTENSION with the initial . removed.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_SUBDIR =
+
+# If the MAN_LINKS tag is set to YES and doxygen generates man output, then it
+# will generate one additional man file for each entity documented in the real
+# man page(s). These additional files only source the real man page, but without
+# them the man command would be unable to find the correct page.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_LINKS = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the XML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_XML tag is set to YES, doxygen will generate an XML file that
+# captures the structure of the code including all documentation.
+# The default value is: NO.
+
+GENERATE_XML = NO
+
+# The XML_OUTPUT tag is used to specify where the XML pages will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: xml.
+# This tag requires that the tag GENERATE_XML is set to YES.
+
+XML_OUTPUT = xml
+
+# If the XML_PROGRAMLISTING tag is set to YES, doxygen will dump the program
+# listings (including syntax highlighting and cross-referencing information) to
+# the XML output. Note that enabling this will significantly increase the size
+# of the XML output.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_XML is set to YES.
+
+XML_PROGRAMLISTING = YES
+
+#---------------------------------------------------------------------------
+# Configuration options related to the DOCBOOK output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_DOCBOOK tag is set to YES, doxygen will generate Docbook files
+# that can be used to generate PDF.
+# The default value is: NO.
+
+GENERATE_DOCBOOK = NO
+
+# The DOCBOOK_OUTPUT tag is used to specify where the Docbook pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be put in
+# front of it.
+# The default directory is: docbook.
+# This tag requires that the tag GENERATE_DOCBOOK is set to YES.
+
+DOCBOOK_OUTPUT = docbook
+
+# If the DOCBOOK_PROGRAMLISTING tag is set to YES, doxygen will include the
+# program listings (including syntax highlighting and cross-referencing
+# information) to the DOCBOOK output. Note that enabling this will significantly
+# increase the size of the DOCBOOK output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_DOCBOOK is set to YES.
+
+DOCBOOK_PROGRAMLISTING = NO
+
+#---------------------------------------------------------------------------
+# Configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_AUTOGEN_DEF tag is set to YES, doxygen will generate an
+# AutoGen Definitions (see http://autogen.sf.net) file that captures the
+# structure of the code including all documentation. Note that this feature is
+# still experimental and incomplete at the moment.
+# The default value is: NO.
+
+GENERATE_AUTOGEN_DEF = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_PERLMOD tag is set to YES, doxygen will generate a Perl module
+# file that captures the structure of the code including all documentation.
+#
+# Note that this feature is still experimental and incomplete at the moment.
+# The default value is: NO.
+
+GENERATE_PERLMOD = NO
+
+# If the PERLMOD_LATEX tag is set to YES, doxygen will generate the necessary
+# Makefile rules, Perl scripts and LaTeX code to be able to generate PDF and DVI
+# output from the Perl module output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_LATEX = NO
+
+# If the PERLMOD_PRETTY tag is set to YES, the Perl module output will be nicely
+# formatted so it can be parsed by a human reader. This is useful if you want to
+# understand what is going on. On the other hand, if this tag is set to NO, the
+# size of the Perl module output will be much smaller and Perl will parse it
+# just the same.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_PRETTY = YES
+
+# The names of the make variables in the generated doxyrules.make file are
+# prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. This is useful
+# so different doxyrules.make files included by the same Makefile don't
+# overwrite each other's variables.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_MAKEVAR_PREFIX =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor
+#---------------------------------------------------------------------------
+
+# If the ENABLE_PREPROCESSING tag is set to YES, doxygen will evaluate all
+# C-preprocessor directives found in the sources and include files.
+# The default value is: YES.
+
+ENABLE_PREPROCESSING = YES
+
+# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
+# in the source code. If set to NO, only conditional compilation will be
+# performed. Macro expansion can be done in a controlled way by setting
+# EXPAND_ONLY_PREDEF to YES.
+# The default value is: NO.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+MACRO_EXPANSION = NO
+
+# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then
+# the macro expansion is limited to the macros specified with the PREDEFINED and
+# EXPAND_AS_DEFINED tags.
+# The default value is: NO.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+EXPAND_ONLY_PREDEF = NO
+
+# If the SEARCH_INCLUDES tag is set to YES, the include files in the
+# INCLUDE_PATH will be searched if a #include is found.
+# The default value is: YES.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+SEARCH_INCLUDES = YES
+
+# The INCLUDE_PATH tag can be used to specify one or more directories that
+# contain include files that are not input files but should be processed by the
+# preprocessor.
+# This tag requires that the tag SEARCH_INCLUDES is set to YES.
+
+INCLUDE_PATH =
+
+# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
+# patterns (like *.h and *.hpp) to filter out the header-files in the
+# directories. If left blank, the patterns specified with FILE_PATTERNS will be
+# used.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+INCLUDE_FILE_PATTERNS =
+
+# The PREDEFINED tag can be used to specify one or more macro names that are
+# defined before the preprocessor is started (similar to the -D option of e.g.
+# gcc). The argument of the tag is a list of macros of the form: name or
+# name=definition (no spaces). If the definition and the "=" are omitted, "=1"
+# is assumed. To prevent a macro definition from being undefined via #undef or
+# recursively expanded use the := operator instead of the = operator.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+PREDEFINED = BR_DOXYGEN_IGNORE
+
+# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
+# tag can be used to specify a list of macro names that should be expanded. The
+# macro definition that is found in the sources will be used. Use the PREDEFINED
+# tag if you want to use a different macro definition that overrules the
+# definition found in the source code.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+EXPAND_AS_DEFINED =
+
+# If the SKIP_FUNCTION_MACROS tag is set to YES then doxygen's preprocessor will
+# remove all references to function-like macros that are alone on a line, have
+# an all uppercase name, and do not end with a semicolon. Such function macros
+# are typically used for boiler-plate code, and will confuse the parser if not
+# removed.
+# The default value is: YES.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+SKIP_FUNCTION_MACROS = YES
+
+#---------------------------------------------------------------------------
+# Configuration options related to external references
+#---------------------------------------------------------------------------
+
+# The TAGFILES tag can be used to specify one or more tag files. For each tag
+# file the location of the external documentation should be added. The format of
+# a tag file without this location is as follows:
+# TAGFILES = file1 file2 ...
+# Adding location for the tag files is done as follows:
+# TAGFILES = file1=loc1 "file2 = loc2" ...
+# where loc1 and loc2 can be relative or absolute paths or URLs. See the
+# section "Linking to external documentation" for more information about the use
+# of tag files.
+# Note: Each tag file must have a unique name (where the name does NOT include
+# the path). If a tag file is not located in the directory in which doxygen is
+# run, you must also specify the path to the tagfile here.
+
+TAGFILES =
+
+# When a file name is specified after GENERATE_TAGFILE, doxygen will create a
+# tag file that is based on the input files it reads. See section "Linking to
+# external documentation" for more information about the usage of tag files.
+
+GENERATE_TAGFILE =
+
+# If the ALLEXTERNALS tag is set to YES, all external class will be listed in
+# the class index. If set to NO, only the inherited external classes will be
+# listed.
+# The default value is: NO.
+
+ALLEXTERNALS = NO
+
+# If the EXTERNAL_GROUPS tag is set to YES, all external groups will be listed
+# in the modules index. If set to NO, only the current project's groups will be
+# listed.
+# The default value is: YES.
+
+EXTERNAL_GROUPS = YES
+
+# If the EXTERNAL_PAGES tag is set to YES, all external pages will be listed in
+# the related pages index. If set to NO, only the current project's pages will
+# be listed.
+# The default value is: YES.
+
+EXTERNAL_PAGES = YES
+
+# The PERL_PATH should be the absolute path and name of the perl script
+# interpreter (i.e. the result of 'which perl').
+# The default file (with absolute path) is: /usr/bin/perl.
+
+PERL_PATH = /usr/bin/perl
+
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool
+#---------------------------------------------------------------------------
+
+# If the CLASS_DIAGRAMS tag is set to YES, doxygen will generate a class diagram
+# (in HTML and LaTeX) for classes with base or super classes. Setting the tag to
+# NO turns the diagrams off. Note that this option also works with HAVE_DOT
+# disabled, but it is recommended to install and use dot, since it yields more
+# powerful graphs.
+# The default value is: YES.
+
+CLASS_DIAGRAMS = NO
+
+# You can define message sequence charts within doxygen comments using the \msc
+# command. Doxygen will then run the mscgen tool (see:
+# http://www.mcternan.me.uk/mscgen/)) to produce the chart and insert it in the
+# documentation. The MSCGEN_PATH tag allows you to specify the directory where
+# the mscgen tool resides. If left empty the tool is assumed to be found in the
+# default search path.
+
+MSCGEN_PATH =
+
+# You can include diagrams made with dia in doxygen documentation. Doxygen will
+# then run dia to produce the diagram and insert it in the documentation. The
+# DIA_PATH tag allows you to specify the directory where the dia binary resides.
+# If left empty dia is assumed to be found in the default search path.
+
+DIA_PATH =
+
+# If set to YES the inheritance and collaboration graphs will hide inheritance
+# and usage relations if the target is undocumented or is not a class.
+# The default value is: YES.
+
+HIDE_UNDOC_RELATIONS = YES
+
+# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is
+# available from the path. This tool is part of Graphviz (see:
+# http://www.graphviz.org/), a graph visualization toolkit from AT&T and Lucent
+# Bell Labs. The other options in this section have no effect if this option is
+# set to NO
+# The default value is: YES.
+
+HAVE_DOT = YES
+
+# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is allowed
+# to run in parallel. When set to 0 doxygen will base this on the number of
+# processors available in the system. You can set it explicitly to a value
+# larger than 0 to get control over the balance between CPU load and processing
+# speed.
+# Minimum value: 0, maximum value: 32, default value: 0.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_NUM_THREADS = 0
+
+# When you want a differently looking font in the dot files that doxygen
+# generates you can specify the font name using DOT_FONTNAME. You need to make
+# sure dot is able to find the font, which can be done by putting it in a
+# standard location or by setting the DOTFONTPATH environment variable or by
+# setting DOT_FONTPATH to the directory containing the font.
+# The default value is: Helvetica.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTNAME = Helvetica
+
+# The DOT_FONTSIZE tag can be used to set the size (in points) of the font of
+# dot graphs.
+# Minimum value: 4, maximum value: 24, default value: 10.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTSIZE = 10
+
+# By default doxygen will tell dot to use the default font as specified with
+# DOT_FONTNAME. If you specify a different font using DOT_FONTNAME you can set
+# the path where dot can find it using this tag.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTPATH =
+
+# If the CLASS_GRAPH tag is set to YES then doxygen will generate a graph for
+# each documented class showing the direct and indirect inheritance relations.
+# Setting this tag to YES will force the CLASS_DIAGRAMS tag to NO.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CLASS_GRAPH = NO
+
+# If the COLLABORATION_GRAPH tag is set to YES then doxygen will generate a
+# graph for each documented class showing the direct and indirect implementation
+# dependencies (inheritance, containment, and class references variables) of the
+# class with other documented classes.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+COLLABORATION_GRAPH = NO
+
+# If the GROUP_GRAPHS tag is set to YES then doxygen will generate a graph for
+# groups, showing the direct groups dependencies.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GROUP_GRAPHS = NO
+
+# If the UML_LOOK tag is set to YES, doxygen will generate inheritance and
+# collaboration diagrams in a style similar to the OMG's Unified Modeling
+# Language.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+UML_LOOK = NO
+
+# If the UML_LOOK tag is enabled, the fields and methods are shown inside the
+# class node. If there are many fields or methods and many nodes the graph may
+# become too big to be useful. The UML_LIMIT_NUM_FIELDS threshold limits the
+# number of items for each type to make the size more manageable. Set this to 0
+# for no limit. Note that the threshold may be exceeded by 50% before the limit
+# is enforced. So when you set the threshold to 10, up to 15 fields may appear,
+# but if the number exceeds 15, the total amount of fields shown is limited to
+# 10.
+# Minimum value: 0, maximum value: 100, default value: 10.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+UML_LIMIT_NUM_FIELDS = 10
+
+# If the TEMPLATE_RELATIONS tag is set to YES then the inheritance and
+# collaboration graphs will show the relations between templates and their
+# instances.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+TEMPLATE_RELATIONS = NO
+
+# If the INCLUDE_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are set to
+# YES then doxygen will generate a graph for each documented file showing the
+# direct and indirect include dependencies of the file with other documented
+# files.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INCLUDE_GRAPH = YES
+
+# If the INCLUDED_BY_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are
+# set to YES then doxygen will generate a graph for each documented file showing
+# the direct and indirect include dependencies of the file with other documented
+# files.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INCLUDED_BY_GRAPH = YES
+
+# If the CALL_GRAPH tag is set to YES then doxygen will generate a call
+# dependency graph for every global function or class method.
+#
+# Note that enabling this option will significantly increase the time of a run.
+# So in most cases it will be better to enable call graphs for selected
+# functions only using the \callgraph command. Disabling a call graph can be
+# accomplished by means of the command \hidecallgraph.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CALL_GRAPH = NO
+
+# If the CALLER_GRAPH tag is set to YES then doxygen will generate a caller
+# dependency graph for every global function or class method.
+#
+# Note that enabling this option will significantly increase the time of a run.
+# So in most cases it will be better to enable caller graphs for selected
+# functions only using the \callergraph command. Disabling a caller graph can be
+# accomplished by means of the command \hidecallergraph.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CALLER_GRAPH = NO
+
+# If the GRAPHICAL_HIERARCHY tag is set to YES then doxygen will graphical
+# hierarchy of all classes instead of a textual one.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GRAPHICAL_HIERARCHY = YES
+
+# If the DIRECTORY_GRAPH tag is set to YES then doxygen will show the
+# dependencies a directory has on other directories in a graphical way. The
+# dependency relations are determined by the #include relations between the
+# files in the directories.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DIRECTORY_GRAPH = YES
+
+# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images
+# generated by dot. For an explanation of the image formats see the section
+# output formats in the documentation of the dot tool (Graphviz (see:
+# http://www.graphviz.org/)).
+# Note: If you choose svg you need to set HTML_FILE_EXTENSION to xhtml in order
+# to make the SVG files visible in IE 9+ (other browsers do not have this
+# requirement).
+# Possible values are: png, png:cairo, png:cairo:cairo, png:cairo:gd, png:gd,
+# png:gd:gd, jpg, jpg:cairo, jpg:cairo:gd, jpg:gd, jpg:gd:gd, gif, gif:cairo,
+# gif:cairo:gd, gif:gd, gif:gd:gd, svg, png:gd, png:gd:gd, png:cairo,
+# png:cairo:gd, png:cairo:cairo, png:cairo:gdiplus, png:gdiplus and
+# png:gdiplus:gdiplus.
+# The default value is: png.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_IMAGE_FORMAT = png
+
+# If DOT_IMAGE_FORMAT is set to svg, then this option can be set to YES to
+# enable generation of interactive SVG images that allow zooming and panning.
+#
+# Note that this requires a modern browser other than Internet Explorer. Tested
+# and working are Firefox, Chrome, Safari, and Opera.
+# Note: For IE 9+ you need to set HTML_FILE_EXTENSION to xhtml in order to make
+# the SVG files visible. Older versions of IE do not have SVG support.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INTERACTIVE_SVG = NO
+
+# The DOT_PATH tag can be used to specify the path where the dot tool can be
+# found. If left blank, it is assumed the dot tool can be found in the path.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_PATH =
+
+# The DOTFILE_DIRS tag can be used to specify one or more directories that
+# contain dot files that are included in the documentation (see the \dotfile
+# command).
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOTFILE_DIRS =
+
+# The MSCFILE_DIRS tag can be used to specify one or more directories that
+# contain msc files that are included in the documentation (see the \mscfile
+# command).
+
+MSCFILE_DIRS =
+
+# The DIAFILE_DIRS tag can be used to specify one or more directories that
+# contain dia files that are included in the documentation (see the \diafile
+# command).
+
+DIAFILE_DIRS =
+
+# When using plantuml, the PLANTUML_JAR_PATH tag should be used to specify the
+# path where java can find the plantuml.jar file. If left blank, it is assumed
+# PlantUML is not used or called during a preprocessing step. Doxygen will
+# generate a warning when it encounters a \startuml command in this case and
+# will not generate output for the diagram.
+
+PLANTUML_JAR_PATH =
+
+# When using plantuml, the specified paths are searched for files specified by
+# the !include statement in a plantuml block.
+
+PLANTUML_INCLUDE_PATH =
+
+# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of nodes
+# that will be shown in the graph. If the number of nodes in a graph becomes
+# larger than this value, doxygen will truncate the graph, which is visualized
+# by representing a node as a red box. Note that doxygen if the number of direct
+# children of the root node in a graph is already larger than
+# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note that
+# the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH.
+# Minimum value: 0, maximum value: 10000, default value: 50.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_GRAPH_MAX_NODES = 50
+
+# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the graphs
+# generated by dot. A depth value of 3 means that only nodes reachable from the
+# root by following a path via at most 3 edges will be shown. Nodes that lay
+# further from the root node will be omitted. Note that setting this option to 1
+# or 2 may greatly reduce the computation time needed for large code bases. Also
+# note that the size of a graph can be further restricted by
+# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction.
+# Minimum value: 0, maximum value: 1000, default value: 0.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+MAX_DOT_GRAPH_DEPTH = 0
+
+# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent
+# background. This is disabled by default, because dot on Windows does not seem
+# to support this out of the box.
+#
+# Warning: Depending on the platform used, enabling this option may lead to
+# badly anti-aliased labels on the edges of a graph (i.e. they become hard to
+# read).
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_TRANSPARENT = NO
+
+# Set the DOT_MULTI_TARGETS tag to YES to allow dot to generate multiple output
+# files in one run (i.e. multiple -o and -T options on the command line). This
+# makes dot run faster, but since only newer versions of dot (>1.8.10) support
+# this, this feature is disabled by default.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_MULTI_TARGETS = NO
+
+# If the GENERATE_LEGEND tag is set to YES doxygen will generate a legend page
+# explaining the meaning of the various boxes and arrows in the dot generated
+# graphs.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GENERATE_LEGEND = YES
+
+# If the DOT_CLEANUP tag is set to YES, doxygen will remove the intermediate dot
+# files that are used to generate the various graphs.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_CLEANUP = YES
diff --git a/test/monniaux/BearSSL/LICENSE.txt b/test/monniaux/BearSSL/LICENSE.txt
new file mode 100644
index 00000000..08850203
--- /dev/null
+++ b/test/monniaux/BearSSL/LICENSE.txt
@@ -0,0 +1,21 @@
+Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/test/monniaux/BearSSL/Makefile b/test/monniaux/BearSSL/Makefile
new file mode 100644
index 00000000..f7f24fcc
--- /dev/null
+++ b/test/monniaux/BearSSL/Makefile
@@ -0,0 +1,45 @@
+# Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# ======================================================================
+
+# The lines below are a horrible hack that nonetheless works. On a
+# "make" utility compatible with Single Unix v4 (this includes GNU and
+# BSD make), the '\' at the end of a command line counts as an escape
+# for the newline character, so the next line is still a comment.
+# However, Microsoft's nmake.exe (that comes with Visual Studio) does
+# not interpret the final '\' that way in a comment. The end result is
+# that when using nmake.exe, this will include "mk/Win.mk", whereas
+# GNU/BSD make will include "mk/Unix.mk".
+
+# \
+!ifndef 0 # \
+!include mk/NMake.mk # \
+!else
+.POSIX:
+include mk/SingleUnix.mk
+# Extra hack for OpenBSD make.
+ifndef: all
+0: all
+endif: all
+# \
+!endif
diff --git a/test/monniaux/BearSSL/README.txt b/test/monniaux/BearSSL/README.txt
new file mode 100644
index 00000000..0cb52887
--- /dev/null
+++ b/test/monniaux/BearSSL/README.txt
@@ -0,0 +1,136 @@
+# Documentation
+
+The most up-to-date documentation is supposed to be available on the
+[BearSSL Web site](https://www.bearssl.org/).
+
+# Disclaimer
+
+BearSSL is considered beta-level software. Most planned functionalities
+are implemented; new evolution may still break both source and binary
+compatibility.
+
+Using BearSSL for production purposes would be a relatively bold but not
+utterly crazy move. BearSSL is free, open-source software, provided
+without any guarantee of fitness or reliability. That being said, it
+appears to behave properly, and only minor issues have been found (and
+fixed) so far. You are encourage to inspect its API and code for
+learning, testing and possibly contributing.
+
+The usage license is explicited in the `LICENSE.txt` file. This is the
+"MIT license". It can be summarised in the following way:
+
+ - You can use and reuse the library as you wish, and modify it, and
+ integrate it in your own code, and distribute it as is or in any
+ modified form, and so on.
+
+ - The only obligation that the license terms put upon you is that you
+ acknowledge and make it clear that if anything breaks, it is not my
+ fault, and I am not liable for anything, regardless of the type and
+ amount of collateral damage. The license terms say that the copyright
+ notice "shall be included in all copies or substantial portions of
+ the Software": this is how the disclaimer is "made explicit".
+ Basically, I have put it in every source file, so just keep it there.
+
+# Installation
+
+Right now, BearSSL is a simple library, along with a few test and debug
+command-line tools. There is no installer yet. The library _can_ be
+compiled as a shared library on some systems, but since the binary API
+is not fully stabilised, this is not a very good idea to do that right
+now.
+
+To compile the code, just type `make`. This will try to use sane
+"default" values. On a Windows system with Visual Studio, run a console
+with the environment initialised for a specific version of the C compiler,
+and type `nmake`.
+
+To override the default settings, create a custom configuration file in
+the `conf` directory, and invoke `make` (or `nmake`) with an explicit
+`CONF=` parameter. For instance, to use the provided `samd20.mk`
+configuration file (that targets cross-compilation for an Atmel board
+that features a Cortex-M0+ CPU), type:
+
+ make CONF=samd20
+
+The `conf/samd20.mk` file includes the `Unix.mk` file and then overrides
+some of the parameters, including the destination directory. Any custom
+configuration can be made the same way.
+
+Some compile-time options can be set through macros, either on the
+compiler command-line, or in the `src/config.h` file. See the comments
+in that file. Some settings are autodetected but they can still be
+explicitly overridden.
+
+When compilation is done, the library (static and DLL, when appropriate)
+and the command-line tools can be found in the designated build
+directory (by default named `build`). The public headers (to be used
+by applications linked against BearSSL) are in the `inc/` directory.
+
+To run the tests:
+
+ - `testcrypto all` runs the cryptographic tests (test vectors on all
+ implemented cryptogaphic functions). It can be slow. You can also
+ run a selection of the tests by providing their names (run
+ `testcrypto` without any parameter to see the available names).
+
+ - `testspeed all` runs a number of performance benchmarks, there again
+ on cryptographic functions. It gives a taste of how things go on the
+ current platform. As for `testcrypto`, specific named benchmarks can
+ be executed.
+
+ - `testx509` runs X.509 validation tests. The test certificates are
+ all in `test/x509/`.
+
+The `brssl` command-line tool produced in the build directory is a
+stand-alone binary. It can exercise some of the functionalities of
+BearSSL, in particular running a test SSL client or server. It is not
+meant for production purposes (e.g. the SSL client has a mode where it
+disregards the inability to validate the server's certificate, which is
+inherently unsafe, but convenient for debug).
+
+**Using the library** means writing some application code that invokes
+it, and linking with the static library. The header files are all in the
+`inc` directory; copy them wherever makes sense (e.g. in the
+`/usr/local/include` directory). The library itself (`libbearssl.a`) is
+what you link against.
+
+Alternatively, you may want to copy the source files directly into your
+own application code. This will make integrating ulterior versions of
+BearSSL more difficult. If you still want to go down that road, then
+simply copy all the `*.h` and `*.c` files from the `src` and `inc`
+directories into your application source code. In the BearSSL source
+archive, the source files are segregated into various sub-directories,
+but this is for my convenience only. There is no technical requirement
+for that, and all files can be dumped together in a simple directory.
+
+Dependencies are simple and systematic:
+
+ - Each `*.c` file includes `inner.h`
+ - `inner.h` includes `config.h` and `bearssl.h`
+ - `bearssl.h` includes the other `bearssl_*.h`
+
+# Versioning
+
+I follow this simple version numbering scheme:
+
+ - Version numbers are `x.y` or `x.y.z` where `x`, `y` and `z` are
+ decimal integers (possibly greater than 10). When the `.z` part is
+ missing, it is equivalent to `.0`.
+
+ - Backward compatibility is maintained, at both source and binary levels,
+ for each major version: this means that if some application code was
+ designed for version `x.y`, then it should compile, link and run
+ properly with any version `x.y'` for any `y'` greater than `y`.
+
+ The major version `0` is an exception. You shall not expect that any
+ version that starts with `0.` offers any kind of compatibility,
+ either source or binary, with any other `0.` version. (Of course I
+ will try to maintain some decent level of source compatibility, but I
+ make no promise in that respect. Since the API uses caller-allocated
+ context structures, I already know that binary compatibility _will_
+ be broken.)
+
+ - Sub-versions (the `y` part) are about added functionality. That is,
+ it can be expected that `1.3` will contain some extra functions when
+ compared to `1.2`. The next version level (the `z` part) is for
+ bugfixes that do not add any functionality.
diff --git a/test/monniaux/BearSSL/compile_ccomp.sh b/test/monniaux/BearSSL/compile_ccomp.sh
new file mode 100755
index 00000000..2ce6ccf6
--- /dev/null
+++ b/test/monniaux/BearSSL/compile_ccomp.sh
@@ -0,0 +1 @@
+make CONF=KalrayCompCert build/testcrypto $@ # build/testspeed
diff --git a/test/monniaux/BearSSL/conf/Kalray.mk b/test/monniaux/BearSSL/conf/Kalray.mk
new file mode 100644
index 00000000..6359edd1
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/Kalray.mk
@@ -0,0 +1,69 @@
+# Configuration for a native build on a generic Unix-like system.
+
+# Build directory.
+BUILD = build
+
+# Extension for executable files.
+E =
+
+# Extension for object files.
+O = .o
+
+# Prefix for library file name.
+LP = lib
+
+# Extension for library file name.
+L = .a
+
+# Prefix for DLL file name.
+DP = lib
+
+# Extension for DLL file name.
+D = .so
+
+# Output file names can be overridden directly. By default, they are
+# assembled using the prefix/extension macros defined above.
+# BEARSSLLIB = libbearssl.a
+# BEARSSLDLL = libbearssl.so
+# BRSSL = brssl
+# TESTCRYPTO = testcrypto
+# TESTSPEED = testspeed
+# TESTX509 = testx509
+
+# File deletion tool.
+RM = rm -f
+
+# Directory creation tool.
+MKDIR = mkdir -p
+
+# C compiler and flags.
+CC = k1-cos-gcc
+CFLAGS = -W -Wall -O3
+CCOUT = -c -o
+
+# Static library building tool.
+AR = ar
+ARFLAGS = -rcs
+AROUT =
+
+# DLL building tool.
+LDDLL = $(CC)
+LDDLLFLAGS = -shared
+LDDLLOUT = -o
+
+# Static linker.
+LD = $(CC)
+LDFLAGS =
+LDOUT = -o
+
+# C# compiler; we assume usage of Mono.
+MKT0COMP = mk$PmkT0.sh
+RUNT0COMP = mono T0Comp.exe
+
+# Set the values to 'no' to disable building of the corresponding element
+# by default. Building can still be invoked with an explicit target call
+# (e.g. 'make dll' to force build the DLL).
+#STATICLIB = no
+DLL = no
+TOOLS = no
+#TESTS = no
diff --git a/test/monniaux/BearSSL/conf/KalrayCompCert.mk b/test/monniaux/BearSSL/conf/KalrayCompCert.mk
new file mode 100644
index 00000000..d67fdb8b
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/KalrayCompCert.mk
@@ -0,0 +1,69 @@
+# Configuration for a native build on a generic Unix-like system.
+
+# Build directory.
+BUILD = build
+
+# Extension for executable files.
+E =
+
+# Extension for object files.
+O = .o
+
+# Prefix for library file name.
+LP = lib
+
+# Extension for library file name.
+L = .a
+
+# Prefix for DLL file name.
+DP = lib
+
+# Extension for DLL file name.
+D = .so
+
+# Output file names can be overridden directly. By default, they are
+# assembled using the prefix/extension macros defined above.
+# BEARSSLLIB = libbearssl.a
+# BEARSSLDLL = libbearssl.so
+# BRSSL = brssl
+# TESTCRYPTO = testcrypto
+# TESTSPEED = testspeed
+# TESTX509 = testx509
+
+# File deletion tool.
+RM = rm -f
+
+# Directory creation tool.
+MKDIR = mkdir -p
+
+# C compiler and flags.
+CC = ../../../ccomp -fstruct-passing -fpostpass= ilp -U__SIZEOF_INT128__ -U__SIZEOF_FLOAT128__
+CFLAGS = -W -Wall -Wno-c11-extensions -O3 -D_POSIX_C_SOURCE=200909L
+CCOUT = -c -o
+
+# Static library building tool.
+AR = ar
+ARFLAGS = -rcs
+AROUT =
+
+# DLL building tool.
+LDDLL = cc
+LDDLLFLAGS = -shared
+LDDLLOUT = -o
+
+# Static linker.
+LD = $(CC)
+LDFLAGS = ../clock.gcc.kvx.o
+LDOUT = -o
+
+# C# compiler; we assume usage of Mono.
+MKT0COMP = mk$PmkT0.sh
+RUNT0COMP = mono T0Comp.exe
+
+# Set the values to 'no' to disable building of the corresponding element
+# by default. Building can still be invoked with an explicit target call
+# (e.g. 'make dll' to force build the DLL).
+#STATICLIB = no
+DLL = no
+#TOOLS = no
+#TESTS = no
diff --git a/test/monniaux/BearSSL/conf/Unix.mk b/test/monniaux/BearSSL/conf/Unix.mk
new file mode 100644
index 00000000..02f2b2be
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/Unix.mk
@@ -0,0 +1,69 @@
+# Configuration for a native build on a generic Unix-like system.
+
+# Build directory.
+BUILD = build
+
+# Extension for executable files.
+E =
+
+# Extension for object files.
+O = .o
+
+# Prefix for library file name.
+LP = lib
+
+# Extension for library file name.
+L = .a
+
+# Prefix for DLL file name.
+DP = lib
+
+# Extension for DLL file name.
+D = .so
+
+# Output file names can be overridden directly. By default, they are
+# assembled using the prefix/extension macros defined above.
+# BEARSSLLIB = libbearssl.a
+# BEARSSLDLL = libbearssl.so
+# BRSSL = brssl
+# TESTCRYPTO = testcrypto
+# TESTSPEED = testspeed
+# TESTX509 = testx509
+
+# File deletion tool.
+RM = rm -f
+
+# Directory creation tool.
+MKDIR = mkdir -p
+
+# C compiler and flags.
+CC = cc
+CFLAGS = -W -Wall -Os -fPIC
+CCOUT = -c -o
+
+# Static library building tool.
+AR = ar
+ARFLAGS = -rcs
+AROUT =
+
+# DLL building tool.
+LDDLL = cc
+LDDLLFLAGS = -shared
+LDDLLOUT = -o
+
+# Static linker.
+LD = cc
+LDFLAGS =
+LDOUT = -o
+
+# C# compiler; we assume usage of Mono.
+MKT0COMP = mk$PmkT0.sh
+RUNT0COMP = mono T0Comp.exe
+
+# Set the values to 'no' to disable building of the corresponding element
+# by default. Building can still be invoked with an explicit target call
+# (e.g. 'make dll' to force build the DLL).
+#STATICLIB = no
+#DLL = no
+#TOOLS = no
+#TESTS = no
diff --git a/test/monniaux/BearSSL/conf/Unix32.mk b/test/monniaux/BearSSL/conf/Unix32.mk
new file mode 100644
index 00000000..0d3bed88
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/Unix32.mk
@@ -0,0 +1,12 @@
+# Example configuration file for compiling on a Unix-like system with
+# GCC, targeting a 32-bit output. Moreover, it enables the "LOMUL" setting
+# to make the code select the "small" integer implementations (i15, m15,
+# ctmul32...), which is not necessarily a good idea for performance, but
+# handy for tests.
+
+include conf/Unix.mk
+
+BUILD = build32
+CFLAGS = -W -Wall -Os -fPIC -m32 -DBR_LOMUL
+LDFLAGS = -m32
+LDDLLFLAGS = -shared -m32
diff --git a/test/monniaux/BearSSL/conf/UnixClang.mk b/test/monniaux/BearSSL/conf/UnixClang.mk
new file mode 100644
index 00000000..3636cf89
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/UnixClang.mk
@@ -0,0 +1,11 @@
+# Example configuration file for compiling on a Unix-like system with
+# clang as compiler instead of gcc.
+
+# We are on a Unix system so we assume a Single Unix compatible 'make'
+# utility, and Unix defaults.
+include conf/Unix.mk
+
+BUILD = bclang
+CC = clang
+LD = clang
+LDDLL = clang
diff --git a/test/monniaux/BearSSL/conf/UnixCompCert.mk b/test/monniaux/BearSSL/conf/UnixCompCert.mk
new file mode 100644
index 00000000..fefb2938
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/UnixCompCert.mk
@@ -0,0 +1,69 @@
+# Configuration for a native build on a generic Unix-like system.
+
+# Build directory.
+BUILD = build
+
+# Extension for executable files.
+E =
+
+# Extension for object files.
+O = .o
+
+# Prefix for library file name.
+LP = lib
+
+# Extension for library file name.
+L = .a
+
+# Prefix for DLL file name.
+DP = lib
+
+# Extension for DLL file name.
+D = .so
+
+# Output file names can be overridden directly. By default, they are
+# assembled using the prefix/extension macros defined above.
+# BEARSSLLIB = libbearssl.a
+# BEARSSLDLL = libbearssl.so
+# BRSSL = brssl
+# TESTCRYPTO = testcrypto
+# TESTSPEED = testspeed
+# TESTX509 = testx509
+
+# File deletion tool.
+RM = rm -f
+
+# Directory creation tool.
+MKDIR = mkdir -p
+
+# C compiler and flags.
+CC = /opt/CompCert/3.4/x86_64-linux/bin/ccomp
+CFLAGS = -W -Wall -O3 -D_POSIX_C_SOURCE=200909L -U__SIZEOF_INT128__ -U__SIZEOF_FLOAT128__ -fstruct-passing
+CCOUT = -c -o
+
+# Static library building tool.
+AR = ar
+ARFLAGS = -rcs
+AROUT =
+
+# DLL building tool.
+LDDLL = cc
+LDDLLFLAGS = -shared
+LDDLLOUT = -o
+
+# Static linker.
+LD = $(CC)
+LDFLAGS =
+LDOUT = -o
+
+# C# compiler; we assume usage of Mono.
+MKT0COMP = mk$PmkT0.sh
+RUNT0COMP = mono T0Comp.exe
+
+# Set the values to 'no' to disable building of the corresponding element
+# by default. Building can still be invoked with an explicit target call
+# (e.g. 'make dll' to force build the DLL).
+#STATICLIB = no
+DLL = no
+#TOOLS = no
+#TESTS = no
diff --git a/test/monniaux/BearSSL/conf/Win.mk b/test/monniaux/BearSSL/conf/Win.mk
new file mode 100644
index 00000000..2ed4bb68
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/Win.mk
@@ -0,0 +1,70 @@
+# Configuration for a native build on a Windows system with Visual Studio.
+
+# Build directory.
+BUILD = build
+
+# Extension for executable files.
+E = .exe
+
+# Extension for object files.
+O = .obj
+
+# Prefix for static library file name.
+LP =
+
+# Extension for static library file name. We add an 's' so that the
+# name is distinct from the 'import library' generated along with the DLL.
+L = s.lib
+
+# Prefix for DLL file name.
+DP =
+
+# Extension for DLL file name.
+D = .dll
+
+# Output file names can be overridden directly. By default, they are
+# assembled using the prefix/extension macros defined above.
+# BEARSSLLIB = bearssls.lib
+# BEARSSLDLL = bearssl.dll
+# BRSSL = brssl.exe
+# TESTCRYPTO = testcrypto.exe
+# TESTSPEED = testspeed.exe
+# TESTX509 = testx509.exe
+
+# File deletion tool.
+RM = del /Q
+
+# Directory creation tool.
+MKDIR = mkdir
+
+# C compiler and flags.
+CC = cl
+CFLAGS = -nologo -W2 -O2
+CCOUT = -c -Fo
+
+# Static library building tool.
+AR = lib
+ARFLAGS = -nologo
+AROUT = -out:
+
+# DLL building tool.
+LDDLL = cl
+LDDLLFLAGS = -nologo -LD -MT
+LDDLLOUT = -Fe
+
+# Static linker.
+LD = cl
+LDFLAGS = -nologo
+LDOUT = -Fe
+
+# C# compiler.
+MKT0COMP = mk$PmkT0.cmd
+RUNT0COMP = T0Comp.exe
+
+# Set the values to 'no' to disable building of the corresponding element
+# by default. Building can still be invoked with an explicit target call
+# (e.g. 'make dll' to force build the DLL).
+#STATICLIB = no
+#DLL = no
+#TOOLS = no
+#TESTS = no
diff --git a/test/monniaux/BearSSL/conf/samd20.mk b/test/monniaux/BearSSL/conf/samd20.mk
new file mode 100644
index 00000000..5b3d5004
--- /dev/null
+++ b/test/monniaux/BearSSL/conf/samd20.mk
@@ -0,0 +1,20 @@
+# Example configuration file for compiling for an Atmel SAM D20 Xplained
+# Pro evaluation kit, on a Unix-like system, with a GNU toolchain.
+
+# We are on a Unix system so we assume a Single Unix compatible 'make'
+# utility, and Unix defaults.
+include conf/Unix.mk
+
+# We override the build directory.
+BUILD = samd20
+
+# C compiler, linker, and static library builder.
+CC = arm-none-eabi-gcc
+CFLAGS = -W -Wall -Os -mthumb -ffunction-sections -fdata-sections -mcpu=cortex-m0plus -DBR_ARMEL_CORTEXM_GCC
+LD = arm-none-eabi-gcc
+AR = arm-none-eabi-ar
+
+# We compile only the static library.
+DLL = no
+TOOLS = no
+TESTS = no
diff --git a/test/monniaux/BearSSL/inc/bearssl.h b/test/monniaux/BearSSL/inc/bearssl.h
new file mode 100644
index 00000000..4f4797cf
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl.h
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_H__
+#define BR_BEARSSL_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+/** \mainpage BearSSL API
+ *
+ * # API Layout
+ *
+ * The functions and structures defined by the BearSSL API are located
+ * in various header files:
+ *
+ * | Header file | Elements |
+ * | :-------------- | :------------------------------------------------ |
+ * | bearssl_hash.h | Hash functions |
+ * | bearssl_hmac.h | HMAC |
+ * | bearssl_kdf.h | Key Derivation Functions |
+ * | bearssl_rand.h | Pseudorandom byte generators |
+ * | bearssl_prf.h | PRF implementations (for SSL/TLS) |
+ * | bearssl_block.h | Symmetric encryption |
+ * | bearssl_aead.h | AEAD algorithms (combined encryption + MAC) |
+ * | bearssl_rsa.h | RSA encryption and signatures |
+ * | bearssl_ec.h | Elliptic curves support (including ECDSA) |
+ * | bearssl_ssl.h | SSL/TLS engine interface |
+ * | bearssl_x509.h | X.509 certificate decoding and validation |
+ * | bearssl_pem.h | Base64/PEM decoding support functions |
+ *
+ * Applications using BearSSL are supposed to simply include `bearssl.h`
+ * as follows:
+ *
+ * #include <bearssl.h>
+ *
+ * The `bearssl.h` file itself includes all the other header files. It is
+ * possible to include specific header files, but it has no practical
+ * advantage for the application. The API is separated into separate
+ * header files only for documentation convenience.
+ *
+ *
+ * # Conventions
+ *
+ * ## MUST and SHALL
+ *
+ * In all descriptions, the usual "MUST", "SHALL", "MAY",... terminology
+ * is used. Failure to meet requirements expressed with a "MUST" or
+ * "SHALL" implies undefined behaviour, which means that segmentation
+ * faults, buffer overflows, and other similar adverse events, may occur.
+ *
+ * In general, BearSSL is not very forgiving of programming errors, and
+ * does not include much failsafes or error reporting when the problem
+ * does not arise from external transient conditions, and can be fixed
+ * only in the application code. This is done so in order to make the
+ * total code footprint lighter.
+ *
+ *
+ * ## `NULL` values
+ *
+ * Function parameters with a pointer type shall not be `NULL` unless
+ * explicitly authorised by the documentation. As an exception, when
+ * the pointer aims at a sequence of bytes and is accompanied with
+ * a length parameter, and the length is zero (meaning that there is
+ * no byte at all to retrieve), then the pointer may be `NULL` even if
+ * not explicitly allowed.
+ *
+ *
+ * ## Memory Allocation
+ *
+ * BearSSL does not perform dynamic memory allocation. This implies that
+ * for any functionality that requires a non-transient state, the caller
+ * is responsible for allocating the relevant context structure. Such
+ * allocation can be done in any appropriate area, including static data
+ * segments, the heap, and the stack, provided that proper alignment is
+ * respected. The header files define these context structures
+ * (including size and contents), so the C compiler should handle
+ * alignment automatically.
+ *
+ * Since there is no dynamic resource allocation, there is also nothing to
+ * release. When the calling code is done with a BearSSL feature, it
+ * may simple release the context structures it allocated itself, with
+ * no "close function" to call. If the context structures were allocated
+ * on the stack (as local variables), then even that release operation is
+ * implicit.
+ *
+ *
+ * ## Structure Contents
+ *
+ * Except when explicitly indicated, structure contents are opaque: they
+ * are included in the header files so that calling code may know the
+ * structure sizes and alignment requirements, but callers SHALL NOT
+ * access individual fields directly. For fields that are supposed to
+ * be read from or written to, the API defines accessor functions (the
+ * simplest of these accessor functions are defined as `static inline`
+ * functions, and the C compiler will optimise them away).
+ *
+ *
+ * # API Usage
+ *
+ * BearSSL usage for running a SSL/TLS client or server is described
+ * on the [BearSSL Web site](https://www.bearssl.org/api1.html). The
+ * BearSSL source archive also comes with sample code.
+ */
+
+#include "bearssl_hash.h"
+#include "bearssl_hmac.h"
+#include "bearssl_kdf.h"
+#include "bearssl_rand.h"
+#include "bearssl_prf.h"
+#include "bearssl_block.h"
+#include "bearssl_aead.h"
+#include "bearssl_rsa.h"
+#include "bearssl_ec.h"
+#include "bearssl_ssl.h"
+#include "bearssl_x509.h"
+#include "bearssl_pem.h"
+
+/** \brief Type for a configuration option.
+ *
+ * A "configuration option" is a value that is selected when the BearSSL
+ * library itself is compiled. Most options are boolean; their value is
+ * then either 1 (option is enabled) or 0 (option is disabled). Some
+ * values have other integer values. Option names correspond to macro
+ * names. Some of the options can be explicitly set in the internal
+ * `"config.h"` file.
+ */
+typedef struct {
+ /** \brief Configurable option name. */
+ const char *name;
+ /** \brief Configurable option value. */
+ long value;
+} br_config_option;
+
+/** \brief Get configuration report.
+ *
+ * This function returns compiled configuration options, each as a
+ * 'long' value. Names match internal macro names, in particular those
+ * that can be set in the `"config.h"` inner file. For boolean options,
+ * the numerical value is 1 if enabled, 0 if disabled. For maximum
+ * key sizes, values are expressed in bits.
+ *
+ * The returned array is terminated by an entry whose `name` is `NULL`.
+ *
+ * \return the configuration report.
+ */
+const br_config_option *br_get_config(void);
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_aead.h b/test/monniaux/BearSSL/inc/bearssl_aead.h
new file mode 100644
index 00000000..8e35a1fd
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_aead.h
@@ -0,0 +1,1059 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_AEAD_H__
+#define BR_BEARSSL_AEAD_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_block.h"
+#include "bearssl_hash.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_aead.h
+ *
+ * # Authenticated Encryption with Additional Data
+ *
+ * This file documents the API for AEAD encryption.
+ *
+ *
+ * ## Procedural API
+ *
+ * An AEAD algorithm processes messages and provides confidentiality
+ * (encryption) and checked integrity (MAC). It uses the following
+ * parameters:
+ *
+ * - A symmetric key. Exact size depends on the AEAD algorithm.
+ *
+ * - A nonce (IV). Size depends on the AEAD algorithm; for most
+ * algorithms, it is crucial for security that any given nonce
+ * value is never used twice for the same key and distinct
+ * messages.
+ *
+ * - Data to encrypt and protect.
+ *
+ * - Additional authenticated data, which is covered by the MAC but
+ * otherwise left untouched (i.e. not encrypted).
+ *
+ * The AEAD algorithm encrypts the data, and produces an authentication
+ * tag. It is assumed that the encrypted data, the tag, the additional
+ * authenticated data and the nonce are sent to the receiver; the
+ * additional data and the nonce may be implicit (e.g. using elements of
+ * the underlying transport protocol, such as record sequence numbers).
+ * The receiver will recompute the tag value and compare it with the one
+ * received; if they match, then the data is correct, and can be
+ * decrypted and used; otherwise, at least one of the elements was
+ * altered in transit, normally leading to wholesale rejection of the
+ * complete message.
+ *
+ * For each AEAD algorithm, identified by a symbolic name (hereafter
+ * denoted as "`xxx`"), the following functions are defined:
+ *
+ * - `br_xxx_init()`
+ *
+ * Initialise the AEAD algorithm, on a provided context structure.
+ * Exact parameters depend on the algorithm, and may include
+ * pointers to extra implementations and context structures. The
+ * secret key is provided at this point, either directly or
+ * indirectly.
+ *
+ * - `br_xxx_reset()`
+ *
+ * Start a new AEAD computation. The nonce value is provided as
+ * parameter to this function.
+ *
+ * - `br_xxx_aad_inject()`
+ *
+ * Inject some additional authenticated data. Additional data may
+ * be provided in several chunks of arbitrary length.
+ *
+ * - `br_xxx_flip()`
+ *
+ * This function MUST be called after injecting all additional
+ * authenticated data, and before beginning to encrypt the plaintext
+ * (or decrypt the ciphertext).
+ *
+ * - `br_xxx_run()`
+ *
+ * Process some plaintext (to encrypt) or ciphertext (to decrypt).
+ * Encryption/decryption is done in place. Data may be provided in
+ * several chunks of arbitrary length.
+ *
+ * - `br_xxx_get_tag()`
+ *
+ * Compute the authentication tag. All message data (encrypted or
+ * decrypted) must have been injected at that point. Also, this
+ * call may modify internal context elements, so it may be called
+ * only once for a given AEAD computation.
+ *
+ * - `br_xxx_check_tag()`
+ *
+ * An alternative to `br_xxx_get_tag()`, meant to be used by the
+ * receiver: the authentication tag is internally recomputed, and
+ * compared with the one provided as parameter.
+ *
+ * This API makes the following assumptions on the AEAD algorithm:
+ *
+ * - Encryption does not expand the size of the ciphertext; there is
+ * no padding. This is true of most modern AEAD modes such as GCM.
+ *
+ * - The additional authenticated data must be processed first,
+ * before the encrypted/decrypted data.
+ *
+ * - Nonce, plaintext and additional authenticated data all consist
+ * in an integral number of bytes. There is no provision to use
+ * elements whose length in bits is not a multiple of 8.
+ *
+ * Each AEAD algorithm has its own requirements and limits on the sizes
+ * of additional data and plaintext. This API does not provide any
+ * way to report invalid usage; it is up to the caller to ensure that
+ * the provided key, nonce, and data elements all fit the algorithm's
+ * requirements.
+ *
+ *
+ * ## Object-Oriented API
+ *
+ * Each context structure begins with a field (called `vtable`) that
+ * points to an instance of a structure that references the relevant
+ * functions through pointers. Each such structure contains the
+ * following:
+ *
+ * - `reset`
+ *
+ * Pointer to the reset function, that allows starting a new
+ * computation.
+ *
+ * - `aad_inject`
+ *
+ * Pointer to the additional authenticated data injection function.
+ *
+ * - `flip`
+ *
+ * Pointer to the function that transitions from additional data
+ * to main message data processing.
+ *
+ * - `get_tag`
+ *
+ * Pointer to the function that computes and returns the tag.
+ *
+ * - `check_tag`
+ *
+ * Pointer to the function that computes and verifies the tag against
+ * a received value.
+ *
+ * Note that there is no OOP method for context initialisation: the
+ * various AEAD algorithms have different requirements that would not
+ * map well to a single initialisation API.
+ *
+ * The OOP API is not provided for CCM, due to its specific requirements
+ * (length of plaintext must be known in advance).
+ */
+
+/**
+ * \brief Class type of an AEAD algorithm.
+ */
+typedef struct br_aead_class_ br_aead_class;
+struct br_aead_class_ {
+
+ /**
+ * \brief Size (in bytes) of authentication tags created by
+ * this AEAD algorithm.
+ */
+ size_t tag_size;
+
+ /**
+ * \brief Reset an AEAD context.
+ *
+ * This function resets an already initialised AEAD context for
+ * a new computation run. Implementations and keys are
+ * conserved. This function can be called at any time; it
+ * cancels any ongoing AEAD computation that uses the provided
+ * context structure.
+
+ * The provided IV is a _nonce_. Each AEAD algorithm has its
+ * own requirements on IV size and contents; for most of them,
+ * it is crucial to security that each nonce value is used
+ * only once for a given secret key.
+ *
+ * \param cc AEAD context structure.
+ * \param iv AEAD nonce to use.
+ * \param len AEAD nonce length (in bytes).
+ */
+ void (*reset)(const br_aead_class **cc, const void *iv, size_t len);
+
+ /**
+ * \brief Inject additional authenticated data.
+ *
+ * The provided data is injected into a running AEAD
+ * computation. Additional data must be injected _before_ the
+ * call to `flip()`. Additional data can be injected in several
+ * chunks of arbitrary length.
+ *
+ * \param cc AEAD context structure.
+ * \param data pointer to additional authenticated data.
+ * \param len length of additional authenticated data (in bytes).
+ */
+ void (*aad_inject)(const br_aead_class **cc,
+ const void *data, size_t len);
+
+ /**
+ * \brief Finish injection of additional authenticated data.
+ *
+ * This function MUST be called before beginning the actual
+ * encryption or decryption (with `run()`), even if no
+ * additional authenticated data was injected. No additional
+ * authenticated data may be injected after this function call.
+ *
+ * \param cc AEAD context structure.
+ */
+ void (*flip)(const br_aead_class **cc);
+
+ /**
+ * \brief Encrypt or decrypt some data.
+ *
+ * Data encryption or decryption can be done after `flip()` has
+ * been called on the context. If `encrypt` is non-zero, then
+ * the provided data shall be plaintext, and it is encrypted in
+ * place. Otherwise, the data shall be ciphertext, and it is
+ * decrypted in place.
+ *
+ * Data may be provided in several chunks of arbitrary length.
+ *
+ * \param cc AEAD context structure.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+ void (*run)(const br_aead_class **cc, int encrypt,
+ void *data, size_t len);
+
+ /**
+ * \brief Compute authentication tag.
+ *
+ * Compute the AEAD authentication tag. The tag length depends
+ * on the AEAD algorithm; it is written in the provided `tag`
+ * buffer. This call terminates the AEAD run: no data may be
+ * processed with that AEAD context afterwards, until `reset()`
+ * is called to initiate a new AEAD run.
+ *
+ * The tag value must normally be sent along with the encrypted
+ * data. When decrypting, the tag value must be recomputed and
+ * compared with the received tag: if the two tag values differ,
+ * then either the tag or the encrypted data was altered in
+ * transit. As an alternative to this function, the
+ * `check_tag()` function may be used to compute and check the
+ * tag value.
+ *
+ * Tag length depends on the AEAD algorithm.
+ *
+ * \param cc AEAD context structure.
+ * \param tag destination buffer for the tag.
+ */
+ void (*get_tag)(const br_aead_class **cc, void *tag);
+
+ /**
+ * \brief Compute and check authentication tag.
+ *
+ * This function is an alternative to `get_tag()`, and is
+ * normally used on the receiving end (i.e. when decrypting
+ * messages). The tag value is recomputed and compared with the
+ * provided tag value. If they match, 1 is returned; on
+ * mismatch, 0 is returned. A returned value of 0 means that the
+ * data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * Tag length depends on the AEAD algorithm.
+ *
+ * \param cc AEAD context structure.
+ * \param tag tag value to compare with.
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+ uint32_t (*check_tag)(const br_aead_class **cc, const void *tag);
+
+ /**
+ * \brief Compute authentication tag (with truncation).
+ *
+ * This function is similar to `get_tag()`, except that the tag
+ * length is provided. Some AEAD algorithms allow several tag
+ * lengths, usually by truncating the normal tag. Shorter tags
+ * mechanically increase success probability of forgeries.
+ * The range of allowed tag lengths depends on the algorithm.
+ *
+ * \param cc AEAD context structure.
+ * \param tag destination buffer for the tag.
+ * \param len tag length (in bytes).
+ */
+ void (*get_tag_trunc)(const br_aead_class **cc, void *tag, size_t len);
+
+ /**
+ * \brief Compute and check authentication tag (with truncation).
+ *
+ * This function is similar to `check_tag()` except that it
+ * works over an explicit tag length. See `get_tag()` for a
+ * discussion of explicit tag lengths; the range of allowed tag
+ * lengths depends on the algorithm.
+ *
+ * \param cc AEAD context structure.
+ * \param tag tag value to compare with.
+ * \param len tag length (in bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+ uint32_t (*check_tag_trunc)(const br_aead_class **cc,
+ const void *tag, size_t len);
+};
+
+/**
+ * \brief Context structure for GCM.
+ *
+ * GCM is an AEAD mode that combines a block cipher in CTR mode with a
+ * MAC based on GHASH, to provide authenticated encryption:
+ *
+ * - Any block cipher with 16-byte blocks can be used with GCM.
+ *
+ * - The nonce can have any length, from 0 up to 2^64-1 bits; however,
+ * 96-bit nonces (12 bytes) are recommended (nonces with a length
+ * distinct from 12 bytes are internally hashed, which risks reusing
+ * nonce value with a small but not always negligible probability).
+ *
+ * - Additional authenticated data may have length up to 2^64-1 bits.
+ *
+ * - Message length may range up to 2^39-256 bits at most.
+ *
+ * - The authentication tag has length 16 bytes.
+ *
+ * The GCM initialisation function receives as parameter an
+ * _initialised_ block cipher implementation context, with the secret
+ * key already set. A pointer to that context will be kept within the
+ * GCM context structure. It is up to the caller to allocate and
+ * initialise that block cipher context.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_aead_class *vtable;
+
+#ifndef BR_DOXYGEN_IGNORE
+ const br_block_ctr_class **bctx;
+ br_ghash gh;
+ unsigned char h[16];
+ unsigned char j0_1[12];
+ unsigned char buf[16];
+ unsigned char y[16];
+ uint32_t j0_2, jc;
+ uint64_t count_aad, count_ctr;
+#endif
+} br_gcm_context;
+
+/**
+ * \brief Initialize a GCM context.
+ *
+ * A block cipher implementation, with its initialised context structure,
+ * is provided. The block cipher MUST use 16-byte blocks in CTR mode,
+ * and its secret key MUST have been already set in the provided context.
+ * A GHASH implementation must also be provided. The parameters are linked
+ * in the GCM context.
+ *
+ * After this function has been called, the `br_gcm_reset()` function must
+ * be called, to provide the IV for GCM computation.
+ *
+ * \param ctx GCM context structure.
+ * \param bctx block cipher context (already initialised with secret key).
+ * \param gh GHASH implementation.
+ */
+void br_gcm_init(br_gcm_context *ctx,
+ const br_block_ctr_class **bctx, br_ghash gh);
+
+/**
+ * \brief Reset a GCM context.
+ *
+ * This function resets an already initialised GCM context for a new
+ * computation run. Implementations and keys are conserved. This function
+ * can be called at any time; it cancels any ongoing GCM computation that
+ * uses the provided context structure.
+ *
+ * The provided IV is a _nonce_. It is critical to GCM security that IV
+ * values are not repeated for the same encryption key. IV can have
+ * arbitrary length (up to 2^64-1 bits), but the "normal" length is
+ * 96 bits (12 bytes).
+ *
+ * \param ctx GCM context structure.
+ * \param iv GCM nonce to use.
+ * \param len GCM nonce length (in bytes).
+ */
+void br_gcm_reset(br_gcm_context *ctx, const void *iv, size_t len);
+
+/**
+ * \brief Inject additional authenticated data into GCM.
+ *
+ * The provided data is injected into a running GCM computation. Additional
+ * data must be injected _before_ the call to `br_gcm_flip()`.
+ * Additional data can be injected in several chunks of arbitrary length;
+ * the maximum total size of additional authenticated data is 2^64-1
+ * bits.
+ *
+ * \param ctx GCM context structure.
+ * \param data pointer to additional authenticated data.
+ * \param len length of additional authenticated data (in bytes).
+ */
+void br_gcm_aad_inject(br_gcm_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Finish injection of additional authenticated data into GCM.
+ *
+ * This function MUST be called before beginning the actual encryption
+ * or decryption (with `br_gcm_run()`), even if no additional authenticated
+ * data was injected. No additional authenticated data may be injected
+ * after this function call.
+ *
+ * \param ctx GCM context structure.
+ */
+void br_gcm_flip(br_gcm_context *ctx);
+
+/**
+ * \brief Encrypt or decrypt some data with GCM.
+ *
+ * Data encryption or decryption can be done after `br_gcm_flip()`
+ * has been called on the context. If `encrypt` is non-zero, then the
+ * provided data shall be plaintext, and it is encrypted in place.
+ * Otherwise, the data shall be ciphertext, and it is decrypted in place.
+ *
+ * Data may be provided in several chunks of arbitrary length. The maximum
+ * total length for data is 2^39-256 bits, i.e. about 65 gigabytes.
+ *
+ * \param ctx GCM context structure.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+void br_gcm_run(br_gcm_context *ctx, int encrypt, void *data, size_t len);
+
+/**
+ * \brief Compute GCM authentication tag.
+ *
+ * Compute the GCM authentication tag. The tag is a 16-byte value which
+ * is written in the provided `tag` buffer. This call terminates the
+ * GCM run: no data may be processed with that GCM context afterwards,
+ * until `br_gcm_reset()` is called to initiate a new GCM run.
+ *
+ * The tag value must normally be sent along with the encrypted data.
+ * When decrypting, the tag value must be recomputed and compared with
+ * the received tag: if the two tag values differ, then either the tag
+ * or the encrypted data was altered in transit. As an alternative to
+ * this function, the `br_gcm_check_tag()` function can be used to
+ * compute and check the tag value.
+ *
+ * \param ctx GCM context structure.
+ * \param tag destination buffer for the tag (16 bytes).
+ */
+void br_gcm_get_tag(br_gcm_context *ctx, void *tag);
+
+/**
+ * \brief Compute and check GCM authentication tag.
+ *
+ * This function is an alternative to `br_gcm_get_tag()`, normally used
+ * on the receiving end (i.e. when decrypting value). The tag value is
+ * recomputed and compared with the provided tag value. If they match, 1
+ * is returned; on mismatch, 0 is returned. A returned value of 0 means
+ * that the data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * \param ctx GCM context structure.
+ * \param tag tag value to compare with (16 bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+uint32_t br_gcm_check_tag(br_gcm_context *ctx, const void *tag);
+
+/**
+ * \brief Compute GCM authentication tag (with truncation).
+ *
+ * This function is similar to `br_gcm_get_tag()`, except that it allows
+ * the tag to be truncated to a smaller length. The intended tag length
+ * is provided as `len` (in bytes); it MUST be no more than 16, but
+ * it may be smaller. Note that decreasing tag length mechanically makes
+ * forgeries easier; NIST SP 800-38D specifies that the tag length shall
+ * lie between 12 and 16 bytes (inclusive), but may be truncated down to
+ * 4 or 8 bytes, for specific applications that can tolerate it. It must
+ * also be noted that successful forgeries leak information on the
+ * authentication key, making subsequent forgeries easier. Therefore,
+ * tag truncation, and in particular truncation to sizes lower than 12
+ * bytes, shall be envisioned only with great care.
+ *
+ * The tag is written in the provided `tag` buffer. This call terminates
+ * the GCM run: no data may be processed with that GCM context
+ * afterwards, until `br_gcm_reset()` is called to initiate a new GCM
+ * run.
+ *
+ * The tag value must normally be sent along with the encrypted data.
+ * When decrypting, the tag value must be recomputed and compared with
+ * the received tag: if the two tag values differ, then either the tag
+ * or the encrypted data was altered in transit. As an alternative to
+ * this function, the `br_gcm_check_tag_trunc()` function can be used to
+ * compute and check the tag value.
+ *
+ * \param ctx GCM context structure.
+ * \param tag destination buffer for the tag.
+ * \param len tag length (16 bytes or less).
+ */
+void br_gcm_get_tag_trunc(br_gcm_context *ctx, void *tag, size_t len);
+
+/**
+ * \brief Compute and check GCM authentication tag (with truncation).
+ *
+ * This function is an alternative to `br_gcm_get_tag_trunc()`, normally used
+ * on the receiving end (i.e. when decrypting value). The tag value is
+ * recomputed and compared with the provided tag value. If they match, 1
+ * is returned; on mismatch, 0 is returned. A returned value of 0 means
+ * that the data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * Tag length MUST be 16 bytes or less. The normal GCM tag length is 16
+ * bytes. See `br_check_tag_trunc()` for some discussion on the potential
+ * perils of truncating authentication tags.
+ *
+ * \param ctx GCM context structure.
+ * \param tag tag value to compare with.
+ * \param len tag length (in bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+uint32_t br_gcm_check_tag_trunc(br_gcm_context *ctx,
+ const void *tag, size_t len);
+
+/**
+ * \brief Class instance for GCM.
+ */
+extern const br_aead_class br_gcm_vtable;
+
+/**
+ * \brief Context structure for EAX.
+ *
+ * EAX is an AEAD mode that combines a block cipher in CTR mode with
+ * CBC-MAC using the same block cipher and the same key, to provide
+ * authenticated encryption:
+ *
+ * - Any block cipher with 16-byte blocks can be used with EAX
+ * (technically, other block sizes are defined as well, but this
+ * is not implemented by these functions; shorter blocks also
+ * imply numerous security issues).
+ *
+ * - The nonce can have any length, as long as nonce values are
+ * not reused (thus, if nonces are randomly selected, the nonce
+ * size should be such that reuse probability is negligible).
+ *
+ * - Additional authenticated data length is unlimited.
+ *
+ * - Message length is unlimited.
+ *
+ * - The authentication tag has length 16 bytes.
+ *
+ * The EAX initialisation function receives as parameter an
+ * _initialised_ block cipher implementation context, with the secret
+ * key already set. A pointer to that context will be kept within the
+ * EAX context structure. It is up to the caller to allocate and
+ * initialise that block cipher context.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_aead_class *vtable;
+
+#ifndef BR_DOXYGEN_IGNORE
+ const br_block_ctrcbc_class **bctx;
+ unsigned char L2[16];
+ unsigned char L4[16];
+ unsigned char nonce[16];
+ unsigned char head[16];
+ unsigned char ctr[16];
+ unsigned char cbcmac[16];
+ unsigned char buf[16];
+ size_t ptr;
+#endif
+} br_eax_context;
+
+/**
+ * \brief EAX captured state.
+ *
+ * Some internal values computed by EAX may be captured at various
+ * points, and reused for another EAX run with the same secret key,
+ * for lower per-message overhead. Captured values do not depend on
+ * the nonce.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char st[3][16];
+#endif
+} br_eax_state;
+
+/**
+ * \brief Initialize an EAX context.
+ *
+ * A block cipher implementation, with its initialised context
+ * structure, is provided. The block cipher MUST use 16-byte blocks in
+ * CTR + CBC-MAC mode, and its secret key MUST have been already set in
+ * the provided context. The parameters are linked in the EAX context.
+ *
+ * After this function has been called, the `br_eax_reset()` function must
+ * be called, to provide the nonce for EAX computation.
+ *
+ * \param ctx EAX context structure.
+ * \param bctx block cipher context (already initialised with secret key).
+ */
+void br_eax_init(br_eax_context *ctx, const br_block_ctrcbc_class **bctx);
+
+/**
+ * \brief Capture pre-AAD state.
+ *
+ * This function precomputes key-dependent data, and stores it in the
+ * provided `st` structure. This structure should then be used with
+ * `br_eax_reset_pre_aad()`, or updated with `br_eax_get_aad_mac()`
+ * and then used with `br_eax_reset_post_aad()`.
+ *
+ * The EAX context structure is unmodified by this call.
+ *
+ * \param ctx EAX context structure.
+ * \param st recipient for captured state.
+ */
+void br_eax_capture(const br_eax_context *ctx, br_eax_state *st);
+
+/**
+ * \brief Reset an EAX context.
+ *
+ * This function resets an already initialised EAX context for a new
+ * computation run. Implementations and keys are conserved. This function
+ * can be called at any time; it cancels any ongoing EAX computation that
+ * uses the provided context structure.
+ *
+ * It is critical to EAX security that nonce values are not repeated for
+ * the same encryption key. Nonces can have arbitrary length. If nonces
+ * are randomly generated, then a nonce length of at least 128 bits (16
+ * bytes) is recommended, to make nonce reuse probability sufficiently
+ * low.
+ *
+ * \param ctx EAX context structure.
+ * \param nonce EAX nonce to use.
+ * \param len EAX nonce length (in bytes).
+ */
+void br_eax_reset(br_eax_context *ctx, const void *nonce, size_t len);
+
+/**
+ * \brief Reset an EAX context with a pre-AAD captured state.
+ *
+ * This function is an alternative to `br_eax_reset()`, that reuses a
+ * previously captured state structure for lower per-message overhead.
+ * The state should have been populated with `br_eax_capture_state()`
+ * but not updated with `br_eax_get_aad_mac()`.
+ *
+ * After this function is called, additional authenticated data MUST
+ * be injected. At least one byte of additional authenticated data
+ * MUST be provided with `br_eax_aad_inject()`; computation result will
+ * be incorrect if `br_eax_flip()` is called right away.
+ *
+ * After injection of the AAD and call to `br_eax_flip()`, at least
+ * one message byte must be provided. Empty messages are not supported
+ * with this reset mode.
+ *
+ * \param ctx EAX context structure.
+ * \param st pre-AAD captured state.
+ * \param nonce EAX nonce to use.
+ * \param len EAX nonce length (in bytes).
+ */
+void br_eax_reset_pre_aad(br_eax_context *ctx, const br_eax_state *st,
+ const void *nonce, size_t len);
+
+/**
+ * \brief Reset an EAX context with a post-AAD captured state.
+ *
+ * This function is an alternative to `br_eax_reset()`, that reuses a
+ * previously captured state structure for lower per-message overhead.
+ * The state should have been populated with `br_eax_capture_state()`
+ * and then updated with `br_eax_get_aad_mac()`.
+ *
+ * After this function is called, message data MUST be injected. The
+ * `br_eax_flip()` function MUST NOT be called. At least one byte of
+ * message data MUST be provided with `br_eax_run()`; empty messages
+ * are not supported with this reset mode.
+ *
+ * \param ctx EAX context structure.
+ * \param st post-AAD captured state.
+ * \param nonce EAX nonce to use.
+ * \param len EAX nonce length (in bytes).
+ */
+void br_eax_reset_post_aad(br_eax_context *ctx, const br_eax_state *st,
+ const void *nonce, size_t len);
+
+/**
+ * \brief Inject additional authenticated data into EAX.
+ *
+ * The provided data is injected into a running EAX computation. Additional
+ * data must be injected _before_ the call to `br_eax_flip()`.
+ * Additional data can be injected in several chunks of arbitrary length;
+ * the total amount of additional authenticated data is unlimited.
+ *
+ * \param ctx EAX context structure.
+ * \param data pointer to additional authenticated data.
+ * \param len length of additional authenticated data (in bytes).
+ */
+void br_eax_aad_inject(br_eax_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Finish injection of additional authenticated data into EAX.
+ *
+ * This function MUST be called before beginning the actual encryption
+ * or decryption (with `br_eax_run()`), even if no additional authenticated
+ * data was injected. No additional authenticated data may be injected
+ * after this function call.
+ *
+ * \param ctx EAX context structure.
+ */
+void br_eax_flip(br_eax_context *ctx);
+
+/**
+ * \brief Obtain a copy of the MAC on additional authenticated data.
+ *
+ * This function may be called only after `br_eax_flip()`; it copies the
+ * AAD-specific MAC value into the provided state. The MAC value depends
+ * on the secret key and the additional data itself, but not on the
+ * nonce. The updated state `st` is meant to be used as parameter for a
+ * further `br_eax_reset_post_aad()` call.
+ *
+ * \param ctx EAX context structure.
+ * \param st captured state to update.
+ */
+static inline void
+br_eax_get_aad_mac(const br_eax_context *ctx, br_eax_state *st)
+{
+ memcpy(st->st[1], ctx->head, sizeof ctx->head);
+}
+
+/**
+ * \brief Encrypt or decrypt some data with EAX.
+ *
+ * Data encryption or decryption can be done after `br_eax_flip()`
+ * has been called on the context. If `encrypt` is non-zero, then the
+ * provided data shall be plaintext, and it is encrypted in place.
+ * Otherwise, the data shall be ciphertext, and it is decrypted in place.
+ *
+ * Data may be provided in several chunks of arbitrary length.
+ *
+ * \param ctx EAX context structure.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+void br_eax_run(br_eax_context *ctx, int encrypt, void *data, size_t len);
+
+/**
+ * \brief Compute EAX authentication tag.
+ *
+ * Compute the EAX authentication tag. The tag is a 16-byte value which
+ * is written in the provided `tag` buffer. This call terminates the
+ * EAX run: no data may be processed with that EAX context afterwards,
+ * until `br_eax_reset()` is called to initiate a new EAX run.
+ *
+ * The tag value must normally be sent along with the encrypted data.
+ * When decrypting, the tag value must be recomputed and compared with
+ * the received tag: if the two tag values differ, then either the tag
+ * or the encrypted data was altered in transit. As an alternative to
+ * this function, the `br_eax_check_tag()` function can be used to
+ * compute and check the tag value.
+ *
+ * \param ctx EAX context structure.
+ * \param tag destination buffer for the tag (16 bytes).
+ */
+void br_eax_get_tag(br_eax_context *ctx, void *tag);
+
+/**
+ * \brief Compute and check EAX authentication tag.
+ *
+ * This function is an alternative to `br_eax_get_tag()`, normally used
+ * on the receiving end (i.e. when decrypting value). The tag value is
+ * recomputed and compared with the provided tag value. If they match, 1
+ * is returned; on mismatch, 0 is returned. A returned value of 0 means
+ * that the data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * \param ctx EAX context structure.
+ * \param tag tag value to compare with (16 bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+uint32_t br_eax_check_tag(br_eax_context *ctx, const void *tag);
+
+/**
+ * \brief Compute EAX authentication tag (with truncation).
+ *
+ * This function is similar to `br_eax_get_tag()`, except that it allows
+ * the tag to be truncated to a smaller length. The intended tag length
+ * is provided as `len` (in bytes); it MUST be no more than 16, but
+ * it may be smaller. Note that decreasing tag length mechanically makes
+ * forgeries easier; NIST SP 800-38D specifies that the tag length shall
+ * lie between 12 and 16 bytes (inclusive), but may be truncated down to
+ * 4 or 8 bytes, for specific applications that can tolerate it. It must
+ * also be noted that successful forgeries leak information on the
+ * authentication key, making subsequent forgeries easier. Therefore,
+ * tag truncation, and in particular truncation to sizes lower than 12
+ * bytes, shall be envisioned only with great care.
+ *
+ * The tag is written in the provided `tag` buffer. This call terminates
+ * the EAX run: no data may be processed with that EAX context
+ * afterwards, until `br_eax_reset()` is called to initiate a new EAX
+ * run.
+ *
+ * The tag value must normally be sent along with the encrypted data.
+ * When decrypting, the tag value must be recomputed and compared with
+ * the received tag: if the two tag values differ, then either the tag
+ * or the encrypted data was altered in transit. As an alternative to
+ * this function, the `br_eax_check_tag_trunc()` function can be used to
+ * compute and check the tag value.
+ *
+ * \param ctx EAX context structure.
+ * \param tag destination buffer for the tag.
+ * \param len tag length (16 bytes or less).
+ */
+void br_eax_get_tag_trunc(br_eax_context *ctx, void *tag, size_t len);
+
+/**
+ * \brief Compute and check EAX authentication tag (with truncation).
+ *
+ * This function is an alternative to `br_eax_get_tag_trunc()`, normally used
+ * on the receiving end (i.e. when decrypting value). The tag value is
+ * recomputed and compared with the provided tag value. If they match, 1
+ * is returned; on mismatch, 0 is returned. A returned value of 0 means
+ * that the data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * Tag length MUST be 16 bytes or less. The normal EAX tag length is 16
+ * bytes. See `br_check_tag_trunc()` for some discussion on the potential
+ * perils of truncating authentication tags.
+ *
+ * \param ctx EAX context structure.
+ * \param tag tag value to compare with.
+ * \param len tag length (in bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+uint32_t br_eax_check_tag_trunc(br_eax_context *ctx,
+ const void *tag, size_t len);
+
+/**
+ * \brief Class instance for EAX.
+ */
+extern const br_aead_class br_eax_vtable;
+
+/**
+ * \brief Context structure for CCM.
+ *
+ * CCM is an AEAD mode that combines a block cipher in CTR mode with
+ * CBC-MAC using the same block cipher and the same key, to provide
+ * authenticated encryption:
+ *
+ * - Any block cipher with 16-byte blocks can be used with CCM
+ * (technically, other block sizes are defined as well, but this
+ * is not implemented by these functions; shorter blocks also
+ * imply numerous security issues).
+ *
+ * - The authentication tag length, and plaintext length, MUST be
+ * known when starting processing data. Plaintext and ciphertext
+ * can still be provided by chunks, but the total size must match
+ * the value provided upon initialisation.
+ *
+ * - The nonce length is constrained between 7 and 13 bytes (inclusive).
+ * Furthermore, the plaintext length, when encoded, must fit over
+ * 15-nonceLen bytes; thus, if the nonce has length 13 bytes, then
+ * the plaintext length cannot exceed 65535 bytes.
+ *
+ * - Additional authenticated data length is practically unlimited
+ * (formal limit is at 2^64 bytes).
+ *
+ * - The authentication tag has length 4 to 16 bytes (even values only).
+ *
+ * The CCM initialisation function receives as parameter an
+ * _initialised_ block cipher implementation context, with the secret
+ * key already set. A pointer to that context will be kept within the
+ * CCM context structure. It is up to the caller to allocate and
+ * initialise that block cipher context.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ const br_block_ctrcbc_class **bctx;
+ unsigned char ctr[16];
+ unsigned char cbcmac[16];
+ unsigned char tagmask[16];
+ unsigned char buf[16];
+ size_t ptr;
+ size_t tag_len;
+#endif
+} br_ccm_context;
+
+/**
+ * \brief Initialize a CCM context.
+ *
+ * A block cipher implementation, with its initialised context
+ * structure, is provided. The block cipher MUST use 16-byte blocks in
+ * CTR + CBC-MAC mode, and its secret key MUST have been already set in
+ * the provided context. The parameters are linked in the CCM context.
+ *
+ * After this function has been called, the `br_ccm_reset()` function must
+ * be called, to provide the nonce for CCM computation.
+ *
+ * \param ctx CCM context structure.
+ * \param bctx block cipher context (already initialised with secret key).
+ */
+void br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx);
+
+/**
+ * \brief Reset a CCM context.
+ *
+ * This function resets an already initialised CCM context for a new
+ * computation run. Implementations and keys are conserved. This function
+ * can be called at any time; it cancels any ongoing CCM computation that
+ * uses the provided context structure.
+ *
+ * The `aad_len` parameter contains the total length, in bytes, of the
+ * additional authenticated data. It may be zero. That length MUST be
+ * exact.
+ *
+ * The `data_len` parameter contains the total length, in bytes, of the
+ * data that will be injected (plaintext or ciphertext). That length MUST
+ * be exact. Moreover, that length MUST be less than 2^(8*(15-nonce_len)).
+ *
+ * The nonce length (`nonce_len`), in bytes, must be in the 7..13 range
+ * (inclusive).
+ *
+ * The tag length (`tag_len`), in bytes, must be in the 4..16 range, and
+ * be an even integer. Short tags mechanically allow for higher forgery
+ * probabilities; hence, tag sizes smaller than 12 bytes shall be used only
+ * with care.
+ *
+ * It is critical to CCM security that nonce values are not repeated for
+ * the same encryption key. Random generation of nonces is not generally
+ * recommended, due to the relatively small maximum nonce value.
+ *
+ * Returned value is 1 on success, 0 on error. An error is reported if
+ * the tag or nonce length is out of range, or if the
+ * plaintext/ciphertext length cannot be encoded with the specified
+ * nonce length.
+ *
+ * \param ctx CCM context structure.
+ * \param nonce CCM nonce to use.
+ * \param nonce_len CCM nonce length (in bytes, 7 to 13).
+ * \param aad_len additional authenticated data length (in bytes).
+ * \param data_len plaintext/ciphertext length (in bytes).
+ * \param tag_len tag length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+int br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,
+ uint64_t aad_len, uint64_t data_len, size_t tag_len);
+
+/**
+ * \brief Inject additional authenticated data into CCM.
+ *
+ * The provided data is injected into a running CCM computation. Additional
+ * data must be injected _before_ the call to `br_ccm_flip()`.
+ * Additional data can be injected in several chunks of arbitrary length,
+ * but the total amount MUST exactly match the value which was provided
+ * to `br_ccm_reset()`.
+ *
+ * \param ctx CCM context structure.
+ * \param data pointer to additional authenticated data.
+ * \param len length of additional authenticated data (in bytes).
+ */
+void br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Finish injection of additional authenticated data into CCM.
+ *
+ * This function MUST be called before beginning the actual encryption
+ * or decryption (with `br_ccm_run()`), even if no additional authenticated
+ * data was injected. No additional authenticated data may be injected
+ * after this function call.
+ *
+ * \param ctx CCM context structure.
+ */
+void br_ccm_flip(br_ccm_context *ctx);
+
+/**
+ * \brief Encrypt or decrypt some data with CCM.
+ *
+ * Data encryption or decryption can be done after `br_ccm_flip()`
+ * has been called on the context. If `encrypt` is non-zero, then the
+ * provided data shall be plaintext, and it is encrypted in place.
+ * Otherwise, the data shall be ciphertext, and it is decrypted in place.
+ *
+ * Data may be provided in several chunks of arbitrary length, provided
+ * that the total length exactly matches the length provided to the
+ * `br_ccm_reset()` call.
+ *
+ * \param ctx CCM context structure.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+void br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len);
+
+/**
+ * \brief Compute CCM authentication tag.
+ *
+ * Compute the CCM authentication tag. This call terminates the CCM
+ * run: all data must have been injected with `br_ccm_run()` (in zero,
+ * one or more successive calls). After this function has been called,
+ * no more data can br processed; a `br_ccm_reset()` call is required
+ * to start a new message.
+ *
+ * The tag length was provided upon context initialisation (last call
+ * to `br_ccm_reset()`); it is returned by this function.
+ *
+ * The tag value must normally be sent along with the encrypted data.
+ * When decrypting, the tag value must be recomputed and compared with
+ * the received tag: if the two tag values differ, then either the tag
+ * or the encrypted data was altered in transit. As an alternative to
+ * this function, the `br_ccm_check_tag()` function can be used to
+ * compute and check the tag value.
+ *
+ * \param ctx CCM context structure.
+ * \param tag destination buffer for the tag (up to 16 bytes).
+ * \return the tag length (in bytes).
+ */
+size_t br_ccm_get_tag(br_ccm_context *ctx, void *tag);
+
+/**
+ * \brief Compute and check CCM authentication tag.
+ *
+ * This function is an alternative to `br_ccm_get_tag()`, normally used
+ * on the receiving end (i.e. when decrypting value). The tag value is
+ * recomputed and compared with the provided tag value. If they match, 1
+ * is returned; on mismatch, 0 is returned. A returned value of 0 means
+ * that the data or the tag was altered in transit, normally leading to
+ * wholesale rejection of the complete message.
+ *
+ * \param ctx CCM context structure.
+ * \param tag tag value to compare with (up to 16 bytes).
+ * \return 1 on success (exact match of tag value), 0 otherwise.
+ */
+uint32_t br_ccm_check_tag(br_ccm_context *ctx, const void *tag);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_block.h b/test/monniaux/BearSSL/inc/bearssl_block.h
new file mode 100644
index 00000000..683a4906
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_block.h
@@ -0,0 +1,2618 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_BLOCK_H__
+#define BR_BEARSSL_BLOCK_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_block.h
+ *
+ * # Block Ciphers and Symmetric Ciphers
+ *
+ * This file documents the API for block ciphers and other symmetric
+ * ciphers.
+ *
+ *
+ * ## Procedural API
+ *
+ * For a block cipher implementation, up to three separate sets of
+ * functions are provided, for CBC encryption, CBC decryption, and CTR
+ * encryption/decryption. Each set has its own context structure,
+ * initialised with the encryption key.
+ *
+ * For CBC encryption and decryption, the data to encrypt or decrypt is
+ * referenced as a sequence of blocks. The implementations assume that
+ * there is no partial block; no padding is applied or removed. The
+ * caller is responsible for handling any kind of padding.
+ *
+ * Function for CTR encryption are defined only for block ciphers with
+ * blocks of 16 bytes or more (i.e. AES, but not DES/3DES).
+ *
+ * Each implemented block cipher is identified by an "internal name"
+ * from which are derived the names of structures and functions that
+ * implement the cipher. For the block cipher of internal name "`xxx`",
+ * the following are defined:
+ *
+ * - `br_xxx_BLOCK_SIZE`
+ *
+ * A macro that evaluates to the block size (in bytes) of the
+ * cipher. For all implemented block ciphers, this value is a
+ * power of two.
+ *
+ * - `br_xxx_cbcenc_keys`
+ *
+ * Context structure that contains the subkeys resulting from the key
+ * expansion. These subkeys are appropriate for CBC encryption. The
+ * structure first field is called `vtable` and points to the
+ * appropriate OOP structure.
+ *
+ * - `br_xxx_cbcenc_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`
+ *
+ * Perform key expansion: subkeys for CBC encryption are computed and
+ * written in the provided context structure. The key length MUST be
+ * adequate for the implemented block cipher. This function also sets
+ * the `vtable` field.
+ *
+ * - `br_xxx_cbcenc_run(const br_xxx_cbcenc_keys *ctx, void *iv, void *data, size_t len)`
+ *
+ * Perform CBC encryption of `len` bytes, in place. The encrypted data
+ * replaces the cleartext. `len` MUST be a multiple of the block length
+ * (if it is not, the function may loop forever or overflow a buffer).
+ * The IV is provided with the `iv` pointer; it is also updated with
+ * a copy of the last encrypted block.
+ *
+ * - `br_xxx_cbcdec_keys`
+ *
+ * Context structure that contains the subkeys resulting from the key
+ * expansion. These subkeys are appropriate for CBC decryption. The
+ * structure first field is called `vtable` and points to the
+ * appropriate OOP structure.
+ *
+ * - `br_xxx_cbcdec_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`
+ *
+ * Perform key expansion: subkeys for CBC decryption are computed and
+ * written in the provided context structure. The key length MUST be
+ * adequate for the implemented block cipher. This function also sets
+ * the `vtable` field.
+ *
+ * - `br_xxx_cbcdec_run(const br_xxx_cbcdec_keys *ctx, void *iv, void *data, size_t num_blocks)`
+ *
+ * Perform CBC decryption of `len` bytes, in place. The decrypted data
+ * replaces the ciphertext. `len` MUST be a multiple of the block length
+ * (if it is not, the function may loop forever or overflow a buffer).
+ * The IV is provided with the `iv` pointer; it is also updated with
+ * a copy of the last _encrypted_ block.
+ *
+ * - `br_xxx_ctr_keys`
+ *
+ * Context structure that contains the subkeys resulting from the key
+ * expansion. These subkeys are appropriate for CTR encryption and
+ * decryption. The structure first field is called `vtable` and
+ * points to the appropriate OOP structure.
+ *
+ * - `br_xxx_ctr_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)`
+ *
+ * Perform key expansion: subkeys for CTR encryption and decryption
+ * are computed and written in the provided context structure. The
+ * key length MUST be adequate for the implemented block cipher. This
+ * function also sets the `vtable` field.
+ *
+ * - `br_xxx_ctr_run(const br_xxx_ctr_keys *ctx, const void *iv, uint32_t cc, void *data, size_t len)` (returns `uint32_t`)
+ *
+ * Perform CTR encryption/decryption of some data. Processing is done
+ * "in place" (the output data replaces the input data). This function
+ * implements the "standard incrementing function" from NIST SP800-38A,
+ * annex B: the IV length shall be 4 bytes less than the block size
+ * (i.e. 12 bytes for AES) and the counter is the 32-bit value starting
+ * with `cc`. The data length (`len`) is not necessarily a multiple of
+ * the block size. The new counter value is returned, which supports
+ * chunked processing, provided that each chunk length (except possibly
+ * the last one) is a multiple of the block size.
+ *
+ * - `br_xxx_ctrcbc_keys`
+ *
+ * Context structure that contains the subkeys resulting from the
+ * key expansion. These subkeys are appropriate for doing combined
+ * CTR encryption/decryption and CBC-MAC, as used in the CCM and EAX
+ * authenticated encryption modes. The structure first field is
+ * called `vtable` and points to the appropriate OOP structure.
+ *
+ * - `br_xxx_ctrcbc_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)`
+ *
+ * Perform key expansion: subkeys for combined CTR
+ * encryption/decryption and CBC-MAC are computed and written in the
+ * provided context structure. The key length MUST be adequate for
+ * the implemented block cipher. This function also sets the
+ * `vtable` field.
+ *
+ * - `br_xxx_ctrcbc_encrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)`
+ *
+ * Perform CTR encryption of some data, and CBC-MAC. Processing is
+ * done "in place" (the output data replaces the input data). This
+ * function applies CTR encryption on the data, using a full
+ * block-size counter (i.e. for 128-bit blocks, the counter is
+ * incremented as a 128-bit value). The 'ctr' array contains the
+ * initial value for the counter (used in the first block) and it is
+ * updated with the new value after data processing. The 'cbcmac'
+ * value shall point to a block-sized value which is used as IV for
+ * CBC-MAC, computed over the encrypted data (output of CTR
+ * encryption); the resulting CBC-MAC is written over 'cbcmac' on
+ * output.
+ *
+ * The data length MUST be a multiple of the block size.
+ *
+ * - `br_xxx_ctrcbc_decrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)`
+ *
+ * Perform CTR decryption of some data, and CBC-MAC. Processing is
+ * done "in place" (the output data replaces the input data). This
+ * function applies CTR decryption on the data, using a full
+ * block-size counter (i.e. for 128-bit blocks, the counter is
+ * incremented as a 128-bit value). The 'ctr' array contains the
+ * initial value for the counter (used in the first block) and it is
+ * updated with the new value after data processing. The 'cbcmac'
+ * value shall point to a block-sized value which is used as IV for
+ * CBC-MAC, computed over the encrypted data (input of CTR
+ * encryption); the resulting CBC-MAC is written over 'cbcmac' on
+ * output.
+ *
+ * The data length MUST be a multiple of the block size.
+ *
+ * - `br_xxx_ctrcbc_ctr(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *data, size_t len)`
+ *
+ * Perform CTR encryption or decryption of the provided data. The
+ * data is processed "in place" (the output data replaces the input
+ * data). A full block-sized counter is applied (i.e. for 128-bit
+ * blocks, the counter is incremented as a 128-bit value). The 'ctr'
+ * array contains the initial value for the counter (used in the
+ * first block), and it is updated with the new value after data
+ * processing.
+ *
+ * The data length MUST be a multiple of the block size.
+ *
+ * - `br_xxx_ctrcbc_mac(const br_xxx_ctrcbc_keys *ctx, void *cbcmac, const void *data, size_t len)`
+ *
+ * Compute CBC-MAC over the provided data. The IV for CBC-MAC is
+ * provided as 'cbcmac'; the output is written over the same array.
+ * The data itself is untouched. The data length MUST be a multiple
+ * of the block size.
+ *
+ *
+ * It shall be noted that the key expansion functions return `void`. If
+ * the provided key length is not allowed, then there will be no error
+ * reporting; implementations need not validate the key length, thus an
+ * invalid key length may result in undefined behaviour (e.g. buffer
+ * overflow).
+ *
+ * Subkey structures contain no interior pointer, and no external
+ * resources are allocated upon key expansion. They can thus be
+ * discarded without any explicit deallocation.
+ *
+ *
+ * ## Object-Oriented API
+ *
+ * Each context structure begins with a field (called `vtable`) that
+ * points to an instance of a structure that references the relevant
+ * functions through pointers. Each such structure contains the
+ * following:
+ *
+ * - `context_size`
+ *
+ * The size (in bytes) of the context structure for subkeys.
+ *
+ * - `block_size`
+ *
+ * The cipher block size (in bytes).
+ *
+ * - `log_block_size`
+ *
+ * The base-2 logarithm of cipher block size (e.g. 4 for blocks
+ * of 16 bytes).
+ *
+ * - `init`
+ *
+ * Pointer to the key expansion function.
+ *
+ * - `run`
+ *
+ * Pointer to the encryption/decryption function.
+ *
+ * For combined CTR/CBC-MAC encryption, the `vtable` has a slightly
+ * different structure:
+ *
+ * - `context_size`
+ *
+ * The size (in bytes) of the context structure for subkeys.
+ *
+ * - `block_size`
+ *
+ * The cipher block size (in bytes).
+ *
+ * - `log_block_size`
+ *
+ * The base-2 logarithm of cipher block size (e.g. 4 for blocks
+ * of 16 bytes).
+ *
+ * - `init`
+ *
+ * Pointer to the key expansion function.
+ *
+ * - `encrypt`
+ *
+ * Pointer to the CTR encryption + CBC-MAC function.
+ *
+ * - `decrypt`
+ *
+ * Pointer to the CTR decryption + CBC-MAC function.
+ *
+ * - `ctr`
+ *
+ * Pointer to the CTR encryption/decryption function.
+ *
+ * - `mac`
+ *
+ * Pointer to the CBC-MAC function.
+ *
+ * For block cipher "`xxx`", static, constant instances of these
+ * structures are defined, under the names:
+ *
+ * - `br_xxx_cbcenc_vtable`
+ * - `br_xxx_cbcdec_vtable`
+ * - `br_xxx_ctr_vtable`
+ * - `br_xxx_ctrcbc_vtable`
+ *
+ *
+ * ## Implemented Block Ciphers
+ *
+ * Provided implementations are:
+ *
+ * | Name | Function | Block Size (bytes) | Key lengths (bytes) |
+ * | :-------- | :------- | :----------------: | :-----------------: |
+ * | aes_big | AES | 16 | 16, 24 and 32 |
+ * | aes_small | AES | 16 | 16, 24 and 32 |
+ * | aes_ct | AES | 16 | 16, 24 and 32 |
+ * | aes_ct64 | AES | 16 | 16, 24 and 32 |
+ * | aes_x86ni | AES | 16 | 16, 24 and 32 |
+ * | aes_pwr8 | AES | 16 | 16, 24 and 32 |
+ * | des_ct | DES/3DES | 8 | 8, 16 and 24 |
+ * | des_tab | DES/3DES | 8 | 8, 16 and 24 |
+ *
+ * **Note:** DES/3DES nominally uses keys of 64, 128 and 192 bits (i.e. 8,
+ * 16 and 24 bytes), but some of the bits are ignored by the algorithm, so
+ * the _effective_ key lengths, from a security point of view, are 56,
+ * 112 and 168 bits, respectively.
+ *
+ * `aes_big` is a "classical" AES implementation, using tables. It
+ * is fast but not constant-time, since it makes data-dependent array
+ * accesses.
+ *
+ * `aes_small` is an AES implementation optimized for code size. It
+ * is substantially slower than `aes_big`; it is not constant-time
+ * either.
+ *
+ * `aes_ct` is a constant-time implementation of AES; its code is about
+ * as big as that of `aes_big`, while its performance is comparable to
+ * that of `aes_small`. However, it is constant-time. This
+ * implementation should thus be considered to be the "default" AES in
+ * BearSSL, to be used unless the operational context guarantees that a
+ * non-constant-time implementation is safe, or an architecture-specific
+ * constant-time implementation can be used (e.g. using dedicated
+ * hardware opcodes).
+ *
+ * `aes_ct64` is another constant-time implementation of AES. It is
+ * similar to `aes_ct` but uses 64-bit values. On 32-bit machines,
+ * `aes_ct64` is not faster than `aes_ct`, often a bit slower, and has
+ * a larger footprint; however, on 64-bit architectures, `aes_ct64`
+ * is typically twice faster than `aes_ct` for modes that allow parallel
+ * operations (i.e. CTR, and CBC decryption, but not CBC encryption).
+ *
+ * `aes_x86ni` exists only on x86 architectures (32-bit and 64-bit). It
+ * uses the AES-NI opcodes when available.
+ *
+ * `aes_pwr8` exists only on PowerPC / POWER architectures (32-bit and
+ * 64-bit, both little-endian and big-endian). It uses the AES opcodes
+ * present in POWER8 and later.
+ *
+ * `des_tab` is a classic, table-based implementation of DES/3DES. It
+ * is not constant-time.
+ *
+ * `des_ct` is an constant-time implementation of DES/3DES. It is
+ * substantially slower than `des_tab`.
+ *
+ * ## ChaCha20 and Poly1305
+ *
+ * ChaCha20 is a stream cipher. Poly1305 is a MAC algorithm. They
+ * are described in [RFC 7539](https://tools.ietf.org/html/rfc7539).
+ *
+ * Two function pointer types are defined:
+ *
+ * - `br_chacha20_run` describes a function that implements ChaCha20
+ * only.
+ *
+ * - `br_poly1305_run` describes an implementation of Poly1305,
+ * in the AEAD combination with ChaCha20 specified in RFC 7539
+ * (the ChaCha20 implementation is provided as a function pointer).
+ *
+ * `chacha20_ct` is a straightforward implementation of ChaCha20 in
+ * plain C; it is constant-time, small, and reasonably fast.
+ *
+ * `chacha20_sse2` leverages SSE2 opcodes (on x86 architectures that
+ * support these opcodes). It is faster than `chacha20_ct`.
+ *
+ * `poly1305_ctmul` is an implementation of the ChaCha20+Poly1305 AEAD
+ * construction, where the Poly1305 part is performed with mixed 32-bit
+ * multiplications (operands are 32-bit, result is 64-bit).
+ *
+ * `poly1305_ctmul32` implements ChaCha20+Poly1305 using pure 32-bit
+ * multiplications (32-bit operands, 32-bit result). It is slower than
+ * `poly1305_ctmul`, except on some specific architectures such as
+ * the ARM Cortex M0+.
+ *
+ * `poly1305_ctmulq` implements ChaCha20+Poly1305 with mixed 64-bit
+ * multiplications (operands are 64-bit, result is 128-bit) on 64-bit
+ * platforms that support such operations.
+ *
+ * `poly1305_i15` implements ChaCha20+Poly1305 with the generic "i15"
+ * big integer implementation. It is meant mostly for testing purposes,
+ * although it can help with saving a few hundred bytes of code footprint
+ * on systems where code size is scarce.
+ */
+
+/**
+ * \brief Class type for CBC encryption implementations.
+ *
+ * A `br_block_cbcenc_class` instance points to the functions implementing
+ * a specific block cipher, when used in CBC mode for encrypting data.
+ */
+typedef struct br_block_cbcenc_class_ br_block_cbcenc_class;
+struct br_block_cbcenc_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate
+ * for containing subkeys.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Size of individual blocks (in bytes).
+ */
+ unsigned block_size;
+
+ /**
+ * \brief Base-2 logarithm of the size of individual blocks,
+ * expressed in bytes.
+ */
+ unsigned log_block_size;
+
+ /**
+ * \brief Initialisation function.
+ *
+ * This function sets the `vtable` field in the context structure.
+ * The key length MUST be one of the key lengths supported by
+ * the implementation.
+ *
+ * \param ctx context structure to initialise.
+ * \param key secret key.
+ * \param key_len key length (in bytes).
+ */
+ void (*init)(const br_block_cbcenc_class **ctx,
+ const void *key, size_t key_len);
+
+ /**
+ * \brief Run the CBC encryption.
+ *
+ * The `iv` parameter points to the IV for this run; it is
+ * updated with a copy of the last encrypted block. The data
+ * is encrypted "in place"; its length (`len`) MUST be a
+ * multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param iv IV for CBC encryption (updated).
+ * \param data data to encrypt.
+ * \param len data length (in bytes, multiple of block size).
+ */
+ void (*run)(const br_block_cbcenc_class *const *ctx,
+ void *iv, void *data, size_t len);
+};
+
+/**
+ * \brief Class type for CBC decryption implementations.
+ *
+ * A `br_block_cbcdec_class` instance points to the functions implementing
+ * a specific block cipher, when used in CBC mode for decrypting data.
+ */
+typedef struct br_block_cbcdec_class_ br_block_cbcdec_class;
+struct br_block_cbcdec_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate
+ * for containing subkeys.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Size of individual blocks (in bytes).
+ */
+ unsigned block_size;
+
+ /**
+ * \brief Base-2 logarithm of the size of individual blocks,
+ * expressed in bytes.
+ */
+ unsigned log_block_size;
+
+ /**
+ * \brief Initialisation function.
+ *
+ * This function sets the `vtable` field in the context structure.
+ * The key length MUST be one of the key lengths supported by
+ * the implementation.
+ *
+ * \param ctx context structure to initialise.
+ * \param key secret key.
+ * \param key_len key length (in bytes).
+ */
+ void (*init)(const br_block_cbcdec_class **ctx,
+ const void *key, size_t key_len);
+
+ /**
+ * \brief Run the CBC decryption.
+ *
+ * The `iv` parameter points to the IV for this run; it is
+ * updated with a copy of the last encrypted block. The data
+ * is decrypted "in place"; its length (`len`) MUST be a
+ * multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param iv IV for CBC decryption (updated).
+ * \param data data to decrypt.
+ * \param len data length (in bytes, multiple of block size).
+ */
+ void (*run)(const br_block_cbcdec_class *const *ctx,
+ void *iv, void *data, size_t len);
+};
+
+/**
+ * \brief Class type for CTR encryption/decryption implementations.
+ *
+ * A `br_block_ctr_class` instance points to the functions implementing
+ * a specific block cipher, when used in CTR mode for encrypting or
+ * decrypting data.
+ */
+typedef struct br_block_ctr_class_ br_block_ctr_class;
+struct br_block_ctr_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate
+ * for containing subkeys.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Size of individual blocks (in bytes).
+ */
+ unsigned block_size;
+
+ /**
+ * \brief Base-2 logarithm of the size of individual blocks,
+ * expressed in bytes.
+ */
+ unsigned log_block_size;
+
+ /**
+ * \brief Initialisation function.
+ *
+ * This function sets the `vtable` field in the context structure.
+ * The key length MUST be one of the key lengths supported by
+ * the implementation.
+ *
+ * \param ctx context structure to initialise.
+ * \param key secret key.
+ * \param key_len key length (in bytes).
+ */
+ void (*init)(const br_block_ctr_class **ctx,
+ const void *key, size_t key_len);
+
+ /**
+ * \brief Run the CTR encryption or decryption.
+ *
+ * The `iv` parameter points to the IV for this run; its
+ * length is exactly 4 bytes less than the block size (e.g.
+ * 12 bytes for AES/CTR). The IV is combined with a 32-bit
+ * block counter to produce the block value which is processed
+ * with the block cipher.
+ *
+ * The data to encrypt or decrypt is updated "in place". Its
+ * length (`len` bytes) is not required to be a multiple of
+ * the block size; if the final block is partial, then the
+ * corresponding key stream bits are dropped.
+ *
+ * The resulting counter value is returned.
+ *
+ * \param ctx context structure (already initialised).
+ * \param iv IV for CTR encryption/decryption.
+ * \param cc initial value for the block counter.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \return the new block counter value.
+ */
+ uint32_t (*run)(const br_block_ctr_class *const *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+};
+
+/**
+ * \brief Class type for combined CTR and CBC-MAC implementations.
+ *
+ * A `br_block_ctrcbc_class` instance points to the functions implementing
+ * a specific block cipher, when used in CTR mode for encrypting or
+ * decrypting data, along with CBC-MAC.
+ */
+typedef struct br_block_ctrcbc_class_ br_block_ctrcbc_class;
+struct br_block_ctrcbc_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate
+ * for containing subkeys.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Size of individual blocks (in bytes).
+ */
+ unsigned block_size;
+
+ /**
+ * \brief Base-2 logarithm of the size of individual blocks,
+ * expressed in bytes.
+ */
+ unsigned log_block_size;
+
+ /**
+ * \brief Initialisation function.
+ *
+ * This function sets the `vtable` field in the context structure.
+ * The key length MUST be one of the key lengths supported by
+ * the implementation.
+ *
+ * \param ctx context structure to initialise.
+ * \param key secret key.
+ * \param key_len key length (in bytes).
+ */
+ void (*init)(const br_block_ctrcbc_class **ctx,
+ const void *key, size_t key_len);
+
+ /**
+ * \brief Run the CTR encryption + CBC-MAC.
+ *
+ * The `ctr` parameter points to the counter; its length shall
+ * be equal to the block size. It is updated by this function
+ * as encryption proceeds.
+ *
+ * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC
+ * is computed over the encrypted data (output of CTR
+ * encryption). Its length shall be equal to the block size. The
+ * computed CBC-MAC value is written over the `cbcmac` array.
+ *
+ * The data to encrypt is updated "in place". Its length (`len`
+ * bytes) MUST be a multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param ctr counter for CTR encryption (initial and final).
+ * \param cbcmac IV and output buffer for CBC-MAC.
+ * \param data data to encrypt.
+ * \param len data length (in bytes).
+ */
+ void (*encrypt)(const br_block_ctrcbc_class *const *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+ /**
+ * \brief Run the CTR decryption + CBC-MAC.
+ *
+ * The `ctr` parameter points to the counter; its length shall
+ * be equal to the block size. It is updated by this function
+ * as decryption proceeds.
+ *
+ * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC
+ * is computed over the encrypted data (i.e. before CTR
+ * decryption). Its length shall be equal to the block size. The
+ * computed CBC-MAC value is written over the `cbcmac` array.
+ *
+ * The data to decrypt is updated "in place". Its length (`len`
+ * bytes) MUST be a multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param ctr counter for CTR encryption (initial and final).
+ * \param cbcmac IV and output buffer for CBC-MAC.
+ * \param data data to decrypt.
+ * \param len data length (in bytes).
+ */
+ void (*decrypt)(const br_block_ctrcbc_class *const *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+ /**
+ * \brief Run the CTR encryption/decryption only.
+ *
+ * The `ctr` parameter points to the counter; its length shall
+ * be equal to the block size. It is updated by this function
+ * as decryption proceeds.
+ *
+ * The data to decrypt is updated "in place". Its length (`len`
+ * bytes) MUST be a multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param ctr counter for CTR encryption (initial and final).
+ * \param data data to decrypt.
+ * \param len data length (in bytes).
+ */
+ void (*ctr)(const br_block_ctrcbc_class *const *ctx,
+ void *ctr, void *data, size_t len);
+
+ /**
+ * \brief Run the CBC-MAC only.
+ *
+ * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC
+ * is computed over the encrypted data (i.e. before CTR
+ * decryption). Its length shall be equal to the block size. The
+ * computed CBC-MAC value is written over the `cbcmac` array.
+ *
+ * The data is unmodified. Its length (`len` bytes) MUST be a
+ * multiple of the block size.
+ *
+ * \param ctx context structure (already initialised).
+ * \param cbcmac IV and output buffer for CBC-MAC.
+ * \param data data to decrypt.
+ * \param len data length (in bytes).
+ */
+ void (*mac)(const br_block_ctrcbc_class *const *ctx,
+ void *cbcmac, const void *data, size_t len);
+};
+
+/*
+ * Traditional, table-based AES implementation. It is fast, but uses
+ * internal tables (in particular a 1 kB table for encryption, another
+ * 1 kB table for decryption, and a 256-byte table for key schedule),
+ * and it is not constant-time. In contexts where cache-timing attacks
+ * apply, this implementation may leak the secret key.
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_big_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_big` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_big_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_big` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_big_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_big` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_big_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_big` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_big_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_big` implementation).
+ */
+extern const br_block_cbcenc_class br_aes_big_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_big` implementation).
+ */
+extern const br_block_cbcdec_class br_aes_big_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_big` implementation).
+ */
+extern const br_block_ctr_class br_aes_big_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_big` implementation).
+ */
+extern const br_block_ctrcbc_class br_aes_big_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_big` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_big` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_big` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_big` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_big_ctrcbc_init(br_aes_big_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to encrypt or decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_big_ctrcbc_encrypt(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_big_ctrcbc_decrypt(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_big_ctrcbc_ctr(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_big` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_big_ctrcbc_mac(const br_aes_big_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/*
+ * AES implementation optimized for size. It is slower than the
+ * traditional table-based AES implementation, but requires much less
+ * code. It still uses data-dependent table accesses (albeit within a
+ * much smaller 256-byte table), which makes it conceptually vulnerable
+ * to cache-timing attacks.
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_small_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_small` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_small_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_small` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_small_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_small` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_small_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_small` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_small_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_small` implementation).
+ */
+extern const br_block_cbcenc_class br_aes_small_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_small` implementation).
+ */
+extern const br_block_cbcdec_class br_aes_small_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_small` implementation).
+ */
+extern const br_block_ctr_class br_aes_small_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_small` implementation).
+ */
+extern const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_small` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_small` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_small` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_small` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_small` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/*
+ * Constant-time AES implementation. Its size is similar to that of
+ * 'aes_big', and its performance is similar to that of 'aes_small' (faster
+ * decryption, slower encryption). However, it is constant-time, i.e.
+ * immune to cache-timing and similar attacks.
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_ct_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_ct` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_ct_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_ct_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_ct_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[60];
+ unsigned num_rounds;
+#endif
+} br_aes_ct_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_ct` implementation).
+ */
+extern const br_block_cbcenc_class br_aes_ct_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_ct` implementation).
+ */
+extern const br_block_cbcdec_class br_aes_ct_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_ct` implementation).
+ */
+extern const br_block_ctr_class br_aes_ct_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_ct` implementation).
+ */
+extern const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/*
+ * 64-bit constant-time AES implementation. It is similar to 'aes_ct'
+ * but uses 64-bit registers, making it about twice faster than 'aes_ct'
+ * on 64-bit platforms, while remaining constant-time and with a similar
+ * code size. (The doubling in performance is only for CBC decryption
+ * and CTR mode; CBC encryption is non-parallel and cannot benefit from
+ * the larger registers.)
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_ct64_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_ct64` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t skey[30];
+ unsigned num_rounds;
+#endif
+} br_aes_ct64_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct64` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t skey[30];
+ unsigned num_rounds;
+#endif
+} br_aes_ct64_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t skey[30];
+ unsigned num_rounds;
+#endif
+} br_aes_ct64_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t skey[30];
+ unsigned num_rounds;
+#endif
+} br_aes_ct64_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_ct64` implementation).
+ */
+extern const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_ct64` implementation).
+ */
+extern const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_ct64` implementation).
+ */
+extern const br_block_ctr_class br_aes_ct64_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_ct64` implementation).
+ */
+extern const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_ct64` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_ct64` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_ct64` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_ct64` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_ct64` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/*
+ * AES implementation using AES-NI opcodes (x86 platform).
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_x86ni_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_x86ni` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_x86ni_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_x86ni` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_x86ni_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_x86ni_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_x86ni_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_x86ni` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_x86ni_cbcenc_get_vtable()`.
+ */
+extern const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_x86ni` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_x86ni_cbcdec_get_vtable()`.
+ */
+extern const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_x86ni` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_x86ni_ctr_get_vtable()`.
+ */
+extern const br_block_ctr_class br_aes_x86ni_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_x86ni` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_x86ni_ctrcbc_get_vtable()`.
+ */
+extern const br_block_ctrcbc_class br_aes_x86ni_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_x86ni` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_x86ni` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_x86ni_cbcdec_init(br_aes_x86ni_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_x86ni` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_x86ni` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_x86ni_ctrcbc_init(br_aes_x86ni_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_x86ni_ctrcbc_encrypt(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_x86ni_ctrcbc_decrypt(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_x86ni_ctrcbc_ctr(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_x86ni` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_x86ni_ctrcbc_mac(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/**
+ * \brief Obtain the `aes_x86ni` AES-CBC (encryption) implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_x86ni_cbcenc_vtable`, if
+ * that implementation was compiled in the library _and_ the x86 AES
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_x86ni` AES-CBC (encryption) implementation, or `NULL`.
+ */
+const br_block_cbcenc_class *br_aes_x86ni_cbcenc_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_x86ni` AES-CBC (decryption) implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_x86ni_cbcdec_vtable`, if
+ * that implementation was compiled in the library _and_ the x86 AES
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_x86ni` AES-CBC (decryption) implementation, or `NULL`.
+ */
+const br_block_cbcdec_class *br_aes_x86ni_cbcdec_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_x86ni` AES-CTR implementation, if available.
+ *
+ * This function returns a pointer to `br_aes_x86ni_ctr_vtable`, if
+ * that implementation was compiled in the library _and_ the x86 AES
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_x86ni` AES-CTR implementation, or `NULL`.
+ */
+const br_block_ctr_class *br_aes_x86ni_ctr_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_x86ni` AES-CTR + CBC-MAC implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_x86ni_ctrcbc_vtable`, if
+ * that implementation was compiled in the library _and_ the x86 AES
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_x86ni` AES-CTR implementation, or `NULL`.
+ */
+const br_block_ctrcbc_class *br_aes_x86ni_ctrcbc_get_vtable(void);
+
+/*
+ * AES implementation using POWER8 opcodes.
+ */
+
+/** \brief AES block size (16 bytes). */
+#define br_aes_pwr8_BLOCK_SIZE 16
+
+/**
+ * \brief Context for AES subkeys (`aes_pwr8` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_pwr8_cbcenc_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_pwr8` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_pwr8_cbcdec_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption
+ * and decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctr_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_pwr8_ctr_keys;
+
+/**
+ * \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption
+ * and decryption + CBC-MAC).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_ctrcbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ unsigned char skni[16 * 15];
+ } skey;
+ unsigned num_rounds;
+#endif
+} br_aes_pwr8_ctrcbc_keys;
+
+/**
+ * \brief Class instance for AES CBC encryption (`aes_pwr8` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_pwr8_cbcenc_get_vtable()`.
+ */
+extern const br_block_cbcenc_class br_aes_pwr8_cbcenc_vtable;
+
+/**
+ * \brief Class instance for AES CBC decryption (`aes_pwr8` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_pwr8_cbcdec_get_vtable()`.
+ */
+extern const br_block_cbcdec_class br_aes_pwr8_cbcdec_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption and decryption
+ * (`aes_pwr8` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_pwr8_ctr_get_vtable()`.
+ */
+extern const br_block_ctr_class br_aes_pwr8_ctr_vtable;
+
+/**
+ * \brief Class instance for AES CTR encryption/decryption + CBC-MAC
+ * (`aes_pwr8` implementation).
+ *
+ * Since this implementation might be omitted from the library, or the
+ * AES opcode unavailable on the current CPU, a pointer to this class
+ * instance should be obtained through `br_aes_pwr8_ctrcbc_get_vtable()`.
+ */
+extern const br_block_ctrcbc_class br_aes_pwr8_ctrcbc_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC encryption
+ * (`aes_pwr8` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_pwr8_cbcenc_init(br_aes_pwr8_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CBC decryption
+ * (`aes_pwr8` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_pwr8_cbcdec_init(br_aes_pwr8_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR encryption
+ * and decryption (`aes_pwr8` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_pwr8_ctr_init(br_aes_pwr8_ctr_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
+ * (`aes_pwr8` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_aes_pwr8_ctrcbc_init(br_aes_pwr8_ctrcbc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_pwr8_cbcenc_run(const br_aes_pwr8_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 16).
+ */
+void br_aes_pwr8_cbcdec_run(const br_aes_pwr8_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CTR encryption and decryption with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (constant, 12 bytes).
+ * \param cc initial block counter value.
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes).
+ * \return new block counter value.
+ */
+uint32_t br_aes_pwr8_ctr_run(const br_aes_pwr8_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief CTR encryption + CBC-MAC with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_pwr8_ctrcbc_encrypt(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR decryption + CBC-MAC with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_pwr8_ctrcbc_decrypt(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len);
+
+/**
+ * \brief CTR encryption/decryption with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param ctr counter for CTR (16 bytes, updated).
+ * \param data data to MAC (updated).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_pwr8_ctrcbc_ctr(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len);
+
+/**
+ * \brief CBC-MAC with AES (`aes_pwr8` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param cbcmac IV for CBC-MAC (updated).
+ * \param data data to MAC (unmodified).
+ * \param len data length (in bytes, MUST be a multiple of 16).
+ */
+void br_aes_pwr8_ctrcbc_mac(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len);
+
+/**
+ * \brief Obtain the `aes_pwr8` AES-CBC (encryption) implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_pwr8_cbcenc_vtable`, if
+ * that implementation was compiled in the library _and_ the POWER8
+ * crypto opcodes are available on the currently running CPU. If either
+ * of these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_pwr8` AES-CBC (encryption) implementation, or `NULL`.
+ */
+const br_block_cbcenc_class *br_aes_pwr8_cbcenc_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_pwr8` AES-CBC (decryption) implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_pwr8_cbcdec_vtable`, if
+ * that implementation was compiled in the library _and_ the POWER8
+ * crypto opcodes are available on the currently running CPU. If either
+ * of these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_pwr8` AES-CBC (decryption) implementation, or `NULL`.
+ */
+const br_block_cbcdec_class *br_aes_pwr8_cbcdec_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_pwr8` AES-CTR implementation, if available.
+ *
+ * This function returns a pointer to `br_aes_pwr8_ctr_vtable`, if that
+ * implementation was compiled in the library _and_ the POWER8 crypto
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_pwr8` AES-CTR implementation, or `NULL`.
+ */
+const br_block_ctr_class *br_aes_pwr8_ctr_get_vtable(void);
+
+/**
+ * \brief Obtain the `aes_pwr8` AES-CTR + CBC-MAC implementation, if
+ * available.
+ *
+ * This function returns a pointer to `br_aes_pwr8_ctrcbc_vtable`, if
+ * that implementation was compiled in the library _and_ the POWER8 AES
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `NULL`.
+ *
+ * \return the `aes_pwr8` AES-CTR implementation, or `NULL`.
+ */
+const br_block_ctrcbc_class *br_aes_pwr8_ctrcbc_get_vtable(void);
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CBC encryption) for all AES implementations.
+ */
+typedef union {
+ const br_block_cbcenc_class *vtable;
+ br_aes_big_cbcenc_keys c_big;
+ br_aes_small_cbcenc_keys c_small;
+ br_aes_ct_cbcenc_keys c_ct;
+ br_aes_ct64_cbcenc_keys c_ct64;
+ br_aes_x86ni_cbcenc_keys c_x86ni;
+ br_aes_pwr8_cbcenc_keys c_pwr8;
+} br_aes_gen_cbcenc_keys;
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CBC decryption) for all AES implementations.
+ */
+typedef union {
+ const br_block_cbcdec_class *vtable;
+ br_aes_big_cbcdec_keys c_big;
+ br_aes_small_cbcdec_keys c_small;
+ br_aes_ct_cbcdec_keys c_ct;
+ br_aes_ct64_cbcdec_keys c_ct64;
+ br_aes_x86ni_cbcdec_keys c_x86ni;
+ br_aes_pwr8_cbcdec_keys c_pwr8;
+} br_aes_gen_cbcdec_keys;
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CTR encryption and decryption) for all AES implementations.
+ */
+typedef union {
+ const br_block_ctr_class *vtable;
+ br_aes_big_ctr_keys c_big;
+ br_aes_small_ctr_keys c_small;
+ br_aes_ct_ctr_keys c_ct;
+ br_aes_ct64_ctr_keys c_ct64;
+ br_aes_x86ni_ctr_keys c_x86ni;
+ br_aes_pwr8_ctr_keys c_pwr8;
+} br_aes_gen_ctr_keys;
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CTR encryption/decryption + CBC-MAC) for all AES implementations.
+ */
+typedef union {
+ const br_block_ctrcbc_class *vtable;
+ br_aes_big_ctrcbc_keys c_big;
+ br_aes_small_ctrcbc_keys c_small;
+ br_aes_ct_ctrcbc_keys c_ct;
+ br_aes_ct64_ctrcbc_keys c_ct64;
+ br_aes_x86ni_ctrcbc_keys c_x86ni;
+ br_aes_pwr8_ctrcbc_keys c_pwr8;
+} br_aes_gen_ctrcbc_keys;
+
+/*
+ * Traditional, table-based implementation for DES/3DES. Since tables are
+ * used, cache-timing attacks are conceptually possible.
+ */
+
+/** \brief DES/3DES block size (8 bytes). */
+#define br_des_tab_BLOCK_SIZE 8
+
+/**
+ * \brief Context for DES subkeys (`des_tab` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[96];
+ unsigned num_rounds;
+#endif
+} br_des_tab_cbcenc_keys;
+
+/**
+ * \brief Context for DES subkeys (`des_tab` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[96];
+ unsigned num_rounds;
+#endif
+} br_des_tab_cbcdec_keys;
+
+/**
+ * \brief Class instance for DES CBC encryption (`des_tab` implementation).
+ */
+extern const br_block_cbcenc_class br_des_tab_cbcenc_vtable;
+
+/**
+ * \brief Class instance for DES CBC decryption (`des_tab` implementation).
+ */
+extern const br_block_cbcdec_class br_des_tab_cbcdec_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for DES CBC encryption
+ * (`des_tab` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for DES CBC decryption
+ * (`des_tab` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with DES (`des_tab` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 8).
+ */
+void br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with DES (`des_tab` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 8).
+ */
+void br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/*
+ * Constant-time implementation for DES/3DES. It is substantially slower
+ * (by a factor of about 4x), but also immune to cache-timing attacks.
+ */
+
+/** \brief DES/3DES block size (8 bytes). */
+#define br_des_ct_BLOCK_SIZE 8
+
+/**
+ * \brief Context for DES subkeys (`des_ct` implementation, CBC encryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcenc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[96];
+ unsigned num_rounds;
+#endif
+} br_des_ct_cbcenc_keys;
+
+/**
+ * \brief Context for DES subkeys (`des_ct` implementation, CBC decryption).
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /** \brief Pointer to vtable for this context. */
+ const br_block_cbcdec_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint32_t skey[96];
+ unsigned num_rounds;
+#endif
+} br_des_ct_cbcdec_keys;
+
+/**
+ * \brief Class instance for DES CBC encryption (`des_ct` implementation).
+ */
+extern const br_block_cbcenc_class br_des_ct_cbcenc_vtable;
+
+/**
+ * \brief Class instance for DES CBC decryption (`des_ct` implementation).
+ */
+extern const br_block_cbcdec_class br_des_ct_cbcdec_vtable;
+
+/**
+ * \brief Context initialisation (key schedule) for DES CBC encryption
+ * (`des_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief Context initialisation (key schedule) for DES CBC decryption
+ * (`des_ct` implementation).
+ *
+ * \param ctx context to initialise.
+ * \param key secret key.
+ * \param len secret key length (in bytes).
+ */
+void br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx,
+ const void *key, size_t len);
+
+/**
+ * \brief CBC encryption with DES (`des_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to encrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 8).
+ */
+void br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/**
+ * \brief CBC decryption with DES (`des_ct` implementation).
+ *
+ * \param ctx context (already initialised).
+ * \param iv IV (updated).
+ * \param data data to decrypt (updated).
+ * \param len data length (in bytes, MUST be multiple of 8).
+ */
+void br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx, void *iv,
+ void *data, size_t len);
+
+/*
+ * These structures are large enough to accommodate subkeys for all
+ * DES/3DES implementations.
+ */
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CBC encryption) for all DES implementations.
+ */
+typedef union {
+ const br_block_cbcenc_class *vtable;
+ br_des_tab_cbcenc_keys tab;
+ br_des_ct_cbcenc_keys ct;
+} br_des_gen_cbcenc_keys;
+
+/**
+ * \brief Aggregate structure large enough to be used as context for
+ * subkeys (CBC decryption) for all DES implementations.
+ */
+typedef union {
+ const br_block_cbcdec_class *vtable;
+ br_des_tab_cbcdec_keys c_tab;
+ br_des_ct_cbcdec_keys c_ct;
+} br_des_gen_cbcdec_keys;
+
+/**
+ * \brief Type for a ChaCha20 implementation.
+ *
+ * An implementation follows the description in RFC 7539:
+ *
+ * - Key is 256 bits (`key` points to exactly 32 bytes).
+ *
+ * - IV is 96 bits (`iv` points to exactly 12 bytes).
+ *
+ * - Block counter is over 32 bits and starts at value `cc`; the
+ * resulting value is returned.
+ *
+ * Data (pointed to by `data`, of length `len`) is encrypted/decrypted
+ * in place. If `len` is not a multiple of 64, then the excess bytes from
+ * the last block processing are dropped (therefore, "chunked" processing
+ * works only as long as each non-final chunk has a length multiple of 64).
+ *
+ * \param key secret key (32 bytes).
+ * \param iv IV (12 bytes).
+ * \param cc initial counter value.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+typedef uint32_t (*br_chacha20_run)(const void *key,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief ChaCha20 implementation (straightforward C code, constant-time).
+ *
+ * \see br_chacha20_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv IV (12 bytes).
+ * \param cc initial counter value.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+uint32_t br_chacha20_ct_run(const void *key,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief ChaCha20 implementation (SSE2 code, constant-time).
+ *
+ * This implementation is available only on x86 platforms, depending on
+ * compiler support. Moreover, in 32-bit mode, it might not actually run,
+ * if the underlying hardware does not implement the SSE2 opcode (in
+ * 64-bit mode, SSE2 is part of the ABI, so if the code could be compiled
+ * at all, then it can run). Use `br_chacha20_sse2_get()` to safely obtain
+ * a pointer to that function.
+ *
+ * \see br_chacha20_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv IV (12 bytes).
+ * \param cc initial counter value.
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ */
+uint32_t br_chacha20_sse2_run(const void *key,
+ const void *iv, uint32_t cc, void *data, size_t len);
+
+/**
+ * \brief Obtain the `sse2` ChaCha20 implementation, if available.
+ *
+ * This function returns a pointer to `br_chacha20_sse2_run`, if
+ * that implementation was compiled in the library _and_ the SSE2
+ * opcodes are available on the currently running CPU. If either of
+ * these conditions is not met, then this function returns `0`.
+ *
+ * \return the `sse2` ChaCha20 implementation, or `0`.
+ */
+br_chacha20_run br_chacha20_sse2_get(void);
+
+/**
+ * \brief Type for a ChaCha20+Poly1305 AEAD implementation.
+ *
+ * The provided data is encrypted or decrypted with ChaCha20. The
+ * authentication tag is computed on the concatenation of the
+ * additional data and the ciphertext, with the padding and lengths
+ * as described in RFC 7539 (section 2.8).
+ *
+ * After decryption, the caller is responsible for checking that the
+ * computed tag matches the expected value.
+ *
+ * \param key secret key (32 bytes).
+ * \param iv nonce (12 bytes).
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \param aad additional authenticated data.
+ * \param aad_len length of additional authenticated data (in bytes).
+ * \param tag output buffer for the authentication tag.
+ * \param ichacha implementation of ChaCha20.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ */
+typedef void (*br_poly1305_run)(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt);
+
+/**
+ * \brief ChaCha20+Poly1305 AEAD implementation (mixed 32-bit multiplications).
+ *
+ * \see br_poly1305_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv nonce (12 bytes).
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \param aad additional authenticated data.
+ * \param aad_len length of additional authenticated data (in bytes).
+ * \param tag output buffer for the authentication tag.
+ * \param ichacha implementation of ChaCha20.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ */
+void br_poly1305_ctmul_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt);
+
+/**
+ * \brief ChaCha20+Poly1305 AEAD implementation (pure 32-bit multiplications).
+ *
+ * \see br_poly1305_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv nonce (12 bytes).
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \param aad additional authenticated data.
+ * \param aad_len length of additional authenticated data (in bytes).
+ * \param tag output buffer for the authentication tag.
+ * \param ichacha implementation of ChaCha20.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ */
+void br_poly1305_ctmul32_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt);
+
+/**
+ * \brief ChaCha20+Poly1305 AEAD implementation (i15).
+ *
+ * This implementation relies on the generic big integer code "i15"
+ * (which uses pure 32-bit multiplications). As such, it may save a
+ * little code footprint in a context where "i15" is already included
+ * (e.g. for elliptic curves or for RSA); however, it is also
+ * substantially slower than the ctmul and ctmul32 implementations.
+ *
+ * \see br_poly1305_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv nonce (12 bytes).
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \param aad additional authenticated data.
+ * \param aad_len length of additional authenticated data (in bytes).
+ * \param tag output buffer for the authentication tag.
+ * \param ichacha implementation of ChaCha20.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ */
+void br_poly1305_i15_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt);
+
+/**
+ * \brief ChaCha20+Poly1305 AEAD implementation (ctmulq).
+ *
+ * This implementation uses 64-bit multiplications (result over 128 bits).
+ * It is available only on platforms that offer such a primitive (in
+ * practice, 64-bit architectures). Use `br_poly1305_ctmulq_get()` to
+ * dynamically obtain a pointer to that function, or 0 if not supported.
+ *
+ * \see br_poly1305_run
+ *
+ * \param key secret key (32 bytes).
+ * \param iv nonce (12 bytes).
+ * \param data data to encrypt or decrypt.
+ * \param len data length (in bytes).
+ * \param aad additional authenticated data.
+ * \param aad_len length of additional authenticated data (in bytes).
+ * \param tag output buffer for the authentication tag.
+ * \param ichacha implementation of ChaCha20.
+ * \param encrypt non-zero for encryption, zero for decryption.
+ */
+void br_poly1305_ctmulq_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt);
+
+/**
+ * \brief Get the ChaCha20+Poly1305 "ctmulq" implementation, if available.
+ *
+ * This function returns a pointer to the `br_poly1305_ctmulq_run()`
+ * function if supported on the current platform; otherwise, it returns 0.
+ *
+ * \return the ctmulq ChaCha20+Poly1305 implementation, or 0.
+ */
+br_poly1305_run br_poly1305_ctmulq_get(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_ec.h b/test/monniaux/BearSSL/inc/bearssl_ec.h
new file mode 100644
index 00000000..f954309e
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_ec.h
@@ -0,0 +1,967 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_EC_H__
+#define BR_BEARSSL_EC_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_rand.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_ec.h
+ *
+ * # Elliptic Curves
+ *
+ * This file documents the EC implementations provided with BearSSL, and
+ * ECDSA.
+ *
+ * ## Elliptic Curve API
+ *
+ * Only "named curves" are supported. Each EC implementation supports
+ * one or several named curves, identified by symbolic identifiers.
+ * These identifiers are small integers, that correspond to the values
+ * registered by the
+ * [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
+ *
+ * Since all currently defined elliptic curve identifiers are in the 0..31
+ * range, it is convenient to encode support of some curves in a 32-bit
+ * word, such that bit x corresponds to curve of identifier x.
+ *
+ * An EC implementation is incarnated by a `br_ec_impl` instance, that
+ * offers the following fields:
+ *
+ * - `supported_curves`
+ *
+ * A 32-bit word that documents the identifiers of the curves supported
+ * by this implementation.
+ *
+ * - `generator()`
+ *
+ * Callback method that returns a pointer to the conventional generator
+ * point for that curve.
+ *
+ * - `order()`
+ *
+ * Callback method that returns a pointer to the subgroup order for
+ * that curve. That value uses unsigned big-endian encoding.
+ *
+ * - `xoff()`
+ *
+ * Callback method that returns the offset and length of the X
+ * coordinate in an encoded point.
+ *
+ * - `mul()`
+ *
+ * Multiply a curve point with an integer.
+ *
+ * - `mulgen()`
+ *
+ * Multiply the curve generator with an integer. This may be faster
+ * than the generic `mul()`.
+ *
+ * - `muladd()`
+ *
+ * Multiply two curve points by two integers, and return the sum of
+ * the two products.
+ *
+ * All curve points are represented in uncompressed format. The `mul()`
+ * and `muladd()` methods take care to validate that the provided points
+ * are really part of the relevant curve subgroup.
+ *
+ * For all point multiplication functions, the following holds:
+ *
+ * - Functions validate that the provided points are valid members
+ * of the relevant curve subgroup. An error is reported if that is
+ * not the case.
+ *
+ * - Processing is constant-time, even if the point operands are not
+ * valid. This holds for both the source and resulting points, and
+ * the multipliers (integers). Only the byte length of the provided
+ * multiplier arrays (not their actual value length in bits) may
+ * leak through timing-based side channels.
+ *
+ * - The multipliers (integers) MUST be lower than the subgroup order.
+ * If this property is not met, then the result is indeterminate,
+ * but an error value is not ncessearily returned.
+ *
+ *
+ * ## ECDSA
+ *
+ * ECDSA signatures have two standard formats, called "raw" and "asn1".
+ * Internally, such a signature is a pair of modular integers `(r,s)`.
+ * The "raw" format is the concatenation of the unsigned big-endian
+ * encodings of these two integers, possibly left-padded with zeros so
+ * that they have the same encoded length. The "asn1" format is the
+ * DER encoding of an ASN.1 structure that contains the two integer
+ * values:
+ *
+ * ECDSASignature ::= SEQUENCE {
+ * r INTEGER,
+ * s INTEGER
+ * }
+ *
+ * In general, in all of X.509 and SSL/TLS, the "asn1" format is used.
+ * BearSSL offers ECDSA implementations for both formats; conversion
+ * functions between the two formats are also provided. Conversion of a
+ * "raw" format signature into "asn1" may enlarge a signature by no more
+ * than 9 bytes for all supported curves; conversely, conversion of an
+ * "asn1" signature to "raw" may expand the signature but the "raw"
+ * length will never be more than twice the length of the "asn1" length
+ * (and usually it will be shorter).
+ *
+ * Note that for a given signature, the "raw" format is not fully
+ * deterministic, in that it does not enforce a minimal common length.
+ */
+
+/*
+ * Standard curve ID. These ID are equal to the assigned numerical
+ * identifiers assigned to these curves for TLS:
+ * http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
+ */
+
+/** \brief Identifier for named curve sect163k1. */
+#define BR_EC_sect163k1 1
+
+/** \brief Identifier for named curve sect163r1. */
+#define BR_EC_sect163r1 2
+
+/** \brief Identifier for named curve sect163r2. */
+#define BR_EC_sect163r2 3
+
+/** \brief Identifier for named curve sect193r1. */
+#define BR_EC_sect193r1 4
+
+/** \brief Identifier for named curve sect193r2. */
+#define BR_EC_sect193r2 5
+
+/** \brief Identifier for named curve sect233k1. */
+#define BR_EC_sect233k1 6
+
+/** \brief Identifier for named curve sect233r1. */
+#define BR_EC_sect233r1 7
+
+/** \brief Identifier for named curve sect239k1. */
+#define BR_EC_sect239k1 8
+
+/** \brief Identifier for named curve sect283k1. */
+#define BR_EC_sect283k1 9
+
+/** \brief Identifier for named curve sect283r1. */
+#define BR_EC_sect283r1 10
+
+/** \brief Identifier for named curve sect409k1. */
+#define BR_EC_sect409k1 11
+
+/** \brief Identifier for named curve sect409r1. */
+#define BR_EC_sect409r1 12
+
+/** \brief Identifier for named curve sect571k1. */
+#define BR_EC_sect571k1 13
+
+/** \brief Identifier for named curve sect571r1. */
+#define BR_EC_sect571r1 14
+
+/** \brief Identifier for named curve secp160k1. */
+#define BR_EC_secp160k1 15
+
+/** \brief Identifier for named curve secp160r1. */
+#define BR_EC_secp160r1 16
+
+/** \brief Identifier for named curve secp160r2. */
+#define BR_EC_secp160r2 17
+
+/** \brief Identifier for named curve secp192k1. */
+#define BR_EC_secp192k1 18
+
+/** \brief Identifier for named curve secp192r1. */
+#define BR_EC_secp192r1 19
+
+/** \brief Identifier for named curve secp224k1. */
+#define BR_EC_secp224k1 20
+
+/** \brief Identifier for named curve secp224r1. */
+#define BR_EC_secp224r1 21
+
+/** \brief Identifier for named curve secp256k1. */
+#define BR_EC_secp256k1 22
+
+/** \brief Identifier for named curve secp256r1. */
+#define BR_EC_secp256r1 23
+
+/** \brief Identifier for named curve secp384r1. */
+#define BR_EC_secp384r1 24
+
+/** \brief Identifier for named curve secp521r1. */
+#define BR_EC_secp521r1 25
+
+/** \brief Identifier for named curve brainpoolP256r1. */
+#define BR_EC_brainpoolP256r1 26
+
+/** \brief Identifier for named curve brainpoolP384r1. */
+#define BR_EC_brainpoolP384r1 27
+
+/** \brief Identifier for named curve brainpoolP512r1. */
+#define BR_EC_brainpoolP512r1 28
+
+/** \brief Identifier for named curve Curve25519. */
+#define BR_EC_curve25519 29
+
+/** \brief Identifier for named curve Curve448. */
+#define BR_EC_curve448 30
+
+/**
+ * \brief Structure for an EC public key.
+ */
+typedef struct {
+ /** \brief Identifier for the curve used by this key. */
+ int curve;
+ /** \brief Public curve point (uncompressed format). */
+ unsigned char *q;
+ /** \brief Length of public curve point (in bytes). */
+ size_t qlen;
+} br_ec_public_key;
+
+/**
+ * \brief Structure for an EC private key.
+ *
+ * The private key is an integer modulo the curve subgroup order. The
+ * encoding below tolerates extra leading zeros. In general, it is
+ * recommended that the private key has the same length as the curve
+ * subgroup order.
+ */
+typedef struct {
+ /** \brief Identifier for the curve used by this key. */
+ int curve;
+ /** \brief Private key (integer, unsigned big-endian encoding). */
+ unsigned char *x;
+ /** \brief Private key length (in bytes). */
+ size_t xlen;
+} br_ec_private_key;
+
+/**
+ * \brief Type for an EC implementation.
+ */
+typedef struct {
+ /**
+ * \brief Supported curves.
+ *
+ * This word is a bitfield: bit `x` is set if the curve of ID `x`
+ * is supported. E.g. an implementation supporting both NIST P-256
+ * (secp256r1, ID 23) and NIST P-384 (secp384r1, ID 24) will have
+ * value `0x01800000` in this field.
+ */
+ uint32_t supported_curves;
+
+ /**
+ * \brief Get the conventional generator.
+ *
+ * This function returns the conventional generator (encoded
+ * curve point) for the specified curve. This function MUST NOT
+ * be called if the curve is not supported.
+ *
+ * \param curve curve identifier.
+ * \param len receiver for the encoded generator length (in bytes).
+ * \return the encoded generator.
+ */
+ const unsigned char *(*generator)(int curve, size_t *len);
+
+ /**
+ * \brief Get the subgroup order.
+ *
+ * This function returns the order of the subgroup generated by
+ * the conventional generator, for the specified curve. Unsigned
+ * big-endian encoding is used. This function MUST NOT be called
+ * if the curve is not supported.
+ *
+ * \param curve curve identifier.
+ * \param len receiver for the encoded order length (in bytes).
+ * \return the encoded order.
+ */
+ const unsigned char *(*order)(int curve, size_t *len);
+
+ /**
+ * \brief Get the offset and length for the X coordinate.
+ *
+ * This function returns the offset and length (in bytes) of
+ * the X coordinate in an encoded non-zero point.
+ *
+ * \param curve curve identifier.
+ * \param len receiver for the X coordinate length (in bytes).
+ * \return the offset for the X coordinate (in bytes).
+ */
+ size_t (*xoff)(int curve, size_t *len);
+
+ /**
+ * \brief Multiply a curve point by an integer.
+ *
+ * The source point is provided in array `G` (of size `Glen` bytes);
+ * the multiplication result is written over it. The multiplier
+ * `x` (of size `xlen` bytes) uses unsigned big-endian encoding.
+ *
+ * Rules:
+ *
+ * - The specified curve MUST be supported.
+ *
+ * - The source point must be a valid point on the relevant curve
+ * subgroup (and not the "point at infinity" either). If this is
+ * not the case, then this function returns an error (0).
+ *
+ * - The multiplier integer MUST be non-zero and less than the
+ * curve subgroup order. If this property does not hold, then
+ * the result is indeterminate and an error code is not
+ * guaranteed.
+ *
+ * Returned value is 1 on success, 0 on error. On error, the
+ * contents of `G` are indeterminate.
+ *
+ * \param G point to multiply.
+ * \param Glen length of the encoded point (in bytes).
+ * \param x multiplier (unsigned big-endian).
+ * \param xlen multiplier length (in bytes).
+ * \param curve curve identifier.
+ * \return 1 on success, 0 on error.
+ */
+ uint32_t (*mul)(unsigned char *G, size_t Glen,
+ const unsigned char *x, size_t xlen, int curve);
+
+ /**
+ * \brief Multiply the generator by an integer.
+ *
+ * The multiplier MUST be non-zero and less than the curve
+ * subgroup order. Results are indeterminate if this property
+ * does not hold.
+ *
+ * \param R output buffer for the point.
+ * \param x multiplier (unsigned big-endian).
+ * \param xlen multiplier length (in bytes).
+ * \param curve curve identifier.
+ * \return encoded result point length (in bytes).
+ */
+ size_t (*mulgen)(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve);
+
+ /**
+ * \brief Multiply two points by two integers and add the
+ * results.
+ *
+ * The point `x*A + y*B` is computed and written back in the `A`
+ * array.
+ *
+ * Rules:
+ *
+ * - The specified curve MUST be supported.
+ *
+ * - The source points (`A` and `B`) must be valid points on
+ * the relevant curve subgroup (and not the "point at
+ * infinity" either). If this is not the case, then this
+ * function returns an error (0).
+ *
+ * - If the `B` pointer is `NULL`, then the conventional
+ * subgroup generator is used. With some implementations,
+ * this may be faster than providing a pointer to the
+ * generator.
+ *
+ * - The multiplier integers (`x` and `y`) MUST be non-zero
+ * and less than the curve subgroup order. If either integer
+ * is zero, then an error is reported, but if one of them is
+ * not lower than the subgroup order, then the result is
+ * indeterminate and an error code is not guaranteed.
+ *
+ * - If the final result is the point at infinity, then an
+ * error is returned.
+ *
+ * Returned value is 1 on success, 0 on error. On error, the
+ * contents of `A` are indeterminate.
+ *
+ * \param A first point to multiply.
+ * \param B second point to multiply (`NULL` for the generator).
+ * \param len common length of the encoded points (in bytes).
+ * \param x multiplier for `A` (unsigned big-endian).
+ * \param xlen length of multiplier for `A` (in bytes).
+ * \param y multiplier for `A` (unsigned big-endian).
+ * \param ylen length of multiplier for `A` (in bytes).
+ * \param curve curve identifier.
+ * \return 1 on success, 0 on error.
+ */
+ uint32_t (*muladd)(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve);
+} br_ec_impl;
+
+/**
+ * \brief EC implementation "i31".
+ *
+ * This implementation internally uses generic code for modular integers,
+ * with a representation as sequences of 31-bit words. It supports secp256r1,
+ * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
+ */
+extern const br_ec_impl br_ec_prime_i31;
+
+/**
+ * \brief EC implementation "i15".
+ *
+ * This implementation internally uses generic code for modular integers,
+ * with a representation as sequences of 15-bit words. It supports secp256r1,
+ * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
+ */
+extern const br_ec_impl br_ec_prime_i15;
+
+/**
+ * \brief EC implementation "m15" for P-256.
+ *
+ * This implementation uses specialised code for curve secp256r1 (also
+ * known as NIST P-256), with optional Karatsuba decomposition, and fast
+ * modular reduction thanks to the field modulus special format. Only
+ * 32-bit multiplications are used (with 32-bit results, not 64-bit).
+ */
+extern const br_ec_impl br_ec_p256_m15;
+
+/**
+ * \brief EC implementation "m31" for P-256.
+ *
+ * This implementation uses specialised code for curve secp256r1 (also
+ * known as NIST P-256), relying on multiplications of 31-bit values
+ * (MUL31).
+ */
+extern const br_ec_impl br_ec_p256_m31;
+
+/**
+ * \brief EC implementation "m62" (specialised code) for P-256.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 64 bits, with a 128-bit result. This implementation is
+ * defined only on platforms that offer the 64x64->128 multiplication
+ * support; use `br_ec_p256_m62_get()` to dynamically obtain a pointer
+ * to that implementation.
+ */
+extern const br_ec_impl br_ec_p256_m62;
+
+/**
+ * \brief Get the "m62" implementation of P-256, if available.
+ *
+ * \return the implementation, or 0.
+ */
+const br_ec_impl *br_ec_p256_m62_get(void);
+
+/**
+ * \brief EC implementation "m64" (specialised code) for P-256.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 64 bits, with a 128-bit result. This implementation is
+ * defined only on platforms that offer the 64x64->128 multiplication
+ * support; use `br_ec_p256_m64_get()` to dynamically obtain a pointer
+ * to that implementation.
+ */
+extern const br_ec_impl br_ec_p256_m64;
+
+/**
+ * \brief Get the "m64" implementation of P-256, if available.
+ *
+ * \return the implementation, or 0.
+ */
+const br_ec_impl *br_ec_p256_m64_get(void);
+
+/**
+ * \brief EC implementation "i15" (generic code) for Curve25519.
+ *
+ * This implementation uses the generic code for modular integers (with
+ * 15-bit words) to support Curve25519. Due to the specificities of the
+ * curve definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_i15;
+
+/**
+ * \brief EC implementation "i31" (generic code) for Curve25519.
+ *
+ * This implementation uses the generic code for modular integers (with
+ * 31-bit words) to support Curve25519. Due to the specificities of the
+ * curve definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_i31;
+
+/**
+ * \brief EC implementation "m15" (specialised code) for Curve25519.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 15 bits. Due to the specificities of the curve
+ * definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_m15;
+
+/**
+ * \brief EC implementation "m31" (specialised code) for Curve25519.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 31 bits. Due to the specificities of the curve
+ * definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_m31;
+
+/**
+ * \brief EC implementation "m62" (specialised code) for Curve25519.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 62 bits, with a 124-bit result. This implementation is
+ * defined only on platforms that offer the 64x64->128 multiplication
+ * support; use `br_ec_c25519_m62_get()` to dynamically obtain a pointer
+ * to that implementation. Due to the specificities of the curve
+ * definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_m62;
+
+/**
+ * \brief Get the "m62" implementation of Curve25519, if available.
+ *
+ * \return the implementation, or 0.
+ */
+const br_ec_impl *br_ec_c25519_m62_get(void);
+
+/**
+ * \brief EC implementation "m64" (specialised code) for Curve25519.
+ *
+ * This implementation uses custom code relying on multiplication of
+ * integers up to 64 bits, with a 128-bit result. This implementation is
+ * defined only on platforms that offer the 64x64->128 multiplication
+ * support; use `br_ec_c25519_m64_get()` to dynamically obtain a pointer
+ * to that implementation. Due to the specificities of the curve
+ * definition, the following applies:
+ *
+ * - `muladd()` is not implemented (the function returns 0 systematically).
+ * - `order()` returns 2^255-1, since the point multiplication algorithm
+ * accepts any 32-bit integer as input (it clears the top bit and low
+ * three bits systematically).
+ */
+extern const br_ec_impl br_ec_c25519_m64;
+
+/**
+ * \brief Get the "m64" implementation of Curve25519, if available.
+ *
+ * \return the implementation, or 0.
+ */
+const br_ec_impl *br_ec_c25519_m64_get(void);
+
+/**
+ * \brief Aggregate EC implementation "m15".
+ *
+ * This implementation is a wrapper for:
+ *
+ * - `br_ec_c25519_m15` for Curve25519
+ * - `br_ec_p256_m15` for NIST P-256
+ * - `br_ec_prime_i15` for other curves (NIST P-384 and NIST-P512)
+ */
+extern const br_ec_impl br_ec_all_m15;
+
+/**
+ * \brief Aggregate EC implementation "m31".
+ *
+ * This implementation is a wrapper for:
+ *
+ * - `br_ec_c25519_m31` for Curve25519
+ * - `br_ec_p256_m31` for NIST P-256
+ * - `br_ec_prime_i31` for other curves (NIST P-384 and NIST-P512)
+ */
+extern const br_ec_impl br_ec_all_m31;
+
+/**
+ * \brief Get the "default" EC implementation for the current system.
+ *
+ * This returns a pointer to the preferred implementation on the
+ * current system.
+ *
+ * \return the default EC implementation.
+ */
+const br_ec_impl *br_ec_get_default(void);
+
+/**
+ * \brief Convert a signature from "raw" to "asn1".
+ *
+ * Conversion is done "in place" and the new length is returned.
+ * Conversion may enlarge the signature, but by no more than 9 bytes at
+ * most. On error, 0 is returned (error conditions include an odd raw
+ * signature length, or an oversized integer).
+ *
+ * \param sig signature to convert.
+ * \param sig_len signature length (in bytes).
+ * \return the new signature length, or 0 on error.
+ */
+size_t br_ecdsa_raw_to_asn1(void *sig, size_t sig_len);
+
+/**
+ * \brief Convert a signature from "asn1" to "raw".
+ *
+ * Conversion is done "in place" and the new length is returned.
+ * Conversion may enlarge the signature, but the new signature length
+ * will be less than twice the source length at most. On error, 0 is
+ * returned (error conditions include an invalid ASN.1 structure or an
+ * oversized integer).
+ *
+ * \param sig signature to convert.
+ * \param sig_len signature length (in bytes).
+ * \return the new signature length, or 0 on error.
+ */
+size_t br_ecdsa_asn1_to_raw(void *sig, size_t sig_len);
+
+/**
+ * \brief Type for an ECDSA signer function.
+ *
+ * A pointer to the EC implementation is provided. The hash value is
+ * assumed to have the length inferred from the designated hash function
+ * class.
+ *
+ * Signature is written in the buffer pointed to by `sig`, and the length
+ * (in bytes) is returned. On error, nothing is written in the buffer,
+ * and 0 is returned. This function returns 0 if the specified curve is
+ * not supported by the provided EC implementation.
+ *
+ * The signature format is either "raw" or "asn1", depending on the
+ * implementation; maximum length is predictable from the implemented
+ * curve:
+ *
+ * | curve | raw | asn1 |
+ * | :--------- | --: | ---: |
+ * | NIST P-256 | 64 | 72 |
+ * | NIST P-384 | 96 | 104 |
+ * | NIST P-521 | 132 | 139 |
+ *
+ * \param impl EC implementation to use.
+ * \param hf hash function used to process the data.
+ * \param hash_value signed data (hashed).
+ * \param sk EC private key.
+ * \param sig destination buffer.
+ * \return the signature length (in bytes), or 0 on error.
+ */
+typedef size_t (*br_ecdsa_sign)(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig);
+
+/**
+ * \brief Type for an ECDSA signature verification function.
+ *
+ * A pointer to the EC implementation is provided. The hashed value,
+ * computed over the purportedly signed data, is also provided with
+ * its length.
+ *
+ * The signature format is either "raw" or "asn1", depending on the
+ * implementation.
+ *
+ * Returned value is 1 on success (valid signature), 0 on error. This
+ * function returns 0 if the specified curve is not supported by the
+ * provided EC implementation.
+ *
+ * \param impl EC implementation to use.
+ * \param hash signed data (hashed).
+ * \param hash_len hash value length (in bytes).
+ * \param pk EC public key.
+ * \param sig signature.
+ * \param sig_len signature length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_ecdsa_vrfy)(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk, const void *sig, size_t sig_len);
+
+/**
+ * \brief ECDSA signature generator, "i31" implementation, "asn1" format.
+ *
+ * \see br_ecdsa_sign()
+ *
+ * \param impl EC implementation to use.
+ * \param hf hash function used to process the data.
+ * \param hash_value signed data (hashed).
+ * \param sk EC private key.
+ * \param sig destination buffer.
+ * \return the signature length (in bytes), or 0 on error.
+ */
+size_t br_ecdsa_i31_sign_asn1(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig);
+
+/**
+ * \brief ECDSA signature generator, "i31" implementation, "raw" format.
+ *
+ * \see br_ecdsa_sign()
+ *
+ * \param impl EC implementation to use.
+ * \param hf hash function used to process the data.
+ * \param hash_value signed data (hashed).
+ * \param sk EC private key.
+ * \param sig destination buffer.
+ * \return the signature length (in bytes), or 0 on error.
+ */
+size_t br_ecdsa_i31_sign_raw(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig);
+
+/**
+ * \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
+ *
+ * \see br_ecdsa_vrfy()
+ *
+ * \param impl EC implementation to use.
+ * \param hash signed data (hashed).
+ * \param hash_len hash value length (in bytes).
+ * \param pk EC public key.
+ * \param sig signature.
+ * \param sig_len signature length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk, const void *sig, size_t sig_len);
+
+/**
+ * \brief ECDSA signature verifier, "i31" implementation, "raw" format.
+ *
+ * \see br_ecdsa_vrfy()
+ *
+ * \param impl EC implementation to use.
+ * \param hash signed data (hashed).
+ * \param hash_len hash value length (in bytes).
+ * \param pk EC public key.
+ * \param sig signature.
+ * \param sig_len signature length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk, const void *sig, size_t sig_len);
+
+/**
+ * \brief ECDSA signature generator, "i15" implementation, "asn1" format.
+ *
+ * \see br_ecdsa_sign()
+ *
+ * \param impl EC implementation to use.
+ * \param hf hash function used to process the data.
+ * \param hash_value signed data (hashed).
+ * \param sk EC private key.
+ * \param sig destination buffer.
+ * \return the signature length (in bytes), or 0 on error.
+ */
+size_t br_ecdsa_i15_sign_asn1(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig);
+
+/**
+ * \brief ECDSA signature generator, "i15" implementation, "raw" format.
+ *
+ * \see br_ecdsa_sign()
+ *
+ * \param impl EC implementation to use.
+ * \param hf hash function used to process the data.
+ * \param hash_value signed data (hashed).
+ * \param sk EC private key.
+ * \param sig destination buffer.
+ * \return the signature length (in bytes), or 0 on error.
+ */
+size_t br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig);
+
+/**
+ * \brief ECDSA signature verifier, "i15" implementation, "asn1" format.
+ *
+ * \see br_ecdsa_vrfy()
+ *
+ * \param impl EC implementation to use.
+ * \param hash signed data (hashed).
+ * \param hash_len hash value length (in bytes).
+ * \param pk EC public key.
+ * \param sig signature.
+ * \param sig_len signature length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk, const void *sig, size_t sig_len);
+
+/**
+ * \brief ECDSA signature verifier, "i15" implementation, "raw" format.
+ *
+ * \see br_ecdsa_vrfy()
+ *
+ * \param impl EC implementation to use.
+ * \param hash signed data (hashed).
+ * \param hash_len hash value length (in bytes).
+ * \param pk EC public key.
+ * \param sig signature.
+ * \param sig_len signature length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk, const void *sig, size_t sig_len);
+
+/**
+ * \brief Get "default" ECDSA implementation (signer, asn1 format).
+ *
+ * This returns the preferred implementation of ECDSA signature generation
+ * ("asn1" output format) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_ecdsa_sign br_ecdsa_sign_asn1_get_default(void);
+
+/**
+ * \brief Get "default" ECDSA implementation (signer, raw format).
+ *
+ * This returns the preferred implementation of ECDSA signature generation
+ * ("raw" output format) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_ecdsa_sign br_ecdsa_sign_raw_get_default(void);
+
+/**
+ * \brief Get "default" ECDSA implementation (verifier, asn1 format).
+ *
+ * This returns the preferred implementation of ECDSA signature verification
+ * ("asn1" output format) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_ecdsa_vrfy br_ecdsa_vrfy_asn1_get_default(void);
+
+/**
+ * \brief Get "default" ECDSA implementation (verifier, raw format).
+ *
+ * This returns the preferred implementation of ECDSA signature verification
+ * ("raw" output format) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_ecdsa_vrfy br_ecdsa_vrfy_raw_get_default(void);
+
+/**
+ * \brief Maximum size for EC private key element buffer.
+ *
+ * This is the largest number of bytes that `br_ec_keygen()` may need or
+ * ever return.
+ */
+#define BR_EC_KBUF_PRIV_MAX_SIZE 72
+
+/**
+ * \brief Maximum size for EC public key element buffer.
+ *
+ * This is the largest number of bytes that `br_ec_compute_public()` may
+ * need or ever return.
+ */
+#define BR_EC_KBUF_PUB_MAX_SIZE 145
+
+/**
+ * \brief Generate a new EC private key.
+ *
+ * If the specified `curve` is not supported by the elliptic curve
+ * implementation (`impl`), then this function returns zero.
+ *
+ * The `sk` structure fields are set to the new private key data. In
+ * particular, `sk.x` is made to point to the provided key buffer (`kbuf`),
+ * in which the actual private key data is written. That buffer is assumed
+ * to be large enough. The `BR_EC_KBUF_PRIV_MAX_SIZE` defines the maximum
+ * size for all supported curves.
+ *
+ * The number of bytes used in `kbuf` is returned. If `kbuf` is `NULL`, then
+ * the private key is not actually generated, and `sk` may also be `NULL`;
+ * the minimum length for `kbuf` is still computed and returned.
+ *
+ * If `sk` is `NULL` but `kbuf` is not `NULL`, then the private key is
+ * still generated and stored in `kbuf`.
+ *
+ * \param rng_ctx source PRNG context (already initialized).
+ * \param impl the elliptic curve implementation.
+ * \param sk the private key structure to fill, or `NULL`.
+ * \param kbuf the key element buffer, or `NULL`.
+ * \param curve the curve identifier.
+ * \return the key data length (in bytes), or zero.
+ */
+size_t br_ec_keygen(const br_prng_class **rng_ctx,
+ const br_ec_impl *impl, br_ec_private_key *sk,
+ void *kbuf, int curve);
+
+/**
+ * \brief Compute EC public key from EC private key.
+ *
+ * This function uses the provided elliptic curve implementation (`impl`)
+ * to compute the public key corresponding to the private key held in `sk`.
+ * The public key point is written into `kbuf`, which is then linked from
+ * the `*pk` structure. The size of the public key point, i.e. the number
+ * of bytes used in `kbuf`, is returned.
+ *
+ * If `kbuf` is `NULL`, then the public key point is NOT computed, and
+ * the public key structure `*pk` is unmodified (`pk` may be `NULL` in
+ * that case). The size of the public key point is still returned.
+ *
+ * If `pk` is `NULL` but `kbuf` is not `NULL`, then the public key
+ * point is computed and stored in `kbuf`, and its size is returned.
+ *
+ * If the curve used by the private key is not supported by the curve
+ * implementation, then this function returns zero.
+ *
+ * The private key MUST be valid. An off-range private key value is not
+ * necessarily detected, and leads to unpredictable results.
+ *
+ * \param impl the elliptic curve implementation.
+ * \param pk the public key structure to fill (or `NULL`).
+ * \param kbuf the public key point buffer (or `NULL`).
+ * \param sk the source private key.
+ * \return the public key point length (in bytes), or zero.
+ */
+size_t br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,
+ void *kbuf, const br_ec_private_key *sk);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_hash.h b/test/monniaux/BearSSL/inc/bearssl_hash.h
new file mode 100644
index 00000000..3b15ba7c
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_hash.h
@@ -0,0 +1,1346 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_HASH_H__
+#define BR_BEARSSL_HASH_H__
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_hash.h
+ *
+ * # Hash Functions
+ *
+ * This file documents the API for hash functions.
+ *
+ *
+ * ## Procedural API
+ *
+ * For each implemented hash function, of name "`xxx`", the following
+ * elements are defined:
+ *
+ * - `br_xxx_vtable`
+ *
+ * An externally defined instance of `br_hash_class`.
+ *
+ * - `br_xxx_SIZE`
+ *
+ * A macro that evaluates to the output size (in bytes) of the
+ * hash function.
+ *
+ * - `br_xxx_ID`
+ *
+ * A macro that evaluates to a symbolic identifier for the hash
+ * function. Such identifiers are used with HMAC and signature
+ * algorithm implementations.
+ *
+ * NOTE: for the "standard" hash functions defined in [the TLS
+ * standard](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1),
+ * the symbolic identifiers match the constants used in TLS, i.e.
+ * 1 to 6 for MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512,
+ * respectively.
+ *
+ * - `br_xxx_context`
+ *
+ * Context for an ongoing computation. It is allocated by the
+ * caller, and a pointer to it is passed to all functions. A
+ * context contains no interior pointer, so it can be moved around
+ * and cloned (with a simple `memcpy()` or equivalent) in order to
+ * capture the function state at some point. Computations that use
+ * distinct context structures are independent of each other. The
+ * first field of `br_xxx_context` is always a pointer to the
+ * `br_xxx_vtable` structure; `br_xxx_init()` sets that pointer.
+ *
+ * - `br_xxx_init(br_xxx_context *ctx)`
+ *
+ * Initialise the provided context. Previous contents of the structure
+ * are ignored. This calls resets the context to the start of a new
+ * hash computation; it also sets the first field of the context
+ * structure (called `vtable`) to a pointer to the statically
+ * allocated constant `br_xxx_vtable` structure.
+ *
+ * - `br_xxx_update(br_xxx_context *ctx, const void *data, size_t len)`
+ *
+ * Add some more bytes to the hash computation represented by the
+ * provided context.
+ *
+ * - `br_xxx_out(const br_xxx_context *ctx, void *out)`
+ *
+ * Complete the hash computation and write the result in the provided
+ * buffer. The output buffer MUST be large enough to accommodate the
+ * result. The context is NOT modified by this operation, so this
+ * function can be used to get a "partial hash" while still keeping
+ * the possibility of adding more bytes to the input.
+ *
+ * - `br_xxx_state(const br_xxx_context *ctx, void *out)`
+ *
+ * Get a copy of the "current state" for the computation so far. For
+ * MD functions (MD5, SHA-1, SHA-2 family), this is the running state
+ * resulting from the processing of the last complete input block.
+ * Returned value is the current input length (in bytes).
+ *
+ * - `br_xxx_set_state(br_xxx_context *ctx, const void *stb, uint64_t count)`
+ *
+ * Set the internal state to the provided values. The 'stb' and
+ * 'count' values shall match that which was obtained from
+ * `br_xxx_state()`. This restores the hash state only if the state
+ * values were at an appropriate block boundary. This does NOT set
+ * the `vtable` pointer in the context.
+ *
+ * Context structures can be discarded without any explicit deallocation.
+ * Hash function implementations are purely software and don't reserve
+ * any resources outside of the context structure itself.
+ *
+ *
+ * ## Object-Oriented API
+ *
+ * For each hash function that follows the procedural API described
+ * above, an object-oriented API is also provided. In that API, function
+ * pointers from the vtable (`br_xxx_vtable`) are used. The vtable
+ * incarnates object-oriented programming. An introduction on the OOP
+ * concept used here can be read on the BearSSL Web site:<br />
+ * &nbsp;&nbsp;&nbsp;[https://www.bearssl.org/oop.html](https://www.bearssl.org/oop.html)
+ *
+ * The vtable offers functions called `init()`, `update()`, `out()`,
+ * `set()` and `set_state()`, which are in fact the functions from
+ * the procedural API. That vtable also contains two informative fields:
+ *
+ * - `context_size`
+ *
+ * The size of the context structure (`br_xxx_context`), in bytes.
+ * This can be used by generic implementations to perform dynamic
+ * context allocation.
+ *
+ * - `desc`
+ *
+ * A "descriptor" field that encodes some information on the hash
+ * function: symbolic identifier, output size, state size,
+ * internal block size, details on the padding.
+ *
+ * Users of this object-oriented API (in particular generic HMAC
+ * implementations) may make the following assumptions:
+ *
+ * - Hash output size is no more than 64 bytes.
+ * - Hash internal state size is no more than 64 bytes.
+ * - Internal block size is a power of two, no less than 16 and no more
+ * than 256.
+ *
+ *
+ * ## Implemented Hash Functions
+ *
+ * Implemented hash functions are:
+ *
+ * | Function | Name | Output length | State length |
+ * | :-------- | :------ | :-----------: | :----------: |
+ * | MD5 | md5 | 16 | 16 |
+ * | SHA-1 | sha1 | 20 | 20 |
+ * | SHA-224 | sha224 | 28 | 32 |
+ * | SHA-256 | sha256 | 32 | 32 |
+ * | SHA-384 | sha384 | 48 | 64 |
+ * | SHA-512 | sha512 | 64 | 64 |
+ * | MD5+SHA-1 | md5sha1 | 36 | 36 |
+ *
+ * (MD5+SHA-1 is the concatenation of MD5 and SHA-1 computed over the
+ * same input; in the implementation, the internal data buffer is
+ * shared, thus making it more memory-efficient than separate MD5 and
+ * SHA-1. It can be useful in implementing SSL 3.0, TLS 1.0 and TLS
+ * 1.1.)
+ *
+ *
+ * ## Multi-Hasher
+ *
+ * An aggregate hasher is provided, that can compute several standard
+ * hash functions in parallel. It uses `br_multihash_context` and a
+ * procedural API. It is configured with the implementations (the vtables)
+ * that it should use; it will then compute all these hash functions in
+ * parallel, on the same input. It is meant to be used in cases when the
+ * hash of an object will be used, but the exact hash function is not
+ * known yet (typically, streamed processing on X.509 certificates).
+ *
+ * Only the standard hash functions (MD5, SHA-1, SHA-224, SHA-256, SHA-384
+ * and SHA-512) are supported by the multi-hasher.
+ *
+ *
+ * ## GHASH
+ *
+ * GHASH is not a generic hash function; it is a _universal_ hash function,
+ * which, as the name does not say, means that it CANNOT be used in most
+ * places where a hash function is needed. GHASH is used within the GCM
+ * encryption mode, to provide the checked integrity functionality.
+ *
+ * A GHASH implementation is basically a function that uses the type defined
+ * in this file under the name `br_ghash`:
+ *
+ * typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);
+ *
+ * The `y` pointer refers to a 16-byte value which is used as input, and
+ * receives the output of the GHASH invocation. `h` is a 16-byte secret
+ * value (that serves as key). `data` and `len` define the input data.
+ *
+ * Three GHASH implementations are provided, all constant-time, based on
+ * the use of integer multiplications with appropriate masking to cancel
+ * carry propagation.
+ */
+
+/**
+ * \brief Class type for hash function implementations.
+ *
+ * A `br_hash_class` instance references the methods implementing a hash
+ * function. Constant instances of this structure are defined for each
+ * implemented hash function. Such instances are also called "vtables".
+ *
+ * Vtables are used to support object-oriented programming, as
+ * described on [the BearSSL Web site](https://www.bearssl.org/oop.html).
+ */
+typedef struct br_hash_class_ br_hash_class;
+struct br_hash_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate for
+ * computing this hash function.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Descriptor word that contains information about the hash
+ * function.
+ *
+ * For each word `xxx` described below, use `BR_HASHDESC_xxx_OFF`
+ * and `BR_HASHDESC_xxx_MASK` to access the specific value, as
+ * follows:
+ *
+ * (hf->desc >> BR_HASHDESC_xxx_OFF) & BR_HASHDESC_xxx_MASK
+ *
+ * The defined elements are:
+ *
+ * - `ID`: the symbolic identifier for the function, as defined
+ * in [TLS](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1)
+ * (MD5 = 1, SHA-1 = 2,...).
+ *
+ * - `OUT`: hash output size, in bytes.
+ *
+ * - `STATE`: internal running state size, in bytes.
+ *
+ * - `LBLEN`: base-2 logarithm for the internal block size, as
+ * defined for HMAC processing (this is 6 for MD5, SHA-1, SHA-224
+ * and SHA-256, since these functions use 64-byte blocks; for
+ * SHA-384 and SHA-512, this is 7, corresponding to their
+ * 128-byte blocks).
+ *
+ * The descriptor may contain a few other flags.
+ */
+ uint32_t desc;
+
+ /**
+ * \brief Initialisation method.
+ *
+ * This method takes as parameter a pointer to a context area,
+ * that it initialises. The first field of the context is set
+ * to this vtable; other elements are initialised for a new hash
+ * computation.
+ *
+ * \param ctx pointer to (the first field of) the context.
+ */
+ void (*init)(const br_hash_class **ctx);
+
+ /**
+ * \brief Data injection method.
+ *
+ * The `len` bytes starting at address `data` are injected into
+ * the running hash computation incarnated by the specified
+ * context. The context is updated accordingly. It is allowed
+ * to have `len == 0`, in which case `data` is ignored (and could
+ * be `NULL`), and nothing happens.
+ * on the input data.
+ *
+ * \param ctx pointer to (the first field of) the context.
+ * \param data pointer to the first data byte to inject.
+ * \param len number of bytes to inject.
+ */
+ void (*update)(const br_hash_class **ctx, const void *data, size_t len);
+
+ /**
+ * \brief Produce hash output.
+ *
+ * The hash output corresponding to all data bytes injected in the
+ * context since the last `init()` call is computed, and written
+ * in the buffer pointed to by `dst`. The hash output size depends
+ * on the implemented hash function (e.g. 16 bytes for MD5).
+ * The context is _not_ modified by this call, so further bytes
+ * may be afterwards injected to continue the current computation.
+ *
+ * \param ctx pointer to (the first field of) the context.
+ * \param dst destination buffer for the hash output.
+ */
+ void (*out)(const br_hash_class *const *ctx, void *dst);
+
+ /**
+ * \brief Get running state.
+ *
+ * This method saves the current running state into the `dst`
+ * buffer. What constitutes the "running state" depends on the
+ * hash function; for Merkle-Damgård hash functions (like
+ * MD5 or SHA-1), this is the output obtained after processing
+ * each block. The number of bytes injected so far is returned.
+ * The context is not modified by this call.
+ *
+ * \param ctx pointer to (the first field of) the context.
+ * \param dst destination buffer for the state.
+ * \return the injected total byte length.
+ */
+ uint64_t (*state)(const br_hash_class *const *ctx, void *dst);
+
+ /**
+ * \brief Set running state.
+ *
+ * This methods replaces the running state for the function.
+ *
+ * \param ctx pointer to (the first field of) the context.
+ * \param stb source buffer for the state.
+ * \param count injected total byte length.
+ */
+ void (*set_state)(const br_hash_class **ctx,
+ const void *stb, uint64_t count);
+};
+
+#ifndef BR_DOXYGEN_IGNORE
+#define BR_HASHDESC_ID(id) ((uint32_t)(id) << BR_HASHDESC_ID_OFF)
+#define BR_HASHDESC_ID_OFF 0
+#define BR_HASHDESC_ID_MASK 0xFF
+
+#define BR_HASHDESC_OUT(size) ((uint32_t)(size) << BR_HASHDESC_OUT_OFF)
+#define BR_HASHDESC_OUT_OFF 8
+#define BR_HASHDESC_OUT_MASK 0x7F
+
+#define BR_HASHDESC_STATE(size) ((uint32_t)(size) << BR_HASHDESC_STATE_OFF)
+#define BR_HASHDESC_STATE_OFF 15
+#define BR_HASHDESC_STATE_MASK 0xFF
+
+#define BR_HASHDESC_LBLEN(ls) ((uint32_t)(ls) << BR_HASHDESC_LBLEN_OFF)
+#define BR_HASHDESC_LBLEN_OFF 23
+#define BR_HASHDESC_LBLEN_MASK 0x0F
+
+#define BR_HASHDESC_MD_PADDING ((uint32_t)1 << 28)
+#define BR_HASHDESC_MD_PADDING_128 ((uint32_t)1 << 29)
+#define BR_HASHDESC_MD_PADDING_BE ((uint32_t)1 << 30)
+#endif
+
+/*
+ * Specific hash functions.
+ *
+ * Rules for contexts:
+ * -- No interior pointer.
+ * -- No pointer to external dynamically allocated resources.
+ * -- First field is called 'vtable' and is a pointer to a
+ * const-qualified br_hash_class instance (pointer is set by init()).
+ * -- SHA-224 and SHA-256 contexts are identical.
+ * -- SHA-384 and SHA-512 contexts are identical.
+ *
+ * Thus, contexts can be moved and cloned to capture the hash function
+ * current state; and there is no need for any explicit "release" function.
+ */
+
+/**
+ * \brief Symbolic identifier for MD5.
+ */
+#define br_md5_ID 1
+
+/**
+ * \brief MD5 output size (in bytes).
+ */
+#define br_md5_SIZE 16
+
+/**
+ * \brief Constant vtable for MD5.
+ */
+extern const br_hash_class br_md5_vtable;
+
+/**
+ * \brief MD5 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[64];
+ uint64_t count;
+ uint32_t val[4];
+#endif
+} br_md5_context;
+
+/**
+ * \brief MD5 context initialisation.
+ *
+ * This function initialises or resets a context for a new MD5
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_md5_init(br_md5_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running MD5 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_md5_update(br_md5_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute MD5 output.
+ *
+ * The MD5 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_md5_out(const br_md5_context *ctx, void *out);
+
+/**
+ * \brief Save MD5 running state.
+ *
+ * The running state for MD5 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_md5_state(const br_md5_context *ctx, void *out);
+
+/**
+ * \brief Restore MD5 running state.
+ *
+ * The running state for MD5 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_md5_set_state(br_md5_context *ctx, const void *stb, uint64_t count);
+
+/**
+ * \brief Symbolic identifier for SHA-1.
+ */
+#define br_sha1_ID 2
+
+/**
+ * \brief SHA-1 output size (in bytes).
+ */
+#define br_sha1_SIZE 20
+
+/**
+ * \brief Constant vtable for SHA-1.
+ */
+extern const br_hash_class br_sha1_vtable;
+
+/**
+ * \brief SHA-1 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[64];
+ uint64_t count;
+ uint32_t val[5];
+#endif
+} br_sha1_context;
+
+/**
+ * \brief SHA-1 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-1
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_sha1_init(br_sha1_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running SHA-1 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_sha1_update(br_sha1_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute SHA-1 output.
+ *
+ * The SHA-1 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_sha1_out(const br_sha1_context *ctx, void *out);
+
+/**
+ * \brief Save SHA-1 running state.
+ *
+ * The running state for SHA-1 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_sha1_state(const br_sha1_context *ctx, void *out);
+
+/**
+ * \brief Restore SHA-1 running state.
+ *
+ * The running state for SHA-1 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_sha1_set_state(br_sha1_context *ctx, const void *stb, uint64_t count);
+
+/**
+ * \brief Symbolic identifier for SHA-224.
+ */
+#define br_sha224_ID 3
+
+/**
+ * \brief SHA-224 output size (in bytes).
+ */
+#define br_sha224_SIZE 28
+
+/**
+ * \brief Constant vtable for SHA-224.
+ */
+extern const br_hash_class br_sha224_vtable;
+
+/**
+ * \brief SHA-224 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[64];
+ uint64_t count;
+ uint32_t val[8];
+#endif
+} br_sha224_context;
+
+/**
+ * \brief SHA-224 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-224
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_sha224_init(br_sha224_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running SHA-224 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_sha224_update(br_sha224_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute SHA-224 output.
+ *
+ * The SHA-224 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_sha224_out(const br_sha224_context *ctx, void *out);
+
+/**
+ * \brief Save SHA-224 running state.
+ *
+ * The running state for SHA-224 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_sha224_state(const br_sha224_context *ctx, void *out);
+
+/**
+ * \brief Restore SHA-224 running state.
+ *
+ * The running state for SHA-224 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_sha224_set_state(br_sha224_context *ctx,
+ const void *stb, uint64_t count);
+
+/**
+ * \brief Symbolic identifier for SHA-256.
+ */
+#define br_sha256_ID 4
+
+/**
+ * \brief SHA-256 output size (in bytes).
+ */
+#define br_sha256_SIZE 32
+
+/**
+ * \brief Constant vtable for SHA-256.
+ */
+extern const br_hash_class br_sha256_vtable;
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief SHA-256 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+} br_sha256_context;
+#else
+typedef br_sha224_context br_sha256_context;
+#endif
+
+/**
+ * \brief SHA-256 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-256
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_sha256_init(br_sha256_context *ctx);
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief Inject some data bytes in a running SHA-256 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_sha256_update(br_sha256_context *ctx, const void *data, size_t len);
+#else
+#define br_sha256_update br_sha224_update
+#endif
+
+/**
+ * \brief Compute SHA-256 output.
+ *
+ * The SHA-256 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_sha256_out(const br_sha256_context *ctx, void *out);
+
+#if BR_DOXYGEN_IGNORE
+/**
+ * \brief Save SHA-256 running state.
+ *
+ * The running state for SHA-256 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_sha256_state(const br_sha256_context *ctx, void *out);
+#else
+#define br_sha256_state br_sha224_state
+#endif
+
+#if BR_DOXYGEN_IGNORE
+/**
+ * \brief Restore SHA-256 running state.
+ *
+ * The running state for SHA-256 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_sha256_set_state(br_sha256_context *ctx,
+ const void *stb, uint64_t count);
+#else
+#define br_sha256_set_state br_sha224_set_state
+#endif
+
+/**
+ * \brief Symbolic identifier for SHA-384.
+ */
+#define br_sha384_ID 5
+
+/**
+ * \brief SHA-384 output size (in bytes).
+ */
+#define br_sha384_SIZE 48
+
+/**
+ * \brief Constant vtable for SHA-384.
+ */
+extern const br_hash_class br_sha384_vtable;
+
+/**
+ * \brief SHA-384 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[128];
+ uint64_t count;
+ uint64_t val[8];
+#endif
+} br_sha384_context;
+
+/**
+ * \brief SHA-384 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-384
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_sha384_init(br_sha384_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running SHA-384 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_sha384_update(br_sha384_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute SHA-384 output.
+ *
+ * The SHA-384 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_sha384_out(const br_sha384_context *ctx, void *out);
+
+/**
+ * \brief Save SHA-384 running state.
+ *
+ * The running state for SHA-384 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_sha384_state(const br_sha384_context *ctx, void *out);
+
+/**
+ * \brief Restore SHA-384 running state.
+ *
+ * The running state for SHA-384 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_sha384_set_state(br_sha384_context *ctx,
+ const void *stb, uint64_t count);
+
+/**
+ * \brief Symbolic identifier for SHA-512.
+ */
+#define br_sha512_ID 6
+
+/**
+ * \brief SHA-512 output size (in bytes).
+ */
+#define br_sha512_SIZE 64
+
+/**
+ * \brief Constant vtable for SHA-512.
+ */
+extern const br_hash_class br_sha512_vtable;
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief SHA-512 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+} br_sha512_context;
+#else
+typedef br_sha384_context br_sha512_context;
+#endif
+
+/**
+ * \brief SHA-512 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-512
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_sha512_init(br_sha512_context *ctx);
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief Inject some data bytes in a running SHA-512 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_sha512_update(br_sha512_context *ctx, const void *data, size_t len);
+#else
+#define br_sha512_update br_sha384_update
+#endif
+
+/**
+ * \brief Compute SHA-512 output.
+ *
+ * The SHA-512 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_sha512_out(const br_sha512_context *ctx, void *out);
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief Save SHA-512 running state.
+ *
+ * The running state for SHA-512 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_sha512_state(const br_sha512_context *ctx, void *out);
+#else
+#define br_sha512_state br_sha384_state
+#endif
+
+#ifdef BR_DOXYGEN_IGNORE
+/**
+ * \brief Restore SHA-512 running state.
+ *
+ * The running state for SHA-512 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_sha512_set_state(br_sha512_context *ctx,
+ const void *stb, uint64_t count);
+#else
+#define br_sha512_set_state br_sha384_set_state
+#endif
+
+/*
+ * "md5sha1" is a special hash function that computes both MD5 and SHA-1
+ * on the same input, and produces a 36-byte output (MD5 and SHA-1
+ * concatenation, in that order). State size is also 36 bytes.
+ */
+
+/**
+ * \brief Symbolic identifier for MD5+SHA-1.
+ *
+ * MD5+SHA-1 is the concatenation of MD5 and SHA-1, computed over the
+ * same input. It is not one of the functions identified in TLS, so
+ * we give it a symbolic identifier of value 0.
+ */
+#define br_md5sha1_ID 0
+
+/**
+ * \brief MD5+SHA-1 output size (in bytes).
+ */
+#define br_md5sha1_SIZE 36
+
+/**
+ * \brief Constant vtable for MD5+SHA-1.
+ */
+extern const br_hash_class br_md5sha1_vtable;
+
+/**
+ * \brief MD5+SHA-1 context.
+ *
+ * First field is a pointer to the vtable; it is set by the initialisation
+ * function. Other fields are not supposed to be accessed by user code.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to vtable for this context.
+ */
+ const br_hash_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[64];
+ uint64_t count;
+ uint32_t val_md5[4];
+ uint32_t val_sha1[5];
+#endif
+} br_md5sha1_context;
+
+/**
+ * \brief MD5+SHA-1 context initialisation.
+ *
+ * This function initialises or resets a context for a new SHA-512
+ * computation. It also sets the vtable pointer.
+ *
+ * \param ctx pointer to the context structure.
+ */
+void br_md5sha1_init(br_md5sha1_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running MD5+SHA-1 computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_md5sha1_update(br_md5sha1_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute MD5+SHA-1 output.
+ *
+ * The MD5+SHA-1 output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `out`. The context
+ * itself is not modified, so extra bytes may be injected afterwards
+ * to continue that computation.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the hash output.
+ */
+void br_md5sha1_out(const br_md5sha1_context *ctx, void *out);
+
+/**
+ * \brief Save MD5+SHA-1 running state.
+ *
+ * The running state for MD5+SHA-1 (output of the last internal block
+ * processing) is written in the buffer pointed to by `out`. The
+ * number of bytes injected since the last initialisation or reset
+ * call is returned. The context is not modified.
+ *
+ * \param ctx pointer to the context structure.
+ * \param out destination buffer for the running state.
+ * \return the injected total byte length.
+ */
+uint64_t br_md5sha1_state(const br_md5sha1_context *ctx, void *out);
+
+/**
+ * \brief Restore MD5+SHA-1 running state.
+ *
+ * The running state for MD5+SHA-1 is set to the provided values.
+ *
+ * \param ctx pointer to the context structure.
+ * \param stb source buffer for the running state.
+ * \param count the injected total byte length.
+ */
+void br_md5sha1_set_state(br_md5sha1_context *ctx,
+ const void *stb, uint64_t count);
+
+/**
+ * \brief Aggregate context for configurable hash function support.
+ *
+ * The `br_hash_compat_context` type is a type which is large enough to
+ * serve as context for all standard hash functions defined above.
+ */
+typedef union {
+ const br_hash_class *vtable;
+ br_md5_context md5;
+ br_sha1_context sha1;
+ br_sha224_context sha224;
+ br_sha256_context sha256;
+ br_sha384_context sha384;
+ br_sha512_context sha512;
+ br_md5sha1_context md5sha1;
+} br_hash_compat_context;
+
+/*
+ * The multi-hasher is a construct that handles hashing of the same input
+ * data with several hash functions, with a single shared input buffer.
+ * It can handle MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512
+ * simultaneously, though which functions are activated depends on
+ * the set implementation pointers.
+ */
+
+/**
+ * \brief Multi-hasher context structure.
+ *
+ * The multi-hasher runs up to six hash functions in the standard TLS list
+ * (MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512) in parallel, over
+ * the same input.
+ *
+ * The multi-hasher does _not_ follow the OOP structure with a vtable.
+ * Instead, it is configured with the vtables of the hash functions it
+ * should run. Structure fields are not supposed to be accessed directly.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char buf[128];
+ uint64_t count;
+ uint32_t val_32[25];
+ uint64_t val_64[16];
+ const br_hash_class *impl[6];
+#endif
+} br_multihash_context;
+
+/**
+ * \brief Clear a multi-hasher context.
+ *
+ * This should always be called once on a given context, _before_ setting
+ * the implementation pointers.
+ *
+ * \param ctx the multi-hasher context.
+ */
+void br_multihash_zero(br_multihash_context *ctx);
+
+/**
+ * \brief Set a hash function implementation.
+ *
+ * Implementations shall be set _after_ clearing the context (with
+ * `br_multihash_zero()`) but _before_ initialising the computation
+ * (with `br_multihash_init()`). The hash function implementation
+ * MUST be one of the standard hash functions (MD5, SHA-1, SHA-224,
+ * SHA-256, SHA-384 or SHA-512); it may also be `NULL` to remove
+ * an implementation from the multi-hasher.
+ *
+ * \param ctx the multi-hasher context.
+ * \param id the hash function symbolic identifier.
+ * \param impl the hash function vtable, or `NULL`.
+ */
+static inline void
+br_multihash_setimpl(br_multihash_context *ctx,
+ int id, const br_hash_class *impl)
+{
+ /*
+ * This code relies on hash functions ID being values 1 to 6,
+ * in the MD5 to SHA-512 order.
+ */
+ ctx->impl[id - 1] = impl;
+}
+
+/**
+ * \brief Get a hash function implementation.
+ *
+ * This function returns the currently configured vtable for a given
+ * hash function (by symbolic ID). If no such function was configured in
+ * the provided multi-hasher context, then this function returns `NULL`.
+ *
+ * \param ctx the multi-hasher context.
+ * \param id the hash function symbolic identifier.
+ * \return the hash function vtable, or `NULL`.
+ */
+static inline const br_hash_class *
+br_multihash_getimpl(const br_multihash_context *ctx, int id)
+{
+ return ctx->impl[id - 1];
+}
+
+/**
+ * \brief Reset a multi-hasher context.
+ *
+ * This function prepares the context for a new hashing computation,
+ * for all implementations configured at that point.
+ *
+ * \param ctx the multi-hasher context.
+ */
+void br_multihash_init(br_multihash_context *ctx);
+
+/**
+ * \brief Inject some data bytes in a running multi-hashing computation.
+ *
+ * The provided context is updated with some data bytes. If the number
+ * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
+ * and may be `NULL`, and this function does nothing.
+ *
+ * \param ctx pointer to the context structure.
+ * \param data pointer to the injected data.
+ * \param len injected data length (in bytes).
+ */
+void br_multihash_update(br_multihash_context *ctx,
+ const void *data, size_t len);
+
+/**
+ * \brief Compute a hash output from a multi-hasher.
+ *
+ * The hash output for the concatenation of all bytes injected in the
+ * provided context since the last initialisation or reset call, is
+ * computed and written in the buffer pointed to by `dst`. The hash
+ * function to use is identified by `id` and must be one of the standard
+ * hash functions. If that hash function was indeed configured in the
+ * multi-hasher context, the corresponding hash value is written in
+ * `dst` and its length (in bytes) is returned. If the hash function
+ * was _not_ configured, then nothing is written in `dst` and 0 is
+ * returned.
+ *
+ * The context itself is not modified, so extra bytes may be injected
+ * afterwards to continue the hash computations.
+ *
+ * \param ctx pointer to the context structure.
+ * \param id the hash function symbolic identifier.
+ * \param dst destination buffer for the hash output.
+ * \return the hash output length (in bytes), or 0.
+ */
+size_t br_multihash_out(const br_multihash_context *ctx, int id, void *dst);
+
+/**
+ * \brief Type for a GHASH implementation.
+ *
+ * GHASH is a sort of keyed hash meant to be used to implement GCM in
+ * combination with a block cipher (with 16-byte blocks).
+ *
+ * The `y` array has length 16 bytes and is used for input and output; in
+ * a complete GHASH run, it starts with an all-zero value. `h` is a 16-byte
+ * value that serves as key (it is derived from the encryption key in GCM,
+ * using the block cipher). The data length (`len`) is expressed in bytes.
+ * The `y` array is updated.
+ *
+ * If the data length is not a multiple of 16, then the data is implicitly
+ * padded with zeros up to the next multiple of 16. Thus, when using GHASH
+ * in GCM, this method may be called twice, for the associated data and
+ * for the ciphertext, respectively; the zero-padding implements exactly
+ * the GCM rules.
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief GHASH implementation using multiplications (mixed 32-bit).
+ *
+ * This implementation uses multiplications of 32-bit values, with a
+ * 64-bit result. It is constant-time (if multiplications are
+ * constant-time).
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+void br_ghash_ctmul(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief GHASH implementation using multiplications (strict 32-bit).
+ *
+ * This implementation uses multiplications of 32-bit values, with a
+ * 32-bit result. It is usually somewhat slower than `br_ghash_ctmul()`,
+ * but it is expected to be faster on architectures for which the
+ * 32-bit multiplication opcode does not yield the upper 32 bits of the
+ * product. It is constant-time (if multiplications are constant-time).
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+void br_ghash_ctmul32(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief GHASH implementation using multiplications (64-bit).
+ *
+ * This implementation uses multiplications of 64-bit values, with a
+ * 64-bit result. It is constant-time (if multiplications are
+ * constant-time). It is substantially faster than `br_ghash_ctmul()`
+ * and `br_ghash_ctmul32()` on most 64-bit architectures.
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+void br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief GHASH implementation using the `pclmulqdq` opcode (part of the
+ * AES-NI instructions).
+ *
+ * This implementation is available only on x86 platforms where the
+ * compiler supports the relevant intrinsic functions. Even if the
+ * compiler supports these functions, the local CPU might not support
+ * the `pclmulqdq` opcode, meaning that a call will fail with an
+ * illegal instruction exception. To safely obtain a pointer to this
+ * function when supported (or 0 otherwise), use `br_ghash_pclmul_get()`.
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+void br_ghash_pclmul(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief Obtain the `pclmul` GHASH implementation, if available.
+ *
+ * If the `pclmul` implementation was compiled in the library (depending
+ * on the compiler abilities) _and_ the local CPU appears to support the
+ * opcode, then this function will return a pointer to the
+ * `br_ghash_pclmul()` function. Otherwise, it will return `0`.
+ *
+ * \return the `pclmul` GHASH implementation, or `0`.
+ */
+br_ghash br_ghash_pclmul_get(void);
+
+/**
+ * \brief GHASH implementation using the POWER8 opcodes.
+ *
+ * This implementation is available only on POWER8 platforms (and later).
+ * To safely obtain a pointer to this function when supported (or 0
+ * otherwise), use `br_ghash_pwr8_get()`.
+ *
+ * \param y the array to update.
+ * \param h the GHASH key.
+ * \param data the input data (may be `NULL` if `len` is zero).
+ * \param len the input data length (in bytes).
+ */
+void br_ghash_pwr8(void *y, const void *h, const void *data, size_t len);
+
+/**
+ * \brief Obtain the `pwr8` GHASH implementation, if available.
+ *
+ * If the `pwr8` implementation was compiled in the library (depending
+ * on the compiler abilities) _and_ the local CPU appears to support the
+ * opcode, then this function will return a pointer to the
+ * `br_ghash_pwr8()` function. Otherwise, it will return `0`.
+ *
+ * \return the `pwr8` GHASH implementation, or `0`.
+ */
+br_ghash br_ghash_pwr8_get(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_hmac.h b/test/monniaux/BearSSL/inc/bearssl_hmac.h
new file mode 100644
index 00000000..4dc01ca3
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_hmac.h
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_HMAC_H__
+#define BR_BEARSSL_HMAC_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_hash.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_hmac.h
+ *
+ * # HMAC
+ *
+ * HMAC is initialized with a key and an underlying hash function; it
+ * then fills a "key context". That context contains the processed
+ * key.
+ *
+ * With the key context, a HMAC context can be initialized to process
+ * the input bytes and obtain the MAC output. The key context is not
+ * modified during that process, and can be reused.
+ *
+ * IMPORTANT: HMAC shall be used only with functions that have the
+ * following properties:
+ *
+ * - hash output size does not exceed 64 bytes;
+ * - hash internal state size does not exceed 64 bytes;
+ * - internal block length is a power of 2 between 16 and 256 bytes.
+ */
+
+/**
+ * \brief HMAC key context.
+ *
+ * The HMAC key context is initialised with a hash function implementation
+ * and a secret key. Contents are opaque (callers should not access them
+ * directly). The caller is responsible for allocating the context where
+ * appropriate. Context initialisation and usage incurs no dynamic
+ * allocation, so there is no release function.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ const br_hash_class *dig_vtable;
+ unsigned char ksi[64], kso[64];
+#endif
+} br_hmac_key_context;
+
+/**
+ * \brief HMAC key context initialisation.
+ *
+ * Initialise the key context with the provided key, using the hash function
+ * identified by `digest_vtable`. This supports arbitrary key lengths.
+ *
+ * \param kc HMAC key context to initialise.
+ * \param digest_vtable pointer to the hash function implementation vtable.
+ * \param key pointer to the HMAC secret key.
+ * \param key_len HMAC secret key length (in bytes).
+ */
+void br_hmac_key_init(br_hmac_key_context *kc,
+ const br_hash_class *digest_vtable, const void *key, size_t key_len);
+
+/*
+ * \brief Get the underlying hash function.
+ *
+ * This function returns a pointer to the implementation vtable of the
+ * hash function used for this HMAC key context.
+ *
+ * \param kc HMAC key context.
+ * \return the hash function implementation.
+ */
+static inline const br_hash_class *br_hmac_key_get_digest(
+ const br_hmac_key_context *kc)
+{
+ return kc->dig_vtable;
+}
+
+/**
+ * \brief HMAC computation context.
+ *
+ * The HMAC computation context maintains the state for a single HMAC
+ * computation. It is modified as input bytes are injected. The context
+ * is caller-allocated and has no release function since it does not
+ * dynamically allocate external resources. Its contents are opaque.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ br_hash_compat_context dig;
+ unsigned char kso[64];
+ size_t out_len;
+#endif
+} br_hmac_context;
+
+/**
+ * \brief HMAC computation initialisation.
+ *
+ * Initialise a HMAC context with a key context. The key context is
+ * unmodified. Relevant data from the key context is immediately copied;
+ * the key context can thus be independently reused, modified or released
+ * without impacting this HMAC computation.
+ *
+ * An explicit output length can be specified; the actual output length
+ * will be the minimum of that value and the natural HMAC output length.
+ * If `out_len` is 0, then the natural HMAC output length is selected. The
+ * "natural output length" is the output length of the underlying hash
+ * function.
+ *
+ * \param ctx HMAC context to initialise.
+ * \param kc HMAC key context (already initialised with the key).
+ * \param out_len HMAC output length (0 to select "natural length").
+ */
+void br_hmac_init(br_hmac_context *ctx,
+ const br_hmac_key_context *kc, size_t out_len);
+
+/**
+ * \brief Get the HMAC output size.
+ *
+ * The HMAC output size is the number of bytes that will actually be
+ * produced with `br_hmac_out()` with the provided context. This function
+ * MUST NOT be called on a non-initialised HMAC computation context.
+ * The returned value is the minimum of the HMAC natural length (output
+ * size of the underlying hash function) and the `out_len` parameter which
+ * was used with the last `br_hmac_init()` call on that context (if the
+ * initialisation `out_len` parameter was 0, then this function will
+ * return the HMAC natural length).
+ *
+ * \param ctx the (already initialised) HMAC computation context.
+ * \return the HMAC actual output size.
+ */
+static inline size_t
+br_hmac_size(br_hmac_context *ctx)
+{
+ return ctx->out_len;
+}
+
+/*
+ * \brief Get the underlying hash function.
+ *
+ * This function returns a pointer to the implementation vtable of the
+ * hash function used for this HMAC context.
+ *
+ * \param hc HMAC context.
+ * \return the hash function implementation.
+ */
+static inline const br_hash_class *br_hmac_get_digest(
+ const br_hmac_context *hc)
+{
+ return hc->dig.vtable;
+}
+
+/**
+ * \brief Inject some bytes in HMAC.
+ *
+ * The provided `len` bytes are injected as extra input in the HMAC
+ * computation incarnated by the `ctx` HMAC context. It is acceptable
+ * that `len` is zero, in which case `data` is ignored (and may be
+ * `NULL`) and this function does nothing.
+ */
+void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);
+
+/**
+ * \brief Compute the HMAC output.
+ *
+ * The destination buffer MUST be large enough to accommodate the result;
+ * its length is at most the "natural length" of HMAC (i.e. the output
+ * length of the underlying hash function). The context is NOT modified;
+ * further bytes may be processed. Thus, "partial HMAC" values can be
+ * efficiently obtained.
+ *
+ * Returned value is the output length (in bytes).
+ *
+ * \param ctx HMAC computation context.
+ * \param out destination buffer for the HMAC output.
+ * \return the produced value length (in bytes).
+ */
+size_t br_hmac_out(const br_hmac_context *ctx, void *out);
+
+/**
+ * \brief Constant-time HMAC computation.
+ *
+ * This function compute the HMAC output in constant time. Some extra
+ * input bytes are processed, then the output is computed. The extra
+ * input consists in the `len` bytes pointed to by `data`. The `len`
+ * parameter must lie between `min_len` and `max_len` (inclusive);
+ * `max_len` bytes are actually read from `data`. Computing time (and
+ * memory access pattern) will not depend upon the data byte contents or
+ * the value of `len`.
+ *
+ * The output is written in the `out` buffer, that MUST be large enough
+ * to receive it.
+ *
+ * The difference `max_len - min_len` MUST be less than 2<sup>30</sup>
+ * (i.e. about one gigabyte).
+ *
+ * This function computes the output properly only if the underlying
+ * hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,
+ * SHA-384 or SHA-512).
+ *
+ * The provided context is NOT modified.
+ *
+ * \param ctx the (already initialised) HMAC computation context.
+ * \param data the extra input bytes.
+ * \param len the extra input length (in bytes).
+ * \param min_len minimum extra input length (in bytes).
+ * \param max_len maximum extra input length (in bytes).
+ * \param out destination buffer for the HMAC output.
+ * \return the produced value length (in bytes).
+ */
+size_t br_hmac_outCT(const br_hmac_context *ctx,
+ const void *data, size_t len, size_t min_len, size_t max_len,
+ void *out);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_kdf.h b/test/monniaux/BearSSL/inc/bearssl_kdf.h
new file mode 100644
index 00000000..955b8436
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_kdf.h
@@ -0,0 +1,284 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_KDF_H__
+#define BR_BEARSSL_KDF_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_hash.h"
+#include "bearssl_hmac.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_kdf.h
+ *
+ * # Key Derivation Functions
+ *
+ * KDF are functions that takes a variable length input, and provide a
+ * variable length output, meant to be used to derive subkeys from a
+ * master key.
+ *
+ * ## HKDF
+ *
+ * HKDF is a KDF defined by [RFC 5869](https://tools.ietf.org/html/rfc5869).
+ * It is based on HMAC, itself using an underlying hash function. Any
+ * hash function can be used, as long as it is compatible with the rules
+ * for the HMAC implementation (i.e. output size is 64 bytes or less, hash
+ * internal state size is 64 bytes or less, and the internal block length is
+ * a power of 2 between 16 and 256 bytes). HKDF has two phases:
+ *
+ * - HKDF-Extract: the input data in ingested, along with a "salt" value.
+ *
+ * - HKDF-Expand: the output is produced, from the result of processing
+ * the input and salt, and using an extra non-secret parameter called
+ * "info".
+ *
+ * The "salt" and "info" strings are non-secret and can be empty. Their role
+ * is normally to bind the input and output, respectively, to conventional
+ * identifiers that qualifu them within the used protocol or application.
+ *
+ * The implementation defined in this file uses the following functions:
+ *
+ * - `br_hkdf_init()`: initialize an HKDF context, with a hash function,
+ * and the salt. This starts the HKDF-Extract process.
+ *
+ * - `br_hkdf_inject()`: inject more input bytes. This function may be
+ * called repeatedly if the input data is provided by chunks.
+ *
+ * - `br_hkdf_flip()`: end the HKDF-Extract process, and start the
+ * HKDF-Expand process.
+ *
+ * - `br_hkdf_produce()`: get the next bytes of output. This function
+ * may be called several times to obtain the full output by chunks.
+ * For correct HKDF processing, the same "info" string must be
+ * provided for each call.
+ *
+ * Note that the HKDF total output size (the number of bytes that
+ * HKDF-Expand is willing to produce) is limited: if the hash output size
+ * is _n_ bytes, then the maximum output size is _255*n_.
+ *
+ * ## SHAKE
+ *
+ * SHAKE is defined in
+ * [FIPS 202](https://csrc.nist.gov/publications/detail/fips/202/final)
+ * under two versions: SHAKE128 and SHAKE256, offering an alleged
+ * "security level" of 128 and 256 bits, respectively (SHAKE128 is
+ * about 20 to 25% faster than SHAKE256). SHAKE internally relies on
+ * the Keccak family of sponge functions, not on any externally provided
+ * hash function. Contrary to HKDF, SHAKE does not have a concept of
+ * either a "salt" or an "info" string. The API consists in four
+ * functions:
+ *
+ * - `br_shake_init()`: initialize a SHAKE context for a given
+ * security level.
+ *
+ * - `br_shake_inject()`: inject more input bytes. This function may be
+ * called repeatedly if the input data is provided by chunks.
+ *
+ * - `br_shake_flip()`: end the data injection process, and start the
+ * data production process.
+ *
+ * - `br_shake_produce()`: get the next bytes of output. This function
+ * may be called several times to obtain the full output by chunks.
+ */
+
+/**
+ * \brief HKDF context.
+ *
+ * The HKDF context is initialized with a hash function implementation
+ * and a salt value. Contents are opaque (callers should not access them
+ * directly). The caller is responsible for allocating the context where
+ * appropriate. Context initialisation and usage incurs no dynamic
+ * allocation, so there is no release function.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ union {
+ br_hmac_context hmac_ctx;
+ br_hmac_key_context prk_ctx;
+ } u;
+ unsigned char buf[64];
+ size_t ptr;
+ size_t dig_len;
+ unsigned chunk_num;
+#endif
+} br_hkdf_context;
+
+/**
+ * \brief HKDF context initialization.
+ *
+ * The underlying hash function and salt value are provided. Arbitrary
+ * salt lengths can be used.
+ *
+ * HKDF makes a difference between a salt of length zero, and an
+ * absent salt (the latter being equivalent to a salt consisting of
+ * bytes of value zero, of the same length as the hash function output).
+ * If `salt_len` is zero, then this function assumes that the salt is
+ * present but of length zero. To specify an _absent_ salt, use
+ * `BR_HKDF_NO_SALT` as `salt` parameter (`salt_len` is then ignored).
+ *
+ * \param hc HKDF context to initialise.
+ * \param digest_vtable pointer to the hash function implementation vtable.
+ * \param salt HKDF-Extract salt.
+ * \param salt_len HKDF-Extract salt length (in bytes).
+ */
+void br_hkdf_init(br_hkdf_context *hc, const br_hash_class *digest_vtable,
+ const void *salt, size_t salt_len);
+
+/**
+ * \brief The special "absent salt" value for HKDF.
+ */
+#define BR_HKDF_NO_SALT (&br_hkdf_no_salt)
+
+#ifndef BR_DOXYGEN_IGNORE
+extern const unsigned char br_hkdf_no_salt;
+#endif
+
+/**
+ * \brief HKDF input injection (HKDF-Extract).
+ *
+ * This function injects some more input bytes ("key material") into
+ * HKDF. This function may be called several times, after `br_hkdf_init()`
+ * but before `br_hkdf_flip()`.
+ *
+ * \param hc HKDF context.
+ * \param ikm extra input bytes.
+ * \param ikm_len number of extra input bytes.
+ */
+void br_hkdf_inject(br_hkdf_context *hc, const void *ikm, size_t ikm_len);
+
+/**
+ * \brief HKDF switch to the HKDF-Expand phase.
+ *
+ * This call terminates the HKDF-Extract process (input injection), and
+ * starts the HKDF-Expand process (output production).
+ *
+ * \param hc HKDF context.
+ */
+void br_hkdf_flip(br_hkdf_context *hc);
+
+/**
+ * \brief HKDF output production (HKDF-Expand).
+ *
+ * Produce more output bytes from the current state. This function may be
+ * called several times, but only after `br_hkdf_flip()`.
+ *
+ * Returned value is the number of actually produced bytes. The total
+ * output length is limited to 255 times the output length of the
+ * underlying hash function.
+ *
+ * \param hc HKDF context.
+ * \param info application specific information string.
+ * \param info_len application specific information string length (in bytes).
+ * \param out destination buffer for the HKDF output.
+ * \param out_len the length of the requested output (in bytes).
+ * \return the produced output length (in bytes).
+ */
+size_t br_hkdf_produce(br_hkdf_context *hc,
+ const void *info, size_t info_len, void *out, size_t out_len);
+
+/**
+ * \brief SHAKE context.
+ *
+ * The HKDF context is initialized with a "security level". The internal
+ * notion is called "capacity"; the capacity is twice the security level
+ * (for instance, SHAKE128 has capacity 256).
+ *
+ * The caller is responsible for allocating the context where
+ * appropriate. Context initialisation and usage incurs no dynamic
+ * allocation, so there is no release function.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char dbuf[200];
+ size_t dptr;
+ size_t rate;
+ uint64_t A[25];
+#endif
+} br_shake_context;
+
+/**
+ * \brief SHAKE context initialization.
+ *
+ * The context is initialized for the provided "security level".
+ * Internally, this sets the "capacity" to twice the security level;
+ * thus, for SHAKE128, the `security_level` parameter should be 128,
+ * which corresponds to a 256-bit capacity.
+ *
+ * Allowed security levels are all multiples of 32, from 32 to 768,
+ * inclusive. Larger security levels imply lower performance; levels
+ * beyond 256 bits don't make much sense. Standard levels are 128
+ * and 256 bits (for SHAKE128 and SHAKE256, respectively).
+ *
+ * \param sc SHAKE context to initialise.
+ * \param security_level security level (in bits).
+ */
+void br_shake_init(br_shake_context *sc, int security_level);
+
+/**
+ * \brief SHAKE input injection.
+ *
+ * This function injects some more input bytes ("key material") into
+ * SHAKE. This function may be called several times, after `br_shake_init()`
+ * but before `br_shake_flip()`.
+ *
+ * \param sc SHAKE context.
+ * \param data extra input bytes.
+ * \param len number of extra input bytes.
+ */
+void br_shake_inject(br_shake_context *sc, const void *data, size_t len);
+
+/**
+ * \brief SHAKE switch to production phase.
+ *
+ * This call terminates the input injection process, and starts the
+ * output production process.
+ *
+ * \param sc SHAKE context.
+ */
+void br_shake_flip(br_shake_context *hc);
+
+/**
+ * \brief SHAKE output production.
+ *
+ * Produce more output bytes from the current state. This function may be
+ * called several times, but only after `br_shake_flip()`.
+ *
+ * There is no practical limit to the number of bytes that may be produced.
+ *
+ * \param sc SHAKE context.
+ * \param out destination buffer for the SHAKE output.
+ * \param len the length of the requested output (in bytes).
+ */
+void br_shake_produce(br_shake_context *sc, void *out, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_pem.h b/test/monniaux/BearSSL/inc/bearssl_pem.h
new file mode 100644
index 00000000..8dba5827
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_pem.h
@@ -0,0 +1,294 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_PEM_H__
+#define BR_BEARSSL_PEM_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_pem.h
+ *
+ * # PEM Support
+ *
+ * PEM is a traditional encoding layer use to store binary objects (in
+ * particular X.509 certificates, and private keys) in text files. While
+ * the acronym comes from an old, defunct standard ("Privacy Enhanced
+ * Mail"), the format has been reused, with some variations, by many
+ * systems, and is a _de facto_ standard, even though it is not, actually,
+ * specified in all clarity anywhere.
+ *
+ * ## Format Details
+ *
+ * BearSSL contains a generic, streamed PEM decoder, which handles the
+ * following format:
+ *
+ * - The input source (a sequence of bytes) is assumed to be the
+ * encoding of a text file in an ASCII-compatible charset. This
+ * includes ISO-8859-1, Windows-1252, and UTF-8 encodings. Each
+ * line ends on a newline character (U+000A LINE FEED). The
+ * U+000D CARRIAGE RETURN characters are ignored, so the code
+ * accepts both Windows-style and Unix-style line endings.
+ *
+ * - Each object begins with a banner that occurs at the start of
+ * a line; the first banner characters are "`-----BEGIN `" (five
+ * dashes, the word "BEGIN", and a space). The banner matching is
+ * not case-sensitive.
+ *
+ * - The _object name_ consists in the characters that follow the
+ * banner start sequence, up to the end of the line, but without
+ * trailing dashes (in "normal" PEM, there are five trailing
+ * dashes, but this implementation is not picky about these dashes).
+ * The BearSSL decoder normalises the name characters to uppercase
+ * (for ASCII letters only) and accepts names up to 127 characters.
+ *
+ * - The object ends with a banner that again occurs at the start of
+ * a line, and starts with "`-----END `" (again case-insensitive).
+ *
+ * - Between that start and end banner, only Base64 data shall occur.
+ * Base64 converts each sequence of three bytes into four
+ * characters; the four characters are ASCII letters, digits, "`+`"
+ * or "`-`" signs, and one or two "`=`" signs may occur in the last
+ * quartet. Whitespace is ignored (whitespace is any ASCII character
+ * of code 32 or less, so control characters are whitespace) and
+ * lines may have arbitrary length; the only restriction is that the
+ * four characters of a quartet must appear on the same line (no
+ * line break inside a quartet).
+ *
+ * - A single file may contain more than one PEM object. Bytes that
+ * occur between objects are ignored.
+ *
+ *
+ * ## PEM Decoder API
+ *
+ * The PEM decoder offers a state-machine API. The caller allocates a
+ * decoder context, then injects source bytes. Source bytes are pushed
+ * with `br_pem_decoder_push()`. The decoder stops accepting bytes when
+ * it reaches an "event", which is either the start of an object, the
+ * end of an object, or a decoding error within an object.
+ *
+ * The `br_pem_decoder_event()` function is used to obtain the current
+ * event; it also clears it, thus allowing the decoder to accept more
+ * bytes. When a object start event is raised, the decoder context
+ * offers the found object name (normalised to ASCII uppercase).
+ *
+ * When an object is reached, the caller must set an appropriate callback
+ * function, which will receive (by chunks) the decoded object data.
+ *
+ * Since the decoder context makes no dynamic allocation, it requires
+ * no explicit deallocation.
+ */
+
+/**
+ * \brief PEM decoder context.
+ *
+ * Contents are opaque (they should not be accessed directly).
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ /* CPU for the T0 virtual machine. */
+ struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+ } cpu;
+ uint32_t dp_stack[32];
+ uint32_t rp_stack[32];
+ int err;
+
+ const unsigned char *hbuf;
+ size_t hlen;
+
+ void (*dest)(void *dest_ctx, const void *src, size_t len);
+ void *dest_ctx;
+
+ unsigned char event;
+ char name[128];
+ unsigned char buf[255];
+ size_t ptr;
+#endif
+} br_pem_decoder_context;
+
+/**
+ * \brief Initialise a PEM decoder structure.
+ *
+ * \param ctx decoder context to initialise.
+ */
+void br_pem_decoder_init(br_pem_decoder_context *ctx);
+
+/**
+ * \brief Push some bytes into the decoder.
+ *
+ * Returned value is the number of bytes actually consumed; this may be
+ * less than the number of provided bytes if an event is raised. When an
+ * event is raised, it must be read (with `br_pem_decoder_event()`);
+ * until the event is read, this function will return 0.
+ *
+ * \param ctx decoder context.
+ * \param data new data bytes.
+ * \param len number of new data bytes.
+ * \return the number of bytes actually received (may be less than `len`).
+ */
+size_t br_pem_decoder_push(br_pem_decoder_context *ctx,
+ const void *data, size_t len);
+
+/**
+ * \brief Set the receiver for decoded data.
+ *
+ * When an object is entered, the provided function (with opaque context
+ * pointer) will be called repeatedly with successive chunks of decoded
+ * data for that object. If `dest` is set to 0, then decoded data is
+ * simply ignored. The receiver can be set at any time, but, in practice,
+ * it should be called immediately after receiving a "start of object"
+ * event.
+ *
+ * \param ctx decoder context.
+ * \param dest callback for receiving decoded data.
+ * \param dest_ctx opaque context pointer for the `dest` callback.
+ */
+static inline void
+br_pem_decoder_setdest(br_pem_decoder_context *ctx,
+ void (*dest)(void *dest_ctx, const void *src, size_t len),
+ void *dest_ctx)
+{
+ ctx->dest = dest;
+ ctx->dest_ctx = dest_ctx;
+}
+
+/**
+ * \brief Get the last event.
+ *
+ * If an event was raised, then this function returns the event value, and
+ * also clears it, thereby allowing the decoder to proceed. If no event
+ * was raised since the last call to `br_pem_decoder_event()`, then this
+ * function returns 0.
+ *
+ * \param ctx decoder context.
+ * \return the raised event, or 0.
+ */
+int br_pem_decoder_event(br_pem_decoder_context *ctx);
+
+/**
+ * \brief Event: start of object.
+ *
+ * This event is raised when the start of a new object has been detected.
+ * The object name (normalised to uppercase) can be accessed with
+ * `br_pem_decoder_name()`.
+ */
+#define BR_PEM_BEGIN_OBJ 1
+
+/**
+ * \brief Event: end of object.
+ *
+ * This event is raised when the end of the current object is reached
+ * (normally, i.e. with no decoding error).
+ */
+#define BR_PEM_END_OBJ 2
+
+/**
+ * \brief Event: decoding error.
+ *
+ * This event is raised when decoding fails within an object.
+ * This formally closes the current object and brings the decoder back
+ * to the "out of any object" state. The offending line in the source
+ * is consumed.
+ */
+#define BR_PEM_ERROR 3
+
+/**
+ * \brief Get the name of the encountered object.
+ *
+ * The encountered object name is defined only when the "start of object"
+ * event is raised. That name is normalised to uppercase (for ASCII letters
+ * only) and does not include trailing dashes.
+ *
+ * \param ctx decoder context.
+ * \return the current object name.
+ */
+static inline const char *
+br_pem_decoder_name(br_pem_decoder_context *ctx)
+{
+ return ctx->name;
+}
+
+/**
+ * \brief Encode an object in PEM.
+ *
+ * This function encodes the provided binary object (`data`, of length `len`
+ * bytes) into PEM. The `banner` text will be included in the header and
+ * footer (e.g. use `"CERTIFICATE"` to get a `"BEGIN CERTIFICATE"` header).
+ *
+ * The length (in characters) of the PEM output is returned; that length
+ * does NOT include the terminating zero, that this function nevertheless
+ * adds. If using the returned value for allocation purposes, the allocated
+ * buffer size MUST be at least one byte larger than the returned size.
+ *
+ * If `dest` is `NULL`, then the encoding does not happen; however, the
+ * length of the encoded object is still computed and returned.
+ *
+ * The `data` pointer may be `NULL` only if `len` is zero (when encoding
+ * an object of length zero, which is not very useful), or when `dest`
+ * is `NULL` (in that case, source data bytes are ignored).
+ *
+ * Some `flags` can be specified to alter the encoding behaviour:
+ *
+ * - If `BR_PEM_LINE64` is set, then line-breaking will occur after
+ * every 64 characters of output, instead of the default of 76.
+ *
+ * - If `BR_PEM_CRLF` is set, then end-of-line sequence will use
+ * CR+LF instead of a single LF.
+ *
+ * The `data` and `dest` buffers may overlap, in which case the source
+ * binary data is destroyed in the process. Note that the PEM-encoded output
+ * is always larger than the source binary.
+ *
+ * \param dest the destination buffer (or `NULL`).
+ * \param data the source buffer (can be `NULL` in some cases).
+ * \param len the source length (in bytes).
+ * \param banner the PEM banner expression.
+ * \param flags the behavioural flags.
+ * \return the PEM object length (in characters), EXCLUDING the final zero.
+ */
+size_t br_pem_encode(void *dest, const void *data, size_t len,
+ const char *banner, unsigned flags);
+
+/**
+ * \brief PEM encoding flag: split lines at 64 characters.
+ */
+#define BR_PEM_LINE64 0x0001
+
+/**
+ * \brief PEM encoding flag: use CR+LF line endings.
+ */
+#define BR_PEM_CRLF 0x0002
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_prf.h b/test/monniaux/BearSSL/inc/bearssl_prf.h
new file mode 100644
index 00000000..fdf608c8
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_prf.h
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_PRF_H__
+#define BR_BEARSSL_PRF_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_prf.h
+ *
+ * # The TLS PRF
+ *
+ * The "PRF" is the pseudorandom function used internally during the
+ * SSL/TLS handshake, notably to expand negotiated shared secrets into
+ * the symmetric encryption keys that will be used to process the
+ * application data.
+ *
+ * TLS 1.0 and 1.1 define a PRF that is based on both MD5 and SHA-1. This
+ * is implemented by the `br_tls10_prf()` function.
+ *
+ * TLS 1.2 redefines the PRF, using an explicit hash function. The
+ * `br_tls12_sha256_prf()` and `br_tls12_sha384_prf()` functions apply that
+ * PRF with, respectively, SHA-256 and SHA-384. Most standard cipher suites
+ * rely on the SHA-256 based PRF, but some use SHA-384.
+ *
+ * The PRF always uses as input three parameters: a "secret" (some
+ * bytes), a "label" (ASCII string), and a "seed" (again some bytes). An
+ * arbitrary output length can be produced. The "seed" is provided as an
+ * arbitrary number of binary chunks, that gets internally concatenated.
+ */
+
+/**
+ * \brief Type for a seed chunk.
+ *
+ * Each chunk may have an arbitrary length, and may be empty (no byte at
+ * all). If the chunk length is zero, then the pointer to the chunk data
+ * may be `NULL`.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to the chunk data.
+ */
+ const void *data;
+
+ /**
+ * \brief Chunk length (in bytes).
+ */
+ size_t len;
+} br_tls_prf_seed_chunk;
+
+/**
+ * \brief PRF implementation for TLS 1.0 and 1.1.
+ *
+ * This PRF is the one specified by TLS 1.0 and 1.1. It internally uses
+ * MD5 and SHA-1.
+ *
+ * \param dst destination buffer.
+ * \param len output length (in bytes).
+ * \param secret secret value (key) for this computation.
+ * \param secret_len length of "secret" (in bytes).
+ * \param label PRF label (zero-terminated ASCII string).
+ * \param seed_num number of seed chunks.
+ * \param seed seed chnks for this computation (usually non-secret).
+ */
+void br_tls10_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed);
+
+/**
+ * \brief PRF implementation for TLS 1.2, with SHA-256.
+ *
+ * This PRF is the one specified by TLS 1.2, when the underlying hash
+ * function is SHA-256.
+ *
+ * \param dst destination buffer.
+ * \param len output length (in bytes).
+ * \param secret secret value (key) for this computation.
+ * \param secret_len length of "secret" (in bytes).
+ * \param label PRF label (zero-terminated ASCII string).
+ * \param seed_num number of seed chunks.
+ * \param seed seed chnks for this computation (usually non-secret).
+ */
+void br_tls12_sha256_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed);
+
+/**
+ * \brief PRF implementation for TLS 1.2, with SHA-384.
+ *
+ * This PRF is the one specified by TLS 1.2, when the underlying hash
+ * function is SHA-384.
+ *
+ * \param dst destination buffer.
+ * \param len output length (in bytes).
+ * \param secret secret value (key) for this computation.
+ * \param secret_len length of "secret" (in bytes).
+ * \param label PRF label (zero-terminated ASCII string).
+ * \param seed_num number of seed chunks.
+ * \param seed seed chnks for this computation (usually non-secret).
+ */
+void br_tls12_sha384_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed);
+
+/**
+ * brief A convenient type name for a PRF implementation.
+ *
+ * \param dst destination buffer.
+ * \param len output length (in bytes).
+ * \param secret secret value (key) for this computation.
+ * \param secret_len length of "secret" (in bytes).
+ * \param label PRF label (zero-terminated ASCII string).
+ * \param seed_num number of seed chunks.
+ * \param seed seed chnks for this computation (usually non-secret).
+ */
+typedef void (*br_tls_prf_impl)(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_rand.h b/test/monniaux/BearSSL/inc/bearssl_rand.h
new file mode 100644
index 00000000..0a9f544f
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_rand.h
@@ -0,0 +1,397 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_RAND_H__
+#define BR_BEARSSL_RAND_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_block.h"
+#include "bearssl_hash.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_rand.h
+ *
+ * # Pseudo-Random Generators
+ *
+ * A PRNG is a state-based engine that outputs pseudo-random bytes on
+ * demand. It is initialized with an initial seed, and additional seed
+ * bytes can be added afterwards. Bytes produced depend on the seeds and
+ * also on the exact sequence of calls (including sizes requested for
+ * each call).
+ *
+ *
+ * ## Procedural and OOP API
+ *
+ * For the PRNG of name "`xxx`", two API are provided. The _procedural_
+ * API defined a context structure `br_xxx_context` and three functions:
+ *
+ * - `br_xxx_init()`
+ *
+ * Initialise the context with an initial seed.
+ *
+ * - `br_xxx_generate()`
+ *
+ * Produce some pseudo-random bytes.
+ *
+ * - `br_xxx_update()`
+ *
+ * Inject some additional seed.
+ *
+ * The initialisation function sets the first context field (`vtable`)
+ * to a pointer to the vtable that supports the OOP API. The OOP API
+ * provides access to the same functions through function pointers,
+ * named `init()`, `generate()` and `update()`.
+ *
+ * Note that the context initialisation method may accept additional
+ * parameters, provided as a 'const void *' pointer at API level. These
+ * additional parameters depend on the implemented PRNG.
+ *
+ *
+ * ## HMAC_DRBG
+ *
+ * HMAC_DRBG is defined in [NIST SP 800-90A Revision
+ * 1](http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf).
+ * It uses HMAC repeatedly, over some configurable underlying hash
+ * function. In BearSSL, it is implemented under the "`hmac_drbg`" name.
+ * The "extra parameters" pointer for context initialisation should be
+ * set to a pointer to the vtable for the underlying hash function (e.g.
+ * pointer to `br_sha256_vtable` to use HMAC_DRBG with SHA-256).
+ *
+ * According to the NIST standard, each request shall produce up to
+ * 2<sup>19</sup> bits (i.e. 64 kB of data); moreover, the context shall
+ * be reseeded at least once every 2<sup>48</sup> requests. This
+ * implementation does not maintain the reseed counter (the threshold is
+ * too high to be reached in practice) and does not object to producing
+ * more than 64 kB in a single request; thus, the code cannot fail,
+ * which corresponds to the fact that the API has no room for error
+ * codes. However, this implies that requesting more than 64 kB in one
+ * `generate()` request, or making more than 2<sup>48</sup> requests
+ * without reseeding, is formally out of NIST specification. There is
+ * no currently known security penalty for exceeding the NIST limits,
+ * and, in any case, HMAC_DRBG usage in implementing SSL/TLS always
+ * stays much below these thresholds.
+ *
+ *
+ * ## AESCTR_DRBG
+ *
+ * AESCTR_DRBG is a custom PRNG based on AES-128 in CTR mode. This is
+ * meant to be used only in situations where you are desperate for
+ * speed, and have an hardware-optimized AES/CTR implementation. Whether
+ * this will yield perceptible improvements depends on what you use the
+ * pseudorandom bytes for, and how many you want; for instance, RSA key
+ * pair generation uses a substantial amount of randomness, and using
+ * AESCTR_DRBG instead of HMAC_DRBG yields a 15 to 20% increase in key
+ * generation speed on a recent x86 CPU (Intel Core i7-6567U at 3.30 GHz).
+ *
+ * Internally, it uses CTR mode with successive counter values, starting
+ * at zero (counter value expressed over 128 bits, big-endian convention).
+ * The counter is not allowed to reach 32768; thus, every 32768*16 bytes
+ * at most, the `update()` function is run (on an empty seed, if none is
+ * provided). The `update()` function computes the new AES-128 key by
+ * applying a custom hash function to the concatenation of a state-dependent
+ * word (encryption of an all-one block with the current key) and the new
+ * seed. The custom hash function uses Hirose's construction over AES-256;
+ * see the comments in `aesctr_drbg.c` for details.
+ *
+ * This DRBG does not follow an existing standard, and thus should be
+ * considered as inadequate for production use until it has been properly
+ * analysed.
+ */
+
+/**
+ * \brief Class type for PRNG implementations.
+ *
+ * A `br_prng_class` instance references the methods implementing a PRNG.
+ * Constant instances of this structure are defined for each implemented
+ * PRNG. Such instances are also called "vtables".
+ */
+typedef struct br_prng_class_ br_prng_class;
+struct br_prng_class_ {
+ /**
+ * \brief Size (in bytes) of the context structure appropriate for
+ * running this PRNG.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Initialisation method.
+ *
+ * The context to initialise is provided as a pointer to its
+ * first field (the vtable pointer); this function sets that
+ * first field to a pointer to the vtable.
+ *
+ * The extra parameters depend on the implementation; each
+ * implementation defines what kind of extra parameters it
+ * expects (if any).
+ *
+ * Requirements on the initial seed depend on the implemented
+ * PRNG.
+ *
+ * \param ctx PRNG context to initialise.
+ * \param params extra parameters for the PRNG.
+ * \param seed initial seed.
+ * \param seed_len initial seed length (in bytes).
+ */
+ void (*init)(const br_prng_class **ctx, const void *params,
+ const void *seed, size_t seed_len);
+
+ /**
+ * \brief Random bytes generation.
+ *
+ * This method produces `len` pseudorandom bytes, in the `out`
+ * buffer. The context is updated accordingly.
+ *
+ * \param ctx PRNG context.
+ * \param out output buffer.
+ * \param len number of pseudorandom bytes to produce.
+ */
+ void (*generate)(const br_prng_class **ctx, void *out, size_t len);
+
+ /**
+ * \brief Inject additional seed bytes.
+ *
+ * The provided seed bytes are added into the PRNG internal
+ * entropy pool.
+ *
+ * \param ctx PRNG context.
+ * \param seed additional seed.
+ * \param seed_len additional seed length (in bytes).
+ */
+ void (*update)(const br_prng_class **ctx,
+ const void *seed, size_t seed_len);
+};
+
+/**
+ * \brief Context for HMAC_DRBG.
+ *
+ * The context contents are opaque, except the first field, which
+ * supports OOP.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to the vtable.
+ *
+ * This field is set with the initialisation method/function.
+ */
+ const br_prng_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char K[64];
+ unsigned char V[64];
+ const br_hash_class *digest_class;
+#endif
+} br_hmac_drbg_context;
+
+/**
+ * \brief Statically allocated, constant vtable for HMAC_DRBG.
+ */
+extern const br_prng_class br_hmac_drbg_vtable;
+
+/**
+ * \brief HMAC_DRBG initialisation.
+ *
+ * The context to initialise is provided as a pointer to its first field
+ * (the vtable pointer); this function sets that first field to a
+ * pointer to the vtable.
+ *
+ * The `seed` value is what is called, in NIST terminology, the
+ * concatenation of the "seed", "nonce" and "personalization string", in
+ * that order.
+ *
+ * The `digest_class` parameter defines the underlying hash function.
+ * Formally, the NIST standard specifies that the hash function shall
+ * be only SHA-1 or one of the SHA-2 functions. This implementation also
+ * works with any other implemented hash function (such as MD5), but
+ * this is non-standard and therefore not recommended.
+ *
+ * \param ctx HMAC_DRBG context to initialise.
+ * \param digest_class vtable for the underlying hash function.
+ * \param seed initial seed.
+ * \param seed_len initial seed length (in bytes).
+ */
+void br_hmac_drbg_init(br_hmac_drbg_context *ctx,
+ const br_hash_class *digest_class, const void *seed, size_t seed_len);
+
+/**
+ * \brief Random bytes generation with HMAC_DRBG.
+ *
+ * This method produces `len` pseudorandom bytes, in the `out`
+ * buffer. The context is updated accordingly. Formally, requesting
+ * more than 65536 bytes in one request falls out of specification
+ * limits (but it won't fail).
+ *
+ * \param ctx HMAC_DRBG context.
+ * \param out output buffer.
+ * \param len number of pseudorandom bytes to produce.
+ */
+void br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len);
+
+/**
+ * \brief Inject additional seed bytes in HMAC_DRBG.
+ *
+ * The provided seed bytes are added into the HMAC_DRBG internal
+ * entropy pool. The process does not _replace_ existing entropy,
+ * thus pushing non-random bytes (i.e. bytes which are known to the
+ * attackers) does not degrade the overall quality of generated bytes.
+ *
+ * \param ctx HMAC_DRBG context.
+ * \param seed additional seed.
+ * \param seed_len additional seed length (in bytes).
+ */
+void br_hmac_drbg_update(br_hmac_drbg_context *ctx,
+ const void *seed, size_t seed_len);
+
+/**
+ * \brief Get the hash function implementation used by a given instance of
+ * HMAC_DRBG.
+ *
+ * This calls MUST NOT be performed on a context which was not
+ * previously initialised.
+ *
+ * \param ctx HMAC_DRBG context.
+ * \return the hash function vtable.
+ */
+static inline const br_hash_class *
+br_hmac_drbg_get_hash(const br_hmac_drbg_context *ctx)
+{
+ return ctx->digest_class;
+}
+
+/**
+ * \brief Type for a provider of entropy seeds.
+ *
+ * A "seeder" is a function that is able to obtain random values from
+ * some source and inject them as entropy seed in a PRNG. A seeder
+ * shall guarantee that the total entropy of the injected seed is large
+ * enough to seed a PRNG for purposes of cryptographic key generation
+ * (i.e. at least 128 bits).
+ *
+ * A seeder may report a failure to obtain adequate entropy. Seeders
+ * shall endeavour to fix themselves transient errors by trying again;
+ * thus, callers may consider reported errors as permanent.
+ *
+ * \param ctx PRNG context to seed.
+ * \return 1 on success, 0 on error.
+ */
+typedef int (*br_prng_seeder)(const br_prng_class **ctx);
+
+/**
+ * \brief Get a seeder backed by the operating system or hardware.
+ *
+ * Get a seeder that feeds on RNG facilities provided by the current
+ * operating system or hardware. If no such facility is known, then 0
+ * is returned.
+ *
+ * If `name` is not `NULL`, then `*name` is set to a symbolic string
+ * that identifies the seeder implementation. If no seeder is returned
+ * and `name` is not `NULL`, then `*name` is set to a pointer to the
+ * constant string `"none"`.
+ *
+ * \param name receiver for seeder name, or `NULL`.
+ * \return the system seeder, if available, or 0.
+ */
+br_prng_seeder br_prng_seeder_system(const char **name);
+
+/**
+ * \brief Context for AESCTR_DRBG.
+ *
+ * The context contents are opaque, except the first field, which
+ * supports OOP.
+ */
+typedef struct {
+ /**
+ * \brief Pointer to the vtable.
+ *
+ * This field is set with the initialisation method/function.
+ */
+ const br_prng_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ br_aes_gen_ctr_keys sk;
+ uint32_t cc;
+#endif
+} br_aesctr_drbg_context;
+
+/**
+ * \brief Statically allocated, constant vtable for AESCTR_DRBG.
+ */
+extern const br_prng_class br_aesctr_drbg_vtable;
+
+/**
+ * \brief AESCTR_DRBG initialisation.
+ *
+ * The context to initialise is provided as a pointer to its first field
+ * (the vtable pointer); this function sets that first field to a
+ * pointer to the vtable.
+ *
+ * The internal AES key is first set to the all-zero key; then, the
+ * `br_aesctr_drbg_update()` function is called with the provided `seed`.
+ * The call is performed even if the seed length (`seed_len`) is zero.
+ *
+ * The `aesctr` parameter defines the underlying AES/CTR implementation.
+ *
+ * \param ctx AESCTR_DRBG context to initialise.
+ * \param aesctr vtable for the AES/CTR implementation.
+ * \param seed initial seed (can be `NULL` if `seed_len` is zero).
+ * \param seed_len initial seed length (in bytes).
+ */
+void br_aesctr_drbg_init(br_aesctr_drbg_context *ctx,
+ const br_block_ctr_class *aesctr, const void *seed, size_t seed_len);
+
+/**
+ * \brief Random bytes generation with AESCTR_DRBG.
+ *
+ * This method produces `len` pseudorandom bytes, in the `out`
+ * buffer. The context is updated accordingly.
+ *
+ * \param ctx AESCTR_DRBG context.
+ * \param out output buffer.
+ * \param len number of pseudorandom bytes to produce.
+ */
+void br_aesctr_drbg_generate(br_aesctr_drbg_context *ctx,
+ void *out, size_t len);
+
+/**
+ * \brief Inject additional seed bytes in AESCTR_DRBG.
+ *
+ * The provided seed bytes are added into the AESCTR_DRBG internal
+ * entropy pool. The process does not _replace_ existing entropy,
+ * thus pushing non-random bytes (i.e. bytes which are known to the
+ * attackers) does not degrade the overall quality of generated bytes.
+ *
+ * \param ctx AESCTR_DRBG context.
+ * \param seed additional seed.
+ * \param seed_len additional seed length (in bytes).
+ */
+void br_aesctr_drbg_update(br_aesctr_drbg_context *ctx,
+ const void *seed, size_t seed_len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_rsa.h b/test/monniaux/BearSSL/inc/bearssl_rsa.h
new file mode 100644
index 00000000..0a069fd3
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_rsa.h
@@ -0,0 +1,1655 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_RSA_H__
+#define BR_BEARSSL_RSA_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_hash.h"
+#include "bearssl_rand.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_rsa.h
+ *
+ * # RSA
+ *
+ * This file documents the RSA implementations provided with BearSSL.
+ * Note that the SSL engine accesses these implementations through a
+ * configurable API, so it is possible to, for instance, run a SSL
+ * server which uses a RSA engine which is not based on this code.
+ *
+ * ## Key Elements
+ *
+ * RSA public and private keys consist in lists of big integers. All
+ * such integers are represented with big-endian unsigned notation:
+ * first byte is the most significant, and the value is positive (so
+ * there is no dedicated "sign bit"). Public and private key structures
+ * thus contain, for each such integer, a pointer to the first value byte
+ * (`unsigned char *`), and a length (`size_t`) which is the number of
+ * relevant bytes. As a general rule, minimal-length encoding is not
+ * enforced: values may have extra leading bytes of value 0.
+ *
+ * RSA public keys consist in two integers:
+ *
+ * - the modulus (`n`);
+ * - the public exponent (`e`).
+ *
+ * RSA private keys, as defined in
+ * [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:
+ *
+ * - the modulus (`n`);
+ * - the public exponent (`e`);
+ * - the private exponent (`d`);
+ * - the first prime factor (`p`);
+ * - the second prime factor (`q`);
+ * - the first reduced exponent (`dp`, which is `d` modulo `p-1`);
+ * - the second reduced exponent (`dq`, which is `d` modulo `q-1`);
+ * - the CRT coefficient (`iq`, the inverse of `q` modulo `p`).
+ *
+ * However, the implementations defined in BearSSL use only five of
+ * these integers: `p`, `q`, `dp`, `dq` and `iq`.
+ *
+ * ## Security Features and Limitations
+ *
+ * The implementations contained in BearSSL have the following limitations
+ * and features:
+ *
+ * - They are constant-time. This means that the execution time and
+ * memory access pattern may depend on the _lengths_ of the private
+ * key components, but not on their value, nor on the value of
+ * the operand. Note that this property is not achieved through
+ * random masking, but "true" constant-time code.
+ *
+ * - They support only private keys with two prime factors. RSA private
+ * keys with three or more prime factors are nominally supported, but
+ * rarely used; they may offer faster operations, at the expense of
+ * more code and potentially a reduction in security if there are
+ * "too many" prime factors.
+ *
+ * - The public exponent may have arbitrary length. Of course, it is
+ * a good idea to keep public exponents small, so that public key
+ * operations are fast; but, contrary to some widely deployed
+ * implementations, BearSSL has no problem with public exponents
+ * longer than 32 bits.
+ *
+ * - The two prime factors of the modulus need not have the same length
+ * (but severely imbalanced factor lengths might reduce security).
+ * Similarly, there is no requirement that the first factor (`p`)
+ * be greater than the second factor (`q`).
+ *
+ * - Prime factors and modulus must be smaller than a compile-time limit.
+ * This is made necessary by the use of fixed-size stack buffers, and
+ * the limit has been adjusted to keep stack usage under 2 kB for the
+ * RSA operations. Currently, the maximum modulus size is 4096 bits,
+ * and the maximum prime factor size is 2080 bits.
+ *
+ * - The RSA functions themselves do not enforce lower size limits,
+ * except that which is absolutely necessary for the operation to
+ * mathematically make sense (e.g. a PKCS#1 v1.5 signature with
+ * SHA-1 requires a modulus of at least 361 bits). It is up to users
+ * of this code to enforce size limitations when appropriate (e.g.
+ * the X.509 validation engine, by default, rejects RSA keys of
+ * less than 1017 bits).
+ *
+ * - Within the size constraints expressed above, arbitrary bit lengths
+ * are supported. There is no requirement that prime factors or
+ * modulus have a size multiple of 8 or 16.
+ *
+ * - When verifying PKCS#1 v1.5 signatures, both variants of the hash
+ * function identifying header (with and without the ASN.1 NULL) are
+ * supported. When producing such signatures, the variant with the
+ * ASN.1 NULL is used.
+ *
+ * ## Implementations
+ *
+ * Three RSA implementations are included:
+ *
+ * - The **i32** implementation internally represents big integers
+ * as arrays of 32-bit integers. It is perfunctory and portable,
+ * but not very efficient.
+ *
+ * - The **i31** implementation uses 32-bit integers, each containing
+ * 31 bits worth of integer data. The i31 implementation is somewhat
+ * faster than the i32 implementation (the reduced integer size makes
+ * carry propagation easier) for a similar code footprint, but uses
+ * very slightly larger stack buffers (about 4% bigger).
+ *
+ * - The **i62** implementation is similar to the i31 implementation,
+ * except that it internally leverages the 64x64->128 multiplication
+ * opcode. This implementation is available only on architectures
+ * where such an opcode exists. It is much faster than i31.
+ *
+ * - The **i15** implementation uses 16-bit integers, each containing
+ * 15 bits worth of integer data. Multiplication results fit on
+ * 32 bits, so this won't use the "widening" multiplication routine
+ * on ARM Cortex M0/M0+, for much better performance and constant-time
+ * execution.
+ */
+
+/**
+ * \brief RSA public key.
+ *
+ * The structure references the modulus and the public exponent. Both
+ * integers use unsigned big-endian representation; extra leading bytes
+ * of value 0 are allowed.
+ */
+typedef struct {
+ /** \brief Modulus. */
+ unsigned char *n;
+ /** \brief Modulus length (in bytes). */
+ size_t nlen;
+ /** \brief Public exponent. */
+ unsigned char *e;
+ /** \brief Public exponent length (in bytes). */
+ size_t elen;
+} br_rsa_public_key;
+
+/**
+ * \brief RSA private key.
+ *
+ * The structure references the private factors, reduced private
+ * exponents, and CRT coefficient. It also contains the bit length of
+ * the modulus. The big integers use unsigned big-endian representation;
+ * extra leading bytes of value 0 are allowed. However, the modulus bit
+ * length (`n_bitlen`) MUST be exact.
+ */
+typedef struct {
+ /** \brief Modulus bit length (in bits, exact value). */
+ uint32_t n_bitlen;
+ /** \brief First prime factor. */
+ unsigned char *p;
+ /** \brief First prime factor length (in bytes). */
+ size_t plen;
+ /** \brief Second prime factor. */
+ unsigned char *q;
+ /** \brief Second prime factor length (in bytes). */
+ size_t qlen;
+ /** \brief First reduced private exponent. */
+ unsigned char *dp;
+ /** \brief First reduced private exponent length (in bytes). */
+ size_t dplen;
+ /** \brief Second reduced private exponent. */
+ unsigned char *dq;
+ /** \brief Second reduced private exponent length (in bytes). */
+ size_t dqlen;
+ /** \brief CRT coefficient. */
+ unsigned char *iq;
+ /** \brief CRT coefficient length (in bytes). */
+ size_t iqlen;
+} br_rsa_private_key;
+
+/**
+ * \brief Type for a RSA public key engine.
+ *
+ * The public key engine performs the modular exponentiation of the
+ * provided value with the public exponent. The value is modified in
+ * place.
+ *
+ * The value length (`xlen`) is verified to have _exactly_ the same
+ * length as the modulus (actual modulus length, without extra leading
+ * zeros in the modulus representation in memory). If the length does
+ * not match, then this function returns 0 and `x[]` is unmodified.
+ *
+ * It `xlen` is correct, then `x[]` is modified. Returned value is 1
+ * on success, 0 on error. Error conditions include an oversized `x[]`
+ * (the array has the same length as the modulus, but the numerical value
+ * is not lower than the modulus) and an invalid modulus (e.g. an even
+ * integer). If an error is reported, then the new contents of `x[]` are
+ * unspecified.
+ *
+ * \param x operand to exponentiate.
+ * \param xlen length of the operand (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_public)(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk);
+
+/**
+ * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
+ *
+ * Parameters are:
+ *
+ * - The signature itself. The provided array is NOT modified.
+ *
+ * - The encoded OID for the hash function. The provided array must begin
+ * with a single byte that contains the length of the OID value (in
+ * bytes), followed by exactly that many bytes. This parameter may
+ * also be `NULL`, in which case the raw hash value should be used
+ * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
+ * to TLS-1.1, with a 36-byte hash value).
+ *
+ * - The hash output length, in bytes.
+ *
+ * - The public key.
+ *
+ * - An output buffer for the hash value. The caller must still compare
+ * it with the hash of the data over which the signature is computed.
+ *
+ * **Constraints:**
+ *
+ * - Hash length MUST be no more than 64 bytes.
+ *
+ * - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`
+ * must have a value in the 0..32 range, inclusive).
+ *
+ * This function verifies that the signature length (`xlen`) matches the
+ * modulus length (this function returns 0 on mismatch). If the modulus
+ * size exceeds the maximum supported RSA size, then the function also
+ * returns 0.
+ *
+ * Returned value is 1 on success, 0 on error.
+ *
+ * Implementations of this type need not be constant-time.
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash_len expected hash value length (in bytes).
+ * \param pk RSA public key.
+ * \param hash_out output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_pkcs1_vrfy)(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out);
+
+/**
+ * \brief Type for a RSA signature verification engine (PSS).
+ *
+ * Parameters are:
+ *
+ * - The signature itself. The provided array is NOT modified.
+ *
+ * - The hash function which was used to hash the message.
+ *
+ * - The hash function to use with MGF1 within the PSS padding. This
+ * is not necessarily the same hash function as the one which was
+ * used to hash the signed message.
+ *
+ * - The hashed message (as an array of bytes).
+ *
+ * - The PSS salt length (in bytes).
+ *
+ * - The public key.
+ *
+ * **Constraints:**
+ *
+ * - Hash message length MUST be no more than 64 bytes.
+ *
+ * Note that, contrary to PKCS#1 v1.5 signature, the hash value of the
+ * signed data cannot be extracted from the signature; it must be
+ * provided to the verification function.
+ *
+ * This function verifies that the signature length (`xlen`) matches the
+ * modulus length (this function returns 0 on mismatch). If the modulus
+ * size exceeds the maximum supported RSA size, then the function also
+ * returns 0.
+ *
+ * Returned value is 1 on success, 0 on error.
+ *
+ * Implementations of this type need not be constant-time.
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hf_data hash function applied on the message.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hash value of the signed message.
+ * \param salt_len PSS salt length (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_pss_vrfy)(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk);
+
+/**
+ * \brief Type for a RSA encryption engine (OAEP).
+ *
+ * Parameters are:
+ *
+ * - A source of random bytes. The source must be already initialized.
+ *
+ * - A hash function, used internally with the mask generation function
+ * (MGF1).
+ *
+ * - A label. The `label` pointer may be `NULL` if `label_len` is zero
+ * (an empty label, which is the default in PKCS#1 v2.2).
+ *
+ * - The public key.
+ *
+ * - The destination buffer. Its maximum length (in bytes) is provided;
+ * if that length is lower than the public key length, then an error
+ * is reported.
+ *
+ * - The source message.
+ *
+ * The encrypted message output has exactly the same length as the modulus
+ * (mathematical length, in bytes, not counting extra leading zeros in the
+ * modulus representation in the public key).
+ *
+ * The source message (`src`, length `src_len`) may overlap with the
+ * destination buffer (`dst`, length `dst_max_len`).
+ *
+ * This function returns the actual encrypted message length, in bytes;
+ * on error, zero is returned. An error is reported if the output buffer
+ * is not large enough, or the public is invalid, or the public key
+ * modulus exceeds the maximum supported RSA size.
+ *
+ * \param rnd source of random bytes.
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param pk RSA public key.
+ * \param dst destination buffer.
+ * \param dst_max_len destination buffer length (maximum encrypted data size).
+ * \param src message to encrypt.
+ * \param src_len source message length (in bytes).
+ * \return encrypted message length (in bytes), or 0 on error.
+ */
+typedef size_t (*br_rsa_oaep_encrypt)(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len);
+
+/**
+ * \brief Type for a RSA private key engine.
+ *
+ * The `x[]` buffer is modified in place, and its length is inferred from
+ * the modulus length (`x[]` is assumed to have a length of
+ * `(sk->n_bitlen+7)/8` bytes).
+ *
+ * Returned value is 1 on success, 0 on error.
+ *
+ * \param x operand to exponentiate.
+ * \param sk RSA private key.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_private)(unsigned char *x,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
+ *
+ * Parameters are:
+ *
+ * - The encoded OID for the hash function. The provided array must begin
+ * with a single byte that contains the length of the OID value (in
+ * bytes), followed by exactly that many bytes. This parameter may
+ * also be `NULL`, in which case the raw hash value should be used
+ * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
+ * to TLS-1.1, with a 36-byte hash value).
+ *
+ * - The hash value computes over the data to sign (its length is
+ * expressed in bytes).
+ *
+ * - The RSA private key.
+ *
+ * - The output buffer, that receives the signature.
+ *
+ * Returned value is 1 on success, 0 on error. Error conditions include
+ * a too small modulus for the provided hash OID and value, or some
+ * invalid key parameters. The signature length is exactly
+ * `(sk->n_bitlen+7)/8` bytes.
+ *
+ * This function is expected to be constant-time with regards to the
+ * private key bytes (lengths of the modulus and the individual factors
+ * may leak, though) and to the hashed data.
+ *
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash hash value.
+ * \param hash_len hash value length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_pkcs1_sign)(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief Type for a RSA signature generation engine (PSS).
+ *
+ * Parameters are:
+ *
+ * - An initialized PRNG for salt generation. If the salt length is
+ * zero (`salt_len` parameter), then the PRNG is optional (this is
+ * not the typical case, as the security proof of RSA/PSS is
+ * tighter when a non-empty salt is used).
+ *
+ * - The hash function which was used to hash the message.
+ *
+ * - The hash function to use with MGF1 within the PSS padding. This
+ * is not necessarily the same function as the one used to hash the
+ * message.
+ *
+ * - The hashed message.
+ *
+ * - The salt length, in bytes.
+ *
+ * - The RSA private key.
+ *
+ * - The output buffer, that receives the signature.
+ *
+ * Returned value is 1 on success, 0 on error. Error conditions include
+ * a too small modulus for the provided hash and salt lengths, or some
+ * invalid key parameters. The signature length is exactly
+ * `(sk->n_bitlen+7)/8` bytes.
+ *
+ * This function is expected to be constant-time with regards to the
+ * private key bytes (lengths of the modulus and the individual factors
+ * may leak, though) and to the hashed data.
+ *
+ * \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).
+ * \param hf_data hash function used to hash the signed data.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hashed message.
+ * \param salt_len salt length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_pss_sign)(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash_value, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief Encoded OID for SHA-1 (in RSA PKCS#1 signatures).
+ */
+#define BR_HASH_OID_SHA1 \
+ ((const unsigned char *)"\x05\x2B\x0E\x03\x02\x1A")
+
+/**
+ * \brief Encoded OID for SHA-224 (in RSA PKCS#1 signatures).
+ */
+#define BR_HASH_OID_SHA224 \
+ ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")
+
+/**
+ * \brief Encoded OID for SHA-256 (in RSA PKCS#1 signatures).
+ */
+#define BR_HASH_OID_SHA256 \
+ ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
+
+/**
+ * \brief Encoded OID for SHA-384 (in RSA PKCS#1 signatures).
+ */
+#define BR_HASH_OID_SHA384 \
+ ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")
+
+/**
+ * \brief Encoded OID for SHA-512 (in RSA PKCS#1 signatures).
+ */
+#define BR_HASH_OID_SHA512 \
+ ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")
+
+/**
+ * \brief Type for a RSA decryption engine (OAEP).
+ *
+ * Parameters are:
+ *
+ * - A hash function, used internally with the mask generation function
+ * (MGF1).
+ *
+ * - A label. The `label` pointer may be `NULL` if `label_len` is zero
+ * (an empty label, which is the default in PKCS#1 v2.2).
+ *
+ * - The private key.
+ *
+ * - The source and destination buffer. The buffer initially contains
+ * the encrypted message; the buffer contents are altered, and the
+ * decrypted message is written at the start of that buffer
+ * (decrypted message is always shorter than the encrypted message).
+ *
+ * If decryption fails in any way, then `*len` is unmodified, and the
+ * function returns 0. Otherwise, `*len` is set to the decrypted message
+ * length, and 1 is returned. The implementation is responsible for
+ * checking that the input message length matches the key modulus length,
+ * and that the padding is correct.
+ *
+ * Implementations MUST use constant-time check of the validity of the
+ * OAEP padding, at least until the leading byte and hash value have
+ * been checked. Whether overall decryption worked, and the length of
+ * the decrypted message, may leak.
+ *
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len encrypted/decrypted message length.
+ * \return 1 on success, 0 on error.
+ */
+typedef uint32_t (*br_rsa_oaep_decrypt)(
+ const br_hash_class *dig, const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len);
+
+/*
+ * RSA "i32" engine. Integers are internally represented as arrays of
+ * 32-bit integers, and the core multiplication primitive is the
+ * 32x32->64 multiplication.
+ */
+
+/**
+ * \brief RSA public key engine "i32".
+ *
+ * \see br_rsa_public
+ *
+ * \param x operand to exponentiate.
+ * \param xlen length of the operand (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA signature verification engine "i32" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash_len expected hash value length (in bytes).
+ * \param pk RSA public key.
+ * \param hash_out output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out);
+
+/**
+ * \brief RSA signature verification engine "i32" (PSS signatures).
+ *
+ * \see br_rsa_pss_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hf_data hash function applied on the message.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hash value of the signed message.
+ * \param salt_len PSS salt length (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA private key engine "i32".
+ *
+ * \see br_rsa_private
+ *
+ * \param x operand to exponentiate.
+ * \param sk RSA private key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_private(unsigned char *x,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief RSA signature generation engine "i32" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_sign
+ *
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash hash value.
+ * \param hash_len hash value length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief RSA signature generation engine "i32" (PSS signatures).
+ *
+ * \see br_rsa_pss_sign
+ *
+ * \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).
+ * \param hf_data hash function used to hash the signed data.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hashed message.
+ * \param salt_len salt length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash_value, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/*
+ * RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
+ * word. This uses slightly more stack space (about 4% more) and code
+ * space, but it quite faster.
+ */
+
+/**
+ * \brief RSA public key engine "i31".
+ *
+ * \see br_rsa_public
+ *
+ * \param x operand to exponentiate.
+ * \param xlen length of the operand (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA signature verification engine "i31" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash_len expected hash value length (in bytes).
+ * \param pk RSA public key.
+ * \param hash_out output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out);
+
+/**
+ * \brief RSA signature verification engine "i31" (PSS signatures).
+ *
+ * \see br_rsa_pss_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hf_data hash function applied on the message.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hash value of the signed message.
+ * \param salt_len PSS salt length (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA private key engine "i31".
+ *
+ * \see br_rsa_private
+ *
+ * \param x operand to exponentiate.
+ * \param sk RSA private key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_private(unsigned char *x,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief RSA signature generation engine "i31" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_sign
+ *
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash hash value.
+ * \param hash_len hash value length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief RSA signature generation engine "i31" (PSS signatures).
+ *
+ * \see br_rsa_pss_sign
+ *
+ * \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).
+ * \param hf_data hash function used to hash the signed data.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hashed message.
+ * \param salt_len salt length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash_value, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/*
+ * RSA "i62" engine. Similar to i31, but internal multiplication use
+ * 64x64->128 multiplications. This is available only on architecture
+ * that offer such an opcode.
+ */
+
+/**
+ * \brief RSA public key engine "i62".
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_public
+ *
+ * \param x operand to exponentiate.
+ * \param xlen length of the operand (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA signature verification engine "i62" (PKCS#1 v1.5 signatures).
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_pkcs1_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash_len expected hash value length (in bytes).
+ * \param pk RSA public key.
+ * \param hash_out output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out);
+
+/**
+ * \brief RSA signature verification engine "i62" (PSS signatures).
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_pss_vrfy_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_pss_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hf_data hash function applied on the message.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hash value of the signed message.
+ * \param salt_len PSS salt length (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA private key engine "i62".
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_private
+ *
+ * \param x operand to exponentiate.
+ * \param sk RSA private key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_private(unsigned char *x,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief RSA signature generation engine "i62" (PKCS#1 v1.5 signatures).
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_pkcs1_sign_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_pkcs1_sign
+ *
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash hash value.
+ * \param hash_len hash value length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief RSA signature generation engine "i62" (PSS signatures).
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_pss_sign_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_pss_sign
+ *
+ * \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).
+ * \param hf_data hash function used to hash the signed data.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hashed message.
+ * \param salt_len salt length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash_value, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief Get the RSA "i62" implementation (public key operations),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_public br_rsa_i62_public_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (PKCS#1 v1.5 signature verification),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_pkcs1_vrfy br_rsa_i62_pkcs1_vrfy_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (PSS signature verification),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_pss_vrfy br_rsa_i62_pss_vrfy_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (private key operations),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_private br_rsa_i62_private_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (PKCS#1 v1.5 signature generation),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_pkcs1_sign br_rsa_i62_pkcs1_sign_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (PSS signature generation),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_pss_sign br_rsa_i62_pss_sign_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (OAEP encryption),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_oaep_encrypt br_rsa_i62_oaep_encrypt_get(void);
+
+/**
+ * \brief Get the RSA "i62" implementation (OAEP decryption),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_oaep_decrypt br_rsa_i62_oaep_decrypt_get(void);
+
+/*
+ * RSA "i15" engine. Integers are represented as 15-bit integers, so
+ * the code uses only 32-bit multiplication (no 64-bit result), which
+ * is vastly faster (and constant-time) on the ARM Cortex M0/M0+.
+ */
+
+/**
+ * \brief RSA public key engine "i15".
+ *
+ * \see br_rsa_public
+ *
+ * \param x operand to exponentiate.
+ * \param xlen length of the operand (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA signature verification engine "i15" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash_len expected hash value length (in bytes).
+ * \param pk RSA public key.
+ * \param hash_out output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out);
+
+/**
+ * \brief RSA signature verification engine "i15" (PSS signatures).
+ *
+ * \see br_rsa_pss_vrfy
+ *
+ * \param x signature buffer.
+ * \param xlen signature length (in bytes).
+ * \param hf_data hash function applied on the message.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hash value of the signed message.
+ * \param salt_len PSS salt length (in bytes).
+ * \param pk RSA public key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk);
+
+/**
+ * \brief RSA private key engine "i15".
+ *
+ * \see br_rsa_private
+ *
+ * \param x operand to exponentiate.
+ * \param sk RSA private key.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_private(unsigned char *x,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief RSA signature generation engine "i15" (PKCS#1 v1.5 signatures).
+ *
+ * \see br_rsa_pkcs1_sign
+ *
+ * \param hash_oid encoded hash algorithm OID (or `NULL`).
+ * \param hash hash value.
+ * \param hash_len hash value length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the hash value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief RSA signature generation engine "i15" (PSS signatures).
+ *
+ * \see br_rsa_pss_sign
+ *
+ * \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).
+ * \param hf_data hash function used to hash the signed data.
+ * \param hf_mgf1 hash function to use with MGF1.
+ * \param hash hashed message.
+ * \param salt_len salt length (in bytes).
+ * \param sk RSA private key.
+ * \param x output buffer for the signature value.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash_value, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x);
+
+/**
+ * \brief Get "default" RSA implementation (public-key operations).
+ *
+ * This returns the preferred implementation of RSA (public-key operations)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_public br_rsa_public_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (private-key operations).
+ *
+ * This returns the preferred implementation of RSA (private-key operations)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_private br_rsa_private_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (PKCS#1 v1.5 signature verification).
+ *
+ * This returns the preferred implementation of RSA (signature verification)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_pkcs1_vrfy br_rsa_pkcs1_vrfy_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (PSS signature verification).
+ *
+ * This returns the preferred implementation of RSA (signature verification)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_pss_vrfy br_rsa_pss_vrfy_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (PKCS#1 v1.5 signature generation).
+ *
+ * This returns the preferred implementation of RSA (signature generation)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_pkcs1_sign br_rsa_pkcs1_sign_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (PSS signature generation).
+ *
+ * This returns the preferred implementation of RSA (signature generation)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_pss_sign br_rsa_pss_sign_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (OAEP encryption).
+ *
+ * This returns the preferred implementation of RSA (OAEP encryption)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_oaep_encrypt br_rsa_oaep_encrypt_get_default(void);
+
+/**
+ * \brief Get "default" RSA implementation (OAEP decryption).
+ *
+ * This returns the preferred implementation of RSA (OAEP decryption)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_oaep_decrypt br_rsa_oaep_decrypt_get_default(void);
+
+/**
+ * \brief RSA decryption helper, for SSL/TLS.
+ *
+ * This function performs the RSA decryption for a RSA-based key exchange
+ * in a SSL/TLS server. The provided RSA engine is used. The `data`
+ * parameter points to the value to decrypt, of length `len` bytes. On
+ * success, the 48-byte pre-master secret is copied into `data`, starting
+ * at the first byte of that buffer; on error, the contents of `data`
+ * become indeterminate.
+ *
+ * This function first checks that the provided value length (`len`) is
+ * not lower than 59 bytes, and matches the RSA modulus length; if neither
+ * of this property is met, then this function returns 0 and the buffer
+ * is unmodified.
+ *
+ * Otherwise, decryption and then padding verification are performed, both
+ * in constant-time. A decryption error, or a bad padding, or an
+ * incorrect decrypted value length are reported with a returned value of
+ * 0; on success, 1 is returned. The caller (SSL server engine) is supposed
+ * to proceed with a random pre-master secret in case of error.
+ *
+ * \param core RSA private key engine.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len length (in bytes) of the data to decrypt.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_ssl_decrypt(br_rsa_private core, const br_rsa_private_key *sk,
+ unsigned char *data, size_t len);
+
+/**
+ * \brief RSA encryption (OAEP) with the "i15" engine.
+ *
+ * \see br_rsa_oaep_encrypt
+ *
+ * \param rnd source of random bytes.
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param pk RSA public key.
+ * \param dst destination buffer.
+ * \param dst_max_len destination buffer length (maximum encrypted data size).
+ * \param src message to encrypt.
+ * \param src_len source message length (in bytes).
+ * \return encrypted message length (in bytes), or 0 on error.
+ */
+size_t br_rsa_i15_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len);
+
+/**
+ * \brief RSA decryption (OAEP) with the "i15" engine.
+ *
+ * \see br_rsa_oaep_decrypt
+ *
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len encrypted/decrypted message length.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i15_oaep_decrypt(
+ const br_hash_class *dig, const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len);
+
+/**
+ * \brief RSA encryption (OAEP) with the "i31" engine.
+ *
+ * \see br_rsa_oaep_encrypt
+ *
+ * \param rnd source of random bytes.
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param pk RSA public key.
+ * \param dst destination buffer.
+ * \param dst_max_len destination buffer length (maximum encrypted data size).
+ * \param src message to encrypt.
+ * \param src_len source message length (in bytes).
+ * \return encrypted message length (in bytes), or 0 on error.
+ */
+size_t br_rsa_i31_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len);
+
+/**
+ * \brief RSA decryption (OAEP) with the "i31" engine.
+ *
+ * \see br_rsa_oaep_decrypt
+ *
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len encrypted/decrypted message length.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i31_oaep_decrypt(
+ const br_hash_class *dig, const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len);
+
+/**
+ * \brief RSA encryption (OAEP) with the "i32" engine.
+ *
+ * \see br_rsa_oaep_encrypt
+ *
+ * \param rnd source of random bytes.
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param pk RSA public key.
+ * \param dst destination buffer.
+ * \param dst_max_len destination buffer length (maximum encrypted data size).
+ * \param src message to encrypt.
+ * \param src_len source message length (in bytes).
+ * \return encrypted message length (in bytes), or 0 on error.
+ */
+size_t br_rsa_i32_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len);
+
+/**
+ * \brief RSA decryption (OAEP) with the "i32" engine.
+ *
+ * \see br_rsa_oaep_decrypt
+ *
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len encrypted/decrypted message length.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i32_oaep_decrypt(
+ const br_hash_class *dig, const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len);
+
+/**
+ * \brief RSA encryption (OAEP) with the "i62" engine.
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_oaep_encrypt_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_oaep_encrypt
+ *
+ * \param rnd source of random bytes.
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param pk RSA public key.
+ * \param dst destination buffer.
+ * \param dst_max_len destination buffer length (maximum encrypted data size).
+ * \param src message to encrypt.
+ * \param src_len source message length (in bytes).
+ * \return encrypted message length (in bytes), or 0 on error.
+ */
+size_t br_rsa_i62_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len);
+
+/**
+ * \brief RSA decryption (OAEP) with the "i62" engine.
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_oaep_decrypt_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_oaep_decrypt
+ *
+ * \param dig hash function to use with MGF1.
+ * \param label label value (may be `NULL` if `label_len` is zero).
+ * \param label_len label length, in bytes.
+ * \param sk RSA private key.
+ * \param data input/output buffer.
+ * \param len encrypted/decrypted message length.
+ * \return 1 on success, 0 on error.
+ */
+uint32_t br_rsa_i62_oaep_decrypt(
+ const br_hash_class *dig, const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len);
+
+/**
+ * \brief Get buffer size to hold RSA private key elements.
+ *
+ * This macro returns the length (in bytes) of the buffer needed to
+ * receive the elements of a RSA private key, as generated by one of
+ * the `br_rsa_*_keygen()` functions. If the provided size is a constant
+ * expression, then the whole macro evaluates to a constant expression.
+ *
+ * \param size target key size (modulus size, in bits)
+ * \return the length of the private key buffer, in bytes.
+ */
+#define BR_RSA_KBUF_PRIV_SIZE(size) (5 * (((size) + 15) >> 4))
+
+/**
+ * \brief Get buffer size to hold RSA public key elements.
+ *
+ * This macro returns the length (in bytes) of the buffer needed to
+ * receive the elements of a RSA public key, as generated by one of
+ * the `br_rsa_*_keygen()` functions. If the provided size is a constant
+ * expression, then the whole macro evaluates to a constant expression.
+ *
+ * \param size target key size (modulus size, in bits)
+ * \return the length of the public key buffer, in bytes.
+ */
+#define BR_RSA_KBUF_PUB_SIZE(size) (4 + (((size) + 7) >> 3))
+
+/**
+ * \brief Type for RSA key pair generator implementation.
+ *
+ * This function generates a new RSA key pair whose modulus has bit
+ * length `size` bits. The private key elements are written in the
+ * `kbuf_priv` buffer, and pointer values and length fields to these
+ * elements are populated in the provided private key structure `sk`.
+ * Similarly, the public key elements are written in `kbuf_pub`, with
+ * pointers and lengths set in `pk`.
+ *
+ * If `pk` is `NULL`, then `kbuf_pub` may be `NULL`, and only the
+ * private key is set.
+ *
+ * If `pubexp` is not zero, then its value will be used as public
+ * exponent. Valid RSA public exponent values are odd integers
+ * greater than 1. If `pubexp` is zero, then the public exponent will
+ * have value 3.
+ *
+ * The provided PRNG (`rng_ctx`) must have already been initialized
+ * and seeded.
+ *
+ * Returned value is 1 on success, 0 on error. An error is reported
+ * if the requested range is outside of the supported key sizes, or
+ * if an invalid non-zero public exponent value is provided. Supported
+ * range starts at 512 bits, and up to an implementation-defined
+ * maximum (by default 4096 bits). Note that key sizes up to 768 bits
+ * have been broken in practice, and sizes lower than 2048 bits are
+ * usually considered to be weak and should not be used.
+ *
+ * \param rng_ctx source PRNG context (already initialized)
+ * \param sk RSA private key structure (destination)
+ * \param kbuf_priv buffer for private key elements
+ * \param pk RSA public key structure (destination), or `NULL`
+ * \param kbuf_pub buffer for public key elements, or `NULL`
+ * \param size target RSA modulus size (in bits)
+ * \param pubexp public exponent to use, or zero
+ * \return 1 on success, 0 on error (invalid parameters)
+ */
+typedef uint32_t (*br_rsa_keygen)(
+ const br_prng_class **rng_ctx,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp);
+
+/**
+ * \brief RSA key pair generation with the "i15" engine.
+ *
+ * \see br_rsa_keygen
+ *
+ * \param rng_ctx source PRNG context (already initialized)
+ * \param sk RSA private key structure (destination)
+ * \param kbuf_priv buffer for private key elements
+ * \param pk RSA public key structure (destination), or `NULL`
+ * \param kbuf_pub buffer for public key elements, or `NULL`
+ * \param size target RSA modulus size (in bits)
+ * \param pubexp public exponent to use, or zero
+ * \return 1 on success, 0 on error (invalid parameters)
+ */
+uint32_t br_rsa_i15_keygen(
+ const br_prng_class **rng_ctx,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp);
+
+/**
+ * \brief RSA key pair generation with the "i31" engine.
+ *
+ * \see br_rsa_keygen
+ *
+ * \param rng_ctx source PRNG context (already initialized)
+ * \param sk RSA private key structure (destination)
+ * \param kbuf_priv buffer for private key elements
+ * \param pk RSA public key structure (destination), or `NULL`
+ * \param kbuf_pub buffer for public key elements, or `NULL`
+ * \param size target RSA modulus size (in bits)
+ * \param pubexp public exponent to use, or zero
+ * \return 1 on success, 0 on error (invalid parameters)
+ */
+uint32_t br_rsa_i31_keygen(
+ const br_prng_class **rng_ctx,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp);
+
+/**
+ * \brief RSA key pair generation with the "i62" engine.
+ *
+ * This function is defined only on architecture that offer a 64x64->128
+ * opcode. Use `br_rsa_i62_keygen_get()` to dynamically obtain a pointer
+ * to that function.
+ *
+ * \see br_rsa_keygen
+ *
+ * \param rng_ctx source PRNG context (already initialized)
+ * \param sk RSA private key structure (destination)
+ * \param kbuf_priv buffer for private key elements
+ * \param pk RSA public key structure (destination), or `NULL`
+ * \param kbuf_pub buffer for public key elements, or `NULL`
+ * \param size target RSA modulus size (in bits)
+ * \param pubexp public exponent to use, or zero
+ * \return 1 on success, 0 on error (invalid parameters)
+ */
+uint32_t br_rsa_i62_keygen(
+ const br_prng_class **rng_ctx,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp);
+
+/**
+ * \brief Get the RSA "i62" implementation (key pair generation),
+ * if available.
+ *
+ * \return the implementation, or 0.
+ */
+br_rsa_keygen br_rsa_i62_keygen_get(void);
+
+/**
+ * \brief Get "default" RSA implementation (key pair generation).
+ *
+ * This returns the preferred implementation of RSA (key pair generation)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_keygen br_rsa_keygen_get_default(void);
+
+/**
+ * \brief Type for a modulus computing function.
+ *
+ * Such a function computes the public modulus from the private key. The
+ * encoded modulus (unsigned big-endian) is written on `n`, and the size
+ * (in bytes) is returned. If `n` is `NULL`, then the size is returned but
+ * the modulus itself is not computed.
+ *
+ * If the key size exceeds an internal limit, 0 is returned.
+ *
+ * \param n destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \return the modulus length (in bytes), or 0.
+ */
+typedef size_t (*br_rsa_compute_modulus)(void *n, const br_rsa_private_key *sk);
+
+/**
+ * \brief Recompute RSA modulus ("i15" engine).
+ *
+ * \see br_rsa_compute_modulus
+ *
+ * \param n destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \return the modulus length (in bytes), or 0.
+ */
+size_t br_rsa_i15_compute_modulus(void *n, const br_rsa_private_key *sk);
+
+/**
+ * \brief Recompute RSA modulus ("i31" engine).
+ *
+ * \see br_rsa_compute_modulus
+ *
+ * \param n destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \return the modulus length (in bytes), or 0.
+ */
+size_t br_rsa_i31_compute_modulus(void *n, const br_rsa_private_key *sk);
+
+/**
+ * \brief Get "default" RSA implementation (recompute modulus).
+ *
+ * This returns the preferred implementation of RSA (recompute modulus)
+ * on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_compute_modulus br_rsa_compute_modulus_get_default(void);
+
+/**
+ * \brief Type for a public exponent computing function.
+ *
+ * Such a function recomputes the public exponent from the private key.
+ * 0 is returned if any of the following occurs:
+ *
+ * - Either `p` or `q` is not equal to 3 modulo 4.
+ *
+ * - The public exponent does not fit on 32 bits.
+ *
+ * - An internal limit is exceeded.
+ *
+ * - The private key is invalid in some way.
+ *
+ * For all private keys produced by the key generator functions
+ * (`br_rsa_keygen` type), this function succeeds and returns the true
+ * public exponent. The public exponent is always an odd integer greater
+ * than 1.
+ *
+ * \return the public exponent, or 0.
+ */
+typedef uint32_t (*br_rsa_compute_pubexp)(const br_rsa_private_key *sk);
+
+/**
+ * \brief Recompute RSA public exponent ("i15" engine).
+ *
+ * \see br_rsa_compute_pubexp
+ *
+ * \return the public exponent, or 0.
+ */
+uint32_t br_rsa_i15_compute_pubexp(const br_rsa_private_key *sk);
+
+/**
+ * \brief Recompute RSA public exponent ("i31" engine).
+ *
+ * \see br_rsa_compute_pubexp
+ *
+ * \return the public exponent, or 0.
+ */
+uint32_t br_rsa_i31_compute_pubexp(const br_rsa_private_key *sk);
+
+/**
+ * \brief Get "default" RSA implementation (recompute public exponent).
+ *
+ * This returns the preferred implementation of RSA (recompute public
+ * exponent) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_compute_pubexp br_rsa_compute_pubexp_get_default(void);
+
+/**
+ * \brief Type for a private exponent computing function.
+ *
+ * An RSA private key (`br_rsa_private_key`) contains two reduced
+ * private exponents, which are sufficient to perform private key
+ * operations. However, standard encoding formats for RSA private keys
+ * require also a copy of the complete private exponent (non-reduced),
+ * which this function recomputes.
+ *
+ * This function suceeds if all the following conditions hold:
+ *
+ * - Both private factors `p` and `q` are equal to 3 modulo 4.
+ *
+ * - The provided public exponent `pubexp` is correct, and, in particular,
+ * is odd, relatively prime to `p-1` and `q-1`, and greater than 1.
+ *
+ * - No internal storage limit is exceeded.
+ *
+ * For all private keys produced by the key generator functions
+ * (`br_rsa_keygen` type), this function succeeds. Note that the API
+ * restricts the public exponent to a maximum size of 32 bits.
+ *
+ * The encoded private exponent is written in `d` (unsigned big-endian
+ * convention), and the length (in bytes) is returned. If `d` is `NULL`,
+ * then the exponent is not written anywhere, but the length is still
+ * returned. On error, 0 is returned.
+ *
+ * Not all error conditions are detected when `d` is `NULL`; therefore, the
+ * returned value shall be checked also when actually producing the value.
+ *
+ * \param d destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \param pubexp the public exponent.
+ * \return the private exponent length (in bytes), or 0.
+ */
+typedef size_t (*br_rsa_compute_privexp)(void *d,
+ const br_rsa_private_key *sk, uint32_t pubexp);
+
+/**
+ * \brief Recompute RSA private exponent ("i15" engine).
+ *
+ * \see br_rsa_compute_privexp
+ *
+ * \param d destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \param pubexp the public exponent.
+ * \return the private exponent length (in bytes), or 0.
+ */
+size_t br_rsa_i15_compute_privexp(void *d,
+ const br_rsa_private_key *sk, uint32_t pubexp);
+
+/**
+ * \brief Recompute RSA private exponent ("i31" engine).
+ *
+ * \see br_rsa_compute_privexp
+ *
+ * \param d destination buffer (or `NULL`).
+ * \param sk RSA private key.
+ * \param pubexp the public exponent.
+ * \return the private exponent length (in bytes), or 0.
+ */
+size_t br_rsa_i31_compute_privexp(void *d,
+ const br_rsa_private_key *sk, uint32_t pubexp);
+
+/**
+ * \brief Get "default" RSA implementation (recompute private exponent).
+ *
+ * This returns the preferred implementation of RSA (recompute private
+ * exponent) on the current system.
+ *
+ * \return the default implementation.
+ */
+br_rsa_compute_privexp br_rsa_compute_privexp_get_default(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_ssl.h b/test/monniaux/BearSSL/inc/bearssl_ssl.h
new file mode 100644
index 00000000..8c8c86bd
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_ssl.h
@@ -0,0 +1,4296 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_SSL_H__
+#define BR_BEARSSL_SSL_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_block.h"
+#include "bearssl_hash.h"
+#include "bearssl_hmac.h"
+#include "bearssl_prf.h"
+#include "bearssl_rand.h"
+#include "bearssl_x509.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_ssl.h
+ *
+ * # SSL
+ *
+ * For an overview of the SSL/TLS API, see [the BearSSL Web
+ * site](https://www.bearssl.org/api1.html).
+ *
+ * The `BR_TLS_*` constants correspond to the standard cipher suites and
+ * their values in the [IANA
+ * registry](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4).
+ *
+ * The `BR_ALERT_*` constants are for standard TLS alert messages. When
+ * a fatal alert message is sent of received, then the SSL engine context
+ * status is set to the sum of that alert value (an integer in the 0..255
+ * range) and a fixed offset (`BR_ERR_SEND_FATAL_ALERT` for a sent alert,
+ * `BR_ERR_RECV_FATAL_ALERT` for a received alert).
+ */
+
+/** \brief Optimal input buffer size. */
+#define BR_SSL_BUFSIZE_INPUT (16384 + 325)
+
+/** \brief Optimal output buffer size. */
+#define BR_SSL_BUFSIZE_OUTPUT (16384 + 85)
+
+/** \brief Optimal buffer size for monodirectional engine
+ (shared input/output buffer). */
+#define BR_SSL_BUFSIZE_MONO BR_SSL_BUFSIZE_INPUT
+
+/** \brief Optimal buffer size for bidirectional engine
+ (single buffer split into two separate input/output buffers). */
+#define BR_SSL_BUFSIZE_BIDI (BR_SSL_BUFSIZE_INPUT + BR_SSL_BUFSIZE_OUTPUT)
+
+/*
+ * Constants for known SSL/TLS protocol versions (SSL 3.0, TLS 1.0, TLS 1.1
+ * and TLS 1.2). Note that though there is a constant for SSL 3.0, that
+ * protocol version is not actually supported.
+ */
+
+/** \brief Protocol version: SSL 3.0 (unsupported). */
+#define BR_SSL30 0x0300
+/** \brief Protocol version: TLS 1.0. */
+#define BR_TLS10 0x0301
+/** \brief Protocol version: TLS 1.1. */
+#define BR_TLS11 0x0302
+/** \brief Protocol version: TLS 1.2. */
+#define BR_TLS12 0x0303
+
+/*
+ * Error constants. They are used to report the reason why a context has
+ * been marked as failed.
+ *
+ * Implementation note: SSL-level error codes should be in the 1..31
+ * range. The 32..63 range is for certificate decoding and validation
+ * errors. Received fatal alerts imply an error code in the 256..511 range.
+ */
+
+/** \brief SSL status: no error so far (0). */
+#define BR_ERR_OK 0
+
+/** \brief SSL status: caller-provided parameter is incorrect. */
+#define BR_ERR_BAD_PARAM 1
+
+/** \brief SSL status: operation requested by the caller cannot be applied
+ with the current context state (e.g. reading data while outgoing data
+ is waiting to be sent). */
+#define BR_ERR_BAD_STATE 2
+
+/** \brief SSL status: incoming protocol or record version is unsupported. */
+#define BR_ERR_UNSUPPORTED_VERSION 3
+
+/** \brief SSL status: incoming record version does not match the expected
+ version. */
+#define BR_ERR_BAD_VERSION 4
+
+/** \brief SSL status: incoming record length is invalid. */
+#define BR_ERR_BAD_LENGTH 5
+
+/** \brief SSL status: incoming record is too large to be processed, or
+ buffer is too small for the handshake message to send. */
+#define BR_ERR_TOO_LARGE 6
+
+/** \brief SSL status: decryption found an invalid padding, or the record
+ MAC is not correct. */
+#define BR_ERR_BAD_MAC 7
+
+/** \brief SSL status: no initial entropy was provided, and none can be
+ obtained from the OS. */
+#define BR_ERR_NO_RANDOM 8
+
+/** \brief SSL status: incoming record type is unknown. */
+#define BR_ERR_UNKNOWN_TYPE 9
+
+/** \brief SSL status: incoming record or message has wrong type with
+ regards to the current engine state. */
+#define BR_ERR_UNEXPECTED 10
+
+/** \brief SSL status: ChangeCipherSpec message from the peer has invalid
+ contents. */
+#define BR_ERR_BAD_CCS 12
+
+/** \brief SSL status: alert message from the peer has invalid contents
+ (odd length). */
+#define BR_ERR_BAD_ALERT 13
+
+/** \brief SSL status: incoming handshake message decoding failed. */
+#define BR_ERR_BAD_HANDSHAKE 14
+
+/** \brief SSL status: ServerHello contains a session ID which is larger
+ than 32 bytes. */
+#define BR_ERR_OVERSIZED_ID 15
+
+/** \brief SSL status: server wants to use a cipher suite that we did
+ not claim to support. This is also reported if we tried to advertise
+ a cipher suite that we do not support. */
+#define BR_ERR_BAD_CIPHER_SUITE 16
+
+/** \brief SSL status: server wants to use a compression that we did not
+ claim to support. */
+#define BR_ERR_BAD_COMPRESSION 17
+
+/** \brief SSL status: server's max fragment length does not match
+ client's. */
+#define BR_ERR_BAD_FRAGLEN 18
+
+/** \brief SSL status: secure renegotiation failed. */
+#define BR_ERR_BAD_SECRENEG 19
+
+/** \brief SSL status: server sent an extension type that we did not
+ announce, or used the same extension type several times in a single
+ ServerHello. */
+#define BR_ERR_EXTRA_EXTENSION 20
+
+/** \brief SSL status: invalid Server Name Indication contents (when
+ used by the server, this extension shall be empty). */
+#define BR_ERR_BAD_SNI 21
+
+/** \brief SSL status: invalid ServerHelloDone from the server (length
+ is not 0). */
+#define BR_ERR_BAD_HELLO_DONE 22
+
+/** \brief SSL status: internal limit exceeded (e.g. server's public key
+ is too large). */
+#define BR_ERR_LIMIT_EXCEEDED 23
+
+/** \brief SSL status: Finished message from peer does not match the
+ expected value. */
+#define BR_ERR_BAD_FINISHED 24
+
+/** \brief SSL status: session resumption attempt with distinct version
+ or cipher suite. */
+#define BR_ERR_RESUME_MISMATCH 25
+
+/** \brief SSL status: unsupported or invalid algorithm (ECDHE curve,
+ signature algorithm, hash function). */
+#define BR_ERR_INVALID_ALGORITHM 26
+
+/** \brief SSL status: invalid signature (on ServerKeyExchange from
+ server, or in CertificateVerify from client). */
+#define BR_ERR_BAD_SIGNATURE 27
+
+/** \brief SSL status: peer's public key does not have the proper type
+ or is not allowed for requested operation. */
+#define BR_ERR_WRONG_KEY_USAGE 28
+
+/** \brief SSL status: client did not send a certificate upon request,
+ or the client certificate could not be validated. */
+#define BR_ERR_NO_CLIENT_AUTH 29
+
+/** \brief SSL status: I/O error or premature close on underlying
+ transport stream. This error code is set only by the simplified
+ I/O API ("br_sslio_*"). */
+#define BR_ERR_IO 31
+
+/** \brief SSL status: base value for a received fatal alert.
+
+ When a fatal alert is received from the peer, the alert value
+ is added to this constant. */
+#define BR_ERR_RECV_FATAL_ALERT 256
+
+/** \brief SSL status: base value for a sent fatal alert.
+
+ When a fatal alert is sent to the peer, the alert value is added
+ to this constant. */
+#define BR_ERR_SEND_FATAL_ALERT 512
+
+/* ===================================================================== */
+
+/**
+ * \brief Decryption engine for SSL.
+ *
+ * When processing incoming records, the SSL engine will use a decryption
+ * engine that uses a specific context structure, and has a set of
+ * methods (a vtable) that follows this template.
+ *
+ * The decryption engine is responsible for applying decryption, verifying
+ * MAC, and keeping track of the record sequence number.
+ */
+typedef struct br_sslrec_in_class_ br_sslrec_in_class;
+struct br_sslrec_in_class_ {
+ /**
+ * \brief Context size (in bytes).
+ */
+ size_t context_size;
+
+ /**
+ * \brief Test validity of the incoming record length.
+ *
+ * This function returns 1 if the announced length for an
+ * incoming record is valid, 0 otherwise,
+ *
+ * \param ctx decryption engine context.
+ * \param record_len incoming record length.
+ * \return 1 of a valid length, 0 otherwise.
+ */
+ int (*check_length)(const br_sslrec_in_class *const *ctx,
+ size_t record_len);
+
+ /**
+ * \brief Decrypt the incoming record.
+ *
+ * This function may assume that the record length is valid
+ * (it has been previously tested with `check_length()`).
+ * Decryption is done in place; `*len` is updated with the
+ * cleartext length, and the address of the first plaintext
+ * byte is returned. If the record is correct but empty, then
+ * `*len` is set to 0 and a non-`NULL` pointer is returned.
+ *
+ * On decryption/MAC error, `NULL` is returned.
+ *
+ * \param ctx decryption engine context.
+ * \param record_type record type (23 for application data, etc).
+ * \param version record version.
+ * \param payload address of encrypted payload.
+ * \param len pointer to payload length (updated).
+ * \return pointer to plaintext, or `NULL` on error.
+ */
+ unsigned char *(*decrypt)(const br_sslrec_in_class **ctx,
+ int record_type, unsigned version,
+ void *payload, size_t *len);
+};
+
+/**
+ * \brief Encryption engine for SSL.
+ *
+ * When building outgoing records, the SSL engine will use an encryption
+ * engine that uses a specific context structure, and has a set of
+ * methods (a vtable) that follows this template.
+ *
+ * The encryption engine is responsible for applying encryption and MAC,
+ * and keeping track of the record sequence number.
+ */
+typedef struct br_sslrec_out_class_ br_sslrec_out_class;
+struct br_sslrec_out_class_ {
+ /**
+ * \brief Context size (in bytes).
+ */
+ size_t context_size;
+
+ /**
+ * \brief Compute maximum plaintext sizes and offsets.
+ *
+ * When this function is called, the `*start` and `*end`
+ * values contain offsets designating the free area in the
+ * outgoing buffer for plaintext data; that free area is
+ * preceded by a 5-byte space which will receive the record
+ * header.
+ *
+ * The `max_plaintext()` function is responsible for adjusting
+ * both `*start` and `*end` to make room for any record-specific
+ * header, MAC, padding, and possible split.
+ *
+ * \param ctx encryption engine context.
+ * \param start pointer to start of plaintext offset (updated).
+ * \param end pointer to start of plaintext offset (updated).
+ */
+ void (*max_plaintext)(const br_sslrec_out_class *const *ctx,
+ size_t *start, size_t *end);
+
+ /**
+ * \brief Perform record encryption.
+ *
+ * This function encrypts the record. The plaintext address and
+ * length are provided. Returned value is the start of the
+ * encrypted record (or sequence of records, if a split was
+ * performed), _including_ the 5-byte header, and `*len` is
+ * adjusted to the total size of the record(s), there again
+ * including the header(s).
+ *
+ * \param ctx decryption engine context.
+ * \param record_type record type (23 for application data, etc).
+ * \param version record version.
+ * \param plaintext address of plaintext.
+ * \param len pointer to plaintext length (updated).
+ * \return pointer to start of built record.
+ */
+ unsigned char *(*encrypt)(const br_sslrec_out_class **ctx,
+ int record_type, unsigned version,
+ void *plaintext, size_t *len);
+};
+
+/**
+ * \brief Context for a no-encryption engine.
+ *
+ * The no-encryption engine processes outgoing records during the initial
+ * handshake, before encryption is applied.
+ */
+typedef struct {
+ /** \brief No-encryption engine vtable. */
+ const br_sslrec_out_class *vtable;
+} br_sslrec_out_clear_context;
+
+/** \brief Static, constant vtable for the no-encryption engine. */
+extern const br_sslrec_out_class br_sslrec_out_clear_vtable;
+
+/* ===================================================================== */
+
+/**
+ * \brief Record decryption engine class, for CBC mode.
+ *
+ * This class type extends the decryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CBC processing: block cipher implementation, block cipher key,
+ * HMAC parameters (hash function, key, MAC length), and IV. If the
+ * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
+ */
+typedef struct br_sslrec_in_cbc_class_ br_sslrec_in_cbc_class;
+struct br_sslrec_in_cbc_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_in_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CBC decryption).
+ * \param bc_key block cipher key.
+ * \param bc_key_len block cipher key length (in bytes).
+ * \param dig_impl hash function for HMAC.
+ * \param mac_key HMAC key.
+ * \param mac_key_len HMAC key length (in bytes).
+ * \param mac_out_len HMAC output length (in bytes).
+ * \param iv initial IV (or `NULL`).
+ */
+ void (*init)(const br_sslrec_in_cbc_class **ctx,
+ const br_block_cbcdec_class *bc_impl,
+ const void *bc_key, size_t bc_key_len,
+ const br_hash_class *dig_impl,
+ const void *mac_key, size_t mac_key_len, size_t mac_out_len,
+ const void *iv);
+};
+
+/**
+ * \brief Record encryption engine class, for CBC mode.
+ *
+ * This class type extends the encryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CBC processing: block cipher implementation, block cipher key,
+ * HMAC parameters (hash function, key, MAC length), and IV. If the
+ * IV is `NULL`, then a per-record IV will be used (TLS 1.1+).
+ */
+typedef struct br_sslrec_out_cbc_class_ br_sslrec_out_cbc_class;
+struct br_sslrec_out_cbc_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_out_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CBC encryption).
+ * \param bc_key block cipher key.
+ * \param bc_key_len block cipher key length (in bytes).
+ * \param dig_impl hash function for HMAC.
+ * \param mac_key HMAC key.
+ * \param mac_key_len HMAC key length (in bytes).
+ * \param mac_out_len HMAC output length (in bytes).
+ * \param iv initial IV (or `NULL`).
+ */
+ void (*init)(const br_sslrec_out_cbc_class **ctx,
+ const br_block_cbcenc_class *bc_impl,
+ const void *bc_key, size_t bc_key_len,
+ const br_hash_class *dig_impl,
+ const void *mac_key, size_t mac_key_len, size_t mac_out_len,
+ const void *iv);
+};
+
+/**
+ * \brief Context structure for decrypting incoming records with
+ * CBC + HMAC.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_sslrec_in_cbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t seq;
+ union {
+ const br_block_cbcdec_class *vtable;
+ br_aes_gen_cbcdec_keys aes;
+ br_des_gen_cbcdec_keys des;
+ } bc;
+ br_hmac_key_context mac;
+ size_t mac_len;
+ unsigned char iv[16];
+ int explicit_IV;
+#endif
+} br_sslrec_in_cbc_context;
+
+/**
+ * \brief Static, constant vtable for record decryption with CBC.
+ */
+extern const br_sslrec_in_cbc_class br_sslrec_in_cbc_vtable;
+
+/**
+ * \brief Context structure for encrypting outgoing records with
+ * CBC + HMAC.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_sslrec_out_cbc_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t seq;
+ union {
+ const br_block_cbcenc_class *vtable;
+ br_aes_gen_cbcenc_keys aes;
+ br_des_gen_cbcenc_keys des;
+ } bc;
+ br_hmac_key_context mac;
+ size_t mac_len;
+ unsigned char iv[16];
+ int explicit_IV;
+#endif
+} br_sslrec_out_cbc_context;
+
+/**
+ * \brief Static, constant vtable for record encryption with CBC.
+ */
+extern const br_sslrec_out_cbc_class br_sslrec_out_cbc_vtable;
+
+/* ===================================================================== */
+
+/**
+ * \brief Record decryption engine class, for GCM mode.
+ *
+ * This class type extends the decryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for GCM processing: block cipher implementation, block cipher key,
+ * GHASH implementation, and 4-byte IV.
+ */
+typedef struct br_sslrec_in_gcm_class_ br_sslrec_in_gcm_class;
+struct br_sslrec_in_gcm_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_in_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CTR).
+ * \param key block cipher key.
+ * \param key_len block cipher key length (in bytes).
+ * \param gh_impl GHASH implementation.
+ * \param iv static IV (4 bytes).
+ */
+ void (*init)(const br_sslrec_in_gcm_class **ctx,
+ const br_block_ctr_class *bc_impl,
+ const void *key, size_t key_len,
+ br_ghash gh_impl,
+ const void *iv);
+};
+
+/**
+ * \brief Record encryption engine class, for GCM mode.
+ *
+ * This class type extends the encryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for GCM processing: block cipher implementation, block cipher key,
+ * GHASH implementation, and 4-byte IV.
+ */
+typedef struct br_sslrec_out_gcm_class_ br_sslrec_out_gcm_class;
+struct br_sslrec_out_gcm_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_out_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CTR).
+ * \param key block cipher key.
+ * \param key_len block cipher key length (in bytes).
+ * \param gh_impl GHASH implementation.
+ * \param iv static IV (4 bytes).
+ */
+ void (*init)(const br_sslrec_out_gcm_class **ctx,
+ const br_block_ctr_class *bc_impl,
+ const void *key, size_t key_len,
+ br_ghash gh_impl,
+ const void *iv);
+};
+
+/**
+ * \brief Context structure for processing records with GCM.
+ *
+ * The same context structure is used for encrypting and decrypting.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ union {
+ const void *gen;
+ const br_sslrec_in_gcm_class *in;
+ const br_sslrec_out_gcm_class *out;
+ } vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t seq;
+ union {
+ const br_block_ctr_class *vtable;
+ br_aes_gen_ctr_keys aes;
+ } bc;
+ br_ghash gh;
+ unsigned char iv[4];
+ unsigned char h[16];
+#endif
+} br_sslrec_gcm_context;
+
+/**
+ * \brief Static, constant vtable for record decryption with GCM.
+ */
+extern const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable;
+
+/**
+ * \brief Static, constant vtable for record encryption with GCM.
+ */
+extern const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable;
+
+/* ===================================================================== */
+
+/**
+ * \brief Record decryption engine class, for ChaCha20+Poly1305.
+ *
+ * This class type extends the decryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
+ * Poly1305 implementation, key, and 12-byte IV.
+ */
+typedef struct br_sslrec_in_chapol_class_ br_sslrec_in_chapol_class;
+struct br_sslrec_in_chapol_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_in_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param ichacha ChaCha20 implementation.
+ * \param ipoly Poly1305 implementation.
+ * \param key secret key (32 bytes).
+ * \param iv static IV (12 bytes).
+ */
+ void (*init)(const br_sslrec_in_chapol_class **ctx,
+ br_chacha20_run ichacha,
+ br_poly1305_run ipoly,
+ const void *key, const void *iv);
+};
+
+/**
+ * \brief Record encryption engine class, for ChaCha20+Poly1305.
+ *
+ * This class type extends the encryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for ChaCha20+Poly1305 processing: ChaCha20 implementation,
+ * Poly1305 implementation, key, and 12-byte IV.
+ */
+typedef struct br_sslrec_out_chapol_class_ br_sslrec_out_chapol_class;
+struct br_sslrec_out_chapol_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_out_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param ichacha ChaCha20 implementation.
+ * \param ipoly Poly1305 implementation.
+ * \param key secret key (32 bytes).
+ * \param iv static IV (12 bytes).
+ */
+ void (*init)(const br_sslrec_out_chapol_class **ctx,
+ br_chacha20_run ichacha,
+ br_poly1305_run ipoly,
+ const void *key, const void *iv);
+};
+
+/**
+ * \brief Context structure for processing records with ChaCha20+Poly1305.
+ *
+ * The same context structure is used for encrypting and decrypting.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ union {
+ const void *gen;
+ const br_sslrec_in_chapol_class *in;
+ const br_sslrec_out_chapol_class *out;
+ } vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t seq;
+ unsigned char key[32];
+ unsigned char iv[12];
+ br_chacha20_run ichacha;
+ br_poly1305_run ipoly;
+#endif
+} br_sslrec_chapol_context;
+
+/**
+ * \brief Static, constant vtable for record decryption with ChaCha20+Poly1305.
+ */
+extern const br_sslrec_in_chapol_class br_sslrec_in_chapol_vtable;
+
+/**
+ * \brief Static, constant vtable for record encryption with ChaCha20+Poly1305.
+ */
+extern const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable;
+
+/* ===================================================================== */
+
+/**
+ * \brief Record decryption engine class, for CCM mode.
+ *
+ * This class type extends the decryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CCM processing: block cipher implementation, block cipher key,
+ * and 4-byte IV.
+ */
+typedef struct br_sslrec_in_ccm_class_ br_sslrec_in_ccm_class;
+struct br_sslrec_in_ccm_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_in_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CTR+CBC).
+ * \param key block cipher key.
+ * \param key_len block cipher key length (in bytes).
+ * \param iv static IV (4 bytes).
+ * \param tag_len tag length (in bytes)
+ */
+ void (*init)(const br_sslrec_in_ccm_class **ctx,
+ const br_block_ctrcbc_class *bc_impl,
+ const void *key, size_t key_len,
+ const void *iv, size_t tag_len);
+};
+
+/**
+ * \brief Record encryption engine class, for CCM mode.
+ *
+ * This class type extends the encryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CCM processing: block cipher implementation, block cipher key,
+ * and 4-byte IV.
+ */
+typedef struct br_sslrec_out_ccm_class_ br_sslrec_out_ccm_class;
+struct br_sslrec_out_ccm_class_ {
+ /**
+ * \brief Superclass, as first vtable field.
+ */
+ br_sslrec_out_class inner;
+
+ /**
+ * \brief Engine initialisation method.
+ *
+ * This method sets the vtable field in the context.
+ *
+ * \param ctx context to initialise.
+ * \param bc_impl block cipher implementation (CTR+CBC).
+ * \param key block cipher key.
+ * \param key_len block cipher key length (in bytes).
+ * \param iv static IV (4 bytes).
+ * \param tag_len tag length (in bytes)
+ */
+ void (*init)(const br_sslrec_out_ccm_class **ctx,
+ const br_block_ctrcbc_class *bc_impl,
+ const void *key, size_t key_len,
+ const void *iv, size_t tag_len);
+};
+
+/**
+ * \brief Context structure for processing records with CCM.
+ *
+ * The same context structure is used for encrypting and decrypting.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ union {
+ const void *gen;
+ const br_sslrec_in_ccm_class *in;
+ const br_sslrec_out_ccm_class *out;
+ } vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ uint64_t seq;
+ union {
+ const br_block_ctrcbc_class *vtable;
+ br_aes_gen_ctrcbc_keys aes;
+ } bc;
+ unsigned char iv[4];
+ size_t tag_len;
+#endif
+} br_sslrec_ccm_context;
+
+/**
+ * \brief Static, constant vtable for record decryption with CCM.
+ */
+extern const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable;
+
+/**
+ * \brief Static, constant vtable for record encryption with CCM.
+ */
+extern const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable;
+
+/* ===================================================================== */
+
+/**
+ * \brief Type for session parameters, to be saved for session resumption.
+ */
+typedef struct {
+ /** \brief Session ID buffer. */
+ unsigned char session_id[32];
+ /** \brief Session ID length (in bytes, at most 32). */
+ unsigned char session_id_len;
+ /** \brief Protocol version. */
+ uint16_t version;
+ /** \brief Cipher suite. */
+ uint16_t cipher_suite;
+ /** \brief Master secret. */
+ unsigned char master_secret[48];
+} br_ssl_session_parameters;
+
+#ifndef BR_DOXYGEN_IGNORE
+/*
+ * Maximum number of cipher suites supported by a client or server.
+ */
+#define BR_MAX_CIPHER_SUITES 48
+#endif
+
+/**
+ * \brief Context structure for SSL engine.
+ *
+ * This strucuture is common to the client and server; both the client
+ * context (`br_ssl_client_context`) and the server context
+ * (`br_ssl_server_context`) include a `br_ssl_engine_context` as their
+ * first field.
+ *
+ * The engine context manages records, including alerts, closures, and
+ * transitions to new encryption/MAC algorithms. Processing of handshake
+ * records is delegated to externally provided code. This structure
+ * should not be used directly.
+ *
+ * Structure contents are opaque and shall not be accessed directly.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ /*
+ * The error code. When non-zero, then the state is "failed" and
+ * no I/O may occur until reset.
+ */
+ int err;
+
+ /*
+ * Configured I/O buffers. They are either disjoint, or identical.
+ */
+ unsigned char *ibuf, *obuf;
+ size_t ibuf_len, obuf_len;
+
+ /*
+ * Maximum fragment length applies to outgoing records; incoming
+ * records can be processed as long as they fit in the input
+ * buffer. It is guaranteed that incoming records at least as big
+ * as max_frag_len can be processed.
+ */
+ uint16_t max_frag_len;
+ unsigned char log_max_frag_len;
+ unsigned char peer_log_max_frag_len;
+
+ /*
+ * Buffering management registers.
+ */
+ size_t ixa, ixb, ixc;
+ size_t oxa, oxb, oxc;
+ unsigned char iomode;
+ unsigned char incrypt;
+
+ /*
+ * Shutdown flag: when set to non-zero, incoming record bytes
+ * will not be accepted anymore. This is used after a close_notify
+ * has been received: afterwards, the engine no longer claims that
+ * it could receive bytes from the transport medium.
+ */
+ unsigned char shutdown_recv;
+
+ /*
+ * 'record_type_in' is set to the incoming record type when the
+ * record header has been received.
+ * 'record_type_out' is used to make the next outgoing record
+ * header when it is ready to go.
+ */
+ unsigned char record_type_in, record_type_out;
+
+ /*
+ * When a record is received, its version is extracted:
+ * -- if 'version_in' is 0, then it is set to the received version;
+ * -- otherwise, if the received version is not identical to
+ * the 'version_in' contents, then a failure is reported.
+ *
+ * This implements the SSL requirement that all records shall
+ * use the negotiated protocol version, once decided (in the
+ * ServerHello). It is up to the handshake handler to adjust this
+ * field when necessary.
+ */
+ uint16_t version_in;
+
+ /*
+ * 'version_out' is used when the next outgoing record is ready
+ * to go.
+ */
+ uint16_t version_out;
+
+ /*
+ * Record handler contexts.
+ */
+ union {
+ const br_sslrec_in_class *vtable;
+ br_sslrec_in_cbc_context cbc;
+ br_sslrec_gcm_context gcm;
+ br_sslrec_chapol_context chapol;
+ br_sslrec_ccm_context ccm;
+ } in;
+ union {
+ const br_sslrec_out_class *vtable;
+ br_sslrec_out_clear_context clear;
+ br_sslrec_out_cbc_context cbc;
+ br_sslrec_gcm_context gcm;
+ br_sslrec_chapol_context chapol;
+ br_sslrec_ccm_context ccm;
+ } out;
+
+ /*
+ * The "application data" flag. Value:
+ * 0 handshake is in process, no application data acceptable
+ * 1 application data can be sent and received
+ * 2 closing, no application data can be sent, but some
+ * can still be received (and discarded)
+ */
+ unsigned char application_data;
+
+ /*
+ * Context RNG.
+ *
+ * rng_init_done is initially 0. It is set to 1 when the
+ * basic structure of the RNG is set, and 2 when some
+ * entropy has been pushed in. The value 2 marks the RNG
+ * as "properly seeded".
+ *
+ * rng_os_rand_done is initially 0. It is set to 1 when
+ * some seeding from the OS or hardware has been attempted.
+ */
+ br_hmac_drbg_context rng;
+ int rng_init_done;
+ int rng_os_rand_done;
+
+ /*
+ * Supported minimum and maximum versions, and cipher suites.
+ */
+ uint16_t version_min;
+ uint16_t version_max;
+ uint16_t suites_buf[BR_MAX_CIPHER_SUITES];
+ unsigned char suites_num;
+
+ /*
+ * For clients, the server name to send as a SNI extension. For
+ * servers, the name received in the SNI extension (if any).
+ */
+ char server_name[256];
+
+ /*
+ * "Security parameters". These are filled by the handshake
+ * handler, and used when switching encryption state.
+ */
+ unsigned char client_random[32];
+ unsigned char server_random[32];
+ br_ssl_session_parameters session;
+
+ /*
+ * ECDHE elements: curve and point from the peer. The server also
+ * uses that buffer for the point to send to the client.
+ */
+ unsigned char ecdhe_curve;
+ unsigned char ecdhe_point[133];
+ unsigned char ecdhe_point_len;
+
+ /*
+ * Secure renegotiation (RFC 5746): 'reneg' can be:
+ * 0 first handshake (server support is not known)
+ * 1 peer does not support secure renegotiation
+ * 2 peer supports secure renegotiation
+ *
+ * The saved_finished buffer contains the client and the
+ * server "Finished" values from the last handshake, in
+ * that order (12 bytes each).
+ */
+ unsigned char reneg;
+ unsigned char saved_finished[24];
+
+ /*
+ * Behavioural flags.
+ */
+ uint32_t flags;
+
+ /*
+ * Context variables for the handshake processor. The 'pad' must
+ * be large enough to accommodate an RSA-encrypted pre-master
+ * secret, or an RSA signature; since we want to support up to
+ * RSA-4096, this means at least 512 bytes. (Other pad usages
+ * require its length to be at least 256.)
+ */
+ struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+ } cpu;
+ uint32_t dp_stack[32];
+ uint32_t rp_stack[32];
+ unsigned char pad[512];
+ unsigned char *hbuf_in, *hbuf_out, *saved_hbuf_out;
+ size_t hlen_in, hlen_out;
+ void (*hsrun)(void *ctx);
+
+ /*
+ * The 'action' value communicates OOB information between the
+ * engine and the handshake processor.
+ *
+ * From the engine:
+ * 0 invocation triggered by I/O
+ * 1 invocation triggered by explicit close
+ * 2 invocation triggered by explicit renegotiation
+ */
+ unsigned char action;
+
+ /*
+ * State for alert messages. Value is either 0, or the value of
+ * the alert level byte (level is either 1 for warning, or 2 for
+ * fatal; we convert all other values to 'fatal').
+ */
+ unsigned char alert;
+
+ /*
+ * Closure flags. This flag is set when a close_notify has been
+ * received from the peer.
+ */
+ unsigned char close_received;
+
+ /*
+ * Multi-hasher for the handshake messages. The handshake handler
+ * is responsible for resetting it when appropriate.
+ */
+ br_multihash_context mhash;
+
+ /*
+ * Pointer to the X.509 engine. The engine is supposed to be
+ * already initialized. It is used to validate the peer's
+ * certificate.
+ */
+ const br_x509_class **x509ctx;
+
+ /*
+ * Certificate chain to send. This is used by both client and
+ * server, when they send their respective Certificate messages.
+ * If chain_len is 0, then chain may be NULL.
+ */
+ const br_x509_certificate *chain;
+ size_t chain_len;
+ const unsigned char *cert_cur;
+ size_t cert_len;
+
+ /*
+ * List of supported protocol names (ALPN extension). If unset,
+ * (number of names is 0), then:
+ * - the client sends no ALPN extension;
+ * - the server ignores any incoming ALPN extension.
+ *
+ * Otherwise:
+ * - the client sends an ALPN extension with all the names;
+ * - the server selects the first protocol in its list that
+ * the client also supports, or fails (fatal alert 120)
+ * if the client sends an ALPN extension and there is no
+ * match.
+ *
+ * The 'selected_protocol' field contains 1+n if the matching
+ * name has index n in the list (the value is 0 if no match was
+ * performed, e.g. the peer did not send an ALPN extension).
+ */
+ const char **protocol_names;
+ uint16_t protocol_names_num;
+ uint16_t selected_protocol;
+
+ /*
+ * Pointers to implementations; left to NULL for unsupported
+ * functions. For the raw hash functions, implementations are
+ * referenced from the multihasher (mhash field).
+ */
+ br_tls_prf_impl prf10;
+ br_tls_prf_impl prf_sha256;
+ br_tls_prf_impl prf_sha384;
+ const br_block_cbcenc_class *iaes_cbcenc;
+ const br_block_cbcdec_class *iaes_cbcdec;
+ const br_block_ctr_class *iaes_ctr;
+ const br_block_ctrcbc_class *iaes_ctrcbc;
+ const br_block_cbcenc_class *ides_cbcenc;
+ const br_block_cbcdec_class *ides_cbcdec;
+ br_ghash ighash;
+ br_chacha20_run ichacha;
+ br_poly1305_run ipoly;
+ const br_sslrec_in_cbc_class *icbc_in;
+ const br_sslrec_out_cbc_class *icbc_out;
+ const br_sslrec_in_gcm_class *igcm_in;
+ const br_sslrec_out_gcm_class *igcm_out;
+ const br_sslrec_in_chapol_class *ichapol_in;
+ const br_sslrec_out_chapol_class *ichapol_out;
+ const br_sslrec_in_ccm_class *iccm_in;
+ const br_sslrec_out_ccm_class *iccm_out;
+ const br_ec_impl *iec;
+ br_rsa_pkcs1_vrfy irsavrfy;
+ br_ecdsa_vrfy iecdsa;
+#endif
+} br_ssl_engine_context;
+
+/**
+ * \brief Get currently defined engine behavioural flags.
+ *
+ * \param cc SSL engine context.
+ * \return the flags.
+ */
+static inline uint32_t
+br_ssl_engine_get_flags(br_ssl_engine_context *cc)
+{
+ return cc->flags;
+}
+
+/**
+ * \brief Set all engine behavioural flags.
+ *
+ * \param cc SSL engine context.
+ * \param flags new value for all flags.
+ */
+static inline void
+br_ssl_engine_set_all_flags(br_ssl_engine_context *cc, uint32_t flags)
+{
+ cc->flags = flags;
+}
+
+/**
+ * \brief Set some engine behavioural flags.
+ *
+ * The flags set in the `flags` parameter are set in the context; other
+ * flags are untouched.
+ *
+ * \param cc SSL engine context.
+ * \param flags additional set flags.
+ */
+static inline void
+br_ssl_engine_add_flags(br_ssl_engine_context *cc, uint32_t flags)
+{
+ cc->flags |= flags;
+}
+
+/**
+ * \brief Clear some engine behavioural flags.
+ *
+ * The flags set in the `flags` parameter are cleared from the context; other
+ * flags are untouched.
+ *
+ * \param cc SSL engine context.
+ * \param flags flags to remove.
+ */
+static inline void
+br_ssl_engine_remove_flags(br_ssl_engine_context *cc, uint32_t flags)
+{
+ cc->flags &= ~flags;
+}
+
+/**
+ * \brief Behavioural flag: enforce server preferences.
+ *
+ * If this flag is set, then the server will enforce its own cipher suite
+ * preference order; otherwise, it follows the client preferences.
+ */
+#define BR_OPT_ENFORCE_SERVER_PREFERENCES ((uint32_t)1 << 0)
+
+/**
+ * \brief Behavioural flag: disable renegotiation.
+ *
+ * If this flag is set, then renegotiations are rejected unconditionally:
+ * they won't be honoured if asked for programmatically, and requests from
+ * the peer are rejected.
+ */
+#define BR_OPT_NO_RENEGOTIATION ((uint32_t)1 << 1)
+
+/**
+ * \brief Behavioural flag: tolerate lack of client authentication.
+ *
+ * If this flag is set in a server and the server requests a client
+ * certificate, but the authentication fails (the client does not send
+ * a certificate, or the client's certificate chain cannot be validated),
+ * then the connection keeps on. Without this flag, a failed client
+ * authentication terminates the connection.
+ *
+ * Notes:
+ *
+ * - If the client's certificate can be validated and its public key is
+ * supported, then a wrong signature value terminates the connection
+ * regardless of that flag.
+ *
+ * - If using full-static ECDH, then a failure to validate the client's
+ * certificate prevents the handshake from succeeding.
+ */
+#define BR_OPT_TOLERATE_NO_CLIENT_AUTH ((uint32_t)1 << 2)
+
+/**
+ * \brief Behavioural flag: fail on application protocol mismatch.
+ *
+ * The ALPN extension ([RFC 7301](https://tools.ietf.org/html/rfc7301))
+ * allows the client to send a list of application protocol names, and
+ * the server to select one. A mismatch is one of the following occurrences:
+ *
+ * - On the client: the client sends a list of names, the server
+ * responds with a protocol name which is _not_ part of the list of
+ * names sent by the client.
+ *
+ * - On the server: the client sends a list of names, and the server
+ * is also configured with a list of names, but there is no common
+ * protocol name between the two lists.
+ *
+ * Normal behaviour in case of mismatch is to report no matching name
+ * (`br_ssl_engine_get_selected_protocol()` returns `NULL`) and carry on.
+ * If the flag is set, then a mismatch implies a protocol failure (if
+ * the mismatch is detected by the server, it will send a fatal alert).
+ *
+ * Note: even with this flag, `br_ssl_engine_get_selected_protocol()`
+ * may still return `NULL` if the client or the server does not send an
+ * ALPN extension at all.
+ */
+#define BR_OPT_FAIL_ON_ALPN_MISMATCH ((uint32_t)1 << 3)
+
+/**
+ * \brief Set the minimum and maximum supported protocol versions.
+ *
+ * The two provided versions MUST be supported by the implementation
+ * (i.e. TLS 1.0, 1.1 and 1.2), and `version_max` MUST NOT be lower
+ * than `version_min`.
+ *
+ * \param cc SSL engine context.
+ * \param version_min minimum supported TLS version.
+ * \param version_max maximum supported TLS version.
+ */
+static inline void
+br_ssl_engine_set_versions(br_ssl_engine_context *cc,
+ unsigned version_min, unsigned version_max)
+{
+ cc->version_min = version_min;
+ cc->version_max = version_max;
+}
+
+/**
+ * \brief Set the list of cipher suites advertised by this context.
+ *
+ * The provided array is copied into the context. It is the caller
+ * responsibility to ensure that all provided suites will be supported
+ * by the context. The engine context has enough room to receive _all_
+ * suites supported by the implementation. The provided array MUST NOT
+ * contain duplicates.
+ *
+ * If the engine is for a client, the "signaling" pseudo-cipher suite
+ * `TLS_FALLBACK_SCSV` can be added at the end of the list, if the
+ * calling application is performing a voluntary downgrade (voluntary
+ * downgrades are not recommended, but if such a downgrade is done, then
+ * adding the fallback pseudo-suite is a good idea).
+ *
+ * \param cc SSL engine context.
+ * \param suites cipher suites.
+ * \param suites_num number of cipher suites.
+ */
+void br_ssl_engine_set_suites(br_ssl_engine_context *cc,
+ const uint16_t *suites, size_t suites_num);
+
+/**
+ * \brief Set the X.509 engine.
+ *
+ * The caller shall ensure that the X.509 engine is properly initialised.
+ *
+ * \param cc SSL engine context.
+ * \param x509ctx X.509 certificate validation context.
+ */
+static inline void
+br_ssl_engine_set_x509(br_ssl_engine_context *cc, const br_x509_class **x509ctx)
+{
+ cc->x509ctx = x509ctx;
+}
+
+/**
+ * \brief Set the supported protocol names.
+ *
+ * Protocol names are part of the ALPN extension ([RFC
+ * 7301](https://tools.ietf.org/html/rfc7301)). Each protocol name is a
+ * character string, containing no more than 255 characters (256 with the
+ * terminating zero). When names are set, then:
+ *
+ * - The client will send an ALPN extension, containing the names. If
+ * the server responds with an ALPN extension, the client will verify
+ * that the response contains one of its name, and report that name
+ * through `br_ssl_engine_get_selected_protocol()`.
+ *
+ * - The server will parse incoming ALPN extension (from clients), and
+ * try to find a common protocol; if none is found, the connection
+ * is aborted with a fatal alert. On match, a response ALPN extension
+ * is sent, and name is reported through
+ * `br_ssl_engine_get_selected_protocol()`.
+ *
+ * The provided array is linked in, and must remain valid while the
+ * connection is live.
+ *
+ * Names MUST NOT be empty. Names MUST NOT be longer than 255 characters
+ * (excluding the terminating 0).
+ *
+ * \param ctx SSL engine context.
+ * \param names list of protocol names (zero-terminated).
+ * \param num number of protocol names (MUST be 1 or more).
+ */
+static inline void
+br_ssl_engine_set_protocol_names(br_ssl_engine_context *ctx,
+ const char **names, size_t num)
+{
+ ctx->protocol_names = names;
+ ctx->protocol_names_num = num;
+}
+
+/**
+ * \brief Get the selected protocol.
+ *
+ * If this context was initialised with a non-empty list of protocol
+ * names, and both client and server sent ALPN extensions during the
+ * handshake, and a common name was found, then that name is returned.
+ * Otherwise, `NULL` is returned.
+ *
+ * The returned pointer is one of the pointers provided to the context
+ * with `br_ssl_engine_set_protocol_names()`.
+ *
+ * \return the selected protocol, or `NULL`.
+ */
+static inline const char *
+br_ssl_engine_get_selected_protocol(br_ssl_engine_context *ctx)
+{
+ unsigned k;
+
+ k = ctx->selected_protocol;
+ return (k == 0 || k == 0xFFFF) ? NULL : ctx->protocol_names[k - 1];
+}
+
+/**
+ * \brief Set a hash function implementation (by ID).
+ *
+ * Hash functions set with this call will be used for SSL/TLS specific
+ * usages, not X.509 certificate validation. Only "standard" hash functions
+ * may be set (MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512). If `impl`
+ * is `NULL`, then the hash function support is removed, not added.
+ *
+ * \param ctx SSL engine context.
+ * \param id hash function identifier.
+ * \param impl hash function implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_hash(br_ssl_engine_context *ctx,
+ int id, const br_hash_class *impl)
+{
+ br_multihash_setimpl(&ctx->mhash, id, impl);
+}
+
+/**
+ * \brief Get a hash function implementation (by ID).
+ *
+ * This function retrieves a hash function implementation which was
+ * set with `br_ssl_engine_set_hash()`.
+ *
+ * \param ctx SSL engine context.
+ * \param id hash function identifier.
+ * \return the hash function implementation (or `NULL`).
+ */
+static inline const br_hash_class *
+br_ssl_engine_get_hash(br_ssl_engine_context *ctx, int id)
+{
+ return br_multihash_getimpl(&ctx->mhash, id);
+}
+
+/**
+ * \brief Set the PRF implementation (for TLS 1.0 and 1.1).
+ *
+ * This function sets (or removes, if `impl` is `NULL`) the implementation
+ * for the PRF used in TLS 1.0 and 1.1.
+ *
+ * \param cc SSL engine context.
+ * \param impl PRF implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_prf10(br_ssl_engine_context *cc, br_tls_prf_impl impl)
+{
+ cc->prf10 = impl;
+}
+
+/**
+ * \brief Set the PRF implementation with SHA-256 (for TLS 1.2).
+ *
+ * This function sets (or removes, if `impl` is `NULL`) the implementation
+ * for the SHA-256 variant of the PRF used in TLS 1.2.
+ *
+ * \param cc SSL engine context.
+ * \param impl PRF implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_prf_sha256(br_ssl_engine_context *cc, br_tls_prf_impl impl)
+{
+ cc->prf_sha256 = impl;
+}
+
+/**
+ * \brief Set the PRF implementation with SHA-384 (for TLS 1.2).
+ *
+ * This function sets (or removes, if `impl` is `NULL`) the implementation
+ * for the SHA-384 variant of the PRF used in TLS 1.2.
+ *
+ * \param cc SSL engine context.
+ * \param impl PRF implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_prf_sha384(br_ssl_engine_context *cc, br_tls_prf_impl impl)
+{
+ cc->prf_sha384 = impl;
+}
+
+/**
+ * \brief Set the AES/CBC implementations.
+ *
+ * \param cc SSL engine context.
+ * \param impl_enc AES/CBC encryption implementation (or `NULL`).
+ * \param impl_dec AES/CBC decryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_aes_cbc(br_ssl_engine_context *cc,
+ const br_block_cbcenc_class *impl_enc,
+ const br_block_cbcdec_class *impl_dec)
+{
+ cc->iaes_cbcenc = impl_enc;
+ cc->iaes_cbcdec = impl_dec;
+}
+
+/**
+ * \brief Set the "default" AES/CBC implementations.
+ *
+ * This function configures in the engine the AES implementations that
+ * should provide best runtime performance on the local system, while
+ * still being safe (in particular, constant-time). It also sets the
+ * handlers for CBC records.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context *cc);
+
+/**
+ * \brief Set the AES/CTR implementation.
+ *
+ * \param cc SSL engine context.
+ * \param impl AES/CTR encryption/decryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_aes_ctr(br_ssl_engine_context *cc,
+ const br_block_ctr_class *impl)
+{
+ cc->iaes_ctr = impl;
+}
+
+/**
+ * \brief Set the "default" implementations for AES/GCM (AES/CTR + GHASH).
+ *
+ * This function configures in the engine the AES/CTR and GHASH
+ * implementation that should provide best runtime performance on the local
+ * system, while still being safe (in particular, constant-time). It also
+ * sets the handlers for GCM records.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context *cc);
+
+/**
+ * \brief Set the DES/CBC implementations.
+ *
+ * \param cc SSL engine context.
+ * \param impl_enc DES/CBC encryption implementation (or `NULL`).
+ * \param impl_dec DES/CBC decryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_des_cbc(br_ssl_engine_context *cc,
+ const br_block_cbcenc_class *impl_enc,
+ const br_block_cbcdec_class *impl_dec)
+{
+ cc->ides_cbcenc = impl_enc;
+ cc->ides_cbcdec = impl_dec;
+}
+
+/**
+ * \brief Set the "default" DES/CBC implementations.
+ *
+ * This function configures in the engine the DES implementations that
+ * should provide best runtime performance on the local system, while
+ * still being safe (in particular, constant-time). It also sets the
+ * handlers for CBC records.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_des_cbc(br_ssl_engine_context *cc);
+
+/**
+ * \brief Set the GHASH implementation (used in GCM mode).
+ *
+ * \param cc SSL engine context.
+ * \param impl GHASH implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_ghash(br_ssl_engine_context *cc, br_ghash impl)
+{
+ cc->ighash = impl;
+}
+
+/**
+ * \brief Set the ChaCha20 implementation.
+ *
+ * \param cc SSL engine context.
+ * \param ichacha ChaCha20 implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_chacha20(br_ssl_engine_context *cc,
+ br_chacha20_run ichacha)
+{
+ cc->ichacha = ichacha;
+}
+
+/**
+ * \brief Set the Poly1305 implementation.
+ *
+ * \param cc SSL engine context.
+ * \param ipoly Poly1305 implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_poly1305(br_ssl_engine_context *cc,
+ br_poly1305_run ipoly)
+{
+ cc->ipoly = ipoly;
+}
+
+/**
+ * \brief Set the "default" ChaCha20 and Poly1305 implementations.
+ *
+ * This function configures in the engine the ChaCha20 and Poly1305
+ * implementations that should provide best runtime performance on the
+ * local system, while still being safe (in particular, constant-time).
+ * It also sets the handlers for ChaCha20+Poly1305 records.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_chapol(br_ssl_engine_context *cc);
+
+/**
+ * \brief Set the AES/CTR+CBC implementation.
+ *
+ * \param cc SSL engine context.
+ * \param impl AES/CTR+CBC encryption/decryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_aes_ctrcbc(br_ssl_engine_context *cc,
+ const br_block_ctrcbc_class *impl)
+{
+ cc->iaes_ctrcbc = impl;
+}
+
+/**
+ * \brief Set the "default" implementations for AES/CCM.
+ *
+ * This function configures in the engine the AES/CTR+CBC
+ * implementation that should provide best runtime performance on the local
+ * system, while still being safe (in particular, constant-time). It also
+ * sets the handlers for CCM records.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_aes_ccm(br_ssl_engine_context *cc);
+
+/**
+ * \brief Set the record encryption and decryption engines for CBC + HMAC.
+ *
+ * \param cc SSL engine context.
+ * \param impl_in record CBC decryption implementation (or `NULL`).
+ * \param impl_out record CBC encryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_cbc(br_ssl_engine_context *cc,
+ const br_sslrec_in_cbc_class *impl_in,
+ const br_sslrec_out_cbc_class *impl_out)
+{
+ cc->icbc_in = impl_in;
+ cc->icbc_out = impl_out;
+}
+
+/**
+ * \brief Set the record encryption and decryption engines for GCM.
+ *
+ * \param cc SSL engine context.
+ * \param impl_in record GCM decryption implementation (or `NULL`).
+ * \param impl_out record GCM encryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_gcm(br_ssl_engine_context *cc,
+ const br_sslrec_in_gcm_class *impl_in,
+ const br_sslrec_out_gcm_class *impl_out)
+{
+ cc->igcm_in = impl_in;
+ cc->igcm_out = impl_out;
+}
+
+/**
+ * \brief Set the record encryption and decryption engines for CCM.
+ *
+ * \param cc SSL engine context.
+ * \param impl_in record CCM decryption implementation (or `NULL`).
+ * \param impl_out record CCM encryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_ccm(br_ssl_engine_context *cc,
+ const br_sslrec_in_ccm_class *impl_in,
+ const br_sslrec_out_ccm_class *impl_out)
+{
+ cc->iccm_in = impl_in;
+ cc->iccm_out = impl_out;
+}
+
+/**
+ * \brief Set the record encryption and decryption engines for
+ * ChaCha20+Poly1305.
+ *
+ * \param cc SSL engine context.
+ * \param impl_in record ChaCha20 decryption implementation (or `NULL`).
+ * \param impl_out record ChaCha20 encryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_chapol(br_ssl_engine_context *cc,
+ const br_sslrec_in_chapol_class *impl_in,
+ const br_sslrec_out_chapol_class *impl_out)
+{
+ cc->ichapol_in = impl_in;
+ cc->ichapol_out = impl_out;
+}
+
+/**
+ * \brief Set the EC implementation.
+ *
+ * The elliptic curve implementation will be used for ECDH and ECDHE
+ * cipher suites, and for ECDSA support.
+ *
+ * \param cc SSL engine context.
+ * \param iec EC implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_ec(br_ssl_engine_context *cc, const br_ec_impl *iec)
+{
+ cc->iec = iec;
+}
+
+/**
+ * \brief Set the "default" EC implementation.
+ *
+ * This function sets the elliptic curve implementation for ECDH and
+ * ECDHE cipher suites, and for ECDSA support. It selects the fastest
+ * implementation on the current system.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_ec(br_ssl_engine_context *cc);
+
+/**
+ * \brief Get the EC implementation configured in the provided engine.
+ *
+ * \param cc SSL engine context.
+ * \return the EC implementation.
+ */
+static inline const br_ec_impl *
+br_ssl_engine_get_ec(br_ssl_engine_context *cc)
+{
+ return cc->iec;
+}
+
+/**
+ * \brief Set the RSA signature verification implementation.
+ *
+ * On the client, this is used to verify the server's signature on its
+ * ServerKeyExchange message (for ECDHE_RSA cipher suites). On the server,
+ * this is used to verify the client's CertificateVerify message (if a
+ * client certificate is requested, and that certificate contains a RSA key).
+ *
+ * \param cc SSL engine context.
+ * \param irsavrfy RSA signature verification implementation.
+ */
+static inline void
+br_ssl_engine_set_rsavrfy(br_ssl_engine_context *cc, br_rsa_pkcs1_vrfy irsavrfy)
+{
+ cc->irsavrfy = irsavrfy;
+}
+
+/**
+ * \brief Set the "default" RSA implementation (signature verification).
+ *
+ * This function sets the RSA implementation (signature verification)
+ * to the fastest implementation available on the current platform.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context *cc);
+
+/**
+ * \brief Get the RSA implementation (signature verification) configured
+ * in the provided engine.
+ *
+ * \param cc SSL engine context.
+ * \return the RSA signature verification implementation.
+ */
+static inline br_rsa_pkcs1_vrfy
+br_ssl_engine_get_rsavrfy(br_ssl_engine_context *cc)
+{
+ return cc->irsavrfy;
+}
+
+/*
+ * \brief Set the ECDSA implementation (signature verification).
+ *
+ * On the client, this is used to verify the server's signature on its
+ * ServerKeyExchange message (for ECDHE_ECDSA cipher suites). On the server,
+ * this is used to verify the client's CertificateVerify message (if a
+ * client certificate is requested, that certificate contains an EC key,
+ * and full-static ECDH is not used).
+ *
+ * The ECDSA implementation will use the EC core implementation configured
+ * in the engine context.
+ *
+ * \param cc client context.
+ * \param iecdsa ECDSA verification implementation.
+ */
+static inline void
+br_ssl_engine_set_ecdsa(br_ssl_engine_context *cc, br_ecdsa_vrfy iecdsa)
+{
+ cc->iecdsa = iecdsa;
+}
+
+/**
+ * \brief Set the "default" ECDSA implementation (signature verification).
+ *
+ * This function sets the ECDSA implementation (signature verification)
+ * to the fastest implementation available on the current platform. This
+ * call also sets the elliptic curve implementation itself, there again
+ * to the fastest EC implementation available.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_set_default_ecdsa(br_ssl_engine_context *cc);
+
+/**
+ * \brief Get the ECDSA implementation (signature verification) configured
+ * in the provided engine.
+ *
+ * \param cc SSL engine context.
+ * \return the ECDSA signature verification implementation.
+ */
+static inline br_ecdsa_vrfy
+br_ssl_engine_get_ecdsa(br_ssl_engine_context *cc)
+{
+ return cc->iecdsa;
+}
+
+/**
+ * \brief Set the I/O buffer for the SSL engine.
+ *
+ * Once this call has been made, `br_ssl_client_reset()` or
+ * `br_ssl_server_reset()` MUST be called before using the context.
+ *
+ * The provided buffer will be used as long as the engine context is
+ * used. The caller is responsible for keeping it available.
+ *
+ * If `bidi` is 0, then the engine will operate in half-duplex mode
+ * (it won't be able to send data while there is unprocessed incoming
+ * data in the buffer, and it won't be able to receive data while there
+ * is unsent data in the buffer). The optimal buffer size in half-duplex
+ * mode is `BR_SSL_BUFSIZE_MONO`; if the buffer is larger, then extra
+ * bytes are ignored. If the buffer is smaller, then this limits the
+ * capacity of the engine to support all allowed record sizes.
+ *
+ * If `bidi` is 1, then the engine will split the buffer into two
+ * parts, for separate handling of outgoing and incoming data. This
+ * enables full-duplex processing, but requires more RAM. The optimal
+ * buffer size in full-duplex mode is `BR_SSL_BUFSIZE_BIDI`; if the
+ * buffer is larger, then extra bytes are ignored. If the buffer is
+ * smaller, then the split will favour the incoming part, so that
+ * interoperability is maximised.
+ *
+ * \param cc SSL engine context
+ * \param iobuf I/O buffer.
+ * \param iobuf_len I/O buffer length (in bytes).
+ * \param bidi non-zero for full-duplex mode.
+ */
+void br_ssl_engine_set_buffer(br_ssl_engine_context *cc,
+ void *iobuf, size_t iobuf_len, int bidi);
+
+/**
+ * \brief Set the I/O buffers for the SSL engine.
+ *
+ * Once this call has been made, `br_ssl_client_reset()` or
+ * `br_ssl_server_reset()` MUST be called before using the context.
+ *
+ * This function is similar to `br_ssl_engine_set_buffer()`, except
+ * that it enforces full-duplex mode, and the two I/O buffers are
+ * provided as separate chunks.
+ *
+ * The macros `BR_SSL_BUFSIZE_INPUT` and `BR_SSL_BUFSIZE_OUTPUT`
+ * evaluate to the optimal (maximum) sizes for the input and output
+ * buffer, respectively.
+ *
+ * \param cc SSL engine context
+ * \param ibuf input buffer.
+ * \param ibuf_len input buffer length (in bytes).
+ * \param obuf output buffer.
+ * \param obuf_len output buffer length (in bytes).
+ */
+void br_ssl_engine_set_buffers_bidi(br_ssl_engine_context *cc,
+ void *ibuf, size_t ibuf_len, void *obuf, size_t obuf_len);
+
+/**
+ * \brief Inject some "initial entropy" in the context.
+ *
+ * This entropy will be added to what can be obtained from the
+ * underlying operating system, if that OS is supported.
+ *
+ * This function may be called several times; all injected entropy chunks
+ * are cumulatively mixed.
+ *
+ * If entropy gathering from the OS is supported and compiled in, then this
+ * step is optional. Otherwise, it is mandatory to inject randomness, and
+ * the caller MUST take care to push (as one or several successive calls)
+ * enough entropy to achieve cryptographic resistance (at least 80 bits,
+ * preferably 128 or more). The engine will report an error if no entropy
+ * was provided and none can be obtained from the OS.
+ *
+ * Take care that this function cannot assess the cryptographic quality of
+ * the provided bytes.
+ *
+ * In all generality, "entropy" must here be considered to mean "that
+ * which the attacker cannot predict". If your OS/architecture does not
+ * have a suitable source of randomness, then you can make do with the
+ * combination of a large enough secret value (possibly a copy of an
+ * asymmetric private key that you also store on the system) AND a
+ * non-repeating value (e.g. current time, provided that the local clock
+ * cannot be reset or altered by the attacker).
+ *
+ * \param cc SSL engine context.
+ * \param data extra entropy to inject.
+ * \param len length of the extra data (in bytes).
+ */
+void br_ssl_engine_inject_entropy(br_ssl_engine_context *cc,
+ const void *data, size_t len);
+
+/**
+ * \brief Get the "server name" in this engine.
+ *
+ * For clients, this is the name provided with `br_ssl_client_reset()`;
+ * for servers, this is the name received from the client as part of the
+ * ClientHello message. If there is no such name (e.g. the client did
+ * not send an SNI extension) then the returned string is empty
+ * (returned pointer points to a byte of value 0).
+ *
+ * The returned pointer refers to a buffer inside the context, which may
+ * be overwritten as part of normal SSL activity (even within the same
+ * connection, if a renegotiation occurs).
+ *
+ * \param cc SSL engine context.
+ * \return the server name (possibly empty).
+ */
+static inline const char *
+br_ssl_engine_get_server_name(const br_ssl_engine_context *cc)
+{
+ return cc->server_name;
+}
+
+/**
+ * \brief Get the protocol version.
+ *
+ * This function returns the protocol version that is used by the
+ * engine. That value is set after sending (for a server) or receiving
+ * (for a client) the ServerHello message.
+ *
+ * \param cc SSL engine context.
+ * \return the protocol version.
+ */
+static inline unsigned
+br_ssl_engine_get_version(const br_ssl_engine_context *cc)
+{
+ return cc->session.version;
+}
+
+/**
+ * \brief Get a copy of the session parameters.
+ *
+ * The session parameters are filled during the handshake, so this
+ * function shall not be called before completion of the handshake.
+ * The initial handshake is completed when the context first allows
+ * application data to be injected.
+ *
+ * This function copies the current session parameters into the provided
+ * structure. Beware that the session parameters include the master
+ * secret, which is sensitive data, to handle with great care.
+ *
+ * \param cc SSL engine context.
+ * \param pp destination structure for the session parameters.
+ */
+static inline void
+br_ssl_engine_get_session_parameters(const br_ssl_engine_context *cc,
+ br_ssl_session_parameters *pp)
+{
+ memcpy(pp, &cc->session, sizeof *pp);
+}
+
+/**
+ * \brief Set the session parameters to the provided values.
+ *
+ * This function is meant to be used in the client, before doing a new
+ * handshake; a session resumption will be attempted with these
+ * parameters. In the server, this function has no effect.
+ *
+ * \param cc SSL engine context.
+ * \param pp source structure for the session parameters.
+ */
+static inline void
+br_ssl_engine_set_session_parameters(br_ssl_engine_context *cc,
+ const br_ssl_session_parameters *pp)
+{
+ memcpy(&cc->session, pp, sizeof *pp);
+}
+
+/**
+ * \brief Get identifier for the curve used for key exchange.
+ *
+ * If the cipher suite uses ECDHE, then this function returns the
+ * identifier for the curve used for transient parameters. This is
+ * defined during the course of the handshake, when the ServerKeyExchange
+ * is sent (on the server) or received (on the client). If the
+ * cipher suite does not use ECDHE (e.g. static ECDH, or RSA key
+ * exchange), then this value is indeterminate.
+ *
+ * @param cc SSL engine context.
+ * @return the ECDHE curve identifier.
+ */
+static inline int
+br_ssl_engine_get_ecdhe_curve(br_ssl_engine_context *cc)
+{
+ return cc->ecdhe_curve;
+}
+
+/**
+ * \brief Get the current engine state.
+ *
+ * An SSL engine (client or server) has, at any time, a state which is
+ * the combination of zero, one or more of these flags:
+ *
+ * - `BR_SSL_CLOSED`
+ *
+ * Engine is finished, no more I/O (until next reset).
+ *
+ * - `BR_SSL_SENDREC`
+ *
+ * Engine has some bytes to send to the peer.
+ *
+ * - `BR_SSL_RECVREC`
+ *
+ * Engine expects some bytes from the peer.
+ *
+ * - `BR_SSL_SENDAPP`
+ *
+ * Engine may receive application data to send (or flush).
+ *
+ * - `BR_SSL_RECVAPP`
+ *
+ * Engine has obtained some application data from the peer,
+ * that should be read by the caller.
+ *
+ * If no flag at all is set (state value is 0), then the engine is not
+ * fully initialised yet.
+ *
+ * The `BR_SSL_CLOSED` flag is exclusive; when it is set, no other flag
+ * is set. To distinguish between a normal closure and an error, use
+ * `br_ssl_engine_last_error()`.
+ *
+ * Generally speaking, `BR_SSL_SENDREC` and `BR_SSL_SENDAPP` are mutually
+ * exclusive: the input buffer, at any point, either accumulates
+ * plaintext data, or contains an assembled record that is being sent.
+ * Similarly, `BR_SSL_RECVREC` and `BR_SSL_RECVAPP` are mutually exclusive.
+ * This may change in a future library version.
+ *
+ * \param cc SSL engine context.
+ * \return the current engine state.
+ */
+unsigned br_ssl_engine_current_state(const br_ssl_engine_context *cc);
+
+/** \brief SSL engine state: closed or failed. */
+#define BR_SSL_CLOSED 0x0001
+/** \brief SSL engine state: record data is ready to be sent to the peer. */
+#define BR_SSL_SENDREC 0x0002
+/** \brief SSL engine state: engine may receive records from the peer. */
+#define BR_SSL_RECVREC 0x0004
+/** \brief SSL engine state: engine may accept application data to send. */
+#define BR_SSL_SENDAPP 0x0008
+/** \brief SSL engine state: engine has received application data. */
+#define BR_SSL_RECVAPP 0x0010
+
+/**
+ * \brief Get the engine error indicator.
+ *
+ * The error indicator is `BR_ERR_OK` (0) if no error was encountered
+ * since the last call to `br_ssl_client_reset()` or
+ * `br_ssl_server_reset()`. Other status values are "sticky": they
+ * remain set, and prevent all I/O activity, until cleared. Only the
+ * reset calls clear the error indicator.
+ *
+ * \param cc SSL engine context.
+ * \return 0, or a non-zero error code.
+ */
+static inline int
+br_ssl_engine_last_error(const br_ssl_engine_context *cc)
+{
+ return cc->err;
+}
+
+/*
+ * There are four I/O operations, each identified by a symbolic name:
+ *
+ * sendapp inject application data in the engine
+ * recvapp retrieving application data from the engine
+ * sendrec sending records on the transport medium
+ * recvrec receiving records from the transport medium
+ *
+ * Terminology works thus: in a layered model where the SSL engine sits
+ * between the application and the network, "send" designates operations
+ * where bytes flow from application to network, and "recv" for the
+ * reverse operation. Application data (the plaintext that is to be
+ * conveyed through SSL) is "app", while encrypted records are "rec".
+ * Note that from the SSL engine point of view, "sendapp" and "recvrec"
+ * designate bytes that enter the engine ("inject" operation), while
+ * "recvapp" and "sendrec" designate bytes that exit the engine
+ * ("extract" operation).
+ *
+ * For the operation 'xxx', two functions are defined:
+ *
+ * br_ssl_engine_xxx_buf
+ * Returns a pointer and length to the buffer to use for that
+ * operation. '*len' is set to the number of bytes that may be read
+ * from the buffer (extract operation) or written to the buffer
+ * (inject operation). If no byte may be exchanged for that operation
+ * at that point, then '*len' is set to zero, and NULL is returned.
+ * The engine state is unmodified by this call.
+ *
+ * br_ssl_engine_xxx_ack
+ * Informs the engine that 'len' bytes have been read from the buffer
+ * (extract operation) or written to the buffer (inject operation).
+ * The 'len' value MUST NOT be zero. The 'len' value MUST NOT exceed
+ * that which was obtained from a preceding br_ssl_engine_xxx_buf()
+ * call.
+ */
+
+/**
+ * \brief Get buffer for application data to send.
+ *
+ * If the engine is ready to accept application data to send to the
+ * peer, then this call returns a pointer to the buffer where such
+ * data shall be written, and its length is written in `*len`.
+ * Otherwise, `*len` is set to 0 and `NULL` is returned.
+ *
+ * \param cc SSL engine context.
+ * \param len receives the application data output buffer length, or 0.
+ * \return the application data output buffer, or `NULL`.
+ */
+unsigned char *br_ssl_engine_sendapp_buf(
+ const br_ssl_engine_context *cc, size_t *len);
+
+/**
+ * \brief Inform the engine of some new application data.
+ *
+ * After writing `len` bytes in the buffer returned by
+ * `br_ssl_engine_sendapp_buf()`, the application shall call this
+ * function to trigger any relevant processing. The `len` parameter
+ * MUST NOT be 0, and MUST NOT exceed the value obtained in the
+ * `br_ssl_engine_sendapp_buf()` call.
+ *
+ * \param cc SSL engine context.
+ * \param len number of bytes pushed (not zero).
+ */
+void br_ssl_engine_sendapp_ack(br_ssl_engine_context *cc, size_t len);
+
+/**
+ * \brief Get buffer for received application data.
+ *
+ * If the engine has received application data from the peer, hen this
+ * call returns a pointer to the buffer from where such data shall be
+ * read, and its length is written in `*len`. Otherwise, `*len` is set
+ * to 0 and `NULL` is returned.
+ *
+ * \param cc SSL engine context.
+ * \param len receives the application data input buffer length, or 0.
+ * \return the application data input buffer, or `NULL`.
+ */
+unsigned char *br_ssl_engine_recvapp_buf(
+ const br_ssl_engine_context *cc, size_t *len);
+
+/**
+ * \brief Acknowledge some received application data.
+ *
+ * After reading `len` bytes from the buffer returned by
+ * `br_ssl_engine_recvapp_buf()`, the application shall call this
+ * function to trigger any relevant processing. The `len` parameter
+ * MUST NOT be 0, and MUST NOT exceed the value obtained in the
+ * `br_ssl_engine_recvapp_buf()` call.
+ *
+ * \param cc SSL engine context.
+ * \param len number of bytes read (not zero).
+ */
+void br_ssl_engine_recvapp_ack(br_ssl_engine_context *cc, size_t len);
+
+/**
+ * \brief Get buffer for record data to send.
+ *
+ * If the engine has prepared some records to send to the peer, then this
+ * call returns a pointer to the buffer from where such data shall be
+ * read, and its length is written in `*len`. Otherwise, `*len` is set
+ * to 0 and `NULL` is returned.
+ *
+ * \param cc SSL engine context.
+ * \param len receives the record data output buffer length, or 0.
+ * \return the record data output buffer, or `NULL`.
+ */
+unsigned char *br_ssl_engine_sendrec_buf(
+ const br_ssl_engine_context *cc, size_t *len);
+
+/**
+ * \brief Acknowledge some sent record data.
+ *
+ * After reading `len` bytes from the buffer returned by
+ * `br_ssl_engine_sendrec_buf()`, the application shall call this
+ * function to trigger any relevant processing. The `len` parameter
+ * MUST NOT be 0, and MUST NOT exceed the value obtained in the
+ * `br_ssl_engine_sendrec_buf()` call.
+ *
+ * \param cc SSL engine context.
+ * \param len number of bytes read (not zero).
+ */
+void br_ssl_engine_sendrec_ack(br_ssl_engine_context *cc, size_t len);
+
+/**
+ * \brief Get buffer for incoming records.
+ *
+ * If the engine is ready to accept records from the peer, then this
+ * call returns a pointer to the buffer where such data shall be
+ * written, and its length is written in `*len`. Otherwise, `*len` is
+ * set to 0 and `NULL` is returned.
+ *
+ * \param cc SSL engine context.
+ * \param len receives the record data input buffer length, or 0.
+ * \return the record data input buffer, or `NULL`.
+ */
+unsigned char *br_ssl_engine_recvrec_buf(
+ const br_ssl_engine_context *cc, size_t *len);
+
+/**
+ * \brief Inform the engine of some new record data.
+ *
+ * After writing `len` bytes in the buffer returned by
+ * `br_ssl_engine_recvrec_buf()`, the application shall call this
+ * function to trigger any relevant processing. The `len` parameter
+ * MUST NOT be 0, and MUST NOT exceed the value obtained in the
+ * `br_ssl_engine_recvrec_buf()` call.
+ *
+ * \param cc SSL engine context.
+ * \param len number of bytes pushed (not zero).
+ */
+void br_ssl_engine_recvrec_ack(br_ssl_engine_context *cc, size_t len);
+
+/**
+ * \brief Flush buffered application data.
+ *
+ * If some application data has been buffered in the engine, then wrap
+ * it into a record and mark it for sending. If no application data has
+ * been buffered but the engine would be ready to accept some, AND the
+ * `force` parameter is non-zero, then an empty record is assembled and
+ * marked for sending. In all other cases, this function does nothing.
+ *
+ * Empty records are technically legal, but not all existing SSL/TLS
+ * implementations support them. Empty records can be useful as a
+ * transparent "keep-alive" mechanism to maintain some low-level
+ * network activity.
+ *
+ * \param cc SSL engine context.
+ * \param force non-zero to force sending an empty record.
+ */
+void br_ssl_engine_flush(br_ssl_engine_context *cc, int force);
+
+/**
+ * \brief Initiate a closure.
+ *
+ * If, at that point, the context is open and in ready state, then a
+ * `close_notify` alert is assembled and marked for sending; this
+ * triggers the closure protocol. Otherwise, no such alert is assembled.
+ *
+ * \param cc SSL engine context.
+ */
+void br_ssl_engine_close(br_ssl_engine_context *cc);
+
+/**
+ * \brief Initiate a renegotiation.
+ *
+ * If the engine is failed or closed, or if the peer is known not to
+ * support secure renegotiation (RFC 5746), or if renegotiations have
+ * been disabled with the `BR_OPT_NO_RENEGOTIATION` flag, or if there
+ * is buffered incoming application data, then this function returns 0
+ * and nothing else happens.
+ *
+ * Otherwise, this function returns 1, and a renegotiation attempt is
+ * triggered (if a handshake is already ongoing at that point, then
+ * no new handshake is triggered).
+ *
+ * \param cc SSL engine context.
+ * \return 1 on success, 0 on error.
+ */
+int br_ssl_engine_renegotiate(br_ssl_engine_context *cc);
+
+/**
+ * \brief Export key material from a connected SSL engine (RFC 5705).
+ *
+ * This calls compute a secret key of arbitrary length from the master
+ * secret of a connected SSL engine. If the provided context is not
+ * currently in "application data" state (initial handshake is not
+ * finished, another handshake is ongoing, or the connection failed or
+ * was closed), then this function returns 0. Otherwise, a secret key of
+ * length `len` bytes is computed and written in the buffer pointed to
+ * by `dst`, and 1 is returned.
+ *
+ * The computed key follows the specification described in RFC 5705.
+ * That RFC includes two key computations, with and without a "context
+ * value". If `context` is `NULL`, then the variant without context is
+ * used; otherwise, the `context_len` bytes located at the address
+ * pointed to by `context` are used in the computation. Note that it
+ * is possible to have a "with context" key with a context length of
+ * zero bytes, by setting `context` to a non-`NULL` value but
+ * `context_len` to 0.
+ *
+ * When context bytes are used, the context length MUST NOT exceed
+ * 65535 bytes.
+ *
+ * \param cc SSL engine context.
+ * \param dst destination buffer for exported key.
+ * \param len exported key length (in bytes).
+ * \param label disambiguation label.
+ * \param context context value (or `NULL`).
+ * \param context_len context length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+int br_ssl_key_export(br_ssl_engine_context *cc,
+ void *dst, size_t len, const char *label,
+ const void *context, size_t context_len);
+
+/*
+ * Pre-declaration for the SSL client context.
+ */
+typedef struct br_ssl_client_context_ br_ssl_client_context;
+
+/**
+ * \brief Type for the client certificate, if requested by the server.
+ */
+typedef struct {
+ /**
+ * \brief Authentication type.
+ *
+ * This is either `BR_AUTH_RSA` (RSA signature), `BR_AUTH_ECDSA`
+ * (ECDSA signature), or `BR_AUTH_ECDH` (static ECDH key exchange).
+ */
+ int auth_type;
+
+ /**
+ * \brief Hash function for computing the CertificateVerify.
+ *
+ * This is the symbolic identifier for the hash function that
+ * will be used to produce the hash of handshake messages, to
+ * be signed into the CertificateVerify. For full static ECDH
+ * (client and server certificates are both EC in the same
+ * curve, and static ECDH is used), this value is set to -1.
+ *
+ * Take care that with TLS 1.0 and 1.1, that value MUST match
+ * the protocol requirements: value must be 0 (MD5+SHA-1) for
+ * a RSA signature, or 2 (SHA-1) for an ECDSA signature. Only
+ * TLS 1.2 allows for other hash functions.
+ */
+ int hash_id;
+
+ /**
+ * \brief Certificate chain to send to the server.
+ *
+ * This is an array of `br_x509_certificate` objects, each
+ * normally containing a DER-encoded certificate. The client
+ * code does not try to decode these elements. If there is no
+ * chain to send to the server, then this pointer shall be
+ * set to `NULL`.
+ */
+ const br_x509_certificate *chain;
+
+ /**
+ * \brief Certificate chain length (number of certificates).
+ *
+ * If there is no chain to send to the server, then this value
+ * shall be set to 0.
+ */
+ size_t chain_len;
+
+} br_ssl_client_certificate;
+
+/*
+ * Note: the constants below for signatures match the TLS constants.
+ */
+
+/** \brief Client authentication type: static ECDH. */
+#define BR_AUTH_ECDH 0
+/** \brief Client authentication type: RSA signature. */
+#define BR_AUTH_RSA 1
+/** \brief Client authentication type: ECDSA signature. */
+#define BR_AUTH_ECDSA 3
+
+/**
+ * \brief Class type for a certificate handler (client side).
+ *
+ * A certificate handler selects a client certificate chain to send to
+ * the server, upon explicit request from that server. It receives
+ * the list of trust anchor DN from the server, and supported types
+ * of certificates and signatures, and returns the chain to use. It
+ * is also invoked to perform the corresponding private key operation
+ * (a signature, or an ECDH computation).
+ *
+ * The SSL client engine will first push the trust anchor DN with
+ * `start_name_list()`, `start_name()`, `append_name()`, `end_name()`
+ * and `end_name_list()`. Then it will call `choose()`, to select the
+ * actual chain (and signature/hash algorithms). Finally, it will call
+ * either `do_sign()` or `do_keyx()`, depending on the algorithm choices.
+ */
+typedef struct br_ssl_client_certificate_class_ br_ssl_client_certificate_class;
+struct br_ssl_client_certificate_class_ {
+ /**
+ * \brief Context size (in bytes).
+ */
+ size_t context_size;
+
+ /**
+ * \brief Begin reception of a list of trust anchor names. This
+ * is called while parsing the incoming CertificateRequest.
+ *
+ * \param pctx certificate handler context.
+ */
+ void (*start_name_list)(const br_ssl_client_certificate_class **pctx);
+
+ /**
+ * \brief Begin reception of a new trust anchor name.
+ *
+ * The total encoded name length is provided; it is less than
+ * 65535 bytes.
+ *
+ * \param pctx certificate handler context.
+ * \param len encoded name length (in bytes).
+ */
+ void (*start_name)(const br_ssl_client_certificate_class **pctx,
+ size_t len);
+
+ /**
+ * \brief Receive some more bytes for the current trust anchor name.
+ *
+ * The provided reference (`data`) points to a transient buffer
+ * they may be reused as soon as this function returns. The chunk
+ * length (`len`) is never zero.
+ *
+ * \param pctx certificate handler context.
+ * \param data anchor name chunk.
+ * \param len anchor name chunk length (in bytes).
+ */
+ void (*append_name)(const br_ssl_client_certificate_class **pctx,
+ const unsigned char *data, size_t len);
+
+ /**
+ * \brief End current trust anchor name.
+ *
+ * This function is called when all the encoded anchor name data
+ * has been provided.
+ *
+ * \param pctx certificate handler context.
+ */
+ void (*end_name)(const br_ssl_client_certificate_class **pctx);
+
+ /**
+ * \brief End list of trust anchor names.
+ *
+ * This function is called when all the anchor names in the
+ * CertificateRequest message have been obtained.
+ *
+ * \param pctx certificate handler context.
+ */
+ void (*end_name_list)(const br_ssl_client_certificate_class **pctx);
+
+ /**
+ * \brief Select client certificate and algorithms.
+ *
+ * This callback function shall fill the provided `choices`
+ * structure with the selected algorithms and certificate chain.
+ * The `hash_id`, `chain` and `chain_len` fields must be set. If
+ * the client cannot or does not wish to send a certificate,
+ * then it shall set `chain` to `NULL` and `chain_len` to 0.
+ *
+ * The `auth_types` parameter describes the authentication types,
+ * signature algorithms and hash functions that are supported by
+ * both the client context and the server, and compatible with
+ * the current protocol version. This is a bit field with the
+ * following contents:
+ *
+ * - If RSA signatures with hash function x are supported, then
+ * bit x is set.
+ *
+ * - If ECDSA signatures with hash function x are supported,
+ * then bit 8+x is set.
+ *
+ * - If static ECDH is supported, with a RSA-signed certificate,
+ * then bit 16 is set.
+ *
+ * - If static ECDH is supported, with an ECDSA-signed certificate,
+ * then bit 17 is set.
+ *
+ * Notes:
+ *
+ * - When using TLS 1.0 or 1.1, the hash function for RSA
+ * signatures is always the special MD5+SHA-1 (id 0), and the
+ * hash function for ECDSA signatures is always SHA-1 (id 2).
+ *
+ * - When using TLS 1.2, the list of hash functions is trimmed
+ * down to include only hash functions that the client context
+ * can support. The actual server list can be obtained with
+ * `br_ssl_client_get_server_hashes()`; that list may be used
+ * to select the certificate chain to send to the server.
+ *
+ * \param pctx certificate handler context.
+ * \param cc SSL client context.
+ * \param auth_types supported authentication types and algorithms.
+ * \param choices destination structure for the policy choices.
+ */
+ void (*choose)(const br_ssl_client_certificate_class **pctx,
+ const br_ssl_client_context *cc, uint32_t auth_types,
+ br_ssl_client_certificate *choices);
+
+ /**
+ * \brief Perform key exchange (client part).
+ *
+ * This callback is invoked in case of a full static ECDH key
+ * exchange:
+ *
+ * - the cipher suite uses `ECDH_RSA` or `ECDH_ECDSA`;
+ *
+ * - the server requests a client certificate;
+ *
+ * - the client has, and sends, a client certificate that
+ * uses an EC key in the same curve as the server's key,
+ * and chooses static ECDH (the `hash_id` field in the choice
+ * structure was set to -1).
+ *
+ * In that situation, this callback is invoked to compute the
+ * client-side ECDH: the provided `data` (of length `*len` bytes)
+ * is the server's public key point (as decoded from its
+ * certificate), and the client shall multiply that point with
+ * its own private key, and write back the X coordinate of the
+ * resulting point in the same buffer, starting at offset 0.
+ * The `*len` value shall be modified to designate the actual
+ * length of the X coordinate.
+ *
+ * The callback must uphold the following:
+ *
+ * - If the input array does not have the proper length for
+ * an encoded curve point, then an error (0) shall be reported.
+ *
+ * - If the input array has the proper length, then processing
+ * MUST be constant-time, even if the data is not a valid
+ * encoded point.
+ *
+ * - This callback MUST check that the input point is valid.
+ *
+ * Returned value is 1 on success, 0 on error.
+ *
+ * \param pctx certificate handler context.
+ * \param data server public key point.
+ * \param len public key point length / X coordinate length.
+ * \return 1 on success, 0 on error.
+ */
+ uint32_t (*do_keyx)(const br_ssl_client_certificate_class **pctx,
+ unsigned char *data, size_t *len);
+
+ /**
+ * \brief Perform a signature (client authentication).
+ *
+ * This callback is invoked when a client certificate was sent,
+ * and static ECDH is not used. It shall compute a signature,
+ * using the client's private key, over the provided hash value
+ * (which is the hash of all previous handshake messages).
+ *
+ * On input, the hash value to sign is in `data`, of size
+ * `hv_len`; the involved hash function is identified by
+ * `hash_id`. The signature shall be computed and written
+ * back into `data`; the total size of that buffer is `len`
+ * bytes.
+ *
+ * This callback shall verify that the signature length does not
+ * exceed `len` bytes, and abstain from writing the signature if
+ * it does not fit.
+ *
+ * For RSA signatures, the `hash_id` may be 0, in which case
+ * this is the special header-less signature specified in TLS 1.0
+ * and 1.1, with a 36-byte hash value. Otherwise, normal PKCS#1
+ * v1.5 signatures shall be computed.
+ *
+ * For ECDSA signatures, the signature value shall use the ASN.1
+ * based encoding.
+ *
+ * Returned value is the signature length (in bytes), or 0 on error.
+ *
+ * \param pctx certificate handler context.
+ * \param hash_id hash function identifier.
+ * \param hv_len hash value length (in bytes).
+ * \param data input/output buffer (hash value, then signature).
+ * \param len total buffer length (in bytes).
+ * \return signature length (in bytes) on success, or 0 on error.
+ */
+ size_t (*do_sign)(const br_ssl_client_certificate_class **pctx,
+ int hash_id, size_t hv_len, unsigned char *data, size_t len);
+};
+
+/**
+ * \brief A single-chain RSA client certificate handler.
+ *
+ * This handler uses a single certificate chain, with a RSA
+ * signature. The list of trust anchor DN is ignored.
+ *
+ * Apart from the first field (vtable pointer), its contents are
+ * opaque and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_ssl_client_certificate_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ const br_x509_certificate *chain;
+ size_t chain_len;
+ const br_rsa_private_key *sk;
+ br_rsa_pkcs1_sign irsasign;
+#endif
+} br_ssl_client_certificate_rsa_context;
+
+/**
+ * \brief A single-chain EC client certificate handler.
+ *
+ * This handler uses a single certificate chain, with a RSA
+ * signature. The list of trust anchor DN is ignored.
+ *
+ * This handler may support both static ECDH, and ECDSA signatures
+ * (either usage may be selectively disabled).
+ *
+ * Apart from the first field (vtable pointer), its contents are
+ * opaque and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_ssl_client_certificate_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ const br_x509_certificate *chain;
+ size_t chain_len;
+ const br_ec_private_key *sk;
+ unsigned allowed_usages;
+ unsigned issuer_key_type;
+ const br_multihash_context *mhash;
+ const br_ec_impl *iec;
+ br_ecdsa_sign iecdsa;
+#endif
+} br_ssl_client_certificate_ec_context;
+
+/**
+ * \brief Context structure for a SSL client.
+ *
+ * The first field (called `eng`) is the SSL engine; all functions that
+ * work on a `br_ssl_engine_context` structure shall take as parameter
+ * a pointer to that field. The other structure fields are opaque and
+ * must not be accessed directly.
+ */
+struct br_ssl_client_context_ {
+ /**
+ * \brief The encapsulated engine context.
+ */
+ br_ssl_engine_context eng;
+
+#ifndef BR_DOXYGEN_IGNORE
+ /*
+ * Minimum ClientHello length; padding with an extension (RFC
+ * 7685) is added if necessary to match at least that length.
+ * Such padding is nominally unnecessary, but it has been used
+ * to work around some server implementation bugs.
+ */
+ uint16_t min_clienthello_len;
+
+ /*
+ * Bit field for algoithms (hash + signature) supported by the
+ * server when requesting a client certificate.
+ */
+ uint32_t hashes;
+
+ /*
+ * Server's public key curve.
+ */
+ int server_curve;
+
+ /*
+ * Context for certificate handler.
+ */
+ const br_ssl_client_certificate_class **client_auth_vtable;
+
+ /*
+ * Client authentication type.
+ */
+ unsigned char auth_type;
+
+ /*
+ * Hash function to use for the client signature. This is 0xFF
+ * if static ECDH is used.
+ */
+ unsigned char hash_id;
+
+ /*
+ * For the core certificate handlers, thus avoiding (in most
+ * cases) the need for an externally provided policy context.
+ */
+ union {
+ const br_ssl_client_certificate_class *vtable;
+ br_ssl_client_certificate_rsa_context single_rsa;
+ br_ssl_client_certificate_ec_context single_ec;
+ } client_auth;
+
+ /*
+ * Implementations.
+ */
+ br_rsa_public irsapub;
+#endif
+};
+
+/**
+ * \brief Get the hash functions and signature algorithms supported by
+ * the server.
+ *
+ * This value is a bit field:
+ *
+ * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
+ * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
+ * or 2 to 6 for the SHA family).
+ *
+ * - If ECDSA is supported with hash function of ID `x`, then bit `8+x`
+ * is set.
+ *
+ * - Newer algorithms are symbolic 16-bit identifiers that do not
+ * represent signature algorithm and hash function separately. If
+ * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
+ * range, then bit `16+x` is set.
+ *
+ * "New algorithms" are currently defined only in draft documents, so
+ * this support is subject to possible change. Right now (early 2017),
+ * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
+ * on Curve448) to bit 24. If the identifiers on the wire change in
+ * future document, then the decoding mechanism in BearSSL will be
+ * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
+ * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
+ * guaranteed yet.
+ *
+ * \param cc client context.
+ * \return the server-supported hash functions and signature algorithms.
+ */
+static inline uint32_t
+br_ssl_client_get_server_hashes(const br_ssl_client_context *cc)
+{
+ return cc->hashes;
+}
+
+/**
+ * \brief Get the server key curve.
+ *
+ * This function returns the ID for the curve used by the server's public
+ * key. This is set when the server's certificate chain is processed;
+ * this value is 0 if the server's key is not an EC key.
+ *
+ * \return the server's public key curve ID, or 0.
+ */
+static inline int
+br_ssl_client_get_server_curve(const br_ssl_client_context *cc)
+{
+ return cc->server_curve;
+}
+
+/*
+ * Each br_ssl_client_init_xxx() function sets the list of supported
+ * cipher suites and used implementations, as specified by the profile
+ * name 'xxx'. Defined profile names are:
+ *
+ * full all supported versions and suites; constant-time implementations
+ * TODO: add other profiles
+ */
+
+/**
+ * \brief SSL client profile: full.
+ *
+ * This function initialises the provided SSL client context with
+ * all supported algorithms and cipher suites. It also initialises
+ * a companion X.509 validation engine with all supported algorithms,
+ * and the provided trust anchors; the X.509 engine will be used by
+ * the client context to validate the server's certificate.
+ *
+ * \param cc client context to initialise.
+ * \param xc X.509 validation context to initialise.
+ * \param trust_anchors trust anchors to use.
+ * \param trust_anchors_num number of trust anchors.
+ */
+void br_ssl_client_init_full(br_ssl_client_context *cc,
+ br_x509_minimal_context *xc,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
+
+/**
+ * \brief Clear the complete contents of a SSL client context.
+ *
+ * Everything is cleared, including the reference to the configured buffer,
+ * implementations, cipher suites and state. This is a preparatory step
+ * to assembling a custom profile.
+ *
+ * \param cc client context to clear.
+ */
+void br_ssl_client_zero(br_ssl_client_context *cc);
+
+/**
+ * \brief Set an externally provided client certificate handler context.
+ *
+ * The handler's methods are invoked when the server requests a client
+ * certificate.
+ *
+ * \param cc client context.
+ * \param pctx certificate handler context (pointer to its vtable field).
+ */
+static inline void
+br_ssl_client_set_client_certificate(br_ssl_client_context *cc,
+ const br_ssl_client_certificate_class **pctx)
+{
+ cc->client_auth_vtable = pctx;
+}
+
+/**
+ * \brief Set the RSA public-key operations implementation.
+ *
+ * This will be used to encrypt the pre-master secret with the server's
+ * RSA public key (RSA-encryption cipher suites only).
+ *
+ * \param cc client context.
+ * \param irsapub RSA public-key encryption implementation.
+ */
+static inline void
+br_ssl_client_set_rsapub(br_ssl_client_context *cc, br_rsa_public irsapub)
+{
+ cc->irsapub = irsapub;
+}
+
+/**
+ * \brief Set the "default" RSA implementation for public-key operations.
+ *
+ * This sets the RSA implementation in the client context (for encrypting
+ * the pre-master secret, in `TLS_RSA_*` cipher suites) to the fastest
+ * available on the current platform.
+ *
+ * \param cc client context.
+ */
+void br_ssl_client_set_default_rsapub(br_ssl_client_context *cc);
+
+/**
+ * \brief Set the minimum ClientHello length (RFC 7685 padding).
+ *
+ * If this value is set and the ClientHello would be shorter, then
+ * the Pad ClientHello extension will be added with enough padding bytes
+ * to reach the target size. Because of the extension header, the resulting
+ * size will sometimes be slightly more than `len` bytes if the target
+ * size cannot be exactly met.
+ *
+ * The target length relates to the _contents_ of the ClientHello, not
+ * counting its 4-byte header. For instance, if `len` is set to 512,
+ * then the padding will bring the ClientHello size to 516 bytes with its
+ * header, and 521 bytes when counting the 5-byte record header.
+ *
+ * \param cc client context.
+ * \param len minimum ClientHello length (in bytes).
+ */
+static inline void
+br_ssl_client_set_min_clienthello_len(br_ssl_client_context *cc, uint16_t len)
+{
+ cc->min_clienthello_len = len;
+}
+
+/**
+ * \brief Prepare or reset a client context for a new connection.
+ *
+ * The `server_name` parameter is used to fill the SNI extension; the
+ * X.509 "minimal" engine will also match that name against the server
+ * names included in the server's certificate. If the parameter is
+ * `NULL` then no SNI extension will be sent, and the X.509 "minimal"
+ * engine (if used for server certificate validation) will not check
+ * presence of any specific name in the received certificate.
+ *
+ * Therefore, setting the `server_name` to `NULL` shall be reserved
+ * to cases where alternate or additional methods are used to ascertain
+ * that the right server public key is used (e.g. a "known key" model).
+ *
+ * If `resume_session` is non-zero and the context was previously used
+ * then the session parameters may be reused (depending on whether the
+ * server previously sent a non-empty session ID, and accepts the session
+ * resumption). The session parameters for session resumption can also
+ * be set explicitly with `br_ssl_engine_set_session_parameters()`.
+ *
+ * On failure, the context is marked as failed, and this function
+ * returns 0. A possible failure condition is when no initial entropy
+ * was injected, and none could be obtained from the OS (either OS
+ * randomness gathering is not supported, or it failed).
+ *
+ * \param cc client context.
+ * \param server_name target server name, or `NULL`.
+ * \param resume_session non-zero to try session resumption.
+ * \return 0 on failure, 1 on success.
+ */
+int br_ssl_client_reset(br_ssl_client_context *cc,
+ const char *server_name, int resume_session);
+
+/**
+ * \brief Forget any session in the context.
+ *
+ * This means that the next handshake that uses this context will
+ * necessarily be a full handshake (this applies both to new connections
+ * and to renegotiations).
+ *
+ * \param cc client context.
+ */
+static inline void
+br_ssl_client_forget_session(br_ssl_client_context *cc)
+{
+ cc->eng.session.session_id_len = 0;
+}
+
+/**
+ * \brief Set client certificate chain and key (single RSA case).
+ *
+ * This function sets a client certificate chain, that the client will
+ * send to the server whenever a client certificate is requested. This
+ * certificate uses an RSA public key; the corresponding private key is
+ * invoked for authentication. Trust anchor names sent by the server are
+ * ignored.
+ *
+ * The provided chain and private key are linked in the client context;
+ * they must remain valid as long as they may be used, i.e. normally
+ * for the duration of the connection, since they might be invoked
+ * again upon renegotiations.
+ *
+ * \param cc SSL client context.
+ * \param chain client certificate chain (SSL order: EE comes first).
+ * \param chain_len client chain length (number of certificates).
+ * \param sk client private key.
+ * \param irsasign RSA signature implementation (PKCS#1 v1.5).
+ */
+void br_ssl_client_set_single_rsa(br_ssl_client_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk, br_rsa_pkcs1_sign irsasign);
+
+/*
+ * \brief Set the client certificate chain and key (single EC case).
+ *
+ * This function sets a client certificate chain, that the client will
+ * send to the server whenever a client certificate is requested. This
+ * certificate uses an EC public key; the corresponding private key is
+ * invoked for authentication. Trust anchor names sent by the server are
+ * ignored.
+ *
+ * The provided chain and private key are linked in the client context;
+ * they must remain valid as long as they may be used, i.e. normally
+ * for the duration of the connection, since they might be invoked
+ * again upon renegotiations.
+ *
+ * The `allowed_usages` is a combination of usages, namely
+ * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`. The `BR_KEYTYPE_KEYX`
+ * value allows full static ECDH, while the `BR_KEYTYPE_SIGN` value
+ * allows ECDSA signatures. If ECDSA signatures are used, then an ECDSA
+ * signature implementation must be provided; otherwise, the `iecdsa`
+ * parameter may be 0.
+ *
+ * The `cert_issuer_key_type` value is either `BR_KEYTYPE_RSA` or
+ * `BR_KEYTYPE_EC`; it is the type of the public key used the the CA
+ * that issued (signed) the client certificate. That value is used with
+ * full static ECDH: support of the certificate by the server depends
+ * on how the certificate was signed. (Note: when using TLS 1.2, this
+ * parameter is ignored; but its value matters for TLS 1.0 and 1.1.)
+ *
+ * \param cc server context.
+ * \param chain server certificate chain to send.
+ * \param chain_len chain length (number of certificates).
+ * \param sk server private key (EC).
+ * \param allowed_usages allowed private key usages.
+ * \param cert_issuer_key_type issuing CA's key type.
+ * \param iec EC core implementation.
+ * \param iecdsa ECDSA signature implementation ("asn1" format).
+ */
+void br_ssl_client_set_single_ec(br_ssl_client_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk, unsigned allowed_usages,
+ unsigned cert_issuer_key_type,
+ const br_ec_impl *iec, br_ecdsa_sign iecdsa);
+
+/**
+ * \brief Type for a "translated cipher suite", as an array of two
+ * 16-bit integers.
+ *
+ * The first element is the cipher suite identifier (as used on the wire).
+ * The second element is the concatenation of four 4-bit elements which
+ * characterise the cipher suite contents. In most to least significant
+ * order, these 4-bit elements are:
+ *
+ * - Bits 12 to 15: key exchange + server key type
+ *
+ * | val | symbolic constant | suite type | details |
+ * | :-- | :----------------------- | :---------- | :----------------------------------------------- |
+ * | 0 | `BR_SSLKEYX_RSA` | RSA | RSA key exchange, key is RSA (encryption) |
+ * | 1 | `BR_SSLKEYX_ECDHE_RSA` | ECDHE_RSA | ECDHE key exchange, key is RSA (signature) |
+ * | 2 | `BR_SSLKEYX_ECDHE_ECDSA` | ECDHE_ECDSA | ECDHE key exchange, key is EC (signature) |
+ * | 3 | `BR_SSLKEYX_ECDH_RSA` | ECDH_RSA | Key is EC (key exchange), cert signed with RSA |
+ * | 4 | `BR_SSLKEYX_ECDH_ECDSA` | ECDH_ECDSA | Key is EC (key exchange), cert signed with ECDSA |
+ *
+ * - Bits 8 to 11: symmetric encryption algorithm
+ *
+ * | val | symbolic constant | symmetric encryption | key strength (bits) |
+ * | :-- | :--------------------- | :------------------- | :------------------ |
+ * | 0 | `BR_SSLENC_3DES_CBC` | 3DES/CBC | 168 |
+ * | 1 | `BR_SSLENC_AES128_CBC` | AES-128/CBC | 128 |
+ * | 2 | `BR_SSLENC_AES256_CBC` | AES-256/CBC | 256 |
+ * | 3 | `BR_SSLENC_AES128_GCM` | AES-128/GCM | 128 |
+ * | 4 | `BR_SSLENC_AES256_GCM` | AES-256/GCM | 256 |
+ * | 5 | `BR_SSLENC_CHACHA20` | ChaCha20/Poly1305 | 256 |
+ *
+ * - Bits 4 to 7: MAC algorithm
+ *
+ * | val | symbolic constant | MAC type | details |
+ * | :-- | :----------------- | :----------- | :------------------------------------ |
+ * | 0 | `BR_SSLMAC_AEAD` | AEAD | No dedicated MAC (encryption is AEAD) |
+ * | 2 | `BR_SSLMAC_SHA1` | HMAC/SHA-1 | Value matches `br_sha1_ID` |
+ * | 4 | `BR_SSLMAC_SHA256` | HMAC/SHA-256 | Value matches `br_sha256_ID` |
+ * | 5 | `BR_SSLMAC_SHA384` | HMAC/SHA-384 | Value matches `br_sha384_ID` |
+ *
+ * - Bits 0 to 3: hash function for PRF when used with TLS-1.2
+ *
+ * | val | symbolic constant | hash function | details |
+ * | :-- | :----------------- | :------------ | :----------------------------------- |
+ * | 4 | `BR_SSLPRF_SHA256` | SHA-256 | Value matches `br_sha256_ID` |
+ * | 5 | `BR_SSLPRF_SHA384` | SHA-384 | Value matches `br_sha384_ID` |
+ *
+ * For instance, cipher suite `TLS_RSA_WITH_AES_128_GCM_SHA256` has
+ * standard identifier 0x009C, and is translated to 0x0304, for, in
+ * that order: RSA key exchange (0), AES-128/GCM (3), AEAD integrity (0),
+ * SHA-256 in the TLS PRF (4).
+ */
+typedef uint16_t br_suite_translated[2];
+
+#ifndef BR_DOXYGEN_IGNORE
+/*
+ * Constants are already documented in the br_suite_translated type.
+ */
+
+#define BR_SSLKEYX_RSA 0
+#define BR_SSLKEYX_ECDHE_RSA 1
+#define BR_SSLKEYX_ECDHE_ECDSA 2
+#define BR_SSLKEYX_ECDH_RSA 3
+#define BR_SSLKEYX_ECDH_ECDSA 4
+
+#define BR_SSLENC_3DES_CBC 0
+#define BR_SSLENC_AES128_CBC 1
+#define BR_SSLENC_AES256_CBC 2
+#define BR_SSLENC_AES128_GCM 3
+#define BR_SSLENC_AES256_GCM 4
+#define BR_SSLENC_CHACHA20 5
+
+#define BR_SSLMAC_AEAD 0
+#define BR_SSLMAC_SHA1 br_sha1_ID
+#define BR_SSLMAC_SHA256 br_sha256_ID
+#define BR_SSLMAC_SHA384 br_sha384_ID
+
+#define BR_SSLPRF_SHA256 br_sha256_ID
+#define BR_SSLPRF_SHA384 br_sha384_ID
+
+#endif
+
+/*
+ * Pre-declaration for the SSL server context.
+ */
+typedef struct br_ssl_server_context_ br_ssl_server_context;
+
+/**
+ * \brief Type for the server policy choices, taken after analysis of
+ * the client message (ClientHello).
+ */
+typedef struct {
+ /**
+ * \brief Cipher suite to use with that client.
+ */
+ uint16_t cipher_suite;
+
+ /**
+ * \brief Hash function or algorithm for signing the ServerKeyExchange.
+ *
+ * This parameter is ignored for `TLS_RSA_*` and `TLS_ECDH_*`
+ * cipher suites; it is used only for `TLS_ECDHE_*` suites, in
+ * which the server _signs_ the ephemeral EC Diffie-Hellman
+ * parameters sent to the client.
+ *
+ * This identifier must be one of the following values:
+ *
+ * - `0xFF00 + id`, where `id` is a hash function identifier
+ * (0 for MD5+SHA-1, or 2 to 6 for one of the SHA functions);
+ *
+ * - a full 16-bit identifier, lower than `0xFF00`.
+ *
+ * If the first option is used, then the SSL engine will
+ * compute the hash of the data that is to be signed, with the
+ * designated hash function. The `do_sign()` method will be
+ * invoked with that hash value provided in the the `data`
+ * buffer.
+ *
+ * If the second option is used, then the SSL engine will NOT
+ * compute a hash on the data; instead, it will provide the
+ * to-be-signed data itself in `data`, i.e. the concatenation of
+ * the client random, server random, and encoded ECDH
+ * parameters. Furthermore, with TLS-1.2 and later, the 16-bit
+ * identifier will be used "as is" in the protocol, in the
+ * SignatureAndHashAlgorithm; for instance, `0x0401` stands for
+ * RSA PKCS#1 v1.5 signature (the `01`) with SHA-256 as hash
+ * function (the `04`).
+ *
+ * Take care that with TLS 1.0 and 1.1, the hash function is
+ * constrainted by the protocol: RSA signature must use
+ * MD5+SHA-1 (so use `0xFF00`), while ECDSA must use SHA-1
+ * (`0xFF02`). Since TLS 1.0 and 1.1 don't include a
+ * SignatureAndHashAlgorithm field in their ServerKeyExchange
+ * messages, any value below `0xFF00` will be usable to send the
+ * raw ServerKeyExchange data to the `do_sign()` callback, but
+ * that callback must still follow the protocol requirements
+ * when generating the signature.
+ */
+ unsigned algo_id;
+
+ /**
+ * \brief Certificate chain to send to the client.
+ *
+ * This is an array of `br_x509_certificate` objects, each
+ * normally containing a DER-encoded certificate. The server
+ * code does not try to decode these elements.
+ */
+ const br_x509_certificate *chain;
+
+ /**
+ * \brief Certificate chain length (number of certificates).
+ */
+ size_t chain_len;
+
+} br_ssl_server_choices;
+
+/**
+ * \brief Class type for a policy handler (server side).
+ *
+ * A policy handler selects the policy parameters for a connection
+ * (cipher suite and other algorithms, and certificate chain to send to
+ * the client); it also performs the server-side computations involving
+ * its permanent private key.
+ *
+ * The SSL server engine will invoke first `choose()`, once the
+ * ClientHello message has been received, then either `do_keyx()`
+ * `do_sign()`, depending on the cipher suite.
+ */
+typedef struct br_ssl_server_policy_class_ br_ssl_server_policy_class;
+struct br_ssl_server_policy_class_ {
+ /**
+ * \brief Context size (in bytes).
+ */
+ size_t context_size;
+
+ /**
+ * \brief Select algorithms and certificates for this connection.
+ *
+ * This callback function shall fill the provided `choices`
+ * structure with the policy choices for this connection. This
+ * entails selecting the cipher suite, hash function for signing
+ * the ServerKeyExchange (applicable only to ECDHE cipher suites),
+ * and certificate chain to send.
+ *
+ * The callback receives a pointer to the server context that
+ * contains the relevant data. In particular, the functions
+ * `br_ssl_server_get_client_suites()`,
+ * `br_ssl_server_get_client_hashes()` and
+ * `br_ssl_server_get_client_curves()` can be used to obtain
+ * the cipher suites, hash functions and elliptic curves
+ * supported by both the client and server, respectively. The
+ * `br_ssl_engine_get_version()` and `br_ssl_engine_get_server_name()`
+ * functions yield the protocol version and requested server name
+ * (SNI), respectively.
+ *
+ * This function may modify its context structure (`pctx`) in
+ * arbitrary ways to keep track of its own choices.
+ *
+ * This function shall return 1 if appropriate policy choices
+ * could be made, or 0 if this connection cannot be pursued.
+ *
+ * \param pctx policy context.
+ * \param cc SSL server context.
+ * \param choices destination structure for the policy choices.
+ * \return 1 on success, 0 on error.
+ */
+ int (*choose)(const br_ssl_server_policy_class **pctx,
+ const br_ssl_server_context *cc,
+ br_ssl_server_choices *choices);
+
+ /**
+ * \brief Perform key exchange (server part).
+ *
+ * This callback is invoked to perform the server-side cryptographic
+ * operation for a key exchange that is not ECDHE. This callback
+ * uses the private key.
+ *
+ * **For RSA key exchange**, the provided `data` (of length `*len`
+ * bytes) shall be decrypted with the server's private key, and
+ * the 48-byte premaster secret copied back to the first 48 bytes
+ * of `data`.
+ *
+ * - The caller makes sure that `*len` is at least 59 bytes.
+ *
+ * - This callback MUST check that the provided length matches
+ * that of the key modulus; it shall report an error otherwise.
+ *
+ * - If the length matches that of the RSA key modulus, then
+ * processing MUST be constant-time, even if decryption fails,
+ * or the padding is incorrect, or the plaintext message length
+ * is not exactly 48 bytes.
+ *
+ * - This callback needs not check the two first bytes of the
+ * obtained pre-master secret (the caller will do that).
+ *
+ * - If an error is reported (0), then what the callback put
+ * in the first 48 bytes of `data` is unimportant (the caller
+ * will use random bytes instead).
+ *
+ * **For ECDH key exchange**, the provided `data` (of length `*len`
+ * bytes) is the elliptic curve point from the client. The
+ * callback shall multiply it with its private key, and store
+ * the resulting X coordinate in `data`, starting at offset 0,
+ * and set `*len` to the length of the X coordinate.
+ *
+ * - If the input array does not have the proper length for
+ * an encoded curve point, then an error (0) shall be reported.
+ *
+ * - If the input array has the proper length, then processing
+ * MUST be constant-time, even if the data is not a valid
+ * encoded point.
+ *
+ * - This callback MUST check that the input point is valid.
+ *
+ * Returned value is 1 on success, 0 on error.
+ *
+ * \param pctx policy context.
+ * \param data key exchange data from the client.
+ * \param len key exchange data length (in bytes).
+ * \return 1 on success, 0 on error.
+ */
+ uint32_t (*do_keyx)(const br_ssl_server_policy_class **pctx,
+ unsigned char *data, size_t *len);
+
+ /**
+ * \brief Perform a signature (for a ServerKeyExchange message).
+ *
+ * This callback function is invoked for ECDHE cipher suites. On
+ * input, the hash value or message to sign is in `data`, of
+ * size `hv_len`; the involved hash function or algorithm is
+ * identified by `algo_id`. The signature shall be computed and
+ * written back into `data`; the total size of that buffer is
+ * `len` bytes.
+ *
+ * This callback shall verify that the signature length does not
+ * exceed `len` bytes, and abstain from writing the signature if
+ * it does not fit.
+ *
+ * The `algo_id` value matches that which was written in the
+ * `choices` structures by the `choose()` callback. This will be
+ * one of the following:
+ *
+ * - `0xFF00 + id` for a hash function identifier `id`. In
+ * that case, the `data` buffer contains a hash value
+ * already computed over the data that is to be signed,
+ * of length `hv_len`. The `id` may be 0 to designate the
+ * special MD5+SHA-1 concatenation (old-style RSA signing).
+ *
+ * - Another value, lower than `0xFF00`. The `data` buffer
+ * then contains the raw, non-hashed data to be signed
+ * (concatenation of the client and server randoms and
+ * ECDH parameters). The callback is responsible to apply
+ * any relevant hashing as part of the signing process.
+ *
+ * Returned value is the signature length (in bytes), or 0 on error.
+ *
+ * \param pctx policy context.
+ * \param algo_id hash function / algorithm identifier.
+ * \param data input/output buffer (message/hash, then signature).
+ * \param hv_len hash value or message length (in bytes).
+ * \param len total buffer length (in bytes).
+ * \return signature length (in bytes) on success, or 0 on error.
+ */
+ size_t (*do_sign)(const br_ssl_server_policy_class **pctx,
+ unsigned algo_id,
+ unsigned char *data, size_t hv_len, size_t len);
+};
+
+/**
+ * \brief A single-chain RSA policy handler.
+ *
+ * This policy context uses a single certificate chain, and a RSA
+ * private key. The context can be restricted to only signatures or
+ * only key exchange.
+ *
+ * Apart from the first field (vtable pointer), its contents are
+ * opaque and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_ssl_server_policy_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ const br_x509_certificate *chain;
+ size_t chain_len;
+ const br_rsa_private_key *sk;
+ unsigned allowed_usages;
+ br_rsa_private irsacore;
+ br_rsa_pkcs1_sign irsasign;
+#endif
+} br_ssl_server_policy_rsa_context;
+
+/**
+ * \brief A single-chain EC policy handler.
+ *
+ * This policy context uses a single certificate chain, and an EC
+ * private key. The context can be restricted to only signatures or
+ * only key exchange.
+ *
+ * Due to how TLS is defined, this context must be made aware whether
+ * the server certificate was itself signed with RSA or ECDSA. The code
+ * does not try to decode the certificate to obtain that information.
+ *
+ * Apart from the first field (vtable pointer), its contents are
+ * opaque and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_ssl_server_policy_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ const br_x509_certificate *chain;
+ size_t chain_len;
+ const br_ec_private_key *sk;
+ unsigned allowed_usages;
+ unsigned cert_issuer_key_type;
+ const br_multihash_context *mhash;
+ const br_ec_impl *iec;
+ br_ecdsa_sign iecdsa;
+#endif
+} br_ssl_server_policy_ec_context;
+
+/**
+ * \brief Class type for a session parameter cache.
+ *
+ * Session parameters are saved in the cache with `save()`, and
+ * retrieved with `load()`. The cache implementation can apply any
+ * storage and eviction strategy that it sees fit. The SSL server
+ * context that performs the request is provided, so that its
+ * functionalities may be used by the implementation (e.g. hash
+ * functions or random number generation).
+ */
+typedef struct br_ssl_session_cache_class_ br_ssl_session_cache_class;
+struct br_ssl_session_cache_class_ {
+ /**
+ * \brief Context size (in bytes).
+ */
+ size_t context_size;
+
+ /**
+ * \brief Record a session.
+ *
+ * This callback should record the provided session parameters.
+ * The `params` structure is transient, so its contents shall
+ * be copied into the cache. The session ID has been randomly
+ * generated and always has length exactly 32 bytes.
+ *
+ * \param ctx session cache context.
+ * \param server_ctx SSL server context.
+ * \param params session parameters to save.
+ */
+ void (*save)(const br_ssl_session_cache_class **ctx,
+ br_ssl_server_context *server_ctx,
+ const br_ssl_session_parameters *params);
+
+ /**
+ * \brief Lookup a session in the cache.
+ *
+ * The session ID to lookup is in `params` and always has length
+ * exactly 32 bytes. If the session parameters are found in the
+ * cache, then the parameters shall be copied into the `params`
+ * structure. Returned value is 1 on successful lookup, 0
+ * otherwise.
+ *
+ * \param ctx session cache context.
+ * \param server_ctx SSL server context.
+ * \param params destination for session parameters.
+ * \return 1 if found, 0 otherwise.
+ */
+ int (*load)(const br_ssl_session_cache_class **ctx,
+ br_ssl_server_context *server_ctx,
+ br_ssl_session_parameters *params);
+};
+
+/**
+ * \brief Context for a basic cache system.
+ *
+ * The system stores session parameters in a buffer provided at
+ * initialisation time. Each entry uses exactly 100 bytes, and
+ * buffer sizes up to 4294967295 bytes are supported.
+ *
+ * Entries are evicted with a LRU (Least Recently Used) policy. A
+ * search tree is maintained to keep lookups fast even with large
+ * caches.
+ *
+ * Apart from the first field (vtable pointer), the structure
+ * contents are opaque and shall not be accessed directly.
+ */
+typedef struct {
+ /** \brief Pointer to vtable. */
+ const br_ssl_session_cache_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ unsigned char *store;
+ size_t store_len, store_ptr;
+ unsigned char index_key[32];
+ const br_hash_class *hash;
+ int init_done;
+ uint32_t head, tail, root;
+#endif
+} br_ssl_session_cache_lru;
+
+/**
+ * \brief Initialise a LRU session cache with the provided storage space.
+ *
+ * The provided storage space must remain valid as long as the cache
+ * is used. Arbitrary lengths are supported, up to 4294967295 bytes;
+ * each entry uses up exactly 100 bytes.
+ *
+ * \param cc session cache context.
+ * \param store storage space for cached entries.
+ * \param store_len storage space length (in bytes).
+ */
+void br_ssl_session_cache_lru_init(br_ssl_session_cache_lru *cc,
+ unsigned char *store, size_t store_len);
+
+/**
+ * \brief Forget an entry in an LRU session cache.
+ *
+ * The session cache context must have been initialised. The entry
+ * with the provided session ID (of exactly 32 bytes) is looked for
+ * in the cache; if located, it is disabled.
+ *
+ * \param cc session cache context.
+ * \param id session ID to forget.
+ */
+void br_ssl_session_cache_lru_forget(
+ br_ssl_session_cache_lru *cc, const unsigned char *id);
+
+/**
+ * \brief Context structure for a SSL server.
+ *
+ * The first field (called `eng`) is the SSL engine; all functions that
+ * work on a `br_ssl_engine_context` structure shall take as parameter
+ * a pointer to that field. The other structure fields are opaque and
+ * must not be accessed directly.
+ */
+struct br_ssl_server_context_ {
+ /**
+ * \brief The encapsulated engine context.
+ */
+ br_ssl_engine_context eng;
+
+#ifndef BR_DOXYGEN_IGNORE
+ /*
+ * Maximum version from the client.
+ */
+ uint16_t client_max_version;
+
+ /*
+ * Session cache.
+ */
+ const br_ssl_session_cache_class **cache_vtable;
+
+ /*
+ * Translated cipher suites supported by the client. The list
+ * is trimmed to include only the cipher suites that the
+ * server also supports; they are in the same order as in the
+ * client message.
+ */
+ br_suite_translated client_suites[BR_MAX_CIPHER_SUITES];
+ unsigned char client_suites_num;
+
+ /*
+ * Hash functions supported by the client, with ECDSA and RSA
+ * (bit mask). For hash function with id 'x', set bit index is
+ * x for RSA, x+8 for ECDSA. For newer algorithms, with ID
+ * 0x08**, bit 16+k is set for algorithm 0x0800+k.
+ */
+ uint32_t hashes;
+
+ /*
+ * Curves supported by the client (bit mask, for named curves).
+ */
+ uint32_t curves;
+
+ /*
+ * Context for chain handler.
+ */
+ const br_ssl_server_policy_class **policy_vtable;
+ uint16_t sign_hash_id;
+
+ /*
+ * For the core handlers, thus avoiding (in most cases) the
+ * need for an externally provided policy context.
+ */
+ union {
+ const br_ssl_server_policy_class *vtable;
+ br_ssl_server_policy_rsa_context single_rsa;
+ br_ssl_server_policy_ec_context single_ec;
+ } chain_handler;
+
+ /*
+ * Buffer for the ECDHE private key.
+ */
+ unsigned char ecdhe_key[70];
+ size_t ecdhe_key_len;
+
+ /*
+ * Trust anchor names for client authentication. "ta_names" and
+ * "tas" cannot be both non-NULL.
+ */
+ const br_x500_name *ta_names;
+ const br_x509_trust_anchor *tas;
+ size_t num_tas;
+ size_t cur_dn_index;
+ const unsigned char *cur_dn;
+ size_t cur_dn_len;
+
+ /*
+ * Buffer for the hash value computed over all handshake messages
+ * prior to CertificateVerify, and identifier for the hash function.
+ */
+ unsigned char hash_CV[64];
+ size_t hash_CV_len;
+ int hash_CV_id;
+
+ /*
+ * Server-specific implementations.
+ * (none for now)
+ */
+#endif
+};
+
+/*
+ * Each br_ssl_server_init_xxx() function sets the list of supported
+ * cipher suites and used implementations, as specified by the profile
+ * name 'xxx'. Defined profile names are:
+ *
+ * full_rsa all supported algorithm, server key type is RSA
+ * full_ec all supported algorithm, server key type is EC
+ * TODO: add other profiles
+ *
+ * Naming scheme for "minimal" profiles: min123
+ *
+ * -- character 1: key exchange
+ * r = RSA
+ * e = ECDHE_RSA
+ * f = ECDHE_ECDSA
+ * u = ECDH_RSA
+ * v = ECDH_ECDSA
+ * -- character 2: version / PRF
+ * 0 = TLS 1.0 / 1.1 with MD5+SHA-1
+ * 2 = TLS 1.2 with SHA-256
+ * 3 = TLS 1.2 with SHA-384
+ * -- character 3: encryption
+ * a = AES/CBC
+ * d = 3DES/CBC
+ * g = AES/GCM
+ * c = ChaCha20+Poly1305
+ */
+
+/**
+ * \brief SSL server profile: full_rsa.
+ *
+ * This function initialises the provided SSL server context with
+ * all supported algorithms and cipher suites that rely on a RSA
+ * key pair.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk RSA private key.
+ */
+void br_ssl_server_init_full_rsa(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief SSL server profile: full_ec.
+ *
+ * This function initialises the provided SSL server context with
+ * all supported algorithms and cipher suites that rely on an EC
+ * key pair.
+ *
+ * The key type of the CA that issued the server's certificate must
+ * be provided, since it matters for ECDH cipher suites (ECDH_RSA
+ * suites require a RSA-powered CA). The key type is either
+ * `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len chain length (number of certificates).
+ * \param cert_issuer_key_type certificate issuer's key type.
+ * \param sk EC private key.
+ */
+void br_ssl_server_init_full_ec(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ unsigned cert_issuer_key_type, const br_ec_private_key *sk);
+
+/**
+ * \brief SSL server profile: minr2g.
+ *
+ * This profile uses only TLS_RSA_WITH_AES_128_GCM_SHA256. Server key is
+ * RSA, and RSA key exchange is used (not forward secure, but uses little
+ * CPU in the client).
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk RSA private key.
+ */
+void br_ssl_server_init_minr2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief SSL server profile: mine2g.
+ *
+ * This profile uses only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. Server key
+ * is RSA, and ECDHE key exchange is used. This suite provides forward
+ * security, with a higher CPU expense on the client, and a somewhat
+ * larger code footprint (compared to "minr2g").
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk RSA private key.
+ */
+void br_ssl_server_init_mine2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief SSL server profile: minf2g.
+ *
+ * This profile uses only TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
+ * Server key is EC, and ECDHE key exchange is used. This suite provides
+ * forward security, with a higher CPU expense on the client and server
+ * (by a factor of about 3 to 4), and a somewhat larger code footprint
+ * (compared to "minu2g" and "minv2g").
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk EC private key.
+ */
+void br_ssl_server_init_minf2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk);
+
+/**
+ * \brief SSL server profile: minu2g.
+ *
+ * This profile uses only TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256.
+ * Server key is EC, and ECDH key exchange is used; the issuing CA used
+ * a RSA key.
+ *
+ * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
+ * but are the lightest on the server (for CPU usage), and are rather
+ * inexpensive on the client as well.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk EC private key.
+ */
+void br_ssl_server_init_minu2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk);
+
+/**
+ * \brief SSL server profile: minv2g.
+ *
+ * This profile uses only TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256.
+ * Server key is EC, and ECDH key exchange is used; the issuing CA used
+ * an EC key.
+ *
+ * The "minu2g" and "minv2g" profiles do not provide forward secrecy,
+ * but are the lightest on the server (for CPU usage), and are rather
+ * inexpensive on the client as well.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk EC private key.
+ */
+void br_ssl_server_init_minv2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk);
+
+/**
+ * \brief SSL server profile: mine2c.
+ *
+ * This profile uses only TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.
+ * Server key is RSA, and ECDHE key exchange is used. This suite
+ * provides forward security.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk RSA private key.
+ */
+void br_ssl_server_init_mine2c(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk);
+
+/**
+ * \brief SSL server profile: minf2c.
+ *
+ * This profile uses only TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.
+ * Server key is EC, and ECDHE key exchange is used. This suite provides
+ * forward security.
+ *
+ * \param cc server context to initialise.
+ * \param chain server certificate chain.
+ * \param chain_len certificate chain length (number of certificate).
+ * \param sk EC private key.
+ */
+void br_ssl_server_init_minf2c(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk);
+
+/**
+ * \brief Get the supported client suites.
+ *
+ * This function shall be called only after the ClientHello has been
+ * processed, typically from the policy engine. The returned array
+ * contains the cipher suites that are supported by both the client
+ * and the server; these suites are in client preference order, unless
+ * the `BR_OPT_ENFORCE_SERVER_PREFERENCES` flag was set, in which case
+ * they are in server preference order.
+ *
+ * The suites are _translated_, which means that each suite is given
+ * as two 16-bit integers: the standard suite identifier, and its
+ * translated version, broken down into its individual components,
+ * as explained with the `br_suite_translated` type.
+ *
+ * The returned array is allocated in the context and will be rewritten
+ * by each handshake.
+ *
+ * \param cc server context.
+ * \param num receives the array size (number of suites).
+ * \return the translated common cipher suites, in preference order.
+ */
+static inline const br_suite_translated *
+br_ssl_server_get_client_suites(const br_ssl_server_context *cc, size_t *num)
+{
+ *num = cc->client_suites_num;
+ return cc->client_suites;
+}
+
+/**
+ * \brief Get the hash functions and signature algorithms supported by
+ * the client.
+ *
+ * This value is a bit field:
+ *
+ * - If RSA (PKCS#1 v1.5) is supported with hash function of ID `x`,
+ * then bit `x` is set (hash function ID is 0 for the special MD5+SHA-1,
+ * or 2 to 6 for the SHA family).
+ *
+ * - If ECDSA is supported with hash function of ID `x`, then bit `8+x`
+ * is set.
+ *
+ * - Newer algorithms are symbolic 16-bit identifiers that do not
+ * represent signature algorithm and hash function separately. If
+ * the TLS-level identifier is `0x0800+x` for a `x` in the 0..15
+ * range, then bit `16+x` is set.
+ *
+ * "New algorithms" are currently defined only in draft documents, so
+ * this support is subject to possible change. Right now (early 2017),
+ * this maps ed25519 (EdDSA on Curve25519) to bit 23, and ed448 (EdDSA
+ * on Curve448) to bit 24. If the identifiers on the wire change in
+ * future document, then the decoding mechanism in BearSSL will be
+ * amended to keep mapping ed25519 and ed448 on bits 23 and 24,
+ * respectively. Mapping of other new algorithms (e.g. RSA/PSS) is not
+ * guaranteed yet.
+ *
+ * \param cc server context.
+ * \return the client-supported hash functions and signature algorithms.
+ */
+static inline uint32_t
+br_ssl_server_get_client_hashes(const br_ssl_server_context *cc)
+{
+ return cc->hashes;
+}
+
+/**
+ * \brief Get the elliptic curves supported by the client.
+ *
+ * This is a bit field (bit x is set if curve of ID x is supported).
+ *
+ * \param cc server context.
+ * \return the client-supported elliptic curves.
+ */
+static inline uint32_t
+br_ssl_server_get_client_curves(const br_ssl_server_context *cc)
+{
+ return cc->curves;
+}
+
+/**
+ * \brief Clear the complete contents of a SSL server context.
+ *
+ * Everything is cleared, including the reference to the configured buffer,
+ * implementations, cipher suites and state. This is a preparatory step
+ * to assembling a custom profile.
+ *
+ * \param cc server context to clear.
+ */
+void br_ssl_server_zero(br_ssl_server_context *cc);
+
+/**
+ * \brief Set an externally provided policy context.
+ *
+ * The policy context's methods are invoked to decide the cipher suite
+ * and certificate chain, and to perform operations involving the server's
+ * private key.
+ *
+ * \param cc server context.
+ * \param pctx policy context (pointer to its vtable field).
+ */
+static inline void
+br_ssl_server_set_policy(br_ssl_server_context *cc,
+ const br_ssl_server_policy_class **pctx)
+{
+ cc->policy_vtable = pctx;
+}
+
+/**
+ * \brief Set the server certificate chain and key (single RSA case).
+ *
+ * This function uses a policy context included in the server context.
+ * It configures use of a single server certificate chain with a RSA
+ * private key. The `allowed_usages` is a combination of usages, namely
+ * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
+ * the corresponding cipher suites (i.e. `TLS_RSA_*` use the RSA key for
+ * key exchange, while `TLS_ECDHE_RSA_*` use the RSA key for signatures).
+ *
+ * \param cc server context.
+ * \param chain server certificate chain to send to the client.
+ * \param chain_len chain length (number of certificates).
+ * \param sk server private key (RSA).
+ * \param allowed_usages allowed private key usages.
+ * \param irsacore RSA core implementation.
+ * \param irsasign RSA signature implementation (PKCS#1 v1.5).
+ */
+void br_ssl_server_set_single_rsa(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk, unsigned allowed_usages,
+ br_rsa_private irsacore, br_rsa_pkcs1_sign irsasign);
+
+/**
+ * \brief Set the server certificate chain and key (single EC case).
+ *
+ * This function uses a policy context included in the server context.
+ * It configures use of a single server certificate chain with an EC
+ * private key. The `allowed_usages` is a combination of usages, namely
+ * `BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`; this enables or disables
+ * the corresponding cipher suites (i.e. `TLS_ECDH_*` use the EC key for
+ * key exchange, while `TLS_ECDHE_ECDSA_*` use the EC key for signatures).
+ *
+ * In order to support `TLS_ECDH_*` cipher suites (non-ephemeral ECDH),
+ * the algorithm type of the key used by the issuing CA to sign the
+ * server's certificate must be provided, as `cert_issuer_key_type`
+ * parameter (this value is either `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`).
+ *
+ * \param cc server context.
+ * \param chain server certificate chain to send.
+ * \param chain_len chain length (number of certificates).
+ * \param sk server private key (EC).
+ * \param allowed_usages allowed private key usages.
+ * \param cert_issuer_key_type issuing CA's key type.
+ * \param iec EC core implementation.
+ * \param iecdsa ECDSA signature implementation ("asn1" format).
+ */
+void br_ssl_server_set_single_ec(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk, unsigned allowed_usages,
+ unsigned cert_issuer_key_type,
+ const br_ec_impl *iec, br_ecdsa_sign iecdsa);
+
+/**
+ * \brief Activate client certificate authentication.
+ *
+ * The trust anchor encoded X.500 names (DN) to send to the client are
+ * provided. A client certificate will be requested and validated through
+ * the X.509 validator configured in the SSL engine. If `num` is 0, then
+ * client certificate authentication is disabled.
+ *
+ * If the client does not send a certificate, or on validation failure,
+ * the handshake aborts. Unauthenticated clients can be tolerated by
+ * setting the `BR_OPT_TOLERATE_NO_CLIENT_AUTH` flag.
+ *
+ * The provided array is linked in, not copied, so that pointer must
+ * remain valid as long as anchor names may be used.
+ *
+ * \param cc server context.
+ * \param ta_names encoded trust anchor names.
+ * \param num number of encoded trust anchor names.
+ */
+static inline void
+br_ssl_server_set_trust_anchor_names(br_ssl_server_context *cc,
+ const br_x500_name *ta_names, size_t num)
+{
+ cc->ta_names = ta_names;
+ cc->tas = NULL;
+ cc->num_tas = num;
+}
+
+/**
+ * \brief Activate client certificate authentication.
+ *
+ * This is a variant for `br_ssl_server_set_trust_anchor_names()`: the
+ * trust anchor names are provided not as an array of stand-alone names
+ * (`br_x500_name` structures), but as an array of trust anchors
+ * (`br_x509_trust_anchor` structures). The server engine itself will
+ * only use the `dn` field of each trust anchor. This is meant to allow
+ * defining a single array of trust anchors, to be used here and in the
+ * X.509 validation engine itself.
+ *
+ * The provided array is linked in, not copied, so that pointer must
+ * remain valid as long as anchor names may be used.
+ *
+ * \param cc server context.
+ * \param tas trust anchors (only names are used).
+ * \param num number of trust anchors.
+ */
+static inline void
+br_ssl_server_set_trust_anchor_names_alt(br_ssl_server_context *cc,
+ const br_x509_trust_anchor *tas, size_t num)
+{
+ cc->ta_names = NULL;
+ cc->tas = tas;
+ cc->num_tas = num;
+}
+
+/**
+ * \brief Configure the cache for session parameters.
+ *
+ * The cache context is provided as a pointer to its first field (vtable
+ * pointer).
+ *
+ * \param cc server context.
+ * \param vtable session cache context.
+ */
+static inline void
+br_ssl_server_set_cache(br_ssl_server_context *cc,
+ const br_ssl_session_cache_class **vtable)
+{
+ cc->cache_vtable = vtable;
+}
+
+/**
+ * \brief Prepare or reset a server context for handling an incoming client.
+ *
+ * \param cc server context.
+ * \return 1 on success, 0 on error.
+ */
+int br_ssl_server_reset(br_ssl_server_context *cc);
+
+/* ===================================================================== */
+
+/*
+ * Context for the simplified I/O context. The transport medium is accessed
+ * through the low_read() and low_write() callback functions, each with
+ * its own opaque context pointer.
+ *
+ * low_read() read some bytes, at most 'len' bytes, into data[]. The
+ * returned value is the number of read bytes, or -1 on error.
+ * The 'len' parameter is guaranteed never to exceed 20000,
+ * so the length always fits in an 'int' on all platforms.
+ *
+ * low_write() write up to 'len' bytes, to be read from data[]. The
+ * returned value is the number of written bytes, or -1 on
+ * error. The 'len' parameter is guaranteed never to exceed
+ * 20000, so the length always fits in an 'int' on all
+ * parameters.
+ *
+ * A socket closure (if the transport medium is a socket) should be reported
+ * as an error (-1). The callbacks shall endeavour to block until at least
+ * one byte can be read or written; a callback returning 0 at times is
+ * acceptable, but this normally leads to the callback being immediately
+ * called again, so the callback should at least always try to block for
+ * some time if no I/O can take place.
+ *
+ * The SSL engine naturally applies some buffering, so the callbacks need
+ * not apply buffers of their own.
+ */
+/**
+ * \brief Context structure for the simplified SSL I/O wrapper.
+ *
+ * This structure is initialised with `br_sslio_init()`. Its contents
+ * are opaque and shall not be accessed directly.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ br_ssl_engine_context *engine;
+ int (*low_read)(void *read_context,
+ unsigned char *data, size_t len);
+ void *read_context;
+ int (*low_write)(void *write_context,
+ const unsigned char *data, size_t len);
+ void *write_context;
+#endif
+} br_sslio_context;
+
+/**
+ * \brief Initialise a simplified I/O wrapper context.
+ *
+ * The simplified I/O wrapper offers a simpler read/write API for a SSL
+ * engine (client or server), using the provided callback functions for
+ * reading data from, or writing data to, the transport medium.
+ *
+ * The callback functions have the following semantics:
+ *
+ * - Each callback receives an opaque context value (of type `void *`)
+ * that the callback may use arbitrarily (or possibly ignore).
+ *
+ * - `low_read()` reads at least one byte, at most `len` bytes, from
+ * the transport medium. Read bytes shall be written in `data`.
+ *
+ * - `low_write()` writes at least one byte, at most `len` bytes, unto
+ * the transport medium. The bytes to write are read from `data`.
+ *
+ * - The `len` parameter is never zero, and is always lower than 20000.
+ *
+ * - The number of processed bytes (read or written) is returned. Since
+ * that number is less than 20000, it always fits on an `int`.
+ *
+ * - On error, the callbacks return -1. Reaching end-of-stream is an
+ * error. Errors are permanent: the SSL connection is terminated.
+ *
+ * - Callbacks SHOULD NOT return 0. This is tolerated, as long as
+ * callbacks endeavour to block for some non-negligible amount of
+ * time until at least one byte can be sent or received (if a
+ * callback returns 0, then the wrapper invokes it again
+ * immediately).
+ *
+ * - Callbacks MAY return as soon as at least one byte is processed;
+ * they MAY also insist on reading or writing _all_ requested bytes.
+ * Since SSL is a self-terminated protocol (each record has a length
+ * header), this does not change semantics.
+ *
+ * - Callbacks need not apply any buffering (for performance) since SSL
+ * itself uses buffers.
+ *
+ * \param ctx wrapper context to initialise.
+ * \param engine SSL engine to wrap.
+ * \param low_read callback for reading data from the transport.
+ * \param read_context context pointer for `low_read()`.
+ * \param low_write callback for writing data on the transport.
+ * \param write_context context pointer for `low_write()`.
+ */
+void br_sslio_init(br_sslio_context *ctx,
+ br_ssl_engine_context *engine,
+ int (*low_read)(void *read_context,
+ unsigned char *data, size_t len),
+ void *read_context,
+ int (*low_write)(void *write_context,
+ const unsigned char *data, size_t len),
+ void *write_context);
+
+/**
+ * \brief Read some application data from a SSL connection.
+ *
+ * If `len` is zero, then this function returns 0 immediately. In
+ * all other cases, it never returns 0.
+ *
+ * This call returns only when at least one byte has been obtained.
+ * Returned value is the number of bytes read, or -1 on error. The
+ * number of bytes always fits on an 'int' (data from a single SSL/TLS
+ * record is returned).
+ *
+ * On error or SSL closure, this function returns -1. The caller should
+ * inspect the error status on the SSL engine to distinguish between
+ * normal closure and error.
+ *
+ * \param cc SSL wrapper context.
+ * \param dst destination buffer for application data.
+ * \param len maximum number of bytes to obtain.
+ * \return number of bytes obtained, or -1 on error.
+ */
+int br_sslio_read(br_sslio_context *cc, void *dst, size_t len);
+
+/**
+ * \brief Read application data from a SSL connection.
+ *
+ * This calls returns only when _all_ requested `len` bytes are read,
+ * or an error is reached. Returned value is 0 on success, -1 on error.
+ * A normal (verified) SSL closure before that many bytes are obtained
+ * is reported as an error by this function.
+ *
+ * \param cc SSL wrapper context.
+ * \param dst destination buffer for application data.
+ * \param len number of bytes to obtain.
+ * \return 0 on success, or -1 on error.
+ */
+int br_sslio_read_all(br_sslio_context *cc, void *dst, size_t len);
+
+/**
+ * \brief Write some application data unto a SSL connection.
+ *
+ * If `len` is zero, then this function returns 0 immediately. In
+ * all other cases, it never returns 0.
+ *
+ * This call returns only when at least one byte has been written.
+ * Returned value is the number of bytes written, or -1 on error. The
+ * number of bytes always fits on an 'int' (less than 20000).
+ *
+ * On error or SSL closure, this function returns -1. The caller should
+ * inspect the error status on the SSL engine to distinguish between
+ * normal closure and error.
+ *
+ * **Important:** SSL is buffered; a "written" byte is a byte that was
+ * injected into the wrapped SSL engine, but this does not necessarily mean
+ * that it has been scheduled for sending. Use `br_sslio_flush()` to
+ * ensure that all pending data has been sent to the transport medium.
+ *
+ * \param cc SSL wrapper context.
+ * \param src source buffer for application data.
+ * \param len maximum number of bytes to write.
+ * \return number of bytes written, or -1 on error.
+ */
+int br_sslio_write(br_sslio_context *cc, const void *src, size_t len);
+
+/**
+ * \brief Write application data unto a SSL connection.
+ *
+ * This calls returns only when _all_ requested `len` bytes have been
+ * written, or an error is reached. Returned value is 0 on success, -1
+ * on error. A normal (verified) SSL closure before that many bytes are
+ * written is reported as an error by this function.
+ *
+ * **Important:** SSL is buffered; a "written" byte is a byte that was
+ * injected into the wrapped SSL engine, but this does not necessarily mean
+ * that it has been scheduled for sending. Use `br_sslio_flush()` to
+ * ensure that all pending data has been sent to the transport medium.
+ *
+ * \param cc SSL wrapper context.
+ * \param src source buffer for application data.
+ * \param len number of bytes to write.
+ * \return 0 on success, or -1 on error.
+ */
+int br_sslio_write_all(br_sslio_context *cc, const void *src, size_t len);
+
+/**
+ * \brief Flush pending data.
+ *
+ * This call makes sure that any buffered application data in the
+ * provided context (including the wrapped SSL engine) has been sent
+ * to the transport medium (i.e. accepted by the `low_write()` callback
+ * method). If there is no such pending data, then this function does
+ * nothing (and returns a success, i.e. 0).
+ *
+ * If the underlying transport medium has its own buffers, then it is
+ * up to the caller to ensure the corresponding flushing.
+ *
+ * Returned value is 0 on success, -1 on error.
+ *
+ * \param cc SSL wrapper context.
+ * \return 0 on success, or -1 on error.
+ */
+int br_sslio_flush(br_sslio_context *cc);
+
+/**
+ * \brief Close the SSL connection.
+ *
+ * This call runs the SSL closure protocol (sending a `close_notify`,
+ * receiving the response `close_notify`). When it returns, the SSL
+ * connection is finished. It is still up to the caller to manage the
+ * possible transport-level termination, if applicable (alternatively,
+ * the underlying transport stream may be reused for non-SSL messages).
+ *
+ * Returned value is 0 on success, -1 on error. A failure by the peer
+ * to process the complete closure protocol (i.e. sending back the
+ * `close_notify`) is an error.
+ *
+ * \param cc SSL wrapper context.
+ * \return 0 on success, or -1 on error.
+ */
+int br_sslio_close(br_sslio_context *cc);
+
+/* ===================================================================== */
+
+/*
+ * Symbolic constants for cipher suites.
+ */
+
+/* From RFC 5246 */
+#define BR_TLS_NULL_WITH_NULL_NULL 0x0000
+#define BR_TLS_RSA_WITH_NULL_MD5 0x0001
+#define BR_TLS_RSA_WITH_NULL_SHA 0x0002
+#define BR_TLS_RSA_WITH_NULL_SHA256 0x003B
+#define BR_TLS_RSA_WITH_RC4_128_MD5 0x0004
+#define BR_TLS_RSA_WITH_RC4_128_SHA 0x0005
+#define BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x000A
+#define BR_TLS_RSA_WITH_AES_128_CBC_SHA 0x002F
+#define BR_TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
+#define BR_TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C
+#define BR_TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D
+#define BR_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA 0x000D
+#define BR_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA 0x0010
+#define BR_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA 0x0013
+#define BR_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016
+#define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA 0x0030
+#define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA 0x0031
+#define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA 0x0032
+#define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033
+#define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA 0x0036
+#define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA 0x0037
+#define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA 0x0038
+#define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039
+#define BR_TLS_DH_DSS_WITH_AES_128_CBC_SHA256 0x003E
+#define BR_TLS_DH_RSA_WITH_AES_128_CBC_SHA256 0x003F
+#define BR_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 0x0040
+#define BR_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067
+#define BR_TLS_DH_DSS_WITH_AES_256_CBC_SHA256 0x0068
+#define BR_TLS_DH_RSA_WITH_AES_256_CBC_SHA256 0x0069
+#define BR_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 0x006A
+#define BR_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B
+#define BR_TLS_DH_anon_WITH_RC4_128_MD5 0x0018
+#define BR_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B
+#define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034
+#define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A
+#define BR_TLS_DH_anon_WITH_AES_128_CBC_SHA256 0x006C
+#define BR_TLS_DH_anon_WITH_AES_256_CBC_SHA256 0x006D
+
+/* From RFC 4492 */
+#define BR_TLS_ECDH_ECDSA_WITH_NULL_SHA 0xC001
+#define BR_TLS_ECDH_ECDSA_WITH_RC4_128_SHA 0xC002
+#define BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC003
+#define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004
+#define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005
+#define BR_TLS_ECDHE_ECDSA_WITH_NULL_SHA 0xC006
+#define BR_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA 0xC007
+#define BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC008
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A
+#define BR_TLS_ECDH_RSA_WITH_NULL_SHA 0xC00B
+#define BR_TLS_ECDH_RSA_WITH_RC4_128_SHA 0xC00C
+#define BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA 0xC00D
+#define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E
+#define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F
+#define BR_TLS_ECDHE_RSA_WITH_NULL_SHA 0xC010
+#define BR_TLS_ECDHE_RSA_WITH_RC4_128_SHA 0xC011
+#define BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012
+#define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013
+#define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014
+#define BR_TLS_ECDH_anon_WITH_NULL_SHA 0xC015
+#define BR_TLS_ECDH_anon_WITH_RC4_128_SHA 0xC016
+#define BR_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA 0xC017
+#define BR_TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018
+#define BR_TLS_ECDH_anon_WITH_AES_256_CBC_SHA 0xC019
+
+/* From RFC 5288 */
+#define BR_TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C
+#define BR_TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D
+#define BR_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009E
+#define BR_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009F
+#define BR_TLS_DH_RSA_WITH_AES_128_GCM_SHA256 0x00A0
+#define BR_TLS_DH_RSA_WITH_AES_256_GCM_SHA384 0x00A1
+#define BR_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 0x00A2
+#define BR_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 0x00A3
+#define BR_TLS_DH_DSS_WITH_AES_128_GCM_SHA256 0x00A4
+#define BR_TLS_DH_DSS_WITH_AES_256_GCM_SHA384 0x00A5
+#define BR_TLS_DH_anon_WITH_AES_128_GCM_SHA256 0x00A6
+#define BR_TLS_DH_anon_WITH_AES_256_GCM_SHA384 0x00A7
+
+/* From RFC 5289 */
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
+#define BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025
+#define BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026
+#define BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027
+#define BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028
+#define BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029
+#define BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C
+#define BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D
+#define BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E
+#define BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F
+#define BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030
+#define BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031
+#define BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032
+
+/* From RFC 6655 and 7251 */
+#define BR_TLS_RSA_WITH_AES_128_CCM 0xC09C
+#define BR_TLS_RSA_WITH_AES_256_CCM 0xC09D
+#define BR_TLS_RSA_WITH_AES_128_CCM_8 0xC0A0
+#define BR_TLS_RSA_WITH_AES_256_CCM_8 0xC0A1
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM 0xC0AD
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 0xC0AF
+
+/* From RFC 7905 */
+#define BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA8
+#define BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA9
+#define BR_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCAA
+#define BR_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAB
+#define BR_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAC
+#define BR_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAD
+#define BR_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 0xCCAE
+
+/* From RFC 7507 */
+#define BR_TLS_FALLBACK_SCSV 0x5600
+
+/*
+ * Symbolic constants for alerts.
+ */
+#define BR_ALERT_CLOSE_NOTIFY 0
+#define BR_ALERT_UNEXPECTED_MESSAGE 10
+#define BR_ALERT_BAD_RECORD_MAC 20
+#define BR_ALERT_RECORD_OVERFLOW 22
+#define BR_ALERT_DECOMPRESSION_FAILURE 30
+#define BR_ALERT_HANDSHAKE_FAILURE 40
+#define BR_ALERT_BAD_CERTIFICATE 42
+#define BR_ALERT_UNSUPPORTED_CERTIFICATE 43
+#define BR_ALERT_CERTIFICATE_REVOKED 44
+#define BR_ALERT_CERTIFICATE_EXPIRED 45
+#define BR_ALERT_CERTIFICATE_UNKNOWN 46
+#define BR_ALERT_ILLEGAL_PARAMETER 47
+#define BR_ALERT_UNKNOWN_CA 48
+#define BR_ALERT_ACCESS_DENIED 49
+#define BR_ALERT_DECODE_ERROR 50
+#define BR_ALERT_DECRYPT_ERROR 51
+#define BR_ALERT_PROTOCOL_VERSION 70
+#define BR_ALERT_INSUFFICIENT_SECURITY 71
+#define BR_ALERT_INTERNAL_ERROR 80
+#define BR_ALERT_USER_CANCELED 90
+#define BR_ALERT_NO_RENEGOTIATION 100
+#define BR_ALERT_UNSUPPORTED_EXTENSION 110
+#define BR_ALERT_NO_APPLICATION_PROTOCOL 120
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/inc/bearssl_x509.h b/test/monniaux/BearSSL/inc/bearssl_x509.h
new file mode 100644
index 00000000..49d2fba0
--- /dev/null
+++ b/test/monniaux/BearSSL/inc/bearssl_x509.h
@@ -0,0 +1,1397 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BR_BEARSSL_X509_H__
+#define BR_BEARSSL_X509_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "bearssl_ec.h"
+#include "bearssl_hash.h"
+#include "bearssl_rsa.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file bearssl_x509.h
+ *
+ * # X.509 Certificate Chain Processing
+ *
+ * An X.509 processing engine receives an X.509 chain, chunk by chunk,
+ * as received from a SSL/TLS client or server (the client receives the
+ * server's certificate chain, and the server receives the client's
+ * certificate chain if it requested a client certificate). The chain
+ * is thus injected in the engine in SSL order (end-entity first).
+ *
+ * The engine's job is to return the public key to use for SSL/TLS.
+ * How exactly that key is obtained and verified is entirely up to the
+ * engine.
+ *
+ * **The "known key" engine** returns a public key which is already known
+ * from out-of-band information (e.g. the client _remembers_ the key from
+ * a previous connection, as in the usual SSH model). This is the simplest
+ * engine since it simply ignores the chain, thereby avoiding the need
+ * for any decoding logic.
+ *
+ * **The "minimal" engine** implements minimal X.509 decoding and chain
+ * validation:
+ *
+ * - The provided chain should validate "as is". There is no attempt
+ * at reordering, skipping or downloading extra certificates.
+ *
+ * - X.509 v1, v2 and v3 certificates are supported.
+ *
+ * - Trust anchors are a DN and a public key. Each anchor is either a
+ * "CA" anchor, or a non-CA.
+ *
+ * - If the end-entity certificate matches a non-CA anchor (subject DN
+ * is equal to the non-CA name, and public key is also identical to
+ * the anchor key), then this is a _direct trust_ case and the
+ * remaining certificates are ignored.
+ *
+ * - Unless direct trust is applied, the chain must be verifiable up to
+ * a certificate whose issuer DN matches the DN from a "CA" trust anchor,
+ * and whose signature is verifiable against that anchor's public key.
+ * Subsequent certificates in the chain are ignored.
+ *
+ * - The engine verifies subject/issuer DN matching, and enforces
+ * processing of Basic Constraints and Key Usage extensions. The
+ * Authority Key Identifier, Subject Key Identifier, Issuer Alt Name,
+ * Subject Directory Attribute, CRL Distribution Points, Freshest CRL,
+ * Authority Info Access and Subject Info Access extensions are
+ * ignored. The Subject Alt Name is decoded for the end-entity
+ * certificate under some conditions (see below). Other extensions
+ * are ignored if non-critical, or imply chain rejection if critical.
+ *
+ * - The Subject Alt Name extension is parsed for names of type `dNSName`
+ * when decoding the end-entity certificate, and only if there is a
+ * server name to match. If there is no SAN extension, then the
+ * Common Name from the subjectDN is used. That name matching is
+ * case-insensitive and honours a single starting wildcard (i.e. if
+ * the name in the certificate starts with "`*.`" then this matches
+ * any word as first element). Note: this name matching is performed
+ * also in the "direct trust" model.
+ *
+ * - DN matching is byte-to-byte equality (a future version might
+ * include some limited processing for case-insensitive matching and
+ * whitespace normalisation).
+ *
+ * - Successful validation produces a public key type but also a set
+ * of allowed usages (`BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`).
+ * The caller is responsible for checking that the key type and
+ * usages are compatible with the expected values (e.g. with the
+ * selected cipher suite, when the client validates the server's
+ * certificate).
+ *
+ * **Important caveats:**
+ *
+ * - The "minimal" engine does not check revocation status. The relevant
+ * extensions are ignored, and CRL or OCSP responses are not gathered
+ * or checked.
+ *
+ * - The "minimal" engine does not currently support Name Constraints
+ * (some basic functionality to handle sub-domains may be added in a
+ * later version).
+ *
+ * - The decoder is not "validating" in the sense that it won't reject
+ * some certificates with invalid field values when these fields are
+ * not actually processed.
+ */
+
+/*
+ * X.509 error codes are in the 32..63 range.
+ */
+
+/** \brief X.509 status: validation was successful; this is not actually
+ an error. */
+#define BR_ERR_X509_OK 32
+
+/** \brief X.509 status: invalid value in an ASN.1 structure. */
+#define BR_ERR_X509_INVALID_VALUE 33
+
+/** \brief X.509 status: truncated certificate. */
+#define BR_ERR_X509_TRUNCATED 34
+
+/** \brief X.509 status: empty certificate chain (no certificate at all). */
+#define BR_ERR_X509_EMPTY_CHAIN 35
+
+/** \brief X.509 status: decoding error: inner element extends beyond
+ outer element size. */
+#define BR_ERR_X509_INNER_TRUNC 36
+
+/** \brief X.509 status: decoding error: unsupported tag class (application
+ or private). */
+#define BR_ERR_X509_BAD_TAG_CLASS 37
+
+/** \brief X.509 status: decoding error: unsupported tag value. */
+#define BR_ERR_X509_BAD_TAG_VALUE 38
+
+/** \brief X.509 status: decoding error: indefinite length. */
+#define BR_ERR_X509_INDEFINITE_LENGTH 39
+
+/** \brief X.509 status: decoding error: extraneous element. */
+#define BR_ERR_X509_EXTRA_ELEMENT 40
+
+/** \brief X.509 status: decoding error: unexpected element. */
+#define BR_ERR_X509_UNEXPECTED 41
+
+/** \brief X.509 status: decoding error: expected constructed element, but
+ is primitive. */
+#define BR_ERR_X509_NOT_CONSTRUCTED 42
+
+/** \brief X.509 status: decoding error: expected primitive element, but
+ is constructed. */
+#define BR_ERR_X509_NOT_PRIMITIVE 43
+
+/** \brief X.509 status: decoding error: BIT STRING length is not multiple
+ of 8. */
+#define BR_ERR_X509_PARTIAL_BYTE 44
+
+/** \brief X.509 status: decoding error: BOOLEAN value has invalid length. */
+#define BR_ERR_X509_BAD_BOOLEAN 45
+
+/** \brief X.509 status: decoding error: value is off-limits. */
+#define BR_ERR_X509_OVERFLOW 46
+
+/** \brief X.509 status: invalid distinguished name. */
+#define BR_ERR_X509_BAD_DN 47
+
+/** \brief X.509 status: invalid date/time representation. */
+#define BR_ERR_X509_BAD_TIME 48
+
+/** \brief X.509 status: certificate contains unsupported features that
+ cannot be ignored. */
+#define BR_ERR_X509_UNSUPPORTED 49
+
+/** \brief X.509 status: key or signature size exceeds internal limits. */
+#define BR_ERR_X509_LIMIT_EXCEEDED 50
+
+/** \brief X.509 status: key type does not match that which was expected. */
+#define BR_ERR_X509_WRONG_KEY_TYPE 51
+
+/** \brief X.509 status: signature is invalid. */
+#define BR_ERR_X509_BAD_SIGNATURE 52
+
+/** \brief X.509 status: validation time is unknown. */
+#define BR_ERR_X509_TIME_UNKNOWN 53
+
+/** \brief X.509 status: certificate is expired or not yet valid. */
+#define BR_ERR_X509_EXPIRED 54
+
+/** \brief X.509 status: issuer/subject DN mismatch in the chain. */
+#define BR_ERR_X509_DN_MISMATCH 55
+
+/** \brief X.509 status: expected server name was not found in the chain. */
+#define BR_ERR_X509_BAD_SERVER_NAME 56
+
+/** \brief X.509 status: unknown critical extension in certificate. */
+#define BR_ERR_X509_CRITICAL_EXTENSION 57
+
+/** \brief X.509 status: not a CA, or path length constraint violation */
+#define BR_ERR_X509_NOT_CA 58
+
+/** \brief X.509 status: Key Usage extension prohibits intended usage. */
+#define BR_ERR_X509_FORBIDDEN_KEY_USAGE 59
+
+/** \brief X.509 status: public key found in certificate is too small. */
+#define BR_ERR_X509_WEAK_PUBLIC_KEY 60
+
+/** \brief X.509 status: chain could not be linked to a trust anchor. */
+#define BR_ERR_X509_NOT_TRUSTED 62
+
+/**
+ * \brief Aggregate structure for public keys.
+ */
+typedef struct {
+ /** \brief Key type: `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC` */
+ unsigned char key_type;
+ /** \brief Actual public key. */
+ union {
+ /** \brief RSA public key. */
+ br_rsa_public_key rsa;
+ /** \brief EC public key. */
+ br_ec_public_key ec;
+ } key;
+} br_x509_pkey;
+
+/**
+ * \brief Distinguished Name (X.500) structure.
+ *
+ * The DN is DER-encoded.
+ */
+typedef struct {
+ /** \brief Encoded DN data. */
+ unsigned char *data;
+ /** \brief Encoded DN length (in bytes). */
+ size_t len;
+} br_x500_name;
+
+/**
+ * \brief Trust anchor structure.
+ */
+typedef struct {
+ /** \brief Encoded DN (X.500 name). */
+ br_x500_name dn;
+ /** \brief Anchor flags (e.g. `BR_X509_TA_CA`). */
+ unsigned flags;
+ /** \brief Anchor public key. */
+ br_x509_pkey pkey;
+} br_x509_trust_anchor;
+
+/**
+ * \brief Trust anchor flag: CA.
+ *
+ * A "CA" anchor is deemed fit to verify signatures on certificates.
+ * A "non-CA" anchor is accepted only for direct trust (server's
+ * certificate name and key match the anchor).
+ */
+#define BR_X509_TA_CA 0x0001
+
+/*
+ * Key type: combination of a basic key type (low 4 bits) and some
+ * optional flags.
+ *
+ * For a public key, the basic key type only is set.
+ *
+ * For an expected key type, the flags indicate the intended purpose(s)
+ * for the key; the basic key type may be set to 0 to indicate that any
+ * key type compatible with the indicated purpose is acceptable.
+ */
+/** \brief Key type: algorithm is RSA. */
+#define BR_KEYTYPE_RSA 1
+/** \brief Key type: algorithm is EC. */
+#define BR_KEYTYPE_EC 2
+
+/**
+ * \brief Key type: usage is "key exchange".
+ *
+ * This value is combined (with bitwise OR) with the algorithm
+ * (`BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`) when informing the X.509
+ * validation engine that it should find a public key of that type,
+ * fit for key exchanges (e.g. `TLS_RSA_*` and `TLS_ECDH_*` cipher
+ * suites).
+ */
+#define BR_KEYTYPE_KEYX 0x10
+
+/**
+ * \brief Key type: usage is "signature".
+ *
+ * This value is combined (with bitwise OR) with the algorithm
+ * (`BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`) when informing the X.509
+ * validation engine that it should find a public key of that type,
+ * fit for signatures (e.g. `TLS_ECDHE_*` cipher suites).
+ */
+#define BR_KEYTYPE_SIGN 0x20
+
+/*
+ * start_chain Called when a new chain is started. If 'server_name'
+ * is not NULL and non-empty, then it is a name that
+ * should be looked for in the EE certificate (in the
+ * SAN extension as dNSName, or in the subjectDN's CN
+ * if there is no SAN extension).
+ * The caller ensures that the provided 'server_name'
+ * pointer remains valid throughout validation.
+ *
+ * start_cert Begins a new certificate in the chain. The provided
+ * length is in bytes; this is the total certificate length.
+ *
+ * append Get some additional bytes for the current certificate.
+ *
+ * end_cert Ends the current certificate.
+ *
+ * end_chain Called at the end of the chain. Returned value is
+ * 0 on success, or a non-zero error code.
+ *
+ * get_pkey Returns the EE certificate public key.
+ *
+ * For a complete chain, start_chain() and end_chain() are always
+ * called. For each certificate, start_cert(), some append() calls, then
+ * end_cert() are called, in that order. There may be no append() call
+ * at all if the certificate is empty (which is not valid but may happen
+ * if the peer sends exactly that).
+ *
+ * get_pkey() shall return a pointer to a structure that is valid as
+ * long as a new chain is not started. This may be a sub-structure
+ * within the context for the engine. This function MAY return a valid
+ * pointer to a public key even in some cases of validation failure,
+ * depending on the validation engine.
+ */
+
+/**
+ * \brief Class type for an X.509 engine.
+ *
+ * A certificate chain validation uses a caller-allocated context, which
+ * contains the running state for that validation. Methods are called
+ * in due order:
+ *
+ * - `start_chain()` is called at the start of the validation.
+ * - Certificates are processed one by one, in SSL order (end-entity
+ * comes first). For each certificate, the following methods are
+ * called:
+ *
+ * - `start_cert()` at the beginning of the certificate.
+ * - `append()` is called zero, one or more times, to provide
+ * the certificate (possibly in chunks).
+ * - `end_cert()` at the end of the certificate.
+ *
+ * - `end_chain()` is called when the last certificate in the chain
+ * was processed.
+ * - `get_pkey()` is called after chain processing, if the chain
+ * validation was successful.
+ *
+ * A context structure may be reused; the `start_chain()` method shall
+ * ensure (re)initialisation.
+ */
+typedef struct br_x509_class_ br_x509_class;
+struct br_x509_class_ {
+ /**
+ * \brief X.509 context size, in bytes.
+ */
+ size_t context_size;
+
+ /**
+ * \brief Start a new chain.
+ *
+ * This method shall set the vtable (first field) of the context
+ * structure.
+ *
+ * The `server_name`, if not `NULL`, will be considered as a
+ * fully qualified domain name, to be matched against the `dNSName`
+ * elements of the end-entity certificate's SAN extension (if there
+ * is no SAN, then the Common Name from the subjectDN will be used).
+ * If `server_name` is `NULL` then no such matching is performed.
+ *
+ * \param ctx validation context.
+ * \param server_name server name to match (or `NULL`).
+ */
+ void (*start_chain)(const br_x509_class **ctx,
+ const char *server_name);
+
+ /**
+ * \brief Start a new certificate.
+ *
+ * \param ctx validation context.
+ * \param length new certificate length (in bytes).
+ */
+ void (*start_cert)(const br_x509_class **ctx, uint32_t length);
+
+ /**
+ * \brief Receive some bytes for the current certificate.
+ *
+ * This function may be called several times in succession for
+ * a given certificate. The caller guarantees that for each
+ * call, `len` is not zero, and the sum of all chunk lengths
+ * for a certificate matches the total certificate length which
+ * was provided in the previous `start_cert()` call.
+ *
+ * If the new certificate is empty (no byte at all) then this
+ * function won't be called at all.
+ *
+ * \param ctx validation context.
+ * \param buf certificate data chunk.
+ * \param len certificate data chunk length (in bytes).
+ */
+ void (*append)(const br_x509_class **ctx,
+ const unsigned char *buf, size_t len);
+
+ /**
+ * \brief Finish the current certificate.
+ *
+ * This function is called when the end of the current certificate
+ * is reached.
+ *
+ * \param ctx validation context.
+ */
+ void (*end_cert)(const br_x509_class **ctx);
+
+ /**
+ * \brief Finish the chain.
+ *
+ * This function is called at the end of the chain. It shall
+ * return either 0 if the validation was successful, or a
+ * non-zero error code. The `BR_ERR_X509_*` constants are
+ * error codes, though other values may be possible.
+ *
+ * \param ctx validation context.
+ * \return 0 on success, or a non-zero error code.
+ */
+ unsigned (*end_chain)(const br_x509_class **ctx);
+
+ /**
+ * \brief Get the resulting end-entity public key.
+ *
+ * The decoded public key is returned. The returned pointer
+ * may be valid only as long as the context structure is
+ * unmodified, i.e. it may cease to be valid if the context
+ * is released or reused.
+ *
+ * This function _may_ return `NULL` if the validation failed.
+ * However, returning a public key does not mean that the
+ * validation was wholly successful; some engines may return
+ * a decoded public key even if the chain did not end on a
+ * trusted anchor.
+ *
+ * If validation succeeded and `usage` is not `NULL`, then
+ * `*usage` is filled with a combination of `BR_KEYTYPE_SIGN`
+ * and/or `BR_KEYTYPE_KEYX` that specifies the validated key
+ * usage types. It is the caller's responsibility to check
+ * that value against the intended use of the public key.
+ *
+ * \param ctx validation context.
+ * \return the end-entity public key, or `NULL`.
+ */
+ const br_x509_pkey *(*get_pkey)(
+ const br_x509_class *const *ctx, unsigned *usages);
+};
+
+/**
+ * \brief The "known key" X.509 engine structure.
+ *
+ * The structure contents are opaque (they shall not be accessed directly),
+ * except for the first field (the vtable).
+ *
+ * The "known key" engine returns an externally configured public key,
+ * and totally ignores the certificate contents.
+ */
+typedef struct {
+ /** \brief Reference to the context vtable. */
+ const br_x509_class *vtable;
+#ifndef BR_DOXYGEN_IGNORE
+ br_x509_pkey pkey;
+ unsigned usages;
+#endif
+} br_x509_knownkey_context;
+
+/**
+ * \brief Class instance for the "known key" X.509 engine.
+ */
+extern const br_x509_class br_x509_knownkey_vtable;
+
+/**
+ * \brief Initialize a "known key" X.509 engine with a known RSA public key.
+ *
+ * The `usages` parameter indicates the allowed key usages for that key
+ * (`BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`).
+ *
+ * The provided pointers are linked in, not copied, so they must remain
+ * valid while the public key may be in usage.
+ *
+ * \param ctx context to initialise.
+ * \param pk known public key.
+ * \param usages allowed key usages.
+ */
+void br_x509_knownkey_init_rsa(br_x509_knownkey_context *ctx,
+ const br_rsa_public_key *pk, unsigned usages);
+
+/**
+ * \brief Initialize a "known key" X.509 engine with a known EC public key.
+ *
+ * The `usages` parameter indicates the allowed key usages for that key
+ * (`BR_KEYTYPE_KEYX` and/or `BR_KEYTYPE_SIGN`).
+ *
+ * The provided pointers are linked in, not copied, so they must remain
+ * valid while the public key may be in usage.
+ *
+ * \param ctx context to initialise.
+ * \param pk known public key.
+ * \param usages allowed key usages.
+ */
+void br_x509_knownkey_init_ec(br_x509_knownkey_context *ctx,
+ const br_ec_public_key *pk, unsigned usages);
+
+#ifndef BR_DOXYGEN_IGNORE
+/*
+ * The minimal X.509 engine has some state buffers which must be large
+ * enough to simultaneously accommodate:
+ * -- the public key extracted from the current certificate;
+ * -- the signature on the current certificate or on the previous
+ * certificate;
+ * -- the public key extracted from the EE certificate.
+ *
+ * We store public key elements in their raw unsigned big-endian
+ * encoding. We want to support up to RSA-4096 with a short (up to 64
+ * bits) public exponent, thus a buffer for a public key must have
+ * length at least 520 bytes. Similarly, a RSA-4096 signature has length
+ * 512 bytes.
+ *
+ * Though RSA public exponents can formally be as large as the modulus
+ * (mathematically, even larger exponents would work, but PKCS#1 forbids
+ * them), exponents that do not fit on 32 bits are extremely rare,
+ * notably because some widespread implementations (e.g. Microsoft's
+ * CryptoAPI) don't support them. Moreover, large public exponent do not
+ * seem to imply any tangible security benefit, and they increase the
+ * cost of public key operations. The X.509 "minimal" engine will tolerate
+ * public exponents of arbitrary size as long as the modulus and the
+ * exponent can fit together in the dedicated buffer.
+ *
+ * EC public keys are shorter than RSA public keys; even with curve
+ * NIST P-521 (the largest curve we care to support), a public key is
+ * encoded over 133 bytes only.
+ */
+#define BR_X509_BUFSIZE_KEY 520
+#define BR_X509_BUFSIZE_SIG 512
+#endif
+
+/**
+ * \brief Type for receiving a name element.
+ *
+ * An array of such structures can be provided to the X.509 decoding
+ * engines. If the specified elements are found in the certificate
+ * subject DN or the SAN extension, then the name contents are copied
+ * as zero-terminated strings into the buffer.
+ *
+ * The decoder converts TeletexString and BMPString to UTF8String, and
+ * ensures that the resulting string is zero-terminated. If the string
+ * does not fit in the provided buffer, then the copy is aborted and an
+ * error is reported.
+ */
+typedef struct {
+ /**
+ * \brief Element OID.
+ *
+ * For X.500 name elements (to be extracted from the subject DN),
+ * this is the encoded OID for the requested name element; the
+ * first byte shall contain the length of the DER-encoded OID
+ * value, followed by the OID value (for instance, OID 2.5.4.3,
+ * for id-at-commonName, will be `03 55 04 03`). This is
+ * equivalent to full DER encoding with the length but without
+ * the tag.
+ *
+ * For SAN name elements, the first byte (`oid[0]`) has value 0,
+ * followed by another byte that matches the expected GeneralName
+ * tag. Allowed second byte values are then:
+ *
+ * - 1: `rfc822Name`
+ *
+ * - 2: `dNSName`
+ *
+ * - 6: `uniformResourceIdentifier`
+ *
+ * - 0: `otherName`
+ *
+ * If first and second byte are 0, then this is a SAN element of
+ * type `otherName`; the `oid[]` array should then contain, right
+ * after the two bytes of value 0, an encoded OID (with the same
+ * conventions as for X.500 name elements). If a match is found
+ * for that OID, then the corresponding name element will be
+ * extracted, as long as it is a supported string type.
+ */
+ const unsigned char *oid;
+
+ /**
+ * \brief Destination buffer.
+ */
+ char *buf;
+
+ /**
+ * \brief Length (in bytes) of the destination buffer.
+ *
+ * The buffer MUST NOT be smaller than 1 byte.
+ */
+ size_t len;
+
+ /**
+ * \brief Decoding status.
+ *
+ * Status is 0 if the name element was not found, 1 if it was
+ * found and decoded, or -1 on error. Error conditions include
+ * an unrecognised encoding, an invalid encoding, or a string
+ * too large for the destination buffer.
+ */
+ int status;
+
+} br_name_element;
+
+/**
+ * \brief The "minimal" X.509 engine structure.
+ *
+ * The structure contents are opaque (they shall not be accessed directly),
+ * except for the first field (the vtable).
+ *
+ * The "minimal" engine performs a rudimentary but serviceable X.509 path
+ * validation.
+ */
+typedef struct {
+ const br_x509_class *vtable;
+
+#ifndef BR_DOXYGEN_IGNORE
+ /* Structure for returning the EE public key. */
+ br_x509_pkey pkey;
+
+ /* CPU for the T0 virtual machine. */
+ struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+ } cpu;
+ uint32_t dp_stack[32];
+ uint32_t rp_stack[32];
+ int err;
+
+ /* Server name to match with the SAN / CN of the EE certificate. */
+ const char *server_name;
+
+ /* Validated key usages. */
+ unsigned char key_usages;
+
+ /* Explicitly set date and time. */
+ uint32_t days, seconds;
+
+ /* Current certificate length (in bytes). Set to 0 when the
+ certificate has been fully processed. */
+ uint32_t cert_length;
+
+ /* Number of certificates processed so far in the current chain.
+ It is incremented at the end of the processing of a certificate,
+ so it is 0 for the EE. */
+ uint32_t num_certs;
+
+ /* Certificate data chunk. */
+ const unsigned char *hbuf;
+ size_t hlen;
+
+ /* The pad serves as destination for various operations. */
+ unsigned char pad[256];
+
+ /* Buffer for EE public key data. */
+ unsigned char ee_pkey_data[BR_X509_BUFSIZE_KEY];
+
+ /* Buffer for currently decoded public key. */
+ unsigned char pkey_data[BR_X509_BUFSIZE_KEY];
+
+ /* Signature type: signer key type, offset to the hash
+ function OID (in the T0 data block) and hash function
+ output length (TBS hash length). */
+ unsigned char cert_signer_key_type;
+ uint16_t cert_sig_hash_oid;
+ unsigned char cert_sig_hash_len;
+
+ /* Current/last certificate signature. */
+ unsigned char cert_sig[BR_X509_BUFSIZE_SIG];
+ uint16_t cert_sig_len;
+
+ /* Minimum RSA key length (difference in bytes from 128). */
+ int16_t min_rsa_size;
+
+ /* Configured trust anchors. */
+ const br_x509_trust_anchor *trust_anchors;
+ size_t trust_anchors_num;
+
+ /*
+ * Multi-hasher for the TBS.
+ */
+ unsigned char do_mhash;
+ br_multihash_context mhash;
+ unsigned char tbs_hash[64];
+
+ /*
+ * Simple hasher for the subject/issuer DN.
+ */
+ unsigned char do_dn_hash;
+ const br_hash_class *dn_hash_impl;
+ br_hash_compat_context dn_hash;
+ unsigned char current_dn_hash[64];
+ unsigned char next_dn_hash[64];
+ unsigned char saved_dn_hash[64];
+
+ /*
+ * Name elements to gather.
+ */
+ br_name_element *name_elts;
+ size_t num_name_elts;
+
+ /*
+ * Public key cryptography implementations (signature verification).
+ */
+ br_rsa_pkcs1_vrfy irsa;
+ br_ecdsa_vrfy iecdsa;
+ const br_ec_impl *iec;
+#endif
+
+} br_x509_minimal_context;
+
+/**
+ * \brief Class instance for the "minimal" X.509 engine.
+ */
+extern const br_x509_class br_x509_minimal_vtable;
+
+/**
+ * \brief Initialise a "minimal" X.509 engine.
+ *
+ * The `dn_hash_impl` parameter shall be a hash function internally used
+ * to match X.500 names (subject/issuer DN, and anchor names). Any standard
+ * hash function may be used, but a collision-resistant hash function is
+ * advised.
+ *
+ * After initialization, some implementations for signature verification
+ * (hash functions and signature algorithms) MUST be added.
+ *
+ * \param ctx context to initialise.
+ * \param dn_hash_impl hash function for DN comparisons.
+ * \param trust_anchors trust anchors.
+ * \param trust_anchors_num number of trust anchors.
+ */
+void br_x509_minimal_init(br_x509_minimal_context *ctx,
+ const br_hash_class *dn_hash_impl,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
+
+/**
+ * \brief Set a supported hash function in an X.509 "minimal" engine.
+ *
+ * Hash functions are used with signature verification algorithms.
+ * Once initialised (with `br_x509_minimal_init()`), the context must
+ * be configured with the hash functions it shall support for that
+ * purpose. The hash function identifier MUST be one of the standard
+ * hash function identifiers (1 to 6, for MD5, SHA-1, SHA-224, SHA-256,
+ * SHA-384 and SHA-512).
+ *
+ * If `impl` is `NULL`, this _removes_ support for the designated
+ * hash function.
+ *
+ * \param ctx validation context.
+ * \param id hash function identifier (from 1 to 6).
+ * \param impl hash function implementation (or `NULL`).
+ */
+static inline void
+br_x509_minimal_set_hash(br_x509_minimal_context *ctx,
+ int id, const br_hash_class *impl)
+{
+ br_multihash_setimpl(&ctx->mhash, id, impl);
+}
+
+/**
+ * \brief Set a RSA signature verification implementation in the X.509
+ * "minimal" engine.
+ *
+ * Once initialised (with `br_x509_minimal_init()`), the context must
+ * be configured with the signature verification implementations that
+ * it is supposed to support. If `irsa` is `0`, then the RSA support
+ * is disabled.
+ *
+ * \param ctx validation context.
+ * \param irsa RSA signature verification implementation (or `0`).
+ */
+static inline void
+br_x509_minimal_set_rsa(br_x509_minimal_context *ctx,
+ br_rsa_pkcs1_vrfy irsa)
+{
+ ctx->irsa = irsa;
+}
+
+/**
+ * \brief Set a ECDSA signature verification implementation in the X.509
+ * "minimal" engine.
+ *
+ * Once initialised (with `br_x509_minimal_init()`), the context must
+ * be configured with the signature verification implementations that
+ * it is supposed to support.
+ *
+ * If `iecdsa` is `0`, then this call disables ECDSA support; in that
+ * case, `iec` may be `NULL`. Otherwise, `iecdsa` MUST point to a function
+ * that verifies ECDSA signatures with format "asn1", and it will use
+ * `iec` as underlying elliptic curve support.
+ *
+ * \param ctx validation context.
+ * \param iec elliptic curve implementation (or `NULL`).
+ * \param iecdsa ECDSA implementation (or `0`).
+ */
+static inline void
+br_x509_minimal_set_ecdsa(br_x509_minimal_context *ctx,
+ const br_ec_impl *iec, br_ecdsa_vrfy iecdsa)
+{
+ ctx->iecdsa = iecdsa;
+ ctx->iec = iec;
+}
+
+/**
+ * \brief Initialise a "minimal" X.509 engine with default algorithms.
+ *
+ * This function performs the same job as `br_x509_minimal_init()`, but
+ * also sets implementations for RSA, ECDSA, and the standard hash
+ * functions.
+ *
+ * \param ctx context to initialise.
+ * \param trust_anchors trust anchors.
+ * \param trust_anchors_num number of trust anchors.
+ */
+void br_x509_minimal_init_full(br_x509_minimal_context *ctx,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
+
+/**
+ * \brief Set the validation time for the X.509 "minimal" engine.
+ *
+ * The validation time is set as two 32-bit integers, for days and
+ * seconds since a fixed epoch:
+ *
+ * - Days are counted in a proleptic Gregorian calendar since
+ * January 1st, 0 AD. Year "0 AD" is the one that preceded "1 AD";
+ * it is also traditionally known as "1 BC".
+ *
+ * - Seconds are counted since midnight, from 0 to 86400 (a count of
+ * 86400 is possible only if a leap second happened).
+ *
+ * The validation date and time is understood in the UTC time zone.
+ *
+ * If the validation date and time are not explicitly set, but BearSSL
+ * was compiled with support for the system clock on the underlying
+ * platform, then the current time will automatically be used. Otherwise,
+ * not setting the validation date and time implies a validation
+ * failure (except in case of direct trust of the EE key).
+ *
+ * \param ctx validation context.
+ * \param days days since January 1st, 0 AD (Gregorian calendar).
+ * \param seconds seconds since midnight (0 to 86400).
+ */
+static inline void
+br_x509_minimal_set_time(br_x509_minimal_context *ctx,
+ uint32_t days, uint32_t seconds)
+{
+ ctx->days = days;
+ ctx->seconds = seconds;
+}
+
+/**
+ * \brief Set the minimal acceptable length for RSA keys (X.509 "minimal"
+ * engine).
+ *
+ * The RSA key length is expressed in bytes. The default minimum key
+ * length is 128 bytes, corresponding to 1017 bits. RSA keys shorter
+ * than the configured length will be rejected, implying validation
+ * failure. This setting applies to keys extracted from certificates
+ * (both end-entity, and intermediate CA) but not to "CA" trust anchors.
+ *
+ * \param ctx validation context.
+ * \param byte_length minimum RSA key length, **in bytes** (not bits).
+ */
+static inline void
+br_x509_minimal_set_minrsa(br_x509_minimal_context *ctx, int byte_length)
+{
+ ctx->min_rsa_size = (int16_t)(byte_length - 128);
+}
+
+/**
+ * \brief Set the name elements to gather.
+ *
+ * The provided array is linked in the context. The elements are
+ * gathered from the EE certificate. If the same element type is
+ * requested several times, then the relevant structures will be filled
+ * in the order the matching values are encountered in the certificate.
+ *
+ * \param ctx validation context.
+ * \param elts array of name element structures to fill.
+ * \param num_elts number of name element structures to fill.
+ */
+static inline void
+br_x509_minimal_set_name_elements(br_x509_minimal_context *ctx,
+ br_name_element *elts, size_t num_elts)
+{
+ ctx->name_elts = elts;
+ ctx->num_name_elts = num_elts;
+}
+
+/**
+ * \brief X.509 decoder context.
+ *
+ * This structure is _not_ for X.509 validation, but for extracting
+ * names and public keys from encoded certificates. Intended usage is
+ * to use (self-signed) certificates as trust anchors.
+ *
+ * Contents are opaque and shall not be accessed directly.
+ */
+typedef struct {
+
+#ifndef BR_DOXYGEN_IGNORE
+ /* Structure for returning the public key. */
+ br_x509_pkey pkey;
+
+ /* CPU for the T0 virtual machine. */
+ struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+ } cpu;
+ uint32_t dp_stack[32];
+ uint32_t rp_stack[32];
+ int err;
+
+ /* The pad serves as destination for various operations. */
+ unsigned char pad[256];
+
+ /* Flag set when decoding succeeds. */
+ unsigned char decoded;
+
+ /* Validity dates. */
+ uint32_t notbefore_days, notbefore_seconds;
+ uint32_t notafter_days, notafter_seconds;
+
+ /* The "CA" flag. This is set to true if the certificate contains
+ a Basic Constraints extension that asserts CA status. */
+ unsigned char isCA;
+
+ /* DN processing: the subject DN is extracted and pushed to the
+ provided callback. */
+ unsigned char copy_dn;
+ void *append_dn_ctx;
+ void (*append_dn)(void *ctx, const void *buf, size_t len);
+
+ /* Certificate data chunk. */
+ const unsigned char *hbuf;
+ size_t hlen;
+
+ /* Buffer for decoded public key. */
+ unsigned char pkey_data[BR_X509_BUFSIZE_KEY];
+
+ /* Type of key and hash function used in the certificate signature. */
+ unsigned char signer_key_type;
+ unsigned char signer_hash_id;
+#endif
+
+} br_x509_decoder_context;
+
+/**
+ * \brief Initialise an X.509 decoder context for processing a new
+ * certificate.
+ *
+ * The `append_dn()` callback (with opaque context `append_dn_ctx`)
+ * will be invoked to receive, chunk by chunk, the certificate's
+ * subject DN. If `append_dn` is `0` then the subject DN will be
+ * ignored.
+ *
+ * \param ctx X.509 decoder context to initialise.
+ * \param append_dn DN receiver callback (or `0`).
+ * \param append_dn_ctx context for the DN receiver callback.
+ */
+void br_x509_decoder_init(br_x509_decoder_context *ctx,
+ void (*append_dn)(void *ctx, const void *buf, size_t len),
+ void *append_dn_ctx);
+
+/**
+ * \brief Push some certificate bytes into a decoder context.
+ *
+ * If `len` is non-zero, then that many bytes are pushed, from address
+ * `data`, into the provided decoder context.
+ *
+ * \param ctx X.509 decoder context.
+ * \param data certificate data chunk.
+ * \param len certificate data chunk length (in bytes).
+ */
+void br_x509_decoder_push(br_x509_decoder_context *ctx,
+ const void *data, size_t len);
+
+/**
+ * \brief Obtain the decoded public key.
+ *
+ * Returned value is a pointer to a structure internal to the decoder
+ * context; releasing or reusing the decoder context invalidates that
+ * structure.
+ *
+ * If decoding was not finished, or failed, then `NULL` is returned.
+ *
+ * \param ctx X.509 decoder context.
+ * \return the public key, or `NULL` on unfinished/error.
+ */
+static inline br_x509_pkey *
+br_x509_decoder_get_pkey(br_x509_decoder_context *ctx)
+{
+ if (ctx->decoded && ctx->err == 0) {
+ return &ctx->pkey;
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * \brief Get decoder error status.
+ *
+ * If no error was reported yet but the certificate decoding is not
+ * finished, then the error code is `BR_ERR_X509_TRUNCATED`. If decoding
+ * was successful, then 0 is returned.
+ *
+ * \param ctx X.509 decoder context.
+ * \return 0 on successful decoding, or a non-zero error code.
+ */
+static inline int
+br_x509_decoder_last_error(br_x509_decoder_context *ctx)
+{
+ if (ctx->err != 0) {
+ return ctx->err;
+ }
+ if (!ctx->decoded) {
+ return BR_ERR_X509_TRUNCATED;
+ }
+ return 0;
+}
+
+/**
+ * \brief Get the "isCA" flag from an X.509 decoder context.
+ *
+ * This flag is set if the decoded certificate claims to be a CA through
+ * a Basic Constraints extension. This flag should not be read before
+ * decoding completed successfully.
+ *
+ * \param ctx X.509 decoder context.
+ * \return the "isCA" flag.
+ */
+static inline int
+br_x509_decoder_isCA(br_x509_decoder_context *ctx)
+{
+ return ctx->isCA;
+}
+
+/**
+ * \brief Get the issuing CA key type (type of algorithm used to sign the
+ * decoded certificate).
+ *
+ * This is `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`. The value 0 is returned
+ * if the signature type was not recognised.
+ *
+ * \param ctx X.509 decoder context.
+ * \return the issuing CA key type.
+ */
+static inline int
+br_x509_decoder_get_signer_key_type(br_x509_decoder_context *ctx)
+{
+ return ctx->signer_key_type;
+}
+
+/**
+ * \brief Get the identifier for the hash function used to sign the decoded
+ * certificate.
+ *
+ * This is 0 if the hash function was not recognised.
+ *
+ * \param ctx X.509 decoder context.
+ * \return the signature hash function identifier.
+ */
+static inline int
+br_x509_decoder_get_signer_hash_id(br_x509_decoder_context *ctx)
+{
+ return ctx->signer_hash_id;
+}
+
+/**
+ * \brief Type for an X.509 certificate (DER-encoded).
+ */
+typedef struct {
+ /** \brief The DER-encoded certificate data. */
+ unsigned char *data;
+ /** \brief The DER-encoded certificate length (in bytes). */
+ size_t data_len;
+} br_x509_certificate;
+
+/**
+ * \brief Private key decoder context.
+ *
+ * The private key decoder recognises RSA and EC private keys, either in
+ * their raw, DER-encoded format, or wrapped in an unencrypted PKCS#8
+ * archive (again DER-encoded).
+ *
+ * Structure contents are opaque and shall not be accessed directly.
+ */
+typedef struct {
+#ifndef BR_DOXYGEN_IGNORE
+ /* Structure for returning the private key. */
+ union {
+ br_rsa_private_key rsa;
+ br_ec_private_key ec;
+ } key;
+
+ /* CPU for the T0 virtual machine. */
+ struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+ } cpu;
+ uint32_t dp_stack[32];
+ uint32_t rp_stack[32];
+ int err;
+
+ /* Private key data chunk. */
+ const unsigned char *hbuf;
+ size_t hlen;
+
+ /* The pad serves as destination for various operations. */
+ unsigned char pad[256];
+
+ /* Decoded key type; 0 until decoding is complete. */
+ unsigned char key_type;
+
+ /* Buffer for the private key elements. It shall be large enough
+ to accommodate all elements for a RSA-4096 private key (roughly
+ five 2048-bit integers, possibly a bit more). */
+ unsigned char key_data[3 * BR_X509_BUFSIZE_SIG];
+#endif
+} br_skey_decoder_context;
+
+/**
+ * \brief Initialise a private key decoder context.
+ *
+ * \param ctx key decoder context to initialise.
+ */
+void br_skey_decoder_init(br_skey_decoder_context *ctx);
+
+/**
+ * \brief Push some data bytes into a private key decoder context.
+ *
+ * If `len` is non-zero, then that many data bytes, starting at address
+ * `data`, are pushed into the decoder.
+ *
+ * \param ctx key decoder context.
+ * \param data private key data chunk.
+ * \param len private key data chunk length (in bytes).
+ */
+void br_skey_decoder_push(br_skey_decoder_context *ctx,
+ const void *data, size_t len);
+
+/**
+ * \brief Get the decoding status for a private key.
+ *
+ * Decoding status is 0 on success, or a non-zero error code. If the
+ * decoding is unfinished when this function is called, then the
+ * status code `BR_ERR_X509_TRUNCATED` is returned.
+ *
+ * \param ctx key decoder context.
+ * \return 0 on successful decoding, or a non-zero error code.
+ */
+static inline int
+br_skey_decoder_last_error(const br_skey_decoder_context *ctx)
+{
+ if (ctx->err != 0) {
+ return ctx->err;
+ }
+ if (ctx->key_type == 0) {
+ return BR_ERR_X509_TRUNCATED;
+ }
+ return 0;
+}
+
+/**
+ * \brief Get the decoded private key type.
+ *
+ * Private key type is `BR_KEYTYPE_RSA` or `BR_KEYTYPE_EC`. If decoding is
+ * not finished or failed, then 0 is returned.
+ *
+ * \param ctx key decoder context.
+ * \return decoded private key type, or 0.
+ */
+static inline int
+br_skey_decoder_key_type(const br_skey_decoder_context *ctx)
+{
+ if (ctx->err == 0) {
+ return ctx->key_type;
+ } else {
+ return 0;
+ }
+}
+
+/**
+ * \brief Get the decoded RSA private key.
+ *
+ * This function returns `NULL` if the decoding failed, or is not
+ * finished, or the key is not RSA. The returned pointer references
+ * structures within the context that can become invalid if the context
+ * is reused or released.
+ *
+ * \param ctx key decoder context.
+ * \return decoded RSA private key, or `NULL`.
+ */
+static inline const br_rsa_private_key *
+br_skey_decoder_get_rsa(const br_skey_decoder_context *ctx)
+{
+ if (ctx->err == 0 && ctx->key_type == BR_KEYTYPE_RSA) {
+ return &ctx->key.rsa;
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * \brief Get the decoded EC private key.
+ *
+ * This function returns `NULL` if the decoding failed, or is not
+ * finished, or the key is not EC. The returned pointer references
+ * structures within the context that can become invalid if the context
+ * is reused or released.
+ *
+ * \param ctx key decoder context.
+ * \return decoded EC private key, or `NULL`.
+ */
+static inline const br_ec_private_key *
+br_skey_decoder_get_ec(const br_skey_decoder_context *ctx)
+{
+ if (ctx->err == 0 && ctx->key_type == BR_KEYTYPE_EC) {
+ return &ctx->key.ec;
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * \brief Encode an RSA private key (raw DER format).
+ *
+ * This function encodes the provided key into the "raw" format specified
+ * in PKCS#1 (RFC 8017, Appendix C, type `RSAPrivateKey`), with DER
+ * encoding rules.
+ *
+ * The key elements are:
+ *
+ * - `sk`: the private key (`p`, `q`, `dp`, `dq` and `iq`)
+ *
+ * - `pk`: the public key (`n` and `e`)
+ *
+ * - `d` (size: `dlen` bytes): the private exponent
+ *
+ * The public key elements, and the private exponent `d`, can be
+ * recomputed from the private key (see `br_rsa_compute_modulus()`,
+ * `br_rsa_compute_pubexp()` and `br_rsa_compute_privexp()`).
+ *
+ * If `dest` is not `NULL`, then the encoded key is written at that
+ * address, and the encoded length (in bytes) is returned. If `dest` is
+ * `NULL`, then nothing is written, but the encoded length is still
+ * computed and returned.
+ *
+ * \param dest the destination buffer (or `NULL`).
+ * \param sk the RSA private key.
+ * \param pk the RSA public key.
+ * \param d the RSA private exponent.
+ * \param dlen the RSA private exponent length (in bytes).
+ * \return the encoded key length (in bytes).
+ */
+size_t br_encode_rsa_raw_der(void *dest, const br_rsa_private_key *sk,
+ const br_rsa_public_key *pk, const void *d, size_t dlen);
+
+/**
+ * \brief Encode an RSA private key (PKCS#8 DER format).
+ *
+ * This function encodes the provided key into the PKCS#8 format
+ * (RFC 5958, type `OneAsymmetricKey`). It wraps around the "raw DER"
+ * format for the RSA key, as implemented by `br_encode_rsa_raw_der()`.
+ *
+ * The key elements are:
+ *
+ * - `sk`: the private key (`p`, `q`, `dp`, `dq` and `iq`)
+ *
+ * - `pk`: the public key (`n` and `e`)
+ *
+ * - `d` (size: `dlen` bytes): the private exponent
+ *
+ * The public key elements, and the private exponent `d`, can be
+ * recomputed from the private key (see `br_rsa_compute_modulus()`,
+ * `br_rsa_compute_pubexp()` and `br_rsa_compute_privexp()`).
+ *
+ * If `dest` is not `NULL`, then the encoded key is written at that
+ * address, and the encoded length (in bytes) is returned. If `dest` is
+ * `NULL`, then nothing is written, but the encoded length is still
+ * computed and returned.
+ *
+ * \param dest the destination buffer (or `NULL`).
+ * \param sk the RSA private key.
+ * \param pk the RSA public key.
+ * \param d the RSA private exponent.
+ * \param dlen the RSA private exponent length (in bytes).
+ * \return the encoded key length (in bytes).
+ */
+size_t br_encode_rsa_pkcs8_der(void *dest, const br_rsa_private_key *sk,
+ const br_rsa_public_key *pk, const void *d, size_t dlen);
+
+/**
+ * \brief Encode an EC private key (raw DER format).
+ *
+ * This function encodes the provided key into the "raw" format specified
+ * in RFC 5915 (type `ECPrivateKey`), with DER encoding rules.
+ *
+ * The private key is provided in `sk`, the public key being `pk`. If
+ * `pk` is `NULL`, then the encoded key will not include the public key
+ * in its `publicKey` field (which is nominally optional).
+ *
+ * If `dest` is not `NULL`, then the encoded key is written at that
+ * address, and the encoded length (in bytes) is returned. If `dest` is
+ * `NULL`, then nothing is written, but the encoded length is still
+ * computed and returned.
+ *
+ * If the key cannot be encoded (e.g. because there is no known OBJECT
+ * IDENTIFIER for the used curve), then 0 is returned.
+ *
+ * \param dest the destination buffer (or `NULL`).
+ * \param sk the EC private key.
+ * \param pk the EC public key (or `NULL`).
+ * \return the encoded key length (in bytes), or 0.
+ */
+size_t br_encode_ec_raw_der(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk);
+
+/**
+ * \brief Encode an EC private key (PKCS#8 DER format).
+ *
+ * This function encodes the provided key into the PKCS#8 format
+ * (RFC 5958, type `OneAsymmetricKey`). The curve is identified
+ * by an OID provided as parameters to the `privateKeyAlgorithm`
+ * field. The private key value (contents of the `privateKey` field)
+ * contains the DER encoding of the `ECPrivateKey` type defined in
+ * RFC 5915, without the `parameters` field (since they would be
+ * redundant with the information in `privateKeyAlgorithm`).
+ *
+ * The private key is provided in `sk`, the public key being `pk`. If
+ * `pk` is not `NULL`, then the encoded public key is included in the
+ * `publicKey` field of the private key value (but not in the `publicKey`
+ * field of the PKCS#8 `OneAsymmetricKey` wrapper).
+ *
+ * If `dest` is not `NULL`, then the encoded key is written at that
+ * address, and the encoded length (in bytes) is returned. If `dest` is
+ * `NULL`, then nothing is written, but the encoded length is still
+ * computed and returned.
+ *
+ * If the key cannot be encoded (e.g. because there is no known OBJECT
+ * IDENTIFIER for the used curve), then 0 is returned.
+ *
+ * \param dest the destination buffer (or `NULL`).
+ * \param sk the EC private key.
+ * \param pk the EC public key (or `NULL`).
+ * \return the encoded key length (in bytes), or 0.
+ */
+size_t br_encode_ec_pkcs8_der(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk);
+
+/**
+ * \brief PEM banner for RSA private key (raw).
+ */
+#define BR_ENCODE_PEM_RSA_RAW "RSA PRIVATE KEY"
+
+/**
+ * \brief PEM banner for EC private key (raw).
+ */
+#define BR_ENCODE_PEM_EC_RAW "EC PRIVATE KEY"
+
+/**
+ * \brief PEM banner for an RSA or EC private key in PKCS#8 format.
+ */
+#define BR_ENCODE_PEM_PKCS8 "PRIVATE KEY"
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/BearSSL/mk/Defaults.mk b/test/monniaux/BearSSL/mk/Defaults.mk
new file mode 100644
index 00000000..4c66025d
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/Defaults.mk
@@ -0,0 +1,41 @@
+# Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# ======================================================================
+
+# This file sets variables with generic default values, which can be
+# overridden in the selected configuration file.
+
+BUILD = build
+OBJDIR = $(BUILD)$Pobj
+BEARSSLLIB = $(BUILD)$P$(LP)bearssl$L
+BEARSSLDLL = $(BUILD)$P$(DP)bearssl$D
+BRSSL = $(BUILD)$Pbrssl$E
+TESTCRYPTO = $(BUILD)$Ptestcrypto$E
+TESTSPEED = $(BUILD)$Ptestspeed$E
+TESTX509 = $(BUILD)$Ptestx509$E
+INCFLAGS = -Isrc -Iinc
+T0COMP = T0Comp.exe
+STATICLIB = lib
+DLL = dll
+TOOLS = tools
+TESTS = tests
diff --git a/test/monniaux/BearSSL/mk/NMake.mk b/test/monniaux/BearSSL/mk/NMake.mk
new file mode 100644
index 00000000..7a53704d
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/NMake.mk
@@ -0,0 +1,38 @@
+# Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# ======================================================================
+
+# This file sets variables for use with NMake.exe, as distributed with
+# Visual Studio.
+
+# Load generic defaults.
+!include mk/Defaults.mk
+
+# Default configuration is 'Win' (native build with Visual Studio).
+CONF = Win
+
+# Path separator.
+P = ^\
+
+!include conf/$(CONF).mk
+!include mk/Rules.mk
diff --git a/test/monniaux/BearSSL/mk/Rules.mk b/test/monniaux/BearSSL/mk/Rules.mk
new file mode 100644
index 00000000..0da4558b
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/Rules.mk
@@ -0,0 +1,1322 @@
+# DMonniaux FIXME
+# files compiled with gcc don't go through ccomp
+
+# Automatically generated rules. Use 'mkrules.sh' to modify/regenerate.
+
+OBJ = \
+ $(OBJDIR)$Psettings$O \
+ $(OBJDIR)$Pccm$O \
+ $(OBJDIR)$Peax$O \
+ $(OBJDIR)$Pgcm$O \
+ $(OBJDIR)$Pccopy$O \
+ $(OBJDIR)$Pdec16be$O \
+ $(OBJDIR)$Pdec16le$O \
+ $(OBJDIR)$Pdec32be$O \
+ $(OBJDIR)$Pdec32le$O \
+ $(OBJDIR)$Pdec64be$O \
+ $(OBJDIR)$Pdec64le$O \
+ $(OBJDIR)$Penc16be$O \
+ $(OBJDIR)$Penc16le$O \
+ $(OBJDIR)$Penc32be$O \
+ $(OBJDIR)$Penc32le$O \
+ $(OBJDIR)$Penc64be$O \
+ $(OBJDIR)$Penc64le$O \
+ $(OBJDIR)$Ppemdec$O \
+ $(OBJDIR)$Ppemenc$O \
+ $(OBJDIR)$Pec_all_m15$O \
+ $(OBJDIR)$Pec_all_m31$O \
+ $(OBJDIR)$Pec_c25519_i15$O \
+ $(OBJDIR)$Pec_c25519_i31$O \
+ $(OBJDIR)$Pec_c25519_m15$O \
+ $(OBJDIR)$Pec_c25519_m31$O \
+ $(OBJDIR)$Pec_c25519_m62$O \
+ $(OBJDIR)$Pec_c25519_m64$O \
+ $(OBJDIR)$Pec_curve25519$O \
+ $(OBJDIR)$Pec_default$O \
+ $(OBJDIR)$Pec_keygen$O \
+ $(OBJDIR)$Pec_p256_m15$O \
+ $(OBJDIR)$Pec_p256_m31$O \
+ $(OBJDIR)$Pec_p256_m62$O \
+ $(OBJDIR)$Pec_p256_m64$O \
+ $(OBJDIR)$Pec_prime_i15$O \
+ $(OBJDIR)$Pec_prime_i31$O \
+ $(OBJDIR)$Pec_pubkey$O \
+ $(OBJDIR)$Pec_secp256r1$O \
+ $(OBJDIR)$Pec_secp384r1$O \
+ $(OBJDIR)$Pec_secp521r1$O \
+ $(OBJDIR)$Pecdsa_atr$O \
+ $(OBJDIR)$Pecdsa_default_sign_asn1$O \
+ $(OBJDIR)$Pecdsa_default_sign_raw$O \
+ $(OBJDIR)$Pecdsa_default_vrfy_asn1$O \
+ $(OBJDIR)$Pecdsa_default_vrfy_raw$O \
+ $(OBJDIR)$Pecdsa_i15_bits$O \
+ $(OBJDIR)$Pecdsa_i15_sign_asn1$O \
+ $(OBJDIR)$Pecdsa_i15_sign_raw$O \
+ $(OBJDIR)$Pecdsa_i15_vrfy_asn1$O \
+ $(OBJDIR)$Pecdsa_i15_vrfy_raw$O \
+ $(OBJDIR)$Pecdsa_i31_bits$O \
+ $(OBJDIR)$Pecdsa_i31_sign_asn1$O \
+ $(OBJDIR)$Pecdsa_i31_sign_raw$O \
+ $(OBJDIR)$Pecdsa_i31_vrfy_asn1$O \
+ $(OBJDIR)$Pecdsa_i31_vrfy_raw$O \
+ $(OBJDIR)$Pecdsa_rta$O \
+ $(OBJDIR)$Pdig_oid$O \
+ $(OBJDIR)$Pdig_size$O \
+ $(OBJDIR)$Pghash_ctmul$O \
+ $(OBJDIR)$Pghash_ctmul32$O \
+ $(OBJDIR)$Pghash_ctmul64$O \
+ $(OBJDIR)$Pghash_pclmul$O \
+ $(OBJDIR)$Pghash_pwr8$O \
+ $(OBJDIR)$Pmd5$O \
+ $(OBJDIR)$Pmd5sha1$O \
+ $(OBJDIR)$Pmgf1$O \
+ $(OBJDIR)$Pmultihash$O \
+ $(OBJDIR)$Psha1$O \
+ $(OBJDIR)$Psha2big$O \
+ $(OBJDIR)$Psha2small$O \
+ $(OBJDIR)$Pi15_add$O \
+ $(OBJDIR)$Pi15_bitlen$O \
+ $(OBJDIR)$Pi15_decmod$O \
+ $(OBJDIR)$Pi15_decode$O \
+ $(OBJDIR)$Pi15_decred$O \
+ $(OBJDIR)$Pi15_encode$O \
+ $(OBJDIR)$Pi15_fmont$O \
+ $(OBJDIR)$Pi15_iszero$O \
+ $(OBJDIR)$Pi15_moddiv$O \
+ $(OBJDIR)$Pi15_modpow$O \
+ $(OBJDIR)$Pi15_modpow2$O \
+ $(OBJDIR)$Pi15_montmul$O \
+ $(OBJDIR)$Pi15_mulacc$O \
+ $(OBJDIR)$Pi15_muladd$O \
+ $(OBJDIR)$Pi15_ninv15$O \
+ $(OBJDIR)$Pi15_reduce$O \
+ $(OBJDIR)$Pi15_rshift$O \
+ $(OBJDIR)$Pi15_sub$O \
+ $(OBJDIR)$Pi15_tmont$O \
+ $(OBJDIR)$Pi31_add$O \
+ $(OBJDIR)$Pi31_bitlen$O \
+ $(OBJDIR)$Pi31_decmod$O \
+ $(OBJDIR)$Pi31_decode$O \
+ $(OBJDIR)$Pi31_decred$O \
+ $(OBJDIR)$Pi31_encode$O \
+ $(OBJDIR)$Pi31_fmont$O \
+ $(OBJDIR)$Pi31_iszero$O \
+ $(OBJDIR)$Pi31_moddiv$O \
+ $(OBJDIR)$Pi31_modpow$O \
+ $(OBJDIR)$Pi31_modpow2$O \
+ $(OBJDIR)$Pi31_montmul$O \
+ $(OBJDIR)$Pi31_mulacc$O \
+ $(OBJDIR)$Pi31_muladd$O \
+ $(OBJDIR)$Pi31_ninv31$O \
+ $(OBJDIR)$Pi31_reduce$O \
+ $(OBJDIR)$Pi31_rshift$O \
+ $(OBJDIR)$Pi31_sub$O \
+ $(OBJDIR)$Pi31_tmont$O \
+ $(OBJDIR)$Pi32_add$O \
+ $(OBJDIR)$Pi32_bitlen$O \
+ $(OBJDIR)$Pi32_decmod$O \
+ $(OBJDIR)$Pi32_decode$O \
+ $(OBJDIR)$Pi32_decred$O \
+ $(OBJDIR)$Pi32_div32$O \
+ $(OBJDIR)$Pi32_encode$O \
+ $(OBJDIR)$Pi32_fmont$O \
+ $(OBJDIR)$Pi32_iszero$O \
+ $(OBJDIR)$Pi32_modpow$O \
+ $(OBJDIR)$Pi32_montmul$O \
+ $(OBJDIR)$Pi32_mulacc$O \
+ $(OBJDIR)$Pi32_muladd$O \
+ $(OBJDIR)$Pi32_ninv32$O \
+ $(OBJDIR)$Pi32_reduce$O \
+ $(OBJDIR)$Pi32_sub$O \
+ $(OBJDIR)$Pi32_tmont$O \
+ $(OBJDIR)$Pi62_modpow2$O \
+ $(OBJDIR)$Phkdf$O \
+ $(OBJDIR)$Pshake$O \
+ $(OBJDIR)$Phmac$O \
+ $(OBJDIR)$Phmac_ct$O \
+ $(OBJDIR)$Paesctr_drbg$O \
+ $(OBJDIR)$Phmac_drbg$O \
+ $(OBJDIR)$Psysrng$O \
+ $(OBJDIR)$Prsa_default_keygen$O \
+ $(OBJDIR)$Prsa_default_modulus$O \
+ $(OBJDIR)$Prsa_default_oaep_decrypt$O \
+ $(OBJDIR)$Prsa_default_oaep_encrypt$O \
+ $(OBJDIR)$Prsa_default_pkcs1_sign$O \
+ $(OBJDIR)$Prsa_default_pkcs1_vrfy$O \
+ $(OBJDIR)$Prsa_default_priv$O \
+ $(OBJDIR)$Prsa_default_privexp$O \
+ $(OBJDIR)$Prsa_default_pss_sign$O \
+ $(OBJDIR)$Prsa_default_pss_vrfy$O \
+ $(OBJDIR)$Prsa_default_pub$O \
+ $(OBJDIR)$Prsa_default_pubexp$O \
+ $(OBJDIR)$Prsa_i15_keygen$O \
+ $(OBJDIR)$Prsa_i15_modulus$O \
+ $(OBJDIR)$Prsa_i15_oaep_decrypt$O \
+ $(OBJDIR)$Prsa_i15_oaep_encrypt$O \
+ $(OBJDIR)$Prsa_i15_pkcs1_sign$O \
+ $(OBJDIR)$Prsa_i15_pkcs1_vrfy$O \
+ $(OBJDIR)$Prsa_i15_priv$O \
+ $(OBJDIR)$Prsa_i15_privexp$O \
+ $(OBJDIR)$Prsa_i15_pss_sign$O \
+ $(OBJDIR)$Prsa_i15_pss_vrfy$O \
+ $(OBJDIR)$Prsa_i15_pub$O \
+ $(OBJDIR)$Prsa_i15_pubexp$O \
+ $(OBJDIR)$Prsa_i31_keygen$O \
+ $(OBJDIR)$Prsa_i31_keygen_inner$O \
+ $(OBJDIR)$Prsa_i31_modulus$O \
+ $(OBJDIR)$Prsa_i31_oaep_decrypt$O \
+ $(OBJDIR)$Prsa_i31_oaep_encrypt$O \
+ $(OBJDIR)$Prsa_i31_pkcs1_sign$O \
+ $(OBJDIR)$Prsa_i31_pkcs1_vrfy$O \
+ $(OBJDIR)$Prsa_i31_priv$O \
+ $(OBJDIR)$Prsa_i31_privexp$O \
+ $(OBJDIR)$Prsa_i31_pss_sign$O \
+ $(OBJDIR)$Prsa_i31_pss_vrfy$O \
+ $(OBJDIR)$Prsa_i31_pub$O \
+ $(OBJDIR)$Prsa_i31_pubexp$O \
+ $(OBJDIR)$Prsa_i32_oaep_decrypt$O \
+ $(OBJDIR)$Prsa_i32_oaep_encrypt$O \
+ $(OBJDIR)$Prsa_i32_pkcs1_sign$O \
+ $(OBJDIR)$Prsa_i32_pkcs1_vrfy$O \
+ $(OBJDIR)$Prsa_i32_priv$O \
+ $(OBJDIR)$Prsa_i32_pss_sign$O \
+ $(OBJDIR)$Prsa_i32_pss_vrfy$O \
+ $(OBJDIR)$Prsa_i32_pub$O \
+ $(OBJDIR)$Prsa_i62_keygen$O \
+ $(OBJDIR)$Prsa_i62_oaep_decrypt$O \
+ $(OBJDIR)$Prsa_i62_oaep_encrypt$O \
+ $(OBJDIR)$Prsa_i62_pkcs1_sign$O \
+ $(OBJDIR)$Prsa_i62_pkcs1_vrfy$O \
+ $(OBJDIR)$Prsa_i62_priv$O \
+ $(OBJDIR)$Prsa_i62_pss_sign$O \
+ $(OBJDIR)$Prsa_i62_pss_vrfy$O \
+ $(OBJDIR)$Prsa_i62_pub$O \
+ $(OBJDIR)$Prsa_oaep_pad$O \
+ $(OBJDIR)$Prsa_oaep_unpad$O \
+ $(OBJDIR)$Prsa_pkcs1_sig_pad$O \
+ $(OBJDIR)$Prsa_pkcs1_sig_unpad$O \
+ $(OBJDIR)$Prsa_pss_sig_pad$O \
+ $(OBJDIR)$Prsa_pss_sig_unpad$O \
+ $(OBJDIR)$Prsa_ssl_decrypt$O \
+ $(OBJDIR)$Pprf$O \
+ $(OBJDIR)$Pprf_md5sha1$O \
+ $(OBJDIR)$Pprf_sha256$O \
+ $(OBJDIR)$Pprf_sha384$O \
+ $(OBJDIR)$Pssl_ccert_single_ec$O \
+ $(OBJDIR)$Pssl_ccert_single_rsa$O \
+ $(OBJDIR)$Pssl_client$O \
+ $(OBJDIR)$Pssl_client_default_rsapub$O \
+ $(OBJDIR)$Pssl_client_full$O \
+ $(OBJDIR)$Pssl_engine$O \
+ $(OBJDIR)$Pssl_engine_default_aescbc$O \
+ $(OBJDIR)$Pssl_engine_default_aesccm$O \
+ $(OBJDIR)$Pssl_engine_default_aesgcm$O \
+ $(OBJDIR)$Pssl_engine_default_chapol$O \
+ $(OBJDIR)$Pssl_engine_default_descbc$O \
+ $(OBJDIR)$Pssl_engine_default_ec$O \
+ $(OBJDIR)$Pssl_engine_default_ecdsa$O \
+ $(OBJDIR)$Pssl_engine_default_rsavrfy$O \
+ $(OBJDIR)$Pssl_hashes$O \
+ $(OBJDIR)$Pssl_hs_client$O \
+ $(OBJDIR)$Pssl_hs_server$O \
+ $(OBJDIR)$Pssl_io$O \
+ $(OBJDIR)$Pssl_keyexport$O \
+ $(OBJDIR)$Pssl_lru$O \
+ $(OBJDIR)$Pssl_rec_cbc$O \
+ $(OBJDIR)$Pssl_rec_ccm$O \
+ $(OBJDIR)$Pssl_rec_chapol$O \
+ $(OBJDIR)$Pssl_rec_gcm$O \
+ $(OBJDIR)$Pssl_scert_single_ec$O \
+ $(OBJDIR)$Pssl_scert_single_rsa$O \
+ $(OBJDIR)$Pssl_server$O \
+ $(OBJDIR)$Pssl_server_full_ec$O \
+ $(OBJDIR)$Pssl_server_full_rsa$O \
+ $(OBJDIR)$Pssl_server_mine2c$O \
+ $(OBJDIR)$Pssl_server_mine2g$O \
+ $(OBJDIR)$Pssl_server_minf2c$O \
+ $(OBJDIR)$Pssl_server_minf2g$O \
+ $(OBJDIR)$Pssl_server_minr2g$O \
+ $(OBJDIR)$Pssl_server_minu2g$O \
+ $(OBJDIR)$Pssl_server_minv2g$O \
+ $(OBJDIR)$Paes_big_cbcdec$O \
+ $(OBJDIR)$Paes_big_cbcenc$O \
+ $(OBJDIR)$Paes_big_ctr$O \
+ $(OBJDIR)$Paes_big_ctrcbc$O \
+ $(OBJDIR)$Paes_big_dec$O \
+ $(OBJDIR)$Paes_big_enc$O \
+ $(OBJDIR)$Paes_common$O \
+ $(OBJDIR)$Paes_ct$O \
+ $(OBJDIR)$Paes_ct64$O \
+ $(OBJDIR)$Paes_ct64_cbcdec$O \
+ $(OBJDIR)$Paes_ct64_cbcenc$O \
+ $(OBJDIR)$Paes_ct64_ctr$O \
+ $(OBJDIR)$Paes_ct64_ctrcbc$O \
+ $(OBJDIR)$Paes_ct64_dec$O \
+ $(OBJDIR)$Paes_ct64_enc$O \
+ $(OBJDIR)$Paes_ct_cbcdec$O \
+ $(OBJDIR)$Paes_ct_cbcenc$O \
+ $(OBJDIR)$Paes_ct_ctr$O \
+ $(OBJDIR)$Paes_ct_ctrcbc$O \
+ $(OBJDIR)$Paes_ct_dec$O \
+ $(OBJDIR)$Paes_ct_enc$O \
+ $(OBJDIR)$Paes_pwr8$O \
+ $(OBJDIR)$Paes_pwr8_cbcdec$O \
+ $(OBJDIR)$Paes_pwr8_cbcenc$O \
+ $(OBJDIR)$Paes_pwr8_ctr$O \
+ $(OBJDIR)$Paes_pwr8_ctrcbc$O \
+ $(OBJDIR)$Paes_small_cbcdec$O \
+ $(OBJDIR)$Paes_small_cbcenc$O \
+ $(OBJDIR)$Paes_small_ctr$O \
+ $(OBJDIR)$Paes_small_ctrcbc$O \
+ $(OBJDIR)$Paes_small_dec$O \
+ $(OBJDIR)$Paes_small_enc$O \
+ $(OBJDIR)$Paes_x86ni$O \
+ $(OBJDIR)$Paes_x86ni_cbcdec$O \
+ $(OBJDIR)$Paes_x86ni_cbcenc$O \
+ $(OBJDIR)$Paes_x86ni_ctr$O \
+ $(OBJDIR)$Paes_x86ni_ctrcbc$O \
+ $(OBJDIR)$Pchacha20_ct$O \
+ $(OBJDIR)$Pchacha20_sse2$O \
+ $(OBJDIR)$Pdes_ct$O \
+ $(OBJDIR)$Pdes_ct_cbcdec$O \
+ $(OBJDIR)$Pdes_ct_cbcenc$O \
+ $(OBJDIR)$Pdes_support$O \
+ $(OBJDIR)$Pdes_tab$O \
+ $(OBJDIR)$Pdes_tab_cbcdec$O \
+ $(OBJDIR)$Pdes_tab_cbcenc$O \
+ $(OBJDIR)$Ppoly1305_ctmul$O \
+ $(OBJDIR)$Ppoly1305_ctmul32$O \
+ $(OBJDIR)$Ppoly1305_ctmulq$O \
+ $(OBJDIR)$Ppoly1305_i15$O \
+ $(OBJDIR)$Pasn1enc$O \
+ $(OBJDIR)$Pencode_ec_pk8der$O \
+ $(OBJDIR)$Pencode_ec_rawder$O \
+ $(OBJDIR)$Pencode_rsa_pk8der$O \
+ $(OBJDIR)$Pencode_rsa_rawder$O \
+ $(OBJDIR)$Pskey_decoder$O \
+ $(OBJDIR)$Px509_decoder$O \
+ $(OBJDIR)$Px509_knownkey$O \
+ $(OBJDIR)$Px509_minimal$O \
+ $(OBJDIR)$Px509_minimal_full$O
+OBJBRSSL = \
+ $(OBJDIR)$Pbrssl$O \
+ $(OBJDIR)$Pcerts$O \
+ $(OBJDIR)$Pchain$O \
+ $(OBJDIR)$Pclient$O \
+ $(OBJDIR)$Perrors$O \
+ $(OBJDIR)$Pfiles$O \
+ $(OBJDIR)$Pimpl$O \
+ $(OBJDIR)$Pkeys$O \
+ $(OBJDIR)$Pnames$O \
+ $(OBJDIR)$Pserver$O \
+ $(OBJDIR)$Pskey$O \
+ $(OBJDIR)$Psslio$O \
+ $(OBJDIR)$Pta$O \
+ $(OBJDIR)$Ptwrch$O \
+ $(OBJDIR)$Pvector$O \
+ $(OBJDIR)$Pverify$O \
+ $(OBJDIR)$Pxmem$O
+OBJTESTCRYPTO = \
+ $(OBJDIR)$Ptest_crypto$O
+OBJTESTSPEED = \
+ $(OBJDIR)$Ptest_speed$O
+OBJTESTX509 = \
+ $(OBJDIR)$Ptest_x509$O
+HEADERSPUB = inc$Pbearssl.h inc$Pbearssl_aead.h inc$Pbearssl_block.h inc$Pbearssl_ec.h inc$Pbearssl_hash.h inc$Pbearssl_hmac.h inc$Pbearssl_kdf.h inc$Pbearssl_pem.h inc$Pbearssl_prf.h inc$Pbearssl_rand.h inc$Pbearssl_rsa.h inc$Pbearssl_ssl.h inc$Pbearssl_x509.h
+HEADERSPRIV = $(HEADERSPUB) src$Pconfig.h src$Pinner.h
+HEADERSTOOLS = $(HEADERSPUB) tools$Pbrssl.h
+T0SRC = T0$PBlobWriter.cs T0$PCPU.cs T0$PCodeElement.cs T0$PCodeElementJump.cs T0$PCodeElementUInt.cs T0$PCodeElementUIntExpr.cs T0$PCodeElementUIntInt.cs T0$PCodeElementUIntUInt.cs T0$PConstData.cs T0$POpcode.cs T0$POpcodeCall.cs T0$POpcodeConst.cs T0$POpcodeGetLocal.cs T0$POpcodeJump.cs T0$POpcodeJumpIf.cs T0$POpcodeJumpIfNot.cs T0$POpcodeJumpUncond.cs T0$POpcodePutLocal.cs T0$POpcodeRet.cs T0$PSType.cs T0$PT0Comp.cs T0$PTPointerBase.cs T0$PTPointerBlob.cs T0$PTPointerExpr.cs T0$PTPointerNull.cs T0$PTPointerXT.cs T0$PTValue.cs T0$PWord.cs T0$PWordBuilder.cs T0$PWordData.cs T0$PWordInterpreted.cs T0$PWordNative.cs
+T0KERN =
+
+all: $(STATICLIB) $(DLL) $(TOOLS) $(TESTS)
+
+no:
+
+lib: $(BEARSSLLIB)
+
+dll: $(BEARSSLDLL)
+
+tools: $(BRSSL)
+
+tests: $(TESTCRYPTO) $(TESTSPEED) $(TESTX509)
+
+T0: kT0
+
+kT0: $(T0COMP) src$Pssl$Pssl_hs_common.t0 src$Pssl$Pssl_hs_client.t0 src$Pssl$Pssl_hs_server.t0 src$Px509$Pasn1.t0 src$Px509$Pskey_decoder.t0 src$Px509$Px509_decoder.t0 src$Px509$Px509_minimal.t0
+ $(RUNT0COMP) -o src$Pcodec$Ppemdec -r br_pem_decoder src$Pcodec$Ppemdec.t0
+ $(RUNT0COMP) -o src$Pssl$Pssl_hs_client -r br_ssl_hs_client src$Pssl$Pssl_hs_common.t0 src$Pssl$Pssl_hs_client.t0
+ $(RUNT0COMP) -o src$Pssl$Pssl_hs_server -r br_ssl_hs_server src$Pssl$Pssl_hs_common.t0 src$Pssl$Pssl_hs_server.t0
+ $(RUNT0COMP) -o src$Px509$Pskey_decoder -r br_skey_decoder src$Px509$Pasn1.t0 src$Px509$Pskey_decoder.t0
+ $(RUNT0COMP) -o src$Px509$Px509_decoder -r br_x509_decoder src$Px509$Pasn1.t0 src$Px509$Px509_decoder.t0
+ $(RUNT0COMP) -o src$Px509$Px509_minimal -r br_x509_minimal src$Px509$Pasn1.t0 src$Px509$Px509_minimal.t0
+
+$(T0COMP): $(T0SRC) $(T0KERN)
+ $(MKT0COMP)
+
+clean:
+ -$(RM) $(OBJDIR)$P*$O
+ -$(RM) $(BEARSSLLIB) $(BEARSSLDLL) $(BRSSL) $(TESTCRYPTO) $(TESTSPEED) $(TESTX509)
+
+$(OBJDIR):
+ -$(MKDIR) $(OBJDIR)
+
+$(BEARSSLLIB): $(OBJDIR) $(OBJ)
+ $(AR) $(ARFLAGS) $(AROUT)$(BEARSSLLIB) $(OBJ)
+
+$(BEARSSLDLL): $(OBJDIR) $(OBJ)
+ $(LDDLL) $(LDDLLFLAGS) $(LDDLLOUT)$(BEARSSLDLL) $(OBJ)
+
+$(BRSSL): $(BEARSSLLIB) $(OBJBRSSL)
+ $(LD) $(LDFLAGS) $(LDOUT)$(BRSSL) $(OBJBRSSL) $(BEARSSLLIB)
+
+$(TESTCRYPTO): $(BEARSSLLIB) $(OBJTESTCRYPTO)
+ $(LD) $(LDFLAGS) $(LDOUT)$(TESTCRYPTO) $(OBJTESTCRYPTO) $(BEARSSLLIB)
+
+$(TESTSPEED): $(BEARSSLLIB) $(OBJTESTSPEED)
+ $(LD) $(LDFLAGS) $(LDOUT)$(TESTSPEED) $(OBJTESTSPEED) $(BEARSSLLIB)
+
+$(TESTX509): $(BEARSSLLIB) $(OBJTESTX509)
+ $(LD) $(LDFLAGS) $(LDOUT)$(TESTX509) $(OBJTESTX509) $(BEARSSLLIB)
+
+$(OBJDIR)$Psettings$O: src$Psettings.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psettings$O src$Psettings.c
+
+$(OBJDIR)$Pccm$O: src$Paead$Pccm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pccm$O src$Paead$Pccm.c
+
+$(OBJDIR)$Peax$O: src$Paead$Peax.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Peax$O src$Paead$Peax.c
+
+$(OBJDIR)$Pgcm$O: src$Paead$Pgcm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pgcm$O src$Paead$Pgcm.c
+
+$(OBJDIR)$Pccopy$O: src$Pcodec$Pccopy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pccopy$O src$Pcodec$Pccopy.c
+
+$(OBJDIR)$Pdec16be$O: src$Pcodec$Pdec16be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec16be$O src$Pcodec$Pdec16be.c
+
+$(OBJDIR)$Pdec16le$O: src$Pcodec$Pdec16le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec16le$O src$Pcodec$Pdec16le.c
+
+$(OBJDIR)$Pdec32be$O: src$Pcodec$Pdec32be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec32be$O src$Pcodec$Pdec32be.c
+
+$(OBJDIR)$Pdec32le$O: src$Pcodec$Pdec32le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec32le$O src$Pcodec$Pdec32le.c
+
+$(OBJDIR)$Pdec64be$O: src$Pcodec$Pdec64be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec64be$O src$Pcodec$Pdec64be.c
+
+$(OBJDIR)$Pdec64le$O: src$Pcodec$Pdec64le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdec64le$O src$Pcodec$Pdec64le.c
+
+$(OBJDIR)$Penc16be$O: src$Pcodec$Penc16be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc16be$O src$Pcodec$Penc16be.c
+
+$(OBJDIR)$Penc16le$O: src$Pcodec$Penc16le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc16le$O src$Pcodec$Penc16le.c
+
+$(OBJDIR)$Penc32be$O: src$Pcodec$Penc32be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc32be$O src$Pcodec$Penc32be.c
+
+$(OBJDIR)$Penc32le$O: src$Pcodec$Penc32le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc32le$O src$Pcodec$Penc32le.c
+
+$(OBJDIR)$Penc64be$O: src$Pcodec$Penc64be.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc64be$O src$Pcodec$Penc64be.c
+
+$(OBJDIR)$Penc64le$O: src$Pcodec$Penc64le.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Penc64le$O src$Pcodec$Penc64le.c
+
+$(OBJDIR)$Ppemdec$O: src$Pcodec$Ppemdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppemdec$O src$Pcodec$Ppemdec.c
+
+$(OBJDIR)$Ppemenc$O: src$Pcodec$Ppemenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppemenc$O src$Pcodec$Ppemenc.c
+
+$(OBJDIR)$Pec_all_m15$O: src$Pec$Pec_all_m15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_all_m15$O src$Pec$Pec_all_m15.c
+
+$(OBJDIR)$Pec_all_m31$O: src$Pec$Pec_all_m31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_all_m31$O src$Pec$Pec_all_m31.c
+
+$(OBJDIR)$Pec_c25519_i15$O: src$Pec$Pec_c25519_i15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_i15$O src$Pec$Pec_c25519_i15.c
+
+$(OBJDIR)$Pec_c25519_i31$O: src$Pec$Pec_c25519_i31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_i31$O src$Pec$Pec_c25519_i31.c
+
+$(OBJDIR)$Pec_c25519_m15$O: src$Pec$Pec_c25519_m15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_m15$O src$Pec$Pec_c25519_m15.c
+
+$(OBJDIR)$Pec_c25519_m31$O: src$Pec$Pec_c25519_m31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_m31$O src$Pec$Pec_c25519_m31.c
+
+$(OBJDIR)$Pec_c25519_m62$O: src$Pec$Pec_c25519_m62.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_m62$O src$Pec$Pec_c25519_m62.c
+
+$(OBJDIR)$Pec_c25519_m64$O: src$Pec$Pec_c25519_m64.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_c25519_m64$O src$Pec$Pec_c25519_m64.c
+
+$(OBJDIR)$Pec_curve25519$O: src$Pec$Pec_curve25519.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_curve25519$O src$Pec$Pec_curve25519.c
+
+$(OBJDIR)$Pec_default$O: src$Pec$Pec_default.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_default$O src$Pec$Pec_default.c
+
+$(OBJDIR)$Pec_keygen$O: src$Pec$Pec_keygen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_keygen$O src$Pec$Pec_keygen.c
+
+$(OBJDIR)$Pec_p256_m15$O: src$Pec$Pec_p256_m15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_p256_m15$O src$Pec$Pec_p256_m15.c
+
+$(OBJDIR)$Pec_p256_m31$O: src$Pec$Pec_p256_m31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_p256_m31$O src$Pec$Pec_p256_m31.c
+
+$(OBJDIR)$Pec_p256_m62$O: src$Pec$Pec_p256_m62.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_p256_m62$O src$Pec$Pec_p256_m62.c
+
+$(OBJDIR)$Pec_p256_m64$O: src$Pec$Pec_p256_m64.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_p256_m64$O src$Pec$Pec_p256_m64.c
+
+$(OBJDIR)$Pec_prime_i15$O: src$Pec$Pec_prime_i15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_prime_i15$O src$Pec$Pec_prime_i15.c
+
+$(OBJDIR)$Pec_prime_i31$O: src$Pec$Pec_prime_i31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_prime_i31$O src$Pec$Pec_prime_i31.c
+
+$(OBJDIR)$Pec_pubkey$O: src$Pec$Pec_pubkey.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_pubkey$O src$Pec$Pec_pubkey.c
+
+$(OBJDIR)$Pec_secp256r1$O: src$Pec$Pec_secp256r1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_secp256r1$O src$Pec$Pec_secp256r1.c
+
+$(OBJDIR)$Pec_secp384r1$O: src$Pec$Pec_secp384r1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_secp384r1$O src$Pec$Pec_secp384r1.c
+
+$(OBJDIR)$Pec_secp521r1$O: src$Pec$Pec_secp521r1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pec_secp521r1$O src$Pec$Pec_secp521r1.c
+
+$(OBJDIR)$Pecdsa_atr$O: src$Pec$Pecdsa_atr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_atr$O src$Pec$Pecdsa_atr.c
+
+$(OBJDIR)$Pecdsa_default_sign_asn1$O: src$Pec$Pecdsa_default_sign_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_default_sign_asn1$O src$Pec$Pecdsa_default_sign_asn1.c
+
+$(OBJDIR)$Pecdsa_default_sign_raw$O: src$Pec$Pecdsa_default_sign_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_default_sign_raw$O src$Pec$Pecdsa_default_sign_raw.c
+
+$(OBJDIR)$Pecdsa_default_vrfy_asn1$O: src$Pec$Pecdsa_default_vrfy_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_default_vrfy_asn1$O src$Pec$Pecdsa_default_vrfy_asn1.c
+
+$(OBJDIR)$Pecdsa_default_vrfy_raw$O: src$Pec$Pecdsa_default_vrfy_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_default_vrfy_raw$O src$Pec$Pecdsa_default_vrfy_raw.c
+
+$(OBJDIR)$Pecdsa_i15_bits$O: src$Pec$Pecdsa_i15_bits.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i15_bits$O src$Pec$Pecdsa_i15_bits.c
+
+$(OBJDIR)$Pecdsa_i15_sign_asn1$O: src$Pec$Pecdsa_i15_sign_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i15_sign_asn1$O src$Pec$Pecdsa_i15_sign_asn1.c
+
+$(OBJDIR)$Pecdsa_i15_sign_raw$O: src$Pec$Pecdsa_i15_sign_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i15_sign_raw$O src$Pec$Pecdsa_i15_sign_raw.c
+
+$(OBJDIR)$Pecdsa_i15_vrfy_asn1$O: src$Pec$Pecdsa_i15_vrfy_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i15_vrfy_asn1$O src$Pec$Pecdsa_i15_vrfy_asn1.c
+
+$(OBJDIR)$Pecdsa_i15_vrfy_raw$O: src$Pec$Pecdsa_i15_vrfy_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i15_vrfy_raw$O src$Pec$Pecdsa_i15_vrfy_raw.c
+
+$(OBJDIR)$Pecdsa_i31_bits$O: src$Pec$Pecdsa_i31_bits.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i31_bits$O src$Pec$Pecdsa_i31_bits.c
+
+$(OBJDIR)$Pecdsa_i31_sign_asn1$O: src$Pec$Pecdsa_i31_sign_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i31_sign_asn1$O src$Pec$Pecdsa_i31_sign_asn1.c
+
+$(OBJDIR)$Pecdsa_i31_sign_raw$O: src$Pec$Pecdsa_i31_sign_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i31_sign_raw$O src$Pec$Pecdsa_i31_sign_raw.c
+
+$(OBJDIR)$Pecdsa_i31_vrfy_asn1$O: src$Pec$Pecdsa_i31_vrfy_asn1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i31_vrfy_asn1$O src$Pec$Pecdsa_i31_vrfy_asn1.c
+
+$(OBJDIR)$Pecdsa_i31_vrfy_raw$O: src$Pec$Pecdsa_i31_vrfy_raw.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_i31_vrfy_raw$O src$Pec$Pecdsa_i31_vrfy_raw.c
+
+$(OBJDIR)$Pecdsa_rta$O: src$Pec$Pecdsa_rta.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pecdsa_rta$O src$Pec$Pecdsa_rta.c
+
+$(OBJDIR)$Pdig_oid$O: src$Phash$Pdig_oid.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdig_oid$O src$Phash$Pdig_oid.c
+
+$(OBJDIR)$Pdig_size$O: src$Phash$Pdig_size.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdig_size$O src$Phash$Pdig_size.c
+
+$(OBJDIR)$Pghash_ctmul$O: src$Phash$Pghash_ctmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pghash_ctmul$O src$Phash$Pghash_ctmul.c
+
+$(OBJDIR)$Pghash_ctmul32$O: src$Phash$Pghash_ctmul32.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pghash_ctmul32$O src$Phash$Pghash_ctmul32.c
+
+$(OBJDIR)$Pghash_ctmul64$O: src$Phash$Pghash_ctmul64.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pghash_ctmul64$O src$Phash$Pghash_ctmul64.c
+
+$(OBJDIR)$Pghash_pclmul$O: src$Phash$Pghash_pclmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pghash_pclmul$O src$Phash$Pghash_pclmul.c
+
+$(OBJDIR)$Pghash_pwr8$O: src$Phash$Pghash_pwr8.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pghash_pwr8$O src$Phash$Pghash_pwr8.c
+
+$(OBJDIR)$Pmd5$O: src$Phash$Pmd5.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pmd5$O src$Phash$Pmd5.c
+
+$(OBJDIR)$Pmd5sha1$O: src$Phash$Pmd5sha1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pmd5sha1$O src$Phash$Pmd5sha1.c
+
+$(OBJDIR)$Pmgf1$O: src$Phash$Pmgf1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pmgf1$O src$Phash$Pmgf1.c
+
+$(OBJDIR)$Pmultihash$O: src$Phash$Pmultihash.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pmultihash$O src$Phash$Pmultihash.c
+
+$(OBJDIR)$Psha1$O: src$Phash$Psha1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psha1$O src$Phash$Psha1.c
+
+$(OBJDIR)$Psha2big$O: src$Phash$Psha2big.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psha2big$O src$Phash$Psha2big.c
+
+$(OBJDIR)$Psha2small$O: src$Phash$Psha2small.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psha2small$O src$Phash$Psha2small.c
+
+$(OBJDIR)$Pi15_add$O: src$Pint$Pi15_add.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_add$O src$Pint$Pi15_add.c
+
+$(OBJDIR)$Pi15_bitlen$O: src$Pint$Pi15_bitlen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_bitlen$O src$Pint$Pi15_bitlen.c
+
+$(OBJDIR)$Pi15_decmod$O: src$Pint$Pi15_decmod.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_decmod$O src$Pint$Pi15_decmod.c
+
+$(OBJDIR)$Pi15_decode$O: src$Pint$Pi15_decode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_decode$O src$Pint$Pi15_decode.c
+
+$(OBJDIR)$Pi15_decred$O: src$Pint$Pi15_decred.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_decred$O src$Pint$Pi15_decred.c
+
+$(OBJDIR)$Pi15_encode$O: src$Pint$Pi15_encode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_encode$O src$Pint$Pi15_encode.c
+
+$(OBJDIR)$Pi15_fmont$O: src$Pint$Pi15_fmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_fmont$O src$Pint$Pi15_fmont.c
+
+$(OBJDIR)$Pi15_iszero$O: src$Pint$Pi15_iszero.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_iszero$O src$Pint$Pi15_iszero.c
+
+$(OBJDIR)$Pi15_moddiv$O: src$Pint$Pi15_moddiv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_moddiv$O src$Pint$Pi15_moddiv.c
+
+$(OBJDIR)$Pi15_modpow$O: src$Pint$Pi15_modpow.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_modpow$O src$Pint$Pi15_modpow.c
+
+$(OBJDIR)$Pi15_modpow2$O: src$Pint$Pi15_modpow2.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_modpow2$O src$Pint$Pi15_modpow2.c
+
+$(OBJDIR)$Pi15_montmul$O: src$Pint$Pi15_montmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_montmul$O src$Pint$Pi15_montmul.c
+
+$(OBJDIR)$Pi15_mulacc$O: src$Pint$Pi15_mulacc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_mulacc$O src$Pint$Pi15_mulacc.c
+
+$(OBJDIR)$Pi15_muladd$O: src$Pint$Pi15_muladd.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_muladd$O src$Pint$Pi15_muladd.c
+
+$(OBJDIR)$Pi15_ninv15$O: src$Pint$Pi15_ninv15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_ninv15$O src$Pint$Pi15_ninv15.c
+
+$(OBJDIR)$Pi15_reduce$O: src$Pint$Pi15_reduce.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_reduce$O src$Pint$Pi15_reduce.c
+
+$(OBJDIR)$Pi15_rshift$O: src$Pint$Pi15_rshift.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_rshift$O src$Pint$Pi15_rshift.c
+
+$(OBJDIR)$Pi15_sub$O: src$Pint$Pi15_sub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_sub$O src$Pint$Pi15_sub.c
+
+$(OBJDIR)$Pi15_tmont$O: src$Pint$Pi15_tmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_tmont$O src$Pint$Pi15_tmont.c
+
+$(OBJDIR)$Pi31_add$O: src$Pint$Pi31_add.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_add$O src$Pint$Pi31_add.c
+
+$(OBJDIR)$Pi31_bitlen$O: src$Pint$Pi31_bitlen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_bitlen$O src$Pint$Pi31_bitlen.c
+
+$(OBJDIR)$Pi31_decmod$O: src$Pint$Pi31_decmod.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_decmod$O src$Pint$Pi31_decmod.c
+
+$(OBJDIR)$Pi31_decode$O: src$Pint$Pi31_decode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_decode$O src$Pint$Pi31_decode.c
+
+$(OBJDIR)$Pi31_decred$O: src$Pint$Pi31_decred.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_decred$O src$Pint$Pi31_decred.c
+
+$(OBJDIR)$Pi31_encode$O: src$Pint$Pi31_encode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_encode$O src$Pint$Pi31_encode.c
+
+$(OBJDIR)$Pi31_fmont$O: src$Pint$Pi31_fmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_fmont$O src$Pint$Pi31_fmont.c
+
+$(OBJDIR)$Pi31_iszero$O: src$Pint$Pi31_iszero.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_iszero$O src$Pint$Pi31_iszero.c
+
+$(OBJDIR)$Pi31_moddiv$O: src$Pint$Pi31_moddiv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_moddiv$O src$Pint$Pi31_moddiv.c
+
+$(OBJDIR)$Pi31_modpow$O: src$Pint$Pi31_modpow.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_modpow$O src$Pint$Pi31_modpow.c
+
+$(OBJDIR)$Pi31_modpow2$O: src$Pint$Pi31_modpow2.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_modpow2$O src$Pint$Pi31_modpow2.c
+
+$(OBJDIR)$Pi31_montmul$O: src$Pint$Pi31_montmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_montmul$O src$Pint$Pi31_montmul.c
+
+$(OBJDIR)$Pi31_mulacc$O: src$Pint$Pi31_mulacc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_mulacc$O src$Pint$Pi31_mulacc.c
+
+$(OBJDIR)$Pi31_muladd$O: src$Pint$Pi31_muladd.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_muladd$O src$Pint$Pi31_muladd.c
+
+$(OBJDIR)$Pi31_ninv31$O: src$Pint$Pi31_ninv31.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_ninv31$O src$Pint$Pi31_ninv31.c
+
+$(OBJDIR)$Pi31_reduce$O: src$Pint$Pi31_reduce.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_reduce$O src$Pint$Pi31_reduce.c
+
+$(OBJDIR)$Pi31_rshift$O: src$Pint$Pi31_rshift.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_rshift$O src$Pint$Pi31_rshift.c
+
+$(OBJDIR)$Pi31_sub$O: src$Pint$Pi31_sub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_sub$O src$Pint$Pi31_sub.c
+
+$(OBJDIR)$Pi31_tmont$O: src$Pint$Pi31_tmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi31_tmont$O src$Pint$Pi31_tmont.c
+
+$(OBJDIR)$Pi32_add$O: src$Pint$Pi32_add.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_add$O src$Pint$Pi32_add.c
+
+$(OBJDIR)$Pi32_bitlen$O: src$Pint$Pi32_bitlen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_bitlen$O src$Pint$Pi32_bitlen.c
+
+$(OBJDIR)$Pi32_decmod$O: src$Pint$Pi32_decmod.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_decmod$O src$Pint$Pi32_decmod.c
+
+$(OBJDIR)$Pi32_decode$O: src$Pint$Pi32_decode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_decode$O src$Pint$Pi32_decode.c
+
+$(OBJDIR)$Pi32_decred$O: src$Pint$Pi32_decred.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_decred$O src$Pint$Pi32_decred.c
+
+$(OBJDIR)$Pi32_div32$O: src$Pint$Pi32_div32.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_div32$O src$Pint$Pi32_div32.c
+
+$(OBJDIR)$Pi32_encode$O: src$Pint$Pi32_encode.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_encode$O src$Pint$Pi32_encode.c
+
+$(OBJDIR)$Pi32_fmont$O: src$Pint$Pi32_fmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_fmont$O src$Pint$Pi32_fmont.c
+
+$(OBJDIR)$Pi32_iszero$O: src$Pint$Pi32_iszero.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_iszero$O src$Pint$Pi32_iszero.c
+
+$(OBJDIR)$Pi32_modpow$O: src$Pint$Pi32_modpow.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_modpow$O src$Pint$Pi32_modpow.c
+
+$(OBJDIR)$Pi32_montmul$O: src$Pint$Pi32_montmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_montmul$O src$Pint$Pi32_montmul.c
+
+$(OBJDIR)$Pi32_mulacc$O: src$Pint$Pi32_mulacc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_mulacc$O src$Pint$Pi32_mulacc.c
+
+$(OBJDIR)$Pi32_muladd$O: src$Pint$Pi32_muladd.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_muladd$O src$Pint$Pi32_muladd.c
+
+$(OBJDIR)$Pi32_ninv32$O: src$Pint$Pi32_ninv32.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_ninv32$O src$Pint$Pi32_ninv32.c
+
+$(OBJDIR)$Pi32_reduce$O: src$Pint$Pi32_reduce.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_reduce$O src$Pint$Pi32_reduce.c
+
+$(OBJDIR)$Pi32_sub$O: src$Pint$Pi32_sub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_sub$O src$Pint$Pi32_sub.c
+
+$(OBJDIR)$Pi32_tmont$O: src$Pint$Pi32_tmont.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi32_tmont$O src$Pint$Pi32_tmont.c
+
+$(OBJDIR)$Pi62_modpow2$O: src$Pint$Pi62_modpow2.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi62_modpow2$O src$Pint$Pi62_modpow2.c
+
+$(OBJDIR)$Phkdf$O: src$Pkdf$Phkdf.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Phkdf$O src$Pkdf$Phkdf.c
+
+$(OBJDIR)$Pshake$O: src$Pkdf$Pshake.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pshake$O src$Pkdf$Pshake.c
+
+$(OBJDIR)$Phmac$O: src$Pmac$Phmac.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Phmac$O src$Pmac$Phmac.c
+
+$(OBJDIR)$Phmac_ct$O: src$Pmac$Phmac_ct.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Phmac_ct$O src$Pmac$Phmac_ct.c
+
+$(OBJDIR)$Paesctr_drbg$O: src$Prand$Paesctr_drbg.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paesctr_drbg$O src$Prand$Paesctr_drbg.c
+
+$(OBJDIR)$Phmac_drbg$O: src$Prand$Phmac_drbg.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Phmac_drbg$O src$Prand$Phmac_drbg.c
+
+$(OBJDIR)$Psysrng$O: src$Prand$Psysrng.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psysrng$O src$Prand$Psysrng.c
+
+$(OBJDIR)$Prsa_default_keygen$O: src$Prsa$Prsa_default_keygen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_keygen$O src$Prsa$Prsa_default_keygen.c
+
+$(OBJDIR)$Prsa_default_modulus$O: src$Prsa$Prsa_default_modulus.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_modulus$O src$Prsa$Prsa_default_modulus.c
+
+$(OBJDIR)$Prsa_default_oaep_decrypt$O: src$Prsa$Prsa_default_oaep_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_oaep_decrypt$O src$Prsa$Prsa_default_oaep_decrypt.c
+
+$(OBJDIR)$Prsa_default_oaep_encrypt$O: src$Prsa$Prsa_default_oaep_encrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_oaep_encrypt$O src$Prsa$Prsa_default_oaep_encrypt.c
+
+$(OBJDIR)$Prsa_default_pkcs1_sign$O: src$Prsa$Prsa_default_pkcs1_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pkcs1_sign$O src$Prsa$Prsa_default_pkcs1_sign.c
+
+$(OBJDIR)$Prsa_default_pkcs1_vrfy$O: src$Prsa$Prsa_default_pkcs1_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pkcs1_vrfy$O src$Prsa$Prsa_default_pkcs1_vrfy.c
+
+$(OBJDIR)$Prsa_default_priv$O: src$Prsa$Prsa_default_priv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_priv$O src$Prsa$Prsa_default_priv.c
+
+$(OBJDIR)$Prsa_default_privexp$O: src$Prsa$Prsa_default_privexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_privexp$O src$Prsa$Prsa_default_privexp.c
+
+$(OBJDIR)$Prsa_default_pss_sign$O: src$Prsa$Prsa_default_pss_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pss_sign$O src$Prsa$Prsa_default_pss_sign.c
+
+$(OBJDIR)$Prsa_default_pss_vrfy$O: src$Prsa$Prsa_default_pss_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pss_vrfy$O src$Prsa$Prsa_default_pss_vrfy.c
+
+$(OBJDIR)$Prsa_default_pub$O: src$Prsa$Prsa_default_pub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pub$O src$Prsa$Prsa_default_pub.c
+
+$(OBJDIR)$Prsa_default_pubexp$O: src$Prsa$Prsa_default_pubexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_default_pubexp$O src$Prsa$Prsa_default_pubexp.c
+
+$(OBJDIR)$Prsa_i15_keygen$O: src$Prsa$Prsa_i15_keygen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_keygen$O src$Prsa$Prsa_i15_keygen.c
+
+$(OBJDIR)$Prsa_i15_modulus$O: src$Prsa$Prsa_i15_modulus.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_modulus$O src$Prsa$Prsa_i15_modulus.c
+
+$(OBJDIR)$Prsa_i15_oaep_decrypt$O: src$Prsa$Prsa_i15_oaep_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_oaep_decrypt$O src$Prsa$Prsa_i15_oaep_decrypt.c
+
+$(OBJDIR)$Prsa_i15_oaep_encrypt$O: src$Prsa$Prsa_i15_oaep_encrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_oaep_encrypt$O src$Prsa$Prsa_i15_oaep_encrypt.c
+
+$(OBJDIR)$Prsa_i15_pkcs1_sign$O: src$Prsa$Prsa_i15_pkcs1_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pkcs1_sign$O src$Prsa$Prsa_i15_pkcs1_sign.c
+
+$(OBJDIR)$Prsa_i15_pkcs1_vrfy$O: src$Prsa$Prsa_i15_pkcs1_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pkcs1_vrfy$O src$Prsa$Prsa_i15_pkcs1_vrfy.c
+
+$(OBJDIR)$Prsa_i15_priv$O: src$Prsa$Prsa_i15_priv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_priv$O src$Prsa$Prsa_i15_priv.c
+
+$(OBJDIR)$Prsa_i15_privexp$O: src$Prsa$Prsa_i15_privexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_privexp$O src$Prsa$Prsa_i15_privexp.c
+
+$(OBJDIR)$Prsa_i15_pss_sign$O: src$Prsa$Prsa_i15_pss_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pss_sign$O src$Prsa$Prsa_i15_pss_sign.c
+
+$(OBJDIR)$Prsa_i15_pss_vrfy$O: src$Prsa$Prsa_i15_pss_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pss_vrfy$O src$Prsa$Prsa_i15_pss_vrfy.c
+
+$(OBJDIR)$Prsa_i15_pub$O: src$Prsa$Prsa_i15_pub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pub$O src$Prsa$Prsa_i15_pub.c
+
+$(OBJDIR)$Prsa_i15_pubexp$O: src$Prsa$Prsa_i15_pubexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i15_pubexp$O src$Prsa$Prsa_i15_pubexp.c
+
+$(OBJDIR)$Prsa_i31_keygen$O: src$Prsa$Prsa_i31_keygen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_keygen$O src$Prsa$Prsa_i31_keygen.c
+
+$(OBJDIR)$Prsa_i31_keygen_inner$O: src$Prsa$Prsa_i31_keygen_inner.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_keygen_inner$O src$Prsa$Prsa_i31_keygen_inner.c
+
+$(OBJDIR)$Prsa_i31_modulus$O: src$Prsa$Prsa_i31_modulus.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_modulus$O src$Prsa$Prsa_i31_modulus.c
+
+$(OBJDIR)$Prsa_i31_oaep_decrypt$O: src$Prsa$Prsa_i31_oaep_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_oaep_decrypt$O src$Prsa$Prsa_i31_oaep_decrypt.c
+
+$(OBJDIR)$Prsa_i31_oaep_encrypt$O: src$Prsa$Prsa_i31_oaep_encrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_oaep_encrypt$O src$Prsa$Prsa_i31_oaep_encrypt.c
+
+$(OBJDIR)$Prsa_i31_pkcs1_sign$O: src$Prsa$Prsa_i31_pkcs1_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pkcs1_sign$O src$Prsa$Prsa_i31_pkcs1_sign.c
+
+$(OBJDIR)$Prsa_i31_pkcs1_vrfy$O: src$Prsa$Prsa_i31_pkcs1_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pkcs1_vrfy$O src$Prsa$Prsa_i31_pkcs1_vrfy.c
+
+$(OBJDIR)$Prsa_i31_priv$O: src$Prsa$Prsa_i31_priv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_priv$O src$Prsa$Prsa_i31_priv.c
+
+$(OBJDIR)$Prsa_i31_privexp$O: src$Prsa$Prsa_i31_privexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_privexp$O src$Prsa$Prsa_i31_privexp.c
+
+$(OBJDIR)$Prsa_i31_pss_sign$O: src$Prsa$Prsa_i31_pss_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pss_sign$O src$Prsa$Prsa_i31_pss_sign.c
+
+$(OBJDIR)$Prsa_i31_pss_vrfy$O: src$Prsa$Prsa_i31_pss_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pss_vrfy$O src$Prsa$Prsa_i31_pss_vrfy.c
+
+$(OBJDIR)$Prsa_i31_pub$O: src$Prsa$Prsa_i31_pub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pub$O src$Prsa$Prsa_i31_pub.c
+
+$(OBJDIR)$Prsa_i31_pubexp$O: src$Prsa$Prsa_i31_pubexp.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i31_pubexp$O src$Prsa$Prsa_i31_pubexp.c
+
+$(OBJDIR)$Prsa_i32_oaep_decrypt$O: src$Prsa$Prsa_i32_oaep_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_oaep_decrypt$O src$Prsa$Prsa_i32_oaep_decrypt.c
+
+$(OBJDIR)$Prsa_i32_oaep_encrypt$O: src$Prsa$Prsa_i32_oaep_encrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_oaep_encrypt$O src$Prsa$Prsa_i32_oaep_encrypt.c
+
+$(OBJDIR)$Prsa_i32_pkcs1_sign$O: src$Prsa$Prsa_i32_pkcs1_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_pkcs1_sign$O src$Prsa$Prsa_i32_pkcs1_sign.c
+
+$(OBJDIR)$Prsa_i32_pkcs1_vrfy$O: src$Prsa$Prsa_i32_pkcs1_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_pkcs1_vrfy$O src$Prsa$Prsa_i32_pkcs1_vrfy.c
+
+$(OBJDIR)$Prsa_i32_priv$O: src$Prsa$Prsa_i32_priv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_priv$O src$Prsa$Prsa_i32_priv.c
+
+$(OBJDIR)$Prsa_i32_pss_sign$O: src$Prsa$Prsa_i32_pss_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_pss_sign$O src$Prsa$Prsa_i32_pss_sign.c
+
+$(OBJDIR)$Prsa_i32_pss_vrfy$O: src$Prsa$Prsa_i32_pss_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_pss_vrfy$O src$Prsa$Prsa_i32_pss_vrfy.c
+
+$(OBJDIR)$Prsa_i32_pub$O: src$Prsa$Prsa_i32_pub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i32_pub$O src$Prsa$Prsa_i32_pub.c
+
+$(OBJDIR)$Prsa_i62_keygen$O: src$Prsa$Prsa_i62_keygen.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_keygen$O src$Prsa$Prsa_i62_keygen.c
+
+$(OBJDIR)$Prsa_i62_oaep_decrypt$O: src$Prsa$Prsa_i62_oaep_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_oaep_decrypt$O src$Prsa$Prsa_i62_oaep_decrypt.c
+
+$(OBJDIR)$Prsa_i62_oaep_encrypt$O: src$Prsa$Prsa_i62_oaep_encrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_oaep_encrypt$O src$Prsa$Prsa_i62_oaep_encrypt.c
+
+$(OBJDIR)$Prsa_i62_pkcs1_sign$O: src$Prsa$Prsa_i62_pkcs1_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_pkcs1_sign$O src$Prsa$Prsa_i62_pkcs1_sign.c
+
+$(OBJDIR)$Prsa_i62_pkcs1_vrfy$O: src$Prsa$Prsa_i62_pkcs1_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_pkcs1_vrfy$O src$Prsa$Prsa_i62_pkcs1_vrfy.c
+
+$(OBJDIR)$Prsa_i62_priv$O: src$Prsa$Prsa_i62_priv.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_priv$O src$Prsa$Prsa_i62_priv.c
+
+$(OBJDIR)$Prsa_i62_pss_sign$O: src$Prsa$Prsa_i62_pss_sign.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_pss_sign$O src$Prsa$Prsa_i62_pss_sign.c
+
+$(OBJDIR)$Prsa_i62_pss_vrfy$O: src$Prsa$Prsa_i62_pss_vrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_pss_vrfy$O src$Prsa$Prsa_i62_pss_vrfy.c
+
+$(OBJDIR)$Prsa_i62_pub$O: src$Prsa$Prsa_i62_pub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_i62_pub$O src$Prsa$Prsa_i62_pub.c
+
+$(OBJDIR)$Prsa_oaep_pad$O: src$Prsa$Prsa_oaep_pad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_oaep_pad$O src$Prsa$Prsa_oaep_pad.c
+
+$(OBJDIR)$Prsa_oaep_unpad$O: src$Prsa$Prsa_oaep_unpad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_oaep_unpad$O src$Prsa$Prsa_oaep_unpad.c
+
+$(OBJDIR)$Prsa_pkcs1_sig_pad$O: src$Prsa$Prsa_pkcs1_sig_pad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_pkcs1_sig_pad$O src$Prsa$Prsa_pkcs1_sig_pad.c
+
+$(OBJDIR)$Prsa_pkcs1_sig_unpad$O: src$Prsa$Prsa_pkcs1_sig_unpad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_pkcs1_sig_unpad$O src$Prsa$Prsa_pkcs1_sig_unpad.c
+
+$(OBJDIR)$Prsa_pss_sig_pad$O: src$Prsa$Prsa_pss_sig_pad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_pss_sig_pad$O src$Prsa$Prsa_pss_sig_pad.c
+
+$(OBJDIR)$Prsa_pss_sig_unpad$O: src$Prsa$Prsa_pss_sig_unpad.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_pss_sig_unpad$O src$Prsa$Prsa_pss_sig_unpad.c
+
+$(OBJDIR)$Prsa_ssl_decrypt$O: src$Prsa$Prsa_ssl_decrypt.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Prsa_ssl_decrypt$O src$Prsa$Prsa_ssl_decrypt.c
+
+$(OBJDIR)$Pprf$O: src$Pssl$Pprf.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pprf$O src$Pssl$Pprf.c
+
+$(OBJDIR)$Pprf_md5sha1$O: src$Pssl$Pprf_md5sha1.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pprf_md5sha1$O src$Pssl$Pprf_md5sha1.c
+
+$(OBJDIR)$Pprf_sha256$O: src$Pssl$Pprf_sha256.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pprf_sha256$O src$Pssl$Pprf_sha256.c
+
+$(OBJDIR)$Pprf_sha384$O: src$Pssl$Pprf_sha384.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pprf_sha384$O src$Pssl$Pprf_sha384.c
+
+$(OBJDIR)$Pssl_ccert_single_ec$O: src$Pssl$Pssl_ccert_single_ec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_ccert_single_ec$O src$Pssl$Pssl_ccert_single_ec.c
+
+$(OBJDIR)$Pssl_ccert_single_rsa$O: src$Pssl$Pssl_ccert_single_rsa.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_ccert_single_rsa$O src$Pssl$Pssl_ccert_single_rsa.c
+
+$(OBJDIR)$Pssl_client$O: src$Pssl$Pssl_client.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_client$O src$Pssl$Pssl_client.c
+
+$(OBJDIR)$Pssl_client_default_rsapub$O: src$Pssl$Pssl_client_default_rsapub.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_client_default_rsapub$O src$Pssl$Pssl_client_default_rsapub.c
+
+$(OBJDIR)$Pssl_client_full$O: src$Pssl$Pssl_client_full.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_client_full$O src$Pssl$Pssl_client_full.c
+
+$(OBJDIR)$Pssl_engine$O: src$Pssl$Pssl_engine.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine$O src$Pssl$Pssl_engine.c
+
+$(OBJDIR)$Pssl_engine_default_aescbc$O: src$Pssl$Pssl_engine_default_aescbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_aescbc$O src$Pssl$Pssl_engine_default_aescbc.c
+
+$(OBJDIR)$Pssl_engine_default_aesccm$O: src$Pssl$Pssl_engine_default_aesccm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_aesccm$O src$Pssl$Pssl_engine_default_aesccm.c
+
+$(OBJDIR)$Pssl_engine_default_aesgcm$O: src$Pssl$Pssl_engine_default_aesgcm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_aesgcm$O src$Pssl$Pssl_engine_default_aesgcm.c
+
+$(OBJDIR)$Pssl_engine_default_chapol$O: src$Pssl$Pssl_engine_default_chapol.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_chapol$O src$Pssl$Pssl_engine_default_chapol.c
+
+$(OBJDIR)$Pssl_engine_default_descbc$O: src$Pssl$Pssl_engine_default_descbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_descbc$O src$Pssl$Pssl_engine_default_descbc.c
+
+$(OBJDIR)$Pssl_engine_default_ec$O: src$Pssl$Pssl_engine_default_ec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_ec$O src$Pssl$Pssl_engine_default_ec.c
+
+$(OBJDIR)$Pssl_engine_default_ecdsa$O: src$Pssl$Pssl_engine_default_ecdsa.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_ecdsa$O src$Pssl$Pssl_engine_default_ecdsa.c
+
+$(OBJDIR)$Pssl_engine_default_rsavrfy$O: src$Pssl$Pssl_engine_default_rsavrfy.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_engine_default_rsavrfy$O src$Pssl$Pssl_engine_default_rsavrfy.c
+
+$(OBJDIR)$Pssl_hashes$O: src$Pssl$Pssl_hashes.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_hashes$O src$Pssl$Pssl_hashes.c
+
+$(OBJDIR)$Pssl_hs_client$O: src$Pssl$Pssl_hs_client.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_hs_client$O src$Pssl$Pssl_hs_client.c
+
+$(OBJDIR)$Pssl_hs_server$O: src$Pssl$Pssl_hs_server.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_hs_server$O src$Pssl$Pssl_hs_server.c
+
+$(OBJDIR)$Pssl_io$O: src$Pssl$Pssl_io.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_io$O src$Pssl$Pssl_io.c
+
+$(OBJDIR)$Pssl_keyexport$O: src$Pssl$Pssl_keyexport.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_keyexport$O src$Pssl$Pssl_keyexport.c
+
+$(OBJDIR)$Pssl_lru$O: src$Pssl$Pssl_lru.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_lru$O src$Pssl$Pssl_lru.c
+
+$(OBJDIR)$Pssl_rec_cbc$O: src$Pssl$Pssl_rec_cbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_rec_cbc$O src$Pssl$Pssl_rec_cbc.c
+
+$(OBJDIR)$Pssl_rec_ccm$O: src$Pssl$Pssl_rec_ccm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_rec_ccm$O src$Pssl$Pssl_rec_ccm.c
+
+$(OBJDIR)$Pssl_rec_chapol$O: src$Pssl$Pssl_rec_chapol.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_rec_chapol$O src$Pssl$Pssl_rec_chapol.c
+
+$(OBJDIR)$Pssl_rec_gcm$O: src$Pssl$Pssl_rec_gcm.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_rec_gcm$O src$Pssl$Pssl_rec_gcm.c
+
+$(OBJDIR)$Pssl_scert_single_ec$O: src$Pssl$Pssl_scert_single_ec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_scert_single_ec$O src$Pssl$Pssl_scert_single_ec.c
+
+$(OBJDIR)$Pssl_scert_single_rsa$O: src$Pssl$Pssl_scert_single_rsa.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_scert_single_rsa$O src$Pssl$Pssl_scert_single_rsa.c
+
+$(OBJDIR)$Pssl_server$O: src$Pssl$Pssl_server.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server$O src$Pssl$Pssl_server.c
+
+$(OBJDIR)$Pssl_server_full_ec$O: src$Pssl$Pssl_server_full_ec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_full_ec$O src$Pssl$Pssl_server_full_ec.c
+
+$(OBJDIR)$Pssl_server_full_rsa$O: src$Pssl$Pssl_server_full_rsa.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_full_rsa$O src$Pssl$Pssl_server_full_rsa.c
+
+$(OBJDIR)$Pssl_server_mine2c$O: src$Pssl$Pssl_server_mine2c.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_mine2c$O src$Pssl$Pssl_server_mine2c.c
+
+$(OBJDIR)$Pssl_server_mine2g$O: src$Pssl$Pssl_server_mine2g.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_mine2g$O src$Pssl$Pssl_server_mine2g.c
+
+$(OBJDIR)$Pssl_server_minf2c$O: src$Pssl$Pssl_server_minf2c.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_minf2c$O src$Pssl$Pssl_server_minf2c.c
+
+$(OBJDIR)$Pssl_server_minf2g$O: src$Pssl$Pssl_server_minf2g.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_minf2g$O src$Pssl$Pssl_server_minf2g.c
+
+$(OBJDIR)$Pssl_server_minr2g$O: src$Pssl$Pssl_server_minr2g.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_minr2g$O src$Pssl$Pssl_server_minr2g.c
+
+$(OBJDIR)$Pssl_server_minu2g$O: src$Pssl$Pssl_server_minu2g.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_minu2g$O src$Pssl$Pssl_server_minu2g.c
+
+$(OBJDIR)$Pssl_server_minv2g$O: src$Pssl$Pssl_server_minv2g.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pssl_server_minv2g$O src$Pssl$Pssl_server_minv2g.c
+
+$(OBJDIR)$Paes_big_cbcdec$O: src$Psymcipher$Paes_big_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_cbcdec$O src$Psymcipher$Paes_big_cbcdec.c
+
+$(OBJDIR)$Paes_big_cbcenc$O: src$Psymcipher$Paes_big_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_cbcenc$O src$Psymcipher$Paes_big_cbcenc.c
+
+$(OBJDIR)$Paes_big_ctr$O: src$Psymcipher$Paes_big_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_ctr$O src$Psymcipher$Paes_big_ctr.c
+
+$(OBJDIR)$Paes_big_ctrcbc$O: src$Psymcipher$Paes_big_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_ctrcbc$O src$Psymcipher$Paes_big_ctrcbc.c
+
+$(OBJDIR)$Paes_big_dec$O: src$Psymcipher$Paes_big_dec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_dec$O src$Psymcipher$Paes_big_dec.c
+
+$(OBJDIR)$Paes_big_enc$O: src$Psymcipher$Paes_big_enc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_big_enc$O src$Psymcipher$Paes_big_enc.c
+
+$(OBJDIR)$Paes_common$O: src$Psymcipher$Paes_common.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_common$O src$Psymcipher$Paes_common.c
+
+$(OBJDIR)$Paes_ct$O: src$Psymcipher$Paes_ct.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct$O src$Psymcipher$Paes_ct.c
+
+$(OBJDIR)$Paes_ct64$O: src$Psymcipher$Paes_ct64.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64$O src$Psymcipher$Paes_ct64.c
+
+$(OBJDIR)$Paes_ct64_cbcdec$O: src$Psymcipher$Paes_ct64_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_cbcdec$O src$Psymcipher$Paes_ct64_cbcdec.c
+
+$(OBJDIR)$Paes_ct64_cbcenc$O: src$Psymcipher$Paes_ct64_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_cbcenc$O src$Psymcipher$Paes_ct64_cbcenc.c
+
+$(OBJDIR)$Paes_ct64_ctr$O: src$Psymcipher$Paes_ct64_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_ctr$O src$Psymcipher$Paes_ct64_ctr.c
+
+$(OBJDIR)$Paes_ct64_ctrcbc$O: src$Psymcipher$Paes_ct64_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_ctrcbc$O src$Psymcipher$Paes_ct64_ctrcbc.c
+
+$(OBJDIR)$Paes_ct64_dec$O: src$Psymcipher$Paes_ct64_dec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_dec$O src$Psymcipher$Paes_ct64_dec.c
+
+$(OBJDIR)$Paes_ct64_enc$O: src$Psymcipher$Paes_ct64_enc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct64_enc$O src$Psymcipher$Paes_ct64_enc.c
+
+$(OBJDIR)$Paes_ct_cbcdec$O: src$Psymcipher$Paes_ct_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_cbcdec$O src$Psymcipher$Paes_ct_cbcdec.c
+
+$(OBJDIR)$Paes_ct_cbcenc$O: src$Psymcipher$Paes_ct_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_cbcenc$O src$Psymcipher$Paes_ct_cbcenc.c
+
+$(OBJDIR)$Paes_ct_ctr$O: src$Psymcipher$Paes_ct_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_ctr$O src$Psymcipher$Paes_ct_ctr.c
+
+$(OBJDIR)$Paes_ct_ctrcbc$O: src$Psymcipher$Paes_ct_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_ctrcbc$O src$Psymcipher$Paes_ct_ctrcbc.c
+
+$(OBJDIR)$Paes_ct_dec$O: src$Psymcipher$Paes_ct_dec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_dec$O src$Psymcipher$Paes_ct_dec.c
+
+$(OBJDIR)$Paes_ct_enc$O: src$Psymcipher$Paes_ct_enc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_ct_enc$O src$Psymcipher$Paes_ct_enc.c
+
+$(OBJDIR)$Paes_pwr8$O: src$Psymcipher$Paes_pwr8.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_pwr8$O src$Psymcipher$Paes_pwr8.c
+
+$(OBJDIR)$Paes_pwr8_cbcdec$O: src$Psymcipher$Paes_pwr8_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_pwr8_cbcdec$O src$Psymcipher$Paes_pwr8_cbcdec.c
+
+$(OBJDIR)$Paes_pwr8_cbcenc$O: src$Psymcipher$Paes_pwr8_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_pwr8_cbcenc$O src$Psymcipher$Paes_pwr8_cbcenc.c
+
+$(OBJDIR)$Paes_pwr8_ctr$O: src$Psymcipher$Paes_pwr8_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_pwr8_ctr$O src$Psymcipher$Paes_pwr8_ctr.c
+
+$(OBJDIR)$Paes_pwr8_ctrcbc$O: src$Psymcipher$Paes_pwr8_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_pwr8_ctrcbc$O src$Psymcipher$Paes_pwr8_ctrcbc.c
+
+$(OBJDIR)$Paes_small_cbcdec$O: src$Psymcipher$Paes_small_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_cbcdec$O src$Psymcipher$Paes_small_cbcdec.c
+
+$(OBJDIR)$Paes_small_cbcenc$O: src$Psymcipher$Paes_small_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_cbcenc$O src$Psymcipher$Paes_small_cbcenc.c
+
+$(OBJDIR)$Paes_small_ctr$O: src$Psymcipher$Paes_small_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_ctr$O src$Psymcipher$Paes_small_ctr.c
+
+$(OBJDIR)$Paes_small_ctrcbc$O: src$Psymcipher$Paes_small_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_ctrcbc$O src$Psymcipher$Paes_small_ctrcbc.c
+
+$(OBJDIR)$Paes_small_dec$O: src$Psymcipher$Paes_small_dec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_dec$O src$Psymcipher$Paes_small_dec.c
+
+$(OBJDIR)$Paes_small_enc$O: src$Psymcipher$Paes_small_enc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_small_enc$O src$Psymcipher$Paes_small_enc.c
+
+$(OBJDIR)$Paes_x86ni$O: src$Psymcipher$Paes_x86ni.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_x86ni$O src$Psymcipher$Paes_x86ni.c
+
+$(OBJDIR)$Paes_x86ni_cbcdec$O: src$Psymcipher$Paes_x86ni_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_x86ni_cbcdec$O src$Psymcipher$Paes_x86ni_cbcdec.c
+
+$(OBJDIR)$Paes_x86ni_cbcenc$O: src$Psymcipher$Paes_x86ni_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_x86ni_cbcenc$O src$Psymcipher$Paes_x86ni_cbcenc.c
+
+$(OBJDIR)$Paes_x86ni_ctr$O: src$Psymcipher$Paes_x86ni_ctr.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_x86ni_ctr$O src$Psymcipher$Paes_x86ni_ctr.c
+
+$(OBJDIR)$Paes_x86ni_ctrcbc$O: src$Psymcipher$Paes_x86ni_ctrcbc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Paes_x86ni_ctrcbc$O src$Psymcipher$Paes_x86ni_ctrcbc.c
+
+$(OBJDIR)$Pchacha20_ct$O: src$Psymcipher$Pchacha20_ct.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pchacha20_ct$O src$Psymcipher$Pchacha20_ct.c
+
+$(OBJDIR)$Pchacha20_sse2$O: src$Psymcipher$Pchacha20_sse2.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pchacha20_sse2$O src$Psymcipher$Pchacha20_sse2.c
+
+$(OBJDIR)$Pdes_ct$O: src$Psymcipher$Pdes_ct.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_ct$O src$Psymcipher$Pdes_ct.c
+
+$(OBJDIR)$Pdes_ct_cbcdec$O: src$Psymcipher$Pdes_ct_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_ct_cbcdec$O src$Psymcipher$Pdes_ct_cbcdec.c
+
+$(OBJDIR)$Pdes_ct_cbcenc$O: src$Psymcipher$Pdes_ct_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_ct_cbcenc$O src$Psymcipher$Pdes_ct_cbcenc.c
+
+$(OBJDIR)$Pdes_support$O: src$Psymcipher$Pdes_support.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_support$O src$Psymcipher$Pdes_support.c
+
+$(OBJDIR)$Pdes_tab$O: src$Psymcipher$Pdes_tab.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_tab$O src$Psymcipher$Pdes_tab.c
+
+$(OBJDIR)$Pdes_tab_cbcdec$O: src$Psymcipher$Pdes_tab_cbcdec.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_tab_cbcdec$O src$Psymcipher$Pdes_tab_cbcdec.c
+
+$(OBJDIR)$Pdes_tab_cbcenc$O: src$Psymcipher$Pdes_tab_cbcenc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pdes_tab_cbcenc$O src$Psymcipher$Pdes_tab_cbcenc.c
+
+$(OBJDIR)$Ppoly1305_ctmul$O: src$Psymcipher$Ppoly1305_ctmul.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppoly1305_ctmul$O src$Psymcipher$Ppoly1305_ctmul.c
+
+$(OBJDIR)$Ppoly1305_ctmul32$O: src$Psymcipher$Ppoly1305_ctmul32.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppoly1305_ctmul32$O src$Psymcipher$Ppoly1305_ctmul32.c
+
+$(OBJDIR)$Ppoly1305_ctmulq$O: src$Psymcipher$Ppoly1305_ctmulq.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppoly1305_ctmulq$O src$Psymcipher$Ppoly1305_ctmulq.c
+
+$(OBJDIR)$Ppoly1305_i15$O: src$Psymcipher$Ppoly1305_i15.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ppoly1305_i15$O src$Psymcipher$Ppoly1305_i15.c
+
+$(OBJDIR)$Pasn1enc$O: src$Px509$Pasn1enc.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pasn1enc$O src$Px509$Pasn1enc.c
+
+$(OBJDIR)$Pencode_ec_pk8der$O: src$Px509$Pencode_ec_pk8der.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pencode_ec_pk8der$O src$Px509$Pencode_ec_pk8der.c
+
+$(OBJDIR)$Pencode_ec_rawder$O: src$Px509$Pencode_ec_rawder.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pencode_ec_rawder$O src$Px509$Pencode_ec_rawder.c
+
+$(OBJDIR)$Pencode_rsa_pk8der$O: src$Px509$Pencode_rsa_pk8der.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pencode_rsa_pk8der$O src$Px509$Pencode_rsa_pk8der.c
+
+$(OBJDIR)$Pencode_rsa_rawder$O: src$Px509$Pencode_rsa_rawder.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pencode_rsa_rawder$O src$Px509$Pencode_rsa_rawder.c
+
+$(OBJDIR)$Pskey_decoder$O: src$Px509$Pskey_decoder.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pskey_decoder$O src$Px509$Pskey_decoder.c
+
+$(OBJDIR)$Px509_decoder$O: src$Px509$Px509_decoder.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Px509_decoder$O src$Px509$Px509_decoder.c
+
+$(OBJDIR)$Px509_knownkey$O: src$Px509$Px509_knownkey.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Px509_knownkey$O src$Px509$Px509_knownkey.c
+
+# nsl_op: Omod: 32-bits modulo not supported yet. Please use 64-bits.
+$(OBJDIR)$Px509_minimal$O: src$Px509$Px509_minimal.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Px509_minimal$O src$Px509$Px509_minimal.c
+
+$(OBJDIR)$Px509_minimal_full$O: src$Px509$Px509_minimal_full.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Px509_minimal_full$O src$Px509$Px509_minimal_full.c
+
+$(OBJDIR)$Pbrssl$O: tools$Pbrssl.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pbrssl$O tools$Pbrssl.c
+
+$(OBJDIR)$Pcerts$O: tools$Pcerts.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pcerts$O tools$Pcerts.c
+
+$(OBJDIR)$Pchain$O: tools$Pchain.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pchain$O tools$Pchain.c
+
+$(OBJDIR)$Pclient$O: tools$Pclient.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pclient$O tools$Pclient.c
+
+$(OBJDIR)$Perrors$O: tools$Perrors.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Perrors$O tools$Perrors.c
+
+$(OBJDIR)$Pfiles$O: tools$Pfiles.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pfiles$O tools$Pfiles.c
+
+$(OBJDIR)$Pimpl$O: tools$Pimpl.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pimpl$O tools$Pimpl.c
+
+$(OBJDIR)$Pkeys$O: tools$Pkeys.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pkeys$O tools$Pkeys.c
+
+$(OBJDIR)$Pnames$O: tools$Pnames.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pnames$O tools$Pnames.c
+
+$(OBJDIR)$Pserver$O: tools$Pserver.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pserver$O tools$Pserver.c
+
+$(OBJDIR)$Pskey$O: tools$Pskey.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pskey$O tools$Pskey.c
+
+$(OBJDIR)$Psslio$O: tools$Psslio.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Psslio$O tools$Psslio.c
+
+$(OBJDIR)$Pta$O: tools$Pta.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pta$O tools$Pta.c
+
+$(OBJDIR)$Ptwrch$O: tools$Ptwrch.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ptwrch$O tools$Ptwrch.c
+
+$(OBJDIR)$Pvector$O: tools$Pvector.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pvector$O tools$Pvector.c
+
+$(OBJDIR)$Pverify$O: tools$Pverify.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pverify$O tools$Pverify.c
+
+$(OBJDIR)$Pxmem$O: tools$Pxmem.c $(HEADERSTOOLS)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pxmem$O tools$Pxmem.c
+
+$(OBJDIR)$Ptest_crypto$O: test$Ptest_crypto.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ptest_crypto$O test$Ptest_crypto.c
+
+$(OBJDIR)$Ptest_speed$O: test$Ptest_speed.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Ptest_speed$O test$Ptest_speed.c
+
+$(OBJDIR)$Ptest_x509$O: test$Ptest_x509.c $(HEADERSPRIV)
+ $(CC) $(CFLAGS) $(INCFLAGS) -DSRCDIRNAME=".." $(CCOUT)$(OBJDIR)$Ptest_x509$O test$Ptest_x509.c
diff --git a/test/monniaux/BearSSL/mk/SingleUnix.mk b/test/monniaux/BearSSL/mk/SingleUnix.mk
new file mode 100644
index 00000000..e169617b
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/SingleUnix.mk
@@ -0,0 +1,38 @@
+# Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# ======================================================================
+
+# This file sets variables for use with a SingleUnix-compatible 'make'
+# utility.
+
+# Load generic default.
+include mk/Defaults.mk
+
+# Path separator.
+P = /
+
+# Default configuration is 'Unix' (native build on a Unix-like system).
+CONF = Unix
+
+include conf/$(CONF).mk
+include mk/Rules.mk
diff --git a/test/monniaux/BearSSL/mk/mkT0.sh b/test/monniaux/BearSSL/mk/mkT0.sh
new file mode 100755
index 00000000..61de5c6e
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/mkT0.sh
@@ -0,0 +1,11 @@
+#! /bin/sh
+
+CSC=$(which mono-csc || which dmcs || echo "none")
+
+if [ $CSC = "none" ]; then
+ echo "Error: Please install mono-devel."
+ exit 1
+fi
+
+set -e
+$CSC /out:T0Comp.exe /main:T0Comp /res:T0/kern.t0,t0-kernel T0/*.cs
diff --git a/test/monniaux/BearSSL/mk/mkrules.sh b/test/monniaux/BearSSL/mk/mkrules.sh
new file mode 100755
index 00000000..297a5d5b
--- /dev/null
+++ b/test/monniaux/BearSSL/mk/mkrules.sh
@@ -0,0 +1,570 @@
+#! /bin/sh
+
+# ========================================================================
+#
+# Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+#
+# ========================================================================
+#
+# This script is used to generate the 'Rules.mk' file from the list
+# of source file included below. If the list changes (e.g. to add a
+# new source file), then add it here and rerun this script.
+#
+# ========================================================================
+
+# Solaris compatibility: switch to a more POSIX-compliant /bin/sh.
+if [ -z "$BR_SCRIPT_LOOP" ] ; then
+ BR_SCRIPT_LOOP=yes
+ export BR_SCRIPT_LOOP
+ if [ -x /usr/xpg6/bin/sh ] ; then
+ exec /usr/xpg6/bin/sh "$0" "$@"
+ fi
+ if [ -x /usr/xpg4/bin/sh ] ; then
+ exec /usr/xpg4/bin/sh "$0" "$@"
+ fi
+fi
+
+# Exit on first error.
+set -e
+
+# Source files. Please keep in alphabetical order.
+coresrc=" \
+ src/settings.c \
+ src/aead/ccm.c \
+ src/aead/eax.c \
+ src/aead/gcm.c \
+ src/codec/ccopy.c \
+ src/codec/dec16be.c \
+ src/codec/dec16le.c \
+ src/codec/dec32be.c \
+ src/codec/dec32le.c \
+ src/codec/dec64be.c \
+ src/codec/dec64le.c \
+ src/codec/enc16be.c \
+ src/codec/enc16le.c \
+ src/codec/enc32be.c \
+ src/codec/enc32le.c \
+ src/codec/enc64be.c \
+ src/codec/enc64le.c \
+ src/codec/pemdec.c \
+ src/codec/pemenc.c \
+ src/ec/ec_all_m15.c \
+ src/ec/ec_all_m31.c \
+ src/ec/ec_c25519_i15.c \
+ src/ec/ec_c25519_i31.c \
+ src/ec/ec_c25519_m15.c \
+ src/ec/ec_c25519_m31.c \
+ src/ec/ec_c25519_m62.c \
+ src/ec/ec_c25519_m64.c \
+ src/ec/ec_curve25519.c \
+ src/ec/ec_default.c \
+ src/ec/ec_keygen.c \
+ src/ec/ec_p256_m15.c \
+ src/ec/ec_p256_m31.c \
+ src/ec/ec_p256_m62.c \
+ src/ec/ec_p256_m64.c \
+ src/ec/ec_prime_i15.c \
+ src/ec/ec_prime_i31.c \
+ src/ec/ec_pubkey.c \
+ src/ec/ec_secp256r1.c \
+ src/ec/ec_secp384r1.c \
+ src/ec/ec_secp521r1.c \
+ src/ec/ecdsa_atr.c \
+ src/ec/ecdsa_default_sign_asn1.c \
+ src/ec/ecdsa_default_sign_raw.c \
+ src/ec/ecdsa_default_vrfy_asn1.c \
+ src/ec/ecdsa_default_vrfy_raw.c \
+ src/ec/ecdsa_i15_bits.c \
+ src/ec/ecdsa_i15_sign_asn1.c \
+ src/ec/ecdsa_i15_sign_raw.c \
+ src/ec/ecdsa_i15_vrfy_asn1.c \
+ src/ec/ecdsa_i15_vrfy_raw.c \
+ src/ec/ecdsa_i31_bits.c \
+ src/ec/ecdsa_i31_sign_asn1.c \
+ src/ec/ecdsa_i31_sign_raw.c \
+ src/ec/ecdsa_i31_vrfy_asn1.c \
+ src/ec/ecdsa_i31_vrfy_raw.c \
+ src/ec/ecdsa_rta.c \
+ src/hash/dig_oid.c \
+ src/hash/dig_size.c \
+ src/hash/ghash_ctmul.c \
+ src/hash/ghash_ctmul32.c \
+ src/hash/ghash_ctmul64.c \
+ src/hash/ghash_pclmul.c \
+ src/hash/ghash_pwr8.c \
+ src/hash/md5.c \
+ src/hash/md5sha1.c \
+ src/hash/mgf1.c \
+ src/hash/multihash.c \
+ src/hash/sha1.c \
+ src/hash/sha2big.c \
+ src/hash/sha2small.c \
+ src/int/i15_add.c \
+ src/int/i15_bitlen.c \
+ src/int/i15_decmod.c \
+ src/int/i15_decode.c \
+ src/int/i15_decred.c \
+ src/int/i15_encode.c \
+ src/int/i15_fmont.c \
+ src/int/i15_iszero.c \
+ src/int/i15_moddiv.c \
+ src/int/i15_modpow.c \
+ src/int/i15_modpow2.c \
+ src/int/i15_montmul.c \
+ src/int/i15_mulacc.c \
+ src/int/i15_muladd.c \
+ src/int/i15_ninv15.c \
+ src/int/i15_reduce.c \
+ src/int/i15_rshift.c \
+ src/int/i15_sub.c \
+ src/int/i15_tmont.c \
+ src/int/i31_add.c \
+ src/int/i31_bitlen.c \
+ src/int/i31_decmod.c \
+ src/int/i31_decode.c \
+ src/int/i31_decred.c \
+ src/int/i31_encode.c \
+ src/int/i31_fmont.c \
+ src/int/i31_iszero.c \
+ src/int/i31_moddiv.c \
+ src/int/i31_modpow.c \
+ src/int/i31_modpow2.c \
+ src/int/i31_montmul.c \
+ src/int/i31_mulacc.c \
+ src/int/i31_muladd.c \
+ src/int/i31_ninv31.c \
+ src/int/i31_reduce.c \
+ src/int/i31_rshift.c \
+ src/int/i31_sub.c \
+ src/int/i31_tmont.c \
+ src/int/i32_add.c \
+ src/int/i32_bitlen.c \
+ src/int/i32_decmod.c \
+ src/int/i32_decode.c \
+ src/int/i32_decred.c \
+ src/int/i32_div32.c \
+ src/int/i32_encode.c \
+ src/int/i32_fmont.c \
+ src/int/i32_iszero.c \
+ src/int/i32_modpow.c \
+ src/int/i32_montmul.c \
+ src/int/i32_mulacc.c \
+ src/int/i32_muladd.c \
+ src/int/i32_ninv32.c \
+ src/int/i32_reduce.c \
+ src/int/i32_sub.c \
+ src/int/i32_tmont.c \
+ src/int/i62_modpow2.c \
+ src/kdf/hkdf.c \
+ src/kdf/shake.c \
+ src/mac/hmac.c \
+ src/mac/hmac_ct.c \
+ src/rand/aesctr_drbg.c \
+ src/rand/hmac_drbg.c \
+ src/rand/sysrng.c \
+ src/rsa/rsa_default_keygen.c \
+ src/rsa/rsa_default_modulus.c \
+ src/rsa/rsa_default_oaep_decrypt.c \
+ src/rsa/rsa_default_oaep_encrypt.c \
+ src/rsa/rsa_default_pkcs1_sign.c \
+ src/rsa/rsa_default_pkcs1_vrfy.c \
+ src/rsa/rsa_default_priv.c \
+ src/rsa/rsa_default_privexp.c \
+ src/rsa/rsa_default_pss_sign.c \
+ src/rsa/rsa_default_pss_vrfy.c \
+ src/rsa/rsa_default_pub.c \
+ src/rsa/rsa_default_pubexp.c \
+ src/rsa/rsa_i15_keygen.c \
+ src/rsa/rsa_i15_modulus.c \
+ src/rsa/rsa_i15_oaep_decrypt.c \
+ src/rsa/rsa_i15_oaep_encrypt.c \
+ src/rsa/rsa_i15_pkcs1_sign.c \
+ src/rsa/rsa_i15_pkcs1_vrfy.c \
+ src/rsa/rsa_i15_priv.c \
+ src/rsa/rsa_i15_privexp.c \
+ src/rsa/rsa_i15_pss_sign.c \
+ src/rsa/rsa_i15_pss_vrfy.c \
+ src/rsa/rsa_i15_pub.c \
+ src/rsa/rsa_i15_pubexp.c \
+ src/rsa/rsa_i31_keygen.c \
+ src/rsa/rsa_i31_keygen_inner.c \
+ src/rsa/rsa_i31_modulus.c \
+ src/rsa/rsa_i31_oaep_decrypt.c \
+ src/rsa/rsa_i31_oaep_encrypt.c \
+ src/rsa/rsa_i31_pkcs1_sign.c \
+ src/rsa/rsa_i31_pkcs1_vrfy.c \
+ src/rsa/rsa_i31_priv.c \
+ src/rsa/rsa_i31_privexp.c \
+ src/rsa/rsa_i31_pss_sign.c \
+ src/rsa/rsa_i31_pss_vrfy.c \
+ src/rsa/rsa_i31_pub.c \
+ src/rsa/rsa_i31_pubexp.c \
+ src/rsa/rsa_i32_oaep_decrypt.c \
+ src/rsa/rsa_i32_oaep_encrypt.c \
+ src/rsa/rsa_i32_pkcs1_sign.c \
+ src/rsa/rsa_i32_pkcs1_vrfy.c \
+ src/rsa/rsa_i32_priv.c \
+ src/rsa/rsa_i32_pss_sign.c \
+ src/rsa/rsa_i32_pss_vrfy.c \
+ src/rsa/rsa_i32_pub.c \
+ src/rsa/rsa_i62_keygen.c \
+ src/rsa/rsa_i62_oaep_decrypt.c \
+ src/rsa/rsa_i62_oaep_encrypt.c \
+ src/rsa/rsa_i62_pkcs1_sign.c \
+ src/rsa/rsa_i62_pkcs1_vrfy.c \
+ src/rsa/rsa_i62_priv.c \
+ src/rsa/rsa_i62_pss_sign.c \
+ src/rsa/rsa_i62_pss_vrfy.c \
+ src/rsa/rsa_i62_pub.c \
+ src/rsa/rsa_oaep_pad.c \
+ src/rsa/rsa_oaep_unpad.c \
+ src/rsa/rsa_pkcs1_sig_pad.c \
+ src/rsa/rsa_pkcs1_sig_unpad.c \
+ src/rsa/rsa_pss_sig_pad.c \
+ src/rsa/rsa_pss_sig_unpad.c \
+ src/rsa/rsa_ssl_decrypt.c \
+ src/ssl/prf.c \
+ src/ssl/prf_md5sha1.c \
+ src/ssl/prf_sha256.c \
+ src/ssl/prf_sha384.c \
+ src/ssl/ssl_ccert_single_ec.c \
+ src/ssl/ssl_ccert_single_rsa.c \
+ src/ssl/ssl_client.c \
+ src/ssl/ssl_client_default_rsapub.c \
+ src/ssl/ssl_client_full.c \
+ src/ssl/ssl_engine.c \
+ src/ssl/ssl_engine_default_aescbc.c \
+ src/ssl/ssl_engine_default_aesccm.c \
+ src/ssl/ssl_engine_default_aesgcm.c \
+ src/ssl/ssl_engine_default_chapol.c \
+ src/ssl/ssl_engine_default_descbc.c \
+ src/ssl/ssl_engine_default_ec.c \
+ src/ssl/ssl_engine_default_ecdsa.c \
+ src/ssl/ssl_engine_default_rsavrfy.c \
+ src/ssl/ssl_hashes.c \
+ src/ssl/ssl_hs_client.c \
+ src/ssl/ssl_hs_server.c \
+ src/ssl/ssl_io.c \
+ src/ssl/ssl_keyexport.c \
+ src/ssl/ssl_lru.c \
+ src/ssl/ssl_rec_cbc.c \
+ src/ssl/ssl_rec_ccm.c \
+ src/ssl/ssl_rec_chapol.c \
+ src/ssl/ssl_rec_gcm.c \
+ src/ssl/ssl_scert_single_ec.c \
+ src/ssl/ssl_scert_single_rsa.c \
+ src/ssl/ssl_server.c \
+ src/ssl/ssl_server_full_ec.c \
+ src/ssl/ssl_server_full_rsa.c \
+ src/ssl/ssl_server_mine2c.c \
+ src/ssl/ssl_server_mine2g.c \
+ src/ssl/ssl_server_minf2c.c \
+ src/ssl/ssl_server_minf2g.c \
+ src/ssl/ssl_server_minr2g.c \
+ src/ssl/ssl_server_minu2g.c \
+ src/ssl/ssl_server_minv2g.c \
+ src/symcipher/aes_big_cbcdec.c \
+ src/symcipher/aes_big_cbcenc.c \
+ src/symcipher/aes_big_ctr.c \
+ src/symcipher/aes_big_ctrcbc.c \
+ src/symcipher/aes_big_dec.c \
+ src/symcipher/aes_big_enc.c \
+ src/symcipher/aes_common.c \
+ src/symcipher/aes_ct.c \
+ src/symcipher/aes_ct64.c \
+ src/symcipher/aes_ct64_cbcdec.c \
+ src/symcipher/aes_ct64_cbcenc.c \
+ src/symcipher/aes_ct64_ctr.c \
+ src/symcipher/aes_ct64_ctrcbc.c \
+ src/symcipher/aes_ct64_dec.c \
+ src/symcipher/aes_ct64_enc.c \
+ src/symcipher/aes_ct_cbcdec.c \
+ src/symcipher/aes_ct_cbcenc.c \
+ src/symcipher/aes_ct_ctr.c \
+ src/symcipher/aes_ct_ctrcbc.c \
+ src/symcipher/aes_ct_dec.c \
+ src/symcipher/aes_ct_enc.c \
+ src/symcipher/aes_pwr8.c \
+ src/symcipher/aes_pwr8_cbcdec.c \
+ src/symcipher/aes_pwr8_cbcenc.c \
+ src/symcipher/aes_pwr8_ctr.c \
+ src/symcipher/aes_pwr8_ctrcbc.c \
+ src/symcipher/aes_small_cbcdec.c \
+ src/symcipher/aes_small_cbcenc.c \
+ src/symcipher/aes_small_ctr.c \
+ src/symcipher/aes_small_ctrcbc.c \
+ src/symcipher/aes_small_dec.c \
+ src/symcipher/aes_small_enc.c \
+ src/symcipher/aes_x86ni.c \
+ src/symcipher/aes_x86ni_cbcdec.c \
+ src/symcipher/aes_x86ni_cbcenc.c \
+ src/symcipher/aes_x86ni_ctr.c \
+ src/symcipher/aes_x86ni_ctrcbc.c \
+ src/symcipher/chacha20_ct.c \
+ src/symcipher/chacha20_sse2.c \
+ src/symcipher/des_ct.c \
+ src/symcipher/des_ct_cbcdec.c \
+ src/symcipher/des_ct_cbcenc.c \
+ src/symcipher/des_support.c \
+ src/symcipher/des_tab.c \
+ src/symcipher/des_tab_cbcdec.c \
+ src/symcipher/des_tab_cbcenc.c \
+ src/symcipher/poly1305_ctmul.c \
+ src/symcipher/poly1305_ctmul32.c \
+ src/symcipher/poly1305_ctmulq.c \
+ src/symcipher/poly1305_i15.c \
+ src/x509/asn1enc.c \
+ src/x509/encode_ec_pk8der.c \
+ src/x509/encode_ec_rawder.c \
+ src/x509/encode_rsa_pk8der.c \
+ src/x509/encode_rsa_rawder.c \
+ src/x509/skey_decoder.c \
+ src/x509/x509_decoder.c \
+ src/x509/x509_knownkey.c \
+ src/x509/x509_minimal.c \
+ src/x509/x509_minimal_full.c"
+
+# Source files for the 'brssl' command-line tool.
+toolssrc=" \
+ tools/brssl.c \
+ tools/certs.c \
+ tools/chain.c \
+ tools/client.c \
+ tools/errors.c \
+ tools/files.c \
+ tools/impl.c \
+ tools/keys.c \
+ tools/names.c \
+ tools/server.c \
+ tools/skey.c \
+ tools/sslio.c \
+ tools/ta.c \
+ tools/twrch.c \
+ tools/vector.c \
+ tools/verify.c \
+ tools/xmem.c"
+
+# Source files the the 'testcrypto' command-line tool.
+testcryptosrc=" \
+ test/test_crypto.c"
+
+# Source files the the 'testspeed' command-line tool.
+testspeedsrc=" \
+ test/test_speed.c"
+
+# Source files the the 'testx509' command-line tool.
+testx509src=" \
+ test/test_x509.c"
+
+# Public header files.
+headerspub=" \
+ inc/bearssl.h \
+ inc/bearssl_aead.h \
+ inc/bearssl_block.h \
+ inc/bearssl_ec.h \
+ inc/bearssl_hash.h \
+ inc/bearssl_hmac.h \
+ inc/bearssl_kdf.h \
+ inc/bearssl_pem.h \
+ inc/bearssl_prf.h \
+ inc/bearssl_rand.h \
+ inc/bearssl_rsa.h \
+ inc/bearssl_ssl.h \
+ inc/bearssl_x509.h"
+
+# Private header files.
+headerspriv=" \
+ src/config.h \
+ src/inner.h"
+
+# Header files for the 'brssl' command-line tool.
+headerstools=" \
+ tools/brssl.h"
+
+# T0 compiler source code.
+t0compsrc=" \
+ T0/BlobWriter.cs \
+ T0/CPU.cs \
+ T0/CodeElement.cs \
+ T0/CodeElementJump.cs \
+ T0/CodeElementUInt.cs \
+ T0/CodeElementUIntExpr.cs \
+ T0/CodeElementUIntInt.cs \
+ T0/CodeElementUIntUInt.cs \
+ T0/ConstData.cs \
+ T0/Opcode.cs \
+ T0/OpcodeCall.cs \
+ T0/OpcodeConst.cs \
+ T0/OpcodeGetLocal.cs \
+ T0/OpcodeJump.cs \
+ T0/OpcodeJumpIf.cs \
+ T0/OpcodeJumpIfNot.cs \
+ T0/OpcodeJumpUncond.cs \
+ T0/OpcodePutLocal.cs \
+ T0/OpcodeRet.cs \
+ T0/SType.cs \
+ T0/T0Comp.cs \
+ T0/TPointerBase.cs \
+ T0/TPointerBlob.cs \
+ T0/TPointerExpr.cs \
+ T0/TPointerNull.cs \
+ T0/TPointerXT.cs \
+ T0/TValue.cs \
+ T0/Word.cs \
+ T0/WordBuilder.cs \
+ T0/WordData.cs \
+ T0/WordInterpreted.cs \
+ T0/WordNative.cs"
+
+t0compkern=" \
+ T0/kern.t0"
+
+# Function to turn slashes into $P (macro for path separator).
+escsep() {
+ printf '%s' "$1" | sed 's/\//$P/g'
+}
+
+# Create rules file.
+rm -f Rules.mk
+cat > Rules.mk <<EOF
+# Automatically generated rules. Use 'mkrules.sh' to modify/regenerate.
+EOF
+
+(printf "\nOBJ ="
+for f in $coresrc ; do
+ printf ' \\\n $(OBJDIR)$P%s' "$(basename "$f" .c)\$O"
+done
+printf "\nOBJBRSSL ="
+for f in $toolssrc ; do
+ printf ' \\\n $(OBJDIR)$P%s' "$(basename "$f" .c)\$O"
+done
+printf "\nOBJTESTCRYPTO ="
+for f in $testcryptosrc ; do
+ printf ' \\\n $(OBJDIR)$P%s' "$(basename "$f" .c)\$O"
+done
+printf "\nOBJTESTSPEED ="
+for f in $testspeedsrc ; do
+ printf ' \\\n $(OBJDIR)$P%s' "$(basename "$f" .c)\$O"
+done
+printf "\nOBJTESTX509 ="
+for f in $testx509src ; do
+ printf ' \\\n $(OBJDIR)$P%s' "$(basename "$f" .c)\$O"
+done
+printf "\nHEADERSPUB ="
+for f in $headerspub ; do
+ printf " %s" "$(escsep "$f")"
+done
+printf "\nHEADERSPRIV = %s" '$(HEADERSPUB)'
+for f in $headerspriv ; do
+ printf " %s" "$(escsep "$f")"
+done
+printf "\nHEADERSTOOLS = %s" '$(HEADERSPUB)'
+for f in $headerstools ; do
+ printf " %s" "$(escsep "$f")"
+done
+printf "\nT0SRC ="
+for f in $t0compsrc ; do
+ printf " %s" "$(escsep "$f")"
+done
+printf "\nT0KERN ="
+for f in $t0kernsrc ; do
+ printf " %s" "$(escsep "$f")"
+done
+printf "\n") >> Rules.mk
+
+cat >> Rules.mk <<EOF
+
+all: \$(STATICLIB) \$(DLL) \$(TOOLS) \$(TESTS)
+
+no:
+
+lib: \$(BEARSSLLIB)
+
+dll: \$(BEARSSLDLL)
+
+tools: \$(BRSSL)
+
+tests: \$(TESTCRYPTO) \$(TESTSPEED) \$(TESTX509)
+
+T0: kT0
+
+kT0: \$(T0COMP) src\$Pssl\$Pssl_hs_common.t0 src\$Pssl\$Pssl_hs_client.t0 src\$Pssl\$Pssl_hs_server.t0 src\$Px509\$Pasn1.t0 src\$Px509\$Pskey_decoder.t0 src\$Px509\$Px509_decoder.t0 src\$Px509\$Px509_minimal.t0
+ \$(RUNT0COMP) -o src\$Pcodec\$Ppemdec -r br_pem_decoder src\$Pcodec\$Ppemdec.t0
+ \$(RUNT0COMP) -o src\$Pssl\$Pssl_hs_client -r br_ssl_hs_client src\$Pssl\$Pssl_hs_common.t0 src\$Pssl\$Pssl_hs_client.t0
+ \$(RUNT0COMP) -o src\$Pssl\$Pssl_hs_server -r br_ssl_hs_server src\$Pssl\$Pssl_hs_common.t0 src\$Pssl\$Pssl_hs_server.t0
+ \$(RUNT0COMP) -o src\$Px509\$Pskey_decoder -r br_skey_decoder src\$Px509\$Pasn1.t0 src\$Px509\$Pskey_decoder.t0
+ \$(RUNT0COMP) -o src\$Px509\$Px509_decoder -r br_x509_decoder src\$Px509\$Pasn1.t0 src\$Px509\$Px509_decoder.t0
+ \$(RUNT0COMP) -o src\$Px509\$Px509_minimal -r br_x509_minimal src\$Px509\$Pasn1.t0 src\$Px509\$Px509_minimal.t0
+
+\$(T0COMP): \$(T0SRC) \$(T0KERN)
+ \$(MKT0COMP)
+
+clean:
+ -\$(RM) \$(OBJDIR)\$P*\$O
+ -\$(RM) \$(BEARSSLLIB) \$(BEARSSLDLL) \$(BRSSL) \$(TESTCRYPTO) \$(TESTSPEED) \$(TESTX509)
+
+\$(OBJDIR):
+ -\$(MKDIR) \$(OBJDIR)
+
+\$(BEARSSLLIB): \$(OBJDIR) \$(OBJ)
+ \$(AR) \$(ARFLAGS) \$(AROUT)\$(BEARSSLLIB) \$(OBJ)
+
+\$(BEARSSLDLL): \$(OBJDIR) \$(OBJ)
+ \$(LDDLL) \$(LDDLLFLAGS) \$(LDDLLOUT)\$(BEARSSLDLL) \$(OBJ)
+
+\$(BRSSL): \$(BEARSSLLIB) \$(OBJBRSSL)
+ \$(LD) \$(LDFLAGS) \$(LDOUT)\$(BRSSL) \$(OBJBRSSL) \$(BEARSSLLIB)
+
+\$(TESTCRYPTO): \$(BEARSSLLIB) \$(OBJTESTCRYPTO)
+ \$(LD) \$(LDFLAGS) \$(LDOUT)\$(TESTCRYPTO) \$(OBJTESTCRYPTO) \$(BEARSSLLIB)
+
+\$(TESTSPEED): \$(BEARSSLLIB) \$(OBJTESTSPEED)
+ \$(LD) \$(LDFLAGS) \$(LDOUT)\$(TESTSPEED) \$(OBJTESTSPEED) \$(BEARSSLLIB)
+
+\$(TESTX509): \$(BEARSSLLIB) \$(OBJTESTX509)
+ \$(LD) \$(LDFLAGS) \$(LDOUT)\$(TESTX509) \$(OBJTESTX509) \$(BEARSSLLIB)
+EOF
+
+(for f in $coresrc ; do
+ b="$(basename "$f" .c)\$O"
+ g="$(escsep "$f")"
+ printf '\n$(OBJDIR)$P%s: %s $(HEADERSPRIV)\n\t$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$P%s %s\n' "$b" "$g" "$b" "$g"
+done
+
+for f in $toolssrc ; do
+ b="$(basename "$f" .c)\$O"
+ g="$(escsep "$f")"
+ printf '\n$(OBJDIR)$P%s: %s $(HEADERSTOOLS)\n\t$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$P%s %s\n' "$b" "$g" "$b" "$g"
+done
+
+for f in $testcryptosrc $testspeedsrc ; do
+ b="$(basename "$f" .c)\$O"
+ g="$(escsep "$f")"
+ printf '\n$(OBJDIR)$P%s: %s $(HEADERSPRIV)\n\t$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$P%s %s\n' "$b" "$g" "$b" "$g"
+done
+
+for f in $testx509src ; do
+ b="$(basename "$f" .c)\$O"
+ g="$(escsep "$f")"
+ printf '\n$(OBJDIR)$P%s: %s $(HEADERSPRIV)\n\t$(CC) $(CFLAGS) $(INCFLAGS) -DSRCDIRNAME=".." $(CCOUT)$(OBJDIR)$P%s %s\n' "$b" "$g" "$b" "$g"
+done) >> Rules.mk
diff --git a/test/monniaux/BearSSL/samples/README.txt b/test/monniaux/BearSSL/samples/README.txt
new file mode 100644
index 00000000..77c93c79
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/README.txt
@@ -0,0 +1,36 @@
+This directory contains sample code for using BearSSL.
+
+client_basic.c
+
+ A sample client code, that connects to a server, performs a SSL
+ handshake, sends a basic HTTP GET request, and dumps the complete
+ answer on stdout.
+
+ Compile it against BearSSL headers (in the ../inc directory) and
+ library (libbearssl.a). This code will validate the server
+ certificate against two hardcoded trust anchors.
+
+server_basic.c
+
+ A sample SSL server, that serves one client at a time. It reads a
+ single HTTP request (that it does not really parse; it just waits for
+ the two successive line endings that mark the end of the request),
+ and pushes a basic response.
+
+ Compile it against BearSSL headers (in the ../inc directory) and
+ library (libbearssl.a). Depending on compilation options (see the
+ code), it will use one of several certificate chains, that exercise
+ various combinations of RSA and EC keys and signatures. These
+ certificate chains link to the trust anchors that are hardcoded
+ in client_basic.c, so the sample client and the sample server can
+ be tested against each other.
+
+custom_profile.c
+
+ A sample C source file that shows how to write your own client or
+ server profiles (selections of cipher suites and algorithms).
+
+
+The .pem files are certificate and keys corresponding to the chains
+and anchors used by the sample client and server. They are provided
+for reference only; these files are not used by the examples.
diff --git a/test/monniaux/BearSSL/samples/cert-ee-ec+rsa.pem b/test/monniaux/BearSSL/samples/cert-ee-ec+rsa.pem
new file mode 100644
index 00000000..07f5457a
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-ee-ec+rsa.pem
@@ -0,0 +1,14 @@
+-----BEGIN CERTIFICATE-----
+MIICcTCCAVmgAwIBAgIUbmO7Sc5BfgOM9Ubyiq5hCDWwlLMwDQYJKoZIhvcNAQELBQAwJzELMAkG
+A1UEBhMCQ0ExGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAeFw0xMDAxMDEwMDAwMDBaFw0zNzEy
+MzEyMzU5NTlaMCExCzAJBgNVBAYTAkNBMRIwEAYDVQQDEwlsb2NhbGhvc3QwWTATBgcqhkjOPQIB
+BggqhkjOPQMBBwNCAARfOJ2n/02Kr/Y0OUYa/Drf9COqqer7xQjeAI6+eaU3WExt3QHKq0ffibbH
+Fx84/B0gFN1FwOCPk044C/zpmaFJo2YwZDAfBgNVHSMEGDAWgBR8z6PGKffzxaoZ0MAW6+BAD85E
+pzAdBgNVHQ4EFgQUww6GqnW0FcDllQkyvl6SdankRJswDAYDVR0TAQH/BAIwADAUBgNVHREEDTAL
+gglsb2NhbGhvc3QwDQYJKoZIhvcNAQELBQADggEBAI6JY6ebqBCOnhjQpHrdLIIWjwsO1cpXu19v
+/FcowCG6rZSoF0JF1zH3hFcZRiQ8x0k4P0h6CRrrUbrFdY5MsiWwxyI+5+VG27E2/BuFUbDtgxbw
+OnnaXShq7mogTLBwXrDtem0tGsm0ccLEox0lhjBUsZgmwVHg+DGtZ0id5qFSOyBHyXDagLWk9D9y
+azcwVzksRptE8dlOu6Zf45rFf2y2Zcu/QHKS0Gj2rnl+JMFbaDAoU3FhevQ2ezvCtuwf3DBABA3q
+swrPddO9nqtxcWh/pUFSAOmzruQe8btpxjvV3tLCJWkIPDfM94Jq5ah7agLavaQBMzPz3kYA7HXP
+4H0=
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-ee-ec.pem b/test/monniaux/BearSSL/samples/cert-ee-ec.pem
new file mode 100644
index 00000000..ff8ddbc3
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-ee-ec.pem
@@ -0,0 +1,10 @@
+-----BEGIN CERTIFICATE-----
+MIIBsDCCAVagAwIBAgIUHE0AkWniRqyQfGRcU/H/t8HLbnowCgYIKoZIzj0EAwIwJzELMAkGA1UE
+BhMCQ0ExGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAeFw0xMDAxMDEwMDAwMDBaFw0zNzEyMzEy
+MzU5NTlaMCExCzAJBgNVBAYTAkNBMRIwEAYDVQQDEwlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggq
+hkjOPQMBBwNCAARfOJ2n/02Kr/Y0OUYa/Drf9COqqer7xQjeAI6+eaU3WExt3QHKq0ffibbHFx84
+/B0gFN1FwOCPk044C/zpmaFJo2YwZDAfBgNVHSMEGDAWgBTw0PEi+XpIFwZ7Pb249c1VnFw+cDAd
+BgNVHQ4EFgQUww6GqnW0FcDllQkyvl6SdankRJswDAYDVR0TAQH/BAIwADAUBgNVHREEDTALggls
+b2NhbGhvc3QwCgYIKoZIzj0EAwIDSAAwRQIhAJH79ATQ5S4B1IzwF2IP3MyAyhjEQHwnA8s0Aw2b
+yFlNAiAFVWni2KFAMzQOfkkyZB0/ax/QLbcvUgRWr9M3j4eZog==
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-ee-rsa.pem b/test/monniaux/BearSSL/samples/cert-ee-rsa.pem
new file mode 100644
index 00000000..ef33e405
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-ee-rsa.pem
@@ -0,0 +1,17 @@
+-----BEGIN CERTIFICATE-----
+MIIDPDCCAiSgAwIBAgIUWNq6Ns3toNpcEDNzjgxkknmSrwMwDQYJKoZIhvcNAQELBQAwJzELMAkG
+A1UEBhMCQ0ExGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAeFw0xMDAxMDEwMDAwMDBaFw0zNzEy
+MzEyMzU5NTlaMCExCzAJBgNVBAYTAkNBMRIwEAYDVQQDEwlsb2NhbGhvc3QwggEiMA0GCSqGSIb3
+DQEBAQUAA4IBDwAwggEKAoIBAQDUeh0nuis6Z7KRavvng0TK7Rx1rd1Ng2LWqmiVsiQhexWuKplo
+Fe1m8LhY59P1LsbZKl7nDi7n/GdZwMhhfUukb92f2ciFh2THuhoPKdSWqHiaa2IgqTLQ7qmMKGFH
+olAqY/Yh3trY1fB/xQCCcOajv1yJJ09RkncDw7DMLjvsI/IvU0GviZP/0oCxQ5fe1hmgkhJ6PWZ5
+4cG84Xdwoos9RoRTP+ROQkE3kh4f/Tiz9++HOYDTVs/04BPeZLBypAOExEHtb/o+4soEINLX3CyC
+K3ribaEcSNvPiU80lz0oqFPa58HhcxWjMHZ/jyNCFD1RNNJarTyby8j+f26OQPO9AgMBAAGjZjBk
+MB8GA1UdIwQYMBaAFMUBrXzmY8mcF1/FoqfhUF/o9ajGMB0GA1UdDgQWBBTFAa185mPJnBdfxaKn
+4VBf6PWoxjAMBgNVHRMBAf8EAjAAMBQGA1UdEQQNMAuCCWxvY2FsaG9zdDANBgkqhkiG9w0BAQsF
+AAOCAQEAcbNdIcIO19DG+Epzh00iAifQx/j9Gm1iWIIIdiAHwEiS8+mYWusNTlaVY2hNq9QAduA3
+zwsRYVlc3valFFnZJZ9Z2dNehqwdpiwyQhkyE0ALVM1nJra9tJakyh9/N9aodes6gVEwuflKAW/R
+1u1P3z8wYAZnko5hhV8atYyzD2Gp+t9dxGQA6oexM199y6OFJG4sZTvqcz+G0/3o5ALGYWomF1IB
+JVx/qM5pH6xhLLcEr/2kepnLJhVM/3TUcwxXDCbr1yrcXMNBu8Lzzha9jnv76d+rIQ2Rs43Yz8j0
+SbnQ4xZwP7Pe1Acl+kZEUolNicjiyrUzf8chvSjv/mZ0Aw==
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-ica-ec.pem b/test/monniaux/BearSSL/samples/cert-ica-ec.pem
new file mode 100644
index 00000000..204ab76c
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-ica-ec.pem
@@ -0,0 +1,10 @@
+-----BEGIN CERTIFICATE-----
+MIIBqTCCAU6gAwIBAgIUINPr4oz+2uajLF478mY6KzZ7sMowCgYIKoZIzj0EAwIwHDELMAkGA1UE
+BhMCQ0ExDTALBgNVBAMTBFJvb3QwHhcNMTAwMTAxMDAwMDAwWhcNMzcxMjMxMjM1OTU5WjAnMQsw
+CQYDVQQGEwJDQTEYMBYGA1UEAxMPSW50ZXJtZWRpYXRlIENBMFkwEwYHKoZIzj0CAQYIKoZIzj0D
+AQcDQgAEcC6SggEXbG2r4dFjCUhJ0qY1UtM8c7uyiDeYh/GN4Oxlmg4T9e2RYci2bTOEbq6OVYDN
+SZ4Hv9CunebQsycWoaNjMGEwHwYDVR0jBBgwFoAUlUG04meq8X+8j3nzaBRaa5IWokAwHQYDVR0O
+BBYEFPDQ8SL5ekgXBns9vbj1zVWcXD5wMA4GA1UdDwEB/wQEAwIAhjAPBgNVHRMBAf8EBTADAQH/
+MAoGCCqGSM49BAMCA0kAMEYCIQCF40ZomdYCellmHLdPNS0INjhhfgVI2GlDH+tW6a0GDgIhAIJw
+tGIDSUbIVFkF2XjbUxzgbmb1DxQ7yS04EnCRVvmp
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-ica-rsa.pem b/test/monniaux/BearSSL/samples/cert-ica-rsa.pem
new file mode 100644
index 00000000..058ffab4
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-ica-rsa.pem
@@ -0,0 +1,17 @@
+-----BEGIN CERTIFICATE-----
+MIIDNDCCAhygAwIBAgIUcA9g7vAHmpxprJdiJk9dBbb5j0gwDQYJKoZIhvcNAQELBQAwHDELMAkG
+A1UEBhMCQ0ExDTALBgNVBAMTBFJvb3QwHhcNMTAwMTAxMDAwMDAwWhcNMzcxMjMxMjM1OTU5WjAn
+MQswCQYDVQQGEwJDQTEYMBYGA1UEAxMPSW50ZXJtZWRpYXRlIENBMIIBIjANBgkqhkiG9w0BAQEF
+AAOCAQ8AMIIBCgKCAQEAs+hrr5wWUuOBDFCrJc7MDcfyH39Q3yxcNdZiLmMnQafkU6hLJ/oTkaP6
+CUovO17Pd7OKwc1JlZx1DWR07+TXS7mhm2jSMHFI6vdLFN8/R6nYu+yPKMz637QflHyW/AgFKPno
+9C8v7mKcijrghVhgtg8tMLTAQVSRTB9frfEZ8MAipn3YP3k0WUJ7W7VBxGR/Us88NyKhL3kllCRB
+wj/6x3X7SLUNGKf0VPMubthDWMSrUOgFrZG2HgF1s1Sc3qCZFfus8VyXSVHM71gSb3NrszQUAQ9a
+nfqq1pPT4urDq7xO7cxRobj4lLa0LKiGKx/2UUMpUl4TibNqeGBOTsAbpQIDAQABo2MwYTAfBgNV
+HSMEGDAWgBTDCry0kGOWkkW8J6DwWIkq1XgAEjAdBgNVHQ4EFgQUfM+jxin388WqGdDAFuvgQA/O
+RKcwDgYDVR0PAQH/BAQDAgCGMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAFQ7
+9OrG5OjAWxKyrfq9qfRiA61XTG8Hp0c1dT5IoltxEAGPk5mdp0fjjj6vLboG/tTkl7wQjaalOjzm
+Ics72hPjSiPrvLqlkJGtVW7V3YVLayfSOXYGLtQjW7tVtUk/fS8hy5Z1GZmpmfELuz7HEKeLelK5
+SeQUCHjnPdmYV9r/2rmNZnWAtV2532ll2xbnHsRA5EaKHnYyFueDZ9p4VqsPTFzxcNpmIPT4D/bc
+L3KXa3hAeZ1bbb4DznBCqCpxEd8ugQHqhhKRT9AY7YSkSDC5uXtWPu+N4R/9kLJEhVhvpzB0fPGu
+jJk/8U1XxZVowjay7MJoesCBqVUF58+vUKw=
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-root-ec.pem b/test/monniaux/BearSSL/samples/cert-root-ec.pem
new file mode 100644
index 00000000..1bfd5786
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-root-ec.pem
@@ -0,0 +1,9 @@
+-----BEGIN CERTIFICATE-----
+MIIBijCCATCgAwIBAgIBCTAKBggqhkjOPQQDAjAcMQswCQYDVQQGEwJDQTENMAsGA1UEAxMEUm9v
+dDAeFw0xMDAxMDEwMDAwMDBaFw0zNzEyMzEyMzU5NTlaMBwxCzAJBgNVBAYTAkNBMQ0wCwYDVQQD
+EwRSb290MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEcXS6q7kwLoHV5Vf58yBoDJz5ZNu0IA1t
+6kDQSm5C/baaaCVE9t97xPze3Xu7xdt8dj9BZkBu26eHwuXYxfN/jaNjMGEwHwYDVR0jBBgwFoAU
+lUG04meq8X+8j3nzaBRaa5IWokAwHQYDVR0OBBYEFJVBtOJnqvF/vI9582gUWmuSFqJAMA4GA1Ud
+DwEB/wQEAwIAhjAPBgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0gAMEUCIQCz1GCIAoRtqU2h
+x62hec7E+/XxnUGDORWnkliiGrcmxQIgNPq9NHD7ts0xYjTwZsd0bN62+iY93lM4LHbkNV9q/gA=
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/cert-root-rsa.pem b/test/monniaux/BearSSL/samples/cert-root-rsa.pem
new file mode 100644
index 00000000..6b5ebd09
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/cert-root-rsa.pem
@@ -0,0 +1,16 @@
+-----BEGIN CERTIFICATE-----
+MIIDFjCCAf6gAwIBAgIBCDANBgkqhkiG9w0BAQsFADAcMQswCQYDVQQGEwJDQTENMAsGA1UEAxME
+Um9vdDAeFw0xMDAxMDEwMDAwMDBaFw0zNzEyMzEyMzU5NTlaMBwxCzAJBgNVBAYTAkNBMQ0wCwYD
+VQQDEwRSb290MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAttk01FD9s696c/HOOL9d
+b0Xh/U6xmMZggybSF9HFt5qjwd5jOZec8F5cyBwXuYgZbfC2LjBQoVRuk8DbzzDLnx4nefHDmVI1
+qj2237CtfMtJzcDt52YQKunOKB8hUPp3TC3a7zxY606/zun7Gtqjg6PNo8qTgNza8xfMeqszgJyy
+1H9GP8U83GGUtycpbiq8Wwk21MY7Deu+ztsdHLwQanFxs/LKKJp38orsQu+xSo7i8hoyKs3ApkYs
+msKFN5F/RqGTgaF0Zt+6szkgkZP6HaGohefk+Qf2EPaoJwG2fxLDQMPJ4rCrSRg6ZLZZt5W1ljbf
+ImmqcmpUTicpow6XFQIDAQABo2MwYTAfBgNVHSMEGDAWgBTDCry0kGOWkkW8J6DwWIkq1XgAEjAd
+BgNVHQ4EFgQUwwq8tJBjlpJFvCeg8FiJKtV4ABIwDgYDVR0PAQH/BAQDAgCGMA8GA1UdEwEB/wQF
+MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAA7SaR2V1Gx71RiL5Cdfr15jkU88snnm+v+0PvIfQrNP
+4RzKJkaD3j//oZf6Hr2yH6c7Bi3OhCxBFNbXjDXiAXVRoTjOSxNSwVq43utl9Z4IKAZzQlEYW5S5
+U0oskEq2q6jDEWoj8lUtQW4GAVuZhq7lNs0jJJu84IBsxHJFgZJ/7+LHFLXs+vQkz3hjzMZ7gIt+
+lwXI/gdY9ld1QB6WLLIzxs7zid+Z0LZTYi851vbmMwUqgL8V5Np0Q0EVHHy1c6LQ0hj1kVLay2ZN
+d2dsUMCQJI5EF2kWon+xFDpAtv0vUT2xuMkYFhjS/aBxzevWCsXuUQphBYaIGli8P68NNPo=
+-----END CERTIFICATE-----
diff --git a/test/monniaux/BearSSL/samples/chain-ec+rsa.h b/test/monniaux/BearSSL/samples/chain-ec+rsa.h
new file mode 100644
index 00000000..2a27ff5c
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/chain-ec+rsa.h
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * A sample server certificate chain with a single intermediate CA.
+ * Certificate key type: EC
+ * Signing algorithm for both certificates: RSA
+ */
+
+static const unsigned char CERT0[] = {
+ 0x30, 0x82, 0x02, 0x71, 0x30, 0x82, 0x01, 0x59, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x6E, 0x63, 0xBB, 0x49, 0xCE, 0x41, 0x7E, 0x03, 0x8C,
+ 0xF5, 0x46, 0xF2, 0x8A, 0xAE, 0x61, 0x08, 0x35, 0xB0, 0x94, 0xB3, 0x30,
+ 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
+ 0x05, 0x00, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
+ 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55,
+ 0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D, 0x65, 0x64,
+ 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x31,
+ 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A,
+ 0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39,
+ 0x35, 0x39, 0x5A, 0x30, 0x21, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
+ 0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03,
+ 0x55, 0x04, 0x03, 0x13, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F,
+ 0x73, 0x74, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
+ 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01,
+ 0x07, 0x03, 0x42, 0x00, 0x04, 0x5F, 0x38, 0x9D, 0xA7, 0xFF, 0x4D, 0x8A,
+ 0xAF, 0xF6, 0x34, 0x39, 0x46, 0x1A, 0xFC, 0x3A, 0xDF, 0xF4, 0x23, 0xAA,
+ 0xA9, 0xEA, 0xFB, 0xC5, 0x08, 0xDE, 0x00, 0x8E, 0xBE, 0x79, 0xA5, 0x37,
+ 0x58, 0x4C, 0x6D, 0xDD, 0x01, 0xCA, 0xAB, 0x47, 0xDF, 0x89, 0xB6, 0xC7,
+ 0x17, 0x1F, 0x38, 0xFC, 0x1D, 0x20, 0x14, 0xDD, 0x45, 0xC0, 0xE0, 0x8F,
+ 0x93, 0x4E, 0x38, 0x0B, 0xFC, 0xE9, 0x99, 0xA1, 0x49, 0xA3, 0x66, 0x30,
+ 0x64, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16,
+ 0x80, 0x14, 0x7C, 0xCF, 0xA3, 0xC6, 0x29, 0xF7, 0xF3, 0xC5, 0xAA, 0x19,
+ 0xD0, 0xC0, 0x16, 0xEB, 0xE0, 0x40, 0x0F, 0xCE, 0x44, 0xA7, 0x30, 0x1D,
+ 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xC3, 0x0E, 0x86,
+ 0xAA, 0x75, 0xB4, 0x15, 0xC0, 0xE5, 0x95, 0x09, 0x32, 0xBE, 0x5E, 0x92,
+ 0x75, 0xA9, 0xE4, 0x44, 0x9B, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13,
+ 0x01, 0x01, 0xFF, 0x04, 0x02, 0x30, 0x00, 0x30, 0x14, 0x06, 0x03, 0x55,
+ 0x1D, 0x11, 0x04, 0x0D, 0x30, 0x0B, 0x82, 0x09, 0x6C, 0x6F, 0x63, 0x61,
+ 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48,
+ 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01,
+ 0x00, 0x8E, 0x89, 0x63, 0xA7, 0x9B, 0xA8, 0x10, 0x8E, 0x9E, 0x18, 0xD0,
+ 0xA4, 0x7A, 0xDD, 0x2C, 0x82, 0x16, 0x8F, 0x0B, 0x0E, 0xD5, 0xCA, 0x57,
+ 0xBB, 0x5F, 0x6F, 0xFC, 0x57, 0x28, 0xC0, 0x21, 0xBA, 0xAD, 0x94, 0xA8,
+ 0x17, 0x42, 0x45, 0xD7, 0x31, 0xF7, 0x84, 0x57, 0x19, 0x46, 0x24, 0x3C,
+ 0xC7, 0x49, 0x38, 0x3F, 0x48, 0x7A, 0x09, 0x1A, 0xEB, 0x51, 0xBA, 0xC5,
+ 0x75, 0x8E, 0x4C, 0xB2, 0x25, 0xB0, 0xC7, 0x22, 0x3E, 0xE7, 0xE5, 0x46,
+ 0xDB, 0xB1, 0x36, 0xFC, 0x1B, 0x85, 0x51, 0xB0, 0xED, 0x83, 0x16, 0xF0,
+ 0x3A, 0x79, 0xDA, 0x5D, 0x28, 0x6A, 0xEE, 0x6A, 0x20, 0x4C, 0xB0, 0x70,
+ 0x5E, 0xB0, 0xED, 0x7A, 0x6D, 0x2D, 0x1A, 0xC9, 0xB4, 0x71, 0xC2, 0xC4,
+ 0xA3, 0x1D, 0x25, 0x86, 0x30, 0x54, 0xB1, 0x98, 0x26, 0xC1, 0x51, 0xE0,
+ 0xF8, 0x31, 0xAD, 0x67, 0x48, 0x9D, 0xE6, 0xA1, 0x52, 0x3B, 0x20, 0x47,
+ 0xC9, 0x70, 0xDA, 0x80, 0xB5, 0xA4, 0xF4, 0x3F, 0x72, 0x6B, 0x37, 0x30,
+ 0x57, 0x39, 0x2C, 0x46, 0x9B, 0x44, 0xF1, 0xD9, 0x4E, 0xBB, 0xA6, 0x5F,
+ 0xE3, 0x9A, 0xC5, 0x7F, 0x6C, 0xB6, 0x65, 0xCB, 0xBF, 0x40, 0x72, 0x92,
+ 0xD0, 0x68, 0xF6, 0xAE, 0x79, 0x7E, 0x24, 0xC1, 0x5B, 0x68, 0x30, 0x28,
+ 0x53, 0x71, 0x61, 0x7A, 0xF4, 0x36, 0x7B, 0x3B, 0xC2, 0xB6, 0xEC, 0x1F,
+ 0xDC, 0x30, 0x40, 0x04, 0x0D, 0xEA, 0xB3, 0x0A, 0xCF, 0x75, 0xD3, 0xBD,
+ 0x9E, 0xAB, 0x71, 0x71, 0x68, 0x7F, 0xA5, 0x41, 0x52, 0x00, 0xE9, 0xB3,
+ 0xAE, 0xE4, 0x1E, 0xF1, 0xBB, 0x69, 0xC6, 0x3B, 0xD5, 0xDE, 0xD2, 0xC2,
+ 0x25, 0x69, 0x08, 0x3C, 0x37, 0xCC, 0xF7, 0x82, 0x6A, 0xE5, 0xA8, 0x7B,
+ 0x6A, 0x02, 0xDA, 0xBD, 0xA4, 0x01, 0x33, 0x33, 0xF3, 0xDE, 0x46, 0x00,
+ 0xEC, 0x75, 0xCF, 0xE0, 0x7D
+};
+
+static const unsigned char CERT1[] = {
+ 0x30, 0x82, 0x03, 0x34, 0x30, 0x82, 0x02, 0x1C, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x70, 0x0F, 0x60, 0xEE, 0xF0, 0x07, 0x9A, 0x9C, 0x69,
+ 0xAC, 0x97, 0x62, 0x26, 0x4F, 0x5D, 0x05, 0xB6, 0xF9, 0x8F, 0x48, 0x30,
+ 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
+ 0x05, 0x00, 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
+ 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
+ 0x04, 0x03, 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x1E, 0x17, 0x0D,
+ 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x5A, 0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35,
+ 0x39, 0x35, 0x39, 0x5A, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
+ 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06,
+ 0x03, 0x55, 0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D,
+ 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01,
+ 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
+ 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01,
+ 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xB3, 0xE8, 0x6B, 0xAF, 0x9C, 0x16,
+ 0x52, 0xE3, 0x81, 0x0C, 0x50, 0xAB, 0x25, 0xCE, 0xCC, 0x0D, 0xC7, 0xF2,
+ 0x1F, 0x7F, 0x50, 0xDF, 0x2C, 0x5C, 0x35, 0xD6, 0x62, 0x2E, 0x63, 0x27,
+ 0x41, 0xA7, 0xE4, 0x53, 0xA8, 0x4B, 0x27, 0xFA, 0x13, 0x91, 0xA3, 0xFA,
+ 0x09, 0x4A, 0x2F, 0x3B, 0x5E, 0xCF, 0x77, 0xB3, 0x8A, 0xC1, 0xCD, 0x49,
+ 0x95, 0x9C, 0x75, 0x0D, 0x64, 0x74, 0xEF, 0xE4, 0xD7, 0x4B, 0xB9, 0xA1,
+ 0x9B, 0x68, 0xD2, 0x30, 0x71, 0x48, 0xEA, 0xF7, 0x4B, 0x14, 0xDF, 0x3F,
+ 0x47, 0xA9, 0xD8, 0xBB, 0xEC, 0x8F, 0x28, 0xCC, 0xFA, 0xDF, 0xB4, 0x1F,
+ 0x94, 0x7C, 0x96, 0xFC, 0x08, 0x05, 0x28, 0xF9, 0xE8, 0xF4, 0x2F, 0x2F,
+ 0xEE, 0x62, 0x9C, 0x8A, 0x3A, 0xE0, 0x85, 0x58, 0x60, 0xB6, 0x0F, 0x2D,
+ 0x30, 0xB4, 0xC0, 0x41, 0x54, 0x91, 0x4C, 0x1F, 0x5F, 0xAD, 0xF1, 0x19,
+ 0xF0, 0xC0, 0x22, 0xA6, 0x7D, 0xD8, 0x3F, 0x79, 0x34, 0x59, 0x42, 0x7B,
+ 0x5B, 0xB5, 0x41, 0xC4, 0x64, 0x7F, 0x52, 0xCF, 0x3C, 0x37, 0x22, 0xA1,
+ 0x2F, 0x79, 0x25, 0x94, 0x24, 0x41, 0xC2, 0x3F, 0xFA, 0xC7, 0x75, 0xFB,
+ 0x48, 0xB5, 0x0D, 0x18, 0xA7, 0xF4, 0x54, 0xF3, 0x2E, 0x6E, 0xD8, 0x43,
+ 0x58, 0xC4, 0xAB, 0x50, 0xE8, 0x05, 0xAD, 0x91, 0xB6, 0x1E, 0x01, 0x75,
+ 0xB3, 0x54, 0x9C, 0xDE, 0xA0, 0x99, 0x15, 0xFB, 0xAC, 0xF1, 0x5C, 0x97,
+ 0x49, 0x51, 0xCC, 0xEF, 0x58, 0x12, 0x6F, 0x73, 0x6B, 0xB3, 0x34, 0x14,
+ 0x01, 0x0F, 0x5A, 0x9D, 0xFA, 0xAA, 0xD6, 0x93, 0xD3, 0xE2, 0xEA, 0xC3,
+ 0xAB, 0xBC, 0x4E, 0xED, 0xCC, 0x51, 0xA1, 0xB8, 0xF8, 0x94, 0xB6, 0xB4,
+ 0x2C, 0xA8, 0x86, 0x2B, 0x1F, 0xF6, 0x51, 0x43, 0x29, 0x52, 0x5E, 0x13,
+ 0x89, 0xB3, 0x6A, 0x78, 0x60, 0x4E, 0x4E, 0xC0, 0x1B, 0xA5, 0x02, 0x03,
+ 0x01, 0x00, 0x01, 0xA3, 0x63, 0x30, 0x61, 0x30, 0x1F, 0x06, 0x03, 0x55,
+ 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xC3, 0x0A, 0xBC, 0xB4,
+ 0x90, 0x63, 0x96, 0x92, 0x45, 0xBC, 0x27, 0xA0, 0xF0, 0x58, 0x89, 0x2A,
+ 0xD5, 0x78, 0x00, 0x12, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04,
+ 0x16, 0x04, 0x14, 0x7C, 0xCF, 0xA3, 0xC6, 0x29, 0xF7, 0xF3, 0xC5, 0xAA,
+ 0x19, 0xD0, 0xC0, 0x16, 0xEB, 0xE0, 0x40, 0x0F, 0xCE, 0x44, 0xA7, 0x30,
+ 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04, 0x04, 0x03,
+ 0x02, 0x00, 0x86, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01,
+ 0xFF, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30, 0x0D, 0x06, 0x09,
+ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03,
+ 0x82, 0x01, 0x01, 0x00, 0x54, 0x3B, 0xF4, 0xEA, 0xC6, 0xE4, 0xE8, 0xC0,
+ 0x5B, 0x12, 0xB2, 0xAD, 0xFA, 0xBD, 0xA9, 0xF4, 0x62, 0x03, 0xAD, 0x57,
+ 0x4C, 0x6F, 0x07, 0xA7, 0x47, 0x35, 0x75, 0x3E, 0x48, 0xA2, 0x5B, 0x71,
+ 0x10, 0x01, 0x8F, 0x93, 0x99, 0x9D, 0xA7, 0x47, 0xE3, 0x8E, 0x3E, 0xAF,
+ 0x2D, 0xBA, 0x06, 0xFE, 0xD4, 0xE4, 0x97, 0xBC, 0x10, 0x8D, 0xA6, 0xA5,
+ 0x3A, 0x3C, 0xE6, 0x21, 0xCB, 0x3B, 0xDA, 0x13, 0xE3, 0x4A, 0x23, 0xEB,
+ 0xBC, 0xBA, 0xA5, 0x90, 0x91, 0xAD, 0x55, 0x6E, 0xD5, 0xDD, 0x85, 0x4B,
+ 0x6B, 0x27, 0xD2, 0x39, 0x76, 0x06, 0x2E, 0xD4, 0x23, 0x5B, 0xBB, 0x55,
+ 0xB5, 0x49, 0x3F, 0x7D, 0x2F, 0x21, 0xCB, 0x96, 0x75, 0x19, 0x99, 0xA9,
+ 0x99, 0xF1, 0x0B, 0xBB, 0x3E, 0xC7, 0x10, 0xA7, 0x8B, 0x7A, 0x52, 0xB9,
+ 0x49, 0xE4, 0x14, 0x08, 0x78, 0xE7, 0x3D, 0xD9, 0x98, 0x57, 0xDA, 0xFF,
+ 0xDA, 0xB9, 0x8D, 0x66, 0x75, 0x80, 0xB5, 0x5D, 0xB9, 0xDF, 0x69, 0x65,
+ 0xDB, 0x16, 0xE7, 0x1E, 0xC4, 0x40, 0xE4, 0x46, 0x8A, 0x1E, 0x76, 0x32,
+ 0x16, 0xE7, 0x83, 0x67, 0xDA, 0x78, 0x56, 0xAB, 0x0F, 0x4C, 0x5C, 0xF1,
+ 0x70, 0xDA, 0x66, 0x20, 0xF4, 0xF8, 0x0F, 0xF6, 0xDC, 0x2F, 0x72, 0x97,
+ 0x6B, 0x78, 0x40, 0x79, 0x9D, 0x5B, 0x6D, 0xBE, 0x03, 0xCE, 0x70, 0x42,
+ 0xA8, 0x2A, 0x71, 0x11, 0xDF, 0x2E, 0x81, 0x01, 0xEA, 0x86, 0x12, 0x91,
+ 0x4F, 0xD0, 0x18, 0xED, 0x84, 0xA4, 0x48, 0x30, 0xB9, 0xB9, 0x7B, 0x56,
+ 0x3E, 0xEF, 0x8D, 0xE1, 0x1F, 0xFD, 0x90, 0xB2, 0x44, 0x85, 0x58, 0x6F,
+ 0xA7, 0x30, 0x74, 0x7C, 0xF1, 0xAE, 0x8C, 0x99, 0x3F, 0xF1, 0x4D, 0x57,
+ 0xC5, 0x95, 0x68, 0xC2, 0x36, 0xB2, 0xEC, 0xC2, 0x68, 0x7A, 0xC0, 0x81,
+ 0xA9, 0x55, 0x05, 0xE7, 0xCF, 0xAF, 0x50, 0xAC
+};
+
+static const br_x509_certificate CHAIN[] = {
+ { (unsigned char *)CERT0, sizeof CERT0 },
+ { (unsigned char *)CERT1, sizeof CERT1 }
+};
+
+#define CHAIN_LEN 2
diff --git a/test/monniaux/BearSSL/samples/chain-ec.h b/test/monniaux/BearSSL/samples/chain-ec.h
new file mode 100644
index 00000000..b4927a32
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/chain-ec.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * A sample server certificate chain with a single intermediate CA.
+ * Certificate key type: EC
+ * Signing algorithm for both certificates: ECDSA
+ */
+
+static const unsigned char CERT0[] = {
+ 0x30, 0x82, 0x01, 0xB0, 0x30, 0x82, 0x01, 0x56, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x1C, 0x4D, 0x00, 0x91, 0x69, 0xE2, 0x46, 0xAC, 0x90,
+ 0x7C, 0x64, 0x5C, 0x53, 0xF1, 0xFF, 0xB7, 0xC1, 0xCB, 0x6E, 0x7A, 0x30,
+ 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
+ 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
+ 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
+ 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D, 0x65, 0x64, 0x69, 0x61, 0x74,
+ 0x65, 0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x31, 0x30, 0x30, 0x31,
+ 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D, 0x33,
+ 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5A,
+ 0x30, 0x21, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
+ 0x02, 0x43, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03,
+ 0x13, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x30,
+ 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
+ 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42,
+ 0x00, 0x04, 0x5F, 0x38, 0x9D, 0xA7, 0xFF, 0x4D, 0x8A, 0xAF, 0xF6, 0x34,
+ 0x39, 0x46, 0x1A, 0xFC, 0x3A, 0xDF, 0xF4, 0x23, 0xAA, 0xA9, 0xEA, 0xFB,
+ 0xC5, 0x08, 0xDE, 0x00, 0x8E, 0xBE, 0x79, 0xA5, 0x37, 0x58, 0x4C, 0x6D,
+ 0xDD, 0x01, 0xCA, 0xAB, 0x47, 0xDF, 0x89, 0xB6, 0xC7, 0x17, 0x1F, 0x38,
+ 0xFC, 0x1D, 0x20, 0x14, 0xDD, 0x45, 0xC0, 0xE0, 0x8F, 0x93, 0x4E, 0x38,
+ 0x0B, 0xFC, 0xE9, 0x99, 0xA1, 0x49, 0xA3, 0x66, 0x30, 0x64, 0x30, 0x1F,
+ 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xF0,
+ 0xD0, 0xF1, 0x22, 0xF9, 0x7A, 0x48, 0x17, 0x06, 0x7B, 0x3D, 0xBD, 0xB8,
+ 0xF5, 0xCD, 0x55, 0x9C, 0x5C, 0x3E, 0x70, 0x30, 0x1D, 0x06, 0x03, 0x55,
+ 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xC3, 0x0E, 0x86, 0xAA, 0x75, 0xB4,
+ 0x15, 0xC0, 0xE5, 0x95, 0x09, 0x32, 0xBE, 0x5E, 0x92, 0x75, 0xA9, 0xE4,
+ 0x44, 0x9B, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF,
+ 0x04, 0x02, 0x30, 0x00, 0x30, 0x14, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04,
+ 0x0D, 0x30, 0x0B, 0x82, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F,
+ 0x73, 0x74, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04,
+ 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0x91, 0xFB,
+ 0xF4, 0x04, 0xD0, 0xE5, 0x2E, 0x01, 0xD4, 0x8C, 0xF0, 0x17, 0x62, 0x0F,
+ 0xDC, 0xCC, 0x80, 0xCA, 0x18, 0xC4, 0x40, 0x7C, 0x27, 0x03, 0xCB, 0x34,
+ 0x03, 0x0D, 0x9B, 0xC8, 0x59, 0x4D, 0x02, 0x20, 0x05, 0x55, 0x69, 0xE2,
+ 0xD8, 0xA1, 0x40, 0x33, 0x34, 0x0E, 0x7E, 0x49, 0x32, 0x64, 0x1D, 0x3F,
+ 0x6B, 0x1F, 0xD0, 0x2D, 0xB7, 0x2F, 0x52, 0x04, 0x56, 0xAF, 0xD3, 0x37,
+ 0x8F, 0x87, 0x99, 0xA2
+};
+
+static const unsigned char CERT1[] = {
+ 0x30, 0x82, 0x01, 0xA9, 0x30, 0x82, 0x01, 0x4E, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x20, 0xD3, 0xEB, 0xE2, 0x8C, 0xFE, 0xDA, 0xE6, 0xA3,
+ 0x2C, 0x5E, 0x3B, 0xF2, 0x66, 0x3A, 0x2B, 0x36, 0x7B, 0xB0, 0xCA, 0x30,
+ 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
+ 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
+ 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
+ 0x04, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x1E, 0x17, 0x0D, 0x31, 0x30, 0x30,
+ 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D,
+ 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39,
+ 0x5A, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
+ 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
+ 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D, 0x65, 0x64, 0x69,
+ 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
+ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48,
+ 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x70, 0x2E, 0x92,
+ 0x82, 0x01, 0x17, 0x6C, 0x6D, 0xAB, 0xE1, 0xD1, 0x63, 0x09, 0x48, 0x49,
+ 0xD2, 0xA6, 0x35, 0x52, 0xD3, 0x3C, 0x73, 0xBB, 0xB2, 0x88, 0x37, 0x98,
+ 0x87, 0xF1, 0x8D, 0xE0, 0xEC, 0x65, 0x9A, 0x0E, 0x13, 0xF5, 0xED, 0x91,
+ 0x61, 0xC8, 0xB6, 0x6D, 0x33, 0x84, 0x6E, 0xAE, 0x8E, 0x55, 0x80, 0xCD,
+ 0x49, 0x9E, 0x07, 0xBF, 0xD0, 0xAE, 0x9D, 0xE6, 0xD0, 0xB3, 0x27, 0x16,
+ 0xA1, 0xA3, 0x63, 0x30, 0x61, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23,
+ 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x95, 0x41, 0xB4, 0xE2, 0x67, 0xAA,
+ 0xF1, 0x7F, 0xBC, 0x8F, 0x79, 0xF3, 0x68, 0x14, 0x5A, 0x6B, 0x92, 0x16,
+ 0xA2, 0x40, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04,
+ 0x14, 0xF0, 0xD0, 0xF1, 0x22, 0xF9, 0x7A, 0x48, 0x17, 0x06, 0x7B, 0x3D,
+ 0xBD, 0xB8, 0xF5, 0xCD, 0x55, 0x9C, 0x5C, 0x3E, 0x70, 0x30, 0x0E, 0x06,
+ 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04, 0x04, 0x03, 0x02, 0x00,
+ 0x86, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04,
+ 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86,
+ 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02,
+ 0x21, 0x00, 0x85, 0xE3, 0x46, 0x68, 0x99, 0xD6, 0x02, 0x7A, 0x59, 0x66,
+ 0x1C, 0xB7, 0x4F, 0x35, 0x2D, 0x08, 0x36, 0x38, 0x61, 0x7E, 0x05, 0x48,
+ 0xD8, 0x69, 0x43, 0x1F, 0xEB, 0x56, 0xE9, 0xAD, 0x06, 0x0E, 0x02, 0x21,
+ 0x00, 0x82, 0x70, 0xB4, 0x62, 0x03, 0x49, 0x46, 0xC8, 0x54, 0x59, 0x05,
+ 0xD9, 0x78, 0xDB, 0x53, 0x1C, 0xE0, 0x6E, 0x66, 0xF5, 0x0F, 0x14, 0x3B,
+ 0xC9, 0x2D, 0x38, 0x12, 0x70, 0x91, 0x56, 0xF9, 0xA9
+};
+
+static const br_x509_certificate CHAIN[] = {
+ { (unsigned char *)CERT0, sizeof CERT0 },
+ { (unsigned char *)CERT1, sizeof CERT1 }
+};
+
+#define CHAIN_LEN 2
diff --git a/test/monniaux/BearSSL/samples/chain-rsa.h b/test/monniaux/BearSSL/samples/chain-rsa.h
new file mode 100644
index 00000000..f8387891
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/chain-rsa.h
@@ -0,0 +1,183 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * A sample server certificate chain with a single intermediate CA.
+ * Certificate key type: RSA
+ * Signing algorithm for both certificates: RSA
+ */
+
+static const unsigned char CERT0[] = {
+ 0x30, 0x82, 0x03, 0x3C, 0x30, 0x82, 0x02, 0x24, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x58, 0xDA, 0xBA, 0x36, 0xCD, 0xED, 0xA0, 0xDA, 0x5C,
+ 0x10, 0x33, 0x73, 0x8E, 0x0C, 0x64, 0x92, 0x79, 0x92, 0xAF, 0x03, 0x30,
+ 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
+ 0x05, 0x00, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
+ 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55,
+ 0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D, 0x65, 0x64,
+ 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x31,
+ 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A,
+ 0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39,
+ 0x35, 0x39, 0x5A, 0x30, 0x21, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
+ 0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03,
+ 0x55, 0x04, 0x03, 0x13, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F,
+ 0x73, 0x74, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86,
+ 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
+ 0x0F, 0x00, 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD4,
+ 0x7A, 0x1D, 0x27, 0xBA, 0x2B, 0x3A, 0x67, 0xB2, 0x91, 0x6A, 0xFB, 0xE7,
+ 0x83, 0x44, 0xCA, 0xED, 0x1C, 0x75, 0xAD, 0xDD, 0x4D, 0x83, 0x62, 0xD6,
+ 0xAA, 0x68, 0x95, 0xB2, 0x24, 0x21, 0x7B, 0x15, 0xAE, 0x2A, 0x99, 0x68,
+ 0x15, 0xED, 0x66, 0xF0, 0xB8, 0x58, 0xE7, 0xD3, 0xF5, 0x2E, 0xC6, 0xD9,
+ 0x2A, 0x5E, 0xE7, 0x0E, 0x2E, 0xE7, 0xFC, 0x67, 0x59, 0xC0, 0xC8, 0x61,
+ 0x7D, 0x4B, 0xA4, 0x6F, 0xDD, 0x9F, 0xD9, 0xC8, 0x85, 0x87, 0x64, 0xC7,
+ 0xBA, 0x1A, 0x0F, 0x29, 0xD4, 0x96, 0xA8, 0x78, 0x9A, 0x6B, 0x62, 0x20,
+ 0xA9, 0x32, 0xD0, 0xEE, 0xA9, 0x8C, 0x28, 0x61, 0x47, 0xA2, 0x50, 0x2A,
+ 0x63, 0xF6, 0x21, 0xDE, 0xDA, 0xD8, 0xD5, 0xF0, 0x7F, 0xC5, 0x00, 0x82,
+ 0x70, 0xE6, 0xA3, 0xBF, 0x5C, 0x89, 0x27, 0x4F, 0x51, 0x92, 0x77, 0x03,
+ 0xC3, 0xB0, 0xCC, 0x2E, 0x3B, 0xEC, 0x23, 0xF2, 0x2F, 0x53, 0x41, 0xAF,
+ 0x89, 0x93, 0xFF, 0xD2, 0x80, 0xB1, 0x43, 0x97, 0xDE, 0xD6, 0x19, 0xA0,
+ 0x92, 0x12, 0x7A, 0x3D, 0x66, 0x79, 0xE1, 0xC1, 0xBC, 0xE1, 0x77, 0x70,
+ 0xA2, 0x8B, 0x3D, 0x46, 0x84, 0x53, 0x3F, 0xE4, 0x4E, 0x42, 0x41, 0x37,
+ 0x92, 0x1E, 0x1F, 0xFD, 0x38, 0xB3, 0xF7, 0xEF, 0x87, 0x39, 0x80, 0xD3,
+ 0x56, 0xCF, 0xF4, 0xE0, 0x13, 0xDE, 0x64, 0xB0, 0x72, 0xA4, 0x03, 0x84,
+ 0xC4, 0x41, 0xED, 0x6F, 0xFA, 0x3E, 0xE2, 0xCA, 0x04, 0x20, 0xD2, 0xD7,
+ 0xDC, 0x2C, 0x82, 0x2B, 0x7A, 0xE2, 0x6D, 0xA1, 0x1C, 0x48, 0xDB, 0xCF,
+ 0x89, 0x4F, 0x34, 0x97, 0x3D, 0x28, 0xA8, 0x53, 0xDA, 0xE7, 0xC1, 0xE1,
+ 0x73, 0x15, 0xA3, 0x30, 0x76, 0x7F, 0x8F, 0x23, 0x42, 0x14, 0x3D, 0x51,
+ 0x34, 0xD2, 0x5A, 0xAD, 0x3C, 0x9B, 0xCB, 0xC8, 0xFE, 0x7F, 0x6E, 0x8E,
+ 0x40, 0xF3, 0xBD, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x66, 0x30, 0x64,
+ 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
+ 0x14, 0xC5, 0x01, 0xAD, 0x7C, 0xE6, 0x63, 0xC9, 0x9C, 0x17, 0x5F, 0xC5,
+ 0xA2, 0xA7, 0xE1, 0x50, 0x5F, 0xE8, 0xF5, 0xA8, 0xC6, 0x30, 0x1D, 0x06,
+ 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xC5, 0x01, 0xAD, 0x7C,
+ 0xE6, 0x63, 0xC9, 0x9C, 0x17, 0x5F, 0xC5, 0xA2, 0xA7, 0xE1, 0x50, 0x5F,
+ 0xE8, 0xF5, 0xA8, 0xC6, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01,
+ 0x01, 0xFF, 0x04, 0x02, 0x30, 0x00, 0x30, 0x14, 0x06, 0x03, 0x55, 0x1D,
+ 0x11, 0x04, 0x0D, 0x30, 0x0B, 0x82, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C,
+ 0x68, 0x6F, 0x73, 0x74, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
+ 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
+ 0x71, 0xB3, 0x5D, 0x21, 0xC2, 0x0E, 0xD7, 0xD0, 0xC6, 0xF8, 0x4A, 0x73,
+ 0x87, 0x4D, 0x22, 0x02, 0x27, 0xD0, 0xC7, 0xF8, 0xFD, 0x1A, 0x6D, 0x62,
+ 0x58, 0x82, 0x08, 0x76, 0x20, 0x07, 0xC0, 0x48, 0x92, 0xF3, 0xE9, 0x98,
+ 0x5A, 0xEB, 0x0D, 0x4E, 0x56, 0x95, 0x63, 0x68, 0x4D, 0xAB, 0xD4, 0x00,
+ 0x76, 0xE0, 0x37, 0xCF, 0x0B, 0x11, 0x61, 0x59, 0x5C, 0xDE, 0xF6, 0xA5,
+ 0x14, 0x59, 0xD9, 0x25, 0x9F, 0x59, 0xD9, 0xD3, 0x5E, 0x86, 0xAC, 0x1D,
+ 0xA6, 0x2C, 0x32, 0x42, 0x19, 0x32, 0x13, 0x40, 0x0B, 0x54, 0xCD, 0x67,
+ 0x26, 0xB6, 0xBD, 0xB4, 0x96, 0xA4, 0xCA, 0x1F, 0x7F, 0x37, 0xD6, 0xA8,
+ 0x75, 0xEB, 0x3A, 0x81, 0x51, 0x30, 0xB9, 0xF9, 0x4A, 0x01, 0x6F, 0xD1,
+ 0xD6, 0xED, 0x4F, 0xDF, 0x3F, 0x30, 0x60, 0x06, 0x67, 0x92, 0x8E, 0x61,
+ 0x85, 0x5F, 0x1A, 0xB5, 0x8C, 0xB3, 0x0F, 0x61, 0xA9, 0xFA, 0xDF, 0x5D,
+ 0xC4, 0x64, 0x00, 0xEA, 0x87, 0xB1, 0x33, 0x5F, 0x7D, 0xCB, 0xA3, 0x85,
+ 0x24, 0x6E, 0x2C, 0x65, 0x3B, 0xEA, 0x73, 0x3F, 0x86, 0xD3, 0xFD, 0xE8,
+ 0xE4, 0x02, 0xC6, 0x61, 0x6A, 0x26, 0x17, 0x52, 0x01, 0x25, 0x5C, 0x7F,
+ 0xA8, 0xCE, 0x69, 0x1F, 0xAC, 0x61, 0x2C, 0xB7, 0x04, 0xAF, 0xFD, 0xA4,
+ 0x7A, 0x99, 0xCB, 0x26, 0x15, 0x4C, 0xFF, 0x74, 0xD4, 0x73, 0x0C, 0x57,
+ 0x0C, 0x26, 0xEB, 0xD7, 0x2A, 0xDC, 0x5C, 0xC3, 0x41, 0xBB, 0xC2, 0xF3,
+ 0xCE, 0x16, 0xBD, 0x8E, 0x7B, 0xFB, 0xE9, 0xDF, 0xAB, 0x21, 0x0D, 0x91,
+ 0xB3, 0x8D, 0xD8, 0xCF, 0xC8, 0xF4, 0x49, 0xB9, 0xD0, 0xE3, 0x16, 0x70,
+ 0x3F, 0xB3, 0xDE, 0xD4, 0x07, 0x25, 0xFA, 0x46, 0x44, 0x52, 0x89, 0x4D,
+ 0x89, 0xC8, 0xE2, 0xCA, 0xB5, 0x33, 0x7F, 0xC7, 0x21, 0xBD, 0x28, 0xEF,
+ 0xFE, 0x66, 0x74, 0x03
+};
+
+static const unsigned char CERT1[] = {
+ 0x30, 0x82, 0x03, 0x34, 0x30, 0x82, 0x02, 0x1C, 0xA0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x70, 0x0F, 0x60, 0xEE, 0xF0, 0x07, 0x9A, 0x9C, 0x69,
+ 0xAC, 0x97, 0x62, 0x26, 0x4F, 0x5D, 0x05, 0xB6, 0xF9, 0x8F, 0x48, 0x30,
+ 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
+ 0x05, 0x00, 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
+ 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
+ 0x04, 0x03, 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x1E, 0x17, 0x0D,
+ 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x5A, 0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35,
+ 0x39, 0x35, 0x39, 0x5A, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
+ 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06,
+ 0x03, 0x55, 0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D,
+ 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01,
+ 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
+ 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01,
+ 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xB3, 0xE8, 0x6B, 0xAF, 0x9C, 0x16,
+ 0x52, 0xE3, 0x81, 0x0C, 0x50, 0xAB, 0x25, 0xCE, 0xCC, 0x0D, 0xC7, 0xF2,
+ 0x1F, 0x7F, 0x50, 0xDF, 0x2C, 0x5C, 0x35, 0xD6, 0x62, 0x2E, 0x63, 0x27,
+ 0x41, 0xA7, 0xE4, 0x53, 0xA8, 0x4B, 0x27, 0xFA, 0x13, 0x91, 0xA3, 0xFA,
+ 0x09, 0x4A, 0x2F, 0x3B, 0x5E, 0xCF, 0x77, 0xB3, 0x8A, 0xC1, 0xCD, 0x49,
+ 0x95, 0x9C, 0x75, 0x0D, 0x64, 0x74, 0xEF, 0xE4, 0xD7, 0x4B, 0xB9, 0xA1,
+ 0x9B, 0x68, 0xD2, 0x30, 0x71, 0x48, 0xEA, 0xF7, 0x4B, 0x14, 0xDF, 0x3F,
+ 0x47, 0xA9, 0xD8, 0xBB, 0xEC, 0x8F, 0x28, 0xCC, 0xFA, 0xDF, 0xB4, 0x1F,
+ 0x94, 0x7C, 0x96, 0xFC, 0x08, 0x05, 0x28, 0xF9, 0xE8, 0xF4, 0x2F, 0x2F,
+ 0xEE, 0x62, 0x9C, 0x8A, 0x3A, 0xE0, 0x85, 0x58, 0x60, 0xB6, 0x0F, 0x2D,
+ 0x30, 0xB4, 0xC0, 0x41, 0x54, 0x91, 0x4C, 0x1F, 0x5F, 0xAD, 0xF1, 0x19,
+ 0xF0, 0xC0, 0x22, 0xA6, 0x7D, 0xD8, 0x3F, 0x79, 0x34, 0x59, 0x42, 0x7B,
+ 0x5B, 0xB5, 0x41, 0xC4, 0x64, 0x7F, 0x52, 0xCF, 0x3C, 0x37, 0x22, 0xA1,
+ 0x2F, 0x79, 0x25, 0x94, 0x24, 0x41, 0xC2, 0x3F, 0xFA, 0xC7, 0x75, 0xFB,
+ 0x48, 0xB5, 0x0D, 0x18, 0xA7, 0xF4, 0x54, 0xF3, 0x2E, 0x6E, 0xD8, 0x43,
+ 0x58, 0xC4, 0xAB, 0x50, 0xE8, 0x05, 0xAD, 0x91, 0xB6, 0x1E, 0x01, 0x75,
+ 0xB3, 0x54, 0x9C, 0xDE, 0xA0, 0x99, 0x15, 0xFB, 0xAC, 0xF1, 0x5C, 0x97,
+ 0x49, 0x51, 0xCC, 0xEF, 0x58, 0x12, 0x6F, 0x73, 0x6B, 0xB3, 0x34, 0x14,
+ 0x01, 0x0F, 0x5A, 0x9D, 0xFA, 0xAA, 0xD6, 0x93, 0xD3, 0xE2, 0xEA, 0xC3,
+ 0xAB, 0xBC, 0x4E, 0xED, 0xCC, 0x51, 0xA1, 0xB8, 0xF8, 0x94, 0xB6, 0xB4,
+ 0x2C, 0xA8, 0x86, 0x2B, 0x1F, 0xF6, 0x51, 0x43, 0x29, 0x52, 0x5E, 0x13,
+ 0x89, 0xB3, 0x6A, 0x78, 0x60, 0x4E, 0x4E, 0xC0, 0x1B, 0xA5, 0x02, 0x03,
+ 0x01, 0x00, 0x01, 0xA3, 0x63, 0x30, 0x61, 0x30, 0x1F, 0x06, 0x03, 0x55,
+ 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xC3, 0x0A, 0xBC, 0xB4,
+ 0x90, 0x63, 0x96, 0x92, 0x45, 0xBC, 0x27, 0xA0, 0xF0, 0x58, 0x89, 0x2A,
+ 0xD5, 0x78, 0x00, 0x12, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04,
+ 0x16, 0x04, 0x14, 0x7C, 0xCF, 0xA3, 0xC6, 0x29, 0xF7, 0xF3, 0xC5, 0xAA,
+ 0x19, 0xD0, 0xC0, 0x16, 0xEB, 0xE0, 0x40, 0x0F, 0xCE, 0x44, 0xA7, 0x30,
+ 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04, 0x04, 0x03,
+ 0x02, 0x00, 0x86, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01,
+ 0xFF, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30, 0x0D, 0x06, 0x09,
+ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03,
+ 0x82, 0x01, 0x01, 0x00, 0x54, 0x3B, 0xF4, 0xEA, 0xC6, 0xE4, 0xE8, 0xC0,
+ 0x5B, 0x12, 0xB2, 0xAD, 0xFA, 0xBD, 0xA9, 0xF4, 0x62, 0x03, 0xAD, 0x57,
+ 0x4C, 0x6F, 0x07, 0xA7, 0x47, 0x35, 0x75, 0x3E, 0x48, 0xA2, 0x5B, 0x71,
+ 0x10, 0x01, 0x8F, 0x93, 0x99, 0x9D, 0xA7, 0x47, 0xE3, 0x8E, 0x3E, 0xAF,
+ 0x2D, 0xBA, 0x06, 0xFE, 0xD4, 0xE4, 0x97, 0xBC, 0x10, 0x8D, 0xA6, 0xA5,
+ 0x3A, 0x3C, 0xE6, 0x21, 0xCB, 0x3B, 0xDA, 0x13, 0xE3, 0x4A, 0x23, 0xEB,
+ 0xBC, 0xBA, 0xA5, 0x90, 0x91, 0xAD, 0x55, 0x6E, 0xD5, 0xDD, 0x85, 0x4B,
+ 0x6B, 0x27, 0xD2, 0x39, 0x76, 0x06, 0x2E, 0xD4, 0x23, 0x5B, 0xBB, 0x55,
+ 0xB5, 0x49, 0x3F, 0x7D, 0x2F, 0x21, 0xCB, 0x96, 0x75, 0x19, 0x99, 0xA9,
+ 0x99, 0xF1, 0x0B, 0xBB, 0x3E, 0xC7, 0x10, 0xA7, 0x8B, 0x7A, 0x52, 0xB9,
+ 0x49, 0xE4, 0x14, 0x08, 0x78, 0xE7, 0x3D, 0xD9, 0x98, 0x57, 0xDA, 0xFF,
+ 0xDA, 0xB9, 0x8D, 0x66, 0x75, 0x80, 0xB5, 0x5D, 0xB9, 0xDF, 0x69, 0x65,
+ 0xDB, 0x16, 0xE7, 0x1E, 0xC4, 0x40, 0xE4, 0x46, 0x8A, 0x1E, 0x76, 0x32,
+ 0x16, 0xE7, 0x83, 0x67, 0xDA, 0x78, 0x56, 0xAB, 0x0F, 0x4C, 0x5C, 0xF1,
+ 0x70, 0xDA, 0x66, 0x20, 0xF4, 0xF8, 0x0F, 0xF6, 0xDC, 0x2F, 0x72, 0x97,
+ 0x6B, 0x78, 0x40, 0x79, 0x9D, 0x5B, 0x6D, 0xBE, 0x03, 0xCE, 0x70, 0x42,
+ 0xA8, 0x2A, 0x71, 0x11, 0xDF, 0x2E, 0x81, 0x01, 0xEA, 0x86, 0x12, 0x91,
+ 0x4F, 0xD0, 0x18, 0xED, 0x84, 0xA4, 0x48, 0x30, 0xB9, 0xB9, 0x7B, 0x56,
+ 0x3E, 0xEF, 0x8D, 0xE1, 0x1F, 0xFD, 0x90, 0xB2, 0x44, 0x85, 0x58, 0x6F,
+ 0xA7, 0x30, 0x74, 0x7C, 0xF1, 0xAE, 0x8C, 0x99, 0x3F, 0xF1, 0x4D, 0x57,
+ 0xC5, 0x95, 0x68, 0xC2, 0x36, 0xB2, 0xEC, 0xC2, 0x68, 0x7A, 0xC0, 0x81,
+ 0xA9, 0x55, 0x05, 0xE7, 0xCF, 0xAF, 0x50, 0xAC
+};
+
+static const br_x509_certificate CHAIN[] = {
+ { (unsigned char *)CERT0, sizeof CERT0 },
+ { (unsigned char *)CERT1, sizeof CERT1 }
+};
+
+#define CHAIN_LEN 2
diff --git a/test/monniaux/BearSSL/samples/client_basic.c b/test/monniaux/BearSSL/samples/client_basic.c
new file mode 100644
index 00000000..31a88be4
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/client_basic.c
@@ -0,0 +1,380 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+#include "bearssl.h"
+
+/*
+ * Connect to the specified host and port. The connected socket is
+ * returned, or -1 on error.
+ */
+static int
+host_connect(const char *host, const char *port)
+{
+ struct addrinfo hints, *si, *p;
+ int fd;
+ int err;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ err = getaddrinfo(host, port, &hints, &si);
+ if (err != 0) {
+ fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
+ gai_strerror(err));
+ return -1;
+ }
+ fd = -1;
+ for (p = si; p != NULL; p = p->ai_next) {
+ struct sockaddr *sa;
+ void *addr;
+ char tmp[INET6_ADDRSTRLEN + 50];
+
+ sa = (struct sockaddr *)p->ai_addr;
+ if (sa->sa_family == AF_INET) {
+ addr = &((struct sockaddr_in *)sa)->sin_addr;
+ } else if (sa->sa_family == AF_INET6) {
+ addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
+ } else {
+ addr = NULL;
+ }
+ if (addr != NULL) {
+ inet_ntop(p->ai_family, addr, tmp, sizeof tmp);
+ } else {
+ sprintf(tmp, "<unknown family: %d>",
+ (int)sa->sa_family);
+ }
+ fprintf(stderr, "connecting to: %s\n", tmp);
+ fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (fd < 0) {
+ perror("socket()");
+ continue;
+ }
+ if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
+ perror("connect()");
+ close(fd);
+ continue;
+ }
+ break;
+ }
+ if (p == NULL) {
+ freeaddrinfo(si);
+ fprintf(stderr, "ERROR: failed to connect\n");
+ return -1;
+ }
+ freeaddrinfo(si);
+ fprintf(stderr, "connected.\n");
+ return fd;
+}
+
+/*
+ * Low-level data read callback for the simplified SSL I/O API.
+ */
+static int
+sock_read(void *ctx, unsigned char *buf, size_t len)
+{
+ for (;;) {
+ ssize_t rlen;
+
+ rlen = read(*(int *)ctx, buf, len);
+ if (rlen <= 0) {
+ if (rlen < 0 && errno == EINTR) {
+ continue;
+ }
+ return -1;
+ }
+ return (int)rlen;
+ }
+}
+
+/*
+ * Low-level data write callback for the simplified SSL I/O API.
+ */
+static int
+sock_write(void *ctx, const unsigned char *buf, size_t len)
+{
+ for (;;) {
+ ssize_t wlen;
+
+ wlen = write(*(int *)ctx, buf, len);
+ if (wlen <= 0) {
+ if (wlen < 0 && errno == EINTR) {
+ continue;
+ }
+ return -1;
+ }
+ return (int)wlen;
+ }
+}
+
+/*
+ * The hardcoded trust anchors. These are the two DN + public key that
+ * correspond to the self-signed certificates cert-root-rsa.pem and
+ * cert-root-ec.pem.
+ *
+ * C code for hardcoded trust anchors can be generated with the "brssl"
+ * command-line tool (with the "ta" command).
+ */
+
+static const unsigned char TA0_DN[] = {
+ 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
+ 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
+ 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
+};
+
+static const unsigned char TA0_RSA_N[] = {
+ 0xB6, 0xD9, 0x34, 0xD4, 0x50, 0xFD, 0xB3, 0xAF, 0x7A, 0x73, 0xF1, 0xCE,
+ 0x38, 0xBF, 0x5D, 0x6F, 0x45, 0xE1, 0xFD, 0x4E, 0xB1, 0x98, 0xC6, 0x60,
+ 0x83, 0x26, 0xD2, 0x17, 0xD1, 0xC5, 0xB7, 0x9A, 0xA3, 0xC1, 0xDE, 0x63,
+ 0x39, 0x97, 0x9C, 0xF0, 0x5E, 0x5C, 0xC8, 0x1C, 0x17, 0xB9, 0x88, 0x19,
+ 0x6D, 0xF0, 0xB6, 0x2E, 0x30, 0x50, 0xA1, 0x54, 0x6E, 0x93, 0xC0, 0xDB,
+ 0xCF, 0x30, 0xCB, 0x9F, 0x1E, 0x27, 0x79, 0xF1, 0xC3, 0x99, 0x52, 0x35,
+ 0xAA, 0x3D, 0xB6, 0xDF, 0xB0, 0xAD, 0x7C, 0xCB, 0x49, 0xCD, 0xC0, 0xED,
+ 0xE7, 0x66, 0x10, 0x2A, 0xE9, 0xCE, 0x28, 0x1F, 0x21, 0x50, 0xFA, 0x77,
+ 0x4C, 0x2D, 0xDA, 0xEF, 0x3C, 0x58, 0xEB, 0x4E, 0xBF, 0xCE, 0xE9, 0xFB,
+ 0x1A, 0xDA, 0xA3, 0x83, 0xA3, 0xCD, 0xA3, 0xCA, 0x93, 0x80, 0xDC, 0xDA,
+ 0xF3, 0x17, 0xCC, 0x7A, 0xAB, 0x33, 0x80, 0x9C, 0xB2, 0xD4, 0x7F, 0x46,
+ 0x3F, 0xC5, 0x3C, 0xDC, 0x61, 0x94, 0xB7, 0x27, 0x29, 0x6E, 0x2A, 0xBC,
+ 0x5B, 0x09, 0x36, 0xD4, 0xC6, 0x3B, 0x0D, 0xEB, 0xBE, 0xCE, 0xDB, 0x1D,
+ 0x1C, 0xBC, 0x10, 0x6A, 0x71, 0x71, 0xB3, 0xF2, 0xCA, 0x28, 0x9A, 0x77,
+ 0xF2, 0x8A, 0xEC, 0x42, 0xEF, 0xB1, 0x4A, 0x8E, 0xE2, 0xF2, 0x1A, 0x32,
+ 0x2A, 0xCD, 0xC0, 0xA6, 0x46, 0x2C, 0x9A, 0xC2, 0x85, 0x37, 0x91, 0x7F,
+ 0x46, 0xA1, 0x93, 0x81, 0xA1, 0x74, 0x66, 0xDF, 0xBA, 0xB3, 0x39, 0x20,
+ 0x91, 0x93, 0xFA, 0x1D, 0xA1, 0xA8, 0x85, 0xE7, 0xE4, 0xF9, 0x07, 0xF6,
+ 0x10, 0xF6, 0xA8, 0x27, 0x01, 0xB6, 0x7F, 0x12, 0xC3, 0x40, 0xC3, 0xC9,
+ 0xE2, 0xB0, 0xAB, 0x49, 0x18, 0x3A, 0x64, 0xB6, 0x59, 0xB7, 0x95, 0xB5,
+ 0x96, 0x36, 0xDF, 0x22, 0x69, 0xAA, 0x72, 0x6A, 0x54, 0x4E, 0x27, 0x29,
+ 0xA3, 0x0E, 0x97, 0x15
+};
+
+static const unsigned char TA0_RSA_E[] = {
+ 0x01, 0x00, 0x01
+};
+
+static const unsigned char TA1_DN[] = {
+ 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
+ 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
+ 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
+};
+
+static const unsigned char TA1_EC_Q[] = {
+ 0x04, 0x71, 0x74, 0xBA, 0xAB, 0xB9, 0x30, 0x2E, 0x81, 0xD5, 0xE5, 0x57,
+ 0xF9, 0xF3, 0x20, 0x68, 0x0C, 0x9C, 0xF9, 0x64, 0xDB, 0xB4, 0x20, 0x0D,
+ 0x6D, 0xEA, 0x40, 0xD0, 0x4A, 0x6E, 0x42, 0xFD, 0xB6, 0x9A, 0x68, 0x25,
+ 0x44, 0xF6, 0xDF, 0x7B, 0xC4, 0xFC, 0xDE, 0xDD, 0x7B, 0xBB, 0xC5, 0xDB,
+ 0x7C, 0x76, 0x3F, 0x41, 0x66, 0x40, 0x6E, 0xDB, 0xA7, 0x87, 0xC2, 0xE5,
+ 0xD8, 0xC5, 0xF3, 0x7F, 0x8D
+};
+
+static const br_x509_trust_anchor TAs[2] = {
+ {
+ { (unsigned char *)TA0_DN, sizeof TA0_DN },
+ BR_X509_TA_CA,
+ {
+ BR_KEYTYPE_RSA,
+ { .rsa = {
+ (unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
+ (unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
+ } }
+ }
+ },
+ {
+ { (unsigned char *)TA1_DN, sizeof TA1_DN },
+ BR_X509_TA_CA,
+ {
+ BR_KEYTYPE_EC,
+ { .ec = {
+ BR_EC_secp256r1,
+ (unsigned char *)TA1_EC_Q, sizeof TA1_EC_Q,
+ } }
+ }
+ }
+};
+
+#define TAs_NUM 2
+
+/*
+ * Main program: this is a simple program that expects 2 or 3 arguments.
+ * The first two arguments are a hostname and a port; the program will
+ * open a SSL connection with that server and port. It will then send
+ * a simple HTTP GET request, using the third argument as target path
+ * ("/" is used as path if no third argument was provided). The HTTP
+ * response, complete with header and contents, is received and written
+ * on stdout.
+ */
+int
+main(int argc, char *argv[])
+{
+ const char *host, *port, *path;
+ int fd;
+ br_ssl_client_context sc;
+ br_x509_minimal_context xc;
+ unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];
+ br_sslio_context ioc;
+
+ /*
+ * Parse command-line argument: host, port, and path. The path
+ * is optional; if absent, "/" is used.
+ */
+ if (argc < 3 || argc > 4) {
+ return EXIT_FAILURE;
+ }
+ host = argv[1];
+ port = argv[2];
+ if (argc == 4) {
+ path = argv[3];
+ } else {
+ path = "/";
+ }
+
+ /*
+ * Ignore SIGPIPE to avoid crashing in case of abrupt socket close.
+ */
+ signal(SIGPIPE, SIG_IGN);
+
+ /*
+ * Open the socket to the target server.
+ */
+ fd = host_connect(host, port);
+ if (fd < 0) {
+ return EXIT_FAILURE;
+ }
+
+ /*
+ * Initialise the client context:
+ * -- Use the "full" profile (all supported algorithms).
+ * -- The provided X.509 validation engine is initialised, with
+ * the hardcoded trust anchor.
+ */
+ br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);
+
+ /*
+ * Set the I/O buffer to the provided array. We allocated a
+ * buffer large enough for full-duplex behaviour with all
+ * allowed sizes of SSL records, hence we set the last argument
+ * to 1 (which means "split the buffer into separate input and
+ * output areas").
+ */
+ br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 1);
+
+ /*
+ * Reset the client context, for a new handshake. We provide the
+ * target host name: it will be used for the SNI extension. The
+ * last parameter is 0: we are not trying to resume a session.
+ */
+ br_ssl_client_reset(&sc, host, 0);
+
+ /*
+ * Initialise the simplified I/O wrapper context, to use our
+ * SSL client context, and the two callbacks for socket I/O.
+ */
+ br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd);
+
+ /*
+ * Note that while the context has, at that point, already
+ * assembled the ClientHello to send, nothing happened on the
+ * network yet. Real I/O will occur only with the next call.
+ *
+ * We write our simple HTTP request. We could test each call
+ * for an error (-1), but this is not strictly necessary, since
+ * the error state "sticks": if the context fails for any reason
+ * (e.g. bad server certificate), then it will remain in failed
+ * state and all subsequent calls will return -1 as well.
+ */
+ br_sslio_write_all(&ioc, "GET ", 4);
+ br_sslio_write_all(&ioc, path, strlen(path));
+ br_sslio_write_all(&ioc, " HTTP/1.0\r\nHost: ", 17);
+ br_sslio_write_all(&ioc, host, strlen(host));
+ br_sslio_write_all(&ioc, "\r\n\r\n", 4);
+
+ /*
+ * SSL is a buffered protocol: we make sure that all our request
+ * bytes are sent onto the wire.
+ */
+ br_sslio_flush(&ioc);
+
+ /*
+ * Read the server's response. We use here a small 512-byte buffer,
+ * but most of the buffering occurs in the client context: the
+ * server will send full records (up to 16384 bytes worth of data
+ * each), and the client context buffers one full record at a time.
+ */
+ for (;;) {
+ int rlen;
+ unsigned char tmp[512];
+
+ rlen = br_sslio_read(&ioc, tmp, sizeof tmp);
+ if (rlen < 0) {
+ break;
+ }
+ fwrite(tmp, 1, rlen, stdout);
+ }
+
+ /*
+ * Close the socket.
+ */
+ close(fd);
+
+ /*
+ * Check whether we closed properly or not. If the engine is
+ * closed, then its error status allows to distinguish between
+ * a normal closure and a SSL error.
+ *
+ * If the engine is NOT closed, then this means that the
+ * underlying network socket was closed or failed in some way.
+ * Note that many Web servers out there do not properly close
+ * their SSL connections (they don't send a close_notify alert),
+ * which will be reported here as "socket closed without proper
+ * SSL termination".
+ */
+ if (br_ssl_engine_current_state(&sc.eng) == BR_SSL_CLOSED) {
+ int err;
+
+ err = br_ssl_engine_last_error(&sc.eng);
+ if (err == 0) {
+ fprintf(stderr, "closed.\n");
+ return EXIT_SUCCESS;
+ } else {
+ fprintf(stderr, "SSL error %d\n", err);
+ return EXIT_FAILURE;
+ }
+ } else {
+ fprintf(stderr,
+ "socket closed without proper SSL termination\n");
+ return EXIT_FAILURE;
+ }
+}
diff --git a/test/monniaux/BearSSL/samples/custom_profile.c b/test/monniaux/BearSSL/samples/custom_profile.c
new file mode 100644
index 00000000..81335329
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/custom_profile.c
@@ -0,0 +1,601 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * A "profile" is an initialisation function for a SSL context, that
+ * configures a list of cipher suites and algorithm implementations.
+ * While BearSSL comes with a few predefined profiles, you might one
+ * to define you own, using the example below as guidance.
+ *
+ * Each individual initialisation call sets a parameter or an algorithm
+ * support. Setting a specific algorithm pulls in the implementation of
+ * that algorithm in the compiled binary, as per static linking
+ * behaviour. Removing some of this calls will then reduce total code
+ * footprint, but also mechanically prevents some features to be
+ * supported (protocol versions and cipher suites).
+ *
+ * The two below define profiles for the client and the server contexts,
+ * respectively. Of course, in a typical size-constrained application,
+ * you would use one or the other, not both, to avoid pulling in code
+ * for both.
+ */
+
+void
+example_client_profile(br_ssl_client_context *cc
+ /* and possibly some other arguments */)
+{
+ /*
+ * A list of cipher suites, by preference (first is most
+ * preferred). The list below contains all cipher suites supported
+ * by BearSSL; trim it done to your needs.
+ */
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
+ };
+
+ /*
+ * Client context must be cleared at some point. This sets
+ * every value and pointer to 0 or NULL.
+ */
+ br_ssl_client_zero(cc);
+
+ /*
+ * Define minimum and maximum protocol versions. Supported
+ * versions are:
+ * BR_TLS10 TLS 1.0
+ * BR_TLS11 TLS 1.1
+ * BR_TLS12 TLS 1.2
+ */
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
+
+ /*
+ * Set the PRF implementation(s).
+ * For TLS 1.0 and 1.1, the "prf10" is needed.
+ * For TLS 1.2, this depends on the cipher suite:
+ * -- cipher suites with a name ending in "SHA384" need "prf_sha384";
+ * -- all others need "prf_sha256".
+ *
+ * Note that a cipher suite like TLS_RSA_WITH_AES_128_CBC_SHA will
+ * use SHA-1 for the per-record MAC (that's what the final "SHA"
+ * means), but still SHA-256 for the PRF when selected along with
+ * the TLS-1.2 protocol version.
+ */
+ br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+ br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
+
+ /*
+ * Set hash functions for the engine. Required hash functions
+ * depend on the protocol and cipher suite:
+ *
+ * -- TLS 1.0 and 1.1 require both MD5 and SHA-1.
+ * -- With TLS 1.2, cipher suites with a name ending in "SHA384"
+ * require SHA-384.
+ * -- With TLS 1.2, cipher suites with a name ending in "SHA256"
+ * require SHA-256.
+ * -- With TLS 1.2, cipher suites with a name ending in "SHA"
+ * require both SHA-256 and SHA-1.
+ *
+ * Moreover, these hash functions are also used to compute
+ * hashes supporting signatures on the server side (for ECDHE_*
+ * cipher suites), and on the client side (for client
+ * certificates, except in the case of full static ECDH). In TLS
+ * 1.0 and 1.1, SHA-1 (and also MD5) will be used, but with TLS
+ * 1.2 these hash functions are negotiated between client and
+ * server; SHA-256 and/or SHA-384 should be sufficient in
+ * practice.
+ *
+ * Note that with current implementations, SHA-224 and SHA-256
+ * share the same file, so if you use one, you may have the other
+ * one with no additional overhead. Similarly, SHA-384 and SHA-512
+ * share the same implementation code.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_md5_ID, &br_md5_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha1_ID, &br_sha1_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha224_ID, &br_sha224_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha384_ID, &br_sha384_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha512_ID, &br_sha512_vtable);
+
+ /*
+ * Set the cipher suites. All specified cipher suite MUST be
+ * supported, and the relevant algorithms MUST have been
+ * configured (failure to provide needed implementations may
+ * trigger unwanted behaviours like segfaults or overflows).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+
+ /*
+ * Public-key algorithm implementations.
+ *
+ * -- RSA public core ("rsapub") is needed for "RSA" key exchange
+ * (cipher suites whose name starts with TLS_RSA).
+ *
+ * -- RSA signature verification ("rsavrfy") is needed for
+ * "ECDHE_RSA" cipher suites (not ECDH_RSA).
+ *
+ * -- Elliptic curve implementation ("ec") is needed for cipher
+ * suites that use elliptic curves (both "ECDH" and "ECDHE"
+ * cipher suites).
+ *
+ * -- ECDSA signature verification is needed for "ECDHE_ECDSA"
+ * cipher suites (but not for ECDHE_RSA, ECDH_ECDSA or ECDH_RSA).
+ *
+ * Normally, you use the "default" implementations, obtained
+ * through relevant function calls. These functions return
+ * implementations that are deemed "best" for the current
+ * platform, where "best" means "fastest within constant-time
+ * implementations". Selecting the default implementation is a
+ * mixture of compile-time and runtime checks.
+ *
+ * Nevertheless, specific implementations may be selected
+ * explicitly, e.g. to use code which is slower but with a
+ * smaller footprint.
+ *
+ * The RSA code comes in three variants, called "i15", "i31" and
+ * "i32". The "i31" code is somewhat faster than the "i32" code.
+ * Usually, "i31" is faster than "i15", except on some specific
+ * architectures (ARM Cortex M0, M0+, M1 and M3) where the "i15"
+ * should be preferred (the "i15" code is constant-time, while
+ * the "i31" is not, and the "i15" code is faster anyway).
+ *
+ * ECDSA code also comes in "i15" and "i31" variants. As in the
+ * case of RSA, the "i31" code is faster, except on the small
+ * ARM Cortex M, where the "i15" code is faster and safer.
+ *
+ * There are no less than 10 elliptic curve implementations:
+ *
+ * - ec_c25519_i15, ec_c25519_i31, ec_c25519_m15 and ec_c25519_m31
+ * implement Curve25519.
+ *
+ * - ec_p256_m15 and ec_p256_m31 implement NIST curve P-256.
+ *
+ * - ec_prime_i15 and ec_prime_i31 implement NIST curves P-256,
+ * P-384 and P-521.
+ *
+ * - ec_all_m15 is an aggregate implementation that uses
+ * ec_c25519_m15, ec_p256_m15 and ec_prime_i15.
+ *
+ * - ec_all_m31 is an aggregate implementation that uses
+ * ec_c25519_m31, ec_p256_m31 and ec_prime_i31.
+ *
+ * For a given curve, "m15" is faster than "i15" (but possibly
+ * with a larger code footprint) and "m31" is faster than "i31"
+ * (there again with a larger code footprint). For best
+ * performance, use ec_all_m31, except on the small ARM Cortex M
+ * where ec_all_m15 should be used. Referencing the other
+ * implementations directly will result in smaller code, but
+ * support for fewer curves and possibly lower performance.
+ */
+ br_ssl_client_set_default_rsapub(cc);
+ br_ssl_engine_set_default_rsavrfy(&cc->eng);
+ br_ssl_engine_set_default_ecdsa(&cc->eng);
+ /* Alternate: set implementations explicitly.
+ br_ssl_client_set_rsapub(cc, &br_rsa_i31_public);
+ br_ssl_client_set_rsavrfy(cc, &br_rsa_i31_pkcs1_vrfy);
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_all_m31);
+ br_ssl_engine_set_ecdsa(&cc->eng, &br_ecdsa_i31_vrfy_asn1);
+ */
+
+ /*
+ * Record handler:
+ * -- Cipher suites in AES_128_CBC, AES_256_CBC and 3DES_EDE_CBC
+ * need the CBC record handler ("set_cbc").
+ * -- Cipher suites in AES_128_GCM and AES_256_GCM need the GCM
+ * record handler ("set_gcm").
+ * -- Cipher suites in CHACHA20_POLY1305 need the ChaCha20+Poly1305
+ * record handler ("set_chapol").
+ */
+ br_ssl_engine_set_cbc(&cc->eng,
+ &br_sslrec_in_cbc_vtable,
+ &br_sslrec_out_cbc_vtable);
+ br_ssl_engine_set_gcm(&cc->eng,
+ &br_sslrec_in_gcm_vtable,
+ &br_sslrec_out_gcm_vtable);
+ br_ssl_engine_set_chapol(&cc->eng,
+ &br_sslrec_in_chapol_vtable,
+ &br_sslrec_out_chapol_vtable);
+
+ /*
+ * Symmetric encryption:
+ * -- AES_128_CBC and AES_256_CBC require an "aes_cbc" implementation
+ * (actually two implementations, for encryption and decryption).
+ * -- 3DES_EDE_CBC requires a "des_cbc" implementation
+ * (actually two implementations, for encryption and decryption).
+ * -- AES_128_GCM and AES_256_GCM require an "aes_ctr" imeplementation
+ * and also a GHASH implementation.
+ *
+ * Two 3DES implementations are provided:
+ *
+ * des_tab Classical table-based implementation; it is
+ * not constant-time.
+ *
+ * dest_ct Constant-time DES/3DES implementation. It is
+ * slower than des_tab.
+ *
+ * Four AES implementations are provided:
+ *
+ * aes_ct Constant-time AES implementation, for 32-bit
+ * systems.
+ *
+ * aes_ct64 Constant-time AES implementation, for 64-bit
+ * systems. It actually also runs on 32-bit systems,
+ * but, on such systems, it yields larger code and
+ * slightly worse performance. On 64-bit systems,
+ * aes_ct64 is about twice faster than aes_ct for
+ * CTR processing (GCM encryption and decryption),
+ * and for CBC (decryption only).
+ *
+ * aes_small Smallest implementation provided, but also the
+ * slowest, and it is not constant-time. Use it
+ * only if desperate for code size.
+ *
+ * aes_big Classical table-based AES implementation. This
+ * is decently fast and still resonably compact,
+ * but it is not constant-time.
+ *
+ * aes_x86ni Very fast implementation that uses the AES-NI
+ * opcodes on recent x86 CPU. But it may not be
+ * compiled in the library if the compiler or
+ * architecture is not supported; and the CPU
+ * may also not support the opcodes. Selection
+ * functions are provided to test for availability
+ * of the code and the opcodes.
+ *
+ * Whether having constant-time implementations is absolutely
+ * required for security depends on the context (in particular
+ * whether the target architecture actually has cache memory),
+ * and while side-channel analysis for non-constant-time AES
+ * code has been demonstrated in lab conditions, it certainly
+ * does not apply to all actual usages, and it has never been
+ * spotted in the wild. It is still considered cautious to use
+ * constant-time code by default, and to consider the other
+ * implementations only if duly measured performance issues make
+ * it mandatory.
+ */
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_ct_cbcenc_vtable,
+ &br_aes_ct_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_ct_ctr_vtable);
+ /* Alternate: aes_ct64
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_ct64_cbcenc_vtable,
+ &br_aes_ct64_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_ct64_ctr_vtable);
+ */
+ /* Alternate: aes_small
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_small_cbcenc_vtable,
+ &br_aes_small_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_small_ctr_vtable);
+ */
+ /* Alternate: aes_big
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_big_cbcenc_vtable,
+ &br_aes_big_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_big_ctr_vtable);
+ */
+ br_ssl_engine_set_des_cbc(&cc->eng,
+ &br_des_ct_cbcenc_vtable,
+ &br_des_ct_cbcdec_vtable);
+ /* Alternate: des_tab
+ br_ssl_engine_set_des_cbc(&cc->eng,
+ &br_des_tab_cbcenc_vtable,
+ &br_des_tab_cbcdec_vtable);
+ */
+
+ /*
+ * GHASH is needed for AES_128_GCM and AES_256_GCM. Three
+ * implementations are provided:
+ *
+ * ctmul Uses 32-bit multiplications with a 64-bit result.
+ *
+ * ctmul32 Uses 32-bit multiplications with a 32-bit result.
+ *
+ * ctmul64 Uses 64-bit multiplications with a 64-bit result.
+ *
+ * On 64-bit platforms, ctmul64 is the smallest and fastest of
+ * the three. On 32-bit systems, ctmul should be preferred. The
+ * ctmul32 implementation is meant to be used for the specific
+ * 32-bit systems that do not have a 32x32->64 multiplier (i.e.
+ * the ARM Cortex-M0 and Cortex-M0+).
+ *
+ * These implementations are all constant-time as long as the
+ * underlying multiplication opcode is constant-time (which is
+ * true for all modern systems, but not for older architectures
+ * such that ARM9 or 80486).
+ */
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul);
+ /* Alternate: ghash_ctmul32
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul32);
+ */
+ /* Alternate: ghash_ctmul64
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul64);
+ */
+
+#if 0
+ /*
+ * For a client, the normal case is to validate the server
+ * certificate with regards to a set of trust anchors. This
+ * entails using a br_x509_minimal_context structure, configured
+ * with the relevant algorithms, as shown below.
+ *
+ * Alternatively, the client could "know" the intended server
+ * public key through an out-of-band mechanism, in which case
+ * a br_x509_knownkey_context is appropriate, for a much reduced
+ * code footprint.
+ *
+ * We assume here that the following extra parameters have been
+ * provided:
+ *
+ * xc engine context (br_x509_minimal_context *)
+ * trust_anchors trust anchors (br_x509_trust_anchor *)
+ * trust_anchors_num number of trust anchors (size_t)
+ */
+
+ /*
+ * The X.509 engine needs a hash function for processing the
+ * subject and issuer DN of certificates and trust anchors. Any
+ * supported hash function is appropriate; here we use SHA-256.
+ * The trust an
+ */
+ br_x509_minimal_init(xc, &br_sha256_vtable,
+ trust_anchors, trust_anchors_num);
+
+ /*
+ * Set suites and asymmetric crypto implementations. We use the
+ * "i31" code for RSA (it is somewhat faster than the "i32"
+ * implementation). These implementations are used for
+ * signature verification on certificates, but not for the
+ * SSL-specific usage of the server's public key. For instance,
+ * if the server has an EC public key but the rest of the chain
+ * (intermediate CA, root...) use RSA, then you would need only
+ * the RSA verification function below.
+ */
+ br_x509_minimal_set_rsa(xc, &br_rsa_i31_pkcs1_vrfy);
+ br_x509_minimal_set_ecdsa(xc,
+ &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
+
+ /*
+ * Set supported hash functions. These are for signatures on
+ * certificates. There again, you only need the hash functions
+ * that are actually used in certificates, but if a given
+ * function was included for the SSL engine, you may as well
+ * add it here.
+ *
+ * Note: the engine explicitly rejects signatures that use MD5.
+ * Thus, there is no need for MD5 here.
+ */
+ br_ssl_engine_set_hash(xc, br_sha1_ID, &br_sha1_vtable);
+ br_ssl_engine_set_hash(xc, br_sha224_ID, &br_sha224_vtable);
+ br_ssl_engine_set_hash(xc, br_sha256_ID, &br_sha256_vtable);
+ br_ssl_engine_set_hash(xc, br_sha384_ID, &br_sha384_vtable);
+ br_ssl_engine_set_hash(xc, br_sha512_ID, &br_sha512_vtable);
+
+ /*
+ * Link the X.509 engine in the SSL engine.
+ */
+ br_ssl_engine_set_x509(&cc->eng, &xc->vtable);
+#endif
+}
+
+/*
+ * Example server profile. Most of it is shared with the client
+ * profile, so see the comments in the client function for details.
+ *
+ * This example function assumes a server with a (unique) RSA private
+ * key, so the list of cipher suites is trimmed down for RSA.
+ */
+void
+example_server_profile(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
+ };
+
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
+
+ br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+ br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
+
+ /*
+ * Apart from the requirements listed in the client side, these
+ * hash functions are also used by the server to compute its
+ * signature on ECDHE parameters. Which functions are needed
+ * depends on what the client may support; furthermore, the
+ * client may fail to send the relevant extension, in which
+ * case the server will default to whatever it can (as per the
+ * standard, it should be SHA-1 in that case).
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_md5_ID, &br_md5_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha1_ID, &br_sha1_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha224_ID, &br_sha224_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha384_ID, &br_sha384_vtable);
+ br_ssl_engine_set_hash(&cc->eng, br_sha512_ID, &br_sha512_vtable);
+
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+
+ /*
+ * Elliptic curve implementation is used for ECDHE suites (but
+ * not for ECDH).
+ */
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_prime_i31);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations. Here, we indicate that the RSA
+ * private key is fit for both signing and decrypting, and we
+ * provide the two relevant implementations.
+
+ * BR_KEYTYPE_KEYX allows TLS_RSA_*, BR_KEYTYPE_SIGN allows
+ * TLS_ECDHE_RSA_*.
+ */
+ br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ br_rsa_i31_private, br_rsa_i31_pkcs1_sign);
+ /*
+ * If the server used an EC private key, this call would look
+ * like this:
+
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ cert_issuer_key_type,
+ &br_ec_prime_i31, br_ecdsa_i31_sign_asn1);
+
+ * Note the tricky points:
+ *
+ * -- "ECDH" cipher suites use only the EC code (&br_ec_prime_i31);
+ * the ECDHE_ECDSA cipher suites need both the EC code and
+ * the ECDSA signature implementation.
+ *
+ * -- For "ECDH" (not "ECDHE") cipher suites, the engine must
+ * know the key type (RSA or EC) for the intermediate CA that
+ * issued the server's certificate; this is an artefact of
+ * how the protocol is defined. BearSSL won't try to decode
+ * the server's certificate to obtain that information (it
+ * could do that, the code is there, but it would increase the
+ * footprint). So this must be provided by the caller.
+ *
+ * -- BR_KEYTYPE_KEYX allows ECDH, BR_KEYTYPE_SIGN allows
+ * ECDHE_ECDSA.
+ */
+
+ br_ssl_engine_set_cbc(&cc->eng,
+ &br_sslrec_in_cbc_vtable,
+ &br_sslrec_out_cbc_vtable);
+ br_ssl_engine_set_gcm(&cc->eng,
+ &br_sslrec_in_gcm_vtable,
+ &br_sslrec_out_gcm_vtable);
+
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_ct_cbcenc_vtable,
+ &br_aes_ct_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_ct_ctr_vtable);
+ /* Alternate: aes_ct64
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_ct64_cbcenc_vtable,
+ &br_aes_ct64_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_ct64_ctr_vtable);
+ */
+ /* Alternate: aes_small
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_small_cbcenc_vtable,
+ &br_aes_small_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_small_ctr_vtable);
+ */
+ /* Alternate: aes_big
+ br_ssl_engine_set_aes_cbc(&cc->eng,
+ &br_aes_big_cbcenc_vtable,
+ &br_aes_big_cbcdec_vtable);
+ br_ssl_engine_set_aes_ctr(&cc->eng,
+ &br_aes_big_ctr_vtable);
+ */
+ br_ssl_engine_set_des_cbc(&cc->eng,
+ &br_des_ct_cbcenc_vtable,
+ &br_des_ct_cbcdec_vtable);
+ /* Alternate: des_tab
+ br_ssl_engine_set_des_cbc(&cc->eng,
+ &br_des_tab_cbcenc_vtable,
+ &br_des_tab_cbcdec_vtable);
+ */
+
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul);
+ /* Alternate: ghash_ctmul32
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul32);
+ */
+ /* Alternate: ghash_ctmul64
+ br_ssl_engine_set_ghash(&cc->eng,
+ &br_ghash_ctmul64);
+ */
+}
diff --git a/test/monniaux/BearSSL/samples/key-ec.h b/test/monniaux/BearSSL/samples/key-ec.h
new file mode 100644
index 00000000..c9d0fa4f
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-ec.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * The private key for the server certificate (EC).
+ */
+
+static const unsigned char EC_X[] = {
+ 0x03, 0x91, 0x5B, 0x42, 0x06, 0x90, 0x73, 0x91, 0x1B, 0x48, 0xEF, 0x08,
+ 0xFB, 0xB5, 0xAD, 0x75, 0x65, 0xF9, 0xE6, 0xF7, 0x21, 0x47, 0x62, 0x48,
+ 0xFA, 0x3F, 0x97, 0x7B, 0x70, 0x9D, 0x86, 0xA5
+};
+
+static const br_ec_private_key EC = {
+ 23,
+ (unsigned char *)EC_X, sizeof EC_X
+};
diff --git a/test/monniaux/BearSSL/samples/key-ee-ec.pem b/test/monniaux/BearSSL/samples/key-ee-ec.pem
new file mode 100644
index 00000000..2e6a1363
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-ee-ec.pem
@@ -0,0 +1,5 @@
+-----BEGIN EC PRIVATE KEY-----
+MHcCAQEEIAORW0IGkHORG0jvCPu1rXVl+eb3IUdiSPo/l3twnYaloAoGCCqGSM49AwEHoUQDQgAE
+Xzidp/9Niq/2NDlGGvw63/Qjqqnq+8UI3gCOvnmlN1hMbd0ByqtH34m2xxcfOPwdIBTdRcDgj5NO
+OAv86ZmhSQ==
+-----END EC PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-ee-rsa.pem b/test/monniaux/BearSSL/samples/key-ee-rsa.pem
new file mode 100644
index 00000000..e8b22b40
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-ee-rsa.pem
@@ -0,0 +1,23 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEA1HodJ7orOmeykWr754NEyu0cda3dTYNi1qpolbIkIXsVriqZaBXtZvC4WOfT
+9S7G2Spe5w4u5/xnWcDIYX1LpG/dn9nIhYdkx7oaDynUlqh4mmtiIKky0O6pjChhR6JQKmP2Id7a
+2NXwf8UAgnDmo79ciSdPUZJ3A8OwzC477CPyL1NBr4mT/9KAsUOX3tYZoJISej1meeHBvOF3cKKL
+PUaEUz/kTkJBN5IeH/04s/fvhzmA01bP9OAT3mSwcqQDhMRB7W/6PuLKBCDS19wsgit64m2hHEjb
+z4lPNJc9KKhT2ufB4XMVozB2f48jQhQ9UTTSWq08m8vI/n9ujkDzvQIDAQABAoIBADzb40jzQpl+
+hT+wsIl96HDlXI76Z1Zh6SgKdF1YQpASdMHHstwE19Rx46OXd3cVWGBwifFNdzL8cU/cb6i43jcx
+0X2NQCm6/6tTi05HkXw7shus4VTwkb0VdxvNnxuJCsQxkJjf/7g3AyVdtIkoNG+3ipZAW7BGLu+1
+mAjLv18h4LniI8JGWQK75z8xNQzEdOFVyqC1PujjC5emesxlRW8Ks+MzQ+20WTpHGkLAoLJZJ1XE
+bDQqFbkRXu4imKLtA4iyBrEpXEKZR85tVO+NQOGdPyH/ugK23nSDcJlI2PTHSlGABTpZZUmigO4O
+b8lppU7cFa2JemKG3kEj0BWDOWMCgYEA+fYynFHXcbJi7YEk2vapLMMtVZSVUeU2Ep4uH47YIiJk
+XqP8YPAU3BBIb08afcw3Iyd2tjGq3nDJ7KsKUPHqeXl0vjurLmOXom8KRXvXbNJtG3AxA68miyjF
++ElnRUHx0zUFJyp5IdoGtj2i6DxA+m/E/PXEBeuaMapAfl7uIlsCgYEA2Zwa3JRR9sGW2g4RPzco
+ejOwxL7faCvTHGVnejyvWVCrKTYXORVxl2LdzSXujf8mj3Ehvo+chU464STH4Urf0GCzxEQurHMW
+XwfJOnNe2pvu4rSpPTMUe+6n1Kz3U+Y+8IVXTIuWG93XNvyJN1l1lnWLLvcELSmJ2befcTvi7ccC
+gYEA5PwCLyvWRwTZFaRaI/EU17nRHPYpuEVXPMUFkclk/BgvhHeLay5knZiZEscPiLB8zkqHuK5V
+TsNaZ+HkaHTFjRSTuvWkgrGfpqE8cpzZo4o9g4ZKkIpyr8bhXOu5nDumEgsfNlr1bupxfZ+HTmJs
+UD/14JowQhAsSFUkEeBbHMMCgYEAjazgoDPAmVK4kAcQm4Ohys3UjINomD3QGHC8ygywbQnkJdSd
+kgCwD8vCdEn54mD4DfOt8I83bGLeWq7Do55H0TbkUyfA622SZxR+optyagmToe3VMY8MCxP6GLDz
+5Z/F4notuBw5ArOP5rDL9Uk9EVQ95bnU8kJVCXZPTD2dJQkCgYByDKfPBpVp9HUgNAPgz5pRk/VC
+LvKFvs5POLWMoplC871lOOI0PyGd9b2zv3M8GN728H+hwlXyOOkOHjHn21HFcY1ncTqfVVJg7kX2
+CJiBt3sv8pZ9c9Cmq6qDSUE1qZBnztO5c1SqhACIiJAdhpvluM6JChtHYjHCP8OMhgk8hg==
+-----END RSA PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-ica-ec.pem b/test/monniaux/BearSSL/samples/key-ica-ec.pem
new file mode 100644
index 00000000..74430980
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-ica-ec.pem
@@ -0,0 +1,5 @@
+-----BEGIN EC PRIVATE KEY-----
+MHcCAQEEIEJOkvo4MZ9N+wlTodEzmWor1RauybudPe92K15viDbzoAoGCCqGSM49AwEHoUQDQgAE
+cC6SggEXbG2r4dFjCUhJ0qY1UtM8c7uyiDeYh/GN4Oxlmg4T9e2RYci2bTOEbq6OVYDNSZ4Hv9Cu
+nebQsycWoQ==
+-----END EC PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-ica-rsa.pem b/test/monniaux/BearSSL/samples/key-ica-rsa.pem
new file mode 100644
index 00000000..2e38e340
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-ica-rsa.pem
@@ -0,0 +1,23 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEowIBAAKCAQEAs+hrr5wWUuOBDFCrJc7MDcfyH39Q3yxcNdZiLmMnQafkU6hLJ/oTkaP6CUov
+O17Pd7OKwc1JlZx1DWR07+TXS7mhm2jSMHFI6vdLFN8/R6nYu+yPKMz637QflHyW/AgFKPno9C8v
+7mKcijrghVhgtg8tMLTAQVSRTB9frfEZ8MAipn3YP3k0WUJ7W7VBxGR/Us88NyKhL3kllCRBwj/6
+x3X7SLUNGKf0VPMubthDWMSrUOgFrZG2HgF1s1Sc3qCZFfus8VyXSVHM71gSb3NrszQUAQ9anfqq
+1pPT4urDq7xO7cxRobj4lLa0LKiGKx/2UUMpUl4TibNqeGBOTsAbpQIDAQABAoIBAAtBpQ85SGpK
+QsZG+9ZjQIAyPPN8j05PY7uYnM8DNC8W9qHHW2B2dKf9pwTSx+7CiV+Xc7yZgBukzOwYF3r1CgV6
+aWKkZdZTGDlfXKrDJx3wQhfL/s8SODYr+nfbbcT6KXx9WnaAx1J2iA3cDjU5qN9rRqwP+yF7TZYC
+NoXXGoTmHb7gj1H1IJNdiUgG0netXpM2HydRLzwHDweKav6sm4V4TTeHPYokVp8W7lL7xRhwQXVD
+lcE1noY5NczXV88k9n0EbqGDwX3pJoDC/xWzBFh/+oupxotM4KY2dKJGmtKTmiIef3u43CPv5SYh
+V2OcEXn+joV5qHGhg6yN+ri0/ucCgYEA4x2NBFNtbpx3lmc2r2hwfbawcTVKKlxqJWdoG2bd7HQz
+bO+uqtxhLOxEou4S9uPUVylvpNlDs3xQ+DF9p8oTo5XlHvA6DT/cAjWSQ+MRHqCV30T0lDSd0McZ
+4PaV1nq94bGuPVrGe/Dex5dL+VVhn2tpoqwudsyx5hHA8hFE1zcCgYEAysnhrQa1HZQ0hOaA2c/e
+A8I47dEIyGi4N6uJVBUMM1ecKifs4FKybOtI+XM3mwuAXUyVQGi2SZBBpE4bN3oUDWGwYF7SAJV0
+JJHpBxlZBvYRC7Vfh8HFPYIQCynP0Q4BeQVDaCPW3puJsTWnn2lNhT/PsSm3v76JwTRrBoqaGgMC
+gYAwyyiAxWu9V+BZb9NP3CBO4fEGYWyNrU0gvBahzHfhVRW3Ucc07iPygtA8MOniIRB9qWlTAVqK
+NSswJ3HXmpKdkpanDvVp405hKyFBdIc5DUclsKrbLHK7aAsnSdLnQXeKBaJpjBcYiadTOi4YYz+W
+AH2xdUyGOXP++dF6MDuaAQKBgQCtWyHygW5pR94R0t9J1FpuCiYSn4ULlgINjTXLzGZuqbGVlCX6
+qpdfZ1At92IMyCtHFwXsVtemUYzcAe1gYpsryVw3Njf+ScVM0fNMn02tFsQBp15wNqT/7OT8NhUz
+GO8HXwl9yE2SZZKzDDQsoZ+kjqVlRU2QvDkVElN/9xK/swKBgDz6m4XHvhvITo2bd8/i4FxTN1gr
+napgAc2DHOzCmeyYCjKvZAJDLeNleni1kj1UfrzVrH2BFM38asRJu8xPI4WjfH1fFktNjXVu6Ctk
+YFu0/0TlWkzowZHmE/K0M0tuJLt42m/DLW8deMyl38x+PUPUZb4OdVAKAua/voOMZtQw
+-----END RSA PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-root-ec.pem b/test/monniaux/BearSSL/samples/key-root-ec.pem
new file mode 100644
index 00000000..e76db90c
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-root-ec.pem
@@ -0,0 +1,5 @@
+-----BEGIN EC PRIVATE KEY-----
+MHcCAQEEINx41f6h/utlj52XNFbIGJdkF3qZy1/g9hH2/yV4t/tnoAoGCCqGSM49AwEHoUQDQgAE
+cXS6q7kwLoHV5Vf58yBoDJz5ZNu0IA1t6kDQSm5C/baaaCVE9t97xPze3Xu7xdt8dj9BZkBu26eH
+wuXYxfN/jQ==
+-----END EC PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-root-rsa.pem b/test/monniaux/BearSSL/samples/key-root-rsa.pem
new file mode 100644
index 00000000..82a2eb03
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-root-rsa.pem
@@ -0,0 +1,23 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEAttk01FD9s696c/HOOL9db0Xh/U6xmMZggybSF9HFt5qjwd5jOZec8F5cyBwX
+uYgZbfC2LjBQoVRuk8DbzzDLnx4nefHDmVI1qj2237CtfMtJzcDt52YQKunOKB8hUPp3TC3a7zxY
+606/zun7Gtqjg6PNo8qTgNza8xfMeqszgJyy1H9GP8U83GGUtycpbiq8Wwk21MY7Deu+ztsdHLwQ
+anFxs/LKKJp38orsQu+xSo7i8hoyKs3ApkYsmsKFN5F/RqGTgaF0Zt+6szkgkZP6HaGohefk+Qf2
+EPaoJwG2fxLDQMPJ4rCrSRg6ZLZZt5W1ljbfImmqcmpUTicpow6XFQIDAQABAoIBAENjavKDBPWn
+yygXKqtEb/GWHlNmoNKO7jv33z9TGxzvW8IULZqos3jtNiG0JNRGgiTALcx5FwZWYUiIMBq8v5bd
+nKv3O+DyaP/crdzkNxRCsekoXSXGuleugsHLs1IudTA4yDMamSTkCZH/LwH3KYNXJ+9hNhqsiu9D
+yqM9HIad2k/hmVFv4NDdH3VWqsQJFfrGEXgKRJKNpuzKeST3oo4iLcwH9NGiKXfjW23ekOiLYFwZ
+q8Yd6tOgNLpGuDWk9rlXPJALyXFZ/3UXZaKU5f0ZRfPfMQgM8toeWeWJS0FW7g6aLCJATsfOu/II
+j3nTVCPryvGF4mwe/y+Rgbd67xMCgYEA3Wpiac27hvbLzwbSTWYsKql5uKV0HTim3haNDuzMHJVY
+ZRJSuTY+rZBoaiuDRKMCMI3dQM8AZUJfHQclyTxjtn17Ig8cTKMN6Wr2MRTBplCq3wNLVjbOPAE2
+cv74gUfgpRLdlyueYO7j6PcTS+l41+wB1A/zAEm346Eoxoztt5cCgYEA02ipYQFX4T+8BSkcEyzP
+qeMT9d3BLPm4tTeq2sXRgcfEbF/ENcAX9OfPSEx4kVtzsfJXma5/dg0fuVgl1hO5WLBT+L6MJ/A2
+sGX3Y7x+doXI0xcNd/Epr7oWqZuCO7477vzJcNtud/KQJj2EmRNaHKKtaohqO5dnNhsM540lnDMC
+gYBU40KT2eJ5ngkJeE4Mio2IVa1rE1PvGBcxsmemPzcKBl/7cAjzJU7mcCT3/3K2T+C5CMq43CQE
+rmuUz3a3LkX0YytgJXbuEt10jiORMaoEv4yjL7okdaKf8r8TW5mexxXjc9Ys7PYtp6kNWhy1z+8a
+qUsSKIM7qwerZ9AgP0usRQKBgQDEHy4y/coG/tdweii/aSzlT/Huf2B8VtaR1yi7eBTaLvb8CwO9
+UY1n970GN1sKjiqQhF/cBFPesmIh0bKYHQgvTLU555uiWWiC0LVmYzF2xrn9ij9GbAXeLeZkRg3V
+Wq/DD+PYvNiIkhBESYG/eIJ6WjhCwna6/cQUH5gjH4AqnQKBgQCsKyjLtx+FeFu7PZ8AgCGWRVvs
+suxB74pxlMqMgBbBHyAGQOTTuaaTr/gwNXAAErAXS1X9VsAcAkM/yFv19mdZmJQG9vUMOe48AZDB
+c7xt3t3Ul71eAlbAYkrQGtq64yO3w7ny4C7GUVqMX03XwKToCrXqeKfGwrZGNP+lgZYrbw==
+-----END RSA PRIVATE KEY-----
diff --git a/test/monniaux/BearSSL/samples/key-rsa.h b/test/monniaux/BearSSL/samples/key-rsa.h
new file mode 100644
index 00000000..8de538d9
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/key-rsa.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "bearssl.h"
+
+/*
+ * The private key for the server certificate (RSA).
+ */
+
+static const unsigned char RSA_P[] = {
+ 0xF9, 0xF6, 0x32, 0x9C, 0x51, 0xD7, 0x71, 0xB2, 0x62, 0xED, 0x81, 0x24,
+ 0xDA, 0xF6, 0xA9, 0x2C, 0xC3, 0x2D, 0x55, 0x94, 0x95, 0x51, 0xE5, 0x36,
+ 0x12, 0x9E, 0x2E, 0x1F, 0x8E, 0xD8, 0x22, 0x22, 0x64, 0x5E, 0xA3, 0xFC,
+ 0x60, 0xF0, 0x14, 0xDC, 0x10, 0x48, 0x6F, 0x4F, 0x1A, 0x7D, 0xCC, 0x37,
+ 0x23, 0x27, 0x76, 0xB6, 0x31, 0xAA, 0xDE, 0x70, 0xC9, 0xEC, 0xAB, 0x0A,
+ 0x50, 0xF1, 0xEA, 0x79, 0x79, 0x74, 0xBE, 0x3B, 0xAB, 0x2E, 0x63, 0x97,
+ 0xA2, 0x6F, 0x0A, 0x45, 0x7B, 0xD7, 0x6C, 0xD2, 0x6D, 0x1B, 0x70, 0x31,
+ 0x03, 0xAF, 0x26, 0x8B, 0x28, 0xC5, 0xF8, 0x49, 0x67, 0x45, 0x41, 0xF1,
+ 0xD3, 0x35, 0x05, 0x27, 0x2A, 0x79, 0x21, 0xDA, 0x06, 0xB6, 0x3D, 0xA2,
+ 0xE8, 0x3C, 0x40, 0xFA, 0x6F, 0xC4, 0xFC, 0xF5, 0xC4, 0x05, 0xEB, 0x9A,
+ 0x31, 0xAA, 0x40, 0x7E, 0x5E, 0xEE, 0x22, 0x5B
+};
+
+static const unsigned char RSA_Q[] = {
+ 0xD9, 0x9C, 0x1A, 0xDC, 0x94, 0x51, 0xF6, 0xC1, 0x96, 0xDA, 0x0E, 0x11,
+ 0x3F, 0x37, 0x28, 0x7A, 0x33, 0xB0, 0xC4, 0xBE, 0xDF, 0x68, 0x2B, 0xD3,
+ 0x1C, 0x65, 0x67, 0x7A, 0x3C, 0xAF, 0x59, 0x50, 0xAB, 0x29, 0x36, 0x17,
+ 0x39, 0x15, 0x71, 0x97, 0x62, 0xDD, 0xCD, 0x25, 0xEE, 0x8D, 0xFF, 0x26,
+ 0x8F, 0x71, 0x21, 0xBE, 0x8F, 0x9C, 0x85, 0x4E, 0x3A, 0xE1, 0x24, 0xC7,
+ 0xE1, 0x4A, 0xDF, 0xD0, 0x60, 0xB3, 0xC4, 0x44, 0x2E, 0xAC, 0x73, 0x16,
+ 0x5F, 0x07, 0xC9, 0x3A, 0x73, 0x5E, 0xDA, 0x9B, 0xEE, 0xE2, 0xB4, 0xA9,
+ 0x3D, 0x33, 0x14, 0x7B, 0xEE, 0xA7, 0xD4, 0xAC, 0xF7, 0x53, 0xE6, 0x3E,
+ 0xF0, 0x85, 0x57, 0x4C, 0x8B, 0x96, 0x1B, 0xDD, 0xD7, 0x36, 0xFC, 0x89,
+ 0x37, 0x59, 0x75, 0x96, 0x75, 0x8B, 0x2E, 0xF7, 0x04, 0x2D, 0x29, 0x89,
+ 0xD9, 0xB7, 0x9F, 0x71, 0x3B, 0xE2, 0xED, 0xC7
+};
+
+static const unsigned char RSA_DP[] = {
+ 0xE4, 0xFC, 0x02, 0x2F, 0x2B, 0xD6, 0x47, 0x04, 0xD9, 0x15, 0xA4, 0x5A,
+ 0x23, 0xF1, 0x14, 0xD7, 0xB9, 0xD1, 0x1C, 0xF6, 0x29, 0xB8, 0x45, 0x57,
+ 0x3C, 0xC5, 0x05, 0x91, 0xC9, 0x64, 0xFC, 0x18, 0x2F, 0x84, 0x77, 0x8B,
+ 0x6B, 0x2E, 0x64, 0x9D, 0x98, 0x99, 0x12, 0xC7, 0x0F, 0x88, 0xB0, 0x7C,
+ 0xCE, 0x4A, 0x87, 0xB8, 0xAE, 0x55, 0x4E, 0xC3, 0x5A, 0x67, 0xE1, 0xE4,
+ 0x68, 0x74, 0xC5, 0x8D, 0x14, 0x93, 0xBA, 0xF5, 0xA4, 0x82, 0xB1, 0x9F,
+ 0xA6, 0xA1, 0x3C, 0x72, 0x9C, 0xD9, 0xA3, 0x8A, 0x3D, 0x83, 0x86, 0x4A,
+ 0x90, 0x8A, 0x72, 0xAF, 0xC6, 0xE1, 0x5C, 0xEB, 0xB9, 0x9C, 0x3B, 0xA6,
+ 0x12, 0x0B, 0x1F, 0x36, 0x5A, 0xF5, 0x6E, 0xEA, 0x71, 0x7D, 0x9F, 0x87,
+ 0x4E, 0x62, 0x6C, 0x50, 0x3F, 0xF5, 0xE0, 0x9A, 0x30, 0x42, 0x10, 0x2C,
+ 0x48, 0x55, 0x24, 0x11, 0xE0, 0x5B, 0x1C, 0xC3
+};
+
+static const unsigned char RSA_DQ[] = {
+ 0x8D, 0xAC, 0xE0, 0xA0, 0x33, 0xC0, 0x99, 0x52, 0xB8, 0x90, 0x07, 0x10,
+ 0x9B, 0x83, 0xA1, 0xCA, 0xCD, 0xD4, 0x8C, 0x83, 0x68, 0x98, 0x3D, 0xD0,
+ 0x18, 0x70, 0xBC, 0xCA, 0x0C, 0xB0, 0x6D, 0x09, 0xE4, 0x25, 0xD4, 0x9D,
+ 0x92, 0x00, 0xB0, 0x0F, 0xCB, 0xC2, 0x74, 0x49, 0xF9, 0xE2, 0x60, 0xF8,
+ 0x0D, 0xF3, 0xAD, 0xF0, 0x8F, 0x37, 0x6C, 0x62, 0xDE, 0x5A, 0xAE, 0xC3,
+ 0xA3, 0x9E, 0x47, 0xD1, 0x36, 0xE4, 0x53, 0x27, 0xC0, 0xEB, 0x6D, 0x92,
+ 0x67, 0x14, 0x7E, 0xA2, 0x9B, 0x72, 0x6A, 0x09, 0x93, 0xA1, 0xED, 0xD5,
+ 0x31, 0x8F, 0x0C, 0x0B, 0x13, 0xFA, 0x18, 0xB0, 0xF3, 0xE5, 0x9F, 0xC5,
+ 0xE2, 0x7A, 0x2D, 0xB8, 0x1C, 0x39, 0x02, 0xB3, 0x8F, 0xE6, 0xB0, 0xCB,
+ 0xF5, 0x49, 0x3D, 0x11, 0x54, 0x3D, 0xE5, 0xB9, 0xD4, 0xF2, 0x42, 0x55,
+ 0x09, 0x76, 0x4F, 0x4C, 0x3D, 0x9D, 0x25, 0x09
+};
+
+static const unsigned char RSA_IQ[] = {
+ 0x72, 0x0C, 0xA7, 0xCF, 0x06, 0x95, 0x69, 0xF4, 0x75, 0x20, 0x34, 0x03,
+ 0xE0, 0xCF, 0x9A, 0x51, 0x93, 0xF5, 0x42, 0x2E, 0xF2, 0x85, 0xBE, 0xCE,
+ 0x4F, 0x38, 0xB5, 0x8C, 0xA2, 0x99, 0x42, 0xF3, 0xBD, 0x65, 0x38, 0xE2,
+ 0x34, 0x3F, 0x21, 0x9D, 0xF5, 0xBD, 0xB3, 0xBF, 0x73, 0x3C, 0x18, 0xDE,
+ 0xF6, 0xF0, 0x7F, 0xA1, 0xC2, 0x55, 0xF2, 0x38, 0xE9, 0x0E, 0x1E, 0x31,
+ 0xE7, 0xDB, 0x51, 0xC5, 0x71, 0x8D, 0x67, 0x71, 0x3A, 0x9F, 0x55, 0x52,
+ 0x60, 0xEE, 0x45, 0xF6, 0x08, 0x98, 0x81, 0xB7, 0x7B, 0x2F, 0xF2, 0x96,
+ 0x7D, 0x73, 0xD0, 0xA6, 0xAB, 0xAA, 0x83, 0x49, 0x41, 0x35, 0xA9, 0x90,
+ 0x67, 0xCE, 0xD3, 0xB9, 0x73, 0x54, 0xAA, 0x84, 0x00, 0x88, 0x88, 0x90,
+ 0x1D, 0x86, 0x9B, 0xE5, 0xB8, 0xCE, 0x89, 0x0A, 0x1B, 0x47, 0x62, 0x31,
+ 0xC2, 0x3F, 0xC3, 0x8C, 0x86, 0x09, 0x3C, 0x86
+};
+
+static const br_rsa_private_key RSA = {
+ 2048,
+ (unsigned char *)RSA_P, sizeof RSA_P,
+ (unsigned char *)RSA_Q, sizeof RSA_Q,
+ (unsigned char *)RSA_DP, sizeof RSA_DP,
+ (unsigned char *)RSA_DQ, sizeof RSA_DQ,
+ (unsigned char *)RSA_IQ, sizeof RSA_IQ
+};
diff --git a/test/monniaux/BearSSL/samples/server_basic.c b/test/monniaux/BearSSL/samples/server_basic.c
new file mode 100644
index 00000000..e774c4d2
--- /dev/null
+++ b/test/monniaux/BearSSL/samples/server_basic.c
@@ -0,0 +1,436 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+#include "bearssl.h"
+
+/*
+ * This sample code can use three possible certificate chains:
+ * -- A full-RSA chain (server key is RSA, certificates are signed with RSA)
+ * -- A full-EC chain (server key is EC, certificates are signed with ECDSA)
+ * -- A mixed chain (server key is EC, certificates are signed with RSA)
+ *
+ * The macros below define which chain is selected. This impacts the list
+ * of supported cipher suites.
+ *
+ * Other macros, which can be defined (with a non-zero value):
+ *
+ * SERVER_PROFILE_MIN_FS
+ * Select a "minimal" profile with forward security (ECDHE cipher
+ * suite).
+ *
+ * SERVER_PROFILE_MIN_NOFS
+ * Select a "minimal" profile without forward security (RSA or ECDH
+ * cipher suite, but not ECDHE).
+ *
+ * SERVER_CHACHA20
+ * If SERVER_PROFILE_MIN_FS is selected, then this macro selects
+ * a cipher suite with ChaCha20+Poly1305; otherwise, AES/GCM is
+ * used. This macro has no effect otherwise, since there is no
+ * non-forward secure cipher suite that uses ChaCha20+Poly1305.
+ */
+
+#if !(SERVER_RSA || SERVER_EC || SERVER_MIXED)
+#define SERVER_RSA 1
+#define SERVER_EC 0
+#define SERVER_MIXED 0
+#endif
+
+#if SERVER_RSA
+#include "chain-rsa.h"
+#include "key-rsa.h"
+#define SKEY RSA
+#elif SERVER_EC
+#include "chain-ec.h"
+#include "key-ec.h"
+#define SKEY EC
+#elif SERVER_MIXED
+#include "chain-ec+rsa.h"
+#include "key-ec.h"
+#define SKEY EC
+#else
+#error Must use one of RSA, EC or MIXED chains.
+#endif
+
+/*
+ * Create a server socket bound to the specified host and port. If 'host'
+ * is NULL, this will bind "generically" (all addresses).
+ *
+ * Returned value is the server socket descriptor, or -1 on error.
+ */
+static int
+host_bind(const char *host, const char *port)
+{
+ struct addrinfo hints, *si, *p;
+ int fd;
+ int err;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ err = getaddrinfo(host, port, &hints, &si);
+ if (err != 0) {
+ fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
+ gai_strerror(err));
+ return -1;
+ }
+ fd = -1;
+ for (p = si; p != NULL; p = p->ai_next) {
+ struct sockaddr *sa;
+ struct sockaddr_in sa4;
+ struct sockaddr_in6 sa6;
+ size_t sa_len;
+ void *addr;
+ char tmp[INET6_ADDRSTRLEN + 50];
+ int opt;
+
+ sa = (struct sockaddr *)p->ai_addr;
+ if (sa->sa_family == AF_INET) {
+ sa4 = *(struct sockaddr_in *)sa;
+ sa = (struct sockaddr *)&sa4;
+ sa_len = sizeof sa4;
+ addr = &sa4.sin_addr;
+ if (host == NULL) {
+ sa4.sin_addr.s_addr = INADDR_ANY;
+ }
+ } else if (sa->sa_family == AF_INET6) {
+ sa6 = *(struct sockaddr_in6 *)sa;
+ sa = (struct sockaddr *)&sa6;
+ sa_len = sizeof sa6;
+ addr = &sa6.sin6_addr;
+ if (host == NULL) {
+ sa6.sin6_addr = in6addr_any;
+ }
+ } else {
+ addr = NULL;
+ sa_len = p->ai_addrlen;
+ }
+ if (addr != NULL) {
+ inet_ntop(p->ai_family, addr, tmp, sizeof tmp);
+ } else {
+ sprintf(tmp, "<unknown family: %d>",
+ (int)sa->sa_family);
+ }
+ fprintf(stderr, "binding to: %s\n", tmp);
+ fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (fd < 0) {
+ perror("socket()");
+ continue;
+ }
+ opt = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
+ opt = 0;
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt);
+ if (bind(fd, sa, sa_len) < 0) {
+ perror("bind()");
+ close(fd);
+ continue;
+ }
+ break;
+ }
+ if (p == NULL) {
+ freeaddrinfo(si);
+ fprintf(stderr, "ERROR: failed to bind\n");
+ return -1;
+ }
+ freeaddrinfo(si);
+ if (listen(fd, 5) < 0) {
+ perror("listen()");
+ close(fd);
+ return -1;
+ }
+ fprintf(stderr, "bound.\n");
+ return fd;
+}
+
+/*
+ * Accept a single client on the provided server socket. This is blocking.
+ * On error, this returns -1.
+ */
+static int
+accept_client(int server_fd)
+{
+ int fd;
+ struct sockaddr sa;
+ socklen_t sa_len;
+ char tmp[INET6_ADDRSTRLEN + 50];
+ const char *name;
+
+ sa_len = sizeof sa;
+ fd = accept(server_fd, &sa, &sa_len);
+ if (fd < 0) {
+ perror("accept()");
+ return -1;
+ }
+ name = NULL;
+ switch (sa.sa_family) {
+ case AF_INET:
+ name = inet_ntop(AF_INET,
+ &((struct sockaddr_in *)&sa)->sin_addr,
+ tmp, sizeof tmp);
+ break;
+ case AF_INET6:
+ name = inet_ntop(AF_INET6,
+ &((struct sockaddr_in6 *)&sa)->sin6_addr,
+ tmp, sizeof tmp);
+ break;
+ }
+ if (name == NULL) {
+ sprintf(tmp, "<unknown: %lu>", (unsigned long)sa.sa_family);
+ name = tmp;
+ }
+ fprintf(stderr, "accepting connection from: %s\n", name);
+ return fd;
+}
+
+/*
+ * Low-level data read callback for the simplified SSL I/O API.
+ */
+static int
+sock_read(void *ctx, unsigned char *buf, size_t len)
+{
+ for (;;) {
+ ssize_t rlen;
+
+ rlen = read(*(int *)ctx, buf, len);
+ if (rlen <= 0) {
+ if (rlen < 0 && errno == EINTR) {
+ continue;
+ }
+ return -1;
+ }
+ return (int)rlen;
+ }
+}
+
+/*
+ * Low-level data write callback for the simplified SSL I/O API.
+ */
+static int
+sock_write(void *ctx, const unsigned char *buf, size_t len)
+{
+ for (;;) {
+ ssize_t wlen;
+
+ wlen = write(*(int *)ctx, buf, len);
+ if (wlen <= 0) {
+ if (wlen < 0 && errno == EINTR) {
+ continue;
+ }
+ return -1;
+ }
+ return (int)wlen;
+ }
+}
+
+/*
+ * Sample HTTP response to send.
+ */
+static const char *HTTP_RES =
+ "HTTP/1.0 200 OK\r\n"
+ "Content-Length: 46\r\n"
+ "Connection: close\r\n"
+ "Content-Type: text/html; charset=iso-8859-1\r\n"
+ "\r\n"
+ "<html>\r\n"
+ "<body>\r\n"
+ "<p>Test!</p>\r\n"
+ "</body>\r\n"
+ "</html>\r\n";
+
+/*
+ * Main program: this is a simple program that expects 1 argument: a
+ * port number. This will start a simple network server on that port,
+ * that expects incoming SSL clients. It handles only one client at a
+ * time (handling several would require threads, sub-processes, or
+ * multiplexing with select()/poll(), all of which being possible).
+ *
+ * For each client, the server will wait for two successive newline
+ * characters (ignoring CR characters, so CR+LF is accepted), then
+ * produce a sample static HTTP response. This is very crude, but
+ * sufficient for explanatory purposes.
+ */
+int
+main(int argc, char *argv[])
+{
+ const char *port;
+ int fd;
+
+ if (argc != 2) {
+ return EXIT_FAILURE;
+ }
+ port = argv[1];
+
+ /*
+ * Ignore SIGPIPE to avoid crashing in case of abrupt socket close.
+ */
+ signal(SIGPIPE, SIG_IGN);
+
+ /*
+ * Open the server socket.
+ */
+ fd = host_bind(NULL, port);
+ if (fd < 0) {
+ return EXIT_FAILURE;
+ }
+
+ /*
+ * Process each client, one at a time.
+ */
+ for (;;) {
+ int cfd;
+ br_ssl_server_context sc;
+ unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];
+ br_sslio_context ioc;
+ int lcwn, err;
+
+ cfd = accept_client(fd);
+ if (cfd < 0) {
+ return EXIT_FAILURE;
+ }
+
+ /*
+ * Initialise the context with the cipher suites and
+ * algorithms. This depends on the server key type
+ * (and, for EC keys, the signature algorithm used by
+ * the CA to sign the server's certificate).
+ *
+ * Depending on the defined macros, we may select one of
+ * the "minimal" profiles. Key exchange algorithm depends
+ * on the key type:
+ * RSA key: RSA or ECDHE_RSA
+ * EC key, cert signed with ECDSA: ECDH_ECDSA or ECDHE_ECDSA
+ * EC key, cert signed with RSA: ECDH_RSA or ECDHE_ECDSA
+ */
+#if SERVER_RSA
+#if SERVER_PROFILE_MIN_FS
+#if SERVER_CHACHA20
+ br_ssl_server_init_mine2c(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_mine2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#endif
+#elif SERVER_PROFILE_MIN_NOFS
+ br_ssl_server_init_minr2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_full_rsa(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#endif
+#elif SERVER_EC
+#if SERVER_PROFILE_MIN_FS
+#if SERVER_CHACHA20
+ br_ssl_server_init_minf2c(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_minf2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#endif
+#elif SERVER_PROFILE_MIN_NOFS
+ br_ssl_server_init_minv2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_full_ec(&sc, CHAIN, CHAIN_LEN,
+ BR_KEYTYPE_EC, &SKEY);
+#endif
+#else /* SERVER_MIXED */
+#if SERVER_PROFILE_MIN_FS
+#if SERVER_CHACHA20
+ br_ssl_server_init_minf2c(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_minf2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#endif
+#elif SERVER_PROFILE_MIN_NOFS
+ br_ssl_server_init_minu2g(&sc, CHAIN, CHAIN_LEN, &SKEY);
+#else
+ br_ssl_server_init_full_ec(&sc, CHAIN, CHAIN_LEN,
+ BR_KEYTYPE_RSA, &SKEY);
+#endif
+#endif
+ /*
+ * Set the I/O buffer to the provided array. We
+ * allocated a buffer large enough for full-duplex
+ * behaviour with all allowed sizes of SSL records,
+ * hence we set the last argument to 1 (which means
+ * "split the buffer into separate input and output
+ * areas").
+ */
+ br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 1);
+
+ /*
+ * Reset the server context, for a new handshake.
+ */
+ br_ssl_server_reset(&sc);
+
+ /*
+ * Initialise the simplified I/O wrapper context.
+ */
+ br_sslio_init(&ioc, &sc.eng, sock_read, &cfd, sock_write, &cfd);
+
+ /*
+ * Read bytes until two successive LF (or CR+LF) are received.
+ */
+ lcwn = 0;
+ for (;;) {
+ unsigned char x;
+
+ if (br_sslio_read(&ioc, &x, 1) < 0) {
+ goto client_drop;
+ }
+ if (x == 0x0D) {
+ continue;
+ }
+ if (x == 0x0A) {
+ if (lcwn) {
+ break;
+ }
+ lcwn = 1;
+ } else {
+ lcwn = 0;
+ }
+ }
+
+ /*
+ * Write a response and close the connection.
+ */
+ br_sslio_write_all(&ioc, HTTP_RES, strlen(HTTP_RES));
+ br_sslio_close(&ioc);
+
+ client_drop:
+ err = br_ssl_engine_last_error(&sc.eng);
+ if (err == 0) {
+ fprintf(stderr, "SSL closed (correctly).\n");
+ } else {
+ fprintf(stderr, "SSL error: %d\n", err);
+ }
+ close(cfd);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/aead/ccm.c b/test/monniaux/BearSSL/src/aead/ccm.c
new file mode 100644
index 00000000..68cc913e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/aead/ccm.c
@@ -0,0 +1,346 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Implementation Notes
+ * ====================
+ *
+ * The combined CTR + CBC-MAC functions can only handle full blocks,
+ * so some buffering is necessary.
+ *
+ * - 'ptr' contains a value from 0 to 15, which is the number of bytes
+ * accumulated in buf[] that still needs to be processed with the
+ * current CBC-MAC computation.
+ *
+ * - When processing the message itself, CTR encryption/decryption is
+ * also done at the same time. The first 'ptr' bytes of buf[] then
+ * contains the plaintext bytes, while the last '16 - ptr' bytes of
+ * buf[] are the remnants of the stream block, to be used against
+ * the next input bytes, when available. When 'ptr' is 0, the
+ * contents of buf[] are to be ignored.
+ *
+ * - The current counter and running CBC-MAC values are kept in 'ctr'
+ * and 'cbcmac', respectively.
+ */
+
+/* see bearssl_block.h */
+void
+br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx)
+{
+ ctx->bctx = bctx;
+}
+
+/* see bearssl_block.h */
+int
+br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,
+ uint64_t aad_len, uint64_t data_len, size_t tag_len)
+{
+ unsigned char tmp[16];
+ unsigned u, q;
+
+ if (nonce_len < 7 || nonce_len > 13) {
+ return 0;
+ }
+ if (tag_len < 4 || tag_len > 16 || (tag_len & 1) != 0) {
+ return 0;
+ }
+ q = 15 - (unsigned)nonce_len;
+ ctx->tag_len = tag_len;
+
+ /*
+ * Block B0, to start CBC-MAC.
+ */
+ tmp[0] = (aad_len > 0 ? 0x40 : 0x00)
+ | (((unsigned)tag_len - 2) << 2)
+ | (q - 1);
+ memcpy(tmp + 1, nonce, nonce_len);
+ for (u = 0; u < q; u ++) {
+ tmp[15 - u] = (unsigned char)data_len;
+ data_len >>= 8;
+ }
+ if (data_len != 0) {
+ /*
+ * If the data length was not entirely consumed in the
+ * loop above, then it exceeds the maximum limit of
+ * q bytes (when encoded).
+ */
+ return 0;
+ }
+
+ /*
+ * Start CBC-MAC.
+ */
+ memset(ctx->cbcmac, 0, sizeof ctx->cbcmac);
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, tmp, sizeof tmp);
+
+ /*
+ * Assemble AAD length header.
+ */
+ if ((aad_len >> 32) != 0) {
+ ctx->buf[0] = 0xFF;
+ ctx->buf[1] = 0xFF;
+ br_enc64be(ctx->buf + 2, aad_len);
+ ctx->ptr = 10;
+ } else if (aad_len >= 0xFF00) {
+ ctx->buf[0] = 0xFF;
+ ctx->buf[1] = 0xFE;
+ br_enc32be(ctx->buf + 2, (uint32_t)aad_len);
+ ctx->ptr = 6;
+ } else if (aad_len > 0) {
+ br_enc16be(ctx->buf, (unsigned)aad_len);
+ ctx->ptr = 2;
+ } else {
+ ctx->ptr = 0;
+ }
+
+ /*
+ * Make initial counter value and compute tag mask.
+ */
+ ctx->ctr[0] = q - 1;
+ memcpy(ctx->ctr + 1, nonce, nonce_len);
+ memset(ctx->ctr + 1 + nonce_len, 0, q);
+ memset(ctx->tagmask, 0, sizeof ctx->tagmask);
+ (*ctx->bctx)->ctr(ctx->bctx, ctx->ctr,
+ ctx->tagmask, sizeof ctx->tagmask);
+
+ return 1;
+}
+
+/* see bearssl_block.h */
+void
+br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len)
+{
+ const unsigned char *dbuf;
+ size_t ptr;
+
+ dbuf = data;
+
+ /*
+ * Complete partial block, if needed.
+ */
+ ptr = ctx->ptr;
+ if (ptr != 0) {
+ size_t clen;
+
+ clen = (sizeof ctx->buf) - ptr;
+ if (clen > len) {
+ memcpy(ctx->buf + ptr, dbuf, len);
+ ctx->ptr = ptr + len;
+ return;
+ }
+ memcpy(ctx->buf + ptr, dbuf, clen);
+ dbuf += clen;
+ len -= clen;
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
+ ctx->buf, sizeof ctx->buf);
+ }
+
+ /*
+ * Process complete blocks.
+ */
+ ptr = len & 15;
+ len -= ptr;
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, dbuf, len);
+ dbuf += len;
+
+ /*
+ * Copy last partial block in the context buffer.
+ */
+ memcpy(ctx->buf, dbuf, ptr);
+ ctx->ptr = ptr;
+}
+
+/* see bearssl_block.h */
+void
+br_ccm_flip(br_ccm_context *ctx)
+{
+ size_t ptr;
+
+ /*
+ * Complete AAD partial block with zeros, if necessary.
+ */
+ ptr = ctx->ptr;
+ if (ptr != 0) {
+ memset(ctx->buf + ptr, 0, (sizeof ctx->buf) - ptr);
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
+ ctx->buf, sizeof ctx->buf);
+ ctx->ptr = 0;
+ }
+
+ /*
+ * Counter was already set by br_ccm_reset().
+ */
+}
+
+/* see bearssl_block.h */
+void
+br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len)
+{
+ unsigned char *dbuf;
+ size_t ptr;
+
+ dbuf = data;
+
+ /*
+ * Complete a partial block, if any: ctx->buf[] contains
+ * ctx->ptr plaintext bytes (already reported), and the other
+ * bytes are CTR stream output.
+ */
+ ptr = ctx->ptr;
+ if (ptr != 0) {
+ size_t clen;
+ size_t u;
+
+ clen = (sizeof ctx->buf) - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ if (encrypt) {
+ for (u = 0; u < clen; u ++) {
+ unsigned w, x;
+
+ w = ctx->buf[ptr + u];
+ x = dbuf[u];
+ ctx->buf[ptr + u] = x;
+ dbuf[u] = w ^ x;
+ }
+ } else {
+ for (u = 0; u < clen; u ++) {
+ unsigned w;
+
+ w = ctx->buf[ptr + u] ^ dbuf[u];
+ dbuf[u] = w;
+ ctx->buf[ptr + u] = w;
+ }
+ }
+ dbuf += clen;
+ len -= clen;
+ ptr += clen;
+ if (ptr < sizeof ctx->buf) {
+ ctx->ptr = ptr;
+ return;
+ }
+ (*ctx->bctx)->mac(ctx->bctx,
+ ctx->cbcmac, ctx->buf, sizeof ctx->buf);
+ }
+
+ /*
+ * Process all complete blocks. Note that the ctrcbc API is for
+ * encrypt-then-MAC (CBC-MAC is computed over the encrypted
+ * blocks) while CCM uses MAC-and-encrypt (CBC-MAC is computed
+ * over the plaintext blocks). Therefore, we need to use the
+ * _decryption_ function for encryption, and the encryption
+ * function for decryption (this works because CTR encryption
+ * and decryption are identical, so the choice really is about
+ * computing the CBC-MAC before or after XORing with the CTR
+ * stream).
+ */
+ ptr = len & 15;
+ len -= ptr;
+ if (encrypt) {
+ (*ctx->bctx)->decrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
+ dbuf, len);
+ } else {
+ (*ctx->bctx)->encrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
+ dbuf, len);
+ }
+ dbuf += len;
+
+ /*
+ * If there is some remaining data, then we need to compute an
+ * extra block of CTR stream.
+ */
+ if (ptr != 0) {
+ size_t u;
+
+ memset(ctx->buf, 0, sizeof ctx->buf);
+ (*ctx->bctx)->ctr(ctx->bctx, ctx->ctr,
+ ctx->buf, sizeof ctx->buf);
+ if (encrypt) {
+ for (u = 0; u < ptr; u ++) {
+ unsigned w, x;
+
+ w = ctx->buf[u];
+ x = dbuf[u];
+ ctx->buf[u] = x;
+ dbuf[u] = w ^ x;
+ }
+ } else {
+ for (u = 0; u < ptr; u ++) {
+ unsigned w;
+
+ w = ctx->buf[u] ^ dbuf[u];
+ dbuf[u] = w;
+ ctx->buf[u] = w;
+ }
+ }
+ }
+ ctx->ptr = ptr;
+}
+
+/* see bearssl_block.h */
+size_t
+br_ccm_get_tag(br_ccm_context *ctx, void *tag)
+{
+ size_t ptr;
+ size_t u;
+
+ /*
+ * If there is some buffered data, then we need to pad it with
+ * zeros and finish up CBC-MAC.
+ */
+ ptr = ctx->ptr;
+ if (ptr != 0) {
+ memset(ctx->buf + ptr, 0, (sizeof ctx->buf) - ptr);
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
+ ctx->buf, sizeof ctx->buf);
+ }
+
+ /*
+ * XOR the tag mask into the CBC-MAC output.
+ */
+ for (u = 0; u < ctx->tag_len; u ++) {
+ ctx->cbcmac[u] ^= ctx->tagmask[u];
+ }
+ memcpy(tag, ctx->cbcmac, ctx->tag_len);
+ return ctx->tag_len;
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_ccm_check_tag(br_ccm_context *ctx, const void *tag)
+{
+ unsigned char tmp[16];
+ size_t u, tag_len;
+ uint32_t z;
+
+ tag_len = br_ccm_get_tag(ctx, tmp);
+ z = 0;
+ for (u = 0; u < tag_len; u ++) {
+ z |= tmp[u] ^ ((const unsigned char *)tag)[u];
+ }
+ return EQ0(z);
+}
diff --git a/test/monniaux/BearSSL/src/aead/eax.c b/test/monniaux/BearSSL/src/aead/eax.c
new file mode 100644
index 00000000..f0351b01
--- /dev/null
+++ b/test/monniaux/BearSSL/src/aead/eax.c
@@ -0,0 +1,526 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Implementation Notes
+ * ====================
+ *
+ * The combined CTR + CBC-MAC functions can only handle full blocks,
+ * so some buffering is necessary. Moreover, EAX has a special padding
+ * rule for CBC-MAC, which implies that we cannot compute the MAC over
+ * the last received full block until we know whether we are at the
+ * end of the data or not.
+ *
+ * - 'ptr' contains a value from 1 to 16, which is the number of bytes
+ * accumulated in buf[] that still needs to be processed with the
+ * current OMAC computation. Beware that this can go to 16: a
+ * complete block cannot be processed until it is known whether it
+ * is the last block or not. However, it can never be 0, because
+ * OMAC^t works on an input that is at least one-block long.
+ *
+ * - When processing the message itself, CTR encryption/decryption is
+ * also done at the same time. The first 'ptr' bytes of buf[] then
+ * contains the encrypted bytes, while the last '16 - ptr' bytes of
+ * buf[] are the remnants of the stream block, to be used against
+ * the next input bytes, when available.
+ *
+ * - The current counter and running CBC-MAC values are kept in 'ctr'
+ * and 'cbcmac', respectively.
+ *
+ * - The derived keys for padding are kept in L2 and L4 (double and
+ * quadruple of Enc_K(0^n), in GF(2^128), respectively).
+ */
+
+/*
+ * Start an OMAC computation; the first block is the big-endian
+ * representation of the provided value ('val' must fit on one byte).
+ * We make it a delayed block because it may also be the last one,
+ */
+static void
+omac_start(br_eax_context *ctx, unsigned val)
+{
+ memset(ctx->cbcmac, 0, sizeof ctx->cbcmac);
+ memset(ctx->buf, 0, sizeof ctx->buf);
+ ctx->buf[15] = val;
+ ctx->ptr = 16;
+}
+
+/*
+ * Double a value in finite field GF(2^128), defined with modulus
+ * X^128+X^7+X^2+X+1.
+ */
+static void
+double_gf128(unsigned char *dst, const unsigned char *src)
+{
+ unsigned cc;
+ int i;
+
+ cc = 0x87 & -((unsigned)src[0] >> 7);
+ for (i = 15; i >= 0; i --) {
+ unsigned z;
+
+ z = (src[i] << 1) ^ cc;
+ cc = z >> 8;
+ dst[i] = (unsigned char)z;
+ }
+}
+
+/*
+ * Apply padding to the last block, currently in ctx->buf (with
+ * ctx->ptr bytes), and finalize OMAC computation.
+ */
+static void
+do_pad(br_eax_context *ctx)
+{
+ unsigned char *pad;
+ size_t ptr, u;
+
+ ptr = ctx->ptr;
+ if (ptr == 16) {
+ pad = ctx->L2;
+ } else {
+ ctx->buf[ptr ++] = 0x80;
+ memset(ctx->buf + ptr, 0x00, 16 - ptr);
+ pad = ctx->L4;
+ }
+ for (u = 0; u < sizeof ctx->buf; u ++) {
+ ctx->buf[u] ^= pad[u];
+ }
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, ctx->buf, sizeof ctx->buf);
+ KILL_TAIL_CALL();
+}
+
+/*
+ * Apply CBC-MAC on the provided data, with buffering management.
+ *
+ * Upon entry, two situations are acceptable:
+ *
+ * ctx->ptr == 0: there is no data to process in ctx->buf
+ * ctx->ptr == 16: there is a full block of unprocessed data in ctx->buf
+ *
+ * Upon exit, ctx->ptr may be zero only if it was already zero on entry,
+ * and len == 0. In all other situations, ctx->ptr will be non-zero on
+ * exit (and may have value 16).
+ */
+static void
+do_cbcmac_chunk(br_eax_context *ctx, const void *data, size_t len)
+{
+ size_t ptr;
+
+ if (len == 0) {
+ return;
+ }
+ ptr = len & (size_t)15;
+ if (ptr == 0) {
+ len -= 16;
+ ptr = 16;
+ } else {
+ len -= ptr;
+ }
+ if (ctx->ptr == 16) {
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
+ ctx->buf, sizeof ctx->buf);
+ }
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac, data, len);
+ memcpy(ctx->buf, (const unsigned char *)data + len, ptr);
+ ctx->ptr = ptr;
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_init(br_eax_context *ctx, const br_block_ctrcbc_class **bctx)
+{
+ unsigned char tmp[16], iv[16];
+
+ ctx->vtable = &br_eax_vtable;
+ ctx->bctx = bctx;
+
+ /*
+ * Encrypt a whole-zero block to compute L2 and L4.
+ */
+ memset(tmp, 0, sizeof tmp);
+ memset(iv, 0, sizeof iv);
+ (*bctx)->ctr(bctx, iv, tmp, sizeof tmp);
+ double_gf128(ctx->L2, tmp);
+ double_gf128(ctx->L4, ctx->L2);
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_capture(const br_eax_context *ctx, br_eax_state *st)
+{
+ /*
+ * We capture the three OMAC* states _after_ processing the
+ * initial block (assuming that nonce, message and AAD are
+ * all non-empty).
+ */
+ int i;
+
+ memset(st->st, 0, sizeof st->st);
+ for (i = 0; i < 3; i ++) {
+ unsigned char tmp[16];
+
+ memset(tmp, 0, sizeof tmp);
+ tmp[15] = (unsigned char)i;
+ (*ctx->bctx)->mac(ctx->bctx, st->st[i], tmp, sizeof tmp);
+ }
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_reset(br_eax_context *ctx, const void *nonce, size_t len)
+{
+ /*
+ * Process nonce with OMAC^0.
+ */
+ omac_start(ctx, 0);
+ do_cbcmac_chunk(ctx, nonce, len);
+ do_pad(ctx);
+ memcpy(ctx->nonce, ctx->cbcmac, sizeof ctx->cbcmac);
+
+ /*
+ * Start OMAC^1 for the AAD ("header" in the EAX specification).
+ */
+ omac_start(ctx, 1);
+
+ /*
+ * We use ctx->head[0] as temporary flag to mark that we are
+ * using a "normal" reset().
+ */
+ ctx->head[0] = 0;
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_reset_pre_aad(br_eax_context *ctx, const br_eax_state *st,
+ const void *nonce, size_t len)
+{
+ if (len == 0) {
+ omac_start(ctx, 0);
+ } else {
+ memcpy(ctx->cbcmac, st->st[0], sizeof ctx->cbcmac);
+ ctx->ptr = 0;
+ do_cbcmac_chunk(ctx, nonce, len);
+ }
+ do_pad(ctx);
+ memcpy(ctx->nonce, ctx->cbcmac, sizeof ctx->cbcmac);
+
+ memcpy(ctx->cbcmac, st->st[1], sizeof ctx->cbcmac);
+ ctx->ptr = 0;
+
+ memcpy(ctx->ctr, st->st[2], sizeof ctx->ctr);
+
+ /*
+ * We use ctx->head[0] as a flag to indicate that we use a
+ * a recorded state, with ctx->ctr containing the preprocessed
+ * first block for OMAC^2.
+ */
+ ctx->head[0] = 1;
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_reset_post_aad(br_eax_context *ctx, const br_eax_state *st,
+ const void *nonce, size_t len)
+{
+ if (len == 0) {
+ omac_start(ctx, 0);
+ } else {
+ memcpy(ctx->cbcmac, st->st[0], sizeof ctx->cbcmac);
+ ctx->ptr = 0;
+ do_cbcmac_chunk(ctx, nonce, len);
+ }
+ do_pad(ctx);
+ memcpy(ctx->nonce, ctx->cbcmac, sizeof ctx->cbcmac);
+ memcpy(ctx->ctr, ctx->nonce, sizeof ctx->nonce);
+
+ memcpy(ctx->head, st->st[1], sizeof ctx->head);
+
+ memcpy(ctx->cbcmac, st->st[2], sizeof ctx->cbcmac);
+ ctx->ptr = 0;
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_aad_inject(br_eax_context *ctx, const void *data, size_t len)
+{
+ size_t ptr;
+
+ ptr = ctx->ptr;
+
+ /*
+ * If there is a partial block, first complete it.
+ */
+ if (ptr < 16) {
+ size_t clen;
+
+ clen = 16 - ptr;
+ if (len <= clen) {
+ memcpy(ctx->buf + ptr, data, len);
+ ctx->ptr = ptr + len;
+ return;
+ }
+ memcpy(ctx->buf + ptr, data, clen);
+ data = (const unsigned char *)data + clen;
+ len -= clen;
+ }
+
+ /*
+ * We now have a full block in buf[], and this is not the last
+ * block.
+ */
+ do_cbcmac_chunk(ctx, data, len);
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_flip(br_eax_context *ctx)
+{
+ int from_capture;
+
+ /*
+ * ctx->head[0] may be non-zero if the context was reset with
+ * a pre-AAD captured state. In that case, ctx->ctr[] contains
+ * the state for OMAC^2 _after_ processing the first block.
+ */
+ from_capture = ctx->head[0];
+
+ /*
+ * Complete the OMAC computation on the AAD.
+ */
+ do_pad(ctx);
+ memcpy(ctx->head, ctx->cbcmac, sizeof ctx->cbcmac);
+
+ /*
+ * Start OMAC^2 for the encrypted data.
+ * If the context was initialized from a captured state, then
+ * the OMAC^2 value is in the ctr[] array.
+ */
+ if (from_capture) {
+ memcpy(ctx->cbcmac, ctx->ctr, sizeof ctx->cbcmac);
+ ctx->ptr = 0;
+ } else {
+ omac_start(ctx, 2);
+ }
+
+ /*
+ * Initial counter value for CTR is the processed nonce.
+ */
+ memcpy(ctx->ctr, ctx->nonce, sizeof ctx->nonce);
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_run(br_eax_context *ctx, int encrypt, void *data, size_t len)
+{
+ unsigned char *dbuf;
+ size_t ptr;
+
+ /*
+ * Ensure that there is actual data to process.
+ */
+ if (len == 0) {
+ return;
+ }
+
+ dbuf = data;
+ ptr = ctx->ptr;
+
+ /*
+ * We may have ptr == 0 here if we initialized from a captured
+ * state. In that case, there is no partially consumed block
+ * or unprocessed data.
+ */
+ if (ptr != 0 && ptr != 16) {
+ /*
+ * We have a partially consumed block.
+ */
+ size_t u, clen;
+
+ clen = 16 - ptr;
+ if (len <= clen) {
+ clen = len;
+ }
+ if (encrypt) {
+ for (u = 0; u < clen; u ++) {
+ ctx->buf[ptr + u] ^= dbuf[u];
+ }
+ memcpy(dbuf, ctx->buf + ptr, clen);
+ } else {
+ for (u = 0; u < clen; u ++) {
+ unsigned dx, sx;
+
+ sx = ctx->buf[ptr + u];
+ dx = dbuf[u];
+ ctx->buf[ptr + u] = dx;
+ dbuf[u] = sx ^ dx;
+ }
+ }
+
+ if (len <= clen) {
+ ctx->ptr = ptr + clen;
+ return;
+ }
+ dbuf += clen;
+ len -= clen;
+ }
+
+ /*
+ * We now have a complete encrypted block in buf[] that must still
+ * be processed with OMAC, and this is not the final buf.
+ * Exception: when ptr == 0, no block has been produced yet.
+ */
+ if (ptr != 0) {
+ (*ctx->bctx)->mac(ctx->bctx, ctx->cbcmac,
+ ctx->buf, sizeof ctx->buf);
+ }
+
+ /*
+ * Do CTR encryption or decryption and CBC-MAC for all full blocks
+ * except the last.
+ */
+ ptr = len & (size_t)15;
+ if (ptr == 0) {
+ len -= 16;
+ ptr = 16;
+ } else {
+ len -= ptr;
+ }
+ if (encrypt) {
+ (*ctx->bctx)->encrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
+ dbuf, len);
+ } else {
+ (*ctx->bctx)->decrypt(ctx->bctx, ctx->ctr, ctx->cbcmac,
+ dbuf, len);
+ }
+ dbuf += len;
+
+ /*
+ * Compute next block of CTR stream, and use it to finish
+ * encrypting or decrypting the data.
+ */
+ memset(ctx->buf, 0, sizeof ctx->buf);
+ (*ctx->bctx)->ctr(ctx->bctx, ctx->ctr, ctx->buf, sizeof ctx->buf);
+ if (encrypt) {
+ size_t u;
+
+ for (u = 0; u < ptr; u ++) {
+ ctx->buf[u] ^= dbuf[u];
+ }
+ memcpy(dbuf, ctx->buf, ptr);
+ } else {
+ size_t u;
+
+ for (u = 0; u < ptr; u ++) {
+ unsigned dx, sx;
+
+ sx = ctx->buf[u];
+ dx = dbuf[u];
+ ctx->buf[u] = dx;
+ dbuf[u] = sx ^ dx;
+ }
+ }
+ ctx->ptr = ptr;
+}
+
+/*
+ * Complete tag computation. The final tag is written in ctx->cbcmac.
+ */
+static void
+do_final(br_eax_context *ctx)
+{
+ size_t u;
+
+ do_pad(ctx);
+
+ /*
+ * Authentication tag is the XOR of the three OMAC outputs for
+ * the nonce, AAD and encrypted data.
+ */
+ for (u = 0; u < 16; u ++) {
+ ctx->cbcmac[u] ^= ctx->nonce[u] ^ ctx->head[u];
+ }
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_get_tag(br_eax_context *ctx, void *tag)
+{
+ do_final(ctx);
+ memcpy(tag, ctx->cbcmac, sizeof ctx->cbcmac);
+}
+
+/* see bearssl_aead.h */
+void
+br_eax_get_tag_trunc(br_eax_context *ctx, void *tag, size_t len)
+{
+ do_final(ctx);
+ memcpy(tag, ctx->cbcmac, len);
+}
+
+/* see bearssl_aead.h */
+uint32_t
+br_eax_check_tag_trunc(br_eax_context *ctx, const void *tag, size_t len)
+{
+ unsigned char tmp[16];
+ size_t u;
+ int x;
+
+ br_eax_get_tag(ctx, tmp);
+ x = 0;
+ for (u = 0; u < len; u ++) {
+ x |= tmp[u] ^ ((const unsigned char *)tag)[u];
+ }
+ return EQ0(x);
+}
+
+/* see bearssl_aead.h */
+uint32_t
+br_eax_check_tag(br_eax_context *ctx, const void *tag)
+{
+ return br_eax_check_tag_trunc(ctx, tag, 16);
+}
+
+/* see bearssl_aead.h */
+const br_aead_class br_eax_vtable = {
+ 16,
+ (void (*)(const br_aead_class **, const void *, size_t))
+ &br_eax_reset,
+ (void (*)(const br_aead_class **, const void *, size_t))
+ &br_eax_aad_inject,
+ (void (*)(const br_aead_class **))
+ &br_eax_flip,
+ (void (*)(const br_aead_class **, int, void *, size_t))
+ &br_eax_run,
+ (void (*)(const br_aead_class **, void *))
+ &br_eax_get_tag,
+ (uint32_t (*)(const br_aead_class **, const void *))
+ &br_eax_check_tag,
+ (void (*)(const br_aead_class **, void *, size_t))
+ &br_eax_get_tag_trunc,
+ (uint32_t (*)(const br_aead_class **, const void *, size_t))
+ &br_eax_check_tag_trunc
+};
diff --git a/test/monniaux/BearSSL/src/aead/gcm.c b/test/monniaux/BearSSL/src/aead/gcm.c
new file mode 100644
index 00000000..8b1b5dba
--- /dev/null
+++ b/test/monniaux/BearSSL/src/aead/gcm.c
@@ -0,0 +1,319 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Implementation Notes
+ * ====================
+ *
+ * Since CTR and GHASH implementations can handle only full blocks, a
+ * 16-byte buffer (buf[]) is maintained in the context:
+ *
+ * - When processing AAD, buf[] contains the 0-15 unprocessed bytes.
+ *
+ * - When doing CTR encryption / decryption, buf[] contains the AES output
+ * for the last partial block, to be used with the next few bytes of
+ * data, as well as the already encrypted bytes. For instance, if the
+ * processed data length so far is 21 bytes, then buf[0..4] contains
+ * the five last encrypted bytes, and buf[5..15] contains the next 11
+ * AES output bytes to be XORed with the next 11 bytes of input.
+ *
+ * The recorded AES output bytes are used to complete the block when
+ * the corresponding bytes are obtained. Note that buf[] always
+ * contains the _encrypted_ bytes, whether we apply encryption or
+ * decryption: these bytes are used as input to GHASH when the block
+ * is complete.
+ *
+ * In both cases, the low bits of the data length counters (count_aad,
+ * count_ctr) are used to work out the current situation.
+ */
+
+/* see bearssl_aead.h */
+void
+br_gcm_init(br_gcm_context *ctx, const br_block_ctr_class **bctx, br_ghash gh)
+{
+ unsigned char iv[12];
+
+ ctx->vtable = &br_gcm_vtable;
+ ctx->bctx = bctx;
+ ctx->gh = gh;
+
+ /*
+ * The GHASH key h[] is the raw encryption of the all-zero
+ * block. Since we only have a CTR implementation, we use it
+ * with an all-zero IV and a zero counter, to CTR-encrypt an
+ * all-zero block.
+ */
+ memset(ctx->h, 0, sizeof ctx->h);
+ memset(iv, 0, sizeof iv);
+ (*bctx)->run(bctx, iv, 0, ctx->h, sizeof ctx->h);
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_reset(br_gcm_context *ctx, const void *iv, size_t len)
+{
+ /*
+ * If the provided nonce is 12 bytes, then this is the initial
+ * IV for CTR mode; it will be used with a counter that starts
+ * at 2 (value 1 is for encrypting the GHASH output into the tag).
+ *
+ * If the provided nonce has any other length, then it is hashed
+ * (with GHASH) into a 16-byte value that will be the IV for CTR
+ * (both 12-byte IV and 32-bit counter).
+ */
+ if (len == 12) {
+ memcpy(ctx->j0_1, iv, 12);
+ ctx->j0_2 = 1;
+ } else {
+ unsigned char ty[16], tmp[16];
+
+ memset(ty, 0, sizeof ty);
+ ctx->gh(ty, ctx->h, iv, len);
+ memset(tmp, 0, 8);
+ br_enc64be(tmp + 8, (uint64_t)len << 3);
+ ctx->gh(ty, ctx->h, tmp, 16);
+ memcpy(ctx->j0_1, ty, 12);
+ ctx->j0_2 = br_dec32be(ty + 12);
+ }
+ ctx->jc = ctx->j0_2 + 1;
+ memset(ctx->y, 0, sizeof ctx->y);
+ ctx->count_aad = 0;
+ ctx->count_ctr = 0;
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_aad_inject(br_gcm_context *ctx, const void *data, size_t len)
+{
+ size_t ptr, dlen;
+
+ ptr = (size_t)ctx->count_aad & (size_t)15;
+ if (ptr != 0) {
+ /*
+ * If there is a partial block, then we first try to
+ * complete it.
+ */
+ size_t clen;
+
+ clen = 16 - ptr;
+ if (len < clen) {
+ memcpy(ctx->buf + ptr, data, len);
+ ctx->count_aad += (uint64_t)len;
+ return;
+ }
+ memcpy(ctx->buf + ptr, data, clen);
+ ctx->gh(ctx->y, ctx->h, ctx->buf, 16);
+ data = (const unsigned char *)data + clen;
+ len -= clen;
+ ctx->count_aad += (uint64_t)clen;
+ }
+
+ /*
+ * Now AAD is aligned on a 16-byte block (with regards to GHASH).
+ * We process all complete blocks, and save the last partial
+ * block.
+ */
+ dlen = len & ~(size_t)15;
+ ctx->gh(ctx->y, ctx->h, data, dlen);
+ memcpy(ctx->buf, (const unsigned char *)data + dlen, len - dlen);
+ ctx->count_aad += (uint64_t)len;
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_flip(br_gcm_context *ctx)
+{
+ /*
+ * We complete the GHASH computation if there is a partial block.
+ * The GHASH implementation automatically applies padding with
+ * zeros.
+ */
+ size_t ptr;
+
+ ptr = (size_t)ctx->count_aad & (size_t)15;
+ if (ptr != 0) {
+ ctx->gh(ctx->y, ctx->h, ctx->buf, ptr);
+ }
+ KILL_TAIL_CALL();
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_run(br_gcm_context *ctx, int encrypt, void *data, size_t len)
+{
+ unsigned char *buf;
+ size_t ptr, dlen;
+
+ buf = data;
+ ptr = (size_t)ctx->count_ctr & (size_t)15;
+ if (ptr != 0) {
+ /*
+ * If we have a partial block, then we try to complete it.
+ */
+ size_t u, clen;
+
+ clen = 16 - ptr;
+ if (len < clen) {
+ clen = len;
+ }
+ for (u = 0; u < clen; u ++) {
+ unsigned x, y;
+
+ x = buf[u];
+ y = x ^ ctx->buf[ptr + u];
+ ctx->buf[ptr + u] = encrypt ? y : x;
+ buf[u] = y;
+ }
+ ctx->count_ctr += (uint64_t)clen;
+ buf += clen;
+ len -= clen;
+ if (ptr + clen < 16) {
+ return;
+ }
+ ctx->gh(ctx->y, ctx->h, ctx->buf, 16);
+ }
+
+ /*
+ * Process full blocks.
+ */
+ dlen = len & ~(size_t)15;
+ if (!encrypt) {
+ ctx->gh(ctx->y, ctx->h, buf, dlen);
+ }
+ ctx->jc = (*ctx->bctx)->run(ctx->bctx, ctx->j0_1, ctx->jc, buf, dlen);
+ if (encrypt) {
+ ctx->gh(ctx->y, ctx->h, buf, dlen);
+ }
+ buf += dlen;
+ len -= dlen;
+ ctx->count_ctr += (uint64_t)dlen;
+
+ if (len > 0) {
+ /*
+ * There is a partial block.
+ */
+ size_t u;
+
+ memset(ctx->buf, 0, sizeof ctx->buf);
+ ctx->jc = (*ctx->bctx)->run(ctx->bctx, ctx->j0_1,
+ ctx->jc, ctx->buf, 16);
+ for (u = 0; u < len; u ++) {
+ unsigned x, y;
+
+ x = buf[u];
+ y = x ^ ctx->buf[u];
+ ctx->buf[u] = encrypt ? y : x;
+ buf[u] = y;
+ }
+ ctx->count_ctr += (uint64_t)len;
+ }
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_get_tag(br_gcm_context *ctx, void *tag)
+{
+ size_t ptr;
+ unsigned char tmp[16];
+
+ ptr = (size_t)ctx->count_ctr & (size_t)15;
+ if (ptr > 0) {
+ /*
+ * There is a partial block: encrypted/decrypted data has
+ * been produced, but the encrypted bytes must still be
+ * processed by GHASH.
+ */
+ ctx->gh(ctx->y, ctx->h, ctx->buf, ptr);
+ }
+
+ /*
+ * Final block for GHASH: the AAD and plaintext lengths (in bits).
+ */
+ br_enc64be(tmp, ctx->count_aad << 3);
+ br_enc64be(tmp + 8, ctx->count_ctr << 3);
+ ctx->gh(ctx->y, ctx->h, tmp, 16);
+
+ /*
+ * Tag is the GHASH output XORed with the encryption of the
+ * nonce with the initial counter value.
+ */
+ memcpy(tag, ctx->y, 16);
+ (*ctx->bctx)->run(ctx->bctx, ctx->j0_1, ctx->j0_2, tag, 16);
+}
+
+/* see bearssl_aead.h */
+void
+br_gcm_get_tag_trunc(br_gcm_context *ctx, void *tag, size_t len)
+{
+ unsigned char tmp[16];
+
+ br_gcm_get_tag(ctx, tmp);
+ memcpy(tag, tmp, len);
+}
+
+/* see bearssl_aead.h */
+uint32_t
+br_gcm_check_tag_trunc(br_gcm_context *ctx, const void *tag, size_t len)
+{
+ unsigned char tmp[16];
+ size_t u;
+ int x;
+
+ br_gcm_get_tag(ctx, tmp);
+ x = 0;
+ for (u = 0; u < len; u ++) {
+ x |= tmp[u] ^ ((const unsigned char *)tag)[u];
+ }
+ return EQ0(x);
+}
+
+/* see bearssl_aead.h */
+uint32_t
+br_gcm_check_tag(br_gcm_context *ctx, const void *tag)
+{
+ return br_gcm_check_tag_trunc(ctx, tag, 16);
+}
+
+/* see bearssl_aead.h */
+const br_aead_class br_gcm_vtable = {
+ 16,
+ (void (*)(const br_aead_class **, const void *, size_t))
+ &br_gcm_reset,
+ (void (*)(const br_aead_class **, const void *, size_t))
+ &br_gcm_aad_inject,
+ (void (*)(const br_aead_class **))
+ &br_gcm_flip,
+ (void (*)(const br_aead_class **, int, void *, size_t))
+ &br_gcm_run,
+ (void (*)(const br_aead_class **, void *))
+ &br_gcm_get_tag,
+ (uint32_t (*)(const br_aead_class **, const void *))
+ &br_gcm_check_tag,
+ (void (*)(const br_aead_class **, void *, size_t))
+ &br_gcm_get_tag_trunc,
+ (uint32_t (*)(const br_aead_class **, const void *, size_t))
+ &br_gcm_check_tag_trunc
+};
diff --git a/test/monniaux/BearSSL/src/codec/ccopy.c b/test/monniaux/BearSSL/src/codec/ccopy.c
new file mode 100644
index 00000000..2beace72
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/ccopy.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_ccopy(uint32_t ctl, void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ uint32_t x, y;
+
+ x = *s ++;
+ y = *d;
+ *d = MUX(ctl, x, y);
+ d ++;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec16be.c b/test/monniaux/BearSSL/src/codec/dec16be.c
new file mode 100644
index 00000000..4f3f7f4a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec16be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec16be(uint16_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec16be(buf);
+ buf += 2;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec16le.c b/test/monniaux/BearSSL/src/codec/dec16le.c
new file mode 100644
index 00000000..84d85364
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec16le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec16le(uint16_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec16le(buf);
+ buf += 2;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec32be.c b/test/monniaux/BearSSL/src/codec/dec32be.c
new file mode 100644
index 00000000..5a8fc596
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec32be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec32be(uint32_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec32be(buf);
+ buf += 4;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec32le.c b/test/monniaux/BearSSL/src/codec/dec32le.c
new file mode 100644
index 00000000..ed36e718
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec32le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec32le(uint32_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec32le(buf);
+ buf += 4;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec64be.c b/test/monniaux/BearSSL/src/codec/dec64be.c
new file mode 100644
index 00000000..0c40a76d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec64be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec64be(uint64_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec64be(buf);
+ buf += 8;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/dec64le.c b/test/monniaux/BearSSL/src/codec/dec64le.c
new file mode 100644
index 00000000..cbd02c2c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/dec64le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_dec64le(uint64_t *v, size_t num, const void *src)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (num -- > 0) {
+ *v ++ = br_dec64le(buf);
+ buf += 8;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc16be.c b/test/monniaux/BearSSL/src/codec/enc16be.c
new file mode 100644
index 00000000..6e066521
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc16be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc16be(void *dst, const uint16_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc16be(buf, *v ++);
+ buf += 2;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc16le.c b/test/monniaux/BearSSL/src/codec/enc16le.c
new file mode 100644
index 00000000..3e5049a0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc16le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc16le(void *dst, const uint16_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc16le(buf, *v ++);
+ buf += 2;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc32be.c b/test/monniaux/BearSSL/src/codec/enc32be.c
new file mode 100644
index 00000000..97298b5e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc32be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc32be(void *dst, const uint32_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc32be(buf, *v ++);
+ buf += 4;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc32le.c b/test/monniaux/BearSSL/src/codec/enc32le.c
new file mode 100644
index 00000000..9e9c8562
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc32le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc32le(void *dst, const uint32_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc32le(buf, *v ++);
+ buf += 4;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc64be.c b/test/monniaux/BearSSL/src/codec/enc64be.c
new file mode 100644
index 00000000..d548944c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc64be.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc64be(void *dst, const uint64_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc64be(buf, *v ++);
+ buf += 8;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/enc64le.c b/test/monniaux/BearSSL/src/codec/enc64le.c
new file mode 100644
index 00000000..1f1d68eb
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/enc64le.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_range_enc64le(void *dst, const uint64_t *v, size_t num)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (num -- > 0) {
+ br_enc64le(buf, *v ++);
+ buf += 8;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/codec/pemdec.c b/test/monniaux/BearSSL/src/codec/pemdec.c
new file mode 100644
index 00000000..8e54e6d0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/pemdec.c
@@ -0,0 +1,526 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_pem_decoder_init_main(void *t0ctx);
+
+void br_pem_decoder_run(void *t0ctx);
+
+
+
+#include "inner.h"
+
+#define CTX ((br_pem_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_pem_decoder_context, cpu)))
+
+/* see bearssl_pem.h */
+void
+br_pem_decoder_init(br_pem_decoder_context *ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_pem_decoder_init_main(&ctx->cpu);
+ br_pem_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_pem.h */
+size_t
+br_pem_decoder_push(br_pem_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ if (ctx->event) {
+ return 0;
+ }
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_pem_decoder_run(&ctx->cpu);
+ return len - ctx->hlen;
+}
+
+/* see bearssl_pem.h */
+int
+br_pem_decoder_event(br_pem_decoder_context *ctx)
+{
+ int event;
+
+ event = ctx->event;
+ ctx->event = 0;
+ return event;
+}
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x42, 0x45, 0x47, 0x49, 0x4E, 0x20,
+ 0x00, 0x2D, 0x2D, 0x2D, 0x2D, 0x45, 0x4E, 0x44, 0x20, 0x00
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x00, 0x09, 0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x01,
+ 0x01, 0x08, 0x00, 0x00, 0x13, 0x13, 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_pem_decoder_context, event)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_pem_decoder_context, name)), 0x00, 0x00, 0x05,
+ 0x14, 0x2C, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x03, 0x13, 0x04, 0x76, 0x01,
+ 0x2D, 0x0C, 0x06, 0x05, 0x2E, 0x01, 0x03, 0x2D, 0x00, 0x01, 0x0D, 0x27,
+ 0x05, 0x04, 0x01, 0x03, 0x2D, 0x00, 0x15, 0x2E, 0x01, 0x02, 0x2D, 0x00,
+ 0x01, 0x01, 0x7F, 0x03, 0x00, 0x25, 0x01, 0x00, 0x18, 0x0D, 0x06, 0x03,
+ 0x13, 0x04, 0x3C, 0x01, 0x7F, 0x18, 0x0D, 0x06, 0x13, 0x13, 0x02, 0x00,
+ 0x05, 0x06, 0x2E, 0x01, 0x03, 0x2D, 0x04, 0x03, 0x01, 0x7F, 0x23, 0x01,
+ 0x00, 0x00, 0x04, 0x23, 0x01, 0x01, 0x18, 0x0D, 0x06, 0x09, 0x13, 0x01,
+ 0x00, 0x23, 0x01, 0x00, 0x00, 0x04, 0x14, 0x01, 0x02, 0x18, 0x0D, 0x06,
+ 0x06, 0x13, 0x01, 0x7F, 0x00, 0x04, 0x08, 0x13, 0x01, 0x03, 0x2D, 0x01,
+ 0x00, 0x00, 0x13, 0x01, 0x00, 0x03, 0x00, 0x04, 0xFF, 0x33, 0x01, 0x2C,
+ 0x14, 0x01, 0x2D, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x7F, 0x00, 0x14, 0x31,
+ 0x06, 0x02, 0x13, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01,
+ 0x02, 0x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00,
+ 0x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x03,
+ 0x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00, 0x02,
+ 0x00, 0x01, 0x06, 0x0A, 0x07, 0x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D,
+ 0x06, 0x04, 0x13, 0x01, 0x03, 0x00, 0x14, 0x01, 0x3D, 0x0D, 0x06, 0x2E,
+ 0x13, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x03, 0x00,
+ 0x2F, 0x05, 0x04, 0x13, 0x01, 0x03, 0x00, 0x01, 0x3D, 0x0C, 0x06, 0x03,
+ 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x0F, 0x10, 0x06, 0x03, 0x01, 0x03,
+ 0x00, 0x02, 0x00, 0x01, 0x04, 0x0F, 0x1C, 0x01, 0x01, 0x00, 0x16, 0x14,
+ 0x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x06,
+ 0x0A, 0x07, 0x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13,
+ 0x01, 0x03, 0x00, 0x14, 0x01, 0x3D, 0x0D, 0x06, 0x20, 0x13, 0x2F, 0x05,
+ 0x03, 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x03, 0x10, 0x06, 0x03, 0x01,
+ 0x03, 0x00, 0x02, 0x00, 0x01, 0x0A, 0x0F, 0x1C, 0x02, 0x00, 0x01, 0x02,
+ 0x0F, 0x1C, 0x01, 0x01, 0x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E,
+ 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x06, 0x0A, 0x07, 0x03, 0x00, 0x02,
+ 0x00, 0x01, 0x10, 0x0F, 0x1C, 0x02, 0x00, 0x01, 0x08, 0x0F, 0x1C, 0x02,
+ 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00, 0x28, 0x01, 0x01, 0x2D, 0x24, 0x06,
+ 0x02, 0x04, 0x7B, 0x04, 0x75, 0x00, 0x14, 0x12, 0x2A, 0x14, 0x05, 0x04,
+ 0x20, 0x01, 0x7F, 0x00, 0x2C, 0x2A, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x05,
+ 0x13, 0x20, 0x01, 0x00, 0x00, 0x0D, 0x05, 0x05, 0x13, 0x2E, 0x01, 0x00,
+ 0x00, 0x1E, 0x04, 0x5E, 0x00, 0x01, 0x01, 0x27, 0x06, 0x0B, 0x22, 0x01,
+ 0x80, 0x7F, 0x2B, 0x14, 0x06, 0x02, 0x30, 0x00, 0x13, 0x04, 0x6E, 0x00,
+ 0x2C, 0x14, 0x31, 0x05, 0x01, 0x00, 0x13, 0x04, 0x77, 0x00, 0x14, 0x14,
+ 0x01, 0x80, 0x61, 0x0E, 0x1B, 0x01, 0x80, 0x7A, 0x0B, 0x10, 0x06, 0x03,
+ 0x01, 0x20, 0x08, 0x00, 0x01, 0x14, 0x03, 0x00, 0x1B, 0x18, 0x05, 0x05,
+ 0x20, 0x2E, 0x01, 0x00, 0x00, 0x2C, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x06,
+ 0x20, 0x02, 0x00, 0x1B, 0x08, 0x00, 0x14, 0x01, 0x0D, 0x0D, 0x06, 0x03,
+ 0x13, 0x04, 0x03, 0x2A, 0x18, 0x1A, 0x1E, 0x1B, 0x1F, 0x1B, 0x04, 0x59,
+ 0x00, 0x19, 0x14, 0x1D, 0x05, 0x01, 0x00, 0x13, 0x11, 0x04, 0x76, 0x00,
+ 0x21, 0x1A, 0x11, 0x00, 0x00, 0x2C, 0x01, 0x0A, 0x0C, 0x06, 0x02, 0x04,
+ 0x78, 0x00, 0x01, 0x01, 0x7F, 0x03, 0x00, 0x2C, 0x14, 0x01, 0x0A, 0x0C,
+ 0x06, 0x09, 0x31, 0x05, 0x04, 0x01, 0x00, 0x03, 0x00, 0x04, 0x70, 0x13,
+ 0x02, 0x00, 0x00, 0x00, 0x14, 0x06, 0x14, 0x1F, 0x14, 0x22, 0x07, 0x17,
+ 0x01, 0x2D, 0x0C, 0x06, 0x08, 0x22, 0x07, 0x1E, 0x01, 0x00, 0x1B, 0x1A,
+ 0x00, 0x04, 0x69, 0x22, 0x1A, 0x00, 0x00, 0x14, 0x01, 0x0A, 0x0C, 0x1B,
+ 0x01, 0x20, 0x0B, 0x10, 0x00
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 15,
+ 19,
+ 24,
+ 29,
+ 67,
+ 149,
+ 384,
+ 396,
+ 431,
+ 450,
+ 460,
+ 479,
+ 523,
+ 534,
+ 539,
+ 549,
+ 574,
+ 601
+};
+
+#define T0_INTERPRETED 29
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_pem_decoder_init_main, 38)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_pem_decoder_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 8: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 9: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 10: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 11: {
+ /* <= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a <= b));
+
+ }
+ break;
+ case 12: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 13: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 14: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 15: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 16: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 17: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 18: {
+ /* data-get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(t0_datablock[addr]);
+
+ }
+ break;
+ case 19: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 20: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 21: {
+ /* flush-buf */
+
+ if (CTX->ptr > 0) {
+ if (CTX->dest) {
+ CTX->dest(CTX->dest_ctx, CTX->buf, CTX->ptr);
+ }
+ CTX->ptr = 0;
+ }
+
+ }
+ break;
+ case 22: {
+ /* from-base64 */
+
+ uint32_t c = T0_POP();
+ uint32_t p, q, r, z;
+ p = c - 0x41;
+ q = c - 0x61;
+ r = c - 0x30;
+
+ z = ((p + 2) & -LT(p, 26))
+ | ((q + 28) & -LT(q, 26))
+ | ((r + 54) & -LT(r, 10))
+ | (64 & -EQ(c, 0x2B))
+ | (65 & -EQ(c, 0x2F))
+ | EQ(c, 0x3D);
+ T0_PUSHi((int32_t)z - 2);
+
+ }
+ break;
+ case 23: {
+ /* get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(*((unsigned char *)CTX + addr));
+
+ }
+ break;
+ case 24: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 25: {
+ /* read8-native */
+
+ if (CTX->hlen > 0) {
+ T0_PUSH(*CTX->hbuf ++);
+ CTX->hlen --;
+ } else {
+ T0_PUSHi(-1);
+ }
+
+ }
+ break;
+ case 26: {
+ /* set8 */
+
+ size_t addr = T0_POP();
+ unsigned x = T0_POP();
+ *((unsigned char *)CTX + addr) = x;
+
+ }
+ break;
+ case 27: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ case 28: {
+ /* write8 */
+
+ unsigned char x = (unsigned char)T0_POP();
+ CTX->buf[CTX->ptr ++] = x;
+ if (CTX->ptr == sizeof CTX->buf) {
+ if (CTX->dest) {
+ CTX->dest(CTX->dest_ctx, CTX->buf, sizeof CTX->buf);
+ }
+ CTX->ptr = 0;
+ }
+
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
diff --git a/test/monniaux/BearSSL/src/codec/pemdec.t0 b/test/monniaux/BearSSL/src/codec/pemdec.t0
new file mode 100644
index 00000000..2237abbf
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/pemdec.t0
@@ -0,0 +1,314 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+preamble {
+
+#include "inner.h"
+
+#define CTX ((br_pem_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_pem_decoder_context, cpu)))
+
+/* see bearssl_pem.h */
+void
+br_pem_decoder_init(br_pem_decoder_context *ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_pem_decoder_init_main(&ctx->cpu);
+ br_pem_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_pem.h */
+size_t
+br_pem_decoder_push(br_pem_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ if (ctx->event) {
+ return 0;
+ }
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_pem_decoder_run(&ctx->cpu);
+ return len - ctx->hlen;
+}
+
+/* see bearssl_pem.h */
+int
+br_pem_decoder_event(br_pem_decoder_context *ctx)
+{
+ int event;
+
+ event = ctx->event;
+ ctx->event = 0;
+ return event;
+}
+
+}
+
+\ Define a word that evaluates to the address of a field within the
+\ decoder context.
+: addr:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(br_pem_decoder_context, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr: event
+addr: name
+addr: buf
+addr: ptr
+
+\ Set a byte at a specific address (offset within the context).
+cc: set8 ( value addr -- ) {
+ size_t addr = T0_POP();
+ unsigned x = T0_POP();
+ *((unsigned char *)CTX + addr) = x;
+}
+
+\ Get a byte at a specific address (offset within the context).
+cc: get8 ( addr -- value ) {
+ size_t addr = T0_POP();
+ T0_PUSH(*((unsigned char *)CTX + addr));
+}
+
+\ Send an event.
+: send-event ( event -- )
+ addr-event set8 co ;
+
+\ Low-level function to read a single byte. Returned value is the byte
+\ (0 to 255), or -1 if there is no available data.
+cc: read8-native ( -- x ) {
+ if (CTX->hlen > 0) {
+ T0_PUSH(*CTX->hbuf ++);
+ CTX->hlen --;
+ } else {
+ T0_PUSHi(-1);
+ }
+}
+
+\ Read next byte. Block until the next byte is available.
+: read8 ( -- x )
+ begin read8-native dup 0< ifnot ret then drop co again ;
+
+\ Read bytes until next end-of-line.
+: skip-newline ( -- )
+ begin read8 `\n <> while repeat ;
+
+\ Read bytes until next end-of-line; verify that they are all whitespace.
+\ This returns -1 if they were all whitespace, 0 otherwise.
+: skip-newline-ws ( -- bool )
+ -1 { r }
+ begin read8 dup `\n <> while ws? ifnot 0 >r then repeat
+ drop r ;
+
+\ Normalise a byte to uppercase (ASCII only).
+: norm-upper ( x -- x )
+ dup dup `a >= swap `z <= and if 32 - then ;
+
+\ Read bytes and compare with the provided string. On mismatch, the
+\ rest of the line is consumed. Matching is not case sensitive.
+: match-string ( str -- bool )
+ begin
+ dup data-get8 norm-upper dup ifnot 2drop -1 ret then
+ read8 norm-upper dup `\n = if drop 2drop 0 ret then
+ = ifnot drop skip-newline 0 ret then
+ 1+
+ again ;
+
+\ Read bytes into the provided buffer, but no more than the provided
+\ count. Reading stops when end-of-line is reached. Returned value
+\ is the count of bytes written to the buffer, or 0 if the buffer size
+\ was exceeded. All bytes are normalised to uppercase (ASCII only).
+: read-bytes ( addr len -- len )
+ dup { orig-len }
+ swap
+ begin
+ over ifnot 2drop skip-newline 0 ret then
+ read8 dup `\n = if 2drop orig-len swap - ret then
+ dup `\r = if drop else norm-upper over set8 then
+ 1+ swap 1- swap
+ again ;
+
+\ Remove trailing dashes from the name buffer.
+: trim-dashes ( len -- )
+ begin dup while
+ 1-
+ dup addr-name + get8 `- <> if
+ addr-name + 1+ 0 swap set8 ret
+ then
+ repeat
+ addr-name set8 ;
+
+\ Scan input for next "begin" banner.
+: next-banner-begin ( -- )
+ begin
+ "-----BEGIN " match-string if
+ addr-name 127 read-bytes
+ dup if trim-dashes ret then
+ drop
+ then
+ again ;
+
+\ Convert a Base64 character to its numerical value. Returned value is
+\ 0 to 63 for Base64 characters, -1 for '=', and -2 for all other characters.
+cc: from-base64 ( char -- x ) {
+ uint32_t c = T0_POP();
+ uint32_t p, q, r, z;
+ p = c - 0x41;
+ q = c - 0x61;
+ r = c - 0x30;
+
+ z = ((p + 2) & -LT(p, 26))
+ | ((q + 28) & -LT(q, 26))
+ | ((r + 54) & -LT(r, 10))
+ | (64 & -EQ(c, 0x2B))
+ | (65 & -EQ(c, 0x2F))
+ | EQ(c, 0x3D);
+ T0_PUSHi((int32_t)z - 2);
+}
+
+\ Test whether a character is whitespace (but not a newline).
+: ws? ( x -- bool )
+ dup `\n <> swap 32 <= and ;
+
+\ Read next character, skipping whitespace (except newline).
+: next-nonws ( -- x )
+ begin
+ read8 dup ws? ifnot ret then
+ drop
+ again ;
+
+\ Write one byte in the output buffer.
+cc: write8 ( x -- ) {
+ unsigned char x = (unsigned char)T0_POP();
+ CTX->buf[CTX->ptr ++] = x;
+ if (CTX->ptr == sizeof CTX->buf) {
+ if (CTX->dest) {
+ CTX->dest(CTX->dest_ctx, CTX->buf, sizeof CTX->buf);
+ }
+ CTX->ptr = 0;
+ }
+}
+
+\ Flush the output buffer.
+cc: flush-buf ( -- ) {
+ if (CTX->ptr > 0) {
+ if (CTX->dest) {
+ CTX->dest(CTX->dest_ctx, CTX->buf, CTX->ptr);
+ }
+ CTX->ptr = 0;
+ }
+}
+
+\ Decode the four next Base64 characters. Returned value is:
+\ 0 quartet processed, three bytes produced.
+\ -1 dash encountered as first character (no leading whitespace).
+\ 1 quartet processed, one or two bytes produced, terminator reached.
+\ 2 end-of-line reached.
+\ 3 error.
+\ For all positive return values, the remaining of the current line has been
+\ consumed.
+: decode-next-quartet ( -- r )
+ \ Process first character. It may be a dash.
+ read8 dup `- = if drop -1 ret then
+ dup ws? if drop next-nonws then
+ dup `\n = if drop 2 ret then
+ from-base64 dup 0< if drop skip-newline 3 ret then
+ { acc }
+
+ \ Second character.
+ next-nonws dup `\n = if drop 3 ret then
+ from-base64 dup 0< if drop skip-newline 3 ret then
+ acc 6 << + >acc
+
+ \ Third character: may be an equal sign.
+ next-nonws dup `\n = if drop 3 ret then
+ dup `= = if
+ \ Fourth character must be an equal sign.
+ drop
+ next-nonws dup `\n = if drop 3 ret then
+ skip-newline-ws ifnot drop 3 ret then
+ `= <> if 3 ret then
+ acc 0x0F and if 3 ret then
+ acc 4 >> write8
+ 1 ret
+ then
+ from-base64 dup 0< if drop skip-newline 3 ret then
+ acc 6 << + >acc
+
+ \ Fourth character: may be an equal sign.
+ next-nonws dup `\n = if drop 3 ret then
+ dup `= = if
+ drop skip-newline-ws ifnot 3 ret then
+ acc 0x03 and if 3 ret then
+ acc 10 >> write8
+ acc 2 >> write8
+ 1 ret
+ then
+ from-base64 dup 0< if drop skip-newline 3 ret then
+ acc 6 << + >acc
+ acc 16 >> write8
+ acc 8 >> write8
+ acc write8
+ 0 ;
+
+\ Check trailer line (possibly, the leading dash has been read). This
+\ sends the appropriate event.
+: check-trailer ( bool -- )
+ ifnot
+ begin read8 dup `\n = while drop repeat
+ `- <> if skip-newline 3 send-event ret then
+ then
+ "----END " match-string ifnot 3 send-event ret then
+ flush-buf
+ skip-newline 2 send-event ;
+
+\ Decode one line worth of characters. Returned value is 0 if the end of the
+\ object is reached, -1 otherwise. The end of object or error event is sent.
+: decode-line ( -- bool )
+ -1 { first }
+ begin
+ decode-next-quartet
+ case
+ 0 of endof
+ -1 of
+ first ifnot
+ skip-newline 3 send-event
+ else
+ -1 check-trailer
+ then
+ 0 ret
+ endof
+ 1 of 0 check-trailer 0 ret endof
+ 2 of -1 ret endof
+
+ \ On decoding error
+ drop 3 send-event 0 ret
+ endcase
+ 0 >first
+ again ;
+
+: main ( -- ! )
+ begin
+ next-banner-begin 1 send-event
+ begin decode-line while repeat
+ again ;
diff --git a/test/monniaux/BearSSL/src/codec/pemenc.c b/test/monniaux/BearSSL/src/codec/pemenc.c
new file mode 100644
index 00000000..236601e6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/codec/pemenc.c
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Get the appropriate Base64 character for a numeric value in the
+ * 0..63 range. This is constant-time.
+ */
+static char
+b64char(uint32_t x)
+{
+ /*
+ * Values 0 to 25 map to 0x41..0x5A ('A' to 'Z')
+ * Values 26 to 51 map to 0x61..0x7A ('a' to 'z')
+ * Values 52 to 61 map to 0x30..0x39 ('0' to '9')
+ * Value 62 maps to 0x2B ('+')
+ * Value 63 maps to 0x2F ('/')
+ */
+ uint32_t a, b, c;
+
+ a = x - 26;
+ b = x - 52;
+ c = x - 62;
+
+ /*
+ * Looking at bits 8..15 of values a, b and c:
+ *
+ * x a b c
+ * ---------------------
+ * 0..25 FF FF FF
+ * 26..51 00 FF FF
+ * 52..61 00 00 FF
+ * 62..63 00 00 00
+ */
+ return (char)(((x + 0x41) & ((a & b & c) >> 8))
+ | ((x + (0x61 - 26)) & ((~a & b & c) >> 8))
+ | ((x - (52 - 0x30)) & ((~a & ~b & c) >> 8))
+ | ((0x2B + ((x & 1) << 2)) & (~(a | b | c) >> 8)));
+}
+
+/* see bearssl_pem.h */
+size_t
+br_pem_encode(void *dest, const void *data, size_t len,
+ const char *banner, unsigned flags)
+{
+ size_t dlen, banner_len, lines;
+ char *d;
+ unsigned char *buf;
+ size_t u;
+ int off, lim;
+
+ banner_len = strlen(banner);
+ /* FIXME: try to avoid divisions here, as they may pull
+ an extra libc function. */
+ if ((flags & BR_PEM_LINE64) != 0) {
+ lines = (len + 47) / 48;
+ } else {
+ lines = (len + 56) / 57;
+ }
+ dlen = (banner_len << 1) + 30 + (((len + 2) / 3) << 2)
+ + lines + 2;
+ if ((flags & BR_PEM_CRLF) != 0) {
+ dlen += lines + 2;
+ }
+
+ if (dest == NULL) {
+ return dlen;
+ }
+
+ d = dest;
+
+ /*
+ * We always move the source data to the end of output buffer;
+ * the encoding process never "catches up" except at the very
+ * end. This also handles all conditions of partial or total
+ * overlap.
+ */
+ buf = (unsigned char *)d + dlen - len;
+ memmove(buf, data, len);
+
+ memcpy(d, "-----BEGIN ", 11);
+ d += 11;
+ memcpy(d, banner, banner_len);
+ d += banner_len;
+ memcpy(d, "-----", 5);
+ d += 5;
+ if ((flags & BR_PEM_CRLF) != 0) {
+ *d ++ = 0x0D;
+ }
+ *d ++ = 0x0A;
+
+ off = 0;
+ lim = (flags & BR_PEM_LINE64) != 0 ? 16 : 19;
+ for (u = 0; (u + 2) < len; u += 3) {
+ uint32_t w;
+
+ w = ((uint32_t)buf[u] << 16)
+ | ((uint32_t)buf[u + 1] << 8)
+ | (uint32_t)buf[u + 2];
+ *d ++ = b64char(w >> 18);
+ *d ++ = b64char((w >> 12) & 0x3F);
+ *d ++ = b64char((w >> 6) & 0x3F);
+ *d ++ = b64char(w & 0x3F);
+ if (++ off == lim) {
+ off = 0;
+ if ((flags & BR_PEM_CRLF) != 0) {
+ *d ++ = 0x0D;
+ }
+ *d ++ = 0x0A;
+ }
+ }
+ if (u < len) {
+ uint32_t w;
+
+ w = (uint32_t)buf[u] << 16;
+ if (u + 1 < len) {
+ w |= (uint32_t)buf[u + 1] << 8;
+ }
+ *d ++ = b64char(w >> 18);
+ *d ++ = b64char((w >> 12) & 0x3F);
+ if (u + 1 < len) {
+ *d ++ = b64char((w >> 6) & 0x3F);
+ } else {
+ *d ++ = 0x3D;
+ }
+ *d ++ = 0x3D;
+ off ++;
+ }
+ if (off != 0) {
+ if ((flags & BR_PEM_CRLF) != 0) {
+ *d ++ = 0x0D;
+ }
+ *d ++ = 0x0A;
+ }
+
+ memcpy(d, "-----END ", 9);
+ d += 9;
+ memcpy(d, banner, banner_len);
+ d += banner_len;
+ memcpy(d, "-----", 5);
+ d += 5;
+ if ((flags & BR_PEM_CRLF) != 0) {
+ *d ++ = 0x0D;
+ }
+ *d ++ = 0x0A;
+
+ /* Final zero, not counted in returned length. */
+ *d ++ = 0x00;
+
+ return dlen;
+}
diff --git a/test/monniaux/BearSSL/src/config.h b/test/monniaux/BearSSL/src/config.h
new file mode 100644
index 00000000..3db50045
--- /dev/null
+++ b/test/monniaux/BearSSL/src/config.h
@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef CONFIG_H__
+#define CONFIG_H__
+
+/*
+ * This file contains compile-time flags that can override the
+ * autodetection performed in relevant files. Each flag is a macro; it
+ * deactivates the feature if defined to 0, activates it if defined to a
+ * non-zero integer (normally 1). If the macro is not defined, then
+ * autodetection applies.
+ */
+
+/*
+ * When BR_64 is enabled, 64-bit integer types are assumed to be
+ * efficient (i.e. the architecture has 64-bit registers and can
+ * do 64-bit operations as fast as 32-bit operations).
+ *
+#define BR_64 1
+ */
+
+/*
+ * When BR_LOMUL is enabled, then multiplications of 32-bit values whose
+ * result are truncated to the low 32 bits are assumed to be
+ * substantially more efficient than 32-bit multiplications that yield
+ * 64-bit results. This is typically the case on low-end ARM Cortex M
+ * systems (M0, M0+, M1, and arguably M3 and M4 as well).
+ *
+#define BR_LOMUL 1
+ */
+
+/*
+ * When BR_SLOW_MUL is enabled, multiplications are assumed to be
+ * substantially slow with regards to other integer operations, thus
+ * making it worth to make more operations for a given task if it allows
+ * using less multiplications.
+ *
+#define BR_SLOW_MUL 1
+ */
+
+/*
+ * When BR_SLOW_MUL15 is enabled, short multplications (on 15-bit words)
+ * are assumed to be substantially slow with regards to other integer
+ * operations, thus making it worth to make more integer operations if
+ * it allows using less multiplications.
+ *
+#define BR_SLOW_MUL15 1
+ */
+
+/*
+ * When BR_CT_MUL31 is enabled, multiplications of 31-bit values (used
+ * in the "i31" big integer implementation) use an alternate implementation
+ * which is slower and larger than the normal multiplication, but should
+ * ensure constant-time multiplications even on architectures where the
+ * multiplication opcode takes a variable number of cycles to complete.
+ *
+#define BR_CT_MUL31 1
+ */
+
+/*
+ * When BR_CT_MUL15 is enabled, multiplications of 15-bit values (held
+ * in 32-bit words) use an alternate implementation which is slower and
+ * larger than the normal multiplication, but should ensure
+ * constant-time multiplications on most/all architectures where the
+ * basic multiplication is not constant-time.
+#define BR_CT_MUL15 1
+ */
+
+/*
+ * When BR_NO_ARITH_SHIFT is enabled, arithmetic right shifts (with sign
+ * extension) are performed with a sequence of operations which is bigger
+ * and slower than a simple right shift on a signed value. This avoids
+ * relying on an implementation-defined behaviour. However, most if not
+ * all C compilers use sign extension for right shifts on signed values,
+ * so this alternate macro is disabled by default.
+#define BR_NO_ARITH_SHIFT 1
+ */
+
+/*
+ * When BR_RDRAND is enabled, the SSL engine will use the RDRAND opcode
+ * to automatically obtain quality randomness for seeding its internal
+ * PRNG. Since that opcode is present only in recent x86 CPU, its
+ * support is dynamically tested; if the current CPU does not support
+ * it, then another random source will be used, such as /dev/urandom or
+ * CryptGenRandom().
+ *
+#define BR_RDRAND 1
+ */
+
+/*
+ * When BR_USE_URANDOM is enabled, the SSL engine will use /dev/urandom
+ * to automatically obtain quality randomness for seedings its internal
+ * PRNG.
+ *
+#define BR_USE_URANDOM 1
+ */
+
+/*
+ * When BR_USE_WIN32_RAND is enabled, the SSL engine will use the Win32
+ * (CryptoAPI) functions (CryptAcquireContext(), CryptGenRandom()...) to
+ * automatically obtain quality randomness for seedings its internal PRNG.
+ *
+ * Note: if both BR_USE_URANDOM and BR_USE_WIN32_RAND are defined, the
+ * former takes precedence.
+ *
+#define BR_USE_WIN32_RAND 1
+ */
+
+/*
+ * When BR_USE_UNIX_TIME is enabled, the X.509 validation engine obtains
+ * the current time from the OS by calling time(), and assuming that the
+ * returned value (a 'time_t') is an integer that counts time in seconds
+ * since the Unix Epoch (Jan 1st, 1970, 00:00 UTC).
+ *
+#define BR_USE_UNIX_TIME 1
+ */
+
+/*
+ * When BR_USE_WIN32_TIME is enabled, the X.509 validation engine obtains
+ * the current time from the OS by calling the Win32 function
+ * GetSystemTimeAsFileTime().
+ *
+ * Note: if both BR_USE_UNIX_TIME and BR_USE_WIN32_TIME are defined, the
+ * former takes precedence.
+ *
+#define BR_USE_WIN32_TIME 1
+ */
+
+/*
+ * When BR_ARMEL_CORTEXM_GCC is enabled, some operations are replaced with
+ * inline assembly which is shorter and/or faster. This should be used
+ * only when all of the following are true:
+ * - target architecture is ARM in Thumb mode
+ * - target endianness is little-endian
+ * - compiler is GCC (or GCC-compatible for inline assembly syntax)
+ *
+ * This is meant for the low-end cores (Cortex M0, M0+, M1, M3).
+ * Note: if BR_LOMUL is not explicitly enabled or disabled, then
+ * enabling BR_ARMEL_CORTEXM_GCC also enables BR_LOMUL.
+ *
+#define BR_ARMEL_CORTEXM_GCC 1
+ */
+
+/*
+ * When BR_AES_X86NI is enabled, the AES implementation using the x86 "NI"
+ * instructions (dedicated AES opcodes) will be compiled. If this is not
+ * enabled explicitly, then that AES implementation will be compiled only
+ * if a compatible compiler is detected. If set explicitly to 0, the
+ * implementation will not be compiled at all.
+ *
+#define BR_AES_X86NI 1
+ */
+
+/*
+ * When BR_SSE2 is enabled, SSE2 intrinsics will be used for some
+ * algorithm implementations that use them (e.g. chacha20_sse2). If this
+ * is not enabled explicitly, then support for SSE2 intrinsics will be
+ * automatically detected. If set explicitly to 0, then SSE2 code will
+ * not be compiled at all.
+ *
+#define BR_SSE2 1
+ */
+
+/*
+ * When BR_POWER8 is enabled, the AES implementation using the POWER ISA
+ * 2.07 opcodes (available on POWER8 processors and later) is compiled.
+ * If this is not enabled explicitly, then that implementation will be
+ * compiled only if a compatible compiler is detected, _and_ the target
+ * architecture is POWER8 or later.
+ *
+#define BR_POWER8 1
+ */
+
+/*
+ * When BR_INT128 is enabled, then code using the 'unsigned __int64'
+ * and 'unsigned __int128' types will be used to leverage 64x64->128
+ * unsigned multiplications. This should work with GCC and compatible
+ * compilers on 64-bit architectures.
+ *
+#define BR_INT128 1
+ */
+
+/*
+ * When BR_UMUL128 is enabled, then code using the '_umul128()' and
+ * '_addcarry_u64()' intrinsics will be used to implement 64x64->128
+ * unsigned multiplications. This should work on Visual C on x64 systems.
+ *
+#define BR_UMUL128 1
+ */
+
+/*
+ * When BR_LE_UNALIGNED is enabled, then the current architecture is
+ * assumed to use little-endian encoding for integers, and to tolerate
+ * unaligned accesses with no or minimal time penalty.
+ *
+#define BR_LE_UNALIGNED 1
+ */
+
+/*
+ * When BR_BE_UNALIGNED is enabled, then the current architecture is
+ * assumed to use big-endian encoding for integers, and to tolerate
+ * unaligned accesses with no or minimal time penalty.
+ *
+#define BR_BE_UNALIGNED 1
+ */
+
+#define KILL_TAIL_CALL() {int x=1;}
+#endif
diff --git a/test/monniaux/BearSSL/src/ec/ec_all_m15.c b/test/monniaux/BearSSL/src/ec/ec_all_m15.c
new file mode 100644
index 00000000..bb550e18
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_all_m15.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.generator(curve, len);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.generator(curve, len);
+ default:
+ return br_ec_prime_i15.generator(curve, len);
+ }
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.order(curve, len);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.order(curve, len);
+ default:
+ return br_ec_prime_i15.order(curve, len);
+ }
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.xoff(curve, len);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.xoff(curve, len);
+ default:
+ return br_ec_prime_i15.xoff(curve, len);
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.mul(G, Glen, kb, kblen, curve);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.mul(G, Glen, kb, kblen, curve);
+ default:
+ return br_ec_prime_i15.mul(G, Glen, kb, kblen, curve);
+ }
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.mulgen(R, x, xlen, curve);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.mulgen(R, x, xlen, curve);
+ default:
+ return br_ec_prime_i15.mulgen(R, x, xlen, curve);
+ }
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return br_ec_p256_m15.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+ case BR_EC_curve25519:
+ return br_ec_c25519_m15.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+ default:
+ return br_ec_prime_i15.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+ }
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_all_m15 = {
+ (uint32_t)0x23800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_all_m31.c b/test/monniaux/BearSSL/src/ec/ec_all_m31.c
new file mode 100644
index 00000000..8fd8c3c0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_all_m31.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.generator(curve, len);
+#else
+ return br_ec_p256_m31.generator(curve, len);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.generator(curve, len);
+#else
+ return br_ec_c25519_m31.generator(curve, len);
+#endif
+ default:
+ return br_ec_prime_i31.generator(curve, len);
+ }
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.order(curve, len);
+#else
+ return br_ec_p256_m31.order(curve, len);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.order(curve, len);
+#else
+ return br_ec_c25519_m31.order(curve, len);
+#endif
+ default:
+ return br_ec_prime_i31.order(curve, len);
+ }
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.xoff(curve, len);
+#else
+ return br_ec_p256_m31.xoff(curve, len);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.xoff(curve, len);
+#else
+ return br_ec_c25519_m31.xoff(curve, len);
+#endif
+ default:
+ return br_ec_prime_i31.xoff(curve, len);
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.mul(G, Glen, kb, kblen, curve);
+#else
+ return br_ec_p256_m31.mul(G, Glen, kb, kblen, curve);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.mul(G, Glen, kb, kblen, curve);
+#else
+ return br_ec_c25519_m31.mul(G, Glen, kb, kblen, curve);
+#endif
+ default:
+ return br_ec_prime_i31.mul(G, Glen, kb, kblen, curve);
+ }
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.mulgen(R, x, xlen, curve);
+#else
+ return br_ec_p256_m31.mulgen(R, x, xlen, curve);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.mulgen(R, x, xlen, curve);
+#else
+ return br_ec_c25519_m31.mulgen(R, x, xlen, curve);
+#endif
+ default:
+ return br_ec_prime_i31.mulgen(R, x, xlen, curve);
+ }
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_p256_m64.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+#else
+ return br_ec_p256_m31.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+#endif
+ case BR_EC_curve25519:
+#if BR_INT128 || BR_UMUL128
+ return br_ec_c25519_m64.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+#else
+ return br_ec_c25519_m31.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+#endif
+ default:
+ return br_ec_prime_i31.muladd(A, B, len,
+ x, xlen, y, ylen, curve);
+ }
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_all_m31 = {
+ (uint32_t)0x23800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_i15.c b/test/monniaux/BearSSL/src/ec/ec_c25519_i15.c
new file mode 100644
index 00000000..8fadcf48
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_i15.c
@@ -0,0 +1,398 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Parameters for the field:
+ * - field modulus p = 2^255-19
+ * - R^2 mod p (R = 2^(15k) for the smallest k such that R >= p)
+ */
+
+static const uint16_t C255_P[] = {
+ 0x0110,
+ 0x7FED, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF
+};
+
+#define P0I 0x4A1B
+
+static const uint16_t C255_R2[] = {
+ 0x0110,
+ 0x0169, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000
+};
+
+/* obsolete
+#include <stdio.h>
+#include <stdlib.h>
+static void
+print_int_mont(const char *name, const uint16_t *x)
+{
+ uint16_t y[18];
+ unsigned char tmp[32];
+ size_t u;
+
+ printf("%s = ", name);
+ memcpy(y, x, sizeof y);
+ br_i15_from_monty(y, C255_P, P0I);
+ br_i15_encode(tmp, sizeof tmp, y);
+ for (u = 0; u < sizeof tmp; u ++) {
+ printf("%02X", tmp[u]);
+ }
+ printf("\n");
+}
+*/
+
+static const uint16_t C255_A24[] = {
+ 0x0110,
+ 0x45D3, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000
+};
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+static void
+cswap(uint16_t *a, uint16_t *b, uint32_t ctl)
+{
+ int i;
+
+ ctl = -ctl;
+ for (i = 0; i < 18; i ++) {
+ uint32_t aw, bw, tw;
+
+ aw = a[i];
+ bw = b[i];
+ tw = ctl & (aw ^ bw);
+ a[i] = aw ^ tw;
+ b[i] = bw ^ tw;
+ }
+}
+
+static void
+c255_add(uint16_t *d, const uint16_t *a, const uint16_t *b)
+{
+ uint32_t ctl;
+ uint16_t t[18];
+
+ memcpy(t, a, sizeof t);
+ ctl = br_i15_add(t, b, 1);
+ ctl |= NOT(br_i15_sub(t, C255_P, 0));
+ br_i15_sub(t, C255_P, ctl);
+ memcpy(d, t, sizeof t);
+}
+
+static void
+c255_sub(uint16_t *d, const uint16_t *a, const uint16_t *b)
+{
+ uint16_t t[18];
+
+ memcpy(t, a, sizeof t);
+ br_i15_add(t, C255_P, br_i15_sub(t, b, 1));
+ memcpy(d, t, sizeof t);
+}
+
+static void
+c255_mul(uint16_t *d, const uint16_t *a, const uint16_t *b)
+{
+ uint16_t t[18];
+
+ br_i15_montymul(t, a, b, C255_P, P0I);
+ memcpy(d, t, sizeof t);
+}
+
+static void
+byteswap(unsigned char *G)
+{
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ unsigned char t;
+
+ t = G[i];
+ G[i] = G[31 - i];
+ G[31 - i] = t;
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+#define ILEN (18 * sizeof(uint16_t))
+
+ /*
+ * The a[] and b[] arrays have an extra word to allow for
+ * decoding without using br_i15_decode_reduce().
+ */
+ uint16_t x1[18], x2[18], x3[18], z2[18], z3[18];
+ uint16_t a[19], aa[18], b[19], bb[18];
+ uint16_t c[18], d[18], e[18], da[18], cb[18];
+ unsigned char k[32];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+ G[31] &= 0x7F;
+
+ /*
+ * Byteswap the point encoding, because it uses little-endian, and
+ * the generic decoding routine uses big-endian.
+ */
+ byteswap(G);
+
+ /*
+ * Decode the point ('u' coordinate). This should be reduced
+ * modulo p, but we prefer to avoid the dependency on
+ * br_i15_decode_reduce(). Instead, we use br_i15_decode_mod()
+ * with a synthetic modulus of value 2^255 (this must work
+ * since G was truncated to 255 bits), then use a conditional
+ * subtraction. We use br_i15_decode_mod() and not
+ * br_i15_decode(), because the ec_prime_i15 implementation uses
+ * the former but not the latter.
+ * br_i15_decode_reduce(a, G, 32, C255_P);
+ */
+ br_i15_zero(b, 0x111);
+ b[18] = 1;
+ br_i15_decode_mod(a, G, 32, b);
+ a[0] = 0x110;
+ br_i15_sub(a, C255_P, NOT(br_i15_sub(a, C255_P, 0)));
+
+ /*
+ * Initialise variables x1, x2, z2, x3 and z3. We set all of them
+ * into Montgomery representation.
+ */
+ br_i15_montymul(x1, a, C255_R2, C255_P, P0I);
+ memcpy(x3, x1, ILEN);
+ br_i15_zero(z2, C255_P[0]);
+ memcpy(x2, z2, ILEN);
+ x2[1] = 19;
+ memcpy(z3, x2, ILEN);
+
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ /* obsolete
+ print_int_mont("x1", x1);
+ */
+
+ swap = 0;
+ for (i = 254; i >= 0; i --) {
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+ swap = kt;
+
+ /* obsolete
+ print_int_mont("x2", x2);
+ print_int_mont("z2", z2);
+ print_int_mont("x3", x3);
+ print_int_mont("z3", z3);
+ */
+
+ c255_add(a, x2, z2);
+ c255_mul(aa, a, a);
+ c255_sub(b, x2, z2);
+ c255_mul(bb, b, b);
+ c255_sub(e, aa, bb);
+ c255_add(c, x3, z3);
+ c255_sub(d, x3, z3);
+ c255_mul(da, d, a);
+ c255_mul(cb, c, b);
+
+ /* obsolete
+ print_int_mont("a ", a);
+ print_int_mont("aa", aa);
+ print_int_mont("b ", b);
+ print_int_mont("bb", bb);
+ print_int_mont("e ", e);
+ print_int_mont("c ", c);
+ print_int_mont("d ", d);
+ print_int_mont("da", da);
+ print_int_mont("cb", cb);
+ */
+
+ c255_add(x3, da, cb);
+ c255_mul(x3, x3, x3);
+ c255_sub(z3, da, cb);
+ c255_mul(z3, z3, z3);
+ c255_mul(z3, z3, x1);
+ c255_mul(x2, aa, bb);
+ c255_mul(z2, C255_A24, e);
+ c255_add(z2, z2, aa);
+ c255_mul(z2, e, z2);
+
+ /* obsolete
+ print_int_mont("x2", x2);
+ print_int_mont("z2", z2);
+ print_int_mont("x3", x3);
+ print_int_mont("z3", z3);
+ */
+ }
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+
+ /*
+ * Inverse z2 with a modular exponentiation. This is a simple
+ * square-and-multiply algorithm; we mutualise most non-squarings
+ * since the exponent contains almost only ones.
+ */
+ memcpy(a, z2, ILEN);
+ for (i = 0; i < 15; i ++) {
+ c255_mul(a, a, a);
+ c255_mul(a, a, z2);
+ }
+ memcpy(b, a, ILEN);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ c255_mul(b, b, b);
+ }
+ c255_mul(b, b, a);
+ }
+ for (i = 14; i >= 0; i --) {
+ c255_mul(b, b, b);
+ if ((0xFFEB >> i) & 1) {
+ c255_mul(b, z2, b);
+ }
+ }
+ c255_mul(b, x2, b);
+
+ /*
+ * To avoid a dependency on br_i15_from_monty(), we use a
+ * Montgomery multiplication with 1.
+ * memcpy(x2, b, ILEN);
+ * br_i15_from_monty(x2, C255_P, P0I);
+ */
+ br_i15_zero(a, C255_P[0]);
+ a[1] = 1;
+ br_i15_montymul(x2, a, b, C255_P, P0I);
+
+ br_i15_encode(G, 32, x2);
+ byteswap(G);
+ return 1;
+
+#undef ILEN
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_i15 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_i31.c b/test/monniaux/BearSSL/src/ec/ec_c25519_i31.c
new file mode 100644
index 00000000..f8ffc2c2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_i31.c
@@ -0,0 +1,390 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Parameters for the field:
+ * - field modulus p = 2^255-19
+ * - R^2 mod p (R = 2^(31k) for the smallest k such that R >= p)
+ */
+
+static const uint32_t C255_P[] = {
+ 0x00000107,
+ 0x7FFFFFED, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x0000007F
+};
+
+#define P0I 0x286BCA1B
+
+static const uint32_t C255_R2[] = {
+ 0x00000107,
+ 0x00000000, 0x02D20000, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000
+};
+
+static const uint32_t C255_A24[] = {
+ 0x00000107,
+ 0x53000000, 0x0000468B, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000
+};
+
+/* obsolete
+#include <stdio.h>
+#include <stdlib.h>
+static void
+print_int_mont(const char *name, const uint32_t *x)
+{
+ uint32_t y[10];
+ unsigned char tmp[32];
+ size_t u;
+
+ printf("%s = ", name);
+ memcpy(y, x, sizeof y);
+ br_i31_from_monty(y, C255_P, P0I);
+ br_i31_encode(tmp, sizeof tmp, y);
+ for (u = 0; u < sizeof tmp; u ++) {
+ printf("%02X", tmp[u]);
+ }
+ printf("\n");
+}
+*/
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+static void
+cswap(uint32_t *a, uint32_t *b, uint32_t ctl)
+{
+ int i;
+
+ ctl = -ctl;
+ for (i = 0; i < 10; i ++) {
+ uint32_t aw, bw, tw;
+
+ aw = a[i];
+ bw = b[i];
+ tw = ctl & (aw ^ bw);
+ a[i] = aw ^ tw;
+ b[i] = bw ^ tw;
+ }
+}
+
+static void
+c255_add(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t ctl;
+ uint32_t t[10];
+
+ memcpy(t, a, sizeof t);
+ ctl = br_i31_add(t, b, 1);
+ ctl |= NOT(br_i31_sub(t, C255_P, 0));
+ br_i31_sub(t, C255_P, ctl);
+ memcpy(d, t, sizeof t);
+}
+
+static void
+c255_sub(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[10];
+
+ memcpy(t, a, sizeof t);
+ br_i31_add(t, C255_P, br_i31_sub(t, b, 1));
+ memcpy(d, t, sizeof t);
+}
+
+static void
+c255_mul(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[10];
+
+ br_i31_montymul(t, a, b, C255_P, P0I);
+ memcpy(d, t, sizeof t);
+}
+
+static void
+byteswap(unsigned char *G)
+{
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ unsigned char t;
+
+ t = G[i];
+ G[i] = G[31 - i];
+ G[31 - i] = t;
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ uint32_t x1[10], x2[10], x3[10], z2[10], z3[10];
+ uint32_t a[10], aa[10], b[10], bb[10];
+ uint32_t c[10], d[10], e[10], da[10], cb[10];
+ unsigned char k[32];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+ G[31] &= 0x7F;
+
+ /*
+ * Byteswap the point encoding, because it uses little-endian, and
+ * the generic decoding routine uses big-endian.
+ */
+ byteswap(G);
+
+ /*
+ * Decode the point ('u' coordinate). This should be reduced
+ * modulo p, but we prefer to avoid the dependency on
+ * br_i31_decode_reduce(). Instead, we use br_i31_decode_mod()
+ * with a synthetic modulus of value 2^255 (this must work
+ * since G was truncated to 255 bits), then use a conditional
+ * subtraction. We use br_i31_decode_mod() and not
+ * br_i31_decode(), because the ec_prime_i31 implementation uses
+ * the former but not the latter.
+ * br_i31_decode_reduce(a, G, 32, C255_P);
+ */
+ br_i31_zero(b, 0x108);
+ b[9] = 0x0080;
+ br_i31_decode_mod(a, G, 32, b);
+ a[0] = 0x107;
+ br_i31_sub(a, C255_P, NOT(br_i31_sub(a, C255_P, 0)));
+
+ /*
+ * Initialise variables x1, x2, z2, x3 and z3. We set all of them
+ * into Montgomery representation.
+ */
+ br_i31_montymul(x1, a, C255_R2, C255_P, P0I);
+ memcpy(x3, x1, sizeof x1);
+ br_i31_zero(z2, C255_P[0]);
+ memcpy(x2, z2, sizeof z2);
+ x2[1] = 0x13000000;
+ memcpy(z3, x2, sizeof x2);
+
+ /*
+ * kb[] is in big-endian notation, but possibly shorter than k[].
+ */
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ /* obsolete
+ print_int_mont("x1", x1);
+ */
+
+ swap = 0;
+ for (i = 254; i >= 0; i --) {
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+ swap = kt;
+
+ /* obsolete
+ print_int_mont("x2", x2);
+ print_int_mont("z2", z2);
+ print_int_mont("x3", x3);
+ print_int_mont("z3", z3);
+ */
+
+ c255_add(a, x2, z2);
+ c255_mul(aa, a, a);
+ c255_sub(b, x2, z2);
+ c255_mul(bb, b, b);
+ c255_sub(e, aa, bb);
+ c255_add(c, x3, z3);
+ c255_sub(d, x3, z3);
+ c255_mul(da, d, a);
+ c255_mul(cb, c, b);
+
+ /* obsolete
+ print_int_mont("a ", a);
+ print_int_mont("aa", aa);
+ print_int_mont("b ", b);
+ print_int_mont("bb", bb);
+ print_int_mont("e ", e);
+ print_int_mont("c ", c);
+ print_int_mont("d ", d);
+ print_int_mont("da", da);
+ print_int_mont("cb", cb);
+ */
+
+ c255_add(x3, da, cb);
+ c255_mul(x3, x3, x3);
+ c255_sub(z3, da, cb);
+ c255_mul(z3, z3, z3);
+ c255_mul(z3, z3, x1);
+ c255_mul(x2, aa, bb);
+ c255_mul(z2, C255_A24, e);
+ c255_add(z2, z2, aa);
+ c255_mul(z2, e, z2);
+
+ /* obsolete
+ print_int_mont("x2", x2);
+ print_int_mont("z2", z2);
+ print_int_mont("x3", x3);
+ print_int_mont("z3", z3);
+ */
+ }
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+
+ /*
+ * Inverse z2 with a modular exponentiation. This is a simple
+ * square-and-multiply algorithm; we mutualise most non-squarings
+ * since the exponent contains almost only ones.
+ */
+ memcpy(a, z2, sizeof z2);
+ for (i = 0; i < 15; i ++) {
+ c255_mul(a, a, a);
+ c255_mul(a, a, z2);
+ }
+ memcpy(b, a, sizeof a);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ c255_mul(b, b, b);
+ }
+ c255_mul(b, b, a);
+ }
+ for (i = 14; i >= 0; i --) {
+ c255_mul(b, b, b);
+ if ((0xFFEB >> i) & 1) {
+ c255_mul(b, z2, b);
+ }
+ }
+ c255_mul(b, x2, b);
+
+ /*
+ * To avoid a dependency on br_i31_from_monty(), we use
+ * a Montgomery multiplication with 1.
+ * memcpy(x2, b, sizeof b);
+ * br_i31_from_monty(x2, C255_P, P0I);
+ */
+ br_i31_zero(a, C255_P[0]);
+ a[1] = 1;
+ br_i31_montymul(x2, a, b, C255_P, P0I);
+
+ br_i31_encode(G, 32, x2);
+ byteswap(G);
+ return 1;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_i31 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_m15.c b/test/monniaux/BearSSL/src/ec/ec_c25519_m15.c
new file mode 100644
index 00000000..deff55b3
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_m15.c
@@ -0,0 +1,1478 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* obsolete
+#include <stdio.h>
+#include <stdlib.h>
+static void
+print_int(const char *name, const uint32_t *x)
+{
+ size_t u;
+ unsigned char tmp[36];
+
+ printf("%s = ", name);
+ for (u = 0; u < 20; u ++) {
+ if (x[u] > 0x1FFF) {
+ printf("INVALID:");
+ for (u = 0; u < 20; u ++) {
+ printf(" %04X", x[u]);
+ }
+ printf("\n");
+ return;
+ }
+ }
+ memset(tmp, 0, sizeof tmp);
+ for (u = 0; u < 20; u ++) {
+ uint32_t w;
+ int j, k;
+
+ w = x[u];
+ j = 13 * (int)u;
+ k = j & 7;
+ if (k != 0) {
+ w <<= k;
+ j -= k;
+ }
+ k = j >> 3;
+ tmp[35 - k] |= (unsigned char)w;
+ tmp[34 - k] |= (unsigned char)(w >> 8);
+ tmp[33 - k] |= (unsigned char)(w >> 16);
+ tmp[32 - k] |= (unsigned char)(w >> 24);
+ }
+ for (u = 4; u < 36; u ++) {
+ printf("%02X", tmp[u]);
+ }
+ printf("\n");
+}
+*/
+
+/*
+ * If BR_NO_ARITH_SHIFT is undefined, or defined to 0, then we _assume_
+ * that right-shifting a signed negative integer copies the sign bit
+ * (arithmetic right-shift). This is "implementation-defined behaviour",
+ * i.e. it is not undefined, but it may differ between compilers. Each
+ * compiler is supposed to document its behaviour in that respect. GCC
+ * explicitly defines that an arithmetic right shift is used. We expect
+ * all other compilers to do the same, because underlying CPU offer an
+ * arithmetic right shift opcode that could not be used otherwise.
+ */
+#if BR_NO_ARITH_SHIFT
+#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
+ | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
+#else
+#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
+#endif
+
+/*
+ * Convert an integer from unsigned little-endian encoding to a sequence of
+ * 13-bit words in little-endian order. The final "partial" word is
+ * returned.
+ */
+static uint32_t
+le8_to_le13(uint32_t *dst, const unsigned char *src, size_t len)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ acc |= (uint32_t)(*src ++) << acc_len;
+ acc_len += 8;
+ if (acc_len >= 13) {
+ *dst ++ = acc & 0x1FFF;
+ acc >>= 13;
+ acc_len -= 13;
+ }
+ }
+ return acc;
+}
+
+/*
+ * Convert an integer (13-bit words, little-endian) to unsigned
+ * little-endian encoding. The total encoding length is provided; all
+ * the destination bytes will be filled.
+ */
+static void
+le13_to_le8(unsigned char *dst, size_t len, const uint32_t *src)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ if (acc_len < 8) {
+ acc |= (*src ++) << acc_len;
+ acc_len += 13;
+ }
+ *dst ++ = (unsigned char)acc;
+ acc >>= 8;
+ acc_len -= 8;
+ }
+}
+
+/*
+ * Normalise an array of words to a strict 13 bits per word. Returned
+ * value is the resulting carry. The source (w) and destination (d)
+ * arrays may be identical, but shall not overlap partially.
+ */
+static inline uint32_t
+norm13(uint32_t *d, const uint32_t *w, size_t len)
+{
+ size_t u;
+ uint32_t cc;
+
+ cc = 0;
+ for (u = 0; u < len; u ++) {
+ int32_t z;
+
+ z = w[u] + cc;
+ d[u] = z & 0x1FFF;
+ cc = ARSH(z, 13);
+ }
+ return cc;
+}
+
+/*
+ * mul20() multiplies two 260-bit integers together. Each word must fit
+ * on 13 bits; source operands use 20 words, destination operand
+ * receives 40 words. All overlaps allowed.
+ *
+ * square20() computes the square of a 260-bit integer. Each word must
+ * fit on 13 bits; source operand uses 20 words, destination operand
+ * receives 40 words. All overlaps allowed.
+ */
+
+#if BR_SLOW_MUL15
+
+static void
+mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * Two-level Karatsuba: turns a 20x20 multiplication into
+ * nine 5x5 multiplications. We use 13-bit words but do not
+ * propagate carries immediately, so words may expand:
+ *
+ * - First Karatsuba decomposition turns the 20x20 mul on
+ * 13-bit words into three 10x10 muls, two on 13-bit words
+ * and one on 14-bit words.
+ *
+ * - Second Karatsuba decomposition further splits these into:
+ *
+ * * four 5x5 muls on 13-bit words
+ * * four 5x5 muls on 14-bit words
+ * * one 5x5 mul on 15-bit words
+ *
+ * Highest word value is 8191, 16382 or 32764, for 13-bit, 14-bit
+ * or 15-bit words, respectively.
+ */
+ uint32_t u[45], v[45], w[90];
+ uint32_t cc;
+ int i;
+
+#define ZADD(dw, d_off, s1w, s1_off, s2w, s2_off) do { \
+ (dw)[5 * (d_off) + 0] = (s1w)[5 * (s1_off) + 0] \
+ + (s2w)[5 * (s2_off) + 0]; \
+ (dw)[5 * (d_off) + 1] = (s1w)[5 * (s1_off) + 1] \
+ + (s2w)[5 * (s2_off) + 1]; \
+ (dw)[5 * (d_off) + 2] = (s1w)[5 * (s1_off) + 2] \
+ + (s2w)[5 * (s2_off) + 2]; \
+ (dw)[5 * (d_off) + 3] = (s1w)[5 * (s1_off) + 3] \
+ + (s2w)[5 * (s2_off) + 3]; \
+ (dw)[5 * (d_off) + 4] = (s1w)[5 * (s1_off) + 4] \
+ + (s2w)[5 * (s2_off) + 4]; \
+ } while (0)
+
+#define ZADDT(dw, d_off, sw, s_off) do { \
+ (dw)[5 * (d_off) + 0] += (sw)[5 * (s_off) + 0]; \
+ (dw)[5 * (d_off) + 1] += (sw)[5 * (s_off) + 1]; \
+ (dw)[5 * (d_off) + 2] += (sw)[5 * (s_off) + 2]; \
+ (dw)[5 * (d_off) + 3] += (sw)[5 * (s_off) + 3]; \
+ (dw)[5 * (d_off) + 4] += (sw)[5 * (s_off) + 4]; \
+ } while (0)
+
+#define ZSUB2F(dw, d_off, s1w, s1_off, s2w, s2_off) do { \
+ (dw)[5 * (d_off) + 0] -= (s1w)[5 * (s1_off) + 0] \
+ + (s2w)[5 * (s2_off) + 0]; \
+ (dw)[5 * (d_off) + 1] -= (s1w)[5 * (s1_off) + 1] \
+ + (s2w)[5 * (s2_off) + 1]; \
+ (dw)[5 * (d_off) + 2] -= (s1w)[5 * (s1_off) + 2] \
+ + (s2w)[5 * (s2_off) + 2]; \
+ (dw)[5 * (d_off) + 3] -= (s1w)[5 * (s1_off) + 3] \
+ + (s2w)[5 * (s2_off) + 3]; \
+ (dw)[5 * (d_off) + 4] -= (s1w)[5 * (s1_off) + 4] \
+ + (s2w)[5 * (s2_off) + 4]; \
+ } while (0)
+
+#define CPR1(w, cprcc) do { \
+ uint32_t cprz = (w) + cprcc; \
+ (w) = cprz & 0x1FFF; \
+ cprcc = cprz >> 13; \
+ } while (0)
+
+#define CPR(dw, d_off) do { \
+ uint32_t cprcc; \
+ cprcc = 0; \
+ CPR1((dw)[(d_off) + 0], cprcc); \
+ CPR1((dw)[(d_off) + 1], cprcc); \
+ CPR1((dw)[(d_off) + 2], cprcc); \
+ CPR1((dw)[(d_off) + 3], cprcc); \
+ CPR1((dw)[(d_off) + 4], cprcc); \
+ CPR1((dw)[(d_off) + 5], cprcc); \
+ CPR1((dw)[(d_off) + 6], cprcc); \
+ CPR1((dw)[(d_off) + 7], cprcc); \
+ CPR1((dw)[(d_off) + 8], cprcc); \
+ (dw)[(d_off) + 9] = cprcc; \
+ } while (0)
+
+ memcpy(u, a, 20 * sizeof *a);
+ ZADD(u, 4, a, 0, a, 1);
+ ZADD(u, 5, a, 2, a, 3);
+ ZADD(u, 6, a, 0, a, 2);
+ ZADD(u, 7, a, 1, a, 3);
+ ZADD(u, 8, u, 6, u, 7);
+
+ memcpy(v, b, 20 * sizeof *b);
+ ZADD(v, 4, b, 0, b, 1);
+ ZADD(v, 5, b, 2, b, 3);
+ ZADD(v, 6, b, 0, b, 2);
+ ZADD(v, 7, b, 1, b, 3);
+ ZADD(v, 8, v, 6, v, 7);
+
+ /*
+ * Do the eight first 8x8 muls. Source words are at most 16382
+ * each, so we can add product results together "as is" in 32-bit
+ * words.
+ */
+ for (i = 0; i < 40; i += 5) {
+ w[(i << 1) + 0] = MUL15(u[i + 0], v[i + 0]);
+ w[(i << 1) + 1] = MUL15(u[i + 0], v[i + 1])
+ + MUL15(u[i + 1], v[i + 0]);
+ w[(i << 1) + 2] = MUL15(u[i + 0], v[i + 2])
+ + MUL15(u[i + 1], v[i + 1])
+ + MUL15(u[i + 2], v[i + 0]);
+ w[(i << 1) + 3] = MUL15(u[i + 0], v[i + 3])
+ + MUL15(u[i + 1], v[i + 2])
+ + MUL15(u[i + 2], v[i + 1])
+ + MUL15(u[i + 3], v[i + 0]);
+ w[(i << 1) + 4] = MUL15(u[i + 0], v[i + 4])
+ + MUL15(u[i + 1], v[i + 3])
+ + MUL15(u[i + 2], v[i + 2])
+ + MUL15(u[i + 3], v[i + 1])
+ + MUL15(u[i + 4], v[i + 0]);
+ w[(i << 1) + 5] = MUL15(u[i + 1], v[i + 4])
+ + MUL15(u[i + 2], v[i + 3])
+ + MUL15(u[i + 3], v[i + 2])
+ + MUL15(u[i + 4], v[i + 1]);
+ w[(i << 1) + 6] = MUL15(u[i + 2], v[i + 4])
+ + MUL15(u[i + 3], v[i + 3])
+ + MUL15(u[i + 4], v[i + 2]);
+ w[(i << 1) + 7] = MUL15(u[i + 3], v[i + 4])
+ + MUL15(u[i + 4], v[i + 3]);
+ w[(i << 1) + 8] = MUL15(u[i + 4], v[i + 4]);
+ w[(i << 1) + 9] = 0;
+ }
+
+ /*
+ * For the 9th multiplication, source words are up to 32764,
+ * so we must do some carry propagation. If we add up to
+ * 4 products and the carry is no more than 524224, then the
+ * result fits in 32 bits, and the next carry will be no more
+ * than 524224 (because 4*(32764^2)+524224 < 8192*524225).
+ *
+ * We thus just skip one of the products in the middle word,
+ * then do a carry propagation (this reduces words to 13 bits
+ * each, except possibly the last, which may use up to 17 bits
+ * or so), then add the missing product.
+ */
+ w[80 + 0] = MUL15(u[40 + 0], v[40 + 0]);
+ w[80 + 1] = MUL15(u[40 + 0], v[40 + 1])
+ + MUL15(u[40 + 1], v[40 + 0]);
+ w[80 + 2] = MUL15(u[40 + 0], v[40 + 2])
+ + MUL15(u[40 + 1], v[40 + 1])
+ + MUL15(u[40 + 2], v[40 + 0]);
+ w[80 + 3] = MUL15(u[40 + 0], v[40 + 3])
+ + MUL15(u[40 + 1], v[40 + 2])
+ + MUL15(u[40 + 2], v[40 + 1])
+ + MUL15(u[40 + 3], v[40 + 0]);
+ w[80 + 4] = MUL15(u[40 + 0], v[40 + 4])
+ + MUL15(u[40 + 1], v[40 + 3])
+ + MUL15(u[40 + 2], v[40 + 2])
+ + MUL15(u[40 + 3], v[40 + 1]);
+ /* + MUL15(u[40 + 4], v[40 + 0]) */
+ w[80 + 5] = MUL15(u[40 + 1], v[40 + 4])
+ + MUL15(u[40 + 2], v[40 + 3])
+ + MUL15(u[40 + 3], v[40 + 2])
+ + MUL15(u[40 + 4], v[40 + 1]);
+ w[80 + 6] = MUL15(u[40 + 2], v[40 + 4])
+ + MUL15(u[40 + 3], v[40 + 3])
+ + MUL15(u[40 + 4], v[40 + 2]);
+ w[80 + 7] = MUL15(u[40 + 3], v[40 + 4])
+ + MUL15(u[40 + 4], v[40 + 3]);
+ w[80 + 8] = MUL15(u[40 + 4], v[40 + 4]);
+
+ CPR(w, 80);
+
+ w[80 + 4] += MUL15(u[40 + 4], v[40 + 0]);
+
+ /*
+ * The products on 14-bit words in slots 6 and 7 yield values
+ * up to 5*(16382^2) each, and we need to subtract two such
+ * values from the higher word. We need the subtraction to fit
+ * in a _signed_ 32-bit integer, i.e. 31 bits + a sign bit.
+ * However, 10*(16382^2) does not fit. So we must perform a
+ * bit of reduction here.
+ */
+ CPR(w, 60);
+ CPR(w, 70);
+
+ /*
+ * Recompose results.
+ */
+
+ /* 0..1*0..1 into 0..3 */
+ ZSUB2F(w, 8, w, 0, w, 2);
+ ZSUB2F(w, 9, w, 1, w, 3);
+ ZADDT(w, 1, w, 8);
+ ZADDT(w, 2, w, 9);
+
+ /* 2..3*2..3 into 4..7 */
+ ZSUB2F(w, 10, w, 4, w, 6);
+ ZSUB2F(w, 11, w, 5, w, 7);
+ ZADDT(w, 5, w, 10);
+ ZADDT(w, 6, w, 11);
+
+ /* (0..1+2..3)*(0..1+2..3) into 12..15 */
+ ZSUB2F(w, 16, w, 12, w, 14);
+ ZSUB2F(w, 17, w, 13, w, 15);
+ ZADDT(w, 13, w, 16);
+ ZADDT(w, 14, w, 17);
+
+ /* first-level recomposition */
+ ZSUB2F(w, 12, w, 0, w, 4);
+ ZSUB2F(w, 13, w, 1, w, 5);
+ ZSUB2F(w, 14, w, 2, w, 6);
+ ZSUB2F(w, 15, w, 3, w, 7);
+ ZADDT(w, 2, w, 12);
+ ZADDT(w, 3, w, 13);
+ ZADDT(w, 4, w, 14);
+ ZADDT(w, 5, w, 15);
+
+ /*
+ * Perform carry propagation to bring all words down to 13 bits.
+ */
+ cc = norm13(d, w, 40);
+ d[39] += (cc << 13);
+
+#undef ZADD
+#undef ZADDT
+#undef ZSUB2F
+#undef CPR1
+#undef CPR
+}
+
+static inline void
+square20(uint32_t *d, const uint32_t *a)
+{
+ mul20(d, a, a);
+}
+
+#else
+
+static void
+mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[39];
+
+ t[ 0] = MUL15(a[ 0], b[ 0]);
+ t[ 1] = MUL15(a[ 0], b[ 1])
+ + MUL15(a[ 1], b[ 0]);
+ t[ 2] = MUL15(a[ 0], b[ 2])
+ + MUL15(a[ 1], b[ 1])
+ + MUL15(a[ 2], b[ 0]);
+ t[ 3] = MUL15(a[ 0], b[ 3])
+ + MUL15(a[ 1], b[ 2])
+ + MUL15(a[ 2], b[ 1])
+ + MUL15(a[ 3], b[ 0]);
+ t[ 4] = MUL15(a[ 0], b[ 4])
+ + MUL15(a[ 1], b[ 3])
+ + MUL15(a[ 2], b[ 2])
+ + MUL15(a[ 3], b[ 1])
+ + MUL15(a[ 4], b[ 0]);
+ t[ 5] = MUL15(a[ 0], b[ 5])
+ + MUL15(a[ 1], b[ 4])
+ + MUL15(a[ 2], b[ 3])
+ + MUL15(a[ 3], b[ 2])
+ + MUL15(a[ 4], b[ 1])
+ + MUL15(a[ 5], b[ 0]);
+ t[ 6] = MUL15(a[ 0], b[ 6])
+ + MUL15(a[ 1], b[ 5])
+ + MUL15(a[ 2], b[ 4])
+ + MUL15(a[ 3], b[ 3])
+ + MUL15(a[ 4], b[ 2])
+ + MUL15(a[ 5], b[ 1])
+ + MUL15(a[ 6], b[ 0]);
+ t[ 7] = MUL15(a[ 0], b[ 7])
+ + MUL15(a[ 1], b[ 6])
+ + MUL15(a[ 2], b[ 5])
+ + MUL15(a[ 3], b[ 4])
+ + MUL15(a[ 4], b[ 3])
+ + MUL15(a[ 5], b[ 2])
+ + MUL15(a[ 6], b[ 1])
+ + MUL15(a[ 7], b[ 0]);
+ t[ 8] = MUL15(a[ 0], b[ 8])
+ + MUL15(a[ 1], b[ 7])
+ + MUL15(a[ 2], b[ 6])
+ + MUL15(a[ 3], b[ 5])
+ + MUL15(a[ 4], b[ 4])
+ + MUL15(a[ 5], b[ 3])
+ + MUL15(a[ 6], b[ 2])
+ + MUL15(a[ 7], b[ 1])
+ + MUL15(a[ 8], b[ 0]);
+ t[ 9] = MUL15(a[ 0], b[ 9])
+ + MUL15(a[ 1], b[ 8])
+ + MUL15(a[ 2], b[ 7])
+ + MUL15(a[ 3], b[ 6])
+ + MUL15(a[ 4], b[ 5])
+ + MUL15(a[ 5], b[ 4])
+ + MUL15(a[ 6], b[ 3])
+ + MUL15(a[ 7], b[ 2])
+ + MUL15(a[ 8], b[ 1])
+ + MUL15(a[ 9], b[ 0]);
+ t[10] = MUL15(a[ 0], b[10])
+ + MUL15(a[ 1], b[ 9])
+ + MUL15(a[ 2], b[ 8])
+ + MUL15(a[ 3], b[ 7])
+ + MUL15(a[ 4], b[ 6])
+ + MUL15(a[ 5], b[ 5])
+ + MUL15(a[ 6], b[ 4])
+ + MUL15(a[ 7], b[ 3])
+ + MUL15(a[ 8], b[ 2])
+ + MUL15(a[ 9], b[ 1])
+ + MUL15(a[10], b[ 0]);
+ t[11] = MUL15(a[ 0], b[11])
+ + MUL15(a[ 1], b[10])
+ + MUL15(a[ 2], b[ 9])
+ + MUL15(a[ 3], b[ 8])
+ + MUL15(a[ 4], b[ 7])
+ + MUL15(a[ 5], b[ 6])
+ + MUL15(a[ 6], b[ 5])
+ + MUL15(a[ 7], b[ 4])
+ + MUL15(a[ 8], b[ 3])
+ + MUL15(a[ 9], b[ 2])
+ + MUL15(a[10], b[ 1])
+ + MUL15(a[11], b[ 0]);
+ t[12] = MUL15(a[ 0], b[12])
+ + MUL15(a[ 1], b[11])
+ + MUL15(a[ 2], b[10])
+ + MUL15(a[ 3], b[ 9])
+ + MUL15(a[ 4], b[ 8])
+ + MUL15(a[ 5], b[ 7])
+ + MUL15(a[ 6], b[ 6])
+ + MUL15(a[ 7], b[ 5])
+ + MUL15(a[ 8], b[ 4])
+ + MUL15(a[ 9], b[ 3])
+ + MUL15(a[10], b[ 2])
+ + MUL15(a[11], b[ 1])
+ + MUL15(a[12], b[ 0]);
+ t[13] = MUL15(a[ 0], b[13])
+ + MUL15(a[ 1], b[12])
+ + MUL15(a[ 2], b[11])
+ + MUL15(a[ 3], b[10])
+ + MUL15(a[ 4], b[ 9])
+ + MUL15(a[ 5], b[ 8])
+ + MUL15(a[ 6], b[ 7])
+ + MUL15(a[ 7], b[ 6])
+ + MUL15(a[ 8], b[ 5])
+ + MUL15(a[ 9], b[ 4])
+ + MUL15(a[10], b[ 3])
+ + MUL15(a[11], b[ 2])
+ + MUL15(a[12], b[ 1])
+ + MUL15(a[13], b[ 0]);
+ t[14] = MUL15(a[ 0], b[14])
+ + MUL15(a[ 1], b[13])
+ + MUL15(a[ 2], b[12])
+ + MUL15(a[ 3], b[11])
+ + MUL15(a[ 4], b[10])
+ + MUL15(a[ 5], b[ 9])
+ + MUL15(a[ 6], b[ 8])
+ + MUL15(a[ 7], b[ 7])
+ + MUL15(a[ 8], b[ 6])
+ + MUL15(a[ 9], b[ 5])
+ + MUL15(a[10], b[ 4])
+ + MUL15(a[11], b[ 3])
+ + MUL15(a[12], b[ 2])
+ + MUL15(a[13], b[ 1])
+ + MUL15(a[14], b[ 0]);
+ t[15] = MUL15(a[ 0], b[15])
+ + MUL15(a[ 1], b[14])
+ + MUL15(a[ 2], b[13])
+ + MUL15(a[ 3], b[12])
+ + MUL15(a[ 4], b[11])
+ + MUL15(a[ 5], b[10])
+ + MUL15(a[ 6], b[ 9])
+ + MUL15(a[ 7], b[ 8])
+ + MUL15(a[ 8], b[ 7])
+ + MUL15(a[ 9], b[ 6])
+ + MUL15(a[10], b[ 5])
+ + MUL15(a[11], b[ 4])
+ + MUL15(a[12], b[ 3])
+ + MUL15(a[13], b[ 2])
+ + MUL15(a[14], b[ 1])
+ + MUL15(a[15], b[ 0]);
+ t[16] = MUL15(a[ 0], b[16])
+ + MUL15(a[ 1], b[15])
+ + MUL15(a[ 2], b[14])
+ + MUL15(a[ 3], b[13])
+ + MUL15(a[ 4], b[12])
+ + MUL15(a[ 5], b[11])
+ + MUL15(a[ 6], b[10])
+ + MUL15(a[ 7], b[ 9])
+ + MUL15(a[ 8], b[ 8])
+ + MUL15(a[ 9], b[ 7])
+ + MUL15(a[10], b[ 6])
+ + MUL15(a[11], b[ 5])
+ + MUL15(a[12], b[ 4])
+ + MUL15(a[13], b[ 3])
+ + MUL15(a[14], b[ 2])
+ + MUL15(a[15], b[ 1])
+ + MUL15(a[16], b[ 0]);
+ t[17] = MUL15(a[ 0], b[17])
+ + MUL15(a[ 1], b[16])
+ + MUL15(a[ 2], b[15])
+ + MUL15(a[ 3], b[14])
+ + MUL15(a[ 4], b[13])
+ + MUL15(a[ 5], b[12])
+ + MUL15(a[ 6], b[11])
+ + MUL15(a[ 7], b[10])
+ + MUL15(a[ 8], b[ 9])
+ + MUL15(a[ 9], b[ 8])
+ + MUL15(a[10], b[ 7])
+ + MUL15(a[11], b[ 6])
+ + MUL15(a[12], b[ 5])
+ + MUL15(a[13], b[ 4])
+ + MUL15(a[14], b[ 3])
+ + MUL15(a[15], b[ 2])
+ + MUL15(a[16], b[ 1])
+ + MUL15(a[17], b[ 0]);
+ t[18] = MUL15(a[ 0], b[18])
+ + MUL15(a[ 1], b[17])
+ + MUL15(a[ 2], b[16])
+ + MUL15(a[ 3], b[15])
+ + MUL15(a[ 4], b[14])
+ + MUL15(a[ 5], b[13])
+ + MUL15(a[ 6], b[12])
+ + MUL15(a[ 7], b[11])
+ + MUL15(a[ 8], b[10])
+ + MUL15(a[ 9], b[ 9])
+ + MUL15(a[10], b[ 8])
+ + MUL15(a[11], b[ 7])
+ + MUL15(a[12], b[ 6])
+ + MUL15(a[13], b[ 5])
+ + MUL15(a[14], b[ 4])
+ + MUL15(a[15], b[ 3])
+ + MUL15(a[16], b[ 2])
+ + MUL15(a[17], b[ 1])
+ + MUL15(a[18], b[ 0]);
+ t[19] = MUL15(a[ 0], b[19])
+ + MUL15(a[ 1], b[18])
+ + MUL15(a[ 2], b[17])
+ + MUL15(a[ 3], b[16])
+ + MUL15(a[ 4], b[15])
+ + MUL15(a[ 5], b[14])
+ + MUL15(a[ 6], b[13])
+ + MUL15(a[ 7], b[12])
+ + MUL15(a[ 8], b[11])
+ + MUL15(a[ 9], b[10])
+ + MUL15(a[10], b[ 9])
+ + MUL15(a[11], b[ 8])
+ + MUL15(a[12], b[ 7])
+ + MUL15(a[13], b[ 6])
+ + MUL15(a[14], b[ 5])
+ + MUL15(a[15], b[ 4])
+ + MUL15(a[16], b[ 3])
+ + MUL15(a[17], b[ 2])
+ + MUL15(a[18], b[ 1])
+ + MUL15(a[19], b[ 0]);
+ t[20] = MUL15(a[ 1], b[19])
+ + MUL15(a[ 2], b[18])
+ + MUL15(a[ 3], b[17])
+ + MUL15(a[ 4], b[16])
+ + MUL15(a[ 5], b[15])
+ + MUL15(a[ 6], b[14])
+ + MUL15(a[ 7], b[13])
+ + MUL15(a[ 8], b[12])
+ + MUL15(a[ 9], b[11])
+ + MUL15(a[10], b[10])
+ + MUL15(a[11], b[ 9])
+ + MUL15(a[12], b[ 8])
+ + MUL15(a[13], b[ 7])
+ + MUL15(a[14], b[ 6])
+ + MUL15(a[15], b[ 5])
+ + MUL15(a[16], b[ 4])
+ + MUL15(a[17], b[ 3])
+ + MUL15(a[18], b[ 2])
+ + MUL15(a[19], b[ 1]);
+ t[21] = MUL15(a[ 2], b[19])
+ + MUL15(a[ 3], b[18])
+ + MUL15(a[ 4], b[17])
+ + MUL15(a[ 5], b[16])
+ + MUL15(a[ 6], b[15])
+ + MUL15(a[ 7], b[14])
+ + MUL15(a[ 8], b[13])
+ + MUL15(a[ 9], b[12])
+ + MUL15(a[10], b[11])
+ + MUL15(a[11], b[10])
+ + MUL15(a[12], b[ 9])
+ + MUL15(a[13], b[ 8])
+ + MUL15(a[14], b[ 7])
+ + MUL15(a[15], b[ 6])
+ + MUL15(a[16], b[ 5])
+ + MUL15(a[17], b[ 4])
+ + MUL15(a[18], b[ 3])
+ + MUL15(a[19], b[ 2]);
+ t[22] = MUL15(a[ 3], b[19])
+ + MUL15(a[ 4], b[18])
+ + MUL15(a[ 5], b[17])
+ + MUL15(a[ 6], b[16])
+ + MUL15(a[ 7], b[15])
+ + MUL15(a[ 8], b[14])
+ + MUL15(a[ 9], b[13])
+ + MUL15(a[10], b[12])
+ + MUL15(a[11], b[11])
+ + MUL15(a[12], b[10])
+ + MUL15(a[13], b[ 9])
+ + MUL15(a[14], b[ 8])
+ + MUL15(a[15], b[ 7])
+ + MUL15(a[16], b[ 6])
+ + MUL15(a[17], b[ 5])
+ + MUL15(a[18], b[ 4])
+ + MUL15(a[19], b[ 3]);
+ t[23] = MUL15(a[ 4], b[19])
+ + MUL15(a[ 5], b[18])
+ + MUL15(a[ 6], b[17])
+ + MUL15(a[ 7], b[16])
+ + MUL15(a[ 8], b[15])
+ + MUL15(a[ 9], b[14])
+ + MUL15(a[10], b[13])
+ + MUL15(a[11], b[12])
+ + MUL15(a[12], b[11])
+ + MUL15(a[13], b[10])
+ + MUL15(a[14], b[ 9])
+ + MUL15(a[15], b[ 8])
+ + MUL15(a[16], b[ 7])
+ + MUL15(a[17], b[ 6])
+ + MUL15(a[18], b[ 5])
+ + MUL15(a[19], b[ 4]);
+ t[24] = MUL15(a[ 5], b[19])
+ + MUL15(a[ 6], b[18])
+ + MUL15(a[ 7], b[17])
+ + MUL15(a[ 8], b[16])
+ + MUL15(a[ 9], b[15])
+ + MUL15(a[10], b[14])
+ + MUL15(a[11], b[13])
+ + MUL15(a[12], b[12])
+ + MUL15(a[13], b[11])
+ + MUL15(a[14], b[10])
+ + MUL15(a[15], b[ 9])
+ + MUL15(a[16], b[ 8])
+ + MUL15(a[17], b[ 7])
+ + MUL15(a[18], b[ 6])
+ + MUL15(a[19], b[ 5]);
+ t[25] = MUL15(a[ 6], b[19])
+ + MUL15(a[ 7], b[18])
+ + MUL15(a[ 8], b[17])
+ + MUL15(a[ 9], b[16])
+ + MUL15(a[10], b[15])
+ + MUL15(a[11], b[14])
+ + MUL15(a[12], b[13])
+ + MUL15(a[13], b[12])
+ + MUL15(a[14], b[11])
+ + MUL15(a[15], b[10])
+ + MUL15(a[16], b[ 9])
+ + MUL15(a[17], b[ 8])
+ + MUL15(a[18], b[ 7])
+ + MUL15(a[19], b[ 6]);
+ t[26] = MUL15(a[ 7], b[19])
+ + MUL15(a[ 8], b[18])
+ + MUL15(a[ 9], b[17])
+ + MUL15(a[10], b[16])
+ + MUL15(a[11], b[15])
+ + MUL15(a[12], b[14])
+ + MUL15(a[13], b[13])
+ + MUL15(a[14], b[12])
+ + MUL15(a[15], b[11])
+ + MUL15(a[16], b[10])
+ + MUL15(a[17], b[ 9])
+ + MUL15(a[18], b[ 8])
+ + MUL15(a[19], b[ 7]);
+ t[27] = MUL15(a[ 8], b[19])
+ + MUL15(a[ 9], b[18])
+ + MUL15(a[10], b[17])
+ + MUL15(a[11], b[16])
+ + MUL15(a[12], b[15])
+ + MUL15(a[13], b[14])
+ + MUL15(a[14], b[13])
+ + MUL15(a[15], b[12])
+ + MUL15(a[16], b[11])
+ + MUL15(a[17], b[10])
+ + MUL15(a[18], b[ 9])
+ + MUL15(a[19], b[ 8]);
+ t[28] = MUL15(a[ 9], b[19])
+ + MUL15(a[10], b[18])
+ + MUL15(a[11], b[17])
+ + MUL15(a[12], b[16])
+ + MUL15(a[13], b[15])
+ + MUL15(a[14], b[14])
+ + MUL15(a[15], b[13])
+ + MUL15(a[16], b[12])
+ + MUL15(a[17], b[11])
+ + MUL15(a[18], b[10])
+ + MUL15(a[19], b[ 9]);
+ t[29] = MUL15(a[10], b[19])
+ + MUL15(a[11], b[18])
+ + MUL15(a[12], b[17])
+ + MUL15(a[13], b[16])
+ + MUL15(a[14], b[15])
+ + MUL15(a[15], b[14])
+ + MUL15(a[16], b[13])
+ + MUL15(a[17], b[12])
+ + MUL15(a[18], b[11])
+ + MUL15(a[19], b[10]);
+ t[30] = MUL15(a[11], b[19])
+ + MUL15(a[12], b[18])
+ + MUL15(a[13], b[17])
+ + MUL15(a[14], b[16])
+ + MUL15(a[15], b[15])
+ + MUL15(a[16], b[14])
+ + MUL15(a[17], b[13])
+ + MUL15(a[18], b[12])
+ + MUL15(a[19], b[11]);
+ t[31] = MUL15(a[12], b[19])
+ + MUL15(a[13], b[18])
+ + MUL15(a[14], b[17])
+ + MUL15(a[15], b[16])
+ + MUL15(a[16], b[15])
+ + MUL15(a[17], b[14])
+ + MUL15(a[18], b[13])
+ + MUL15(a[19], b[12]);
+ t[32] = MUL15(a[13], b[19])
+ + MUL15(a[14], b[18])
+ + MUL15(a[15], b[17])
+ + MUL15(a[16], b[16])
+ + MUL15(a[17], b[15])
+ + MUL15(a[18], b[14])
+ + MUL15(a[19], b[13]);
+ t[33] = MUL15(a[14], b[19])
+ + MUL15(a[15], b[18])
+ + MUL15(a[16], b[17])
+ + MUL15(a[17], b[16])
+ + MUL15(a[18], b[15])
+ + MUL15(a[19], b[14]);
+ t[34] = MUL15(a[15], b[19])
+ + MUL15(a[16], b[18])
+ + MUL15(a[17], b[17])
+ + MUL15(a[18], b[16])
+ + MUL15(a[19], b[15]);
+ t[35] = MUL15(a[16], b[19])
+ + MUL15(a[17], b[18])
+ + MUL15(a[18], b[17])
+ + MUL15(a[19], b[16]);
+ t[36] = MUL15(a[17], b[19])
+ + MUL15(a[18], b[18])
+ + MUL15(a[19], b[17]);
+ t[37] = MUL15(a[18], b[19])
+ + MUL15(a[19], b[18]);
+ t[38] = MUL15(a[19], b[19]);
+
+ d[39] = norm13(d, t, 39);
+}
+
+static void
+square20(uint32_t *d, const uint32_t *a)
+{
+ uint32_t t[39];
+
+ t[ 0] = MUL15(a[ 0], a[ 0]);
+ t[ 1] = ((MUL15(a[ 0], a[ 1])) << 1);
+ t[ 2] = MUL15(a[ 1], a[ 1])
+ + ((MUL15(a[ 0], a[ 2])) << 1);
+ t[ 3] = ((MUL15(a[ 0], a[ 3])
+ + MUL15(a[ 1], a[ 2])) << 1);
+ t[ 4] = MUL15(a[ 2], a[ 2])
+ + ((MUL15(a[ 0], a[ 4])
+ + MUL15(a[ 1], a[ 3])) << 1);
+ t[ 5] = ((MUL15(a[ 0], a[ 5])
+ + MUL15(a[ 1], a[ 4])
+ + MUL15(a[ 2], a[ 3])) << 1);
+ t[ 6] = MUL15(a[ 3], a[ 3])
+ + ((MUL15(a[ 0], a[ 6])
+ + MUL15(a[ 1], a[ 5])
+ + MUL15(a[ 2], a[ 4])) << 1);
+ t[ 7] = ((MUL15(a[ 0], a[ 7])
+ + MUL15(a[ 1], a[ 6])
+ + MUL15(a[ 2], a[ 5])
+ + MUL15(a[ 3], a[ 4])) << 1);
+ t[ 8] = MUL15(a[ 4], a[ 4])
+ + ((MUL15(a[ 0], a[ 8])
+ + MUL15(a[ 1], a[ 7])
+ + MUL15(a[ 2], a[ 6])
+ + MUL15(a[ 3], a[ 5])) << 1);
+ t[ 9] = ((MUL15(a[ 0], a[ 9])
+ + MUL15(a[ 1], a[ 8])
+ + MUL15(a[ 2], a[ 7])
+ + MUL15(a[ 3], a[ 6])
+ + MUL15(a[ 4], a[ 5])) << 1);
+ t[10] = MUL15(a[ 5], a[ 5])
+ + ((MUL15(a[ 0], a[10])
+ + MUL15(a[ 1], a[ 9])
+ + MUL15(a[ 2], a[ 8])
+ + MUL15(a[ 3], a[ 7])
+ + MUL15(a[ 4], a[ 6])) << 1);
+ t[11] = ((MUL15(a[ 0], a[11])
+ + MUL15(a[ 1], a[10])
+ + MUL15(a[ 2], a[ 9])
+ + MUL15(a[ 3], a[ 8])
+ + MUL15(a[ 4], a[ 7])
+ + MUL15(a[ 5], a[ 6])) << 1);
+ t[12] = MUL15(a[ 6], a[ 6])
+ + ((MUL15(a[ 0], a[12])
+ + MUL15(a[ 1], a[11])
+ + MUL15(a[ 2], a[10])
+ + MUL15(a[ 3], a[ 9])
+ + MUL15(a[ 4], a[ 8])
+ + MUL15(a[ 5], a[ 7])) << 1);
+ t[13] = ((MUL15(a[ 0], a[13])
+ + MUL15(a[ 1], a[12])
+ + MUL15(a[ 2], a[11])
+ + MUL15(a[ 3], a[10])
+ + MUL15(a[ 4], a[ 9])
+ + MUL15(a[ 5], a[ 8])
+ + MUL15(a[ 6], a[ 7])) << 1);
+ t[14] = MUL15(a[ 7], a[ 7])
+ + ((MUL15(a[ 0], a[14])
+ + MUL15(a[ 1], a[13])
+ + MUL15(a[ 2], a[12])
+ + MUL15(a[ 3], a[11])
+ + MUL15(a[ 4], a[10])
+ + MUL15(a[ 5], a[ 9])
+ + MUL15(a[ 6], a[ 8])) << 1);
+ t[15] = ((MUL15(a[ 0], a[15])
+ + MUL15(a[ 1], a[14])
+ + MUL15(a[ 2], a[13])
+ + MUL15(a[ 3], a[12])
+ + MUL15(a[ 4], a[11])
+ + MUL15(a[ 5], a[10])
+ + MUL15(a[ 6], a[ 9])
+ + MUL15(a[ 7], a[ 8])) << 1);
+ t[16] = MUL15(a[ 8], a[ 8])
+ + ((MUL15(a[ 0], a[16])
+ + MUL15(a[ 1], a[15])
+ + MUL15(a[ 2], a[14])
+ + MUL15(a[ 3], a[13])
+ + MUL15(a[ 4], a[12])
+ + MUL15(a[ 5], a[11])
+ + MUL15(a[ 6], a[10])
+ + MUL15(a[ 7], a[ 9])) << 1);
+ t[17] = ((MUL15(a[ 0], a[17])
+ + MUL15(a[ 1], a[16])
+ + MUL15(a[ 2], a[15])
+ + MUL15(a[ 3], a[14])
+ + MUL15(a[ 4], a[13])
+ + MUL15(a[ 5], a[12])
+ + MUL15(a[ 6], a[11])
+ + MUL15(a[ 7], a[10])
+ + MUL15(a[ 8], a[ 9])) << 1);
+ t[18] = MUL15(a[ 9], a[ 9])
+ + ((MUL15(a[ 0], a[18])
+ + MUL15(a[ 1], a[17])
+ + MUL15(a[ 2], a[16])
+ + MUL15(a[ 3], a[15])
+ + MUL15(a[ 4], a[14])
+ + MUL15(a[ 5], a[13])
+ + MUL15(a[ 6], a[12])
+ + MUL15(a[ 7], a[11])
+ + MUL15(a[ 8], a[10])) << 1);
+ t[19] = ((MUL15(a[ 0], a[19])
+ + MUL15(a[ 1], a[18])
+ + MUL15(a[ 2], a[17])
+ + MUL15(a[ 3], a[16])
+ + MUL15(a[ 4], a[15])
+ + MUL15(a[ 5], a[14])
+ + MUL15(a[ 6], a[13])
+ + MUL15(a[ 7], a[12])
+ + MUL15(a[ 8], a[11])
+ + MUL15(a[ 9], a[10])) << 1);
+ t[20] = MUL15(a[10], a[10])
+ + ((MUL15(a[ 1], a[19])
+ + MUL15(a[ 2], a[18])
+ + MUL15(a[ 3], a[17])
+ + MUL15(a[ 4], a[16])
+ + MUL15(a[ 5], a[15])
+ + MUL15(a[ 6], a[14])
+ + MUL15(a[ 7], a[13])
+ + MUL15(a[ 8], a[12])
+ + MUL15(a[ 9], a[11])) << 1);
+ t[21] = ((MUL15(a[ 2], a[19])
+ + MUL15(a[ 3], a[18])
+ + MUL15(a[ 4], a[17])
+ + MUL15(a[ 5], a[16])
+ + MUL15(a[ 6], a[15])
+ + MUL15(a[ 7], a[14])
+ + MUL15(a[ 8], a[13])
+ + MUL15(a[ 9], a[12])
+ + MUL15(a[10], a[11])) << 1);
+ t[22] = MUL15(a[11], a[11])
+ + ((MUL15(a[ 3], a[19])
+ + MUL15(a[ 4], a[18])
+ + MUL15(a[ 5], a[17])
+ + MUL15(a[ 6], a[16])
+ + MUL15(a[ 7], a[15])
+ + MUL15(a[ 8], a[14])
+ + MUL15(a[ 9], a[13])
+ + MUL15(a[10], a[12])) << 1);
+ t[23] = ((MUL15(a[ 4], a[19])
+ + MUL15(a[ 5], a[18])
+ + MUL15(a[ 6], a[17])
+ + MUL15(a[ 7], a[16])
+ + MUL15(a[ 8], a[15])
+ + MUL15(a[ 9], a[14])
+ + MUL15(a[10], a[13])
+ + MUL15(a[11], a[12])) << 1);
+ t[24] = MUL15(a[12], a[12])
+ + ((MUL15(a[ 5], a[19])
+ + MUL15(a[ 6], a[18])
+ + MUL15(a[ 7], a[17])
+ + MUL15(a[ 8], a[16])
+ + MUL15(a[ 9], a[15])
+ + MUL15(a[10], a[14])
+ + MUL15(a[11], a[13])) << 1);
+ t[25] = ((MUL15(a[ 6], a[19])
+ + MUL15(a[ 7], a[18])
+ + MUL15(a[ 8], a[17])
+ + MUL15(a[ 9], a[16])
+ + MUL15(a[10], a[15])
+ + MUL15(a[11], a[14])
+ + MUL15(a[12], a[13])) << 1);
+ t[26] = MUL15(a[13], a[13])
+ + ((MUL15(a[ 7], a[19])
+ + MUL15(a[ 8], a[18])
+ + MUL15(a[ 9], a[17])
+ + MUL15(a[10], a[16])
+ + MUL15(a[11], a[15])
+ + MUL15(a[12], a[14])) << 1);
+ t[27] = ((MUL15(a[ 8], a[19])
+ + MUL15(a[ 9], a[18])
+ + MUL15(a[10], a[17])
+ + MUL15(a[11], a[16])
+ + MUL15(a[12], a[15])
+ + MUL15(a[13], a[14])) << 1);
+ t[28] = MUL15(a[14], a[14])
+ + ((MUL15(a[ 9], a[19])
+ + MUL15(a[10], a[18])
+ + MUL15(a[11], a[17])
+ + MUL15(a[12], a[16])
+ + MUL15(a[13], a[15])) << 1);
+ t[29] = ((MUL15(a[10], a[19])
+ + MUL15(a[11], a[18])
+ + MUL15(a[12], a[17])
+ + MUL15(a[13], a[16])
+ + MUL15(a[14], a[15])) << 1);
+ t[30] = MUL15(a[15], a[15])
+ + ((MUL15(a[11], a[19])
+ + MUL15(a[12], a[18])
+ + MUL15(a[13], a[17])
+ + MUL15(a[14], a[16])) << 1);
+ t[31] = ((MUL15(a[12], a[19])
+ + MUL15(a[13], a[18])
+ + MUL15(a[14], a[17])
+ + MUL15(a[15], a[16])) << 1);
+ t[32] = MUL15(a[16], a[16])
+ + ((MUL15(a[13], a[19])
+ + MUL15(a[14], a[18])
+ + MUL15(a[15], a[17])) << 1);
+ t[33] = ((MUL15(a[14], a[19])
+ + MUL15(a[15], a[18])
+ + MUL15(a[16], a[17])) << 1);
+ t[34] = MUL15(a[17], a[17])
+ + ((MUL15(a[15], a[19])
+ + MUL15(a[16], a[18])) << 1);
+ t[35] = ((MUL15(a[16], a[19])
+ + MUL15(a[17], a[18])) << 1);
+ t[36] = MUL15(a[18], a[18])
+ + ((MUL15(a[17], a[19])) << 1);
+ t[37] = ((MUL15(a[18], a[19])) << 1);
+ t[38] = MUL15(a[19], a[19]);
+
+ d[39] = norm13(d, t, 39);
+}
+
+#endif
+
+/*
+ * Perform a "final reduction" in field F255 (field for Curve25519)
+ * The source value must be less than twice the modulus. If the value
+ * is not lower than the modulus, then the modulus is subtracted and
+ * this function returns 1; otherwise, it leaves it untouched and it
+ * returns 0.
+ */
+static uint32_t
+reduce_final_f255(uint32_t *d)
+{
+ uint32_t t[20];
+ uint32_t cc;
+ int i;
+
+ memcpy(t, d, sizeof t);
+ cc = 19;
+ for (i = 0; i < 20; i ++) {
+ uint32_t w;
+
+ w = t[i] + cc;
+ cc = w >> 13;
+ t[i] = w & 0x1FFF;
+ }
+ cc = t[19] >> 8;
+ t[19] &= 0xFF;
+ CCOPY(cc, d, t, sizeof t);
+ return cc;
+}
+
+static void
+f255_mulgen(uint32_t *d, const uint32_t *a, const uint32_t *b, int square)
+{
+ uint32_t t[40], cc, w;
+
+ /*
+ * Compute raw multiplication. All result words fit in 13 bits
+ * each; upper word (t[39]) must fit on 5 bits, since the product
+ * of two 256-bit integers must fit on 512 bits.
+ */
+ if (square) {
+ square20(t, a);
+ } else {
+ mul20(t, a, b);
+ }
+
+ /*
+ * Modular reduction: each high word is added where necessary.
+ * Since the modulus is 2^255-19 and word 20 corresponds to
+ * offset 20*13 = 260, word 20+k must be added to word k with
+ * a factor of 19*2^5 = 608. The extra bits in word 19 are also
+ * added that way.
+ */
+ cc = MUL15(t[19] >> 8, 19);
+ t[19] &= 0xFF;
+
+#define MM1(x) do { \
+ w = t[x] + cc + MUL15(t[(x) + 20], 608); \
+ t[x] = w & 0x1FFF; \
+ cc = w >> 13; \
+ } while (0)
+
+ MM1( 0);
+ MM1( 1);
+ MM1( 2);
+ MM1( 3);
+ MM1( 4);
+ MM1( 5);
+ MM1( 6);
+ MM1( 7);
+ MM1( 8);
+ MM1( 9);
+ MM1(10);
+ MM1(11);
+ MM1(12);
+ MM1(13);
+ MM1(14);
+ MM1(15);
+ MM1(16);
+ MM1(17);
+ MM1(18);
+ MM1(19);
+
+#undef MM1
+
+ cc = MUL15(w >> 8, 19);
+ t[19] &= 0xFF;
+
+#define MM2(x) do { \
+ w = t[x] + cc; \
+ d[x] = w & 0x1FFF; \
+ cc = w >> 13; \
+ } while (0)
+
+ MM2( 0);
+ MM2( 1);
+ MM2( 2);
+ MM2( 3);
+ MM2( 4);
+ MM2( 5);
+ MM2( 6);
+ MM2( 7);
+ MM2( 8);
+ MM2( 9);
+ MM2(10);
+ MM2(11);
+ MM2(12);
+ MM2(13);
+ MM2(14);
+ MM2(15);
+ MM2(16);
+ MM2(17);
+ MM2(18);
+ MM2(19);
+
+#undef MM2
+}
+
+/*
+ * Perform a multiplication of two integers modulo 2^255-19.
+ * Operands are arrays of 20 words, each containing 13 bits of data, in
+ * little-endian order. Input value may be up to 2^256-1; on output, value
+ * fits on 256 bits and is lower than twice the modulus.
+ *
+ * f255_mul() is the general multiplication, f255_square() is specialised
+ * for squarings.
+ */
+#define f255_mul(d, a, b) f255_mulgen(d, a, b, 0)
+#define f255_square(d, a) f255_mulgen(d, a, a, 1)
+
+/*
+ * Add two values in F255. Partial reduction is performed (down to less
+ * than twice the modulus).
+ */
+static void
+f255_add(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ int i;
+ uint32_t cc, w;
+
+ cc = 0;
+ for (i = 0; i < 20; i ++) {
+ w = a[i] + b[i] + cc;
+ d[i] = w & 0x1FFF;
+ cc = w >> 13;
+ }
+ cc = MUL15(w >> 8, 19);
+ d[19] &= 0xFF;
+ for (i = 0; i < 20; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x1FFF;
+ cc = w >> 13;
+ }
+}
+
+/*
+ * Subtract one value from another in F255. Partial reduction is
+ * performed (down to less than twice the modulus).
+ */
+static void
+f255_sub(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * We actually compute a - b + 2*p, so that the final value is
+ * necessarily positive.
+ */
+ int i;
+ uint32_t cc, w;
+
+ cc = (uint32_t)-38;
+ for (i = 0; i < 20; i ++) {
+ w = a[i] - b[i] + cc;
+ d[i] = w & 0x1FFF;
+ cc = ARSH(w, 13);
+ }
+ cc = MUL15((w + 0x200) >> 8, 19);
+ d[19] &= 0xFF;
+ for (i = 0; i < 20; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x1FFF;
+ cc = w >> 13;
+ }
+}
+
+/*
+ * Multiply an integer by the 'A24' constant (121665). Partial reduction
+ * is performed (down to less than twice the modulus).
+ */
+static void
+f255_mul_a24(uint32_t *d, const uint32_t *a)
+{
+ int i;
+ uint32_t cc, w;
+
+ cc = 0;
+ for (i = 0; i < 20; i ++) {
+ w = MUL15(a[i], 121665) + cc;
+ d[i] = w & 0x1FFF;
+ cc = w >> 13;
+ }
+ cc = MUL15(w >> 8, 19);
+ d[19] &= 0xFF;
+ for (i = 0; i < 20; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x1FFF;
+ cc = w >> 13;
+ }
+}
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+static void
+cswap(uint32_t *a, uint32_t *b, uint32_t ctl)
+{
+ int i;
+
+ ctl = -ctl;
+ for (i = 0; i < 20; i ++) {
+ uint32_t aw, bw, tw;
+
+ aw = a[i];
+ bw = b[i];
+ tw = ctl & (aw ^ bw);
+ a[i] = aw ^ tw;
+ b[i] = bw ^ tw;
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ uint32_t x1[20], x2[20], x3[20], z2[20], z3[20];
+ uint32_t a[20], aa[20], b[20], bb[20];
+ uint32_t c[20], d[20], e[20], da[20], cb[20];
+ unsigned char k[32];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+ G[31] &= 0x7F;
+
+ /*
+ * Initialise variables x1, x2, z2, x3 and z3. We set all of them
+ * into Montgomery representation.
+ */
+ x1[19] = le8_to_le13(x1, G, 32);
+ memcpy(x3, x1, sizeof x1);
+ memset(z2, 0, sizeof z2);
+ memset(x2, 0, sizeof x2);
+ x2[0] = 1;
+ memset(z3, 0, sizeof z3);
+ z3[0] = 1;
+
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ /* obsolete
+ print_int("x1", x1);
+ */
+
+ swap = 0;
+ for (i = 254; i >= 0; i --) {
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+ swap = kt;
+
+ /* obsolete
+ print_int("x2", x2);
+ print_int("z2", z2);
+ print_int("x3", x3);
+ print_int("z3", z3);
+ */
+
+ f255_add(a, x2, z2);
+ f255_square(aa, a);
+ f255_sub(b, x2, z2);
+ f255_square(bb, b);
+ f255_sub(e, aa, bb);
+ f255_add(c, x3, z3);
+ f255_sub(d, x3, z3);
+ f255_mul(da, d, a);
+ f255_mul(cb, c, b);
+
+ /* obsolete
+ print_int("a ", a);
+ print_int("aa", aa);
+ print_int("b ", b);
+ print_int("bb", bb);
+ print_int("e ", e);
+ print_int("c ", c);
+ print_int("d ", d);
+ print_int("da", da);
+ print_int("cb", cb);
+ */
+
+ f255_add(x3, da, cb);
+ f255_square(x3, x3);
+ f255_sub(z3, da, cb);
+ f255_square(z3, z3);
+ f255_mul(z3, z3, x1);
+ f255_mul(x2, aa, bb);
+ f255_mul_a24(z2, e);
+ f255_add(z2, z2, aa);
+ f255_mul(z2, e, z2);
+
+ /* obsolete
+ print_int("x2", x2);
+ print_int("z2", z2);
+ print_int("x3", x3);
+ print_int("z3", z3);
+ */
+ }
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+
+ /*
+ * Inverse z2 with a modular exponentiation. This is a simple
+ * square-and-multiply algorithm; we mutualise most non-squarings
+ * since the exponent contains almost only ones.
+ */
+ memcpy(a, z2, sizeof z2);
+ for (i = 0; i < 15; i ++) {
+ f255_square(a, a);
+ f255_mul(a, a, z2);
+ }
+ memcpy(b, a, sizeof a);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ f255_square(b, b);
+ }
+ f255_mul(b, b, a);
+ }
+ for (i = 14; i >= 0; i --) {
+ f255_square(b, b);
+ if ((0xFFEB >> i) & 1) {
+ f255_mul(b, z2, b);
+ }
+ }
+ f255_mul(x2, x2, b);
+ reduce_final_f255(x2);
+ le13_to_le8(G, 32, x2);
+ return 1;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_m15 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_m31.c b/test/monniaux/BearSSL/src/ec/ec_c25519_m31.c
new file mode 100644
index 00000000..1dd6d514
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_m31.c
@@ -0,0 +1,800 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* obsolete
+#include <stdio.h>
+#include <stdlib.h>
+static void
+print_int(const char *name, const uint32_t *x)
+{
+ size_t u;
+ unsigned char tmp[40];
+
+ printf("%s = ", name);
+ for (u = 0; u < 9; u ++) {
+ if (x[u] > 0x3FFFFFFF) {
+ printf("INVALID:");
+ for (u = 0; u < 9; u ++) {
+ printf(" %08X", x[u]);
+ }
+ printf("\n");
+ return;
+ }
+ }
+ memset(tmp, 0, sizeof tmp);
+ for (u = 0; u < 9; u ++) {
+ uint64_t w;
+ int j, k;
+
+ w = x[u];
+ j = 30 * (int)u;
+ k = j & 7;
+ if (k != 0) {
+ w <<= k;
+ j -= k;
+ }
+ k = j >> 3;
+ for (j = 0; j < 8; j ++) {
+ tmp[39 - k - j] |= (unsigned char)w;
+ w >>= 8;
+ }
+ }
+ for (u = 8; u < 40; u ++) {
+ printf("%02X", tmp[u]);
+ }
+ printf("\n");
+}
+*/
+
+/*
+ * If BR_NO_ARITH_SHIFT is undefined, or defined to 0, then we _assume_
+ * that right-shifting a signed negative integer copies the sign bit
+ * (arithmetic right-shift). This is "implementation-defined behaviour",
+ * i.e. it is not undefined, but it may differ between compilers. Each
+ * compiler is supposed to document its behaviour in that respect. GCC
+ * explicitly defines that an arithmetic right shift is used. We expect
+ * all other compilers to do the same, because underlying CPU offer an
+ * arithmetic right shift opcode that could not be used otherwise.
+ */
+#if BR_NO_ARITH_SHIFT
+#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
+ | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
+#else
+#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
+#endif
+
+/*
+ * Convert an integer from unsigned little-endian encoding to a sequence of
+ * 30-bit words in little-endian order. The final "partial" word is
+ * returned.
+ */
+static uint32_t
+le8_to_le30(uint32_t *dst, const unsigned char *src, size_t len)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ uint32_t b;
+
+ b = *src ++;
+ if (acc_len < 22) {
+ acc |= b << acc_len;
+ acc_len += 8;
+ } else {
+ *dst ++ = (acc | (b << acc_len)) & 0x3FFFFFFF;
+ acc = b >> (30 - acc_len);
+ acc_len -= 22;
+ }
+ }
+ return acc;
+}
+
+/*
+ * Convert an integer (30-bit words, little-endian) to unsigned
+ * little-endian encoding. The total encoding length is provided; all
+ * the destination bytes will be filled.
+ */
+static void
+le30_to_le8(unsigned char *dst, size_t len, const uint32_t *src)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ if (acc_len < 8) {
+ uint32_t w;
+
+ w = *src ++;
+ *dst ++ = (unsigned char)(acc | (w << acc_len));
+ acc = w >> (8 - acc_len);
+ acc_len += 22;
+ } else {
+ *dst ++ = (unsigned char)acc;
+ acc >>= 8;
+ acc_len -= 8;
+ }
+ }
+}
+
+/*
+ * Multiply two integers. Source integers are represented as arrays of
+ * nine 30-bit words, for values up to 2^270-1. Result is encoded over
+ * 18 words of 30 bits each.
+ */
+static void
+mul9(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * Maximum intermediate result is no more than
+ * 10376293531797946367, which fits in 64 bits. Reason:
+ *
+ * 10376293531797946367 = 9 * (2^30-1)^2 + 9663676406
+ * 10376293531797946367 < 9663676407 * 2^30
+ *
+ * Thus, adding together 9 products of 30-bit integers, with
+ * a carry of at most 9663676406, yields an integer that fits
+ * on 64 bits and generates a carry of at most 9663676406.
+ */
+ uint64_t t[17];
+ uint64_t cc;
+ int i;
+
+ t[ 0] = MUL31(a[0], b[0]);
+ t[ 1] = MUL31(a[0], b[1])
+ + MUL31(a[1], b[0]);
+ t[ 2] = MUL31(a[0], b[2])
+ + MUL31(a[1], b[1])
+ + MUL31(a[2], b[0]);
+ t[ 3] = MUL31(a[0], b[3])
+ + MUL31(a[1], b[2])
+ + MUL31(a[2], b[1])
+ + MUL31(a[3], b[0]);
+ t[ 4] = MUL31(a[0], b[4])
+ + MUL31(a[1], b[3])
+ + MUL31(a[2], b[2])
+ + MUL31(a[3], b[1])
+ + MUL31(a[4], b[0]);
+ t[ 5] = MUL31(a[0], b[5])
+ + MUL31(a[1], b[4])
+ + MUL31(a[2], b[3])
+ + MUL31(a[3], b[2])
+ + MUL31(a[4], b[1])
+ + MUL31(a[5], b[0]);
+ t[ 6] = MUL31(a[0], b[6])
+ + MUL31(a[1], b[5])
+ + MUL31(a[2], b[4])
+ + MUL31(a[3], b[3])
+ + MUL31(a[4], b[2])
+ + MUL31(a[5], b[1])
+ + MUL31(a[6], b[0]);
+ t[ 7] = MUL31(a[0], b[7])
+ + MUL31(a[1], b[6])
+ + MUL31(a[2], b[5])
+ + MUL31(a[3], b[4])
+ + MUL31(a[4], b[3])
+ + MUL31(a[5], b[2])
+ + MUL31(a[6], b[1])
+ + MUL31(a[7], b[0]);
+ t[ 8] = MUL31(a[0], b[8])
+ + MUL31(a[1], b[7])
+ + MUL31(a[2], b[6])
+ + MUL31(a[3], b[5])
+ + MUL31(a[4], b[4])
+ + MUL31(a[5], b[3])
+ + MUL31(a[6], b[2])
+ + MUL31(a[7], b[1])
+ + MUL31(a[8], b[0]);
+ t[ 9] = MUL31(a[1], b[8])
+ + MUL31(a[2], b[7])
+ + MUL31(a[3], b[6])
+ + MUL31(a[4], b[5])
+ + MUL31(a[5], b[4])
+ + MUL31(a[6], b[3])
+ + MUL31(a[7], b[2])
+ + MUL31(a[8], b[1]);
+ t[10] = MUL31(a[2], b[8])
+ + MUL31(a[3], b[7])
+ + MUL31(a[4], b[6])
+ + MUL31(a[5], b[5])
+ + MUL31(a[6], b[4])
+ + MUL31(a[7], b[3])
+ + MUL31(a[8], b[2]);
+ t[11] = MUL31(a[3], b[8])
+ + MUL31(a[4], b[7])
+ + MUL31(a[5], b[6])
+ + MUL31(a[6], b[5])
+ + MUL31(a[7], b[4])
+ + MUL31(a[8], b[3]);
+ t[12] = MUL31(a[4], b[8])
+ + MUL31(a[5], b[7])
+ + MUL31(a[6], b[6])
+ + MUL31(a[7], b[5])
+ + MUL31(a[8], b[4]);
+ t[13] = MUL31(a[5], b[8])
+ + MUL31(a[6], b[7])
+ + MUL31(a[7], b[6])
+ + MUL31(a[8], b[5]);
+ t[14] = MUL31(a[6], b[8])
+ + MUL31(a[7], b[7])
+ + MUL31(a[8], b[6]);
+ t[15] = MUL31(a[7], b[8])
+ + MUL31(a[8], b[7]);
+ t[16] = MUL31(a[8], b[8]);
+
+ /*
+ * Propagate carries.
+ */
+ cc = 0;
+ for (i = 0; i < 17; i ++) {
+ uint64_t w;
+
+ w = t[i] + cc;
+ d[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ d[17] = (uint32_t)cc;
+}
+
+/*
+ * Square a 270-bit integer, represented as an array of nine 30-bit words.
+ * Result uses 18 words of 30 bits each.
+ */
+static void
+square9(uint32_t *d, const uint32_t *a)
+{
+ uint64_t t[17];
+ uint64_t cc;
+ int i;
+
+ t[ 0] = MUL31(a[0], a[0]);
+ t[ 1] = ((MUL31(a[0], a[1])) << 1);
+ t[ 2] = MUL31(a[1], a[1])
+ + ((MUL31(a[0], a[2])) << 1);
+ t[ 3] = ((MUL31(a[0], a[3])
+ + MUL31(a[1], a[2])) << 1);
+ t[ 4] = MUL31(a[2], a[2])
+ + ((MUL31(a[0], a[4])
+ + MUL31(a[1], a[3])) << 1);
+ t[ 5] = ((MUL31(a[0], a[5])
+ + MUL31(a[1], a[4])
+ + MUL31(a[2], a[3])) << 1);
+ t[ 6] = MUL31(a[3], a[3])
+ + ((MUL31(a[0], a[6])
+ + MUL31(a[1], a[5])
+ + MUL31(a[2], a[4])) << 1);
+ t[ 7] = ((MUL31(a[0], a[7])
+ + MUL31(a[1], a[6])
+ + MUL31(a[2], a[5])
+ + MUL31(a[3], a[4])) << 1);
+ t[ 8] = MUL31(a[4], a[4])
+ + ((MUL31(a[0], a[8])
+ + MUL31(a[1], a[7])
+ + MUL31(a[2], a[6])
+ + MUL31(a[3], a[5])) << 1);
+ t[ 9] = ((MUL31(a[1], a[8])
+ + MUL31(a[2], a[7])
+ + MUL31(a[3], a[6])
+ + MUL31(a[4], a[5])) << 1);
+ t[10] = MUL31(a[5], a[5])
+ + ((MUL31(a[2], a[8])
+ + MUL31(a[3], a[7])
+ + MUL31(a[4], a[6])) << 1);
+ t[11] = ((MUL31(a[3], a[8])
+ + MUL31(a[4], a[7])
+ + MUL31(a[5], a[6])) << 1);
+ t[12] = MUL31(a[6], a[6])
+ + ((MUL31(a[4], a[8])
+ + MUL31(a[5], a[7])) << 1);
+ t[13] = ((MUL31(a[5], a[8])
+ + MUL31(a[6], a[7])) << 1);
+ t[14] = MUL31(a[7], a[7])
+ + ((MUL31(a[6], a[8])) << 1);
+ t[15] = ((MUL31(a[7], a[8])) << 1);
+ t[16] = MUL31(a[8], a[8]);
+
+ /*
+ * Propagate carries.
+ */
+ cc = 0;
+ for (i = 0; i < 17; i ++) {
+ uint64_t w;
+
+ w = t[i] + cc;
+ d[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ d[17] = (uint32_t)cc;
+}
+
+/*
+ * Perform a "final reduction" in field F255 (field for Curve25519)
+ * The source value must be less than twice the modulus. If the value
+ * is not lower than the modulus, then the modulus is subtracted and
+ * this function returns 1; otherwise, it leaves it untouched and it
+ * returns 0.
+ */
+static uint32_t
+reduce_final_f255(uint32_t *d)
+{
+ uint32_t t[9];
+ uint32_t cc;
+ int i;
+
+ memcpy(t, d, sizeof t);
+ cc = 19;
+ for (i = 0; i < 9; i ++) {
+ uint32_t w;
+
+ w = t[i] + cc;
+ cc = w >> 30;
+ t[i] = w & 0x3FFFFFFF;
+ }
+ cc = t[8] >> 15;
+ t[8] &= 0x7FFF;
+ CCOPY(cc, d, t, sizeof t);
+ return cc;
+}
+
+/*
+ * Perform a multiplication of two integers modulo 2^255-19.
+ * Operands are arrays of 9 words, each containing 30 bits of data, in
+ * little-endian order. Input value may be up to 2^256-1; on output, value
+ * fits on 256 bits and is lower than twice the modulus.
+ */
+static void
+f255_mul(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[18], cc;
+ int i;
+
+ /*
+ * Compute raw multiplication. All result words fit in 30 bits
+ * each; upper word (t[17]) must fit on 2 bits, since the product
+ * of two 256-bit integers must fit on 512 bits.
+ */
+ mul9(t, a, b);
+
+ /*
+ * Modular reduction: each high word is added where necessary.
+ * Since the modulus is 2^255-19 and word 9 corresponds to
+ * offset 9*30 = 270, word 9+k must be added to word k with
+ * a factor of 19*2^15 = 622592. The extra bits in word 8 are also
+ * added that way.
+ *
+ * Keeping the carry on 32 bits helps with 32-bit architectures,
+ * and does not noticeably impact performance on 64-bit systems.
+ */
+ cc = MUL15(t[8] >> 15, 19); /* at most 19*(2^15-1) = 622573 */
+ t[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ uint64_t w;
+
+ w = (uint64_t)t[i] + (uint64_t)cc + MUL31(t[i + 9], 622592);
+ t[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = (uint32_t)(w >> 30); /* at most 622592 */
+ }
+
+ /*
+ * Original product was up to (2^256-1)^2, i.e. a 512-bit integer.
+ * This was split into two parts (upper of 257 bits, lower of 255
+ * bits), and the upper was added to the lower with a factor 19,
+ * which means that the intermediate value is less than 77*2^255
+ * (19*2^257 + 2^255). Therefore, the extra bits "t[8] >> 15" are
+ * less than 77, and the initial carry cc is at most 76*19 = 1444.
+ */
+ cc = MUL15(t[8] >> 15, 19);
+ t[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ uint32_t z;
+
+ z = t[i] + cc;
+ d[i] = z & 0x3FFFFFFF;
+ cc = z >> 30;
+ }
+
+ /*
+ * Final result is at most 2^255 + 1443. In particular, the last
+ * carry is necessarily 0, since t[8] was truncated to 15 bits.
+ */
+}
+
+/*
+ * Perform a squaring of an integer modulo 2^255-19.
+ * Operands are arrays of 9 words, each containing 30 bits of data, in
+ * little-endian order. Input value may be up to 2^256-1; on output, value
+ * fits on 256 bits and is lower than twice the modulus.
+ */
+static void
+f255_square(uint32_t *d, const uint32_t *a)
+{
+ uint32_t t[18], cc;
+ int i;
+
+ /*
+ * Compute raw squaring. All result words fit in 30 bits
+ * each; upper word (t[17]) must fit on 2 bits, since the square
+ * of a 256-bit integers must fit on 512 bits.
+ */
+ square9(t, a);
+
+ /*
+ * Modular reduction: each high word is added where necessary.
+ * See f255_mul() for details on the reduction and carry limits.
+ */
+ cc = MUL15(t[8] >> 15, 19);
+ t[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ uint64_t w;
+
+ w = (uint64_t)t[i] + (uint64_t)cc + MUL31(t[i + 9], 622592);
+ t[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = (uint32_t)(w >> 30);
+ }
+ cc = MUL15(t[8] >> 15, 19);
+ t[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ uint32_t z;
+
+ z = t[i] + cc;
+ d[i] = z & 0x3FFFFFFF;
+ cc = z >> 30;
+ }
+}
+
+/*
+ * Add two values in F255. Partial reduction is performed (down to less
+ * than twice the modulus).
+ */
+static void
+f255_add(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * Since operand words fit on 30 bits, we can use 32-bit
+ * variables throughout.
+ */
+ int i;
+ uint32_t cc, w;
+
+ cc = 0;
+ for (i = 0; i < 9; i ++) {
+ w = a[i] + b[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ cc = MUL15(w >> 15, 19);
+ d[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+}
+
+/*
+ * Subtract one value from another in F255. Partial reduction is
+ * performed (down to less than twice the modulus).
+ */
+static void
+f255_sub(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * We actually compute a - b + 2*p, so that the final value is
+ * necessarily positive.
+ */
+ int i;
+ uint32_t cc, w;
+
+ cc = (uint32_t)-38;
+ for (i = 0; i < 9; i ++) {
+ w = a[i] - b[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = ARSH(w, 30);
+ }
+ cc = MUL15((w + 0x10000) >> 15, 19);
+ d[8] &= 0x7FFF;
+ for (i = 0; i < 9; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+}
+
+/*
+ * Multiply an integer by the 'A24' constant (121665). Partial reduction
+ * is performed (down to less than twice the modulus).
+ */
+static void
+f255_mul_a24(uint32_t *d, const uint32_t *a)
+{
+ int i;
+ uint64_t w;
+ uint32_t cc;
+
+ /*
+ * a[] is over 256 bits, thus a[8] has length at most 16 bits.
+ * We single out the processing of the last word: intermediate
+ * value w is up to 121665*2^16, yielding a carry for the next
+ * loop of at most 19*(121665*2^16/2^15) = 4623289.
+ */
+ cc = 0;
+ for (i = 0; i < 8; i ++) {
+ w = MUL31(a[i], 121665) + (uint64_t)cc;
+ d[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = (uint32_t)(w >> 30);
+ }
+ w = MUL31(a[8], 121665) + (uint64_t)cc;
+ d[8] = (uint32_t)w & 0x7FFF;
+ cc = MUL15((uint32_t)(w >> 15), 19);
+
+ for (i = 0; i < 9; i ++) {
+ uint32_t z;
+
+ z = d[i] + cc;
+ d[i] = z & 0x3FFFFFFF;
+ cc = z >> 30;
+ }
+}
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+static void
+cswap(uint32_t *a, uint32_t *b, uint32_t ctl)
+{
+ int i;
+
+ ctl = -ctl;
+ for (i = 0; i < 9; i ++) {
+ uint32_t aw, bw, tw;
+
+ aw = a[i];
+ bw = b[i];
+ tw = ctl & (aw ^ bw);
+ a[i] = aw ^ tw;
+ b[i] = bw ^ tw;
+ }
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ uint32_t x1[9], x2[9], x3[9], z2[9], z3[9];
+ uint32_t a[9], aa[9], b[9], bb[9];
+ uint32_t c[9], d[9], e[9], da[9], cb[9];
+ unsigned char k[32];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+ G[31] &= 0x7F;
+
+ /*
+ * Initialise variables x1, x2, z2, x3 and z3. We set all of them
+ * into Montgomery representation.
+ */
+ x1[8] = le8_to_le30(x1, G, 32);
+ memcpy(x3, x1, sizeof x1);
+ memset(z2, 0, sizeof z2);
+ memset(x2, 0, sizeof x2);
+ x2[0] = 1;
+ memset(z3, 0, sizeof z3);
+ z3[0] = 1;
+
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ /* obsolete
+ print_int("x1", x1);
+ */
+
+ swap = 0;
+ for (i = 254; i >= 0; i --) {
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+ swap = kt;
+
+ /* obsolete
+ print_int("x2", x2);
+ print_int("z2", z2);
+ print_int("x3", x3);
+ print_int("z3", z3);
+ */
+
+ f255_add(a, x2, z2);
+ f255_square(aa, a);
+ f255_sub(b, x2, z2);
+ f255_square(bb, b);
+ f255_sub(e, aa, bb);
+ f255_add(c, x3, z3);
+ f255_sub(d, x3, z3);
+ f255_mul(da, d, a);
+ f255_mul(cb, c, b);
+
+ /* obsolete
+ print_int("a ", a);
+ print_int("aa", aa);
+ print_int("b ", b);
+ print_int("bb", bb);
+ print_int("e ", e);
+ print_int("c ", c);
+ print_int("d ", d);
+ print_int("da", da);
+ print_int("cb", cb);
+ */
+
+ f255_add(x3, da, cb);
+ f255_square(x3, x3);
+ f255_sub(z3, da, cb);
+ f255_square(z3, z3);
+ f255_mul(z3, z3, x1);
+ f255_mul(x2, aa, bb);
+ f255_mul_a24(z2, e);
+ f255_add(z2, z2, aa);
+ f255_mul(z2, e, z2);
+
+ /* obsolete
+ print_int("x2", x2);
+ print_int("z2", z2);
+ print_int("x3", x3);
+ print_int("z3", z3);
+ */
+ }
+ cswap(x2, x3, swap);
+ cswap(z2, z3, swap);
+
+ /*
+ * Inverse z2 with a modular exponentiation. This is a simple
+ * square-and-multiply algorithm; we mutualise most non-squarings
+ * since the exponent contains almost only ones.
+ */
+ memcpy(a, z2, sizeof z2);
+ for (i = 0; i < 15; i ++) {
+ f255_square(a, a);
+ f255_mul(a, a, z2);
+ }
+ memcpy(b, a, sizeof a);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ f255_square(b, b);
+ }
+ f255_mul(b, b, a);
+ }
+ for (i = 14; i >= 0; i --) {
+ f255_square(b, b);
+ if ((0xFFEB >> i) & 1) {
+ f255_mul(b, z2, b);
+ }
+ }
+ f255_mul(x2, x2, b);
+ reduce_final_f255(x2);
+ le30_to_le8(G, 32, x2);
+ return 1;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_m31 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_m62.c b/test/monniaux/BearSSL/src/ec/ec_c25519_m62.c
new file mode 100644
index 00000000..6b058eb1
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_m62.c
@@ -0,0 +1,605 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_UMUL128
+#include <intrin.h>
+#endif
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+/*
+ * A field element is encoded as five 64-bit integers, in basis 2^51.
+ * Limbs may be occasionally larger than 2^51, to save on carry
+ * propagation costs.
+ */
+
+#define MASK51 (((uint64_t)1 << 51) - (uint64_t)1)
+
+/*
+ * Swap two field elements, conditionally on a flag.
+ */
+static inline void
+f255_cswap(uint64_t *a, uint64_t *b, uint32_t ctl)
+{
+ uint64_t m, w;
+
+ m = -(uint64_t)ctl;
+ w = m & (a[0] ^ b[0]); a[0] ^= w; b[0] ^= w;
+ w = m & (a[1] ^ b[1]); a[1] ^= w; b[1] ^= w;
+ w = m & (a[2] ^ b[2]); a[2] ^= w; b[2] ^= w;
+ w = m & (a[3] ^ b[3]); a[3] ^= w; b[3] ^= w;
+ w = m & (a[4] ^ b[4]); a[4] ^= w; b[4] ^= w;
+}
+
+/*
+ * Addition with no carry propagation. Limbs double in size.
+ */
+static inline void
+f255_add(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+ d[0] = a[0] + b[0];
+ d[1] = a[1] + b[1];
+ d[2] = a[2] + b[2];
+ d[3] = a[3] + b[3];
+ d[4] = a[4] + b[4];
+}
+
+/*
+ * Subtraction.
+ * On input, limbs must fit on 60 bits each. On output, result is
+ * partially reduced, with max value 2^255+19456; moreover, all
+ * limbs will fit on 51 bits, except the low limb, which may have
+ * value up to 2^51+19455.
+ */
+static inline void
+f255_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+ uint64_t cc, w;
+
+ /*
+ * We compute d = (2^255-19)*1024 + a - b. Since the limbs
+ * fit on 60 bits, the maximum value of operands are slightly
+ * more than 2^264, but much less than 2^265-19456. This
+ * ensures that the result is positive.
+ */
+
+ /*
+ * Initial carry is 19456, since we add 2^265-19456. Each
+ * individual subtraction may yield a carry up to 513.
+ */
+ w = a[0] - b[0] - 19456;
+ d[0] = w & MASK51;
+ cc = -(w >> 51) & 0x3FF;
+ w = a[1] - b[1] - cc;
+ d[1] = w & MASK51;
+ cc = -(w >> 51) & 0x3FF;
+ w = a[2] - b[2] - cc;
+ d[2] = w & MASK51;
+ cc = -(w >> 51) & 0x3FF;
+ w = a[3] - b[3] - cc;
+ d[3] = w & MASK51;
+ cc = -(w >> 51) & 0x3FF;
+ d[4] = ((uint64_t)1 << 61) + a[4] - b[4] - cc;
+
+ /*
+ * Partial reduction. The intermediate result may be up to
+ * slightly above 2^265, but less than 2^265+2^255. When we
+ * truncate to 255 bits, the upper bits will be at most 1024.
+ */
+ d[0] += 19 * (d[4] >> 51);
+ d[4] &= MASK51;
+}
+
+/*
+ * UMUL51(hi, lo, x, y) computes:
+ *
+ * hi = floor((x * y) / (2^51))
+ * lo = x * y mod 2^51
+ *
+ * Note that lo < 2^51, but "hi" may be larger, if the input operands are
+ * larger.
+ */
+#if BR_INT128
+
+#define UMUL51(hi, lo, x, y) do { \
+ unsigned __int128 umul_tmp; \
+ umul_tmp = (unsigned __int128)(x) * (unsigned __int128)(y); \
+ (hi) = (uint64_t)(umul_tmp >> 51); \
+ (lo) = (uint64_t)umul_tmp & MASK51; \
+ } while (0)
+
+#elif BR_UMUL128
+
+#define UMUL51(hi, lo, x, y) do { \
+ uint64_t umul_hi, umul_lo; \
+ umul_lo = _umul128((x), (y), &umul_hi); \
+ (hi) = (umul_hi << 13) | (umul_lo >> 51); \
+ (lo) = umul_lo & MASK51; \
+ } while (0)
+
+#endif
+
+/*
+ * Multiplication.
+ * On input, limbs must fit on 54 bits each.
+ * On output, limb 0 is at most 2^51 + 155647, and other limbs fit
+ * on 51 bits each.
+ */
+static inline void
+f255_mul(uint64_t *d, uint64_t *a, uint64_t *b)
+{
+ uint64_t t[10], hi, lo, w, cc;
+
+ /*
+ * Perform cross products, accumulating values without carry
+ * propagation.
+ *
+ * Since input limbs fit on 54 bits each, each individual
+ * UMUL51 will produce a "hi" of less than 2^57. The maximum
+ * sum will be at most 5*(2^57-1) + 4*(2^51-1) (for t[5]),
+ * i.e. less than 324*2^51.
+ */
+
+ UMUL51(t[1], t[0], a[0], b[0]);
+
+ UMUL51(t[2], lo, a[1], b[0]); t[1] += lo;
+ UMUL51(hi, lo, a[0], b[1]); t[1] += lo; t[2] += hi;
+
+ UMUL51(t[3], lo, a[2], b[0]); t[2] += lo;
+ UMUL51(hi, lo, a[1], b[1]); t[2] += lo; t[3] += hi;
+ UMUL51(hi, lo, a[0], b[2]); t[2] += lo; t[3] += hi;
+
+ UMUL51(t[4], lo, a[3], b[0]); t[3] += lo;
+ UMUL51(hi, lo, a[2], b[1]); t[3] += lo; t[4] += hi;
+ UMUL51(hi, lo, a[1], b[2]); t[3] += lo; t[4] += hi;
+ UMUL51(hi, lo, a[0], b[3]); t[3] += lo; t[4] += hi;
+
+ UMUL51(t[5], lo, a[4], b[0]); t[4] += lo;
+ UMUL51(hi, lo, a[3], b[1]); t[4] += lo; t[5] += hi;
+ UMUL51(hi, lo, a[2], b[2]); t[4] += lo; t[5] += hi;
+ UMUL51(hi, lo, a[1], b[3]); t[4] += lo; t[5] += hi;
+ UMUL51(hi, lo, a[0], b[4]); t[4] += lo; t[5] += hi;
+
+ UMUL51(t[6], lo, a[4], b[1]); t[5] += lo;
+ UMUL51(hi, lo, a[3], b[2]); t[5] += lo; t[6] += hi;
+ UMUL51(hi, lo, a[2], b[3]); t[5] += lo; t[6] += hi;
+ UMUL51(hi, lo, a[1], b[4]); t[5] += lo; t[6] += hi;
+
+ UMUL51(t[7], lo, a[4], b[2]); t[6] += lo;
+ UMUL51(hi, lo, a[3], b[3]); t[6] += lo; t[7] += hi;
+ UMUL51(hi, lo, a[2], b[4]); t[6] += lo; t[7] += hi;
+
+ UMUL51(t[8], lo, a[4], b[3]); t[7] += lo;
+ UMUL51(hi, lo, a[3], b[4]); t[7] += lo; t[8] += hi;
+
+ UMUL51(t[9], lo, a[4], b[4]); t[8] += lo;
+
+ /*
+ * The upper words t[5]..t[9] are folded back into the lower
+ * words, using the rule that 2^255 = 19 in the field.
+ *
+ * Since each t[i] is less than 324*2^51, the additions below
+ * will yield less than 6480*2^51 in each limb; this fits in
+ * 64 bits (6480*2^51 < 8192*2^51 = 2^64), hence there is
+ * no overflow.
+ */
+ t[0] += 19 * t[5];
+ t[1] += 19 * t[6];
+ t[2] += 19 * t[7];
+ t[3] += 19 * t[8];
+ t[4] += 19 * t[9];
+
+ /*
+ * Propagate carries.
+ */
+ w = t[0];
+ d[0] = w & MASK51;
+ cc = w >> 51;
+ w = t[1] + cc;
+ d[1] = w & MASK51;
+ cc = w >> 51;
+ w = t[2] + cc;
+ d[2] = w & MASK51;
+ cc = w >> 51;
+ w = t[3] + cc;
+ d[3] = w & MASK51;
+ cc = w >> 51;
+ w = t[4] + cc;
+ d[4] = w & MASK51;
+ cc = w >> 51;
+
+ /*
+ * Since the limbs were 64-bit values, the top carry is at
+ * most 8192 (in practice, that cannot be reached). We simply
+ * performed a partial reduction.
+ */
+ d[0] += 19 * cc;
+}
+
+/*
+ * Multiplication by A24 = 121665.
+ * Input must have limbs of 60 bits at most.
+ */
+static inline void
+f255_mul_a24(uint64_t *d, const uint64_t *a)
+{
+ uint64_t t[5], cc, w;
+
+ /*
+ * 121665 = 15 * 8111. We first multiply by 15, with carry
+ * propagation and partial reduction.
+ */
+ w = a[0] * 15;
+ t[0] = w & MASK51;
+ cc = w >> 51;
+ w = a[1] * 15 + cc;
+ t[1] = w & MASK51;
+ cc = w >> 51;
+ w = a[2] * 15 + cc;
+ t[2] = w & MASK51;
+ cc = w >> 51;
+ w = a[3] * 15 + cc;
+ t[3] = w & MASK51;
+ cc = w >> 51;
+ w = a[4] * 15 + cc;
+ t[4] = w & MASK51;
+ t[0] += 19 * (w >> 51);
+
+ /*
+ * Then multiplication by 8111. At that point, we known that
+ * t[0] is less than 2^51 + 19*8192, and other limbs are less
+ * than 2^51; thus, there will be no overflow.
+ */
+ w = t[0] * 8111;
+ d[0] = w & MASK51;
+ cc = w >> 51;
+ w = t[1] * 8111 + cc;
+ d[1] = w & MASK51;
+ cc = w >> 51;
+ w = t[2] * 8111 + cc;
+ d[2] = w & MASK51;
+ cc = w >> 51;
+ w = t[3] * 8111 + cc;
+ d[3] = w & MASK51;
+ cc = w >> 51;
+ w = t[4] * 8111 + cc;
+ d[4] = w & MASK51;
+ d[0] += 19 * (w >> 51);
+}
+
+/*
+ * Finalize reduction.
+ * On input, limbs must fit on 51 bits, except possibly the low limb,
+ * which may be slightly above 2^51.
+ */
+static inline void
+f255_final_reduce(uint64_t *a)
+{
+ uint64_t t[5], cc, w;
+
+ /*
+ * We add 19. If the result (in t[]) is below 2^255, then a[]
+ * is already less than 2^255-19, thus already reduced.
+ * Otherwise, we subtract 2^255 from t[], in which case we
+ * have t = a - (2^255-19), and that's our result.
+ */
+ w = a[0] + 19;
+ t[0] = w & MASK51;
+ cc = w >> 51;
+ w = a[1] + cc;
+ t[1] = w & MASK51;
+ cc = w >> 51;
+ w = a[2] + cc;
+ t[2] = w & MASK51;
+ cc = w >> 51;
+ w = a[3] + cc;
+ t[3] = w & MASK51;
+ cc = w >> 51;
+ w = a[4] + cc;
+ t[4] = w & MASK51;
+ cc = w >> 51;
+
+ /*
+ * The bit 255 of t is in cc. If that bit is 0, when a[] must
+ * be unchanged; otherwise, it must be replaced with t[].
+ */
+ cc = -cc;
+ a[0] ^= cc & (a[0] ^ t[0]);
+ a[1] ^= cc & (a[1] ^ t[1]);
+ a[2] ^= cc & (a[2] ^ t[2]);
+ a[3] ^= cc & (a[3] ^ t[3]);
+ a[4] ^= cc & (a[4] ^ t[4]);
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ unsigned char k[32];
+ uint64_t x1[5], x2[5], z2[5], x3[5], z3[5];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+
+ /*
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared; the "& MASK51" in the initialization for
+ * x1[4] clears that bit.
+ */
+ x1[0] = br_dec64le(&G[0]) & MASK51;
+ x1[1] = (br_dec64le(&G[6]) >> 3) & MASK51;
+ x1[2] = (br_dec64le(&G[12]) >> 6) & MASK51;
+ x1[3] = (br_dec64le(&G[19]) >> 1) & MASK51;
+ x1[4] = (br_dec64le(&G[24]) >> 12) & MASK51;
+
+ /*
+ * We can use memset() to clear values, because exact-width types
+ * like uint64_t are guaranteed to have no padding bits or
+ * trap representations.
+ */
+ memset(x2, 0, sizeof x2);
+ x2[0] = 1;
+ memset(z2, 0, sizeof z2);
+ memcpy(x3, x1, sizeof x1);
+ memcpy(z3, x2, sizeof x2);
+
+ /*
+ * The multiplier is provided in big-endian notation, and
+ * possibly shorter than 32 bytes.
+ */
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ swap = 0;
+
+ for (i = 254; i >= 0; i --) {
+ uint64_t a[5], aa[5], b[5], bb[5], e[5];
+ uint64_t c[5], d[5], da[5], cb[5];
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ f255_cswap(x2, x3, swap);
+ f255_cswap(z2, z3, swap);
+ swap = kt;
+
+ /*
+ * At that point, limbs of x_2 and z_2 are assumed to fit
+ * on at most 52 bits each.
+ *
+ * Each f255_add() adds one bit to the maximum range of
+ * the values, but f255_sub() and f255_mul() bring back
+ * the limbs into 52 bits. All f255_add() outputs are
+ * used only as inputs for f255_mul(), which ensures
+ * that limbs remain in the proper range.
+ */
+
+ /* A = x_2 + z_2 -- limbs fit on 53 bits each */
+ f255_add(a, x2, z2);
+
+ /* AA = A^2 */
+ f255_mul(aa, a, a);
+
+ /* B = x_2 - z_2 */
+ f255_sub(b, x2, z2);
+
+ /* BB = B^2 */
+ f255_mul(bb, b, b);
+
+ /* E = AA - BB */
+ f255_sub(e, aa, bb);
+
+ /* C = x_3 + z_3 -- limbs fit on 53 bits each */
+ f255_add(c, x3, z3);
+
+ /* D = x_3 - z_3 */
+ f255_sub(d, x3, z3);
+
+ /* DA = D * A */
+ f255_mul(da, d, a);
+
+ /* CB = C * B */
+ f255_mul(cb, c, b);
+
+ /* x_3 = (DA + CB)^2 */
+ f255_add(x3, da, cb);
+ f255_mul(x3, x3, x3);
+
+ /* z_3 = x_1 * (DA - CB)^2 */
+ f255_sub(z3, da, cb);
+ f255_mul(z3, z3, z3);
+ f255_mul(z3, x1, z3);
+
+ /* x_2 = AA * BB */
+ f255_mul(x2, aa, bb);
+
+ /* z_2 = E * (AA + a24 * E) */
+ f255_mul_a24(z2, e);
+ f255_add(z2, aa, z2);
+ f255_mul(z2, e, z2);
+ }
+
+ f255_cswap(x2, x3, swap);
+ f255_cswap(z2, z3, swap);
+
+ /*
+ * Compute 1/z2 = z2^(p-2). Since p = 2^255-19, we can mutualize
+ * most non-squarings. We use x1 and x3, now useless, as temporaries.
+ */
+ memcpy(x1, z2, sizeof z2);
+ for (i = 0; i < 15; i ++) {
+ f255_mul(x1, x1, x1);
+ f255_mul(x1, x1, z2);
+ }
+ memcpy(x3, x1, sizeof x1);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ f255_mul(x3, x3, x3);
+ }
+ f255_mul(x3, x3, x1);
+ }
+ for (i = 14; i >= 0; i --) {
+ f255_mul(x3, x3, x3);
+ if ((0xFFEB >> i) & 1) {
+ f255_mul(x3, z2, x3);
+ }
+ }
+
+ /*
+ * Compute x2/z2. We have 1/z2 in x3.
+ */
+ f255_mul(x2, x2, x3);
+ f255_final_reduce(x2);
+
+ /*
+ * Encode the final x2 value in little-endian. We first assemble
+ * the limbs into 64-bit values.
+ */
+ x2[0] |= x2[1] << 51;
+ x2[1] = (x2[1] >> 13) | (x2[2] << 38);
+ x2[2] = (x2[2] >> 26) | (x2[3] << 25);
+ x2[3] = (x2[3] >> 39) | (x2[4] << 12);
+ br_enc64le(G, x2[0]);
+ br_enc64le(G + 8, x2[1]);
+ br_enc64le(G + 16, x2[2]);
+ br_enc64le(G + 24, x2[3]);
+ return 1;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_m62 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_c25519_m62_get(void)
+{
+ return &br_ec_c25519_m62;
+}
+
+#else
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_c25519_m62_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/ec/ec_c25519_m64.c b/test/monniaux/BearSSL/src/ec/ec_c25519_m64.c
new file mode 100644
index 00000000..7e7f12f7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_c25519_m64.c
@@ -0,0 +1,835 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_UMUL128
+#include <intrin.h>
+#endif
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return GEN;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return ORDER;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 0;
+}
+
+/*
+ * A field element is encoded as four 64-bit integers, in basis 2^63.
+ * Operations return partially reduced values, which may range up to
+ * 2^255+37.
+ */
+
+#define MASK63 (((uint64_t)1 << 63) - (uint64_t)1)
+
+/*
+ * Swap two field elements, conditionally on a flag.
+ */
+static inline void
+f255_cswap(uint64_t *a, uint64_t *b, uint32_t ctl)
+{
+ uint64_t m, w;
+
+ m = -(uint64_t)ctl;
+ w = m & (a[0] ^ b[0]); a[0] ^= w; b[0] ^= w;
+ w = m & (a[1] ^ b[1]); a[1] ^= w; b[1] ^= w;
+ w = m & (a[2] ^ b[2]); a[2] ^= w; b[2] ^= w;
+ w = m & (a[3] ^ b[3]); a[3] ^= w; b[3] ^= w;
+}
+
+/*
+ * Addition in the field.
+ */
+static inline void
+f255_add(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+
+ uint64_t t0, t1, t2, t3, cc;
+ unsigned __int128 z;
+
+ z = (unsigned __int128)a[0] + (unsigned __int128)b[0];
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)a[1] + (unsigned __int128)b[1] + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[2] + (unsigned __int128)b[2] + (z >> 64);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[3] + (unsigned __int128)b[3] + (z >> 64);
+ t3 = (uint64_t)z & MASK63;
+ cc = (uint64_t)(z >> 63);
+
+ /*
+ * Since operands are at most 2^255+37, the sum is at most
+ * 2^256+74; thus, the carry cc is equal to 0, 1 or 2.
+ *
+ * We use: 2^255 = 19 mod p.
+ * Since we add 0, 19 or 38 to a value that fits on 255 bits,
+ * the result is at most 2^255+37.
+ */
+ z = (unsigned __int128)t0 + (unsigned __int128)(19 * cc);
+ d[0] = (uint64_t)z;
+ z = (unsigned __int128)t1 + (z >> 64);
+ d[1] = (uint64_t)z;
+ z = (unsigned __int128)t2 + (z >> 64);
+ d[2] = (uint64_t)z;
+ d[3] = t3 + (uint64_t)(z >> 64);
+
+#elif BR_UMUL128
+
+ uint64_t t0, t1, t2, t3, cc;
+ unsigned char k;
+
+ k = _addcarry_u64(0, a[0], b[0], &t0);
+ k = _addcarry_u64(k, a[1], b[1], &t1);
+ k = _addcarry_u64(k, a[2], b[2], &t2);
+ k = _addcarry_u64(k, a[3], b[3], &t3);
+ cc = (k << 1) + (t3 >> 63);
+ t3 &= MASK63;
+
+ /*
+ * Since operands are at most 2^255+37, the sum is at most
+ * 2^256+74; thus, the carry cc is equal to 0, 1 or 2.
+ *
+ * We use: 2^255 = 19 mod p.
+ * Since we add 0, 19 or 38 to a value that fits on 255 bits,
+ * the result is at most 2^255+37.
+ */
+ k = _addcarry_u64(0, t0, 19 * cc, &d[0]);
+ k = _addcarry_u64(k, t1, 0, &d[1]);
+ k = _addcarry_u64(k, t2, 0, &d[2]);
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
+
+#endif
+}
+
+/*
+ * Subtraction.
+ * On input, limbs must fit on 60 bits each. On output, result is
+ * partially reduced, with max value 2^255+19456; moreover, all
+ * limbs will fit on 51 bits, except the low limb, which may have
+ * value up to 2^51+19455.
+ */
+static inline void
+f255_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+
+ /*
+ * We compute t = 2^256 - 38 + a - b, which is necessarily
+ * positive but lower than 2^256 + 2^255, since a <= 2^255 + 37
+ * and b <= 2^255 + 37. We then subtract 0, p or 2*p, depending
+ * on the two upper bits of t (bits 255 and 256).
+ */
+
+ uint64_t t0, t1, t2, t3, t4, cc;
+ unsigned __int128 z;
+
+ z = (unsigned __int128)a[0] - (unsigned __int128)b[0] - 38;
+ t0 = (uint64_t)z;
+ cc = -(uint64_t)(z >> 64);
+ z = (unsigned __int128)a[1] - (unsigned __int128)b[1]
+ - (unsigned __int128)cc;
+ t1 = (uint64_t)z;
+ cc = -(uint64_t)(z >> 64);
+ z = (unsigned __int128)a[2] - (unsigned __int128)b[2]
+ - (unsigned __int128)cc;
+ t2 = (uint64_t)z;
+ cc = -(uint64_t)(z >> 64);
+ z = (unsigned __int128)a[3] - (unsigned __int128)b[3]
+ - (unsigned __int128)cc;
+ t3 = (uint64_t)z;
+ t4 = 1 + (uint64_t)(z >> 64);
+
+ /*
+ * We have a 257-bit result. The two top bits can be 00, 01 or 10,
+ * but not 11 (value t <= 2^256 - 38 + 2^255 + 37 = 2^256 + 2^255 - 1).
+ * Therefore, we can truncate to 255 bits, and add 0, 19 or 38.
+ * This guarantees that the result is at most 2^255+37.
+ */
+ cc = (38 & -t4) + (19 & -(t3 >> 63));
+ t3 &= MASK63;
+ z = (unsigned __int128)t0 + (unsigned __int128)cc;
+ d[0] = (uint64_t)z;
+ z = (unsigned __int128)t1 + (z >> 64);
+ d[1] = (uint64_t)z;
+ z = (unsigned __int128)t2 + (z >> 64);
+ d[2] = (uint64_t)z;
+ d[3] = t3 + (uint64_t)(z >> 64);
+
+#elif BR_UMUL128
+
+ /*
+ * We compute t = 2^256 - 38 + a - b, which is necessarily
+ * positive but lower than 2^256 + 2^255, since a <= 2^255 + 37
+ * and b <= 2^255 + 37. We then subtract 0, p or 2*p, depending
+ * on the two upper bits of t (bits 255 and 256).
+ */
+
+ uint64_t t0, t1, t2, t3, t4;
+ unsigned char k;
+
+ k = _subborrow_u64(0, a[0], b[0], &t0);
+ k = _subborrow_u64(k, a[1], b[1], &t1);
+ k = _subborrow_u64(k, a[2], b[2], &t2);
+ k = _subborrow_u64(k, a[3], b[3], &t3);
+ (void)_subborrow_u64(k, 1, 0, &t4);
+
+ k = _subborrow_u64(0, t0, 38, &t0);
+ k = _subborrow_u64(k, t1, 0, &t1);
+ k = _subborrow_u64(k, t2, 0, &t2);
+ k = _subborrow_u64(k, t3, 0, &t3);
+ (void)_subborrow_u64(k, t4, 0, &t4);
+
+ /*
+ * We have a 257-bit result. The two top bits can be 00, 01 or 10,
+ * but not 11 (value t <= 2^256 - 38 + 2^255 + 37 = 2^256 + 2^255 - 1).
+ * Therefore, we can truncate to 255 bits, and add 0, 19 or 38.
+ * This guarantees that the result is at most 2^255+37.
+ */
+ t4 = (38 & -t4) + (19 & -(t3 >> 63));
+ t3 &= MASK63;
+ k = _addcarry_u64(0, t0, t4, &d[0]);
+ k = _addcarry_u64(k, t1, 0, &d[1]);
+ k = _addcarry_u64(k, t2, 0, &d[2]);
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
+
+#endif
+}
+
+/*
+ * Multiplication.
+ */
+static inline void
+f255_mul(uint64_t *d, uint64_t *a, uint64_t *b)
+{
+#if BR_INT128
+
+ unsigned __int128 z;
+ uint64_t t0, t1, t2, t3, t4, t5, t6, t7, th;
+
+ /*
+ * Compute the product a*b over plain integers.
+ */
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[0];
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[1] + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[2] + (z >> 64);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[3] + (z >> 64);
+ t3 = (uint64_t)z;
+ t4 = (uint64_t)(z >> 64);
+
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[0]
+ + (unsigned __int128)t1;
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[1]
+ + (unsigned __int128)t2 + (z >> 64);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[2]
+ + (unsigned __int128)t3 + (z >> 64);
+ t3 = (uint64_t)z;
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[3]
+ + (unsigned __int128)t4 + (z >> 64);
+ t4 = (uint64_t)z;
+ t5 = (uint64_t)(z >> 64);
+
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[0]
+ + (unsigned __int128)t2;
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[1]
+ + (unsigned __int128)t3 + (z >> 64);
+ t3 = (uint64_t)z;
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[2]
+ + (unsigned __int128)t4 + (z >> 64);
+ t4 = (uint64_t)z;
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[3]
+ + (unsigned __int128)t5 + (z >> 64);
+ t5 = (uint64_t)z;
+ t6 = (uint64_t)(z >> 64);
+
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[0]
+ + (unsigned __int128)t3;
+ t3 = (uint64_t)z;
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[1]
+ + (unsigned __int128)t4 + (z >> 64);
+ t4 = (uint64_t)z;
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[2]
+ + (unsigned __int128)t5 + (z >> 64);
+ t5 = (uint64_t)z;
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[3]
+ + (unsigned __int128)t6 + (z >> 64);
+ t6 = (uint64_t)z;
+ t7 = (uint64_t)(z >> 64);
+
+ /*
+ * Modulo p, we have:
+ *
+ * 2^255 = 19
+ * 2^510 = 19*19 = 361
+ *
+ * We split the intermediate t into three parts, in basis
+ * 2^255. The low one will be in t0..t3; the middle one in t4..t7.
+ * The upper one can only be a single bit (th), since the
+ * multiplication operands are at most 2^255+37 each.
+ */
+ th = t7 >> 62;
+ t7 = ((t7 << 1) | (t6 >> 63)) & MASK63;
+ t6 = (t6 << 1) | (t5 >> 63);
+ t5 = (t5 << 1) | (t4 >> 63);
+ t4 = (t4 << 1) | (t3 >> 63);
+ t3 &= MASK63;
+
+ /*
+ * Multiply the middle part (t4..t7) by 19. We truncate it to
+ * 255 bits; the extra bits will go along with th.
+ */
+ z = (unsigned __int128)t4 * 19;
+ t4 = (uint64_t)z;
+ z = (unsigned __int128)t5 * 19 + (z >> 64);
+ t5 = (uint64_t)z;
+ z = (unsigned __int128)t6 * 19 + (z >> 64);
+ t6 = (uint64_t)z;
+ z = (unsigned __int128)t7 * 19 + (z >> 64);
+ t7 = (uint64_t)z & MASK63;
+
+ th = (361 & -th) + (19 * (uint64_t)(z >> 63));
+
+ /*
+ * Add elements together.
+ * At this point:
+ * t0..t3 fits on 255 bits.
+ * t4..t7 fits on 255 bits.
+ * th <= 361 + 342 = 703.
+ */
+ z = (unsigned __int128)t0 + (unsigned __int128)t4
+ + (unsigned __int128)th;
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)t1 + (unsigned __int128)t5 + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)t2 + (unsigned __int128)t6 + (z >> 64);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)t3 + (unsigned __int128)t7 + (z >> 64);
+ t3 = (uint64_t)z & MASK63;
+ th = (uint64_t)(z >> 63);
+
+ /*
+ * Since the sum is at most 2^256 + 703, the two upper bits, in th,
+ * can only have value 0, 1 or 2. We just add th*19, which
+ * guarantees a result of at most 2^255+37.
+ */
+ z = (unsigned __int128)t0 + (19 * th);
+ d[0] = (uint64_t)z;
+ z = (unsigned __int128)t1 + (z >> 64);
+ d[1] = (uint64_t)z;
+ z = (unsigned __int128)t2 + (z >> 64);
+ d[2] = (uint64_t)z;
+ d[3] = t3 + (uint64_t)(z >> 64);
+
+#elif BR_UMUL128
+
+ uint64_t t0, t1, t2, t3, t4, t5, t6, t7, th;
+ uint64_t h0, h1, h2, h3;
+ unsigned char k;
+
+ /*
+ * Compute the product a*b over plain integers.
+ */
+ t0 = _umul128(a[0], b[0], &h0);
+ t1 = _umul128(a[0], b[1], &h1);
+ k = _addcarry_u64(0, t1, h0, &t1);
+ t2 = _umul128(a[0], b[2], &h2);
+ k = _addcarry_u64(k, t2, h1, &t2);
+ t3 = _umul128(a[0], b[3], &h3);
+ k = _addcarry_u64(k, t3, h2, &t3);
+ (void)_addcarry_u64(k, h3, 0, &t4);
+
+ k = _addcarry_u64(0, _umul128(a[1], b[0], &h0), t1, &t1);
+ k = _addcarry_u64(k, _umul128(a[1], b[1], &h1), t2, &t2);
+ k = _addcarry_u64(k, _umul128(a[1], b[2], &h2), t3, &t3);
+ k = _addcarry_u64(k, _umul128(a[1], b[3], &h3), t4, &t4);
+ t5 = k;
+ k = _addcarry_u64(0, t2, h0, &t2);
+ k = _addcarry_u64(k, t3, h1, &t3);
+ k = _addcarry_u64(k, t4, h2, &t4);
+ (void)_addcarry_u64(k, t5, h3, &t5);
+
+ k = _addcarry_u64(0, _umul128(a[2], b[0], &h0), t2, &t2);
+ k = _addcarry_u64(k, _umul128(a[2], b[1], &h1), t3, &t3);
+ k = _addcarry_u64(k, _umul128(a[2], b[2], &h2), t4, &t4);
+ k = _addcarry_u64(k, _umul128(a[2], b[3], &h3), t5, &t5);
+ t6 = k;
+ k = _addcarry_u64(0, t3, h0, &t3);
+ k = _addcarry_u64(k, t4, h1, &t4);
+ k = _addcarry_u64(k, t5, h2, &t5);
+ (void)_addcarry_u64(k, t6, h3, &t6);
+
+ k = _addcarry_u64(0, _umul128(a[3], b[0], &h0), t3, &t3);
+ k = _addcarry_u64(k, _umul128(a[3], b[1], &h1), t4, &t4);
+ k = _addcarry_u64(k, _umul128(a[3], b[2], &h2), t5, &t5);
+ k = _addcarry_u64(k, _umul128(a[3], b[3], &h3), t6, &t6);
+ t7 = k;
+ k = _addcarry_u64(0, t4, h0, &t4);
+ k = _addcarry_u64(k, t5, h1, &t5);
+ k = _addcarry_u64(k, t6, h2, &t6);
+ (void)_addcarry_u64(k, t7, h3, &t7);
+
+ /*
+ * Modulo p, we have:
+ *
+ * 2^255 = 19
+ * 2^510 = 19*19 = 361
+ *
+ * We split the intermediate t into three parts, in basis
+ * 2^255. The low one will be in t0..t3; the middle one in t4..t7.
+ * The upper one can only be a single bit (th), since the
+ * multiplication operands are at most 2^255+37 each.
+ */
+ th = t7 >> 62;
+ t7 = ((t7 << 1) | (t6 >> 63)) & MASK63;
+ t6 = (t6 << 1) | (t5 >> 63);
+ t5 = (t5 << 1) | (t4 >> 63);
+ t4 = (t4 << 1) | (t3 >> 63);
+ t3 &= MASK63;
+
+ /*
+ * Multiply the middle part (t4..t7) by 19. We truncate it to
+ * 255 bits; the extra bits will go along with th.
+ */
+ t4 = _umul128(t4, 19, &h0);
+ t5 = _umul128(t5, 19, &h1);
+ t6 = _umul128(t6, 19, &h2);
+ t7 = _umul128(t7, 19, &h3);
+ k = _addcarry_u64(0, t5, h0, &t5);
+ k = _addcarry_u64(k, t6, h1, &t6);
+ k = _addcarry_u64(k, t7, h2, &t7);
+ (void)_addcarry_u64(k, h3, 0, &h3);
+ th = (361 & -th) + (19 * ((h3 << 1) + (t7 >> 63)));
+ t7 &= MASK63;
+
+ /*
+ * Add elements together.
+ * At this point:
+ * t0..t3 fits on 255 bits.
+ * t4..t7 fits on 255 bits.
+ * th <= 361 + 342 = 703.
+ */
+ k = _addcarry_u64(0, t0, t4, &t0);
+ k = _addcarry_u64(k, t1, t5, &t1);
+ k = _addcarry_u64(k, t2, t6, &t2);
+ k = _addcarry_u64(k, t3, t7, &t3);
+ t4 = k;
+ k = _addcarry_u64(0, t0, th, &t0);
+ k = _addcarry_u64(k, t1, 0, &t1);
+ k = _addcarry_u64(k, t2, 0, &t2);
+ k = _addcarry_u64(k, t3, 0, &t3);
+ (void)_addcarry_u64(k, t4, 0, &t4);
+
+ th = (t4 << 1) + (t3 >> 63);
+ t3 &= MASK63;
+
+ /*
+ * Since the sum is at most 2^256 + 703, the two upper bits, in th,
+ * can only have value 0, 1 or 2. We just add th*19, which
+ * guarantees a result of at most 2^255+37.
+ */
+ k = _addcarry_u64(0, t0, 19 * th, &d[0]);
+ k = _addcarry_u64(k, t1, 0, &d[1]);
+ k = _addcarry_u64(k, t2, 0, &d[2]);
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
+
+#endif
+}
+
+/*
+ * Multiplication by A24 = 121665.
+ */
+static inline void
+f255_mul_a24(uint64_t *d, const uint64_t *a)
+{
+#if BR_INT128
+
+ uint64_t t0, t1, t2, t3;
+ unsigned __int128 z;
+
+ z = (unsigned __int128)a[0] * 121665;
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)a[1] * 121665 + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[2] * 121665 + (z >> 64);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[3] * 121665 + (z >> 64);
+ t3 = (uint64_t)z & MASK63;
+
+ z = (unsigned __int128)t0 + (19 * (uint64_t)(z >> 63));
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)t1 + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)t2 + (z >> 64);
+ t2 = (uint64_t)z;
+ t3 = t3 + (uint64_t)(z >> 64);
+
+ z = (unsigned __int128)t0 + (19 & -(t3 >> 63));
+ d[0] = (uint64_t)z;
+ z = (unsigned __int128)t1 + (z >> 64);
+ d[1] = (uint64_t)z;
+ z = (unsigned __int128)t2 + (z >> 64);
+ d[2] = (uint64_t)z;
+ d[3] = (t3 & MASK63) + (uint64_t)(z >> 64);
+
+#elif BR_UMUL128
+
+ uint64_t t0, t1, t2, t3, t4, h0, h1, h2, h3;
+ unsigned char k;
+
+ t0 = _umul128(a[0], 121665, &h0);
+ t1 = _umul128(a[1], 121665, &h1);
+ k = _addcarry_u64(0, t1, h0, &t1);
+ t2 = _umul128(a[2], 121665, &h2);
+ k = _addcarry_u64(k, t2, h1, &t2);
+ t3 = _umul128(a[3], 121665, &h3);
+ k = _addcarry_u64(k, t3, h2, &t3);
+ (void)_addcarry_u64(k, h3, 0, &t4);
+
+ t4 = (t4 << 1) + (t3 >> 63);
+ t3 &= MASK63;
+ k = _addcarry_u64(0, t0, 19 * t4, &t0);
+ k = _addcarry_u64(k, t1, 0, &t1);
+ k = _addcarry_u64(k, t2, 0, &t2);
+ (void)_addcarry_u64(k, t3, 0, &t3);
+
+ t4 = 19 & -(t3 >> 63);
+ t3 &= MASK63;
+ k = _addcarry_u64(0, t0, t4, &d[0]);
+ k = _addcarry_u64(k, t1, 0, &d[1]);
+ k = _addcarry_u64(k, t2, 0, &d[2]);
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
+
+#endif
+}
+
+/*
+ * Finalize reduction.
+ */
+static inline void
+f255_final_reduce(uint64_t *a)
+{
+#if BR_INT128
+
+ uint64_t t0, t1, t2, t3, m;
+ unsigned __int128 z;
+
+ /*
+ * We add 19. If the result (in t) is below 2^255, then a[]
+ * is already less than 2^255-19, thus already reduced.
+ * Otherwise, we subtract 2^255 from t[], in which case we
+ * have t = a - (2^255-19), and that's our result.
+ */
+ z = (unsigned __int128)a[0] + 19;
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)a[1] + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[2] + (z >> 64);
+ t2 = (uint64_t)z;
+ t3 = a[3] + (uint64_t)(z >> 64);
+
+ m = -(t3 >> 63);
+ t3 &= MASK63;
+ a[0] ^= m & (a[0] ^ t0);
+ a[1] ^= m & (a[1] ^ t1);
+ a[2] ^= m & (a[2] ^ t2);
+ a[3] ^= m & (a[3] ^ t3);
+
+#elif BR_UMUL128
+
+ uint64_t t0, t1, t2, t3, m;
+ unsigned char k;
+
+ /*
+ * We add 19. If the result (in t) is below 2^255, then a[]
+ * is already less than 2^255-19, thus already reduced.
+ * Otherwise, we subtract 2^255 from t[], in which case we
+ * have t = a - (2^255-19), and that's our result.
+ */
+ k = _addcarry_u64(0, a[0], 19, &t0);
+ k = _addcarry_u64(k, a[1], 0, &t1);
+ k = _addcarry_u64(k, a[2], 0, &t2);
+ (void)_addcarry_u64(k, a[3], 0, &t3);
+
+ m = -(t3 >> 63);
+ t3 &= MASK63;
+ a[0] ^= m & (a[0] ^ t0);
+ a[1] ^= m & (a[1] ^ t1);
+ a[2] ^= m & (a[2] ^ t2);
+ a[3] ^= m & (a[3] ^ t3);
+
+#endif
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *kb, size_t kblen, int curve)
+{
+ unsigned char k[32];
+ uint64_t x1[4], x2[4], z2[4], x3[4], z3[4];
+ uint32_t swap;
+ int i;
+
+ (void)curve;
+
+ /*
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
+ * in 32 bytes as well.
+ */
+ if (Glen != 32 || kblen > 32) {
+ return 0;
+ }
+
+ /*
+ * RFC 7748 mandates that the high bit of the last point byte must
+ * be ignored/cleared.
+ */
+ x1[0] = br_dec64le(&G[ 0]);
+ x1[1] = br_dec64le(&G[ 8]);
+ x1[2] = br_dec64le(&G[16]);
+ x1[3] = br_dec64le(&G[24]) & MASK63;
+
+ /*
+ * We can use memset() to clear values, because exact-width types
+ * like uint64_t are guaranteed to have no padding bits or
+ * trap representations.
+ */
+ memset(x2, 0, sizeof x2);
+ x2[0] = 1;
+ memset(z2, 0, sizeof z2);
+ memcpy(x3, x1, sizeof x1);
+ memcpy(z3, x2, sizeof x2);
+
+ /*
+ * The multiplier is provided in big-endian notation, and
+ * possibly shorter than 32 bytes.
+ */
+ memset(k, 0, (sizeof k) - kblen);
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
+ k[31] &= 0xF8;
+ k[0] &= 0x7F;
+ k[0] |= 0x40;
+
+ swap = 0;
+
+ for (i = 254; i >= 0; i --) {
+ uint64_t a[4], aa[4], b[4], bb[4], e[4];
+ uint64_t c[4], d[4], da[4], cb[4];
+ uint32_t kt;
+
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
+ swap ^= kt;
+ f255_cswap(x2, x3, swap);
+ f255_cswap(z2, z3, swap);
+ swap = kt;
+
+ /* A = x_2 + z_2 */
+ f255_add(a, x2, z2);
+
+ /* AA = A^2 */
+ f255_mul(aa, a, a);
+
+ /* B = x_2 - z_2 */
+ f255_sub(b, x2, z2);
+
+ /* BB = B^2 */
+ f255_mul(bb, b, b);
+
+ /* E = AA - BB */
+ f255_sub(e, aa, bb);
+
+ /* C = x_3 + z_3 */
+ f255_add(c, x3, z3);
+
+ /* D = x_3 - z_3 */
+ f255_sub(d, x3, z3);
+
+ /* DA = D * A */
+ f255_mul(da, d, a);
+
+ /* CB = C * B */
+ f255_mul(cb, c, b);
+
+ /* x_3 = (DA + CB)^2 */
+ f255_add(x3, da, cb);
+ f255_mul(x3, x3, x3);
+
+ /* z_3 = x_1 * (DA - CB)^2 */
+ f255_sub(z3, da, cb);
+ f255_mul(z3, z3, z3);
+ f255_mul(z3, x1, z3);
+
+ /* x_2 = AA * BB */
+ f255_mul(x2, aa, bb);
+
+ /* z_2 = E * (AA + a24 * E) */
+ f255_mul_a24(z2, e);
+ f255_add(z2, aa, z2);
+ f255_mul(z2, e, z2);
+ }
+
+ f255_cswap(x2, x3, swap);
+ f255_cswap(z2, z3, swap);
+
+ /*
+ * Compute 1/z2 = z2^(p-2). Since p = 2^255-19, we can mutualize
+ * most non-squarings. We use x1 and x3, now useless, as temporaries.
+ */
+ memcpy(x1, z2, sizeof z2);
+ for (i = 0; i < 15; i ++) {
+ f255_mul(x1, x1, x1);
+ f255_mul(x1, x1, z2);
+ }
+ memcpy(x3, x1, sizeof x1);
+ for (i = 0; i < 14; i ++) {
+ int j;
+
+ for (j = 0; j < 16; j ++) {
+ f255_mul(x3, x3, x3);
+ }
+ f255_mul(x3, x3, x1);
+ }
+ for (i = 14; i >= 0; i --) {
+ f255_mul(x3, x3, x3);
+ if ((0xFFEB >> i) & 1) {
+ f255_mul(x3, z2, x3);
+ }
+ }
+
+ /*
+ * Compute x2/z2. We have 1/z2 in x3.
+ */
+ f255_mul(x2, x2, x3);
+ f255_final_reduce(x2);
+
+ /*
+ * Encode the final x2 value in little-endian.
+ */
+ br_enc64le(G, x2[0]);
+ br_enc64le(G + 8, x2[1]);
+ br_enc64le(G + 16, x2[2]);
+ br_enc64le(G + 24, x2[3]);
+ return 1;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We don't implement this method, since it is used for ECDSA
+ * only, and there is no ECDSA over Curve25519 (which instead
+ * uses EdDSA).
+ */
+ (void)A;
+ (void)B;
+ (void)len;
+ (void)x;
+ (void)xlen;
+ (void)y;
+ (void)ylen;
+ (void)curve;
+ return 0;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_c25519_m64 = {
+ (uint32_t)0x20000000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_c25519_m64_get(void)
+{
+ return &br_ec_c25519_m64;
+}
+
+#else
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_c25519_m64_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/ec/ec_curve25519.c b/test/monniaux/BearSSL/src/ec/ec_curve25519.c
new file mode 100644
index 00000000..a47d215e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_curve25519.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char GEN[] = {
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const unsigned char ORDER[] = {
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+/* see inner.h */
+const br_ec_curve_def br_curve25519 = {
+ BR_EC_curve25519,
+ ORDER, sizeof ORDER,
+ GEN, sizeof GEN
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_default.c b/test/monniaux/BearSSL/src/ec/ec_default.c
new file mode 100644
index 00000000..7bb6e0c7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_default.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_get_default(void)
+{
+#if BR_LOMUL
+ return &br_ec_all_m15;
+#else
+ return &br_ec_all_m31;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ec/ec_keygen.c b/test/monniaux/BearSSL/src/ec/ec_keygen.c
new file mode 100644
index 00000000..02a30962
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_keygen.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+size_t
+br_ec_keygen(const br_prng_class **rng_ctx,
+ const br_ec_impl *impl, br_ec_private_key *sk,
+ void *kbuf, int curve)
+{
+ const unsigned char *order;
+ unsigned char *buf;
+ size_t len;
+ unsigned mask;
+
+ if (curve < 0 || curve >= 32
+ || ((impl->supported_curves >> curve) & 1) == 0)
+ {
+ return 0;
+ }
+ order = impl->order(curve, &len);
+ while (len > 0 && *order == 0) {
+ order ++;
+ len --;
+ }
+ if (kbuf == NULL || len == 0) {
+ return len;
+ }
+ mask = order[0];
+ mask |= (mask >> 1);
+ mask |= (mask >> 2);
+ mask |= (mask >> 4);
+
+ /*
+ * We generate sequences of random bits of the right size, until
+ * the value is strictly lower than the curve order (we also
+ * check for all-zero values, which are invalid).
+ */
+ buf = kbuf;
+ for (;;) {
+ size_t u;
+ unsigned cc, zz;
+
+ (*rng_ctx)->generate(rng_ctx, buf, len);
+ buf[0] &= mask;
+ cc = 0;
+ u = len;
+ zz = 0;
+ while (u -- > 0) {
+ cc = ((unsigned)(buf[u] - order[u] - cc) >> 8) & 1;
+ zz |= buf[u];
+ }
+ if (cc != 0 && zz != 0) {
+ break;
+ }
+ }
+
+ if (sk != NULL) {
+ sk->curve = curve;
+ sk->x = buf;
+ sk->xlen = len;
+ }
+ return len;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ec_p256_m15.c b/test/monniaux/BearSSL/src/ec/ec_p256_m15.c
new file mode 100644
index 00000000..8d68d1d2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_p256_m15.c
@@ -0,0 +1,2130 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * If BR_NO_ARITH_SHIFT is undefined, or defined to 0, then we _assume_
+ * that right-shifting a signed negative integer copies the sign bit
+ * (arithmetic right-shift). This is "implementation-defined behaviour",
+ * i.e. it is not undefined, but it may differ between compilers. Each
+ * compiler is supposed to document its behaviour in that respect. GCC
+ * explicitly defines that an arithmetic right shift is used. We expect
+ * all other compilers to do the same, because underlying CPU offer an
+ * arithmetic right shift opcode that could not be used otherwise.
+ */
+#if BR_NO_ARITH_SHIFT
+#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
+ | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
+#else
+#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
+#endif
+
+/*
+ * Convert an integer from unsigned big-endian encoding to a sequence of
+ * 13-bit words in little-endian order. The final "partial" word is
+ * returned.
+ */
+static uint32_t
+be8_to_le13(uint32_t *dst, const unsigned char *src, size_t len)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ acc |= (uint32_t)src[len] << acc_len;
+ acc_len += 8;
+ if (acc_len >= 13) {
+ *dst ++ = acc & 0x1FFF;
+ acc >>= 13;
+ acc_len -= 13;
+ }
+ }
+ return acc;
+}
+
+/*
+ * Convert an integer (13-bit words, little-endian) to unsigned
+ * big-endian encoding. The total encoding length is provided; all
+ * the destination bytes will be filled.
+ */
+static void
+le13_to_be8(unsigned char *dst, size_t len, const uint32_t *src)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ if (acc_len < 8) {
+ acc |= (*src ++) << acc_len;
+ acc_len += 13;
+ }
+ dst[len] = (unsigned char)acc;
+ acc >>= 8;
+ acc_len -= 8;
+ }
+}
+
+/*
+ * Normalise an array of words to a strict 13 bits per word. Returned
+ * value is the resulting carry. The source (w) and destination (d)
+ * arrays may be identical, but shall not overlap partially.
+ */
+static inline uint32_t
+norm13(uint32_t *d, const uint32_t *w, size_t len)
+{
+ size_t u;
+ uint32_t cc;
+
+ cc = 0;
+ for (u = 0; u < len; u ++) {
+ int32_t z;
+
+ z = w[u] + cc;
+ d[u] = z & 0x1FFF;
+ cc = ARSH(z, 13);
+ }
+ return cc;
+}
+
+/*
+ * mul20() multiplies two 260-bit integers together. Each word must fit
+ * on 13 bits; source operands use 20 words, destination operand
+ * receives 40 words. All overlaps allowed.
+ *
+ * square20() computes the square of a 260-bit integer. Each word must
+ * fit on 13 bits; source operand uses 20 words, destination operand
+ * receives 40 words. All overlaps allowed.
+ */
+
+#if BR_SLOW_MUL15
+
+static void
+mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * Two-level Karatsuba: turns a 20x20 multiplication into
+ * nine 5x5 multiplications. We use 13-bit words but do not
+ * propagate carries immediately, so words may expand:
+ *
+ * - First Karatsuba decomposition turns the 20x20 mul on
+ * 13-bit words into three 10x10 muls, two on 13-bit words
+ * and one on 14-bit words.
+ *
+ * - Second Karatsuba decomposition further splits these into:
+ *
+ * * four 5x5 muls on 13-bit words
+ * * four 5x5 muls on 14-bit words
+ * * one 5x5 mul on 15-bit words
+ *
+ * Highest word value is 8191, 16382 or 32764, for 13-bit, 14-bit
+ * or 15-bit words, respectively.
+ */
+ uint32_t u[45], v[45], w[90];
+ uint32_t cc;
+ int i;
+
+#define ZADD(dw, d_off, s1w, s1_off, s2w, s2_off) do { \
+ (dw)[5 * (d_off) + 0] = (s1w)[5 * (s1_off) + 0] \
+ + (s2w)[5 * (s2_off) + 0]; \
+ (dw)[5 * (d_off) + 1] = (s1w)[5 * (s1_off) + 1] \
+ + (s2w)[5 * (s2_off) + 1]; \
+ (dw)[5 * (d_off) + 2] = (s1w)[5 * (s1_off) + 2] \
+ + (s2w)[5 * (s2_off) + 2]; \
+ (dw)[5 * (d_off) + 3] = (s1w)[5 * (s1_off) + 3] \
+ + (s2w)[5 * (s2_off) + 3]; \
+ (dw)[5 * (d_off) + 4] = (s1w)[5 * (s1_off) + 4] \
+ + (s2w)[5 * (s2_off) + 4]; \
+ } while (0)
+
+#define ZADDT(dw, d_off, sw, s_off) do { \
+ (dw)[5 * (d_off) + 0] += (sw)[5 * (s_off) + 0]; \
+ (dw)[5 * (d_off) + 1] += (sw)[5 * (s_off) + 1]; \
+ (dw)[5 * (d_off) + 2] += (sw)[5 * (s_off) + 2]; \
+ (dw)[5 * (d_off) + 3] += (sw)[5 * (s_off) + 3]; \
+ (dw)[5 * (d_off) + 4] += (sw)[5 * (s_off) + 4]; \
+ } while (0)
+
+#define ZSUB2F(dw, d_off, s1w, s1_off, s2w, s2_off) do { \
+ (dw)[5 * (d_off) + 0] -= (s1w)[5 * (s1_off) + 0] \
+ + (s2w)[5 * (s2_off) + 0]; \
+ (dw)[5 * (d_off) + 1] -= (s1w)[5 * (s1_off) + 1] \
+ + (s2w)[5 * (s2_off) + 1]; \
+ (dw)[5 * (d_off) + 2] -= (s1w)[5 * (s1_off) + 2] \
+ + (s2w)[5 * (s2_off) + 2]; \
+ (dw)[5 * (d_off) + 3] -= (s1w)[5 * (s1_off) + 3] \
+ + (s2w)[5 * (s2_off) + 3]; \
+ (dw)[5 * (d_off) + 4] -= (s1w)[5 * (s1_off) + 4] \
+ + (s2w)[5 * (s2_off) + 4]; \
+ } while (0)
+
+#define CPR1(w, cprcc) do { \
+ uint32_t cprz = (w) + cprcc; \
+ (w) = cprz & 0x1FFF; \
+ cprcc = cprz >> 13; \
+ } while (0)
+
+#define CPR(dw, d_off) do { \
+ uint32_t cprcc; \
+ cprcc = 0; \
+ CPR1((dw)[(d_off) + 0], cprcc); \
+ CPR1((dw)[(d_off) + 1], cprcc); \
+ CPR1((dw)[(d_off) + 2], cprcc); \
+ CPR1((dw)[(d_off) + 3], cprcc); \
+ CPR1((dw)[(d_off) + 4], cprcc); \
+ CPR1((dw)[(d_off) + 5], cprcc); \
+ CPR1((dw)[(d_off) + 6], cprcc); \
+ CPR1((dw)[(d_off) + 7], cprcc); \
+ CPR1((dw)[(d_off) + 8], cprcc); \
+ (dw)[(d_off) + 9] = cprcc; \
+ } while (0)
+
+ memcpy(u, a, 20 * sizeof *a);
+ ZADD(u, 4, a, 0, a, 1);
+ ZADD(u, 5, a, 2, a, 3);
+ ZADD(u, 6, a, 0, a, 2);
+ ZADD(u, 7, a, 1, a, 3);
+ ZADD(u, 8, u, 6, u, 7);
+
+ memcpy(v, b, 20 * sizeof *b);
+ ZADD(v, 4, b, 0, b, 1);
+ ZADD(v, 5, b, 2, b, 3);
+ ZADD(v, 6, b, 0, b, 2);
+ ZADD(v, 7, b, 1, b, 3);
+ ZADD(v, 8, v, 6, v, 7);
+
+ /*
+ * Do the eight first 8x8 muls. Source words are at most 16382
+ * each, so we can add product results together "as is" in 32-bit
+ * words.
+ */
+ for (i = 0; i < 40; i += 5) {
+ w[(i << 1) + 0] = MUL15(u[i + 0], v[i + 0]);
+ w[(i << 1) + 1] = MUL15(u[i + 0], v[i + 1])
+ + MUL15(u[i + 1], v[i + 0]);
+ w[(i << 1) + 2] = MUL15(u[i + 0], v[i + 2])
+ + MUL15(u[i + 1], v[i + 1])
+ + MUL15(u[i + 2], v[i + 0]);
+ w[(i << 1) + 3] = MUL15(u[i + 0], v[i + 3])
+ + MUL15(u[i + 1], v[i + 2])
+ + MUL15(u[i + 2], v[i + 1])
+ + MUL15(u[i + 3], v[i + 0]);
+ w[(i << 1) + 4] = MUL15(u[i + 0], v[i + 4])
+ + MUL15(u[i + 1], v[i + 3])
+ + MUL15(u[i + 2], v[i + 2])
+ + MUL15(u[i + 3], v[i + 1])
+ + MUL15(u[i + 4], v[i + 0]);
+ w[(i << 1) + 5] = MUL15(u[i + 1], v[i + 4])
+ + MUL15(u[i + 2], v[i + 3])
+ + MUL15(u[i + 3], v[i + 2])
+ + MUL15(u[i + 4], v[i + 1]);
+ w[(i << 1) + 6] = MUL15(u[i + 2], v[i + 4])
+ + MUL15(u[i + 3], v[i + 3])
+ + MUL15(u[i + 4], v[i + 2]);
+ w[(i << 1) + 7] = MUL15(u[i + 3], v[i + 4])
+ + MUL15(u[i + 4], v[i + 3]);
+ w[(i << 1) + 8] = MUL15(u[i + 4], v[i + 4]);
+ w[(i << 1) + 9] = 0;
+ }
+
+ /*
+ * For the 9th multiplication, source words are up to 32764,
+ * so we must do some carry propagation. If we add up to
+ * 4 products and the carry is no more than 524224, then the
+ * result fits in 32 bits, and the next carry will be no more
+ * than 524224 (because 4*(32764^2)+524224 < 8192*524225).
+ *
+ * We thus just skip one of the products in the middle word,
+ * then do a carry propagation (this reduces words to 13 bits
+ * each, except possibly the last, which may use up to 17 bits
+ * or so), then add the missing product.
+ */
+ w[80 + 0] = MUL15(u[40 + 0], v[40 + 0]);
+ w[80 + 1] = MUL15(u[40 + 0], v[40 + 1])
+ + MUL15(u[40 + 1], v[40 + 0]);
+ w[80 + 2] = MUL15(u[40 + 0], v[40 + 2])
+ + MUL15(u[40 + 1], v[40 + 1])
+ + MUL15(u[40 + 2], v[40 + 0]);
+ w[80 + 3] = MUL15(u[40 + 0], v[40 + 3])
+ + MUL15(u[40 + 1], v[40 + 2])
+ + MUL15(u[40 + 2], v[40 + 1])
+ + MUL15(u[40 + 3], v[40 + 0]);
+ w[80 + 4] = MUL15(u[40 + 0], v[40 + 4])
+ + MUL15(u[40 + 1], v[40 + 3])
+ + MUL15(u[40 + 2], v[40 + 2])
+ + MUL15(u[40 + 3], v[40 + 1]);
+ /* + MUL15(u[40 + 4], v[40 + 0]) */
+ w[80 + 5] = MUL15(u[40 + 1], v[40 + 4])
+ + MUL15(u[40 + 2], v[40 + 3])
+ + MUL15(u[40 + 3], v[40 + 2])
+ + MUL15(u[40 + 4], v[40 + 1]);
+ w[80 + 6] = MUL15(u[40 + 2], v[40 + 4])
+ + MUL15(u[40 + 3], v[40 + 3])
+ + MUL15(u[40 + 4], v[40 + 2]);
+ w[80 + 7] = MUL15(u[40 + 3], v[40 + 4])
+ + MUL15(u[40 + 4], v[40 + 3]);
+ w[80 + 8] = MUL15(u[40 + 4], v[40 + 4]);
+
+ CPR(w, 80);
+
+ w[80 + 4] += MUL15(u[40 + 4], v[40 + 0]);
+
+ /*
+ * The products on 14-bit words in slots 6 and 7 yield values
+ * up to 5*(16382^2) each, and we need to subtract two such
+ * values from the higher word. We need the subtraction to fit
+ * in a _signed_ 32-bit integer, i.e. 31 bits + a sign bit.
+ * However, 10*(16382^2) does not fit. So we must perform a
+ * bit of reduction here.
+ */
+ CPR(w, 60);
+ CPR(w, 70);
+
+ /*
+ * Recompose results.
+ */
+
+ /* 0..1*0..1 into 0..3 */
+ ZSUB2F(w, 8, w, 0, w, 2);
+ ZSUB2F(w, 9, w, 1, w, 3);
+ ZADDT(w, 1, w, 8);
+ ZADDT(w, 2, w, 9);
+
+ /* 2..3*2..3 into 4..7 */
+ ZSUB2F(w, 10, w, 4, w, 6);
+ ZSUB2F(w, 11, w, 5, w, 7);
+ ZADDT(w, 5, w, 10);
+ ZADDT(w, 6, w, 11);
+
+ /* (0..1+2..3)*(0..1+2..3) into 12..15 */
+ ZSUB2F(w, 16, w, 12, w, 14);
+ ZSUB2F(w, 17, w, 13, w, 15);
+ ZADDT(w, 13, w, 16);
+ ZADDT(w, 14, w, 17);
+
+ /* first-level recomposition */
+ ZSUB2F(w, 12, w, 0, w, 4);
+ ZSUB2F(w, 13, w, 1, w, 5);
+ ZSUB2F(w, 14, w, 2, w, 6);
+ ZSUB2F(w, 15, w, 3, w, 7);
+ ZADDT(w, 2, w, 12);
+ ZADDT(w, 3, w, 13);
+ ZADDT(w, 4, w, 14);
+ ZADDT(w, 5, w, 15);
+
+ /*
+ * Perform carry propagation to bring all words down to 13 bits.
+ */
+ cc = norm13(d, w, 40);
+ d[39] += (cc << 13);
+
+#undef ZADD
+#undef ZADDT
+#undef ZSUB2F
+#undef CPR1
+#undef CPR
+}
+
+static inline void
+square20(uint32_t *d, const uint32_t *a)
+{
+ mul20(d, a, a);
+}
+
+#else
+
+static void
+mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[39];
+
+ t[ 0] = MUL15(a[ 0], b[ 0]);
+ t[ 1] = MUL15(a[ 0], b[ 1])
+ + MUL15(a[ 1], b[ 0]);
+ t[ 2] = MUL15(a[ 0], b[ 2])
+ + MUL15(a[ 1], b[ 1])
+ + MUL15(a[ 2], b[ 0]);
+ t[ 3] = MUL15(a[ 0], b[ 3])
+ + MUL15(a[ 1], b[ 2])
+ + MUL15(a[ 2], b[ 1])
+ + MUL15(a[ 3], b[ 0]);
+ t[ 4] = MUL15(a[ 0], b[ 4])
+ + MUL15(a[ 1], b[ 3])
+ + MUL15(a[ 2], b[ 2])
+ + MUL15(a[ 3], b[ 1])
+ + MUL15(a[ 4], b[ 0]);
+ t[ 5] = MUL15(a[ 0], b[ 5])
+ + MUL15(a[ 1], b[ 4])
+ + MUL15(a[ 2], b[ 3])
+ + MUL15(a[ 3], b[ 2])
+ + MUL15(a[ 4], b[ 1])
+ + MUL15(a[ 5], b[ 0]);
+ t[ 6] = MUL15(a[ 0], b[ 6])
+ + MUL15(a[ 1], b[ 5])
+ + MUL15(a[ 2], b[ 4])
+ + MUL15(a[ 3], b[ 3])
+ + MUL15(a[ 4], b[ 2])
+ + MUL15(a[ 5], b[ 1])
+ + MUL15(a[ 6], b[ 0]);
+ t[ 7] = MUL15(a[ 0], b[ 7])
+ + MUL15(a[ 1], b[ 6])
+ + MUL15(a[ 2], b[ 5])
+ + MUL15(a[ 3], b[ 4])
+ + MUL15(a[ 4], b[ 3])
+ + MUL15(a[ 5], b[ 2])
+ + MUL15(a[ 6], b[ 1])
+ + MUL15(a[ 7], b[ 0]);
+ t[ 8] = MUL15(a[ 0], b[ 8])
+ + MUL15(a[ 1], b[ 7])
+ + MUL15(a[ 2], b[ 6])
+ + MUL15(a[ 3], b[ 5])
+ + MUL15(a[ 4], b[ 4])
+ + MUL15(a[ 5], b[ 3])
+ + MUL15(a[ 6], b[ 2])
+ + MUL15(a[ 7], b[ 1])
+ + MUL15(a[ 8], b[ 0]);
+ t[ 9] = MUL15(a[ 0], b[ 9])
+ + MUL15(a[ 1], b[ 8])
+ + MUL15(a[ 2], b[ 7])
+ + MUL15(a[ 3], b[ 6])
+ + MUL15(a[ 4], b[ 5])
+ + MUL15(a[ 5], b[ 4])
+ + MUL15(a[ 6], b[ 3])
+ + MUL15(a[ 7], b[ 2])
+ + MUL15(a[ 8], b[ 1])
+ + MUL15(a[ 9], b[ 0]);
+ t[10] = MUL15(a[ 0], b[10])
+ + MUL15(a[ 1], b[ 9])
+ + MUL15(a[ 2], b[ 8])
+ + MUL15(a[ 3], b[ 7])
+ + MUL15(a[ 4], b[ 6])
+ + MUL15(a[ 5], b[ 5])
+ + MUL15(a[ 6], b[ 4])
+ + MUL15(a[ 7], b[ 3])
+ + MUL15(a[ 8], b[ 2])
+ + MUL15(a[ 9], b[ 1])
+ + MUL15(a[10], b[ 0]);
+ t[11] = MUL15(a[ 0], b[11])
+ + MUL15(a[ 1], b[10])
+ + MUL15(a[ 2], b[ 9])
+ + MUL15(a[ 3], b[ 8])
+ + MUL15(a[ 4], b[ 7])
+ + MUL15(a[ 5], b[ 6])
+ + MUL15(a[ 6], b[ 5])
+ + MUL15(a[ 7], b[ 4])
+ + MUL15(a[ 8], b[ 3])
+ + MUL15(a[ 9], b[ 2])
+ + MUL15(a[10], b[ 1])
+ + MUL15(a[11], b[ 0]);
+ t[12] = MUL15(a[ 0], b[12])
+ + MUL15(a[ 1], b[11])
+ + MUL15(a[ 2], b[10])
+ + MUL15(a[ 3], b[ 9])
+ + MUL15(a[ 4], b[ 8])
+ + MUL15(a[ 5], b[ 7])
+ + MUL15(a[ 6], b[ 6])
+ + MUL15(a[ 7], b[ 5])
+ + MUL15(a[ 8], b[ 4])
+ + MUL15(a[ 9], b[ 3])
+ + MUL15(a[10], b[ 2])
+ + MUL15(a[11], b[ 1])
+ + MUL15(a[12], b[ 0]);
+ t[13] = MUL15(a[ 0], b[13])
+ + MUL15(a[ 1], b[12])
+ + MUL15(a[ 2], b[11])
+ + MUL15(a[ 3], b[10])
+ + MUL15(a[ 4], b[ 9])
+ + MUL15(a[ 5], b[ 8])
+ + MUL15(a[ 6], b[ 7])
+ + MUL15(a[ 7], b[ 6])
+ + MUL15(a[ 8], b[ 5])
+ + MUL15(a[ 9], b[ 4])
+ + MUL15(a[10], b[ 3])
+ + MUL15(a[11], b[ 2])
+ + MUL15(a[12], b[ 1])
+ + MUL15(a[13], b[ 0]);
+ t[14] = MUL15(a[ 0], b[14])
+ + MUL15(a[ 1], b[13])
+ + MUL15(a[ 2], b[12])
+ + MUL15(a[ 3], b[11])
+ + MUL15(a[ 4], b[10])
+ + MUL15(a[ 5], b[ 9])
+ + MUL15(a[ 6], b[ 8])
+ + MUL15(a[ 7], b[ 7])
+ + MUL15(a[ 8], b[ 6])
+ + MUL15(a[ 9], b[ 5])
+ + MUL15(a[10], b[ 4])
+ + MUL15(a[11], b[ 3])
+ + MUL15(a[12], b[ 2])
+ + MUL15(a[13], b[ 1])
+ + MUL15(a[14], b[ 0]);
+ t[15] = MUL15(a[ 0], b[15])
+ + MUL15(a[ 1], b[14])
+ + MUL15(a[ 2], b[13])
+ + MUL15(a[ 3], b[12])
+ + MUL15(a[ 4], b[11])
+ + MUL15(a[ 5], b[10])
+ + MUL15(a[ 6], b[ 9])
+ + MUL15(a[ 7], b[ 8])
+ + MUL15(a[ 8], b[ 7])
+ + MUL15(a[ 9], b[ 6])
+ + MUL15(a[10], b[ 5])
+ + MUL15(a[11], b[ 4])
+ + MUL15(a[12], b[ 3])
+ + MUL15(a[13], b[ 2])
+ + MUL15(a[14], b[ 1])
+ + MUL15(a[15], b[ 0]);
+ t[16] = MUL15(a[ 0], b[16])
+ + MUL15(a[ 1], b[15])
+ + MUL15(a[ 2], b[14])
+ + MUL15(a[ 3], b[13])
+ + MUL15(a[ 4], b[12])
+ + MUL15(a[ 5], b[11])
+ + MUL15(a[ 6], b[10])
+ + MUL15(a[ 7], b[ 9])
+ + MUL15(a[ 8], b[ 8])
+ + MUL15(a[ 9], b[ 7])
+ + MUL15(a[10], b[ 6])
+ + MUL15(a[11], b[ 5])
+ + MUL15(a[12], b[ 4])
+ + MUL15(a[13], b[ 3])
+ + MUL15(a[14], b[ 2])
+ + MUL15(a[15], b[ 1])
+ + MUL15(a[16], b[ 0]);
+ t[17] = MUL15(a[ 0], b[17])
+ + MUL15(a[ 1], b[16])
+ + MUL15(a[ 2], b[15])
+ + MUL15(a[ 3], b[14])
+ + MUL15(a[ 4], b[13])
+ + MUL15(a[ 5], b[12])
+ + MUL15(a[ 6], b[11])
+ + MUL15(a[ 7], b[10])
+ + MUL15(a[ 8], b[ 9])
+ + MUL15(a[ 9], b[ 8])
+ + MUL15(a[10], b[ 7])
+ + MUL15(a[11], b[ 6])
+ + MUL15(a[12], b[ 5])
+ + MUL15(a[13], b[ 4])
+ + MUL15(a[14], b[ 3])
+ + MUL15(a[15], b[ 2])
+ + MUL15(a[16], b[ 1])
+ + MUL15(a[17], b[ 0]);
+ t[18] = MUL15(a[ 0], b[18])
+ + MUL15(a[ 1], b[17])
+ + MUL15(a[ 2], b[16])
+ + MUL15(a[ 3], b[15])
+ + MUL15(a[ 4], b[14])
+ + MUL15(a[ 5], b[13])
+ + MUL15(a[ 6], b[12])
+ + MUL15(a[ 7], b[11])
+ + MUL15(a[ 8], b[10])
+ + MUL15(a[ 9], b[ 9])
+ + MUL15(a[10], b[ 8])
+ + MUL15(a[11], b[ 7])
+ + MUL15(a[12], b[ 6])
+ + MUL15(a[13], b[ 5])
+ + MUL15(a[14], b[ 4])
+ + MUL15(a[15], b[ 3])
+ + MUL15(a[16], b[ 2])
+ + MUL15(a[17], b[ 1])
+ + MUL15(a[18], b[ 0]);
+ t[19] = MUL15(a[ 0], b[19])
+ + MUL15(a[ 1], b[18])
+ + MUL15(a[ 2], b[17])
+ + MUL15(a[ 3], b[16])
+ + MUL15(a[ 4], b[15])
+ + MUL15(a[ 5], b[14])
+ + MUL15(a[ 6], b[13])
+ + MUL15(a[ 7], b[12])
+ + MUL15(a[ 8], b[11])
+ + MUL15(a[ 9], b[10])
+ + MUL15(a[10], b[ 9])
+ + MUL15(a[11], b[ 8])
+ + MUL15(a[12], b[ 7])
+ + MUL15(a[13], b[ 6])
+ + MUL15(a[14], b[ 5])
+ + MUL15(a[15], b[ 4])
+ + MUL15(a[16], b[ 3])
+ + MUL15(a[17], b[ 2])
+ + MUL15(a[18], b[ 1])
+ + MUL15(a[19], b[ 0]);
+ t[20] = MUL15(a[ 1], b[19])
+ + MUL15(a[ 2], b[18])
+ + MUL15(a[ 3], b[17])
+ + MUL15(a[ 4], b[16])
+ + MUL15(a[ 5], b[15])
+ + MUL15(a[ 6], b[14])
+ + MUL15(a[ 7], b[13])
+ + MUL15(a[ 8], b[12])
+ + MUL15(a[ 9], b[11])
+ + MUL15(a[10], b[10])
+ + MUL15(a[11], b[ 9])
+ + MUL15(a[12], b[ 8])
+ + MUL15(a[13], b[ 7])
+ + MUL15(a[14], b[ 6])
+ + MUL15(a[15], b[ 5])
+ + MUL15(a[16], b[ 4])
+ + MUL15(a[17], b[ 3])
+ + MUL15(a[18], b[ 2])
+ + MUL15(a[19], b[ 1]);
+ t[21] = MUL15(a[ 2], b[19])
+ + MUL15(a[ 3], b[18])
+ + MUL15(a[ 4], b[17])
+ + MUL15(a[ 5], b[16])
+ + MUL15(a[ 6], b[15])
+ + MUL15(a[ 7], b[14])
+ + MUL15(a[ 8], b[13])
+ + MUL15(a[ 9], b[12])
+ + MUL15(a[10], b[11])
+ + MUL15(a[11], b[10])
+ + MUL15(a[12], b[ 9])
+ + MUL15(a[13], b[ 8])
+ + MUL15(a[14], b[ 7])
+ + MUL15(a[15], b[ 6])
+ + MUL15(a[16], b[ 5])
+ + MUL15(a[17], b[ 4])
+ + MUL15(a[18], b[ 3])
+ + MUL15(a[19], b[ 2]);
+ t[22] = MUL15(a[ 3], b[19])
+ + MUL15(a[ 4], b[18])
+ + MUL15(a[ 5], b[17])
+ + MUL15(a[ 6], b[16])
+ + MUL15(a[ 7], b[15])
+ + MUL15(a[ 8], b[14])
+ + MUL15(a[ 9], b[13])
+ + MUL15(a[10], b[12])
+ + MUL15(a[11], b[11])
+ + MUL15(a[12], b[10])
+ + MUL15(a[13], b[ 9])
+ + MUL15(a[14], b[ 8])
+ + MUL15(a[15], b[ 7])
+ + MUL15(a[16], b[ 6])
+ + MUL15(a[17], b[ 5])
+ + MUL15(a[18], b[ 4])
+ + MUL15(a[19], b[ 3]);
+ t[23] = MUL15(a[ 4], b[19])
+ + MUL15(a[ 5], b[18])
+ + MUL15(a[ 6], b[17])
+ + MUL15(a[ 7], b[16])
+ + MUL15(a[ 8], b[15])
+ + MUL15(a[ 9], b[14])
+ + MUL15(a[10], b[13])
+ + MUL15(a[11], b[12])
+ + MUL15(a[12], b[11])
+ + MUL15(a[13], b[10])
+ + MUL15(a[14], b[ 9])
+ + MUL15(a[15], b[ 8])
+ + MUL15(a[16], b[ 7])
+ + MUL15(a[17], b[ 6])
+ + MUL15(a[18], b[ 5])
+ + MUL15(a[19], b[ 4]);
+ t[24] = MUL15(a[ 5], b[19])
+ + MUL15(a[ 6], b[18])
+ + MUL15(a[ 7], b[17])
+ + MUL15(a[ 8], b[16])
+ + MUL15(a[ 9], b[15])
+ + MUL15(a[10], b[14])
+ + MUL15(a[11], b[13])
+ + MUL15(a[12], b[12])
+ + MUL15(a[13], b[11])
+ + MUL15(a[14], b[10])
+ + MUL15(a[15], b[ 9])
+ + MUL15(a[16], b[ 8])
+ + MUL15(a[17], b[ 7])
+ + MUL15(a[18], b[ 6])
+ + MUL15(a[19], b[ 5]);
+ t[25] = MUL15(a[ 6], b[19])
+ + MUL15(a[ 7], b[18])
+ + MUL15(a[ 8], b[17])
+ + MUL15(a[ 9], b[16])
+ + MUL15(a[10], b[15])
+ + MUL15(a[11], b[14])
+ + MUL15(a[12], b[13])
+ + MUL15(a[13], b[12])
+ + MUL15(a[14], b[11])
+ + MUL15(a[15], b[10])
+ + MUL15(a[16], b[ 9])
+ + MUL15(a[17], b[ 8])
+ + MUL15(a[18], b[ 7])
+ + MUL15(a[19], b[ 6]);
+ t[26] = MUL15(a[ 7], b[19])
+ + MUL15(a[ 8], b[18])
+ + MUL15(a[ 9], b[17])
+ + MUL15(a[10], b[16])
+ + MUL15(a[11], b[15])
+ + MUL15(a[12], b[14])
+ + MUL15(a[13], b[13])
+ + MUL15(a[14], b[12])
+ + MUL15(a[15], b[11])
+ + MUL15(a[16], b[10])
+ + MUL15(a[17], b[ 9])
+ + MUL15(a[18], b[ 8])
+ + MUL15(a[19], b[ 7]);
+ t[27] = MUL15(a[ 8], b[19])
+ + MUL15(a[ 9], b[18])
+ + MUL15(a[10], b[17])
+ + MUL15(a[11], b[16])
+ + MUL15(a[12], b[15])
+ + MUL15(a[13], b[14])
+ + MUL15(a[14], b[13])
+ + MUL15(a[15], b[12])
+ + MUL15(a[16], b[11])
+ + MUL15(a[17], b[10])
+ + MUL15(a[18], b[ 9])
+ + MUL15(a[19], b[ 8]);
+ t[28] = MUL15(a[ 9], b[19])
+ + MUL15(a[10], b[18])
+ + MUL15(a[11], b[17])
+ + MUL15(a[12], b[16])
+ + MUL15(a[13], b[15])
+ + MUL15(a[14], b[14])
+ + MUL15(a[15], b[13])
+ + MUL15(a[16], b[12])
+ + MUL15(a[17], b[11])
+ + MUL15(a[18], b[10])
+ + MUL15(a[19], b[ 9]);
+ t[29] = MUL15(a[10], b[19])
+ + MUL15(a[11], b[18])
+ + MUL15(a[12], b[17])
+ + MUL15(a[13], b[16])
+ + MUL15(a[14], b[15])
+ + MUL15(a[15], b[14])
+ + MUL15(a[16], b[13])
+ + MUL15(a[17], b[12])
+ + MUL15(a[18], b[11])
+ + MUL15(a[19], b[10]);
+ t[30] = MUL15(a[11], b[19])
+ + MUL15(a[12], b[18])
+ + MUL15(a[13], b[17])
+ + MUL15(a[14], b[16])
+ + MUL15(a[15], b[15])
+ + MUL15(a[16], b[14])
+ + MUL15(a[17], b[13])
+ + MUL15(a[18], b[12])
+ + MUL15(a[19], b[11]);
+ t[31] = MUL15(a[12], b[19])
+ + MUL15(a[13], b[18])
+ + MUL15(a[14], b[17])
+ + MUL15(a[15], b[16])
+ + MUL15(a[16], b[15])
+ + MUL15(a[17], b[14])
+ + MUL15(a[18], b[13])
+ + MUL15(a[19], b[12]);
+ t[32] = MUL15(a[13], b[19])
+ + MUL15(a[14], b[18])
+ + MUL15(a[15], b[17])
+ + MUL15(a[16], b[16])
+ + MUL15(a[17], b[15])
+ + MUL15(a[18], b[14])
+ + MUL15(a[19], b[13]);
+ t[33] = MUL15(a[14], b[19])
+ + MUL15(a[15], b[18])
+ + MUL15(a[16], b[17])
+ + MUL15(a[17], b[16])
+ + MUL15(a[18], b[15])
+ + MUL15(a[19], b[14]);
+ t[34] = MUL15(a[15], b[19])
+ + MUL15(a[16], b[18])
+ + MUL15(a[17], b[17])
+ + MUL15(a[18], b[16])
+ + MUL15(a[19], b[15]);
+ t[35] = MUL15(a[16], b[19])
+ + MUL15(a[17], b[18])
+ + MUL15(a[18], b[17])
+ + MUL15(a[19], b[16]);
+ t[36] = MUL15(a[17], b[19])
+ + MUL15(a[18], b[18])
+ + MUL15(a[19], b[17]);
+ t[37] = MUL15(a[18], b[19])
+ + MUL15(a[19], b[18]);
+ t[38] = MUL15(a[19], b[19]);
+ d[39] = norm13(d, t, 39);
+}
+
+static void
+square20(uint32_t *d, const uint32_t *a)
+{
+ uint32_t t[39];
+
+ t[ 0] = MUL15(a[ 0], a[ 0]);
+ t[ 1] = ((MUL15(a[ 0], a[ 1])) << 1);
+ t[ 2] = MUL15(a[ 1], a[ 1])
+ + ((MUL15(a[ 0], a[ 2])) << 1);
+ t[ 3] = ((MUL15(a[ 0], a[ 3])
+ + MUL15(a[ 1], a[ 2])) << 1);
+ t[ 4] = MUL15(a[ 2], a[ 2])
+ + ((MUL15(a[ 0], a[ 4])
+ + MUL15(a[ 1], a[ 3])) << 1);
+ t[ 5] = ((MUL15(a[ 0], a[ 5])
+ + MUL15(a[ 1], a[ 4])
+ + MUL15(a[ 2], a[ 3])) << 1);
+ t[ 6] = MUL15(a[ 3], a[ 3])
+ + ((MUL15(a[ 0], a[ 6])
+ + MUL15(a[ 1], a[ 5])
+ + MUL15(a[ 2], a[ 4])) << 1);
+ t[ 7] = ((MUL15(a[ 0], a[ 7])
+ + MUL15(a[ 1], a[ 6])
+ + MUL15(a[ 2], a[ 5])
+ + MUL15(a[ 3], a[ 4])) << 1);
+ t[ 8] = MUL15(a[ 4], a[ 4])
+ + ((MUL15(a[ 0], a[ 8])
+ + MUL15(a[ 1], a[ 7])
+ + MUL15(a[ 2], a[ 6])
+ + MUL15(a[ 3], a[ 5])) << 1);
+ t[ 9] = ((MUL15(a[ 0], a[ 9])
+ + MUL15(a[ 1], a[ 8])
+ + MUL15(a[ 2], a[ 7])
+ + MUL15(a[ 3], a[ 6])
+ + MUL15(a[ 4], a[ 5])) << 1);
+ t[10] = MUL15(a[ 5], a[ 5])
+ + ((MUL15(a[ 0], a[10])
+ + MUL15(a[ 1], a[ 9])
+ + MUL15(a[ 2], a[ 8])
+ + MUL15(a[ 3], a[ 7])
+ + MUL15(a[ 4], a[ 6])) << 1);
+ t[11] = ((MUL15(a[ 0], a[11])
+ + MUL15(a[ 1], a[10])
+ + MUL15(a[ 2], a[ 9])
+ + MUL15(a[ 3], a[ 8])
+ + MUL15(a[ 4], a[ 7])
+ + MUL15(a[ 5], a[ 6])) << 1);
+ t[12] = MUL15(a[ 6], a[ 6])
+ + ((MUL15(a[ 0], a[12])
+ + MUL15(a[ 1], a[11])
+ + MUL15(a[ 2], a[10])
+ + MUL15(a[ 3], a[ 9])
+ + MUL15(a[ 4], a[ 8])
+ + MUL15(a[ 5], a[ 7])) << 1);
+ t[13] = ((MUL15(a[ 0], a[13])
+ + MUL15(a[ 1], a[12])
+ + MUL15(a[ 2], a[11])
+ + MUL15(a[ 3], a[10])
+ + MUL15(a[ 4], a[ 9])
+ + MUL15(a[ 5], a[ 8])
+ + MUL15(a[ 6], a[ 7])) << 1);
+ t[14] = MUL15(a[ 7], a[ 7])
+ + ((MUL15(a[ 0], a[14])
+ + MUL15(a[ 1], a[13])
+ + MUL15(a[ 2], a[12])
+ + MUL15(a[ 3], a[11])
+ + MUL15(a[ 4], a[10])
+ + MUL15(a[ 5], a[ 9])
+ + MUL15(a[ 6], a[ 8])) << 1);
+ t[15] = ((MUL15(a[ 0], a[15])
+ + MUL15(a[ 1], a[14])
+ + MUL15(a[ 2], a[13])
+ + MUL15(a[ 3], a[12])
+ + MUL15(a[ 4], a[11])
+ + MUL15(a[ 5], a[10])
+ + MUL15(a[ 6], a[ 9])
+ + MUL15(a[ 7], a[ 8])) << 1);
+ t[16] = MUL15(a[ 8], a[ 8])
+ + ((MUL15(a[ 0], a[16])
+ + MUL15(a[ 1], a[15])
+ + MUL15(a[ 2], a[14])
+ + MUL15(a[ 3], a[13])
+ + MUL15(a[ 4], a[12])
+ + MUL15(a[ 5], a[11])
+ + MUL15(a[ 6], a[10])
+ + MUL15(a[ 7], a[ 9])) << 1);
+ t[17] = ((MUL15(a[ 0], a[17])
+ + MUL15(a[ 1], a[16])
+ + MUL15(a[ 2], a[15])
+ + MUL15(a[ 3], a[14])
+ + MUL15(a[ 4], a[13])
+ + MUL15(a[ 5], a[12])
+ + MUL15(a[ 6], a[11])
+ + MUL15(a[ 7], a[10])
+ + MUL15(a[ 8], a[ 9])) << 1);
+ t[18] = MUL15(a[ 9], a[ 9])
+ + ((MUL15(a[ 0], a[18])
+ + MUL15(a[ 1], a[17])
+ + MUL15(a[ 2], a[16])
+ + MUL15(a[ 3], a[15])
+ + MUL15(a[ 4], a[14])
+ + MUL15(a[ 5], a[13])
+ + MUL15(a[ 6], a[12])
+ + MUL15(a[ 7], a[11])
+ + MUL15(a[ 8], a[10])) << 1);
+ t[19] = ((MUL15(a[ 0], a[19])
+ + MUL15(a[ 1], a[18])
+ + MUL15(a[ 2], a[17])
+ + MUL15(a[ 3], a[16])
+ + MUL15(a[ 4], a[15])
+ + MUL15(a[ 5], a[14])
+ + MUL15(a[ 6], a[13])
+ + MUL15(a[ 7], a[12])
+ + MUL15(a[ 8], a[11])
+ + MUL15(a[ 9], a[10])) << 1);
+ t[20] = MUL15(a[10], a[10])
+ + ((MUL15(a[ 1], a[19])
+ + MUL15(a[ 2], a[18])
+ + MUL15(a[ 3], a[17])
+ + MUL15(a[ 4], a[16])
+ + MUL15(a[ 5], a[15])
+ + MUL15(a[ 6], a[14])
+ + MUL15(a[ 7], a[13])
+ + MUL15(a[ 8], a[12])
+ + MUL15(a[ 9], a[11])) << 1);
+ t[21] = ((MUL15(a[ 2], a[19])
+ + MUL15(a[ 3], a[18])
+ + MUL15(a[ 4], a[17])
+ + MUL15(a[ 5], a[16])
+ + MUL15(a[ 6], a[15])
+ + MUL15(a[ 7], a[14])
+ + MUL15(a[ 8], a[13])
+ + MUL15(a[ 9], a[12])
+ + MUL15(a[10], a[11])) << 1);
+ t[22] = MUL15(a[11], a[11])
+ + ((MUL15(a[ 3], a[19])
+ + MUL15(a[ 4], a[18])
+ + MUL15(a[ 5], a[17])
+ + MUL15(a[ 6], a[16])
+ + MUL15(a[ 7], a[15])
+ + MUL15(a[ 8], a[14])
+ + MUL15(a[ 9], a[13])
+ + MUL15(a[10], a[12])) << 1);
+ t[23] = ((MUL15(a[ 4], a[19])
+ + MUL15(a[ 5], a[18])
+ + MUL15(a[ 6], a[17])
+ + MUL15(a[ 7], a[16])
+ + MUL15(a[ 8], a[15])
+ + MUL15(a[ 9], a[14])
+ + MUL15(a[10], a[13])
+ + MUL15(a[11], a[12])) << 1);
+ t[24] = MUL15(a[12], a[12])
+ + ((MUL15(a[ 5], a[19])
+ + MUL15(a[ 6], a[18])
+ + MUL15(a[ 7], a[17])
+ + MUL15(a[ 8], a[16])
+ + MUL15(a[ 9], a[15])
+ + MUL15(a[10], a[14])
+ + MUL15(a[11], a[13])) << 1);
+ t[25] = ((MUL15(a[ 6], a[19])
+ + MUL15(a[ 7], a[18])
+ + MUL15(a[ 8], a[17])
+ + MUL15(a[ 9], a[16])
+ + MUL15(a[10], a[15])
+ + MUL15(a[11], a[14])
+ + MUL15(a[12], a[13])) << 1);
+ t[26] = MUL15(a[13], a[13])
+ + ((MUL15(a[ 7], a[19])
+ + MUL15(a[ 8], a[18])
+ + MUL15(a[ 9], a[17])
+ + MUL15(a[10], a[16])
+ + MUL15(a[11], a[15])
+ + MUL15(a[12], a[14])) << 1);
+ t[27] = ((MUL15(a[ 8], a[19])
+ + MUL15(a[ 9], a[18])
+ + MUL15(a[10], a[17])
+ + MUL15(a[11], a[16])
+ + MUL15(a[12], a[15])
+ + MUL15(a[13], a[14])) << 1);
+ t[28] = MUL15(a[14], a[14])
+ + ((MUL15(a[ 9], a[19])
+ + MUL15(a[10], a[18])
+ + MUL15(a[11], a[17])
+ + MUL15(a[12], a[16])
+ + MUL15(a[13], a[15])) << 1);
+ t[29] = ((MUL15(a[10], a[19])
+ + MUL15(a[11], a[18])
+ + MUL15(a[12], a[17])
+ + MUL15(a[13], a[16])
+ + MUL15(a[14], a[15])) << 1);
+ t[30] = MUL15(a[15], a[15])
+ + ((MUL15(a[11], a[19])
+ + MUL15(a[12], a[18])
+ + MUL15(a[13], a[17])
+ + MUL15(a[14], a[16])) << 1);
+ t[31] = ((MUL15(a[12], a[19])
+ + MUL15(a[13], a[18])
+ + MUL15(a[14], a[17])
+ + MUL15(a[15], a[16])) << 1);
+ t[32] = MUL15(a[16], a[16])
+ + ((MUL15(a[13], a[19])
+ + MUL15(a[14], a[18])
+ + MUL15(a[15], a[17])) << 1);
+ t[33] = ((MUL15(a[14], a[19])
+ + MUL15(a[15], a[18])
+ + MUL15(a[16], a[17])) << 1);
+ t[34] = MUL15(a[17], a[17])
+ + ((MUL15(a[15], a[19])
+ + MUL15(a[16], a[18])) << 1);
+ t[35] = ((MUL15(a[16], a[19])
+ + MUL15(a[17], a[18])) << 1);
+ t[36] = MUL15(a[18], a[18])
+ + ((MUL15(a[17], a[19])) << 1);
+ t[37] = ((MUL15(a[18], a[19])) << 1);
+ t[38] = MUL15(a[19], a[19]);
+ d[39] = norm13(d, t, 39);
+}
+
+#endif
+
+/*
+ * Modulus for field F256 (field for point coordinates in curve P-256).
+ */
+static const uint32_t F256[] = {
+ 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x001F,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0400, 0x0000,
+ 0x0000, 0x1FF8, 0x1FFF, 0x01FF
+};
+
+/*
+ * The 'b' curve equation coefficient for P-256.
+ */
+static const uint32_t P256_B[] = {
+ 0x004B, 0x1E93, 0x0F89, 0x1C78, 0x03BC, 0x187B, 0x114E, 0x1619,
+ 0x1D06, 0x0328, 0x01AF, 0x0D31, 0x1557, 0x15DE, 0x1ECF, 0x127C,
+ 0x0A3A, 0x0EC5, 0x118D, 0x00B5
+};
+
+/*
+ * Perform a "short reduction" in field F256 (field for curve P-256).
+ * The source value should be less than 262 bits; on output, it will
+ * be at most 257 bits, and less than twice the modulus.
+ */
+static void
+reduce_f256(uint32_t *d)
+{
+ uint32_t x;
+
+ x = d[19] >> 9;
+ d[19] &= 0x01FF;
+ d[17] += x << 3;
+ d[14] -= x << 10;
+ d[7] -= x << 5;
+ d[0] += x;
+ norm13(d, d, 20);
+}
+
+/*
+ * Perform a "final reduction" in field F256 (field for curve P-256).
+ * The source value must be less than twice the modulus. If the value
+ * is not lower than the modulus, then the modulus is subtracted and
+ * this function returns 1; otherwise, it leaves it untouched and it
+ * returns 0.
+ */
+static uint32_t
+reduce_final_f256(uint32_t *d)
+{
+ uint32_t t[20];
+ uint32_t cc;
+ int i;
+
+ memcpy(t, d, sizeof t);
+ cc = 0;
+ for (i = 0; i < 20; i ++) {
+ uint32_t w;
+
+ w = t[i] - F256[i] - cc;
+ cc = w >> 31;
+ t[i] = w & 0x1FFF;
+ }
+ cc ^= 1;
+ CCOPY(cc, d, t, sizeof t);
+ return cc;
+}
+
+/*
+ * Perform a multiplication of two integers modulo
+ * 2^256-2^224+2^192+2^96-1 (for NIST curve P-256). Operands are arrays
+ * of 20 words, each containing 13 bits of data, in little-endian order.
+ * On input, upper word may be up to 13 bits (hence value up to 2^260-1);
+ * on output, value fits on 257 bits and is lower than twice the modulus.
+ */
+static void
+mul_f256(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[40], cc;
+ int i;
+
+ /*
+ * Compute raw multiplication. All result words fit in 13 bits
+ * each.
+ */
+ mul20(t, a, b);
+
+ /*
+ * Modular reduction: each high word in added/subtracted where
+ * necessary.
+ *
+ * The modulus is:
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1
+ * Therefore:
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p
+ *
+ * For a word x at bit offset n (n >= 256), we have:
+ * x*2^n = x*2^(n-32) - x*2^(n-64)
+ * - x*2^(n - 160) + x*2^(n-256) mod p
+ *
+ * Thus, we can nullify the high word if we reinject it at some
+ * proper emplacements.
+ */
+ for (i = 39; i >= 20; i --) {
+ uint32_t x;
+
+ x = t[i];
+ t[i - 2] += ARSH(x, 6);
+ t[i - 3] += (x << 7) & 0x1FFF;
+ t[i - 4] -= ARSH(x, 12);
+ t[i - 5] -= (x << 1) & 0x1FFF;
+ t[i - 12] -= ARSH(x, 4);
+ t[i - 13] -= (x << 9) & 0x1FFF;
+ t[i - 19] += ARSH(x, 9);
+ t[i - 20] += (x << 4) & 0x1FFF;
+ }
+
+ /*
+ * Propagate carries. This is a signed propagation, and the
+ * result may be negative. The loop above may enlarge values,
+ * but not two much: worst case is the chain involving t[i - 3],
+ * in which a value may be added to itself up to 7 times. Since
+ * starting values are 13-bit each, all words fit on 20 bits
+ * (21 to account for the sign bit).
+ */
+ cc = norm13(t, t, 20);
+
+ /*
+ * Perform modular reduction again for the bits beyond 256 (the carry
+ * and the bits 256..259). Since the largest shift below is by 10
+ * bits, and the values fit on 21 bits, values fit in 32-bit words,
+ * thereby allowing injecting full word values.
+ */
+ cc = (cc << 4) | (t[19] >> 9);
+ t[19] &= 0x01FF;
+ t[17] += cc << 3;
+ t[14] -= cc << 10;
+ t[7] -= cc << 5;
+ t[0] += cc;
+
+ /*
+ * If the carry is negative, then after carry propagation, we may
+ * end up with a value which is negative, and we don't want that.
+ * Thus, in that case, we add the modulus. Note that the subtraction
+ * result, when the carry is negative, is always smaller than the
+ * modulus, so the extra addition will not make the value exceed
+ * twice the modulus.
+ */
+ cc >>= 31;
+ t[0] -= cc;
+ t[7] += cc << 5;
+ t[14] += cc << 10;
+ t[17] -= cc << 3;
+ t[19] += cc << 9;
+
+ norm13(d, t, 20);
+}
+
+/*
+ * Square an integer modulo 2^256-2^224+2^192+2^96-1 (for NIST curve
+ * P-256). Operand is an array of 20 words, each containing 13 bits of
+ * data, in little-endian order. On input, upper word may be up to 13
+ * bits (hence value up to 2^260-1); on output, value fits on 257 bits
+ * and is lower than twice the modulus.
+ */
+static void
+square_f256(uint32_t *d, const uint32_t *a)
+{
+ uint32_t t[40], cc;
+ int i;
+
+ /*
+ * Compute raw square. All result words fit in 13 bits each.
+ */
+ square20(t, a);
+
+ /*
+ * Modular reduction: each high word in added/subtracted where
+ * necessary.
+ *
+ * The modulus is:
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1
+ * Therefore:
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p
+ *
+ * For a word x at bit offset n (n >= 256), we have:
+ * x*2^n = x*2^(n-32) - x*2^(n-64)
+ * - x*2^(n - 160) + x*2^(n-256) mod p
+ *
+ * Thus, we can nullify the high word if we reinject it at some
+ * proper emplacements.
+ */
+ for (i = 39; i >= 20; i --) {
+ uint32_t x;
+
+ x = t[i];
+ t[i - 2] += ARSH(x, 6);
+ t[i - 3] += (x << 7) & 0x1FFF;
+ t[i - 4] -= ARSH(x, 12);
+ t[i - 5] -= (x << 1) & 0x1FFF;
+ t[i - 12] -= ARSH(x, 4);
+ t[i - 13] -= (x << 9) & 0x1FFF;
+ t[i - 19] += ARSH(x, 9);
+ t[i - 20] += (x << 4) & 0x1FFF;
+ }
+
+ /*
+ * Propagate carries. This is a signed propagation, and the
+ * result may be negative. The loop above may enlarge values,
+ * but not two much: worst case is the chain involving t[i - 3],
+ * in which a value may be added to itself up to 7 times. Since
+ * starting values are 13-bit each, all words fit on 20 bits
+ * (21 to account for the sign bit).
+ */
+ cc = norm13(t, t, 20);
+
+ /*
+ * Perform modular reduction again for the bits beyond 256 (the carry
+ * and the bits 256..259). Since the largest shift below is by 10
+ * bits, and the values fit on 21 bits, values fit in 32-bit words,
+ * thereby allowing injecting full word values.
+ */
+ cc = (cc << 4) | (t[19] >> 9);
+ t[19] &= 0x01FF;
+ t[17] += cc << 3;
+ t[14] -= cc << 10;
+ t[7] -= cc << 5;
+ t[0] += cc;
+
+ /*
+ * If the carry is negative, then after carry propagation, we may
+ * end up with a value which is negative, and we don't want that.
+ * Thus, in that case, we add the modulus. Note that the subtraction
+ * result, when the carry is negative, is always smaller than the
+ * modulus, so the extra addition will not make the value exceed
+ * twice the modulus.
+ */
+ cc >>= 31;
+ t[0] -= cc;
+ t[7] += cc << 5;
+ t[14] += cc << 10;
+ t[17] -= cc << 3;
+ t[19] += cc << 9;
+
+ norm13(d, t, 20);
+}
+
+/*
+ * Jacobian coordinates for a point in P-256: affine coordinates (X,Y)
+ * are such that:
+ * X = x / z^2
+ * Y = y / z^3
+ * For the point at infinity, z = 0.
+ * Each point thus admits many possible representations.
+ *
+ * Coordinates are represented in arrays of 32-bit integers, each holding
+ * 13 bits of data. Values may also be slightly greater than the modulus,
+ * but they will always be lower than twice the modulus.
+ */
+typedef struct {
+ uint32_t x[20];
+ uint32_t y[20];
+ uint32_t z[20];
+} p256_jacobian;
+
+/*
+ * Convert a point to affine coordinates:
+ * - If the point is the point at infinity, then all three coordinates
+ * are set to 0.
+ * - Otherwise, the 'z' coordinate is set to 1, and the 'x' and 'y'
+ * coordinates are the 'X' and 'Y' affine coordinates.
+ * The coordinates are guaranteed to be lower than the modulus.
+ */
+static void
+p256_to_affine(p256_jacobian *P)
+{
+ uint32_t t1[20], t2[20];
+ int i;
+
+ /*
+ * Invert z with a modular exponentiation: the modulus is
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1, and the exponent is
+ * p-2. Exponent bit pattern (from high to low) is:
+ * - 32 bits of value 1
+ * - 31 bits of value 0
+ * - 1 bit of value 1
+ * - 96 bits of value 0
+ * - 94 bits of value 1
+ * - 1 bit of value 0
+ * - 1 bit of value 1
+ * Thus, we precompute z^(2^31-1) to speed things up.
+ *
+ * If z = 0 (point at infinity) then the modular exponentiation
+ * will yield 0, which leads to the expected result (all three
+ * coordinates set to 0).
+ */
+
+ /*
+ * A simple square-and-multiply for z^(2^31-1). We could save about
+ * two dozen multiplications here with an addition chain, but
+ * this would require a bit more code, and extra stack buffers.
+ */
+ memcpy(t1, P->z, sizeof P->z);
+ for (i = 0; i < 30; i ++) {
+ square_f256(t1, t1);
+ mul_f256(t1, t1, P->z);
+ }
+
+ /*
+ * Square-and-multiply. Apart from the squarings, we have a few
+ * multiplications to set bits to 1; we multiply by the original z
+ * for setting 1 bit, and by t1 for setting 31 bits.
+ */
+ memcpy(t2, P->z, sizeof P->z);
+ for (i = 1; i < 256; i ++) {
+ square_f256(t2, t2);
+ switch (i) {
+ case 31:
+ case 190:
+ case 221:
+ case 252:
+ mul_f256(t2, t2, t1);
+ break;
+ case 63:
+ case 253:
+ case 255:
+ mul_f256(t2, t2, P->z);
+ break;
+ }
+ }
+
+ /*
+ * Now that we have 1/z, multiply x by 1/z^2 and y by 1/z^3.
+ */
+ mul_f256(t1, t2, t2);
+ mul_f256(P->x, t1, P->x);
+ mul_f256(t1, t1, t2);
+ mul_f256(P->y, t1, P->y);
+ reduce_final_f256(P->x);
+ reduce_final_f256(P->y);
+
+ /*
+ * Multiply z by 1/z. If z = 0, then this will yield 0, otherwise
+ * this will set z to 1.
+ */
+ mul_f256(P->z, P->z, t2);
+ reduce_final_f256(P->z);
+}
+
+/*
+ * Double a point in P-256. This function works for all valid points,
+ * including the point at infinity.
+ */
+static void
+p256_double(p256_jacobian *Q)
+{
+ /*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * These formulas work for all points, including points of order 2
+ * and points at infinity:
+ * - If y = 0 then z' = 0. But there is no such point in P-256
+ * anyway.
+ * - If z = 0 then z' = 0.
+ */
+ uint32_t t1[20], t2[20], t3[20], t4[20];
+ int i;
+
+ /*
+ * Compute z^2 in t1.
+ */
+ square_f256(t1, Q->z);
+
+ /*
+ * Compute x-z^2 in t2 and x+z^2 in t1.
+ */
+ for (i = 0; i < 20; i ++) {
+ t2[i] = (F256[i] << 1) + Q->x[i] - t1[i];
+ t1[i] += Q->x[i];
+ }
+ norm13(t1, t1, 20);
+ norm13(t2, t2, 20);
+
+ /*
+ * Compute 3*(x+z^2)*(x-z^2) in t1.
+ */
+ mul_f256(t3, t1, t2);
+ for (i = 0; i < 20; i ++) {
+ t1[i] = MUL15(3, t3[i]);
+ }
+ norm13(t1, t1, 20);
+
+ /*
+ * Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ square_f256(t3, Q->y);
+ for (i = 0; i < 20; i ++) {
+ t3[i] <<= 1;
+ }
+ norm13(t3, t3, 20);
+ mul_f256(t2, Q->x, t3);
+ for (i = 0; i < 20; i ++) {
+ t2[i] <<= 1;
+ }
+ norm13(t2, t2, 20);
+ reduce_f256(t2);
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ square_f256(Q->x, t1);
+ for (i = 0; i < 20; i ++) {
+ Q->x[i] += (F256[i] << 2) - (t2[i] << 1);
+ }
+ norm13(Q->x, Q->x, 20);
+ reduce_f256(Q->x);
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ mul_f256(t4, Q->y, Q->z);
+ for (i = 0; i < 20; i ++) {
+ Q->z[i] = t4[i] << 1;
+ }
+ norm13(Q->z, Q->z, 20);
+ reduce_f256(Q->z);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ for (i = 0; i < 20; i ++) {
+ t2[i] += (F256[i] << 1) - Q->x[i];
+ }
+ norm13(t2, t2, 20);
+ mul_f256(Q->y, t1, t2);
+ square_f256(t4, t3);
+ for (i = 0; i < 20; i ++) {
+ Q->y[i] += (F256[i] << 2) - (t4[i] << 1);
+ }
+ norm13(Q->y, Q->y, 20);
+ reduce_f256(Q->y);
+}
+
+/*
+ * Add point P2 to point P1.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0 but P2 != 0
+ * - If P1 != 0 but P2 == 0
+ * - If P1 == P2
+ *
+ * In all three cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate
+ * - P1 == 0 and P2 == 0
+ * - The Y coordinate of one of the points is 0 and the other point is
+ * the point at infinity.
+ *
+ * The third case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ */
+static uint32_t
+p256_add(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ */
+ uint32_t t1[20], t2[20], t3[20], t4[20], t5[20], t6[20], t7[20];
+ uint32_t ret;
+ int i;
+
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ square_f256(t3, P2->z);
+ mul_f256(t1, P1->x, t3);
+ mul_f256(t4, P2->z, t3);
+ mul_f256(t3, P1->y, t4);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ square_f256(t4, P1->z);
+ mul_f256(t2, P2->x, t4);
+ mul_f256(t5, P1->z, t4);
+ mul_f256(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ for (i = 0; i < 20; i ++) {
+ t2[i] += (F256[i] << 1) - t1[i];
+ t4[i] += (F256[i] << 1) - t3[i];
+ }
+ norm13(t2, t2, 20);
+ norm13(t4, t4, 20);
+ reduce_f256(t4);
+ reduce_final_f256(t4);
+ ret = 0;
+ for (i = 0; i < 20; i ++) {
+ ret |= t4[i];
+ }
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ square_f256(t7, t2);
+ mul_f256(t6, t1, t7);
+ mul_f256(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ square_f256(P1->x, t4);
+ for (i = 0; i < 20; i ++) {
+ P1->x[i] += (F256[i] << 3) - t5[i] - (t6[i] << 1);
+ }
+ norm13(P1->x, P1->x, 20);
+ reduce_f256(P1->x);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ for (i = 0; i < 20; i ++) {
+ t6[i] += (F256[i] << 1) - P1->x[i];
+ }
+ norm13(t6, t6, 20);
+ mul_f256(P1->y, t4, t6);
+ mul_f256(t1, t5, t3);
+ for (i = 0; i < 20; i ++) {
+ P1->y[i] += (F256[i] << 1) - t1[i];
+ }
+ norm13(P1->y, P1->y, 20);
+ reduce_f256(P1->y);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ mul_f256(t1, P1->z, P2->z);
+ mul_f256(P1->z, t1, t2);
+
+ return ret;
+}
+
+/*
+ * Add point P2 to point P1. This is a specialised function for the
+ * case when P2 is a non-zero point in affine coordinate.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0
+ * - If P1 == P2
+ *
+ * In both cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate
+ * - The Y coordinate of P2 is 0 and P1 is the point at infinity.
+ *
+ * The second case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ */
+static uint32_t
+p256_add_mixed(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ */
+ uint32_t t1[20], t2[20], t3[20], t4[20], t5[20], t6[20], t7[20];
+ uint32_t ret;
+ int i;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ square_f256(t4, P1->z);
+ mul_f256(t2, P2->x, t4);
+ mul_f256(t5, P1->z, t4);
+ mul_f256(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ for (i = 0; i < 20; i ++) {
+ t2[i] += (F256[i] << 1) - t1[i];
+ t4[i] += (F256[i] << 1) - t3[i];
+ }
+ norm13(t2, t2, 20);
+ norm13(t4, t4, 20);
+ reduce_f256(t4);
+ reduce_final_f256(t4);
+ ret = 0;
+ for (i = 0; i < 20; i ++) {
+ ret |= t4[i];
+ }
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ square_f256(t7, t2);
+ mul_f256(t6, t1, t7);
+ mul_f256(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ square_f256(P1->x, t4);
+ for (i = 0; i < 20; i ++) {
+ P1->x[i] += (F256[i] << 3) - t5[i] - (t6[i] << 1);
+ }
+ norm13(P1->x, P1->x, 20);
+ reduce_f256(P1->x);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ for (i = 0; i < 20; i ++) {
+ t6[i] += (F256[i] << 1) - P1->x[i];
+ }
+ norm13(t6, t6, 20);
+ mul_f256(P1->y, t4, t6);
+ mul_f256(t1, t5, t3);
+ for (i = 0; i < 20; i ++) {
+ P1->y[i] += (F256[i] << 1) - t1[i];
+ }
+ norm13(P1->y, P1->y, 20);
+ reduce_f256(P1->y);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ mul_f256(P1->z, P1->z, t2);
+
+ return ret;
+}
+
+/*
+ * Decode a P-256 point. This function does not support the point at
+ * infinity. Returned value is 0 if the point is invalid, 1 otherwise.
+ */
+static uint32_t
+p256_decode(p256_jacobian *P, const void *src, size_t len)
+{
+ const unsigned char *buf;
+ uint32_t tx[20], ty[20], t1[20], t2[20];
+ uint32_t bad;
+ int i;
+
+ if (len != 65) {
+ return 0;
+ }
+ buf = src;
+
+ /*
+ * First byte must be 0x04 (uncompressed format). We could support
+ * "hybrid format" (first byte is 0x06 or 0x07, and encodes the
+ * least significant bit of the Y coordinate), but it is explicitly
+ * forbidden by RFC 5480 (section 2.2).
+ */
+ bad = NEQ(buf[0], 0x04);
+
+ /*
+ * Decode the coordinates, and check that they are both lower
+ * than the modulus.
+ */
+ tx[19] = be8_to_le13(tx, buf + 1, 32);
+ ty[19] = be8_to_le13(ty, buf + 33, 32);
+ bad |= reduce_final_f256(tx);
+ bad |= reduce_final_f256(ty);
+
+ /*
+ * Check curve equation.
+ */
+ square_f256(t1, tx);
+ mul_f256(t1, tx, t1);
+ square_f256(t2, ty);
+ for (i = 0; i < 20; i ++) {
+ t1[i] += (F256[i] << 3) - MUL15(3, tx[i]) + P256_B[i] - t2[i];
+ }
+ norm13(t1, t1, 20);
+ reduce_f256(t1);
+ reduce_final_f256(t1);
+ for (i = 0; i < 20; i ++) {
+ bad |= t1[i];
+ }
+
+ /*
+ * Copy coordinates to the point structure.
+ */
+ memcpy(P->x, tx, sizeof tx);
+ memcpy(P->y, ty, sizeof ty);
+ memset(P->z, 0, sizeof P->z);
+ P->z[0] = 1;
+ return EQ(bad, 0);
+}
+
+/*
+ * Encode a point into a buffer. This function assumes that the point is
+ * valid, in affine coordinates, and not the point at infinity.
+ */
+static void
+p256_encode(void *dst, const p256_jacobian *P)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = 0x04;
+ le13_to_be8(buf + 1, 32, P->x);
+ le13_to_be8(buf + 33, 32, P->y);
+}
+
+/*
+ * Multiply a curve point by an integer. The integer is assumed to be
+ * lower than the curve order, and the base point must not be the point
+ * at infinity.
+ */
+static void
+p256_mul(p256_jacobian *P, const unsigned char *x, size_t xlen)
+{
+ /*
+ * qz is a flag that is initially 1, and remains equal to 1
+ * as long as the point is the point at infinity.
+ *
+ * We use a 2-bit window to handle multiplier bits by pairs.
+ * The precomputed window really is the points P2 and P3.
+ */
+ uint32_t qz;
+ p256_jacobian P2, P3, Q, T, U;
+
+ /*
+ * Compute window values.
+ */
+ P2 = *P;
+ p256_double(&P2);
+ P3 = *P;
+ p256_add(&P3, &P2);
+
+ /*
+ * We start with Q = 0. We process multiplier bits 2 by 2.
+ */
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+
+ for (k = 6; k >= 0; k -= 2) {
+ uint32_t bits;
+ uint32_t bnz;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ T = *P;
+ U = Q;
+ bits = (*x >> k) & (uint32_t)3;
+ bnz = NEQ(bits, 0);
+ CCOPY(EQ(bits, 2), &T, &P2, sizeof T);
+ CCOPY(EQ(bits, 3), &T, &P3, sizeof T);
+ p256_add(&U, &T);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ }
+ x ++;
+ }
+ *P = Q;
+}
+
+/*
+ * Precomputed window: k*G points, where G is the curve generator, and k
+ * is an integer from 1 to 15 (inclusive). The X and Y coordinates of
+ * the point are encoded as 20 words of 13 bits each (little-endian
+ * order); 13-bit words are then grouped 2-by-2 into 32-bit words
+ * (little-endian order within each word).
+ */
+static const uint32_t Gwin[15][20] = {
+
+ { 0x04C60296, 0x02721176, 0x19D00F4A, 0x102517AC,
+ 0x13B8037D, 0x0748103C, 0x1E730E56, 0x08481FE2,
+ 0x0F97012C, 0x00D605F4, 0x1DFA11F5, 0x0C801A0D,
+ 0x0F670CBB, 0x0AED0CC5, 0x115E0E33, 0x181F0785,
+ 0x13F514A7, 0x0FF30E3B, 0x17171E1A, 0x009F18D0 },
+
+ { 0x1B341978, 0x16911F11, 0x0D9A1A60, 0x1C4E1FC8,
+ 0x1E040969, 0x096A06B0, 0x091C0030, 0x09EF1A29,
+ 0x18C40D03, 0x00F91C9E, 0x13C313D1, 0x096F0748,
+ 0x011419E0, 0x1CC713A6, 0x1DD31DAD, 0x1EE80C36,
+ 0x1ECD0C69, 0x1A0800A4, 0x08861B8E, 0x000E1DD5 },
+
+ { 0x173F1D6C, 0x02CC06F1, 0x14C21FB4, 0x043D1EB6,
+ 0x0F3606B7, 0x1A971C59, 0x1BF71951, 0x01481323,
+ 0x068D0633, 0x00BD12F9, 0x13EA1032, 0x136209E8,
+ 0x1C1E19A7, 0x06C7013E, 0x06C10AB0, 0x14C908BB,
+ 0x05830CE1, 0x1FEF18DD, 0x00620998, 0x010E0D19 },
+
+ { 0x18180852, 0x0604111A, 0x0B771509, 0x1B6F0156,
+ 0x00181FE2, 0x1DCC0AF4, 0x16EF0659, 0x11F70E80,
+ 0x11A912D0, 0x01C414D2, 0x027618C6, 0x05840FC6,
+ 0x100215C4, 0x187E0C3B, 0x12771C96, 0x150C0B5D,
+ 0x0FF705FD, 0x07981C67, 0x1AD20C63, 0x01C11C55 },
+
+ { 0x1E8113ED, 0x0A940370, 0x12920215, 0x1FA31D6F,
+ 0x1F7C0C82, 0x10CD03F7, 0x02640560, 0x081A0B5E,
+ 0x1BD21151, 0x00A21642, 0x0D0B0DA4, 0x0176113F,
+ 0x04440D1D, 0x001A1360, 0x1068012F, 0x1F141E49,
+ 0x10DF136B, 0x0E4F162B, 0x0D44104A, 0x01C1105F },
+
+ { 0x011411A9, 0x01551A4F, 0x0ADA0C6B, 0x01BD0EC8,
+ 0x18120C74, 0x112F1778, 0x099202CB, 0x0C05124B,
+ 0x195316A4, 0x01600685, 0x1E3B1FE2, 0x189014E3,
+ 0x0B5E1FD7, 0x0E0311F8, 0x08E000F7, 0x174E00DE,
+ 0x160702DF, 0x1B5A15BF, 0x03A11237, 0x01D01704 },
+
+ { 0x0C3D12A3, 0x0C501C0C, 0x17AD1300, 0x1715003F,
+ 0x03F719F8, 0x18031ED8, 0x1D980667, 0x0F681896,
+ 0x1B7D00BF, 0x011C14CE, 0x0FA000B4, 0x1C3501B0,
+ 0x0D901C55, 0x06790C10, 0x029E0736, 0x0DEB0400,
+ 0x034F183A, 0x030619B4, 0x0DEF0033, 0x00E71AC7 },
+
+ { 0x1B7D1393, 0x1B3B1076, 0x0BED1B4D, 0x13011F3A,
+ 0x0E0E1238, 0x156A132B, 0x013A02D3, 0x160A0D01,
+ 0x1CED1EE9, 0x00C5165D, 0x184C157E, 0x08141A83,
+ 0x153C0DA5, 0x1ED70F9D, 0x05170D51, 0x02CF13B8,
+ 0x18AE1771, 0x1B04113F, 0x05EC11E9, 0x015A16B3 },
+
+ { 0x04A41EE0, 0x1D1412E4, 0x1C591D79, 0x118511B7,
+ 0x14F00ACB, 0x1AE31E1C, 0x049C0D51, 0x016E061E,
+ 0x1DB71EDF, 0x01D41A35, 0x0E8208FA, 0x14441293,
+ 0x011F1E85, 0x1D54137A, 0x026B114F, 0x151D0832,
+ 0x00A50964, 0x1F9C1E1C, 0x064B12C9, 0x005409D1 },
+
+ { 0x062B123F, 0x0C0D0501, 0x183704C3, 0x08E31120,
+ 0x0A2E0A6C, 0x14440FED, 0x090A0D1E, 0x13271964,
+ 0x0B590A3A, 0x019D1D9B, 0x05780773, 0x09770A91,
+ 0x0F770CA3, 0x053F19D4, 0x02C80DED, 0x1A761304,
+ 0x091E0DD9, 0x15D201B8, 0x151109AA, 0x010F0198 },
+
+ { 0x05E101D1, 0x072314DD, 0x045F1433, 0x1A041541,
+ 0x10B3142E, 0x01840736, 0x1C1B19DB, 0x098B0418,
+ 0x1DBC083B, 0x007D1444, 0x01511740, 0x11DD1F3A,
+ 0x04ED0E2F, 0x1B4B1A62, 0x10480D04, 0x09E911A2,
+ 0x04211AFA, 0x19140893, 0x04D60CC4, 0x01210648 },
+
+ { 0x112703C4, 0x018B1BA1, 0x164C1D50, 0x05160BE0,
+ 0x0BCC1830, 0x01CB1554, 0x13291732, 0x1B2B1918,
+ 0x0DED0817, 0x00E80775, 0x0A2401D3, 0x0BFE08B3,
+ 0x0E531199, 0x058616E9, 0x04770B91, 0x110F0C55,
+ 0x19C11554, 0x0BFB1159, 0x03541C38, 0x000E1C2D },
+
+ { 0x10390C01, 0x02BB0751, 0x0AC5098E, 0x096C17AB,
+ 0x03C90E28, 0x10BD18BF, 0x002E1F2D, 0x092B0986,
+ 0x1BD700AC, 0x002E1F20, 0x1E3D1FD8, 0x077718BB,
+ 0x06F919C4, 0x187407ED, 0x11370E14, 0x081E139C,
+ 0x00481ADB, 0x14AB0289, 0x066A0EBE, 0x00C70ED6 },
+
+ { 0x0694120B, 0x124E1CC9, 0x0E2F0570, 0x17CF081A,
+ 0x078906AC, 0x066D17CF, 0x1B3207F4, 0x0C5705E9,
+ 0x10001C38, 0x00A919DE, 0x06851375, 0x0F900BD8,
+ 0x080401BA, 0x0EEE0D42, 0x1B8B11EA, 0x0B4519F0,
+ 0x090F18C0, 0x062E1508, 0x0DD909F4, 0x01EB067C },
+
+ { 0x0CDC1D5F, 0x0D1818F9, 0x07781636, 0x125B18E8,
+ 0x0D7003AF, 0x13110099, 0x1D9B1899, 0x175C1EB7,
+ 0x0E34171A, 0x01E01153, 0x081A0F36, 0x0B391783,
+ 0x1D1F147E, 0x19CE16D7, 0x11511B21, 0x1F2C10F9,
+ 0x12CA0E51, 0x05A31D39, 0x171A192E, 0x016B0E4F }
+};
+
+/*
+ * Lookup one of the Gwin[] values, by index. This is constant-time.
+ */
+static void
+lookup_Gwin(p256_jacobian *T, uint32_t idx)
+{
+ uint32_t xy[20];
+ uint32_t k;
+ size_t u;
+
+ memset(xy, 0, sizeof xy);
+ for (k = 0; k < 15; k ++) {
+ uint32_t m;
+
+ m = -EQ(idx, k + 1);
+ for (u = 0; u < 20; u ++) {
+ xy[u] |= m & Gwin[k][u];
+ }
+ }
+ for (u = 0; u < 10; u ++) {
+ T->x[(u << 1) + 0] = xy[u] & 0xFFFF;
+ T->x[(u << 1) + 1] = xy[u] >> 16;
+ T->y[(u << 1) + 0] = xy[u + 10] & 0xFFFF;
+ T->y[(u << 1) + 1] = xy[u + 10] >> 16;
+ }
+ memset(T->z, 0, sizeof T->z);
+ T->z[0] = 1;
+}
+
+/*
+ * Multiply the generator by an integer. The integer is assumed non-zero
+ * and lower than the curve order.
+ */
+static void
+p256_mulgen(p256_jacobian *P, const unsigned char *x, size_t xlen)
+{
+ /*
+ * qz is a flag that is initially 1, and remains equal to 1
+ * as long as the point is the point at infinity.
+ *
+ * We use a 4-bit window to handle multiplier bits by groups
+ * of 4. The precomputed window is constant static data, with
+ * points in affine coordinates; we use a constant-time lookup.
+ */
+ p256_jacobian Q;
+ uint32_t qz;
+
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+ unsigned bx;
+
+ bx = *x ++;
+ for (k = 0; k < 2; k ++) {
+ uint32_t bits;
+ uint32_t bnz;
+ p256_jacobian T, U;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ bits = (bx >> 4) & 0x0F;
+ bnz = NEQ(bits, 0);
+ lookup_Gwin(&T, bits);
+ U = Q;
+ p256_add_mixed(&U, &T);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ bx <<= 4;
+ }
+ }
+ *P = Q;
+}
+
+static const unsigned char P256_G[] = {
+ 0x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,
+ 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,
+ 0x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,
+ 0x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,
+ 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,
+ 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,
+ 0x68, 0x37, 0xBF, 0x51, 0xF5
+};
+
+static const unsigned char P256_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,
+ 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,
+ 0x25, 0x51
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_G;
+ return P256_G;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_N;
+ return P256_N;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 1;
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ uint32_t r;
+ p256_jacobian P;
+
+ (void)curve;
+ r = p256_decode(&P, G, Glen);
+ p256_mul(&P, x, xlen);
+ if (Glen >= 65) {
+ p256_to_affine(&P);
+ p256_encode(G, &P);
+ }
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ p256_jacobian P;
+
+ (void)curve;
+ p256_mulgen(&P, x, xlen);
+ p256_to_affine(&P);
+ p256_encode(R, &P);
+ return 65;
+
+ /*
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+ */
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ p256_jacobian P, Q;
+ uint32_t r, t, z;
+ int i;
+
+ (void)curve;
+ r = p256_decode(&P, A, len);
+ p256_mul(&P, x, xlen);
+ if (B == NULL) {
+ p256_mulgen(&Q, y, ylen);
+ } else {
+ r &= p256_decode(&Q, B, len);
+ p256_mul(&Q, y, ylen);
+ }
+
+ /*
+ * The final addition may fail in case both points are equal.
+ */
+ t = p256_add(&P, &Q);
+ reduce_final_f256(P.z);
+ z = 0;
+ for (i = 0; i < 20; i ++) {
+ z |= P.z[i];
+ }
+ z = EQ(z, 0);
+ p256_double(&Q);
+
+ /*
+ * If z is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * z = 0, t = 0 return P (normal addition)
+ * z = 0, t = 1 return P (normal addition)
+ * z = 1, t = 0 return Q (a 'double' case)
+ * z = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(z & ~t, &P, &Q, sizeof Q);
+ p256_to_affine(&P);
+ p256_encode(A, &P);
+ r &= ~(z & t);
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_p256_m15 = {
+ (uint32_t)0x00800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_p256_m31.c b/test/monniaux/BearSSL/src/ec/ec_p256_m31.c
new file mode 100644
index 00000000..d57ef7b0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_p256_m31.c
@@ -0,0 +1,1475 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * If BR_NO_ARITH_SHIFT is undefined, or defined to 0, then we _assume_
+ * that right-shifting a signed negative integer copies the sign bit
+ * (arithmetic right-shift). This is "implementation-defined behaviour",
+ * i.e. it is not undefined, but it may differ between compilers. Each
+ * compiler is supposed to document its behaviour in that respect. GCC
+ * explicitly defines that an arithmetic right shift is used. We expect
+ * all other compilers to do the same, because underlying CPU offer an
+ * arithmetic right shift opcode that could not be used otherwise.
+ */
+#if BR_NO_ARITH_SHIFT
+#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
+ | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
+#define ARSHW(x, n) (((uint64_t)(x) >> (n)) \
+ | ((-((uint64_t)(x) >> 63)) << (64 - (n))))
+#else
+#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
+#define ARSHW(x, n) ((*(int64_t *)&(x)) >> (n))
+#endif
+
+/*
+ * Convert an integer from unsigned big-endian encoding to a sequence of
+ * 30-bit words in little-endian order. The final "partial" word is
+ * returned.
+ */
+static uint32_t
+be8_to_le30(uint32_t *dst, const unsigned char *src, size_t len)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ uint32_t b;
+
+ b = src[len];
+ if (acc_len < 22) {
+ acc |= b << acc_len;
+ acc_len += 8;
+ } else {
+ *dst ++ = (acc | (b << acc_len)) & 0x3FFFFFFF;
+ acc = b >> (30 - acc_len);
+ acc_len -= 22;
+ }
+ }
+ return acc;
+}
+
+/*
+ * Convert an integer (30-bit words, little-endian) to unsigned
+ * big-endian encoding. The total encoding length is provided; all
+ * the destination bytes will be filled.
+ */
+static void
+le30_to_be8(unsigned char *dst, size_t len, const uint32_t *src)
+{
+ uint32_t acc;
+ int acc_len;
+
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ if (acc_len < 8) {
+ uint32_t w;
+
+ w = *src ++;
+ dst[len] = (unsigned char)(acc | (w << acc_len));
+ acc = w >> (8 - acc_len);
+ acc_len += 22;
+ } else {
+ dst[len] = (unsigned char)acc;
+ acc >>= 8;
+ acc_len -= 8;
+ }
+ }
+}
+
+/*
+ * Multiply two integers. Source integers are represented as arrays of
+ * nine 30-bit words, for values up to 2^270-1. Result is encoded over
+ * 18 words of 30 bits each.
+ */
+static void
+mul9(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ /*
+ * Maximum intermediate result is no more than
+ * 10376293531797946367, which fits in 64 bits. Reason:
+ *
+ * 10376293531797946367 = 9 * (2^30-1)^2 + 9663676406
+ * 10376293531797946367 < 9663676407 * 2^30
+ *
+ * Thus, adding together 9 products of 30-bit integers, with
+ * a carry of at most 9663676406, yields an integer that fits
+ * on 64 bits and generates a carry of at most 9663676406.
+ */
+ uint64_t t[17];
+ uint64_t cc;
+ int i;
+
+ t[ 0] = MUL31(a[0], b[0]);
+ t[ 1] = MUL31(a[0], b[1])
+ + MUL31(a[1], b[0]);
+ t[ 2] = MUL31(a[0], b[2])
+ + MUL31(a[1], b[1])
+ + MUL31(a[2], b[0]);
+ t[ 3] = MUL31(a[0], b[3])
+ + MUL31(a[1], b[2])
+ + MUL31(a[2], b[1])
+ + MUL31(a[3], b[0]);
+ t[ 4] = MUL31(a[0], b[4])
+ + MUL31(a[1], b[3])
+ + MUL31(a[2], b[2])
+ + MUL31(a[3], b[1])
+ + MUL31(a[4], b[0]);
+ t[ 5] = MUL31(a[0], b[5])
+ + MUL31(a[1], b[4])
+ + MUL31(a[2], b[3])
+ + MUL31(a[3], b[2])
+ + MUL31(a[4], b[1])
+ + MUL31(a[5], b[0]);
+ t[ 6] = MUL31(a[0], b[6])
+ + MUL31(a[1], b[5])
+ + MUL31(a[2], b[4])
+ + MUL31(a[3], b[3])
+ + MUL31(a[4], b[2])
+ + MUL31(a[5], b[1])
+ + MUL31(a[6], b[0]);
+ t[ 7] = MUL31(a[0], b[7])
+ + MUL31(a[1], b[6])
+ + MUL31(a[2], b[5])
+ + MUL31(a[3], b[4])
+ + MUL31(a[4], b[3])
+ + MUL31(a[5], b[2])
+ + MUL31(a[6], b[1])
+ + MUL31(a[7], b[0]);
+ t[ 8] = MUL31(a[0], b[8])
+ + MUL31(a[1], b[7])
+ + MUL31(a[2], b[6])
+ + MUL31(a[3], b[5])
+ + MUL31(a[4], b[4])
+ + MUL31(a[5], b[3])
+ + MUL31(a[6], b[2])
+ + MUL31(a[7], b[1])
+ + MUL31(a[8], b[0]);
+ t[ 9] = MUL31(a[1], b[8])
+ + MUL31(a[2], b[7])
+ + MUL31(a[3], b[6])
+ + MUL31(a[4], b[5])
+ + MUL31(a[5], b[4])
+ + MUL31(a[6], b[3])
+ + MUL31(a[7], b[2])
+ + MUL31(a[8], b[1]);
+ t[10] = MUL31(a[2], b[8])
+ + MUL31(a[3], b[7])
+ + MUL31(a[4], b[6])
+ + MUL31(a[5], b[5])
+ + MUL31(a[6], b[4])
+ + MUL31(a[7], b[3])
+ + MUL31(a[8], b[2]);
+ t[11] = MUL31(a[3], b[8])
+ + MUL31(a[4], b[7])
+ + MUL31(a[5], b[6])
+ + MUL31(a[6], b[5])
+ + MUL31(a[7], b[4])
+ + MUL31(a[8], b[3]);
+ t[12] = MUL31(a[4], b[8])
+ + MUL31(a[5], b[7])
+ + MUL31(a[6], b[6])
+ + MUL31(a[7], b[5])
+ + MUL31(a[8], b[4]);
+ t[13] = MUL31(a[5], b[8])
+ + MUL31(a[6], b[7])
+ + MUL31(a[7], b[6])
+ + MUL31(a[8], b[5]);
+ t[14] = MUL31(a[6], b[8])
+ + MUL31(a[7], b[7])
+ + MUL31(a[8], b[6]);
+ t[15] = MUL31(a[7], b[8])
+ + MUL31(a[8], b[7]);
+ t[16] = MUL31(a[8], b[8]);
+
+ /*
+ * Propagate carries.
+ */
+ cc = 0;
+ for (i = 0; i < 17; i ++) {
+ uint64_t w;
+
+ w = t[i] + cc;
+ d[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ d[17] = (uint32_t)cc;
+}
+
+/*
+ * Square a 270-bit integer, represented as an array of nine 30-bit words.
+ * Result uses 18 words of 30 bits each.
+ */
+static void
+square9(uint32_t *d, const uint32_t *a)
+{
+ uint64_t t[17];
+ uint64_t cc;
+ int i;
+
+ t[ 0] = MUL31(a[0], a[0]);
+ t[ 1] = ((MUL31(a[0], a[1])) << 1);
+ t[ 2] = MUL31(a[1], a[1])
+ + ((MUL31(a[0], a[2])) << 1);
+ t[ 3] = ((MUL31(a[0], a[3])
+ + MUL31(a[1], a[2])) << 1);
+ t[ 4] = MUL31(a[2], a[2])
+ + ((MUL31(a[0], a[4])
+ + MUL31(a[1], a[3])) << 1);
+ t[ 5] = ((MUL31(a[0], a[5])
+ + MUL31(a[1], a[4])
+ + MUL31(a[2], a[3])) << 1);
+ t[ 6] = MUL31(a[3], a[3])
+ + ((MUL31(a[0], a[6])
+ + MUL31(a[1], a[5])
+ + MUL31(a[2], a[4])) << 1);
+ t[ 7] = ((MUL31(a[0], a[7])
+ + MUL31(a[1], a[6])
+ + MUL31(a[2], a[5])
+ + MUL31(a[3], a[4])) << 1);
+ t[ 8] = MUL31(a[4], a[4])
+ + ((MUL31(a[0], a[8])
+ + MUL31(a[1], a[7])
+ + MUL31(a[2], a[6])
+ + MUL31(a[3], a[5])) << 1);
+ t[ 9] = ((MUL31(a[1], a[8])
+ + MUL31(a[2], a[7])
+ + MUL31(a[3], a[6])
+ + MUL31(a[4], a[5])) << 1);
+ t[10] = MUL31(a[5], a[5])
+ + ((MUL31(a[2], a[8])
+ + MUL31(a[3], a[7])
+ + MUL31(a[4], a[6])) << 1);
+ t[11] = ((MUL31(a[3], a[8])
+ + MUL31(a[4], a[7])
+ + MUL31(a[5], a[6])) << 1);
+ t[12] = MUL31(a[6], a[6])
+ + ((MUL31(a[4], a[8])
+ + MUL31(a[5], a[7])) << 1);
+ t[13] = ((MUL31(a[5], a[8])
+ + MUL31(a[6], a[7])) << 1);
+ t[14] = MUL31(a[7], a[7])
+ + ((MUL31(a[6], a[8])) << 1);
+ t[15] = ((MUL31(a[7], a[8])) << 1);
+ t[16] = MUL31(a[8], a[8]);
+
+ /*
+ * Propagate carries.
+ */
+ cc = 0;
+ for (i = 0; i < 17; i ++) {
+ uint64_t w;
+
+ w = t[i] + cc;
+ d[i] = (uint32_t)w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ d[17] = (uint32_t)cc;
+}
+
+/*
+ * Base field modulus for P-256.
+ */
+static const uint32_t F256[] = {
+
+ 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x0000003F, 0x00000000,
+ 0x00000000, 0x00001000, 0x3FFFC000, 0x0000FFFF
+};
+
+/*
+ * The 'b' curve equation coefficient for P-256.
+ */
+static const uint32_t P256_B[] = {
+
+ 0x27D2604B, 0x2F38F0F8, 0x053B0F63, 0x0741AC33, 0x1886BC65,
+ 0x2EF555DA, 0x293E7B3E, 0x0D762A8E, 0x00005AC6
+};
+
+/*
+ * Addition in the field. Source operands shall fit on 257 bits; output
+ * will be lower than twice the modulus.
+ */
+static void
+add_f256(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t w, cc;
+ int i;
+
+ cc = 0;
+ for (i = 0; i < 9; i ++) {
+ w = a[i] + b[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = w >> 30;
+ }
+ w >>= 16;
+ d[8] &= 0xFFFF;
+ d[3] -= w << 6;
+ d[6] -= w << 12;
+ d[7] += w << 14;
+ cc = w;
+ for (i = 0; i < 9; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = ARSH(w, 30);
+ }
+}
+
+/*
+ * Subtraction in the field. Source operands shall be smaller than twice
+ * the modulus; the result will fulfil the same property.
+ */
+static void
+sub_f256(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t w, cc;
+ int i;
+
+ /*
+ * We really compute a - b + 2*p to make sure that the result is
+ * positive.
+ */
+ w = a[0] - b[0] - 0x00002;
+ d[0] = w & 0x3FFFFFFF;
+ w = a[1] - b[1] + ARSH(w, 30);
+ d[1] = w & 0x3FFFFFFF;
+ w = a[2] - b[2] + ARSH(w, 30);
+ d[2] = w & 0x3FFFFFFF;
+ w = a[3] - b[3] + ARSH(w, 30) + 0x00080;
+ d[3] = w & 0x3FFFFFFF;
+ w = a[4] - b[4] + ARSH(w, 30);
+ d[4] = w & 0x3FFFFFFF;
+ w = a[5] - b[5] + ARSH(w, 30);
+ d[5] = w & 0x3FFFFFFF;
+ w = a[6] - b[6] + ARSH(w, 30) + 0x02000;
+ d[6] = w & 0x3FFFFFFF;
+ w = a[7] - b[7] + ARSH(w, 30) - 0x08000;
+ d[7] = w & 0x3FFFFFFF;
+ w = a[8] - b[8] + ARSH(w, 30) + 0x20000;
+ d[8] = w & 0xFFFF;
+ w >>= 16;
+ d[8] &= 0xFFFF;
+ d[3] -= w << 6;
+ d[6] -= w << 12;
+ d[7] += w << 14;
+ cc = w;
+ for (i = 0; i < 9; i ++) {
+ w = d[i] + cc;
+ d[i] = w & 0x3FFFFFFF;
+ cc = ARSH(w, 30);
+ }
+}
+
+/*
+ * Compute a multiplication in F256. Source operands shall be less than
+ * twice the modulus.
+ */
+static void
+mul_f256(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ uint32_t t[18];
+ uint64_t s[18];
+ uint64_t cc, x;
+ uint32_t z, c;
+ int i;
+
+ mul9(t, a, b);
+
+ /*
+ * Modular reduction: each high word in added/subtracted where
+ * necessary.
+ *
+ * The modulus is:
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1
+ * Therefore:
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p
+ *
+ * For a word x at bit offset n (n >= 256), we have:
+ * x*2^n = x*2^(n-32) - x*2^(n-64)
+ * - x*2^(n - 160) + x*2^(n-256) mod p
+ *
+ * Thus, we can nullify the high word if we reinject it at some
+ * proper emplacements.
+ *
+ * We use 64-bit intermediate words to allow for carries to
+ * accumulate easily, before performing the final propagation.
+ */
+ for (i = 0; i < 18; i ++) {
+ s[i] = t[i];
+ }
+
+ for (i = 17; i >= 9; i --) {
+ uint64_t y;
+
+ y = s[i];
+ s[i - 1] += ARSHW(y, 2);
+ s[i - 2] += (y << 28) & 0x3FFFFFFF;
+ s[i - 2] -= ARSHW(y, 4);
+ s[i - 3] -= (y << 26) & 0x3FFFFFFF;
+ s[i - 5] -= ARSHW(y, 10);
+ s[i - 6] -= (y << 20) & 0x3FFFFFFF;
+ s[i - 8] += ARSHW(y, 16);
+ s[i - 9] += (y << 14) & 0x3FFFFFFF;
+ }
+
+ /*
+ * Carry propagation must be signed. Moreover, we may have overdone
+ * it a bit, and obtain a negative result.
+ *
+ * The loop above ran 9 times; each time, each word was augmented
+ * by at most one extra word (in absolute value). Thus, the top
+ * word must in fine fit in 39 bits, so the carry below will fit
+ * on 9 bits.
+ */
+ cc = 0;
+ for (i = 0; i < 9; i ++) {
+ x = s[i] + cc;
+ d[i] = (uint32_t)x & 0x3FFFFFFF;
+ cc = ARSHW(x, 30);
+ }
+
+ /*
+ * All nine words fit on 30 bits, but there may be an extra
+ * carry for a few bits (at most 9), and that carry may be
+ * negative. Moreover, we want the result to fit on 257 bits.
+ * The two lines below ensure that the word in d[] has length
+ * 256 bits, and the (signed) carry (beyond 2^256) is in cc. The
+ * significant length of cc is less than 24 bits, so we will be
+ * able to switch to 32-bit operations.
+ */
+ cc = ARSHW(x, 16);
+ d[8] &= 0xFFFF;
+
+ /*
+ * One extra round of reduction, for cc*2^256, which means
+ * adding cc*(2^224-2^192-2^96+1) to a 256-bit (nonnegative)
+ * value. If cc is negative, then it may happen (rarely, but
+ * not neglectibly so) that the result would be negative. In
+ * order to avoid that, if cc is negative, then we add the
+ * modulus once. Note that if cc is negative, then propagating
+ * that carry must yield a value lower than the modulus, so
+ * adding the modulus once will keep the final result under
+ * twice the modulus.
+ */
+ z = (uint32_t)cc;
+ d[3] -= z << 6;
+ d[6] -= (z << 12) & 0x3FFFFFFF;
+ d[7] -= ARSH(z, 18);
+ d[7] += (z << 14) & 0x3FFFFFFF;
+ d[8] += ARSH(z, 16);
+ c = z >> 31;
+ d[0] -= c;
+ d[3] += c << 6;
+ d[6] += c << 12;
+ d[7] -= c << 14;
+ d[8] += c << 16;
+ for (i = 0; i < 9; i ++) {
+ uint32_t w;
+
+ w = d[i] + z;
+ d[i] = w & 0x3FFFFFFF;
+ z = ARSH(w, 30);
+ }
+}
+
+/*
+ * Compute a square in F256. Source operand shall be less than
+ * twice the modulus.
+ */
+static void
+square_f256(uint32_t *d, const uint32_t *a)
+{
+ uint32_t t[18];
+ uint64_t s[18];
+ uint64_t cc, x;
+ uint32_t z, c;
+ int i;
+
+ square9(t, a);
+
+ /*
+ * Modular reduction: each high word in added/subtracted where
+ * necessary.
+ *
+ * The modulus is:
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1
+ * Therefore:
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p
+ *
+ * For a word x at bit offset n (n >= 256), we have:
+ * x*2^n = x*2^(n-32) - x*2^(n-64)
+ * - x*2^(n - 160) + x*2^(n-256) mod p
+ *
+ * Thus, we can nullify the high word if we reinject it at some
+ * proper emplacements.
+ *
+ * We use 64-bit intermediate words to allow for carries to
+ * accumulate easily, before performing the final propagation.
+ */
+ for (i = 0; i < 18; i ++) {
+ s[i] = t[i];
+ }
+
+ for (i = 17; i >= 9; i --) {
+ uint64_t y;
+
+ y = s[i];
+ s[i - 1] += ARSHW(y, 2);
+ s[i - 2] += (y << 28) & 0x3FFFFFFF;
+ s[i - 2] -= ARSHW(y, 4);
+ s[i - 3] -= (y << 26) & 0x3FFFFFFF;
+ s[i - 5] -= ARSHW(y, 10);
+ s[i - 6] -= (y << 20) & 0x3FFFFFFF;
+ s[i - 8] += ARSHW(y, 16);
+ s[i - 9] += (y << 14) & 0x3FFFFFFF;
+ }
+
+ /*
+ * Carry propagation must be signed. Moreover, we may have overdone
+ * it a bit, and obtain a negative result.
+ *
+ * The loop above ran 9 times; each time, each word was augmented
+ * by at most one extra word (in absolute value). Thus, the top
+ * word must in fine fit in 39 bits, so the carry below will fit
+ * on 9 bits.
+ */
+ cc = 0;
+ for (i = 0; i < 9; i ++) {
+ x = s[i] + cc;
+ d[i] = (uint32_t)x & 0x3FFFFFFF;
+ cc = ARSHW(x, 30);
+ }
+
+ /*
+ * All nine words fit on 30 bits, but there may be an extra
+ * carry for a few bits (at most 9), and that carry may be
+ * negative. Moreover, we want the result to fit on 257 bits.
+ * The two lines below ensure that the word in d[] has length
+ * 256 bits, and the (signed) carry (beyond 2^256) is in cc. The
+ * significant length of cc is less than 24 bits, so we will be
+ * able to switch to 32-bit operations.
+ */
+ cc = ARSHW(x, 16);
+ d[8] &= 0xFFFF;
+
+ /*
+ * One extra round of reduction, for cc*2^256, which means
+ * adding cc*(2^224-2^192-2^96+1) to a 256-bit (nonnegative)
+ * value. If cc is negative, then it may happen (rarely, but
+ * not neglectibly so) that the result would be negative. In
+ * order to avoid that, if cc is negative, then we add the
+ * modulus once. Note that if cc is negative, then propagating
+ * that carry must yield a value lower than the modulus, so
+ * adding the modulus once will keep the final result under
+ * twice the modulus.
+ */
+ z = (uint32_t)cc;
+ d[3] -= z << 6;
+ d[6] -= (z << 12) & 0x3FFFFFFF;
+ d[7] -= ARSH(z, 18);
+ d[7] += (z << 14) & 0x3FFFFFFF;
+ d[8] += ARSH(z, 16);
+ c = z >> 31;
+ d[0] -= c;
+ d[3] += c << 6;
+ d[6] += c << 12;
+ d[7] -= c << 14;
+ d[8] += c << 16;
+ for (i = 0; i < 9; i ++) {
+ uint32_t w;
+
+ w = d[i] + z;
+ d[i] = w & 0x3FFFFFFF;
+ z = ARSH(w, 30);
+ }
+}
+
+/*
+ * Perform a "final reduction" in field F256 (field for curve P-256).
+ * The source value must be less than twice the modulus. If the value
+ * is not lower than the modulus, then the modulus is subtracted and
+ * this function returns 1; otherwise, it leaves it untouched and it
+ * returns 0.
+ */
+static uint32_t
+reduce_final_f256(uint32_t *d)
+{
+ uint32_t t[9];
+ uint32_t cc;
+ int i;
+
+ cc = 0;
+ for (i = 0; i < 9; i ++) {
+ uint32_t w;
+
+ w = d[i] - F256[i] - cc;
+ cc = w >> 31;
+ t[i] = w & 0x3FFFFFFF;
+ }
+ cc ^= 1;
+ CCOPY(cc, d, t, sizeof t);
+ return cc;
+}
+
+/*
+ * Jacobian coordinates for a point in P-256: affine coordinates (X,Y)
+ * are such that:
+ * X = x / z^2
+ * Y = y / z^3
+ * For the point at infinity, z = 0.
+ * Each point thus admits many possible representations.
+ *
+ * Coordinates are represented in arrays of 32-bit integers, each holding
+ * 30 bits of data. Values may also be slightly greater than the modulus,
+ * but they will always be lower than twice the modulus.
+ */
+typedef struct {
+ uint32_t x[9];
+ uint32_t y[9];
+ uint32_t z[9];
+} p256_jacobian;
+
+/*
+ * Convert a point to affine coordinates:
+ * - If the point is the point at infinity, then all three coordinates
+ * are set to 0.
+ * - Otherwise, the 'z' coordinate is set to 1, and the 'x' and 'y'
+ * coordinates are the 'X' and 'Y' affine coordinates.
+ * The coordinates are guaranteed to be lower than the modulus.
+ */
+static void
+p256_to_affine(p256_jacobian *P)
+{
+ uint32_t t1[9], t2[9];
+ int i;
+
+ /*
+ * Invert z with a modular exponentiation: the modulus is
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1, and the exponent is
+ * p-2. Exponent bit pattern (from high to low) is:
+ * - 32 bits of value 1
+ * - 31 bits of value 0
+ * - 1 bit of value 1
+ * - 96 bits of value 0
+ * - 94 bits of value 1
+ * - 1 bit of value 0
+ * - 1 bit of value 1
+ * Thus, we precompute z^(2^31-1) to speed things up.
+ *
+ * If z = 0 (point at infinity) then the modular exponentiation
+ * will yield 0, which leads to the expected result (all three
+ * coordinates set to 0).
+ */
+
+ /*
+ * A simple square-and-multiply for z^(2^31-1). We could save about
+ * two dozen multiplications here with an addition chain, but
+ * this would require a bit more code, and extra stack buffers.
+ */
+ memcpy(t1, P->z, sizeof P->z);
+ for (i = 0; i < 30; i ++) {
+ square_f256(t1, t1);
+ mul_f256(t1, t1, P->z);
+ }
+
+ /*
+ * Square-and-multiply. Apart from the squarings, we have a few
+ * multiplications to set bits to 1; we multiply by the original z
+ * for setting 1 bit, and by t1 for setting 31 bits.
+ */
+ memcpy(t2, P->z, sizeof P->z);
+ for (i = 1; i < 256; i ++) {
+ square_f256(t2, t2);
+ switch (i) {
+ case 31:
+ case 190:
+ case 221:
+ case 252:
+ mul_f256(t2, t2, t1);
+ break;
+ case 63:
+ case 253:
+ case 255:
+ mul_f256(t2, t2, P->z);
+ break;
+ }
+ }
+
+ /*
+ * Now that we have 1/z, multiply x by 1/z^2 and y by 1/z^3.
+ */
+ mul_f256(t1, t2, t2);
+ mul_f256(P->x, t1, P->x);
+ mul_f256(t1, t1, t2);
+ mul_f256(P->y, t1, P->y);
+ reduce_final_f256(P->x);
+ reduce_final_f256(P->y);
+
+ /*
+ * Multiply z by 1/z. If z = 0, then this will yield 0, otherwise
+ * this will set z to 1.
+ */
+ mul_f256(P->z, P->z, t2);
+ reduce_final_f256(P->z);
+}
+
+/*
+ * Double a point in P-256. This function works for all valid points,
+ * including the point at infinity.
+ */
+static void
+p256_double(p256_jacobian *Q)
+{
+ /*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * These formulas work for all points, including points of order 2
+ * and points at infinity:
+ * - If y = 0 then z' = 0. But there is no such point in P-256
+ * anyway.
+ * - If z = 0 then z' = 0.
+ */
+ uint32_t t1[9], t2[9], t3[9], t4[9];
+
+ /*
+ * Compute z^2 in t1.
+ */
+ square_f256(t1, Q->z);
+
+ /*
+ * Compute x-z^2 in t2 and x+z^2 in t1.
+ */
+ add_f256(t2, Q->x, t1);
+ sub_f256(t1, Q->x, t1);
+
+ /*
+ * Compute 3*(x+z^2)*(x-z^2) in t1.
+ */
+ mul_f256(t3, t1, t2);
+ add_f256(t1, t3, t3);
+ add_f256(t1, t3, t1);
+
+ /*
+ * Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ square_f256(t3, Q->y);
+ add_f256(t3, t3, t3);
+ mul_f256(t2, Q->x, t3);
+ add_f256(t2, t2, t2);
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ square_f256(Q->x, t1);
+ sub_f256(Q->x, Q->x, t2);
+ sub_f256(Q->x, Q->x, t2);
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ mul_f256(t4, Q->y, Q->z);
+ add_f256(Q->z, t4, t4);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ sub_f256(t2, t2, Q->x);
+ mul_f256(Q->y, t1, t2);
+ square_f256(t4, t3);
+ add_f256(t4, t4, t4);
+ sub_f256(Q->y, Q->y, t4);
+}
+
+/*
+ * Add point P2 to point P1.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0 but P2 != 0
+ * - If P1 != 0 but P2 == 0
+ * - If P1 == P2
+ *
+ * In all three cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate
+ * - P1 == 0 and P2 == 0
+ * - The Y coordinate of one of the points is 0 and the other point is
+ * the point at infinity.
+ *
+ * The third case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ */
+static uint32_t
+p256_add(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ */
+ uint32_t t1[9], t2[9], t3[9], t4[9], t5[9], t6[9], t7[9];
+ uint32_t ret;
+ int i;
+
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ square_f256(t3, P2->z);
+ mul_f256(t1, P1->x, t3);
+ mul_f256(t4, P2->z, t3);
+ mul_f256(t3, P1->y, t4);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ square_f256(t4, P1->z);
+ mul_f256(t2, P2->x, t4);
+ mul_f256(t5, P1->z, t4);
+ mul_f256(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ sub_f256(t2, t2, t1);
+ sub_f256(t4, t4, t3);
+ reduce_final_f256(t4);
+ ret = 0;
+ for (i = 0; i < 9; i ++) {
+ ret |= t4[i];
+ }
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ square_f256(t7, t2);
+ mul_f256(t6, t1, t7);
+ mul_f256(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ square_f256(P1->x, t4);
+ sub_f256(P1->x, P1->x, t5);
+ sub_f256(P1->x, P1->x, t6);
+ sub_f256(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ sub_f256(t6, t6, P1->x);
+ mul_f256(P1->y, t4, t6);
+ mul_f256(t1, t5, t3);
+ sub_f256(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ mul_f256(t1, P1->z, P2->z);
+ mul_f256(P1->z, t1, t2);
+
+ return ret;
+}
+
+/*
+ * Add point P2 to point P1. This is a specialised function for the
+ * case when P2 is a non-zero point in affine coordinate.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0
+ * - If P1 == P2
+ *
+ * In both cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate
+ * - The Y coordinate of P2 is 0 and P1 is the point at infinity.
+ *
+ * The second case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ */
+static uint32_t
+p256_add_mixed(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ */
+ uint32_t t1[9], t2[9], t3[9], t4[9], t5[9], t6[9], t7[9];
+ uint32_t ret;
+ int i;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ square_f256(t4, P1->z);
+ mul_f256(t2, P2->x, t4);
+ mul_f256(t5, P1->z, t4);
+ mul_f256(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ sub_f256(t2, t2, t1);
+ sub_f256(t4, t4, t3);
+ reduce_final_f256(t4);
+ ret = 0;
+ for (i = 0; i < 9; i ++) {
+ ret |= t4[i];
+ }
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ square_f256(t7, t2);
+ mul_f256(t6, t1, t7);
+ mul_f256(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ square_f256(P1->x, t4);
+ sub_f256(P1->x, P1->x, t5);
+ sub_f256(P1->x, P1->x, t6);
+ sub_f256(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ sub_f256(t6, t6, P1->x);
+ mul_f256(P1->y, t4, t6);
+ mul_f256(t1, t5, t3);
+ sub_f256(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ mul_f256(P1->z, P1->z, t2);
+
+ return ret;
+}
+
+/*
+ * Decode a P-256 point. This function does not support the point at
+ * infinity. Returned value is 0 if the point is invalid, 1 otherwise.
+ */
+static uint32_t
+p256_decode(p256_jacobian *P, const void *src, size_t len)
+{
+ const unsigned char *buf;
+ uint32_t tx[9], ty[9], t1[9], t2[9];
+ uint32_t bad;
+ int i;
+
+ if (len != 65) {
+ return 0;
+ }
+ buf = src;
+
+ /*
+ * First byte must be 0x04 (uncompressed format). We could support
+ * "hybrid format" (first byte is 0x06 or 0x07, and encodes the
+ * least significant bit of the Y coordinate), but it is explicitly
+ * forbidden by RFC 5480 (section 2.2).
+ */
+ bad = NEQ(buf[0], 0x04);
+
+ /*
+ * Decode the coordinates, and check that they are both lower
+ * than the modulus.
+ */
+ tx[8] = be8_to_le30(tx, buf + 1, 32);
+ ty[8] = be8_to_le30(ty, buf + 33, 32);
+ bad |= reduce_final_f256(tx);
+ bad |= reduce_final_f256(ty);
+
+ /*
+ * Check curve equation.
+ */
+ square_f256(t1, tx);
+ mul_f256(t1, tx, t1);
+ square_f256(t2, ty);
+ sub_f256(t1, t1, tx);
+ sub_f256(t1, t1, tx);
+ sub_f256(t1, t1, tx);
+ add_f256(t1, t1, P256_B);
+ sub_f256(t1, t1, t2);
+ reduce_final_f256(t1);
+ for (i = 0; i < 9; i ++) {
+ bad |= t1[i];
+ }
+
+ /*
+ * Copy coordinates to the point structure.
+ */
+ memcpy(P->x, tx, sizeof tx);
+ memcpy(P->y, ty, sizeof ty);
+ memset(P->z, 0, sizeof P->z);
+ P->z[0] = 1;
+ return EQ(bad, 0);
+}
+
+/*
+ * Encode a point into a buffer. This function assumes that the point is
+ * valid, in affine coordinates, and not the point at infinity.
+ */
+static void
+p256_encode(void *dst, const p256_jacobian *P)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = 0x04;
+ le30_to_be8(buf + 1, 32, P->x);
+ le30_to_be8(buf + 33, 32, P->y);
+}
+
+/*
+ * Multiply a curve point by an integer. The integer is assumed to be
+ * lower than the curve order, and the base point must not be the point
+ * at infinity.
+ */
+static void
+p256_mul(p256_jacobian *P, const unsigned char *x, size_t xlen)
+{
+ /*
+ * qz is a flag that is initially 1, and remains equal to 1
+ * as long as the point is the point at infinity.
+ *
+ * We use a 2-bit window to handle multiplier bits by pairs.
+ * The precomputed window really is the points P2 and P3.
+ */
+ uint32_t qz;
+ p256_jacobian P2, P3, Q, T, U;
+
+ /*
+ * Compute window values.
+ */
+ P2 = *P;
+ p256_double(&P2);
+ P3 = *P;
+ p256_add(&P3, &P2);
+
+ /*
+ * We start with Q = 0. We process multiplier bits 2 by 2.
+ */
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+
+ for (k = 6; k >= 0; k -= 2) {
+ uint32_t bits;
+ uint32_t bnz;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ T = *P;
+ U = Q;
+ bits = (*x >> k) & (uint32_t)3;
+ bnz = NEQ(bits, 0);
+ CCOPY(EQ(bits, 2), &T, &P2, sizeof T);
+ CCOPY(EQ(bits, 3), &T, &P3, sizeof T);
+ p256_add(&U, &T);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ }
+ x ++;
+ }
+ *P = Q;
+}
+
+/*
+ * Precomputed window: k*G points, where G is the curve generator, and k
+ * is an integer from 1 to 15 (inclusive). The X and Y coordinates of
+ * the point are encoded as 9 words of 30 bits each (little-endian
+ * order).
+ */
+static const uint32_t Gwin[15][18] = {
+
+ { 0x1898C296, 0x1284E517, 0x1EB33A0F, 0x00DF604B,
+ 0x2440F277, 0x339B958E, 0x04247F8B, 0x347CB84B,
+ 0x00006B17, 0x37BF51F5, 0x2ED901A0, 0x3315ECEC,
+ 0x338CD5DA, 0x0F9E162B, 0x1FAD29F0, 0x27F9B8EE,
+ 0x10B8BF86, 0x00004FE3 },
+
+ { 0x07669978, 0x182D23F1, 0x3F21B35A, 0x225A789D,
+ 0x351AC3C0, 0x08E00C12, 0x34F7E8A5, 0x1EC62340,
+ 0x00007CF2, 0x227873D1, 0x3812DE74, 0x0E982299,
+ 0x1F6B798F, 0x3430DBBA, 0x366B1A7D, 0x2D040293,
+ 0x154436E3, 0x00000777 },
+
+ { 0x06E7FD6C, 0x2D05986F, 0x3ADA985F, 0x31ADC87B,
+ 0x0BF165E6, 0x1FBE5475, 0x30A44C8F, 0x3934698C,
+ 0x00005ECB, 0x227D5032, 0x29E6C49E, 0x04FB83D9,
+ 0x0AAC0D8E, 0x24A2ECD8, 0x2C1B3869, 0x0FF7E374,
+ 0x19031266, 0x00008734 },
+
+ { 0x2B030852, 0x024C0911, 0x05596EF5, 0x07F8B6DE,
+ 0x262BD003, 0x3779967B, 0x08FBBA02, 0x128D4CB4,
+ 0x0000E253, 0x184ED8C6, 0x310B08FC, 0x30EE0055,
+ 0x3F25B0FC, 0x062D764E, 0x3FB97F6A, 0x33CC719D,
+ 0x15D69318, 0x0000E0F1 },
+
+ { 0x03D033ED, 0x05552837, 0x35BE5242, 0x2320BF47,
+ 0x268FDFEF, 0x13215821, 0x140D2D78, 0x02DE9454,
+ 0x00005159, 0x3DA16DA4, 0x0742ED13, 0x0D80888D,
+ 0x004BC035, 0x0A79260D, 0x06FCDAFE, 0x2727D8AE,
+ 0x1F6A2412, 0x0000E0C1 },
+
+ { 0x3C2291A9, 0x1AC2ABA4, 0x3B215B4C, 0x131D037A,
+ 0x17DDE302, 0x0C90B2E2, 0x0602C92D, 0x05CA9DA9,
+ 0x0000B01A, 0x0FC77FE2, 0x35F1214E, 0x07E16BDF,
+ 0x003DDC07, 0x2703791C, 0x3038B7EE, 0x3DAD56FE,
+ 0x041D0C8D, 0x0000E85C },
+
+ { 0x3187B2A3, 0x0018A1C0, 0x00FEF5B3, 0x3E7E2E2A,
+ 0x01FB607E, 0x2CC199F0, 0x37B4625B, 0x0EDBE82F,
+ 0x00008E53, 0x01F400B4, 0x15786A1B, 0x3041B21C,
+ 0x31CD8CF2, 0x35900053, 0x1A7E0E9B, 0x318366D0,
+ 0x076F780C, 0x000073EB },
+
+ { 0x1B6FB393, 0x13767707, 0x3CE97DBB, 0x348E2603,
+ 0x354CADC1, 0x09D0B4EA, 0x1B053404, 0x1DE76FBA,
+ 0x000062D9, 0x0F09957E, 0x295029A8, 0x3E76A78D,
+ 0x3B547DAE, 0x27CEE0A2, 0x0575DC45, 0x1D8244FF,
+ 0x332F647A, 0x0000AD5A },
+
+ { 0x10949EE0, 0x1E7A292E, 0x06DF8B3D, 0x02B2E30B,
+ 0x31F8729E, 0x24E35475, 0x30B71878, 0x35EDBFB7,
+ 0x0000EA68, 0x0DD048FA, 0x21688929, 0x0DE823FE,
+ 0x1C53FAA9, 0x0EA0C84D, 0x052A592A, 0x1FCE7870,
+ 0x11325CB2, 0x00002A27 },
+
+ { 0x04C5723F, 0x30D81A50, 0x048306E4, 0x329B11C7,
+ 0x223FB545, 0x085347A8, 0x2993E591, 0x1B5ACA8E,
+ 0x0000CEF6, 0x04AF0773, 0x28D2EEA9, 0x2751EEEC,
+ 0x037B4A7F, 0x3B4C1059, 0x08F37674, 0x2AE906E1,
+ 0x18A88A6A, 0x00008786 },
+
+ { 0x34BC21D1, 0x0CCE474D, 0x15048BF4, 0x1D0BB409,
+ 0x021CDA16, 0x20DE76C3, 0x34C59063, 0x04EDE20E,
+ 0x00003ED1, 0x282A3740, 0x0BE3BBF3, 0x29889DAE,
+ 0x03413697, 0x34C68A09, 0x210EBE93, 0x0C8A224C,
+ 0x0826B331, 0x00009099 },
+
+ { 0x0624E3C4, 0x140317BA, 0x2F82C99D, 0x260C0A2C,
+ 0x25D55179, 0x194DCC83, 0x3D95E462, 0x356F6A05,
+ 0x0000741D, 0x0D4481D3, 0x2657FC8B, 0x1BA5CA71,
+ 0x3AE44B0D, 0x07B1548E, 0x0E0D5522, 0x05FDC567,
+ 0x2D1AA70E, 0x00000770 },
+
+ { 0x06072C01, 0x23857675, 0x1EAD58A9, 0x0B8A12D9,
+ 0x1EE2FC79, 0x0177CB61, 0x0495A618, 0x20DEB82B,
+ 0x0000177C, 0x2FC7BFD8, 0x310EEF8B, 0x1FB4DF39,
+ 0x3B8530E8, 0x0F4E7226, 0x0246B6D0, 0x2A558A24,
+ 0x163353AF, 0x000063BB },
+
+ { 0x24D2920B, 0x1C249DCC, 0x2069C5E5, 0x09AB2F9E,
+ 0x36DF3CF1, 0x1991FD0C, 0x062B97A7, 0x1E80070E,
+ 0x000054E7, 0x20D0B375, 0x2E9F20BD, 0x35090081,
+ 0x1C7A9DDC, 0x22E7C371, 0x087E3016, 0x03175421,
+ 0x3C6ECA7D, 0x0000F599 },
+
+ { 0x259B9D5F, 0x0D9A318F, 0x23A0EF16, 0x00EBE4B7,
+ 0x088265AE, 0x2CDE2666, 0x2BAE7ADF, 0x1371A5C6,
+ 0x0000F045, 0x0D034F36, 0x1F967378, 0x1B5FA3F4,
+ 0x0EC8739D, 0x1643E62A, 0x1653947E, 0x22D1F4E6,
+ 0x0FB8D64B, 0x0000B5B9 }
+};
+
+/*
+ * Lookup one of the Gwin[] values, by index. This is constant-time.
+ */
+static void
+lookup_Gwin(p256_jacobian *T, uint32_t idx)
+{
+ uint32_t xy[18];
+ uint32_t k;
+ size_t u;
+
+ memset(xy, 0, sizeof xy);
+ for (k = 0; k < 15; k ++) {
+ uint32_t m;
+
+ m = -EQ(idx, k + 1);
+ for (u = 0; u < 18; u ++) {
+ xy[u] |= m & Gwin[k][u];
+ }
+ }
+ memcpy(T->x, &xy[0], sizeof T->x);
+ memcpy(T->y, &xy[9], sizeof T->y);
+ memset(T->z, 0, sizeof T->z);
+ T->z[0] = 1;
+}
+
+/*
+ * Multiply the generator by an integer. The integer is assumed non-zero
+ * and lower than the curve order.
+ */
+static void
+p256_mulgen(p256_jacobian *P, const unsigned char *x, size_t xlen)
+{
+ /*
+ * qz is a flag that is initially 1, and remains equal to 1
+ * as long as the point is the point at infinity.
+ *
+ * We use a 4-bit window to handle multiplier bits by groups
+ * of 4. The precomputed window is constant static data, with
+ * points in affine coordinates; we use a constant-time lookup.
+ */
+ p256_jacobian Q;
+ uint32_t qz;
+
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+ unsigned bx;
+
+ bx = *x ++;
+ for (k = 0; k < 2; k ++) {
+ uint32_t bits;
+ uint32_t bnz;
+ p256_jacobian T, U;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ bits = (bx >> 4) & 0x0F;
+ bnz = NEQ(bits, 0);
+ lookup_Gwin(&T, bits);
+ U = Q;
+ p256_add_mixed(&U, &T);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ bx <<= 4;
+ }
+ }
+ *P = Q;
+}
+
+static const unsigned char P256_G[] = {
+ 0x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,
+ 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,
+ 0x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,
+ 0x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,
+ 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,
+ 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,
+ 0x68, 0x37, 0xBF, 0x51, 0xF5
+};
+
+static const unsigned char P256_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,
+ 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,
+ 0x25, 0x51
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_G;
+ return P256_G;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_N;
+ return P256_N;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 1;
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ uint32_t r;
+ p256_jacobian P;
+
+ (void)curve;
+ r = p256_decode(&P, G, Glen);
+ p256_mul(&P, x, xlen);
+ if (Glen >= 65) {
+ p256_to_affine(&P);
+ p256_encode(G, &P);
+ }
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ p256_jacobian P;
+
+ (void)curve;
+ p256_mulgen(&P, x, xlen);
+ p256_to_affine(&P);
+ p256_encode(R, &P);
+ return 65;
+
+ /*
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+ */
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ p256_jacobian P, Q;
+ uint32_t r, t, z;
+ int i;
+
+ (void)curve;
+ r = p256_decode(&P, A, len);
+ p256_mul(&P, x, xlen);
+ if (B == NULL) {
+ p256_mulgen(&Q, y, ylen);
+ } else {
+ r &= p256_decode(&Q, B, len);
+ p256_mul(&Q, y, ylen);
+ }
+
+ /*
+ * The final addition may fail in case both points are equal.
+ */
+ t = p256_add(&P, &Q);
+ reduce_final_f256(P.z);
+ z = 0;
+ for (i = 0; i < 9; i ++) {
+ z |= P.z[i];
+ }
+ z = EQ(z, 0);
+ p256_double(&Q);
+
+ /*
+ * If z is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * z = 0, t = 0 return P (normal addition)
+ * z = 0, t = 1 return P (normal addition)
+ * z = 1, t = 0 return Q (a 'double' case)
+ * z = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(z & ~t, &P, &Q, sizeof Q);
+ p256_to_affine(&P);
+ p256_encode(A, &P);
+ r &= ~(z & t);
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_p256_m31 = {
+ (uint32_t)0x00800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_p256_m62.c b/test/monniaux/BearSSL/src/ec/ec_p256_m62.c
new file mode 100644
index 00000000..3bcb95b5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_p256_m62.c
@@ -0,0 +1,1765 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_UMUL128
+#include <intrin.h>
+#endif
+
+static const unsigned char P256_G[] = {
+ 0x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,
+ 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,
+ 0x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,
+ 0x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,
+ 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,
+ 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,
+ 0x68, 0x37, 0xBF, 0x51, 0xF5
+};
+
+static const unsigned char P256_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,
+ 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,
+ 0x25, 0x51
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_G;
+ return P256_G;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_N;
+ return P256_N;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 1;
+}
+
+/*
+ * A field element is encoded as five 64-bit integers, in basis 2^52.
+ * Limbs may occasionally exceed 2^52.
+ *
+ * A _partially reduced_ value is such that the following hold:
+ * - top limb is less than 2^48 + 2^30
+ * - the other limbs fit on 53 bits each
+ * In particular, such a value is less than twice the modulus p.
+ */
+
+#define BIT(n) ((uint64_t)1 << (n))
+#define MASK48 (BIT(48) - BIT(0))
+#define MASK52 (BIT(52) - BIT(0))
+
+/* R = 2^260 mod p */
+static const uint64_t F256_R[] = {
+ 0x0000000000010, 0xF000000000000, 0xFFFFFFFFFFFFF,
+ 0xFFEFFFFFFFFFF, 0x00000000FFFFF
+};
+
+/* Curve equation is y^2 = x^3 - 3*x + B. This constant is B*R mod p
+ (Montgomery representation of B). */
+static const uint64_t P256_B_MONTY[] = {
+ 0xDF6229C4BDDFD, 0xCA8843090D89C, 0x212ED6ACF005C,
+ 0x83415A220ABF7, 0x0C30061DD4874
+};
+
+/*
+ * Addition in the field. Carry propagation is not performed.
+ * On input, limbs may be up to 63 bits each; on output, they will
+ * be up to one bit more than on input.
+ */
+static inline void
+f256_add(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+ d[0] = a[0] + b[0];
+ d[1] = a[1] + b[1];
+ d[2] = a[2] + b[2];
+ d[3] = a[3] + b[3];
+ d[4] = a[4] + b[4];
+}
+
+/*
+ * Partially reduce the provided value.
+ * Input: limbs can go up to 61 bits each.
+ * Output: partially reduced.
+ */
+static inline void
+f256_partial_reduce(uint64_t *a)
+{
+ uint64_t w, cc, s;
+
+ /*
+ * Propagate carries.
+ */
+ w = a[0];
+ a[0] = w & MASK52;
+ cc = w >> 52;
+ w = a[1] + cc;
+ a[1] = w & MASK52;
+ cc = w >> 52;
+ w = a[2] + cc;
+ a[2] = w & MASK52;
+ cc = w >> 52;
+ w = a[3] + cc;
+ a[3] = w & MASK52;
+ cc = w >> 52;
+ a[4] += cc;
+
+ s = a[4] >> 48; /* s < 2^14 */
+ a[0] += s; /* a[0] < 2^52 + 2^14 */
+ w = a[1] - (s << 44);
+ a[1] = w & MASK52; /* a[1] < 2^52 */
+ cc = -(w >> 52) & 0xFFF; /* cc < 16 */
+ w = a[2] - cc;
+ a[2] = w & MASK52; /* a[2] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = a[3] - cc - (s << 36);
+ a[3] = w & MASK52; /* a[3] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = a[4] & MASK48;
+ a[4] = w + (s << 16) - cc; /* a[4] < 2^48 + 2^30 */
+}
+
+/*
+ * Subtraction in the field.
+ * Input: limbs must fit on 60 bits each; in particular, the complete
+ * integer will be less than 2^268 + 2^217.
+ * Output: partially reduced.
+ */
+static inline void
+f256_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+ uint64_t t[5], w, s, cc;
+
+ /*
+ * We compute d = 2^13*p + a - b; this ensures a positive
+ * intermediate value.
+ *
+ * Each individual addition/subtraction may yield a positive or
+ * negative result; thus, we need to handle a signed carry, thus
+ * with sign extension. We prefer not to use signed types (int64_t)
+ * because conversion from unsigned to signed is cumbersome (a
+ * direct cast with the top bit set is undefined behavior; instead,
+ * we have to use pointer aliasing, using the guaranteed properties
+ * of exact-width types, but this requires the compiler to optimize
+ * away the writes and reads from RAM), and right-shifting a
+ * signed negative value is implementation-defined. Therefore,
+ * we use a custom sign extension.
+ */
+
+ w = a[0] - b[0] - BIT(13);
+ t[0] = w & MASK52;
+ cc = w >> 52;
+ cc |= -(cc & BIT(11));
+ w = a[1] - b[1] + cc;
+ t[1] = w & MASK52;
+ cc = w >> 52;
+ cc |= -(cc & BIT(11));
+ w = a[2] - b[2] + cc;
+ t[2] = (w & MASK52) + BIT(5);
+ cc = w >> 52;
+ cc |= -(cc & BIT(11));
+ w = a[3] - b[3] + cc;
+ t[3] = (w & MASK52) + BIT(49);
+ cc = w >> 52;
+ cc |= -(cc & BIT(11));
+ t[4] = (BIT(61) - BIT(29)) + a[4] - b[4] + cc;
+
+ /*
+ * Perform partial reduction. Rule is:
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p
+ *
+ * At that point:
+ * 0 <= t[0] <= 2^52 - 1
+ * 0 <= t[1] <= 2^52 - 1
+ * 2^5 <= t[2] <= 2^52 + 2^5 - 1
+ * 2^49 <= t[3] <= 2^52 + 2^49 - 1
+ * 2^59 < t[4] <= 2^61 + 2^60 - 2^29
+ *
+ * Thus, the value 's' (t[4] / 2^48) will be necessarily
+ * greater than 2048, and less than 12288.
+ */
+ s = t[4] >> 48;
+
+ d[0] = t[0] + s; /* d[0] <= 2^52 + 12287 */
+ w = t[1] - (s << 44);
+ d[1] = w & MASK52; /* d[1] <= 2^52 - 1 */
+ cc = -(w >> 52) & 0xFFF; /* cc <= 48 */
+ w = t[2] - cc;
+ cc = w >> 63; /* cc = 0 or 1 */
+ d[2] = w + (cc << 52); /* d[2] <= 2^52 + 31 */
+ w = t[3] - cc - (s << 36);
+ cc = w >> 63; /* cc = 0 or 1 */
+ d[3] = w + (cc << 52); /* t[3] <= 2^52 + 2^49 - 1 */
+ d[4] = (t[4] & MASK48) + (s << 16) - cc; /* d[4] < 2^48 + 2^30 */
+
+ /*
+ * If s = 0, then none of the limbs is modified, and there cannot
+ * be an overflow; if s != 0, then (s << 16) > cc, and there is
+ * no overflow either.
+ */
+}
+
+/*
+ * Montgomery multiplication in the field.
+ * Input: limbs must fit on 56 bits each.
+ * Output: partially reduced.
+ */
+static void
+f256_montymul(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+
+ int i;
+ uint64_t t[5];
+
+ t[0] = 0;
+ t[1] = 0;
+ t[2] = 0;
+ t[3] = 0;
+ t[4] = 0;
+ for (i = 0; i < 5; i ++) {
+ uint64_t x, f, cc, w, s;
+ unsigned __int128 z;
+
+ /*
+ * Since limbs of a[] and b[] fit on 56 bits each,
+ * each individual product fits on 112 bits. Also,
+ * the factor f fits on 52 bits, so f<<48 fits on
+ * 112 bits too. This guarantees that carries (cc)
+ * will fit on 62 bits, thus no overflow.
+ *
+ * The operations below compute:
+ * t <- (t + x*b + f*p) / 2^64
+ */
+ x = a[i];
+ z = (unsigned __int128)b[0] * (unsigned __int128)x
+ + (unsigned __int128)t[0];
+ f = (uint64_t)z & MASK52;
+ cc = (uint64_t)(z >> 52);
+ z = (unsigned __int128)b[1] * (unsigned __int128)x
+ + (unsigned __int128)t[1] + cc
+ + ((unsigned __int128)f << 44);
+ t[0] = (uint64_t)z & MASK52;
+ cc = (uint64_t)(z >> 52);
+ z = (unsigned __int128)b[2] * (unsigned __int128)x
+ + (unsigned __int128)t[2] + cc;
+ t[1] = (uint64_t)z & MASK52;
+ cc = (uint64_t)(z >> 52);
+ z = (unsigned __int128)b[3] * (unsigned __int128)x
+ + (unsigned __int128)t[3] + cc
+ + ((unsigned __int128)f << 36);
+ t[2] = (uint64_t)z & MASK52;
+ cc = (uint64_t)(z >> 52);
+ z = (unsigned __int128)b[4] * (unsigned __int128)x
+ + (unsigned __int128)t[4] + cc
+ + ((unsigned __int128)f << 48)
+ - ((unsigned __int128)f << 16);
+ t[3] = (uint64_t)z & MASK52;
+ t[4] = (uint64_t)(z >> 52);
+
+ /*
+ * t[4] may be up to 62 bits here; we need to do a
+ * partial reduction. Note that limbs t[0] to t[3]
+ * fit on 52 bits each.
+ */
+ s = t[4] >> 48; /* s < 2^14 */
+ t[0] += s; /* t[0] < 2^52 + 2^14 */
+ w = t[1] - (s << 44);
+ t[1] = w & MASK52; /* t[1] < 2^52 */
+ cc = -(w >> 52) & 0xFFF; /* cc < 16 */
+ w = t[2] - cc;
+ t[2] = w & MASK52; /* t[2] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = t[3] - cc - (s << 36);
+ t[3] = w & MASK52; /* t[3] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = t[4] & MASK48;
+ t[4] = w + (s << 16) - cc; /* t[4] < 2^48 + 2^30 */
+
+ /*
+ * The final t[4] cannot overflow because cc is 0 or 1,
+ * and cc can be 1 only if s != 0.
+ */
+ }
+
+ d[0] = t[0];
+ d[1] = t[1];
+ d[2] = t[2];
+ d[3] = t[3];
+ d[4] = t[4];
+
+#elif BR_UMUL128
+
+ int i;
+ uint64_t t[5];
+
+ t[0] = 0;
+ t[1] = 0;
+ t[2] = 0;
+ t[3] = 0;
+ t[4] = 0;
+ for (i = 0; i < 5; i ++) {
+ uint64_t x, f, cc, w, s, zh, zl;
+ unsigned char k;
+
+ /*
+ * Since limbs of a[] and b[] fit on 56 bits each,
+ * each individual product fits on 112 bits. Also,
+ * the factor f fits on 52 bits, so f<<48 fits on
+ * 112 bits too. This guarantees that carries (cc)
+ * will fit on 62 bits, thus no overflow.
+ *
+ * The operations below compute:
+ * t <- (t + x*b + f*p) / 2^64
+ */
+ x = a[i];
+ zl = _umul128(b[0], x, &zh);
+ k = _addcarry_u64(0, t[0], zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ f = zl & MASK52;
+ cc = (zl >> 52) | (zh << 12);
+
+ zl = _umul128(b[1], x, &zh);
+ k = _addcarry_u64(0, t[1], zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, cc, zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, f << 44, zl, &zl);
+ (void)_addcarry_u64(k, f >> 20, zh, &zh);
+ t[0] = zl & MASK52;
+ cc = (zl >> 52) | (zh << 12);
+
+ zl = _umul128(b[2], x, &zh);
+ k = _addcarry_u64(0, t[2], zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, cc, zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ t[1] = zl & MASK52;
+ cc = (zl >> 52) | (zh << 12);
+
+ zl = _umul128(b[3], x, &zh);
+ k = _addcarry_u64(0, t[3], zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, cc, zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, f << 36, zl, &zl);
+ (void)_addcarry_u64(k, f >> 28, zh, &zh);
+ t[2] = zl & MASK52;
+ cc = (zl >> 52) | (zh << 12);
+
+ zl = _umul128(b[4], x, &zh);
+ k = _addcarry_u64(0, t[4], zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, cc, zl, &zl);
+ (void)_addcarry_u64(k, 0, zh, &zh);
+ k = _addcarry_u64(0, f << 48, zl, &zl);
+ (void)_addcarry_u64(k, f >> 16, zh, &zh);
+ k = _subborrow_u64(0, zl, f << 16, &zl);
+ (void)_subborrow_u64(k, zh, f >> 48, &zh);
+ t[3] = zl & MASK52;
+ t[4] = (zl >> 52) | (zh << 12);
+
+ /*
+ * t[4] may be up to 62 bits here; we need to do a
+ * partial reduction. Note that limbs t[0] to t[3]
+ * fit on 52 bits each.
+ */
+ s = t[4] >> 48; /* s < 2^14 */
+ t[0] += s; /* t[0] < 2^52 + 2^14 */
+ w = t[1] - (s << 44);
+ t[1] = w & MASK52; /* t[1] < 2^52 */
+ cc = -(w >> 52) & 0xFFF; /* cc < 16 */
+ w = t[2] - cc;
+ t[2] = w & MASK52; /* t[2] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = t[3] - cc - (s << 36);
+ t[3] = w & MASK52; /* t[3] < 2^52 */
+ cc = w >> 63; /* cc = 0 or 1 */
+ w = t[4] & MASK48;
+ t[4] = w + (s << 16) - cc; /* t[4] < 2^48 + 2^30 */
+
+ /*
+ * The final t[4] cannot overflow because cc is 0 or 1,
+ * and cc can be 1 only if s != 0.
+ */
+ }
+
+ d[0] = t[0];
+ d[1] = t[1];
+ d[2] = t[2];
+ d[3] = t[3];
+ d[4] = t[4];
+
+#endif
+}
+
+/*
+ * Montgomery squaring in the field; currently a basic wrapper around
+ * multiplication (inline, should be optimized away).
+ * TODO: see if some extra speed can be gained here.
+ */
+static inline void
+f256_montysquare(uint64_t *d, const uint64_t *a)
+{
+ f256_montymul(d, a, a);
+}
+
+/*
+ * Convert to Montgomery representation.
+ */
+static void
+f256_tomonty(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * R2 = 2^520 mod p.
+ * If R = 2^260 mod p, then R2 = R^2 mod p; and the Montgomery
+ * multiplication of a by R2 is: a*R2/R = a*R mod p, i.e. the
+ * conversion to Montgomery representation.
+ */
+ static const uint64_t R2[] = {
+ 0x0000000000300, 0xFFFFFFFF00000, 0xFFFFEFFFFFFFB,
+ 0xFDFFFFFFFFFFF, 0x0000004FFFFFF
+ };
+
+ f256_montymul(d, a, R2);
+}
+
+/*
+ * Convert from Montgomery representation.
+ */
+static void
+f256_frommonty(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * Montgomery multiplication by 1 is division by 2^260 modulo p.
+ */
+ static const uint64_t one[] = { 1, 0, 0, 0, 0 };
+
+ f256_montymul(d, a, one);
+}
+
+/*
+ * Inversion in the field. If the source value is 0 modulo p, then this
+ * returns 0 or p. This function uses Montgomery representation.
+ */
+static void
+f256_invert(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * We compute a^(p-2) mod p. The exponent pattern (from high to
+ * low) is:
+ * - 32 bits of value 1
+ * - 31 bits of value 0
+ * - 1 bit of value 1
+ * - 96 bits of value 0
+ * - 94 bits of value 1
+ * - 1 bit of value 0
+ * - 1 bit of value 1
+ * To speed up the square-and-multiply algorithm, we precompute
+ * a^(2^31-1).
+ */
+
+ uint64_t r[5], t[5];
+ int i;
+
+ memcpy(t, a, sizeof t);
+ for (i = 0; i < 30; i ++) {
+ f256_montysquare(t, t);
+ f256_montymul(t, t, a);
+ }
+
+ memcpy(r, t, sizeof t);
+ for (i = 224; i >= 0; i --) {
+ f256_montysquare(r, r);
+ switch (i) {
+ case 0:
+ case 2:
+ case 192:
+ case 224:
+ f256_montymul(r, r, a);
+ break;
+ case 3:
+ case 34:
+ case 65:
+ f256_montymul(r, r, t);
+ break;
+ }
+ }
+ memcpy(d, r, sizeof r);
+}
+
+/*
+ * Finalize reduction.
+ * Input value should be partially reduced.
+ * On output, limbs a[0] to a[3] fit on 52 bits each, limb a[4] fits
+ * on 48 bits, and the integer is less than p.
+ */
+static inline void
+f256_final_reduce(uint64_t *a)
+{
+ uint64_t r[5], t[5], w, cc;
+ int i;
+
+ /*
+ * Propagate carries to ensure that limbs 0 to 3 fit on 52 bits.
+ */
+ cc = 0;
+ for (i = 0; i < 5; i ++) {
+ w = a[i] + cc;
+ r[i] = w & MASK52;
+ cc = w >> 52;
+ }
+
+ /*
+ * We compute t = r + (2^256 - p) = r + 2^224 - 2^192 - 2^96 + 1.
+ * If t < 2^256, then r < p, and we return r. Otherwise, we
+ * want to return r - p = t - 2^256.
+ */
+
+ /*
+ * Add 2^224 + 1, and propagate carries to ensure that limbs
+ * t[0] to t[3] fit in 52 bits each.
+ */
+ w = r[0] + 1;
+ t[0] = w & MASK52;
+ cc = w >> 52;
+ w = r[1] + cc;
+ t[1] = w & MASK52;
+ cc = w >> 52;
+ w = r[2] + cc;
+ t[2] = w & MASK52;
+ cc = w >> 52;
+ w = r[3] + cc;
+ t[3] = w & MASK52;
+ cc = w >> 52;
+ t[4] = r[4] + cc + BIT(16);
+
+ /*
+ * Subtract 2^192 + 2^96. Since we just added 2^224 + 1, the
+ * result cannot be negative.
+ */
+ w = t[1] - BIT(44);
+ t[1] = w & MASK52;
+ cc = w >> 63;
+ w = t[2] - cc;
+ t[2] = w & MASK52;
+ cc = w >> 63;
+ w = t[3] - BIT(36);
+ t[3] = w & MASK52;
+ cc = w >> 63;
+ t[4] -= cc;
+
+ /*
+ * If the top limb t[4] fits on 48 bits, then r[] is already
+ * in the proper range. Otherwise, t[] is the value to return
+ * (truncated to 256 bits).
+ */
+ cc = -(t[4] >> 48);
+ t[4] &= MASK48;
+ for (i = 0; i < 5; i ++) {
+ a[i] = r[i] ^ (cc & (r[i] ^ t[i]));
+ }
+}
+
+/*
+ * Points in affine and Jacobian coordinates.
+ *
+ * - In affine coordinates, the point-at-infinity cannot be encoded.
+ * - Jacobian coordinates (X,Y,Z) correspond to affine (X/Z^2,Y/Z^3);
+ * if Z = 0 then this is the point-at-infinity.
+ */
+typedef struct {
+ uint64_t x[5];
+ uint64_t y[5];
+} p256_affine;
+
+typedef struct {
+ uint64_t x[5];
+ uint64_t y[5];
+ uint64_t z[5];
+} p256_jacobian;
+
+/*
+ * Decode a field element (unsigned big endian notation).
+ */
+static void
+f256_decode(uint64_t *a, const unsigned char *buf)
+{
+ uint64_t w0, w1, w2, w3;
+
+ w3 = br_dec64be(buf + 0);
+ w2 = br_dec64be(buf + 8);
+ w1 = br_dec64be(buf + 16);
+ w0 = br_dec64be(buf + 24);
+ a[0] = w0 & MASK52;
+ a[1] = ((w0 >> 52) | (w1 << 12)) & MASK52;
+ a[2] = ((w1 >> 40) | (w2 << 24)) & MASK52;
+ a[3] = ((w2 >> 28) | (w3 << 36)) & MASK52;
+ a[4] = w3 >> 16;
+}
+
+/*
+ * Encode a field element (unsigned big endian notation). The field
+ * element MUST be fully reduced.
+ */
+static void
+f256_encode(unsigned char *buf, const uint64_t *a)
+{
+ uint64_t w0, w1, w2, w3;
+
+ w0 = a[0] | (a[1] << 52);
+ w1 = (a[1] >> 12) | (a[2] << 40);
+ w2 = (a[2] >> 24) | (a[3] << 28);
+ w3 = (a[3] >> 36) | (a[4] << 16);
+ br_enc64be(buf + 0, w3);
+ br_enc64be(buf + 8, w2);
+ br_enc64be(buf + 16, w1);
+ br_enc64be(buf + 24, w0);
+}
+
+/*
+ * Decode a point. The returned point is in Jacobian coordinates, but
+ * with z = 1. If the encoding is invalid, or encodes a point which is
+ * not on the curve, or encodes the point at infinity, then this function
+ * returns 0. Otherwise, 1 is returned.
+ *
+ * The buffer is assumed to have length exactly 65 bytes.
+ */
+static uint32_t
+point_decode(p256_jacobian *P, const unsigned char *buf)
+{
+ uint64_t x[5], y[5], t[5], x3[5], tt;
+ uint32_t r;
+
+ /*
+ * Header byte shall be 0x04.
+ */
+ r = EQ(buf[0], 0x04);
+
+ /*
+ * Decode X and Y coordinates, and convert them into
+ * Montgomery representation.
+ */
+ f256_decode(x, buf + 1);
+ f256_decode(y, buf + 33);
+ f256_tomonty(x, x);
+ f256_tomonty(y, y);
+
+ /*
+ * Verify y^2 = x^3 + A*x + B. In curve P-256, A = -3.
+ * Note that the Montgomery representation of 0 is 0. We must
+ * take care to apply the final reduction to make sure we have
+ * 0 and not p.
+ */
+ f256_montysquare(t, y);
+ f256_montysquare(x3, x);
+ f256_montymul(x3, x3, x);
+ f256_sub(t, t, x3);
+ f256_add(t, t, x);
+ f256_add(t, t, x);
+ f256_add(t, t, x);
+ f256_sub(t, t, P256_B_MONTY);
+ f256_final_reduce(t);
+ tt = t[0] | t[1] | t[2] | t[3] | t[4];
+ r &= EQ((uint32_t)(tt | (tt >> 32)), 0);
+
+ /*
+ * Return the point in Jacobian coordinates (and Montgomery
+ * representation).
+ */
+ memcpy(P->x, x, sizeof x);
+ memcpy(P->y, y, sizeof y);
+ memcpy(P->z, F256_R, sizeof F256_R);
+ return r;
+}
+
+/*
+ * Final conversion for a point:
+ * - The point is converted back to affine coordinates.
+ * - Final reduction is performed.
+ * - The point is encoded into the provided buffer.
+ *
+ * If the point is the point-at-infinity, all operations are performed,
+ * but the buffer contents are indeterminate, and 0 is returned. Otherwise,
+ * the encoded point is written in the buffer, and 1 is returned.
+ */
+static uint32_t
+point_encode(unsigned char *buf, const p256_jacobian *P)
+{
+ uint64_t t1[5], t2[5], z;
+
+ /* Set t1 = 1/z^2 and t2 = 1/z^3. */
+ f256_invert(t2, P->z);
+ f256_montysquare(t1, t2);
+ f256_montymul(t2, t2, t1);
+
+ /* Compute affine coordinates x (in t1) and y (in t2). */
+ f256_montymul(t1, P->x, t1);
+ f256_montymul(t2, P->y, t2);
+
+ /* Convert back from Montgomery representation, and finalize
+ reductions. */
+ f256_frommonty(t1, t1);
+ f256_frommonty(t2, t2);
+ f256_final_reduce(t1);
+ f256_final_reduce(t2);
+
+ /* Encode. */
+ buf[0] = 0x04;
+ f256_encode(buf + 1, t1);
+ f256_encode(buf + 33, t2);
+
+ /* Return success if and only if P->z != 0. */
+ z = P->z[0] | P->z[1] | P->z[2] | P->z[3] | P->z[4];
+ return NEQ((uint32_t)(z | z >> 32), 0);
+}
+
+/*
+ * Point doubling in Jacobian coordinates: point P is doubled.
+ * Note: if the source point is the point-at-infinity, then the result is
+ * still the point-at-infinity, which is correct. Moreover, if the three
+ * coordinates were zero, then they still are zero in the returned value.
+ */
+static void
+p256_double(p256_jacobian *P)
+{
+ /*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * These formulas work for all points, including points of order 2
+ * and points at infinity:
+ * - If y = 0 then z' = 0. But there is no such point in P-256
+ * anyway.
+ * - If z = 0 then z' = 0.
+ */
+ uint64_t t1[5], t2[5], t3[5], t4[5];
+
+ /*
+ * Compute z^2 in t1.
+ */
+ f256_montysquare(t1, P->z);
+
+ /*
+ * Compute x-z^2 in t2 and x+z^2 in t1.
+ */
+ f256_add(t2, P->x, t1);
+ f256_sub(t1, P->x, t1);
+
+ /*
+ * Compute 3*(x+z^2)*(x-z^2) in t1.
+ */
+ f256_montymul(t3, t1, t2);
+ f256_add(t1, t3, t3);
+ f256_add(t1, t3, t1);
+
+ /*
+ * Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ f256_montysquare(t3, P->y);
+ f256_add(t3, t3, t3);
+ f256_montymul(t2, P->x, t3);
+ f256_add(t2, t2, t2);
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ f256_montysquare(P->x, t1);
+ f256_sub(P->x, P->x, t2);
+ f256_sub(P->x, P->x, t2);
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ f256_montymul(t4, P->y, P->z);
+ f256_add(P->z, t4, t4);
+ f256_partial_reduce(P->z);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ f256_sub(t2, t2, P->x);
+ f256_montymul(P->y, t1, t2);
+ f256_montysquare(t4, t3);
+ f256_add(t4, t4, t4);
+ f256_sub(P->y, P->y, t4);
+}
+
+/*
+ * Point addition (Jacobian coordinates): P1 is replaced with P1+P2.
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0 but P2 != 0
+ * - If P1 != 0 but P2 == 0
+ * - If P1 == P2
+ *
+ * In all three cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate.
+ * - P1 == 0 and P2 == 0.
+ * - The Y coordinate of one of the points is 0 and the other point is
+ * the point at infinity.
+ *
+ * The third case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ *
+ * Note that you can get a returned value of 0 with a correct result,
+ * e.g. if P1 and P2 have the same Y coordinate, but distinct X coordinates.
+ */
+static uint32_t
+p256_add(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ */
+ uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt;
+ uint32_t ret;
+
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ f256_montysquare(t3, P2->z);
+ f256_montymul(t1, P1->x, t3);
+ f256_montymul(t4, P2->z, t3);
+ f256_montymul(t3, P1->y, t4);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+ f256_final_reduce(t4);
+ tt = t4[0] | t4[1] | t4[2] | t4[3] | t4[4];
+ ret = (uint32_t)(tt | (tt >> 32));
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ f256_montymul(t1, P1->z, P2->z);
+ f256_montymul(P1->z, t1, t2);
+
+ return ret;
+}
+
+/*
+ * Point addition (mixed coordinates): P1 is replaced with P1+P2.
+ * This is a specialised function for the case when P2 is a non-zero point
+ * in affine coordinates.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0
+ * - If P1 == P2
+ *
+ * In both cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y (affine) coordinate.
+ * - The Y coordinate of P2 is 0 and P1 is the point at infinity.
+ *
+ * The second case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ *
+ * Again, a value of 0 may be returned in some cases where the addition
+ * result is correct.
+ */
+static uint32_t
+p256_add_mixed(p256_jacobian *P1, const p256_affine *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ */
+ uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt;
+ uint32_t ret;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+ f256_final_reduce(t4);
+ tt = t4[0] | t4[1] | t4[2] | t4[3] | t4[4];
+ ret = (uint32_t)(tt | (tt >> 32));
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ f256_montymul(P1->z, P1->z, t2);
+
+ return ret;
+}
+
+#if 0
+/* unused */
+/*
+ * Point addition (mixed coordinates, complete): P1 is replaced with P1+P2.
+ * This is a specialised function for the case when P2 is a non-zero point
+ * in affine coordinates.
+ *
+ * This function returns the correct result in all cases.
+ */
+static uint32_t
+p256_add_complete_mixed(p256_jacobian *P1, const p256_affine *P2)
+{
+ /*
+ * Addtions formulas, in the general case, are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ *
+ * These formulas mishandle the two following cases:
+ *
+ * - If P1 is the point-at-infinity (z1 = 0), then z3 is
+ * incorrectly set to 0.
+ *
+ * - If P1 = P2, then u1 = u2 and s1 = s2, and x3, y3 and z3
+ * are all set to 0.
+ *
+ * However, if P1 + P2 = 0, then u1 = u2 but s1 != s2, and then
+ * we correctly get z3 = 0 (the point-at-infinity).
+ *
+ * To fix the case P1 = 0, we perform at the end a copy of P2
+ * over P1, conditional to z1 = 0.
+ *
+ * For P1 = P2: in that case, both h and r are set to 0, and
+ * we get x3, y3 and z3 equal to 0. We can test for that
+ * occurrence to make a mask which will be all-one if P1 = P2,
+ * or all-zero otherwise; then we can compute the double of P2
+ * and add it, combined with the mask, to (x3,y3,z3).
+ *
+ * Using the doubling formulas in p256_double() on (x2,y2),
+ * simplifying since P2 is affine (i.e. z2 = 1, implicitly),
+ * we get:
+ * s = 4*x2*y2^2
+ * m = 3*(x2 + 1)*(x2 - 1)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y2^4
+ * z' = 2*y2
+ * which requires only 6 multiplications. Added to the 11
+ * multiplications of the normal mixed addition in Jacobian
+ * coordinates, we get a cost of 17 multiplications in total.
+ */
+ uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt, zz;
+ int i;
+
+ /*
+ * Set zz to -1 if P1 is the point at infinity, 0 otherwise.
+ */
+ zz = P1->z[0] | P1->z[1] | P1->z[2] | P1->z[3] | P1->z[4];
+ zz = ((zz | -zz) >> 63) - (uint64_t)1;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+
+ /*
+ * If both h = 0 and r = 0, then P1 = P2, and we want to set
+ * the mask tt to -1; otherwise, the mask will be 0.
+ */
+ f256_final_reduce(t2);
+ f256_final_reduce(t4);
+ tt = t2[0] | t2[1] | t2[2] | t2[3] | t2[4]
+ | t4[0] | t4[1] | t4[2] | t4[3] | t4[4];
+ tt = ((tt | -tt) >> 63) - (uint64_t)1;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1.
+ */
+ f256_montymul(P1->z, P1->z, t2);
+
+ /*
+ * The "double" result, in case P1 = P2.
+ */
+
+ /*
+ * Compute z' = 2*y2 (in t1).
+ */
+ f256_add(t1, P2->y, P2->y);
+ f256_partial_reduce(t1);
+
+ /*
+ * Compute 2*(y2^2) (in t2) and s = 4*x2*(y2^2) (in t3).
+ */
+ f256_montysquare(t2, P2->y);
+ f256_add(t2, t2, t2);
+ f256_add(t3, t2, t2);
+ f256_montymul(t3, P2->x, t3);
+
+ /*
+ * Compute m = 3*(x2^2 - 1) (in t4).
+ */
+ f256_montysquare(t4, P2->x);
+ f256_sub(t4, t4, F256_R);
+ f256_add(t5, t4, t4);
+ f256_add(t4, t4, t5);
+
+ /*
+ * Compute x' = m^2 - 2*s (in t5).
+ */
+ f256_montysquare(t5, t4);
+ f256_sub(t5, t3);
+ f256_sub(t5, t3);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y2^4 (in t6).
+ */
+ f256_sub(t6, t3, t5);
+ f256_montymul(t6, t6, t4);
+ f256_montysquare(t7, t2);
+ f256_sub(t6, t6, t7);
+ f256_sub(t6, t6, t7);
+
+ /*
+ * We now have the alternate (doubling) coordinates in (t5,t6,t1).
+ * We combine them with (x3,y3,z3).
+ */
+ for (i = 0; i < 5; i ++) {
+ P1->x[i] |= tt & t5[i];
+ P1->y[i] |= tt & t6[i];
+ P1->z[i] |= tt & t1[i];
+ }
+
+ /*
+ * If P1 = 0, then we get z3 = 0 (which is invalid); if z1 is 0,
+ * then we want to replace the result with a copy of P2. The
+ * test on z1 was done at the start, in the zz mask.
+ */
+ for (i = 0; i < 5; i ++) {
+ P1->x[i] ^= zz & (P1->x[i] ^ P2->x[i]);
+ P1->y[i] ^= zz & (P1->y[i] ^ P2->y[i]);
+ P1->z[i] ^= zz & (P1->z[i] ^ F256_R[i]);
+ }
+}
+#endif
+
+/*
+ * Inner function for computing a point multiplication. A window is
+ * provided, with points 1*P to 15*P in affine coordinates.
+ *
+ * Assumptions:
+ * - All provided points are valid points on the curve.
+ * - Multiplier is non-zero, and smaller than the curve order.
+ * - Everything is in Montgomery representation.
+ */
+static void
+point_mul_inner(p256_jacobian *R, const p256_affine *W,
+ const unsigned char *k, size_t klen)
+{
+ p256_jacobian Q;
+ uint32_t qz;
+
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (klen -- > 0) {
+ int i;
+ unsigned bk;
+
+ bk = *k ++;
+ for (i = 0; i < 2; i ++) {
+ uint32_t bits;
+ uint32_t bnz;
+ p256_affine T;
+ p256_jacobian U;
+ uint32_t n;
+ int j;
+ uint64_t m;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ bits = (bk >> 4) & 0x0F;
+ bnz = NEQ(bits, 0);
+
+ /*
+ * Lookup point in window. If the bits are 0,
+ * we get something invalid, which is not a
+ * problem because we will use it only if the
+ * bits are non-zero.
+ */
+ memset(&T, 0, sizeof T);
+ for (n = 0; n < 15; n ++) {
+ m = -(uint64_t)EQ(bits, n + 1);
+ T.x[0] |= m & W[n].x[0];
+ T.x[1] |= m & W[n].x[1];
+ T.x[2] |= m & W[n].x[2];
+ T.x[3] |= m & W[n].x[3];
+ T.x[4] |= m & W[n].x[4];
+ T.y[0] |= m & W[n].y[0];
+ T.y[1] |= m & W[n].y[1];
+ T.y[2] |= m & W[n].y[2];
+ T.y[3] |= m & W[n].y[3];
+ T.y[4] |= m & W[n].y[4];
+ }
+
+ U = Q;
+ p256_add_mixed(&U, &T);
+
+ /*
+ * If qz is still 1, then Q was all-zeros, and this
+ * is conserved through p256_double().
+ */
+ m = -(uint64_t)(bnz & qz);
+ for (j = 0; j < 5; j ++) {
+ Q.x[j] ^= m & (Q.x[j] ^ T.x[j]);
+ Q.y[j] ^= m & (Q.y[j] ^ T.y[j]);
+ Q.z[j] ^= m & (Q.z[j] ^ F256_R[j]);
+ }
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ bk <<= 4;
+ }
+ }
+ *R = Q;
+}
+
+/*
+ * Convert a window from Jacobian to affine coordinates. A single
+ * field inversion is used. This function works for windows up to
+ * 32 elements.
+ *
+ * The destination array (aff[]) and the source array (jac[]) may
+ * overlap, provided that the start of aff[] is not after the start of
+ * jac[]. Even if the arrays do _not_ overlap, the source array is
+ * modified.
+ */
+static void
+window_to_affine(p256_affine *aff, p256_jacobian *jac, int num)
+{
+ /*
+ * Convert the window points to affine coordinates. We use the
+ * following trick to mutualize the inversion computation: if
+ * we have z1, z2, z3, and z4, and want to invert all of them,
+ * we compute u = 1/(z1*z2*z3*z4), and then we have:
+ * 1/z1 = u*z2*z3*z4
+ * 1/z2 = u*z1*z3*z4
+ * 1/z3 = u*z1*z2*z4
+ * 1/z4 = u*z1*z2*z3
+ *
+ * The partial products are computed recursively:
+ *
+ * - on input (z_1,z_2), return (z_2,z_1) and z_1*z_2
+ * - on input (z_1,z_2,... z_n):
+ * recurse on (z_1,z_2,... z_(n/2)) -> r1 and m1
+ * recurse on (z_(n/2+1),z_(n/2+2)... z_n) -> r2 and m2
+ * multiply elements of r1 by m2 -> s1
+ * multiply elements of r2 by m1 -> s2
+ * return r1||r2 and m1*m2
+ *
+ * In the example below, we suppose that we have 14 elements.
+ * Let z1, z2,... zE be the 14 values to invert (index noted in
+ * hexadecimal, starting at 1).
+ *
+ * - Depth 1:
+ * swap(z1, z2); z12 = z1*z2
+ * swap(z3, z4); z34 = z3*z4
+ * swap(z5, z6); z56 = z5*z6
+ * swap(z7, z8); z78 = z7*z8
+ * swap(z9, zA); z9A = z9*zA
+ * swap(zB, zC); zBC = zB*zC
+ * swap(zD, zE); zDE = zD*zE
+ *
+ * - Depth 2:
+ * z1 <- z1*z34, z2 <- z2*z34, z3 <- z3*z12, z4 <- z4*z12
+ * z1234 = z12*z34
+ * z5 <- z5*z78, z6 <- z6*z78, z7 <- z7*z56, z8 <- z8*z56
+ * z5678 = z56*z78
+ * z9 <- z9*zBC, zA <- zA*zBC, zB <- zB*z9A, zC <- zC*z9A
+ * z9ABC = z9A*zBC
+ *
+ * - Depth 3:
+ * z1 <- z1*z5678, z2 <- z2*z5678, z3 <- z3*z5678, z4 <- z4*z5678
+ * z5 <- z5*z1234, z6 <- z6*z1234, z7 <- z7*z1234, z8 <- z8*z1234
+ * z12345678 = z1234*z5678
+ * z9 <- z9*zDE, zA <- zA*zDE, zB <- zB*zDE, zC <- zC*zDE
+ * zD <- zD*z9ABC, zE*z9ABC
+ * z9ABCDE = z9ABC*zDE
+ *
+ * - Depth 4:
+ * multiply z1..z8 by z9ABCDE
+ * multiply z9..zE by z12345678
+ * final z = z12345678*z9ABCDE
+ */
+
+ uint64_t z[16][5];
+ int i, k, s;
+#define zt (z[15])
+#define zu (z[14])
+#define zv (z[13])
+
+ /*
+ * First recursion step (pairwise swapping and multiplication).
+ * If there is an odd number of elements, then we "invent" an
+ * extra one with coordinate Z = 1 (in Montgomery representation).
+ */
+ for (i = 0; (i + 1) < num; i += 2) {
+ memcpy(zt, jac[i].z, sizeof zt);
+ memcpy(jac[i].z, jac[i + 1].z, sizeof zt);
+ memcpy(jac[i + 1].z, zt, sizeof zt);
+ f256_montymul(z[i >> 1], jac[i].z, jac[i + 1].z);
+ }
+ if ((num & 1) != 0) {
+ memcpy(z[num >> 1], jac[num - 1].z, sizeof zt);
+ memcpy(jac[num - 1].z, F256_R, sizeof F256_R);
+ }
+
+ /*
+ * Perform further recursion steps. At the entry of each step,
+ * the process has been done for groups of 's' points. The
+ * integer k is the log2 of s.
+ */
+ for (k = 1, s = 2; s < num; k ++, s <<= 1) {
+ int n;
+
+ for (i = 0; i < num; i ++) {
+ f256_montymul(jac[i].z, jac[i].z, z[(i >> k) ^ 1]);
+ }
+ n = (num + s - 1) >> k;
+ for (i = 0; i < (n >> 1); i ++) {
+ f256_montymul(z[i], z[i << 1], z[(i << 1) + 1]);
+ }
+ if ((n & 1) != 0) {
+ memmove(z[n >> 1], z[n], sizeof zt);
+ }
+ }
+
+ /*
+ * Invert the final result, and convert all points.
+ */
+ f256_invert(zt, z[0]);
+ for (i = 0; i < num; i ++) {
+ f256_montymul(zv, jac[i].z, zt);
+ f256_montysquare(zu, zv);
+ f256_montymul(zv, zv, zu);
+ f256_montymul(aff[i].x, jac[i].x, zu);
+ f256_montymul(aff[i].y, jac[i].y, zv);
+ }
+}
+
+/*
+ * Multiply the provided point by an integer.
+ * Assumptions:
+ * - Source point is a valid curve point.
+ * - Source point is not the point-at-infinity.
+ * - Integer is not 0, and is lower than the curve order.
+ * If these conditions are not met, then the result is indeterminate
+ * (but the process is still constant-time).
+ */
+static void
+p256_mul(p256_jacobian *P, const unsigned char *k, size_t klen)
+{
+ union {
+ p256_affine aff[15];
+ p256_jacobian jac[15];
+ } window;
+ int i;
+
+ /*
+ * Compute window, in Jacobian coordinates.
+ */
+ window.jac[0] = *P;
+ for (i = 2; i < 16; i ++) {
+ window.jac[i - 1] = window.jac[(i >> 1) - 1];
+ if ((i & 1) == 0) {
+ p256_double(&window.jac[i - 1]);
+ } else {
+ p256_add(&window.jac[i - 1], &window.jac[i >> 1]);
+ }
+ }
+
+ /*
+ * Convert the window points to affine coordinates. Point
+ * window[0] is the source point, already in affine coordinates.
+ */
+ window_to_affine(window.aff, window.jac, 15);
+
+ /*
+ * Perform point multiplication.
+ */
+ point_mul_inner(P, window.aff, k, klen);
+}
+
+/*
+ * Precomputed window for the conventional generator: P256_Gwin[n]
+ * contains (n+1)*G (affine coordinates, in Montgomery representation).
+ */
+static const p256_affine P256_Gwin[] = {
+ {
+ { 0x30D418A9143C1, 0xC4FEDB60179E7, 0x62251075BA95F,
+ 0x5C669FB732B77, 0x08905F76B5375 },
+ { 0x5357CE95560A8, 0x43A19E45CDDF2, 0x21F3258B4AB8E,
+ 0xD8552E88688DD, 0x0571FF18A5885 }
+ },
+ {
+ { 0x46D410DDD64DF, 0x0B433827D8500, 0x1490D9AA6AE3C,
+ 0xA3A832205038D, 0x06BB32E52DCF3 },
+ { 0x48D361BEE1A57, 0xB7B236FF82F36, 0x042DBE152CD7C,
+ 0xA3AA9A8FB0E92, 0x08C577517A5B8 }
+ },
+ {
+ { 0x3F904EEBC1272, 0x9E87D81FBFFAC, 0xCBBC98B027F84,
+ 0x47E46AD77DD87, 0x06936A3FD6FF7 },
+ { 0x5C1FC983A7EBD, 0xC3861FE1AB04C, 0x2EE98E583E47A,
+ 0xC06A88208311A, 0x05F06A2AB587C }
+ },
+ {
+ { 0xB50D46918DCC5, 0xD7623C17374B0, 0x100AF24650A6E,
+ 0x76ABCDAACACE8, 0x077362F591B01 },
+ { 0xF24CE4CBABA68, 0x17AD6F4472D96, 0xDDD22E1762847,
+ 0x862EB6C36DEE5, 0x04B14C39CC5AB }
+ },
+ {
+ { 0x8AAEC45C61F5C, 0x9D4B9537DBE1B, 0x76C20C90EC649,
+ 0x3C7D41CB5AAD0, 0x0907960649052 },
+ { 0x9B4AE7BA4F107, 0xF75EB882BEB30, 0x7A1F6873C568E,
+ 0x915C540A9877E, 0x03A076BB9DD1E }
+ },
+ {
+ { 0x47373E77664A1, 0xF246CEE3E4039, 0x17A3AD55AE744,
+ 0x673C50A961A5B, 0x03074B5964213 },
+ { 0x6220D377E44BA, 0x30DFF14B593D3, 0x639F11299C2B5,
+ 0x75F5424D44CEF, 0x04C9916DEA07F }
+ },
+ {
+ { 0x354EA0173B4F1, 0x3C23C00F70746, 0x23BB082BD2021,
+ 0xE03E43EAAB50C, 0x03BA5119D3123 },
+ { 0xD0303F5B9D4DE, 0x17DA67BDD2847, 0xC941956742F2F,
+ 0x8670F933BDC77, 0x0AEDD9164E240 }
+ },
+ {
+ { 0x4CD19499A78FB, 0x4BF9B345527F1, 0x2CFC6B462AB5C,
+ 0x30CDF90F02AF0, 0x0763891F62652 },
+ { 0xA3A9532D49775, 0xD7F9EBA15F59D, 0x60BBF021E3327,
+ 0xF75C23C7B84BE, 0x06EC12F2C706D }
+ },
+ {
+ { 0x6E8F264E20E8E, 0xC79A7A84175C9, 0xC8EB00ABE6BFE,
+ 0x16A4CC09C0444, 0x005B3081D0C4E },
+ { 0x777AA45F33140, 0xDCE5D45E31EB7, 0xB12F1A56AF7BE,
+ 0xF9B2B6E019A88, 0x086659CDFD835 }
+ },
+ {
+ { 0xDBD19DC21EC8C, 0x94FCF81392C18, 0x250B4998F9868,
+ 0x28EB37D2CD648, 0x0C61C947E4B34 },
+ { 0x407880DD9E767, 0x0C83FBE080C2B, 0x9BE5D2C43A899,
+ 0xAB4EF7D2D6577, 0x08719A555B3B4 }
+ },
+ {
+ { 0x260A6245E4043, 0x53E7FDFE0EA7D, 0xAC1AB59DE4079,
+ 0x072EFF3A4158D, 0x0E7090F1949C9 },
+ { 0x85612B944E886, 0xE857F61C81A76, 0xAD643D250F939,
+ 0x88DAC0DAA891E, 0x089300244125B }
+ },
+ {
+ { 0x1AA7D26977684, 0x58A345A3304B7, 0x37385EABDEDEF,
+ 0x155E409D29DEE, 0x0EE1DF780B83E },
+ { 0x12D91CBB5B437, 0x65A8956370CAC, 0xDE6D66170ED2F,
+ 0xAC9B8228CFA8A, 0x0FF57C95C3238 }
+ },
+ {
+ { 0x25634B2ED7097, 0x9156FD30DCCC4, 0x9E98110E35676,
+ 0x7594CBCD43F55, 0x038477ACC395B },
+ { 0x2B90C00EE17FF, 0xF842ED2E33575, 0x1F5BC16874838,
+ 0x7968CD06422BD, 0x0BC0876AB9E7B }
+ },
+ {
+ { 0xA35BB0CF664AF, 0x68F9707E3A242, 0x832660126E48F,
+ 0x72D2717BF54C6, 0x0AAE7333ED12C },
+ { 0x2DB7995D586B1, 0xE732237C227B5, 0x65E7DBBE29569,
+ 0xBBBD8E4193E2A, 0x052706DC3EAA1 }
+ },
+ {
+ { 0xD8B7BC60055BE, 0xD76E27E4B72BC, 0x81937003CC23E,
+ 0xA090E337424E4, 0x02AA0E43EAD3D },
+ { 0x524F6383C45D2, 0x422A41B2540B8, 0x8A4797D766355,
+ 0xDF444EFA6DE77, 0x0042170A9079A }
+ },
+};
+
+/*
+ * Multiply the conventional generator of the curve by the provided
+ * integer. Return is written in *P.
+ *
+ * Assumptions:
+ * - Integer is not 0, and is lower than the curve order.
+ * If this conditions is not met, then the result is indeterminate
+ * (but the process is still constant-time).
+ */
+static void
+p256_mulgen(p256_jacobian *P, const unsigned char *k, size_t klen)
+{
+ point_mul_inner(P, P256_Gwin, k, klen);
+}
+
+/*
+ * Return 1 if all of the following hold:
+ * - klen <= 32
+ * - k != 0
+ * - k is lower than the curve order
+ * Otherwise, return 0.
+ *
+ * Constant-time behaviour: only klen may be observable.
+ */
+static uint32_t
+check_scalar(const unsigned char *k, size_t klen)
+{
+ uint32_t z;
+ int32_t c;
+ size_t u;
+
+ if (klen > 32) {
+ return 0;
+ }
+ z = 0;
+ for (u = 0; u < klen; u ++) {
+ z |= k[u];
+ }
+ if (klen == 32) {
+ c = 0;
+ for (u = 0; u < klen; u ++) {
+ c |= -(int32_t)EQ0(c) & CMP(k[u], P256_N[u]);
+ }
+ } else {
+ c = -1;
+ }
+ return NEQ(z, 0) & LT0(c);
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *k, size_t klen, int curve)
+{
+ uint32_t r;
+ p256_jacobian P;
+
+ (void)curve;
+ if (Glen != 65) {
+ return 0;
+ }
+ r = check_scalar(k, klen);
+ r &= point_decode(&P, G);
+ p256_mul(&P, k, klen);
+ r &= point_encode(G, &P);
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *k, size_t klen, int curve)
+{
+ p256_jacobian P;
+
+ (void)curve;
+ p256_mulgen(&P, k, klen);
+ point_encode(R, &P);
+ return 65;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We might want to use Shamir's trick here: make a composite
+ * window of u*P+v*Q points, to merge the two doubling-ladders
+ * into one. This, however, has some complications:
+ *
+ * - During the computation, we may hit the point-at-infinity.
+ * Thus, we would need p256_add_complete_mixed() (complete
+ * formulas for point addition), with a higher cost (17 muls
+ * instead of 11).
+ *
+ * - A 4-bit window would be too large, since it would involve
+ * 16*16-1 = 255 points. For the same window size as in the
+ * p256_mul() case, we would need to reduce the window size
+ * to 2 bits, and thus perform twice as many non-doubling
+ * point additions.
+ *
+ * - The window may itself contain the point-at-infinity, and
+ * thus cannot be in all generality be made of affine points.
+ * Instead, we would need to make it a window of points in
+ * Jacobian coordinates. Even p256_add_complete_mixed() would
+ * be inappropriate.
+ *
+ * For these reasons, the code below performs two separate
+ * point multiplications, then computes the final point addition
+ * (which is both a "normal" addition, and a doubling, to handle
+ * all cases).
+ */
+
+ p256_jacobian P, Q;
+ uint32_t r, t, s;
+ uint64_t z;
+
+ (void)curve;
+ if (len != 65) {
+ return 0;
+ }
+ r = point_decode(&P, A);
+ p256_mul(&P, x, xlen);
+ if (B == NULL) {
+ p256_mulgen(&Q, y, ylen);
+ } else {
+ r &= point_decode(&Q, B);
+ p256_mul(&Q, y, ylen);
+ }
+
+ /*
+ * The final addition may fail in case both points are equal.
+ */
+ t = p256_add(&P, &Q);
+ f256_final_reduce(P.z);
+ z = P.z[0] | P.z[1] | P.z[2] | P.z[3] | P.z[4];
+ s = EQ((uint32_t)(z | (z >> 32)), 0);
+ p256_double(&Q);
+
+ /*
+ * If s is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * s = 0, t = 0 return P (normal addition)
+ * s = 0, t = 1 return P (normal addition)
+ * s = 1, t = 0 return Q (a 'double' case)
+ * s = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(s & ~t, &P, &Q, sizeof Q);
+ point_encode(A, &P);
+ r &= ~(s & t);
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_p256_m62 = {
+ (uint32_t)0x00800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_p256_m62_get(void)
+{
+ return &br_ec_p256_m62;
+}
+
+#else
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_p256_m62_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/ec/ec_p256_m64.c b/test/monniaux/BearSSL/src/ec/ec_p256_m64.c
new file mode 100644
index 00000000..5a7ea177
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_p256_m64.c
@@ -0,0 +1,1730 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_UMUL128
+#include <intrin.h>
+#endif
+
+static const unsigned char P256_G[] = {
+ 0x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,
+ 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,
+ 0x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,
+ 0x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,
+ 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,
+ 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,
+ 0x68, 0x37, 0xBF, 0x51, 0xF5
+};
+
+static const unsigned char P256_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,
+ 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,
+ 0x25, 0x51
+};
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_G;
+ return P256_G;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ (void)curve;
+ *len = sizeof P256_N;
+ return P256_N;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ (void)curve;
+ *len = 32;
+ return 1;
+}
+
+/*
+ * A field element is encoded as four 64-bit integers, in basis 2^64.
+ * Values may reach up to 2^256-1. Montgomery multiplication is used.
+ */
+
+/* R = 2^256 mod p */
+static const uint64_t F256_R[] = {
+ 0x0000000000000001, 0xFFFFFFFF00000000,
+ 0xFFFFFFFFFFFFFFFF, 0x00000000FFFFFFFE
+};
+
+/* Curve equation is y^2 = x^3 - 3*x + B. This constant is B*R mod p
+ (Montgomery representation of B). */
+static const uint64_t P256_B_MONTY[] = {
+ 0xD89CDF6229C4BDDF, 0xACF005CD78843090,
+ 0xE5A220ABF7212ED6, 0xDC30061D04874834
+};
+
+/*
+ * Addition in the field.
+ */
+static inline void
+f256_add(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+ unsigned __int128 w;
+ uint64_t t;
+
+ w = (unsigned __int128)a[0] + b[0];
+ d[0] = (uint64_t)w;
+ w = (unsigned __int128)a[1] + b[1] + (w >> 64);
+ d[1] = (uint64_t)w;
+ w = (unsigned __int128)a[2] + b[2] + (w >> 64);
+ d[2] = (uint64_t)w;
+ w = (unsigned __int128)a[3] + b[3] + (w >> 64);
+ d[3] = (uint64_t)w;
+ t = (uint64_t)(w >> 64);
+
+ /*
+ * 2^256 = 2^224 - 2^192 - 2^96 + 1 in the field.
+ */
+ w = (unsigned __int128)d[0] + t;
+ d[0] = (uint64_t)w;
+ w = (unsigned __int128)d[1] + (w >> 64) - (t << 32);
+ d[1] = (uint64_t)w;
+ /* Here, carry "w >> 64" can only be 0 or -1 */
+ w = (unsigned __int128)d[2] - ((w >> 64) & 1);
+ d[2] = (uint64_t)w;
+ /* Again, carry is 0 or -1 */
+ d[3] += (uint64_t)(w >> 64) + (t << 32) - t;
+
+#elif BR_UMUL128
+
+ unsigned char cc;
+ uint64_t t;
+
+ cc = _addcarry_u64(0, a[0], b[0], &d[0]);
+ cc = _addcarry_u64(cc, a[1], b[1], &d[1]);
+ cc = _addcarry_u64(cc, a[2], b[2], &d[2]);
+ cc = _addcarry_u64(cc, a[3], b[3], &d[3]);
+
+ /*
+ * If there is a carry, then we want to subtract p, which we
+ * do by adding 2^256 - p.
+ */
+ t = cc;
+ cc = _addcarry_u64(cc, d[0], 0, &d[0]);
+ cc = _addcarry_u64(cc, d[1], -(t << 32), &d[1]);
+ cc = _addcarry_u64(cc, d[2], -t, &d[2]);
+ (void)_addcarry_u64(cc, d[3], (t << 32) - (t << 1), &d[3]);
+
+#endif
+}
+
+/*
+ * Subtraction in the field.
+ */
+static inline void
+f256_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+
+ unsigned __int128 w;
+ uint64_t t;
+
+ w = (unsigned __int128)a[0] - b[0];
+ d[0] = (uint64_t)w;
+ w = (unsigned __int128)a[1] - b[1] - ((w >> 64) & 1);
+ d[1] = (uint64_t)w;
+ w = (unsigned __int128)a[2] - b[2] - ((w >> 64) & 1);
+ d[2] = (uint64_t)w;
+ w = (unsigned __int128)a[3] - b[3] - ((w >> 64) & 1);
+ d[3] = (uint64_t)w;
+ t = (uint64_t)(w >> 64) & 1;
+
+ /*
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1.
+ */
+ w = (unsigned __int128)d[0] - t;
+ d[0] = (uint64_t)w;
+ w = (unsigned __int128)d[1] + (t << 32) - ((w >> 64) & 1);
+ d[1] = (uint64_t)w;
+ /* Here, carry "w >> 64" can only be 0 or +1 */
+ w = (unsigned __int128)d[2] + (w >> 64);
+ d[2] = (uint64_t)w;
+ /* Again, carry is 0 or +1 */
+ d[3] += (uint64_t)(w >> 64) - (t << 32) + t;
+
+#elif BR_UMUL128
+
+ unsigned char cc;
+ uint64_t t;
+
+ cc = _subborrow_u64(0, a[0], b[0], &d[0]);
+ cc = _subborrow_u64(cc, a[1], b[1], &d[1]);
+ cc = _subborrow_u64(cc, a[2], b[2], &d[2]);
+ cc = _subborrow_u64(cc, a[3], b[3], &d[3]);
+
+ /*
+ * If there is a carry, then we need to add p.
+ */
+ t = cc;
+ cc = _addcarry_u64(0, d[0], -t, &d[0]);
+ cc = _addcarry_u64(cc, d[1], (-t) >> 32, &d[1]);
+ cc = _addcarry_u64(cc, d[2], 0, &d[2]);
+ (void)_addcarry_u64(cc, d[3], t - (t << 32), &d[3]);
+
+#endif
+}
+
+/*
+ * Montgomery multiplication in the field.
+ */
+static void
+f256_montymul(uint64_t *d, const uint64_t *a, const uint64_t *b)
+{
+#if BR_INT128
+
+ uint64_t x, f, t0, t1, t2, t3, t4;
+ unsigned __int128 z, ff;
+ int i;
+
+ /*
+ * When computing d <- d + a[u]*b, we also add f*p such
+ * that d + a[u]*b + f*p is a multiple of 2^64. Since
+ * p = -1 mod 2^64, we can compute f = d[0] + a[u]*b[0] mod 2^64.
+ */
+
+ /*
+ * Step 1: t <- (a[0]*b + f*p) / 2^64
+ * We have f = a[0]*b[0] mod 2^64. Since p = -1 mod 2^64, this
+ * ensures that (a[0]*b + f*p) is a multiple of 2^64.
+ *
+ * We also have: f*p = f*2^256 - f*2^224 + f*2^192 + f*2^96 - f.
+ */
+ x = a[0];
+ z = (unsigned __int128)b[0] * x;
+ f = (uint64_t)z;
+ z = (unsigned __int128)b[1] * x + (z >> 64) + (uint64_t)(f << 32);
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)b[2] * x + (z >> 64) + (uint64_t)(f >> 32);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)b[3] * x + (z >> 64) + f;
+ t2 = (uint64_t)z;
+ t3 = (uint64_t)(z >> 64);
+ ff = ((unsigned __int128)f << 64) - ((unsigned __int128)f << 32);
+ z = (unsigned __int128)t2 + (uint64_t)ff;
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)t3 + (z >> 64) + (ff >> 64);
+ t3 = (uint64_t)z;
+ t4 = (uint64_t)(z >> 64);
+
+ /*
+ * Steps 2 to 4: t <- (t + a[i]*b + f*p) / 2^64
+ */
+ for (i = 1; i < 4; i ++) {
+ x = a[i];
+
+ /* t <- (t + x*b - f) / 2^64 */
+ z = (unsigned __int128)b[0] * x + t0;
+ f = (uint64_t)z;
+ z = (unsigned __int128)b[1] * x + t1 + (z >> 64);
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)b[2] * x + t2 + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)b[3] * x + t3 + (z >> 64);
+ t2 = (uint64_t)z;
+ z = t4 + (z >> 64);
+ t3 = (uint64_t)z;
+ t4 = (uint64_t)(z >> 64);
+
+ /* t <- t + f*2^32, carry in the upper half of z */
+ z = (unsigned __int128)t0 + (uint64_t)(f << 32);
+ t0 = (uint64_t)z;
+ z = (z >> 64) + (unsigned __int128)t1 + (uint64_t)(f >> 32);
+ t1 = (uint64_t)z;
+
+ /* t <- t + f*2^192 - f*2^160 + f*2^128 */
+ ff = ((unsigned __int128)f << 64)
+ - ((unsigned __int128)f << 32) + f;
+ z = (z >> 64) + (unsigned __int128)t2 + (uint64_t)ff;
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)t3 + (z >> 64) + (ff >> 64);
+ t3 = (uint64_t)z;
+ t4 += (uint64_t)(z >> 64);
+ }
+
+ /*
+ * At that point, we have computed t = (a*b + F*p) / 2^256, where
+ * F is a 256-bit integer whose limbs are the "f" coefficients
+ * in the steps above. We have:
+ * a <= 2^256-1
+ * b <= 2^256-1
+ * F <= 2^256-1
+ * Hence:
+ * a*b + F*p <= (2^256-1)*(2^256-1) + p*(2^256-1)
+ * a*b + F*p <= 2^256*(2^256 - 2 + p) + 1 - p
+ * Therefore:
+ * t < 2^256 + p - 2
+ * Since p < 2^256, it follows that:
+ * t4 can be only 0 or 1
+ * t - p < 2^256
+ * We can therefore subtract p from t, conditionally on t4, to
+ * get a nonnegative result that fits on 256 bits.
+ */
+ z = (unsigned __int128)t0 + t4;
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)t1 - (t4 << 32) + (z >> 64);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)t2 - (z >> 127);
+ t2 = (uint64_t)z;
+ t3 = t3 - (uint64_t)(z >> 127) - t4 + (t4 << 32);
+
+ d[0] = t0;
+ d[1] = t1;
+ d[2] = t2;
+ d[3] = t3;
+
+#elif BR_UMUL128
+
+ uint64_t x, f, t0, t1, t2, t3, t4;
+ uint64_t zl, zh, ffl, ffh;
+ unsigned char k, m;
+ int i;
+
+ /*
+ * When computing d <- d + a[u]*b, we also add f*p such
+ * that d + a[u]*b + f*p is a multiple of 2^64. Since
+ * p = -1 mod 2^64, we can compute f = d[0] + a[u]*b[0] mod 2^64.
+ */
+
+ /*
+ * Step 1: t <- (a[0]*b + f*p) / 2^64
+ * We have f = a[0]*b[0] mod 2^64. Since p = -1 mod 2^64, this
+ * ensures that (a[0]*b + f*p) is a multiple of 2^64.
+ *
+ * We also have: f*p = f*2^256 - f*2^224 + f*2^192 + f*2^96 - f.
+ */
+ x = a[0];
+
+ zl = _umul128(b[0], x, &zh);
+ f = zl;
+ t0 = zh;
+
+ zl = _umul128(b[1], x, &zh);
+ k = _addcarry_u64(0, zl, t0, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, f << 32, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ t0 = zl;
+ t1 = zh;
+
+ zl = _umul128(b[2], x, &zh);
+ k = _addcarry_u64(0, zl, t1, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, f >> 32, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ t1 = zl;
+ t2 = zh;
+
+ zl = _umul128(b[3], x, &zh);
+ k = _addcarry_u64(0, zl, t2, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, f, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ t2 = zl;
+ t3 = zh;
+
+ t4 = _addcarry_u64(0, t3, f, &t3);
+ k = _subborrow_u64(0, t2, f << 32, &t2);
+ k = _subborrow_u64(k, t3, f >> 32, &t3);
+ (void)_subborrow_u64(k, t4, 0, &t4);
+
+ /*
+ * Steps 2 to 4: t <- (t + a[i]*b + f*p) / 2^64
+ */
+ for (i = 1; i < 4; i ++) {
+ x = a[i];
+ /* f = t0 + x * b[0]; -- computed below */
+
+ /* t <- (t + x*b - f) / 2^64 */
+ zl = _umul128(b[0], x, &zh);
+ k = _addcarry_u64(0, zl, t0, &f);
+ (void)_addcarry_u64(k, zh, 0, &t0);
+
+ zl = _umul128(b[1], x, &zh);
+ k = _addcarry_u64(0, zl, t0, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, t1, &t0);
+ (void)_addcarry_u64(k, zh, 0, &t1);
+
+ zl = _umul128(b[2], x, &zh);
+ k = _addcarry_u64(0, zl, t1, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, t2, &t1);
+ (void)_addcarry_u64(k, zh, 0, &t2);
+
+ zl = _umul128(b[3], x, &zh);
+ k = _addcarry_u64(0, zl, t2, &zl);
+ (void)_addcarry_u64(k, zh, 0, &zh);
+ k = _addcarry_u64(0, zl, t3, &t2);
+ (void)_addcarry_u64(k, zh, 0, &t3);
+
+ t4 = _addcarry_u64(0, t3, t4, &t3);
+
+ /* t <- t + f*2^32, carry in k */
+ k = _addcarry_u64(0, t0, f << 32, &t0);
+ k = _addcarry_u64(k, t1, f >> 32, &t1);
+
+ /* t <- t + f*2^192 - f*2^160 + f*2^128 */
+ m = _subborrow_u64(0, f, f << 32, &ffl);
+ (void)_subborrow_u64(m, f, f >> 32, &ffh);
+ k = _addcarry_u64(k, t2, ffl, &t2);
+ k = _addcarry_u64(k, t3, ffh, &t3);
+ (void)_addcarry_u64(k, t4, 0, &t4);
+ }
+
+ /*
+ * At that point, we have computed t = (a*b + F*p) / 2^256, where
+ * F is a 256-bit integer whose limbs are the "f" coefficients
+ * in the steps above. We have:
+ * a <= 2^256-1
+ * b <= 2^256-1
+ * F <= 2^256-1
+ * Hence:
+ * a*b + F*p <= (2^256-1)*(2^256-1) + p*(2^256-1)
+ * a*b + F*p <= 2^256*(2^256 - 2 + p) + 1 - p
+ * Therefore:
+ * t < 2^256 + p - 2
+ * Since p < 2^256, it follows that:
+ * t4 can be only 0 or 1
+ * t - p < 2^256
+ * We can therefore subtract p from t, conditionally on t4, to
+ * get a nonnegative result that fits on 256 bits.
+ */
+ k = _addcarry_u64(0, t0, t4, &t0);
+ k = _addcarry_u64(k, t1, -(t4 << 32), &t1);
+ k = _addcarry_u64(k, t2, -t4, &t2);
+ (void)_addcarry_u64(k, t3, (t4 << 32) - (t4 << 1), &t3);
+
+ d[0] = t0;
+ d[1] = t1;
+ d[2] = t2;
+ d[3] = t3;
+
+#endif
+}
+
+/*
+ * Montgomery squaring in the field; currently a basic wrapper around
+ * multiplication (inline, should be optimized away).
+ * TODO: see if some extra speed can be gained here.
+ */
+static inline void
+f256_montysquare(uint64_t *d, const uint64_t *a)
+{
+ f256_montymul(d, a, a);
+}
+
+/*
+ * Convert to Montgomery representation.
+ */
+static void
+f256_tomonty(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * R2 = 2^512 mod p.
+ * If R = 2^256 mod p, then R2 = R^2 mod p; and the Montgomery
+ * multiplication of a by R2 is: a*R2/R = a*R mod p, i.e. the
+ * conversion to Montgomery representation.
+ */
+ static const uint64_t R2[] = {
+ 0x0000000000000003,
+ 0xFFFFFFFBFFFFFFFF,
+ 0xFFFFFFFFFFFFFFFE,
+ 0x00000004FFFFFFFD
+ };
+
+ f256_montymul(d, a, R2);
+}
+
+/*
+ * Convert from Montgomery representation.
+ */
+static void
+f256_frommonty(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * Montgomery multiplication by 1 is division by 2^256 modulo p.
+ */
+ static const uint64_t one[] = { 1, 0, 0, 0 };
+
+ f256_montymul(d, a, one);
+}
+
+/*
+ * Inversion in the field. If the source value is 0 modulo p, then this
+ * returns 0 or p. This function uses Montgomery representation.
+ */
+static void
+f256_invert(uint64_t *d, const uint64_t *a)
+{
+ /*
+ * We compute a^(p-2) mod p. The exponent pattern (from high to
+ * low) is:
+ * - 32 bits of value 1
+ * - 31 bits of value 0
+ * - 1 bit of value 1
+ * - 96 bits of value 0
+ * - 94 bits of value 1
+ * - 1 bit of value 0
+ * - 1 bit of value 1
+ * To speed up the square-and-multiply algorithm, we precompute
+ * a^(2^31-1).
+ */
+
+ uint64_t r[4], t[4];
+ int i;
+
+ memcpy(t, a, sizeof t);
+ for (i = 0; i < 30; i ++) {
+ f256_montysquare(t, t);
+ f256_montymul(t, t, a);
+ }
+
+ memcpy(r, t, sizeof t);
+ for (i = 224; i >= 0; i --) {
+ f256_montysquare(r, r);
+ switch (i) {
+ case 0:
+ case 2:
+ case 192:
+ case 224:
+ f256_montymul(r, r, a);
+ break;
+ case 3:
+ case 34:
+ case 65:
+ f256_montymul(r, r, t);
+ break;
+ }
+ }
+ memcpy(d, r, sizeof r);
+}
+
+/*
+ * Finalize reduction.
+ * Input value fits on 256 bits. This function subtracts p if and only
+ * if the input is greater than or equal to p.
+ */
+static inline void
+f256_final_reduce(uint64_t *a)
+{
+#if BR_INT128
+
+ uint64_t t0, t1, t2, t3, cc;
+ unsigned __int128 z;
+
+ /*
+ * We add 2^224 - 2^192 - 2^96 + 1 to a. If there is no carry,
+ * then a < p; otherwise, the addition result we computed is
+ * the value we must return.
+ */
+ z = (unsigned __int128)a[0] + 1;
+ t0 = (uint64_t)z;
+ z = (unsigned __int128)a[1] + (z >> 64) - ((uint64_t)1 << 32);
+ t1 = (uint64_t)z;
+ z = (unsigned __int128)a[2] - (z >> 127);
+ t2 = (uint64_t)z;
+ z = (unsigned __int128)a[3] - (z >> 127) + 0xFFFFFFFF;
+ t3 = (uint64_t)z;
+ cc = -(uint64_t)(z >> 64);
+
+ a[0] ^= cc & (a[0] ^ t0);
+ a[1] ^= cc & (a[1] ^ t1);
+ a[2] ^= cc & (a[2] ^ t2);
+ a[3] ^= cc & (a[3] ^ t3);
+
+#elif BR_UMUL128
+
+ uint64_t t0, t1, t2, t3, m;
+ unsigned char k;
+
+ k = _addcarry_u64(0, a[0], (uint64_t)1, &t0);
+ k = _addcarry_u64(k, a[1], -((uint64_t)1 << 32), &t1);
+ k = _addcarry_u64(k, a[2], -(uint64_t)1, &t2);
+ k = _addcarry_u64(k, a[3], ((uint64_t)1 << 32) - 2, &t3);
+ m = -(uint64_t)k;
+
+ a[0] ^= m & (a[0] ^ t0);
+ a[1] ^= m & (a[1] ^ t1);
+ a[2] ^= m & (a[2] ^ t2);
+ a[3] ^= m & (a[3] ^ t3);
+
+#endif
+}
+
+/*
+ * Points in affine and Jacobian coordinates.
+ *
+ * - In affine coordinates, the point-at-infinity cannot be encoded.
+ * - Jacobian coordinates (X,Y,Z) correspond to affine (X/Z^2,Y/Z^3);
+ * if Z = 0 then this is the point-at-infinity.
+ */
+typedef struct {
+ uint64_t x[4];
+ uint64_t y[4];
+} p256_affine;
+
+typedef struct {
+ uint64_t x[4];
+ uint64_t y[4];
+ uint64_t z[4];
+} p256_jacobian;
+
+/*
+ * Decode a point. The returned point is in Jacobian coordinates, but
+ * with z = 1. If the encoding is invalid, or encodes a point which is
+ * not on the curve, or encodes the point at infinity, then this function
+ * returns 0. Otherwise, 1 is returned.
+ *
+ * The buffer is assumed to have length exactly 65 bytes.
+ */
+static uint32_t
+point_decode(p256_jacobian *P, const unsigned char *buf)
+{
+ uint64_t x[4], y[4], t[4], x3[4], tt;
+ uint32_t r;
+
+ /*
+ * Header byte shall be 0x04.
+ */
+ r = EQ(buf[0], 0x04);
+
+ /*
+ * Decode X and Y coordinates, and convert them into
+ * Montgomery representation.
+ */
+ x[3] = br_dec64be(buf + 1);
+ x[2] = br_dec64be(buf + 9);
+ x[1] = br_dec64be(buf + 17);
+ x[0] = br_dec64be(buf + 25);
+ y[3] = br_dec64be(buf + 33);
+ y[2] = br_dec64be(buf + 41);
+ y[1] = br_dec64be(buf + 49);
+ y[0] = br_dec64be(buf + 57);
+ f256_tomonty(x, x);
+ f256_tomonty(y, y);
+
+ /*
+ * Verify y^2 = x^3 + A*x + B. In curve P-256, A = -3.
+ * Note that the Montgomery representation of 0 is 0. We must
+ * take care to apply the final reduction to make sure we have
+ * 0 and not p.
+ */
+ f256_montysquare(t, y);
+ f256_montysquare(x3, x);
+ f256_montymul(x3, x3, x);
+ f256_sub(t, t, x3);
+ f256_add(t, t, x);
+ f256_add(t, t, x);
+ f256_add(t, t, x);
+ f256_sub(t, t, P256_B_MONTY);
+ f256_final_reduce(t);
+ tt = t[0] | t[1] | t[2] | t[3];
+ r &= EQ((uint32_t)(tt | (tt >> 32)), 0);
+
+ /*
+ * Return the point in Jacobian coordinates (and Montgomery
+ * representation).
+ */
+ memcpy(P->x, x, sizeof x);
+ memcpy(P->y, y, sizeof y);
+ memcpy(P->z, F256_R, sizeof F256_R);
+ return r;
+}
+
+/*
+ * Final conversion for a point:
+ * - The point is converted back to affine coordinates.
+ * - Final reduction is performed.
+ * - The point is encoded into the provided buffer.
+ *
+ * If the point is the point-at-infinity, all operations are performed,
+ * but the buffer contents are indeterminate, and 0 is returned. Otherwise,
+ * the encoded point is written in the buffer, and 1 is returned.
+ */
+static uint32_t
+point_encode(unsigned char *buf, const p256_jacobian *P)
+{
+ uint64_t t1[4], t2[4], z;
+
+ /* Set t1 = 1/z^2 and t2 = 1/z^3. */
+ f256_invert(t2, P->z);
+ f256_montysquare(t1, t2);
+ f256_montymul(t2, t2, t1);
+
+ /* Compute affine coordinates x (in t1) and y (in t2). */
+ f256_montymul(t1, P->x, t1);
+ f256_montymul(t2, P->y, t2);
+
+ /* Convert back from Montgomery representation, and finalize
+ reductions. */
+ f256_frommonty(t1, t1);
+ f256_frommonty(t2, t2);
+ f256_final_reduce(t1);
+ f256_final_reduce(t2);
+
+ /* Encode. */
+ buf[0] = 0x04;
+ br_enc64be(buf + 1, t1[3]);
+ br_enc64be(buf + 9, t1[2]);
+ br_enc64be(buf + 17, t1[1]);
+ br_enc64be(buf + 25, t1[0]);
+ br_enc64be(buf + 33, t2[3]);
+ br_enc64be(buf + 41, t2[2]);
+ br_enc64be(buf + 49, t2[1]);
+ br_enc64be(buf + 57, t2[0]);
+
+ /* Return success if and only if P->z != 0. */
+ z = P->z[0] | P->z[1] | P->z[2] | P->z[3];
+ return NEQ((uint32_t)(z | z >> 32), 0);
+}
+
+/*
+ * Point doubling in Jacobian coordinates: point P is doubled.
+ * Note: if the source point is the point-at-infinity, then the result is
+ * still the point-at-infinity, which is correct. Moreover, if the three
+ * coordinates were zero, then they still are zero in the returned value.
+ *
+ * (Note: this is true even without the final reduction: if the three
+ * coordinates are encoded as four words of value zero each, then the
+ * result will also have all-zero coordinate encodings, not the alternate
+ * encoding as the integer p.)
+ */
+static void
+p256_double(p256_jacobian *P)
+{
+ /*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * These formulas work for all points, including points of order 2
+ * and points at infinity:
+ * - If y = 0 then z' = 0. But there is no such point in P-256
+ * anyway.
+ * - If z = 0 then z' = 0.
+ */
+ uint64_t t1[4], t2[4], t3[4], t4[4];
+
+ /*
+ * Compute z^2 in t1.
+ */
+ f256_montysquare(t1, P->z);
+
+ /*
+ * Compute x-z^2 in t2 and x+z^2 in t1.
+ */
+ f256_add(t2, P->x, t1);
+ f256_sub(t1, P->x, t1);
+
+ /*
+ * Compute 3*(x+z^2)*(x-z^2) in t1.
+ */
+ f256_montymul(t3, t1, t2);
+ f256_add(t1, t3, t3);
+ f256_add(t1, t3, t1);
+
+ /*
+ * Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ f256_montysquare(t3, P->y);
+ f256_add(t3, t3, t3);
+ f256_montymul(t2, P->x, t3);
+ f256_add(t2, t2, t2);
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ f256_montysquare(P->x, t1);
+ f256_sub(P->x, P->x, t2);
+ f256_sub(P->x, P->x, t2);
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ f256_montymul(t4, P->y, P->z);
+ f256_add(P->z, t4, t4);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ f256_sub(t2, t2, P->x);
+ f256_montymul(P->y, t1, t2);
+ f256_montysquare(t4, t3);
+ f256_add(t4, t4, t4);
+ f256_sub(P->y, P->y, t4);
+}
+
+/*
+ * Point addition (Jacobian coordinates): P1 is replaced with P1+P2.
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0 but P2 != 0
+ * - If P1 != 0 but P2 == 0
+ * - If P1 == P2
+ *
+ * In all three cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y coordinate.
+ * - P1 == 0 and P2 == 0.
+ * - The Y coordinate of one of the points is 0 and the other point is
+ * the point at infinity.
+ *
+ * The third case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ *
+ * Note that you can get a returned value of 0 with a correct result,
+ * e.g. if P1 and P2 have the same Y coordinate, but distinct X coordinates.
+ */
+static uint32_t
+p256_add(p256_jacobian *P1, const p256_jacobian *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ */
+ uint64_t t1[4], t2[4], t3[4], t4[4], t5[4], t6[4], t7[4], tt;
+ uint32_t ret;
+
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ f256_montysquare(t3, P2->z);
+ f256_montymul(t1, P1->x, t3);
+ f256_montymul(t4, P2->z, t3);
+ f256_montymul(t3, P1->y, t4);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+ f256_final_reduce(t4);
+ tt = t4[0] | t4[1] | t4[2] | t4[3];
+ ret = (uint32_t)(tt | (tt >> 32));
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ f256_montymul(t1, P1->z, P2->z);
+ f256_montymul(P1->z, t1, t2);
+
+ return ret;
+}
+
+/*
+ * Point addition (mixed coordinates): P1 is replaced with P1+P2.
+ * This is a specialised function for the case when P2 is a non-zero point
+ * in affine coordinates.
+ *
+ * This function computes the wrong result in the following cases:
+ *
+ * - If P1 == 0
+ * - If P1 == P2
+ *
+ * In both cases, P1 is set to the point at infinity.
+ *
+ * Returned value is 0 if one of the following occurs:
+ *
+ * - P1 and P2 have the same Y (affine) coordinate.
+ * - The Y coordinate of P2 is 0 and P1 is the point at infinity.
+ *
+ * The second case cannot actually happen with valid points, since a point
+ * with Y == 0 is a point of order 2, and there is no point of order 2 on
+ * curve P-256.
+ *
+ * Therefore, assuming that P1 != 0 on input, then the caller
+ * can apply the following:
+ *
+ * - If the result is not the point at infinity, then it is correct.
+ * - Otherwise, if the returned value is 1, then this is a case of
+ * P1+P2 == 0, so the result is indeed the point at infinity.
+ * - Otherwise, P1 == P2, so a "double" operation should have been
+ * performed.
+ *
+ * Again, a value of 0 may be returned in some cases where the addition
+ * result is correct.
+ */
+static uint32_t
+p256_add_mixed(p256_jacobian *P1, const p256_affine *P2)
+{
+ /*
+ * Addtions formulas are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ */
+ uint64_t t1[4], t2[4], t3[4], t4[4], t5[4], t6[4], t7[4], tt;
+ uint32_t ret;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * We need to test whether r is zero, so we will do some extra
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+ f256_final_reduce(t4);
+ tt = t4[0] | t4[1] | t4[2] | t4[3];
+ ret = (uint32_t)(tt | (tt >> 32));
+ ret = (ret | -ret) >> 31;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ f256_montymul(P1->z, P1->z, t2);
+
+ return ret;
+}
+
+#if 0
+/* unused */
+/*
+ * Point addition (mixed coordinates, complete): P1 is replaced with P1+P2.
+ * This is a specialised function for the case when P2 is a non-zero point
+ * in affine coordinates.
+ *
+ * This function returns the correct result in all cases.
+ */
+static uint32_t
+p256_add_complete_mixed(p256_jacobian *P1, const p256_affine *P2)
+{
+ /*
+ * Addtions formulas, in the general case, are:
+ *
+ * u1 = x1
+ * u2 = x2 * z1^2
+ * s1 = y1
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1
+ *
+ * These formulas mishandle the two following cases:
+ *
+ * - If P1 is the point-at-infinity (z1 = 0), then z3 is
+ * incorrectly set to 0.
+ *
+ * - If P1 = P2, then u1 = u2 and s1 = s2, and x3, y3 and z3
+ * are all set to 0.
+ *
+ * However, if P1 + P2 = 0, then u1 = u2 but s1 != s2, and then
+ * we correctly get z3 = 0 (the point-at-infinity).
+ *
+ * To fix the case P1 = 0, we perform at the end a copy of P2
+ * over P1, conditional to z1 = 0.
+ *
+ * For P1 = P2: in that case, both h and r are set to 0, and
+ * we get x3, y3 and z3 equal to 0. We can test for that
+ * occurrence to make a mask which will be all-one if P1 = P2,
+ * or all-zero otherwise; then we can compute the double of P2
+ * and add it, combined with the mask, to (x3,y3,z3).
+ *
+ * Using the doubling formulas in p256_double() on (x2,y2),
+ * simplifying since P2 is affine (i.e. z2 = 1, implicitly),
+ * we get:
+ * s = 4*x2*y2^2
+ * m = 3*(x2 + 1)*(x2 - 1)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y2^4
+ * z' = 2*y2
+ * which requires only 6 multiplications. Added to the 11
+ * multiplications of the normal mixed addition in Jacobian
+ * coordinates, we get a cost of 17 multiplications in total.
+ */
+ uint64_t t1[4], t2[4], t3[4], t4[4], t5[4], t6[4], t7[4], tt, zz;
+ int i;
+
+ /*
+ * Set zz to -1 if P1 is the point at infinity, 0 otherwise.
+ */
+ zz = P1->z[0] | P1->z[1] | P1->z[2] | P1->z[3];
+ zz = ((zz | -zz) >> 63) - (uint64_t)1;
+
+ /*
+ * Compute u1 = x1 (in t1) and s1 = y1 (in t3).
+ */
+ memcpy(t1, P1->x, sizeof t1);
+ memcpy(t3, P1->y, sizeof t3);
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ f256_montysquare(t4, P1->z);
+ f256_montymul(t2, P2->x, t4);
+ f256_montymul(t5, P1->z, t4);
+ f256_montymul(t4, P2->y, t5);
+
+ /*
+ * Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).
+ * reduce.
+ */
+ f256_sub(t2, t2, t1);
+ f256_sub(t4, t4, t3);
+
+ /*
+ * If both h = 0 and r = 0, then P1 = P2, and we want to set
+ * the mask tt to -1; otherwise, the mask will be 0.
+ */
+ f256_final_reduce(t2);
+ f256_final_reduce(t4);
+ tt = t2[0] | t2[1] | t2[2] | t2[3] | t4[0] | t4[1] | t4[2] | t4[3];
+ tt = ((tt | -tt) >> 63) - (uint64_t)1;
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5);
+ */
+ f256_montysquare(t7, t2);
+ f256_montymul(t6, t1, t7);
+ f256_montymul(t5, t7, t2);
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ */
+ f256_montysquare(P1->x, t4);
+ f256_sub(P1->x, P1->x, t5);
+ f256_sub(P1->x, P1->x, t6);
+ f256_sub(P1->x, P1->x, t6);
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ f256_sub(t6, t6, P1->x);
+ f256_montymul(P1->y, t4, t6);
+ f256_montymul(t1, t5, t3);
+ f256_sub(P1->y, P1->y, t1);
+
+ /*
+ * Compute z3 = h*z1.
+ */
+ f256_montymul(P1->z, P1->z, t2);
+
+ /*
+ * The "double" result, in case P1 = P2.
+ */
+
+ /*
+ * Compute z' = 2*y2 (in t1).
+ */
+ f256_add(t1, P2->y, P2->y);
+
+ /*
+ * Compute 2*(y2^2) (in t2) and s = 4*x2*(y2^2) (in t3).
+ */
+ f256_montysquare(t2, P2->y);
+ f256_add(t2, t2, t2);
+ f256_add(t3, t2, t2);
+ f256_montymul(t3, P2->x, t3);
+
+ /*
+ * Compute m = 3*(x2^2 - 1) (in t4).
+ */
+ f256_montysquare(t4, P2->x);
+ f256_sub(t4, t4, F256_R);
+ f256_add(t5, t4, t4);
+ f256_add(t4, t4, t5);
+
+ /*
+ * Compute x' = m^2 - 2*s (in t5).
+ */
+ f256_montysquare(t5, t4);
+ f256_sub(t5, t3);
+ f256_sub(t5, t3);
+
+ /*
+ * Compute y' = m*(s - x') - 8*y2^4 (in t6).
+ */
+ f256_sub(t6, t3, t5);
+ f256_montymul(t6, t6, t4);
+ f256_montysquare(t7, t2);
+ f256_sub(t6, t6, t7);
+ f256_sub(t6, t6, t7);
+
+ /*
+ * We now have the alternate (doubling) coordinates in (t5,t6,t1).
+ * We combine them with (x3,y3,z3).
+ */
+ for (i = 0; i < 4; i ++) {
+ P1->x[i] |= tt & t5[i];
+ P1->y[i] |= tt & t6[i];
+ P1->z[i] |= tt & t1[i];
+ }
+
+ /*
+ * If P1 = 0, then we get z3 = 0 (which is invalid); if z1 is 0,
+ * then we want to replace the result with a copy of P2. The
+ * test on z1 was done at the start, in the zz mask.
+ */
+ for (i = 0; i < 4; i ++) {
+ P1->x[i] ^= zz & (P1->x[i] ^ P2->x[i]);
+ P1->y[i] ^= zz & (P1->y[i] ^ P2->y[i]);
+ P1->z[i] ^= zz & (P1->z[i] ^ F256_R[i]);
+ }
+}
+#endif
+
+/*
+ * Inner function for computing a point multiplication. A window is
+ * provided, with points 1*P to 15*P in affine coordinates.
+ *
+ * Assumptions:
+ * - All provided points are valid points on the curve.
+ * - Multiplier is non-zero, and smaller than the curve order.
+ * - Everything is in Montgomery representation.
+ */
+static void
+point_mul_inner(p256_jacobian *R, const p256_affine *W,
+ const unsigned char *k, size_t klen)
+{
+ p256_jacobian Q;
+ uint32_t qz;
+
+ memset(&Q, 0, sizeof Q);
+ qz = 1;
+ while (klen -- > 0) {
+ int i;
+ unsigned bk;
+
+ bk = *k ++;
+ for (i = 0; i < 2; i ++) {
+ uint32_t bits;
+ uint32_t bnz;
+ p256_affine T;
+ p256_jacobian U;
+ uint32_t n;
+ int j;
+ uint64_t m;
+
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ p256_double(&Q);
+ bits = (bk >> 4) & 0x0F;
+ bnz = NEQ(bits, 0);
+
+ /*
+ * Lookup point in window. If the bits are 0,
+ * we get something invalid, which is not a
+ * problem because we will use it only if the
+ * bits are non-zero.
+ */
+ memset(&T, 0, sizeof T);
+ for (n = 0; n < 15; n ++) {
+ m = -(uint64_t)EQ(bits, n + 1);
+ T.x[0] |= m & W[n].x[0];
+ T.x[1] |= m & W[n].x[1];
+ T.x[2] |= m & W[n].x[2];
+ T.x[3] |= m & W[n].x[3];
+ T.y[0] |= m & W[n].y[0];
+ T.y[1] |= m & W[n].y[1];
+ T.y[2] |= m & W[n].y[2];
+ T.y[3] |= m & W[n].y[3];
+ }
+
+ U = Q;
+ p256_add_mixed(&U, &T);
+
+ /*
+ * If qz is still 1, then Q was all-zeros, and this
+ * is conserved through p256_double().
+ */
+ m = -(uint64_t)(bnz & qz);
+ for (j = 0; j < 4; j ++) {
+ Q.x[j] |= m & T.x[j];
+ Q.y[j] |= m & T.y[j];
+ Q.z[j] |= m & F256_R[j];
+ }
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ bk <<= 4;
+ }
+ }
+ *R = Q;
+}
+
+/*
+ * Convert a window from Jacobian to affine coordinates. A single
+ * field inversion is used. This function works for windows up to
+ * 32 elements.
+ *
+ * The destination array (aff[]) and the source array (jac[]) may
+ * overlap, provided that the start of aff[] is not after the start of
+ * jac[]. Even if the arrays do _not_ overlap, the source array is
+ * modified.
+ */
+static void
+window_to_affine(p256_affine *aff, p256_jacobian *jac, int num)
+{
+ /*
+ * Convert the window points to affine coordinates. We use the
+ * following trick to mutualize the inversion computation: if
+ * we have z1, z2, z3, and z4, and want to inverse all of them,
+ * we compute u = 1/(z1*z2*z3*z4), and then we have:
+ * 1/z1 = u*z2*z3*z4
+ * 1/z2 = u*z1*z3*z4
+ * 1/z3 = u*z1*z2*z4
+ * 1/z4 = u*z1*z2*z3
+ *
+ * The partial products are computed recursively:
+ *
+ * - on input (z_1,z_2), return (z_2,z_1) and z_1*z_2
+ * - on input (z_1,z_2,... z_n):
+ * recurse on (z_1,z_2,... z_(n/2)) -> r1 and m1
+ * recurse on (z_(n/2+1),z_(n/2+2)... z_n) -> r2 and m2
+ * multiply elements of r1 by m2 -> s1
+ * multiply elements of r2 by m1 -> s2
+ * return r1||r2 and m1*m2
+ *
+ * In the example below, we suppose that we have 14 elements.
+ * Let z1, z2,... zE be the 14 values to invert (index noted in
+ * hexadecimal, starting at 1).
+ *
+ * - Depth 1:
+ * swap(z1, z2); z12 = z1*z2
+ * swap(z3, z4); z34 = z3*z4
+ * swap(z5, z6); z56 = z5*z6
+ * swap(z7, z8); z78 = z7*z8
+ * swap(z9, zA); z9A = z9*zA
+ * swap(zB, zC); zBC = zB*zC
+ * swap(zD, zE); zDE = zD*zE
+ *
+ * - Depth 2:
+ * z1 <- z1*z34, z2 <- z2*z34, z3 <- z3*z12, z4 <- z4*z12
+ * z1234 = z12*z34
+ * z5 <- z5*z78, z6 <- z6*z78, z7 <- z7*z56, z8 <- z8*z56
+ * z5678 = z56*z78
+ * z9 <- z9*zBC, zA <- zA*zBC, zB <- zB*z9A, zC <- zC*z9A
+ * z9ABC = z9A*zBC
+ *
+ * - Depth 3:
+ * z1 <- z1*z5678, z2 <- z2*z5678, z3 <- z3*z5678, z4 <- z4*z5678
+ * z5 <- z5*z1234, z6 <- z6*z1234, z7 <- z7*z1234, z8 <- z8*z1234
+ * z12345678 = z1234*z5678
+ * z9 <- z9*zDE, zA <- zA*zDE, zB <- zB*zDE, zC <- zC*zDE
+ * zD <- zD*z9ABC, zE*z9ABC
+ * z9ABCDE = z9ABC*zDE
+ *
+ * - Depth 4:
+ * multiply z1..z8 by z9ABCDE
+ * multiply z9..zE by z12345678
+ * final z = z12345678*z9ABCDE
+ */
+
+ uint64_t z[16][4];
+ int i, k, s;
+#define zt (z[15])
+#define zu (z[14])
+#define zv (z[13])
+
+ /*
+ * First recursion step (pairwise swapping and multiplication).
+ * If there is an odd number of elements, then we "invent" an
+ * extra one with coordinate Z = 1 (in Montgomery representation).
+ */
+ for (i = 0; (i + 1) < num; i += 2) {
+ memcpy(zt, jac[i].z, sizeof zt);
+ memcpy(jac[i].z, jac[i + 1].z, sizeof zt);
+ memcpy(jac[i + 1].z, zt, sizeof zt);
+ f256_montymul(z[i >> 1], jac[i].z, jac[i + 1].z);
+ }
+ if ((num & 1) != 0) {
+ memcpy(z[num >> 1], jac[num - 1].z, sizeof zt);
+ memcpy(jac[num - 1].z, F256_R, sizeof F256_R);
+ }
+
+ /*
+ * Perform further recursion steps. At the entry of each step,
+ * the process has been done for groups of 's' points. The
+ * integer k is the log2 of s.
+ */
+ for (k = 1, s = 2; s < num; k ++, s <<= 1) {
+ int n;
+
+ for (i = 0; i < num; i ++) {
+ f256_montymul(jac[i].z, jac[i].z, z[(i >> k) ^ 1]);
+ }
+ n = (num + s - 1) >> k;
+ for (i = 0; i < (n >> 1); i ++) {
+ f256_montymul(z[i], z[i << 1], z[(i << 1) + 1]);
+ }
+ if ((n & 1) != 0) {
+ memmove(z[n >> 1], z[n], sizeof zt);
+ }
+ }
+
+ /*
+ * Invert the final result, and convert all points.
+ */
+ f256_invert(zt, z[0]);
+ for (i = 0; i < num; i ++) {
+ f256_montymul(zv, jac[i].z, zt);
+ f256_montysquare(zu, zv);
+ f256_montymul(zv, zv, zu);
+ f256_montymul(aff[i].x, jac[i].x, zu);
+ f256_montymul(aff[i].y, jac[i].y, zv);
+ }
+}
+
+/*
+ * Multiply the provided point by an integer.
+ * Assumptions:
+ * - Source point is a valid curve point.
+ * - Source point is not the point-at-infinity.
+ * - Integer is not 0, and is lower than the curve order.
+ * If these conditions are not met, then the result is indeterminate
+ * (but the process is still constant-time).
+ */
+static void
+p256_mul(p256_jacobian *P, const unsigned char *k, size_t klen)
+{
+ union {
+ p256_affine aff[15];
+ p256_jacobian jac[15];
+ } window;
+ int i;
+
+ /*
+ * Compute window, in Jacobian coordinates.
+ */
+ window.jac[0] = *P;
+ for (i = 2; i < 16; i ++) {
+ window.jac[i - 1] = window.jac[(i >> 1) - 1];
+ if ((i & 1) == 0) {
+ p256_double(&window.jac[i - 1]);
+ } else {
+ p256_add(&window.jac[i - 1], &window.jac[i >> 1]);
+ }
+ }
+
+ /*
+ * Convert the window points to affine coordinates. Point
+ * window[0] is the source point, already in affine coordinates.
+ */
+ window_to_affine(window.aff, window.jac, 15);
+
+ /*
+ * Perform point multiplication.
+ */
+ point_mul_inner(P, window.aff, k, klen);
+}
+
+/*
+ * Precomputed window for the conventional generator: P256_Gwin[n]
+ * contains (n+1)*G (affine coordinates, in Montgomery representation).
+ */
+static const p256_affine P256_Gwin[] = {
+ {
+ { 0x79E730D418A9143C, 0x75BA95FC5FEDB601,
+ 0x79FB732B77622510, 0x18905F76A53755C6 },
+ { 0xDDF25357CE95560A, 0x8B4AB8E4BA19E45C,
+ 0xD2E88688DD21F325, 0x8571FF1825885D85 }
+ },
+ {
+ { 0x850046D410DDD64D, 0xAA6AE3C1A433827D,
+ 0x732205038D1490D9, 0xF6BB32E43DCF3A3B },
+ { 0x2F3648D361BEE1A5, 0x152CD7CBEB236FF8,
+ 0x19A8FB0E92042DBE, 0x78C577510A5B8A3B }
+ },
+ {
+ { 0xFFAC3F904EEBC127, 0xB027F84A087D81FB,
+ 0x66AD77DD87CBBC98, 0x26936A3FB6FF747E },
+ { 0xB04C5C1FC983A7EB, 0x583E47AD0861FE1A,
+ 0x788208311A2EE98E, 0xD5F06A29E587CC07 }
+ },
+ {
+ { 0x74B0B50D46918DCC, 0x4650A6EDC623C173,
+ 0x0CDAACACE8100AF2, 0x577362F541B0176B },
+ { 0x2D96F24CE4CBABA6, 0x17628471FAD6F447,
+ 0x6B6C36DEE5DDD22E, 0x84B14C394C5AB863 }
+ },
+ {
+ { 0xBE1B8AAEC45C61F5, 0x90EC649A94B9537D,
+ 0x941CB5AAD076C20C, 0xC9079605890523C8 },
+ { 0xEB309B4AE7BA4F10, 0x73C568EFE5EB882B,
+ 0x3540A9877E7A1F68, 0x73A076BB2DD1E916 }
+ },
+ {
+ { 0x403947373E77664A, 0x55AE744F346CEE3E,
+ 0xD50A961A5B17A3AD, 0x13074B5954213673 },
+ { 0x93D36220D377E44B, 0x299C2B53ADFF14B5,
+ 0xF424D44CEF639F11, 0xA4C9916D4A07F75F }
+ },
+ {
+ { 0x0746354EA0173B4F, 0x2BD20213D23C00F7,
+ 0xF43EAAB50C23BB08, 0x13BA5119C3123E03 },
+ { 0x2847D0303F5B9D4D, 0x6742F2F25DA67BDD,
+ 0xEF933BDC77C94195, 0xEAEDD9156E240867 }
+ },
+ {
+ { 0x27F14CD19499A78F, 0x462AB5C56F9B3455,
+ 0x8F90F02AF02CFC6B, 0xB763891EB265230D },
+ { 0xF59DA3A9532D4977, 0x21E3327DCF9EBA15,
+ 0x123C7B84BE60BBF0, 0x56EC12F27706DF76 }
+ },
+ {
+ { 0x75C96E8F264E20E8, 0xABE6BFED59A7A841,
+ 0x2CC09C0444C8EB00, 0xE05B3080F0C4E16B },
+ { 0x1EB7777AA45F3314, 0x56AF7BEDCE5D45E3,
+ 0x2B6E019A88B12F1A, 0x086659CDFD835F9B }
+ },
+ {
+ { 0x2C18DBD19DC21EC8, 0x98F9868A0FCF8139,
+ 0x737D2CD648250B49, 0xCC61C94724B3428F },
+ { 0x0C2B407880DD9E76, 0xC43A8991383FBE08,
+ 0x5F7D2D65779BE5D2, 0x78719A54EB3B4AB5 }
+ },
+ {
+ { 0xEA7D260A6245E404, 0x9DE407956E7FDFE0,
+ 0x1FF3A4158DAC1AB5, 0x3E7090F1649C9073 },
+ { 0x1A7685612B944E88, 0x250F939EE57F61C8,
+ 0x0C0DAA891EAD643D, 0x68930023E125B88E }
+ },
+ {
+ { 0x04B71AA7D2697768, 0xABDEDEF5CA345A33,
+ 0x2409D29DEE37385E, 0x4EE1DF77CB83E156 },
+ { 0x0CAC12D91CBB5B43, 0x170ED2F6CA895637,
+ 0x28228CFA8ADE6D66, 0x7FF57C9553238ACA }
+ },
+ {
+ { 0xCCC425634B2ED709, 0x0E356769856FD30D,
+ 0xBCBCD43F559E9811, 0x738477AC5395B759 },
+ { 0x35752B90C00EE17F, 0x68748390742ED2E3,
+ 0x7CD06422BD1F5BC1, 0xFBC08769C9E7B797 }
+ },
+ {
+ { 0xA242A35BB0CF664A, 0x126E48F77F9707E3,
+ 0x1717BF54C6832660, 0xFAAE7332FD12C72E },
+ { 0x27B52DB7995D586B, 0xBE29569E832237C2,
+ 0xE8E4193E2A65E7DB, 0x152706DC2EAA1BBB }
+ },
+ {
+ { 0x72BCD8B7BC60055B, 0x03CC23EE56E27E4B,
+ 0xEE337424E4819370, 0xE2AA0E430AD3DA09 },
+ { 0x40B8524F6383C45D, 0xD766355442A41B25,
+ 0x64EFA6DE778A4797, 0x2042170A7079ADF4 }
+ }
+};
+
+/*
+ * Multiply the conventional generator of the curve by the provided
+ * integer. Return is written in *P.
+ *
+ * Assumptions:
+ * - Integer is not 0, and is lower than the curve order.
+ * If this conditions is not met, then the result is indeterminate
+ * (but the process is still constant-time).
+ */
+static void
+p256_mulgen(p256_jacobian *P, const unsigned char *k, size_t klen)
+{
+ point_mul_inner(P, P256_Gwin, k, klen);
+}
+
+/*
+ * Return 1 if all of the following hold:
+ * - klen <= 32
+ * - k != 0
+ * - k is lower than the curve order
+ * Otherwise, return 0.
+ *
+ * Constant-time behaviour: only klen may be observable.
+ */
+static uint32_t
+check_scalar(const unsigned char *k, size_t klen)
+{
+ uint32_t z;
+ int32_t c;
+ size_t u;
+
+ if (klen > 32) {
+ return 0;
+ }
+ z = 0;
+ for (u = 0; u < klen; u ++) {
+ z |= k[u];
+ }
+ if (klen == 32) {
+ c = 0;
+ for (u = 0; u < klen; u ++) {
+ c |= -(int32_t)EQ0(c) & CMP(k[u], P256_N[u]);
+ }
+ } else {
+ c = -1;
+ }
+ return NEQ(z, 0) & LT0(c);
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *k, size_t klen, int curve)
+{
+ uint32_t r;
+ p256_jacobian P;
+
+ (void)curve;
+ if (Glen != 65) {
+ return 0;
+ }
+ r = check_scalar(k, klen);
+ r &= point_decode(&P, G);
+ p256_mul(&P, k, klen);
+ r &= point_encode(G, &P);
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *k, size_t klen, int curve)
+{
+ p256_jacobian P;
+
+ (void)curve;
+ p256_mulgen(&P, k, klen);
+ point_encode(R, &P);
+ return 65;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ /*
+ * We might want to use Shamir's trick here: make a composite
+ * window of u*P+v*Q points, to merge the two doubling-ladders
+ * into one. This, however, has some complications:
+ *
+ * - During the computation, we may hit the point-at-infinity.
+ * Thus, we would need p256_add_complete_mixed() (complete
+ * formulas for point addition), with a higher cost (17 muls
+ * instead of 11).
+ *
+ * - A 4-bit window would be too large, since it would involve
+ * 16*16-1 = 255 points. For the same window size as in the
+ * p256_mul() case, we would need to reduce the window size
+ * to 2 bits, and thus perform twice as many non-doubling
+ * point additions.
+ *
+ * - The window may itself contain the point-at-infinity, and
+ * thus cannot be in all generality be made of affine points.
+ * Instead, we would need to make it a window of points in
+ * Jacobian coordinates. Even p256_add_complete_mixed() would
+ * be inappropriate.
+ *
+ * For these reasons, the code below performs two separate
+ * point multiplications, then computes the final point addition
+ * (which is both a "normal" addition, and a doubling, to handle
+ * all cases).
+ */
+
+ p256_jacobian P, Q;
+ uint32_t r, t, s;
+ uint64_t z;
+
+ (void)curve;
+ if (len != 65) {
+ return 0;
+ }
+ r = point_decode(&P, A);
+ p256_mul(&P, x, xlen);
+ if (B == NULL) {
+ p256_mulgen(&Q, y, ylen);
+ } else {
+ r &= point_decode(&Q, B);
+ p256_mul(&Q, y, ylen);
+ }
+
+ /*
+ * The final addition may fail in case both points are equal.
+ */
+ t = p256_add(&P, &Q);
+ f256_final_reduce(P.z);
+ z = P.z[0] | P.z[1] | P.z[2] | P.z[3];
+ s = EQ((uint32_t)(z | (z >> 32)), 0);
+ p256_double(&Q);
+
+ /*
+ * If s is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * s = 0, t = 0 return P (normal addition)
+ * s = 0, t = 1 return P (normal addition)
+ * s = 1, t = 0 return Q (a 'double' case)
+ * s = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(s & ~t, &P, &Q, sizeof Q);
+ point_encode(A, &P);
+ r &= ~(s & t);
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_p256_m64 = {
+ (uint32_t)0x00800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_p256_m64_get(void)
+{
+ return &br_ec_p256_m64;
+}
+
+#else
+
+/* see bearssl_ec.h */
+const br_ec_impl *
+br_ec_p256_m64_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/ec/ec_prime_i15.c b/test/monniaux/BearSSL/src/ec/ec_prime_i15.c
new file mode 100644
index 00000000..0f210f24
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_prime_i15.c
@@ -0,0 +1,820 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Parameters for supported curves:
+ * - field modulus p
+ * - R^2 mod p (R = 2^(15k) for the smallest k such that R >= p)
+ * - b*R mod p (b is the second curve equation parameter)
+ */
+
+static const uint16_t P256_P[] = {
+ 0x0111,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x003F, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x1000, 0x0000, 0x4000, 0x7FFF,
+ 0x7FFF, 0x0001
+};
+
+static const uint16_t P256_R2[] = {
+ 0x0111,
+ 0x0000, 0x6000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7FFC, 0x7FFF,
+ 0x7FBF, 0x7FFF, 0x7FBF, 0x7FFF, 0x7FFF, 0x7FFF, 0x77FF, 0x7FFF,
+ 0x4FFF, 0x0000
+};
+
+static const uint16_t P256_B[] = {
+ 0x0111,
+ 0x770C, 0x5EEF, 0x29C4, 0x3EC4, 0x6273, 0x0486, 0x4543, 0x3993,
+ 0x3C01, 0x6B56, 0x212E, 0x57EE, 0x4882, 0x204B, 0x7483, 0x3C16,
+ 0x0187, 0x0000
+};
+
+static const uint16_t P384_P[] = {
+ 0x0199,
+ 0x7FFF, 0x7FFF, 0x0003, 0x0000, 0x0000, 0x0000, 0x7FC0, 0x7FFF,
+ 0x7EFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x01FF
+};
+
+static const uint16_t P384_R2[] = {
+ 0x0199,
+ 0x1000, 0x0000, 0x0000, 0x7FFF, 0x7FFF, 0x0001, 0x0000, 0x0010,
+ 0x0000, 0x0000, 0x0000, 0x7F00, 0x7FFF, 0x01FF, 0x0000, 0x1000,
+ 0x0000, 0x2000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000
+};
+
+static const uint16_t P384_B[] = {
+ 0x0199,
+ 0x7333, 0x2096, 0x70D1, 0x2310, 0x3020, 0x6197, 0x1464, 0x35BB,
+ 0x70CA, 0x0117, 0x1920, 0x4136, 0x5FC8, 0x5713, 0x4938, 0x7DD2,
+ 0x4DD2, 0x4A71, 0x0220, 0x683E, 0x2C87, 0x4DB1, 0x7BFF, 0x6C09,
+ 0x0452, 0x0084
+};
+
+static const uint16_t P521_P[] = {
+ 0x022B,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
+ 0x7FFF, 0x7FFF, 0x07FF
+};
+
+static const uint16_t P521_R2[] = {
+ 0x022B,
+ 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000
+};
+
+static const uint16_t P521_B[] = {
+ 0x022B,
+ 0x7002, 0x6A07, 0x751A, 0x228F, 0x71EF, 0x5869, 0x20F4, 0x1EFC,
+ 0x7357, 0x37E0, 0x4EEC, 0x605E, 0x1652, 0x26F6, 0x31FA, 0x4A8F,
+ 0x6193, 0x3C2A, 0x3C42, 0x48C7, 0x3489, 0x6771, 0x4C57, 0x5CCD,
+ 0x2725, 0x545B, 0x503B, 0x5B42, 0x21A0, 0x2534, 0x687E, 0x70E4,
+ 0x1618, 0x27D7, 0x0465
+};
+
+typedef struct {
+ const uint16_t *p;
+ const uint16_t *b;
+ const uint16_t *R2;
+ uint16_t p0i;
+ size_t point_len;
+} curve_params;
+
+static inline const curve_params *
+id_to_curve(int curve)
+{
+ static const curve_params pp[] = {
+ { P256_P, P256_B, P256_R2, 0x0001, 65 },
+ { P384_P, P384_B, P384_R2, 0x0001, 97 },
+ { P521_P, P521_B, P521_R2, 0x0001, 133 }
+ };
+
+ return &pp[curve - BR_EC_secp256r1];
+}
+
+#define I15_LEN ((BR_MAX_EC_SIZE + 29) / 15)
+
+/*
+ * Type for a point in Jacobian coordinates:
+ * -- three values, x, y and z, in Montgomery representation
+ * -- affine coordinates are X = x / z^2 and Y = y / z^3
+ * -- for the point at infinity, z = 0
+ */
+typedef struct {
+ uint16_t c[3][I15_LEN];
+} jacobian;
+
+/*
+ * We use a custom interpreter that uses a dozen registers, and
+ * only six operations:
+ * MSET(d, a) copy a into d
+ * MADD(d, a) d = d+a (modular)
+ * MSUB(d, a) d = d-a (modular)
+ * MMUL(d, a, b) d = a*b (Montgomery multiplication)
+ * MINV(d, a, b) invert d modulo p; a and b are used as scratch registers
+ * MTZ(d) clear return value if d = 0
+ * Destination of MMUL (d) must be distinct from operands (a and b).
+ * There is no such constraint for MSUB and MADD.
+ *
+ * Registers include the operand coordinates, and temporaries.
+ */
+#define MSET(d, a) (0x0000 + ((d) << 8) + ((a) << 4))
+#define MADD(d, a) (0x1000 + ((d) << 8) + ((a) << 4))
+#define MSUB(d, a) (0x2000 + ((d) << 8) + ((a) << 4))
+#define MMUL(d, a, b) (0x3000 + ((d) << 8) + ((a) << 4) + (b))
+#define MINV(d, a, b) (0x4000 + ((d) << 8) + ((a) << 4) + (b))
+#define MTZ(d) (0x5000 + ((d) << 8))
+#define ENDCODE 0
+
+/*
+ * Registers for the input operands.
+ */
+#define P1x 0
+#define P1y 1
+#define P1z 2
+#define P2x 3
+#define P2y 4
+#define P2z 5
+
+/*
+ * Alternate names for the first input operand.
+ */
+#define Px 0
+#define Py 1
+#define Pz 2
+
+/*
+ * Temporaries.
+ */
+#define t1 6
+#define t2 7
+#define t3 8
+#define t4 9
+#define t5 10
+#define t6 11
+#define t7 12
+
+/*
+ * Extra scratch registers available when there is no second operand (e.g.
+ * for "double" and "affine").
+ */
+#define t8 3
+#define t9 4
+#define t10 5
+
+/*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * If y = 0 (P has order 2) then this yields infinity (z' = 0), as it
+ * should. This case should not happen anyway, because our curves have
+ * prime order, and thus do not contain any point of order 2.
+ *
+ * If P is infinity (z = 0), then again the formulas yield infinity,
+ * which is correct. Thus, this code works for all points.
+ *
+ * Cost: 8 multiplications
+ */
+static const uint16_t code_double[] = {
+ /*
+ * Compute z^2 (in t1).
+ */
+ MMUL(t1, Pz, Pz),
+
+ /*
+ * Compute x-z^2 (in t2) and then x+z^2 (in t1).
+ */
+ MSET(t2, Px),
+ MSUB(t2, t1),
+ MADD(t1, Px),
+
+ /*
+ * Compute m = 3*(x+z^2)*(x-z^2) (in t1).
+ */
+ MMUL(t3, t1, t2),
+ MSET(t1, t3),
+ MADD(t1, t3),
+ MADD(t1, t3),
+
+ /*
+ * Compute s = 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ MMUL(t3, Py, Py),
+ MADD(t3, t3),
+ MMUL(t2, Px, t3),
+ MADD(t2, t2),
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ MMUL(Px, t1, t1),
+ MSUB(Px, t2),
+ MSUB(Px, t2),
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ MMUL(t4, Py, Pz),
+ MSET(Pz, t4),
+ MADD(Pz, t4),
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ MSUB(t2, Px),
+ MMUL(Py, t1, t2),
+ MMUL(t4, t3, t3),
+ MSUB(Py, t4),
+ MSUB(Py, t4),
+
+ ENDCODE
+};
+
+/*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ *
+ * If both P1 and P2 are infinity, then z1 == 0 and z2 == 0, implying that
+ * z3 == 0, so the result is correct.
+ * If either of P1 or P2 is infinity, but not both, then z3 == 0, which is
+ * not correct.
+ * h == 0 only if u1 == u2; this happens in two cases:
+ * -- if s1 == s2 then P1 and/or P2 is infinity, or P1 == P2
+ * -- if s1 != s2 then P1 + P2 == infinity (but neither P1 or P2 is infinity)
+ *
+ * Thus, the following situations are not handled correctly:
+ * -- P1 = 0 and P2 != 0
+ * -- P1 != 0 and P2 = 0
+ * -- P1 = P2
+ * All other cases are properly computed. However, even in "incorrect"
+ * situations, the three coordinates still are properly formed field
+ * elements.
+ *
+ * The returned flag is cleared if r == 0. This happens in the following
+ * cases:
+ * -- Both points are on the same horizontal line (same Y coordinate).
+ * -- Both points are infinity.
+ * -- One point is infinity and the other is on line Y = 0.
+ * The third case cannot happen with our curves (there is no valid point
+ * on line Y = 0 since that would be a point of order 2). If the two
+ * source points are non-infinity, then remains only the case where the
+ * two points are on the same horizontal line.
+ *
+ * This allows us to detect the "P1 == P2" case, assuming that P1 != 0 and
+ * P2 != 0:
+ * -- If the returned value is not the point at infinity, then it was properly
+ * computed.
+ * -- Otherwise, if the returned flag is 1, then P1+P2 = 0, and the result
+ * is indeed the point at infinity.
+ * -- Otherwise (result is infinity, flag is 0), then P1 = P2 and we should
+ * use the 'double' code.
+ *
+ * Cost: 16 multiplications
+ */
+static const uint16_t code_add[] = {
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ MMUL(t3, P2z, P2z),
+ MMUL(t1, P1x, t3),
+ MMUL(t4, P2z, t3),
+ MMUL(t3, P1y, t4),
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ MMUL(t4, P1z, P1z),
+ MMUL(t2, P2x, t4),
+ MMUL(t5, P1z, t4),
+ MMUL(t4, P2y, t5),
+
+ /*
+ * Compute h = u2 - u1 (in t2) and r = s2 - s1 (in t4).
+ */
+ MSUB(t2, t1),
+ MSUB(t4, t3),
+
+ /*
+ * Report cases where r = 0 through the returned flag.
+ */
+ MTZ(t4),
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5).
+ */
+ MMUL(t7, t2, t2),
+ MMUL(t6, t1, t7),
+ MMUL(t5, t7, t2),
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ * t1 and t7 can be used as scratch registers.
+ */
+ MMUL(P1x, t4, t4),
+ MSUB(P1x, t5),
+ MSUB(P1x, t6),
+ MSUB(P1x, t6),
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ MSUB(t6, P1x),
+ MMUL(P1y, t4, t6),
+ MMUL(t1, t5, t3),
+ MSUB(P1y, t1),
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ MMUL(t1, P1z, P2z),
+ MMUL(P1z, t1, t2),
+
+ ENDCODE
+};
+
+/*
+ * Check that the point is on the curve. This code snippet assumes the
+ * following conventions:
+ * -- Coordinates x and y have been freshly decoded in P1 (but not
+ * converted to Montgomery coordinates yet).
+ * -- P2x, P2y and P2z are set to, respectively, R^2, b*R and 1.
+ */
+static const uint16_t code_check[] = {
+
+ /* Convert x and y to Montgomery representation. */
+ MMUL(t1, P1x, P2x),
+ MMUL(t2, P1y, P2x),
+ MSET(P1x, t1),
+ MSET(P1y, t2),
+
+ /* Compute x^3 in t1. */
+ MMUL(t2, P1x, P1x),
+ MMUL(t1, P1x, t2),
+
+ /* Subtract 3*x from t1. */
+ MSUB(t1, P1x),
+ MSUB(t1, P1x),
+ MSUB(t1, P1x),
+
+ /* Add b. */
+ MADD(t1, P2y),
+
+ /* Compute y^2 in t2. */
+ MMUL(t2, P1y, P1y),
+
+ /* Compare y^2 with x^3 - 3*x + b; they must match. */
+ MSUB(t1, t2),
+ MTZ(t1),
+
+ /* Set z to 1 (in Montgomery representation). */
+ MMUL(P1z, P2x, P2z),
+
+ ENDCODE
+};
+
+/*
+ * Conversion back to affine coordinates. This code snippet assumes that
+ * the z coordinate of P2 is set to 1 (not in Montgomery representation).
+ */
+static const uint16_t code_affine[] = {
+
+ /* Save z*R in t1. */
+ MSET(t1, P1z),
+
+ /* Compute z^3 in t2. */
+ MMUL(t2, P1z, P1z),
+ MMUL(t3, P1z, t2),
+ MMUL(t2, t3, P2z),
+
+ /* Invert to (1/z^3) in t2. */
+ MINV(t2, t3, t4),
+
+ /* Compute y. */
+ MSET(t3, P1y),
+ MMUL(P1y, t2, t3),
+
+ /* Compute (1/z^2) in t3. */
+ MMUL(t3, t2, t1),
+
+ /* Compute x. */
+ MSET(t2, P1x),
+ MMUL(P1x, t2, t3),
+
+ ENDCODE
+};
+
+static uint32_t
+run_code(jacobian *P1, const jacobian *P2,
+ const curve_params *cc, const uint16_t *code)
+{
+ uint32_t r;
+ uint16_t t[13][I15_LEN];
+ size_t u;
+
+ r = 1;
+
+ /*
+ * Copy the two operands in the dedicated registers.
+ */
+ memcpy(t[P1x], P1->c, 3 * I15_LEN * sizeof(uint16_t));
+ memcpy(t[P2x], P2->c, 3 * I15_LEN * sizeof(uint16_t));
+
+ /*
+ * Run formulas.
+ */
+ for (u = 0;; u ++) {
+ unsigned op, d, a, b;
+
+ op = code[u];
+ if (op == 0) {
+ break;
+ }
+ d = (op >> 8) & 0x0F;
+ a = (op >> 4) & 0x0F;
+ b = op & 0x0F;
+ op >>= 12;
+ switch (op) {
+ uint32_t ctl;
+ size_t plen;
+ unsigned char tp[(BR_MAX_EC_SIZE + 7) >> 3];
+
+ case 0:
+ memcpy(t[d], t[a], I15_LEN * sizeof(uint16_t));
+ break;
+ case 1:
+ ctl = br_i15_add(t[d], t[a], 1);
+ ctl |= NOT(br_i15_sub(t[d], cc->p, 0));
+ br_i15_sub(t[d], cc->p, ctl);
+ break;
+ case 2:
+ br_i15_add(t[d], cc->p, br_i15_sub(t[d], t[a], 1));
+ break;
+ case 3:
+ br_i15_montymul(t[d], t[a], t[b], cc->p, cc->p0i);
+ break;
+ case 4:
+ plen = (cc->p[0] - (cc->p[0] >> 4) + 7) >> 3;
+ br_i15_encode(tp, plen, cc->p);
+ tp[plen - 1] -= 2;
+ br_i15_modpow(t[d], tp, plen,
+ cc->p, cc->p0i, t[a], t[b]);
+ break;
+ default:
+ r &= ~br_i15_iszero(t[d]);
+ break;
+ }
+ }
+
+ /*
+ * Copy back result.
+ */
+ memcpy(P1->c, t[P1x], 3 * I15_LEN * sizeof(uint16_t));
+ return r;
+}
+
+static void
+set_one(uint16_t *x, const uint16_t *p)
+{
+ size_t plen;
+
+ plen = (p[0] + 31) >> 4;
+ memset(x, 0, plen * sizeof *x);
+ x[0] = p[0];
+ x[1] = 0x0001;
+}
+
+static void
+point_zero(jacobian *P, const curve_params *cc)
+{
+ memset(P, 0, sizeof *P);
+ P->c[0][0] = P->c[1][0] = P->c[2][0] = cc->p[0];
+}
+
+static inline void
+point_double(jacobian *P, const curve_params *cc)
+{
+ run_code(P, P, cc, code_double);
+}
+
+static inline uint32_t
+point_add(jacobian *P1, const jacobian *P2, const curve_params *cc)
+{
+ return run_code(P1, P2, cc, code_add);
+}
+
+static void
+point_mul(jacobian *P, const unsigned char *x, size_t xlen,
+ const curve_params *cc)
+{
+ /*
+ * We do a simple double-and-add ladder with a 2-bit window
+ * to make only one add every two doublings. We thus first
+ * precompute 2P and 3P in some local buffers.
+ *
+ * We always perform two doublings and one addition; the
+ * addition is with P, 2P and 3P and is done in a temporary
+ * array.
+ *
+ * The addition code cannot handle cases where one of the
+ * operands is infinity, which is the case at the start of the
+ * ladder. We therefore need to maintain a flag that controls
+ * this situation.
+ */
+ uint32_t qz;
+ jacobian P2, P3, Q, T, U;
+
+ memcpy(&P2, P, sizeof P2);
+ point_double(&P2, cc);
+ memcpy(&P3, P, sizeof P3);
+ point_add(&P3, &P2, cc);
+
+ point_zero(&Q, cc);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+
+ for (k = 6; k >= 0; k -= 2) {
+ uint32_t bits;
+ uint32_t bnz;
+
+ point_double(&Q, cc);
+ point_double(&Q, cc);
+ memcpy(&T, P, sizeof T);
+ memcpy(&U, &Q, sizeof U);
+ bits = (*x >> k) & (uint32_t)3;
+ bnz = NEQ(bits, 0);
+ CCOPY(EQ(bits, 2), &T, &P2, sizeof T);
+ CCOPY(EQ(bits, 3), &T, &P3, sizeof T);
+ point_add(&U, &T, cc);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ }
+ x ++;
+ }
+ memcpy(P, &Q, sizeof Q);
+}
+
+/*
+ * Decode point into Jacobian coordinates. This function does not support
+ * the point at infinity. If the point is invalid then this returns 0, but
+ * the coordinates are still set to properly formed field elements.
+ */
+static uint32_t
+point_decode(jacobian *P, const void *src, size_t len, const curve_params *cc)
+{
+ /*
+ * Points must use uncompressed format:
+ * -- first byte is 0x04;
+ * -- coordinates X and Y use unsigned big-endian, with the same
+ * length as the field modulus.
+ *
+ * We don't support hybrid format (uncompressed, but first byte
+ * has value 0x06 or 0x07, depending on the least significant bit
+ * of Y) because it is rather useless, and explicitly forbidden
+ * by PKIX (RFC 5480, section 2.2).
+ *
+ * We don't support compressed format either, because it is not
+ * much used in practice (there are or were patent-related
+ * concerns about point compression, which explains the lack of
+ * generalised support). Also, point compression support would
+ * need a bit more code.
+ */
+ const unsigned char *buf;
+ size_t plen, zlen;
+ uint32_t r;
+ jacobian Q;
+
+ buf = src;
+ point_zero(P, cc);
+ plen = (cc->p[0] - (cc->p[0] >> 4) + 7) >> 3;
+ if (len != 1 + (plen << 1)) {
+ return 0;
+ }
+ r = br_i15_decode_mod(P->c[0], buf + 1, plen, cc->p);
+ r &= br_i15_decode_mod(P->c[1], buf + 1 + plen, plen, cc->p);
+
+ /*
+ * Check first byte.
+ */
+ r &= EQ(buf[0], 0x04);
+ /* obsolete
+ r &= EQ(buf[0], 0x04) | (EQ(buf[0] & 0xFE, 0x06)
+ & ~(uint32_t)(buf[0] ^ buf[plen << 1]));
+ */
+
+ /*
+ * Convert coordinates and check that the point is valid.
+ */
+ zlen = ((cc->p[0] + 31) >> 4) * sizeof(uint16_t);
+ memcpy(Q.c[0], cc->R2, zlen);
+ memcpy(Q.c[1], cc->b, zlen);
+ set_one(Q.c[2], cc->p);
+ r &= ~run_code(P, &Q, cc, code_check);
+ return r;
+}
+
+/*
+ * Encode a point. This method assumes that the point is correct and is
+ * not the point at infinity. Encoded size is always 1+2*plen, where
+ * plen is the field modulus length, in bytes.
+ */
+static void
+point_encode(void *dst, const jacobian *P, const curve_params *cc)
+{
+ unsigned char *buf;
+ size_t plen;
+ jacobian Q, T;
+
+ buf = dst;
+ plen = (cc->p[0] - (cc->p[0] >> 4) + 7) >> 3;
+ buf[0] = 0x04;
+ memcpy(&Q, P, sizeof *P);
+ set_one(T.c[2], cc->p);
+ run_code(&Q, &T, cc, code_affine);
+ br_i15_encode(buf + 1, plen, Q.c[0]);
+ br_i15_encode(buf + 1 + plen, plen, Q.c[1]);
+}
+
+static const br_ec_curve_def *
+id_to_curve_def(int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return &br_secp256r1;
+ case BR_EC_secp384r1:
+ return &br_secp384r1;
+ case BR_EC_secp521r1:
+ return &br_secp521r1;
+ }
+ return NULL;
+}
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ const br_ec_curve_def *cd;
+
+ cd = id_to_curve_def(curve);
+ *len = cd->generator_len;
+ return cd->generator;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ const br_ec_curve_def *cd;
+
+ cd = id_to_curve_def(curve);
+ *len = cd->order_len;
+ return cd->order;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ api_generator(curve, len);
+ *len >>= 1;
+ return 1;
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ uint32_t r;
+ const curve_params *cc;
+ jacobian P;
+
+ cc = id_to_curve(curve);
+ r = point_decode(&P, G, Glen, cc);
+ point_mul(&P, x, xlen, cc);
+ if (Glen == cc->point_len) {
+ point_encode(G, &P, cc);
+ }
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ uint32_t r, t, z;
+ const curve_params *cc;
+ jacobian P, Q;
+
+ /*
+ * TODO: see about merging the two ladders. Right now, we do
+ * two independent point multiplications, which is a bit
+ * wasteful of CPU resources (but yields short code).
+ */
+
+ cc = id_to_curve(curve);
+ r = point_decode(&P, A, len, cc);
+ if (B == NULL) {
+ size_t Glen;
+
+ B = api_generator(curve, &Glen);
+ }
+ r &= point_decode(&Q, B, len, cc);
+ point_mul(&P, x, xlen, cc);
+ point_mul(&Q, y, ylen, cc);
+
+ /*
+ * We want to compute P+Q. Since the base points A and B are distinct
+ * from infinity, and the multipliers are non-zero and lower than the
+ * curve order, then we know that P and Q are non-infinity. This
+ * leaves two special situations to test for:
+ * -- If P = Q then we must use point_double().
+ * -- If P+Q = 0 then we must report an error.
+ */
+ t = point_add(&P, &Q, cc);
+ point_double(&Q, cc);
+ z = br_i15_iszero(P.c[2]);
+
+ /*
+ * If z is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * z = 0, t = 0 return P (normal addition)
+ * z = 0, t = 1 return P (normal addition)
+ * z = 1, t = 0 return Q (a 'double' case)
+ * z = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(z & ~t, &P, &Q, sizeof Q);
+ point_encode(A, &P, cc);
+ r &= ~(z & t);
+
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_prime_i15 = {
+ (uint32_t)0x03800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_prime_i31.c b/test/monniaux/BearSSL/src/ec/ec_prime_i31.c
new file mode 100644
index 00000000..0586a3b5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_prime_i31.c
@@ -0,0 +1,819 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Parameters for supported curves (field modulus, and 'b' equation
+ * parameter; both values use the 'i31' format, and 'b' is in Montgomery
+ * representation).
+ */
+
+static const uint32_t P256_P[] = {
+ 0x00000108,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x00000007,
+ 0x00000000, 0x00000000, 0x00000040, 0x7FFFFF80,
+ 0x000000FF
+};
+
+static const uint32_t P256_R2[] = {
+ 0x00000108,
+ 0x00014000, 0x00018000, 0x00000000, 0x7FF40000,
+ 0x7FEFFFFF, 0x7FF7FFFF, 0x7FAFFFFF, 0x005FFFFF,
+ 0x00000000
+};
+
+static const uint32_t P256_B[] = {
+ 0x00000108,
+ 0x6FEE1803, 0x6229C4BD, 0x21B139BE, 0x327150AA,
+ 0x3567802E, 0x3F7212ED, 0x012E4355, 0x782DD38D,
+ 0x0000000E
+};
+
+static const uint32_t P384_P[] = {
+ 0x0000018C,
+ 0x7FFFFFFF, 0x00000001, 0x00000000, 0x7FFFFFF8,
+ 0x7FFFFFEF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x00000FFF
+};
+
+static const uint32_t P384_R2[] = {
+ 0x0000018C,
+ 0x00000000, 0x00000080, 0x7FFFFE00, 0x000001FF,
+ 0x00000800, 0x00000000, 0x7FFFE000, 0x00001FFF,
+ 0x00008000, 0x00008000, 0x00000000, 0x00000000,
+ 0x00000000
+};
+
+static const uint32_t P384_B[] = {
+ 0x0000018C,
+ 0x6E666840, 0x070D0392, 0x5D810231, 0x7651D50C,
+ 0x17E218D6, 0x1B192002, 0x44EFE441, 0x3A524E2B,
+ 0x2719BA5F, 0x41F02209, 0x36C5643E, 0x5813EFFE,
+ 0x000008A5
+};
+
+static const uint32_t P521_P[] = {
+ 0x00000219,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
+ 0x01FFFFFF
+};
+
+static const uint32_t P521_R2[] = {
+ 0x00000219,
+ 0x00001000, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000,
+ 0x00000000
+};
+
+static const uint32_t P521_B[] = {
+ 0x00000219,
+ 0x540FC00A, 0x228FEA35, 0x2C34F1EF, 0x67BF107A,
+ 0x46FC1CD5, 0x1605E9DD, 0x6937B165, 0x272A3D8F,
+ 0x42785586, 0x44C8C778, 0x15F3B8B4, 0x64B73366,
+ 0x03BA8B69, 0x0D05B42A, 0x21F929A2, 0x2C31C393,
+ 0x00654FAE
+};
+
+typedef struct {
+ const uint32_t *p;
+ const uint32_t *b;
+ const uint32_t *R2;
+ uint32_t p0i;
+} curve_params;
+
+static inline const curve_params *
+id_to_curve(int curve)
+{
+ static const curve_params pp[] = {
+ { P256_P, P256_B, P256_R2, 0x00000001 },
+ { P384_P, P384_B, P384_R2, 0x00000001 },
+ { P521_P, P521_B, P521_R2, 0x00000001 }
+ };
+
+ return &pp[curve - BR_EC_secp256r1];
+}
+
+#define I31_LEN ((BR_MAX_EC_SIZE + 61) / 31)
+
+/*
+ * Type for a point in Jacobian coordinates:
+ * -- three values, x, y and z, in Montgomery representation
+ * -- affine coordinates are X = x / z^2 and Y = y / z^3
+ * -- for the point at infinity, z = 0
+ */
+typedef struct {
+ uint32_t c[3][I31_LEN];
+} jacobian;
+
+/*
+ * We use a custom interpreter that uses a dozen registers, and
+ * only six operations:
+ * MSET(d, a) copy a into d
+ * MADD(d, a) d = d+a (modular)
+ * MSUB(d, a) d = d-a (modular)
+ * MMUL(d, a, b) d = a*b (Montgomery multiplication)
+ * MINV(d, a, b) invert d modulo p; a and b are used as scratch registers
+ * MTZ(d) clear return value if d = 0
+ * Destination of MMUL (d) must be distinct from operands (a and b).
+ * There is no such constraint for MSUB and MADD.
+ *
+ * Registers include the operand coordinates, and temporaries.
+ */
+#define MSET(d, a) (0x0000 + ((d) << 8) + ((a) << 4))
+#define MADD(d, a) (0x1000 + ((d) << 8) + ((a) << 4))
+#define MSUB(d, a) (0x2000 + ((d) << 8) + ((a) << 4))
+#define MMUL(d, a, b) (0x3000 + ((d) << 8) + ((a) << 4) + (b))
+#define MINV(d, a, b) (0x4000 + ((d) << 8) + ((a) << 4) + (b))
+#define MTZ(d) (0x5000 + ((d) << 8))
+#define ENDCODE 0
+
+/*
+ * Registers for the input operands.
+ */
+#define P1x 0
+#define P1y 1
+#define P1z 2
+#define P2x 3
+#define P2y 4
+#define P2z 5
+
+/*
+ * Alternate names for the first input operand.
+ */
+#define Px 0
+#define Py 1
+#define Pz 2
+
+/*
+ * Temporaries.
+ */
+#define t1 6
+#define t2 7
+#define t3 8
+#define t4 9
+#define t5 10
+#define t6 11
+#define t7 12
+
+/*
+ * Extra scratch registers available when there is no second operand (e.g.
+ * for "double" and "affine").
+ */
+#define t8 3
+#define t9 4
+#define t10 5
+
+/*
+ * Doubling formulas are:
+ *
+ * s = 4*x*y^2
+ * m = 3*(x + z^2)*(x - z^2)
+ * x' = m^2 - 2*s
+ * y' = m*(s - x') - 8*y^4
+ * z' = 2*y*z
+ *
+ * If y = 0 (P has order 2) then this yields infinity (z' = 0), as it
+ * should. This case should not happen anyway, because our curves have
+ * prime order, and thus do not contain any point of order 2.
+ *
+ * If P is infinity (z = 0), then again the formulas yield infinity,
+ * which is correct. Thus, this code works for all points.
+ *
+ * Cost: 8 multiplications
+ */
+static const uint16_t code_double[] = {
+ /*
+ * Compute z^2 (in t1).
+ */
+ MMUL(t1, Pz, Pz),
+
+ /*
+ * Compute x-z^2 (in t2) and then x+z^2 (in t1).
+ */
+ MSET(t2, Px),
+ MSUB(t2, t1),
+ MADD(t1, Px),
+
+ /*
+ * Compute m = 3*(x+z^2)*(x-z^2) (in t1).
+ */
+ MMUL(t3, t1, t2),
+ MSET(t1, t3),
+ MADD(t1, t3),
+ MADD(t1, t3),
+
+ /*
+ * Compute s = 4*x*y^2 (in t2) and 2*y^2 (in t3).
+ */
+ MMUL(t3, Py, Py),
+ MADD(t3, t3),
+ MMUL(t2, Px, t3),
+ MADD(t2, t2),
+
+ /*
+ * Compute x' = m^2 - 2*s.
+ */
+ MMUL(Px, t1, t1),
+ MSUB(Px, t2),
+ MSUB(Px, t2),
+
+ /*
+ * Compute z' = 2*y*z.
+ */
+ MMUL(t4, Py, Pz),
+ MSET(Pz, t4),
+ MADD(Pz, t4),
+
+ /*
+ * Compute y' = m*(s - x') - 8*y^4. Note that we already have
+ * 2*y^2 in t3.
+ */
+ MSUB(t2, Px),
+ MMUL(Py, t1, t2),
+ MMUL(t4, t3, t3),
+ MSUB(Py, t4),
+ MSUB(Py, t4),
+
+ ENDCODE
+};
+
+/*
+ * Addtions formulas are:
+ *
+ * u1 = x1 * z2^2
+ * u2 = x2 * z1^2
+ * s1 = y1 * z2^3
+ * s2 = y2 * z1^3
+ * h = u2 - u1
+ * r = s2 - s1
+ * x3 = r^2 - h^3 - 2 * u1 * h^2
+ * y3 = r * (u1 * h^2 - x3) - s1 * h^3
+ * z3 = h * z1 * z2
+ *
+ * If both P1 and P2 are infinity, then z1 == 0 and z2 == 0, implying that
+ * z3 == 0, so the result is correct.
+ * If either of P1 or P2 is infinity, but not both, then z3 == 0, which is
+ * not correct.
+ * h == 0 only if u1 == u2; this happens in two cases:
+ * -- if s1 == s2 then P1 and/or P2 is infinity, or P1 == P2
+ * -- if s1 != s2 then P1 + P2 == infinity (but neither P1 or P2 is infinity)
+ *
+ * Thus, the following situations are not handled correctly:
+ * -- P1 = 0 and P2 != 0
+ * -- P1 != 0 and P2 = 0
+ * -- P1 = P2
+ * All other cases are properly computed. However, even in "incorrect"
+ * situations, the three coordinates still are properly formed field
+ * elements.
+ *
+ * The returned flag is cleared if r == 0. This happens in the following
+ * cases:
+ * -- Both points are on the same horizontal line (same Y coordinate).
+ * -- Both points are infinity.
+ * -- One point is infinity and the other is on line Y = 0.
+ * The third case cannot happen with our curves (there is no valid point
+ * on line Y = 0 since that would be a point of order 2). If the two
+ * source points are non-infinity, then remains only the case where the
+ * two points are on the same horizontal line.
+ *
+ * This allows us to detect the "P1 == P2" case, assuming that P1 != 0 and
+ * P2 != 0:
+ * -- If the returned value is not the point at infinity, then it was properly
+ * computed.
+ * -- Otherwise, if the returned flag is 1, then P1+P2 = 0, and the result
+ * is indeed the point at infinity.
+ * -- Otherwise (result is infinity, flag is 0), then P1 = P2 and we should
+ * use the 'double' code.
+ *
+ * Cost: 16 multiplications
+ */
+static const uint16_t code_add[] = {
+ /*
+ * Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).
+ */
+ MMUL(t3, P2z, P2z),
+ MMUL(t1, P1x, t3),
+ MMUL(t4, P2z, t3),
+ MMUL(t3, P1y, t4),
+
+ /*
+ * Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).
+ */
+ MMUL(t4, P1z, P1z),
+ MMUL(t2, P2x, t4),
+ MMUL(t5, P1z, t4),
+ MMUL(t4, P2y, t5),
+
+ /*
+ * Compute h = u2 - u1 (in t2) and r = s2 - s1 (in t4).
+ */
+ MSUB(t2, t1),
+ MSUB(t4, t3),
+
+ /*
+ * Report cases where r = 0 through the returned flag.
+ */
+ MTZ(t4),
+
+ /*
+ * Compute u1*h^2 (in t6) and h^3 (in t5).
+ */
+ MMUL(t7, t2, t2),
+ MMUL(t6, t1, t7),
+ MMUL(t5, t7, t2),
+
+ /*
+ * Compute x3 = r^2 - h^3 - 2*u1*h^2.
+ * t1 and t7 can be used as scratch registers.
+ */
+ MMUL(P1x, t4, t4),
+ MSUB(P1x, t5),
+ MSUB(P1x, t6),
+ MSUB(P1x, t6),
+
+ /*
+ * Compute y3 = r*(u1*h^2 - x3) - s1*h^3.
+ */
+ MSUB(t6, P1x),
+ MMUL(P1y, t4, t6),
+ MMUL(t1, t5, t3),
+ MSUB(P1y, t1),
+
+ /*
+ * Compute z3 = h*z1*z2.
+ */
+ MMUL(t1, P1z, P2z),
+ MMUL(P1z, t1, t2),
+
+ ENDCODE
+};
+
+/*
+ * Check that the point is on the curve. This code snippet assumes the
+ * following conventions:
+ * -- Coordinates x and y have been freshly decoded in P1 (but not
+ * converted to Montgomery coordinates yet).
+ * -- P2x, P2y and P2z are set to, respectively, R^2, b*R and 1.
+ */
+static const uint16_t code_check[] = {
+
+ /* Convert x and y to Montgomery representation. */
+ MMUL(t1, P1x, P2x),
+ MMUL(t2, P1y, P2x),
+ MSET(P1x, t1),
+ MSET(P1y, t2),
+
+ /* Compute x^3 in t1. */
+ MMUL(t2, P1x, P1x),
+ MMUL(t1, P1x, t2),
+
+ /* Subtract 3*x from t1. */
+ MSUB(t1, P1x),
+ MSUB(t1, P1x),
+ MSUB(t1, P1x),
+
+ /* Add b. */
+ MADD(t1, P2y),
+
+ /* Compute y^2 in t2. */
+ MMUL(t2, P1y, P1y),
+
+ /* Compare y^2 with x^3 - 3*x + b; they must match. */
+ MSUB(t1, t2),
+ MTZ(t1),
+
+ /* Set z to 1 (in Montgomery representation). */
+ MMUL(P1z, P2x, P2z),
+
+ ENDCODE
+};
+
+/*
+ * Conversion back to affine coordinates. This code snippet assumes that
+ * the z coordinate of P2 is set to 1 (not in Montgomery representation).
+ */
+static const uint16_t code_affine[] = {
+
+ /* Save z*R in t1. */
+ MSET(t1, P1z),
+
+ /* Compute z^3 in t2. */
+ MMUL(t2, P1z, P1z),
+ MMUL(t3, P1z, t2),
+ MMUL(t2, t3, P2z),
+
+ /* Invert to (1/z^3) in t2. */
+ MINV(t2, t3, t4),
+
+ /* Compute y. */
+ MSET(t3, P1y),
+ MMUL(P1y, t2, t3),
+
+ /* Compute (1/z^2) in t3. */
+ MMUL(t3, t2, t1),
+
+ /* Compute x. */
+ MSET(t2, P1x),
+ MMUL(P1x, t2, t3),
+
+ ENDCODE
+};
+
+static uint32_t
+run_code(jacobian *P1, const jacobian *P2,
+ const curve_params *cc, const uint16_t *code)
+{
+ uint32_t r;
+ uint32_t t[13][I31_LEN];
+ size_t u;
+
+ r = 1;
+
+ /*
+ * Copy the two operands in the dedicated registers.
+ */
+ memcpy(t[P1x], P1->c, 3 * I31_LEN * sizeof(uint32_t));
+ memcpy(t[P2x], P2->c, 3 * I31_LEN * sizeof(uint32_t));
+
+ /*
+ * Run formulas.
+ */
+ for (u = 0;; u ++) {
+ unsigned op, d, a, b;
+
+ op = code[u];
+ if (op == 0) {
+ break;
+ }
+ d = (op >> 8) & 0x0F;
+ a = (op >> 4) & 0x0F;
+ b = op & 0x0F;
+ op >>= 12;
+ switch (op) {
+ uint32_t ctl;
+ size_t plen;
+ unsigned char tp[(BR_MAX_EC_SIZE + 7) >> 3];
+
+ case 0:
+ memcpy(t[d], t[a], I31_LEN * sizeof(uint32_t));
+ break;
+ case 1:
+ ctl = br_i31_add(t[d], t[a], 1);
+ ctl |= NOT(br_i31_sub(t[d], cc->p, 0));
+ br_i31_sub(t[d], cc->p, ctl);
+ break;
+ case 2:
+ br_i31_add(t[d], cc->p, br_i31_sub(t[d], t[a], 1));
+ break;
+ case 3:
+ br_i31_montymul(t[d], t[a], t[b], cc->p, cc->p0i);
+ break;
+ case 4:
+ plen = (cc->p[0] - (cc->p[0] >> 5) + 7) >> 3;
+ br_i31_encode(tp, plen, cc->p);
+ tp[plen - 1] -= 2;
+ br_i31_modpow(t[d], tp, plen,
+ cc->p, cc->p0i, t[a], t[b]);
+ break;
+ default:
+ r &= ~br_i31_iszero(t[d]);
+ break;
+ }
+ }
+
+ /*
+ * Copy back result.
+ */
+ memcpy(P1->c, t[P1x], 3 * I31_LEN * sizeof(uint32_t));
+ return r;
+}
+
+static void
+set_one(uint32_t *x, const uint32_t *p)
+{
+ size_t plen;
+
+ plen = (p[0] + 63) >> 5;
+ memset(x, 0, plen * sizeof *x);
+ x[0] = p[0];
+ x[1] = 0x00000001;
+}
+
+static void
+point_zero(jacobian *P, const curve_params *cc)
+{
+ memset(P, 0, sizeof *P);
+ P->c[0][0] = P->c[1][0] = P->c[2][0] = cc->p[0];
+}
+
+static inline void
+point_double(jacobian *P, const curve_params *cc)
+{
+ run_code(P, P, cc, code_double);
+}
+
+static inline uint32_t
+point_add(jacobian *P1, const jacobian *P2, const curve_params *cc)
+{
+ return run_code(P1, P2, cc, code_add);
+}
+
+static void
+point_mul(jacobian *P, const unsigned char *x, size_t xlen,
+ const curve_params *cc)
+{
+ /*
+ * We do a simple double-and-add ladder with a 2-bit window
+ * to make only one add every two doublings. We thus first
+ * precompute 2P and 3P in some local buffers.
+ *
+ * We always perform two doublings and one addition; the
+ * addition is with P, 2P and 3P and is done in a temporary
+ * array.
+ *
+ * The addition code cannot handle cases where one of the
+ * operands is infinity, which is the case at the start of the
+ * ladder. We therefore need to maintain a flag that controls
+ * this situation.
+ */
+ uint32_t qz;
+ jacobian P2, P3, Q, T, U;
+
+ memcpy(&P2, P, sizeof P2);
+ point_double(&P2, cc);
+ memcpy(&P3, P, sizeof P3);
+ point_add(&P3, &P2, cc);
+
+ point_zero(&Q, cc);
+ qz = 1;
+ while (xlen -- > 0) {
+ int k;
+
+ for (k = 6; k >= 0; k -= 2) {
+ uint32_t bits;
+ uint32_t bnz;
+
+ point_double(&Q, cc);
+ point_double(&Q, cc);
+ memcpy(&T, P, sizeof T);
+ memcpy(&U, &Q, sizeof U);
+ bits = (*x >> k) & (uint32_t)3;
+ bnz = NEQ(bits, 0);
+ CCOPY(EQ(bits, 2), &T, &P2, sizeof T);
+ CCOPY(EQ(bits, 3), &T, &P3, sizeof T);
+ point_add(&U, &T, cc);
+ CCOPY(bnz & qz, &Q, &T, sizeof Q);
+ CCOPY(bnz & ~qz, &Q, &U, sizeof Q);
+ qz &= ~bnz;
+ }
+ x ++;
+ }
+ memcpy(P, &Q, sizeof Q);
+}
+
+/*
+ * Decode point into Jacobian coordinates. This function does not support
+ * the point at infinity. If the point is invalid then this returns 0, but
+ * the coordinates are still set to properly formed field elements.
+ */
+static uint32_t
+point_decode(jacobian *P, const void *src, size_t len, const curve_params *cc)
+{
+ /*
+ * Points must use uncompressed format:
+ * -- first byte is 0x04;
+ * -- coordinates X and Y use unsigned big-endian, with the same
+ * length as the field modulus.
+ *
+ * We don't support hybrid format (uncompressed, but first byte
+ * has value 0x06 or 0x07, depending on the least significant bit
+ * of Y) because it is rather useless, and explicitly forbidden
+ * by PKIX (RFC 5480, section 2.2).
+ *
+ * We don't support compressed format either, because it is not
+ * much used in practice (there are or were patent-related
+ * concerns about point compression, which explains the lack of
+ * generalised support). Also, point compression support would
+ * need a bit more code.
+ */
+ const unsigned char *buf;
+ size_t plen, zlen;
+ uint32_t r;
+ jacobian Q;
+
+ buf = src;
+ point_zero(P, cc);
+ plen = (cc->p[0] - (cc->p[0] >> 5) + 7) >> 3;
+ if (len != 1 + (plen << 1)) {
+ return 0;
+ }
+ r = br_i31_decode_mod(P->c[0], buf + 1, plen, cc->p);
+ r &= br_i31_decode_mod(P->c[1], buf + 1 + plen, plen, cc->p);
+
+ /*
+ * Check first byte.
+ */
+ r &= EQ(buf[0], 0x04);
+ /* obsolete
+ r &= EQ(buf[0], 0x04) | (EQ(buf[0] & 0xFE, 0x06)
+ & ~(uint32_t)(buf[0] ^ buf[plen << 1]));
+ */
+
+ /*
+ * Convert coordinates and check that the point is valid.
+ */
+ zlen = ((cc->p[0] + 63) >> 5) * sizeof(uint32_t);
+ memcpy(Q.c[0], cc->R2, zlen);
+ memcpy(Q.c[1], cc->b, zlen);
+ set_one(Q.c[2], cc->p);
+ r &= ~run_code(P, &Q, cc, code_check);
+ return r;
+}
+
+/*
+ * Encode a point. This method assumes that the point is correct and is
+ * not the point at infinity. Encoded size is always 1+2*plen, where
+ * plen is the field modulus length, in bytes.
+ */
+static void
+point_encode(void *dst, const jacobian *P, const curve_params *cc)
+{
+ unsigned char *buf;
+ uint32_t xbl;
+ size_t plen;
+ jacobian Q, T;
+
+ buf = dst;
+ xbl = cc->p[0];
+ xbl -= (xbl >> 5);
+ plen = (xbl + 7) >> 3;
+ buf[0] = 0x04;
+ memcpy(&Q, P, sizeof *P);
+ set_one(T.c[2], cc->p);
+ run_code(&Q, &T, cc, code_affine);
+ br_i31_encode(buf + 1, plen, Q.c[0]);
+ br_i31_encode(buf + 1 + plen, plen, Q.c[1]);
+}
+
+static const br_ec_curve_def *
+id_to_curve_def(int curve)
+{
+ switch (curve) {
+ case BR_EC_secp256r1:
+ return &br_secp256r1;
+ case BR_EC_secp384r1:
+ return &br_secp384r1;
+ case BR_EC_secp521r1:
+ return &br_secp521r1;
+ }
+ return NULL;
+}
+
+static const unsigned char *
+api_generator(int curve, size_t *len)
+{
+ const br_ec_curve_def *cd;
+
+ cd = id_to_curve_def(curve);
+ *len = cd->generator_len;
+ return cd->generator;
+}
+
+static const unsigned char *
+api_order(int curve, size_t *len)
+{
+ const br_ec_curve_def *cd;
+
+ cd = id_to_curve_def(curve);
+ *len = cd->order_len;
+ return cd->order;
+}
+
+static size_t
+api_xoff(int curve, size_t *len)
+{
+ api_generator(curve, len);
+ *len >>= 1;
+ return 1;
+}
+
+static uint32_t
+api_mul(unsigned char *G, size_t Glen,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ uint32_t r;
+ const curve_params *cc;
+ jacobian P;
+
+ cc = id_to_curve(curve);
+ r = point_decode(&P, G, Glen, cc);
+ point_mul(&P, x, xlen, cc);
+ point_encode(G, &P, cc);
+ return r;
+}
+
+static size_t
+api_mulgen(unsigned char *R,
+ const unsigned char *x, size_t xlen, int curve)
+{
+ const unsigned char *G;
+ size_t Glen;
+
+ G = api_generator(curve, &Glen);
+ memcpy(R, G, Glen);
+ api_mul(R, Glen, x, xlen, curve);
+ return Glen;
+}
+
+static uint32_t
+api_muladd(unsigned char *A, const unsigned char *B, size_t len,
+ const unsigned char *x, size_t xlen,
+ const unsigned char *y, size_t ylen, int curve)
+{
+ uint32_t r, t, z;
+ const curve_params *cc;
+ jacobian P, Q;
+
+ /*
+ * TODO: see about merging the two ladders. Right now, we do
+ * two independent point multiplications, which is a bit
+ * wasteful of CPU resources (but yields short code).
+ */
+
+ cc = id_to_curve(curve);
+ r = point_decode(&P, A, len, cc);
+ if (B == NULL) {
+ size_t Glen;
+
+ B = api_generator(curve, &Glen);
+ }
+ r &= point_decode(&Q, B, len, cc);
+ point_mul(&P, x, xlen, cc);
+ point_mul(&Q, y, ylen, cc);
+
+ /*
+ * We want to compute P+Q. Since the base points A and B are distinct
+ * from infinity, and the multipliers are non-zero and lower than the
+ * curve order, then we know that P and Q are non-infinity. This
+ * leaves two special situations to test for:
+ * -- If P = Q then we must use point_double().
+ * -- If P+Q = 0 then we must report an error.
+ */
+ t = point_add(&P, &Q, cc);
+ point_double(&Q, cc);
+ z = br_i31_iszero(P.c[2]);
+
+ /*
+ * If z is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we
+ * have the following:
+ *
+ * z = 0, t = 0 return P (normal addition)
+ * z = 0, t = 1 return P (normal addition)
+ * z = 1, t = 0 return Q (a 'double' case)
+ * z = 1, t = 1 report an error (P+Q = 0)
+ */
+ CCOPY(z & ~t, &P, &Q, sizeof Q);
+ point_encode(A, &P, cc);
+ r &= ~(z & t);
+
+ return r;
+}
+
+/* see bearssl_ec.h */
+const br_ec_impl br_ec_prime_i31 = {
+ (uint32_t)0x03800000,
+ &api_generator,
+ &api_order,
+ &api_xoff,
+ &api_mul,
+ &api_mulgen,
+ &api_muladd
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_pubkey.c b/test/monniaux/BearSSL/src/ec/ec_pubkey.c
new file mode 100644
index 00000000..383ff286
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_pubkey.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char POINT_LEN[] = {
+ 0, /* 0: not a valid curve ID */
+ 43, /* sect163k1 */
+ 43, /* sect163r1 */
+ 43, /* sect163r2 */
+ 51, /* sect193r1 */
+ 51, /* sect193r2 */
+ 61, /* sect233k1 */
+ 61, /* sect233r1 */
+ 61, /* sect239k1 */
+ 73, /* sect283k1 */
+ 73, /* sect283r1 */
+ 105, /* sect409k1 */
+ 105, /* sect409r1 */
+ 145, /* sect571k1 */
+ 145, /* sect571r1 */
+ 41, /* secp160k1 */
+ 41, /* secp160r1 */
+ 41, /* secp160r2 */
+ 49, /* secp192k1 */
+ 49, /* secp192r1 */
+ 57, /* secp224k1 */
+ 57, /* secp224r1 */
+ 65, /* secp256k1 */
+ 65, /* secp256r1 */
+ 97, /* secp384r1 */
+ 133, /* secp521r1 */
+ 65, /* brainpoolP256r1 */
+ 97, /* brainpoolP384r1 */
+ 129, /* brainpoolP512r1 */
+ 32, /* curve25519 */
+ 56, /* curve448 */
+};
+
+/* see bearssl_ec.h */
+size_t
+br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,
+ void *kbuf, const br_ec_private_key *sk)
+{
+ int curve;
+ size_t len;
+
+ curve = sk->curve;
+ if (curve < 0 || curve >= 32 || curve >= (int)(sizeof POINT_LEN)
+ || ((impl->supported_curves >> curve) & 1) == 0)
+ {
+ return 0;
+ }
+ if (kbuf == NULL) {
+ return POINT_LEN[curve];
+ }
+ len = impl->mulgen(kbuf, sk->x, sk->xlen, curve);
+ if (pk != NULL) {
+ pk->curve = curve;
+ pk->q = kbuf;
+ pk->qlen = len;
+ }
+ return len;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ec_secp256r1.c b/test/monniaux/BearSSL/src/ec/ec_secp256r1.c
new file mode 100644
index 00000000..a9d6c456
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_secp256r1.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char P256_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
+ 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
+};
+
+static const unsigned char P256_G[] = {
+ 0x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42,
+ 0x47, 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40,
+ 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33,
+ 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2,
+ 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,
+ 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E,
+ 0x16, 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E,
+ 0xCE, 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51,
+ 0xF5
+};
+
+/* see inner.h */
+const br_ec_curve_def br_secp256r1 = {
+ BR_EC_secp256r1,
+ P256_N, sizeof P256_N,
+ P256_G, sizeof P256_G
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_secp384r1.c b/test/monniaux/BearSSL/src/ec/ec_secp384r1.c
new file mode 100644
index 00000000..693d93e4
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_secp384r1.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char P384_N[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xC7, 0x63, 0x4D, 0x81, 0xF4, 0x37, 0x2D, 0xDF,
+ 0x58, 0x1A, 0x0D, 0xB2, 0x48, 0xB0, 0xA7, 0x7A,
+ 0xEC, 0xEC, 0x19, 0x6A, 0xCC, 0xC5, 0x29, 0x73
+};
+
+static const unsigned char P384_G[] = {
+ 0x04, 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05,
+ 0x37, 0x8E, 0xB1, 0xC7, 0x1E, 0xF3, 0x20, 0xAD,
+ 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B,
+ 0x98, 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A,
+ 0x38, 0x55, 0x02, 0xF2, 0x5D, 0xBF, 0x55, 0x29,
+ 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A,
+ 0xB7, 0x36, 0x17, 0xDE, 0x4A, 0x96, 0x26, 0x2C,
+ 0x6F, 0x5D, 0x9E, 0x98, 0xBF, 0x92, 0x92, 0xDC,
+ 0x29, 0xF8, 0xF4, 0x1D, 0xBD, 0x28, 0x9A, 0x14,
+ 0x7C, 0xE9, 0xDA, 0x31, 0x13, 0xB5, 0xF0, 0xB8,
+ 0xC0, 0x0A, 0x60, 0xB1, 0xCE, 0x1D, 0x7E, 0x81,
+ 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E,
+ 0x5F
+};
+
+/* see inner.h */
+const br_ec_curve_def br_secp384r1 = {
+ BR_EC_secp384r1,
+ P384_N, sizeof P384_N,
+ P384_G, sizeof P384_G
+};
diff --git a/test/monniaux/BearSSL/src/ec/ec_secp521r1.c b/test/monniaux/BearSSL/src/ec/ec_secp521r1.c
new file mode 100644
index 00000000..161acd0e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ec_secp521r1.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const unsigned char P521_N[] = {
+ 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFA, 0x51, 0x86, 0x87, 0x83, 0xBF, 0x2F,
+ 0x96, 0x6B, 0x7F, 0xCC, 0x01, 0x48, 0xF7, 0x09,
+ 0xA5, 0xD0, 0x3B, 0xB5, 0xC9, 0xB8, 0x89, 0x9C,
+ 0x47, 0xAE, 0xBB, 0x6F, 0xB7, 0x1E, 0x91, 0x38,
+ 0x64, 0x09
+};
+
+static const unsigned char P521_G[] = {
+ 0x04, 0x00, 0xC6, 0x85, 0x8E, 0x06, 0xB7, 0x04,
+ 0x04, 0xE9, 0xCD, 0x9E, 0x3E, 0xCB, 0x66, 0x23,
+ 0x95, 0xB4, 0x42, 0x9C, 0x64, 0x81, 0x39, 0x05,
+ 0x3F, 0xB5, 0x21, 0xF8, 0x28, 0xAF, 0x60, 0x6B,
+ 0x4D, 0x3D, 0xBA, 0xA1, 0x4B, 0x5E, 0x77, 0xEF,
+ 0xE7, 0x59, 0x28, 0xFE, 0x1D, 0xC1, 0x27, 0xA2,
+ 0xFF, 0xA8, 0xDE, 0x33, 0x48, 0xB3, 0xC1, 0x85,
+ 0x6A, 0x42, 0x9B, 0xF9, 0x7E, 0x7E, 0x31, 0xC2,
+ 0xE5, 0xBD, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6A,
+ 0x78, 0x9A, 0x3B, 0xC0, 0x04, 0x5C, 0x8A, 0x5F,
+ 0xB4, 0x2C, 0x7D, 0x1B, 0xD9, 0x98, 0xF5, 0x44,
+ 0x49, 0x57, 0x9B, 0x44, 0x68, 0x17, 0xAF, 0xBD,
+ 0x17, 0x27, 0x3E, 0x66, 0x2C, 0x97, 0xEE, 0x72,
+ 0x99, 0x5E, 0xF4, 0x26, 0x40, 0xC5, 0x50, 0xB9,
+ 0x01, 0x3F, 0xAD, 0x07, 0x61, 0x35, 0x3C, 0x70,
+ 0x86, 0xA2, 0x72, 0xC2, 0x40, 0x88, 0xBE, 0x94,
+ 0x76, 0x9F, 0xD1, 0x66, 0x50
+};
+
+/* see inner.h */
+const br_ec_curve_def br_secp521r1 = {
+ BR_EC_secp521r1,
+ P521_N, sizeof P521_N,
+ P521_G, sizeof P521_G
+};
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_atr.c b/test/monniaux/BearSSL/src/ec/ecdsa_atr.c
new file mode 100644
index 00000000..3a11226e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_atr.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_asn1_to_raw(void *sig, size_t sig_len)
+{
+ /*
+ * Note: this code is a bit lenient in that it accepts a few
+ * deviations to DER with regards to minimality of encoding of
+ * lengths and integer values. These deviations are still
+ * unambiguous.
+ *
+ * Signature format is a SEQUENCE of two INTEGER values. We
+ * support only integers of less than 127 bytes each (signed
+ * encoding) so the resulting raw signature will have length
+ * at most 254 bytes.
+ */
+
+ unsigned char *buf, *r, *s;
+ size_t zlen, rlen, slen, off;
+ unsigned char tmp[254];
+
+ buf = sig;
+ if (sig_len < 8) {
+ return 0;
+ }
+
+ /*
+ * First byte is SEQUENCE tag.
+ */
+ if (buf[0] != 0x30) {
+ return 0;
+ }
+
+ /*
+ * The SEQUENCE length will be encoded over one or two bytes. We
+ * limit the total SEQUENCE contents to 255 bytes, because it
+ * makes things simpler; this is enough for subgroup orders up
+ * to 999 bits.
+ */
+ zlen = buf[1];
+ if (zlen > 0x80) {
+ if (zlen != 0x81) {
+ return 0;
+ }
+ zlen = buf[2];
+ if (zlen != sig_len - 3) {
+ return 0;
+ }
+ off = 3;
+ } else {
+ if (zlen != sig_len - 2) {
+ return 0;
+ }
+ off = 2;
+ }
+
+ /*
+ * First INTEGER (r).
+ */
+ if (buf[off ++] != 0x02) {
+ return 0;
+ }
+ rlen = buf[off ++];
+ if (rlen >= 0x80) {
+ return 0;
+ }
+ r = buf + off;
+ off += rlen;
+
+ /*
+ * Second INTEGER (s).
+ */
+ if (off + 2 > sig_len) {
+ return 0;
+ }
+ if (buf[off ++] != 0x02) {
+ return 0;
+ }
+ slen = buf[off ++];
+ if (slen >= 0x80 || slen != sig_len - off) {
+ return 0;
+ }
+ s = buf + off;
+
+ /*
+ * Removing leading zeros from r and s.
+ */
+ while (rlen > 0 && *r == 0) {
+ rlen --;
+ r ++;
+ }
+ while (slen > 0 && *s == 0) {
+ slen --;
+ s ++;
+ }
+
+ /*
+ * Compute common length for the two integers, then copy integers
+ * into the temporary buffer, and finally copy it back over the
+ * signature buffer.
+ */
+ zlen = rlen > slen ? rlen : slen;
+ sig_len = zlen << 1;
+ memset(tmp, 0, sig_len);
+ memcpy(tmp + zlen - rlen, r, rlen);
+ memcpy(tmp + sig_len - slen, s, slen);
+ memcpy(sig, tmp, sig_len);
+ return sig_len;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_asn1.c
new file mode 100644
index 00000000..afbf8acb
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_asn1.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+br_ecdsa_sign
+br_ecdsa_sign_asn1_get_default(void)
+{
+#if BR_LOMUL
+ return &br_ecdsa_i15_sign_asn1;
+#else
+ return &br_ecdsa_i31_sign_asn1;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_raw.c
new file mode 100644
index 00000000..287c9704
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_default_sign_raw.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+br_ecdsa_sign
+br_ecdsa_sign_raw_get_default(void)
+{
+#if BR_LOMUL
+ return &br_ecdsa_i15_sign_raw;
+#else
+ return &br_ecdsa_i31_sign_raw;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_asn1.c
new file mode 100644
index 00000000..fe0996e8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_asn1.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+br_ecdsa_vrfy
+br_ecdsa_vrfy_asn1_get_default(void)
+{
+#if BR_LOMUL
+ return &br_ecdsa_i15_vrfy_asn1;
+#else
+ return &br_ecdsa_i31_vrfy_asn1;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_raw.c
new file mode 100644
index 00000000..e564a105
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_default_vrfy_raw.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ec.h */
+br_ecdsa_vrfy
+br_ecdsa_vrfy_raw_get_default(void)
+{
+#if BR_LOMUL
+ return &br_ecdsa_i15_vrfy_raw;
+#else
+ return &br_ecdsa_i31_vrfy_raw;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i15_bits.c b/test/monniaux/BearSSL/src/ec/ecdsa_i15_bits.c
new file mode 100644
index 00000000..402d14a6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i15_bits.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_ecdsa_i15_bits2int(uint16_t *x,
+ const void *src, size_t len, uint32_t ebitlen)
+{
+ uint32_t bitlen, hbitlen;
+ int sc;
+
+ bitlen = ebitlen - (ebitlen >> 4);
+ hbitlen = (uint32_t)len << 3;
+ if (hbitlen > bitlen) {
+ len = (bitlen + 7) >> 3;
+ sc = (int)((hbitlen - bitlen) & 7);
+ } else {
+ sc = 0;
+ }
+ br_i15_zero(x, ebitlen);
+ br_i15_decode(x, src, len);
+ br_i15_rshift(x, sc);
+ x[0] = ebitlen;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_asn1.c
new file mode 100644
index 00000000..ab4a283c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_asn1.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_i15_sign_asn1(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig)
+{
+ unsigned char rsig[(ORDER_LEN << 1) + 12];
+ size_t sig_len;
+
+ sig_len = br_ecdsa_i15_sign_raw(impl, hf, hash_value, sk, rsig);
+ if (sig_len == 0) {
+ return 0;
+ }
+ sig_len = br_ecdsa_raw_to_asn1(rsig, sig_len);
+ memcpy(sig, rsig, sig_len);
+ return sig_len;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_raw.c
new file mode 100644
index 00000000..39b2e1d7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i15_sign_raw.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define I15_LEN ((BR_MAX_EC_SIZE + 29) / 15)
+#define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))
+#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig)
+{
+ /*
+ * IMPORTANT: this code is fit only for curves with a prime
+ * order. This is needed so that modular reduction of the X
+ * coordinate of a point can be done with a simple subtraction.
+ * We also rely on the last byte of the curve order to be distinct
+ * from 0 and 1.
+ */
+ const br_ec_curve_def *cd;
+ uint16_t n[I15_LEN], r[I15_LEN], s[I15_LEN], x[I15_LEN];
+ uint16_t m[I15_LEN], k[I15_LEN], t1[I15_LEN], t2[I15_LEN];
+ unsigned char tt[ORDER_LEN << 1];
+ unsigned char eU[POINT_LEN];
+ size_t hash_len, nlen, ulen;
+ uint16_t n0i;
+ uint32_t ctl;
+ br_hmac_drbg_context drbg;
+
+ /*
+ * If the curve is not supported, then exit with an error.
+ */
+ if (((impl->supported_curves >> sk->curve) & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Get the curve parameters (generator and order).
+ */
+ switch (sk->curve) {
+ case BR_EC_secp256r1:
+ cd = &br_secp256r1;
+ break;
+ case BR_EC_secp384r1:
+ cd = &br_secp384r1;
+ break;
+ case BR_EC_secp521r1:
+ cd = &br_secp521r1;
+ break;
+ default:
+ return 0;
+ }
+
+ /*
+ * Get modulus.
+ */
+ nlen = cd->order_len;
+ br_i15_decode(n, cd->order, nlen);
+ n0i = br_i15_ninv15(n[1]);
+
+ /*
+ * Get private key as an i15 integer. This also checks that the
+ * private key is well-defined (not zero, and less than the
+ * curve order).
+ */
+ if (!br_i15_decode_mod(x, sk->x, sk->xlen, n)) {
+ return 0;
+ }
+ if (br_i15_iszero(x)) {
+ return 0;
+ }
+
+ /*
+ * Get hash length.
+ */
+ hash_len = (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+
+ /*
+ * Truncate and reduce the hash value modulo the curve order.
+ */
+ br_ecdsa_i15_bits2int(m, hash_value, hash_len, n[0]);
+ br_i15_sub(m, n, br_i15_sub(m, n, 0) ^ 1);
+
+ /*
+ * RFC 6979 generation of the "k" value.
+ *
+ * The process uses HMAC_DRBG (with the hash function used to
+ * process the message that is to be signed). The seed is the
+ * concatenation of the encodings of the private key and
+ * the hash value (after truncation and modular reduction).
+ */
+ br_i15_encode(tt, nlen, x);
+ br_i15_encode(tt + nlen, nlen, m);
+ br_hmac_drbg_init(&drbg, hf, tt, nlen << 1);
+ for (;;) {
+ br_hmac_drbg_generate(&drbg, tt, nlen);
+ br_ecdsa_i15_bits2int(k, tt, nlen, n[0]);
+ if (br_i15_iszero(k)) {
+ continue;
+ }
+ if (br_i15_sub(k, n, 0)) {
+ break;
+ }
+ }
+
+ /*
+ * Compute k*G and extract the X coordinate, then reduce it
+ * modulo the curve order. Since we support only curves with
+ * prime order, that reduction is only a matter of computing
+ * a subtraction.
+ */
+ br_i15_encode(tt, nlen, k);
+ ulen = impl->mulgen(eU, tt, nlen, sk->curve);
+ br_i15_zero(r, n[0]);
+ br_i15_decode(r, &eU[1], ulen >> 1);
+ r[0] = n[0];
+ br_i15_sub(r, n, br_i15_sub(r, n, 0) ^ 1);
+
+ /*
+ * Compute 1/k in double-Montgomery representation. We do so by
+ * first converting _from_ Montgomery representation (twice),
+ * then using a modular exponentiation.
+ */
+ br_i15_from_monty(k, n, n0i);
+ br_i15_from_monty(k, n, n0i);
+ memcpy(tt, cd->order, nlen);
+ tt[nlen - 1] -= 2;
+ br_i15_modpow(k, tt, nlen, n, n0i, t1, t2);
+
+ /*
+ * Compute s = (m+xr)/k (mod n).
+ * The k[] array contains R^2/k (double-Montgomery representation);
+ * we thus can use direct Montgomery multiplications and conversions
+ * from Montgomery, avoiding any call to br_i15_to_monty() (which
+ * is slower).
+ */
+ br_i15_from_monty(m, n, n0i);
+ br_i15_montymul(t1, x, r, n, n0i);
+ ctl = br_i15_add(t1, m, 1);
+ ctl |= br_i15_sub(t1, n, 0) ^ 1;
+ br_i15_sub(t1, n, ctl);
+ br_i15_montymul(s, t1, k, n, n0i);
+
+ /*
+ * Encode r and s in the signature.
+ */
+ br_i15_encode(sig, nlen, r);
+ br_i15_encode((unsigned char *)sig + nlen, nlen, s);
+ return nlen << 1;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_asn1.c
new file mode 100644
index 00000000..f4bef997
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_asn1.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define FIELD_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+uint32_t
+br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk,
+ const void *sig, size_t sig_len)
+{
+ /*
+ * We use a double-sized buffer because a malformed ASN.1 signature
+ * may trigger a size expansion when converting to "raw" format.
+ */
+ unsigned char rsig[(FIELD_LEN << 2) + 24];
+
+ if (sig_len > ((sizeof rsig) >> 1)) {
+ return 0;
+ }
+ memcpy(rsig, sig, sig_len);
+ sig_len = br_ecdsa_asn1_to_raw(rsig, sig_len);
+ return br_ecdsa_i15_vrfy_raw(impl, hash, hash_len, pk, rsig, sig_len);
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_raw.c
new file mode 100644
index 00000000..14dd5e46
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i15_vrfy_raw.c
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define I15_LEN ((BR_MAX_EC_SIZE + 29) / 15)
+#define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))
+
+/* see bearssl_ec.h */
+uint32_t
+br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk,
+ const void *sig, size_t sig_len)
+{
+ /*
+ * IMPORTANT: this code is fit only for curves with a prime
+ * order. This is needed so that modular reduction of the X
+ * coordinate of a point can be done with a simple subtraction.
+ */
+ const br_ec_curve_def *cd;
+ uint16_t n[I15_LEN], r[I15_LEN], s[I15_LEN], t1[I15_LEN], t2[I15_LEN];
+ unsigned char tx[(BR_MAX_EC_SIZE + 7) >> 3];
+ unsigned char ty[(BR_MAX_EC_SIZE + 7) >> 3];
+ unsigned char eU[POINT_LEN];
+ size_t nlen, rlen, ulen;
+ uint16_t n0i;
+ uint32_t res;
+
+ /*
+ * If the curve is not supported, then report an error.
+ */
+ if (((impl->supported_curves >> pk->curve) & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Get the curve parameters (generator and order).
+ */
+ switch (pk->curve) {
+ case BR_EC_secp256r1:
+ cd = &br_secp256r1;
+ break;
+ case BR_EC_secp384r1:
+ cd = &br_secp384r1;
+ break;
+ case BR_EC_secp521r1:
+ cd = &br_secp521r1;
+ break;
+ default:
+ return 0;
+ }
+
+ /*
+ * Signature length must be even.
+ */
+ if (sig_len & 1) {
+ return 0;
+ }
+ rlen = sig_len >> 1;
+
+ /*
+ * Public key point must have the proper size for this curve.
+ */
+ if (pk->qlen != cd->generator_len) {
+ return 0;
+ }
+
+ /*
+ * Get modulus; then decode the r and s values. They must be
+ * lower than the modulus, and s must not be null.
+ */
+ nlen = cd->order_len;
+ br_i15_decode(n, cd->order, nlen);
+ n0i = br_i15_ninv15(n[1]);
+ if (!br_i15_decode_mod(r, sig, rlen, n)) {
+ return 0;
+ }
+ if (!br_i15_decode_mod(s, (const unsigned char *)sig + rlen, rlen, n)) {
+ return 0;
+ }
+ if (br_i15_iszero(s)) {
+ return 0;
+ }
+
+ /*
+ * Invert s. We do that with a modular exponentiation; we use
+ * the fact that for all the curves we support, the least
+ * significant byte is not 0 or 1, so we can subtract 2 without
+ * any carry to process.
+ * We also want 1/s in Montgomery representation, which can be
+ * done by converting _from_ Montgomery representation before
+ * the inversion (because (1/s)*R = 1/(s/R)).
+ */
+ br_i15_from_monty(s, n, n0i);
+ memcpy(tx, cd->order, nlen);
+ tx[nlen - 1] -= 2;
+ br_i15_modpow(s, tx, nlen, n, n0i, t1, t2);
+
+ /*
+ * Truncate the hash to the modulus length (in bits) and reduce
+ * it modulo the curve order. The modular reduction can be done
+ * with a subtraction since the truncation already reduced the
+ * value to the modulus bit length.
+ */
+ br_ecdsa_i15_bits2int(t1, hash, hash_len, n[0]);
+ br_i15_sub(t1, n, br_i15_sub(t1, n, 0) ^ 1);
+
+ /*
+ * Multiply the (truncated, reduced) hash value with 1/s, result in
+ * t2, encoded in ty.
+ */
+ br_i15_montymul(t2, t1, s, n, n0i);
+ br_i15_encode(ty, nlen, t2);
+
+ /*
+ * Multiply r with 1/s, result in t1, encoded in tx.
+ */
+ br_i15_montymul(t1, r, s, n, n0i);
+ br_i15_encode(tx, nlen, t1);
+
+ /*
+ * Compute the point x*Q + y*G.
+ */
+ ulen = cd->generator_len;
+ memcpy(eU, pk->q, ulen);
+ res = impl->muladd(eU, NULL, ulen,
+ tx, nlen, ty, nlen, cd->curve);
+
+ /*
+ * Get the X coordinate, reduce modulo the curve order, and
+ * compare with the 'r' value.
+ *
+ * The modular reduction can be done with subtractions because
+ * we work with curves of prime order, so the curve order is
+ * close to the field order (Hasse's theorem).
+ */
+ br_i15_zero(t1, n[0]);
+ br_i15_decode(t1, &eU[1], ulen >> 1);
+ t1[0] = n[0];
+ br_i15_sub(t1, n, br_i15_sub(t1, n, 0) ^ 1);
+ res &= ~br_i15_sub(t1, r, 1);
+ res &= br_i15_iszero(t1);
+ return res;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i31_bits.c b/test/monniaux/BearSSL/src/ec/ecdsa_i31_bits.c
new file mode 100644
index 00000000..9a8d6730
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i31_bits.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_ecdsa_i31_bits2int(uint32_t *x,
+ const void *src, size_t len, uint32_t ebitlen)
+{
+ uint32_t bitlen, hbitlen;
+ int sc;
+
+ bitlen = ebitlen - (ebitlen >> 5);
+ hbitlen = (uint32_t)len << 3;
+ if (hbitlen > bitlen) {
+ len = (bitlen + 7) >> 3;
+ sc = (int)((hbitlen - bitlen) & 7);
+ } else {
+ sc = 0;
+ }
+ br_i31_zero(x, ebitlen);
+ br_i31_decode(x, src, len);
+ br_i31_rshift(x, sc);
+ x[0] = ebitlen;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_asn1.c
new file mode 100644
index 00000000..cf0d351d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_asn1.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_i31_sign_asn1(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig)
+{
+ unsigned char rsig[(ORDER_LEN << 1) + 12];
+ size_t sig_len;
+
+ sig_len = br_ecdsa_i31_sign_raw(impl, hf, hash_value, sk, rsig);
+ if (sig_len == 0) {
+ return 0;
+ }
+ sig_len = br_ecdsa_raw_to_asn1(rsig, sig_len);
+ memcpy(sig, rsig, sig_len);
+ return sig_len;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_raw.c
new file mode 100644
index 00000000..1df98fed
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i31_sign_raw.c
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define I31_LEN ((BR_MAX_EC_SIZE + 61) / 31)
+#define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))
+#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_i31_sign_raw(const br_ec_impl *impl,
+ const br_hash_class *hf, const void *hash_value,
+ const br_ec_private_key *sk, void *sig)
+{
+ /*
+ * IMPORTANT: this code is fit only for curves with a prime
+ * order. This is needed so that modular reduction of the X
+ * coordinate of a point can be done with a simple subtraction.
+ * We also rely on the last byte of the curve order to be distinct
+ * from 0 and 1.
+ */
+ const br_ec_curve_def *cd;
+ uint32_t n[I31_LEN], r[I31_LEN], s[I31_LEN], x[I31_LEN];
+ uint32_t m[I31_LEN], k[I31_LEN], t1[I31_LEN], t2[I31_LEN];
+ unsigned char tt[ORDER_LEN << 1];
+ unsigned char eU[POINT_LEN];
+ size_t hash_len, nlen, ulen;
+ uint32_t n0i, ctl;
+ br_hmac_drbg_context drbg;
+
+ /*
+ * If the curve is not supported, then exit with an error.
+ */
+ if (((impl->supported_curves >> sk->curve) & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Get the curve parameters (generator and order).
+ */
+ switch (sk->curve) {
+ case BR_EC_secp256r1:
+ cd = &br_secp256r1;
+ break;
+ case BR_EC_secp384r1:
+ cd = &br_secp384r1;
+ break;
+ case BR_EC_secp521r1:
+ cd = &br_secp521r1;
+ break;
+ default:
+ return 0;
+ }
+
+ /*
+ * Get modulus.
+ */
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ n0i = br_i31_ninv31(n[1]);
+
+ /*
+ * Get private key as an i31 integer. This also checks that the
+ * private key is well-defined (not zero, and less than the
+ * curve order).
+ */
+ if (!br_i31_decode_mod(x, sk->x, sk->xlen, n)) {
+ return 0;
+ }
+ if (br_i31_iszero(x)) {
+ return 0;
+ }
+
+ /*
+ * Get hash length.
+ */
+ hash_len = (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+
+ /*
+ * Truncate and reduce the hash value modulo the curve order.
+ */
+ br_ecdsa_i31_bits2int(m, hash_value, hash_len, n[0]);
+ br_i31_sub(m, n, br_i31_sub(m, n, 0) ^ 1);
+
+ /*
+ * RFC 6979 generation of the "k" value.
+ *
+ * The process uses HMAC_DRBG (with the hash function used to
+ * process the message that is to be signed). The seed is the
+ * concatenation of the encodings of the private key and
+ * the hash value (after truncation and modular reduction).
+ */
+ br_i31_encode(tt, nlen, x);
+ br_i31_encode(tt + nlen, nlen, m);
+ br_hmac_drbg_init(&drbg, hf, tt, nlen << 1);
+ for (;;) {
+ br_hmac_drbg_generate(&drbg, tt, nlen);
+ br_ecdsa_i31_bits2int(k, tt, nlen, n[0]);
+ if (br_i31_iszero(k)) {
+ continue;
+ }
+ if (br_i31_sub(k, n, 0)) {
+ break;
+ }
+ }
+
+ /*
+ * Compute k*G and extract the X coordinate, then reduce it
+ * modulo the curve order. Since we support only curves with
+ * prime order, that reduction is only a matter of computing
+ * a subtraction.
+ */
+ br_i31_encode(tt, nlen, k);
+ ulen = impl->mulgen(eU, tt, nlen, sk->curve);
+ br_i31_zero(r, n[0]);
+ br_i31_decode(r, &eU[1], ulen >> 1);
+ r[0] = n[0];
+ br_i31_sub(r, n, br_i31_sub(r, n, 0) ^ 1);
+
+ /*
+ * Compute 1/k in double-Montgomery representation. We do so by
+ * first converting _from_ Montgomery representation (twice),
+ * then using a modular exponentiation.
+ */
+ br_i31_from_monty(k, n, n0i);
+ br_i31_from_monty(k, n, n0i);
+ memcpy(tt, cd->order, nlen);
+ tt[nlen - 1] -= 2;
+ br_i31_modpow(k, tt, nlen, n, n0i, t1, t2);
+
+ /*
+ * Compute s = (m+xr)/k (mod n).
+ * The k[] array contains R^2/k (double-Montgomery representation);
+ * we thus can use direct Montgomery multiplications and conversions
+ * from Montgomery, avoiding any call to br_i31_to_monty() (which
+ * is slower).
+ */
+ br_i31_from_monty(m, n, n0i);
+ br_i31_montymul(t1, x, r, n, n0i);
+ ctl = br_i31_add(t1, m, 1);
+ ctl |= br_i31_sub(t1, n, 0) ^ 1;
+ br_i31_sub(t1, n, ctl);
+ br_i31_montymul(s, t1, k, n, n0i);
+
+ /*
+ * Encode r and s in the signature.
+ */
+ br_i31_encode(sig, nlen, r);
+ br_i31_encode((unsigned char *)sig + nlen, nlen, s);
+ return nlen << 1;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_asn1.c b/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_asn1.c
new file mode 100644
index 00000000..4161aaaa
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_asn1.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define FIELD_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
+
+/* see bearssl_ec.h */
+uint32_t
+br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk,
+ const void *sig, size_t sig_len)
+{
+ /*
+ * We use a double-sized buffer because a malformed ASN.1 signature
+ * may trigger a size expansion when converting to "raw" format.
+ */
+ unsigned char rsig[(FIELD_LEN << 2) + 24];
+
+ if (sig_len > ((sizeof rsig) >> 1)) {
+ return 0;
+ }
+ memcpy(rsig, sig, sig_len);
+ sig_len = br_ecdsa_asn1_to_raw(rsig, sig_len);
+ return br_ecdsa_i31_vrfy_raw(impl, hash, hash_len, pk, rsig, sig_len);
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_raw.c b/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_raw.c
new file mode 100644
index 00000000..259477fd
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_i31_vrfy_raw.c
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define I31_LEN ((BR_MAX_EC_SIZE + 61) / 31)
+#define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))
+
+/* see bearssl_ec.h */
+uint32_t
+br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
+ const void *hash, size_t hash_len,
+ const br_ec_public_key *pk,
+ const void *sig, size_t sig_len)
+{
+ /*
+ * IMPORTANT: this code is fit only for curves with a prime
+ * order. This is needed so that modular reduction of the X
+ * coordinate of a point can be done with a simple subtraction.
+ */
+ const br_ec_curve_def *cd;
+ uint32_t n[I31_LEN], r[I31_LEN], s[I31_LEN], t1[I31_LEN], t2[I31_LEN];
+ unsigned char tx[(BR_MAX_EC_SIZE + 7) >> 3];
+ unsigned char ty[(BR_MAX_EC_SIZE + 7) >> 3];
+ unsigned char eU[POINT_LEN];
+ size_t nlen, rlen, ulen;
+ uint32_t n0i, res;
+
+ /*
+ * If the curve is not supported, then report an error.
+ */
+ if (((impl->supported_curves >> pk->curve) & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Get the curve parameters (generator and order).
+ */
+ switch (pk->curve) {
+ case BR_EC_secp256r1:
+ cd = &br_secp256r1;
+ break;
+ case BR_EC_secp384r1:
+ cd = &br_secp384r1;
+ break;
+ case BR_EC_secp521r1:
+ cd = &br_secp521r1;
+ break;
+ default:
+ return 0;
+ }
+
+ /*
+ * Signature length must be even.
+ */
+ if (sig_len & 1) {
+ return 0;
+ }
+ rlen = sig_len >> 1;
+
+ /*
+ * Public key point must have the proper size for this curve.
+ */
+ if (pk->qlen != cd->generator_len) {
+ return 0;
+ }
+
+ /*
+ * Get modulus; then decode the r and s values. They must be
+ * lower than the modulus, and s must not be null.
+ */
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ n0i = br_i31_ninv31(n[1]);
+ if (!br_i31_decode_mod(r, sig, rlen, n)) {
+ return 0;
+ }
+ if (!br_i31_decode_mod(s, (const unsigned char *)sig + rlen, rlen, n)) {
+ return 0;
+ }
+ if (br_i31_iszero(s)) {
+ return 0;
+ }
+
+ /*
+ * Invert s. We do that with a modular exponentiation; we use
+ * the fact that for all the curves we support, the least
+ * significant byte is not 0 or 1, so we can subtract 2 without
+ * any carry to process.
+ * We also want 1/s in Montgomery representation, which can be
+ * done by converting _from_ Montgomery representation before
+ * the inversion (because (1/s)*R = 1/(s/R)).
+ */
+ br_i31_from_monty(s, n, n0i);
+ memcpy(tx, cd->order, nlen);
+ tx[nlen - 1] -= 2;
+ br_i31_modpow(s, tx, nlen, n, n0i, t1, t2);
+
+ /*
+ * Truncate the hash to the modulus length (in bits) and reduce
+ * it modulo the curve order. The modular reduction can be done
+ * with a subtraction since the truncation already reduced the
+ * value to the modulus bit length.
+ */
+ br_ecdsa_i31_bits2int(t1, hash, hash_len, n[0]);
+ br_i31_sub(t1, n, br_i31_sub(t1, n, 0) ^ 1);
+
+ /*
+ * Multiply the (truncated, reduced) hash value with 1/s, result in
+ * t2, encoded in ty.
+ */
+ br_i31_montymul(t2, t1, s, n, n0i);
+ br_i31_encode(ty, nlen, t2);
+
+ /*
+ * Multiply r with 1/s, result in t1, encoded in tx.
+ */
+ br_i31_montymul(t1, r, s, n, n0i);
+ br_i31_encode(tx, nlen, t1);
+
+ /*
+ * Compute the point x*Q + y*G.
+ */
+ ulen = cd->generator_len;
+ memcpy(eU, pk->q, ulen);
+ res = impl->muladd(eU, NULL, ulen,
+ tx, nlen, ty, nlen, cd->curve);
+
+ /*
+ * Get the X coordinate, reduce modulo the curve order, and
+ * compare with the 'r' value.
+ *
+ * The modular reduction can be done with subtractions because
+ * we work with curves of prime order, so the curve order is
+ * close to the field order (Hasse's theorem).
+ */
+ br_i31_zero(t1, n[0]);
+ br_i31_decode(t1, &eU[1], ulen >> 1);
+ t1[0] = n[0];
+ br_i31_sub(t1, n, br_i31_sub(t1, n, 0) ^ 1);
+ res &= ~br_i31_sub(t1, r, 1);
+ res &= br_i31_iszero(t1);
+ return res;
+}
diff --git a/test/monniaux/BearSSL/src/ec/ecdsa_rta.c b/test/monniaux/BearSSL/src/ec/ecdsa_rta.c
new file mode 100644
index 00000000..005c62c2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ec/ecdsa_rta.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Compute ASN.1 encoded length for the provided integer. The ASN.1
+ * encoding is signed, so its leading bit must have value 0; it must
+ * also be of minimal length (so leading bytes of value 0 must be
+ * removed, except if that would contradict the rule about the sign
+ * bit).
+ */
+static size_t
+asn1_int_length(const unsigned char *x, size_t xlen)
+{
+ while (xlen > 0 && *x == 0) {
+ x ++;
+ xlen --;
+ }
+ if (xlen == 0 || *x >= 0x80) {
+ xlen ++;
+ }
+ return xlen;
+}
+
+/* see bearssl_ec.h */
+size_t
+br_ecdsa_raw_to_asn1(void *sig, size_t sig_len)
+{
+ /*
+ * Internal buffer is large enough to accommodate a signature
+ * such that r and s fit on 125 bytes each (signed encoding),
+ * meaning a curve order of up to 999 bits. This is the limit
+ * that ensures "simple" length encodings.
+ */
+ unsigned char *buf;
+ size_t hlen, rlen, slen, zlen, off;
+ unsigned char tmp[257];
+
+ buf = sig;
+ if ((sig_len & 1) != 0) {
+ return 0;
+ }
+
+ /*
+ * Compute lengths for the two integers.
+ */
+ hlen = sig_len >> 1;
+ rlen = asn1_int_length(buf, hlen);
+ slen = asn1_int_length(buf + hlen, hlen);
+ if (rlen > 125 || slen > 125) {
+ return 0;
+ }
+
+ /*
+ * SEQUENCE header.
+ */
+ tmp[0] = 0x30;
+ zlen = rlen + slen + 4;
+ if (zlen >= 0x80) {
+ tmp[1] = 0x81;
+ tmp[2] = zlen;
+ off = 3;
+ } else {
+ tmp[1] = zlen;
+ off = 2;
+ }
+
+ /*
+ * First INTEGER (r).
+ */
+ tmp[off ++] = 0x02;
+ tmp[off ++] = rlen;
+ if (rlen > hlen) {
+ tmp[off] = 0x00;
+ memcpy(tmp + off + 1, buf, hlen);
+ } else {
+ memcpy(tmp + off, buf + hlen - rlen, rlen);
+ }
+ off += rlen;
+
+ /*
+ * Second INTEGER (s).
+ */
+ tmp[off ++] = 0x02;
+ tmp[off ++] = slen;
+ if (slen > hlen) {
+ tmp[off] = 0x00;
+ memcpy(tmp + off + 1, buf + hlen, hlen);
+ } else {
+ memcpy(tmp + off, buf + sig_len - slen, slen);
+ }
+ off += slen;
+
+ /*
+ * Return ASN.1 signature.
+ */
+ memcpy(sig, tmp, off);
+ return off;
+}
diff --git a/test/monniaux/BearSSL/src/hash/dig_oid.c b/test/monniaux/BearSSL/src/hash/dig_oid.c
new file mode 100644
index 00000000..cd9692c9
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/dig_oid.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * This file contains the encoded OID for the standard hash functions.
+ * Such OID appear in, for instance, the PKCS#1 v1.5 padding for RSA
+ * signatures.
+ */
+
+static const unsigned char md5_OID[] = {
+ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05
+};
+
+static const unsigned char sha1_OID[] = {
+ 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char sha224_OID[] = {
+ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char sha256_OID[] = {
+ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char sha384_OID[] = {
+ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char sha512_OID[] = {
+ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+/* see inner.h */
+const unsigned char *
+br_digest_OID(int digest_id, size_t *len)
+{
+ switch (digest_id) {
+ case br_md5_ID:
+ *len = sizeof md5_OID;
+ return md5_OID;
+ case br_sha1_ID:
+ *len = sizeof sha1_OID;
+ return sha1_OID;
+ case br_sha224_ID:
+ *len = sizeof sha224_OID;
+ return sha224_OID;
+ case br_sha256_ID:
+ *len = sizeof sha256_OID;
+ return sha256_OID;
+ case br_sha384_ID:
+ *len = sizeof sha384_OID;
+ return sha384_OID;
+ case br_sha512_ID:
+ *len = sizeof sha512_OID;
+ return sha512_OID;
+ default:
+ *len = 0;
+ return NULL;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/hash/dig_size.c b/test/monniaux/BearSSL/src/hash/dig_size.c
new file mode 100644
index 00000000..4625d2c6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/dig_size.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+size_t
+br_digest_size_by_ID(int digest_id)
+{
+ switch (digest_id) {
+ case br_md5sha1_ID:
+ return br_md5_SIZE + br_sha1_SIZE;
+ case br_md5_ID:
+ return br_md5_SIZE;
+ case br_sha1_ID:
+ return br_sha1_SIZE;
+ case br_sha224_ID:
+ return br_sha224_SIZE;
+ case br_sha256_ID:
+ return br_sha256_SIZE;
+ case br_sha384_ID:
+ return br_sha384_SIZE;
+ case br_sha512_ID:
+ return br_sha512_SIZE;
+ default:
+ /* abort(); */
+ return 0;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/hash/ghash_ctmul.c b/test/monniaux/BearSSL/src/hash/ghash_ctmul.c
new file mode 100644
index 00000000..36232025
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/ghash_ctmul.c
@@ -0,0 +1,345 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * We compute "carryless multiplications" through normal integer
+ * multiplications, masking out enough bits to create "holes" in which
+ * carries may expand without altering our bits; we really use 8 data
+ * bits per 32-bit word, spaced every fourth bit. Accumulated carries
+ * may not exceed 8 in total, which fits in 4 bits.
+ *
+ * It would be possible to use a 3-bit spacing, allowing two operands,
+ * one with 7 non-zero data bits, the other one with 10 or 11 non-zero
+ * data bits; this asymmetric splitting makes the overall code more
+ * complex with thresholds and exceptions, and does not appear to be
+ * worth the effort.
+ */
+
+/*
+ * We cannot really autodetect whether multiplications are "slow" or
+ * not. A typical example is the ARM Cortex M0+, which exists in two
+ * versions: one with a 1-cycle multiplication opcode, the other with
+ * a 32-cycle multiplication opcode. They both use exactly the same
+ * architecture and ABI, and cannot be distinguished from each other
+ * at compile-time.
+ *
+ * Since most modern CPU (even embedded CPU) still have fast
+ * multiplications, we use the "fast mul" code by default.
+ */
+
+#if BR_SLOW_MUL
+
+/*
+ * This implementation uses Karatsuba-like reduction to make fewer
+ * integer multiplications (9 instead of 16), at the expense of extra
+ * logical operations (XOR, shifts...). On modern x86 CPU that offer
+ * fast, pipelined multiplications, this code is about twice slower than
+ * the simpler code with 16 multiplications. This tendency may be
+ * reversed on low-end platforms with expensive multiplications.
+ */
+
+#define MUL32(h, l, x, y) do { \
+ uint64_t mul32tmp = MUL(x, y); \
+ (h) = (uint32_t)(mul32tmp >> 32); \
+ (l) = (uint32_t)mul32tmp; \
+ } while (0)
+
+static inline void
+bmul(uint32_t *hi, uint32_t *lo, uint32_t x, uint32_t y)
+{
+ uint32_t x0, x1, x2, x3;
+ uint32_t y0, y1, y2, y3;
+ uint32_t a0, a1, a2, a3, a4, a5, a6, a7, a8;
+ uint32_t b0, b1, b2, b3, b4, b5, b6, b7, b8;
+
+ x0 = x & (uint32_t)0x11111111;
+ x1 = x & (uint32_t)0x22222222;
+ x2 = x & (uint32_t)0x44444444;
+ x3 = x & (uint32_t)0x88888888;
+ y0 = y & (uint32_t)0x11111111;
+ y1 = y & (uint32_t)0x22222222;
+ y2 = y & (uint32_t)0x44444444;
+ y3 = y & (uint32_t)0x88888888;
+
+ /*
+ * (x0+W*x1)*(y0+W*y1) -> a0:b0
+ * (x2+W*x3)*(y2+W*y3) -> a3:b3
+ * ((x0+x2)+W*(x1+x3))*((y0+y2)+W*(y1+y3)) -> a6:b6
+ */
+ a0 = x0;
+ b0 = y0;
+ a1 = x1 >> 1;
+ b1 = y1 >> 1;
+ a2 = a0 ^ a1;
+ b2 = b0 ^ b1;
+ a3 = x2 >> 2;
+ b3 = y2 >> 2;
+ a4 = x3 >> 3;
+ b4 = y3 >> 3;
+ a5 = a3 ^ a4;
+ b5 = b3 ^ b4;
+ a6 = a0 ^ a3;
+ b6 = b0 ^ b3;
+ a7 = a1 ^ a4;
+ b7 = b1 ^ b4;
+ a8 = a6 ^ a7;
+ b8 = b6 ^ b7;
+
+ MUL32(b0, a0, b0, a0);
+ MUL32(b1, a1, b1, a1);
+ MUL32(b2, a2, b2, a2);
+ MUL32(b3, a3, b3, a3);
+ MUL32(b4, a4, b4, a4);
+ MUL32(b5, a5, b5, a5);
+ MUL32(b6, a6, b6, a6);
+ MUL32(b7, a7, b7, a7);
+ MUL32(b8, a8, b8, a8);
+
+ a0 &= (uint32_t)0x11111111;
+ a1 &= (uint32_t)0x11111111;
+ a2 &= (uint32_t)0x11111111;
+ a3 &= (uint32_t)0x11111111;
+ a4 &= (uint32_t)0x11111111;
+ a5 &= (uint32_t)0x11111111;
+ a6 &= (uint32_t)0x11111111;
+ a7 &= (uint32_t)0x11111111;
+ a8 &= (uint32_t)0x11111111;
+ b0 &= (uint32_t)0x11111111;
+ b1 &= (uint32_t)0x11111111;
+ b2 &= (uint32_t)0x11111111;
+ b3 &= (uint32_t)0x11111111;
+ b4 &= (uint32_t)0x11111111;
+ b5 &= (uint32_t)0x11111111;
+ b6 &= (uint32_t)0x11111111;
+ b7 &= (uint32_t)0x11111111;
+ b8 &= (uint32_t)0x11111111;
+
+ a2 ^= a0 ^ a1;
+ b2 ^= b0 ^ b1;
+ a0 ^= (a2 << 1) ^ (a1 << 2);
+ b0 ^= (b2 << 1) ^ (b1 << 2);
+ a5 ^= a3 ^ a4;
+ b5 ^= b3 ^ b4;
+ a3 ^= (a5 << 1) ^ (a4 << 2);
+ b3 ^= (b5 << 1) ^ (b4 << 2);
+ a8 ^= a6 ^ a7;
+ b8 ^= b6 ^ b7;
+ a6 ^= (a8 << 1) ^ (a7 << 2);
+ b6 ^= (b8 << 1) ^ (b7 << 2);
+ a6 ^= a0 ^ a3;
+ b6 ^= b0 ^ b3;
+ *lo = a0 ^ (a6 << 2) ^ (a3 << 4);
+ *hi = b0 ^ (b6 << 2) ^ (b3 << 4) ^ (a6 >> 30) ^ (a3 >> 28);
+}
+
+#else
+
+/*
+ * Simple multiplication in GF(2)[X], using 16 integer multiplications.
+ */
+
+static inline void
+bmul(uint32_t *hi, uint32_t *lo, uint32_t x, uint32_t y)
+{
+ uint32_t x0, x1, x2, x3;
+ uint32_t y0, y1, y2, y3;
+ uint64_t z0, z1, z2, z3;
+ uint64_t z;
+
+ x0 = x & (uint32_t)0x11111111;
+ x1 = x & (uint32_t)0x22222222;
+ x2 = x & (uint32_t)0x44444444;
+ x3 = x & (uint32_t)0x88888888;
+ y0 = y & (uint32_t)0x11111111;
+ y1 = y & (uint32_t)0x22222222;
+ y2 = y & (uint32_t)0x44444444;
+ y3 = y & (uint32_t)0x88888888;
+ z0 = MUL(x0, y0) ^ MUL(x1, y3) ^ MUL(x2, y2) ^ MUL(x3, y1);
+ z1 = MUL(x0, y1) ^ MUL(x1, y0) ^ MUL(x2, y3) ^ MUL(x3, y2);
+ z2 = MUL(x0, y2) ^ MUL(x1, y1) ^ MUL(x2, y0) ^ MUL(x3, y3);
+ z3 = MUL(x0, y3) ^ MUL(x1, y2) ^ MUL(x2, y1) ^ MUL(x3, y0);
+ z0 &= (uint64_t)0x1111111111111111;
+ z1 &= (uint64_t)0x2222222222222222;
+ z2 &= (uint64_t)0x4444444444444444;
+ z3 &= (uint64_t)0x8888888888888888;
+ z = z0 | z1 | z2 | z3;
+ *lo = (uint32_t)z;
+ *hi = (uint32_t)(z >> 32);
+}
+
+#endif
+
+/* see bearssl_hash.h */
+void
+br_ghash_ctmul(void *y, const void *h, const void *data, size_t len)
+{
+ const unsigned char *buf, *hb;
+ unsigned char *yb;
+ uint32_t yw[4];
+ uint32_t hw[4];
+
+ /*
+ * Throughout the loop we handle the y and h values as arrays
+ * of 32-bit words.
+ */
+ buf = data;
+ yb = y;
+ hb = h;
+ yw[3] = br_dec32be(yb);
+ yw[2] = br_dec32be(yb + 4);
+ yw[1] = br_dec32be(yb + 8);
+ yw[0] = br_dec32be(yb + 12);
+ hw[3] = br_dec32be(hb);
+ hw[2] = br_dec32be(hb + 4);
+ hw[1] = br_dec32be(hb + 8);
+ hw[0] = br_dec32be(hb + 12);
+ while (len > 0) {
+ const unsigned char *src;
+ unsigned char tmp[16];
+ int i;
+ uint32_t a[9], b[9], zw[8];
+ uint32_t c0, c1, c2, c3, d0, d1, d2, d3, e0, e1, e2, e3;
+
+ /*
+ * Get the next 16-byte block (using zero-padding if
+ * necessary).
+ */
+ if (len >= 16) {
+ src = buf;
+ buf += 16;
+ len -= 16;
+ } else {
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ src = tmp;
+ len = 0;
+ }
+
+ /*
+ * Decode the block. The GHASH standard mandates
+ * big-endian encoding.
+ */
+ yw[3] ^= br_dec32be(src);
+ yw[2] ^= br_dec32be(src + 4);
+ yw[1] ^= br_dec32be(src + 8);
+ yw[0] ^= br_dec32be(src + 12);
+
+ /*
+ * We multiply two 128-bit field elements. We use
+ * Karatsuba to turn that into three 64-bit
+ * multiplications, which are themselves done with a
+ * total of nine 32-bit multiplications.
+ */
+
+ /*
+ * y[0,1]*h[0,1] -> 0..2
+ * y[2,3]*h[2,3] -> 3..5
+ * (y[0,1]+y[2,3])*(h[0,1]+h[2,3]) -> 6..8
+ */
+ a[0] = yw[0];
+ b[0] = hw[0];
+ a[1] = yw[1];
+ b[1] = hw[1];
+ a[2] = a[0] ^ a[1];
+ b[2] = b[0] ^ b[1];
+
+ a[3] = yw[2];
+ b[3] = hw[2];
+ a[4] = yw[3];
+ b[4] = hw[3];
+ a[5] = a[3] ^ a[4];
+ b[5] = b[3] ^ b[4];
+
+ a[6] = a[0] ^ a[3];
+ b[6] = b[0] ^ b[3];
+ a[7] = a[1] ^ a[4];
+ b[7] = b[1] ^ b[4];
+ a[8] = a[6] ^ a[7];
+ b[8] = b[6] ^ b[7];
+
+ for (i = 0; i < 9; i ++) {
+ bmul(&b[i], &a[i], b[i], a[i]);
+ }
+
+ c0 = a[0];
+ c1 = b[0] ^ a[2] ^ a[0] ^ a[1];
+ c2 = a[1] ^ b[2] ^ b[0] ^ b[1];
+ c3 = b[1];
+ d0 = a[3];
+ d1 = b[3] ^ a[5] ^ a[3] ^ a[4];
+ d2 = a[4] ^ b[5] ^ b[3] ^ b[4];
+ d3 = b[4];
+ e0 = a[6];
+ e1 = b[6] ^ a[8] ^ a[6] ^ a[7];
+ e2 = a[7] ^ b[8] ^ b[6] ^ b[7];
+ e3 = b[7];
+
+ e0 ^= c0 ^ d0;
+ e1 ^= c1 ^ d1;
+ e2 ^= c2 ^ d2;
+ e3 ^= c3 ^ d3;
+ c2 ^= e0;
+ c3 ^= e1;
+ d0 ^= e2;
+ d1 ^= e3;
+
+ /*
+ * GHASH specification has the bits "reversed" (most
+ * significant is in fact least significant), which does
+ * not matter for a carryless multiplication, except that
+ * the 255-bit result must be shifted by 1 bit.
+ */
+ zw[0] = c0 << 1;
+ zw[1] = (c1 << 1) | (c0 >> 31);
+ zw[2] = (c2 << 1) | (c1 >> 31);
+ zw[3] = (c3 << 1) | (c2 >> 31);
+ zw[4] = (d0 << 1) | (c3 >> 31);
+ zw[5] = (d1 << 1) | (d0 >> 31);
+ zw[6] = (d2 << 1) | (d1 >> 31);
+ zw[7] = (d3 << 1) | (d2 >> 31);
+
+ /*
+ * We now do the reduction modulo the field polynomial
+ * to get back to 128 bits.
+ */
+ for (i = 0; i < 4; i ++) {
+ uint32_t lw;
+
+ lw = zw[i];
+ zw[i + 4] ^= lw ^ (lw >> 1) ^ (lw >> 2) ^ (lw >> 7);
+ zw[i + 3] ^= (lw << 31) ^ (lw << 30) ^ (lw << 25);
+ }
+ memcpy(yw, zw + 4, sizeof yw);
+ }
+
+ /*
+ * Encode back the result.
+ */
+ br_enc32be(yb, yw[3]);
+ br_enc32be(yb + 4, yw[2]);
+ br_enc32be(yb + 8, yw[1]);
+ br_enc32be(yb + 12, yw[0]);
+}
diff --git a/test/monniaux/BearSSL/src/hash/ghash_ctmul32.c b/test/monniaux/BearSSL/src/hash/ghash_ctmul32.c
new file mode 100644
index 00000000..c66af465
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/ghash_ctmul32.c
@@ -0,0 +1,251 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * This implementation uses 32-bit multiplications, and only the low
+ * 32 bits for each multiplication result. This is meant primarily for
+ * the ARM Cortex M0 and M0+, whose multiplication opcode does not yield
+ * the upper 32 bits; but it might also be useful on architectures where
+ * access to the upper 32 bits requires use of specific registers that
+ * create contention (e.g. on i386, "mul" necessarily outputs the result
+ * in edx:eax, while "imul" can use any registers but is limited to the
+ * low 32 bits).
+ *
+ * The implementation trick that is used here is bit-reversing (bit 0
+ * is swapped with bit 31, bit 1 with bit 30, and so on). In GF(2)[X],
+ * for all values x and y, we have:
+ * rev32(x) * rev32(y) = rev64(x * y)
+ * In other words, if we bit-reverse (over 32 bits) the operands, then we
+ * bit-reverse (over 64 bits) the result.
+ */
+
+/*
+ * Multiplication in GF(2)[X], truncated to its low 32 bits.
+ */
+static inline uint32_t
+bmul32(uint32_t x, uint32_t y)
+{
+ uint32_t x0, x1, x2, x3;
+ uint32_t y0, y1, y2, y3;
+ uint32_t z0, z1, z2, z3;
+
+ x0 = x & (uint32_t)0x11111111;
+ x1 = x & (uint32_t)0x22222222;
+ x2 = x & (uint32_t)0x44444444;
+ x3 = x & (uint32_t)0x88888888;
+ y0 = y & (uint32_t)0x11111111;
+ y1 = y & (uint32_t)0x22222222;
+ y2 = y & (uint32_t)0x44444444;
+ y3 = y & (uint32_t)0x88888888;
+ z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
+ z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
+ z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
+ z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
+ z0 &= (uint32_t)0x11111111;
+ z1 &= (uint32_t)0x22222222;
+ z2 &= (uint32_t)0x44444444;
+ z3 &= (uint32_t)0x88888888;
+ return z0 | z1 | z2 | z3;
+}
+
+/*
+ * Bit-reverse a 32-bit word.
+ */
+static uint32_t
+rev32(uint32_t x)
+{
+#define RMS(m, s) do { \
+ x = ((x & (uint32_t)(m)) << (s)) \
+ | ((x >> (s)) & (uint32_t)(m)); \
+ } while (0)
+
+ RMS(0x55555555, 1);
+ RMS(0x33333333, 2);
+ RMS(0x0F0F0F0F, 4);
+ RMS(0x00FF00FF, 8);
+ return (x << 16) | (x >> 16);
+
+#undef RMS
+}
+
+/* see bearssl_hash.h */
+void
+br_ghash_ctmul32(void *y, const void *h, const void *data, size_t len)
+{
+ /*
+ * This implementation is similar to br_ghash_ctmul() except
+ * that we have to do the multiplication twice, with the
+ * "normal" and "bit reversed" operands. Hence we end up with
+ * eighteen 32-bit multiplications instead of nine.
+ */
+
+ const unsigned char *buf, *hb;
+ unsigned char *yb;
+ uint32_t yw[4];
+ uint32_t hw[4], hwr[4];
+
+ buf = data;
+ yb = y;
+ hb = h;
+ yw[3] = br_dec32be(yb);
+ yw[2] = br_dec32be(yb + 4);
+ yw[1] = br_dec32be(yb + 8);
+ yw[0] = br_dec32be(yb + 12);
+ hw[3] = br_dec32be(hb);
+ hw[2] = br_dec32be(hb + 4);
+ hw[1] = br_dec32be(hb + 8);
+ hw[0] = br_dec32be(hb + 12);
+ hwr[3] = rev32(hw[3]);
+ hwr[2] = rev32(hw[2]);
+ hwr[1] = rev32(hw[1]);
+ hwr[0] = rev32(hw[0]);
+ while (len > 0) {
+ const unsigned char *src;
+ unsigned char tmp[16];
+ int i;
+ uint32_t a[18], b[18], c[18];
+ uint32_t d0, d1, d2, d3, d4, d5, d6, d7;
+ uint32_t zw[8];
+
+ if (len >= 16) {
+ src = buf;
+ buf += 16;
+ len -= 16;
+ } else {
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ src = tmp;
+ len = 0;
+ }
+ yw[3] ^= br_dec32be(src);
+ yw[2] ^= br_dec32be(src + 4);
+ yw[1] ^= br_dec32be(src + 8);
+ yw[0] ^= br_dec32be(src + 12);
+
+ /*
+ * We are using Karatsuba: the 128x128 multiplication is
+ * reduced to three 64x64 multiplications, hence nine
+ * 32x32 multiplications. With the bit-reversal trick,
+ * we have to perform 18 32x32 multiplications.
+ */
+
+ /*
+ * y[0,1]*h[0,1] -> 0,1,4
+ * y[2,3]*h[2,3] -> 2,3,5
+ * (y[0,1]+y[2,3])*(h[0,1]+h[2,3]) -> 6,7,8
+ */
+
+ a[0] = yw[0];
+ a[1] = yw[1];
+ a[2] = yw[2];
+ a[3] = yw[3];
+ a[4] = a[0] ^ a[1];
+ a[5] = a[2] ^ a[3];
+ a[6] = a[0] ^ a[2];
+ a[7] = a[1] ^ a[3];
+ a[8] = a[6] ^ a[7];
+
+ a[ 9] = rev32(yw[0]);
+ a[10] = rev32(yw[1]);
+ a[11] = rev32(yw[2]);
+ a[12] = rev32(yw[3]);
+ a[13] = a[ 9] ^ a[10];
+ a[14] = a[11] ^ a[12];
+ a[15] = a[ 9] ^ a[11];
+ a[16] = a[10] ^ a[12];
+ a[17] = a[15] ^ a[16];
+
+ b[0] = hw[0];
+ b[1] = hw[1];
+ b[2] = hw[2];
+ b[3] = hw[3];
+ b[4] = b[0] ^ b[1];
+ b[5] = b[2] ^ b[3];
+ b[6] = b[0] ^ b[2];
+ b[7] = b[1] ^ b[3];
+ b[8] = b[6] ^ b[7];
+
+ b[ 9] = hwr[0];
+ b[10] = hwr[1];
+ b[11] = hwr[2];
+ b[12] = hwr[3];
+ b[13] = b[ 9] ^ b[10];
+ b[14] = b[11] ^ b[12];
+ b[15] = b[ 9] ^ b[11];
+ b[16] = b[10] ^ b[12];
+ b[17] = b[15] ^ b[16];
+
+ for (i = 0; i < 18; i ++) {
+ c[i] = bmul32(a[i], b[i]);
+ }
+
+ c[4] ^= c[0] ^ c[1];
+ c[5] ^= c[2] ^ c[3];
+ c[8] ^= c[6] ^ c[7];
+
+ c[13] ^= c[ 9] ^ c[10];
+ c[14] ^= c[11] ^ c[12];
+ c[17] ^= c[15] ^ c[16];
+
+ /*
+ * y[0,1]*h[0,1] -> 0,9^4,1^13,10
+ * y[2,3]*h[2,3] -> 2,11^5,3^14,12
+ * (y[0,1]+y[2,3])*(h[0,1]+h[2,3]) -> 6,15^8,7^17,16
+ */
+ d0 = c[0];
+ d1 = c[4] ^ (rev32(c[9]) >> 1);
+ d2 = c[1] ^ c[0] ^ c[2] ^ c[6] ^ (rev32(c[13]) >> 1);
+ d3 = c[4] ^ c[5] ^ c[8]
+ ^ (rev32(c[10] ^ c[9] ^ c[11] ^ c[15]) >> 1);
+ d4 = c[2] ^ c[1] ^ c[3] ^ c[7]
+ ^ (rev32(c[13] ^ c[14] ^ c[17]) >> 1);
+ d5 = c[5] ^ (rev32(c[11] ^ c[10] ^ c[12] ^ c[16]) >> 1);
+ d6 = c[3] ^ (rev32(c[14]) >> 1);
+ d7 = rev32(c[12]) >> 1;
+
+ zw[0] = d0 << 1;
+ zw[1] = (d1 << 1) | (d0 >> 31);
+ zw[2] = (d2 << 1) | (d1 >> 31);
+ zw[3] = (d3 << 1) | (d2 >> 31);
+ zw[4] = (d4 << 1) | (d3 >> 31);
+ zw[5] = (d5 << 1) | (d4 >> 31);
+ zw[6] = (d6 << 1) | (d5 >> 31);
+ zw[7] = (d7 << 1) | (d6 >> 31);
+
+ for (i = 0; i < 4; i ++) {
+ uint32_t lw;
+
+ lw = zw[i];
+ zw[i + 4] ^= lw ^ (lw >> 1) ^ (lw >> 2) ^ (lw >> 7);
+ zw[i + 3] ^= (lw << 31) ^ (lw << 30) ^ (lw << 25);
+ }
+ memcpy(yw, zw + 4, sizeof yw);
+ }
+ br_enc32be(yb, yw[3]);
+ br_enc32be(yb + 4, yw[2]);
+ br_enc32be(yb + 8, yw[1]);
+ br_enc32be(yb + 12, yw[0]);
+}
diff --git a/test/monniaux/BearSSL/src/hash/ghash_ctmul64.c b/test/monniaux/BearSSL/src/hash/ghash_ctmul64.c
new file mode 100644
index 00000000..a46f16fe
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/ghash_ctmul64.c
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * This is the 64-bit variant of br_ghash_ctmul32(), with 64-bit operands
+ * and bit reversal of 64-bit words.
+ */
+
+static inline uint64_t
+bmul64(uint64_t x, uint64_t y)
+{
+ uint64_t x0, x1, x2, x3;
+ uint64_t y0, y1, y2, y3;
+ uint64_t z0, z1, z2, z3;
+
+ x0 = x & (uint64_t)0x1111111111111111;
+ x1 = x & (uint64_t)0x2222222222222222;
+ x2 = x & (uint64_t)0x4444444444444444;
+ x3 = x & (uint64_t)0x8888888888888888;
+ y0 = y & (uint64_t)0x1111111111111111;
+ y1 = y & (uint64_t)0x2222222222222222;
+ y2 = y & (uint64_t)0x4444444444444444;
+ y3 = y & (uint64_t)0x8888888888888888;
+ z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
+ z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
+ z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
+ z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
+ z0 &= (uint64_t)0x1111111111111111;
+ z1 &= (uint64_t)0x2222222222222222;
+ z2 &= (uint64_t)0x4444444444444444;
+ z3 &= (uint64_t)0x8888888888888888;
+ return z0 | z1 | z2 | z3;
+}
+
+static uint64_t
+rev64(uint64_t x)
+{
+#define RMS(m, s) do { \
+ x = ((x & (uint64_t)(m)) << (s)) \
+ | ((x >> (s)) & (uint64_t)(m)); \
+ } while (0)
+
+ RMS(0x5555555555555555, 1);
+ RMS(0x3333333333333333, 2);
+ RMS(0x0F0F0F0F0F0F0F0F, 4);
+ RMS(0x00FF00FF00FF00FF, 8);
+ RMS(0x0000FFFF0000FFFF, 16);
+ return (x << 32) | (x >> 32);
+
+#undef RMS
+}
+
+/* see bearssl_ghash.h */
+void
+br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len)
+{
+ const unsigned char *buf, *hb;
+ unsigned char *yb;
+ uint64_t y0, y1;
+ uint64_t h0, h1, h2, h0r, h1r, h2r;
+
+ buf = data;
+ yb = y;
+ hb = h;
+ y1 = br_dec64be(yb);
+ y0 = br_dec64be(yb + 8);
+ h1 = br_dec64be(hb);
+ h0 = br_dec64be(hb + 8);
+ h0r = rev64(h0);
+ h1r = rev64(h1);
+ h2 = h0 ^ h1;
+ h2r = h0r ^ h1r;
+ while (len > 0) {
+ const unsigned char *src;
+ unsigned char tmp[16];
+ uint64_t y0r, y1r, y2, y2r;
+ uint64_t z0, z1, z2, z0h, z1h, z2h;
+ uint64_t v0, v1, v2, v3;
+
+ if (len >= 16) {
+ src = buf;
+ buf += 16;
+ len -= 16;
+ } else {
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ src = tmp;
+ len = 0;
+ }
+ y1 ^= br_dec64be(src);
+ y0 ^= br_dec64be(src + 8);
+
+ y0r = rev64(y0);
+ y1r = rev64(y1);
+ y2 = y0 ^ y1;
+ y2r = y0r ^ y1r;
+
+ z0 = bmul64(y0, h0);
+ z1 = bmul64(y1, h1);
+ z2 = bmul64(y2, h2);
+ z0h = bmul64(y0r, h0r);
+ z1h = bmul64(y1r, h1r);
+ z2h = bmul64(y2r, h2r);
+ z2 ^= z0 ^ z1;
+ z2h ^= z0h ^ z1h;
+ z0h = rev64(z0h) >> 1;
+ z1h = rev64(z1h) >> 1;
+ z2h = rev64(z2h) >> 1;
+
+ v0 = z0;
+ v1 = z0h ^ z2;
+ v2 = z1 ^ z2h;
+ v3 = z1h;
+
+ v3 = (v3 << 1) | (v2 >> 63);
+ v2 = (v2 << 1) | (v1 >> 63);
+ v1 = (v1 << 1) | (v0 >> 63);
+ v0 = (v0 << 1);
+
+ v2 ^= v0 ^ (v0 >> 1) ^ (v0 >> 2) ^ (v0 >> 7);
+ v1 ^= (v0 << 63) ^ (v0 << 62) ^ (v0 << 57);
+ v3 ^= v1 ^ (v1 >> 1) ^ (v1 >> 2) ^ (v1 >> 7);
+ v2 ^= (v1 << 63) ^ (v1 << 62) ^ (v1 << 57);
+
+ y0 = v2;
+ y1 = v3;
+ }
+
+ br_enc64be(yb, y1);
+ br_enc64be(yb + 8, y0);
+}
diff --git a/test/monniaux/BearSSL/src/hash/ghash_pclmul.c b/test/monniaux/BearSSL/src/hash/ghash_pclmul.c
new file mode 100644
index 00000000..a58e7dc0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/ghash_pclmul.c
@@ -0,0 +1,389 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+/*
+ * This is the GHASH implementation that leverages the pclmulqdq opcode
+ * (from the AES-NI instructions).
+ */
+
+#if BR_AES_X86NI
+
+/*
+ * Test CPU support for PCLMULQDQ.
+ */
+static inline int
+pclmul_supported(void)
+{
+ /*
+ * Bit mask for features in ECX:
+ * 1 PCLMULQDQ support
+ */
+ return br_cpuid(0, 0, 0x00000002, 0);
+}
+
+/* see bearssl_hash.h */
+br_ghash
+br_ghash_pclmul_get(void)
+{
+ return pclmul_supported() ? &br_ghash_pclmul : 0;
+}
+
+BR_TARGETS_X86_UP
+
+/*
+ * GHASH is defined over elements of GF(2^128) with "full little-endian"
+ * representation: leftmost byte is least significant, and, within each
+ * byte, leftmost _bit_ is least significant. The natural ordering in
+ * x86 is "mixed little-endian": bytes are ordered from least to most
+ * significant, but bits within a byte are in most-to-least significant
+ * order. Going to full little-endian representation would require
+ * reversing bits within each byte, which is doable but expensive.
+ *
+ * Instead, we go to full big-endian representation, by swapping bytes
+ * around, which is done with a single _mm_shuffle_epi8() opcode (it
+ * comes with SSSE3; all CPU that offer pclmulqdq also have SSSE3). We
+ * can use a full big-endian representation because in a carryless
+ * multiplication, we have a nice bit reversal property:
+ *
+ * rev_128(x) * rev_128(y) = rev_255(x * y)
+ *
+ * So by using full big-endian, we still get the right result, except
+ * that it is right-shifted by 1 bit. The left-shift is relatively
+ * inexpensive, and it can be mutualised.
+ *
+ *
+ * Since SSE2 opcodes do not have facilities for shitfting full 128-bit
+ * values with bit precision, we have to break down values into 64-bit
+ * chunks. We number chunks from 0 to 3 in left to right order.
+ */
+
+/*
+ * Byte-swap a complete 128-bit value. This normally uses
+ * _mm_shuffle_epi8(), which gets translated to pshufb (an SSSE3 opcode).
+ * However, this crashes old Clang versions, so, for Clang before 3.8,
+ * we use an alternate (and less efficient) version.
+ */
+#if BR_CLANG && !BR_CLANG_3_8
+#define BYTESWAP_DECL
+#define BYTESWAP_PREP (void)0
+#define BYTESWAP(x) do { \
+ __m128i byteswap1, byteswap2; \
+ byteswap1 = (x); \
+ byteswap2 = _mm_srli_epi16(byteswap1, 8); \
+ byteswap1 = _mm_slli_epi16(byteswap1, 8); \
+ byteswap1 = _mm_or_si128(byteswap1, byteswap2); \
+ byteswap1 = _mm_shufflelo_epi16(byteswap1, 0x1B); \
+ byteswap1 = _mm_shufflehi_epi16(byteswap1, 0x1B); \
+ (x) = _mm_shuffle_epi32(byteswap1, 0x4E); \
+ } while (0)
+#else
+#define BYTESWAP_DECL __m128i byteswap_index;
+#define BYTESWAP_PREP do { \
+ byteswap_index = _mm_set_epi8( \
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); \
+ } while (0)
+#define BYTESWAP(x) do { \
+ (x) = _mm_shuffle_epi8((x), byteswap_index); \
+ } while (0)
+#endif
+
+/*
+ * Call pclmulqdq. Clang appears to have trouble with the intrinsic, so,
+ * for that compiler, we use inline assembly. Inline assembly is
+ * potentially a bit slower because the compiler does not understand
+ * what the opcode does, and thus cannot optimize instruction
+ * scheduling.
+ *
+ * We use a target of "sse2" only, so that Clang may still handle the
+ * '__m128i' type and allocate SSE2 registers.
+ */
+#if BR_CLANG
+BR_TARGET("sse2")
+static inline __m128i
+pclmulqdq00(__m128i x, __m128i y)
+{
+ __asm__ ("pclmulqdq $0x00, %1, %0" : "+x" (x) : "x" (y));
+ return x;
+}
+BR_TARGET("sse2")
+static inline __m128i
+pclmulqdq11(__m128i x, __m128i y)
+{
+ __asm__ ("pclmulqdq $0x11, %1, %0" : "+x" (x) : "x" (y));
+ return x;
+}
+#else
+#define pclmulqdq00(x, y) _mm_clmulepi64_si128(x, y, 0x00)
+#define pclmulqdq11(x, y) _mm_clmulepi64_si128(x, y, 0x11)
+#endif
+
+/*
+ * From a 128-bit value kw, compute kx as the XOR of the two 64-bit
+ * halves of kw (into the right half of kx; left half is unspecified).
+ */
+#define BK(kw, kx) do { \
+ kx = _mm_xor_si128(kw, _mm_shuffle_epi32(kw, 0x0E)); \
+ } while (0)
+
+/*
+ * Combine two 64-bit values (k0:k1) into a 128-bit (kw) value and
+ * the XOR of the two values (kx).
+ */
+#define PBK(k0, k1, kw, kx) do { \
+ kw = _mm_unpacklo_epi64(k1, k0); \
+ kx = _mm_xor_si128(k0, k1); \
+ } while (0)
+
+/*
+ * Left-shift by 1 bit a 256-bit value (in four 64-bit words).
+ */
+#define SL_256(x0, x1, x2, x3) do { \
+ x0 = _mm_or_si128( \
+ _mm_slli_epi64(x0, 1), \
+ _mm_srli_epi64(x1, 63)); \
+ x1 = _mm_or_si128( \
+ _mm_slli_epi64(x1, 1), \
+ _mm_srli_epi64(x2, 63)); \
+ x2 = _mm_or_si128( \
+ _mm_slli_epi64(x2, 1), \
+ _mm_srli_epi64(x3, 63)); \
+ x3 = _mm_slli_epi64(x3, 1); \
+ } while (0)
+
+/*
+ * Perform reduction in GF(2^128). The 256-bit value is in x0..x3;
+ * result is written in x0..x1.
+ */
+#define REDUCE_F128(x0, x1, x2, x3) do { \
+ x1 = _mm_xor_si128( \
+ x1, \
+ _mm_xor_si128( \
+ _mm_xor_si128( \
+ x3, \
+ _mm_srli_epi64(x3, 1)), \
+ _mm_xor_si128( \
+ _mm_srli_epi64(x3, 2), \
+ _mm_srli_epi64(x3, 7)))); \
+ x2 = _mm_xor_si128( \
+ _mm_xor_si128( \
+ x2, \
+ _mm_slli_epi64(x3, 63)), \
+ _mm_xor_si128( \
+ _mm_slli_epi64(x3, 62), \
+ _mm_slli_epi64(x3, 57))); \
+ x0 = _mm_xor_si128( \
+ x0, \
+ _mm_xor_si128( \
+ _mm_xor_si128( \
+ x2, \
+ _mm_srli_epi64(x2, 1)), \
+ _mm_xor_si128( \
+ _mm_srli_epi64(x2, 2), \
+ _mm_srli_epi64(x2, 7)))); \
+ x1 = _mm_xor_si128( \
+ _mm_xor_si128( \
+ x1, \
+ _mm_slli_epi64(x2, 63)), \
+ _mm_xor_si128( \
+ _mm_slli_epi64(x2, 62), \
+ _mm_slli_epi64(x2, 57))); \
+ } while (0)
+
+/*
+ * Square value kw into (dw,dx).
+ */
+#define SQUARE_F128(kw, dw, dx) do { \
+ __m128i z0, z1, z2, z3; \
+ z1 = pclmulqdq11(kw, kw); \
+ z3 = pclmulqdq00(kw, kw); \
+ z0 = _mm_shuffle_epi32(z1, 0x0E); \
+ z2 = _mm_shuffle_epi32(z3, 0x0E); \
+ SL_256(z0, z1, z2, z3); \
+ REDUCE_F128(z0, z1, z2, z3); \
+ PBK(z0, z1, dw, dx); \
+ } while (0)
+
+/* see bearssl_hash.h */
+BR_TARGET("ssse3,pclmul")
+void
+br_ghash_pclmul(void *y, const void *h, const void *data, size_t len)
+{
+ const unsigned char *buf1, *buf2;
+ unsigned char tmp[64];
+ size_t num4, num1;
+ __m128i yw, h1w, h1x;
+ BYTESWAP_DECL
+
+ /*
+ * We split data into two chunks. First chunk starts at buf1
+ * and contains num4 blocks of 64-byte values. Second chunk
+ * starts at buf2 and contains num1 blocks of 16-byte values.
+ * We want the first chunk to be as large as possible.
+ */
+ buf1 = data;
+ num4 = len >> 6;
+ len &= 63;
+ buf2 = buf1 + (num4 << 6);
+ num1 = (len + 15) >> 4;
+ if ((len & 15) != 0) {
+ memcpy(tmp, buf2, len);
+ memset(tmp + len, 0, (num1 << 4) - len);
+ buf2 = tmp;
+ }
+
+ /*
+ * Preparatory step for endian conversions.
+ */
+ BYTESWAP_PREP;
+
+ /*
+ * Load y and h.
+ */
+ yw = _mm_loadu_si128(y);
+ h1w = _mm_loadu_si128(h);
+ BYTESWAP(yw);
+ BYTESWAP(h1w);
+ BK(h1w, h1x);
+
+ if (num4 > 0) {
+ __m128i h2w, h2x, h3w, h3x, h4w, h4x;
+ __m128i t0, t1, t2, t3;
+
+ /*
+ * Compute h2 = h^2.
+ */
+ SQUARE_F128(h1w, h2w, h2x);
+
+ /*
+ * Compute h3 = h^3 = h*(h^2).
+ */
+ t1 = pclmulqdq11(h1w, h2w);
+ t3 = pclmulqdq00(h1w, h2w);
+ t2 = _mm_xor_si128(pclmulqdq00(h1x, h2x),
+ _mm_xor_si128(t1, t3));
+ t0 = _mm_shuffle_epi32(t1, 0x0E);
+ t1 = _mm_xor_si128(t1, _mm_shuffle_epi32(t2, 0x0E));
+ t2 = _mm_xor_si128(t2, _mm_shuffle_epi32(t3, 0x0E));
+ SL_256(t0, t1, t2, t3);
+ REDUCE_F128(t0, t1, t2, t3);
+ PBK(t0, t1, h3w, h3x);
+
+ /*
+ * Compute h4 = h^4 = (h^2)^2.
+ */
+ SQUARE_F128(h2w, h4w, h4x);
+
+ while (num4 -- > 0) {
+ __m128i aw0, aw1, aw2, aw3;
+ __m128i ax0, ax1, ax2, ax3;
+
+ aw0 = _mm_loadu_si128((void *)(buf1 + 0));
+ aw1 = _mm_loadu_si128((void *)(buf1 + 16));
+ aw2 = _mm_loadu_si128((void *)(buf1 + 32));
+ aw3 = _mm_loadu_si128((void *)(buf1 + 48));
+ BYTESWAP(aw0);
+ BYTESWAP(aw1);
+ BYTESWAP(aw2);
+ BYTESWAP(aw3);
+ buf1 += 64;
+
+ aw0 = _mm_xor_si128(aw0, yw);
+ BK(aw1, ax1);
+ BK(aw2, ax2);
+ BK(aw3, ax3);
+ BK(aw0, ax0);
+
+ t1 = _mm_xor_si128(
+ _mm_xor_si128(
+ pclmulqdq11(aw0, h4w),
+ pclmulqdq11(aw1, h3w)),
+ _mm_xor_si128(
+ pclmulqdq11(aw2, h2w),
+ pclmulqdq11(aw3, h1w)));
+ t3 = _mm_xor_si128(
+ _mm_xor_si128(
+ pclmulqdq00(aw0, h4w),
+ pclmulqdq00(aw1, h3w)),
+ _mm_xor_si128(
+ pclmulqdq00(aw2, h2w),
+ pclmulqdq00(aw3, h1w)));
+ t2 = _mm_xor_si128(
+ _mm_xor_si128(
+ pclmulqdq00(ax0, h4x),
+ pclmulqdq00(ax1, h3x)),
+ _mm_xor_si128(
+ pclmulqdq00(ax2, h2x),
+ pclmulqdq00(ax3, h1x)));
+ t2 = _mm_xor_si128(t2, _mm_xor_si128(t1, t3));
+ t0 = _mm_shuffle_epi32(t1, 0x0E);
+ t1 = _mm_xor_si128(t1, _mm_shuffle_epi32(t2, 0x0E));
+ t2 = _mm_xor_si128(t2, _mm_shuffle_epi32(t3, 0x0E));
+ SL_256(t0, t1, t2, t3);
+ REDUCE_F128(t0, t1, t2, t3);
+ yw = _mm_unpacklo_epi64(t1, t0);
+ }
+ }
+
+ while (num1 -- > 0) {
+ __m128i aw, ax;
+ __m128i t0, t1, t2, t3;
+
+ aw = _mm_loadu_si128((void *)buf2);
+ BYTESWAP(aw);
+ buf2 += 16;
+
+ aw = _mm_xor_si128(aw, yw);
+ BK(aw, ax);
+
+ t1 = pclmulqdq11(aw, h1w);
+ t3 = pclmulqdq00(aw, h1w);
+ t2 = pclmulqdq00(ax, h1x);
+ t2 = _mm_xor_si128(t2, _mm_xor_si128(t1, t3));
+ t0 = _mm_shuffle_epi32(t1, 0x0E);
+ t1 = _mm_xor_si128(t1, _mm_shuffle_epi32(t2, 0x0E));
+ t2 = _mm_xor_si128(t2, _mm_shuffle_epi32(t3, 0x0E));
+ SL_256(t0, t1, t2, t3);
+ REDUCE_F128(t0, t1, t2, t3);
+ yw = _mm_unpacklo_epi64(t1, t0);
+ }
+
+ BYTESWAP(yw);
+ _mm_storeu_si128(y, yw);
+}
+
+BR_TARGETS_X86_DOWN
+
+#else
+
+/* see bearssl_hash.h */
+br_ghash
+br_ghash_pclmul_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/hash/ghash_pwr8.c b/test/monniaux/BearSSL/src/hash/ghash_pwr8.c
new file mode 100644
index 00000000..2e7b0f4c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/ghash_pwr8.c
@@ -0,0 +1,411 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+/*
+ * This is the GHASH implementation that leverages the POWER8 opcodes.
+ */
+
+#if BR_POWER8
+
+/*
+ * Some symbolic names for registers.
+ * HB0 = 16 bytes of value 0
+ * HB1 = 16 bytes of value 1
+ * HB2 = 16 bytes of value 2
+ * HB6 = 16 bytes of value 6
+ * HB7 = 16 bytes of value 7
+ * TT0, TT1 and TT2 are temporaries
+ *
+ * BSW holds the pattern for byteswapping 32-bit words; this is set only
+ * on little-endian systems. XBSW is the same register with the +32 offset
+ * for access with the VSX opcodes.
+ */
+#define HB0 0
+#define HB1 1
+#define HB2 2
+#define HB6 3
+#define HB7 4
+#define TT0 5
+#define TT1 6
+#define TT2 7
+
+#define BSW 8
+#define XBSW 40
+
+/*
+ * Macro to initialise the constants.
+ */
+#define INIT \
+ vxor(HB0, HB0, HB0) \
+ vspltisb(HB1, 1) \
+ vspltisb(HB2, 2) \
+ vspltisb(HB6, 6) \
+ vspltisb(HB7, 7) \
+ INIT_BSW
+
+/*
+ * Fix endianness of a value after reading it or before writing it, if
+ * necessary.
+ */
+#if BR_POWER8_LE
+#define INIT_BSW lxvw4x(XBSW, 0, %[idx2be])
+#define FIX_ENDIAN(xx) vperm(xx, xx, xx, BSW)
+#else
+#define INIT_BSW
+#define FIX_ENDIAN(xx)
+#endif
+
+/*
+ * Left-shift x0:x1 by one bit to the left. This is a corrective action
+ * needed because GHASH is defined in full little-endian specification,
+ * while the opcodes use full big-endian convention, so the 255-bit product
+ * ends up one bit to the right.
+ */
+#define SL_256(x0, x1) \
+ vsldoi(TT0, HB0, x1, 1) \
+ vsl(x0, x0, HB1) \
+ vsr(TT0, TT0, HB7) \
+ vsl(x1, x1, HB1) \
+ vxor(x0, x0, TT0)
+
+/*
+ * Reduce x0:x1 in GF(2^128), result in xd (register xd may be the same as
+ * x0 or x1, or a different register). x0 and x1 are modified.
+ */
+#define REDUCE_F128(xd, x0, x1) \
+ vxor(x0, x0, x1) \
+ vsr(TT0, x1, HB1) \
+ vsr(TT1, x1, HB2) \
+ vsr(TT2, x1, HB7) \
+ vxor(x0, x0, TT0) \
+ vxor(TT1, TT1, TT2) \
+ vxor(x0, x0, TT1) \
+ vsldoi(x1, x1, HB0, 15) \
+ vsl(TT1, x1, HB6) \
+ vsl(TT2, x1, HB1) \
+ vxor(x1, TT1, TT2) \
+ vsr(TT0, x1, HB1) \
+ vsr(TT1, x1, HB2) \
+ vsr(TT2, x1, HB7) \
+ vxor(x0, x0, x1) \
+ vxor(x0, x0, TT0) \
+ vxor(TT1, TT1, TT2) \
+ vxor(xd, x0, TT1)
+
+/* see bearssl_hash.h */
+void
+br_ghash_pwr8(void *y, const void *h, const void *data, size_t len)
+{
+ const unsigned char *buf1, *buf2;
+ size_t num4, num1;
+ unsigned char tmp[64];
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ buf1 = data;
+
+ /*
+ * Assembly code requires data into two chunks; first chunk
+ * must contain a number of blocks which is a multiple of 4.
+ * Since the processing for the first chunk is faster, we want
+ * to make it as big as possible.
+ *
+ * For the remainder, there are two possibilities:
+ * -- if the remainder size is a multiple of 16, then use it
+ * in place;
+ * -- otherwise, copy it to the tmp[] array and pad it with
+ * zeros.
+ */
+ num4 = len >> 6;
+ buf2 = buf1 + (num4 << 6);
+ len &= 63;
+ num1 = (len + 15) >> 4;
+ if ((len & 15) != 0) {
+ memcpy(tmp, buf2, len);
+ memset(tmp + len, 0, (num1 << 4) - len);
+ buf2 = tmp;
+ }
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+ INIT
+
+ /*
+ * Load current h (denoted hereafter h1) in v9.
+ */
+ lxvw4x(41, 0, %[h])
+ FIX_ENDIAN(9)
+
+ /*
+ * Load current y into v28.
+ */
+ lxvw4x(60, 0, %[y])
+ FIX_ENDIAN(28)
+
+ /*
+ * Split h1 into three registers:
+ * v17 = h1_1:h1_0
+ * v18 = 0:h1_0
+ * v19 = h1_1:0
+ */
+ xxpermdi(49, 41, 41, 2)
+ vsldoi(18, HB0, 9, 8)
+ vsldoi(19, 9, HB0, 8)
+
+ /*
+ * If num4 is 0, skip directly to the second chunk.
+ */
+ cmpldi(%[num4], 0)
+ beq(chunk1)
+
+ /*
+ * Compute h2 = h*h in v10.
+ */
+ vpmsumd(10, 18, 18)
+ vpmsumd(11, 19, 19)
+ SL_256(10, 11)
+ REDUCE_F128(10, 10, 11)
+
+ /*
+ * Compute h3 = h*h*h in v11.
+ * We first split h2 into:
+ * v10 = h2_0:h2_1
+ * v11 = 0:h2_0
+ * v12 = h2_1:0
+ * Then we do the product with h1, and reduce into v11.
+ */
+ vsldoi(11, HB0, 10, 8)
+ vsldoi(12, 10, HB0, 8)
+ vpmsumd(13, 10, 17)
+ vpmsumd(11, 11, 18)
+ vpmsumd(12, 12, 19)
+ vsldoi(14, HB0, 13, 8)
+ vsldoi(15, 13, HB0, 8)
+ vxor(11, 11, 14)
+ vxor(12, 12, 15)
+ SL_256(11, 12)
+ REDUCE_F128(11, 11, 12)
+
+ /*
+ * Compute h4 = h*h*h*h in v12. This is done by squaring h2.
+ */
+ vsldoi(12, HB0, 10, 8)
+ vsldoi(13, 10, HB0, 8)
+ vpmsumd(12, 12, 12)
+ vpmsumd(13, 13, 13)
+ SL_256(12, 13)
+ REDUCE_F128(12, 12, 13)
+
+ /*
+ * Repack h1, h2, h3 and h4:
+ * v13 = h4_0:h3_0
+ * v14 = h4_1:h3_1
+ * v15 = h2_0:h1_0
+ * v16 = h2_1:h1_1
+ */
+ xxpermdi(45, 44, 43, 0)
+ xxpermdi(46, 44, 43, 3)
+ xxpermdi(47, 42, 41, 0)
+ xxpermdi(48, 42, 41, 3)
+
+ /*
+ * Loop for each group of four blocks.
+ */
+ mtctr(%[num4])
+ label(loop4)
+ /*
+ * Read the four next blocks.
+ * v20 = y + a0 = b0
+ * v21 = a1 = b1
+ * v22 = a2 = b2
+ * v23 = a3 = b3
+ */
+ lxvw4x(52, %[cc0], %[buf1])
+ lxvw4x(53, %[cc1], %[buf1])
+ lxvw4x(54, %[cc2], %[buf1])
+ lxvw4x(55, %[cc3], %[buf1])
+ FIX_ENDIAN(20)
+ FIX_ENDIAN(21)
+ FIX_ENDIAN(22)
+ FIX_ENDIAN(23)
+ addi(%[buf1], %[buf1], 64)
+ vxor(20, 20, 28)
+
+ /*
+ * Repack the blocks into v9, v10, v11 and v12.
+ * v9 = b0_0:b1_0
+ * v10 = b0_1:b1_1
+ * v11 = b2_0:b3_0
+ * v12 = b2_1:b3_1
+ */
+ xxpermdi(41, 52, 53, 0)
+ xxpermdi(42, 52, 53, 3)
+ xxpermdi(43, 54, 55, 0)
+ xxpermdi(44, 54, 55, 3)
+
+ /*
+ * Compute the products.
+ * v20 = b0_0*h4_0 + b1_0*h3_0
+ * v21 = b0_1*h4_0 + b1_1*h3_0
+ * v22 = b0_0*h4_1 + b1_0*h3_1
+ * v23 = b0_1*h4_1 + b1_1*h3_1
+ * v24 = b2_0*h2_0 + b3_0*h1_0
+ * v25 = b2_1*h2_0 + b3_1*h1_0
+ * v26 = b2_0*h2_1 + b3_0*h1_1
+ * v27 = b2_1*h2_1 + b3_1*h1_1
+ */
+ vpmsumd(20, 13, 9)
+ vpmsumd(21, 13, 10)
+ vpmsumd(22, 14, 9)
+ vpmsumd(23, 14, 10)
+ vpmsumd(24, 15, 11)
+ vpmsumd(25, 15, 12)
+ vpmsumd(26, 16, 11)
+ vpmsumd(27, 16, 12)
+
+ /*
+ * Sum products into a single 256-bit result in v11:v12.
+ */
+ vxor(11, 20, 24)
+ vxor(12, 23, 27)
+ vxor( 9, 21, 22)
+ vxor(10, 25, 26)
+ vxor(20, 9, 10)
+ vsldoi( 9, HB0, 20, 8)
+ vsldoi(10, 20, HB0, 8)
+ vxor(11, 11, 9)
+ vxor(12, 12, 10)
+
+ /*
+ * Fix and reduce in GF(2^128); this is the new y (in v28).
+ */
+ SL_256(11, 12)
+ REDUCE_F128(28, 11, 12)
+
+ /*
+ * Loop for next group of four blocks.
+ */
+ bdnz(loop4)
+
+ /*
+ * Process second chunk, one block at a time.
+ */
+ label(chunk1)
+ cmpldi(%[num1], 0)
+ beq(done)
+
+ mtctr(%[num1])
+ label(loop1)
+ /*
+ * Load next data block and XOR it into y.
+ */
+ lxvw4x(41, 0, %[buf2])
+#if BR_POWER8_LE
+ FIX_ENDIAN(9)
+#endif
+ addi(%[buf2], %[buf2], 16)
+ vxor(9, 28, 9)
+
+ /*
+ * Split y into doublewords:
+ * v9 = y_0:y_1
+ * v10 = 0:y_0
+ * v11 = y_1:0
+ */
+ vsldoi(10, HB0, 9, 8)
+ vsldoi(11, 9, HB0, 8)
+
+ /*
+ * Compute products with h:
+ * v12 = y_0 * h_0
+ * v13 = y_1 * h_1
+ * v14 = y_1 * h_0 + y_0 * h_1
+ */
+ vpmsumd(14, 9, 17)
+ vpmsumd(12, 10, 18)
+ vpmsumd(13, 11, 19)
+
+ /*
+ * Propagate v14 into v12:v13 to finalise product.
+ */
+ vsldoi(10, HB0, 14, 8)
+ vsldoi(11, 14, HB0, 8)
+ vxor(12, 12, 10)
+ vxor(13, 13, 11)
+
+ /*
+ * Fix result and reduce into v28 (next value for y).
+ */
+ SL_256(12, 13)
+ REDUCE_F128(28, 12, 13)
+ bdnz(loop1)
+
+ label(done)
+ /*
+ * Write back the new y.
+ */
+ FIX_ENDIAN(28)
+ stxvw4x(60, 0, %[y])
+
+: [buf1] "+b" (buf1), [buf2] "+b" (buf2)
+: [y] "b" (y), [h] "b" (h), [num4] "b" (num4), [num1] "b" (num1),
+ [cc0] "b" (cc0), [cc1] "b" (cc1), [cc2] "b" (cc2), [cc3] "b" (cc3)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+/* see bearssl_hash.h */
+br_ghash
+br_ghash_pwr8_get(void)
+{
+ return &br_ghash_pwr8;
+}
+
+#else
+
+/* see bearssl_hash.h */
+br_ghash
+br_ghash_pwr8_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/hash/md5.c b/test/monniaux/BearSSL/src/hash/md5.c
new file mode 100644
index 00000000..0df7abe0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/md5.c
@@ -0,0 +1,208 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define F(B, C, D) ((((C) ^ (D)) & (B)) ^ (D))
+#define G(B, C, D) ((((C) ^ (B)) & (D)) ^ (C))
+#define H(B, C, D) ((B) ^ (C) ^ (D))
+#define I(B, C, D) ((C) ^ ((B) | ~(D)))
+
+#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
+
+/* see inner.h */
+const uint32_t br_md5_IV[4] = {
+ 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476
+};
+
+static const uint32_t K[64] = {
+ 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
+ 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
+ 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
+ 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
+
+ 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
+ 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
+ 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
+ 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
+
+ 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
+ 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
+ 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
+ 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
+
+ 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
+ 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
+ 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
+ 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
+};
+
+static const unsigned char MP[48] = {
+ 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
+ 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
+ 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9
+};
+
+/* see inner.h */
+void
+br_md5_round(const unsigned char *buf, uint32_t *val)
+{
+ uint32_t m[16];
+ uint32_t a, b, c, d;
+ int i;
+
+ a = val[0];
+ b = val[1];
+ c = val[2];
+ d = val[3];
+ /* obsolete
+ for (i = 0; i < 16; i ++) {
+ m[i] = br_dec32le(buf + (i << 2));
+ }
+ */
+ br_range_dec32le(m, 16, buf);
+
+ for (i = 0; i < 16; i += 4) {
+ a = b + ROTL(a + F(b, c, d) + m[i + 0] + K[i + 0], 7);
+ d = a + ROTL(d + F(a, b, c) + m[i + 1] + K[i + 1], 12);
+ c = d + ROTL(c + F(d, a, b) + m[i + 2] + K[i + 2], 17);
+ b = c + ROTL(b + F(c, d, a) + m[i + 3] + K[i + 3], 22);
+ }
+ for (i = 16; i < 32; i += 4) {
+ a = b + ROTL(a + G(b, c, d) + m[MP[i - 16]] + K[i + 0], 5);
+ d = a + ROTL(d + G(a, b, c) + m[MP[i - 15]] + K[i + 1], 9);
+ c = d + ROTL(c + G(d, a, b) + m[MP[i - 14]] + K[i + 2], 14);
+ b = c + ROTL(b + G(c, d, a) + m[MP[i - 13]] + K[i + 3], 20);
+ }
+ for (i = 32; i < 48; i += 4) {
+ a = b + ROTL(a + H(b, c, d) + m[MP[i - 16]] + K[i + 0], 4);
+ d = a + ROTL(d + H(a, b, c) + m[MP[i - 15]] + K[i + 1], 11);
+ c = d + ROTL(c + H(d, a, b) + m[MP[i - 14]] + K[i + 2], 16);
+ b = c + ROTL(b + H(c, d, a) + m[MP[i - 13]] + K[i + 3], 23);
+ }
+ for (i = 48; i < 64; i += 4) {
+ a = b + ROTL(a + I(b, c, d) + m[MP[i - 16]] + K[i + 0], 6);
+ d = a + ROTL(d + I(a, b, c) + m[MP[i - 15]] + K[i + 1], 10);
+ c = d + ROTL(c + I(d, a, b) + m[MP[i - 14]] + K[i + 2], 15);
+ b = c + ROTL(b + I(c, d, a) + m[MP[i - 13]] + K[i + 3], 21);
+ }
+
+ val[0] += a;
+ val[1] += b;
+ val[2] += c;
+ val[3] += d;
+}
+
+/* see bearssl.h */
+void
+br_md5_init(br_md5_context *cc)
+{
+ cc->vtable = &br_md5_vtable;
+ memcpy(cc->val, br_md5_IV, sizeof cc->val);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_md5_update(br_md5_context *cc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)cc->count & 63;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 64 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(cc->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ cc->count += (uint64_t)clen;
+ if (ptr == 64) {
+ br_md5_round(cc->buf, cc->val);
+ ptr = 0;
+ }
+ }
+}
+
+/* see bearssl.h */
+void
+br_md5_out(const br_md5_context *cc, void *dst)
+{
+ unsigned char buf[64];
+ uint32_t val[4];
+ size_t ptr;
+
+ ptr = (size_t)cc->count & 63;
+ memcpy(buf, cc->buf, ptr);
+ memcpy(val, cc->val, sizeof val);
+ buf[ptr ++] = 0x80;
+ if (ptr > 56) {
+ memset(buf + ptr, 0, 64 - ptr);
+ br_md5_round(buf, val);
+ memset(buf, 0, 56);
+ } else {
+ memset(buf + ptr, 0, 56 - ptr);
+ }
+ br_enc64le(buf + 56, cc->count << 3);
+ br_md5_round(buf, val);
+ br_range_enc32le(dst, val, 4);
+}
+
+/* see bearssl.h */
+uint64_t
+br_md5_state(const br_md5_context *cc, void *dst)
+{
+ br_range_enc32le(dst, cc->val, 4);
+ return cc->count;
+}
+
+/* see bearssl.h */
+void
+br_md5_set_state(br_md5_context *cc, const void *stb, uint64_t count)
+{
+ br_range_dec32le(cc->val, 4, stb);
+ cc->count = count;
+}
+
+/* see bearssl.h */
+const br_hash_class br_md5_vtable = {
+ sizeof(br_md5_context),
+ BR_HASHDESC_ID(br_md5_ID)
+ | BR_HASHDESC_OUT(16)
+ | BR_HASHDESC_STATE(16)
+ | BR_HASHDESC_LBLEN(6)
+ | BR_HASHDESC_MD_PADDING,
+ (void (*)(const br_hash_class **))&br_md5_init,
+ (void (*)(const br_hash_class **, const void *, size_t))&br_md5_update,
+ (void (*)(const br_hash_class *const *, void *))&br_md5_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_md5_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_md5_set_state
+};
diff --git a/test/monniaux/BearSSL/src/hash/md5sha1.c b/test/monniaux/BearSSL/src/hash/md5sha1.c
new file mode 100644
index 00000000..f701aeed
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/md5sha1.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl.h */
+void
+br_md5sha1_init(br_md5sha1_context *cc)
+{
+ cc->vtable = &br_md5sha1_vtable;
+ memcpy(cc->val_md5, br_md5_IV, sizeof cc->val_md5);
+ memcpy(cc->val_sha1, br_sha1_IV, sizeof cc->val_sha1);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_md5sha1_update(br_md5sha1_context *cc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)cc->count & 63;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 64 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(cc->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ cc->count += (uint64_t)clen;
+ if (ptr == 64) {
+ br_md5_round(cc->buf, cc->val_md5);
+ br_sha1_round(cc->buf, cc->val_sha1);
+ ptr = 0;
+ }
+ }
+}
+
+/* see bearssl.h */
+void
+br_md5sha1_out(const br_md5sha1_context *cc, void *dst)
+{
+ unsigned char buf[64];
+ uint32_t val_md5[4];
+ uint32_t val_sha1[5];
+ size_t ptr;
+ unsigned char *out;
+ uint64_t count;
+
+ count = cc->count;
+ ptr = (size_t)count & 63;
+ memcpy(buf, cc->buf, ptr);
+ memcpy(val_md5, cc->val_md5, sizeof val_md5);
+ memcpy(val_sha1, cc->val_sha1, sizeof val_sha1);
+ buf[ptr ++] = 0x80;
+ if (ptr > 56) {
+ memset(buf + ptr, 0, 64 - ptr);
+ br_md5_round(buf, val_md5);
+ br_sha1_round(buf, val_sha1);
+ memset(buf, 0, 56);
+ } else {
+ memset(buf + ptr, 0, 56 - ptr);
+ }
+ count <<= 3;
+ br_enc64le(buf + 56, count);
+ br_md5_round(buf, val_md5);
+ br_enc64be(buf + 56, count);
+ br_sha1_round(buf, val_sha1);
+ out = dst;
+ br_range_enc32le(out, val_md5, 4);
+ br_range_enc32be(out + 16, val_sha1, 5);
+}
+
+/* see bearssl.h */
+uint64_t
+br_md5sha1_state(const br_md5sha1_context *cc, void *dst)
+{
+ unsigned char *out;
+
+ out = dst;
+ br_range_enc32le(out, cc->val_md5, 4);
+ br_range_enc32be(out + 16, cc->val_sha1, 5);
+ return cc->count;
+}
+
+/* see bearssl.h */
+void
+br_md5sha1_set_state(br_md5sha1_context *cc, const void *stb, uint64_t count)
+{
+ const unsigned char *buf;
+
+ buf = stb;
+ br_range_dec32le(cc->val_md5, 4, buf);
+ br_range_dec32be(cc->val_sha1, 5, buf + 16);
+ cc->count = count;
+}
+
+/* see bearssl.h */
+const br_hash_class br_md5sha1_vtable = {
+ sizeof(br_md5sha1_context),
+ BR_HASHDESC_ID(br_md5sha1_ID)
+ | BR_HASHDESC_OUT(36)
+ | BR_HASHDESC_STATE(36)
+ | BR_HASHDESC_LBLEN(6),
+ (void (*)(const br_hash_class **))&br_md5sha1_init,
+ (void (*)(const br_hash_class **, const void *, size_t))
+ &br_md5sha1_update,
+ (void (*)(const br_hash_class *const *, void *))
+ &br_md5sha1_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))
+ &br_md5sha1_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_md5sha1_set_state
+};
diff --git a/test/monniaux/BearSSL/src/hash/mgf1.c b/test/monniaux/BearSSL/src/hash/mgf1.c
new file mode 100644
index 00000000..7a235887
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/mgf1.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_mgf1_xor(void *data, size_t len,
+ const br_hash_class *dig, const void *seed, size_t seed_len)
+{
+ unsigned char *buf;
+ size_t u, hlen;
+ uint32_t c;
+
+ buf = data;
+ hlen = br_digest_size(dig);
+ for (u = 0, c = 0; u < len; u += hlen, c ++) {
+ br_hash_compat_context hc;
+ unsigned char tmp[64];
+ size_t v;
+
+ hc.vtable = dig;
+ dig->init(&hc.vtable);
+ dig->update(&hc.vtable, seed, seed_len);
+ br_enc32be(tmp, c);
+ dig->update(&hc.vtable, tmp, 4);
+ dig->out(&hc.vtable, tmp);
+ for (v = 0; v < hlen; v ++) {
+ if ((u + v) >= len) {
+ break;
+ }
+ buf[u + v] ^= tmp[v];
+ }
+ }
+}
diff --git a/test/monniaux/BearSSL/src/hash/multihash.c b/test/monniaux/BearSSL/src/hash/multihash.c
new file mode 100644
index 00000000..b6df2e0e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/multihash.c
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * An aggregate context that is large enough for all supported hash
+ * functions.
+ */
+typedef union {
+ const br_hash_class *vtable;
+ br_md5_context md5;
+ br_sha1_context sha1;
+ br_sha224_context sha224;
+ br_sha256_context sha256;
+ br_sha384_context sha384;
+ br_sha512_context sha512;
+} gen_hash_context;
+
+/*
+ * Get the offset to the state for a specific hash function within the
+ * context structure. This shall be called only for the supported hash
+ * functions,
+ */
+static size_t
+get_state_offset(int id)
+{
+ if (id >= 5) {
+ /*
+ * SHA-384 has id 5, and SHA-512 has id 6. Both use
+ * eight 64-bit words for their state.
+ */
+ return offsetof(br_multihash_context, val_64)
+ + ((size_t)(id - 5) * (8 * sizeof(uint64_t)));
+ } else {
+ /*
+ * MD5 has id 1, SHA-1 has id 2, SHA-224 has id 3 and
+ * SHA-256 has id 4. They use 32-bit words for their
+ * states (4 words for MD5, 5 for SHA-1, 8 for SHA-224
+ * and 8 for SHA-256).
+ */
+ unsigned x;
+
+ x = id - 1;
+ x = ((x + (x & (x >> 1))) << 2) + (x >> 1);
+ return offsetof(br_multihash_context, val_32)
+ + x * sizeof(uint32_t);
+ }
+}
+
+/* see bearssl_hash.h */
+void
+br_multihash_zero(br_multihash_context *ctx)
+{
+ /*
+ * This is not standard, but yields very short and efficient code,
+ * and it works "everywhere".
+ */
+ memset(ctx, 0, sizeof *ctx);
+}
+
+/* see bearssl_hash.h */
+void
+br_multihash_init(br_multihash_context *ctx)
+{
+ int i;
+
+ ctx->count = 0;
+ for (i = 1; i <= 6; i ++) {
+ const br_hash_class *hc;
+
+ hc = ctx->impl[i - 1];
+ if (hc != NULL) {
+ gen_hash_context g;
+
+ hc->init(&g.vtable);
+ hc->state(&g.vtable,
+ (unsigned char *)ctx + get_state_offset(i));
+ }
+ }
+}
+
+/* see bearssl_hash.h */
+void
+br_multihash_update(br_multihash_context *ctx, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)ctx->count & 127;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 128 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(ctx->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ ctx->count += (uint64_t)clen;
+ if (ptr == 128) {
+ int i;
+
+ for (i = 1; i <= 6; i ++) {
+ const br_hash_class *hc;
+
+ hc = ctx->impl[i - 1];
+ if (hc != NULL) {
+ gen_hash_context g;
+ unsigned char *state;
+
+ state = (unsigned char *)ctx
+ + get_state_offset(i);
+ hc->set_state(&g.vtable,
+ state, ctx->count - 128);
+ hc->update(&g.vtable, ctx->buf, 128);
+ hc->state(&g.vtable, state);
+ }
+ }
+ ptr = 0;
+ }
+ }
+}
+
+/* see bearssl_hash.h */
+size_t
+br_multihash_out(const br_multihash_context *ctx, int id, void *dst)
+{
+ const br_hash_class *hc;
+ gen_hash_context g;
+ const unsigned char *state;
+
+ hc = ctx->impl[id - 1];
+ if (hc == NULL) {
+ return 0;
+ }
+ state = (const unsigned char *)ctx + get_state_offset(id);
+ hc->set_state(&g.vtable, state, ctx->count & ~(uint64_t)127);
+ hc->update(&g.vtable, ctx->buf, ctx->count & (uint64_t)127);
+ hc->out(&g.vtable, dst);
+ return (hc->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+}
diff --git a/test/monniaux/BearSSL/src/hash/sha1.c b/test/monniaux/BearSSL/src/hash/sha1.c
new file mode 100644
index 00000000..4f65d846
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/sha1.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define F(B, C, D) ((((C) ^ (D)) & (B)) ^ (D))
+#define G(B, C, D) ((B) ^ (C) ^ (D))
+#define H(B, C, D) (((D) & (C)) | (((D) | (C)) & (B)))
+#define I(B, C, D) G(B, C, D)
+
+#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
+
+#define K1 ((uint32_t)0x5A827999)
+#define K2 ((uint32_t)0x6ED9EBA1)
+#define K3 ((uint32_t)0x8F1BBCDC)
+#define K4 ((uint32_t)0xCA62C1D6)
+
+/* see inner.h */
+const uint32_t br_sha1_IV[5] = {
+ 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
+};
+
+/* see inner.h */
+void
+br_sha1_round(const unsigned char *buf, uint32_t *val)
+{
+ uint32_t m[80];
+ uint32_t a, b, c, d, e;
+ int i;
+
+ a = val[0];
+ b = val[1];
+ c = val[2];
+ d = val[3];
+ e = val[4];
+ br_range_dec32be(m, 16, buf);
+ for (i = 16; i < 80; i ++) {
+ uint32_t x = m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16];
+ m[i] = ROTL(x, 1);
+ }
+
+ for (i = 0; i < 20; i += 5) {
+ e += ROTL(a, 5) + F(b, c, d) + K1 + m[i + 0]; b = ROTL(b, 30);
+ d += ROTL(e, 5) + F(a, b, c) + K1 + m[i + 1]; a = ROTL(a, 30);
+ c += ROTL(d, 5) + F(e, a, b) + K1 + m[i + 2]; e = ROTL(e, 30);
+ b += ROTL(c, 5) + F(d, e, a) + K1 + m[i + 3]; d = ROTL(d, 30);
+ a += ROTL(b, 5) + F(c, d, e) + K1 + m[i + 4]; c = ROTL(c, 30);
+ }
+ for (i = 20; i < 40; i += 5) {
+ e += ROTL(a, 5) + G(b, c, d) + K2 + m[i + 0]; b = ROTL(b, 30);
+ d += ROTL(e, 5) + G(a, b, c) + K2 + m[i + 1]; a = ROTL(a, 30);
+ c += ROTL(d, 5) + G(e, a, b) + K2 + m[i + 2]; e = ROTL(e, 30);
+ b += ROTL(c, 5) + G(d, e, a) + K2 + m[i + 3]; d = ROTL(d, 30);
+ a += ROTL(b, 5) + G(c, d, e) + K2 + m[i + 4]; c = ROTL(c, 30);
+ }
+ for (i = 40; i < 60; i += 5) {
+ e += ROTL(a, 5) + H(b, c, d) + K3 + m[i + 0]; b = ROTL(b, 30);
+ d += ROTL(e, 5) + H(a, b, c) + K3 + m[i + 1]; a = ROTL(a, 30);
+ c += ROTL(d, 5) + H(e, a, b) + K3 + m[i + 2]; e = ROTL(e, 30);
+ b += ROTL(c, 5) + H(d, e, a) + K3 + m[i + 3]; d = ROTL(d, 30);
+ a += ROTL(b, 5) + H(c, d, e) + K3 + m[i + 4]; c = ROTL(c, 30);
+ }
+ for (i = 60; i < 80; i += 5) {
+ e += ROTL(a, 5) + I(b, c, d) + K4 + m[i + 0]; b = ROTL(b, 30);
+ d += ROTL(e, 5) + I(a, b, c) + K4 + m[i + 1]; a = ROTL(a, 30);
+ c += ROTL(d, 5) + I(e, a, b) + K4 + m[i + 2]; e = ROTL(e, 30);
+ b += ROTL(c, 5) + I(d, e, a) + K4 + m[i + 3]; d = ROTL(d, 30);
+ a += ROTL(b, 5) + I(c, d, e) + K4 + m[i + 4]; c = ROTL(c, 30);
+ }
+
+ val[0] += a;
+ val[1] += b;
+ val[2] += c;
+ val[3] += d;
+ val[4] += e;
+}
+
+/* see bearssl.h */
+void
+br_sha1_init(br_sha1_context *cc)
+{
+ cc->vtable = &br_sha1_vtable;
+ memcpy(cc->val, br_sha1_IV, sizeof cc->val);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_sha1_update(br_sha1_context *cc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)cc->count & 63;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 64 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(cc->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ cc->count += (uint64_t)clen;
+ if (ptr == 64) {
+ br_sha1_round(cc->buf, cc->val);
+ ptr = 0;
+ }
+ }
+}
+
+/* see bearssl.h */
+void
+br_sha1_out(const br_sha1_context *cc, void *dst)
+{
+ unsigned char buf[64];
+ uint32_t val[5];
+ size_t ptr;
+
+ ptr = (size_t)cc->count & 63;
+ memcpy(buf, cc->buf, ptr);
+ memcpy(val, cc->val, sizeof val);
+ buf[ptr ++] = 0x80;
+ if (ptr > 56) {
+ memset(buf + ptr, 0, 64 - ptr);
+ br_sha1_round(buf, val);
+ memset(buf, 0, 56);
+ } else {
+ memset(buf + ptr, 0, 56 - ptr);
+ }
+ br_enc64be(buf + 56, cc->count << 3);
+ br_sha1_round(buf, val);
+ br_range_enc32be(dst, val, 5);
+}
+
+/* see bearssl.h */
+uint64_t
+br_sha1_state(const br_sha1_context *cc, void *dst)
+{
+ br_range_enc32be(dst, cc->val, 5);
+ return cc->count;
+}
+
+/* see bearssl.h */
+void
+br_sha1_set_state(br_sha1_context *cc, const void *stb, uint64_t count)
+{
+ br_range_dec32be(cc->val, 5, stb);
+ cc->count = count;
+}
+
+/* see bearssl.h */
+const br_hash_class br_sha1_vtable = {
+ sizeof(br_sha1_context),
+ BR_HASHDESC_ID(br_sha1_ID)
+ | BR_HASHDESC_OUT(20)
+ | BR_HASHDESC_STATE(20)
+ | BR_HASHDESC_LBLEN(6)
+ | BR_HASHDESC_MD_PADDING
+ | BR_HASHDESC_MD_PADDING_BE,
+ (void (*)(const br_hash_class **))&br_sha1_init,
+ (void (*)(const br_hash_class **, const void *, size_t))&br_sha1_update,
+ (void (*)(const br_hash_class *const *, void *))&br_sha1_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_sha1_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_sha1_set_state
+};
diff --git a/test/monniaux/BearSSL/src/hash/sha2big.c b/test/monniaux/BearSSL/src/hash/sha2big.c
new file mode 100644
index 00000000..5be92ed5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/sha2big.c
@@ -0,0 +1,285 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define CH(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z))
+#define MAJ(X, Y, Z) (((Y) & (Z)) | (((Y) | (Z)) & (X)))
+
+#define ROTR(x, n) (((uint64_t)(x) << (64 - (n))) | ((uint64_t)(x) >> (n)))
+
+#define BSG5_0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
+#define BSG5_1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
+#define SSG5_0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ (uint64_t)((x) >> 7))
+#define SSG5_1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ (uint64_t)((x) >> 6))
+
+static const uint64_t IV384[8] = {
+ 0xCBBB9D5DC1059ED8, 0x629A292A367CD507,
+ 0x9159015A3070DD17, 0x152FECD8F70E5939,
+ 0x67332667FFC00B31, 0x8EB44A8768581511,
+ 0xDB0C2E0D64F98FA7, 0x47B5481DBEFA4FA4
+};
+
+static const uint64_t IV512[8] = {
+ 0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
+ 0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
+ 0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
+ 0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179
+};
+
+static const uint64_t K[80] = {
+ 0x428A2F98D728AE22, 0x7137449123EF65CD,
+ 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
+ 0x3956C25BF348B538, 0x59F111F1B605D019,
+ 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
+ 0xD807AA98A3030242, 0x12835B0145706FBE,
+ 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
+ 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1,
+ 0x9BDC06A725C71235, 0xC19BF174CF692694,
+ 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3,
+ 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
+ 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483,
+ 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
+ 0x983E5152EE66DFAB, 0xA831C66D2DB43210,
+ 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
+ 0xC6E00BF33DA88FC2, 0xD5A79147930AA725,
+ 0x06CA6351E003826F, 0x142929670A0E6E70,
+ 0x27B70A8546D22FFC, 0x2E1B21385C26C926,
+ 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
+ 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8,
+ 0x81C2C92E47EDAEE6, 0x92722C851482353B,
+ 0xA2BFE8A14CF10364, 0xA81A664BBC423001,
+ 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
+ 0xD192E819D6EF5218, 0xD69906245565A910,
+ 0xF40E35855771202A, 0x106AA07032BBD1B8,
+ 0x19A4C116B8D2D0C8, 0x1E376C085141AB53,
+ 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
+ 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB,
+ 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
+ 0x748F82EE5DEFB2FC, 0x78A5636F43172F60,
+ 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
+ 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9,
+ 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
+ 0xCA273ECEEA26619C, 0xD186B8C721C0C207,
+ 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
+ 0x06F067AA72176FBA, 0x0A637DC5A2C898A6,
+ 0x113F9804BEF90DAE, 0x1B710B35131C471B,
+ 0x28DB77F523047D84, 0x32CAAB7B40C72493,
+ 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
+ 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A,
+ 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
+};
+
+static void
+sha2big_round(const unsigned char *buf, uint64_t *val)
+{
+
+#define SHA2BIG_STEP(A, B, C, D, E, F, G, H, j) do { \
+ uint64_t T1, T2; \
+ T1 = H + BSG5_1(E) + CH(E, F, G) + K[j] + w[j]; \
+ T2 = BSG5_0(A) + MAJ(A, B, C); \
+ D += T1; \
+ H = T1 + T2; \
+ } while (0)
+
+ int i;
+ uint64_t a, b, c, d, e, f, g, h;
+ uint64_t w[80];
+
+ br_range_dec64be(w, 16, buf);
+ for (i = 16; i < 80; i ++) {
+ w[i] = SSG5_1(w[i - 2]) + w[i - 7]
+ + SSG5_0(w[i - 15]) + w[i - 16];
+ }
+ a = val[0];
+ b = val[1];
+ c = val[2];
+ d = val[3];
+ e = val[4];
+ f = val[5];
+ g = val[6];
+ h = val[7];
+ for (i = 0; i < 80; i += 8) {
+ SHA2BIG_STEP(a, b, c, d, e, f, g, h, i + 0);
+ SHA2BIG_STEP(h, a, b, c, d, e, f, g, i + 1);
+ SHA2BIG_STEP(g, h, a, b, c, d, e, f, i + 2);
+ SHA2BIG_STEP(f, g, h, a, b, c, d, e, i + 3);
+ SHA2BIG_STEP(e, f, g, h, a, b, c, d, i + 4);
+ SHA2BIG_STEP(d, e, f, g, h, a, b, c, i + 5);
+ SHA2BIG_STEP(c, d, e, f, g, h, a, b, i + 6);
+ SHA2BIG_STEP(b, c, d, e, f, g, h, a, i + 7);
+ }
+ val[0] += a;
+ val[1] += b;
+ val[2] += c;
+ val[3] += d;
+ val[4] += e;
+ val[5] += f;
+ val[6] += g;
+ val[7] += h;
+}
+
+static void
+sha2big_update(br_sha384_context *cc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)cc->count & 127;
+ cc->count += (uint64_t)len;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 128 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(cc->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ if (ptr == 128) {
+ sha2big_round(cc->buf, cc->val);
+ ptr = 0;
+ }
+ }
+}
+
+static void
+sha2big_out(const br_sha384_context *cc, void *dst, int num)
+{
+ unsigned char buf[128];
+ uint64_t val[8];
+ size_t ptr;
+
+ ptr = (size_t)cc->count & 127;
+ memcpy(buf, cc->buf, ptr);
+ memcpy(val, cc->val, sizeof val);
+ buf[ptr ++] = 0x80;
+ if (ptr > 112) {
+ memset(buf + ptr, 0, 128 - ptr);
+ sha2big_round(buf, val);
+ memset(buf, 0, 112);
+ } else {
+ memset(buf + ptr, 0, 112 - ptr);
+ }
+ br_enc64be(buf + 112, cc->count >> 61);
+ br_enc64be(buf + 120, cc->count << 3);
+ sha2big_round(buf, val);
+ br_range_enc64be(dst, val, num);
+}
+
+/* see bearssl.h */
+void
+br_sha384_init(br_sha384_context *cc)
+{
+ cc->vtable = &br_sha384_vtable;
+ memcpy(cc->val, IV384, sizeof IV384);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_sha384_update(br_sha384_context *cc, const void *data, size_t len)
+{
+ sha2big_update(cc, data, len);
+}
+
+/* see bearssl.h */
+void
+br_sha384_out(const br_sha384_context *cc, void *dst)
+{
+ sha2big_out(cc, dst, 6);
+}
+
+/* see bearssl.h */
+uint64_t
+br_sha384_state(const br_sha384_context *cc, void *dst)
+{
+ br_range_enc64be(dst, cc->val, 8);
+ return cc->count;
+}
+
+/* see bearssl.h */
+void
+br_sha384_set_state(br_sha384_context *cc, const void *stb, uint64_t count)
+{
+ br_range_dec64be(cc->val, 8, stb);
+ cc->count = count;
+}
+
+/* see bearssl.h */
+void
+br_sha512_init(br_sha512_context *cc)
+{
+ cc->vtable = &br_sha512_vtable;
+ memcpy(cc->val, IV512, sizeof IV512);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_sha512_out(const br_sha512_context *cc, void *dst)
+{
+ sha2big_out(cc, dst, 8);
+}
+
+/* see bearssl.h */
+const br_hash_class br_sha384_vtable = {
+ sizeof(br_sha384_context),
+ BR_HASHDESC_ID(br_sha384_ID)
+ | BR_HASHDESC_OUT(48)
+ | BR_HASHDESC_STATE(64)
+ | BR_HASHDESC_LBLEN(7)
+ | BR_HASHDESC_MD_PADDING
+ | BR_HASHDESC_MD_PADDING_BE
+ | BR_HASHDESC_MD_PADDING_128,
+ (void (*)(const br_hash_class **))&br_sha384_init,
+ (void (*)(const br_hash_class **, const void *, size_t))
+ &br_sha384_update,
+ (void (*)(const br_hash_class *const *, void *))&br_sha384_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_sha384_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_sha384_set_state
+};
+
+/* see bearssl.h */
+const br_hash_class br_sha512_vtable = {
+ sizeof(br_sha512_context),
+ BR_HASHDESC_ID(br_sha512_ID)
+ | BR_HASHDESC_OUT(64)
+ | BR_HASHDESC_STATE(64)
+ | BR_HASHDESC_LBLEN(7)
+ | BR_HASHDESC_MD_PADDING
+ | BR_HASHDESC_MD_PADDING_BE
+ | BR_HASHDESC_MD_PADDING_128,
+ (void (*)(const br_hash_class **))&br_sha512_init,
+ (void (*)(const br_hash_class **, const void *, size_t))
+ &br_sha512_update,
+ (void (*)(const br_hash_class *const *, void *))&br_sha512_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_sha512_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_sha512_set_state
+};
diff --git a/test/monniaux/BearSSL/src/hash/sha2small.c b/test/monniaux/BearSSL/src/hash/sha2small.c
new file mode 100644
index 00000000..ca196559
--- /dev/null
+++ b/test/monniaux/BearSSL/src/hash/sha2small.c
@@ -0,0 +1,341 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define CH(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z))
+#define MAJ(X, Y, Z) (((Y) & (Z)) | (((Y) | (Z)) & (X)))
+
+#define ROTR(x, n) (((uint32_t)(x) << (32 - (n))) | ((uint32_t)(x) >> (n)))
+
+#define BSG2_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
+#define BSG2_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
+#define SSG2_0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (uint32_t)((x) >> 3))
+#define SSG2_1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (uint32_t)((x) >> 10))
+
+/* see inner.h */
+const uint32_t br_sha224_IV[8] = {
+ 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939,
+ 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4
+};
+
+/* see inner.h */
+const uint32_t br_sha256_IV[8] = {
+ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
+ 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
+};
+
+static const uint32_t K[64] = {
+ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
+ 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
+ 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
+ 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
+ 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
+ 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
+ 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
+ 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
+ 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
+ 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
+ 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
+ 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
+ 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
+ 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
+ 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
+ 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
+};
+
+/* see inner.h */
+void
+br_sha2small_round(const unsigned char *buf, uint32_t *val)
+{
+
+#define SHA2_STEP(A, B, C, D, E, F, G, H, j) do { \
+ uint32_t T1, T2; \
+ T1 = H + BSG2_1(E) + CH(E, F, G) + K[j] + w[j]; \
+ T2 = BSG2_0(A) + MAJ(A, B, C); \
+ D += T1; \
+ H = T1 + T2; \
+ } while (0)
+
+ int i;
+ uint32_t a, b, c, d, e, f, g, h;
+ uint32_t w[64];
+
+ br_range_dec32be(w, 16, buf);
+ for (i = 16; i < 64; i ++) {
+ w[i] = SSG2_1(w[i - 2]) + w[i - 7]
+ + SSG2_0(w[i - 15]) + w[i - 16];
+ }
+ a = val[0];
+ b = val[1];
+ c = val[2];
+ d = val[3];
+ e = val[4];
+ f = val[5];
+ g = val[6];
+ h = val[7];
+ for (i = 0; i < 64; i += 8) {
+ SHA2_STEP(a, b, c, d, e, f, g, h, i + 0);
+ SHA2_STEP(h, a, b, c, d, e, f, g, i + 1);
+ SHA2_STEP(g, h, a, b, c, d, e, f, i + 2);
+ SHA2_STEP(f, g, h, a, b, c, d, e, i + 3);
+ SHA2_STEP(e, f, g, h, a, b, c, d, i + 4);
+ SHA2_STEP(d, e, f, g, h, a, b, c, i + 5);
+ SHA2_STEP(c, d, e, f, g, h, a, b, i + 6);
+ SHA2_STEP(b, c, d, e, f, g, h, a, i + 7);
+ }
+ val[0] += a;
+ val[1] += b;
+ val[2] += c;
+ val[3] += d;
+ val[4] += e;
+ val[5] += f;
+ val[6] += g;
+ val[7] += h;
+
+#if 0
+/* obsolete */
+#define SHA2_MEXP1(pc) do { \
+ W[pc] = br_dec32be(buf + ((pc) << 2)); \
+ } while (0)
+
+#define SHA2_MEXP2(pc) do { \
+ W[(pc) & 0x0F] = SSG2_1(W[((pc) - 2) & 0x0F]) \
+ + W[((pc) - 7) & 0x0F] \
+ + SSG2_0(W[((pc) - 15) & 0x0F]) + W[(pc) & 0x0F]; \
+ } while (0)
+
+#define SHA2_STEPn(n, a, b, c, d, e, f, g, h, pc) do { \
+ uint32_t t1, t2; \
+ SHA2_MEXP ## n(pc); \
+ t1 = h + BSG2_1(e) + CH(e, f, g) \
+ + K[pcount + (pc)] + W[(pc) & 0x0F]; \
+ t2 = BSG2_0(a) + MAJ(a, b, c); \
+ d += t1; \
+ h = t1 + t2; \
+ } while (0)
+
+#define SHA2_STEP1(a, b, c, d, e, f, g, h, pc) \
+ SHA2_STEPn(1, a, b, c, d, e, f, g, h, pc)
+#define SHA2_STEP2(a, b, c, d, e, f, g, h, pc) \
+ SHA2_STEPn(2, a, b, c, d, e, f, g, h, pc)
+
+ uint32_t A, B, C, D, E, F, G, H;
+ uint32_t W[16];
+ unsigned pcount;
+
+ A = val[0];
+ B = val[1];
+ C = val[2];
+ D = val[3];
+ E = val[4];
+ F = val[5];
+ G = val[6];
+ H = val[7];
+ pcount = 0;
+ SHA2_STEP1(A, B, C, D, E, F, G, H, 0);
+ SHA2_STEP1(H, A, B, C, D, E, F, G, 1);
+ SHA2_STEP1(G, H, A, B, C, D, E, F, 2);
+ SHA2_STEP1(F, G, H, A, B, C, D, E, 3);
+ SHA2_STEP1(E, F, G, H, A, B, C, D, 4);
+ SHA2_STEP1(D, E, F, G, H, A, B, C, 5);
+ SHA2_STEP1(C, D, E, F, G, H, A, B, 6);
+ SHA2_STEP1(B, C, D, E, F, G, H, A, 7);
+ SHA2_STEP1(A, B, C, D, E, F, G, H, 8);
+ SHA2_STEP1(H, A, B, C, D, E, F, G, 9);
+ SHA2_STEP1(G, H, A, B, C, D, E, F, 10);
+ SHA2_STEP1(F, G, H, A, B, C, D, E, 11);
+ SHA2_STEP1(E, F, G, H, A, B, C, D, 12);
+ SHA2_STEP1(D, E, F, G, H, A, B, C, 13);
+ SHA2_STEP1(C, D, E, F, G, H, A, B, 14);
+ SHA2_STEP1(B, C, D, E, F, G, H, A, 15);
+ for (pcount = 16; pcount < 64; pcount += 16) {
+ SHA2_STEP2(A, B, C, D, E, F, G, H, 0);
+ SHA2_STEP2(H, A, B, C, D, E, F, G, 1);
+ SHA2_STEP2(G, H, A, B, C, D, E, F, 2);
+ SHA2_STEP2(F, G, H, A, B, C, D, E, 3);
+ SHA2_STEP2(E, F, G, H, A, B, C, D, 4);
+ SHA2_STEP2(D, E, F, G, H, A, B, C, 5);
+ SHA2_STEP2(C, D, E, F, G, H, A, B, 6);
+ SHA2_STEP2(B, C, D, E, F, G, H, A, 7);
+ SHA2_STEP2(A, B, C, D, E, F, G, H, 8);
+ SHA2_STEP2(H, A, B, C, D, E, F, G, 9);
+ SHA2_STEP2(G, H, A, B, C, D, E, F, 10);
+ SHA2_STEP2(F, G, H, A, B, C, D, E, 11);
+ SHA2_STEP2(E, F, G, H, A, B, C, D, 12);
+ SHA2_STEP2(D, E, F, G, H, A, B, C, 13);
+ SHA2_STEP2(C, D, E, F, G, H, A, B, 14);
+ SHA2_STEP2(B, C, D, E, F, G, H, A, 15);
+ }
+ val[0] += A;
+ val[1] += B;
+ val[2] += C;
+ val[3] += D;
+ val[4] += E;
+ val[5] += F;
+ val[6] += G;
+ val[7] += H;
+#endif
+}
+
+static void
+sha2small_update(br_sha224_context *cc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t ptr;
+
+ buf = data;
+ ptr = (size_t)cc->count & 63;
+ cc->count += (uint64_t)len;
+ while (len > 0) {
+ size_t clen;
+
+ clen = 64 - ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(cc->buf + ptr, buf, clen);
+ ptr += clen;
+ buf += clen;
+ len -= clen;
+ if (ptr == 64) {
+ br_sha2small_round(cc->buf, cc->val);
+ ptr = 0;
+ }
+ }
+}
+
+static void
+sha2small_out(const br_sha224_context *cc, void *dst, int num)
+{
+ unsigned char buf[64];
+ uint32_t val[8];
+ size_t ptr;
+
+ ptr = (size_t)cc->count & 63;
+ memcpy(buf, cc->buf, ptr);
+ memcpy(val, cc->val, sizeof val);
+ buf[ptr ++] = 0x80;
+ if (ptr > 56) {
+ memset(buf + ptr, 0, 64 - ptr);
+ br_sha2small_round(buf, val);
+ memset(buf, 0, 56);
+ } else {
+ memset(buf + ptr, 0, 56 - ptr);
+ }
+ br_enc64be(buf + 56, cc->count << 3);
+ br_sha2small_round(buf, val);
+ br_range_enc32be(dst, val, num);
+}
+
+/* see bearssl.h */
+void
+br_sha224_init(br_sha224_context *cc)
+{
+ cc->vtable = &br_sha224_vtable;
+ memcpy(cc->val, br_sha224_IV, sizeof cc->val);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_sha224_update(br_sha224_context *cc, const void *data, size_t len)
+{
+ sha2small_update(cc, data, len);
+}
+
+/* see bearssl.h */
+void
+br_sha224_out(const br_sha224_context *cc, void *dst)
+{
+ sha2small_out(cc, dst, 7);
+}
+
+/* see bearssl.h */
+uint64_t
+br_sha224_state(const br_sha224_context *cc, void *dst)
+{
+ br_range_enc32be(dst, cc->val, 8);
+ return cc->count;
+}
+
+/* see bearssl.h */
+void
+br_sha224_set_state(br_sha224_context *cc, const void *stb, uint64_t count)
+{
+ br_range_dec32be(cc->val, 8, stb);
+ cc->count = count;
+}
+
+/* see bearssl.h */
+void
+br_sha256_init(br_sha256_context *cc)
+{
+ cc->vtable = &br_sha256_vtable;
+ memcpy(cc->val, br_sha256_IV, sizeof cc->val);
+ cc->count = 0;
+}
+
+/* see bearssl.h */
+void
+br_sha256_out(const br_sha256_context *cc, void *dst)
+{
+ sha2small_out(cc, dst, 8);
+}
+
+/* see bearssl.h */
+const br_hash_class br_sha224_vtable = {
+ sizeof(br_sha224_context),
+ BR_HASHDESC_ID(br_sha224_ID)
+ | BR_HASHDESC_OUT(28)
+ | BR_HASHDESC_STATE(32)
+ | BR_HASHDESC_LBLEN(6)
+ | BR_HASHDESC_MD_PADDING
+ | BR_HASHDESC_MD_PADDING_BE,
+ (void (*)(const br_hash_class **))&br_sha224_init,
+ (void (*)(const br_hash_class **,
+ const void *, size_t))&br_sha224_update,
+ (void (*)(const br_hash_class *const *, void *))&br_sha224_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_sha224_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_sha224_set_state
+};
+
+/* see bearssl.h */
+const br_hash_class br_sha256_vtable = {
+ sizeof(br_sha256_context),
+ BR_HASHDESC_ID(br_sha256_ID)
+ | BR_HASHDESC_OUT(32)
+ | BR_HASHDESC_STATE(32)
+ | BR_HASHDESC_LBLEN(6)
+ | BR_HASHDESC_MD_PADDING
+ | BR_HASHDESC_MD_PADDING_BE,
+ (void (*)(const br_hash_class **))&br_sha256_init,
+ (void (*)(const br_hash_class **,
+ const void *, size_t))&br_sha256_update,
+ (void (*)(const br_hash_class *const *, void *))&br_sha256_out,
+ (uint64_t (*)(const br_hash_class *const *, void *))&br_sha256_state,
+ (void (*)(const br_hash_class **, const void *, uint64_t))
+ &br_sha256_set_state
+};
diff --git a/test/monniaux/BearSSL/src/inner.h b/test/monniaux/BearSSL/src/inner.h
new file mode 100644
index 00000000..986220f0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/inner.h
@@ -0,0 +1,2557 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef INNER_H__
+#define INNER_H__
+
+#include <string.h>
+#include <limits.h>
+
+#include "config.h"
+#include "bearssl.h"
+
+/*
+ * On MSVC, disable the warning about applying unary minus on an
+ * unsigned type: it is standard, we do it all the time, and for
+ * good reasons.
+ */
+#if _MSC_VER
+#pragma warning( disable : 4146 )
+#endif
+
+/*
+ * Maximum size for a RSA modulus (in bits). Allocated stack buffers
+ * depend on that size, so this value should be kept small. Currently,
+ * 2048-bit RSA keys offer adequate security, and should still do so for
+ * the next few decades; however, a number of widespread PKI have
+ * already set their root keys to RSA-4096, so we should be able to
+ * process such keys.
+ *
+ * This value MUST be a multiple of 64. This value MUST NOT exceed 47666
+ * (some computations in RSA key generation rely on the factor size being
+ * no more than 23833 bits). RSA key sizes beyond 3072 bits don't make a
+ * lot of sense anyway.
+ */
+#define BR_MAX_RSA_SIZE 4096
+
+/*
+ * Minimum size for a RSA modulus (in bits); this value is used only to
+ * filter out invalid parameters for key pair generation. Normally,
+ * applications should not use RSA keys smaller than 2048 bits; but some
+ * specific cases might need shorter keys, for legacy or research
+ * purposes.
+ */
+#define BR_MIN_RSA_SIZE 512
+
+/*
+ * Maximum size for a RSA factor (in bits). This is for RSA private-key
+ * operations. Default is to support factors up to a bit more than half
+ * the maximum modulus size.
+ *
+ * This value MUST be a multiple of 32.
+ */
+#define BR_MAX_RSA_FACTOR ((BR_MAX_RSA_SIZE + 64) >> 1)
+
+/*
+ * Maximum size for an EC curve (modulus or order), in bits. Size of
+ * stack buffers depends on that parameter. This size MUST be a multiple
+ * of 8 (so that decoding an integer with that many bytes does not
+ * overflow).
+ */
+#define BR_MAX_EC_SIZE 528
+
+/*
+ * Some macros to recognize the current architecture. Right now, we are
+ * interested into automatically recognizing architecture with efficient
+ * 64-bit types so that we may automatically use implementations that
+ * use 64-bit registers in that case. Future versions may detect, e.g.,
+ * availability of SSE2 intrinsics.
+ *
+ * If 'unsigned long' is a 64-bit type, then we assume that 64-bit types
+ * are efficient. Otherwise, we rely on macros that depend on compiler,
+ * OS and architecture. In any case, failure to detect the architecture
+ * as 64-bit means that the 32-bit code will be used, and that code
+ * works also on 64-bit architectures (the 64-bit code may simply be
+ * more efficient).
+ *
+ * The test on 'unsigned long' should already catch most cases, the one
+ * notable exception being Windows code where 'unsigned long' is kept to
+ * 32-bit for compatibility with all the legacy code that liberally uses
+ * the 'DWORD' type for 32-bit values.
+ *
+ * Macro names are taken from: http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
+ */
+#ifndef BR_64
+#if ((ULONG_MAX >> 31) >> 31) == 3
+#define BR_64 1
+#elif defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
+#define BR_64 1
+#elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \
+ || defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
+#define BR_64 1
+#elif defined(__sparc64__)
+#define BR_64 1
+#elif defined(__x86_64__) || defined(_M_X64)
+#define BR_64 1
+#elif defined(__aarch64__) || defined(_M_ARM64)
+#define BR_64 1
+#elif defined(__mips64)
+#define BR_64 1
+#endif
+#endif
+
+/*
+ * Set BR_LOMUL on platforms where it makes sense.
+ */
+#ifndef BR_LOMUL
+#if BR_ARMEL_CORTEXM_GCC
+#define BR_LOMUL 1
+#endif
+#endif
+
+/*
+ * Architecture detection.
+ */
+#ifndef BR_i386
+#if __i386__ || _M_IX86
+#define BR_i386 1
+#endif
+#endif
+
+#ifndef BR_amd64
+#if __x86_64__ || _M_X64
+#define BR_amd64 1
+#endif
+#endif
+
+/*
+ * Compiler brand and version.
+ *
+ * Implementations that use intrinsics need to detect the compiler type
+ * and version because some specific actions may be needed to activate
+ * the corresponding opcodes, both for header inclusion, and when using
+ * them in a function.
+ *
+ * BR_GCC, BR_CLANG and BR_MSC will be set to 1 for, respectively, GCC,
+ * Clang and MS Visual C. For each of them, sub-macros will be defined
+ * for versions; each sub-macro is set whenever the compiler version is
+ * at least as recent as the one corresponding to the macro.
+ */
+
+/*
+ * GCC thresholds are on versions 4.4 to 4.9 and 5.0.
+ */
+#ifndef BR_GCC
+#if __GNUC__ && !__clang__
+#define BR_GCC 1
+
+#if __GNUC__ > 4
+#define BR_GCC_5_0 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
+#define BR_GCC_4_9 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 8
+#define BR_GCC_4_8 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 7
+#define BR_GCC_4_7 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
+#define BR_GCC_4_6 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 5
+#define BR_GCC_4_5 1
+#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 4
+#define BR_GCC_4_4 1
+#endif
+
+#if BR_GCC_5_0
+#define BR_GCC_4_9 1
+#endif
+#if BR_GCC_4_9
+#define BR_GCC_4_8 1
+#endif
+#if BR_GCC_4_8
+#define BR_GCC_4_7 1
+#endif
+#if BR_GCC_4_7
+#define BR_GCC_4_6 1
+#endif
+#if BR_GCC_4_6
+#define BR_GCC_4_5 1
+#endif
+#if BR_GCC_4_5
+#define BR_GCC_4_4 1
+#endif
+
+#endif
+#endif
+
+/*
+ * Clang thresholds are on versions 3.7.0 and 3.8.0.
+ */
+#ifndef BR_CLANG
+#if __clang__
+#define BR_CLANG 1
+
+#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 8)
+#define BR_CLANG_3_8 1
+#elif __clang_major__ == 3 && __clang_minor__ >= 7
+#define BR_CLANG_3_7 1
+#endif
+
+#if BR_CLANG_3_8
+#define BR_CLANG_3_7 1
+#endif
+
+#endif
+#endif
+
+/*
+ * MS Visual C thresholds are on Visual Studio 2005 to 2015.
+ */
+#ifndef BR_MSC
+#if _MSC_VER
+#define BR_MSC 1
+
+#if _MSC_VER >= 1900
+#define BR_MSC_2015 1
+#elif _MSC_VER >= 1800
+#define BR_MSC_2013 1
+#elif _MSC_VER >= 1700
+#define BR_MSC_2012 1
+#elif _MSC_VER >= 1600
+#define BR_MSC_2010 1
+#elif _MSC_VER >= 1500
+#define BR_MSC_2008 1
+#elif _MSC_VER >= 1400
+#define BR_MSC_2005 1
+#endif
+
+#if BR_MSC_2015
+#define BR_MSC_2013 1
+#endif
+#if BR_MSC_2013
+#define BR_MSC_2012 1
+#endif
+#if BR_MSC_2012
+#define BR_MSC_2010 1
+#endif
+#if BR_MSC_2010
+#define BR_MSC_2008 1
+#endif
+#if BR_MSC_2008
+#define BR_MSC_2005 1
+#endif
+
+#endif
+#endif
+
+/*
+ * GCC 4.4+ and Clang 3.7+ allow tagging specific functions with a
+ * 'target' attribute that activates support for specific opcodes.
+ */
+#if BR_GCC_4_4 || BR_CLANG_3_7
+#define BR_TARGET(x) __attribute__((target(x)))
+#else
+#define BR_TARGET(x)
+#endif
+
+/*
+ * AES-NI intrinsics are available on x86 (32-bit and 64-bit) with
+ * GCC 4.8+, Clang 3.7+ and MSC 2012+.
+ */
+#ifndef BR_AES_X86NI
+#if (BR_i386 || BR_amd64) && (BR_GCC_4_8 || BR_CLANG_3_7 || BR_MSC_2012)
+#define BR_AES_X86NI 1
+#endif
+#endif
+
+/*
+ * SSE2 intrinsics are available on x86 (32-bit and 64-bit) with
+ * GCC 4.4+, Clang 3.7+ and MSC 2005+.
+ */
+#ifndef BR_SSE2
+#if (BR_i386 || BR_amd64) && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
+#define BR_SSE2 1
+#endif
+#endif
+
+/*
+ * RDRAND intrinsics are available on x86 (32-bit and 64-bit) with
+ * GCC 4.6+, Clang 3.7+ and MSC 2012+.
+ */
+#ifndef BR_RDRAND
+#if (BR_i386 || BR_amd64) && (BR_GCC_4_6 || BR_CLANG_3_7 || BR_MSC_2012)
+#define BR_RDRAND 1
+#endif
+#endif
+
+/*
+ * Determine type of OS for random number generation. Macro names and
+ * values are documented on:
+ * https://sourceforge.net/p/predef/wiki/OperatingSystems/
+ *
+ * TODO: enrich the list of detected system. Also add detection for
+ * alternate system calls like getentropy(), which are usually
+ * preferable when available.
+ */
+
+#ifndef BR_USE_URANDOM
+#if defined _AIX \
+ || defined __ANDROID__ \
+ || defined __FreeBSD__ \
+ || defined __NetBSD__ \
+ || defined __OpenBSD__ \
+ || defined __DragonFly__ \
+ || defined __linux__ \
+ || (defined __sun && (defined __SVR4 || defined __svr4__)) \
+ || (defined __APPLE__ && defined __MACH__)
+#define BR_USE_URANDOM 1
+#endif
+#endif
+
+#ifndef BR_USE_WIN32_RAND
+#if defined _WIN32 || defined _WIN64
+#define BR_USE_WIN32_RAND 1
+#endif
+#endif
+
+/*
+ * POWER8 crypto support. We rely on compiler macros for the
+ * architecture, since we do not have a reliable, simple way to detect
+ * the required support at runtime (we could try running an opcode, and
+ * trapping the exception or signal on illegal instruction, but this
+ * induces some non-trivial OS dependencies that we would prefer to
+ * avoid if possible).
+ */
+#ifndef BR_POWER8
+#if __GNUC__ && ((_ARCH_PWR8 || _ARCH_PPC) && __CRYPTO__)
+#define BR_POWER8 1
+#endif
+#endif
+
+/*
+ * Detect endinanness on POWER8.
+ */
+#if BR_POWER8
+#if defined BR_POWER8_LE
+#undef BR_POWER8_BE
+#if BR_POWER8_LE
+#define BR_POWER8_BE 0
+#else
+#define BR_POWER8_BE 1
+#endif
+#elif defined BR_POWER8_BE
+#undef BR_POWER8_LE
+#if BR_POWER8_BE
+#define BR_POWER8_LE 0
+#else
+#define BR_POWER8_LE 1
+#endif
+#else
+#if __LITTLE_ENDIAN__
+#define BR_POWER8_LE 1
+#define BR_POWER8_BE 0
+#else
+#define BR_POWER8_LE 0
+#define BR_POWER8_BE 1
+#endif
+#endif
+#endif
+
+/*
+ * Detect support for 128-bit integers.
+ */
+#if !defined BR_INT128 && !defined BR_UMUL128
+#ifdef __SIZEOF_INT128__
+#define BR_INT128 1
+#elif _M_X64
+#define BR_UMUL128 1
+#endif
+#endif
+
+/*
+ * Detect support for unaligned accesses with known endianness.
+ *
+ * x86 (both 32-bit and 64-bit) is little-endian and allows unaligned
+ * accesses.
+ *
+ * POWER/PowerPC allows unaligned accesses when big-endian. POWER8 and
+ * later also allow unaligned accesses when little-endian.
+ */
+#if !defined BR_LE_UNALIGNED && !defined BR_BE_UNALIGNED
+
+#if __i386 || __i386__ || __x86_64__ || _M_IX86 || _M_X64
+#define BR_LE_UNALIGNED 1
+#elif BR_POWER8_BE
+#define BR_BE_UNALIGNED 1
+#elif BR_POWER8_LE
+#define BR_LE_UNALIGNED 1
+#elif (__powerpc__ || __powerpc64__ || _M_PPC || _ARCH_PPC || _ARCH_PPC64) \
+ && __BIG_ENDIAN__
+#define BR_BE_UNALIGNED 1
+#endif
+
+#endif
+
+/*
+ * Detect support for an OS-provided time source.
+ */
+
+#ifndef BR_USE_UNIX_TIME
+#if defined __unix__ || defined __linux__ \
+ || defined _POSIX_SOURCE || defined _POSIX_C_SOURCE \
+ || (defined __APPLE__ && defined __MACH__)
+#define BR_USE_UNIX_TIME 1
+#endif
+#endif
+
+#ifndef BR_USE_WIN32_TIME
+#if defined _WIN32 || defined _WIN64
+#define BR_USE_WIN32_TIME 1
+#endif
+#endif
+
+/* ==================================================================== */
+/*
+ * Encoding/decoding functions.
+ *
+ * 32-bit and 64-bit decoding, both little-endian and big-endian, is
+ * implemented with the inline functions below.
+ *
+ * When allowed by some compile-time options (autodetected or provided),
+ * optimised code is used, to perform direct memory access when the
+ * underlying architecture supports it, both for endianness and
+ * alignment. This, however, may trigger strict aliasing issues; the
+ * code below uses unions to perform (supposedly) safe type punning.
+ * Since the C aliasing rules are relatively complex and were amended,
+ * or at least re-explained with different phrasing, in all successive
+ * versions of the C standard, it is always a bit risky to bet that any
+ * specific version of a C compiler got it right, for some notion of
+ * "right".
+ */
+
+typedef union {
+ uint16_t u;
+ unsigned char b[sizeof(uint16_t)];
+} br_union_u16;
+
+typedef union {
+ uint32_t u;
+ unsigned char b[sizeof(uint32_t)];
+} br_union_u32;
+
+typedef union {
+ uint64_t u;
+ unsigned char b[sizeof(uint64_t)];
+} br_union_u64;
+
+static inline void
+br_enc16le(void *dst, unsigned x)
+{
+#if BR_LE_UNALIGNED
+ ((br_union_u16 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = (unsigned char)x;
+ buf[1] = (unsigned char)(x >> 8);
+#endif
+}
+
+static inline void
+br_enc16be(void *dst, unsigned x)
+{
+#if BR_BE_UNALIGNED
+ ((br_union_u16 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = (unsigned char)(x >> 8);
+ buf[1] = (unsigned char)x;
+#endif
+}
+
+static inline unsigned
+br_dec16le(const void *src)
+{
+#if BR_LE_UNALIGNED
+ return ((const br_union_u16 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return (unsigned)buf[0] | ((unsigned)buf[1] << 8);
+#endif
+}
+
+static inline unsigned
+br_dec16be(const void *src)
+{
+#if BR_BE_UNALIGNED
+ return ((const br_union_u16 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
+#endif
+}
+
+static inline void
+br_enc32le(void *dst, uint32_t x)
+{
+#if BR_LE_UNALIGNED
+ ((br_union_u32 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = (unsigned char)x;
+ buf[1] = (unsigned char)(x >> 8);
+ buf[2] = (unsigned char)(x >> 16);
+ buf[3] = (unsigned char)(x >> 24);
+#endif
+}
+
+static inline void
+br_enc32be(void *dst, uint32_t x)
+{
+#if BR_BE_UNALIGNED
+ ((br_union_u32 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ buf[0] = (unsigned char)(x >> 24);
+ buf[1] = (unsigned char)(x >> 16);
+ buf[2] = (unsigned char)(x >> 8);
+ buf[3] = (unsigned char)x;
+#endif
+}
+
+static inline uint32_t
+br_dec32le(const void *src)
+{
+#if BR_LE_UNALIGNED
+ return ((const br_union_u32 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return (uint32_t)buf[0]
+ | ((uint32_t)buf[1] << 8)
+ | ((uint32_t)buf[2] << 16)
+ | ((uint32_t)buf[3] << 24);
+#endif
+}
+
+static inline uint32_t
+br_dec32be(const void *src)
+{
+#if BR_BE_UNALIGNED
+ return ((const br_union_u32 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return ((uint32_t)buf[0] << 24)
+ | ((uint32_t)buf[1] << 16)
+ | ((uint32_t)buf[2] << 8)
+ | (uint32_t)buf[3];
+#endif
+}
+
+static inline void
+br_enc64le(void *dst, uint64_t x)
+{
+#if BR_LE_UNALIGNED
+ ((br_union_u64 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ br_enc32le(buf, (uint32_t)x);
+ br_enc32le(buf + 4, (uint32_t)(x >> 32));
+#endif
+}
+
+static inline void
+br_enc64be(void *dst, uint64_t x)
+{
+#if BR_BE_UNALIGNED
+ ((br_union_u64 *)dst)->u = x;
+#else
+ unsigned char *buf;
+
+ buf = dst;
+ br_enc32be(buf, (uint32_t)(x >> 32));
+ br_enc32be(buf + 4, (uint32_t)x);
+#endif
+}
+
+static inline uint64_t
+br_dec64le(const void *src)
+{
+#if BR_LE_UNALIGNED
+ return ((const br_union_u64 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return (uint64_t)br_dec32le(buf)
+ | ((uint64_t)br_dec32le(buf + 4) << 32);
+#endif
+}
+
+static inline uint64_t
+br_dec64be(const void *src)
+{
+#if BR_BE_UNALIGNED
+ return ((const br_union_u64 *)src)->u;
+#else
+ const unsigned char *buf;
+
+ buf = src;
+ return ((uint64_t)br_dec32be(buf) << 32)
+ | (uint64_t)br_dec32be(buf + 4);
+#endif
+}
+
+/*
+ * Range decoding and encoding (for several successive values).
+ */
+void br_range_dec16le(uint16_t *v, size_t num, const void *src);
+void br_range_dec16be(uint16_t *v, size_t num, const void *src);
+void br_range_enc16le(void *dst, const uint16_t *v, size_t num);
+void br_range_enc16be(void *dst, const uint16_t *v, size_t num);
+
+void br_range_dec32le(uint32_t *v, size_t num, const void *src);
+void br_range_dec32be(uint32_t *v, size_t num, const void *src);
+void br_range_enc32le(void *dst, const uint32_t *v, size_t num);
+void br_range_enc32be(void *dst, const uint32_t *v, size_t num);
+
+void br_range_dec64le(uint64_t *v, size_t num, const void *src);
+void br_range_dec64be(uint64_t *v, size_t num, const void *src);
+void br_range_enc64le(void *dst, const uint64_t *v, size_t num);
+void br_range_enc64be(void *dst, const uint64_t *v, size_t num);
+
+/*
+ * Byte-swap a 32-bit integer.
+ */
+static inline uint32_t
+br_swap32(uint32_t x)
+{
+ x = ((x & (uint32_t)0x00FF00FF) << 8)
+ | ((x >> 8) & (uint32_t)0x00FF00FF);
+ return (x << 16) | (x >> 16);
+}
+
+/* ==================================================================== */
+/*
+ * Support code for hash functions.
+ */
+
+/*
+ * IV for MD5, SHA-1, SHA-224 and SHA-256.
+ */
+extern const uint32_t br_md5_IV[];
+extern const uint32_t br_sha1_IV[];
+extern const uint32_t br_sha224_IV[];
+extern const uint32_t br_sha256_IV[];
+
+/*
+ * Round functions for MD5, SHA-1, SHA-224 and SHA-256 (SHA-224 and
+ * SHA-256 use the same round function).
+ */
+void br_md5_round(const unsigned char *buf, uint32_t *val);
+void br_sha1_round(const unsigned char *buf, uint32_t *val);
+void br_sha2small_round(const unsigned char *buf, uint32_t *val);
+
+/*
+ * The core function for the TLS PRF. It computes
+ * P_hash(secret, label + seed), and XORs the result into the dst buffer.
+ */
+void br_tls_phash(void *dst, size_t len,
+ const br_hash_class *dig,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed);
+
+/*
+ * Copy all configured hash implementations from a multihash context
+ * to another.
+ */
+static inline void
+br_multihash_copyimpl(br_multihash_context *dst,
+ const br_multihash_context *src)
+{
+ memcpy((void *)dst->impl, src->impl, sizeof src->impl);
+}
+
+/* ==================================================================== */
+/*
+ * Constant-time primitives. These functions manipulate 32-bit values in
+ * order to provide constant-time comparisons and multiplexers.
+ *
+ * Boolean values (the "ctl" bits) MUST have value 0 or 1.
+ *
+ * Implementation notes:
+ * =====================
+ *
+ * The uintN_t types are unsigned and with width exactly N bits; the C
+ * standard guarantees that computations are performed modulo 2^N, and
+ * there can be no overflow. Negation (unary '-') works on unsigned types
+ * as well.
+ *
+ * The intN_t types are guaranteed to have width exactly N bits, with no
+ * padding bit, and using two's complement representation. Casting
+ * intN_t to uintN_t really is conversion modulo 2^N. Beware that intN_t
+ * types, being signed, trigger implementation-defined behaviour on
+ * overflow (including raising some signal): with GCC, while modular
+ * arithmetics are usually applied, the optimizer may assume that
+ * overflows don't occur (unless the -fwrapv command-line option is
+ * added); Clang has the additional -ftrapv option to explicitly trap on
+ * integer overflow or underflow.
+ */
+
+/*
+ * Negate a boolean.
+ */
+static inline uint32_t
+NOT(uint32_t ctl)
+{
+ return ctl ^ 1;
+}
+
+/*
+ * Multiplexer: returns x if ctl == 1, y if ctl == 0.
+ */
+static inline uint32_t
+MUX(uint32_t ctl, uint32_t x, uint32_t y)
+{
+ return y ^ (-ctl & (x ^ y));
+}
+
+/*
+ * Equality check: returns 1 if x == y, 0 otherwise.
+ */
+static inline uint32_t
+EQ(uint32_t x, uint32_t y)
+{
+ uint32_t q;
+
+ q = x ^ y;
+ return NOT((q | -q) >> 31);
+}
+
+/*
+ * Inequality check: returns 1 if x != y, 0 otherwise.
+ */
+static inline uint32_t
+NEQ(uint32_t x, uint32_t y)
+{
+ uint32_t q;
+
+ q = x ^ y;
+ return (q | -q) >> 31;
+}
+
+/*
+ * Comparison: returns 1 if x > y, 0 otherwise.
+ */
+static inline uint32_t
+GT(uint32_t x, uint32_t y)
+{
+ /*
+ * If both x < 2^31 and x < 2^31, then y-x will have its high
+ * bit set if x > y, cleared otherwise.
+ *
+ * If either x >= 2^31 or y >= 2^31 (but not both), then the
+ * result is the high bit of x.
+ *
+ * If both x >= 2^31 and y >= 2^31, then we can virtually
+ * subtract 2^31 from both, and we are back to the first case.
+ * Since (y-2^31)-(x-2^31) = y-x, the subtraction is already
+ * fine.
+ */
+ uint32_t z;
+
+ z = y - x;
+ return (z ^ ((x ^ y) & (x ^ z))) >> 31;
+}
+
+/*
+ * Other comparisons (greater-or-equal, lower-than, lower-or-equal).
+ */
+#define GE(x, y) NOT(GT(y, x))
+#define LT(x, y) GT(y, x)
+#define LE(x, y) NOT(GT(x, y))
+
+/*
+ * General comparison: returned value is -1, 0 or 1, depending on
+ * whether x is lower than, equal to, or greater than y.
+ */
+static inline int32_t
+CMP(uint32_t x, uint32_t y)
+{
+ return (int32_t)GT(x, y) | -(int32_t)GT(y, x);
+}
+
+/*
+ * Returns 1 if x == 0, 0 otherwise. Take care that the operand is signed.
+ */
+static inline uint32_t
+EQ0(int32_t x)
+{
+ uint32_t q;
+
+ q = (uint32_t)x;
+ return ~(q | -q) >> 31;
+}
+
+/*
+ * Returns 1 if x > 0, 0 otherwise. Take care that the operand is signed.
+ */
+static inline uint32_t
+GT0(int32_t x)
+{
+ /*
+ * High bit of -x is 0 if x == 0, but 1 if x > 0.
+ */
+ uint32_t q;
+
+ q = (uint32_t)x;
+ return (~q & -q) >> 31;
+}
+
+/*
+ * Returns 1 if x >= 0, 0 otherwise. Take care that the operand is signed.
+ */
+static inline uint32_t
+GE0(int32_t x)
+{
+ return ~(uint32_t)x >> 31;
+}
+
+/*
+ * Returns 1 if x < 0, 0 otherwise. Take care that the operand is signed.
+ */
+static inline uint32_t
+LT0(int32_t x)
+{
+ return (uint32_t)x >> 31;
+}
+
+/*
+ * Returns 1 if x <= 0, 0 otherwise. Take care that the operand is signed.
+ */
+static inline uint32_t
+LE0(int32_t x)
+{
+ uint32_t q;
+
+ /*
+ * ~-x has its high bit set if and only if -x is nonnegative (as
+ * a signed int), i.e. x is in the -(2^31-1) to 0 range. We must
+ * do an OR with x itself to account for x = -2^31.
+ */
+ q = (uint32_t)x;
+ return (q | ~-q) >> 31;
+}
+
+/*
+ * Conditional copy: src[] is copied into dst[] if and only if ctl is 1.
+ * dst[] and src[] may overlap completely (but not partially).
+ */
+void br_ccopy(uint32_t ctl, void *dst, const void *src, size_t len);
+
+#define CCOPY br_ccopy
+
+/*
+ * Compute the bit length of a 32-bit integer. Returned value is between 0
+ * and 32 (inclusive).
+ */
+static inline uint32_t
+BIT_LENGTH(uint32_t x)
+{
+ uint32_t k, c;
+
+ k = NEQ(x, 0);
+ c = GT(x, 0xFFFF); x = MUX(c, x >> 16, x); k += c << 4;
+ c = GT(x, 0x00FF); x = MUX(c, x >> 8, x); k += c << 3;
+ c = GT(x, 0x000F); x = MUX(c, x >> 4, x); k += c << 2;
+ c = GT(x, 0x0003); x = MUX(c, x >> 2, x); k += c << 1;
+ k += GT(x, 0x0001);
+ return k;
+}
+
+/*
+ * Compute the minimum of x and y.
+ */
+static inline uint32_t
+MIN(uint32_t x, uint32_t y)
+{
+ return MUX(GT(x, y), y, x);
+}
+
+/*
+ * Compute the maximum of x and y.
+ */
+static inline uint32_t
+MAX(uint32_t x, uint32_t y)
+{
+ return MUX(GT(x, y), x, y);
+}
+
+/*
+ * Multiply two 32-bit integers, with a 64-bit result. This default
+ * implementation assumes that the basic multiplication operator
+ * yields constant-time code.
+ */
+#define MUL(x, y) ((uint64_t)(x) * (uint64_t)(y))
+
+#if BR_CT_MUL31
+
+/*
+ * Alternate implementation of MUL31, that will be constant-time on some
+ * (old) platforms where the default MUL31 is not. Unfortunately, it is
+ * also substantially slower, and yields larger code, on more modern
+ * platforms, which is why it is deactivated by default.
+ *
+ * MUL31_lo() must do some extra work because on some platforms, the
+ * _signed_ multiplication may return early if the top bits are 1.
+ * Simply truncating (casting) the output of MUL31() would not be
+ * sufficient, because the compiler may notice that we keep only the low
+ * word, and then replace automatically the unsigned multiplication with
+ * a signed multiplication opcode.
+ */
+#define MUL31(x, y) ((uint64_t)((x) | (uint32_t)0x80000000) \
+ * (uint64_t)((y) | (uint32_t)0x80000000) \
+ - ((uint64_t)(x) << 31) - ((uint64_t)(y) << 31) \
+ - ((uint64_t)1 << 62))
+static inline uint32_t
+MUL31_lo(uint32_t x, uint32_t y)
+{
+ uint32_t xl, xh;
+ uint32_t yl, yh;
+
+ xl = (x & 0xFFFF) | (uint32_t)0x80000000;
+ xh = (x >> 16) | (uint32_t)0x80000000;
+ yl = (y & 0xFFFF) | (uint32_t)0x80000000;
+ yh = (y >> 16) | (uint32_t)0x80000000;
+ return (xl * yl + ((xl * yh + xh * yl) << 16)) & (uint32_t)0x7FFFFFFF;
+}
+
+#else
+
+/*
+ * Multiply two 31-bit integers, with a 62-bit result. This default
+ * implementation assumes that the basic multiplication operator
+ * yields constant-time code.
+ * The MUL31_lo() macro returns only the low 31 bits of the product.
+ */
+#define MUL31(x, y) ((uint64_t)(x) * (uint64_t)(y))
+#define MUL31_lo(x, y) (((uint32_t)(x) * (uint32_t)(y)) & (uint32_t)0x7FFFFFFF)
+
+#endif
+
+/*
+ * Multiply two words together; the sum of the lengths of the two
+ * operands must not exceed 31 (for instance, one operand may use 16
+ * bits if the other fits on 15). If BR_CT_MUL15 is non-zero, then the
+ * macro will contain some extra operations that help in making the
+ * operation constant-time on some platforms, where the basic 32-bit
+ * multiplication is not constant-time.
+ */
+#if BR_CT_MUL15
+#define MUL15(x, y) (((uint32_t)(x) | (uint32_t)0x80000000) \
+ * ((uint32_t)(y) | (uint32_t)0x80000000) \
+ & (uint32_t)0x7FFFFFFF)
+#else
+#define MUL15(x, y) ((uint32_t)(x) * (uint32_t)(y))
+#endif
+
+/*
+ * Arithmetic right shift (sign bit is copied). What happens when
+ * right-shifting a negative value is _implementation-defined_, so it
+ * does not trigger undefined behaviour, but it is still up to each
+ * compiler to define (and document) what it does. Most/all compilers
+ * will do an arithmetic shift, the sign bit being used to fill the
+ * holes; this is a native operation on the underlying CPU, and it would
+ * make little sense for the compiler to do otherwise. GCC explicitly
+ * documents that it follows that convention.
+ *
+ * Still, if BR_NO_ARITH_SHIFT is defined (and non-zero), then an
+ * alternate version will be used, that does not rely on such
+ * implementation-defined behaviour. Unfortunately, it is also slower
+ * and yields bigger code, which is why it is deactivated by default.
+ */
+#if BR_NO_ARITH_SHIFT
+#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
+ | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
+#else
+#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
+#endif
+
+/*
+ * Constant-time division. The dividend hi:lo is divided by the
+ * divisor d; the quotient is returned and the remainder is written
+ * in *r. If hi == d, then the quotient does not fit on 32 bits;
+ * returned value is thus truncated. If hi > d, returned values are
+ * indeterminate.
+ */
+uint32_t br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r);
+
+/*
+ * Wrapper for br_divrem(); the remainder is returned, and the quotient
+ * is discarded.
+ */
+static inline uint32_t
+br_rem(uint32_t hi, uint32_t lo, uint32_t d)
+{
+ uint32_t r;
+
+ br_divrem(hi, lo, d, &r);
+ return r;
+}
+
+/*
+ * Wrapper for br_divrem(); the quotient is returned, and the remainder
+ * is discarded.
+ */
+static inline uint32_t
+br_div(uint32_t hi, uint32_t lo, uint32_t d)
+{
+ uint32_t r;
+
+ return br_divrem(hi, lo, d, &r);
+}
+
+/* ==================================================================== */
+
+/*
+ * Integers 'i32'
+ * --------------
+ *
+ * The 'i32' functions implement computations on big integers using
+ * an internal representation as an array of 32-bit integers. For
+ * an array x[]:
+ * -- x[0] contains the "announced bit length" of the integer
+ * -- x[1], x[2]... contain the value in little-endian order (x[1]
+ * contains the least significant 32 bits)
+ *
+ * Multiplications rely on the elementary 32x32->64 multiplication.
+ *
+ * The announced bit length specifies the number of bits that are
+ * significant in the subsequent 32-bit words. Unused bits in the
+ * last (most significant) word are set to 0; subsequent words are
+ * uninitialized and need not exist at all.
+ *
+ * The execution time and memory access patterns of all computations
+ * depend on the announced bit length, but not on the actual word
+ * values. For modular integers, the announced bit length of any integer
+ * modulo n is equal to the actual bit length of n; thus, computations
+ * on modular integers are "constant-time" (only the modulus length may
+ * leak).
+ */
+
+/*
+ * Compute the actual bit length of an integer. The argument x should
+ * point to the first (least significant) value word of the integer.
+ * The len 'xlen' contains the number of 32-bit words to access.
+ *
+ * CT: value or length of x does not leak.
+ */
+uint32_t br_i32_bit_length(uint32_t *x, size_t xlen);
+
+/*
+ * Decode an integer from its big-endian unsigned representation. The
+ * "true" bit length of the integer is computed, but all words of x[]
+ * corresponding to the full 'len' bytes of the source are set.
+ *
+ * CT: value or length of x does not leak.
+ */
+void br_i32_decode(uint32_t *x, const void *src, size_t len);
+
+/*
+ * Decode an integer from its big-endian unsigned representation. The
+ * integer MUST be lower than m[]; the announced bit length written in
+ * x[] will be equal to that of m[]. All 'len' bytes from the source are
+ * read.
+ *
+ * Returned value is 1 if the decode value fits within the modulus, 0
+ * otherwise. In the latter case, the x[] buffer will be set to 0 (but
+ * still with the announced bit length of m[]).
+ *
+ * CT: value or length of x does not leak. Memory access pattern depends
+ * only of 'len' and the announced bit length of m. Whether x fits or
+ * not does not leak either.
+ */
+uint32_t br_i32_decode_mod(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+
+/*
+ * Reduce an integer (a[]) modulo another (m[]). The result is written
+ * in x[] and its announced bit length is set to be equal to that of m[].
+ *
+ * x[] MUST be distinct from a[] and m[].
+ *
+ * CT: only announced bit lengths leak, not values of x, a or m.
+ */
+void br_i32_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
+
+/*
+ * Decode an integer from its big-endian unsigned representation, and
+ * reduce it modulo the provided modulus m[]. The announced bit length
+ * of the result is set to be equal to that of the modulus.
+ *
+ * x[] MUST be distinct from m[].
+ */
+void br_i32_decode_reduce(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+
+/*
+ * Encode an integer into its big-endian unsigned representation. The
+ * output length in bytes is provided (parameter 'len'); if the length
+ * is too short then the integer is appropriately truncated; if it is
+ * too long then the extra bytes are set to 0.
+ */
+void br_i32_encode(void *dst, size_t len, const uint32_t *x);
+
+/*
+ * Multiply x[] by 2^32 and then add integer z, modulo m[]. This
+ * function assumes that x[] and m[] have the same announced bit
+ * length, and the announced bit length of m[] matches its true
+ * bit length.
+ *
+ * x[] and m[] MUST be distinct arrays.
+ *
+ * CT: only the common announced bit length of x and m leaks, not
+ * the values of x, z or m.
+ */
+void br_i32_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
+
+/*
+ * Extract one word from an integer. The offset is counted in bits.
+ * The word MUST entirely fit within the word elements corresponding
+ * to the announced bit length of a[].
+ */
+static inline uint32_t
+br_i32_word(const uint32_t *a, uint32_t off)
+{
+ size_t u;
+ unsigned j;
+
+ u = (size_t)(off >> 5) + 1;
+ j = (unsigned)off & 31;
+ if (j == 0) {
+ return a[u];
+ } else {
+ return (a[u] >> j) | (a[u + 1] << (32 - j));
+ }
+}
+
+/*
+ * Test whether an integer is zero.
+ */
+uint32_t br_i32_iszero(const uint32_t *x);
+
+/*
+ * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
+ * is unmodified, but the carry is still computed and returned. The
+ * arrays a[] and b[] MUST have the same announced bit length.
+ *
+ * a[] and b[] MAY be the same array, but partial overlap is not allowed.
+ */
+uint32_t br_i32_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
+
+/*
+ * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
+ * then a[] is unmodified, but the carry is still computed and returned.
+ * The arrays a[] and b[] MUST have the same announced bit length.
+ *
+ * a[] and b[] MAY be the same array, but partial overlap is not allowed.
+ */
+uint32_t br_i32_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
+
+/*
+ * Compute d+a*b, result in d. The initial announced bit length of d[]
+ * MUST match that of a[]. The d[] array MUST be large enough to
+ * accommodate the full result, plus (possibly) an extra word. The
+ * resulting announced bit length of d[] will be the sum of the announced
+ * bit lengths of a[] and b[] (therefore, it may be larger than the actual
+ * bit length of the numerical result).
+ *
+ * a[] and b[] may be the same array. d[] must be disjoint from both a[]
+ * and b[].
+ */
+void br_i32_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
+
+/*
+ * Zeroize an integer. The announced bit length is set to the provided
+ * value, and the corresponding words are set to 0.
+ */
+static inline void
+br_i32_zero(uint32_t *x, uint32_t bit_len)
+{
+ *x ++ = bit_len;
+ memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
+}
+
+/*
+ * Compute -(1/x) mod 2^32. If x is even, then this function returns 0.
+ */
+uint32_t br_i32_ninv32(uint32_t x);
+
+/*
+ * Convert a modular integer to Montgomery representation. The integer x[]
+ * MUST be lower than m[], but with the same announced bit length.
+ */
+void br_i32_to_monty(uint32_t *x, const uint32_t *m);
+
+/*
+ * Convert a modular integer back from Montgomery representation. The
+ * integer x[] MUST be lower than m[], but with the same announced bit
+ * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
+ * the least significant value word of m[] (this works only if m[] is
+ * an odd integer).
+ */
+void br_i32_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
+
+/*
+ * Compute a modular Montgomery multiplication. d[] is filled with the
+ * value of x*y/R modulo m[] (where R is the Montgomery factor). The
+ * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
+ * numerically lower than m[]. x[] and y[] MAY be the same array. The
+ * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
+ * significant value word of m[] (this works only if m[] is an odd
+ * integer).
+ */
+void br_i32_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i);
+
+/*
+ * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
+ * (same announced bit length, lower value). m[] MUST be odd. The
+ * exponent is in big-endian unsigned notation, over 'elen' bytes. The
+ * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
+ * significant value word of m[] (this works only if m[] is an odd
+ * integer). The t1[] and t2[] parameters must be temporary arrays,
+ * each large enough to accommodate an integer with the same size as m[].
+ */
+void br_i32_modpow(uint32_t *x, const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
+
+/* ==================================================================== */
+
+/*
+ * Integers 'i31'
+ * --------------
+ *
+ * The 'i31' functions implement computations on big integers using
+ * an internal representation as an array of 32-bit integers. For
+ * an array x[]:
+ * -- x[0] encodes the array length and the "announced bit length"
+ * of the integer: namely, if the announced bit length is k,
+ * then x[0] = ((k / 31) << 5) + (k % 31).
+ * -- x[1], x[2]... contain the value in little-endian order, 31
+ * bits per word (x[1] contains the least significant 31 bits).
+ * The upper bit of each word is 0.
+ *
+ * Multiplications rely on the elementary 32x32->64 multiplication.
+ *
+ * The announced bit length specifies the number of bits that are
+ * significant in the subsequent 32-bit words. Unused bits in the
+ * last (most significant) word are set to 0; subsequent words are
+ * uninitialized and need not exist at all.
+ *
+ * The execution time and memory access patterns of all computations
+ * depend on the announced bit length, but not on the actual word
+ * values. For modular integers, the announced bit length of any integer
+ * modulo n is equal to the actual bit length of n; thus, computations
+ * on modular integers are "constant-time" (only the modulus length may
+ * leak).
+ */
+
+/*
+ * Test whether an integer is zero.
+ */
+uint32_t br_i31_iszero(const uint32_t *x);
+
+/*
+ * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
+ * is unmodified, but the carry is still computed and returned. The
+ * arrays a[] and b[] MUST have the same announced bit length.
+ *
+ * a[] and b[] MAY be the same array, but partial overlap is not allowed.
+ */
+uint32_t br_i31_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
+
+/*
+ * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
+ * then a[] is unmodified, but the carry is still computed and returned.
+ * The arrays a[] and b[] MUST have the same announced bit length.
+ *
+ * a[] and b[] MAY be the same array, but partial overlap is not allowed.
+ */
+uint32_t br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
+
+/*
+ * Compute the ENCODED actual bit length of an integer. The argument x
+ * should point to the first (least significant) value word of the
+ * integer. The len 'xlen' contains the number of 32-bit words to
+ * access. The upper bit of each value word MUST be 0.
+ * Returned value is ((k / 31) << 5) + (k % 31) if the bit length is k.
+ *
+ * CT: value or length of x does not leak.
+ */
+uint32_t br_i31_bit_length(uint32_t *x, size_t xlen);
+
+/*
+ * Decode an integer from its big-endian unsigned representation. The
+ * "true" bit length of the integer is computed and set in the encoded
+ * announced bit length (x[0]), but all words of x[] corresponding to
+ * the full 'len' bytes of the source are set.
+ *
+ * CT: value or length of x does not leak.
+ */
+void br_i31_decode(uint32_t *x, const void *src, size_t len);
+
+/*
+ * Decode an integer from its big-endian unsigned representation. The
+ * integer MUST be lower than m[]; the (encoded) announced bit length
+ * written in x[] will be equal to that of m[]. All 'len' bytes from the
+ * source are read.
+ *
+ * Returned value is 1 if the decode value fits within the modulus, 0
+ * otherwise. In the latter case, the x[] buffer will be set to 0 (but
+ * still with the announced bit length of m[]).
+ *
+ * CT: value or length of x does not leak. Memory access pattern depends
+ * only of 'len' and the announced bit length of m. Whether x fits or
+ * not does not leak either.
+ */
+uint32_t br_i31_decode_mod(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+
+/*
+ * Zeroize an integer. The announced bit length is set to the provided
+ * value, and the corresponding words are set to 0. The ENCODED bit length
+ * is expected here.
+ */
+static inline void
+br_i31_zero(uint32_t *x, uint32_t bit_len)
+{
+ *x ++ = bit_len;
+ memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
+}
+
+/*
+ * Right-shift an integer. The shift amount must be lower than 31
+ * bits.
+ */
+void br_i31_rshift(uint32_t *x, int count);
+
+/*
+ * Reduce an integer (a[]) modulo another (m[]). The result is written
+ * in x[] and its announced bit length is set to be equal to that of m[].
+ *
+ * x[] MUST be distinct from a[] and m[].
+ *
+ * CT: only announced bit lengths leak, not values of x, a or m.
+ */
+void br_i31_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
+
+/*
+ * Decode an integer from its big-endian unsigned representation, and
+ * reduce it modulo the provided modulus m[]. The announced bit length
+ * of the result is set to be equal to that of the modulus.
+ *
+ * x[] MUST be distinct from m[].
+ */
+void br_i31_decode_reduce(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+
+/*
+ * Multiply x[] by 2^31 and then add integer z, modulo m[]. This
+ * function assumes that x[] and m[] have the same announced bit
+ * length, the announced bit length of m[] matches its true
+ * bit length.
+ *
+ * x[] and m[] MUST be distinct arrays. z MUST fit in 31 bits (upper
+ * bit set to 0).
+ *
+ * CT: only the common announced bit length of x and m leaks, not
+ * the values of x, z or m.
+ */
+void br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
+
+/*
+ * Encode an integer into its big-endian unsigned representation. The
+ * output length in bytes is provided (parameter 'len'); if the length
+ * is too short then the integer is appropriately truncated; if it is
+ * too long then the extra bytes are set to 0.
+ */
+void br_i31_encode(void *dst, size_t len, const uint32_t *x);
+
+/*
+ * Compute -(1/x) mod 2^31. If x is even, then this function returns 0.
+ */
+uint32_t br_i31_ninv31(uint32_t x);
+
+/*
+ * Compute a modular Montgomery multiplication. d[] is filled with the
+ * value of x*y/R modulo m[] (where R is the Montgomery factor). The
+ * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
+ * numerically lower than m[]. x[] and y[] MAY be the same array. The
+ * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
+ * significant value word of m[] (this works only if m[] is an odd
+ * integer).
+ */
+void br_i31_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i);
+
+/*
+ * Convert a modular integer to Montgomery representation. The integer x[]
+ * MUST be lower than m[], but with the same announced bit length.
+ */
+void br_i31_to_monty(uint32_t *x, const uint32_t *m);
+
+/*
+ * Convert a modular integer back from Montgomery representation. The
+ * integer x[] MUST be lower than m[], but with the same announced bit
+ * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
+ * the least significant value word of m[] (this works only if m[] is
+ * an odd integer).
+ */
+void br_i31_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
+
+/*
+ * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
+ * (same announced bit length, lower value). m[] MUST be odd. The
+ * exponent is in big-endian unsigned notation, over 'elen' bytes. The
+ * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
+ * significant value word of m[] (this works only if m[] is an odd
+ * integer). The t1[] and t2[] parameters must be temporary arrays,
+ * each large enough to accommodate an integer with the same size as m[].
+ */
+void br_i31_modpow(uint32_t *x, const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
+
+/*
+ * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
+ * (same announced bit length, lower value). m[] MUST be odd. The
+ * exponent is in big-endian unsigned notation, over 'elen' bytes. The
+ * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
+ * significant value word of m[] (this works only if m[] is an odd
+ * integer). The tmp[] array is used for temporaries, and has size
+ * 'twlen' words; it must be large enough to accommodate at least two
+ * temporary values with the same size as m[] (including the leading
+ * "bit length" word). If there is room for more temporaries, then this
+ * function may use the extra room for window-based optimisation,
+ * resulting in faster computations.
+ *
+ * Returned value is 1 on success, 0 on error. An error is reported if
+ * the provided tmp[] array is too short.
+ */
+uint32_t br_i31_modpow_opt(uint32_t *x, const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
+
+/*
+ * Compute d+a*b, result in d. The initial announced bit length of d[]
+ * MUST match that of a[]. The d[] array MUST be large enough to
+ * accommodate the full result, plus (possibly) an extra word. The
+ * resulting announced bit length of d[] will be the sum of the announced
+ * bit lengths of a[] and b[] (therefore, it may be larger than the actual
+ * bit length of the numerical result).
+ *
+ * a[] and b[] may be the same array. d[] must be disjoint from both a[]
+ * and b[].
+ */
+void br_i31_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
+
+/*
+ * Compute x/y mod m, result in x. Values x and y must be between 0 and
+ * m-1, and have the same announced bit length as m. Modulus m must be
+ * odd. The "m0i" parameter is equal to -1/m mod 2^31. The array 't'
+ * must point to a temporary area that can hold at least three integers
+ * of the size of m.
+ *
+ * m may not overlap x and y. x and y may overlap each other (this can
+ * be useful to test whether a value is invertible modulo m). t must be
+ * disjoint from all other arrays.
+ *
+ * Returned value is 1 on success, 0 otherwise. Success is attained if
+ * y is invertible modulo m.
+ */
+uint32_t br_i31_moddiv(uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i, uint32_t *t);
+
+/* ==================================================================== */
+
+/*
+ * FIXME: document "i15" functions.
+ */
+
+static inline void
+br_i15_zero(uint16_t *x, uint16_t bit_len)
+{
+ *x ++ = bit_len;
+ memset(x, 0, ((bit_len + 15) >> 4) * sizeof *x);
+}
+
+uint32_t br_i15_iszero(const uint16_t *x);
+
+uint16_t br_i15_ninv15(uint16_t x);
+
+uint32_t br_i15_add(uint16_t *a, const uint16_t *b, uint32_t ctl);
+
+uint32_t br_i15_sub(uint16_t *a, const uint16_t *b, uint32_t ctl);
+
+void br_i15_muladd_small(uint16_t *x, uint16_t z, const uint16_t *m);
+
+void br_i15_montymul(uint16_t *d, const uint16_t *x, const uint16_t *y,
+ const uint16_t *m, uint16_t m0i);
+
+void br_i15_to_monty(uint16_t *x, const uint16_t *m);
+
+void br_i15_modpow(uint16_t *x, const unsigned char *e, size_t elen,
+ const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2);
+
+uint32_t br_i15_modpow_opt(uint16_t *x, const unsigned char *e, size_t elen,
+ const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen);
+
+void br_i15_encode(void *dst, size_t len, const uint16_t *x);
+
+uint32_t br_i15_decode_mod(uint16_t *x,
+ const void *src, size_t len, const uint16_t *m);
+
+void br_i15_rshift(uint16_t *x, int count);
+
+uint32_t br_i15_bit_length(uint16_t *x, size_t xlen);
+
+void br_i15_decode(uint16_t *x, const void *src, size_t len);
+
+void br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i);
+
+void br_i15_decode_reduce(uint16_t *x,
+ const void *src, size_t len, const uint16_t *m);
+
+void br_i15_reduce(uint16_t *x, const uint16_t *a, const uint16_t *m);
+
+void br_i15_mulacc(uint16_t *d, const uint16_t *a, const uint16_t *b);
+
+uint32_t br_i15_moddiv(uint16_t *x, const uint16_t *y,
+ const uint16_t *m, uint16_t m0i, uint16_t *t);
+
+/*
+ * Variant of br_i31_modpow_opt() that internally uses 64x64->128
+ * multiplications. It expects the same parameters as br_i31_modpow_opt(),
+ * except that the temporaries should be 64-bit integers, not 32-bit
+ * integers.
+ */
+uint32_t br_i62_modpow_opt(uint32_t *x31, const unsigned char *e, size_t elen,
+ const uint32_t *m31, uint32_t m0i31, uint64_t *tmp, size_t twlen);
+
+/*
+ * Type for a function with the same API as br_i31_modpow_opt() (some
+ * implementations of this type may have stricter alignment requirements
+ * on the temporaries).
+ */
+typedef uint32_t (*br_i31_modpow_opt_type)(uint32_t *x,
+ const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
+
+/*
+ * Wrapper for br_i62_modpow_opt() that uses the same type as
+ * br_i31_modpow_opt(); however, it requires its 'tmp' argument to the
+ * 64-bit aligned.
+ */
+uint32_t br_i62_modpow_opt_as_i31(uint32_t *x,
+ const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
+
+/* ==================================================================== */
+
+static inline size_t
+br_digest_size(const br_hash_class *digest_class)
+{
+ return (size_t)(digest_class->desc >> BR_HASHDESC_OUT_OFF)
+ & BR_HASHDESC_OUT_MASK;
+}
+
+/*
+ * Get the output size (in bytes) of a hash function.
+ */
+size_t br_digest_size_by_ID(int digest_id);
+
+/*
+ * Get the OID (encoded OBJECT IDENTIFIER value, without tag and length)
+ * for a hash function. If digest_id is not a supported digest identifier
+ * (in particular if it is equal to 0, i.e. br_md5sha1_ID), then NULL is
+ * returned and *len is set to 0.
+ */
+const unsigned char *br_digest_OID(int digest_id, size_t *len);
+
+/* ==================================================================== */
+/*
+ * DES support functions.
+ */
+
+/*
+ * Apply DES Initial Permutation.
+ */
+void br_des_do_IP(uint32_t *xl, uint32_t *xr);
+
+/*
+ * Apply DES Final Permutation (inverse of IP).
+ */
+void br_des_do_invIP(uint32_t *xl, uint32_t *xr);
+
+/*
+ * Key schedule unit: for a DES key (8 bytes), compute 16 subkeys. Each
+ * subkey is two 28-bit words represented as two 32-bit words; the PC-2
+ * bit extration is NOT applied.
+ */
+void br_des_keysched_unit(uint32_t *skey, const void *key);
+
+/*
+ * Reversal of 16 DES sub-keys (for decryption).
+ */
+void br_des_rev_skey(uint32_t *skey);
+
+/*
+ * DES/3DES key schedule for 'des_tab' (encryption direction). Returned
+ * value is the number of rounds.
+ */
+unsigned br_des_tab_keysched(uint32_t *skey, const void *key, size_t key_len);
+
+/*
+ * DES/3DES key schedule for 'des_ct' (encryption direction). Returned
+ * value is the number of rounds.
+ */
+unsigned br_des_ct_keysched(uint32_t *skey, const void *key, size_t key_len);
+
+/*
+ * DES/3DES subkey decompression (from the compressed bitsliced subkeys).
+ */
+void br_des_ct_skey_expand(uint32_t *sk_exp,
+ unsigned num_rounds, const uint32_t *skey);
+
+/*
+ * DES/3DES block encryption/decryption ('des_tab').
+ */
+void br_des_tab_process_block(unsigned num_rounds,
+ const uint32_t *skey, void *block);
+
+/*
+ * DES/3DES block encryption/decryption ('des_ct').
+ */
+void br_des_ct_process_block(unsigned num_rounds,
+ const uint32_t *skey, void *block);
+
+/* ==================================================================== */
+/*
+ * AES support functions.
+ */
+
+/*
+ * The AES S-box (256-byte table).
+ */
+extern const unsigned char br_aes_S[];
+
+/*
+ * AES key schedule. skey[] is filled with n+1 128-bit subkeys, where n
+ * is the number of rounds (10 to 14, depending on key size). The number
+ * of rounds is returned. If the key size is invalid (not 16, 24 or 32),
+ * then 0 is returned.
+ *
+ * This implementation uses a 256-byte table and is NOT constant-time.
+ */
+unsigned br_aes_keysched(uint32_t *skey, const void *key, size_t key_len);
+
+/*
+ * AES key schedule for decryption ('aes_big' implementation).
+ */
+unsigned br_aes_big_keysched_inv(uint32_t *skey,
+ const void *key, size_t key_len);
+
+/*
+ * AES block encryption with the 'aes_big' implementation (fast, but
+ * not constant-time). This function encrypts a single block "in place".
+ */
+void br_aes_big_encrypt(unsigned num_rounds, const uint32_t *skey, void *data);
+
+/*
+ * AES block decryption with the 'aes_big' implementation (fast, but
+ * not constant-time). This function decrypts a single block "in place".
+ */
+void br_aes_big_decrypt(unsigned num_rounds, const uint32_t *skey, void *data);
+
+/*
+ * AES block encryption with the 'aes_small' implementation (small, but
+ * slow and not constant-time). This function encrypts a single block
+ * "in place".
+ */
+void br_aes_small_encrypt(unsigned num_rounds,
+ const uint32_t *skey, void *data);
+
+/*
+ * AES block decryption with the 'aes_small' implementation (small, but
+ * slow and not constant-time). This function decrypts a single block
+ * "in place".
+ */
+void br_aes_small_decrypt(unsigned num_rounds,
+ const uint32_t *skey, void *data);
+
+/*
+ * The constant-time implementation is "bitsliced": the 128-bit state is
+ * split over eight 32-bit words q* in the following way:
+ *
+ * -- Input block consists in 16 bytes:
+ * a00 a10 a20 a30 a01 a11 a21 a31 a02 a12 a22 a32 a03 a13 a23 a33
+ * In the terminology of FIPS 197, this is a 4x4 matrix which is read
+ * column by column.
+ *
+ * -- Each byte is split into eight bits which are distributed over the
+ * eight words, at the same rank. Thus, for a byte x at rank k, bit 0
+ * (least significant) of x will be at rank k in q0 (if that bit is b,
+ * then it contributes "b << k" to the value of q0), bit 1 of x will be
+ * at rank k in q1, and so on.
+ *
+ * -- Ranks given to bits are in "row order" and are either all even, or
+ * all odd. Two independent AES states are thus interleaved, one using
+ * the even ranks, the other the odd ranks. Row order means:
+ * a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33
+ *
+ * Converting input bytes from two AES blocks to bitslice representation
+ * is done in the following way:
+ * -- Decode first block into the four words q0 q2 q4 q6, in that order,
+ * using little-endian convention.
+ * -- Decode second block into the four words q1 q3 q5 q7, in that order,
+ * using little-endian convention.
+ * -- Call br_aes_ct_ortho().
+ *
+ * Converting back to bytes is done by using the reverse operations. Note
+ * that br_aes_ct_ortho() is its own inverse.
+ */
+
+/*
+ * Perform bytewise orthogonalization of eight 32-bit words. Bytes
+ * of q0..q7 are spread over all words: for a byte x that occurs
+ * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
+ * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
+ *
+ * This operation is an involution.
+ */
+void br_aes_ct_ortho(uint32_t *q);
+
+/*
+ * The AES S-box, as a bitsliced constant-time version. The input array
+ * consists in eight 32-bit words; 32 S-box instances are computed in
+ * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
+ * are spread over the words 0 to 7, at the same rank.
+ */
+void br_aes_ct_bitslice_Sbox(uint32_t *q);
+
+/*
+ * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
+ */
+void br_aes_ct_bitslice_invSbox(uint32_t *q);
+
+/*
+ * Compute AES encryption on bitsliced data. Since input is stored on
+ * eight 32-bit words, two block encryptions are actually performed
+ * in parallel.
+ */
+void br_aes_ct_bitslice_encrypt(unsigned num_rounds,
+ const uint32_t *skey, uint32_t *q);
+
+/*
+ * Compute AES decryption on bitsliced data. Since input is stored on
+ * eight 32-bit words, two block decryptions are actually performed
+ * in parallel.
+ */
+void br_aes_ct_bitslice_decrypt(unsigned num_rounds,
+ const uint32_t *skey, uint32_t *q);
+
+/*
+ * AES key schedule, constant-time version. skey[] is filled with n+1
+ * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
+ * on key size). The number of rounds is returned. If the key size is
+ * invalid (not 16, 24 or 32), then 0 is returned.
+ */
+unsigned br_aes_ct_keysched(uint32_t *comp_skey,
+ const void *key, size_t key_len);
+
+/*
+ * Expand AES subkeys as produced by br_aes_ct_keysched(), into
+ * a larger array suitable for br_aes_ct_bitslice_encrypt() and
+ * br_aes_ct_bitslice_decrypt().
+ */
+void br_aes_ct_skey_expand(uint32_t *skey,
+ unsigned num_rounds, const uint32_t *comp_skey);
+
+/*
+ * For the ct64 implementation, the same bitslicing technique is used,
+ * but four instances are interleaved. First instance uses bits 0, 4,
+ * 8, 12,... of each word; second instance uses bits 1, 5, 9, 13,...
+ * and so on.
+ */
+
+/*
+ * Perform bytewise orthogonalization of eight 64-bit words. Bytes
+ * of q0..q7 are spread over all words: for a byte x that occurs
+ * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
+ * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
+ *
+ * This operation is an involution.
+ */
+void br_aes_ct64_ortho(uint64_t *q);
+
+/*
+ * Interleave bytes for an AES input block. If input bytes are
+ * denoted 0123456789ABCDEF, and have been decoded with little-endian
+ * convention (w[0] contains 0123, with '3' being most significant;
+ * w[1] contains 4567, and so on), then output word q0 will be
+ * set to 08192A3B (again little-endian convention) and q1 will
+ * be set to 4C5D6E7F.
+ */
+void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w);
+
+/*
+ * Perform the opposite of br_aes_ct64_interleave_in().
+ */
+void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1);
+
+/*
+ * The AES S-box, as a bitsliced constant-time version. The input array
+ * consists in eight 64-bit words; 64 S-box instances are computed in
+ * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
+ * are spread over the words 0 to 7, at the same rank.
+ */
+void br_aes_ct64_bitslice_Sbox(uint64_t *q);
+
+/*
+ * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
+ */
+void br_aes_ct64_bitslice_invSbox(uint64_t *q);
+
+/*
+ * Compute AES encryption on bitsliced data. Since input is stored on
+ * eight 64-bit words, four block encryptions are actually performed
+ * in parallel.
+ */
+void br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
+ const uint64_t *skey, uint64_t *q);
+
+/*
+ * Compute AES decryption on bitsliced data. Since input is stored on
+ * eight 64-bit words, four block decryptions are actually performed
+ * in parallel.
+ */
+void br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
+ const uint64_t *skey, uint64_t *q);
+
+/*
+ * AES key schedule, constant-time version. skey[] is filled with n+1
+ * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
+ * on key size). The number of rounds is returned. If the key size is
+ * invalid (not 16, 24 or 32), then 0 is returned.
+ */
+unsigned br_aes_ct64_keysched(uint64_t *comp_skey,
+ const void *key, size_t key_len);
+
+/*
+ * Expand AES subkeys as produced by br_aes_ct64_keysched(), into
+ * a larger array suitable for br_aes_ct64_bitslice_encrypt() and
+ * br_aes_ct64_bitslice_decrypt().
+ */
+void br_aes_ct64_skey_expand(uint64_t *skey,
+ unsigned num_rounds, const uint64_t *comp_skey);
+
+/*
+ * Test support for AES-NI opcodes.
+ */
+int br_aes_x86ni_supported(void);
+
+/*
+ * AES key schedule, using x86 AES-NI instructions. This yields the
+ * subkeys in the encryption direction. Number of rounds is returned.
+ * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
+ */
+unsigned br_aes_x86ni_keysched_enc(unsigned char *skni,
+ const void *key, size_t len);
+
+/*
+ * AES key schedule, using x86 AES-NI instructions. This yields the
+ * subkeys in the decryption direction. Number of rounds is returned.
+ * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
+ */
+unsigned br_aes_x86ni_keysched_dec(unsigned char *skni,
+ const void *key, size_t len);
+
+/*
+ * Test support for AES POWER8 opcodes.
+ */
+int br_aes_pwr8_supported(void);
+
+/*
+ * AES key schedule, using POWER8 instructions. This yields the
+ * subkeys in the encryption direction. Number of rounds is returned.
+ * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
+ */
+unsigned br_aes_pwr8_keysched(unsigned char *skni,
+ const void *key, size_t len);
+
+/* ==================================================================== */
+/*
+ * RSA.
+ */
+
+/*
+ * Apply proper PKCS#1 v1.5 padding (for signatures). 'hash_oid' is
+ * the encoded hash function OID, or NULL.
+ */
+uint32_t br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ uint32_t n_bitlen, unsigned char *x);
+
+/*
+ * Check PKCS#1 v1.5 padding (for signatures). 'hash_oid' is the encoded
+ * hash function OID, or NULL. The provided 'sig' value is _after_ the
+ * modular exponentiation, i.e. it should be the padded hash. On
+ * success, the hashed message is extracted.
+ */
+uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
+ const unsigned char *hash_oid, size_t hash_len,
+ unsigned char *hash_out);
+
+/*
+ * Apply proper PSS padding. The 'x' buffer is output only: it
+ * receives the value that is to be exponentiated.
+ */
+uint32_t br_rsa_pss_sig_pad(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ uint32_t n_bitlen, unsigned char *x);
+
+/*
+ * Check PSS padding. The provided value is the one _after_
+ * the modular exponentiation; it is modified by this function.
+ * This function infers the signature length from the public key
+ * size, i.e. it assumes that this has already been verified (as
+ * part of the exponentiation).
+ */
+uint32_t br_rsa_pss_sig_unpad(
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_public_key *pk, unsigned char *x);
+
+/*
+ * Apply OAEP padding. Returned value is the actual padded string length,
+ * or zero on error.
+ */
+size_t br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len, const br_rsa_public_key *pk,
+ void *dst, size_t dst_nax_len, const void *src, size_t src_len);
+
+/*
+ * Unravel and check OAEP padding. If the padding is correct, then 1 is
+ * returned, '*len' is adjusted to the length of the message, and the
+ * data is moved to the start of the 'data' buffer. If the padding is
+ * incorrect, then 0 is returned and '*len' is untouched. Either way,
+ * the complete buffer contents are altered.
+ */
+uint32_t br_rsa_oaep_unpad(const br_hash_class *dig,
+ const void *label, size_t label_len, void *data, size_t *len);
+
+/*
+ * Compute MGF1 for a given seed, and XOR the output into the provided
+ * buffer.
+ */
+void br_mgf1_xor(void *data, size_t len,
+ const br_hash_class *dig, const void *seed, size_t seed_len);
+
+/*
+ * Inner function for RSA key generation; used by the "i31" and "i62"
+ * implementations.
+ */
+uint32_t br_rsa_i31_keygen_inner(const br_prng_class **rng,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp, br_i31_modpow_opt_type mp31);
+
+/* ==================================================================== */
+/*
+ * Elliptic curves.
+ */
+
+/*
+ * Type for generic EC parameters: curve order (unsigned big-endian
+ * encoding) and encoded conventional generator.
+ */
+typedef struct {
+ int curve;
+ const unsigned char *order;
+ size_t order_len;
+ const unsigned char *generator;
+ size_t generator_len;
+} br_ec_curve_def;
+
+extern const br_ec_curve_def br_secp256r1;
+extern const br_ec_curve_def br_secp384r1;
+extern const br_ec_curve_def br_secp521r1;
+
+/*
+ * For Curve25519, the advertised "order" really is 2^255-1, since the
+ * point multipliction function really works over arbitrary 255-bit
+ * scalars. This value is only meant as a hint for ECDH key generation;
+ * only ECDSA uses the exact curve order, and ECDSA is not used with
+ * that specific curve.
+ */
+extern const br_ec_curve_def br_curve25519;
+
+/*
+ * Decode some bytes as an i31 integer, with truncation (corresponding
+ * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
+ * length is provided as last parameter. The resulting value will have
+ * this declared bit length, and consists the big-endian unsigned decoding
+ * of exactly that many bits in the source (capped at the source length).
+ */
+void br_ecdsa_i31_bits2int(uint32_t *x,
+ const void *src, size_t len, uint32_t ebitlen);
+
+/*
+ * Decode some bytes as an i15 integer, with truncation (corresponding
+ * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
+ * length is provided as last parameter. The resulting value will have
+ * this declared bit length, and consists the big-endian unsigned decoding
+ * of exactly that many bits in the source (capped at the source length).
+ */
+void br_ecdsa_i15_bits2int(uint16_t *x,
+ const void *src, size_t len, uint32_t ebitlen);
+
+/* ==================================================================== */
+/*
+ * ASN.1 support functions.
+ */
+
+/*
+ * A br_asn1_uint structure contains encoding information about an
+ * INTEGER nonnegative value: pointer to the integer contents (unsigned
+ * big-endian representation), length of the integer contents,
+ * and length of the encoded value. The data shall have minimal length:
+ * - If the integer value is zero, then 'len' must be zero.
+ * - If the integer value is not zero, then data[0] must be non-zero.
+ *
+ * Under these conditions, 'asn1len' is necessarily equal to either len
+ * or len+1.
+ */
+typedef struct {
+ const unsigned char *data;
+ size_t len;
+ size_t asn1len;
+} br_asn1_uint;
+
+/*
+ * Given an encoded integer (unsigned big-endian, with possible leading
+ * bytes of value 0), returned the "prepared INTEGER" structure.
+ */
+br_asn1_uint br_asn1_uint_prepare(const void *xdata, size_t xlen);
+
+/*
+ * Encode an ASN.1 length. The length of the encoded length is returned.
+ * If 'dest' is NULL, then no encoding is performed, but the length of
+ * the encoded length is still computed and returned.
+ */
+size_t br_asn1_encode_length(void *dest, size_t len);
+
+/*
+ * Convenient macro for computing lengths of lengths.
+ */
+#define len_of_len(len) br_asn1_encode_length(NULL, len)
+
+/*
+ * Encode a (prepared) ASN.1 INTEGER. The encoded length is returned.
+ * If 'dest' is NULL, then no encoding is performed, but the length of
+ * the encoded integer is still computed and returned.
+ */
+size_t br_asn1_encode_uint(void *dest, br_asn1_uint pp);
+
+/*
+ * Get the OID that identifies an elliptic curve. Returned value is
+ * the DER-encoded OID, with the length (always one byte) but without
+ * the tag. Thus, the first byte of the returned buffer contains the
+ * number of subsequent bytes in the value. If the curve is not
+ * recognised, NULL is returned.
+ */
+const unsigned char *br_get_curve_OID(int curve);
+
+/*
+ * Inner function for EC private key encoding. This is equivalent to
+ * the API function br_encode_ec_raw_der(), except for an extra
+ * parameter: if 'include_curve_oid' is zero, then the curve OID is
+ * _not_ included in the output blob (this is for PKCS#8 support).
+ */
+size_t br_encode_ec_raw_der_inner(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk,
+ int include_curve_oid);
+
+/* ==================================================================== */
+/*
+ * SSL/TLS support functions.
+ */
+
+/*
+ * Record types.
+ */
+#define BR_SSL_CHANGE_CIPHER_SPEC 20
+#define BR_SSL_ALERT 21
+#define BR_SSL_HANDSHAKE 22
+#define BR_SSL_APPLICATION_DATA 23
+
+/*
+ * Handshake message types.
+ */
+#define BR_SSL_HELLO_REQUEST 0
+#define BR_SSL_CLIENT_HELLO 1
+#define BR_SSL_SERVER_HELLO 2
+#define BR_SSL_CERTIFICATE 11
+#define BR_SSL_SERVER_KEY_EXCHANGE 12
+#define BR_SSL_CERTIFICATE_REQUEST 13
+#define BR_SSL_SERVER_HELLO_DONE 14
+#define BR_SSL_CERTIFICATE_VERIFY 15
+#define BR_SSL_CLIENT_KEY_EXCHANGE 16
+#define BR_SSL_FINISHED 20
+
+/*
+ * Alert levels.
+ */
+#define BR_LEVEL_WARNING 1
+#define BR_LEVEL_FATAL 2
+
+/*
+ * Low-level I/O state.
+ */
+#define BR_IO_FAILED 0
+#define BR_IO_IN 1
+#define BR_IO_OUT 2
+#define BR_IO_INOUT 3
+
+/*
+ * Mark a SSL engine as failed. The provided error code is recorded if
+ * the engine was not already marked as failed. If 'err' is 0, then the
+ * engine is marked as closed (without error).
+ */
+void br_ssl_engine_fail(br_ssl_engine_context *cc, int err);
+
+/*
+ * Test whether the engine is closed (normally or as a failure).
+ */
+static inline int
+br_ssl_engine_closed(const br_ssl_engine_context *cc)
+{
+ return cc->iomode == BR_IO_FAILED;
+}
+
+/*
+ * Configure a new maximum fragment length. If possible, the maximum
+ * length for outgoing records is immediately adjusted (if there are
+ * not already too many buffered bytes for that).
+ */
+void br_ssl_engine_new_max_frag_len(
+ br_ssl_engine_context *rc, unsigned max_frag_len);
+
+/*
+ * Test whether the current incoming record has been fully received
+ * or not. This functions returns 0 only if a complete record header
+ * has been received, but some of the (possibly encrypted) payload
+ * has not yet been obtained.
+ */
+int br_ssl_engine_recvrec_finished(const br_ssl_engine_context *rc);
+
+/*
+ * Flush the current record (if not empty). This is meant to be called
+ * from the handshake processor only.
+ */
+void br_ssl_engine_flush_record(br_ssl_engine_context *cc);
+
+/*
+ * Test whether there is some accumulated payload to send.
+ */
+static inline int
+br_ssl_engine_has_pld_to_send(const br_ssl_engine_context *rc)
+{
+ return rc->oxa != rc->oxb && rc->oxa != rc->oxc;
+}
+
+/*
+ * Initialize RNG in engine. Returned value is 1 on success, 0 on error.
+ * This function will try to use the OS-provided RNG, if available. If
+ * there is no OS-provided RNG, or if it failed, and no entropy was
+ * injected by the caller, then a failure will be reported. On error,
+ * the context error code is set.
+ */
+int br_ssl_engine_init_rand(br_ssl_engine_context *cc);
+
+/*
+ * Reset the handshake-related parts of the engine.
+ */
+void br_ssl_engine_hs_reset(br_ssl_engine_context *cc,
+ void (*hsinit)(void *), void (*hsrun)(void *));
+
+/*
+ * Get the PRF to use for this context, for the provided PRF hash
+ * function ID.
+ */
+br_tls_prf_impl br_ssl_engine_get_PRF(br_ssl_engine_context *cc, int prf_id);
+
+/*
+ * Consume the provided pre-master secret and compute the corresponding
+ * master secret. The 'prf_id' is the ID of the hash function to use
+ * with the TLS 1.2 PRF (ignored if the version is TLS 1.0 or 1.1).
+ */
+void br_ssl_engine_compute_master(br_ssl_engine_context *cc,
+ int prf_id, const void *pms, size_t len);
+
+/*
+ * Switch to CBC decryption for incoming records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
+ * mac_id id of hash function for HMAC
+ * bc_impl block cipher implementation (CBC decryption)
+ * cipher_key_len block cipher key length (in bytes)
+ */
+void br_ssl_engine_switch_cbc_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id, int mac_id,
+ const br_block_cbcdec_class *bc_impl, size_t cipher_key_len);
+
+/*
+ * Switch to CBC encryption for outgoing records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
+ * mac_id id of hash function for HMAC
+ * bc_impl block cipher implementation (CBC encryption)
+ * cipher_key_len block cipher key length (in bytes)
+ */
+void br_ssl_engine_switch_cbc_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id, int mac_id,
+ const br_block_cbcenc_class *bc_impl, size_t cipher_key_len);
+
+/*
+ * Switch to GCM decryption for incoming records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ * bc_impl block cipher implementation (CTR)
+ * cipher_key_len block cipher key length (in bytes)
+ */
+void br_ssl_engine_switch_gcm_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctr_class *bc_impl, size_t cipher_key_len);
+
+/*
+ * Switch to GCM encryption for outgoing records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ * bc_impl block cipher implementation (CTR)
+ * cipher_key_len block cipher key length (in bytes)
+ */
+void br_ssl_engine_switch_gcm_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctr_class *bc_impl, size_t cipher_key_len);
+
+/*
+ * Switch to ChaCha20+Poly1305 decryption for incoming records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ */
+void br_ssl_engine_switch_chapol_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id);
+
+/*
+ * Switch to ChaCha20+Poly1305 encryption for outgoing records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ */
+void br_ssl_engine_switch_chapol_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id);
+
+/*
+ * Switch to CCM decryption for incoming records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ * bc_impl block cipher implementation (CTR+CBC)
+ * cipher_key_len block cipher key length (in bytes)
+ * tag_len tag length (in bytes)
+ */
+void br_ssl_engine_switch_ccm_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctrcbc_class *bc_impl,
+ size_t cipher_key_len, size_t tag_len);
+
+/*
+ * Switch to GCM encryption for outgoing records.
+ * cc the engine context
+ * is_client non-zero for a client, zero for a server
+ * prf_id id of hash function for PRF
+ * bc_impl block cipher implementation (CTR+CBC)
+ * cipher_key_len block cipher key length (in bytes)
+ * tag_len tag length (in bytes)
+ */
+void br_ssl_engine_switch_ccm_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctrcbc_class *bc_impl,
+ size_t cipher_key_len, size_t tag_len);
+
+/*
+ * Calls to T0-generated code.
+ */
+void br_ssl_hs_client_init_main(void *ctx);
+void br_ssl_hs_client_run(void *ctx);
+void br_ssl_hs_server_init_main(void *ctx);
+void br_ssl_hs_server_run(void *ctx);
+
+/*
+ * Get the hash function to use for signatures, given a bit mask of
+ * supported hash functions. This implements a strict choice order
+ * (namely SHA-256, SHA-384, SHA-512, SHA-224, SHA-1). If the mask
+ * does not document support of any of these hash functions, then this
+ * functions returns 0.
+ */
+int br_ssl_choose_hash(unsigned bf);
+
+/* ==================================================================== */
+
+/*
+ * PowerPC / POWER assembly stuff. The special BR_POWER_ASM_MACROS macro
+ * must be defined before including this file; this is done by source
+ * files that use some inline assembly for PowerPC / POWER machines.
+ */
+
+#if BR_POWER_ASM_MACROS
+
+#define lxvw4x(xt, ra, rb) lxvw4x_(xt, ra, rb)
+#define stxvw4x(xt, ra, rb) stxvw4x_(xt, ra, rb)
+
+#define bdnz(foo) bdnz_(foo)
+#define bdz(foo) bdz_(foo)
+#define beq(foo) beq_(foo)
+
+#define li(rx, value) li_(rx, value)
+#define addi(rx, ra, imm) addi_(rx, ra, imm)
+#define cmpldi(rx, imm) cmpldi_(rx, imm)
+#define mtctr(rx) mtctr_(rx)
+#define vspltb(vrt, vrb, uim) vspltb_(vrt, vrb, uim)
+#define vspltw(vrt, vrb, uim) vspltw_(vrt, vrb, uim)
+#define vspltisb(vrt, imm) vspltisb_(vrt, imm)
+#define vspltisw(vrt, imm) vspltisw_(vrt, imm)
+#define vrlw(vrt, vra, vrb) vrlw_(vrt, vra, vrb)
+#define vsbox(vrt, vra) vsbox_(vrt, vra)
+#define vxor(vrt, vra, vrb) vxor_(vrt, vra, vrb)
+#define vand(vrt, vra, vrb) vand_(vrt, vra, vrb)
+#define vsro(vrt, vra, vrb) vsro_(vrt, vra, vrb)
+#define vsl(vrt, vra, vrb) vsl_(vrt, vra, vrb)
+#define vsldoi(vt, va, vb, sh) vsldoi_(vt, va, vb, sh)
+#define vsr(vrt, vra, vrb) vsr_(vrt, vra, vrb)
+#define vaddcuw(vrt, vra, vrb) vaddcuw_(vrt, vra, vrb)
+#define vadduwm(vrt, vra, vrb) vadduwm_(vrt, vra, vrb)
+#define vsububm(vrt, vra, vrb) vsububm_(vrt, vra, vrb)
+#define vsubuwm(vrt, vra, vrb) vsubuwm_(vrt, vra, vrb)
+#define vsrw(vrt, vra, vrb) vsrw_(vrt, vra, vrb)
+#define vcipher(vt, va, vb) vcipher_(vt, va, vb)
+#define vcipherlast(vt, va, vb) vcipherlast_(vt, va, vb)
+#define vncipher(vt, va, vb) vncipher_(vt, va, vb)
+#define vncipherlast(vt, va, vb) vncipherlast_(vt, va, vb)
+#define vperm(vt, va, vb, vc) vperm_(vt, va, vb, vc)
+#define vpmsumd(vt, va, vb) vpmsumd_(vt, va, vb)
+#define xxpermdi(vt, va, vb, d) xxpermdi_(vt, va, vb, d)
+
+#define lxvw4x_(xt, ra, rb) "\tlxvw4x\t" #xt "," #ra "," #rb "\n"
+#define stxvw4x_(xt, ra, rb) "\tstxvw4x\t" #xt "," #ra "," #rb "\n"
+
+#define label(foo) #foo "%=:\n"
+#define bdnz_(foo) "\tbdnz\t" #foo "%=\n"
+#define bdz_(foo) "\tbdz\t" #foo "%=\n"
+#define beq_(foo) "\tbeq\t" #foo "%=\n"
+
+#define li_(rx, value) "\tli\t" #rx "," #value "\n"
+#define addi_(rx, ra, imm) "\taddi\t" #rx "," #ra "," #imm "\n"
+#define cmpldi_(rx, imm) "\tcmpldi\t" #rx "," #imm "\n"
+#define mtctr_(rx) "\tmtctr\t" #rx "\n"
+#define vspltb_(vrt, vrb, uim) "\tvspltb\t" #vrt "," #vrb "," #uim "\n"
+#define vspltw_(vrt, vrb, uim) "\tvspltw\t" #vrt "," #vrb "," #uim "\n"
+#define vspltisb_(vrt, imm) "\tvspltisb\t" #vrt "," #imm "\n"
+#define vspltisw_(vrt, imm) "\tvspltisw\t" #vrt "," #imm "\n"
+#define vrlw_(vrt, vra, vrb) "\tvrlw\t" #vrt "," #vra "," #vrb "\n"
+#define vsbox_(vrt, vra) "\tvsbox\t" #vrt "," #vra "\n"
+#define vxor_(vrt, vra, vrb) "\tvxor\t" #vrt "," #vra "," #vrb "\n"
+#define vand_(vrt, vra, vrb) "\tvand\t" #vrt "," #vra "," #vrb "\n"
+#define vsro_(vrt, vra, vrb) "\tvsro\t" #vrt "," #vra "," #vrb "\n"
+#define vsl_(vrt, vra, vrb) "\tvsl\t" #vrt "," #vra "," #vrb "\n"
+#define vsldoi_(vt, va, vb, sh) "\tvsldoi\t" #vt "," #va "," #vb "," #sh "\n"
+#define vsr_(vrt, vra, vrb) "\tvsr\t" #vrt "," #vra "," #vrb "\n"
+#define vaddcuw_(vrt, vra, vrb) "\tvaddcuw\t" #vrt "," #vra "," #vrb "\n"
+#define vadduwm_(vrt, vra, vrb) "\tvadduwm\t" #vrt "," #vra "," #vrb "\n"
+#define vsububm_(vrt, vra, vrb) "\tvsububm\t" #vrt "," #vra "," #vrb "\n"
+#define vsubuwm_(vrt, vra, vrb) "\tvsubuwm\t" #vrt "," #vra "," #vrb "\n"
+#define vsrw_(vrt, vra, vrb) "\tvsrw\t" #vrt "," #vra "," #vrb "\n"
+#define vcipher_(vt, va, vb) "\tvcipher\t" #vt "," #va "," #vb "\n"
+#define vcipherlast_(vt, va, vb) "\tvcipherlast\t" #vt "," #va "," #vb "\n"
+#define vncipher_(vt, va, vb) "\tvncipher\t" #vt "," #va "," #vb "\n"
+#define vncipherlast_(vt, va, vb) "\tvncipherlast\t" #vt "," #va "," #vb "\n"
+#define vperm_(vt, va, vb, vc) "\tvperm\t" #vt "," #va "," #vb "," #vc "\n"
+#define vpmsumd_(vt, va, vb) "\tvpmsumd\t" #vt "," #va "," #vb "\n"
+#define xxpermdi_(vt, va, vb, d) "\txxpermdi\t" #vt "," #va "," #vb "," #d "\n"
+
+#endif
+
+/* ==================================================================== */
+/*
+ * Special "activate intrinsics" code, needed for some compiler versions.
+ * This is defined at the end of this file, so that it won't impact any
+ * of the inline functions defined previously; and it is controlled by
+ * a specific macro defined in the caller code.
+ *
+ * Calling code conventions:
+ *
+ * - Caller must define BR_ENABLE_INTRINSICS before including "inner.h".
+ * - Functions that use intrinsics must be enclosed in an "enabled"
+ * region (between BR_TARGETS_X86_UP and BR_TARGETS_X86_DOWN).
+ * - Functions that use intrinsics must be tagged with the appropriate
+ * BR_TARGET().
+ */
+
+#if BR_ENABLE_INTRINSICS && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
+
+/*
+ * x86 intrinsics (both 32-bit and 64-bit).
+ */
+#if BR_i386 || BR_amd64
+
+/*
+ * On GCC before version 5.0, we need to use the pragma to enable the
+ * target options globally, because the 'target' function attribute
+ * appears to be unreliable. Before 4.6 we must also avoid the
+ * push_options / pop_options mechanism, because it tends to trigger
+ * some internal compiler errors.
+ */
+#if BR_GCC && !BR_GCC_5_0
+#if BR_GCC_4_6
+#define BR_TARGETS_X86_UP \
+ _Pragma("GCC push_options") \
+ _Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul,rdrnd\")")
+#define BR_TARGETS_X86_DOWN \
+ _Pragma("GCC pop_options")
+#else
+#define BR_TARGETS_X86_UP \
+ _Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul\")")
+#define BR_TARGETS_X86_DOWN
+#endif
+#pragma GCC diagnostic ignored "-Wpsabi"
+#endif
+
+#if BR_CLANG && !BR_CLANG_3_8
+#undef __SSE2__
+#undef __SSE3__
+#undef __SSSE3__
+#undef __SSE4_1__
+#undef __AES__
+#undef __PCLMUL__
+#undef __RDRND__
+#define __SSE2__ 1
+#define __SSE3__ 1
+#define __SSSE3__ 1
+#define __SSE4_1__ 1
+#define __AES__ 1
+#define __PCLMUL__ 1
+#define __RDRND__ 1
+#endif
+
+#ifndef BR_TARGETS_X86_UP
+#define BR_TARGETS_X86_UP
+#endif
+#ifndef BR_TARGETS_X86_DOWN
+#define BR_TARGETS_X86_DOWN
+#endif
+
+#if BR_GCC || BR_CLANG
+BR_TARGETS_X86_UP
+#include <x86intrin.h>
+#include <cpuid.h>
+#define br_bswap32 __builtin_bswap32
+BR_TARGETS_X86_DOWN
+#endif
+
+#if BR_MSC
+#include <stdlib.h>
+#include <intrin.h>
+#include <immintrin.h>
+#define br_bswap32 _byteswap_ulong
+#endif
+
+static inline int
+br_cpuid(uint32_t mask_eax, uint32_t mask_ebx,
+ uint32_t mask_ecx, uint32_t mask_edx)
+{
+#if BR_GCC || BR_CLANG
+ unsigned eax, ebx, ecx, edx;
+
+ if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
+ if ((eax & mask_eax) == mask_eax
+ && (ebx & mask_ebx) == mask_ebx
+ && (ecx & mask_ecx) == mask_ecx
+ && (edx & mask_edx) == mask_edx)
+ {
+ return 1;
+ }
+ }
+#elif BR_MSC
+ int info[4];
+
+ __cpuid(info, 1);
+ if (((uint32_t)info[0] & mask_eax) == mask_eax
+ && ((uint32_t)info[1] & mask_ebx) == mask_ebx
+ && ((uint32_t)info[2] & mask_ecx) == mask_ecx
+ && ((uint32_t)info[3] & mask_edx) == mask_edx)
+ {
+ return 1;
+ }
+#endif
+ return 0;
+}
+
+#endif
+
+#endif
+
+/* ==================================================================== */
+
+#endif
diff --git a/test/monniaux/BearSSL/src/int/i15_add.c b/test/monniaux/BearSSL/src/int/i15_add.c
new file mode 100644
index 00000000..97e29b82
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_add.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_add(uint16_t *a, const uint16_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 31) >> 4;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw + bw + cc;
+ cc = naw >> 15;
+ a[u] = MUX(ctl, naw & 0x7FFF, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_bitlen.c b/test/monniaux/BearSSL/src/int/i15_bitlen.c
new file mode 100644
index 00000000..ad744671
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_bitlen.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_bit_length(uint16_t *x, size_t xlen)
+{
+ uint32_t tw, twk;
+
+ tw = 0;
+ twk = 0;
+ while (xlen -- > 0) {
+ uint32_t w, c;
+
+ c = EQ(tw, 0);
+ w = x[xlen];
+ tw = MUX(c, w, tw);
+ twk = MUX(c, (uint32_t)xlen, twk);
+ }
+ return (twk << 4) + BIT_LENGTH(tw);
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_decmod.c b/test/monniaux/BearSSL/src/int/i15_decmod.c
new file mode 100644
index 00000000..6076c57b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_decmod.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_decode_mod(uint16_t *x, const void *src, size_t len, const uint16_t *m)
+{
+ /*
+ * Two-pass algorithm: in the first pass, we determine whether the
+ * value fits; in the second pass, we do the actual write.
+ *
+ * During the first pass, 'r' contains the comparison result so
+ * far:
+ * 0x00000000 value is equal to the modulus
+ * 0x00000001 value is greater than the modulus
+ * 0xFFFFFFFF value is lower than the modulus
+ *
+ * Since we iterate starting with the least significant bytes (at
+ * the end of src[]), each new comparison overrides the previous
+ * except when the comparison yields 0 (equal).
+ *
+ * During the second pass, 'r' is either 0xFFFFFFFF (value fits)
+ * or 0x00000000 (value does not fit).
+ *
+ * We must iterate over all bytes of the source, _and_ possibly
+ * some extra virtual bytes (with value 0) so as to cover the
+ * complete modulus as well. We also add 4 such extra bytes beyond
+ * the modulus length because it then guarantees that no accumulated
+ * partial word remains to be processed.
+ */
+ const unsigned char *buf;
+ size_t mlen, tlen;
+ int pass;
+ uint32_t r;
+
+ buf = src;
+ mlen = (m[0] + 15) >> 4;
+ tlen = (mlen << 1);
+ if (tlen < len) {
+ tlen = len;
+ }
+ tlen += 4;
+ r = 0;
+ for (pass = 0; pass < 2; pass ++) {
+ size_t u, v;
+ uint32_t acc;
+ int acc_len;
+
+ v = 1;
+ acc = 0;
+ acc_len = 0;
+ for (u = 0; u < tlen; u ++) {
+ uint32_t b;
+
+ if (u < len) {
+ b = buf[len - 1 - u];
+ } else {
+ b = 0;
+ }
+ acc |= (b << acc_len);
+ acc_len += 8;
+ if (acc_len >= 15) {
+ uint32_t xw;
+
+ xw = acc & (uint32_t)0x7FFF;
+ acc_len -= 15;
+ acc = b >> (8 - acc_len);
+ if (v <= mlen) {
+ if (pass) {
+ x[v] = r & xw;
+ } else {
+ uint32_t cc;
+
+ cc = (uint32_t)CMP(xw, m[v]);
+ r = MUX(EQ(cc, 0), r, cc);
+ }
+ } else {
+ if (!pass) {
+ r = MUX(EQ(xw, 0), r, 1);
+ }
+ }
+ v ++;
+ }
+ }
+
+ /*
+ * When we reach this point at the end of the first pass:
+ * r is either 0, 1 or -1; we want to set r to 0 if it
+ * is equal to 0 or 1, and leave it to -1 otherwise.
+ *
+ * When we reach this point at the end of the second pass:
+ * r is either 0 or -1; we want to leave that value
+ * untouched. This is a subcase of the previous.
+ */
+ r >>= 1;
+ r |= (r << 1);
+ }
+
+ x[0] = m[0];
+ return r & (uint32_t)1;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_decode.c b/test/monniaux/BearSSL/src/int/i15_decode.c
new file mode 100644
index 00000000..fc2c0be0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_decode.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_decode(uint16_t *x, const void *src, size_t len)
+{
+ const unsigned char *buf;
+ size_t v;
+ uint32_t acc;
+ int acc_len;
+
+ buf = src;
+ v = 1;
+ acc = 0;
+ acc_len = 0;
+ while (len -- > 0) {
+ uint32_t b;
+
+ b = buf[len];
+ acc |= (b << acc_len);
+ acc_len += 8;
+ if (acc_len >= 15) {
+ x[v ++] = acc & 0x7FFF;
+ acc_len -= 15;
+ acc >>= 15;
+ }
+ }
+ if (acc_len != 0) {
+ x[v ++] = acc;
+ }
+ x[0] = br_i15_bit_length(x + 1, v - 1);
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_decred.c b/test/monniaux/BearSSL/src/int/i15_decred.c
new file mode 100644
index 00000000..81e7dd1b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_decred.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_decode_reduce(uint16_t *x,
+ const void *src, size_t len, const uint16_t *m)
+{
+ uint32_t m_ebitlen, m_rbitlen;
+ size_t mblen, k;
+ const unsigned char *buf;
+ uint32_t acc;
+ int acc_len;
+
+ /*
+ * Get the encoded bit length.
+ */
+ m_ebitlen = m[0];
+
+ /*
+ * Special case for an invalid (null) modulus.
+ */
+ if (m_ebitlen == 0) {
+ x[0] = 0;
+ return;
+ }
+
+ /*
+ * Clear the destination.
+ */
+ br_i15_zero(x, m_ebitlen);
+
+ /*
+ * First decode directly as many bytes as possible. This requires
+ * computing the actual bit length.
+ */
+ m_rbitlen = m_ebitlen >> 4;
+ m_rbitlen = (m_ebitlen & 15) + (m_rbitlen << 4) - m_rbitlen;
+ mblen = (m_rbitlen + 7) >> 3;
+ k = mblen - 1;
+ if (k >= len) {
+ br_i15_decode(x, src, len);
+ x[0] = m_ebitlen;
+ return;
+ }
+ buf = src;
+ br_i15_decode(x, buf, k);
+ x[0] = m_ebitlen;
+
+ /*
+ * Input remaining bytes, using 15-bit words.
+ */
+ acc = 0;
+ acc_len = 0;
+ while (k < len) {
+ uint32_t v;
+
+ v = buf[k ++];
+ acc = (acc << 8) | v;
+ acc_len += 8;
+ if (acc_len >= 15) {
+ br_i15_muladd_small(x, acc >> (acc_len - 15), m);
+ acc_len -= 15;
+ acc &= ~((uint32_t)-1 << acc_len);
+ }
+ }
+
+ /*
+ * We may have some bits accumulated. We then perform a shift to
+ * be able to inject these bits as a full 15-bit word.
+ */
+ if (acc_len != 0) {
+ acc = (acc | (x[1] << acc_len)) & 0x7FFF;
+ br_i15_rshift(x, 15 - acc_len);
+ br_i15_muladd_small(x, acc, m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_encode.c b/test/monniaux/BearSSL/src/int/i15_encode.c
new file mode 100644
index 00000000..50668f47
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_encode.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_encode(void *dst, size_t len, const uint16_t *x)
+{
+ unsigned char *buf;
+ size_t u, xlen;
+ uint32_t acc;
+ int acc_len;
+
+ xlen = (x[0] + 15) >> 4;
+ if (xlen == 0) {
+ memset(dst, 0, len);
+ return;
+ }
+ u = 1;
+ acc = 0;
+ acc_len = 0;
+ buf = dst;
+ while (len -- > 0) {
+ if (acc_len < 8) {
+ if (u <= xlen) {
+ acc += (uint32_t)x[u ++] << acc_len;
+ }
+ acc_len += 15;
+ }
+ buf[len] = (unsigned char)acc;
+ acc >>= 8;
+ acc_len -= 8;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_fmont.c b/test/monniaux/BearSSL/src/int/i15_fmont.c
new file mode 100644
index 00000000..3450b723
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_fmont.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i)
+{
+ size_t len, u, v;
+
+ len = (m[0] + 15) >> 4;
+ for (u = 0; u < len; u ++) {
+ uint32_t f, cc;
+
+ f = MUL15(x[1], m0i) & 0x7FFF;
+ cc = 0;
+ for (v = 0; v < len; v ++) {
+ uint32_t z;
+
+ z = (uint32_t)x[v + 1] + MUL15(f, m[v + 1]) + cc;
+ cc = z >> 15;
+ if (v != 0) {
+ x[v] = z & 0x7FFF;
+ }
+ }
+ x[len] = cc;
+ }
+
+ /*
+ * We may have to do an extra subtraction, but only if the
+ * value in x[] is indeed greater than or equal to that of m[],
+ * which is why we must do two calls (first call computes the
+ * carry, second call performs the subtraction only if the carry
+ * is 0).
+ */
+ br_i15_sub(x, m, NOT(br_i15_sub(x, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_iszero.c b/test/monniaux/BearSSL/src/int/i15_iszero.c
new file mode 100644
index 00000000..d4b6f10b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_iszero.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_iszero(const uint16_t *x)
+{
+ uint32_t z;
+ size_t u;
+
+ z = 0;
+ for (u = (x[0] + 15) >> 4; u > 0; u --) {
+ z |= x[u];
+ }
+ return ~(z | -z) >> 31;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_moddiv.c b/test/monniaux/BearSSL/src/int/i15_moddiv.c
new file mode 100644
index 00000000..45af756d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_moddiv.c
@@ -0,0 +1,465 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * In this file, we handle big integers with a custom format, i.e.
+ * without the usual one-word header. Value is split into 15-bit words,
+ * each stored in a 16-bit slot (top bit is zero) in little-endian
+ * order. The length (in words) is provided explicitly. In some cases,
+ * the value can be negative (using two's complement representation). In
+ * some cases, the top word is allowed to have a 16th bit.
+ */
+
+/*
+ * Negate big integer conditionally. The value consists of 'len' words,
+ * with 15 bits in each word (the top bit of each word should be 0,
+ * except possibly for the last word). If 'ctl' is 1, the negation is
+ * computed; otherwise, if 'ctl' is 0, then the value is unchanged.
+ */
+static void
+cond_negate(uint16_t *a, size_t len, uint32_t ctl)
+{
+ size_t k;
+ uint32_t cc, xm;
+
+ cc = ctl;
+ xm = 0x7FFF & -ctl;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw;
+
+ aw = a[k];
+ aw = (aw ^ xm) + cc;
+ a[k] = aw & 0x7FFF;
+ cc = (aw >> 15) & 1;
+ }
+}
+
+/*
+ * Finish modular reduction. Rules on input parameters:
+ *
+ * if neg = 1, then -m <= a < 0
+ * if neg = 0, then 0 <= a < 2*m
+ *
+ * If neg = 0, then the top word of a[] may use 16 bits.
+ *
+ * Also, modulus m must be odd.
+ */
+static void
+finish_mod(uint16_t *a, size_t len, const uint16_t *m, uint32_t neg)
+{
+ size_t k;
+ uint32_t cc, xm, ym;
+
+ /*
+ * First pass: compare a (assumed nonnegative) with m.
+ */
+ cc = 0;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw, mw;
+
+ aw = a[k];
+ mw = m[k];
+ cc = (aw - mw - cc) >> 31;
+ }
+
+ /*
+ * At this point:
+ * if neg = 1, then we must add m (regardless of cc)
+ * if neg = 0 and cc = 0, then we must subtract m
+ * if neg = 0 and cc = 1, then we must do nothing
+ */
+ xm = 0x7FFF & -neg;
+ ym = -(neg | (1 - cc));
+ cc = neg;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw, mw;
+
+ aw = a[k];
+ mw = (m[k] ^ xm) & ym;
+ aw = aw - mw - cc;
+ a[k] = aw & 0x7FFF;
+ cc = aw >> 31;
+ }
+}
+
+/*
+ * Compute:
+ * a <- (a*pa+b*pb)/(2^15)
+ * b <- (a*qa+b*qb)/(2^15)
+ * The division is assumed to be exact (i.e. the low word is dropped).
+ * If the final a is negative, then it is negated. Similarly for b.
+ * Returned value is the combination of two bits:
+ * bit 0: 1 if a had to be negated, 0 otherwise
+ * bit 1: 1 if b had to be negated, 0 otherwise
+ *
+ * Factors pa, pb, qa and qb must be at most 2^15 in absolute value.
+ * Source integers a and b must be nonnegative; top word is not allowed
+ * to contain an extra 16th bit.
+ */
+static uint32_t
+co_reduce(uint16_t *a, uint16_t *b, size_t len,
+ int32_t pa, int32_t pb, int32_t qa, int32_t qb)
+{
+ size_t k;
+ int32_t cca, ccb;
+ uint32_t nega, negb;
+
+ cca = 0;
+ ccb = 0;
+ for (k = 0; k < len; k ++) {
+ uint32_t wa, wb, za, zb;
+ uint16_t tta, ttb;
+
+ /*
+ * Since:
+ * |pa| <= 2^15
+ * |pb| <= 2^15
+ * 0 <= wa <= 2^15 - 1
+ * 0 <= wb <= 2^15 - 1
+ * |cca| <= 2^16 - 1
+ * Then:
+ * |za| <= (2^15-1)*(2^16) + (2^16-1) = 2^31 - 1
+ *
+ * Thus, the new value of cca is such that |cca| <= 2^16 - 1.
+ * The same applies to ccb.
+ */
+ wa = a[k];
+ wb = b[k];
+ za = wa * (uint32_t)pa + wb * (uint32_t)pb + (uint32_t)cca;
+ zb = wa * (uint32_t)qa + wb * (uint32_t)qb + (uint32_t)ccb;
+ if (k > 0) {
+ a[k - 1] = za & 0x7FFF;
+ b[k - 1] = zb & 0x7FFF;
+ }
+ tta = za >> 15;
+ ttb = zb >> 15;
+ cca = *(int16_t *)&tta;
+ ccb = *(int16_t *)&ttb;
+ }
+ a[len - 1] = (uint16_t)cca;
+ b[len - 1] = (uint16_t)ccb;
+ nega = (uint32_t)cca >> 31;
+ negb = (uint32_t)ccb >> 31;
+ cond_negate(a, len, nega);
+ cond_negate(b, len, negb);
+ return nega | (negb << 1);
+}
+
+/*
+ * Compute:
+ * a <- (a*pa+b*pb)/(2^15) mod m
+ * b <- (a*qa+b*qb)/(2^15) mod m
+ *
+ * m0i is equal to -1/m[0] mod 2^15.
+ *
+ * Factors pa, pb, qa and qb must be at most 2^15 in absolute value.
+ * Source integers a and b must be nonnegative; top word is not allowed
+ * to contain an extra 16th bit.
+ */
+static void
+co_reduce_mod(uint16_t *a, uint16_t *b, size_t len,
+ int32_t pa, int32_t pb, int32_t qa, int32_t qb,
+ const uint16_t *m, uint16_t m0i)
+{
+ size_t k;
+ int32_t cca, ccb, fa, fb;
+
+ cca = 0;
+ ccb = 0;
+ fa = ((a[0] * (uint32_t)pa + b[0] * (uint32_t)pb) * m0i) & 0x7FFF;
+ fb = ((a[0] * (uint32_t)qa + b[0] * (uint32_t)qb) * m0i) & 0x7FFF;
+ for (k = 0; k < len; k ++) {
+ uint32_t wa, wb, za, zb;
+ uint32_t tta, ttb;
+
+ /*
+ * In this loop, carries 'cca' and 'ccb' always fit on
+ * 17 bits (in absolute value).
+ */
+ wa = a[k];
+ wb = b[k];
+ za = wa * (uint32_t)pa + wb * (uint32_t)pb
+ + m[k] * (uint32_t)fa + (uint32_t)cca;
+ zb = wa * (uint32_t)qa + wb * (uint32_t)qb
+ + m[k] * (uint32_t)fb + (uint32_t)ccb;
+ if (k > 0) {
+ a[k - 1] = za & 0x7FFF;
+ b[k - 1] = zb & 0x7FFF;
+ }
+
+ /*
+ * The XOR-and-sub construction below does an arithmetic
+ * right shift in a portable way (technically, right-shifting
+ * a negative signed value is implementation-defined in C).
+ */
+#define M ((uint32_t)1 << 16)
+ tta = za >> 15;
+ ttb = zb >> 15;
+ tta = (tta ^ M) - M;
+ ttb = (ttb ^ M) - M;
+ cca = *(int32_t *)&tta;
+ ccb = *(int32_t *)&ttb;
+#undef M
+ }
+ a[len - 1] = (uint32_t)cca;
+ b[len - 1] = (uint32_t)ccb;
+
+ /*
+ * At this point:
+ * -m <= a < 2*m
+ * -m <= b < 2*m
+ * (this is a case of Montgomery reduction)
+ * The top word of 'a' and 'b' may have a 16-th bit set.
+ * We may have to add or subtract the modulus.
+ */
+ finish_mod(a, len, m, (uint32_t)cca >> 31);
+ finish_mod(b, len, m, (uint32_t)ccb >> 31);
+}
+
+/* see inner.h */
+uint32_t
+br_i15_moddiv(uint16_t *x, const uint16_t *y, const uint16_t *m, uint16_t m0i,
+ uint16_t *t)
+{
+ /*
+ * Algorithm is an extended binary GCD. We maintain four values
+ * a, b, u and v, with the following invariants:
+ *
+ * a * x = y * u mod m
+ * b * x = y * v mod m
+ *
+ * Starting values are:
+ *
+ * a = y
+ * b = m
+ * u = x
+ * v = 0
+ *
+ * The formal definition of the algorithm is a sequence of steps:
+ *
+ * - If a is even, then a <- a/2 and u <- u/2 mod m.
+ * - Otherwise, if b is even, then b <- b/2 and v <- v/2 mod m.
+ * - Otherwise, if a > b, then a <- (a-b)/2 and u <- (u-v)/2 mod m.
+ * - Otherwise, b <- (b-a)/2 and v <- (v-u)/2 mod m.
+ *
+ * Algorithm stops when a = b. At that point, they both are equal
+ * to GCD(y,m); the modular division succeeds if that value is 1.
+ * The result of the modular division is then u (or v: both are
+ * equal at that point).
+ *
+ * Each step makes either a or b shrink by at least one bit; hence,
+ * if m has bit length k bits, then 2k-2 steps are sufficient.
+ *
+ *
+ * Though complexity is quadratic in the size of m, the bit-by-bit
+ * processing is not very efficient. We can speed up processing by
+ * remarking that the decisions are taken based only on observation
+ * of the top and low bits of a and b.
+ *
+ * In the loop below, at each iteration, we use the two top words
+ * of a and b, and the low words of a and b, to compute reduction
+ * parameters pa, pb, qa and qb such that the new values for a
+ * and b are:
+ *
+ * a' = (a*pa + b*pb) / (2^15)
+ * b' = (a*qa + b*qb) / (2^15)
+ *
+ * the division being exact.
+ *
+ * Since the choices are based on the top words, they may be slightly
+ * off, requiring an optional correction: if a' < 0, then we replace
+ * pa with -pa, and pb with -pb. The total length of a and b is
+ * thus reduced by at least 14 bits at each iteration.
+ *
+ * The stopping conditions are still the same, though: when a
+ * and b become equal, they must be both odd (since m is odd,
+ * the GCD cannot be even), therefore the next operation is a
+ * subtraction, and one of the values becomes 0. At that point,
+ * nothing else happens, i.e. one value is stuck at 0, and the
+ * other one is the GCD.
+ */
+ size_t len, k;
+ uint16_t *a, *b, *u, *v;
+ uint32_t num, r;
+
+ len = (m[0] + 15) >> 4;
+ a = t;
+ b = a + len;
+ u = x + 1;
+ v = b + len;
+ memcpy(a, y + 1, len * sizeof *y);
+ memcpy(b, m + 1, len * sizeof *m);
+ memset(v, 0, len * sizeof *v);
+
+ /*
+ * Loop below ensures that a and b are reduced by some bits each,
+ * for a total of at least 14 bits.
+ */
+ for (num = ((m[0] - (m[0] >> 4)) << 1) + 14; num >= 14; num -= 14) {
+ size_t j;
+ uint32_t c0, c1;
+ uint32_t a0, a1, b0, b1;
+ uint32_t a_hi, b_hi, a_lo, b_lo;
+ int32_t pa, pb, qa, qb;
+ int i;
+
+ /*
+ * Extract top words of a and b. If j is the highest
+ * index >= 1 such that a[j] != 0 or b[j] != 0, then we want
+ * (a[j] << 15) + a[j - 1], and (b[j] << 15) + b[j - 1].
+ * If a and b are down to one word each, then we use a[0]
+ * and b[0].
+ */
+ c0 = (uint32_t)-1;
+ c1 = (uint32_t)-1;
+ a0 = 0;
+ a1 = 0;
+ b0 = 0;
+ b1 = 0;
+ j = len;
+ while (j -- > 0) {
+ uint32_t aw, bw;
+
+ aw = a[j];
+ bw = b[j];
+ a0 ^= (a0 ^ aw) & c0;
+ a1 ^= (a1 ^ aw) & c1;
+ b0 ^= (b0 ^ bw) & c0;
+ b1 ^= (b1 ^ bw) & c1;
+ c1 = c0;
+ c0 &= (((aw | bw) + 0xFFFF) >> 16) - (uint32_t)1;
+ }
+
+ /*
+ * If c1 = 0, then we grabbed two words for a and b.
+ * If c1 != 0 but c0 = 0, then we grabbed one word. It
+ * is not possible that c1 != 0 and c0 != 0, because that
+ * would mean that both integers are zero.
+ */
+ a1 |= a0 & c1;
+ a0 &= ~c1;
+ b1 |= b0 & c1;
+ b0 &= ~c1;
+ a_hi = (a0 << 15) + a1;
+ b_hi = (b0 << 15) + b1;
+ a_lo = a[0];
+ b_lo = b[0];
+
+ /*
+ * Compute reduction factors:
+ *
+ * a' = a*pa + b*pb
+ * b' = a*qa + b*qb
+ *
+ * such that a' and b' are both multiple of 2^15, but are
+ * only marginally larger than a and b.
+ */
+ pa = 1;
+ pb = 0;
+ qa = 0;
+ qb = 1;
+ for (i = 0; i < 15; i ++) {
+ /*
+ * At each iteration:
+ *
+ * a <- (a-b)/2 if: a is odd, b is odd, a_hi > b_hi
+ * b <- (b-a)/2 if: a is odd, b is odd, a_hi <= b_hi
+ * a <- a/2 if: a is even
+ * b <- b/2 if: a is odd, b is even
+ *
+ * We multiply a_lo and b_lo by 2 at each
+ * iteration, thus a division by 2 really is a
+ * non-multiplication by 2.
+ */
+ uint32_t r, oa, ob, cAB, cBA, cA;
+
+ /*
+ * cAB = 1 if b must be subtracted from a
+ * cBA = 1 if a must be subtracted from b
+ * cA = 1 if a is divided by 2, 0 otherwise
+ *
+ * Rules:
+ *
+ * cAB and cBA cannot be both 1.
+ * if a is not divided by 2, b is.
+ */
+ r = GT(a_hi, b_hi);
+ oa = (a_lo >> i) & 1;
+ ob = (b_lo >> i) & 1;
+ cAB = oa & ob & r;
+ cBA = oa & ob & NOT(r);
+ cA = cAB | NOT(oa);
+
+ /*
+ * Conditional subtractions.
+ */
+ a_lo -= b_lo & -cAB;
+ a_hi -= b_hi & -cAB;
+ pa -= qa & -(int32_t)cAB;
+ pb -= qb & -(int32_t)cAB;
+ b_lo -= a_lo & -cBA;
+ b_hi -= a_hi & -cBA;
+ qa -= pa & -(int32_t)cBA;
+ qb -= pb & -(int32_t)cBA;
+
+ /*
+ * Shifting.
+ */
+ a_lo += a_lo & (cA - 1);
+ pa += pa & ((int32_t)cA - 1);
+ pb += pb & ((int32_t)cA - 1);
+ a_hi ^= (a_hi ^ (a_hi >> 1)) & -cA;
+ b_lo += b_lo & -cA;
+ qa += qa & -(int32_t)cA;
+ qb += qb & -(int32_t)cA;
+ b_hi ^= (b_hi ^ (b_hi >> 1)) & (cA - 1);
+ }
+
+ /*
+ * Replace a and b with new values a' and b'.
+ */
+ r = co_reduce(a, b, len, pa, pb, qa, qb);
+ pa -= pa * ((r & 1) << 1);
+ pb -= pb * ((r & 1) << 1);
+ qa -= qa * (r & 2);
+ qb -= qb * (r & 2);
+ co_reduce_mod(u, v, len, pa, pb, qa, qb, m + 1, m0i);
+ }
+
+ /*
+ * Now one of the arrays should be 0, and the other contains
+ * the GCD. If a is 0, then u is 0 as well, and v contains
+ * the division result.
+ * Result is correct if and only if GCD is 1.
+ */
+ r = (a[0] | b[0]) ^ 1;
+ u[0] |= v[0];
+ for (k = 1; k < len; k ++) {
+ r |= a[k] | b[k];
+ u[k] |= v[k];
+ }
+ return EQ0(r);
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_modpow.c b/test/monniaux/BearSSL/src/int/i15_modpow.c
new file mode 100644
index 00000000..9bf304e5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_modpow.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_modpow(uint16_t *x,
+ const unsigned char *e, size_t elen,
+ const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2)
+{
+ size_t mlen;
+ unsigned k;
+
+ mlen = ((m[0] + 31) >> 4) * sizeof m[0];
+ memcpy(t1, x, mlen);
+ br_i15_to_monty(t1, m);
+ br_i15_zero(x, m[0]);
+ x[1] = 1;
+ for (k = 0; k < ((unsigned)elen << 3); k ++) {
+ uint32_t ctl;
+
+ ctl = (e[elen - 1 - (k >> 3)] >> (k & 7)) & 1;
+ br_i15_montymul(t2, x, t1, m, m0i);
+ CCOPY(ctl, x, t2, mlen);
+ br_i15_montymul(t2, t1, t1, m, m0i);
+ memcpy(t1, t2, mlen);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_modpow2.c b/test/monniaux/BearSSL/src/int/i15_modpow2.c
new file mode 100644
index 00000000..4b321186
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_modpow2.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_modpow_opt(uint16_t *x,
+ const unsigned char *e, size_t elen,
+ const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen)
+{
+ size_t mlen, mwlen;
+ uint16_t *t1, *t2, *base;
+ size_t u, v;
+ uint32_t acc;
+ int acc_len, win_len;
+
+ /*
+ * Get modulus size.
+ */
+ mwlen = (m[0] + 31) >> 4;
+ mlen = mwlen * sizeof m[0];
+ mwlen += (mwlen & 1);
+ t1 = tmp;
+ t2 = tmp + mwlen;
+
+ /*
+ * Compute possible window size, with a maximum of 5 bits.
+ * When the window has size 1 bit, we use a specific code
+ * that requires only two temporaries. Otherwise, for a
+ * window of k bits, we need 2^k+1 temporaries.
+ */
+ if (twlen < (mwlen << 1)) {
+ return 0;
+ }
+ for (win_len = 5; win_len > 1; win_len --) {
+ if ((((uint32_t)1 << win_len) + 1) * mwlen <= twlen) {
+ break;
+ }
+ }
+
+ /*
+ * Everything is done in Montgomery representation.
+ */
+ br_i15_to_monty(x, m);
+
+ /*
+ * Compute window contents. If the window has size one bit only,
+ * then t2 is set to x; otherwise, t2[0] is left untouched, and
+ * t2[k] is set to x^k (for k >= 1).
+ */
+ if (win_len == 1) {
+ memcpy(t2, x, mlen);
+ } else {
+ memcpy(t2 + mwlen, x, mlen);
+ base = t2 + mwlen;
+ for (u = 2; u < ((unsigned)1 << win_len); u ++) {
+ br_i15_montymul(base + mwlen, base, x, m, m0i);
+ base += mwlen;
+ }
+ }
+
+ /*
+ * We need to set x to 1, in Montgomery representation. This can
+ * be done efficiently by setting the high word to 1, then doing
+ * one word-sized shift.
+ */
+ br_i15_zero(x, m[0]);
+ x[(m[0] + 15) >> 4] = 1;
+ br_i15_muladd_small(x, 0, m);
+
+ /*
+ * We process bits from most to least significant. At each
+ * loop iteration, we have acc_len bits in acc.
+ */
+ acc = 0;
+ acc_len = 0;
+ while (acc_len > 0 || elen > 0) {
+ int i, k;
+ uint32_t bits;
+
+ /*
+ * Get the next bits.
+ */
+ k = win_len;
+ if (acc_len < win_len) {
+ if (elen > 0) {
+ acc = (acc << 8) | *e ++;
+ elen --;
+ acc_len += 8;
+ } else {
+ k = acc_len;
+ }
+ }
+ bits = (acc >> (acc_len - k)) & (((uint32_t)1 << k) - 1);
+ acc_len -= k;
+
+ /*
+ * We could get exactly k bits. Compute k squarings.
+ */
+ for (i = 0; i < k; i ++) {
+ br_i15_montymul(t1, x, x, m, m0i);
+ memcpy(x, t1, mlen);
+ }
+
+ /*
+ * Window lookup: we want to set t2 to the window
+ * lookup value, assuming the bits are non-zero. If
+ * the window length is 1 bit only, then t2 is
+ * already set; otherwise, we do a constant-time lookup.
+ */
+ if (win_len > 1) {
+ br_i15_zero(t2, m[0]);
+ base = t2 + mwlen;
+ for (u = 1; u < ((uint32_t)1 << k); u ++) {
+ uint32_t mask;
+
+ mask = -EQ(u, bits);
+ for (v = 1; v < mwlen; v ++) {
+ t2[v] |= mask & base[v];
+ }
+ base += mwlen;
+ }
+ }
+
+ /*
+ * Multiply with the looked-up value. We keep the
+ * product only if the exponent bits are not all-zero.
+ */
+ br_i15_montymul(t1, x, t2, m, m0i);
+ CCOPY(NEQ(bits, 0), x, t1, mlen);
+ }
+
+ /*
+ * Convert back from Montgomery representation, and exit.
+ */
+ br_i15_from_monty(x, m, m0i);
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_montmul.c b/test/monniaux/BearSSL/src/int/i15_montmul.c
new file mode 100644
index 00000000..e98bc32c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_montmul.c
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_montymul(uint16_t *d, const uint16_t *x, const uint16_t *y,
+ const uint16_t *m, uint16_t m0i)
+{
+ size_t len, len4, u, v;
+ uint32_t dh;
+
+ len = (m[0] + 15) >> 4;
+ len4 = len & ~(size_t)3;
+ br_i15_zero(d, m[0]);
+ dh = 0;
+ for (u = 0; u < len; u ++) {
+ uint32_t f, xu, r, zh;
+
+ xu = x[u + 1];
+ f = MUL15((d[1] + MUL15(x[u + 1], y[1])) & 0x7FFF, m0i)
+ & 0x7FFF;
+#if BR_ARMEL_CORTEXM_GCC
+ if (len4 != 0) {
+ uint16_t *limit;
+
+ limit = d + len4;
+ asm volatile (
+"\n\
+ @ carry: r=r2 \n\
+ @ multipliers: xu=r3 f=r4 \n\
+ @ base registers: d+v=r5 y+v=r6 m+v=r7 \n\
+ @ r8 contains 0x7FFF \n\
+ @ r9 contains d+len4 \n\
+ ldr r0, %[limit] \n\
+ ldr r3, %[xu] \n\
+ mov r9, r0 \n\
+ ldr r4, %[f] \n\
+ eor r2, r2 \n\
+ ldr r5, %[d] \n\
+ sub r1, r2, #1 \n\
+ ldr r6, %[y] \n\
+ lsr r1, r1, #17 \n\
+ ldr r7, %[m] \n\
+ mov r8, r1 \n\
+loop%=: \n\
+ ldrh r0, [r6, #2] \n\
+ ldrh r1, [r7, #2] \n\
+ mul r0, r3 \n\
+ mul r1, r4 \n\
+ add r2, r0, r2 \n\
+ ldrh r0, [r5, #2] \n\
+ add r2, r1, r2 \n\
+ mov r1, r8 \n\
+ add r2, r0, r2 \n\
+ and r1, r2 \n\
+ lsr r2, r2, #15 \n\
+ strh r1, [r5, #0] \n\
+ \n\
+ ldrh r0, [r6, #4] \n\
+ ldrh r1, [r7, #4] \n\
+ mul r0, r3 \n\
+ mul r1, r4 \n\
+ add r2, r0, r2 \n\
+ ldrh r0, [r5, #4] \n\
+ add r2, r1, r2 \n\
+ mov r1, r8 \n\
+ add r2, r0, r2 \n\
+ and r1, r2 \n\
+ lsr r2, r2, #15 \n\
+ strh r1, [r5, #2] \n\
+ \n\
+ ldrh r0, [r6, #6] \n\
+ ldrh r1, [r7, #6] \n\
+ mul r0, r3 \n\
+ mul r1, r4 \n\
+ add r2, r0, r2 \n\
+ ldrh r0, [r5, #6] \n\
+ add r2, r1, r2 \n\
+ mov r1, r8 \n\
+ add r2, r0, r2 \n\
+ and r1, r2 \n\
+ lsr r2, r2, #15 \n\
+ strh r1, [r5, #4] \n\
+ \n\
+ ldrh r0, [r6, #8] \n\
+ ldrh r1, [r7, #8] \n\
+ mul r0, r3 \n\
+ mul r1, r4 \n\
+ add r2, r0, r2 \n\
+ ldrh r0, [r5, #8] \n\
+ add r2, r1, r2 \n\
+ mov r1, r8 \n\
+ add r2, r0, r2 \n\
+ and r1, r2 \n\
+ lsr r2, r2, #15 \n\
+ strh r1, [r5, #6] \n\
+ \n\
+ add r5, r5, #8 \n\
+ add r6, r6, #8 \n\
+ add r7, r7, #8 \n\
+ cmp r5, r9 \n\
+ bne loop%= \n\
+ \n\
+ str r2, %[carry] \n\
+"
+: [carry] "=m" (r)
+: [xu] "m" (xu), [f] "m" (f), [d] "m" (d), [y] "m" (y),
+ [m] "m" (m), [limit] "m" (limit)
+: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
+ } else {
+ r = 0;
+ }
+ v = len4;
+#else
+ r = 0;
+ for (v = 0; v < len4; v += 4) {
+ uint32_t z;
+
+ z = d[v + 1] + MUL15(xu, y[v + 1])
+ + MUL15(f, m[v + 1]) + r;
+ r = z >> 15;
+ d[v + 0] = z & 0x7FFF;
+ z = d[v + 2] + MUL15(xu, y[v + 2])
+ + MUL15(f, m[v + 2]) + r;
+ r = z >> 15;
+ d[v + 1] = z & 0x7FFF;
+ z = d[v + 3] + MUL15(xu, y[v + 3])
+ + MUL15(f, m[v + 3]) + r;
+ r = z >> 15;
+ d[v + 2] = z & 0x7FFF;
+ z = d[v + 4] + MUL15(xu, y[v + 4])
+ + MUL15(f, m[v + 4]) + r;
+ r = z >> 15;
+ d[v + 3] = z & 0x7FFF;
+ }
+#endif
+ for (; v < len; v ++) {
+ uint32_t z;
+
+ z = d[v + 1] + MUL15(xu, y[v + 1])
+ + MUL15(f, m[v + 1]) + r;
+ r = z >> 15;
+ d[v + 0] = z & 0x7FFF;
+ }
+
+ zh = dh + r;
+ d[len] = zh & 0x7FFF;
+ dh = zh >> 15;
+ }
+
+ /*
+ * Restore the bit length (it was overwritten in the loop above).
+ */
+ d[0] = m[0];
+
+ /*
+ * d[] may be greater than m[], but it is still lower than twice
+ * the modulus.
+ */
+ br_i15_sub(d, m, NEQ(dh, 0) | NOT(br_i15_sub(d, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_mulacc.c b/test/monniaux/BearSSL/src/int/i15_mulacc.c
new file mode 100644
index 00000000..7a073ac6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_mulacc.c
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_mulacc(uint16_t *d, const uint16_t *a, const uint16_t *b)
+{
+ size_t alen, blen, u;
+ unsigned dl, dh;
+
+ alen = (a[0] + 15) >> 4;
+ blen = (b[0] + 15) >> 4;
+
+ /*
+ * Announced bit length of d[] will be the sum of the announced
+ * bit lengths of a[] and b[]; but the lengths are encoded.
+ */
+ dl = (a[0] & 15) + (b[0] & 15);
+ dh = (a[0] >> 4) + (b[0] >> 4);
+ d[0] = (dh << 4) + dl + (~(uint32_t)(dl - 15) >> 31);
+
+ for (u = 0; u < blen; u ++) {
+ uint32_t f;
+ size_t v;
+ uint32_t cc;
+
+ f = b[1 + u];
+ cc = 0;
+ for (v = 0; v < alen; v ++) {
+ uint32_t z;
+
+ z = (uint32_t)d[1 + u + v] + MUL15(f, a[1 + v]) + cc;
+ cc = z >> 15;
+ d[1 + u + v] = z & 0x7FFF;
+ }
+ d[1 + u + alen] = cc;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_muladd.c b/test/monniaux/BearSSL/src/int/i15_muladd.c
new file mode 100644
index 00000000..c4b72168
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_muladd.c
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Constant-time division. The divisor must not be larger than 16 bits,
+ * and the quotient must fit on 17 bits.
+ */
+static uint32_t
+divrem16(uint32_t x, uint32_t d, uint32_t *r)
+{
+ int i;
+ uint32_t q;
+
+ q = 0;
+ d <<= 16;
+ for (i = 16; i >= 0; i --) {
+ uint32_t ctl;
+
+ ctl = LE(d, x);
+ q |= ctl << i;
+ x -= (-ctl) & d;
+ d >>= 1;
+ }
+ if (r != NULL) {
+ *r = x;
+ }
+ return q;
+}
+
+/* see inner.h */
+void
+br_i15_muladd_small(uint16_t *x, uint16_t z, const uint16_t *m)
+{
+ /*
+ * Constant-time: we accept to leak the exact bit length of the
+ * modulus m.
+ */
+ unsigned m_bitlen, mblr;
+ size_t u, mlen;
+ uint32_t hi, a0, a, b, q;
+ uint32_t cc, tb, over, under;
+
+ /*
+ * Simple case: the modulus fits on one word.
+ */
+ m_bitlen = m[0];
+ if (m_bitlen == 0) {
+ return;
+ }
+ if (m_bitlen <= 15) {
+ uint32_t rem;
+
+ divrem16(((uint32_t)x[1] << 15) | z, m[1], &rem);
+ x[1] = rem;
+ return;
+ }
+ mlen = (m_bitlen + 15) >> 4;
+ mblr = m_bitlen & 15;
+
+ /*
+ * Principle: we estimate the quotient (x*2^15+z)/m by
+ * doing a 30/15 division with the high words.
+ *
+ * Let:
+ * w = 2^15
+ * a = (w*a0 + a1) * w^N + a2
+ * b = b0 * w^N + b2
+ * such that:
+ * 0 <= a0 < w
+ * 0 <= a1 < w
+ * 0 <= a2 < w^N
+ * w/2 <= b0 < w
+ * 0 <= b2 < w^N
+ * a < w*b
+ * I.e. the two top words of a are a0:a1, the top word of b is
+ * b0, we ensured that b0 is "full" (high bit set), and a is
+ * such that the quotient q = a/b fits on one word (0 <= q < w).
+ *
+ * If a = b*q + r (with 0 <= r < q), then we can estimate q by
+ * using a division on the top words:
+ * a0*w + a1 = b0*u + v (with 0 <= v < b0)
+ * Then the following holds:
+ * 0 <= u <= w
+ * u-2 <= q <= u
+ */
+ hi = x[mlen];
+ if (mblr == 0) {
+ a0 = x[mlen];
+ memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
+ x[1] = z;
+ a = (a0 << 15) + x[mlen];
+ b = m[mlen];
+ } else {
+ a0 = (x[mlen] << (15 - mblr)) | (x[mlen - 1] >> mblr);
+ memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
+ x[1] = z;
+ a = (a0 << 15) | (((x[mlen] << (15 - mblr))
+ | (x[mlen - 1] >> mblr)) & 0x7FFF);
+ b = (m[mlen] << (15 - mblr)) | (m[mlen - 1] >> mblr);
+ }
+ q = divrem16(a, b, NULL);
+
+ /*
+ * We computed an estimate for q, but the real one may be q,
+ * q-1 or q-2; moreover, the division may have returned a value
+ * 8000 or even 8001 if the two high words were identical, and
+ * we want to avoid values beyond 7FFF. We thus adjust q so
+ * that the "true" multiplier will be q+1, q or q-1, and q is
+ * in the 0000..7FFF range.
+ */
+ q = MUX(EQ(b, a0), 0x7FFF, q - 1 + ((q - 1) >> 31));
+
+ /*
+ * We subtract q*m from x (x has an extra high word of value 'hi').
+ * Since q may be off by 1 (in either direction), we may have to
+ * add or subtract m afterwards.
+ *
+ * The 'tb' flag will be true (1) at the end of the loop if the
+ * result is greater than or equal to the modulus (not counting
+ * 'hi' or the carry).
+ */
+ cc = 0;
+ tb = 1;
+ for (u = 1; u <= mlen; u ++) {
+ uint32_t mw, zl, xw, nxw;
+
+ mw = m[u];
+ zl = MUL15(mw, q) + cc;
+ cc = zl >> 15;
+ zl &= 0x7FFF;
+ xw = x[u];
+ nxw = xw - zl;
+ cc += nxw >> 31;
+ nxw &= 0x7FFF;
+ x[u] = nxw;
+ tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
+ }
+
+ /*
+ * If we underestimated q, then either cc < hi (one extra bit
+ * beyond the top array word), or cc == hi and tb is true (no
+ * extra bit, but the result is not lower than the modulus).
+ *
+ * If we overestimated q, then cc > hi.
+ */
+ over = GT(cc, hi);
+ under = ~over & (tb | LT(cc, hi));
+ br_i15_add(x, m, over);
+ br_i15_sub(x, m, under);
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_ninv15.c b/test/monniaux/BearSSL/src/int/i15_ninv15.c
new file mode 100644
index 00000000..de3a3ba8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_ninv15.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint16_t
+br_i15_ninv15(uint16_t x)
+{
+ uint32_t y;
+
+ y = 2 - x;
+ y = MUL15(y, 2 - MUL15(x, y));
+ y = MUL15(y, 2 - MUL15(x, y));
+ y = MUL15(y, 2 - MUL15(x, y));
+ return MUX(x & 1, -y, 0) & 0x7FFF;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_reduce.c b/test/monniaux/BearSSL/src/int/i15_reduce.c
new file mode 100644
index 00000000..0931b104
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_reduce.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_reduce(uint16_t *x, const uint16_t *a, const uint16_t *m)
+{
+ uint32_t m_bitlen, a_bitlen;
+ size_t mlen, alen, u;
+
+ m_bitlen = m[0];
+ mlen = (m_bitlen + 15) >> 4;
+
+ x[0] = m_bitlen;
+ if (m_bitlen == 0) {
+ return;
+ }
+
+ /*
+ * If the source is shorter, then simply copy all words from a[]
+ * and zero out the upper words.
+ */
+ a_bitlen = a[0];
+ alen = (a_bitlen + 15) >> 4;
+ if (a_bitlen < m_bitlen) {
+ memcpy(x + 1, a + 1, alen * sizeof *a);
+ for (u = alen; u < mlen; u ++) {
+ x[u + 1] = 0;
+ }
+ return;
+ }
+
+ /*
+ * The source length is at least equal to that of the modulus.
+ * We must thus copy N-1 words, and input the remaining words
+ * one by one.
+ */
+ memcpy(x + 1, a + 2 + (alen - mlen), (mlen - 1) * sizeof *a);
+ x[mlen] = 0;
+ for (u = 1 + alen - mlen; u > 0; u --) {
+ br_i15_muladd_small(x, a[u], m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_rshift.c b/test/monniaux/BearSSL/src/int/i15_rshift.c
new file mode 100644
index 00000000..f9991ab6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_rshift.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_rshift(uint16_t *x, int count)
+{
+ size_t u, len;
+ unsigned r;
+
+ len = (x[0] + 15) >> 4;
+ if (len == 0) {
+ return;
+ }
+ r = x[1] >> count;
+ for (u = 2; u <= len; u ++) {
+ unsigned w;
+
+ w = x[u];
+ x[u - 1] = ((w << (15 - count)) | r) & 0x7FFF;
+ r = w >> count;
+ }
+ x[len] = r;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_sub.c b/test/monniaux/BearSSL/src/int/i15_sub.c
new file mode 100644
index 00000000..1983c4dc
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_sub.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i15_sub(uint16_t *a, const uint16_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 31) >> 4;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw - bw - cc;
+ cc = naw >> 31;
+ a[u] = MUX(ctl, naw & 0x7FFF, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i15_tmont.c b/test/monniaux/BearSSL/src/int/i15_tmont.c
new file mode 100644
index 00000000..d5c4b8b7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i15_tmont.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i15_to_monty(uint16_t *x, const uint16_t *m)
+{
+ unsigned k;
+
+ for (k = (m[0] + 15) >> 4; k > 0; k --) {
+ br_i15_muladd_small(x, 0, m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_add.c b/test/monniaux/BearSSL/src/int/i31_add.c
new file mode 100644
index 00000000..2ca47c6b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_add.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_add(uint32_t *a, const uint32_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 63) >> 5;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw + bw + cc;
+ cc = naw >> 31;
+ a[u] = MUX(ctl, naw & (uint32_t)0x7FFFFFFF, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_bitlen.c b/test/monniaux/BearSSL/src/int/i31_bitlen.c
new file mode 100644
index 00000000..3e127c2c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_bitlen.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_bit_length(uint32_t *x, size_t xlen)
+{
+ uint32_t tw, twk;
+
+ tw = 0;
+ twk = 0;
+ while (xlen -- > 0) {
+ uint32_t w, c;
+
+ c = EQ(tw, 0);
+ w = x[xlen];
+ tw = MUX(c, w, tw);
+ twk = MUX(c, (uint32_t)xlen, twk);
+ }
+ return (twk << 5) + BIT_LENGTH(tw);
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_decmod.c b/test/monniaux/BearSSL/src/int/i31_decmod.c
new file mode 100644
index 00000000..3cd7bfe9
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_decmod.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_decode_mod(uint32_t *x, const void *src, size_t len, const uint32_t *m)
+{
+ /*
+ * Two-pass algorithm: in the first pass, we determine whether the
+ * value fits; in the second pass, we do the actual write.
+ *
+ * During the first pass, 'r' contains the comparison result so
+ * far:
+ * 0x00000000 value is equal to the modulus
+ * 0x00000001 value is greater than the modulus
+ * 0xFFFFFFFF value is lower than the modulus
+ *
+ * Since we iterate starting with the least significant bytes (at
+ * the end of src[]), each new comparison overrides the previous
+ * except when the comparison yields 0 (equal).
+ *
+ * During the second pass, 'r' is either 0xFFFFFFFF (value fits)
+ * or 0x00000000 (value does not fit).
+ *
+ * We must iterate over all bytes of the source, _and_ possibly
+ * some extra virtual bytes (with value 0) so as to cover the
+ * complete modulus as well. We also add 4 such extra bytes beyond
+ * the modulus length because it then guarantees that no accumulated
+ * partial word remains to be processed.
+ */
+ const unsigned char *buf;
+ size_t mlen, tlen;
+ int pass;
+ uint32_t r;
+
+ buf = src;
+ mlen = (m[0] + 31) >> 5;
+ tlen = (mlen << 2);
+ if (tlen < len) {
+ tlen = len;
+ }
+ tlen += 4;
+ r = 0;
+ for (pass = 0; pass < 2; pass ++) {
+ size_t u, v;
+ uint32_t acc;
+ int acc_len;
+
+ v = 1;
+ acc = 0;
+ acc_len = 0;
+ for (u = 0; u < tlen; u ++) {
+ uint32_t b;
+
+ if (u < len) {
+ b = buf[len - 1 - u];
+ } else {
+ b = 0;
+ }
+ acc |= (b << acc_len);
+ acc_len += 8;
+ if (acc_len >= 31) {
+ uint32_t xw;
+
+ xw = acc & (uint32_t)0x7FFFFFFF;
+ acc_len -= 31;
+ acc = b >> (8 - acc_len);
+ if (v <= mlen) {
+ if (pass) {
+ x[v] = r & xw;
+ } else {
+ uint32_t cc;
+
+ cc = (uint32_t)CMP(xw, m[v]);
+ r = MUX(EQ(cc, 0), r, cc);
+ }
+ } else {
+ if (!pass) {
+ r = MUX(EQ(xw, 0), r, 1);
+ }
+ }
+ v ++;
+ }
+ }
+
+ /*
+ * When we reach this point at the end of the first pass:
+ * r is either 0, 1 or -1; we want to set r to 0 if it
+ * is equal to 0 or 1, and leave it to -1 otherwise.
+ *
+ * When we reach this point at the end of the second pass:
+ * r is either 0 or -1; we want to leave that value
+ * untouched. This is a subcase of the previous.
+ */
+ r >>= 1;
+ r |= (r << 1);
+ }
+
+ x[0] = m[0];
+ return r & (uint32_t)1;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_decode.c b/test/monniaux/BearSSL/src/int/i31_decode.c
new file mode 100644
index 00000000..8ec6d908
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_decode.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_decode(uint32_t *x, const void *src, size_t len)
+{
+ const unsigned char *buf;
+ size_t u, v;
+ uint32_t acc;
+ int acc_len;
+
+ buf = src;
+ u = len;
+ v = 1;
+ acc = 0;
+ acc_len = 0;
+ while (u -- > 0) {
+ uint32_t b;
+
+ b = buf[u];
+ acc |= (b << acc_len);
+ acc_len += 8;
+ if (acc_len >= 31) {
+ x[v ++] = acc & (uint32_t)0x7FFFFFFF;
+ acc_len -= 31;
+ acc = b >> (8 - acc_len);
+ }
+ }
+ if (acc_len != 0) {
+ x[v ++] = acc;
+ }
+ x[0] = br_i31_bit_length(x + 1, v - 1);
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_decred.c b/test/monniaux/BearSSL/src/int/i31_decred.c
new file mode 100644
index 00000000..43db6624
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_decred.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_decode_reduce(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m)
+{
+ uint32_t m_ebitlen, m_rbitlen;
+ size_t mblen, k;
+ const unsigned char *buf;
+ uint32_t acc;
+ int acc_len;
+
+ /*
+ * Get the encoded bit length.
+ */
+ m_ebitlen = m[0];
+
+ /*
+ * Special case for an invalid (null) modulus.
+ */
+ if (m_ebitlen == 0) {
+ x[0] = 0;
+ return;
+ }
+
+ /*
+ * Clear the destination.
+ */
+ br_i31_zero(x, m_ebitlen);
+
+ /*
+ * First decode directly as many bytes as possible. This requires
+ * computing the actual bit length.
+ */
+ m_rbitlen = m_ebitlen >> 5;
+ m_rbitlen = (m_ebitlen & 31) + (m_rbitlen << 5) - m_rbitlen;
+ mblen = (m_rbitlen + 7) >> 3;
+ k = mblen - 1;
+ if (k >= len) {
+ br_i31_decode(x, src, len);
+ x[0] = m_ebitlen;
+ return;
+ }
+ buf = src;
+ br_i31_decode(x, buf, k);
+ x[0] = m_ebitlen;
+
+ /*
+ * Input remaining bytes, using 31-bit words.
+ */
+ acc = 0;
+ acc_len = 0;
+ while (k < len) {
+ uint32_t v;
+
+ v = buf[k ++];
+ if (acc_len >= 23) {
+ acc_len -= 23;
+ acc <<= (8 - acc_len);
+ acc |= v >> acc_len;
+ br_i31_muladd_small(x, acc, m);
+ acc = v & (0xFF >> (8 - acc_len));
+ } else {
+ acc = (acc << 8) | v;
+ acc_len += 8;
+ }
+ }
+
+ /*
+ * We may have some bits accumulated. We then perform a shift to
+ * be able to inject these bits as a full 31-bit word.
+ */
+ if (acc_len != 0) {
+ acc = (acc | (x[1] << acc_len)) & 0x7FFFFFFF;
+ br_i31_rshift(x, 31 - acc_len);
+ br_i31_muladd_small(x, acc, m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_encode.c b/test/monniaux/BearSSL/src/int/i31_encode.c
new file mode 100644
index 00000000..b6b40c45
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_encode.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_encode(void *dst, size_t len, const uint32_t *x)
+{
+ unsigned char *buf;
+ size_t k, xlen;
+ uint32_t acc;
+ int acc_len;
+
+ xlen = (x[0] + 31) >> 5;
+ if (xlen == 0) {
+ memset(dst, 0, len);
+ return;
+ }
+ buf = (unsigned char *)dst + len;
+ k = 1;
+ acc = 0;
+ acc_len = 0;
+ while (len != 0) {
+ uint32_t w;
+
+ w = (k <= xlen) ? x[k] : 0;
+ k ++;
+ if (acc_len == 0) {
+ acc = w;
+ acc_len = 31;
+ } else {
+ uint32_t z;
+
+ z = acc | (w << acc_len);
+ acc_len --;
+ acc = w >> (31 - acc_len);
+ if (len >= 4) {
+ buf -= 4;
+ len -= 4;
+ br_enc32be(buf, z);
+ } else {
+ switch (len) {
+ case 3:
+ buf[-3] = (unsigned char)(z >> 16);
+ /* fall through */
+ case 2:
+ buf[-2] = (unsigned char)(z >> 8);
+ /* fall through */
+ case 1:
+ buf[-1] = (unsigned char)z;
+ break;
+ }
+ return;
+ }
+ }
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_fmont.c b/test/monniaux/BearSSL/src/int/i31_fmont.c
new file mode 100644
index 00000000..c24b4176
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_fmont.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i)
+{
+ size_t len, u, v;
+
+ len = (m[0] + 31) >> 5;
+ for (u = 0; u < len; u ++) {
+ uint32_t f;
+ uint64_t cc;
+
+ f = MUL31_lo(x[1], m0i);
+ cc = 0;
+ for (v = 0; v < len; v ++) {
+ uint64_t z;
+
+ z = (uint64_t)x[v + 1] + MUL31(f, m[v + 1]) + cc;
+ cc = z >> 31;
+ if (v != 0) {
+ x[v] = (uint32_t)z & 0x7FFFFFFF;
+ }
+ }
+ x[len] = (uint32_t)cc;
+ }
+
+ /*
+ * We may have to do an extra subtraction, but only if the
+ * value in x[] is indeed greater than or equal to that of m[],
+ * which is why we must do two calls (first call computes the
+ * carry, second call performs the subtraction only if the carry
+ * is 0).
+ */
+ br_i31_sub(x, m, NOT(br_i31_sub(x, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_iszero.c b/test/monniaux/BearSSL/src/int/i31_iszero.c
new file mode 100644
index 00000000..8a7ea44f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_iszero.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_iszero(const uint32_t *x)
+{
+ uint32_t z;
+ size_t u;
+
+ z = 0;
+ for (u = (x[0] + 31) >> 5; u > 0; u --) {
+ z |= x[u];
+ }
+ return ~(z | -z) >> 31;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_moddiv.c b/test/monniaux/BearSSL/src/int/i31_moddiv.c
new file mode 100644
index 00000000..99505911
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_moddiv.c
@@ -0,0 +1,488 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * In this file, we handle big integers with a custom format, i.e.
+ * without the usual one-word header. Value is split into 31-bit words,
+ * each stored in a 32-bit slot (top bit is zero) in little-endian
+ * order. The length (in words) is provided explicitly. In some cases,
+ * the value can be negative (using two's complement representation). In
+ * some cases, the top word is allowed to have a 32th bit.
+ */
+
+/*
+ * Negate big integer conditionally. The value consists of 'len' words,
+ * with 31 bits in each word (the top bit of each word should be 0,
+ * except possibly for the last word). If 'ctl' is 1, the negation is
+ * computed; otherwise, if 'ctl' is 0, then the value is unchanged.
+ */
+static void
+cond_negate(uint32_t *a, size_t len, uint32_t ctl)
+{
+ size_t k;
+ uint32_t cc, xm;
+
+ cc = ctl;
+ xm = -ctl >> 1;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw;
+
+ aw = a[k];
+ aw = (aw ^ xm) + cc;
+ a[k] = aw & 0x7FFFFFFF;
+ cc = aw >> 31;
+ }
+}
+
+/*
+ * Finish modular reduction. Rules on input parameters:
+ *
+ * if neg = 1, then -m <= a < 0
+ * if neg = 0, then 0 <= a < 2*m
+ *
+ * If neg = 0, then the top word of a[] may use 32 bits.
+ *
+ * Also, modulus m must be odd.
+ */
+static void
+finish_mod(uint32_t *a, size_t len, const uint32_t *m, uint32_t neg)
+{
+ size_t k;
+ uint32_t cc, xm, ym;
+
+ /*
+ * First pass: compare a (assumed nonnegative) with m.
+ * Note that if the final word uses the top extra bit, then
+ * subtracting m must yield a value less than 2^31, since we
+ * assumed that a < 2*m.
+ */
+ cc = 0;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw, mw;
+
+ aw = a[k];
+ mw = m[k];
+ cc = (aw - mw - cc) >> 31;
+ }
+
+ /*
+ * At this point:
+ * if neg = 1, then we must add m (regardless of cc)
+ * if neg = 0 and cc = 0, then we must subtract m
+ * if neg = 0 and cc = 1, then we must do nothing
+ */
+ xm = -neg >> 1;
+ ym = -(neg | (1 - cc));
+ cc = neg;
+ for (k = 0; k < len; k ++) {
+ uint32_t aw, mw;
+
+ aw = a[k];
+ mw = (m[k] ^ xm) & ym;
+ aw = aw - mw - cc;
+ a[k] = aw & 0x7FFFFFFF;
+ cc = aw >> 31;
+ }
+}
+
+/*
+ * Compute:
+ * a <- (a*pa+b*pb)/(2^31)
+ * b <- (a*qa+b*qb)/(2^31)
+ * The division is assumed to be exact (i.e. the low word is dropped).
+ * If the final a is negative, then it is negated. Similarly for b.
+ * Returned value is the combination of two bits:
+ * bit 0: 1 if a had to be negated, 0 otherwise
+ * bit 1: 1 if b had to be negated, 0 otherwise
+ *
+ * Factors pa, pb, qa and qb must be at most 2^31 in absolute value.
+ * Source integers a and b must be nonnegative; top word is not allowed
+ * to contain an extra 32th bit.
+ */
+static uint32_t
+co_reduce(uint32_t *a, uint32_t *b, size_t len,
+ int64_t pa, int64_t pb, int64_t qa, int64_t qb)
+{
+ size_t k;
+ int64_t cca, ccb;
+ uint32_t nega, negb;
+
+ cca = 0;
+ ccb = 0;
+ for (k = 0; k < len; k ++) {
+ uint32_t wa, wb;
+ uint64_t za, zb;
+ uint64_t tta, ttb;
+
+ /*
+ * Since:
+ * |pa| <= 2^31
+ * |pb| <= 2^31
+ * 0 <= wa <= 2^31 - 1
+ * 0 <= wb <= 2^31 - 1
+ * |cca| <= 2^32 - 1
+ * Then:
+ * |za| <= (2^31-1)*(2^32) + (2^32-1) = 2^63 - 1
+ *
+ * Thus, the new value of cca is such that |cca| <= 2^32 - 1.
+ * The same applies to ccb.
+ */
+ wa = a[k];
+ wb = b[k];
+ za = wa * (uint64_t)pa + wb * (uint64_t)pb + (uint64_t)cca;
+ zb = wa * (uint64_t)qa + wb * (uint64_t)qb + (uint64_t)ccb;
+ if (k > 0) {
+ a[k - 1] = za & 0x7FFFFFFF;
+ b[k - 1] = zb & 0x7FFFFFFF;
+ }
+
+ /*
+ * For the new values of cca and ccb, we need a signed
+ * right-shift; since, in C, right-shifting a signed
+ * negative value is implementation-defined, we use a
+ * custom portable sign extension expression.
+ */
+#define M ((uint64_t)1 << 32)
+ tta = za >> 31;
+ ttb = zb >> 31;
+ tta = (tta ^ M) - M;
+ ttb = (ttb ^ M) - M;
+ cca = *(int64_t *)&tta;
+ ccb = *(int64_t *)&ttb;
+#undef M
+ }
+ a[len - 1] = (uint32_t)cca;
+ b[len - 1] = (uint32_t)ccb;
+
+ nega = (uint32_t)((uint64_t)cca >> 63);
+ negb = (uint32_t)((uint64_t)ccb >> 63);
+ cond_negate(a, len, nega);
+ cond_negate(b, len, negb);
+ return nega | (negb << 1);
+}
+
+/*
+ * Compute:
+ * a <- (a*pa+b*pb)/(2^31) mod m
+ * b <- (a*qa+b*qb)/(2^31) mod m
+ *
+ * m0i is equal to -1/m[0] mod 2^31.
+ *
+ * Factors pa, pb, qa and qb must be at most 2^31 in absolute value.
+ * Source integers a and b must be nonnegative; top word is not allowed
+ * to contain an extra 32th bit.
+ */
+static void
+co_reduce_mod(uint32_t *a, uint32_t *b, size_t len,
+ int64_t pa, int64_t pb, int64_t qa, int64_t qb,
+ const uint32_t *m, uint32_t m0i)
+{
+ size_t k;
+ int64_t cca, ccb;
+ uint32_t fa, fb;
+
+ cca = 0;
+ ccb = 0;
+ fa = ((a[0] * (uint32_t)pa + b[0] * (uint32_t)pb) * m0i) & 0x7FFFFFFF;
+ fb = ((a[0] * (uint32_t)qa + b[0] * (uint32_t)qb) * m0i) & 0x7FFFFFFF;
+ for (k = 0; k < len; k ++) {
+ uint32_t wa, wb;
+ uint64_t za, zb;
+ uint64_t tta, ttb;
+
+ /*
+ * In this loop, carries 'cca' and 'ccb' always fit on
+ * 33 bits (in absolute value).
+ */
+ wa = a[k];
+ wb = b[k];
+ za = wa * (uint64_t)pa + wb * (uint64_t)pb
+ + m[k] * (uint64_t)fa + (uint64_t)cca;
+ zb = wa * (uint64_t)qa + wb * (uint64_t)qb
+ + m[k] * (uint64_t)fb + (uint64_t)ccb;
+ if (k > 0) {
+ a[k - 1] = (uint32_t)za & 0x7FFFFFFF;
+ b[k - 1] = (uint32_t)zb & 0x7FFFFFFF;
+ }
+
+#define M ((uint64_t)1 << 32)
+ tta = za >> 31;
+ ttb = zb >> 31;
+ tta = (tta ^ M) - M;
+ ttb = (ttb ^ M) - M;
+ cca = *(int64_t *)&tta;
+ ccb = *(int64_t *)&ttb;
+#undef M
+ }
+ a[len - 1] = (uint32_t)cca;
+ b[len - 1] = (uint32_t)ccb;
+
+ /*
+ * At this point:
+ * -m <= a < 2*m
+ * -m <= b < 2*m
+ * (this is a case of Montgomery reduction)
+ * The top word of 'a' and 'b' may have a 32-th bit set.
+ * We may have to add or subtract the modulus.
+ */
+ finish_mod(a, len, m, (uint32_t)((uint64_t)cca >> 63));
+ finish_mod(b, len, m, (uint32_t)((uint64_t)ccb >> 63));
+}
+
+/* see inner.h */
+uint32_t
+br_i31_moddiv(uint32_t *x, const uint32_t *y, const uint32_t *m, uint32_t m0i,
+ uint32_t *t)
+{
+ /*
+ * Algorithm is an extended binary GCD. We maintain four values
+ * a, b, u and v, with the following invariants:
+ *
+ * a * x = y * u mod m
+ * b * x = y * v mod m
+ *
+ * Starting values are:
+ *
+ * a = y
+ * b = m
+ * u = x
+ * v = 0
+ *
+ * The formal definition of the algorithm is a sequence of steps:
+ *
+ * - If a is even, then a <- a/2 and u <- u/2 mod m.
+ * - Otherwise, if b is even, then b <- b/2 and v <- v/2 mod m.
+ * - Otherwise, if a > b, then a <- (a-b)/2 and u <- (u-v)/2 mod m.
+ * - Otherwise, b <- (b-a)/2 and v <- (v-u)/2 mod m.
+ *
+ * Algorithm stops when a = b. At that point, they both are equal
+ * to GCD(y,m); the modular division succeeds if that value is 1.
+ * The result of the modular division is then u (or v: both are
+ * equal at that point).
+ *
+ * Each step makes either a or b shrink by at least one bit; hence,
+ * if m has bit length k bits, then 2k-2 steps are sufficient.
+ *
+ *
+ * Though complexity is quadratic in the size of m, the bit-by-bit
+ * processing is not very efficient. We can speed up processing by
+ * remarking that the decisions are taken based only on observation
+ * of the top and low bits of a and b.
+ *
+ * In the loop below, at each iteration, we use the two top words
+ * of a and b, and the low words of a and b, to compute reduction
+ * parameters pa, pb, qa and qb such that the new values for a
+ * and b are:
+ *
+ * a' = (a*pa + b*pb) / (2^31)
+ * b' = (a*qa + b*qb) / (2^31)
+ *
+ * the division being exact.
+ *
+ * Since the choices are based on the top words, they may be slightly
+ * off, requiring an optional correction: if a' < 0, then we replace
+ * pa with -pa, and pb with -pb. The total length of a and b is
+ * thus reduced by at least 30 bits at each iteration.
+ *
+ * The stopping conditions are still the same, though: when a
+ * and b become equal, they must be both odd (since m is odd,
+ * the GCD cannot be even), therefore the next operation is a
+ * subtraction, and one of the values becomes 0. At that point,
+ * nothing else happens, i.e. one value is stuck at 0, and the
+ * other one is the GCD.
+ */
+ size_t len, k;
+ uint32_t *a, *b, *u, *v;
+ uint32_t num, r;
+
+ len = (m[0] + 31) >> 5;
+ a = t;
+ b = a + len;
+ u = x + 1;
+ v = b + len;
+ memcpy(a, y + 1, len * sizeof *y);
+ memcpy(b, m + 1, len * sizeof *m);
+ memset(v, 0, len * sizeof *v);
+
+ /*
+ * Loop below ensures that a and b are reduced by some bits each,
+ * for a total of at least 30 bits.
+ */
+ for (num = ((m[0] - (m[0] >> 5)) << 1) + 30; num >= 30; num -= 30) {
+ size_t j;
+ uint32_t c0, c1;
+ uint32_t a0, a1, b0, b1;
+ uint64_t a_hi, b_hi;
+ uint32_t a_lo, b_lo;
+ int64_t pa, pb, qa, qb;
+ int i;
+
+ /*
+ * Extract top words of a and b. If j is the highest
+ * index >= 1 such that a[j] != 0 or b[j] != 0, then we want
+ * (a[j] << 31) + a[j - 1], and (b[j] << 31) + b[j - 1].
+ * If a and b are down to one word each, then we use a[0]
+ * and b[0].
+ */
+ c0 = (uint32_t)-1;
+ c1 = (uint32_t)-1;
+ a0 = 0;
+ a1 = 0;
+ b0 = 0;
+ b1 = 0;
+ j = len;
+ while (j -- > 0) {
+ uint32_t aw, bw;
+
+ aw = a[j];
+ bw = b[j];
+ a0 ^= (a0 ^ aw) & c0;
+ a1 ^= (a1 ^ aw) & c1;
+ b0 ^= (b0 ^ bw) & c0;
+ b1 ^= (b1 ^ bw) & c1;
+ c1 = c0;
+ c0 &= (((aw | bw) + 0x7FFFFFFF) >> 31) - (uint32_t)1;
+ }
+
+ /*
+ * If c1 = 0, then we grabbed two words for a and b.
+ * If c1 != 0 but c0 = 0, then we grabbed one word. It
+ * is not possible that c1 != 0 and c0 != 0, because that
+ * would mean that both integers are zero.
+ */
+ a1 |= a0 & c1;
+ a0 &= ~c1;
+ b1 |= b0 & c1;
+ b0 &= ~c1;
+ a_hi = ((uint64_t)a0 << 31) + a1;
+ b_hi = ((uint64_t)b0 << 31) + b1;
+ a_lo = a[0];
+ b_lo = b[0];
+
+ /*
+ * Compute reduction factors:
+ *
+ * a' = a*pa + b*pb
+ * b' = a*qa + b*qb
+ *
+ * such that a' and b' are both multiple of 2^31, but are
+ * only marginally larger than a and b.
+ */
+ pa = 1;
+ pb = 0;
+ qa = 0;
+ qb = 1;
+ for (i = 0; i < 31; i ++) {
+ /*
+ * At each iteration:
+ *
+ * a <- (a-b)/2 if: a is odd, b is odd, a_hi > b_hi
+ * b <- (b-a)/2 if: a is odd, b is odd, a_hi <= b_hi
+ * a <- a/2 if: a is even
+ * b <- b/2 if: a is odd, b is even
+ *
+ * We multiply a_lo and b_lo by 2 at each
+ * iteration, thus a division by 2 really is a
+ * non-multiplication by 2.
+ */
+ uint32_t r, oa, ob, cAB, cBA, cA;
+ uint64_t rz;
+
+ /*
+ * r = GT(a_hi, b_hi)
+ * But the GT() function works on uint32_t operands,
+ * so we inline a 64-bit version here.
+ */
+ rz = b_hi - a_hi;
+ r = (uint32_t)((rz ^ ((a_hi ^ b_hi)
+ & (a_hi ^ rz))) >> 63);
+
+ /*
+ * cAB = 1 if b must be subtracted from a
+ * cBA = 1 if a must be subtracted from b
+ * cA = 1 if a is divided by 2, 0 otherwise
+ *
+ * Rules:
+ *
+ * cAB and cBA cannot be both 1.
+ * if a is not divided by 2, b is.
+ */
+ oa = (a_lo >> i) & 1;
+ ob = (b_lo >> i) & 1;
+ cAB = oa & ob & r;
+ cBA = oa & ob & NOT(r);
+ cA = cAB | NOT(oa);
+
+ /*
+ * Conditional subtractions.
+ */
+ a_lo -= b_lo & -cAB;
+ a_hi -= b_hi & -(uint64_t)cAB;
+ pa -= qa & -(int64_t)cAB;
+ pb -= qb & -(int64_t)cAB;
+ b_lo -= a_lo & -cBA;
+ b_hi -= a_hi & -(uint64_t)cBA;
+ qa -= pa & -(int64_t)cBA;
+ qb -= pb & -(int64_t)cBA;
+
+ /*
+ * Shifting.
+ */
+ a_lo += a_lo & (cA - 1);
+ pa += pa & ((int64_t)cA - 1);
+ pb += pb & ((int64_t)cA - 1);
+ a_hi ^= (a_hi ^ (a_hi >> 1)) & -(uint64_t)cA;
+ b_lo += b_lo & -cA;
+ qa += qa & -(int64_t)cA;
+ qb += qb & -(int64_t)cA;
+ b_hi ^= (b_hi ^ (b_hi >> 1)) & ((uint64_t)cA - 1);
+ }
+
+ /*
+ * Replace a and b with new values a' and b'.
+ */
+ r = co_reduce(a, b, len, pa, pb, qa, qb);
+ pa -= pa * ((r & 1) << 1);
+ pb -= pb * ((r & 1) << 1);
+ qa -= qa * (r & 2);
+ qb -= qb * (r & 2);
+ co_reduce_mod(u, v, len, pa, pb, qa, qb, m + 1, m0i);
+ }
+
+ /*
+ * Now one of the arrays should be 0, and the other contains
+ * the GCD. If a is 0, then u is 0 as well, and v contains
+ * the division result.
+ * Result is correct if and only if GCD is 1.
+ */
+ r = (a[0] | b[0]) ^ 1;
+ u[0] |= v[0];
+ for (k = 1; k < len; k ++) {
+ r |= a[k] | b[k];
+ u[k] |= v[k];
+ }
+ return EQ0(r);
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_modpow.c b/test/monniaux/BearSSL/src/int/i31_modpow.c
new file mode 100644
index 00000000..4ef3f5d5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_modpow.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_modpow(uint32_t *x,
+ const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2)
+{
+ size_t mlen;
+ uint32_t k;
+
+ /*
+ * 'mlen' is the length of m[] expressed in bytes (including
+ * the "bit length" first field).
+ */
+ mlen = ((m[0] + 63) >> 5) * sizeof m[0];
+
+ /*
+ * Throughout the algorithm:
+ * -- t1[] is in Montgomery representation; it contains x, x^2,
+ * x^4, x^8...
+ * -- The result is accumulated, in normal representation, in
+ * the x[] array.
+ * -- t2[] is used as destination buffer for each multiplication.
+ *
+ * Note that there is no need to call br_i32_from_monty().
+ */
+ memcpy(t1, x, mlen);
+ br_i31_to_monty(t1, m);
+ br_i31_zero(x, m[0]);
+ x[1] = 1;
+ for (k = 0; k < ((uint32_t)elen << 3); k ++) {
+ uint32_t ctl;
+
+ ctl = (e[elen - 1 - (k >> 3)] >> (k & 7)) & 1;
+ br_i31_montymul(t2, x, t1, m, m0i);
+ CCOPY(ctl, x, t2, mlen);
+ br_i31_montymul(t2, t1, t1, m, m0i);
+ memcpy(t1, t2, mlen);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_modpow2.c b/test/monniaux/BearSSL/src/int/i31_modpow2.c
new file mode 100644
index 00000000..0b8f8cf7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_modpow2.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_modpow_opt(uint32_t *x,
+ const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen)
+{
+ size_t mlen, mwlen;
+ uint32_t *t1, *t2, *base;
+ size_t u, v;
+ uint32_t acc;
+ int acc_len, win_len;
+
+ /*
+ * Get modulus size.
+ */
+ mwlen = (m[0] + 63) >> 5;
+ mlen = mwlen * sizeof m[0];
+ mwlen += (mwlen & 1);
+ t1 = tmp;
+ t2 = tmp + mwlen;
+
+ /*
+ * Compute possible window size, with a maximum of 5 bits.
+ * When the window has size 1 bit, we use a specific code
+ * that requires only two temporaries. Otherwise, for a
+ * window of k bits, we need 2^k+1 temporaries.
+ */
+ if (twlen < (mwlen << 1)) {
+ return 0;
+ }
+ for (win_len = 5; win_len > 1; win_len --) {
+ if ((((uint32_t)1 << win_len) + 1) * mwlen <= twlen) {
+ break;
+ }
+ }
+
+ /*
+ * Everything is done in Montgomery representation.
+ */
+ br_i31_to_monty(x, m);
+
+ /*
+ * Compute window contents. If the window has size one bit only,
+ * then t2 is set to x; otherwise, t2[0] is left untouched, and
+ * t2[k] is set to x^k (for k >= 1).
+ */
+ if (win_len == 1) {
+ memcpy(t2, x, mlen);
+ } else {
+ memcpy(t2 + mwlen, x, mlen);
+ base = t2 + mwlen;
+ for (u = 2; u < ((unsigned)1 << win_len); u ++) {
+ br_i31_montymul(base + mwlen, base, x, m, m0i);
+ base += mwlen;
+ }
+ }
+
+ /*
+ * We need to set x to 1, in Montgomery representation. This can
+ * be done efficiently by setting the high word to 1, then doing
+ * one word-sized shift.
+ */
+ br_i31_zero(x, m[0]);
+ x[(m[0] + 31) >> 5] = 1;
+ br_i31_muladd_small(x, 0, m);
+
+ /*
+ * We process bits from most to least significant. At each
+ * loop iteration, we have acc_len bits in acc.
+ */
+ acc = 0;
+ acc_len = 0;
+ while (acc_len > 0 || elen > 0) {
+ int i, k;
+ uint32_t bits;
+
+ /*
+ * Get the next bits.
+ */
+ k = win_len;
+ if (acc_len < win_len) {
+ if (elen > 0) {
+ acc = (acc << 8) | *e ++;
+ elen --;
+ acc_len += 8;
+ } else {
+ k = acc_len;
+ }
+ }
+ bits = (acc >> (acc_len - k)) & (((uint32_t)1 << k) - 1);
+ acc_len -= k;
+
+ /*
+ * We could get exactly k bits. Compute k squarings.
+ */
+ for (i = 0; i < k; i ++) {
+ br_i31_montymul(t1, x, x, m, m0i);
+ memcpy(x, t1, mlen);
+ }
+
+ /*
+ * Window lookup: we want to set t2 to the window
+ * lookup value, assuming the bits are non-zero. If
+ * the window length is 1 bit only, then t2 is
+ * already set; otherwise, we do a constant-time lookup.
+ */
+ if (win_len > 1) {
+ br_i31_zero(t2, m[0]);
+ base = t2 + mwlen;
+ for (u = 1; u < ((uint32_t)1 << k); u ++) {
+ uint32_t mask;
+
+ mask = -EQ(u, bits);
+ for (v = 1; v < mwlen; v ++) {
+ t2[v] |= mask & base[v];
+ }
+ base += mwlen;
+ }
+ }
+
+ /*
+ * Multiply with the looked-up value. We keep the
+ * product only if the exponent bits are not all-zero.
+ */
+ br_i31_montymul(t1, x, t2, m, m0i);
+ CCOPY(NEQ(bits, 0), x, t1, mlen);
+ }
+
+ /*
+ * Convert back from Montgomery representation, and exit.
+ */
+ br_i31_from_monty(x, m, m0i);
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_montmul.c b/test/monniaux/BearSSL/src/int/i31_montmul.c
new file mode 100644
index 00000000..758f8f42
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_montmul.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i)
+{
+ /*
+ * Each outer loop iteration computes:
+ * d <- (d + xu*y + f*m) / 2^31
+ * We have xu <= 2^31-1 and f <= 2^31-1.
+ * Thus, if d <= 2*m-1 on input, then:
+ * 2*m-1 + 2*(2^31-1)*m <= (2^32)*m-1
+ * and the new d value is less than 2*m.
+ *
+ * We represent d over 31-bit words, with an extra word 'dh'
+ * which can thus be only 0 or 1.
+ */
+ size_t len, len4, u, v;
+ uint32_t dh;
+
+ len = (m[0] + 31) >> 5;
+ len4 = len & ~(size_t)3;
+ br_i31_zero(d, m[0]);
+ dh = 0;
+ for (u = 0; u < len; u ++) {
+ /*
+ * The carry for each operation fits on 32 bits:
+ * d[v+1] <= 2^31-1
+ * xu*y[v+1] <= (2^31-1)*(2^31-1)
+ * f*m[v+1] <= (2^31-1)*(2^31-1)
+ * r <= 2^32-1
+ * (2^31-1) + 2*(2^31-1)*(2^31-1) + (2^32-1) = 2^63 - 2^31
+ * After division by 2^31, the new r is then at most 2^32-1
+ *
+ * Using a 32-bit carry has performance benefits on 32-bit
+ * systems; however, on 64-bit architectures, we prefer to
+ * keep the carry (r) in a 64-bit register, thus avoiding some
+ * "clear high bits" operations.
+ */
+ uint32_t f, xu;
+#if BR_64
+ uint64_t r;
+#else
+ uint32_t r;
+#endif
+
+ xu = x[u + 1];
+ f = MUL31_lo((d[1] + MUL31_lo(x[u + 1], y[1])), m0i);
+
+ r = 0;
+ for (v = 0; v < len4; v += 4) {
+ uint64_t z;
+
+ z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])
+ + MUL31(f, m[v + 1]) + r;
+ r = z >> 31;
+ d[v + 0] = (uint32_t)z & 0x7FFFFFFF;
+ z = (uint64_t)d[v + 2] + MUL31(xu, y[v + 2])
+ + MUL31(f, m[v + 2]) + r;
+ r = z >> 31;
+ d[v + 1] = (uint32_t)z & 0x7FFFFFFF;
+ z = (uint64_t)d[v + 3] + MUL31(xu, y[v + 3])
+ + MUL31(f, m[v + 3]) + r;
+ r = z >> 31;
+ d[v + 2] = (uint32_t)z & 0x7FFFFFFF;
+ z = (uint64_t)d[v + 4] + MUL31(xu, y[v + 4])
+ + MUL31(f, m[v + 4]) + r;
+ r = z >> 31;
+ d[v + 3] = (uint32_t)z & 0x7FFFFFFF;
+ }
+ for (; v < len; v ++) {
+ uint64_t z;
+
+ z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])
+ + MUL31(f, m[v + 1]) + r;
+ r = z >> 31;
+ d[v] = (uint32_t)z & 0x7FFFFFFF;
+ }
+
+ /*
+ * Since the new dh can only be 0 or 1, the addition of
+ * the old dh with the carry MUST fit on 32 bits, and
+ * thus can be done into dh itself.
+ */
+ dh += r;
+ d[len] = dh & 0x7FFFFFFF;
+ dh >>= 31;
+ }
+
+ /*
+ * We must write back the bit length because it was overwritten in
+ * the loop (not overwriting it would require a test in the loop,
+ * which would yield bigger and slower code).
+ */
+ d[0] = m[0];
+
+ /*
+ * d[] may still be greater than m[] at that point; notably, the
+ * 'dh' word may be non-zero.
+ */
+ br_i31_sub(d, m, NEQ(dh, 0) | NOT(br_i31_sub(d, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_mulacc.c b/test/monniaux/BearSSL/src/int/i31_mulacc.c
new file mode 100644
index 00000000..7410e546
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_mulacc.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ size_t alen, blen, u;
+ uint32_t dl, dh;
+
+ alen = (a[0] + 31) >> 5;
+ blen = (b[0] + 31) >> 5;
+
+ /*
+ * We want to add the two bit lengths, but these are encoded,
+ * which requires some extra care.
+ */
+ dl = (a[0] & 31) + (b[0] & 31);
+ dh = (a[0] >> 5) + (b[0] >> 5);
+ d[0] = (dh << 5) + dl + (~(uint32_t)(dl - 31) >> 31);
+
+ for (u = 0; u < blen; u ++) {
+ uint32_t f;
+ size_t v;
+
+ /*
+ * Carry always fits on 31 bits; we want to keep it in a
+ * 32-bit register on 32-bit architectures (on a 64-bit
+ * architecture, cast down from 64 to 32 bits means
+ * clearing the high bits, which is not free; on a 32-bit
+ * architecture, the same operation really means ignoring
+ * the top register, which has negative or zero cost).
+ */
+#if BR_64
+ uint64_t cc;
+#else
+ uint32_t cc;
+#endif
+
+ f = b[1 + u];
+ cc = 0;
+ for (v = 0; v < alen; v ++) {
+ uint64_t z;
+
+ z = (uint64_t)d[1 + u + v] + MUL31(f, a[1 + v]) + cc;
+ cc = z >> 31;
+ d[1 + u + v] = (uint32_t)z & 0x7FFFFFFF;
+ }
+ d[1 + u + alen] = (uint32_t)cc;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_muladd.c b/test/monniaux/BearSSL/src/int/i31_muladd.c
new file mode 100644
index 00000000..eecd9e2c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_muladd.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)
+{
+ uint32_t m_bitlen;
+ unsigned mblr;
+ size_t u, mlen;
+ uint32_t a0, a1, b0, hi, g, q, tb;
+ uint32_t under, over;
+ uint32_t cc;
+
+ /*
+ * We can test on the modulus bit length since we accept to
+ * leak that length.
+ */
+ m_bitlen = m[0];
+ if (m_bitlen == 0) {
+ return;
+ }
+ if (m_bitlen <= 31) {
+ uint32_t lo;
+
+ hi = x[1] >> 1;
+ lo = (x[1] << 31) | z;
+ x[1] = br_rem(hi, lo, m[1]);
+ return;
+ }
+ mlen = (m_bitlen + 31) >> 5;
+ mblr = (unsigned)m_bitlen & 31;
+
+ /*
+ * Principle: we estimate the quotient (x*2^31+z)/m by
+ * doing a 64/32 division with the high words.
+ *
+ * Let:
+ * w = 2^31
+ * a = (w*a0 + a1) * w^N + a2
+ * b = b0 * w^N + b2
+ * such that:
+ * 0 <= a0 < w
+ * 0 <= a1 < w
+ * 0 <= a2 < w^N
+ * w/2 <= b0 < w
+ * 0 <= b2 < w^N
+ * a < w*b
+ * I.e. the two top words of a are a0:a1, the top word of b is
+ * b0, we ensured that b0 is "full" (high bit set), and a is
+ * such that the quotient q = a/b fits on one word (0 <= q < w).
+ *
+ * If a = b*q + r (with 0 <= r < q), we can estimate q by
+ * doing an Euclidean division on the top words:
+ * a0*w+a1 = b0*u + v (with 0 <= v < b0)
+ * Then the following holds:
+ * 0 <= u <= w
+ * u-2 <= q <= u
+ */
+ hi = x[mlen];
+ if (mblr == 0) {
+ a0 = x[mlen];
+ memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
+ x[1] = z;
+ a1 = x[mlen];
+ b0 = m[mlen];
+ } else {
+ a0 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
+ & 0x7FFFFFFF;
+ memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
+ x[1] = z;
+ a1 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
+ & 0x7FFFFFFF;
+ b0 = ((m[mlen] << (31 - mblr)) | (m[mlen - 1] >> mblr))
+ & 0x7FFFFFFF;
+ }
+
+ /*
+ * We estimate a divisor q. If the quotient returned by br_div()
+ * is g:
+ * -- If a0 == b0 then g == 0; we want q = 0x7FFFFFFF.
+ * -- Otherwise:
+ * -- if g == 0 then we set q = 0;
+ * -- otherwise, we set q = g - 1.
+ * The properties described above then ensure that the true
+ * quotient is q-1, q or q+1.
+ *
+ * Take care that a0, a1 and b0 are 31-bit words, not 32-bit. We
+ * must adjust the parameters to br_div() accordingly.
+ */
+ g = br_div(a0 >> 1, a1 | (a0 << 31), b0);
+ q = MUX(EQ(a0, b0), 0x7FFFFFFF, MUX(EQ(g, 0), 0, g - 1));
+
+ /*
+ * We subtract q*m from x (with the extra high word of value 'hi').
+ * Since q may be off by 1 (in either direction), we may have to
+ * add or subtract m afterwards.
+ *
+ * The 'tb' flag will be true (1) at the end of the loop if the
+ * result is greater than or equal to the modulus (not counting
+ * 'hi' or the carry).
+ */
+ cc = 0;
+ tb = 1;
+ for (u = 1; u <= mlen; u ++) {
+ uint32_t mw, zw, xw, nxw;
+ uint64_t zl;
+
+ mw = m[u];
+ zl = MUL31(mw, q) + cc;
+ cc = (uint32_t)(zl >> 31);
+ zw = (uint32_t)zl & (uint32_t)0x7FFFFFFF;
+ xw = x[u];
+ nxw = xw - zw;
+ cc += nxw >> 31;
+ nxw &= 0x7FFFFFFF;
+ x[u] = nxw;
+ tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
+ }
+
+ /*
+ * If we underestimated q, then either cc < hi (one extra bit
+ * beyond the top array word), or cc == hi and tb is true (no
+ * extra bit, but the result is not lower than the modulus). In
+ * these cases we must subtract m once.
+ *
+ * Otherwise, we may have overestimated, which will show as
+ * cc > hi (thus a negative result). Correction is adding m once.
+ */
+ over = GT(cc, hi);
+ under = ~over & (tb | LT(cc, hi));
+ br_i31_add(x, m, over);
+ br_i31_sub(x, m, under);
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_ninv31.c b/test/monniaux/BearSSL/src/int/i31_ninv31.c
new file mode 100644
index 00000000..dd83c96a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_ninv31.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_ninv31(uint32_t x)
+{
+ uint32_t y;
+
+ y = 2 - x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ return MUX(x & 1, -y, 0) & 0x7FFFFFFF;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_reduce.c b/test/monniaux/BearSSL/src/int/i31_reduce.c
new file mode 100644
index 00000000..5c9523ed
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_reduce.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m)
+{
+ uint32_t m_bitlen, a_bitlen;
+ size_t mlen, alen, u;
+
+ m_bitlen = m[0];
+ mlen = (m_bitlen + 31) >> 5;
+
+ x[0] = m_bitlen;
+ if (m_bitlen == 0) {
+ return;
+ }
+
+ /*
+ * If the source is shorter, then simply copy all words from a[]
+ * and zero out the upper words.
+ */
+ a_bitlen = a[0];
+ alen = (a_bitlen + 31) >> 5;
+ if (a_bitlen < m_bitlen) {
+ memcpy(x + 1, a + 1, alen * sizeof *a);
+ for (u = alen; u < mlen; u ++) {
+ x[u + 1] = 0;
+ }
+ return;
+ }
+
+ /*
+ * The source length is at least equal to that of the modulus.
+ * We must thus copy N-1 words, and input the remaining words
+ * one by one.
+ */
+ memcpy(x + 1, a + 2 + (alen - mlen), (mlen - 1) * sizeof *a);
+ x[mlen] = 0;
+ for (u = 1 + alen - mlen; u > 0; u --) {
+ br_i31_muladd_small(x, a[u], m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_rshift.c b/test/monniaux/BearSSL/src/int/i31_rshift.c
new file mode 100644
index 00000000..db6ba0b8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_rshift.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_rshift(uint32_t *x, int count)
+{
+ size_t u, len;
+ uint32_t r;
+
+ len = (x[0] + 31) >> 5;
+ if (len == 0) {
+ return;
+ }
+ r = x[1] >> count;
+ for (u = 2; u <= len; u ++) {
+ uint32_t w;
+
+ w = x[u];
+ x[u - 1] = ((w << (31 - count)) | r) & 0x7FFFFFFF;
+ r = w >> count;
+ }
+ x[len] = r;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_sub.c b/test/monniaux/BearSSL/src/int/i31_sub.c
new file mode 100644
index 00000000..39108951
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_sub.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 63) >> 5;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw - bw - cc;
+ cc = naw >> 31;
+ a[u] = MUX(ctl, naw & 0x7FFFFFFF, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i31_tmont.c b/test/monniaux/BearSSL/src/int/i31_tmont.c
new file mode 100644
index 00000000..4798ff65
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i31_tmont.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i31_to_monty(uint32_t *x, const uint32_t *m)
+{
+ uint32_t k;
+
+ for (k = (m[0] + 31) >> 5; k > 0; k --) {
+ br_i31_muladd_small(x, 0, m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_add.c b/test/monniaux/BearSSL/src/int/i32_add.c
new file mode 100644
index 00000000..620baffd
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_add.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_add(uint32_t *a, const uint32_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 63) >> 5;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw + bw + cc;
+
+ /*
+ * Carry is 1 if naw < aw. Carry is also 1 if naw == aw
+ * AND the carry was already 1.
+ */
+ cc = (cc & EQ(naw, aw)) | LT(naw, aw);
+ a[u] = MUX(ctl, naw, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_bitlen.c b/test/monniaux/BearSSL/src/int/i32_bitlen.c
new file mode 100644
index 00000000..40ce9fa0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_bitlen.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_bit_length(uint32_t *x, size_t xlen)
+{
+ uint32_t tw, twk;
+
+ tw = 0;
+ twk = 0;
+ while (xlen -- > 0) {
+ uint32_t w, c;
+
+ c = EQ(tw, 0);
+ w = x[xlen];
+ tw = MUX(c, w, tw);
+ twk = MUX(c, (uint32_t)xlen, twk);
+ }
+ return (twk << 5) + BIT_LENGTH(tw);
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_decmod.c b/test/monniaux/BearSSL/src/int/i32_decmod.c
new file mode 100644
index 00000000..a859af12
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_decmod.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_decode_mod(uint32_t *x, const void *src, size_t len, const uint32_t *m)
+{
+ const unsigned char *buf;
+ uint32_t r;
+ size_t u, v, mlen;
+
+ buf = src;
+
+ /*
+ * First pass: determine whether the value fits. The 'r' value
+ * will contain the comparison result, as 0x00000000 (value is
+ * equal to the modulus), 0x00000001 (value is greater than the
+ * modulus), or 0xFFFFFFFF (value is lower than the modulus).
+ */
+ mlen = (m[0] + 7) >> 3;
+ r = 0;
+ for (u = (mlen > len) ? mlen : len; u > 0; u --) {
+ uint32_t mb, xb;
+
+ v = u - 1;
+ if (v >= mlen) {
+ mb = 0;
+ } else {
+ mb = (m[1 + (v >> 2)] >> ((v & 3) << 3)) & 0xFF;
+ }
+ if (v >= len) {
+ xb = 0;
+ } else {
+ xb = buf[len - u];
+ }
+ r = MUX(EQ(r, 0), (uint32_t)CMP(xb, mb), r);
+ }
+
+ /*
+ * Only r == 0xFFFFFFFF is acceptable. We want to set r to 0xFF if
+ * the value fits, 0x00 otherwise.
+ */
+ r >>= 24;
+ br_i32_zero(x, m[0]);
+ u = (mlen > len) ? len : mlen;
+ while (u > 0) {
+ uint32_t xb;
+
+ xb = buf[len - u] & r;
+ u --;
+ x[1 + (u >> 2)] |= xb << ((u & 3) << 3);
+ }
+ return r >> 7;
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_decode.c b/test/monniaux/BearSSL/src/int/i32_decode.c
new file mode 100644
index 00000000..f2890384
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_decode.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_decode(uint32_t *x, const void *src, size_t len)
+{
+ const unsigned char *buf;
+ size_t u, v;
+
+ buf = src;
+ u = len;
+ v = 1;
+ for (;;) {
+ if (u < 4) {
+ uint32_t w;
+
+ if (u < 2) {
+ if (u == 0) {
+ break;
+ } else {
+ w = buf[0];
+ }
+ } else {
+ if (u == 2) {
+ w = br_dec16be(buf);
+ } else {
+ w = ((uint32_t)buf[0] << 16)
+ | br_dec16be(buf + 1);
+ }
+ }
+ x[v ++] = w;
+ break;
+ } else {
+ u -= 4;
+ x[v ++] = br_dec32be(buf + u);
+ }
+ }
+ x[0] = br_i32_bit_length(x + 1, v - 1);
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_decred.c b/test/monniaux/BearSSL/src/int/i32_decred.c
new file mode 100644
index 00000000..dc476db0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_decred.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_decode_reduce(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m)
+{
+ uint32_t m_bitlen;
+ size_t mblen, k, q;
+ const unsigned char *buf;
+
+ m_bitlen = m[0];
+
+ /*
+ * Special case for an invalid modulus.
+ */
+ if (m_bitlen == 0) {
+ x[0] = 0;
+ return;
+ }
+
+ /*
+ * Clear the destination.
+ */
+ br_i32_zero(x, m_bitlen);
+
+ /*
+ * First decode directly as many bytes as possible without
+ * reduction, taking care to leave a number of bytes which
+ * is a multiple of 4.
+ */
+ mblen = (m_bitlen + 7) >> 3;
+ k = mblen - 1;
+
+ /*
+ * Up to k bytes can be safely decoded.
+ */
+ if (k >= len) {
+ br_i32_decode(x, src, len);
+ x[0] = m_bitlen;
+ return;
+ }
+
+ /*
+ * We want to first inject some bytes with direct decoding,
+ * then extra bytes by whole 32-bit words. First compute
+ * the size that should be injected that way.
+ */
+ buf = src;
+ q = (len - k + 3) & ~(size_t)3;
+
+ /*
+ * It may happen that this is more than what we already have
+ * (by at most 3 bytes). Such a case may happen only with
+ * a very short modulus. In that case, we must process the first
+ * bytes "manually".
+ */
+ if (q > len) {
+ int i;
+ uint32_t w;
+
+ w = 0;
+ for (i = 0; i < 4; i ++) {
+ w <<= 8;
+ if (q <= len) {
+ w |= buf[len - q];
+ }
+ q --;
+ }
+ br_i32_muladd_small(x, w, m);
+ } else {
+ br_i32_decode(x, buf, len - q);
+ x[0] = m_bitlen;
+ }
+
+ /*
+ * At that point, we have exactly q bytes to inject, and q is
+ * a multiple of 4.
+ */
+ for (k = len - q; k < len; k += 4) {
+ br_i32_muladd_small(x, br_dec32be(buf + k), m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_div32.c b/test/monniaux/BearSSL/src/int/i32_div32.c
new file mode 100644
index 00000000..d8b8023d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_div32.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r)
+{
+ /* TODO: optimize this */
+ uint32_t q;
+ uint32_t ch, cf;
+ int k;
+
+ q = 0;
+ ch = EQ(hi, d);
+ hi = MUX(ch, 0, hi);
+ for (k = 31; k > 0; k --) {
+ int j;
+ uint32_t w, ctl, hi2, lo2;
+
+ j = 32 - k;
+ w = (hi << j) | (lo >> k);
+ ctl = GE(w, d) | (hi >> k);
+ hi2 = (w - d) >> j;
+ lo2 = lo - (d << k);
+ hi = MUX(ctl, hi2, hi);
+ lo = MUX(ctl, lo2, lo);
+ q |= ctl << k;
+ }
+ cf = GE(lo, d) | hi;
+ q |= cf;
+ *r = MUX(cf, lo - d, lo);
+ return q;
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_encode.c b/test/monniaux/BearSSL/src/int/i32_encode.c
new file mode 100644
index 00000000..303652f9
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_encode.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_encode(void *dst, size_t len, const uint32_t *x)
+{
+ unsigned char *buf;
+ size_t k;
+
+ buf = dst;
+
+ /*
+ * Compute the announced size of x in bytes; extra bytes are
+ * filled with zeros.
+ */
+ k = (x[0] + 7) >> 3;
+ while (len > k) {
+ *buf ++ = 0;
+ len --;
+ }
+
+ /*
+ * Now we use k as index within x[]. That index starts at 1;
+ * we initialize it to the topmost complete word, and process
+ * any remaining incomplete word.
+ */
+ k = (len + 3) >> 2;
+ switch (len & 3) {
+ case 3:
+ *buf ++ = x[k] >> 16;
+ /* fall through */
+ case 2:
+ *buf ++ = x[k] >> 8;
+ /* fall through */
+ case 1:
+ *buf ++ = x[k];
+ k --;
+ }
+
+ /*
+ * Encode all complete words.
+ */
+ while (k > 0) {
+ br_enc32be(buf, x[k]);
+ k --;
+ buf += 4;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_fmont.c b/test/monniaux/BearSSL/src/int/i32_fmont.c
new file mode 100644
index 00000000..dc1c9344
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_fmont.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i)
+{
+ size_t len, u, v;
+
+ len = (m[0] + 31) >> 5;
+ for (u = 0; u < len; u ++) {
+ uint32_t f;
+ uint64_t cc;
+
+ f = x[1] * m0i;
+ cc = 0;
+ for (v = 0; v < len; v ++) {
+ uint64_t z;
+
+ z = (uint64_t)x[v + 1] + MUL(f, m[v + 1]) + cc;
+ cc = z >> 32;
+ if (v != 0) {
+ x[v] = (uint32_t)z;
+ }
+ }
+ x[len] = (uint32_t)cc;
+ }
+
+ /*
+ * We may have to do an extra subtraction, but only if the
+ * value in x[] is indeed greater than or equal to that of m[],
+ * which is why we must do two calls (first call computes the
+ * carry, second call performs the subtraction only if the carry
+ * is 0).
+ */
+ br_i32_sub(x, m, NOT(br_i32_sub(x, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_iszero.c b/test/monniaux/BearSSL/src/int/i32_iszero.c
new file mode 100644
index 00000000..659df7f2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_iszero.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_iszero(const uint32_t *x)
+{
+ uint32_t z;
+ size_t u;
+
+ z = 0;
+ for (u = (x[0] + 31) >> 5; u > 0; u --) {
+ z |= x[u];
+ }
+ return ~(z | -z) >> 31;
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_modpow.c b/test/monniaux/BearSSL/src/int/i32_modpow.c
new file mode 100644
index 00000000..034aba06
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_modpow.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_modpow(uint32_t *x,
+ const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2)
+{
+ size_t mlen;
+ uint32_t k;
+
+ /*
+ * 'mlen' is the length of m[] expressed in bytes (including
+ * the "bit length" first field).
+ */
+ mlen = ((m[0] + 63) >> 5) * sizeof m[0];
+
+ /*
+ * Throughout the algorithm:
+ * -- t1[] is in Montgomery representation; it contains x, x^2,
+ * x^4, x^8...
+ * -- The result is accumulated, in normal representation, in
+ * the x[] array.
+ * -- t2[] is used as destination buffer for each multiplication.
+ *
+ * Note that there is no need to call br_i32_from_monty().
+ */
+ memcpy(t1, x, mlen);
+ br_i32_to_monty(t1, m);
+ br_i32_zero(x, m[0]);
+ x[1] = 1;
+ for (k = 0; k < ((uint32_t)elen << 3); k ++) {
+ uint32_t ctl;
+
+ ctl = (e[elen - 1 - (k >> 3)] >> (k & 7)) & 1;
+ br_i32_montymul(t2, x, t1, m, m0i);
+ CCOPY(ctl, x, t2, mlen);
+ br_i32_montymul(t2, t1, t1, m, m0i);
+ memcpy(t1, t2, mlen);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_montmul.c b/test/monniaux/BearSSL/src/int/i32_montmul.c
new file mode 100644
index 00000000..7edb376c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_montmul.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i)
+{
+ size_t len, u, v;
+ uint64_t dh;
+
+ len = (m[0] + 31) >> 5;
+ br_i32_zero(d, m[0]);
+ dh = 0;
+ for (u = 0; u < len; u ++) {
+ uint32_t f, xu;
+ uint64_t r1, r2, zh;
+
+ xu = x[u + 1];
+ f = (d[1] + x[u + 1] * y[1]) * m0i;
+ r1 = 0;
+ r2 = 0;
+ for (v = 0; v < len; v ++) {
+ uint64_t z;
+ uint32_t t;
+
+ z = (uint64_t)d[v + 1] + MUL(xu, y[v + 1]) + r1;
+ r1 = z >> 32;
+ t = (uint32_t)z;
+ z = (uint64_t)t + MUL(f, m[v + 1]) + r2;
+ r2 = z >> 32;
+ if (v != 0) {
+ d[v] = (uint32_t)z;
+ }
+ }
+ zh = dh + r1 + r2;
+ d[len] = (uint32_t)zh;
+ dh = zh >> 32;
+ }
+
+ /*
+ * d[] may still be greater than m[] at that point; notably, the
+ * 'dh' word may be non-zero.
+ */
+ br_i32_sub(d, m, NEQ(dh, 0) | NOT(br_i32_sub(d, m, 0)));
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_mulacc.c b/test/monniaux/BearSSL/src/int/i32_mulacc.c
new file mode 100644
index 00000000..55da3858
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_mulacc.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b)
+{
+ size_t alen, blen, u;
+
+ alen = (a[0] + 31) >> 5;
+ blen = (b[0] + 31) >> 5;
+ d[0] = a[0] + b[0];
+ for (u = 0; u < blen; u ++) {
+ uint32_t f;
+ size_t v;
+#if BR_64
+ uint64_t cc;
+#else
+ uint32_t cc;
+#endif
+
+ f = b[1 + u];
+ cc = 0;
+ for (v = 0; v < alen; v ++) {
+ uint64_t z;
+
+ z = (uint64_t)d[1 + u + v] + MUL(f, a[1 + v]) + cc;
+ cc = z >> 32;
+ d[1 + u + v] = (uint32_t)z;
+ }
+ d[1 + u + alen] = (uint32_t)cc;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_muladd.c b/test/monniaux/BearSSL/src/int/i32_muladd.c
new file mode 100644
index 00000000..dd526ad5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_muladd.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)
+{
+ uint32_t m_bitlen;
+ size_t u, mlen;
+ uint32_t a0, a1, b0, hi, g, q, tb;
+ uint32_t chf, clow, under, over;
+ uint64_t cc;
+
+ /*
+ * We can test on the modulus bit length since we accept to
+ * leak that length.
+ */
+ m_bitlen = m[0];
+ if (m_bitlen == 0) {
+ return;
+ }
+ if (m_bitlen <= 32) {
+ x[1] = br_rem(x[1], z, m[1]);
+ return;
+ }
+ mlen = (m_bitlen + 31) >> 5;
+
+ /*
+ * Principle: we estimate the quotient (x*2^32+z)/m by
+ * doing a 64/32 division with the high words.
+ *
+ * Let:
+ * w = 2^32
+ * a = (w*a0 + a1) * w^N + a2
+ * b = b0 * w^N + b2
+ * such that:
+ * 0 <= a0 < w
+ * 0 <= a1 < w
+ * 0 <= a2 < w^N
+ * w/2 <= b0 < w
+ * 0 <= b2 < w^N
+ * a < w*b
+ * I.e. the two top words of a are a0:a1, the top word of b is
+ * b0, we ensured that b0 is "full" (high bit set), and a is
+ * such that the quotient q = a/b fits on one word (0 <= q < w).
+ *
+ * If a = b*q + r (with 0 <= r < q), we can estimate q by
+ * doing an Euclidean division on the top words:
+ * a0*w+a1 = b0*u + v (with 0 <= v < w)
+ * Then the following holds:
+ * 0 <= u <= w
+ * u-2 <= q <= u
+ */
+ a0 = br_i32_word(x, m_bitlen - 32);
+ hi = x[mlen];
+ memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
+ x[1] = z;
+ a1 = br_i32_word(x, m_bitlen - 32);
+ b0 = br_i32_word(m, m_bitlen - 32);
+
+ /*
+ * We estimate a divisor q. If the quotient returned by br_div()
+ * is g:
+ * -- If a0 == b0 then g == 0; we want q = 0xFFFFFFFF.
+ * -- Otherwise:
+ * -- if g == 0 then we set q = 0;
+ * -- otherwise, we set q = g - 1.
+ * The properties described above then ensure that the true
+ * quotient is q-1, q or q+1.
+ */
+ g = br_div(a0, a1, b0);
+ q = MUX(EQ(a0, b0), 0xFFFFFFFF, MUX(EQ(g, 0), 0, g - 1));
+
+ /*
+ * We subtract q*m from x (with the extra high word of value 'hi').
+ * Since q may be off by 1 (in either direction), we may have to
+ * add or subtract m afterwards.
+ *
+ * The 'tb' flag will be true (1) at the end of the loop if the
+ * result is greater than or equal to the modulus (not counting
+ * 'hi' or the carry).
+ */
+ cc = 0;
+ tb = 1;
+ for (u = 1; u <= mlen; u ++) {
+ uint32_t mw, zw, xw, nxw;
+ uint64_t zl;
+
+ mw = m[u];
+ zl = MUL(mw, q) + cc;
+ cc = (uint32_t)(zl >> 32);
+ zw = (uint32_t)zl;
+ xw = x[u];
+ nxw = xw - zw;
+ cc += (uint64_t)GT(nxw, xw);
+ x[u] = nxw;
+ tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
+ }
+
+ /*
+ * If we underestimated q, then either cc < hi (one extra bit
+ * beyond the top array word), or cc == hi and tb is true (no
+ * extra bit, but the result is not lower than the modulus). In
+ * these cases we must subtract m once.
+ *
+ * Otherwise, we may have overestimated, which will show as
+ * cc > hi (thus a negative result). Correction is adding m once.
+ */
+ chf = (uint32_t)(cc >> 32);
+ clow = (uint32_t)cc;
+ over = chf | GT(clow, hi);
+ under = ~over & (tb | (~chf & LT(clow, hi)));
+ br_i32_add(x, m, over);
+ br_i32_sub(x, m, under);
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_ninv32.c b/test/monniaux/BearSSL/src/int/i32_ninv32.c
new file mode 100644
index 00000000..65644341
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_ninv32.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_ninv32(uint32_t x)
+{
+ uint32_t y;
+
+ y = 2 - x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ y *= 2 - y * x;
+ return MUX(x & 1, -y, 0);
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_reduce.c b/test/monniaux/BearSSL/src/int/i32_reduce.c
new file mode 100644
index 00000000..90fff092
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_reduce.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m)
+{
+ uint32_t m_bitlen, a_bitlen;
+ size_t mlen, alen, u;
+
+ m_bitlen = m[0];
+ mlen = (m_bitlen + 31) >> 5;
+
+ x[0] = m_bitlen;
+ if (m_bitlen == 0) {
+ return;
+ }
+
+ /*
+ * If the source is shorter, then simply copy all words from a[]
+ * and zero out the upper words.
+ */
+ a_bitlen = a[0];
+ alen = (a_bitlen + 31) >> 5;
+ if (a_bitlen < m_bitlen) {
+ memcpy(x + 1, a + 1, alen * sizeof *a);
+ for (u = alen; u < mlen; u ++) {
+ x[u + 1] = 0;
+ }
+ return;
+ }
+
+ /*
+ * The source length is at least equal to that of the modulus.
+ * We must thus copy N-1 words, and input the remaining words
+ * one by one.
+ */
+ memcpy(x + 1, a + 2 + (alen - mlen), (mlen - 1) * sizeof *a);
+ x[mlen] = 0;
+ for (u = 1 + alen - mlen; u > 0; u --) {
+ br_i32_muladd_small(x, a[u], m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_sub.c b/test/monniaux/BearSSL/src/int/i32_sub.c
new file mode 100644
index 00000000..9c500238
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_sub.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_i32_sub(uint32_t *a, const uint32_t *b, uint32_t ctl)
+{
+ uint32_t cc;
+ size_t u, m;
+
+ cc = 0;
+ m = (a[0] + 63) >> 5;
+ for (u = 1; u < m; u ++) {
+ uint32_t aw, bw, naw;
+
+ aw = a[u];
+ bw = b[u];
+ naw = aw - bw - cc;
+
+ /*
+ * Carry is 1 if naw > aw. Carry is 1 also if naw == aw
+ * AND the carry was already 1.
+ */
+ cc = (cc & EQ(naw, aw)) | GT(naw, aw);
+ a[u] = MUX(ctl, naw, aw);
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/int/i32_tmont.c b/test/monniaux/BearSSL/src/int/i32_tmont.c
new file mode 100644
index 00000000..058cd886
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i32_tmont.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_i32_to_monty(uint32_t *x, const uint32_t *m)
+{
+ uint32_t k;
+
+ for (k = (m[0] + 31) >> 5; k > 0; k --) {
+ br_i32_muladd_small(x, 0, m);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/int/i62_modpow2.c b/test/monniaux/BearSSL/src/int/i62_modpow2.c
new file mode 100644
index 00000000..2db537f0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/int/i62_modpow2.c
@@ -0,0 +1,493 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_INT128
+
+/*
+ * Compute x*y+v1+v2. Operands are 64-bit, and result is 128-bit, with
+ * high word in "hi" and low word in "lo".
+ */
+#define FMA1(hi, lo, x, y, v1, v2) do { \
+ unsigned __int128 fmaz; \
+ fmaz = (unsigned __int128)(x) * (unsigned __int128)(y) \
+ + (unsigned __int128)(v1) + (unsigned __int128)(v2); \
+ (hi) = (uint64_t)(fmaz >> 64); \
+ (lo) = (uint64_t)fmaz; \
+ } while (0)
+
+/*
+ * Compute x1*y1+x2*y2+v1+v2. Operands are 64-bit, and result is 128-bit,
+ * with high word in "hi" and low word in "lo".
+ *
+ * Callers should ensure that the two inner products, and the v1 and v2
+ * operands, are multiple of 4 (this is not used by this specific definition
+ * but may help other implementations).
+ */
+#define FMA2(hi, lo, x1, y1, x2, y2, v1, v2) do { \
+ unsigned __int128 fmaz; \
+ fmaz = (unsigned __int128)(x1) * (unsigned __int128)(y1) \
+ + (unsigned __int128)(x2) * (unsigned __int128)(y2) \
+ + (unsigned __int128)(v1) + (unsigned __int128)(v2); \
+ (hi) = (uint64_t)(fmaz >> 64); \
+ (lo) = (uint64_t)fmaz; \
+ } while (0)
+
+#elif BR_UMUL128
+
+#include <intrin.h>
+
+#define FMA1(hi, lo, x, y, v1, v2) do { \
+ uint64_t fmahi, fmalo; \
+ unsigned char fmacc; \
+ fmalo = _umul128((x), (y), &fmahi); \
+ fmacc = _addcarry_u64(0, fmalo, (v1), &fmalo); \
+ _addcarry_u64(fmacc, fmahi, 0, &fmahi); \
+ fmacc = _addcarry_u64(0, fmalo, (v2), &(lo)); \
+ _addcarry_u64(fmacc, fmahi, 0, &(hi)); \
+ } while (0)
+
+/*
+ * Normally we should use _addcarry_u64() for FMA2 too, but it makes
+ * Visual Studio crash. Instead we use this version, which leverages
+ * the fact that the vx operands, and the products, are multiple of 4.
+ * This is unfortunately slower.
+ */
+#define FMA2(hi, lo, x1, y1, x2, y2, v1, v2) do { \
+ uint64_t fma1hi, fma1lo; \
+ uint64_t fma2hi, fma2lo; \
+ uint64_t fmatt; \
+ fma1lo = _umul128((x1), (y1), &fma1hi); \
+ fma2lo = _umul128((x2), (y2), &fma2hi); \
+ fmatt = (fma1lo >> 2) + (fma2lo >> 2) \
+ + ((v1) >> 2) + ((v2) >> 2); \
+ (lo) = fmatt << 2; \
+ (hi) = fma1hi + fma2hi + (fmatt >> 62); \
+ } while (0)
+
+/*
+ * The FMA2 macro definition we would prefer to use, but it triggers
+ * an internal compiler error in Visual Studio 2015.
+ *
+#define FMA2(hi, lo, x1, y1, x2, y2, v1, v2) do { \
+ uint64_t fma1hi, fma1lo; \
+ uint64_t fma2hi, fma2lo; \
+ unsigned char fmacc; \
+ fma1lo = _umul128((x1), (y1), &fma1hi); \
+ fma2lo = _umul128((x2), (y2), &fma2hi); \
+ fmacc = _addcarry_u64(0, fma1lo, (v1), &fma1lo); \
+ _addcarry_u64(fmacc, fma1hi, 0, &fma1hi); \
+ fmacc = _addcarry_u64(0, fma2lo, (v2), &fma2lo); \
+ _addcarry_u64(fmacc, fma2hi, 0, &fma2hi); \
+ fmacc = _addcarry_u64(0, fma1lo, fma2lo, &(lo)); \
+ _addcarry_u64(fmacc, fma1hi, fma2hi, &(hi)); \
+ } while (0)
+ */
+
+#endif
+
+#define MASK62 ((uint64_t)0x3FFFFFFFFFFFFFFF)
+#define MUL62_lo(x, y) (((uint64_t)(x) * (uint64_t)(y)) & MASK62)
+
+/*
+ * Subtract b from a, and return the final carry. If 'ctl32' is 0, then
+ * a[] is kept unmodified, but the final carry is still computed and
+ * returned.
+ */
+static uint32_t
+i62_sub(uint64_t *a, const uint64_t *b, size_t num, uint32_t ctl32)
+{
+ uint64_t cc, mask;
+ size_t u;
+
+ cc = 0;
+ ctl32 = -ctl32;
+ mask = (uint64_t)ctl32 | ((uint64_t)ctl32 << 32);
+ for (u = 0; u < num; u ++) {
+ uint64_t aw, bw, dw;
+
+ aw = a[u];
+ bw = b[u];
+ dw = aw - bw - cc;
+ cc = dw >> 63;
+ dw &= MASK62;
+ a[u] = aw ^ (mask & (dw ^ aw));
+ }
+ return (uint32_t)cc;
+}
+
+/*
+ * Montgomery multiplication, over arrays of 62-bit values. The
+ * destination array (d) must be distinct from the other operands
+ * (x, y and m). All arrays are in little-endian format (least
+ * significant word comes first) over 'num' words.
+ */
+static void
+montymul(uint64_t *d, const uint64_t *x, const uint64_t *y,
+ const uint64_t *m, size_t num, uint64_t m0i)
+{
+ uint64_t dh;
+ size_t u, num4;
+
+ num4 = 1 + ((num - 1) & ~(size_t)3);
+ memset(d, 0, num * sizeof *d);
+ dh = 0;
+ for (u = 0; u < num; u ++) {
+ size_t v;
+ uint64_t f, xu;
+ uint64_t r, zh;
+ uint64_t hi, lo;
+
+ xu = x[u] << 2;
+ f = MUL62_lo(d[0] + MUL62_lo(x[u], y[0]), m0i) << 2;
+
+ FMA2(hi, lo, xu, y[0], f, m[0], d[0] << 2, 0);
+ r = hi;
+
+ for (v = 1; v < num4; v += 4) {
+ FMA2(hi, lo, xu, y[v + 0],
+ f, m[v + 0], d[v + 0] << 2, r << 2);
+ r = hi + (r >> 62);
+ d[v - 1] = lo >> 2;
+ FMA2(hi, lo, xu, y[v + 1],
+ f, m[v + 1], d[v + 1] << 2, r << 2);
+ r = hi + (r >> 62);
+ d[v + 0] = lo >> 2;
+ FMA2(hi, lo, xu, y[v + 2],
+ f, m[v + 2], d[v + 2] << 2, r << 2);
+ r = hi + (r >> 62);
+ d[v + 1] = lo >> 2;
+ FMA2(hi, lo, xu, y[v + 3],
+ f, m[v + 3], d[v + 3] << 2, r << 2);
+ r = hi + (r >> 62);
+ d[v + 2] = lo >> 2;
+ }
+ for (; v < num; v ++) {
+ FMA2(hi, lo, xu, y[v], f, m[v], d[v] << 2, r << 2);
+ r = hi + (r >> 62);
+ d[v - 1] = lo >> 2;
+ }
+
+ zh = dh + r;
+ d[num - 1] = zh & MASK62;
+ dh = zh >> 62;
+ }
+ i62_sub(d, m, num, (uint32_t)dh | NOT(i62_sub(d, m, num, 0)));
+}
+
+/*
+ * Conversion back from Montgomery representation.
+ */
+static void
+frommonty(uint64_t *x, const uint64_t *m, size_t num, uint64_t m0i)
+{
+ size_t u, v;
+
+ for (u = 0; u < num; u ++) {
+ uint64_t f, cc;
+
+ f = MUL62_lo(x[0], m0i) << 2;
+ cc = 0;
+ for (v = 0; v < num; v ++) {
+ uint64_t hi, lo;
+
+ FMA1(hi, lo, f, m[v], x[v] << 2, cc);
+ cc = hi << 2;
+ if (v != 0) {
+ x[v - 1] = lo >> 2;
+ }
+ }
+ x[num - 1] = cc >> 2;
+ }
+ i62_sub(x, m, num, NOT(i62_sub(x, m, num, 0)));
+}
+
+/* see inner.h */
+uint32_t
+br_i62_modpow_opt(uint32_t *x31, const unsigned char *e, size_t elen,
+ const uint32_t *m31, uint32_t m0i31, uint64_t *tmp, size_t twlen)
+{
+ size_t u, mw31num, mw62num;
+ uint64_t *x, *m, *t1, *t2;
+ uint64_t m0i;
+ uint32_t acc;
+ int win_len, acc_len;
+
+ /*
+ * Get modulus size, in words.
+ */
+ mw31num = (m31[0] + 31) >> 5;
+ mw62num = (mw31num + 1) >> 1;
+
+ /*
+ * In order to apply this function, we must have enough room to
+ * copy the operand and modulus into the temporary array, along
+ * with at least two temporaries. If there is not enough room,
+ * switch to br_i31_modpow(). We also use br_i31_modpow() if the
+ * modulus length is not at least four words (94 bits or more).
+ */
+ if (mw31num < 4 || (mw62num << 2) > twlen) {
+ /*
+ * We assume here that we can split an aligned uint64_t
+ * into two properly aligned uint32_t. Since both types
+ * are supposed to have an exact width with no padding,
+ * then this property must hold.
+ */
+ size_t txlen;
+
+ txlen = mw31num + 1;
+ if (twlen < txlen) {
+ return 0;
+ }
+ br_i31_modpow(x31, e, elen, m31, m0i31,
+ (uint32_t *)tmp, (uint32_t *)tmp + txlen);
+ return 1;
+ }
+
+ /*
+ * Convert x to Montgomery representation: this means that
+ * we replace x with x*2^z mod m, where z is the smallest multiple
+ * of the word size such that 2^z >= m. We want to reuse the 31-bit
+ * functions here (for constant-time operation), but we need z
+ * for a 62-bit word size.
+ */
+ for (u = 0; u < mw62num; u ++) {
+ br_i31_muladd_small(x31, 0, m31);
+ br_i31_muladd_small(x31, 0, m31);
+ }
+
+ /*
+ * Assemble operands into arrays of 62-bit words. Note that
+ * all the arrays of 62-bit words that we will handle here
+ * are without any leading size word.
+ *
+ * We also adjust tmp and twlen to account for the words used
+ * for these extra arrays.
+ */
+ m = tmp;
+ x = tmp + mw62num;
+ tmp += (mw62num << 1);
+ twlen -= (mw62num << 1);
+ for (u = 0; u < mw31num; u += 2) {
+ size_t v;
+
+ v = u >> 1;
+ if ((u + 1) == mw31num) {
+ m[v] = (uint64_t)m31[u + 1];
+ x[v] = (uint64_t)x31[u + 1];
+ } else {
+ m[v] = (uint64_t)m31[u + 1]
+ + ((uint64_t)m31[u + 2] << 31);
+ x[v] = (uint64_t)x31[u + 1]
+ + ((uint64_t)x31[u + 2] << 31);
+ }
+ }
+
+ /*
+ * Compute window size. We support windows up to 5 bits; for a
+ * window of size k bits, we need 2^k+1 temporaries (for k = 1,
+ * we use special code that uses only 2 temporaries).
+ */
+ for (win_len = 5; win_len > 1; win_len --) {
+ if ((((uint32_t)1 << win_len) + 1) * mw62num <= twlen) {
+ break;
+ }
+ }
+
+ t1 = tmp;
+ t2 = tmp + mw62num;
+
+ /*
+ * Compute m0i, which is equal to -(1/m0) mod 2^62. We were
+ * provided with m0i31, which already fulfills this property
+ * modulo 2^31; the single expression below is then sufficient.
+ */
+ m0i = (uint64_t)m0i31;
+ m0i = MUL62_lo(m0i, (uint64_t)2 + MUL62_lo(m0i, m[0]));
+
+ /*
+ * Compute window contents. If the window has size one bit only,
+ * then t2 is set to x; otherwise, t2[0] is left untouched, and
+ * t2[k] is set to x^k (for k >= 1).
+ */
+ if (win_len == 1) {
+ memcpy(t2, x, mw62num * sizeof *x);
+ } else {
+ uint64_t *base;
+
+ memcpy(t2 + mw62num, x, mw62num * sizeof *x);
+ base = t2 + mw62num;
+ for (u = 2; u < ((unsigned)1 << win_len); u ++) {
+ montymul(base + mw62num, base, x, m, mw62num, m0i);
+ base += mw62num;
+ }
+ }
+
+ /*
+ * Set x to 1, in Montgomery representation. We again use the
+ * 31-bit code.
+ */
+ br_i31_zero(x31, m31[0]);
+ x31[(m31[0] + 31) >> 5] = 1;
+ br_i31_muladd_small(x31, 0, m31);
+ if (mw31num & 1) {
+ br_i31_muladd_small(x31, 0, m31);
+ }
+ for (u = 0; u < mw31num; u += 2) {
+ size_t v;
+
+ v = u >> 1;
+ if ((u + 1) == mw31num) {
+ x[v] = (uint64_t)x31[u + 1];
+ } else {
+ x[v] = (uint64_t)x31[u + 1]
+ + ((uint64_t)x31[u + 2] << 31);
+ }
+ }
+
+ /*
+ * We process bits from most to least significant. At each
+ * loop iteration, we have acc_len bits in acc.
+ */
+ acc = 0;
+ acc_len = 0;
+ while (acc_len > 0 || elen > 0) {
+ int i, k;
+ uint32_t bits;
+ uint64_t mask1, mask2;
+
+ /*
+ * Get the next bits.
+ */
+ k = win_len;
+ if (acc_len < win_len) {
+ if (elen > 0) {
+ acc = (acc << 8) | *e ++;
+ elen --;
+ acc_len += 8;
+ } else {
+ k = acc_len;
+ }
+ }
+ bits = (acc >> (acc_len - k)) & (((uint32_t)1 << k) - 1);
+ acc_len -= k;
+
+ /*
+ * We could get exactly k bits. Compute k squarings.
+ */
+ for (i = 0; i < k; i ++) {
+ montymul(t1, x, x, m, mw62num, m0i);
+ memcpy(x, t1, mw62num * sizeof *x);
+ }
+
+ /*
+ * Window lookup: we want to set t2 to the window
+ * lookup value, assuming the bits are non-zero. If
+ * the window length is 1 bit only, then t2 is
+ * already set; otherwise, we do a constant-time lookup.
+ */
+ if (win_len > 1) {
+ uint64_t *base;
+
+ memset(t2, 0, mw62num * sizeof *t2);
+ base = t2 + mw62num;
+ for (u = 1; u < ((uint32_t)1 << k); u ++) {
+ uint64_t mask;
+ size_t v;
+
+ mask = -(uint64_t)EQ(u, bits);
+ for (v = 0; v < mw62num; v ++) {
+ t2[v] |= mask & base[v];
+ }
+ base += mw62num;
+ }
+ }
+
+ /*
+ * Multiply with the looked-up value. We keep the product
+ * only if the exponent bits are not all-zero.
+ */
+ montymul(t1, x, t2, m, mw62num, m0i);
+ mask1 = -(uint64_t)EQ(bits, 0);
+ mask2 = ~mask1;
+ for (u = 0; u < mw62num; u ++) {
+ x[u] = (mask1 & x[u]) | (mask2 & t1[u]);
+ }
+ }
+
+ /*
+ * Convert back from Montgomery representation.
+ */
+ frommonty(x, m, mw62num, m0i);
+
+ /*
+ * Convert result into 31-bit words.
+ */
+ for (u = 0; u < mw31num; u += 2) {
+ uint64_t zw;
+
+ zw = x[u >> 1];
+ x31[u + 1] = (uint32_t)zw & 0x7FFFFFFF;
+ if ((u + 1) < mw31num) {
+ x31[u + 2] = (uint32_t)(zw >> 31);
+ }
+ }
+ return 1;
+}
+
+#else
+
+/* see inner.h */
+uint32_t
+br_i62_modpow_opt(uint32_t *x31, const unsigned char *e, size_t elen,
+ const uint32_t *m31, uint32_t m0i31, uint64_t *tmp, size_t twlen)
+{
+ size_t mwlen;
+
+ mwlen = (m31[0] + 63) >> 5;
+ if (twlen < mwlen) {
+ return 0;
+ }
+ return br_i31_modpow_opt(x31, e, elen, m31, m0i31,
+ (uint32_t *)tmp, twlen << 1);
+}
+
+#endif
+
+/* see inner.h */
+uint32_t
+br_i62_modpow_opt_as_i31(uint32_t *x31, const unsigned char *e, size_t elen,
+ const uint32_t *m31, uint32_t m0i31, uint32_t *tmp, size_t twlen)
+{
+ /*
+ * As documented, this function expects the 'tmp' argument to be
+ * 64-bit aligned. This is OK since this function is internal (it
+ * is not part of BearSSL's public API).
+ */
+ return br_i62_modpow_opt(x31, e, elen, m31, m0i31,
+ (uint64_t *)tmp, twlen >> 1);
+}
diff --git a/test/monniaux/BearSSL/src/kdf/hkdf.c b/test/monniaux/BearSSL/src/kdf/hkdf.c
new file mode 100644
index 00000000..6a36851b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/kdf/hkdf.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+const unsigned char br_hkdf_no_salt = 0;
+
+/* see bearssl_kdf.h */
+void
+br_hkdf_init(br_hkdf_context *hc, const br_hash_class *digest_vtable,
+ const void *salt, size_t salt_len)
+{
+ br_hmac_key_context kc;
+ unsigned char tmp[64];
+
+ if (salt == BR_HKDF_NO_SALT) {
+ salt = tmp;
+ salt_len = br_digest_size(digest_vtable);
+ memset(tmp, 0, salt_len);
+ }
+ br_hmac_key_init(&kc, digest_vtable, salt, salt_len);
+ br_hmac_init(&hc->u.hmac_ctx, &kc, 0);
+ hc->dig_len = br_hmac_size(&hc->u.hmac_ctx);
+}
+
+/* see bearssl_kdf.h */
+void
+br_hkdf_inject(br_hkdf_context *hc, const void *ikm, size_t ikm_len)
+{
+ br_hmac_update(&hc->u.hmac_ctx, ikm, ikm_len);
+}
+
+/* see bearssl_kdf.h */
+void
+br_hkdf_flip(br_hkdf_context *hc)
+{
+ unsigned char tmp[64];
+
+ br_hmac_out(&hc->u.hmac_ctx, tmp);
+ br_hmac_key_init(&hc->u.prk_ctx,
+ br_hmac_get_digest(&hc->u.hmac_ctx), tmp, hc->dig_len);
+ hc->ptr = hc->dig_len;
+ hc->chunk_num = 0;
+}
+
+/* see bearssl_kdf.h */
+size_t
+br_hkdf_produce(br_hkdf_context *hc,
+ const void *info, size_t info_len, void *out, size_t out_len)
+{
+ size_t tlen;
+
+ tlen = 0;
+ while (out_len > 0) {
+ size_t clen;
+
+ if (hc->ptr == hc->dig_len) {
+ br_hmac_context hmac_ctx;
+ unsigned char x;
+
+ hc->chunk_num ++;
+ if (hc->chunk_num == 256) {
+ return tlen;
+ }
+ x = hc->chunk_num;
+ br_hmac_init(&hmac_ctx, &hc->u.prk_ctx, 0);
+ if (x != 1) {
+ br_hmac_update(&hmac_ctx, hc->buf, hc->dig_len);
+ }
+ br_hmac_update(&hmac_ctx, info, info_len);
+ br_hmac_update(&hmac_ctx, &x, 1);
+ br_hmac_out(&hmac_ctx, hc->buf);
+ hc->ptr = 0;
+ }
+ clen = hc->dig_len - hc->ptr;
+ if (clen > out_len) {
+ clen = out_len;
+ }
+ memcpy(out, hc->buf + hc->ptr, clen);
+ out = (unsigned char *)out + clen;
+ out_len -= clen;
+ hc->ptr += clen;
+ tlen += clen;
+ }
+ return tlen;
+}
diff --git a/test/monniaux/BearSSL/src/kdf/shake.c b/test/monniaux/BearSSL/src/kdf/shake.c
new file mode 100644
index 00000000..80d7176d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/kdf/shake.c
@@ -0,0 +1,590 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Round constants.
+ */
+static const uint64_t RC[] = {
+ 0x0000000000000001, 0x0000000000008082,
+ 0x800000000000808A, 0x8000000080008000,
+ 0x000000000000808B, 0x0000000080000001,
+ 0x8000000080008081, 0x8000000000008009,
+ 0x000000000000008A, 0x0000000000000088,
+ 0x0000000080008009, 0x000000008000000A,
+ 0x000000008000808B, 0x800000000000008B,
+ 0x8000000000008089, 0x8000000000008003,
+ 0x8000000000008002, 0x8000000000000080,
+ 0x000000000000800A, 0x800000008000000A,
+ 0x8000000080008081, 0x8000000000008080,
+ 0x0000000080000001, 0x8000000080008008
+};
+
+/*
+ * XOR a block of data into the provided state. This supports only
+ * blocks whose length is a multiple of 64 bits.
+ */
+static void
+xor_block(uint64_t *A, const void *data, size_t rate)
+{
+ size_t u;
+
+ for (u = 0; u < rate; u += 8) {
+ A[u >> 3] ^= br_dec64le((const unsigned char *)data + u);
+ }
+}
+
+/*
+ * Process a block with the provided data. The data length must be a
+ * multiple of 8 (in bytes); normally, this is the "rate".
+ */
+static void
+process_block(uint64_t *A)
+{
+ uint64_t t0, t1, t2, t3, t4;
+ uint64_t tt0, tt1, tt2, tt3;
+ uint64_t t, kt;
+ uint64_t c0, c1, c2, c3, c4, bnn;
+ int j;
+
+ /*
+ * Compute the 24 rounds. This loop is partially unrolled (each
+ * iteration computes two rounds).
+ */
+ for (j = 0; j < 24; j += 2) {
+
+ tt0 = A[ 1] ^ A[ 6];
+ tt1 = A[11] ^ A[16];
+ tt0 ^= A[21] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 4] ^ A[ 9];
+ tt3 = A[14] ^ A[19];
+ tt0 ^= A[24];
+ tt2 ^= tt3;
+ t0 = tt0 ^ tt2;
+
+ tt0 = A[ 2] ^ A[ 7];
+ tt1 = A[12] ^ A[17];
+ tt0 ^= A[22] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 0] ^ A[ 5];
+ tt3 = A[10] ^ A[15];
+ tt0 ^= A[20];
+ tt2 ^= tt3;
+ t1 = tt0 ^ tt2;
+
+ tt0 = A[ 3] ^ A[ 8];
+ tt1 = A[13] ^ A[18];
+ tt0 ^= A[23] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 1] ^ A[ 6];
+ tt3 = A[11] ^ A[16];
+ tt0 ^= A[21];
+ tt2 ^= tt3;
+ t2 = tt0 ^ tt2;
+
+ tt0 = A[ 4] ^ A[ 9];
+ tt1 = A[14] ^ A[19];
+ tt0 ^= A[24] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 2] ^ A[ 7];
+ tt3 = A[12] ^ A[17];
+ tt0 ^= A[22];
+ tt2 ^= tt3;
+ t3 = tt0 ^ tt2;
+
+ tt0 = A[ 0] ^ A[ 5];
+ tt1 = A[10] ^ A[15];
+ tt0 ^= A[20] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 3] ^ A[ 8];
+ tt3 = A[13] ^ A[18];
+ tt0 ^= A[23];
+ tt2 ^= tt3;
+ t4 = tt0 ^ tt2;
+
+ A[ 0] = A[ 0] ^ t0;
+ A[ 5] = A[ 5] ^ t0;
+ A[10] = A[10] ^ t0;
+ A[15] = A[15] ^ t0;
+ A[20] = A[20] ^ t0;
+ A[ 1] = A[ 1] ^ t1;
+ A[ 6] = A[ 6] ^ t1;
+ A[11] = A[11] ^ t1;
+ A[16] = A[16] ^ t1;
+ A[21] = A[21] ^ t1;
+ A[ 2] = A[ 2] ^ t2;
+ A[ 7] = A[ 7] ^ t2;
+ A[12] = A[12] ^ t2;
+ A[17] = A[17] ^ t2;
+ A[22] = A[22] ^ t2;
+ A[ 3] = A[ 3] ^ t3;
+ A[ 8] = A[ 8] ^ t3;
+ A[13] = A[13] ^ t3;
+ A[18] = A[18] ^ t3;
+ A[23] = A[23] ^ t3;
+ A[ 4] = A[ 4] ^ t4;
+ A[ 9] = A[ 9] ^ t4;
+ A[14] = A[14] ^ t4;
+ A[19] = A[19] ^ t4;
+ A[24] = A[24] ^ t4;
+ A[ 5] = (A[ 5] << 36) | (A[ 5] >> (64 - 36));
+ A[10] = (A[10] << 3) | (A[10] >> (64 - 3));
+ A[15] = (A[15] << 41) | (A[15] >> (64 - 41));
+ A[20] = (A[20] << 18) | (A[20] >> (64 - 18));
+ A[ 1] = (A[ 1] << 1) | (A[ 1] >> (64 - 1));
+ A[ 6] = (A[ 6] << 44) | (A[ 6] >> (64 - 44));
+ A[11] = (A[11] << 10) | (A[11] >> (64 - 10));
+ A[16] = (A[16] << 45) | (A[16] >> (64 - 45));
+ A[21] = (A[21] << 2) | (A[21] >> (64 - 2));
+ A[ 2] = (A[ 2] << 62) | (A[ 2] >> (64 - 62));
+ A[ 7] = (A[ 7] << 6) | (A[ 7] >> (64 - 6));
+ A[12] = (A[12] << 43) | (A[12] >> (64 - 43));
+ A[17] = (A[17] << 15) | (A[17] >> (64 - 15));
+ A[22] = (A[22] << 61) | (A[22] >> (64 - 61));
+ A[ 3] = (A[ 3] << 28) | (A[ 3] >> (64 - 28));
+ A[ 8] = (A[ 8] << 55) | (A[ 8] >> (64 - 55));
+ A[13] = (A[13] << 25) | (A[13] >> (64 - 25));
+ A[18] = (A[18] << 21) | (A[18] >> (64 - 21));
+ A[23] = (A[23] << 56) | (A[23] >> (64 - 56));
+ A[ 4] = (A[ 4] << 27) | (A[ 4] >> (64 - 27));
+ A[ 9] = (A[ 9] << 20) | (A[ 9] >> (64 - 20));
+ A[14] = (A[14] << 39) | (A[14] >> (64 - 39));
+ A[19] = (A[19] << 8) | (A[19] >> (64 - 8));
+ A[24] = (A[24] << 14) | (A[24] >> (64 - 14));
+ bnn = ~A[12];
+ kt = A[ 6] | A[12];
+ c0 = A[ 0] ^ kt;
+ kt = bnn | A[18];
+ c1 = A[ 6] ^ kt;
+ kt = A[18] & A[24];
+ c2 = A[12] ^ kt;
+ kt = A[24] | A[ 0];
+ c3 = A[18] ^ kt;
+ kt = A[ 0] & A[ 6];
+ c4 = A[24] ^ kt;
+ A[ 0] = c0;
+ A[ 6] = c1;
+ A[12] = c2;
+ A[18] = c3;
+ A[24] = c4;
+ bnn = ~A[22];
+ kt = A[ 9] | A[10];
+ c0 = A[ 3] ^ kt;
+ kt = A[10] & A[16];
+ c1 = A[ 9] ^ kt;
+ kt = A[16] | bnn;
+ c2 = A[10] ^ kt;
+ kt = A[22] | A[ 3];
+ c3 = A[16] ^ kt;
+ kt = A[ 3] & A[ 9];
+ c4 = A[22] ^ kt;
+ A[ 3] = c0;
+ A[ 9] = c1;
+ A[10] = c2;
+ A[16] = c3;
+ A[22] = c4;
+ bnn = ~A[19];
+ kt = A[ 7] | A[13];
+ c0 = A[ 1] ^ kt;
+ kt = A[13] & A[19];
+ c1 = A[ 7] ^ kt;
+ kt = bnn & A[20];
+ c2 = A[13] ^ kt;
+ kt = A[20] | A[ 1];
+ c3 = bnn ^ kt;
+ kt = A[ 1] & A[ 7];
+ c4 = A[20] ^ kt;
+ A[ 1] = c0;
+ A[ 7] = c1;
+ A[13] = c2;
+ A[19] = c3;
+ A[20] = c4;
+ bnn = ~A[17];
+ kt = A[ 5] & A[11];
+ c0 = A[ 4] ^ kt;
+ kt = A[11] | A[17];
+ c1 = A[ 5] ^ kt;
+ kt = bnn | A[23];
+ c2 = A[11] ^ kt;
+ kt = A[23] & A[ 4];
+ c3 = bnn ^ kt;
+ kt = A[ 4] | A[ 5];
+ c4 = A[23] ^ kt;
+ A[ 4] = c0;
+ A[ 5] = c1;
+ A[11] = c2;
+ A[17] = c3;
+ A[23] = c4;
+ bnn = ~A[ 8];
+ kt = bnn & A[14];
+ c0 = A[ 2] ^ kt;
+ kt = A[14] | A[15];
+ c1 = bnn ^ kt;
+ kt = A[15] & A[21];
+ c2 = A[14] ^ kt;
+ kt = A[21] | A[ 2];
+ c3 = A[15] ^ kt;
+ kt = A[ 2] & A[ 8];
+ c4 = A[21] ^ kt;
+ A[ 2] = c0;
+ A[ 8] = c1;
+ A[14] = c2;
+ A[15] = c3;
+ A[21] = c4;
+ A[ 0] = A[ 0] ^ RC[j + 0];
+
+ tt0 = A[ 6] ^ A[ 9];
+ tt1 = A[ 7] ^ A[ 5];
+ tt0 ^= A[ 8] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[24] ^ A[22];
+ tt3 = A[20] ^ A[23];
+ tt0 ^= A[21];
+ tt2 ^= tt3;
+ t0 = tt0 ^ tt2;
+
+ tt0 = A[12] ^ A[10];
+ tt1 = A[13] ^ A[11];
+ tt0 ^= A[14] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 0] ^ A[ 3];
+ tt3 = A[ 1] ^ A[ 4];
+ tt0 ^= A[ 2];
+ tt2 ^= tt3;
+ t1 = tt0 ^ tt2;
+
+ tt0 = A[18] ^ A[16];
+ tt1 = A[19] ^ A[17];
+ tt0 ^= A[15] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[ 6] ^ A[ 9];
+ tt3 = A[ 7] ^ A[ 5];
+ tt0 ^= A[ 8];
+ tt2 ^= tt3;
+ t2 = tt0 ^ tt2;
+
+ tt0 = A[24] ^ A[22];
+ tt1 = A[20] ^ A[23];
+ tt0 ^= A[21] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[12] ^ A[10];
+ tt3 = A[13] ^ A[11];
+ tt0 ^= A[14];
+ tt2 ^= tt3;
+ t3 = tt0 ^ tt2;
+
+ tt0 = A[ 0] ^ A[ 3];
+ tt1 = A[ 1] ^ A[ 4];
+ tt0 ^= A[ 2] ^ tt1;
+ tt0 = (tt0 << 1) | (tt0 >> 63);
+ tt2 = A[18] ^ A[16];
+ tt3 = A[19] ^ A[17];
+ tt0 ^= A[15];
+ tt2 ^= tt3;
+ t4 = tt0 ^ tt2;
+
+ A[ 0] = A[ 0] ^ t0;
+ A[ 3] = A[ 3] ^ t0;
+ A[ 1] = A[ 1] ^ t0;
+ A[ 4] = A[ 4] ^ t0;
+ A[ 2] = A[ 2] ^ t0;
+ A[ 6] = A[ 6] ^ t1;
+ A[ 9] = A[ 9] ^ t1;
+ A[ 7] = A[ 7] ^ t1;
+ A[ 5] = A[ 5] ^ t1;
+ A[ 8] = A[ 8] ^ t1;
+ A[12] = A[12] ^ t2;
+ A[10] = A[10] ^ t2;
+ A[13] = A[13] ^ t2;
+ A[11] = A[11] ^ t2;
+ A[14] = A[14] ^ t2;
+ A[18] = A[18] ^ t3;
+ A[16] = A[16] ^ t3;
+ A[19] = A[19] ^ t3;
+ A[17] = A[17] ^ t3;
+ A[15] = A[15] ^ t3;
+ A[24] = A[24] ^ t4;
+ A[22] = A[22] ^ t4;
+ A[20] = A[20] ^ t4;
+ A[23] = A[23] ^ t4;
+ A[21] = A[21] ^ t4;
+ A[ 3] = (A[ 3] << 36) | (A[ 3] >> (64 - 36));
+ A[ 1] = (A[ 1] << 3) | (A[ 1] >> (64 - 3));
+ A[ 4] = (A[ 4] << 41) | (A[ 4] >> (64 - 41));
+ A[ 2] = (A[ 2] << 18) | (A[ 2] >> (64 - 18));
+ A[ 6] = (A[ 6] << 1) | (A[ 6] >> (64 - 1));
+ A[ 9] = (A[ 9] << 44) | (A[ 9] >> (64 - 44));
+ A[ 7] = (A[ 7] << 10) | (A[ 7] >> (64 - 10));
+ A[ 5] = (A[ 5] << 45) | (A[ 5] >> (64 - 45));
+ A[ 8] = (A[ 8] << 2) | (A[ 8] >> (64 - 2));
+ A[12] = (A[12] << 62) | (A[12] >> (64 - 62));
+ A[10] = (A[10] << 6) | (A[10] >> (64 - 6));
+ A[13] = (A[13] << 43) | (A[13] >> (64 - 43));
+ A[11] = (A[11] << 15) | (A[11] >> (64 - 15));
+ A[14] = (A[14] << 61) | (A[14] >> (64 - 61));
+ A[18] = (A[18] << 28) | (A[18] >> (64 - 28));
+ A[16] = (A[16] << 55) | (A[16] >> (64 - 55));
+ A[19] = (A[19] << 25) | (A[19] >> (64 - 25));
+ A[17] = (A[17] << 21) | (A[17] >> (64 - 21));
+ A[15] = (A[15] << 56) | (A[15] >> (64 - 56));
+ A[24] = (A[24] << 27) | (A[24] >> (64 - 27));
+ A[22] = (A[22] << 20) | (A[22] >> (64 - 20));
+ A[20] = (A[20] << 39) | (A[20] >> (64 - 39));
+ A[23] = (A[23] << 8) | (A[23] >> (64 - 8));
+ A[21] = (A[21] << 14) | (A[21] >> (64 - 14));
+ bnn = ~A[13];
+ kt = A[ 9] | A[13];
+ c0 = A[ 0] ^ kt;
+ kt = bnn | A[17];
+ c1 = A[ 9] ^ kt;
+ kt = A[17] & A[21];
+ c2 = A[13] ^ kt;
+ kt = A[21] | A[ 0];
+ c3 = A[17] ^ kt;
+ kt = A[ 0] & A[ 9];
+ c4 = A[21] ^ kt;
+ A[ 0] = c0;
+ A[ 9] = c1;
+ A[13] = c2;
+ A[17] = c3;
+ A[21] = c4;
+ bnn = ~A[14];
+ kt = A[22] | A[ 1];
+ c0 = A[18] ^ kt;
+ kt = A[ 1] & A[ 5];
+ c1 = A[22] ^ kt;
+ kt = A[ 5] | bnn;
+ c2 = A[ 1] ^ kt;
+ kt = A[14] | A[18];
+ c3 = A[ 5] ^ kt;
+ kt = A[18] & A[22];
+ c4 = A[14] ^ kt;
+ A[18] = c0;
+ A[22] = c1;
+ A[ 1] = c2;
+ A[ 5] = c3;
+ A[14] = c4;
+ bnn = ~A[23];
+ kt = A[10] | A[19];
+ c0 = A[ 6] ^ kt;
+ kt = A[19] & A[23];
+ c1 = A[10] ^ kt;
+ kt = bnn & A[ 2];
+ c2 = A[19] ^ kt;
+ kt = A[ 2] | A[ 6];
+ c3 = bnn ^ kt;
+ kt = A[ 6] & A[10];
+ c4 = A[ 2] ^ kt;
+ A[ 6] = c0;
+ A[10] = c1;
+ A[19] = c2;
+ A[23] = c3;
+ A[ 2] = c4;
+ bnn = ~A[11];
+ kt = A[ 3] & A[ 7];
+ c0 = A[24] ^ kt;
+ kt = A[ 7] | A[11];
+ c1 = A[ 3] ^ kt;
+ kt = bnn | A[15];
+ c2 = A[ 7] ^ kt;
+ kt = A[15] & A[24];
+ c3 = bnn ^ kt;
+ kt = A[24] | A[ 3];
+ c4 = A[15] ^ kt;
+ A[24] = c0;
+ A[ 3] = c1;
+ A[ 7] = c2;
+ A[11] = c3;
+ A[15] = c4;
+ bnn = ~A[16];
+ kt = bnn & A[20];
+ c0 = A[12] ^ kt;
+ kt = A[20] | A[ 4];
+ c1 = bnn ^ kt;
+ kt = A[ 4] & A[ 8];
+ c2 = A[20] ^ kt;
+ kt = A[ 8] | A[12];
+ c3 = A[ 4] ^ kt;
+ kt = A[12] & A[16];
+ c4 = A[ 8] ^ kt;
+ A[12] = c0;
+ A[16] = c1;
+ A[20] = c2;
+ A[ 4] = c3;
+ A[ 8] = c4;
+ A[ 0] = A[ 0] ^ RC[j + 1];
+ t = A[ 5];
+ A[ 5] = A[18];
+ A[18] = A[11];
+ A[11] = A[10];
+ A[10] = A[ 6];
+ A[ 6] = A[22];
+ A[22] = A[20];
+ A[20] = A[12];
+ A[12] = A[19];
+ A[19] = A[15];
+ A[15] = A[24];
+ A[24] = A[ 8];
+ A[ 8] = t;
+ t = A[ 1];
+ A[ 1] = A[ 9];
+ A[ 9] = A[14];
+ A[14] = A[ 2];
+ A[ 2] = A[13];
+ A[13] = A[23];
+ A[23] = A[ 4];
+ A[ 4] = A[21];
+ A[21] = A[16];
+ A[16] = A[ 3];
+ A[ 3] = A[17];
+ A[17] = A[ 7];
+ A[ 7] = t;
+ }
+}
+
+/* see bearssl_kdf.h */
+void
+br_shake_init(br_shake_context *sc, int security_level)
+{
+ sc->rate = 200 - (size_t)(security_level >> 2);
+ sc->dptr = 0;
+ memset(sc->A, 0, sizeof sc->A);
+ sc->A[ 1] = ~(uint64_t)0;
+ sc->A[ 2] = ~(uint64_t)0;
+ sc->A[ 8] = ~(uint64_t)0;
+ sc->A[12] = ~(uint64_t)0;
+ sc->A[17] = ~(uint64_t)0;
+ sc->A[20] = ~(uint64_t)0;
+}
+
+/* see bearssl_kdf.h */
+void
+br_shake_inject(br_shake_context *sc, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t rate, dptr;
+
+ buf = data;
+ rate = sc->rate;
+ dptr = sc->dptr;
+ while (len > 0) {
+ size_t clen;
+
+ clen = rate - dptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(sc->dbuf + dptr, buf, clen);
+ dptr += clen;
+ buf += clen;
+ len -= clen;
+ if (dptr == rate) {
+ xor_block(sc->A, sc->dbuf, rate);
+ process_block(sc->A);
+ dptr = 0;
+ }
+ }
+ sc->dptr = dptr;
+}
+
+/* see bearssl_kdf.h */
+void
+br_shake_flip(br_shake_context *sc)
+{
+ /*
+ * We apply padding and pre-XOR the value into the state. We
+ * set dptr to the end of the buffer, so that first call to
+ * shake_extract() will process the block.
+ */
+ if ((sc->dptr + 1) == sc->rate) {
+ sc->dbuf[sc->dptr ++] = 0x9F;
+ } else {
+ sc->dbuf[sc->dptr ++] = 0x1F;
+ memset(sc->dbuf + sc->dptr, 0x00, sc->rate - sc->dptr - 1);
+ sc->dbuf[sc->rate - 1] = 0x80;
+ sc->dptr = sc->rate;
+ }
+ xor_block(sc->A, sc->dbuf, sc->rate);
+}
+
+/* see bearssl_kdf.h */
+void
+br_shake_produce(br_shake_context *sc, void *out, size_t len)
+{
+ unsigned char *buf;
+ size_t dptr, rate;
+
+ buf = out;
+ dptr = sc->dptr;
+ rate = sc->rate;
+ while (len > 0) {
+ size_t clen;
+
+ if (dptr == rate) {
+ unsigned char *dbuf;
+ uint64_t *A;
+
+ A = sc->A;
+ dbuf = sc->dbuf;
+ process_block(A);
+ br_enc64le(dbuf + 0, A[ 0]);
+ br_enc64le(dbuf + 8, ~A[ 1]);
+ br_enc64le(dbuf + 16, ~A[ 2]);
+ br_enc64le(dbuf + 24, A[ 3]);
+ br_enc64le(dbuf + 32, A[ 4]);
+ br_enc64le(dbuf + 40, A[ 5]);
+ br_enc64le(dbuf + 48, A[ 6]);
+ br_enc64le(dbuf + 56, A[ 7]);
+ br_enc64le(dbuf + 64, ~A[ 8]);
+ br_enc64le(dbuf + 72, A[ 9]);
+ br_enc64le(dbuf + 80, A[10]);
+ br_enc64le(dbuf + 88, A[11]);
+ br_enc64le(dbuf + 96, ~A[12]);
+ br_enc64le(dbuf + 104, A[13]);
+ br_enc64le(dbuf + 112, A[14]);
+ br_enc64le(dbuf + 120, A[15]);
+ br_enc64le(dbuf + 128, A[16]);
+ br_enc64le(dbuf + 136, ~A[17]);
+ br_enc64le(dbuf + 144, A[18]);
+ br_enc64le(dbuf + 152, A[19]);
+ br_enc64le(dbuf + 160, ~A[20]);
+ br_enc64le(dbuf + 168, A[21]);
+ br_enc64le(dbuf + 176, A[22]);
+ br_enc64le(dbuf + 184, A[23]);
+ br_enc64le(dbuf + 192, A[24]);
+ dptr = 0;
+ }
+ clen = rate - dptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(buf, sc->dbuf + dptr, clen);
+ dptr += clen;
+ buf += clen;
+ len -= clen;
+ }
+ sc->dptr = dptr;
+}
diff --git a/test/monniaux/BearSSL/src/mac/hmac.c b/test/monniaux/BearSSL/src/mac/hmac.c
new file mode 100644
index 00000000..dda92413
--- /dev/null
+++ b/test/monniaux/BearSSL/src/mac/hmac.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static inline size_t
+block_size(const br_hash_class *dig)
+{
+ unsigned ls;
+
+ ls = (unsigned)(dig->desc >> BR_HASHDESC_LBLEN_OFF)
+ & BR_HASHDESC_LBLEN_MASK;
+ return (size_t)1 << ls;
+}
+
+static void
+process_key(const br_hash_class **hc, void *ks,
+ const void *key, size_t key_len, unsigned bb)
+{
+ unsigned char tmp[256];
+ size_t blen, u;
+
+ blen = block_size(*hc);
+ memcpy(tmp, key, key_len);
+ for (u = 0; u < key_len; u ++) {
+ tmp[u] ^= (unsigned char)bb;
+ }
+ memset(tmp + key_len, bb, blen - key_len);
+ (*hc)->init(hc);
+ (*hc)->update(hc, tmp, blen);
+ (*hc)->state(hc, ks);
+}
+
+/* see bearssl.h */
+void
+br_hmac_key_init(br_hmac_key_context *kc,
+ const br_hash_class *dig, const void *key, size_t key_len)
+{
+ br_hash_compat_context hc;
+ unsigned char kbuf[64];
+
+ kc->dig_vtable = dig;
+ hc.vtable = dig;
+ if (key_len > block_size(dig)) {
+ dig->init(&hc.vtable);
+ dig->update(&hc.vtable, key, key_len);
+ dig->out(&hc.vtable, kbuf);
+ key = kbuf;
+ key_len = br_digest_size(dig);
+ }
+ process_key(&hc.vtable, kc->ksi, key, key_len, 0x36);
+ process_key(&hc.vtable, kc->kso, key, key_len, 0x5C);
+}
+
+/* see bearssl.h */
+void
+br_hmac_init(br_hmac_context *ctx,
+ const br_hmac_key_context *kc, size_t out_len)
+{
+ const br_hash_class *dig;
+ size_t blen, hlen;
+
+ dig = kc->dig_vtable;
+ blen = block_size(dig);
+ dig->init(&ctx->dig.vtable);
+ dig->set_state(&ctx->dig.vtable, kc->ksi, (uint64_t)blen);
+ memcpy(ctx->kso, kc->kso, sizeof kc->kso);
+ hlen = br_digest_size(dig);
+ if (out_len > 0 && out_len < hlen) {
+ hlen = out_len;
+ }
+ ctx->out_len = hlen;
+}
+
+/* see bearssl.h */
+void
+br_hmac_update(br_hmac_context *ctx, const void *data, size_t len)
+{
+ ctx->dig.vtable->update(&ctx->dig.vtable, data, len);
+ KILL_TAIL_CALL();
+}
+
+/* see bearssl.h */
+size_t
+br_hmac_out(const br_hmac_context *ctx, void *out)
+{
+ const br_hash_class *dig;
+ br_hash_compat_context hc;
+ unsigned char tmp[64];
+ size_t blen, hlen;
+
+ dig = ctx->dig.vtable;
+ dig->out(&ctx->dig.vtable, tmp);
+ blen = block_size(dig);
+ dig->init(&hc.vtable);
+ dig->set_state(&hc.vtable, ctx->kso, (uint64_t)blen);
+ hlen = br_digest_size(dig);
+ dig->update(&hc.vtable, tmp, hlen);
+ dig->out(&hc.vtable, tmp);
+ memcpy(out, tmp, ctx->out_len);
+ return ctx->out_len;
+}
diff --git a/test/monniaux/BearSSL/src/mac/hmac_ct.c b/test/monniaux/BearSSL/src/mac/hmac_ct.c
new file mode 100644
index 00000000..e1c1d802
--- /dev/null
+++ b/test/monniaux/BearSSL/src/mac/hmac_ct.c
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static inline size_t
+hash_size(const br_hash_class *dig)
+{
+ return (unsigned)(dig->desc >> BR_HASHDESC_OUT_OFF)
+ & BR_HASHDESC_OUT_MASK;
+}
+
+static inline size_t
+block_size(const br_hash_class *dig)
+{
+ unsigned ls;
+
+ ls = (unsigned)(dig->desc >> BR_HASHDESC_LBLEN_OFF)
+ & BR_HASHDESC_LBLEN_MASK;
+ return (size_t)1 << ls;
+}
+
+/* see bearssl.h */
+size_t
+br_hmac_outCT(const br_hmac_context *ctx,
+ const void *data, size_t len, size_t min_len, size_t max_len,
+ void *out)
+{
+ /*
+ * Method implemented here is inspired from the descriptions on:
+ * https://www.imperialviolet.org/2013/02/04/luckythirteen.html
+ *
+ * Principle: we input bytes one by one. We use a MUX to push
+ * padding bytes instead of data bytes when appropriate. At each
+ * block limit, we get the current hash function state: this is
+ * a potential output, since we handle MD padding ourselves.
+ *
+ * be 1 for big-endian, 0 for little-endian
+ * po minimal MD padding length
+ * bs block size (always a power of 2)
+ * hlen hash output size
+ */
+
+ const br_hash_class *dig;
+ br_hash_compat_context hc;
+ int be;
+ uint32_t po, bs;
+ uint32_t kr, km, kl, kz, u;
+ uint64_t count, ncount, bit_len;
+ unsigned char tmp1[64], tmp2[64];
+ size_t hlen;
+
+ /*
+ * Copy the current hash context.
+ */
+ hc = ctx->dig;
+
+ /*
+ * Get function-specific information.
+ */
+ dig = hc.vtable;
+ be = (dig->desc & BR_HASHDESC_MD_PADDING_BE) != 0;
+ po = 9;
+ if (dig->desc & BR_HASHDESC_MD_PADDING_128) {
+ po += 8;
+ }
+ bs = block_size(dig);
+ hlen = hash_size(dig);
+
+ /*
+ * Get current input length and compute total bit length.
+ */
+ count = dig->state(&hc.vtable, tmp1);
+ bit_len = (count + (uint64_t)len) << 3;
+
+ /*
+ * We can input the blocks that we are sure we will use.
+ * This offers better performance (no MUX for these blocks)
+ * and also ensures that the remaining lengths fit on 32 bits.
+ */
+ ncount = (count + (uint64_t)min_len) & ~(uint64_t)(bs - 1);
+ if (ncount > count) {
+ size_t zlen;
+
+ zlen = (size_t)(ncount - count);
+ dig->update(&hc.vtable, data, zlen);
+ data = (const unsigned char *)data + zlen;
+ len -= zlen;
+ max_len -= zlen;
+ count = ncount;
+ }
+
+ /*
+ * At that point:
+ * -- 'count' contains the number of bytes already processed
+ * (in total).
+ * -- We must input 'len' bytes. 'min_len' is unimportant: we
+ * used it to know how many full blocks we could process
+ * directly. Now only len and max_len matter.
+ *
+ * We compute kr, kl, kz and km.
+ * kr number of input bytes already in the current block
+ * km index of the first byte after the end of the last padding
+ * block, if length is max_len
+ * kz index of the last byte of the actual last padding block
+ * kl index of the start of the encoded length
+ *
+ * km, kz and kl are counted from the current offset in the
+ * input data.
+ */
+ kr = (uint32_t)count & (bs - 1);
+ kz = ((kr + (uint32_t)len + po + bs - 1) & ~(bs - 1)) - 1 - kr;
+ kl = kz - 7;
+ km = ((kr + (uint32_t)max_len + po + bs - 1) & ~(bs - 1)) - kr;
+
+ /*
+ * We must now process km bytes. For index u from 0 to km-1:
+ * d is from data[] if u < max_len, 0x00 otherwise
+ * e is an encoded length byte or 0x00, depending on u
+ * The tests for d and e need not be constant-time, since
+ * they relate only to u and max_len, not to the actual length.
+ *
+ * Actual input length is then:
+ * d if u < len
+ * 0x80 if u == len
+ * 0x00 if u > len and u < kl
+ * e if u >= kl
+ *
+ * Hash state is obtained whenever we reach a full block. This
+ * is the result we want if and only if u == kz.
+ */
+ memset(tmp2, 0, sizeof tmp2);
+ for (u = 0; u < km; u ++) {
+ uint32_t v;
+ uint32_t d, e, x0, x1;
+ unsigned char x[1];
+
+ d = (u < max_len) ? ((const unsigned char *)data)[u] : 0x00;
+ v = (kr + u) & (bs - 1);
+ if (v >= (bs - 8)) {
+ unsigned j;
+
+ j = (v - (bs - 8)) << 3;
+ if (be) {
+ e = (uint32_t)(bit_len >> (56 - j));
+ } else {
+ e = (uint32_t)(bit_len >> j);
+ }
+ e &= 0xFF;
+ } else {
+ e = 0x00;
+ }
+ x0 = MUX(EQ(u, (uint32_t)len), 0x80, d);
+ x1 = MUX(LT(u, kl), 0x00, e);
+ x[0] = MUX(LE(u, (uint32_t)len), x0, x1);
+ dig->update(&hc.vtable, x, 1);
+ if (v == (bs - 1)) {
+ dig->state(&hc.vtable, tmp1);
+ CCOPY(EQ(u, kz), tmp2, tmp1, hlen);
+ }
+ }
+
+ /*
+ * Inner hash output is in tmp2[]; we finish processing.
+ */
+ dig->init(&hc.vtable);
+ dig->set_state(&hc.vtable, ctx->kso, (uint64_t)bs);
+ dig->update(&hc.vtable, tmp2, hlen);
+ dig->out(&hc.vtable, tmp2);
+ memcpy(out, tmp2, ctx->out_len);
+ return ctx->out_len;
+}
diff --git a/test/monniaux/BearSSL/src/rand/aesctr_drbg.c b/test/monniaux/BearSSL/src/rand/aesctr_drbg.c
new file mode 100644
index 00000000..8dbd5010
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rand/aesctr_drbg.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rand.h */
+void
+br_aesctr_drbg_init(br_aesctr_drbg_context *ctx,
+ const br_block_ctr_class *aesctr,
+ const void *seed, size_t len)
+{
+ unsigned char tmp[16];
+
+ ctx->vtable = &br_aesctr_drbg_vtable;
+ memset(tmp, 0, sizeof tmp);
+ aesctr->init(&ctx->sk.vtable, tmp, 16);
+ ctx->cc = 0;
+ br_aesctr_drbg_update(ctx, seed, len);
+}
+
+/* see bearssl_rand.h */
+void
+br_aesctr_drbg_generate(br_aesctr_drbg_context *ctx, void *out, size_t len)
+{
+ unsigned char *buf;
+ unsigned char iv[12];
+
+ buf = out;
+ memset(iv, 0, sizeof iv);
+ while (len > 0) {
+ size_t clen;
+
+ /*
+ * We generate data by blocks of at most 65280 bytes. This
+ * allows for unambiguously testing the counter overflow
+ * condition; also, it should work on 16-bit architectures
+ * (where 'size_t' is 16 bits only).
+ */
+ clen = len;
+ if (clen > 65280) {
+ clen = 65280;
+ }
+
+ /*
+ * We make sure that the counter won't exceed the configured
+ * limit.
+ */
+ if ((uint32_t)(ctx->cc + ((clen + 15) >> 4)) > 32768) {
+ clen = (32768 - ctx->cc) << 4;
+ if (clen > len) {
+ clen = len;
+ }
+ }
+
+ /*
+ * Run CTR.
+ */
+ memset(buf, 0, clen);
+ ctx->cc = ctx->sk.vtable->run(&ctx->sk.vtable,
+ iv, ctx->cc, buf, clen);
+ buf += clen;
+ len -= clen;
+
+ /*
+ * Every 32768 blocks, we force a state update.
+ */
+ if (ctx->cc >= 32768) {
+ br_aesctr_drbg_update(ctx, NULL, 0);
+ }
+ }
+}
+
+/* see bearssl_rand.h */
+void
+br_aesctr_drbg_update(br_aesctr_drbg_context *ctx, const void *seed, size_t len)
+{
+ /*
+ * We use a Hirose construction on AES-256 to make a hash function.
+ * Function definition:
+ * - running state consists in two 16-byte blocks G and H
+ * - initial values of G and H are conventional
+ * - there is a fixed block-sized constant C
+ * - for next data block m:
+ * set AES key to H||m
+ * G' = E(G) xor G
+ * H' = E(G xor C) xor G xor C
+ * G <- G', H <- H'
+ * - once all blocks have been processed, output is H||G
+ *
+ * Constants:
+ * G_init = B6 B6 ... B6
+ * H_init = A5 A5 ... A5
+ * C = 01 00 ... 00
+ *
+ * With this hash function h(), we compute the new state as
+ * follows:
+ * - produce a state-dependent value s as encryption of an
+ * all-one block with AES and the current key
+ * - compute the new key as the first 128 bits of h(s||seed)
+ *
+ * Original Hirose article:
+ * https://www.iacr.org/archive/fse2006/40470213/40470213.pdf
+ */
+
+ unsigned char s[16], iv[12];
+ unsigned char G[16], H[16];
+ int first;
+
+ /*
+ * Use an all-one IV to get a fresh output block that depends on the
+ * current seed.
+ */
+ memset(iv, 0xFF, sizeof iv);
+ memset(s, 0, 16);
+ ctx->sk.vtable->run(&ctx->sk.vtable, iv, 0xFFFFFFFF, s, 16);
+
+ /*
+ * Set G[] and H[] to conventional start values.
+ */
+ memset(G, 0xB6, sizeof G);
+ memset(H, 0x5A, sizeof H);
+
+ /*
+ * Process the concatenation of the current state and the seed
+ * with the custom hash function.
+ */
+ first = 1;
+ for (;;) {
+ unsigned char tmp[32];
+ unsigned char newG[16];
+
+ /*
+ * Assemble new key H||m into tmp[].
+ */
+ memcpy(tmp, H, 16);
+ if (first) {
+ memcpy(tmp + 16, s, 16);
+ first = 0;
+ } else {
+ size_t clen;
+
+ if (len == 0) {
+ break;
+ }
+ clen = len < 16 ? len : 16;
+ memcpy(tmp + 16, seed, clen);
+ memset(tmp + 16 + clen, 0, 16 - clen);
+ seed = (const unsigned char *)seed + clen;
+ len -= clen;
+ }
+ ctx->sk.vtable->init(&ctx->sk.vtable, tmp, 32);
+
+ /*
+ * Compute new G and H values.
+ */
+ memcpy(iv, G, 12);
+ memcpy(newG, G, 16);
+ ctx->sk.vtable->run(&ctx->sk.vtable, iv,
+ br_dec32be(G + 12), newG, 16);
+ iv[0] ^= 0x01;
+ memcpy(H, G, 16);
+ H[0] ^= 0x01;
+ ctx->sk.vtable->run(&ctx->sk.vtable, iv,
+ br_dec32be(G + 12), H, 16);
+ memcpy(G, newG, 16);
+ }
+
+ /*
+ * Output hash value is H||G. We truncate it to its first 128 bits,
+ * i.e. H; that's our new AES key.
+ */
+ ctx->sk.vtable->init(&ctx->sk.vtable, H, 16);
+ ctx->cc = 0;
+}
+
+/* see bearssl_rand.h */
+const br_prng_class br_aesctr_drbg_vtable = {
+ sizeof(br_aesctr_drbg_context),
+ (void (*)(const br_prng_class **, const void *, const void *, size_t))
+ &br_aesctr_drbg_init,
+ (void (*)(const br_prng_class **, void *, size_t))
+ &br_aesctr_drbg_generate,
+ (void (*)(const br_prng_class **, const void *, size_t))
+ &br_aesctr_drbg_update
+};
diff --git a/test/monniaux/BearSSL/src/rand/hmac_drbg.c b/test/monniaux/BearSSL/src/rand/hmac_drbg.c
new file mode 100644
index 00000000..d746756d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rand/hmac_drbg.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl.h */
+void
+br_hmac_drbg_init(br_hmac_drbg_context *ctx,
+ const br_hash_class *digest_class, const void *seed, size_t len)
+{
+ size_t hlen;
+
+ ctx->vtable = &br_hmac_drbg_vtable;
+ hlen = br_digest_size(digest_class);
+ memset(ctx->K, 0x00, hlen);
+ memset(ctx->V, 0x01, hlen);
+ ctx->digest_class = digest_class;
+ br_hmac_drbg_update(ctx, seed, len);
+}
+
+/* see bearssl.h */
+void
+br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len)
+{
+ const br_hash_class *dig;
+ br_hmac_key_context kc;
+ br_hmac_context hc;
+ size_t hlen;
+ unsigned char *buf;
+ unsigned char x;
+
+ dig = ctx->digest_class;
+ hlen = br_digest_size(dig);
+ br_hmac_key_init(&kc, dig, ctx->K, hlen);
+ buf = out;
+ while (len > 0) {
+ size_t clen;
+
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ br_hmac_out(&hc, ctx->V);
+ clen = hlen;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(buf, ctx->V, clen);
+ buf += clen;
+ len -= clen;
+ }
+
+ /*
+ * To prepare the state for the next request, we should call
+ * br_hmac_drbg_update() with an empty additional seed. However,
+ * we already have an initialized HMAC context with the right
+ * initial key, and we don't want to push another one on the
+ * stack, so we inline that update() call here.
+ */
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ x = 0x00;
+ br_hmac_update(&hc, &x, 1);
+ br_hmac_out(&hc, ctx->K);
+ br_hmac_key_init(&kc, dig, ctx->K, hlen);
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ br_hmac_out(&hc, ctx->V);
+}
+
+/* see bearssl.h */
+void
+br_hmac_drbg_update(br_hmac_drbg_context *ctx, const void *seed, size_t len)
+{
+ const br_hash_class *dig;
+ br_hmac_key_context kc;
+ br_hmac_context hc;
+ size_t hlen;
+ unsigned char x;
+
+ dig = ctx->digest_class;
+ hlen = br_digest_size(dig);
+
+ /*
+ * 1. K = HMAC(K, V || 0x00 || seed)
+ */
+ br_hmac_key_init(&kc, dig, ctx->K, hlen);
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ x = 0x00;
+ br_hmac_update(&hc, &x, 1);
+ br_hmac_update(&hc, seed, len);
+ br_hmac_out(&hc, ctx->K);
+ br_hmac_key_init(&kc, dig, ctx->K, hlen);
+
+ /*
+ * 2. V = HMAC(K, V)
+ */
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ br_hmac_out(&hc, ctx->V);
+
+ /*
+ * 3. If the additional seed is empty, then stop here.
+ */
+ if (len == 0) {
+ return;
+ }
+
+ /*
+ * 4. K = HMAC(K, V || 0x01 || seed)
+ */
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ x = 0x01;
+ br_hmac_update(&hc, &x, 1);
+ br_hmac_update(&hc, seed, len);
+ br_hmac_out(&hc, ctx->K);
+ br_hmac_key_init(&kc, dig, ctx->K, hlen);
+
+ /*
+ * 5. V = HMAC(K, V)
+ */
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, ctx->V, hlen);
+ br_hmac_out(&hc, ctx->V);
+}
+
+/* see bearssl.h */
+const br_prng_class br_hmac_drbg_vtable = {
+ sizeof(br_hmac_drbg_context),
+ (void (*)(const br_prng_class **, const void *, const void *, size_t))
+ &br_hmac_drbg_init,
+ (void (*)(const br_prng_class **, void *, size_t))
+ &br_hmac_drbg_generate,
+ (void (*)(const br_prng_class **, const void *, size_t))
+ &br_hmac_drbg_update
+};
diff --git a/test/monniaux/BearSSL/src/rand/sysrng.c b/test/monniaux/BearSSL/src/rand/sysrng.c
new file mode 100644
index 00000000..5ddbcbea
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rand/sysrng.c
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_USE_URANDOM
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#endif
+
+#if BR_USE_WIN32_RAND
+#include <windows.h>
+#include <wincrypt.h>
+#pragma comment(lib, "advapi32")
+#endif
+
+#if BR_RDRAND
+BR_TARGETS_X86_UP
+BR_TARGET("rdrnd")
+static int
+seeder_rdrand(const br_prng_class **ctx)
+{
+ unsigned char tmp[32];
+ size_t u;
+
+ for (u = 0; u < sizeof tmp; u += sizeof(uint32_t)) {
+ int j;
+ uint32_t x;
+
+ /*
+ * We use the 32-bit intrinsic so that code is compatible
+ * with both 32-bit and 64-bit architectures.
+ *
+ * Intel recommends trying at least 10 times in case of
+ * failure.
+ */
+ for (j = 0; j < 10; j ++) {
+ if (_rdrand32_step(&x)) {
+ goto next_word;
+ }
+ }
+ return 0;
+ next_word:
+ br_enc32le(tmp + u, x);
+ }
+ (*ctx)->update(ctx, tmp, sizeof tmp);
+ return 1;
+}
+BR_TARGETS_X86_DOWN
+
+static int
+rdrand_supported(void)
+{
+ /*
+ * The RDRND support is bit 30 of ECX, as returned by CPUID.
+ */
+ return br_cpuid(0, 0, 0x40000000, 0);
+}
+
+#endif
+
+#if BR_USE_URANDOM
+static int
+seeder_urandom(const br_prng_class **ctx)
+{
+ int f;
+
+ f = open("/dev/urandom", O_RDONLY);
+ if (f >= 0) {
+ unsigned char tmp[32];
+ size_t u;
+
+ for (u = 0; u < sizeof tmp;) {
+ ssize_t len;
+
+ len = read(f, tmp + u, (sizeof tmp) - u);
+ if (len < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ break;
+ }
+ u += (size_t)len;
+ }
+ close(f);
+ if (u == sizeof tmp) {
+ (*ctx)->update(ctx, tmp, sizeof tmp);
+ return 1;
+ }
+ }
+ return 0;
+}
+#endif
+
+#if BR_USE_WIN32_RAND
+static int
+seeder_win32(const br_prng_class **ctx)
+{
+ HCRYPTPROV hp;
+
+ if (CryptAcquireContext(&hp, 0, 0, PROV_RSA_FULL,
+ CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
+ {
+ BYTE buf[32];
+ BOOL r;
+
+ r = CryptGenRandom(hp, sizeof buf, buf);
+ CryptReleaseContext(hp, 0);
+ if (r) {
+ (*ctx)->update(ctx, buf, sizeof buf);
+ return 1;
+ }
+ }
+ return 0;
+}
+#endif
+
+/* see bearssl_rand.h */
+br_prng_seeder
+br_prng_seeder_system(const char **name)
+{
+#if BR_RDRAND
+ if (rdrand_supported()) {
+ if (name != NULL) {
+ *name = "rdrand";
+ }
+ return &seeder_rdrand;
+ }
+#endif
+#if BR_USE_URANDOM
+ if (name != NULL) {
+ *name = "urandom";
+ }
+ return &seeder_urandom;
+#elif BR_USE_WIN32_RAND
+ if (name != NULL) {
+ *name = "win32";
+ }
+ return &seeder_win32;
+#else
+ if (name != NULL) {
+ *name = "none";
+ }
+ return 0;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_keygen.c b/test/monniaux/BearSSL/src/rsa/rsa_default_keygen.c
new file mode 100644
index 00000000..f2e83c8d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_keygen.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_keygen
+br_rsa_keygen_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_keygen;
+#elif BR_LOMUL
+ return &br_rsa_i15_keygen;
+#else
+ return &br_rsa_i31_keygen;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_modulus.c b/test/monniaux/BearSSL/src/rsa/rsa_default_modulus.c
new file mode 100644
index 00000000..57d4be56
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_modulus.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_compute_modulus
+br_rsa_compute_modulus_get_default(void)
+{
+#if BR_LOMUL
+ return &br_rsa_i15_compute_modulus;
+#else
+ return &br_rsa_i31_compute_modulus;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_decrypt.c
new file mode 100644
index 00000000..7345d64e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_decrypt.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_decrypt
+br_rsa_oaep_decrypt_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_oaep_decrypt;
+#elif BR_LOMUL
+ return &br_rsa_i15_oaep_decrypt;
+#else
+ return &br_rsa_i31_oaep_decrypt;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_encrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_encrypt.c
new file mode 100644
index 00000000..ae33fcc6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_oaep_encrypt.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_encrypt
+br_rsa_oaep_encrypt_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_oaep_encrypt;
+#elif BR_LOMUL
+ return &br_rsa_i15_oaep_encrypt;
+#else
+ return &br_rsa_i31_oaep_encrypt;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_sign.c
new file mode 100644
index 00000000..e926704f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_sign.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_sign
+br_rsa_pkcs1_sign_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_pkcs1_sign;
+#elif BR_LOMUL
+ return &br_rsa_i15_pkcs1_sign;
+#else
+ return &br_rsa_i31_pkcs1_sign;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_vrfy.c
new file mode 100644
index 00000000..b3dbeb7b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pkcs1_vrfy.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_vrfy
+br_rsa_pkcs1_vrfy_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_pkcs1_vrfy;
+#elif BR_LOMUL
+ return &br_rsa_i15_pkcs1_vrfy;
+#else
+ return &br_rsa_i31_pkcs1_vrfy;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_priv.c b/test/monniaux/BearSSL/src/rsa/rsa_default_priv.c
new file mode 100644
index 00000000..bb0b2c00
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_priv.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_private
+br_rsa_private_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_private;
+#elif BR_LOMUL
+ return &br_rsa_i15_private;
+#else
+ return &br_rsa_i31_private;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_privexp.c b/test/monniaux/BearSSL/src/rsa/rsa_default_privexp.c
new file mode 100644
index 00000000..cda45559
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_privexp.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_compute_privexp
+br_rsa_compute_privexp_get_default(void)
+{
+#if BR_LOMUL
+ return &br_rsa_i15_compute_privexp;
+#else
+ return &br_rsa_i31_compute_privexp;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pss_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pss_sign.c
new file mode 100644
index 00000000..ce4f3e07
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pss_sign.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_pss_sign
+br_rsa_pss_sign_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_pss_sign;
+#elif BR_LOMUL
+ return &br_rsa_i15_pss_sign;
+#else
+ return &br_rsa_i31_pss_sign;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pss_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pss_vrfy.c
new file mode 100644
index 00000000..e3a9ad9f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pss_vrfy.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_pss_vrfy
+br_rsa_pss_vrfy_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_pss_vrfy;
+#elif BR_LOMUL
+ return &br_rsa_i15_pss_vrfy;
+#else
+ return &br_rsa_i31_pss_vrfy;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pub.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pub.c
new file mode 100644
index 00000000..a1f03ef3
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pub.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_public
+br_rsa_public_get_default(void)
+{
+#if BR_INT128 || BR_UMUL128
+ return &br_rsa_i62_public;
+#elif BR_LOMUL
+ return &br_rsa_i15_public;
+#else
+ return &br_rsa_i31_public;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_default_pubexp.c b/test/monniaux/BearSSL/src/rsa/rsa_default_pubexp.c
new file mode 100644
index 00000000..47bc0005
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_default_pubexp.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+br_rsa_compute_pubexp
+br_rsa_compute_pubexp_get_default(void)
+{
+#if BR_LOMUL
+ return &br_rsa_i15_compute_pubexp;
+#else
+ return &br_rsa_i31_compute_pubexp;
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_keygen.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_keygen.c
new file mode 100644
index 00000000..1c011fe0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_keygen.c
@@ -0,0 +1,583 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Make a random integer of the provided size. The size is encoded.
+ * The header word is untouched.
+ */
+static void
+mkrand(const br_prng_class **rng, uint16_t *x, uint32_t esize)
+{
+ size_t u, len;
+ unsigned m;
+
+ len = (esize + 15) >> 4;
+ (*rng)->generate(rng, x + 1, len * sizeof(uint16_t));
+ for (u = 1; u < len; u ++) {
+ x[u] &= 0x7FFF;
+ }
+ m = esize & 15;
+ if (m == 0) {
+ x[len] &= 0x7FFF;
+ } else {
+ x[len] &= 0x7FFF >> (15 - m);
+ }
+}
+
+/*
+ * This is the big-endian unsigned representation of the product of
+ * all small primes from 13 to 1481.
+ */
+static const unsigned char SMALL_PRIMES[] = {
+ 0x2E, 0xAB, 0x92, 0xD1, 0x8B, 0x12, 0x47, 0x31, 0x54, 0x0A,
+ 0x99, 0x5D, 0x25, 0x5E, 0xE2, 0x14, 0x96, 0x29, 0x1E, 0xB7,
+ 0x78, 0x70, 0xCC, 0x1F, 0xA5, 0xAB, 0x8D, 0x72, 0x11, 0x37,
+ 0xFB, 0xD8, 0x1E, 0x3F, 0x5B, 0x34, 0x30, 0x17, 0x8B, 0xE5,
+ 0x26, 0x28, 0x23, 0xA1, 0x8A, 0xA4, 0x29, 0xEA, 0xFD, 0x9E,
+ 0x39, 0x60, 0x8A, 0xF3, 0xB5, 0xA6, 0xEB, 0x3F, 0x02, 0xB6,
+ 0x16, 0xC3, 0x96, 0x9D, 0x38, 0xB0, 0x7D, 0x82, 0x87, 0x0C,
+ 0xF7, 0xBE, 0x24, 0xE5, 0x5F, 0x41, 0x04, 0x79, 0x76, 0x40,
+ 0xE7, 0x00, 0x22, 0x7E, 0xB5, 0x85, 0x7F, 0x8D, 0x01, 0x50,
+ 0xE9, 0xD3, 0x29, 0x42, 0x08, 0xB3, 0x51, 0x40, 0x7B, 0xD7,
+ 0x8D, 0xCC, 0x10, 0x01, 0x64, 0x59, 0x28, 0xB6, 0x53, 0xF3,
+ 0x50, 0x4E, 0xB1, 0xF2, 0x58, 0xCD, 0x6E, 0xF5, 0x56, 0x3E,
+ 0x66, 0x2F, 0xD7, 0x07, 0x7F, 0x52, 0x4C, 0x13, 0x24, 0xDC,
+ 0x8E, 0x8D, 0xCC, 0xED, 0x77, 0xC4, 0x21, 0xD2, 0xFD, 0x08,
+ 0xEA, 0xD7, 0xC0, 0x5C, 0x13, 0x82, 0x81, 0x31, 0x2F, 0x2B,
+ 0x08, 0xE4, 0x80, 0x04, 0x7A, 0x0C, 0x8A, 0x3C, 0xDC, 0x22,
+ 0xE4, 0x5A, 0x7A, 0xB0, 0x12, 0x5E, 0x4A, 0x76, 0x94, 0x77,
+ 0xC2, 0x0E, 0x92, 0xBA, 0x8A, 0xA0, 0x1F, 0x14, 0x51, 0x1E,
+ 0x66, 0x6C, 0x38, 0x03, 0x6C, 0xC7, 0x4A, 0x4B, 0x70, 0x80,
+ 0xAF, 0xCA, 0x84, 0x51, 0xD8, 0xD2, 0x26, 0x49, 0xF5, 0xA8,
+ 0x5E, 0x35, 0x4B, 0xAC, 0xCE, 0x29, 0x92, 0x33, 0xB7, 0xA2,
+ 0x69, 0x7D, 0x0C, 0xE0, 0x9C, 0xDB, 0x04, 0xD6, 0xB4, 0xBC,
+ 0x39, 0xD7, 0x7F, 0x9E, 0x9D, 0x78, 0x38, 0x7F, 0x51, 0x54,
+ 0x50, 0x8B, 0x9E, 0x9C, 0x03, 0x6C, 0xF5, 0x9D, 0x2C, 0x74,
+ 0x57, 0xF0, 0x27, 0x2A, 0xC3, 0x47, 0xCA, 0xB9, 0xD7, 0x5C,
+ 0xFF, 0xC2, 0xAC, 0x65, 0x4E, 0xBD
+};
+
+/*
+ * We need temporary values for at least 7 integers of the same size
+ * as a factor (including header word); more space helps with performance
+ * (in modular exponentiations), but we much prefer to remain under
+ * 2 kilobytes in total, to save stack space. The macro TEMPS below
+ * exceeds 1024 (which is a count in 16-bit words) when BR_MAX_RSA_SIZE
+ * is greater than 4350 (default value is 4096, so the 2-kB limit is
+ * maintained unless BR_MAX_RSA_SIZE was modified).
+ */
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+#define TEMPS MAX(1024, 7 * ((((BR_MAX_RSA_SIZE + 1) >> 1) + 29) / 15))
+
+/*
+ * Perform trial division on a candidate prime. This computes
+ * y = SMALL_PRIMES mod x, then tries to compute y/y mod x. The
+ * br_i15_moddiv() function will report an error if y is not invertible
+ * modulo x. Returned value is 1 on success (none of the small primes
+ * divides x), 0 on error (a non-trivial GCD is obtained).
+ *
+ * This function assumes that x is odd.
+ */
+static uint32_t
+trial_divisions(const uint16_t *x, uint16_t *t)
+{
+ uint16_t *y;
+ uint16_t x0i;
+
+ y = t;
+ t += 1 + ((x[0] + 15) >> 4);
+ x0i = br_i15_ninv15(x[1]);
+ br_i15_decode_reduce(y, SMALL_PRIMES, sizeof SMALL_PRIMES, x);
+ return br_i15_moddiv(y, y, x, x0i, t);
+}
+
+/*
+ * Perform n rounds of Miller-Rabin on the candidate prime x. This
+ * function assumes that x = 3 mod 4.
+ *
+ * Returned value is 1 on success (all rounds completed successfully),
+ * 0 otherwise.
+ */
+static uint32_t
+miller_rabin(const br_prng_class **rng, const uint16_t *x, int n,
+ uint16_t *t, size_t tlen)
+{
+ /*
+ * Since x = 3 mod 4, the Miller-Rabin test is simple:
+ * - get a random base a (such that 1 < a < x-1)
+ * - compute z = a^((x-1)/2) mod x
+ * - if z != 1 and z != x-1, the number x is composite
+ *
+ * We generate bases 'a' randomly with a size which is
+ * one bit less than x, which ensures that a < x-1. It
+ * is not useful to verify that a > 1 because the probability
+ * that we get a value a equal to 0 or 1 is much smaller
+ * than the probability of our Miller-Rabin tests not to
+ * detect a composite, which is already quite smaller than the
+ * probability of the hardware misbehaving and return a
+ * composite integer because of some glitch (e.g. bad RAM
+ * or ill-timed cosmic ray).
+ */
+ unsigned char *xm1d2;
+ size_t xlen, xm1d2_len, xm1d2_len_u16, u;
+ uint32_t asize;
+ unsigned cc;
+ uint16_t x0i;
+
+ /*
+ * Compute (x-1)/2 (encoded).
+ */
+ xm1d2 = (unsigned char *)t;
+ xm1d2_len = ((x[0] - (x[0] >> 4)) + 7) >> 3;
+ br_i15_encode(xm1d2, xm1d2_len, x);
+ cc = 0;
+ for (u = 0; u < xm1d2_len; u ++) {
+ unsigned w;
+
+ w = xm1d2[u];
+ xm1d2[u] = (unsigned char)((w >> 1) | cc);
+ cc = w << 7;
+ }
+
+ /*
+ * We used some words of the provided buffer for (x-1)/2.
+ */
+ xm1d2_len_u16 = (xm1d2_len + 1) >> 1;
+ t += xm1d2_len_u16;
+ tlen -= xm1d2_len_u16;
+
+ xlen = (x[0] + 15) >> 4;
+ asize = x[0] - 1 - EQ0(x[0] & 15);
+ x0i = br_i15_ninv15(x[1]);
+ while (n -- > 0) {
+ uint16_t *a;
+ uint32_t eq1, eqm1;
+
+ /*
+ * Generate a random base. We don't need the base to be
+ * really uniform modulo x, so we just get a random
+ * number which is one bit shorter than x.
+ */
+ a = t;
+ a[0] = x[0];
+ a[xlen] = 0;
+ mkrand(rng, a, asize);
+
+ /*
+ * Compute a^((x-1)/2) mod x. We assume here that the
+ * function will not fail (the temporary array is large
+ * enough).
+ */
+ br_i15_modpow_opt(a, xm1d2, xm1d2_len,
+ x, x0i, t + 1 + xlen, tlen - 1 - xlen);
+
+ /*
+ * We must obtain either 1 or x-1. Note that x is odd,
+ * hence x-1 differs from x only in its low word (no
+ * carry).
+ */
+ eq1 = a[1] ^ 1;
+ eqm1 = a[1] ^ (x[1] - 1);
+ for (u = 2; u <= xlen; u ++) {
+ eq1 |= a[u];
+ eqm1 |= a[u] ^ x[u];
+ }
+
+ if ((EQ0(eq1) | EQ0(eqm1)) == 0) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/*
+ * Create a random prime of the provided size. 'size' is the _encoded_
+ * bit length. The two top bits and the two bottom bits are set to 1.
+ */
+static void
+mkprime(const br_prng_class **rng, uint16_t *x, uint32_t esize,
+ uint32_t pubexp, uint16_t *t, size_t tlen)
+{
+ size_t len;
+
+ x[0] = esize;
+ len = (esize + 15) >> 4;
+ for (;;) {
+ size_t u;
+ uint32_t m3, m5, m7, m11;
+ int rounds;
+
+ /*
+ * Generate random bits. We force the two top bits and the
+ * two bottom bits to 1.
+ */
+ mkrand(rng, x, esize);
+ if ((esize & 15) == 0) {
+ x[len] |= 0x6000;
+ } else if ((esize & 15) == 1) {
+ x[len] |= 0x0001;
+ x[len - 1] |= 0x4000;
+ } else {
+ x[len] |= 0x0003 << ((esize & 15) - 2);
+ }
+ x[1] |= 0x0003;
+
+ /*
+ * Trial division with low primes (3, 5, 7 and 11). We
+ * use the following properties:
+ *
+ * 2^2 = 1 mod 3
+ * 2^4 = 1 mod 5
+ * 2^3 = 1 mod 7
+ * 2^10 = 1 mod 11
+ */
+ m3 = 0;
+ m5 = 0;
+ m7 = 0;
+ m11 = 0;
+ for (u = 0; u < len; u ++) {
+ uint32_t w;
+
+ w = x[1 + u];
+ m3 += w << (u & 1);
+ m3 = (m3 & 0xFF) + (m3 >> 8);
+ m5 += w << ((4 - u) & 3);
+ m5 = (m5 & 0xFF) + (m5 >> 8);
+ m7 += w;
+ m7 = (m7 & 0x1FF) + (m7 >> 9);
+ m11 += w << (5 & -(u & 1));
+ m11 = (m11 & 0x3FF) + (m11 >> 10);
+ }
+
+ /*
+ * Maximum values of m* at this point:
+ * m3: 511
+ * m5: 2310
+ * m7: 510
+ * m11: 2047
+ * We use the same properties to make further reductions.
+ */
+
+ m3 = (m3 & 0x0F) + (m3 >> 4); /* max: 46 */
+ m3 = (m3 & 0x0F) + (m3 >> 4); /* max: 16 */
+ m3 = ((m3 * 43) >> 5) & 3;
+
+ m5 = (m5 & 0xFF) + (m5 >> 8); /* max: 263 */
+ m5 = (m5 & 0x0F) + (m5 >> 4); /* max: 30 */
+ m5 = (m5 & 0x0F) + (m5 >> 4); /* max: 15 */
+ m5 -= 10 & -GT(m5, 9);
+ m5 -= 5 & -GT(m5, 4);
+
+ m7 = (m7 & 0x3F) + (m7 >> 6); /* max: 69 */
+ m7 = (m7 & 7) + (m7 >> 3); /* max: 14 */
+ m7 = ((m7 * 147) >> 7) & 7;
+
+ /*
+ * 2^5 = 32 = -1 mod 11.
+ */
+ m11 = (m11 & 0x1F) + 66 - (m11 >> 5); /* max: 97 */
+ m11 -= 88 & -GT(m11, 87);
+ m11 -= 44 & -GT(m11, 43);
+ m11 -= 22 & -GT(m11, 21);
+ m11 -= 11 & -GT(m11, 10);
+
+ /*
+ * If any of these modulo is 0, then the candidate is
+ * not prime. Also, if pubexp is 3, 5, 7 or 11, and the
+ * corresponding modulus is 1, then the candidate must
+ * be rejected, because we need e to be invertible
+ * modulo p-1. We can use simple comparisons here
+ * because they won't leak information on a candidate
+ * that we keep, only on one that we reject (and is thus
+ * not secret).
+ */
+ if (m3 == 0 || m5 == 0 || m7 == 0 || m11 == 0) {
+ continue;
+ }
+ if ((pubexp == 3 && m3 == 1)
+ || (pubexp == 5 && m5 == 5)
+ || (pubexp == 7 && m5 == 7)
+ || (pubexp == 11 && m5 == 11))
+ {
+ continue;
+ }
+
+ /*
+ * More trial divisions.
+ */
+ if (!trial_divisions(x, t)) {
+ continue;
+ }
+
+ /*
+ * Miller-Rabin algorithm. Since we selected a random
+ * integer, not a maliciously crafted integer, we can use
+ * relatively few rounds to lower the risk of a false
+ * positive (i.e. declaring prime a non-prime) under
+ * 2^(-80). It is not useful to lower the probability much
+ * below that, since that would be substantially below
+ * the probability of the hardware misbehaving. Sufficient
+ * numbers of rounds are extracted from the Handbook of
+ * Applied Cryptography, note 4.49 (page 149).
+ *
+ * Since we work on the encoded size (esize), we need to
+ * compare with encoded thresholds.
+ */
+ if (esize < 320) {
+ rounds = 12;
+ } else if (esize < 480) {
+ rounds = 9;
+ } else if (esize < 693) {
+ rounds = 6;
+ } else if (esize < 906) {
+ rounds = 4;
+ } else if (esize < 1386) {
+ rounds = 3;
+ } else {
+ rounds = 2;
+ }
+
+ if (miller_rabin(rng, x, rounds, t, tlen)) {
+ return;
+ }
+ }
+}
+
+/*
+ * Let p be a prime (p > 2^33, p = 3 mod 4). Let m = (p-1)/2, provided
+ * as parameter (with announced bit length equal to that of p). This
+ * function computes d = 1/e mod p-1 (for an odd integer e). Returned
+ * value is 1 on success, 0 on error (an error is reported if e is not
+ * invertible modulo p-1).
+ *
+ * The temporary buffer (t) must have room for at least 4 integers of
+ * the size of p.
+ */
+static uint32_t
+invert_pubexp(uint16_t *d, const uint16_t *m, uint32_t e, uint16_t *t)
+{
+ uint16_t *f;
+ uint32_t r;
+
+ f = t;
+ t += 1 + ((m[0] + 15) >> 4);
+
+ /*
+ * Compute d = 1/e mod m. Since p = 3 mod 4, m is odd.
+ */
+ br_i15_zero(d, m[0]);
+ d[1] = 1;
+ br_i15_zero(f, m[0]);
+ f[1] = e & 0x7FFF;
+ f[2] = (e >> 15) & 0x7FFF;
+ f[3] = e >> 30;
+ r = br_i15_moddiv(d, f, m, br_i15_ninv15(m[1]), t);
+
+ /*
+ * We really want d = 1/e mod p-1, with p = 2m. By the CRT,
+ * the result is either the d we got, or d + m.
+ *
+ * Let's write e*d = 1 + k*m, for some integer k. Integers e
+ * and m are odd. If d is odd, then e*d is odd, which implies
+ * that k must be even; in that case, e*d = 1 + (k/2)*2m, and
+ * thus d is already fine. Conversely, if d is even, then k
+ * is odd, and we must add m to d in order to get the correct
+ * result.
+ */
+ br_i15_add(d, m, (uint32_t)(1 - (d[1] & 1)));
+
+ return r;
+}
+
+/*
+ * Swap two buffers in RAM. They must be disjoint.
+ */
+static void
+bufswap(void *b1, void *b2, size_t len)
+{
+ size_t u;
+ unsigned char *buf1, *buf2;
+
+ buf1 = b1;
+ buf2 = b2;
+ for (u = 0; u < len; u ++) {
+ unsigned w;
+
+ w = buf1[u];
+ buf1[u] = buf2[u];
+ buf2[u] = w;
+ }
+}
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_keygen(const br_prng_class **rng,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp)
+{
+ uint32_t esize_p, esize_q;
+ size_t plen, qlen, tlen;
+ uint16_t *p, *q, *t;
+ uint16_t tmp[TEMPS];
+ uint32_t r;
+
+ if (size < BR_MIN_RSA_SIZE || size > BR_MAX_RSA_SIZE) {
+ return 0;
+ }
+ if (pubexp == 0) {
+ pubexp = 3;
+ } else if (pubexp == 1 || (pubexp & 1) == 0) {
+ return 0;
+ }
+
+ esize_p = (size + 1) >> 1;
+ esize_q = size - esize_p;
+ sk->n_bitlen = size;
+ sk->p = kbuf_priv;
+ sk->plen = (esize_p + 7) >> 3;
+ sk->q = sk->p + sk->plen;
+ sk->qlen = (esize_q + 7) >> 3;
+ sk->dp = sk->q + sk->qlen;
+ sk->dplen = sk->plen;
+ sk->dq = sk->dp + sk->dplen;
+ sk->dqlen = sk->qlen;
+ sk->iq = sk->dq + sk->dqlen;
+ sk->iqlen = sk->plen;
+
+ if (pk != NULL) {
+ pk->n = kbuf_pub;
+ pk->nlen = (size + 7) >> 3;
+ pk->e = pk->n + pk->nlen;
+ pk->elen = 4;
+ br_enc32be(pk->e, pubexp);
+ while (*pk->e == 0) {
+ pk->e ++;
+ pk->elen --;
+ }
+ }
+
+ /*
+ * We now switch to encoded sizes.
+ *
+ * floor((x * 17477) / (2^18)) is equal to floor(x/15) for all
+ * integers x from 0 to 23833.
+ */
+ esize_p += MUL15(esize_p, 17477) >> 18;
+ esize_q += MUL15(esize_q, 17477) >> 18;
+ plen = (esize_p + 15) >> 4;
+ qlen = (esize_q + 15) >> 4;
+ p = tmp;
+ q = p + 1 + plen;
+ t = q + 1 + qlen;
+ tlen = ((sizeof tmp) / sizeof(uint16_t)) - (2 + plen + qlen);
+
+ /*
+ * When looking for primes p and q, we temporarily divide
+ * candidates by 2, in order to compute the inverse of the
+ * public exponent.
+ */
+
+ for (;;) {
+ mkprime(rng, p, esize_p, pubexp, t, tlen);
+ br_i15_rshift(p, 1);
+ if (invert_pubexp(t, p, pubexp, t + 1 + plen)) {
+ br_i15_add(p, p, 1);
+ p[1] |= 1;
+ br_i15_encode(sk->p, sk->plen, p);
+ br_i15_encode(sk->dp, sk->dplen, t);
+ break;
+ }
+ }
+
+ for (;;) {
+ mkprime(rng, q, esize_q, pubexp, t, tlen);
+ br_i15_rshift(q, 1);
+ if (invert_pubexp(t, q, pubexp, t + 1 + qlen)) {
+ br_i15_add(q, q, 1);
+ q[1] |= 1;
+ br_i15_encode(sk->q, sk->qlen, q);
+ br_i15_encode(sk->dq, sk->dqlen, t);
+ break;
+ }
+ }
+
+ /*
+ * If p and q have the same size, then it is possible that q > p
+ * (when the target modulus size is odd, we generate p with a
+ * greater bit length than q). If q > p, we want to swap p and q
+ * (and also dp and dq) for two reasons:
+ * - The final step below (inversion of q modulo p) is easier if
+ * p > q.
+ * - While BearSSL's RSA code is perfectly happy with RSA keys such
+ * that p < q, some other implementations have restrictions and
+ * require p > q.
+ *
+ * Note that we can do a simple non-constant-time swap here,
+ * because the only information we leak here is that we insist on
+ * returning p and q such that p > q, which is not a secret.
+ */
+ if (esize_p == esize_q && br_i15_sub(p, q, 0) == 1) {
+ bufswap(p, q, (1 + plen) * sizeof *p);
+ bufswap(sk->p, sk->q, sk->plen);
+ bufswap(sk->dp, sk->dq, sk->dplen);
+ }
+
+ /*
+ * We have produced p, q, dp and dq. We can now compute iq = 1/d mod p.
+ *
+ * We ensured that p >= q, so this is just a matter of updating the
+ * header word for q (and possibly adding an extra word).
+ *
+ * Theoretically, the call below may fail, in case we were
+ * extraordinarily unlucky, and p = q. Another failure case is if
+ * Miller-Rabin failed us _twice_, and p and q are non-prime and
+ * have a factor is common. We report the error mostly because it
+ * is cheap and we can, but in practice this never happens (or, at
+ * least, it happens way less often than hardware glitches).
+ */
+ q[0] = p[0];
+ if (plen > qlen) {
+ q[plen] = 0;
+ t ++;
+ tlen --;
+ }
+ br_i15_zero(t, p[0]);
+ t[1] = 1;
+ r = br_i15_moddiv(t, q, p, br_i15_ninv15(p[1]), t + 1 + plen);
+ br_i15_encode(sk->iq, sk->iqlen, t);
+
+ /*
+ * Compute the public modulus too, if required.
+ */
+ if (pk != NULL) {
+ br_i15_zero(t, p[0]);
+ br_i15_mulacc(t, p, q);
+ br_i15_encode(pk->n, pk->nlen, t);
+ }
+
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_modulus.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_modulus.c
new file mode 100644
index 00000000..16458c3e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_modulus.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i15_compute_modulus(void *n, const br_rsa_private_key *sk)
+{
+ uint16_t tmp[4 * (((BR_MAX_RSA_SIZE / 2) + 14) / 15) + 5];
+ uint16_t *t, *p, *q;
+ const unsigned char *pbuf, *qbuf;
+ size_t nlen, plen, qlen, tlen;
+
+ /*
+ * Compute actual byte and lengths for p and q.
+ */
+ pbuf = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ qbuf = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *qbuf == 0) {
+ qbuf ++;
+ qlen --;
+ }
+
+ t = tmp;
+ tlen = (sizeof tmp) / (sizeof tmp[0]);
+
+ /*
+ * Decode p.
+ */
+ if ((15 * tlen) < (plen << 3) + 15) {
+ return 0;
+ }
+ br_i15_decode(t, pbuf, plen);
+ p = t;
+ plen = (p[0] + 31) >> 4;
+ t += plen;
+ tlen -= plen;
+
+ /*
+ * Decode q.
+ */
+ if ((15 * tlen) < (qlen << 3) + 15) {
+ return 0;
+ }
+ br_i15_decode(t, qbuf, qlen);
+ q = t;
+ qlen = (q[0] + 31) >> 4;
+ t += qlen;
+ tlen -= qlen;
+
+ /*
+ * Computation can proceed only if we have enough room for the
+ * modulus.
+ */
+ if (tlen < (plen + qlen + 1)) {
+ return 0;
+ }
+
+ /*
+ * Private key already contains the modulus bit length, from which
+ * we can infer the output length. Even if n is NULL, we still had
+ * to decode p and q to make sure that the product can be computed.
+ */
+ nlen = (sk->n_bitlen + 7) >> 3;
+ if (n != NULL) {
+ br_i15_zero(t, p[0]);
+ br_i15_mulacc(t, p, q);
+ br_i15_encode(n, nlen, t);
+ }
+ return nlen;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_decrypt.c
new file mode 100644
index 00000000..927eecd8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_decrypt.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_oaep_decrypt(const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len)
+{
+ uint32_t r;
+
+ if (*len != ((sk->n_bitlen + 7) >> 3)) {
+ return 0;
+ }
+ r = br_rsa_i15_private(data, sk);
+ r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_encrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_encrypt.c
new file mode 100644
index 00000000..b9a6cfa4
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_oaep_encrypt.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i15_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len)
+{
+ size_t dlen;
+
+ dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
+ pk, dst, dst_max_len, src, src_len);
+ if (dlen == 0) {
+ return 0;
+ }
+ return dlen & -(size_t)br_rsa_i15_public(dst, dlen, pk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_sign.c
new file mode 100644
index 00000000..f519423d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_sign.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) {
+ return 0;
+ }
+ return br_rsa_i15_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_vrfy.c
new file mode 100644
index 00000000..2c351849
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pkcs1_vrfy.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i15_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_priv.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_priv.c
new file mode 100644
index 00000000..177cc3a6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_priv.c
@@ -0,0 +1,209 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define U (2 + ((BR_MAX_RSA_FACTOR + 14) / 15))
+#define TLEN (8 * U)
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
+{
+ const unsigned char *p, *q;
+ size_t plen, qlen;
+ size_t fwlen;
+ uint16_t p0i, q0i;
+ size_t xlen, u;
+ uint16_t tmp[1 + TLEN];
+ long z;
+ uint16_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
+ uint32_t r;
+
+ /*
+ * Compute the actual lengths of p and q, in bytes.
+ * These lengths are not considered secret (we cannot really hide
+ * them anyway in constant-time code).
+ */
+ p = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *p == 0) {
+ p ++;
+ plen --;
+ }
+ q = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *q == 0) {
+ q ++;
+ qlen --;
+ }
+
+ /*
+ * Compute the maximum factor length, in words.
+ */
+ z = (long)(plen > qlen ? plen : qlen) << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 15;
+ fwlen ++;
+ }
+ /*
+ * Round up the word length to an even number.
+ */
+ fwlen += (fwlen & 1);
+
+ /*
+ * We need to fit at least 6 values in the stack buffer.
+ */
+ if (6 * fwlen > TLEN) {
+ return 0;
+ }
+
+ /*
+ * Compute signature length (in bytes).
+ */
+ xlen = (sk->n_bitlen + 7) >> 3;
+
+ /*
+ * Ensure 32-bit alignment for value words.
+ */
+ mq = tmp;
+ if (((uintptr_t)mq & 2) == 0) {
+ mq ++;
+ }
+
+ /*
+ * Decode q.
+ */
+ br_i15_decode(mq, q, qlen);
+
+ /*
+ * Decode p.
+ */
+ t1 = mq + fwlen;
+ br_i15_decode(t1, p, plen);
+
+ /*
+ * Compute the modulus (product of the two factors), to compare
+ * it with the source value. We use br_i15_mulacc(), since it's
+ * already used later on.
+ */
+ t2 = mq + 2 * fwlen;
+ br_i15_zero(t2, mq[0]);
+ br_i15_mulacc(t2, mq, t1);
+
+ /*
+ * We encode the modulus into bytes, to perform the comparison
+ * with bytes. We know that the product length, in bytes, is
+ * exactly xlen.
+ * The comparison actually computes the carry when subtracting
+ * the modulus from the source value; that carry must be 1 for
+ * a value in the correct range. We keep it in r, which is our
+ * accumulator for the error code.
+ */
+ t3 = mq + 4 * fwlen;
+ br_i15_encode(t3, xlen, t2);
+ u = xlen;
+ r = 0;
+ while (u > 0) {
+ uint32_t wn, wx;
+
+ u --;
+ wn = ((unsigned char *)t3)[u];
+ wx = x[u];
+ r = ((wx - (wn + r)) >> 8) & 1;
+ }
+
+ /*
+ * Move the decoded p to another temporary buffer.
+ */
+ mp = mq + 2 * fwlen;
+ memmove(mp, t1, fwlen * sizeof *t1);
+
+ /*
+ * Compute s2 = x^dq mod q.
+ */
+ q0i = br_i15_ninv15(mq[1]);
+ s2 = mq + fwlen;
+ br_i15_decode_reduce(s2, x, xlen, mq);
+ r &= br_i15_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
+ mq + 3 * fwlen, TLEN - 3 * fwlen);
+
+ /*
+ * Compute s1 = x^dq mod q.
+ */
+ p0i = br_i15_ninv15(mp[1]);
+ s1 = mq + 3 * fwlen;
+ br_i15_decode_reduce(s1, x, xlen, mp);
+ r &= br_i15_modpow_opt(s1, sk->dp, sk->dplen, mp, p0i,
+ mq + 4 * fwlen, TLEN - 4 * fwlen);
+
+ /*
+ * Compute:
+ * h = (s1 - s2)*(1/q) mod p
+ * s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is
+ * unclear about whether p may be lower than q (some existing,
+ * widely deployed implementations of RSA don't tolerate p < q),
+ * but we want to support that occurrence, so we need to use the
+ * reduction function.
+ *
+ * Since we use br_i15_decode_reduce() for iq (purportedly, the
+ * inverse of q modulo p), we also tolerate improperly large
+ * values for this parameter.
+ */
+ t1 = mq + 4 * fwlen;
+ t2 = mq + 5 * fwlen;
+ br_i15_reduce(t2, s2, mp);
+ br_i15_add(s1, mp, br_i15_sub(s1, t2, 1));
+ br_i15_to_monty(s1, mp);
+ br_i15_decode_reduce(t1, sk->iq, sk->iqlen, mp);
+ br_i15_montymul(t2, s1, t1, mp, p0i);
+
+ /*
+ * h is now in t2. We compute the final result:
+ * s = s2 + q*h
+ * All these operations are non-modular.
+ *
+ * We need mq, s2 and t2. We use the t3 buffer as destination.
+ * The buffers mp, s1 and t1 are no longer needed, so we can
+ * reuse them for t3. Moreover, the first step of the computation
+ * is to copy s2 into t3, after which s2 is not needed. Right
+ * now, mq is in slot 0, s2 is in slot 1, and t2 in slot 5.
+ * Therefore, we have ample room for t3 by simply using s2.
+ */
+ t3 = s2;
+ br_i15_mulacc(t3, mq, t2);
+
+ /*
+ * Encode the result. Since we already checked the value of xlen,
+ * we can just use it right away.
+ */
+ br_i15_encode(x, xlen, t3);
+
+ /*
+ * The only error conditions remaining at that point are invalid
+ * values for p and q (even integers).
+ */
+ return p0i & q0i & r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_privexp.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_privexp.c
new file mode 100644
index 00000000..57d6918e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_privexp.c
@@ -0,0 +1,320 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i15_compute_privexp(void *d,
+ const br_rsa_private_key *sk, uint32_t e)
+{
+ /*
+ * We want to invert e modulo phi = (p-1)(q-1). This first
+ * requires computing phi, which is easy since we have the factors
+ * p and q in the private key structure.
+ *
+ * Since p = 3 mod 4 and q = 3 mod 4, phi/4 is an odd integer.
+ * We could invert e modulo phi/4 then patch the result to
+ * modulo phi, but this would involve assembling three modulus-wide
+ * values (phi/4, 1 and e) and calling moddiv, that requires
+ * three more temporaries, for a total of six big integers, or
+ * slightly more than 3 kB of stack space for RSA-4096. This
+ * exceeds our stack requirements.
+ *
+ * Instead, we first use one step of the extended GCD:
+ *
+ * - We compute phi = k*e + r (Euclidean division of phi by e).
+ * If public exponent e is correct, then r != 0 (e must be
+ * invertible modulo phi). We also have k != 0 since we
+ * enforce non-ridiculously-small factors.
+ *
+ * - We find small u, v such that u*e - v*r = 1 (using a
+ * binary GCD; we can arrange for u < r and v < e, i.e. all
+ * values fit on 32 bits).
+ *
+ * - Solution is: d = u + v*k
+ * This last computation is exact: since u < r and v < e,
+ * the above implies d < r + e*((phi-r)/e) = phi
+ */
+
+ uint16_t tmp[4 * ((BR_MAX_RSA_FACTOR + 14) / 15) + 12];
+ uint16_t *p, *q, *k, *m, *z, *phi;
+ const unsigned char *pbuf, *qbuf;
+ size_t plen, qlen, u, len, dlen;
+ uint32_t r, a, b, u0, v0, u1, v1, he, hr;
+ int i;
+
+ /*
+ * Check that e is correct.
+ */
+ if (e < 3 || (e & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Check lengths of p and q, and that they are both odd.
+ */
+ pbuf = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ if (plen < 5 || plen > (BR_MAX_RSA_FACTOR / 8)
+ || (pbuf[plen - 1] & 1) != 1)
+ {
+ return 0;
+ }
+ qbuf = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *qbuf == 0) {
+ qbuf ++;
+ qlen --;
+ }
+ if (qlen < 5 || qlen > (BR_MAX_RSA_FACTOR / 8)
+ || (qbuf[qlen - 1] & 1) != 1)
+ {
+ return 0;
+ }
+
+ /*
+ * Output length is that of the modulus.
+ */
+ dlen = (sk->n_bitlen + 7) >> 3;
+ if (d == NULL) {
+ return dlen;
+ }
+
+ p = tmp;
+ br_i15_decode(p, pbuf, plen);
+ plen = (p[0] + 15) >> 4;
+ q = p + 1 + plen;
+ br_i15_decode(q, qbuf, qlen);
+ qlen = (q[0] + 15) >> 4;
+
+ /*
+ * Compute phi = (p-1)*(q-1), then move it over p-1 and q-1 (that
+ * we do not need anymore). The mulacc function sets the announced
+ * bit length of t to be the sum of the announced bit lengths of
+ * p-1 and q-1, which is usually exact but may overshoot by one 1
+ * bit in some cases; we readjust it to its true length.
+ */
+ p[1] --;
+ q[1] --;
+ phi = q + 1 + qlen;
+ br_i15_zero(phi, p[0]);
+ br_i15_mulacc(phi, p, q);
+ len = (phi[0] + 15) >> 4;
+ memmove(tmp, phi, (1 + len) * sizeof *phi);
+ phi = tmp;
+ phi[0] = br_i15_bit_length(phi + 1, len);
+ len = (phi[0] + 15) >> 4;
+
+ /*
+ * Divide phi by public exponent e. The final remainder r must be
+ * non-zero (otherwise, the key is invalid). The quotient is k,
+ * which we write over phi, since we don't need phi after that.
+ */
+ r = 0;
+ for (u = len; u >= 1; u --) {
+ /*
+ * Upon entry, r < e, and phi[u] < 2^15; hence,
+ * hi:lo < e*2^15. Thus, the produced word k[u]
+ * must be lower than 2^15, and the new remainder r
+ * is lower than e.
+ */
+ uint32_t hi, lo;
+
+ hi = r >> 17;
+ lo = (r << 15) + phi[u];
+ phi[u] = br_divrem(hi, lo, e, &r);
+ }
+ if (r == 0) {
+ return 0;
+ }
+ k = phi;
+
+ /*
+ * Compute u and v such that u*e - v*r = GCD(e,r). We use
+ * a binary GCD algorithm, with 6 extra integers a, b,
+ * u0, u1, v0 and v1. Initial values are:
+ * a = e u0 = 1 v0 = 0
+ * b = r u1 = r v1 = e-1
+ * The following invariants are maintained:
+ * a = u0*e - v0*r
+ * b = u1*e - v1*r
+ * 0 < a <= e
+ * 0 < b <= r
+ * 0 <= u0 <= r
+ * 0 <= v0 <= e
+ * 0 <= u1 <= r
+ * 0 <= v1 <= e
+ *
+ * At each iteration, we reduce either a or b by one bit, and
+ * adjust u0, u1, v0 and v1 to maintain the invariants:
+ * - if a is even, then a <- a/2
+ * - otherwise, if b is even, then b <- b/2
+ * - otherwise, if a > b, then a <- (a-b)/2
+ * - otherwise, if b > a, then b <- (b-a)/2
+ * Algorithm stops when a = b. At that point, the common value
+ * is the GCD of e and r; it must be 1 (otherwise, the private
+ * key or public exponent is not valid). The (u0,v0) or (u1,v1)
+ * pairs are the solution we are looking for.
+ *
+ * Since either a or b is reduced by at least 1 bit at each
+ * iteration, 62 iterations are enough to reach the end
+ * condition.
+ *
+ * To maintain the invariants, we must compute the same operations
+ * on the u* and v* values that we do on a and b:
+ * - When a is divided by 2, u0 and v0 must be divided by 2.
+ * - When b is divided by 2, u1 and v1 must be divided by 2.
+ * - When b is subtracted from a, u1 and v1 are subtracted from
+ * u0 and v0, respectively.
+ * - When a is subtracted from b, u0 and v0 are subtracted from
+ * u1 and v1, respectively.
+ *
+ * However, we want to keep the u* and v* values in their proper
+ * ranges. The following remarks apply:
+ *
+ * - When a is divided by 2, then a is even. Therefore:
+ *
+ * * If r is odd, then u0 and v0 must have the same parity;
+ * if they are both odd, then adding r to u0 and e to v0
+ * makes them both even, and the division by 2 brings them
+ * back to the proper range.
+ *
+ * * If r is even, then u0 must be even; if v0 is odd, then
+ * adding r to u0 and e to v0 makes them both even, and the
+ * division by 2 brings them back to the proper range.
+ *
+ * Thus, all we need to do is to look at the parity of v0,
+ * and add (r,e) to (u0,v0) when v0 is odd. In order to avoid
+ * a 32-bit overflow, we can add ((r+1)/2,(e/2)+1) after the
+ * division (r+1 does not overflow since r < e; and (e/2)+1
+ * is equal to (e+1)/2 since e is odd).
+ *
+ * - When we subtract b from a, three cases may occur:
+ *
+ * * u1 <= u0 and v1 <= v0: just do the subtractions
+ *
+ * * u1 > u0 and v1 > v0: compute:
+ * (u0, v0) <- (u0 + r - u1, v0 + e - v1)
+ *
+ * * u1 <= u0 and v1 > v0: compute:
+ * (u0, v0) <- (u0 + r - u1, v0 + e - v1)
+ *
+ * The fourth case (u1 > u0 and v1 <= v0) is not possible
+ * because it would contradict "b < a" (which is the reason
+ * why we subtract b from a).
+ *
+ * The tricky case is the third one: from the equations, it
+ * seems that u0 may go out of range. However, the invariants
+ * and ranges of other values imply that, in that case, the
+ * new u0 does not actually exceed the range.
+ *
+ * We can thus handle the subtraction by adding (r,e) based
+ * solely on the comparison between v0 and v1.
+ */
+ a = e;
+ b = r;
+ u0 = 1;
+ v0 = 0;
+ u1 = r;
+ v1 = e - 1;
+ hr = (r + 1) >> 1;
+ he = (e >> 1) + 1;
+ for (i = 0; i < 62; i ++) {
+ uint32_t oa, ob, agtb, bgta;
+ uint32_t sab, sba, da, db;
+ uint32_t ctl;
+
+ oa = a & 1; /* 1 if a is odd */
+ ob = b & 1; /* 1 if b is odd */
+ agtb = GT(a, b); /* 1 if a > b */
+ bgta = GT(b, a); /* 1 if b > a */
+
+ sab = oa & ob & agtb; /* 1 if a <- a-b */
+ sba = oa & ob & bgta; /* 1 if b <- b-a */
+
+ /* a <- a-b, u0 <- u0-u1, v0 <- v0-v1 */
+ ctl = GT(v1, v0);
+ a -= b & -sab;
+ u0 -= (u1 - (r & -ctl)) & -sab;
+ v0 -= (v1 - (e & -ctl)) & -sab;
+
+ /* b <- b-a, u1 <- u1-u0 mod r, v1 <- v1-v0 mod e */
+ ctl = GT(v0, v1);
+ b -= a & -sba;
+ u1 -= (u0 - (r & -ctl)) & -sba;
+ v1 -= (v0 - (e & -ctl)) & -sba;
+
+ da = NOT(oa) | sab; /* 1 if a <- a/2 */
+ db = (oa & NOT(ob)) | sba; /* 1 if b <- b/2 */
+
+ /* a <- a/2, u0 <- u0/2, v0 <- v0/2 */
+ ctl = v0 & 1;
+ a ^= (a ^ (a >> 1)) & -da;
+ u0 ^= (u0 ^ ((u0 >> 1) + (hr & -ctl))) & -da;
+ v0 ^= (v0 ^ ((v0 >> 1) + (he & -ctl))) & -da;
+
+ /* b <- b/2, u1 <- u1/2 mod r, v1 <- v1/2 mod e */
+ ctl = v1 & 1;
+ b ^= (b ^ (b >> 1)) & -db;
+ u1 ^= (u1 ^ ((u1 >> 1) + (hr & -ctl))) & -db;
+ v1 ^= (v1 ^ ((v1 >> 1) + (he & -ctl))) & -db;
+ }
+
+ /*
+ * Check that the GCD is indeed 1. If not, then the key is invalid
+ * (and there's no harm in leaking that piece of information).
+ */
+ if (a != 1) {
+ return 0;
+ }
+
+ /*
+ * Now we have u0*e - v0*r = 1. Let's compute the result as:
+ * d = u0 + v0*k
+ * We still have k in the tmp[] array, and its announced bit
+ * length is that of phi.
+ */
+ m = k + 1 + len;
+ m[0] = (2 << 4) + 2; /* bit length is 32 bits, encoded */
+ m[1] = v0 & 0x7FFF;
+ m[2] = (v0 >> 15) & 0x7FFF;
+ m[3] = v0 >> 30;
+ z = m + 4;
+ br_i15_zero(z, k[0]);
+ z[1] = u0 & 0x7FFF;
+ z[2] = (u0 >> 15) & 0x7FFF;
+ z[3] = u0 >> 30;
+ br_i15_mulacc(z, k, m);
+
+ /*
+ * Encode the result.
+ */
+ br_i15_encode(d, dlen, z);
+ return dlen;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_sign.c
new file mode 100644
index 00000000..dd9385b0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_sign.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pss_sig_pad(rng, hf_data, hf_mgf1, hash,
+ salt_len, sk->n_bitlen, x))
+ {
+ return 0;
+ }
+ return br_rsa_i15_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_vrfy.c
new file mode 100644
index 00000000..7d9f2cbc
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pss_vrfy.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i15_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pss_sig_unpad(hf_data, hf_mgf1,
+ hash, salt_len, pk, sig);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pub.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pub.c
new file mode 100644
index 00000000..9eab5e84
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pub.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * As a strict minimum, we need four buffers that can hold a
+ * modular integer.
+ */
+#define TLEN (4 * (2 + ((BR_MAX_RSA_SIZE + 14) / 15)))
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk)
+{
+ const unsigned char *n;
+ size_t nlen;
+ uint16_t tmp[1 + TLEN];
+ uint16_t *m, *a, *t;
+ size_t fwlen;
+ long z;
+ uint16_t m0i;
+ uint32_t r;
+
+ /*
+ * Get the actual length of the modulus, and see if it fits within
+ * our stack buffer. We also check that the length of x[] is valid.
+ */
+ n = pk->n;
+ nlen = pk->nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+ if (nlen == 0 || nlen > (BR_MAX_RSA_SIZE >> 3) || xlen != nlen) {
+ return 0;
+ }
+ z = (long)nlen << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 15;
+ fwlen ++;
+ }
+ /*
+ * Round up length to an even number.
+ */
+ fwlen += (fwlen & 1);
+
+ /*
+ * The modulus gets decoded into m[].
+ * The value to exponentiate goes into a[].
+ * The temporaries for modular exponentiations are in t[].
+ *
+ * We want the first value word of each integer to be aligned
+ * on a 32-bit boundary.
+ */
+ m = tmp;
+ if (((uintptr_t)m & 2) == 0) {
+ m ++;
+ }
+ a = m + fwlen;
+ t = m + 2 * fwlen;
+
+ /*
+ * Decode the modulus.
+ */
+ br_i15_decode(m, n, nlen);
+ m0i = br_i15_ninv15(m[1]);
+
+ /*
+ * Note: if m[] is even, then m0i == 0. Otherwise, m0i must be
+ * an odd integer.
+ */
+ r = m0i & 1;
+
+ /*
+ * Decode x[] into a[]; we also check that its value is proper.
+ */
+ r &= br_i15_decode_mod(a, x, xlen, m);
+
+ /*
+ * Compute the modular exponentiation.
+ */
+ br_i15_modpow_opt(a, pk->e, pk->elen, m, m0i, t, TLEN - 2 * fwlen);
+
+ /*
+ * Encode the result.
+ */
+ br_i15_encode(x, xlen, a);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i15_pubexp.c b/test/monniaux/BearSSL/src/rsa/rsa_i15_pubexp.c
new file mode 100644
index 00000000..803bff79
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i15_pubexp.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Recompute public exponent, based on factor p and reduced private
+ * exponent dp.
+ */
+static uint32_t
+get_pubexp(const unsigned char *pbuf, size_t plen,
+ const unsigned char *dpbuf, size_t dplen)
+{
+ /*
+ * dp is the inverse of e modulo p-1. If p = 3 mod 4, then
+ * p-1 = 2*((p-1)/2). Taken modulo 2, e is odd and has inverse 1;
+ * thus, dp must be odd.
+ *
+ * We compute the inverse of dp modulo (p-1)/2. This requires
+ * first reducing dp modulo (p-1)/2 (this can be done with a
+ * conditional subtract, no need to use the generic modular
+ * reduction function); then, we use moddiv.
+ */
+
+ uint16_t tmp[6 * ((BR_MAX_RSA_FACTOR + 29) / 15)];
+ uint16_t *p, *dp, *x;
+ size_t len;
+ uint32_t e;
+
+ /*
+ * Compute actual factor length (in bytes) and check that it fits
+ * under our size constraints.
+ */
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ if (plen == 0 || plen < 5 || plen > (BR_MAX_RSA_FACTOR / 8)) {
+ return 0;
+ }
+
+ /*
+ * Compute actual reduced exponent length (in bytes) and check that
+ * it is not longer than p.
+ */
+ while (dplen > 0 && *dpbuf == 0) {
+ dpbuf ++;
+ dplen --;
+ }
+ if (dplen > plen || dplen == 0
+ || (dplen == plen && dpbuf[0] > pbuf[0]))
+ {
+ return 0;
+ }
+
+ /*
+ * Verify that p = 3 mod 4 and that dp is odd.
+ */
+ if ((pbuf[plen - 1] & 3) != 3 || (dpbuf[dplen - 1] & 1) != 1) {
+ return 0;
+ }
+
+ /*
+ * Decode p and compute (p-1)/2.
+ */
+ p = tmp;
+ br_i15_decode(p, pbuf, plen);
+ len = (p[0] + 31) >> 4;
+ br_i15_rshift(p, 1);
+
+ /*
+ * Decode dp and make sure its announced bit length matches that of
+ * p (we already know that the size of dp, in bits, does not exceed
+ * the size of p, so we just have to copy the header word).
+ */
+ dp = p + len;
+ memset(dp, 0, len * sizeof *dp);
+ br_i15_decode(dp, dpbuf, dplen);
+ dp[0] = p[0];
+
+ /*
+ * Subtract (p-1)/2 from dp if necessary.
+ */
+ br_i15_sub(dp, p, NOT(br_i15_sub(dp, p, 0)));
+
+ /*
+ * If another subtraction is needed, then this means that the
+ * value was invalid. We don't care to leak information about
+ * invalid keys.
+ */
+ if (br_i15_sub(dp, p, 0) == 0) {
+ return 0;
+ }
+
+ /*
+ * Invert dp modulo (p-1)/2. If the inversion fails, then the
+ * key value was invalid.
+ */
+ x = dp + len;
+ br_i15_zero(x, p[0]);
+ x[1] = 1;
+ if (br_i15_moddiv(x, dp, p, br_i15_ninv15(p[1]), x + len) == 0) {
+ return 0;
+ }
+
+ /*
+ * We now have an inverse. We must set it to zero (error) if its
+ * length is greater than 32 bits and/or if it is an even integer.
+ * Take care that the bit_length function returns an encoded
+ * bit length.
+ */
+ e = (uint32_t)x[1] | ((uint32_t)x[2] << 15) | ((uint32_t)x[3] << 30);
+ e &= -LT(br_i15_bit_length(x + 1, len - 1), 35);
+ e &= -(e & 1);
+ return e;
+}
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i15_compute_pubexp(const br_rsa_private_key *sk)
+{
+ /*
+ * Get the public exponent from both p and q. This is the right
+ * exponent if we get twice the same value.
+ */
+ uint32_t ep, eq;
+
+ ep = get_pubexp(sk->p, sk->plen, sk->dp, sk->dplen);
+ eq = get_pubexp(sk->q, sk->qlen, sk->dq, sk->dqlen);
+ return ep & -EQ(ep, eq);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen.c
new file mode 100644
index 00000000..77708f8b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_keygen(const br_prng_class **rng,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp)
+{
+ return br_rsa_i31_keygen_inner(rng,
+ sk, kbuf_priv, pk, kbuf_pub, size, pubexp,
+ &br_i31_modpow_opt);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen_inner.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen_inner.c
new file mode 100644
index 00000000..9ec881b5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_keygen_inner.c
@@ -0,0 +1,608 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Make a random integer of the provided size. The size is encoded.
+ * The header word is untouched.
+ */
+static void
+mkrand(const br_prng_class **rng, uint32_t *x, uint32_t esize)
+{
+ size_t u, len;
+ unsigned m;
+
+ len = (esize + 31) >> 5;
+ (*rng)->generate(rng, x + 1, len * sizeof(uint32_t));
+ for (u = 1; u < len; u ++) {
+ x[u] &= 0x7FFFFFFF;
+ }
+ m = esize & 31;
+ if (m == 0) {
+ x[len] &= 0x7FFFFFFF;
+ } else {
+ x[len] &= 0x7FFFFFFF >> (31 - m);
+ }
+}
+
+/*
+ * This is the big-endian unsigned representation of the product of
+ * all small primes from 13 to 1481.
+ */
+static const unsigned char SMALL_PRIMES[] = {
+ 0x2E, 0xAB, 0x92, 0xD1, 0x8B, 0x12, 0x47, 0x31, 0x54, 0x0A,
+ 0x99, 0x5D, 0x25, 0x5E, 0xE2, 0x14, 0x96, 0x29, 0x1E, 0xB7,
+ 0x78, 0x70, 0xCC, 0x1F, 0xA5, 0xAB, 0x8D, 0x72, 0x11, 0x37,
+ 0xFB, 0xD8, 0x1E, 0x3F, 0x5B, 0x34, 0x30, 0x17, 0x8B, 0xE5,
+ 0x26, 0x28, 0x23, 0xA1, 0x8A, 0xA4, 0x29, 0xEA, 0xFD, 0x9E,
+ 0x39, 0x60, 0x8A, 0xF3, 0xB5, 0xA6, 0xEB, 0x3F, 0x02, 0xB6,
+ 0x16, 0xC3, 0x96, 0x9D, 0x38, 0xB0, 0x7D, 0x82, 0x87, 0x0C,
+ 0xF7, 0xBE, 0x24, 0xE5, 0x5F, 0x41, 0x04, 0x79, 0x76, 0x40,
+ 0xE7, 0x00, 0x22, 0x7E, 0xB5, 0x85, 0x7F, 0x8D, 0x01, 0x50,
+ 0xE9, 0xD3, 0x29, 0x42, 0x08, 0xB3, 0x51, 0x40, 0x7B, 0xD7,
+ 0x8D, 0xCC, 0x10, 0x01, 0x64, 0x59, 0x28, 0xB6, 0x53, 0xF3,
+ 0x50, 0x4E, 0xB1, 0xF2, 0x58, 0xCD, 0x6E, 0xF5, 0x56, 0x3E,
+ 0x66, 0x2F, 0xD7, 0x07, 0x7F, 0x52, 0x4C, 0x13, 0x24, 0xDC,
+ 0x8E, 0x8D, 0xCC, 0xED, 0x77, 0xC4, 0x21, 0xD2, 0xFD, 0x08,
+ 0xEA, 0xD7, 0xC0, 0x5C, 0x13, 0x82, 0x81, 0x31, 0x2F, 0x2B,
+ 0x08, 0xE4, 0x80, 0x04, 0x7A, 0x0C, 0x8A, 0x3C, 0xDC, 0x22,
+ 0xE4, 0x5A, 0x7A, 0xB0, 0x12, 0x5E, 0x4A, 0x76, 0x94, 0x77,
+ 0xC2, 0x0E, 0x92, 0xBA, 0x8A, 0xA0, 0x1F, 0x14, 0x51, 0x1E,
+ 0x66, 0x6C, 0x38, 0x03, 0x6C, 0xC7, 0x4A, 0x4B, 0x70, 0x80,
+ 0xAF, 0xCA, 0x84, 0x51, 0xD8, 0xD2, 0x26, 0x49, 0xF5, 0xA8,
+ 0x5E, 0x35, 0x4B, 0xAC, 0xCE, 0x29, 0x92, 0x33, 0xB7, 0xA2,
+ 0x69, 0x7D, 0x0C, 0xE0, 0x9C, 0xDB, 0x04, 0xD6, 0xB4, 0xBC,
+ 0x39, 0xD7, 0x7F, 0x9E, 0x9D, 0x78, 0x38, 0x7F, 0x51, 0x54,
+ 0x50, 0x8B, 0x9E, 0x9C, 0x03, 0x6C, 0xF5, 0x9D, 0x2C, 0x74,
+ 0x57, 0xF0, 0x27, 0x2A, 0xC3, 0x47, 0xCA, 0xB9, 0xD7, 0x5C,
+ 0xFF, 0xC2, 0xAC, 0x65, 0x4E, 0xBD
+};
+
+/*
+ * We need temporary values for at least 7 integers of the same size
+ * as a factor (including header word); more space helps with performance
+ * (in modular exponentiations), but we much prefer to remain under
+ * 2 kilobytes in total, to save stack space. The macro TEMPS below
+ * exceeds 512 (which is a count in 32-bit words) when BR_MAX_RSA_SIZE
+ * is greater than 4464 (default value is 4096, so the 2-kB limit is
+ * maintained unless BR_MAX_RSA_SIZE was modified).
+ */
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+#define ROUND2(x) ((((x) + 1) >> 1) << 1)
+
+#define TEMPS MAX(512, ROUND2(7 * ((((BR_MAX_RSA_SIZE + 1) >> 1) + 61) / 31)))
+
+/*
+ * Perform trial division on a candidate prime. This computes
+ * y = SMALL_PRIMES mod x, then tries to compute y/y mod x. The
+ * br_i31_moddiv() function will report an error if y is not invertible
+ * modulo x. Returned value is 1 on success (none of the small primes
+ * divides x), 0 on error (a non-trivial GCD is obtained).
+ *
+ * This function assumes that x is odd.
+ */
+static uint32_t
+trial_divisions(const uint32_t *x, uint32_t *t)
+{
+ uint32_t *y;
+ uint32_t x0i;
+
+ y = t;
+ t += 1 + ((x[0] + 31) >> 5);
+ x0i = br_i31_ninv31(x[1]);
+ br_i31_decode_reduce(y, SMALL_PRIMES, sizeof SMALL_PRIMES, x);
+ return br_i31_moddiv(y, y, x, x0i, t);
+}
+
+/*
+ * Perform n rounds of Miller-Rabin on the candidate prime x. This
+ * function assumes that x = 3 mod 4.
+ *
+ * Returned value is 1 on success (all rounds completed successfully),
+ * 0 otherwise.
+ */
+static uint32_t
+miller_rabin(const br_prng_class **rng, const uint32_t *x, int n,
+ uint32_t *t, size_t tlen, br_i31_modpow_opt_type mp31)
+{
+ /*
+ * Since x = 3 mod 4, the Miller-Rabin test is simple:
+ * - get a random base a (such that 1 < a < x-1)
+ * - compute z = a^((x-1)/2) mod x
+ * - if z != 1 and z != x-1, the number x is composite
+ *
+ * We generate bases 'a' randomly with a size which is
+ * one bit less than x, which ensures that a < x-1. It
+ * is not useful to verify that a > 1 because the probability
+ * that we get a value a equal to 0 or 1 is much smaller
+ * than the probability of our Miller-Rabin tests not to
+ * detect a composite, which is already quite smaller than the
+ * probability of the hardware misbehaving and return a
+ * composite integer because of some glitch (e.g. bad RAM
+ * or ill-timed cosmic ray).
+ */
+ unsigned char *xm1d2;
+ size_t xlen, xm1d2_len, xm1d2_len_u32, u;
+ uint32_t asize;
+ unsigned cc;
+ uint32_t x0i;
+
+ /*
+ * Compute (x-1)/2 (encoded).
+ */
+ xm1d2 = (unsigned char *)t;
+ xm1d2_len = ((x[0] - (x[0] >> 5)) + 7) >> 3;
+ br_i31_encode(xm1d2, xm1d2_len, x);
+ cc = 0;
+ for (u = 0; u < xm1d2_len; u ++) {
+ unsigned w;
+
+ w = xm1d2[u];
+ xm1d2[u] = (unsigned char)((w >> 1) | cc);
+ cc = w << 7;
+ }
+
+ /*
+ * We used some words of the provided buffer for (x-1)/2.
+ */
+ xm1d2_len_u32 = (xm1d2_len + 3) >> 2;
+ t += xm1d2_len_u32;
+ tlen -= xm1d2_len_u32;
+
+ xlen = (x[0] + 31) >> 5;
+ asize = x[0] - 1 - EQ0(x[0] & 31);
+ x0i = br_i31_ninv31(x[1]);
+ while (n -- > 0) {
+ uint32_t *a, *t2;
+ uint32_t eq1, eqm1;
+ size_t t2len;
+
+ /*
+ * Generate a random base. We don't need the base to be
+ * really uniform modulo x, so we just get a random
+ * number which is one bit shorter than x.
+ */
+ a = t;
+ a[0] = x[0];
+ a[xlen] = 0;
+ mkrand(rng, a, asize);
+
+ /*
+ * Compute a^((x-1)/2) mod x. We assume here that the
+ * function will not fail (the temporary array is large
+ * enough).
+ */
+ t2 = t + 1 + xlen;
+ t2len = tlen - 1 - xlen;
+ if ((t2len & 1) != 0) {
+ /*
+ * Since the source array is 64-bit aligned and
+ * has an even number of elements (TEMPS), we
+ * can use the parity of the remaining length to
+ * detect and adjust alignment.
+ */
+ t2 ++;
+ t2len --;
+ }
+ mp31(a, xm1d2, xm1d2_len, x, x0i, t2, t2len);
+
+ /*
+ * We must obtain either 1 or x-1. Note that x is odd,
+ * hence x-1 differs from x only in its low word (no
+ * carry).
+ */
+ eq1 = a[1] ^ 1;
+ eqm1 = a[1] ^ (x[1] - 1);
+ for (u = 2; u <= xlen; u ++) {
+ eq1 |= a[u];
+ eqm1 |= a[u] ^ x[u];
+ }
+
+ if ((EQ0(eq1) | EQ0(eqm1)) == 0) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/*
+ * Create a random prime of the provided size. 'size' is the _encoded_
+ * bit length. The two top bits and the two bottom bits are set to 1.
+ */
+static void
+mkprime(const br_prng_class **rng, uint32_t *x, uint32_t esize,
+ uint32_t pubexp, uint32_t *t, size_t tlen, br_i31_modpow_opt_type mp31)
+{
+ size_t len;
+
+ x[0] = esize;
+ len = (esize + 31) >> 5;
+ for (;;) {
+ size_t u;
+ uint32_t m3, m5, m7, m11;
+ int rounds, s7, s11;
+
+ /*
+ * Generate random bits. We force the two top bits and the
+ * two bottom bits to 1.
+ */
+ mkrand(rng, x, esize);
+ if ((esize & 31) == 0) {
+ x[len] |= 0x60000000;
+ } else if ((esize & 31) == 1) {
+ x[len] |= 0x00000001;
+ x[len - 1] |= 0x40000000;
+ } else {
+ x[len] |= 0x00000003 << ((esize & 31) - 2);
+ }
+ x[1] |= 0x00000003;
+
+ /*
+ * Trial division with low primes (3, 5, 7 and 11). We
+ * use the following properties:
+ *
+ * 2^2 = 1 mod 3
+ * 2^4 = 1 mod 5
+ * 2^3 = 1 mod 7
+ * 2^10 = 1 mod 11
+ */
+ m3 = 0;
+ m5 = 0;
+ m7 = 0;
+ m11 = 0;
+ s7 = 0;
+ s11 = 0;
+ for (u = 0; u < len; u ++) {
+ uint32_t w, w3, w5, w7, w11;
+
+ w = x[1 + u];
+ w3 = (w & 0xFFFF) + (w >> 16); /* max: 98302 */
+ w5 = (w & 0xFFFF) + (w >> 16); /* max: 98302 */
+ w7 = (w & 0x7FFF) + (w >> 15); /* max: 98302 */
+ w11 = (w & 0xFFFFF) + (w >> 20); /* max: 1050622 */
+
+ m3 += w3 << (u & 1);
+ m3 = (m3 & 0xFF) + (m3 >> 8); /* max: 1025 */
+
+ m5 += w5 << ((4 - u) & 3);
+ m5 = (m5 & 0xFFF) + (m5 >> 12); /* max: 4479 */
+
+ m7 += w7 << s7;
+ m7 = (m7 & 0x1FF) + (m7 >> 9); /* max: 1280 */
+ if (++ s7 == 3) {
+ s7 = 0;
+ }
+
+ m11 += w11 << s11;
+ if (++ s11 == 10) {
+ s11 = 0;
+ }
+ m11 = (m11 & 0x3FF) + (m11 >> 10); /* max: 526847 */
+ }
+
+ m3 = (m3 & 0x3F) + (m3 >> 6); /* max: 78 */
+ m3 = (m3 & 0x0F) + (m3 >> 4); /* max: 18 */
+ m3 = ((m3 * 43) >> 5) & 3;
+
+ m5 = (m5 & 0xFF) + (m5 >> 8); /* max: 271 */
+ m5 = (m5 & 0x0F) + (m5 >> 4); /* max: 31 */
+ m5 -= 20 & -GT(m5, 19);
+ m5 -= 10 & -GT(m5, 9);
+ m5 -= 5 & -GT(m5, 4);
+
+ m7 = (m7 & 0x3F) + (m7 >> 6); /* max: 82 */
+ m7 = (m7 & 0x07) + (m7 >> 3); /* max: 16 */
+ m7 = ((m7 * 147) >> 7) & 7;
+
+ /*
+ * 2^5 = 32 = -1 mod 11.
+ */
+ m11 = (m11 & 0x3FF) + (m11 >> 10); /* max: 1536 */
+ m11 = (m11 & 0x3FF) + (m11 >> 10); /* max: 1023 */
+ m11 = (m11 & 0x1F) + 33 - (m11 >> 5); /* max: 64 */
+ m11 -= 44 & -GT(m11, 43);
+ m11 -= 22 & -GT(m11, 21);
+ m11 -= 11 & -GT(m11, 10);
+
+ /*
+ * If any of these modulo is 0, then the candidate is
+ * not prime. Also, if pubexp is 3, 5, 7 or 11, and the
+ * corresponding modulus is 1, then the candidate must
+ * be rejected, because we need e to be invertible
+ * modulo p-1. We can use simple comparisons here
+ * because they won't leak information on a candidate
+ * that we keep, only on one that we reject (and is thus
+ * not secret).
+ */
+ if (m3 == 0 || m5 == 0 || m7 == 0 || m11 == 0) {
+ continue;
+ }
+ if ((pubexp == 3 && m3 == 1)
+ || (pubexp == 5 && m5 == 5)
+ || (pubexp == 7 && m5 == 7)
+ || (pubexp == 11 && m5 == 11))
+ {
+ continue;
+ }
+
+ /*
+ * More trial divisions.
+ */
+ if (!trial_divisions(x, t)) {
+ continue;
+ }
+
+ /*
+ * Miller-Rabin algorithm. Since we selected a random
+ * integer, not a maliciously crafted integer, we can use
+ * relatively few rounds to lower the risk of a false
+ * positive (i.e. declaring prime a non-prime) under
+ * 2^(-80). It is not useful to lower the probability much
+ * below that, since that would be substantially below
+ * the probability of the hardware misbehaving. Sufficient
+ * numbers of rounds are extracted from the Handbook of
+ * Applied Cryptography, note 4.49 (page 149).
+ *
+ * Since we work on the encoded size (esize), we need to
+ * compare with encoded thresholds.
+ */
+ if (esize < 309) {
+ rounds = 12;
+ } else if (esize < 464) {
+ rounds = 9;
+ } else if (esize < 670) {
+ rounds = 6;
+ } else if (esize < 877) {
+ rounds = 4;
+ } else if (esize < 1341) {
+ rounds = 3;
+ } else {
+ rounds = 2;
+ }
+
+ if (miller_rabin(rng, x, rounds, t, tlen, mp31)) {
+ return;
+ }
+ }
+}
+
+/*
+ * Let p be a prime (p > 2^33, p = 3 mod 4). Let m = (p-1)/2, provided
+ * as parameter (with announced bit length equal to that of p). This
+ * function computes d = 1/e mod p-1 (for an odd integer e). Returned
+ * value is 1 on success, 0 on error (an error is reported if e is not
+ * invertible modulo p-1).
+ *
+ * The temporary buffer (t) must have room for at least 4 integers of
+ * the size of p.
+ */
+static uint32_t
+invert_pubexp(uint32_t *d, const uint32_t *m, uint32_t e, uint32_t *t)
+{
+ uint32_t *f;
+ uint32_t r;
+
+ f = t;
+ t += 1 + ((m[0] + 31) >> 5);
+
+ /*
+ * Compute d = 1/e mod m. Since p = 3 mod 4, m is odd.
+ */
+ br_i31_zero(d, m[0]);
+ d[1] = 1;
+ br_i31_zero(f, m[0]);
+ f[1] = e & 0x7FFFFFFF;
+ f[2] = e >> 31;
+ r = br_i31_moddiv(d, f, m, br_i31_ninv31(m[1]), t);
+
+ /*
+ * We really want d = 1/e mod p-1, with p = 2m. By the CRT,
+ * the result is either the d we got, or d + m.
+ *
+ * Let's write e*d = 1 + k*m, for some integer k. Integers e
+ * and m are odd. If d is odd, then e*d is odd, which implies
+ * that k must be even; in that case, e*d = 1 + (k/2)*2m, and
+ * thus d is already fine. Conversely, if d is even, then k
+ * is odd, and we must add m to d in order to get the correct
+ * result.
+ */
+ br_i31_add(d, m, (uint32_t)(1 - (d[1] & 1)));
+
+ return r;
+}
+
+/*
+ * Swap two buffers in RAM. They must be disjoint.
+ */
+static void
+bufswap(void *b1, void *b2, size_t len)
+{
+ size_t u;
+ unsigned char *buf1, *buf2;
+
+ buf1 = b1;
+ buf2 = b2;
+ for (u = 0; u < len; u ++) {
+ unsigned w;
+
+ w = buf1[u];
+ buf1[u] = buf2[u];
+ buf2[u] = w;
+ }
+}
+
+/* see inner.h */
+uint32_t
+br_rsa_i31_keygen_inner(const br_prng_class **rng,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp, br_i31_modpow_opt_type mp31)
+{
+ uint32_t esize_p, esize_q;
+ size_t plen, qlen, tlen;
+ uint32_t *p, *q, *t;
+ union {
+ uint32_t t32[TEMPS];
+ uint64_t t64[TEMPS >> 1]; /* for 64-bit alignment */
+ } tmp;
+ uint32_t r;
+
+ if (size < BR_MIN_RSA_SIZE || size > BR_MAX_RSA_SIZE) {
+ return 0;
+ }
+ if (pubexp == 0) {
+ pubexp = 3;
+ } else if (pubexp == 1 || (pubexp & 1) == 0) {
+ return 0;
+ }
+
+ esize_p = (size + 1) >> 1;
+ esize_q = size - esize_p;
+ sk->n_bitlen = size;
+ sk->p = kbuf_priv;
+ sk->plen = (esize_p + 7) >> 3;
+ sk->q = sk->p + sk->plen;
+ sk->qlen = (esize_q + 7) >> 3;
+ sk->dp = sk->q + sk->qlen;
+ sk->dplen = sk->plen;
+ sk->dq = sk->dp + sk->dplen;
+ sk->dqlen = sk->qlen;
+ sk->iq = sk->dq + sk->dqlen;
+ sk->iqlen = sk->plen;
+
+ if (pk != NULL) {
+ pk->n = kbuf_pub;
+ pk->nlen = (size + 7) >> 3;
+ pk->e = pk->n + pk->nlen;
+ pk->elen = 4;
+ br_enc32be(pk->e, pubexp);
+ while (*pk->e == 0) {
+ pk->e ++;
+ pk->elen --;
+ }
+ }
+
+ /*
+ * We now switch to encoded sizes.
+ *
+ * floor((x * 16913) / (2^19)) is equal to floor(x/31) for all
+ * integers x from 0 to 34966; the intermediate product fits on
+ * 30 bits, thus we can use MUL31().
+ */
+ esize_p += MUL31(esize_p, 16913) >> 19;
+ esize_q += MUL31(esize_q, 16913) >> 19;
+ plen = (esize_p + 31) >> 5;
+ qlen = (esize_q + 31) >> 5;
+ p = tmp.t32;
+ q = p + 1 + plen;
+ t = q + 1 + qlen;
+ tlen = ((sizeof tmp.t32) / sizeof(uint32_t)) - (2 + plen + qlen);
+
+ /*
+ * When looking for primes p and q, we temporarily divide
+ * candidates by 2, in order to compute the inverse of the
+ * public exponent.
+ */
+
+ for (;;) {
+ mkprime(rng, p, esize_p, pubexp, t, tlen, mp31);
+ br_i31_rshift(p, 1);
+ if (invert_pubexp(t, p, pubexp, t + 1 + plen)) {
+ br_i31_add(p, p, 1);
+ p[1] |= 1;
+ br_i31_encode(sk->p, sk->plen, p);
+ br_i31_encode(sk->dp, sk->dplen, t);
+ break;
+ }
+ }
+
+ for (;;) {
+ mkprime(rng, q, esize_q, pubexp, t, tlen, mp31);
+ br_i31_rshift(q, 1);
+ if (invert_pubexp(t, q, pubexp, t + 1 + qlen)) {
+ br_i31_add(q, q, 1);
+ q[1] |= 1;
+ br_i31_encode(sk->q, sk->qlen, q);
+ br_i31_encode(sk->dq, sk->dqlen, t);
+ break;
+ }
+ }
+
+ /*
+ * If p and q have the same size, then it is possible that q > p
+ * (when the target modulus size is odd, we generate p with a
+ * greater bit length than q). If q > p, we want to swap p and q
+ * (and also dp and dq) for two reasons:
+ * - The final step below (inversion of q modulo p) is easier if
+ * p > q.
+ * - While BearSSL's RSA code is perfectly happy with RSA keys such
+ * that p < q, some other implementations have restrictions and
+ * require p > q.
+ *
+ * Note that we can do a simple non-constant-time swap here,
+ * because the only information we leak here is that we insist on
+ * returning p and q such that p > q, which is not a secret.
+ */
+ if (esize_p == esize_q && br_i31_sub(p, q, 0) == 1) {
+ bufswap(p, q, (1 + plen) * sizeof *p);
+ bufswap(sk->p, sk->q, sk->plen);
+ bufswap(sk->dp, sk->dq, sk->dplen);
+ }
+
+ /*
+ * We have produced p, q, dp and dq. We can now compute iq = 1/d mod p.
+ *
+ * We ensured that p >= q, so this is just a matter of updating the
+ * header word for q (and possibly adding an extra word).
+ *
+ * Theoretically, the call below may fail, in case we were
+ * extraordinarily unlucky, and p = q. Another failure case is if
+ * Miller-Rabin failed us _twice_, and p and q are non-prime and
+ * have a factor is common. We report the error mostly because it
+ * is cheap and we can, but in practice this never happens (or, at
+ * least, it happens way less often than hardware glitches).
+ */
+ q[0] = p[0];
+ if (plen > qlen) {
+ q[plen] = 0;
+ t ++;
+ tlen --;
+ }
+ br_i31_zero(t, p[0]);
+ t[1] = 1;
+ r = br_i31_moddiv(t, q, p, br_i31_ninv31(p[1]), t + 1 + plen);
+ br_i31_encode(sk->iq, sk->iqlen, t);
+
+ /*
+ * Compute the public modulus too, if required.
+ */
+ if (pk != NULL) {
+ br_i31_zero(t, p[0]);
+ br_i31_mulacc(t, p, q);
+ br_i31_encode(pk->n, pk->nlen, t);
+ }
+
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_modulus.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_modulus.c
new file mode 100644
index 00000000..f5f997f5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_modulus.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i31_compute_modulus(void *n, const br_rsa_private_key *sk)
+{
+ uint32_t tmp[4 * (((BR_MAX_RSA_SIZE / 2) + 30) / 31) + 5];
+ uint32_t *t, *p, *q;
+ const unsigned char *pbuf, *qbuf;
+ size_t nlen, plen, qlen, tlen;
+
+ /*
+ * Compute actual byte and lengths for p and q.
+ */
+ pbuf = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ qbuf = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *qbuf == 0) {
+ qbuf ++;
+ qlen --;
+ }
+
+ t = tmp;
+ tlen = (sizeof tmp) / (sizeof tmp[0]);
+
+ /*
+ * Decode p.
+ */
+ if ((31 * tlen) < (plen << 3) + 31) {
+ return 0;
+ }
+ br_i31_decode(t, pbuf, plen);
+ p = t;
+ plen = (p[0] + 63) >> 5;
+ t += plen;
+ tlen -= plen;
+
+ /*
+ * Decode q.
+ */
+ if ((31 * tlen) < (qlen << 3) + 31) {
+ return 0;
+ }
+ br_i31_decode(t, qbuf, qlen);
+ q = t;
+ qlen = (q[0] + 63) >> 5;
+ t += qlen;
+ tlen -= qlen;
+
+ /*
+ * Computation can proceed only if we have enough room for the
+ * modulus.
+ */
+ if (tlen < (plen + qlen + 1)) {
+ return 0;
+ }
+
+ /*
+ * Private key already contains the modulus bit length, from which
+ * we can infer the output length. Even if n is NULL, we still had
+ * to decode p and q to make sure that the product can be computed.
+ */
+ nlen = (sk->n_bitlen + 7) >> 3;
+ if (n != NULL) {
+ br_i31_zero(t, p[0]);
+ br_i31_mulacc(t, p, q);
+ br_i31_encode(n, nlen, t);
+ }
+ return nlen;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_decrypt.c
new file mode 100644
index 00000000..06fdd93c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_decrypt.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_oaep_decrypt(const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len)
+{
+ uint32_t r;
+
+ if (*len != ((sk->n_bitlen + 7) >> 3)) {
+ return 0;
+ }
+ r = br_rsa_i31_private(data, sk);
+ r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_encrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_encrypt.c
new file mode 100644
index 00000000..367008ce
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_oaep_encrypt.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i31_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len)
+{
+ size_t dlen;
+
+ dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
+ pk, dst, dst_max_len, src, src_len);
+ if (dlen == 0) {
+ return 0;
+ }
+ return dlen & -(size_t)br_rsa_i31_public(dst, dlen, pk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_sign.c
new file mode 100644
index 00000000..784d3c20
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_sign.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) {
+ return 0;
+ }
+ return br_rsa_i31_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_vrfy.c
new file mode 100644
index 00000000..e79a002d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pkcs1_vrfy.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i31_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_priv.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_priv.c
new file mode 100644
index 00000000..b1e1244e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_priv.c
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define U (2 + ((BR_MAX_RSA_FACTOR + 30) / 31))
+#define TLEN (8 * U)
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk)
+{
+ const unsigned char *p, *q;
+ size_t plen, qlen;
+ size_t fwlen;
+ uint32_t p0i, q0i;
+ size_t xlen, u;
+ uint32_t tmp[1 + TLEN];
+ long z;
+ uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
+ uint32_t r;
+
+ /*
+ * Compute the actual lengths of p and q, in bytes.
+ * These lengths are not considered secret (we cannot really hide
+ * them anyway in constant-time code).
+ */
+ p = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *p == 0) {
+ p ++;
+ plen --;
+ }
+ q = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *q == 0) {
+ q ++;
+ qlen --;
+ }
+
+ /*
+ * Compute the maximum factor length, in words.
+ */
+ z = (long)(plen > qlen ? plen : qlen) << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 31;
+ fwlen ++;
+ }
+
+ /*
+ * Round up the word length to an even number.
+ */
+ fwlen += (fwlen & 1);
+
+ /*
+ * We need to fit at least 6 values in the stack buffer.
+ */
+ if (6 * fwlen > TLEN) {
+ return 0;
+ }
+
+ /*
+ * Compute modulus length (in bytes).
+ */
+ xlen = (sk->n_bitlen + 7) >> 3;
+
+ /*
+ * Decode q.
+ */
+ mq = tmp;
+ br_i31_decode(mq, q, qlen);
+
+ /*
+ * Decode p.
+ */
+ t1 = mq + fwlen;
+ br_i31_decode(t1, p, plen);
+
+ /*
+ * Compute the modulus (product of the two factors), to compare
+ * it with the source value. We use br_i31_mulacc(), since it's
+ * already used later on.
+ */
+ t2 = mq + 2 * fwlen;
+ br_i31_zero(t2, mq[0]);
+ br_i31_mulacc(t2, mq, t1);
+
+ /*
+ * We encode the modulus into bytes, to perform the comparison
+ * with bytes. We know that the product length, in bytes, is
+ * exactly xlen.
+ * The comparison actually computes the carry when subtracting
+ * the modulus from the source value; that carry must be 1 for
+ * a value in the correct range. We keep it in r, which is our
+ * accumulator for the error code.
+ */
+ t3 = mq + 4 * fwlen;
+ br_i31_encode(t3, xlen, t2);
+ u = xlen;
+ r = 0;
+ while (u > 0) {
+ uint32_t wn, wx;
+
+ u --;
+ wn = ((unsigned char *)t3)[u];
+ wx = x[u];
+ r = ((wx - (wn + r)) >> 8) & 1;
+ }
+
+ /*
+ * Move the decoded p to another temporary buffer.
+ */
+ mp = mq + 2 * fwlen;
+ memmove(mp, t1, fwlen * sizeof *t1);
+
+ /*
+ * Compute s2 = x^dq mod q.
+ */
+ q0i = br_i31_ninv31(mq[1]);
+ s2 = mq + fwlen;
+ br_i31_decode_reduce(s2, x, xlen, mq);
+ r &= br_i31_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
+ mq + 3 * fwlen, TLEN - 3 * fwlen);
+
+ /*
+ * Compute s1 = x^dp mod p.
+ */
+ p0i = br_i31_ninv31(mp[1]);
+ s1 = mq + 3 * fwlen;
+ br_i31_decode_reduce(s1, x, xlen, mp);
+ r &= br_i31_modpow_opt(s1, sk->dp, sk->dplen, mp, p0i,
+ mq + 4 * fwlen, TLEN - 4 * fwlen);
+
+ /*
+ * Compute:
+ * h = (s1 - s2)*(1/q) mod p
+ * s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is
+ * unclear about whether p may be lower than q (some existing,
+ * widely deployed implementations of RSA don't tolerate p < q),
+ * but we want to support that occurrence, so we need to use the
+ * reduction function.
+ *
+ * Since we use br_i31_decode_reduce() for iq (purportedly, the
+ * inverse of q modulo p), we also tolerate improperly large
+ * values for this parameter.
+ */
+ t1 = mq + 4 * fwlen;
+ t2 = mq + 5 * fwlen;
+ br_i31_reduce(t2, s2, mp);
+ br_i31_add(s1, mp, br_i31_sub(s1, t2, 1));
+ br_i31_to_monty(s1, mp);
+ br_i31_decode_reduce(t1, sk->iq, sk->iqlen, mp);
+ br_i31_montymul(t2, s1, t1, mp, p0i);
+
+ /*
+ * h is now in t2. We compute the final result:
+ * s = s2 + q*h
+ * All these operations are non-modular.
+ *
+ * We need mq, s2 and t2. We use the t3 buffer as destination.
+ * The buffers mp, s1 and t1 are no longer needed, so we can
+ * reuse them for t3. Moreover, the first step of the computation
+ * is to copy s2 into t3, after which s2 is not needed. Right
+ * now, mq is in slot 0, s2 is in slot 1, and t2 is in slot 5.
+ * Therefore, we have ample room for t3 by simply using s2.
+ */
+ t3 = s2;
+ br_i31_mulacc(t3, mq, t2);
+
+ /*
+ * Encode the result. Since we already checked the value of xlen,
+ * we can just use it right away.
+ */
+ br_i31_encode(x, xlen, t3);
+
+ /*
+ * The only error conditions remaining at that point are invalid
+ * values for p and q (even integers).
+ */
+ return p0i & q0i & r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_privexp.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_privexp.c
new file mode 100644
index 00000000..eee62a09
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_privexp.c
@@ -0,0 +1,318 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i31_compute_privexp(void *d,
+ const br_rsa_private_key *sk, uint32_t e)
+{
+ /*
+ * We want to invert e modulo phi = (p-1)(q-1). This first
+ * requires computing phi, which is easy since we have the factors
+ * p and q in the private key structure.
+ *
+ * Since p = 3 mod 4 and q = 3 mod 4, phi/4 is an odd integer.
+ * We could invert e modulo phi/4 then patch the result to
+ * modulo phi, but this would involve assembling three modulus-wide
+ * values (phi/4, 1 and e) and calling moddiv, that requires
+ * three more temporaries, for a total of six big integers, or
+ * slightly more than 3 kB of stack space for RSA-4096. This
+ * exceeds our stack requirements.
+ *
+ * Instead, we first use one step of the extended GCD:
+ *
+ * - We compute phi = k*e + r (Euclidean division of phi by e).
+ * If public exponent e is correct, then r != 0 (e must be
+ * invertible modulo phi). We also have k != 0 since we
+ * enforce non-ridiculously-small factors.
+ *
+ * - We find small u, v such that u*e - v*r = 1 (using a
+ * binary GCD; we can arrange for u < r and v < e, i.e. all
+ * values fit on 32 bits).
+ *
+ * - Solution is: d = u + v*k
+ * This last computation is exact: since u < r and v < e,
+ * the above implies d < r + e*((phi-r)/e) = phi
+ */
+
+ uint32_t tmp[4 * ((BR_MAX_RSA_FACTOR + 30) / 31) + 12];
+ uint32_t *p, *q, *k, *m, *z, *phi;
+ const unsigned char *pbuf, *qbuf;
+ size_t plen, qlen, u, len, dlen;
+ uint32_t r, a, b, u0, v0, u1, v1, he, hr;
+ int i;
+
+ /*
+ * Check that e is correct.
+ */
+ if (e < 3 || (e & 1) == 0) {
+ return 0;
+ }
+
+ /*
+ * Check lengths of p and q, and that they are both odd.
+ */
+ pbuf = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ if (plen < 5 || plen > (BR_MAX_RSA_FACTOR / 8)
+ || (pbuf[plen - 1] & 1) != 1)
+ {
+ return 0;
+ }
+ qbuf = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *qbuf == 0) {
+ qbuf ++;
+ qlen --;
+ }
+ if (qlen < 5 || qlen > (BR_MAX_RSA_FACTOR / 8)
+ || (qbuf[qlen - 1] & 1) != 1)
+ {
+ return 0;
+ }
+
+ /*
+ * Output length is that of the modulus.
+ */
+ dlen = (sk->n_bitlen + 7) >> 3;
+ if (d == NULL) {
+ return dlen;
+ }
+
+ p = tmp;
+ br_i31_decode(p, pbuf, plen);
+ plen = (p[0] + 31) >> 5;
+ q = p + 1 + plen;
+ br_i31_decode(q, qbuf, qlen);
+ qlen = (q[0] + 31) >> 5;
+
+ /*
+ * Compute phi = (p-1)*(q-1), then move it over p-1 and q-1 (that
+ * we do not need anymore). The mulacc function sets the announced
+ * bit length of t to be the sum of the announced bit lengths of
+ * p-1 and q-1, which is usually exact but may overshoot by one 1
+ * bit in some cases; we readjust it to its true length.
+ */
+ p[1] --;
+ q[1] --;
+ phi = q + 1 + qlen;
+ br_i31_zero(phi, p[0]);
+ br_i31_mulacc(phi, p, q);
+ len = (phi[0] + 31) >> 5;
+ memmove(tmp, phi, (1 + len) * sizeof *phi);
+ phi = tmp;
+ phi[0] = br_i31_bit_length(phi + 1, len);
+ len = (phi[0] + 31) >> 5;
+
+ /*
+ * Divide phi by public exponent e. The final remainder r must be
+ * non-zero (otherwise, the key is invalid). The quotient is k,
+ * which we write over phi, since we don't need phi after that.
+ */
+ r = 0;
+ for (u = len; u >= 1; u --) {
+ /*
+ * Upon entry, r < e, and phi[u] < 2^31; hence,
+ * hi:lo < e*2^31. Thus, the produced word k[u]
+ * must be lower than 2^31, and the new remainder r
+ * is lower than e.
+ */
+ uint32_t hi, lo;
+
+ hi = r >> 1;
+ lo = (r << 31) + phi[u];
+ phi[u] = br_divrem(hi, lo, e, &r);
+ }
+ if (r == 0) {
+ return 0;
+ }
+ k = phi;
+
+ /*
+ * Compute u and v such that u*e - v*r = GCD(e,r). We use
+ * a binary GCD algorithm, with 6 extra integers a, b,
+ * u0, u1, v0 and v1. Initial values are:
+ * a = e u0 = 1 v0 = 0
+ * b = r u1 = r v1 = e-1
+ * The following invariants are maintained:
+ * a = u0*e - v0*r
+ * b = u1*e - v1*r
+ * 0 < a <= e
+ * 0 < b <= r
+ * 0 <= u0 <= r
+ * 0 <= v0 <= e
+ * 0 <= u1 <= r
+ * 0 <= v1 <= e
+ *
+ * At each iteration, we reduce either a or b by one bit, and
+ * adjust u0, u1, v0 and v1 to maintain the invariants:
+ * - if a is even, then a <- a/2
+ * - otherwise, if b is even, then b <- b/2
+ * - otherwise, if a > b, then a <- (a-b)/2
+ * - otherwise, if b > a, then b <- (b-a)/2
+ * Algorithm stops when a = b. At that point, the common value
+ * is the GCD of e and r; it must be 1 (otherwise, the private
+ * key or public exponent is not valid). The (u0,v0) or (u1,v1)
+ * pairs are the solution we are looking for.
+ *
+ * Since either a or b is reduced by at least 1 bit at each
+ * iteration, 62 iterations are enough to reach the end
+ * condition.
+ *
+ * To maintain the invariants, we must compute the same operations
+ * on the u* and v* values that we do on a and b:
+ * - When a is divided by 2, u0 and v0 must be divided by 2.
+ * - When b is divided by 2, u1 and v1 must be divided by 2.
+ * - When b is subtracted from a, u1 and v1 are subtracted from
+ * u0 and v0, respectively.
+ * - When a is subtracted from b, u0 and v0 are subtracted from
+ * u1 and v1, respectively.
+ *
+ * However, we want to keep the u* and v* values in their proper
+ * ranges. The following remarks apply:
+ *
+ * - When a is divided by 2, then a is even. Therefore:
+ *
+ * * If r is odd, then u0 and v0 must have the same parity;
+ * if they are both odd, then adding r to u0 and e to v0
+ * makes them both even, and the division by 2 brings them
+ * back to the proper range.
+ *
+ * * If r is even, then u0 must be even; if v0 is odd, then
+ * adding r to u0 and e to v0 makes them both even, and the
+ * division by 2 brings them back to the proper range.
+ *
+ * Thus, all we need to do is to look at the parity of v0,
+ * and add (r,e) to (u0,v0) when v0 is odd. In order to avoid
+ * a 32-bit overflow, we can add ((r+1)/2,(e/2)+1) after the
+ * division (r+1 does not overflow since r < e; and (e/2)+1
+ * is equal to (e+1)/2 since e is odd).
+ *
+ * - When we subtract b from a, three cases may occur:
+ *
+ * * u1 <= u0 and v1 <= v0: just do the subtractions
+ *
+ * * u1 > u0 and v1 > v0: compute:
+ * (u0, v0) <- (u0 + r - u1, v0 + e - v1)
+ *
+ * * u1 <= u0 and v1 > v0: compute:
+ * (u0, v0) <- (u0 + r - u1, v0 + e - v1)
+ *
+ * The fourth case (u1 > u0 and v1 <= v0) is not possible
+ * because it would contradict "b < a" (which is the reason
+ * why we subtract b from a).
+ *
+ * The tricky case is the third one: from the equations, it
+ * seems that u0 may go out of range. However, the invariants
+ * and ranges of other values imply that, in that case, the
+ * new u0 does not actually exceed the range.
+ *
+ * We can thus handle the subtraction by adding (r,e) based
+ * solely on the comparison between v0 and v1.
+ */
+ a = e;
+ b = r;
+ u0 = 1;
+ v0 = 0;
+ u1 = r;
+ v1 = e - 1;
+ hr = (r + 1) >> 1;
+ he = (e >> 1) + 1;
+ for (i = 0; i < 62; i ++) {
+ uint32_t oa, ob, agtb, bgta;
+ uint32_t sab, sba, da, db;
+ uint32_t ctl;
+
+ oa = a & 1; /* 1 if a is odd */
+ ob = b & 1; /* 1 if b is odd */
+ agtb = GT(a, b); /* 1 if a > b */
+ bgta = GT(b, a); /* 1 if b > a */
+
+ sab = oa & ob & agtb; /* 1 if a <- a-b */
+ sba = oa & ob & bgta; /* 1 if b <- b-a */
+
+ /* a <- a-b, u0 <- u0-u1, v0 <- v0-v1 */
+ ctl = GT(v1, v0);
+ a -= b & -sab;
+ u0 -= (u1 - (r & -ctl)) & -sab;
+ v0 -= (v1 - (e & -ctl)) & -sab;
+
+ /* b <- b-a, u1 <- u1-u0 mod r, v1 <- v1-v0 mod e */
+ ctl = GT(v0, v1);
+ b -= a & -sba;
+ u1 -= (u0 - (r & -ctl)) & -sba;
+ v1 -= (v0 - (e & -ctl)) & -sba;
+
+ da = NOT(oa) | sab; /* 1 if a <- a/2 */
+ db = (oa & NOT(ob)) | sba; /* 1 if b <- b/2 */
+
+ /* a <- a/2, u0 <- u0/2, v0 <- v0/2 */
+ ctl = v0 & 1;
+ a ^= (a ^ (a >> 1)) & -da;
+ u0 ^= (u0 ^ ((u0 >> 1) + (hr & -ctl))) & -da;
+ v0 ^= (v0 ^ ((v0 >> 1) + (he & -ctl))) & -da;
+
+ /* b <- b/2, u1 <- u1/2 mod r, v1 <- v1/2 mod e */
+ ctl = v1 & 1;
+ b ^= (b ^ (b >> 1)) & -db;
+ u1 ^= (u1 ^ ((u1 >> 1) + (hr & -ctl))) & -db;
+ v1 ^= (v1 ^ ((v1 >> 1) + (he & -ctl))) & -db;
+ }
+
+ /*
+ * Check that the GCD is indeed 1. If not, then the key is invalid
+ * (and there's no harm in leaking that piece of information).
+ */
+ if (a != 1) {
+ return 0;
+ }
+
+ /*
+ * Now we have u0*e - v0*r = 1. Let's compute the result as:
+ * d = u0 + v0*k
+ * We still have k in the tmp[] array, and its announced bit
+ * length is that of phi.
+ */
+ m = k + 1 + len;
+ m[0] = (1 << 5) + 1; /* bit length is 32 bits, encoded */
+ m[1] = v0 & 0x7FFFFFFF;
+ m[2] = v0 >> 31;
+ z = m + 3;
+ br_i31_zero(z, k[0]);
+ z[1] = u0 & 0x7FFFFFFF;
+ z[2] = u0 >> 31;
+ br_i31_mulacc(z, k, m);
+
+ /*
+ * Encode the result.
+ */
+ br_i31_encode(d, dlen, z);
+ return dlen;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_sign.c
new file mode 100644
index 00000000..b06f3e21
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_sign.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pss_sig_pad(rng, hf_data, hf_mgf1, hash,
+ salt_len, sk->n_bitlen, x))
+ {
+ return 0;
+ }
+ return br_rsa_i31_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_vrfy.c
new file mode 100644
index 00000000..77a9b28f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pss_vrfy.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i31_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pss_sig_unpad(hf_data, hf_mgf1,
+ hash, salt_len, pk, sig);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pub.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pub.c
new file mode 100644
index 00000000..d5f3fe2c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pub.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * As a strict minimum, we need four buffers that can hold a
+ * modular integer.
+ */
+#define TLEN (4 * (2 + ((BR_MAX_RSA_SIZE + 30) / 31)))
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk)
+{
+ const unsigned char *n;
+ size_t nlen;
+ uint32_t tmp[1 + TLEN];
+ uint32_t *m, *a, *t;
+ size_t fwlen;
+ long z;
+ uint32_t m0i, r;
+
+ /*
+ * Get the actual length of the modulus, and see if it fits within
+ * our stack buffer. We also check that the length of x[] is valid.
+ */
+ n = pk->n;
+ nlen = pk->nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+ if (nlen == 0 || nlen > (BR_MAX_RSA_SIZE >> 3) || xlen != nlen) {
+ return 0;
+ }
+ z = (long)nlen << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 31;
+ fwlen ++;
+ }
+ /*
+ * Round up length to an even number.
+ */
+ fwlen += (fwlen & 1);
+
+ /*
+ * The modulus gets decoded into m[].
+ * The value to exponentiate goes into a[].
+ * The temporaries for modular exponentiation are in t[].
+ */
+ m = tmp;
+ a = m + fwlen;
+ t = m + 2 * fwlen;
+
+ /*
+ * Decode the modulus.
+ */
+ br_i31_decode(m, n, nlen);
+ m0i = br_i31_ninv31(m[1]);
+
+ /*
+ * Note: if m[] is even, then m0i == 0. Otherwise, m0i must be
+ * an odd integer.
+ */
+ r = m0i & 1;
+
+ /*
+ * Decode x[] into a[]; we also check that its value is proper.
+ */
+ r &= br_i31_decode_mod(a, x, xlen, m);
+
+ /*
+ * Compute the modular exponentiation.
+ */
+ br_i31_modpow_opt(a, pk->e, pk->elen, m, m0i, t, TLEN - 2 * fwlen);
+
+ /*
+ * Encode the result.
+ */
+ br_i31_encode(x, xlen, a);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i31_pubexp.c b/test/monniaux/BearSSL/src/rsa/rsa_i31_pubexp.c
new file mode 100644
index 00000000..f26537d8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i31_pubexp.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Recompute public exponent, based on factor p and reduced private
+ * exponent dp.
+ */
+static uint32_t
+get_pubexp(const unsigned char *pbuf, size_t plen,
+ const unsigned char *dpbuf, size_t dplen)
+{
+ /*
+ * dp is the inverse of e modulo p-1. If p = 3 mod 4, then
+ * p-1 = 2*((p-1)/2). Taken modulo 2, e is odd and has inverse 1;
+ * thus, dp must be odd.
+ *
+ * We compute the inverse of dp modulo (p-1)/2. This requires
+ * first reducing dp modulo (p-1)/2 (this can be done with a
+ * conditional subtract, no need to use the generic modular
+ * reduction function); then, we use moddiv.
+ */
+
+ uint32_t tmp[6 * ((BR_MAX_RSA_FACTOR + 61) / 31)];
+ uint32_t *p, *dp, *x;
+ size_t len;
+ uint32_t e;
+
+ /*
+ * Compute actual factor length (in bytes) and check that it fits
+ * under our size constraints.
+ */
+ while (plen > 0 && *pbuf == 0) {
+ pbuf ++;
+ plen --;
+ }
+ if (plen == 0 || plen < 5 || plen > (BR_MAX_RSA_FACTOR / 8)) {
+ return 0;
+ }
+
+ /*
+ * Compute actual reduced exponent length (in bytes) and check that
+ * it is not longer than p.
+ */
+ while (dplen > 0 && *dpbuf == 0) {
+ dpbuf ++;
+ dplen --;
+ }
+ if (dplen > plen || dplen == 0
+ || (dplen == plen && dpbuf[0] > pbuf[0]))
+ {
+ return 0;
+ }
+
+ /*
+ * Verify that p = 3 mod 4 and that dp is odd.
+ */
+ if ((pbuf[plen - 1] & 3) != 3 || (dpbuf[dplen - 1] & 1) != 1) {
+ return 0;
+ }
+
+ /*
+ * Decode p and compute (p-1)/2.
+ */
+ p = tmp;
+ br_i31_decode(p, pbuf, plen);
+ len = (p[0] + 63) >> 5;
+ br_i31_rshift(p, 1);
+
+ /*
+ * Decode dp and make sure its announced bit length matches that of
+ * p (we already know that the size of dp, in bits, does not exceed
+ * the size of p, so we just have to copy the header word).
+ */
+ dp = p + len;
+ memset(dp, 0, len * sizeof *dp);
+ br_i31_decode(dp, dpbuf, dplen);
+ dp[0] = p[0];
+
+ /*
+ * Subtract (p-1)/2 from dp if necessary.
+ */
+ br_i31_sub(dp, p, NOT(br_i31_sub(dp, p, 0)));
+
+ /*
+ * If another subtraction is needed, then this means that the
+ * value was invalid. We don't care to leak information about
+ * invalid keys.
+ */
+ if (br_i31_sub(dp, p, 0) == 0) {
+ return 0;
+ }
+
+ /*
+ * Invert dp modulo (p-1)/2. If the inversion fails, then the
+ * key value was invalid.
+ */
+ x = dp + len;
+ br_i31_zero(x, p[0]);
+ x[1] = 1;
+ if (br_i31_moddiv(x, dp, p, br_i31_ninv31(p[1]), x + len) == 0) {
+ return 0;
+ }
+
+ /*
+ * We now have an inverse. We must set it to zero (error) if its
+ * length is greater than 32 bits and/or if it is an even integer.
+ * Take care that the bit_length function returns an encoded
+ * bit length.
+ */
+ e = (uint32_t)x[1] | ((uint32_t)x[2] << 31);
+ e &= -LT(br_i31_bit_length(x + 1, len - 1), 34);
+ e &= -(e & 1);
+ return e;
+}
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i31_compute_pubexp(const br_rsa_private_key *sk)
+{
+ /*
+ * Get the public exponent from both p and q. This is the right
+ * exponent if we get twice the same value.
+ */
+ uint32_t ep, eq;
+
+ ep = get_pubexp(sk->p, sk->plen, sk->dp, sk->dplen);
+ eq = get_pubexp(sk->q, sk->qlen, sk->dq, sk->dqlen);
+ return ep & -EQ(ep, eq);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_decrypt.c
new file mode 100644
index 00000000..ecfd92b1
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_decrypt.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_oaep_decrypt(const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len)
+{
+ uint32_t r;
+
+ if (*len != ((sk->n_bitlen + 7) >> 3)) {
+ return 0;
+ }
+ r = br_rsa_i32_private(data, sk);
+ r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_encrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_encrypt.c
new file mode 100644
index 00000000..dc17f3f2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_oaep_encrypt.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i32_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len)
+{
+ size_t dlen;
+
+ dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
+ pk, dst, dst_max_len, src, src_len);
+ if (dlen == 0) {
+ return 0;
+ }
+ return dlen & -(size_t)br_rsa_i32_public(dst, dlen, pk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_sign.c
new file mode 100644
index 00000000..44b6e6d5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_sign.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) {
+ return 0;
+ }
+ return br_rsa_i32_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_vrfy.c
new file mode 100644
index 00000000..6ee7a198
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_pkcs1_vrfy.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i32_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_priv.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_priv.c
new file mode 100644
index 00000000..05c22ec3
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_priv.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define U (1 + (BR_MAX_RSA_FACTOR >> 5))
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_private(unsigned char *x, const br_rsa_private_key *sk)
+{
+ const unsigned char *p, *q;
+ size_t plen, qlen;
+ uint32_t tmp[6 * U];
+ uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
+ uint32_t p0i, q0i;
+ size_t xlen, u;
+ uint32_t r;
+
+ /*
+ * All our temporary buffers are from the tmp[] array.
+ *
+ * The mp, mq, s1, s2, t1 and t2 buffers are large enough to
+ * contain a RSA factor. The t3 buffer can contain a complete
+ * RSA modulus. t3 shares its storage space with s2, s1 and t1,
+ * in that order (this is important, see below).
+ */
+ mq = tmp;
+ mp = tmp + U;
+ t2 = tmp + 2 * U;
+ s2 = tmp + 3 * U;
+ s1 = tmp + 4 * U;
+ t1 = tmp + 5 * U;
+ t3 = s2;
+
+ /*
+ * Compute the actual lengths (in bytes) of p and q, and check
+ * that they fit within our stack buffers.
+ */
+ p = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *p == 0) {
+ p ++;
+ plen --;
+ }
+ q = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *q == 0) {
+ q ++;
+ qlen --;
+ }
+ if (plen > (BR_MAX_RSA_FACTOR >> 3)
+ || qlen > (BR_MAX_RSA_FACTOR >> 3))
+ {
+ return 0;
+ }
+
+ /*
+ * Decode p and q.
+ */
+ br_i32_decode(mp, p, plen);
+ br_i32_decode(mq, q, qlen);
+
+ /*
+ * Recompute modulus, to compare with the source value.
+ */
+ br_i32_zero(t2, mp[0]);
+ br_i32_mulacc(t2, mp, mq);
+ xlen = (sk->n_bitlen + 7) >> 3;
+ br_i32_encode(t2 + 2 * U, xlen, t2);
+ u = xlen;
+ r = 0;
+ while (u > 0) {
+ uint32_t wn, wx;
+
+ u --;
+ wn = ((unsigned char *)(t2 + 2 * U))[u];
+ wx = x[u];
+ r = ((wx - (wn + r)) >> 8) & 1;
+ }
+
+ /*
+ * Compute s1 = x^dp mod p.
+ */
+ p0i = br_i32_ninv32(mp[1]);
+ br_i32_decode_reduce(s1, x, xlen, mp);
+ br_i32_modpow(s1, sk->dp, sk->dplen, mp, p0i, t1, t2);
+
+ /*
+ * Compute s2 = x^dq mod q.
+ */
+ q0i = br_i32_ninv32(mq[1]);
+ br_i32_decode_reduce(s2, x, xlen, mq);
+ br_i32_modpow(s2, sk->dq, sk->dqlen, mq, q0i, t1, t2);
+
+ /*
+ * Compute:
+ * h = (s1 - s2)*(1/q) mod p
+ * s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is
+ * unclear about whether p may be lower than q (some existing,
+ * widely deployed implementations of RSA don't tolerate p < q),
+ * but we want to support that occurrence, so we need to use the
+ * reduction function.
+ *
+ * Since we use br_i32_decode_reduce() for iq (purportedly, the
+ * inverse of q modulo p), we also tolerate improperly large
+ * values for this parameter.
+ */
+ br_i32_reduce(t2, s2, mp);
+ br_i32_add(s1, mp, br_i32_sub(s1, t2, 1));
+ br_i32_to_monty(s1, mp);
+ br_i32_decode_reduce(t1, sk->iq, sk->iqlen, mp);
+ br_i32_montymul(t2, s1, t1, mp, p0i);
+
+ /*
+ * h is now in t2. We compute the final result:
+ * s = s2 + q*h
+ * All these operations are non-modular.
+ *
+ * We need mq, s2 and t2. We use the t3 buffer as destination.
+ * The buffers mp, s1 and t1 are no longer needed. Moreover,
+ * the first step is to copy s2 into the destination buffer t3.
+ * We thus arranged for t3 to actually share space with s2, and
+ * to be followed by the space formerly used by s1 and t1.
+ */
+ br_i32_mulacc(t3, mq, t2);
+
+ /*
+ * Encode the result. Since we already checked the value of xlen,
+ * we can just use it right away.
+ */
+ br_i32_encode(x, xlen, t3);
+
+ /*
+ * The only error conditions remaining at that point are invalid
+ * values for p and q (even integers).
+ */
+ return p0i & q0i & r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_sign.c
new file mode 100644
index 00000000..0f72f927
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_sign.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pss_sig_pad(rng, hf_data, hf_mgf1, hash,
+ salt_len, sk->n_bitlen, x))
+ {
+ return 0;
+ }
+ return br_rsa_i32_private(x, sk);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_vrfy.c
new file mode 100644
index 00000000..2e70d234
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_pss_vrfy.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i32_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pss_sig_unpad(hf_data, hf_mgf1,
+ hash, salt_len, pk, sig);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i32_pub.c b/test/monniaux/BearSSL/src/rsa/rsa_i32_pub.c
new file mode 100644
index 00000000..6e8d8e3e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i32_pub.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i32_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk)
+{
+ const unsigned char *n;
+ size_t nlen;
+ uint32_t m[1 + (BR_MAX_RSA_SIZE >> 5)];
+ uint32_t a[1 + (BR_MAX_RSA_SIZE >> 5)];
+ uint32_t t1[1 + (BR_MAX_RSA_SIZE >> 5)];
+ uint32_t t2[1 + (BR_MAX_RSA_SIZE >> 5)];
+ uint32_t m0i, r;
+
+ /*
+ * Get the actual length of the modulus, and see if it fits within
+ * our stack buffer. We also check that the length of x[] is valid.
+ */
+ n = pk->n;
+ nlen = pk->nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+ if (nlen == 0 || nlen > (BR_MAX_RSA_SIZE >> 3) || xlen != nlen) {
+ return 0;
+ }
+ br_i32_decode(m, n, nlen);
+ m0i = br_i32_ninv32(m[1]);
+
+ /*
+ * Note: if m[] is even, then m0i == 0. Otherwise, m0i must be
+ * an odd integer.
+ */
+ r = m0i & 1;
+
+ /*
+ * Decode x[] into a[]; we also check that its value is proper.
+ */
+ r &= br_i32_decode_mod(a, x, xlen, m);
+
+ /*
+ * Compute the modular exponentiation.
+ */
+ br_i32_modpow(a, pk->e, pk->elen, m, m0i, t1, t2);
+
+ /*
+ * Encode the result.
+ */
+ br_i32_encode(x, xlen, a);
+ return r;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_keygen.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_keygen.c
new file mode 100644
index 00000000..8f55c375
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_keygen.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_keygen(const br_prng_class **rng,
+ br_rsa_private_key *sk, void *kbuf_priv,
+ br_rsa_public_key *pk, void *kbuf_pub,
+ unsigned size, uint32_t pubexp)
+{
+ return br_rsa_i31_keygen_inner(rng,
+ sk, kbuf_priv, pk, kbuf_pub, size, pubexp,
+ &br_i62_modpow_opt_as_i31);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_keygen
+br_rsa_i62_keygen_get()
+{
+ return &br_rsa_i62_keygen;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_keygen
+br_rsa_i62_keygen_get()
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_decrypt.c
new file mode 100644
index 00000000..38470dd3
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_decrypt.c
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_oaep_decrypt(const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_private_key *sk, void *data, size_t *len)
+{
+ uint32_t r;
+
+ if (*len != ((sk->n_bitlen + 7) >> 3)) {
+ return 0;
+ }
+ r = br_rsa_i62_private(data, sk);
+ r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
+ return r;
+}
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_decrypt
+br_rsa_i62_oaep_decrypt_get(void)
+{
+ return &br_rsa_i62_oaep_decrypt;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_decrypt
+br_rsa_i62_oaep_decrypt_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_encrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_encrypt.c
new file mode 100644
index 00000000..cf41ecb8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_oaep_encrypt.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+size_t
+br_rsa_i62_oaep_encrypt(
+ const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len)
+{
+ size_t dlen;
+
+ dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
+ pk, dst, dst_max_len, src, src_len);
+ if (dlen == 0) {
+ return 0;
+ }
+ return dlen & -(size_t)br_rsa_i62_public(dst, dlen, pk);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_encrypt
+br_rsa_i62_oaep_encrypt_get(void)
+{
+ return &br_rsa_i62_oaep_encrypt;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_oaep_encrypt
+br_rsa_i62_oaep_encrypt_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_sign.c
new file mode 100644
index 00000000..a20a0846
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_sign.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) {
+ return 0;
+ }
+ return br_rsa_i62_private(x, sk);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_sign
+br_rsa_i62_pkcs1_sign_get(void)
+{
+ return &br_rsa_i62_pkcs1_sign;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_sign
+br_rsa_i62_pkcs1_sign_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_vrfy.c
new file mode 100644
index 00000000..6519161b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_pkcs1_vrfy.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen,
+ const unsigned char *hash_oid, size_t hash_len,
+ const br_rsa_public_key *pk, unsigned char *hash_out)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i62_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_vrfy
+br_rsa_i62_pkcs1_vrfy_get(void)
+{
+ return &br_rsa_i62_pkcs1_vrfy;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_pkcs1_vrfy
+br_rsa_i62_pkcs1_vrfy_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_priv.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_priv.c
new file mode 100644
index 00000000..f0da6006
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_priv.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#define U (2 + ((BR_MAX_RSA_FACTOR + 30) / 31))
+#define TLEN (4 * U) /* TLEN is counted in 64-bit words */
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_private(unsigned char *x, const br_rsa_private_key *sk)
+{
+ const unsigned char *p, *q;
+ size_t plen, qlen;
+ size_t fwlen;
+ uint32_t p0i, q0i;
+ size_t xlen, u;
+ uint64_t tmp[TLEN];
+ long z;
+ uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
+ uint32_t r;
+
+ /*
+ * Compute the actual lengths of p and q, in bytes.
+ * These lengths are not considered secret (we cannot really hide
+ * them anyway in constant-time code).
+ */
+ p = sk->p;
+ plen = sk->plen;
+ while (plen > 0 && *p == 0) {
+ p ++;
+ plen --;
+ }
+ q = sk->q;
+ qlen = sk->qlen;
+ while (qlen > 0 && *q == 0) {
+ q ++;
+ qlen --;
+ }
+
+ /*
+ * Compute the maximum factor length, in words.
+ */
+ z = (long)(plen > qlen ? plen : qlen) << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 31;
+ fwlen ++;
+ }
+
+ /*
+ * Convert size to 62-bit words.
+ */
+ fwlen = (fwlen + 1) >> 1;
+
+ /*
+ * We need to fit at least 6 values in the stack buffer.
+ */
+ if (6 * fwlen > TLEN) {
+ return 0;
+ }
+
+ /*
+ * Compute signature length (in bytes).
+ */
+ xlen = (sk->n_bitlen + 7) >> 3;
+
+ /*
+ * Decode q.
+ */
+ mq = (uint32_t *)tmp;
+ br_i31_decode(mq, q, qlen);
+
+ /*
+ * Decode p.
+ */
+ t1 = (uint32_t *)(tmp + fwlen);
+ br_i31_decode(t1, p, plen);
+
+ /*
+ * Compute the modulus (product of the two factors), to compare
+ * it with the source value. We use br_i31_mulacc(), since it's
+ * already used later on.
+ */
+ t2 = (uint32_t *)(tmp + 2 * fwlen);
+ br_i31_zero(t2, mq[0]);
+ br_i31_mulacc(t2, mq, t1);
+
+ /*
+ * We encode the modulus into bytes, to perform the comparison
+ * with bytes. We know that the product length, in bytes, is
+ * exactly xlen.
+ * The comparison actually computes the carry when subtracting
+ * the modulus from the source value; that carry must be 1 for
+ * a value in the correct range. We keep it in r, which is our
+ * accumulator for the error code.
+ */
+ t3 = (uint32_t *)(tmp + 4 * fwlen);
+ br_i31_encode(t3, xlen, t2);
+ u = xlen;
+ r = 0;
+ while (u > 0) {
+ uint32_t wn, wx;
+
+ u --;
+ wn = ((unsigned char *)t3)[u];
+ wx = x[u];
+ r = ((wx - (wn + r)) >> 8) & 1;
+ }
+
+ /*
+ * Move the decoded p to another temporary buffer.
+ */
+ mp = (uint32_t *)(tmp + 2 * fwlen);
+ memmove(mp, t1, 2 * fwlen * sizeof *t1);
+
+ /*
+ * Compute s2 = x^dq mod q.
+ */
+ q0i = br_i31_ninv31(mq[1]);
+ s2 = (uint32_t *)(tmp + fwlen);
+ br_i31_decode_reduce(s2, x, xlen, mq);
+ r &= br_i62_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
+ tmp + 3 * fwlen, TLEN - 3 * fwlen);
+
+ /*
+ * Compute s1 = x^dp mod p.
+ */
+ p0i = br_i31_ninv31(mp[1]);
+ s1 = (uint32_t *)(tmp + 3 * fwlen);
+ br_i31_decode_reduce(s1, x, xlen, mp);
+ r &= br_i62_modpow_opt(s1, sk->dp, sk->dplen, mp, p0i,
+ tmp + 4 * fwlen, TLEN - 4 * fwlen);
+
+ /*
+ * Compute:
+ * h = (s1 - s2)*(1/q) mod p
+ * s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is
+ * unclear about whether p may be lower than q (some existing,
+ * widely deployed implementations of RSA don't tolerate p < q),
+ * but we want to support that occurrence, so we need to use the
+ * reduction function.
+ *
+ * Since we use br_i31_decode_reduce() for iq (purportedly, the
+ * inverse of q modulo p), we also tolerate improperly large
+ * values for this parameter.
+ */
+ t1 = (uint32_t *)(tmp + 4 * fwlen);
+ t2 = (uint32_t *)(tmp + 5 * fwlen);
+ br_i31_reduce(t2, s2, mp);
+ br_i31_add(s1, mp, br_i31_sub(s1, t2, 1));
+ br_i31_to_monty(s1, mp);
+ br_i31_decode_reduce(t1, sk->iq, sk->iqlen, mp);
+ br_i31_montymul(t2, s1, t1, mp, p0i);
+
+ /*
+ * h is now in t2. We compute the final result:
+ * s = s2 + q*h
+ * All these operations are non-modular.
+ *
+ * We need mq, s2 and t2. We use the t3 buffer as destination.
+ * The buffers mp, s1 and t1 are no longer needed, so we can
+ * reuse them for t3. Moreover, the first step of the computation
+ * is to copy s2 into t3, after which s2 is not needed. Right
+ * now, mq is in slot 0, s2 is in slot 1, and t2 is in slot 5.
+ * Therefore, we have ample room for t3 by simply using s2.
+ */
+ t3 = s2;
+ br_i31_mulacc(t3, mq, t2);
+
+ /*
+ * Encode the result. Since we already checked the value of xlen,
+ * we can just use it right away.
+ */
+ br_i31_encode(x, xlen, t3);
+
+ /*
+ * The only error conditions remaining at that point are invalid
+ * values for p and q (even integers).
+ */
+ return p0i & q0i & r;
+}
+
+/* see bearssl_rsa.h */
+br_rsa_private
+br_rsa_i62_private_get(void)
+{
+ return &br_rsa_i62_private;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_private
+br_rsa_i62_private_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_sign.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_sign.c
new file mode 100644
index 00000000..7232f6d5
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_sign.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_pss_sign(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_private_key *sk, unsigned char *x)
+{
+ if (!br_rsa_pss_sig_pad(rng, hf_data, hf_mgf1, hash,
+ salt_len, sk->n_bitlen, x))
+ {
+ return 0;
+ }
+ return br_rsa_i62_private(x, sk);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_pss_sign
+br_rsa_i62_pss_sign_get(void)
+{
+ return &br_rsa_i62_pss_sign;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_pss_sign
+br_rsa_i62_pss_sign_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_vrfy.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_vrfy.c
new file mode 100644
index 00000000..e726e823
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_pss_vrfy.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_pss_vrfy(const unsigned char *x, size_t xlen,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const void *hash, size_t salt_len, const br_rsa_public_key *pk)
+{
+ unsigned char sig[BR_MAX_RSA_SIZE >> 3];
+
+ if (xlen > (sizeof sig)) {
+ return 0;
+ }
+ memcpy(sig, x, xlen);
+ if (!br_rsa_i62_public(sig, xlen, pk)) {
+ return 0;
+ }
+ return br_rsa_pss_sig_unpad(hf_data, hf_mgf1,
+ hash, salt_len, pk, sig);
+}
+
+/* see bearssl_rsa.h */
+br_rsa_pss_vrfy
+br_rsa_i62_pss_vrfy_get(void)
+{
+ return &br_rsa_i62_pss_vrfy;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_pss_vrfy
+br_rsa_i62_pss_vrfy_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_i62_pub.c b/test/monniaux/BearSSL/src/rsa/rsa_i62_pub.c
new file mode 100644
index 00000000..70cf61bd
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_i62_pub.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+/*
+ * As a strict minimum, we need four buffers that can hold a
+ * modular integer. But TLEN is expressed in 64-bit words.
+ */
+#define TLEN (2 * (2 + ((BR_MAX_RSA_SIZE + 30) / 31)))
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_i62_public(unsigned char *x, size_t xlen,
+ const br_rsa_public_key *pk)
+{
+ const unsigned char *n;
+ size_t nlen;
+ uint64_t tmp[TLEN];
+ uint32_t *m, *a;
+ size_t fwlen;
+ long z;
+ uint32_t m0i, r;
+
+ /*
+ * Get the actual length of the modulus, and see if it fits within
+ * our stack buffer. We also check that the length of x[] is valid.
+ */
+ n = pk->n;
+ nlen = pk->nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+ if (nlen == 0 || nlen > (BR_MAX_RSA_SIZE >> 3) || xlen != nlen) {
+ return 0;
+ }
+ z = (long)nlen << 3;
+ fwlen = 1;
+ while (z > 0) {
+ z -= 31;
+ fwlen ++;
+ }
+ /*
+ * Convert fwlen to a count in 62-bit words.
+ */
+ fwlen = (fwlen + 1) >> 1;
+
+ /*
+ * The modulus gets decoded into m[].
+ * The value to exponentiate goes into a[].
+ */
+ m = (uint32_t *)tmp;
+ a = (uint32_t *)(tmp + fwlen);
+
+ /*
+ * Decode the modulus.
+ */
+ br_i31_decode(m, n, nlen);
+ m0i = br_i31_ninv31(m[1]);
+
+ /*
+ * Note: if m[] is even, then m0i == 0. Otherwise, m0i must be
+ * an odd integer.
+ */
+ r = m0i & 1;
+
+ /*
+ * Decode x[] into a[]; we also check that its value is proper.
+ */
+ r &= br_i31_decode_mod(a, x, xlen, m);
+
+ /*
+ * Compute the modular exponentiation.
+ */
+ br_i62_modpow_opt(a, pk->e, pk->elen, m, m0i,
+ tmp + 2 * fwlen, TLEN - 2 * fwlen);
+
+ /*
+ * Encode the result.
+ */
+ br_i31_encode(x, xlen, a);
+ return r;
+}
+
+/* see bearssl_rsa.h */
+br_rsa_public
+br_rsa_i62_public_get(void)
+{
+ return &br_rsa_i62_public;
+}
+
+#else
+
+/* see bearssl_rsa.h */
+br_rsa_public
+br_rsa_i62_public_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_oaep_pad.c b/test/monniaux/BearSSL/src/rsa/rsa_oaep_pad.c
new file mode 100644
index 00000000..5327dc26
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_oaep_pad.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Hash some data. This is put as a separate function so that stack
+ * allocation of the hash function context is done only for the duration
+ * of the hash.
+ */
+static void
+hash_data(const br_hash_class *dig, void *dst, const void *src, size_t len)
+{
+ br_hash_compat_context hc;
+
+ hc.vtable = dig;
+ dig->init(&hc.vtable);
+ dig->update(&hc.vtable, src, len);
+ dig->out(&hc.vtable, dst);
+}
+
+/* see inner.h */
+size_t
+br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
+ const void *label, size_t label_len,
+ const br_rsa_public_key *pk,
+ void *dst, size_t dst_max_len,
+ const void *src, size_t src_len)
+{
+ size_t k, hlen;
+ unsigned char *buf;
+
+ hlen = br_digest_size(dig);
+
+ /*
+ * Compute actual modulus length (in bytes).
+ */
+ k = pk->nlen;
+ while (k > 0 && pk->n[k - 1] == 0) {
+ k --;
+ }
+
+ /*
+ * An error is reported if:
+ * - the modulus is too short;
+ * - the source message length is too long;
+ * - the destination buffer is too short.
+ */
+ if (k < ((hlen << 1) + 2)
+ || src_len > (k - (hlen << 1) - 2)
+ || dst_max_len < k)
+ {
+ return 0;
+ }
+
+ /*
+ * Apply padding. At this point, things cannot fail.
+ */
+ buf = dst;
+
+ /*
+ * Assemble: DB = lHash || PS || 0x01 || M
+ * We first place the source message M with memmove(), so that
+ * overlaps between source and destination buffers are supported.
+ */
+ memmove(buf + k - src_len, src, src_len);
+ hash_data(dig, buf + 1 + hlen, label, label_len);
+ memset(buf + 1 + (hlen << 1), 0, k - src_len - (hlen << 1) - 2);
+ buf[k - src_len - 1] = 0x01;
+
+ /*
+ * Make the random seed.
+ */
+ (*rnd)->generate(rnd, buf + 1, hlen);
+
+ /*
+ * Mask DB with the mask generated from the seed.
+ */
+ br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
+
+ /*
+ * Mask the seed with the mask generated from the masked DB.
+ */
+ br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
+
+ /*
+ * Padding result: EM = 0x00 || maskedSeed || maskedDB.
+ */
+ buf[0] = 0x00;
+ return k;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_oaep_unpad.c b/test/monniaux/BearSSL/src/rsa/rsa_oaep_unpad.c
new file mode 100644
index 00000000..7c4be6a8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_oaep_unpad.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Hash some data and XOR the result into the provided buffer. This is put
+ * as a separate function so that stack allocation of the hash function
+ * context is done only for the duration of the hash.
+ */
+static void
+xor_hash_data(const br_hash_class *dig, void *dst, const void *src, size_t len)
+{
+ br_hash_compat_context hc;
+ unsigned char tmp[64];
+ unsigned char *buf;
+ size_t u, hlen;
+
+ hc.vtable = dig;
+ dig->init(&hc.vtable);
+ dig->update(&hc.vtable, src, len);
+ dig->out(&hc.vtable, tmp);
+ buf = dst;
+ hlen = br_digest_size(dig);
+ for (u = 0; u < hlen; u ++) {
+ buf[u] ^= tmp[u];
+ }
+}
+
+/* see inner.h */
+uint32_t
+br_rsa_oaep_unpad(const br_hash_class *dig,
+ const void *label, size_t label_len,
+ void *data, size_t *len)
+{
+ size_t u, k, hlen;
+ unsigned char *buf;
+ uint32_t r, s, zlen;
+
+ hlen = br_digest_size(dig);
+ k = *len;
+ buf = data;
+
+ /*
+ * There must be room for the padding.
+ */
+ if (k < ((hlen << 1) + 2)) {
+ return 0;
+ }
+
+ /*
+ * Unmask the seed, then the DB value.
+ */
+ br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
+ br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
+
+ /*
+ * Hash the label and XOR it with the value in the array; if
+ * they are equal then these should yield only zeros.
+ */
+ xor_hash_data(dig, buf + 1 + hlen, label, label_len);
+
+ /*
+ * At that point, if the padding was correct, when we should
+ * have: 0x00 || seed || 0x00 ... 0x00 0x01 || M
+ * Padding is valid as long as:
+ * - There is at least hlen+1 leading bytes of value 0x00.
+ * - There is at least one non-zero byte.
+ * - The first (leftmost) non-zero byte has value 0x01.
+ *
+ * Ultimately, we may leak the resulting message length, i.e.
+ * the position of the byte of value 0x01, but we must take care
+ * to do so only if the number of zero bytes has been verified
+ * to be at least hlen+1.
+ *
+ * The loop below counts the number of bytes of value 0x00, and
+ * checks that the next byte has value 0x01, in constant-time.
+ *
+ * - If the initial byte (before the seed) is not 0x00, then
+ * r and s are set to 0, and stay there.
+ * - Value r is 1 until the first non-zero byte is reached
+ * (after the seed); it switches to 0 at that point.
+ * - Value s is set to 1 if and only if the data encountered
+ * at the time of the transition of r from 1 to 0 has value
+ * exactly 0x01.
+ * - Value zlen counts the number of leading bytes of value zero
+ * (after the seed).
+ */
+ r = 1 - ((buf[0] + 0xFF) >> 8);
+ s = 0;
+ zlen = 0;
+ for (u = hlen + 1; u < k; u ++) {
+ uint32_t w, nz;
+
+ w = buf[u];
+
+ /*
+ * nz == 1 only for the first non-zero byte.
+ */
+ nz = r & ((w + 0xFF) >> 8);
+ s |= nz & EQ(w, 0x01);
+ r &= NOT(nz);
+ zlen += r;
+ }
+
+ /*
+ * Padding is correct only if s == 1, _and_ zlen >= hlen.
+ */
+ s &= GE(zlen, (uint32_t)hlen);
+
+ /*
+ * At that point, padding was verified, and we are now allowed
+ * to make conditional jumps.
+ */
+ if (s) {
+ size_t plen;
+
+ plen = 2 + hlen + zlen;
+ k -= plen;
+ memmove(buf, buf + plen, k);
+ *len = k;
+ }
+ return s;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_pad.c b/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_pad.c
new file mode 100644
index 00000000..06c3bd70
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_pad.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid,
+ const unsigned char *hash, size_t hash_len,
+ uint32_t n_bitlen, unsigned char *x)
+{
+ size_t u, x3, xlen;
+
+ /*
+ * Padded hash value has format:
+ * 00 01 FF .. FF 00 30 x1 30 x2 06 x3 OID 05 00 04 x4 HASH
+ *
+ * with the following rules:
+ *
+ * -- Total length is equal to the modulus length (unsigned
+ * encoding).
+ *
+ * -- There must be at least eight bytes of value 0xFF.
+ *
+ * -- x4 is equal to the hash length (hash_len).
+ *
+ * -- x3 is equal to the encoded OID value length (hash_oid[0]).
+ *
+ * -- x2 = x3 + 4.
+ *
+ * -- x1 = x2 + x4 + 4 = x3 + x4 + 8.
+ *
+ * Note: the "05 00" is optional (signatures with and without
+ * that sequence exist in practice), but notes in PKCS#1 seem to
+ * indicate that the presence of that sequence (specifically,
+ * an ASN.1 NULL value for the hash parameters) may be slightly
+ * more "standard" than the opposite.
+ */
+ xlen = (n_bitlen + 7) >> 3;
+
+ if (hash_oid == NULL) {
+ if (xlen < hash_len + 11) {
+ return 0;
+ }
+ x[0] = 0x00;
+ x[1] = 0x01;
+ u = xlen - hash_len;
+ memset(x + 2, 0xFF, u - 3);
+ x[u - 1] = 0x00;
+ } else {
+ x3 = hash_oid[0];
+
+ /*
+ * Check that there is enough room for all the elements,
+ * including at least eight bytes of value 0xFF.
+ */
+ if (xlen < (x3 + hash_len + 21)) {
+ return 0;
+ }
+ x[0] = 0x00;
+ x[1] = 0x01;
+ u = xlen - x3 - hash_len - 11;
+ memset(x + 2, 0xFF, u - 2);
+ x[u] = 0x00;
+ x[u + 1] = 0x30;
+ x[u + 2] = x3 + hash_len + 8;
+ x[u + 3] = 0x30;
+ x[u + 4] = x3 + 4;
+ x[u + 5] = 0x06;
+ memcpy(x + u + 6, hash_oid, x3 + 1);
+ u += x3 + 7;
+ x[u ++] = 0x05;
+ x[u ++] = 0x00;
+ x[u ++] = 0x04;
+ x[u ++] = hash_len;
+ }
+ memcpy(x + u, hash, hash_len);
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_unpad.c b/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_unpad.c
new file mode 100644
index 00000000..c8ae08fa
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_pkcs1_sig_unpad.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
+ const unsigned char *hash_oid, size_t hash_len,
+ unsigned char *hash_out)
+{
+ static const unsigned char pad1[] = {
+ 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+ };
+
+ unsigned char pad2[43];
+ size_t u, x2, x3, pad_len, zlen;
+
+ if (sig_len < 11) {
+ return 0;
+ }
+
+ /*
+ * Expected format:
+ * 00 01 FF ... FF 00 30 x1 30 x2 06 x3 OID [ 05 00 ] 04 x4 HASH
+ *
+ * with the following rules:
+ *
+ * -- Total length is that of the modulus and the signature
+ * (this was already verified by br_rsa_i31_public()).
+ *
+ * -- There are at least eight bytes of value 0xFF.
+ *
+ * -- x4 is equal to the hash length (hash_len).
+ *
+ * -- x3 is equal to the encoded OID value length (so x3 is the
+ * first byte of hash_oid[]).
+ *
+ * -- If the "05 00" is present, then x2 == x3 + 4; otherwise,
+ * x2 == x3 + 2.
+ *
+ * -- x1 == x2 + x4 + 4.
+ *
+ * So the total length after the last "FF" is either x3 + x4 + 11
+ * (with the "05 00") or x3 + x4 + 9 (without the "05 00").
+ */
+
+ /*
+ * Check the "00 01 FF .. FF 00" with at least eight 0xFF bytes.
+ * The comparison is valid because we made sure that the signature
+ * is at least 11 bytes long.
+ */
+ if (memcmp(sig, pad1, sizeof pad1) != 0) {
+ return 0;
+ }
+ for (u = sizeof pad1; u < sig_len; u ++) {
+ if (sig[u] != 0xFF) {
+ break;
+ }
+ }
+
+ /*
+ * Remaining length is sig_len - u bytes (including the 00 just
+ * after the last FF). This must be equal to one of the two
+ * possible values (depending on whether the "05 00" sequence is
+ * present or not).
+ */
+ if (hash_oid == NULL) {
+ if (sig_len - u != hash_len + 1 || sig[u] != 0x00) {
+ return 0;
+ }
+ } else {
+ x3 = hash_oid[0];
+ pad_len = x3 + 9;
+ memset(pad2, 0, pad_len);
+ zlen = sig_len - u - hash_len;
+ if (zlen == pad_len) {
+ x2 = x3 + 2;
+ } else if (zlen == pad_len + 2) {
+ x2 = x3 + 4;
+ pad_len = zlen;
+ pad2[pad_len - 4] = 0x05;
+ } else {
+ return 0;
+ }
+ pad2[1] = 0x30;
+ pad2[2] = x2 + hash_len + 4;
+ pad2[3] = 0x30;
+ pad2[4] = x2;
+ pad2[5] = 0x06;
+ memcpy(pad2 + 6, hash_oid, x3 + 1);
+ pad2[pad_len - 2] = 0x04;
+ pad2[pad_len - 1] = hash_len;
+ if (memcmp(pad2, sig + u, pad_len) != 0) {
+ return 0;
+ }
+ }
+ memcpy(hash_out, sig + sig_len - hash_len, hash_len);
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_pad.c b/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_pad.c
new file mode 100644
index 00000000..13e90274
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_pad.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_rsa_pss_sig_pad(const br_prng_class **rng,
+ const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ uint32_t n_bitlen, unsigned char *x)
+{
+ size_t xlen, hash_len;
+ br_hash_compat_context hc;
+ unsigned char *salt, *seed;
+
+ hash_len = br_digest_size(hf_data);
+
+ /*
+ * The padded string is one bit smaller than the modulus;
+ * notably, if the modulus length is equal to 1 modulo 8, then
+ * the padded string will be one _byte_ smaller, and the first
+ * byte will be set to 0. We apply these transformations here.
+ */
+ n_bitlen --;
+ if ((n_bitlen & 7) == 0) {
+ *x ++ = 0;
+ }
+ xlen = (n_bitlen + 7) >> 3;
+
+ /*
+ * Check that the modulus is large enough for the hash value
+ * length combined with the intended salt length.
+ */
+ if (hash_len > xlen || salt_len > xlen
+ || (hash_len + salt_len + 2) > xlen)
+ {
+ return 0;
+ }
+
+ /*
+ * Produce a random salt.
+ */
+ salt = x + xlen - hash_len - salt_len - 1;
+ if (salt_len != 0) {
+ (*rng)->generate(rng, salt, salt_len);
+ }
+
+ /*
+ * Compute the seed for MGF1.
+ */
+ seed = x + xlen - hash_len - 1;
+ hf_data->init(&hc.vtable);
+ memset(seed, 0, 8);
+ hf_data->update(&hc.vtable, seed, 8);
+ hf_data->update(&hc.vtable, hash, hash_len);
+ hf_data->update(&hc.vtable, salt, salt_len);
+ hf_data->out(&hc.vtable, seed);
+
+ /*
+ * Prepare string PS (padded salt). The salt is already at the
+ * right place.
+ */
+ memset(x, 0, xlen - salt_len - hash_len - 2);
+ x[xlen - salt_len - hash_len - 2] = 0x01;
+
+ /*
+ * Generate the mask and XOR it into PS.
+ */
+ br_mgf1_xor(x, xlen - hash_len - 1, hf_mgf1, seed, hash_len);
+
+ /*
+ * Clear the top bits to ensure the value is lower than the
+ * modulus.
+ */
+ x[0] &= 0xFF >> (((uint32_t)xlen << 3) - n_bitlen);
+
+ /*
+ * The seed (H) is already in the right place. We just set the
+ * last byte.
+ */
+ x[xlen - 1] = 0xBC;
+
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_unpad.c b/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_unpad.c
new file mode 100644
index 00000000..a9f8ca3a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_pss_sig_unpad.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+uint32_t
+br_rsa_pss_sig_unpad(const br_hash_class *hf_data,
+ const br_hash_class *hf_mgf1,
+ const unsigned char *hash, size_t salt_len,
+ const br_rsa_public_key *pk, unsigned char *x)
+{
+ size_t u, xlen, hash_len;
+ br_hash_compat_context hc;
+ unsigned char *seed, *salt;
+ unsigned char tmp[64];
+ uint32_t r, n_bitlen;
+
+ hash_len = br_digest_size(hf_data);
+
+ /*
+ * Value r will be set to a non-zero value is any test fails.
+ */
+ r = 0;
+
+ /*
+ * The value bit length (as an integer) must be strictly less than
+ * that of the modulus.
+ */
+ for (u = 0; u < pk->nlen; u ++) {
+ if (pk->n[u] != 0) {
+ break;
+ }
+ }
+ if (u == pk->nlen) {
+ return 0;
+ }
+ n_bitlen = BIT_LENGTH(pk->n[u]) + ((uint32_t)(pk->nlen - u - 1) << 3);
+ n_bitlen --;
+ if ((n_bitlen & 7) == 0) {
+ r |= *x ++;
+ } else {
+ r |= x[0] & (0xFF << (n_bitlen & 7));
+ }
+ xlen = (n_bitlen + 7) >> 3;
+
+ /*
+ * Check that the modulus is large enough for the hash value
+ * length combined with the intended salt length.
+ */
+ if (hash_len > xlen || salt_len > xlen
+ || (hash_len + salt_len + 2) > xlen)
+ {
+ return 0;
+ }
+
+ /*
+ * Check value of rightmost byte.
+ */
+ r |= x[xlen - 1] ^ 0xBC;
+
+ /*
+ * Generate the mask and XOR it into the first bytes to reveal PS;
+ * we must also mask out the leading bits.
+ */
+ seed = x + xlen - hash_len - 1;
+ br_mgf1_xor(x, xlen - hash_len - 1, hf_mgf1, seed, hash_len);
+ if ((n_bitlen & 7) != 0) {
+ x[0] &= 0xFF >> (8 - (n_bitlen & 7));
+ }
+
+ /*
+ * Check that all padding bytes have the expected value.
+ */
+ for (u = 0; u < (xlen - hash_len - salt_len - 2); u ++) {
+ r |= x[u];
+ }
+ r |= x[xlen - hash_len - salt_len - 2] ^ 0x01;
+
+ /*
+ * Recompute H.
+ */
+ salt = x + xlen - hash_len - salt_len - 1;
+ hf_data->init(&hc.vtable);
+ memset(tmp, 0, 8);
+ hf_data->update(&hc.vtable, tmp, 8);
+ hf_data->update(&hc.vtable, hash, hash_len);
+ hf_data->update(&hc.vtable, salt, salt_len);
+ hf_data->out(&hc.vtable, tmp);
+
+ /*
+ * Check that the recomputed H value matches the one appearing
+ * in the string.
+ */
+ for (u = 0; u < hash_len; u ++) {
+ r |= tmp[u] ^ x[(xlen - salt_len - 1) + u];
+ }
+
+ return EQ0(r);
+}
diff --git a/test/monniaux/BearSSL/src/rsa/rsa_ssl_decrypt.c b/test/monniaux/BearSSL/src/rsa/rsa_ssl_decrypt.c
new file mode 100644
index 00000000..047eb18c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/rsa/rsa_ssl_decrypt.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_rsa.h */
+uint32_t
+br_rsa_ssl_decrypt(br_rsa_private core, const br_rsa_private_key *sk,
+ unsigned char *data, size_t len)
+{
+ uint32_t x;
+ size_t u;
+
+ /*
+ * A first check on length. Since this test works only on the
+ * buffer length, it needs not (and cannot) be constant-time.
+ */
+ if (len < 59 || len != (sk->n_bitlen + 7) >> 3) {
+ return 0;
+ }
+ x = core(data, sk);
+
+ x &= EQ(data[0], 0x00);
+ x &= EQ(data[1], 0x02);
+ for (u = 2; u < (len - 49); u ++) {
+ x &= NEQ(data[u], 0);
+ }
+ x &= EQ(data[len - 49], 0x00);
+ memmove(data, data + len - 48, 48);
+ return x;
+}
diff --git a/test/monniaux/BearSSL/src/settings.c b/test/monniaux/BearSSL/src/settings.c
new file mode 100644
index 00000000..309271cc
--- /dev/null
+++ b/test/monniaux/BearSSL/src/settings.c
@@ -0,0 +1,306 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const br_config_option config[] = {
+ { "BR_64",
+#if BR_64
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_AES_X86NI",
+#if BR_AES_X86NI
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_amd64",
+#if BR_amd64
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_ARMEL_CORTEXM_GCC",
+#if BR_ARMEL_CORTEXM_GCC
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_BE_UNALIGNED",
+#if BR_BE_UNALIGNED
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_CLANG",
+#if BR_CLANG
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_CLANG_3_7",
+#if BR_CLANG_3_7
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_CLANG_3_8",
+#if BR_CLANG_3_8
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_CT_MUL15",
+#if BR_CT_MUL15
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_CT_MUL31",
+#if BR_CT_MUL31
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC",
+#if BR_GCC
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_4",
+#if BR_GCC_4_4
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_5",
+#if BR_GCC_4_5
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_6",
+#if BR_GCC_4_6
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_7",
+#if BR_GCC_4_7
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_8",
+#if BR_GCC_4_8
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_4_9",
+#if BR_GCC_4_9
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_GCC_5_0",
+#if BR_GCC_5_0
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_i386",
+#if BR_i386
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_INT128",
+#if BR_INT128
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_LE_UNALIGNED",
+#if BR_LE_UNALIGNED
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_LOMUL",
+#if BR_LOMUL
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MAX_EC_SIZE", BR_MAX_EC_SIZE },
+ { "BR_MAX_RSA_SIZE", BR_MAX_RSA_SIZE },
+ { "BR_MAX_RSA_FACTOR", BR_MAX_RSA_FACTOR },
+ { "BR_MSC",
+#if BR_MSC
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2005",
+#if BR_MSC_2005
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2008",
+#if BR_MSC_2008
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2010",
+#if BR_MSC_2010
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2012",
+#if BR_MSC_2012
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2013",
+#if BR_MSC_2013
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_MSC_2015",
+#if BR_MSC_2015
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_POWER8",
+#if BR_POWER8
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_RDRAND",
+#if BR_RDRAND
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_SLOW_MUL",
+#if BR_SLOW_MUL
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_SLOW_MUL15",
+#if BR_SLOW_MUL15
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_SSE2",
+#if BR_SSE2
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_UMUL128",
+#if BR_UMUL128
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_USE_UNIX_TIME",
+#if BR_USE_UNIX_TIME
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_USE_WIN32_RAND",
+#if BR_USE_WIN32_RAND
+ 1
+#else
+ 0
+#endif
+ },
+ { "BR_USE_WIN32_TIME",
+#if BR_USE_WIN32_TIME
+ 1
+#else
+ 0
+#endif
+ },
+
+ { NULL, 0 }
+};
+
+/* see bearssl.h */
+const br_config_option *
+br_get_config(void)
+{
+ return config;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/prf.c b/test/monniaux/BearSSL/src/ssl/prf.c
new file mode 100644
index 00000000..f04a5fb7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/prf.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_tls_phash(void *dst, size_t len,
+ const br_hash_class *dig,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed)
+{
+ unsigned char *buf;
+ unsigned char tmp[64], a[64];
+ br_hmac_key_context kc;
+ br_hmac_context hc;
+ size_t label_len, hlen, u;
+
+ if (len == 0) {
+ return;
+ }
+ buf = dst;
+ for (label_len = 0; label[label_len]; label_len ++);
+ hlen = br_digest_size(dig);
+ br_hmac_key_init(&kc, dig, secret, secret_len);
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, label, label_len);
+ for (u = 0; u < seed_num; u ++) {
+ br_hmac_update(&hc, seed[u].data, seed[u].len);
+ }
+ br_hmac_out(&hc, a);
+ for (;;) {
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, a, hlen);
+ br_hmac_update(&hc, label, label_len);
+ for (u = 0; u < seed_num; u ++) {
+ br_hmac_update(&hc, seed[u].data, seed[u].len);
+ }
+ br_hmac_out(&hc, tmp);
+ for (u = 0; u < hlen && u < len; u ++) {
+ buf[u] ^= tmp[u];
+ }
+ buf += u;
+ len -= u;
+ if (len == 0) {
+ return;
+ }
+ br_hmac_init(&hc, &kc, 0);
+ br_hmac_update(&hc, a, hlen);
+ br_hmac_out(&hc, a);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/ssl/prf_md5sha1.c b/test/monniaux/BearSSL/src/ssl/prf_md5sha1.c
new file mode 100644
index 00000000..3212833a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/prf_md5sha1.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl.h */
+void
+br_tls10_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed)
+{
+ const unsigned char *s1;
+ size_t slen;
+
+ s1 = secret;
+ slen = (secret_len + 1) >> 1;
+ memset(dst, 0, len);
+ br_tls_phash(dst, len, &br_md5_vtable,
+ s1, slen, label, seed_num, seed);
+ br_tls_phash(dst, len, &br_sha1_vtable,
+ s1 + secret_len - slen, slen, label, seed_num, seed);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/prf_sha256.c b/test/monniaux/BearSSL/src/ssl/prf_sha256.c
new file mode 100644
index 00000000..76041de8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/prf_sha256.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl.h */
+void
+br_tls12_sha256_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed)
+{
+ memset(dst, 0, len);
+ br_tls_phash(dst, len, &br_sha256_vtable,
+ secret, secret_len, label, seed_num, seed);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/prf_sha384.c b/test/monniaux/BearSSL/src/ssl/prf_sha384.c
new file mode 100644
index 00000000..c20c4e65
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/prf_sha384.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl.h */
+void
+br_tls12_sha384_prf(void *dst, size_t len,
+ const void *secret, size_t secret_len, const char *label,
+ size_t seed_num, const br_tls_prf_seed_chunk *seed)
+{
+ memset(dst, 0, len);
+ br_tls_phash(dst, len, &br_sha384_vtable,
+ secret, secret_len, label, seed_num, seed);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_ec.c b/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_ec.c
new file mode 100644
index 00000000..93ebcde6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_ec.c
@@ -0,0 +1,156 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static void
+cc_none0(const br_ssl_client_certificate_class **pctx)
+{
+ (void)pctx;
+}
+
+static void
+cc_none1(const br_ssl_client_certificate_class **pctx, size_t len)
+{
+ (void)pctx;
+ (void)len;
+}
+
+static void
+cc_none2(const br_ssl_client_certificate_class **pctx,
+ const unsigned char *data, size_t len)
+{
+ (void)pctx;
+ (void)data;
+ (void)len;
+}
+
+static void
+cc_choose(const br_ssl_client_certificate_class **pctx,
+ const br_ssl_client_context *cc, uint32_t auth_types,
+ br_ssl_client_certificate *choices)
+{
+ br_ssl_client_certificate_ec_context *zc;
+ int x;
+ int scurve;
+
+ zc = (br_ssl_client_certificate_ec_context *)pctx;
+ scurve = br_ssl_client_get_server_curve(cc);
+
+ if ((zc->allowed_usages & BR_KEYTYPE_KEYX) != 0
+ && scurve == zc->sk->curve)
+ {
+ int x;
+
+ x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;
+ if (((auth_types >> x) & 1) != 0) {
+ choices->auth_type = BR_AUTH_ECDH;
+ choices->hash_id = -1;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+ }
+ }
+
+ /*
+ * For ECDSA authentication, we must choose an appropriate
+ * hash function.
+ */
+ x = br_ssl_choose_hash((unsigned)(auth_types >> 8));
+ if (x == 0 || (zc->allowed_usages & BR_KEYTYPE_SIGN) == 0) {
+ memset(choices, 0, sizeof *choices);
+ return;
+ }
+ choices->auth_type = BR_AUTH_ECDSA;
+ choices->hash_id = x;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+}
+
+static uint32_t
+cc_do_keyx(const br_ssl_client_certificate_class **pctx,
+ unsigned char *data, size_t *len)
+{
+ br_ssl_client_certificate_ec_context *zc;
+ uint32_t r;
+ size_t xoff, xlen;
+
+ zc = (br_ssl_client_certificate_ec_context *)pctx;
+ r = zc->iec->mul(data, *len, zc->sk->x, zc->sk->xlen, zc->sk->curve);
+ xoff = zc->iec->xoff(zc->sk->curve, &xlen);
+ memmove(data, data + xoff, xlen);
+ *len = xlen;
+ return r;
+}
+
+static size_t
+cc_do_sign(const br_ssl_client_certificate_class **pctx,
+ int hash_id, size_t hv_len, unsigned char *data, size_t len)
+{
+ br_ssl_client_certificate_ec_context *zc;
+ unsigned char hv[64];
+ const br_hash_class *hc;
+
+ zc = (br_ssl_client_certificate_ec_context *)pctx;
+ memcpy(hv, data, hv_len);
+ hc = br_multihash_getimpl(zc->mhash, hash_id);
+ if (hc == NULL) {
+ return 0;
+ }
+ if (len < 139) {
+ return 0;
+ }
+ return zc->iecdsa(zc->iec, hc, hv, zc->sk, data);
+}
+
+static const br_ssl_client_certificate_class ccert_vtable = {
+ sizeof(br_ssl_client_certificate_ec_context),
+ cc_none0, /* start_name_list */
+ cc_none1, /* start_name */
+ cc_none2, /* append_name */
+ cc_none0, /* end_name */
+ cc_none0, /* end_name_list */
+ cc_choose,
+ cc_do_keyx,
+ cc_do_sign
+};
+
+/* see bearssl_ssl.h */
+void
+br_ssl_client_set_single_ec(br_ssl_client_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk, unsigned allowed_usages,
+ unsigned cert_issuer_key_type,
+ const br_ec_impl *iec, br_ecdsa_sign iecdsa)
+{
+ cc->client_auth.single_ec.vtable = &ccert_vtable;
+ cc->client_auth.single_ec.chain = chain;
+ cc->client_auth.single_ec.chain_len = chain_len;
+ cc->client_auth.single_ec.sk = sk;
+ cc->client_auth.single_ec.allowed_usages = allowed_usages;
+ cc->client_auth.single_ec.issuer_key_type = cert_issuer_key_type;
+ cc->client_auth.single_ec.mhash = &cc->eng.mhash;
+ cc->client_auth.single_ec.iec = iec;
+ cc->client_auth.single_ec.iecdsa = iecdsa;
+ cc->client_auth_vtable = &cc->client_auth.single_ec.vtable;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_rsa.c b/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_rsa.c
new file mode 100644
index 00000000..690df20f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_ccert_single_rsa.c
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static void
+cc_none0(const br_ssl_client_certificate_class **pctx)
+{
+ (void)pctx;
+}
+
+static void
+cc_none1(const br_ssl_client_certificate_class **pctx, size_t len)
+{
+ (void)pctx;
+ (void)len;
+}
+
+static void
+cc_none2(const br_ssl_client_certificate_class **pctx,
+ const unsigned char *data, size_t len)
+{
+ (void)pctx;
+ (void)data;
+ (void)len;
+}
+
+static void
+cc_choose(const br_ssl_client_certificate_class **pctx,
+ const br_ssl_client_context *cc, uint32_t auth_types,
+ br_ssl_client_certificate *choices)
+{
+ br_ssl_client_certificate_rsa_context *zc;
+ int x;
+
+ (void)cc;
+ zc = (br_ssl_client_certificate_rsa_context *)pctx;
+ x = br_ssl_choose_hash((unsigned)auth_types);
+ if (x == 0 && (auth_types & 1) == 0) {
+ memset(choices, 0, sizeof *choices);
+ }
+ choices->auth_type = BR_AUTH_RSA;
+ choices->hash_id = x;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+}
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char HASH_OID_SHA1[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char HASH_OID_SHA224[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char HASH_OID_SHA256[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char HASH_OID_SHA384[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char HASH_OID_SHA512[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static const unsigned char *HASH_OID[] = {
+ HASH_OID_SHA1,
+ HASH_OID_SHA224,
+ HASH_OID_SHA256,
+ HASH_OID_SHA384,
+ HASH_OID_SHA512
+};
+
+static size_t
+cc_do_sign(const br_ssl_client_certificate_class **pctx,
+ int hash_id, size_t hv_len, unsigned char *data, size_t len)
+{
+ br_ssl_client_certificate_rsa_context *zc;
+ unsigned char hv[64];
+ const unsigned char *hash_oid;
+ size_t sig_len;
+
+ zc = (br_ssl_client_certificate_rsa_context *)pctx;
+ memcpy(hv, data, hv_len);
+ if (hash_id == 0) {
+ hash_oid = NULL;
+ } else if (hash_id >= 2 && hash_id <= 6) {
+ hash_oid = HASH_OID[hash_id - 2];
+ } else {
+ return 0;
+ }
+ sig_len = (zc->sk->n_bitlen + 7) >> 3;
+ if (len < sig_len) {
+ return 0;
+ }
+ return zc->irsasign(hash_oid, hv, hv_len, zc->sk, data) ? sig_len : 0;
+}
+
+static const br_ssl_client_certificate_class ccert_vtable = {
+ sizeof(br_ssl_client_certificate_rsa_context),
+ cc_none0, /* start_name_list */
+ cc_none1, /* start_name */
+ cc_none2, /* append_name */
+ cc_none0, /* end_name */
+ cc_none0, /* end_name_list */
+ cc_choose,
+ 0,
+ cc_do_sign
+};
+
+/* see bearssl_ssl.h */
+void
+br_ssl_client_set_single_rsa(br_ssl_client_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk, br_rsa_pkcs1_sign irsasign)
+{
+ cc->client_auth.single_rsa.vtable = &ccert_vtable;
+ cc->client_auth.single_rsa.chain = chain;
+ cc->client_auth.single_rsa.chain_len = chain_len;
+ cc->client_auth.single_rsa.sk = sk;
+ cc->client_auth.single_rsa.irsasign = irsasign;
+ cc->client_auth_vtable = &cc->client_auth.single_rsa.vtable;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_client.c b/test/monniaux/BearSSL/src/ssl/ssl_client.c
new file mode 100644
index 00000000..28c404b8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_client.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_client_zero(br_ssl_client_context *cc)
+{
+ /*
+ * For really standard C, we should explicitly set to NULL all
+ * pointers, and 0 all other fields. However, on all our target
+ * architectures, a direct memset() will work, be faster, and
+ * use a lot less code.
+ */
+ memset(cc, 0, sizeof *cc);
+}
+
+/* see bearssl_ssl.h */
+int
+br_ssl_client_reset(br_ssl_client_context *cc,
+ const char *server_name, int resume_session)
+{
+ size_t n;
+
+ br_ssl_engine_set_buffer(&cc->eng, NULL, 0, 0);
+ cc->eng.version_out = cc->eng.version_min;
+ if (!resume_session) {
+ br_ssl_client_forget_session(cc);
+ }
+ if (!br_ssl_engine_init_rand(&cc->eng)) {
+ return 0;
+ }
+
+ /*
+ * We always set back the "reneg" flag to 0 because we use it
+ * to distinguish between first handshake and renegotiation.
+ * Note that "renegotiation" and "session resumption" are two
+ * different things.
+ */
+ cc->eng.reneg = 0;
+
+ if (server_name == NULL) {
+ cc->eng.server_name[0] = 0;
+ } else {
+ n = strlen(server_name) + 1;
+ if (n > sizeof cc->eng.server_name) {
+ br_ssl_engine_fail(&cc->eng, BR_ERR_BAD_PARAM);
+ return 0;
+ }
+ memcpy(cc->eng.server_name, server_name, n);
+ }
+
+ br_ssl_engine_hs_reset(&cc->eng,
+ br_ssl_hs_client_init_main, br_ssl_hs_client_run);
+ return br_ssl_engine_last_error(&cc->eng) == BR_ERR_OK;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_client_default_rsapub.c b/test/monniaux/BearSSL/src/ssl/ssl_client_default_rsapub.c
new file mode 100644
index 00000000..2cdaab89
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_client_default_rsapub.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_client_set_default_rsapub(br_ssl_client_context *cc)
+{
+ br_ssl_client_set_rsapub(cc, br_rsa_public_get_default());
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_client_full.c b/test/monniaux/BearSSL/src/ssl/ssl_client_full.c
new file mode 100644
index 00000000..98143499
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_client_full.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_client_init_full(br_ssl_client_context *cc,
+ br_x509_minimal_context *xc,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num)
+{
+ /*
+ * The "full" profile supports all implemented cipher suites.
+ *
+ * Rationale for suite order, from most important to least
+ * important rule:
+ *
+ * -- Don't use 3DES if AES or ChaCha20 is available.
+ * -- Try to have Forward Secrecy (ECDHE suite) if possible.
+ * -- When not using Forward Secrecy, ECDH key exchange is
+ * better than RSA key exchange (slightly more expensive on the
+ * client, but much cheaper on the server, and it implies smaller
+ * messages).
+ * -- ChaCha20+Poly1305 is better than AES/GCM (faster, smaller code).
+ * -- GCM is better than CCM and CBC. CCM is better than CBC.
+ * -- CCM is preferable over CCM_8 (with CCM_8, forgeries may succeed
+ * with probability 2^(-64)).
+ * -- AES-128 is preferred over AES-256 (AES-128 is already
+ * strong enough, and AES-256 is 40% more expensive).
+ */
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_RSA_WITH_AES_128_CCM,
+ BR_TLS_RSA_WITH_AES_256_CCM,
+ BR_TLS_RSA_WITH_AES_128_CCM_8,
+ BR_TLS_RSA_WITH_AES_256_CCM_8,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
+ };
+
+ /*
+ * All hash functions are activated.
+ * Note: the X.509 validation engine will nonetheless refuse to
+ * validate signatures that use MD5 as hash function.
+ */
+ static const br_hash_class *hashes[] = {
+ &br_md5_vtable,
+ &br_sha1_vtable,
+ &br_sha224_vtable,
+ &br_sha256_vtable,
+ &br_sha384_vtable,
+ &br_sha512_vtable
+ };
+
+ int id;
+
+ /*
+ * Reset client context and set supported versions from TLS-1.0
+ * to TLS-1.2 (inclusive).
+ */
+ br_ssl_client_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
+
+ /*
+ * X.509 engine uses SHA-256 to hash certificate DN (for
+ * comparisons).
+ */
+ br_x509_minimal_init(xc, &br_sha256_vtable,
+ trust_anchors, trust_anchors_num);
+
+ /*
+ * Set suites and asymmetric crypto implementations. We use the
+ * "i31" code for RSA (it is somewhat faster than the "i32"
+ * implementation).
+ * TODO: change that when better implementations are made available.
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_client_set_default_rsapub(cc);
+ br_ssl_engine_set_default_rsavrfy(&cc->eng);
+ br_ssl_engine_set_default_ecdsa(&cc->eng);
+ br_x509_minimal_set_rsa(xc, br_ssl_engine_get_rsavrfy(&cc->eng));
+ br_x509_minimal_set_ecdsa(xc,
+ br_ssl_engine_get_ec(&cc->eng),
+ br_ssl_engine_get_ecdsa(&cc->eng));
+
+ /*
+ * Set supported hash functions, for the SSL engine and for the
+ * X.509 engine.
+ */
+ for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
+ const br_hash_class *hc;
+
+ hc = hashes[id - 1];
+ br_ssl_engine_set_hash(&cc->eng, id, hc);
+ br_x509_minimal_set_hash(xc, id, hc);
+ }
+
+ /*
+ * Link the X.509 engine in the SSL engine.
+ */
+ br_ssl_engine_set_x509(&cc->eng, &xc->vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+ br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
+
+ /*
+ * Symmetric encryption. We use the "default" implementations
+ * (fastest among constant-time implementations).
+ */
+ br_ssl_engine_set_default_aes_cbc(&cc->eng);
+ br_ssl_engine_set_default_aes_ccm(&cc->eng);
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+ br_ssl_engine_set_default_des_cbc(&cc->eng);
+ br_ssl_engine_set_default_chapol(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine.c b/test/monniaux/BearSSL/src/ssl/ssl_engine.c
new file mode 100644
index 00000000..f4ffe185
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine.c
@@ -0,0 +1,1569 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if 0
+/* obsolete */
+
+/*
+ * If BR_USE_URANDOM is not defined, then try to autodetect its presence
+ * through compiler macros.
+ */
+#ifndef BR_USE_URANDOM
+
+/*
+ * Macro values documented on:
+ * https://sourceforge.net/p/predef/wiki/OperatingSystems/
+ *
+ * Only the most common systems have been included here for now. This
+ * should be enriched later on.
+ */
+#if defined _AIX \
+ || defined __ANDROID__ \
+ || defined __FreeBSD__ \
+ || defined __NetBSD__ \
+ || defined __OpenBSD__ \
+ || defined __DragonFly__ \
+ || defined __linux__ \
+ || (defined __sun && (defined __SVR4 || defined __svr4__)) \
+ || (defined __APPLE__ && defined __MACH__)
+#define BR_USE_URANDOM 1
+#endif
+
+#endif
+
+/*
+ * If BR_USE_WIN32_RAND is not defined, perform autodetection here.
+ */
+#ifndef BR_USE_WIN32_RAND
+
+#if defined _WIN32 || defined _WIN64
+#define BR_USE_WIN32_RAND 1
+#endif
+
+#endif
+
+#if BR_USE_URANDOM
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#endif
+
+#if BR_USE_WIN32_RAND
+#include <windows.h>
+#include <wincrypt.h>
+#pragma comment(lib, "advapi32")
+#endif
+
+#endif
+
+/* ==================================================================== */
+/*
+ * This part of the file does the low-level record management.
+ */
+
+/*
+ * IMPLEMENTATION NOTES
+ * ====================
+ *
+ * In this file, we designate by "input" (and the "i" letter) the "recv"
+ * operations: incoming records from the peer, from which payload data
+ * is obtained, and must be extracted by the application (or the SSL
+ * handshake engine). Similarly, "output" (and the "o" letter) is for
+ * "send": payload data injected by the application (and SSL handshake
+ * engine), to be wrapped into records, that are then conveyed to the
+ * peer over the transport medium.
+ *
+ * The input and output buffers may be distinct or shared. When
+ * shared, input and output cannot occur concurrently; the caller
+ * must make sure that it never needs to output data while input
+ * data has been received. In practice, a shared buffer prevents
+ * pipelining of HTTP requests, or similar protocols; however, a
+ * shared buffer saves RAM.
+ *
+ * The input buffer is pointed to by 'ibuf' and has size 'ibuf_len';
+ * the output buffer is pointed to by 'obuf' and has size 'obuf_len'.
+ * From the size of these buffers is derived the maximum fragment
+ * length, which will be honoured upon sending records; regardless of
+ * that length, incoming records will be processed as long as they
+ * fit in the input buffer, and their length still complies with the
+ * protocol specification (maximum plaintext payload length is 16384
+ * bytes).
+ *
+ * Three registers are used to manage buffering in ibuf, called ixa,
+ * ixb and ixc. Similarly, three registers are used to manage buffering
+ * in obuf, called oxa, oxb and oxc.
+ *
+ *
+ * At any time, the engine is in one of the following modes:
+ * -- Failed mode: an error occurs, no I/O can happen.
+ * -- Input mode: the engine can either receive record bytes from the
+ * transport layer, or it has some buffered payload bytes to yield.
+ * -- Output mode: the engine can either receive payload bytes, or it
+ * has some record bytes to send to the transport layer.
+ * -- Input/Output mode: both input and output modes are active. When
+ * the buffer is shared, this can happen only when the buffer is empty
+ * (no buffered payload bytes or record bytes in either direction).
+ *
+ *
+ * Failed mode:
+ * ------------
+ *
+ * I/O failed for some reason (invalid received data, not enough room
+ * for the next record...). No I/O may ever occur again for this context,
+ * until an explicit reset is performed. This mode, and the error code,
+ * are also used for protocol errors, especially handshake errors.
+ *
+ *
+ * Input mode:
+ * -----------
+ *
+ * ixa index within ibuf[] for the currently read data
+ * ixb maximum index within ibuf[] for the currently read data
+ * ixc number of bytes not yet received for the current record
+ *
+ * -- When ixa == ixb, there is no available data for readers. When
+ * ixa != ixb, there is available data and it starts at offset ixa.
+ *
+ * -- When waiting for the next record header, ixa and ixb are equal
+ * and contain a value ranging from 0 to 4; ixc is equal to 5-ixa.
+ *
+ * -- When the header has been received, record data is obtained. The
+ * ixc field records how many bytes are still needed to reach the
+ * end of the current record.
+ *
+ * ** If encryption is active, then ixa and ixb are kept equal, and
+ * point to the end of the currently received record bytes. When
+ * ixc reaches 0, decryption/MAC is applied, and ixa and ixb are
+ * adjusted.
+ *
+ * ** If encryption is not active, then ixa and ixb are distinct
+ * and data can be read right away. Additional record data is
+ * obtained only when ixa == ixb.
+ *
+ * Note: in input mode and no encryption, records larger than the buffer
+ * size are allowed. When encryption is active, the complete record must
+ * fit within the buffer, since it cannot be decrypted/MACed until it
+ * has been completely received.
+ *
+ * -- When receiving the next record header, 'version_in' contains the
+ * expected input version (0 if not expecting a specific version); on
+ * mismatch, the mode switches to 'failed'.
+ *
+ * -- When the header has been received, 'version_in' contains the received
+ * version. It is up to the caller to check and adjust the 'version_in' field
+ * to implement the required semantics.
+ *
+ * -- The 'record_type_in' field is updated with the incoming record type
+ * when the next record header has been received.
+ *
+ *
+ * Output mode:
+ * ------------
+ *
+ * oxa index within obuf[] for the currently accumulated data
+ * oxb maximum index within obuf[] for record data
+ * oxc pointer for start of record data, and for record sending
+ *
+ * -- When oxa != oxb, more data can be accumulated into the current
+ * record; when oxa == oxb, a closed record is being sent.
+ *
+ * -- When accumulating data, oxc points to the start of the data.
+ *
+ * -- During record sending, oxa (and oxb) point to the next record byte
+ * to send, and oxc indicates the end of the current record.
+ *
+ * Note: sent records must fit within the buffer, since the header is
+ * adjusted only when the complete record has been assembled.
+ *
+ * -- The 'version_out' and 'record_type_out' fields are used to build the
+ * record header when the mode is switched to 'sending'.
+ *
+ *
+ * Modes:
+ * ------
+ *
+ * The state register iomode contains one of the following values:
+ *
+ * BR_IO_FAILED I/O failed
+ * BR_IO_IN input mode
+ * BR_IO_OUT output mode
+ * BR_IO_INOUT input/output mode
+ *
+ * Whether encryption is active on incoming records is indicated by the
+ * incrypt flag. For outgoing records, there is no such flag; "encryption"
+ * is always considered active, but initially uses functions that do not
+ * encrypt anything. The 'incrypt' flag is needed because when there is
+ * no active encryption, records larger than the I/O buffer are accepted.
+ *
+ * Note: we do not support no-encryption modes (MAC only).
+ *
+ * TODO: implement GCM support
+ *
+ *
+ * Misc:
+ * -----
+ *
+ * 'max_frag_len' is the maximum plaintext size for an outgoing record.
+ * By default, it is set to the maximum value that fits in the provided
+ * buffers, in the following list: 512, 1024, 2048, 4096, 16384. The
+ * caller may change it if needed, but the new value MUST still fit in
+ * the buffers, and it MUST be one of the list above for compatibility
+ * with the Maximum Fragment Length extension.
+ *
+ * For incoming records, only the total buffer length and current
+ * encryption mode impact the maximum length for incoming records. The
+ * 'max_frag_len' value is still adjusted so that records up to that
+ * length can be both received and sent.
+ *
+ *
+ * Offsets and lengths:
+ * --------------------
+ *
+ * When sending fragments with TLS-1.1+, the maximum overhead is:
+ * 5 bytes for the record header
+ * 16 bytes for the explicit IV
+ * 48 bytes for the MAC (HMAC/SHA-384)
+ * 16 bytes for the padding (AES)
+ * so a total of 85 extra bytes. Note that we support block cipher sizes
+ * up to 16 bytes (AES) and HMAC output sizes up to 48 bytes (SHA-384).
+ *
+ * With TLS-1.0 and CBC mode, we apply a 1/n-1 split, for a maximum
+ * overhead of:
+ * 5 bytes for the first record header
+ * 32 bytes for the first record payload (AES-CBC + HMAC/SHA-1)
+ * 5 bytes for the second record header
+ * 20 bytes for the MAC (HMAC/SHA-1)
+ * 16 bytes for the padding (AES)
+ * -1 byte to account for the payload byte in the first record
+ * so a total of 77 extra bytes at most, less than the 85 bytes above.
+ * Note that with TLS-1.0, the MAC is HMAC with either MD5 or SHA-1, but
+ * no other hash function.
+ *
+ * The implementation does not try to send larger records when the current
+ * encryption mode has less overhead.
+ *
+ * Maximum input record overhead is:
+ * 5 bytes for the record header
+ * 16 bytes for the explicit IV (TLS-1.1+)
+ * 48 bytes for the MAC (HMAC/SHA-384)
+ * 256 bytes for the padding
+ * so a total of 325 extra bytes.
+ *
+ * When receiving the next record header, it is written into the buffer
+ * bytes 0 to 4 (inclusive). Record data is always written into buf[]
+ * starting at offset 5. When encryption is active, the plaintext data
+ * may start at a larger offset (e.g. because of an explicit IV).
+ */
+
+#define MAX_OUT_OVERHEAD 85
+#define MAX_IN_OVERHEAD 325
+
+/* see inner.h */
+void
+br_ssl_engine_fail(br_ssl_engine_context *rc, int err)
+{
+ if (rc->iomode != BR_IO_FAILED) {
+ rc->iomode = BR_IO_FAILED;
+ rc->err = err;
+ }
+}
+
+/*
+ * Adjust registers for a new incoming record.
+ */
+static void
+make_ready_in(br_ssl_engine_context *rc)
+{
+ rc->ixa = rc->ixb = 0;
+ rc->ixc = 5;
+ if (rc->iomode == BR_IO_IN) {
+ rc->iomode = BR_IO_INOUT;
+ }
+}
+
+/*
+ * Adjust registers for a new outgoing record.
+ */
+static void
+make_ready_out(br_ssl_engine_context *rc)
+{
+ size_t a, b;
+
+ a = 5;
+ b = rc->obuf_len - a;
+ rc->out.vtable->max_plaintext(&rc->out.vtable, &a, &b);
+ if ((b - a) > rc->max_frag_len) {
+ b = a + rc->max_frag_len;
+ }
+ rc->oxa = a;
+ rc->oxb = b;
+ rc->oxc = a;
+ if (rc->iomode == BR_IO_OUT) {
+ rc->iomode = BR_IO_INOUT;
+ }
+}
+
+/* see inner.h */
+void
+br_ssl_engine_new_max_frag_len(br_ssl_engine_context *rc, unsigned max_frag_len)
+{
+ size_t nxb;
+
+ rc->max_frag_len = max_frag_len;
+ nxb = rc->oxc + max_frag_len;
+ if (rc->oxa < rc->oxb && rc->oxb > nxb && rc->oxa < nxb) {
+ rc->oxb = nxb;
+ }
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_buffer(br_ssl_engine_context *rc,
+ void *buf, size_t buf_len, int bidi)
+{
+ if (buf == NULL) {
+ br_ssl_engine_set_buffers_bidi(rc, NULL, 0, NULL, 0);
+ } else {
+ /*
+ * In bidirectional mode, we want to maximise input
+ * buffer size, since we support arbitrary fragmentation
+ * when sending, but the peer will not necessarily
+ * comply to any low fragment length (in particular if
+ * we are the server, because the maximum fragment
+ * length extension is under client control).
+ *
+ * We keep a minimum size of 512 bytes for the plaintext
+ * of our outgoing records.
+ *
+ * br_ssl_engine_set_buffers_bidi() will compute the maximum
+ * fragment length for outgoing records by using the minimum
+ * of allocated spaces for both input and output records,
+ * rounded down to a standard length.
+ */
+ if (bidi) {
+ size_t w;
+
+ if (buf_len < (512 + MAX_IN_OVERHEAD
+ + 512 + MAX_OUT_OVERHEAD))
+ {
+ rc->iomode = BR_IO_FAILED;
+ rc->err = BR_ERR_BAD_PARAM;
+ return;
+ } else if (buf_len < (16384 + MAX_IN_OVERHEAD
+ + 512 + MAX_OUT_OVERHEAD))
+ {
+ w = 512 + MAX_OUT_OVERHEAD;
+ } else {
+ w = buf_len - (16384 + MAX_IN_OVERHEAD);
+ }
+ br_ssl_engine_set_buffers_bidi(rc,
+ buf, buf_len - w,
+ (unsigned char *)buf + w, w);
+ } else {
+ br_ssl_engine_set_buffers_bidi(rc,
+ buf, buf_len, NULL, 0);
+ }
+ }
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_buffers_bidi(br_ssl_engine_context *rc,
+ void *ibuf, size_t ibuf_len, void *obuf, size_t obuf_len)
+{
+ rc->iomode = BR_IO_INOUT;
+ rc->incrypt = 0;
+ rc->err = BR_ERR_OK;
+ rc->version_in = 0;
+ rc->record_type_in = 0;
+ rc->version_out = 0;
+ rc->record_type_out = 0;
+ if (ibuf == NULL) {
+ if (rc->ibuf == NULL) {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_PARAM);
+ }
+ } else {
+ unsigned u;
+
+ rc->ibuf = ibuf;
+ rc->ibuf_len = ibuf_len;
+ if (obuf == NULL) {
+ obuf = ibuf;
+ obuf_len = ibuf_len;
+ }
+ rc->obuf = obuf;
+ rc->obuf_len = obuf_len;
+
+ /*
+ * Compute the maximum fragment length, that fits for
+ * both incoming and outgoing records. This length will
+ * be used in fragment length negotiation, so we must
+ * honour it both ways. Regardless, larger incoming
+ * records will be accepted, as long as they fit in the
+ * actual buffer size.
+ */
+ for (u = 14; u >= 9; u --) {
+ size_t flen;
+
+ flen = (size_t)1 << u;
+ if (obuf_len >= flen + MAX_OUT_OVERHEAD
+ && ibuf_len >= flen + MAX_IN_OVERHEAD)
+ {
+ break;
+ }
+ }
+ if (u == 8) {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_PARAM);
+ return;
+ } else if (u == 13) {
+ u = 12;
+ }
+ rc->max_frag_len = (size_t)1 << u;
+ rc->log_max_frag_len = u;
+ rc->peer_log_max_frag_len = 0;
+ }
+ rc->out.vtable = &br_sslrec_out_clear_vtable;
+ make_ready_in(rc);
+ make_ready_out(rc);
+}
+
+/*
+ * Clear buffers in both directions.
+ */
+static void
+engine_clearbuf(br_ssl_engine_context *rc)
+{
+ make_ready_in(rc);
+ make_ready_out(rc);
+}
+
+/*
+ * Make sure the internal PRNG is initialised (but not necessarily
+ * seeded properly yet).
+ */
+static int
+rng_init(br_ssl_engine_context *cc)
+{
+ const br_hash_class *h;
+
+ if (cc->rng_init_done != 0) {
+ return 1;
+ }
+
+ /*
+ * If using TLS-1.2, then SHA-256 or SHA-384 must be present (or
+ * both); we prefer SHA-256 which is faster for 32-bit systems.
+ *
+ * If using TLS-1.0 or 1.1 then SHA-1 must be present.
+ *
+ * Though HMAC_DRBG/SHA-1 is, as far as we know, as safe as
+ * these things can be, we still prefer the SHA-2 functions over
+ * SHA-1, if only for public relations (known theoretical
+ * weaknesses of SHA-1 with regards to collisions are mostly
+ * irrelevant here, but they still make people nervous).
+ */
+ h = br_multihash_getimpl(&cc->mhash, br_sha256_ID);
+ if (!h) {
+ h = br_multihash_getimpl(&cc->mhash, br_sha384_ID);
+ if (!h) {
+ h = br_multihash_getimpl(&cc->mhash,
+ br_sha1_ID);
+ if (!h) {
+ br_ssl_engine_fail(cc, BR_ERR_BAD_STATE);
+ return 0;
+ }
+ }
+ }
+ br_hmac_drbg_init(&cc->rng, h, NULL, 0);
+ cc->rng_init_done = 1;
+ return 1;
+}
+
+/* see inner.h */
+int
+br_ssl_engine_init_rand(br_ssl_engine_context *cc)
+{
+ if (!rng_init(cc)) {
+ return 0;
+ }
+
+ /*
+ * We always try OS/hardware seeding once. If it works, then
+ * we assume proper seeding. If not, then external entropy must
+ * have been injected; otherwise, we report an error.
+ */
+ if (!cc->rng_os_rand_done) {
+ br_prng_seeder sd;
+
+ sd = br_prng_seeder_system(NULL);
+ if (sd != 0 && sd(&cc->rng.vtable)) {
+ cc->rng_init_done = 2;
+ }
+ cc->rng_os_rand_done = 1;
+ }
+ if (cc->rng_init_done < 2) {
+ br_ssl_engine_fail(cc, BR_ERR_NO_RANDOM);
+ return 0;
+ }
+ return 1;
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_inject_entropy(br_ssl_engine_context *cc,
+ const void *data, size_t len)
+{
+ /*
+ * Externally provided entropy is assumed to be "good enough"
+ * (we cannot really test its quality) so if the RNG structure
+ * could be initialised at all, then we marked the RNG as
+ * "properly seeded".
+ */
+ if (!rng_init(cc)) {
+ return;
+ }
+ br_hmac_drbg_update(&cc->rng, data, len);
+ cc->rng_init_done = 2;
+}
+
+/*
+ * We define a few internal functions that implement the low-level engine
+ * API for I/O; the external API (br_ssl_engine_sendapp_buf() and similar
+ * functions) is built upon these function, with special processing for
+ * records which are not of type "application data".
+ *
+ * recvrec_buf, recvrec_ack receives bytes from transport medium
+ * sendrec_buf, sendrec_ack send bytes to transport medium
+ * recvpld_buf, recvpld_ack receives payload data from engine
+ * sendpld_buf, sendpld_ack send payload data to engine
+ */
+
+static unsigned char *
+recvrec_buf(const br_ssl_engine_context *rc, size_t *len)
+{
+ if (rc->shutdown_recv) {
+ *len = 0;
+ return NULL;
+ }
+
+ /*
+ * Bytes from the transport can be injected only if the mode is
+ * compatible (in or in/out), and ixa == ixb; ixc then contains
+ * the number of bytes that are still expected (but it may
+ * exceed our buffer size).
+ *
+ * We cannot get "stuck" here (buffer is full, but still more
+ * data is expected) because oversized records are detected when
+ * their header is processed.
+ */
+ switch (rc->iomode) {
+ case BR_IO_IN:
+ case BR_IO_INOUT:
+ if (rc->ixa == rc->ixb) {
+ size_t z;
+
+ z = rc->ixc;
+ if (z > rc->ibuf_len - rc->ixa) {
+ z = rc->ibuf_len - rc->ixa;
+ }
+ *len = z;
+ return rc->ibuf + rc->ixa;
+ }
+ break;
+ }
+ *len = 0;
+ return NULL;
+}
+
+static void
+recvrec_ack(br_ssl_engine_context *rc, size_t len)
+{
+ unsigned char *pbuf;
+ size_t pbuf_len;
+
+ /*
+ * Adjust state if necessary (for a shared input/output buffer):
+ * we got some incoming bytes, so we cannot (temporarily) handle
+ * outgoing data.
+ */
+ if (rc->iomode == BR_IO_INOUT && rc->ibuf == rc->obuf) {
+ rc->iomode = BR_IO_IN;
+ }
+
+ /*
+ * Adjust data pointers.
+ */
+ rc->ixb = (rc->ixa += len);
+ rc->ixc -= len;
+
+ /*
+ * If we are receiving a header and did not fully obtained it
+ * yet, then just wait for the next bytes.
+ */
+ if (rc->ixa < 5) {
+ return;
+ }
+
+ /*
+ * If we just obtained a full header, process it.
+ */
+ if (rc->ixa == 5) {
+ unsigned version;
+ unsigned rlen;
+
+ /*
+ * Get record type and version. We support only versions
+ * 3.x (if the version major number does not match, then
+ * we suppose that the record format is too alien for us
+ * to process it).
+ *
+ * Note: right now, we reject clients that try to send
+ * a ClientHello in a format compatible with SSL-2.0. It
+ * is unclear whether this will ever be supported; and
+ * if we want to support it, then this might be done in
+ * in the server-specific code, not here.
+ */
+ rc->record_type_in = rc->ibuf[0];
+ version = br_dec16be(rc->ibuf + 1);
+ if ((version >> 8) != 3) {
+ br_ssl_engine_fail(rc, BR_ERR_UNSUPPORTED_VERSION);
+ return;
+ }
+
+ /*
+ * We ensure that successive records have the same
+ * version. The handshake code must check and adjust the
+ * variables when necessary to accommodate the protocol
+ * negotiation details.
+ */
+ if (rc->version_in != 0 && rc->version_in != version) {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_VERSION);
+ return;
+ }
+ rc->version_in = version;
+
+ /*
+ * Decode record length. We must check that the length
+ * is valid (relatively to the current encryption mode)
+ * and also (if encryption is active) that the record
+ * will fit in our buffer.
+ *
+ * When no encryption is active, we can process records
+ * by chunks, and thus accept any record up to the
+ * maximum allowed plaintext length (16384 bytes).
+ */
+ rlen = br_dec16be(rc->ibuf + 3);
+ if (rc->incrypt) {
+ if (!rc->in.vtable->check_length(
+ &rc->in.vtable, rlen))
+ {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_LENGTH);
+ return;
+ }
+ if (rlen > (rc->ibuf_len - 5)) {
+ br_ssl_engine_fail(rc, BR_ERR_TOO_LARGE);
+ return;
+ }
+ } else {
+ if (rlen > 16384) {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_LENGTH);
+ return;
+ }
+ }
+
+ /*
+ * If the record is completely empty then we must switch
+ * to a new record. Note that, in that case, we
+ * completely ignore the record type, which is fitting
+ * since we received no actual data of that type.
+ *
+ * A completely empty record is technically allowed as
+ * long as encryption/MAC is not active, i.e. before
+ * completion of the first handshake. It it still weird;
+ * it might conceptually be useful as a heartbeat or
+ * keep-alive mechanism while some lengthy operation is
+ * going on, e.g. interaction with a human user.
+ */
+ if (rlen == 0) {
+ make_ready_in(rc);
+ } else {
+ rc->ixa = rc->ixb = 5;
+ rc->ixc = rlen;
+ }
+ return;
+ }
+
+ /*
+ * If there is no active encryption, then the data can be read
+ * right away. Note that we do not receive bytes from the
+ * transport medium when we still have payload bytes to be
+ * acknowledged.
+ */
+ if (!rc->incrypt) {
+ rc->ixa = 5;
+ return;
+ }
+
+ /*
+ * Since encryption is active, we must wait for a full record
+ * before processing it.
+ */
+ if (rc->ixc != 0) {
+ return;
+ }
+
+ /*
+ * We got the full record. Decrypt it.
+ */
+ pbuf_len = rc->ixa - 5;
+ pbuf = rc->in.vtable->decrypt(&rc->in.vtable,
+ rc->record_type_in, rc->version_in, rc->ibuf + 5, &pbuf_len);
+ if (pbuf == 0) {
+ br_ssl_engine_fail(rc, BR_ERR_BAD_MAC);
+ return;
+ }
+ rc->ixa = (size_t)(pbuf - rc->ibuf);
+ rc->ixb = rc->ixa + pbuf_len;
+
+ /*
+ * Decryption may have yielded an empty record, in which case
+ * we get back to "ready" state immediately.
+ */
+ if (rc->ixa == rc->ixb) {
+ make_ready_in(rc);
+ }
+}
+
+/* see inner.h */
+int
+br_ssl_engine_recvrec_finished(const br_ssl_engine_context *rc)
+{
+ switch (rc->iomode) {
+ case BR_IO_IN:
+ case BR_IO_INOUT:
+ return rc->ixc == 0 || rc->ixa < 5;
+ default:
+ return 1;
+ }
+}
+
+static unsigned char *
+recvpld_buf(const br_ssl_engine_context *rc, size_t *len)
+{
+ /*
+ * There is payload data to be read only if the mode is
+ * compatible, and ixa != ixb.
+ */
+ switch (rc->iomode) {
+ case BR_IO_IN:
+ case BR_IO_INOUT:
+ *len = rc->ixb - rc->ixa;
+ return (*len == 0) ? NULL : (rc->ibuf + rc->ixa);
+ default:
+ *len = 0;
+ return NULL;
+ }
+}
+
+static void
+recvpld_ack(br_ssl_engine_context *rc, size_t len)
+{
+ rc->ixa += len;
+
+ /*
+ * If we read all the available data, then we either expect
+ * the remainder of the current record (if the current record
+ * was not finished; this may happen when encryption is not
+ * active), or go to "ready" state.
+ */
+ if (rc->ixa == rc->ixb) {
+ if (rc->ixc == 0) {
+ make_ready_in(rc);
+ } else {
+ rc->ixa = rc->ixb = 5;
+ }
+ }
+}
+
+static unsigned char *
+sendpld_buf(const br_ssl_engine_context *rc, size_t *len)
+{
+ /*
+ * Payload data can be injected only if the current mode is
+ * compatible, and oxa != oxb.
+ */
+ switch (rc->iomode) {
+ case BR_IO_OUT:
+ case BR_IO_INOUT:
+ *len = rc->oxb - rc->oxa;
+ return (*len == 0) ? NULL : (rc->obuf + rc->oxa);
+ default:
+ *len = 0;
+ return NULL;
+ }
+}
+
+/*
+ * If some payload bytes have been accumulated, then wrap them into
+ * an outgoing record. Otherwise, this function does nothing, unless
+ * 'force' is non-zero, in which case an empty record is assembled.
+ *
+ * The caller must take care not to invoke this function if the engine
+ * is not currently ready to receive payload bytes to send.
+ */
+static void
+sendpld_flush(br_ssl_engine_context *rc, int force)
+{
+ size_t xlen;
+ unsigned char *buf;
+
+ if (rc->oxa == rc->oxb) {
+ return;
+ }
+ xlen = rc->oxa - rc->oxc;
+ if (xlen == 0 && !force) {
+ return;
+ }
+ buf = rc->out.vtable->encrypt(&rc->out.vtable,
+ rc->record_type_out, rc->version_out,
+ rc->obuf + rc->oxc, &xlen);
+ rc->oxb = rc->oxa = (size_t)(buf - rc->obuf);
+ rc->oxc = rc->oxa + xlen;
+}
+
+static void
+sendpld_ack(br_ssl_engine_context *rc, size_t len)
+{
+ /*
+ * If using a shared buffer, then we may have to modify the
+ * current mode.
+ */
+ if (rc->iomode == BR_IO_INOUT && rc->ibuf == rc->obuf) {
+ rc->iomode = BR_IO_OUT;
+ }
+ rc->oxa += len;
+ if (rc->oxa >= rc->oxb) {
+ /*
+ * Set oxb to one more than oxa so that sendpld_flush()
+ * does not mistakingly believe that a record is
+ * already prepared and being sent.
+ */
+ rc->oxb = rc->oxa + 1;
+ sendpld_flush(rc, 0);
+ }
+}
+
+static unsigned char *
+sendrec_buf(const br_ssl_engine_context *rc, size_t *len)
+{
+ /*
+ * When still gathering payload bytes, oxc points to the start
+ * of the record data, so oxc <= oxa. However, when a full
+ * record has been completed, oxc points to the end of the record,
+ * so oxc > oxa.
+ */
+ switch (rc->iomode) {
+ case BR_IO_OUT:
+ case BR_IO_INOUT:
+ if (rc->oxc > rc->oxa) {
+ *len = rc->oxc - rc->oxa;
+ return rc->obuf + rc->oxa;
+ }
+ break;
+ }
+ *len = 0;
+ return NULL;
+}
+
+static void
+sendrec_ack(br_ssl_engine_context *rc, size_t len)
+{
+ rc->oxb = (rc->oxa += len);
+ if (rc->oxa == rc->oxc) {
+ make_ready_out(rc);
+ }
+}
+
+/*
+ * Test whether there is some buffered outgoing record that still must
+ * sent.
+ */
+static inline int
+has_rec_tosend(const br_ssl_engine_context *rc)
+{
+ return rc->oxa == rc->oxb && rc->oxa != rc->oxc;
+}
+
+/*
+ * The "no encryption" mode has no overhead. It limits the payload size
+ * to the maximum size allowed by the standard (16384 bytes); the caller
+ * is responsible for possibly enforcing a smaller fragment length.
+ */
+static void
+clear_max_plaintext(const br_sslrec_out_clear_context *cc,
+ size_t *start, size_t *end)
+{
+ size_t len;
+
+ (void)cc;
+ len = *end - *start;
+ if (len > 16384) {
+ *end = *start + 16384;
+ }
+}
+
+/*
+ * In "no encryption" mode, encryption is trivial (a no-operation) so
+ * we just have to encode the header.
+ */
+static unsigned char *
+clear_encrypt(br_sslrec_out_clear_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf;
+
+ (void)cc;
+ buf = (unsigned char *)data - 5;
+ buf[0] = record_type;
+ br_enc16be(buf + 1, version);
+ br_enc16be(buf + 3, *data_len);
+ *data_len += 5;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_out_class br_sslrec_out_clear_vtable = {
+ sizeof(br_sslrec_out_clear_context),
+ (void (*)(const br_sslrec_out_class *const *, size_t *, size_t *))
+ &clear_max_plaintext,
+ (unsigned char *(*)(const br_sslrec_out_class **,
+ int, unsigned, void *, size_t *))
+ &clear_encrypt
+};
+
+/* ==================================================================== */
+/*
+ * In this part of the file, we handle the various record types, and
+ * communications with the handshake processor.
+ */
+
+/*
+ * IMPLEMENTATION NOTES
+ * ====================
+ *
+ * The handshake processor is written in T0 and runs as a coroutine.
+ * It receives the contents of all records except application data, and
+ * is responsible for producing the contents of all records except
+ * application data.
+ *
+ * A state flag is maintained, which specifies whether application data
+ * is acceptable or not. When it is set:
+ *
+ * -- Application data can be injected as payload data (provided that
+ * the output buffer is ready for that).
+ *
+ * -- Incoming application data records are accepted, and yield data
+ * that the caller may retrieve.
+ *
+ * When the flag is cleared, application data is not accepted from the
+ * application, and incoming application data records trigger an error.
+ *
+ *
+ * Records of type handshake, alert or change-cipher-spec are handled
+ * by the handshake processor. The handshake processor is written in T0
+ * and runs as a coroutine; it gets invoked whenever one of the following
+ * situations is reached:
+ *
+ * -- An incoming record has type handshake, alert or change-cipher-spec,
+ * and yields data that can be read (zero-length records are thus
+ * ignored).
+ *
+ * -- An outgoing record has just finished being sent, and the "application
+ * data" flag is cleared.
+ *
+ * -- The caller wishes to perform a close (call to br_ssl_engine_close()).
+ *
+ * -- The caller wishes to perform a renegotiation (call to
+ * br_ssl_engine_renegotiate()).
+ *
+ * Whenever the handshake processor is entered, access to the payload
+ * buffers is provided, along with some information about explicit
+ * closures or renegotiations.
+ */
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_suites(br_ssl_engine_context *cc,
+ const uint16_t *suites, size_t suites_num)
+{
+ if ((suites_num * sizeof *suites) > sizeof cc->suites_buf) {
+ br_ssl_engine_fail(cc, BR_ERR_BAD_PARAM);
+ return;
+ }
+ memcpy(cc->suites_buf, suites, suites_num * sizeof *suites);
+ cc->suites_num = suites_num;
+}
+
+/*
+ * Give control to handshake processor. 'action' is 1 for a close,
+ * 2 for a renegotiation, or 0 for a jump due to I/O completion.
+ */
+static void
+jump_handshake(br_ssl_engine_context *cc, int action)
+{
+ /*
+ * We use a loop because the handshake processor actions may
+ * allow for more actions; namely, if the processor reads all
+ * input data, then it may allow for output data to be produced,
+ * in case of a shared in/out buffer.
+ */
+ for (;;) {
+ size_t hlen_in, hlen_out;
+
+ /*
+ * Get input buffer. We do not want to provide
+ * application data to the handshake processor (we could
+ * get called with an explicit close or renegotiation
+ * while there is application data ready to be read).
+ */
+ cc->hbuf_in = recvpld_buf(cc, &hlen_in);
+ if (cc->hbuf_in != NULL
+ && cc->record_type_in == BR_SSL_APPLICATION_DATA)
+ {
+ hlen_in = 0;
+ }
+
+ /*
+ * Get output buffer. The handshake processor never
+ * leaves an unfinished outgoing record, so if there is
+ * buffered output, then it MUST be some application
+ * data, so the processor cannot write to it.
+ */
+ cc->saved_hbuf_out = cc->hbuf_out = sendpld_buf(cc, &hlen_out);
+ if (cc->hbuf_out != NULL && br_ssl_engine_has_pld_to_send(cc)) {
+ hlen_out = 0;
+ }
+
+ /*
+ * Note: hlen_in and hlen_out can be both non-zero only if
+ * the input and output buffers are disjoint. Thus, we can
+ * offer both buffers to the handshake code.
+ */
+
+ cc->hlen_in = hlen_in;
+ cc->hlen_out = hlen_out;
+ cc->action = action;
+ cc->hsrun(&cc->cpu);
+ if (br_ssl_engine_closed(cc)) {
+ return;
+ }
+ if (cc->hbuf_out != cc->saved_hbuf_out) {
+ sendpld_ack(cc, cc->hbuf_out - cc->saved_hbuf_out);
+ }
+ if (hlen_in != cc->hlen_in) {
+ recvpld_ack(cc, hlen_in - cc->hlen_in);
+ if (cc->hlen_in == 0) {
+ /*
+ * We read all data bytes, which may have
+ * released the output buffer in case it
+ * is shared with the input buffer, and
+ * the handshake code might be waiting for
+ * that.
+ */
+ action = 0;
+ continue;
+ }
+ }
+ break;
+ }
+}
+
+/* see inner.h */
+void
+br_ssl_engine_flush_record(br_ssl_engine_context *cc)
+{
+ if (cc->hbuf_out != cc->saved_hbuf_out) {
+ sendpld_ack(cc, cc->hbuf_out - cc->saved_hbuf_out);
+ }
+ if (br_ssl_engine_has_pld_to_send(cc)) {
+ sendpld_flush(cc, 0);
+ }
+ cc->saved_hbuf_out = cc->hbuf_out = sendpld_buf(cc, &cc->hlen_out);
+}
+
+/* see bearssl_ssl.h */
+unsigned char *
+br_ssl_engine_sendapp_buf(const br_ssl_engine_context *cc, size_t *len)
+{
+ if (!(cc->application_data & 1)) {
+ *len = 0;
+ return NULL;
+ }
+ return sendpld_buf(cc, len);
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_sendapp_ack(br_ssl_engine_context *cc, size_t len)
+{
+ sendpld_ack(cc, len);
+}
+
+/* see bearssl_ssl.h */
+unsigned char *
+br_ssl_engine_recvapp_buf(const br_ssl_engine_context *cc, size_t *len)
+{
+ if (!(cc->application_data & 1)
+ || cc->record_type_in != BR_SSL_APPLICATION_DATA)
+ {
+ *len = 0;
+ return NULL;
+ }
+ return recvpld_buf(cc, len);
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_recvapp_ack(br_ssl_engine_context *cc, size_t len)
+{
+ recvpld_ack(cc, len);
+}
+
+/* see bearssl_ssl.h */
+unsigned char *
+br_ssl_engine_sendrec_buf(const br_ssl_engine_context *cc, size_t *len)
+{
+ return sendrec_buf(cc, len);
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_sendrec_ack(br_ssl_engine_context *cc, size_t len)
+{
+ sendrec_ack(cc, len);
+ if (len != 0 && !has_rec_tosend(cc)
+ && (cc->record_type_out != BR_SSL_APPLICATION_DATA
+ || (cc->application_data & 1) == 0))
+ {
+ jump_handshake(cc, 0);
+ }
+}
+
+/* see bearssl_ssl.h */
+unsigned char *
+br_ssl_engine_recvrec_buf(const br_ssl_engine_context *cc, size_t *len)
+{
+ return recvrec_buf(cc, len);
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_recvrec_ack(br_ssl_engine_context *cc, size_t len)
+{
+ unsigned char *buf;
+
+ recvrec_ack(cc, len);
+ if (br_ssl_engine_closed(cc)) {
+ return;
+ }
+
+ /*
+ * We just received some bytes from the peer. This may have
+ * yielded some payload bytes, in which case we must process
+ * them according to the record type.
+ */
+ buf = recvpld_buf(cc, &len);
+ if (buf != NULL) {
+ switch (cc->record_type_in) {
+ case BR_SSL_CHANGE_CIPHER_SPEC:
+ case BR_SSL_ALERT:
+ case BR_SSL_HANDSHAKE:
+ jump_handshake(cc, 0);
+ break;
+ case BR_SSL_APPLICATION_DATA:
+ if (cc->application_data == 1) {
+ break;
+ }
+
+ /*
+ * If we are currently closing, and waiting for
+ * a close_notify from the peer, then incoming
+ * application data should be discarded.
+ */
+ if (cc->application_data == 2) {
+ recvpld_ack(cc, len);
+ break;
+ }
+
+ /* Fall through */
+ default:
+ br_ssl_engine_fail(cc, BR_ERR_UNEXPECTED);
+ break;
+ }
+ }
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_close(br_ssl_engine_context *cc)
+{
+ if (!br_ssl_engine_closed(cc)) {
+ jump_handshake(cc, 1);
+ }
+}
+
+/* see bearssl_ssl.h */
+int
+br_ssl_engine_renegotiate(br_ssl_engine_context *cc)
+{
+ size_t len;
+
+ if (br_ssl_engine_closed(cc) || cc->reneg == 1
+ || (cc->flags & BR_OPT_NO_RENEGOTIATION) != 0
+ || br_ssl_engine_recvapp_buf(cc, &len) != NULL)
+ {
+ return 0;
+ }
+ jump_handshake(cc, 2);
+ return 1;
+}
+
+/* see bearssl.h */
+unsigned
+br_ssl_engine_current_state(const br_ssl_engine_context *cc)
+{
+ unsigned s;
+ size_t len;
+
+ if (br_ssl_engine_closed(cc)) {
+ return BR_SSL_CLOSED;
+ }
+
+ s = 0;
+ if (br_ssl_engine_sendrec_buf(cc, &len) != NULL) {
+ s |= BR_SSL_SENDREC;
+ }
+ if (br_ssl_engine_recvrec_buf(cc, &len) != NULL) {
+ s |= BR_SSL_RECVREC;
+ }
+ if (br_ssl_engine_sendapp_buf(cc, &len) != NULL) {
+ s |= BR_SSL_SENDAPP;
+ }
+ if (br_ssl_engine_recvapp_buf(cc, &len) != NULL) {
+ s |= BR_SSL_RECVAPP;
+ }
+ return s;
+}
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_flush(br_ssl_engine_context *cc, int force)
+{
+ if (!br_ssl_engine_closed(cc) && (cc->application_data & 1) != 0) {
+ sendpld_flush(cc, force);
+ }
+}
+
+/* see inner.h */
+void
+br_ssl_engine_hs_reset(br_ssl_engine_context *cc,
+ void (*hsinit)(void *), void (*hsrun)(void *))
+{
+ engine_clearbuf(cc);
+ cc->cpu.dp = cc->dp_stack;
+ cc->cpu.rp = cc->rp_stack;
+ hsinit(&cc->cpu);
+ cc->hsrun = hsrun;
+ cc->shutdown_recv = 0;
+ cc->application_data = 0;
+ cc->alert = 0;
+ jump_handshake(cc, 0);
+}
+
+/* see inner.h */
+br_tls_prf_impl
+br_ssl_engine_get_PRF(br_ssl_engine_context *cc, int prf_id)
+{
+ if (cc->session.version >= BR_TLS12) {
+ if (prf_id == br_sha384_ID) {
+ return cc->prf_sha384;
+ } else {
+ return cc->prf_sha256;
+ }
+ } else {
+ return cc->prf10;
+ }
+}
+
+/* see inner.h */
+void
+br_ssl_engine_compute_master(br_ssl_engine_context *cc,
+ int prf_id, const void *pms, size_t pms_len)
+{
+ br_tls_prf_impl iprf;
+ br_tls_prf_seed_chunk seed[2] = {
+ { cc->client_random, sizeof cc->client_random },
+ { cc->server_random, sizeof cc->server_random }
+ };
+
+ iprf = br_ssl_engine_get_PRF(cc, prf_id);
+ iprf(cc->session.master_secret, sizeof cc->session.master_secret,
+ pms, pms_len, "master secret", 2, seed);
+}
+
+/*
+ * Compute key block.
+ */
+static void
+compute_key_block(br_ssl_engine_context *cc, int prf_id,
+ size_t half_len, unsigned char *kb)
+{
+ br_tls_prf_impl iprf;
+ br_tls_prf_seed_chunk seed[2] = {
+ { cc->server_random, sizeof cc->server_random },
+ { cc->client_random, sizeof cc->client_random }
+ };
+
+ iprf = br_ssl_engine_get_PRF(cc, prf_id);
+ iprf(kb, half_len << 1,
+ cc->session.master_secret, sizeof cc->session.master_secret,
+ "key expansion", 2, seed);
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_cbc_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id, int mac_id,
+ const br_block_cbcdec_class *bc_impl, size_t cipher_key_len)
+{
+ unsigned char kb[192];
+ unsigned char *cipher_key, *mac_key, *iv;
+ const br_hash_class *imh;
+ size_t mac_key_len, mac_out_len, iv_len;
+
+ imh = br_ssl_engine_get_hash(cc, mac_id);
+ mac_out_len = (imh->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+ mac_key_len = mac_out_len;
+
+ /*
+ * TLS 1.1+ uses per-record explicit IV, so no IV to generate here.
+ */
+ if (cc->session.version >= BR_TLS11) {
+ iv_len = 0;
+ } else {
+ iv_len = bc_impl->block_size;
+ }
+ compute_key_block(cc, prf_id,
+ mac_key_len + cipher_key_len + iv_len, kb);
+ if (is_client) {
+ mac_key = &kb[mac_key_len];
+ cipher_key = &kb[(mac_key_len << 1) + cipher_key_len];
+ iv = &kb[((mac_key_len + cipher_key_len) << 1) + iv_len];
+ } else {
+ mac_key = &kb[0];
+ cipher_key = &kb[mac_key_len << 1];
+ iv = &kb[(mac_key_len + cipher_key_len) << 1];
+ }
+ if (iv_len == 0) {
+ iv = NULL;
+ }
+ cc->icbc_in->init(&cc->in.cbc.vtable,
+ bc_impl, cipher_key, cipher_key_len,
+ imh, mac_key, mac_key_len, mac_out_len, iv);
+ cc->incrypt = 1;
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_cbc_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id, int mac_id,
+ const br_block_cbcenc_class *bc_impl, size_t cipher_key_len)
+{
+ unsigned char kb[192];
+ unsigned char *cipher_key, *mac_key, *iv;
+ const br_hash_class *imh;
+ size_t mac_key_len, mac_out_len, iv_len;
+
+ imh = br_ssl_engine_get_hash(cc, mac_id);
+ mac_out_len = (imh->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+ mac_key_len = mac_out_len;
+
+ /*
+ * TLS 1.1+ uses per-record explicit IV, so no IV to generate here.
+ */
+ if (cc->session.version >= BR_TLS11) {
+ iv_len = 0;
+ } else {
+ iv_len = bc_impl->block_size;
+ }
+ compute_key_block(cc, prf_id,
+ mac_key_len + cipher_key_len + iv_len, kb);
+ if (is_client) {
+ mac_key = &kb[0];
+ cipher_key = &kb[mac_key_len << 1];
+ iv = &kb[(mac_key_len + cipher_key_len) << 1];
+ } else {
+ mac_key = &kb[mac_key_len];
+ cipher_key = &kb[(mac_key_len << 1) + cipher_key_len];
+ iv = &kb[((mac_key_len + cipher_key_len) << 1) + iv_len];
+ }
+ if (iv_len == 0) {
+ iv = NULL;
+ }
+ cc->icbc_out->init(&cc->out.cbc.vtable,
+ bc_impl, cipher_key, cipher_key_len,
+ imh, mac_key, mac_key_len, mac_out_len, iv);
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_gcm_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctr_class *bc_impl, size_t cipher_key_len)
+{
+ unsigned char kb[72];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, cipher_key_len + 4, kb);
+ if (is_client) {
+ cipher_key = &kb[cipher_key_len];
+ iv = &kb[(cipher_key_len << 1) + 4];
+ } else {
+ cipher_key = &kb[0];
+ iv = &kb[cipher_key_len << 1];
+ }
+ cc->igcm_in->init(&cc->in.gcm.vtable.in,
+ bc_impl, cipher_key, cipher_key_len, cc->ighash, iv);
+ cc->incrypt = 1;
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_gcm_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctr_class *bc_impl, size_t cipher_key_len)
+{
+ unsigned char kb[72];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, cipher_key_len + 4, kb);
+ if (is_client) {
+ cipher_key = &kb[0];
+ iv = &kb[cipher_key_len << 1];
+ } else {
+ cipher_key = &kb[cipher_key_len];
+ iv = &kb[(cipher_key_len << 1) + 4];
+ }
+ cc->igcm_out->init(&cc->out.gcm.vtable.out,
+ bc_impl, cipher_key, cipher_key_len, cc->ighash, iv);
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_chapol_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id)
+{
+ unsigned char kb[88];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, 44, kb);
+ if (is_client) {
+ cipher_key = &kb[32];
+ iv = &kb[76];
+ } else {
+ cipher_key = &kb[0];
+ iv = &kb[64];
+ }
+ cc->ichapol_in->init(&cc->in.chapol.vtable.in,
+ cc->ichacha, cc->ipoly, cipher_key, iv);
+ cc->incrypt = 1;
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_chapol_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id)
+{
+ unsigned char kb[88];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, 44, kb);
+ if (is_client) {
+ cipher_key = &kb[0];
+ iv = &kb[64];
+ } else {
+ cipher_key = &kb[32];
+ iv = &kb[76];
+ }
+ cc->ichapol_out->init(&cc->out.chapol.vtable.out,
+ cc->ichacha, cc->ipoly, cipher_key, iv);
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_ccm_in(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctrcbc_class *bc_impl,
+ size_t cipher_key_len, size_t tag_len)
+{
+ unsigned char kb[72];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, cipher_key_len + 4, kb);
+ if (is_client) {
+ cipher_key = &kb[cipher_key_len];
+ iv = &kb[(cipher_key_len << 1) + 4];
+ } else {
+ cipher_key = &kb[0];
+ iv = &kb[cipher_key_len << 1];
+ }
+ cc->iccm_in->init(&cc->in.ccm.vtable.in,
+ bc_impl, cipher_key, cipher_key_len, iv, tag_len);
+ cc->incrypt = 1;
+}
+
+/* see inner.h */
+void
+br_ssl_engine_switch_ccm_out(br_ssl_engine_context *cc,
+ int is_client, int prf_id,
+ const br_block_ctrcbc_class *bc_impl,
+ size_t cipher_key_len, size_t tag_len)
+{
+ unsigned char kb[72];
+ unsigned char *cipher_key, *iv;
+
+ compute_key_block(cc, prf_id, cipher_key_len + 4, kb);
+ if (is_client) {
+ cipher_key = &kb[0];
+ iv = &kb[cipher_key_len << 1];
+ } else {
+ cipher_key = &kb[cipher_key_len];
+ iv = &kb[(cipher_key_len << 1) + 4];
+ }
+ cc->iccm_out->init(&cc->out.ccm.vtable.out,
+ bc_impl, cipher_key, cipher_key_len, iv, tag_len);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aescbc.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aescbc.c
new file mode 100644
index 00000000..8c5cdb57
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aescbc.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context *cc)
+{
+#if BR_AES_X86NI || BR_POWER8
+ const br_block_cbcenc_class *ienc;
+ const br_block_cbcdec_class *idec;
+#endif
+
+ br_ssl_engine_set_cbc(cc,
+ &br_sslrec_in_cbc_vtable,
+ &br_sslrec_out_cbc_vtable);
+#if BR_AES_X86NI
+ ienc = br_aes_x86ni_cbcenc_get_vtable();
+ idec = br_aes_x86ni_cbcdec_get_vtable();
+ if (ienc != NULL && idec != NULL) {
+ br_ssl_engine_set_aes_cbc(cc, ienc, idec);
+ return;
+ }
+#endif
+#if BR_POWER8
+ ienc = br_aes_pwr8_cbcenc_get_vtable();
+ idec = br_aes_pwr8_cbcdec_get_vtable();
+ if (ienc != NULL && idec != NULL) {
+ br_ssl_engine_set_aes_cbc(cc, ienc, idec);
+ return;
+ }
+#endif
+#if BR_64
+ br_ssl_engine_set_aes_cbc(cc,
+ &br_aes_ct64_cbcenc_vtable,
+ &br_aes_ct64_cbcdec_vtable);
+#else
+ br_ssl_engine_set_aes_cbc(cc,
+ &br_aes_ct_cbcenc_vtable,
+ &br_aes_ct_cbcdec_vtable);
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesccm.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesccm.c
new file mode 100644
index 00000000..15c0a78f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesccm.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_aes_ccm(br_ssl_engine_context *cc)
+{
+#if BR_AES_X86NI || BR_POWER8
+ const br_block_ctrcbc_class *ictrcbc;
+#endif
+
+ br_ssl_engine_set_ccm(cc,
+ &br_sslrec_in_ccm_vtable,
+ &br_sslrec_out_ccm_vtable);
+#if BR_AES_X86NI
+ ictrcbc = br_aes_x86ni_ctrcbc_get_vtable();
+ if (ictrcbc != NULL) {
+ br_ssl_engine_set_aes_ctrcbc(cc, ictrcbc);
+ } else {
+#if BR_64
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct64_ctrcbc_vtable);
+#else
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct_ctrcbc_vtable);
+#endif
+ }
+#elif BR_POWER8
+ ictrcbc = br_aes_pwr8_ctrcbc_get_vtable();
+ if (ictrcbc != NULL) {
+ br_ssl_engine_set_aes_ctrcbc(cc, ictrcbc);
+ } else {
+#if BR_64
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct64_ctrcbc_vtable);
+#else
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct_ctrcbc_vtable);
+#endif
+ }
+#else
+#if BR_64
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct64_ctrcbc_vtable);
+#else
+ br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct_ctrcbc_vtable);
+#endif
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesgcm.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesgcm.c
new file mode 100644
index 00000000..c44a7074
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_aesgcm.c
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context *cc)
+{
+#if BR_AES_X86NI || BR_POWER8
+ const br_block_ctr_class *ictr;
+ br_ghash ighash;
+#endif
+
+ br_ssl_engine_set_gcm(cc,
+ &br_sslrec_in_gcm_vtable,
+ &br_sslrec_out_gcm_vtable);
+#if BR_AES_X86NI
+ ictr = br_aes_x86ni_ctr_get_vtable();
+ if (ictr != NULL) {
+ br_ssl_engine_set_aes_ctr(cc, ictr);
+ } else {
+#if BR_64
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct64_ctr_vtable);
+#else
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct_ctr_vtable);
+#endif
+ }
+#elif BR_POWER8
+ ictr = br_aes_pwr8_ctr_get_vtable();
+ if (ictr != NULL) {
+ br_ssl_engine_set_aes_ctr(cc, ictr);
+ } else {
+#if BR_64
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct64_ctr_vtable);
+#else
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct_ctr_vtable);
+#endif
+ }
+#else
+#if BR_64
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct64_ctr_vtable);
+#else
+ br_ssl_engine_set_aes_ctr(cc, &br_aes_ct_ctr_vtable);
+#endif
+#endif
+#if BR_AES_X86NI
+ ighash = br_ghash_pclmul_get();
+ if (ighash != 0) {
+ br_ssl_engine_set_ghash(cc, ighash);
+ return;
+ }
+#endif
+#if BR_POWER8
+ ighash = br_ghash_pwr8_get();
+ if (ighash != 0) {
+ br_ssl_engine_set_ghash(cc, ighash);
+ return;
+ }
+#endif
+#if BR_LOMUL
+ br_ssl_engine_set_ghash(cc, &br_ghash_ctmul32);
+#elif BR_64
+ br_ssl_engine_set_ghash(cc, &br_ghash_ctmul64);
+#else
+ br_ssl_engine_set_ghash(cc, &br_ghash_ctmul);
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_chapol.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_chapol.c
new file mode 100644
index 00000000..47a0c984
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_chapol.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_chapol(br_ssl_engine_context *cc)
+{
+#if BR_INT128 || BR_UMUL128
+ br_poly1305_run bp;
+#endif
+#if BR_SSE2
+ br_chacha20_run bc;
+#endif
+
+ br_ssl_engine_set_chapol(cc,
+ &br_sslrec_in_chapol_vtable,
+ &br_sslrec_out_chapol_vtable);
+#if BR_SSE2
+ bc = br_chacha20_sse2_get();
+ if (bc) {
+ br_ssl_engine_set_chacha20(cc, bc);
+ } else {
+#endif
+ br_ssl_engine_set_chacha20(cc, &br_chacha20_ct_run);
+#if BR_SSE2
+ }
+#endif
+#if BR_INT128 || BR_UMUL128
+ bp = br_poly1305_ctmulq_get();
+ if (bp) {
+ br_ssl_engine_set_poly1305(cc, bp);
+ } else {
+#endif
+#if BR_LOMUL
+ br_ssl_engine_set_poly1305(cc, &br_poly1305_ctmul32_run);
+#else
+ br_ssl_engine_set_poly1305(cc, &br_poly1305_ctmul_run);
+#endif
+#if BR_INT128 || BR_UMUL128
+ }
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_descbc.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_descbc.c
new file mode 100644
index 00000000..0253cb2f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_descbc.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_des_cbc(br_ssl_engine_context *cc)
+{
+ br_ssl_engine_set_cbc(cc,
+ &br_sslrec_in_cbc_vtable,
+ &br_sslrec_out_cbc_vtable);
+ br_ssl_engine_set_des_cbc(cc,
+ &br_des_ct_cbcenc_vtable,
+ &br_des_ct_cbcdec_vtable);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ec.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ec.c
new file mode 100644
index 00000000..0213ae63
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ec.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_ec(br_ssl_engine_context *cc)
+{
+#if BR_LOMUL
+ br_ssl_engine_set_ec(cc, &br_ec_all_m15);
+#else
+ br_ssl_engine_set_ec(cc, &br_ec_all_m31);
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ecdsa.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ecdsa.c
new file mode 100644
index 00000000..13040025
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_ecdsa.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_ecdsa(br_ssl_engine_context *cc)
+{
+#if BR_LOMUL
+ br_ssl_engine_set_ec(cc, &br_ec_all_m15);
+ br_ssl_engine_set_ecdsa(cc, &br_ecdsa_i15_vrfy_asn1);
+#else
+ br_ssl_engine_set_ec(cc, &br_ec_all_m31);
+ br_ssl_engine_set_ecdsa(cc, &br_ecdsa_i31_vrfy_asn1);
+#endif
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_engine_default_rsavrfy.c b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_rsavrfy.c
new file mode 100644
index 00000000..ad0628ab
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_engine_default_rsavrfy.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context *cc)
+{
+ br_ssl_engine_set_rsavrfy(cc, br_rsa_pkcs1_vrfy_get_default());
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hashes.c b/test/monniaux/BearSSL/src/ssl/ssl_hashes.c
new file mode 100644
index 00000000..e10a980c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hashes.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+int
+br_ssl_choose_hash(unsigned bf)
+{
+ static const unsigned char pref[] = {
+ br_sha256_ID, br_sha384_ID, br_sha512_ID,
+ br_sha224_ID, br_sha1_ID
+ };
+ size_t u;
+
+ for (u = 0; u < sizeof pref; u ++) {
+ int x;
+
+ x = pref[u];
+ if ((bf >> x) & 1) {
+ return x;
+ }
+ }
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hs_client.c b/test/monniaux/BearSSL/src/ssl/ssl_hs_client.c
new file mode 100644
index 00000000..de36165a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hs_client.c
@@ -0,0 +1,1915 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_ssl_hs_client_init_main(void *t0ctx);
+
+void br_ssl_hs_client_run(void *t0ctx);
+
+
+
+#include <stddef.h>
+#include <string.h>
+
+#include "inner.h"
+
+/*
+ * This macro evaluates to a pointer to the current engine context.
+ */
+#define ENG ((br_ssl_engine_context *)(void *)((unsigned char *)t0ctx - offsetof(br_ssl_engine_context, cpu)))
+
+
+
+
+
+/*
+ * This macro evaluates to a pointer to the client context, under that
+ * specific name. It must be noted that since the engine context is the
+ * first field of the br_ssl_client_context structure ('eng'), then
+ * pointers values of both types are interchangeable, modulo an
+ * appropriate cast. This also means that "addresses" computed as offsets
+ * within the structure work for both kinds of context.
+ */
+#define CTX ((br_ssl_client_context *)ENG)
+
+/*
+ * Generate the pre-master secret for RSA key exchange, and encrypt it
+ * with the server's public key. Returned value is either the encrypted
+ * data length (in bytes), or -x on error, with 'x' being an error code.
+ *
+ * This code assumes that the public key has been already verified (it
+ * was properly obtained by the X.509 engine, and it has the right type,
+ * i.e. it is of type RSA and suitable for encryption).
+ */
+static int
+make_pms_rsa(br_ssl_client_context *ctx, int prf_id)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ const unsigned char *n;
+ unsigned char *pms;
+ size_t nlen, u;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+
+ /*
+ * Compute actual RSA key length, in case there are leading zeros.
+ */
+ n = pk->key.rsa.n;
+ nlen = pk->key.rsa.nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+
+ /*
+ * We need at least 59 bytes (48 bytes for pre-master secret, and
+ * 11 bytes for the PKCS#1 type 2 padding). Note that the X.509
+ * minimal engine normally blocks RSA keys shorter than 128 bytes,
+ * so this is mostly for public keys provided explicitly by the
+ * caller.
+ */
+ if (nlen < 59) {
+ return -BR_ERR_X509_WEAK_PUBLIC_KEY;
+ }
+ if (nlen > sizeof ctx->eng.pad) {
+ return -BR_ERR_LIMIT_EXCEEDED;
+ }
+
+ /*
+ * Make PMS.
+ */
+ pms = ctx->eng.pad + nlen - 48;
+ br_enc16be(pms, ctx->eng.version_max);
+ br_hmac_drbg_generate(&ctx->eng.rng, pms + 2, 46);
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, pms, 48);
+
+ /*
+ * Apply PKCS#1 type 2 padding.
+ */
+ ctx->eng.pad[0] = 0x00;
+ ctx->eng.pad[1] = 0x02;
+ ctx->eng.pad[nlen - 49] = 0x00;
+ br_hmac_drbg_generate(&ctx->eng.rng, ctx->eng.pad + 2, nlen - 51);
+ for (u = 2; u < nlen - 49; u ++) {
+ while (ctx->eng.pad[u] == 0) {
+ br_hmac_drbg_generate(&ctx->eng.rng,
+ &ctx->eng.pad[u], 1);
+ }
+ }
+
+ /*
+ * Compute RSA encryption.
+ */
+ if (!ctx->irsapub(ctx->eng.pad, nlen, &pk->key.rsa)) {
+ return -BR_ERR_LIMIT_EXCEEDED;
+ }
+ return (int)nlen;
+}
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char *HASH_OID[] = {
+ BR_HASH_OID_SHA1,
+ BR_HASH_OID_SHA224,
+ BR_HASH_OID_SHA256,
+ BR_HASH_OID_SHA384,
+ BR_HASH_OID_SHA512
+};
+
+/*
+ * Check the RSA signature on the ServerKeyExchange message.
+ *
+ * hash hash function ID (2 to 6), or 0 for MD5+SHA-1 (with RSA only)
+ * use_rsa non-zero for RSA signature, zero for ECDSA
+ * sig_len signature length (in bytes); signature value is in the pad
+ *
+ * Returned value is 0 on success, or an error code.
+ */
+static int
+verify_SKE_sig(br_ssl_client_context *ctx,
+ int hash, int use_rsa, size_t sig_len)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ br_multihash_context mhc;
+ unsigned char hv[64], head[4];
+ size_t hv_len;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ br_multihash_zero(&mhc);
+ br_multihash_copyimpl(&mhc, &ctx->eng.mhash);
+ br_multihash_init(&mhc);
+ br_multihash_update(&mhc,
+ ctx->eng.client_random, sizeof ctx->eng.client_random);
+ br_multihash_update(&mhc,
+ ctx->eng.server_random, sizeof ctx->eng.server_random);
+ head[0] = 3;
+ head[1] = 0;
+ head[2] = ctx->eng.ecdhe_curve;
+ head[3] = ctx->eng.ecdhe_point_len;
+ br_multihash_update(&mhc, head, sizeof head);
+ br_multihash_update(&mhc,
+ ctx->eng.ecdhe_point, ctx->eng.ecdhe_point_len);
+ if (hash) {
+ hv_len = br_multihash_out(&mhc, hash, hv);
+ if (hv_len == 0) {
+ return BR_ERR_INVALID_ALGORITHM;
+ }
+ } else {
+ if (!br_multihash_out(&mhc, br_md5_ID, hv)
+ || !br_multihash_out(&mhc, br_sha1_ID, hv + 16))
+ {
+ return BR_ERR_INVALID_ALGORITHM;
+ }
+ hv_len = 36;
+ }
+ if (use_rsa) {
+ unsigned char tmp[64];
+ const unsigned char *hash_oid;
+
+ if (hash) {
+ hash_oid = HASH_OID[hash - 2];
+ } else {
+ hash_oid = NULL;
+ }
+ if (!ctx->eng.irsavrfy(ctx->eng.pad, sig_len,
+ hash_oid, hv_len, &pk->key.rsa, tmp)
+ || memcmp(tmp, hv, hv_len) != 0)
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ } else {
+ if (!ctx->eng.iecdsa(ctx->eng.iec, hv, hv_len, &pk->key.ec,
+ ctx->eng.pad, sig_len))
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Perform client-side ECDH (or ECDHE). The point that should be sent to
+ * the server is written in the pad; returned value is either the point
+ * length (in bytes), or -x on error, with 'x' being an error code.
+ *
+ * The point _from_ the server is taken from ecdhe_point[] if 'ecdhe'
+ * is non-zero, or from the X.509 engine context if 'ecdhe' is zero
+ * (for static ECDH).
+ */
+static int
+make_pms_ecdh(br_ssl_client_context *ctx, unsigned ecdhe, int prf_id)
+{
+ int curve;
+ unsigned char key[66], point[133];
+ const unsigned char *order, *point_src;
+ size_t glen, olen, point_len, xoff, xlen;
+ unsigned char mask;
+
+ if (ecdhe) {
+ curve = ctx->eng.ecdhe_curve;
+ point_src = ctx->eng.ecdhe_point;
+ point_len = ctx->eng.ecdhe_point_len;
+ } else {
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ curve = pk->key.ec.curve;
+ point_src = pk->key.ec.q;
+ point_len = pk->key.ec.qlen;
+ }
+ if ((ctx->eng.iec->supported_curves & ((uint32_t)1 << curve)) == 0) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ /*
+ * We need to generate our key, as a non-zero random value which
+ * is lower than the curve order, in a "large enough" range. We
+ * force top bit to 0 and bottom bit to 1, which guarantees that
+ * the value is in the proper range.
+ */
+ order = ctx->eng.iec->order(curve, &olen);
+ mask = 0xFF;
+ while (mask >= order[0]) {
+ mask >>= 1;
+ }
+ br_hmac_drbg_generate(&ctx->eng.rng, key, olen);
+ key[0] &= mask;
+ key[olen - 1] |= 0x01;
+
+ /*
+ * Compute the common ECDH point, whose X coordinate is the
+ * pre-master secret.
+ */
+ ctx->eng.iec->generator(curve, &glen);
+ if (glen != point_len) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ memcpy(point, point_src, glen);
+ if (!ctx->eng.iec->mul(point, glen, key, olen, curve)) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ /*
+ * The pre-master secret is the X coordinate.
+ */
+ xoff = ctx->eng.iec->xoff(curve, &xlen);
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, point + xoff, xlen);
+
+ ctx->eng.iec->mulgen(point, key, olen, curve);
+ memcpy(ctx->eng.pad, point, glen);
+ return (int)glen;
+}
+
+/*
+ * Perform full static ECDH. This occurs only in the context of client
+ * authentication with certificates: the server uses an EC public key,
+ * the cipher suite is of type ECDH (not ECDHE), the server requested a
+ * client certificate and accepts static ECDH, the client has a
+ * certificate with an EC public key in the same curve, and accepts
+ * static ECDH as well.
+ *
+ * Returned value is 0 on success, -1 on error.
+ */
+static int
+make_pms_static_ecdh(br_ssl_client_context *ctx, int prf_id)
+{
+ unsigned char point[133];
+ size_t point_len;
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ point_len = pk->key.ec.qlen;
+ if (point_len > sizeof point) {
+ return -1;
+ }
+ memcpy(point, pk->key.ec.q, point_len);
+ if (!(*ctx->client_auth_vtable)->do_keyx(
+ ctx->client_auth_vtable, point, &point_len))
+ {
+ return -1;
+ }
+ br_ssl_engine_compute_master(&ctx->eng,
+ prf_id, point, point_len);
+ return 0;
+}
+
+/*
+ * Compute the client-side signature. This is invoked only when a
+ * signature-based client authentication was selected. The computed
+ * signature is in the pad; its length (in bytes) is returned. On
+ * error, 0 is returned.
+ */
+static size_t
+make_client_sign(br_ssl_client_context *ctx)
+{
+ size_t hv_len;
+
+ /*
+ * Compute hash of handshake messages so far. This "cannot" fail
+ * because the list of supported hash functions provided to the
+ * client certificate handler was trimmed to include only the
+ * hash functions that the multi-hasher supports.
+ */
+ if (ctx->hash_id) {
+ hv_len = br_multihash_out(&ctx->eng.mhash,
+ ctx->hash_id, ctx->eng.pad);
+ } else {
+ br_multihash_out(&ctx->eng.mhash,
+ br_md5_ID, ctx->eng.pad);
+ br_multihash_out(&ctx->eng.mhash,
+ br_sha1_ID, ctx->eng.pad + 16);
+ hv_len = 36;
+ }
+ return (*ctx->client_auth_vtable)->do_sign(
+ ctx->client_auth_vtable, ctx->hash_id, hv_len,
+ ctx->eng.pad, sizeof ctx->eng.pad);
+}
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x00, 0x0A, 0x00, 0x24, 0x00, 0x2F, 0x01, 0x24, 0x00, 0x35, 0x02,
+ 0x24, 0x00, 0x3C, 0x01, 0x44, 0x00, 0x3D, 0x02, 0x44, 0x00, 0x9C, 0x03,
+ 0x04, 0x00, 0x9D, 0x04, 0x05, 0xC0, 0x03, 0x40, 0x24, 0xC0, 0x04, 0x41,
+ 0x24, 0xC0, 0x05, 0x42, 0x24, 0xC0, 0x08, 0x20, 0x24, 0xC0, 0x09, 0x21,
+ 0x24, 0xC0, 0x0A, 0x22, 0x24, 0xC0, 0x0D, 0x30, 0x24, 0xC0, 0x0E, 0x31,
+ 0x24, 0xC0, 0x0F, 0x32, 0x24, 0xC0, 0x12, 0x10, 0x24, 0xC0, 0x13, 0x11,
+ 0x24, 0xC0, 0x14, 0x12, 0x24, 0xC0, 0x23, 0x21, 0x44, 0xC0, 0x24, 0x22,
+ 0x55, 0xC0, 0x25, 0x41, 0x44, 0xC0, 0x26, 0x42, 0x55, 0xC0, 0x27, 0x11,
+ 0x44, 0xC0, 0x28, 0x12, 0x55, 0xC0, 0x29, 0x31, 0x44, 0xC0, 0x2A, 0x32,
+ 0x55, 0xC0, 0x2B, 0x23, 0x04, 0xC0, 0x2C, 0x24, 0x05, 0xC0, 0x2D, 0x43,
+ 0x04, 0xC0, 0x2E, 0x44, 0x05, 0xC0, 0x2F, 0x13, 0x04, 0xC0, 0x30, 0x14,
+ 0x05, 0xC0, 0x31, 0x33, 0x04, 0xC0, 0x32, 0x34, 0x05, 0xC0, 0x9C, 0x06,
+ 0x04, 0xC0, 0x9D, 0x07, 0x04, 0xC0, 0xA0, 0x08, 0x04, 0xC0, 0xA1, 0x09,
+ 0x04, 0xC0, 0xAC, 0x26, 0x04, 0xC0, 0xAD, 0x27, 0x04, 0xC0, 0xAE, 0x28,
+ 0x04, 0xC0, 0xAF, 0x29, 0x04, 0xCC, 0xA8, 0x15, 0x04, 0xCC, 0xA9, 0x25,
+ 0x04, 0x00, 0x00
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x0D, 0x00, 0x00, 0x01,
+ 0x00, 0x0E, 0x00, 0x00, 0x01, 0x00, 0x0F, 0x00, 0x00, 0x01, 0x01, 0x08,
+ 0x00, 0x00, 0x01, 0x01, 0x09, 0x00, 0x00, 0x01, 0x02, 0x08, 0x00, 0x00,
+ 0x01, 0x02, 0x09, 0x00, 0x00, 0x25, 0x25, 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_CCS), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_CIPHER_SUITE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_COMPRESSION), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_FINISHED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_FRAGLEN), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_HANDSHAKE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_HELLO_DONE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_PARAM), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_SECRENEG), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_SNI), 0x00, 0x00, 0x01, T0_INT1(BR_ERR_BAD_VERSION),
+ 0x00, 0x00, 0x01, T0_INT1(BR_ERR_EXTRA_EXTENSION), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_INVALID_ALGORITHM), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_LIMIT_EXCEEDED), 0x00, 0x00, 0x01, T0_INT1(BR_ERR_OK),
+ 0x00, 0x00, 0x01, T0_INT1(BR_ERR_OVERSIZED_ID), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_RESUME_MISMATCH), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_UNEXPECTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_UNSUPPORTED_VERSION), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_WRONG_KEY_USAGE), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, action)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, alert)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, application_data)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_client_context, auth_type)), 0x00, 0x00,
+ 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, cipher_suite)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, client_random)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, close_received)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, ecdhe_curve)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, ecdhe_point)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, ecdhe_point_len)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, flags)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_client_context, hash_id)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_client_context, hashes)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, log_max_frag_len)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_client_context, min_clienthello_len)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, pad)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, protocol_names_num)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, record_type_in)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, record_type_out)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, reneg)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, saved_finished)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, selected_protocol)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, server_name)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, server_random)), 0x00, 0x00,
+ 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, session_id)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, session_id_len)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, shutdown_recv)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, suites_buf)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, suites_num)), 0x00, 0x00,
+ 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, version)),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_in)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, version_max)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_min)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_out)),
+ 0x00, 0x00, 0x09, 0x26, 0x58, 0x06, 0x02, 0x68, 0x28, 0x00, 0x00, 0x06,
+ 0x08, 0x2C, 0x0E, 0x05, 0x02, 0x71, 0x28, 0x04, 0x01, 0x3C, 0x00, 0x00,
+ 0x01, 0x01, 0x00, 0x01, 0x03, 0x00, 0x99, 0x26, 0x5E, 0x44, 0x9D, 0x26,
+ 0x05, 0x04, 0x60, 0x01, 0x00, 0x00, 0x02, 0x00, 0x0E, 0x06, 0x02, 0x9D,
+ 0x00, 0x5E, 0x04, 0x6B, 0x00, 0x06, 0x02, 0x68, 0x28, 0x00, 0x00, 0x26,
+ 0x89, 0x44, 0x05, 0x03, 0x01, 0x0C, 0x08, 0x44, 0x79, 0x2C, 0xAB, 0x1C,
+ 0x84, 0x01, 0x0C, 0x31, 0x00, 0x00, 0x26, 0x1F, 0x01, 0x08, 0x0B, 0x44,
+ 0x5C, 0x1F, 0x08, 0x00, 0x01, 0x03, 0x00, 0x77, 0x2E, 0x02, 0x00, 0x36,
+ 0x17, 0x01, 0x01, 0x0B, 0x77, 0x3E, 0x29, 0x1A, 0x36, 0x06, 0x07, 0x02,
+ 0x00, 0xCF, 0x03, 0x00, 0x04, 0x75, 0x01, 0x00, 0xC5, 0x02, 0x00, 0x26,
+ 0x1A, 0x17, 0x06, 0x02, 0x6F, 0x28, 0xCF, 0x04, 0x76, 0x01, 0x01, 0x00,
+ 0x77, 0x3E, 0x01, 0x16, 0x87, 0x3E, 0x01, 0x00, 0x8A, 0x3C, 0x34, 0xD5,
+ 0x29, 0xB4, 0x06, 0x09, 0x01, 0x7F, 0xAF, 0x01, 0x7F, 0xD2, 0x04, 0x80,
+ 0x53, 0xB1, 0x79, 0x2C, 0xA1, 0x01, T0_INT1(BR_KEYTYPE_SIGN), 0x17,
+ 0x06, 0x01, 0xB5, 0xB8, 0x26, 0x01, 0x0D, 0x0E, 0x06, 0x07, 0x25, 0xB7,
+ 0xB8, 0x01, 0x7F, 0x04, 0x02, 0x01, 0x00, 0x03, 0x00, 0x01, 0x0E, 0x0E,
+ 0x05, 0x02, 0x72, 0x28, 0x06, 0x02, 0x67, 0x28, 0x33, 0x06, 0x02, 0x72,
+ 0x28, 0x02, 0x00, 0x06, 0x1C, 0xD3, 0x80, 0x2E, 0x01, 0x81, 0x7F, 0x0E,
+ 0x06, 0x0D, 0x25, 0x01, 0x10, 0xDE, 0x01, 0x00, 0xDD, 0x79, 0x2C, 0xAB,
+ 0x24, 0x04, 0x04, 0xD6, 0x06, 0x01, 0xD4, 0x04, 0x01, 0xD6, 0x01, 0x7F,
+ 0xD2, 0x01, 0x7F, 0xAF, 0x01, 0x01, 0x77, 0x3E, 0x01, 0x17, 0x87, 0x3E,
+ 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x9A, 0x01, 0x0C, 0x11, 0x01, 0x00,
+ 0x38, 0x0E, 0x06, 0x05, 0x25, 0x01,
+ T0_INT1(BR_KEYTYPE_RSA | BR_KEYTYPE_KEYX), 0x04, 0x30, 0x01, 0x01,
+ 0x38, 0x0E, 0x06, 0x05, 0x25, 0x01,
+ T0_INT1(BR_KEYTYPE_RSA | BR_KEYTYPE_SIGN), 0x04, 0x25, 0x01, 0x02,
+ 0x38, 0x0E, 0x06, 0x05, 0x25, 0x01,
+ T0_INT1(BR_KEYTYPE_EC | BR_KEYTYPE_SIGN), 0x04, 0x1A, 0x01, 0x03,
+ 0x38, 0x0E, 0x06, 0x05, 0x25, 0x01,
+ T0_INT1(BR_KEYTYPE_EC | BR_KEYTYPE_KEYX), 0x04, 0x0F, 0x01, 0x04,
+ 0x38, 0x0E, 0x06, 0x05, 0x25, 0x01,
+ T0_INT1(BR_KEYTYPE_EC | BR_KEYTYPE_KEYX), 0x04, 0x04, 0x01, 0x00,
+ 0x44, 0x25, 0x00, 0x00, 0x82, 0x2E, 0x01, 0x0E, 0x0E, 0x06, 0x04, 0x01,
+ 0x00, 0x04, 0x02, 0x01, 0x05, 0x00, 0x00, 0x40, 0x06, 0x04, 0x01, 0x06,
+ 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x88, 0x2E, 0x26, 0x06, 0x08, 0x01,
+ 0x01, 0x09, 0x01, 0x11, 0x07, 0x04, 0x03, 0x25, 0x01, 0x05, 0x00, 0x01,
+ 0x41, 0x03, 0x00, 0x25, 0x01, 0x00, 0x43, 0x06, 0x03, 0x02, 0x00, 0x08,
+ 0x42, 0x06, 0x03, 0x02, 0x00, 0x08, 0x26, 0x06, 0x06, 0x01, 0x01, 0x0B,
+ 0x01, 0x06, 0x08, 0x00, 0x00, 0x8B, 0x3F, 0x26, 0x06, 0x03, 0x01, 0x09,
+ 0x08, 0x00, 0x01, 0x40, 0x26, 0x06, 0x1E, 0x01, 0x00, 0x03, 0x00, 0x26,
+ 0x06, 0x0E, 0x26, 0x01, 0x01, 0x17, 0x02, 0x00, 0x08, 0x03, 0x00, 0x01,
+ 0x01, 0x11, 0x04, 0x6F, 0x25, 0x02, 0x00, 0x01, 0x01, 0x0B, 0x01, 0x06,
+ 0x08, 0x00, 0x00, 0x7F, 0x2D, 0x44, 0x11, 0x01, 0x01, 0x17, 0x35, 0x00,
+ 0x00, 0x9F, 0xCE, 0x26, 0x01, 0x07, 0x17, 0x01, 0x00, 0x38, 0x0E, 0x06,
+ 0x09, 0x25, 0x01, 0x10, 0x17, 0x06, 0x01, 0x9F, 0x04, 0x35, 0x01, 0x01,
+ 0x38, 0x0E, 0x06, 0x2C, 0x25, 0x25, 0x01, 0x00, 0x77, 0x3E, 0xB3, 0x88,
+ 0x2E, 0x01, 0x01, 0x0E, 0x01, 0x01, 0xA8, 0x37, 0x06, 0x17, 0x29, 0x1A,
+ 0x36, 0x06, 0x04, 0xCE, 0x25, 0x04, 0x78, 0x01, 0x80, 0x64, 0xC5, 0x01,
+ 0x01, 0x77, 0x3E, 0x01, 0x17, 0x87, 0x3E, 0x04, 0x01, 0x9F, 0x04, 0x03,
+ 0x72, 0x28, 0x25, 0x04, 0xFF, 0x34, 0x01, 0x26, 0x03, 0x00, 0x09, 0x26,
+ 0x58, 0x06, 0x02, 0x68, 0x28, 0x02, 0x00, 0x00, 0x00, 0x9A, 0x01, 0x0F,
+ 0x17, 0x00, 0x00, 0x76, 0x2E, 0x01, 0x00, 0x38, 0x0E, 0x06, 0x10, 0x25,
+ 0x26, 0x01, 0x01, 0x0D, 0x06, 0x03, 0x25, 0x01, 0x02, 0x76, 0x3E, 0x01,
+ 0x00, 0x04, 0x21, 0x01, 0x01, 0x38, 0x0E, 0x06, 0x14, 0x25, 0x01, 0x00,
+ 0x76, 0x3E, 0x26, 0x01, 0x80, 0x64, 0x0E, 0x06, 0x05, 0x01, 0x82, 0x00,
+ 0x08, 0x28, 0x5A, 0x04, 0x07, 0x25, 0x01, 0x82, 0x00, 0x08, 0x28, 0x25,
+ 0x00, 0x00, 0x01, 0x00, 0x2F, 0x06, 0x05, 0x3A, 0xAC, 0x37, 0x04, 0x78,
+ 0x26, 0x06, 0x04, 0x01, 0x01, 0x8F, 0x3E, 0x00, 0x01, 0xBF, 0xAA, 0xBF,
+ 0xAA, 0xC1, 0x84, 0x44, 0x26, 0x03, 0x00, 0xB6, 0x9B, 0x9B, 0x02, 0x00,
+ 0x4D, 0x26, 0x58, 0x06, 0x0A, 0x01, 0x03, 0xA8, 0x06, 0x02, 0x72, 0x28,
+ 0x25, 0x04, 0x03, 0x5C, 0x8A, 0x3C, 0x00, 0x00, 0x2F, 0x06, 0x0B, 0x86,
+ 0x2E, 0x01, 0x14, 0x0D, 0x06, 0x02, 0x72, 0x28, 0x04, 0x11, 0xCE, 0x01,
+ 0x07, 0x17, 0x26, 0x01, 0x02, 0x0D, 0x06, 0x06, 0x06, 0x02, 0x72, 0x28,
+ 0x04, 0x70, 0x25, 0xC2, 0x01, 0x01, 0x0D, 0x33, 0x37, 0x06, 0x02, 0x61,
+ 0x28, 0x26, 0x01, 0x01, 0xC8, 0x36, 0xB2, 0x00, 0x01, 0xB8, 0x01, 0x0B,
+ 0x0E, 0x05, 0x02, 0x72, 0x28, 0x26, 0x01, 0x03, 0x0E, 0x06, 0x08, 0xC0,
+ 0x06, 0x02, 0x68, 0x28, 0x44, 0x25, 0x00, 0x44, 0x57, 0xC0, 0xAA, 0x26,
+ 0x06, 0x23, 0xC0, 0xAA, 0x26, 0x56, 0x26, 0x06, 0x18, 0x26, 0x01, 0x82,
+ 0x00, 0x0F, 0x06, 0x05, 0x01, 0x82, 0x00, 0x04, 0x01, 0x26, 0x03, 0x00,
+ 0x84, 0x02, 0x00, 0xB6, 0x02, 0x00, 0x53, 0x04, 0x65, 0x9B, 0x54, 0x04,
+ 0x5A, 0x9B, 0x9B, 0x55, 0x26, 0x06, 0x02, 0x35, 0x00, 0x25, 0x2B, 0x00,
+ 0x00, 0x79, 0x2C, 0xA1, 0x01, 0x7F, 0xB0, 0x26, 0x58, 0x06, 0x02, 0x35,
+ 0x28, 0x26, 0x05, 0x02, 0x72, 0x28, 0x38, 0x17, 0x0D, 0x06, 0x02, 0x74,
+ 0x28, 0x3B, 0x00, 0x00, 0x9C, 0xB8, 0x01, 0x14, 0x0D, 0x06, 0x02, 0x72,
+ 0x28, 0x84, 0x01, 0x0C, 0x08, 0x01, 0x0C, 0xB6, 0x9B, 0x84, 0x26, 0x01,
+ 0x0C, 0x08, 0x01, 0x0C, 0x30, 0x05, 0x02, 0x64, 0x28, 0x00, 0x00, 0xB9,
+ 0x06, 0x02, 0x72, 0x28, 0x06, 0x02, 0x66, 0x28, 0x00, 0x0A, 0xB8, 0x01,
+ 0x02, 0x0E, 0x05, 0x02, 0x72, 0x28, 0xBF, 0x03, 0x00, 0x02, 0x00, 0x95,
+ 0x2C, 0x0A, 0x02, 0x00, 0x94, 0x2C, 0x0F, 0x37, 0x06, 0x02, 0x73, 0x28,
+ 0x02, 0x00, 0x93, 0x2C, 0x0D, 0x06, 0x02, 0x6B, 0x28, 0x02, 0x00, 0x96,
+ 0x3C, 0x8C, 0x01, 0x20, 0xB6, 0x01, 0x00, 0x03, 0x01, 0xC1, 0x03, 0x02,
+ 0x02, 0x02, 0x01, 0x20, 0x0F, 0x06, 0x02, 0x70, 0x28, 0x84, 0x02, 0x02,
+ 0xB6, 0x02, 0x02, 0x8E, 0x2E, 0x0E, 0x02, 0x02, 0x01, 0x00, 0x0F, 0x17,
+ 0x06, 0x0B, 0x8D, 0x84, 0x02, 0x02, 0x30, 0x06, 0x04, 0x01, 0x7F, 0x03,
+ 0x01, 0x8D, 0x84, 0x02, 0x02, 0x31, 0x02, 0x02, 0x8E, 0x3E, 0x02, 0x00,
+ 0x92, 0x02, 0x01, 0x98, 0xBF, 0x26, 0xC3, 0x58, 0x06, 0x02, 0x62, 0x28,
+ 0x26, 0xCD, 0x02, 0x00, 0x01, 0x86, 0x03, 0x0A, 0x17, 0x06, 0x02, 0x62,
+ 0x28, 0x79, 0x02, 0x01, 0x98, 0xC1, 0x06, 0x02, 0x63, 0x28, 0x26, 0x06,
+ 0x81, 0x47, 0xBF, 0xAA, 0xA6, 0x03, 0x03, 0xA4, 0x03, 0x04, 0xA2, 0x03,
+ 0x05, 0xA5, 0x03, 0x06, 0xA7, 0x03, 0x07, 0xA3, 0x03, 0x08, 0x27, 0x03,
+ 0x09, 0x26, 0x06, 0x81, 0x18, 0xBF, 0x01, 0x00, 0x38, 0x0E, 0x06, 0x0F,
+ 0x25, 0x02, 0x03, 0x05, 0x02, 0x6C, 0x28, 0x01, 0x00, 0x03, 0x03, 0xBE,
+ 0x04, 0x80, 0x7F, 0x01, 0x01, 0x38, 0x0E, 0x06, 0x0F, 0x25, 0x02, 0x05,
+ 0x05, 0x02, 0x6C, 0x28, 0x01, 0x00, 0x03, 0x05, 0xBC, 0x04, 0x80, 0x6A,
+ 0x01, 0x83, 0xFE, 0x01, 0x38, 0x0E, 0x06, 0x0F, 0x25, 0x02, 0x04, 0x05,
+ 0x02, 0x6C, 0x28, 0x01, 0x00, 0x03, 0x04, 0xBD, 0x04, 0x80, 0x53, 0x01,
+ 0x0D, 0x38, 0x0E, 0x06, 0x0E, 0x25, 0x02, 0x06, 0x05, 0x02, 0x6C, 0x28,
+ 0x01, 0x00, 0x03, 0x06, 0xBA, 0x04, 0x3F, 0x01, 0x0A, 0x38, 0x0E, 0x06,
+ 0x0E, 0x25, 0x02, 0x07, 0x05, 0x02, 0x6C, 0x28, 0x01, 0x00, 0x03, 0x07,
+ 0xBA, 0x04, 0x2B, 0x01, 0x0B, 0x38, 0x0E, 0x06, 0x0E, 0x25, 0x02, 0x08,
+ 0x05, 0x02, 0x6C, 0x28, 0x01, 0x00, 0x03, 0x08, 0xBA, 0x04, 0x17, 0x01,
+ 0x10, 0x38, 0x0E, 0x06, 0x0E, 0x25, 0x02, 0x09, 0x05, 0x02, 0x6C, 0x28,
+ 0x01, 0x00, 0x03, 0x09, 0xAE, 0x04, 0x03, 0x6C, 0x28, 0x25, 0x04, 0xFE,
+ 0x64, 0x02, 0x04, 0x06, 0x0D, 0x02, 0x04, 0x01, 0x05, 0x0F, 0x06, 0x02,
+ 0x69, 0x28, 0x01, 0x01, 0x88, 0x3E, 0x9B, 0x04, 0x0C, 0xA4, 0x01, 0x05,
+ 0x0F, 0x06, 0x02, 0x69, 0x28, 0x01, 0x01, 0x88, 0x3E, 0x9B, 0x02, 0x01,
+ 0x00, 0x04, 0xB8, 0x01, 0x0C, 0x0E, 0x05, 0x02, 0x72, 0x28, 0xC1, 0x01,
+ 0x03, 0x0E, 0x05, 0x02, 0x6D, 0x28, 0xBF, 0x26, 0x7C, 0x3E, 0x26, 0x01,
+ 0x20, 0x10, 0x06, 0x02, 0x6D, 0x28, 0x40, 0x44, 0x11, 0x01, 0x01, 0x17,
+ 0x05, 0x02, 0x6D, 0x28, 0xC1, 0x26, 0x01, 0x81, 0x05, 0x0F, 0x06, 0x02,
+ 0x6D, 0x28, 0x26, 0x7E, 0x3E, 0x7D, 0x44, 0xB6, 0x92, 0x2C, 0x01, 0x86,
+ 0x03, 0x10, 0x03, 0x00, 0x79, 0x2C, 0xCB, 0x03, 0x01, 0x01, 0x02, 0x03,
+ 0x02, 0x02, 0x00, 0x06, 0x21, 0xC1, 0x26, 0x26, 0x01, 0x02, 0x0A, 0x44,
+ 0x01, 0x06, 0x0F, 0x37, 0x06, 0x02, 0x6D, 0x28, 0x03, 0x02, 0xC1, 0x02,
+ 0x01, 0x01, 0x01, 0x0B, 0x01, 0x03, 0x08, 0x0E, 0x05, 0x02, 0x6D, 0x28,
+ 0x04, 0x08, 0x02, 0x01, 0x06, 0x04, 0x01, 0x00, 0x03, 0x02, 0xBF, 0x26,
+ 0x03, 0x03, 0x26, 0x01, 0x84, 0x00, 0x0F, 0x06, 0x02, 0x6E, 0x28, 0x84,
+ 0x44, 0xB6, 0x02, 0x02, 0x02, 0x01, 0x02, 0x03, 0x50, 0x26, 0x06, 0x01,
+ 0x28, 0x25, 0x9B, 0x00, 0x02, 0x03, 0x00, 0x03, 0x01, 0x02, 0x00, 0x97,
+ 0x02, 0x01, 0x02, 0x00, 0x39, 0x26, 0x01, 0x00, 0x0E, 0x06, 0x02, 0x60,
+ 0x00, 0xD0, 0x04, 0x74, 0x02, 0x01, 0x00, 0x03, 0x00, 0xC1, 0xAA, 0x26,
+ 0x06, 0x80, 0x43, 0xC1, 0x01, 0x01, 0x38, 0x0E, 0x06, 0x06, 0x25, 0x01,
+ 0x81, 0x7F, 0x04, 0x2E, 0x01, 0x80, 0x40, 0x38, 0x0E, 0x06, 0x07, 0x25,
+ 0x01, 0x83, 0xFE, 0x00, 0x04, 0x20, 0x01, 0x80, 0x41, 0x38, 0x0E, 0x06,
+ 0x07, 0x25, 0x01, 0x84, 0x80, 0x00, 0x04, 0x12, 0x01, 0x80, 0x42, 0x38,
+ 0x0E, 0x06, 0x07, 0x25, 0x01, 0x88, 0x80, 0x00, 0x04, 0x04, 0x01, 0x00,
+ 0x44, 0x25, 0x02, 0x00, 0x37, 0x03, 0x00, 0x04, 0xFF, 0x39, 0x9B, 0x79,
+ 0x2C, 0xC9, 0x05, 0x09, 0x02, 0x00, 0x01, 0x83, 0xFF, 0x7F, 0x17, 0x03,
+ 0x00, 0x92, 0x2C, 0x01, 0x86, 0x03, 0x10, 0x06, 0x3A, 0xBB, 0x26, 0x81,
+ 0x3D, 0x41, 0x25, 0x26, 0x01, 0x08, 0x0B, 0x37, 0x01, 0x8C, 0x80, 0x00,
+ 0x37, 0x17, 0x02, 0x00, 0x17, 0x02, 0x00, 0x01, 0x8C, 0x80, 0x00, 0x17,
+ 0x06, 0x19, 0x26, 0x01, 0x81, 0x7F, 0x17, 0x06, 0x05, 0x01, 0x84, 0x80,
+ 0x00, 0x37, 0x26, 0x01, 0x83, 0xFE, 0x00, 0x17, 0x06, 0x05, 0x01, 0x88,
+ 0x80, 0x00, 0x37, 0x03, 0x00, 0x04, 0x09, 0x02, 0x00, 0x01, 0x8C, 0x88,
+ 0x01, 0x17, 0x03, 0x00, 0x16, 0xBF, 0xAA, 0x26, 0x06, 0x23, 0xBF, 0xAA,
+ 0x26, 0x15, 0x26, 0x06, 0x18, 0x26, 0x01, 0x82, 0x00, 0x0F, 0x06, 0x05,
+ 0x01, 0x82, 0x00, 0x04, 0x01, 0x26, 0x03, 0x01, 0x84, 0x02, 0x01, 0xB6,
+ 0x02, 0x01, 0x12, 0x04, 0x65, 0x9B, 0x13, 0x04, 0x5A, 0x9B, 0x14, 0x9B,
+ 0x02, 0x00, 0x2A, 0x00, 0x00, 0xB9, 0x26, 0x5A, 0x06, 0x07, 0x25, 0x06,
+ 0x02, 0x66, 0x28, 0x04, 0x74, 0x00, 0x00, 0xC2, 0x01, 0x03, 0xC0, 0x44,
+ 0x25, 0x44, 0x00, 0x00, 0xBF, 0xC6, 0x00, 0x03, 0x01, 0x00, 0x03, 0x00,
+ 0xBF, 0xAA, 0x26, 0x06, 0x80, 0x50, 0xC1, 0x03, 0x01, 0xC1, 0x03, 0x02,
+ 0x02, 0x01, 0x01, 0x08, 0x0E, 0x06, 0x16, 0x02, 0x02, 0x01, 0x0F, 0x0C,
+ 0x06, 0x0D, 0x01, 0x01, 0x02, 0x02, 0x01, 0x10, 0x08, 0x0B, 0x02, 0x00,
+ 0x37, 0x03, 0x00, 0x04, 0x2A, 0x02, 0x01, 0x01, 0x02, 0x10, 0x02, 0x01,
+ 0x01, 0x06, 0x0C, 0x17, 0x02, 0x02, 0x01, 0x01, 0x0E, 0x02, 0x02, 0x01,
+ 0x03, 0x0E, 0x37, 0x17, 0x06, 0x11, 0x02, 0x00, 0x01, 0x01, 0x02, 0x02,
+ 0x5D, 0x01, 0x02, 0x0B, 0x02, 0x01, 0x08, 0x0B, 0x37, 0x03, 0x00, 0x04,
+ 0xFF, 0x2C, 0x9B, 0x02, 0x00, 0x00, 0x00, 0xBF, 0x01, 0x01, 0x0E, 0x05,
+ 0x02, 0x65, 0x28, 0xC1, 0x01, 0x08, 0x08, 0x82, 0x2E, 0x0E, 0x05, 0x02,
+ 0x65, 0x28, 0x00, 0x00, 0xBF, 0x88, 0x2E, 0x05, 0x15, 0x01, 0x01, 0x0E,
+ 0x05, 0x02, 0x69, 0x28, 0xC1, 0x01, 0x00, 0x0E, 0x05, 0x02, 0x69, 0x28,
+ 0x01, 0x02, 0x88, 0x3E, 0x04, 0x1C, 0x01, 0x19, 0x0E, 0x05, 0x02, 0x69,
+ 0x28, 0xC1, 0x01, 0x18, 0x0E, 0x05, 0x02, 0x69, 0x28, 0x84, 0x01, 0x18,
+ 0xB6, 0x89, 0x84, 0x01, 0x18, 0x30, 0x05, 0x02, 0x69, 0x28, 0x00, 0x00,
+ 0xBF, 0x06, 0x02, 0x6A, 0x28, 0x00, 0x00, 0x01, 0x02, 0x97, 0xC2, 0x01,
+ 0x08, 0x0B, 0xC2, 0x08, 0x00, 0x00, 0x01, 0x03, 0x97, 0xC2, 0x01, 0x08,
+ 0x0B, 0xC2, 0x08, 0x01, 0x08, 0x0B, 0xC2, 0x08, 0x00, 0x00, 0x01, 0x01,
+ 0x97, 0xC2, 0x00, 0x00, 0x3A, 0x26, 0x58, 0x05, 0x01, 0x00, 0x25, 0xD0,
+ 0x04, 0x76, 0x02, 0x03, 0x00, 0x91, 0x2E, 0x03, 0x01, 0x01, 0x00, 0x26,
+ 0x02, 0x01, 0x0A, 0x06, 0x10, 0x26, 0x01, 0x01, 0x0B, 0x90, 0x08, 0x2C,
+ 0x02, 0x00, 0x0E, 0x06, 0x01, 0x00, 0x5C, 0x04, 0x6A, 0x25, 0x01, 0x7F,
+ 0x00, 0x00, 0x01, 0x15, 0x87, 0x3E, 0x44, 0x52, 0x25, 0x52, 0x25, 0x29,
+ 0x00, 0x00, 0x01, 0x01, 0x44, 0xC4, 0x00, 0x00, 0x44, 0x38, 0x97, 0x44,
+ 0x26, 0x06, 0x05, 0xC2, 0x25, 0x5D, 0x04, 0x78, 0x25, 0x00, 0x00, 0x26,
+ 0x01, 0x81, 0xAC, 0x00, 0x0E, 0x06, 0x04, 0x25, 0x01, 0x7F, 0x00, 0x9A,
+ 0x59, 0x00, 0x02, 0x03, 0x00, 0x79, 0x2C, 0x9A, 0x03, 0x01, 0x02, 0x01,
+ 0x01, 0x0F, 0x17, 0x02, 0x01, 0x01, 0x04, 0x11, 0x01, 0x0F, 0x17, 0x02,
+ 0x01, 0x01, 0x08, 0x11, 0x01, 0x0F, 0x17, 0x01, 0x00, 0x38, 0x0E, 0x06,
+ 0x10, 0x25, 0x01, 0x00, 0x01, 0x18, 0x02, 0x00, 0x06, 0x03, 0x49, 0x04,
+ 0x01, 0x4A, 0x04, 0x81, 0x0D, 0x01, 0x01, 0x38, 0x0E, 0x06, 0x10, 0x25,
+ 0x01, 0x01, 0x01, 0x10, 0x02, 0x00, 0x06, 0x03, 0x49, 0x04, 0x01, 0x4A,
+ 0x04, 0x80, 0x77, 0x01, 0x02, 0x38, 0x0E, 0x06, 0x10, 0x25, 0x01, 0x01,
+ 0x01, 0x20, 0x02, 0x00, 0x06, 0x03, 0x49, 0x04, 0x01, 0x4A, 0x04, 0x80,
+ 0x61, 0x01, 0x03, 0x38, 0x0E, 0x06, 0x0F, 0x25, 0x25, 0x01, 0x10, 0x02,
+ 0x00, 0x06, 0x03, 0x47, 0x04, 0x01, 0x48, 0x04, 0x80, 0x4C, 0x01, 0x04,
+ 0x38, 0x0E, 0x06, 0x0E, 0x25, 0x25, 0x01, 0x20, 0x02, 0x00, 0x06, 0x03,
+ 0x47, 0x04, 0x01, 0x48, 0x04, 0x38, 0x01, 0x05, 0x38, 0x0E, 0x06, 0x0C,
+ 0x25, 0x25, 0x02, 0x00, 0x06, 0x03, 0x4B, 0x04, 0x01, 0x4C, 0x04, 0x26,
+ 0x26, 0x01, 0x09, 0x0F, 0x06, 0x02, 0x68, 0x28, 0x44, 0x25, 0x26, 0x01,
+ 0x01, 0x17, 0x01, 0x04, 0x0B, 0x01, 0x10, 0x08, 0x44, 0x01, 0x08, 0x17,
+ 0x01, 0x10, 0x44, 0x09, 0x02, 0x00, 0x06, 0x03, 0x45, 0x04, 0x01, 0x46,
+ 0x00, 0x25, 0x00, 0x00, 0x9A, 0x01, 0x0C, 0x11, 0x01, 0x02, 0x0F, 0x00,
+ 0x00, 0x9A, 0x01, 0x0C, 0x11, 0x26, 0x5B, 0x44, 0x01, 0x03, 0x0A, 0x17,
+ 0x00, 0x00, 0x9A, 0x01, 0x0C, 0x11, 0x01, 0x01, 0x0E, 0x00, 0x00, 0x9A,
+ 0x01, 0x0C, 0x11, 0x5A, 0x00, 0x00, 0x9A, 0x01, 0x81, 0x70, 0x17, 0x01,
+ 0x20, 0x0D, 0x00, 0x00, 0x1B, 0x01, 0x00, 0x75, 0x2E, 0x26, 0x06, 0x22,
+ 0x01, 0x01, 0x38, 0x0E, 0x06, 0x06, 0x25, 0x01, 0x00, 0x9E, 0x04, 0x14,
+ 0x01, 0x02, 0x38, 0x0E, 0x06, 0x0D, 0x25, 0x77, 0x2E, 0x01, 0x01, 0x0E,
+ 0x06, 0x03, 0x01, 0x10, 0x37, 0x04, 0x01, 0x25, 0x04, 0x01, 0x25, 0x7B,
+ 0x2E, 0x05, 0x33, 0x2F, 0x06, 0x30, 0x86, 0x2E, 0x01, 0x14, 0x38, 0x0E,
+ 0x06, 0x06, 0x25, 0x01, 0x02, 0x37, 0x04, 0x22, 0x01, 0x15, 0x38, 0x0E,
+ 0x06, 0x09, 0x25, 0xAD, 0x06, 0x03, 0x01, 0x7F, 0x9E, 0x04, 0x13, 0x01,
+ 0x16, 0x38, 0x0E, 0x06, 0x06, 0x25, 0x01, 0x01, 0x37, 0x04, 0x07, 0x25,
+ 0x01, 0x04, 0x37, 0x01, 0x00, 0x25, 0x1A, 0x06, 0x03, 0x01, 0x08, 0x37,
+ 0x00, 0x00, 0x1B, 0x26, 0x05, 0x13, 0x2F, 0x06, 0x10, 0x86, 0x2E, 0x01,
+ 0x15, 0x0E, 0x06, 0x08, 0x25, 0xAD, 0x01, 0x00, 0x77, 0x3E, 0x04, 0x01,
+ 0x20, 0x00, 0x00, 0xCE, 0x01, 0x07, 0x17, 0x01, 0x01, 0x0F, 0x06, 0x02,
+ 0x72, 0x28, 0x00, 0x01, 0x03, 0x00, 0x29, 0x1A, 0x06, 0x05, 0x02, 0x00,
+ 0x87, 0x3E, 0x00, 0xCE, 0x25, 0x04, 0x74, 0x00, 0x01, 0x14, 0xD1, 0x01,
+ 0x01, 0xDE, 0x29, 0x26, 0x01, 0x00, 0xC8, 0x01, 0x16, 0xD1, 0xD7, 0x29,
+ 0x00, 0x00, 0x01, 0x0B, 0xDE, 0x4E, 0x26, 0x26, 0x01, 0x03, 0x08, 0xDD,
+ 0xDD, 0x18, 0x26, 0x58, 0x06, 0x02, 0x25, 0x00, 0xDD, 0x1D, 0x26, 0x06,
+ 0x05, 0x84, 0x44, 0xD8, 0x04, 0x77, 0x25, 0x04, 0x6C, 0x00, 0x21, 0x01,
+ 0x0F, 0xDE, 0x26, 0x92, 0x2C, 0x01, 0x86, 0x03, 0x10, 0x06, 0x0C, 0x01,
+ 0x04, 0x08, 0xDD, 0x80, 0x2E, 0xDE, 0x78, 0x2E, 0xDE, 0x04, 0x02, 0x5E,
+ 0xDD, 0x26, 0xDC, 0x84, 0x44, 0xD8, 0x00, 0x02, 0xA4, 0xA6, 0x08, 0xA2,
+ 0x08, 0xA5, 0x08, 0xA7, 0x08, 0xA3, 0x08, 0x27, 0x08, 0x03, 0x00, 0x01,
+ 0x01, 0xDE, 0x01, 0x27, 0x8E, 0x2E, 0x08, 0x91, 0x2E, 0x01, 0x01, 0x0B,
+ 0x08, 0x02, 0x00, 0x06, 0x04, 0x5E, 0x02, 0x00, 0x08, 0x83, 0x2C, 0x38,
+ 0x09, 0x26, 0x5B, 0x06, 0x24, 0x02, 0x00, 0x05, 0x04, 0x44, 0x5E, 0x44,
+ 0x5F, 0x01, 0x04, 0x09, 0x26, 0x58, 0x06, 0x03, 0x25, 0x01, 0x00, 0x26,
+ 0x01, 0x04, 0x08, 0x02, 0x00, 0x08, 0x03, 0x00, 0x44, 0x01, 0x04, 0x08,
+ 0x38, 0x08, 0x44, 0x04, 0x03, 0x25, 0x01, 0x7F, 0x03, 0x01, 0xDD, 0x94,
+ 0x2C, 0xDC, 0x7A, 0x01, 0x04, 0x19, 0x7A, 0x01, 0x04, 0x08, 0x01, 0x1C,
+ 0x32, 0x7A, 0x01, 0x20, 0xD8, 0x8D, 0x8E, 0x2E, 0xDA, 0x91, 0x2E, 0x26,
+ 0x01, 0x01, 0x0B, 0xDC, 0x90, 0x44, 0x26, 0x06, 0x0F, 0x5D, 0x38, 0x2C,
+ 0x26, 0xC7, 0x05, 0x02, 0x62, 0x28, 0xDC, 0x44, 0x5E, 0x44, 0x04, 0x6E,
+ 0x60, 0x01, 0x01, 0xDE, 0x01, 0x00, 0xDE, 0x02, 0x00, 0x06, 0x81, 0x5A,
+ 0x02, 0x00, 0xDC, 0xA4, 0x06, 0x0E, 0x01, 0x83, 0xFE, 0x01, 0xDC, 0x89,
+ 0xA4, 0x01, 0x04, 0x09, 0x26, 0xDC, 0x5D, 0xDA, 0xA6, 0x06, 0x16, 0x01,
+ 0x00, 0xDC, 0x8B, 0xA6, 0x01, 0x04, 0x09, 0x26, 0xDC, 0x01, 0x02, 0x09,
+ 0x26, 0xDC, 0x01, 0x00, 0xDE, 0x01, 0x03, 0x09, 0xD9, 0xA2, 0x06, 0x0C,
+ 0x01, 0x01, 0xDC, 0x01, 0x01, 0xDC, 0x82, 0x2E, 0x01, 0x08, 0x09, 0xDE,
+ 0xA5, 0x06, 0x19, 0x01, 0x0D, 0xDC, 0xA5, 0x01, 0x04, 0x09, 0x26, 0xDC,
+ 0x01, 0x02, 0x09, 0xDC, 0x42, 0x06, 0x03, 0x01, 0x03, 0xDB, 0x43, 0x06,
+ 0x03, 0x01, 0x01, 0xDB, 0xA7, 0x26, 0x06, 0x36, 0x01, 0x0A, 0xDC, 0x01,
+ 0x04, 0x09, 0x26, 0xDC, 0x5F, 0xDC, 0x40, 0x01, 0x00, 0x26, 0x01, 0x82,
+ 0x80, 0x80, 0x80, 0x00, 0x17, 0x06, 0x0A, 0x01, 0xFD, 0xFF, 0xFF, 0xFF,
+ 0x7F, 0x17, 0x01, 0x1D, 0xDC, 0x26, 0x01, 0x20, 0x0A, 0x06, 0x0C, 0xA0,
+ 0x11, 0x01, 0x01, 0x17, 0x06, 0x02, 0x26, 0xDC, 0x5C, 0x04, 0x6E, 0x60,
+ 0x04, 0x01, 0x25, 0xA3, 0x06, 0x0A, 0x01, 0x0B, 0xDC, 0x01, 0x02, 0xDC,
+ 0x01, 0x82, 0x00, 0xDC, 0x27, 0x26, 0x06, 0x1F, 0x01, 0x10, 0xDC, 0x01,
+ 0x04, 0x09, 0x26, 0xDC, 0x5F, 0xDC, 0x85, 0x2C, 0x01, 0x00, 0xA0, 0x0F,
+ 0x06, 0x0A, 0x26, 0x1E, 0x26, 0xDE, 0x84, 0x44, 0xD8, 0x5C, 0x04, 0x72,
+ 0x60, 0x04, 0x01, 0x25, 0x02, 0x01, 0x58, 0x05, 0x11, 0x01, 0x15, 0xDC,
+ 0x02, 0x01, 0x26, 0xDC, 0x26, 0x06, 0x06, 0x5D, 0x01, 0x00, 0xDE, 0x04,
+ 0x77, 0x25, 0x00, 0x00, 0x01, 0x10, 0xDE, 0x79, 0x2C, 0x26, 0xCC, 0x06,
+ 0x0C, 0xAB, 0x23, 0x26, 0x5E, 0xDD, 0x26, 0xDC, 0x84, 0x44, 0xD8, 0x04,
+ 0x0D, 0x26, 0xCA, 0x44, 0xAB, 0x22, 0x26, 0x5C, 0xDD, 0x26, 0xDE, 0x84,
+ 0x44, 0xD8, 0x00, 0x00, 0x9C, 0x01, 0x14, 0xDE, 0x01, 0x0C, 0xDD, 0x84,
+ 0x01, 0x0C, 0xD8, 0x00, 0x00, 0x51, 0x26, 0x01, 0x00, 0x0E, 0x06, 0x02,
+ 0x60, 0x00, 0xCE, 0x25, 0x04, 0x73, 0x00, 0x26, 0xDC, 0xD8, 0x00, 0x00,
+ 0x26, 0xDE, 0xD8, 0x00, 0x01, 0x03, 0x00, 0x41, 0x25, 0x26, 0x01, 0x10,
+ 0x17, 0x06, 0x06, 0x01, 0x04, 0xDE, 0x02, 0x00, 0xDE, 0x26, 0x01, 0x08,
+ 0x17, 0x06, 0x06, 0x01, 0x03, 0xDE, 0x02, 0x00, 0xDE, 0x26, 0x01, 0x20,
+ 0x17, 0x06, 0x06, 0x01, 0x05, 0xDE, 0x02, 0x00, 0xDE, 0x26, 0x01, 0x80,
+ 0x40, 0x17, 0x06, 0x06, 0x01, 0x06, 0xDE, 0x02, 0x00, 0xDE, 0x01, 0x04,
+ 0x17, 0x06, 0x06, 0x01, 0x02, 0xDE, 0x02, 0x00, 0xDE, 0x00, 0x00, 0x26,
+ 0x01, 0x08, 0x4F, 0xDE, 0xDE, 0x00, 0x00, 0x26, 0x01, 0x10, 0x4F, 0xDE,
+ 0xDC, 0x00, 0x00, 0x26, 0x52, 0x06, 0x02, 0x25, 0x00, 0xCE, 0x25, 0x04,
+ 0x76
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 15,
+ 20,
+ 25,
+ 30,
+ 35,
+ 40,
+ 44,
+ 48,
+ 52,
+ 56,
+ 60,
+ 64,
+ 68,
+ 72,
+ 76,
+ 80,
+ 84,
+ 88,
+ 92,
+ 96,
+ 100,
+ 104,
+ 108,
+ 112,
+ 116,
+ 120,
+ 124,
+ 129,
+ 134,
+ 139,
+ 144,
+ 149,
+ 154,
+ 159,
+ 164,
+ 169,
+ 174,
+ 179,
+ 184,
+ 189,
+ 194,
+ 199,
+ 204,
+ 209,
+ 214,
+ 219,
+ 224,
+ 229,
+ 234,
+ 239,
+ 244,
+ 249,
+ 254,
+ 259,
+ 264,
+ 269,
+ 274,
+ 279,
+ 284,
+ 289,
+ 294,
+ 303,
+ 316,
+ 320,
+ 345,
+ 351,
+ 370,
+ 381,
+ 422,
+ 542,
+ 546,
+ 611,
+ 626,
+ 637,
+ 655,
+ 684,
+ 694,
+ 730,
+ 740,
+ 818,
+ 832,
+ 838,
+ 897,
+ 916,
+ 951,
+ 1000,
+ 1076,
+ 1103,
+ 1134,
+ 1145,
+ 1497,
+ 1644,
+ 1668,
+ 1884,
+ 1898,
+ 1907,
+ 1911,
+ 2006,
+ 2027,
+ 2083,
+ 2090,
+ 2101,
+ 2117,
+ 2123,
+ 2134,
+ 2169,
+ 2181,
+ 2187,
+ 2202,
+ 2218,
+ 2411,
+ 2420,
+ 2433,
+ 2442,
+ 2449,
+ 2459,
+ 2565,
+ 2590,
+ 2603,
+ 2619,
+ 2637,
+ 2669,
+ 2703,
+ 3071,
+ 3107,
+ 3120,
+ 3134,
+ 3139,
+ 3144,
+ 3210,
+ 3218,
+ 3226
+};
+
+#define T0_INTERPRETED 88
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_ssl_hs_client_init_main, 169)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_ssl_hs_client_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* * */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a * b);
+
+ }
+ break;
+ case 8: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 9: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 10: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 11: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 12: {
+ /* <= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a <= b));
+
+ }
+ break;
+ case 13: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 14: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 15: {
+ /* > */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a > b));
+
+ }
+ break;
+ case 16: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 17: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 18: {
+ /* anchor-dn-append-name */
+
+ size_t len;
+
+ len = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->append_name(
+ CTX->client_auth_vtable, ENG->pad, len);
+ }
+
+ }
+ break;
+ case 19: {
+ /* anchor-dn-end-name */
+
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->end_name(
+ CTX->client_auth_vtable);
+ }
+
+ }
+ break;
+ case 20: {
+ /* anchor-dn-end-name-list */
+
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->end_name_list(
+ CTX->client_auth_vtable);
+ }
+
+ }
+ break;
+ case 21: {
+ /* anchor-dn-start-name */
+
+ size_t len;
+
+ len = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->start_name(
+ CTX->client_auth_vtable, len);
+ }
+
+ }
+ break;
+ case 22: {
+ /* anchor-dn-start-name-list */
+
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->start_name_list(
+ CTX->client_auth_vtable);
+ }
+
+ }
+ break;
+ case 23: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 24: {
+ /* begin-cert */
+
+ if (ENG->chain_len == 0) {
+ T0_PUSHi(-1);
+ } else {
+ ENG->cert_cur = ENG->chain->data;
+ ENG->cert_len = ENG->chain->data_len;
+ ENG->chain ++;
+ ENG->chain_len --;
+ T0_PUSH(ENG->cert_len);
+ }
+
+ }
+ break;
+ case 25: {
+ /* bzero */
+
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ memset(addr, 0, len);
+
+ }
+ break;
+ case 26: {
+ /* can-output? */
+
+ T0_PUSHi(-(ENG->hlen_out > 0));
+
+ }
+ break;
+ case 27: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 28: {
+ /* compute-Finished-inner */
+
+ int prf_id = T0_POP();
+ int from_client = T0_POPi();
+ unsigned char tmp[48];
+ br_tls_prf_seed_chunk seed;
+
+ br_tls_prf_impl prf = br_ssl_engine_get_PRF(ENG, prf_id);
+ seed.data = tmp;
+ if (ENG->session.version >= BR_TLS12) {
+ seed.len = br_multihash_out(&ENG->mhash, prf_id, tmp);
+ } else {
+ br_multihash_out(&ENG->mhash, br_md5_ID, tmp);
+ br_multihash_out(&ENG->mhash, br_sha1_ID, tmp + 16);
+ seed.len = 36;
+ }
+ prf(ENG->pad, 12, ENG->session.master_secret,
+ sizeof ENG->session.master_secret,
+ from_client ? "client finished" : "server finished",
+ 1, &seed);
+
+ }
+ break;
+ case 29: {
+ /* copy-cert-chunk */
+
+ size_t clen;
+
+ clen = ENG->cert_len;
+ if (clen > sizeof ENG->pad) {
+ clen = sizeof ENG->pad;
+ }
+ memcpy(ENG->pad, ENG->cert_cur, clen);
+ ENG->cert_cur += clen;
+ ENG->cert_len -= clen;
+ T0_PUSH(clen);
+
+ }
+ break;
+ case 30: {
+ /* copy-protocol-name */
+
+ size_t idx = T0_POP();
+ size_t len = strlen(ENG->protocol_names[idx]);
+ memcpy(ENG->pad, ENG->protocol_names[idx], len);
+ T0_PUSH(len);
+
+ }
+ break;
+ case 31: {
+ /* data-get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(t0_datablock[addr]);
+
+ }
+ break;
+ case 32: {
+ /* discard-input */
+
+ ENG->hlen_in = 0;
+
+ }
+ break;
+ case 33: {
+ /* do-client-sign */
+
+ size_t sig_len;
+
+ sig_len = make_client_sign(CTX);
+ if (sig_len == 0) {
+ br_ssl_engine_fail(ENG, BR_ERR_INVALID_ALGORITHM);
+ T0_CO();
+ }
+ T0_PUSH(sig_len);
+
+ }
+ break;
+ case 34: {
+ /* do-ecdh */
+
+ unsigned prf_id = T0_POP();
+ unsigned ecdhe = T0_POP();
+ int x;
+
+ x = make_pms_ecdh(CTX, ecdhe, prf_id);
+ if (x < 0) {
+ br_ssl_engine_fail(ENG, -x);
+ T0_CO();
+ } else {
+ T0_PUSH(x);
+ }
+
+ }
+ break;
+ case 35: {
+ /* do-rsa-encrypt */
+
+ int x;
+
+ x = make_pms_rsa(CTX, T0_POP());
+ if (x < 0) {
+ br_ssl_engine_fail(ENG, -x);
+ T0_CO();
+ } else {
+ T0_PUSH(x);
+ }
+
+ }
+ break;
+ case 36: {
+ /* do-static-ecdh */
+
+ unsigned prf_id = T0_POP();
+
+ if (make_pms_static_ecdh(CTX, prf_id) < 0) {
+ br_ssl_engine_fail(ENG, BR_ERR_INVALID_ALGORITHM);
+ T0_CO();
+ }
+
+ }
+ break;
+ case 37: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 38: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 39: {
+ /* ext-ALPN-length */
+
+ size_t u, len;
+
+ if (ENG->protocol_names_num == 0) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ len = 6;
+ for (u = 0; u < ENG->protocol_names_num; u ++) {
+ len += 1 + strlen(ENG->protocol_names[u]);
+ }
+ T0_PUSH(len);
+
+ }
+ break;
+ case 40: {
+ /* fail */
+
+ br_ssl_engine_fail(ENG, (int)T0_POPi());
+ T0_CO();
+
+ }
+ break;
+ case 41: {
+ /* flush-record */
+
+ br_ssl_engine_flush_record(ENG);
+
+ }
+ break;
+ case 42: {
+ /* get-client-chain */
+
+ uint32_t auth_types;
+
+ auth_types = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ br_ssl_client_certificate ux;
+
+ (*CTX->client_auth_vtable)->choose(CTX->client_auth_vtable,
+ CTX, auth_types, &ux);
+ CTX->auth_type = (unsigned char)ux.auth_type;
+ CTX->hash_id = (unsigned char)ux.hash_id;
+ ENG->chain = ux.chain;
+ ENG->chain_len = ux.chain_len;
+ } else {
+ CTX->hash_id = 0;
+ ENG->chain_len = 0;
+ }
+
+ }
+ break;
+ case 43: {
+ /* get-key-type-usages */
+
+ const br_x509_class *xc;
+ const br_x509_pkey *pk;
+ unsigned usages;
+
+ xc = *(ENG->x509ctx);
+ pk = xc->get_pkey(ENG->x509ctx, &usages);
+ if (pk == NULL) {
+ T0_PUSH(0);
+ } else {
+ T0_PUSH(pk->key_type | usages);
+ }
+
+ }
+ break;
+ case 44: {
+ /* get16 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint16_t *)(void *)((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 45: {
+ /* get32 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint32_t *)(void *)((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 46: {
+ /* get8 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 47: {
+ /* has-input? */
+
+ T0_PUSHi(-(ENG->hlen_in != 0));
+
+ }
+ break;
+ case 48: {
+ /* memcmp */
+
+ size_t len = (size_t)T0_POP();
+ void *addr2 = (unsigned char *)ENG + (size_t)T0_POP();
+ void *addr1 = (unsigned char *)ENG + (size_t)T0_POP();
+ int x = memcmp(addr1, addr2, len);
+ T0_PUSH((uint32_t)-(x == 0));
+
+ }
+ break;
+ case 49: {
+ /* memcpy */
+
+ size_t len = (size_t)T0_POP();
+ void *src = (unsigned char *)ENG + (size_t)T0_POP();
+ void *dst = (unsigned char *)ENG + (size_t)T0_POP();
+ memcpy(dst, src, len);
+
+ }
+ break;
+ case 50: {
+ /* mkrand */
+
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ br_hmac_drbg_generate(&ENG->rng, addr, len);
+
+ }
+ break;
+ case 51: {
+ /* more-incoming-bytes? */
+
+ T0_PUSHi(ENG->hlen_in != 0 || !br_ssl_engine_recvrec_finished(ENG));
+
+ }
+ break;
+ case 52: {
+ /* multihash-init */
+
+ br_multihash_init(&ENG->mhash);
+
+ }
+ break;
+ case 53: {
+ /* neg */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(-a);
+
+ }
+ break;
+ case 54: {
+ /* not */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(~a);
+
+ }
+ break;
+ case 55: {
+ /* or */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a | b);
+
+ }
+ break;
+ case 56: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 57: {
+ /* read-chunk-native */
+
+ size_t clen = ENG->hlen_in;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy((unsigned char *)ENG + addr, ENG->hbuf_in, clen);
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_in, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_in += clen;
+ ENG->hlen_in -= clen;
+ }
+
+ }
+ break;
+ case 58: {
+ /* read8-native */
+
+ if (ENG->hlen_in > 0) {
+ unsigned char x;
+
+ x = *ENG->hbuf_in ++;
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ T0_PUSH(x);
+ ENG->hlen_in --;
+ } else {
+ T0_PUSHi(-1);
+ }
+
+ }
+ break;
+ case 59: {
+ /* set-server-curve */
+
+ const br_x509_class *xc;
+ const br_x509_pkey *pk;
+
+ xc = *(ENG->x509ctx);
+ pk = xc->get_pkey(ENG->x509ctx, NULL);
+ CTX->server_curve =
+ (pk->key_type == BR_KEYTYPE_EC) ? pk->key.ec.curve : 0;
+
+ }
+ break;
+ case 60: {
+ /* set16 */
+
+ size_t addr = (size_t)T0_POP();
+ *(uint16_t *)(void *)((unsigned char *)ENG + addr) = (uint16_t)T0_POP();
+
+ }
+ break;
+ case 61: {
+ /* set32 */
+
+ size_t addr = (size_t)T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)ENG + addr) = (uint32_t)T0_POP();
+
+ }
+ break;
+ case 62: {
+ /* set8 */
+
+ size_t addr = (size_t)T0_POP();
+ *((unsigned char *)ENG + addr) = (unsigned char)T0_POP();
+
+ }
+ break;
+ case 63: {
+ /* strlen */
+
+ void *str = (unsigned char *)ENG + (size_t)T0_POP();
+ T0_PUSH((uint32_t)strlen(str));
+
+ }
+ break;
+ case 64: {
+ /* supported-curves */
+
+ uint32_t x = ENG->iec == NULL ? 0 : ENG->iec->supported_curves;
+ T0_PUSH(x);
+
+ }
+ break;
+ case 65: {
+ /* supported-hash-functions */
+
+ int i;
+ unsigned x, num;
+
+ x = 0;
+ num = 0;
+ for (i = br_sha1_ID; i <= br_sha512_ID; i ++) {
+ if (br_multihash_getimpl(&ENG->mhash, i)) {
+ x |= 1U << i;
+ num ++;
+ }
+ }
+ T0_PUSH(x);
+ T0_PUSH(num);
+
+ }
+ break;
+ case 66: {
+ /* supports-ecdsa? */
+
+ T0_PUSHi(-(ENG->iecdsa != 0));
+
+ }
+ break;
+ case 67: {
+ /* supports-rsa-sign? */
+
+ T0_PUSHi(-(ENG->irsavrfy != 0));
+
+ }
+ break;
+ case 68: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ case 69: {
+ /* switch-aesccm-in */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+
+ }
+ break;
+ case 70: {
+ /* switch-aesccm-out */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+
+ }
+ break;
+ case 71: {
+ /* switch-aesgcm-in */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+
+ }
+ break;
+ case 72: {
+ /* switch-aesgcm-out */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+
+ }
+ break;
+ case 73: {
+ /* switch-cbc-in */
+
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_in(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcdec : ENG->ides_cbcdec, cipher_key_len);
+
+ }
+ break;
+ case 74: {
+ /* switch-cbc-out */
+
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_out(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcenc : ENG->ides_cbcenc, cipher_key_len);
+
+ }
+ break;
+ case 75: {
+ /* switch-chapol-in */
+
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_in(ENG, is_client, prf_id);
+
+ }
+ break;
+ case 76: {
+ /* switch-chapol-out */
+
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_out(ENG, is_client, prf_id);
+
+ }
+ break;
+ case 77: {
+ /* test-protocol-name */
+
+ size_t len = T0_POP();
+ size_t u;
+
+ for (u = 0; u < ENG->protocol_names_num; u ++) {
+ const char *name;
+
+ name = ENG->protocol_names[u];
+ if (len == strlen(name) && memcmp(ENG->pad, name, len) == 0) {
+ T0_PUSH(u);
+ T0_RET();
+ }
+ }
+ T0_PUSHi(-1);
+
+ }
+ break;
+ case 78: {
+ /* total-chain-length */
+
+ size_t u;
+ uint32_t total;
+
+ total = 0;
+ for (u = 0; u < ENG->chain_len; u ++) {
+ total += 3 + (uint32_t)ENG->chain[u].data_len;
+ }
+ T0_PUSH(total);
+
+ }
+ break;
+ case 79: {
+ /* u>> */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x >> c);
+
+ }
+ break;
+ case 80: {
+ /* verify-SKE-sig */
+
+ size_t sig_len = T0_POP();
+ int use_rsa = T0_POPi();
+ int hash = T0_POPi();
+
+ T0_PUSH(verify_SKE_sig(CTX, hash, use_rsa, sig_len));
+
+ }
+ break;
+ case 81: {
+ /* write-blob-chunk */
+
+ size_t clen = ENG->hlen_out;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy(ENG->hbuf_out, (unsigned char *)ENG + addr, clen);
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_out, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_out += clen;
+ ENG->hlen_out -= clen;
+ }
+
+ }
+ break;
+ case 82: {
+ /* write8-native */
+
+ unsigned char x;
+
+ x = (unsigned char)T0_POP();
+ if (ENG->hlen_out > 0) {
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ *ENG->hbuf_out ++ = x;
+ ENG->hlen_out --;
+ T0_PUSHi(-1);
+ } else {
+ T0_PUSHi(0);
+ }
+
+ }
+ break;
+ case 83: {
+ /* x509-append */
+
+ const br_x509_class *xc;
+ size_t len;
+
+ xc = *(ENG->x509ctx);
+ len = T0_POP();
+ xc->append(ENG->x509ctx, ENG->pad, len);
+
+ }
+ break;
+ case 84: {
+ /* x509-end-cert */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->end_cert(ENG->x509ctx);
+
+ }
+ break;
+ case 85: {
+ /* x509-end-chain */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ T0_PUSH(xc->end_chain(ENG->x509ctx));
+
+ }
+ break;
+ case 86: {
+ /* x509-start-cert */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->start_cert(ENG->x509ctx, T0_POP());
+
+ }
+ break;
+ case 87: {
+ /* x509-start-chain */
+
+ const br_x509_class *xc;
+ uint32_t bc;
+
+ bc = T0_POP();
+ xc = *(ENG->x509ctx);
+ xc->start_chain(ENG->x509ctx, bc ? ENG->server_name : NULL);
+
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hs_client.t0 b/test/monniaux/BearSSL/src/ssl/ssl_hs_client.t0
new file mode 100644
index 00000000..23b39e71
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hs_client.t0
@@ -0,0 +1,1276 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+\ ----------------------------------------------------------------------
+\ Handshake processing code, for the client.
+\ The common T0 code (ssl_hs_common.t0) shall be read first.
+
+preamble {
+
+/*
+ * This macro evaluates to a pointer to the client context, under that
+ * specific name. It must be noted that since the engine context is the
+ * first field of the br_ssl_client_context structure ('eng'), then
+ * pointers values of both types are interchangeable, modulo an
+ * appropriate cast. This also means that "addresses" computed as offsets
+ * within the structure work for both kinds of context.
+ */
+#define CTX ((br_ssl_client_context *)ENG)
+
+/*
+ * Generate the pre-master secret for RSA key exchange, and encrypt it
+ * with the server's public key. Returned value is either the encrypted
+ * data length (in bytes), or -x on error, with 'x' being an error code.
+ *
+ * This code assumes that the public key has been already verified (it
+ * was properly obtained by the X.509 engine, and it has the right type,
+ * i.e. it is of type RSA and suitable for encryption).
+ */
+static int
+make_pms_rsa(br_ssl_client_context *ctx, int prf_id)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ const unsigned char *n;
+ unsigned char *pms;
+ size_t nlen, u;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+
+ /*
+ * Compute actual RSA key length, in case there are leading zeros.
+ */
+ n = pk->key.rsa.n;
+ nlen = pk->key.rsa.nlen;
+ while (nlen > 0 && *n == 0) {
+ n ++;
+ nlen --;
+ }
+
+ /*
+ * We need at least 59 bytes (48 bytes for pre-master secret, and
+ * 11 bytes for the PKCS#1 type 2 padding). Note that the X.509
+ * minimal engine normally blocks RSA keys shorter than 128 bytes,
+ * so this is mostly for public keys provided explicitly by the
+ * caller.
+ */
+ if (nlen < 59) {
+ return -BR_ERR_X509_WEAK_PUBLIC_KEY;
+ }
+ if (nlen > sizeof ctx->eng.pad) {
+ return -BR_ERR_LIMIT_EXCEEDED;
+ }
+
+ /*
+ * Make PMS.
+ */
+ pms = ctx->eng.pad + nlen - 48;
+ br_enc16be(pms, ctx->eng.version_max);
+ br_hmac_drbg_generate(&ctx->eng.rng, pms + 2, 46);
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, pms, 48);
+
+ /*
+ * Apply PKCS#1 type 2 padding.
+ */
+ ctx->eng.pad[0] = 0x00;
+ ctx->eng.pad[1] = 0x02;
+ ctx->eng.pad[nlen - 49] = 0x00;
+ br_hmac_drbg_generate(&ctx->eng.rng, ctx->eng.pad + 2, nlen - 51);
+ for (u = 2; u < nlen - 49; u ++) {
+ while (ctx->eng.pad[u] == 0) {
+ br_hmac_drbg_generate(&ctx->eng.rng,
+ &ctx->eng.pad[u], 1);
+ }
+ }
+
+ /*
+ * Compute RSA encryption.
+ */
+ if (!ctx->irsapub(ctx->eng.pad, nlen, &pk->key.rsa)) {
+ return -BR_ERR_LIMIT_EXCEEDED;
+ }
+ return (int)nlen;
+}
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char *HASH_OID[] = {
+ BR_HASH_OID_SHA1,
+ BR_HASH_OID_SHA224,
+ BR_HASH_OID_SHA256,
+ BR_HASH_OID_SHA384,
+ BR_HASH_OID_SHA512
+};
+
+/*
+ * Check the RSA signature on the ServerKeyExchange message.
+ *
+ * hash hash function ID (2 to 6), or 0 for MD5+SHA-1 (with RSA only)
+ * use_rsa non-zero for RSA signature, zero for ECDSA
+ * sig_len signature length (in bytes); signature value is in the pad
+ *
+ * Returned value is 0 on success, or an error code.
+ */
+static int
+verify_SKE_sig(br_ssl_client_context *ctx,
+ int hash, int use_rsa, size_t sig_len)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ br_multihash_context mhc;
+ unsigned char hv[64], head[4];
+ size_t hv_len;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ br_multihash_zero(&mhc);
+ br_multihash_copyimpl(&mhc, &ctx->eng.mhash);
+ br_multihash_init(&mhc);
+ br_multihash_update(&mhc,
+ ctx->eng.client_random, sizeof ctx->eng.client_random);
+ br_multihash_update(&mhc,
+ ctx->eng.server_random, sizeof ctx->eng.server_random);
+ head[0] = 3;
+ head[1] = 0;
+ head[2] = ctx->eng.ecdhe_curve;
+ head[3] = ctx->eng.ecdhe_point_len;
+ br_multihash_update(&mhc, head, sizeof head);
+ br_multihash_update(&mhc,
+ ctx->eng.ecdhe_point, ctx->eng.ecdhe_point_len);
+ if (hash) {
+ hv_len = br_multihash_out(&mhc, hash, hv);
+ if (hv_len == 0) {
+ return BR_ERR_INVALID_ALGORITHM;
+ }
+ } else {
+ if (!br_multihash_out(&mhc, br_md5_ID, hv)
+ || !br_multihash_out(&mhc, br_sha1_ID, hv + 16))
+ {
+ return BR_ERR_INVALID_ALGORITHM;
+ }
+ hv_len = 36;
+ }
+ if (use_rsa) {
+ unsigned char tmp[64];
+ const unsigned char *hash_oid;
+
+ if (hash) {
+ hash_oid = HASH_OID[hash - 2];
+ } else {
+ hash_oid = NULL;
+ }
+ if (!ctx->eng.irsavrfy(ctx->eng.pad, sig_len,
+ hash_oid, hv_len, &pk->key.rsa, tmp)
+ || memcmp(tmp, hv, hv_len) != 0)
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ } else {
+ if (!ctx->eng.iecdsa(ctx->eng.iec, hv, hv_len, &pk->key.ec,
+ ctx->eng.pad, sig_len))
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Perform client-side ECDH (or ECDHE). The point that should be sent to
+ * the server is written in the pad; returned value is either the point
+ * length (in bytes), or -x on error, with 'x' being an error code.
+ *
+ * The point _from_ the server is taken from ecdhe_point[] if 'ecdhe'
+ * is non-zero, or from the X.509 engine context if 'ecdhe' is zero
+ * (for static ECDH).
+ */
+static int
+make_pms_ecdh(br_ssl_client_context *ctx, unsigned ecdhe, int prf_id)
+{
+ int curve;
+ unsigned char key[66], point[133];
+ const unsigned char *order, *point_src;
+ size_t glen, olen, point_len, xoff, xlen;
+ unsigned char mask;
+
+ if (ecdhe) {
+ curve = ctx->eng.ecdhe_curve;
+ point_src = ctx->eng.ecdhe_point;
+ point_len = ctx->eng.ecdhe_point_len;
+ } else {
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ curve = pk->key.ec.curve;
+ point_src = pk->key.ec.q;
+ point_len = pk->key.ec.qlen;
+ }
+ if ((ctx->eng.iec->supported_curves & ((uint32_t)1 << curve)) == 0) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ /*
+ * We need to generate our key, as a non-zero random value which
+ * is lower than the curve order, in a "large enough" range. We
+ * force top bit to 0 and bottom bit to 1, which guarantees that
+ * the value is in the proper range.
+ */
+ order = ctx->eng.iec->order(curve, &olen);
+ mask = 0xFF;
+ while (mask >= order[0]) {
+ mask >>= 1;
+ }
+ br_hmac_drbg_generate(&ctx->eng.rng, key, olen);
+ key[0] &= mask;
+ key[olen - 1] |= 0x01;
+
+ /*
+ * Compute the common ECDH point, whose X coordinate is the
+ * pre-master secret.
+ */
+ ctx->eng.iec->generator(curve, &glen);
+ if (glen != point_len) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ memcpy(point, point_src, glen);
+ if (!ctx->eng.iec->mul(point, glen, key, olen, curve)) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+
+ /*
+ * The pre-master secret is the X coordinate.
+ */
+ xoff = ctx->eng.iec->xoff(curve, &xlen);
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, point + xoff, xlen);
+
+ ctx->eng.iec->mulgen(point, key, olen, curve);
+ memcpy(ctx->eng.pad, point, glen);
+ return (int)glen;
+}
+
+/*
+ * Perform full static ECDH. This occurs only in the context of client
+ * authentication with certificates: the server uses an EC public key,
+ * the cipher suite is of type ECDH (not ECDHE), the server requested a
+ * client certificate and accepts static ECDH, the client has a
+ * certificate with an EC public key in the same curve, and accepts
+ * static ECDH as well.
+ *
+ * Returned value is 0 on success, -1 on error.
+ */
+static int
+make_pms_static_ecdh(br_ssl_client_context *ctx, int prf_id)
+{
+ unsigned char point[133];
+ size_t point_len;
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ point_len = pk->key.ec.qlen;
+ if (point_len > sizeof point) {
+ return -1;
+ }
+ memcpy(point, pk->key.ec.q, point_len);
+ if (!(*ctx->client_auth_vtable)->do_keyx(
+ ctx->client_auth_vtable, point, &point_len))
+ {
+ return -1;
+ }
+ br_ssl_engine_compute_master(&ctx->eng,
+ prf_id, point, point_len);
+ return 0;
+}
+
+/*
+ * Compute the client-side signature. This is invoked only when a
+ * signature-based client authentication was selected. The computed
+ * signature is in the pad; its length (in bytes) is returned. On
+ * error, 0 is returned.
+ */
+static size_t
+make_client_sign(br_ssl_client_context *ctx)
+{
+ size_t hv_len;
+
+ /*
+ * Compute hash of handshake messages so far. This "cannot" fail
+ * because the list of supported hash functions provided to the
+ * client certificate handler was trimmed to include only the
+ * hash functions that the multi-hasher supports.
+ */
+ if (ctx->hash_id) {
+ hv_len = br_multihash_out(&ctx->eng.mhash,
+ ctx->hash_id, ctx->eng.pad);
+ } else {
+ br_multihash_out(&ctx->eng.mhash,
+ br_md5_ID, ctx->eng.pad);
+ br_multihash_out(&ctx->eng.mhash,
+ br_sha1_ID, ctx->eng.pad + 16);
+ hv_len = 36;
+ }
+ return (*ctx->client_auth_vtable)->do_sign(
+ ctx->client_auth_vtable, ctx->hash_id, hv_len,
+ ctx->eng.pad, sizeof ctx->eng.pad);
+}
+
+}
+
+\ =======================================================================
+
+: addr-ctx:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(br_ssl_client_context, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr-ctx: min_clienthello_len
+addr-ctx: hashes
+addr-ctx: auth_type
+addr-ctx: hash_id
+
+\ Length of the Secure Renegotiation extension. This is 5 for the
+\ first handshake, 17 for a renegotiation (if the server supports the
+\ extension), or 0 if we know that the server does not support the
+\ extension.
+: ext-reneg-length ( -- n )
+ addr-reneg get8 dup if 1 - 17 * else drop 5 then ;
+
+\ Length of SNI extension.
+: ext-sni-length ( -- len )
+ addr-server_name strlen dup if 9 + then ;
+
+\ Length of Maximum Fragment Length extension.
+: ext-frag-length ( -- len )
+ addr-log_max_frag_len get8 14 = if 0 else 5 then ;
+
+\ Length of Signatures extension.
+: ext-signatures-length ( -- len )
+ supported-hash-functions { num } drop 0
+ supports-rsa-sign? if num + then
+ supports-ecdsa? if num + then
+ dup if 1 << 6 + then ;
+
+\ Write supported hash functions ( sign -- )
+: write-hashes
+ { sign }
+ supported-hash-functions drop
+ \ We advertise hash functions in the following preference order:
+ \ SHA-256 SHA-224 SHA-384 SHA-512 SHA-1
+ \ Rationale:
+ \ -- SHA-256 and SHA-224 are more efficient on 32-bit architectures
+ \ -- SHA-1 is less than ideally collision-resistant
+ dup 0x10 and if 4 write8 sign write8 then
+ dup 0x08 and if 3 write8 sign write8 then
+ dup 0x20 and if 5 write8 sign write8 then
+ dup 0x40 and if 6 write8 sign write8 then
+ 0x04 and if 2 write8 sign write8 then ;
+
+\ Length of Supported Curves extension.
+: ext-supported-curves-length ( -- len )
+ supported-curves dup if
+ 0 { x }
+ begin dup while
+ dup 1 and x + >x
+ 1 >>
+ repeat
+ drop x 1 << 6 +
+ then ;
+
+\ Length of Supported Point Formats extension.
+: ext-point-format-length ( -- len )
+ supported-curves if 6 else 0 then ;
+
+\ Length of ALPN extension.
+cc: ext-ALPN-length ( -- len ) {
+ size_t u, len;
+
+ if (ENG->protocol_names_num == 0) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ len = 6;
+ for (u = 0; u < ENG->protocol_names_num; u ++) {
+ len += 1 + strlen(ENG->protocol_names[u]);
+ }
+ T0_PUSH(len);
+}
+
+\ Write handshake message: ClientHello
+: write-ClientHello ( -- )
+ { ; total-ext-length }
+
+ \ Compute length for extensions (without the general two-byte header).
+ \ This does not take padding extension into account.
+ ext-reneg-length ext-sni-length + ext-frag-length +
+ ext-signatures-length +
+ ext-supported-curves-length + ext-point-format-length +
+ ext-ALPN-length +
+ >total-ext-length
+
+ \ ClientHello type
+ 1 write8
+
+ \ Compute and write length
+ 39 addr-session_id_len get8 + addr-suites_num get8 1 << +
+ total-ext-length if 2+ total-ext-length + then
+ \ Compute padding (if requested).
+ addr-min_clienthello_len get16 over - dup 0> if
+ \ We well add a Pad ClientHello extension, which has its
+ \ own header (4 bytes) and might be the only extension
+ \ (2 extra bytes for the extension list header).
+ total-ext-length ifnot swap 2+ swap 2- then
+ \ Account for the extension header.
+ 4 - dup 0< if drop 0 then
+ \ Adjust total extension length.
+ dup 4 + total-ext-length + >total-ext-length
+ \ Adjust ClientHello length.
+ swap 4 + over + swap
+ else
+ drop
+ -1
+ then
+ { ext-padding-amount }
+ write24
+
+ \ Protocol version
+ addr-version_max get16 write16
+
+ \ Client random
+ addr-client_random 4 bzero
+ addr-client_random 4 + 28 mkrand
+ addr-client_random 32 write-blob
+
+ \ Session ID
+ addr-session_id addr-session_id_len get8 write-blob-head8
+
+ \ Supported cipher suites. We also check here that we indeed
+ \ support all these suites.
+ addr-suites_num get8 dup 1 << write16
+ addr-suites_buf swap
+ begin
+ dup while 1-
+ over get16
+ dup suite-supported? ifnot ERR_BAD_CIPHER_SUITE fail then
+ write16
+ swap 2+ swap
+ repeat
+ 2drop
+
+ \ Compression methods (only "null" compression)
+ 1 write8 0 write8
+
+ \ Extensions
+ total-ext-length if
+ total-ext-length write16
+ ext-reneg-length if
+ 0xFF01 write16 \ extension type (0xFF01)
+ addr-saved_finished
+ ext-reneg-length 4 - dup write16 \ extension length
+ 1- write-blob-head8 \ verify data
+ then
+ ext-sni-length if
+ 0x0000 write16 \ extension type (0)
+ addr-server_name
+ ext-sni-length 4 - dup write16 \ extension length
+ 2 - dup write16 \ ServerNameList length
+ 0 write8 \ name type: host_name
+ 3 - write-blob-head16 \ the name itself
+ then
+ ext-frag-length if
+ 0x0001 write16 \ extension type (1)
+ 0x0001 write16 \ extension length
+ addr-log_max_frag_len get8 8 - write8
+ then
+ ext-signatures-length if
+ 0x000D write16 \ extension type (13)
+ ext-signatures-length 4 - dup write16 \ extension length
+ 2 - write16 \ list length
+ supports-ecdsa? if 3 write-hashes then
+ supports-rsa-sign? if 1 write-hashes then
+ then
+ \ TODO: add an API to specify preference order for curves.
+ \ Right now we send Curve25519 first, then other curves in
+ \ increasing ID values (hence P-256 in second).
+ ext-supported-curves-length dup if
+ 0x000A write16 \ extension type (10)
+ 4 - dup write16 \ extension length
+ 2- write16 \ list length
+ supported-curves 0
+ dup 0x20000000 and if
+ 0xDFFFFFFF and 29 write16
+ then
+ begin dup 32 < while
+ dup2 >> 1 and if dup write16 then
+ 1+
+ repeat
+ 2drop
+ else
+ drop
+ then
+ ext-point-format-length if
+ 0x000B write16 \ extension type (11)
+ 0x0002 write16 \ extension length
+ 0x0100 write16 \ value: 1 format: uncompressed
+ then
+ ext-ALPN-length dup if
+ 0x0010 write16 \ extension type (16)
+ 4 - dup write16 \ extension length
+ 2- write16 \ list length
+ addr-protocol_names_num get16 0
+ begin
+ dup2 > while
+ dup copy-protocol-name
+ dup write8 addr-pad swap write-blob
+ 1+
+ repeat
+ 2drop
+ else
+ drop
+ then
+ ext-padding-amount 0< ifnot
+ 0x0015 write16 \ extension value (21)
+ ext-padding-amount
+ dup write16 \ extension length
+ begin dup while
+ 1- 0 write8 repeat \ value (only zeros)
+ drop
+ then
+ then
+ ;
+
+\ =======================================================================
+
+\ Parse server SNI extension. If present, then it should be empty.
+: read-server-sni ( lim -- lim )
+ read16 if ERR_BAD_SNI fail then ;
+
+\ Parse server Max Fragment Length extension. If present, then it should
+\ advertise the same length as the client. Note that whether the server
+\ sends it or not changes nothing for us: we won't send any record larger
+\ than the advertised value anyway, and we will accept incoming records
+\ up to our input buffer length.
+: read-server-frag ( lim -- lim )
+ read16 1 = ifnot ERR_BAD_FRAGLEN fail then
+ read8 8 + addr-log_max_frag_len get8 = ifnot ERR_BAD_FRAGLEN fail then ;
+
+\ Parse server Secure Renegotiation extension. This is called only if
+\ the client sent that extension, so we only have two cases to
+\ distinguish: first handshake, and renegotiation; in the latter case,
+\ we know that the server supports the extension, otherwise the client
+\ would not have sent it.
+: read-server-reneg ( lim -- lim )
+ read16
+ addr-reneg get8 ifnot
+ \ "reneg" is 0, so this is a first handshake. The server's
+ \ extension MUST be empty. We also learn that the server
+ \ supports the extension.
+ 1 = ifnot ERR_BAD_SECRENEG fail then
+ read8 0 = ifnot ERR_BAD_SECRENEG fail then
+ 2 addr-reneg set8
+ else
+ \ "reneg" is non-zero, and we sent an extension, so it must
+ \ be 2 and this is a renegotiation. We must verify that
+ \ the extension contents have length exactly 24 bytes and
+ \ match the saved client and server "Finished".
+ 25 = ifnot ERR_BAD_SECRENEG fail then
+ read8 24 = ifnot ERR_BAD_SECRENEG fail then
+ addr-pad 24 read-blob
+ addr-saved_finished addr-pad 24 memcmp ifnot
+ ERR_BAD_SECRENEG fail
+ then
+ then ;
+
+\ Read the ALPN extension from the server. It must contain a single name,
+\ and that name must match one of our names.
+: read-ALPN-from-server ( lim -- lim )
+ \ Extension contents length.
+ read16 open-elt
+ \ Length of list of names.
+ read16 open-elt
+ \ There should be a single name.
+ read8 addr-pad swap dup { len } read-blob
+ close-elt
+ close-elt
+ len test-protocol-name dup 0< if
+ 3 flag? if ERR_UNEXPECTED fail then
+ drop
+ else
+ 1+ addr-selected_protocol set16
+ then ;
+
+\ Save a value in a 16-bit field, or check it in case of session resumption.
+: check-resume ( val addr resume -- )
+ if get16 = ifnot ERR_RESUME_MISMATCH fail then else set16 then ;
+
+cc: DEBUG-BLOB ( addr len -- ) {
+ extern int printf(const char *fmt, ...);
+
+ size_t len = T0_POP();
+ unsigned char *buf = (unsigned char *)CTX + T0_POP();
+ size_t u;
+
+ printf("BLOB:");
+ for (u = 0; u < len; u ++) {
+ if (u % 16 == 0) {
+ printf("\n ");
+ }
+ printf(" %02x", buf[u]);
+ }
+ printf("\n");
+}
+
+\ Parse incoming ServerHello. Returned value is true (-1) on session
+\ resumption.
+: read-ServerHello ( -- bool )
+ \ Get header, and check message type.
+ read-handshake-header 2 = ifnot ERR_UNEXPECTED fail then
+
+ \ Get protocol version.
+ read16 { version }
+ version addr-version_min get16 < version addr-version_max get16 > or if
+ ERR_UNSUPPORTED_VERSION fail
+ then
+
+ \ Enforce chosen version for subsequent records in both directions.
+ version addr-version_in get16 <> if ERR_BAD_VERSION fail then
+ version addr-version_out set16
+
+ \ Server random.
+ addr-server_random 32 read-blob
+
+ \ The "session resumption" flag.
+ 0 { resume }
+
+ \ Session ID.
+ read8 { idlen }
+ idlen 32 > if ERR_OVERSIZED_ID fail then
+ addr-pad idlen read-blob
+ idlen addr-session_id_len get8 = idlen 0 > and if
+ addr-session_id addr-pad idlen memcmp if
+ \ Server session ID is non-empty and matches what
+ \ we sent, so this is a session resumption.
+ -1 >resume
+ then
+ then
+ addr-session_id addr-pad idlen memcpy
+ idlen addr-session_id_len set8
+
+ \ Record version.
+ version addr-version resume check-resume
+
+ \ Cipher suite. We check that it is part of the list of cipher
+ \ suites that we advertised.
+ read16
+ dup scan-suite 0< if ERR_BAD_CIPHER_SUITE fail then
+ \ Also check that the cipher suite is compatible with the
+ \ announced version: suites that don't use HMAC/SHA-1 are
+ \ for TLS-1.2 only, not older versions.
+ dup use-tls12? version 0x0303 < and if ERR_BAD_CIPHER_SUITE fail then
+ addr-cipher_suite resume check-resume
+
+ \ Compression method. Should be 0 (no compression).
+ read8 if ERR_BAD_COMPRESSION fail then
+
+ \ Parse extensions (if any). If there is no extension, then the
+ \ read limit (on the TOS) should be 0 at that point.
+ dup if
+ \ Length of extension list.
+ \ message size.
+ read16 open-elt
+
+ \ Enumerate extensions. For each of them, check that we
+ \ sent an extension of that type, and did not see it
+ \ yet; and then process it.
+ ext-sni-length { ok-sni }
+ ext-reneg-length { ok-reneg }
+ ext-frag-length { ok-frag }
+ ext-signatures-length { ok-signatures }
+ ext-supported-curves-length { ok-curves }
+ ext-point-format-length { ok-points }
+ ext-ALPN-length { ok-ALPN }
+ begin dup while
+ read16
+ case
+ \ Server Name Indication. The server may
+ \ send such an extension if it uses the SNI
+ \ from the client, but that "response
+ \ extension" is supposed to be empty.
+ 0x0000 of
+ ok-sni ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-sni
+ read-server-sni
+ endof
+
+ \ Max Frag Length. The contents shall be
+ \ a single byte whose value matches the one
+ \ sent by the client.
+ 0x0001 of
+ ok-frag ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-frag
+ read-server-frag
+ endof
+
+ \ Secure Renegotiation.
+ 0xFF01 of
+ ok-reneg ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-reneg
+ read-server-reneg
+ endof
+
+ \ Signature Algorithms.
+ \ Normally, the server should never send this
+ \ extension (so says RFC 5246 #7.4.1.4.1),
+ \ but some existing servers do.
+ 0x000D of
+ ok-signatures ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-signatures
+ read-ignore-16
+ endof
+
+ \ Supported Curves.
+ 0x000A of
+ ok-curves ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-curves
+ read-ignore-16
+ endof
+
+ \ Supported Point Formats.
+ 0x000B of
+ ok-points ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-points
+ read-ignore-16
+ endof
+
+ \ ALPN.
+ 0x0010 of
+ ok-ALPN ifnot
+ ERR_EXTRA_EXTENSION fail
+ then
+ 0 >ok-ALPN
+ read-ALPN-from-server
+ endof
+
+ ERR_EXTRA_EXTENSION fail
+ endcase
+ repeat
+
+ \ If we sent a secure renegotiation extension but did not
+ \ receive a response, then the server does not support
+ \ secure renegotiation. This is a hard failure if this
+ \ is a renegotiation.
+ ok-reneg if
+ ok-reneg 5 > if ERR_BAD_SECRENEG fail then
+ 1 addr-reneg set8
+ then
+ close-elt
+ else
+ \ No extension received at all, so the server does not
+ \ support secure renegotiation. This is a hard failure
+ \ if the server was previously known to support it (i.e.
+ \ this is a renegotiation).
+ ext-reneg-length 5 > if ERR_BAD_SECRENEG fail then
+ 1 addr-reneg set8
+ then
+ close-elt
+ resume
+ ;
+
+cc: set-server-curve ( -- ) {
+ const br_x509_class *xc;
+ const br_x509_pkey *pk;
+
+ xc = *(ENG->x509ctx);
+ pk = xc->get_pkey(ENG->x509ctx, NULL);
+ CTX->server_curve =
+ (pk->key_type == BR_KEYTYPE_EC) ? pk->key.ec.curve : 0;
+}
+
+\ Read Certificate message from server.
+: read-Certificate-from-server ( -- )
+ addr-cipher_suite get16 expected-key-type
+ -1 read-Certificate
+ dup 0< if neg fail then
+ dup ifnot ERR_UNEXPECTED fail then
+ over and <> if ERR_WRONG_KEY_USAGE fail then
+
+ \ Set server curve (used for static ECDH).
+ set-server-curve ;
+
+\ Verify signature on ECDHE point sent by the server.
+\ 'hash' is the hash function to use (1 to 6, or 0 for RSA with MD5+SHA-1)
+\ 'use-rsa' is 0 for ECDSA, -1 for for RSA
+\ 'sig-len' is the signature length (in bytes)
+\ The signature itself is in the pad.
+cc: verify-SKE-sig ( hash use-rsa sig-len -- err ) {
+ size_t sig_len = T0_POP();
+ int use_rsa = T0_POPi();
+ int hash = T0_POPi();
+
+ T0_PUSH(verify_SKE_sig(CTX, hash, use_rsa, sig_len));
+}
+
+\ Parse ServerKeyExchange
+: read-ServerKeyExchange ( -- )
+ \ Get header, and check message type.
+ read-handshake-header 12 = ifnot ERR_UNEXPECTED fail then
+
+ \ We expect a named curve, and we must support it.
+ read8 3 = ifnot ERR_INVALID_ALGORITHM fail then
+ read16 dup addr-ecdhe_curve set8
+ dup 32 >= if ERR_INVALID_ALGORITHM fail then
+ supported-curves swap >> 1 and ifnot ERR_INVALID_ALGORITHM fail then
+
+ \ Read the server point.
+ read8
+ dup 133 > if ERR_INVALID_ALGORITHM fail then
+ dup addr-ecdhe_point_len set8
+ addr-ecdhe_point swap read-blob
+
+ \ If using TLS-1.2+, then the hash function and signature algorithm
+ \ are explicitly provided; the signature algorithm must match what
+ \ the cipher suite specifies. With TLS-1.0 and 1.1, the signature
+ \ algorithm is inferred from the cipher suite, and the hash is
+ \ either MD5+SHA-1 (for RSA signatures) or SHA-1 (for ECDSA).
+ addr-version get16 0x0303 >= { tls1.2+ }
+ addr-cipher_suite get16 use-rsa-ecdhe? { use-rsa }
+ 2 { hash }
+ tls1.2+ if
+ \ Read hash function; accept only the SHA-* identifiers
+ \ (from SHA-1 to SHA-512, no MD5 here).
+ read8
+ dup dup 2 < swap 6 > or if ERR_INVALID_ALGORITHM fail then
+ >hash
+ read8
+ \ Get expected signature algorithm and compare with what
+ \ the server just sent. Expected value is 1 for RSA, 3
+ \ for ECDSA. Note that 'use-rsa' evaluates to -1 for RSA,
+ \ 0 for ECDSA.
+ use-rsa 1 << 3 + = ifnot ERR_INVALID_ALGORITHM fail then
+ else
+ \ For MD5+SHA-1, we set 'hash' to 0.
+ use-rsa if 0 >hash then
+ then
+
+ \ Read signature into the pad.
+ read16 dup { sig-len }
+
+ dup 512 > if ERR_LIMIT_EXCEEDED fail then
+ addr-pad swap read-blob
+
+ \ Verify signature.
+ hash use-rsa sig-len verify-SKE-sig
+ dup if fail then drop
+
+ close-elt ;
+
+\ Client certificate: start processing of anchor names.
+cc: anchor-dn-start-name-list ( -- ) {
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->start_name_list(
+ CTX->client_auth_vtable);
+ }
+}
+
+\ Client certificate: start a new anchor DN (length is 16-bit).
+cc: anchor-dn-start-name ( length -- ) {
+ size_t len;
+
+ len = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->start_name(
+ CTX->client_auth_vtable, len);
+ }
+}
+
+\ Client certificate: push some data for current anchor DN.
+cc: anchor-dn-append-name ( length -- ) {
+ size_t len;
+
+ len = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->append_name(
+ CTX->client_auth_vtable, ENG->pad, len);
+ }
+}
+
+\ Client certificate: end current anchor DN.
+cc: anchor-dn-end-name ( -- ) {
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->end_name(
+ CTX->client_auth_vtable);
+ }
+}
+
+\ Client certificate: end list of anchor DN.
+cc: anchor-dn-end-name-list ( -- ) {
+ if (CTX->client_auth_vtable != NULL) {
+ (*CTX->client_auth_vtable)->end_name_list(
+ CTX->client_auth_vtable);
+ }
+}
+
+\ Client certificate: obtain the client certificate chain.
+cc: get-client-chain ( auth_types -- ) {
+ uint32_t auth_types;
+
+ auth_types = T0_POP();
+ if (CTX->client_auth_vtable != NULL) {
+ br_ssl_client_certificate ux;
+
+ (*CTX->client_auth_vtable)->choose(CTX->client_auth_vtable,
+ CTX, auth_types, &ux);
+ CTX->auth_type = (unsigned char)ux.auth_type;
+ CTX->hash_id = (unsigned char)ux.hash_id;
+ ENG->chain = ux.chain;
+ ENG->chain_len = ux.chain_len;
+ } else {
+ CTX->hash_id = 0;
+ ENG->chain_len = 0;
+ }
+}
+
+\ Parse CertificateRequest. Header has already been read.
+: read-contents-CertificateRequest ( lim -- )
+ \ Read supported client authentication types. We keep only
+ \ RSA, ECDSA, and ECDH.
+ 0 { auth_types }
+ read8 open-elt
+ begin dup while
+ read8 case
+ 1 of 0x0000FF endof
+ 64 of 0x00FF00 endof
+ 65 of 0x010000 endof
+ 66 of 0x020000 endof
+ 0 swap
+ endcase
+ auth_types or >auth_types
+ repeat
+ close-elt
+
+ \ Full static ECDH is allowed only if the cipher suite is ECDH
+ \ (not ECDHE). It would be theoretically feasible to use static
+ \ ECDH on the client side with an ephemeral key pair from the
+ \ server, but RFC 4492 (section 3) forbids it because ECDHE suites
+ \ are supposed to provide forward secrecy, and static ECDH would
+ \ negate that property.
+ addr-cipher_suite get16 use-ecdh? ifnot
+ auth_types 0xFFFF and >auth_types
+ then
+
+ \ Note: if the cipher suite is ECDH, then the X.509 validation
+ \ engine was invoked with the BR_KEYTYPE_EC | BR_KEYTYPE_KEYX
+ \ combination, so the server's public key has already been
+ \ checked to be fit for a key exchange.
+
+ \ With TLS 1.2:
+ \ - rsa_fixed_ecdh and ecdsa_fixed_ecdh are synoymous.
+ \ - There is an explicit list of supported sign+hash.
+ \ With TLS 1.0,
+ addr-version get16 0x0303 >= if
+ \ With TLS 1.2:
+ \ - There is an explicit list of supported sign+hash.
+ \ - The ECDH flags must be adjusted for RSA/ECDSA
+ \ support.
+ read-list-sign-algos dup addr-hashes set32
+
+ \ Trim down the list depending on what hash functions
+ \ we support (since the hashing itself is done by the SSL
+ \ engine, not by the certificate handler).
+ supported-hash-functions drop dup 8 << or 0x030000 or and
+
+ auth_types and
+ auth_types 0x030000 and if
+ dup 0x0000FF and if 0x010000 or then
+ dup 0x00FF00 and if 0x020000 or then
+ then
+ >auth_types
+ else
+ \ TLS 1.0 or 1.1. The hash function is fixed for signatures
+ \ (MD5+SHA-1 for RSA, SHA-1 for ECDSA).
+ auth_types 0x030401 and >auth_types
+ then
+
+ \ Parse list of anchor DN.
+ anchor-dn-start-name-list
+ read16 open-elt
+ begin dup while
+ read16 open-elt
+ dup anchor-dn-start-name
+
+ \ We read the DN by chunks through the pad, so
+ \ as to use the existing reading function (read-blob)
+ \ that also ensures proper hashing.
+ begin
+ dup while
+ dup 256 > if 256 else dup then { len }
+ addr-pad len read-blob
+ len anchor-dn-append-name
+ repeat
+ close-elt
+ anchor-dn-end-name
+ repeat
+ close-elt
+ anchor-dn-end-name-list
+
+ \ We should have reached the message end.
+ close-elt
+
+ \ Obtain the client chain.
+ auth_types get-client-chain
+ ;
+
+\ (obsolete)
+\ Write an empty Certificate message.
+\ : write-empty-Certificate ( -- )
+\ 11 write8 3 write24 0 write24 ;
+
+cc: do-rsa-encrypt ( prf_id -- nlen ) {
+ int x;
+
+ x = make_pms_rsa(CTX, T0_POP());
+ if (x < 0) {
+ br_ssl_engine_fail(ENG, -x);
+ T0_CO();
+ } else {
+ T0_PUSH(x);
+ }
+}
+
+cc: do-ecdh ( echde prf_id -- ulen ) {
+ unsigned prf_id = T0_POP();
+ unsigned ecdhe = T0_POP();
+ int x;
+
+ x = make_pms_ecdh(CTX, ecdhe, prf_id);
+ if (x < 0) {
+ br_ssl_engine_fail(ENG, -x);
+ T0_CO();
+ } else {
+ T0_PUSH(x);
+ }
+}
+
+cc: do-static-ecdh ( prf-id -- ) {
+ unsigned prf_id = T0_POP();
+
+ if (make_pms_static_ecdh(CTX, prf_id) < 0) {
+ br_ssl_engine_fail(ENG, BR_ERR_INVALID_ALGORITHM);
+ T0_CO();
+ }
+}
+
+cc: do-client-sign ( -- sig_len ) {
+ size_t sig_len;
+
+ sig_len = make_client_sign(CTX);
+ if (sig_len == 0) {
+ br_ssl_engine_fail(ENG, BR_ERR_INVALID_ALGORITHM);
+ T0_CO();
+ }
+ T0_PUSH(sig_len);
+}
+
+\ Write ClientKeyExchange.
+: write-ClientKeyExchange ( -- )
+ 16 write8
+ addr-cipher_suite get16
+ dup use-rsa-keyx? if
+ prf-id do-rsa-encrypt
+ dup 2+ write24
+ dup write16
+ addr-pad swap write-blob
+ else
+ dup use-ecdhe? swap prf-id do-ecdh
+ dup 1+ write24
+ dup write8
+ addr-pad swap write-blob
+ then ;
+
+\ Write CertificateVerify. This is invoked only if a client certificate
+\ was requested and sent, and the authentication is not full static ECDH.
+: write-CertificateVerify ( -- )
+ do-client-sign
+ 15 write8 dup
+ addr-version get16 0x0303 >= if
+ 4 + write24
+ addr-hash_id get8 write8
+ addr-auth_type get8 write8
+ else
+ 2+ write24
+ then
+ dup write16 addr-pad swap write-blob ;
+
+\ =======================================================================
+
+\ Perform a handshake.
+: do-handshake ( -- )
+ 0 addr-application_data set8
+ 22 addr-record_type_out set8
+ 0 addr-selected_protocol set16
+ multihash-init
+
+ write-ClientHello
+ flush-record
+ read-ServerHello
+
+ if
+ \ Session resumption.
+ -1 read-CCS-Finished
+ -1 write-CCS-Finished
+
+ else
+
+ \ Not a session resumption.
+
+ \ Read certificate; then check key type and usages against
+ \ cipher suite.
+ read-Certificate-from-server
+
+ \ Depending on cipher suite, we may now expect a
+ \ ServerKeyExchange.
+ addr-cipher_suite get16 expected-key-type
+ CX 0 63 { BR_KEYTYPE_SIGN } and if
+ read-ServerKeyExchange
+ then
+
+ \ Get next header.
+ read-handshake-header
+
+ \ If this is a CertificateRequest, parse it, then read
+ \ next header.
+ dup 13 = if
+ drop read-contents-CertificateRequest
+ read-handshake-header
+ -1
+ else
+ 0
+ then
+ { seen-CR }
+
+ \ At that point, we should have a ServerHelloDone,
+ \ whose length must be 0.
+ 14 = ifnot ERR_UNEXPECTED fail then
+ if ERR_BAD_HELLO_DONE fail then
+
+ \ There should not be more bytes in the record at that point.
+ more-incoming-bytes? if ERR_UNEXPECTED fail then
+
+ seen-CR if
+ \ If the server requested a client certificate, then
+ \ we must write a Certificate message (it may be
+ \ empty).
+ write-Certificate
+
+ \ If using static ECDH, then the ClientKeyExchange
+ \ is empty, and there is no CertificateVerify.
+ \ Otherwise, there is a ClientKeyExchange; there
+ \ will then be a CertificateVerify if a client chain
+ \ was indeed sent.
+ addr-hash_id get8 0xFF = if
+ drop
+ 16 write8 0 write24
+ addr-cipher_suite get16 prf-id do-static-ecdh
+ else
+ write-ClientKeyExchange
+ if write-CertificateVerify then
+ then
+ else
+ write-ClientKeyExchange
+ then
+
+ -1 write-CCS-Finished
+ -1 read-CCS-Finished
+ then
+
+ \ Now we should be invoked only in case of renegotiation.
+ 1 addr-application_data set8
+ 23 addr-record_type_out set8 ;
+
+\ Read a HelloRequest message.
+: read-HelloRequest ( -- )
+ \ A HelloRequest has length 0 and type 0.
+ read-handshake-header-core
+ if ERR_UNEXPECTED fail then
+ if ERR_BAD_HANDSHAKE fail then ;
+
+\ Entry point.
+: main ( -- ! )
+ \ Perform initial handshake.
+ do-handshake
+
+ begin
+ \ Wait for further invocation. At that point, we should
+ \ get either an explicit call for renegotiation, or
+ \ an incoming HelloRequest handshake message.
+ wait-co
+ dup 0x07 and case
+ 0x00 of
+ 0x10 and if
+ do-handshake
+ then
+ endof
+ 0x01 of
+ drop
+ 0 addr-application_data set8
+ read-HelloRequest
+ \ Reject renegotiations if the peer does not
+ \ support secure renegotiation, or if the
+ \ "no renegotiation" flag is set.
+ addr-reneg get8 1 = 1 flag? or if
+ flush-record
+ begin can-output? not while
+ wait-co drop
+ repeat
+ 100 send-warning
+ \ We rejected the renegotiation,
+ \ but the connection is not dead.
+ \ We must set back things into
+ \ working "application data" state.
+ 1 addr-application_data set8
+ 23 addr-record_type_out set8
+ else
+ do-handshake
+ then
+ endof
+ ERR_UNEXPECTED fail
+ endcase
+ again
+ ;
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hs_common.t0 b/test/monniaux/BearSSL/src/ssl/ssl_hs_common.t0
new file mode 100644
index 00000000..4674891c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hs_common.t0
@@ -0,0 +1,1382 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+\ ----------------------------------------------------------------------
+\ This is the common T0 code for processing handshake messages (code that
+\ is used by both client and server).
+
+preamble {
+
+#include <stddef.h>
+#include <string.h>
+
+#include "inner.h"
+
+/*
+ * This macro evaluates to a pointer to the current engine context.
+ */
+#define ENG ((br_ssl_engine_context *)(void *)((unsigned char *)t0ctx - offsetof(br_ssl_engine_context, cpu)))
+
+}
+
+\ IMPLEMENTATION NOTES
+\ ====================
+\
+\ This code handles all records except application data records.
+\ Application data is accepted (incoming records, outgoing payload data)
+\ only when the application_data flag is set, which is done at the end
+\ of the handshake; and it is cleared whenever a renegotiation or a
+\ closure takes place.
+\
+\ Incoming alerts are processed on the fly; fatal alerts terminate the
+\ context, while warnings are ignored, except for close_notify, which
+\ triggers the closure procedure. That procedure never returns (it ends
+\ with an 'ERR_OK fail' call). We can thus make this processing right
+\ into the read functions.
+\
+\ Specific actions from the caller (closure or renegotiation) may happen
+\ only when jumping back into the T0 code, i.e. just after a 'co' call.
+\ Similarly, incoming record type may change only while the caller has
+\ control, so we need to check that type only when returning from a 'co'.
+\
+\ The handshake processor needs to defer back to the caller ('co') only
+\ in one of the following situations:
+\
+\ -- Some handshake data is expected.
+\
+\ -- The handshake is finished, and application data may flow. There may
+\ be some incoming handshake data (HelloRequest from the server). This
+\ is the only situation where a renegotiation call won't be ignored.
+\
+\ -- Some change-cipher-spec data is expected.
+\
+\ -- An alert record is expected. Other types of incoming records will be
+\ skipped.
+\
+\ -- Waiting for the currently accumulated record to be sent and the
+\ output buffer to become free again for another record.
+
+\ Placeholder for handling not yet implemented functionalities.
+: NYI ( -- ! )
+ "NOT YET IMPLEMENTED!" puts cr -1 fail ;
+
+\ Debug function that prints a string (and a newline) on stderr.
+cc: DBG ( addr -- ) {
+ extern void *stderr;
+ extern int fprintf(void *, const char *, ...);
+ fprintf(stderr, "%s\n", &t0_datablock[T0_POPi()]);
+}
+
+\ Debug function that prints a string and an integer value (followed
+\ by a newline) on stderr.
+cc: DBG2 ( addr x -- ) {
+ extern void *stderr;
+ extern int fprintf(void *, const char *, ...);
+ int32_t x = T0_POPi();
+ fprintf(stderr, "%s: %ld (0x%08lX)\n",
+ &t0_datablock[T0_POPi()], (long)x, (unsigned long)(uint32_t)x);
+}
+
+\ Mark the context as failed with a specific error code. This also
+\ returns control to the caller.
+cc: fail ( err -- ! ) {
+ br_ssl_engine_fail(ENG, (int)T0_POPi());
+ T0_CO();
+}
+
+\ Read a byte from the context (address is offset in context).
+cc: get8 ( addr -- val ) {
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*((unsigned char *)ENG + addr));
+}
+
+\ Read a 16-bit word from the context (address is offset in context).
+cc: get16 ( addr -- val ) {
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint16_t *)(void *)((unsigned char *)ENG + addr));
+}
+
+\ Read a 32-bit word from the context (address is offset in context).
+cc: get32 ( addr -- val ) {
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint32_t *)(void *)((unsigned char *)ENG + addr));
+}
+
+\ Set a byte in the context (address is offset in context).
+cc: set8 ( val addr -- ) {
+ size_t addr = (size_t)T0_POP();
+ *((unsigned char *)ENG + addr) = (unsigned char)T0_POP();
+}
+
+\ Set a 16-bit word in the context (address is offset in context).
+cc: set16 ( val addr -- ) {
+ size_t addr = (size_t)T0_POP();
+ *(uint16_t *)(void *)((unsigned char *)ENG + addr) = (uint16_t)T0_POP();
+}
+
+\ Set a 32-bit word in the context (address is offset in context).
+cc: set32 ( val addr -- ) {
+ size_t addr = (size_t)T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)ENG + addr) = (uint32_t)T0_POP();
+}
+
+\ Define a word that evaluates as an address of a field within the
+\ engine context. The field name (C identifier) must follow in the
+\ source. For field 'foo', the defined word is 'addr-foo'.
+: addr-eng:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(br_ssl_engine_context, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr-eng: max_frag_len
+addr-eng: log_max_frag_len
+addr-eng: peer_log_max_frag_len
+addr-eng: shutdown_recv
+addr-eng: record_type_in
+addr-eng: record_type_out
+addr-eng: version_in
+addr-eng: version_out
+addr-eng: application_data
+addr-eng: version_min
+addr-eng: version_max
+addr-eng: suites_buf
+addr-eng: suites_num
+addr-eng: server_name
+addr-eng: client_random
+addr-eng: server_random
+addr-eng: ecdhe_curve
+addr-eng: ecdhe_point
+addr-eng: ecdhe_point_len
+addr-eng: reneg
+addr-eng: saved_finished
+addr-eng: flags
+addr-eng: pad
+addr-eng: action
+addr-eng: alert
+addr-eng: close_received
+addr-eng: protocol_names_num
+addr-eng: selected_protocol
+
+\ Similar to 'addr-eng:', for fields in the 'session' substructure.
+: addr-session-field:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr-session-field: session_id
+addr-session-field: session_id_len
+addr-session-field: version
+addr-session-field: cipher_suite
+addr-session-field: master_secret
+
+\ Check a server flag by index.
+: flag? ( index -- bool )
+ addr-flags get32 swap >> 1 and neg ;
+
+\ Define a word that evaluates to an error constant. This assumes that
+\ all relevant error codes are in the 0..63 range.
+: err:
+ next-word { name }
+ name 0 1 define-word
+ 0 63 "BR_" name + make-CX postpone literal postpone ; ;
+
+err: ERR_OK
+err: ERR_BAD_PARAM
+err: ERR_BAD_STATE
+err: ERR_UNSUPPORTED_VERSION
+err: ERR_BAD_VERSION
+err: ERR_BAD_LENGTH
+err: ERR_TOO_LARGE
+err: ERR_BAD_MAC
+err: ERR_NO_RANDOM
+err: ERR_UNKNOWN_TYPE
+err: ERR_UNEXPECTED
+err: ERR_BAD_CCS
+err: ERR_BAD_ALERT
+err: ERR_BAD_HANDSHAKE
+err: ERR_OVERSIZED_ID
+err: ERR_BAD_CIPHER_SUITE
+err: ERR_BAD_COMPRESSION
+err: ERR_BAD_FRAGLEN
+err: ERR_BAD_SECRENEG
+err: ERR_EXTRA_EXTENSION
+err: ERR_BAD_SNI
+err: ERR_BAD_HELLO_DONE
+err: ERR_LIMIT_EXCEEDED
+err: ERR_BAD_FINISHED
+err: ERR_RESUME_MISMATCH
+err: ERR_INVALID_ALGORITHM
+err: ERR_BAD_SIGNATURE
+err: ERR_WRONG_KEY_USAGE
+err: ERR_NO_CLIENT_AUTH
+
+\ Get supported curves (bit mask).
+cc: supported-curves ( -- x ) {
+ uint32_t x = ENG->iec == NULL ? 0 : ENG->iec->supported_curves;
+ T0_PUSH(x);
+}
+
+\ Get supported hash functions (bit mask and number).
+\ Note: this (on purpose) skips MD5.
+cc: supported-hash-functions ( -- x num ) {
+ int i;
+ unsigned x, num;
+
+ x = 0;
+ num = 0;
+ for (i = br_sha1_ID; i <= br_sha512_ID; i ++) {
+ if (br_multihash_getimpl(&ENG->mhash, i)) {
+ x |= 1U << i;
+ num ++;
+ }
+ }
+ T0_PUSH(x);
+ T0_PUSH(num);
+}
+
+\ Test support for RSA signatures.
+cc: supports-rsa-sign? ( -- bool ) {
+ T0_PUSHi(-(ENG->irsavrfy != 0));
+}
+
+\ Test support for ECDSA signatures.
+cc: supports-ecdsa? ( -- bool ) {
+ T0_PUSHi(-(ENG->iecdsa != 0));
+}
+
+\ (Re)initialise the multihasher.
+cc: multihash-init ( -- ) {
+ br_multihash_init(&ENG->mhash);
+}
+
+\ Flush the current record: if some payload data has been accumulated,
+\ close the record and schedule it for sending. If there is no such data,
+\ this function does nothing.
+cc: flush-record ( -- ) {
+ br_ssl_engine_flush_record(ENG);
+}
+
+\ Yield control to the caller.
+\ When the control is returned to us, react to the new context. Returned
+\ value is a bitwise combination of the following:
+\ 0x01 handshake data is available
+\ 0x02 change-cipher-spec data is available
+\ 0x04 some data other than handshake or change-cipher-spec is available
+\ 0x08 output buffer is ready for a new outgoing record
+\ 0x10 renegotiation is requested and not to be ignored
+\ Flags 0x01, 0x02 and 0x04 are mutually exclusive.
+: wait-co ( -- state )
+ co
+ 0
+ addr-action get8 dup if
+ case
+ 1 of 0 do-close endof
+ 2 of addr-application_data get8 1 = if
+ 0x10 or
+ then endof
+ endcase
+ else
+ drop
+ then
+ addr-close_received get8 ifnot
+ has-input? if
+ addr-record_type_in get8 case
+
+ \ ChangeCipherSpec
+ 20 of 0x02 or endof
+
+ \ Alert -- if close_notify received, trigger
+ \ the closure sequence.
+ 21 of process-alerts if -1 do-close then endof
+
+ \ Handshake
+ 22 of 0x01 or endof
+
+ \ Not CCS, Alert or Handshake.
+ drop 0x04 or 0
+ endcase
+ then
+ then
+ can-output? if 0x08 or then ;
+
+\ Send an alert message. This shall be called only when there is room for
+\ an outgoing record.
+: send-alert ( level alert -- )
+ 21 addr-record_type_out set8
+ swap write8-native drop write8-native drop
+ flush-record ;
+
+\ Send an alert message of level "warning". This shall be called only when
+\ there is room for an outgoing record.
+: send-warning ( alert -- )
+ 1 swap send-alert ;
+
+\ Fail by sending a fatal alert.
+: fail-alert ( alert -- ! )
+ { alert }
+ flush-record
+ begin can-output? not while wait-co drop repeat
+ 2 alert send-alert
+ begin can-output? not while wait-co drop repeat
+ alert 512 + fail ;
+
+\ Perform the close operation:
+\ -- Prevent new application data from the caller.
+\ -- Incoming data is discarded (except alerts).
+\ -- Outgoing data is flushed.
+\ -- A close_notify alert is sent.
+\ -- If 'cnr' is zero, then incoming data is discarded until a close_notify
+\ is received.
+\ -- At the end, the context is terminated.
+\
+\ cnr shall be either 0 or -1.
+: do-close ( cnr -- ! )
+ \ 'cnr' is set to non-zero when a close_notify is received from
+ \ the peer.
+ { cnr }
+
+ \ Get out of application data state. If we were accepting
+ \ application data (flag is 1), and we still expect a close_notify
+ \ from the peer (cnr is 0), then we should set the flag to 2.
+ \ In all other cases, flag should be set to 0.
+ addr-application_data get8 cnr not and 1 << addr-application_data set8
+
+ \ Flush existing payload if any.
+ flush-record
+
+ \ Wait for room to send the close_notify. Since individual records
+ \ can always hold at least 512 bytes, we know that when there is
+ \ room, then there is room for a complete close_notify (two bytes).
+ begin can-output? not while cnr wait-for-close >cnr repeat
+
+ \ Write the close_notify and flush it.
+ \ 21 addr-record_type_out set8
+ \ 1 write8-native 0 write8-native 2drop
+ \ flush-record
+ 0 send-warning
+
+ \ Loop until our record has been sent (we know it's gone when
+ \ writing is again possible) and a close_notify has been received.
+ cnr
+ begin
+ dup can-output? and if ERR_OK fail then
+ wait-for-close
+ again ;
+
+\ Yield control to the engine, with a possible flush. If 'cnr' is 0,
+\ then input is analysed: all input is discarded, until a close_notify
+\ is received.
+: wait-for-close ( cnr -- cnr )
+ co
+ dup ifnot
+ has-input? if
+ addr-record_type_in get8 21 = if
+ drop process-alerts
+ \ If we received a close_notify then we
+ \ no longer accept incoming application
+ \ data records.
+ 0 addr-application_data set8
+ else
+ discard-input
+ then
+ then
+ then ;
+
+\ Test whether there is some accumulated payload that still needs to be
+\ sent.
+cc: payload-to-send? ( -- bool ) {
+ T0_PUSHi(-br_ssl_engine_has_pld_to_send(ENG));
+}
+
+\ Test whether there is some available input data.
+cc: has-input? ( -- bool ) {
+ T0_PUSHi(-(ENG->hlen_in != 0));
+}
+
+\ Test whether some payload bytes may be written.
+cc: can-output? ( -- bool ) {
+ T0_PUSHi(-(ENG->hlen_out > 0));
+}
+
+\ Discard current input entirely.
+cc: discard-input ( -- ) {
+ ENG->hlen_in = 0;
+}
+
+\ Low-level read for one byte. If there is no available byte right
+\ away, then -1 is returned. Otherwise, the byte value is returned.
+\ If the current record type is "handshake" then the read byte is also
+\ injected in the multi-hasher.
+cc: read8-native ( -- x ) {
+ if (ENG->hlen_in > 0) {
+ unsigned char x;
+
+ x = *ENG->hbuf_in ++;
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ T0_PUSH(x);
+ ENG->hlen_in --;
+ } else {
+ T0_PUSHi(-1);
+ }
+}
+
+\ Low-level read for several bytes. On entry, this expects an address
+\ (offset in the engine context) and a length; these values designate
+\ where the chunk should go. Upon exit, the new address and length
+\ are pushed; that output length contains how many bytes could not be
+\ read. If there is no available byte for reading, the address and
+\ length are unchanged.
+\ If the current record type is "handshake" then the read bytes are
+\ injected in the multi-hasher.
+cc: read-chunk-native ( addr len -- addr len ) {
+ size_t clen = ENG->hlen_in;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy((unsigned char *)ENG + addr, ENG->hbuf_in, clen);
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_in, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_in += clen;
+ ENG->hlen_in -= clen;
+ }
+}
+
+\ Process available alert bytes. If a fatal alert is received, then the
+\ context is terminated; otherwise, this returns either true (-1) if a
+\ close_notify was received, false (0) otherwise.
+: process-alerts ( -- bool )
+ 0
+ begin has-input? while read8-native process-alert-byte or repeat
+ dup if 1 addr-shutdown_recv set8 then ;
+
+\ Process an alert byte. Returned value is non-zero if this is a close_notify,
+\ zero otherwise.
+: process-alert-byte ( x -- bool )
+ addr-alert get8 case
+ 0 of
+ \ 'alert' field is 0, so this byte shall be a level.
+ \ Levels shall be 1 (warning) or 2 (fatal); we convert
+ \ all other values to "fatal".
+ dup 1 <> if drop 2 then
+ addr-alert set8 0
+ endof
+ 1 of
+ 0 addr-alert set8
+ \ close_notify has value 0.
+ \ no_renegotiation has value 100, and we treat it
+ \ as a fatal alert.
+ dup 100 = if 256 + fail then
+ 0=
+ endof
+ \ Fatal alert implies context termination.
+ drop 256 + fail
+ endcase ;
+
+\ In general we only deal with handshake data here. Alerts are processed
+\ in specific code right when they are received, and ChangeCipherSpec has
+\ its own handling code. So we need to check that the data is "handshake"
+\ only when returning from a coroutine call.
+
+\ Yield control to the engine. Alerts are processed; if incoming data is
+\ neither handshake or alert, then an error is triggered.
+: wait-for-handshake ( -- )
+ wait-co 0x07 and 0x01 > if ERR_UNEXPECTED fail then ;
+
+\ Flush outgoing data (if any), then wait for the output buffer to be
+\ clear; when this is done, set the output record type to the specified
+\ value.
+: wait-rectype-out ( rectype -- )
+ { rectype }
+ flush-record
+ begin
+ can-output? if rectype addr-record_type_out set8 ret then
+ wait-co drop
+ again ;
+
+\ Read one byte of handshake data. Block until that byte is available.
+\ This does not check any length.
+: read8-nc ( -- x )
+ begin
+ read8-native dup 0< ifnot ret then
+ drop wait-for-handshake
+ again ;
+
+\ Test whether there are some more bytes in the current record. These
+\ bytes have not necessarily been received yet (processing of unencrypted
+\ records may begin before all bytes are received).
+cc: more-incoming-bytes? ( -- bool ) {
+ T0_PUSHi(ENG->hlen_in != 0 || !br_ssl_engine_recvrec_finished(ENG));
+}
+
+\ For reading functions, the TOS is supposed to contain the number of bytes
+\ that can still be read (from encapsulating structure header), and it is
+\ updated.
+
+: check-len ( lim len -- lim )
+ - dup 0< if ERR_BAD_PARAM fail then ;
+
+\ Read one byte of handshake data. This pushes an integer in the 0..255 range.
+: read8 ( lim -- lim x )
+ 1 check-len read8-nc ;
+
+\ Read a 16-bit value (in the 0..65535 range)
+: read16 ( lim -- lim n )
+ 2 check-len read8-nc 8 << read8-nc + ;
+
+\ Read a 24-bit value (in the 0..16777215 range)
+: read24 ( lim -- lim n )
+ 3 check-len read8-nc 8 << read8-nc + 8 << read8-nc + ;
+
+\ Read some bytes. The "address" is an offset within the context
+\ structure.
+: read-blob ( lim addr len -- lim )
+ { addr len }
+ len check-len
+ addr len
+ begin
+ read-chunk-native
+ dup 0 = if 2drop ret then
+ wait-for-handshake
+ again ;
+
+\ Read some bytes and drop them.
+: skip-blob ( lim len -- lim )
+ swap over check-len swap
+ begin dup while read8-nc drop 1- repeat
+ drop ;
+
+\ Read a 16-bit length, then skip exactly that many bytes.
+: read-ignore-16 ( lim -- lim )
+ read16 skip-blob ;
+
+\ Open a substructure: the inner structure length is checked against,
+\ and subtracted, from the output structure current limit.
+: open-elt ( lim len -- lim-outer lim-inner )
+ dup { len }
+ - dup 0< if ERR_BAD_PARAM fail then
+ len ;
+
+\ Close the current structure. This checks that the limit is 0.
+: close-elt ( lim -- )
+ if ERR_BAD_PARAM fail then ;
+
+\ Write one byte of handshake data.
+: write8 ( n -- )
+ begin
+ dup write8-native if drop ret then
+ wait-co drop
+ again ;
+
+\ Low-level write for one byte. On exit, it pushes either -1 (byte was
+\ written) or 0 (no room in output buffer).
+cc: write8-native ( x -- bool ) {
+ unsigned char x;
+
+ x = (unsigned char)T0_POP();
+ if (ENG->hlen_out > 0) {
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ *ENG->hbuf_out ++ = x;
+ ENG->hlen_out --;
+ T0_PUSHi(-1);
+ } else {
+ T0_PUSHi(0);
+ }
+}
+
+\ Write a 16-bit value.
+: write16 ( n -- )
+ dup 8 u>> write8 write8 ;
+
+\ Write a 24-bit value.
+: write24 ( n -- )
+ dup 16 u>> write8 write16 ;
+
+\ Write some bytes. The "address" is an offset within the context
+\ structure.
+: write-blob ( addr len -- )
+ begin
+ write-blob-chunk
+ dup 0 = if 2drop ret then
+ wait-co drop
+ again ;
+
+cc: write-blob-chunk ( addr len -- addr len ) {
+ size_t clen = ENG->hlen_out;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy(ENG->hbuf_out, (unsigned char *)ENG + addr, clen);
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_out, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_out += clen;
+ ENG->hlen_out -= clen;
+ }
+}
+
+\ Write a blob with the length as header (over one byte)
+: write-blob-head8 ( addr len -- )
+ dup write8 write-blob ;
+
+\ Write a blob with the length as header (over two bytes)
+: write-blob-head16 ( addr len -- )
+ dup write16 write-blob ;
+
+\ Perform a byte-to-byte comparison between two blobs. Each blob is
+\ provided as an "address" (offset in the context structure); the
+\ length is common. Returned value is true (-1) if the two blobs are
+\ equal, false (0) otherwise.
+cc: memcmp ( addr1 addr2 len -- bool ) {
+ size_t len = (size_t)T0_POP();
+ void *addr2 = (unsigned char *)ENG + (size_t)T0_POP();
+ void *addr1 = (unsigned char *)ENG + (size_t)T0_POP();
+ int x = memcmp(addr1, addr2, len);
+ T0_PUSH((uint32_t)-(x == 0));
+}
+
+\ Copy bytes between two areas, whose addresses are provided as
+\ offsets in the context structure.
+cc: memcpy ( dst src len -- ) {
+ size_t len = (size_t)T0_POP();
+ void *src = (unsigned char *)ENG + (size_t)T0_POP();
+ void *dst = (unsigned char *)ENG + (size_t)T0_POP();
+ memcpy(dst, src, len);
+}
+
+\ Get string length (zero-terminated). The string address is provided as
+\ an offset relative to the context start. Returned length does not include
+\ the terminated 0.
+cc: strlen ( str -- len ) {
+ void *str = (unsigned char *)ENG + (size_t)T0_POP();
+ T0_PUSH((uint32_t)strlen(str));
+}
+
+\ Fill a buffer with zeros. The buffer address is an offset in the context.
+cc: bzero ( addr len -- ) {
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ memset(addr, 0, len);
+}
+
+\ Scan the list of supported cipher suites for a given value. If found,
+\ then the list index at which it was found is returned; otherwise, -1
+\ is returned.
+: scan-suite ( suite -- index )
+ { suite }
+ addr-suites_num get8 { num }
+ 0
+ begin dup num < while
+ dup 1 << addr-suites_buf + get16 suite = if ret then
+ 1+
+ repeat
+ drop -1 ;
+
+\ =======================================================================
+
+\ Generate random bytes into buffer (address is offset in context).
+cc: mkrand ( addr len -- ) {
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ br_hmac_drbg_generate(&ENG->rng, addr, len);
+}
+
+\ Read a handshake message header: type and length. These are returned
+\ in reverse order (type is TOS, length is below it).
+: read-handshake-header-core ( -- lim type )
+ read8-nc 3 read24 swap drop swap ;
+
+\ Read a handshake message header: type and length. If the header is for
+\ a HelloRequest message, then it is discarded and a new header is read
+\ (repeatedly if necessary).
+: read-handshake-header ( -- lim type )
+ begin
+ read-handshake-header-core dup 0= while
+ drop if ERR_BAD_HANDSHAKE fail then
+ repeat ;
+
+\ =======================================================================
+
+\ Cipher suite processing.
+\
+\ Unfortunately, cipher suite identifiers are attributed mostly arbitrary,
+\ so we have to map the cipher suite numbers we support into aggregate
+\ words that encode the information we need. Table below is organized
+\ as a sequence of pairs of 16-bit words, the first being the cipher suite
+\ identifier, the second encoding the algorithm elements. The suites are
+\ ordered by increasing cipher suite ID, so that fast lookups may be
+\ performed with a binary search (not implemented for the moment, since it
+\ does not appear to matter much in practice).
+\
+\ Algorithm elements are encoded over 4 bits each, in the following order
+\ (most significant to least significant):
+\
+\ -- Server key type:
+\ 0 RSA (RSA key exchange)
+\ 1 ECDHE-RSA (ECDHE key exchange, RSA signature)
+\ 2 ECDHE-ECDSA (ECDHE key exchange, ECDSA signature)
+\ 3 ECDH-RSA (ECDH key exchange, certificate is RSA-signed)
+\ 4 ECDH-ECDSA (ECDH key exchange, certificate is ECDSA-signed)
+\ -- Encryption algorithm:
+\ 0 3DES/CBC
+\ 1 AES-128/CBC
+\ 2 AES-256/CBC
+\ 3 AES-128/GCM
+\ 4 AES-256/GCM
+\ 5 ChaCha20/Poly1305
+\ 6 AES-128/CCM
+\ 7 AES-256/CCM
+\ 8 AES-128/CCM8
+\ 9 AES-256/CCM8
+\ -- MAC algorithm:
+\ 0 none (for suites with AEAD encryption)
+\ 2 HMAC/SHA-1
+\ 4 HMAC/SHA-256
+\ 5 HMAC/SHA-384
+\ -- PRF for TLS-1.2:
+\ 4 with SHA-256
+\ 5 with SHA-384
+\
+\ WARNING: if adding a new cipher suite that does not use SHA-256 for the
+\ PRF (with TLS 1.2), be sure to check the suites_sha384[] array defined
+\ in ssl/ssl_keyexport.c
+
+data: cipher-suite-def
+
+hexb| 000A 0024 | \ TLS_RSA_WITH_3DES_EDE_CBC_SHA
+hexb| 002F 0124 | \ TLS_RSA_WITH_AES_128_CBC_SHA
+hexb| 0035 0224 | \ TLS_RSA_WITH_AES_256_CBC_SHA
+hexb| 003C 0144 | \ TLS_RSA_WITH_AES_128_CBC_SHA256
+hexb| 003D 0244 | \ TLS_RSA_WITH_AES_256_CBC_SHA256
+
+hexb| 009C 0304 | \ TLS_RSA_WITH_AES_128_GCM_SHA256
+hexb| 009D 0405 | \ TLS_RSA_WITH_AES_256_GCM_SHA384
+
+hexb| C003 4024 | \ TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
+hexb| C004 4124 | \ TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
+hexb| C005 4224 | \ TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
+hexb| C008 2024 | \ TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
+hexb| C009 2124 | \ TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
+hexb| C00A 2224 | \ TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
+hexb| C00D 3024 | \ TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
+hexb| C00E 3124 | \ TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
+hexb| C00F 3224 | \ TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
+hexb| C012 1024 | \ TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
+hexb| C013 1124 | \ TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
+hexb| C014 1224 | \ TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
+
+hexb| C023 2144 | \ TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
+hexb| C024 2255 | \ TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
+hexb| C025 4144 | \ TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
+hexb| C026 4255 | \ TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
+hexb| C027 1144 | \ TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
+hexb| C028 1255 | \ TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
+hexb| C029 3144 | \ TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
+hexb| C02A 3255 | \ TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
+hexb| C02B 2304 | \ TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
+hexb| C02C 2405 | \ TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
+hexb| C02D 4304 | \ TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
+hexb| C02E 4405 | \ TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
+hexb| C02F 1304 | \ TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
+hexb| C030 1405 | \ TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
+hexb| C031 3304 | \ TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
+hexb| C032 3405 | \ TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
+
+hexb| C09C 0604 | \ TLS_RSA_WITH_AES_128_CCM
+hexb| C09D 0704 | \ TLS_RSA_WITH_AES_256_CCM
+hexb| C0A0 0804 | \ TLS_RSA_WITH_AES_128_CCM_8
+hexb| C0A1 0904 | \ TLS_RSA_WITH_AES_256_CCM_8
+hexb| C0AC 2604 | \ TLS_ECDHE_ECDSA_WITH_AES_128_CCM
+hexb| C0AD 2704 | \ TLS_ECDHE_ECDSA_WITH_AES_256_CCM
+hexb| C0AE 2804 | \ TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
+hexb| C0AF 2904 | \ TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
+
+hexb| CCA8 1504 | \ TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
+hexb| CCA9 2504 | \ TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
+
+hexb| 0000 | \ List terminator.
+
+\ Convert cipher suite identifier to element words. This returns 0 if
+\ the cipher suite is not known.
+: cipher-suite-to-elements ( suite -- elts )
+ { id }
+ cipher-suite-def
+ begin
+ dup 2+ swap data-get16
+ dup ifnot 2drop 0 ret then
+ id = if data-get16 ret then
+ 2+
+ again ;
+
+\ Check that a given cipher suite is supported. Note that this also
+\ returns true (-1) for the TLS_FALLBACK_SCSV pseudo-ciphersuite.
+: suite-supported? ( suite -- bool )
+ dup 0x5600 = if drop -1 ret then
+ cipher-suite-to-elements 0<> ;
+
+\ Get expected key type for cipher suite. The key type is one of
+\ BR_KEYTYPE_RSA or BR_KEYTYPE_EC, combined with either BR_KEYTYPE_KEYX
+\ (RSA encryption or static ECDH) or BR_KEYTYPE_SIGN (RSA or ECDSA
+\ signature, for ECDHE cipher suites).
+: expected-key-type ( suite -- key-type )
+ cipher-suite-to-elements 12 >>
+ case
+ 0 of CX 0 63 { BR_KEYTYPE_RSA | BR_KEYTYPE_KEYX } endof
+ 1 of CX 0 63 { BR_KEYTYPE_RSA | BR_KEYTYPE_SIGN } endof
+ 2 of CX 0 63 { BR_KEYTYPE_EC | BR_KEYTYPE_SIGN } endof
+ 3 of CX 0 63 { BR_KEYTYPE_EC | BR_KEYTYPE_KEYX } endof
+ 4 of CX 0 63 { BR_KEYTYPE_EC | BR_KEYTYPE_KEYX } endof
+ 0 swap
+ endcase ;
+
+\ Test whether the cipher suite uses RSA key exchange.
+: use-rsa-keyx? ( suite -- bool )
+ cipher-suite-to-elements 12 >> 0= ;
+
+\ Test whether the cipher suite uses ECDHE key exchange, signed with RSA.
+: use-rsa-ecdhe? ( suite -- bool )
+ cipher-suite-to-elements 12 >> 1 = ;
+
+\ Test whether the cipher suite uses ECDHE key exchange, signed with ECDSA.
+: use-ecdsa-ecdhe? ( suite -- bool )
+ cipher-suite-to-elements 12 >> 2 = ;
+
+\ Test whether the cipher suite uses ECDHE key exchange (with RSA or ECDSA).
+: use-ecdhe? ( suite -- bool )
+ cipher-suite-to-elements 12 >> dup 0> swap 3 < and ;
+
+\ Test whether the cipher suite uses ECDH (static) key exchange.
+: use-ecdh? ( suite -- bool )
+ cipher-suite-to-elements 12 >> 2 > ;
+
+\ Get identifier for the PRF (TLS 1.2).
+: prf-id ( suite -- id )
+ cipher-suite-to-elements 15 and ;
+
+\ Test whether a cipher suite is only for TLS-1.2. Cipher suites that
+\ can be used with TLS-1.0 or 1.1 use HMAC/SHA-1. RFC do not formally
+\ forbid using a CBC-based TLS-1.2 cipher suite, e.g. based on HMAC/SHA-256,
+\ with older protocol versions; however, servers should not do that, since
+\ it may confuse clients. Since the server code does not try such games,
+\ for consistency, the client should reject it as well (normal servers
+\ don't do that, so any attempt is a sign of foul play).
+: use-tls12? ( suite -- bool )
+ cipher-suite-to-elements 0xF0 and 0x20 <> ;
+
+\ Switch to negotiated security parameters for input or output.
+: switch-encryption ( is-client for-input -- )
+ { for-input }
+ addr-cipher_suite get16 cipher-suite-to-elements { elts }
+
+ \ prf_id
+ elts 15 and
+
+ \ mac_id
+ elts 4 >> 15 and
+
+ \ cipher type and key length
+ elts 8 >> 15 and case
+ \ 3DES/CBC
+ 0 of 0 24
+ for-input if
+ switch-cbc-in
+ else
+ switch-cbc-out
+ then
+ endof
+
+ \ AES-128/CBC
+ 1 of 1 16
+ for-input if
+ switch-cbc-in
+ else
+ switch-cbc-out
+ then
+ endof
+
+ \ AES-256/CBC
+ 2 of 1 32
+ for-input if
+ switch-cbc-in
+ else
+ switch-cbc-out
+ then
+ endof
+
+ \ AES-128/GCM
+ 3 of drop 16
+ for-input if
+ switch-aesgcm-in
+ else
+ switch-aesgcm-out
+ then
+ endof
+
+ \ AES-256/GCM
+ 4 of drop 32
+ for-input if
+ switch-aesgcm-in
+ else
+ switch-aesgcm-out
+ then
+ endof
+
+ \ ChaCha20+Poly1305
+ 5 of drop
+ for-input if
+ switch-chapol-in
+ else
+ switch-chapol-out
+ then
+ endof
+
+ \ Now we only have AES/CCM suites (6 to 9). Since the
+ \ input is between 0 and 15, and we checked values 0 to 5,
+ \ we only need to reject values larger than 9.
+ dup 9 > if
+ ERR_BAD_PARAM fail
+ then
+
+ \ Stack: is_client prf_id mac_id cipher_id
+ \ We want to remove the mac_id (it is zero for CCM suites)
+ \ and replace the cipher_id with the key and tag lengths.
+ \ The following table applies:
+ \ id key length tag length
+ \ 6 16 16
+ \ 7 32 16
+ \ 8 16 8
+ \ 9 32 8
+ swap drop
+ dup 1 and 4 << 16 + swap
+ 8 and 16 swap -
+ for-input if
+ switch-aesccm-in
+ else
+ switch-aesccm-out
+ then
+ ret
+ endcase
+ ;
+
+cc: switch-cbc-out ( is_client prf_id mac_id aes cipher_key_len -- ) {
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_out(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcenc : ENG->ides_cbcenc, cipher_key_len);
+}
+
+cc: switch-cbc-in ( is_client prf_id mac_id aes cipher_key_len -- ) {
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_in(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcdec : ENG->ides_cbcdec, cipher_key_len);
+}
+
+cc: switch-aesgcm-out ( is_client prf_id cipher_key_len -- ) {
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+}
+
+cc: switch-aesgcm-in ( is_client prf_id cipher_key_len -- ) {
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+}
+
+cc: switch-chapol-out ( is_client prf_id -- ) {
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_out(ENG, is_client, prf_id);
+}
+
+cc: switch-chapol-in ( is_client prf_id -- ) {
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_in(ENG, is_client, prf_id);
+}
+
+cc: switch-aesccm-out ( is_client prf_id cipher_key_len tag_len -- ) {
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+}
+
+cc: switch-aesccm-in ( is_client prf_id cipher_key_len tag_len -- ) {
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+}
+
+\ Write Finished message.
+: write-Finished ( from_client -- )
+ compute-Finished
+ 20 write8 12 write24 addr-pad 12 write-blob ;
+
+\ Read Finished message.
+: read-Finished ( from_client -- )
+ compute-Finished
+ read-handshake-header 20 <> if ERR_UNEXPECTED fail then
+ addr-pad 12 + 12 read-blob
+ close-elt
+ addr-pad dup 12 + 12 memcmp ifnot ERR_BAD_FINISHED fail then ;
+
+\ Compute the "Finished" contents (either the value to send, or the
+\ expected value). The 12-byte string is written in the pad. The
+\ "from_client" value is non-zero for the Finished sent by the client.
+\ The computed value is also saved in the relevant buffer for handling
+\ secure renegotiation.
+: compute-Finished ( from_client -- )
+ dup addr-saved_finished swap ifnot 12 + then swap
+ addr-cipher_suite get16 prf-id compute-Finished-inner
+ addr-pad 12 memcpy ;
+
+cc: compute-Finished-inner ( from_client prf_id -- ) {
+ int prf_id = T0_POP();
+ int from_client = T0_POPi();
+ unsigned char tmp[48];
+ br_tls_prf_seed_chunk seed;
+
+ br_tls_prf_impl prf = br_ssl_engine_get_PRF(ENG, prf_id);
+ seed.data = tmp;
+ if (ENG->session.version >= BR_TLS12) {
+ seed.len = br_multihash_out(&ENG->mhash, prf_id, tmp);
+ } else {
+ br_multihash_out(&ENG->mhash, br_md5_ID, tmp);
+ br_multihash_out(&ENG->mhash, br_sha1_ID, tmp + 16);
+ seed.len = 36;
+ }
+ prf(ENG->pad, 12, ENG->session.master_secret,
+ sizeof ENG->session.master_secret,
+ from_client ? "client finished" : "server finished",
+ 1, &seed);
+}
+
+\ Receive ChangeCipherSpec and Finished from the peer.
+: read-CCS-Finished ( is-client -- )
+ has-input? if
+ addr-record_type_in get8 20 <> if ERR_UNEXPECTED fail then
+ else
+ begin
+ wait-co 0x07 and dup 0x02 <> while
+ if ERR_UNEXPECTED fail then
+ repeat
+ drop
+ then
+ read8-nc 1 <> more-incoming-bytes? or if ERR_BAD_CCS fail then
+ dup 1 switch-encryption
+
+ \ Read and verify Finished from peer.
+ not read-Finished ;
+
+\ Send ChangeCipherSpec and Finished to the peer.
+: write-CCS-Finished ( is-client -- )
+ \ Flush and wait for output buffer to be clear, so that we may
+ \ write our ChangeCipherSpec. We must switch immediately after
+ \ triggering the flush.
+ 20 wait-rectype-out
+ 1 write8
+ flush-record
+ dup 0 switch-encryption
+ 22 wait-rectype-out
+ write-Finished
+ flush-record ;
+
+\ Read and parse a list of supported signature algorithms (with hash
+\ functions). The resulting bit field is returned.
+: read-list-sign-algos ( lim -- lim value )
+ 0 { hashes }
+ read16 open-elt
+ begin dup while
+ read8 { hash } read8 { sign }
+
+ \ If hash is 0x08 then this is a "new algorithm" identifier,
+ \ and we set the corresponding bit if it is in the 0..15
+ \ range. Otherwise, we keep the value only if the signature
+ \ is either 1 (RSA) or 3 (ECDSA), and the hash is one of the
+ \ SHA-* functions (2 to 6). Note that we reject MD5.
+ hash 8 = if
+ sign 15 <= if
+ 1 sign 16 + << hashes or >hashes
+ then
+ else
+ hash 2 >= hash 6 <= and
+ sign 1 = sign 3 = or
+ and if
+ hashes 1 sign 1- 2 << hash + << or >hashes
+ then
+ then
+ repeat
+ close-elt
+ hashes ;
+
+\ =======================================================================
+
+\ Compute total chain length. This includes the individual certificate
+\ headers, but not the total chain header. This also sets the cert_cur,
+\ cert_len and chain_len context fields.
+cc: total-chain-length ( -- len ) {
+ size_t u;
+ uint32_t total;
+
+ total = 0;
+ for (u = 0; u < ENG->chain_len; u ++) {
+ total += 3 + (uint32_t)ENG->chain[u].data_len;
+ }
+ T0_PUSH(total);
+}
+
+\ Get length for current certificate in the chain; if the chain end was
+\ reached, then this returns -1.
+cc: begin-cert ( -- len ) {
+ if (ENG->chain_len == 0) {
+ T0_PUSHi(-1);
+ } else {
+ ENG->cert_cur = ENG->chain->data;
+ ENG->cert_len = ENG->chain->data_len;
+ ENG->chain ++;
+ ENG->chain_len --;
+ T0_PUSH(ENG->cert_len);
+ }
+}
+
+\ Copy a chunk of certificate data into the pad. Returned value is the
+\ chunk length, or 0 if the certificate end is reached.
+cc: copy-cert-chunk ( -- len ) {
+ size_t clen;
+
+ clen = ENG->cert_len;
+ if (clen > sizeof ENG->pad) {
+ clen = sizeof ENG->pad;
+ }
+ memcpy(ENG->pad, ENG->cert_cur, clen);
+ ENG->cert_cur += clen;
+ ENG->cert_len -= clen;
+ T0_PUSH(clen);
+}
+
+\ Write a Certificate message. Total chain length (excluding the 3-byte
+\ header) is returned; it is 0 if the chain is empty.
+: write-Certificate ( -- total_chain_len )
+ 11 write8
+ total-chain-length dup
+ dup 3 + write24 write24
+ begin
+ begin-cert
+ dup 0< if drop ret then write24
+ begin copy-cert-chunk dup while
+ addr-pad swap write-blob
+ repeat
+ drop
+ again ;
+
+cc: x509-start-chain ( by_client -- ) {
+ const br_x509_class *xc;
+ uint32_t bc;
+
+ bc = T0_POP();
+ xc = *(ENG->x509ctx);
+ xc->start_chain(ENG->x509ctx, bc ? ENG->server_name : NULL);
+}
+
+cc: x509-start-cert ( length -- ) {
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->start_cert(ENG->x509ctx, T0_POP());
+}
+
+cc: x509-append ( length -- ) {
+ const br_x509_class *xc;
+ size_t len;
+
+ xc = *(ENG->x509ctx);
+ len = T0_POP();
+ xc->append(ENG->x509ctx, ENG->pad, len);
+}
+
+cc: x509-end-cert ( -- ) {
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->end_cert(ENG->x509ctx);
+}
+
+cc: x509-end-chain ( -- err ) {
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ T0_PUSH(xc->end_chain(ENG->x509ctx));
+}
+
+cc: get-key-type-usages ( -- key-type-usages ) {
+ const br_x509_class *xc;
+ const br_x509_pkey *pk;
+ unsigned usages;
+
+ xc = *(ENG->x509ctx);
+ pk = xc->get_pkey(ENG->x509ctx, &usages);
+ if (pk == NULL) {
+ T0_PUSH(0);
+ } else {
+ T0_PUSH(pk->key_type | usages);
+ }
+}
+
+\ Read a Certificate message.
+\ Parameter: non-zero if this is a read by the client of a certificate
+\ sent by the server; zero otherwise.
+\ Returned value:
+\ - Empty: 0
+\ - Valid: combination of key type and allowed key usages.
+\ - Invalid: negative (-x for error code x)
+: read-Certificate ( by_client -- key-type-usages )
+ \ Get header, and check message type.
+ read-handshake-header 11 = ifnot ERR_UNEXPECTED fail then
+
+ \ If the chain is empty, do some special processing.
+ dup 3 = if
+ read24 if ERR_BAD_PARAM fail then
+ swap drop ret
+ then
+
+ \ Start processing the chain through the X.509 engine.
+ swap x509-start-chain
+
+ \ Total chain length is a 24-bit integer.
+ read24 open-elt
+ begin
+ dup while
+ read24 open-elt
+ dup x509-start-cert
+
+ \ We read the certificate by chunks through the pad, so
+ \ as to use the existing reading function (read-blob)
+ \ that also ensures proper hashing.
+ begin
+ dup while
+ dup 256 > if 256 else dup then { len }
+ addr-pad len read-blob
+ len x509-append
+ repeat
+ close-elt
+ x509-end-cert
+ repeat
+
+ \ We must close the chain AND the handshake message.
+ close-elt
+ close-elt
+
+ \ Chain processing is finished; get the error code.
+ x509-end-chain
+ dup if neg ret then drop
+
+ \ Return key type and usages.
+ get-key-type-usages ;
+
+\ =======================================================================
+
+\ Copy a specific protocol name from the list to the pad. The byte
+\ length is returned.
+cc: copy-protocol-name ( idx -- len ) {
+ size_t idx = T0_POP();
+ size_t len = strlen(ENG->protocol_names[idx]);
+ memcpy(ENG->pad, ENG->protocol_names[idx], len);
+ T0_PUSH(len);
+}
+
+\ Compare name in pad with the configured list of protocol names.
+\ If a match is found, then the index is returned; otherwise, -1
+\ is returned.
+cc: test-protocol-name ( len -- n ) {
+ size_t len = T0_POP();
+ size_t u;
+
+ for (u = 0; u < ENG->protocol_names_num; u ++) {
+ const char *name;
+
+ name = ENG->protocol_names[u];
+ if (len == strlen(name) && memcmp(ENG->pad, name, len) == 0) {
+ T0_PUSH(u);
+ T0_RET();
+ }
+ }
+ T0_PUSHi(-1);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hs_server.c b/test/monniaux/BearSSL/src/ssl/ssl_hs_server.c
new file mode 100644
index 00000000..5f8cae79
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hs_server.c
@@ -0,0 +1,2009 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_ssl_hs_server_init_main(void *t0ctx);
+
+void br_ssl_hs_server_run(void *t0ctx);
+
+
+
+#include <stddef.h>
+#include <string.h>
+
+#include "inner.h"
+
+/*
+ * This macro evaluates to a pointer to the current engine context.
+ */
+#define ENG ((br_ssl_engine_context *)(void *)((unsigned char *)t0ctx - offsetof(br_ssl_engine_context, cpu)))
+
+
+
+
+
+/*
+ * This macro evaluates to a pointer to the server context, under that
+ * specific name. It must be noted that since the engine context is the
+ * first field of the br_ssl_server_context structure ('eng'), then
+ * pointers values of both types are interchangeable, modulo an
+ * appropriate cast. This also means that "addresses" computed as offsets
+ * within the structure work for both kinds of context.
+ */
+#define CTX ((br_ssl_server_context *)ENG)
+
+/*
+ * Decrypt the pre-master secret (RSA key exchange).
+ */
+static void
+do_rsa_decrypt(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *epms, size_t len)
+{
+ uint32_t x;
+ unsigned char rpms[48];
+
+ /*
+ * Decrypt the PMS.
+ */
+ x = (*ctx->policy_vtable)->do_keyx(ctx->policy_vtable, epms, &len);
+
+ /*
+ * Set the first two bytes to the maximum supported client
+ * protocol version. These bytes are used for version rollback
+ * detection; forceing the two bytes will make the master secret
+ * wrong if the bytes are not correct. This process is
+ * recommended by RFC 5246 (section 7.4.7.1).
+ */
+ br_enc16be(epms, ctx->client_max_version);
+
+ /*
+ * Make a random PMS and copy it above the decrypted value if the
+ * decryption failed. Note that we use a constant-time conditional
+ * copy.
+ */
+ br_hmac_drbg_generate(&ctx->eng.rng, rpms, sizeof rpms);
+ br_ccopy(x ^ 1, epms, rpms, sizeof rpms);
+
+ /*
+ * Compute master secret.
+ */
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, epms, 48);
+
+ /*
+ * Clear the pre-master secret from RAM: it is normally a buffer
+ * in the context, hence potentially long-lived.
+ */
+ memset(epms, 0, len);
+}
+
+/*
+ * Common part for ECDH and ECDHE.
+ */
+static void
+ecdh_common(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *xcoor, size_t xcoor_len, uint32_t ctl)
+{
+ unsigned char rpms[80];
+
+ if (xcoor_len > sizeof rpms) {
+ xcoor_len = sizeof rpms;
+ ctl = 0;
+ }
+
+ /*
+ * Make a random PMS and copy it above the decrypted value if the
+ * decryption failed. Note that we use a constant-time conditional
+ * copy.
+ */
+ br_hmac_drbg_generate(&ctx->eng.rng, rpms, xcoor_len);
+ br_ccopy(ctl ^ 1, xcoor, rpms, xcoor_len);
+
+ /*
+ * Compute master secret.
+ */
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, xcoor, xcoor_len);
+
+ /*
+ * Clear the pre-master secret from RAM: it is normally a buffer
+ * in the context, hence potentially long-lived.
+ */
+ memset(xcoor, 0, xcoor_len);
+}
+
+/*
+ * Do the ECDH key exchange (not ECDHE).
+ */
+static void
+do_ecdh(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *cpoint, size_t cpoint_len)
+{
+ uint32_t x;
+
+ /*
+ * Finalise the key exchange.
+ */
+ x = (*ctx->policy_vtable)->do_keyx(ctx->policy_vtable,
+ cpoint, &cpoint_len);
+ ecdh_common(ctx, prf_id, cpoint, cpoint_len, x);
+}
+
+/*
+ * Do the full static ECDH key exchange. When this function is called,
+ * it has already been verified that the cipher suite uses ECDH (not ECDHE),
+ * and the client's public key (from its certificate) has type EC and is
+ * apt for key exchange.
+ */
+static void
+do_static_ecdh(br_ssl_server_context *ctx, int prf_id)
+{
+ unsigned char cpoint[133];
+ size_t cpoint_len;
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ cpoint_len = pk->key.ec.qlen;
+ if (cpoint_len > sizeof cpoint) {
+ /*
+ * If the point is larger than our buffer then we need to
+ * restrict it. Length 2 is not a valid point length, so
+ * the ECDH will fail.
+ */
+ cpoint_len = 2;
+ }
+ memcpy(cpoint, pk->key.ec.q, cpoint_len);
+ do_ecdh(ctx, prf_id, cpoint, cpoint_len);
+}
+
+static size_t
+hash_data(br_ssl_server_context *ctx,
+ void *dst, int hash_id, const void *src, size_t len)
+{
+ const br_hash_class *hf;
+ br_hash_compat_context hc;
+
+ if (hash_id == 0) {
+ unsigned char tmp[36];
+
+ hf = br_multihash_getimpl(&ctx->eng.mhash, br_md5_ID);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, tmp);
+ hf = br_multihash_getimpl(&ctx->eng.mhash, br_sha1_ID);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, tmp + 16);
+ memcpy(dst, tmp, 36);
+ return 36;
+ } else {
+ hf = br_multihash_getimpl(&ctx->eng.mhash, hash_id);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, dst);
+ return (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+ }
+}
+
+/*
+ * Do the ECDHE key exchange (part 1: generation of transient key, and
+ * computing of the point to send to the client). Returned value is the
+ * signature length (in bytes), or -x on error (with x being an error
+ * code). The encoded point is written in the ecdhe_point[] context buffer
+ * (length in ecdhe_point_len).
+ */
+static int
+do_ecdhe_part1(br_ssl_server_context *ctx, int curve)
+{
+ unsigned algo_id;
+ unsigned mask;
+ const unsigned char *order;
+ size_t olen, glen;
+ size_t hv_len, sig_len;
+
+ if (!((ctx->eng.iec->supported_curves >> curve) & 1)) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+ ctx->eng.ecdhe_curve = curve;
+
+ /*
+ * Generate our private key. We need a non-zero random value
+ * which is lower than the curve order, in a "large enough"
+ * range. We force the top bit to 0 and bottom bit to 1, which
+ * does the trick. Note that contrary to what happens in ECDSA,
+ * this is not a problem if we do not cover the full range of
+ * possible values.
+ */
+ order = ctx->eng.iec->order(curve, &olen);
+ mask = 0xFF;
+ while (mask >= order[0]) {
+ mask >>= 1;
+ }
+ br_hmac_drbg_generate(&ctx->eng.rng, ctx->ecdhe_key, olen);
+ ctx->ecdhe_key[0] &= mask;
+ ctx->ecdhe_key[olen - 1] |= 0x01;
+ ctx->ecdhe_key_len = olen;
+
+ /*
+ * Compute our ECDH point.
+ */
+ glen = ctx->eng.iec->mulgen(ctx->eng.ecdhe_point,
+ ctx->ecdhe_key, olen, curve);
+ ctx->eng.ecdhe_point_len = glen;
+
+ /*
+ * Assemble the message to be signed, and possibly hash it.
+ */
+ memcpy(ctx->eng.pad, ctx->eng.client_random, 32);
+ memcpy(ctx->eng.pad + 32, ctx->eng.server_random, 32);
+ ctx->eng.pad[64 + 0] = 0x03;
+ ctx->eng.pad[64 + 1] = 0x00;
+ ctx->eng.pad[64 + 2] = curve;
+ ctx->eng.pad[64 + 3] = ctx->eng.ecdhe_point_len;
+ memcpy(ctx->eng.pad + 64 + 4,
+ ctx->eng.ecdhe_point, ctx->eng.ecdhe_point_len);
+ hv_len = 64 + 4 + ctx->eng.ecdhe_point_len;
+ algo_id = ctx->sign_hash_id;
+ if (algo_id >= (unsigned)0xFF00) {
+ hv_len = hash_data(ctx, ctx->eng.pad, algo_id & 0xFF,
+ ctx->eng.pad, hv_len);
+ if (hv_len == 0) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+ }
+
+ sig_len = (*ctx->policy_vtable)->do_sign(ctx->policy_vtable,
+ algo_id, ctx->eng.pad, hv_len, sizeof ctx->eng.pad);
+ return sig_len ? (int)sig_len : -BR_ERR_INVALID_ALGORITHM;
+}
+
+/*
+ * Do the ECDHE key exchange (part 2: computation of the shared secret
+ * from the point sent by the client).
+ */
+static void
+do_ecdhe_part2(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *cpoint, size_t cpoint_len)
+{
+ int curve;
+ uint32_t ctl;
+ size_t xoff, xlen;
+
+ curve = ctx->eng.ecdhe_curve;
+
+ /*
+ * Finalise the key exchange.
+ */
+ ctl = ctx->eng.iec->mul(cpoint, cpoint_len,
+ ctx->ecdhe_key, ctx->ecdhe_key_len, curve);
+ xoff = ctx->eng.iec->xoff(curve, &xlen);
+ ecdh_common(ctx, prf_id, cpoint + xoff, xlen, ctl);
+
+ /*
+ * Clear the ECDHE private key. Forward Secrecy is achieved insofar
+ * as that key does not get stolen, so we'd better destroy it
+ * as soon as it ceases to be useful.
+ */
+ memset(ctx->ecdhe_key, 0, ctx->ecdhe_key_len);
+}
+
+/*
+ * Offset for hash value within the pad (when obtaining all hash values,
+ * in preparation for verification of the CertificateVerify message).
+ * Order is MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512; last value
+ * is used to get the total length.
+ */
+static const unsigned char HASH_PAD_OFF[] = { 0, 16, 36, 64, 96, 144, 208 };
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char HASH_OID_SHA1[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char HASH_OID_SHA224[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char HASH_OID_SHA256[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char HASH_OID_SHA384[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char HASH_OID_SHA512[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static const unsigned char *HASH_OID[] = {
+ HASH_OID_SHA1,
+ HASH_OID_SHA224,
+ HASH_OID_SHA256,
+ HASH_OID_SHA384,
+ HASH_OID_SHA512
+};
+
+/*
+ * Verify the signature in CertificateVerify. Returned value is 0 on
+ * success, or a non-zero error code. Lack of implementation of the
+ * designated signature algorithm is reported as a "bad signature"
+ * error (because it means that the peer did not honour our advertised
+ * set of supported signature algorithms).
+ */
+static int
+verify_CV_sig(br_ssl_server_context *ctx, size_t sig_len)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ int id;
+
+ id = ctx->hash_CV_id;
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ if (pk->key_type == BR_KEYTYPE_RSA) {
+ unsigned char tmp[64];
+ const unsigned char *hash_oid;
+
+ if (id == 0) {
+ hash_oid = NULL;
+ } else {
+ hash_oid = HASH_OID[id - 2];
+ }
+ if (ctx->eng.irsavrfy == 0) {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ if (!ctx->eng.irsavrfy(ctx->eng.pad, sig_len,
+ hash_oid, ctx->hash_CV_len, &pk->key.rsa, tmp)
+ || memcmp(tmp, ctx->hash_CV, ctx->hash_CV_len) != 0)
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ } else {
+ if (ctx->eng.iecdsa == 0) {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ if (!ctx->eng.iecdsa(ctx->eng.iec,
+ ctx->hash_CV, ctx->hash_CV_len,
+ &pk->key.ec, ctx->eng.pad, sig_len))
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ }
+ return 0;
+}
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x00, 0x0A, 0x00, 0x24, 0x00, 0x2F, 0x01, 0x24, 0x00, 0x35, 0x02,
+ 0x24, 0x00, 0x3C, 0x01, 0x44, 0x00, 0x3D, 0x02, 0x44, 0x00, 0x9C, 0x03,
+ 0x04, 0x00, 0x9D, 0x04, 0x05, 0xC0, 0x03, 0x40, 0x24, 0xC0, 0x04, 0x41,
+ 0x24, 0xC0, 0x05, 0x42, 0x24, 0xC0, 0x08, 0x20, 0x24, 0xC0, 0x09, 0x21,
+ 0x24, 0xC0, 0x0A, 0x22, 0x24, 0xC0, 0x0D, 0x30, 0x24, 0xC0, 0x0E, 0x31,
+ 0x24, 0xC0, 0x0F, 0x32, 0x24, 0xC0, 0x12, 0x10, 0x24, 0xC0, 0x13, 0x11,
+ 0x24, 0xC0, 0x14, 0x12, 0x24, 0xC0, 0x23, 0x21, 0x44, 0xC0, 0x24, 0x22,
+ 0x55, 0xC0, 0x25, 0x41, 0x44, 0xC0, 0x26, 0x42, 0x55, 0xC0, 0x27, 0x11,
+ 0x44, 0xC0, 0x28, 0x12, 0x55, 0xC0, 0x29, 0x31, 0x44, 0xC0, 0x2A, 0x32,
+ 0x55, 0xC0, 0x2B, 0x23, 0x04, 0xC0, 0x2C, 0x24, 0x05, 0xC0, 0x2D, 0x43,
+ 0x04, 0xC0, 0x2E, 0x44, 0x05, 0xC0, 0x2F, 0x13, 0x04, 0xC0, 0x30, 0x14,
+ 0x05, 0xC0, 0x31, 0x33, 0x04, 0xC0, 0x32, 0x34, 0x05, 0xC0, 0x9C, 0x06,
+ 0x04, 0xC0, 0x9D, 0x07, 0x04, 0xC0, 0xA0, 0x08, 0x04, 0xC0, 0xA1, 0x09,
+ 0x04, 0xC0, 0xAC, 0x26, 0x04, 0xC0, 0xAD, 0x27, 0x04, 0xC0, 0xAE, 0x28,
+ 0x04, 0xC0, 0xAF, 0x29, 0x04, 0xCC, 0xA8, 0x15, 0x04, 0xCC, 0xA9, 0x25,
+ 0x04, 0x00, 0x00
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x00, 0x0B, 0x00, 0x00, 0x01, 0x00, 0x0E, 0x00, 0x00, 0x01,
+ 0x00, 0x0F, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x01, 0x01, 0x08,
+ 0x00, 0x00, 0x01, 0x01, 0x09, 0x00, 0x00, 0x01, 0x02, 0x08, 0x00, 0x00,
+ 0x01, 0x02, 0x09, 0x00, 0x00, 0x29, 0x29, 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_CCS), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_FINISHED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_FRAGLEN), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_HANDSHAKE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_PARAM), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_SECRENEG), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_SIGNATURE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_BAD_VERSION), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_INVALID_ALGORITHM), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_LIMIT_EXCEEDED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_NO_CLIENT_AUTH), 0x00, 0x00, 0x01, T0_INT1(BR_ERR_OK),
+ 0x00, 0x00, 0x01, T0_INT1(BR_ERR_OVERSIZED_ID), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_UNEXPECTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_WRONG_KEY_USAGE), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, action)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, alert)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, application_data)), 0x00, 0x00,
+ 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, cipher_suite)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_server_context, client_max_version)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, client_random)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_server_context, client_suites)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_server_context, client_suites_num)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, close_received)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_server_context, curves)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, ecdhe_point)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, ecdhe_point_len)),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, flags)),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(br_ssl_server_context, hashes)),
+ 0x00, 0x00, 0x7B, 0x01,
+ T0_INT2(BR_MAX_CIPHER_SUITES * sizeof(br_suite_translated)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, log_max_frag_len)),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, pad)), 0x00,
+ 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, peer_log_max_frag_len)), 0x00,
+ 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, protocol_names_num)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, record_type_in)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, record_type_out)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, reneg)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, saved_finished)), 0x00,
+ 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, selected_protocol)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, server_name)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, server_random)), 0x00, 0x00,
+ 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, session_id)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, session_id_len)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, shutdown_recv)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_server_context, sign_hash_id)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, suites_buf)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, suites_num)), 0x00,
+ 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, session) + offsetof(br_ssl_session_parameters, version)),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_in)),
+ 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_ssl_engine_context, version_max)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_min)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(br_ssl_engine_context, version_out)),
+ 0x00, 0x00, 0x09, 0x2A, 0x5D, 0x06, 0x02, 0x6A, 0x2B, 0x00, 0x00, 0x01,
+ 0x01, 0x00, 0x01, 0x03, 0x00, 0x9B, 0x2A, 0x63, 0x47, 0x9F, 0x2A, 0x05,
+ 0x04, 0x65, 0x01, 0x00, 0x00, 0x02, 0x00, 0x0F, 0x06, 0x02, 0x9F, 0x00,
+ 0x63, 0x04, 0x6B, 0x00, 0x06, 0x02, 0x6A, 0x2B, 0x00, 0x00, 0x2A, 0x8B,
+ 0x47, 0x05, 0x03, 0x01, 0x0C, 0x08, 0x47, 0x78, 0x2E, 0xA8, 0x1C, 0x85,
+ 0x01, 0x0C, 0x33, 0x00, 0x00, 0x2A, 0x22, 0x01, 0x08, 0x0C, 0x47, 0x61,
+ 0x22, 0x08, 0x00, 0x01, 0x03, 0x00, 0x77, 0x30, 0x02, 0x00, 0x38, 0x13,
+ 0x01, 0x01, 0x0C, 0x77, 0x42, 0x2C, 0x19, 0x38, 0x06, 0x07, 0x02, 0x00,
+ 0xD0, 0x03, 0x00, 0x04, 0x75, 0x01, 0x00, 0xC7, 0x02, 0x00, 0x2A, 0x19,
+ 0x13, 0x06, 0x02, 0x71, 0x2B, 0xD0, 0x04, 0x76, 0x00, 0x01, 0x00, 0x77,
+ 0x42, 0x01, 0x16, 0x89, 0x42, 0x01, 0x00, 0x8C, 0x40, 0x36, 0xB1, 0x35,
+ 0x06, 0x02, 0x73, 0x2B, 0x06, 0x0A, 0xD7, 0x01, 0x00, 0xD3, 0x01, 0x00,
+ 0xAD, 0x04, 0x80, 0x46, 0xD7, 0xD4, 0x29, 0xD9, 0x50, 0x06, 0x01, 0xD5,
+ 0xD8, 0x2C, 0x50, 0x06, 0x31, 0x01, 0x00, 0xAE, 0x2A, 0x5D, 0x06, 0x0F,
+ 0x01, 0x02, 0xA4, 0x05, 0x02, 0x37, 0x2B, 0x29, 0xB2, 0xB0, 0x2A, 0xC9,
+ 0x29, 0x04, 0x19, 0x2A, 0x5F, 0x06, 0x0B, 0x29, 0x01, 0x02, 0xA4, 0x05,
+ 0x02, 0x70, 0x2B, 0xB2, 0x04, 0x0A, 0xB4, 0x2A, 0x05, 0x04, 0x29, 0xAB,
+ 0x04, 0x02, 0xB3, 0xAF, 0x04, 0x01, 0xB2, 0x01, 0x00, 0xAD, 0x01, 0x00,
+ 0xD3, 0x3E, 0x01, 0x01, 0x77, 0x42, 0x01, 0x17, 0x89, 0x42, 0x00, 0x00,
+ 0x3A, 0x3A, 0x00, 0x01, 0x03, 0x00, 0x2C, 0x19, 0x38, 0x06, 0x04, 0xCF,
+ 0x29, 0x04, 0x78, 0x01, 0x02, 0x02, 0x00, 0xC6, 0x19, 0x38, 0x06, 0x04,
+ 0xCF, 0x29, 0x04, 0x78, 0x02, 0x00, 0x01, 0x84, 0x00, 0x08, 0x2B, 0x00,
+ 0x00, 0x81, 0x2F, 0x47, 0x12, 0x01, 0x01, 0x13, 0x37, 0x00, 0x00, 0x2A,
+ 0x05, 0x04, 0x29, 0x01, 0x7F, 0x00, 0x01, 0x00, 0xA2, 0x12, 0x01, 0x01,
+ 0x13, 0x5F, 0x06, 0x03, 0x61, 0x04, 0x75, 0x47, 0x29, 0x00, 0x00, 0x01,
+ 0x7F, 0xA1, 0xCF, 0x2A, 0x01, 0x07, 0x13, 0x01, 0x00, 0x3A, 0x0F, 0x06,
+ 0x0D, 0x29, 0x01, 0x10, 0x13, 0x06, 0x05, 0x01, 0x00, 0x77, 0x42, 0xC5,
+ 0x04, 0x33, 0x01, 0x01, 0x3A, 0x0F, 0x06, 0x2A, 0x29, 0x29, 0x8A, 0x30,
+ 0x01, 0x01, 0x0F, 0x01, 0x01, 0xA4, 0x39, 0x06, 0x18, 0xC8, 0x2C, 0x19,
+ 0x38, 0x06, 0x04, 0xCF, 0x29, 0x04, 0x78, 0x01, 0x80, 0x64, 0xC7, 0x01,
+ 0x01, 0x77, 0x42, 0x01, 0x17, 0x89, 0x42, 0x04, 0x03, 0x01, 0x00, 0xA1,
+ 0x04, 0x03, 0x73, 0x2B, 0x29, 0x04, 0xFF, 0x32, 0x01, 0x2A, 0x03, 0x00,
+ 0x09, 0x2A, 0x5D, 0x06, 0x02, 0x6A, 0x2B, 0x02, 0x00, 0x00, 0x00, 0x9C,
+ 0x01, 0x0F, 0x13, 0x00, 0x00, 0x76, 0x30, 0x01, 0x00, 0x3A, 0x0F, 0x06,
+ 0x10, 0x29, 0x2A, 0x01, 0x01, 0x0E, 0x06, 0x03, 0x29, 0x01, 0x02, 0x76,
+ 0x42, 0x01, 0x00, 0x04, 0x21, 0x01, 0x01, 0x3A, 0x0F, 0x06, 0x14, 0x29,
+ 0x01, 0x00, 0x76, 0x42, 0x2A, 0x01, 0x80, 0x64, 0x0F, 0x06, 0x05, 0x01,
+ 0x82, 0x00, 0x08, 0x2B, 0x5F, 0x04, 0x07, 0x29, 0x01, 0x82, 0x00, 0x08,
+ 0x2B, 0x29, 0x00, 0x00, 0x01, 0x00, 0x31, 0x06, 0x05, 0x3D, 0xA9, 0x39,
+ 0x04, 0x78, 0x2A, 0x06, 0x04, 0x01, 0x01, 0x91, 0x42, 0x00, 0x00, 0x01,
+ 0x1F, 0x13, 0x01, 0x12, 0x0F, 0x05, 0x02, 0x74, 0x2B, 0x78, 0x2E, 0x2A,
+ 0xCB, 0x05, 0x02, 0x73, 0x2B, 0xA8, 0x28, 0x00, 0x02, 0x87, 0x2E, 0x05,
+ 0x02, 0xBC, 0x00, 0xC0, 0xA7, 0xC0, 0xA7, 0x01, 0x7E, 0x03, 0x00, 0x2A,
+ 0x06, 0x17, 0xC2, 0x2A, 0x03, 0x01, 0x85, 0x47, 0xB6, 0x02, 0x01, 0x51,
+ 0x2A, 0x02, 0x00, 0x53, 0x06, 0x04, 0x03, 0x00, 0x04, 0x01, 0x29, 0x04,
+ 0x66, 0x9D, 0x9D, 0x02, 0x00, 0x61, 0x8C, 0x40, 0x00, 0x00, 0x31, 0x06,
+ 0x0B, 0x88, 0x30, 0x01, 0x14, 0x0E, 0x06, 0x02, 0x73, 0x2B, 0x04, 0x11,
+ 0xCF, 0x01, 0x07, 0x13, 0x2A, 0x01, 0x02, 0x0E, 0x06, 0x06, 0x06, 0x02,
+ 0x73, 0x2B, 0x04, 0x70, 0x29, 0xC3, 0x01, 0x01, 0x0E, 0x35, 0x39, 0x06,
+ 0x02, 0x66, 0x2B, 0x2A, 0x01, 0x01, 0xCA, 0x38, 0xB5, 0x00, 0x01, 0xBA,
+ 0x01, 0x0B, 0x0F, 0x05, 0x02, 0x73, 0x2B, 0x2A, 0x01, 0x03, 0x0F, 0x06,
+ 0x08, 0xC1, 0x06, 0x02, 0x6A, 0x2B, 0x47, 0x29, 0x00, 0x47, 0x5C, 0xC1,
+ 0xA7, 0x2A, 0x06, 0x23, 0xC1, 0xA7, 0x2A, 0x5B, 0x2A, 0x06, 0x18, 0x2A,
+ 0x01, 0x82, 0x00, 0x10, 0x06, 0x05, 0x01, 0x82, 0x00, 0x04, 0x01, 0x2A,
+ 0x03, 0x00, 0x85, 0x02, 0x00, 0xB6, 0x02, 0x00, 0x58, 0x04, 0x65, 0x9D,
+ 0x59, 0x04, 0x5A, 0x9D, 0x9D, 0x5A, 0x2A, 0x06, 0x02, 0x37, 0x00, 0x29,
+ 0x2D, 0x00, 0x02, 0x2A, 0x01, 0x20, 0x13, 0x05, 0x02, 0x74, 0x2B, 0x01,
+ 0x0F, 0x13, 0x03, 0x00, 0xB0, 0x95, 0x2E, 0x01, 0x86, 0x03, 0x11, 0x06,
+ 0x23, 0xC0, 0x2A, 0x01, 0x81, 0x7F, 0x13, 0x61, 0x01, 0x01, 0x12, 0x02,
+ 0x00, 0x0F, 0x05, 0x02, 0x6C, 0x2B, 0x01, 0x08, 0x12, 0x2A, 0x01, 0x02,
+ 0x0B, 0x3A, 0x01, 0x06, 0x10, 0x39, 0x06, 0x02, 0x6E, 0x2B, 0x04, 0x0D,
+ 0x02, 0x00, 0x01, 0x01, 0x0F, 0x06, 0x04, 0x01, 0x00, 0x04, 0x02, 0x01,
+ 0x02, 0x20, 0x05, 0x02, 0x6E, 0x2B, 0xC0, 0x2A, 0x03, 0x01, 0x2A, 0x01,
+ 0x84, 0x00, 0x10, 0x06, 0x02, 0x6F, 0x2B, 0x85, 0x47, 0xB6, 0x02, 0x01,
+ 0x55, 0x2A, 0x06, 0x01, 0x2B, 0x29, 0x9D, 0x00, 0x00, 0x1D, 0xBA, 0x01,
+ 0x0F, 0x0F, 0x05, 0x02, 0x73, 0x2B, 0x00, 0x0A, 0xBA, 0x01, 0x01, 0x0F,
+ 0x05, 0x02, 0x73, 0x2B, 0xC0, 0x2A, 0x03, 0x00, 0x79, 0x40, 0x7A, 0x01,
+ 0x20, 0xB6, 0xC2, 0x2A, 0x01, 0x20, 0x10, 0x06, 0x02, 0x72, 0x2B, 0x2A,
+ 0x90, 0x42, 0x8F, 0x47, 0xB6, 0x1A, 0x03, 0x01, 0xC0, 0xA7, 0x01, 0x00,
+ 0x03, 0x02, 0x01, 0x00, 0x03, 0x03, 0x83, 0xA2, 0x17, 0x3A, 0x08, 0x03,
+ 0x04, 0x03, 0x05, 0x2A, 0x06, 0x80, 0x6D, 0xC0, 0x2A, 0x03, 0x06, 0x02,
+ 0x01, 0x06, 0x0A, 0x2A, 0x78, 0x2E, 0x0F, 0x06, 0x04, 0x01, 0x7F, 0x03,
+ 0x03, 0x2A, 0x01, 0x81, 0x7F, 0x0F, 0x06, 0x0A, 0x8A, 0x30, 0x06, 0x02,
+ 0x6B, 0x2B, 0x01, 0x7F, 0x03, 0x02, 0x2A, 0x01, 0x81, 0xAC, 0x00, 0x0F,
+ 0x06, 0x11, 0x02, 0x00, 0x98, 0x2E, 0x11, 0x02, 0x00, 0x97, 0x2E, 0x0B,
+ 0x13, 0x06, 0x04, 0x01, 0x7F, 0x03, 0x00, 0xC4, 0x2A, 0x5D, 0x06, 0x03,
+ 0x29, 0x04, 0x26, 0x01, 0x00, 0xA4, 0x06, 0x0B, 0x01, 0x02, 0x0C, 0x7B,
+ 0x08, 0x02, 0x06, 0x47, 0x40, 0x04, 0x16, 0x29, 0x02, 0x05, 0x02, 0x04,
+ 0x11, 0x06, 0x02, 0x69, 0x2B, 0x02, 0x06, 0x02, 0x05, 0x40, 0x02, 0x05,
+ 0x01, 0x04, 0x08, 0x03, 0x05, 0x04, 0xFF, 0x0F, 0x29, 0x01, 0x00, 0x03,
+ 0x07, 0xC2, 0xA7, 0x2A, 0x06, 0x09, 0xC2, 0x05, 0x04, 0x01, 0x7F, 0x03,
+ 0x07, 0x04, 0x74, 0x9D, 0x01, 0x00, 0x8D, 0x42, 0x01, 0x88, 0x04, 0x82,
+ 0x41, 0x01, 0x84, 0x80, 0x80, 0x00, 0x7E, 0x41, 0x2A, 0x06, 0x80, 0x4E,
+ 0xC0, 0xA7, 0x2A, 0x06, 0x80, 0x47, 0xC0, 0x01, 0x00, 0x3A, 0x0F, 0x06,
+ 0x04, 0x29, 0xB9, 0x04, 0x39, 0x01, 0x01, 0x3A, 0x0F, 0x06, 0x04, 0x29,
+ 0xB7, 0x04, 0x2F, 0x01, 0x83, 0xFE, 0x01, 0x3A, 0x0F, 0x06, 0x04, 0x29,
+ 0xB8, 0x04, 0x23, 0x01, 0x0D, 0x3A, 0x0F, 0x06, 0x04, 0x29, 0xBE, 0x04,
+ 0x19, 0x01, 0x0A, 0x3A, 0x0F, 0x06, 0x04, 0x29, 0xBF, 0x04, 0x0F, 0x01,
+ 0x10, 0x3A, 0x0F, 0x06, 0x04, 0x29, 0xAC, 0x04, 0x05, 0x29, 0xBC, 0x01,
+ 0x00, 0x29, 0x04, 0xFF, 0x35, 0x9D, 0x9D, 0x02, 0x01, 0x02, 0x03, 0x13,
+ 0x03, 0x01, 0x02, 0x00, 0x5D, 0x06, 0x08, 0x79, 0x2E, 0x99, 0x40, 0x01,
+ 0x80, 0x56, 0xA3, 0x97, 0x2E, 0x2A, 0x02, 0x00, 0x10, 0x06, 0x03, 0x29,
+ 0x02, 0x00, 0x2A, 0x01, 0x86, 0x00, 0x0B, 0x06, 0x02, 0x6D, 0x2B, 0x02,
+ 0x00, 0x98, 0x2E, 0x0B, 0x06, 0x04, 0x01, 0x80, 0x46, 0xA3, 0x02, 0x01,
+ 0x06, 0x10, 0x95, 0x2E, 0x02, 0x00, 0x0D, 0x06, 0x05, 0x29, 0x95, 0x2E,
+ 0x04, 0x04, 0x01, 0x00, 0x03, 0x01, 0x2A, 0x95, 0x40, 0x2A, 0x96, 0x40,
+ 0x2A, 0x99, 0x40, 0x01, 0x86, 0x03, 0x11, 0x03, 0x08, 0x02, 0x02, 0x06,
+ 0x04, 0x01, 0x02, 0x8A, 0x42, 0x8A, 0x30, 0x05, 0x04, 0x01, 0x01, 0x8A,
+ 0x42, 0x02, 0x07, 0x05, 0x03, 0x01, 0x28, 0xA3, 0x44, 0x29, 0x01, 0x82,
+ 0x01, 0x07, 0x01, 0xFC, 0x80, 0x00, 0x39, 0x82, 0x2F, 0x13, 0x2A, 0x82,
+ 0x41, 0x2A, 0x01, 0x81, 0x7F, 0x13, 0x5E, 0x37, 0x47, 0x01, 0x08, 0x12,
+ 0x5E, 0x01, 0x02, 0x13, 0x39, 0x01, 0x0C, 0x0C, 0x03, 0x09, 0x7E, 0x2F,
+ 0x43, 0x13, 0x2A, 0x7E, 0x41, 0x05, 0x04, 0x01, 0x00, 0x03, 0x09, 0x02,
+ 0x01, 0x06, 0x03, 0x01, 0x7F, 0x00, 0x8F, 0x01, 0x20, 0x34, 0x01, 0x20,
+ 0x90, 0x42, 0x7B, 0x2A, 0x03, 0x05, 0x2A, 0x02, 0x04, 0x0B, 0x06, 0x80,
+ 0x49, 0x2A, 0x2E, 0x2A, 0x9C, 0x2A, 0x01, 0x0C, 0x12, 0x2A, 0x01, 0x01,
+ 0x0F, 0x47, 0x01, 0x02, 0x0F, 0x39, 0x06, 0x0A, 0x2A, 0x02, 0x09, 0x13,
+ 0x05, 0x04, 0x65, 0x01, 0x00, 0x2A, 0x02, 0x08, 0x05, 0x0E, 0x2A, 0x01,
+ 0x81, 0x70, 0x13, 0x01, 0x20, 0x0E, 0x06, 0x04, 0x65, 0x01, 0x00, 0x2A,
+ 0x2A, 0x06, 0x10, 0x02, 0x05, 0x63, 0x40, 0x02, 0x05, 0x40, 0x02, 0x05,
+ 0x01, 0x04, 0x08, 0x03, 0x05, 0x04, 0x01, 0x65, 0x01, 0x04, 0x08, 0x04,
+ 0xFF, 0x30, 0x29, 0x02, 0x05, 0x7B, 0x09, 0x01, 0x02, 0x12, 0x2A, 0x05,
+ 0x03, 0x01, 0x28, 0xA3, 0x7C, 0x42, 0x8C, 0x2E, 0x01, 0x83, 0xFF, 0x7F,
+ 0x0F, 0x06, 0x0D, 0x01, 0x03, 0xA4, 0x06, 0x04, 0x01, 0x80, 0x78, 0xA3,
+ 0x01, 0x00, 0x8C, 0x40, 0x18, 0x05, 0x03, 0x01, 0x28, 0xA3, 0x01, 0x00,
+ 0x00, 0x00, 0xB4, 0xB3, 0x00, 0x04, 0x78, 0x2E, 0xCE, 0x06, 0x16, 0xC0,
+ 0x2A, 0x01, 0x84, 0x00, 0x10, 0x06, 0x02, 0x6F, 0x2B, 0x2A, 0x03, 0x00,
+ 0x85, 0x47, 0xB6, 0x02, 0x00, 0x78, 0x2E, 0xA8, 0x27, 0x78, 0x2E, 0x2A,
+ 0xCC, 0x47, 0xCB, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01, 0x02, 0x02, 0x39,
+ 0x06, 0x14, 0xC2, 0x2A, 0x03, 0x03, 0x85, 0x47, 0xB6, 0x02, 0x03, 0x78,
+ 0x2E, 0xA8, 0x02, 0x02, 0x06, 0x03, 0x26, 0x04, 0x01, 0x24, 0x9D, 0x00,
+ 0x00, 0xBA, 0x01, 0x10, 0x0F, 0x05, 0x02, 0x73, 0x2B, 0x00, 0x00, 0x9E,
+ 0xBA, 0x01, 0x14, 0x0E, 0x06, 0x02, 0x73, 0x2B, 0x85, 0x01, 0x0C, 0x08,
+ 0x01, 0x0C, 0xB6, 0x9D, 0x85, 0x2A, 0x01, 0x0C, 0x08, 0x01, 0x0C, 0x32,
+ 0x05, 0x02, 0x67, 0x2B, 0x00, 0x02, 0x03, 0x00, 0x03, 0x01, 0x02, 0x00,
+ 0x9A, 0x02, 0x01, 0x02, 0x00, 0x3C, 0x2A, 0x01, 0x00, 0x0F, 0x06, 0x02,
+ 0x65, 0x00, 0xD1, 0x04, 0x74, 0x00, 0xC0, 0x01, 0x01, 0x0E, 0x06, 0x02,
+ 0x68, 0x2B, 0xC2, 0x2A, 0x2A, 0x5F, 0x47, 0x01, 0x05, 0x11, 0x39, 0x06,
+ 0x02, 0x68, 0x2B, 0x01, 0x08, 0x08, 0x2A, 0x84, 0x30, 0x0B, 0x06, 0x0D,
+ 0x2A, 0x01, 0x01, 0x47, 0x0C, 0x3F, 0x2A, 0x84, 0x42, 0x86, 0x42, 0x04,
+ 0x01, 0x29, 0x00, 0x00, 0xC0, 0x8A, 0x30, 0x01, 0x00, 0x3A, 0x0F, 0x06,
+ 0x13, 0x29, 0x01, 0x01, 0x0F, 0x05, 0x02, 0x6B, 0x2B, 0xC2, 0x06, 0x02,
+ 0x6B, 0x2B, 0x01, 0x02, 0x8A, 0x42, 0x04, 0x28, 0x01, 0x02, 0x3A, 0x0F,
+ 0x06, 0x1F, 0x29, 0x01, 0x0D, 0x0F, 0x05, 0x02, 0x6B, 0x2B, 0xC2, 0x01,
+ 0x0C, 0x0F, 0x05, 0x02, 0x6B, 0x2B, 0x85, 0x01, 0x0C, 0xB6, 0x8B, 0x85,
+ 0x01, 0x0C, 0x32, 0x05, 0x02, 0x6B, 0x2B, 0x04, 0x03, 0x6B, 0x2B, 0x29,
+ 0x00, 0x00, 0xC0, 0xA7, 0xC0, 0xA7, 0x2A, 0x06, 0x1D, 0xC2, 0x06, 0x03,
+ 0xBC, 0x04, 0x15, 0xC0, 0x2A, 0x01, 0x81, 0x7F, 0x0D, 0x06, 0x0C, 0x2A,
+ 0x8D, 0x08, 0x01, 0x00, 0x47, 0x42, 0x8D, 0x47, 0xB6, 0x04, 0x01, 0xC9,
+ 0x04, 0x60, 0x9D, 0x9D, 0x00, 0x00, 0xBB, 0x2A, 0x5F, 0x06, 0x07, 0x29,
+ 0x06, 0x02, 0x69, 0x2B, 0x04, 0x74, 0x00, 0x00, 0xC3, 0x01, 0x03, 0xC1,
+ 0x47, 0x29, 0x47, 0x00, 0x00, 0xC0, 0xC9, 0x00, 0x03, 0x01, 0x00, 0x03,
+ 0x00, 0xC0, 0xA7, 0x2A, 0x06, 0x80, 0x50, 0xC2, 0x03, 0x01, 0xC2, 0x03,
+ 0x02, 0x02, 0x01, 0x01, 0x08, 0x0F, 0x06, 0x16, 0x02, 0x02, 0x01, 0x0F,
+ 0x0D, 0x06, 0x0D, 0x01, 0x01, 0x02, 0x02, 0x01, 0x10, 0x08, 0x0C, 0x02,
+ 0x00, 0x39, 0x03, 0x00, 0x04, 0x2A, 0x02, 0x01, 0x01, 0x02, 0x11, 0x02,
+ 0x01, 0x01, 0x06, 0x0D, 0x13, 0x02, 0x02, 0x01, 0x01, 0x0F, 0x02, 0x02,
+ 0x01, 0x03, 0x0F, 0x39, 0x13, 0x06, 0x11, 0x02, 0x00, 0x01, 0x01, 0x02,
+ 0x02, 0x62, 0x01, 0x02, 0x0C, 0x02, 0x01, 0x08, 0x0C, 0x39, 0x03, 0x00,
+ 0x04, 0xFF, 0x2C, 0x9D, 0x02, 0x00, 0x00, 0x00, 0xC0, 0xA7, 0xBD, 0x82,
+ 0x41, 0x9D, 0x00, 0x00, 0xC0, 0xA7, 0xC0, 0xA7, 0x01, 0x00, 0x7E, 0x41,
+ 0x2A, 0x06, 0x15, 0xC0, 0x2A, 0x01, 0x20, 0x0B, 0x06, 0x0B, 0x01, 0x01,
+ 0x47, 0x0C, 0x7E, 0x2F, 0x39, 0x7E, 0x41, 0x04, 0x01, 0x29, 0x04, 0x68,
+ 0x9D, 0x9D, 0x00, 0x00, 0x01, 0x02, 0x9A, 0xC3, 0x01, 0x08, 0x0C, 0xC3,
+ 0x08, 0x00, 0x00, 0x01, 0x03, 0x9A, 0xC3, 0x01, 0x08, 0x0C, 0xC3, 0x08,
+ 0x01, 0x08, 0x0C, 0xC3, 0x08, 0x00, 0x00, 0x01, 0x01, 0x9A, 0xC3, 0x00,
+ 0x00, 0x3D, 0x2A, 0x5D, 0x05, 0x01, 0x00, 0x29, 0xD1, 0x04, 0x76, 0x02,
+ 0x03, 0x00, 0x94, 0x30, 0x03, 0x01, 0x01, 0x00, 0x2A, 0x02, 0x01, 0x0B,
+ 0x06, 0x10, 0x2A, 0x01, 0x01, 0x0C, 0x93, 0x08, 0x2E, 0x02, 0x00, 0x0F,
+ 0x06, 0x01, 0x00, 0x61, 0x04, 0x6A, 0x29, 0x01, 0x7F, 0x00, 0x00, 0x2C,
+ 0x19, 0x38, 0x06, 0x04, 0xCF, 0x29, 0x04, 0x78, 0x01, 0x16, 0x89, 0x42,
+ 0x01, 0x00, 0xE2, 0x01, 0x00, 0xE1, 0x2C, 0x01, 0x17, 0x89, 0x42, 0x00,
+ 0x00, 0x01, 0x15, 0x89, 0x42, 0x47, 0x57, 0x29, 0x57, 0x29, 0x2C, 0x00,
+ 0x00, 0x01, 0x01, 0x47, 0xC6, 0x00, 0x00, 0xBB, 0x01, 0x01, 0x0F, 0x05,
+ 0x02, 0x73, 0x2B, 0x2A, 0xC9, 0x29, 0x00, 0x00, 0x47, 0x3A, 0x9A, 0x47,
+ 0x2A, 0x06, 0x05, 0xC3, 0x29, 0x62, 0x04, 0x78, 0x29, 0x00, 0x02, 0x03,
+ 0x00, 0x78, 0x2E, 0x9C, 0x03, 0x01, 0x02, 0x01, 0x01, 0x0F, 0x13, 0x02,
+ 0x01, 0x01, 0x04, 0x12, 0x01, 0x0F, 0x13, 0x02, 0x01, 0x01, 0x08, 0x12,
+ 0x01, 0x0F, 0x13, 0x01, 0x00, 0x3A, 0x0F, 0x06, 0x10, 0x29, 0x01, 0x00,
+ 0x01, 0x18, 0x02, 0x00, 0x06, 0x03, 0x4C, 0x04, 0x01, 0x4D, 0x04, 0x81,
+ 0x0D, 0x01, 0x01, 0x3A, 0x0F, 0x06, 0x10, 0x29, 0x01, 0x01, 0x01, 0x10,
+ 0x02, 0x00, 0x06, 0x03, 0x4C, 0x04, 0x01, 0x4D, 0x04, 0x80, 0x77, 0x01,
+ 0x02, 0x3A, 0x0F, 0x06, 0x10, 0x29, 0x01, 0x01, 0x01, 0x20, 0x02, 0x00,
+ 0x06, 0x03, 0x4C, 0x04, 0x01, 0x4D, 0x04, 0x80, 0x61, 0x01, 0x03, 0x3A,
+ 0x0F, 0x06, 0x0F, 0x29, 0x29, 0x01, 0x10, 0x02, 0x00, 0x06, 0x03, 0x4A,
+ 0x04, 0x01, 0x4B, 0x04, 0x80, 0x4C, 0x01, 0x04, 0x3A, 0x0F, 0x06, 0x0E,
+ 0x29, 0x29, 0x01, 0x20, 0x02, 0x00, 0x06, 0x03, 0x4A, 0x04, 0x01, 0x4B,
+ 0x04, 0x38, 0x01, 0x05, 0x3A, 0x0F, 0x06, 0x0C, 0x29, 0x29, 0x02, 0x00,
+ 0x06, 0x03, 0x4E, 0x04, 0x01, 0x4F, 0x04, 0x26, 0x2A, 0x01, 0x09, 0x10,
+ 0x06, 0x02, 0x6A, 0x2B, 0x47, 0x29, 0x2A, 0x01, 0x01, 0x13, 0x01, 0x04,
+ 0x0C, 0x01, 0x10, 0x08, 0x47, 0x01, 0x08, 0x13, 0x01, 0x10, 0x47, 0x09,
+ 0x02, 0x00, 0x06, 0x03, 0x48, 0x04, 0x01, 0x49, 0x00, 0x29, 0x00, 0x00,
+ 0x9C, 0x01, 0x0C, 0x12, 0x01, 0x02, 0x10, 0x00, 0x00, 0x9C, 0x01, 0x0C,
+ 0x12, 0x2A, 0x60, 0x47, 0x01, 0x03, 0x0B, 0x13, 0x00, 0x00, 0x9C, 0x01,
+ 0x0C, 0x12, 0x01, 0x01, 0x0F, 0x00, 0x00, 0x9C, 0x01, 0x0C, 0x12, 0x5F,
+ 0x00, 0x00, 0x1B, 0x01, 0x00, 0x75, 0x30, 0x2A, 0x06, 0x22, 0x01, 0x01,
+ 0x3A, 0x0F, 0x06, 0x06, 0x29, 0x01, 0x00, 0xA0, 0x04, 0x14, 0x01, 0x02,
+ 0x3A, 0x0F, 0x06, 0x0D, 0x29, 0x77, 0x30, 0x01, 0x01, 0x0F, 0x06, 0x03,
+ 0x01, 0x10, 0x39, 0x04, 0x01, 0x29, 0x04, 0x01, 0x29, 0x7D, 0x30, 0x05,
+ 0x33, 0x31, 0x06, 0x30, 0x88, 0x30, 0x01, 0x14, 0x3A, 0x0F, 0x06, 0x06,
+ 0x29, 0x01, 0x02, 0x39, 0x04, 0x22, 0x01, 0x15, 0x3A, 0x0F, 0x06, 0x09,
+ 0x29, 0xAA, 0x06, 0x03, 0x01, 0x7F, 0xA0, 0x04, 0x13, 0x01, 0x16, 0x3A,
+ 0x0F, 0x06, 0x06, 0x29, 0x01, 0x01, 0x39, 0x04, 0x07, 0x29, 0x01, 0x04,
+ 0x39, 0x01, 0x00, 0x29, 0x19, 0x06, 0x03, 0x01, 0x08, 0x39, 0x00, 0x00,
+ 0x1B, 0x2A, 0x05, 0x13, 0x31, 0x06, 0x10, 0x88, 0x30, 0x01, 0x15, 0x0F,
+ 0x06, 0x08, 0x29, 0xAA, 0x01, 0x00, 0x77, 0x42, 0x04, 0x01, 0x23, 0x00,
+ 0x00, 0xCF, 0x01, 0x07, 0x13, 0x01, 0x01, 0x10, 0x06, 0x02, 0x73, 0x2B,
+ 0x00, 0x01, 0x03, 0x00, 0x2C, 0x19, 0x06, 0x05, 0x02, 0x00, 0x89, 0x42,
+ 0x00, 0xCF, 0x29, 0x04, 0x74, 0x00, 0x01, 0x14, 0xD2, 0x01, 0x01, 0xE2,
+ 0x2C, 0x2A, 0x01, 0x00, 0xCA, 0x01, 0x16, 0xD2, 0xD6, 0x2C, 0x00, 0x00,
+ 0x01, 0x0B, 0xE2, 0x52, 0x2A, 0x2A, 0x01, 0x03, 0x08, 0xE1, 0xE1, 0x14,
+ 0x2A, 0x5D, 0x06, 0x02, 0x29, 0x00, 0xE1, 0x1E, 0x2A, 0x06, 0x05, 0x85,
+ 0x47, 0xDA, 0x04, 0x77, 0x29, 0x04, 0x6C, 0x00, 0x01, 0x00, 0xDC, 0x95,
+ 0x2E, 0x01, 0x86, 0x03, 0x11, 0x06, 0x05, 0x63, 0x01, 0x00, 0xDD, 0x08,
+ 0x50, 0x08, 0x01, 0x03, 0x08, 0x01, 0x0D, 0xE2, 0xE1, 0x01, 0x00, 0xDC,
+ 0xE2, 0x01, 0x01, 0xDC, 0x29, 0x95, 0x2E, 0x01, 0x86, 0x03, 0x11, 0x06,
+ 0x08, 0x01, 0x00, 0xDD, 0xE0, 0x01, 0x01, 0xDD, 0x29, 0x50, 0xE0, 0x16,
+ 0x15, 0x2A, 0x5D, 0x06, 0x02, 0x29, 0x00, 0xE0, 0x1F, 0x2A, 0x06, 0x05,
+ 0x85, 0x47, 0xDA, 0x04, 0x77, 0x29, 0x04, 0x6C, 0x00, 0x9E, 0x01, 0x14,
+ 0xE2, 0x01, 0x0C, 0xE1, 0x85, 0x01, 0x0C, 0xDA, 0x00, 0x04, 0x03, 0x00,
+ 0x01, 0x02, 0xE2, 0x01, 0x80, 0x46, 0x8A, 0x30, 0x01, 0x02, 0x0F, 0x06,
+ 0x0C, 0x02, 0x00, 0x06, 0x04, 0x01, 0x05, 0x04, 0x02, 0x01, 0x1D, 0x04,
+ 0x02, 0x01, 0x00, 0x03, 0x01, 0x86, 0x30, 0x06, 0x04, 0x01, 0x05, 0x04,
+ 0x02, 0x01, 0x00, 0x03, 0x02, 0x8C, 0x2E, 0x2A, 0x06, 0x05, 0x62, 0x21,
+ 0x01, 0x07, 0x08, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x02, 0x03,
+ 0x08, 0x2A, 0x06, 0x03, 0x01, 0x02, 0x08, 0x08, 0xE1, 0x95, 0x2E, 0xE0,
+ 0x8E, 0x01, 0x04, 0x17, 0x8E, 0x01, 0x04, 0x08, 0x01, 0x1C, 0x34, 0x8E,
+ 0x01, 0x20, 0xDA, 0x01, 0x20, 0xE2, 0x8F, 0x01, 0x20, 0xDA, 0x78, 0x2E,
+ 0xE0, 0x01, 0x00, 0xE2, 0x02, 0x01, 0x02, 0x02, 0x08, 0x02, 0x03, 0x08,
+ 0x2A, 0x06, 0x80, 0x40, 0xE0, 0x02, 0x01, 0x2A, 0x06, 0x10, 0x01, 0x83,
+ 0xFE, 0x01, 0xE0, 0x01, 0x04, 0x09, 0x2A, 0xE0, 0x62, 0x8B, 0x47, 0xDB,
+ 0x04, 0x01, 0x29, 0x02, 0x02, 0x06, 0x0C, 0x01, 0x01, 0xE0, 0x01, 0x01,
+ 0xE0, 0x86, 0x30, 0x01, 0x08, 0x09, 0xE2, 0x02, 0x03, 0x2A, 0x06, 0x11,
+ 0x01, 0x10, 0xE0, 0x01, 0x04, 0x09, 0x2A, 0xE0, 0x64, 0x2A, 0xE0, 0x62,
+ 0x85, 0x47, 0xDB, 0x04, 0x01, 0x29, 0x04, 0x01, 0x29, 0x00, 0x00, 0x01,
+ 0x0E, 0xE2, 0x01, 0x00, 0xE1, 0x00, 0x03, 0x78, 0x2E, 0xCC, 0x05, 0x01,
+ 0x00, 0x7E, 0x2F, 0x2A, 0x01, 0x82, 0x80, 0x80, 0x80, 0x00, 0x13, 0x06,
+ 0x05, 0x29, 0x01, 0x1D, 0x04, 0x0E, 0x2A, 0x01, 0x83, 0xC0, 0x80, 0x80,
+ 0x00, 0x13, 0x2A, 0x06, 0x01, 0x47, 0x29, 0xA5, 0x03, 0x00, 0x02, 0x00,
+ 0x25, 0x2A, 0x5D, 0x06, 0x02, 0x37, 0x2B, 0x03, 0x01, 0x95, 0x2E, 0x01,
+ 0x86, 0x03, 0x11, 0x03, 0x02, 0x01, 0x0C, 0xE2, 0x02, 0x01, 0x80, 0x30,
+ 0x08, 0x02, 0x02, 0x01, 0x02, 0x13, 0x08, 0x01, 0x06, 0x08, 0xE1, 0x01,
+ 0x03, 0xE2, 0x02, 0x00, 0xE0, 0x7F, 0x80, 0x30, 0xDB, 0x02, 0x02, 0x06,
+ 0x1C, 0x92, 0x2E, 0x2A, 0x01, 0x83, 0xFE, 0x00, 0x0B, 0x06, 0x03, 0xE0,
+ 0x04, 0x0F, 0x01, 0x81, 0x7F, 0x13, 0xE2, 0x78, 0x2E, 0xCD, 0x01, 0x01,
+ 0x0C, 0x01, 0x03, 0x08, 0xE2, 0x02, 0x01, 0xE0, 0x85, 0x02, 0x01, 0xDA,
+ 0x00, 0x00, 0x56, 0x2A, 0x01, 0x00, 0x0F, 0x06, 0x02, 0x65, 0x00, 0xCF,
+ 0x29, 0x04, 0x73, 0x00, 0x2A, 0xE2, 0xDA, 0x00, 0x00, 0x01, 0x00, 0x78,
+ 0x2E, 0xCB, 0x06, 0x0C, 0x63, 0x3A, 0x06, 0x08, 0x01, 0x80, 0x41, 0xE2,
+ 0x01, 0x80, 0x42, 0xE2, 0x46, 0x06, 0x07, 0x61, 0x3A, 0x06, 0x03, 0x01,
+ 0x01, 0xE2, 0x45, 0x06, 0x08, 0x61, 0x3A, 0x06, 0x04, 0x01, 0x80, 0x40,
+ 0xE2, 0x47, 0x29, 0x00, 0x01, 0x01, 0x00, 0x03, 0x00, 0x46, 0x45, 0x39,
+ 0x05, 0x14, 0x01, 0x01, 0x01, 0x80, 0x7C, 0xDE, 0x03, 0x00, 0x01, 0x03,
+ 0x01, 0x80, 0x7C, 0xDE, 0x02, 0x00, 0x08, 0x47, 0x29, 0x00, 0x46, 0x06,
+ 0x07, 0x01, 0x01, 0x44, 0x29, 0xDE, 0x03, 0x00, 0x45, 0x06, 0x0A, 0x01,
+ 0x03, 0x44, 0x29, 0xDE, 0x02, 0x00, 0x08, 0x03, 0x00, 0x29, 0x02, 0x00,
+ 0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0xDF, 0x01, 0x05, 0xDF, 0x01, 0x06,
+ 0xDF, 0x01, 0x03, 0xDF, 0x01, 0x02, 0xDF, 0x0A, 0x65, 0x00, 0x01, 0x03,
+ 0x00, 0x3A, 0x01, 0x01, 0x02, 0x00, 0x0C, 0x13, 0x05, 0x01, 0x00, 0x63,
+ 0x01, 0x03, 0x3B, 0x06, 0x07, 0x02, 0x00, 0xE2, 0x01, 0x02, 0x3B, 0xE2,
+ 0x00, 0x00, 0x2A, 0x01, 0x08, 0x54, 0xE2, 0xE2, 0x00, 0x00, 0x2A, 0x01,
+ 0x10, 0x54, 0xE2, 0xE0, 0x00, 0x00, 0x2A, 0x57, 0x06, 0x02, 0x29, 0x00,
+ 0xCF, 0x29, 0x04, 0x76
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 15,
+ 20,
+ 25,
+ 30,
+ 35,
+ 40,
+ 44,
+ 48,
+ 52,
+ 56,
+ 60,
+ 64,
+ 68,
+ 72,
+ 76,
+ 80,
+ 84,
+ 88,
+ 92,
+ 96,
+ 100,
+ 104,
+ 109,
+ 114,
+ 119,
+ 124,
+ 129,
+ 134,
+ 139,
+ 144,
+ 149,
+ 154,
+ 159,
+ 164,
+ 169,
+ 174,
+ 180,
+ 185,
+ 190,
+ 195,
+ 200,
+ 205,
+ 210,
+ 215,
+ 220,
+ 225,
+ 230,
+ 235,
+ 240,
+ 245,
+ 250,
+ 255,
+ 260,
+ 265,
+ 270,
+ 275,
+ 280,
+ 285,
+ 290,
+ 299,
+ 303,
+ 328,
+ 334,
+ 353,
+ 364,
+ 405,
+ 516,
+ 520,
+ 553,
+ 563,
+ 587,
+ 669,
+ 683,
+ 689,
+ 748,
+ 767,
+ 789,
+ 838,
+ 887,
+ 963,
+ 1065,
+ 1076,
+ 1670,
+ 1674,
+ 1741,
+ 1751,
+ 1782,
+ 1806,
+ 1852,
+ 1922,
+ 1962,
+ 1976,
+ 1985,
+ 1989,
+ 2084,
+ 2092,
+ 2128,
+ 2139,
+ 2155,
+ 2161,
+ 2172,
+ 2207,
+ 2233,
+ 2245,
+ 2251,
+ 2264,
+ 2279,
+ 2472,
+ 2481,
+ 2494,
+ 2503,
+ 2510,
+ 2616,
+ 2641,
+ 2654,
+ 2670,
+ 2688,
+ 2720,
+ 2793,
+ 2806,
+ 2987,
+ 2995,
+ 3122,
+ 3136,
+ 3141,
+ 3185,
+ 3242,
+ 3263,
+ 3290,
+ 3298,
+ 3306
+};
+
+#define T0_INTERPRETED 93
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_ssl_hs_server_init_main, 166)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_ssl_hs_server_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* * */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a * b);
+
+ }
+ break;
+ case 8: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 9: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 10: {
+ /* -rot */
+ T0_NROT();
+ }
+ break;
+ case 11: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 12: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 13: {
+ /* <= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a <= b));
+
+ }
+ break;
+ case 14: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 15: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 16: {
+ /* > */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a > b));
+
+ }
+ break;
+ case 17: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 18: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 19: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 20: {
+ /* begin-cert */
+
+ if (ENG->chain_len == 0) {
+ T0_PUSHi(-1);
+ } else {
+ ENG->cert_cur = ENG->chain->data;
+ ENG->cert_len = ENG->chain->data_len;
+ ENG->chain ++;
+ ENG->chain_len --;
+ T0_PUSH(ENG->cert_len);
+ }
+
+ }
+ break;
+ case 21: {
+ /* begin-ta-name */
+
+ const br_x500_name *dn;
+ if (CTX->cur_dn_index >= CTX->num_tas) {
+ T0_PUSHi(-1);
+ } else {
+ if (CTX->ta_names == NULL) {
+ dn = &CTX->tas[CTX->cur_dn_index].dn;
+ } else {
+ dn = &CTX->ta_names[CTX->cur_dn_index];
+ }
+ CTX->cur_dn_index ++;
+ CTX->cur_dn = dn->data;
+ CTX->cur_dn_len = dn->len;
+ T0_PUSH(CTX->cur_dn_len);
+ }
+
+ }
+ break;
+ case 22: {
+ /* begin-ta-name-list */
+
+ CTX->cur_dn_index = 0;
+
+ }
+ break;
+ case 23: {
+ /* bzero */
+
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ memset(addr, 0, len);
+
+ }
+ break;
+ case 24: {
+ /* call-policy-handler */
+
+ int x;
+ br_ssl_server_choices choices;
+
+ x = (*CTX->policy_vtable)->choose(
+ CTX->policy_vtable, CTX, &choices);
+ ENG->session.cipher_suite = choices.cipher_suite;
+ CTX->sign_hash_id = choices.algo_id;
+ ENG->chain = choices.chain;
+ ENG->chain_len = choices.chain_len;
+ T0_PUSHi(-(x != 0));
+
+ }
+ break;
+ case 25: {
+ /* can-output? */
+
+ T0_PUSHi(-(ENG->hlen_out > 0));
+
+ }
+ break;
+ case 26: {
+ /* check-resume */
+
+ if (ENG->session.session_id_len == 32
+ && CTX->cache_vtable != NULL && (*CTX->cache_vtable)->load(
+ CTX->cache_vtable, CTX, &ENG->session))
+ {
+ T0_PUSHi(-1);
+ } else {
+ T0_PUSH(0);
+ }
+
+ }
+ break;
+ case 27: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 28: {
+ /* compute-Finished-inner */
+
+ int prf_id = T0_POP();
+ int from_client = T0_POPi();
+ unsigned char tmp[48];
+ br_tls_prf_seed_chunk seed;
+
+ br_tls_prf_impl prf = br_ssl_engine_get_PRF(ENG, prf_id);
+ seed.data = tmp;
+ if (ENG->session.version >= BR_TLS12) {
+ seed.len = br_multihash_out(&ENG->mhash, prf_id, tmp);
+ } else {
+ br_multihash_out(&ENG->mhash, br_md5_ID, tmp);
+ br_multihash_out(&ENG->mhash, br_sha1_ID, tmp + 16);
+ seed.len = 36;
+ }
+ prf(ENG->pad, 12, ENG->session.master_secret,
+ sizeof ENG->session.master_secret,
+ from_client ? "client finished" : "server finished",
+ 1, &seed);
+
+ }
+ break;
+ case 29: {
+ /* compute-hash-CV */
+
+ int i;
+
+ for (i = 1; i <= 6; i ++) {
+ br_multihash_out(&ENG->mhash, i,
+ ENG->pad + HASH_PAD_OFF[i - 1]);
+ }
+
+ }
+ break;
+ case 30: {
+ /* copy-cert-chunk */
+
+ size_t clen;
+
+ clen = ENG->cert_len;
+ if (clen > sizeof ENG->pad) {
+ clen = sizeof ENG->pad;
+ }
+ memcpy(ENG->pad, ENG->cert_cur, clen);
+ ENG->cert_cur += clen;
+ ENG->cert_len -= clen;
+ T0_PUSH(clen);
+
+ }
+ break;
+ case 31: {
+ /* copy-dn-chunk */
+
+ size_t clen;
+
+ clen = CTX->cur_dn_len;
+ if (clen > sizeof ENG->pad) {
+ clen = sizeof ENG->pad;
+ }
+ memcpy(ENG->pad, CTX->cur_dn, clen);
+ CTX->cur_dn += clen;
+ CTX->cur_dn_len -= clen;
+ T0_PUSH(clen);
+
+ }
+ break;
+ case 32: {
+ /* copy-hash-CV */
+
+ int id = T0_POP();
+ size_t off, len;
+
+ if (id == 0) {
+ off = 0;
+ len = 36;
+ } else {
+ if (br_multihash_getimpl(&ENG->mhash, id) == 0) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ off = HASH_PAD_OFF[id - 1];
+ len = HASH_PAD_OFF[id] - off;
+ }
+ memcpy(CTX->hash_CV, ENG->pad + off, len);
+ CTX->hash_CV_len = len;
+ CTX->hash_CV_id = id;
+ T0_PUSHi(-1);
+
+ }
+ break;
+ case 33: {
+ /* copy-protocol-name */
+
+ size_t idx = T0_POP();
+ size_t len = strlen(ENG->protocol_names[idx]);
+ memcpy(ENG->pad, ENG->protocol_names[idx], len);
+ T0_PUSH(len);
+
+ }
+ break;
+ case 34: {
+ /* data-get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(t0_datablock[addr]);
+
+ }
+ break;
+ case 35: {
+ /* discard-input */
+
+ ENG->hlen_in = 0;
+
+ }
+ break;
+ case 36: {
+ /* do-ecdh */
+
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_ecdh(CTX, prf_id, ENG->pad, len);
+
+ }
+ break;
+ case 37: {
+ /* do-ecdhe-part1 */
+
+ int curve = T0_POPi();
+ T0_PUSHi(do_ecdhe_part1(CTX, curve));
+
+ }
+ break;
+ case 38: {
+ /* do-ecdhe-part2 */
+
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_ecdhe_part2(CTX, prf_id, ENG->pad, len);
+
+ }
+ break;
+ case 39: {
+ /* do-rsa-decrypt */
+
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_rsa_decrypt(CTX, prf_id, ENG->pad, len);
+
+ }
+ break;
+ case 40: {
+ /* do-static-ecdh */
+
+ do_static_ecdh(CTX, T0_POP());
+
+ }
+ break;
+ case 41: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 42: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 43: {
+ /* fail */
+
+ br_ssl_engine_fail(ENG, (int)T0_POPi());
+ T0_CO();
+
+ }
+ break;
+ case 44: {
+ /* flush-record */
+
+ br_ssl_engine_flush_record(ENG);
+
+ }
+ break;
+ case 45: {
+ /* get-key-type-usages */
+
+ const br_x509_class *xc;
+ const br_x509_pkey *pk;
+ unsigned usages;
+
+ xc = *(ENG->x509ctx);
+ pk = xc->get_pkey(ENG->x509ctx, &usages);
+ if (pk == NULL) {
+ T0_PUSH(0);
+ } else {
+ T0_PUSH(pk->key_type | usages);
+ }
+
+ }
+ break;
+ case 46: {
+ /* get16 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint16_t *)(void *)((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 47: {
+ /* get32 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*(uint32_t *)(void *)((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 48: {
+ /* get8 */
+
+ size_t addr = (size_t)T0_POP();
+ T0_PUSH(*((unsigned char *)ENG + addr));
+
+ }
+ break;
+ case 49: {
+ /* has-input? */
+
+ T0_PUSHi(-(ENG->hlen_in != 0));
+
+ }
+ break;
+ case 50: {
+ /* memcmp */
+
+ size_t len = (size_t)T0_POP();
+ void *addr2 = (unsigned char *)ENG + (size_t)T0_POP();
+ void *addr1 = (unsigned char *)ENG + (size_t)T0_POP();
+ int x = memcmp(addr1, addr2, len);
+ T0_PUSH((uint32_t)-(x == 0));
+
+ }
+ break;
+ case 51: {
+ /* memcpy */
+
+ size_t len = (size_t)T0_POP();
+ void *src = (unsigned char *)ENG + (size_t)T0_POP();
+ void *dst = (unsigned char *)ENG + (size_t)T0_POP();
+ memcpy(dst, src, len);
+
+ }
+ break;
+ case 52: {
+ /* mkrand */
+
+ size_t len = (size_t)T0_POP();
+ void *addr = (unsigned char *)ENG + (size_t)T0_POP();
+ br_hmac_drbg_generate(&ENG->rng, addr, len);
+
+ }
+ break;
+ case 53: {
+ /* more-incoming-bytes? */
+
+ T0_PUSHi(ENG->hlen_in != 0 || !br_ssl_engine_recvrec_finished(ENG));
+
+ }
+ break;
+ case 54: {
+ /* multihash-init */
+
+ br_multihash_init(&ENG->mhash);
+
+ }
+ break;
+ case 55: {
+ /* neg */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(-a);
+
+ }
+ break;
+ case 56: {
+ /* not */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(~a);
+
+ }
+ break;
+ case 57: {
+ /* or */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a | b);
+
+ }
+ break;
+ case 58: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 59: {
+ /* pick */
+ T0_PICK(T0_POP());
+ }
+ break;
+ case 60: {
+ /* read-chunk-native */
+
+ size_t clen = ENG->hlen_in;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy((unsigned char *)ENG + addr, ENG->hbuf_in, clen);
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_in, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_in += clen;
+ ENG->hlen_in -= clen;
+ }
+
+ }
+ break;
+ case 61: {
+ /* read8-native */
+
+ if (ENG->hlen_in > 0) {
+ unsigned char x;
+
+ x = *ENG->hbuf_in ++;
+ if (ENG->record_type_in == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ T0_PUSH(x);
+ ENG->hlen_in --;
+ } else {
+ T0_PUSHi(-1);
+ }
+
+ }
+ break;
+ case 62: {
+ /* save-session */
+
+ if (CTX->cache_vtable != NULL) {
+ (*CTX->cache_vtable)->save(
+ CTX->cache_vtable, CTX, &ENG->session);
+ }
+
+ }
+ break;
+ case 63: {
+ /* set-max-frag-len */
+
+ size_t max_frag_len = T0_POP();
+
+ br_ssl_engine_new_max_frag_len(ENG, max_frag_len);
+
+ /*
+ * We must adjust our own output limit. Since we call this only
+ * after receiving a ClientHello and before beginning to send
+ * the ServerHello, the next output record should be empty at
+ * that point, so we can use max_frag_len as a limit.
+ */
+ if (ENG->hlen_out > max_frag_len) {
+ ENG->hlen_out = max_frag_len;
+ }
+
+ }
+ break;
+ case 64: {
+ /* set16 */
+
+ size_t addr = (size_t)T0_POP();
+ *(uint16_t *)(void *)((unsigned char *)ENG + addr) = (uint16_t)T0_POP();
+
+ }
+ break;
+ case 65: {
+ /* set32 */
+
+ size_t addr = (size_t)T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)ENG + addr) = (uint32_t)T0_POP();
+
+ }
+ break;
+ case 66: {
+ /* set8 */
+
+ size_t addr = (size_t)T0_POP();
+ *((unsigned char *)ENG + addr) = (unsigned char)T0_POP();
+
+ }
+ break;
+ case 67: {
+ /* supported-curves */
+
+ uint32_t x = ENG->iec == NULL ? 0 : ENG->iec->supported_curves;
+ T0_PUSH(x);
+
+ }
+ break;
+ case 68: {
+ /* supported-hash-functions */
+
+ int i;
+ unsigned x, num;
+
+ x = 0;
+ num = 0;
+ for (i = br_sha1_ID; i <= br_sha512_ID; i ++) {
+ if (br_multihash_getimpl(&ENG->mhash, i)) {
+ x |= 1U << i;
+ num ++;
+ }
+ }
+ T0_PUSH(x);
+ T0_PUSH(num);
+
+ }
+ break;
+ case 69: {
+ /* supports-ecdsa? */
+
+ T0_PUSHi(-(ENG->iecdsa != 0));
+
+ }
+ break;
+ case 70: {
+ /* supports-rsa-sign? */
+
+ T0_PUSHi(-(ENG->irsavrfy != 0));
+
+ }
+ break;
+ case 71: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ case 72: {
+ /* switch-aesccm-in */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+
+ }
+ break;
+ case 73: {
+ /* switch-aesccm-out */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len, tag_len;
+
+ tag_len = T0_POP();
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_ccm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctrcbc, cipher_key_len, tag_len);
+
+ }
+ break;
+ case 74: {
+ /* switch-aesgcm-in */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_in(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+
+ }
+ break;
+ case 75: {
+ /* switch-aesgcm-out */
+
+ int is_client, prf_id;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_gcm_out(ENG, is_client, prf_id,
+ ENG->iaes_ctr, cipher_key_len);
+
+ }
+ break;
+ case 76: {
+ /* switch-cbc-in */
+
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_in(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcdec : ENG->ides_cbcdec, cipher_key_len);
+
+ }
+ break;
+ case 77: {
+ /* switch-cbc-out */
+
+ int is_client, prf_id, mac_id, aes;
+ unsigned cipher_key_len;
+
+ cipher_key_len = T0_POP();
+ aes = T0_POP();
+ mac_id = T0_POP();
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_cbc_out(ENG, is_client, prf_id, mac_id,
+ aes ? ENG->iaes_cbcenc : ENG->ides_cbcenc, cipher_key_len);
+
+ }
+ break;
+ case 78: {
+ /* switch-chapol-in */
+
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_in(ENG, is_client, prf_id);
+
+ }
+ break;
+ case 79: {
+ /* switch-chapol-out */
+
+ int is_client, prf_id;
+
+ prf_id = T0_POP();
+ is_client = T0_POP();
+ br_ssl_engine_switch_chapol_out(ENG, is_client, prf_id);
+
+ }
+ break;
+ case 80: {
+ /* ta-names-total-length */
+
+ size_t u, len;
+
+ len = 0;
+ if (CTX->ta_names != NULL) {
+ for (u = 0; u < CTX->num_tas; u ++) {
+ len += CTX->ta_names[u].len + 2;
+ }
+ } else if (CTX->tas != NULL) {
+ for (u = 0; u < CTX->num_tas; u ++) {
+ len += CTX->tas[u].dn.len + 2;
+ }
+ }
+ T0_PUSH(len);
+
+ }
+ break;
+ case 81: {
+ /* test-protocol-name */
+
+ size_t len = T0_POP();
+ size_t u;
+
+ for (u = 0; u < ENG->protocol_names_num; u ++) {
+ const char *name;
+
+ name = ENG->protocol_names[u];
+ if (len == strlen(name) && memcmp(ENG->pad, name, len) == 0) {
+ T0_PUSH(u);
+ T0_RET();
+ }
+ }
+ T0_PUSHi(-1);
+
+ }
+ break;
+ case 82: {
+ /* total-chain-length */
+
+ size_t u;
+ uint32_t total;
+
+ total = 0;
+ for (u = 0; u < ENG->chain_len; u ++) {
+ total += 3 + (uint32_t)ENG->chain[u].data_len;
+ }
+ T0_PUSH(total);
+
+ }
+ break;
+ case 83: {
+ /* u< */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 84: {
+ /* u>> */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x >> c);
+
+ }
+ break;
+ case 85: {
+ /* verify-CV-sig */
+
+ int err;
+
+ err = verify_CV_sig(CTX, T0_POP());
+ T0_PUSHi(err);
+
+ }
+ break;
+ case 86: {
+ /* write-blob-chunk */
+
+ size_t clen = ENG->hlen_out;
+ if (clen > 0) {
+ uint32_t addr, len;
+
+ len = T0_POP();
+ addr = T0_POP();
+ if ((size_t)len < clen) {
+ clen = (size_t)len;
+ }
+ memcpy(ENG->hbuf_out, (unsigned char *)ENG + addr, clen);
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, ENG->hbuf_out, clen);
+ }
+ T0_PUSH(addr + (uint32_t)clen);
+ T0_PUSH(len - (uint32_t)clen);
+ ENG->hbuf_out += clen;
+ ENG->hlen_out -= clen;
+ }
+
+ }
+ break;
+ case 87: {
+ /* write8-native */
+
+ unsigned char x;
+
+ x = (unsigned char)T0_POP();
+ if (ENG->hlen_out > 0) {
+ if (ENG->record_type_out == BR_SSL_HANDSHAKE) {
+ br_multihash_update(&ENG->mhash, &x, 1);
+ }
+ *ENG->hbuf_out ++ = x;
+ ENG->hlen_out --;
+ T0_PUSHi(-1);
+ } else {
+ T0_PUSHi(0);
+ }
+
+ }
+ break;
+ case 88: {
+ /* x509-append */
+
+ const br_x509_class *xc;
+ size_t len;
+
+ xc = *(ENG->x509ctx);
+ len = T0_POP();
+ xc->append(ENG->x509ctx, ENG->pad, len);
+
+ }
+ break;
+ case 89: {
+ /* x509-end-cert */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->end_cert(ENG->x509ctx);
+
+ }
+ break;
+ case 90: {
+ /* x509-end-chain */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ T0_PUSH(xc->end_chain(ENG->x509ctx));
+
+ }
+ break;
+ case 91: {
+ /* x509-start-cert */
+
+ const br_x509_class *xc;
+
+ xc = *(ENG->x509ctx);
+ xc->start_cert(ENG->x509ctx, T0_POP());
+
+ }
+ break;
+ case 92: {
+ /* x509-start-chain */
+
+ const br_x509_class *xc;
+ uint32_t bc;
+
+ bc = T0_POP();
+ xc = *(ENG->x509ctx);
+ xc->start_chain(ENG->x509ctx, bc ? ENG->server_name : NULL);
+
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_hs_server.t0 b/test/monniaux/BearSSL/src/ssl/ssl_hs_server.t0
new file mode 100644
index 00000000..9f6e934e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_hs_server.t0
@@ -0,0 +1,1510 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+\ ----------------------------------------------------------------------
+\ Handshake processing code, for the server.
+\ The common T0 code (ssl_hs_common.t0) shall be read first.
+
+preamble {
+
+/*
+ * This macro evaluates to a pointer to the server context, under that
+ * specific name. It must be noted that since the engine context is the
+ * first field of the br_ssl_server_context structure ('eng'), then
+ * pointers values of both types are interchangeable, modulo an
+ * appropriate cast. This also means that "addresses" computed as offsets
+ * within the structure work for both kinds of context.
+ */
+#define CTX ((br_ssl_server_context *)ENG)
+
+/*
+ * Decrypt the pre-master secret (RSA key exchange).
+ */
+static void
+do_rsa_decrypt(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *epms, size_t len)
+{
+ uint32_t x;
+ unsigned char rpms[48];
+
+ /*
+ * Decrypt the PMS.
+ */
+ x = (*ctx->policy_vtable)->do_keyx(ctx->policy_vtable, epms, &len);
+
+ /*
+ * Set the first two bytes to the maximum supported client
+ * protocol version. These bytes are used for version rollback
+ * detection; forceing the two bytes will make the master secret
+ * wrong if the bytes are not correct. This process is
+ * recommended by RFC 5246 (section 7.4.7.1).
+ */
+ br_enc16be(epms, ctx->client_max_version);
+
+ /*
+ * Make a random PMS and copy it above the decrypted value if the
+ * decryption failed. Note that we use a constant-time conditional
+ * copy.
+ */
+ br_hmac_drbg_generate(&ctx->eng.rng, rpms, sizeof rpms);
+ br_ccopy(x ^ 1, epms, rpms, sizeof rpms);
+
+ /*
+ * Compute master secret.
+ */
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, epms, 48);
+
+ /*
+ * Clear the pre-master secret from RAM: it is normally a buffer
+ * in the context, hence potentially long-lived.
+ */
+ memset(epms, 0, len);
+}
+
+/*
+ * Common part for ECDH and ECDHE.
+ */
+static void
+ecdh_common(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *xcoor, size_t xcoor_len, uint32_t ctl)
+{
+ unsigned char rpms[80];
+
+ if (xcoor_len > sizeof rpms) {
+ xcoor_len = sizeof rpms;
+ ctl = 0;
+ }
+
+ /*
+ * Make a random PMS and copy it above the decrypted value if the
+ * decryption failed. Note that we use a constant-time conditional
+ * copy.
+ */
+ br_hmac_drbg_generate(&ctx->eng.rng, rpms, xcoor_len);
+ br_ccopy(ctl ^ 1, xcoor, rpms, xcoor_len);
+
+ /*
+ * Compute master secret.
+ */
+ br_ssl_engine_compute_master(&ctx->eng, prf_id, xcoor, xcoor_len);
+
+ /*
+ * Clear the pre-master secret from RAM: it is normally a buffer
+ * in the context, hence potentially long-lived.
+ */
+ memset(xcoor, 0, xcoor_len);
+}
+
+/*
+ * Do the ECDH key exchange (not ECDHE).
+ */
+static void
+do_ecdh(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *cpoint, size_t cpoint_len)
+{
+ uint32_t x;
+
+ /*
+ * Finalise the key exchange.
+ */
+ x = (*ctx->policy_vtable)->do_keyx(ctx->policy_vtable,
+ cpoint, &cpoint_len);
+ ecdh_common(ctx, prf_id, cpoint, cpoint_len, x);
+}
+
+/*
+ * Do the full static ECDH key exchange. When this function is called,
+ * it has already been verified that the cipher suite uses ECDH (not ECDHE),
+ * and the client's public key (from its certificate) has type EC and is
+ * apt for key exchange.
+ */
+static void
+do_static_ecdh(br_ssl_server_context *ctx, int prf_id)
+{
+ unsigned char cpoint[133];
+ size_t cpoint_len;
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ cpoint_len = pk->key.ec.qlen;
+ if (cpoint_len > sizeof cpoint) {
+ /*
+ * If the point is larger than our buffer then we need to
+ * restrict it. Length 2 is not a valid point length, so
+ * the ECDH will fail.
+ */
+ cpoint_len = 2;
+ }
+ memcpy(cpoint, pk->key.ec.q, cpoint_len);
+ do_ecdh(ctx, prf_id, cpoint, cpoint_len);
+}
+
+static size_t
+hash_data(br_ssl_server_context *ctx,
+ void *dst, int hash_id, const void *src, size_t len)
+{
+ const br_hash_class *hf;
+ br_hash_compat_context hc;
+
+ if (hash_id == 0) {
+ unsigned char tmp[36];
+
+ hf = br_multihash_getimpl(&ctx->eng.mhash, br_md5_ID);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, tmp);
+ hf = br_multihash_getimpl(&ctx->eng.mhash, br_sha1_ID);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, tmp + 16);
+ memcpy(dst, tmp, 36);
+ return 36;
+ } else {
+ hf = br_multihash_getimpl(&ctx->eng.mhash, hash_id);
+ if (hf == NULL) {
+ return 0;
+ }
+ hf->init(&hc.vtable);
+ hf->update(&hc.vtable, src, len);
+ hf->out(&hc.vtable, dst);
+ return (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
+ }
+}
+
+/*
+ * Do the ECDHE key exchange (part 1: generation of transient key, and
+ * computing of the point to send to the client). Returned value is the
+ * signature length (in bytes), or -x on error (with x being an error
+ * code). The encoded point is written in the ecdhe_point[] context buffer
+ * (length in ecdhe_point_len).
+ */
+static int
+do_ecdhe_part1(br_ssl_server_context *ctx, int curve)
+{
+ unsigned algo_id;
+ unsigned mask;
+ const unsigned char *order;
+ size_t olen, glen;
+ size_t hv_len, sig_len;
+
+ if (!((ctx->eng.iec->supported_curves >> curve) & 1)) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+ ctx->eng.ecdhe_curve = curve;
+
+ /*
+ * Generate our private key. We need a non-zero random value
+ * which is lower than the curve order, in a "large enough"
+ * range. We force the top bit to 0 and bottom bit to 1, which
+ * does the trick. Note that contrary to what happens in ECDSA,
+ * this is not a problem if we do not cover the full range of
+ * possible values.
+ */
+ order = ctx->eng.iec->order(curve, &olen);
+ mask = 0xFF;
+ while (mask >= order[0]) {
+ mask >>= 1;
+ }
+ br_hmac_drbg_generate(&ctx->eng.rng, ctx->ecdhe_key, olen);
+ ctx->ecdhe_key[0] &= mask;
+ ctx->ecdhe_key[olen - 1] |= 0x01;
+ ctx->ecdhe_key_len = olen;
+
+ /*
+ * Compute our ECDH point.
+ */
+ glen = ctx->eng.iec->mulgen(ctx->eng.ecdhe_point,
+ ctx->ecdhe_key, olen, curve);
+ ctx->eng.ecdhe_point_len = glen;
+
+ /*
+ * Assemble the message to be signed, and possibly hash it.
+ */
+ memcpy(ctx->eng.pad, ctx->eng.client_random, 32);
+ memcpy(ctx->eng.pad + 32, ctx->eng.server_random, 32);
+ ctx->eng.pad[64 + 0] = 0x03;
+ ctx->eng.pad[64 + 1] = 0x00;
+ ctx->eng.pad[64 + 2] = curve;
+ ctx->eng.pad[64 + 3] = ctx->eng.ecdhe_point_len;
+ memcpy(ctx->eng.pad + 64 + 4,
+ ctx->eng.ecdhe_point, ctx->eng.ecdhe_point_len);
+ hv_len = 64 + 4 + ctx->eng.ecdhe_point_len;
+ algo_id = ctx->sign_hash_id;
+ if (algo_id >= (unsigned)0xFF00) {
+ hv_len = hash_data(ctx, ctx->eng.pad, algo_id & 0xFF,
+ ctx->eng.pad, hv_len);
+ if (hv_len == 0) {
+ return -BR_ERR_INVALID_ALGORITHM;
+ }
+ }
+
+ sig_len = (*ctx->policy_vtable)->do_sign(ctx->policy_vtable,
+ algo_id, ctx->eng.pad, hv_len, sizeof ctx->eng.pad);
+ return sig_len ? (int)sig_len : -BR_ERR_INVALID_ALGORITHM;
+}
+
+/*
+ * Do the ECDHE key exchange (part 2: computation of the shared secret
+ * from the point sent by the client).
+ */
+static void
+do_ecdhe_part2(br_ssl_server_context *ctx, int prf_id,
+ unsigned char *cpoint, size_t cpoint_len)
+{
+ int curve;
+ uint32_t ctl;
+ size_t xoff, xlen;
+
+ curve = ctx->eng.ecdhe_curve;
+
+ /*
+ * Finalise the key exchange.
+ */
+ ctl = ctx->eng.iec->mul(cpoint, cpoint_len,
+ ctx->ecdhe_key, ctx->ecdhe_key_len, curve);
+ xoff = ctx->eng.iec->xoff(curve, &xlen);
+ ecdh_common(ctx, prf_id, cpoint + xoff, xlen, ctl);
+
+ /*
+ * Clear the ECDHE private key. Forward Secrecy is achieved insofar
+ * as that key does not get stolen, so we'd better destroy it
+ * as soon as it ceases to be useful.
+ */
+ memset(ctx->ecdhe_key, 0, ctx->ecdhe_key_len);
+}
+
+/*
+ * Offset for hash value within the pad (when obtaining all hash values,
+ * in preparation for verification of the CertificateVerify message).
+ * Order is MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512; last value
+ * is used to get the total length.
+ */
+static const unsigned char HASH_PAD_OFF[] = { 0, 16, 36, 64, 96, 144, 208 };
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char HASH_OID_SHA1[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char HASH_OID_SHA224[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char HASH_OID_SHA256[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char HASH_OID_SHA384[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char HASH_OID_SHA512[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static const unsigned char *HASH_OID[] = {
+ HASH_OID_SHA1,
+ HASH_OID_SHA224,
+ HASH_OID_SHA256,
+ HASH_OID_SHA384,
+ HASH_OID_SHA512
+};
+
+/*
+ * Verify the signature in CertificateVerify. Returned value is 0 on
+ * success, or a non-zero error code. Lack of implementation of the
+ * designated signature algorithm is reported as a "bad signature"
+ * error (because it means that the peer did not honour our advertised
+ * set of supported signature algorithms).
+ */
+static int
+verify_CV_sig(br_ssl_server_context *ctx, size_t sig_len)
+{
+ const br_x509_class **xc;
+ const br_x509_pkey *pk;
+ int id;
+
+ id = ctx->hash_CV_id;
+ xc = ctx->eng.x509ctx;
+ pk = (*xc)->get_pkey(xc, NULL);
+ if (pk->key_type == BR_KEYTYPE_RSA) {
+ unsigned char tmp[64];
+ const unsigned char *hash_oid;
+
+ if (id == 0) {
+ hash_oid = NULL;
+ } else {
+ hash_oid = HASH_OID[id - 2];
+ }
+ if (ctx->eng.irsavrfy == 0) {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ if (!ctx->eng.irsavrfy(ctx->eng.pad, sig_len,
+ hash_oid, ctx->hash_CV_len, &pk->key.rsa, tmp)
+ || memcmp(tmp, ctx->hash_CV, ctx->hash_CV_len) != 0)
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ } else {
+ if (ctx->eng.iecdsa == 0) {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ if (!ctx->eng.iecdsa(ctx->eng.iec,
+ ctx->hash_CV, ctx->hash_CV_len,
+ &pk->key.ec, ctx->eng.pad, sig_len))
+ {
+ return BR_ERR_BAD_SIGNATURE;
+ }
+ }
+ return 0;
+}
+
+}
+
+\ =======================================================================
+
+: addr-ctx:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(br_ssl_server_context, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr-ctx: client_max_version
+addr-ctx: client_suites
+addr-ctx: client_suites_num
+addr-ctx: hashes
+addr-ctx: curves
+addr-ctx: sign_hash_id
+
+\ Get address and length of the client_suites[] buffer. Length is expressed
+\ in bytes.
+: addr-len-client_suites ( -- addr len )
+ addr-client_suites
+ CX 0 1023 { BR_MAX_CIPHER_SUITES * sizeof(br_suite_translated) } ;
+
+\ Read the client SNI extension.
+: read-client-sni ( lim -- lim )
+ \ Open extension value.
+ read16 open-elt
+
+ \ Open ServerNameList.
+ read16 open-elt
+
+ \ Find if there is a name of type 0 (host_name) with a length
+ \ that fits in our dedicated buffer.
+ begin dup while
+ read8 if
+ read-ignore-16
+ else
+ read16
+ dup 255 <= if
+ dup addr-server_name + 0 swap set8
+ addr-server_name swap read-blob
+ else
+ skip-blob
+ then
+ then
+ repeat
+
+ \ Close ServerNameList.
+ close-elt
+
+ \ Close extension value.
+ close-elt ;
+
+\ Set the new maximum fragment length. BEWARE: this shall be called only
+\ after reading the ClientHello and before writing the ServerHello.
+cc: set-max-frag-len ( len -- ) {
+ size_t max_frag_len = T0_POP();
+
+ br_ssl_engine_new_max_frag_len(ENG, max_frag_len);
+
+ /*
+ * We must adjust our own output limit. Since we call this only
+ * after receiving a ClientHello and before beginning to send
+ * the ServerHello, the next output record should be empty at
+ * that point, so we can use max_frag_len as a limit.
+ */
+ if (ENG->hlen_out > max_frag_len) {
+ ENG->hlen_out = max_frag_len;
+ }
+}
+
+\ Read the client Max Frag Length extension.
+: read-client-frag ( lim -- lim )
+ \ Extension value must have length exactly 1 byte.
+ read16 1 <> if ERR_BAD_FRAGLEN fail then
+ read8
+
+ \ The byte value must be 1, 2, 3 or 4.
+ dup dup 0= swap 5 >= or if ERR_BAD_FRAGLEN fail then
+
+ \ If our own maximum fragment length is greater, then we reduce
+ \ our length.
+ 8 + dup addr-log_max_frag_len get8 < if
+ dup 1 swap << set-max-frag-len
+ dup addr-log_max_frag_len set8
+ addr-peer_log_max_frag_len set8
+ else
+ drop
+ then ;
+
+\ Read the Secure Renegotiation extension from the client.
+: read-client-reneg ( lim -- lim )
+ \ Get value length.
+ read16
+
+ \ The "reneg" value is one of:
+ \ 0 on first handshake, client support is unknown
+ \ 1 client does not support secure renegotiation
+ \ 2 client supports secure renegotiation
+ addr-reneg get8 case
+ 0 of
+ \ First handshake, value length shall be 1.
+ 1 = ifnot ERR_BAD_SECRENEG fail then
+ read8 if ERR_BAD_SECRENEG fail then
+ 2 addr-reneg set8
+ endof
+ 2 of
+ \ Renegotiation, value shall consist of 13 bytes
+ \ (header + copy of the saved client "Finished").
+ 13 = ifnot ERR_BAD_SECRENEG fail then
+ read8 12 = ifnot ERR_BAD_SECRENEG fail then
+ addr-pad 12 read-blob
+ addr-saved_finished addr-pad 12 memcmp ifnot
+ ERR_BAD_SECRENEG fail
+ then
+ endof
+
+ \ If "reneg" is 1 then the client is not supposed to support
+ \ the extension, and it sends it nonetheless, which means
+ \ foul play.
+ ERR_BAD_SECRENEG fail
+ endcase ;
+
+\ Read the Signature Algorithms extension.
+: read-signatures ( lim -- lim )
+ \ Open extension value.
+ read16 open-elt
+
+ read-list-sign-algos addr-hashes set32
+
+ \ Close extension value.
+ close-elt ;
+
+\ Read the Supported Curves extension.
+: read-supported-curves ( lim -- lim )
+ \ Open extension value.
+ read16 open-elt
+
+ \ Open list of curve identifiers.
+ read16 open-elt
+
+ \ Get all supported curves.
+ 0 addr-curves set32
+ begin dup while
+ read16 dup 32 < if
+ 1 swap << addr-curves get32 or addr-curves set32
+ else
+ drop
+ then
+ repeat
+ close-elt
+ close-elt ;
+
+\ Read the ALPN extension from client.
+: read-ALPN-from-client ( lim -- lim )
+ \ If we do not have configured names, then we just ignore the
+ \ extension.
+ addr-protocol_names_num get16 ifnot read-ignore-16 ret then
+
+ \ Open extension value.
+ read16 open-elt
+
+ \ Open list of protocol names.
+ read16 open-elt
+
+ \ Get all names and test for their support. We keep the one with
+ \ the lowest index (because we apply server's preferences, as
+ \ recommended by RFC 7301, section 3.2. We set the 'found' variable
+ \ to -2 and use an unsigned comparison, making -2 a huge value.
+ -2 { found }
+ begin dup while
+ read8 dup { len } addr-pad swap read-blob
+ len test-protocol-name dup found u< if
+ >found
+ else
+ drop
+ then
+ repeat
+
+ \ End of extension.
+ close-elt
+ close-elt
+
+ \ Write back found name index (or not). If no match was found,
+ \ then we write -1 (0xFFFF) in the index value, not 0, so that
+ \ the caller knows that we tried to match, and failed.
+ found 1+ addr-selected_protocol set16 ;
+
+\ Call policy handler to get cipher suite, hash function identifier and
+\ certificate chain. Returned value is 0 (false) on failure.
+cc: call-policy-handler ( -- bool ) {
+ int x;
+ br_ssl_server_choices choices;
+
+ x = (*CTX->policy_vtable)->choose(
+ CTX->policy_vtable, CTX, &choices);
+ ENG->session.cipher_suite = choices.cipher_suite;
+ CTX->sign_hash_id = choices.algo_id;
+ ENG->chain = choices.chain;
+ ENG->chain_len = choices.chain_len;
+ T0_PUSHi(-(x != 0));
+}
+
+\ Check for a remembered session.
+cc: check-resume ( -- bool ) {
+ if (ENG->session.session_id_len == 32
+ && CTX->cache_vtable != NULL && (*CTX->cache_vtable)->load(
+ CTX->cache_vtable, CTX, &ENG->session))
+ {
+ T0_PUSHi(-1);
+ } else {
+ T0_PUSH(0);
+ }
+}
+
+\ Save the current session.
+cc: save-session ( -- ) {
+ if (CTX->cache_vtable != NULL) {
+ (*CTX->cache_vtable)->save(
+ CTX->cache_vtable, CTX, &ENG->session);
+ }
+}
+
+\ Read and drop ClientHello. This is used when a client-triggered
+\ renegotiation attempt is rejected.
+: skip-ClientHello ( -- )
+ read-handshake-header-core
+ 1 = ifnot ERR_UNEXPECTED fail then
+ dup skip-blob drop ;
+
+\ Read ClientHello. If the session is resumed, then -1 is returned.
+: read-ClientHello ( -- resume )
+ \ Get header, and check message type.
+ read-handshake-header 1 = ifnot ERR_UNEXPECTED fail then
+
+ \ Get maximum protocol version from client.
+ read16 dup { client-version-max } addr-client_max_version set16
+
+ \ Client random.
+ addr-client_random 32 read-blob
+
+ \ Client session ID.
+ read8 dup 32 > if ERR_OVERSIZED_ID fail then
+ dup addr-session_id_len set8
+ addr-session_id swap read-blob
+
+ \ Lookup session for resumption. We should do that here because
+ \ we need to verify that the remembered cipher suite is still
+ \ matched by this ClientHello.
+ check-resume { resume }
+
+ \ Cipher suites. We read all cipher suites from client, each time
+ \ matching against our own list. We accumulate suites in the
+ \ client_suites[] context buffer: we keep suites that are
+ \ supported by both the client and the server (so the list size
+ \ cannot exceed that of the server list), and we keep them in
+ \ either client or server preference order (depending on the
+ \ relevant flag).
+ \
+ \ We also need to identify the pseudo cipher suite for secure
+ \ renegotiation here.
+ read16 open-elt
+ 0 { reneg-scsv }
+ 0 { resume-suite }
+ addr-len-client_suites dup2 bzero
+ over + { css-off css-max }
+ begin
+ dup while
+ read16 dup { suite }
+
+ \ Check that when resuming a session, the requested
+ \ suite is still valid.
+ resume if
+ dup addr-cipher_suite get16 = if
+ -1 >resume-suite
+ then
+ then
+
+ \ Special handling for TLS_EMPTY_RENEGOTIATION_INFO_SCSV.
+ \ This fake cipher suite may occur only in the first
+ \ handshake.
+ dup 0x00FF = if
+ addr-reneg get8 if ERR_BAD_SECRENEG fail then
+ -1 >reneg-scsv
+ then
+
+ \ Special handling for TLS_FALLBACK_SCSV. If the client
+ \ maximum version is less than our own maximum version,
+ \ then this is an undue downgrade. We mark it by setting
+ \ the client max version to 0x10000.
+ dup 0x5600 = if
+ client-version-max addr-version_min get16 >=
+ client-version-max addr-version_max get16 < and if
+ -1 >client-version-max
+ then
+ then
+
+ \ Test whether the suite is supported by the server.
+ scan-suite dup 0< if
+ \ We do not support this cipher suite. Note
+ \ that this also covers the case of pseudo
+ \ cipher suites.
+ drop
+ else
+ \ If we use server order, then we place the
+ \ suite at the computed offset; otherwise, we
+ \ append it to the list at the current place.
+ 0 flag? if
+ 2 << addr-client_suites + suite swap set16
+ else
+ drop
+ \ We need to test for list length because
+ \ the client list may have duplicates,
+ \ that we do not filter. Duplicates are
+ \ invalid so this is not a problem if we
+ \ reject such clients.
+ css-off css-max >= if
+ ERR_BAD_HANDSHAKE fail
+ then
+ suite css-off set16
+ css-off 4 + >css-off
+ then
+ then
+ repeat
+ drop
+
+ \ Compression methods. We need method 0 (no compression).
+ 0 { ok-compression }
+ read8 open-elt
+ begin dup while
+ read8 ifnot -1 >ok-compression then
+ repeat
+ close-elt
+
+ \ Set default values for parameters that may be affected by
+ \ extensions:
+ \ -- server name is empty
+ \ -- client is reputed to know RSA and ECDSA, both with SHA-1
+ \ -- the default elliptic curve is P-256 (secp256r1, id = 23)
+ 0 addr-server_name set8
+ 0x0404 addr-hashes set32
+ 0x800000 addr-curves set32
+
+ \ Process extensions, if any.
+ dup if
+ read16 open-elt
+ begin dup while
+ read16 case
+ \ Server Name Indication.
+ 0x0000 of
+ read-client-sni
+ endof
+ \ Max Frag Length.
+ 0x0001 of
+ read-client-frag
+ endof
+ \ Secure Renegotiation.
+ 0xFF01 of
+ read-client-reneg
+ endof
+ \ Signature Algorithms.
+ 0x000D of
+ read-signatures
+ endof
+ \ Supported Curves.
+ 0x000A of
+ read-supported-curves
+ endof
+ \ Supported Point Formats.
+ \ We only support "uncompressed", that all
+ \ implementations are supposed to support,
+ \ so we can simply ignore that extension.
+ \ 0x000B of
+ \ read-ignore-16
+ \ endof
+
+ \ ALPN
+ 0x0010 of
+ read-ALPN-from-client
+ endof
+
+ \ Other extensions are ignored.
+ drop read-ignore-16 0
+ endcase
+ repeat
+ close-elt
+ then
+
+ \ Close message.
+ close-elt
+
+ \ Cancel session resumption if the cipher suite was not found.
+ resume resume-suite and >resume
+
+ \ Now check the received data. Since the client is expecting an
+ \ answer, we can send an appropriate fatal alert on any error.
+
+ \ Compute protocol version as the minimum of our maximum version,
+ \ and the maximum version sent by the client. If that is less than
+ \ 0x0300 (SSL-3.0), then fail. Otherwise, we may at least send an
+ \ alert with that version. We still reject versions lower than our
+ \ configured minimum.
+ \ As a special case, in case of undue downgrade, we send a specific
+ \ alert (see RFC 7507). Note that this case may happen only if
+ \ we would otherwise accept the client's version.
+ client-version-max 0< if
+ addr-client_max_version get16 addr-version_out set16
+ 86 fail-alert
+ then
+ addr-version_max get16
+ dup client-version-max > if drop client-version-max then
+ dup 0x0300 < if ERR_BAD_VERSION fail then
+ client-version-max addr-version_min get16 < if
+ 70 fail-alert
+ then
+ \ If resuming the session, then enforce the previously negotiated
+ \ version (if still possible).
+ resume if
+ addr-version get16 client-version-max <= if
+ drop addr-version get16
+ else
+ 0 >resume
+ then
+ then
+ dup addr-version set16
+ dup addr-version_in set16
+ dup addr-version_out set16
+ 0x0303 >= { can-tls12 }
+
+ \ If the client sent TLS_EMPTY_RENEGOTIATION_INFO_SCSV, then
+ \ we should mark the client as "supporting secure renegotiation".
+ reneg-scsv if 2 addr-reneg set8 then
+
+ \ If, at that point, the 'reneg' value is still 0, then the client
+ \ did not send the extension or the SCSV, so we have to assume
+ \ that secure renegotiation is not supported by that client.
+ addr-reneg get8 ifnot 1 addr-reneg set8 then
+
+ \ Check compression.
+ ok-compression ifnot 40 fail-alert then
+
+ \ Filter hash function support by what the server also supports.
+ \ If no common hash function remains with RSA and/or ECDSA, then
+ \ the corresponding ECDHE suites are not possible.
+ supported-hash-functions drop 257 * 0xFFFF0000 or
+ addr-hashes get32 and dup addr-hashes set32
+ \ In 'can-ecdhe', bit 12 is set if ECDHE_RSA is possible, bit 13 is
+ \ set if ECDHE_ECDSA is possible.
+ dup 0xFF and 0<> neg
+ swap 8 >> 0<> 2 and or 12 << { can-ecdhe }
+
+ \ Filter supported curves. If there is no common curve between
+ \ client and us, then ECDHE suites cannot be used. Note that we
+ \ may still allow ECDH, depending on the EC key handler.
+ addr-curves get32 supported-curves and dup addr-curves set32
+ ifnot 0 >can-ecdhe then
+
+ \ If resuming a session, then the next steps are not necessary;
+ \ we won't invoke the policy handler.
+ resume if -1 ret then
+
+ \ We are not resuming, so a new session ID should be generated.
+ \ We don't check that the new ID is distinct from the one sent
+ \ by the client because probability of such an event is 2^(-256),
+ \ i.e. much (much) lower than that of an undetected transmission
+ \ error or hardware miscomputation, and with similar consequences
+ \ (handshake simply fails).
+ addr-session_id 32 mkrand
+ 32 addr-session_id_len set8
+
+ \ Translate common cipher suites, then squeeze out holes: there
+ \ may be holes because of the way we fill the list when the
+ \ server preference order is enforced, and also in case some
+ \ suites are filtered out. In particular:
+ \ -- ECDHE suites are removed if there is no common hash function
+ \ (for the relevant signature algorithm) or no common curve.
+ \ -- TLS-1.2-only suites are removed if the negotiated version is
+ \ TLS-1.1 or lower.
+ addr-client_suites dup >css-off
+ begin dup css-max < while
+ dup get16 dup cipher-suite-to-elements
+ dup 12 >> dup 1 = swap 2 = or if
+ dup can-ecdhe and ifnot
+ 2drop 0 dup
+ then
+ then
+ can-tls12 ifnot
+ \ Suites compatible with TLS-1.0 and TLS-1.1 are
+ \ exactly the ones that use HMAC/SHA-1.
+ dup 0xF0 and 0x20 <> if
+ 2drop 0 dup
+ then
+ then
+ dup if
+ css-off 2+ set16 css-off set16
+ css-off 4 + >css-off
+ else
+ 2drop
+ then
+ 4 +
+ repeat
+ drop
+ css-off addr-client_suites - 2 >>
+ dup ifnot
+ \ No common cipher suite: handshake failure.
+ 40 fail-alert
+ then
+ addr-client_suites_num set8
+
+ \ Check ALPN.
+ addr-selected_protocol get16 0xFFFF = if
+ 3 flag? if 120 fail-alert then
+ 0 addr-selected_protocol set16
+ then
+
+ \ Call policy handler to obtain the cipher suite and other
+ \ parameters.
+ call-policy-handler ifnot 40 fail-alert then
+
+ \ We are not resuming a session.
+ 0 ;
+
+\ Write ServerHello.
+: write-ServerHello ( initial -- )
+ { initial }
+ \ Compute ServerHello length.
+ 2 write8 70
+
+ \ Compute length of Secure Renegotiation extension.
+ addr-reneg get8 2 = if
+ initial if 5 else 29 then
+ else
+ 0
+ then
+ { ext-reneg-len }
+
+ \ Compute length of Max Fragment Length extension.
+ addr-peer_log_max_frag_len get8 if 5 else 0 then
+ { ext-max-frag-len }
+
+ \ Compute length of ALPN extension. This also copy the
+ \ selected protocol name into the pad.
+ addr-selected_protocol get16 dup if 1- copy-protocol-name 7 + then
+ { ext-ALPN-len }
+
+ \ Adjust ServerHello length to account for the extensions.
+ ext-reneg-len ext-max-frag-len + ext-ALPN-len + dup if 2 + then +
+ write24
+
+ \ Protocol version
+ addr-version get16 write16
+
+ \ Server random
+ addr-server_random 4 bzero
+ addr-server_random 4 + 28 mkrand
+ addr-server_random 32 write-blob
+
+ \ Session ID
+ \ TODO: if we have no session cache at all, we might send here
+ \ an empty session ID. This would save a bit of network
+ \ bandwidth.
+ 32 write8
+ addr-session_id 32 write-blob
+
+ \ Cipher suite
+ addr-cipher_suite get16 write16
+
+ \ Compression method
+ 0 write8
+
+ \ Extensions
+ ext-reneg-len ext-max-frag-len + ext-ALPN-len + dup if
+ write16
+ ext-reneg-len dup if
+ 0xFF01 write16
+ 4 - dup write16
+ 1- addr-saved_finished swap write-blob-head8
+ else
+ drop
+ then
+ ext-max-frag-len if
+ 0x0001 write16
+ 1 write16 addr-peer_log_max_frag_len get8 8 - write8
+ then
+ ext-ALPN-len dup if
+ \ Note: the selected protocol name was previously
+ \ copied into the pad.
+ 0x0010 write16
+ 4 - dup write16
+ 2- dup write16
+ 1- addr-pad swap write-blob-head8
+ else
+ drop
+ then
+ else
+ drop
+ then ;
+
+\ Do the first part of ECDHE. Returned value is the computed signature
+\ length, or a negative error code on error.
+cc: do-ecdhe-part1 ( curve -- len ) {
+ int curve = T0_POPi();
+ T0_PUSHi(do_ecdhe_part1(CTX, curve));
+}
+
+\ Get index of first bit set to 1 (in low to high order).
+: lowest-1 ( bits -- n )
+ dup ifnot drop -1 ret then
+ 0 begin dup2 >> 1 and 0= while 1+ repeat
+ swap drop ;
+
+\ Write the Server Key Exchange message (if applicable).
+: write-ServerKeyExchange ( -- )
+ addr-cipher_suite get16 use-ecdhe? ifnot ret then
+
+ \ We must select an appropriate curve among the curves that
+ \ are supported both by us and the peer. Right now, we apply
+ \ a fixed preference order: Curve25519, P-256, P-384, P-521,
+ \ then the common curve with the lowest ID.
+ \ (TODO: add some option to make that behaviour configurable.)
+ \
+ \ This loop always terminates because previous processing made
+ \ sure that ECDHE suites are not selectable if there is no common
+ \ curve.
+ addr-curves get32
+ dup 0x20000000 and if
+ drop 29
+ else
+ dup 0x38000000 and dup if swap then
+ drop lowest-1
+ then
+ { curve-id }
+
+ \ Compute the signed curve point to send.
+ curve-id do-ecdhe-part1 dup 0< if neg fail then { sig-len }
+
+ \ If using TLS-1.2+, then the hash function and signature
+ \ algorithm are explicitly encoded in the message.
+ addr-version get16 0x0303 >= { tls1.2+ }
+
+ 12 write8
+ sig-len addr-ecdhe_point_len get8 + tls1.2+ 2 and + 6 + write24
+
+ \ Curve parameters: named curve with 16-bit ID.
+ 3 write8 curve-id write16
+
+ \ Public point.
+ addr-ecdhe_point addr-ecdhe_point_len get8 write-blob-head8
+
+ \ If TLS-1.2+, write hash and signature identifiers.
+ tls1.2+ if
+ \ sign_hash_id contains either a hash identifier,
+ \ or the complete 16-bit value to write.
+ addr-sign_hash_id get16
+ dup 0xFF00 < if
+ write16
+ else
+ 0xFF and write8
+ \ 'use-rsa-ecdhe?' returns -1 for RSA, 0 for
+ \ ECDSA. The byte on the wire shall be 1 for RSA,
+ \ 3 for ECDSA.
+ addr-cipher_suite get16 use-rsa-ecdhe? 1 << 3 + write8
+ then
+ then
+
+ \ Signature.
+ sig-len write16
+ addr-pad sig-len write-blob ;
+
+\ Get length of the list of anchor names to send to the client. The length
+\ includes the per-name 2-byte header, but _not_ the 2-byte header for
+\ the list itself. If no client certificate is requested, then this
+\ returns 0.
+cc: ta-names-total-length ( -- len ) {
+ size_t u, len;
+
+ len = 0;
+ if (CTX->ta_names != NULL) {
+ for (u = 0; u < CTX->num_tas; u ++) {
+ len += CTX->ta_names[u].len + 2;
+ }
+ } else if (CTX->tas != NULL) {
+ for (u = 0; u < CTX->num_tas; u ++) {
+ len += CTX->tas[u].dn.len + 2;
+ }
+ }
+ T0_PUSH(len);
+}
+
+\ Compute length and optionally write the contents of the list of
+\ supported client authentication methods.
+: write-list-auth ( do_write -- len )
+ 0
+ addr-cipher_suite get16 use-ecdh? if
+ 2+ over if 65 write8 66 write8 then
+ then
+ supports-rsa-sign? if 1+ over if 1 write8 then then
+ supports-ecdsa? if 1+ over if 64 write8 then then
+ swap drop ;
+
+: write-signhash-inner2 ( dow algo hashes len id -- dow algo hashes len )
+ { id }
+ over 1 id << and ifnot ret then
+ 2+
+ 3 pick if id write8 2 pick write8 then ;
+
+: write-signhash-inner1 ( dow algo hashes -- dow len )
+ 0
+ 4 write-signhash-inner2
+ 5 write-signhash-inner2
+ 6 write-signhash-inner2
+ 3 write-signhash-inner2
+ 2 write-signhash-inner2
+ -rot 2drop ;
+
+\ Compute length and optionally write the contents of the list of
+\ supported sign+hash algorithms.
+: write-list-signhash ( do_write -- len )
+ 0 { len }
+ \ If supporting neither RSA nor ECDSA in the engine, then we
+ \ will do only static ECDH, and thus we claim support for
+ \ everything (for the X.509 validator).
+ supports-rsa-sign? supports-ecdsa? or ifnot
+ 1 0x7C write-signhash-inner1 >len
+ 3 0x7C write-signhash-inner1 len +
+ swap drop ret
+ then
+ supports-rsa-sign? if
+ 1 supported-hash-functions drop
+ write-signhash-inner1 >len
+ then
+ supports-ecdsa? if
+ 3 supported-hash-functions drop
+ write-signhash-inner1 len + >len
+ then
+ drop len ;
+
+\ Initialise index for sending the list of anchor DN.
+cc: begin-ta-name-list ( -- ) {
+ CTX->cur_dn_index = 0;
+}
+
+\ Switch to next DN in the list. Returned value is the DN length, or -1
+\ if the end of the list was reached.
+cc: begin-ta-name ( -- len ) {
+ const br_x500_name *dn;
+ if (CTX->cur_dn_index >= CTX->num_tas) {
+ T0_PUSHi(-1);
+ } else {
+ if (CTX->ta_names == NULL) {
+ dn = &CTX->tas[CTX->cur_dn_index].dn;
+ } else {
+ dn = &CTX->ta_names[CTX->cur_dn_index];
+ }
+ CTX->cur_dn_index ++;
+ CTX->cur_dn = dn->data;
+ CTX->cur_dn_len = dn->len;
+ T0_PUSH(CTX->cur_dn_len);
+ }
+}
+
+\ Copy a chunk of the current DN into the pad. Returned value is the
+\ chunk length; this is 0 when the end of the current DN is reached.
+cc: copy-dn-chunk ( -- len ) {
+ size_t clen;
+
+ clen = CTX->cur_dn_len;
+ if (clen > sizeof ENG->pad) {
+ clen = sizeof ENG->pad;
+ }
+ memcpy(ENG->pad, CTX->cur_dn, clen);
+ CTX->cur_dn += clen;
+ CTX->cur_dn_len -= clen;
+ T0_PUSH(clen);
+}
+
+\ Write a CertificateRequest message.
+: write-CertificateRequest ( -- )
+ \ The list of client authentication types includes:
+ \ rsa_sign (1)
+ \ ecdsa_sign (64)
+ \ rsa_fixed_ecdh (65)
+ \ ecdsa_fixed_ecdh (66)
+ \ rsa_sign and ecdsa_sign require, respectively, RSA and ECDSA
+ \ support. Static ECDH requires that the cipher suite is ECDH.
+ \ When we ask for static ECDH, we always send both rsa_fixed_ecdh
+ \ and ecdsa_fixed_ecdh because what matters there is what the
+ \ X.509 engine may support, and we do not control that.
+ \
+ \ With TLS 1.2, we must also send a list of supported signature
+ \ and hash algorithms. That list is supposed to qualify both
+ \ the engine itself, and the X.509 validator, which are separate
+ \ in BearSSL. There again, we use the engine capabilities in that
+ \ list, and resort to a generic all-support list if only
+ \ static ECDH is accepted.
+ \
+ \ (In practice, client implementations tend to have at most one
+ \ or two certificates, and send the chain regardless of what
+ \ algorithms are used in it.)
+
+ 0 write-list-auth
+ addr-version get16 0x0303 >= if
+ 2+ 0 write-list-signhash +
+ then
+ ta-names-total-length + 3 +
+
+ \ Message header
+ 13 write8 write24
+
+ \ List of authentication methods
+ 0 write-list-auth write8 1 write-list-auth drop
+
+ \ For TLS 1.2+, list of sign+hash
+ addr-version get16 0x0303 >= if
+ 0 write-list-signhash write16 1 write-list-signhash drop
+ then
+
+ \ Trust anchor names
+ ta-names-total-length write16
+ begin-ta-name-list
+ begin
+ begin-ta-name
+ dup 0< if drop ret then write16
+ begin copy-dn-chunk dup while
+ addr-pad swap write-blob
+ repeat
+ drop
+ again ;
+
+\ Write the Server Hello Done message.
+: write-ServerHelloDone ( -- )
+ 14 write8 0 write24 ;
+
+\ Perform RSA decryption of the client-sent pre-master secret. The value
+\ is in the pad, and its length is provided as parameter.
+cc: do-rsa-decrypt ( len prf_id -- ) {
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_rsa_decrypt(CTX, prf_id, ENG->pad, len);
+}
+
+\ Perform ECDH (not ECDHE). The point from the client is in the pad, and
+\ its length is provided as parameter.
+cc: do-ecdh ( len prf_id -- ) {
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_ecdh(CTX, prf_id, ENG->pad, len);
+}
+
+\ Do the second part of ECDHE.
+cc: do-ecdhe-part2 ( len prf_id -- ) {
+ int prf_id = T0_POPi();
+ size_t len = T0_POP();
+ do_ecdhe_part2(CTX, prf_id, ENG->pad, len);
+}
+
+\ Perform static ECDH. The point from the client is the public key
+\ extracted from its certificate.
+cc: do-static-ecdh ( prf_id -- ) {
+ do_static_ecdh(CTX, T0_POP());
+}
+
+\ Read a ClientKeyExchange header.
+: read-ClientKeyExchange-header ( -- len )
+ read-handshake-header 16 = ifnot ERR_UNEXPECTED fail then ;
+
+\ Read the Client Key Exchange contents (non-empty case).
+: read-ClientKeyExchange-contents ( lim -- )
+ \ What we should get depends on the cipher suite.
+ addr-cipher_suite get16 use-rsa-keyx? if
+ \ RSA key exchange: we expect a RSA-encrypted value.
+ read16
+ dup 512 > if ERR_LIMIT_EXCEEDED fail then
+ dup { enc-rsa-len }
+ addr-pad swap read-blob
+ enc-rsa-len addr-cipher_suite get16 prf-id do-rsa-decrypt
+ then
+ addr-cipher_suite get16 dup use-ecdhe? swap use-ecdh? { ecdhe ecdh }
+ ecdh ecdhe or if
+ \ ECDH or ECDHE key exchange: we expect an EC point.
+ read8 dup { ec-point-len }
+ addr-pad swap read-blob
+ ec-point-len addr-cipher_suite get16 prf-id
+ ecdhe if do-ecdhe-part2 else do-ecdh then
+ then
+ close-elt ;
+
+\ Read the Client Key Exchange (normal case).
+: read-ClientKeyExchange ( -- )
+ read-ClientKeyExchange-header
+ read-ClientKeyExchange-contents ;
+
+\ Obtain all possible hash values for handshake messages so far. This
+\ is done because we need the hash value for the CertificateVerify
+\ _before_ knowing which hash function will actually be used, as this
+\ information is obtained from decoding the message header itself.
+\ All hash values are stored in the pad (208 bytes in total).
+cc: compute-hash-CV ( -- ) {
+ int i;
+
+ for (i = 1; i <= 6; i ++) {
+ br_multihash_out(&ENG->mhash, i,
+ ENG->pad + HASH_PAD_OFF[i - 1]);
+ }
+}
+
+\ Copy the proper hash value from the pad into the dedicated buffer.
+\ Returned value is true (-1) on success, false (0) on error (error
+\ being an unimplemented hash function). The id has already been verified
+\ to be either 0 (for MD5+SHA-1) or one of the SHA-* functions.
+cc: copy-hash-CV ( hash_id -- bool ) {
+ int id = T0_POP();
+ size_t off, len;
+
+ if (id == 0) {
+ off = 0;
+ len = 36;
+ } else {
+ if (br_multihash_getimpl(&ENG->mhash, id) == 0) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ off = HASH_PAD_OFF[id - 1];
+ len = HASH_PAD_OFF[id] - off;
+ }
+ memcpy(CTX->hash_CV, ENG->pad + off, len);
+ CTX->hash_CV_len = len;
+ CTX->hash_CV_id = id;
+ T0_PUSHi(-1);
+}
+
+\ Verify signature in CertificateVerify. Output is 0 on success, or a
+\ non-zero error code.
+cc: verify-CV-sig ( sig-len -- err ) {
+ int err;
+
+ err = verify_CV_sig(CTX, T0_POP());
+ T0_PUSHi(err);
+}
+
+\ Process static ECDH.
+: process-static-ECDH ( ktu -- )
+ \ Static ECDH is allowed only if the cipher suite uses ECDH, and
+ \ the client's public key has type EC and allows key exchange.
+ \ BR_KEYTYPE_KEYX is 0x10, and BR_KEYTYPE_EC is 2.
+ 0x1F and 0x12 = ifnot ERR_WRONG_KEY_USAGE fail then
+ addr-cipher_suite get16
+ dup use-ecdh? ifnot ERR_UNEXPECTED fail then
+ prf-id
+ do-static-ecdh ;
+
+\ Read CertificateVerify header.
+: read-CertificateVerify-header ( -- lim )
+ compute-hash-CV
+ read-handshake-header 15 = ifnot ERR_UNEXPECTED fail then ;
+
+\ Read CertificateVerify. The client key type + usage is expected on the
+\ stack.
+: read-CertificateVerify ( ktu -- )
+ \ Check that the key allows for signatures.
+ dup 0x20 and ifnot ERR_WRONG_KEY_USAGE fail then
+ 0x0F and { key-type }
+
+ \ Get header.
+ read-CertificateVerify-header
+
+ \ With TLS 1.2+, there is an explicit hash + signature indication,
+ \ which must be compatible with the key type.
+ addr-version get16 0x0303 >= if
+ \ Get hash function, then signature algorithm. The
+ \ signature algorithm is 1 (RSA) or 3 (ECDSA) while our
+ \ symbolic constants for key types are 1 (RSA) or 2 (EC).
+ read16
+ dup 0xFF and 1+ 1 >> key-type = ifnot
+ ERR_BAD_SIGNATURE fail
+ then
+ 8 >>
+
+ \ We support only SHA-1, SHA-224, SHA-256, SHA-384
+ \ and SHA-512. We explicitly reject MD5.
+ dup 2 < over 6 > or if ERR_INVALID_ALGORITHM fail then
+ else
+ \ With TLS 1.0 and 1.1, hash is MD5+SHA-1 (0) for RSA,
+ \ SHA-1 (2) for ECDSA.
+ key-type 0x01 = if 0 else 2 then
+ then
+ copy-hash-CV ifnot ERR_INVALID_ALGORITHM fail then
+
+ \ Read signature.
+ read16 dup { sig-len }
+ dup 512 > if ERR_LIMIT_EXCEEDED fail then
+ addr-pad swap read-blob
+ sig-len verify-CV-sig
+ dup if fail then drop
+
+ close-elt ;
+
+\ Send a HelloRequest.
+: send-HelloRequest ( -- )
+ flush-record
+ begin can-output? not while wait-co drop repeat
+ 22 addr-record_type_out set8
+ 0 write8 0 write24 flush-record
+ 23 addr-record_type_out set8 ;
+
+\ Make a handshake.
+: do-handshake ( initial -- )
+ 0 addr-application_data set8
+ 22 addr-record_type_out set8
+ 0 addr-selected_protocol set16
+ multihash-init
+ read-ClientHello
+ more-incoming-bytes? if ERR_UNEXPECTED fail then
+ if
+ \ Session resumption
+ write-ServerHello
+ 0 write-CCS-Finished
+ 0 read-CCS-Finished
+ else
+ \ Not a session resumption
+ write-ServerHello
+ write-Certificate drop
+ write-ServerKeyExchange
+ ta-names-total-length if
+ write-CertificateRequest
+ then
+ write-ServerHelloDone
+ flush-record
+
+ \ If we sent a CertificateRequest then we expect a
+ \ Certificate message.
+ ta-names-total-length if
+ \ Read client certificate.
+ 0 read-Certificate
+
+ choice
+ dup 0< uf
+ \ Client certificate validation failed.
+ 2 flag? ifnot neg fail then
+ drop
+ read-ClientKeyExchange
+ read-CertificateVerify-header
+ dup skip-blob drop
+ enduf
+ dup 0= uf
+ \ Client sent no certificate at all.
+ drop
+ 2 flag? ifnot
+ ERR_NO_CLIENT_AUTH fail
+ then
+ read-ClientKeyExchange
+ enduf
+
+ \ Client certificate was validated.
+ read-ClientKeyExchange-header
+ dup ifnot
+ \ Empty ClientKeyExchange.
+ drop
+ process-static-ECDH
+ else
+ read-ClientKeyExchange-contents
+ read-CertificateVerify
+ then
+ endchoice
+ else
+ \ No client certificate request, we just expect
+ \ a non-empty ClientKeyExchange.
+ read-ClientKeyExchange
+ then
+ 0 read-CCS-Finished
+ 0 write-CCS-Finished
+ save-session
+ then
+ 1 addr-application_data set8
+ 23 addr-record_type_out set8 ;
+
+\ Entry point.
+: main ( -- ! )
+ \ Perform initial handshake.
+ -1 do-handshake
+
+ begin
+ \ Wait for further invocation. At that point, we should
+ \ get either an explicit call for renegotiation, or
+ \ an incoming ClientHello handshake message.
+ wait-co
+ dup 0x07 and case
+ 0x00 of
+ 0x10 and if
+ \ The best we can do is ask for a
+ \ renegotiation, then wait for it
+ \ to happen.
+ 0 addr-application_data set8
+ send-HelloRequest
+ then
+ endof
+ 0x01 of
+ \ Reject renegotiations if the peer does not
+ \ support secure renegotiation, or if the
+ \ "no renegotiation" flag is set.
+ drop
+ addr-reneg get8 1 = 1 flag? or if
+ skip-ClientHello
+ flush-record
+ begin can-output? not while
+ wait-co drop
+ repeat
+ 100 send-warning
+ \ Put back connection in "application
+ \ data" state: it's not dead yet.
+ 1 addr-application_data set8
+ 23 addr-record_type_out set8
+ else
+ 0 do-handshake
+ then
+ endof
+ ERR_UNEXPECTED fail
+ endcase
+ again
+ ;
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_io.c b/test/monniaux/BearSSL/src/ssl/ssl_io.c
new file mode 100644
index 00000000..19526159
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_io.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_sslio_init(br_sslio_context *ctx,
+ br_ssl_engine_context *engine,
+ int (*low_read)(void *read_context,
+ unsigned char *data, size_t len),
+ void *read_context,
+ int (*low_write)(void *write_context,
+ const unsigned char *data, size_t len),
+ void *write_context)
+{
+ ctx->engine = engine;
+ ctx->low_read = low_read;
+ ctx->read_context = read_context;
+ ctx->low_write = low_write;
+ ctx->write_context = write_context;
+}
+
+/*
+ * Run the engine, until the specified target state is achieved, or
+ * an error occurs. The target state is SENDAPP, RECVAPP, or the
+ * combination of both (the combination matches either). When a match is
+ * achieved, this function returns 0. On error, it returns -1.
+ */
+static int
+run_until(br_sslio_context *ctx, unsigned target)
+{
+ for (;;) {
+ unsigned state;
+
+ state = br_ssl_engine_current_state(ctx->engine);
+ if (state & BR_SSL_CLOSED) {
+ return -1;
+ }
+
+ /*
+ * If there is some record data to send, do it. This takes
+ * precedence over everything else.
+ */
+ if (state & BR_SSL_SENDREC) {
+ unsigned char *buf;
+ size_t len;
+ int wlen;
+
+ buf = br_ssl_engine_sendrec_buf(ctx->engine, &len);
+ wlen = ctx->low_write(ctx->write_context, buf, len);
+ if (wlen < 0) {
+ /*
+ * If we received a close_notify and we
+ * still send something, then we have our
+ * own response close_notify to send, and
+ * the peer is allowed by RFC 5246 not to
+ * wait for it.
+ */
+ if (!ctx->engine->shutdown_recv) {
+ br_ssl_engine_fail(
+ ctx->engine, BR_ERR_IO);
+ }
+ return -1;
+ }
+ if (wlen > 0) {
+ br_ssl_engine_sendrec_ack(ctx->engine, wlen);
+ }
+ continue;
+ }
+
+ /*
+ * If we reached our target, then we are finished.
+ */
+ if (state & target) {
+ return 0;
+ }
+
+ /*
+ * If some application data must be read, and we did not
+ * exit, then this means that we are trying to write data,
+ * and that's not possible until the application data is
+ * read. This may happen if using a shared in/out buffer,
+ * and the underlying protocol is not strictly half-duplex.
+ * This is unrecoverable here, so we report an error.
+ */
+ if (state & BR_SSL_RECVAPP) {
+ return -1;
+ }
+
+ /*
+ * If we reached that point, then either we are trying
+ * to read data and there is some, or the engine is stuck
+ * until a new record is obtained.
+ */
+ if (state & BR_SSL_RECVREC) {
+ unsigned char *buf;
+ size_t len;
+ int rlen;
+
+ buf = br_ssl_engine_recvrec_buf(ctx->engine, &len);
+ rlen = ctx->low_read(ctx->read_context, buf, len);
+ if (rlen < 0) {
+ br_ssl_engine_fail(ctx->engine, BR_ERR_IO);
+ return -1;
+ }
+ if (rlen > 0) {
+ br_ssl_engine_recvrec_ack(ctx->engine, rlen);
+ }
+ continue;
+ }
+
+ /*
+ * We can reach that point if the target RECVAPP, and
+ * the state contains SENDAPP only. This may happen with
+ * a shared in/out buffer. In that case, we must flush
+ * the buffered data to "make room" for a new incoming
+ * record.
+ */
+ br_ssl_engine_flush(ctx->engine, 0);
+ }
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_read(br_sslio_context *ctx, void *dst, size_t len)
+{
+ unsigned char *buf;
+ size_t alen;
+
+ if (len == 0) {
+ return 0;
+ }
+ if (run_until(ctx, BR_SSL_RECVAPP) < 0) {
+ return -1;
+ }
+ buf = br_ssl_engine_recvapp_buf(ctx->engine, &alen);
+ if (alen > len) {
+ alen = len;
+ }
+ memcpy(dst, buf, alen);
+ br_ssl_engine_recvapp_ack(ctx->engine, alen);
+ return (int)alen;
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_read_all(br_sslio_context *ctx, void *dst, size_t len)
+{
+ unsigned char *buf;
+
+ buf = dst;
+ while (len > 0) {
+ int rlen;
+
+ rlen = br_sslio_read(ctx, buf, len);
+ if (rlen < 0) {
+ return -1;
+ }
+ buf += rlen;
+ len -= (size_t)rlen;
+ }
+ return 0;
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_write(br_sslio_context *ctx, const void *src, size_t len)
+{
+ unsigned char *buf;
+ size_t alen;
+
+ if (len == 0) {
+ return 0;
+ }
+ if (run_until(ctx, BR_SSL_SENDAPP) < 0) {
+ return -1;
+ }
+ buf = br_ssl_engine_sendapp_buf(ctx->engine, &alen);
+ if (alen > len) {
+ alen = len;
+ }
+ memcpy(buf, src, alen);
+ br_ssl_engine_sendapp_ack(ctx->engine, alen);
+ return (int)alen;
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_write_all(br_sslio_context *ctx, const void *src, size_t len)
+{
+ const unsigned char *buf;
+
+ buf = src;
+ while (len > 0) {
+ int wlen;
+
+ wlen = br_sslio_write(ctx, buf, len);
+ if (wlen < 0) {
+ return -1;
+ }
+ buf += wlen;
+ len -= (size_t)wlen;
+ }
+ return 0;
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_flush(br_sslio_context *ctx)
+{
+ /*
+ * We trigger a flush. We know the data is gone when there is
+ * no longer any record data to send, and we can either read
+ * or write application data. The call to run_until() does the
+ * job because it ensures that any assembled record data is
+ * first sent down the wire before considering anything else.
+ */
+ br_ssl_engine_flush(ctx->engine, 0);
+ return run_until(ctx, BR_SSL_SENDAPP | BR_SSL_RECVAPP);
+}
+
+/* see bearssl_ssl.h */
+int
+br_sslio_close(br_sslio_context *ctx)
+{
+ br_ssl_engine_close(ctx->engine);
+ while (br_ssl_engine_current_state(ctx->engine) != BR_SSL_CLOSED) {
+ /*
+ * Discard any incoming application data.
+ */
+ size_t len;
+
+ run_until(ctx, BR_SSL_RECVAPP);
+ if (br_ssl_engine_recvapp_buf(ctx->engine, &len) != NULL) {
+ br_ssl_engine_recvapp_ack(ctx->engine, len);
+ }
+ }
+ return br_ssl_engine_last_error(ctx->engine) == BR_ERR_OK;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_keyexport.c b/test/monniaux/BearSSL/src/ssl/ssl_keyexport.c
new file mode 100644
index 00000000..58e6dc3c
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_keyexport.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Supported cipher suites that use SHA-384 for the PRF when selected
+ * for TLS 1.2. All other cipher suites are deemed to use SHA-256.
+ */
+static const uint16_t suites_sha384[] = {
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
+};
+
+/* see bearssl_ssl.h */
+int
+br_ssl_key_export(br_ssl_engine_context *cc,
+ void *dst, size_t len, const char *label,
+ const void *context, size_t context_len)
+{
+ br_tls_prf_seed_chunk chunks[4];
+ br_tls_prf_impl iprf;
+ size_t num_chunks, u;
+ unsigned char tmp[2];
+ int prf_id;
+
+ if (cc->application_data != 1) {
+ return 0;
+ }
+ chunks[0].data = cc->client_random;
+ chunks[0].len = sizeof cc->client_random;
+ chunks[1].data = cc->server_random;
+ chunks[1].len = sizeof cc->server_random;
+ if (context != NULL) {
+ br_enc16be(tmp, (unsigned)context_len);
+ chunks[2].data = tmp;
+ chunks[2].len = 2;
+ chunks[3].data = context;
+ chunks[3].len = context_len;
+ num_chunks = 4;
+ } else {
+ num_chunks = 2;
+ }
+ prf_id = BR_SSLPRF_SHA256;
+ for (u = 0; u < (sizeof suites_sha384) / sizeof(uint16_t); u ++) {
+ if (suites_sha384[u] == cc->session.cipher_suite) {
+ prf_id = BR_SSLPRF_SHA384;
+ }
+ }
+ iprf = br_ssl_engine_get_PRF(cc, prf_id);
+ iprf(dst, len,
+ cc->session.master_secret, sizeof cc->session.master_secret,
+ label, num_chunks, chunks);
+ return 1;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_lru.c b/test/monniaux/BearSSL/src/ssl/ssl_lru.c
new file mode 100644
index 00000000..4c71011f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_lru.c
@@ -0,0 +1,537 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Each entry consists in a fixed number of bytes. Entries are concatenated
+ * in the store block. "Addresses" are really offsets in the block,
+ * expressed over 32 bits (so the cache may have size at most 4 GB, which
+ * "ought to be enough for everyone"). The "null address" is 0xFFFFFFFF.
+ * Note that since the storage block alignment is in no way guaranteed, we
+ * perform only accesses that can handle unaligned data.
+ *
+ * Two concurrent data structures are maintained:
+ *
+ * -- Entries are organised in a doubly-linked list; saved entries are added
+ * at the head, and loaded entries are moved to the head. Eviction uses
+ * the list tail (this is the LRU algorithm).
+ *
+ * -- Entries are indexed with a binary tree: all left descendants of a
+ * node have a lower session ID (in lexicographic order), while all
+ * right descendants have a higher session ID. The tree is heuristically
+ * balanced.
+ *
+ * Entry format:
+ *
+ * session ID 32 bytes
+ * master secret 48 bytes
+ * protocol version 2 bytes (big endian)
+ * cipher suite 2 bytes (big endian)
+ * list prev 4 bytes (big endian)
+ * list next 4 bytes (big endian)
+ * tree left child 4 bytes (big endian)
+ * tree right child 4 bytes (big endian)
+ *
+ * If an entry has a protocol version set to 0, then it is "disabled":
+ * it was a session pushed to the cache at some point, but it has
+ * been explicitly removed.
+ *
+ * We need to keep the tree balanced because an attacker could make
+ * handshakes, selecting some specific sessions (by reusing them) to
+ * try to make us make an imbalanced tree that makes lookups expensive
+ * (a denial-of-service attack that would persist as long as the cache
+ * remains, i.e. even after the attacker made all his connections).
+ * To do that, we replace the session ID (or the start of the session ID)
+ * with a HMAC value computed over the replaced part; the hash function
+ * implementation and the key are obtained from the server context upon
+ * first save() call.
+ *
+ * Theoretically, an attacker could use the exact timing of the lookup
+ * to infer the current tree topology, and try to revive entries to make
+ * it as unbalanced as possible. However, since the session ID are
+ * chosen randomly by the server, and the attacker cannot see the
+ * indexing values and must thus rely on blind selection, it should be
+ * exponentially difficult for the attacker to maintain a large
+ * imbalance.
+ */
+#define SESSION_ID_LEN 32
+#define MASTER_SECRET_LEN 48
+
+#define SESSION_ID_OFF 0
+#define MASTER_SECRET_OFF 32
+#define VERSION_OFF 80
+#define CIPHER_SUITE_OFF 82
+#define LIST_PREV_OFF 84
+#define LIST_NEXT_OFF 88
+#define TREE_LEFT_OFF 92
+#define TREE_RIGHT_OFF 96
+
+#define LRU_ENTRY_LEN 100
+
+#define ADDR_NULL ((uint32_t)-1)
+
+#define GETSET(name, off) \
+static inline uint32_t get_ ## name(br_ssl_session_cache_lru *cc, uint32_t x) \
+{ \
+ return br_dec32be(cc->store + x + (off)); \
+} \
+static inline void set_ ## name(br_ssl_session_cache_lru *cc, \
+ uint32_t x, uint32_t val) \
+{ \
+ br_enc32be(cc->store + x + (off), val); \
+}
+
+GETSET(prev, LIST_PREV_OFF)
+GETSET(next, LIST_NEXT_OFF)
+GETSET(left, TREE_LEFT_OFF)
+GETSET(right, TREE_RIGHT_OFF)
+
+/*
+ * Transform the session ID by replacing the first N bytes with a HMAC
+ * value computed over these bytes, using the random key K (the HMAC
+ * value is truncated if needed). HMAC will use the same hash function
+ * as the DRBG in the SSL server context, so with SHA-256, SHA-384,
+ * or SHA-1, depending on what is available.
+ *
+ * The risk of collision is considered too small to be a concern; and
+ * the impact of a collision is low (the handshake won't succeed). This
+ * risk is much lower than any transmission error, which would lead to
+ * the same consequences.
+ *
+ * Source and destination arrays msut be disjoint.
+ */
+static void
+mask_id(br_ssl_session_cache_lru *cc,
+ const unsigned char *src, unsigned char *dst)
+{
+ br_hmac_key_context hkc;
+ br_hmac_context hc;
+
+ memcpy(dst, src, SESSION_ID_LEN);
+ br_hmac_key_init(&hkc, cc->hash, cc->index_key, sizeof cc->index_key);
+ br_hmac_init(&hc, &hkc, SESSION_ID_LEN);
+ br_hmac_update(&hc, src, SESSION_ID_LEN);
+ br_hmac_out(&hc, dst);
+}
+
+/*
+ * Find a node by ID. Returned value is the node address, or ADDR_NULL if
+ * the node is not found.
+ *
+ * If addr_link is not NULL, then '*addr_link' is set to the address of the
+ * last followed link. If the found node is the root, or if the tree is
+ * empty, then '*addr_link' is set to ADDR_NULL.
+ */
+static uint32_t
+find_node(br_ssl_session_cache_lru *cc, const unsigned char *id,
+ uint32_t *addr_link)
+{
+ uint32_t x, y;
+
+ x = cc->root;
+ y = ADDR_NULL;
+ while (x != ADDR_NULL) {
+ int r;
+
+ r = memcmp(id, cc->store + x + SESSION_ID_OFF, SESSION_ID_LEN);
+ if (r < 0) {
+ y = x + TREE_LEFT_OFF;
+ x = get_left(cc, x);
+ } else if (r == 0) {
+ if (addr_link != NULL) {
+ *addr_link = y;
+ }
+ return x;
+ } else {
+ y = x + TREE_RIGHT_OFF;
+ x = get_right(cc, x);
+ }
+ }
+ if (addr_link != NULL) {
+ *addr_link = y;
+ }
+ return ADDR_NULL;
+}
+
+/*
+ * For node x, find its replacement upon removal.
+ *
+ * -- If node x has no child, then this returns ADDR_NULL.
+ * -- Otherwise, if node x has a left child, then the replacement is the
+ * rightmost left-descendent.
+ * -- Otherwise, the replacement is the leftmost right-descendent.
+ *
+ * If a node is returned, then '*al' is set to the address of the field
+ * that points to that node. Otherwise (node x has no child), '*al' is
+ * set to ADDR_NULL.
+ *
+ * Note that the replacement node, when found, is always a descendent
+ * of node 'x', so it cannot be the tree root. Thus, '*al' can be set
+ * to ADDR_NULL only when no node is found and ADDR_NULL is returned.
+ */
+static uint32_t
+find_replacement_node(br_ssl_session_cache_lru *cc, uint32_t x, uint32_t *al)
+{
+ uint32_t y1, y2;
+
+ y1 = get_left(cc, x);
+ if (y1 != ADDR_NULL) {
+ y2 = x + TREE_LEFT_OFF;
+ for (;;) {
+ uint32_t z;
+
+ z = get_right(cc, y1);
+ if (z == ADDR_NULL) {
+ *al = y2;
+ return y1;
+ }
+ y2 = y1 + TREE_RIGHT_OFF;
+ y1 = z;
+ }
+ }
+ y1 = get_right(cc, x);
+ if (y1 != ADDR_NULL) {
+ y2 = x + TREE_RIGHT_OFF;
+ for (;;) {
+ uint32_t z;
+
+ z = get_left(cc, y1);
+ if (z == ADDR_NULL) {
+ *al = y2;
+ return y1;
+ }
+ y2 = y1 + TREE_LEFT_OFF;
+ y1 = z;
+ }
+ }
+ *al = ADDR_NULL;
+ return ADDR_NULL;
+}
+
+/*
+ * Set the link at address 'alx' to point to node 'x'. If 'alx' is
+ * ADDR_NULL, then this sets the tree root to 'x'.
+ */
+static inline void
+set_link(br_ssl_session_cache_lru *cc, uint32_t alx, uint32_t x)
+{
+ if (alx == ADDR_NULL) {
+ cc->root = x;
+ } else {
+ br_enc32be(cc->store + alx, x);
+ }
+}
+
+/*
+ * Remove node 'x' from the tree. This function shall not be called if
+ * node 'x' is not part of the tree.
+ */
+static void
+remove_node(br_ssl_session_cache_lru *cc, uint32_t x)
+{
+ uint32_t alx, y, aly;
+
+ /*
+ * Removal algorithm:
+ * ------------------
+ *
+ * - If we remove the root, then the tree becomes empty.
+ *
+ * - If the removed node has no child, then we can simply remove
+ * it, with nothing else to do.
+ *
+ * - Otherwise, the removed node must be replaced by either its
+ * rightmost left-descendent, or its leftmost right-descendent.
+ * The replacement node itself must be removed from its current
+ * place. By definition, that replacement node has either no
+ * child, or at most a single child that will replace it in the
+ * tree.
+ */
+
+ /*
+ * Find node back and its ancestor link. If the node was the
+ * root, then alx is set to ADDR_NULL.
+ */
+ find_node(cc, cc->store + x + SESSION_ID_OFF, &alx);
+
+ /*
+ * Find replacement node 'y', and 'aly' is set to the address of
+ * the link to that replacement node. If the removed node has no
+ * child, then both 'y' and 'aly' are set to ADDR_NULL.
+ */
+ y = find_replacement_node(cc, x, &aly);
+
+ if (y != ADDR_NULL) {
+ uint32_t z;
+
+ /*
+ * The unlinked replacement node may have one child (but
+ * not two) that takes its place.
+ */
+ z = get_left(cc, y);
+ if (z == ADDR_NULL) {
+ z = get_right(cc, y);
+ }
+ set_link(cc, aly, z);
+
+ /*
+ * Link the replacement node in its new place, overwriting
+ * the current link to the node 'x' (which removes 'x').
+ */
+ set_link(cc, alx, y);
+
+ /*
+ * The replacement node adopts the left and right children
+ * of the removed node. Note that this also works even if
+ * the replacement node was a direct descendent of the
+ * removed node, since we unlinked it previously.
+ */
+ set_left(cc, y, get_left(cc, x));
+ set_right(cc, y, get_right(cc, x));
+ } else {
+ /*
+ * No replacement, we simply unlink the node 'x'.
+ */
+ set_link(cc, alx, ADDR_NULL);
+ }
+}
+
+static void
+lru_save(const br_ssl_session_cache_class **ctx,
+ br_ssl_server_context *server_ctx,
+ const br_ssl_session_parameters *params)
+{
+ br_ssl_session_cache_lru *cc;
+ unsigned char id[SESSION_ID_LEN];
+ uint32_t x, alx;
+
+ cc = (br_ssl_session_cache_lru *)ctx;
+
+ /*
+ * If the buffer is too small, we don't record anything. This
+ * test avoids problems in subsequent code.
+ */
+ if (cc->store_len < LRU_ENTRY_LEN) {
+ return;
+ }
+
+ /*
+ * Upon the first save in a session cache instance, we obtain
+ * a random key for our indexing.
+ */
+ if (!cc->init_done) {
+ br_hmac_drbg_generate(&server_ctx->eng.rng,
+ cc->index_key, sizeof cc->index_key);
+ cc->hash = br_hmac_drbg_get_hash(&server_ctx->eng.rng);
+ cc->init_done = 1;
+ }
+ mask_id(cc, params->session_id, id);
+
+ /*
+ * Look for the node in the tree. If the same ID is already used,
+ * then reject it. This is a collision event, which should be
+ * exceedingly rare.
+ * Note: we do NOT record the emplacement here, because the
+ * removal of an entry may change the tree topology.
+ */
+ if (find_node(cc, id, NULL) != ADDR_NULL) {
+ return;
+ }
+
+ /*
+ * Find some room for the new parameters. If the cache is not
+ * full yet, add it to the end of the area and bump the pointer up.
+ * Otherwise, evict the list tail entry. Note that we already
+ * filtered out the case of a ridiculously small buffer that
+ * cannot hold any entry at all; thus, if there is no room for an
+ * extra entry, then the cache cannot be empty.
+ */
+ if (cc->store_ptr > (cc->store_len - LRU_ENTRY_LEN)) {
+ /*
+ * Evict tail. If the buffer has room for a single entry,
+ * then this may also be the head.
+ */
+ x = cc->tail;
+ cc->tail = get_prev(cc, x);
+ if (cc->tail == ADDR_NULL) {
+ cc->head = ADDR_NULL;
+ } else {
+ set_next(cc, cc->tail, ADDR_NULL);
+ }
+
+ /*
+ * Remove the node from the tree.
+ */
+ remove_node(cc, x);
+ } else {
+ /*
+ * Allocate room for new node.
+ */
+ x = cc->store_ptr;
+ cc->store_ptr += LRU_ENTRY_LEN;
+ }
+
+ /*
+ * Find the emplacement for the new node, and link it.
+ */
+ find_node(cc, id, &alx);
+ set_link(cc, alx, x);
+ set_left(cc, x, ADDR_NULL);
+ set_right(cc, x, ADDR_NULL);
+
+ /*
+ * New entry becomes new list head. It may also become the list
+ * tail if the cache was empty at that point.
+ */
+ if (cc->head == ADDR_NULL) {
+ cc->tail = x;
+ } else {
+ set_prev(cc, cc->head, x);
+ }
+ set_prev(cc, x, ADDR_NULL);
+ set_next(cc, x, cc->head);
+ cc->head = x;
+
+ /*
+ * Fill data in the entry.
+ */
+ memcpy(cc->store + x + SESSION_ID_OFF, id, SESSION_ID_LEN);
+ memcpy(cc->store + x + MASTER_SECRET_OFF,
+ params->master_secret, MASTER_SECRET_LEN);
+ br_enc16be(cc->store + x + VERSION_OFF, params->version);
+ br_enc16be(cc->store + x + CIPHER_SUITE_OFF, params->cipher_suite);
+}
+
+static int
+lru_load(const br_ssl_session_cache_class **ctx,
+ br_ssl_server_context *server_ctx,
+ br_ssl_session_parameters *params)
+{
+ br_ssl_session_cache_lru *cc;
+ unsigned char id[SESSION_ID_LEN];
+ uint32_t x;
+
+ (void)server_ctx;
+ cc = (br_ssl_session_cache_lru *)ctx;
+ if (!cc->init_done) {
+ return 0;
+ }
+ mask_id(cc, params->session_id, id);
+ x = find_node(cc, id, NULL);
+ if (x != ADDR_NULL) {
+ unsigned version;
+
+ version = br_dec16be(cc->store + x + VERSION_OFF);
+ if (version == 0) {
+ /*
+ * Entry is disabled, we pretend we did not find it.
+ * Notably, we don't move it to the front of the
+ * LRU list.
+ */
+ return 0;
+ }
+ params->version = version;
+ params->cipher_suite = br_dec16be(
+ cc->store + x + CIPHER_SUITE_OFF);
+ memcpy(params->master_secret,
+ cc->store + x + MASTER_SECRET_OFF,
+ MASTER_SECRET_LEN);
+ if (x != cc->head) {
+ /*
+ * Found node is not at list head, so move
+ * it to the head.
+ */
+ uint32_t p, n;
+
+ p = get_prev(cc, x);
+ n = get_next(cc, x);
+ set_next(cc, p, n);
+ if (n == ADDR_NULL) {
+ cc->tail = p;
+ } else {
+ set_prev(cc, n, p);
+ }
+ set_prev(cc, cc->head, x);
+ set_next(cc, x, cc->head);
+ set_prev(cc, x, ADDR_NULL);
+ cc->head = x;
+ }
+ return 1;
+ }
+ return 0;
+}
+
+static const br_ssl_session_cache_class lru_class = {
+ sizeof(br_ssl_session_cache_lru),
+ &lru_save,
+ &lru_load
+};
+
+/* see inner.h */
+void
+br_ssl_session_cache_lru_init(br_ssl_session_cache_lru *cc,
+ unsigned char *store, size_t store_len)
+{
+ cc->vtable = &lru_class;
+ cc->store = store;
+ cc->store_len = store_len;
+ cc->store_ptr = 0;
+ cc->init_done = 0;
+ cc->head = ADDR_NULL;
+ cc->tail = ADDR_NULL;
+ cc->root = ADDR_NULL;
+}
+
+/* see bearssl_ssl.h */
+void br_ssl_session_cache_lru_forget(
+ br_ssl_session_cache_lru *cc, const unsigned char *id)
+{
+ unsigned char mid[SESSION_ID_LEN];
+ uint32_t addr;
+
+ /*
+ * If the cache is not initialised yet, then it is empty, and
+ * there is nothing to forget.
+ */
+ if (!cc->init_done) {
+ return;
+ }
+
+ /*
+ * Look for the node in the tree. If found, the entry is marked
+ * as "disabled"; it will be reused in due course, as it ages
+ * through the list.
+ *
+ * We do not go through the complex moves of actually releasing
+ * the entry right away because explicitly forgetting sessions
+ * should be a rare event, meant mostly for testing purposes,
+ * so this is not worth the extra code size.
+ */
+ mask_id(cc, id, mid);
+ addr = find_node(cc, mid, NULL);
+ if (addr != ADDR_NULL) {
+ br_enc16be(cc->store + addr + VERSION_OFF, 0);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_rec_cbc.c b/test/monniaux/BearSSL/src/ssl/ssl_rec_cbc.c
new file mode 100644
index 00000000..c0806049
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_rec_cbc.c
@@ -0,0 +1,440 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static void
+in_cbc_init(br_sslrec_in_cbc_context *cc,
+ const br_block_cbcdec_class *bc_impl,
+ const void *bc_key, size_t bc_key_len,
+ const br_hash_class *dig_impl,
+ const void *mac_key, size_t mac_key_len, size_t mac_out_len,
+ const void *iv)
+{
+ cc->vtable = &br_sslrec_in_cbc_vtable;
+ cc->seq = 0;
+ bc_impl->init(&cc->bc.vtable, bc_key, bc_key_len);
+ br_hmac_key_init(&cc->mac, dig_impl, mac_key, mac_key_len);
+ cc->mac_len = mac_out_len;
+ if (iv == NULL) {
+ memset(cc->iv, 0, sizeof cc->iv);
+ cc->explicit_IV = 1;
+ } else {
+ memcpy(cc->iv, iv, bc_impl->block_size);
+ cc->explicit_IV = 0;
+ }
+}
+
+static int
+cbc_check_length(const br_sslrec_in_cbc_context *cc, size_t rlen)
+{
+ /*
+ * Plaintext size: at most 16384 bytes
+ * Padding: at most 256 bytes
+ * MAC: mac_len extra bytes
+ * TLS 1.1+: each record has an explicit IV
+ *
+ * Minimum length includes at least one byte of padding, and the
+ * MAC.
+ *
+ * Total length must be a multiple of the block size.
+ */
+ size_t blen;
+ size_t min_len, max_len;
+
+ blen = cc->bc.vtable->block_size;
+ min_len = (blen + cc->mac_len) & ~(blen - 1);
+ max_len = (16384 + 256 + cc->mac_len) & ~(blen - 1);
+ if (cc->explicit_IV) {
+ min_len += blen;
+ max_len += blen;
+ }
+ return min_len <= rlen && rlen <= max_len;
+}
+
+/*
+ * Rotate array buf[] of length 'len' to the left (towards low indices)
+ * by 'num' bytes if ctl is 1; otherwise, leave it unchanged. This is
+ * constant-time. 'num' MUST be lower than 'len'. 'len' MUST be lower
+ * than or equal to 64.
+ */
+static void
+cond_rotate(uint32_t ctl, unsigned char *buf, size_t len, size_t num)
+{
+ unsigned char tmp[64];
+ size_t u, v;
+
+ for (u = 0, v = num; u < len; u ++) {
+ tmp[u] = MUX(ctl, buf[v], buf[u]);
+ if (++ v == len) {
+ v = 0;
+ }
+ }
+ memcpy(buf, tmp, len);
+}
+
+static unsigned char *
+cbc_decrypt(br_sslrec_in_cbc_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ /*
+ * We represent all lengths on 32-bit integers, because:
+ * -- SSL record lengths always fit in 32 bits;
+ * -- our constant-time primitives operate on 32-bit integers.
+ */
+ unsigned char *buf;
+ uint32_t u, v, len, blen, min_len, max_len;
+ uint32_t good, pad_len, rot_count, len_withmac, len_nomac;
+ unsigned char tmp1[64], tmp2[64];
+ int i;
+ br_hmac_context hc;
+
+ buf = data;
+ len = *data_len;
+ blen = cc->bc.vtable->block_size;
+
+ /*
+ * Decrypt data, and skip the explicit IV (if applicable). Note
+ * that the total length is supposed to have been verified by
+ * the caller. If there is an explicit IV, then we actually
+ * "decrypt" it using the implicit IV (from previous record),
+ * which is useless but harmless.
+ */
+ cc->bc.vtable->run(&cc->bc.vtable, cc->iv, data, len);
+ if (cc->explicit_IV) {
+ buf += blen;
+ len -= blen;
+ }
+
+ /*
+ * Compute minimum and maximum length of plaintext + MAC. These
+ * lengths can be inferred from the outside: they are not secret.
+ */
+ min_len = (cc->mac_len + 256 < len) ? len - 256 : cc->mac_len;
+ max_len = len - 1;
+
+ /*
+ * Use the last decrypted byte to compute the actual payload
+ * length. Take care not to underflow (we use unsigned types).
+ */
+ pad_len = buf[max_len];
+ good = LE(pad_len, (uint32_t)(max_len - min_len));
+ len = MUX(good, (uint32_t)(max_len - pad_len), min_len);
+
+ /*
+ * Check padding contents: all padding bytes must be equal to
+ * the value of pad_len.
+ */
+ for (u = min_len; u < max_len; u ++) {
+ good &= LT(u, len) | EQ(buf[u], pad_len);
+ }
+
+ /*
+ * Extract the MAC value. This is done in one pass, but results
+ * in a "rotated" MAC value depending on where it actually
+ * occurs. The 'rot_count' value is set to the offset of the
+ * first MAC byte within tmp1[].
+ *
+ * min_len and max_len are also adjusted to the minimum and
+ * maximum lengths of the plaintext alone (without the MAC).
+ */
+ len_withmac = (uint32_t)len;
+ len_nomac = len_withmac - cc->mac_len;
+ min_len -= cc->mac_len;
+ rot_count = 0;
+ memset(tmp1, 0, cc->mac_len);
+ v = 0;
+ for (u = min_len; u < max_len; u ++) {
+ tmp1[v] |= MUX(GE(u, len_nomac) & LT(u, len_withmac),
+ buf[u], 0x00);
+ rot_count = MUX(EQ(u, len_nomac), v, rot_count);
+ if (++ v == cc->mac_len) {
+ v = 0;
+ }
+ }
+ max_len -= cc->mac_len;
+
+ /*
+ * Rotate back the MAC value. The loop below does the constant-time
+ * rotation in time n*log n for a MAC output of length n. We assume
+ * that the MAC output length is no more than 64 bytes, so the
+ * rotation count fits on 6 bits.
+ */
+ for (i = 5; i >= 0; i --) {
+ uint32_t rc;
+
+ rc = (uint32_t)1 << i;
+ cond_rotate(rot_count >> i, tmp1, cc->mac_len, rc);
+ rot_count &= ~rc;
+ }
+
+ /*
+ * Recompute the HMAC value. The input is the concatenation of
+ * the sequence number (8 bytes), the record header (5 bytes),
+ * and the payload.
+ *
+ * At that point, min_len is the minimum plaintext length, but
+ * max_len still includes the MAC length.
+ */
+ br_enc64be(tmp2, cc->seq ++);
+ tmp2[8] = (unsigned char)record_type;
+ br_enc16be(tmp2 + 9, version);
+ br_enc16be(tmp2 + 11, len_nomac);
+ br_hmac_init(&hc, &cc->mac, cc->mac_len);
+ br_hmac_update(&hc, tmp2, 13);
+ br_hmac_outCT(&hc, buf, len_nomac, min_len, max_len, tmp2);
+
+ /*
+ * Compare the extracted and recomputed MAC values.
+ */
+ for (u = 0; u < cc->mac_len; u ++) {
+ good &= EQ0(tmp1[u] ^ tmp2[u]);
+ }
+
+ /*
+ * Check that the plaintext length is valid. The previous
+ * check was on the encrypted length, but the padding may have
+ * turned shorter than expected.
+ *
+ * Once this final test is done, the critical "constant-time"
+ * section ends and we can make conditional jumps again.
+ */
+ good &= LE(len_nomac, 16384);
+
+ if (!good) {
+ return 0;
+ }
+ *data_len = len_nomac;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_in_cbc_class br_sslrec_in_cbc_vtable = {
+ {
+ sizeof(br_sslrec_in_cbc_context),
+ (int (*)(const br_sslrec_in_class *const *, size_t))
+ &cbc_check_length,
+ (unsigned char *(*)(const br_sslrec_in_class **,
+ int, unsigned, void *, size_t *))
+ &cbc_decrypt
+ },
+ (void (*)(const br_sslrec_in_cbc_class **,
+ const br_block_cbcdec_class *, const void *, size_t,
+ const br_hash_class *, const void *, size_t, size_t,
+ const void *))
+ &in_cbc_init
+};
+
+/*
+ * For CBC output:
+ *
+ * -- With TLS 1.1+, there is an explicit IV. Generation method uses
+ * HMAC, computed over the current sequence number, and the current MAC
+ * key. The resulting value is truncated to the size of a block, and
+ * added at the head of the plaintext; it will get encrypted along with
+ * the data. This custom generation mechanism is "safe" under the
+ * assumption that HMAC behaves like a random oracle; since the MAC for
+ * a record is computed over the concatenation of the sequence number,
+ * the record header and the plaintext, the HMAC-for-IV will not collide
+ * with the normal HMAC.
+ *
+ * -- With TLS 1.0, for application data, we want to enforce a 1/n-1
+ * split, as a countermeasure against chosen-plaintext attacks. We thus
+ * need to leave some room in the buffer for that extra record.
+ */
+
+static void
+out_cbc_init(br_sslrec_out_cbc_context *cc,
+ const br_block_cbcenc_class *bc_impl,
+ const void *bc_key, size_t bc_key_len,
+ const br_hash_class *dig_impl,
+ const void *mac_key, size_t mac_key_len, size_t mac_out_len,
+ const void *iv)
+{
+ cc->vtable = &br_sslrec_out_cbc_vtable;
+ cc->seq = 0;
+ bc_impl->init(&cc->bc.vtable, bc_key, bc_key_len);
+ br_hmac_key_init(&cc->mac, dig_impl, mac_key, mac_key_len);
+ cc->mac_len = mac_out_len;
+ if (iv == NULL) {
+ memset(cc->iv, 0, sizeof cc->iv);
+ cc->explicit_IV = 1;
+ } else {
+ memcpy(cc->iv, iv, bc_impl->block_size);
+ cc->explicit_IV = 0;
+ }
+}
+
+static void
+cbc_max_plaintext(const br_sslrec_out_cbc_context *cc,
+ size_t *start, size_t *end)
+{
+ size_t blen, len;
+
+ blen = cc->bc.vtable->block_size;
+ if (cc->explicit_IV) {
+ *start += blen;
+ } else {
+ *start += 4 + ((cc->mac_len + blen + 1) & ~(blen - 1));
+ }
+ len = (*end - *start) & ~(blen - 1);
+ len -= 1 + cc->mac_len;
+ if (len > 16384) {
+ len = 16384;
+ }
+ *end = *start + len;
+}
+
+static unsigned char *
+cbc_encrypt(br_sslrec_out_cbc_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf, *rbuf;
+ size_t len, blen, plen;
+ unsigned char tmp[13];
+ br_hmac_context hc;
+
+ buf = data;
+ len = *data_len;
+ blen = cc->bc.vtable->block_size;
+
+ /*
+ * If using TLS 1.0, with more than one byte of plaintext, and
+ * the record is application data, then we need to compute
+ * a "split". We do not perform the split on other record types
+ * because it turned out that some existing, deployed
+ * implementations of SSL/TLS do not tolerate the splitting of
+ * some message types (in particular the Finished message).
+ *
+ * If using TLS 1.1+, then there is an explicit IV. We produce
+ * that IV by adding an extra initial plaintext block, whose
+ * value is computed with HMAC over the record sequence number.
+ */
+ if (cc->explicit_IV) {
+ /*
+ * We use here the fact that all the HMAC variants we
+ * support can produce at least 16 bytes, while all the
+ * block ciphers we support have blocks of no more than
+ * 16 bytes. Thus, we can always truncate the HMAC output
+ * down to the block size.
+ */
+ br_enc64be(tmp, cc->seq);
+ br_hmac_init(&hc, &cc->mac, blen);
+ br_hmac_update(&hc, tmp, 8);
+ br_hmac_out(&hc, buf - blen);
+ rbuf = buf - blen - 5;
+ } else {
+ if (len > 1 && record_type == BR_SSL_APPLICATION_DATA) {
+ /*
+ * To do the split, we use a recursive invocation;
+ * since we only give one byte to the inner call,
+ * the recursion stops there.
+ *
+ * We need to compute the exact size of the extra
+ * record, so that the two resulting records end up
+ * being sequential in RAM.
+ *
+ * We use here the fact that cbc_max_plaintext()
+ * adjusted the start offset to leave room for the
+ * initial fragment.
+ */
+ size_t xlen;
+
+ rbuf = buf - 4
+ - ((cc->mac_len + blen + 1) & ~(blen - 1));
+ rbuf[0] = buf[0];
+ xlen = 1;
+ rbuf = cbc_encrypt(cc, record_type,
+ version, rbuf, &xlen);
+ buf ++;
+ len --;
+ } else {
+ rbuf = buf - 5;
+ }
+ }
+
+ /*
+ * Compute MAC.
+ */
+ br_enc64be(tmp, cc->seq ++);
+ tmp[8] = record_type;
+ br_enc16be(tmp + 9, version);
+ br_enc16be(tmp + 11, len);
+ br_hmac_init(&hc, &cc->mac, cc->mac_len);
+ br_hmac_update(&hc, tmp, 13);
+ br_hmac_update(&hc, buf, len);
+ br_hmac_out(&hc, buf + len);
+ len += cc->mac_len;
+
+ /*
+ * Add padding.
+ */
+ plen = blen - (len & (blen - 1));
+ memset(buf + len, (unsigned)plen - 1, plen);
+ len += plen;
+
+ /*
+ * If an explicit IV is used, the corresponding extra block was
+ * already put in place earlier; we just have to account for it
+ * here.
+ */
+ if (cc->explicit_IV) {
+ buf -= blen;
+ len += blen;
+ }
+
+ /*
+ * Encrypt the whole thing. If there is an explicit IV, we also
+ * encrypt it, which is fine (encryption of a uniformly random
+ * block is still a uniformly random block).
+ */
+ cc->bc.vtable->run(&cc->bc.vtable, cc->iv, buf, len);
+
+ /*
+ * Add the header and return.
+ */
+ buf[-5] = record_type;
+ br_enc16be(buf - 4, version);
+ br_enc16be(buf - 2, len);
+ *data_len = (size_t)((buf + len) - rbuf);
+ return rbuf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_out_cbc_class br_sslrec_out_cbc_vtable = {
+ {
+ sizeof(br_sslrec_out_cbc_context),
+ (void (*)(const br_sslrec_out_class *const *,
+ size_t *, size_t *))
+ &cbc_max_plaintext,
+ (unsigned char *(*)(const br_sslrec_out_class **,
+ int, unsigned, void *, size_t *))
+ &cbc_encrypt
+ },
+ (void (*)(const br_sslrec_out_cbc_class **,
+ const br_block_cbcenc_class *, const void *, size_t,
+ const br_hash_class *, const void *, size_t, size_t,
+ const void *))
+ &out_cbc_init
+};
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_rec_ccm.c b/test/monniaux/BearSSL/src/ssl/ssl_rec_ccm.c
new file mode 100644
index 00000000..92c32952
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_rec_ccm.c
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * CCM initialisation. This does everything except setting the vtable,
+ * which depends on whether this is a context for encrypting or for
+ * decrypting.
+ */
+static void
+gen_ccm_init(br_sslrec_ccm_context *cc,
+ const br_block_ctrcbc_class *bc_impl,
+ const void *key, size_t key_len,
+ const void *iv, size_t tag_len)
+{
+ cc->seq = 0;
+ bc_impl->init(&cc->bc.vtable, key, key_len);
+ memcpy(cc->iv, iv, sizeof cc->iv);
+ cc->tag_len = tag_len;
+}
+
+static void
+in_ccm_init(br_sslrec_ccm_context *cc,
+ const br_block_ctrcbc_class *bc_impl,
+ const void *key, size_t key_len,
+ const void *iv, size_t tag_len)
+{
+ cc->vtable.in = &br_sslrec_in_ccm_vtable;
+ gen_ccm_init(cc, bc_impl, key, key_len, iv, tag_len);
+}
+
+static int
+ccm_check_length(const br_sslrec_ccm_context *cc, size_t rlen)
+{
+ /*
+ * CCM overhead is 8 bytes for nonce_explicit, and the tag
+ * (normally 8 or 16 bytes, depending on cipher suite).
+ */
+ size_t over;
+
+ over = 8 + cc->tag_len;
+ return rlen >= over && rlen <= (16384 + over);
+}
+
+static unsigned char *
+ccm_decrypt(br_sslrec_ccm_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ br_ccm_context zc;
+ unsigned char *buf;
+ unsigned char nonce[12], header[13];
+ size_t len;
+
+ buf = (unsigned char *)data + 8;
+ len = *data_len - (8 + cc->tag_len);
+
+ /*
+ * Make nonce (implicit + explicit parts).
+ */
+ memcpy(nonce, cc->iv, sizeof cc->iv);
+ memcpy(nonce + 4, data, 8);
+
+ /*
+ * Assemble synthetic header for the AAD.
+ */
+ br_enc64be(header, cc->seq ++);
+ header[8] = (unsigned char)record_type;
+ br_enc16be(header + 9, version);
+ br_enc16be(header + 11, len);
+
+ /*
+ * Perform CCM decryption.
+ */
+ br_ccm_init(&zc, &cc->bc.vtable);
+ br_ccm_reset(&zc, nonce, sizeof nonce, sizeof header, len, cc->tag_len);
+ br_ccm_aad_inject(&zc, header, sizeof header);
+ br_ccm_flip(&zc);
+ br_ccm_run(&zc, 0, buf, len);
+ if (!br_ccm_check_tag(&zc, buf + len)) {
+ return NULL;
+ }
+ *data_len = len;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable = {
+ {
+ sizeof(br_sslrec_ccm_context),
+ (int (*)(const br_sslrec_in_class *const *, size_t))
+ &ccm_check_length,
+ (unsigned char *(*)(const br_sslrec_in_class **,
+ int, unsigned, void *, size_t *))
+ &ccm_decrypt
+ },
+ (void (*)(const br_sslrec_in_ccm_class **,
+ const br_block_ctrcbc_class *, const void *, size_t,
+ const void *, size_t))
+ &in_ccm_init
+};
+
+static void
+out_ccm_init(br_sslrec_ccm_context *cc,
+ const br_block_ctrcbc_class *bc_impl,
+ const void *key, size_t key_len,
+ const void *iv, size_t tag_len)
+{
+ cc->vtable.out = &br_sslrec_out_ccm_vtable;
+ gen_ccm_init(cc, bc_impl, key, key_len, iv, tag_len);
+}
+
+static void
+ccm_max_plaintext(const br_sslrec_ccm_context *cc,
+ size_t *start, size_t *end)
+{
+ size_t len;
+
+ *start += 8;
+ len = *end - *start - cc->tag_len;
+ if (len > 16384) {
+ len = 16384;
+ }
+ *end = *start + len;
+}
+
+static unsigned char *
+ccm_encrypt(br_sslrec_ccm_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ br_ccm_context zc;
+ unsigned char *buf;
+ unsigned char nonce[12], header[13];
+ size_t len;
+
+ buf = (unsigned char *)data;
+ len = *data_len;
+
+ /*
+ * Make nonce; the explicit part is an encoding of the sequence
+ * number.
+ */
+ memcpy(nonce, cc->iv, sizeof cc->iv);
+ br_enc64be(nonce + 4, cc->seq);
+
+ /*
+ * Assemble synthetic header for the AAD.
+ */
+ br_enc64be(header, cc->seq ++);
+ header[8] = (unsigned char)record_type;
+ br_enc16be(header + 9, version);
+ br_enc16be(header + 11, len);
+
+ /*
+ * Perform CCM encryption.
+ */
+ br_ccm_init(&zc, &cc->bc.vtable);
+ br_ccm_reset(&zc, nonce, sizeof nonce, sizeof header, len, cc->tag_len);
+ br_ccm_aad_inject(&zc, header, sizeof header);
+ br_ccm_flip(&zc);
+ br_ccm_run(&zc, 1, buf, len);
+ br_ccm_get_tag(&zc, buf + len);
+
+ /*
+ * Assemble header and adjust pointer/length.
+ */
+ len += 8 + cc->tag_len;
+ buf -= 13;
+ memcpy(buf + 5, nonce + 4, 8);
+ buf[0] = (unsigned char)record_type;
+ br_enc16be(buf + 1, version);
+ br_enc16be(buf + 3, len);
+ *data_len = len + 5;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable = {
+ {
+ sizeof(br_sslrec_ccm_context),
+ (void (*)(const br_sslrec_out_class *const *,
+ size_t *, size_t *))
+ &ccm_max_plaintext,
+ (unsigned char *(*)(const br_sslrec_out_class **,
+ int, unsigned, void *, size_t *))
+ &ccm_encrypt
+ },
+ (void (*)(const br_sslrec_out_ccm_class **,
+ const br_block_ctrcbc_class *, const void *, size_t,
+ const void *, size_t))
+ &out_ccm_init
+};
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_rec_chapol.c b/test/monniaux/BearSSL/src/ssl/ssl_rec_chapol.c
new file mode 100644
index 00000000..73b3c785
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_rec_chapol.c
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static void
+gen_chapol_init(br_sslrec_chapol_context *cc,
+ br_chacha20_run ichacha, br_poly1305_run ipoly,
+ const void *key, const void *iv)
+{
+ cc->seq = 0;
+ cc->ichacha = ichacha;
+ cc->ipoly = ipoly;
+ memcpy(cc->key, key, sizeof cc->key);
+ memcpy(cc->iv, iv, sizeof cc->iv);
+}
+
+static void
+gen_chapol_process(br_sslrec_chapol_context *cc,
+ int record_type, unsigned version, void *data, size_t len,
+ void *tag, int encrypt)
+{
+ unsigned char header[13];
+ unsigned char nonce[12];
+ uint64_t seq;
+ size_t u;
+
+ seq = cc->seq ++;
+ br_enc64be(header, seq);
+ header[8] = (unsigned char)record_type;
+ br_enc16be(header + 9, version);
+ br_enc16be(header + 11, len);
+ memcpy(nonce, cc->iv, 12);
+ for (u = 0; u < 8; u ++) {
+ nonce[11 - u] ^= (unsigned char)seq;
+ seq >>= 8;
+ }
+ cc->ipoly(cc->key, nonce, data, len, header, sizeof header,
+ tag, cc->ichacha, encrypt);
+}
+
+static void
+in_chapol_init(br_sslrec_chapol_context *cc,
+ br_chacha20_run ichacha, br_poly1305_run ipoly,
+ const void *key, const void *iv)
+{
+ cc->vtable.in = &br_sslrec_in_chapol_vtable;
+ gen_chapol_init(cc, ichacha, ipoly, key, iv);
+}
+
+static int
+chapol_check_length(const br_sslrec_chapol_context *cc, size_t rlen)
+{
+ /*
+ * Overhead is just the authentication tag (16 bytes).
+ */
+ (void)cc;
+ return rlen >= 16 && rlen <= (16384 + 16);
+}
+
+static unsigned char *
+chapol_decrypt(br_sslrec_chapol_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf;
+ size_t u, len;
+ unsigned char tag[16];
+ unsigned bad;
+
+ buf = data;
+ len = *data_len - 16;
+ gen_chapol_process(cc, record_type, version, buf, len, tag, 0);
+ bad = 0;
+ for (u = 0; u < 16; u ++) {
+ bad |= tag[u] ^ buf[len + u];
+ }
+ if (bad) {
+ return NULL;
+ }
+ *data_len = len;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_in_chapol_class br_sslrec_in_chapol_vtable = {
+ {
+ sizeof(br_sslrec_chapol_context),
+ (int (*)(const br_sslrec_in_class *const *, size_t))
+ &chapol_check_length,
+ (unsigned char *(*)(const br_sslrec_in_class **,
+ int, unsigned, void *, size_t *))
+ &chapol_decrypt
+ },
+ (void (*)(const br_sslrec_in_chapol_class **,
+ br_chacha20_run, br_poly1305_run,
+ const void *, const void *))
+ &in_chapol_init
+};
+
+static void
+out_chapol_init(br_sslrec_chapol_context *cc,
+ br_chacha20_run ichacha, br_poly1305_run ipoly,
+ const void *key, const void *iv)
+{
+ cc->vtable.out = &br_sslrec_out_chapol_vtable;
+ gen_chapol_init(cc, ichacha, ipoly, key, iv);
+}
+
+static void
+chapol_max_plaintext(const br_sslrec_chapol_context *cc,
+ size_t *start, size_t *end)
+{
+ size_t len;
+
+ (void)cc;
+ len = *end - *start - 16;
+ if (len > 16384) {
+ len = 16384;
+ }
+ *end = *start + len;
+}
+
+static unsigned char *
+chapol_encrypt(br_sslrec_chapol_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf;
+ size_t len;
+
+ buf = data;
+ len = *data_len;
+ gen_chapol_process(cc, record_type, version, buf, len, buf + len, 1);
+ buf -= 5;
+ buf[0] = (unsigned char)record_type;
+ br_enc16be(buf + 1, version);
+ br_enc16be(buf + 3, len + 16);
+ *data_len = len + 21;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable = {
+ {
+ sizeof(br_sslrec_chapol_context),
+ (void (*)(const br_sslrec_out_class *const *,
+ size_t *, size_t *))
+ &chapol_max_plaintext,
+ (unsigned char *(*)(const br_sslrec_out_class **,
+ int, unsigned, void *, size_t *))
+ &chapol_encrypt
+ },
+ (void (*)(const br_sslrec_out_chapol_class **,
+ br_chacha20_run, br_poly1305_run,
+ const void *, const void *))
+ &out_chapol_init
+};
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_rec_gcm.c b/test/monniaux/BearSSL/src/ssl/ssl_rec_gcm.c
new file mode 100644
index 00000000..70df2777
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_rec_gcm.c
@@ -0,0 +1,235 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * GCM initialisation. This does everything except setting the vtable,
+ * which depends on whether this is a context for encrypting or for
+ * decrypting.
+ */
+static void
+gen_gcm_init(br_sslrec_gcm_context *cc,
+ const br_block_ctr_class *bc_impl,
+ const void *key, size_t key_len,
+ br_ghash gh_impl,
+ const void *iv)
+{
+ unsigned char tmp[12];
+
+ cc->seq = 0;
+ bc_impl->init(&cc->bc.vtable, key, key_len);
+ cc->gh = gh_impl;
+ memcpy(cc->iv, iv, sizeof cc->iv);
+ memset(cc->h, 0, sizeof cc->h);
+ memset(tmp, 0, sizeof tmp);
+ bc_impl->run(&cc->bc.vtable, tmp, 0, cc->h, sizeof cc->h);
+}
+
+static void
+in_gcm_init(br_sslrec_gcm_context *cc,
+ const br_block_ctr_class *bc_impl,
+ const void *key, size_t key_len,
+ br_ghash gh_impl,
+ const void *iv)
+{
+ cc->vtable.in = &br_sslrec_in_gcm_vtable;
+ gen_gcm_init(cc, bc_impl, key, key_len, gh_impl, iv);
+}
+
+static int
+gcm_check_length(const br_sslrec_gcm_context *cc, size_t rlen)
+{
+ /*
+ * GCM adds a fixed overhead:
+ * 8 bytes for the nonce_explicit (before the ciphertext)
+ * 16 bytes for the authentication tag (after the ciphertext)
+ */
+ (void)cc;
+ return rlen >= 24 && rlen <= (16384 + 24);
+}
+
+/*
+ * Compute the authentication tag. The value written in 'tag' must still
+ * be CTR-encrypted.
+ */
+static void
+do_tag(br_sslrec_gcm_context *cc,
+ int record_type, unsigned version,
+ void *data, size_t len, void *tag)
+{
+ unsigned char header[13];
+ unsigned char footer[16];
+
+ /*
+ * Compute authentication tag. Three elements must be injected in
+ * sequence, each possibly 0-padded to reach a length multiple
+ * of the block size: the 13-byte header (sequence number, record
+ * type, protocol version, record length), the cipher text, and
+ * the word containing the encodings of the bit lengths of the two
+ * other elements.
+ */
+ br_enc64be(header, cc->seq ++);
+ header[8] = (unsigned char)record_type;
+ br_enc16be(header + 9, version);
+ br_enc16be(header + 11, len);
+ br_enc64be(footer, (uint64_t)(sizeof header) << 3);
+ br_enc64be(footer + 8, (uint64_t)len << 3);
+ memset(tag, 0, 16);
+ cc->gh(tag, cc->h, header, sizeof header);
+ cc->gh(tag, cc->h, data, len);
+ cc->gh(tag, cc->h, footer, sizeof footer);
+}
+
+/*
+ * Do CTR encryption. This also does CTR encryption of a single block at
+ * address 'xortag' with the counter value appropriate for the final
+ * processing of the authentication tag.
+ */
+static void
+do_ctr(br_sslrec_gcm_context *cc, const void *nonce, void *data, size_t len,
+ void *xortag)
+{
+ unsigned char iv[12];
+
+ memcpy(iv, cc->iv, 4);
+ memcpy(iv + 4, nonce, 8);
+ cc->bc.vtable->run(&cc->bc.vtable, iv, 2, data, len);
+ cc->bc.vtable->run(&cc->bc.vtable, iv, 1, xortag, 16);
+}
+
+static unsigned char *
+gcm_decrypt(br_sslrec_gcm_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf;
+ size_t len, u;
+ uint32_t bad;
+ unsigned char tag[16];
+
+ buf = (unsigned char *)data + 8;
+ len = *data_len - 24;
+ do_tag(cc, record_type, version, buf, len, tag);
+ do_ctr(cc, data, buf, len, tag);
+
+ /*
+ * Compare the computed tag with the value from the record. It
+ * is possibly useless to do a constant-time comparison here,
+ * but it does not hurt.
+ */
+ bad = 0;
+ for (u = 0; u < 16; u ++) {
+ bad |= tag[u] ^ buf[len + u];
+ }
+ if (bad) {
+ return NULL;
+ }
+ *data_len = len;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable = {
+ {
+ sizeof(br_sslrec_gcm_context),
+ (int (*)(const br_sslrec_in_class *const *, size_t))
+ &gcm_check_length,
+ (unsigned char *(*)(const br_sslrec_in_class **,
+ int, unsigned, void *, size_t *))
+ &gcm_decrypt
+ },
+ (void (*)(const br_sslrec_in_gcm_class **,
+ const br_block_ctr_class *, const void *, size_t,
+ br_ghash, const void *))
+ &in_gcm_init
+};
+
+static void
+out_gcm_init(br_sslrec_gcm_context *cc,
+ const br_block_ctr_class *bc_impl,
+ const void *key, size_t key_len,
+ br_ghash gh_impl,
+ const void *iv)
+{
+ cc->vtable.out = &br_sslrec_out_gcm_vtable;
+ gen_gcm_init(cc, bc_impl, key, key_len, gh_impl, iv);
+}
+
+static void
+gcm_max_plaintext(const br_sslrec_gcm_context *cc,
+ size_t *start, size_t *end)
+{
+ size_t len;
+
+ (void)cc;
+ *start += 8;
+ len = *end - *start - 16;
+ if (len > 16384) {
+ len = 16384;
+ }
+ *end = *start + len;
+}
+
+static unsigned char *
+gcm_encrypt(br_sslrec_gcm_context *cc,
+ int record_type, unsigned version, void *data, size_t *data_len)
+{
+ unsigned char *buf;
+ size_t u, len;
+ unsigned char tmp[16];
+
+ buf = (unsigned char *)data;
+ len = *data_len;
+ memset(tmp, 0, sizeof tmp);
+ br_enc64be(buf - 8, cc->seq);
+ do_ctr(cc, buf - 8, buf, len, tmp);
+ do_tag(cc, record_type, version, buf, len, buf + len);
+ for (u = 0; u < 16; u ++) {
+ buf[len + u] ^= tmp[u];
+ }
+ len += 24;
+ buf -= 13;
+ buf[0] = (unsigned char)record_type;
+ br_enc16be(buf + 1, version);
+ br_enc16be(buf + 3, len);
+ *data_len = len + 5;
+ return buf;
+}
+
+/* see bearssl_ssl.h */
+const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable = {
+ {
+ sizeof(br_sslrec_gcm_context),
+ (void (*)(const br_sslrec_out_class *const *,
+ size_t *, size_t *))
+ &gcm_max_plaintext,
+ (unsigned char *(*)(const br_sslrec_out_class **,
+ int, unsigned, void *, size_t *))
+ &gcm_encrypt
+ },
+ (void (*)(const br_sslrec_out_gcm_class **,
+ const br_block_ctr_class *, const void *, size_t,
+ br_ghash, const void *))
+ &out_gcm_init
+};
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_scert_single_ec.c b/test/monniaux/BearSSL/src/ssl/ssl_scert_single_ec.c
new file mode 100644
index 00000000..ce8d7539
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_scert_single_ec.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static int
+se_choose(const br_ssl_server_policy_class **pctx,
+ const br_ssl_server_context *cc,
+ br_ssl_server_choices *choices)
+{
+ br_ssl_server_policy_ec_context *pc;
+ const br_suite_translated *st;
+ size_t u, st_num;
+ unsigned hash_id;
+
+ pc = (br_ssl_server_policy_ec_context *)pctx;
+ st = br_ssl_server_get_client_suites(cc, &st_num);
+ hash_id = br_ssl_choose_hash(br_ssl_server_get_client_hashes(cc) >> 8);
+ if (cc->eng.session.version < BR_TLS12) {
+ hash_id = br_sha1_ID;
+ }
+ choices->chain = pc->chain;
+ choices->chain_len = pc->chain_len;
+ for (u = 0; u < st_num; u ++) {
+ unsigned tt;
+
+ tt = st[u][1];
+ switch (tt >> 12) {
+ case BR_SSLKEYX_ECDH_RSA:
+ if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0
+ && pc->cert_issuer_key_type == BR_KEYTYPE_RSA)
+ {
+ choices->cipher_suite = st[u][0];
+ return 1;
+ }
+ break;
+ case BR_SSLKEYX_ECDH_ECDSA:
+ if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0
+ && pc->cert_issuer_key_type == BR_KEYTYPE_EC)
+ {
+ choices->cipher_suite = st[u][0];
+ return 1;
+ }
+ break;
+ case BR_SSLKEYX_ECDHE_ECDSA:
+ if ((pc->allowed_usages & BR_KEYTYPE_SIGN) != 0
+ && hash_id != 0)
+ {
+ choices->cipher_suite = st[u][0];
+ choices->algo_id = hash_id + 0xFF00;
+ return 1;
+ }
+ break;
+ }
+ }
+ return 0;
+}
+
+static uint32_t
+se_do_keyx(const br_ssl_server_policy_class **pctx,
+ unsigned char *data, size_t *len)
+{
+ br_ssl_server_policy_ec_context *pc;
+ uint32_t r;
+ size_t xoff, xlen;
+
+ pc = (br_ssl_server_policy_ec_context *)pctx;
+ r = pc->iec->mul(data, *len, pc->sk->x, pc->sk->xlen, pc->sk->curve);
+ xoff = pc->iec->xoff(pc->sk->curve, &xlen);
+ memmove(data, data + xoff, xlen);
+ *len = xlen;
+ return r;
+}
+
+static size_t
+se_do_sign(const br_ssl_server_policy_class **pctx,
+ unsigned algo_id, unsigned char *data, size_t hv_len, size_t len)
+{
+ br_ssl_server_policy_ec_context *pc;
+ unsigned char hv[64];
+ const br_hash_class *hc;
+
+ algo_id &= 0xFF;
+ pc = (br_ssl_server_policy_ec_context *)pctx;
+ hc = br_multihash_getimpl(pc->mhash, algo_id);
+ if (hc == NULL) {
+ return 0;
+ }
+ memcpy(hv, data, hv_len);
+ if (len < 139) {
+ return 0;
+ }
+ return pc->iecdsa(pc->iec, hc, hv, pc->sk, data);
+}
+
+static const br_ssl_server_policy_class se_policy_vtable = {
+ sizeof(br_ssl_server_policy_ec_context),
+ se_choose,
+ se_do_keyx,
+ se_do_sign
+};
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_set_single_ec(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk, unsigned allowed_usages,
+ unsigned cert_issuer_key_type,
+ const br_ec_impl *iec, br_ecdsa_sign iecdsa)
+{
+ cc->chain_handler.single_ec.vtable = &se_policy_vtable;
+ cc->chain_handler.single_ec.chain = chain;
+ cc->chain_handler.single_ec.chain_len = chain_len;
+ cc->chain_handler.single_ec.sk = sk;
+ cc->chain_handler.single_ec.allowed_usages = allowed_usages;
+ cc->chain_handler.single_ec.cert_issuer_key_type = cert_issuer_key_type;
+ cc->chain_handler.single_ec.mhash = &cc->eng.mhash;
+ cc->chain_handler.single_ec.iec = iec;
+ cc->chain_handler.single_ec.iecdsa = iecdsa;
+ cc->policy_vtable = &cc->chain_handler.single_ec.vtable;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_scert_single_rsa.c b/test/monniaux/BearSSL/src/ssl/ssl_scert_single_rsa.c
new file mode 100644
index 00000000..b2c77679
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_scert_single_rsa.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static int
+sr_choose(const br_ssl_server_policy_class **pctx,
+ const br_ssl_server_context *cc,
+ br_ssl_server_choices *choices)
+{
+ br_ssl_server_policy_rsa_context *pc;
+ const br_suite_translated *st;
+ size_t u, st_num;
+ unsigned hash_id;
+ int fh;
+
+ pc = (br_ssl_server_policy_rsa_context *)pctx;
+ st = br_ssl_server_get_client_suites(cc, &st_num);
+ if (cc->eng.session.version < BR_TLS12) {
+ hash_id = 0;
+ fh = 1;
+ } else {
+ hash_id = br_ssl_choose_hash(
+ br_ssl_server_get_client_hashes(cc));
+ fh = (hash_id != 0);
+ }
+ choices->chain = pc->chain;
+ choices->chain_len = pc->chain_len;
+ for (u = 0; u < st_num; u ++) {
+ unsigned tt;
+
+ tt = st[u][1];
+ switch (tt >> 12) {
+ case BR_SSLKEYX_RSA:
+ if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0) {
+ choices->cipher_suite = st[u][0];
+ return 1;
+ }
+ break;
+ case BR_SSLKEYX_ECDHE_RSA:
+ if ((pc->allowed_usages & BR_KEYTYPE_SIGN) != 0 && fh) {
+ choices->cipher_suite = st[u][0];
+ choices->algo_id = hash_id + 0xFF00;
+ return 1;
+ }
+ break;
+ }
+ }
+ return 0;
+}
+
+static uint32_t
+sr_do_keyx(const br_ssl_server_policy_class **pctx,
+ unsigned char *data, size_t *len)
+{
+ br_ssl_server_policy_rsa_context *pc;
+
+ pc = (br_ssl_server_policy_rsa_context *)pctx;
+ return br_rsa_ssl_decrypt(pc->irsacore, pc->sk, data, *len);
+}
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char HASH_OID_SHA1[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char HASH_OID_SHA224[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char HASH_OID_SHA256[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char HASH_OID_SHA384[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char HASH_OID_SHA512[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static const unsigned char *HASH_OID[] = {
+ HASH_OID_SHA1,
+ HASH_OID_SHA224,
+ HASH_OID_SHA256,
+ HASH_OID_SHA384,
+ HASH_OID_SHA512
+};
+
+static size_t
+sr_do_sign(const br_ssl_server_policy_class **pctx,
+ unsigned algo_id, unsigned char *data, size_t hv_len, size_t len)
+{
+ br_ssl_server_policy_rsa_context *pc;
+ unsigned char hv[64];
+ size_t sig_len;
+ const unsigned char *hash_oid;
+
+ pc = (br_ssl_server_policy_rsa_context *)pctx;
+ memcpy(hv, data, hv_len);
+ algo_id &= 0xFF;
+ if (algo_id == 0) {
+ hash_oid = NULL;
+ } else if (algo_id >= 2 && algo_id <= 6) {
+ hash_oid = HASH_OID[algo_id - 2];
+ } else {
+ return 0;
+ }
+ sig_len = (pc->sk->n_bitlen + 7) >> 3;
+ if (len < sig_len) {
+ return 0;
+ }
+ return pc->irsasign(hash_oid, hv, hv_len, pc->sk, data) ? sig_len : 0;
+}
+
+static const br_ssl_server_policy_class sr_policy_vtable = {
+ sizeof(br_ssl_server_policy_rsa_context),
+ sr_choose,
+ sr_do_keyx,
+ sr_do_sign
+};
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_set_single_rsa(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk, unsigned allowed_usages,
+ br_rsa_private irsacore, br_rsa_pkcs1_sign irsasign)
+{
+ cc->chain_handler.single_rsa.vtable = &sr_policy_vtable;
+ cc->chain_handler.single_rsa.chain = chain;
+ cc->chain_handler.single_rsa.chain_len = chain_len;
+ cc->chain_handler.single_rsa.sk = sk;
+ cc->chain_handler.single_rsa.allowed_usages = allowed_usages;
+ cc->chain_handler.single_rsa.irsacore = irsacore;
+ cc->chain_handler.single_rsa.irsasign = irsasign;
+ cc->policy_vtable = &cc->chain_handler.single_rsa.vtable;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server.c b/test/monniaux/BearSSL/src/ssl/ssl_server.c
new file mode 100644
index 00000000..5578b630
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_zero(br_ssl_server_context *cc)
+{
+ /*
+ * For really standard C, we should explicitly set to NULL all
+ * pointers, and 0 all other fields. However, on all our target
+ * architectures, a direct memset() will work, be faster, and
+ * use a lot less code.
+ */
+ memset(cc, 0, sizeof *cc);
+}
+
+/* see bearssl_ssl.h */
+int
+br_ssl_server_reset(br_ssl_server_context *cc)
+{
+ br_ssl_engine_set_buffer(&cc->eng, NULL, 0, 0);
+ if (!br_ssl_engine_init_rand(&cc->eng)) {
+ return 0;
+ }
+ cc->eng.reneg = 0;
+ br_ssl_engine_hs_reset(&cc->eng,
+ br_ssl_hs_server_init_main, br_ssl_hs_server_run);
+ return br_ssl_engine_last_error(&cc->eng) == BR_ERR_OK;
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_full_ec.c b/test/monniaux/BearSSL/src/ssl/ssl_server_full_ec.c
new file mode 100644
index 00000000..bccc0930
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_full_ec.c
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_full_ec(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ unsigned cert_issuer_key_type, const br_ec_private_key *sk)
+{
+ /*
+ * The "full" profile supports all implemented cipher suites.
+ *
+ * Rationale for suite order, from most important to least
+ * important rule:
+ *
+ * -- Don't use 3DES if AES is available.
+ * -- Try to have Forward Secrecy (ECDHE suite) if possible.
+ * -- ChaCha20+Poly1305 is better than AES/GCM (faster, smaller).
+ * -- GCM is better than CCM and CBC. CCM is better than CBC.
+ * -- CCM is better than CCM_8.
+ * -- AES-128 is preferred over AES-256 (AES-128 is already
+ * strong enough, and AES-256 is 40% more expensive).
+ *
+ * Note that for ECDH suites, the list will be automatically
+ * filtered based on the issuing CA key type.
+ */
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
+ };
+
+ /*
+ * All hash functions are activated.
+ * Note: the X.509 validation engine will nonetheless refuse to
+ * validate signatures that use MD5 as hash function.
+ */
+ static const br_hash_class *hashes[] = {
+ &br_md5_vtable,
+ &br_sha1_vtable,
+ &br_sha224_vtable,
+ &br_sha256_vtable,
+ &br_sha384_vtable,
+ &br_sha512_vtable
+ };
+
+ int id;
+
+ /*
+ * Reset server context and set supported versions from TLS-1.0
+ * to TLS-1.2 (inclusive).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_default_ec(&cc->eng);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ cert_issuer_key_type,
+ br_ssl_engine_get_ec(&cc->eng),
+#if BR_LOMUL
+ br_ecdsa_i15_sign_asn1
+#else
+ br_ecdsa_i31_sign_asn1
+#endif
+ );
+
+ /*
+ * Set supported hash functions.
+ */
+ for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
+ const br_hash_class *hc;
+
+ hc = hashes[id - 1];
+ br_ssl_engine_set_hash(&cc->eng, id, hc);
+ }
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+ br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_cbc(&cc->eng);
+ br_ssl_engine_set_default_aes_ccm(&cc->eng);
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+ br_ssl_engine_set_default_des_cbc(&cc->eng);
+ br_ssl_engine_set_default_chapol(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_full_rsa.c b/test/monniaux/BearSSL/src/ssl/ssl_server_full_rsa.c
new file mode 100644
index 00000000..d67c0761
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_full_rsa.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_full_rsa(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk)
+{
+ /*
+ * The "full" profile supports all implemented cipher suites.
+ *
+ * Rationale for suite order, from most important to least
+ * important rule:
+ *
+ * -- Don't use 3DES if AES is available.
+ * -- Try to have Forward Secrecy (ECDHE suite) if possible.
+ * -- ChaCha20+Poly1305 is better than AES/GCM (faster, smaller).
+ * -- GCM is better than CBC.
+ * -- AES-128 is preferred over AES-256 (AES-128 is already
+ * strong enough, and AES-256 is 40% more expensive).
+ */
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ BR_TLS_RSA_WITH_AES_128_CCM,
+ BR_TLS_RSA_WITH_AES_256_CCM,
+ BR_TLS_RSA_WITH_AES_128_CCM_8,
+ BR_TLS_RSA_WITH_AES_256_CCM_8,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA,
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA,
+ BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
+ };
+
+ /*
+ * All hash functions are activated.
+ * Note: the X.509 validation engine will nonetheless refuse to
+ * validate signatures that use MD5 as hash function.
+ */
+ static const br_hash_class *hashes[] = {
+ &br_md5_vtable,
+ &br_sha1_vtable,
+ &br_sha224_vtable,
+ &br_sha256_vtable,
+ &br_sha384_vtable,
+ &br_sha512_vtable
+ };
+
+ int id;
+
+ /*
+ * Reset server context and set supported versions from TLS-1.0
+ * to TLS-1.2 (inclusive).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS10, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_default_ec(&cc->eng);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ br_rsa_private_get_default(),
+ br_rsa_pkcs1_sign_get_default());
+
+ /*
+ * Set supported hash functions.
+ */
+ for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
+ const br_hash_class *hc;
+
+ hc = hashes[id - 1];
+ br_ssl_engine_set_hash(&cc->eng, id, hc);
+ }
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf10(&cc->eng, &br_tls10_prf);
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+ br_ssl_engine_set_prf_sha384(&cc->eng, &br_tls12_sha384_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_cbc(&cc->eng);
+ br_ssl_engine_set_default_aes_ccm(&cc->eng);
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+ br_ssl_engine_set_default_des_cbc(&cc->eng);
+ br_ssl_engine_set_default_chapol(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_mine2c.c b/test/monniaux/BearSSL/src/ssl/ssl_server_mine2c.c
new file mode 100644
index 00000000..bf61b565
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_mine2c.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_mine2c(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_all_m15);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
+ BR_KEYTYPE_SIGN, 0, br_rsa_i31_pkcs1_sign);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_chapol(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_mine2g.c b/test/monniaux/BearSSL/src/ssl/ssl_server_mine2g.c
new file mode 100644
index 00000000..80fa5b11
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_mine2g.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_mine2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_all_m15);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
+ BR_KEYTYPE_SIGN, 0, br_rsa_i31_pkcs1_sign);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_minf2c.c b/test/monniaux/BearSSL/src/ssl/ssl_server_minf2c.c
new file mode 100644
index 00000000..3f442369
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_minf2c.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_minf2c(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_all_m15);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_SIGN, 0, &br_ec_all_m15, br_ecdsa_i31_sign_asn1);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_chapol(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_minf2g.c b/test/monniaux/BearSSL/src/ssl/ssl_server_minf2g.c
new file mode 100644
index 00000000..8613de1e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_minf2g.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_minf2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites and elliptic curve implementation (for ECDHE).
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+ br_ssl_engine_set_ec(&cc->eng, &br_ec_all_m15);
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_SIGN, 0, &br_ec_all_m15, br_ecdsa_i31_sign_asn1);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_minr2g.c b/test/monniaux/BearSSL/src/ssl/ssl_server_minr2g.c
new file mode 100644
index 00000000..83c238b4
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_minr2g.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_minr2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_rsa_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites.
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_rsa(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX, br_rsa_i31_private, 0);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_minu2g.c b/test/monniaux/BearSSL/src/ssl/ssl_server_minu2g.c
new file mode 100644
index 00000000..67213842
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_minu2g.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_minu2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites.
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX, BR_KEYTYPE_RSA, &br_ec_all_m15, 0);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/ssl/ssl_server_minv2g.c b/test/monniaux/BearSSL/src/ssl/ssl_server_minv2g.c
new file mode 100644
index 00000000..194e654e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/ssl/ssl_server_minv2g.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_ssl.h */
+void
+br_ssl_server_init_minv2g(br_ssl_server_context *cc,
+ const br_x509_certificate *chain, size_t chain_len,
+ const br_ec_private_key *sk)
+{
+ static const uint16_t suites[] = {
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
+ };
+
+ /*
+ * Reset server context and set supported versions to TLS-1.2 (only).
+ */
+ br_ssl_server_zero(cc);
+ br_ssl_engine_set_versions(&cc->eng, BR_TLS12, BR_TLS12);
+
+ /*
+ * Set suites.
+ */
+ br_ssl_engine_set_suites(&cc->eng, suites,
+ (sizeof suites) / (sizeof suites[0]));
+
+ /*
+ * Set the "server policy": handler for the certificate chain
+ * and private key operations.
+ */
+ br_ssl_server_set_single_ec(cc, chain, chain_len, sk,
+ BR_KEYTYPE_KEYX, BR_KEYTYPE_EC, &br_ec_all_m15, 0);
+
+ /*
+ * Set supported hash functions.
+ */
+ br_ssl_engine_set_hash(&cc->eng, br_sha256_ID, &br_sha256_vtable);
+
+ /*
+ * Set the PRF implementations.
+ */
+ br_ssl_engine_set_prf_sha256(&cc->eng, &br_tls12_sha256_prf);
+
+ /*
+ * Symmetric encryption.
+ */
+ br_ssl_engine_set_default_aes_gcm(&cc->eng);
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_big_cbcdec.c
new file mode 100644
index 00000000..d969a3bf
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_cbcdec.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_big_cbcdec_vtable;
+ ctx->num_rounds = br_aes_big_keysched_inv(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16];
+ int i;
+
+ memcpy(tmp, buf, 16);
+ br_aes_big_decrypt(ctx->num_rounds, ctx->skey, buf);
+ for (i = 0; i < 16; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ memcpy(ivbuf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_big_cbcdec_vtable = {
+ sizeof(br_aes_big_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_big_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_big_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_big_cbcenc.c
new file mode 100644
index 00000000..265e53b8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_cbcenc.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_big_cbcenc_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ br_aes_big_encrypt(ctx->num_rounds, ctx->skey, buf);
+ memcpy(ivbuf, buf, 16);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_big_cbcenc_vtable = {
+ sizeof(br_aes_big_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_big_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_big_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_big_ctr.c
new file mode 100644
index 00000000..18fbb846
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_ctr.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_big_ctr_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16];
+
+ memcpy(tmp, iv, 12);
+ br_enc32be(tmp + 12, cc ++);
+ br_aes_big_encrypt(ctx->num_rounds, ctx->skey, tmp);
+ if (len <= 16) {
+ xorbuf(buf, tmp, len);
+ break;
+ }
+ xorbuf(buf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ }
+ return cc;
+}
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_big_ctr_vtable = {
+ sizeof(br_aes_big_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_big_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_big_ctr_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_big_ctrcbc.c
new file mode 100644
index 00000000..d45ca769
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_ctrcbc.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctrcbc_init(br_aes_big_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_big_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctrcbc_ctr(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char *buf, *bctr;
+ uint32_t cc0, cc1, cc2, cc3;
+
+ buf = data;
+ bctr = ctr;
+ cc3 = br_dec32be(bctr + 0);
+ cc2 = br_dec32be(bctr + 4);
+ cc1 = br_dec32be(bctr + 8);
+ cc0 = br_dec32be(bctr + 12);
+ while (len > 0) {
+ unsigned char tmp[16];
+ uint32_t carry;
+
+ br_enc32be(tmp + 0, cc3);
+ br_enc32be(tmp + 4, cc2);
+ br_enc32be(tmp + 8, cc1);
+ br_enc32be(tmp + 12, cc0);
+ br_aes_big_encrypt(ctx->num_rounds, ctx->skey, tmp);
+ xorbuf(buf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ cc0 ++;
+ carry = (~(cc0 | -cc0)) >> 31;
+ cc1 += carry;
+ carry &= (~(cc1 | -cc1)) >> 31;
+ cc2 += carry;
+ carry &= (~(cc2 | -cc2)) >> 31;
+ cc3 += carry;
+ }
+ br_enc32be(bctr + 0, cc3);
+ br_enc32be(bctr + 4, cc2);
+ br_enc32be(bctr + 8, cc1);
+ br_enc32be(bctr + 12, cc0);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctrcbc_mac(const br_aes_big_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ const unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ xorbuf(cbcmac, buf, 16);
+ br_aes_big_encrypt(ctx->num_rounds, ctx->skey, cbcmac);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctrcbc_encrypt(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ br_aes_big_ctrcbc_ctr(ctx, ctr, data, len);
+ br_aes_big_ctrcbc_mac(ctx, cbcmac, data, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_big_ctrcbc_decrypt(const br_aes_big_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ br_aes_big_ctrcbc_mac(ctx, cbcmac, data, len);
+ br_aes_big_ctrcbc_ctr(ctx, ctr, data, len);
+}
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_big_ctrcbc_vtable = {
+ sizeof(br_aes_big_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_big_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_big_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_big_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_big_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_big_ctrcbc_mac
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_dec.c b/test/monniaux/BearSSL/src/symcipher/aes_big_dec.c
new file mode 100644
index 00000000..a5d0e3c6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_dec.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Inverse S-box (used in key schedule for decryption).
+ */
+static const unsigned char iS[] = {
+ 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
+ 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
+ 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
+ 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
+ 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
+ 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
+ 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
+ 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
+ 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
+ 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
+ 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
+ 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
+ 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
+ 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
+ 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
+ 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
+ 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
+ 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
+ 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
+ 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
+ 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
+ 0x55, 0x21, 0x0C, 0x7D
+};
+
+static const uint32_t iSsm0[] = {
+ 0x51F4A750, 0x7E416553, 0x1A17A4C3, 0x3A275E96, 0x3BAB6BCB, 0x1F9D45F1,
+ 0xACFA58AB, 0x4BE30393, 0x2030FA55, 0xAD766DF6, 0x88CC7691, 0xF5024C25,
+ 0x4FE5D7FC, 0xC52ACBD7, 0x26354480, 0xB562A38F, 0xDEB15A49, 0x25BA1B67,
+ 0x45EA0E98, 0x5DFEC0E1, 0xC32F7502, 0x814CF012, 0x8D4697A3, 0x6BD3F9C6,
+ 0x038F5FE7, 0x15929C95, 0xBF6D7AEB, 0x955259DA, 0xD4BE832D, 0x587421D3,
+ 0x49E06929, 0x8EC9C844, 0x75C2896A, 0xF48E7978, 0x99583E6B, 0x27B971DD,
+ 0xBEE14FB6, 0xF088AD17, 0xC920AC66, 0x7DCE3AB4, 0x63DF4A18, 0xE51A3182,
+ 0x97513360, 0x62537F45, 0xB16477E0, 0xBB6BAE84, 0xFE81A01C, 0xF9082B94,
+ 0x70486858, 0x8F45FD19, 0x94DE6C87, 0x527BF8B7, 0xAB73D323, 0x724B02E2,
+ 0xE31F8F57, 0x6655AB2A, 0xB2EB2807, 0x2FB5C203, 0x86C57B9A, 0xD33708A5,
+ 0x302887F2, 0x23BFA5B2, 0x02036ABA, 0xED16825C, 0x8ACF1C2B, 0xA779B492,
+ 0xF307F2F0, 0x4E69E2A1, 0x65DAF4CD, 0x0605BED5, 0xD134621F, 0xC4A6FE8A,
+ 0x342E539D, 0xA2F355A0, 0x058AE132, 0xA4F6EB75, 0x0B83EC39, 0x4060EFAA,
+ 0x5E719F06, 0xBD6E1051, 0x3E218AF9, 0x96DD063D, 0xDD3E05AE, 0x4DE6BD46,
+ 0x91548DB5, 0x71C45D05, 0x0406D46F, 0x605015FF, 0x1998FB24, 0xD6BDE997,
+ 0x894043CC, 0x67D99E77, 0xB0E842BD, 0x07898B88, 0xE7195B38, 0x79C8EEDB,
+ 0xA17C0A47, 0x7C420FE9, 0xF8841EC9, 0x00000000, 0x09808683, 0x322BED48,
+ 0x1E1170AC, 0x6C5A724E, 0xFD0EFFFB, 0x0F853856, 0x3DAED51E, 0x362D3927,
+ 0x0A0FD964, 0x685CA621, 0x9B5B54D1, 0x24362E3A, 0x0C0A67B1, 0x9357E70F,
+ 0xB4EE96D2, 0x1B9B919E, 0x80C0C54F, 0x61DC20A2, 0x5A774B69, 0x1C121A16,
+ 0xE293BA0A, 0xC0A02AE5, 0x3C22E043, 0x121B171D, 0x0E090D0B, 0xF28BC7AD,
+ 0x2DB6A8B9, 0x141EA9C8, 0x57F11985, 0xAF75074C, 0xEE99DDBB, 0xA37F60FD,
+ 0xF701269F, 0x5C72F5BC, 0x44663BC5, 0x5BFB7E34, 0x8B432976, 0xCB23C6DC,
+ 0xB6EDFC68, 0xB8E4F163, 0xD731DCCA, 0x42638510, 0x13972240, 0x84C61120,
+ 0x854A247D, 0xD2BB3DF8, 0xAEF93211, 0xC729A16D, 0x1D9E2F4B, 0xDCB230F3,
+ 0x0D8652EC, 0x77C1E3D0, 0x2BB3166C, 0xA970B999, 0x119448FA, 0x47E96422,
+ 0xA8FC8CC4, 0xA0F03F1A, 0x567D2CD8, 0x223390EF, 0x87494EC7, 0xD938D1C1,
+ 0x8CCAA2FE, 0x98D40B36, 0xA6F581CF, 0xA57ADE28, 0xDAB78E26, 0x3FADBFA4,
+ 0x2C3A9DE4, 0x5078920D, 0x6A5FCC9B, 0x547E4662, 0xF68D13C2, 0x90D8B8E8,
+ 0x2E39F75E, 0x82C3AFF5, 0x9F5D80BE, 0x69D0937C, 0x6FD52DA9, 0xCF2512B3,
+ 0xC8AC993B, 0x10187DA7, 0xE89C636E, 0xDB3BBB7B, 0xCD267809, 0x6E5918F4,
+ 0xEC9AB701, 0x834F9AA8, 0xE6956E65, 0xAAFFE67E, 0x21BCCF08, 0xEF15E8E6,
+ 0xBAE79BD9, 0x4A6F36CE, 0xEA9F09D4, 0x29B07CD6, 0x31A4B2AF, 0x2A3F2331,
+ 0xC6A59430, 0x35A266C0, 0x744EBC37, 0xFC82CAA6, 0xE090D0B0, 0x33A7D815,
+ 0xF104984A, 0x41ECDAF7, 0x7FCD500E, 0x1791F62F, 0x764DD68D, 0x43EFB04D,
+ 0xCCAA4D54, 0xE49604DF, 0x9ED1B5E3, 0x4C6A881B, 0xC12C1FB8, 0x4665517F,
+ 0x9D5EEA04, 0x018C355D, 0xFA877473, 0xFB0B412E, 0xB3671D5A, 0x92DBD252,
+ 0xE9105633, 0x6DD64713, 0x9AD7618C, 0x37A10C7A, 0x59F8148E, 0xEB133C89,
+ 0xCEA927EE, 0xB761C935, 0xE11CE5ED, 0x7A47B13C, 0x9CD2DF59, 0x55F2733F,
+ 0x1814CE79, 0x73C737BF, 0x53F7CDEA, 0x5FFDAA5B, 0xDF3D6F14, 0x7844DB86,
+ 0xCAAFF381, 0xB968C43E, 0x3824342C, 0xC2A3405F, 0x161DC372, 0xBCE2250C,
+ 0x283C498B, 0xFF0D9541, 0x39A80171, 0x080CB3DE, 0xD8B4E49C, 0x6456C190,
+ 0x7BCB8461, 0xD532B670, 0x486C5C74, 0xD0B85742
+};
+
+static unsigned
+mul2(unsigned x)
+{
+ x <<= 1;
+ return x ^ ((unsigned)(-(int)(x >> 8)) & 0x11B);
+}
+
+static unsigned
+mul9(unsigned x)
+{
+ return x ^ mul2(mul2(mul2(x)));
+}
+
+static unsigned
+mulb(unsigned x)
+{
+ unsigned x2;
+
+ x2 = mul2(x);
+ return x ^ x2 ^ mul2(mul2(x2));
+}
+
+static unsigned
+muld(unsigned x)
+{
+ unsigned x4;
+
+ x4 = mul2(mul2(x));
+ return x ^ x4 ^ mul2(x4);
+}
+
+static unsigned
+mule(unsigned x)
+{
+ unsigned x2, x4;
+
+ x2 = mul2(x);
+ x4 = mul2(x2);
+ return x2 ^ x4 ^ mul2(x4);
+}
+
+/* see inner.h */
+unsigned
+br_aes_big_keysched_inv(uint32_t *skey, const void *key, size_t key_len)
+{
+ unsigned num_rounds;
+ int i, m;
+
+ /*
+ * Sub-keys for decryption are distinct from encryption sub-keys
+ * in that InvMixColumns() is already applied for the inner
+ * rounds.
+ */
+ num_rounds = br_aes_keysched(skey, key, key_len);
+ m = (int)(num_rounds << 2);
+ for (i = 4; i < m; i ++) {
+ uint32_t p;
+ unsigned p0, p1, p2, p3;
+ uint32_t q0, q1, q2, q3;
+
+ p = skey[i];
+ p0 = p >> 24;
+ p1 = (p >> 16) & 0xFF;
+ p2 = (p >> 8) & 0xFF;
+ p3 = p & 0xFF;
+ q0 = mule(p0) ^ mulb(p1) ^ muld(p2) ^ mul9(p3);
+ q1 = mul9(p0) ^ mule(p1) ^ mulb(p2) ^ muld(p3);
+ q2 = muld(p0) ^ mul9(p1) ^ mule(p2) ^ mulb(p3);
+ q3 = mulb(p0) ^ muld(p1) ^ mul9(p2) ^ mule(p3);
+ skey[i] = (q0 << 24) | (q1 << 16) | (q2 << 8) | q3;
+ }
+ return num_rounds;
+}
+
+static inline uint32_t
+rotr(uint32_t x, int n)
+{
+ return (x << (32 - n)) | (x >> n);
+}
+
+#define iSboxExt0(x) (iSsm0[x])
+#define iSboxExt1(x) (rotr(iSsm0[x], 8))
+#define iSboxExt2(x) (rotr(iSsm0[x], 16))
+#define iSboxExt3(x) (rotr(iSsm0[x], 24))
+
+/* see bearssl.h */
+void
+br_aes_big_decrypt(unsigned num_rounds, const uint32_t *skey, void *data)
+{
+ unsigned char *buf;
+ uint32_t s0, s1, s2, s3;
+ uint32_t t0, t1, t2, t3;
+ unsigned u;
+
+ buf = data;
+ s0 = br_dec32be(buf);
+ s1 = br_dec32be(buf + 4);
+ s2 = br_dec32be(buf + 8);
+ s3 = br_dec32be(buf + 12);
+ s0 ^= skey[(num_rounds << 2) + 0];
+ s1 ^= skey[(num_rounds << 2) + 1];
+ s2 ^= skey[(num_rounds << 2) + 2];
+ s3 ^= skey[(num_rounds << 2) + 3];
+ for (u = num_rounds - 1; u > 0; u --) {
+ uint32_t v0 = iSboxExt0(s0 >> 24)
+ ^ iSboxExt1((s3 >> 16) & 0xFF)
+ ^ iSboxExt2((s2 >> 8) & 0xFF)
+ ^ iSboxExt3(s1 & 0xFF);
+ uint32_t v1 = iSboxExt0(s1 >> 24)
+ ^ iSboxExt1((s0 >> 16) & 0xFF)
+ ^ iSboxExt2((s3 >> 8) & 0xFF)
+ ^ iSboxExt3(s2 & 0xFF);
+ uint32_t v2 = iSboxExt0(s2 >> 24)
+ ^ iSboxExt1((s1 >> 16) & 0xFF)
+ ^ iSboxExt2((s0 >> 8) & 0xFF)
+ ^ iSboxExt3(s3 & 0xFF);
+ uint32_t v3 = iSboxExt0(s3 >> 24)
+ ^ iSboxExt1((s2 >> 16) & 0xFF)
+ ^ iSboxExt2((s1 >> 8) & 0xFF)
+ ^ iSboxExt3(s0 & 0xFF);
+ s0 = v0;
+ s1 = v1;
+ s2 = v2;
+ s3 = v3;
+ s0 ^= skey[u << 2];
+ s1 ^= skey[(u << 2) + 1];
+ s2 ^= skey[(u << 2) + 2];
+ s3 ^= skey[(u << 2) + 3];
+ }
+ t0 = ((uint32_t)iS[s0 >> 24] << 24)
+ | ((uint32_t)iS[(s3 >> 16) & 0xFF] << 16)
+ | ((uint32_t)iS[(s2 >> 8) & 0xFF] << 8)
+ | (uint32_t)iS[s1 & 0xFF];
+ t1 = ((uint32_t)iS[s1 >> 24] << 24)
+ | ((uint32_t)iS[(s0 >> 16) & 0xFF] << 16)
+ | ((uint32_t)iS[(s3 >> 8) & 0xFF] << 8)
+ | (uint32_t)iS[s2 & 0xFF];
+ t2 = ((uint32_t)iS[s2 >> 24] << 24)
+ | ((uint32_t)iS[(s1 >> 16) & 0xFF] << 16)
+ | ((uint32_t)iS[(s0 >> 8) & 0xFF] << 8)
+ | (uint32_t)iS[s3 & 0xFF];
+ t3 = ((uint32_t)iS[s3 >> 24] << 24)
+ | ((uint32_t)iS[(s2 >> 16) & 0xFF] << 16)
+ | ((uint32_t)iS[(s1 >> 8) & 0xFF] << 8)
+ | (uint32_t)iS[s0 & 0xFF];
+ s0 = t0 ^ skey[0];
+ s1 = t1 ^ skey[1];
+ s2 = t2 ^ skey[2];
+ s3 = t3 ^ skey[3];
+ br_enc32be(buf, s0);
+ br_enc32be(buf + 4, s1);
+ br_enc32be(buf + 8, s2);
+ br_enc32be(buf + 12, s3);
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_big_enc.c b/test/monniaux/BearSSL/src/symcipher/aes_big_enc.c
new file mode 100644
index 00000000..bbabb9a6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_big_enc.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define S br_aes_S
+
+static const uint32_t Ssm0[] = {
+ 0xC66363A5, 0xF87C7C84, 0xEE777799, 0xF67B7B8D, 0xFFF2F20D, 0xD66B6BBD,
+ 0xDE6F6FB1, 0x91C5C554, 0x60303050, 0x02010103, 0xCE6767A9, 0x562B2B7D,
+ 0xE7FEFE19, 0xB5D7D762, 0x4DABABE6, 0xEC76769A, 0x8FCACA45, 0x1F82829D,
+ 0x89C9C940, 0xFA7D7D87, 0xEFFAFA15, 0xB25959EB, 0x8E4747C9, 0xFBF0F00B,
+ 0x41ADADEC, 0xB3D4D467, 0x5FA2A2FD, 0x45AFAFEA, 0x239C9CBF, 0x53A4A4F7,
+ 0xE4727296, 0x9BC0C05B, 0x75B7B7C2, 0xE1FDFD1C, 0x3D9393AE, 0x4C26266A,
+ 0x6C36365A, 0x7E3F3F41, 0xF5F7F702, 0x83CCCC4F, 0x6834345C, 0x51A5A5F4,
+ 0xD1E5E534, 0xF9F1F108, 0xE2717193, 0xABD8D873, 0x62313153, 0x2A15153F,
+ 0x0804040C, 0x95C7C752, 0x46232365, 0x9DC3C35E, 0x30181828, 0x379696A1,
+ 0x0A05050F, 0x2F9A9AB5, 0x0E070709, 0x24121236, 0x1B80809B, 0xDFE2E23D,
+ 0xCDEBEB26, 0x4E272769, 0x7FB2B2CD, 0xEA75759F, 0x1209091B, 0x1D83839E,
+ 0x582C2C74, 0x341A1A2E, 0x361B1B2D, 0xDC6E6EB2, 0xB45A5AEE, 0x5BA0A0FB,
+ 0xA45252F6, 0x763B3B4D, 0xB7D6D661, 0x7DB3B3CE, 0x5229297B, 0xDDE3E33E,
+ 0x5E2F2F71, 0x13848497, 0xA65353F5, 0xB9D1D168, 0x00000000, 0xC1EDED2C,
+ 0x40202060, 0xE3FCFC1F, 0x79B1B1C8, 0xB65B5BED, 0xD46A6ABE, 0x8DCBCB46,
+ 0x67BEBED9, 0x7239394B, 0x944A4ADE, 0x984C4CD4, 0xB05858E8, 0x85CFCF4A,
+ 0xBBD0D06B, 0xC5EFEF2A, 0x4FAAAAE5, 0xEDFBFB16, 0x864343C5, 0x9A4D4DD7,
+ 0x66333355, 0x11858594, 0x8A4545CF, 0xE9F9F910, 0x04020206, 0xFE7F7F81,
+ 0xA05050F0, 0x783C3C44, 0x259F9FBA, 0x4BA8A8E3, 0xA25151F3, 0x5DA3A3FE,
+ 0x804040C0, 0x058F8F8A, 0x3F9292AD, 0x219D9DBC, 0x70383848, 0xF1F5F504,
+ 0x63BCBCDF, 0x77B6B6C1, 0xAFDADA75, 0x42212163, 0x20101030, 0xE5FFFF1A,
+ 0xFDF3F30E, 0xBFD2D26D, 0x81CDCD4C, 0x180C0C14, 0x26131335, 0xC3ECEC2F,
+ 0xBE5F5FE1, 0x359797A2, 0x884444CC, 0x2E171739, 0x93C4C457, 0x55A7A7F2,
+ 0xFC7E7E82, 0x7A3D3D47, 0xC86464AC, 0xBA5D5DE7, 0x3219192B, 0xE6737395,
+ 0xC06060A0, 0x19818198, 0x9E4F4FD1, 0xA3DCDC7F, 0x44222266, 0x542A2A7E,
+ 0x3B9090AB, 0x0B888883, 0x8C4646CA, 0xC7EEEE29, 0x6BB8B8D3, 0x2814143C,
+ 0xA7DEDE79, 0xBC5E5EE2, 0x160B0B1D, 0xADDBDB76, 0xDBE0E03B, 0x64323256,
+ 0x743A3A4E, 0x140A0A1E, 0x924949DB, 0x0C06060A, 0x4824246C, 0xB85C5CE4,
+ 0x9FC2C25D, 0xBDD3D36E, 0x43ACACEF, 0xC46262A6, 0x399191A8, 0x319595A4,
+ 0xD3E4E437, 0xF279798B, 0xD5E7E732, 0x8BC8C843, 0x6E373759, 0xDA6D6DB7,
+ 0x018D8D8C, 0xB1D5D564, 0x9C4E4ED2, 0x49A9A9E0, 0xD86C6CB4, 0xAC5656FA,
+ 0xF3F4F407, 0xCFEAEA25, 0xCA6565AF, 0xF47A7A8E, 0x47AEAEE9, 0x10080818,
+ 0x6FBABAD5, 0xF0787888, 0x4A25256F, 0x5C2E2E72, 0x381C1C24, 0x57A6A6F1,
+ 0x73B4B4C7, 0x97C6C651, 0xCBE8E823, 0xA1DDDD7C, 0xE874749C, 0x3E1F1F21,
+ 0x964B4BDD, 0x61BDBDDC, 0x0D8B8B86, 0x0F8A8A85, 0xE0707090, 0x7C3E3E42,
+ 0x71B5B5C4, 0xCC6666AA, 0x904848D8, 0x06030305, 0xF7F6F601, 0x1C0E0E12,
+ 0xC26161A3, 0x6A35355F, 0xAE5757F9, 0x69B9B9D0, 0x17868691, 0x99C1C158,
+ 0x3A1D1D27, 0x279E9EB9, 0xD9E1E138, 0xEBF8F813, 0x2B9898B3, 0x22111133,
+ 0xD26969BB, 0xA9D9D970, 0x078E8E89, 0x339494A7, 0x2D9B9BB6, 0x3C1E1E22,
+ 0x15878792, 0xC9E9E920, 0x87CECE49, 0xAA5555FF, 0x50282878, 0xA5DFDF7A,
+ 0x038C8C8F, 0x59A1A1F8, 0x09898980, 0x1A0D0D17, 0x65BFBFDA, 0xD7E6E631,
+ 0x844242C6, 0xD06868B8, 0x824141C3, 0x299999B0, 0x5A2D2D77, 0x1E0F0F11,
+ 0x7BB0B0CB, 0xA85454FC, 0x6DBBBBD6, 0x2C16163A
+};
+
+static inline uint32_t
+rotr(uint32_t x, int n)
+{
+ return (x << (32 - n)) | (x >> n);
+}
+
+#define SboxExt0(x) (Ssm0[x])
+#define SboxExt1(x) (rotr(Ssm0[x], 8))
+#define SboxExt2(x) (rotr(Ssm0[x], 16))
+#define SboxExt3(x) (rotr(Ssm0[x], 24))
+
+
+/* see bearssl.h */
+void
+br_aes_big_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
+{
+ unsigned char *buf;
+ uint32_t s0, s1, s2, s3;
+ uint32_t t0, t1, t2, t3;
+ unsigned u;
+
+ buf = data;
+ s0 = br_dec32be(buf);
+ s1 = br_dec32be(buf + 4);
+ s2 = br_dec32be(buf + 8);
+ s3 = br_dec32be(buf + 12);
+ s0 ^= skey[0];
+ s1 ^= skey[1];
+ s2 ^= skey[2];
+ s3 ^= skey[3];
+ for (u = 1; u < num_rounds; u ++) {
+ uint32_t v0, v1, v2, v3;
+
+ v0 = SboxExt0(s0 >> 24)
+ ^ SboxExt1((s1 >> 16) & 0xFF)
+ ^ SboxExt2((s2 >> 8) & 0xFF)
+ ^ SboxExt3(s3 & 0xFF);
+ v1 = SboxExt0(s1 >> 24)
+ ^ SboxExt1((s2 >> 16) & 0xFF)
+ ^ SboxExt2((s3 >> 8) & 0xFF)
+ ^ SboxExt3(s0 & 0xFF);
+ v2 = SboxExt0(s2 >> 24)
+ ^ SboxExt1((s3 >> 16) & 0xFF)
+ ^ SboxExt2((s0 >> 8) & 0xFF)
+ ^ SboxExt3(s1 & 0xFF);
+ v3 = SboxExt0(s3 >> 24)
+ ^ SboxExt1((s0 >> 16) & 0xFF)
+ ^ SboxExt2((s1 >> 8) & 0xFF)
+ ^ SboxExt3(s2 & 0xFF);
+ s0 = v0;
+ s1 = v1;
+ s2 = v2;
+ s3 = v3;
+ s0 ^= skey[u << 2];
+ s1 ^= skey[(u << 2) + 1];
+ s2 ^= skey[(u << 2) + 2];
+ s3 ^= skey[(u << 2) + 3];
+ }
+ t0 = ((uint32_t)S[s0 >> 24] << 24)
+ | ((uint32_t)S[(s1 >> 16) & 0xFF] << 16)
+ | ((uint32_t)S[(s2 >> 8) & 0xFF] << 8)
+ | (uint32_t)S[s3 & 0xFF];
+ t1 = ((uint32_t)S[s1 >> 24] << 24)
+ | ((uint32_t)S[(s2 >> 16) & 0xFF] << 16)
+ | ((uint32_t)S[(s3 >> 8) & 0xFF] << 8)
+ | (uint32_t)S[s0 & 0xFF];
+ t2 = ((uint32_t)S[s2 >> 24] << 24)
+ | ((uint32_t)S[(s3 >> 16) & 0xFF] << 16)
+ | ((uint32_t)S[(s0 >> 8) & 0xFF] << 8)
+ | (uint32_t)S[s1 & 0xFF];
+ t3 = ((uint32_t)S[s3 >> 24] << 24)
+ | ((uint32_t)S[(s0 >> 16) & 0xFF] << 16)
+ | ((uint32_t)S[(s1 >> 8) & 0xFF] << 8)
+ | (uint32_t)S[s2 & 0xFF];
+ s0 = t0 ^ skey[num_rounds << 2];
+ s1 = t1 ^ skey[(num_rounds << 2) + 1];
+ s2 = t2 ^ skey[(num_rounds << 2) + 2];
+ s3 = t3 ^ skey[(num_rounds << 2) + 3];
+ br_enc32be(buf, s0);
+ br_enc32be(buf + 4, s1);
+ br_enc32be(buf + 8, s2);
+ br_enc32be(buf + 12, s3);
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_common.c b/test/monniaux/BearSSL/src/symcipher/aes_common.c
new file mode 100644
index 00000000..72c64fb1
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_common.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static const uint32_t Rcon[] = {
+ 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
+ 0x40000000, 0x80000000, 0x1B000000, 0x36000000
+};
+
+#define S br_aes_S
+
+/* see inner.h */
+const unsigned char br_aes_S[] = {
+ 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B,
+ 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
+ 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
+ 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
+ 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2,
+ 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
+ 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
+ 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
+ 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F,
+ 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
+ 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC,
+ 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
+ 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14,
+ 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
+ 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D,
+ 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
+ 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F,
+ 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
+ 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11,
+ 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
+ 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F,
+ 0xB0, 0x54, 0xBB, 0x16
+};
+
+static uint32_t
+SubWord(uint32_t x)
+{
+ return ((uint32_t)S[x >> 24] << 24)
+ | ((uint32_t)S[(x >> 16) & 0xFF] << 16)
+ | ((uint32_t)S[(x >> 8) & 0xFF] << 8)
+ | (uint32_t)S[x & 0xFF];
+}
+
+/* see inner.h */
+unsigned
+br_aes_keysched(uint32_t *skey, const void *key, size_t key_len)
+{
+ unsigned num_rounds;
+ int i, j, k, nk, nkf;
+
+ switch (key_len) {
+ case 16:
+ num_rounds = 10;
+ break;
+ case 24:
+ num_rounds = 12;
+ break;
+ case 32:
+ num_rounds = 14;
+ break;
+ default:
+ /* abort(); */
+ return 0;
+ }
+ nk = (int)(key_len >> 2);
+ nkf = (int)((num_rounds + 1) << 2);
+ for (i = 0; i < nk; i ++) {
+ skey[i] = br_dec32be((const unsigned char *)key + (i << 2));
+ }
+ for (i = nk, j = 0, k = 0; i < nkf; i ++) {
+ uint32_t tmp;
+
+ tmp = skey[i - 1];
+ if (j == 0) {
+ tmp = (tmp << 8) | (tmp >> 24);
+ tmp = SubWord(tmp) ^ Rcon[k];
+ } else if (nk > 6 && j == 4) {
+ tmp = SubWord(tmp);
+ }
+ skey[i] = skey[i - nk] ^ tmp;
+ if (++ j == nk) {
+ j = 0;
+ k ++;
+ }
+ }
+ return num_rounds;
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct.c b/test/monniaux/BearSSL/src/symcipher/aes_ct.c
new file mode 100644
index 00000000..66776d9e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct.c
@@ -0,0 +1,328 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_aes_ct_bitslice_Sbox(uint32_t *q)
+{
+ /*
+ * This S-box implementation is a straightforward translation of
+ * the circuit described by Boyar and Peralta in "A new
+ * combinational logic minimization technique with applications
+ * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
+ *
+ * Note that variables x* (input) and s* (output) are numbered
+ * in "reverse" order (x0 is the high bit, x7 is the low bit).
+ */
+
+ uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
+ uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
+ uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
+ uint32_t y20, y21;
+ uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
+ uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
+ uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
+ uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
+ uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
+ uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
+ uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
+ uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
+ uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
+ uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
+
+ x0 = q[7];
+ x1 = q[6];
+ x2 = q[5];
+ x3 = q[4];
+ x4 = q[3];
+ x5 = q[2];
+ x6 = q[1];
+ x7 = q[0];
+
+ /*
+ * Top linear transformation.
+ */
+ y14 = x3 ^ x5;
+ y13 = x0 ^ x6;
+ y9 = x0 ^ x3;
+ y8 = x0 ^ x5;
+ t0 = x1 ^ x2;
+ y1 = t0 ^ x7;
+ y4 = y1 ^ x3;
+ y12 = y13 ^ y14;
+ y2 = y1 ^ x0;
+ y5 = y1 ^ x6;
+ y3 = y5 ^ y8;
+ t1 = x4 ^ y12;
+ y15 = t1 ^ x5;
+ y20 = t1 ^ x1;
+ y6 = y15 ^ x7;
+ y10 = y15 ^ t0;
+ y11 = y20 ^ y9;
+ y7 = x7 ^ y11;
+ y17 = y10 ^ y11;
+ y19 = y10 ^ y8;
+ y16 = t0 ^ y11;
+ y21 = y13 ^ y16;
+ y18 = x0 ^ y16;
+
+ /*
+ * Non-linear section.
+ */
+ t2 = y12 & y15;
+ t3 = y3 & y6;
+ t4 = t3 ^ t2;
+ t5 = y4 & x7;
+ t6 = t5 ^ t2;
+ t7 = y13 & y16;
+ t8 = y5 & y1;
+ t9 = t8 ^ t7;
+ t10 = y2 & y7;
+ t11 = t10 ^ t7;
+ t12 = y9 & y11;
+ t13 = y14 & y17;
+ t14 = t13 ^ t12;
+ t15 = y8 & y10;
+ t16 = t15 ^ t12;
+ t17 = t4 ^ t14;
+ t18 = t6 ^ t16;
+ t19 = t9 ^ t14;
+ t20 = t11 ^ t16;
+ t21 = t17 ^ y20;
+ t22 = t18 ^ y19;
+ t23 = t19 ^ y21;
+ t24 = t20 ^ y18;
+
+ t25 = t21 ^ t22;
+ t26 = t21 & t23;
+ t27 = t24 ^ t26;
+ t28 = t25 & t27;
+ t29 = t28 ^ t22;
+ t30 = t23 ^ t24;
+ t31 = t22 ^ t26;
+ t32 = t31 & t30;
+ t33 = t32 ^ t24;
+ t34 = t23 ^ t33;
+ t35 = t27 ^ t33;
+ t36 = t24 & t35;
+ t37 = t36 ^ t34;
+ t38 = t27 ^ t36;
+ t39 = t29 & t38;
+ t40 = t25 ^ t39;
+
+ t41 = t40 ^ t37;
+ t42 = t29 ^ t33;
+ t43 = t29 ^ t40;
+ t44 = t33 ^ t37;
+ t45 = t42 ^ t41;
+ z0 = t44 & y15;
+ z1 = t37 & y6;
+ z2 = t33 & x7;
+ z3 = t43 & y16;
+ z4 = t40 & y1;
+ z5 = t29 & y7;
+ z6 = t42 & y11;
+ z7 = t45 & y17;
+ z8 = t41 & y10;
+ z9 = t44 & y12;
+ z10 = t37 & y3;
+ z11 = t33 & y4;
+ z12 = t43 & y13;
+ z13 = t40 & y5;
+ z14 = t29 & y2;
+ z15 = t42 & y9;
+ z16 = t45 & y14;
+ z17 = t41 & y8;
+
+ /*
+ * Bottom linear transformation.
+ */
+ t46 = z15 ^ z16;
+ t47 = z10 ^ z11;
+ t48 = z5 ^ z13;
+ t49 = z9 ^ z10;
+ t50 = z2 ^ z12;
+ t51 = z2 ^ z5;
+ t52 = z7 ^ z8;
+ t53 = z0 ^ z3;
+ t54 = z6 ^ z7;
+ t55 = z16 ^ z17;
+ t56 = z12 ^ t48;
+ t57 = t50 ^ t53;
+ t58 = z4 ^ t46;
+ t59 = z3 ^ t54;
+ t60 = t46 ^ t57;
+ t61 = z14 ^ t57;
+ t62 = t52 ^ t58;
+ t63 = t49 ^ t58;
+ t64 = z4 ^ t59;
+ t65 = t61 ^ t62;
+ t66 = z1 ^ t63;
+ s0 = t59 ^ t63;
+ s6 = t56 ^ ~t62;
+ s7 = t48 ^ ~t60;
+ t67 = t64 ^ t65;
+ s3 = t53 ^ t66;
+ s4 = t51 ^ t66;
+ s5 = t47 ^ t65;
+ s1 = t64 ^ ~s3;
+ s2 = t55 ^ ~t67;
+
+ q[7] = s0;
+ q[6] = s1;
+ q[5] = s2;
+ q[4] = s3;
+ q[3] = s4;
+ q[2] = s5;
+ q[1] = s6;
+ q[0] = s7;
+}
+
+/* see inner.h */
+void
+br_aes_ct_ortho(uint32_t *q)
+{
+#define SWAPN(cl, ch, s, x, y) do { \
+ uint32_t a, b; \
+ a = (x); \
+ b = (y); \
+ (x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \
+ (y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \
+ } while (0)
+
+#define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
+#define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
+#define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
+
+ SWAP2(q[0], q[1]);
+ SWAP2(q[2], q[3]);
+ SWAP2(q[4], q[5]);
+ SWAP2(q[6], q[7]);
+
+ SWAP4(q[0], q[2]);
+ SWAP4(q[1], q[3]);
+ SWAP4(q[4], q[6]);
+ SWAP4(q[5], q[7]);
+
+ SWAP8(q[0], q[4]);
+ SWAP8(q[1], q[5]);
+ SWAP8(q[2], q[6]);
+ SWAP8(q[3], q[7]);
+}
+
+static const unsigned char Rcon[] = {
+ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
+};
+
+static uint32_t
+sub_word(uint32_t x)
+{
+ uint32_t q[8];
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ q[i] = x;
+ }
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_Sbox(q);
+ br_aes_ct_ortho(q);
+ return q[0];
+}
+
+/* see inner.h */
+unsigned
+br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)
+{
+ unsigned num_rounds;
+ int i, j, k, nk, nkf;
+ uint32_t tmp;
+ uint32_t skey[120];
+
+ switch (key_len) {
+ case 16:
+ num_rounds = 10;
+ break;
+ case 24:
+ num_rounds = 12;
+ break;
+ case 32:
+ num_rounds = 14;
+ break;
+ default:
+ /* abort(); */
+ return 0;
+ }
+ nk = (int)(key_len >> 2);
+ nkf = (int)((num_rounds + 1) << 2);
+ tmp = 0;
+ for (i = 0; i < nk; i ++) {
+ tmp = br_dec32le((const unsigned char *)key + (i << 2));
+ skey[(i << 1) + 0] = tmp;
+ skey[(i << 1) + 1] = tmp;
+ }
+ for (i = nk, j = 0, k = 0; i < nkf; i ++) {
+ if (j == 0) {
+ tmp = (tmp << 24) | (tmp >> 8);
+ tmp = sub_word(tmp) ^ Rcon[k];
+ } else if (nk > 6 && j == 4) {
+ tmp = sub_word(tmp);
+ }
+ tmp ^= skey[(i - nk) << 1];
+ skey[(i << 1) + 0] = tmp;
+ skey[(i << 1) + 1] = tmp;
+ if (++ j == nk) {
+ j = 0;
+ k ++;
+ }
+ }
+ for (i = 0; i < nkf; i += 4) {
+ br_aes_ct_ortho(skey + (i << 1));
+ }
+ for (i = 0, j = 0; i < nkf; i ++, j += 2) {
+ comp_skey[i] = (skey[j + 0] & 0x55555555)
+ | (skey[j + 1] & 0xAAAAAAAA);
+ }
+ return num_rounds;
+}
+
+/* see inner.h */
+void
+br_aes_ct_skey_expand(uint32_t *skey,
+ unsigned num_rounds, const uint32_t *comp_skey)
+{
+ unsigned u, v, n;
+
+ n = (num_rounds + 1) << 2;
+ for (u = 0, v = 0; u < n; u ++, v += 2) {
+ uint32_t x, y;
+
+ x = y = comp_skey[u];
+ x &= 0x55555555;
+ skey[v + 0] = x | (x << 1);
+ y &= 0xAAAAAAAA;
+ skey[v + 1] = y | (y >> 1);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64.c
new file mode 100644
index 00000000..15238116
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64.c
@@ -0,0 +1,398 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_aes_ct64_bitslice_Sbox(uint64_t *q)
+{
+ /*
+ * This S-box implementation is a straightforward translation of
+ * the circuit described by Boyar and Peralta in "A new
+ * combinational logic minimization technique with applications
+ * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
+ *
+ * Note that variables x* (input) and s* (output) are numbered
+ * in "reverse" order (x0 is the high bit, x7 is the low bit).
+ */
+
+ uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
+ uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
+ uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
+ uint64_t y20, y21;
+ uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
+ uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
+ uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
+ uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
+ uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
+ uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
+ uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
+ uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
+ uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
+ uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
+
+ x0 = q[7];
+ x1 = q[6];
+ x2 = q[5];
+ x3 = q[4];
+ x4 = q[3];
+ x5 = q[2];
+ x6 = q[1];
+ x7 = q[0];
+
+ /*
+ * Top linear transformation.
+ */
+ y14 = x3 ^ x5;
+ y13 = x0 ^ x6;
+ y9 = x0 ^ x3;
+ y8 = x0 ^ x5;
+ t0 = x1 ^ x2;
+ y1 = t0 ^ x7;
+ y4 = y1 ^ x3;
+ y12 = y13 ^ y14;
+ y2 = y1 ^ x0;
+ y5 = y1 ^ x6;
+ y3 = y5 ^ y8;
+ t1 = x4 ^ y12;
+ y15 = t1 ^ x5;
+ y20 = t1 ^ x1;
+ y6 = y15 ^ x7;
+ y10 = y15 ^ t0;
+ y11 = y20 ^ y9;
+ y7 = x7 ^ y11;
+ y17 = y10 ^ y11;
+ y19 = y10 ^ y8;
+ y16 = t0 ^ y11;
+ y21 = y13 ^ y16;
+ y18 = x0 ^ y16;
+
+ /*
+ * Non-linear section.
+ */
+ t2 = y12 & y15;
+ t3 = y3 & y6;
+ t4 = t3 ^ t2;
+ t5 = y4 & x7;
+ t6 = t5 ^ t2;
+ t7 = y13 & y16;
+ t8 = y5 & y1;
+ t9 = t8 ^ t7;
+ t10 = y2 & y7;
+ t11 = t10 ^ t7;
+ t12 = y9 & y11;
+ t13 = y14 & y17;
+ t14 = t13 ^ t12;
+ t15 = y8 & y10;
+ t16 = t15 ^ t12;
+ t17 = t4 ^ t14;
+ t18 = t6 ^ t16;
+ t19 = t9 ^ t14;
+ t20 = t11 ^ t16;
+ t21 = t17 ^ y20;
+ t22 = t18 ^ y19;
+ t23 = t19 ^ y21;
+ t24 = t20 ^ y18;
+
+ t25 = t21 ^ t22;
+ t26 = t21 & t23;
+ t27 = t24 ^ t26;
+ t28 = t25 & t27;
+ t29 = t28 ^ t22;
+ t30 = t23 ^ t24;
+ t31 = t22 ^ t26;
+ t32 = t31 & t30;
+ t33 = t32 ^ t24;
+ t34 = t23 ^ t33;
+ t35 = t27 ^ t33;
+ t36 = t24 & t35;
+ t37 = t36 ^ t34;
+ t38 = t27 ^ t36;
+ t39 = t29 & t38;
+ t40 = t25 ^ t39;
+
+ t41 = t40 ^ t37;
+ t42 = t29 ^ t33;
+ t43 = t29 ^ t40;
+ t44 = t33 ^ t37;
+ t45 = t42 ^ t41;
+ z0 = t44 & y15;
+ z1 = t37 & y6;
+ z2 = t33 & x7;
+ z3 = t43 & y16;
+ z4 = t40 & y1;
+ z5 = t29 & y7;
+ z6 = t42 & y11;
+ z7 = t45 & y17;
+ z8 = t41 & y10;
+ z9 = t44 & y12;
+ z10 = t37 & y3;
+ z11 = t33 & y4;
+ z12 = t43 & y13;
+ z13 = t40 & y5;
+ z14 = t29 & y2;
+ z15 = t42 & y9;
+ z16 = t45 & y14;
+ z17 = t41 & y8;
+
+ /*
+ * Bottom linear transformation.
+ */
+ t46 = z15 ^ z16;
+ t47 = z10 ^ z11;
+ t48 = z5 ^ z13;
+ t49 = z9 ^ z10;
+ t50 = z2 ^ z12;
+ t51 = z2 ^ z5;
+ t52 = z7 ^ z8;
+ t53 = z0 ^ z3;
+ t54 = z6 ^ z7;
+ t55 = z16 ^ z17;
+ t56 = z12 ^ t48;
+ t57 = t50 ^ t53;
+ t58 = z4 ^ t46;
+ t59 = z3 ^ t54;
+ t60 = t46 ^ t57;
+ t61 = z14 ^ t57;
+ t62 = t52 ^ t58;
+ t63 = t49 ^ t58;
+ t64 = z4 ^ t59;
+ t65 = t61 ^ t62;
+ t66 = z1 ^ t63;
+ s0 = t59 ^ t63;
+ s6 = t56 ^ ~t62;
+ s7 = t48 ^ ~t60;
+ t67 = t64 ^ t65;
+ s3 = t53 ^ t66;
+ s4 = t51 ^ t66;
+ s5 = t47 ^ t65;
+ s1 = t64 ^ ~s3;
+ s2 = t55 ^ ~t67;
+
+ q[7] = s0;
+ q[6] = s1;
+ q[5] = s2;
+ q[4] = s3;
+ q[3] = s4;
+ q[2] = s5;
+ q[1] = s6;
+ q[0] = s7;
+}
+
+/* see inner.h */
+void
+br_aes_ct64_ortho(uint64_t *q)
+{
+#define SWAPN(cl, ch, s, x, y) do { \
+ uint64_t a, b; \
+ a = (x); \
+ b = (y); \
+ (x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \
+ (y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \
+ } while (0)
+
+#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)
+#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)
+#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y)
+
+ SWAP2(q[0], q[1]);
+ SWAP2(q[2], q[3]);
+ SWAP2(q[4], q[5]);
+ SWAP2(q[6], q[7]);
+
+ SWAP4(q[0], q[2]);
+ SWAP4(q[1], q[3]);
+ SWAP4(q[4], q[6]);
+ SWAP4(q[5], q[7]);
+
+ SWAP8(q[0], q[4]);
+ SWAP8(q[1], q[5]);
+ SWAP8(q[2], q[6]);
+ SWAP8(q[3], q[7]);
+}
+
+/* see inner.h */
+void
+br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w)
+{
+ uint64_t x0, x1, x2, x3;
+
+ x0 = w[0];
+ x1 = w[1];
+ x2 = w[2];
+ x3 = w[3];
+ x0 |= (x0 << 16);
+ x1 |= (x1 << 16);
+ x2 |= (x2 << 16);
+ x3 |= (x3 << 16);
+ x0 &= (uint64_t)0x0000FFFF0000FFFF;
+ x1 &= (uint64_t)0x0000FFFF0000FFFF;
+ x2 &= (uint64_t)0x0000FFFF0000FFFF;
+ x3 &= (uint64_t)0x0000FFFF0000FFFF;
+ x0 |= (x0 << 8);
+ x1 |= (x1 << 8);
+ x2 |= (x2 << 8);
+ x3 |= (x3 << 8);
+ x0 &= (uint64_t)0x00FF00FF00FF00FF;
+ x1 &= (uint64_t)0x00FF00FF00FF00FF;
+ x2 &= (uint64_t)0x00FF00FF00FF00FF;
+ x3 &= (uint64_t)0x00FF00FF00FF00FF;
+ *q0 = x0 | (x2 << 8);
+ *q1 = x1 | (x3 << 8);
+}
+
+/* see inner.h */
+void
+br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1)
+{
+ uint64_t x0, x1, x2, x3;
+
+ x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
+ x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
+ x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
+ x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
+ x0 |= (x0 >> 8);
+ x1 |= (x1 >> 8);
+ x2 |= (x2 >> 8);
+ x3 |= (x3 >> 8);
+ x0 &= (uint64_t)0x0000FFFF0000FFFF;
+ x1 &= (uint64_t)0x0000FFFF0000FFFF;
+ x2 &= (uint64_t)0x0000FFFF0000FFFF;
+ x3 &= (uint64_t)0x0000FFFF0000FFFF;
+ w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
+ w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
+ w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
+ w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
+}
+
+static const unsigned char Rcon[] = {
+ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
+};
+
+static uint32_t
+sub_word(uint32_t x)
+{
+ uint64_t q[8];
+
+ memset(q, 0, sizeof q);
+ q[0] = x;
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_Sbox(q);
+ br_aes_ct64_ortho(q);
+ return (uint32_t)q[0];
+}
+
+/* see inner.h */
+unsigned
+br_aes_ct64_keysched(uint64_t *comp_skey, const void *key, size_t key_len)
+{
+ unsigned num_rounds;
+ int i, j, k, nk, nkf;
+ uint32_t tmp;
+ uint32_t skey[60];
+
+ switch (key_len) {
+ case 16:
+ num_rounds = 10;
+ break;
+ case 24:
+ num_rounds = 12;
+ break;
+ case 32:
+ num_rounds = 14;
+ break;
+ default:
+ /* abort(); */
+ return 0;
+ }
+ nk = (int)(key_len >> 2);
+ nkf = (int)((num_rounds + 1) << 2);
+ br_range_dec32le(skey, (key_len >> 2), key);
+ tmp = skey[(key_len >> 2) - 1];
+ for (i = nk, j = 0, k = 0; i < nkf; i ++) {
+ if (j == 0) {
+ tmp = (tmp << 24) | (tmp >> 8);
+ tmp = sub_word(tmp) ^ Rcon[k];
+ } else if (nk > 6 && j == 4) {
+ tmp = sub_word(tmp);
+ }
+ tmp ^= skey[i - nk];
+ skey[i] = tmp;
+ if (++ j == nk) {
+ j = 0;
+ k ++;
+ }
+ }
+
+ for (i = 0, j = 0; i < nkf; i += 4, j += 2) {
+ uint64_t q[8];
+
+ br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);
+ q[1] = q[0];
+ q[2] = q[0];
+ q[3] = q[0];
+ q[5] = q[4];
+ q[6] = q[4];
+ q[7] = q[4];
+ br_aes_ct64_ortho(q);
+ comp_skey[j + 0] =
+ (q[0] & (uint64_t)0x1111111111111111)
+ | (q[1] & (uint64_t)0x2222222222222222)
+ | (q[2] & (uint64_t)0x4444444444444444)
+ | (q[3] & (uint64_t)0x8888888888888888);
+ comp_skey[j + 1] =
+ (q[4] & (uint64_t)0x1111111111111111)
+ | (q[5] & (uint64_t)0x2222222222222222)
+ | (q[6] & (uint64_t)0x4444444444444444)
+ | (q[7] & (uint64_t)0x8888888888888888);
+ }
+ return num_rounds;
+}
+
+/* see inner.h */
+void
+br_aes_ct64_skey_expand(uint64_t *skey,
+ unsigned num_rounds, const uint64_t *comp_skey)
+{
+ unsigned u, v, n;
+
+ n = (num_rounds + 1) << 1;
+ for (u = 0, v = 0; u < n; u ++, v += 4) {
+ uint64_t x0, x1, x2, x3;
+
+ x0 = x1 = x2 = x3 = comp_skey[u];
+ x0 &= (uint64_t)0x1111111111111111;
+ x1 &= (uint64_t)0x2222222222222222;
+ x2 &= (uint64_t)0x4444444444444444;
+ x3 &= (uint64_t)0x8888888888888888;
+ x1 >>= 1;
+ x2 >>= 2;
+ x3 >>= 3;
+ skey[v + 0] = (x0 << 4) - x0;
+ skey[v + 1] = (x1 << 4) - x1;
+ skey[v + 2] = (x2 << 4) - x2;
+ skey[v + 3] = (x3 << 4) - x3;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcdec.c
new file mode 100644
index 00000000..5a7360bc
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcdec.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct64_cbcdec_vtable;
+ ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf;
+ uint64_t sk_exp[120];
+ uint32_t ivw[4];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ br_range_dec32le(ivw, 4, iv);
+ buf = data;
+ while (len > 0) {
+ uint64_t q[8];
+ uint32_t w1[16], w2[16];
+ int i;
+
+ if (len >= 64) {
+ br_range_dec32le(w1, 16, buf);
+ } else {
+ br_range_dec32le(w1, len >> 2, buf);
+ }
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_in(
+ &q[i], &q[i + 4], w1 + (i << 2));
+ }
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_out(
+ w2 + (i << 2), q[i], q[i + 4]);
+ }
+ for (i = 0; i < 4; i ++) {
+ w2[i] ^= ivw[i];
+ }
+ if (len >= 64) {
+ for (i = 4; i < 16; i ++) {
+ w2[i] ^= w1[i - 4];
+ }
+ memcpy(ivw, w1 + 12, sizeof ivw);
+ br_range_enc32le(buf, w2, 16);
+ } else {
+ int j;
+
+ j = (int)(len >> 2);
+ for (i = 4; i < j; i ++) {
+ w2[i] ^= w1[i - 4];
+ }
+ memcpy(ivw, w1 + j - 4, sizeof ivw);
+ br_range_enc32le(buf, w2, j);
+ break;
+ }
+ buf += 64;
+ len -= 64;
+ }
+ br_range_enc32le(iv, ivw, 4);
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable = {
+ sizeof(br_aes_ct64_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_ct64_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_ct64_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcenc.c
new file mode 100644
index 00000000..6cb9dece
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_cbcenc.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct64_cbcenc_vtable;
+ ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf;
+ uint64_t sk_exp[120];
+ uint32_t ivw[4];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ br_range_dec32le(ivw, 4, iv);
+ buf = data;
+ while (len > 0) {
+ uint32_t w[4];
+ uint64_t q[8];
+
+ w[0] = ivw[0] ^ br_dec32le(buf);
+ w[1] = ivw[1] ^ br_dec32le(buf + 4);
+ w[2] = ivw[2] ^ br_dec32le(buf + 8);
+ w[3] = ivw[3] ^ br_dec32le(buf + 12);
+ br_aes_ct64_interleave_in(&q[0], &q[4], w);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_interleave_out(w, q[0], q[4]);
+ memcpy(ivw, w, sizeof w);
+ br_enc32le(buf, w[0]);
+ br_enc32le(buf + 4, w[1]);
+ br_enc32le(buf + 8, w[2]);
+ br_enc32le(buf + 12, w[3]);
+ buf += 16;
+ len -= 16;
+ }
+ br_range_enc32le(iv, ivw, 4);
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable = {
+ sizeof(br_aes_ct64_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_ct64_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_ct64_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctr.c
new file mode 100644
index 00000000..1275873d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctr.c
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct64_ctr_vtable;
+ ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ uint32_t ivw[16];
+ uint64_t sk_exp[120];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ br_range_dec32le(ivw, 3, iv);
+ memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t));
+ memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t));
+ memcpy(ivw + 12, ivw, 3 * sizeof(uint32_t));
+ buf = data;
+ while (len > 0) {
+ uint64_t q[8];
+ uint32_t w[16];
+ unsigned char tmp[64];
+ int i;
+
+ /*
+ * TODO: see if we can save on the first br_aes_ct64_ortho()
+ * call, since iv0/iv1/iv2 are constant for the whole run.
+ */
+ memcpy(w, ivw, sizeof ivw);
+ w[3] = br_swap32(cc);
+ w[7] = br_swap32(cc + 1);
+ w[11] = br_swap32(cc + 2);
+ w[15] = br_swap32(cc + 3);
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_in(
+ &q[i], &q[i + 4], w + (i << 2));
+ }
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_out(
+ w + (i << 2), q[i], q[i + 4]);
+ }
+ br_range_enc32le(tmp, w, 16);
+ if (len <= 64) {
+ xorbuf(buf, tmp, len);
+ cc += (uint32_t)len >> 4;
+ break;
+ }
+ xorbuf(buf, tmp, 64);
+ buf += 64;
+ len -= 64;
+ cc += 4;
+ }
+ return cc;
+}
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_ct64_ctr_vtable = {
+ sizeof(br_aes_ct64_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_ct64_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_ct64_ctr_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctrcbc.c
new file mode 100644
index 00000000..21bb8efa
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_ctrcbc.c
@@ -0,0 +1,433 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct64_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint64_t sk_exp[120];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ buf = data;
+ while (len > 0) {
+ uint64_t q[8];
+ uint32_t w[16];
+ unsigned char tmp[64];
+ int i, j;
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ j = (len >= 64) ? 16 : (int)(len >> 2);
+ for (i = 0; i < j; i += 4) {
+ uint32_t carry;
+
+ w[i + 0] = br_swap32(iv0);
+ w[i + 1] = br_swap32(iv1);
+ w[i + 2] = br_swap32(iv2);
+ w[i + 3] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+ }
+ memset(w + i, 0, (16 - i) * sizeof(uint32_t));
+
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_in(
+ &q[i], &q[i + 4], w + (i << 2));
+ }
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ for (i = 0; i < 4; i ++) {
+ br_aes_ct64_interleave_out(
+ w + (i << 2), q[i], q[i + 4]);
+ }
+
+ br_range_enc32le(tmp, w, 16);
+ if (len <= 64) {
+ xorbuf(buf, tmp, len);
+ break;
+ }
+ xorbuf(buf, tmp, 64);
+ buf += 64;
+ len -= 64;
+ }
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint64_t q[8];
+ uint64_t sk_exp[120];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+
+ buf = data;
+ memset(q, 0, sizeof q);
+ while (len > 0) {
+ uint32_t w[4];
+
+ w[0] = cm0 ^ br_dec32le(buf + 0);
+ w[1] = cm1 ^ br_dec32le(buf + 4);
+ w[2] = cm2 ^ br_dec32le(buf + 8);
+ w[3] = cm3 ^ br_dec32le(buf + 12);
+
+ br_aes_ct64_interleave_in(&q[0], &q[4], w);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_interleave_out(w, q[0], q[4]);
+
+ cm0 = w[0];
+ cm1 = w[1];
+ cm2 = w[2];
+ cm3 = w[3];
+ buf += 16;
+ len -= 16;
+ }
+
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ /*
+ * When encrypting, the CBC-MAC processing must be lagging by
+ * one block, since it operates on the encrypted values, so
+ * it must wait for that encryption to complete.
+ */
+
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint64_t sk_exp[120];
+ uint64_t q[8];
+ int first_iter;
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ /*
+ * The current CBC-MAC value is kept in little-endian convention.
+ */
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+
+ buf = data;
+ first_iter = 1;
+ memset(q, 0, sizeof q);
+ while (len > 0) {
+ uint32_t w[8], carry;
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ w[0] = br_swap32(iv0);
+ w[1] = br_swap32(iv1);
+ w[2] = br_swap32(iv2);
+ w[3] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+
+ /*
+ * The block for CBC-MAC.
+ */
+ w[4] = cm0;
+ w[5] = cm1;
+ w[6] = cm2;
+ w[7] = cm3;
+
+ br_aes_ct64_interleave_in(&q[0], &q[4], w);
+ br_aes_ct64_interleave_in(&q[1], &q[5], w + 4);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_interleave_out(w, q[0], q[4]);
+ br_aes_ct64_interleave_out(w + 4, q[1], q[5]);
+
+ /*
+ * We do the XOR with the plaintext in 32-bit registers,
+ * so that the value are available for CBC-MAC processing
+ * as well.
+ */
+ w[0] ^= br_dec32le(buf + 0);
+ w[1] ^= br_dec32le(buf + 4);
+ w[2] ^= br_dec32le(buf + 8);
+ w[3] ^= br_dec32le(buf + 12);
+ br_enc32le(buf + 0, w[0]);
+ br_enc32le(buf + 4, w[1]);
+ br_enc32le(buf + 8, w[2]);
+ br_enc32le(buf + 12, w[3]);
+
+ buf += 16;
+ len -= 16;
+
+ /*
+ * We set the cm* values to the block to encrypt in the
+ * next iteration.
+ */
+ if (first_iter) {
+ first_iter = 0;
+ cm0 ^= w[0];
+ cm1 ^= w[1];
+ cm2 ^= w[2];
+ cm3 ^= w[3];
+ } else {
+ cm0 = w[0] ^ w[4];
+ cm1 = w[1] ^ w[5];
+ cm2 = w[2] ^ w[6];
+ cm3 = w[3] ^ w[7];
+ }
+
+ /*
+ * If this was the last iteration, then compute the
+ * extra block encryption to complete CBC-MAC.
+ */
+ if (len == 0) {
+ w[0] = cm0;
+ w[1] = cm1;
+ w[2] = cm2;
+ w[3] = cm3;
+ br_aes_ct64_interleave_in(&q[0], &q[4], w);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(
+ ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_interleave_out(w, q[0], q[4]);
+ cm0 = w[0];
+ cm1 = w[1];
+ cm2 = w[2];
+ cm3 = w[3];
+ break;
+ }
+ }
+
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint64_t sk_exp[120];
+ uint64_t q[8];
+
+ br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ /*
+ * The current CBC-MAC value is kept in little-endian convention.
+ */
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+
+ buf = data;
+ memset(q, 0, sizeof q);
+ while (len > 0) {
+ uint32_t w[8], carry;
+ unsigned char tmp[16];
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ w[0] = br_swap32(iv0);
+ w[1] = br_swap32(iv1);
+ w[2] = br_swap32(iv2);
+ w[3] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+
+ /*
+ * The block for CBC-MAC.
+ */
+ w[4] = cm0 ^ br_dec32le(buf + 0);
+ w[5] = cm1 ^ br_dec32le(buf + 4);
+ w[6] = cm2 ^ br_dec32le(buf + 8);
+ w[7] = cm3 ^ br_dec32le(buf + 12);
+
+ br_aes_ct64_interleave_in(&q[0], &q[4], w);
+ br_aes_ct64_interleave_in(&q[1], &q[5], w + 4);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct64_ortho(q);
+ br_aes_ct64_interleave_out(w, q[0], q[4]);
+ br_aes_ct64_interleave_out(w + 4, q[1], q[5]);
+
+ br_enc32le(tmp + 0, w[0]);
+ br_enc32le(tmp + 4, w[1]);
+ br_enc32le(tmp + 8, w[2]);
+ br_enc32le(tmp + 12, w[3]);
+ xorbuf(buf, tmp, 16);
+ cm0 = w[4];
+ cm1 = w[5];
+ cm2 = w[6];
+ cm3 = w[7];
+ buf += 16;
+ len -= 16;
+ }
+
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable = {
+ sizeof(br_aes_ct64_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_ct64_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_ct64_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_ct64_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_ct64_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_ct64_ctrcbc_mac
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_dec.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_dec.c
new file mode 100644
index 00000000..ab00e099
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_dec.c
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_aes_ct64_bitslice_invSbox(uint64_t *q)
+{
+ /*
+ * See br_aes_ct_bitslice_invSbox(). This is the natural extension
+ * to 64-bit registers.
+ */
+ uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
+
+ q0 = ~q[0];
+ q1 = ~q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = ~q[5];
+ q6 = ~q[6];
+ q7 = q[7];
+ q[7] = q1 ^ q4 ^ q6;
+ q[6] = q0 ^ q3 ^ q5;
+ q[5] = q7 ^ q2 ^ q4;
+ q[4] = q6 ^ q1 ^ q3;
+ q[3] = q5 ^ q0 ^ q2;
+ q[2] = q4 ^ q7 ^ q1;
+ q[1] = q3 ^ q6 ^ q0;
+ q[0] = q2 ^ q5 ^ q7;
+
+ br_aes_ct64_bitslice_Sbox(q);
+
+ q0 = ~q[0];
+ q1 = ~q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = ~q[5];
+ q6 = ~q[6];
+ q7 = q[7];
+ q[7] = q1 ^ q4 ^ q6;
+ q[6] = q0 ^ q3 ^ q5;
+ q[5] = q7 ^ q2 ^ q4;
+ q[4] = q6 ^ q1 ^ q3;
+ q[3] = q5 ^ q0 ^ q2;
+ q[2] = q4 ^ q7 ^ q1;
+ q[1] = q3 ^ q6 ^ q0;
+ q[0] = q2 ^ q5 ^ q7;
+}
+
+static void
+add_round_key(uint64_t *q, const uint64_t *sk)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ q[i] ^= sk[i];
+ }
+}
+
+static void
+inv_shift_rows(uint64_t *q)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ uint64_t x;
+
+ x = q[i];
+ q[i] = (x & (uint64_t)0x000000000000FFFF)
+ | ((x & (uint64_t)0x000000000FFF0000) << 4)
+ | ((x & (uint64_t)0x00000000F0000000) >> 12)
+ | ((x & (uint64_t)0x000000FF00000000) << 8)
+ | ((x & (uint64_t)0x0000FF0000000000) >> 8)
+ | ((x & (uint64_t)0x000F000000000000) << 12)
+ | ((x & (uint64_t)0xFFF0000000000000) >> 4);
+ }
+}
+
+static inline uint64_t
+rotr32(uint64_t x)
+{
+ return (x << 32) | (x >> 32);
+}
+
+static void
+inv_mix_columns(uint64_t *q)
+{
+ uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
+ uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
+
+ q0 = q[0];
+ q1 = q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = q[5];
+ q6 = q[6];
+ q7 = q[7];
+ r0 = (q0 >> 16) | (q0 << 48);
+ r1 = (q1 >> 16) | (q1 << 48);
+ r2 = (q2 >> 16) | (q2 << 48);
+ r3 = (q3 >> 16) | (q3 << 48);
+ r4 = (q4 >> 16) | (q4 << 48);
+ r5 = (q5 >> 16) | (q5 << 48);
+ r6 = (q6 >> 16) | (q6 << 48);
+ r7 = (q7 >> 16) | (q7 << 48);
+
+ q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr32(q0 ^ q5 ^ q6 ^ r0 ^ r5);
+ q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
+ q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr32(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
+ q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr32(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
+ q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
+ q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
+ q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr32(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
+ q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr32(q4 ^ q5 ^ q7 ^ r4 ^ r7);
+}
+
+/* see inner.h */
+void
+br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
+ const uint64_t *skey, uint64_t *q)
+{
+ unsigned u;
+
+ add_round_key(q, skey + (num_rounds << 3));
+ for (u = num_rounds - 1; u > 0; u --) {
+ inv_shift_rows(q);
+ br_aes_ct64_bitslice_invSbox(q);
+ add_round_key(q, skey + (u << 3));
+ inv_mix_columns(q);
+ }
+ inv_shift_rows(q);
+ br_aes_ct64_bitslice_invSbox(q);
+ add_round_key(q, skey);
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct64_enc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct64_enc.c
new file mode 100644
index 00000000..78631ced
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct64_enc.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static inline void
+add_round_key(uint64_t *q, const uint64_t *sk)
+{
+ q[0] ^= sk[0];
+ q[1] ^= sk[1];
+ q[2] ^= sk[2];
+ q[3] ^= sk[3];
+ q[4] ^= sk[4];
+ q[5] ^= sk[5];
+ q[6] ^= sk[6];
+ q[7] ^= sk[7];
+}
+
+static inline void
+shift_rows(uint64_t *q)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ uint64_t x;
+
+ x = q[i];
+ q[i] = (x & (uint64_t)0x000000000000FFFF)
+ | ((x & (uint64_t)0x00000000FFF00000) >> 4)
+ | ((x & (uint64_t)0x00000000000F0000) << 12)
+ | ((x & (uint64_t)0x0000FF0000000000) >> 8)
+ | ((x & (uint64_t)0x000000FF00000000) << 8)
+ | ((x & (uint64_t)0xF000000000000000) >> 12)
+ | ((x & (uint64_t)0x0FFF000000000000) << 4);
+ }
+}
+
+static inline uint64_t
+rotr32(uint64_t x)
+{
+ return (x << 32) | (x >> 32);
+}
+
+static inline void
+mix_columns(uint64_t *q)
+{
+ uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
+ uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
+
+ q0 = q[0];
+ q1 = q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = q[5];
+ q6 = q[6];
+ q7 = q[7];
+ r0 = (q0 >> 16) | (q0 << 48);
+ r1 = (q1 >> 16) | (q1 << 48);
+ r2 = (q2 >> 16) | (q2 << 48);
+ r3 = (q3 >> 16) | (q3 << 48);
+ r4 = (q4 >> 16) | (q4 << 48);
+ r5 = (q5 >> 16) | (q5 << 48);
+ r6 = (q6 >> 16) | (q6 << 48);
+ r7 = (q7 >> 16) | (q7 << 48);
+
+ q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
+ q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
+ q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
+ q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
+ q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
+ q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
+ q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
+ q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
+}
+
+/* see inner.h */
+void
+br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
+ const uint64_t *skey, uint64_t *q)
+{
+ unsigned u;
+
+ add_round_key(q, skey);
+ for (u = 1; u < num_rounds; u ++) {
+ br_aes_ct64_bitslice_Sbox(q);
+ shift_rows(q);
+ mix_columns(q);
+ add_round_key(q, skey + (u << 3));
+ }
+ br_aes_ct64_bitslice_Sbox(q);
+ shift_rows(q);
+ add_round_key(q, skey + (num_rounds << 3));
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcdec.c
new file mode 100644
index 00000000..522645ad
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcdec.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct_cbcdec_vtable;
+ ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t sk_exp[120];
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ ivbuf = iv;
+ iv0 = br_dec32le(ivbuf);
+ iv1 = br_dec32le(ivbuf + 4);
+ iv2 = br_dec32le(ivbuf + 8);
+ iv3 = br_dec32le(ivbuf + 12);
+ buf = data;
+ while (len > 0) {
+ uint32_t q[8], sq[8];
+
+ q[0] = br_dec32le(buf);
+ q[2] = br_dec32le(buf + 4);
+ q[4] = br_dec32le(buf + 8);
+ q[6] = br_dec32le(buf + 12);
+ if (len >= 32) {
+ q[1] = br_dec32le(buf + 16);
+ q[3] = br_dec32le(buf + 20);
+ q[5] = br_dec32le(buf + 24);
+ q[7] = br_dec32le(buf + 28);
+ } else {
+ q[1] = 0;
+ q[3] = 0;
+ q[5] = 0;
+ q[7] = 0;
+ }
+ memcpy(sq, q, sizeof q);
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+ br_enc32le(buf, q[0] ^ iv0);
+ br_enc32le(buf + 4, q[2] ^ iv1);
+ br_enc32le(buf + 8, q[4] ^ iv2);
+ br_enc32le(buf + 12, q[6] ^ iv3);
+ if (len < 32) {
+ iv0 = sq[0];
+ iv1 = sq[2];
+ iv2 = sq[4];
+ iv3 = sq[6];
+ break;
+ }
+ br_enc32le(buf + 16, q[1] ^ sq[0]);
+ br_enc32le(buf + 20, q[3] ^ sq[2]);
+ br_enc32le(buf + 24, q[5] ^ sq[4]);
+ br_enc32le(buf + 28, q[7] ^ sq[6]);
+ iv0 = sq[1];
+ iv1 = sq[3];
+ iv2 = sq[5];
+ iv3 = sq[7];
+ buf += 32;
+ len -= 32;
+ }
+ br_enc32le(ivbuf, iv0);
+ br_enc32le(ivbuf + 4, iv1);
+ br_enc32le(ivbuf + 8, iv2);
+ br_enc32le(ivbuf + 12, iv3);
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_ct_cbcdec_vtable = {
+ sizeof(br_aes_ct_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_ct_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_ct_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcenc.c
new file mode 100644
index 00000000..cb85977b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_cbcenc.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct_cbcenc_vtable;
+ ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+ uint32_t q[8];
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t sk_exp[120];
+
+ q[1] = 0;
+ q[3] = 0;
+ q[5] = 0;
+ q[7] = 0;
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ ivbuf = iv;
+ iv0 = br_dec32le(ivbuf);
+ iv1 = br_dec32le(ivbuf + 4);
+ iv2 = br_dec32le(ivbuf + 8);
+ iv3 = br_dec32le(ivbuf + 12);
+ buf = data;
+ while (len > 0) {
+ q[0] = iv0 ^ br_dec32le(buf);
+ q[2] = iv1 ^ br_dec32le(buf + 4);
+ q[4] = iv2 ^ br_dec32le(buf + 8);
+ q[6] = iv3 ^ br_dec32le(buf + 12);
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+ iv0 = q[0];
+ iv1 = q[2];
+ iv2 = q[4];
+ iv3 = q[6];
+ br_enc32le(buf, iv0);
+ br_enc32le(buf + 4, iv1);
+ br_enc32le(buf + 8, iv2);
+ br_enc32le(buf + 12, iv3);
+ buf += 16;
+ len -= 16;
+ }
+ br_enc32le(ivbuf, iv0);
+ br_enc32le(ivbuf + 4, iv1);
+ br_enc32le(ivbuf + 8, iv2);
+ br_enc32le(ivbuf + 12, iv3);
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_ct_cbcenc_vtable = {
+ sizeof(br_aes_ct_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_ct_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_ct_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_ctr.c
new file mode 100644
index 00000000..f407689e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_ctr.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct_ctr_vtable;
+ ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ const unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2;
+ uint32_t sk_exp[120];
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ ivbuf = iv;
+ iv0 = br_dec32le(ivbuf);
+ iv1 = br_dec32le(ivbuf + 4);
+ iv2 = br_dec32le(ivbuf + 8);
+ buf = data;
+ while (len > 0) {
+ uint32_t q[8];
+ unsigned char tmp[32];
+
+ /*
+ * TODO: see if we can save on the first br_aes_ct_ortho()
+ * call, since iv0/iv1/iv2 are constant for the whole run.
+ */
+ q[0] = q[1] = iv0;
+ q[2] = q[3] = iv1;
+ q[4] = q[5] = iv2;
+ q[6] = br_swap32(cc);
+ q[7] = br_swap32(cc + 1);
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+ br_enc32le(tmp, q[0]);
+ br_enc32le(tmp + 4, q[2]);
+ br_enc32le(tmp + 8, q[4]);
+ br_enc32le(tmp + 12, q[6]);
+ br_enc32le(tmp + 16, q[1]);
+ br_enc32le(tmp + 20, q[3]);
+ br_enc32le(tmp + 24, q[5]);
+ br_enc32le(tmp + 28, q[7]);
+
+ if (len <= 32) {
+ xorbuf(buf, tmp, len);
+ cc ++;
+ if (len > 16) {
+ cc ++;
+ }
+ break;
+ }
+ xorbuf(buf, tmp, 32);
+ buf += 32;
+ len -= 32;
+ cc += 2;
+ }
+ return cc;
+}
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_ct_ctr_vtable = {
+ sizeof(br_aes_ct_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_ct_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_ct_ctr_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_ctrcbc.c
new file mode 100644
index 00000000..8ae9fc75
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_ctrcbc.c
@@ -0,0 +1,422 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_ct_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t sk_exp[120];
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ buf = data;
+ while (len > 0) {
+ uint32_t q[8], carry;
+ unsigned char tmp[32];
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ q[0] = br_swap32(iv0);
+ q[2] = br_swap32(iv1);
+ q[4] = br_swap32(iv2);
+ q[6] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+ q[1] = br_swap32(iv0);
+ q[3] = br_swap32(iv1);
+ q[5] = br_swap32(iv2);
+ q[7] = br_swap32(iv3);
+ if (len > 16) {
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+ }
+
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+
+ br_enc32le(tmp, q[0]);
+ br_enc32le(tmp + 4, q[2]);
+ br_enc32le(tmp + 8, q[4]);
+ br_enc32le(tmp + 12, q[6]);
+ br_enc32le(tmp + 16, q[1]);
+ br_enc32le(tmp + 20, q[3]);
+ br_enc32le(tmp + 24, q[5]);
+ br_enc32le(tmp + 28, q[7]);
+
+ if (len <= 32) {
+ xorbuf(buf, tmp, len);
+ break;
+ }
+ xorbuf(buf, tmp, 32);
+ buf += 32;
+ len -= 32;
+ }
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint32_t q[8];
+ uint32_t sk_exp[120];
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ buf = data;
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+ q[1] = 0;
+ q[3] = 0;
+ q[5] = 0;
+ q[7] = 0;
+
+ while (len > 0) {
+ q[0] = cm0 ^ br_dec32le(buf + 0);
+ q[2] = cm1 ^ br_dec32le(buf + 4);
+ q[4] = cm2 ^ br_dec32le(buf + 8);
+ q[6] = cm3 ^ br_dec32le(buf + 12);
+
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+
+ cm0 = q[0];
+ cm1 = q[2];
+ cm2 = q[4];
+ cm3 = q[6];
+ buf += 16;
+ len -= 16;
+ }
+
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ /*
+ * When encrypting, the CBC-MAC processing must be lagging by
+ * one block, since it operates on the encrypted values, so
+ * it must wait for that encryption to complete.
+ */
+
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint32_t sk_exp[120];
+ int first_iter;
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ /*
+ * The current CBC-MAC value is kept in little-endian convention.
+ */
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+
+ buf = data;
+ first_iter = 1;
+ while (len > 0) {
+ uint32_t q[8], carry;
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ q[0] = br_swap32(iv0);
+ q[2] = br_swap32(iv1);
+ q[4] = br_swap32(iv2);
+ q[6] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+
+ /*
+ * The odd values are used for CBC-MAC.
+ */
+ q[1] = cm0;
+ q[3] = cm1;
+ q[5] = cm2;
+ q[7] = cm3;
+
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+
+ /*
+ * We do the XOR with the plaintext in 32-bit registers,
+ * so that the value are available for CBC-MAC processing
+ * as well.
+ */
+ q[0] ^= br_dec32le(buf + 0);
+ q[2] ^= br_dec32le(buf + 4);
+ q[4] ^= br_dec32le(buf + 8);
+ q[6] ^= br_dec32le(buf + 12);
+ br_enc32le(buf + 0, q[0]);
+ br_enc32le(buf + 4, q[2]);
+ br_enc32le(buf + 8, q[4]);
+ br_enc32le(buf + 12, q[6]);
+
+ buf += 16;
+ len -= 16;
+
+ /*
+ * We set the cm* values to the block to encrypt in the
+ * next iteration.
+ */
+ if (first_iter) {
+ first_iter = 0;
+ cm0 ^= q[0];
+ cm1 ^= q[2];
+ cm2 ^= q[4];
+ cm3 ^= q[6];
+ } else {
+ cm0 = q[0] ^ q[1];
+ cm1 = q[2] ^ q[3];
+ cm2 = q[4] ^ q[5];
+ cm3 = q[6] ^ q[7];
+ }
+
+ /*
+ * If this was the last iteration, then compute the
+ * extra block encryption to complete CBC-MAC.
+ */
+ if (len == 0) {
+ q[0] = cm0;
+ q[2] = cm1;
+ q[4] = cm2;
+ q[6] = cm3;
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+ cm0 = q[0];
+ cm1 = q[2];
+ cm2 = q[4];
+ cm3 = q[6];
+ break;
+ }
+ }
+
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char *ivbuf;
+ uint32_t iv0, iv1, iv2, iv3;
+ uint32_t cm0, cm1, cm2, cm3;
+ uint32_t sk_exp[120];
+
+ br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+
+ /*
+ * We keep the counter as four 32-bit values, with big-endian
+ * convention, because that's what is expected for purposes of
+ * incrementing the counter value.
+ */
+ ivbuf = ctr;
+ iv0 = br_dec32be(ivbuf + 0);
+ iv1 = br_dec32be(ivbuf + 4);
+ iv2 = br_dec32be(ivbuf + 8);
+ iv3 = br_dec32be(ivbuf + 12);
+
+ /*
+ * The current CBC-MAC value is kept in little-endian convention.
+ */
+ cm0 = br_dec32le((unsigned char *)cbcmac + 0);
+ cm1 = br_dec32le((unsigned char *)cbcmac + 4);
+ cm2 = br_dec32le((unsigned char *)cbcmac + 8);
+ cm3 = br_dec32le((unsigned char *)cbcmac + 12);
+
+ buf = data;
+ while (len > 0) {
+ uint32_t q[8], carry;
+ unsigned char tmp[16];
+
+ /*
+ * The bitslice implementation expects values in
+ * little-endian convention, so we have to byteswap them.
+ */
+ q[0] = br_swap32(iv0);
+ q[2] = br_swap32(iv1);
+ q[4] = br_swap32(iv2);
+ q[6] = br_swap32(iv3);
+ iv3 ++;
+ carry = ~(iv3 | -iv3) >> 31;
+ iv2 += carry;
+ carry &= -(~(iv2 | -iv2) >> 31);
+ iv1 += carry;
+ carry &= -(~(iv1 | -iv1) >> 31);
+ iv0 += carry;
+
+ /*
+ * The odd values are used for CBC-MAC.
+ */
+ q[1] = cm0 ^ br_dec32le(buf + 0);
+ q[3] = cm1 ^ br_dec32le(buf + 4);
+ q[5] = cm2 ^ br_dec32le(buf + 8);
+ q[7] = cm3 ^ br_dec32le(buf + 12);
+
+ br_aes_ct_ortho(q);
+ br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
+ br_aes_ct_ortho(q);
+
+ br_enc32le(tmp + 0, q[0]);
+ br_enc32le(tmp + 4, q[2]);
+ br_enc32le(tmp + 8, q[4]);
+ br_enc32le(tmp + 12, q[6]);
+ xorbuf(buf, tmp, 16);
+ cm0 = q[1];
+ cm1 = q[3];
+ cm2 = q[5];
+ cm3 = q[7];
+ buf += 16;
+ len -= 16;
+ }
+
+ br_enc32be(ivbuf + 0, iv0);
+ br_enc32be(ivbuf + 4, iv1);
+ br_enc32be(ivbuf + 8, iv2);
+ br_enc32be(ivbuf + 12, iv3);
+ br_enc32le((unsigned char *)cbcmac + 0, cm0);
+ br_enc32le((unsigned char *)cbcmac + 4, cm1);
+ br_enc32le((unsigned char *)cbcmac + 8, cm2);
+ br_enc32le((unsigned char *)cbcmac + 12, cm3);
+}
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable = {
+ sizeof(br_aes_ct_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_ct_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_ct_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_ct_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_ct_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_ct_ctrcbc_mac
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_dec.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_dec.c
new file mode 100644
index 00000000..7f32d2bd
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_dec.c
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_aes_ct_bitslice_invSbox(uint32_t *q)
+{
+ /*
+ * AES S-box is:
+ * S(x) = A(I(x)) ^ 0x63
+ * where I() is inversion in GF(256), and A() is a linear
+ * transform (0 is formally defined to be its own inverse).
+ * Since inversion is an involution, the inverse S-box can be
+ * computed from the S-box as:
+ * iS(x) = B(S(B(x ^ 0x63)) ^ 0x63)
+ * where B() is the inverse of A(). Indeed, for any y in GF(256):
+ * iS(S(y)) = B(A(I(B(A(I(y)) ^ 0x63 ^ 0x63))) ^ 0x63 ^ 0x63) = y
+ *
+ * Note: we reuse the implementation of the forward S-box,
+ * instead of duplicating it here, so that total code size is
+ * lower. By merging the B() transforms into the S-box circuit
+ * we could make faster CBC decryption, but CBC decryption is
+ * already quite faster than CBC encryption because we can
+ * process two blocks in parallel.
+ */
+ uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
+
+ q0 = ~q[0];
+ q1 = ~q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = ~q[5];
+ q6 = ~q[6];
+ q7 = q[7];
+ q[7] = q1 ^ q4 ^ q6;
+ q[6] = q0 ^ q3 ^ q5;
+ q[5] = q7 ^ q2 ^ q4;
+ q[4] = q6 ^ q1 ^ q3;
+ q[3] = q5 ^ q0 ^ q2;
+ q[2] = q4 ^ q7 ^ q1;
+ q[1] = q3 ^ q6 ^ q0;
+ q[0] = q2 ^ q5 ^ q7;
+
+ br_aes_ct_bitslice_Sbox(q);
+
+ q0 = ~q[0];
+ q1 = ~q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = ~q[5];
+ q6 = ~q[6];
+ q7 = q[7];
+ q[7] = q1 ^ q4 ^ q6;
+ q[6] = q0 ^ q3 ^ q5;
+ q[5] = q7 ^ q2 ^ q4;
+ q[4] = q6 ^ q1 ^ q3;
+ q[3] = q5 ^ q0 ^ q2;
+ q[2] = q4 ^ q7 ^ q1;
+ q[1] = q3 ^ q6 ^ q0;
+ q[0] = q2 ^ q5 ^ q7;
+}
+
+static void
+add_round_key(uint32_t *q, const uint32_t *sk)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ q[i] ^= sk[i];
+ }
+}
+
+static void
+inv_shift_rows(uint32_t *q)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ uint32_t x;
+
+ x = q[i];
+ q[i] = (x & 0x000000FF)
+ | ((x & 0x00003F00) << 2) | ((x & 0x0000C000) >> 6)
+ | ((x & 0x000F0000) << 4) | ((x & 0x00F00000) >> 4)
+ | ((x & 0x03000000) << 6) | ((x & 0xFC000000) >> 2);
+ }
+}
+
+static inline uint32_t
+rotr16(uint32_t x)
+{
+ return (x << 16) | (x >> 16);
+}
+
+static void
+inv_mix_columns(uint32_t *q)
+{
+ uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
+ uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
+
+ q0 = q[0];
+ q1 = q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = q[5];
+ q6 = q[6];
+ q7 = q[7];
+ r0 = (q0 >> 8) | (q0 << 24);
+ r1 = (q1 >> 8) | (q1 << 24);
+ r2 = (q2 >> 8) | (q2 << 24);
+ r3 = (q3 >> 8) | (q3 << 24);
+ r4 = (q4 >> 8) | (q4 << 24);
+ r5 = (q5 >> 8) | (q5 << 24);
+ r6 = (q6 >> 8) | (q6 << 24);
+ r7 = (q7 >> 8) | (q7 << 24);
+
+ q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr16(q0 ^ q5 ^ q6 ^ r0 ^ r5);
+ q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
+ q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr16(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
+ q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr16(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
+ q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
+ q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
+ q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr16(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
+ q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr16(q4 ^ q5 ^ q7 ^ r4 ^ r7);
+}
+
+/* see inner.h */
+void
+br_aes_ct_bitslice_decrypt(unsigned num_rounds,
+ const uint32_t *skey, uint32_t *q)
+{
+ unsigned u;
+
+ add_round_key(q, skey + (num_rounds << 3));
+ for (u = num_rounds - 1; u > 0; u --) {
+ inv_shift_rows(q);
+ br_aes_ct_bitslice_invSbox(q);
+ add_round_key(q, skey + (u << 3));
+ inv_mix_columns(q);
+ }
+ inv_shift_rows(q);
+ br_aes_ct_bitslice_invSbox(q);
+ add_round_key(q, skey);
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_ct_enc.c b/test/monniaux/BearSSL/src/symcipher/aes_ct_enc.c
new file mode 100644
index 00000000..089bf356
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_ct_enc.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+static inline void
+add_round_key(uint32_t *q, const uint32_t *sk)
+{
+ q[0] ^= sk[0];
+ q[1] ^= sk[1];
+ q[2] ^= sk[2];
+ q[3] ^= sk[3];
+ q[4] ^= sk[4];
+ q[5] ^= sk[5];
+ q[6] ^= sk[6];
+ q[7] ^= sk[7];
+}
+
+static inline void
+shift_rows(uint32_t *q)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ uint32_t x;
+
+ x = q[i];
+ q[i] = (x & 0x000000FF)
+ | ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6)
+ | ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4)
+ | ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2);
+ }
+}
+
+static inline uint32_t
+rotr16(uint32_t x)
+{
+ return (x << 16) | (x >> 16);
+}
+
+static inline void
+mix_columns(uint32_t *q)
+{
+ uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
+ uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
+
+ q0 = q[0];
+ q1 = q[1];
+ q2 = q[2];
+ q3 = q[3];
+ q4 = q[4];
+ q5 = q[5];
+ q6 = q[6];
+ q7 = q[7];
+ r0 = (q0 >> 8) | (q0 << 24);
+ r1 = (q1 >> 8) | (q1 << 24);
+ r2 = (q2 >> 8) | (q2 << 24);
+ r3 = (q3 >> 8) | (q3 << 24);
+ r4 = (q4 >> 8) | (q4 << 24);
+ r5 = (q5 >> 8) | (q5 << 24);
+ r6 = (q6 >> 8) | (q6 << 24);
+ r7 = (q7 >> 8) | (q7 << 24);
+
+ q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0);
+ q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1);
+ q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2);
+ q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3);
+ q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4);
+ q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5);
+ q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6);
+ q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7);
+}
+
+/* see inner.h */
+void
+br_aes_ct_bitslice_encrypt(unsigned num_rounds,
+ const uint32_t *skey, uint32_t *q)
+{
+ unsigned u;
+
+ add_round_key(q, skey);
+ for (u = 1; u < num_rounds; u ++) {
+ br_aes_ct_bitslice_Sbox(q);
+ shift_rows(q);
+ mix_columns(q);
+ add_round_key(q, skey + (u << 3));
+ }
+ br_aes_ct_bitslice_Sbox(q);
+ shift_rows(q);
+ add_round_key(q, skey + (num_rounds << 3));
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_pwr8.c b/test/monniaux/BearSSL/src/symcipher/aes_pwr8.c
new file mode 100644
index 00000000..b2c63c32
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_pwr8.c
@@ -0,0 +1,445 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+/*
+ * This code contains the AES key schedule implementation using the
+ * POWER8 opcodes.
+ */
+
+#if BR_POWER8
+
+static void
+key_schedule_128(unsigned char *sk, const unsigned char *key)
+{
+ long cc;
+
+ static const uint32_t fmod[] = { 0x11B, 0x11B, 0x11B, 0x11B };
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+
+ /*
+ * We use the VSX instructions for loading and storing the
+ * key/subkeys, since they support unaligned accesses. The rest
+ * of the computation is VMX only. VMX register 0 is VSX
+ * register 32.
+ */
+ asm volatile (
+
+ /*
+ * v0 = all-zero word
+ * v1 = constant -8 / +8, copied into four words
+ * v2 = current subkey
+ * v3 = Rcon (x4 words)
+ * v6 = constant 8, copied into four words
+ * v7 = constant 0x11B, copied into four words
+ * v8 = constant for byteswapping words
+ */
+ vspltisw(0, 0)
+#if BR_POWER8_LE
+ vspltisw(1, -8)
+#else
+ vspltisw(1, 8)
+#endif
+ lxvw4x(34, 0, %[key])
+ vspltisw(3, 1)
+ vspltisw(6, 8)
+ lxvw4x(39, 0, %[fmod])
+#if BR_POWER8_LE
+ lxvw4x(40, 0, %[idx2be])
+#endif
+
+ /*
+ * First subkey is a copy of the key itself.
+ */
+#if BR_POWER8_LE
+ vperm(4, 2, 2, 8)
+ stxvw4x(36, 0, %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+#endif
+
+ /*
+ * Loop must run 10 times.
+ */
+ li(%[cc], 10)
+ mtctr(%[cc])
+ label(loop)
+ /* Increment subkey address */
+ addi(%[sk], %[sk], 16)
+
+ /* Compute SubWord(RotWord(temp)) xor Rcon (into v4, splat) */
+ vrlw(4, 2, 1)
+ vsbox(4, 4)
+#if BR_POWER8_LE
+ vxor(4, 4, 3)
+#else
+ vsldoi(5, 3, 0, 3)
+ vxor(4, 4, 5)
+#endif
+ vspltw(4, 4, 3)
+
+ /* XOR words for next subkey */
+ vsldoi(5, 0, 2, 12)
+ vxor(2, 2, 5)
+ vsldoi(5, 0, 2, 12)
+ vxor(2, 2, 5)
+ vsldoi(5, 0, 2, 12)
+ vxor(2, 2, 5)
+ vxor(2, 2, 4)
+
+ /* Store next subkey */
+#if BR_POWER8_LE
+ vperm(4, 2, 2, 8)
+ stxvw4x(36, 0, %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+#endif
+
+ /* Update Rcon */
+ vadduwm(3, 3, 3)
+ vsrw(4, 3, 6)
+ vsubuwm(4, 0, 4)
+ vand(4, 4, 7)
+ vxor(3, 3, 4)
+
+ bdnz(loop)
+
+: [sk] "+b" (sk), [cc] "+b" (cc)
+: [key] "b" (key), [fmod] "b" (fmod)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "ctr", "memory"
+ );
+}
+
+static void
+key_schedule_192(unsigned char *sk, const unsigned char *key)
+{
+ long cc;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+
+ /*
+ * We use the VSX instructions for loading and storing the
+ * key/subkeys, since they support unaligned accesses. The rest
+ * of the computation is VMX only. VMX register 0 is VSX
+ * register 32.
+ */
+ asm volatile (
+
+ /*
+ * v0 = all-zero word
+ * v1 = constant -8 / +8, copied into four words
+ * v2, v3 = current subkey
+ * v5 = Rcon (x4 words) (already shifted on big-endian)
+ * v6 = constant 8, copied into four words
+ * v8 = constant for byteswapping words
+ *
+ * The left two words of v3 are ignored.
+ */
+ vspltisw(0, 0)
+#if BR_POWER8_LE
+ vspltisw(1, -8)
+#else
+ vspltisw(1, 8)
+#endif
+ li(%[cc], 8)
+ lxvw4x(34, 0, %[key])
+ lxvw4x(35, %[cc], %[key])
+ vsldoi(3, 3, 0, 8)
+ vspltisw(5, 1)
+#if !BR_POWER8_LE
+ vsldoi(5, 5, 0, 3)
+#endif
+ vspltisw(6, 8)
+#if BR_POWER8_LE
+ lxvw4x(40, 0, %[idx2be])
+#endif
+
+ /*
+ * Loop must run 8 times. Each iteration produces 256
+ * bits of subkeys, with a 64-bit overlap.
+ */
+ li(%[cc], 8)
+ mtctr(%[cc])
+ li(%[cc], 16)
+ label(loop)
+
+ /*
+ * Last 6 words in v2:v3l. Compute next 6 words into
+ * v3r:v4.
+ */
+ vrlw(10, 3, 1)
+ vsbox(10, 10)
+ vxor(10, 10, 5)
+ vspltw(10, 10, 1)
+ vsldoi(11, 0, 10, 8)
+
+ vsldoi(12, 0, 2, 12)
+ vxor(12, 2, 12)
+ vsldoi(13, 0, 12, 12)
+ vxor(12, 12, 13)
+ vsldoi(13, 0, 12, 12)
+ vxor(12, 12, 13)
+
+ vspltw(13, 12, 3)
+ vxor(13, 13, 3)
+ vsldoi(14, 0, 3, 12)
+ vxor(13, 13, 14)
+
+ vsldoi(4, 12, 13, 8)
+ vsldoi(14, 0, 3, 8)
+ vsldoi(3, 14, 12, 8)
+
+ vxor(3, 3, 11)
+ vxor(4, 4, 10)
+
+ /*
+ * Update Rcon. Since for a 192-bit key, we use only 8
+ * such constants, we will not hit the field modulus,
+ * so a simple shift (addition) works well.
+ */
+ vadduwm(5, 5, 5)
+
+ /*
+ * Write out the two left 128-bit words
+ */
+#if BR_POWER8_LE
+ vperm(10, 2, 2, 8)
+ vperm(11, 3, 3, 8)
+ stxvw4x(42, 0, %[sk])
+ stxvw4x(43, %[cc], %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+ stxvw4x(35, %[cc], %[sk])
+#endif
+ addi(%[sk], %[sk], 24)
+
+ /*
+ * Shift words for next iteration.
+ */
+ vsldoi(2, 3, 4, 8)
+ vsldoi(3, 4, 0, 8)
+
+ bdnz(loop)
+
+ /*
+ * The loop wrote the first 50 subkey words, but we need
+ * to produce 52, so we must do one last write.
+ */
+#if BR_POWER8_LE
+ vperm(10, 2, 2, 8)
+ stxvw4x(42, 0, %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+#endif
+
+: [sk] "+b" (sk), [cc] "+b" (cc)
+: [key] "b" (key)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
+ "v8", "v9", "v10", "v11", "v12", "v13", "v14", "ctr", "memory"
+ );
+}
+
+static void
+key_schedule_256(unsigned char *sk, const unsigned char *key)
+{
+ long cc;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+
+ /*
+ * We use the VSX instructions for loading and storing the
+ * key/subkeys, since they support unaligned accesses. The rest
+ * of the computation is VMX only. VMX register 0 is VSX
+ * register 32.
+ */
+ asm volatile (
+
+ /*
+ * v0 = all-zero word
+ * v1 = constant -8 / +8, copied into four words
+ * v2, v3 = current subkey
+ * v6 = Rcon (x4 words) (already shifted on big-endian)
+ * v7 = constant 8, copied into four words
+ * v8 = constant for byteswapping words
+ *
+ * The left two words of v3 are ignored.
+ */
+ vspltisw(0, 0)
+#if BR_POWER8_LE
+ vspltisw(1, -8)
+#else
+ vspltisw(1, 8)
+#endif
+ li(%[cc], 16)
+ lxvw4x(34, 0, %[key])
+ lxvw4x(35, %[cc], %[key])
+ vspltisw(6, 1)
+#if !BR_POWER8_LE
+ vsldoi(6, 6, 0, 3)
+#endif
+ vspltisw(7, 8)
+#if BR_POWER8_LE
+ lxvw4x(40, 0, %[idx2be])
+#endif
+
+ /*
+ * Loop must run 7 times. Each iteration produces two
+ * subkeys.
+ */
+ li(%[cc], 7)
+ mtctr(%[cc])
+ li(%[cc], 16)
+ label(loop)
+
+ /*
+ * Current words are in v2:v3. Compute next word in v4.
+ */
+ vrlw(10, 3, 1)
+ vsbox(10, 10)
+ vxor(10, 10, 6)
+ vspltw(10, 10, 3)
+
+ vsldoi(4, 0, 2, 12)
+ vxor(4, 2, 4)
+ vsldoi(5, 0, 4, 12)
+ vxor(4, 4, 5)
+ vsldoi(5, 0, 4, 12)
+ vxor(4, 4, 5)
+ vxor(4, 4, 10)
+
+ /*
+ * Then other word in v5.
+ */
+ vsbox(10, 4)
+ vspltw(10, 10, 3)
+
+ vsldoi(5, 0, 3, 12)
+ vxor(5, 3, 5)
+ vsldoi(11, 0, 5, 12)
+ vxor(5, 5, 11)
+ vsldoi(11, 0, 5, 12)
+ vxor(5, 5, 11)
+ vxor(5, 5, 10)
+
+ /*
+ * Update Rcon. Since for a 256-bit key, we use only 7
+ * such constants, we will not hit the field modulus,
+ * so a simple shift (addition) works well.
+ */
+ vadduwm(6, 6, 6)
+
+ /*
+ * Write out the two left 128-bit words
+ */
+#if BR_POWER8_LE
+ vperm(10, 2, 2, 8)
+ vperm(11, 3, 3, 8)
+ stxvw4x(42, 0, %[sk])
+ stxvw4x(43, %[cc], %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+ stxvw4x(35, %[cc], %[sk])
+#endif
+ addi(%[sk], %[sk], 32)
+
+ /*
+ * Replace v2:v3 with v4:v5.
+ */
+ vxor(2, 0, 4)
+ vxor(3, 0, 5)
+
+ bdnz(loop)
+
+ /*
+ * The loop wrote the first 14 subkeys, but we need 15,
+ * so we must do an extra write.
+ */
+#if BR_POWER8_LE
+ vperm(10, 2, 2, 8)
+ stxvw4x(42, 0, %[sk])
+#else
+ stxvw4x(34, 0, %[sk])
+#endif
+
+: [sk] "+b" (sk), [cc] "+b" (cc)
+: [key] "b" (key)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
+ "v8", "v9", "v10", "v11", "v12", "v13", "v14", "ctr", "memory"
+ );
+}
+
+/* see inner.h */
+int
+br_aes_pwr8_supported(void)
+{
+ return 1;
+}
+
+/* see inner.h */
+unsigned
+br_aes_pwr8_keysched(unsigned char *sk, const void *key, size_t len)
+{
+ switch (len) {
+ case 16:
+ key_schedule_128(sk, key);
+ return 10;
+ case 24:
+ key_schedule_192(sk, key);
+ return 12;
+ default:
+ key_schedule_256(sk, key);
+ return 14;
+ }
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcdec.c
new file mode 100644
index 00000000..e535ba6f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcdec.c
@@ -0,0 +1,670 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+#if BR_POWER8
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_cbcdec_init(br_aes_pwr8_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_pwr8_cbcdec_vtable;
+ ctx->num_rounds = br_aes_pwr8_keysched(ctx->skey.skni, key, len);
+}
+
+static void
+cbcdec_128(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v10
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v24.
+ */
+ lxvw4x(56, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(24, 24, 24, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next ciphertext words in v16..v19. Also save them
+ * in v20..v23.
+ */
+ lxvw4x(48, %[cc0], %[buf])
+ lxvw4x(49, %[cc1], %[buf])
+ lxvw4x(50, %[cc2], %[buf])
+ lxvw4x(51, %[cc3], %[buf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ vand(20, 16, 16)
+ vand(21, 17, 17)
+ vand(22, 18, 18)
+ vand(23, 19, 19)
+
+ /*
+ * Decrypt the blocks.
+ */
+ vxor(16, 16, 10)
+ vxor(17, 17, 10)
+ vxor(18, 18, 10)
+ vxor(19, 19, 10)
+ vncipher(16, 16, 9)
+ vncipher(17, 17, 9)
+ vncipher(18, 18, 9)
+ vncipher(19, 19, 9)
+ vncipher(16, 16, 8)
+ vncipher(17, 17, 8)
+ vncipher(18, 18, 8)
+ vncipher(19, 19, 8)
+ vncipher(16, 16, 7)
+ vncipher(17, 17, 7)
+ vncipher(18, 18, 7)
+ vncipher(19, 19, 7)
+ vncipher(16, 16, 6)
+ vncipher(17, 17, 6)
+ vncipher(18, 18, 6)
+ vncipher(19, 19, 6)
+ vncipher(16, 16, 5)
+ vncipher(17, 17, 5)
+ vncipher(18, 18, 5)
+ vncipher(19, 19, 5)
+ vncipher(16, 16, 4)
+ vncipher(17, 17, 4)
+ vncipher(18, 18, 4)
+ vncipher(19, 19, 4)
+ vncipher(16, 16, 3)
+ vncipher(17, 17, 3)
+ vncipher(18, 18, 3)
+ vncipher(19, 19, 3)
+ vncipher(16, 16, 2)
+ vncipher(17, 17, 2)
+ vncipher(18, 18, 2)
+ vncipher(19, 19, 2)
+ vncipher(16, 16, 1)
+ vncipher(17, 17, 1)
+ vncipher(18, 18, 1)
+ vncipher(19, 19, 1)
+ vncipherlast(16, 16, 0)
+ vncipherlast(17, 17, 0)
+ vncipherlast(18, 18, 0)
+ vncipherlast(19, 19, 0)
+
+ /*
+ * XOR decrypted blocks with IV / previous block.
+ */
+ vxor(16, 16, 24)
+ vxor(17, 17, 20)
+ vxor(18, 18, 21)
+ vxor(19, 19, 22)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ /*
+ * Fourth encrypted block is IV for next run.
+ */
+ vand(24, 23, 23)
+
+ addi(%[buf], %[buf], 64)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (num_blocks >> 2)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+static void
+cbcdec_192(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v12
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(43, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(44, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v24.
+ */
+ lxvw4x(56, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(24, 24, 24, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next ciphertext words in v16..v19. Also save them
+ * in v20..v23.
+ */
+ lxvw4x(48, %[cc0], %[buf])
+ lxvw4x(49, %[cc1], %[buf])
+ lxvw4x(50, %[cc2], %[buf])
+ lxvw4x(51, %[cc3], %[buf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ vand(20, 16, 16)
+ vand(21, 17, 17)
+ vand(22, 18, 18)
+ vand(23, 19, 19)
+
+ /*
+ * Decrypt the blocks.
+ */
+ vxor(16, 16, 12)
+ vxor(17, 17, 12)
+ vxor(18, 18, 12)
+ vxor(19, 19, 12)
+ vncipher(16, 16, 11)
+ vncipher(17, 17, 11)
+ vncipher(18, 18, 11)
+ vncipher(19, 19, 11)
+ vncipher(16, 16, 10)
+ vncipher(17, 17, 10)
+ vncipher(18, 18, 10)
+ vncipher(19, 19, 10)
+ vncipher(16, 16, 9)
+ vncipher(17, 17, 9)
+ vncipher(18, 18, 9)
+ vncipher(19, 19, 9)
+ vncipher(16, 16, 8)
+ vncipher(17, 17, 8)
+ vncipher(18, 18, 8)
+ vncipher(19, 19, 8)
+ vncipher(16, 16, 7)
+ vncipher(17, 17, 7)
+ vncipher(18, 18, 7)
+ vncipher(19, 19, 7)
+ vncipher(16, 16, 6)
+ vncipher(17, 17, 6)
+ vncipher(18, 18, 6)
+ vncipher(19, 19, 6)
+ vncipher(16, 16, 5)
+ vncipher(17, 17, 5)
+ vncipher(18, 18, 5)
+ vncipher(19, 19, 5)
+ vncipher(16, 16, 4)
+ vncipher(17, 17, 4)
+ vncipher(18, 18, 4)
+ vncipher(19, 19, 4)
+ vncipher(16, 16, 3)
+ vncipher(17, 17, 3)
+ vncipher(18, 18, 3)
+ vncipher(19, 19, 3)
+ vncipher(16, 16, 2)
+ vncipher(17, 17, 2)
+ vncipher(18, 18, 2)
+ vncipher(19, 19, 2)
+ vncipher(16, 16, 1)
+ vncipher(17, 17, 1)
+ vncipher(18, 18, 1)
+ vncipher(19, 19, 1)
+ vncipherlast(16, 16, 0)
+ vncipherlast(17, 17, 0)
+ vncipherlast(18, 18, 0)
+ vncipherlast(19, 19, 0)
+
+ /*
+ * XOR decrypted blocks with IV / previous block.
+ */
+ vxor(16, 16, 24)
+ vxor(17, 17, 20)
+ vxor(18, 18, 21)
+ vxor(19, 19, 22)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ /*
+ * Fourth encrypted block is IV for next run.
+ */
+ vand(24, 23, 23)
+
+ addi(%[buf], %[buf], 64)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (num_blocks >> 2)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+static void
+cbcdec_256(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v14
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(43, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(44, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(45, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(46, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v24.
+ */
+ lxvw4x(56, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(24, 24, 24, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next ciphertext words in v16..v19. Also save them
+ * in v20..v23.
+ */
+ lxvw4x(48, %[cc0], %[buf])
+ lxvw4x(49, %[cc1], %[buf])
+ lxvw4x(50, %[cc2], %[buf])
+ lxvw4x(51, %[cc3], %[buf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ vand(20, 16, 16)
+ vand(21, 17, 17)
+ vand(22, 18, 18)
+ vand(23, 19, 19)
+
+ /*
+ * Decrypt the blocks.
+ */
+ vxor(16, 16, 14)
+ vxor(17, 17, 14)
+ vxor(18, 18, 14)
+ vxor(19, 19, 14)
+ vncipher(16, 16, 13)
+ vncipher(17, 17, 13)
+ vncipher(18, 18, 13)
+ vncipher(19, 19, 13)
+ vncipher(16, 16, 12)
+ vncipher(17, 17, 12)
+ vncipher(18, 18, 12)
+ vncipher(19, 19, 12)
+ vncipher(16, 16, 11)
+ vncipher(17, 17, 11)
+ vncipher(18, 18, 11)
+ vncipher(19, 19, 11)
+ vncipher(16, 16, 10)
+ vncipher(17, 17, 10)
+ vncipher(18, 18, 10)
+ vncipher(19, 19, 10)
+ vncipher(16, 16, 9)
+ vncipher(17, 17, 9)
+ vncipher(18, 18, 9)
+ vncipher(19, 19, 9)
+ vncipher(16, 16, 8)
+ vncipher(17, 17, 8)
+ vncipher(18, 18, 8)
+ vncipher(19, 19, 8)
+ vncipher(16, 16, 7)
+ vncipher(17, 17, 7)
+ vncipher(18, 18, 7)
+ vncipher(19, 19, 7)
+ vncipher(16, 16, 6)
+ vncipher(17, 17, 6)
+ vncipher(18, 18, 6)
+ vncipher(19, 19, 6)
+ vncipher(16, 16, 5)
+ vncipher(17, 17, 5)
+ vncipher(18, 18, 5)
+ vncipher(19, 19, 5)
+ vncipher(16, 16, 4)
+ vncipher(17, 17, 4)
+ vncipher(18, 18, 4)
+ vncipher(19, 19, 4)
+ vncipher(16, 16, 3)
+ vncipher(17, 17, 3)
+ vncipher(18, 18, 3)
+ vncipher(19, 19, 3)
+ vncipher(16, 16, 2)
+ vncipher(17, 17, 2)
+ vncipher(18, 18, 2)
+ vncipher(19, 19, 2)
+ vncipher(16, 16, 1)
+ vncipher(17, 17, 1)
+ vncipher(18, 18, 1)
+ vncipher(19, 19, 1)
+ vncipherlast(16, 16, 0)
+ vncipherlast(17, 17, 0)
+ vncipherlast(18, 18, 0)
+ vncipherlast(19, 19, 0)
+
+ /*
+ * XOR decrypted blocks with IV / previous block.
+ */
+ vxor(16, 16, 24)
+ vxor(17, 17, 20)
+ vxor(18, 18, 21)
+ vxor(19, 19, 22)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ /*
+ * Fourth encrypted block is IV for next run.
+ */
+ vand(24, 23, 23)
+
+ addi(%[buf], %[buf], 64)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (num_blocks >> 2)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_cbcdec_run(const br_aes_pwr8_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char nextiv[16];
+ unsigned char *buf;
+
+ if (len == 0) {
+ return;
+ }
+ buf = data;
+ memcpy(nextiv, buf + len - 16, 16);
+ if (len >= 64) {
+ size_t num_blocks;
+ unsigned char tmp[16];
+
+ num_blocks = (len >> 4) & ~(size_t)3;
+ memcpy(tmp, buf + (num_blocks << 4) - 16, 16);
+ switch (ctx->num_rounds) {
+ case 10:
+ cbcdec_128(ctx->skey.skni, iv, buf, num_blocks);
+ break;
+ case 12:
+ cbcdec_192(ctx->skey.skni, iv, buf, num_blocks);
+ break;
+ default:
+ cbcdec_256(ctx->skey.skni, iv, buf, num_blocks);
+ break;
+ }
+ buf += num_blocks << 4;
+ len &= 63;
+ memcpy(iv, tmp, 16);
+ }
+ if (len > 0) {
+ unsigned char tmp[64];
+
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ switch (ctx->num_rounds) {
+ case 10:
+ cbcdec_128(ctx->skey.skni, iv, tmp, 4);
+ break;
+ case 12:
+ cbcdec_192(ctx->skey.skni, iv, tmp, 4);
+ break;
+ default:
+ cbcdec_256(ctx->skey.skni, iv, tmp, 4);
+ break;
+ }
+ memcpy(buf, tmp, len);
+ }
+ memcpy(iv, nextiv, 16);
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_pwr8_cbcdec_vtable = {
+ sizeof(br_aes_pwr8_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_pwr8_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_pwr8_cbcdec_run
+};
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class *
+br_aes_pwr8_cbcdec_get_vtable(void)
+{
+ return br_aes_pwr8_supported() ? &br_aes_pwr8_cbcdec_vtable : NULL;
+}
+
+#else
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class *
+br_aes_pwr8_cbcdec_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcenc.c
new file mode 100644
index 00000000..00f8eca7
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_cbcenc.c
@@ -0,0 +1,417 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+#if BR_POWER8
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_cbcenc_init(br_aes_pwr8_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_pwr8_cbcenc_vtable;
+ ctx->num_rounds = br_aes_pwr8_keysched(ctx->skey.skni, key, len);
+}
+
+static void
+cbcenc_128(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t len)
+{
+ long cc;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v10
+ */
+ lxvw4x(32, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(33, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(34, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(35, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(36, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(37, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(38, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(39, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(40, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(41, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(42, %[cc], %[sk])
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v16.
+ */
+ lxvw4x(48, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next plaintext word and XOR with current IV.
+ */
+ lxvw4x(49, 0, %[buf])
+#if BR_POWER8_LE
+ vperm(17, 17, 17, 15)
+#endif
+ vxor(16, 16, 17)
+
+ /*
+ * Encrypt the block.
+ */
+ vxor(16, 16, 0)
+ vcipher(16, 16, 1)
+ vcipher(16, 16, 2)
+ vcipher(16, 16, 3)
+ vcipher(16, 16, 4)
+ vcipher(16, 16, 5)
+ vcipher(16, 16, 6)
+ vcipher(16, 16, 7)
+ vcipher(16, 16, 8)
+ vcipher(16, 16, 9)
+ vcipherlast(16, 16, 10)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(17, 16, 16, 15)
+ stxvw4x(49, 0, %[buf])
+#else
+ stxvw4x(48, 0, %[buf])
+#endif
+ addi(%[buf], %[buf], 16)
+
+ bdnz(loop)
+
+: [cc] "+b" (cc), [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (len >> 4)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "ctr", "memory"
+ );
+}
+
+static void
+cbcenc_192(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t len)
+{
+ long cc;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v12
+ */
+ lxvw4x(32, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(33, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(34, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(35, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(36, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(37, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(38, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(39, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(40, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(41, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(42, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(43, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(44, %[cc], %[sk])
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v16.
+ */
+ lxvw4x(48, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next plaintext word and XOR with current IV.
+ */
+ lxvw4x(49, 0, %[buf])
+#if BR_POWER8_LE
+ vperm(17, 17, 17, 15)
+#endif
+ vxor(16, 16, 17)
+
+ /*
+ * Encrypt the block.
+ */
+ vxor(16, 16, 0)
+ vcipher(16, 16, 1)
+ vcipher(16, 16, 2)
+ vcipher(16, 16, 3)
+ vcipher(16, 16, 4)
+ vcipher(16, 16, 5)
+ vcipher(16, 16, 6)
+ vcipher(16, 16, 7)
+ vcipher(16, 16, 8)
+ vcipher(16, 16, 9)
+ vcipher(16, 16, 10)
+ vcipher(16, 16, 11)
+ vcipherlast(16, 16, 12)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(17, 16, 16, 15)
+ stxvw4x(49, 0, %[buf])
+#else
+ stxvw4x(48, 0, %[buf])
+#endif
+ addi(%[buf], %[buf], 16)
+
+ bdnz(loop)
+
+: [cc] "+b" (cc), [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (len >> 4)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "ctr", "memory"
+ );
+}
+
+static void
+cbcenc_256(const unsigned char *sk,
+ const unsigned char *iv, unsigned char *buf, size_t len)
+{
+ long cc;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+
+ cc = 0;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v14
+ */
+ lxvw4x(32, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(33, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(34, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(35, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(36, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(37, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(38, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(39, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(40, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(41, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(42, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(43, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(44, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(45, %[cc], %[sk])
+ addi(%[cc], %[cc], 16)
+ lxvw4x(46, %[cc], %[sk])
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * Load IV into v16.
+ */
+ lxvw4x(48, 0, %[iv])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Load next plaintext word and XOR with current IV.
+ */
+ lxvw4x(49, 0, %[buf])
+#if BR_POWER8_LE
+ vperm(17, 17, 17, 15)
+#endif
+ vxor(16, 16, 17)
+
+ /*
+ * Encrypt the block.
+ */
+ vxor(16, 16, 0)
+ vcipher(16, 16, 1)
+ vcipher(16, 16, 2)
+ vcipher(16, 16, 3)
+ vcipher(16, 16, 4)
+ vcipher(16, 16, 5)
+ vcipher(16, 16, 6)
+ vcipher(16, 16, 7)
+ vcipher(16, 16, 8)
+ vcipher(16, 16, 9)
+ vcipher(16, 16, 10)
+ vcipher(16, 16, 11)
+ vcipher(16, 16, 12)
+ vcipher(16, 16, 13)
+ vcipherlast(16, 16, 14)
+
+ /*
+ * Store back result (with byteswap)
+ */
+#if BR_POWER8_LE
+ vperm(17, 16, 16, 15)
+ stxvw4x(49, 0, %[buf])
+#else
+ stxvw4x(48, 0, %[buf])
+#endif
+ addi(%[buf], %[buf], 16)
+
+ bdnz(loop)
+
+: [cc] "+b" (cc), [buf] "+b" (buf)
+: [sk] "b" (sk), [iv] "b" (iv), [num_blocks] "b" (len >> 4)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "ctr", "memory"
+ );
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_cbcenc_run(const br_aes_pwr8_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ if (len > 0) {
+ switch (ctx->num_rounds) {
+ case 10:
+ cbcenc_128(ctx->skey.skni, iv, data, len);
+ break;
+ case 12:
+ cbcenc_192(ctx->skey.skni, iv, data, len);
+ break;
+ default:
+ cbcenc_256(ctx->skey.skni, iv, data, len);
+ break;
+ }
+ memcpy(iv, (unsigned char *)data + (len - 16), 16);
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_pwr8_cbcenc_vtable = {
+ sizeof(br_aes_pwr8_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_pwr8_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_pwr8_cbcenc_run
+};
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class *
+br_aes_pwr8_cbcenc_get_vtable(void)
+{
+ return br_aes_pwr8_supported() ? &br_aes_pwr8_cbcenc_vtable : NULL;
+}
+
+#else
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class *
+br_aes_pwr8_cbcenc_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctr.c
new file mode 100644
index 00000000..f5d20c0b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctr.c
@@ -0,0 +1,717 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+#if BR_POWER8
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctr_init(br_aes_pwr8_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_pwr8_ctr_vtable;
+ ctx->num_rounds = br_aes_pwr8_keysched(ctx->skey.skni, key, len);
+}
+
+static void
+ctr_128(const unsigned char *sk, const unsigned char *ivbuf,
+ unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+ static const uint32_t ctrinc[] = {
+ 0, 0, 0, 4
+ };
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v10
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * v28 = increment for IV counter.
+ */
+ lxvw4x(60, 0, %[ctrinc])
+
+ /*
+ * Load IV into v16..v19
+ */
+ lxvw4x(48, %[cc0], %[ivbuf])
+ lxvw4x(49, %[cc1], %[ivbuf])
+ lxvw4x(50, %[cc2], %[ivbuf])
+ lxvw4x(51, %[cc3], %[ivbuf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Compute next IV into v24..v27
+ */
+ vadduwm(24, 16, 28)
+ vadduwm(25, 17, 28)
+ vadduwm(26, 18, 28)
+ vadduwm(27, 19, 28)
+
+ /*
+ * Load next data blocks. We do this early on but we
+ * won't need them until IV encryption is done.
+ */
+ lxvw4x(52, %[cc0], %[buf])
+ lxvw4x(53, %[cc1], %[buf])
+ lxvw4x(54, %[cc2], %[buf])
+ lxvw4x(55, %[cc3], %[buf])
+
+ /*
+ * Encrypt the current IV.
+ */
+ vxor(16, 16, 0)
+ vxor(17, 17, 0)
+ vxor(18, 18, 0)
+ vxor(19, 19, 0)
+ vcipher(16, 16, 1)
+ vcipher(17, 17, 1)
+ vcipher(18, 18, 1)
+ vcipher(19, 19, 1)
+ vcipher(16, 16, 2)
+ vcipher(17, 17, 2)
+ vcipher(18, 18, 2)
+ vcipher(19, 19, 2)
+ vcipher(16, 16, 3)
+ vcipher(17, 17, 3)
+ vcipher(18, 18, 3)
+ vcipher(19, 19, 3)
+ vcipher(16, 16, 4)
+ vcipher(17, 17, 4)
+ vcipher(18, 18, 4)
+ vcipher(19, 19, 4)
+ vcipher(16, 16, 5)
+ vcipher(17, 17, 5)
+ vcipher(18, 18, 5)
+ vcipher(19, 19, 5)
+ vcipher(16, 16, 6)
+ vcipher(17, 17, 6)
+ vcipher(18, 18, 6)
+ vcipher(19, 19, 6)
+ vcipher(16, 16, 7)
+ vcipher(17, 17, 7)
+ vcipher(18, 18, 7)
+ vcipher(19, 19, 7)
+ vcipher(16, 16, 8)
+ vcipher(17, 17, 8)
+ vcipher(18, 18, 8)
+ vcipher(19, 19, 8)
+ vcipher(16, 16, 9)
+ vcipher(17, 17, 9)
+ vcipher(18, 18, 9)
+ vcipher(19, 19, 9)
+ vcipherlast(16, 16, 10)
+ vcipherlast(17, 17, 10)
+ vcipherlast(18, 18, 10)
+ vcipherlast(19, 19, 10)
+
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ /*
+ * Load next plaintext word and XOR with encrypted IV.
+ */
+ vxor(16, 20, 16)
+ vxor(17, 21, 17)
+ vxor(18, 22, 18)
+ vxor(19, 23, 19)
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ addi(%[buf], %[buf], 64)
+
+ /*
+ * Update IV.
+ */
+ vand(16, 24, 24)
+ vand(17, 25, 25)
+ vand(18, 26, 26)
+ vand(19, 27, 27)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [ivbuf] "b" (ivbuf), [num_blocks] "b" (num_blocks >> 2),
+ [ctrinc] "b" (ctrinc)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+static void
+ctr_192(const unsigned char *sk, const unsigned char *ivbuf,
+ unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+ static const uint32_t ctrinc[] = {
+ 0, 0, 0, 4
+ };
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v12
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(43, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(44, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * v28 = increment for IV counter.
+ */
+ lxvw4x(60, 0, %[ctrinc])
+
+ /*
+ * Load IV into v16..v19
+ */
+ lxvw4x(48, %[cc0], %[ivbuf])
+ lxvw4x(49, %[cc1], %[ivbuf])
+ lxvw4x(50, %[cc2], %[ivbuf])
+ lxvw4x(51, %[cc3], %[ivbuf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Compute next IV into v24..v27
+ */
+ vadduwm(24, 16, 28)
+ vadduwm(25, 17, 28)
+ vadduwm(26, 18, 28)
+ vadduwm(27, 19, 28)
+
+ /*
+ * Load next data blocks. We do this early on but we
+ * won't need them until IV encryption is done.
+ */
+ lxvw4x(52, %[cc0], %[buf])
+ lxvw4x(53, %[cc1], %[buf])
+ lxvw4x(54, %[cc2], %[buf])
+ lxvw4x(55, %[cc3], %[buf])
+
+ /*
+ * Encrypt the current IV.
+ */
+ vxor(16, 16, 0)
+ vxor(17, 17, 0)
+ vxor(18, 18, 0)
+ vxor(19, 19, 0)
+ vcipher(16, 16, 1)
+ vcipher(17, 17, 1)
+ vcipher(18, 18, 1)
+ vcipher(19, 19, 1)
+ vcipher(16, 16, 2)
+ vcipher(17, 17, 2)
+ vcipher(18, 18, 2)
+ vcipher(19, 19, 2)
+ vcipher(16, 16, 3)
+ vcipher(17, 17, 3)
+ vcipher(18, 18, 3)
+ vcipher(19, 19, 3)
+ vcipher(16, 16, 4)
+ vcipher(17, 17, 4)
+ vcipher(18, 18, 4)
+ vcipher(19, 19, 4)
+ vcipher(16, 16, 5)
+ vcipher(17, 17, 5)
+ vcipher(18, 18, 5)
+ vcipher(19, 19, 5)
+ vcipher(16, 16, 6)
+ vcipher(17, 17, 6)
+ vcipher(18, 18, 6)
+ vcipher(19, 19, 6)
+ vcipher(16, 16, 7)
+ vcipher(17, 17, 7)
+ vcipher(18, 18, 7)
+ vcipher(19, 19, 7)
+ vcipher(16, 16, 8)
+ vcipher(17, 17, 8)
+ vcipher(18, 18, 8)
+ vcipher(19, 19, 8)
+ vcipher(16, 16, 9)
+ vcipher(17, 17, 9)
+ vcipher(18, 18, 9)
+ vcipher(19, 19, 9)
+ vcipher(16, 16, 10)
+ vcipher(17, 17, 10)
+ vcipher(18, 18, 10)
+ vcipher(19, 19, 10)
+ vcipher(16, 16, 11)
+ vcipher(17, 17, 11)
+ vcipher(18, 18, 11)
+ vcipher(19, 19, 11)
+ vcipherlast(16, 16, 12)
+ vcipherlast(17, 17, 12)
+ vcipherlast(18, 18, 12)
+ vcipherlast(19, 19, 12)
+
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ /*
+ * Load next plaintext word and XOR with encrypted IV.
+ */
+ vxor(16, 20, 16)
+ vxor(17, 21, 17)
+ vxor(18, 22, 18)
+ vxor(19, 23, 19)
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ addi(%[buf], %[buf], 64)
+
+ /*
+ * Update IV.
+ */
+ vand(16, 24, 24)
+ vand(17, 25, 25)
+ vand(18, 26, 26)
+ vand(19, 27, 27)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [ivbuf] "b" (ivbuf), [num_blocks] "b" (num_blocks >> 2),
+ [ctrinc] "b" (ctrinc)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+static void
+ctr_256(const unsigned char *sk, const unsigned char *ivbuf,
+ unsigned char *buf, size_t num_blocks)
+{
+ long cc0, cc1, cc2, cc3;
+
+#if BR_POWER8_LE
+ static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+ };
+#endif
+ static const uint32_t ctrinc[] = {
+ 0, 0, 0, 4
+ };
+
+ cc0 = 0;
+ cc1 = 16;
+ cc2 = 32;
+ cc3 = 48;
+ asm volatile (
+
+ /*
+ * Load subkeys into v0..v14
+ */
+ lxvw4x(32, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(33, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(34, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(35, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(36, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(37, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(38, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(39, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(40, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(41, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(42, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(43, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(44, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(45, %[cc0], %[sk])
+ addi(%[cc0], %[cc0], 16)
+ lxvw4x(46, %[cc0], %[sk])
+ li(%[cc0], 0)
+
+#if BR_POWER8_LE
+ /*
+ * v15 = constant for byteswapping words
+ */
+ lxvw4x(47, 0, %[idx2be])
+#endif
+ /*
+ * v28 = increment for IV counter.
+ */
+ lxvw4x(60, 0, %[ctrinc])
+
+ /*
+ * Load IV into v16..v19
+ */
+ lxvw4x(48, %[cc0], %[ivbuf])
+ lxvw4x(49, %[cc1], %[ivbuf])
+ lxvw4x(50, %[cc2], %[ivbuf])
+ lxvw4x(51, %[cc3], %[ivbuf])
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ mtctr(%[num_blocks])
+ label(loop)
+ /*
+ * Compute next IV into v24..v27
+ */
+ vadduwm(24, 16, 28)
+ vadduwm(25, 17, 28)
+ vadduwm(26, 18, 28)
+ vadduwm(27, 19, 28)
+
+ /*
+ * Load next data blocks. We do this early on but we
+ * won't need them until IV encryption is done.
+ */
+ lxvw4x(52, %[cc0], %[buf])
+ lxvw4x(53, %[cc1], %[buf])
+ lxvw4x(54, %[cc2], %[buf])
+ lxvw4x(55, %[cc3], %[buf])
+
+ /*
+ * Encrypt the current IV.
+ */
+ vxor(16, 16, 0)
+ vxor(17, 17, 0)
+ vxor(18, 18, 0)
+ vxor(19, 19, 0)
+ vcipher(16, 16, 1)
+ vcipher(17, 17, 1)
+ vcipher(18, 18, 1)
+ vcipher(19, 19, 1)
+ vcipher(16, 16, 2)
+ vcipher(17, 17, 2)
+ vcipher(18, 18, 2)
+ vcipher(19, 19, 2)
+ vcipher(16, 16, 3)
+ vcipher(17, 17, 3)
+ vcipher(18, 18, 3)
+ vcipher(19, 19, 3)
+ vcipher(16, 16, 4)
+ vcipher(17, 17, 4)
+ vcipher(18, 18, 4)
+ vcipher(19, 19, 4)
+ vcipher(16, 16, 5)
+ vcipher(17, 17, 5)
+ vcipher(18, 18, 5)
+ vcipher(19, 19, 5)
+ vcipher(16, 16, 6)
+ vcipher(17, 17, 6)
+ vcipher(18, 18, 6)
+ vcipher(19, 19, 6)
+ vcipher(16, 16, 7)
+ vcipher(17, 17, 7)
+ vcipher(18, 18, 7)
+ vcipher(19, 19, 7)
+ vcipher(16, 16, 8)
+ vcipher(17, 17, 8)
+ vcipher(18, 18, 8)
+ vcipher(19, 19, 8)
+ vcipher(16, 16, 9)
+ vcipher(17, 17, 9)
+ vcipher(18, 18, 9)
+ vcipher(19, 19, 9)
+ vcipher(16, 16, 10)
+ vcipher(17, 17, 10)
+ vcipher(18, 18, 10)
+ vcipher(19, 19, 10)
+ vcipher(16, 16, 11)
+ vcipher(17, 17, 11)
+ vcipher(18, 18, 11)
+ vcipher(19, 19, 11)
+ vcipher(16, 16, 12)
+ vcipher(17, 17, 12)
+ vcipher(18, 18, 12)
+ vcipher(19, 19, 12)
+ vcipher(16, 16, 13)
+ vcipher(17, 17, 13)
+ vcipher(18, 18, 13)
+ vcipher(19, 19, 13)
+ vcipherlast(16, 16, 14)
+ vcipherlast(17, 17, 14)
+ vcipherlast(18, 18, 14)
+ vcipherlast(19, 19, 14)
+
+#if BR_POWER8_LE
+ vperm(16, 16, 16, 15)
+ vperm(17, 17, 17, 15)
+ vperm(18, 18, 18, 15)
+ vperm(19, 19, 19, 15)
+#endif
+
+ /*
+ * Load next plaintext word and XOR with encrypted IV.
+ */
+ vxor(16, 20, 16)
+ vxor(17, 21, 17)
+ vxor(18, 22, 18)
+ vxor(19, 23, 19)
+ stxvw4x(48, %[cc0], %[buf])
+ stxvw4x(49, %[cc1], %[buf])
+ stxvw4x(50, %[cc2], %[buf])
+ stxvw4x(51, %[cc3], %[buf])
+
+ addi(%[buf], %[buf], 64)
+
+ /*
+ * Update IV.
+ */
+ vand(16, 24, 24)
+ vand(17, 25, 25)
+ vand(18, 26, 26)
+ vand(19, 27, 27)
+
+ bdnz(loop)
+
+: [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3),
+ [buf] "+b" (buf)
+: [sk] "b" (sk), [ivbuf] "b" (ivbuf), [num_blocks] "b" (num_blocks >> 2),
+ [ctrinc] "b" (ctrinc)
+#if BR_POWER8_LE
+ , [idx2be] "b" (idx2be)
+#endif
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
+ "ctr", "memory"
+ );
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_aes_pwr8_ctr_run(const br_aes_pwr8_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char ivbuf[64];
+
+ buf = data;
+ memcpy(ivbuf + 0, iv, 12);
+ memcpy(ivbuf + 16, iv, 12);
+ memcpy(ivbuf + 32, iv, 12);
+ memcpy(ivbuf + 48, iv, 12);
+ if (len >= 64) {
+ br_enc32be(ivbuf + 12, cc + 0);
+ br_enc32be(ivbuf + 28, cc + 1);
+ br_enc32be(ivbuf + 44, cc + 2);
+ br_enc32be(ivbuf + 60, cc + 3);
+ switch (ctx->num_rounds) {
+ case 10:
+ ctr_128(ctx->skey.skni, ivbuf, buf,
+ (len >> 4) & ~(size_t)3);
+ break;
+ case 12:
+ ctr_192(ctx->skey.skni, ivbuf, buf,
+ (len >> 4) & ~(size_t)3);
+ break;
+ default:
+ ctr_256(ctx->skey.skni, ivbuf, buf,
+ (len >> 4) & ~(size_t)3);
+ break;
+ }
+ cc += (len >> 4) & ~(size_t)3;
+ buf += len & ~(size_t)63;
+ len &= 63;
+ }
+ if (len > 0) {
+ unsigned char tmp[64];
+
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ br_enc32be(ivbuf + 12, cc + 0);
+ br_enc32be(ivbuf + 28, cc + 1);
+ br_enc32be(ivbuf + 44, cc + 2);
+ br_enc32be(ivbuf + 60, cc + 3);
+ switch (ctx->num_rounds) {
+ case 10:
+ ctr_128(ctx->skey.skni, ivbuf, tmp, 4);
+ break;
+ case 12:
+ ctr_192(ctx->skey.skni, ivbuf, tmp, 4);
+ break;
+ default:
+ ctr_256(ctx->skey.skni, ivbuf, tmp, 4);
+ break;
+ }
+ memcpy(buf, tmp, len);
+ cc += (len + 15) >> 4;
+ }
+ return cc;
+}
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_pwr8_ctr_vtable = {
+ sizeof(br_aes_pwr8_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_pwr8_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_pwr8_ctr_run
+};
+
+/* see bearssl_block.h */
+const br_block_ctr_class *
+br_aes_pwr8_ctr_get_vtable(void)
+{
+ return br_aes_pwr8_supported() ? &br_aes_pwr8_ctr_vtable : NULL;
+}
+
+#else
+
+/* see bearssl_block.h */
+const br_block_ctr_class *
+br_aes_pwr8_ctr_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctrcbc.c
new file mode 100644
index 00000000..a67d30b6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_pwr8_ctrcbc.c
@@ -0,0 +1,946 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_POWER_ASM_MACROS 1
+#include "inner.h"
+
+#if BR_POWER8
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class *
+br_aes_pwr8_ctrcbc_get_vtable(void)
+{
+ return br_aes_pwr8_supported() ? &br_aes_pwr8_ctrcbc_vtable : NULL;
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctrcbc_init(br_aes_pwr8_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_pwr8_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_pwr8_keysched(ctx->skey.skni, key, len);
+}
+
+/*
+ * Register conventions for CTR + CBC-MAC:
+ *
+ * AES subkeys are in registers 0 to 10/12/14 (depending on keys size)
+ * Register v15 contains the byteswap index register (little-endian only)
+ * Register v16 contains the CTR counter value
+ * Register v17 contains the CBC-MAC current value
+ * Registers v18 to v27 are scratch
+ * Counter increment uses v28, v29 and v30
+ *
+ * For CTR alone:
+ *
+ * AES subkeys are in registers 0 to 10/12/14 (depending on keys size)
+ * Register v15 contains the byteswap index register (little-endian only)
+ * Registers v16 to v19 contain the CTR counter values (four blocks)
+ * Registers v20 to v27 are scratch
+ * Counter increment uses v28, v29 and v30
+ */
+
+#define LOAD_SUBKEYS_128 \
+ lxvw4x(32, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(33, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(34, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(35, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(36, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(37, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(38, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(39, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(40, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(41, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(42, %[cc], %[sk])
+
+#define LOAD_SUBKEYS_192 \
+ LOAD_SUBKEYS_128 \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(43, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(44, %[cc], %[sk])
+
+#define LOAD_SUBKEYS_256 \
+ LOAD_SUBKEYS_192 \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(45, %[cc], %[sk]) \
+ addi(%[cc], %[cc], 16) \
+ lxvw4x(46, %[cc], %[sk])
+
+#define BLOCK_ENCRYPT_128(x) \
+ vxor(x, x, 0) \
+ vcipher(x, x, 1) \
+ vcipher(x, x, 2) \
+ vcipher(x, x, 3) \
+ vcipher(x, x, 4) \
+ vcipher(x, x, 5) \
+ vcipher(x, x, 6) \
+ vcipher(x, x, 7) \
+ vcipher(x, x, 8) \
+ vcipher(x, x, 9) \
+ vcipherlast(x, x, 10)
+
+#define BLOCK_ENCRYPT_192(x) \
+ vxor(x, x, 0) \
+ vcipher(x, x, 1) \
+ vcipher(x, x, 2) \
+ vcipher(x, x, 3) \
+ vcipher(x, x, 4) \
+ vcipher(x, x, 5) \
+ vcipher(x, x, 6) \
+ vcipher(x, x, 7) \
+ vcipher(x, x, 8) \
+ vcipher(x, x, 9) \
+ vcipher(x, x, 10) \
+ vcipher(x, x, 11) \
+ vcipherlast(x, x, 12)
+
+#define BLOCK_ENCRYPT_256(x) \
+ vxor(x, x, 0) \
+ vcipher(x, x, 1) \
+ vcipher(x, x, 2) \
+ vcipher(x, x, 3) \
+ vcipher(x, x, 4) \
+ vcipher(x, x, 5) \
+ vcipher(x, x, 6) \
+ vcipher(x, x, 7) \
+ vcipher(x, x, 8) \
+ vcipher(x, x, 9) \
+ vcipher(x, x, 10) \
+ vcipher(x, x, 11) \
+ vcipher(x, x, 12) \
+ vcipher(x, x, 13) \
+ vcipherlast(x, x, 14)
+
+#define BLOCK_ENCRYPT_X2_128(x, y) \
+ vxor(x, x, 0) \
+ vxor(y, y, 0) \
+ vcipher(x, x, 1) \
+ vcipher(y, y, 1) \
+ vcipher(x, x, 2) \
+ vcipher(y, y, 2) \
+ vcipher(x, x, 3) \
+ vcipher(y, y, 3) \
+ vcipher(x, x, 4) \
+ vcipher(y, y, 4) \
+ vcipher(x, x, 5) \
+ vcipher(y, y, 5) \
+ vcipher(x, x, 6) \
+ vcipher(y, y, 6) \
+ vcipher(x, x, 7) \
+ vcipher(y, y, 7) \
+ vcipher(x, x, 8) \
+ vcipher(y, y, 8) \
+ vcipher(x, x, 9) \
+ vcipher(y, y, 9) \
+ vcipherlast(x, x, 10) \
+ vcipherlast(y, y, 10)
+
+#define BLOCK_ENCRYPT_X2_192(x, y) \
+ vxor(x, x, 0) \
+ vxor(y, y, 0) \
+ vcipher(x, x, 1) \
+ vcipher(y, y, 1) \
+ vcipher(x, x, 2) \
+ vcipher(y, y, 2) \
+ vcipher(x, x, 3) \
+ vcipher(y, y, 3) \
+ vcipher(x, x, 4) \
+ vcipher(y, y, 4) \
+ vcipher(x, x, 5) \
+ vcipher(y, y, 5) \
+ vcipher(x, x, 6) \
+ vcipher(y, y, 6) \
+ vcipher(x, x, 7) \
+ vcipher(y, y, 7) \
+ vcipher(x, x, 8) \
+ vcipher(y, y, 8) \
+ vcipher(x, x, 9) \
+ vcipher(y, y, 9) \
+ vcipher(x, x, 10) \
+ vcipher(y, y, 10) \
+ vcipher(x, x, 11) \
+ vcipher(y, y, 11) \
+ vcipherlast(x, x, 12) \
+ vcipherlast(y, y, 12)
+
+#define BLOCK_ENCRYPT_X2_256(x, y) \
+ vxor(x, x, 0) \
+ vxor(y, y, 0) \
+ vcipher(x, x, 1) \
+ vcipher(y, y, 1) \
+ vcipher(x, x, 2) \
+ vcipher(y, y, 2) \
+ vcipher(x, x, 3) \
+ vcipher(y, y, 3) \
+ vcipher(x, x, 4) \
+ vcipher(y, y, 4) \
+ vcipher(x, x, 5) \
+ vcipher(y, y, 5) \
+ vcipher(x, x, 6) \
+ vcipher(y, y, 6) \
+ vcipher(x, x, 7) \
+ vcipher(y, y, 7) \
+ vcipher(x, x, 8) \
+ vcipher(y, y, 8) \
+ vcipher(x, x, 9) \
+ vcipher(y, y, 9) \
+ vcipher(x, x, 10) \
+ vcipher(y, y, 10) \
+ vcipher(x, x, 11) \
+ vcipher(y, y, 11) \
+ vcipher(x, x, 12) \
+ vcipher(y, y, 12) \
+ vcipher(x, x, 13) \
+ vcipher(y, y, 13) \
+ vcipherlast(x, x, 14) \
+ vcipherlast(y, y, 14)
+
+#define BLOCK_ENCRYPT_X4_128(x0, x1, x2, x3) \
+ vxor(x0, x0, 0) \
+ vxor(x1, x1, 0) \
+ vxor(x2, x2, 0) \
+ vxor(x3, x3, 0) \
+ vcipher(x0, x0, 1) \
+ vcipher(x1, x1, 1) \
+ vcipher(x2, x2, 1) \
+ vcipher(x3, x3, 1) \
+ vcipher(x0, x0, 2) \
+ vcipher(x1, x1, 2) \
+ vcipher(x2, x2, 2) \
+ vcipher(x3, x3, 2) \
+ vcipher(x0, x0, 3) \
+ vcipher(x1, x1, 3) \
+ vcipher(x2, x2, 3) \
+ vcipher(x3, x3, 3) \
+ vcipher(x0, x0, 4) \
+ vcipher(x1, x1, 4) \
+ vcipher(x2, x2, 4) \
+ vcipher(x3, x3, 4) \
+ vcipher(x0, x0, 5) \
+ vcipher(x1, x1, 5) \
+ vcipher(x2, x2, 5) \
+ vcipher(x3, x3, 5) \
+ vcipher(x0, x0, 6) \
+ vcipher(x1, x1, 6) \
+ vcipher(x2, x2, 6) \
+ vcipher(x3, x3, 6) \
+ vcipher(x0, x0, 7) \
+ vcipher(x1, x1, 7) \
+ vcipher(x2, x2, 7) \
+ vcipher(x3, x3, 7) \
+ vcipher(x0, x0, 8) \
+ vcipher(x1, x1, 8) \
+ vcipher(x2, x2, 8) \
+ vcipher(x3, x3, 8) \
+ vcipher(x0, x0, 9) \
+ vcipher(x1, x1, 9) \
+ vcipher(x2, x2, 9) \
+ vcipher(x3, x3, 9) \
+ vcipherlast(x0, x0, 10) \
+ vcipherlast(x1, x1, 10) \
+ vcipherlast(x2, x2, 10) \
+ vcipherlast(x3, x3, 10)
+
+#define BLOCK_ENCRYPT_X4_192(x0, x1, x2, x3) \
+ vxor(x0, x0, 0) \
+ vxor(x1, x1, 0) \
+ vxor(x2, x2, 0) \
+ vxor(x3, x3, 0) \
+ vcipher(x0, x0, 1) \
+ vcipher(x1, x1, 1) \
+ vcipher(x2, x2, 1) \
+ vcipher(x3, x3, 1) \
+ vcipher(x0, x0, 2) \
+ vcipher(x1, x1, 2) \
+ vcipher(x2, x2, 2) \
+ vcipher(x3, x3, 2) \
+ vcipher(x0, x0, 3) \
+ vcipher(x1, x1, 3) \
+ vcipher(x2, x2, 3) \
+ vcipher(x3, x3, 3) \
+ vcipher(x0, x0, 4) \
+ vcipher(x1, x1, 4) \
+ vcipher(x2, x2, 4) \
+ vcipher(x3, x3, 4) \
+ vcipher(x0, x0, 5) \
+ vcipher(x1, x1, 5) \
+ vcipher(x2, x2, 5) \
+ vcipher(x3, x3, 5) \
+ vcipher(x0, x0, 6) \
+ vcipher(x1, x1, 6) \
+ vcipher(x2, x2, 6) \
+ vcipher(x3, x3, 6) \
+ vcipher(x0, x0, 7) \
+ vcipher(x1, x1, 7) \
+ vcipher(x2, x2, 7) \
+ vcipher(x3, x3, 7) \
+ vcipher(x0, x0, 8) \
+ vcipher(x1, x1, 8) \
+ vcipher(x2, x2, 8) \
+ vcipher(x3, x3, 8) \
+ vcipher(x0, x0, 9) \
+ vcipher(x1, x1, 9) \
+ vcipher(x2, x2, 9) \
+ vcipher(x3, x3, 9) \
+ vcipher(x0, x0, 10) \
+ vcipher(x1, x1, 10) \
+ vcipher(x2, x2, 10) \
+ vcipher(x3, x3, 10) \
+ vcipher(x0, x0, 11) \
+ vcipher(x1, x1, 11) \
+ vcipher(x2, x2, 11) \
+ vcipher(x3, x3, 11) \
+ vcipherlast(x0, x0, 12) \
+ vcipherlast(x1, x1, 12) \
+ vcipherlast(x2, x2, 12) \
+ vcipherlast(x3, x3, 12)
+
+#define BLOCK_ENCRYPT_X4_256(x0, x1, x2, x3) \
+ vxor(x0, x0, 0) \
+ vxor(x1, x1, 0) \
+ vxor(x2, x2, 0) \
+ vxor(x3, x3, 0) \
+ vcipher(x0, x0, 1) \
+ vcipher(x1, x1, 1) \
+ vcipher(x2, x2, 1) \
+ vcipher(x3, x3, 1) \
+ vcipher(x0, x0, 2) \
+ vcipher(x1, x1, 2) \
+ vcipher(x2, x2, 2) \
+ vcipher(x3, x3, 2) \
+ vcipher(x0, x0, 3) \
+ vcipher(x1, x1, 3) \
+ vcipher(x2, x2, 3) \
+ vcipher(x3, x3, 3) \
+ vcipher(x0, x0, 4) \
+ vcipher(x1, x1, 4) \
+ vcipher(x2, x2, 4) \
+ vcipher(x3, x3, 4) \
+ vcipher(x0, x0, 5) \
+ vcipher(x1, x1, 5) \
+ vcipher(x2, x2, 5) \
+ vcipher(x3, x3, 5) \
+ vcipher(x0, x0, 6) \
+ vcipher(x1, x1, 6) \
+ vcipher(x2, x2, 6) \
+ vcipher(x3, x3, 6) \
+ vcipher(x0, x0, 7) \
+ vcipher(x1, x1, 7) \
+ vcipher(x2, x2, 7) \
+ vcipher(x3, x3, 7) \
+ vcipher(x0, x0, 8) \
+ vcipher(x1, x1, 8) \
+ vcipher(x2, x2, 8) \
+ vcipher(x3, x3, 8) \
+ vcipher(x0, x0, 9) \
+ vcipher(x1, x1, 9) \
+ vcipher(x2, x2, 9) \
+ vcipher(x3, x3, 9) \
+ vcipher(x0, x0, 10) \
+ vcipher(x1, x1, 10) \
+ vcipher(x2, x2, 10) \
+ vcipher(x3, x3, 10) \
+ vcipher(x0, x0, 11) \
+ vcipher(x1, x1, 11) \
+ vcipher(x2, x2, 11) \
+ vcipher(x3, x3, 11) \
+ vcipher(x0, x0, 12) \
+ vcipher(x1, x1, 12) \
+ vcipher(x2, x2, 12) \
+ vcipher(x3, x3, 12) \
+ vcipher(x0, x0, 13) \
+ vcipher(x1, x1, 13) \
+ vcipher(x2, x2, 13) \
+ vcipher(x3, x3, 13) \
+ vcipherlast(x0, x0, 14) \
+ vcipherlast(x1, x1, 14) \
+ vcipherlast(x2, x2, 14) \
+ vcipherlast(x3, x3, 14)
+
+#if BR_POWER8_LE
+static const uint32_t idx2be[] = {
+ 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
+};
+#define BYTESWAP_INIT lxvw4x(47, 0, %[idx2be])
+#define BYTESWAP(x) vperm(x, x, x, 15)
+#define BYTESWAPX(d, s) vperm(d, s, s, 15)
+#define BYTESWAP_REG , [idx2be] "b" (idx2be)
+#else
+#define BYTESWAP_INIT
+#define BYTESWAP(x)
+#define BYTESWAPX(d, s) vand(d, s, s)
+#define BYTESWAP_REG
+#endif
+
+static const uint32_t ctrinc[] = {
+ 0, 0, 0, 1
+};
+static const uint32_t ctrinc_x4[] = {
+ 0, 0, 0, 4
+};
+#define INCR_128_INIT lxvw4x(60, 0, %[ctrinc])
+#define INCR_128_X4_INIT lxvw4x(60, 0, %[ctrinc_x4])
+#define INCR_128(d, s) \
+ vaddcuw(29, s, 28) \
+ vadduwm(d, s, 28) \
+ vsldoi(30, 29, 29, 4) \
+ vaddcuw(29, d, 30) \
+ vadduwm(d, d, 30) \
+ vsldoi(30, 29, 29, 4) \
+ vaddcuw(29, d, 30) \
+ vadduwm(d, d, 30) \
+ vsldoi(30, 29, 29, 4) \
+ vadduwm(d, d, 30)
+
+#define MKCTR(size) \
+static void \
+ctr_ ## size(const unsigned char *sk, \
+ unsigned char *ctrbuf, unsigned char *buf, size_t num_blocks_x4) \
+{ \
+ long cc, cc0, cc1, cc2, cc3; \
+ \
+ cc = 0; \
+ cc0 = 0; \
+ cc1 = 16; \
+ cc2 = 32; \
+ cc3 = 48; \
+ asm volatile ( \
+ \
+ /* \
+ * Load subkeys into v0..v10 \
+ */ \
+ LOAD_SUBKEYS_ ## size \
+ li(%[cc], 0) \
+ \
+ BYTESWAP_INIT \
+ INCR_128_X4_INIT \
+ \
+ /* \
+ * Load current CTR counters into v16 to v19. \
+ */ \
+ lxvw4x(48, %[cc0], %[ctrbuf]) \
+ lxvw4x(49, %[cc1], %[ctrbuf]) \
+ lxvw4x(50, %[cc2], %[ctrbuf]) \
+ lxvw4x(51, %[cc3], %[ctrbuf]) \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ BYTESWAP(18) \
+ BYTESWAP(19) \
+ \
+ mtctr(%[num_blocks_x4]) \
+ \
+ label(loop) \
+ /* \
+ * Compute next counter values into v20..v23. \
+ */ \
+ INCR_128(20, 16) \
+ INCR_128(21, 17) \
+ INCR_128(22, 18) \
+ INCR_128(23, 19) \
+ \
+ /* \
+ * Encrypt counter values and XOR into next data blocks. \
+ */ \
+ lxvw4x(56, %[cc0], %[buf]) \
+ lxvw4x(57, %[cc1], %[buf]) \
+ lxvw4x(58, %[cc2], %[buf]) \
+ lxvw4x(59, %[cc3], %[buf]) \
+ BYTESWAP(24) \
+ BYTESWAP(25) \
+ BYTESWAP(26) \
+ BYTESWAP(27) \
+ BLOCK_ENCRYPT_X4_ ## size(16, 17, 18, 19) \
+ vxor(16, 16, 24) \
+ vxor(17, 17, 25) \
+ vxor(18, 18, 26) \
+ vxor(19, 19, 27) \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ BYTESWAP(18) \
+ BYTESWAP(19) \
+ stxvw4x(48, %[cc0], %[buf]) \
+ stxvw4x(49, %[cc1], %[buf]) \
+ stxvw4x(50, %[cc2], %[buf]) \
+ stxvw4x(51, %[cc3], %[buf]) \
+ \
+ /* \
+ * Update counters and data pointer. \
+ */ \
+ vand(16, 20, 20) \
+ vand(17, 21, 21) \
+ vand(18, 22, 22) \
+ vand(19, 23, 23) \
+ addi(%[buf], %[buf], 64) \
+ \
+ bdnz(loop) \
+ \
+ /* \
+ * Write back new counter values. \
+ */ \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ BYTESWAP(18) \
+ BYTESWAP(19) \
+ stxvw4x(48, %[cc0], %[ctrbuf]) \
+ stxvw4x(49, %[cc1], %[ctrbuf]) \
+ stxvw4x(50, %[cc2], %[ctrbuf]) \
+ stxvw4x(51, %[cc3], %[ctrbuf]) \
+ \
+: [cc] "+b" (cc), [buf] "+b" (buf), \
+ [cc0] "+b" (cc0), [cc1] "+b" (cc1), [cc2] "+b" (cc2), [cc3] "+b" (cc3) \
+: [sk] "b" (sk), [ctrbuf] "b" (ctrbuf), \
+ [num_blocks_x4] "b" (num_blocks_x4), [ctrinc_x4] "b" (ctrinc_x4) \
+ BYTESWAP_REG \
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", \
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", \
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", \
+ "v30", "ctr", "memory" \
+ ); \
+}
+
+MKCTR(128)
+MKCTR(192)
+MKCTR(256)
+
+#define MKCBCMAC(size) \
+static void \
+cbcmac_ ## size(const unsigned char *sk, \
+ unsigned char *cbcmac, const unsigned char *buf, size_t num_blocks) \
+{ \
+ long cc; \
+ \
+ cc = 0; \
+ asm volatile ( \
+ \
+ /* \
+ * Load subkeys into v0..v10 \
+ */ \
+ LOAD_SUBKEYS_ ## size \
+ li(%[cc], 0) \
+ \
+ BYTESWAP_INIT \
+ \
+ /* \
+ * Load current CBC-MAC value into v16. \
+ */ \
+ lxvw4x(48, %[cc], %[cbcmac]) \
+ BYTESWAP(16) \
+ \
+ mtctr(%[num_blocks]) \
+ \
+ label(loop) \
+ /* \
+ * Load next block, XOR into current CBC-MAC value, \
+ * and then encrypt it. \
+ */ \
+ lxvw4x(49, %[cc], %[buf]) \
+ BYTESWAP(17) \
+ vxor(16, 16, 17) \
+ BLOCK_ENCRYPT_ ## size(16) \
+ addi(%[buf], %[buf], 16) \
+ \
+ bdnz(loop) \
+ \
+ /* \
+ * Write back new CBC-MAC value. \
+ */ \
+ BYTESWAP(16) \
+ stxvw4x(48, %[cc], %[cbcmac]) \
+ \
+: [cc] "+b" (cc), [buf] "+b" (buf) \
+: [sk] "b" (sk), [cbcmac] "b" (cbcmac), [num_blocks] "b" (num_blocks) \
+ BYTESWAP_REG \
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", \
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", \
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", \
+ "v30", "ctr", "memory" \
+ ); \
+}
+
+MKCBCMAC(128)
+MKCBCMAC(192)
+MKCBCMAC(256)
+
+#define MKENCRYPT(size) \
+static void \
+ctrcbc_ ## size ## _encrypt(const unsigned char *sk, \
+ unsigned char *ctr, unsigned char *cbcmac, unsigned char *buf, \
+ size_t num_blocks) \
+{ \
+ long cc; \
+ \
+ cc = 0; \
+ asm volatile ( \
+ \
+ /* \
+ * Load subkeys into v0..v10 \
+ */ \
+ LOAD_SUBKEYS_ ## size \
+ li(%[cc], 0) \
+ \
+ BYTESWAP_INIT \
+ INCR_128_INIT \
+ \
+ /* \
+ * Load current CTR counter into v16, and current \
+ * CBC-MAC IV into v17. \
+ */ \
+ lxvw4x(48, %[cc], %[ctr]) \
+ lxvw4x(49, %[cc], %[cbcmac]) \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ \
+ /* \
+ * At each iteration, we do two parallel encryption: \
+ * - new counter value for encryption of the next block; \
+ * - CBC-MAC over the previous encrypted block. \
+ * Thus, each plaintext block implies two AES instances, \
+ * over two successive iterations. This requires a single \
+ * counter encryption before the loop, and a single \
+ * CBC-MAC encryption after the loop. \
+ */ \
+ \
+ /* \
+ * Encrypt first block (into v20). \
+ */ \
+ lxvw4x(52, %[cc], %[buf]) \
+ BYTESWAP(20) \
+ INCR_128(22, 16) \
+ BLOCK_ENCRYPT_ ## size(16) \
+ vxor(20, 20, 16) \
+ BYTESWAPX(21, 20) \
+ stxvw4x(53, %[cc], %[buf]) \
+ vand(16, 22, 22) \
+ addi(%[buf], %[buf], 16) \
+ \
+ /* \
+ * Load loop counter; skip the loop if there is only \
+ * one block in total (already handled by the boundary \
+ * conditions). \
+ */ \
+ mtctr(%[num_blocks]) \
+ bdz(fastexit) \
+ \
+ label(loop) \
+ /* \
+ * Upon loop entry: \
+ * v16 counter value for next block \
+ * v17 current CBC-MAC value \
+ * v20 encrypted previous block \
+ */ \
+ vxor(17, 17, 20) \
+ INCR_128(22, 16) \
+ lxvw4x(52, %[cc], %[buf]) \
+ BYTESWAP(20) \
+ BLOCK_ENCRYPT_X2_ ## size(16, 17) \
+ vxor(20, 20, 16) \
+ BYTESWAPX(21, 20) \
+ stxvw4x(53, %[cc], %[buf]) \
+ addi(%[buf], %[buf], 16) \
+ vand(16, 22, 22) \
+ \
+ bdnz(loop) \
+ \
+ label(fastexit) \
+ vxor(17, 17, 20) \
+ BLOCK_ENCRYPT_ ## size(17) \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ stxvw4x(48, %[cc], %[ctr]) \
+ stxvw4x(49, %[cc], %[cbcmac]) \
+ \
+: [cc] "+b" (cc), [buf] "+b" (buf) \
+: [sk] "b" (sk), [ctr] "b" (ctr), [cbcmac] "b" (cbcmac), \
+ [num_blocks] "b" (num_blocks), [ctrinc] "b" (ctrinc) \
+ BYTESWAP_REG \
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", \
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", \
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", \
+ "v30", "ctr", "memory" \
+ ); \
+}
+
+MKENCRYPT(128)
+MKENCRYPT(192)
+MKENCRYPT(256)
+
+#define MKDECRYPT(size) \
+static void \
+ctrcbc_ ## size ## _decrypt(const unsigned char *sk, \
+ unsigned char *ctr, unsigned char *cbcmac, unsigned char *buf, \
+ size_t num_blocks) \
+{ \
+ long cc; \
+ \
+ cc = 0; \
+ asm volatile ( \
+ \
+ /* \
+ * Load subkeys into v0..v10 \
+ */ \
+ LOAD_SUBKEYS_ ## size \
+ li(%[cc], 0) \
+ \
+ BYTESWAP_INIT \
+ INCR_128_INIT \
+ \
+ /* \
+ * Load current CTR counter into v16, and current \
+ * CBC-MAC IV into v17. \
+ */ \
+ lxvw4x(48, %[cc], %[ctr]) \
+ lxvw4x(49, %[cc], %[cbcmac]) \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ \
+ /* \
+ * At each iteration, we do two parallel encryption: \
+ * - new counter value for decryption of the next block; \
+ * - CBC-MAC over the next encrypted block. \
+ * Each iteration performs the two AES instances related \
+ * to the current block; there is thus no need for some \
+ * extra pre-loop and post-loop work as in encryption. \
+ */ \
+ \
+ mtctr(%[num_blocks]) \
+ \
+ label(loop) \
+ /* \
+ * Upon loop entry: \
+ * v16 counter value for next block \
+ * v17 current CBC-MAC value \
+ */ \
+ lxvw4x(52, %[cc], %[buf]) \
+ BYTESWAP(20) \
+ vxor(17, 17, 20) \
+ INCR_128(22, 16) \
+ BLOCK_ENCRYPT_X2_ ## size(16, 17) \
+ vxor(20, 20, 16) \
+ BYTESWAPX(21, 20) \
+ stxvw4x(53, %[cc], %[buf]) \
+ addi(%[buf], %[buf], 16) \
+ vand(16, 22, 22) \
+ \
+ bdnz(loop) \
+ \
+ /* \
+ * Store back counter and CBC-MAC value. \
+ */ \
+ BYTESWAP(16) \
+ BYTESWAP(17) \
+ stxvw4x(48, %[cc], %[ctr]) \
+ stxvw4x(49, %[cc], %[cbcmac]) \
+ \
+: [cc] "+b" (cc), [buf] "+b" (buf) \
+: [sk] "b" (sk), [ctr] "b" (ctr), [cbcmac] "b" (cbcmac), \
+ [num_blocks] "b" (num_blocks), [ctrinc] "b" (ctrinc) \
+ BYTESWAP_REG \
+: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", \
+ "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", \
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", \
+ "v30", "ctr", "memory" \
+ ); \
+}
+
+MKDECRYPT(128)
+MKDECRYPT(192)
+MKDECRYPT(256)
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctrcbc_encrypt(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ if (len == 0) {
+ return;
+ }
+ switch (ctx->num_rounds) {
+ case 10:
+ ctrcbc_128_encrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ case 12:
+ ctrcbc_192_encrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ default:
+ ctrcbc_256_encrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctrcbc_decrypt(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ if (len == 0) {
+ return;
+ }
+ switch (ctx->num_rounds) {
+ case 10:
+ ctrcbc_128_decrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ case 12:
+ ctrcbc_192_decrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ default:
+ ctrcbc_256_decrypt(ctx->skey.skni, ctr, cbcmac, data, len >> 4);
+ break;
+ }
+}
+
+static inline void
+incr_ctr(void *dst, const void *src)
+{
+ uint64_t hi, lo;
+
+ hi = br_dec64be(src);
+ lo = br_dec64be((const unsigned char *)src + 8);
+ lo ++;
+ hi += ((lo | -lo) >> 63) ^ (uint64_t)1;
+ br_enc64be(dst, hi);
+ br_enc64be((unsigned char *)dst + 8, lo);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctrcbc_ctr(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char ctrbuf[64];
+
+ memcpy(ctrbuf, ctr, 16);
+ incr_ctr(ctrbuf + 16, ctrbuf);
+ incr_ctr(ctrbuf + 32, ctrbuf + 16);
+ incr_ctr(ctrbuf + 48, ctrbuf + 32);
+ if (len >= 64) {
+ switch (ctx->num_rounds) {
+ case 10:
+ ctr_128(ctx->skey.skni, ctrbuf, data, len >> 6);
+ break;
+ case 12:
+ ctr_192(ctx->skey.skni, ctrbuf, data, len >> 6);
+ break;
+ default:
+ ctr_256(ctx->skey.skni, ctrbuf, data, len >> 6);
+ break;
+ }
+ data = (unsigned char *)data + (len & ~(size_t)63);
+ len &= 63;
+ }
+ if (len > 0) {
+ unsigned char tmp[64];
+
+ if (len >= 32) {
+ if (len >= 48) {
+ memcpy(ctr, ctrbuf + 48, 16);
+ } else {
+ memcpy(ctr, ctrbuf + 32, 16);
+ }
+ } else {
+ if (len >= 16) {
+ memcpy(ctr, ctrbuf + 16, 16);
+ }
+ }
+ memcpy(tmp, data, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ switch (ctx->num_rounds) {
+ case 10:
+ ctr_128(ctx->skey.skni, ctrbuf, tmp, 1);
+ break;
+ case 12:
+ ctr_192(ctx->skey.skni, ctrbuf, tmp, 1);
+ break;
+ default:
+ ctr_256(ctx->skey.skni, ctrbuf, tmp, 1);
+ break;
+ }
+ memcpy(data, tmp, len);
+ } else {
+ memcpy(ctr, ctrbuf, 16);
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_pwr8_ctrcbc_mac(const br_aes_pwr8_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ if (len > 0) {
+ switch (ctx->num_rounds) {
+ case 10:
+ cbcmac_128(ctx->skey.skni, cbcmac, data, len >> 4);
+ break;
+ case 12:
+ cbcmac_192(ctx->skey.skni, cbcmac, data, len >> 4);
+ break;
+ default:
+ cbcmac_256(ctx->skey.skni, cbcmac, data, len >> 4);
+ break;
+ }
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_pwr8_ctrcbc_vtable = {
+ sizeof(br_aes_pwr8_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_pwr8_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_pwr8_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_pwr8_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_pwr8_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_pwr8_ctrcbc_mac
+};
+
+#else
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class *
+br_aes_pwr8_ctrcbc_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_small_cbcdec.c
new file mode 100644
index 00000000..8567244b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_cbcdec.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_small_cbcdec_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16];
+ int i;
+
+ memcpy(tmp, buf, 16);
+ br_aes_small_decrypt(ctx->num_rounds, ctx->skey, buf);
+ for (i = 0; i < 16; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ memcpy(ivbuf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_small_cbcdec_vtable = {
+ sizeof(br_aes_small_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_small_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_small_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_small_cbcenc.c
new file mode 100644
index 00000000..0dc2910a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_cbcenc.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_small_cbcenc_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ br_aes_small_encrypt(ctx->num_rounds, ctx->skey, buf);
+ memcpy(ivbuf, buf, 16);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_small_cbcenc_vtable = {
+ sizeof(br_aes_small_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_small_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_small_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_small_ctr.c
new file mode 100644
index 00000000..d5d371c6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_ctr.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_small_ctr_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+uint32_t
+br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16];
+
+ memcpy(tmp, iv, 12);
+ br_enc32be(tmp + 12, cc ++);
+ br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);
+ if (len <= 16) {
+ xorbuf(buf, tmp, len);
+ break;
+ }
+ xorbuf(buf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ }
+ return cc;
+}
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_small_ctr_vtable = {
+ sizeof(br_aes_small_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_small_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_small_ctr_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_small_ctrcbc.c
new file mode 100644
index 00000000..2d6ba329
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_ctrcbc.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_small_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
+}
+
+static void
+xorbuf(void *dst, const void *src, size_t len)
+{
+ unsigned char *d;
+ const unsigned char *s;
+
+ d = dst;
+ s = src;
+ while (len -- > 0) {
+ *d ++ ^= *s ++;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char *buf, *bctr;
+ uint32_t cc0, cc1, cc2, cc3;
+
+ buf = data;
+ bctr = ctr;
+ cc3 = br_dec32be(bctr + 0);
+ cc2 = br_dec32be(bctr + 4);
+ cc1 = br_dec32be(bctr + 8);
+ cc0 = br_dec32be(bctr + 12);
+ while (len > 0) {
+ unsigned char tmp[16];
+ uint32_t carry;
+
+ br_enc32be(tmp + 0, cc3);
+ br_enc32be(tmp + 4, cc2);
+ br_enc32be(tmp + 8, cc1);
+ br_enc32be(tmp + 12, cc0);
+ br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);
+ xorbuf(buf, tmp, 16);
+ buf += 16;
+ len -= 16;
+ cc0 ++;
+ carry = (~(cc0 | -cc0)) >> 31;
+ cc1 += carry;
+ carry &= (~(cc1 | -cc1)) >> 31;
+ cc2 += carry;
+ carry &= (~(cc2 | -cc2)) >> 31;
+ cc3 += carry;
+ }
+ br_enc32be(bctr + 0, cc3);
+ br_enc32be(bctr + 4, cc2);
+ br_enc32be(bctr + 8, cc1);
+ br_enc32be(bctr + 12, cc0);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ const unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ xorbuf(cbcmac, buf, 16);
+ br_aes_small_encrypt(ctx->num_rounds, ctx->skey, cbcmac);
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ br_aes_small_ctrcbc_ctr(ctx, ctr, data, len);
+ br_aes_small_ctrcbc_mac(ctx, cbcmac, data, len);
+}
+
+/* see bearssl_block.h */
+void
+br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ br_aes_small_ctrcbc_mac(ctx, cbcmac, data, len);
+ br_aes_small_ctrcbc_ctr(ctx, ctr, data, len);
+}
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable = {
+ sizeof(br_aes_small_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_small_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_small_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_small_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_small_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_small_ctrcbc_mac
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_dec.c b/test/monniaux/BearSSL/src/symcipher/aes_small_dec.c
new file mode 100644
index 00000000..59dca8ec
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_dec.c
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Inverse S-box.
+ */
+static const unsigned char iS[] = {
+ 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
+ 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
+ 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
+ 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
+ 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
+ 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
+ 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
+ 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
+ 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
+ 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
+ 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
+ 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
+ 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
+ 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
+ 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
+ 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
+ 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
+ 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
+ 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
+ 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
+ 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
+ 0x55, 0x21, 0x0C, 0x7D
+};
+
+static void
+add_round_key(unsigned *state, const uint32_t *skeys)
+{
+ int i;
+
+ for (i = 0; i < 16; i += 4) {
+ uint32_t k;
+
+ k = *skeys ++;
+ state[i + 0] ^= (unsigned)(k >> 24);
+ state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
+ state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
+ state[i + 3] ^= (unsigned)k & 0xFF;
+ }
+}
+
+static void
+inv_sub_bytes(unsigned *state)
+{
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ state[i] = iS[state[i]];
+ }
+}
+
+static void
+inv_shift_rows(unsigned *state)
+{
+ unsigned tmp;
+
+ tmp = state[13];
+ state[13] = state[9];
+ state[9] = state[5];
+ state[5] = state[1];
+ state[1] = tmp;
+
+ tmp = state[2];
+ state[2] = state[10];
+ state[10] = tmp;
+ tmp = state[6];
+ state[6] = state[14];
+ state[14] = tmp;
+
+ tmp = state[3];
+ state[3] = state[7];
+ state[7] = state[11];
+ state[11] = state[15];
+ state[15] = tmp;
+}
+
+static inline unsigned
+gf256red(unsigned x)
+{
+ unsigned y;
+
+ y = x >> 8;
+ return (x ^ y ^ (y << 1) ^ (y << 3) ^ (y << 4)) & 0xFF;
+}
+
+static void
+inv_mix_columns(unsigned *state)
+{
+ int i;
+
+ for (i = 0; i < 16; i += 4) {
+ unsigned s0, s1, s2, s3;
+ unsigned t0, t1, t2, t3;
+
+ s0 = state[i + 0];
+ s1 = state[i + 1];
+ s2 = state[i + 2];
+ s3 = state[i + 3];
+ t0 = (s0 << 1) ^ (s0 << 2) ^ (s0 << 3)
+ ^ s1 ^ (s1 << 1) ^ (s1 << 3)
+ ^ s2 ^ (s2 << 2) ^ (s2 << 3)
+ ^ s3 ^ (s3 << 3);
+ t1 = s0 ^ (s0 << 3)
+ ^ (s1 << 1) ^ (s1 << 2) ^ (s1 << 3)
+ ^ s2 ^ (s2 << 1) ^ (s2 << 3)
+ ^ s3 ^ (s3 << 2) ^ (s3 << 3);
+ t2 = s0 ^ (s0 << 2) ^ (s0 << 3)
+ ^ s1 ^ (s1 << 3)
+ ^ (s2 << 1) ^ (s2 << 2) ^ (s2 << 3)
+ ^ s3 ^ (s3 << 1) ^ (s3 << 3);
+ t3 = s0 ^ (s0 << 1) ^ (s0 << 3)
+ ^ s1 ^ (s1 << 2) ^ (s1 << 3)
+ ^ s2 ^ (s2 << 3)
+ ^ (s3 << 1) ^ (s3 << 2) ^ (s3 << 3);
+ state[i + 0] = gf256red(t0);
+ state[i + 1] = gf256red(t1);
+ state[i + 2] = gf256red(t2);
+ state[i + 3] = gf256red(t3);
+ }
+}
+
+/* see inner.h */
+void
+br_aes_small_decrypt(unsigned num_rounds, const uint32_t *skey, void *data)
+{
+ unsigned char *buf;
+ unsigned state[16];
+ unsigned u;
+
+ buf = data;
+ for (u = 0; u < 16; u ++) {
+ state[u] = buf[u];
+ }
+ add_round_key(state, skey + (num_rounds << 2));
+ for (u = num_rounds - 1; u > 0; u --) {
+ inv_shift_rows(state);
+ inv_sub_bytes(state);
+ add_round_key(state, skey + (u << 2));
+ inv_mix_columns(state);
+ }
+ inv_shift_rows(state);
+ inv_sub_bytes(state);
+ add_round_key(state, skey);
+ for (u = 0; u < 16; u ++) {
+ buf[u] = state[u];
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_small_enc.c b/test/monniaux/BearSSL/src/symcipher/aes_small_enc.c
new file mode 100644
index 00000000..29f48a8f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_small_enc.c
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#define S br_aes_S
+
+static void
+add_round_key(unsigned *state, const uint32_t *skeys)
+{
+ int i;
+
+ for (i = 0; i < 16; i += 4) {
+ uint32_t k;
+
+ k = *skeys ++;
+ state[i + 0] ^= (unsigned)(k >> 24);
+ state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
+ state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
+ state[i + 3] ^= (unsigned)k & 0xFF;
+ }
+}
+
+static void
+sub_bytes(unsigned *state)
+{
+ int i;
+
+ for (i = 0; i < 16; i ++) {
+ state[i] = S[state[i]];
+ }
+}
+
+static void
+shift_rows(unsigned *state)
+{
+ unsigned tmp;
+
+ tmp = state[1];
+ state[1] = state[5];
+ state[5] = state[9];
+ state[9] = state[13];
+ state[13] = tmp;
+
+ tmp = state[2];
+ state[2] = state[10];
+ state[10] = tmp;
+ tmp = state[6];
+ state[6] = state[14];
+ state[14] = tmp;
+
+ tmp = state[15];
+ state[15] = state[11];
+ state[11] = state[7];
+ state[7] = state[3];
+ state[3] = tmp;
+}
+
+static void
+mix_columns(unsigned *state)
+{
+ int i;
+
+ for (i = 0; i < 16; i += 4) {
+ unsigned s0, s1, s2, s3;
+ unsigned t0, t1, t2, t3;
+
+ s0 = state[i + 0];
+ s1 = state[i + 1];
+ s2 = state[i + 2];
+ s3 = state[i + 3];
+ t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
+ t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
+ t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
+ t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
+ state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
+ state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
+ state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
+ state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
+ }
+}
+
+/* see inner.h */
+void
+br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
+{
+ unsigned char *buf;
+ unsigned state[16];
+ unsigned u;
+
+ buf = data;
+ for (u = 0; u < 16; u ++) {
+ state[u] = buf[u];
+ }
+ add_round_key(state, skey);
+ for (u = 1; u < num_rounds; u ++) {
+ sub_bytes(state);
+ shift_rows(state);
+ mix_columns(state);
+ add_round_key(state, skey + (u << 2));
+ }
+ sub_bytes(state);
+ shift_rows(state);
+ add_round_key(state, skey + (num_rounds << 2));
+ for (u = 0; u < 16; u ++) {
+ buf[u] = state[u];
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_x86ni.c b/test/monniaux/BearSSL/src/symcipher/aes_x86ni.c
new file mode 100644
index 00000000..d5408f13
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_x86ni.c
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+/*
+ * This code contains the AES key schedule implementation using the
+ * AES-NI opcodes.
+ */
+
+#if BR_AES_X86NI
+
+/* see inner.h */
+int
+br_aes_x86ni_supported(void)
+{
+ /*
+ * Bit mask for features in ECX:
+ * 19 SSE4.1 (used for _mm_insert_epi32(), for AES-CTR)
+ * 25 AES-NI
+ */
+ return br_cpuid(0, 0, 0x02080000, 0);
+}
+
+BR_TARGETS_X86_UP
+
+BR_TARGET("sse2,aes")
+static inline __m128i
+expand_step128(__m128i k, __m128i k2)
+{
+ k = _mm_xor_si128(k, _mm_slli_si128(k, 4));
+ k = _mm_xor_si128(k, _mm_slli_si128(k, 4));
+ k = _mm_xor_si128(k, _mm_slli_si128(k, 4));
+ k2 = _mm_shuffle_epi32(k2, 0xFF);
+ return _mm_xor_si128(k, k2);
+}
+
+BR_TARGET("sse2,aes")
+static inline void
+expand_step192(__m128i *t1, __m128i *t2, __m128i *t3)
+{
+ __m128i t4;
+
+ *t2 = _mm_shuffle_epi32(*t2, 0x55);
+ t4 = _mm_slli_si128(*t1, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ *t1 = _mm_xor_si128(*t1, *t2);
+ *t2 = _mm_shuffle_epi32(*t1, 0xFF);
+ t4 = _mm_slli_si128(*t3, 0x4);
+ *t3 = _mm_xor_si128(*t3, t4);
+ *t3 = _mm_xor_si128(*t3, *t2);
+}
+
+BR_TARGET("sse2,aes")
+static inline void
+expand_step256_1(__m128i *t1, __m128i *t2)
+{
+ __m128i t4;
+
+ *t2 = _mm_shuffle_epi32(*t2, 0xFF);
+ t4 = _mm_slli_si128(*t1, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t1 = _mm_xor_si128(*t1, t4);
+ *t1 = _mm_xor_si128(*t1, *t2);
+}
+
+BR_TARGET("sse2,aes")
+static inline void
+expand_step256_2(__m128i *t1, __m128i *t3)
+{
+ __m128i t2, t4;
+
+ t4 = _mm_aeskeygenassist_si128(*t1, 0x0);
+ t2 = _mm_shuffle_epi32(t4, 0xAA);
+ t4 = _mm_slli_si128(*t3, 0x4);
+ *t3 = _mm_xor_si128(*t3, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t3 = _mm_xor_si128(*t3, t4);
+ t4 = _mm_slli_si128(t4, 0x4);
+ *t3 = _mm_xor_si128(*t3, t4);
+ *t3 = _mm_xor_si128(*t3, t2);
+}
+
+/*
+ * Perform key schedule for AES, encryption direction. Subkeys are written
+ * in sk[], and the number of rounds is returned. Key length MUST be 16,
+ * 24 or 32 bytes.
+ */
+BR_TARGET("sse2,aes")
+static unsigned
+x86ni_keysched(__m128i *sk, const void *key, size_t len)
+{
+ const unsigned char *kb;
+
+#define KEXP128(k, i, rcon) do { \
+ k = expand_step128(k, _mm_aeskeygenassist_si128(k, rcon)); \
+ sk[i] = k; \
+ } while (0)
+
+#define KEXP192(i, rcon1, rcon2) do { \
+ sk[(i) + 0] = t1; \
+ sk[(i) + 1] = t3; \
+ t2 = _mm_aeskeygenassist_si128(t3, rcon1); \
+ expand_step192(&t1, &t2, &t3); \
+ sk[(i) + 1] = _mm_castpd_si128(_mm_shuffle_pd( \
+ _mm_castsi128_pd(sk[(i) + 1]), \
+ _mm_castsi128_pd(t1), 0)); \
+ sk[(i) + 2] = _mm_castpd_si128(_mm_shuffle_pd( \
+ _mm_castsi128_pd(t1), \
+ _mm_castsi128_pd(t3), 1)); \
+ t2 = _mm_aeskeygenassist_si128(t3, rcon2); \
+ expand_step192(&t1, &t2, &t3); \
+ } while (0)
+
+#define KEXP256(i, rcon) do { \
+ sk[(i) + 0] = t3; \
+ t2 = _mm_aeskeygenassist_si128(t3, rcon); \
+ expand_step256_1(&t1, &t2); \
+ sk[(i) + 1] = t1; \
+ expand_step256_2(&t1, &t3); \
+ } while (0)
+
+ kb = key;
+ switch (len) {
+ __m128i t1, t2, t3;
+
+ case 16:
+ t1 = _mm_loadu_si128((const void *)kb);
+ sk[0] = t1;
+ KEXP128(t1, 1, 0x01);
+ KEXP128(t1, 2, 0x02);
+ KEXP128(t1, 3, 0x04);
+ KEXP128(t1, 4, 0x08);
+ KEXP128(t1, 5, 0x10);
+ KEXP128(t1, 6, 0x20);
+ KEXP128(t1, 7, 0x40);
+ KEXP128(t1, 8, 0x80);
+ KEXP128(t1, 9, 0x1B);
+ KEXP128(t1, 10, 0x36);
+ return 10;
+
+ case 24:
+ t1 = _mm_loadu_si128((const void *)kb);
+ t3 = _mm_loadu_si128((const void *)(kb + 8));
+ t3 = _mm_shuffle_epi32(t3, 0x4E);
+ KEXP192(0, 0x01, 0x02);
+ KEXP192(3, 0x04, 0x08);
+ KEXP192(6, 0x10, 0x20);
+ KEXP192(9, 0x40, 0x80);
+ sk[12] = t1;
+ return 12;
+
+ case 32:
+ t1 = _mm_loadu_si128((const void *)kb);
+ t3 = _mm_loadu_si128((const void *)(kb + 16));
+ sk[0] = t1;
+ KEXP256( 1, 0x01);
+ KEXP256( 3, 0x02);
+ KEXP256( 5, 0x04);
+ KEXP256( 7, 0x08);
+ KEXP256( 9, 0x10);
+ KEXP256(11, 0x20);
+ sk[13] = t3;
+ t2 = _mm_aeskeygenassist_si128(t3, 0x40);
+ expand_step256_1(&t1, &t2);
+ sk[14] = t1;
+ return 14;
+
+ default:
+ return 0;
+ }
+
+#undef KEXP128
+#undef KEXP192
+#undef KEXP256
+}
+
+/* see inner.h */
+BR_TARGET("sse2,aes")
+unsigned
+br_aes_x86ni_keysched_enc(unsigned char *skni, const void *key, size_t len)
+{
+ __m128i sk[15];
+ unsigned num_rounds;
+
+ num_rounds = x86ni_keysched(sk, key, len);
+ memcpy(skni, sk, (num_rounds + 1) << 4);
+ return num_rounds;
+}
+
+/* see inner.h */
+BR_TARGET("sse2,aes")
+unsigned
+br_aes_x86ni_keysched_dec(unsigned char *skni, const void *key, size_t len)
+{
+ __m128i sk[15];
+ unsigned u, num_rounds;
+
+ num_rounds = x86ni_keysched(sk, key, len);
+ _mm_storeu_si128((void *)skni, sk[num_rounds]);
+ for (u = 1; u < num_rounds; u ++) {
+ _mm_storeu_si128((void *)(skni + (u << 4)),
+ _mm_aesimc_si128(sk[num_rounds - u]));
+ }
+ _mm_storeu_si128((void *)(skni + (num_rounds << 4)), sk[0]);
+ return num_rounds;
+}
+
+BR_TARGETS_X86_DOWN
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcdec.c
new file mode 100644
index 00000000..862b1b5b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcdec.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_AES_X86NI
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class *
+br_aes_x86ni_cbcdec_get_vtable(void)
+{
+ return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcdec_vtable : NULL;
+}
+
+/* see bearssl_block.h */
+void
+br_aes_x86ni_cbcdec_init(br_aes_x86ni_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_x86ni_cbcdec_vtable;
+ ctx->num_rounds = br_aes_x86ni_keysched_dec(ctx->skey.skni, key, len);
+}
+
+BR_TARGETS_X86_UP
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,aes")
+void
+br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15], ivx;
+ unsigned u;
+
+ buf = data;
+ ivx = _mm_loadu_si128(iv);
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+ while (len > 0) {
+ __m128i x0, x1, x2, x3, e0, e1, e2, e3;
+
+ x0 = _mm_loadu_si128((void *)(buf + 0));
+ if (len >= 64) {
+ x1 = _mm_loadu_si128((void *)(buf + 16));
+ x2 = _mm_loadu_si128((void *)(buf + 32));
+ x3 = _mm_loadu_si128((void *)(buf + 48));
+ } else {
+ x0 = _mm_loadu_si128((void *)(buf + 0));
+ if (len >= 32) {
+ x1 = _mm_loadu_si128((void *)(buf + 16));
+ if (len >= 48) {
+ x2 = _mm_loadu_si128(
+ (void *)(buf + 32));
+ x3 = x2;
+ } else {
+ x2 = x0;
+ x3 = x1;
+ }
+ } else {
+ x1 = x0;
+ x2 = x0;
+ x3 = x0;
+ }
+ }
+ e0 = x0;
+ e1 = x1;
+ e2 = x2;
+ e3 = x3;
+ x0 = _mm_xor_si128(x0, sk[0]);
+ x1 = _mm_xor_si128(x1, sk[0]);
+ x2 = _mm_xor_si128(x2, sk[0]);
+ x3 = _mm_xor_si128(x3, sk[0]);
+ x0 = _mm_aesdec_si128(x0, sk[1]);
+ x1 = _mm_aesdec_si128(x1, sk[1]);
+ x2 = _mm_aesdec_si128(x2, sk[1]);
+ x3 = _mm_aesdec_si128(x3, sk[1]);
+ x0 = _mm_aesdec_si128(x0, sk[2]);
+ x1 = _mm_aesdec_si128(x1, sk[2]);
+ x2 = _mm_aesdec_si128(x2, sk[2]);
+ x3 = _mm_aesdec_si128(x3, sk[2]);
+ x0 = _mm_aesdec_si128(x0, sk[3]);
+ x1 = _mm_aesdec_si128(x1, sk[3]);
+ x2 = _mm_aesdec_si128(x2, sk[3]);
+ x3 = _mm_aesdec_si128(x3, sk[3]);
+ x0 = _mm_aesdec_si128(x0, sk[4]);
+ x1 = _mm_aesdec_si128(x1, sk[4]);
+ x2 = _mm_aesdec_si128(x2, sk[4]);
+ x3 = _mm_aesdec_si128(x3, sk[4]);
+ x0 = _mm_aesdec_si128(x0, sk[5]);
+ x1 = _mm_aesdec_si128(x1, sk[5]);
+ x2 = _mm_aesdec_si128(x2, sk[5]);
+ x3 = _mm_aesdec_si128(x3, sk[5]);
+ x0 = _mm_aesdec_si128(x0, sk[6]);
+ x1 = _mm_aesdec_si128(x1, sk[6]);
+ x2 = _mm_aesdec_si128(x2, sk[6]);
+ x3 = _mm_aesdec_si128(x3, sk[6]);
+ x0 = _mm_aesdec_si128(x0, sk[7]);
+ x1 = _mm_aesdec_si128(x1, sk[7]);
+ x2 = _mm_aesdec_si128(x2, sk[7]);
+ x3 = _mm_aesdec_si128(x3, sk[7]);
+ x0 = _mm_aesdec_si128(x0, sk[8]);
+ x1 = _mm_aesdec_si128(x1, sk[8]);
+ x2 = _mm_aesdec_si128(x2, sk[8]);
+ x3 = _mm_aesdec_si128(x3, sk[8]);
+ x0 = _mm_aesdec_si128(x0, sk[9]);
+ x1 = _mm_aesdec_si128(x1, sk[9]);
+ x2 = _mm_aesdec_si128(x2, sk[9]);
+ x3 = _mm_aesdec_si128(x3, sk[9]);
+ if (num_rounds == 10) {
+ x0 = _mm_aesdeclast_si128(x0, sk[10]);
+ x1 = _mm_aesdeclast_si128(x1, sk[10]);
+ x2 = _mm_aesdeclast_si128(x2, sk[10]);
+ x3 = _mm_aesdeclast_si128(x3, sk[10]);
+ } else if (num_rounds == 12) {
+ x0 = _mm_aesdec_si128(x0, sk[10]);
+ x1 = _mm_aesdec_si128(x1, sk[10]);
+ x2 = _mm_aesdec_si128(x2, sk[10]);
+ x3 = _mm_aesdec_si128(x3, sk[10]);
+ x0 = _mm_aesdec_si128(x0, sk[11]);
+ x1 = _mm_aesdec_si128(x1, sk[11]);
+ x2 = _mm_aesdec_si128(x2, sk[11]);
+ x3 = _mm_aesdec_si128(x3, sk[11]);
+ x0 = _mm_aesdeclast_si128(x0, sk[12]);
+ x1 = _mm_aesdeclast_si128(x1, sk[12]);
+ x2 = _mm_aesdeclast_si128(x2, sk[12]);
+ x3 = _mm_aesdeclast_si128(x3, sk[12]);
+ } else {
+ x0 = _mm_aesdec_si128(x0, sk[10]);
+ x1 = _mm_aesdec_si128(x1, sk[10]);
+ x2 = _mm_aesdec_si128(x2, sk[10]);
+ x3 = _mm_aesdec_si128(x3, sk[10]);
+ x0 = _mm_aesdec_si128(x0, sk[11]);
+ x1 = _mm_aesdec_si128(x1, sk[11]);
+ x2 = _mm_aesdec_si128(x2, sk[11]);
+ x3 = _mm_aesdec_si128(x3, sk[11]);
+ x0 = _mm_aesdec_si128(x0, sk[12]);
+ x1 = _mm_aesdec_si128(x1, sk[12]);
+ x2 = _mm_aesdec_si128(x2, sk[12]);
+ x3 = _mm_aesdec_si128(x3, sk[12]);
+ x0 = _mm_aesdec_si128(x0, sk[13]);
+ x1 = _mm_aesdec_si128(x1, sk[13]);
+ x2 = _mm_aesdec_si128(x2, sk[13]);
+ x3 = _mm_aesdec_si128(x3, sk[13]);
+ x0 = _mm_aesdeclast_si128(x0, sk[14]);
+ x1 = _mm_aesdeclast_si128(x1, sk[14]);
+ x2 = _mm_aesdeclast_si128(x2, sk[14]);
+ x3 = _mm_aesdeclast_si128(x3, sk[14]);
+ }
+ x0 = _mm_xor_si128(x0, ivx);
+ x1 = _mm_xor_si128(x1, e0);
+ x2 = _mm_xor_si128(x2, e1);
+ x3 = _mm_xor_si128(x3, e2);
+ ivx = e3;
+ _mm_storeu_si128((void *)(buf + 0), x0);
+ if (len >= 64) {
+ _mm_storeu_si128((void *)(buf + 16), x1);
+ _mm_storeu_si128((void *)(buf + 32), x2);
+ _mm_storeu_si128((void *)(buf + 48), x3);
+ buf += 64;
+ len -= 64;
+ } else {
+ if (len >= 32) {
+ _mm_storeu_si128((void *)(buf + 16), x1);
+ if (len >= 48) {
+ _mm_storeu_si128(
+ (void *)(buf + 32), x2);
+ }
+ }
+ break;
+ }
+ }
+ _mm_storeu_si128(iv, ivx);
+}
+
+BR_TARGETS_X86_DOWN
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable = {
+ sizeof(br_aes_x86ni_cbcdec_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_aes_x86ni_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_aes_x86ni_cbcdec_run
+};
+
+#else
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class *
+br_aes_x86ni_cbcdec_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcenc.c
new file mode 100644
index 00000000..85feecdb
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_cbcenc.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_AES_X86NI
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class *
+br_aes_x86ni_cbcenc_get_vtable(void)
+{
+ return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcenc_vtable : NULL;
+}
+
+/* see bearssl_block.h */
+void
+br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_x86ni_cbcenc_vtable;
+ ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
+}
+
+BR_TARGETS_X86_UP
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,aes")
+void
+br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15], ivx;
+ unsigned u;
+
+ buf = data;
+ ivx = _mm_loadu_si128(iv);
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+ while (len > 0) {
+ __m128i x;
+
+ x = _mm_xor_si128(_mm_loadu_si128((void *)buf), ivx);
+ x = _mm_xor_si128(x, sk[0]);
+ x = _mm_aesenc_si128(x, sk[1]);
+ x = _mm_aesenc_si128(x, sk[2]);
+ x = _mm_aesenc_si128(x, sk[3]);
+ x = _mm_aesenc_si128(x, sk[4]);
+ x = _mm_aesenc_si128(x, sk[5]);
+ x = _mm_aesenc_si128(x, sk[6]);
+ x = _mm_aesenc_si128(x, sk[7]);
+ x = _mm_aesenc_si128(x, sk[8]);
+ x = _mm_aesenc_si128(x, sk[9]);
+ if (num_rounds == 10) {
+ x = _mm_aesenclast_si128(x, sk[10]);
+ } else if (num_rounds == 12) {
+ x = _mm_aesenc_si128(x, sk[10]);
+ x = _mm_aesenc_si128(x, sk[11]);
+ x = _mm_aesenclast_si128(x, sk[12]);
+ } else {
+ x = _mm_aesenc_si128(x, sk[10]);
+ x = _mm_aesenc_si128(x, sk[11]);
+ x = _mm_aesenc_si128(x, sk[12]);
+ x = _mm_aesenc_si128(x, sk[13]);
+ x = _mm_aesenclast_si128(x, sk[14]);
+ }
+ ivx = x;
+ _mm_storeu_si128((void *)buf, x);
+ buf += 16;
+ len -= 16;
+ }
+ _mm_storeu_si128(iv, ivx);
+}
+
+BR_TARGETS_X86_DOWN
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable = {
+ sizeof(br_aes_x86ni_cbcenc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_aes_x86ni_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_aes_x86ni_cbcenc_run
+};
+
+#else
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class *
+br_aes_x86ni_cbcenc_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctr.c b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctr.c
new file mode 100644
index 00000000..1cddd606
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctr.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_AES_X86NI
+
+/* see bearssl_block.h */
+const br_block_ctr_class *
+br_aes_x86ni_ctr_get_vtable(void)
+{
+ return br_aes_x86ni_supported() ? &br_aes_x86ni_ctr_vtable : NULL;
+}
+
+/* see bearssl_block.h */
+void
+br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_x86ni_ctr_vtable;
+ ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
+}
+
+BR_TARGETS_X86_UP
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,sse4.1,aes")
+uint32_t
+br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys *ctx,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned char ivbuf[16];
+ unsigned num_rounds;
+ __m128i sk[15];
+ __m128i ivx;
+ unsigned u;
+
+ buf = data;
+ memcpy(ivbuf, iv, 12);
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+ ivx = _mm_loadu_si128((void *)ivbuf);
+ while (len > 0) {
+ __m128i x0, x1, x2, x3;
+
+ x0 = _mm_insert_epi32(ivx, br_bswap32(cc + 0), 3);
+ x1 = _mm_insert_epi32(ivx, br_bswap32(cc + 1), 3);
+ x2 = _mm_insert_epi32(ivx, br_bswap32(cc + 2), 3);
+ x3 = _mm_insert_epi32(ivx, br_bswap32(cc + 3), 3);
+ x0 = _mm_xor_si128(x0, sk[0]);
+ x1 = _mm_xor_si128(x1, sk[0]);
+ x2 = _mm_xor_si128(x2, sk[0]);
+ x3 = _mm_xor_si128(x3, sk[0]);
+ x0 = _mm_aesenc_si128(x0, sk[1]);
+ x1 = _mm_aesenc_si128(x1, sk[1]);
+ x2 = _mm_aesenc_si128(x2, sk[1]);
+ x3 = _mm_aesenc_si128(x3, sk[1]);
+ x0 = _mm_aesenc_si128(x0, sk[2]);
+ x1 = _mm_aesenc_si128(x1, sk[2]);
+ x2 = _mm_aesenc_si128(x2, sk[2]);
+ x3 = _mm_aesenc_si128(x3, sk[2]);
+ x0 = _mm_aesenc_si128(x0, sk[3]);
+ x1 = _mm_aesenc_si128(x1, sk[3]);
+ x2 = _mm_aesenc_si128(x2, sk[3]);
+ x3 = _mm_aesenc_si128(x3, sk[3]);
+ x0 = _mm_aesenc_si128(x0, sk[4]);
+ x1 = _mm_aesenc_si128(x1, sk[4]);
+ x2 = _mm_aesenc_si128(x2, sk[4]);
+ x3 = _mm_aesenc_si128(x3, sk[4]);
+ x0 = _mm_aesenc_si128(x0, sk[5]);
+ x1 = _mm_aesenc_si128(x1, sk[5]);
+ x2 = _mm_aesenc_si128(x2, sk[5]);
+ x3 = _mm_aesenc_si128(x3, sk[5]);
+ x0 = _mm_aesenc_si128(x0, sk[6]);
+ x1 = _mm_aesenc_si128(x1, sk[6]);
+ x2 = _mm_aesenc_si128(x2, sk[6]);
+ x3 = _mm_aesenc_si128(x3, sk[6]);
+ x0 = _mm_aesenc_si128(x0, sk[7]);
+ x1 = _mm_aesenc_si128(x1, sk[7]);
+ x2 = _mm_aesenc_si128(x2, sk[7]);
+ x3 = _mm_aesenc_si128(x3, sk[7]);
+ x0 = _mm_aesenc_si128(x0, sk[8]);
+ x1 = _mm_aesenc_si128(x1, sk[8]);
+ x2 = _mm_aesenc_si128(x2, sk[8]);
+ x3 = _mm_aesenc_si128(x3, sk[8]);
+ x0 = _mm_aesenc_si128(x0, sk[9]);
+ x1 = _mm_aesenc_si128(x1, sk[9]);
+ x2 = _mm_aesenc_si128(x2, sk[9]);
+ x3 = _mm_aesenc_si128(x3, sk[9]);
+ if (num_rounds == 10) {
+ x0 = _mm_aesenclast_si128(x0, sk[10]);
+ x1 = _mm_aesenclast_si128(x1, sk[10]);
+ x2 = _mm_aesenclast_si128(x2, sk[10]);
+ x3 = _mm_aesenclast_si128(x3, sk[10]);
+ } else if (num_rounds == 12) {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x2 = _mm_aesenc_si128(x2, sk[10]);
+ x3 = _mm_aesenc_si128(x3, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x2 = _mm_aesenc_si128(x2, sk[11]);
+ x3 = _mm_aesenc_si128(x3, sk[11]);
+ x0 = _mm_aesenclast_si128(x0, sk[12]);
+ x1 = _mm_aesenclast_si128(x1, sk[12]);
+ x2 = _mm_aesenclast_si128(x2, sk[12]);
+ x3 = _mm_aesenclast_si128(x3, sk[12]);
+ } else {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x2 = _mm_aesenc_si128(x2, sk[10]);
+ x3 = _mm_aesenc_si128(x3, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x2 = _mm_aesenc_si128(x2, sk[11]);
+ x3 = _mm_aesenc_si128(x3, sk[11]);
+ x0 = _mm_aesenc_si128(x0, sk[12]);
+ x1 = _mm_aesenc_si128(x1, sk[12]);
+ x2 = _mm_aesenc_si128(x2, sk[12]);
+ x3 = _mm_aesenc_si128(x3, sk[12]);
+ x0 = _mm_aesenc_si128(x0, sk[13]);
+ x1 = _mm_aesenc_si128(x1, sk[13]);
+ x2 = _mm_aesenc_si128(x2, sk[13]);
+ x3 = _mm_aesenc_si128(x3, sk[13]);
+ x0 = _mm_aesenclast_si128(x0, sk[14]);
+ x1 = _mm_aesenclast_si128(x1, sk[14]);
+ x2 = _mm_aesenclast_si128(x2, sk[14]);
+ x3 = _mm_aesenclast_si128(x3, sk[14]);
+ }
+ if (len >= 64) {
+ x0 = _mm_xor_si128(x0,
+ _mm_loadu_si128((void *)(buf + 0)));
+ x1 = _mm_xor_si128(x1,
+ _mm_loadu_si128((void *)(buf + 16)));
+ x2 = _mm_xor_si128(x2,
+ _mm_loadu_si128((void *)(buf + 32)));
+ x3 = _mm_xor_si128(x3,
+ _mm_loadu_si128((void *)(buf + 48)));
+ _mm_storeu_si128((void *)(buf + 0), x0);
+ _mm_storeu_si128((void *)(buf + 16), x1);
+ _mm_storeu_si128((void *)(buf + 32), x2);
+ _mm_storeu_si128((void *)(buf + 48), x3);
+ buf += 64;
+ len -= 64;
+ cc += 4;
+ } else {
+ unsigned char tmp[64];
+
+ _mm_storeu_si128((void *)(tmp + 0), x0);
+ _mm_storeu_si128((void *)(tmp + 16), x1);
+ _mm_storeu_si128((void *)(tmp + 32), x2);
+ _mm_storeu_si128((void *)(tmp + 48), x3);
+ for (u = 0; u < len; u ++) {
+ buf[u] ^= tmp[u];
+ }
+ cc += (uint32_t)len >> 4;
+ break;
+ }
+ }
+ return cc;
+}
+
+BR_TARGETS_X86_DOWN
+
+/* see bearssl_block.h */
+const br_block_ctr_class br_aes_x86ni_ctr_vtable = {
+ sizeof(br_aes_x86ni_ctr_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctr_class **, const void *, size_t))
+ &br_aes_x86ni_ctr_init,
+ (uint32_t (*)(const br_block_ctr_class *const *,
+ const void *, uint32_t, void *, size_t))
+ &br_aes_x86ni_ctr_run
+};
+
+#else
+
+/* see bearssl_block.h */
+const br_block_ctr_class *
+br_aes_x86ni_ctr_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctrcbc.c b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctrcbc.c
new file mode 100644
index 00000000..f57fead6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/aes_x86ni_ctrcbc.c
@@ -0,0 +1,596 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_AES_X86NI
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class *
+br_aes_x86ni_ctrcbc_get_vtable(void)
+{
+ return br_aes_x86ni_supported() ? &br_aes_x86ni_ctrcbc_vtable : NULL;
+}
+
+/* see bearssl_block.h */
+void
+br_aes_x86ni_ctrcbc_init(br_aes_x86ni_ctrcbc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_aes_x86ni_ctrcbc_vtable;
+ ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
+}
+
+BR_TARGETS_X86_UP
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,sse4.1,aes")
+void
+br_aes_x86ni_ctrcbc_ctr(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15];
+ __m128i ivx0, ivx1, ivx2, ivx3;
+ __m128i erev, zero, one, four, notthree;
+ unsigned u;
+
+ buf = data;
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+
+ /*
+ * Some SSE2 constants.
+ */
+ erev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15);
+ zero = _mm_setzero_si128();
+ one = _mm_set_epi64x(0, 1);
+ four = _mm_set_epi64x(0, 4);
+ notthree = _mm_sub_epi64(zero, four);
+
+ /*
+ * Decode the counter in big-endian and pre-increment the other
+ * three counters.
+ */
+ ivx0 = _mm_shuffle_epi8(_mm_loadu_si128((void *)ctr), erev);
+ ivx1 = _mm_add_epi64(ivx0, one);
+ ivx1 = _mm_sub_epi64(ivx1,
+ _mm_slli_si128(_mm_cmpeq_epi64(ivx1, zero), 8));
+ ivx2 = _mm_add_epi64(ivx1, one);
+ ivx2 = _mm_sub_epi64(ivx2,
+ _mm_slli_si128(_mm_cmpeq_epi64(ivx2, zero), 8));
+ ivx3 = _mm_add_epi64(ivx2, one);
+ ivx3 = _mm_sub_epi64(ivx3,
+ _mm_slli_si128(_mm_cmpeq_epi64(ivx3, zero), 8));
+ while (len > 0) {
+ __m128i x0, x1, x2, x3;
+
+ /*
+ * Load counter values; we need to byteswap them because
+ * the specification says that they use big-endian.
+ */
+ x0 = _mm_shuffle_epi8(ivx0, erev);
+ x1 = _mm_shuffle_epi8(ivx1, erev);
+ x2 = _mm_shuffle_epi8(ivx2, erev);
+ x3 = _mm_shuffle_epi8(ivx3, erev);
+
+ x0 = _mm_xor_si128(x0, sk[0]);
+ x1 = _mm_xor_si128(x1, sk[0]);
+ x2 = _mm_xor_si128(x2, sk[0]);
+ x3 = _mm_xor_si128(x3, sk[0]);
+ x0 = _mm_aesenc_si128(x0, sk[1]);
+ x1 = _mm_aesenc_si128(x1, sk[1]);
+ x2 = _mm_aesenc_si128(x2, sk[1]);
+ x3 = _mm_aesenc_si128(x3, sk[1]);
+ x0 = _mm_aesenc_si128(x0, sk[2]);
+ x1 = _mm_aesenc_si128(x1, sk[2]);
+ x2 = _mm_aesenc_si128(x2, sk[2]);
+ x3 = _mm_aesenc_si128(x3, sk[2]);
+ x0 = _mm_aesenc_si128(x0, sk[3]);
+ x1 = _mm_aesenc_si128(x1, sk[3]);
+ x2 = _mm_aesenc_si128(x2, sk[3]);
+ x3 = _mm_aesenc_si128(x3, sk[3]);
+ x0 = _mm_aesenc_si128(x0, sk[4]);
+ x1 = _mm_aesenc_si128(x1, sk[4]);
+ x2 = _mm_aesenc_si128(x2, sk[4]);
+ x3 = _mm_aesenc_si128(x3, sk[4]);
+ x0 = _mm_aesenc_si128(x0, sk[5]);
+ x1 = _mm_aesenc_si128(x1, sk[5]);
+ x2 = _mm_aesenc_si128(x2, sk[5]);
+ x3 = _mm_aesenc_si128(x3, sk[5]);
+ x0 = _mm_aesenc_si128(x0, sk[6]);
+ x1 = _mm_aesenc_si128(x1, sk[6]);
+ x2 = _mm_aesenc_si128(x2, sk[6]);
+ x3 = _mm_aesenc_si128(x3, sk[6]);
+ x0 = _mm_aesenc_si128(x0, sk[7]);
+ x1 = _mm_aesenc_si128(x1, sk[7]);
+ x2 = _mm_aesenc_si128(x2, sk[7]);
+ x3 = _mm_aesenc_si128(x3, sk[7]);
+ x0 = _mm_aesenc_si128(x0, sk[8]);
+ x1 = _mm_aesenc_si128(x1, sk[8]);
+ x2 = _mm_aesenc_si128(x2, sk[8]);
+ x3 = _mm_aesenc_si128(x3, sk[8]);
+ x0 = _mm_aesenc_si128(x0, sk[9]);
+ x1 = _mm_aesenc_si128(x1, sk[9]);
+ x2 = _mm_aesenc_si128(x2, sk[9]);
+ x3 = _mm_aesenc_si128(x3, sk[9]);
+ if (num_rounds == 10) {
+ x0 = _mm_aesenclast_si128(x0, sk[10]);
+ x1 = _mm_aesenclast_si128(x1, sk[10]);
+ x2 = _mm_aesenclast_si128(x2, sk[10]);
+ x3 = _mm_aesenclast_si128(x3, sk[10]);
+ } else if (num_rounds == 12) {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x2 = _mm_aesenc_si128(x2, sk[10]);
+ x3 = _mm_aesenc_si128(x3, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x2 = _mm_aesenc_si128(x2, sk[11]);
+ x3 = _mm_aesenc_si128(x3, sk[11]);
+ x0 = _mm_aesenclast_si128(x0, sk[12]);
+ x1 = _mm_aesenclast_si128(x1, sk[12]);
+ x2 = _mm_aesenclast_si128(x2, sk[12]);
+ x3 = _mm_aesenclast_si128(x3, sk[12]);
+ } else {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x2 = _mm_aesenc_si128(x2, sk[10]);
+ x3 = _mm_aesenc_si128(x3, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x2 = _mm_aesenc_si128(x2, sk[11]);
+ x3 = _mm_aesenc_si128(x3, sk[11]);
+ x0 = _mm_aesenc_si128(x0, sk[12]);
+ x1 = _mm_aesenc_si128(x1, sk[12]);
+ x2 = _mm_aesenc_si128(x2, sk[12]);
+ x3 = _mm_aesenc_si128(x3, sk[12]);
+ x0 = _mm_aesenc_si128(x0, sk[13]);
+ x1 = _mm_aesenc_si128(x1, sk[13]);
+ x2 = _mm_aesenc_si128(x2, sk[13]);
+ x3 = _mm_aesenc_si128(x3, sk[13]);
+ x0 = _mm_aesenclast_si128(x0, sk[14]);
+ x1 = _mm_aesenclast_si128(x1, sk[14]);
+ x2 = _mm_aesenclast_si128(x2, sk[14]);
+ x3 = _mm_aesenclast_si128(x3, sk[14]);
+ }
+ if (len >= 64) {
+ x0 = _mm_xor_si128(x0,
+ _mm_loadu_si128((void *)(buf + 0)));
+ x1 = _mm_xor_si128(x1,
+ _mm_loadu_si128((void *)(buf + 16)));
+ x2 = _mm_xor_si128(x2,
+ _mm_loadu_si128((void *)(buf + 32)));
+ x3 = _mm_xor_si128(x3,
+ _mm_loadu_si128((void *)(buf + 48)));
+ _mm_storeu_si128((void *)(buf + 0), x0);
+ _mm_storeu_si128((void *)(buf + 16), x1);
+ _mm_storeu_si128((void *)(buf + 32), x2);
+ _mm_storeu_si128((void *)(buf + 48), x3);
+ buf += 64;
+ len -= 64;
+ } else {
+ unsigned char tmp[64];
+
+ _mm_storeu_si128((void *)(tmp + 0), x0);
+ _mm_storeu_si128((void *)(tmp + 16), x1);
+ _mm_storeu_si128((void *)(tmp + 32), x2);
+ _mm_storeu_si128((void *)(tmp + 48), x3);
+ for (u = 0; u < len; u ++) {
+ buf[u] ^= tmp[u];
+ }
+ switch (len) {
+ case 16:
+ ivx0 = ivx1;
+ break;
+ case 32:
+ ivx0 = ivx2;
+ break;
+ case 48:
+ ivx0 = ivx3;
+ break;
+ }
+ break;
+ }
+
+ /*
+ * Add 4 to each counter value. For carry propagation
+ * into the upper 64-bit words, we would need to compare
+ * the results with 4, but SSE2+ has only _signed_
+ * comparisons. Instead, we mask out the low two bits,
+ * and check whether the remaining bits are zero.
+ */
+ ivx0 = _mm_add_epi64(ivx0, four);
+ ivx1 = _mm_add_epi64(ivx1, four);
+ ivx2 = _mm_add_epi64(ivx2, four);
+ ivx3 = _mm_add_epi64(ivx3, four);
+ ivx0 = _mm_sub_epi64(ivx0,
+ _mm_slli_si128(_mm_cmpeq_epi64(
+ _mm_and_si128(ivx0, notthree), zero), 8));
+ ivx1 = _mm_sub_epi64(ivx1,
+ _mm_slli_si128(_mm_cmpeq_epi64(
+ _mm_and_si128(ivx1, notthree), zero), 8));
+ ivx2 = _mm_sub_epi64(ivx2,
+ _mm_slli_si128(_mm_cmpeq_epi64(
+ _mm_and_si128(ivx2, notthree), zero), 8));
+ ivx3 = _mm_sub_epi64(ivx3,
+ _mm_slli_si128(_mm_cmpeq_epi64(
+ _mm_and_si128(ivx3, notthree), zero), 8));
+ }
+
+ /*
+ * Write back new counter value. The loop took care to put the
+ * right counter value in ivx0.
+ */
+ _mm_storeu_si128((void *)ctr, _mm_shuffle_epi8(ivx0, erev));
+}
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,sse4.1,aes")
+void
+br_aes_x86ni_ctrcbc_mac(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *cbcmac, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15], ivx;
+ unsigned u;
+
+ buf = data;
+ ivx = _mm_loadu_si128(cbcmac);
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+ while (len > 0) {
+ __m128i x;
+
+ x = _mm_xor_si128(_mm_loadu_si128((void *)buf), ivx);
+ x = _mm_xor_si128(x, sk[0]);
+ x = _mm_aesenc_si128(x, sk[1]);
+ x = _mm_aesenc_si128(x, sk[2]);
+ x = _mm_aesenc_si128(x, sk[3]);
+ x = _mm_aesenc_si128(x, sk[4]);
+ x = _mm_aesenc_si128(x, sk[5]);
+ x = _mm_aesenc_si128(x, sk[6]);
+ x = _mm_aesenc_si128(x, sk[7]);
+ x = _mm_aesenc_si128(x, sk[8]);
+ x = _mm_aesenc_si128(x, sk[9]);
+ if (num_rounds == 10) {
+ x = _mm_aesenclast_si128(x, sk[10]);
+ } else if (num_rounds == 12) {
+ x = _mm_aesenc_si128(x, sk[10]);
+ x = _mm_aesenc_si128(x, sk[11]);
+ x = _mm_aesenclast_si128(x, sk[12]);
+ } else {
+ x = _mm_aesenc_si128(x, sk[10]);
+ x = _mm_aesenc_si128(x, sk[11]);
+ x = _mm_aesenc_si128(x, sk[12]);
+ x = _mm_aesenc_si128(x, sk[13]);
+ x = _mm_aesenclast_si128(x, sk[14]);
+ }
+ ivx = x;
+ buf += 16;
+ len -= 16;
+ }
+ _mm_storeu_si128(cbcmac, ivx);
+}
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,sse4.1,aes")
+void
+br_aes_x86ni_ctrcbc_encrypt(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15];
+ __m128i ivx, cmx;
+ __m128i erev, zero, one;
+ unsigned u;
+ int first_iter;
+
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+
+ /*
+ * Some SSE2 constants.
+ */
+ erev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15);
+ zero = _mm_setzero_si128();
+ one = _mm_set_epi64x(0, 1);
+
+ /*
+ * Decode the counter in big-endian.
+ */
+ ivx = _mm_shuffle_epi8(_mm_loadu_si128(ctr), erev);
+ cmx = _mm_loadu_si128(cbcmac);
+
+ buf = data;
+ first_iter = 1;
+ while (len > 0) {
+ __m128i dx, x0, x1;
+
+ /*
+ * Load initial values:
+ * dx encrypted block of data
+ * x0 counter (for CTR encryption)
+ * x1 input for CBC-MAC
+ */
+ dx = _mm_loadu_si128((void *)buf);
+ x0 = _mm_shuffle_epi8(ivx, erev);
+ x1 = cmx;
+
+ x0 = _mm_xor_si128(x0, sk[0]);
+ x1 = _mm_xor_si128(x1, sk[0]);
+ x0 = _mm_aesenc_si128(x0, sk[1]);
+ x1 = _mm_aesenc_si128(x1, sk[1]);
+ x0 = _mm_aesenc_si128(x0, sk[2]);
+ x1 = _mm_aesenc_si128(x1, sk[2]);
+ x0 = _mm_aesenc_si128(x0, sk[3]);
+ x1 = _mm_aesenc_si128(x1, sk[3]);
+ x0 = _mm_aesenc_si128(x0, sk[4]);
+ x1 = _mm_aesenc_si128(x1, sk[4]);
+ x0 = _mm_aesenc_si128(x0, sk[5]);
+ x1 = _mm_aesenc_si128(x1, sk[5]);
+ x0 = _mm_aesenc_si128(x0, sk[6]);
+ x1 = _mm_aesenc_si128(x1, sk[6]);
+ x0 = _mm_aesenc_si128(x0, sk[7]);
+ x1 = _mm_aesenc_si128(x1, sk[7]);
+ x0 = _mm_aesenc_si128(x0, sk[8]);
+ x1 = _mm_aesenc_si128(x1, sk[8]);
+ x0 = _mm_aesenc_si128(x0, sk[9]);
+ x1 = _mm_aesenc_si128(x1, sk[9]);
+ if (num_rounds == 10) {
+ x0 = _mm_aesenclast_si128(x0, sk[10]);
+ x1 = _mm_aesenclast_si128(x1, sk[10]);
+ } else if (num_rounds == 12) {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x0 = _mm_aesenclast_si128(x0, sk[12]);
+ x1 = _mm_aesenclast_si128(x1, sk[12]);
+ } else {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x0 = _mm_aesenc_si128(x0, sk[12]);
+ x1 = _mm_aesenc_si128(x1, sk[12]);
+ x0 = _mm_aesenc_si128(x0, sk[13]);
+ x1 = _mm_aesenc_si128(x1, sk[13]);
+ x0 = _mm_aesenclast_si128(x0, sk[14]);
+ x1 = _mm_aesenclast_si128(x1, sk[14]);
+ }
+
+ x0 = _mm_xor_si128(x0, dx);
+ if (first_iter) {
+ cmx = _mm_xor_si128(cmx, x0);
+ first_iter = 0;
+ } else {
+ cmx = _mm_xor_si128(x1, x0);
+ }
+ _mm_storeu_si128((void *)buf, x0);
+
+ buf += 16;
+ len -= 16;
+
+ /*
+ * Increment the counter value.
+ */
+ ivx = _mm_add_epi64(ivx, one);
+ ivx = _mm_sub_epi64(ivx,
+ _mm_slli_si128(_mm_cmpeq_epi64(ivx, zero), 8));
+
+ /*
+ * If this was the last iteration, then compute the
+ * extra block encryption to complete CBC-MAC.
+ */
+ if (len == 0) {
+ cmx = _mm_xor_si128(cmx, sk[0]);
+ cmx = _mm_aesenc_si128(cmx, sk[1]);
+ cmx = _mm_aesenc_si128(cmx, sk[2]);
+ cmx = _mm_aesenc_si128(cmx, sk[3]);
+ cmx = _mm_aesenc_si128(cmx, sk[4]);
+ cmx = _mm_aesenc_si128(cmx, sk[5]);
+ cmx = _mm_aesenc_si128(cmx, sk[6]);
+ cmx = _mm_aesenc_si128(cmx, sk[7]);
+ cmx = _mm_aesenc_si128(cmx, sk[8]);
+ cmx = _mm_aesenc_si128(cmx, sk[9]);
+ if (num_rounds == 10) {
+ cmx = _mm_aesenclast_si128(cmx, sk[10]);
+ } else if (num_rounds == 12) {
+ cmx = _mm_aesenc_si128(cmx, sk[10]);
+ cmx = _mm_aesenc_si128(cmx, sk[11]);
+ cmx = _mm_aesenclast_si128(cmx, sk[12]);
+ } else {
+ cmx = _mm_aesenc_si128(cmx, sk[10]);
+ cmx = _mm_aesenc_si128(cmx, sk[11]);
+ cmx = _mm_aesenc_si128(cmx, sk[12]);
+ cmx = _mm_aesenc_si128(cmx, sk[13]);
+ cmx = _mm_aesenclast_si128(cmx, sk[14]);
+ }
+ break;
+ }
+ }
+
+ /*
+ * Write back new counter value and CBC-MAC value.
+ */
+ _mm_storeu_si128(ctr, _mm_shuffle_epi8(ivx, erev));
+ _mm_storeu_si128(cbcmac, cmx);
+}
+
+/* see bearssl_block.h */
+BR_TARGET("sse2,sse4.1,aes")
+void
+br_aes_x86ni_ctrcbc_decrypt(const br_aes_x86ni_ctrcbc_keys *ctx,
+ void *ctr, void *cbcmac, void *data, size_t len)
+{
+ unsigned char *buf;
+ unsigned num_rounds;
+ __m128i sk[15];
+ __m128i ivx, cmx;
+ __m128i erev, zero, one;
+ unsigned u;
+
+ num_rounds = ctx->num_rounds;
+ for (u = 0; u <= num_rounds; u ++) {
+ sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
+ }
+
+ /*
+ * Some SSE2 constants.
+ */
+ erev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15);
+ zero = _mm_setzero_si128();
+ one = _mm_set_epi64x(0, 1);
+
+ /*
+ * Decode the counter in big-endian.
+ */
+ ivx = _mm_shuffle_epi8(_mm_loadu_si128(ctr), erev);
+ cmx = _mm_loadu_si128(cbcmac);
+
+ buf = data;
+ while (len > 0) {
+ __m128i dx, x0, x1;
+
+ /*
+ * Load initial values:
+ * dx encrypted block of data
+ * x0 counter (for CTR encryption)
+ * x1 input for CBC-MAC
+ */
+ dx = _mm_loadu_si128((void *)buf);
+ x0 = _mm_shuffle_epi8(ivx, erev);
+ x1 = _mm_xor_si128(cmx, dx);
+
+ x0 = _mm_xor_si128(x0, sk[0]);
+ x1 = _mm_xor_si128(x1, sk[0]);
+ x0 = _mm_aesenc_si128(x0, sk[1]);
+ x1 = _mm_aesenc_si128(x1, sk[1]);
+ x0 = _mm_aesenc_si128(x0, sk[2]);
+ x1 = _mm_aesenc_si128(x1, sk[2]);
+ x0 = _mm_aesenc_si128(x0, sk[3]);
+ x1 = _mm_aesenc_si128(x1, sk[3]);
+ x0 = _mm_aesenc_si128(x0, sk[4]);
+ x1 = _mm_aesenc_si128(x1, sk[4]);
+ x0 = _mm_aesenc_si128(x0, sk[5]);
+ x1 = _mm_aesenc_si128(x1, sk[5]);
+ x0 = _mm_aesenc_si128(x0, sk[6]);
+ x1 = _mm_aesenc_si128(x1, sk[6]);
+ x0 = _mm_aesenc_si128(x0, sk[7]);
+ x1 = _mm_aesenc_si128(x1, sk[7]);
+ x0 = _mm_aesenc_si128(x0, sk[8]);
+ x1 = _mm_aesenc_si128(x1, sk[8]);
+ x0 = _mm_aesenc_si128(x0, sk[9]);
+ x1 = _mm_aesenc_si128(x1, sk[9]);
+ if (num_rounds == 10) {
+ x0 = _mm_aesenclast_si128(x0, sk[10]);
+ x1 = _mm_aesenclast_si128(x1, sk[10]);
+ } else if (num_rounds == 12) {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x0 = _mm_aesenclast_si128(x0, sk[12]);
+ x1 = _mm_aesenclast_si128(x1, sk[12]);
+ } else {
+ x0 = _mm_aesenc_si128(x0, sk[10]);
+ x1 = _mm_aesenc_si128(x1, sk[10]);
+ x0 = _mm_aesenc_si128(x0, sk[11]);
+ x1 = _mm_aesenc_si128(x1, sk[11]);
+ x0 = _mm_aesenc_si128(x0, sk[12]);
+ x1 = _mm_aesenc_si128(x1, sk[12]);
+ x0 = _mm_aesenc_si128(x0, sk[13]);
+ x1 = _mm_aesenc_si128(x1, sk[13]);
+ x0 = _mm_aesenclast_si128(x0, sk[14]);
+ x1 = _mm_aesenclast_si128(x1, sk[14]);
+ }
+ x0 = _mm_xor_si128(x0, dx);
+ cmx = x1;
+ _mm_storeu_si128((void *)buf, x0);
+
+ buf += 16;
+ len -= 16;
+
+ /*
+ * Increment the counter value.
+ */
+ ivx = _mm_add_epi64(ivx, one);
+ ivx = _mm_sub_epi64(ivx,
+ _mm_slli_si128(_mm_cmpeq_epi64(ivx, zero), 8));
+ }
+
+ /*
+ * Write back new counter value and CBC-MAC value.
+ */
+ _mm_storeu_si128(ctr, _mm_shuffle_epi8(ivx, erev));
+ _mm_storeu_si128(cbcmac, cmx);
+}
+
+BR_TARGETS_X86_DOWN
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class br_aes_x86ni_ctrcbc_vtable = {
+ sizeof(br_aes_x86ni_ctrcbc_keys),
+ 16,
+ 4,
+ (void (*)(const br_block_ctrcbc_class **, const void *, size_t))
+ &br_aes_x86ni_ctrcbc_init,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_x86ni_ctrcbc_encrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, void *, size_t))
+ &br_aes_x86ni_ctrcbc_decrypt,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, void *, size_t))
+ &br_aes_x86ni_ctrcbc_ctr,
+ (void (*)(const br_block_ctrcbc_class *const *,
+ void *, const void *, size_t))
+ &br_aes_x86ni_ctrcbc_mac
+};
+
+#else
+
+/* see bearssl_block.h */
+const br_block_ctrcbc_class *
+br_aes_x86ni_ctrcbc_get_vtable(void)
+{
+ return NULL;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/chacha20_ct.c b/test/monniaux/BearSSL/src/symcipher/chacha20_ct.c
new file mode 100644
index 00000000..9961eb11
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/chacha20_ct.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+uint32_t
+br_chacha20_ct_run(const void *key,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ uint32_t kw[8], ivw[3];
+ size_t u;
+
+ static const uint32_t CW[] = {
+ 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
+ };
+
+ buf = data;
+ for (u = 0; u < 8; u ++) {
+ kw[u] = br_dec32le((const unsigned char *)key + (u << 2));
+ }
+ for (u = 0; u < 3; u ++) {
+ ivw[u] = br_dec32le((const unsigned char *)iv + (u << 2));
+ }
+ while (len > 0) {
+ uint32_t state[16];
+ int i;
+ size_t clen;
+ unsigned char tmp[64];
+
+ memcpy(&state[0], CW, sizeof CW);
+ memcpy(&state[4], kw, sizeof kw);
+ state[12] = cc;
+ memcpy(&state[13], ivw, sizeof ivw);
+ for (i = 0; i < 10; i ++) {
+
+#define QROUND(a, b, c, d) do { \
+ state[a] += state[b]; \
+ state[d] ^= state[a]; \
+ state[d] = (state[d] << 16) | (state[d] >> 16); \
+ state[c] += state[d]; \
+ state[b] ^= state[c]; \
+ state[b] = (state[b] << 12) | (state[b] >> 20); \
+ state[a] += state[b]; \
+ state[d] ^= state[a]; \
+ state[d] = (state[d] << 8) | (state[d] >> 24); \
+ state[c] += state[d]; \
+ state[b] ^= state[c]; \
+ state[b] = (state[b] << 7) | (state[b] >> 25); \
+ } while (0)
+
+ QROUND( 0, 4, 8, 12);
+ QROUND( 1, 5, 9, 13);
+ QROUND( 2, 6, 10, 14);
+ QROUND( 3, 7, 11, 15);
+ QROUND( 0, 5, 10, 15);
+ QROUND( 1, 6, 11, 12);
+ QROUND( 2, 7, 8, 13);
+ QROUND( 3, 4, 9, 14);
+
+#undef QROUND
+
+ }
+ for (u = 0; u < 4; u ++) {
+ br_enc32le(&tmp[u << 2], state[u] + CW[u]);
+ }
+ for (u = 4; u < 12; u ++) {
+ br_enc32le(&tmp[u << 2], state[u] + kw[u - 4]);
+ }
+ br_enc32le(&tmp[48], state[12] + cc);
+ for (u = 13; u < 16; u ++) {
+ br_enc32le(&tmp[u << 2], state[u] + ivw[u - 13]);
+ }
+
+ clen = len < 64 ? len : 64;
+ for (u = 0; u < clen; u ++) {
+ buf[u] ^= tmp[u];
+ }
+ buf += clen;
+ len -= clen;
+ cc ++;
+ }
+ return cc;
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/chacha20_sse2.c b/test/monniaux/BearSSL/src/symcipher/chacha20_sse2.c
new file mode 100644
index 00000000..92b4a4a8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/chacha20_sse2.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BR_ENABLE_INTRINSICS 1
+#include "inner.h"
+
+#if BR_SSE2
+
+/*
+ * This file contains a ChaCha20 implementation that leverages SSE2
+ * opcodes for better performance.
+ */
+
+/* see bearssl_block.h */
+br_chacha20_run
+br_chacha20_sse2_get(void)
+{
+ /*
+ * If using 64-bit mode, then SSE2 opcodes should be automatically
+ * available, since they are part of the ABI.
+ *
+ * In 32-bit mode, we use CPUID to detect the SSE2 feature.
+ */
+
+#if BR_amd64
+ return &br_chacha20_sse2_run;
+#else
+
+ /*
+ * SSE2 support is indicated by bit 26 in EDX.
+ */
+ if (br_cpuid(0, 0, 0, 0x04000000)) {
+ return &br_chacha20_sse2_run;
+ } else {
+ return 0;
+ }
+#endif
+}
+
+BR_TARGETS_X86_UP
+
+/* see bearssl_block.h */
+BR_TARGET("sse2")
+uint32_t
+br_chacha20_sse2_run(const void *key,
+ const void *iv, uint32_t cc, void *data, size_t len)
+{
+ unsigned char *buf;
+ uint32_t ivtmp[4];
+ __m128i kw0, kw1;
+ __m128i iw, cw;
+ __m128i one;
+
+ static const uint32_t CW[] = {
+ 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
+ };
+
+ buf = data;
+ kw0 = _mm_loadu_si128(key);
+ kw1 = _mm_loadu_si128((const void *)((const unsigned char *)key + 16));
+ ivtmp[0] = cc;
+ memcpy(ivtmp + 1, iv, 12);
+ iw = _mm_loadu_si128((const void *)ivtmp);
+ cw = _mm_loadu_si128((const void *)CW);
+ one = _mm_set_epi32(0, 0, 0, 1);
+
+ while (len > 0) {
+ /*
+ * sj contains state words 4*j to 4*j+3.
+ */
+ __m128i s0, s1, s2, s3;
+ int i;
+
+ s0 = cw;
+ s1 = kw0;
+ s2 = kw1;
+ s3 = iw;
+ for (i = 0; i < 10; i ++) {
+ /*
+ * Even round is straightforward application on
+ * the state words.
+ */
+ s0 = _mm_add_epi32(s0, s1);
+ s3 = _mm_xor_si128(s3, s0);
+ s3 = _mm_or_si128(
+ _mm_slli_epi32(s3, 16),
+ _mm_srli_epi32(s3, 16));
+
+ s2 = _mm_add_epi32(s2, s3);
+ s1 = _mm_xor_si128(s1, s2);
+ s1 = _mm_or_si128(
+ _mm_slli_epi32(s1, 12),
+ _mm_srli_epi32(s1, 20));
+
+ s0 = _mm_add_epi32(s0, s1);
+ s3 = _mm_xor_si128(s3, s0);
+ s3 = _mm_or_si128(
+ _mm_slli_epi32(s3, 8),
+ _mm_srli_epi32(s3, 24));
+
+ s2 = _mm_add_epi32(s2, s3);
+ s1 = _mm_xor_si128(s1, s2);
+ s1 = _mm_or_si128(
+ _mm_slli_epi32(s1, 7),
+ _mm_srli_epi32(s1, 25));
+
+ /*
+ * For the odd round, we must rotate some state
+ * words so that the computations apply on the
+ * right combinations of words.
+ */
+ s1 = _mm_shuffle_epi32(s1, 0x39);
+ s2 = _mm_shuffle_epi32(s2, 0x4E);
+ s3 = _mm_shuffle_epi32(s3, 0x93);
+
+ s0 = _mm_add_epi32(s0, s1);
+ s3 = _mm_xor_si128(s3, s0);
+ s3 = _mm_or_si128(
+ _mm_slli_epi32(s3, 16),
+ _mm_srli_epi32(s3, 16));
+
+ s2 = _mm_add_epi32(s2, s3);
+ s1 = _mm_xor_si128(s1, s2);
+ s1 = _mm_or_si128(
+ _mm_slli_epi32(s1, 12),
+ _mm_srli_epi32(s1, 20));
+
+ s0 = _mm_add_epi32(s0, s1);
+ s3 = _mm_xor_si128(s3, s0);
+ s3 = _mm_or_si128(
+ _mm_slli_epi32(s3, 8),
+ _mm_srli_epi32(s3, 24));
+
+ s2 = _mm_add_epi32(s2, s3);
+ s1 = _mm_xor_si128(s1, s2);
+ s1 = _mm_or_si128(
+ _mm_slli_epi32(s1, 7),
+ _mm_srli_epi32(s1, 25));
+
+ /*
+ * After the odd round, we rotate back the values
+ * to undo the rotate at the start of the odd round.
+ */
+ s1 = _mm_shuffle_epi32(s1, 0x93);
+ s2 = _mm_shuffle_epi32(s2, 0x4E);
+ s3 = _mm_shuffle_epi32(s3, 0x39);
+ }
+
+ /*
+ * Addition with the initial state.
+ */
+ s0 = _mm_add_epi32(s0, cw);
+ s1 = _mm_add_epi32(s1, kw0);
+ s2 = _mm_add_epi32(s2, kw1);
+ s3 = _mm_add_epi32(s3, iw);
+
+ /*
+ * Increment block counter.
+ */
+ iw = _mm_add_epi32(iw, one);
+
+ /*
+ * XOR final state with the data.
+ */
+ if (len < 64) {
+ unsigned char tmp[64];
+ size_t u;
+
+ _mm_storeu_si128((void *)(tmp + 0), s0);
+ _mm_storeu_si128((void *)(tmp + 16), s1);
+ _mm_storeu_si128((void *)(tmp + 32), s2);
+ _mm_storeu_si128((void *)(tmp + 48), s3);
+ for (u = 0; u < len; u ++) {
+ buf[u] ^= tmp[u];
+ }
+ break;
+ } else {
+ __m128i b0, b1, b2, b3;
+
+ b0 = _mm_loadu_si128((const void *)(buf + 0));
+ b1 = _mm_loadu_si128((const void *)(buf + 16));
+ b2 = _mm_loadu_si128((const void *)(buf + 32));
+ b3 = _mm_loadu_si128((const void *)(buf + 48));
+ b0 = _mm_xor_si128(b0, s0);
+ b1 = _mm_xor_si128(b1, s1);
+ b2 = _mm_xor_si128(b2, s2);
+ b3 = _mm_xor_si128(b3, s3);
+ _mm_storeu_si128((void *)(buf + 0), b0);
+ _mm_storeu_si128((void *)(buf + 16), b1);
+ _mm_storeu_si128((void *)(buf + 32), b2);
+ _mm_storeu_si128((void *)(buf + 48), b3);
+ buf += 64;
+ len -= 64;
+ }
+ }
+
+ /*
+ * _mm_extract_epi32() requires SSE4.1. We prefer to stick to
+ * raw SSE2, thus we use _mm_extract_epi16().
+ */
+ return (uint32_t)_mm_extract_epi16(iw, 0)
+ | ((uint32_t)_mm_extract_epi16(iw, 1) << 16);
+}
+
+BR_TARGETS_X86_DOWN
+
+#else
+
+/* see bearssl_block.h */
+br_chacha20_run
+br_chacha20_sse2_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/des_ct.c b/test/monniaux/BearSSL/src/symcipher/des_ct.c
new file mode 100644
index 00000000..581c0ab2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_ct.c
@@ -0,0 +1,411 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * During key schedule, we need to apply bit extraction PC-2 then permute
+ * things into our bitslice representation. PC-2 extracts 48 bits out
+ * of two 28-bit words (kl and kr), and we store these bits into two
+ * 32-bit words sk0 and sk1.
+ *
+ * -- bit 16+x of sk0 comes from bit QL0[x] of kl
+ * -- bit x of sk0 comes from bit QR0[x] of kr
+ * -- bit 16+x of sk1 comes from bit QL1[x] of kl
+ * -- bit x of sk1 comes from bit QR1[x] of kr
+ */
+
+static const unsigned char QL0[] = {
+ 17, 4, 27, 23, 13, 22, 7, 18,
+ 16, 24, 2, 20, 1, 8, 15, 26
+};
+
+static const unsigned char QR0[] = {
+ 25, 19, 9, 1, 5, 11, 23, 8,
+ 17, 0, 22, 3, 6, 20, 27, 24
+};
+
+static const unsigned char QL1[] = {
+ 28, 28, 14, 11, 28, 28, 25, 0,
+ 28, 28, 5, 9, 28, 28, 12, 21
+};
+
+static const unsigned char QR1[] = {
+ 28, 28, 15, 4, 28, 28, 26, 16,
+ 28, 28, 12, 7, 28, 28, 10, 14
+};
+
+/*
+ * 32-bit rotation. The C compiler is supposed to recognize it as a
+ * rotation and use the local architecture rotation opcode (if available).
+ */
+static inline uint32_t
+rotl(uint32_t x, int n)
+{
+ return (x << n) | (x >> (32 - n));
+}
+
+/*
+ * Compute key schedule for 8 key bytes (produces 32 subkey words).
+ */
+static void
+keysched_unit(uint32_t *skey, const void *key)
+{
+ int i;
+
+ br_des_keysched_unit(skey, key);
+
+ /*
+ * Apply PC-2 + bitslicing.
+ */
+ for (i = 0; i < 16; i ++) {
+ uint32_t kl, kr, sk0, sk1;
+ int j;
+
+ kl = skey[(i << 1) + 0];
+ kr = skey[(i << 1) + 1];
+ sk0 = 0;
+ sk1 = 0;
+ for (j = 0; j < 16; j ++) {
+ sk0 <<= 1;
+ sk1 <<= 1;
+ sk0 |= ((kl >> QL0[j]) & (uint32_t)1) << 16;
+ sk0 |= (kr >> QR0[j]) & (uint32_t)1;
+ sk1 |= ((kl >> QL1[j]) & (uint32_t)1) << 16;
+ sk1 |= (kr >> QR1[j]) & (uint32_t)1;
+ }
+
+ skey[(i << 1) + 0] = sk0;
+ skey[(i << 1) + 1] = sk1;
+ }
+
+#if 0
+ /*
+ * Speed-optimized version for PC-2 + bitslicing.
+ * (Unused. Kept for reference only.)
+ */
+ sk0 = kl & (uint32_t)0x00100000;
+ sk0 |= (kl & (uint32_t)0x08008000) << 2;
+ sk0 |= (kl & (uint32_t)0x00400000) << 4;
+ sk0 |= (kl & (uint32_t)0x00800000) << 5;
+ sk0 |= (kl & (uint32_t)0x00040000) << 6;
+ sk0 |= (kl & (uint32_t)0x00010000) << 7;
+ sk0 |= (kl & (uint32_t)0x00000100) << 10;
+ sk0 |= (kl & (uint32_t)0x00022000) << 14;
+ sk0 |= (kl & (uint32_t)0x00000082) << 18;
+ sk0 |= (kl & (uint32_t)0x00000004) << 19;
+ sk0 |= (kl & (uint32_t)0x04000000) >> 10;
+ sk0 |= (kl & (uint32_t)0x00000010) << 26;
+ sk0 |= (kl & (uint32_t)0x01000000) >> 2;
+
+ sk0 |= kr & (uint32_t)0x00000100;
+ sk0 |= (kr & (uint32_t)0x00000008) << 1;
+ sk0 |= (kr & (uint32_t)0x00000200) << 4;
+ sk0 |= rotl(kr & (uint32_t)0x08000021, 6);
+ sk0 |= (kr & (uint32_t)0x01000000) >> 24;
+ sk0 |= (kr & (uint32_t)0x00000002) << 11;
+ sk0 |= (kr & (uint32_t)0x00100000) >> 18;
+ sk0 |= (kr & (uint32_t)0x00400000) >> 17;
+ sk0 |= (kr & (uint32_t)0x00800000) >> 14;
+ sk0 |= (kr & (uint32_t)0x02020000) >> 10;
+ sk0 |= (kr & (uint32_t)0x00080000) >> 5;
+ sk0 |= (kr & (uint32_t)0x00000040) >> 3;
+ sk0 |= (kr & (uint32_t)0x00000800) >> 1;
+
+ sk1 = kl & (uint32_t)0x02000000;
+ sk1 |= (kl & (uint32_t)0x00001000) << 5;
+ sk1 |= (kl & (uint32_t)0x00000200) << 11;
+ sk1 |= (kl & (uint32_t)0x00004000) << 15;
+ sk1 |= (kl & (uint32_t)0x00000020) << 16;
+ sk1 |= (kl & (uint32_t)0x00000800) << 17;
+ sk1 |= (kl & (uint32_t)0x00000001) << 24;
+ sk1 |= (kl & (uint32_t)0x00200000) >> 5;
+
+ sk1 |= (kr & (uint32_t)0x00000010) << 8;
+ sk1 |= (kr & (uint32_t)0x04000000) >> 17;
+ sk1 |= (kr & (uint32_t)0x00004000) >> 14;
+ sk1 |= (kr & (uint32_t)0x00000400) >> 9;
+ sk1 |= (kr & (uint32_t)0x00010000) >> 8;
+ sk1 |= (kr & (uint32_t)0x00001000) >> 7;
+ sk1 |= (kr & (uint32_t)0x00000080) >> 3;
+ sk1 |= (kr & (uint32_t)0x00008000) >> 2;
+#endif
+}
+
+/* see inner.h */
+unsigned
+br_des_ct_keysched(uint32_t *skey, const void *key, size_t key_len)
+{
+ switch (key_len) {
+ case 8:
+ keysched_unit(skey, key);
+ return 1;
+ case 16:
+ keysched_unit(skey, key);
+ keysched_unit(skey + 32, (const unsigned char *)key + 8);
+ br_des_rev_skey(skey + 32);
+ memcpy(skey + 64, skey, 32 * sizeof *skey);
+ return 3;
+ default:
+ keysched_unit(skey, key);
+ keysched_unit(skey + 32, (const unsigned char *)key + 8);
+ br_des_rev_skey(skey + 32);
+ keysched_unit(skey + 64, (const unsigned char *)key + 16);
+ return 3;
+ }
+}
+
+/*
+ * DES confusion function. This function performs expansion E (32 to
+ * 48 bits), XOR with subkey, S-boxes, and permutation P.
+ */
+static inline uint32_t
+Fconf(uint32_t r0, const uint32_t *sk)
+{
+ /*
+ * Each 6->4 S-box is virtually turned into four 6->1 boxes; we
+ * thus end up with 32 boxes that we call "T-boxes" here. We will
+ * evaluate them with bitslice code.
+ *
+ * Each T-box is a circuit of multiplexers (sort of) and thus
+ * takes 70 inputs: the 6 actual T-box inputs, and 64 constants
+ * that describe the T-box output for all combinations of the
+ * 6 inputs. With this model, all T-boxes are identical (with
+ * distinct inputs) and thus can be executed in parallel with
+ * bitslice code.
+ *
+ * T-boxes are numbered from 0 to 31, in least-to-most
+ * significant order. Thus, S-box S1 corresponds to T-boxes 31,
+ * 30, 29 and 28, in that order. T-box 'n' is computed with the
+ * bits at rank 'n' in the 32-bit words.
+ *
+ * Words x0 to x5 contain the T-box inputs 0 to 5.
+ */
+ uint32_t x0, x1, x2, x3, x4, x5, z0;
+ uint32_t y0, y1, y2, y3, y4, y5, y6, y7, y8, y9;
+ uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
+ uint32_t y20, y21, y22, y23, y24, y25, y26, y27, y28, y29;
+ uint32_t y30;
+
+ /*
+ * Spread input bits over the 6 input words x*.
+ */
+ x1 = r0 & (uint32_t)0x11111111;
+ x2 = (r0 >> 1) & (uint32_t)0x11111111;
+ x3 = (r0 >> 2) & (uint32_t)0x11111111;
+ x4 = (r0 >> 3) & (uint32_t)0x11111111;
+ x1 = (x1 << 4) - x1;
+ x2 = (x2 << 4) - x2;
+ x3 = (x3 << 4) - x3;
+ x4 = (x4 << 4) - x4;
+ x0 = (x4 << 4) | (x4 >> 28);
+ x5 = (x1 >> 4) | (x1 << 28);
+
+ /*
+ * XOR with the subkey for this round.
+ */
+ x0 ^= sk[0];
+ x1 ^= sk[1];
+ x2 ^= sk[2];
+ x3 ^= sk[3];
+ x4 ^= sk[4];
+ x5 ^= sk[5];
+
+ /*
+ * The T-boxes are done in parallel, since they all use a
+ * "tree of multiplexer". We use "fake multiplexers":
+ *
+ * y = a ^ (x & b)
+ *
+ * computes y as either 'a' (if x == 0) or 'a ^ b' (if x == 1).
+ */
+ y0 = (uint32_t)0xEFA72C4D ^ (x0 & (uint32_t)0xEC7AC69C);
+ y1 = (uint32_t)0xAEAAEDFF ^ (x0 & (uint32_t)0x500FB821);
+ y2 = (uint32_t)0x37396665 ^ (x0 & (uint32_t)0x40EFA809);
+ y3 = (uint32_t)0x68D7B833 ^ (x0 & (uint32_t)0xA5EC0B28);
+ y4 = (uint32_t)0xC9C755BB ^ (x0 & (uint32_t)0x252CF820);
+ y5 = (uint32_t)0x73FC3606 ^ (x0 & (uint32_t)0x40205801);
+ y6 = (uint32_t)0xA2A0A918 ^ (x0 & (uint32_t)0xE220F929);
+ y7 = (uint32_t)0x8222BD90 ^ (x0 & (uint32_t)0x44A3F9E1);
+ y8 = (uint32_t)0xD6B6AC77 ^ (x0 & (uint32_t)0x794F104A);
+ y9 = (uint32_t)0x3069300C ^ (x0 & (uint32_t)0x026F320B);
+ y10 = (uint32_t)0x6CE0D5CC ^ (x0 & (uint32_t)0x7640B01A);
+ y11 = (uint32_t)0x59A9A22D ^ (x0 & (uint32_t)0x238F1572);
+ y12 = (uint32_t)0xAC6D0BD4 ^ (x0 & (uint32_t)0x7A63C083);
+ y13 = (uint32_t)0x21C83200 ^ (x0 & (uint32_t)0x11CCA000);
+ y14 = (uint32_t)0xA0E62188 ^ (x0 & (uint32_t)0x202F69AA);
+ /* y15 = (uint32_t)0x00000000 ^ (x0 & (uint32_t)0x00000000); */
+ y16 = (uint32_t)0xAF7D655A ^ (x0 & (uint32_t)0x51B33BE9);
+ y17 = (uint32_t)0xF0168AA3 ^ (x0 & (uint32_t)0x3B0FE8AE);
+ y18 = (uint32_t)0x90AA30C6 ^ (x0 & (uint32_t)0x90BF8816);
+ y19 = (uint32_t)0x5AB2750A ^ (x0 & (uint32_t)0x09E34F9B);
+ y20 = (uint32_t)0x5391BE65 ^ (x0 & (uint32_t)0x0103BE88);
+ y21 = (uint32_t)0x93372BAF ^ (x0 & (uint32_t)0x49AC8E25);
+ y22 = (uint32_t)0xF288210C ^ (x0 & (uint32_t)0x922C313D);
+ y23 = (uint32_t)0x920AF5C0 ^ (x0 & (uint32_t)0x70EF31B0);
+ y24 = (uint32_t)0x63D312C0 ^ (x0 & (uint32_t)0x6A707100);
+ y25 = (uint32_t)0x537B3006 ^ (x0 & (uint32_t)0xB97C9011);
+ y26 = (uint32_t)0xA2EFB0A5 ^ (x0 & (uint32_t)0xA320C959);
+ y27 = (uint32_t)0xBC8F96A5 ^ (x0 & (uint32_t)0x6EA0AB4A);
+ y28 = (uint32_t)0xFAD176A5 ^ (x0 & (uint32_t)0x6953DDF8);
+ y29 = (uint32_t)0x665A14A3 ^ (x0 & (uint32_t)0xF74F3E2B);
+ y30 = (uint32_t)0xF2EFF0CC ^ (x0 & (uint32_t)0xF0306CAD);
+ /* y31 = (uint32_t)0x00000000 ^ (x0 & (uint32_t)0x00000000); */
+
+ y0 = y0 ^ (x1 & y1);
+ y1 = y2 ^ (x1 & y3);
+ y2 = y4 ^ (x1 & y5);
+ y3 = y6 ^ (x1 & y7);
+ y4 = y8 ^ (x1 & y9);
+ y5 = y10 ^ (x1 & y11);
+ y6 = y12 ^ (x1 & y13);
+ y7 = y14; /* was: y14 ^ (x1 & y15) */
+ y8 = y16 ^ (x1 & y17);
+ y9 = y18 ^ (x1 & y19);
+ y10 = y20 ^ (x1 & y21);
+ y11 = y22 ^ (x1 & y23);
+ y12 = y24 ^ (x1 & y25);
+ y13 = y26 ^ (x1 & y27);
+ y14 = y28 ^ (x1 & y29);
+ y15 = y30; /* was: y30 ^ (x1 & y31) */
+
+ y0 = y0 ^ (x2 & y1);
+ y1 = y2 ^ (x2 & y3);
+ y2 = y4 ^ (x2 & y5);
+ y3 = y6 ^ (x2 & y7);
+ y4 = y8 ^ (x2 & y9);
+ y5 = y10 ^ (x2 & y11);
+ y6 = y12 ^ (x2 & y13);
+ y7 = y14 ^ (x2 & y15);
+
+ y0 = y0 ^ (x3 & y1);
+ y1 = y2 ^ (x3 & y3);
+ y2 = y4 ^ (x3 & y5);
+ y3 = y6 ^ (x3 & y7);
+
+ y0 = y0 ^ (x4 & y1);
+ y1 = y2 ^ (x4 & y3);
+
+ y0 = y0 ^ (x5 & y1);
+
+ /*
+ * The P permutation:
+ * -- Each bit move is converted into a mask + left rotation.
+ * -- Rotations that use the same movement are coalesced together.
+ * -- Left and right shifts are used as alternatives to a rotation
+ * where appropriate (this will help architectures that do not have
+ * a rotation opcode).
+ */
+ z0 = (y0 & (uint32_t)0x00000004) << 3;
+ z0 |= (y0 & (uint32_t)0x00004000) << 4;
+ z0 |= rotl(y0 & 0x12020120, 5);
+ z0 |= (y0 & (uint32_t)0x00100000) << 6;
+ z0 |= (y0 & (uint32_t)0x00008000) << 9;
+ z0 |= (y0 & (uint32_t)0x04000000) >> 22;
+ z0 |= (y0 & (uint32_t)0x00000001) << 11;
+ z0 |= rotl(y0 & 0x20000200, 12);
+ z0 |= (y0 & (uint32_t)0x00200000) >> 19;
+ z0 |= (y0 & (uint32_t)0x00000040) << 14;
+ z0 |= (y0 & (uint32_t)0x00010000) << 15;
+ z0 |= (y0 & (uint32_t)0x00000002) << 16;
+ z0 |= rotl(y0 & 0x40801800, 17);
+ z0 |= (y0 & (uint32_t)0x00080000) >> 13;
+ z0 |= (y0 & (uint32_t)0x00000010) << 21;
+ z0 |= (y0 & (uint32_t)0x01000000) >> 10;
+ z0 |= rotl(y0 & 0x88000008, 24);
+ z0 |= (y0 & (uint32_t)0x00000480) >> 7;
+ z0 |= (y0 & (uint32_t)0x00442000) >> 6;
+ return z0;
+}
+
+/*
+ * Process one block through 16 successive rounds, omitting the swap
+ * in the final round.
+ */
+static void
+process_block_unit(uint32_t *pl, uint32_t *pr, const uint32_t *sk_exp)
+{
+ int i;
+ uint32_t l, r;
+
+ l = *pl;
+ r = *pr;
+ for (i = 0; i < 16; i ++) {
+ uint32_t t;
+
+ t = l ^ Fconf(r, sk_exp);
+ l = r;
+ r = t;
+ sk_exp += 6;
+ }
+ *pl = r;
+ *pr = l;
+}
+
+/* see inner.h */
+void
+br_des_ct_process_block(unsigned num_rounds,
+ const uint32_t *sk_exp, void *block)
+{
+ unsigned char *buf;
+ uint32_t l, r;
+
+ buf = block;
+ l = br_dec32be(buf);
+ r = br_dec32be(buf + 4);
+ br_des_do_IP(&l, &r);
+ while (num_rounds -- > 0) {
+ process_block_unit(&l, &r, sk_exp);
+ sk_exp += 96;
+ }
+ br_des_do_invIP(&l, &r);
+ br_enc32be(buf, l);
+ br_enc32be(buf + 4, r);
+}
+
+/* see inner.h */
+void
+br_des_ct_skey_expand(uint32_t *sk_exp,
+ unsigned num_rounds, const uint32_t *skey)
+{
+ num_rounds <<= 4;
+ while (num_rounds -- > 0) {
+ uint32_t v, w0, w1, w2, w3;
+
+ v = *skey ++;
+ w0 = v & 0x11111111;
+ w1 = (v >> 1) & 0x11111111;
+ w2 = (v >> 2) & 0x11111111;
+ w3 = (v >> 3) & 0x11111111;
+ *sk_exp ++ = (w0 << 4) - w0;
+ *sk_exp ++ = (w1 << 4) - w1;
+ *sk_exp ++ = (w2 << 4) - w2;
+ *sk_exp ++ = (w3 << 4) - w3;
+ v = *skey ++;
+ w0 = v & 0x11111111;
+ w1 = (v >> 1) & 0x11111111;
+ *sk_exp ++ = (w0 << 4) - w0;
+ *sk_exp ++ = (w1 << 4) - w1;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/des_ct_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/des_ct_cbcdec.c
new file mode 100644
index 00000000..d208a3d2
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_ct_cbcdec.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_des_ct_cbcdec_vtable;
+ ctx->num_rounds = br_des_ct_keysched(ctx->skey, key, len);
+ if (len == 8) {
+ br_des_rev_skey(ctx->skey);
+ } else {
+ int i;
+
+ for (i = 0; i < 48; i += 2) {
+ uint32_t t;
+
+ t = ctx->skey[i];
+ ctx->skey[i] = ctx->skey[94 - i];
+ ctx->skey[94 - i] = t;
+ t = ctx->skey[i + 1];
+ ctx->skey[i + 1] = ctx->skey[95 - i];
+ ctx->skey[95 - i] = t;
+ }
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+ uint32_t sk_exp[288];
+
+ br_des_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[8];
+ int i;
+
+ memcpy(tmp, buf, 8);
+ br_des_ct_process_block(ctx->num_rounds, sk_exp, buf);
+ for (i = 0; i < 8; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ memcpy(ivbuf, tmp, 8);
+ buf += 8;
+ len -= 8;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_des_ct_cbcdec_vtable = {
+ sizeof(br_des_ct_cbcdec_keys),
+ 8,
+ 3,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_des_ct_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_des_ct_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/des_ct_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/des_ct_cbcenc.c
new file mode 100644
index 00000000..4b3610e0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_ct_cbcenc.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_des_ct_cbcenc_vtable;
+ ctx->num_rounds = br_des_ct_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+ uint32_t sk_exp[288];
+
+ br_des_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ br_des_ct_process_block(ctx->num_rounds, sk_exp, buf);
+ memcpy(ivbuf, buf, 8);
+ buf += 8;
+ len -= 8;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_des_ct_cbcenc_vtable = {
+ sizeof(br_des_ct_cbcenc_keys),
+ 8,
+ 3,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_des_ct_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_des_ct_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/des_support.c b/test/monniaux/BearSSL/src/symcipher/des_support.c
new file mode 100644
index 00000000..37f6db32
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_support.c
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+void
+br_des_do_IP(uint32_t *xl, uint32_t *xr)
+{
+ /*
+ * Permutation algorithm is initially from Richard Outerbridge;
+ * implementation here is adapted from Crypto++ "des.cpp" file
+ * (which is in public domain).
+ */
+ uint32_t l, r, t;
+
+ l = *xl;
+ r = *xr;
+ t = ((l >> 4) ^ r) & (uint32_t)0x0F0F0F0F;
+ r ^= t;
+ l ^= t << 4;
+ t = ((l >> 16) ^ r) & (uint32_t)0x0000FFFF;
+ r ^= t;
+ l ^= t << 16;
+ t = ((r >> 2) ^ l) & (uint32_t)0x33333333;
+ l ^= t;
+ r ^= t << 2;
+ t = ((r >> 8) ^ l) & (uint32_t)0x00FF00FF;
+ l ^= t;
+ r ^= t << 8;
+ t = ((l >> 1) ^ r) & (uint32_t)0x55555555;
+ r ^= t;
+ l ^= t << 1;
+ *xl = l;
+ *xr = r;
+}
+
+/* see inner.h */
+void
+br_des_do_invIP(uint32_t *xl, uint32_t *xr)
+{
+ /*
+ * See br_des_do_IP().
+ */
+ uint32_t l, r, t;
+
+ l = *xl;
+ r = *xr;
+ t = ((l >> 1) ^ r) & 0x55555555;
+ r ^= t;
+ l ^= t << 1;
+ t = ((r >> 8) ^ l) & 0x00FF00FF;
+ l ^= t;
+ r ^= t << 8;
+ t = ((r >> 2) ^ l) & 0x33333333;
+ l ^= t;
+ r ^= t << 2;
+ t = ((l >> 16) ^ r) & 0x0000FFFF;
+ r ^= t;
+ l ^= t << 16;
+ t = ((l >> 4) ^ r) & 0x0F0F0F0F;
+ r ^= t;
+ l ^= t << 4;
+ *xl = l;
+ *xr = r;
+}
+
+/* see inner.h */
+void
+br_des_keysched_unit(uint32_t *skey, const void *key)
+{
+ uint32_t xl, xr, kl, kr;
+ int i;
+
+ xl = br_dec32be(key);
+ xr = br_dec32be((const unsigned char *)key + 4);
+
+ /*
+ * Permutation PC-1 is quite similar to the IP permutation.
+ * Definition of IP (in FIPS 46-3 notations) is:
+ * 58 50 42 34 26 18 10 2
+ * 60 52 44 36 28 20 12 4
+ * 62 54 46 38 30 22 14 6
+ * 64 56 48 40 32 24 16 8
+ * 57 49 41 33 25 17 9 1
+ * 59 51 43 35 27 19 11 3
+ * 61 53 45 37 29 21 13 5
+ * 63 55 47 39 31 23 15 7
+ *
+ * Definition of PC-1 is:
+ * 57 49 41 33 25 17 9 1
+ * 58 50 42 34 26 18 10 2
+ * 59 51 43 35 27 19 11 3
+ * 60 52 44 36
+ * 63 55 47 39 31 23 15 7
+ * 62 54 46 38 30 22 14 6
+ * 61 53 45 37 29 21 13 5
+ * 28 20 12 4
+ */
+ br_des_do_IP(&xl, &xr);
+ kl = ((xr & (uint32_t)0xFF000000) >> 4)
+ | ((xl & (uint32_t)0xFF000000) >> 12)
+ | ((xr & (uint32_t)0x00FF0000) >> 12)
+ | ((xl & (uint32_t)0x00FF0000) >> 20);
+ kr = ((xr & (uint32_t)0x000000FF) << 20)
+ | ((xl & (uint32_t)0x0000FF00) << 4)
+ | ((xr & (uint32_t)0x0000FF00) >> 4)
+ | ((xl & (uint32_t)0x000F0000) >> 16);
+
+ /*
+ * For each round, rotate the two 28-bit words kl and kr.
+ * The extraction of the 48-bit subkey (PC-2) is not done yet.
+ */
+ for (i = 0; i < 16; i ++) {
+ if ((1 << i) & 0x8103) {
+ kl = (kl << 1) | (kl >> 27);
+ kr = (kr << 1) | (kr >> 27);
+ } else {
+ kl = (kl << 2) | (kl >> 26);
+ kr = (kr << 2) | (kr >> 26);
+ }
+ kl &= (uint32_t)0x0FFFFFFF;
+ kr &= (uint32_t)0x0FFFFFFF;
+ skey[(i << 1) + 0] = kl;
+ skey[(i << 1) + 1] = kr;
+ }
+}
+
+/* see inner.h */
+void
+br_des_rev_skey(uint32_t *skey)
+{
+ int i;
+
+ for (i = 0; i < 16; i += 2) {
+ uint32_t t;
+
+ t = skey[i + 0];
+ skey[i + 0] = skey[30 - i];
+ skey[30 - i] = t;
+ t = skey[i + 1];
+ skey[i + 1] = skey[31 - i];
+ skey[31 - i] = t;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/des_tab.c b/test/monniaux/BearSSL/src/symcipher/des_tab.c
new file mode 100644
index 00000000..3f8e4f9f
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_tab.c
@@ -0,0 +1,310 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * PC2left[x] tells where bit x goes when applying PC-2. 'x' is a bit
+ * position in the left rotated key word. Both position are in normal
+ * order (rightmost bit is 0).
+ */
+static const unsigned char PC2left[] = {
+ 16, 3, 7, 24, 20, 11, 24,
+ 13, 2, 10, 24, 22, 5, 15,
+ 23, 1, 9, 21, 12, 24, 6,
+ 4, 14, 18, 8, 17, 0, 19
+};
+
+/*
+ * Similar to PC2left[x], for the right rotated key word.
+ */
+static const unsigned char PC2right[] = {
+ 8, 18, 24, 6, 22, 15, 3,
+ 10, 12, 19, 5, 14, 11, 24,
+ 4, 23, 16, 9, 24, 20, 2,
+ 24, 7, 13, 0, 21, 17, 1
+};
+
+/*
+ * S-boxes and PC-1 merged.
+ */
+static const uint32_t S1[] = {
+ 0x00808200, 0x00000000, 0x00008000, 0x00808202,
+ 0x00808002, 0x00008202, 0x00000002, 0x00008000,
+ 0x00000200, 0x00808200, 0x00808202, 0x00000200,
+ 0x00800202, 0x00808002, 0x00800000, 0x00000002,
+ 0x00000202, 0x00800200, 0x00800200, 0x00008200,
+ 0x00008200, 0x00808000, 0x00808000, 0x00800202,
+ 0x00008002, 0x00800002, 0x00800002, 0x00008002,
+ 0x00000000, 0x00000202, 0x00008202, 0x00800000,
+ 0x00008000, 0x00808202, 0x00000002, 0x00808000,
+ 0x00808200, 0x00800000, 0x00800000, 0x00000200,
+ 0x00808002, 0x00008000, 0x00008200, 0x00800002,
+ 0x00000200, 0x00000002, 0x00800202, 0x00008202,
+ 0x00808202, 0x00008002, 0x00808000, 0x00800202,
+ 0x00800002, 0x00000202, 0x00008202, 0x00808200,
+ 0x00000202, 0x00800200, 0x00800200, 0x00000000,
+ 0x00008002, 0x00008200, 0x00000000, 0x00808002
+};
+
+static const uint32_t S2[] = {
+ 0x40084010, 0x40004000, 0x00004000, 0x00084010,
+ 0x00080000, 0x00000010, 0x40080010, 0x40004010,
+ 0x40000010, 0x40084010, 0x40084000, 0x40000000,
+ 0x40004000, 0x00080000, 0x00000010, 0x40080010,
+ 0x00084000, 0x00080010, 0x40004010, 0x00000000,
+ 0x40000000, 0x00004000, 0x00084010, 0x40080000,
+ 0x00080010, 0x40000010, 0x00000000, 0x00084000,
+ 0x00004010, 0x40084000, 0x40080000, 0x00004010,
+ 0x00000000, 0x00084010, 0x40080010, 0x00080000,
+ 0x40004010, 0x40080000, 0x40084000, 0x00004000,
+ 0x40080000, 0x40004000, 0x00000010, 0x40084010,
+ 0x00084010, 0x00000010, 0x00004000, 0x40000000,
+ 0x00004010, 0x40084000, 0x00080000, 0x40000010,
+ 0x00080010, 0x40004010, 0x40000010, 0x00080010,
+ 0x00084000, 0x00000000, 0x40004000, 0x00004010,
+ 0x40000000, 0x40080010, 0x40084010, 0x00084000
+};
+
+static const uint32_t S3[] = {
+ 0x00000104, 0x04010100, 0x00000000, 0x04010004,
+ 0x04000100, 0x00000000, 0x00010104, 0x04000100,
+ 0x00010004, 0x04000004, 0x04000004, 0x00010000,
+ 0x04010104, 0x00010004, 0x04010000, 0x00000104,
+ 0x04000000, 0x00000004, 0x04010100, 0x00000100,
+ 0x00010100, 0x04010000, 0x04010004, 0x00010104,
+ 0x04000104, 0x00010100, 0x00010000, 0x04000104,
+ 0x00000004, 0x04010104, 0x00000100, 0x04000000,
+ 0x04010100, 0x04000000, 0x00010004, 0x00000104,
+ 0x00010000, 0x04010100, 0x04000100, 0x00000000,
+ 0x00000100, 0x00010004, 0x04010104, 0x04000100,
+ 0x04000004, 0x00000100, 0x00000000, 0x04010004,
+ 0x04000104, 0x00010000, 0x04000000, 0x04010104,
+ 0x00000004, 0x00010104, 0x00010100, 0x04000004,
+ 0x04010000, 0x04000104, 0x00000104, 0x04010000,
+ 0x00010104, 0x00000004, 0x04010004, 0x00010100
+};
+
+static const uint32_t S4[] = {
+ 0x80401000, 0x80001040, 0x80001040, 0x00000040,
+ 0x00401040, 0x80400040, 0x80400000, 0x80001000,
+ 0x00000000, 0x00401000, 0x00401000, 0x80401040,
+ 0x80000040, 0x00000000, 0x00400040, 0x80400000,
+ 0x80000000, 0x00001000, 0x00400000, 0x80401000,
+ 0x00000040, 0x00400000, 0x80001000, 0x00001040,
+ 0x80400040, 0x80000000, 0x00001040, 0x00400040,
+ 0x00001000, 0x00401040, 0x80401040, 0x80000040,
+ 0x00400040, 0x80400000, 0x00401000, 0x80401040,
+ 0x80000040, 0x00000000, 0x00000000, 0x00401000,
+ 0x00001040, 0x00400040, 0x80400040, 0x80000000,
+ 0x80401000, 0x80001040, 0x80001040, 0x00000040,
+ 0x80401040, 0x80000040, 0x80000000, 0x00001000,
+ 0x80400000, 0x80001000, 0x00401040, 0x80400040,
+ 0x80001000, 0x00001040, 0x00400000, 0x80401000,
+ 0x00000040, 0x00400000, 0x00001000, 0x00401040
+};
+
+static const uint32_t S5[] = {
+ 0x00000080, 0x01040080, 0x01040000, 0x21000080,
+ 0x00040000, 0x00000080, 0x20000000, 0x01040000,
+ 0x20040080, 0x00040000, 0x01000080, 0x20040080,
+ 0x21000080, 0x21040000, 0x00040080, 0x20000000,
+ 0x01000000, 0x20040000, 0x20040000, 0x00000000,
+ 0x20000080, 0x21040080, 0x21040080, 0x01000080,
+ 0x21040000, 0x20000080, 0x00000000, 0x21000000,
+ 0x01040080, 0x01000000, 0x21000000, 0x00040080,
+ 0x00040000, 0x21000080, 0x00000080, 0x01000000,
+ 0x20000000, 0x01040000, 0x21000080, 0x20040080,
+ 0x01000080, 0x20000000, 0x21040000, 0x01040080,
+ 0x20040080, 0x00000080, 0x01000000, 0x21040000,
+ 0x21040080, 0x00040080, 0x21000000, 0x21040080,
+ 0x01040000, 0x00000000, 0x20040000, 0x21000000,
+ 0x00040080, 0x01000080, 0x20000080, 0x00040000,
+ 0x00000000, 0x20040000, 0x01040080, 0x20000080
+};
+
+static const uint32_t S6[] = {
+ 0x10000008, 0x10200000, 0x00002000, 0x10202008,
+ 0x10200000, 0x00000008, 0x10202008, 0x00200000,
+ 0x10002000, 0x00202008, 0x00200000, 0x10000008,
+ 0x00200008, 0x10002000, 0x10000000, 0x00002008,
+ 0x00000000, 0x00200008, 0x10002008, 0x00002000,
+ 0x00202000, 0x10002008, 0x00000008, 0x10200008,
+ 0x10200008, 0x00000000, 0x00202008, 0x10202000,
+ 0x00002008, 0x00202000, 0x10202000, 0x10000000,
+ 0x10002000, 0x00000008, 0x10200008, 0x00202000,
+ 0x10202008, 0x00200000, 0x00002008, 0x10000008,
+ 0x00200000, 0x10002000, 0x10000000, 0x00002008,
+ 0x10000008, 0x10202008, 0x00202000, 0x10200000,
+ 0x00202008, 0x10202000, 0x00000000, 0x10200008,
+ 0x00000008, 0x00002000, 0x10200000, 0x00202008,
+ 0x00002000, 0x00200008, 0x10002008, 0x00000000,
+ 0x10202000, 0x10000000, 0x00200008, 0x10002008
+};
+
+static const uint32_t S7[] = {
+ 0x00100000, 0x02100001, 0x02000401, 0x00000000,
+ 0x00000400, 0x02000401, 0x00100401, 0x02100400,
+ 0x02100401, 0x00100000, 0x00000000, 0x02000001,
+ 0x00000001, 0x02000000, 0x02100001, 0x00000401,
+ 0x02000400, 0x00100401, 0x00100001, 0x02000400,
+ 0x02000001, 0x02100000, 0x02100400, 0x00100001,
+ 0x02100000, 0x00000400, 0x00000401, 0x02100401,
+ 0x00100400, 0x00000001, 0x02000000, 0x00100400,
+ 0x02000000, 0x00100400, 0x00100000, 0x02000401,
+ 0x02000401, 0x02100001, 0x02100001, 0x00000001,
+ 0x00100001, 0x02000000, 0x02000400, 0x00100000,
+ 0x02100400, 0x00000401, 0x00100401, 0x02100400,
+ 0x00000401, 0x02000001, 0x02100401, 0x02100000,
+ 0x00100400, 0x00000000, 0x00000001, 0x02100401,
+ 0x00000000, 0x00100401, 0x02100000, 0x00000400,
+ 0x02000001, 0x02000400, 0x00000400, 0x00100001
+};
+
+static const uint32_t S8[] = {
+ 0x08000820, 0x00000800, 0x00020000, 0x08020820,
+ 0x08000000, 0x08000820, 0x00000020, 0x08000000,
+ 0x00020020, 0x08020000, 0x08020820, 0x00020800,
+ 0x08020800, 0x00020820, 0x00000800, 0x00000020,
+ 0x08020000, 0x08000020, 0x08000800, 0x00000820,
+ 0x00020800, 0x00020020, 0x08020020, 0x08020800,
+ 0x00000820, 0x00000000, 0x00000000, 0x08020020,
+ 0x08000020, 0x08000800, 0x00020820, 0x00020000,
+ 0x00020820, 0x00020000, 0x08020800, 0x00000800,
+ 0x00000020, 0x08020020, 0x00000800, 0x00020820,
+ 0x08000800, 0x00000020, 0x08000020, 0x08020000,
+ 0x08020020, 0x08000000, 0x00020000, 0x08000820,
+ 0x00000000, 0x08020820, 0x00020020, 0x08000020,
+ 0x08020000, 0x08000800, 0x08000820, 0x00000000,
+ 0x08020820, 0x00020800, 0x00020800, 0x00000820,
+ 0x00000820, 0x00020020, 0x08000000, 0x08020800
+};
+
+static inline uint32_t
+Fconf(uint32_t r0, uint32_t skl, uint32_t skr)
+{
+ uint32_t r1;
+
+ r1 = (r0 << 16) | (r0 >> 16);
+ return
+ S1[((r1 >> 11) ^ (skl >> 18)) & 0x3F]
+ | S2[((r0 >> 23) ^ (skl >> 12)) & 0x3F]
+ | S3[((r0 >> 19) ^ (skl >> 6)) & 0x3F]
+ | S4[((r0 >> 15) ^ (skl )) & 0x3F]
+ | S5[((r0 >> 11) ^ (skr >> 18)) & 0x3F]
+ | S6[((r0 >> 7) ^ (skr >> 12)) & 0x3F]
+ | S7[((r0 >> 3) ^ (skr >> 6)) & 0x3F]
+ | S8[((r1 >> 15) ^ (skr )) & 0x3F];
+}
+
+static void
+process_block_unit(uint32_t *pl, uint32_t *pr, const uint32_t *skey)
+{
+ int i;
+ uint32_t l, r;
+
+ l = *pl;
+ r = *pr;
+ for (i = 0; i < 16; i ++) {
+ uint32_t t;
+
+ t = l ^ Fconf(r, skey[(i << 1) + 0], skey[(i << 1) + 1]);
+ l = r;
+ r = t;
+ }
+ *pl = r;
+ *pr = l;
+}
+
+/* see inner.h */
+void
+br_des_tab_process_block(unsigned num_rounds, const uint32_t *skey, void *block)
+{
+ unsigned char *buf;
+ uint32_t l, r;
+
+ buf = block;
+ l = br_dec32be(buf);
+ r = br_dec32be(buf + 4);
+ br_des_do_IP(&l, &r);
+ while (num_rounds -- > 0) {
+ process_block_unit(&l, &r, skey);
+ skey += 32;
+ }
+ br_des_do_invIP(&l, &r);
+ br_enc32be(buf, l);
+ br_enc32be(buf + 4, r);
+}
+
+static void
+keysched_unit(uint32_t *skey, const void *key)
+{
+ int i;
+
+ br_des_keysched_unit(skey, key);
+
+ /*
+ * Apply PC-2 to get the 48-bit subkeys.
+ */
+ for (i = 0; i < 16; i ++) {
+ uint32_t xl, xr, ul, ur;
+ int j;
+
+ xl = skey[(i << 1) + 0];
+ xr = skey[(i << 1) + 1];
+ ul = 0;
+ ur = 0;
+ for (j = 0; j < 28; j ++) {
+ ul |= (xl & 1) << PC2left[j];
+ ur |= (xr & 1) << PC2right[j];
+ xl >>= 1;
+ xr >>= 1;
+ }
+ skey[(i << 1) + 0] = ul;
+ skey[(i << 1) + 1] = ur;
+ }
+}
+
+/* see inner.h */
+unsigned
+br_des_tab_keysched(uint32_t *skey, const void *key, size_t key_len)
+{
+ switch (key_len) {
+ case 8:
+ keysched_unit(skey, key);
+ return 1;
+ case 16:
+ keysched_unit(skey, key);
+ keysched_unit(skey + 32, (const unsigned char *)key + 8);
+ br_des_rev_skey(skey + 32);
+ memcpy(skey + 64, skey, 32 * sizeof *skey);
+ return 3;
+ default:
+ keysched_unit(skey, key);
+ keysched_unit(skey + 32, (const unsigned char *)key + 8);
+ br_des_rev_skey(skey + 32);
+ keysched_unit(skey + 64, (const unsigned char *)key + 16);
+ return 3;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/des_tab_cbcdec.c b/test/monniaux/BearSSL/src/symcipher/des_tab_cbcdec.c
new file mode 100644
index 00000000..e7eabe9d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_tab_cbcdec.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_des_tab_cbcdec_vtable;
+ ctx->num_rounds = br_des_tab_keysched(ctx->skey, key, len);
+ if (len == 8) {
+ br_des_rev_skey(ctx->skey);
+ } else {
+ int i;
+
+ for (i = 0; i < 48; i += 2) {
+ uint32_t t;
+
+ t = ctx->skey[i];
+ ctx->skey[i] = ctx->skey[94 - i];
+ ctx->skey[94 - i] = t;
+ t = ctx->skey[i + 1];
+ ctx->skey[i + 1] = ctx->skey[95 - i];
+ ctx->skey[95 - i] = t;
+ }
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[8];
+ int i;
+
+ memcpy(tmp, buf, 8);
+ br_des_tab_process_block(ctx->num_rounds, ctx->skey, buf);
+ for (i = 0; i < 8; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ memcpy(ivbuf, tmp, 8);
+ buf += 8;
+ len -= 8;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcdec_class br_des_tab_cbcdec_vtable = {
+ sizeof(br_des_tab_cbcdec_keys),
+ 8,
+ 3,
+ (void (*)(const br_block_cbcdec_class **, const void *, size_t))
+ &br_des_tab_cbcdec_init,
+ (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
+ &br_des_tab_cbcdec_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/des_tab_cbcenc.c b/test/monniaux/BearSSL/src/symcipher/des_tab_cbcenc.c
new file mode 100644
index 00000000..3a45ba3e
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/des_tab_cbcenc.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_block.h */
+void
+br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx,
+ const void *key, size_t len)
+{
+ ctx->vtable = &br_des_tab_cbcenc_vtable;
+ ctx->num_rounds = br_des_tab_keysched(ctx->skey, key, len);
+}
+
+/* see bearssl_block.h */
+void
+br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx,
+ void *iv, void *data, size_t len)
+{
+ unsigned char *buf, *ivbuf;
+
+ ivbuf = iv;
+ buf = data;
+ while (len > 0) {
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ buf[i] ^= ivbuf[i];
+ }
+ br_des_tab_process_block(ctx->num_rounds, ctx->skey, buf);
+ memcpy(ivbuf, buf, 8);
+ buf += 8;
+ len -= 8;
+ }
+}
+
+/* see bearssl_block.h */
+const br_block_cbcenc_class br_des_tab_cbcenc_vtable = {
+ sizeof(br_des_tab_cbcenc_keys),
+ 8,
+ 3,
+ (void (*)(const br_block_cbcenc_class **, const void *, size_t))
+ &br_des_tab_cbcenc_init,
+ (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
+ &br_des_tab_cbcenc_run
+};
diff --git a/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul.c b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul.c
new file mode 100644
index 00000000..150e610a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul.c
@@ -0,0 +1,260 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Perform the inner processing of blocks for Poly1305. The accumulator
+ * and the r key are provided as arrays of 26-bit words (these words
+ * are allowed to have an extra bit, i.e. use 27 bits).
+ *
+ * On output, all accumulator words fit on 26 bits, except acc[1], which
+ * may be slightly larger (but by a very small amount only).
+ */
+static void
+poly1305_inner(uint32_t *acc, const uint32_t *r, const void *data, size_t len)
+{
+ /*
+ * Implementation notes: we split the 130-bit values into five
+ * 26-bit words. This gives us some space for carries.
+ *
+ * This code is inspired from the public-domain code available
+ * on:
+ * https://github.com/floodyberry/poly1305-donna
+ *
+ * Since we compute modulo 2^130-5, the "upper words" become
+ * low words with a factor of 5; that is, x*2^130 = x*5 mod p.
+ */
+ const unsigned char *buf;
+ uint32_t a0, a1, a2, a3, a4;
+ uint32_t r0, r1, r2, r3, r4;
+ uint32_t u1, u2, u3, u4;
+
+ r0 = r[0];
+ r1 = r[1];
+ r2 = r[2];
+ r3 = r[3];
+ r4 = r[4];
+
+ u1 = r1 * 5;
+ u2 = r2 * 5;
+ u3 = r3 * 5;
+ u4 = r4 * 5;
+
+ a0 = acc[0];
+ a1 = acc[1];
+ a2 = acc[2];
+ a3 = acc[3];
+ a4 = acc[4];
+
+ buf = data;
+ while (len > 0) {
+ uint64_t w0, w1, w2, w3, w4;
+ uint64_t c;
+ unsigned char tmp[16];
+
+ /*
+ * If there is a partial block, right-pad it with zeros.
+ */
+ if (len < 16) {
+ memset(tmp, 0, sizeof tmp);
+ memcpy(tmp, buf, len);
+ buf = tmp;
+ len = 16;
+ }
+
+ /*
+ * Decode next block and apply the "high bit"; that value
+ * is added to the accumulator.
+ */
+ a0 += br_dec32le(buf) & 0x03FFFFFF;
+ a1 += (br_dec32le(buf + 3) >> 2) & 0x03FFFFFF;
+ a2 += (br_dec32le(buf + 6) >> 4) & 0x03FFFFFF;
+ a3 += (br_dec32le(buf + 9) >> 6) & 0x03FFFFFF;
+ a4 += (br_dec32le(buf + 12) >> 8) | 0x01000000;
+
+ /*
+ * Compute multiplication.
+ */
+#define M(x, y) ((uint64_t)(x) * (uint64_t)(y))
+
+ w0 = M(a0, r0) + M(a1, u4) + M(a2, u3) + M(a3, u2) + M(a4, u1);
+ w1 = M(a0, r1) + M(a1, r0) + M(a2, u4) + M(a3, u3) + M(a4, u2);
+ w2 = M(a0, r2) + M(a1, r1) + M(a2, r0) + M(a3, u4) + M(a4, u3);
+ w3 = M(a0, r3) + M(a1, r2) + M(a2, r1) + M(a3, r0) + M(a4, u4);
+ w4 = M(a0, r4) + M(a1, r3) + M(a2, r2) + M(a3, r1) + M(a4, r0);
+
+#undef M
+ /*
+ * Perform some (partial) modular reduction. This step is
+ * enough to keep values in ranges such that there won't
+ * be carry overflows. Most of the reduction was done in
+ * the multiplication step (by using the 'u*' values, and
+ * using the fact that 2^130 = -5 mod p); here we perform
+ * some carry propagation.
+ */
+ c = w0 >> 26;
+ a0 = (uint32_t)w0 & 0x3FFFFFF;
+ w1 += c;
+ c = w1 >> 26;
+ a1 = (uint32_t)w1 & 0x3FFFFFF;
+ w2 += c;
+ c = w2 >> 26;
+ a2 = (uint32_t)w2 & 0x3FFFFFF;
+ w3 += c;
+ c = w3 >> 26;
+ a3 = (uint32_t)w3 & 0x3FFFFFF;
+ w4 += c;
+ c = w4 >> 26;
+ a4 = (uint32_t)w4 & 0x3FFFFFF;
+ a0 += (uint32_t)c * 5;
+ a1 += a0 >> 26;
+ a0 &= 0x3FFFFFF;
+
+ buf += 16;
+ len -= 16;
+ }
+
+ acc[0] = a0;
+ acc[1] = a1;
+ acc[2] = a2;
+ acc[3] = a3;
+ acc[4] = a4;
+}
+
+/* see bearssl_block.h */
+void
+br_poly1305_ctmul_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt)
+{
+ unsigned char pkey[32], foot[16];
+ uint32_t r[5], acc[5], cc, ctl, hi;
+ uint64_t w;
+ int i;
+
+ /*
+ * Compute the MAC key. The 'r' value is the first 16 bytes of
+ * pkey[].
+ */
+ memset(pkey, 0, sizeof pkey);
+ ichacha(key, iv, 0, pkey, sizeof pkey);
+
+ /*
+ * If encrypting, ChaCha20 must run first, followed by Poly1305.
+ * When decrypting, the operations are reversed.
+ */
+ if (encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+
+ /*
+ * Run Poly1305. We must process the AAD, then ciphertext, then
+ * the footer (with the lengths). Note that the AAD and ciphertext
+ * are meant to be padded with zeros up to the next multiple of 16,
+ * and the length of the footer is 16 bytes as well.
+ */
+
+ /*
+ * Decode the 'r' value into 26-bit words, with the "clamping"
+ * operation applied.
+ */
+ r[0] = br_dec32le(pkey) & 0x03FFFFFF;
+ r[1] = (br_dec32le(pkey + 3) >> 2) & 0x03FFFF03;
+ r[2] = (br_dec32le(pkey + 6) >> 4) & 0x03FFC0FF;
+ r[3] = (br_dec32le(pkey + 9) >> 6) & 0x03F03FFF;
+ r[4] = (br_dec32le(pkey + 12) >> 8) & 0x000FFFFF;
+
+ /*
+ * Accumulator is 0.
+ */
+ memset(acc, 0, sizeof acc);
+
+ /*
+ * Process the additional authenticated data, ciphertext, and
+ * footer in due order.
+ */
+ br_enc64le(foot, (uint64_t)aad_len);
+ br_enc64le(foot + 8, (uint64_t)len);
+ poly1305_inner(acc, r, aad, aad_len);
+ poly1305_inner(acc, r, data, len);
+ poly1305_inner(acc, r, foot, sizeof foot);
+
+ /*
+ * Finalise modular reduction. This is done with carry propagation
+ * and applying the '2^130 = -5 mod p' rule. Note that the output
+ * of poly1035_inner() is already mostly reduced, since only
+ * acc[1] may be (very slightly) above 2^26. A single loop back
+ * to acc[1] will be enough to make the value fit in 130 bits.
+ */
+ cc = 0;
+ for (i = 1; i <= 6; i ++) {
+ int j;
+
+ j = (i >= 5) ? i - 5 : i;
+ acc[j] += cc;
+ cc = acc[j] >> 26;
+ acc[j] &= 0x03FFFFFF;
+ }
+
+ /*
+ * We may still have a value in the 2^130-5..2^130-1 range, in
+ * which case we must reduce it again. The code below selects,
+ * in constant-time, between 'acc' and 'acc-p',
+ */
+ ctl = GT(acc[0], 0x03FFFFFA);
+ for (i = 1; i < 5; i ++) {
+ ctl &= EQ(acc[i], 0x03FFFFFF);
+ }
+ cc = 5;
+ for (i = 0; i < 5; i ++) {
+ uint32_t t;
+
+ t = (acc[i] + cc);
+ cc = t >> 26;
+ t &= 0x03FFFFFF;
+ acc[i] = MUX(ctl, t, acc[i]);
+ }
+
+ /*
+ * Convert back the accumulator to 32-bit words, and add the
+ * 's' value (second half of pkey[]). That addition is done
+ * modulo 2^128.
+ */
+ w = (uint64_t)acc[0] + ((uint64_t)acc[1] << 26) + br_dec32le(pkey + 16);
+ br_enc32le((unsigned char *)tag, (uint32_t)w);
+ w = (w >> 32) + ((uint64_t)acc[2] << 20) + br_dec32le(pkey + 20);
+ br_enc32le((unsigned char *)tag + 4, (uint32_t)w);
+ w = (w >> 32) + ((uint64_t)acc[3] << 14) + br_dec32le(pkey + 24);
+ br_enc32le((unsigned char *)tag + 8, (uint32_t)w);
+ hi = (uint32_t)(w >> 32) + (acc[4] << 8) + br_dec32le(pkey + 28);
+ br_enc32le((unsigned char *)tag + 12, hi);
+
+ /*
+ * If decrypting, then ChaCha20 runs _after_ Poly1305.
+ */
+ if (!encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul32.c b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul32.c
new file mode 100644
index 00000000..15d9635d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmul32.c
@@ -0,0 +1,297 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * Perform the inner processing of blocks for Poly1305.
+ */
+static void
+poly1305_inner(uint32_t *a, const uint32_t *r, const void *data, size_t len)
+{
+ /*
+ * Implementation notes: we split the 130-bit values into ten
+ * 13-bit words. This gives us some space for carries and allows
+ * using only 32x32->32 multiplications, which are way faster than
+ * 32x32->64 multiplications on the ARM Cortex-M0/M0+, and also
+ * help in making constant-time code on the Cortex-M3.
+ *
+ * Since we compute modulo 2^130-5, the "upper words" become
+ * low words with a factor of 5; that is, x*2^130 = x*5 mod p.
+ * This has already been integrated in the r[] array, which
+ * is extended to the 0..18 range.
+ *
+ * In each loop iteration, a[] and r[] words are 13-bit each,
+ * except a[1] which may use 14 bits.
+ */
+ const unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16];
+ uint32_t b[10];
+ unsigned u, v;
+ uint32_t z, cc1, cc2;
+
+ /*
+ * If there is a partial block, right-pad it with zeros.
+ */
+ if (len < 16) {
+ memset(tmp, 0, sizeof tmp);
+ memcpy(tmp, buf, len);
+ buf = tmp;
+ len = 16;
+ }
+
+ /*
+ * Decode next block and apply the "high bit"; that value
+ * is added to the accumulator.
+ */
+ v = br_dec16le(buf);
+ a[0] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[2] << 3;
+ v |= buf[3] << 11;
+ a[1] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[4] << 6;
+ a[2] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[5] << 1;
+ v |= buf[6] << 9;
+ a[3] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[7] << 4;
+ v |= buf[8] << 12;
+ a[4] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[9] << 7;
+ a[5] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[10] << 2;
+ v |= buf[11] << 10;
+ a[6] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[12] << 5;
+ a[7] += v & 0x01FFF;
+ v = br_dec16le(buf + 13);
+ a[8] += v & 0x01FFF;
+ v >>= 13;
+ v |= buf[15] << 3;
+ a[9] += v | 0x00800;
+
+ /*
+ * At that point, all a[] values fit on 14 bits, while
+ * all r[] values fit on 13 bits. Thus products fit on
+ * 27 bits, and we can accumulate up to 31 of them in
+ * a 32-bit word and still have some room for carries.
+ */
+
+ /*
+ * Now a[] contains words with values up to 14 bits each.
+ * We perform the multiplication with r[].
+ *
+ * The extended words of r[] may be larger than 13 bits
+ * (they are 5 times a 13-bit word) so the full summation
+ * may yield values up to 46 times a 27-bit word, which
+ * does not fit on a 32-bit word. To avoid that issue, we
+ * must split the loop below in two, with a carry
+ * propagation operation in the middle.
+ */
+ cc1 = 0;
+ for (u = 0; u < 10; u ++) {
+ uint32_t s;
+
+ s = cc1
+ + MUL15(a[0], r[u + 9 - 0])
+ + MUL15(a[1], r[u + 9 - 1])
+ + MUL15(a[2], r[u + 9 - 2])
+ + MUL15(a[3], r[u + 9 - 3])
+ + MUL15(a[4], r[u + 9 - 4]);
+ b[u] = s & 0x1FFF;
+ cc1 = s >> 13;
+ }
+ cc2 = 0;
+ for (u = 0; u < 10; u ++) {
+ uint32_t s;
+
+ s = b[u] + cc2
+ + MUL15(a[5], r[u + 9 - 5])
+ + MUL15(a[6], r[u + 9 - 6])
+ + MUL15(a[7], r[u + 9 - 7])
+ + MUL15(a[8], r[u + 9 - 8])
+ + MUL15(a[9], r[u + 9 - 9]);
+ b[u] = s & 0x1FFF;
+ cc2 = s >> 13;
+ }
+ memcpy(a, b, sizeof b);
+
+ /*
+ * The two carries "loop back" with a factor of 5. We
+ * propagate them into a[0] and a[1].
+ */
+ z = cc1 + cc2;
+ z += (z << 2) + a[0];
+ a[0] = z & 0x1FFF;
+ a[1] += z >> 13;
+
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_poly1305_ctmul32_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt)
+{
+ unsigned char pkey[32], foot[16];
+ uint32_t z, r[19], acc[10], cc, ctl;
+ int i;
+
+ /*
+ * Compute the MAC key. The 'r' value is the first 16 bytes of
+ * pkey[].
+ */
+ memset(pkey, 0, sizeof pkey);
+ ichacha(key, iv, 0, pkey, sizeof pkey);
+
+ /*
+ * If encrypting, ChaCha20 must run first, followed by Poly1305.
+ * When decrypting, the operations are reversed.
+ */
+ if (encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+
+ /*
+ * Run Poly1305. We must process the AAD, then ciphertext, then
+ * the footer (with the lengths). Note that the AAD and ciphertext
+ * are meant to be padded with zeros up to the next multiple of 16,
+ * and the length of the footer is 16 bytes as well.
+ */
+
+ /*
+ * Decode the 'r' value into 13-bit words, with the "clamping"
+ * operation applied.
+ */
+ z = br_dec32le(pkey) & 0x03FFFFFF;
+ r[9] = z & 0x1FFF;
+ r[10] = z >> 13;
+ z = (br_dec32le(pkey + 3) >> 2) & 0x03FFFF03;
+ r[11] = z & 0x1FFF;
+ r[12] = z >> 13;
+ z = (br_dec32le(pkey + 6) >> 4) & 0x03FFC0FF;
+ r[13] = z & 0x1FFF;
+ r[14] = z >> 13;
+ z = (br_dec32le(pkey + 9) >> 6) & 0x03F03FFF;
+ r[15] = z & 0x1FFF;
+ r[16] = z >> 13;
+ z = (br_dec32le(pkey + 12) >> 8) & 0x000FFFFF;
+ r[17] = z & 0x1FFF;
+ r[18] = z >> 13;
+
+ /*
+ * Extend r[] with the 5x factor pre-applied.
+ */
+ for (i = 0; i < 9; i ++) {
+ r[i] = MUL15(5, r[i + 10]);
+ }
+
+ /*
+ * Accumulator is 0.
+ */
+ memset(acc, 0, sizeof acc);
+
+ /*
+ * Process the additional authenticated data, ciphertext, and
+ * footer in due order.
+ */
+ br_enc64le(foot, (uint64_t)aad_len);
+ br_enc64le(foot + 8, (uint64_t)len);
+ poly1305_inner(acc, r, aad, aad_len);
+ poly1305_inner(acc, r, data, len);
+ poly1305_inner(acc, r, foot, sizeof foot);
+
+ /*
+ * Finalise modular reduction. This is done with carry propagation
+ * and applying the '2^130 = -5 mod p' rule. Note that the output
+ * of poly1035_inner() is already mostly reduced, since only
+ * acc[1] may be (very slightly) above 2^13. A single loop back
+ * to acc[1] will be enough to make the value fit in 130 bits.
+ */
+ cc = 0;
+ for (i = 1; i < 10; i ++) {
+ z = acc[i] + cc;
+ acc[i] = z & 0x1FFF;
+ cc = z >> 13;
+ }
+ z = acc[0] + cc + (cc << 2);
+ acc[0] = z & 0x1FFF;
+ acc[1] += z >> 13;
+
+ /*
+ * We may still have a value in the 2^130-5..2^130-1 range, in
+ * which case we must reduce it again. The code below selects,
+ * in constant-time, between 'acc' and 'acc-p',
+ */
+ ctl = GT(acc[0], 0x1FFA);
+ for (i = 1; i < 10; i ++) {
+ ctl &= EQ(acc[i], 0x1FFF);
+ }
+ acc[0] = MUX(ctl, acc[0] - 0x1FFB, acc[0]);
+ for (i = 1; i < 10; i ++) {
+ acc[i] &= ~(-ctl);
+ }
+
+ /*
+ * Convert back the accumulator to 32-bit words, and add the
+ * 's' value (second half of pkey[]). That addition is done
+ * modulo 2^128.
+ */
+ z = acc[0] + (acc[1] << 13) + br_dec16le(pkey + 16);
+ br_enc16le((unsigned char *)tag, z & 0xFFFF);
+ z = (z >> 16) + (acc[2] << 10) + br_dec16le(pkey + 18);
+ br_enc16le((unsigned char *)tag + 2, z & 0xFFFF);
+ z = (z >> 16) + (acc[3] << 7) + br_dec16le(pkey + 20);
+ br_enc16le((unsigned char *)tag + 4, z & 0xFFFF);
+ z = (z >> 16) + (acc[4] << 4) + br_dec16le(pkey + 22);
+ br_enc16le((unsigned char *)tag + 6, z & 0xFFFF);
+ z = (z >> 16) + (acc[5] << 1) + (acc[6] << 14) + br_dec16le(pkey + 24);
+ br_enc16le((unsigned char *)tag + 8, z & 0xFFFF);
+ z = (z >> 16) + (acc[7] << 11) + br_dec16le(pkey + 26);
+ br_enc16le((unsigned char *)tag + 10, z & 0xFFFF);
+ z = (z >> 16) + (acc[8] << 8) + br_dec16le(pkey + 28);
+ br_enc16le((unsigned char *)tag + 12, z & 0xFFFF);
+ z = (z >> 16) + (acc[9] << 5) + br_dec16le(pkey + 30);
+ br_enc16le((unsigned char *)tag + 14, z & 0xFFFF);
+
+ /*
+ * If decrypting, then ChaCha20 runs _after_ Poly1305.
+ */
+ if (!encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/symcipher/poly1305_ctmulq.c b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmulq.c
new file mode 100644
index 00000000..b00683a6
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/poly1305_ctmulq.c
@@ -0,0 +1,475 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+#if BR_INT128 || BR_UMUL128
+
+#if BR_INT128
+
+#define MUL128(hi, lo, x, y) do { \
+ unsigned __int128 mul128tmp; \
+ mul128tmp = (unsigned __int128)(x) * (unsigned __int128)(y); \
+ (hi) = (uint64_t)(mul128tmp >> 64); \
+ (lo) = (uint64_t)mul128tmp; \
+ } while (0)
+
+#elif BR_UMUL128
+
+#include <intrin.h>
+
+#define MUL128(hi, lo, x, y) do { \
+ (lo) = _umul128((x), (y), &(hi)); \
+ } while (0)
+
+#endif
+
+#define MASK42 ((uint64_t)0x000003FFFFFFFFFF)
+#define MASK44 ((uint64_t)0x00000FFFFFFFFFFF)
+
+/*
+ * The "accumulator" word is nominally a 130-bit value. We split it into
+ * words of 44 bits, each held in a 64-bit variable.
+ *
+ * If the current accumulator is a = a0 + a1*W + a2*W^2 (where W = 2^44)
+ * and r = r0 + r1*W + r2*W^2, then:
+ *
+ * a*r = (a0*r0)
+ * + (a0*r1 + a1*r0) * W
+ * + (a0*r2 + a1*r1 + a2*r0) * W^2
+ * + (a1*r2 + a2*r1) * W^3
+ * + (a2*r2) * W^4
+ *
+ * We want to reduce that value modulo p = 2^130-5, so W^3 = 20 mod p,
+ * and W^4 = 20*W mod p. Thus, if we define u1 = 20*r1 and u2 = 20*r2,
+ * then the equations above become:
+ *
+ * b0 = a0*r0 + a1*u2 + a2*u1
+ * b1 = a0*r1 + a1*r0 + a2*u2
+ * b2 = a0*r2 + a1*r1 + a2*r0
+ *
+ * In order to make u1 fit in 44 bits, we can change these equations
+ * into:
+ *
+ * b0 = a0*r0 + a1*u2 + a2*t1
+ * b1 = a0*r1 + a1*r0 + a2*t2
+ * b2 = a0*r2 + a1*r1 + a2*r0
+ *
+ * Where t1 is u1 truncated to 44 bits, and t2 is u2 added to the extra
+ * bits of u1. Note that since r is clamped down to a 124-bit value, the
+ * values u2 and t2 fit on 44 bits too.
+ *
+ * The bx values are larger than 44 bits, so we may split them into a
+ * lower half (cx, 44 bits) and an upper half (dx). The new values for
+ * the accumulator are then:
+ *
+ * e0 = c0 + 20*d2
+ * e1 = c1 + d0
+ * e2 = c2 + d1
+ *
+ * The equations allow for some room, i.e. the ax values may be larger
+ * than 44 bits. Similarly, the ex values will usually be larger than
+ * the ax. Thus, some sort of carry propagation must be done regularly,
+ * though not necessarily at each iteration. In particular, we do not
+ * need to compute the additions (for the bx values) over 128-bit
+ * quantities; we can stick to 64-bit computations.
+ *
+ *
+ * Since the 128-bit result of a 64x64 multiplication is actually
+ * represented over two 64-bit registers, it is cheaper to arrange for
+ * any split that happens between the "high" and "low" halves to be on
+ * that 64-bit boundary. This is done by left shifting the rx, ux and tx
+ * by 20 bits (since they all fit on 44 bits each, this shift is
+ * always possible).
+ */
+
+static void
+poly1305_inner_big(uint64_t *acc, uint64_t *r, const void *data, size_t len)
+{
+
+#define MX(hi, lo, m0, m1, m2) do { \
+ uint64_t mxhi, mxlo; \
+ MUL128(mxhi, mxlo, a0, m0); \
+ (hi) = mxhi; \
+ (lo) = mxlo >> 20; \
+ MUL128(mxhi, mxlo, a1, m1); \
+ (hi) += mxhi; \
+ (lo) += mxlo >> 20; \
+ MUL128(mxhi, mxlo, a2, m2); \
+ (hi) += mxhi; \
+ (lo) += mxlo >> 20; \
+ } while (0)
+
+ const unsigned char *buf;
+ uint64_t a0, a1, a2;
+ uint64_t r0, r1, r2, t1, t2, u2;
+
+ r0 = r[0];
+ r1 = r[1];
+ r2 = r[2];
+ t1 = r[3];
+ t2 = r[4];
+ u2 = r[5];
+ a0 = acc[0];
+ a1 = acc[1];
+ a2 = acc[2];
+ buf = data;
+
+ while (len > 0) {
+ uint64_t v0, v1, v2;
+ uint64_t c0, c1, c2, d0, d1, d2;
+
+ v0 = br_dec64le(buf + 0);
+ v1 = br_dec64le(buf + 8);
+ v2 = v1 >> 24;
+ v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
+ v0 &= MASK44;
+ a0 += v0;
+ a1 += v1;
+ a2 += v2 + ((uint64_t)1 << 40);
+ MX(d0, c0, r0, u2, t1);
+ MX(d1, c1, r1, r0, t2);
+ MX(d2, c2, r2, r1, r0);
+ a0 = c0 + 20 * d2;
+ a1 = c1 + d0;
+ a2 = c2 + d1;
+
+ v0 = br_dec64le(buf + 16);
+ v1 = br_dec64le(buf + 24);
+ v2 = v1 >> 24;
+ v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
+ v0 &= MASK44;
+ a0 += v0;
+ a1 += v1;
+ a2 += v2 + ((uint64_t)1 << 40);
+ MX(d0, c0, r0, u2, t1);
+ MX(d1, c1, r1, r0, t2);
+ MX(d2, c2, r2, r1, r0);
+ a0 = c0 + 20 * d2;
+ a1 = c1 + d0;
+ a2 = c2 + d1;
+
+ v0 = br_dec64le(buf + 32);
+ v1 = br_dec64le(buf + 40);
+ v2 = v1 >> 24;
+ v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
+ v0 &= MASK44;
+ a0 += v0;
+ a1 += v1;
+ a2 += v2 + ((uint64_t)1 << 40);
+ MX(d0, c0, r0, u2, t1);
+ MX(d1, c1, r1, r0, t2);
+ MX(d2, c2, r2, r1, r0);
+ a0 = c0 + 20 * d2;
+ a1 = c1 + d0;
+ a2 = c2 + d1;
+
+ v0 = br_dec64le(buf + 48);
+ v1 = br_dec64le(buf + 56);
+ v2 = v1 >> 24;
+ v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
+ v0 &= MASK44;
+ a0 += v0;
+ a1 += v1;
+ a2 += v2 + ((uint64_t)1 << 40);
+ MX(d0, c0, r0, u2, t1);
+ MX(d1, c1, r1, r0, t2);
+ MX(d2, c2, r2, r1, r0);
+ a0 = c0 + 20 * d2;
+ a1 = c1 + d0;
+ a2 = c2 + d1;
+
+ a1 += a0 >> 44;
+ a0 &= MASK44;
+ a2 += a1 >> 44;
+ a1 &= MASK44;
+ a0 += 20 * (a2 >> 44);
+ a2 &= MASK44;
+
+ buf += 64;
+ len -= 64;
+ }
+ acc[0] = a0;
+ acc[1] = a1;
+ acc[2] = a2;
+
+#undef MX
+}
+
+static void
+poly1305_inner_small(uint64_t *acc, uint64_t *r, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ uint64_t a0, a1, a2;
+ uint64_t r0, r1, r2, t1, t2, u2;
+
+ r0 = r[0];
+ r1 = r[1];
+ r2 = r[2];
+ t1 = r[3];
+ t2 = r[4];
+ u2 = r[5];
+ a0 = acc[0];
+ a1 = acc[1];
+ a2 = acc[2];
+ buf = data;
+
+ while (len > 0) {
+ uint64_t v0, v1, v2;
+ uint64_t c0, c1, c2, d0, d1, d2;
+ unsigned char tmp[16];
+
+ if (len < 16) {
+ memcpy(tmp, buf, len);
+ memset(tmp + len, 0, (sizeof tmp) - len);
+ buf = tmp;
+ len = 16;
+ }
+ v0 = br_dec64le(buf + 0);
+ v1 = br_dec64le(buf + 8);
+
+ v2 = v1 >> 24;
+ v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
+ v0 &= MASK44;
+
+ a0 += v0;
+ a1 += v1;
+ a2 += v2 + ((uint64_t)1 << 40);
+
+#define MX(hi, lo, m0, m1, m2) do { \
+ uint64_t mxhi, mxlo; \
+ MUL128(mxhi, mxlo, a0, m0); \
+ (hi) = mxhi; \
+ (lo) = mxlo >> 20; \
+ MUL128(mxhi, mxlo, a1, m1); \
+ (hi) += mxhi; \
+ (lo) += mxlo >> 20; \
+ MUL128(mxhi, mxlo, a2, m2); \
+ (hi) += mxhi; \
+ (lo) += mxlo >> 20; \
+ } while (0)
+
+ MX(d0, c0, r0, u2, t1);
+ MX(d1, c1, r1, r0, t2);
+ MX(d2, c2, r2, r1, r0);
+
+#undef MX
+
+ a0 = c0 + 20 * d2;
+ a1 = c1 + d0;
+ a2 = c2 + d1;
+
+ a1 += a0 >> 44;
+ a0 &= MASK44;
+ a2 += a1 >> 44;
+ a1 &= MASK44;
+ a0 += 20 * (a2 >> 44);
+ a2 &= MASK44;
+
+ buf += 16;
+ len -= 16;
+ }
+ acc[0] = a0;
+ acc[1] = a1;
+ acc[2] = a2;
+}
+
+static inline void
+poly1305_inner(uint64_t *acc, uint64_t *r, const void *data, size_t len)
+{
+ if (len >= 64) {
+ size_t len2;
+
+ len2 = len & ~(size_t)63;
+ poly1305_inner_big(acc, r, data, len2);
+ data = (const unsigned char *)data + len2;
+ len -= len2;
+ }
+ if (len > 0) {
+ poly1305_inner_small(acc, r, data, len);
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_poly1305_ctmulq_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt)
+{
+ unsigned char pkey[32], foot[16];
+ uint64_t r[6], acc[3], r0, r1;
+ uint32_t v0, v1, v2, v3, v4;
+ uint64_t w0, w1, w2, w3;
+ uint32_t ctl;
+
+ /*
+ * Compute the MAC key. The 'r' value is the first 16 bytes of
+ * pkey[].
+ */
+ memset(pkey, 0, sizeof pkey);
+ ichacha(key, iv, 0, pkey, sizeof pkey);
+
+ /*
+ * If encrypting, ChaCha20 must run first, followed by Poly1305.
+ * When decrypting, the operations are reversed.
+ */
+ if (encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+
+ /*
+ * Run Poly1305. We must process the AAD, then ciphertext, then
+ * the footer (with the lengths). Note that the AAD and ciphertext
+ * are meant to be padded with zeros up to the next multiple of 16,
+ * and the length of the footer is 16 bytes as well.
+ */
+
+ /*
+ * Apply the "clamping" on r.
+ */
+ pkey[ 3] &= 0x0F;
+ pkey[ 4] &= 0xFC;
+ pkey[ 7] &= 0x0F;
+ pkey[ 8] &= 0xFC;
+ pkey[11] &= 0x0F;
+ pkey[12] &= 0xFC;
+ pkey[15] &= 0x0F;
+
+ /*
+ * Decode the 'r' value into 44-bit words, left-shifted by 20 bits.
+ * Also compute the u1 and u2 values.
+ */
+ r0 = br_dec64le(pkey + 0);
+ r1 = br_dec64le(pkey + 8);
+ r[0] = r0 << 20;
+ r[1] = ((r0 >> 24) | (r1 << 40)) & ~(uint64_t)0xFFFFF;
+ r[2] = (r1 >> 4) & ~(uint64_t)0xFFFFF;
+ r1 = 20 * (r[1] >> 20);
+ r[3] = r1 << 20;
+ r[5] = 20 * r[2];
+ r[4] = (r[5] + (r1 >> 24)) & ~(uint64_t)0xFFFFF;
+
+ /*
+ * Accumulator is 0.
+ */
+ acc[0] = 0;
+ acc[1] = 0;
+ acc[2] = 0;
+
+ /*
+ * Process the additional authenticated data, ciphertext, and
+ * footer in due order.
+ */
+ br_enc64le(foot, (uint64_t)aad_len);
+ br_enc64le(foot + 8, (uint64_t)len);
+ poly1305_inner(acc, r, aad, aad_len);
+ poly1305_inner(acc, r, data, len);
+ poly1305_inner_small(acc, r, foot, sizeof foot);
+
+ /*
+ * Finalise modular reduction. At that point, the value consists
+ * in three 44-bit values (the lowest one might be slightly above
+ * 2^44). Two loops shall be sufficient.
+ */
+ acc[1] += (acc[0] >> 44);
+ acc[0] &= MASK44;
+ acc[2] += (acc[1] >> 44);
+ acc[1] &= MASK44;
+ acc[0] += 5 * (acc[2] >> 42);
+ acc[2] &= MASK42;
+ acc[1] += (acc[0] >> 44);
+ acc[0] &= MASK44;
+ acc[2] += (acc[1] >> 44);
+ acc[1] &= MASK44;
+ acc[0] += 5 * (acc[2] >> 42);
+ acc[2] &= MASK42;
+
+ /*
+ * The value may still fall in the 2^130-5..2^130-1 range, in
+ * which case we must reduce it again. The code below selects,
+ * in constant-time, between 'acc' and 'acc-p'. We encode the
+ * value over four 32-bit integers to finish the operation.
+ */
+ v0 = (uint32_t)acc[0];
+ v1 = (uint32_t)(acc[0] >> 32) | ((uint32_t)acc[1] << 12);
+ v2 = (uint32_t)(acc[1] >> 20) | ((uint32_t)acc[2] << 24);
+ v3 = (uint32_t)(acc[2] >> 8);
+ v4 = (uint32_t)(acc[2] >> 40);
+
+ ctl = GT(v0, 0xFFFFFFFA);
+ ctl &= EQ(v1, 0xFFFFFFFF);
+ ctl &= EQ(v2, 0xFFFFFFFF);
+ ctl &= EQ(v3, 0xFFFFFFFF);
+ ctl &= EQ(v4, 0x00000003);
+ v0 = MUX(ctl, v0 + 5, v0);
+ v1 = MUX(ctl, 0, v1);
+ v2 = MUX(ctl, 0, v2);
+ v3 = MUX(ctl, 0, v3);
+
+ /*
+ * Add the "s" value. This is done modulo 2^128. Don't forget
+ * carry propagation...
+ */
+ w0 = (uint64_t)v0 + (uint64_t)br_dec32le(pkey + 16);
+ w1 = (uint64_t)v1 + (uint64_t)br_dec32le(pkey + 20) + (w0 >> 32);
+ w2 = (uint64_t)v2 + (uint64_t)br_dec32le(pkey + 24) + (w1 >> 32);
+ w3 = (uint64_t)v3 + (uint64_t)br_dec32le(pkey + 28) + (w2 >> 32);
+ v0 = (uint32_t)w0;
+ v1 = (uint32_t)w1;
+ v2 = (uint32_t)w2;
+ v3 = (uint32_t)w3;
+
+ /*
+ * Encode the tag.
+ */
+ br_enc32le((unsigned char *)tag + 0, v0);
+ br_enc32le((unsigned char *)tag + 4, v1);
+ br_enc32le((unsigned char *)tag + 8, v2);
+ br_enc32le((unsigned char *)tag + 12, v3);
+
+ /*
+ * If decrypting, then ChaCha20 runs _after_ Poly1305.
+ */
+ if (!encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+}
+
+/* see bearssl_block.h */
+br_poly1305_run
+br_poly1305_ctmulq_get(void)
+{
+ return &br_poly1305_ctmulq_run;
+}
+
+#else
+
+/* see bearssl_block.h */
+br_poly1305_run
+br_poly1305_ctmulq_get(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/test/monniaux/BearSSL/src/symcipher/poly1305_i15.c b/test/monniaux/BearSSL/src/symcipher/poly1305_i15.c
new file mode 100644
index 00000000..6f892121
--- /dev/null
+++ b/test/monniaux/BearSSL/src/symcipher/poly1305_i15.c
@@ -0,0 +1,221 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/*
+ * This is a "reference" implementation of Poly1305 that uses the
+ * generic "i15" code for big integers. It is slow, but it handles all
+ * big-integer operations with generic code, thereby avoiding most
+ * tricky situations with carry propagation and modular reduction.
+ */
+
+/*
+ * Modulus: 2^130-5.
+ */
+static const uint16_t P1305[] = {
+ 0x008A,
+ 0x7FFB, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x03FF
+};
+
+/*
+ * -p mod 2^15.
+ */
+#define P0I 0x4CCD
+
+/*
+ * R^2 mod p, for conversion to Montgomery representation (R = 2^135,
+ * since we use 9 words of 15 bits each, and 15*9 = 135).
+ */
+static const uint16_t R2[] = {
+ 0x008A,
+ 0x6400, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
+};
+
+/*
+ * Perform the inner processing of blocks for Poly1305. The "r" array
+ * is in Montgomery representation, while the "a" array is not.
+ */
+static void
+poly1305_inner(uint16_t *a, const uint16_t *r, const void *data, size_t len)
+{
+ const unsigned char *buf;
+
+ buf = data;
+ while (len > 0) {
+ unsigned char tmp[16], rev[16];
+ uint16_t b[10];
+ uint32_t ctl;
+ int i;
+
+ /*
+ * If there is a partial block, right-pad it with zeros.
+ */
+ if (len < 16) {
+ memset(tmp, 0, sizeof tmp);
+ memcpy(tmp, buf, len);
+ buf = tmp;
+ len = 16;
+ }
+
+ /*
+ * Decode next block and apply the "high bit". Since
+ * decoding is little-endian, we must byte-swap the buffer.
+ */
+ for (i = 0; i < 16; i ++) {
+ rev[i] = buf[15 - i];
+ }
+ br_i15_decode_mod(b, rev, sizeof rev, P1305);
+ b[9] |= 0x0100;
+
+ /*
+ * Add the accumulator to the decoded block (modular
+ * addition).
+ */
+ ctl = br_i15_add(b, a, 1);
+ ctl |= NOT(br_i15_sub(b, P1305, 0));
+ br_i15_sub(b, P1305, ctl);
+
+ /*
+ * Multiply by r, result is the new accumulator value.
+ */
+ br_i15_montymul(a, b, r, P1305, P0I);
+
+ buf += 16;
+ len -= 16;
+ }
+}
+
+/*
+ * Byteswap a 16-byte value.
+ */
+static void
+byteswap16(unsigned char *buf)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ unsigned x;
+
+ x = buf[i];
+ buf[i] = buf[15 - i];
+ buf[15 - i] = x;
+ }
+}
+
+/* see bearssl_block.h */
+void
+br_poly1305_i15_run(const void *key, const void *iv,
+ void *data, size_t len, const void *aad, size_t aad_len,
+ void *tag, br_chacha20_run ichacha, int encrypt)
+{
+ unsigned char pkey[32], foot[16];
+ uint16_t t[10], r[10], acc[10];
+
+ /*
+ * Compute the MAC key. The 'r' value is the first 16 bytes of
+ * pkey[].
+ */
+ memset(pkey, 0, sizeof pkey);
+ ichacha(key, iv, 0, pkey, sizeof pkey);
+
+ /*
+ * If encrypting, ChaCha20 must run first, followed by Poly1305.
+ * When decrypting, the operations are reversed.
+ */
+ if (encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+
+ /*
+ * Run Poly1305. We must process the AAD, then ciphertext, then
+ * the footer (with the lengths). Note that the AAD and ciphertext
+ * are meant to be padded with zeros up to the next multiple of 16,
+ * and the length of the footer is 16 bytes as well.
+ */
+
+ /*
+ * Apply the "clamping" operation on the encoded 'r' value.
+ */
+ pkey[ 3] &= 0x0F;
+ pkey[ 7] &= 0x0F;
+ pkey[11] &= 0x0F;
+ pkey[15] &= 0x0F;
+ pkey[ 4] &= 0xFC;
+ pkey[ 8] &= 0xFC;
+ pkey[12] &= 0xFC;
+
+ /*
+ * Decode the clamped 'r' value. Decoding should use little-endian
+ * so we must byteswap the value first.
+ */
+ byteswap16(pkey);
+ br_i15_decode_mod(t, pkey, 16, P1305);
+
+ /*
+ * Convert 'r' to Montgomery representation.
+ */
+ br_i15_montymul(r, t, R2, P1305, P0I);
+
+ /*
+ * Accumulator is 0.
+ */
+ br_i15_zero(acc, 0x8A);
+
+ /*
+ * Process the additional authenticated data, ciphertext, and
+ * footer in due order.
+ */
+ br_enc64le(foot, (uint64_t)aad_len);
+ br_enc64le(foot + 8, (uint64_t)len);
+ poly1305_inner(acc, r, aad, aad_len);
+ poly1305_inner(acc, r, data, len);
+ poly1305_inner(acc, r, foot, sizeof foot);
+
+ /*
+ * Decode the value 's'. Again, a byteswap is needed.
+ */
+ byteswap16(pkey + 16);
+ br_i15_decode_mod(t, pkey + 16, 16, P1305);
+
+ /*
+ * Add the value 's' to the accumulator. That addition is done
+ * modulo 2^128, so we just ignore the carry.
+ */
+ br_i15_add(acc, t, 1);
+
+ /*
+ * Encode the result (128 low bits) to the tag. Encoding should
+ * be little-endian.
+ */
+ br_i15_encode(tag, 16, acc);
+ byteswap16(tag);
+
+ /*
+ * If decrypting, then ChaCha20 runs _after_ Poly1305.
+ */
+ if (!encrypt) {
+ ichacha(key, iv, 1, data, len);
+ }
+}
diff --git a/test/monniaux/BearSSL/src/x509/asn1.t0 b/test/monniaux/BearSSL/src/x509/asn1.t0
new file mode 100644
index 00000000..ba592526
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/asn1.t0
@@ -0,0 +1,757 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+\ =======================================================================
+
+\ This file contains code which is common to all engines that do some
+\ ASN.1 decoding. It should not be compiled on its own, but only along
+\ with another file (e.g. x509_minimal.t0) which uses it.
+\
+\ Users must define several things:
+\
+\ -- In the preamble, a macro called "CTX" that evaluates to the current
+\ context structure.
+\
+\ -- In the preamble, a macro called "CONTEXT_NAME" that evaluates to the
+\ context structure type. This will be invoked during compilation.
+\
+\ -- A word called "read8-low" ( -- x ) that reads the next byte, or -1
+\ if the input buffer is empty. That word is usually written in C.
+\
+\ -- A word called "read-blob-inner" ( addr len -- addr len ) that is
+\ the multi-byte version of read8-low.
+\
+\ -- A word called "skip-remaining-inner" ( lim -- lim ) which reads but
+\ drops some input bytes.
+
+preamble {
+
+#include "inner.h"
+
+}
+
+\ Read next source character, skipping blanks.
+: skip-blanks begin char dup 32 > if ret then drop again ;
+
+: fail-oid
+ "Invalid OID" puts cr exitvm ;
+
+\ Read a decimal integer, followed by either a dot or whitespace.
+\ Note: this does not check for overflows.
+: parse-number ( -- val nextchar )
+ char decval
+ begin
+ char
+ dup dup `. = swap 32 <= or if ret then
+ decval swap 10 * +
+ again ;
+
+\ Encode a number in unsigned 7E format.
+: encode7E ( val -- )
+ 0 encode7E-inner ;
+
+: encode7E-inner ( val eb -- )
+ swap dup 0x7F > if
+ dup 7 u>> 0x80 encode7E-inner 0x7F and
+ then
+ or data-add8 ;
+
+\ Decode an OID from source, and encode it. First byte is length,
+\ followed by encoded ASN.1 DER value. The OID is encoded in the
+\ current data block.
+: OID
+ \ Get current data address, and push a 0 for length.
+ current-data 0 data-add8
+ \ Skip blanks and get first digit, which must be 0, 1 or 2.
+ skip-blanks decval dup 2 > if fail-oid then
+ 40 *
+ \ Next character must be a dot.
+ char `. <> if fail-oid then
+ \ Second group must be one or two digits.
+ parse-number { nextchar }
+ dup 40 >= if fail-oid then
+ + encode7E
+ \ While next character is a dot, keep encoding numbers.
+ begin nextchar `. = while
+ parse-number >nextchar
+ encode7E
+ repeat
+ \ Write back length in the first byte.
+ dup current-data swap - 1- swap data-set8
+ ; immediate
+
+\ Define a new data word for an encoded OID. The OID is read from the
+\ source.
+: OID:
+ new-data-block next-word define-data-word postpone OID ;
+
+\ Define a word that evaluates to the address of a field within the
+\ context.
+: addr:
+ next-word { field }
+ "addr-" field + 0 1 define-word
+ 0 8191 "offsetof(CONTEXT_NAME, " field + ")" + make-CX
+ postpone literal postpone ; ;
+
+addr: pad
+
+\ Define a word that evaluates to an error code through a macro name.
+: err:
+ next-word { name }
+ name 0 1 define-word
+ 0 63 "BR_" name + make-CX postpone literal postpone ; ;
+
+err: ERR_X509_INVALID_VALUE
+err: ERR_X509_TRUNCATED
+err: ERR_X509_EMPTY_CHAIN
+err: ERR_X509_INNER_TRUNC
+err: ERR_X509_BAD_TAG_CLASS
+err: ERR_X509_BAD_TAG_VALUE
+err: ERR_X509_INDEFINITE_LENGTH
+err: ERR_X509_EXTRA_ELEMENT
+err: ERR_X509_UNEXPECTED
+err: ERR_X509_NOT_CONSTRUCTED
+err: ERR_X509_NOT_PRIMITIVE
+err: ERR_X509_PARTIAL_BYTE
+err: ERR_X509_BAD_BOOLEAN
+err: ERR_X509_OVERFLOW
+err: ERR_X509_BAD_DN
+err: ERR_X509_BAD_TIME
+err: ERR_X509_UNSUPPORTED
+err: ERR_X509_LIMIT_EXCEEDED
+err: ERR_X509_WRONG_KEY_TYPE
+err: ERR_X509_BAD_SIGNATURE
+err: ERR_X509_EXPIRED
+err: ERR_X509_DN_MISMATCH
+err: ERR_X509_BAD_SERVER_NAME
+err: ERR_X509_CRITICAL_EXTENSION
+err: ERR_X509_NOT_CA
+err: ERR_X509_FORBIDDEN_KEY_USAGE
+err: ERR_X509_WEAK_PUBLIC_KEY
+
+: KEYTYPE_RSA CX 0 15 { BR_KEYTYPE_RSA } ;
+: KEYTYPE_EC CX 0 15 { BR_KEYTYPE_EC } ;
+
+cc: fail ( err -- ! ) {
+ CTX->err = T0_POPi();
+ T0_CO();
+}
+
+\ Read one byte from the stream.
+: read8-nc ( -- x )
+ begin
+ read8-low dup 0 >= if ret then
+ drop co
+ again ;
+
+\ Read one byte, enforcing current read limit.
+: read8 ( lim -- lim x )
+ dup ifnot ERR_X509_INNER_TRUNC fail then
+ 1- read8-nc ;
+
+\ Read a 16-bit value, big-endian encoding.
+: read16be ( lim -- lim x )
+ read8 8 << swap read8 rot + ;
+
+\ Read a 16-bit value, little-endian encoding.
+: read16le ( lim -- lim x )
+ read8 swap read8 8 << rot + ;
+
+\ Read all bytes from the current element, then close it (i.e. drop the
+\ limit). Destination address is an offset within the context.
+: read-blob ( lim addr -- )
+ swap
+ begin dup while read-blob-inner dup if co then repeat
+ 2drop ;
+
+\ Skip remaining bytes in the current structure, but do not close it
+\ (thus, this leaves the value 0 on the stack).
+: skip-remaining ( lim -- lim )
+ begin dup while skip-remaining-inner dup if co then repeat ;
+
+: skip-remaining-inner ( lim -- lim )
+ 0 over read-blob-inner -rot 2drop ;
+
+cc: set8 ( val addr -- ) {
+ uint32_t addr = T0_POP();
+ *((unsigned char *)CTX + addr) = (unsigned char)T0_POP();
+}
+
+cc: set16 ( val addr -- ) {
+ uint32_t addr = T0_POP();
+ *(uint16_t *)(void *)((unsigned char *)CTX + addr) = T0_POP();
+}
+
+cc: set32 ( val addr -- ) {
+ uint32_t addr = T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)CTX + addr) = T0_POP();
+}
+
+cc: get8 ( addr -- val ) {
+ uint32_t addr = T0_POP();
+ T0_PUSH(*((unsigned char *)CTX + addr));
+}
+
+cc: get16 ( addr -- val ) {
+ uint32_t addr = T0_POP();
+ T0_PUSH(*(uint16_t *)(void *)((unsigned char *)CTX + addr));
+}
+
+cc: get32 ( addr -- val ) {
+ uint32_t addr = T0_POP();
+ T0_PUSH(*(uint32_t *)(void *)((unsigned char *)CTX + addr));
+}
+
+\ Read an ASN.1 tag. This function returns the "constructed" status
+\ and the tag value. The constructed status is a boolean (-1 for
+\ constructed, 0 for primitive). The tag value is either 0 to 31 for
+\ a universal tag, or 32+x for a contextual tag of value x. Tag classes
+\ "application" and "private" are rejected. Universal tags beyond 30
+\ are rejected. Contextual tags beyond 30 are rejected. Thus, accepted
+\ tags will necessarily fit on exactly one byte. This does not support
+\ the whole of ASN.1/BER, but is sufficient for certificate parsing.
+: read-tag ( lim -- lim constructed value )
+ read8 { fb }
+
+ \ Constructed flag is bit 5.
+ fb 5 >> 0x01 and neg
+
+ \ Class is in bits 6 and 7. Accepted classes are 00 (universal)
+ \ and 10 (context). We check that bit 6 is 0, and shift back
+ \ bit 7 so that we get 0 (universal) or 32 (context).
+ fb 6 >> dup 0x01 and if ERR_X509_BAD_TAG_CLASS fail then
+ 4 <<
+
+ \ Tag value is in bits 0..4. If the value is 31, then this is
+ \ an extended tag, encoded over subsequent bytes, and we do
+ \ not support that.
+ fb 0x1F and dup 0x1F = if ERR_X509_BAD_TAG_VALUE fail then
+ + ;
+
+\ Read a tag, but only if not at the end of the current object. If there
+\ is no room for another element (limit is zero), then this will push a
+\ synthetic "no tag" value (primitive, with value -1).
+: read-tag-or-end ( lim -- lim constructed value )
+ dup ifnot 0 -1 ret then
+ read-tag ;
+
+\ Compare the read tag with the provided value. If equal, then the
+\ element is skipped, and a new tag is read (or end of object).
+: iftag-skip ( lim constructed value ref -- lim constructed value )
+ over = if
+ 2drop
+ read-length-open-elt skip-close-elt
+ read-tag-or-end
+ then ;
+
+\ Read an ASN.1 length. This supports only definite lengths (theoretically,
+\ certificates may use an indefinite length for the outer structure, using
+\ DER only in the TBS, but this never happens in practice, except in a
+\ single example certificate from 15 years ago that also fails to decode
+\ properly for other reasons).
+: read-length ( lim -- lim length )
+ read8
+ \ Lengths in 0x00..0x7F get encoded as a single byte.
+ dup 0x80 < if ret then
+
+ \ If the byte is 0x80 then this is an indefinite length, and we
+ \ do not support that.
+ 0x80 - dup ifnot ERR_X509_INDEFINITE_LENGTH fail then
+
+ \ Masking out bit 7, this yields the number of bytes over which
+ \ the value is encoded. Since the total certificate length must
+ \ fit over 3 bytes (this is a consequence of SSL/TLS message
+ \ format), we can reject big lengths and keep the length in a
+ \ single integer.
+ { n } 0
+ begin n 0 > while n 1- >n
+ dup 0x7FFFFF > if ERR_X509_INNER_TRUNC fail then
+ 8 << swap read8 rot +
+ repeat ;
+
+\ Open a sub-structure. This subtracts the length from the limit, and
+\ pushes the length back as new limit.
+: open-elt ( lim length -- lim_outer lim_inner )
+ dup2 < if ERR_X509_INNER_TRUNC fail then
+ dup { len } - len ;
+
+\ Read a length and open the value as a sub-structure.
+: read-length-open-elt ( lim -- lim_outer lim_inner )
+ read-length open-elt ;
+
+\ Close a sub-structure. This verifies that there is no remaining
+\ element to read.
+: close-elt ( lim -- )
+ if ERR_X509_EXTRA_ELEMENT fail then ;
+
+\ Skip remaining bytes in the current structure, then close it.
+: skip-close-elt ( lim -- )
+ skip-remaining drop ;
+
+\ Read a length and then skip the value.
+: read-length-skip ( lim -- lim )
+ read-length-open-elt skip-close-elt ;
+
+\ Check that a given tag is constructed and has the expected value.
+: check-tag-constructed ( constructed value refvalue -- )
+ = ifnot ERR_X509_UNEXPECTED fail then
+ check-constructed ;
+
+\ Check that the top value is true; report a "not constructed"
+\ error otherwise.
+: check-constructed ( constructed -- )
+ ifnot ERR_X509_NOT_CONSTRUCTED fail then ;
+
+\ Check that a given tag is primitive and has the expected value.
+: check-tag-primitive ( constructed value refvalue -- )
+ = ifnot ERR_X509_UNEXPECTED fail then
+ check-primitive ;
+
+\ Check that the top value is true; report a "not primitive"
+\ error otherwise.
+: check-primitive ( constructed -- )
+ if ERR_X509_NOT_PRIMITIVE fail then ;
+
+\ Check that the tag is for a constructed SEQUENCE.
+: check-sequence ( constructed value -- )
+ 0x10 check-tag-constructed ;
+
+\ Read a tag, check that it is for a constructed SEQUENCE, and open
+\ it as a sub-element.
+: read-sequence-open ( lim -- lim_outer lim_inner )
+ read-tag check-sequence read-length-open-elt ;
+
+\ Read the next element as a BIT STRING with no ignore bits, and open
+\ it as a sub-element.
+: read-bits-open ( lim -- lim_outer lim_inner )
+ read-tag 0x03 check-tag-primitive
+ read-length-open-elt
+ read8 if ERR_X509_PARTIAL_BYTE fail then ;
+
+OID: rsaEncryption 1.2.840.113549.1.1.1
+
+OID: sha1WithRSAEncryption 1.2.840.113549.1.1.5
+OID: sha224WithRSAEncryption 1.2.840.113549.1.1.14
+OID: sha256WithRSAEncryption 1.2.840.113549.1.1.11
+OID: sha384WithRSAEncryption 1.2.840.113549.1.1.12
+OID: sha512WithRSAEncryption 1.2.840.113549.1.1.13
+
+OID: id-sha1 1.3.14.3.2.26
+OID: id-sha224 2.16.840.1.101.3.4.2.4
+OID: id-sha256 2.16.840.1.101.3.4.2.1
+OID: id-sha384 2.16.840.1.101.3.4.2.2
+OID: id-sha512 2.16.840.1.101.3.4.2.3
+
+OID: id-ecPublicKey 1.2.840.10045.2.1
+
+OID: ansix9p256r1 1.2.840.10045.3.1.7
+OID: ansix9p384r1 1.3.132.0.34
+OID: ansix9p521r1 1.3.132.0.35
+
+OID: ecdsa-with-SHA1 1.2.840.10045.4.1
+OID: ecdsa-with-SHA224 1.2.840.10045.4.3.1
+OID: ecdsa-with-SHA256 1.2.840.10045.4.3.2
+OID: ecdsa-with-SHA384 1.2.840.10045.4.3.3
+OID: ecdsa-with-SHA512 1.2.840.10045.4.3.4
+
+OID: id-at-commonName 2.5.4.3
+
+\ Read a "small value". This assumes that the tag has just been read
+\ and processed, but not the length. The first pad byte is set to the
+\ value length; the encoded value itself follows. If the value length
+\ exceeds 255 bytes, then a single 0 is written in the pad, and this
+\ method returns false (0). Otherwise, it returns true (-1).
+\ Either way, the element is fully read.
+: read-small-value ( lim -- lim bool )
+ read-length-open-elt
+ dup 255 > if skip-close-elt 0 addr-pad set8 0 ret then
+ dup addr-pad set8
+ addr-pad 1+ read-blob
+ -1 ;
+
+\ Read an OID as a "small value" (tag, length and value). A boolean
+\ value is returned, which is true (-1) if the OID value fits on the pad,
+\ false (0) otherwise.
+: read-OID ( lim -- lim bool )
+ read-tag 0x06 check-tag-primitive read-small-value ;
+
+\ Read a UTF-8 code point. On error, return 0. Reading a code point of
+\ value 0 is considered to be an error.
+: read-UTF8 ( lim -- lim val )
+ read8
+ choice
+ dup 0x80 < uf ret enduf
+ dup 0xC0 < uf drop 0 ret enduf
+ dup 0xE0 < uf 0x1F and 1 read-UTF8-next 0x80 0x7FF enduf
+ dup 0xF0 < uf 0x0F and 2 read-UTF8-next 0x800 0xFFFF enduf
+ dup 0xF8 < uf 0x07 and 3 read-UTF8-next 0x10000 0x10FFFF enduf
+ drop 0 ret
+ endchoice
+ between? ifnot drop 0 then
+ ;
+
+\ Read n subsequent bytes to complete the provided first byte. The final
+\ value is -1 on error, or the code point numerical value. The final
+\ value is duplicated.
+: read-UTF8-next ( lim val n -- lim val val )
+ begin dup while
+ -rot
+ read-UTF8-chunk
+ rot 1-
+ repeat
+ drop dup ;
+
+\ Read one byte, that should be a trailing UTF-8 byte, and complement the
+\ current value. On error, value is set to -1.
+: read-UTF8-chunk ( lim val -- lim val )
+ swap
+ \ If we are at the end of the value, report an error but don't fail.
+ dup ifnot 2drop 0 -1 ret then
+ read8 rot
+ dup 0< if swap drop ret then 6 <<
+ swap dup 6 >> 2 <> if 2drop -1 ret then
+ 0x3F and + ;
+
+: high-surrogate? ( x -- x bool )
+ dup 0xD800 0xDBFF between? ;
+
+: low-surrogate? ( x -- x bool )
+ dup 0xDC00 0xDFFF between? ;
+
+: assemble-surrogate-pair ( hi lim lo -- lim val )
+ low-surrogate? ifnot rot 2drop 0 ret then
+ rot 10 << + 0x35FDC00 - ;
+
+\ Read a UTF-16 code point (big-endian). Returned value is 0 on error.
+: read-UTF16BE ( lim -- lim val )
+ read16be
+ choice
+ high-surrogate? uf
+ swap dup ifnot 2drop 0 0 ret then
+ read16be assemble-surrogate-pair
+ enduf
+ low-surrogate? uf
+ drop 0
+ enduf
+ endchoice ;
+
+\ Read a UTF-16 code point (little-endian). Returned value is 0 on error.
+: read-UTF16LE ( lim -- lim val )
+ read16le
+ choice
+ high-surrogate? uf
+ swap dup ifnot 2drop 0 0 ret then
+ read16le assemble-surrogate-pair
+ enduf
+ low-surrogate? uf
+ drop 0
+ enduf
+ endchoice ;
+
+\ Add byte to current pad value. Offset is updated, or set to 0 on error.
+: pad-append ( off val -- off )
+ over dup 0= swap 256 >= or if 2drop 0 ret then
+ over addr-pad + set8 1+ ;
+
+\ Add UTF-8 chunk byte to the pad. The 'nn' parameter is the shift count.
+: pad-append-UTF8-chunk ( off val nn -- off )
+ >> 0x3F and 0x80 or pad-append ;
+
+\ Test whether a code point is invalid when encoding. This rejects the
+\ 66 noncharacters, and also the surrogate range; this function does NOT
+\ check that the value is in the 0..10FFFF range.
+: valid-unicode? ( val -- bool )
+ dup 0xFDD0 0xFEDF between? if drop 0 ret then
+ dup 0xD800 0xDFFF between? if drop 0 ret then
+ 0xFFFF and 0xFFFE < ;
+
+\ Encode a code point in UTF-8. Offset is in the pad; it is updated, or
+\ set to 0 on error. Leading BOM are ignored.
+: encode-UTF8 ( val off -- off )
+ \ Skip leading BOM (U+FEFF when off is 1).
+ dup2 1 = swap 0xFEFF = and if swap drop ret then
+
+ swap dup { val }
+ dup valid-unicode? ifnot 2drop 0 ret then
+ choice
+ dup 0x80 < uf pad-append enduf
+ dup 0x800 < uf
+ 6 >> 0xC0 or pad-append
+ val 0 pad-append-UTF8-chunk
+ enduf
+ dup 0xFFFF < uf
+ 12 >> 0xE0 or pad-append
+ val 6 pad-append-UTF8-chunk
+ val 0 pad-append-UTF8-chunk
+ enduf
+ 18 >> 0xF0 or pad-append
+ val 12 pad-append-UTF8-chunk
+ val 6 pad-append-UTF8-chunk
+ val 0 pad-append-UTF8-chunk
+ endchoice ;
+
+\ Read a string value into the pad; this function checks that the source
+\ characters are UTF-8 and non-zero. The string length (in bytes) is
+\ written in the first pad byte. Returned value is true (-1) on success,
+\ false (0) on error.
+: read-value-UTF8 ( lim -- lim bool )
+ read-length-open-elt
+ 1 { off }
+ begin dup while
+ read-UTF8 dup ifnot drop skip-close-elt 0 ret then
+ off encode-UTF8 >off
+ repeat
+ drop off dup ifnot ret then 1- addr-pad set8 -1 ;
+
+\ Decode a UTF-16 string into the pad. The string is converted to UTF-8,
+\ and the length is written in the first pad byte. A leading BOM is
+\ honoured (big-endian is assumed if there is no BOM). A code point of
+\ value 0 is an error. Returned value is true (-1) on success, false (0)
+\ on error.
+: read-value-UTF16 ( lim -- lim bool )
+ read-length-open-elt
+ dup ifnot addr-pad set8 -1 ret then
+ 1 { off }
+ read-UTF16BE dup 0xFFFE = if
+ \ Leading BOM, and indicates little-endian.
+ drop
+ begin dup while
+ read-UTF16LE dup ifnot drop skip-close-elt 0 ret then
+ off encode-UTF8 >off
+ repeat
+ else
+ dup ifnot drop skip-close-elt 0 ret then
+ \ Big-endian BOM, or no BOM.
+ begin
+ off encode-UTF8 >off
+ dup while
+ read-UTF16BE dup ifnot drop skip-close-elt 0 ret then
+ repeat
+ then
+ drop off dup ifnot ret then 1- addr-pad set8 -1 ;
+
+\ Decode a latin-1 string into the pad. The string is converted to UTF-8,
+\ and the length is written in the first pad byte. A source byte of
+\ value 0 is an error. Returned value is true (-1) on success, false (0)
+\ on error.
+: read-value-latin1 ( lim -- lim bool )
+ read-length-open-elt
+ 1 { off }
+ begin dup while
+ read8 dup ifnot drop skip-close-elt 0 ret then
+ off encode-UTF8 >off
+ repeat
+ drop off dup ifnot ret then 1- addr-pad set8 -1 ;
+
+\ Read a value and interpret it as an INTEGER or ENUMERATED value. If
+\ the integer value does not fit on an unsigned 32-bit value, an error
+\ is reported. This function assumes that the tag has just been read
+\ and processed, but not the length.
+: read-small-int-value ( lim -- lim x )
+ read-length-open-elt
+ dup ifnot ERR_X509_OVERFLOW fail then
+ read8 dup 0x80 >= if ERR_X509_OVERFLOW fail then
+ { x }
+ begin dup while
+ read8 x dup 0xFFFFFF >= if ERR_X509_OVERFLOW fail then
+ 8 << + >x
+ repeat
+ drop x ;
+
+\ Compare the OID in the pad with an OID in the constant data block.
+\ Returned value is -1 on equality, 0 otherwise.
+cc: eqOID ( addrConst -- bool ) {
+ const unsigned char *a2 = &t0_datablock[T0_POP()];
+ const unsigned char *a1 = &CTX->pad[0];
+ size_t len = a1[0];
+ int x;
+ if (len == a2[0]) {
+ x = -(memcmp(a1 + 1, a2 + 1, len) == 0);
+ } else {
+ x = 0;
+ }
+ T0_PUSH((uint32_t)x);
+}
+
+\ Compare two blobs in the context. Returned value is -1 on equality, 0
+\ otherwise.
+cc: eqblob ( addr1 addr2 len -- bool ) {
+ size_t len = T0_POP();
+ const unsigned char *a2 = (const unsigned char *)CTX + T0_POP();
+ const unsigned char *a1 = (const unsigned char *)CTX + T0_POP();
+ T0_PUSHi(-(memcmp(a1, a2, len) == 0));
+}
+
+\ Check that a value is in a given range (inclusive).
+: between? ( x min max -- bool )
+ { min max } dup min >= swap max <= and ;
+
+\ Convert the provided byte value into a number in the 0..9 range,
+\ assuming that it is an ASCII digit. A non-digit triggers an error
+\ (a "bad time" error since this is used in date/time decoding).
+: digit-dec ( char -- value )
+ `0 - dup 0 9 between? ifnot ERR_X509_BAD_TIME fail then ;
+
+\ Read two ASCII digits and return the value in the 0..99 range. An
+\ error is reported if the characters are not ASCII digits.
+: read-dec2 ( lim -- lim x )
+ read8 digit-dec 10 * { x } read8 digit-dec x + ;
+
+\ Read two ASCII digits and check that the value is in the provided
+\ range (inclusive).
+: read-dec2-range ( lim min max -- lim x )
+ { min max }
+ read-dec2 dup min max between? ifnot ERR_X509_BAD_TIME fail then ;
+
+\ Maximum days in a month and accumulated day count. Each
+\ 16-bit value contains the month day count in its lower 5 bits. The first
+\ 12 values are for a normal year, the other 12 for a leap year.
+data: month-to-days
+hexb| 001F 03FC 077F 0B5E 0F1F 12FE 16BF 1A9F 1E7E 223F 261E 29DF |
+hexb| 001F 03FD 079F 0B7E 0F3F 131E 16DF 1ABF 1E9E 225F 263E 29FF |
+
+\ Read a date (UTCTime or GeneralizedTime). The date value is converted
+\ to a day count and a second count. The day count starts at 0 for
+\ January 1st, 0 AD (that's they year before 1 AD, also known as 1 BC)
+\ in a proleptic Gregorian calendar (i.e. Gregorian rules are assumed to
+\ extend indefinitely in the past). The second count is between 0 and
+\ 86400 (inclusive, in case of a leap second).
+: read-date ( lim -- lim days seconds )
+ \ Read tag; must be UTCTime or GeneralizedTime. Year count is
+ \ 4 digits with GeneralizedTime, 2 digits with UTCTime.
+ read-tag
+ dup 0x17 0x18 between? ifnot ERR_X509_BAD_TIME fail then
+ 0x18 = { y4d }
+ check-primitive
+ read-length-open-elt
+
+ \ We compute the days and seconds counts during decoding, in
+ \ order to minimize the number of needed temporary variables.
+ { ; days seconds x }
+
+ \ Year is 4-digit with GeneralizedTime. With UTCTime, the year
+ \ is in the 1950..2049 range, and only the last two digits are
+ \ present in the encoding.
+ read-dec2
+ y4d if
+ 100 * >x read-dec2 x +
+ else
+ dup 50 < if 100 + then 1900 +
+ then
+ >x
+ x 365 * x 3 + 4 / + x 99 + 100 / - x 399 + 400 / + >days
+
+ \ Month is 1..12. Number of days in a months depend on the
+ \ month and on the year (year count is in x at that point).
+ 1 12 read-dec2-range
+ 1- 1 <<
+ x 4 % 0= x 100 % 0<> x 400 % 0= or and if 24 + then
+ month-to-days + data-get16
+ dup 5 >> days + >days
+ 0x1F and
+
+ \ Day. At this point, the TOS contains the maximum day count for
+ \ the current month.
+ 1 swap read-dec2-range
+ days + 1- >days
+
+ \ Hour, minute and seconds. Count of seconds is allowed to go to
+ \ 60 in case of leap seconds (in practice, leap seconds really
+ \ occur only at the very end of the day, so this computation is
+ \ exact for a real leap second, and a spurious leap second only
+ \ implies a one-second shift that we can ignore).
+ 0 23 read-dec2-range 3600 * >seconds
+ 0 59 read-dec2-range 60 * seconds + >seconds
+ 0 60 read-dec2-range seconds + >seconds
+
+ \ At this point, we may have fractional seconds. This should
+ \ happen only with GeneralizedTime, but we accept it for UTCTime
+ \ too (and, anyway, we ignore these fractional seconds).
+ read8 dup `. = if
+ drop
+ begin read8 dup `0 `9 between? while drop repeat
+ then
+
+ \ The time zone should be 'Z', not followed by anything. Other
+ \ time zone indications are not DER and thus not supposed to
+ \ appear in certificates.
+ `Z <> if ERR_X509_BAD_TIME fail then
+ close-elt
+ days seconds ;
+
+\ Read an INTEGER (tag, length and value). The INTEGER is supposed to be
+\ positive; its unsigned big-endian encoding is stored in the provided
+\ in-context buffer. Returned value is the decoded length. If the integer
+\ did not fit, or the value is negative, then an error is reported.
+: read-integer ( lim addr len -- lim dlen )
+ rot read-tag 0x02 check-tag-primitive -rot
+ read-integer-next ;
+
+\ Identical to read-integer, but the tag has already been read and checked.
+: read-integer-next ( lim addr len -- lim dlen )
+ dup { addr len origlen }
+ read-length-open-elt
+ \ Read first byte; sign bit must be 0.
+ read8 dup 0x80 >= if ERR_X509_OVERFLOW fail then
+ \ Skip leading bytes of value 0. If there are only bytes of
+ \ value 0, then return.
+ begin dup 0 = while
+ drop dup ifnot drop 0 ret then
+ read8
+ repeat
+ \ At that point, we have the first non-zero byte on the stack.
+ begin
+ len dup ifnot ERR_X509_LIMIT_EXCEEDED fail then 1- >len
+ addr set8 addr 1+ >addr
+ dup while read8
+ repeat
+ drop origlen len - ;
+
+\ Read a BOOLEAN value. This should be called immediately after reading
+\ the tag.
+: read-boolean ( lim constructed value -- lim bool )
+ 0x01 check-tag-primitive
+ read-length 1 <> if ERR_X509_BAD_BOOLEAN fail then
+ read8 0<> ;
+
+\ Identify an elliptic curve: read the OID, then check it against the
+\ known curve OID.
+: read-curve-ID ( lim -- lim curve )
+ read-OID ifnot ERR_X509_UNSUPPORTED fail then
+ choice
+ ansix9p256r1 eqOID uf 23 enduf
+ ansix9p384r1 eqOID uf 24 enduf
+ ansix9p521r1 eqOID uf 25 enduf
+ ERR_X509_UNSUPPORTED fail
+ endchoice ;
+
+\ A convenient debug word: print the current data stack contents.
+cc: DEBUG ( -- ) {
+ extern int printf(const char *fmt, ...);
+ uint32_t *p;
+
+ printf("<stack:");
+ for (p = &CTX->dp_stack[0]; p != dp; p ++) {
+ printf(" %lu", (unsigned long)*p);
+ }
+ printf(" >\n");
+}
diff --git a/test/monniaux/BearSSL/src/x509/asn1enc.c b/test/monniaux/BearSSL/src/x509/asn1enc.c
new file mode 100644
index 00000000..7a749633
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/asn1enc.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+br_asn1_uint
+br_asn1_uint_prepare(const void *xdata, size_t xlen)
+{
+ const unsigned char *x;
+ br_asn1_uint t;
+
+ x = xdata;
+ while (xlen > 0 && *x == 0) {
+ x ++;
+ xlen --;
+ }
+ t.data = x;
+ t.len = xlen;
+ t.asn1len = xlen;
+ if (xlen == 0 || x[0] >= 0x80) {
+ t.asn1len ++;
+ }
+ return t;
+}
+
+/* see inner.h */
+size_t
+br_asn1_encode_length(void *dest, size_t len)
+{
+ unsigned char *buf;
+ size_t z;
+ int i, j;
+
+ buf = dest;
+ if (len < 0x80) {
+ if (buf != NULL) {
+ *buf = len;
+ }
+ return 1;
+ }
+ i = 0;
+ for (z = len; z != 0; z >>= 8) {
+ i ++;
+ }
+ if (buf != NULL) {
+ *buf ++ = 0x80 + i;
+ for (j = i - 1; j >= 0; j --) {
+ *buf ++ = len >> (j << 3);
+ }
+ }
+ return i + 1;
+}
+
+/* see inner.h */
+size_t
+br_asn1_encode_uint(void *dest, br_asn1_uint pp)
+{
+ unsigned char *buf;
+ size_t lenlen;
+
+ if (dest == NULL) {
+ return 1 + br_asn1_encode_length(NULL, pp.asn1len) + pp.asn1len;
+ }
+ buf = dest;
+ *buf ++ = 0x02;
+ lenlen = br_asn1_encode_length(buf, pp.asn1len);
+ buf += lenlen;
+ *buf = 0x00;
+ memcpy(buf + pp.asn1len - pp.len, pp.data, pp.len);
+ return 1 + lenlen + pp.asn1len;
+}
diff --git a/test/monniaux/BearSSL/src/x509/encode_ec_pk8der.c b/test/monniaux/BearSSL/src/x509/encode_ec_pk8der.c
new file mode 100644
index 00000000..53717ce3
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/encode_ec_pk8der.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_x509.h */
+size_t
+br_encode_ec_pkcs8_der(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk)
+{
+ /*
+ * ASN.1 format:
+ *
+ * OneAsymmetricKey ::= SEQUENCE {
+ * version Version,
+ * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
+ * privateKey PrivateKey,
+ * attributes [0] Attributes OPTIONAL,
+ * ...,
+ * [[2: publicKey [1] PublicKey OPTIONAL ]],
+ * ...
+ * }
+ *
+ * We don't include attributes or public key (the public key
+ * is included in the private key value instead). The
+ * 'version' field is an INTEGER that we will set to 0
+ * (meaning 'v1', compatible with previous versions of PKCS#8).
+ * The 'privateKeyAlgorithm' structure is an AlgorithmIdentifier
+ * whose OID should be id-ecPublicKey, with, as parameters, the
+ * curve OID. The 'privateKey' is an OCTET STRING, whose value
+ * is the "raw DER" encoding of the key pair.
+ */
+
+ /*
+ * OID id-ecPublicKey (1.2.840.10045.2.1), DER-encoded (with
+ * the tag).
+ */
+ static const unsigned char OID_ECPUBKEY[] = {
+ 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
+ };
+
+ size_t len_version, len_privateKeyAlgorithm, len_privateKeyValue;
+ size_t len_privateKey, len_seq;
+ const unsigned char *oid;
+
+ oid = br_get_curve_OID(sk->curve);
+ if (oid == NULL) {
+ return 0;
+ }
+ len_version = 3;
+ len_privateKeyAlgorithm = 2 + sizeof OID_ECPUBKEY + 2 + oid[0];
+ len_privateKeyValue = br_encode_ec_raw_der_inner(NULL, sk, pk, 0);
+ len_privateKey = 1 + len_of_len(len_privateKeyValue)
+ + len_privateKeyValue;
+ len_seq = len_version + len_privateKeyAlgorithm + len_privateKey;
+
+ if (dest == NULL) {
+ return 1 + len_of_len(len_seq) + len_seq;
+ } else {
+ unsigned char *buf;
+ size_t lenlen;
+
+ buf = dest;
+ *buf ++ = 0x30; /* SEQUENCE tag */
+ lenlen = br_asn1_encode_length(buf, len_seq);
+ buf += lenlen;
+
+ /* version */
+ *buf ++ = 0x02;
+ *buf ++ = 0x01;
+ *buf ++ = 0x00;
+
+ /* privateKeyAlgorithm */
+ *buf ++ = 0x30;
+ *buf ++ = (sizeof OID_ECPUBKEY) + 2 + oid[0];
+ memcpy(buf, OID_ECPUBKEY, sizeof OID_ECPUBKEY);
+ buf += sizeof OID_ECPUBKEY;
+ *buf ++ = 0x06;
+ memcpy(buf, oid, 1 + oid[0]);
+ buf += 1 + oid[0];
+
+ /* privateKey */
+ *buf ++ = 0x04;
+ buf += br_asn1_encode_length(buf, len_privateKeyValue);
+ br_encode_ec_raw_der_inner(buf, sk, pk, 0);
+
+ return 1 + lenlen + len_seq;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/x509/encode_ec_rawder.c b/test/monniaux/BearSSL/src/x509/encode_ec_rawder.c
new file mode 100644
index 00000000..5985909a
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/encode_ec_rawder.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see inner.h */
+const unsigned char *
+br_get_curve_OID(int curve)
+{
+ static const unsigned char OID_secp256r1[] = {
+ 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
+ };
+ static const unsigned char OID_secp384r1[] = {
+ 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22
+ };
+ static const unsigned char OID_secp521r1[] = {
+ 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23
+ };
+
+ switch (curve) {
+ case BR_EC_secp256r1: return OID_secp256r1;
+ case BR_EC_secp384r1: return OID_secp384r1;
+ case BR_EC_secp521r1: return OID_secp521r1;
+ default:
+ return NULL;
+ }
+}
+
+/* see inner.h */
+size_t
+br_encode_ec_raw_der_inner(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk,
+ int include_curve_oid)
+{
+ /*
+ * ASN.1 format:
+ *
+ * ECPrivateKey ::= SEQUENCE {
+ * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
+ * privateKey OCTET STRING,
+ * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
+ * publicKey [1] BIT STRING OPTIONAL
+ * }
+ *
+ * The tages '[0]' and '[1]' are explicit. The 'ECParameters'
+ * is a CHOICE; in our case, it will always be an OBJECT IDENTIFIER
+ * that identifies the curve.
+ *
+ * The value of the 'privateKey' field is the raw unsigned big-endian
+ * encoding of the private key (integer modulo the curve subgroup
+ * order); there is no INTEGER tag, and the leading bit may be 1.
+ * Also, leading bytes of value 0x00 are _not_ removed.
+ *
+ * The 'publicKey' contents are the raw encoded public key point,
+ * normally uncompressed (leading byte of value 0x04, followed
+ * by the unsigned big-endian encodings of the X and Y coordinates,
+ * padded to the full field length if necessary).
+ */
+
+ size_t len_version, len_privateKey, len_parameters, len_publicKey;
+ size_t len_publicKey_bits, len_seq;
+ const unsigned char *oid;
+
+ if (include_curve_oid) {
+ oid = br_get_curve_OID(sk->curve);
+ if (oid == NULL) {
+ return 0;
+ }
+ } else {
+ oid = NULL;
+ }
+ len_version = 3;
+ len_privateKey = 1 + len_of_len(sk->xlen) + sk->xlen;
+ if (include_curve_oid) {
+ len_parameters = 4 + oid[0];
+ } else {
+ len_parameters = 0;
+ }
+ if (pk == NULL) {
+ len_publicKey = 0;
+ len_publicKey_bits = 0;
+ } else {
+ len_publicKey_bits = 2 + len_of_len(pk->qlen) + pk->qlen;
+ len_publicKey = 1 + len_of_len(len_publicKey_bits)
+ + len_publicKey_bits;
+ }
+ len_seq = len_version + len_privateKey + len_parameters + len_publicKey;
+ if (dest == NULL) {
+ return 1 + len_of_len(len_seq) + len_seq;
+ } else {
+ unsigned char *buf;
+ size_t lenlen;
+
+ buf = dest;
+ *buf ++ = 0x30; /* SEQUENCE tag */
+ lenlen = br_asn1_encode_length(buf, len_seq);
+ buf += lenlen;
+
+ /* version */
+ *buf ++ = 0x02;
+ *buf ++ = 0x01;
+ *buf ++ = 0x01;
+
+ /* privateKey */
+ *buf ++ = 0x04;
+ buf += br_asn1_encode_length(buf, sk->xlen);
+ memcpy(buf, sk->x, sk->xlen);
+ buf += sk->xlen;
+
+ /* parameters */
+ if (include_curve_oid) {
+ *buf ++ = 0xA0;
+ *buf ++ = oid[0] + 2;
+ *buf ++ = 0x06;
+ memcpy(buf, oid, oid[0] + 1);
+ buf += oid[0] + 1;
+ }
+
+ /* publicKey */
+ if (pk != NULL) {
+ *buf ++ = 0xA1;
+ buf += br_asn1_encode_length(buf, len_publicKey_bits);
+ *buf ++ = 0x03;
+ buf += br_asn1_encode_length(buf, pk->qlen + 1);
+ *buf ++ = 0x00;
+ memcpy(buf, pk->q, pk->qlen);
+ /* buf += pk->qlen; */
+ }
+
+ return 1 + lenlen + len_seq;
+ }
+}
+
+/* see bearssl_x509.h */
+size_t
+br_encode_ec_raw_der(void *dest,
+ const br_ec_private_key *sk, const br_ec_public_key *pk)
+{
+ return br_encode_ec_raw_der_inner(dest, sk, pk, 1);
+}
diff --git a/test/monniaux/BearSSL/src/x509/encode_rsa_pk8der.c b/test/monniaux/BearSSL/src/x509/encode_rsa_pk8der.c
new file mode 100644
index 00000000..c053503d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/encode_rsa_pk8der.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_x509.h */
+size_t
+br_encode_rsa_pkcs8_der(void *dest, const br_rsa_private_key *sk,
+ const br_rsa_public_key *pk, const void *d, size_t dlen)
+{
+ /*
+ * ASN.1 format:
+ *
+ * OneAsymmetricKey ::= SEQUENCE {
+ * version Version,
+ * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
+ * privateKey PrivateKey,
+ * attributes [0] Attributes OPTIONAL,
+ * ...,
+ * [[2: publicKey [1] PublicKey OPTIONAL ]],
+ * ...
+ * }
+ *
+ * We don't include attributes or public key. The 'version' field
+ * is an INTEGER that we will set to 0 (meaning 'v1', compatible
+ * with previous versions of PKCS#8). The 'privateKeyAlgorithm'
+ * structure is an AlgorithmIdentifier whose OID should be
+ * rsaEncryption, with NULL parameters. The 'privateKey' is an
+ * OCTET STRING, whose value is the "raw DER" encoding of the
+ * key pair.
+ *
+ * Since the private key value comes last, this function really
+ * adds a header, which is mostly fixed (only some lengths have
+ * to be modified.
+ */
+
+ /*
+ * Concatenation of:
+ * - DER encoding of an INTEGER of value 0 (the 'version' field)
+ * - DER encoding of a PrivateKeyAlgorithmIdentifier that uses
+ * the rsaEncryption OID, and NULL parameters
+ * - An OCTET STRING tag
+ */
+ static const unsigned char PK8_HEAD[] = {
+ 0x02, 0x01, 0x00,
+ 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
+ 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
+ 0x04
+ };
+
+ size_t len_raw, len_seq;
+
+ len_raw = br_encode_rsa_raw_der(NULL, sk, pk, d, dlen);
+ len_seq = (sizeof PK8_HEAD) + len_of_len(len_raw) + len_raw;
+ if (dest == NULL) {
+ return 1 + len_of_len(len_seq) + len_seq;
+ } else {
+ unsigned char *buf;
+ size_t lenlen;
+
+ buf = dest;
+ *buf ++ = 0x30; /* SEQUENCE tag */
+ lenlen = br_asn1_encode_length(buf, len_seq);
+ buf += lenlen;
+
+ /* version, privateKeyAlgorithm, privateKey tag */
+ memcpy(buf, PK8_HEAD, sizeof PK8_HEAD);
+ buf += sizeof PK8_HEAD;
+
+ /* privateKey */
+ buf += br_asn1_encode_length(buf, len_raw);
+ br_encode_rsa_raw_der(buf, sk, pk, d, dlen);
+
+ return 1 + lenlen + len_seq;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/x509/encode_rsa_rawder.c b/test/monniaux/BearSSL/src/x509/encode_rsa_rawder.c
new file mode 100644
index 00000000..1a8052b0
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/encode_rsa_rawder.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_x509.h */
+size_t
+br_encode_rsa_raw_der(void *dest, const br_rsa_private_key *sk,
+ const br_rsa_public_key *pk, const void *d, size_t dlen)
+{
+ /*
+ * ASN.1 format:
+ *
+ * RSAPrivateKey ::= SEQUENCE {
+ * version Version,
+ * modulus INTEGER, -- n
+ * publicExponent INTEGER, -- e
+ * privateExponent INTEGER, -- d
+ * prime1 INTEGER, -- p
+ * prime2 INTEGER, -- q
+ * exponent1 INTEGER, -- d mod (p-1)
+ * exponent2 INTEGER, -- d mod (q-1)
+ * coefficient INTEGER, -- (inverse of q) mod p
+ * otherPrimeInfos OtherPrimeInfos OPTIONAL
+ * }
+ *
+ * The 'version' field is an INTEGER of value 0 (meaning: there
+ * are exactly two prime factors), and 'otherPrimeInfos' will
+ * be absent (because there are exactly two prime factors).
+ */
+
+ br_asn1_uint num[9];
+ size_t u, slen;
+
+ /*
+ * For all INTEGER values, get the pointer and length for the
+ * data bytes.
+ */
+ num[0] = br_asn1_uint_prepare(NULL, 0);
+ num[1] = br_asn1_uint_prepare(pk->n, pk->nlen);
+ num[2] = br_asn1_uint_prepare(pk->e, pk->elen);
+ num[3] = br_asn1_uint_prepare(d, dlen);
+ num[4] = br_asn1_uint_prepare(sk->p, sk->plen);
+ num[5] = br_asn1_uint_prepare(sk->q, sk->qlen);
+ num[6] = br_asn1_uint_prepare(sk->dp, sk->dplen);
+ num[7] = br_asn1_uint_prepare(sk->dq, sk->dqlen);
+ num[8] = br_asn1_uint_prepare(sk->iq, sk->iqlen);
+
+ /*
+ * Get the length of the SEQUENCE contents.
+ */
+ slen = 0;
+ for (u = 0; u < 9; u ++) {
+ uint32_t ilen;
+
+ ilen = num[u].asn1len;
+ slen += 1 + len_of_len(ilen) + ilen;
+ }
+
+ if (dest == NULL) {
+ return 1 + len_of_len(slen) + slen;
+ } else {
+ unsigned char *buf;
+ size_t lenlen;
+
+ buf = dest;
+ *buf ++ = 0x30; /* SEQUENCE tag */
+ lenlen = br_asn1_encode_length(buf, slen);
+ buf += lenlen;
+ for (u = 0; u < 9; u ++) {
+ buf += br_asn1_encode_uint(buf, num[u]);
+ }
+ return 1 + lenlen + slen;
+ }
+}
diff --git a/test/monniaux/BearSSL/src/x509/skey_decoder.c b/test/monniaux/BearSSL/src/x509/skey_decoder.c
new file mode 100644
index 00000000..f4e43e7b
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/skey_decoder.c
@@ -0,0 +1,650 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_skey_decoder_init_main(void *t0ctx);
+
+void br_skey_decoder_run(void *t0ctx);
+
+
+
+#include "inner.h"
+
+
+
+
+
+#include "inner.h"
+
+#define CTX ((br_skey_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_skey_decoder_context, cpu)))
+#define CONTEXT_NAME br_skey_decoder_context
+
+/* see bearssl_x509.h */
+void
+br_skey_decoder_init(br_skey_decoder_context *ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_skey_decoder_init_main(&ctx->cpu);
+ br_skey_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_x509.h */
+void
+br_skey_decoder_push(br_skey_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_skey_decoder_run(&ctx->cpu);
+}
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x07,
+ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x08, 0x2A, 0x86, 0x48, 0xCE,
+ 0x3D, 0x03, 0x01, 0x07, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x05, 0x2B,
+ 0x81, 0x04, 0x00, 0x23
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x01, 0x01, 0x08, 0x00, 0x00, 0x13,
+ 0x13, 0x00, 0x00, 0x01, T0_INT1(BR_ERR_X509_BAD_TAG_CLASS), 0x00, 0x00,
+ 0x01, T0_INT1(BR_ERR_X509_BAD_TAG_VALUE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_EXTRA_ELEMENT), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INDEFINITE_LENGTH), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INNER_TRUNC), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INVALID_VALUE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_LIMIT_EXCEEDED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_CONSTRUCTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_PRIMITIVE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_OVERFLOW), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNEXPECTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNSUPPORTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_KEYTYPE_EC), 0x00, 0x00, 0x01, T0_INT1(BR_KEYTYPE_RSA),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, key_data)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, key_type)), 0x00, 0x00,
+ 0x33, 0x48, 0x00, 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, pad)),
+ 0x00, 0x00, 0x01, 0x13, 0x00, 0x00, 0x01, 0x1C, 0x00, 0x00, 0x01, 0x22,
+ 0x00, 0x00, 0x05, 0x02, 0x2C, 0x16, 0x00, 0x00, 0x06, 0x02, 0x2D, 0x16,
+ 0x00, 0x00, 0x01, 0x10, 0x3D, 0x00, 0x00, 0x0D, 0x05, 0x02, 0x2F, 0x16,
+ 0x3A, 0x00, 0x00, 0x0D, 0x05, 0x02, 0x2F, 0x16, 0x3B, 0x00, 0x00, 0x06,
+ 0x02, 0x27, 0x16, 0x00, 0x01, 0x03, 0x00, 0x54, 0x57, 0x01, 0x02, 0x3E,
+ 0x55, 0x23, 0x06, 0x02, 0x30, 0x16, 0x57, 0x01, 0x04, 0x3E, 0x02, 0x00,
+ 0x41, 0x3F, 0x00, 0x02, 0x03, 0x00, 0x53, 0x14, 0x14, 0x03, 0x01, 0x48,
+ 0x0E, 0x06, 0x02, 0x30, 0x16, 0x33, 0x4C, 0x58, 0x01, 0x7F, 0x19, 0x0D,
+ 0x06, 0x04, 0x13, 0x13, 0x04, 0x29, 0x01, 0x20, 0x19, 0x0D, 0x06, 0x16,
+ 0x13, 0x3A, 0x53, 0x4D, 0x02, 0x00, 0x06, 0x09, 0x02, 0x00, 0x0C, 0x06,
+ 0x02, 0x2A, 0x16, 0x04, 0x02, 0x03, 0x00, 0x3F, 0x04, 0x0D, 0x01, 0x21,
+ 0x19, 0x0D, 0x06, 0x04, 0x13, 0x3A, 0x04, 0x03, 0x30, 0x16, 0x13, 0x5D,
+ 0x02, 0x00, 0x05, 0x02, 0x30, 0x16, 0x02, 0x00, 0x02, 0x01, 0x1D, 0x00,
+ 0x02, 0x53, 0x4B, 0x05, 0x02, 0x30, 0x16, 0x5B, 0x15, 0x06, 0x07, 0x5D,
+ 0x01, 0x7F, 0x03, 0x01, 0x04, 0x16, 0x46, 0x15, 0x06, 0x10, 0x01, 0x00,
+ 0x03, 0x01, 0x14, 0x06, 0x03, 0x4D, 0x04, 0x02, 0x01, 0x00, 0x03, 0x00,
+ 0x04, 0x02, 0x30, 0x16, 0x3F, 0x57, 0x01, 0x04, 0x3E, 0x53, 0x02, 0x01,
+ 0x06, 0x03, 0x43, 0x04, 0x03, 0x02, 0x00, 0x40, 0x3F, 0x5D, 0x02, 0x01,
+ 0x06, 0x03, 0x32, 0x04, 0x01, 0x31, 0x00, 0x00, 0x54, 0x57, 0x01, 0x02,
+ 0x3E, 0x55, 0x06, 0x02, 0x30, 0x16, 0x57, 0x01, 0x02, 0x3E, 0x44, 0x3F,
+ 0x00, 0x07, 0x35, 0x50, 0x14, 0x05, 0x02, 0x2F, 0x16, 0x23, 0x01, 0x03,
+ 0x0B, 0x33, 0x17, 0x47, 0x07, 0x03, 0x00, 0x4F, 0x4F, 0x35, 0x4E, 0x14,
+ 0x14, 0x03, 0x01, 0x03, 0x02, 0x51, 0x14, 0x03, 0x03, 0x02, 0x02, 0x07,
+ 0x14, 0x03, 0x02, 0x51, 0x14, 0x03, 0x04, 0x02, 0x02, 0x07, 0x14, 0x03,
+ 0x02, 0x51, 0x14, 0x03, 0x05, 0x02, 0x02, 0x07, 0x14, 0x03, 0x02, 0x51,
+ 0x03, 0x06, 0x02, 0x00, 0x02, 0x01, 0x02, 0x03, 0x02, 0x04, 0x02, 0x05,
+ 0x02, 0x06, 0x1E, 0x00, 0x00, 0x19, 0x19, 0x00, 0x00, 0x01, 0x0B, 0x00,
+ 0x00, 0x01, 0x00, 0x20, 0x14, 0x06, 0x08, 0x01, 0x01, 0x21, 0x20, 0x22,
+ 0x20, 0x04, 0x75, 0x13, 0x00, 0x00, 0x01,
+ T0_INT2(3 * BR_X509_BUFSIZE_KEY), 0x00, 0x01, 0x01, 0x87, 0xFF, 0xFF,
+ 0x7F, 0x54, 0x57, 0x01, 0x02, 0x3E, 0x55, 0x01, 0x01, 0x0E, 0x06, 0x02,
+ 0x30, 0x16, 0x57, 0x01, 0x02, 0x19, 0x0D, 0x06, 0x06, 0x13, 0x3B, 0x44,
+ 0x32, 0x04, 0x1C, 0x01, 0x04, 0x19, 0x0D, 0x06, 0x08, 0x13, 0x3B, 0x01,
+ 0x00, 0x41, 0x31, 0x04, 0x0E, 0x01, 0x10, 0x19, 0x0D, 0x06, 0x05, 0x13,
+ 0x3A, 0x42, 0x04, 0x03, 0x30, 0x16, 0x13, 0x03, 0x00, 0x3F, 0x02, 0x00,
+ 0x34, 0x1F, 0x5A, 0x27, 0x16, 0x00, 0x01, 0x45, 0x0A, 0x06, 0x02, 0x29,
+ 0x16, 0x14, 0x03, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x57, 0x01, 0x06,
+ 0x3E, 0x56, 0x00, 0x00, 0x20, 0x14, 0x06, 0x07, 0x1A, 0x14, 0x06, 0x01,
+ 0x12, 0x04, 0x76, 0x24, 0x00, 0x00, 0x4B, 0x05, 0x02, 0x30, 0x16, 0x37,
+ 0x15, 0x06, 0x04, 0x01, 0x17, 0x04, 0x12, 0x38, 0x15, 0x06, 0x04, 0x01,
+ 0x18, 0x04, 0x0A, 0x39, 0x15, 0x06, 0x04, 0x01, 0x19, 0x04, 0x02, 0x30,
+ 0x16, 0x00, 0x00, 0x1C, 0x57, 0x01, 0x02, 0x3E, 0x09, 0x50, 0x00, 0x00,
+ 0x35, 0x4E, 0x13, 0x00, 0x03, 0x14, 0x03, 0x00, 0x03, 0x01, 0x03, 0x02,
+ 0x53, 0x59, 0x14, 0x01, 0x81, 0x00, 0x0F, 0x06, 0x02, 0x2E, 0x16, 0x14,
+ 0x01, 0x00, 0x0D, 0x06, 0x0B, 0x13, 0x14, 0x05, 0x04, 0x13, 0x01, 0x00,
+ 0x00, 0x59, 0x04, 0x6F, 0x02, 0x01, 0x14, 0x05, 0x02, 0x2B, 0x16, 0x23,
+ 0x03, 0x01, 0x02, 0x02, 0x1F, 0x02, 0x02, 0x22, 0x03, 0x02, 0x14, 0x06,
+ 0x03, 0x59, 0x04, 0x68, 0x13, 0x02, 0x00, 0x02, 0x01, 0x08, 0x00, 0x00,
+ 0x14, 0x35, 0x1C, 0x08, 0x20, 0x1C, 0x07, 0x20, 0x4E, 0x00, 0x01, 0x59,
+ 0x14, 0x01, 0x81, 0x00, 0x0A, 0x06, 0x01, 0x00, 0x01, 0x81, 0x00, 0x08,
+ 0x14, 0x05, 0x02, 0x28, 0x16, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01,
+ 0x00, 0x0E, 0x06, 0x19, 0x02, 0x00, 0x23, 0x03, 0x00, 0x14, 0x01, 0x83,
+ 0xFF, 0xFF, 0x7F, 0x0E, 0x06, 0x02, 0x29, 0x16, 0x01, 0x08, 0x0B, 0x20,
+ 0x59, 0x1C, 0x07, 0x04, 0x60, 0x00, 0x00, 0x52, 0x4A, 0x00, 0x00, 0x57,
+ 0x3C, 0x53, 0x00, 0x01, 0x53, 0x14, 0x05, 0x02, 0x2E, 0x16, 0x59, 0x14,
+ 0x01, 0x81, 0x00, 0x0F, 0x06, 0x02, 0x2E, 0x16, 0x03, 0x00, 0x14, 0x06,
+ 0x16, 0x59, 0x02, 0x00, 0x14, 0x01, 0x87, 0xFF, 0xFF, 0x7F, 0x0F, 0x06,
+ 0x02, 0x2E, 0x16, 0x01, 0x08, 0x0B, 0x07, 0x03, 0x00, 0x04, 0x67, 0x13,
+ 0x02, 0x00, 0x00, 0x00, 0x53, 0x14, 0x01, 0x81, 0x7F, 0x0E, 0x06, 0x08,
+ 0x5C, 0x01, 0x00, 0x36, 0x1F, 0x01, 0x00, 0x00, 0x14, 0x36, 0x1F, 0x36,
+ 0x22, 0x4C, 0x01, 0x7F, 0x00, 0x01, 0x59, 0x03, 0x00, 0x02, 0x00, 0x01,
+ 0x05, 0x10, 0x01, 0x01, 0x11, 0x18, 0x02, 0x00, 0x01, 0x06, 0x10, 0x14,
+ 0x01, 0x01, 0x11, 0x06, 0x02, 0x25, 0x16, 0x01, 0x04, 0x0B, 0x02, 0x00,
+ 0x01, 0x1F, 0x11, 0x14, 0x01, 0x1F, 0x0D, 0x06, 0x02, 0x26, 0x16, 0x07,
+ 0x00, 0x00, 0x14, 0x05, 0x05, 0x01, 0x00, 0x01, 0x7F, 0x00, 0x57, 0x00,
+ 0x00, 0x14, 0x05, 0x02, 0x29, 0x16, 0x23, 0x5A, 0x00, 0x00, 0x1B, 0x14,
+ 0x01, 0x00, 0x0F, 0x06, 0x01, 0x00, 0x13, 0x12, 0x04, 0x74, 0x00, 0x01,
+ 0x01, 0x00, 0x00, 0x5D, 0x13, 0x00, 0x00, 0x14, 0x06, 0x07, 0x5E, 0x14,
+ 0x06, 0x01, 0x12, 0x04, 0x76, 0x00, 0x00, 0x01, 0x00, 0x19, 0x1A, 0x09,
+ 0x24, 0x00
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 14,
+ 18,
+ 22,
+ 26,
+ 30,
+ 34,
+ 38,
+ 42,
+ 46,
+ 50,
+ 54,
+ 58,
+ 62,
+ 66,
+ 70,
+ 75,
+ 80,
+ 84,
+ 89,
+ 93,
+ 97,
+ 101,
+ 107,
+ 113,
+ 118,
+ 126,
+ 134,
+ 140,
+ 163,
+ 244,
+ 311,
+ 329,
+ 404,
+ 408,
+ 412,
+ 429,
+ 434,
+ 505,
+ 519,
+ 526,
+ 540,
+ 573,
+ 582,
+ 587,
+ 654,
+ 665,
+ 721,
+ 725,
+ 730,
+ 778,
+ 804,
+ 848,
+ 859,
+ 868,
+ 881,
+ 885,
+ 889,
+ 901
+};
+
+#define T0_INTERPRETED 34
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_skey_decoder_init_main, 73)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_skey_decoder_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 8: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 9: {
+ /* -rot */
+ T0_NROT();
+ }
+ break;
+ case 10: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 11: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 12: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 13: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 14: {
+ /* > */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a > b));
+
+ }
+ break;
+ case 15: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 16: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 17: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 18: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 19: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 20: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 21: {
+ /* eqOID */
+
+ const unsigned char *a2 = &t0_datablock[T0_POP()];
+ const unsigned char *a1 = &CTX->pad[0];
+ size_t len = a1[0];
+ int x;
+ if (len == a2[0]) {
+ x = -(memcmp(a1 + 1, a2 + 1, len) == 0);
+ } else {
+ x = 0;
+ }
+ T0_PUSH((uint32_t)x);
+
+ }
+ break;
+ case 22: {
+ /* fail */
+
+ CTX->err = T0_POPi();
+ T0_CO();
+
+ }
+ break;
+ case 23: {
+ /* get8 */
+
+ uint32_t addr = T0_POP();
+ T0_PUSH(*((unsigned char *)CTX + addr));
+
+ }
+ break;
+ case 24: {
+ /* neg */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(-a);
+
+ }
+ break;
+ case 25: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 26: {
+ /* read-blob-inner */
+
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+
+ }
+ break;
+ case 27: {
+ /* read8-low */
+
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ CTX->hlen --;
+ T0_PUSH(*CTX->hbuf ++);
+ }
+
+ }
+ break;
+ case 28: {
+ /* rot */
+ T0_ROT();
+ }
+ break;
+ case 29: {
+ /* set-ec-key */
+
+ size_t xlen = T0_POP();
+ uint32_t curve = T0_POP();
+ CTX->key.ec.curve = curve;
+ CTX->key.ec.x = CTX->key_data;
+ CTX->key.ec.xlen = xlen;
+
+ }
+ break;
+ case 30: {
+ /* set-rsa-key */
+
+ size_t iqlen = T0_POP();
+ size_t dqlen = T0_POP();
+ size_t dplen = T0_POP();
+ size_t qlen = T0_POP();
+ size_t plen = T0_POP();
+ uint32_t n_bitlen = T0_POP();
+ size_t off;
+
+ CTX->key.rsa.n_bitlen = n_bitlen;
+ CTX->key.rsa.p = CTX->key_data;
+ CTX->key.rsa.plen = plen;
+ off = plen;
+ CTX->key.rsa.q = CTX->key_data + off;
+ CTX->key.rsa.qlen = qlen;
+ off += qlen;
+ CTX->key.rsa.dp = CTX->key_data + off;
+ CTX->key.rsa.dplen = dplen;
+ off += dplen;
+ CTX->key.rsa.dq = CTX->key_data + off;
+ CTX->key.rsa.dqlen = dqlen;
+ off += dqlen;
+ CTX->key.rsa.iq = CTX->key_data + off;
+ CTX->key.rsa.iqlen = iqlen;
+
+ }
+ break;
+ case 31: {
+ /* set8 */
+
+ uint32_t addr = T0_POP();
+ *((unsigned char *)CTX + addr) = (unsigned char)T0_POP();
+
+ }
+ break;
+ case 32: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ case 33: {
+ /* u>> */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x >> c);
+
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
diff --git a/test/monniaux/BearSSL/src/x509/skey_decoder.t0 b/test/monniaux/BearSSL/src/x509/skey_decoder.t0
new file mode 100644
index 00000000..5b594211
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/skey_decoder.t0
@@ -0,0 +1,373 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+preamble {
+
+#include "inner.h"
+
+#define CTX ((br_skey_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_skey_decoder_context, cpu)))
+#define CONTEXT_NAME br_skey_decoder_context
+
+/* see bearssl_x509.h */
+void
+br_skey_decoder_init(br_skey_decoder_context *ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_skey_decoder_init_main(&ctx->cpu);
+ br_skey_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_x509.h */
+void
+br_skey_decoder_push(br_skey_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_skey_decoder_run(&ctx->cpu);
+}
+
+}
+
+addr: key_type
+addr: key_data
+
+cc: read8-low ( -- x ) {
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ CTX->hlen --;
+ T0_PUSH(*CTX->hbuf ++);
+ }
+}
+
+cc: read-blob-inner ( addr len -- addr len ) {
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+}
+
+\ Get the length of the key_data buffer.
+: len-key_data
+ CX 0 8191 { 3 * BR_X509_BUFSIZE_KEY } ;
+
+\ Get the address and length for the key_data buffer.
+: addr-len-key_data ( -- addr len )
+ addr-key_data len-key_data ;
+
+\ Set the private key (RSA).
+cc: set-rsa-key ( n_bitlen plen qlen dplen dqlen iqlen -- ) {
+ size_t iqlen = T0_POP();
+ size_t dqlen = T0_POP();
+ size_t dplen = T0_POP();
+ size_t qlen = T0_POP();
+ size_t plen = T0_POP();
+ uint32_t n_bitlen = T0_POP();
+ size_t off;
+
+ CTX->key.rsa.n_bitlen = n_bitlen;
+ CTX->key.rsa.p = CTX->key_data;
+ CTX->key.rsa.plen = plen;
+ off = plen;
+ CTX->key.rsa.q = CTX->key_data + off;
+ CTX->key.rsa.qlen = qlen;
+ off += qlen;
+ CTX->key.rsa.dp = CTX->key_data + off;
+ CTX->key.rsa.dplen = dplen;
+ off += dplen;
+ CTX->key.rsa.dq = CTX->key_data + off;
+ CTX->key.rsa.dqlen = dqlen;
+ off += dqlen;
+ CTX->key.rsa.iq = CTX->key_data + off;
+ CTX->key.rsa.iqlen = iqlen;
+}
+
+\ Set the private key (EC).
+cc: set-ec-key ( curve xlen -- ) {
+ size_t xlen = T0_POP();
+ uint32_t curve = T0_POP();
+ CTX->key.ec.curve = curve;
+ CTX->key.ec.x = CTX->key_data;
+ CTX->key.ec.xlen = xlen;
+}
+
+\ Get the bit length for an integer (unsigned).
+: int-bit-length ( x -- bitlen )
+ 0 swap
+ begin dup while 1 u>> swap 1+ swap repeat
+ drop ;
+
+\ Read an INTEGER into the key_data buffer, but then ignore it.
+: read-integer-ignore ( lim -- lim )
+ addr-len-key_data read-integer drop ;
+
+\ Read an INTEGER into the key_data buffer, at the provided offset.
+\ Returned value is the integer length (in bytes).
+: read-integer-off ( lim off -- lim dlen )
+ dup addr-len-key_data rot - swap rot + swap read-integer ;
+
+\ Decode RSA key, starting with the SEQUENCE tag.
+: decode-RSA ( lim -- lim )
+ read-sequence-open
+
+ \ Version should be 0.
+ read-tag 0x02 check-tag-primitive read-small-int-value if
+ ERR_X509_UNSUPPORTED fail
+ then
+
+ \ Read tag for the modulus; should be INTEGER. Then use the
+ \ decode-RSA-next function for the remainder of the key.
+ read-tag 0x02 check-tag-primitive
+ decode-RSA-next
+
+ \ Close the SEQUENCE.
+ close-elt ;
+
+\ Decode RSA key; the version, and the tag for the modulus, have been
+\ read.
+: decode-RSA-next ( lim -- lim )
+ \ Modulus: we read it but we do not keep it; we merely gather
+ \ the modulus bit length.
+ addr-len-key_data read-integer-next
+ dup ifnot ERR_X509_UNEXPECTED fail then
+ 1- 3 << addr-key_data get8 int-bit-length + { n_bitlen }
+
+ \ Public exponent: read but skip.
+ read-integer-ignore
+
+ \ Private exponent: read but skip.
+ read-integer-ignore
+
+ \ First prime factor.
+ addr-len-key_data read-integer dup dup { off plen }
+
+ \ Second prime factor.
+ read-integer-off dup { qlen } off + dup >off
+
+ \ First reduced private exponent.
+ read-integer-off dup { dplen } off + dup >off
+
+ \ Second reduced private exponent.
+ read-integer-off dup { dqlen } off + dup >off
+
+ \ CRT coefficient.
+ read-integer-off { iqlen }
+
+ \ Set RSA key.
+ n_bitlen plen qlen dplen dqlen iqlen set-rsa-key
+
+ \ The caller will close the sequence, thereby validating that there
+ \ is no extra field.
+ ;
+
+\ Decode an EC key, starting with the SEQUENCE tag.
+: decode-EC ( lim curve -- lim )
+ { curve }
+ read-sequence-open
+
+ \ Version should be 1.
+ read-tag 0x02 check-tag-primitive read-small-int-value 1- if
+ ERR_X509_UNSUPPORTED fail
+ then
+
+ \ Read tag for the private key; should be OCTET STRING. Then use the
+ \ decode-EC-next function for the remainder of the key.
+ read-tag 0x04 check-tag-primitive
+ curve decode-EC-next
+
+ \ Close the SEQUENCE.
+ close-elt ;
+
+\ Decode an EC key; the version, and the tag for the OCTET STRING, have
+\ already been read. The curve ID is provided (0 if unknown).
+: decode-EC-next ( lim curve -- lim )
+ { curve }
+
+ \ Read the private key proper.
+ read-length-open-elt
+ dup dup { xlen } len-key_data > if ERR_X509_UNSUPPORTED fail then
+ addr-key_data read-blob
+
+ \ Next element might be the curve identifier.
+ read-tag-or-end
+ case
+
+ \ End of structure.
+ -1 of drop endof
+
+ \ Curve parameters; we support only named curves.
+ 0x20 of
+ check-constructed read-length-open-elt
+ read-curve-ID
+ curve if
+ curve <> if ERR_X509_INVALID_VALUE fail then
+ else
+ >curve
+ then
+ close-elt
+ endof
+
+ \ Public key. We ignore it.
+ 0x21 of check-constructed endof
+
+ ERR_X509_UNSUPPORTED fail
+ endcase
+ skip-remaining
+
+ \ The curve must have been defined one way or another.
+ curve ifnot ERR_X509_UNSUPPORTED fail then
+
+ \ Set the EC key.
+ curve xlen set-ec-key
+
+ \ The caller will close the sequence.
+ ;
+
+\ Decode a PKCS#8 object. The version and the tag for the AlgorithmIdentifier
+\ structure have already been read. This function returns the key type.
+: decode-PKCS8-next ( lim -- lim keytype )
+ \ Decode the AlgorithmIdentifier.
+ read-length-open-elt
+ read-OID ifnot ERR_X509_UNSUPPORTED fail then
+ { ; is-rsa curve }
+ choice
+ rsaEncryption eqOID uf
+ \ RSA private key. We ignore the parameters.
+ skip-remaining -1 >is-rsa
+ enduf
+ id-ecPublicKey eqOID uf
+ \ EC private key. Parameters, if present, shall
+ \ identify the curve.
+ 0 >is-rsa
+ dup if read-curve-ID else 0 then >curve
+ enduf
+
+ ERR_X509_UNSUPPORTED fail
+ endchoice
+ close-elt
+
+ \ Open private key value and decode it.
+ read-tag 0x04 check-tag-primitive
+ read-length-open-elt
+ is-rsa if
+ decode-RSA
+ else
+ curve decode-EC
+ then
+ close-elt
+
+ \ We ignore any extra field, i.e. attributes or public key.
+ skip-remaining
+
+ \ Return the key type.
+ is-rsa if KEYTYPE_RSA else KEYTYPE_EC then
+ ;
+
+\ Decode a private key.
+: main ( -- ! )
+ \ RSA private key format is defined in PKCS#1 (RFC 3447):
+ \ RSAPrivateKey ::= SEQUENCE {
+ \ version INTEGER, -- 0 or 1
+ \ n INTEGER,
+ \ e INTEGER,
+ \ d INTEGER,
+ \ p INTEGER,
+ \ q INTEGER,
+ \ dp INTEGER,
+ \ dq INTEGER,
+ \ iq INTEGER,
+ \ other OtherPrimeInfos OPTIONAL
+ \ }
+ \ We do not support keys with more than two primes (these have
+ \ version 1); thus, we expect the version field to be 0, and
+ \ the 'other' field to be absent.
+ \
+ \ EC private key format is defined in RFC 5915:
+ \ ECPrivateKey ::= SEQUENCE {
+ \ version INTEGER, -- always 1
+ \ privateKey OCTET STRING,
+ \ parameters [0] EXPLICIT OBJECT IDENTIFIER OPTIONAL,
+ \ publicKey [1] EXPLICIT BIT STRING OPTIONAL
+ \ }
+ \ The "parameters" might conceptually be a complex curve description
+ \ structure but we support only named curves. The private key
+ \ contents are the unsigned big-endian encoding of the key value,
+ \ which is exactly what we want.
+ \
+ \ PKCS#8 (unencrypted) is:
+ \ OneAsymmetricKey ::= SEQUENCE {
+ \ version INTEGER, -- 0 or 1
+ \ algorithm AlgorithmIdentifier,
+ \ privateKey OCTET STRING,
+ \ attributes [0] IMPLICIT Attributes OPTIONAL,
+ \ publicKey [1] IMPLICIT BIT STRING OPTIONAL
+ \ }
+ \ The 'publicKey' field is an add-on from RFC 5958 and may be
+ \ present only if the 'version' is v2 (i.e. has value 1). We
+ \ ignore it anyway.
+
+ \ An arbitrary upper limit on the private key size.
+ 0xFFFFFF
+
+ \ Open the outer SEQUENCE.
+ read-sequence-open
+
+ \ All our schemas begin with a small INTEGER which is either 0 or
+ \ 1. We don't care which it is.
+ read-tag 0x02 check-tag-primitive read-small-int-value 1 > if
+ ERR_X509_UNSUPPORTED fail
+ then
+
+ \ Get next tag: it should be either an INTEGER (RSA private key),
+ \ an OCTET STRING (EC private key), or a SEQUENCE (for an
+ \ AlgorithmIdentifier, in a PKCS#8 object).
+ read-tag
+ case
+ 0x02 of check-primitive decode-RSA-next KEYTYPE_RSA endof
+ 0x04 of check-primitive 0 decode-EC-next KEYTYPE_EC endof
+ 0x10 of check-constructed decode-PKCS8-next endof
+ ERR_X509_UNSUPPORTED fail
+ endcase
+ { key-type }
+
+ \ Close the SEQUENCE.
+ close-elt
+
+ \ Set the key type, which marks the decoding as a success.
+ key-type addr-key_type set8
+
+ \ Read one byte, then fail: if the read succeeds, then there is
+ \ some trailing byte.
+ read8-nc ERR_X509_EXTRA_ELEMENT fail
+ ;
diff --git a/test/monniaux/BearSSL/src/x509/x509_decoder.c b/test/monniaux/BearSSL/src/x509/x509_decoder.c
new file mode 100644
index 00000000..8dd970f1
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_decoder.c
@@ -0,0 +1,773 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_x509_decoder_init_main(void *t0ctx);
+
+void br_x509_decoder_run(void *t0ctx);
+
+
+
+#include "inner.h"
+
+
+
+
+
+#include "inner.h"
+
+#define CTX ((br_x509_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_x509_decoder_context, cpu)))
+#define CONTEXT_NAME br_x509_decoder_context
+
+/* see bearssl_x509.h */
+void
+br_x509_decoder_init(br_x509_decoder_context *ctx,
+ void (*append_dn)(void *ctx, const void *buf, size_t len),
+ void *append_dn_ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ /* obsolete
+ ctx->err = 0;
+ ctx->hbuf = NULL;
+ ctx->hlen = 0;
+ */
+ ctx->append_dn = append_dn;
+ ctx->append_dn_ctx = append_dn_ctx;
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_x509_decoder_init_main(&ctx->cpu);
+ br_x509_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_x509.h */
+void
+br_x509_decoder_push(br_x509_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_x509_decoder_run(&ctx->cpu);
+}
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x09,
+ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x09, 0x2A, 0x86,
+ 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0E, 0x09, 0x2A, 0x86, 0x48, 0x86,
+ 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
+ 0x01, 0x01, 0x0C, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
+ 0x0D, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x08, 0x2A, 0x86,
+ 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22,
+ 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
+ 0x04, 0x01, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x01, 0x08,
+ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x08, 0x2A, 0x86, 0x48,
+ 0xCE, 0x3D, 0x04, 0x03, 0x03, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04,
+ 0x03, 0x04, 0x00, 0x1F, 0x03, 0xFC, 0x07, 0x7F, 0x0B, 0x5E, 0x0F, 0x1F,
+ 0x12, 0xFE, 0x16, 0xBF, 0x1A, 0x9F, 0x1E, 0x7E, 0x22, 0x3F, 0x26, 0x1E,
+ 0x29, 0xDF, 0x00, 0x1F, 0x03, 0xFD, 0x07, 0x9F, 0x0B, 0x7E, 0x0F, 0x3F,
+ 0x13, 0x1E, 0x16, 0xDF, 0x1A, 0xBF, 0x1E, 0x9E, 0x22, 0x5F, 0x26, 0x3E,
+ 0x29, 0xFF, 0x03, 0x55, 0x1D, 0x13
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00, 0x00, 0x01,
+ 0x01, 0x09, 0x00, 0x00, 0x01, 0x01, 0x0A, 0x00, 0x00, 0x1A, 0x1A, 0x00,
+ 0x00, 0x01, T0_INT1(BR_ERR_X509_BAD_BOOLEAN), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TAG_CLASS), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TAG_VALUE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TIME), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_EXTRA_ELEMENT), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INDEFINITE_LENGTH), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INNER_TRUNC), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_LIMIT_EXCEEDED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_CONSTRUCTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_PRIMITIVE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_OVERFLOW), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_PARTIAL_BYTE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNEXPECTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNSUPPORTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_KEYTYPE_EC), 0x00, 0x00, 0x01, T0_INT1(BR_KEYTYPE_RSA),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, copy_dn)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, decoded)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, isCA)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_x509_decoder_context, pkey_data)), 0x01,
+ T0_INT2(BR_X509_BUFSIZE_KEY), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, notafter_days)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, notafter_seconds)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, notbefore_days)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, notbefore_seconds)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, pad)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, signer_hash_id)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, signer_key_type)), 0x00, 0x00, 0x01,
+ 0x80, 0x45, 0x00, 0x00, 0x01, 0x80, 0x4E, 0x00, 0x00, 0x01, 0x80, 0x54,
+ 0x00, 0x00, 0x01, 0x81, 0x36, 0x00, 0x02, 0x03, 0x00, 0x03, 0x01, 0x1B,
+ 0x02, 0x01, 0x13, 0x26, 0x02, 0x00, 0x0F, 0x15, 0x00, 0x00, 0x05, 0x02,
+ 0x34, 0x1D, 0x00, 0x00, 0x06, 0x02, 0x35, 0x1D, 0x00, 0x00, 0x01, 0x10,
+ 0x4F, 0x00, 0x00, 0x11, 0x05, 0x02, 0x38, 0x1D, 0x4C, 0x00, 0x00, 0x11,
+ 0x05, 0x02, 0x38, 0x1D, 0x4D, 0x00, 0x00, 0x06, 0x02, 0x30, 0x1D, 0x00,
+ 0x00, 0x1B, 0x19, 0x01, 0x08, 0x0E, 0x26, 0x29, 0x19, 0x09, 0x00, 0x00,
+ 0x01, 0x30, 0x0A, 0x1B, 0x01, 0x00, 0x01, 0x09, 0x4B, 0x05, 0x02, 0x2F,
+ 0x1D, 0x00, 0x00, 0x20, 0x20, 0x00, 0x00, 0x01, 0x80, 0x5A, 0x00, 0x00,
+ 0x01, 0x80, 0x62, 0x00, 0x00, 0x01, 0x80, 0x6B, 0x00, 0x00, 0x01, 0x80,
+ 0x74, 0x00, 0x00, 0x01, 0x80, 0x7D, 0x00, 0x00, 0x01, 0x3D, 0x00, 0x00,
+ 0x20, 0x11, 0x06, 0x04, 0x2B, 0x6B, 0x7A, 0x71, 0x00, 0x04, 0x01, 0x00,
+ 0x3D, 0x25, 0x01, 0x00, 0x3C, 0x25, 0x01, 0x87, 0xFF, 0xFF, 0x7F, 0x6D,
+ 0x6D, 0x70, 0x1B, 0x01, 0x20, 0x11, 0x06, 0x11, 0x1A, 0x4C, 0x6B, 0x70,
+ 0x01, 0x02, 0x50, 0x6E, 0x01, 0x02, 0x12, 0x06, 0x02, 0x39, 0x1D, 0x51,
+ 0x70, 0x01, 0x02, 0x50, 0x6C, 0x6D, 0x7A, 0x6D, 0x7A, 0x6D, 0x65, 0x43,
+ 0x24, 0x42, 0x24, 0x65, 0x41, 0x24, 0x40, 0x24, 0x51, 0x01, 0x01, 0x3C,
+ 0x25, 0x6D, 0x7A, 0x01, 0x00, 0x3C, 0x25, 0x6D, 0x6D, 0x60, 0x05, 0x02,
+ 0x39, 0x1D, 0x74, 0x1C, 0x06, 0x1C, 0x7A, 0x61, 0x6D, 0x3F, 0x68, 0x03,
+ 0x00, 0x3F, 0x26, 0x02, 0x00, 0x09, 0x26, 0x02, 0x00, 0x0A, 0x68, 0x03,
+ 0x01, 0x51, 0x51, 0x02, 0x00, 0x02, 0x01, 0x18, 0x04, 0x1E, 0x5A, 0x1C,
+ 0x06, 0x18, 0x64, 0x03, 0x02, 0x51, 0x61, 0x1B, 0x03, 0x03, 0x1B, 0x3F,
+ 0x23, 0x0D, 0x06, 0x02, 0x33, 0x1D, 0x62, 0x02, 0x02, 0x02, 0x03, 0x17,
+ 0x04, 0x02, 0x39, 0x1D, 0x51, 0x01, 0x00, 0x3E, 0x25, 0x71, 0x01, 0x21,
+ 0x5B, 0x01, 0x22, 0x5B, 0x1B, 0x01, 0x23, 0x11, 0x06, 0x28, 0x1A, 0x4C,
+ 0x6B, 0x6D, 0x1B, 0x06, 0x1D, 0x6D, 0x60, 0x1A, 0x70, 0x1B, 0x01, 0x01,
+ 0x11, 0x06, 0x03, 0x63, 0x1A, 0x70, 0x01, 0x04, 0x50, 0x6B, 0x4A, 0x1C,
+ 0x06, 0x03, 0x5F, 0x04, 0x01, 0x7B, 0x51, 0x51, 0x04, 0x60, 0x51, 0x51,
+ 0x04, 0x08, 0x01, 0x7F, 0x11, 0x05, 0x02, 0x38, 0x1D, 0x1A, 0x51, 0x6D,
+ 0x60, 0x06, 0x80, 0x63, 0x75, 0x1C, 0x06, 0x06, 0x01, 0x02, 0x3B, 0x04,
+ 0x80, 0x57, 0x76, 0x1C, 0x06, 0x06, 0x01, 0x03, 0x3B, 0x04, 0x80, 0x4D,
+ 0x77, 0x1C, 0x06, 0x06, 0x01, 0x04, 0x3B, 0x04, 0x80, 0x43, 0x78, 0x1C,
+ 0x06, 0x05, 0x01, 0x05, 0x3B, 0x04, 0x3A, 0x79, 0x1C, 0x06, 0x05, 0x01,
+ 0x06, 0x3B, 0x04, 0x31, 0x55, 0x1C, 0x06, 0x05, 0x01, 0x02, 0x3A, 0x04,
+ 0x28, 0x56, 0x1C, 0x06, 0x05, 0x01, 0x03, 0x3A, 0x04, 0x1F, 0x57, 0x1C,
+ 0x06, 0x05, 0x01, 0x04, 0x3A, 0x04, 0x16, 0x58, 0x1C, 0x06, 0x05, 0x01,
+ 0x05, 0x3A, 0x04, 0x0D, 0x59, 0x1C, 0x06, 0x05, 0x01, 0x06, 0x3A, 0x04,
+ 0x04, 0x01, 0x00, 0x01, 0x00, 0x04, 0x04, 0x01, 0x00, 0x01, 0x00, 0x46,
+ 0x25, 0x45, 0x25, 0x7A, 0x61, 0x7A, 0x51, 0x1A, 0x01, 0x01, 0x3D, 0x25,
+ 0x73, 0x30, 0x1D, 0x00, 0x00, 0x01, 0x81, 0x06, 0x00, 0x01, 0x54, 0x0D,
+ 0x06, 0x02, 0x32, 0x1D, 0x1B, 0x03, 0x00, 0x0A, 0x02, 0x00, 0x00, 0x00,
+ 0x6D, 0x71, 0x1B, 0x01, 0x01, 0x11, 0x06, 0x08, 0x63, 0x01, 0x01, 0x15,
+ 0x3E, 0x25, 0x04, 0x01, 0x2B, 0x7A, 0x00, 0x00, 0x70, 0x01, 0x06, 0x50,
+ 0x6F, 0x00, 0x00, 0x70, 0x01, 0x03, 0x50, 0x6B, 0x72, 0x06, 0x02, 0x37,
+ 0x1D, 0x00, 0x00, 0x26, 0x1B, 0x06, 0x07, 0x21, 0x1B, 0x06, 0x01, 0x16,
+ 0x04, 0x76, 0x2B, 0x00, 0x00, 0x01, 0x01, 0x50, 0x6A, 0x01, 0x01, 0x10,
+ 0x06, 0x02, 0x2C, 0x1D, 0x72, 0x27, 0x00, 0x00, 0x60, 0x05, 0x02, 0x39,
+ 0x1D, 0x47, 0x1C, 0x06, 0x04, 0x01, 0x17, 0x04, 0x12, 0x48, 0x1C, 0x06,
+ 0x04, 0x01, 0x18, 0x04, 0x0A, 0x49, 0x1C, 0x06, 0x04, 0x01, 0x19, 0x04,
+ 0x02, 0x39, 0x1D, 0x00, 0x04, 0x70, 0x1B, 0x01, 0x17, 0x01, 0x18, 0x4B,
+ 0x05, 0x02, 0x2F, 0x1D, 0x01, 0x18, 0x11, 0x03, 0x00, 0x4D, 0x6B, 0x66,
+ 0x02, 0x00, 0x06, 0x0C, 0x01, 0x80, 0x64, 0x08, 0x03, 0x01, 0x66, 0x02,
+ 0x01, 0x09, 0x04, 0x0E, 0x1B, 0x01, 0x32, 0x0D, 0x06, 0x04, 0x01, 0x80,
+ 0x64, 0x09, 0x01, 0x8E, 0x6C, 0x09, 0x03, 0x01, 0x02, 0x01, 0x01, 0x82,
+ 0x6D, 0x08, 0x02, 0x01, 0x01, 0x03, 0x09, 0x01, 0x04, 0x0C, 0x09, 0x02,
+ 0x01, 0x01, 0x80, 0x63, 0x09, 0x01, 0x80, 0x64, 0x0C, 0x0A, 0x02, 0x01,
+ 0x01, 0x83, 0x0F, 0x09, 0x01, 0x83, 0x10, 0x0C, 0x09, 0x03, 0x03, 0x01,
+ 0x01, 0x01, 0x0C, 0x67, 0x2A, 0x01, 0x01, 0x0E, 0x02, 0x01, 0x01, 0x04,
+ 0x07, 0x28, 0x02, 0x01, 0x01, 0x80, 0x64, 0x07, 0x27, 0x02, 0x01, 0x01,
+ 0x83, 0x10, 0x07, 0x28, 0x1F, 0x15, 0x06, 0x03, 0x01, 0x18, 0x09, 0x5D,
+ 0x09, 0x52, 0x1B, 0x01, 0x05, 0x14, 0x02, 0x03, 0x09, 0x03, 0x03, 0x01,
+ 0x1F, 0x15, 0x01, 0x01, 0x26, 0x67, 0x02, 0x03, 0x09, 0x2A, 0x03, 0x03,
+ 0x01, 0x00, 0x01, 0x17, 0x67, 0x01, 0x9C, 0x10, 0x08, 0x03, 0x02, 0x01,
+ 0x00, 0x01, 0x3B, 0x67, 0x01, 0x3C, 0x08, 0x02, 0x02, 0x09, 0x03, 0x02,
+ 0x01, 0x00, 0x01, 0x3C, 0x67, 0x02, 0x02, 0x09, 0x03, 0x02, 0x72, 0x1B,
+ 0x01, 0x2E, 0x11, 0x06, 0x0D, 0x1A, 0x72, 0x1B, 0x01, 0x30, 0x01, 0x39,
+ 0x4B, 0x06, 0x03, 0x1A, 0x04, 0x74, 0x01, 0x80, 0x5A, 0x10, 0x06, 0x02,
+ 0x2F, 0x1D, 0x51, 0x02, 0x03, 0x02, 0x02, 0x00, 0x01, 0x72, 0x53, 0x01,
+ 0x0A, 0x08, 0x03, 0x00, 0x72, 0x53, 0x02, 0x00, 0x09, 0x00, 0x02, 0x03,
+ 0x00, 0x03, 0x01, 0x66, 0x1B, 0x02, 0x01, 0x02, 0x00, 0x4B, 0x05, 0x02,
+ 0x2F, 0x1D, 0x00, 0x00, 0x23, 0x70, 0x01, 0x02, 0x50, 0x0B, 0x69, 0x00,
+ 0x03, 0x1B, 0x03, 0x00, 0x03, 0x01, 0x03, 0x02, 0x6B, 0x72, 0x1B, 0x01,
+ 0x81, 0x00, 0x13, 0x06, 0x02, 0x36, 0x1D, 0x1B, 0x01, 0x00, 0x11, 0x06,
+ 0x0B, 0x1A, 0x1B, 0x05, 0x04, 0x1A, 0x01, 0x00, 0x00, 0x72, 0x04, 0x6F,
+ 0x02, 0x01, 0x1B, 0x05, 0x02, 0x33, 0x1D, 0x2A, 0x03, 0x01, 0x02, 0x02,
+ 0x25, 0x02, 0x02, 0x29, 0x03, 0x02, 0x1B, 0x06, 0x03, 0x72, 0x04, 0x68,
+ 0x1A, 0x02, 0x00, 0x02, 0x01, 0x0A, 0x00, 0x01, 0x72, 0x1B, 0x01, 0x81,
+ 0x00, 0x0D, 0x06, 0x01, 0x00, 0x01, 0x81, 0x00, 0x0A, 0x1B, 0x05, 0x02,
+ 0x31, 0x1D, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x12, 0x06,
+ 0x19, 0x02, 0x00, 0x2A, 0x03, 0x00, 0x1B, 0x01, 0x83, 0xFF, 0xFF, 0x7F,
+ 0x12, 0x06, 0x02, 0x32, 0x1D, 0x01, 0x08, 0x0E, 0x26, 0x72, 0x23, 0x09,
+ 0x04, 0x60, 0x00, 0x00, 0x6A, 0x5E, 0x00, 0x00, 0x6B, 0x7A, 0x00, 0x00,
+ 0x70, 0x4E, 0x6B, 0x00, 0x01, 0x6B, 0x1B, 0x05, 0x02, 0x36, 0x1D, 0x72,
+ 0x1B, 0x01, 0x81, 0x00, 0x13, 0x06, 0x02, 0x36, 0x1D, 0x03, 0x00, 0x1B,
+ 0x06, 0x16, 0x72, 0x02, 0x00, 0x1B, 0x01, 0x87, 0xFF, 0xFF, 0x7F, 0x13,
+ 0x06, 0x02, 0x36, 0x1D, 0x01, 0x08, 0x0E, 0x09, 0x03, 0x00, 0x04, 0x67,
+ 0x1A, 0x02, 0x00, 0x00, 0x00, 0x6B, 0x1B, 0x01, 0x81, 0x7F, 0x12, 0x06,
+ 0x08, 0x7A, 0x01, 0x00, 0x44, 0x25, 0x01, 0x00, 0x00, 0x1B, 0x44, 0x25,
+ 0x44, 0x29, 0x62, 0x01, 0x7F, 0x00, 0x01, 0x72, 0x03, 0x00, 0x02, 0x00,
+ 0x01, 0x05, 0x14, 0x01, 0x01, 0x15, 0x1E, 0x02, 0x00, 0x01, 0x06, 0x14,
+ 0x1B, 0x01, 0x01, 0x15, 0x06, 0x02, 0x2D, 0x1D, 0x01, 0x04, 0x0E, 0x02,
+ 0x00, 0x01, 0x1F, 0x15, 0x1B, 0x01, 0x1F, 0x11, 0x06, 0x02, 0x2E, 0x1D,
+ 0x09, 0x00, 0x00, 0x1B, 0x05, 0x05, 0x01, 0x00, 0x01, 0x7F, 0x00, 0x70,
+ 0x00, 0x00, 0x1B, 0x05, 0x02, 0x32, 0x1D, 0x2A, 0x73, 0x00, 0x00, 0x22,
+ 0x1B, 0x01, 0x00, 0x13, 0x06, 0x01, 0x00, 0x1A, 0x16, 0x04, 0x74, 0x00,
+ 0x01, 0x01, 0x00, 0x00, 0x01, 0x0B, 0x00, 0x00, 0x01, 0x15, 0x00, 0x00,
+ 0x01, 0x1F, 0x00, 0x00, 0x01, 0x29, 0x00, 0x00, 0x01, 0x33, 0x00, 0x00,
+ 0x7B, 0x1A, 0x00, 0x00, 0x1B, 0x06, 0x07, 0x7C, 0x1B, 0x06, 0x01, 0x16,
+ 0x04, 0x76, 0x00, 0x00, 0x01, 0x00, 0x20, 0x21, 0x0B, 0x2B, 0x00
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 15,
+ 20,
+ 24,
+ 28,
+ 32,
+ 36,
+ 40,
+ 44,
+ 48,
+ 52,
+ 56,
+ 60,
+ 64,
+ 68,
+ 72,
+ 76,
+ 80,
+ 84,
+ 88,
+ 93,
+ 98,
+ 103,
+ 111,
+ 116,
+ 121,
+ 126,
+ 131,
+ 136,
+ 141,
+ 146,
+ 151,
+ 156,
+ 161,
+ 166,
+ 181,
+ 187,
+ 193,
+ 198,
+ 206,
+ 214,
+ 220,
+ 231,
+ 246,
+ 250,
+ 255,
+ 260,
+ 265,
+ 270,
+ 275,
+ 279,
+ 289,
+ 620,
+ 625,
+ 639,
+ 659,
+ 666,
+ 678,
+ 692,
+ 707,
+ 740,
+ 960,
+ 974,
+ 991,
+ 1000,
+ 1067,
+ 1123,
+ 1127,
+ 1131,
+ 1136,
+ 1184,
+ 1210,
+ 1254,
+ 1265,
+ 1274,
+ 1287,
+ 1291,
+ 1295,
+ 1299,
+ 1303,
+ 1307,
+ 1311,
+ 1315,
+ 1327
+};
+
+#define T0_INTERPRETED 39
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_x509_decoder_init_main, 92)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_x509_decoder_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* %25 */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSHi(a % b);
+
+ }
+ break;
+ case 8: {
+ /* * */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a * b);
+
+ }
+ break;
+ case 9: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 10: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 11: {
+ /* -rot */
+ T0_NROT();
+ }
+ break;
+ case 12: {
+ /* / */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSHi(a / b);
+
+ }
+ break;
+ case 13: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 14: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 15: {
+ /* <= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a <= b));
+
+ }
+ break;
+ case 16: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 17: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 18: {
+ /* > */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a > b));
+
+ }
+ break;
+ case 19: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 20: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 21: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 22: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 23: {
+ /* copy-ec-pkey */
+
+ size_t qlen = T0_POP();
+ uint32_t curve = T0_POP();
+ CTX->pkey.key_type = BR_KEYTYPE_EC;
+ CTX->pkey.key.ec.curve = curve;
+ CTX->pkey.key.ec.q = CTX->pkey_data;
+ CTX->pkey.key.ec.qlen = qlen;
+
+ }
+ break;
+ case 24: {
+ /* copy-rsa-pkey */
+
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ CTX->pkey.key_type = BR_KEYTYPE_RSA;
+ CTX->pkey.key.rsa.n = CTX->pkey_data;
+ CTX->pkey.key.rsa.nlen = nlen;
+ CTX->pkey.key.rsa.e = CTX->pkey_data + nlen;
+ CTX->pkey.key.rsa.elen = elen;
+
+ }
+ break;
+ case 25: {
+ /* data-get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(t0_datablock[addr]);
+
+ }
+ break;
+ case 26: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 27: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 28: {
+ /* eqOID */
+
+ const unsigned char *a2 = &t0_datablock[T0_POP()];
+ const unsigned char *a1 = &CTX->pad[0];
+ size_t len = a1[0];
+ int x;
+ if (len == a2[0]) {
+ x = -(memcmp(a1 + 1, a2 + 1, len) == 0);
+ } else {
+ x = 0;
+ }
+ T0_PUSH((uint32_t)x);
+
+ }
+ break;
+ case 29: {
+ /* fail */
+
+ CTX->err = T0_POPi();
+ T0_CO();
+
+ }
+ break;
+ case 30: {
+ /* neg */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(-a);
+
+ }
+ break;
+ case 31: {
+ /* or */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a | b);
+
+ }
+ break;
+ case 32: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 33: {
+ /* read-blob-inner */
+
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ if (CTX->copy_dn && CTX->append_dn) {
+ CTX->append_dn(CTX->append_dn_ctx, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+
+ }
+ break;
+ case 34: {
+ /* read8-low */
+
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ unsigned char x = *CTX->hbuf ++;
+ if (CTX->copy_dn && CTX->append_dn) {
+ CTX->append_dn(CTX->append_dn_ctx, &x, 1);
+ }
+ CTX->hlen --;
+ T0_PUSH(x);
+ }
+
+ }
+ break;
+ case 35: {
+ /* rot */
+ T0_ROT();
+ }
+ break;
+ case 36: {
+ /* set32 */
+
+ uint32_t addr = T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)CTX + addr) = T0_POP();
+
+ }
+ break;
+ case 37: {
+ /* set8 */
+
+ uint32_t addr = T0_POP();
+ *((unsigned char *)CTX + addr) = (unsigned char)T0_POP();
+
+ }
+ break;
+ case 38: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
diff --git a/test/monniaux/BearSSL/src/x509/x509_decoder.t0 b/test/monniaux/BearSSL/src/x509/x509_decoder.t0
new file mode 100644
index 00000000..0bf276fb
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_decoder.t0
@@ -0,0 +1,321 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+preamble {
+
+#include "inner.h"
+
+#define CTX ((br_x509_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_x509_decoder_context, cpu)))
+#define CONTEXT_NAME br_x509_decoder_context
+
+/* see bearssl_x509.h */
+void
+br_x509_decoder_init(br_x509_decoder_context *ctx,
+ void (*append_dn)(void *ctx, const void *buf, size_t len),
+ void *append_dn_ctx)
+{
+ memset(ctx, 0, sizeof *ctx);
+ /* obsolete
+ ctx->err = 0;
+ ctx->hbuf = NULL;
+ ctx->hlen = 0;
+ */
+ ctx->append_dn = append_dn;
+ ctx->append_dn_ctx = append_dn_ctx;
+ ctx->cpu.dp = &ctx->dp_stack[0];
+ ctx->cpu.rp = &ctx->rp_stack[0];
+ br_x509_decoder_init_main(&ctx->cpu);
+ br_x509_decoder_run(&ctx->cpu);
+}
+
+/* see bearssl_x509.h */
+void
+br_x509_decoder_push(br_x509_decoder_context *ctx,
+ const void *data, size_t len)
+{
+ ctx->hbuf = data;
+ ctx->hlen = len;
+ br_x509_decoder_run(&ctx->cpu);
+}
+
+}
+
+addr: decoded
+addr: notbefore_days
+addr: notbefore_seconds
+addr: notafter_days
+addr: notafter_seconds
+addr: isCA
+addr: copy_dn
+addr: signer_key_type
+addr: signer_hash_id
+
+cc: read8-low ( -- x ) {
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ unsigned char x = *CTX->hbuf ++;
+ if (CTX->copy_dn && CTX->append_dn) {
+ CTX->append_dn(CTX->append_dn_ctx, &x, 1);
+ }
+ CTX->hlen --;
+ T0_PUSH(x);
+ }
+}
+
+cc: read-blob-inner ( addr len -- addr len ) {
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ if (CTX->copy_dn && CTX->append_dn) {
+ CTX->append_dn(CTX->append_dn_ctx, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+}
+
+\ Get the address and length for the pkey_data buffer.
+: addr-len-pkey_data ( -- addr len )
+ CX 0 8191 { offsetof(br_x509_decoder_context, pkey_data) }
+ CX 0 8191 { BR_X509_BUFSIZE_KEY } ;
+
+\ Copy the public key (RSA) to the permanent buffer.
+cc: copy-rsa-pkey ( nlen elen -- ) {
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ CTX->pkey.key_type = BR_KEYTYPE_RSA;
+ CTX->pkey.key.rsa.n = CTX->pkey_data;
+ CTX->pkey.key.rsa.nlen = nlen;
+ CTX->pkey.key.rsa.e = CTX->pkey_data + nlen;
+ CTX->pkey.key.rsa.elen = elen;
+}
+
+\ Copy the public key (EC) to the permanent buffer.
+cc: copy-ec-pkey ( curve qlen -- ) {
+ size_t qlen = T0_POP();
+ uint32_t curve = T0_POP();
+ CTX->pkey.key_type = BR_KEYTYPE_EC;
+ CTX->pkey.key.ec.curve = curve;
+ CTX->pkey.key.ec.q = CTX->pkey_data;
+ CTX->pkey.key.ec.qlen = qlen;
+}
+
+\ Extensions with specific processing.
+OID: basicConstraints 2.5.29.19
+
+\ Process a Basic Constraints extension. We want the "CA" flag only.
+: process-basicConstraints ( lim -- lim )
+ read-sequence-open
+ read-tag-or-end dup 0x01 = if
+ read-boolean 1 and addr-isCA set8
+ else
+ 2drop
+ then
+ skip-close-elt
+ ;
+
+\ Decode a certificate.
+: main ( -- ! )
+
+ \ Initialise state flags.
+ 0 addr-decoded set8
+ 0 addr-copy_dn set8
+
+ \ An arbitrary limit for the total certificate size.
+ 0xFFFFFF
+
+ \ Open the outer SEQUENCE.
+ read-sequence-open
+
+ \ TBS
+ read-sequence-open
+
+ \ First element may be an explicit version. We accept only
+ \ versions 0 to 2 (certificates v1 to v3).
+ read-tag dup 0x20 = if
+ drop check-constructed read-length-open-elt
+ read-tag
+ 0x02 check-tag-primitive
+ read-small-int-value
+ 2 > if ERR_X509_UNSUPPORTED fail then
+ close-elt
+ read-tag
+ then
+
+ \ Serial number. We just check that the tag is correct.
+ 0x02 check-tag-primitive read-length-skip
+
+ \ Signature algorithm.
+ read-sequence-open skip-close-elt
+
+ \ Issuer name.
+ read-sequence-open skip-close-elt
+
+ \ Validity dates.
+ read-sequence-open
+ read-date addr-notbefore_seconds set32 addr-notbefore_days set32
+ read-date addr-notafter_seconds set32 addr-notafter_days set32
+ close-elt
+
+ \ Subject name.
+ 1 addr-copy_dn set8
+ read-sequence-open skip-close-elt
+ 0 addr-copy_dn set8
+
+ \ Public Key.
+ read-sequence-open
+ \ Algorithm Identifier. Right now we are only interested in the
+ \ OID, since we only support RSA keys.
+ \ TODO: support EC keys
+ read-sequence-open
+ read-OID ifnot ERR_X509_UNSUPPORTED fail then
+ choice
+ \ RSA public key.
+ rsaEncryption eqOID uf
+ skip-close-elt
+ \ Public key itself: the BIT STRING contains bytes
+ \ (no partial byte) and these bytes encode the
+ \ actual value.
+ read-bits-open
+ \ RSA public key is a SEQUENCE of two
+ \ INTEGER. We get both INTEGER values into
+ \ the pkey_data[] buffer, if they fit.
+ read-sequence-open
+ addr-len-pkey_data
+ read-integer { nlen }
+ addr-len-pkey_data swap nlen + swap nlen -
+ read-integer { elen }
+ close-elt
+ close-elt
+ nlen elen copy-rsa-pkey
+ enduf
+
+ \ EC public key.
+ id-ecPublicKey eqOID uf
+ \ We support only named curves, for which the
+ \ "parameters" field in the AlgorithmIdentifier
+ \ field should be an OID.
+ read-curve-ID { curve }
+ close-elt
+ read-bits-open
+ dup { qlen }
+ dup addr-len-pkey_data rot < if
+ ERR_X509_LIMIT_EXCEEDED fail
+ then
+ read-blob
+ curve qlen copy-ec-pkey
+ enduf
+ ERR_X509_UNSUPPORTED fail
+ endchoice
+ close-elt
+
+ \ This flag will be set to true if the Basic Constraints extension
+ \ is encountered.
+ 0 addr-isCA set8
+
+ \ Skip issuerUniqueID and subjectUniqueID, and process extensions
+ \ if present. Extensions are an explicit context tag of value 3
+ \ around a SEQUENCE OF extensions. Each extension is a SEQUENCE
+ \ with an OID, an optional boolean, and a value; the value is
+ \ an OCTET STRING.
+ read-tag-or-end
+ 0x21 iftag-skip
+ 0x22 iftag-skip
+ dup 0x23 = if
+ drop
+ check-constructed read-length-open-elt
+ read-sequence-open
+ begin dup while
+ read-sequence-open
+ read-OID drop
+ read-tag dup 0x01 = if
+ read-boolean drop
+ read-tag
+ then
+ 0x04 check-tag-primitive read-length-open-elt
+ choice
+ \ Extensions with specific processing.
+ basicConstraints eqOID uf
+ process-basicConstraints
+ enduf
+ skip-remaining
+ endchoice
+ close-elt
+ close-elt
+ repeat
+ close-elt
+ close-elt
+ else
+ -1 = ifnot ERR_X509_UNEXPECTED fail then
+ drop
+ then
+
+ close-elt
+
+ \ signature algorithm
+ read-sequence-open
+ read-OID if
+ choice
+ sha1WithRSAEncryption eqOID uf 2 KEYTYPE_RSA enduf
+ sha224WithRSAEncryption eqOID uf 3 KEYTYPE_RSA enduf
+ sha256WithRSAEncryption eqOID uf 4 KEYTYPE_RSA enduf
+ sha384WithRSAEncryption eqOID uf 5 KEYTYPE_RSA enduf
+ sha512WithRSAEncryption eqOID uf 6 KEYTYPE_RSA enduf
+
+ ecdsa-with-SHA1 eqOID uf 2 KEYTYPE_EC enduf
+ ecdsa-with-SHA224 eqOID uf 3 KEYTYPE_EC enduf
+ ecdsa-with-SHA256 eqOID uf 4 KEYTYPE_EC enduf
+ ecdsa-with-SHA384 eqOID uf 5 KEYTYPE_EC enduf
+ ecdsa-with-SHA512 eqOID uf 6 KEYTYPE_EC enduf
+
+ 0 0
+ endchoice
+ else
+ 0 0
+ then
+ addr-signer_key_type set8
+ addr-signer_hash_id set8
+ skip-close-elt
+ \ read-sequence-open skip-close-elt
+
+ \ signature value
+ read-bits-open skip-close-elt
+
+ \ Close the outer SEQUENCE.
+ close-elt
+ drop
+
+ \ Mark the decoding as successful.
+ 1 addr-decoded set8
+
+ \ Read one byte, then fail: if the read succeeds, then there is
+ \ some trailing byte.
+ read8-nc ERR_X509_EXTRA_ELEMENT fail
+ ;
diff --git a/test/monniaux/BearSSL/src/x509/x509_knownkey.c b/test/monniaux/BearSSL/src/x509/x509_knownkey.c
new file mode 100644
index 00000000..7674f3fd
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_knownkey.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_x509.h */
+void
+br_x509_knownkey_init_rsa(br_x509_knownkey_context *ctx,
+ const br_rsa_public_key *pk, unsigned usages)
+{
+ ctx->vtable = &br_x509_knownkey_vtable;
+ ctx->pkey.key_type = BR_KEYTYPE_RSA;
+ ctx->pkey.key.rsa = *pk;
+ ctx->usages = usages;
+}
+
+/* see bearssl_x509.h */
+void
+br_x509_knownkey_init_ec(br_x509_knownkey_context *ctx,
+ const br_ec_public_key *pk, unsigned usages)
+{
+ ctx->vtable = &br_x509_knownkey_vtable;
+ ctx->pkey.key_type = BR_KEYTYPE_EC;
+ ctx->pkey.key.ec = *pk;
+ ctx->usages = usages;
+}
+
+static void
+kk_start_chain(const br_x509_class **ctx, const char *server_name)
+{
+ (void)ctx;
+ (void)server_name;
+}
+
+static void
+kk_start_cert(const br_x509_class **ctx, uint32_t length)
+{
+ (void)ctx;
+ (void)length;
+}
+
+static void
+kk_append(const br_x509_class **ctx, const unsigned char *buf, size_t len)
+{
+ (void)ctx;
+ (void)buf;
+ (void)len;
+}
+
+static void
+kk_end_cert(const br_x509_class **ctx)
+{
+ (void)ctx;
+}
+
+static unsigned
+kk_end_chain(const br_x509_class **ctx)
+{
+ (void)ctx;
+ return 0;
+}
+
+static const br_x509_pkey *
+kk_get_pkey(const br_x509_class *const *ctx, unsigned *usages)
+{
+ const br_x509_knownkey_context *xc;
+
+ xc = (const br_x509_knownkey_context *)ctx;
+ if (usages != NULL) {
+ *usages = xc->usages;
+ }
+ return &xc->pkey;
+}
+
+/* see bearssl_x509.h */
+const br_x509_class br_x509_knownkey_vtable = {
+ sizeof(br_x509_knownkey_context),
+ kk_start_chain,
+ kk_start_cert,
+ kk_append,
+ kk_end_cert,
+ kk_end_chain,
+ kk_get_pkey
+};
diff --git a/test/monniaux/BearSSL/src/x509/x509_minimal.c b/test/monniaux/BearSSL/src/x509/x509_minimal.c
new file mode 100644
index 00000000..3b876ef8
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_minimal.c
@@ -0,0 +1,1713 @@
+/* Automatically generated code; do not modify directly. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint32_t *dp;
+ uint32_t *rp;
+ const unsigned char *ip;
+} t0_context;
+
+static uint32_t
+t0_parse7E_unsigned(const unsigned char **p)
+{
+ uint32_t x;
+
+ x = 0;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ return x;
+ }
+ }
+}
+
+static int32_t
+t0_parse7E_signed(const unsigned char **p)
+{
+ int neg;
+ uint32_t x;
+
+ neg = ((**p) >> 6) & 1;
+ x = (uint32_t)-neg;
+ for (;;) {
+ unsigned y;
+
+ y = *(*p) ++;
+ x = (x << 7) | (uint32_t)(y & 0x7F);
+ if (y < 0x80) {
+ if (neg) {
+ return -(int32_t)~x - 1;
+ } else {
+ return (int32_t)x;
+ }
+ }
+ }
+}
+
+#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)
+#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)
+#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)
+#define T0_INT1(x) T0_FBYTE(x, 0)
+#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)
+
+/* static const unsigned char t0_datablock[]; */
+
+
+void br_x509_minimal_init_main(void *t0ctx);
+
+void br_x509_minimal_run(void *t0ctx);
+
+
+
+#include "inner.h"
+
+
+
+
+
+#include "inner.h"
+
+/*
+ * Implementation Notes
+ * --------------------
+ *
+ * The C code pushes the data by chunks; all decoding is done in the
+ * T0 code. The cert_length value is set to the certificate length when
+ * a new certificate is started; the T0 code picks it up as outer limit,
+ * and decoding functions use it to ensure that no attempt is made at
+ * reading past it. The T0 code also checks that once the certificate is
+ * decoded, there are no trailing bytes.
+ *
+ * The T0 code sets cert_length to 0 when the certificate is fully
+ * decoded.
+ *
+ * The C code must still perform two checks:
+ *
+ * -- If the certificate length is 0, then the T0 code will not be
+ * invoked at all. This invalid condition must thus be reported by the
+ * C code.
+ *
+ * -- When reaching the end of certificate, the C code must verify that
+ * the certificate length has been set to 0, thereby signaling that
+ * the T0 code properly decoded a certificate.
+ *
+ * Processing of a chain works in the following way:
+ *
+ * -- The error flag is set to a non-zero value when validation is
+ * finished. The value is either BR_ERR_X509_OK (validation is
+ * successful) or another non-zero error code. When a non-zero error
+ * code is obtained, the remaining bytes in the current certificate and
+ * the subsequent certificates (if any) are completely ignored.
+ *
+ * -- Each certificate is decoded in due course, with the following
+ * "interesting points":
+ *
+ * -- Start of the TBS: the multihash engine is reset and activated.
+ *
+ * -- Start of the issuer DN: the secondary hash engine is started,
+ * to process the encoded issuer DN.
+ *
+ * -- End of the issuer DN: the secondary hash engine is stopped. The
+ * resulting hash value is computed and then copied into the
+ * next_dn_hash[] buffer.
+ *
+ * -- Start of the subject DN: the secondary hash engine is started,
+ * to process the encoded subject DN.
+ *
+ * -- For the EE certificate only: the Common Name, if any, is matched
+ * against the expected server name.
+ *
+ * -- End of the subject DN: the secondary hash engine is stopped. The
+ * resulting hash value is computed into the pad. It is then processed:
+ *
+ * -- If this is the EE certificate, then the hash is ignored
+ * (except for direct trust processing, see later; the hash is
+ * simply left in current_dn_hash[]).
+ *
+ * -- Otherwise, the hashed subject DN is compared with the saved
+ * hash value (in saved_dn_hash[]). They must match.
+ *
+ * Either way, the next_dn_hash[] value is then copied into the
+ * saved_dn_hash[] value. Thus, at that point, saved_dn_hash[]
+ * contains the hash of the issuer DN for the current certificate,
+ * and current_dn_hash[] contains the hash of the subject DN for the
+ * current certificate.
+ *
+ * -- Public key: it is decoded into the cert_pkey[] buffer. Unknown
+ * key types are reported at that point.
+ *
+ * -- If this is the EE certificate, then the key type is compared
+ * with the expected key type (initialization parameter). The public
+ * key data is copied to ee_pkey_data[]. The key and hashed subject
+ * DN are also compared with the "direct trust" keys; if the key
+ * and DN are matched, then validation ends with a success.
+ *
+ * -- Otherwise, the saved signature (cert_sig[]) is verified
+ * against the saved TBS hash (tbs_hash[]) and that freshly
+ * decoded public key. Failure here ends validation with an error.
+ *
+ * -- Extensions: extension values are processed in due order.
+ *
+ * -- Basic Constraints: for all certificates except EE, must be
+ * present, indicate a CA, and have a path legnth compatible with
+ * the chain length so far.
+ *
+ * -- Key Usage: for the EE, if present, must allow signatures
+ * or encryption/key exchange, as required for the cipher suite.
+ * For non-EE, if present, must have the "certificate sign" bit.
+ *
+ * -- Subject Alt Name: for the EE, dNSName names are matched
+ * against the server name. Ignored for non-EE.
+ *
+ * -- Authority Key Identifier, Subject Key Identifier, Issuer
+ * Alt Name, Subject Directory Attributes, CRL Distribution Points
+ * Freshest CRL, Authority Info Access and Subject Info Access
+ * extensions are always ignored: they either contain only
+ * informative data, or they relate to revocation processing, which
+ * we explicitly do not support.
+ *
+ * -- All other extensions are ignored if non-critical. If a
+ * critical extension other than the ones above is encountered,
+ * then a failure is reported.
+ *
+ * -- End of the TBS: the multihash engine is stopped.
+ *
+ * -- Signature algorithm: the signature algorithm on the
+ * certificate is decoded. A failure is reported if that algorithm
+ * is unknown. The hashed TBS corresponding to the signature hash
+ * function is computed and stored in tbs_hash[] (if not supported,
+ * then a failure is reported). The hash OID and length are stored
+ * in cert_sig_hash_oid and cert_sig_hash_len.
+ *
+ * -- Signature value: the signature value is copied into the
+ * cert_sig[] array.
+ *
+ * -- Certificate end: the hashed issuer DN (saved_dn_hash[]) is
+ * looked up in the trust store (CA trust anchors only); for all
+ * that match, the signature (cert_sig[]) is verified against the
+ * anchor public key (hashed TBS is in tbs_hash[]). If one of these
+ * signatures is valid, then validation ends with a success.
+ *
+ * -- If the chain end is reached without obtaining a validation success,
+ * then validation is reported as failed.
+ */
+
+#if BR_USE_UNIX_TIME
+#include <time.h>
+#endif
+
+#if BR_USE_WIN32_TIME
+#include <windows.h>
+#endif
+
+/*
+ * The T0 compiler will produce these prototypes declarations in the
+ * header.
+ *
+void br_x509_minimal_init_main(void *ctx);
+void br_x509_minimal_run(void *ctx);
+ */
+
+/* see bearssl_x509.h */
+void
+br_x509_minimal_init(br_x509_minimal_context *ctx,
+ const br_hash_class *dn_hash_impl,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->vtable = &br_x509_minimal_vtable;
+ ctx->dn_hash_impl = dn_hash_impl;
+ ctx->trust_anchors = trust_anchors;
+ ctx->trust_anchors_num = trust_anchors_num;
+}
+
+static void
+xm_start_chain(const br_x509_class **ctx, const char *server_name)
+{
+ br_x509_minimal_context *cc;
+ size_t u;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ for (u = 0; u < cc->num_name_elts; u ++) {
+ cc->name_elts[u].status = 0;
+ cc->name_elts[u].buf[0] = 0;
+ }
+ memset(&cc->pkey, 0, sizeof cc->pkey);
+ cc->num_certs = 0;
+ cc->err = 0;
+ cc->cpu.dp = cc->dp_stack;
+ cc->cpu.rp = cc->rp_stack;
+ br_x509_minimal_init_main(&cc->cpu);
+ if (server_name == NULL || *server_name == 0) {
+ cc->server_name = NULL;
+ } else {
+ cc->server_name = server_name;
+ }
+}
+
+static void
+xm_start_cert(const br_x509_class **ctx, uint32_t length)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err != 0) {
+ return;
+ }
+ if (length == 0) {
+ cc->err = BR_ERR_X509_TRUNCATED;
+ return;
+ }
+ cc->cert_length = length;
+}
+
+static void
+xm_append(const br_x509_class **ctx, const unsigned char *buf, size_t len)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err != 0) {
+ return;
+ }
+ cc->hbuf = buf;
+ cc->hlen = len;
+ br_x509_minimal_run(&cc->cpu);
+}
+
+static void
+xm_end_cert(const br_x509_class **ctx)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == 0 && cc->cert_length != 0) {
+ cc->err = BR_ERR_X509_TRUNCATED;
+ }
+ cc->num_certs ++;
+}
+
+static unsigned
+xm_end_chain(const br_x509_class **ctx)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == 0) {
+ if (cc->num_certs == 0) {
+ cc->err = BR_ERR_X509_EMPTY_CHAIN;
+ } else {
+ cc->err = BR_ERR_X509_NOT_TRUSTED;
+ }
+ } else if (cc->err == BR_ERR_X509_OK) {
+ return 0;
+ }
+ return (unsigned)cc->err;
+}
+
+static const br_x509_pkey *
+xm_get_pkey(const br_x509_class *const *ctx, unsigned *usages)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == BR_ERR_X509_OK
+ || cc->err == BR_ERR_X509_NOT_TRUSTED)
+ {
+ if (usages != NULL) {
+ *usages = cc->key_usages;
+ }
+ return &((br_x509_minimal_context *)(void *)ctx)->pkey;
+ } else {
+ return NULL;
+ }
+}
+
+/* see bearssl_x509.h */
+const br_x509_class br_x509_minimal_vtable = {
+ sizeof(br_x509_minimal_context),
+ xm_start_chain,
+ xm_start_cert,
+ xm_append,
+ xm_end_cert,
+ xm_end_chain,
+ xm_get_pkey
+};
+
+#define CTX ((br_x509_minimal_context *)(void *)((unsigned char *)t0ctx - offsetof(br_x509_minimal_context, cpu)))
+#define CONTEXT_NAME br_x509_minimal_context
+
+#define DNHASH_LEN ((CTX->dn_hash_impl->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK)
+
+/*
+ * Hash a DN (from a trust anchor) into the provided buffer. This uses the
+ * DN hash implementation and context structure from the X.509 engine
+ * context.
+ */
+static void
+hash_dn(br_x509_minimal_context *ctx, const void *dn, size_t len,
+ unsigned char *out)
+{
+ ctx->dn_hash_impl->init(&ctx->dn_hash.vtable);
+ ctx->dn_hash_impl->update(&ctx->dn_hash.vtable, dn, len);
+ ctx->dn_hash_impl->out(&ctx->dn_hash.vtable, out);
+}
+
+/*
+ * Compare two big integers for equality. The integers use unsigned big-endian
+ * encoding; extra leading bytes (of value 0) are allowed.
+ */
+static int
+eqbigint(const unsigned char *b1, size_t len1,
+ const unsigned char *b2, size_t len2)
+{
+ while (len1 > 0 && *b1 == 0) {
+ b1 ++;
+ len1 --;
+ }
+ while (len2 > 0 && *b2 == 0) {
+ b2 ++;
+ len2 --;
+ }
+ if (len1 != len2) {
+ return 0;
+ }
+ return memcmp(b1, b2, len1) == 0;
+}
+
+/*
+ * Compare two strings for equality, in a case-insensitive way. This
+ * function handles casing only for ASCII letters.
+ */
+static int
+eqnocase(const void *s1, const void *s2, size_t len)
+{
+ const unsigned char *buf1, *buf2;
+
+ buf1 = s1;
+ buf2 = s2;
+ while (len -- > 0) {
+ int x1, x2;
+
+ x1 = *buf1 ++;
+ x2 = *buf2 ++;
+ if (x1 >= 'A' && x1 <= 'Z') {
+ x1 += 'a' - 'A';
+ }
+ if (x2 >= 'A' && x2 <= 'Z') {
+ x2 += 'a' - 'A';
+ }
+ if (x1 != x2) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static int verify_signature(br_x509_minimal_context *ctx,
+ const br_x509_pkey *pk);
+
+
+
+static const unsigned char t0_datablock[] = {
+ 0x00, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x09,
+ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x09, 0x2A, 0x86,
+ 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0E, 0x09, 0x2A, 0x86, 0x48, 0x86,
+ 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
+ 0x01, 0x01, 0x0C, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
+ 0x0D, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x09, 0x60, 0x86, 0x48, 0x01,
+ 0x65, 0x03, 0x04, 0x02, 0x04, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
+ 0x04, 0x02, 0x01, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
+ 0x02, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x07,
+ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x08, 0x2A, 0x86, 0x48, 0xCE,
+ 0x3D, 0x03, 0x01, 0x07, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x05, 0x2B,
+ 0x81, 0x04, 0x00, 0x23, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x01,
+ 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x01, 0x08, 0x2A, 0x86,
+ 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
+ 0x04, 0x03, 0x03, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x04,
+ 0x03, 0x55, 0x04, 0x03, 0x00, 0x1F, 0x03, 0xFC, 0x07, 0x7F, 0x0B, 0x5E,
+ 0x0F, 0x1F, 0x12, 0xFE, 0x16, 0xBF, 0x1A, 0x9F, 0x1E, 0x7E, 0x22, 0x3F,
+ 0x26, 0x1E, 0x29, 0xDF, 0x00, 0x1F, 0x03, 0xFD, 0x07, 0x9F, 0x0B, 0x7E,
+ 0x0F, 0x3F, 0x13, 0x1E, 0x16, 0xDF, 0x1A, 0xBF, 0x1E, 0x9E, 0x22, 0x5F,
+ 0x26, 0x3E, 0x29, 0xFF, 0x03, 0x55, 0x1D, 0x13, 0x03, 0x55, 0x1D, 0x0F,
+ 0x03, 0x55, 0x1D, 0x11, 0x03, 0x55, 0x1D, 0x20, 0x08, 0x2B, 0x06, 0x01,
+ 0x05, 0x05, 0x07, 0x02, 0x01, 0x03, 0x55, 0x1D, 0x23, 0x03, 0x55, 0x1D,
+ 0x0E, 0x03, 0x55, 0x1D, 0x12, 0x03, 0x55, 0x1D, 0x09, 0x03, 0x55, 0x1D,
+ 0x1F, 0x03, 0x55, 0x1D, 0x2E, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
+ 0x01, 0x01, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x0B
+};
+
+static const unsigned char t0_codeblock[] = {
+ 0x00, 0x01, 0x00, 0x0D, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x01,
+ 0x00, 0x11, 0x00, 0x00, 0x01, 0x01, 0x09, 0x00, 0x00, 0x01, 0x01, 0x0A,
+ 0x00, 0x00, 0x24, 0x24, 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_BOOLEAN), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_DN), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_SERVER_NAME), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TAG_CLASS), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TAG_VALUE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_BAD_TIME), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_CRITICAL_EXTENSION), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_DN_MISMATCH), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_EXPIRED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_EXTRA_ELEMENT), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_FORBIDDEN_KEY_USAGE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INDEFINITE_LENGTH), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_INNER_TRUNC), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_LIMIT_EXCEEDED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_CA), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_CONSTRUCTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_NOT_PRIMITIVE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_OVERFLOW), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_PARTIAL_BYTE), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNEXPECTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_UNSUPPORTED), 0x00, 0x00, 0x01,
+ T0_INT1(BR_ERR_X509_WEAK_PUBLIC_KEY), 0x00, 0x00, 0x01,
+ T0_INT1(BR_KEYTYPE_EC), 0x00, 0x00, 0x01, T0_INT1(BR_KEYTYPE_RSA),
+ 0x00, 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, cert_length)), 0x00,
+ 0x00, 0x01, T0_INT2(offsetof(CONTEXT_NAME, cert_sig)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, cert_sig_hash_len)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, cert_sig_hash_oid)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, cert_sig_len)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, cert_signer_key_type)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, current_dn_hash)), 0x00, 0x00,
+ 0x01, T0_INT2(offsetof(CONTEXT_NAME, key_usages)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(br_x509_minimal_context, pkey_data)), 0x01,
+ T0_INT2(BR_X509_BUFSIZE_KEY), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, min_rsa_size)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, next_dn_hash)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, num_certs)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, pad)), 0x00, 0x00, 0x01,
+ T0_INT2(offsetof(CONTEXT_NAME, saved_dn_hash)), 0x00, 0x00, 0xC9, 0x71,
+ 0x00, 0x00, 0x01, 0x80, 0x73, 0x00, 0x00, 0x01, 0x80, 0x7C, 0x00, 0x00,
+ 0x01, 0x81, 0x02, 0x00, 0x00, 0x92, 0x05, 0x05, 0x34, 0x42, 0x01, 0x00,
+ 0x00, 0x34, 0x01, 0x0A, 0x0E, 0x09, 0x01, 0x9A, 0xFF, 0xB8, 0x00, 0x0A,
+ 0x00, 0x00, 0x01, 0x82, 0x19, 0x00, 0x00, 0x01, 0x82, 0x01, 0x00, 0x00,
+ 0x01, 0x81, 0x68, 0x00, 0x04, 0x03, 0x00, 0x03, 0x01, 0x03, 0x02, 0x03,
+ 0x03, 0x02, 0x03, 0x02, 0x01, 0x11, 0x06, 0x07, 0x02, 0x02, 0x02, 0x00,
+ 0x0D, 0x04, 0x05, 0x02, 0x03, 0x02, 0x01, 0x0D, 0x00, 0x02, 0x03, 0x00,
+ 0x03, 0x01, 0x25, 0x02, 0x01, 0x13, 0x3B, 0x02, 0x00, 0x0F, 0x15, 0x00,
+ 0x00, 0x01, 0x81, 0x74, 0x00, 0x00, 0x05, 0x02, 0x52, 0x28, 0x00, 0x00,
+ 0x06, 0x02, 0x53, 0x28, 0x00, 0x00, 0x01, 0x10, 0x77, 0x00, 0x00, 0x11,
+ 0x05, 0x02, 0x56, 0x28, 0x74, 0x00, 0x00, 0x11, 0x05, 0x02, 0x56, 0x28,
+ 0x75, 0x00, 0x00, 0x06, 0x02, 0x4C, 0x28, 0x00, 0x00, 0x01, 0x82, 0x11,
+ 0x00, 0x00, 0x25, 0x20, 0x01, 0x08, 0x0E, 0x3B, 0x40, 0x20, 0x09, 0x00,
+ 0x09, 0x03, 0x00, 0x5B, 0x2B, 0xAF, 0x39, 0xAF, 0xB3, 0x25, 0x01, 0x20,
+ 0x11, 0x06, 0x11, 0x24, 0x74, 0xAD, 0xB3, 0x01, 0x02, 0x78, 0xB0, 0x01,
+ 0x02, 0x12, 0x06, 0x02, 0x57, 0x28, 0x79, 0xB3, 0x01, 0x02, 0x78, 0xAE,
+ 0xAF, 0xC2, 0x9C, 0x65, 0x61, 0x21, 0x16, 0xAF, 0xA7, 0x29, 0x69, 0x06,
+ 0x02, 0x4B, 0x28, 0xA7, 0x29, 0x71, 0x06, 0x02, 0x4B, 0x28, 0x79, 0x02,
+ 0x00, 0x06, 0x05, 0x9D, 0x03, 0x01, 0x04, 0x09, 0x9C, 0x61, 0x68, 0x21,
+ 0x27, 0x05, 0x02, 0x4A, 0x28, 0x68, 0x65, 0x21, 0x16, 0xAF, 0xAF, 0x9E,
+ 0x05, 0x02, 0x57, 0x28, 0xBC, 0x26, 0x06, 0x27, 0xC2, 0xA4, 0xAF, 0x63,
+ 0xAA, 0x03, 0x03, 0x63, 0x3B, 0x02, 0x03, 0x09, 0x3B, 0x02, 0x03, 0x0A,
+ 0xAA, 0x03, 0x04, 0x79, 0x64, 0x2A, 0x01, 0x81, 0x00, 0x09, 0x02, 0x03,
+ 0x12, 0x06, 0x02, 0x58, 0x28, 0x79, 0x5A, 0x03, 0x02, 0x04, 0x3A, 0x88,
+ 0x26, 0x06, 0x34, 0x9E, 0x05, 0x02, 0x57, 0x28, 0x6A, 0x26, 0x06, 0x04,
+ 0x01, 0x17, 0x04, 0x12, 0x6B, 0x26, 0x06, 0x04, 0x01, 0x18, 0x04, 0x0A,
+ 0x6C, 0x26, 0x06, 0x04, 0x01, 0x19, 0x04, 0x02, 0x57, 0x28, 0x03, 0x05,
+ 0x79, 0xA4, 0x25, 0x03, 0x06, 0x25, 0x63, 0x34, 0x0D, 0x06, 0x02, 0x50,
+ 0x28, 0xA5, 0x59, 0x03, 0x02, 0x04, 0x02, 0x57, 0x28, 0x79, 0x02, 0x00,
+ 0x06, 0x21, 0x02, 0x02, 0x5A, 0x30, 0x11, 0x06, 0x08, 0x24, 0x02, 0x03,
+ 0x02, 0x04, 0x1D, 0x04, 0x10, 0x59, 0x30, 0x11, 0x06, 0x08, 0x24, 0x02,
+ 0x05, 0x02, 0x06, 0x1C, 0x04, 0x03, 0x57, 0x28, 0x24, 0x04, 0x24, 0x02,
+ 0x02, 0x5A, 0x30, 0x11, 0x06, 0x08, 0x24, 0x02, 0x03, 0x02, 0x04, 0x23,
+ 0x04, 0x10, 0x59, 0x30, 0x11, 0x06, 0x08, 0x24, 0x02, 0x05, 0x02, 0x06,
+ 0x22, 0x04, 0x03, 0x57, 0x28, 0x24, 0x25, 0x06, 0x01, 0x28, 0x24, 0x01,
+ 0x00, 0x03, 0x07, 0xB4, 0x01, 0x21, 0x8F, 0x01, 0x22, 0x8F, 0x25, 0x01,
+ 0x23, 0x11, 0x06, 0x81, 0x26, 0x24, 0x74, 0xAD, 0xAF, 0x25, 0x06, 0x81,
+ 0x1A, 0x01, 0x00, 0x03, 0x08, 0xAF, 0x9E, 0x24, 0xB3, 0x25, 0x01, 0x01,
+ 0x11, 0x06, 0x04, 0xA6, 0x03, 0x08, 0xB3, 0x01, 0x04, 0x78, 0xAD, 0x70,
+ 0x26, 0x06, 0x0F, 0x02, 0x00, 0x06, 0x03, 0xC3, 0x04, 0x05, 0x99, 0x01,
+ 0x7F, 0x03, 0x07, 0x04, 0x80, 0x6C, 0x91, 0x26, 0x06, 0x06, 0x02, 0x00,
+ 0x9B, 0x04, 0x80, 0x62, 0xC5, 0x26, 0x06, 0x11, 0x02, 0x00, 0x06, 0x09,
+ 0x01, 0x00, 0x03, 0x01, 0x98, 0x03, 0x01, 0x04, 0x01, 0xC3, 0x04, 0x80,
+ 0x4D, 0x73, 0x26, 0x06, 0x0A, 0x02, 0x08, 0x06, 0x03, 0x9A, 0x04, 0x01,
+ 0xC3, 0x04, 0x3F, 0x6F, 0x26, 0x06, 0x03, 0xC3, 0x04, 0x38, 0xC8, 0x26,
+ 0x06, 0x03, 0xC3, 0x04, 0x31, 0x90, 0x26, 0x06, 0x03, 0xC3, 0x04, 0x2A,
+ 0xC6, 0x26, 0x06, 0x03, 0xC3, 0x04, 0x23, 0x7A, 0x26, 0x06, 0x03, 0xC3,
+ 0x04, 0x1C, 0x85, 0x26, 0x06, 0x03, 0xC3, 0x04, 0x15, 0x6E, 0x26, 0x06,
+ 0x03, 0xC3, 0x04, 0x0E, 0xC7, 0x26, 0x06, 0x03, 0xC3, 0x04, 0x07, 0x02,
+ 0x08, 0x06, 0x02, 0x49, 0x28, 0xC3, 0x79, 0x79, 0x04, 0xFE, 0x62, 0x79,
+ 0x79, 0x04, 0x08, 0x01, 0x7F, 0x11, 0x05, 0x02, 0x56, 0x28, 0x24, 0x79,
+ 0x3A, 0x02, 0x00, 0x06, 0x08, 0x02, 0x01, 0x3C, 0x2F, 0x05, 0x02, 0x45,
+ 0x28, 0x02, 0x00, 0x06, 0x01, 0x17, 0x02, 0x00, 0x02, 0x07, 0x2F, 0x05,
+ 0x02, 0x51, 0x28, 0xB3, 0x76, 0xAD, 0x9E, 0x06, 0x80, 0x77, 0xBD, 0x26,
+ 0x06, 0x07, 0x01, 0x02, 0x5A, 0x8A, 0x04, 0x80, 0x5E, 0xBE, 0x26, 0x06,
+ 0x07, 0x01, 0x03, 0x5A, 0x8B, 0x04, 0x80, 0x53, 0xBF, 0x26, 0x06, 0x07,
+ 0x01, 0x04, 0x5A, 0x8C, 0x04, 0x80, 0x48, 0xC0, 0x26, 0x06, 0x06, 0x01,
+ 0x05, 0x5A, 0x8D, 0x04, 0x3E, 0xC1, 0x26, 0x06, 0x06, 0x01, 0x06, 0x5A,
+ 0x8E, 0x04, 0x34, 0x7F, 0x26, 0x06, 0x06, 0x01, 0x02, 0x59, 0x8A, 0x04,
+ 0x2A, 0x80, 0x26, 0x06, 0x06, 0x01, 0x03, 0x59, 0x8B, 0x04, 0x20, 0x81,
+ 0x26, 0x06, 0x06, 0x01, 0x04, 0x59, 0x8C, 0x04, 0x16, 0x82, 0x26, 0x06,
+ 0x06, 0x01, 0x05, 0x59, 0x8D, 0x04, 0x0C, 0x83, 0x26, 0x06, 0x06, 0x01,
+ 0x06, 0x59, 0x8E, 0x04, 0x02, 0x57, 0x28, 0x5E, 0x35, 0x60, 0x37, 0x1B,
+ 0x25, 0x05, 0x02, 0x57, 0x28, 0x5D, 0x37, 0x04, 0x02, 0x57, 0x28, 0xC2,
+ 0xA4, 0x25, 0x01, T0_INT2(BR_X509_BUFSIZE_SIG), 0x12, 0x06, 0x02, 0x50,
+ 0x28, 0x25, 0x5F, 0x35, 0x5C, 0xA5, 0x79, 0x79, 0x01, 0x00, 0x5B, 0x36,
+ 0x18, 0x00, 0x00, 0x01, 0x30, 0x0A, 0x25, 0x01, 0x00, 0x01, 0x09, 0x72,
+ 0x05, 0x02, 0x48, 0x28, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x01, 0x81,
+ 0x08, 0x00, 0x00, 0x01, 0x81, 0x10, 0x00, 0x00, 0x01, 0x81, 0x19, 0x00,
+ 0x00, 0x01, 0x81, 0x22, 0x00, 0x00, 0x01, 0x81, 0x2B, 0x00, 0x01, 0x7E,
+ 0x01, 0x01, 0x11, 0x3B, 0x01, 0x83, 0xFD, 0x7F, 0x11, 0x15, 0x06, 0x03,
+ 0x3B, 0x24, 0x00, 0x3B, 0x25, 0x03, 0x00, 0x25, 0xCA, 0x05, 0x04, 0x42,
+ 0x01, 0x00, 0x00, 0x25, 0x01, 0x81, 0x00, 0x0D, 0x06, 0x04, 0x96, 0x04,
+ 0x80, 0x49, 0x25, 0x01, 0x90, 0x00, 0x0D, 0x06, 0x0F, 0x01, 0x06, 0x14,
+ 0x01, 0x81, 0x40, 0x2F, 0x96, 0x02, 0x00, 0x01, 0x00, 0x97, 0x04, 0x33,
+ 0x25, 0x01, 0x83, 0xFF, 0x7F, 0x0D, 0x06, 0x14, 0x01, 0x0C, 0x14, 0x01,
+ 0x81, 0x60, 0x2F, 0x96, 0x02, 0x00, 0x01, 0x06, 0x97, 0x02, 0x00, 0x01,
+ 0x00, 0x97, 0x04, 0x17, 0x01, 0x12, 0x14, 0x01, 0x81, 0x70, 0x2F, 0x96,
+ 0x02, 0x00, 0x01, 0x0C, 0x97, 0x02, 0x00, 0x01, 0x06, 0x97, 0x02, 0x00,
+ 0x01, 0x00, 0x97, 0x00, 0x00, 0x01, 0x82, 0x15, 0x00, 0x00, 0x25, 0x01,
+ 0x83, 0xB0, 0x00, 0x01, 0x83, 0xB7, 0x7F, 0x72, 0x00, 0x00, 0x01, 0x81,
+ 0x34, 0x00, 0x00, 0x01, 0x80, 0x6B, 0x00, 0x00, 0x01, 0x81, 0x78, 0x00,
+ 0x00, 0x01, 0x3D, 0x00, 0x00, 0x01, 0x80, 0x43, 0x00, 0x00, 0x01, 0x80,
+ 0x4D, 0x00, 0x00, 0x01, 0x80, 0x57, 0x00, 0x00, 0x01, 0x80, 0x61, 0x00,
+ 0x00, 0x30, 0x11, 0x06, 0x04, 0x42, 0xAD, 0xC2, 0xB4, 0x00, 0x00, 0x01,
+ 0x82, 0x09, 0x00, 0x00, 0x01, 0x81, 0x6C, 0x00, 0x00, 0x25, 0x01, 0x83,
+ 0xB8, 0x00, 0x01, 0x83, 0xBF, 0x7F, 0x72, 0x00, 0x00, 0x01, 0x30, 0x62,
+ 0x37, 0x01, 0x7F, 0x7C, 0x19, 0x01, 0x00, 0x7C, 0x19, 0x04, 0x7A, 0x00,
+ 0x01, 0x81, 0x38, 0x00, 0x01, 0x7E, 0x0D, 0x06, 0x02, 0x4F, 0x28, 0x25,
+ 0x03, 0x00, 0x0A, 0x02, 0x00, 0x00, 0x00, 0x30, 0x25, 0x3F, 0x3B, 0x01,
+ 0x82, 0x00, 0x13, 0x2F, 0x06, 0x04, 0x42, 0x01, 0x00, 0x00, 0x30, 0x67,
+ 0x09, 0x37, 0x40, 0x00, 0x00, 0x14, 0x01, 0x3F, 0x15, 0x01, 0x81, 0x00,
+ 0x2F, 0x96, 0x00, 0x02, 0x01, 0x00, 0x03, 0x00, 0xAF, 0x25, 0x06, 0x80,
+ 0x59, 0xB3, 0x01, 0x20, 0x30, 0x11, 0x06, 0x17, 0x24, 0x74, 0xAD, 0x9E,
+ 0x24, 0x01, 0x7F, 0x2E, 0x03, 0x01, 0xB3, 0x01, 0x20, 0x77, 0xAD, 0xB2,
+ 0x02, 0x01, 0x1F, 0x79, 0x79, 0x04, 0x38, 0x01, 0x21, 0x30, 0x11, 0x06,
+ 0x08, 0x24, 0x75, 0xB6, 0x01, 0x01, 0x1E, 0x04, 0x2A, 0x01, 0x22, 0x30,
+ 0x11, 0x06, 0x11, 0x24, 0x75, 0xB6, 0x25, 0x06, 0x06, 0x2C, 0x02, 0x00,
+ 0x2F, 0x03, 0x00, 0x01, 0x02, 0x1E, 0x04, 0x13, 0x01, 0x26, 0x30, 0x11,
+ 0x06, 0x08, 0x24, 0x75, 0xB6, 0x01, 0x06, 0x1E, 0x04, 0x05, 0x42, 0xAE,
+ 0x01, 0x00, 0x24, 0x04, 0xFF, 0x23, 0x79, 0x02, 0x00, 0x00, 0x00, 0xAF,
+ 0xB4, 0x25, 0x01, 0x01, 0x11, 0x06, 0x08, 0xA6, 0x05, 0x02, 0x51, 0x28,
+ 0xB4, 0x04, 0x02, 0x51, 0x28, 0x25, 0x01, 0x02, 0x11, 0x06, 0x0C, 0x24,
+ 0x75, 0xB0, 0x66, 0x2B, 0x41, 0x0D, 0x06, 0x02, 0x51, 0x28, 0xB4, 0x01,
+ 0x7F, 0x10, 0x06, 0x02, 0x56, 0x28, 0x24, 0x79, 0x00, 0x00, 0xAF, 0x25,
+ 0x06, 0x1A, 0xAF, 0x9E, 0x24, 0x25, 0x06, 0x11, 0xAF, 0x25, 0x06, 0x0C,
+ 0xAF, 0x9E, 0x24, 0x89, 0x26, 0x05, 0x02, 0x49, 0x28, 0xC2, 0x04, 0x71,
+ 0x79, 0x79, 0x04, 0x63, 0x79, 0x00, 0x02, 0x03, 0x00, 0xB3, 0x01, 0x03,
+ 0x78, 0xAD, 0xBA, 0x03, 0x01, 0x02, 0x01, 0x01, 0x07, 0x12, 0x06, 0x02,
+ 0x56, 0x28, 0x25, 0x01, 0x00, 0x30, 0x11, 0x06, 0x05, 0x24, 0x4D, 0x28,
+ 0x04, 0x15, 0x01, 0x01, 0x30, 0x11, 0x06, 0x0A, 0x24, 0xBA, 0x02, 0x01,
+ 0x14, 0x02, 0x01, 0x0E, 0x04, 0x05, 0x24, 0xBA, 0x01, 0x00, 0x24, 0x02,
+ 0x00, 0x06, 0x19, 0x01, 0x00, 0x30, 0x01, 0x38, 0x15, 0x06, 0x03, 0x01,
+ 0x10, 0x2F, 0x3B, 0x01, 0x81, 0x40, 0x15, 0x06, 0x03, 0x01, 0x20, 0x2F,
+ 0x62, 0x37, 0x04, 0x07, 0x01, 0x04, 0x15, 0x05, 0x02, 0x4D, 0x28, 0xC2,
+ 0x00, 0x00, 0x38, 0xAF, 0xC2, 0x1A, 0x00, 0x03, 0x01, 0x00, 0x03, 0x00,
+ 0x38, 0xAF, 0x25, 0x06, 0x30, 0xB3, 0x01, 0x11, 0x77, 0xAD, 0x25, 0x05,
+ 0x02, 0x44, 0x28, 0x25, 0x06, 0x20, 0xAF, 0x9E, 0x24, 0x87, 0x26, 0x03,
+ 0x01, 0x01, 0x00, 0x2E, 0x03, 0x02, 0xB2, 0x25, 0x02, 0x01, 0x15, 0x06,
+ 0x07, 0x2C, 0x06, 0x04, 0x01, 0x7F, 0x03, 0x00, 0x02, 0x02, 0x1F, 0x79,
+ 0x04, 0x5D, 0x79, 0x04, 0x4D, 0x79, 0x1A, 0x02, 0x00, 0x00, 0x00, 0xB3,
+ 0x01, 0x06, 0x78, 0xB1, 0x00, 0x00, 0xB8, 0x86, 0x06, 0x0E, 0x3B, 0x25,
+ 0x05, 0x06, 0x42, 0x01, 0x00, 0x01, 0x00, 0x00, 0xB8, 0x6D, 0x04, 0x08,
+ 0x92, 0x06, 0x05, 0x24, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0xB9, 0x86,
+ 0x06, 0x0E, 0x3B, 0x25, 0x05, 0x06, 0x42, 0x01, 0x00, 0x01, 0x00, 0x00,
+ 0xB9, 0x6D, 0x04, 0x08, 0x92, 0x06, 0x05, 0x24, 0x01, 0x00, 0x04, 0x00,
+ 0x00, 0x00, 0xBA, 0x25, 0x01, 0x81, 0x00, 0x0D, 0x06, 0x04, 0x00, 0x04,
+ 0x80, 0x55, 0x25, 0x01, 0x81, 0x40, 0x0D, 0x06, 0x07, 0x24, 0x01, 0x00,
+ 0x00, 0x04, 0x80, 0x47, 0x25, 0x01, 0x81, 0x60, 0x0D, 0x06, 0x0E, 0x01,
+ 0x1F, 0x15, 0x01, 0x01, 0xA3, 0x01, 0x81, 0x00, 0x01, 0x8F, 0x7F, 0x04,
+ 0x32, 0x25, 0x01, 0x81, 0x70, 0x0D, 0x06, 0x0F, 0x01, 0x0F, 0x15, 0x01,
+ 0x02, 0xA3, 0x01, 0x90, 0x00, 0x01, 0x83, 0xFF, 0x7F, 0x04, 0x1C, 0x25,
+ 0x01, 0x81, 0x78, 0x0D, 0x06, 0x11, 0x01, 0x07, 0x15, 0x01, 0x03, 0xA3,
+ 0x01, 0x84, 0x80, 0x00, 0x01, 0x80, 0xC3, 0xFF, 0x7F, 0x04, 0x04, 0x24,
+ 0x01, 0x00, 0x00, 0x72, 0x05, 0x03, 0x24, 0x01, 0x00, 0x00, 0x00, 0x3B,
+ 0x25, 0x05, 0x06, 0x42, 0x01, 0x00, 0x01, 0x7F, 0x00, 0xBA, 0x34, 0x25,
+ 0x3D, 0x06, 0x03, 0x3B, 0x24, 0x00, 0x01, 0x06, 0x0E, 0x3B, 0x25, 0x01,
+ 0x06, 0x14, 0x01, 0x02, 0x10, 0x06, 0x04, 0x42, 0x01, 0x7F, 0x00, 0x01,
+ 0x3F, 0x15, 0x09, 0x00, 0x00, 0x25, 0x06, 0x06, 0x0B, 0xA2, 0x34, 0x41,
+ 0x04, 0x77, 0x24, 0x25, 0x00, 0x00, 0xB3, 0x01, 0x03, 0x78, 0xAD, 0xBA,
+ 0x06, 0x02, 0x55, 0x28, 0x00, 0x00, 0x3B, 0x25, 0x06, 0x07, 0x31, 0x25,
+ 0x06, 0x01, 0x19, 0x04, 0x76, 0x42, 0x00, 0x00, 0x01, 0x01, 0x78, 0xAC,
+ 0x01, 0x01, 0x10, 0x06, 0x02, 0x43, 0x28, 0xBA, 0x3E, 0x00, 0x04, 0xB3,
+ 0x25, 0x01, 0x17, 0x01, 0x18, 0x72, 0x05, 0x02, 0x48, 0x28, 0x01, 0x18,
+ 0x11, 0x03, 0x00, 0x75, 0xAD, 0xA8, 0x02, 0x00, 0x06, 0x0C, 0x01, 0x80,
+ 0x64, 0x08, 0x03, 0x01, 0xA8, 0x02, 0x01, 0x09, 0x04, 0x0E, 0x25, 0x01,
+ 0x32, 0x0D, 0x06, 0x04, 0x01, 0x80, 0x64, 0x09, 0x01, 0x8E, 0x6C, 0x09,
+ 0x03, 0x01, 0x02, 0x01, 0x01, 0x82, 0x6D, 0x08, 0x02, 0x01, 0x01, 0x03,
+ 0x09, 0x01, 0x04, 0x0C, 0x09, 0x02, 0x01, 0x01, 0x80, 0x63, 0x09, 0x01,
+ 0x80, 0x64, 0x0C, 0x0A, 0x02, 0x01, 0x01, 0x83, 0x0F, 0x09, 0x01, 0x83,
+ 0x10, 0x0C, 0x09, 0x03, 0x03, 0x01, 0x01, 0x01, 0x0C, 0xA9, 0x41, 0x01,
+ 0x01, 0x0E, 0x02, 0x01, 0x01, 0x04, 0x07, 0x3F, 0x02, 0x01, 0x01, 0x80,
+ 0x64, 0x07, 0x3E, 0x02, 0x01, 0x01, 0x83, 0x10, 0x07, 0x3F, 0x2F, 0x15,
+ 0x06, 0x03, 0x01, 0x18, 0x09, 0x94, 0x09, 0x7B, 0x25, 0x01, 0x05, 0x14,
+ 0x02, 0x03, 0x09, 0x03, 0x03, 0x01, 0x1F, 0x15, 0x01, 0x01, 0x3B, 0xA9,
+ 0x02, 0x03, 0x09, 0x41, 0x03, 0x03, 0x01, 0x00, 0x01, 0x17, 0xA9, 0x01,
+ 0x9C, 0x10, 0x08, 0x03, 0x02, 0x01, 0x00, 0x01, 0x3B, 0xA9, 0x01, 0x3C,
+ 0x08, 0x02, 0x02, 0x09, 0x03, 0x02, 0x01, 0x00, 0x01, 0x3C, 0xA9, 0x02,
+ 0x02, 0x09, 0x03, 0x02, 0xBA, 0x25, 0x01, 0x2E, 0x11, 0x06, 0x0D, 0x24,
+ 0xBA, 0x25, 0x01, 0x30, 0x01, 0x39, 0x72, 0x06, 0x03, 0x24, 0x04, 0x74,
+ 0x01, 0x80, 0x5A, 0x10, 0x06, 0x02, 0x48, 0x28, 0x79, 0x02, 0x03, 0x02,
+ 0x02, 0x00, 0x01, 0xBA, 0x7D, 0x01, 0x0A, 0x08, 0x03, 0x00, 0xBA, 0x7D,
+ 0x02, 0x00, 0x09, 0x00, 0x02, 0x03, 0x00, 0x03, 0x01, 0xA8, 0x25, 0x02,
+ 0x01, 0x02, 0x00, 0x72, 0x05, 0x02, 0x48, 0x28, 0x00, 0x00, 0x34, 0xB3,
+ 0x01, 0x02, 0x78, 0x0B, 0xAB, 0x00, 0x03, 0x25, 0x03, 0x00, 0x03, 0x01,
+ 0x03, 0x02, 0xAD, 0xBA, 0x25, 0x01, 0x81, 0x00, 0x13, 0x06, 0x02, 0x54,
+ 0x28, 0x25, 0x01, 0x00, 0x11, 0x06, 0x0B, 0x24, 0x25, 0x05, 0x04, 0x24,
+ 0x01, 0x00, 0x00, 0xBA, 0x04, 0x6F, 0x02, 0x01, 0x25, 0x05, 0x02, 0x50,
+ 0x28, 0x41, 0x03, 0x01, 0x02, 0x02, 0x37, 0x02, 0x02, 0x40, 0x03, 0x02,
+ 0x25, 0x06, 0x03, 0xBA, 0x04, 0x68, 0x24, 0x02, 0x00, 0x02, 0x01, 0x0A,
+ 0x00, 0x01, 0xBA, 0x25, 0x01, 0x81, 0x00, 0x0D, 0x06, 0x01, 0x00, 0x01,
+ 0x81, 0x00, 0x0A, 0x25, 0x05, 0x02, 0x4E, 0x28, 0x03, 0x00, 0x01, 0x00,
+ 0x02, 0x00, 0x01, 0x00, 0x12, 0x06, 0x19, 0x02, 0x00, 0x41, 0x03, 0x00,
+ 0x25, 0x01, 0x83, 0xFF, 0xFF, 0x7F, 0x12, 0x06, 0x02, 0x4F, 0x28, 0x01,
+ 0x08, 0x0E, 0x3B, 0xBA, 0x34, 0x09, 0x04, 0x60, 0x00, 0x00, 0xAC, 0x95,
+ 0x00, 0x00, 0xAD, 0xC2, 0x00, 0x00, 0xB3, 0x76, 0xAD, 0x00, 0x01, 0xAD,
+ 0x25, 0x05, 0x02, 0x54, 0x28, 0xBA, 0x25, 0x01, 0x81, 0x00, 0x13, 0x06,
+ 0x02, 0x54, 0x28, 0x03, 0x00, 0x25, 0x06, 0x16, 0xBA, 0x02, 0x00, 0x25,
+ 0x01, 0x87, 0xFF, 0xFF, 0x7F, 0x13, 0x06, 0x02, 0x54, 0x28, 0x01, 0x08,
+ 0x0E, 0x09, 0x03, 0x00, 0x04, 0x67, 0x24, 0x02, 0x00, 0x00, 0x00, 0xAD,
+ 0x25, 0x01, 0x81, 0x7F, 0x12, 0x06, 0x08, 0xC2, 0x01, 0x00, 0x67, 0x37,
+ 0x01, 0x00, 0x00, 0x25, 0x67, 0x37, 0x67, 0x40, 0xA5, 0x01, 0x7F, 0x00,
+ 0x00, 0xB3, 0x01, 0x0C, 0x30, 0x11, 0x06, 0x05, 0x24, 0x75, 0xB6, 0x04,
+ 0x3E, 0x01, 0x12, 0x30, 0x11, 0x06, 0x05, 0x24, 0x75, 0xB7, 0x04, 0x33,
+ 0x01, 0x13, 0x30, 0x11, 0x06, 0x05, 0x24, 0x75, 0xB7, 0x04, 0x28, 0x01,
+ 0x14, 0x30, 0x11, 0x06, 0x05, 0x24, 0x75, 0xB7, 0x04, 0x1D, 0x01, 0x16,
+ 0x30, 0x11, 0x06, 0x05, 0x24, 0x75, 0xB7, 0x04, 0x12, 0x01, 0x1E, 0x30,
+ 0x11, 0x06, 0x05, 0x24, 0x75, 0xB5, 0x04, 0x07, 0x42, 0xAE, 0x01, 0x00,
+ 0x01, 0x00, 0x24, 0x00, 0x01, 0xBA, 0x03, 0x00, 0x02, 0x00, 0x01, 0x05,
+ 0x14, 0x01, 0x01, 0x15, 0x2D, 0x02, 0x00, 0x01, 0x06, 0x14, 0x25, 0x01,
+ 0x01, 0x15, 0x06, 0x02, 0x46, 0x28, 0x01, 0x04, 0x0E, 0x02, 0x00, 0x01,
+ 0x1F, 0x15, 0x25, 0x01, 0x1F, 0x11, 0x06, 0x02, 0x47, 0x28, 0x09, 0x00,
+ 0x00, 0x25, 0x05, 0x05, 0x01, 0x00, 0x01, 0x7F, 0x00, 0xB3, 0x00, 0x01,
+ 0xAD, 0x25, 0x05, 0x05, 0x67, 0x37, 0x01, 0x7F, 0x00, 0x01, 0x01, 0x03,
+ 0x00, 0x9F, 0x25, 0x01, 0x83, 0xFF, 0x7E, 0x11, 0x06, 0x16, 0x24, 0x25,
+ 0x06, 0x10, 0xA0, 0x25, 0x05, 0x05, 0x24, 0xC2, 0x01, 0x00, 0x00, 0x02,
+ 0x00, 0x84, 0x03, 0x00, 0x04, 0x6D, 0x04, 0x1B, 0x25, 0x05, 0x05, 0x24,
+ 0xC2, 0x01, 0x00, 0x00, 0x02, 0x00, 0x84, 0x03, 0x00, 0x25, 0x06, 0x0B,
+ 0x9F, 0x25, 0x05, 0x05, 0x24, 0xC2, 0x01, 0x00, 0x00, 0x04, 0x6D, 0x24,
+ 0x02, 0x00, 0x25, 0x05, 0x01, 0x00, 0x41, 0x67, 0x37, 0x01, 0x7F, 0x00,
+ 0x01, 0xAD, 0x01, 0x01, 0x03, 0x00, 0x25, 0x06, 0x10, 0xA1, 0x25, 0x05,
+ 0x05, 0x24, 0xC2, 0x01, 0x00, 0x00, 0x02, 0x00, 0x84, 0x03, 0x00, 0x04,
+ 0x6D, 0x24, 0x02, 0x00, 0x25, 0x05, 0x01, 0x00, 0x41, 0x67, 0x37, 0x01,
+ 0x7F, 0x00, 0x01, 0xAD, 0x01, 0x01, 0x03, 0x00, 0x25, 0x06, 0x10, 0xBA,
+ 0x25, 0x05, 0x05, 0x24, 0xC2, 0x01, 0x00, 0x00, 0x02, 0x00, 0x84, 0x03,
+ 0x00, 0x04, 0x6D, 0x24, 0x02, 0x00, 0x25, 0x05, 0x01, 0x00, 0x41, 0x67,
+ 0x37, 0x01, 0x7F, 0x00, 0x00, 0xBA, 0x01, 0x08, 0x0E, 0x3B, 0xBA, 0x34,
+ 0x09, 0x00, 0x00, 0xBA, 0x3B, 0xBA, 0x01, 0x08, 0x0E, 0x34, 0x09, 0x00,
+ 0x00, 0x25, 0x05, 0x02, 0x4F, 0x28, 0x41, 0xBB, 0x00, 0x00, 0x32, 0x25,
+ 0x01, 0x00, 0x13, 0x06, 0x01, 0x00, 0x24, 0x19, 0x04, 0x74, 0x00, 0x01,
+ 0x01, 0x00, 0x00, 0x01, 0x0B, 0x00, 0x00, 0x01, 0x15, 0x00, 0x00, 0x01,
+ 0x1F, 0x00, 0x00, 0x01, 0x29, 0x00, 0x00, 0x01, 0x33, 0x00, 0x00, 0xC3,
+ 0x24, 0x00, 0x00, 0x25, 0x06, 0x07, 0xC4, 0x25, 0x06, 0x01, 0x19, 0x04,
+ 0x76, 0x00, 0x00, 0x01, 0x00, 0x30, 0x31, 0x0B, 0x42, 0x00, 0x00, 0x01,
+ 0x81, 0x70, 0x00, 0x00, 0x01, 0x82, 0x0D, 0x00, 0x00, 0x01, 0x82, 0x22,
+ 0x00, 0x00, 0x01, 0x82, 0x05, 0x00, 0x00, 0x01, 0x03, 0x33, 0x01, 0x03,
+ 0x33, 0x00, 0x00, 0x25, 0x01, 0x83, 0xFB, 0x50, 0x01, 0x83, 0xFD, 0x5F,
+ 0x72, 0x06, 0x04, 0x24, 0x01, 0x00, 0x00, 0x25, 0x01, 0x83, 0xB0, 0x00,
+ 0x01, 0x83, 0xBF, 0x7F, 0x72, 0x06, 0x04, 0x24, 0x01, 0x00, 0x00, 0x01,
+ 0x83, 0xFF, 0x7F, 0x15, 0x01, 0x83, 0xFF, 0x7E, 0x0D, 0x00
+};
+
+static const uint16_t t0_caddr[] = {
+ 0,
+ 5,
+ 10,
+ 15,
+ 20,
+ 25,
+ 29,
+ 33,
+ 37,
+ 41,
+ 45,
+ 49,
+ 53,
+ 57,
+ 61,
+ 65,
+ 69,
+ 73,
+ 77,
+ 81,
+ 85,
+ 89,
+ 93,
+ 97,
+ 101,
+ 105,
+ 109,
+ 113,
+ 117,
+ 121,
+ 125,
+ 130,
+ 135,
+ 140,
+ 145,
+ 150,
+ 155,
+ 160,
+ 165,
+ 173,
+ 178,
+ 183,
+ 188,
+ 193,
+ 198,
+ 202,
+ 207,
+ 212,
+ 217,
+ 238,
+ 243,
+ 248,
+ 253,
+ 282,
+ 297,
+ 302,
+ 308,
+ 314,
+ 319,
+ 327,
+ 335,
+ 341,
+ 346,
+ 357,
+ 992,
+ 1007,
+ 1011,
+ 1016,
+ 1021,
+ 1026,
+ 1031,
+ 1036,
+ 1150,
+ 1155,
+ 1167,
+ 1172,
+ 1177,
+ 1182,
+ 1186,
+ 1191,
+ 1196,
+ 1201,
+ 1206,
+ 1216,
+ 1221,
+ 1226,
+ 1238,
+ 1253,
+ 1258,
+ 1272,
+ 1294,
+ 1305,
+ 1408,
+ 1455,
+ 1488,
+ 1579,
+ 1585,
+ 1648,
+ 1655,
+ 1683,
+ 1711,
+ 1816,
+ 1858,
+ 1871,
+ 1883,
+ 1897,
+ 1912,
+ 2132,
+ 2146,
+ 2163,
+ 2172,
+ 2239,
+ 2295,
+ 2299,
+ 2303,
+ 2308,
+ 2356,
+ 2382,
+ 2458,
+ 2502,
+ 2513,
+ 2598,
+ 2636,
+ 2674,
+ 2684,
+ 2694,
+ 2703,
+ 2716,
+ 2720,
+ 2724,
+ 2728,
+ 2732,
+ 2736,
+ 2740,
+ 2744,
+ 2756,
+ 2764,
+ 2769,
+ 2774,
+ 2779,
+ 2784,
+ 2792
+};
+
+#define T0_INTERPRETED 61
+
+#define T0_ENTER(ip, rp, slot) do { \
+ const unsigned char *t0_newip; \
+ uint32_t t0_lnum; \
+ t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \
+ t0_lnum = t0_parse7E_unsigned(&t0_newip); \
+ (rp) += t0_lnum; \
+ *((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \
+ (ip) = t0_newip; \
+ } while (0)
+
+#define T0_DEFENTRY(name, slot) \
+void \
+name(void *ctx) \
+{ \
+ t0_context *t0ctx = ctx; \
+ t0ctx->ip = &t0_codeblock[0]; \
+ T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \
+}
+
+T0_DEFENTRY(br_x509_minimal_init_main, 147)
+
+#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)
+
+void
+br_x509_minimal_run(void *t0ctx)
+{
+ uint32_t *dp, *rp;
+ const unsigned char *ip;
+
+#define T0_LOCAL(x) (*(rp - 2 - (x)))
+#define T0_POP() (*-- dp)
+#define T0_POPi() (*(int32_t *)(-- dp))
+#define T0_PEEK(x) (*(dp - 1 - (x)))
+#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))
+#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)
+#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)
+#define T0_RPOP() (*-- rp)
+#define T0_RPOPi() (*(int32_t *)(-- rp))
+#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)
+#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)
+#define T0_ROLL(x) do { \
+ size_t t0len = (size_t)(x); \
+ uint32_t t0tmp = *(dp - 1 - t0len); \
+ memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_SWAP() do { \
+ uint32_t t0tmp = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_ROT() do { \
+ uint32_t t0tmp = *(dp - 3); \
+ *(dp - 3) = *(dp - 2); \
+ *(dp - 2) = *(dp - 1); \
+ *(dp - 1) = t0tmp; \
+} while (0)
+#define T0_NROT() do { \
+ uint32_t t0tmp = *(dp - 1); \
+ *(dp - 1) = *(dp - 2); \
+ *(dp - 2) = *(dp - 3); \
+ *(dp - 3) = t0tmp; \
+} while (0)
+#define T0_PICK(x) do { \
+ uint32_t t0depth = (x); \
+ T0_PUSH(T0_PEEK(t0depth)); \
+} while (0)
+#define T0_CO() do { \
+ goto t0_exit; \
+} while (0)
+#define T0_RET() goto t0_next
+
+ dp = ((t0_context *)t0ctx)->dp;
+ rp = ((t0_context *)t0ctx)->rp;
+ ip = ((t0_context *)t0ctx)->ip;
+ goto t0_next;
+ for (;;) {
+ uint32_t t0x;
+
+ t0_next:
+ t0x = T0_NEXT(&ip);
+ if (t0x < T0_INTERPRETED) {
+ switch (t0x) {
+ int32_t t0off;
+
+ case 0: /* ret */
+ t0x = T0_RPOP();
+ rp -= (t0x >> 16);
+ t0x &= 0xFFFF;
+ if (t0x == 0) {
+ ip = NULL;
+ goto t0_exit;
+ }
+ ip = &t0_codeblock[t0x];
+ break;
+ case 1: /* literal constant */
+ T0_PUSHi(t0_parse7E_signed(&ip));
+ break;
+ case 2: /* read local */
+ T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));
+ break;
+ case 3: /* write local */
+ T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();
+ break;
+ case 4: /* jump */
+ t0off = t0_parse7E_signed(&ip);
+ ip += t0off;
+ break;
+ case 5: /* jump if */
+ t0off = t0_parse7E_signed(&ip);
+ if (T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 6: /* jump if not */
+ t0off = t0_parse7E_signed(&ip);
+ if (!T0_POP()) {
+ ip += t0off;
+ }
+ break;
+ case 7: {
+ /* %25 */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSHi(a % b);
+
+ }
+ break;
+ case 8: {
+ /* * */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a * b);
+
+ }
+ break;
+ case 9: {
+ /* + */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a + b);
+
+ }
+ break;
+ case 10: {
+ /* - */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a - b);
+
+ }
+ break;
+ case 11: {
+ /* -rot */
+ T0_NROT();
+ }
+ break;
+ case 12: {
+ /* / */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSHi(a / b);
+
+ }
+ break;
+ case 13: {
+ /* < */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a < b));
+
+ }
+ break;
+ case 14: {
+ /* << */
+
+ int c = (int)T0_POPi();
+ uint32_t x = T0_POP();
+ T0_PUSH(x << c);
+
+ }
+ break;
+ case 15: {
+ /* <= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a <= b));
+
+ }
+ break;
+ case 16: {
+ /* <> */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a != b));
+
+ }
+ break;
+ case 17: {
+ /* = */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(-(uint32_t)(a == b));
+
+ }
+ break;
+ case 18: {
+ /* > */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a > b));
+
+ }
+ break;
+ case 19: {
+ /* >= */
+
+ int32_t b = T0_POPi();
+ int32_t a = T0_POPi();
+ T0_PUSH(-(uint32_t)(a >= b));
+
+ }
+ break;
+ case 20: {
+ /* >> */
+
+ int c = (int)T0_POPi();
+ int32_t x = T0_POPi();
+ T0_PUSHi(x >> c);
+
+ }
+ break;
+ case 21: {
+ /* and */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a & b);
+
+ }
+ break;
+ case 22: {
+ /* blobcopy */
+
+ size_t len = T0_POP();
+ unsigned char *src = (unsigned char *)CTX + T0_POP();
+ unsigned char *dst = (unsigned char *)CTX + T0_POP();
+ memcpy(dst, src, len);
+
+ }
+ break;
+ case 23: {
+ /* check-direct-trust */
+
+ size_t u;
+
+ for (u = 0; u < CTX->trust_anchors_num; u ++) {
+ const br_x509_trust_anchor *ta;
+ unsigned char hashed_DN[64];
+ int kt;
+
+ ta = &CTX->trust_anchors[u];
+ if (ta->flags & BR_X509_TA_CA) {
+ continue;
+ }
+ hash_dn(CTX, ta->dn.data, ta->dn.len, hashed_DN);
+ if (memcmp(hashed_DN, CTX->current_dn_hash, DNHASH_LEN)) {
+ continue;
+ }
+ kt = CTX->pkey.key_type;
+ if ((ta->pkey.key_type & 0x0F) != kt) {
+ continue;
+ }
+ switch (kt) {
+
+ case BR_KEYTYPE_RSA:
+ if (!eqbigint(CTX->pkey.key.rsa.n,
+ CTX->pkey.key.rsa.nlen,
+ ta->pkey.key.rsa.n,
+ ta->pkey.key.rsa.nlen)
+ || !eqbigint(CTX->pkey.key.rsa.e,
+ CTX->pkey.key.rsa.elen,
+ ta->pkey.key.rsa.e,
+ ta->pkey.key.rsa.elen))
+ {
+ continue;
+ }
+ break;
+
+ case BR_KEYTYPE_EC:
+ if (CTX->pkey.key.ec.curve != ta->pkey.key.ec.curve
+ || CTX->pkey.key.ec.qlen != ta->pkey.key.ec.qlen
+ || memcmp(CTX->pkey.key.ec.q,
+ ta->pkey.key.ec.q,
+ ta->pkey.key.ec.qlen) != 0)
+ {
+ continue;
+ }
+ break;
+
+ default:
+ continue;
+ }
+
+ /*
+ * Direct trust match!
+ */
+ CTX->err = BR_ERR_X509_OK;
+ T0_CO();
+ }
+
+ }
+ break;
+ case 24: {
+ /* check-trust-anchor-CA */
+
+ size_t u;
+
+ for (u = 0; u < CTX->trust_anchors_num; u ++) {
+ const br_x509_trust_anchor *ta;
+ unsigned char hashed_DN[64];
+
+ ta = &CTX->trust_anchors[u];
+ if (!(ta->flags & BR_X509_TA_CA)) {
+ continue;
+ }
+ hash_dn(CTX, ta->dn.data, ta->dn.len, hashed_DN);
+ if (memcmp(hashed_DN, CTX->saved_dn_hash, DNHASH_LEN)) {
+ continue;
+ }
+ if (verify_signature(CTX, &ta->pkey) == 0) {
+ CTX->err = BR_ERR_X509_OK;
+ T0_CO();
+ }
+ }
+
+ }
+ break;
+ case 25: {
+ /* co */
+ T0_CO();
+ }
+ break;
+ case 26: {
+ /* compute-dn-hash */
+
+ CTX->dn_hash_impl->out(&CTX->dn_hash.vtable, CTX->current_dn_hash);
+ CTX->do_dn_hash = 0;
+
+ }
+ break;
+ case 27: {
+ /* compute-tbs-hash */
+
+ int id = T0_POPi();
+ size_t len;
+ len = br_multihash_out(&CTX->mhash, id, CTX->tbs_hash);
+ T0_PUSH(len);
+
+ }
+ break;
+ case 28: {
+ /* copy-ee-ec-pkey */
+
+ size_t qlen = T0_POP();
+ uint32_t curve = T0_POP();
+ memcpy(CTX->ee_pkey_data, CTX->pkey_data, qlen);
+ CTX->pkey.key_type = BR_KEYTYPE_EC;
+ CTX->pkey.key.ec.curve = curve;
+ CTX->pkey.key.ec.q = CTX->ee_pkey_data;
+ CTX->pkey.key.ec.qlen = qlen;
+
+ }
+ break;
+ case 29: {
+ /* copy-ee-rsa-pkey */
+
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ memcpy(CTX->ee_pkey_data, CTX->pkey_data, nlen + elen);
+ CTX->pkey.key_type = BR_KEYTYPE_RSA;
+ CTX->pkey.key.rsa.n = CTX->ee_pkey_data;
+ CTX->pkey.key.rsa.nlen = nlen;
+ CTX->pkey.key.rsa.e = CTX->ee_pkey_data + nlen;
+ CTX->pkey.key.rsa.elen = elen;
+
+ }
+ break;
+ case 30: {
+ /* copy-name-SAN */
+
+ unsigned tag = T0_POP();
+ unsigned ok = T0_POP();
+ size_t u, len;
+
+ len = CTX->pad[0];
+ for (u = 0; u < CTX->num_name_elts; u ++) {
+ br_name_element *ne;
+
+ ne = &CTX->name_elts[u];
+ if (ne->status == 0 && ne->oid[0] == 0 && ne->oid[1] == tag) {
+ if (ok && ne->len > len) {
+ memcpy(ne->buf, CTX->pad + 1, len);
+ ne->buf[len] = 0;
+ ne->status = 1;
+ } else {
+ ne->status = -1;
+ }
+ break;
+ }
+ }
+
+ }
+ break;
+ case 31: {
+ /* copy-name-element */
+
+ size_t len;
+ int32_t off = T0_POPi();
+ int ok = T0_POPi();
+
+ if (off >= 0) {
+ br_name_element *ne = &CTX->name_elts[off];
+
+ if (ok) {
+ len = CTX->pad[0];
+ if (len < ne->len) {
+ memcpy(ne->buf, CTX->pad + 1, len);
+ ne->buf[len] = 0;
+ ne->status = 1;
+ } else {
+ ne->status = -1;
+ }
+ } else {
+ ne->status = -1;
+ }
+ }
+
+ }
+ break;
+ case 32: {
+ /* data-get8 */
+
+ size_t addr = T0_POP();
+ T0_PUSH(t0_datablock[addr]);
+
+ }
+ break;
+ case 33: {
+ /* dn-hash-length */
+
+ T0_PUSH(DNHASH_LEN);
+
+ }
+ break;
+ case 34: {
+ /* do-ecdsa-vrfy */
+
+ size_t qlen = T0_POP();
+ int curve = T0_POP();
+ br_x509_pkey pk;
+
+ pk.key_type = BR_KEYTYPE_EC;
+ pk.key.ec.curve = curve;
+ pk.key.ec.q = CTX->pkey_data;
+ pk.key.ec.qlen = qlen;
+ T0_PUSH(verify_signature(CTX, &pk));
+
+ }
+ break;
+ case 35: {
+ /* do-rsa-vrfy */
+
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ br_x509_pkey pk;
+
+ pk.key_type = BR_KEYTYPE_RSA;
+ pk.key.rsa.n = CTX->pkey_data;
+ pk.key.rsa.nlen = nlen;
+ pk.key.rsa.e = CTX->pkey_data + nlen;
+ pk.key.rsa.elen = elen;
+ T0_PUSH(verify_signature(CTX, &pk));
+
+ }
+ break;
+ case 36: {
+ /* drop */
+ (void)T0_POP();
+ }
+ break;
+ case 37: {
+ /* dup */
+ T0_PUSH(T0_PEEK(0));
+ }
+ break;
+ case 38: {
+ /* eqOID */
+
+ const unsigned char *a2 = &t0_datablock[T0_POP()];
+ const unsigned char *a1 = &CTX->pad[0];
+ size_t len = a1[0];
+ int x;
+ if (len == a2[0]) {
+ x = -(memcmp(a1 + 1, a2 + 1, len) == 0);
+ } else {
+ x = 0;
+ }
+ T0_PUSH((uint32_t)x);
+
+ }
+ break;
+ case 39: {
+ /* eqblob */
+
+ size_t len = T0_POP();
+ const unsigned char *a2 = (const unsigned char *)CTX + T0_POP();
+ const unsigned char *a1 = (const unsigned char *)CTX + T0_POP();
+ T0_PUSHi(-(memcmp(a1, a2, len) == 0));
+
+ }
+ break;
+ case 40: {
+ /* fail */
+
+ CTX->err = T0_POPi();
+ T0_CO();
+
+ }
+ break;
+ case 41: {
+ /* get-system-date */
+
+ if (CTX->days == 0 && CTX->seconds == 0) {
+#if BR_USE_UNIX_TIME
+ time_t x = time(NULL);
+
+ T0_PUSH((uint32_t)(x / 86400) + 719528);
+ T0_PUSH((uint32_t)(x % 86400));
+#elif BR_USE_WIN32_TIME
+ FILETIME ft;
+ uint64_t x;
+
+ GetSystemTimeAsFileTime(&ft);
+ x = ((uint64_t)ft.dwHighDateTime << 32)
+ + (uint64_t)ft.dwLowDateTime;
+ x = (x / 10000000);
+ T0_PUSH((uint32_t)(x / 86400) + 584754);
+ T0_PUSH((uint32_t)(x % 86400));
+#else
+ CTX->err = BR_ERR_X509_TIME_UNKNOWN;
+ T0_CO();
+#endif
+ } else {
+ T0_PUSH(CTX->days);
+ T0_PUSH(CTX->seconds);
+ }
+
+ }
+ break;
+ case 42: {
+ /* get16 */
+
+ uint32_t addr = T0_POP();
+ T0_PUSH(*(uint16_t *)(void *)((unsigned char *)CTX + addr));
+
+ }
+ break;
+ case 43: {
+ /* get32 */
+
+ uint32_t addr = T0_POP();
+ T0_PUSH(*(uint32_t *)(void *)((unsigned char *)CTX + addr));
+
+ }
+ break;
+ case 44: {
+ /* match-server-name */
+
+ size_t n1, n2;
+
+ if (CTX->server_name == NULL) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ n1 = strlen(CTX->server_name);
+ n2 = CTX->pad[0];
+ if (n1 == n2 && eqnocase(&CTX->pad[1], CTX->server_name, n1)) {
+ T0_PUSHi(-1);
+ T0_RET();
+ }
+ if (n2 >= 2 && CTX->pad[1] == '*' && CTX->pad[2] == '.') {
+ size_t u;
+
+ u = 0;
+ while (u < n1 && CTX->server_name[u] != '.') {
+ u ++;
+ }
+ u ++;
+ n1 -= u;
+ if ((n2 - 2) == n1
+ && eqnocase(&CTX->pad[3], CTX->server_name + u, n1))
+ {
+ T0_PUSHi(-1);
+ T0_RET();
+ }
+ }
+ T0_PUSH(0);
+
+ }
+ break;
+ case 45: {
+ /* neg */
+
+ uint32_t a = T0_POP();
+ T0_PUSH(-a);
+
+ }
+ break;
+ case 46: {
+ /* offset-name-element */
+
+ unsigned san = T0_POP();
+ size_t u;
+
+ for (u = 0; u < CTX->num_name_elts; u ++) {
+ if (CTX->name_elts[u].status == 0) {
+ const unsigned char *oid;
+ size_t len, off;
+
+ oid = CTX->name_elts[u].oid;
+ if (san) {
+ if (oid[0] != 0 || oid[1] != 0) {
+ continue;
+ }
+ off = 2;
+ } else {
+ off = 0;
+ }
+ len = oid[off];
+ if (len != 0 && len == CTX->pad[0]
+ && memcmp(oid + off + 1,
+ CTX->pad + 1, len) == 0)
+ {
+ T0_PUSH(u);
+ T0_RET();
+ }
+ }
+ }
+ T0_PUSHi(-1);
+
+ }
+ break;
+ case 47: {
+ /* or */
+
+ uint32_t b = T0_POP();
+ uint32_t a = T0_POP();
+ T0_PUSH(a | b);
+
+ }
+ break;
+ case 48: {
+ /* over */
+ T0_PUSH(T0_PEEK(1));
+ }
+ break;
+ case 49: {
+ /* read-blob-inner */
+
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ if (CTX->do_mhash) {
+ br_multihash_update(&CTX->mhash, CTX->hbuf, clen);
+ }
+ if (CTX->do_dn_hash) {
+ CTX->dn_hash_impl->update(
+ &CTX->dn_hash.vtable, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+
+ }
+ break;
+ case 50: {
+ /* read8-low */
+
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ unsigned char x = *CTX->hbuf ++;
+ if (CTX->do_mhash) {
+ br_multihash_update(&CTX->mhash, &x, 1);
+ }
+ if (CTX->do_dn_hash) {
+ CTX->dn_hash_impl->update(&CTX->dn_hash.vtable, &x, 1);
+ }
+ CTX->hlen --;
+ T0_PUSH(x);
+ }
+
+ }
+ break;
+ case 51: {
+ /* roll */
+ T0_ROLL(T0_POP());
+ }
+ break;
+ case 52: {
+ /* rot */
+ T0_ROT();
+ }
+ break;
+ case 53: {
+ /* set16 */
+
+ uint32_t addr = T0_POP();
+ *(uint16_t *)(void *)((unsigned char *)CTX + addr) = T0_POP();
+
+ }
+ break;
+ case 54: {
+ /* set32 */
+
+ uint32_t addr = T0_POP();
+ *(uint32_t *)(void *)((unsigned char *)CTX + addr) = T0_POP();
+
+ }
+ break;
+ case 55: {
+ /* set8 */
+
+ uint32_t addr = T0_POP();
+ *((unsigned char *)CTX + addr) = (unsigned char)T0_POP();
+
+ }
+ break;
+ case 56: {
+ /* start-dn-hash */
+
+ CTX->dn_hash_impl->init(&CTX->dn_hash.vtable);
+ CTX->do_dn_hash = 1;
+
+ }
+ break;
+ case 57: {
+ /* start-tbs-hash */
+
+ br_multihash_init(&CTX->mhash);
+ CTX->do_mhash = 1;
+
+ }
+ break;
+ case 58: {
+ /* stop-tbs-hash */
+
+ CTX->do_mhash = 0;
+
+ }
+ break;
+ case 59: {
+ /* swap */
+ T0_SWAP();
+ }
+ break;
+ case 60: {
+ /* zero-server-name */
+
+ T0_PUSHi(-(CTX->server_name == NULL));
+
+ }
+ break;
+ }
+
+ } else {
+ T0_ENTER(ip, rp, t0x);
+ }
+ }
+t0_exit:
+ ((t0_context *)t0ctx)->dp = dp;
+ ((t0_context *)t0ctx)->rp = rp;
+ ((t0_context *)t0ctx)->ip = ip;
+}
+
+
+
+/*
+ * Verify the signature on the certificate with the provided public key.
+ * This function checks the public key type with regards to the expected
+ * type. Returned value is either 0 on success, or a non-zero error code.
+ */
+static int
+verify_signature(br_x509_minimal_context *ctx, const br_x509_pkey *pk)
+{
+ int kt;
+
+ kt = ctx->cert_signer_key_type;
+ if ((pk->key_type & 0x0F) != kt) {
+ return BR_ERR_X509_WRONG_KEY_TYPE;
+ }
+ switch (kt) {
+ unsigned char tmp[64];
+
+ case BR_KEYTYPE_RSA:
+ if (ctx->irsa == 0) {
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+ if (!ctx->irsa(ctx->cert_sig, ctx->cert_sig_len,
+ &t0_datablock[ctx->cert_sig_hash_oid],
+ ctx->cert_sig_hash_len, &pk->key.rsa, tmp))
+ {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ if (memcmp(ctx->tbs_hash, tmp, ctx->cert_sig_hash_len) != 0) {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ return 0;
+
+ case BR_KEYTYPE_EC:
+ if (ctx->iecdsa == 0) {
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+ if (!ctx->iecdsa(ctx->iec, ctx->tbs_hash,
+ ctx->cert_sig_hash_len, &pk->key.ec,
+ ctx->cert_sig, ctx->cert_sig_len))
+ {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ return 0;
+
+ default:
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+}
+
+
diff --git a/test/monniaux/BearSSL/src/x509/x509_minimal.t0 b/test/monniaux/BearSSL/src/x509/x509_minimal.t0
new file mode 100644
index 00000000..1e60016d
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_minimal.t0
@@ -0,0 +1,1508 @@
+\ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+\
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+\ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+\ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+\ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+\ SOFTWARE.
+
+preamble {
+
+#include "inner.h"
+
+/*
+ * Implementation Notes
+ * --------------------
+ *
+ * The C code pushes the data by chunks; all decoding is done in the
+ * T0 code. The cert_length value is set to the certificate length when
+ * a new certificate is started; the T0 code picks it up as outer limit,
+ * and decoding functions use it to ensure that no attempt is made at
+ * reading past it. The T0 code also checks that once the certificate is
+ * decoded, there are no trailing bytes.
+ *
+ * The T0 code sets cert_length to 0 when the certificate is fully
+ * decoded.
+ *
+ * The C code must still perform two checks:
+ *
+ * -- If the certificate length is 0, then the T0 code will not be
+ * invoked at all. This invalid condition must thus be reported by the
+ * C code.
+ *
+ * -- When reaching the end of certificate, the C code must verify that
+ * the certificate length has been set to 0, thereby signaling that
+ * the T0 code properly decoded a certificate.
+ *
+ * Processing of a chain works in the following way:
+ *
+ * -- The error flag is set to a non-zero value when validation is
+ * finished. The value is either BR_ERR_X509_OK (validation is
+ * successful) or another non-zero error code. When a non-zero error
+ * code is obtained, the remaining bytes in the current certificate and
+ * the subsequent certificates (if any) are completely ignored.
+ *
+ * -- Each certificate is decoded in due course, with the following
+ * "interesting points":
+ *
+ * -- Start of the TBS: the multihash engine is reset and activated.
+ *
+ * -- Start of the issuer DN: the secondary hash engine is started,
+ * to process the encoded issuer DN.
+ *
+ * -- End of the issuer DN: the secondary hash engine is stopped. The
+ * resulting hash value is computed and then copied into the
+ * next_dn_hash[] buffer.
+ *
+ * -- Start of the subject DN: the secondary hash engine is started,
+ * to process the encoded subject DN.
+ *
+ * -- For the EE certificate only: the Common Name, if any, is matched
+ * against the expected server name.
+ *
+ * -- End of the subject DN: the secondary hash engine is stopped. The
+ * resulting hash value is computed into the pad. It is then processed:
+ *
+ * -- If this is the EE certificate, then the hash is ignored
+ * (except for direct trust processing, see later; the hash is
+ * simply left in current_dn_hash[]).
+ *
+ * -- Otherwise, the hashed subject DN is compared with the saved
+ * hash value (in saved_dn_hash[]). They must match.
+ *
+ * Either way, the next_dn_hash[] value is then copied into the
+ * saved_dn_hash[] value. Thus, at that point, saved_dn_hash[]
+ * contains the hash of the issuer DN for the current certificate,
+ * and current_dn_hash[] contains the hash of the subject DN for the
+ * current certificate.
+ *
+ * -- Public key: it is decoded into the cert_pkey[] buffer. Unknown
+ * key types are reported at that point.
+ *
+ * -- If this is the EE certificate, then the key type is compared
+ * with the expected key type (initialization parameter). The public
+ * key data is copied to ee_pkey_data[]. The key and hashed subject
+ * DN are also compared with the "direct trust" keys; if the key
+ * and DN are matched, then validation ends with a success.
+ *
+ * -- Otherwise, the saved signature (cert_sig[]) is verified
+ * against the saved TBS hash (tbs_hash[]) and that freshly
+ * decoded public key. Failure here ends validation with an error.
+ *
+ * -- Extensions: extension values are processed in due order.
+ *
+ * -- Basic Constraints: for all certificates except EE, must be
+ * present, indicate a CA, and have a path legnth compatible with
+ * the chain length so far.
+ *
+ * -- Key Usage: for the EE, if present, must allow signatures
+ * or encryption/key exchange, as required for the cipher suite.
+ * For non-EE, if present, must have the "certificate sign" bit.
+ *
+ * -- Subject Alt Name: for the EE, dNSName names are matched
+ * against the server name. Ignored for non-EE.
+ *
+ * -- Authority Key Identifier, Subject Key Identifier, Issuer
+ * Alt Name, Subject Directory Attributes, CRL Distribution Points
+ * Freshest CRL, Authority Info Access and Subject Info Access
+ * extensions are always ignored: they either contain only
+ * informative data, or they relate to revocation processing, which
+ * we explicitly do not support.
+ *
+ * -- All other extensions are ignored if non-critical. If a
+ * critical extension other than the ones above is encountered,
+ * then a failure is reported.
+ *
+ * -- End of the TBS: the multihash engine is stopped.
+ *
+ * -- Signature algorithm: the signature algorithm on the
+ * certificate is decoded. A failure is reported if that algorithm
+ * is unknown. The hashed TBS corresponding to the signature hash
+ * function is computed and stored in tbs_hash[] (if not supported,
+ * then a failure is reported). The hash OID and length are stored
+ * in cert_sig_hash_oid and cert_sig_hash_len.
+ *
+ * -- Signature value: the signature value is copied into the
+ * cert_sig[] array.
+ *
+ * -- Certificate end: the hashed issuer DN (saved_dn_hash[]) is
+ * looked up in the trust store (CA trust anchors only); for all
+ * that match, the signature (cert_sig[]) is verified against the
+ * anchor public key (hashed TBS is in tbs_hash[]). If one of these
+ * signatures is valid, then validation ends with a success.
+ *
+ * -- If the chain end is reached without obtaining a validation success,
+ * then validation is reported as failed.
+ */
+
+#if BR_USE_UNIX_TIME
+#include <time.h>
+#endif
+
+#if BR_USE_WIN32_TIME
+#include <windows.h>
+#endif
+
+/*
+ * The T0 compiler will produce these prototypes declarations in the
+ * header.
+ *
+void br_x509_minimal_init_main(void *ctx);
+void br_x509_minimal_run(void *ctx);
+ */
+
+/* see bearssl_x509.h */
+void
+br_x509_minimal_init(br_x509_minimal_context *ctx,
+ const br_hash_class *dn_hash_impl,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num)
+{
+ memset(ctx, 0, sizeof *ctx);
+ ctx->vtable = &br_x509_minimal_vtable;
+ ctx->dn_hash_impl = dn_hash_impl;
+ ctx->trust_anchors = trust_anchors;
+ ctx->trust_anchors_num = trust_anchors_num;
+}
+
+static void
+xm_start_chain(const br_x509_class **ctx, const char *server_name)
+{
+ br_x509_minimal_context *cc;
+ size_t u;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ for (u = 0; u < cc->num_name_elts; u ++) {
+ cc->name_elts[u].status = 0;
+ cc->name_elts[u].buf[0] = 0;
+ }
+ memset(&cc->pkey, 0, sizeof cc->pkey);
+ cc->num_certs = 0;
+ cc->err = 0;
+ cc->cpu.dp = cc->dp_stack;
+ cc->cpu.rp = cc->rp_stack;
+ br_x509_minimal_init_main(&cc->cpu);
+ if (server_name == NULL || *server_name == 0) {
+ cc->server_name = NULL;
+ } else {
+ cc->server_name = server_name;
+ }
+}
+
+static void
+xm_start_cert(const br_x509_class **ctx, uint32_t length)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err != 0) {
+ return;
+ }
+ if (length == 0) {
+ cc->err = BR_ERR_X509_TRUNCATED;
+ return;
+ }
+ cc->cert_length = length;
+}
+
+static void
+xm_append(const br_x509_class **ctx, const unsigned char *buf, size_t len)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err != 0) {
+ return;
+ }
+ cc->hbuf = buf;
+ cc->hlen = len;
+ br_x509_minimal_run(&cc->cpu);
+}
+
+static void
+xm_end_cert(const br_x509_class **ctx)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == 0 && cc->cert_length != 0) {
+ cc->err = BR_ERR_X509_TRUNCATED;
+ }
+ cc->num_certs ++;
+}
+
+static unsigned
+xm_end_chain(const br_x509_class **ctx)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == 0) {
+ if (cc->num_certs == 0) {
+ cc->err = BR_ERR_X509_EMPTY_CHAIN;
+ } else {
+ cc->err = BR_ERR_X509_NOT_TRUSTED;
+ }
+ } else if (cc->err == BR_ERR_X509_OK) {
+ return 0;
+ }
+ return (unsigned)cc->err;
+}
+
+static const br_x509_pkey *
+xm_get_pkey(const br_x509_class *const *ctx, unsigned *usages)
+{
+ br_x509_minimal_context *cc;
+
+ cc = (br_x509_minimal_context *)(void *)ctx;
+ if (cc->err == BR_ERR_X509_OK
+ || cc->err == BR_ERR_X509_NOT_TRUSTED)
+ {
+ if (usages != NULL) {
+ *usages = cc->key_usages;
+ }
+ return &((br_x509_minimal_context *)(void *)ctx)->pkey;
+ } else {
+ return NULL;
+ }
+}
+
+/* see bearssl_x509.h */
+const br_x509_class br_x509_minimal_vtable = {
+ sizeof(br_x509_minimal_context),
+ xm_start_chain,
+ xm_start_cert,
+ xm_append,
+ xm_end_cert,
+ xm_end_chain,
+ xm_get_pkey
+};
+
+#define CTX ((br_x509_minimal_context *)(void *)((unsigned char *)t0ctx - offsetof(br_x509_minimal_context, cpu)))
+#define CONTEXT_NAME br_x509_minimal_context
+
+#define DNHASH_LEN ((CTX->dn_hash_impl->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK)
+
+/*
+ * Hash a DN (from a trust anchor) into the provided buffer. This uses the
+ * DN hash implementation and context structure from the X.509 engine
+ * context.
+ */
+static void
+hash_dn(br_x509_minimal_context *ctx, const void *dn, size_t len,
+ unsigned char *out)
+{
+ ctx->dn_hash_impl->init(&ctx->dn_hash.vtable);
+ ctx->dn_hash_impl->update(&ctx->dn_hash.vtable, dn, len);
+ ctx->dn_hash_impl->out(&ctx->dn_hash.vtable, out);
+}
+
+/*
+ * Compare two big integers for equality. The integers use unsigned big-endian
+ * encoding; extra leading bytes (of value 0) are allowed.
+ */
+static int
+eqbigint(const unsigned char *b1, size_t len1,
+ const unsigned char *b2, size_t len2)
+{
+ while (len1 > 0 && *b1 == 0) {
+ b1 ++;
+ len1 --;
+ }
+ while (len2 > 0 && *b2 == 0) {
+ b2 ++;
+ len2 --;
+ }
+ if (len1 != len2) {
+ return 0;
+ }
+ return memcmp(b1, b2, len1) == 0;
+}
+
+/*
+ * Compare two strings for equality, in a case-insensitive way. This
+ * function handles casing only for ASCII letters.
+ */
+static int
+eqnocase(const void *s1, const void *s2, size_t len)
+{
+ const unsigned char *buf1, *buf2;
+
+ buf1 = s1;
+ buf2 = s2;
+ while (len -- > 0) {
+ int x1, x2;
+
+ x1 = *buf1 ++;
+ x2 = *buf2 ++;
+ if (x1 >= 'A' && x1 <= 'Z') {
+ x1 += 'a' - 'A';
+ }
+ if (x2 >= 'A' && x2 <= 'Z') {
+ x2 += 'a' - 'A';
+ }
+ if (x1 != x2) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static int verify_signature(br_x509_minimal_context *ctx,
+ const br_x509_pkey *pk);
+
+}
+
+postamble {
+
+/*
+ * Verify the signature on the certificate with the provided public key.
+ * This function checks the public key type with regards to the expected
+ * type. Returned value is either 0 on success, or a non-zero error code.
+ */
+static int
+verify_signature(br_x509_minimal_context *ctx, const br_x509_pkey *pk)
+{
+ int kt;
+
+ kt = ctx->cert_signer_key_type;
+ if ((pk->key_type & 0x0F) != kt) {
+ return BR_ERR_X509_WRONG_KEY_TYPE;
+ }
+ switch (kt) {
+ unsigned char tmp[64];
+
+ case BR_KEYTYPE_RSA:
+ if (ctx->irsa == 0) {
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+ if (!ctx->irsa(ctx->cert_sig, ctx->cert_sig_len,
+ &t0_datablock[ctx->cert_sig_hash_oid],
+ ctx->cert_sig_hash_len, &pk->key.rsa, tmp))
+ {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ if (memcmp(ctx->tbs_hash, tmp, ctx->cert_sig_hash_len) != 0) {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ return 0;
+
+ case BR_KEYTYPE_EC:
+ if (ctx->iecdsa == 0) {
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+ if (!ctx->iecdsa(ctx->iec, ctx->tbs_hash,
+ ctx->cert_sig_hash_len, &pk->key.ec,
+ ctx->cert_sig, ctx->cert_sig_len))
+ {
+ return BR_ERR_X509_BAD_SIGNATURE;
+ }
+ return 0;
+
+ default:
+ return BR_ERR_X509_UNSUPPORTED;
+ }
+}
+
+}
+
+cc: read8-low ( -- x ) {
+ if (CTX->hlen == 0) {
+ T0_PUSHi(-1);
+ } else {
+ unsigned char x = *CTX->hbuf ++;
+ if (CTX->do_mhash) {
+ br_multihash_update(&CTX->mhash, &x, 1);
+ }
+ if (CTX->do_dn_hash) {
+ CTX->dn_hash_impl->update(&CTX->dn_hash.vtable, &x, 1);
+ }
+ CTX->hlen --;
+ T0_PUSH(x);
+ }
+}
+
+addr: cert_length
+addr: num_certs
+
+cc: read-blob-inner ( addr len -- addr len ) {
+ uint32_t len = T0_POP();
+ uint32_t addr = T0_POP();
+ size_t clen = CTX->hlen;
+ if (clen > len) {
+ clen = (size_t)len;
+ }
+ if (addr != 0) {
+ memcpy((unsigned char *)CTX + addr, CTX->hbuf, clen);
+ }
+ if (CTX->do_mhash) {
+ br_multihash_update(&CTX->mhash, CTX->hbuf, clen);
+ }
+ if (CTX->do_dn_hash) {
+ CTX->dn_hash_impl->update(
+ &CTX->dn_hash.vtable, CTX->hbuf, clen);
+ }
+ CTX->hbuf += clen;
+ CTX->hlen -= clen;
+ T0_PUSH(addr + clen);
+ T0_PUSH(len - clen);
+}
+
+\ Compute the TBS hash, using the provided hash ID. The hash value is
+\ written in the tbs_hash[] array, and the hash length is returned. If
+\ the requested hash function is not supported, then 0 is returned.
+cc: compute-tbs-hash ( id -- hashlen ) {
+ int id = T0_POPi();
+ size_t len;
+ len = br_multihash_out(&CTX->mhash, id, CTX->tbs_hash);
+ T0_PUSH(len);
+}
+
+\ Push true (-1) if no server name is expected in the EE certificate.
+cc: zero-server-name ( -- bool ) {
+ T0_PUSHi(-(CTX->server_name == NULL));
+}
+
+addr: key_usages
+addr: cert_sig
+addr: cert_sig_len
+addr: cert_signer_key_type
+addr: cert_sig_hash_oid
+addr: cert_sig_hash_len
+addr: tbs_hash
+addr: min_rsa_size
+
+\ Start TBS hash computation. The hash functions are reinitialised.
+cc: start-tbs-hash ( -- ) {
+ br_multihash_init(&CTX->mhash);
+ CTX->do_mhash = 1;
+}
+
+\ Stop TBS hash computation.
+cc: stop-tbs-hash ( -- ) {
+ CTX->do_mhash = 0;
+}
+
+\ Start DN hash computation.
+cc: start-dn-hash ( -- ) {
+ CTX->dn_hash_impl->init(&CTX->dn_hash.vtable);
+ CTX->do_dn_hash = 1;
+}
+
+\ Terminate DN hash computation and write the DN hash into the
+\ current_dn_hash buffer.
+cc: compute-dn-hash ( -- ) {
+ CTX->dn_hash_impl->out(&CTX->dn_hash.vtable, CTX->current_dn_hash);
+ CTX->do_dn_hash = 0;
+}
+
+\ Get the length of hash values obtained with the DN hasher.
+cc: dn-hash-length ( -- len ) {
+ T0_PUSH(DNHASH_LEN);
+}
+
+\ Copy data between two areas in the context.
+cc: blobcopy ( addr-dst addr-src len -- ) {
+ size_t len = T0_POP();
+ unsigned char *src = (unsigned char *)CTX + T0_POP();
+ unsigned char *dst = (unsigned char *)CTX + T0_POP();
+ memcpy(dst, src, len);
+}
+
+addr: current_dn_hash
+addr: next_dn_hash
+addr: saved_dn_hash
+
+\ Read a DN, hashing it into current_dn_hash. The DN contents are not
+\ inspected (only the outer tag, for SEQUENCE, is checked).
+: read-DN ( lim -- lim )
+ start-dn-hash
+ read-sequence-open skip-close-elt
+ compute-dn-hash ;
+
+cc: offset-name-element ( san -- n ) {
+ unsigned san = T0_POP();
+ size_t u;
+
+ for (u = 0; u < CTX->num_name_elts; u ++) {
+ if (CTX->name_elts[u].status == 0) {
+ const unsigned char *oid;
+ size_t len, off;
+
+ oid = CTX->name_elts[u].oid;
+ if (san) {
+ if (oid[0] != 0 || oid[1] != 0) {
+ continue;
+ }
+ off = 2;
+ } else {
+ off = 0;
+ }
+ len = oid[off];
+ if (len != 0 && len == CTX->pad[0]
+ && memcmp(oid + off + 1,
+ CTX->pad + 1, len) == 0)
+ {
+ T0_PUSH(u);
+ T0_RET();
+ }
+ }
+ }
+ T0_PUSHi(-1);
+}
+
+cc: copy-name-element ( bool offbuf -- ) {
+ size_t len;
+ int32_t off = T0_POPi();
+ int ok = T0_POPi();
+
+ if (off >= 0) {
+ br_name_element *ne = &CTX->name_elts[off];
+
+ if (ok) {
+ len = CTX->pad[0];
+ if (len < ne->len) {
+ memcpy(ne->buf, CTX->pad + 1, len);
+ ne->buf[len] = 0;
+ ne->status = 1;
+ } else {
+ ne->status = -1;
+ }
+ } else {
+ ne->status = -1;
+ }
+ }
+}
+
+cc: copy-name-SAN ( bool tag -- ) {
+ unsigned tag = T0_POP();
+ unsigned ok = T0_POP();
+ size_t u, len;
+
+ len = CTX->pad[0];
+ for (u = 0; u < CTX->num_name_elts; u ++) {
+ br_name_element *ne;
+
+ ne = &CTX->name_elts[u];
+ if (ne->status == 0 && ne->oid[0] == 0 && ne->oid[1] == tag) {
+ if (ok && ne->len > len) {
+ memcpy(ne->buf, CTX->pad + 1, len);
+ ne->buf[len] = 0;
+ ne->status = 1;
+ } else {
+ ne->status = -1;
+ }
+ break;
+ }
+ }
+}
+
+\ Read a value, decoding string types. If the string type is recognised
+\ and the value could be converted to UTF-8 into the pad, then true (-1)
+\ is returned; in all other cases, false (0) is returned. Either way, the
+\ object is consumed.
+: read-string ( lim -- lim bool )
+ read-tag case
+ \ UTF8String
+ 12 of check-primitive read-value-UTF8 endof
+ \ NumericString
+ 18 of check-primitive read-value-latin1 endof
+ \ PrintableString
+ 19 of check-primitive read-value-latin1 endof
+ \ TeletexString
+ 20 of check-primitive read-value-latin1 endof
+ \ IA5String
+ 22 of check-primitive read-value-latin1 endof
+ \ BMPString
+ 30 of check-primitive read-value-UTF16 endof
+ 2drop read-length-skip 0 0
+ endcase ;
+
+\ Read a DN for the EE. The normalized DN hash is computed and stored in the
+\ current_dn_hash.
+\ Name elements are gathered. Also, the Common Name is matched against the
+\ intended server name.
+\ Returned value is true (-1) if the CN matches the intended server name,
+\ false (0) otherwise.
+: read-DN-EE ( lim -- lim bool )
+ \ Flag will be set to true if there is a CN and it matches the
+ \ intended server name.
+ 0 { eename-matches }
+
+ \ Activate DN hashing.
+ start-dn-hash
+
+ \ Parse the DN structure: it is a SEQUENCE of SET of
+ \ AttributeTypeAndValue. Each AttributeTypeAndValue is a
+ \ SEQUENCE { OBJECT IDENTIFIER, ANY }.
+ read-sequence-open
+ begin
+ dup while
+
+ read-tag 0x11 check-tag-constructed read-length-open-elt
+ dup ifnot ERR_X509_BAD_DN fail then
+ begin
+ dup while
+
+ read-sequence-open
+
+ \ Read the OID. If the OID could not be read (too
+ \ long) then the first pad byte will be 0.
+ read-OID drop
+
+ \ If it is the Common Name then we'll need to
+ \ match it against the intended server name (if
+ \ applicable).
+ id-at-commonName eqOID { isCN }
+
+ \ Get offset for reception buffer for that element
+ \ (or -1).
+ 0 offset-name-element { offbuf }
+
+ \ Try to read the value as a string.
+ read-string
+
+ \ If the value could be decoded as a string,
+ \ copy it and/or match it, as appropriate.
+ dup isCN and if
+ match-server-name if
+ -1 >eename-matches
+ then
+ then
+ offbuf copy-name-element
+
+ \ Close the SEQUENCE
+ close-elt
+
+ repeat
+ close-elt
+ repeat
+ close-elt
+
+ \ Compute DN hash and deactivate DN hashing.
+ compute-dn-hash
+
+ \ Return the CN match flag.
+ eename-matches ;
+
+\ Get the validation date and time from the context or system.
+cc: get-system-date ( -- days seconds ) {
+ if (CTX->days == 0 && CTX->seconds == 0) {
+#if BR_USE_UNIX_TIME
+ time_t x = time(NULL);
+
+ T0_PUSH((uint32_t)(x / 86400) + 719528);
+ T0_PUSH((uint32_t)(x % 86400));
+#elif BR_USE_WIN32_TIME
+ FILETIME ft;
+ uint64_t x;
+
+ GetSystemTimeAsFileTime(&ft);
+ x = ((uint64_t)ft.dwHighDateTime << 32)
+ + (uint64_t)ft.dwLowDateTime;
+ x = (x / 10000000);
+ T0_PUSH((uint32_t)(x / 86400) + 584754);
+ T0_PUSH((uint32_t)(x % 86400));
+#else
+ CTX->err = BR_ERR_X509_TIME_UNKNOWN;
+ T0_CO();
+#endif
+ } else {
+ T0_PUSH(CTX->days);
+ T0_PUSH(CTX->seconds);
+ }
+}
+
+\ Compare two dates (days+seconds) together.
+: before ( days1 seconds1 days2 seconds2 -- bool )
+ { d1 s1 d2 s2 }
+ d1 d2 = if s1 s2 < else d1 d2 < then ;
+
+: after ( days1 seconds1 days2 seconds2 -- bool )
+ swap2 before ;
+
+\ Swap the top two elements with the two elements immediately below.
+: swap2 ( a b c d -- c d a b )
+ 3 roll 3 roll ;
+
+\ Match the name in the pad with the expected server name. Returned value
+\ is true (-1) on match, false (0) otherwise. If there is no expected
+\ server name, then 0 is returned.
+\ Match conditions: either an exact match (case insensitive), or a
+\ wildcard match, if the found name starts with "*.". We only match a
+\ starting wildcard, and only against a complete DN name component.
+cc: match-server-name ( -- bool ) {
+ size_t n1, n2;
+
+ if (CTX->server_name == NULL) {
+ T0_PUSH(0);
+ T0_RET();
+ }
+ n1 = strlen(CTX->server_name);
+ n2 = CTX->pad[0];
+ if (n1 == n2 && eqnocase(&CTX->pad[1], CTX->server_name, n1)) {
+ T0_PUSHi(-1);
+ T0_RET();
+ }
+ if (n2 >= 2 && CTX->pad[1] == '*' && CTX->pad[2] == '.') {
+ size_t u;
+
+ u = 0;
+ while (u < n1 && CTX->server_name[u] != '.') {
+ u ++;
+ }
+ u ++;
+ n1 -= u;
+ if ((n2 - 2) == n1
+ && eqnocase(&CTX->pad[3], CTX->server_name + u, n1))
+ {
+ T0_PUSHi(-1);
+ T0_RET();
+ }
+ }
+ T0_PUSH(0);
+}
+
+\ Get the address and length for the pkey_data buffer.
+: addr-len-pkey_data ( -- addr len )
+ CX 0 8191 { offsetof(br_x509_minimal_context, pkey_data) }
+ CX 0 8191 { BR_X509_BUFSIZE_KEY } ;
+
+\ Copy the EE public key to the permanent buffer (RSA).
+cc: copy-ee-rsa-pkey ( nlen elen -- ) {
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ memcpy(CTX->ee_pkey_data, CTX->pkey_data, nlen + elen);
+ CTX->pkey.key_type = BR_KEYTYPE_RSA;
+ CTX->pkey.key.rsa.n = CTX->ee_pkey_data;
+ CTX->pkey.key.rsa.nlen = nlen;
+ CTX->pkey.key.rsa.e = CTX->ee_pkey_data + nlen;
+ CTX->pkey.key.rsa.elen = elen;
+}
+
+\ Copy the EE public key to the permanent buffer (EC).
+cc: copy-ee-ec-pkey ( curve qlen -- ) {
+ size_t qlen = T0_POP();
+ uint32_t curve = T0_POP();
+ memcpy(CTX->ee_pkey_data, CTX->pkey_data, qlen);
+ CTX->pkey.key_type = BR_KEYTYPE_EC;
+ CTX->pkey.key.ec.curve = curve;
+ CTX->pkey.key.ec.q = CTX->ee_pkey_data;
+ CTX->pkey.key.ec.qlen = qlen;
+}
+
+\ Check whether the current certificate (EE) is directly trusted.
+cc: check-direct-trust ( -- ) {
+ size_t u;
+
+ for (u = 0; u < CTX->trust_anchors_num; u ++) {
+ const br_x509_trust_anchor *ta;
+ unsigned char hashed_DN[64];
+ int kt;
+
+ ta = &CTX->trust_anchors[u];
+ if (ta->flags & BR_X509_TA_CA) {
+ continue;
+ }
+ hash_dn(CTX, ta->dn.data, ta->dn.len, hashed_DN);
+ if (memcmp(hashed_DN, CTX->current_dn_hash, DNHASH_LEN)) {
+ continue;
+ }
+ kt = CTX->pkey.key_type;
+ if ((ta->pkey.key_type & 0x0F) != kt) {
+ continue;
+ }
+ switch (kt) {
+
+ case BR_KEYTYPE_RSA:
+ if (!eqbigint(CTX->pkey.key.rsa.n,
+ CTX->pkey.key.rsa.nlen,
+ ta->pkey.key.rsa.n,
+ ta->pkey.key.rsa.nlen)
+ || !eqbigint(CTX->pkey.key.rsa.e,
+ CTX->pkey.key.rsa.elen,
+ ta->pkey.key.rsa.e,
+ ta->pkey.key.rsa.elen))
+ {
+ continue;
+ }
+ break;
+
+ case BR_KEYTYPE_EC:
+ if (CTX->pkey.key.ec.curve != ta->pkey.key.ec.curve
+ || CTX->pkey.key.ec.qlen != ta->pkey.key.ec.qlen
+ || memcmp(CTX->pkey.key.ec.q,
+ ta->pkey.key.ec.q,
+ ta->pkey.key.ec.qlen) != 0)
+ {
+ continue;
+ }
+ break;
+
+ default:
+ continue;
+ }
+
+ /*
+ * Direct trust match!
+ */
+ CTX->err = BR_ERR_X509_OK;
+ T0_CO();
+ }
+}
+
+\ Check the signature on the certificate with regards to all trusted CA.
+\ We use the issuer hash (in saved_dn_hash[]) as CA identifier.
+cc: check-trust-anchor-CA ( -- ) {
+ size_t u;
+
+ for (u = 0; u < CTX->trust_anchors_num; u ++) {
+ const br_x509_trust_anchor *ta;
+ unsigned char hashed_DN[64];
+
+ ta = &CTX->trust_anchors[u];
+ if (!(ta->flags & BR_X509_TA_CA)) {
+ continue;
+ }
+ hash_dn(CTX, ta->dn.data, ta->dn.len, hashed_DN);
+ if (memcmp(hashed_DN, CTX->saved_dn_hash, DNHASH_LEN)) {
+ continue;
+ }
+ if (verify_signature(CTX, &ta->pkey) == 0) {
+ CTX->err = BR_ERR_X509_OK;
+ T0_CO();
+ }
+ }
+}
+
+\ Verify RSA signature. This uses the public key that was just decoded
+\ into CTX->pkey_data; the modulus and exponent length are provided as
+\ parameters. The resulting hash value is compared with the one in
+\ tbs_hash. Returned value is 0 on success, or a non-zero error code.
+cc: do-rsa-vrfy ( nlen elen -- err ) {
+ size_t elen = T0_POP();
+ size_t nlen = T0_POP();
+ br_x509_pkey pk;
+
+ pk.key_type = BR_KEYTYPE_RSA;
+ pk.key.rsa.n = CTX->pkey_data;
+ pk.key.rsa.nlen = nlen;
+ pk.key.rsa.e = CTX->pkey_data + nlen;
+ pk.key.rsa.elen = elen;
+ T0_PUSH(verify_signature(CTX, &pk));
+}
+
+\ Verify ECDSA signature. This uses the public key that was just decoded
+\ into CTX->pkey_dayta; the curve ID and public point length are provided
+\ as parameters. The hash value in tbs_hash is used. Returned value is 0
+\ on success, or non-zero error code.
+cc: do-ecdsa-vrfy ( curve qlen -- err ) {
+ size_t qlen = T0_POP();
+ int curve = T0_POP();
+ br_x509_pkey pk;
+
+ pk.key_type = BR_KEYTYPE_EC;
+ pk.key.ec.curve = curve;
+ pk.key.ec.q = CTX->pkey_data;
+ pk.key.ec.qlen = qlen;
+ T0_PUSH(verify_signature(CTX, &pk));
+}
+
+cc: print-bytes ( addr len -- ) {
+ extern int printf(const char *fmt, ...);
+ size_t len = T0_POP();
+ unsigned char *buf = (unsigned char *)CTX + T0_POP();
+ size_t u;
+
+ for (u = 0; u < len; u ++) {
+ printf("%02X", buf[u]);
+ }
+}
+
+cc: printOID ( -- ) {
+ extern int printf(const char *fmt, ...);
+ size_t u, len;
+
+ len = CTX->pad[0];
+ if (len == 0) {
+ printf("*");
+ T0_RET();
+ }
+ printf("%u.%u", CTX->pad[1] / 40, CTX->pad[1] % 40);
+ u = 2;
+ while (u <= len) {
+ unsigned long ul;
+
+ ul = 0;
+ for (;;) {
+ int x;
+
+ if (u > len) {
+ printf("BAD");
+ T0_RET();
+ }
+ x = CTX->pad[u ++];
+ ul = (ul << 7) + (x & 0x7F);
+ if (!(x & 0x80)) {
+ break;
+ }
+ }
+ printf(".%lu", ul);
+ }
+}
+
+\ Extensions with specific processing.
+OID: basicConstraints 2.5.29.19
+OID: keyUsage 2.5.29.15
+OID: subjectAltName 2.5.29.17
+OID: certificatePolicies 2.5.29.32
+
+\ Policy qualifier "pointer to CPS"
+OID: id-qt-cps 1.3.6.1.5.5.7.2.1
+
+\ Extensions which are ignored when encountered, even if critical.
+OID: authorityKeyIdentifier 2.5.29.35
+OID: subjectKeyIdentifier 2.5.29.14
+OID: issuerAltName 2.5.29.18
+OID: subjectDirectoryAttributes 2.5.29.9
+OID: crlDistributionPoints 2.5.29.31
+OID: freshestCRL 2.5.29.46
+OID: authorityInfoAccess 1.3.6.1.5.5.7.1.1
+OID: subjectInfoAccess 1.3.6.1.5.5.7.1.11
+
+\ Process a Basic Constraints extension. This should be called only if
+\ the certificate is not the EE. We check that the extension contains
+\ the "CA" flag, and that the path length, if specified, is compatible
+\ with the current chain length.
+: process-basicConstraints ( lim -- lim )
+ read-sequence-open
+ read-tag-or-end
+ dup 0x01 = if
+ read-boolean ifnot ERR_X509_NOT_CA fail then
+ read-tag-or-end
+ else
+ ERR_X509_NOT_CA fail
+ then
+ dup 0x02 = if
+ drop check-primitive read-small-int-value
+ addr-num_certs get32 1- < if ERR_X509_NOT_CA fail then
+ read-tag-or-end
+ then
+ -1 <> if ERR_X509_UNEXPECTED fail then
+ drop
+ close-elt
+ ;
+
+\ Process a Key Usage extension.
+\ For the EE certificate:
+\ -- if the key usage contains keyEncipherment (2), dataEncipherment (3)
+\ or keyAgreement (4), then the "key exchange" usage is allowed;
+\ -- if the key usage contains digitalSignature (0) or nonRepudiation (1),
+\ then the "signature" usage is allowed.
+\ For CA certificates, the extension must contain keyCertSign (5).
+: process-keyUsage ( lim ee -- lim )
+ { ee }
+
+ \ Read tag for the BIT STRING and open it.
+ read-tag 0x03 check-tag-primitive
+ read-length-open-elt
+ \ First byte indicates number of ignored bits in the last byte. It
+ \ must be between 0 and 7.
+ read8 { ign }
+ ign 7 > if ERR_X509_UNEXPECTED fail then
+ \ Depending on length, we have either 0, 1 or more bytes to read.
+ dup case
+ 0 of ERR_X509_FORBIDDEN_KEY_USAGE fail endof
+ 1 of read8 ign >> ign << endof
+ drop read8 0
+ endcase
+
+ \ Check bits.
+ ee if
+ \ EE: get usages.
+ 0
+ over 0x38 and if 0x10 or then
+ swap 0xC0 and if 0x20 or then
+ addr-key_usages set8
+ else
+ \ Not EE: keyCertSign must be set.
+ 0x04 and ifnot ERR_X509_FORBIDDEN_KEY_USAGE fail then
+ then
+
+ \ We don't care about subsequent bytes.
+ skip-close-elt ;
+
+\ Process a Certificate Policies extension.
+\
+\ Since we don't actually support full policies processing, this function
+\ only checks that the extension contents can be safely ignored. Indeed,
+\ we don't validate against a specific set of policies (in RFC 5280
+\ terminology, user-initial-policy-set only contains the special value
+\ any-policy). Moreover, we don't support policy constraints (if a
+\ critical Policy Constraints extension is encountered, the validation
+\ will fail). Therefore, we can safely ignore the contents of this
+\ extension, except if it is critical AND one of the policy OID has a
+\ qualifier which is distinct from id-qt-cps (because id-qt-cps is
+\ specially designated by RFC 5280 has having no mandated action).
+\
+\ This function is called only if the extension is critical.
+: process-certPolicies ( lim -- lim )
+ \ Extension value is a SEQUENCE OF PolicyInformation.
+ read-sequence-open
+ begin dup while
+ \ PolicyInformation ::= SEQUENCE {
+ \ policyIdentifier OBJECT IDENTIFIER,
+ \ policyQualifiers SEQUENCE OF PolicyQualifierInfo OPTIONAL
+ \ }
+ read-sequence-open
+ read-OID drop
+ dup if
+ read-sequence-open
+ begin dup while
+ \ PolicyQualifierInfo ::= SEQUENCE {
+ \ policyQualifierId OBJECT IDENTIFIER,
+ \ qualifier ANY
+ \ }
+ read-sequence-open
+ read-OID drop id-qt-cps eqOID ifnot
+ ERR_X509_CRITICAL_EXTENSION fail
+ then
+ skip-close-elt
+ repeat
+ close-elt
+ then
+ close-elt
+ repeat
+ close-elt ;
+
+\ Process a Subject Alt Name extension. Returned value is a boolean set
+\ to true if the expected server name was matched against a dNSName in
+\ the extension.
+: process-SAN ( lim -- lim bool )
+ 0 { m }
+ read-sequence-open
+ begin dup while
+ \ Read the tag. If the tag is context-0, then parse an
+ \ 'otherName'. If the tag is context-2, then parse a
+ \ dNSName. If the tag is context-1 or context-6,
+ \ parse
+ read-tag case
+ \ OtherName
+ 0x20 of
+ \ OtherName ::= SEQUENCE {
+ \ type-id OBJECT IDENTIFIER,
+ \ value [0] EXPLICIT ANY
+ \ }
+ check-constructed read-length-open-elt
+ read-OID drop
+ -1 offset-name-element { offbuf }
+ read-tag 0x20 check-tag-constructed
+ read-length-open-elt
+ read-string offbuf copy-name-element
+ close-elt
+ close-elt
+ endof
+ \ rfc822Name (IA5String)
+ 0x21 of
+ check-primitive
+ read-value-UTF8 1 copy-name-SAN
+ endof
+ \ dNSName (IA5String)
+ 0x22 of
+ check-primitive
+ read-value-UTF8
+ dup if match-server-name m or >m then
+ 2 copy-name-SAN
+ endof
+ \ uniformResourceIdentifier (IA5String)
+ 0x26 of
+ check-primitive
+ read-value-UTF8 6 copy-name-SAN
+ endof
+ 2drop read-length-skip 0
+ endcase
+
+ \ We check only names of type dNSName; they use IA5String,
+ \ which is basically ASCII.
+ \ read-tag 0x22 = if
+ \ check-primitive
+ \ read-small-value drop
+ \ match-server-name m or >m
+ \ else
+ \ drop read-length-skip
+ \ then
+ repeat
+ close-elt
+ m ;
+
+\ Decode a certificate. The "ee" boolean must be true for the EE.
+: decode-certificate ( ee -- )
+ { ee }
+
+ \ Obtain the total certificate length.
+ addr-cert_length get32
+
+ \ Open the outer SEQUENCE.
+ read-sequence-open
+
+ \ TBS
+ \ Activate hashing.
+ start-tbs-hash
+ read-sequence-open
+
+ \ First element may be an explicit version. We accept only
+ \ versions 0 to 2 (certificates v1 to v3).
+ read-tag dup 0x20 = if
+ drop check-constructed read-length-open-elt
+ read-tag
+ 0x02 check-tag-primitive
+ read-small-int-value
+ 2 > if ERR_X509_UNSUPPORTED fail then
+ close-elt
+ read-tag
+ then
+
+ \ Serial number. We just check that the tag is correct.
+ 0x02 check-tag-primitive
+ read-length-skip
+
+ \ Signature algorithm. This structure is redundant with the one
+ \ on the outside; we just skip it.
+ read-sequence-open skip-close-elt
+
+ \ Issuer name: hashed, then copied into next_dn_hash[].
+ read-DN
+ addr-next_dn_hash addr-current_dn_hash dn-hash-length blobcopy
+
+ \ Validity dates.
+ read-sequence-open
+ read-date get-system-date after if ERR_X509_EXPIRED fail then
+ read-date get-system-date before if ERR_X509_EXPIRED fail then
+ close-elt
+
+ \ Subject name.
+ ee if
+ \ For the EE, we must check whether the Common Name, if
+ \ any, matches the expected server name.
+ read-DN-EE { eename }
+ else
+ \ For a non-EE certificate, the hashed subject DN must match
+ \ the saved hashed issuer DN from the previous certificate.
+ read-DN
+ addr-current_dn_hash addr-saved_dn_hash dn-hash-length eqblob
+ ifnot ERR_X509_DN_MISMATCH fail then
+ then
+ \ Move the hashed issuer DN for this certificate into the
+ \ saved_dn_hash[] array.
+ addr-saved_dn_hash addr-next_dn_hash dn-hash-length blobcopy
+
+ \ Public Key.
+ read-sequence-open
+ \ Algorithm Identifier. Right now we are only interested in the
+ \ OID, since we only support RSA keys.
+ read-sequence-open
+ read-OID ifnot ERR_X509_UNSUPPORTED fail then
+ { ; pkey-type }
+ choice
+ \ RSA public key.
+ rsaEncryption eqOID uf
+ skip-close-elt
+ \ Public key itself: the BIT STRING contains bytes
+ \ (no partial byte) and these bytes encode the
+ \ actual value.
+ read-bits-open
+ \ RSA public key is a SEQUENCE of two
+ \ INTEGER. We get both INTEGER values into
+ \ the pkey_data[] buffer, if they fit.
+ read-sequence-open
+ addr-len-pkey_data
+ read-integer { nlen }
+ addr-len-pkey_data swap nlen + swap nlen -
+ read-integer { elen }
+ close-elt
+
+ \ Check that the public key fits our minimal
+ \ size requirements. Note that the integer
+ \ decoder already skipped the leading bytes
+ \ of value 0, so we are working on the true
+ \ modulus length here.
+ addr-min_rsa_size get16 128 + nlen > if
+ ERR_X509_WEAK_PUBLIC_KEY fail
+ then
+ close-elt
+ KEYTYPE_RSA >pkey-type
+ enduf
+
+ \ EC public key.
+ id-ecPublicKey eqOID uf
+ \ We support only named curves, for which the
+ \ "parameters" field in the AlgorithmIdentifier
+ \ field should be an OID.
+ read-OID ifnot ERR_X509_UNSUPPORTED fail then
+ choice
+ ansix9p256r1 eqOID uf 23 enduf
+ ansix9p384r1 eqOID uf 24 enduf
+ ansix9p521r1 eqOID uf 25 enduf
+ ERR_X509_UNSUPPORTED fail
+ endchoice
+ { curve }
+ close-elt
+ read-bits-open
+ dup { qlen }
+ dup addr-len-pkey_data rot < if
+ ERR_X509_LIMIT_EXCEEDED fail
+ then
+ read-blob
+ KEYTYPE_EC >pkey-type
+ enduf
+
+ \ Not a recognised public key type.
+ ERR_X509_UNSUPPORTED fail
+ endchoice
+ close-elt
+
+ \ Process public key.
+ ee if
+ \ For the EE certificate, copy the key data to the
+ \ relevant buffer.
+ pkey-type case
+ KEYTYPE_RSA of nlen elen copy-ee-rsa-pkey endof
+ KEYTYPE_EC of curve qlen copy-ee-ec-pkey endof
+ ERR_X509_UNSUPPORTED fail
+ endcase
+ else
+ \ Verify signature on previous certificate. We invoke
+ \ the RSA implementation.
+ pkey-type case
+ KEYTYPE_RSA of nlen elen do-rsa-vrfy endof
+ KEYTYPE_EC of curve qlen do-ecdsa-vrfy endof
+ ERR_X509_UNSUPPORTED fail
+ endcase
+ dup if fail then
+ drop
+ then
+
+ \ This flag will be set to true if the Basic Constraints extension
+ \ is encountered.
+ 0 { seenBC }
+
+ \ Skip issuerUniqueID and subjectUniqueID, and process extensions
+ \ if present. Extensions are an explicit context tag of value 3
+ \ around a SEQUENCE OF extensions. Each extension is a SEQUENCE
+ \ with an OID, an optional boolean, and a value; the value is
+ \ an OCTET STRING.
+ read-tag-or-end
+ 0x21 iftag-skip
+ 0x22 iftag-skip
+ dup 0x23 = if
+ drop
+ check-constructed read-length-open-elt
+ read-sequence-open
+ begin dup while
+ 0 { critical }
+ read-sequence-open
+ read-OID drop
+ read-tag dup 0x01 = if
+ read-boolean >critical
+ read-tag
+ then
+ 0x04 check-tag-primitive read-length-open-elt
+ choice
+ \ Extensions with specific processing.
+ basicConstraints eqOID uf
+ ee if
+ skip-remaining
+ else
+ process-basicConstraints
+ -1 >seenBC
+ then
+ enduf
+ keyUsage eqOID uf
+ ee process-keyUsage
+ enduf
+ subjectAltName eqOID uf
+ ee if
+ 0 >eename
+ process-SAN >eename
+ else
+ skip-remaining
+ then
+ enduf
+
+ \ We don't implement full processing of
+ \ policies. The call below mostly checks
+ \ that the contents of the Certificate
+ \ Policies extension can be safely ignored.
+ certificatePolicies eqOID uf
+ critical if
+ process-certPolicies
+ else
+ skip-remaining
+ then
+ enduf
+
+ \ Extensions which are always ignored,
+ \ even if critical.
+ authorityKeyIdentifier eqOID uf
+ skip-remaining
+ enduf
+ subjectKeyIdentifier eqOID uf
+ skip-remaining
+ enduf
+ issuerAltName eqOID uf
+ skip-remaining
+ enduf
+ subjectDirectoryAttributes eqOID uf
+ skip-remaining
+ enduf
+ crlDistributionPoints eqOID uf
+ skip-remaining
+ enduf
+ freshestCRL eqOID uf
+ skip-remaining
+ enduf
+ authorityInfoAccess eqOID uf
+ skip-remaining
+ enduf
+ subjectInfoAccess eqOID uf
+ skip-remaining
+ enduf
+
+ \ Unrecognized extensions trigger a failure
+ \ if critical; otherwise, they are just
+ \ ignored.
+ critical if
+ ERR_X509_CRITICAL_EXTENSION fail
+ then
+ skip-remaining
+ endchoice
+ close-elt
+ close-elt
+ repeat
+ close-elt
+ close-elt
+ else
+ -1 = ifnot ERR_X509_UNEXPECTED fail then
+ drop
+ then
+
+ close-elt
+ \ Terminate hashing.
+ stop-tbs-hash
+
+ \ For the EE certificate, verify that the intended server name
+ \ was matched.
+ ee if
+ eename zero-server-name or ifnot
+ ERR_X509_BAD_SERVER_NAME fail
+ then
+ then
+
+ \ If this is the EE certificate, then direct trust may apply.
+ \ Note: we do this at this point, not immediately after decoding
+ \ the public key, because even in case of direct trust we still
+ \ want to check the server name with regards to the SAN extension.
+ \ However, we want to check direct trust before trying to decode
+ \ the signature algorithm, because it should work even if that
+ \ algorithm is not supported.
+ ee if check-direct-trust then
+
+ \ Non-EE certificates MUST have a Basic Constraints extension
+ \ (that marks them as being CA).
+ ee seenBC or ifnot ERR_X509_NOT_CA fail then
+
+ \ signature algorithm
+ read-tag check-sequence read-length-open-elt
+ \ Read and understand the OID. Right now, we support only
+ \ RSA with PKCS#1 v1.5 padding, and hash functions SHA-1,
+ \ SHA-224, SHA-256, SHA-384 and SHA-512. We purposely do NOT
+ \ support MD5 here.
+ \ TODO: add support for RSA/PSS
+ read-OID if
+ \ Based on the signature OID, we get:
+ \ -- the signing key type
+ \ -- the hash function numeric identifier
+ \ -- the hash function OID
+ choice
+ sha1WithRSAEncryption eqOID
+ uf 2 KEYTYPE_RSA id-sha1 enduf
+ sha224WithRSAEncryption eqOID
+ uf 3 KEYTYPE_RSA id-sha224 enduf
+ sha256WithRSAEncryption eqOID
+ uf 4 KEYTYPE_RSA id-sha256 enduf
+ sha384WithRSAEncryption eqOID
+ uf 5 KEYTYPE_RSA id-sha384 enduf
+ sha512WithRSAEncryption eqOID
+ uf 6 KEYTYPE_RSA id-sha512 enduf
+
+ ecdsa-with-SHA1 eqOID
+ uf 2 KEYTYPE_EC id-sha1 enduf
+ ecdsa-with-SHA224 eqOID
+ uf 3 KEYTYPE_EC id-sha224 enduf
+ ecdsa-with-SHA256 eqOID
+ uf 4 KEYTYPE_EC id-sha256 enduf
+ ecdsa-with-SHA384 eqOID
+ uf 5 KEYTYPE_EC id-sha384 enduf
+ ecdsa-with-SHA512 eqOID
+ uf 6 KEYTYPE_EC id-sha512 enduf
+ ERR_X509_UNSUPPORTED fail
+ endchoice
+ addr-cert_sig_hash_oid set16
+ addr-cert_signer_key_type set8
+
+ \ Compute the TBS hash into tbs_hash.
+ compute-tbs-hash
+ dup ifnot ERR_X509_UNSUPPORTED fail then
+ addr-cert_sig_hash_len set8
+ else
+ ERR_X509_UNSUPPORTED fail
+ then
+ \ We ignore the parameters, whether they are present or not,
+ \ because we got all the information from the OID.
+ skip-close-elt
+
+ \ signature value
+ read-bits-open
+ dup CX 0 8191 { BR_X509_BUFSIZE_SIG } > if
+ ERR_X509_LIMIT_EXCEEDED fail
+ then
+ dup addr-cert_sig_len set16
+ addr-cert_sig read-blob
+
+ \ Close the outer SEQUENCE.
+ close-elt
+
+ \ Close the advertised total certificate length. This checks that
+ \ there is no trailing garbage after the certificate.
+ close-elt
+
+ \ Flag the certificate as fully processed.
+ 0 addr-cert_length set32
+
+ \ Check whether the issuer for the current certificate is known
+ \ as a trusted CA; in which case, verify the signature.
+ check-trust-anchor-CA ;
+
+: main
+ \ Unless restricted by a Key Usage extension, all usages are
+ \ deemed allowed.
+ 0x30 addr-key_usages set8
+ -1 decode-certificate
+ co
+ begin
+ 0 decode-certificate co
+ again
+ ;
diff --git a/test/monniaux/BearSSL/src/x509/x509_minimal_full.c b/test/monniaux/BearSSL/src/x509/x509_minimal_full.c
new file mode 100644
index 00000000..2b544267
--- /dev/null
+++ b/test/monniaux/BearSSL/src/x509/x509_minimal_full.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "inner.h"
+
+/* see bearssl_x509.h */
+void
+br_x509_minimal_init_full(br_x509_minimal_context *xc,
+ const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num)
+{
+ /*
+ * All hash functions are activated.
+ * Note: the X.509 validation engine will nonetheless refuse to
+ * validate signatures that use MD5 as hash function.
+ */
+ static const br_hash_class *hashes[] = {
+ &br_md5_vtable,
+ &br_sha1_vtable,
+ &br_sha224_vtable,
+ &br_sha256_vtable,
+ &br_sha384_vtable,
+ &br_sha512_vtable
+ };
+
+ int id;
+
+ br_x509_minimal_init(xc, &br_sha256_vtable,
+ trust_anchors, trust_anchors_num);
+ br_x509_minimal_set_rsa(xc, &br_rsa_i31_pkcs1_vrfy);
+ br_x509_minimal_set_ecdsa(xc,
+ &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
+ for (id = br_md5_ID; id <= br_sha512_ID; id ++) {
+ const br_hash_class *hc;
+
+ hc = hashes[id - 1];
+ br_x509_minimal_set_hash(xc, id, hc);
+ }
+}
diff --git a/test/monniaux/BearSSL/test/test_crypto.c b/test/monniaux/BearSSL/test/test_crypto.c
new file mode 100644
index 00000000..a4f33ac0
--- /dev/null
+++ b/test/monniaux/BearSSL/test/test_crypto.c
@@ -0,0 +1,9480 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "bearssl.h"
+#include "inner.h"
+#include "../../clock.h"
+
+/*
+ * Decode an hexadecimal string. Returned value is the number of decoded
+ * bytes.
+ */
+static size_t
+hextobin(unsigned char *dst, const char *src)
+{
+ size_t num;
+ unsigned acc;
+ int z;
+
+ num = 0;
+ z = 0;
+ acc = 0;
+ while (*src != 0) {
+ int c = *src ++;
+ if (c >= '0' && c <= '9') {
+ c -= '0';
+ } else if (c >= 'A' && c <= 'F') {
+ c -= ('A' - 10);
+ } else if (c >= 'a' && c <= 'f') {
+ c -= ('a' - 10);
+ } else {
+ continue;
+ }
+ if (z) {
+ *dst ++ = (acc << 4) + c;
+ num ++;
+ } else {
+ acc = c;
+ }
+ z = !z;
+ }
+ return num;
+}
+
+static void
+check_equals(const char *banner, const void *v1, const void *v2, size_t len)
+{
+ size_t u;
+ const unsigned char *b;
+
+ if (memcmp(v1, v2, len) == 0) {
+ return;
+ }
+ fprintf(stderr, "\n%s failed\n", banner);
+ fprintf(stderr, "v1: ");
+ for (u = 0, b = v1; u < len; u ++) {
+ fprintf(stderr, "%02X", b[u]);
+ }
+ fprintf(stderr, "\nv2: ");
+ for (u = 0, b = v2; u < len; u ++) {
+ fprintf(stderr, "%02X", b[u]);
+ }
+ fprintf(stderr, "\n");
+ exit(EXIT_FAILURE);
+}
+
+#define HASH_SIZE(cname) br_ ## cname ## _SIZE
+
+#define TEST_HASH(Name, cname) \
+static void \
+test_ ## cname ## _internal(char *data, char *refres) \
+{ \
+ br_ ## cname ## _context mc; \
+ unsigned char res[HASH_SIZE(cname)], ref[HASH_SIZE(cname)]; \
+ size_t u, n; \
+ \
+ hextobin(ref, refres); \
+ n = strlen(data); \
+ br_ ## cname ## _init(&mc); \
+ br_ ## cname ## _update(&mc, data, n); \
+ br_ ## cname ## _out(&mc, res); \
+ check_equals("KAT " #Name " 1", res, ref, HASH_SIZE(cname)); \
+ br_ ## cname ## _init(&mc); \
+ for (u = 0; u < n; u ++) { \
+ br_ ## cname ## _update(&mc, data + u, 1); \
+ } \
+ br_ ## cname ## _out(&mc, res); \
+ check_equals("KAT " #Name " 2", res, ref, HASH_SIZE(cname)); \
+ for (u = 0; u < n; u ++) { \
+ br_ ## cname ## _context mc2; \
+ br_ ## cname ## _init(&mc); \
+ br_ ## cname ## _update(&mc, data, u); \
+ mc2 = mc; \
+ br_ ## cname ## _update(&mc, data + u, n - u); \
+ br_ ## cname ## _out(&mc, res); \
+ check_equals("KAT " #Name " 3", res, ref, HASH_SIZE(cname)); \
+ br_ ## cname ## _update(&mc2, data + u, n - u); \
+ br_ ## cname ## _out(&mc2, res); \
+ check_equals("KAT " #Name " 4", res, ref, HASH_SIZE(cname)); \
+ } \
+ memset(&mc, 0, sizeof mc); \
+ memset(res, 0, sizeof res); \
+ br_ ## cname ## _vtable.init(&mc.vtable); \
+ mc.vtable->update(&mc.vtable, data, n); \
+ mc.vtable->out(&mc.vtable, res); \
+ check_equals("KAT " #Name " 5", res, ref, HASH_SIZE(cname)); \
+ memset(res, 0, sizeof res); \
+ mc.vtable->init(&mc.vtable); \
+ mc.vtable->update(&mc.vtable, data, n); \
+ mc.vtable->out(&mc.vtable, res); \
+ check_equals("KAT " #Name " 6", res, ref, HASH_SIZE(cname)); \
+}
+
+#define KAT_MILLION_A(Name, cname, refres) do { \
+ br_ ## cname ## _context mc; \
+ unsigned char buf[1000]; \
+ unsigned char res[HASH_SIZE(cname)], ref[HASH_SIZE(cname)]; \
+ int i; \
+ \
+ hextobin(ref, refres); \
+ memset(buf, 'a', sizeof buf); \
+ br_ ## cname ## _init(&mc); \
+ for (i = 0; i < 1000; i ++) { \
+ br_ ## cname ## _update(&mc, buf, sizeof buf); \
+ } \
+ br_ ## cname ## _out(&mc, res); \
+ check_equals("KAT " #Name " 5", res, ref, HASH_SIZE(cname)); \
+ } while (0)
+
+TEST_HASH(MD5, md5)
+TEST_HASH(SHA-1, sha1)
+TEST_HASH(SHA-224, sha224)
+TEST_HASH(SHA-256, sha256)
+TEST_HASH(SHA-384, sha384)
+TEST_HASH(SHA-512, sha512)
+
+static void
+test_MD5(void)
+{
+ printf("Test MD5: ");
+ fflush(stdout);
+ test_md5_internal("", "d41d8cd98f00b204e9800998ecf8427e");
+ test_md5_internal("a", "0cc175b9c0f1b6a831c399e269772661");
+ test_md5_internal("abc", "900150983cd24fb0d6963f7d28e17f72");
+ test_md5_internal("message digest", "f96b697d7cb7938d525a2f31aaf161d0");
+ test_md5_internal("abcdefghijklmnopqrstuvwxyz",
+ "c3fcd3d76192e4007dfb496cca67e13b");
+ test_md5_internal("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu"
+ "vwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f");
+ test_md5_internal("1234567890123456789012345678901234567890123456789"
+ "0123456789012345678901234567890",
+ "57edf4a22be3c955ac49da2e2107b67a");
+ KAT_MILLION_A(MD5, md5,
+ "7707d6ae4e027c70eea2a935c2296f21");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_SHA1(void)
+{
+ printf("Test SHA-1: ");
+ fflush(stdout);
+ test_sha1_internal("abc", "a9993e364706816aba3e25717850c26c9cd0d89d");
+ test_sha1_internal("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlm"
+ "nomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
+
+ KAT_MILLION_A(SHA-1, sha1,
+ "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_SHA224(void)
+{
+ printf("Test SHA-224: ");
+ fflush(stdout);
+ test_sha224_internal("abc",
+ "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7");
+ test_sha224_internal("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlm"
+ "nomnopnopq",
+ "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525");
+
+ KAT_MILLION_A(SHA-224, sha224,
+ "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_SHA256(void)
+{
+ printf("Test SHA-256: ");
+ fflush(stdout);
+ test_sha256_internal("abc",
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+ test_sha256_internal("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlm"
+ "nomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
+
+ KAT_MILLION_A(SHA-256, sha256,
+ "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_SHA384(void)
+{
+ printf("Test SHA-384: ");
+ fflush(stdout);
+ test_sha384_internal("abc",
+ "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"
+ "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");
+ test_sha384_internal(
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+ "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"
+ "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039");
+
+ KAT_MILLION_A(SHA-384, sha384,
+ "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"
+ "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_SHA512(void)
+{
+ printf("Test SHA-512: ");
+ fflush(stdout);
+ test_sha512_internal("abc",
+ "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
+ "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
+ test_sha512_internal(
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+ "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
+ "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");
+
+ KAT_MILLION_A(SHA-512, sha512,
+ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"
+ "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_MD5_SHA1(void)
+{
+ unsigned char buf[500], out[36], outM[16], outS[20];
+ unsigned char seed[1];
+ br_hmac_drbg_context rc;
+ br_md5_context mc;
+ br_sha1_context sc;
+ br_md5sha1_context cc;
+ size_t u;
+
+ printf("Test MD5+SHA-1: ");
+ fflush(stdout);
+
+ seed[0] = 0;
+ br_hmac_drbg_init(&rc, &br_sha256_vtable, seed, sizeof seed);
+ for (u = 0; u < sizeof buf; u ++) {
+ size_t v;
+
+ br_hmac_drbg_generate(&rc, buf, u);
+ br_md5_init(&mc);
+ br_md5_update(&mc, buf, u);
+ br_md5_out(&mc, outM);
+ br_sha1_init(&sc);
+ br_sha1_update(&sc, buf, u);
+ br_sha1_out(&sc, outS);
+ br_md5sha1_init(&cc);
+ br_md5sha1_update(&cc, buf, u);
+ br_md5sha1_out(&cc, out);
+ check_equals("MD5+SHA-1 [1]", out, outM, 16);
+ check_equals("MD5+SHA-1 [2]", out + 16, outS, 20);
+ br_md5sha1_init(&cc);
+ for (v = 0; v < u; v ++) {
+ br_md5sha1_update(&cc, buf + v, 1);
+ }
+ br_md5sha1_out(&cc, out);
+ check_equals("MD5+SHA-1 [3]", out, outM, 16);
+ check_equals("MD5+SHA-1 [4]", out + 16, outS, 20);
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+/*
+ * Compute a hash function, on some data, by ID. Returned value is
+ * hash output length.
+ */
+static size_t
+do_hash(int id, const void *data, size_t len, void *out)
+{
+ br_md5_context cmd5;
+ br_sha1_context csha1;
+ br_sha224_context csha224;
+ br_sha256_context csha256;
+ br_sha384_context csha384;
+ br_sha512_context csha512;
+
+ switch (id) {
+ case br_md5_ID:
+ br_md5_init(&cmd5);
+ br_md5_update(&cmd5, data, len);
+ br_md5_out(&cmd5, out);
+ return 16;
+ case br_sha1_ID:
+ br_sha1_init(&csha1);
+ br_sha1_update(&csha1, data, len);
+ br_sha1_out(&csha1, out);
+ return 20;
+ case br_sha224_ID:
+ br_sha224_init(&csha224);
+ br_sha224_update(&csha224, data, len);
+ br_sha224_out(&csha224, out);
+ return 28;
+ case br_sha256_ID:
+ br_sha256_init(&csha256);
+ br_sha256_update(&csha256, data, len);
+ br_sha256_out(&csha256, out);
+ return 32;
+ case br_sha384_ID:
+ br_sha384_init(&csha384);
+ br_sha384_update(&csha384, data, len);
+ br_sha384_out(&csha384, out);
+ return 48;
+ case br_sha512_ID:
+ br_sha512_init(&csha512);
+ br_sha512_update(&csha512, data, len);
+ br_sha512_out(&csha512, out);
+ return 64;
+ default:
+ fprintf(stderr, "Uknown hash function: %d\n", id);
+ exit(EXIT_FAILURE);
+ return 0;
+ }
+}
+
+/*
+ * Tests for a multihash. Returned value should be 258 multiplied by the
+ * number of hash functions implemented by the context.
+ */
+static int
+test_multihash_inner(br_multihash_context *mc)
+{
+ /*
+ * Try hashing messages for all lengths from 0 to 257 bytes
+ * (inclusive). Each attempt is done twice, with data input
+ * either in one go, or byte by byte. In the byte by byte
+ * test, intermediate result are obtained and checked.
+ */
+ size_t len;
+ unsigned char buf[258];
+ int i;
+ int tcount;
+
+ tcount = 0;
+ for (len = 0; len < sizeof buf; len ++) {
+ br_sha1_context sc;
+ unsigned char tmp[20];
+
+ br_sha1_init(&sc);
+ br_sha1_update(&sc, buf, len);
+ br_sha1_out(&sc, tmp);
+ buf[len] = tmp[0];
+ }
+ for (len = 0; len <= 257; len ++) {
+ size_t u;
+
+ br_multihash_init(mc);
+ br_multihash_update(mc, buf, len);
+ for (i = 1; i <= 6; i ++) {
+ unsigned char tmp[64], tmp2[64];
+ size_t olen, olen2;
+
+ olen = br_multihash_out(mc, i, tmp);
+ if (olen == 0) {
+ continue;
+ }
+ olen2 = do_hash(i, buf, len, tmp2);
+ if (olen != olen2) {
+ fprintf(stderr,
+ "Bad hash output length: %u / %u\n",
+ (unsigned)olen, (unsigned)olen2);
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Hash output", tmp, tmp2, olen);
+ tcount ++;
+ }
+
+ br_multihash_init(mc);
+ for (u = 0; u < len; u ++) {
+ br_multihash_update(mc, buf + u, 1);
+ for (i = 1; i <= 6; i ++) {
+ unsigned char tmp[64], tmp2[64];
+ size_t olen, olen2;
+
+ olen = br_multihash_out(mc, i, tmp);
+ if (olen == 0) {
+ continue;
+ }
+ olen2 = do_hash(i, buf, u + 1, tmp2);
+ if (olen != olen2) {
+ fprintf(stderr, "Bad hash output"
+ " length: %u / %u\n",
+ (unsigned)olen,
+ (unsigned)olen2);
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Hash output", tmp, tmp2, olen);
+ }
+ }
+ }
+ return tcount;
+}
+
+static void
+test_multihash(void)
+{
+ br_multihash_context mc;
+
+ printf("Test MultiHash: ");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_md5_ID, &br_md5_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_sha1_ID, &br_sha1_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_sha224_ID, &br_sha224_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_sha256_ID, &br_sha256_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_sha384_ID, &br_sha384_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_sha512_ID, &br_sha512_vtable);
+ if (test_multihash_inner(&mc) != 258) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ br_multihash_zero(&mc);
+ br_multihash_setimpl(&mc, br_md5_ID, &br_md5_vtable);
+ br_multihash_setimpl(&mc, br_sha1_ID, &br_sha1_vtable);
+ br_multihash_setimpl(&mc, br_sha224_ID, &br_sha224_vtable);
+ br_multihash_setimpl(&mc, br_sha256_ID, &br_sha256_vtable);
+ br_multihash_setimpl(&mc, br_sha384_ID, &br_sha384_vtable);
+ br_multihash_setimpl(&mc, br_sha512_ID, &br_sha512_vtable);
+ if (test_multihash_inner(&mc) != 258 * 6) {
+ fprintf(stderr, "Failed test count\n");
+ }
+ printf(".");
+ fflush(stdout);
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+do_KAT_HMAC_bin_bin(const br_hash_class *digest_class,
+ const void *key, size_t key_len,
+ const void *data, size_t data_len, const char *href)
+{
+ br_hmac_key_context kc;
+ br_hmac_context ctx;
+ unsigned char tmp[64], ref[64];
+ size_t u, len;
+
+ len = hextobin(ref, href);
+ br_hmac_key_init(&kc, digest_class, key, key_len);
+ br_hmac_init(&ctx, &kc, 0);
+ br_hmac_update(&ctx, data, data_len);
+ br_hmac_out(&ctx, tmp);
+ check_equals("KAT HMAC 1", tmp, ref, len);
+
+ br_hmac_init(&ctx, &kc, 0);
+ for (u = 0; u < data_len; u ++) {
+ br_hmac_update(&ctx, (const unsigned char *)data + u, 1);
+ }
+ br_hmac_out(&ctx, tmp);
+ check_equals("KAT HMAC 2", tmp, ref, len);
+
+ for (u = 0; u < data_len; u ++) {
+ br_hmac_init(&ctx, &kc, 0);
+ br_hmac_update(&ctx, data, u);
+ br_hmac_out(&ctx, tmp);
+ br_hmac_update(&ctx,
+ (const unsigned char *)data + u, data_len - u);
+ br_hmac_out(&ctx, tmp);
+ check_equals("KAT HMAC 3", tmp, ref, len);
+ }
+}
+
+static void
+do_KAT_HMAC_str_str(const br_hash_class *digest_class, const char *key,
+ const char *data, const char *href)
+{
+ do_KAT_HMAC_bin_bin(digest_class, key, strlen(key),
+ data, strlen(data), href);
+}
+
+static void
+do_KAT_HMAC_hex_hex(const br_hash_class *digest_class, const char *skey,
+ const char *sdata, const char *href)
+{
+ unsigned char key[1024];
+ unsigned char data[1024];
+
+ do_KAT_HMAC_bin_bin(digest_class, key, hextobin(key, skey),
+ data, hextobin(data, sdata), href);
+}
+
+static void
+do_KAT_HMAC_hex_str(const br_hash_class *digest_class,
+ const char *skey, const char *data, const char *href)
+{
+ unsigned char key[1024];
+
+ do_KAT_HMAC_bin_bin(digest_class, key, hextobin(key, skey),
+ data, strlen(data), href);
+}
+
+static void
+test_HMAC_CT(const br_hash_class *digest_class,
+ const void *key, size_t key_len, const void *data)
+{
+ br_hmac_key_context kc;
+ br_hmac_context hc1, hc2;
+ unsigned char buf1[64], buf2[64];
+ size_t u, v;
+
+ br_hmac_key_init(&kc, digest_class, key, key_len);
+
+ for (u = 0; u < 2; u ++) {
+ for (v = 0; v < 130; v ++) {
+ size_t min_len, max_len;
+ size_t w;
+
+ min_len = v;
+ max_len = v + 256;
+ for (w = min_len; w <= max_len; w ++) {
+ char tmp[30];
+ size_t hlen1, hlen2;
+
+ br_hmac_init(&hc1, &kc, 0);
+ br_hmac_update(&hc1, data, u + w);
+ hlen1 = br_hmac_out(&hc1, buf1);
+ br_hmac_init(&hc2, &kc, 0);
+ br_hmac_update(&hc2, data, u);
+ hlen2 = br_hmac_outCT(&hc2,
+ (const unsigned char *)data + u, w,
+ min_len, max_len, buf2);
+ if (hlen1 != hlen2) {
+ fprintf(stderr, "HMAC length mismatch:"
+ " %u / %u\n", (unsigned)hlen1,
+ (unsigned)hlen2);
+ exit(EXIT_FAILURE);
+ }
+ sprintf(tmp, "HMAC CT %u,%u,%u",
+ (unsigned)u, (unsigned)v, (unsigned)w);
+ check_equals(tmp, buf1, buf2, hlen1);
+ }
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+}
+
+static void
+test_HMAC(void)
+{
+ unsigned char data[1000];
+ unsigned x;
+ size_t u;
+ const char key[] = "test HMAC key";
+
+ printf("Test HMAC: ");
+ fflush(stdout);
+ do_KAT_HMAC_hex_str(&br_md5_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "Hi There",
+ "9294727a3638bb1c13f48ef8158bfc9d");
+ do_KAT_HMAC_str_str(&br_md5_vtable,
+ "Jefe",
+ "what do ya want for nothing?",
+ "750c783e6ab0b503eaa86e310a5db738");
+ do_KAT_HMAC_hex_hex(&br_md5_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
+ "56be34521d144c88dbb8c733f0e8b3f6");
+ do_KAT_HMAC_hex_hex(&br_md5_vtable,
+ "0102030405060708090a0b0c0d0e0f10111213141516171819",
+ "CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD",
+ "697eaf0aca3a3aea3a75164746ffaa79");
+ do_KAT_HMAC_hex_str(&br_md5_vtable,
+ "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
+ "Test With Truncation",
+ "56461ef2342edc00f9bab995690efd4c");
+ do_KAT_HMAC_hex_str(&br_md5_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "Test Using Larger Than Block-Size Key - Hash Key First",
+ "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd");
+ do_KAT_HMAC_hex_str(&br_md5_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ "6f630fad67cda0ee1fb1f562db3aa53e");
+
+ do_KAT_HMAC_hex_str(&br_sha1_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "Hi There",
+ "b617318655057264e28bc0b6fb378c8ef146be00");
+ do_KAT_HMAC_str_str(&br_sha1_vtable,
+ "Jefe",
+ "what do ya want for nothing?",
+ "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
+ do_KAT_HMAC_hex_hex(&br_sha1_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
+ "125d7342b9ac11cd91a39af48aa17b4f63f175d3");
+ do_KAT_HMAC_hex_hex(&br_sha1_vtable,
+ "0102030405060708090a0b0c0d0e0f10111213141516171819",
+ "CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD",
+ "4c9007f4026250c6bc8414f9bf50c86c2d7235da");
+ do_KAT_HMAC_hex_str(&br_sha1_vtable,
+ "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
+ "Test With Truncation",
+ "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04");
+ do_KAT_HMAC_hex_str(&br_sha1_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "Test Using Larger Than Block-Size Key - Hash Key First",
+ "aa4ae5e15272d00e95705637ce8a3b55ed402112");
+ do_KAT_HMAC_hex_str(&br_sha1_vtable,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ "e8e99d0f45237d786d6bbaa7965c7808bbff1a91");
+
+ /* From RFC 4231 */
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "4869205468657265",
+ "896fb1128abbdf196832107cd49df33f"
+ "47b4b1169912ba4f53684b22");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "4869205468657265",
+ "b0344c61d8db38535ca8afceaf0bf12b"
+ "881dc200c9833da726e9376c2e32cff7");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "4869205468657265",
+ "afd03944d84895626b0825f4ab46907f"
+ "15f9dadbe4101ec682aa034c7cebc59c"
+ "faea9ea9076ede7f4af152e8b2fa9cb6");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "4869205468657265",
+ "87aa7cdea5ef619d4ff0b4241a1d6cb0"
+ "2379f4e2ce4ec2787ad0b30545e17cde"
+ "daa833b7d6b8a702038b274eaea3f4e4"
+ "be9d914eeb61f1702e696c203a126854");
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "4a656665",
+ "7768617420646f2079612077616e7420"
+ "666f72206e6f7468696e673f",
+ "a30e01098bc6dbbf45690f3a7e9e6d0f"
+ "8bbea2a39e6148008fd05e44");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "4a656665",
+ "7768617420646f2079612077616e7420"
+ "666f72206e6f7468696e673f",
+ "5bdcc146bf60754e6a042426089575c7"
+ "5a003f089d2739839dec58b964ec3843");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "4a656665",
+ "7768617420646f2079612077616e7420"
+ "666f72206e6f7468696e673f",
+ "af45d2e376484031617f78d2b58a6b1b"
+ "9c7ef464f5a01b47e42ec3736322445e"
+ "8e2240ca5e69e2c78b3239ecfab21649");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "4a656665",
+ "7768617420646f2079612077616e7420"
+ "666f72206e6f7468696e673f",
+ "164b7a7bfcf819e2e395fbe73b56e0a3"
+ "87bd64222e831fd610270cd7ea250554"
+ "9758bf75c05a994a6d034f65f8f0e6fd"
+ "caeab1a34d4a6b4b636e070a38bce737");
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaa",
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddd",
+ "7fb3cb3588c6c1f6ffa9694d7d6ad264"
+ "9365b0c1f65d69d1ec8333ea");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaa",
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddd",
+ "773ea91e36800e46854db8ebd09181a7"
+ "2959098b3ef8c122d9635514ced565fe");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaa",
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddd",
+ "88062608d3e6ad8a0aa2ace014c8a86f"
+ "0aa635d947ac9febe83ef4e55966144b"
+ "2a5ab39dc13814b94e3ab6e101a34f27");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaa",
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddddddddddddddddddddddddddddddd"
+ "dddd",
+ "fa73b0089d56a284efb0f0756c890be9"
+ "b1b5dbdd8ee81a3655f83e33b2279d39"
+ "bf3e848279a722c806b485a47e67c807"
+ "b946a337bee8942674278859e13292fb");
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "0102030405060708090a0b0c0d0e0f10"
+ "111213141516171819",
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcd",
+ "6c11506874013cac6a2abc1bb382627c"
+ "ec6a90d86efc012de7afec5a");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "0102030405060708090a0b0c0d0e0f10"
+ "111213141516171819",
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcd",
+ "82558a389a443c0ea4cc819899f2083a"
+ "85f0faa3e578f8077a2e3ff46729665b");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "0102030405060708090a0b0c0d0e0f10"
+ "111213141516171819",
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcd",
+ "3e8a69b7783c25851933ab6290af6ca7"
+ "7a9981480850009cc5577c6e1f573b4e"
+ "6801dd23c4a7d679ccf8a386c674cffb");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "0102030405060708090a0b0c0d0e0f10"
+ "111213141516171819",
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
+ "cdcd",
+ "b0ba465637458c6990e5a8c5f61d4af7"
+ "e576d97ff94b872de76f8050361ee3db"
+ "a91ca5c11aa25eb4d679275cc5788063"
+ "a5f19741120c4f2de2adebeb10a298dd");
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54657374205573696e67204c61726765"
+ "72205468616e20426c6f636b2d53697a"
+ "65204b6579202d2048617368204b6579"
+ "204669727374",
+ "95e9a0db962095adaebe9b2d6f0dbce2"
+ "d499f112f2d2b7273fa6870e");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54657374205573696e67204c61726765"
+ "72205468616e20426c6f636b2d53697a"
+ "65204b6579202d2048617368204b6579"
+ "204669727374",
+ "60e431591ee0b67f0d8a26aacbf5b77f"
+ "8e0bc6213728c5140546040f0ee37f54");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54657374205573696e67204c61726765"
+ "72205468616e20426c6f636b2d53697a"
+ "65204b6579202d2048617368204b6579"
+ "204669727374",
+ "4ece084485813e9088d2c63a041bc5b4"
+ "4f9ef1012a2b588f3cd11f05033ac4c6"
+ "0c2ef6ab4030fe8296248df163f44952");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54657374205573696e67204c61726765"
+ "72205468616e20426c6f636b2d53697a"
+ "65204b6579202d2048617368204b6579"
+ "204669727374",
+ "80b24263c7c1a3ebb71493c1dd7be8b4"
+ "9b46d1f41b4aeec1121b013783f8f352"
+ "6b56d037e05f2598bd0fd2215d6a1e52"
+ "95e64f73f63f0aec8b915a985d786598");
+
+ do_KAT_HMAC_hex_hex(&br_sha224_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54686973206973206120746573742075"
+ "73696e672061206c6172676572207468"
+ "616e20626c6f636b2d73697a65206b65"
+ "7920616e642061206c61726765722074"
+ "68616e20626c6f636b2d73697a652064"
+ "6174612e20546865206b6579206e6565"
+ "647320746f2062652068617368656420"
+ "6265666f7265206265696e6720757365"
+ "642062792074686520484d414320616c"
+ "676f726974686d2e",
+ "3a854166ac5d9f023f54d517d0b39dbd"
+ "946770db9c2b95c9f6f565d1");
+
+ do_KAT_HMAC_hex_hex(&br_sha256_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54686973206973206120746573742075"
+ "73696e672061206c6172676572207468"
+ "616e20626c6f636b2d73697a65206b65"
+ "7920616e642061206c61726765722074"
+ "68616e20626c6f636b2d73697a652064"
+ "6174612e20546865206b6579206e6565"
+ "647320746f2062652068617368656420"
+ "6265666f7265206265696e6720757365"
+ "642062792074686520484d414320616c"
+ "676f726974686d2e",
+ "9b09ffa71b942fcb27635fbcd5b0e944"
+ "bfdc63644f0713938a7f51535c3a35e2");
+
+ do_KAT_HMAC_hex_hex(&br_sha384_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54686973206973206120746573742075"
+ "73696e672061206c6172676572207468"
+ "616e20626c6f636b2d73697a65206b65"
+ "7920616e642061206c61726765722074"
+ "68616e20626c6f636b2d73697a652064"
+ "6174612e20546865206b6579206e6565"
+ "647320746f2062652068617368656420"
+ "6265666f7265206265696e6720757365"
+ "642062792074686520484d414320616c"
+ "676f726974686d2e",
+ "6617178e941f020d351e2f254e8fd32c"
+ "602420feb0b8fb9adccebb82461e99c5"
+ "a678cc31e799176d3860e6110c46523e");
+
+ do_KAT_HMAC_hex_hex(&br_sha512_vtable,
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaa",
+ "54686973206973206120746573742075"
+ "73696e672061206c6172676572207468"
+ "616e20626c6f636b2d73697a65206b65"
+ "7920616e642061206c61726765722074"
+ "68616e20626c6f636b2d73697a652064"
+ "6174612e20546865206b6579206e6565"
+ "647320746f2062652068617368656420"
+ "6265666f7265206265696e6720757365"
+ "642062792074686520484d414320616c"
+ "676f726974686d2e",
+ "e37b6a775dc87dbaa4dfa9f96e5e3ffd"
+ "debd71f8867289865df5a32d20cdc944"
+ "b6022cac3c4982b10d5eeb55c3e4de15"
+ "134676fb6de0446065c97440fa8c6a58");
+
+ for (x = 1, u = 0; u < sizeof data; u ++) {
+ data[u] = x;
+ x = (x * 45) % 257;
+ }
+ printf("(MD5) ");
+ test_HMAC_CT(&br_md5_vtable, key, sizeof key, data);
+ printf("(SHA-1) ");
+ test_HMAC_CT(&br_sha1_vtable, key, sizeof key, data);
+ printf("(SHA-224) ");
+ test_HMAC_CT(&br_sha224_vtable, key, sizeof key, data);
+ printf("(SHA-256) ");
+ test_HMAC_CT(&br_sha256_vtable, key, sizeof key, data);
+ printf("(SHA-384) ");
+ test_HMAC_CT(&br_sha384_vtable, key, sizeof key, data);
+ printf("(SHA-512) ");
+ test_HMAC_CT(&br_sha512_vtable, key, sizeof key, data);
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_HKDF_inner(const br_hash_class *dig, const char *ikmhex,
+ const char *salthex, const char *infohex, const char *okmhex)
+{
+ unsigned char ikm[100], saltbuf[100], info[100], okm[100], tmp[107];
+ const unsigned char *salt;
+ size_t ikm_len, salt_len, info_len, okm_len;
+ br_hkdf_context hc;
+ size_t u;
+
+ ikm_len = hextobin(ikm, ikmhex);
+ if (salthex == NULL) {
+ salt = BR_HKDF_NO_SALT;
+ salt_len = 0;
+ } else {
+ salt = saltbuf;
+ salt_len = hextobin(saltbuf, salthex);
+ }
+ info_len = hextobin(info, infohex);
+ okm_len = hextobin(okm, okmhex);
+
+ br_hkdf_init(&hc, dig, salt, salt_len);
+ br_hkdf_inject(&hc, ikm, ikm_len);
+ br_hkdf_flip(&hc);
+ br_hkdf_produce(&hc, info, info_len, tmp, okm_len);
+ check_equals("KAT HKDF 1", tmp, okm, okm_len);
+
+ br_hkdf_init(&hc, dig, salt, salt_len);
+ for (u = 0; u < ikm_len; u ++) {
+ br_hkdf_inject(&hc, &ikm[u], 1);
+ }
+ br_hkdf_flip(&hc);
+ for (u = 0; u < okm_len; u ++) {
+ br_hkdf_produce(&hc, info, info_len, &tmp[u], 1);
+ }
+ check_equals("KAT HKDF 2", tmp, okm, okm_len);
+
+ br_hkdf_init(&hc, dig, salt, salt_len);
+ br_hkdf_inject(&hc, ikm, ikm_len);
+ br_hkdf_flip(&hc);
+ for (u = 0; u < okm_len; u += 7) {
+ br_hkdf_produce(&hc, info, info_len, &tmp[u], 7);
+ }
+ check_equals("KAT HKDF 3", tmp, okm, okm_len);
+
+ printf(".");
+ fflush(stdout);
+}
+
+static void
+test_HKDF(void)
+{
+ printf("Test HKDF: ");
+ fflush(stdout);
+
+ test_HKDF_inner(&br_sha256_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "000102030405060708090a0b0c",
+ "f0f1f2f3f4f5f6f7f8f9",
+ "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865");
+
+ test_HKDF_inner(&br_sha256_vtable,
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
+ "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
+ "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
+ "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87");
+
+ test_HKDF_inner(&br_sha256_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "",
+ "",
+ "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8");
+
+ test_HKDF_inner(&br_sha1_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b",
+ "000102030405060708090a0b0c",
+ "f0f1f2f3f4f5f6f7f8f9",
+ "085a01ea1b10f36933068b56efa5ad81a4f14b822f5b091568a9cdd4f155fda2c22e422478d305f3f896");
+
+ test_HKDF_inner(&br_sha1_vtable,
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
+ "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
+ "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
+ "0bd770a74d1160f7c9f12cd5912a06ebff6adcae899d92191fe4305673ba2ffe8fa3f1a4e5ad79f3f334b3b202b2173c486ea37ce3d397ed034c7f9dfeb15c5e927336d0441f4c4300e2cff0d0900b52d3b4");
+
+ test_HKDF_inner(&br_sha1_vtable,
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "",
+ "",
+ "0ac1af7002b3d761d1e55298da9d0506b9ae52057220a306e07b6b87e8df21d0ea00033de03984d34918");
+
+ test_HKDF_inner(&br_sha1_vtable,
+ "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
+ NULL,
+ "",
+ "2c91117204d745f3500d636a62f64f0ab3bae548aa53d423b0d1f27ebba6f5e5673a081d70cce7acfc48");
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+/*
+ * Known-answer test vectors for SHAKE128, from the NIST validation test
+ * suite. Each vector is a pair (input,output).
+ */
+static const char *const KAT_SHAKE128[] = {
+
+ "e4e932fc9907620ebebffd32b10fda7890a5bc20e5f41d5589882a18c2960e7aafd8730ee697469e5b0abb1d84de92ddba169802e31570374ef9939fde2b960e6b34ac7a65d36bacba4cd33bfa028cbbba486f32367548cb3a36dacf422924d0e0a7e3285ee158a2a42e4b765da3507b56e54998263b2c7b14e7078e35b74127d5d7220018e995e6e1572db5f3e8678357922f1cfd90a5afa6b420c600fd737b136c70e9dd14",
+ "459ce4fa824ee1910a678abc77c1f769",
+
+ "18636f702f216b1b9302e59d82192f4e002f82d526c3f04cbd4f9b9f0bcd2535ed7a67d326da66bdf7fc821ef0fff1a905d56c81e4472856863908d104301133ad111e39552cd542ef78d9b35f20419b893f4a93aee848e9f86ae3fd53d27fea7fb1fc69631fa0f3a5ff51267785086ab4f682d42baf394b3b6992e9a0bb58a38ce0692df9bbaf183e18523ee1352c5fad817e0c04a3e1c476be7f5e92f482a6fb29cd4bbf09ea",
+ "b7b9db481898f888e5ee4ed629859844",
+
+ "5d9ff9fe63c328ddbe0c865ac6ba605c52a14ee8e4870ba320ce849283532f2551959e74cf1a54c8b30ed75dd92e076637e4ad5213b3574e73d6640bd6245bc121378174dccdaa769e6e4f2dc650e1166c775d0a982021c0b160fe9438098e86b6cdc786f2a6d1ef68751551f7e99773daa28598d9961002c0b47ab511c8707df69f9b32796b723bf7685251d2c0d08567ad4e8540ddcc1b8a1a01f6c92aaaadcaf42301d9e53463",
+ "f50af2684408915871948779a14c147c",
+
+ "38c0be76e7b60f262f1499e328e0519f864bbb9d134d00345d8942d0ab762c3936c0cd1896eca6b77b3c01089dd285e9f61708a62e5ea4bf57c50decda5c215fb18ac149d7ace09ffdfed91e7fbf068d96908e42cf1e7ee7bc001c7ee9e378a311e44311923de4681f24c92eb5f0fb13d07ef679ded3b733f402168dc050568dbf97fb79afe8db994874783e27ad8d040ba8e75343c6762c6793a42247eee5a6216b908817f5edbbdf",
+ "e4786ad8f2ea9c8e420a6f50b5feec9a",
+
+ "ec586d52ad2ced1f96bd9458a5a1f64bc1b4cce1fa52517513c9ebe63d0d0eeb26ae5da73208137e08baa22651599a01bc65cbaa467baeceb8cd013d71d0b2406534fe2e6619da3aa380928f6effb09f42ba1fb7048e90d7898f1dc259b52c51b2d2970cd0c70afb6cf8acba83fd01cc589b0f31bcf2bf3b8df7879d7d2546c514706f6cf97b6a6b6d0a37d018ba553108f0e240f70f03a0ccee86f76589c64594f6cf74679bc330ad9f",
+ "191a3710c72d11da7a2410bc73ba9d9f",
+
+ "c201dfe59e03574476e3c220c971c1685ea96ea137daed2ac10845c54d8e6e53c307acdf956f1bdef3868ab53e758c7cbeb4cd02972ba311f998e5f3983000345c8947aa59b78bb301b6ecbe9808ee0de99ed0b938fc19f677997398bd84bcd6f34d5b4ed123d04a093a8f42c1700fa2472f1ecc00957761a2d296bda3d2cbc0f21d8ed4e4fb122b71db1d49a0f516c3402f6046d93de6dae20df7683462557abfbf88437c8678dfa2613b",
+ "464121895e5c9d85190bcee0437453dd",
+
+ "bd34acd613e0e0da6bebc45ba73fefa0bd8aa8ebba34040a07944f29eb63adea527101b8cd960e58d9ecddc0643b5e2d8db55170ace4678892e0a57612c50a4dc0647189f839b9a1229e22e0353dfa707acb7ab893f4ebe8bb910cd14f21b8fb8e77c4f19db027e0cd685d60212e0d920b34e96b774bd54f0a0f4ce2ac5f001b4411c19ac2e3a03b63b454eb30f4ddbac959673260d370e708c32d5030682ad56a99322972ba6eda6be9d027",
+ "8e167ceae101ea0b3b98175f66e46b0e",
+
+ "166b4fec6967c2a25f80c0075379978124833b84894c3cb3a538f649dcee08b8e41707901f6273a128cce964ac1e9b977bb7fe28de8bc2542c6c07109889cea84d34ada6bde8c8f5358afc46b5ef5db3009fe3a2efd860ed0ad6b540595246c27849abf7eafea9e5af42607519f3c51ddbc353bc633afec56aff69a0c953584d8ede684b4faefeb8be7d7db97e32bc1c35abb73ce3ba8425726d89f98e93ed93b67b4c6993ffafb789c1bbda8d",
+ "eb2fa0e8e04e698ca511d6abf7de84fb",
+
+ "62c625d31a400c5ff092d6fd638f1ea911ad912f2aabffea2377b1d2af4efeb6eb2519c5d8482d530f41acdab0fbe43f9c27d357e4df3caa8189fa7745ff95f811ed13e6497a1040852a1149890216d078ee6eb34461cfa6693ba631dbefacf83ce5ba3f531ddeadba16ae50d6eedce20cca0b4b3278e16644535e0859676c3fd5d6b7d7df7bbe2316cc2bfa7f055fffc2835225976d9a737b9ac905a7affc544288b1b7d6dad92901162f4c6d90",
+ "bb0acc4423c1d8cfc788e748ade8d5fd",
+
+ "8af63bbe701b84ff9b0c9d2fd830e28b7d557af3fcf4874bb7b69f2116388090d70bff64a600427eeea22f7bee0324900fbce9b8752fe312d40f8a8485231da5d94694daadb3d6bf3e7f2cc83f67f52829cc9cf1d3fcc87d42b3d20ec2e27cb135aee068acbca68734ac7a5ff3e3bd1a738e7be63de39e56aaaa6104f6fd077c964ccc55cba41ca1783003883100e52f94096fdfdc6dcd63b3fd1db148fc24cda22640eb34f19ed4b113ad8a2144d3",
+ "4a824cae0f236eab147bd6ebf66eafc2",
+
+ "a8c0f0e4afcda47e02afaaa2357c589e6b94168a6f6f142b019938186efa5b1b645bb4da032694b7376d54f4462e8c1ba5d6869d1003f3b9d98edc9f81c9dbd685058adb7a583c0b5c9debc224bb72c5982bfcdd67b4bdc57579e0467436c0a1b4c75a2d3cea034119455654f6ab7163ed9b61949d09da187d612b556fca724599a80c1970645023156f7df2e584f0bf4c2e9b08d98bb27a984fa7149c0b598adbb089e73f4f8d77f92248e419d0599f",
+ "4800f8f5e598a26ee05a0ea141f849d0",
+
+ "a035c12af3fb705602540bd0f4a00395e1625edf2d44af4a145b463585aba46b34ee3203eb9132842000f54dcd234e347c28486ea18414af2d3445916049403adfa3ed3906fdb3b27f2aa4bb149df405c12fb0bf0e1dacb79c50bec3fde2295fc8dd5c97ed46dd28475a80e27017dc50d9feff9b1a1861ac86371791037e49221923e6e44874962d9f18f1898a98ee5dec1e9eca6d7c1ad4166fbac41b2587caf7fef3e7be90c80aafed5f7a0928127321",
+ "2d124d81a4a45ad9c0b91cca23cc2991",
+
+ "d41739834414a0792470d53dee0f3f6c5a197314d3a14d75278440048294eab69df6eb7a33c9f807b5082bd93eb29d76c92837f6a2d6c5c21a154c9c7f509ee04b662b099c501a76e404996fe2997163d1abdd73df019c35e06d45b144f4dbb0462fa13767f12f4e1b2bc605c20ce1b9d96c0c94726af953e154d14cb9c8c8aff719f40c7cf45f15c1445ba6c65215024b316d60435905a686929874c6148e64c4eccd90c3a1d1553d18ff57d6b536c58ec3",
+ "551fc7eceeee151523be716538258e2e",
+
+ "5bbb333460ffac345e4d2bc2dba303ef75b85c57233590fabd22d547bf9e1d7a4ad43a286b2a4618a0bb42559808fd813bea376ceacc07e608167ad1b9ec7d7ae919fd2991464cf63570c7dfb299b61836bd73a29007cf1faa45b1e5539a00514272c35d58bb877526530187afbcf55a6f1757209c50af4eab96c2ab160e6ea75dc8d6ef4bf2bf3e7a4b3a7619db84efede22a0f960e701b14f0f44c89b18f2640017c05ef51bcf93942b8d3775d2980b80435",
+ "2c98dce5b1ec5f1f23554a755fac7700",
+
+ "8040a7296d7553886e5b25c7cf1f64a6a0a143185a83abf5c5813bef18008ec762e9bcc12ab7235552cf67274210b73942ac525f26364af431fc88cc34961169f6bf8872d864f360b9fbc27b18160d0578381db509e72e678402731157555bf9026b1325c1a34c136b863eab9a58ec720cedaa0049bfddb4863d03a6ca65f3dd4f9465c32b9db4d52f19e39f10ffdfe8c475032a2fe5e145ff524073d5ed617fa5e387325f7ab50fcf5cba40c2326bcf6a753019",
+ "c0bb8427ef0ca4e457d2887878d91310",
+
+ "cbaceb762e6c2f5f96052d4a681b899b84de459d198b3624bd35b471bdc59655b1405e9a5448b09e93e60941e486ad01d943e164f5655b97be28f75413c0ab08c099bd3650e33316234e8c83c012ad146b331e88fb037667e6e814e69e5f100b20417113c946a1116cc71ed7a3c87119623564d0d26c70dd5cfc75ef03acaea6f8c0e3f96877e0d599d8270635aee25be6d21b0522a82f4149ec8037edaf6b21709c7aafd580daaad00a0fd91fcfe6211d90abef95",
+ "626bd9eb0982b6db884d38e8c234854e",
+
+ "1bbee570394bc18d0f8713c7149cabb84e0567dd184510e922d97f5fb96b045f494808c02014f06074bd45b8a8ad12b4cb448ec16285fb27670fce99914f100ad6f504c32fa40ab39beec306667f76f9ab98b3ec18c036b8f1b60d4457a9fe53cbab23a0ee64d72d8a03d6d8d67a9f2ff6eb1d85c25d8746c8b4858794e094e12f54ab80e5ba1f774be5c456810755ffb52415b5e8c6b776f5f37b8bcf5c9b5d0ad7e58a9d0fa938e67ad5aaee8c5f11ef2be3a41362",
+ "a489ab3eb43f65ffbd4d4c34169ee762",
+
+ "aeacffca0e87bfdb2e6e74bfb67c9c90a8b6fb918b9be164cafcab7d570d8cd693bd8ee47243d3cbdaf921ce4d6e9e09c8b6d762eb0507bd597d976f6243e1f5e0d839e75ea72e2780da0d5e9f72a7a9b397548f762c3837c6a7c5d74b2081705ba70ab91adb5758e6b94058f2b141d830ff7b007538fb3ad8233f9e5bcbf6adcdd20843ee08d6c7d53cc3a58f53f3fe0997539e2f51d92e56990daad76dc816fd013b6d225634db140e9d2bbe7f45830406e44fee9d59",
+ "4eaa27b085d08fc6a7473e672ea2ca1b",
+
+ "a22314d2173ca4d53897924c4b395f0ae52c7fff4880525cee9055f866879af35f22759903b779898676a216feefd4ed75d484f83c00b58383b9279e2732cbc2cb5479b72abee5b4ab0bd0c937537b7a47f461ad419225c6045cca10c191225f0e4389f3355cd3a0d2de822c9d6f3cf984147de3fd3d8a6c9a02a617ddac87114f770b16cc96289321782108d94a00b153bd40651809cabe6c32237a2389e321b67769e89676cdd6c060162592ecadebdd7512fa3bfece04",
+ "eea88229becc3608df892998b80cf57b",
+
+ "f99bba3e3b14c8de38c8edecd9c983aa641320a251130f45596a00d2cfeefe7933f1a2c105c78627d782fd07a60001c06a286d14ec706dcdd8a232a613e1ea684ee7ef54dc903ec1c09c2c060bb0549a659fd47ae9e8b9cb3680b7c1c2d11ebf720209c06879d8f51d9ee1afafe263807c01bb9def83db879a89f7eb85c681c6c6cc58cc52893d0b131186cc3b9e16bad7d48c46a74abb492d475beb04c9fdc573cc454242c8534bcc7c822356ea558f9fa3ae3bb844415916",
+ "5109746cb7a61482e6e28de02db1a4a5",
+
+ "564da8460dc0c3d20b1fda3628349a399ba52446b5d3626fd0039ab282bc437b166f186b3c5e6c58ffb6bd95f8fe8b73c1b56a07ad37572eb6e148cfb7750760dcc03fac567ad7d3536d80922dda8ac4e118fc29c47ee3677183ea4e06242b6090864591c3ddaf4bef8c4cb52f8e3f35e4140034616faf21e831a9b8d68f5a841a0a52a2eb4f9ac9bb5b488766e251cdb0f29faeeed463640333ad948e7f3ad362948c68379740539f219d8f3ba069952efa0021d273a738aad0",
+ "f43552da8b2623a130196e70a770230d",
+
+ "8a54e8bf30eeb2e098955f2eef10af3c0a32391656fdff82120e4785bb35a629c8635e7e98c9eadfa93ed6760ae1d40313000dd85339b528cadfe28258a09e9976643a462477e6d022eb7f6a6338a8fdbf261c28e8ed43869f9a032f28b4d881fb202720bc42cf3b6d650211e35d53b4766a0f0dfd60d121fa05519211bb7d69bf5fcb124870cda8f17406747097fcb0a1968e907adb888341ea75b6fcfbb4d92ae8ce27b04a07a016df3399f330cb77a67040b847a68f33de0f16",
+ "c51c6e34cef091a05dfcf30d45b21536",
+
+ "2a64753a74d768b82c5638a0b24ef0da181bc7d6e2c4ffdb0ae50d9c48ecfa0d90880974db5f9ac32a004e25c8186cd7d0e88439f0f652256c03e47f663eff0d5cb7c089f2167ff5f28df82f910badc5f4b3860af28cbb6a1c7af3fafa6dae5398d8e0a14165def78be77ee6948f7a4d8a64167271ed0352203082368de1cd874bd3b2e351b28170fdf42871590d9d179ce27c99f481f287820fd95ba60124517e907e78a9662e09519e3ef868ebdcca311700a603b04fae4afe4090",
+ "2d2ee67938422ae12f8cfa8b2e744577",
+
+ "a7d645b70f27f01617e76abc2ae514164f18d6fd4f3464e71a7fc05a67e101a79b3b52d4ecfa3ddac6ec2a116d5222e8e536d9d90fffec9c1442679b06db8aa7c53dcde92006211b3dd779f83b6289f015c4cd21ca16ce83bb3ea162540bb012ee82bddef4722341454f5f59da3cd098a96abbbdc9a19202d61c7697979afa50deb22a9bb067ccb4a6fce51c930a7f4767cfaa9454c9c1832f83ee2318b0f0c95d761c079c0ca2dc28871229aef11f64199ca290b2b5e26d8c1c12ec1f",
+ "ec989e0290fc737952de37dd1ebc01c6",
+
+ "3436fe321f2a41478164b8b408a7a8f54ff2a79cb2020bf36118a2e3b3fca414bd42e55624cc4f402f909016209b10f0c55626194a098bb6519d0fa844a68ab3eaa116df39797b1e6c51eb30557df0c4f3d1a2e0471f1d8264fb3288c6c15dcde4daf795083aad2b5f2d31c84c542fb702ea83b7524ca9a1c1b9754ade5604abd375f23f3916cdad31aecaa7b028b7121a2a316713991759925f3fb8366c6795defa6ea77416c4ed095c1f9527026f1d621815b8310d4ff3fc76f798760b",
+ "bb5e48212442ad7ae83697092024c22b",
+
+ "01bdb4f89f84b728a9d6b3a03f60709900571c1a2a0f912702cad73677ceeae202babde3d0197e3e23381cb9f6350792e05937703aa76f9a84b5c36705bb58f6b2ea6b1e51ff94a8de174cbc2ec5ae9ad2627a8b3ea45f162b727a7639f71a4cd9f6c6926a5d81d0a21c4c923037ed199f1aef517e2eea03bea9044c5baab84e3f85d625635bcb1c37ef232144b44c770f2b9dab416b96c906016acfb3fbba62ab40a4c08323fcf66437d953b164541cea3a8c81d186eed0cb23b3e98813a9",
+ "8bb7ffa4572616f3bc7c33bd70bbcd59",
+
+ "9ae51ed483306c9a5a6db027f03cd4472cf3a71df5f1e11852306123d01ab81c259eeb88128275858efb8cff207ba5278dca3a21b358cbfdb5d223e958f3dca5ad9d2537f128c3dfb1fa564d3157de120f7b7d5524e67fc7abf897d9a5bd6b2c7c0a5348e6c95e920c919778ec7a86effb2ff91f0f44045c7dca46597e216e98d80efe25ba0d4f84e7e9d5e81689a5a6990d34e83e1a62a67371b7d2adc7ecd30ad1ad35359e9d9f8a299b057a2f441e313eb819770fa18cd41572adf856edc4",
+ "e7f66f49f70d506a9b5508cc50f65cf2",
+
+ "899c81ea1162514ea7a2d3487d0efcc4648a3067f891131918d59cc19a266b4f3c955c00ddd95cddedf27b86220c432d6ca548e52cf2011da17fd667a2177a7f93e37b8892d51898f1485277e9e046a48cb8b999fcbcf550db53d40602421a3f76cd070a971e2d869beb80a53b54ac30ac0aab0cd1b696bbaf99bb25216ff199cd9a280f567c44b0d4252c98812e1ddab4e445c414aa8d650598b64d6768a7948093051e36b7051c823c7ed6213743a98d8eaf4b2b5e8157c699ea053cf4e53877",
+ "52173b139c76a744b7a4d2221d4178c4",
+
+ "e50422869373abac1c26e738fb3ccb577b65975a7998ba096b04ef3aa148ada2cbe6beeabcf52d056d1766c245ab999d97445fdb6d59a0d6843eb4959752c89fe07b8411ddcfebef509482b8896bb43de7c875b29da52606b278b8704c62154b2da9bb237e68aa10cb85814250e4e4de73da200991e51241fd9a45f446de5a4bb959ad4727283510e9d2ac8a207ef0284163aa05d27f2d316e8ca1480f30604a8d74a0a661775398af644bb584a1a2c55c4959d0e7dd3f7c0c3614962fbeefeeafe0",
+ "f4c517a82c850c3c4c96d23a8f3106b8",
+
+ "066febbe205ea342cde69fd4c72889442e14a5977d886252bdbc2ff5f8dd8fc5f1f870ce121ab929a6b6227b484648be9b3501443cfdecf8f58d4de834ed1800bb244c18985a8232583ac6fc789aa59d1c5e87ad03994085bbf6e1ba1157d4e4ccbb28a49b6529e54b3b34613d6cc9671855e2dcbba6838176c093737962eaf88c85ab780184d4cae78013b28103dca7f7e3b8d94a6ae0728db30a1c535783c4644a7e9eb4ffac6a95d30cf52ba805e220d0b2aa9a2e7de26a97efbd877ec6d1bad148",
+ "bac7162dc8328911fa639f26ba952ab0",
+
+ "ccf92b17b9cf0d8577c1f3db9c19d3c86f16bab4058611f6aa97204783ebd07671eab55e375c4b16e03780675bb5738369aa7cf3b9156cd250f516392f5e0efa30cbb09132b66457756621f947093029e10233938c846513086023252d1bac9dd3442598f004e0b200f7dd79aa3a9122a0c6e77bc7fc8521988050f3c64b32c620fc1b5bba6f458e4791bdcfca731fd66e9da093b1a45264c8ffa48b3f1628dfe19c9ac1d71f1d5214ddc7e4f0da60ae122f67c394a55645628228d5e3a3174fdccbaab4",
+ "19a9eadf9c7c000fe340603f27bd830b",
+
+ "a37dcfab50a317e6a7cc51524b5d611a53652b59fc7df0229af3dac4d527d54c1134a14b2ed325d9727d07d9c3d0797f1a34561034be6de98b551dc384132235eaedae7a9b97bb7581a2a0f2c4e8e32f3e294f9b30f646dd33ce58187188146e14f01dc3ffb581c3bc834726b66c4732a98c3f8256ed22077ba8b34c024d53fe798517abc2f61eca0c6722fc02254c9141a54d4e106aaa6d4b2957e6a12c88ed00f4c4bc4c223b92579859fc0edb9b53f0bba286c53786198c9b6c6eb5eb5b4490844b7d06",
+ "b9e1455d06233d14b8d3020441351a76",
+
+ "0248b909e1f31ee855a03b6c81366757aa3732d2eca0b06a2b1015584c2d8205a4431fcdb02f6a03077ccf368ecb78b3eb78664b3c7ac157088b6cf9758adda4bc1d2cdedb9a69448a2833cf6f21865795bbd5551be859ed297aa82c288b898e331c07c3c8fcc4b2c4ec90bf8e003a499248a677f1b020357625f079cdf92fcbef89d904e11d23569e0f0e8c52303c93c867023a269bc036d8d36d69ca9c7664daacc92a8dc42c3600dbd4c02278333d216011252271def835ce4783883c0760dbcc00bc33bb",
+ "ea4606777e21f27d4ae860b3c25283b7",
+
+ "ce283768aa91488c75c71ee80a4df9495377b6a9ae3351a5962aa8317f08818a0117cf6c391331866d3abc2beea2fa4a43cf32a08385ea2c03dbabe3319104a6c0a3d171061ebed5a23306a8618a81fb63d9dd4c79b42bfdd2a79e05d78290e653f4c6dfd75bf5625ddb85c82bad9444faba3e1558691c004bb50afe37822e320131361d7572e015e559c0f313b53e0d529dde64e74bc41eb52e77361a3ae5721483a795a80a87d684d63f92e347843eb1a8439fef032b3d5a396b154751bd8ed211a3ae37cbf0",
+ "dca4d5f9f9b7f8011f4c2f547ce42847",
+
+ "19265f48c1ea240990847dc15d8198785d55ea6243ef7012ac903beabbdc2bd60032fb3a9f397d28aebb27d7deb7cf505eb1b36bfc4dbcfa8e1c044490b695b50e0974d3c5f0de748508d12ed9bfce10eaadde8fa128d3c30c12d0d403f60baf0b53d2fd7a38cc55dc1182b096c11d1ec9f171b879a73bd6ef1aa7825bc5162cbeba1d9f0739d1337c8142445ce645e4c32477cdcdf37e99fedb9236e24a3d94f0e45ea0b41a74762efe19d27555cdc89feef5b6e533237603fe98d8deae084f69799deac9043e86",
+ "688e532e15bde53b0b652291edfb7681",
+
+ "1080391fa810c50c7437ec058459d3a8cd23c33071c187474151151c809871b6eaf4cf88f592f84557e1eef5c847d3490912072b25b1919af724c0b5ecb111150bd95460328a0b1ba29613c0bd6486110fe6dfab8cca5fde18f5b0bc4d2dc970781511d2e45fc7385c3da18eeb18b3a9e68593d82c75bbbcadab2e5a29745f6f3a924e039579f4418dbee186d9cc24b896d96bd990186bdcbd3082b70aee9bb95a36531ecc405ae13d011bd10fe69fe728c8aed73d1d38e5506bf4fa770347f7e0eb6749121cc0be75",
+ "cbf8ee5d477630dac9457a9a0659497d",
+
+ "0a13ad2c7a239b4ba73ea6592ae84ea9",
+ "5feaf99c15f48851943ff9baa6e5055d8377f0dd347aa4dbece51ad3a6d9ce0c01aee9fe2260b80a4673a909b532adcdd1e421c32d6460535b5fe392a58d2634979a5a104d6c470aa3306c400b061db91c463b2848297bca2bc26d1864ba49d7ff949ebca50fbf79a5e63716dc82b600bd52ca7437ed774d169f6bf02e46487956fba2230f34cd2a0485484d",
+
+ NULL
+};
+
+/*
+ * Known-answer test vectors for SHAKE256, from the NIST validation test
+ * suite. Each vector is a pair (input,output).
+ */
+static const char *const KAT_SHAKE256[] = {
+ "389fe2a4eecdab928818c1aa6f14fabd41b8ff1a246247b05b1b4672171ce1008f922683529f3ad8dca192f268b66679068063b7ed25a1b5129ad4a1fa22c673cc1105d1aad6d82f4138783a9fe07d77451897277ed27e6fefec2cb56eb2494d18a5e7559d7b6fdddf66db4cbc9926fe270901327e70c8241798b4761dd652d49ad434d8d4",
+ "50717d9da0d528c3da799a3307ec74fc086a7d45acfb157774ac28e01ecc74f7",
+
+ "719effd45ed3a8394bf6c49b43f35879176a598601bd6f598867f966a38f512d21dc51b1488c162cbdc00301a41a09f2078a26937c652cfe02b8c4c92ddbb23583495ba825ae845eb2425c5b6856bda48c2cafae0c0c2e1764942d94be50da2b5d8b24a23b647a37f124d691d8cefbf76ef8fbc0fbdafb0a74a53aaf9f165075784ab485d4d4",
+ "6881babbb48e9eea72eeb3524db56e4efc323f3350b6be3cdb1f9c6826e359da",
+
+ "362f1eb00b37a9613b1ae82b90452579d42f8b1f9ede95f86badc6cdf04c9b79af08be4bc94d7cac136979026b92a2d44d2b642ea1431b47d75fce61367919f171486a007cc271d19de0d1c4c6a11c7a2251fe3aee0bb8938a7dd043d0eb0758a4768c95cc9f6f1703075839487879b47c29c10b2c3e5326ac8f363c65aa4ef76f1b8bd363eb60",
+ "c6ce60c1852ea780ed845aac4ca6a30e09f5c0064c9675865178717cfeb1dc97",
+
+ "d8f12b97f81d47aebbfb7314ff04172cf2be71c3778e238bcccdeecb691fbd542b00e5b7b1a0abb507f107f781fea700ea7e375fdea9e029754a0ea62216774bda3c59e8783d022360fe9625621c0d93e27f7bc03632942150716f019d048a752ccc0f93139c55df0f4aaa066a0550cf22e8c54e47d0475ba56b9842a392ffbc6bd98f1e4b64abd1",
+ "e2e1c432dd07c2ee89a78f31211c92eeb5306c4fa4db93c4e5cd43080d6079e4",
+
+ "a10d05d7e51e75dc150f640ec4722837220b86df2a3580ca1c826ec22ea250977e8663634cc4f212663e6f22e3ffc2a81465e194b885a1356fcbcc0072e1738d80d285e21c70a1f4f5f3296ba6e298a69f3715ff63be4850f5be6cb68cdba5948e3b94dbbce82989aa75b97073e55139aac849a894a71c2294a2776ce6588fb59007b8d796f434da6e",
+ "02f17bf86dc7b7f9c3fb96e4b3a10ca574cd0f8dedda50f3dda8008ce9e8fec9",
+
+ "152009657b680243c03af091d05cce6d1e0c3220a1f178ae1c521daba386694f5bab51cd819b9be1ae1c43a859571eb59d8cbd613c039462e5465ba0b28db544f57a10113406ccf772bc9fe5b02538e0b483225209c1eca447ab870e955befae6bf30dd89d92ddae9580ccf0dfac6415ec592a9a0f14c79acce9679f52d65fb8468012cbc225152d9ed2",
+ "b341f4114eee547eddeb2e7363b11d1e31d5e1eb5c18ea702b9d96b404938bad",
+
+ "eaf4249b5347c2395104a96d39fbf5322c9af2f8ec6a8c45efdc06a2b246efb5502952ab53b52ed9ca8f25a29cd1789b1b5333eddc29a5fbc76c13456a3eae8c9208c1381d062ff60a061da5d26cec73fb7a6a43eace4953f92cd01bc97ed078da19da095842afd938f1f83f84d53703f397fec2bd635f94ada5a3eb78103ebf4de503e8ad7295cb7dd91e",
+ "d14c7422c0832687786f1722f69c81fbe25b5889886bf85c7c7271bf7575517b",
+
+ "a03e55ee76150a6498634099ae418184228320bc838dbfe8276913761516ec9021226f4b597ba622a0823ca499618169c79eb44af2f182d1cc53caefd458a3ed7bbea0a5854653f2b3c20f659f70f23ae786238a8d0e59c29ef49d53125e50abf43b6f65c31f16bc174e43468717dddfcb63f5e21e8d4ba0e674140a97cffab1d5c165f1d9aef968154c60ad",
+ "fa889888d3b984c1577fe7c38ca86f0df859291502fe0b2f6e82c778babff377",
+
+ "2fb4178a0af42b155a739e2910b004e0781c1bca697ca479bf8e71430aefc043883cc7a151779013d2ad07a47cd652b5bdfd604130a1c565115ac51ff3c0ae56b5886c1ab2f0572e385e4fc33c430b874b46aedec49f9b6f45c08be3633bdde99ee02d7e9325276b74cc9d0fb6bfd85e093f2c2a8d3dcfa24308ec18c229f2072b8b32545ee0a9d46e3f1a0f53",
+ "254a115343d0ebd865e5d3ff6c61c3f9b65fe96ea92865a5681b1f1f0d1b00e9",
+
+ "dd344dd531f415a590a9c1838f242af8605bc0c29c1a71283ff5cd8af581683c94c48095e9e9e042b73804e0fd467ecb78699930696f3b6a9890108b99a0e4384e8a51bbadf99b53c358d8cef9fd545a97a13399861458f35a2e86309009c546136d086f058c0c7fbdf083750cb17250c5ebd8247c6f906c8db978a26123d30dec58ecdb7a0afd6face84efcbdca",
+ "2d56bef53fde76ef9849f97be2ed22d3c3d10f23b049eca2a8aba0d1fec33119",
+
+ "353111e447fee6f0bd05d562f30626ab9fb06384a620c49034a5eb3c0bc6d1eb1b86015053e6041ab8ac1cd7b4633512b0a318bfe592e2da6eabb44aa2bead0ba238158c2ea5db56bd7342efccf9d7fe76b8a6af45e0ad594816915f65749054f1d1b7627e4355ecf4e3af72e4d0f5b51877751c6f110f57e86ce942fcef640c31d94e98ecc959238683cb28a3f178",
+ "11b27034db724b46882a3086815a835947d19322885e08595be271c511ef783d",
+
+ "c4e5a5afa1c7d2edd5a21db8b4891ed53c926131f82c69d323b3f410114281fecbc9102bfa5f298e06d91fbd7e9b9661bbae43e7c013f3796557cf2db568de7c94a7cbf5a53ee9326ab4740cadbf1a0b1f59b92040156b977eb4c047a1f34a0c66a85f776a0d1ac34a5ca30b099cb0bbb2ba4c453edbd815b7f14fc69e8cce968bf453171374c428eef8342459db6359",
+ "f1ebe75725c26b82ffb59c5a577edaa2f24e49c9070cb9ca007e65938f33dae4",
+
+ "3b79da982ac5f2a0646374472826361c9d2d2e481414db678e67e0967e5cf3cdd0c1f570293362207191ecd78fb063347350d8135a4f02614d1de12feb70a0046939c078d7d673fea589460265290334d217d6231274ae0d3891e6f50da725f710c983d9bb16ede20833caef34f9dec3c36a6f9fc4eaa71256ac3a136b6a494dcc5985ba5e5c9773a377c0c78387bc8a4d",
+ "1fc7c4802141e2db7a9199c747d885a72d8f068262863843c9f4cbb19db38994",
+
+ "cf9552db2edd8947fd7fbbb2f7189a578343e742891ae6fb85fa0f64da8706e468f0cdc5607539db5726a2679aeddf3ac2ce711e886eff71dad203132e6ac283164e814414c7f686b011fd02c95f8c262920e9725c811a22c1339e0de16e5acd0036d620f2dda98e30c9324c2b778961e0c0b507ad5b205463a448199c9bb60b4f303420a1be3b3cfed5ab0d693cbe331036",
+ "b51adb0c2375c9d302ba61859040fa4bfa0091275eec1053fc13950aae706c25",
+
+ "4ebc9225da5f168c07ef62f621d742cd7c71bbd063269f5e51d65ef164791fe90e070f8b0e96f9499ec21843ee52290fd219c3b5b719ebfedcefe4efbf6b4490d57e4df27d59796f37d35734110b96fd634f5f20bc3de9cd1c28479464be84270ae7f16211f0be8839e8c8d0734ab22097dd371859d9be527a4b2fe83bba0637170ba6e3b1a2ef1c0cca121ffa57a4ffd78af2",
+ "54a3fd90ae00dfc77644ca16b4964c3b32a4641c5305704ee25d9f8fdbfb5c7f",
+
+ "a83f74dcbb48d679db402433020e33dacfa2c37f1e39b2d9dcdc70e81a2ab3d75f586c274376f90a39f49c0dad642cfa4f810afdae7157050847646d60cc6adcd27f7c6a24dab9049dd7c6111ab37c555ef2dd16aaa34d7e8de5ff41feaaad80a8bb8cec85fd7f2eaef28a8772828ab3a5fc24143a58fc0c15bf27ab1a4de28a8a1584f68f65b151154cd1b6dc5ac0dccba7c73d",
+ "5d084841c35b1cd9c43082746960ff5bb2d3de78f9bfdd80dc9ca4f5eae2a66d",
+
+ "734f872c431ab145706b7517e496a3be98bca885fca0105a99b54980f47caa84b60cb3720bf29748483cf7abd0d1f1d9380459dfa968460c86e5d1a54f0b19dac6a78bf9509460e29dd466bb8bdf04e5483b782eb74d6448166f897add43d295e946942ad9a814fab95b4aaede6ae4c8108c8edaeff971f58f7cf96566c9dc9b6812586b70d5bc78e2f829ec8e179a6cd81d224b16",
+ "14ec5a3c2ad919aa0f0492f206710347e742e7a58d6fdfd4b2c93dc2183b7b6f",
+
+ "10112498600da6e925d54d3e8cb0cdc90d0488b243d404b9fb879d1c8beb77bb6579b77aebdbf3e785abe61df17e69e8db219f29ae226f7ca9923719350abef876ec6b3920ebb5c28ccedb2a0b70d5d67a0c8a6116b74341922e60a867d24aa96cf1a89ca647d6c361c5922e7f91f9db114db322249c6a50dde28093c94c01166e11d66c26f73c322d1875f0f8e6bd41c86d803480d8",
+ "c9a88a3f221a857cc994a858f7cb4567979ada7834a265278e55de04c1fe496a",
+
+ "6969a27ad5d0aae6479b2b044bb4b043642375ff503ccb538e17be2f1e41f6aa88b1db991ffefd6087cfb20875920192b671be8b7381f7e1b33d8ff5213429f110fe475cbc74b3ecd2211f9b33f308fcf536e0d0abc36bd5e7756adefddd7728093730ec339c97313179b9e40e3f8e2a2a5c21f5836bf0d632a7961239a6a7f77b44dc700cdd70d8abbfc90c8dde5bc45dcaca2380df4e",
+ "bcdec7a8776380df27a4613cb50b7221995d3f752fa55691798ac2dfa0b15599",
+
+ "163cf8e89b260a81a3d6e4787587a304b35eab8b84faebcef14c626290a9e15f601d135cf503bc9ad5d23e7f213a6146787053f618c6ee90467e3a8df1e03387928acc375608339f7fa45788077fa82f87e11d3c58ce7cf3f8dad6aeaf3e508b722a2a62075df9fa6af4377c707ffe27aa5a11468c3b1c5fce073dae13eac2d1c9a635c5502b96115e69e741a262ee96a78336fcfc34573c",
+ "181d10fa5a58ca57077be52eda53910135087312ca7711084e4a5213c81cb4a2",
+
+ "3a023141ab4db8b08c5cb6792ad97abdf0116d512ea8f4141a8b987f1527657d2fd98f7deca55cc6492a3d0bfad53e40f656a1ac3550c63eb8554f24cb11819a87c5ec009af84e304b69b50eb847e46162a4f8e1ec284b902002994e332461a84ab08ef23cad57959aff64a9ed9632c73ee5b818dc964bb2597cbf25d6c9cf508081be7a5b2e3f9e3fd69305202af11a92002a7b8b038d4c6b",
+ "b75b698857675f8aff2b482ac437925af3ea86198484cbc87b60e6dacb13e7e8",
+
+ "2fd7ed70c6946b11c819775fd45bc0924c02e131ab6d4a3618f67e6d3b77801d4f0d87ea781bf9fa57929757dc70f5945c872eb4e480d547cc1f2fd68fc99f81da4361e7e2bc7b46fb0ef1e3674139ad6b50ee1da830c960a90fccb8b9dac020f701e22fac7eda3edb14eccd1ad47223a1e68a35a1860cc9d74dbfdb60b2cc40cfd072897d6afc2a202cf0dc9f338a3f25d068c4758987ca7d61",
+ "85c9275ec610ffbcd7f785c0ad24b7700b32ee352e6720f1ea2305bdb7f45277",
+
+ "cecb838187223873bab25205a54dadb1ab5a633958cbef3aa04f930467c8f7a947ff12548d964ddc843fe699f72c9377f1c76948c7a2fb5f58b1c65a94b7cd3f3bfe80cbe74be2064d11eb1bc0e52b67f732b1d00f2e2b58d30c4ff13c7479943430958d9f283f199c9029320860bdaa450404773955c74e99c9f47367e642cfb9fd1843bd14ac3cfa246887d885916763a62ae54c011668304e7e",
+ "3a5dd05e009e7f985a2668885dd0ea30c5502a1b5c575db6a4c1149c2e6229c1",
+
+ "283dfdb2e1dc081e3c2b377ba5bc6491cc4af08c40fbfa5e3fe2d45fcdc8b736032cb5fdaa88f0a008d60a86fa53dc7443836bae2475175f2d48163a52ee216241306d87f3f2dd5281b976043a6a135af2555ab39c71ee741ce9e6ac56d87ff48b510d9ae5a338fe50db643b8c8a710a80c8a5e4d278e667b4ce2dfb010f37b588987e7ca822676a1d44bd7419395e4e96e43489eb1167ff9efed170",
+ "5643c4252210fd45a2a67cd0a97d37e80d1b4a3c2fc86b0c3a3b4d3c1723b9ec",
+
+ "f32d2e50e8d5df7ce59a9d60255a19f48bffe790e3b1e0ba6b4bc53d920b257bff8d8003d5faac66367d784706f690b2f1f3a0afafdcbc16866d00a41169734f418d31d7a1c3ca9ede99e5b986f1294710fa5d011d5fcd13fdbef02b755b49cfbf168bf3d39a00cbe5d82bde2fb4ad5cf0fd65b1b5a3db5ad724dff745486da2830ed480f3e61795542094dd88a5e3989ae501e5ff10ae921c89133309",
+ "1ead94e30440b647d4cb4d7b3ed6b87ac07e8d72b3e5f28352bf14a78232ff1d",
+
+ "8bbc18eab6bcd9a3d6b90ec56d3be949e02a8866d69c7808e1ec787e600c7f72a41c001f513b6cbe079df94142dda2447f956e41a12df60392f0215d2d65331b5cdc06397d4796530b4bc45d7a975394627537b4e09e0f6c3a53f00fc1a9648cfc25b2a00288604a28ecf780dc100620d1f169295d9acb2b1f3c6afce4811aadcb1e8dbca8a8d18ba7a81a1132f1c2d014318e07dec7332889d4198c5e95",
+ "429f15c653f92734bfe4d1749e84da8c28861b70c5158bf59809ece810221774",
+
+ "a3d0eecfeff88df1cdd1e86df7bd2ec3ba60bcedfc9c42ef7dc021b05dfc1808df19201a6c6694e4dbf69514ef08ad1d21c7b28ba034ee9397607cefaedef5e9d3784db53a21f703a22b50d5dbba3a8e8579074c1a8b9a782fc5c89cf61a047408563c476110fe77acd9df58c2ba1d3e6dde83da718b8dc6cd57cd5e3e988dd2051cb679ea1af16881690b44acf09e54615eeedaad1b11a4f97e53de8d40d8",
+ "afccfd3b18f6d292d2e125884b721b3e3099c4dac8aef05ab0fba26799043d02",
+
+ "2ecb657808b29574b020545fb7f94071406047ef4de20c003cf08cbd91930187f55b079d7f99fded33cdae2bc8623021af990d4650c4a19197b4c38faf74a8b40d3803efb1907180a8e1150ed6167ff4f293d3ddd26a2790e9d22c0d0ed511d87e48a4952500bbd51943d230687df5941334e1dc5a3e66a43a320f5c351c059c517531b76352a1938ddb2db806ff5aa619667e6c71a7257693bcb4a7acb34ca8",
+ "c994acd17e08e8efd3ba83915245781e3727bac445672c44e6335e4f7deaf90b",
+
+ "e649888592d192c5fb59f10560f5f5a7b0ac21739c35dd80f1fe6b5825731c572f7cc4549c476b84e049459aea7fe533fbfaad72b79a89e77d1addb6f44cbbf5e6a65a5552fec305bc92ced3c84b4d95074387c71184e875d413f65c2b2d874cb3d031d0da7d0311383d72f823e296937d8f97bad17a62f29ef1a091f39be8233c01330d5c4c9170fc501b5022ca29f605e6c59220055f2585bcc29e742046432c",
+ "88a9aa4b4ffac981d1ef0e8b233cb309695f89211cd4e94d50760909e3cb919c",
+
+ "816b0bffd99b0f7821e6093ef152723a9cb45f7a082ef8d6bdf72cd33b5aa3c79102f43e2b74199decdd20057d0e227ae4c57945582e2e9653a9b16eeacecdbc5aaedac7e35c35cbd9adede7f83bbf36f8b0453d61416a85a17821885b3757d203fa2560a85c4b4c10dddaac0ae230b700fd2929cc6f94e9ccebe4e9399d284eb46b3ed2227b4366baf54d1b5c0a5d4225358fd240c0940bff8b62592a092a7b978b",
+ "c593f3d663c48426ce892f22584d49a3335cce3456194b7b5ee4814fab477fcb",
+
+ "a10918880cf31a8551af80bcb0d1a6ed71ca42c71e533967ef0fb71c866b7e6ddcca7e5d7cdfa6edef59fbe377c6e7ca00b1d33a530ef8598dd971a2cff995e5386a858f109b012c4615802a1d5e7fe0221d19cf617ed827d8d8cb8d2c8ed81b9b3354a832f1d14a402b371a0a611737c0543b0eb06b82d8ba56eb6304f1ef16ef6b143049a7bf50c4e2493aa69756d8c39f627fa89d9d741a99f9afbfeb81de1a5bec",
+ "d557aed03eb7c4c4c8091efdee992c9ad7f8d2e79e9296b40a08acae37868d48",
+
+ "de7ba70e45c879ad6c90ada6fda071c2b692840f7893eeca9b69ef8285b4357b7b735151b6cb6cddba04365ce3d520ce41e1cb9da681c07ffcc4619ddcb420f55ddbeefd2a06f689d8498cee7643606865a3f8b96aeb5d1301751438f4b34fe02dba655bc80280776d6795a4dd749a56cae1f3abec5a2d4e5183ee9bf5382c0492199eb3b946707022673bc641f0346119a3a4bb555698f895f6d90e06cc1e2835ff814d",
+ "06cfdd9cd7ce04abcdbf3121a9ba379505dbbb52f148c9d28ad9b50facf573ab",
+
+ "6e9a5752ff8ae7c385b088e651ef2543daae1624562052f787c9e0f5d83e8f01a82ce7d3e69b5f55de74d14d52412a3dcd356687346cbcd59e7315b8650bc3907e2a70ab054354b11cc7ac3ff6ec67d22fad22e75f125660eeb1d02a2a75621d969ed92385092e9de8b20102657742c9a91f328afe9a8a60208af9914c03d4719b8f0a838e7656e2ea3cb8dfc66a25ece2927eb93a8dbf9cdb077936f63e82543306ea1347",
+ "cb1e8082bb94629f162f20d815bcf3b212007bc049951a29ddb18a1f556bf3d1",
+
+ "b05007119789d382fa750d2087dde79b37a5459c24522b649ac976b07059cbdf99fcce56f6da94246e0f5ae241ae77dd99068f7863240acb5c99c4906f7d06403eb3b679ff6fcaa389f602d3aea5d7efcc35af149f3d523459f8a104f5498615c8fc2740594f5f4872b16ebb77c9ef19f7ba0b3881a6ede7b97175d2aac731a65e608975ac82395b52c805624423a7a3431e0daeb066c12ca389a9c338fef03a296644dea211",
+ "9021fefc1a020cd0c579e3dd67a66dacfabedde9cd36ddfc7d5c5c7c47be2721",
+
+ "a19909e14ddf9b3c470df6bb604604ad767c38c83b2b747937472b791173c3a10a733dffcae417295f2a71d183ab709a1d3be02a0bd61d811f95338967db44eeb2cf2a2f4f105ef618a418a5b031b831086f653328ddf43c2cb30b698c188638a196199a65cb374a7b61335c6f40a6193e01100a19a6c2536689fb4308935128e0ae5268937d6ccd8e4a0a21484000fbc7da29d8669b4e6dd5004a3c61b36c6676011dc0628ec3",
+ "7dcbf4dd9c27fd8340f51c553898502cec53d3bc83198352fc58465625c076a2",
+
+ "b0dffe4a5f64f612359397e4e070a8fa01296c1d8cee25177104d76a7c154e4279cb62a99d9d7afa21e84f983041f3df030a115b4b437638cfa3d0fa56e7b66fc76be9e18ff7da8f43db6c5f863efacd2eb39c27a20da6fc867572d29bb96017e0e71a5afe1b1dbbe29575a0ac0ec7aac84c95e85af5be4ae0a14458133252230d687e7cb1b04b65483df2c5685a62601aff85053ba2c509234fcff585fb967c96169bb0725f6d75",
+ "8e7023d18902a9184a0191f1c7a2b79030e833800baeeb33e2d0673500245dfa",
+
+ "dda3625c78f733c7df0b5f4987cd30d7207afa40ca07f3b686c0458aea2f62371a3f98a2f3a1e5a0896f0cb9d40fe82ca65b0132e0fe5d87e621992750483855e3763ae2bf98f0acd9201065acf105962c7b88e3fc277490e0f5d6447563440d209271a544a4fef4b86892d578392c1d9a23b8da8448e1d85d82276ac14a3166b9d96472ea8cb47e0c8dba929eb007cad89bb99fe22a4c674312b21f9cc4a56996943cd1191abc54bf",
+ "ad83957a387225aad811b0737f582dbe7eb616187a8ba8e09b00db5d0bee4a7b",
+
+ "5cd623be5b6bf6d1bcb414c826d0f4ce60793791b6d82dae9f9e9b699e50bba266e2850541882d80b2c9edfa59d504421818ff45740f37853e5b9bc67214af0a5f5fd5c00843cc39cbb8765b4001de99643c7923f738ac5922868f865dd3f1cb90759c597843d9e34daa3754a2fd89bd8c0d2e9106fa95149448ff11273587cb414a603759315f6881c6b94b46700d94d8b2a5f86bfdf99ddcc974cf98e47bf4ba09acc273b463afaf35",
+ "f754a71e3439760aec2d763751e160d05d3de0809dd4fd6aeef588da8b86a517",
+
+ "42c0a452e83840ae858c094c044961d5f2195ddb34a21cd1f5ab575be3803ac99b9872dd617688d515cd6da562e756853947c9ab7e8ef85a019b4f1baff6494b0a6f87d5d602234115fe42ee3667e89b8a98112cf72cfdabf01fcb8ea4314938768b0bc2aea5bafa6e67aface78fc021cc525ae60746d1ceac7ff33a2bf8e398c935252a5127f5090650dd69dd28861ee9becf6017a21ccb1b03f0a9aa15bf74eab5fd9727507b75c701f3",
+ "d5980482d666dde4f2c3a99b45e523fd6410be999a96ba8c5df397c950605e70",
+
+ "fece673103322483b85340e991e478c2c15e2d795a98adb5b697b4cf17a733898aaa4ffd11b1add300c9edb7a818740a33286fd8cf82140b0f7f2bde8d5bce94d58b6d697e5015c99a8df1c051d611b2c8c96a4c48a11eba9c08fe1aba2d4d31a617c75d9439e2cb4d4654ead346d52048ea26bb0c1c522a26db346de54639cac6f668c299919f43e09c1f1f78914abd7b32ac0f641c39c3749fd5be55cd1ac6fed1557ed683d1981c395946",
+ "17f4b2f60cb364da5e8a62db58e07eb1c44b888c433adc1e62461879cd271463",
+
+ "a542b2bdf8e04ec2a004cccd2f89e7bfd17ace1ad285c91360ac20e9913e3976a806000494c28b61b9d7ff36f342ad94d8d281d03e949d91fe8f4127f7b2ee1e550bcb13133a47c7be2400727cece45a4e1f95a3922e1269cc22950ca58bb7cb34b9da957d2fc81b3755982ad36dd238b9c8d33dd53a72c452cbe341a5afdca5ce79f730da8b5886add18f06feafbf57a33700430fa003c919f3f56dff08a5d3aab1e88c33353d30a700adad07",
+ "50cf700b5b6c802e20da4c1f9b75bd0a6632678212bd0e2418201f3a10389994",
+
+ "8fa67f49db80f22bc267a70e5636dfbc8a21c83d9691fe4b9c3051068b3fc9e94430e7fdfb712e4ce086e299ff5a104e65d7ceb685b4c46cda8eeb14cd3b9548d85baed5ec2f412810af3d034cd67a75c541f70829f8663c4d8cea3415621fb0954e5b3b756333a69a0a41b402522517f087ca9b4a06eba23f4fd5d02c5c6e07c132769660b50dadc5c07515ec751a1d2fd2cfd8b0855b85f602344fdbd28a37a52e874e73ccd627dbf9628cd1e8",
+ "3379265620eb781d6b59e331cc525e60e8c063e19f96cfabb2fda9aa83cdeba5",
+
+ "23ae9cd31da25c0187c0247be19e089872742d772f73d0efde5889c97b40d12ddbbec35b8f2b1f9c0b3d947708db3f2726306f4dd6ffabe37736f671bfc551835db0825adc6314e2cb479fe41b92497dc8638dcfbc0e3bf6f0b4c03dd418a892f1ad6138ccf442bc0e04cb2ae36a2f80a0340f63a849891190fc719781e0de44dedde95d2783b1121e9fa3b1280cf81af5cc7e7363579c1da03390e68fc5fc806e67a132b5bb6acd413eace2b120ac",
+ "a17a00ac106c0af50c4f449d3cdcc2cdbb9848d2d85a36ff434099162e25606c",
+
+ "3bfa57a5f9f60203059defd501977628908ee42116e4674dc0a52a32c5bac02aeb60c6714cd9c47c5a61558c21648884ccee85f76b637486f3709a698641c54bf5f5eb5b844f0ea0edae628ca73fb2d567710080e8a96c3fe83857fc738ac7b6639f0d8c28bfa617c56a60fd1b8fbdc36afe9ce3151e161fa5e3a71411fb8e123d48762bc093558aea7f950706bb72f8dc7ca3497a2b3ccf345ad3d9eafde10889d76c61d432e3a165d34ad0ee2d9619",
+ "1a2cfebf3483c33a5eba84121737d892cf8bd6c3ba324fd4ae4c2db42872e54f",
+
+ "e9b9525afd5634cf8d16df4ae7e12e8ae206c6ed6e7d4dd96f6fd75accf7a10cc22b023c7f569e4aec88dd51ca519c0a00c922ee33d3559b98a32d79067e6a9d50c182eed125de864841455be751991ea635c163ddbde6031223e2be0fd9f5253885bab81c4b5a4b4a4a00ae66698d8c7c538c9493c068d786f7dc710f90ac6c257f93e1884e7c609aaaf5927021e01d292a6bc87e6643e09b2505da2d2cf639bdb6f3b33cb8ab8fdf690b512d02fa9956",
+ "3ff47b4bf4f908aace95b0468a54b7e6644fe07df69ae327c0ff2e45325b97b9",
+
+ "13ec10c6b27a6ce6fdd5e2314e8626a28a69f313ec62f29b044cde1aff32e61228c252b9affe6a4ca93593a55932bc10aeb3f85b0c1d6c2c506d6c970e72e1f01c3aeede55cad3b1971111f60e1fcf48b5937c691952b691617f6a058ba73decf83b2b5e2b446ebfce52a24bf5b526f1a7f0c5659b6b96713f68208cfe38c2adc3af5361b9d5051c56de8fcc975d8bb48db41c7818cfd574f312d652f08f38dc857dac0e88e55e70379f20a37b7dc4396ec6",
+ "9703a69f279ef15b843b355f86b3f7098a46eafcad625920d93e0e3fb136fc5f",
+
+ "3d8263a177af8c5beabc76a4388e0816ab1bf1f5856e985791f15688feebe4ac6d480fa64999b339575be66d8e7c7435281b8c4ef990b86a00ac128e3c41b6b9c0e573c60af4c69391d408639d7de6815b38122731a6389d4f0534a587af82175ee3f5c963c8acb1bfaf434e0e9946436df9eb46d4bb0038a7842295873c300f6ecaff76fb1e4fdb0a75fef588d87cc486e67f738bd4f8832fb24526e5f0a8e91920f8967bfd96599aada321b4437049cc8836",
+ "e82d636a61c7657029699374a2da3dfabfae366e7708c7e4ba2dacd8b786a36f",
+
+ "01f793fa05548645f644a64ee1b5ff7fd38eaa233f874cd59f3ddf385e86b5e9f601b9b256f2f901864d61988d11c98593d7335543ab4d85731a3e39078c9e3012d5c6f83f064b5e7089c529a46dd5081efe66c8c49932cac5be88b57e674d689f98423389388446fb1f5969ee7029eebd29cbe489f8038edc5148148cbdca77e375b3cafc2fada07038a5c133c3cf21b881eb125c71c6b801fa03bdf9371b472792a3276094ce5417fb32973a0dcf87572d4db8",
+ "98bf0fd777137c94300ab5b1bff7b3f487a03a788e6bb96c715ba6f10ba1922b",
+
+ "71a986d2f662bf36dcbadbba0657f4e2797b569610e2d82271ee6d813f01f6db922a5a4ca405d9e7cddc9dfbb1129294b8c27845bea337250c2f721887045e50288ad513acd6a6be8dce300a308e2f8e600bd585fbf61dd2ebe45c4158ab18101c0f1eae789ecfc205d8bb6fed9371d65a9e94dd2fa5322ff75452851abfcc2357025ea56e24fbfb1d4266b34ee900768fc3dfd6c2761f4716c97d6a36092192c0abbc81f832d372be535b5dbd578576e6c2dbf61d",
+ "27255d504a38296857b8d382dc8ad4f1ca03ef3a8d1983e54bc01ef97b04e581",
+
+ "69ee06f5f53f74c76674751f8fa80efb42f43e71132ae0fc5ec6d2148c21570191e8baf0b9cd3547a57c103690d10d8ed84804d7b9b5cb9d5b35580a0f642abad5d0e5ca23ae3c32e1cc1355b8c7e5d78c7e64af47c6607dd960ea1d7d28b97c3d8ecdaab84a5131234cc6a68ef25e7d687ea62146c76845e02fd0745cd4cdf0d00bbab9020a3eec72e4714e9abb4029743012573d1fac9c798a513937d22ebd962df61f8854ca0ad67c5b7864885282b77df076b436",
+ "600b41954a9398ee66ea0e603c8c80d936fbc8be98c74f44ae13b0aa4b50b8d5",
+
+ "2a74e9800ce49aac07af3df2e451f245d4ffa5304c318574135eb7f39a064bcc8bf66fc8a4c8e2f5c6a9ac90495f0d28938ab301e9292fb78461aa23e87ad482712b1ed42f172983f4977e45aaba7f43ea8a9e7bcb91cc63f89c34cf06bf2a1404995e6e53d9569fb8011bd9af6b32de0289cd669b7043c19698bebd9bdd33ca6bca985cb81751913a70eb14ff790c41030eaa8a00cf7c1987dcaeb650ddd9eccf46326707d902a1a36c56be43ecf7b414a29caea3b55f",
+ "4e549f206099a8b3183fa3b86af220b1b6554ac3d8d52c54d093e68f60597256",
+
+ "5b2e2f2fd3ecc733a6198d34e5d143c176b60c3cc3dac6deafdf99fbce5cd088d583e8da4f01e7b09226f074f24613be345f691a46fb610b2d5855503ec761659152744db3a1a78f9b1fce7fdf584dbe28a52e04e40c701d3a62a13243b2af4a77e3fb106594afd7a84b52db16cf99ca3ad2808305d39a1dc043a52b45e7623e6f7da4accfa2a690a0f3a112fd739ee9522d891e111a8812a6448bc2ac2c234a616997a8579335c36d5fe6acfe0b052358fd715d70a7e104",
+ "24a3de94be98126ce95cfd3140754230b6880c71cfe4ec215c3f451bdc8bb690",
+
+ "013944b7958b6b3686b14bdb042f2f5b42768edc20fdd6a90894692b15f6e5157b9da9de23da95749524102f1bb150032343d6fbe64537e247162243fea59f95f53e95aff2a38f82775fbf06e7574475e9a2a8b8119aad1ebe3349543e8cef9239c410124c0fe2c6f409604aae4a92185c3a0efbeb26bfc63394e5451ed45d740dd823ef774615aad3caf9e2b9b1c25344b40facba11f5406fe1fefee6a571a33a22d42ebc6fb094de4c94b650b55c9068b7b3b3c783d7f53a",
+ "009661924d01ad811d4c598580eb954362b8554c5e9cd13686acbe41ac8c3940",
+
+ "72c2880163482bbe822cf72ff0e02be7081d271b366fd94c0cf37926925f76a9de44b086e590e7cc915773c314d336187ba9d03b866d1106b769b49fa99a4a9fa3fc74746d085504627a4792c757cde65b2fcaa82f9ff00eb81b7ab723ea1ed6e8723d92a2b65ead1e1dda64b275d897d0377c2ada0d5cab38913435a958da94d62f74a92da4e810ecc994017c344074014a50892fbe3e265f5448e2e2eb662295ba7f81b5dadc76f504dd31ce9debc517efad8cd5ba7fc754eb",
+ "77cf32d62a3d0622cd90f7c858ce1ae3bda60f9edc9cf50f7ecc9d7253d8d18d",
+
+ "c6dad2ff2cba3ed8873955178068b5704cbccf1e8c62eed472d275f726a7670a68ae2d6a763d943b30c616a27aab5a34e254feaf838093e828d8e905b5ca8decc39491fc8b9f8bfa050fe04e5198436f5593789ca8515ecdaeaf2ce905eafb3920b5851d32892cfd4e3d3e83ccd67707eea0c74bc47e56694c7ec609deb0b8d7c739913535a37e2c5377b5a9b40efee6f5a472269eae83a54a6d3dcf08c4ccb000473dac5a9489705be6cf28d1e7e1f2b2c60293008aee6aefa61b",
+ "8708b77ac39005607b179857c037f64860540e80ed7c7a4240e09ae62c88f87e",
+
+ "02553a2117e654ac28d948a6f67a83daf2089a95ff6631ff78131baa755cc36c4ad0ca6a51f5f176ea393a9bbf2b4af54deb12c6a0dfaec75da88dbc0655d34b7ad6fb0ebbb3c1e7f4fe3f94bb865683934d4fe7b53cc20b1016b7e68eab0cf1994e1735de888ba8500ea0b970f16e2acc159a1ec6e435739743e15194c53603af1f640640dd19600653a53368d55c92012b3b935c3fcfa6fc195325a00d192cc5332baa6b1831b81cb3952a2b9be6643a777a70feb5584d477f5489",
+ "376b551c1e8f908d7e1979efa436ab69013d2e85c34430dc826179b4f94480ae",
+
+ "9945c4f0e067b943986b6841b8fd21109e91d2f2549c711a11039abf03d37a6e4b34eba44a98e09c1b38046660c19e39424ab80ab38a805df648ee5c6212a72663322269c1de093325afe205d955ee2acf885146e5417432672ba807d5540c79e729b067cfa1faafbeb84947a91fd98a4d32e7cf712a15406b940feae5026f10e100dec5fb497cbaee3b83545a892701c530c0cddfac2a300a6b6c2a19829992589ff4accd3e57f9be20d65374f99f393e6a2467b82e7da94c9807f2fa",
+ "a4ab2e8f96b69097d84596b628e7bb76f460c001043ce5fa6e379fd29d1eabba",
+
+ "a4d7897eaf5c49979b361c39a67f47e26c2f75e5ffe0645539d4de245138eb8cadaa45aef7fa0c7a732dbbce90c85be2bd4bf6e37dfb4fdebee4d0e0671fc45c3051c6ccb674799bcfda7a431a6e93b3db3e32f30636190a9a2e5620302876e0d4d2f6201353fac4554341df6efb591c6f100f5dc21a2aa176ba592bd7db69e14237bbf2371df6bbb072f9ecb1f714e621c97768d82eea6bf98ebf4a82c005262188ff894a5dd549866f88b00ee82bd99872515d71fac230ccb472c55a60",
+ "9510ff5231813a865918badd0011f05915364165492ef17b85929a63e4951589",
+
+ "22813ee9edc5c2a90d8b3f07b48d9534e60f08312dc296d68fe78719bdb7478d8d037129aa182c4b8ae5bafca1604e76d5251ee43160ba68ddee9c624ebf00f0ba7ff6b1cf75b5cfa4ab323cf04ff13b7a591b23d06ed25f3c04c1baf4c8f7da913cf509c2a5053c4224ce4d0723268cbdf2277672b285c493731ea81799d353fa8497baed70c59a4c99b7b950a39470863a69667ff67c9ec981ddb41ffb3d63dd9d034bb79d9df1a95214083199e4efbd770a7a5f005ef5c877236674b6dd",
+ "44f8a8b05fc643566f1f53a93a122f7902d2cab68bb02267c0479339371a7304",
+
+ "eebfa2629596f61a926c4cd472ecb03eb2ecaf7f7650b12f7d2b8aa755284b7ccb295e46a62dd2a69577f38765ed1ea377bed34972470c5e3538cda310f2fd353334745a66f7557afb969e6c0132fdf4bb55e68951d5e25bc4fc2a9427e574de0d290d263ebc28a0ae11760caf85f63765fa0fc47ac2dc2c14c0c70404c9597f415050339443f2209430a2eed5acb1765df5768457d6a1db0ccbcc7a0e66531eb6f16608d1555c00973b4a9add70d5b88b8e44504fd9da709367627fad840bc5",
+ "9949d3ac3c05b4a08b85fa371811fd3f0b50c71950fef50acbb59c450ab1c587",
+
+ "ddf38f51b732aea3fdf1fe4c756d17961262163d737f407fad17e9724a19959a92425cbb099193ec38fca8edb0614eba4dbfda60b8a6ed102fec547289a22c3b74464a02023ada50647545f6f57959a37a85a4b5a70b2050e66416ad55c33cb50d6820cfaa16caf608c69d0e4a9d7f78211c3ae44b97216659e8f6cdb6640b30e50ea8c90a0bad06ac5678deb9b50962caec6494a930377b11debd77b46de2d382a2a8992902c9aad88d9e0d49a93f88fe5dec6dcbbfacb794b0335558c609c66e",
+ "954473b4965a57c4cbb20e199b8730487eb621f5fd694a1eb1667940da0d6728",
+
+ "184e1b9ccec71f837dca25838db073d51cacc26246fda091a468135d12e67faab69ac9d93e05bd9a687dad01c8db5bddc6751a45e64c2f734c867dd67f1e62626ddadc2baf7df0320f3e4c7e477a2b6f0ca679504b87372bb3a522e173fd8f7945f69ab9ab967ff378f6482293f3a936f82728abff188060e1ae48a778ebd09846d64cacb9b83487ad8bea1433b09ed791e06f7f8a65d2bbdf8a384f1550eb677962392b624bd593b6e77a7daf17d1fddfb995f472d8f5e4b41f3a02d394a98de583",
+ "0a7506e1b6cc43acdb4f2ec456e069e6e4b7608deb70dbe7ccb88578658be9da",
+
+ "c436d19f05550b6979bdc69bfd27ea4cd80c1a60f00a8b093e89178c7f9e8d492c304cf6ad59102bca0e0b23620338c15fc9ecd1e939ae91da16486f72ee1e154d41bfa391e6ba3b6ca9b3c3be39b5e61242ca5cd3d6c96cbd1170af91fdb2160db3522e1bc3b1a349d6e50479920ac5d9bedd8a16a787a3cdc2b6d24392f25555cc2f20b2ba9e6b47ddc96cfbd6df669d874ce21a758d3cf4704362ef7786d90ed67b01bd91299950058885accddbcf44e340ed4807864218653ee7ff7215aa1e1761",
+ "206be726fc681367387ff0a15303533058070f9655438ad8142cf39a0523b2ce",
+
+ "daf7c7526cdb85127df59220fbcb67dc5069ef58dc069a18a2e4ad164178dc0927cb1ae70120b0a975d78c4e1491dc228a95dc401873ec5645e7e6a8d0ffae58e8800be49f87b5f09d6caf4611ebd61bee86bb945325ae884a001b88b6be1a1c87de41503057bc6f5b7ba00fdb217d4de203335a746506371bf8f4bcddfd45df6bad65339bd9efaf18ce0ab1587bf842cfd6ec9c637b1cea1f96184e2b045a28fcb51e96c85574373d2b9335724170821ec58f6108af1929bea430458a1a7f80a2be1580",
+ "742389244ad26d7a16d1f2b01e9c83e987a283bbf3aa2907a556746fe8c98c38",
+
+ "597dadb776945e01c564f17eed4b5c1bbb34eebb13bce37d2d93363efe24b660f3785cc9e557dc2e4ab17a91a83d1f085060acc148508e43897993f66a20fbe65d46d3c4d9cf7e2e97e3952f0195f10ae8c20533753c719f6228d53d69a5e3c5fdafb9b039426d8716c2e961e09af9a8eb24a21b82c9b6192069a51ce3fc96843d7ab696edf9d0c42d151f2e2d95606ac14c2a80563c82392b02ab9abe6e3bab8471747ddc3cd06a46a6de9fd0ce4dd8d202466bdbe00088ebbb8ebfe341fbc2395a986df0",
+ "892985bdf2379f8ae138aac016894ee23408955d627cfa699fa5fa1439340a91",
+
+ "0efc14917a94f5320eb734c2b9e45f659d06c9f5c454deff0e76b30f6ee9e22e56a494a870fcdf138fc5538ce5bacf44761f993ccca4ae4ced8d576a8a10fd2979fe3e8066a641cdc5f746190ae4819e1d0d2886089bcbf6f36be44b5370afa45e523ba0c25bc169969436f1912b1c7b7a189d5edf00da050a5a813b31d09da5ede8b390ede30aeeece64a9ae05749e4758a2149b99d868219a056c18cf972370e07cdd95006c264ae33ab9e6130afdff6a9dbd1fe38747408868c65ccb4d45fa9f9b102528c",
+ "73088e0551c89477bcb675245c5c6347b4230390285832c7d723bf668c8061fb",
+
+ "9ac34ec974d28b18b7bcf6982eac60ebc670b0674e2acd697b49bfeb2fb81159fa5579a1e2a5bb8a5fc6ca46aaa5304a3771b15d804f2bef054fc1ad919e3852befea1c0bb74394f4d408d651412e247107bd32e64a23c9e593857f3a5ae253deea5104d8aa6ce108913881cf55d3c89587860027f8cc81b7eeec9e5f44e9fc190320c71d4a3427519250394d4ed07b9174f9e005b7696117c575fad05e76d86ae8cde5423d25d25076046f4392a0a7e56e8d6517fc66f265c5d617060e258354f9dce1dfe9de6",
+ "17cba68f47a0615b3513d28a44feda6ad36b6e6eb1ead7232f4e2a4e1a64bf50",
+
+ "d00df64c4bb9e2fd16fb6f9ca746d6cf162015ec7326e41a5d51e9b3d0792fed3f17d5bae34f03ec522e229d53304dcef105024ece941edeba410892846b2c7a1039ab82aa9750979a7bc70bf96d093bc3461b6f2d38f801380eccc286b562996cfce06d4a98b245176bc4ae4006f45eb36cc71636185acdfe429c0a7d5fbb927be7dc43685a0f40f185824ed102f57eeafe6d0d943e2d883564e233126f1eac648207ccafe651ce4f5169b35369f3e48f84771aedb2577b04fd0506ecef72305055cacfc4435e38",
+ "67302648e0082254d8d342b4eb8070ef9a44e0fc55c3d9a3f20613e4824aff21",
+
+ "fff5deb2bc7f43bd2db44ceff874e9c3b7c1a2f54cc6889f74186ca2a03d5047006b1b26e0919147379c81887df3403ebe43571fed8279607a2eb81a26d6f8f217dca3f927799ed182017c127069f2eb6f068b0d85979dc4d4867c676f6bedf36cd2def33b3e54a3366ea45478dee612f391a785bd0ede15aba921512103199228d434dbc1e899047a6861183e5b04fb716c11503dee2399261d10a0e5a76317736b0d7b6480573e76791b246ae734ee12203336ac3f539a6e6cb01c625eb3c9741dd199ca0d759753",
+ "bf64c9ab7042245fb2d8054edd699086dbe27a1ce904174d28bc0831ed9acf97",
+
+ "8d8001e2c096f1b88e7c9224a086efd4797fbf74a8033a2d422a2b6b8f6747e4",
+ "2e975f6a8a14f0704d51b13667d8195c219f71e6345696c49fa4b9d08e9225d3d39393425152c97e71dd24601c11abcfa0f12f53c680bd3ae757b8134a9c10d429615869217fdd5885c4db174985703a6d6de94a667eac3023443a8337ae1bc601b76d7d38ec3c34463105f0d3949d78e562a039e4469548b609395de5a4fd43c46ca9fd6ee29ada5efc07d84d553249450dab4a49c483ded250c9338f85cd937ae66bb436f3b4026e859fda1ca571432f3bfc09e7c03ca4d183b741111ca0483d0edabc03feb23b17ee48e844ba2408d9dcfd0139d2e8c7310125aee801c61ab7900d1efc47c078281766f361c5e6111346235e1dc38325666c",
+
+ NULL
+};
+
+static void
+test_SHAKE_KAT(int security_level, const char *const *kat)
+{
+ size_t u;
+
+ for (u = 0; kat[u] != NULL; u += 2) {
+ unsigned char msg[250], out[250], ref[250];
+ size_t msg_len, out_len, v;
+ br_shake_context sc;
+
+ msg_len = hextobin(msg, kat[u]);
+ out_len = hextobin(ref, kat[u + 1]);
+ br_shake_init(&sc, security_level);
+ br_shake_inject(&sc, msg, msg_len);
+ br_shake_flip(&sc);
+ br_shake_produce(&sc, out, out_len);
+ check_equals("KAT 1", out, ref, out_len);
+
+ br_shake_init(&sc, security_level);
+ for (v = 0; v < msg_len; v ++) {
+ br_shake_inject(&sc, msg + v, 1);
+ }
+ br_shake_flip(&sc);
+ br_shake_produce(&sc, out, out_len);
+ check_equals("KAT 2", out, ref, out_len);
+
+ br_shake_init(&sc, security_level);
+ br_shake_inject(&sc, msg, msg_len);
+ br_shake_flip(&sc);
+ for (v = 0; v < out_len; v ++) {
+ unsigned char x;
+
+ br_shake_produce(&sc, &x, 1);
+ if (x != ref[v]) {
+ fprintf(stderr, "KAT 3 (byte %u)\n",
+ (unsigned)v);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+}
+
+static void
+test_SHAKE_MonteCarlo(int security_level,
+ size_t minoutlen, size_t maxoutlen, const char *smsg, const char *sref)
+{
+ unsigned char out[250], ref[250];
+ size_t len, rlen, outlen, range;
+ int i, j;
+
+ hextobin(out, smsg);
+ outlen = maxoutlen;
+ range = maxoutlen - minoutlen + 1;
+ for (j = 0; j < 100; j ++) {
+ for (i = 1; i < 1001; i ++) {
+ br_shake_context sc;
+
+ len = outlen;
+ br_shake_init(&sc, security_level);
+ br_shake_inject(&sc, out, 16);
+ br_shake_flip(&sc);
+ br_shake_produce(&sc, out, len);
+ if (len < 16) {
+ memset(out + len, 0, 16 - len);
+ }
+ outlen = minoutlen
+ + (br_dec16be(out + len - 2) % range);
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ rlen = hextobin(ref, sref);
+ if (rlen != len) {
+ fprintf(stderr, "MC: bad length (%u vs %u)\n",
+ (unsigned)len, (unsigned)rlen);
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT MC", out, ref, len);
+}
+
+static void
+test_SHAKE(void)
+{
+ printf("Test SHAKE: ");
+ fflush(stdout);
+
+ test_SHAKE_KAT(128, KAT_SHAKE128);
+
+ printf(" ");
+ fflush(stdout);
+
+ test_SHAKE_MonteCarlo(128, 16, 140,
+ "c8b310cb97efa3855434998fa81c7674",
+ "4aa371f0099b04a909f9b1680e8b52a21c6510ea2640137d501ffa114bf84717b1f725d64bae4ae5d87a");
+
+ printf(" ");
+ fflush(stdout);
+
+ test_SHAKE_KAT(256, KAT_SHAKE256);
+
+ printf(" ");
+ fflush(stdout);
+
+ test_SHAKE_MonteCarlo(256, 2, 250,
+ "48a0321b3653e4e86446d00f6a036efd",
+ "d4c8c26ded38cca426d8d1c8f8aedb5c543541333839deca8713cfd8684480fe923f57c3a5c89cb61427c220c7");
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_HMAC_DRBG(void)
+{
+ br_hmac_drbg_context ctx;
+ unsigned char seed[42], tmp[30];
+ unsigned char ref1[30], ref2[30], ref3[30];
+ size_t seed_len;
+
+ printf("Test HMAC_DRBG: ");
+ fflush(stdout);
+
+ seed_len = hextobin(seed,
+ "009A4D6792295A7F730FC3F2B49CBC0F62E862272F"
+ "01795EDF0D54DB760F156D0DAC04C0322B3A204224");
+ hextobin(ref1,
+ "9305A46DE7FF8EB107194DEBD3FD48AA"
+ "20D5E7656CBE0EA69D2A8D4E7C67");
+ hextobin(ref2,
+ "C70C78608A3B5BE9289BE90EF6E81A9E"
+ "2C1516D5751D2F75F50033E45F73");
+ hextobin(ref3,
+ "475E80E992140567FCC3A50DAB90FE84"
+ "BCD7BB03638E9C4656A06F37F650");
+ br_hmac_drbg_init(&ctx, &br_sha256_vtable, seed, seed_len);
+ br_hmac_drbg_generate(&ctx, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 1", tmp, ref1, sizeof tmp);
+ br_hmac_drbg_generate(&ctx, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 2", tmp, ref2, sizeof tmp);
+ br_hmac_drbg_generate(&ctx, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 3", tmp, ref3, sizeof tmp);
+
+ memset(&ctx, 0, sizeof ctx);
+ br_hmac_drbg_vtable.init(&ctx.vtable,
+ &br_sha256_vtable, seed, seed_len);
+ ctx.vtable->generate(&ctx.vtable, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 4", tmp, ref1, sizeof tmp);
+ ctx.vtable->generate(&ctx.vtable, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 5", tmp, ref2, sizeof tmp);
+ ctx.vtable->generate(&ctx.vtable, tmp, sizeof tmp);
+ check_equals("KAT HMAC_DRBG 6", tmp, ref3, sizeof tmp);
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_AESCTR_DRBG(void)
+{
+ br_aesctr_drbg_context ctx;
+ const br_block_ctr_class *ictr;
+ unsigned char tmp1[64], tmp2[64];
+
+ printf("Test AESCTR_DRBG: ");
+ fflush(stdout);
+
+ ictr = br_aes_x86ni_ctr_get_vtable();
+ if (ictr == NULL) {
+ ictr = br_aes_pwr8_ctr_get_vtable();
+ if (ictr == NULL) {
+#if BR_64
+ ictr = &br_aes_ct64_ctr_vtable;
+#else
+ ictr = &br_aes_ct_ctr_vtable;
+#endif
+ }
+ }
+ br_aesctr_drbg_init(&ctx, ictr, NULL, 0);
+ ctx.vtable->generate(&ctx.vtable, tmp1, sizeof tmp1);
+ ctx.vtable->update(&ctx.vtable, "new seed", 8);
+ ctx.vtable->generate(&ctx.vtable, tmp2, sizeof tmp2);
+
+ if (memcmp(tmp1, tmp2, sizeof tmp1) == 0) {
+ fprintf(stderr, "AESCTR_DRBG failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+do_KAT_PRF(br_tls_prf_impl prf,
+ const char *ssecret, const char *label, const char *sseed,
+ const char *sref)
+{
+ unsigned char secret[100], seed[100], ref[500], out[500];
+ size_t secret_len, seed_len, ref_len;
+ br_tls_prf_seed_chunk chunks[2];
+
+ secret_len = hextobin(secret, ssecret);
+ seed_len = hextobin(seed, sseed);
+ ref_len = hextobin(ref, sref);
+
+ chunks[0].data = seed;
+ chunks[0].len = seed_len;
+ prf(out, ref_len, secret, secret_len, label, 1, chunks);
+ check_equals("TLS PRF KAT 1", out, ref, ref_len);
+
+ chunks[0].data = seed;
+ chunks[0].len = seed_len;
+ chunks[1].data = NULL;
+ chunks[1].len = 0;
+ prf(out, ref_len, secret, secret_len, label, 2, chunks);
+ check_equals("TLS PRF KAT 2", out, ref, ref_len);
+
+ chunks[0].data = NULL;
+ chunks[0].len = 0;
+ chunks[1].data = seed;
+ chunks[1].len = seed_len;
+ prf(out, ref_len, secret, secret_len, label, 2, chunks);
+ check_equals("TLS PRF KAT 3", out, ref, ref_len);
+
+ chunks[0].data = seed;
+ chunks[0].len = seed_len >> 1;
+ chunks[1].data = seed + chunks[0].len;
+ chunks[1].len = seed_len - chunks[0].len;
+ prf(out, ref_len, secret, secret_len, label, 2, chunks);
+ check_equals("TLS PRF KAT 4", out, ref, ref_len);
+}
+
+static void
+test_PRF(void)
+{
+ printf("Test TLS PRF: ");
+ fflush(stdout);
+
+ /*
+ * Test vector taken from an email that was on:
+ * http://www.imc.org/ietf-tls/mail-archive/msg01589.html
+ * but no longer exists there; a version archived in 2008
+ * can be found on http://www.archive.org/
+ */
+ do_KAT_PRF(&br_tls10_prf,
+ "abababababababababababababababababababababababababababababababababababababababababababababababab",
+ "PRF Testvector",
+ "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
+ "d3d4d1e349b5d515044666d51de32bab258cb521b6b053463e354832fd976754443bcf9a296519bc289abcbc1187e4ebd31e602353776c408aafb74cbc85eff69255f9788faa184cbb957a9819d84a5d7eb006eb459d3ae8de9810454b8b2d8f1afbc655a8c9a013");
+
+ /*
+ * Test vectors are taken from:
+ * https://www.ietf.org/mail-archive/web/tls/current/msg03416.html
+ */
+ do_KAT_PRF(&br_tls12_sha256_prf,
+ "9bbe436ba940f017b17652849a71db35",
+ "test label",
+ "a0ba9f936cda311827a6f796ffd5198c",
+ "e3f229ba727be17b8d122620557cd453c2aab21d07c3d495329b52d4e61edb5a6b301791e90d35c9c9a46b4e14baf9af0fa022f7077def17abfd3797c0564bab4fbc91666e9def9b97fce34f796789baa48082d122ee42c5a72e5a5110fff70187347b66");
+ do_KAT_PRF(&br_tls12_sha384_prf,
+ "b80b733d6ceefcdc71566ea48e5567df",
+ "test label",
+ "cd665cf6a8447dd6ff8b27555edb7465",
+ "7b0c18e9ced410ed1804f2cfa34a336a1c14dffb4900bb5fd7942107e81c83cde9ca0faa60be9fe34f82b1233c9146a0e534cb400fed2700884f9dc236f80edd8bfa961144c9e8d792eca722a7b32fc3d416d473ebc2c5fd4abfdad05d9184259b5bf8cd4d90fa0d31e2dec479e4f1a26066f2eea9a69236a3e52655c9e9aee691c8f3a26854308d5eaa3be85e0990703d73e56f");
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+/*
+ * AES known-answer tests. Order: key, plaintext, ciphertext.
+ */
+static const char *const KAT_AES[] = {
+ /*
+ * From FIPS-197.
+ */
+ "000102030405060708090a0b0c0d0e0f",
+ "00112233445566778899aabbccddeeff",
+ "69c4e0d86a7b0430d8cdb78070b4c55a",
+
+ "000102030405060708090a0b0c0d0e0f1011121314151617",
+ "00112233445566778899aabbccddeeff",
+ "dda97ca4864cdfe06eaf70a0ec0d7191",
+
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
+ "00112233445566778899aabbccddeeff",
+ "8ea2b7ca516745bfeafc49904b496089",
+
+ /*
+ * From NIST validation suite (ECBVarTxt128.rsp).
+ */
+ "00000000000000000000000000000000",
+ "80000000000000000000000000000000",
+ "3ad78e726c1ec02b7ebfe92b23d9ec34",
+
+ "00000000000000000000000000000000",
+ "c0000000000000000000000000000000",
+ "aae5939c8efdf2f04e60b9fe7117b2c2",
+
+ "00000000000000000000000000000000",
+ "e0000000000000000000000000000000",
+ "f031d4d74f5dcbf39daaf8ca3af6e527",
+
+ "00000000000000000000000000000000",
+ "f0000000000000000000000000000000",
+ "96d9fd5cc4f07441727df0f33e401a36",
+
+ "00000000000000000000000000000000",
+ "f8000000000000000000000000000000",
+ "30ccdb044646d7e1f3ccea3dca08b8c0",
+
+ "00000000000000000000000000000000",
+ "fc000000000000000000000000000000",
+ "16ae4ce5042a67ee8e177b7c587ecc82",
+
+ "00000000000000000000000000000000",
+ "fe000000000000000000000000000000",
+ "b6da0bb11a23855d9c5cb1b4c6412e0a",
+
+ "00000000000000000000000000000000",
+ "ff000000000000000000000000000000",
+ "db4f1aa530967d6732ce4715eb0ee24b",
+
+ "00000000000000000000000000000000",
+ "ff800000000000000000000000000000",
+ "a81738252621dd180a34f3455b4baa2f",
+
+ "00000000000000000000000000000000",
+ "ffc00000000000000000000000000000",
+ "77e2b508db7fd89234caf7939ee5621a",
+
+ "00000000000000000000000000000000",
+ "ffe00000000000000000000000000000",
+ "b8499c251f8442ee13f0933b688fcd19",
+
+ "00000000000000000000000000000000",
+ "fff00000000000000000000000000000",
+ "965135f8a81f25c9d630b17502f68e53",
+
+ "00000000000000000000000000000000",
+ "fff80000000000000000000000000000",
+ "8b87145a01ad1c6cede995ea3670454f",
+
+ "00000000000000000000000000000000",
+ "fffc0000000000000000000000000000",
+ "8eae3b10a0c8ca6d1d3b0fa61e56b0b2",
+
+ "00000000000000000000000000000000",
+ "fffe0000000000000000000000000000",
+ "64b4d629810fda6bafdf08f3b0d8d2c5",
+
+ "00000000000000000000000000000000",
+ "ffff0000000000000000000000000000",
+ "d7e5dbd3324595f8fdc7d7c571da6c2a",
+
+ "00000000000000000000000000000000",
+ "ffff8000000000000000000000000000",
+ "f3f72375264e167fca9de2c1527d9606",
+
+ "00000000000000000000000000000000",
+ "ffffc000000000000000000000000000",
+ "8ee79dd4f401ff9b7ea945d86666c13b",
+
+ "00000000000000000000000000000000",
+ "ffffe000000000000000000000000000",
+ "dd35cea2799940b40db3f819cb94c08b",
+
+ "00000000000000000000000000000000",
+ "fffff000000000000000000000000000",
+ "6941cb6b3e08c2b7afa581ebdd607b87",
+
+ "00000000000000000000000000000000",
+ "fffff800000000000000000000000000",
+ "2c20f439f6bb097b29b8bd6d99aad799",
+
+ "00000000000000000000000000000000",
+ "fffffc00000000000000000000000000",
+ "625d01f058e565f77ae86378bd2c49b3",
+
+ "00000000000000000000000000000000",
+ "fffffe00000000000000000000000000",
+ "c0b5fd98190ef45fbb4301438d095950",
+
+ "00000000000000000000000000000000",
+ "ffffff00000000000000000000000000",
+ "13001ff5d99806efd25da34f56be854b",
+
+ "00000000000000000000000000000000",
+ "ffffff80000000000000000000000000",
+ "3b594c60f5c8277a5113677f94208d82",
+
+ "00000000000000000000000000000000",
+ "ffffffc0000000000000000000000000",
+ "e9c0fc1818e4aa46bd2e39d638f89e05",
+
+ "00000000000000000000000000000000",
+ "ffffffe0000000000000000000000000",
+ "f8023ee9c3fdc45a019b4e985c7e1a54",
+
+ "00000000000000000000000000000000",
+ "fffffff0000000000000000000000000",
+ "35f40182ab4662f3023baec1ee796b57",
+
+ "00000000000000000000000000000000",
+ "fffffff8000000000000000000000000",
+ "3aebbad7303649b4194a6945c6cc3694",
+
+ "00000000000000000000000000000000",
+ "fffffffc000000000000000000000000",
+ "a2124bea53ec2834279bed7f7eb0f938",
+
+ "00000000000000000000000000000000",
+ "fffffffe000000000000000000000000",
+ "b9fb4399fa4facc7309e14ec98360b0a",
+
+ "00000000000000000000000000000000",
+ "ffffffff000000000000000000000000",
+ "c26277437420c5d634f715aea81a9132",
+
+ "00000000000000000000000000000000",
+ "ffffffff800000000000000000000000",
+ "171a0e1b2dd424f0e089af2c4c10f32f",
+
+ "00000000000000000000000000000000",
+ "ffffffffc00000000000000000000000",
+ "7cadbe402d1b208fe735edce00aee7ce",
+
+ "00000000000000000000000000000000",
+ "ffffffffe00000000000000000000000",
+ "43b02ff929a1485af6f5c6d6558baa0f",
+
+ "00000000000000000000000000000000",
+ "fffffffff00000000000000000000000",
+ "092faacc9bf43508bf8fa8613ca75dea",
+
+ "00000000000000000000000000000000",
+ "fffffffff80000000000000000000000",
+ "cb2bf8280f3f9742c7ed513fe802629c",
+
+ "00000000000000000000000000000000",
+ "fffffffffc0000000000000000000000",
+ "215a41ee442fa992a6e323986ded3f68",
+
+ "00000000000000000000000000000000",
+ "fffffffffe0000000000000000000000",
+ "f21e99cf4f0f77cea836e11a2fe75fb1",
+
+ "00000000000000000000000000000000",
+ "ffffffffff0000000000000000000000",
+ "95e3a0ca9079e646331df8b4e70d2cd6",
+
+ "00000000000000000000000000000000",
+ "ffffffffff8000000000000000000000",
+ "4afe7f120ce7613f74fc12a01a828073",
+
+ "00000000000000000000000000000000",
+ "ffffffffffc000000000000000000000",
+ "827f000e75e2c8b9d479beed913fe678",
+
+ "00000000000000000000000000000000",
+ "ffffffffffe000000000000000000000",
+ "35830c8e7aaefe2d30310ef381cbf691",
+
+ "00000000000000000000000000000000",
+ "fffffffffff000000000000000000000",
+ "191aa0f2c8570144f38657ea4085ebe5",
+
+ "00000000000000000000000000000000",
+ "fffffffffff800000000000000000000",
+ "85062c2c909f15d9269b6c18ce99c4f0",
+
+ "00000000000000000000000000000000",
+ "fffffffffffc00000000000000000000",
+ "678034dc9e41b5a560ed239eeab1bc78",
+
+ "00000000000000000000000000000000",
+ "fffffffffffe00000000000000000000",
+ "c2f93a4ce5ab6d5d56f1b93cf19911c1",
+
+ "00000000000000000000000000000000",
+ "ffffffffffff00000000000000000000",
+ "1c3112bcb0c1dcc749d799743691bf82",
+
+ "00000000000000000000000000000000",
+ "ffffffffffff80000000000000000000",
+ "00c55bd75c7f9c881989d3ec1911c0d4",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffc0000000000000000000",
+ "ea2e6b5ef182b7dff3629abd6a12045f",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffe0000000000000000000",
+ "22322327e01780b17397f24087f8cc6f",
+
+ "00000000000000000000000000000000",
+ "fffffffffffff0000000000000000000",
+ "c9cacb5cd11692c373b2411768149ee7",
+
+ "00000000000000000000000000000000",
+ "fffffffffffff8000000000000000000",
+ "a18e3dbbca577860dab6b80da3139256",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffc000000000000000000",
+ "79b61c37bf328ecca8d743265a3d425c",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffe000000000000000000",
+ "d2d99c6bcc1f06fda8e27e8ae3f1ccc7",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffff000000000000000000",
+ "1bfd4b91c701fd6b61b7f997829d663b",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffff800000000000000000",
+ "11005d52f25f16bdc9545a876a63490a",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffc00000000000000000",
+ "3a4d354f02bb5a5e47d39666867f246a",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffe00000000000000000",
+ "d451b8d6e1e1a0ebb155fbbf6e7b7dc3",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffff00000000000000000",
+ "6898d4f42fa7ba6a10ac05e87b9f2080",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffff80000000000000000",
+ "b611295e739ca7d9b50f8e4c0e754a3f",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffc0000000000000000",
+ "7d33fc7d8abe3ca1936759f8f5deaf20",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffe0000000000000000",
+ "3b5e0f566dc96c298f0c12637539b25c",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffff0000000000000000",
+ "f807c3e7985fe0f5a50e2cdb25c5109e",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffff8000000000000000",
+ "41f992a856fb278b389a62f5d274d7e9",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffc000000000000000",
+ "10d3ed7a6fe15ab4d91acbc7d0767ab1",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffe000000000000000",
+ "21feecd45b2e675973ac33bf0c5424fc",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffff000000000000000",
+ "1480cb3955ba62d09eea668f7c708817",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffff800000000000000",
+ "66404033d6b72b609354d5496e7eb511",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffc00000000000000",
+ "1c317a220a7d700da2b1e075b00266e1",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffe00000000000000",
+ "ab3b89542233f1271bf8fd0c0f403545",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffff00000000000000",
+ "d93eae966fac46dca927d6b114fa3f9e",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffff80000000000000",
+ "1bdec521316503d9d5ee65df3ea94ddf",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffc0000000000000",
+ "eef456431dea8b4acf83bdae3717f75f",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffe0000000000000",
+ "06f2519a2fafaa596bfef5cfa15c21b9",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffff0000000000000",
+ "251a7eac7e2fe809e4aa8d0d7012531a",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffff8000000000000",
+ "3bffc16e4c49b268a20f8d96a60b4058",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffc000000000000",
+ "e886f9281999c5bb3b3e8862e2f7c988",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffe000000000000",
+ "563bf90d61beef39f48dd625fcef1361",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffff000000000000",
+ "4d37c850644563c69fd0acd9a049325b",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffff800000000000",
+ "b87c921b91829ef3b13ca541ee1130a6",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffc00000000000",
+ "2e65eb6b6ea383e109accce8326b0393",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffe00000000000",
+ "9ca547f7439edc3e255c0f4d49aa8990",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffff00000000000",
+ "a5e652614c9300f37816b1f9fd0c87f9",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffff80000000000",
+ "14954f0b4697776f44494fe458d814ed",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffc0000000000",
+ "7c8d9ab6c2761723fe42f8bb506cbcf7",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffe0000000000",
+ "db7e1932679fdd99742aab04aa0d5a80",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffff0000000000",
+ "4c6a1c83e568cd10f27c2d73ded19c28",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffff8000000000",
+ "90ecbe6177e674c98de412413f7ac915",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffc000000000",
+ "90684a2ac55fe1ec2b8ebd5622520b73",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffe000000000",
+ "7472f9a7988607ca79707795991035e6",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffff000000000",
+ "56aff089878bf3352f8df172a3ae47d8",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffff800000000",
+ "65c0526cbe40161b8019a2a3171abd23",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffc00000000",
+ "377be0be33b4e3e310b4aabda173f84f",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffe00000000",
+ "9402e9aa6f69de6504da8d20c4fcaa2f",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffff00000000",
+ "123c1f4af313ad8c2ce648b2e71fb6e1",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffff80000000",
+ "1ffc626d30203dcdb0019fb80f726cf4",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffc0000000",
+ "76da1fbe3a50728c50fd2e621b5ad885",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffe0000000",
+ "082eb8be35f442fb52668e16a591d1d6",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffff0000000",
+ "e656f9ecf5fe27ec3e4a73d00c282fb3",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffff8000000",
+ "2ca8209d63274cd9a29bb74bcd77683a",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffc000000",
+ "79bf5dce14bb7dd73a8e3611de7ce026",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffe000000",
+ "3c849939a5d29399f344c4a0eca8a576",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffff000000",
+ "ed3c0a94d59bece98835da7aa4f07ca2",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffff800000",
+ "63919ed4ce10196438b6ad09d99cd795",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffc00000",
+ "7678f3a833f19fea95f3c6029e2bc610",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffe00000",
+ "3aa426831067d36b92be7c5f81c13c56",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffff00000",
+ "9272e2d2cdd11050998c845077a30ea0",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffff80000",
+ "088c4b53f5ec0ff814c19adae7f6246c",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffc0000",
+ "4010a5e401fdf0a0354ddbcc0d012b17",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffe0000",
+ "a87a385736c0a6189bd6589bd8445a93",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff0000",
+ "545f2b83d9616dccf60fa9830e9cd287",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff8000",
+ "4b706f7f92406352394037a6d4f4688d",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffc000",
+ "b7972b3941c44b90afa7b264bfba7387",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffe000",
+ "6f45732cf10881546f0fd23896d2bb60",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff000",
+ "2e3579ca15af27f64b3c955a5bfc30ba",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff800",
+ "34a2c5a91ae2aec99b7d1b5fa6780447",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffc00",
+ "a4d6616bd04f87335b0e53351227a9ee",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffe00",
+ "7f692b03945867d16179a8cefc83ea3f",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff00",
+ "3bd141ee84a0e6414a26e7a4f281f8a2",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff80",
+ "d1788f572d98b2b16ec5d5f3922b99bc",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffc0",
+ "0833ff6f61d98a57b288e8c3586b85a6",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffe0",
+ "8568261797de176bf0b43becc6285afb",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff0",
+ "f9b0fda0c4a898f5b9e6f661c4ce4d07",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff8",
+ "8ade895913685c67c5269f8aae42983e",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffc",
+ "39bde67d5c8ed8a8b1c37eb8fa9f5ac0",
+
+ "00000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffe",
+ "5c005e72c1418c44f569f2ea33ba54f3",
+
+ "00000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffff",
+ "3f5b8cc9ea855a0afa7347d23e8d664e",
+
+ /*
+ * From NIST validation suite (ECBVarTxt192.rsp).
+ */
+ "000000000000000000000000000000000000000000000000",
+ "80000000000000000000000000000000",
+ "6cd02513e8d4dc986b4afe087a60bd0c",
+
+ "000000000000000000000000000000000000000000000000",
+ "c0000000000000000000000000000000",
+ "2ce1f8b7e30627c1c4519eada44bc436",
+
+ "000000000000000000000000000000000000000000000000",
+ "e0000000000000000000000000000000",
+ "9946b5f87af446f5796c1fee63a2da24",
+
+ "000000000000000000000000000000000000000000000000",
+ "f0000000000000000000000000000000",
+ "2a560364ce529efc21788779568d5555",
+
+ "000000000000000000000000000000000000000000000000",
+ "f8000000000000000000000000000000",
+ "35c1471837af446153bce55d5ba72a0a",
+
+ "000000000000000000000000000000000000000000000000",
+ "fc000000000000000000000000000000",
+ "ce60bc52386234f158f84341e534cd9e",
+
+ "000000000000000000000000000000000000000000000000",
+ "fe000000000000000000000000000000",
+ "8c7c27ff32bcf8dc2dc57c90c2903961",
+
+ "000000000000000000000000000000000000000000000000",
+ "ff000000000000000000000000000000",
+ "32bb6a7ec84499e166f936003d55a5bb",
+
+ "000000000000000000000000000000000000000000000000",
+ "ff800000000000000000000000000000",
+ "a5c772e5c62631ef660ee1d5877f6d1b",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffc00000000000000000000000000000",
+ "030d7e5b64f380a7e4ea5387b5cd7f49",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffe00000000000000000000000000000",
+ "0dc9a2610037009b698f11bb7e86c83e",
+
+ "000000000000000000000000000000000000000000000000",
+ "fff00000000000000000000000000000",
+ "0046612c766d1840c226364f1fa7ed72",
+
+ "000000000000000000000000000000000000000000000000",
+ "fff80000000000000000000000000000",
+ "4880c7e08f27befe78590743c05e698b",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffc0000000000000000000000000000",
+ "2520ce829a26577f0f4822c4ecc87401",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffe0000000000000000000000000000",
+ "8765e8acc169758319cb46dc7bcf3dca",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffff0000000000000000000000000000",
+ "e98f4ba4f073df4baa116d011dc24a28",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffff8000000000000000000000000000",
+ "f378f68c5dbf59e211b3a659a7317d94",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffc000000000000000000000000000",
+ "283d3b069d8eb9fb432d74b96ca762b4",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffe000000000000000000000000000",
+ "a7e1842e8a87861c221a500883245c51",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffff000000000000000000000000000",
+ "77aa270471881be070fb52c7067ce732",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffff800000000000000000000000000",
+ "01b0f476d484f43f1aeb6efa9361a8ac",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffc00000000000000000000000000",
+ "1c3a94f1c052c55c2d8359aff2163b4f",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffe00000000000000000000000000",
+ "e8a067b604d5373d8b0f2e05a03b341b",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffff00000000000000000000000000",
+ "a7876ec87f5a09bfea42c77da30fd50e",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffff80000000000000000000000000",
+ "0cf3e9d3a42be5b854ca65b13f35f48d",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffc0000000000000000000000000",
+ "6c62f6bbcab7c3e821c9290f08892dda",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffe0000000000000000000000000",
+ "7f5e05bd2068738196fee79ace7e3aec",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffff0000000000000000000000000",
+ "440e0d733255cda92fb46e842fe58054",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffff8000000000000000000000000",
+ "aa5d5b1c4ea1b7a22e5583ac2e9ed8a7",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffc000000000000000000000000",
+ "77e537e89e8491e8662aae3bc809421d",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffe000000000000000000000000",
+ "997dd3e9f1598bfa73f75973f7e93b76",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffff000000000000000000000000",
+ "1b38d4f7452afefcb7fc721244e4b72e",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffff800000000000000000000000",
+ "0be2b18252e774dda30cdda02c6906e3",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffc00000000000000000000000",
+ "d2695e59c20361d82652d7d58b6f11b2",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffe00000000000000000000000",
+ "902d88d13eae52089abd6143cfe394e9",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffff00000000000000000000000",
+ "d49bceb3b823fedd602c305345734bd2",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffff80000000000000000000000",
+ "707b1dbb0ffa40ef7d95def421233fae",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffc0000000000000000000000",
+ "7ca0c1d93356d9eb8aa952084d75f913",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffe0000000000000000000000",
+ "f2cbf9cb186e270dd7bdb0c28febc57d",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffff0000000000000000000000",
+ "c94337c37c4e790ab45780bd9c3674a0",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffff8000000000000000000000",
+ "8e3558c135252fb9c9f367ed609467a1",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffc000000000000000000000",
+ "1b72eeaee4899b443914e5b3a57fba92",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffe000000000000000000000",
+ "011865f91bc56868d051e52c9efd59b7",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffff000000000000000000000",
+ "e4771318ad7a63dd680f6e583b7747ea",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffff800000000000000000000",
+ "61e3d194088dc8d97e9e6db37457eac5",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffc00000000000000000000",
+ "36ff1ec9ccfbc349e5d356d063693ad6",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffe00000000000000000000",
+ "3cc9e9a9be8cc3f6fb2ea24088e9bb19",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffff00000000000000000000",
+ "1ee5ab003dc8722e74905d9a8fe3d350",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffff80000000000000000000",
+ "245339319584b0a412412869d6c2eada",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffc0000000000000000000",
+ "7bd496918115d14ed5380852716c8814",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffe0000000000000000000",
+ "273ab2f2b4a366a57d582a339313c8b1",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffff0000000000000000000",
+ "113365a9ffbe3b0ca61e98507554168b",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffff8000000000000000000",
+ "afa99c997ac478a0dea4119c9e45f8b1",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffc000000000000000000",
+ "9216309a7842430b83ffb98638011512",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffe000000000000000000",
+ "62abc792288258492a7cb45145f4b759",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffff000000000000000000",
+ "534923c169d504d7519c15d30e756c50",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffff800000000000000000",
+ "fa75e05bcdc7e00c273fa33f6ee441d2",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffc00000000000000000",
+ "7d350fa6057080f1086a56b17ec240db",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffe00000000000000000",
+ "f34e4a6324ea4a5c39a661c8fe5ada8f",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffff00000000000000000",
+ "0882a16f44088d42447a29ac090ec17e",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffff80000000000000000",
+ "3a3c15bfc11a9537c130687004e136ee",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffc0000000000000000",
+ "22c0a7678dc6d8cf5c8a6d5a9960767c",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffe0000000000000000",
+ "b46b09809d68b9a456432a79bdc2e38c",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffff0000000000000000",
+ "93baaffb35fbe739c17c6ac22eecf18f",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffff8000000000000000",
+ "c8aa80a7850675bc007c46df06b49868",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffc000000000000000",
+ "12c6f3877af421a918a84b775858021d",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffe000000000000000",
+ "33f123282c5d633924f7d5ba3f3cab11",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffff000000000000000",
+ "a8f161002733e93ca4527d22c1a0c5bb",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffff800000000000000",
+ "b72f70ebf3e3fda23f508eec76b42c02",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffc00000000000000",
+ "6a9d965e6274143f25afdcfc88ffd77c",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffe00000000000000",
+ "a0c74fd0b9361764ce91c5200b095357",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffff00000000000000",
+ "091d1fdc2bd2c346cd5046a8c6209146",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffff80000000000000",
+ "e2a37580116cfb71856254496ab0aca8",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffc0000000000000",
+ "e0b3a00785917c7efc9adba322813571",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffe0000000000000",
+ "733d41f4727b5ef0df4af4cf3cffa0cb",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffff0000000000000",
+ "a99ebb030260826f981ad3e64490aa4f",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffff8000000000000",
+ "73f34c7d3eae5e80082c1647524308ee",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffc000000000000",
+ "40ebd5ad082345b7a2097ccd3464da02",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffe000000000000",
+ "7cc4ae9a424b2cec90c97153c2457ec5",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffff000000000000",
+ "54d632d03aba0bd0f91877ebdd4d09cb",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffff800000000000",
+ "d3427be7e4d27cd54f5fe37b03cf0897",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffc00000000000",
+ "b2099795e88cc158fd75ea133d7e7fbe",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffe00000000000",
+ "a6cae46fb6fadfe7a2c302a34242817b",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffff00000000000",
+ "026a7024d6a902e0b3ffccbaa910cc3f",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffff80000000000",
+ "156f07767a85a4312321f63968338a01",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffc0000000000",
+ "15eec9ebf42b9ca76897d2cd6c5a12e2",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffe0000000000",
+ "db0d3a6fdcc13f915e2b302ceeb70fd8",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffff0000000000",
+ "71dbf37e87a2e34d15b20e8f10e48924",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffff8000000000",
+ "c745c451e96ff3c045e4367c833e3b54",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffc000000000",
+ "340da09c2dd11c3b679d08ccd27dd595",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffe000000000",
+ "8279f7c0c2a03ee660c6d392db025d18",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffff000000000",
+ "a4b2c7d8eba531ff47c5041a55fbd1ec",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffff800000000",
+ "74569a2ca5a7bd5131ce8dc7cbfbf72f",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffc00000000",
+ "3713da0c0219b63454035613b5a403dd",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffe00000000",
+ "8827551ddcc9df23fa72a3de4e9f0b07",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffff00000000",
+ "2e3febfd625bfcd0a2c06eb460da1732",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffff80000000",
+ "ee82e6ba488156f76496311da6941deb",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffc0000000",
+ "4770446f01d1f391256e85a1b30d89d3",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffe0000000",
+ "af04b68f104f21ef2afb4767cf74143c",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffff0000000",
+ "cf3579a9ba38c8e43653173e14f3a4c6",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffff8000000",
+ "b3bba904f4953e09b54800af2f62e7d4",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffc000000",
+ "fc4249656e14b29eb9c44829b4c59a46",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffe000000",
+ "9b31568febe81cfc2e65af1c86d1a308",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffff000000",
+ "9ca09c25f273a766db98a480ce8dfedc",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffff800000",
+ "b909925786f34c3c92d971883c9fbedf",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffc00000",
+ "82647f1332fe570a9d4d92b2ee771d3b",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffe00000",
+ "3604a7e80832b3a99954bca6f5b9f501",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffff00000",
+ "884607b128c5de3ab39a529a1ef51bef",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffff80000",
+ "670cfa093d1dbdb2317041404102435e",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffc0000",
+ "7a867195f3ce8769cbd336502fbb5130",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffe0000",
+ "52efcf64c72b2f7ca5b3c836b1078c15",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff0000",
+ "4019250f6eefb2ac5ccbcae044e75c7e",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff8000",
+ "022c4f6f5a017d292785627667ddef24",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffc000",
+ "e9c21078a2eb7e03250f71000fa9e3ed",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffe000",
+ "a13eaeeb9cd391da4e2b09490b3e7fad",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff000",
+ "c958a171dca1d4ed53e1af1d380803a9",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff800",
+ "21442e07a110667f2583eaeeee44dc8c",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffc00",
+ "59bbb353cf1dd867a6e33737af655e99",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffe00",
+ "43cd3b25375d0ce41087ff9fe2829639",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff00",
+ "6b98b17e80d1118e3516bd768b285a84",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff80",
+ "ae47ed3676ca0c08deea02d95b81db58",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffc0",
+ "34ec40dc20413795ed53628ea748720b",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffe0",
+ "4dc68163f8e9835473253542c8a65d46",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff0",
+ "2aabb999f43693175af65c6c612c46fb",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff8",
+ "e01f94499dac3547515c5b1d756f0f58",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffc",
+ "9d12435a46480ce00ea349f71799df9a",
+
+ "000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffe",
+ "cef41d16d266bdfe46938ad7884cc0cf",
+
+ "000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffff",
+ "b13db4da1f718bc6904797c82bcf2d32",
+
+ /*
+ * From NIST validation suite (ECBVarTxt256.rsp).
+ */
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "80000000000000000000000000000000",
+ "ddc6bf790c15760d8d9aeb6f9a75fd4e",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "c0000000000000000000000000000000",
+ "0a6bdc6d4c1e6280301fd8e97ddbe601",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "e0000000000000000000000000000000",
+ "9b80eefb7ebe2d2b16247aa0efc72f5d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "f0000000000000000000000000000000",
+ "7f2c5ece07a98d8bee13c51177395ff7",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "f8000000000000000000000000000000",
+ "7818d800dcf6f4be1e0e94f403d1e4c2",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fc000000000000000000000000000000",
+ "e74cd1c92f0919c35a0324123d6177d3",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fe000000000000000000000000000000",
+ "8092a4dcf2da7e77e93bdd371dfed82e",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ff000000000000000000000000000000",
+ "49af6b372135acef10132e548f217b17",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ff800000000000000000000000000000",
+ "8bcd40f94ebb63b9f7909676e667f1e7",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffc00000000000000000000000000000",
+ "fe1cffb83f45dcfb38b29be438dbd3ab",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffe00000000000000000000000000000",
+ "0dc58a8d886623705aec15cb1e70dc0e",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fff00000000000000000000000000000",
+ "c218faa16056bd0774c3e8d79c35a5e4",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fff80000000000000000000000000000",
+ "047bba83f7aa841731504e012208fc9e",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffc0000000000000000000000000000",
+ "dc8f0e4915fd81ba70a331310882f6da",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffe0000000000000000000000000000",
+ "1569859ea6b7206c30bf4fd0cbfac33c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffff0000000000000000000000000000",
+ "300ade92f88f48fa2df730ec16ef44cd",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffff8000000000000000000000000000",
+ "1fe6cc3c05965dc08eb0590c95ac71d0",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffc000000000000000000000000000",
+ "59e858eaaa97fec38111275b6cf5abc0",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffe000000000000000000000000000",
+ "2239455e7afe3b0616100288cc5a723b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffff000000000000000000000000000",
+ "3ee500c5c8d63479717163e55c5c4522",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffff800000000000000000000000000",
+ "d5e38bf15f16d90e3e214041d774daa8",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffc00000000000000000000000000",
+ "b1f4066e6f4f187dfe5f2ad1b17819d0",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffe00000000000000000000000000",
+ "6ef4cc4de49b11065d7af2909854794a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffff00000000000000000000000000",
+ "ac86bc606b6640c309e782f232bf367f",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffff80000000000000000000000000",
+ "36aff0ef7bf3280772cf4cac80a0d2b2",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffc0000000000000000000000000",
+ "1f8eedea0f62a1406d58cfc3ecea72cf",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffe0000000000000000000000000",
+ "abf4154a3375a1d3e6b1d454438f95a6",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffff0000000000000000000000000",
+ "96f96e9d607f6615fc192061ee648b07",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffff8000000000000000000000000",
+ "cf37cdaaa0d2d536c71857634c792064",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffc000000000000000000000000",
+ "fbd6640c80245c2b805373f130703127",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffe000000000000000000000000",
+ "8d6a8afe55a6e481badae0d146f436db",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffff000000000000000000000000",
+ "6a4981f2915e3e68af6c22385dd06756",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffff800000000000000000000000",
+ "42a1136e5f8d8d21d3101998642d573b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffc00000000000000000000000",
+ "9b471596dc69ae1586cee6158b0b0181",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffe00000000000000000000000",
+ "753665c4af1eff33aa8b628bf8741cfd",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffff00000000000000000000000",
+ "9a682acf40be01f5b2a4193c9a82404d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffff80000000000000000000000",
+ "54fafe26e4287f17d1935f87eb9ade01",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffc0000000000000000000000",
+ "49d541b2e74cfe73e6a8e8225f7bd449",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffe0000000000000000000000",
+ "11a45530f624ff6f76a1b3826626ff7b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffff0000000000000000000000",
+ "f96b0c4a8bc6c86130289f60b43b8fba",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffff8000000000000000000000",
+ "48c7d0e80834ebdc35b6735f76b46c8b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffc000000000000000000000",
+ "2463531ab54d66955e73edc4cb8eaa45",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffe000000000000000000000",
+ "ac9bd8e2530469134b9d5b065d4f565b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffff000000000000000000000",
+ "3f5f9106d0e52f973d4890e6f37e8a00",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffff800000000000000000000",
+ "20ebc86f1304d272e2e207e59db639f0",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffc00000000000000000000",
+ "e67ae6426bf9526c972cff072b52252c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffe00000000000000000000",
+ "1a518dddaf9efa0d002cc58d107edfc8",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffff00000000000000000000",
+ "ead731af4d3a2fe3b34bed047942a49f",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffff80000000000000000000",
+ "b1d4efe40242f83e93b6c8d7efb5eae9",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffc0000000000000000000",
+ "cd2b1fec11fd906c5c7630099443610a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffe0000000000000000000",
+ "a1853fe47fe29289d153161d06387d21",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffff0000000000000000000",
+ "4632154179a555c17ea604d0889fab14",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffff8000000000000000000",
+ "dd27cac6401a022e8f38f9f93e774417",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffc000000000000000000",
+ "c090313eb98674f35f3123385fb95d4d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffe000000000000000000",
+ "cc3526262b92f02edce548f716b9f45c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffff000000000000000000",
+ "c0838d1a2b16a7c7f0dfcc433c399c33",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffff800000000000000000",
+ "0d9ac756eb297695eed4d382eb126d26",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffc00000000000000000",
+ "56ede9dda3f6f141bff1757fa689c3e1",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffe00000000000000000",
+ "768f520efe0f23e61d3ec8ad9ce91774",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffff00000000000000000",
+ "b1144ddfa75755213390e7c596660490",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffff80000000000000000",
+ "1d7c0c4040b355b9d107a99325e3b050",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffc0000000000000000",
+ "d8e2bb1ae8ee3dcf5bf7d6c38da82a1a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffe0000000000000000",
+ "faf82d178af25a9886a47e7f789b98d7",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffff0000000000000000",
+ "9b58dbfd77fe5aca9cfc190cd1b82d19",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffff8000000000000000",
+ "77f392089042e478ac16c0c86a0b5db5",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffc000000000000000",
+ "19f08e3420ee69b477ca1420281c4782",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffe000000000000000",
+ "a1b19beee4e117139f74b3c53fdcb875",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffff000000000000000",
+ "a37a5869b218a9f3a0868d19aea0ad6a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffff800000000000000",
+ "bc3594e865bcd0261b13202731f33580",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffc00000000000000",
+ "811441ce1d309eee7185e8c752c07557",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffe00000000000000",
+ "959971ce4134190563518e700b9874d1",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffff00000000000000",
+ "76b5614a042707c98e2132e2e805fe63",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffff80000000000000",
+ "7d9fa6a57530d0f036fec31c230b0cc6",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffc0000000000000",
+ "964153a83bf6989a4ba80daa91c3e081",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffe0000000000000",
+ "a013014d4ce8054cf2591d06f6f2f176",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffff0000000000000",
+ "d1c5f6399bf382502e385eee1474a869",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffff8000000000000",
+ "0007e20b8298ec354f0f5fe7470f36bd",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffc000000000000",
+ "b95ba05b332da61ef63a2b31fcad9879",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffe000000000000",
+ "4620a49bd967491561669ab25dce45f4",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffff000000000000",
+ "12e71214ae8e04f0bb63d7425c6f14d5",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffff800000000000",
+ "4cc42fc1407b008fe350907c092e80ac",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffc00000000000",
+ "08b244ce7cbc8ee97fbba808cb146fda",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffe00000000000",
+ "39b333e8694f21546ad1edd9d87ed95b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffff00000000000",
+ "3b271f8ab2e6e4a20ba8090f43ba78f3",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffff80000000000",
+ "9ad983f3bf651cd0393f0a73cccdea50",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffc0000000000",
+ "8f476cbff75c1f725ce18e4bbcd19b32",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffe0000000000",
+ "905b6267f1d6ab5320835a133f096f2a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffff0000000000",
+ "145b60d6d0193c23f4221848a892d61a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffff8000000000",
+ "55cfb3fb6d75cad0445bbc8dafa25b0f",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffc000000000",
+ "7b8e7098e357ef71237d46d8b075b0f5",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffe000000000",
+ "2bf27229901eb40f2df9d8398d1505ae",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffff000000000",
+ "83a63402a77f9ad5c1e931a931ecd706",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffff800000000",
+ "6f8ba6521152d31f2bada1843e26b973",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffc00000000",
+ "e5c3b8e30fd2d8e6239b17b44bd23bbd",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffe00000000",
+ "1ac1f7102c59933e8b2ddc3f14e94baa",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffff00000000",
+ "21d9ba49f276b45f11af8fc71a088e3d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffff80000000",
+ "649f1cddc3792b4638635a392bc9bade",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffc0000000",
+ "e2775e4b59c1bc2e31a2078c11b5a08c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffe0000000",
+ "2be1fae5048a25582a679ca10905eb80",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffff0000000",
+ "da86f292c6f41ea34fb2068df75ecc29",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffff8000000",
+ "220df19f85d69b1b562fa69a3c5beca5",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffc000000",
+ "1f11d5d0355e0b556ccdb6c7f5083b4d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffe000000",
+ "62526b78be79cb384633c91f83b4151b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffff000000",
+ "90ddbcb950843592dd47bbef00fdc876",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffff800000",
+ "2fd0e41c5b8402277354a7391d2618e2",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffc00000",
+ "3cdf13e72dee4c581bafec70b85f9660",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffe00000",
+ "afa2ffc137577092e2b654fa199d2c43",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffff00000",
+ "8d683ee63e60d208e343ce48dbc44cac",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffff80000",
+ "705a4ef8ba2133729c20185c3d3a4763",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffc0000",
+ "0861a861c3db4e94194211b77ed761b9",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffe0000",
+ "4b00c27e8b26da7eab9d3a88dec8b031",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff0000",
+ "5f397bf03084820cc8810d52e5b666e9",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffff8000",
+ "63fafabb72c07bfbd3ddc9b1203104b8",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffc000",
+ "683e2140585b18452dd4ffbb93c95df9",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffe000",
+ "286894e48e537f8763b56707d7d155c8",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff000",
+ "a423deabc173dcf7e2c4c53e77d37cd1",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffff800",
+ "eb8168313e1cfdfdb5e986d5429cf172",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffc00",
+ "27127daafc9accd2fb334ec3eba52323",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffe00",
+ "ee0715b96f72e3f7a22a5064fc592f4c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff00",
+ "29ee526770f2a11dcfa989d1ce88830f",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffff80",
+ "0493370e054b09871130fe49af730a5a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffc0",
+ "9b7b940f6c509f9e44a4ee140448ee46",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffe0",
+ "2915be4a1ecfdcbe3e023811a12bb6c7",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff0",
+ "7240e524bc51d8c4d440b1be55d1062c",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffff8",
+ "da63039d38cb4612b2dc36ba26684b93",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffc",
+ "0f59cb5a4b522e2ac56c1a64f558ad9a",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "fffffffffffffffffffffffffffffffe",
+ "7bfe9d876c6d63c1d035da8fe21c409d",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "ffffffffffffffffffffffffffffffff",
+ "acdace8078a32b1a182bfa4987ca1347",
+
+ /*
+ * Table end marker.
+ */
+ NULL
+};
+
+/*
+ * AES known-answer tests for CBC. Order: key, IV, plaintext, ciphertext.
+ */
+static const char *const KAT_AES_CBC[] = {
+ /*
+ * From NIST validation suite "Multiblock Message Test"
+ * (cbcmmt128.rsp).
+ */
+ "1f8e4973953f3fb0bd6b16662e9a3c17",
+ "2fe2b333ceda8f98f4a99b40d2cd34a8",
+ "45cf12964fc824ab76616ae2f4bf0822",
+ "0f61c4d44c5147c03c195ad7e2cc12b2",
+
+ "0700d603a1c514e46b6191ba430a3a0c",
+ "aad1583cd91365e3bb2f0c3430d065bb",
+ "068b25c7bfb1f8bdd4cfc908f69dffc5ddc726a197f0e5f720f730393279be91",
+ "c4dc61d9725967a3020104a9738f23868527ce839aab1752fd8bdb95a82c4d00",
+
+ "3348aa51e9a45c2dbe33ccc47f96e8de",
+ "19153c673160df2b1d38c28060e59b96",
+ "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5e1847a6ad5d54127a399ab07ee3599",
+ "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded97818e1f127a28d72db5652749f0c6afee5",
+
+ "b7f3c9576e12dd0db63e8f8fac2b9a39",
+ "c80f095d8bb1a060699f7c19974a1aa0",
+ "9ac19954ce1319b354d3220460f71c1e373f1cd336240881160cfde46ebfed2e791e8d5a1a136ebd1dc469dec00c4187722b841cdabcb22c1be8a14657da200e",
+ "19b9609772c63f338608bf6eb52ca10be65097f89c1e0905c42401fd47791ae2c5440b2d473116ca78bd9ff2fb6015cfd316524eae7dcb95ae738ebeae84a467",
+
+ "b6f9afbfe5a1562bba1368fc72ac9d9c",
+ "3f9d5ebe250ee7ce384b0d00ee849322",
+ "db397ec22718dbffb9c9d13de0efcd4611bf792be4fce0dc5f25d4f577ed8cdbd4eb9208d593dda3d4653954ab64f05676caa3ce9bfa795b08b67ceebc923fdc89a8c431188e9e482d8553982cf304d1",
+ "10ea27b19e16b93af169c4a88e06e35c99d8b420980b058e34b4b8f132b13766f72728202b089f428fecdb41c79f8aa0d0ef68f5786481cca29e2126f69bc14160f1ae2187878ba5c49cf3961e1b7ee9",
+
+ "bbe7b7ba07124ff1ae7c3416fe8b465e",
+ "7f65b5ee3630bed6b84202d97fb97a1e",
+ "2aad0c2c4306568bad7447460fd3dac054346d26feddbc9abd9110914011b4794be2a9a00a519a51a5b5124014f4ed2735480db21b434e99a911bb0b60fe0253763725b628d5739a5117b7ee3aefafc5b4c1bf446467e7bf5f78f31ff7caf187",
+ "3b8611bfc4973c5cd8e982b073b33184cd26110159172e44988eb5ff5661a1e16fad67258fcbfee55469267a12dc374893b4e3533d36f5634c3095583596f135aa8cd1138dc898bc5651ee35a92ebf89ab6aeb5366653bc60a70e0074fc11efe",
+
+ "89a553730433f7e6d67d16d373bd5360",
+ "f724558db3433a523f4e51a5bea70497",
+ "807bc4ea684eedcfdcca30180680b0f1ae2814f35f36d053c5aea6595a386c1442770f4d7297d8b91825ee7237241da8925dd594ccf676aecd46ca2068e8d37a3a0ec8a7d5185a201e663b5ff36ae197110188a23503763b8218826d23ced74b31e9f6e2d7fbfa6cb43420c7807a8625",
+ "406af1429a478c3d07e555c5287a60500d37fc39b68e5bbb9bafd6ddb223828561d6171a308d5b1a4551e8a5e7d572918d25c968d3871848d2f16635caa9847f38590b1df58ab5efb985f2c66cfaf86f61b3f9c0afad6c963c49cee9b8bc81a2ddb06c967f325515a4849eec37ce721a",
+
+ "c491ca31f91708458e29a925ec558d78",
+ "9ef934946e5cd0ae97bd58532cb49381",
+ "cb6a787e0dec56f9a165957f81af336ca6b40785d9e94093c6190e5152649f882e874d79ac5e167bd2a74ce5ae088d2ee854f6539e0a94796b1e1bd4c9fcdbc79acbef4d01eeb89776d18af71ae2a4fc47dd66df6c4dbe1d1850e466549a47b636bcc7c2b3a62495b56bb67b6d455f1eebd9bfefecbca6c7f335cfce9b45cb9d",
+ "7b2931f5855f717145e00f152a9f4794359b1ffcb3e55f594e33098b51c23a6c74a06c1d94fded7fd2ae42c7db7acaef5844cb33aeddc6852585ed0020a6699d2cb53809cefd169148ce42292afab063443978306c582c18b9ce0da3d084ce4d3c482cfd8fcf1a85084e89fb88b40a084d5e972466d07666126fb761f84078f2",
+
+ "f6e87d71b0104d6eb06a68dc6a71f498",
+ "1c245f26195b76ebebc2edcac412a2f8",
+ "f82bef3c73a6f7f80db285726d691db6bf55eec25a859d3ba0e0445f26b9bb3b16a3161ed1866e4dd8f2e5f8ecb4e46d74a7a78c20cdfc7bcc9e479ba7a0caba9438238ad0c01651d5d98de37f03ddce6e6b4bd4ab03cf9e8ed818aedfa1cf963b932067b97d776dce1087196e7e913f7448e38244509f0caf36bd8217e15336d35c149fd4e41707893fdb84014f8729",
+ "b09512f3eff9ed0d85890983a73dadbb7c3678d52581be64a8a8fc586f490f2521297a478a0598040ebd0f5509fafb0969f9d9e600eaef33b1b93eed99687b167f89a5065aac439ce46f3b8d22d30865e64e45ef8cd30b6984353a844a11c8cd60dba0e8866b3ee30d24b3fa8a643b328353e06010fa8273c8fd54ef0a2b6930e5520aae5cd5902f9b86a33592ca4365",
+
+ "2c14413751c31e2730570ba3361c786b",
+ "1dbbeb2f19abb448af849796244a19d7",
+ "40d930f9a05334d9816fe204999c3f82a03f6a0457a8c475c94553d1d116693adc618049f0a769a2eed6a6cb14c0143ec5cccdbc8dec4ce560cfd206225709326d4de7948e54d603d01b12d7fed752fb23f1aa4494fbb00130e9ded4e77e37c079042d828040c325b1a5efd15fc842e44014ca4374bf38f3c3fc3ee327733b0c8aee1abcd055772f18dc04603f7b2c1ea69ff662361f2be0a171bbdcea1e5d3f",
+ "6be8a12800455a320538853e0cba31bd2d80ea0c85164a4c5c261ae485417d93effe2ebc0d0a0b51d6ea18633d210cf63c0c4ddbc27607f2e81ed9113191ef86d56f3b99be6c415a4150299fb846ce7160b40b63baf1179d19275a2e83698376d28b92548c68e06e6d994e2c1501ed297014e702cdefee2f656447706009614d801de1caaf73f8b7fa56cf1ba94b631933bbe577624380850f117435a0355b2b",
+
+ /*
+ * From NIST validation suite "Multiblock Message Test"
+ * (cbcmmt192.rsp).
+ */
+ "ba75f4d1d9d7cf7f551445d56cc1a8ab2a078e15e049dc2c",
+ "531ce78176401666aa30db94ec4a30eb",
+ "c51fc276774dad94bcdc1d2891ec8668",
+ "70dd95a14ee975e239df36ff4aee1d5d",
+
+ "eab3b19c581aa873e1981c83ab8d83bbf8025111fb2e6b21",
+ "f3d6667e8d4d791e60f7505ba383eb05",
+ "9d4e4cccd1682321856df069e3f1c6fa391a083a9fb02d59db74c14081b3acc4",
+ "51d44779f90d40a80048276c035cb49ca2a47bcb9b9cf7270b9144793787d53f",
+
+ "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
+ "eaaeca2e07ddedf562f94df63f0a650f",
+ "c5ce958613bf741718c17444484ebaf1050ddcacb59b9590178cbe69d7ad7919608cb03af13bbe04f3506b718a301ea0",
+ "ed6a50e0c6921d52d6647f75d67b4fd56ace1fedb8b5a6a997b4d131640547d22c5d884a75e6752b5846b5b33a5181f4",
+
+ "067bb17b4df785697eaccf961f98e212cb75e6797ce935cb",
+ "8b59c9209c529ca8391c9fc0ce033c38",
+ "db3785a889b4bd387754da222f0e4c2d2bfe0d79e05bc910fba941beea30f1239eacf0068f4619ec01c368e986fca6b7c58e490579d29611bd10087986eff54f",
+ "d5f5589760bf9c762228fde236de1fa2dd2dad448db3fa9be0c4196efd46a35c84dd1ac77d9db58c95918cb317a6430a08d2fb6a8e8b0f1c9b72c7a344dc349f",
+
+ "0fd39de83e0be77a79c8a4a612e3dd9c8aae2ce35e7a2bf8",
+ "7e1d629b84f93b079be51f9a5f5cb23c",
+ "38fbda37e28fa86d9d83a4345e419dea95d28c7818ff25925db6ac3aedaf0a86154e20a4dfcc5b1b4192895393e5eb5846c88bdbd41ecf7af3104f410eaee470f5d9017ed460475f626953035a13db1f",
+ "edadae2f9a45ff3473e02d904c94d94a30a4d92da4deb6bcb4b0774472694571842039f21c496ef93fd658842c735f8a81fcd0aa578442ab893b18f606aed1bab11f81452dd45e9b56adf2eccf4ea095",
+
+ "e3fecc75f0075a09b383dfd389a3d33cc9b854b3b254c0f4",
+ "36eab883afef936cc38f63284619cd19",
+ "931b2f5f3a5820d53a6beaaa6431083a3488f4eb03b0f5b57ef838e1579623103bd6e6800377538b2e51ef708f3c4956432e8a8ee6a34e190642b26ad8bdae6c2af9a6c7996f3b6004d2671e41f1c9f40ee03d1c4a52b0a0654a331f15f34dce",
+ "75395974bd32b3665654a6c8e396b88ae34b123575872a7ab687d8e76b46df911a8a590cd01d2f5c330be3a6626e9dd3aa5e10ed14e8ff829811b6fed50f3f533ca4385a1cbca78f5c4744e50f2f8359165c2485d1324e76c3eae76a0ccac629",
+
+ "f9c27565eb07947c8cb51b79248430f7b1066c3d2fdc3d13",
+ "2bd67cc89ab7948d644a49672843cbd9",
+ "6abcc270173cf114d44847e911a050db57ba7a2e2c161c6f37ccb6aaa4677bddcaf50cad0b5f8758fcf7c0ebc650ceb5cd52cafb8f8dd3edcece55d9f1f08b9fa8f54365cf56e28b9596a7e1dd1d3418e4444a7724add4cf79d527b183ec88de4be4eeff29c80a97e54f85351cb189ee",
+ "ca282924a61187feb40520979106e5cc861957f23828dcb7285e0eaac8a0ca2a6b60503d63d6039f4693dba32fa1f73ae2e709ca94911f28a5edd1f30eaddd54680c43acc9c74cd90d8bb648b4e544275f47e514daa20697f66c738eb30337f017fca1a26da4d1a0cc0a0e98e2463070",
+
+ "fb09cf9e00dbf883689d079c920077c0073c31890b55bab5",
+ "e3c89bd097c3abddf64f4881db6dbfe2",
+ "c1a37683fb289467dd1b2c89efba16bbd2ee24cf18d19d44596ded2682c79a2f711c7a32bf6a24badd32a4ee637c73b7a41da6258635650f91fb9ffa45bdfc3cb122136241b3deced8996aa51ea8d3e81c9d70e006a44bc0571ed48623a0d622a93fa9da290baaedf5d9e876c94620945ff8ecc83f27379ed55cf490c5790f27",
+ "8158e21420f25b59d6ae943fa1cbf21f02e979f419dab0126a721b7eef55bee9ad97f5ccff7d239057bbc19a8c378142f7672f1d5e7e17d7bebcb0070e8355cace6660171a53b61816ae824a6ef69ce470b6ffd3b5bb4b438874d91d27854d3b6f25860d3868958de3307d62b1339bdddb8a318c0ce0f33c17caf0e9f6040820",
+
+ "bca6fa3c67fd294e958f66fe8bd64f45f428f5bc8e9733a7",
+ "92a47f2833f1450d1da41717bdc6e83c",
+ "5becbc31d8bead6d36ae014a5863d14a431e6b55d29ea6baaa417271716db3a33b2e506b452086dfe690834ac2de30bc41254ec5401ec47d064237c7792fdcd7914d8af20eb114756642d519021a8c75a92f6bc53d326ae9a5b7e1b10a9756574692934d9939fc399e0c203f7edf8e7e6482eadd31a0400770e897b48c6bca2b404593045080e93377358c42a0f4dede",
+ "926db248cc1ba20f0c57631a7c8aef094f791937b905949e3460240e8bfa6fa483115a1b310b6e4369caebc5262888377b1ddaa5800ea496a2bdff0f9a1031e7129c9a20e35621e7f0b8baca0d87030f2ae7ca8593c8599677a06fd4b26009ead08fecac24caa9cf2cad3b470c8227415a7b1e0f2eab3fad96d70a209c8bb26c627677e2531b9435ca6e3c444d195b5f",
+
+ "162ad50ee64a0702aa551f571dedc16b2c1b6a1e4d4b5eee",
+ "24408038161a2ccae07b029bb66355c1",
+ "be8abf00901363987a82cc77d0ec91697ba3857f9e4f84bd79406c138d02698f003276d0449120bef4578d78fecabe8e070e11710b3f0a2744bd52434ec70015884c181ebdfd51c604a71c52e4c0e110bc408cd462b248a80b8a8ac06bb952ac1d7faed144807f1a731b7febcaf7835762defe92eccfc7a9944e1c702cffe6bc86733ed321423121085ac02df8962bcbc1937092eebf0e90a8b20e3dd8c244ae",
+ "c82cf2c476dea8cb6a6e607a40d2f0391be82ea9ec84a537a6820f9afb997b76397d005424faa6a74dc4e8c7aa4a8900690f894b6d1dca80675393d2243adac762f159301e357e98b724762310cd5a7bafe1c2a030dba46fd93a9fdb89cc132ca9c17dc72031ec6822ee5a9d99dbca66c784c01b0885cbb62e29d97801927ec415a5d215158d325f9ee689437ad1b7684ad33c0d92739451ac87f39ff8c31b84",
+
+ /*
+ * From NIST validation suite "Multiblock Message Test"
+ * (cbcmmt256.rsp).
+ */
+ "6ed76d2d97c69fd1339589523931f2a6cff554b15f738f21ec72dd97a7330907",
+ "851e8764776e6796aab722dbb644ace8",
+ "6282b8c05c5c1530b97d4816ca434762",
+ "6acc04142e100a65f51b97adf5172c41",
+
+ "dce26c6b4cfb286510da4eecd2cffe6cdf430f33db9b5f77b460679bd49d13ae",
+ "fdeaa134c8d7379d457175fd1a57d3fc",
+ "50e9eee1ac528009e8cbcd356975881f957254b13f91d7c6662d10312052eb00",
+ "2fa0df722a9fd3b64cb18fb2b3db55ff2267422757289413f8f657507412a64c",
+
+ "fe8901fecd3ccd2ec5fdc7c7a0b50519c245b42d611a5ef9e90268d59f3edf33",
+ "bd416cb3b9892228d8f1df575692e4d0",
+ "8d3aa196ec3d7c9b5bb122e7fe77fb1295a6da75abe5d3a510194d3a8a4157d5c89d40619716619859da3ec9b247ced9",
+ "608e82c7ab04007adb22e389a44797fed7de090c8c03ca8a2c5acd9e84df37fbc58ce8edb293e98f02b640d6d1d72464",
+
+ "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
+ "c0cd2bebccbb6c49920bd5482ac756e8",
+ "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4567dd68e8ccd4c38ac563b13639c",
+ "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364c389fd639bdda647daa3bdadab2eb5594",
+
+ "9adc8fbd506e032af7fa20cf5343719de6d1288c158c63d6878aaf64ce26ca85",
+ "11958dc6ab81e1c7f01631e9944e620f",
+ "c7917f84f747cd8c4b4fedc2219bdbc5f4d07588389d8248854cf2c2f89667a2d7bcf53e73d32684535f42318e24cd45793950b3825e5d5c5c8fcd3e5dda4ce9246d18337ef3052d8b21c5561c8b660e",
+ "9c99e68236bb2e929db1089c7750f1b356d39ab9d0c40c3e2f05108ae9d0c30b04832ccdbdc08ebfa426b7f5efde986ed05784ce368193bb3699bc691065ac62e258b9aa4cc557e2b45b49ce05511e65",
+
+ "73b8faf00b3302ac99855cf6f9e9e48518690a5906a4869d4dcf48d282faae2a",
+ "b3cb97a80a539912b8c21f450d3b9395",
+ "3adea6e06e42c4f041021491f2775ef6378cb08824165edc4f6448e232175b60d0345b9f9c78df6596ec9d22b7b9e76e8f3c76b32d5d67273f1d83fe7a6fc3dd3c49139170fa5701b3beac61b490f0a9e13f844640c4500f9ad3087adfb0ae10",
+ "ac3d6dbafe2e0f740632fd9e820bf6044cd5b1551cbb9cc03c0b25c39ccb7f33b83aacfca40a3265f2bbff879153448acacb88fcfb3bb7b10fe463a68c0109f028382e3e557b1adf02ed648ab6bb895df0205d26ebbfa9a5fd8cebd8e4bee3dc",
+
+ "9ddf3745896504ff360a51a3eb49c01b79fccebc71c3abcb94a949408b05b2c9",
+ "e79026639d4aa230b5ccffb0b29d79bc",
+ "cf52e5c3954c51b94c9e38acb8c9a7c76aebdaa9943eae0a1ce155a2efdb4d46985d935511471452d9ee64d2461cb2991d59fc0060697f9a671672163230f367fed1422316e52d29eceacb8768f56d9b80f6d278093c9a8acd3cfd7edd8ebd5c293859f64d2f8486ae1bd593c65bc014",
+ "34df561bd2cfebbcb7af3b4b8d21ca5258312e7e2e4e538e35ad2490b6112f0d7f148f6aa8d522a7f3c61d785bd667db0e1dc4606c318ea4f26af4fe7d11d4dcff0456511b4aed1a0d91ba4a1fd6cd9029187bc5881a5a07fe02049d39368e83139b12825bae2c7be81e6f12c61bb5c5",
+
+ "458b67bf212d20f3a57fce392065582dcefbf381aa22949f8338ab9052260e1d",
+ "4c12effc5963d40459602675153e9649",
+ "256fd73ce35ae3ea9c25dd2a9454493e96d8633fe633b56176dce8785ce5dbbb84dbf2c8a2eeb1e96b51899605e4f13bbc11b93bf6f39b3469be14858b5b720d4a522d36feed7a329c9b1e852c9280c47db8039c17c4921571a07d1864128330e09c308ddea1694e95c84500f1a61e614197e86a30ecc28df64ccb3ccf5437aa",
+ "90b7b9630a2378f53f501ab7beff039155008071bc8438e789932cfd3eb1299195465e6633849463fdb44375278e2fdb1310821e6492cf80ff15cb772509fb426f3aeee27bd4938882fd2ae6b5bd9d91fa4a43b17bb439ebbe59c042310163a82a5fe5388796eee35a181a1271f00be29b852d8fa759bad01ff4678f010594cd",
+
+ "d2412db0845d84e5732b8bbd642957473b81fb99ca8bff70e7920d16c1dbec89",
+ "51c619fcf0b23f0c7925f400a6cacb6d",
+ "026006c4a71a180c9929824d9d095b8faaa86fc4fa25ecac61d85ff6de92dfa8702688c02a282c1b8af4449707f22d75e91991015db22374c95f8f195d5bb0afeb03040ff8965e0e1339dba5653e174f8aa5a1b39fe3ac839ce307a4e44b4f8f1b0063f738ec18acdbff2ebfe07383e734558723e741f0a1836dafdf9de82210a9248bc113b3c1bc8b4e252ca01bd803",
+ "0254b23463bcabec5a395eb74c8fb0eb137a07bc6f5e9f61ec0b057de305714f8fa294221c91a159c315939b81e300ee902192ec5f15254428d8772f79324ec43298ca21c00b370273ee5e5ed90e43efa1e05a5d171209fe34f9f29237dba2a6726650fd3b1321747d1208863c6c3c6b3e2d879ab5f25782f08ba8f2abbe63e0bedb4a227e81afb36bb6645508356d34",
+
+ "48be597e632c16772324c8d3fa1d9c5a9ecd010f14ec5d110d3bfec376c5532b",
+ "d6d581b8cf04ebd3b6eaa1b53f047ee1",
+ "0c63d413d3864570e70bb6618bf8a4b9585586688c32bba0a5ecc1362fada74ada32c52acfd1aa7444ba567b4e7daaecf7cc1cb29182af164ae5232b002868695635599807a9a7f07a1f137e97b1e1c9dabc89b6a5e4afa9db5855edaa575056a8f4f8242216242bb0c256310d9d329826ac353d715fa39f80cec144d6424558f9f70b98c920096e0f2c855d594885a00625880e9dfb734163cecef72cf030b8",
+ "fc5873e50de8faf4c6b84ba707b0854e9db9ab2e9f7d707fbba338c6843a18fc6facebaf663d26296fb329b4d26f18494c79e09e779647f9bafa87489630d79f4301610c2300c19dbf3148b7cac8c4f4944102754f332e92b6f7c5e75bc6179eb877a078d4719009021744c14f13fd2a55a2b9c44d18000685a845a4f632c7c56a77306efa66a24d05d088dcd7c13fe24fc447275965db9e4d37fbc9304448cd",
+
+ /*
+ * End-of-table marker.
+ */
+ NULL
+};
+
+/*
+ * AES known-answer tests for CTR. Order: key, IV, plaintext, ciphertext.
+ */
+static const char *const KAT_AES_CTR[] = {
+ /*
+ * From RFC 3686.
+ */
+ "ae6852f8121067cc4bf7a5765577f39e",
+ "000000300000000000000000",
+ "53696e676c6520626c6f636b206d7367",
+ "e4095d4fb7a7b3792d6175a3261311b8",
+
+ "7e24067817fae0d743d6ce1f32539163",
+ "006cb6dbc0543b59da48d90b",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
+ "5104a106168a72d9790d41ee8edad388eb2e1efc46da57c8fce630df9141be28",
+
+ "7691be035e5020a8ac6e618529f9a0dc",
+ "00e0017b27777f3f4a1786f0",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223",
+ "c1cf48a89f2ffdd9cf4652e9efdb72d74540a42bde6d7836d59a5ceaaef3105325b2072f",
+
+ "16af5b145fc9f579c175f93e3bfb0eed863d06ccfdb78515",
+ "0000004836733c147d6d93cb",
+ "53696e676c6520626c6f636b206d7367",
+ "4b55384fe259c9c84e7935a003cbe928",
+
+ "7c5cb2401b3dc33c19e7340819e0f69c678c3db8e6f6a91a",
+ "0096b03b020c6eadc2cb500d",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
+ "453243fc609b23327edfaafa7131cd9f8490701c5ad4a79cfc1fe0ff42f4fb00",
+
+ "02bf391ee8ecb159b959617b0965279bf59b60a786d3e0fe",
+ "0007bdfd5cbd60278dcc0912",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223",
+ "96893fc55e5c722f540b7dd1ddf7e758d288bc95c69165884536c811662f2188abee0935",
+
+ "776beff2851db06f4c8a0542c8696f6c6a81af1eec96b4d37fc1d689e6c1c104",
+ "00000060db5672c97aa8f0b2",
+ "53696e676c6520626c6f636b206d7367",
+ "145ad01dbf824ec7560863dc71e3e0c0",
+
+ "f6d66d6bd52d59bb0796365879eff886c66dd51a5b6a99744b50590c87a23884",
+ "00faac24c1585ef15a43d875",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
+ "f05e231b3894612c49ee000b804eb2a9b8306b508f839d6a5530831d9344af1c",
+
+ "ff7a617ce69148e4f1726e2f43581de2aa62d9f805532edff1eed687fb54153d",
+ "001cc5b751a51d70a1c11148",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223",
+ "eb6c52821d0bbbf7ce7594462aca4faab407df866569fd07f48cc0b583d6071f1ec0e6b8",
+
+ /*
+ * End-of-table marker.
+ */
+ NULL
+};
+
+static void
+monte_carlo_AES_encrypt(const br_block_cbcenc_class *ve,
+ char *skey, char *splain, char *scipher)
+{
+ unsigned char key[32];
+ unsigned char buf[16];
+ unsigned char pbuf[16];
+ unsigned char cipher[16];
+ size_t key_len;
+ int i, j, k;
+ br_aes_gen_cbcenc_keys v_ec;
+ const br_block_cbcenc_class **ec;
+
+ ec = &v_ec.vtable;
+ key_len = hextobin(key, skey);
+ hextobin(buf, splain);
+ hextobin(cipher, scipher);
+ for (i = 0; i < 100; i ++) {
+ ve->init(ec, key, key_len);
+ for (j = 0; j < 1000; j ++) {
+ unsigned char iv[16];
+
+ memcpy(pbuf, buf, sizeof buf);
+ memset(iv, 0, sizeof iv);
+ ve->run(ec, iv, buf, sizeof buf);
+ }
+ switch (key_len) {
+ case 16:
+ for (k = 0; k < 16; k ++) {
+ key[k] ^= buf[k];
+ }
+ break;
+ case 24:
+ for (k = 0; k < 8; k ++) {
+ key[k] ^= pbuf[8 + k];
+ }
+ for (k = 0; k < 16; k ++) {
+ key[8 + k] ^= buf[k];
+ }
+ break;
+ default:
+ for (k = 0; k < 16; k ++) {
+ key[k] ^= pbuf[k];
+ key[16 + k] ^= buf[k];
+ }
+ break;
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+ check_equals("MC AES encrypt", buf, cipher, sizeof buf);
+}
+
+static void
+monte_carlo_AES_decrypt(const br_block_cbcdec_class *vd,
+ char *skey, char *scipher, char *splain)
+{
+ unsigned char key[32];
+ unsigned char buf[16];
+ unsigned char pbuf[16];
+ unsigned char plain[16];
+ size_t key_len;
+ int i, j, k;
+ br_aes_gen_cbcdec_keys v_dc;
+ const br_block_cbcdec_class **dc;
+
+ dc = &v_dc.vtable;
+ key_len = hextobin(key, skey);
+ hextobin(buf, scipher);
+ hextobin(plain, splain);
+ for (i = 0; i < 100; i ++) {
+ vd->init(dc, key, key_len);
+ for (j = 0; j < 1000; j ++) {
+ unsigned char iv[16];
+
+ memcpy(pbuf, buf, sizeof buf);
+ memset(iv, 0, sizeof iv);
+ vd->run(dc, iv, buf, sizeof buf);
+ }
+ switch (key_len) {
+ case 16:
+ for (k = 0; k < 16; k ++) {
+ key[k] ^= buf[k];
+ }
+ break;
+ case 24:
+ for (k = 0; k < 8; k ++) {
+ key[k] ^= pbuf[8 + k];
+ }
+ for (k = 0; k < 16; k ++) {
+ key[8 + k] ^= buf[k];
+ }
+ break;
+ default:
+ for (k = 0; k < 16; k ++) {
+ key[k] ^= pbuf[k];
+ key[16 + k] ^= buf[k];
+ }
+ break;
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+ check_equals("MC AES decrypt", buf, plain, sizeof buf);
+}
+
+static void
+test_AES_generic(char *name,
+ const br_block_cbcenc_class *ve,
+ const br_block_cbcdec_class *vd,
+ const br_block_ctr_class *vc,
+ int with_MC, int with_CBC)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ if (ve->block_size != 16 || vd->block_size != 16
+ || ve->log_block_size != 4 || vd->log_block_size != 4)
+ {
+ fprintf(stderr, "%s failed: wrong block size\n", name);
+ exit(EXIT_FAILURE);
+ }
+
+ for (u = 0; KAT_AES[u]; u += 3) {
+ unsigned char key[32];
+ unsigned char plain[16];
+ unsigned char cipher[16];
+ unsigned char buf[16];
+ unsigned char iv[16];
+ size_t key_len;
+ br_aes_gen_cbcenc_keys v_ec;
+ br_aes_gen_cbcdec_keys v_dc;
+ const br_block_cbcenc_class **ec;
+ const br_block_cbcdec_class **dc;
+
+ ec = &v_ec.vtable;
+ dc = &v_dc.vtable;
+ key_len = hextobin(key, KAT_AES[u]);
+ hextobin(plain, KAT_AES[u + 1]);
+ hextobin(cipher, KAT_AES[u + 2]);
+ ve->init(ec, key, key_len);
+ memcpy(buf, plain, sizeof plain);
+ memset(iv, 0, sizeof iv);
+ ve->run(ec, iv, buf, sizeof buf);
+ check_equals("KAT AES encrypt", buf, cipher, sizeof cipher);
+ vd->init(dc, key, key_len);
+ memset(iv, 0, sizeof iv);
+ vd->run(dc, iv, buf, sizeof buf);
+ check_equals("KAT AES decrypt", buf, plain, sizeof plain);
+ }
+
+ if (with_CBC) {
+ for (u = 0; KAT_AES_CBC[u]; u += 4) {
+ unsigned char key[32];
+ unsigned char ivref[16];
+ unsigned char plain[200];
+ unsigned char cipher[200];
+ unsigned char buf[200];
+ unsigned char iv[16];
+ size_t key_len, data_len, v;
+ br_aes_gen_cbcenc_keys v_ec;
+ br_aes_gen_cbcdec_keys v_dc;
+ const br_block_cbcenc_class **ec;
+ const br_block_cbcdec_class **dc;
+
+ ec = &v_ec.vtable;
+ dc = &v_dc.vtable;
+ key_len = hextobin(key, KAT_AES_CBC[u]);
+ hextobin(ivref, KAT_AES_CBC[u + 1]);
+ data_len = hextobin(plain, KAT_AES_CBC[u + 2]);
+ hextobin(cipher, KAT_AES_CBC[u + 3]);
+ ve->init(ec, key, key_len);
+
+ memcpy(buf, plain, data_len);
+ memcpy(iv, ivref, 16);
+ ve->run(ec, iv, buf, data_len);
+ check_equals("KAT CBC AES encrypt",
+ buf, cipher, data_len);
+ vd->init(dc, key, key_len);
+ memcpy(iv, ivref, 16);
+ vd->run(dc, iv, buf, data_len);
+ check_equals("KAT CBC AES decrypt",
+ buf, plain, data_len);
+
+ memcpy(buf, plain, data_len);
+ memcpy(iv, ivref, 16);
+ for (v = 0; v < data_len; v += 16) {
+ ve->run(ec, iv, buf + v, 16);
+ }
+ check_equals("KAT CBC AES encrypt (2)",
+ buf, cipher, data_len);
+ memcpy(iv, ivref, 16);
+ for (v = 0; v < data_len; v += 16) {
+ vd->run(dc, iv, buf + v, 16);
+ }
+ check_equals("KAT CBC AES decrypt (2)",
+ buf, plain, data_len);
+ }
+
+ /*
+ * We want to check proper IV management for CBC:
+ * encryption and decryption must properly copy the _last_
+ * encrypted block as new IV, for all sizes.
+ */
+ for (u = 1; u <= 35; u ++) {
+ br_hmac_drbg_context rng;
+ unsigned char x;
+ size_t key_len, data_len;
+ size_t v;
+
+ br_hmac_drbg_init(&rng, &br_sha256_vtable,
+ "seed for AES/CBC", 16);
+ x = u;
+ br_hmac_drbg_update(&rng, &x, 1);
+ data_len = u << 4;
+ for (key_len = 16; key_len <= 32; key_len += 16) {
+ unsigned char key[32];
+ unsigned char iv[16], iv1[16], iv2[16];
+ unsigned char plain[35 * 16];
+ unsigned char tmp1[sizeof plain];
+ unsigned char tmp2[sizeof plain];
+ br_aes_gen_cbcenc_keys v_ec;
+ br_aes_gen_cbcdec_keys v_dc;
+ const br_block_cbcenc_class **ec;
+ const br_block_cbcdec_class **dc;
+
+ br_hmac_drbg_generate(&rng, key, key_len);
+ br_hmac_drbg_generate(&rng, iv, sizeof iv);
+ br_hmac_drbg_generate(&rng, plain, data_len);
+
+ ec = &v_ec.vtable;
+ ve->init(ec, key, key_len);
+ memcpy(iv1, iv, sizeof iv);
+ memcpy(tmp1, plain, data_len);
+ ve->run(ec, iv1, tmp1, data_len);
+ check_equals("IV CBC AES (1)",
+ tmp1 + data_len - 16, iv1, 16);
+ memcpy(iv2, iv, sizeof iv);
+ memcpy(tmp2, plain, data_len);
+ for (v = 0; v < data_len; v += 16) {
+ ve->run(ec, iv2, tmp2 + v, 16);
+ }
+ check_equals("IV CBC AES (2)",
+ tmp2 + data_len - 16, iv2, 16);
+ check_equals("IV CBC AES (3)",
+ tmp1, tmp2, data_len);
+
+ dc = &v_dc.vtable;
+ vd->init(dc, key, key_len);
+ memcpy(iv1, iv, sizeof iv);
+ vd->run(dc, iv1, tmp1, data_len);
+ check_equals("IV CBC AES (4)", iv1, iv2, 16);
+ check_equals("IV CBC AES (5)",
+ tmp1, plain, data_len);
+ memcpy(iv2, iv, sizeof iv);
+ for (v = 0; v < data_len; v += 16) {
+ vd->run(dc, iv2, tmp2 + v, 16);
+ }
+ check_equals("IV CBC AES (6)", iv1, iv2, 16);
+ check_equals("IV CBC AES (7)",
+ tmp2, plain, data_len);
+ }
+ }
+ }
+
+ if (vc != NULL) {
+ if (vc->block_size != 16 || vc->log_block_size != 4) {
+ fprintf(stderr, "%s failed: wrong block size\n", name);
+ exit(EXIT_FAILURE);
+ }
+ for (u = 0; KAT_AES_CTR[u]; u += 4) {
+ unsigned char key[32];
+ unsigned char iv[12];
+ unsigned char plain[200];
+ unsigned char cipher[200];
+ unsigned char buf[200];
+ size_t key_len, data_len, v;
+ uint32_t c;
+ br_aes_gen_ctr_keys v_xc;
+ const br_block_ctr_class **xc;
+
+ xc = &v_xc.vtable;
+ key_len = hextobin(key, KAT_AES_CTR[u]);
+ hextobin(iv, KAT_AES_CTR[u + 1]);
+ data_len = hextobin(plain, KAT_AES_CTR[u + 2]);
+ hextobin(cipher, KAT_AES_CTR[u + 3]);
+ vc->init(xc, key, key_len);
+ memcpy(buf, plain, data_len);
+ vc->run(xc, iv, 1, buf, data_len);
+ check_equals("KAT CTR AES (1)", buf, cipher, data_len);
+ vc->run(xc, iv, 1, buf, data_len);
+ check_equals("KAT CTR AES (2)", buf, plain, data_len);
+
+ memcpy(buf, plain, data_len);
+ c = 1;
+ for (v = 0; v < data_len; v += 32) {
+ size_t clen;
+
+ clen = data_len - v;
+ if (clen > 32) {
+ clen = 32;
+ }
+ c = vc->run(xc, iv, c, buf + v, clen);
+ }
+ check_equals("KAT CTR AES (3)", buf, cipher, data_len);
+
+ memcpy(buf, plain, data_len);
+ c = 1;
+ for (v = 0; v < data_len; v += 16) {
+ size_t clen;
+
+ clen = data_len - v;
+ if (clen > 16) {
+ clen = 16;
+ }
+ c = vc->run(xc, iv, c, buf + v, clen);
+ }
+ check_equals("KAT CTR AES (4)", buf, cipher, data_len);
+ }
+ }
+
+ if (with_MC) {
+ monte_carlo_AES_encrypt(
+ ve,
+ "139a35422f1d61de3c91787fe0507afd",
+ "b9145a768b7dc489a096b546f43b231f",
+ "fb2649694783b551eacd9d5db6126d47");
+ monte_carlo_AES_decrypt(
+ vd,
+ "0c60e7bf20ada9baa9e1ddf0d1540726",
+ "b08a29b11a500ea3aca42c36675b9785",
+ "d1d2bfdc58ffcad2341b095bce55221e");
+
+ monte_carlo_AES_encrypt(
+ ve,
+ "b9a63e09e1dfc42e93a90d9bad739e5967aef672eedd5da9",
+ "85a1f7a58167b389cddc8a9ff175ee26",
+ "5d1196da8f184975e240949a25104554");
+ monte_carlo_AES_decrypt(
+ vd,
+ "4b97585701c03fbebdfa8555024f589f1482c58a00fdd9fd",
+ "d0bd0e02ded155e4516be83f42d347a4",
+ "b63ef1b79507a62eba3dafcec54a6328");
+
+ monte_carlo_AES_encrypt(
+ ve,
+ "f9e8389f5b80712e3886cc1fa2d28a3b8c9cd88a2d4a54c6aa86ce0fef944be0",
+ "b379777f9050e2a818f2940cbbd9aba4",
+ "c5d2cb3d5b7ff0e23e308967ee074825");
+ monte_carlo_AES_decrypt(
+ vd,
+ "2b09ba39b834062b9e93f48373b8dd018dedf1e5ba1b8af831ebbacbc92a2643",
+ "89649bd0115f30bd878567610223a59d",
+ "e3d3868f578caf34e36445bf14cefc68");
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_AES_big(void)
+{
+ test_AES_generic("AES_big",
+ &br_aes_big_cbcenc_vtable,
+ &br_aes_big_cbcdec_vtable,
+ &br_aes_big_ctr_vtable,
+ 1, 1);
+}
+
+static void
+test_AES_small(void)
+{
+ test_AES_generic("AES_small",
+ &br_aes_small_cbcenc_vtable,
+ &br_aes_small_cbcdec_vtable,
+ &br_aes_small_ctr_vtable,
+ 1, 1);
+}
+
+static void
+test_AES_ct(void)
+{
+ test_AES_generic("AES_ct",
+ &br_aes_ct_cbcenc_vtable,
+ &br_aes_ct_cbcdec_vtable,
+ &br_aes_ct_ctr_vtable,
+ 1, 1);
+}
+
+static void
+test_AES_ct64(void)
+{
+ test_AES_generic("AES_ct64",
+ &br_aes_ct64_cbcenc_vtable,
+ &br_aes_ct64_cbcdec_vtable,
+ &br_aes_ct64_ctr_vtable,
+ 1, 1);
+}
+
+static void
+test_AES_x86ni(void)
+{
+ const br_block_cbcenc_class *x_cbcenc;
+ const br_block_cbcdec_class *x_cbcdec;
+ const br_block_ctr_class *x_ctr;
+ int hcbcenc, hcbcdec, hctr;
+
+ x_cbcenc = br_aes_x86ni_cbcenc_get_vtable();
+ x_cbcdec = br_aes_x86ni_cbcdec_get_vtable();
+ x_ctr = br_aes_x86ni_ctr_get_vtable();
+ hcbcenc = (x_cbcenc != NULL);
+ hcbcdec = (x_cbcdec != NULL);
+ hctr = (x_ctr != NULL);
+ if (hcbcenc != hctr || hcbcdec != hctr) {
+ fprintf(stderr, "AES_x86ni availability mismatch (%d/%d/%d)\n",
+ hcbcenc, hcbcdec, hctr);
+ exit(EXIT_FAILURE);
+ }
+ if (hctr) {
+ test_AES_generic("AES_x86ni",
+ x_cbcenc, x_cbcdec, x_ctr, 1, 1);
+ } else {
+ printf("Test AES_x86ni: UNAVAILABLE\n");
+ }
+}
+
+static void
+test_AES_pwr8(void)
+{
+ const br_block_cbcenc_class *x_cbcenc;
+ const br_block_cbcdec_class *x_cbcdec;
+ const br_block_ctr_class *x_ctr;
+ int hcbcenc, hcbcdec, hctr;
+
+ x_cbcenc = br_aes_pwr8_cbcenc_get_vtable();
+ x_cbcdec = br_aes_pwr8_cbcdec_get_vtable();
+ x_ctr = br_aes_pwr8_ctr_get_vtable();
+ hcbcenc = (x_cbcenc != NULL);
+ hcbcdec = (x_cbcdec != NULL);
+ hctr = (x_ctr != NULL);
+ if (hcbcenc != hctr || hcbcdec != hctr) {
+ fprintf(stderr, "AES_pwr8 availability mismatch (%d/%d/%d)\n",
+ hcbcenc, hcbcdec, hctr);
+ exit(EXIT_FAILURE);
+ }
+ if (hctr) {
+ test_AES_generic("AES_pwr8",
+ x_cbcenc, x_cbcdec, x_ctr, 1, 1);
+ } else {
+ printf("Test AES_pwr8: UNAVAILABLE\n");
+ }
+}
+
+/*
+ * Custom CTR + CBC-MAC AES implementation. Can also do CTR-only, and
+ * CBC-MAC-only. The 'aes_big' implementation (CTR) is used. This is
+ * meant for comparisons.
+ *
+ * If 'ctr' is NULL then no encryption/decryption is done; otherwise,
+ * CTR encryption/decryption is performed (full-block counter) and the
+ * 'ctr' array is updated with the new counter value.
+ *
+ * If 'cbcmac' is NULL then no CBC-MAC is done; otherwise, CBC-MAC is
+ * applied on the encrypted data, with 'cbcmac' as IV and destination
+ * buffer for the output. If 'ctr' is not NULL and 'encrypt' is non-zero,
+ * then CBC-MAC is computed over the result of CTR processing; otherwise,
+ * CBC-MAC is computed over the input data itself.
+ */
+static void
+do_aes_ctrcbc(const void *key, size_t key_len, int encrypt,
+ void *ctr, void *cbcmac, unsigned char *data, size_t len)
+{
+ br_aes_big_ctr_keys bc;
+ int i;
+
+ br_aes_big_ctr_init(&bc, key, key_len);
+ for (i = 0; i < 2; i ++) {
+ /*
+ * CBC-MAC is computed on the encrypted data, so in
+ * first pass if decrypting, second pass if encrypting.
+ */
+ if (cbcmac != NULL
+ && ((encrypt && i == 1) || (!encrypt && i == 0)))
+ {
+ unsigned char zz[16];
+ size_t u;
+
+ memcpy(zz, cbcmac, sizeof zz);
+ for (u = 0; u < len; u += 16) {
+ unsigned char tmp[16];
+ size_t v;
+
+ for (v = 0; v < 16; v ++) {
+ tmp[v] = zz[v] ^ data[u + v];
+ }
+ memset(zz, 0, sizeof zz);
+ br_aes_big_ctr_run(&bc,
+ tmp, br_dec32be(tmp + 12), zz, 16);
+ }
+ memcpy(cbcmac, zz, sizeof zz);
+ }
+
+ /*
+ * CTR encryption/decryption is done only in the first pass.
+ * We process data block per block, because the CTR-only
+ * class uses a 32-bit counter, while the CTR+CBC-MAC
+ * class uses a 128-bit counter.
+ */
+ if (ctr != NULL && i == 0) {
+ unsigned char zz[16];
+ size_t u;
+
+ memcpy(zz, ctr, sizeof zz);
+ for (u = 0; u < len; u += 16) {
+ int i;
+
+ br_aes_big_ctr_run(&bc,
+ zz, br_dec32be(zz + 12), data + u, 16);
+ for (i = 15; i >= 0; i --) {
+ zz[i] = (zz[i] + 1) & 0xFF;
+ if (zz[i] != 0) {
+ break;
+ }
+ }
+ }
+ memcpy(ctr, zz, sizeof zz);
+ }
+ }
+}
+
+static void
+test_AES_CTRCBC_inner(const char *name, const br_block_ctrcbc_class *vt)
+{
+ br_hmac_drbg_context rng;
+ size_t key_len;
+
+ printf("Test AES CTR/CBC-MAC %s: ", name);
+ fflush(stdout);
+
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, name, strlen(name));
+ for (key_len = 16; key_len <= 32; key_len += 8) {
+ br_aes_gen_ctrcbc_keys bc;
+ unsigned char key[32];
+ size_t data_len;
+
+ br_hmac_drbg_generate(&rng, key, key_len);
+ vt->init(&bc.vtable, key, key_len);
+ for (data_len = 0; data_len <= 512; data_len += 16) {
+ unsigned char plain[512];
+ unsigned char data1[sizeof plain];
+ unsigned char data2[sizeof plain];
+ unsigned char ctr[16], cbcmac[16];
+ unsigned char ctr1[16], cbcmac1[16];
+ unsigned char ctr2[16], cbcmac2[16];
+ int i;
+
+ br_hmac_drbg_generate(&rng, plain, data_len);
+
+ for (i = 0; i <= 16; i ++) {
+ if (i == 0) {
+ br_hmac_drbg_generate(&rng, ctr, 16);
+ } else {
+ memset(ctr, 0, i - 1);
+ memset(ctr + i - 1, 0xFF, 17 - i);
+ }
+ br_hmac_drbg_generate(&rng, cbcmac, 16);
+
+ memcpy(data1, plain, data_len);
+ memcpy(ctr1, ctr, 16);
+ vt->ctr(&bc.vtable, ctr1, data1, data_len);
+ memcpy(data2, plain, data_len);
+ memcpy(ctr2, ctr, 16);
+ do_aes_ctrcbc(key, key_len, 1,
+ ctr2, NULL, data2, data_len);
+ check_equals("CTR-only data",
+ data1, data2, data_len);
+ check_equals("CTR-only counter",
+ ctr1, ctr2, 16);
+
+ memcpy(data1, plain, data_len);
+ memcpy(cbcmac1, cbcmac, 16);
+ vt->mac(&bc.vtable, cbcmac1, data1, data_len);
+ memcpy(data2, plain, data_len);
+ memcpy(cbcmac2, cbcmac, 16);
+ do_aes_ctrcbc(key, key_len, 1,
+ NULL, cbcmac2, data2, data_len);
+ check_equals("CBC-MAC-only",
+ cbcmac1, cbcmac2, 16);
+
+ memcpy(data1, plain, data_len);
+ memcpy(ctr1, ctr, 16);
+ memcpy(cbcmac1, cbcmac, 16);
+ vt->encrypt(&bc.vtable,
+ ctr1, cbcmac1, data1, data_len);
+ memcpy(data2, plain, data_len);
+ memcpy(ctr2, ctr, 16);
+ memcpy(cbcmac2, cbcmac, 16);
+ do_aes_ctrcbc(key, key_len, 1,
+ ctr2, cbcmac2, data2, data_len);
+ check_equals("encrypt: combined data",
+ data1, data2, data_len);
+ check_equals("encrypt: combined counter",
+ ctr1, ctr2, 16);
+ check_equals("encrypt: combined CBC-MAC",
+ cbcmac1, cbcmac2, 16);
+
+ memcpy(ctr1, ctr, 16);
+ memcpy(cbcmac1, cbcmac, 16);
+ vt->decrypt(&bc.vtable,
+ ctr1, cbcmac1, data1, data_len);
+ memcpy(ctr2, ctr, 16);
+ memcpy(cbcmac2, cbcmac, 16);
+ do_aes_ctrcbc(key, key_len, 0,
+ ctr2, cbcmac2, data2, data_len);
+ check_equals("decrypt: combined data",
+ data1, data2, data_len);
+ check_equals("decrypt: combined counter",
+ ctr1, ctr2, 16);
+ check_equals("decrypt: combined CBC-MAC",
+ cbcmac1, cbcmac2, 16);
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" ");
+ fflush(stdout);
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_AES_CTRCBC_big(void)
+{
+ test_AES_CTRCBC_inner("big", &br_aes_big_ctrcbc_vtable);
+}
+
+static void
+test_AES_CTRCBC_small(void)
+{
+ test_AES_CTRCBC_inner("small", &br_aes_small_ctrcbc_vtable);
+}
+
+static void
+test_AES_CTRCBC_ct(void)
+{
+ test_AES_CTRCBC_inner("ct", &br_aes_ct_ctrcbc_vtable);
+}
+
+static void
+test_AES_CTRCBC_ct64(void)
+{
+ test_AES_CTRCBC_inner("ct64", &br_aes_ct64_ctrcbc_vtable);
+}
+
+static void
+test_AES_CTRCBC_x86ni(void)
+{
+ const br_block_ctrcbc_class *vt;
+
+ vt = br_aes_x86ni_ctrcbc_get_vtable();
+ if (vt != NULL) {
+ test_AES_CTRCBC_inner("x86ni", vt);
+ } else {
+ printf("Test AES CTR/CBC-MAC x86ni: UNAVAILABLE\n");
+ }
+}
+
+static void
+test_AES_CTRCBC_pwr8(void)
+{
+ const br_block_ctrcbc_class *vt;
+
+ vt = br_aes_pwr8_ctrcbc_get_vtable();
+ if (vt != NULL) {
+ test_AES_CTRCBC_inner("pwr8", vt);
+ } else {
+ printf("Test AES CTR/CBC-MAC pwr8: UNAVAILABLE\n");
+ }
+}
+
+/*
+ * DES known-answer tests. Order: plaintext, key, ciphertext.
+ * (mostly from NIST SP 800-20).
+ */
+static const char *const KAT_DES[] = {
+ "10316E028C8F3B4A", "0000000000000000", "82DCBAFBDEAB6602",
+ "8000000000000000", "0000000000000000", "95A8D72813DAA94D",
+ "4000000000000000", "0000000000000000", "0EEC1487DD8C26D5",
+ "2000000000000000", "0000000000000000", "7AD16FFB79C45926",
+ "1000000000000000", "0000000000000000", "D3746294CA6A6CF3",
+ "0800000000000000", "0000000000000000", "809F5F873C1FD761",
+ "0400000000000000", "0000000000000000", "C02FAFFEC989D1FC",
+ "0200000000000000", "0000000000000000", "4615AA1D33E72F10",
+ "0100000000000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0080000000000000", "0000000000000000", "2055123350C00858",
+ "0040000000000000", "0000000000000000", "DF3B99D6577397C8",
+ "0020000000000000", "0000000000000000", "31FE17369B5288C9",
+ "0010000000000000", "0000000000000000", "DFDD3CC64DAE1642",
+ "0008000000000000", "0000000000000000", "178C83CE2B399D94",
+ "0004000000000000", "0000000000000000", "50F636324A9B7F80",
+ "0002000000000000", "0000000000000000", "A8468EE3BC18F06D",
+ "0001000000000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000800000000000", "0000000000000000", "A2DC9E92FD3CDE92",
+ "0000400000000000", "0000000000000000", "CAC09F797D031287",
+ "0000200000000000", "0000000000000000", "90BA680B22AEB525",
+ "0000100000000000", "0000000000000000", "CE7A24F350E280B6",
+ "0000080000000000", "0000000000000000", "882BFF0AA01A0B87",
+ "0000040000000000", "0000000000000000", "25610288924511C2",
+ "0000020000000000", "0000000000000000", "C71516C29C75D170",
+ "0000010000000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000008000000000", "0000000000000000", "5199C29A52C9F059",
+ "0000004000000000", "0000000000000000", "C22F0A294A71F29F",
+ "0000002000000000", "0000000000000000", "EE371483714C02EA",
+ "0000001000000000", "0000000000000000", "A81FBD448F9E522F",
+ "0000000800000000", "0000000000000000", "4F644C92E192DFED",
+ "0000000400000000", "0000000000000000", "1AFA9A66A6DF92AE",
+ "0000000200000000", "0000000000000000", "B3C1CC715CB879D8",
+ "0000000100000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000000080000000", "0000000000000000", "19D032E64AB0BD8B",
+ "0000000040000000", "0000000000000000", "3CFAA7A7DC8720DC",
+ "0000000020000000", "0000000000000000", "B7265F7F447AC6F3",
+ "0000000010000000", "0000000000000000", "9DB73B3C0D163F54",
+ "0000000008000000", "0000000000000000", "8181B65BABF4A975",
+ "0000000004000000", "0000000000000000", "93C9B64042EAA240",
+ "0000000002000000", "0000000000000000", "5570530829705592",
+ "0000000001000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000000000800000", "0000000000000000", "8638809E878787A0",
+ "0000000000400000", "0000000000000000", "41B9A79AF79AC208",
+ "0000000000200000", "0000000000000000", "7A9BE42F2009A892",
+ "0000000000100000", "0000000000000000", "29038D56BA6D2745",
+ "0000000000080000", "0000000000000000", "5495C6ABF1E5DF51",
+ "0000000000040000", "0000000000000000", "AE13DBD561488933",
+ "0000000000020000", "0000000000000000", "024D1FFA8904E389",
+ "0000000000010000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000000000008000", "0000000000000000", "D1399712F99BF02E",
+ "0000000000004000", "0000000000000000", "14C1D7C1CFFEC79E",
+ "0000000000002000", "0000000000000000", "1DE5279DAE3BED6F",
+ "0000000000001000", "0000000000000000", "E941A33F85501303",
+ "0000000000000800", "0000000000000000", "DA99DBBC9A03F379",
+ "0000000000000400", "0000000000000000", "B7FC92F91D8E92E9",
+ "0000000000000200", "0000000000000000", "AE8E5CAA3CA04E85",
+ "0000000000000100", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000000000000080", "0000000000000000", "9CC62DF43B6EED74",
+ "0000000000000040", "0000000000000000", "D863DBB5C59A91A0",
+ "0000000000000020", "0000000000000000", "A1AB2190545B91D7",
+ "0000000000000010", "0000000000000000", "0875041E64C570F7",
+ "0000000000000008", "0000000000000000", "5A594528BEBEF1CC",
+ "0000000000000004", "0000000000000000", "FCDB3291DE21F0C0",
+ "0000000000000002", "0000000000000000", "869EFD7F9F265A09",
+ "0000000000000001", "0000000000000000", "8CA64DE9C1B123A7",
+ "0000000000000000", "8000000000000000", "95F8A5E5DD31D900",
+ "0000000000000000", "4000000000000000", "DD7F121CA5015619",
+ "0000000000000000", "2000000000000000", "2E8653104F3834EA",
+ "0000000000000000", "1000000000000000", "4BD388FF6CD81D4F",
+ "0000000000000000", "0800000000000000", "20B9E767B2FB1456",
+ "0000000000000000", "0400000000000000", "55579380D77138EF",
+ "0000000000000000", "0200000000000000", "6CC5DEFAAF04512F",
+ "0000000000000000", "0100000000000000", "0D9F279BA5D87260",
+ "0000000000000000", "0080000000000000", "D9031B0271BD5A0A",
+ "0000000000000000", "0040000000000000", "424250B37C3DD951",
+ "0000000000000000", "0020000000000000", "B8061B7ECD9A21E5",
+ "0000000000000000", "0010000000000000", "F15D0F286B65BD28",
+ "0000000000000000", "0008000000000000", "ADD0CC8D6E5DEBA1",
+ "0000000000000000", "0004000000000000", "E6D5F82752AD63D1",
+ "0000000000000000", "0002000000000000", "ECBFE3BD3F591A5E",
+ "0000000000000000", "0001000000000000", "F356834379D165CD",
+ "0000000000000000", "0000800000000000", "2B9F982F20037FA9",
+ "0000000000000000", "0000400000000000", "889DE068A16F0BE6",
+ "0000000000000000", "0000200000000000", "E19E275D846A1298",
+ "0000000000000000", "0000100000000000", "329A8ED523D71AEC",
+ "0000000000000000", "0000080000000000", "E7FCE22557D23C97",
+ "0000000000000000", "0000040000000000", "12A9F5817FF2D65D",
+ "0000000000000000", "0000020000000000", "A484C3AD38DC9C19",
+ "0000000000000000", "0000010000000000", "FBE00A8A1EF8AD72",
+ "0000000000000000", "0000008000000000", "750D079407521363",
+ "0000000000000000", "0000004000000000", "64FEED9C724C2FAF",
+ "0000000000000000", "0000002000000000", "F02B263B328E2B60",
+ "0000000000000000", "0000001000000000", "9D64555A9A10B852",
+ "0000000000000000", "0000000800000000", "D106FF0BED5255D7",
+ "0000000000000000", "0000000400000000", "E1652C6B138C64A5",
+ "0000000000000000", "0000000200000000", "E428581186EC8F46",
+ "0000000000000000", "0000000100000000", "AEB5F5EDE22D1A36",
+ "0000000000000000", "0000000080000000", "E943D7568AEC0C5C",
+ "0000000000000000", "0000000040000000", "DF98C8276F54B04B",
+ "0000000000000000", "0000000020000000", "B160E4680F6C696F",
+ "0000000000000000", "0000000010000000", "FA0752B07D9C4AB8",
+ "0000000000000000", "0000000008000000", "CA3A2B036DBC8502",
+ "0000000000000000", "0000000004000000", "5E0905517BB59BCF",
+ "0000000000000000", "0000000002000000", "814EEB3B91D90726",
+ "0000000000000000", "0000000001000000", "4D49DB1532919C9F",
+ "0000000000000000", "0000000000800000", "25EB5FC3F8CF0621",
+ "0000000000000000", "0000000000400000", "AB6A20C0620D1C6F",
+ "0000000000000000", "0000000000200000", "79E90DBC98F92CCA",
+ "0000000000000000", "0000000000100000", "866ECEDD8072BB0E",
+ "0000000000000000", "0000000000080000", "8B54536F2F3E64A8",
+ "0000000000000000", "0000000000040000", "EA51D3975595B86B",
+ "0000000000000000", "0000000000020000", "CAFFC6AC4542DE31",
+ "0000000000000000", "0000000000010000", "8DD45A2DDF90796C",
+ "0000000000000000", "0000000000008000", "1029D55E880EC2D0",
+ "0000000000000000", "0000000000004000", "5D86CB23639DBEA9",
+ "0000000000000000", "0000000000002000", "1D1CA853AE7C0C5F",
+ "0000000000000000", "0000000000001000", "CE332329248F3228",
+ "0000000000000000", "0000000000000800", "8405D1ABE24FB942",
+ "0000000000000000", "0000000000000400", "E643D78090CA4207",
+ "0000000000000000", "0000000000000200", "48221B9937748A23",
+ "0000000000000000", "0000000000000100", "DD7C0BBD61FAFD54",
+ "0000000000000000", "0000000000000080", "2FBC291A570DB5C4",
+ "0000000000000000", "0000000000000040", "E07C30D7E4E26E12",
+ "0000000000000000", "0000000000000020", "0953E2258E8E90A1",
+ "0000000000000000", "0000000000000010", "5B711BC4CEEBF2EE",
+ "0000000000000000", "0000000000000008", "CC083F1E6D9E85F6",
+ "0000000000000000", "0000000000000004", "D2FD8867D50D2DFE",
+ "0000000000000000", "0000000000000002", "06E7EA22CE92708F",
+ "0000000000000000", "0000000000000001", "166B40B44ABA4BD6",
+ "0000000000000000", "0000000000000000", "8CA64DE9C1B123A7",
+ "0101010101010101", "0101010101010101", "994D4DC157B96C52",
+ "0202020202020202", "0202020202020202", "E127C2B61D98E6E2",
+ "0303030303030303", "0303030303030303", "984C91D78A269CE3",
+ "0404040404040404", "0404040404040404", "1F4570BB77550683",
+ "0505050505050505", "0505050505050505", "3990ABF98D672B16",
+ "0606060606060606", "0606060606060606", "3F5150BBA081D585",
+ "0707070707070707", "0707070707070707", "C65242248C9CF6F2",
+ "0808080808080808", "0808080808080808", "10772D40FAD24257",
+ "0909090909090909", "0909090909090909", "F0139440647A6E7B",
+ "0A0A0A0A0A0A0A0A", "0A0A0A0A0A0A0A0A", "0A288603044D740C",
+ "0B0B0B0B0B0B0B0B", "0B0B0B0B0B0B0B0B", "6359916942F7438F",
+ "0C0C0C0C0C0C0C0C", "0C0C0C0C0C0C0C0C", "934316AE443CF08B",
+ "0D0D0D0D0D0D0D0D", "0D0D0D0D0D0D0D0D", "E3F56D7F1130A2B7",
+ "0E0E0E0E0E0E0E0E", "0E0E0E0E0E0E0E0E", "A2E4705087C6B6B4",
+ "0F0F0F0F0F0F0F0F", "0F0F0F0F0F0F0F0F", "D5D76E09A447E8C3",
+ "1010101010101010", "1010101010101010", "DD7515F2BFC17F85",
+ "1111111111111111", "1111111111111111", "F40379AB9E0EC533",
+ "1212121212121212", "1212121212121212", "96CD27784D1563E5",
+ "1313131313131313", "1313131313131313", "2911CF5E94D33FE1",
+ "1414141414141414", "1414141414141414", "377B7F7CA3E5BBB3",
+ "1515151515151515", "1515151515151515", "701AA63832905A92",
+ "1616161616161616", "1616161616161616", "2006E716C4252D6D",
+ "1717171717171717", "1717171717171717", "452C1197422469F8",
+ "1818181818181818", "1818181818181818", "C33FD1EB49CB64DA",
+ "1919191919191919", "1919191919191919", "7572278F364EB50D",
+ "1A1A1A1A1A1A1A1A", "1A1A1A1A1A1A1A1A", "69E51488403EF4C3",
+ "1B1B1B1B1B1B1B1B", "1B1B1B1B1B1B1B1B", "FF847E0ADF192825",
+ "1C1C1C1C1C1C1C1C", "1C1C1C1C1C1C1C1C", "521B7FB3B41BB791",
+ "1D1D1D1D1D1D1D1D", "1D1D1D1D1D1D1D1D", "26059A6A0F3F6B35",
+ "1E1E1E1E1E1E1E1E", "1E1E1E1E1E1E1E1E", "F24A8D2231C77538",
+ "1F1F1F1F1F1F1F1F", "1F1F1F1F1F1F1F1F", "4FD96EC0D3304EF6",
+ "2020202020202020", "2020202020202020", "18A9D580A900B699",
+ "2121212121212121", "2121212121212121", "88586E1D755B9B5A",
+ "2222222222222222", "2222222222222222", "0F8ADFFB11DC2784",
+ "2323232323232323", "2323232323232323", "2F30446C8312404A",
+ "2424242424242424", "2424242424242424", "0BA03D9E6C196511",
+ "2525252525252525", "2525252525252525", "3E55E997611E4B7D",
+ "2626262626262626", "2626262626262626", "B2522FB5F158F0DF",
+ "2727272727272727", "2727272727272727", "2109425935406AB8",
+ "2828282828282828", "2828282828282828", "11A16028F310FF16",
+ "2929292929292929", "2929292929292929", "73F0C45F379FE67F",
+ "2A2A2A2A2A2A2A2A", "2A2A2A2A2A2A2A2A", "DCAD4338F7523816",
+ "2B2B2B2B2B2B2B2B", "2B2B2B2B2B2B2B2B", "B81634C1CEAB298C",
+ "2C2C2C2C2C2C2C2C", "2C2C2C2C2C2C2C2C", "DD2CCB29B6C4C349",
+ "2D2D2D2D2D2D2D2D", "2D2D2D2D2D2D2D2D", "7D07A77A2ABD50A7",
+ "2E2E2E2E2E2E2E2E", "2E2E2E2E2E2E2E2E", "30C1B0C1FD91D371",
+ "2F2F2F2F2F2F2F2F", "2F2F2F2F2F2F2F2F", "C4427B31AC61973B",
+ "3030303030303030", "3030303030303030", "F47BB46273B15EB5",
+ "3131313131313131", "3131313131313131", "655EA628CF62585F",
+ "3232323232323232", "3232323232323232", "AC978C247863388F",
+ "3333333333333333", "3333333333333333", "0432ED386F2DE328",
+ "3434343434343434", "3434343434343434", "D254014CB986B3C2",
+ "3535353535353535", "3535353535353535", "B256E34BEDB49801",
+ "3636363636363636", "3636363636363636", "37F8759EB77E7BFC",
+ "3737373737373737", "3737373737373737", "5013CA4F62C9CEA0",
+ "3838383838383838", "3838383838383838", "8940F7B3EACA5939",
+ "3939393939393939", "3939393939393939", "E22B19A55086774B",
+ "3A3A3A3A3A3A3A3A", "3A3A3A3A3A3A3A3A", "B04A2AAC925ABB0B",
+ "3B3B3B3B3B3B3B3B", "3B3B3B3B3B3B3B3B", "8D250D58361597FC",
+ "3C3C3C3C3C3C3C3C", "3C3C3C3C3C3C3C3C", "51F0114FB6A6CD37",
+ "3D3D3D3D3D3D3D3D", "3D3D3D3D3D3D3D3D", "9D0BB4DB830ECB73",
+ "3E3E3E3E3E3E3E3E", "3E3E3E3E3E3E3E3E", "E96089D6368F3E1A",
+ "3F3F3F3F3F3F3F3F", "3F3F3F3F3F3F3F3F", "5C4CA877A4E1E92D",
+ "4040404040404040", "4040404040404040", "6D55DDBC8DEA95FF",
+ "4141414141414141", "4141414141414141", "19DF84AC95551003",
+ "4242424242424242", "4242424242424242", "724E7332696D08A7",
+ "4343434343434343", "4343434343434343", "B91810B8CDC58FE2",
+ "4444444444444444", "4444444444444444", "06E23526EDCCD0C4",
+ "4545454545454545", "4545454545454545", "EF52491D5468D441",
+ "4646464646464646", "4646464646464646", "48019C59E39B90C5",
+ "4747474747474747", "4747474747474747", "0544083FB902D8C0",
+ "4848484848484848", "4848484848484848", "63B15CADA668CE12",
+ "4949494949494949", "4949494949494949", "EACC0C1264171071",
+ "4A4A4A4A4A4A4A4A", "4A4A4A4A4A4A4A4A", "9D2B8C0AC605F274",
+ "4B4B4B4B4B4B4B4B", "4B4B4B4B4B4B4B4B", "C90F2F4C98A8FB2A",
+ "4C4C4C4C4C4C4C4C", "4C4C4C4C4C4C4C4C", "03481B4828FD1D04",
+ "4D4D4D4D4D4D4D4D", "4D4D4D4D4D4D4D4D", "C78FC45A1DCEA2E2",
+ "4E4E4E4E4E4E4E4E", "4E4E4E4E4E4E4E4E", "DB96D88C3460D801",
+ "4F4F4F4F4F4F4F4F", "4F4F4F4F4F4F4F4F", "6C69E720F5105518",
+ "5050505050505050", "5050505050505050", "0D262E418BC893F3",
+ "5151515151515151", "5151515151515151", "6AD84FD7848A0A5C",
+ "5252525252525252", "5252525252525252", "C365CB35B34B6114",
+ "5353535353535353", "5353535353535353", "1155392E877F42A9",
+ "5454545454545454", "5454545454545454", "531BE5F9405DA715",
+ "5555555555555555", "5555555555555555", "3BCDD41E6165A5E8",
+ "5656565656565656", "5656565656565656", "2B1FF5610A19270C",
+ "5757575757575757", "5757575757575757", "D90772CF3F047CFD",
+ "5858585858585858", "5858585858585858", "1BEA27FFB72457B7",
+ "5959595959595959", "5959595959595959", "85C3E0C429F34C27",
+ "5A5A5A5A5A5A5A5A", "5A5A5A5A5A5A5A5A", "F9038021E37C7618",
+ "5B5B5B5B5B5B5B5B", "5B5B5B5B5B5B5B5B", "35BC6FF838DBA32F",
+ "5C5C5C5C5C5C5C5C", "5C5C5C5C5C5C5C5C", "4927ACC8CE45ECE7",
+ "5D5D5D5D5D5D5D5D", "5D5D5D5D5D5D5D5D", "E812EE6E3572985C",
+ "5E5E5E5E5E5E5E5E", "5E5E5E5E5E5E5E5E", "9BB93A89627BF65F",
+ "5F5F5F5F5F5F5F5F", "5F5F5F5F5F5F5F5F", "EF12476884CB74CA",
+ "6060606060606060", "6060606060606060", "1BF17E00C09E7CBF",
+ "6161616161616161", "6161616161616161", "29932350C098DB5D",
+ "6262626262626262", "6262626262626262", "B476E6499842AC54",
+ "6363636363636363", "6363636363636363", "5C662C29C1E96056",
+ "6464646464646464", "6464646464646464", "3AF1703D76442789",
+ "6565656565656565", "6565656565656565", "86405D9B425A8C8C",
+ "6666666666666666", "6666666666666666", "EBBF4810619C2C55",
+ "6767676767676767", "6767676767676767", "F8D1CD7367B21B5D",
+ "6868686868686868", "6868686868686868", "9EE703142BF8D7E2",
+ "6969696969696969", "6969696969696969", "5FDFFFC3AAAB0CB3",
+ "6A6A6A6A6A6A6A6A", "6A6A6A6A6A6A6A6A", "26C940AB13574231",
+ "6B6B6B6B6B6B6B6B", "6B6B6B6B6B6B6B6B", "1E2DC77E36A84693",
+ "6C6C6C6C6C6C6C6C", "6C6C6C6C6C6C6C6C", "0F4FF4D9BC7E2244",
+ "6D6D6D6D6D6D6D6D", "6D6D6D6D6D6D6D6D", "A4C9A0D04D3280CD",
+ "6E6E6E6E6E6E6E6E", "6E6E6E6E6E6E6E6E", "9FAF2C96FE84919D",
+ "6F6F6F6F6F6F6F6F", "6F6F6F6F6F6F6F6F", "115DBC965E6096C8",
+ "7070707070707070", "7070707070707070", "AF531E9520994017",
+ "7171717171717171", "7171717171717171", "B971ADE70E5C89EE",
+ "7272727272727272", "7272727272727272", "415D81C86AF9C376",
+ "7373737373737373", "7373737373737373", "8DFB864FDB3C6811",
+ "7474747474747474", "7474747474747474", "10B1C170E3398F91",
+ "7575757575757575", "7575757575757575", "CFEF7A1C0218DB1E",
+ "7676767676767676", "7676767676767676", "DBAC30A2A40B1B9C",
+ "7777777777777777", "7777777777777777", "89D3BF37052162E9",
+ "7878787878787878", "7878787878787878", "80D9230BDAEB67DC",
+ "7979797979797979", "7979797979797979", "3440911019AD68D7",
+ "7A7A7A7A7A7A7A7A", "7A7A7A7A7A7A7A7A", "9626FE57596E199E",
+ "7B7B7B7B7B7B7B7B", "7B7B7B7B7B7B7B7B", "DEA0B796624BB5BA",
+ "7C7C7C7C7C7C7C7C", "7C7C7C7C7C7C7C7C", "E9E40542BDDB3E9D",
+ "7D7D7D7D7D7D7D7D", "7D7D7D7D7D7D7D7D", "8AD99914B354B911",
+ "7E7E7E7E7E7E7E7E", "7E7E7E7E7E7E7E7E", "6F85B98DD12CB13B",
+ "7F7F7F7F7F7F7F7F", "7F7F7F7F7F7F7F7F", "10130DA3C3A23924",
+ "8080808080808080", "8080808080808080", "EFECF25C3C5DC6DB",
+ "8181818181818181", "8181818181818181", "907A46722ED34EC4",
+ "8282828282828282", "8282828282828282", "752666EB4CAB46EE",
+ "8383838383838383", "8383838383838383", "161BFABD4224C162",
+ "8484848484848484", "8484848484848484", "215F48699DB44A45",
+ "8585858585858585", "8585858585858585", "69D901A8A691E661",
+ "8686868686868686", "8686868686868686", "CBBF6EEFE6529728",
+ "8787878787878787", "8787878787878787", "7F26DCF425149823",
+ "8888888888888888", "8888888888888888", "762C40C8FADE9D16",
+ "8989898989898989", "8989898989898989", "2453CF5D5BF4E463",
+ "8A8A8A8A8A8A8A8A", "8A8A8A8A8A8A8A8A", "301085E3FDE724E1",
+ "8B8B8B8B8B8B8B8B", "8B8B8B8B8B8B8B8B", "EF4E3E8F1CC6706E",
+ "8C8C8C8C8C8C8C8C", "8C8C8C8C8C8C8C8C", "720479B024C397EE",
+ "8D8D8D8D8D8D8D8D", "8D8D8D8D8D8D8D8D", "BEA27E3795063C89",
+ "8E8E8E8E8E8E8E8E", "8E8E8E8E8E8E8E8E", "468E5218F1A37611",
+ "8F8F8F8F8F8F8F8F", "8F8F8F8F8F8F8F8F", "50ACE16ADF66BFE8",
+ "9090909090909090", "9090909090909090", "EEA24369A19F6937",
+ "9191919191919191", "9191919191919191", "6050D369017B6E62",
+ "9292929292929292", "9292929292929292", "5B365F2FB2CD7F32",
+ "9393939393939393", "9393939393939393", "F0B00B264381DDBB",
+ "9494949494949494", "9494949494949494", "E1D23881C957B96C",
+ "9595959595959595", "9595959595959595", "D936BF54ECA8BDCE",
+ "9696969696969696", "9696969696969696", "A020003C5554F34C",
+ "9797979797979797", "9797979797979797", "6118FCEBD407281D",
+ "9898989898989898", "9898989898989898", "072E328C984DE4A2",
+ "9999999999999999", "9999999999999999", "1440B7EF9E63D3AA",
+ "9A9A9A9A9A9A9A9A", "9A9A9A9A9A9A9A9A", "79BFA264BDA57373",
+ "9B9B9B9B9B9B9B9B", "9B9B9B9B9B9B9B9B", "C50E8FC289BBD876",
+ "9C9C9C9C9C9C9C9C", "9C9C9C9C9C9C9C9C", "A399D3D63E169FA9",
+ "9D9D9D9D9D9D9D9D", "9D9D9D9D9D9D9D9D", "4B8919B667BD53AB",
+ "9E9E9E9E9E9E9E9E", "9E9E9E9E9E9E9E9E", "D66CDCAF3F6724A2",
+ "9F9F9F9F9F9F9F9F", "9F9F9F9F9F9F9F9F", "E40E81FF3F618340",
+ "A0A0A0A0A0A0A0A0", "A0A0A0A0A0A0A0A0", "10EDB8977B348B35",
+ "A1A1A1A1A1A1A1A1", "A1A1A1A1A1A1A1A1", "6446C5769D8409A0",
+ "A2A2A2A2A2A2A2A2", "A2A2A2A2A2A2A2A2", "17ED1191CA8D67A3",
+ "A3A3A3A3A3A3A3A3", "A3A3A3A3A3A3A3A3", "B6D8533731BA1318",
+ "A4A4A4A4A4A4A4A4", "A4A4A4A4A4A4A4A4", "CA439007C7245CD0",
+ "A5A5A5A5A5A5A5A5", "A5A5A5A5A5A5A5A5", "06FC7FDE1C8389E7",
+ "A6A6A6A6A6A6A6A6", "A6A6A6A6A6A6A6A6", "7A3C1F3BD60CB3D8",
+ "A7A7A7A7A7A7A7A7", "A7A7A7A7A7A7A7A7", "E415D80048DBA848",
+ "A8A8A8A8A8A8A8A8", "A8A8A8A8A8A8A8A8", "26F88D30C0FB8302",
+ "A9A9A9A9A9A9A9A9", "A9A9A9A9A9A9A9A9", "D4E00A9EF5E6D8F3",
+ "AAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAA", "C4322BE19E9A5A17",
+ "ABABABABABABABAB", "ABABABABABABABAB", "ACE41A06BFA258EA",
+ "ACACACACACACACAC", "ACACACACACACACAC", "EEAAC6D17880BD56",
+ "ADADADADADADADAD", "ADADADADADADADAD", "3C9A34CA4CB49EEB",
+ "AEAEAEAEAEAEAEAE", "AEAEAEAEAEAEAEAE", "9527B0287B75F5A3",
+ "AFAFAFAFAFAFAFAF", "AFAFAFAFAFAFAFAF", "F2D9D1BE74376C0C",
+ "B0B0B0B0B0B0B0B0", "B0B0B0B0B0B0B0B0", "939618DF0AEFAAE7",
+ "B1B1B1B1B1B1B1B1", "B1B1B1B1B1B1B1B1", "24692773CB9F27FE",
+ "B2B2B2B2B2B2B2B2", "B2B2B2B2B2B2B2B2", "38703BA5E2315D1D",
+ "B3B3B3B3B3B3B3B3", "B3B3B3B3B3B3B3B3", "FCB7E4B7D702E2FB",
+ "B4B4B4B4B4B4B4B4", "B4B4B4B4B4B4B4B4", "36F0D0B3675704D5",
+ "B5B5B5B5B5B5B5B5", "B5B5B5B5B5B5B5B5", "62D473F539FA0D8B",
+ "B6B6B6B6B6B6B6B6", "B6B6B6B6B6B6B6B6", "1533F3ED9BE8EF8E",
+ "B7B7B7B7B7B7B7B7", "B7B7B7B7B7B7B7B7", "9C4EA352599731ED",
+ "B8B8B8B8B8B8B8B8", "B8B8B8B8B8B8B8B8", "FABBF7C046FD273F",
+ "B9B9B9B9B9B9B9B9", "B9B9B9B9B9B9B9B9", "B7FE63A61C646F3A",
+ "BABABABABABABABA", "BABABABABABABABA", "10ADB6E2AB972BBE",
+ "BBBBBBBBBBBBBBBB", "BBBBBBBBBBBBBBBB", "F91DCAD912332F3B",
+ "BCBCBCBCBCBCBCBC", "BCBCBCBCBCBCBCBC", "46E7EF47323A701D",
+ "BDBDBDBDBDBDBDBD", "BDBDBDBDBDBDBDBD", "8DB18CCD9692F758",
+ "BEBEBEBEBEBEBEBE", "BEBEBEBEBEBEBEBE", "E6207B536AAAEFFC",
+ "BFBFBFBFBFBFBFBF", "BFBFBFBFBFBFBFBF", "92AA224372156A00",
+ "C0C0C0C0C0C0C0C0", "C0C0C0C0C0C0C0C0", "A3B357885B1E16D2",
+ "C1C1C1C1C1C1C1C1", "C1C1C1C1C1C1C1C1", "169F7629C970C1E5",
+ "C2C2C2C2C2C2C2C2", "C2C2C2C2C2C2C2C2", "62F44B247CF1348C",
+ "C3C3C3C3C3C3C3C3", "C3C3C3C3C3C3C3C3", "AE0FEEB0495932C8",
+ "C4C4C4C4C4C4C4C4", "C4C4C4C4C4C4C4C4", "72DAF2A7C9EA6803",
+ "C5C5C5C5C5C5C5C5", "C5C5C5C5C5C5C5C5", "4FB5D5536DA544F4",
+ "C6C6C6C6C6C6C6C6", "C6C6C6C6C6C6C6C6", "1DD4E65AAF7988B4",
+ "C7C7C7C7C7C7C7C7", "C7C7C7C7C7C7C7C7", "76BF084C1535A6C6",
+ "C8C8C8C8C8C8C8C8", "C8C8C8C8C8C8C8C8", "AFEC35B09D36315F",
+ "C9C9C9C9C9C9C9C9", "C9C9C9C9C9C9C9C9", "C8078A6148818403",
+ "CACACACACACACACA", "CACACACACACACACA", "4DA91CB4124B67FE",
+ "CBCBCBCBCBCBCBCB", "CBCBCBCBCBCBCBCB", "2DABFEB346794C3D",
+ "CCCCCCCCCCCCCCCC", "CCCCCCCCCCCCCCCC", "FBCD12C790D21CD7",
+ "CDCDCDCDCDCDCDCD", "CDCDCDCDCDCDCDCD", "536873DB879CC770",
+ "CECECECECECECECE", "CECECECECECECECE", "9AA159D7309DA7A0",
+ "CFCFCFCFCFCFCFCF", "CFCFCFCFCFCFCFCF", "0B844B9D8C4EA14A",
+ "D0D0D0D0D0D0D0D0", "D0D0D0D0D0D0D0D0", "3BBD84CE539E68C4",
+ "D1D1D1D1D1D1D1D1", "D1D1D1D1D1D1D1D1", "CF3E4F3E026E2C8E",
+ "D2D2D2D2D2D2D2D2", "D2D2D2D2D2D2D2D2", "82F85885D542AF58",
+ "D3D3D3D3D3D3D3D3", "D3D3D3D3D3D3D3D3", "22D334D6493B3CB6",
+ "D4D4D4D4D4D4D4D4", "D4D4D4D4D4D4D4D4", "47E9CB3E3154D673",
+ "D5D5D5D5D5D5D5D5", "D5D5D5D5D5D5D5D5", "2352BCC708ADC7E9",
+ "D6D6D6D6D6D6D6D6", "D6D6D6D6D6D6D6D6", "8C0F3BA0C8601980",
+ "D7D7D7D7D7D7D7D7", "D7D7D7D7D7D7D7D7", "EE5E9FD70CEF00E9",
+ "D8D8D8D8D8D8D8D8", "D8D8D8D8D8D8D8D8", "DEF6BDA6CABF9547",
+ "D9D9D9D9D9D9D9D9", "D9D9D9D9D9D9D9D9", "4DADD04A0EA70F20",
+ "DADADADADADADADA", "DADADADADADADADA", "C1AA16689EE1B482",
+ "DBDBDBDBDBDBDBDB", "DBDBDBDBDBDBDBDB", "F45FC26193E69AEE",
+ "DCDCDCDCDCDCDCDC", "DCDCDCDCDCDCDCDC", "D0CFBB937CEDBFB5",
+ "DDDDDDDDDDDDDDDD", "DDDDDDDDDDDDDDDD", "F0752004EE23D87B",
+ "DEDEDEDEDEDEDEDE", "DEDEDEDEDEDEDEDE", "77A791E28AA464A5",
+ "DFDFDFDFDFDFDFDF", "DFDFDFDFDFDFDFDF", "E7562A7F56FF4966",
+ "E0E0E0E0E0E0E0E0", "E0E0E0E0E0E0E0E0", "B026913F2CCFB109",
+ "E1E1E1E1E1E1E1E1", "E1E1E1E1E1E1E1E1", "0DB572DDCE388AC7",
+ "E2E2E2E2E2E2E2E2", "E2E2E2E2E2E2E2E2", "D9FA6595F0C094CA",
+ "E3E3E3E3E3E3E3E3", "E3E3E3E3E3E3E3E3", "ADE4804C4BE4486E",
+ "E4E4E4E4E4E4E4E4", "E4E4E4E4E4E4E4E4", "007B81F520E6D7DA",
+ "E5E5E5E5E5E5E5E5", "E5E5E5E5E5E5E5E5", "961AEB77BFC10B3C",
+ "E6E6E6E6E6E6E6E6", "E6E6E6E6E6E6E6E6", "8A8DD870C9B14AF2",
+ "E7E7E7E7E7E7E7E7", "E7E7E7E7E7E7E7E7", "3CC02E14B6349B25",
+ "E8E8E8E8E8E8E8E8", "E8E8E8E8E8E8E8E8", "BAD3EE68BDDB9607",
+ "E9E9E9E9E9E9E9E9", "E9E9E9E9E9E9E9E9", "DFF918E93BDAD292",
+ "EAEAEAEAEAEAEAEA", "EAEAEAEAEAEAEAEA", "8FE559C7CD6FA56D",
+ "EBEBEBEBEBEBEBEB", "EBEBEBEBEBEBEBEB", "C88480835C1A444C",
+ "ECECECECECECECEC", "ECECECECECECECEC", "D6EE30A16B2CC01E",
+ "EDEDEDEDEDEDEDED", "EDEDEDEDEDEDEDED", "6932D887B2EA9C1A",
+ "EEEEEEEEEEEEEEEE", "EEEEEEEEEEEEEEEE", "0BFC865461F13ACC",
+ "EFEFEFEFEFEFEFEF", "EFEFEFEFEFEFEFEF", "228AEA0D403E807A",
+ "F0F0F0F0F0F0F0F0", "F0F0F0F0F0F0F0F0", "2A2891F65BB8173C",
+ "F1F1F1F1F1F1F1F1", "F1F1F1F1F1F1F1F1", "5D1B8FAF7839494B",
+ "F2F2F2F2F2F2F2F2", "F2F2F2F2F2F2F2F2", "1C0A9280EECF5D48",
+ "F3F3F3F3F3F3F3F3", "F3F3F3F3F3F3F3F3", "6CBCE951BBC30F74",
+ "F4F4F4F4F4F4F4F4", "F4F4F4F4F4F4F4F4", "9CA66E96BD08BC70",
+ "F5F5F5F5F5F5F5F5", "F5F5F5F5F5F5F5F5", "F5D779FCFBB28BF3",
+ "F6F6F6F6F6F6F6F6", "F6F6F6F6F6F6F6F6", "0FEC6BBF9B859184",
+ "F7F7F7F7F7F7F7F7", "F7F7F7F7F7F7F7F7", "EF88D2BF052DBDA8",
+ "F8F8F8F8F8F8F8F8", "F8F8F8F8F8F8F8F8", "39ADBDDB7363090D",
+ "F9F9F9F9F9F9F9F9", "F9F9F9F9F9F9F9F9", "C0AEAF445F7E2A7A",
+ "FAFAFAFAFAFAFAFA", "FAFAFAFAFAFAFAFA", "C66F54067298D4E9",
+ "FBFBFBFBFBFBFBFB", "FBFBFBFBFBFBFBFB", "E0BA8F4488AAF97C",
+ "FCFCFCFCFCFCFCFC", "FCFCFCFCFCFCFCFC", "67B36E2875D9631C",
+ "FDFDFDFDFDFDFDFD", "FDFDFDFDFDFDFDFD", "1ED83D49E267191D",
+ "FEFEFEFEFEFEFEFE", "FEFEFEFEFEFEFEFE", "66B2B23EA84693AD",
+ "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "7359B2163E4EDC58",
+ "0001020304050607", "0011223344556677", "3EF0A891CF8ED990",
+ "2BD6459F82C5B300", "EA024714AD5C4D84", "126EFE8ED312190A",
+
+ NULL
+};
+
+/*
+ * Known-answer tests for DES/3DES in CBC mode. Order: key, IV,
+ * plaintext, ciphertext.
+ */
+static const char *const KAT_DES_CBC[] = {
+ /*
+ * From NIST validation suite (tdesmmt.zip).
+ */
+ "34a41a8c293176c1b30732ecfe38ae8a34a41a8c293176c1",
+ "f55b4855228bd0b4",
+ "7dd880d2a9ab411c",
+ "c91892948b6cadb4",
+
+ "70a88fa1dfb9942fa77f40157ffef2ad70a88fa1dfb9942f",
+ "ece08ce2fdc6ce80",
+ "bc225304d5a3a5c9918fc5006cbc40cc",
+ "27f67dc87af7ddb4b68f63fa7c2d454a",
+
+ "e091790be55be0bc0780153861a84adce091790be55be0bc",
+ "fd7d430f86fbbffe",
+ "03c7fffd7f36499c703dedc9df4de4a92dd4382e576d6ae9",
+ "053aeba85dd3a23bfbe8440a432f9578f312be60fb9f0035",
+
+ "857feacd16157c58e5347a70e56e578a857feacd16157c58",
+ "002dcb6d46ef0969",
+ "1f13701c7f0d7385307507a18e89843ebd295bd5e239ef109347a6898c6d3fd5",
+ "a0e4edde34f05bd8397ce279e49853e9387ba04be562f5fa19c3289c3f5a3391",
+
+ "a173545b265875ba852331fbb95b49a8a173545b265875ba",
+ "ab385756391d364c",
+ "d08894c565608d9ae51dda63b85b3b33b1703bb5e4f1abcbb8794e743da5d6f3bf630f2e9b6d5b54",
+ "370b47acf89ac6bdbb13c9a7336787dc41e1ad8beead32281d0609fb54968404bdf2894892590658",
+
+ "26376bcb2f23df1083cd684fe00ed3c726376bcb2f23df10",
+ "33acfb0f3d240ea6",
+ "903a1911da1e6877f23c1985a9b61786ef438e0ce1240885035ad60fc916b18e5d71a1fb9c5d1eff61db75c0076f6efb",
+ "7a4f7510f6ec0b93e2495d21a8355684d303a770ebda2e0e51ff33d72b20cb73e58e2e3de2ef6b2e12c504c0f181ba63",
+
+ "3e1f98135d027cec752f67765408a7913e1f98135d027cec",
+ "11f5f2304b28f68b",
+ "7c022f5af24f7925d323d4d0e20a2ce49272c5e764b22c806f4b6ddc406d864fe5bd1c3f45556d3eb30c8676c2f8b54a5a32423a0bd95a07",
+ "2bb4b131fa4ae0b4f0378a2cdb68556af6eee837613016d7ea936f3931f25f8b3ae351d5e9d00be665676e2400408b5db9892d95421e7f1a",
+
+ "13b9d549cd136ec7bf9e9810ef2cdcbf13b9d549cd136ec7",
+ "a82c1b1057badcc8",
+ "1fff1563bc1645b55cb23ea34a0049dfc06607150614b621dedcb07f20433402a2d869c95ac4a070c7a3da838c928a385f899c5d21ecb58f4e5cbdad98d39b8c",
+ "75f804d4a2c542a31703e23df26cc38861a0729090e6eae5672c1db8c0b09fba9b125bbca7d6c7d330b3859e6725c6d26de21c4e3af7f5ea94df3cde2349ce37",
+
+ "20320dfdad579bb57c6e4acd769dbadf20320dfdad579bb5",
+ "879201b5857ccdea",
+ "0431283cc8bb4dc7750a9d5c68578486932091632a12d0a79f2c54e3d122130881fff727050f317a40fcd1a8d13793458b99fc98254ba6a233e3d95b55cf5a3faff78809999ea4bf",
+ "85d17840eb2af5fc727027336bfd71a2b31bd14a1d9eb64f8a08bfc4f56eaa9ca7654a5ae698287869cc27324813730de4f1384e0b8cfbc472ff5470e3c5e4bd8ceb23dc2d91988c",
+
+ "23abb073a2df34cb3d1fdce6b092582c23abb073a2df34cb",
+ "7d7fbf19e8562d32",
+ "31e718fd95e6d7ca4f94763191add2674ab07c909d88c486916c16d60a048a0cf8cdb631cebec791362cd0c202eb61e166b65c1f65d0047c8aec57d3d84b9e17032442dce148e1191b06a12c284cc41e",
+ "c9a3f75ab6a7cd08a7fd53ca540aafe731d257ee1c379fadcc4cc1a06e7c12bddbeb7562c436d1da849ed072629e82a97b56d9becc25ff4f16f21c5f2a01911604f0b5c49df96cb641faee662ca8aa68",
+
+ "b5cb1504802326c73df186e3e352a20de643b0d63ee30e37",
+ "43f791134c5647ba",
+ "dcc153cef81d6f24",
+ "92538bd8af18d3ba",
+
+ "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358",
+ "c2e999cb6249023c",
+ "c689aee38a301bb316da75db36f110b5",
+ "e9afaba5ec75ea1bbe65506655bb4ecb",
+
+ "1a5d4c0825072a15a8ad9dfdaeda8c048adffb85bc4fced0",
+ "7fcfa736f7548b6f",
+ "983c3edacd939406010e1bc6ff9e12320ac5008117fa8f84",
+ "d84fa24f38cf451ca2c9adc960120bd8ff9871584fe31cee",
+
+ "d98aadc76d4a3716158c32866efbb9ce834af2297379a49d",
+ "3c5220327c502b44",
+ "6174079dda53ca723ebf00a66837f8d5ce648c08acaa5ee45ffe62210ef79d3e",
+ "f5bd4d600bed77bec78409e3530ebda1d815506ed53103015b87e371ae000958",
+
+ "ef6d3e54266d978ffb0b8ce6689d803e2cd34cc802fd0252",
+ "38bae5bce06d0ad9",
+ "c4f228b537223cd01c0debb5d9d4e12ba71656618d119b2f8f0af29d23efa3a9e43c4c458a1b79a0",
+ "9e3289fb18379f55aa4e45a7e0e6df160b33b75f8627ad0954f8fdcb78cee55a4664caeda1000fe5",
+
+ "625bc19b19df83abfb2f5bec9d4f2062017525a75bc26e70",
+ "bd0cff364ff69a91",
+ "8152d2ab876c3c8201403a5a406d3feaf27319dbea6ad01e24f4d18203704b86de70da6bbb6d638e5aba3ff576b79b28",
+ "706fe7a973fac40e25b2b4499ce527078944c70e976d017b6af86a3a7a6b52943a72ba18a58000d2b61fdc3bfef2bc4a",
+
+ "b6383176046e6880a1023bf45768b5bf5119022fe054bfe5",
+ "ec13ca541c43401e",
+ "cd5a886e9af011346c4dba36a424f96a78a1ddf28aaa4188bf65451f4efaffc7179a6dd237c0ae35d9b672314e5cb032612597f7e462c6f3",
+ "b030f976f46277ee211c4a324d5c87555d1084513a1223d3b84416b52bbc28f4b77f3a9d8d0d91dc37d3dbe8af8be98f74674b02f9a38527",
+
+ "3d8cf273d343b9aedccddacb91ad86206737adc86b4a49a7",
+ "bb3a9a0c71c62ef0",
+ "1fde3991c32ce220b5b6666a9234f2fd7bd24b921829fd9cdc6eb4218be9eac9faa9c2351777349128086b6d58776bc86ff2f76ee1b3b2850a318462b8983fa1",
+ "422ce705a46bb52ad928dab6c863166d617c6fc24003633120d91918314bbf464cea7345c3c35f2042f2d6929735d74d7728f22fea618a0b9cf5b1281acb13fb",
+
+ "fbceb5cb646b925be0b92f7f6b493d5e5b16e9159732732a",
+ "2e17b3c7025ae86b",
+ "4c309bc8e1e464fdd2a2b8978645d668d455f7526bd8d7b6716a722f6a900b815c4a73cc30e788065c1dfca7bf5958a6cc5440a5ebe7f8691c20278cde95db764ff8ce8994ece89c",
+ "c02129bdf4bbbd75e71605a00b12c80db6b4e05308e916615011f09147ed915dd1bc67f27f9e027e4e13df36b55464a31c11b4d1fe3d855d89df492e1a7201b995c1ba16a8dbabee",
+
+ "9b162a0df8ad9b61c88676e3d586434570b902f12a2046e0",
+ "ebd6fefe029ad54b",
+ "f4c1c918e77355c8156f0fd778da52bff121ae5f2f44eaf4d2754946d0e10d1f18ce3a0176e69c18b7d20b6e0d0bee5eb5edfe4bd60e4d92adcd86bce72e76f94ee5cbcaa8b01cfddcea2ade575e66ac",
+ "1ff3c8709f403a8eff291aedf50c010df5c5ff64a8b205f1fce68564798897a390db16ee0d053856b75898009731da290fcc119dad987277aacef694872e880c4bb41471063fae05c89f25e4bd0cad6a",
+
+ NULL
+};
+
+static void
+xor_buf(unsigned char *dst, const unsigned char *src, size_t len)
+{
+ while (len -- > 0) {
+ *dst ++ ^= *src ++;
+ }
+}
+
+static void
+monte_carlo_DES_encrypt(const br_block_cbcenc_class *ve)
+{
+ unsigned char k1[8], k2[8], k3[8];
+ unsigned char buf[8];
+ unsigned char cipher[8];
+ int i, j;
+ br_des_gen_cbcenc_keys v_ec;
+ void *ec;
+
+ ec = &v_ec;
+ hextobin(k1, "9ec2372c86379df4");
+ hextobin(k2, "ad7ac4464f73805d");
+ hextobin(k3, "20c4f87564527c91");
+ hextobin(buf, "b624d6bd41783ab1");
+ hextobin(cipher, "eafd97b190b167fe");
+ for (i = 0; i < 400; i ++) {
+ unsigned char key[24];
+
+ memcpy(key, k1, 8);
+ memcpy(key + 8, k2, 8);
+ memcpy(key + 16, k3, 8);
+ ve->init(ec, key, sizeof key);
+ for (j = 0; j < 10000; j ++) {
+ unsigned char iv[8];
+
+ memset(iv, 0, sizeof iv);
+ ve->run(ec, iv, buf, sizeof buf);
+ switch (j) {
+ case 9997: xor_buf(k3, buf, 8); break;
+ case 9998: xor_buf(k2, buf, 8); break;
+ case 9999: xor_buf(k1, buf, 8); break;
+ }
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+ check_equals("MC DES encrypt", buf, cipher, sizeof buf);
+}
+
+static void
+monte_carlo_DES_decrypt(const br_block_cbcdec_class *vd)
+{
+ unsigned char k1[8], k2[8], k3[8];
+ unsigned char buf[8];
+ unsigned char plain[8];
+ int i, j;
+ br_des_gen_cbcdec_keys v_dc;
+ void *dc;
+
+ dc = &v_dc;
+ hextobin(k1, "79b63486e0ce37e0");
+ hextobin(k2, "08e65231abae3710");
+ hextobin(k3, "1f5eb69e925ef185");
+ hextobin(buf, "2783aa729432fe96");
+ hextobin(plain, "44937ca532cdbf98");
+ for (i = 0; i < 400; i ++) {
+ unsigned char key[24];
+
+ memcpy(key, k1, 8);
+ memcpy(key + 8, k2, 8);
+ memcpy(key + 16, k3, 8);
+ vd->init(dc, key, sizeof key);
+ for (j = 0; j < 10000; j ++) {
+ unsigned char iv[8];
+
+ memset(iv, 0, sizeof iv);
+ vd->run(dc, iv, buf, sizeof buf);
+ switch (j) {
+ case 9997: xor_buf(k3, buf, 8); break;
+ case 9998: xor_buf(k2, buf, 8); break;
+ case 9999: xor_buf(k1, buf, 8); break;
+ }
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+ check_equals("MC DES decrypt", buf, plain, sizeof buf);
+}
+
+static void
+test_DES_generic(char *name,
+ const br_block_cbcenc_class *ve,
+ const br_block_cbcdec_class *vd,
+ int with_MC, int with_CBC)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ if (ve->block_size != 8 || vd->block_size != 8) {
+ fprintf(stderr, "%s failed: wrong block size\n", name);
+ exit(EXIT_FAILURE);
+ }
+
+ for (u = 0; KAT_DES[u]; u += 3) {
+ unsigned char key[24];
+ unsigned char plain[8];
+ unsigned char cipher[8];
+ unsigned char buf[8];
+ unsigned char iv[8];
+ size_t key_len;
+ br_des_gen_cbcenc_keys v_ec;
+ br_des_gen_cbcdec_keys v_dc;
+ const br_block_cbcenc_class **ec;
+ const br_block_cbcdec_class **dc;
+
+ ec = &v_ec.vtable;
+ dc = &v_dc.vtable;
+ key_len = hextobin(key, KAT_DES[u]);
+ hextobin(plain, KAT_DES[u + 1]);
+ hextobin(cipher, KAT_DES[u + 2]);
+ ve->init(ec, key, key_len);
+ memcpy(buf, plain, sizeof plain);
+ memset(iv, 0, sizeof iv);
+ ve->run(ec, iv, buf, sizeof buf);
+ check_equals("KAT DES encrypt", buf, cipher, sizeof cipher);
+ vd->init(dc, key, key_len);
+ memset(iv, 0, sizeof iv);
+ vd->run(dc, iv, buf, sizeof buf);
+ check_equals("KAT DES decrypt", buf, plain, sizeof plain);
+
+ if (key_len == 8) {
+ memcpy(key + 8, key, 8);
+ memcpy(key + 16, key, 8);
+ ve->init(ec, key, 24);
+ memcpy(buf, plain, sizeof plain);
+ memset(iv, 0, sizeof iv);
+ ve->run(ec, iv, buf, sizeof buf);
+ check_equals("KAT DES->3 encrypt",
+ buf, cipher, sizeof cipher);
+ vd->init(dc, key, 24);
+ memset(iv, 0, sizeof iv);
+ vd->run(dc, iv, buf, sizeof buf);
+ check_equals("KAT DES->3 decrypt",
+ buf, plain, sizeof plain);
+ }
+ }
+
+ if (with_CBC) {
+ for (u = 0; KAT_DES_CBC[u]; u += 4) {
+ unsigned char key[24];
+ unsigned char ivref[8];
+ unsigned char plain[200];
+ unsigned char cipher[200];
+ unsigned char buf[200];
+ unsigned char iv[8];
+ size_t key_len, data_len, v;
+ br_des_gen_cbcenc_keys v_ec;
+ br_des_gen_cbcdec_keys v_dc;
+ const br_block_cbcenc_class **ec;
+ const br_block_cbcdec_class **dc;
+
+ ec = &v_ec.vtable;
+ dc = &v_dc.vtable;
+ key_len = hextobin(key, KAT_DES_CBC[u]);
+ hextobin(ivref, KAT_DES_CBC[u + 1]);
+ data_len = hextobin(plain, KAT_DES_CBC[u + 2]);
+ hextobin(cipher, KAT_DES_CBC[u + 3]);
+ ve->init(ec, key, key_len);
+
+ memcpy(buf, plain, data_len);
+ memcpy(iv, ivref, 8);
+ ve->run(ec, iv, buf, data_len);
+ check_equals("KAT CBC DES encrypt",
+ buf, cipher, data_len);
+ vd->init(dc, key, key_len);
+ memcpy(iv, ivref, 8);
+ vd->run(dc, iv, buf, data_len);
+ check_equals("KAT CBC DES decrypt",
+ buf, plain, data_len);
+
+ memcpy(buf, plain, data_len);
+ memcpy(iv, ivref, 8);
+ for (v = 0; v < data_len; v += 8) {
+ ve->run(ec, iv, buf + v, 8);
+ }
+ check_equals("KAT CBC DES encrypt (2)",
+ buf, cipher, data_len);
+ memcpy(iv, ivref, 8);
+ for (v = 0; v < data_len; v += 8) {
+ vd->run(dc, iv, buf + v, 8);
+ }
+ check_equals("KAT CBC DES decrypt (2)",
+ buf, plain, data_len);
+ }
+ }
+
+ if (with_MC) {
+ monte_carlo_DES_encrypt(ve);
+ monte_carlo_DES_decrypt(vd);
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_DES_tab(void)
+{
+ test_DES_generic("DES_tab",
+ &br_des_tab_cbcenc_vtable,
+ &br_des_tab_cbcdec_vtable,
+ 1, 1);
+}
+
+static void
+test_DES_ct(void)
+{
+ test_DES_generic("DES_ct",
+ &br_des_ct_cbcenc_vtable,
+ &br_des_ct_cbcdec_vtable,
+ 1, 1);
+}
+
+static const struct {
+ const char *skey;
+ const char *snonce;
+ uint32_t counter;
+ const char *splain;
+ const char *scipher;
+} KAT_CHACHA20[] = {
+ {
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "000000000000000000000000",
+ 0,
+ "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
+ "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"
+ },
+ {
+ "0000000000000000000000000000000000000000000000000000000000000001",
+ "000000000000000000000002",
+ 1,
+ "416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f",
+ "a3fbf07df3fa2fde4f376ca23e82737041605d9f4f4f57bd8cff2c1d4b7955ec2a97948bd3722915c8f3d337f7d370050e9e96d647b7c39f56e031ca5eb6250d4042e02785ececfa4b4bb5e8ead0440e20b6e8db09d881a7c6132f420e52795042bdfa7773d8a9051447b3291ce1411c680465552aa6c405b7764d5e87bea85ad00f8449ed8f72d0d662ab052691ca66424bc86d2df80ea41f43abf937d3259dc4b2d0dfb48a6c9139ddd7f76966e928e635553ba76c5c879d7b35d49eb2e62b0871cdac638939e25e8a1e0ef9d5280fa8ca328b351c3c765989cbcf3daa8b6ccc3aaf9f3979c92b3720fc88dc95ed84a1be059c6499b9fda236e7e818b04b0bc39c1e876b193bfe5569753f88128cc08aaa9b63d1a16f80ef2554d7189c411f5869ca52c5b83fa36ff216b9c1d30062bebcfd2dc5bce0911934fda79a86f6e698ced759c3ff9b6477338f3da4f9cd8514ea9982ccafb341b2384dd902f3d1ab7ac61dd29c6f21ba5b862f3730e37cfdc4fd806c22f221"
+ },
+ {
+ "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
+ "000000000000000000000002",
+ 42,
+ "2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e",
+ "62e6347f95ed87a45ffae7426f27a1df5fb69110044c0d73118effa95b01e5cf166d3df2d721caf9b21e5fb14c616871fd84c54f9d65b283196c7fe4f60553ebf39c6402c42234e32a356b3e764312a61a5532055716ead6962568f87d3f3f7704c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1"
+ },
+ { 0, 0, 0, 0, 0 }
+};
+
+static void
+test_ChaCha20_generic(const char *name, br_chacha20_run cr)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+ if (cr == 0) {
+ printf("UNAVAILABLE\n");
+ return;
+ }
+
+ for (u = 0; KAT_CHACHA20[u].skey; u ++) {
+ unsigned char key[32], nonce[12], plain[400], cipher[400];
+ uint32_t cc;
+ size_t v, len;
+
+ hextobin(key, KAT_CHACHA20[u].skey);
+ hextobin(nonce, KAT_CHACHA20[u].snonce);
+ cc = KAT_CHACHA20[u].counter;
+ len = hextobin(plain, KAT_CHACHA20[u].splain);
+ hextobin(cipher, KAT_CHACHA20[u].scipher);
+
+ for (v = 0; v < len; v ++) {
+ unsigned char tmp[400];
+ size_t w;
+ uint32_t cc2;
+
+ memset(tmp, 0, sizeof tmp);
+ memcpy(tmp, plain, v);
+ if (cr(key, nonce, cc, tmp, v)
+ != cc + (uint32_t)((v + 63) >> 6))
+ {
+ fprintf(stderr, "ChaCha20: wrong counter\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(tmp, cipher, v) != 0) {
+ fprintf(stderr, "ChaCha20 KAT fail (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ for (w = v; w < sizeof tmp; w ++) {
+ if (tmp[w] != 0) {
+ fprintf(stderr, "ChaCha20: overrun\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ for (w = 0, cc2 = cc; w < v; w += 64, cc2 ++) {
+ size_t x;
+
+ x = v - w;
+ if (x > 64) {
+ x = 64;
+ }
+ if (cr(key, nonce, cc2, tmp + w, x)
+ != (cc2 + 1))
+ {
+ fprintf(stderr, "ChaCha20:"
+ " wrong counter (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (memcmp(tmp, plain, v) != 0) {
+ fprintf(stderr, "ChaCha20 KAT fail (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_ChaCha20_ct(void)
+{
+ test_ChaCha20_generic("ChaCha20_ct", &br_chacha20_ct_run);
+}
+
+static void
+test_ChaCha20_sse2(void)
+{
+ test_ChaCha20_generic("ChaCha20_sse2", br_chacha20_sse2_get());
+}
+
+static const struct {
+ const char *splain;
+ const char *saad;
+ const char *skey;
+ const char *snonce;
+ const char *scipher;
+ const char *stag;
+} KAT_POLY1305[] = {
+ {
+ "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e",
+ "50515253c0c1c2c3c4c5c6c7",
+ "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
+ "070000004041424344454647",
+ "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116",
+ "1ae10b594f09e26a7e902ecbd0600691"
+ },
+ { 0, 0, 0, 0, 0, 0 }
+};
+
+static void
+test_Poly1305_inner(const char *name, br_poly1305_run ipoly,
+ br_poly1305_run iref)
+{
+ size_t u;
+ br_hmac_drbg_context rng;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ for (u = 0; KAT_POLY1305[u].skey; u ++) {
+ unsigned char key[32], nonce[12], plain[400], cipher[400];
+ unsigned char aad[400], tag[16], data[400], tmp[16];
+ size_t len, aad_len;
+
+ len = hextobin(plain, KAT_POLY1305[u].splain);
+ aad_len = hextobin(aad, KAT_POLY1305[u].saad);
+ hextobin(key, KAT_POLY1305[u].skey);
+ hextobin(nonce, KAT_POLY1305[u].snonce);
+ hextobin(cipher, KAT_POLY1305[u].scipher);
+ hextobin(tag, KAT_POLY1305[u].stag);
+
+ memcpy(data, plain, len);
+ ipoly(key, nonce, data, len,
+ aad, aad_len, tmp, br_chacha20_ct_run, 1);
+ check_equals("ChaCha20+Poly1305 KAT (1)", data, cipher, len);
+ check_equals("ChaCha20+Poly1305 KAT (2)", tmp, tag, 16);
+ ipoly(key, nonce, data, len,
+ aad, aad_len, tmp, br_chacha20_ct_run, 0);
+ check_equals("ChaCha20+Poly1305 KAT (3)", data, plain, len);
+ check_equals("ChaCha20+Poly1305 KAT (4)", tmp, tag, 16);
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" ");
+ fflush(stdout);
+
+ /*
+ * We compare the "ipoly" and "iref" implementations together on
+ * a bunch of pseudo-random messages.
+ */
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, "seed for Poly1305", 17);
+ for (u = 0; u < 100; u ++) {
+ unsigned char plain[100], aad[100], tmp[100];
+ unsigned char key[32], iv[12], tag1[16], tag2[16];
+
+ br_hmac_drbg_generate(&rng, key, sizeof key);
+ br_hmac_drbg_generate(&rng, iv, sizeof iv);
+ br_hmac_drbg_generate(&rng, plain, u);
+ br_hmac_drbg_generate(&rng, aad, u);
+ memcpy(tmp, plain, u);
+ memset(tmp + u, 0xFF, (sizeof tmp) - u);
+ ipoly(key, iv, tmp, u, aad, u, tag1,
+ &br_chacha20_ct_run, 1);
+ memset(tmp + u, 0x00, (sizeof tmp) - u);
+ iref(key, iv, tmp, u, aad, u, tag2,
+ &br_chacha20_ct_run, 0);
+ if (memcmp(tmp, plain, u) != 0) {
+ fprintf(stderr, "cross enc/dec failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(tag1, tag2, sizeof tag1) != 0) {
+ fprintf(stderr, "cross MAC failed\n");
+ exit(EXIT_FAILURE);
+ }
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_Poly1305_ctmul(void)
+{
+ test_Poly1305_inner("Poly1305_ctmul", &br_poly1305_ctmul_run,
+ &br_poly1305_i15_run);
+}
+
+static void
+test_Poly1305_ctmul32(void)
+{
+ test_Poly1305_inner("Poly1305_ctmul32", &br_poly1305_ctmul32_run,
+ &br_poly1305_i15_run);
+}
+
+static void
+test_Poly1305_i15(void)
+{
+ test_Poly1305_inner("Poly1305_i15", &br_poly1305_i15_run,
+ &br_poly1305_ctmul_run);
+}
+
+static void
+test_Poly1305_ctmulq(void)
+{
+ br_poly1305_run bp;
+
+ bp = br_poly1305_ctmulq_get();
+ if (bp == 0) {
+ printf("Test Poly1305_ctmulq: UNAVAILABLE\n");
+ } else {
+ test_Poly1305_inner("Poly1305_ctmulq", bp,
+ &br_poly1305_ctmul_run);
+ }
+}
+
+/*
+ * A 1024-bit RSA key, generated with OpenSSL.
+ */
+static const unsigned char RSA_N[] = {
+ 0xBF, 0xB4, 0xA6, 0x2E, 0x87, 0x3F, 0x9C, 0x8D,
+ 0xA0, 0xC4, 0x2E, 0x7B, 0x59, 0x36, 0x0F, 0xB0,
+ 0xFF, 0xE1, 0x25, 0x49, 0xE5, 0xE6, 0x36, 0xB0,
+ 0x48, 0xC2, 0x08, 0x6B, 0x77, 0xA7, 0xC0, 0x51,
+ 0x66, 0x35, 0x06, 0xA9, 0x59, 0xDF, 0x17, 0x7F,
+ 0x15, 0xF6, 0xB4, 0xE5, 0x44, 0xEE, 0x72, 0x3C,
+ 0x53, 0x11, 0x52, 0xC9, 0xC9, 0x61, 0x4F, 0x92,
+ 0x33, 0x64, 0x70, 0x43, 0x07, 0xF1, 0x3F, 0x7F,
+ 0x15, 0xAC, 0xF0, 0xC1, 0x54, 0x7D, 0x55, 0xC0,
+ 0x29, 0xDC, 0x9E, 0xCC, 0xE4, 0x1D, 0x11, 0x72,
+ 0x45, 0xF4, 0xD2, 0x70, 0xFC, 0x34, 0xB2, 0x1F,
+ 0xF3, 0xAD, 0x6A, 0xF0, 0xE5, 0x56, 0x11, 0xF8,
+ 0x0C, 0x3A, 0x8B, 0x04, 0x46, 0x7C, 0x77, 0xD9,
+ 0x41, 0x1F, 0x40, 0xBE, 0x93, 0x80, 0x9D, 0x23,
+ 0x75, 0x80, 0x12, 0x26, 0x5A, 0x72, 0x1C, 0xDD,
+ 0x47, 0xB3, 0x2A, 0x33, 0xD8, 0x19, 0x61, 0xE3
+};
+static const unsigned char RSA_E[] = {
+ 0x01, 0x00, 0x01
+};
+/* unused
+static const unsigned char RSA_D[] = {
+ 0xAE, 0x56, 0x0B, 0x56, 0x7E, 0xDA, 0x83, 0x75,
+ 0x6C, 0xC1, 0x5C, 0x00, 0x02, 0x96, 0x1E, 0x58,
+ 0xF9, 0xA9, 0xF7, 0x2E, 0x27, 0xEB, 0x5E, 0xCA,
+ 0x9B, 0xB0, 0x10, 0xD6, 0x22, 0x7F, 0xA4, 0x6E,
+ 0xA2, 0x03, 0x10, 0xE6, 0xCB, 0x7B, 0x0D, 0x34,
+ 0x1E, 0x76, 0x37, 0xF5, 0xD3, 0xE5, 0x00, 0x70,
+ 0x09, 0x9E, 0xD4, 0x69, 0xFB, 0x40, 0x0A, 0x8B,
+ 0xCB, 0x3E, 0xC8, 0xB4, 0xBC, 0xB1, 0x50, 0xEA,
+ 0x9D, 0xD9, 0x89, 0x8A, 0x98, 0x40, 0x79, 0xD1,
+ 0x07, 0x66, 0xA7, 0x90, 0x63, 0x82, 0xB1, 0xE0,
+ 0x24, 0xD0, 0x89, 0x6A, 0xEC, 0xC5, 0xF3, 0x21,
+ 0x7D, 0xB8, 0xA5, 0x45, 0x3A, 0x3B, 0x34, 0x42,
+ 0xC2, 0x82, 0x3C, 0x8D, 0xFA, 0x5D, 0xA0, 0xA8,
+ 0x24, 0xC8, 0x40, 0x22, 0x19, 0xCB, 0xB5, 0x85,
+ 0x67, 0x69, 0x60, 0xE4, 0xD0, 0x7E, 0xA3, 0x3B,
+ 0xF7, 0x70, 0x50, 0xC9, 0x5C, 0x97, 0x29, 0x49
+};
+*/
+static const unsigned char RSA_P[] = {
+ 0xF2, 0xE7, 0x6F, 0x66, 0x2E, 0xC4, 0x03, 0xD4,
+ 0x89, 0x24, 0xCC, 0xE1, 0xCD, 0x3F, 0x01, 0x82,
+ 0xC1, 0xFB, 0xAF, 0x44, 0xFA, 0xCC, 0x0E, 0xAA,
+ 0x9D, 0x74, 0xA9, 0x65, 0xEF, 0xED, 0x4C, 0x87,
+ 0xF0, 0xB3, 0xC6, 0xEA, 0x61, 0x85, 0xDE, 0x4E,
+ 0x66, 0xB2, 0x5A, 0x9F, 0x7A, 0x41, 0xC5, 0x66,
+ 0x57, 0xDF, 0x88, 0xF0, 0xB5, 0xF2, 0xC7, 0x7E,
+ 0xE6, 0x55, 0x21, 0x96, 0x83, 0xD8, 0xAB, 0x57
+};
+static const unsigned char RSA_Q[] = {
+ 0xCA, 0x0A, 0x92, 0xBF, 0x58, 0xB0, 0x2E, 0xF6,
+ 0x66, 0x50, 0xB1, 0x48, 0x29, 0x42, 0x86, 0x6C,
+ 0x98, 0x06, 0x7E, 0xB8, 0xB5, 0x4F, 0xFB, 0xC4,
+ 0xF3, 0xC3, 0x36, 0x91, 0x07, 0xB6, 0xDB, 0xE9,
+ 0x56, 0x3C, 0x51, 0x7D, 0xB5, 0xEC, 0x0A, 0xA9,
+ 0x7C, 0x66, 0xF9, 0xD8, 0x25, 0xDE, 0xD2, 0x94,
+ 0x5A, 0x58, 0xF1, 0x93, 0xE4, 0xF0, 0x5F, 0x27,
+ 0xBD, 0x83, 0xC7, 0xCA, 0x48, 0x6A, 0xB2, 0x55
+};
+static const unsigned char RSA_DP[] = {
+ 0xAF, 0x97, 0xBE, 0x60, 0x0F, 0xCE, 0x83, 0x36,
+ 0x51, 0x2D, 0xD9, 0x2E, 0x22, 0x41, 0x39, 0xC6,
+ 0x5C, 0x94, 0xA4, 0xCF, 0x28, 0xBD, 0xFA, 0x9C,
+ 0x3B, 0xD6, 0xE9, 0xDE, 0x56, 0xE3, 0x24, 0x3F,
+ 0xE1, 0x31, 0x14, 0xCA, 0xBA, 0x55, 0x1B, 0xAF,
+ 0x71, 0x6D, 0xDD, 0x35, 0x0C, 0x1C, 0x1F, 0xA7,
+ 0x2C, 0x3E, 0xDB, 0xAF, 0xA6, 0xD8, 0x2A, 0x7F,
+ 0x01, 0xE2, 0xE8, 0xB4, 0xF5, 0xFA, 0xDB, 0x61
+};
+static const unsigned char RSA_DQ[] = {
+ 0x29, 0xC0, 0x4B, 0x98, 0xFD, 0x13, 0xD3, 0x70,
+ 0x99, 0xAE, 0x1D, 0x24, 0x83, 0x5A, 0x3A, 0xFB,
+ 0x1F, 0xE3, 0x5F, 0xB6, 0x7D, 0xC9, 0x5C, 0x86,
+ 0xD3, 0xB4, 0xC8, 0x86, 0xE9, 0xE8, 0x30, 0xC3,
+ 0xA4, 0x4D, 0x6C, 0xAD, 0xA4, 0xB5, 0x75, 0x72,
+ 0x96, 0xC1, 0x94, 0xE9, 0xC4, 0xD1, 0xAA, 0x04,
+ 0x7C, 0x33, 0x1B, 0x20, 0xEB, 0xD3, 0x7C, 0x66,
+ 0x72, 0xF4, 0x53, 0x8A, 0x0A, 0xB2, 0xF9, 0xCD
+};
+static const unsigned char RSA_IQ[] = {
+ 0xE8, 0xEB, 0x04, 0x79, 0xA5, 0xC1, 0x79, 0xDE,
+ 0xD5, 0x49, 0xA1, 0x0B, 0x48, 0xB9, 0x0E, 0x55,
+ 0x74, 0x2C, 0x54, 0xEE, 0xA8, 0xB0, 0x01, 0xC2,
+ 0xD2, 0x3C, 0x3E, 0x47, 0x3A, 0x7C, 0xC8, 0x3D,
+ 0x2E, 0x33, 0x54, 0x4D, 0x40, 0x29, 0x41, 0x74,
+ 0xBA, 0xE1, 0x93, 0x09, 0xEC, 0xE0, 0x1B, 0x4D,
+ 0x1F, 0x2A, 0xCA, 0x4A, 0x0B, 0x5F, 0xE6, 0xBE,
+ 0x59, 0x0A, 0xC4, 0xC9, 0xD9, 0x82, 0xAC, 0xE1
+};
+
+static const br_rsa_public_key RSA_PK = {
+ (void *)RSA_N, sizeof RSA_N,
+ (void *)RSA_E, sizeof RSA_E
+};
+
+static const br_rsa_private_key RSA_SK = {
+ 1024,
+ (void *)RSA_P, sizeof RSA_P,
+ (void *)RSA_Q, sizeof RSA_Q,
+ (void *)RSA_DP, sizeof RSA_DP,
+ (void *)RSA_DQ, sizeof RSA_DQ,
+ (void *)RSA_IQ, sizeof RSA_IQ
+};
+
+/*
+ * A 2048-bit RSA key, generated with OpenSSL.
+ */
+static const unsigned char RSA2048_N[] = {
+ 0xEA, 0xB1, 0xB0, 0x87, 0x60, 0xE2, 0x69, 0xF5,
+ 0xC9, 0x3F, 0xCB, 0x4F, 0x9E, 0x7D, 0xD0, 0x56,
+ 0x54, 0x8F, 0xF5, 0x59, 0x97, 0x04, 0x3F, 0x30,
+ 0xE1, 0xFB, 0x7B, 0xF5, 0xA0, 0xEB, 0xA7, 0x7B,
+ 0x29, 0x96, 0x7B, 0x32, 0x48, 0x48, 0xA4, 0x99,
+ 0x90, 0x92, 0x48, 0xFB, 0xDC, 0xEC, 0x8A, 0x3B,
+ 0xE0, 0x57, 0x6E, 0xED, 0x1C, 0x5B, 0x78, 0xCF,
+ 0x07, 0x41, 0x96, 0x4C, 0x2F, 0xA2, 0xD1, 0xC8,
+ 0xA0, 0x5F, 0xFC, 0x2A, 0x5B, 0x3F, 0xBC, 0xD7,
+ 0xE6, 0x91, 0xF1, 0x44, 0xD6, 0xD8, 0x41, 0x66,
+ 0x3E, 0x80, 0xEE, 0x98, 0x73, 0xD5, 0x32, 0x60,
+ 0x7F, 0xDF, 0xBF, 0xB2, 0x0B, 0xA5, 0xCA, 0x11,
+ 0x88, 0x1A, 0x0E, 0xA1, 0x61, 0x4C, 0x5A, 0x70,
+ 0xCE, 0x12, 0xC0, 0x61, 0xF5, 0x50, 0x0E, 0xF6,
+ 0xC1, 0xC2, 0x88, 0x8B, 0xE5, 0xCE, 0xAE, 0x90,
+ 0x65, 0x23, 0xA7, 0xAD, 0xCB, 0x04, 0x17, 0x00,
+ 0xA2, 0xDB, 0xB0, 0x21, 0x49, 0xDD, 0x3C, 0x2E,
+ 0x8C, 0x47, 0x27, 0xF2, 0x84, 0x51, 0x63, 0xEB,
+ 0xF8, 0xAF, 0x63, 0xA7, 0x89, 0xE1, 0xF0, 0x2F,
+ 0xF9, 0x9C, 0x0A, 0x8A, 0xBC, 0x57, 0x05, 0xB0,
+ 0xEF, 0xA0, 0xDA, 0x67, 0x70, 0xAF, 0x3F, 0xA4,
+ 0x92, 0xFC, 0x4A, 0xAC, 0xEF, 0x89, 0x41, 0x58,
+ 0x57, 0x63, 0x0F, 0x6A, 0x89, 0x68, 0x45, 0x4C,
+ 0x20, 0xF9, 0x7F, 0x50, 0x9D, 0x8C, 0x52, 0xC4,
+ 0xC1, 0x33, 0xCD, 0x42, 0x35, 0x12, 0xEC, 0x82,
+ 0xF9, 0xC1, 0xB7, 0x60, 0x7B, 0x52, 0x61, 0xD0,
+ 0xAE, 0xFD, 0x4B, 0x68, 0xB1, 0x55, 0x0E, 0xAB,
+ 0x99, 0x24, 0x52, 0x60, 0x8E, 0xDB, 0x90, 0x34,
+ 0x61, 0xE3, 0x95, 0x7C, 0x34, 0x64, 0x06, 0xCB,
+ 0x44, 0x17, 0x70, 0x78, 0xC1, 0x1B, 0x87, 0x8F,
+ 0xCF, 0xB0, 0x7D, 0x93, 0x59, 0x84, 0x49, 0xF5,
+ 0x55, 0xBB, 0x48, 0xCA, 0xD3, 0x76, 0x1E, 0x7F
+};
+static const unsigned char RSA2048_E[] = {
+ 0x01, 0x00, 0x01
+};
+static const unsigned char RSA2048_P[] = {
+ 0xF9, 0xA7, 0xB5, 0xC4, 0xE8, 0x52, 0xEC, 0xB1,
+ 0x33, 0x6A, 0x68, 0x32, 0x63, 0x2D, 0xBA, 0xE5,
+ 0x61, 0x14, 0x69, 0x82, 0xC8, 0x31, 0x14, 0xD5,
+ 0xC2, 0x6C, 0x1A, 0xBE, 0xA0, 0x68, 0xA6, 0xC5,
+ 0xEA, 0x40, 0x59, 0xFB, 0x0A, 0x30, 0x3D, 0xD5,
+ 0xDD, 0x94, 0xAE, 0x0C, 0x9F, 0xEE, 0x19, 0x0C,
+ 0xA8, 0xF2, 0x85, 0x27, 0x60, 0xAA, 0xD5, 0x7C,
+ 0x59, 0x91, 0x1F, 0xAF, 0x5E, 0x00, 0xC8, 0x2D,
+ 0xCA, 0xB4, 0x70, 0xA1, 0xF8, 0x8C, 0x0A, 0xB3,
+ 0x08, 0x95, 0x03, 0x9E, 0xA4, 0x6B, 0x9D, 0x55,
+ 0x47, 0xE0, 0xEC, 0xB3, 0x21, 0x7C, 0xE4, 0x16,
+ 0x91, 0xE3, 0xD7, 0x1B, 0x3D, 0x81, 0xF1, 0xED,
+ 0x16, 0xF9, 0x05, 0x0E, 0xA6, 0x9F, 0x37, 0x73,
+ 0x18, 0x1B, 0x9C, 0x9D, 0x33, 0xAD, 0x25, 0xEF,
+ 0x3A, 0xC0, 0x4B, 0x34, 0x24, 0xF5, 0xFD, 0x59,
+ 0xF5, 0x65, 0xE6, 0x92, 0x2A, 0x04, 0x06, 0x3D
+};
+static const unsigned char RSA2048_Q[] = {
+ 0xF0, 0xA8, 0xA4, 0x20, 0xDD, 0xF3, 0x99, 0xE6,
+ 0x1C, 0xB1, 0x21, 0xE8, 0x66, 0x68, 0x48, 0x00,
+ 0x04, 0xE3, 0x21, 0xA3, 0xE8, 0xC5, 0xFD, 0x85,
+ 0x6D, 0x2C, 0x98, 0xE3, 0x36, 0x39, 0x3E, 0x80,
+ 0xB7, 0x36, 0xA5, 0xA9, 0xBB, 0xEB, 0x1E, 0xB8,
+ 0xEB, 0x44, 0x65, 0xE8, 0x81, 0x7D, 0xE0, 0x87,
+ 0xC1, 0x08, 0x94, 0xDD, 0x92, 0x40, 0xF4, 0x8B,
+ 0x3C, 0xB5, 0xC1, 0xAD, 0x9D, 0x4C, 0x14, 0xCD,
+ 0xD9, 0x2D, 0xB6, 0xE4, 0x99, 0xB3, 0x71, 0x63,
+ 0x64, 0xE1, 0x31, 0x7E, 0x34, 0x95, 0x96, 0x52,
+ 0x85, 0x27, 0xBE, 0x40, 0x10, 0x0A, 0x9E, 0x01,
+ 0x1C, 0xBB, 0xB2, 0x5B, 0x40, 0x85, 0x65, 0x6E,
+ 0xA0, 0x88, 0x73, 0xF6, 0x22, 0xCC, 0x23, 0x26,
+ 0x62, 0xAD, 0x92, 0x57, 0x57, 0xF4, 0xD4, 0xDF,
+ 0xD9, 0x7C, 0xDE, 0xAD, 0xD2, 0x1F, 0x32, 0x29,
+ 0xBA, 0xE7, 0xE2, 0x32, 0xA1, 0xA0, 0xBF, 0x6B
+};
+static const unsigned char RSA2048_DP[] = {
+ 0xB2, 0xF9, 0xD7, 0x66, 0xC5, 0x83, 0x05, 0x6A,
+ 0x77, 0xC8, 0xB5, 0xD0, 0x41, 0xA7, 0xBC, 0x0F,
+ 0xCB, 0x4B, 0xFD, 0xE4, 0x23, 0x2E, 0x84, 0x98,
+ 0x46, 0x1C, 0x88, 0x03, 0xD7, 0x2D, 0x8F, 0x39,
+ 0xDD, 0x98, 0xAA, 0xA9, 0x3D, 0x01, 0x9E, 0xA2,
+ 0xDE, 0x8A, 0x43, 0x48, 0x8B, 0xB2, 0xFE, 0xC4,
+ 0x43, 0xAE, 0x31, 0x65, 0x2C, 0x78, 0xEC, 0x39,
+ 0x8C, 0x60, 0x6C, 0xCD, 0xA4, 0xDF, 0x7C, 0xA2,
+ 0xCF, 0x6A, 0x12, 0x41, 0x1B, 0xD5, 0x11, 0xAA,
+ 0x8D, 0xE1, 0x7E, 0x49, 0xD1, 0xE7, 0xD0, 0x50,
+ 0x1E, 0x0A, 0x92, 0xC6, 0x4C, 0xA0, 0xA3, 0x47,
+ 0xC6, 0xE9, 0x07, 0x01, 0xE1, 0x53, 0x72, 0x23,
+ 0x9D, 0x4F, 0x82, 0x9F, 0xA1, 0x36, 0x0D, 0x63,
+ 0x76, 0x89, 0xFC, 0xF9, 0xF9, 0xDD, 0x0C, 0x8F,
+ 0xF7, 0x97, 0x79, 0x92, 0x75, 0x58, 0xE0, 0x7B,
+ 0x08, 0x61, 0x38, 0x2D, 0xDA, 0xEF, 0x2D, 0xA5
+};
+static const unsigned char RSA2048_DQ[] = {
+ 0x8B, 0x69, 0x56, 0x33, 0x08, 0x00, 0x8F, 0x3D,
+ 0xC3, 0x8F, 0x45, 0x52, 0x48, 0xC8, 0xCE, 0x34,
+ 0xDC, 0x9F, 0xEB, 0x23, 0xF5, 0xBB, 0x84, 0x62,
+ 0xDF, 0xDC, 0xBE, 0xF0, 0x98, 0xBF, 0xCE, 0x9A,
+ 0x68, 0x08, 0x4B, 0x2D, 0xA9, 0x83, 0xC9, 0xF7,
+ 0x5B, 0xAA, 0xF2, 0xD2, 0x1E, 0xF9, 0x99, 0xB1,
+ 0x6A, 0xBC, 0x9A, 0xE8, 0x44, 0x4A, 0x46, 0x9F,
+ 0xC6, 0x5A, 0x90, 0x49, 0x0F, 0xDF, 0x3C, 0x0A,
+ 0x07, 0x6E, 0xB9, 0x0D, 0x72, 0x90, 0x85, 0xF6,
+ 0x0B, 0x41, 0x7D, 0x17, 0x5C, 0x44, 0xEF, 0xA0,
+ 0xFC, 0x2C, 0x0A, 0xC5, 0x37, 0xC5, 0xBE, 0xC4,
+ 0x6C, 0x2D, 0xBB, 0x63, 0xAB, 0x5B, 0xDB, 0x67,
+ 0x9B, 0xAD, 0x90, 0x67, 0x9C, 0xBE, 0xDE, 0xF9,
+ 0xE4, 0x9E, 0x22, 0x31, 0x60, 0xED, 0x9E, 0xC7,
+ 0xD2, 0x48, 0xC9, 0x02, 0xAE, 0xBF, 0x8D, 0xA2,
+ 0xA8, 0xF8, 0x9D, 0x8B, 0xB1, 0x1F, 0xDA, 0xE3
+};
+static const unsigned char RSA2048_IQ[] = {
+ 0xB5, 0x48, 0xD4, 0x48, 0x5A, 0x33, 0xCD, 0x13,
+ 0xFE, 0xC6, 0xF7, 0x01, 0x0A, 0x3E, 0x40, 0xA3,
+ 0x45, 0x94, 0x6F, 0x85, 0xE4, 0x68, 0x66, 0xEC,
+ 0x69, 0x6A, 0x3E, 0xE0, 0x62, 0x3F, 0x0C, 0xEF,
+ 0x21, 0xCC, 0xDA, 0xAD, 0x75, 0x98, 0x12, 0xCA,
+ 0x9E, 0x31, 0xDD, 0x95, 0x0D, 0xBD, 0x55, 0xEB,
+ 0x92, 0xF7, 0x9E, 0xBD, 0xFC, 0x28, 0x35, 0x96,
+ 0x31, 0xDC, 0x53, 0x80, 0xA3, 0x57, 0x89, 0x3C,
+ 0x4A, 0xEC, 0x40, 0x75, 0x13, 0xAC, 0x4F, 0x36,
+ 0x3A, 0x86, 0x9A, 0xA6, 0x58, 0xC9, 0xED, 0xCB,
+ 0xD6, 0xBB, 0xB2, 0xD9, 0xAA, 0x04, 0xC4, 0xE8,
+ 0x47, 0x3E, 0xBD, 0x14, 0x9B, 0x8F, 0x61, 0x70,
+ 0x69, 0x66, 0x23, 0x62, 0x18, 0xE3, 0x52, 0x98,
+ 0xE3, 0x22, 0xE9, 0x6F, 0xDA, 0x28, 0x68, 0x08,
+ 0xB8, 0xB9, 0x8B, 0x97, 0x8B, 0x77, 0x3F, 0xCA,
+ 0x9D, 0x9D, 0xBE, 0xD5, 0x2D, 0x3E, 0xC2, 0x11
+};
+
+static const br_rsa_public_key RSA2048_PK = {
+ (void *)RSA2048_N, sizeof RSA2048_N,
+ (void *)RSA2048_E, sizeof RSA2048_E
+};
+
+static const br_rsa_private_key RSA2048_SK = {
+ 2048,
+ (void *)RSA2048_P, sizeof RSA2048_P,
+ (void *)RSA2048_Q, sizeof RSA2048_Q,
+ (void *)RSA2048_DP, sizeof RSA2048_DP,
+ (void *)RSA2048_DQ, sizeof RSA2048_DQ,
+ (void *)RSA2048_IQ, sizeof RSA2048_IQ
+};
+
+/*
+ * A 4096-bit RSA key, generated with OpenSSL.
+ */
+static const unsigned char RSA4096_N[] = {
+ 0xAA, 0x17, 0x71, 0xBC, 0x92, 0x3E, 0xB5, 0xBD,
+ 0x3E, 0x64, 0xCF, 0x03, 0x9B, 0x24, 0x65, 0x33,
+ 0x5F, 0xB4, 0x47, 0x89, 0xE5, 0x63, 0xE4, 0xA0,
+ 0x5A, 0x51, 0x95, 0x07, 0x73, 0xEE, 0x00, 0xF6,
+ 0x3E, 0x31, 0x0E, 0xDA, 0x15, 0xC3, 0xAA, 0x21,
+ 0x6A, 0xCD, 0xFF, 0x46, 0x6B, 0xDF, 0x0A, 0x7F,
+ 0x8A, 0xC2, 0x25, 0x19, 0x47, 0x44, 0xD8, 0x52,
+ 0xC1, 0x56, 0x25, 0x6A, 0xE0, 0xD2, 0x61, 0x11,
+ 0x2C, 0xF7, 0x73, 0x9F, 0x5F, 0x74, 0xAA, 0xDD,
+ 0xDE, 0xAF, 0x81, 0xF6, 0x0C, 0x1A, 0x3A, 0xF9,
+ 0xC5, 0x47, 0x82, 0x75, 0x1D, 0x41, 0xF0, 0xB2,
+ 0xFD, 0xBA, 0xE2, 0xA4, 0xA1, 0xB8, 0x32, 0x48,
+ 0x06, 0x0D, 0x29, 0x2F, 0x44, 0x14, 0xF5, 0xAC,
+ 0x54, 0x83, 0xC4, 0xB6, 0x85, 0x85, 0x9B, 0x1C,
+ 0x05, 0x61, 0x28, 0x62, 0x24, 0xA8, 0xF0, 0xE6,
+ 0x80, 0xA7, 0x91, 0xE8, 0xC7, 0x8E, 0x52, 0x17,
+ 0xBE, 0xAF, 0xC6, 0x0A, 0xA3, 0xFB, 0xD1, 0x04,
+ 0x15, 0x3B, 0x14, 0x35, 0xA5, 0x41, 0xF5, 0x30,
+ 0xFE, 0xEF, 0x53, 0xA7, 0x89, 0x91, 0x78, 0x30,
+ 0xBE, 0x3A, 0xB1, 0x4B, 0x2E, 0x4A, 0x0E, 0x25,
+ 0x1D, 0xCF, 0x51, 0x54, 0x52, 0xF1, 0x88, 0x85,
+ 0x36, 0x23, 0xDE, 0xBA, 0x66, 0x25, 0x60, 0x8D,
+ 0x45, 0xD7, 0xD8, 0x10, 0x41, 0x64, 0xC7, 0x4B,
+ 0xCE, 0x72, 0x13, 0xD7, 0x20, 0xF8, 0x2A, 0x74,
+ 0xA5, 0x05, 0xF4, 0x5A, 0x90, 0xF4, 0x9C, 0xE7,
+ 0xC9, 0xCF, 0x1E, 0xD5, 0x9C, 0xAC, 0xE5, 0x00,
+ 0x83, 0x73, 0x9F, 0xE7, 0xC6, 0x93, 0xC0, 0x06,
+ 0xA7, 0xB8, 0xF8, 0x46, 0x90, 0xC8, 0x78, 0x27,
+ 0x2E, 0xCC, 0xC0, 0x2A, 0x20, 0xC5, 0xFC, 0x63,
+ 0x22, 0xA1, 0xD6, 0x16, 0xAD, 0x9C, 0xD6, 0xFC,
+ 0x7A, 0x6E, 0x9C, 0x98, 0x51, 0xEE, 0x6B, 0x6D,
+ 0x8F, 0xEF, 0xCE, 0x7C, 0x5D, 0x16, 0xB0, 0xCE,
+ 0x9C, 0xEE, 0x92, 0xCF, 0xB7, 0xEB, 0x41, 0x36,
+ 0x3A, 0x6C, 0xF2, 0x0D, 0x26, 0x11, 0x2F, 0x6C,
+ 0x27, 0x62, 0xA2, 0xCC, 0x63, 0x53, 0xBD, 0xFC,
+ 0x9F, 0xBE, 0x9B, 0xBD, 0xE5, 0xA7, 0xDA, 0xD4,
+ 0xF8, 0xED, 0x5E, 0x59, 0x2D, 0xAC, 0xCD, 0x13,
+ 0xEB, 0xE5, 0x9E, 0x39, 0x82, 0x8B, 0xFD, 0xA8,
+ 0xFB, 0xCB, 0x86, 0x27, 0xC7, 0x4B, 0x4C, 0xD0,
+ 0xBA, 0x12, 0xD0, 0x76, 0x1A, 0xDB, 0x30, 0xC5,
+ 0xB3, 0x2C, 0x4C, 0xC5, 0x32, 0x03, 0x05, 0x67,
+ 0x8D, 0xD0, 0x14, 0x37, 0x59, 0x2B, 0xE3, 0x1C,
+ 0x25, 0x3E, 0xA5, 0xE4, 0xF1, 0x0D, 0x34, 0xBB,
+ 0xD5, 0xF6, 0x76, 0x45, 0x5B, 0x0F, 0x1E, 0x07,
+ 0x0A, 0xBA, 0x9D, 0x71, 0x87, 0xDE, 0x45, 0x50,
+ 0xE5, 0x0F, 0x32, 0xBB, 0x5C, 0x32, 0x2D, 0x40,
+ 0xCD, 0x19, 0x95, 0x4E, 0xC5, 0x54, 0x3A, 0x9A,
+ 0x46, 0x9B, 0x85, 0xFE, 0x53, 0xB7, 0xD8, 0x65,
+ 0x6D, 0x68, 0x0C, 0xBB, 0xE3, 0x3D, 0x8E, 0x64,
+ 0xBE, 0x27, 0x15, 0xAB, 0x12, 0x20, 0xD9, 0x84,
+ 0xF5, 0x02, 0xE4, 0xBB, 0xDD, 0xAB, 0x59, 0x51,
+ 0xF4, 0xE1, 0x79, 0xBE, 0xB8, 0xA3, 0x8E, 0xD1,
+ 0x1C, 0xB0, 0xFA, 0x48, 0x76, 0xC2, 0x9D, 0x7A,
+ 0x01, 0xA5, 0xAF, 0x8C, 0xBA, 0xAA, 0x4C, 0x06,
+ 0x2B, 0x0A, 0x62, 0xF0, 0x79, 0x5B, 0x42, 0xFC,
+ 0xF8, 0xBF, 0xD4, 0xDD, 0x62, 0x32, 0xE3, 0xCE,
+ 0xF1, 0x2C, 0xE6, 0xED, 0xA8, 0x8A, 0x41, 0xA3,
+ 0xC1, 0x1E, 0x07, 0xB6, 0x43, 0x10, 0x80, 0xB7,
+ 0xF3, 0xD0, 0x53, 0x2A, 0x9A, 0x98, 0xA7, 0x4F,
+ 0x9E, 0xA3, 0x3E, 0x1B, 0xDA, 0x93, 0x15, 0xF2,
+ 0xF4, 0x20, 0xA5, 0xA8, 0x4F, 0x8A, 0xBA, 0xED,
+ 0xB1, 0x17, 0x6C, 0x0F, 0xD9, 0x8F, 0x38, 0x11,
+ 0xF3, 0xD9, 0x5E, 0x88, 0xA1, 0xA1, 0x82, 0x8B,
+ 0x30, 0xD7, 0xC6, 0xCE, 0x4E, 0x30, 0x55, 0x57
+};
+static const unsigned char RSA4096_E[] = {
+ 0x01, 0x00, 0x01
+};
+static const unsigned char RSA4096_P[] = {
+ 0xD3, 0x7A, 0x22, 0xD8, 0x9B, 0xBF, 0x42, 0xB4,
+ 0x53, 0x04, 0x10, 0x6A, 0x84, 0xFD, 0x7C, 0x1D,
+ 0xF6, 0xF4, 0x10, 0x65, 0xAA, 0xE5, 0xE1, 0x4E,
+ 0xB4, 0x37, 0xF7, 0xAC, 0xF7, 0xD3, 0xB2, 0x3B,
+ 0xFE, 0xE7, 0x63, 0x42, 0xE9, 0xF0, 0x3C, 0xE0,
+ 0x42, 0xB4, 0xBB, 0x09, 0xD0, 0xB2, 0x7C, 0x70,
+ 0xA4, 0x11, 0x97, 0x90, 0x01, 0xD0, 0x0E, 0x7B,
+ 0xAF, 0x7D, 0x30, 0x4E, 0x6B, 0x3A, 0xCC, 0x50,
+ 0x4E, 0xAF, 0x2F, 0xC3, 0xC2, 0x4F, 0x7E, 0xC5,
+ 0xB3, 0x76, 0x33, 0xFB, 0xA7, 0xB1, 0x96, 0xA5,
+ 0x46, 0x41, 0xC6, 0xDA, 0x5A, 0xFD, 0x17, 0x0A,
+ 0x6A, 0x86, 0x54, 0x83, 0xE1, 0x57, 0xE7, 0xAF,
+ 0x8C, 0x42, 0xE5, 0x39, 0xF2, 0xC7, 0xFC, 0x4A,
+ 0x3D, 0x3C, 0x94, 0x89, 0xC2, 0xC6, 0x2D, 0x0A,
+ 0x5F, 0xD0, 0x21, 0x23, 0x5C, 0xC9, 0xC8, 0x44,
+ 0x8A, 0x96, 0x72, 0x4D, 0x96, 0xC6, 0x17, 0x0C,
+ 0x36, 0x43, 0x7F, 0xD8, 0xA0, 0x7A, 0x31, 0x7E,
+ 0xCE, 0x13, 0xE3, 0x13, 0x2E, 0xE0, 0x91, 0xC2,
+ 0x61, 0x13, 0x16, 0x8D, 0x99, 0xCB, 0xA9, 0x2C,
+ 0x4D, 0x9D, 0xDD, 0x1D, 0x03, 0xE7, 0xA7, 0x50,
+ 0xF4, 0x16, 0x43, 0xB1, 0x7F, 0x99, 0x61, 0x3F,
+ 0xA5, 0x59, 0x91, 0x16, 0xC3, 0x06, 0x63, 0x59,
+ 0xE9, 0xDA, 0xB5, 0x06, 0x2E, 0x0C, 0xD9, 0xAB,
+ 0x93, 0x89, 0x12, 0x82, 0xFB, 0x90, 0xD9, 0x30,
+ 0x60, 0xF7, 0x35, 0x2D, 0x18, 0x78, 0xEB, 0x2B,
+ 0xA1, 0x06, 0x67, 0x37, 0xDE, 0x72, 0x20, 0xD2,
+ 0x80, 0xE5, 0x2C, 0xD7, 0x5E, 0xC7, 0x67, 0x2D,
+ 0x40, 0xE7, 0x7A, 0xCF, 0x4A, 0x69, 0x9D, 0xA7,
+ 0x90, 0x9F, 0x3B, 0xDF, 0x07, 0x97, 0x64, 0x69,
+ 0x06, 0x4F, 0xBA, 0xF4, 0xE5, 0xBD, 0x71, 0x60,
+ 0x36, 0xB7, 0xA3, 0xDE, 0x76, 0xC5, 0x38, 0xD7,
+ 0x1D, 0x9A, 0xFC, 0x36, 0x3D, 0x3B, 0xDC, 0xCF
+};
+static const unsigned char RSA4096_Q[] = {
+ 0xCD, 0xE6, 0xC6, 0xA6, 0x42, 0x4C, 0x45, 0x65,
+ 0x8B, 0x85, 0x76, 0xFC, 0x21, 0xB6, 0x57, 0x79,
+ 0x3C, 0xE4, 0xE3, 0x85, 0x55, 0x2F, 0x59, 0xD3,
+ 0x3F, 0x74, 0xAF, 0x9F, 0x11, 0x04, 0x10, 0x8B,
+ 0xF9, 0x5F, 0x4D, 0x25, 0xEE, 0x20, 0xF9, 0x69,
+ 0x3B, 0x02, 0xB6, 0x43, 0x0D, 0x0C, 0xED, 0x30,
+ 0x31, 0x57, 0xE7, 0x9A, 0x57, 0x24, 0x6B, 0x4A,
+ 0x5E, 0xA2, 0xBF, 0xD4, 0x47, 0x7D, 0xFA, 0x78,
+ 0x51, 0x86, 0x80, 0x68, 0x85, 0x7C, 0x7B, 0x08,
+ 0x4A, 0x35, 0x24, 0x4F, 0x8B, 0x24, 0x49, 0xF8,
+ 0x16, 0x06, 0x9C, 0x57, 0x4E, 0x94, 0x4C, 0xBD,
+ 0x6E, 0x53, 0x52, 0xC9, 0xC1, 0x64, 0x43, 0x22,
+ 0x1E, 0xDD, 0xEB, 0xAC, 0x90, 0x58, 0xCA, 0xBA,
+ 0x9C, 0xAC, 0xCF, 0xDD, 0x08, 0x6D, 0xB7, 0x31,
+ 0xDB, 0x0D, 0x83, 0xE6, 0x50, 0xA6, 0x69, 0xB1,
+ 0x1C, 0x68, 0x92, 0xB4, 0xB5, 0x76, 0xDE, 0xBD,
+ 0x4F, 0xA5, 0x30, 0xED, 0x23, 0xFF, 0xE5, 0x80,
+ 0x21, 0xAB, 0xED, 0xE6, 0xDC, 0x32, 0x3D, 0xF7,
+ 0x45, 0xB8, 0x19, 0x3D, 0x8E, 0x15, 0x7C, 0xE5,
+ 0x0D, 0xC8, 0x9B, 0x7D, 0x1F, 0x7C, 0x14, 0x14,
+ 0x41, 0x09, 0xA7, 0xEB, 0xFB, 0xD9, 0x5F, 0x9A,
+ 0x94, 0xB6, 0xD5, 0xA0, 0x2C, 0xAF, 0xB5, 0xEF,
+ 0x5C, 0x5A, 0x8E, 0x34, 0xA1, 0x8F, 0xEB, 0x38,
+ 0x0F, 0x31, 0x6E, 0x45, 0x21, 0x7A, 0xAA, 0xAF,
+ 0x6C, 0xB1, 0x8E, 0xB2, 0xB9, 0xD4, 0x1E, 0xEF,
+ 0x66, 0xD8, 0x4E, 0x3D, 0xF2, 0x0C, 0xF1, 0xBA,
+ 0xFB, 0xA9, 0x27, 0xD2, 0x45, 0x54, 0x83, 0x4B,
+ 0x10, 0xC4, 0x9A, 0x32, 0x9C, 0xC7, 0x9A, 0xCF,
+ 0x4E, 0xBF, 0x07, 0xFC, 0x27, 0xB7, 0x96, 0x1D,
+ 0xDE, 0x9D, 0xE4, 0x84, 0x68, 0x00, 0x9A, 0x9F,
+ 0x3D, 0xE6, 0xC7, 0x26, 0x11, 0x48, 0x79, 0xFA,
+ 0x09, 0x76, 0xC8, 0x25, 0x3A, 0xE4, 0x70, 0xF9
+};
+static const unsigned char RSA4096_DP[] = {
+ 0x5C, 0xE3, 0x3E, 0xBF, 0x09, 0xD9, 0xFE, 0x80,
+ 0x9A, 0x1E, 0x24, 0xDF, 0xC4, 0xBE, 0x5A, 0x70,
+ 0x06, 0xF2, 0xB8, 0xE9, 0x0F, 0x21, 0x9D, 0xCF,
+ 0x26, 0x15, 0x97, 0x32, 0x60, 0x40, 0x99, 0xFF,
+ 0x04, 0x3D, 0xBA, 0x39, 0xBF, 0xEB, 0x87, 0xB1,
+ 0xB1, 0x5B, 0x14, 0xF4, 0x80, 0xB8, 0x85, 0x34,
+ 0x2C, 0xBC, 0x95, 0x67, 0xE9, 0x83, 0xEB, 0x78,
+ 0xA4, 0x62, 0x46, 0x7F, 0x8B, 0x55, 0xEE, 0x3C,
+ 0x2F, 0xF3, 0x7E, 0xF5, 0x6B, 0x39, 0xE3, 0xA3,
+ 0x0E, 0xEA, 0x92, 0x76, 0xAC, 0xF7, 0xB2, 0x05,
+ 0xB2, 0x50, 0x5D, 0xF9, 0xB7, 0x11, 0x87, 0xB7,
+ 0x49, 0x86, 0xEB, 0x44, 0x6A, 0x0C, 0x64, 0x75,
+ 0x95, 0x14, 0x24, 0xFF, 0x49, 0x06, 0x52, 0x68,
+ 0x81, 0x71, 0x44, 0x85, 0x26, 0x0A, 0x49, 0xEA,
+ 0x4E, 0x9F, 0x6A, 0x8E, 0xCF, 0xC8, 0xC9, 0xB0,
+ 0x61, 0x77, 0x27, 0x89, 0xB0, 0xFA, 0x1D, 0x51,
+ 0x7D, 0xDC, 0x34, 0x21, 0x80, 0x8B, 0x6B, 0x86,
+ 0x19, 0x1A, 0x5F, 0x19, 0x23, 0xF3, 0xFB, 0xD1,
+ 0xF7, 0x35, 0x9D, 0x28, 0x61, 0x2F, 0x35, 0x85,
+ 0x82, 0x2A, 0x1E, 0xDF, 0x09, 0xC2, 0x0C, 0x99,
+ 0xE0, 0x3C, 0x8F, 0x4B, 0x3D, 0x92, 0xAF, 0x46,
+ 0x77, 0x68, 0x59, 0xF4, 0x37, 0x81, 0x6C, 0xCE,
+ 0x27, 0x8B, 0xAB, 0x0B, 0xA5, 0xDA, 0x7B, 0x19,
+ 0x83, 0xDA, 0x27, 0x49, 0x65, 0x1A, 0x00, 0x6B,
+ 0xE1, 0x8B, 0x73, 0xCD, 0xF4, 0xFB, 0xD7, 0xBF,
+ 0xF8, 0x20, 0x89, 0xE1, 0xDE, 0x51, 0x1E, 0xDD,
+ 0x97, 0x44, 0x12, 0x68, 0x1E, 0xF7, 0x52, 0xF8,
+ 0x6B, 0x93, 0xC1, 0x3B, 0x9F, 0xA1, 0xB8, 0x5F,
+ 0xCB, 0x84, 0x45, 0x95, 0xF7, 0x0D, 0xA6, 0x4B,
+ 0x03, 0x3C, 0xAE, 0x0F, 0xB7, 0x81, 0x78, 0x75,
+ 0x1C, 0x53, 0x99, 0x24, 0xB3, 0xE2, 0x78, 0xCE,
+ 0xF3, 0xF0, 0x09, 0x6C, 0x01, 0x85, 0x73, 0xBD
+};
+static const unsigned char RSA4096_DQ[] = {
+ 0xCD, 0x88, 0xAC, 0x8B, 0x92, 0x6A, 0xA8, 0x6B,
+ 0x71, 0x16, 0xCD, 0x6B, 0x6A, 0x0B, 0xA6, 0xCD,
+ 0xF3, 0x27, 0x58, 0xA6, 0xE4, 0x1D, 0xDC, 0x40,
+ 0xAF, 0x7B, 0x3F, 0x44, 0x3D, 0xAC, 0x1D, 0x08,
+ 0x5C, 0xE9, 0xF1, 0x0D, 0x07, 0xE4, 0x0A, 0x94,
+ 0x2C, 0xBF, 0xCC, 0x48, 0xAA, 0x62, 0x58, 0xF2,
+ 0x5E, 0x8F, 0x2D, 0x36, 0x37, 0xFE, 0xB6, 0xCB,
+ 0x0A, 0x24, 0xD3, 0xF0, 0x87, 0x5D, 0x0E, 0x05,
+ 0xC4, 0xFB, 0xCA, 0x7A, 0x8B, 0xA5, 0x72, 0xFB,
+ 0x17, 0x78, 0x6C, 0xC2, 0xAA, 0x56, 0x93, 0x2F,
+ 0xFE, 0x6C, 0xA2, 0xEB, 0xD4, 0x18, 0xDD, 0x71,
+ 0xCB, 0x0B, 0x89, 0xFC, 0xB3, 0xFB, 0xED, 0xB7,
+ 0xC5, 0xB0, 0x29, 0x6D, 0x9C, 0xB9, 0xC5, 0xC4,
+ 0xFA, 0x58, 0xD7, 0x36, 0x01, 0x0F, 0xE4, 0x6A,
+ 0xF4, 0x0B, 0x4D, 0xBB, 0x3E, 0x8E, 0x9F, 0xBA,
+ 0x98, 0x6D, 0x1A, 0xE5, 0x20, 0xAF, 0x84, 0x30,
+ 0xDD, 0xAC, 0x3C, 0x66, 0xBC, 0x24, 0xD9, 0x67,
+ 0x4A, 0x35, 0x61, 0xC9, 0xAD, 0xCC, 0xC9, 0x66,
+ 0x68, 0x46, 0x19, 0x8C, 0x04, 0xA5, 0x16, 0x83,
+ 0x5F, 0x7A, 0xFD, 0x1B, 0xAD, 0xAE, 0x22, 0x2D,
+ 0x05, 0xAF, 0x29, 0xDC, 0xBB, 0x0E, 0x86, 0x0C,
+ 0xBC, 0x9E, 0xB6, 0x28, 0xA9, 0xF2, 0xCC, 0x5E,
+ 0x1F, 0x86, 0x95, 0xA5, 0x9C, 0x11, 0x19, 0xF0,
+ 0x5F, 0xDA, 0x2C, 0x04, 0xFE, 0x22, 0x80, 0xF7,
+ 0x94, 0x3C, 0xBA, 0x01, 0x56, 0xD6, 0x93, 0xFA,
+ 0xCE, 0x62, 0xE5, 0xD7, 0x98, 0x23, 0xAB, 0xB9,
+ 0xC7, 0x35, 0x57, 0xF6, 0xE2, 0x16, 0x36, 0xE9,
+ 0x5B, 0xD7, 0xA5, 0x45, 0x18, 0x93, 0x77, 0xC9,
+ 0xB1, 0x05, 0xA8, 0x66, 0xE1, 0x0E, 0xB5, 0xDF,
+ 0x23, 0x35, 0xE1, 0xC2, 0xFA, 0x3E, 0x80, 0x1A,
+ 0xAD, 0xA4, 0x0C, 0xEF, 0xC7, 0x18, 0xDE, 0x09,
+ 0xE6, 0x20, 0x98, 0x31, 0xF1, 0xD3, 0xCF, 0xA1
+};
+static const unsigned char RSA4096_IQ[] = {
+ 0x76, 0xD7, 0x75, 0xDF, 0xA3, 0x0C, 0x9D, 0x64,
+ 0x6E, 0x00, 0x82, 0x2E, 0x5C, 0x5E, 0x43, 0xC4,
+ 0xD2, 0x28, 0xB0, 0xB1, 0xA8, 0xD8, 0x26, 0x91,
+ 0xA0, 0xF5, 0xC8, 0x69, 0xFF, 0x24, 0x33, 0xAB,
+ 0x67, 0xC7, 0xA3, 0xAE, 0xBB, 0x17, 0x27, 0x5B,
+ 0x5A, 0xCD, 0x67, 0xA3, 0x70, 0x91, 0x9E, 0xD5,
+ 0xF1, 0x97, 0x00, 0x0A, 0x30, 0x64, 0x3D, 0x9B,
+ 0xBF, 0xB5, 0x8C, 0xAC, 0xC7, 0x20, 0x0A, 0xD2,
+ 0x76, 0x36, 0x36, 0x5D, 0xE4, 0xAC, 0x5D, 0xBC,
+ 0x44, 0x32, 0xB0, 0x76, 0x33, 0x40, 0xDD, 0x29,
+ 0x22, 0xE0, 0xFF, 0x55, 0x4C, 0xCE, 0x3F, 0x43,
+ 0x34, 0x95, 0x94, 0x7C, 0x22, 0x0D, 0xAB, 0x20,
+ 0x38, 0x70, 0xC3, 0x4A, 0x19, 0xCF, 0x81, 0xCE,
+ 0x79, 0x28, 0x6C, 0xC2, 0xA3, 0xB3, 0x48, 0x20,
+ 0x2D, 0x3E, 0x74, 0x45, 0x2C, 0xAA, 0x9F, 0xA5,
+ 0xC2, 0xE3, 0x2D, 0x41, 0x95, 0xBD, 0x78, 0xAB,
+ 0x6A, 0xA8, 0x7A, 0x45, 0x52, 0xE2, 0x66, 0xE7,
+ 0x6C, 0x38, 0x03, 0xA5, 0xDA, 0xAD, 0x94, 0x3C,
+ 0x6A, 0xA1, 0xA2, 0xD5, 0xCD, 0xDE, 0x05, 0xCC,
+ 0x6E, 0x3D, 0x8A, 0xF6, 0x9A, 0xA5, 0x0F, 0xA9,
+ 0x18, 0xC4, 0xF9, 0x9C, 0x2F, 0xB3, 0xF1, 0x30,
+ 0x38, 0x60, 0x69, 0x09, 0x67, 0x2C, 0xE9, 0x42,
+ 0x68, 0x3C, 0x70, 0x32, 0x1A, 0x44, 0x32, 0x02,
+ 0x82, 0x9F, 0x60, 0xE8, 0xA4, 0x42, 0x74, 0xA2,
+ 0xA2, 0x5A, 0x99, 0xDC, 0xC8, 0xCA, 0x15, 0x4D,
+ 0xFF, 0xF1, 0x8A, 0x23, 0xD8, 0xD3, 0xB1, 0x9A,
+ 0xB4, 0x0B, 0xBB, 0xE8, 0x38, 0x74, 0x0C, 0x52,
+ 0xC7, 0x8B, 0x63, 0x4C, 0xEA, 0x7D, 0x5F, 0x58,
+ 0x34, 0x53, 0x3E, 0x23, 0x10, 0xBB, 0x60, 0x6B,
+ 0x52, 0x9D, 0x89, 0x9F, 0xF0, 0x5F, 0xCE, 0xB3,
+ 0x9C, 0x0E, 0x75, 0x0F, 0x87, 0xF6, 0x66, 0xA5,
+ 0x4C, 0x94, 0x84, 0xFE, 0x94, 0xB9, 0x04, 0xB7
+};
+
+static const br_rsa_public_key RSA4096_PK = {
+ (void *)RSA4096_N, sizeof RSA4096_N,
+ (void *)RSA4096_E, sizeof RSA4096_E
+};
+
+static const br_rsa_private_key RSA4096_SK = {
+ 4096,
+ (void *)RSA4096_P, sizeof RSA4096_P,
+ (void *)RSA4096_Q, sizeof RSA4096_Q,
+ (void *)RSA4096_DP, sizeof RSA4096_DP,
+ (void *)RSA4096_DQ, sizeof RSA4096_DQ,
+ (void *)RSA4096_IQ, sizeof RSA4096_IQ
+};
+
+static void
+test_RSA_core(const char *name, br_rsa_public fpub, br_rsa_private fpriv)
+{
+ unsigned char t1[512], t2[512], t3[512];
+ size_t len;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ /*
+ * A KAT test (computed with OpenSSL).
+ */
+ len = hextobin(t1, "45A3DC6A106BCD3BD0E48FB579643AA3FF801E5903E80AA9B43A695A8E7F454E93FA208B69995FF7A6D5617C2FEB8E546375A664977A48931842AAE796B5A0D64393DCA35F3490FC157F5BD83B9D58C2F7926E6AE648A2BD96CAB8FCCD3D35BB11424AD47D973FF6D69CA774841AEC45DFAE99CCF79893E7047FDE6CB00AA76D");
+ hextobin(t2, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003021300906052B0E03021A05000414A94A8FE5CCB19BA61C4C0873D391E987982FBBD3");
+ memcpy(t3, t1, len);
+ if (!fpub(t3, len, &RSA_PK)) {
+ fprintf(stderr, "RSA public operation failed (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA pub", t2, t3, len);
+ if (!fpriv(t3, &RSA_SK)) {
+ fprintf(stderr, "RSA private operation failed (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA priv (1)", t1, t3, len);
+
+ /*
+ * Another KAT test, with a (fake) hash value slightly different
+ * (last byte is 0xD9 instead of 0xD3).
+ */
+ len = hextobin(t1, "32C2DB8B2C73BBCA9960CB3F11FEDEE7B699359EF2EEC3A632E56B7FF3DE2F371E5179BAB03F17E0BB20D2891ACAB679F95DA9B43A01DAAD192FADD25D8ACCF1498EC80F5BBCAC88EA59D60E3BC9D3CE27743981DE42385FFFFF04DD2D716E1A46C04A28ECAF6CD200DAB81083A830D61538D69BB39A183107BD50302AA6BC28");
+ hextobin(t2, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003021300906052B0E03021A05000414A94A8FE5CCB19BA61C4C0873D391E987982FBBD9");
+ memcpy(t3, t1, len);
+ if (!fpub(t3, len, &RSA_PK)) {
+ fprintf(stderr, "RSA public operation failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA pub", t2, t3, len);
+ if (!fpriv(t3, &RSA_SK)) {
+ fprintf(stderr, "RSA private operation failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA priv (2)", t1, t3, len);
+
+ /*
+ * Third KAT vector is invalid, because the encrypted value is
+ * out of range: instead of x, value is x+n (where n is the
+ * modulus). Mathematically, this still works, but implementations
+ * are supposed to reject such cases.
+ */
+ len = hextobin(t1, "F27781B9B3B358583A24F9BA6B34EE98B67A5AE8D8D4FA567BA773EB6B85EF88848680640A1E2F5FD117876E5FB928B64C6EFC7E03632A3F4C941E15657C0C705F3BB8D0B03A0249143674DB1FE6E5406D690BF2DA76EA7FF3AC6FCE12C7801252FAD52D332BE4AB41F9F8CF1728CDF98AB8E8C20E0C350E4F707A6402C01E0B");
+ hextobin(t2, "BFB6A62E873F9C8DA0C42E7B59360FB0FFE12549E5E636B048C2086B77A7C051663506A959DF177F15F6B4E544EE723C531152C9C9614F923364704307F13F7F15ACF0C1547D55C029DC9ECCE41D117245F4D270FC34B21FF3AD6AEFE58633281540902F547F79F3461F44D33CCB2D094231ADCC76BE25511B4513BB70491DBC");
+ memcpy(t3, t1, len);
+ if (fpub(t3, len, &RSA_PK)) {
+ size_t u;
+ fprintf(stderr, "RSA public operation should have failed"
+ " (value out of range)\n");
+ fprintf(stderr, "x = ");
+ for (u = 0; u < len; u ++) {
+ fprintf(stderr, "%02X", t3[u]);
+ }
+ fprintf(stderr, "\n");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(t3, t2, len);
+ if (fpriv(t3, &RSA_SK)) {
+ size_t u;
+ fprintf(stderr, "RSA private operation should have failed"
+ " (value out of range)\n");
+ fprintf(stderr, "x = ");
+ for (u = 0; u < len; u ++) {
+ fprintf(stderr, "%02X", t3[u]);
+ }
+ fprintf(stderr, "\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * RSA-2048 test vector.
+ */
+ len = hextobin(t1, "B188ED4EF173A30AED3889926E3CF1CE03FE3BAA7AB122B119A8CD529062F235A7B321008FB898894A624B3E6C8C5374950E78FAC86651345FE2ABA0791968284F23B0D794F8DCDDA924518854822CB7FF2AA9F205AACD909BB5EA541534CC00DBC2EF7727B9FE1BAFE6241B931E8BD01E13632E5AF9E94F4A335772B61F24D6F6AA642AEABB173E36F546CB02B19A1E5D4E27E3EB67F2E986E9F084D4BD266543800B1DC96088A05DFA9AFA595398E9A766D41DD8DA4F74F36C9D74867F0BF7BFA8622EE43C79DA0CEAC14B5D39DE074BDB89D84145BC19D8B2D0EA74DBF2DC29E907BF7C7506A2603CD8BC25EFE955D0125EDB2685EF158B020C9FC539242A");
+ hextobin(t2, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003031300D060960864801650304020105000420A5A0A792A09438811584A68E240C6C89F1FB1C53C0C86E270B942635F4F6B24A");
+ memcpy(t3, t1, len);
+ if (!fpub(t3, len, &RSA2048_PK)) {
+ fprintf(stderr, "RSA public operation failed (2048)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA pub", t2, t3, len);
+ if (!fpriv(t3, &RSA2048_SK)) {
+ fprintf(stderr, "RSA private operation failed (2048)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA priv (2048)", t1, t3, len);
+
+ /*
+ * RSA-4096 test vector.
+ */
+ len = hextobin(t1, "7D35B6B4D85252D08A2658C0B04126CC617B0E56B2A782A5FA2722AD05BD49538111682C12DA2C5FA1B9C30FB1AB8DA2C6A49EB4226A4D32290CF091FBB22EC499C7B18192C230B29F957DAF551F1EAD1917BA9E03D757100BD1F96B829708A6188A3927436113BB21E175D436BBB7A90E20162203FFB8F675313DFB21EFDA3EA0C7CC9B605AE7FB47E2DD2A9C4D5F124D7DE1B690AF9ADFEDC6055E0F9D2C9A891FB2501F3055D6DA7E94D51672BA1E86AEB782E4B020F70E0DF5399262909FC5B4770B987F2826EF2099A15F3CD5A0D6FE82E0C85FBA2C53C77305F534A7B0C7EA0D5244E37F1C1318EEF7079995F0642E4AB80EB0ED60DB4955FB652ED372DAC787581054A827C37A25C7B4DE7AE7EF3D099D47D6682ADF02BCC4DE04DDF2920F7124CF5B4955705E4BDB97A0BF341B584797878B4D3795134A9469FB391E4E4988F0AA451027CBC2ED6121FC23B26BF593E3C51DEDD53B62E23050D5B41CA34204679916A87AF1B17873A0867924D0C303942ADA478B769487FCEF861D4B20DCEE6942CCB84184833CDB258167258631C796BC1977D001354E2EE168ABE3B45FC969EA7F22B8E133C57A10FBB25ED19694E89C399CF7723B3C0DF0CC9F57A8ED0959EFC392FB31B8ADAEA969E2DEE8282CB245E5677368F00CCE4BA52C07C16BE7F9889D57191D5B2FE552D72B3415C64C09EE622457766EC809344A1EFE");
+ hextobin(t2, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003031300D0609608648016503040201050004205B60DD5AD5B3C62E0DA25FD0D8CB26325E1CE32CC9ED234B288235BCCF6ED2C8");
+ memcpy(t3, t1, len);
+ if (!fpub(t3, len, &RSA4096_PK)) {
+ fprintf(stderr, "RSA public operation failed (4096)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA pub", t2, t3, len);
+ if (!fpriv(t3, &RSA4096_SK)) {
+ fprintf(stderr, "RSA private operation failed (4096)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA priv (4096)", t1, t3, len);
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static const unsigned char SHA1_OID[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static void
+test_RSA_sign(const char *name, br_rsa_private fpriv,
+ br_rsa_pkcs1_sign fsign, br_rsa_pkcs1_vrfy fvrfy)
+{
+ unsigned char t1[128], t2[128];
+ unsigned char hv[20], tmp[20];
+ unsigned char rsa_n[128], rsa_e[3], rsa_p[64], rsa_q[64];
+ unsigned char rsa_dp[64], rsa_dq[64], rsa_iq[64];
+ br_rsa_public_key rsa_pk;
+ br_rsa_private_key rsa_sk;
+ unsigned char hv2[64], tmp2[64], sig[128];
+ br_sha1_context hc;
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ /*
+ * Verify the KAT test (computed with OpenSSL).
+ */
+ hextobin(t1, "45A3DC6A106BCD3BD0E48FB579643AA3FF801E5903E80AA9B43A695A8E7F454E93FA208B69995FF7A6D5617C2FEB8E546375A664977A48931842AAE796B5A0D64393DCA35F3490FC157F5BD83B9D58C2F7926E6AE648A2BD96CAB8FCCD3D35BB11424AD47D973FF6D69CA774841AEC45DFAE99CCF79893E7047FDE6CB00AA76D");
+ br_sha1_init(&hc);
+ br_sha1_update(&hc, "test", 4);
+ br_sha1_out(&hc, hv);
+ if (!fvrfy(t1, sizeof t1, SHA1_OID, sizeof tmp, &RSA_PK, tmp)) {
+ fprintf(stderr, "Signature verification failed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Extracted hash value", hv, tmp, sizeof tmp);
+
+ /*
+ * Regenerate the signature. This should yield the same value as
+ * the KAT test, since PKCS#1 v1.5 signatures are deterministic
+ * (except the usual detail about hash function parameter
+ * encoding, but OpenSSL uses the same convention as BearSSL).
+ */
+ if (!fsign(SHA1_OID, hv, 20, &RSA_SK, t2)) {
+ fprintf(stderr, "Signature generation failed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Regenerated signature", t1, t2, sizeof t1);
+
+ /*
+ * Use the raw private core to generate fake signatures, where
+ * one byte of the padded hash value is altered. They should all be
+ * rejected.
+ */
+ hextobin(t2, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003021300906052B0E03021A05000414A94A8FE5CCB19BA61C4C0873D391E987982FBBD3");
+ for (u = 0; u < (sizeof t2) - 20; u ++) {
+ memcpy(t1, t2, sizeof t2);
+ t1[u] ^= 0x01;
+ if (!fpriv(t1, &RSA_SK)) {
+ fprintf(stderr, "RSA private key operation failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (fvrfy(t1, sizeof t1, SHA1_OID, sizeof tmp, &RSA_PK, tmp)) {
+ fprintf(stderr,
+ "Signature verification should have failed\n");
+ exit(EXIT_FAILURE);
+ }
+ printf(".");
+ fflush(stdout);
+ }
+
+ /*
+ * Another KAT test, which historically showed a bug.
+ */
+ rsa_pk.n = rsa_n;
+ rsa_pk.nlen = hextobin(rsa_n, "E65DAEF196D22C300B3DAE1CE5157EDF821BB6038E419D8D363A8B2DA84A1321042330E6F87A8BD8FE6BA1D2A17031955ED2315CC5FD2397197E238A5E0D2D0AFD25717E814EC4D2BBA887327A3C5B3A450FD8D547BDFCBB0F73B997CA13DD5E7572C4D5BAA764A349BAB2F868ACF4574AE2C7AEC94B77D2EE00A21B6CB175BB");
+ rsa_pk.e = rsa_e;
+ rsa_pk.elen = hextobin(rsa_e, "010001");
+
+ rsa_sk.n_bitlen = 1024;
+ rsa_sk.p = rsa_p;
+ rsa_sk.plen = hextobin(rsa_p, "FF58513DBA4F3F42DFDFD3E6AFB6BD62DE27E06BA3C9D9F9B542CB21228C2AAE67936514161C8FDC1A248A50195CAF22ADC50DA89BFED1B9EEFBB37304241357");
+ rsa_sk.q = rsa_q;
+ rsa_sk.qlen = hextobin(rsa_q, "E6F4F66818B7442297DDEB45E9B3D438E5B57BB5EF86EFF2462AD6B9C10F383517CDD2E7E36EAD4BEBCC57CFE8AA985F7E7B38B96D30FFBE9ED9FE21B1CFB63D");
+ rsa_sk.dp = rsa_dp;
+ rsa_sk.dplen = hextobin(rsa_dp, "6F89517B682D83919F9EF2BDBA955526A1A9C382E139A3A84AC01160B8E9871F458901C7035D988D6931FAE4C01F57350BB89E9DBEFE50F829E6F25CD43B39E3");
+ rsa_sk.dq = rsa_dq;
+ rsa_sk.dqlen = hextobin(rsa_dq, "409E08D2D7176F58BE64B88EB6F4394C31F8B4C412600E821A5FA1F416AFCB6A0F5EE6C33A3E9CFDC0DB4B3640427A9F3D23FC9AE491F0FBC435F98433DB8981");
+ rsa_sk.iq = rsa_iq;
+ rsa_sk.iqlen = hextobin(rsa_iq, "CF333D6AD66D02B4D11C8C23CA669D14D71803ADC3943BE03B1E48F52F385BCFDDFD0F85AD02A984E504FC6612549D4E7867B7D09DD13196BFC3FAA4B57393A9");
+ hextobin(sig, "CFB84D161E6DB130736FC6212EBE575571AF341CEF5757C19952A5364C90E3C47549E520E26253DAE70F645F31FA8B5DA9AE282741D3CA4B1CC365B7BD75D6D61D4CFD9AD9EDD17D23E0BA7D9775138DBABC7FF2A57587FE1EA1B51E8F3C68326E26FF89D8CF92BDD4C787D04857DFC3266E6B33B92AA08809929C72642F35C2");
+
+ hextobin(hv2, "F66C62B38E1CC69C378C0E16574AE5C6443FDFA3E85C6205C00B3231CAA3074EC1481BDC22AB575E6CF3CCD9EDA6B39F83923FC0E6475C799D257545F77233B4");
+ if (!fsign(BR_HASH_OID_SHA512, hv2, 64, &rsa_sk, t2)) {
+ fprintf(stderr, "Signature generation failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Regenerated signature (2)", t2, sig, sizeof t2);
+ if (!fvrfy(t2, sizeof t2, BR_HASH_OID_SHA512,
+ sizeof tmp2, &rsa_pk, tmp2))
+ {
+ fprintf(stderr, "Signature verification failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("Extracted hash value (2)", hv2, tmp2, sizeof tmp2);
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+/*
+ * Fake RNG that returns exactly the provided bytes.
+ */
+typedef struct {
+ const br_prng_class *vtable;
+ unsigned char buf[128];
+ size_t ptr, len;
+} rng_fake_ctx;
+
+static void rng_fake_init(rng_fake_ctx *cc,
+ const void *params, const void *seed, size_t len);
+static void rng_fake_generate(rng_fake_ctx *cc, void *dst, size_t len);
+static void rng_fake_update(rng_fake_ctx *cc, const void *src, size_t len);
+
+static const br_prng_class rng_fake_vtable = {
+ sizeof(rng_fake_ctx),
+ (void (*)(const br_prng_class **,
+ const void *, const void *, size_t))&rng_fake_init,
+ (void (*)(const br_prng_class **,
+ void *, size_t))&rng_fake_generate,
+ (void (*)(const br_prng_class **,
+ const void *, size_t))&rng_fake_update
+};
+
+static void
+rng_fake_init(rng_fake_ctx *cc, const void *params,
+ const void *seed, size_t len)
+{
+ (void)params;
+ if (len > sizeof cc->buf) {
+ fprintf(stderr, "seed is too large (%lu bytes)\n",
+ (unsigned long)len);
+ exit(EXIT_FAILURE);
+ }
+ cc->vtable = &rng_fake_vtable;
+ memcpy(cc->buf, seed, len);
+ cc->ptr = 0;
+ cc->len = len;
+}
+
+static void
+rng_fake_generate(rng_fake_ctx *cc, void *dst, size_t len)
+{
+ if (len > (cc->len - cc->ptr)) {
+ fprintf(stderr, "asking for more data than expected\n");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(dst, cc->buf + cc->ptr, len);
+ cc->ptr += len;
+}
+
+static void
+rng_fake_update(rng_fake_ctx *cc, const void *src, size_t len)
+{
+ (void)cc;
+ (void)src;
+ (void)len;
+ fprintf(stderr, "unexpected update\n");
+ exit(EXIT_FAILURE);
+}
+
+/*
+ * Test vectors from pkcs-1v2-1d2-vec.zip (originally from ftp.rsa.com).
+ * There are ten RSA keys, and for each RSA key, there are 6 messages,
+ * each with an explicit salt.
+ *
+ * Field order:
+ * modulus (n)
+ * public exponent (e)
+ * first factor (p)
+ * second factor (q)
+ * first private exponent (dp)
+ * second private exponent (dq)
+ * CRT coefficient (iq)
+ * message 1
+ * salt 1 (20-byte random value)
+ * signature 1
+ * message 2
+ * salt 2 (20-byte random value)
+ * signature 2
+ * ...
+ * message 6
+ * salt 6 (20-byte random value)
+ * signature 6
+ *
+ * This pattern is repeated for all keys. The array stops on a NULL.
+ */
+static const char *KAT_RSA_PSS[] = {
+
+ /* 1024-bit key */
+ "a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",
+ "010001",
+ "33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325",
+ "e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443",
+ "b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd",
+ "28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979",
+ "1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729",
+ "27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d",
+
+ "cdc87da223d786df3b45e0bbbc721326d1ee2af806cc315475cc6f0d9c66e1b62371d45ce2392e1ac92844c310102f156a0d8d52c1f4c40ba3aa65095786cb769757a6563ba958fed0bcc984e8b517a3d5f515b23b8a41e74aa867693f90dfb061a6e86dfaaee64472c00e5f20945729cbebe77f06ce78e08f4098fba41f9d6193c0317e8b60d4b6084acb42d29e3808a3bc372d85e331170fcbf7cc72d0b71c296648b3a4d10f416295d0807aa625cab2744fd9ea8fd223c42537029828bd16be02546f130fd2e33b936d2676e08aed1b73318b750a0167d0",
+ "dee959c7e06411361420ff80185ed57f3e6776af",
+ "9074308fb598e9701b2294388e52f971faac2b60a5145af185df5287b5ed2887e57ce7fd44dc8634e407c8e0e4360bc226f3ec227f9d9e54638e8d31f5051215df6ebb9c2f9579aa77598a38f914b5b9c1bd83c4e2f9f382a0d0aa3542ffee65984a601bc69eb28deb27dca12c82c2d4c3f66cd500f1ff2b994d8a4e30cbb33c",
+
+ "851384cdfe819c22ed6c4ccb30daeb5cf059bc8e1166b7e3530c4c233e2b5f8f71a1cca582d43ecc72b1bca16dfc7013226b9e",
+ "ef2869fa40c346cb183dab3d7bffc98fd56df42d",
+ "3ef7f46e831bf92b32274142a585ffcefbdca7b32ae90d10fb0f0c729984f04ef29a9df0780775ce43739b97838390db0a5505e63de927028d9d29b219ca2c4517832558a55d694a6d25b9dab66003c4cccd907802193be5170d26147d37b93590241be51c25055f47ef62752cfbe21418fafe98c22c4d4d47724fdb5669e843",
+
+ "a4b159941761c40c6a82f2b80d1b94f5aa2654fd17e12d588864679b54cd04ef8bd03012be8dc37f4b83af7963faff0dfa225477437c48017ff2be8191cf3955fc07356eab3f322f7f620e21d254e5db4324279fe067e0910e2e81ca2cab31c745e67a54058eb50d993cdb9ed0b4d029c06d21a94ca661c3ce27fae1d6cb20f4564d66ce4767583d0e5f060215b59017be85ea848939127bd8c9c4d47b51056c031cf336f17c9980f3b8f5b9b6878e8b797aa43b882684333e17893fe9caa6aa299f7ed1a18ee2c54864b7b2b99b72618fb02574d139ef50f019c9eef416971338e7d470",
+ "710b9c4747d800d4de87f12afdce6df18107cc77",
+ "666026fba71bd3e7cf13157cc2c51a8e4aa684af9778f91849f34335d141c00154c4197621f9624a675b5abc22ee7d5baaffaae1c9baca2cc373b3f33e78e6143c395a91aa7faca664eb733afd14d8827259d99a7550faca501ef2b04e33c23aa51f4b9e8282efdb728cc0ab09405a91607c6369961bc8270d2d4f39fce612b1",
+
+ "bc656747fa9eafb3f0",
+ "056f00985de14d8ef5cea9e82f8c27bef720335e",
+ "4609793b23e9d09362dc21bb47da0b4f3a7622649a47d464019b9aeafe53359c178c91cd58ba6bcb78be0346a7bc637f4b873d4bab38ee661f199634c547a1ad8442e03da015b136e543f7ab07c0c13e4225b8de8cce25d4f6eb8400f81f7e1833b7ee6e334d370964ca79fdb872b4d75223b5eeb08101591fb532d155a6de87",
+
+ "b45581547e5427770c768e8b82b75564e0ea4e9c32594d6bff706544de0a8776c7a80b4576550eee1b2acabc7e8b7d3ef7bb5b03e462c11047eadd00629ae575480ac1470fe046f13a2bf5af17921dc4b0aa8b02bee6334911651d7f8525d10f32b51d33be520d3ddf5a709955a3dfe78283b9e0ab54046d150c177f037fdccc5be4ea5f68b5e5a38c9d7edcccc4975f455a6909b4",
+ "80e70ff86a08de3ec60972b39b4fbfdcea67ae8e",
+ "1d2aad221ca4d31ddf13509239019398e3d14b32dc34dc5af4aeaea3c095af73479cf0a45e5629635a53a018377615b16cb9b13b3e09d671eb71e387b8545c5960da5a64776e768e82b2c93583bf104c3fdb23512b7b4e89f633dd0063a530db4524b01c3f384c09310e315a79dcd3d684022a7f31c865a664e316978b759fad",
+
+ "10aae9a0ab0b595d0841207b700d48d75faedde3b775cd6b4cc88ae06e4694ec74ba18f8520d4f5ea69cbbe7cc2beba43efdc10215ac4eb32dc302a1f53dc6c4352267e7936cfebf7c8d67035784a3909fa859c7b7b59b8e39c5c2349f1886b705a30267d402f7486ab4f58cad5d69adb17ab8cd0ce1caf5025af4ae24b1fb8794c6070cc09a51e2f9911311e3877d0044c71c57a993395008806b723ac38373d395481818528c1e7053739282053529510e935cd0fa77b8fa53cc2d474bd4fb3cc5c672d6ffdc90a00f9848712c4bcfe46c60573659b11e6457e861f0f604b6138d144f8ce4e2da73",
+ "a8ab69dd801f0074c2a1fc60649836c616d99681",
+ "2a34f6125e1f6b0bf971e84fbd41c632be8f2c2ace7de8b6926e31ff93e9af987fbc06e51e9be14f5198f91f3f953bd67da60a9df59764c3dc0fe08e1cbef0b75f868d10ad3fba749fef59fb6dac46a0d6e504369331586f58e4628f39aa278982543bc0eeb537dc61958019b394fb273f215858a0a01ac4d650b955c67f4c58",
+
+ /* 1025-bit key */
+ "01d40c1bcf97a68ae7cdbd8a7bf3e34fa19dcca4ef75a47454375f94514d88fed006fb829f8419ff87d6315da68a1ff3a0938e9abb3464011c303ad99199cf0c7c7a8b477dce829e8844f625b115e5e9c4a59cf8f8113b6834336a2fd2689b472cbb5e5cabe674350c59b6c17e176874fb42f8fc3d176a017edc61fd326c4b33c9",
+ "010001",
+ "027d147e4673057377fd1ea201565772176a7dc38358d376045685a2e787c23c15576bc16b9f444402d6bfc5d98a3e88ea13ef67c353eca0c0ddba9255bd7b8bb50a644afdfd1dd51695b252d22e7318d1b6687a1c10ff75545f3db0fe602d5f2b7f294e3601eab7b9d1cecd767f64692e3e536ca2846cb0c2dd486a39fa75b1",
+ "016601e926a0f8c9e26ecab769ea65a5e7c52cc9e080ef519457c644da6891c5a104d3ea7955929a22e7c68a7af9fcad777c3ccc2b9e3d3650bce404399b7e59d1",
+ "014eafa1d4d0184da7e31f877d1281ddda625664869e8379e67ad3b75eae74a580e9827abd6eb7a002cb5411f5266797768fb8e95ae40e3e8a01f35ff89e56c079",
+ "e247cce504939b8f0a36090de200938755e2444b29539a7da7a902f6056835c0db7b52559497cfe2c61a8086d0213c472c78851800b171f6401de2e9c2756f31",
+ "b12fba757855e586e46f64c38a70c68b3f548d93d787b399999d4c8f0bbd2581c21e19ed0018a6d5d3df86424b3abcad40199d31495b61309f27c1bf55d487c1",
+ "564b1e1fa003bda91e89090425aac05b91da9ee25061e7628d5f51304a84992fdc33762bd378a59f030a334d532bd0dae8f298ea9ed844636ad5fb8cbdc03cad",
+
+ "daba032066263faedb659848115278a52c44faa3a76f37515ed336321072c40a9d9b53bc05014078adf520875146aae70ff060226dcb7b1f1fc27e9360",
+ "57bf160bcb02bb1dc7280cf0458530b7d2832ff7",
+ "014c5ba5338328ccc6e7a90bf1c0ab3fd606ff4796d3c12e4b639ed9136a5fec6c16d8884bdd99cfdc521456b0742b736868cf90de099adb8d5ffd1deff39ba4007ab746cefdb22d7df0e225f54627dc65466131721b90af445363a8358b9f607642f78fab0ab0f43b7168d64bae70d8827848d8ef1e421c5754ddf42c2589b5b3",
+
+ "e4f8601a8a6da1be34447c0959c058570c3668cfd51dd5f9ccd6ad4411fe8213486d78a6c49f93efc2ca2288cebc2b9b60bd04b1e220d86e3d4848d709d032d1e8c6a070c6af9a499fcf95354b14ba6127c739de1bb0fd16431e46938aec0cf8ad9eb72e832a7035de9b7807bdc0ed8b68eb0f5ac2216be40ce920c0db0eddd3860ed788efaccaca502d8f2bd6d1a7c1f41ff46f1681c8f1f818e9c4f6d91a0c7803ccc63d76a6544d843e084e363b8acc55aa531733edb5dee5b5196e9f03e8b731b3776428d9e457fe3fbcb3db7274442d785890e9cb0854b6444dace791d7273de1889719338a77fe",
+ "7f6dd359e604e60870e898e47b19bf2e5a7b2a90",
+ "010991656cca182b7f29d2dbc007e7ae0fec158eb6759cb9c45c5ff87c7635dd46d150882f4de1e9ae65e7f7d9018f6836954a47c0a81a8a6b6f83f2944d6081b1aa7c759b254b2c34b691da67cc0226e20b2f18b42212761dcd4b908a62b371b5918c5742af4b537e296917674fb914194761621cc19a41f6fb953fbcbb649dea",
+
+ "52a1d96c8ac39e41e455809801b927a5b445c10d902a0dcd3850d22a66d2bb0703e67d5867114595aabf5a7aeb5a8f87034bbb30e13cfd4817a9be76230023606d0286a3faf8a4d22b728ec518079f9e64526e3a0cc7941aa338c437997c680ccac67c66bfa1",
+ "fca862068bce2246724b708a0519da17e648688c",
+ "007f0030018f53cdc71f23d03659fde54d4241f758a750b42f185f87578520c30742afd84359b6e6e8d3ed959dc6fe486bedc8e2cf001f63a7abe16256a1b84df0d249fc05d3194ce5f0912742dbbf80dd174f6c51f6bad7f16cf3364eba095a06267dc3793803ac7526aebe0a475d38b8c2247ab51c4898df7047dc6adf52c6c4",
+
+ "a7182c83ac18be6570a106aa9d5c4e3dbbd4afaeb0c60c4a23e1969d79ff",
+ "8070ef2de945c02387684ba0d33096732235d440",
+ "009cd2f4edbe23e12346ae8c76dd9ad3230a62076141f16c152ba18513a48ef6f010e0e37fd3df10a1ec629a0cb5a3b5d2893007298c30936a95903b6ba85555d9ec3673a06108fd62a2fda56d1ce2e85c4db6b24a81ca3b496c36d4fd06eb7c9166d8e94877c42bea622b3bfe9251fdc21d8d5371badad78a488214796335b40b",
+
+ "86a83d4a72ee932a4f5630af6579a386b78fe88999e0abd2d49034a4bfc854dd94f1094e2e8cd7a179d19588e4aefc1b1bd25e95e3dd461f",
+ "17639a4e88d722c4fca24d079a8b29c32433b0c9",
+ "00ec430824931ebd3baa43034dae98ba646b8c36013d1671c3cf1cf8260c374b19f8e1cc8d965012405e7e9bf7378612dfcc85fce12cda11f950bd0ba8876740436c1d2595a64a1b32efcfb74a21c873b3cc33aaf4e3dc3953de67f0674c0453b4fd9f604406d441b816098cb106fe3472bc251f815f59db2e4378a3addc181ecf",
+
+ "049f9154d871ac4a7c7ab45325ba7545a1ed08f70525b2667cf1",
+ "37810def1055ed922b063df798de5d0aabf886ee",
+ "00475b1648f814a8dc0abdc37b5527f543b666bb6e39d30e5b49d3b876dccc58eac14e32a2d55c2616014456ad2f246fc8e3d560da3ddf379a1c0bd200f10221df078c219a151bc8d4ec9d2fc2564467811014ef15d8ea01c2ebbff8c2c8efab38096e55fcbe3285c7aa558851254faffa92c1c72b78758663ef4582843139d7a6",
+
+ /* 1026-bit key */
+ "02f246ef451ed3eebb9a310200cc25859c048e4be798302991112eb68ce6db674e280da21feded1ae74880ca522b18db249385012827c515f0e466a1ffa691d98170574e9d0eadb087586ca48933da3cc953d95bd0ed50de10ddcb6736107d6c831c7f663e833ca4c097e700ce0fb945f88fb85fe8e5a773172565b914a471a443",
+ "010001",
+ "651451733b56de5ac0a689a4aeb6e6894a69014e076c88dd7a667eab3232bbccd2fc44ba2fa9c31db46f21edd1fdb23c5c128a5da5bab91e7f952b67759c7cff705415ac9fa0907c7ca6178f668fb948d869da4cc3b7356f4008dfd5449d32ee02d9a477eb69fc29266e5d9070512375a50fbbcc27e238ad98425f6ebbf88991",
+ "01bd36e18ece4b0fdb2e9c9d548bd1a7d6e2c21c6fdc35074a1d05b1c6c8b3d558ea2639c9a9a421680169317252558bd148ad215aac550e2dcf12a82d0ebfe853",
+ "01b1b656ad86d8e19d5dc86292b3a192fdf6e0dd37877bad14822fa00190cab265f90d3f02057b6f54d6ecb14491e5adeacebc48bf0ebd2a2ad26d402e54f61651",
+ "1f2779fd2e3e5e6bae05539518fba0cd0ead1aa4513a7cba18f1cf10e3f68195693d278a0f0ee72f89f9bc760d80e2f9d0261d516501c6ae39f14a476ce2ccf5",
+ "011a0d36794b04a854aab4b2462d439a5046c91d940b2bc6f75b62956fef35a2a6e63c5309817f307bbff9d59e7e331bd363f6d66849b18346adea169f0ae9aec1",
+ "0b30f0ecf558752fb3a6ce4ba2b8c675f659eba6c376585a1b39712d038ae3d2b46fcb418ae15d0905da6440e1513a30b9b7d6668fbc5e88e5ab7a175e73ba35",
+
+ "594b37333bbb2c84524a87c1a01f75fcec0e3256f108e38dca36d70d0057",
+ "f31ad6c8cf89df78ed77feacbcc2f8b0a8e4cfaa",
+ "0088b135fb1794b6b96c4a3e678197f8cac52b64b2fe907d6f27de761124964a99a01a882740ecfaed6c01a47464bb05182313c01338a8cd097214cd68ca103bd57d3bc9e816213e61d784f182467abf8a01cf253e99a156eaa8e3e1f90e3c6e4e3aa2d83ed0345b89fafc9c26077c14b6ac51454fa26e446e3a2f153b2b16797f",
+
+ "8b769528884a0d1ffd090cf102993e796dadcfbddd38e44ff6324ca451",
+ "fcf9f0e1f199a3d1d0da681c5b8606fc642939f7",
+ "02a5f0a858a0864a4f65017a7d69454f3f973a2999839b7bbc48bf78641169179556f595fa41f6ff18e286c2783079bc0910ee9cc34f49ba681124f923dfa88f426141a368a5f5a930c628c2c3c200e18a7644721a0cbec6dd3f6279bde3e8f2be5e2d4ee56f97e7ceaf33054be7042bd91a63bb09f897bd41e81197dee99b11af",
+
+ "1abdba489c5ada2f995ed16f19d5a94d9e6ec34a8d84f84557d26e5ef9b02b22887e3f9a4b690ad1149209c20c61431f0c017c36c2657b35d7b07d3f5ad8708507a9c1b831df835a56f831071814ea5d3d8d8f6ade40cba38b42db7a2d3d7a29c8f0a79a7838cf58a9757fa2fe4c40df9baa193bfc6f92b123ad57b07ace3e6ac068c9f106afd9eeb03b4f37c25dbfbcfb3071f6f9771766d072f3bb070af6605532973ae25051",
+ "986e7c43dbb671bd41b9a7f4b6afc80e805f2423",
+ "0244bcd1c8c16955736c803be401272e18cb990811b14f72db964124d5fa760649cbb57afb8755dbb62bf51f466cf23a0a1607576e983d778fceffa92df7548aea8ea4ecad2c29dd9f95bc07fe91ecf8bee255bfe8762fd7690aa9bfa4fa0849ef728c2c42c4532364522df2ab7f9f8a03b63f7a499175828668f5ef5a29e3802c",
+
+ "8fb431f5ee792b6c2ac7db53cc428655aeb32d03f4e889c5c25de683c461b53acf89f9f8d3aabdf6b9f0c2a1de12e15b49edb3919a652fe9491c25a7fce1f722c2543608b69dc375ec",
+ "f8312d9c8eea13ec0a4c7b98120c87509087c478",
+ "0196f12a005b98129c8df13c4cb16f8aa887d3c40d96df3a88e7532ef39cd992f273abc370bc1be6f097cfebbf0118fd9ef4b927155f3df22b904d90702d1f7ba7a52bed8b8942f412cd7bd676c9d18e170391dcd345c06a730964b3f30bcce0bb20ba106f9ab0eeb39cf8a6607f75c0347f0af79f16afa081d2c92d1ee6f836b8",
+
+ "fef4161dfaaf9c5295051dfc1ff3810c8c9ec2e866f7075422c8ec4216a9c4ff49427d483cae10c8534a41b2fd15fee06960ec6fb3f7a7e94a2f8a2e3e43dc4a40576c3097ac953b1de86f0b4ed36d644f23ae14425529622464ca0cbf0b1741347238157fab59e4de5524096d62baec63ac64",
+ "50327efec6292f98019fc67a2a6638563e9b6e2d",
+ "021eca3ab4892264ec22411a752d92221076d4e01c0e6f0dde9afd26ba5acf6d739ef987545d16683e5674c9e70f1de649d7e61d48d0caeb4fb4d8b24fba84a6e3108fee7d0705973266ac524b4ad280f7ae17dc59d96d3351586b5a3bdb895d1e1f7820ac6135d8753480998382ba32b7349559608c38745290a85ef4e9f9bd83",
+
+ "efd237bb098a443aeeb2bf6c3f8c81b8c01b7fcb3feb",
+ "b0de3fc25b65f5af96b1d5cc3b27d0c6053087b3",
+ "012fafec862f56e9e92f60ab0c77824f4299a0ca734ed26e0644d5d222c7f0bde03964f8e70a5cb65ed44e44d56ae0edf1ff86ca032cc5dd4404dbb76ab854586c44eed8336d08d457ce6c03693b45c0f1efef93624b95b8ec169c616d20e5538ebc0b6737a6f82b4bc0570924fc6b35759a3348426279f8b3d7744e2d222426ce",
+
+ /* 1027-bit key */
+ "054adb7886447efe6f57e0368f06cf52b0a3370760d161cef126b91be7f89c421b62a6ec1da3c311d75ed50e0ab5fff3fd338acc3aa8a4e77ee26369acb81ba900fa83f5300cf9bb6c53ad1dc8a178b815db4235a9a9da0c06de4e615ea1277ce559e9c108de58c14a81aa77f5a6f8d1335494498848c8b95940740be7bf7c3705",
+ "010001",
+ "fa041f8cd9697ceed38ec8caa275523b4dd72b09a301d3541d72f5d31c05cbce2d6983b36183af10690bd46c46131e35789431a556771dd0049b57461bf060c1f68472e8a67c25f357e5b6b4738fa541a730346b4a07649a2dfa806a69c975b6aba64678acc7f5913e89c622f2d8abb1e3e32554e39df94ba60c002e387d9011",
+ "029232336d2838945dba9dd7723f4e624a05f7375b927a87abe6a893a1658fd49f47f6c7b0fa596c65fa68a23f0ab432962d18d4343bd6fd671a5ea8d148413995",
+ "020ef5efe7c5394aed2272f7e81a74f4c02d145894cb1b3cab23a9a0710a2afc7e3329acbb743d01f680c4d02afb4c8fde7e20930811bb2b995788b5e872c20bb1",
+ "026e7e28010ecf2412d9523ad704647fb4fe9b66b1a681581b0e15553a89b1542828898f27243ebab45ff5e1acb9d4df1b051fbc62824dbc6f6c93261a78b9a759",
+ "012ddcc86ef655998c39ddae11718669e5e46cf1495b07e13b1014cd69b3af68304ad2a6b64321e78bf3bbca9bb494e91d451717e2d97564c6549465d0205cf421",
+ "010600c4c21847459fe576703e2ebecae8a5094ee63f536bf4ac68d3c13e5e4f12ac5cc10ab6a2d05a199214d1824747d551909636b774c22cac0b837599abcc75",
+
+ "9fb03b827c8217d9",
+ "ed7c98c95f30974fbe4fbddcf0f28d6021c0e91d",
+ "0323d5b7bf20ba4539289ae452ae4297080feff4518423ff4811a817837e7d82f1836cdfab54514ff0887bddeebf40bf99b047abc3ecfa6a37a3ef00f4a0c4a88aae0904b745c846c4107e8797723e8ac810d9e3d95dfa30ff4966f4d75d13768d20857f2b1406f264cfe75e27d7652f4b5ed3575f28a702f8c4ed9cf9b2d44948",
+
+ "0ca2ad77797ece86de5bf768750ddb5ed6a3116ad99bbd17edf7f782f0db1cd05b0f677468c5ea420dc116b10e80d110de2b0461ea14a38be68620392e7e893cb4ea9393fb886c20ff790642305bf302003892e54df9f667509dc53920df583f50a3dd61abb6fab75d600377e383e6aca6710eeea27156e06752c94ce25ae99fcbf8592dbe2d7e27453cb44de07100ebb1a2a19811a478adbeab270f94e8fe369d90b3ca612f9f",
+ "22d71d54363a4217aa55113f059b3384e3e57e44",
+ "049d0185845a264d28feb1e69edaec090609e8e46d93abb38371ce51f4aa65a599bdaaa81d24fba66a08a116cb644f3f1e653d95c89db8bbd5daac2709c8984000178410a7c6aa8667ddc38c741f710ec8665aa9052be929d4e3b16782c1662114c5414bb0353455c392fc28f3db59054b5f365c49e1d156f876ee10cb4fd70598",
+
+ "288062afc08fcdb7c5f8650b29837300461dd5676c17a20a3c8fb5148949e3f73d66b3ae82c7240e27c5b3ec4328ee7d6ddf6a6a0c9b5b15bcda196a9d0c76b119d534d85abd123962d583b76ce9d180bce1ca",
+ "4af870fbc6516012ca916c70ba862ac7e8243617",
+ "03fbc410a2ced59500fb99f9e2af2781ada74e13145624602782e2994813eefca0519ecd253b855fb626a90d771eae028b0c47a199cbd9f8e3269734af4163599090713a3fa910fa0960652721432b971036a7181a2bc0cab43b0b598bc6217461d7db305ff7e954c5b5bb231c39e791af6bcfa76b147b081321f72641482a2aad",
+
+ "6f4f9ab9501199cef55c6cf408fe7b36c557c49d420a4763d2463c8ad44b3cfc5be2742c0e7d9b0f6608f08c7f47b693ee",
+ "40d2e180fae1eac439c190b56c2c0e14ddf9a226",
+ "0486644bc66bf75d28335a6179b10851f43f09bded9fac1af33252bb9953ba4298cd6466b27539a70adaa3f89b3db3c74ab635d122f4ee7ce557a61e59b82ffb786630e5f9db53c77d9a0c12fab5958d4c2ce7daa807cd89ba2cc7fcd02ff470ca67b229fcce814c852c73cc93bea35be68459ce478e9d4655d121c8472f371d4f",
+
+ "e17d20385d501955823c3f666254c1d3dd36ad5168b8f18d286fdcf67a7dad94097085fab7ed86fe2142a28771717997ef1a7a08884efc39356d76077aaf82459a7fad45848875f2819b098937fe923bcc9dc442d72d754d812025090c9bc03db3080c138dd63b355d0b4b85d6688ac19f4de15084a0ba4e373b93ef4a555096691915dc23c00e954cdeb20a47cd55d16c3d8681d46ed7f2ed5ea42795be17baed25f0f4d113b3636addd585f16a8b5aec0c8fa9c5f03cbf3b9b73",
+ "2497dc2b4615dfae5a663d49ffd56bf7efc11304",
+ "022a80045353904cb30cbb542d7d4990421a6eec16a8029a8422adfd22d6aff8c4cc0294af110a0c067ec86a7d364134459bb1ae8ff836d5a8a2579840996b320b19f13a13fad378d931a65625dae2739f0c53670b35d9d3cbac08e733e4ec2b83af4b9196d63e7c4ff1ddeae2a122791a125bfea8deb0de8ccf1f4ffaf6e6fb0a",
+
+ "afbc19d479249018fdf4e09f618726440495de11ddeee38872d775fcea74a23896b5343c9c38d46af0dba224d047580cc60a65e9391cf9b59b36a860598d4e8216722f993b91cfae87bc255af89a6a199bca4a391eadbc3a24903c0bd667368f6be78e3feabfb4ffd463122763740ffbbefeab9a25564bc5d1c24c93e422f75073e2ad72bf45b10df00b52a147128e73fee33fa3f0577d77f80fbc2df1bed313290c12777f50",
+ "a334db6faebf11081a04f87c2d621cdec7930b9b",
+ "00938dcb6d583046065f69c78da7a1f1757066a7fa75125a9d2929f0b79a60b627b082f11f5b196f28eb9daa6f21c05e5140f6aef1737d2023075c05ecf04a028c686a2ab3e7d5a0664f295ce12995e890908b6ad21f0839eb65b70393a7b5afd9871de0caa0cedec5b819626756209d13ab1e7bb9546a26ff37e9a51af9fd562e",
+
+ /* 1028-bit key */
+ "0d10f661f29940f5ed39aa260966deb47843679d2b6fb25b3de370f3ac7c19916391fd25fb527ebfa6a4b4df45a1759d996c4bb4ebd18828c44fc52d0191871740525f47a4b0cc8da325ed8aa676b0d0f626e0a77f07692170acac8082f42faa7dc7cd123e730e31a87985204cabcbe6670d43a2dd2b2ddef5e05392fc213bc507",
+ "010001",
+ "03ce08b104fff396a979bd3e4e46925b6319ddb63acbcfd819f17d16b8077b3a87101ff34b77fe48b8b205a96e9151ba8ecea64d0cce7b23c3e6a6b83058bc49dae816ae736db5a4708e2ad435232b567f9096ce59ff28061e79ab1c02d717e6b23cea6db8eb5192fa7c1eab227dba74621c45601896eef13792c8440beb15aac1",
+ "03f2f331f4142d4f24b43aa10279a89652d4e7537221a1a7b2a25deb551e5de9ac497411c227a94e45f91c2d1c13cc046cf4ce14e32d058734210d44a87ee1b73f",
+ "034f090d73b55803030cf0361a5d8081bfb79f851523feac0a2124d08d4013ff08487771a870d0479dc0686c62f7718dfecf024b17c9267678059171339cc00839",
+ "02aa663adbf51ab887a018cb426e78bc2fe182dcb2f7bcb50441d17fdf0f06798b5071c6e2f5feb4d54ad8182311c1ef62d4c49f18d1f51f54b2d2cffba4da1be5",
+ "02bbe706078b5c0b391512d411db1b199b5a5664b84042ead37fe994ae72b9532dfbfb3e9e6981a0fbb806513141b7c2163fe56c395e4bfaee57e3833f9b918df9",
+ "0242b6cd00d30a767aee9a898ead453c8eaea63d500b7d1e00713edae51ce36b23b664df26e63e266ec8f76e6e63ed1ba41eb033b120f7ea5212ae21a98fbc16",
+
+ "30c7d557458b436decfdc14d06cb7b96b06718c48d7de57482a868ae7f065870a6216506d11b779323dfdf046cf5775129134b4d5689e4d9c0ce1e12d7d4b06cb5fc5820decfa41baf59bf257b32f025b7679b445b9499c92555145885992f1b76f84891ee4d3be0f5150fd5901e3a4c8ed43fd36b61d022e65ad5008dbf33293c22bfbfd07321f0f1d5fa9fdf0014c2fcb0358aad0e354b0d29",
+ "081b233b43567750bd6e78f396a88b9f6a445151",
+ "0ba373f76e0921b70a8fbfe622f0bf77b28a3db98e361051c3d7cb92ad0452915a4de9c01722f6823eeb6adf7e0ca8290f5de3e549890ac2a3c5950ab217ba58590894952de96f8df111b2575215da6c161590c745be612476ee578ed384ab33e3ece97481a252f5c79a98b5532ae00cdd62f2ecc0cd1baefe80d80b962193ec1d",
+
+ "e7b32e1556ea1b2795046ac69739d22ac8966bf11c116f614b166740e96b90653e5750945fcf772186c03790a07fda323e1a61916b06ee2157db3dff80d67d5e39a53ae268c8f09ed99a732005b0bc6a04af4e08d57a00e7201b3060efaadb73113bfc087fd837093aa25235b8c149f56215f031c24ad5bde7f29960df7d524070f7449c6f785084be1a0f733047f336f9154738674547db02a9f44dfc6e60301081e1ce99847f3b5b601ff06b4d5776a9740b9aa0d34058fd3b906e4f7859dfb07d7173e5e6f6350adac21f27b2307469",
+ "bd0ce19549d0700120cbe51077dbbbb00a8d8b09",
+ "08180de825e4b8b014a32da8ba761555921204f2f90d5f24b712908ff84f3e220ad17997c0dd6e706630ba3e84add4d5e7ab004e58074b549709565d43ad9e97b5a7a1a29e85b9f90f4aafcdf58321de8c5974ef9abf2d526f33c0f2f82e95d158ea6b81f1736db8d1af3d6ac6a83b32d18bae0ff1b2fe27de4c76ed8c7980a34e",
+
+ "8d8396e36507fe1ef6a19017548e0c716674c2fec233adb2f775665ec41f2bd0ba396b061a9daa7e866f7c23fd3531954300a342f924535ea1498c48f6c879932865fc02000c528723b7ad0335745b51209a0afed932af8f0887c219004d2abd894ea92559ee3198af3a734fe9b9638c263a728ad95a5ae8ce3eb15839f3aa7852bb390706e7760e43a71291a2e3f827237deda851874c517665f545f27238df86557f375d09ccd8bd15d8ccf61f5d78ca5c7f5cde782e6bf5d0057056d4bad98b3d2f9575e824ab7a33ff57b0ac100ab0d6ead7aa0b50f6e4d3e5ec0b966b",
+ "815779a91b3a8bd049bf2aeb920142772222c9ca",
+ "05e0fdbdf6f756ef733185ccfa8ced2eb6d029d9d56e35561b5db8e70257ee6fd019d2f0bbf669fe9b9821e78df6d41e31608d58280f318ee34f559941c8df13287574bac000b7e58dc4f414ba49fb127f9d0f8936638c76e85356c994f79750f7fa3cf4fd482df75e3fb9978cd061f7abb17572e6e63e0bde12cbdcf18c68b979",
+
+ "328c659e0a6437433cceb73c14",
+ "9aec4a7480d5bbc42920d7ca235db674989c9aac",
+ "0bc989853bc2ea86873271ce183a923ab65e8a53100e6df5d87a24c4194eb797813ee2a187c097dd872d591da60c568605dd7e742d5af4e33b11678ccb63903204a3d080b0902c89aba8868f009c0f1c0cb85810bbdd29121abb8471ff2d39e49fd92d56c655c8e037ad18fafbdc92c95863f7f61ea9efa28fea401369d19daea1",
+
+ "f37b962379a47d415a376eec8973150bcb34edd5ab654041b61430560c2144582ba133c867d852d6b8e23321901302ecb45b09ec88b1527178fa043263f3067d9ffe973032a99f4cb08ad2c7e0a2456cdd57a7df56fe6053527a5aeb67d7e552063c1ca97b1beffa7b39e997caf27878ea0f62cbebc8c21df4c889a202851e949088490c249b6e9acf1d8063f5be2343989bf95c4da01a2be78b4ab6b378015bc37957f76948b5e58e440c28453d40d7cfd57e7d690600474ab5e75973b1ea0c5f1e45d14190afe2f4eb6d3bdf71f1d2f8bb156a1c295d04aaeb9d689dce79ed62bc443e",
+ "e20c1e9878512c39970f58375e1549a68b64f31d",
+ "0aefa943b698b9609edf898ad22744ac28dc239497cea369cbbd84f65c95c0ad776b594740164b59a739c6ff7c2f07c7c077a86d95238fe51e1fcf33574a4ae0684b42a3f6bf677d91820ca89874467b2c23add77969c80717430d0efc1d3695892ce855cb7f7011630f4df26def8ddf36fc23905f57fa6243a485c770d5681fcd",
+
+ "c6103c330c1ef718c141e47b8fa859be4d5b96259e7d142070ecd485839dba5a8369c17c1114035e532d195c74f44a0476a2d3e8a4da210016caced0e367cb867710a4b5aa2df2b8e5daf5fdc647807d4d5ebb6c56b9763ccdae4dea3308eb0ac2a89501cb209d2639fa5bf87ce790747d3cb2d295e84564f2f637824f0c13028129b0aa4a422d162282",
+ "23291e4a3307e8bbb776623ab34e4a5f4cc8a8db",
+ "02802dccfa8dfaf5279bf0b4a29ba1b157611faeaaf419b8919d15941900c1339e7e92e6fae562c53e6cc8e84104b110bce03ad18525e3c49a0eadad5d3f28f244a8ed89edbafbb686277cfa8ae909714d6b28f4bf8e293aa04c41efe7c0a81266d5c061e2575be032aa464674ff71626219bd74cc45f0e7ed4e3ff96eee758e8f",
+
+ /* 1029-bit key */
+ "164ca31cff609f3a0e7101b039f2e4fe6dd37519ab98598d179e174996598071f47d3a04559158d7be373cf1aa53f0aa6ef09039e5678c2a4c63900514c8c4f8aaed5de12a5f10b09c311af8c0ffb5b7a297f2efc63b8d6b0510931f0b98e48bf5fc6ec4e7b8db1ffaeb08c38e02adb8f03a48229c99e969431f61cb8c4dc698d1",
+ "010001",
+ "03b664ee3b7566723fc6eaf28abb430a3980f1126c81de8ad709eab39ac9dcd0b1550b3729d87068e952009df544534c1f50829a78f4591eb8fd57140426a6bb0405b6a6f51a57d9267b7bbc653391a699a2a90dac8ae226bcc60fa8cd934c73c7b03b1f6b818158631838a8612e6e6ea92be24f8324faf5b1fd8587225267ba6f",
+ "04f0548c9626ab1ebf1244934741d99a06220efa2a5856aa0e75730b2ec96adc86be894fa2803b53a5e85d276acbd29ab823f80a7391bb54a5051672fb04eeb543",
+ "0483e0ae47915587743ff345362b555d3962d98bb6f15f848b4c92b1771ca8ed107d8d3ee65ec44517dd0faa481a387e902f7a2e747c269e7ea44480bc538b8e5b",
+ "03a8e8aea9920c1aa3b2f0d846e4b850d81ca306a51c83544f949f64f90dcf3f8e2661f07e561220a180388fbe273e70e2e5dca83a0e1348dd6490c731d6ece1ab",
+ "0135bdcdb60bf2197c436ed34b32cd8b4fc77778832ba76703551fb242b301699593af77fd8fc394a8526ad23cc41a03806bd897fe4b0ea646558aaddcc99e8a25",
+ "0304c03d9c736503a984abbd9ba22301407c4a2ab1dd85766481b60d45401152e692be14f4121d9aa3fd6e0b4d1d3a973538a31d42ee6e1e5ef620231a2bbaf35f",
+
+ "0a20b774addc2fa51245ed7cb9da609e50cac6636a52543f97458eed7340f8d53ffc64918f949078ee03ef60d42b5fec246050bd5505cd8cb597bad3c4e713b0ef30644e76adabb0de01a1561efb255158c74fc801e6e919e581b46f0f0ddd08e4f34c7810b5ed8318f91d7c8c",
+ "5b4ea2ef629cc22f3b538e016904b47b1e40bfd5",
+ "04c0cfacec04e5badbece159a5a1103f69b3f32ba593cb4cc4b1b7ab455916a96a27cd2678ea0f46ba37f7fc9c86325f29733b389f1d97f43e7201c0f348fc45fe42892335362eee018b5b161f2f9393031225c713012a576bc88e23052489868d9010cbf033ecc568e8bc152bdc59d560e41291915d28565208e22aeec9ef85d1",
+
+ "2aaff6631f621ce615760a9ebce94bb333077ad86488c861d4b76d29c1f48746c611ae1e03ced4445d7cfa1fe5f62e1b3f08452bde3b6ef81973bafbb57f97bceef873985395b8260589aa88cb7db50ab469262e551bdcd9a56f275a0ac4fe484700c35f3dbf2b469ede864741b86fa59172a360ba95a02e139be50ddfb7cf0b42faeabbfbbaa86a4497699c4f2dfd5b08406af7e14144427c253ec0efa20eaf9a8be8cd49ce1f1bc4e93e619cf2aa8ed4fb39bc8590d0f7b96488f7317ac9abf7bee4e3a0e715",
+ "83146a9e782722c28b014f98b4267bda2ac9504f",
+ "0a2314250cf52b6e4e908de5b35646bcaa24361da8160fb0f9257590ab3ace42b0dc3e77ad2db7c203a20bd952fbb56b1567046ecfaa933d7b1000c3de9ff05b7d989ba46fd43bc4c2d0a3986b7ffa13471d37eb5b47d64707bd290cfd6a9f393ad08ec1e3bd71bb5792615035cdaf2d8929aed3be098379377e777ce79aaa4773",
+
+ "0f6195d04a6e6fc7e2c9600dbf840c39ea8d4d624fd53507016b0e26858a5e0aecd7ada543ae5c0ab3a62599cba0a54e6bf446e262f989978f9ddf5e9a41",
+ "a87b8aed07d7b8e2daf14ddca4ac68c4d0aabff8",
+ "086df6b500098c120f24ff8423f727d9c61a5c9007d3b6a31ce7cf8f3cbec1a26bb20e2bd4a046793299e03e37a21b40194fb045f90b18bf20a47992ccd799cf9c059c299c0526854954aade8a6ad9d97ec91a1145383f42468b231f4d72f23706d9853c3fa43ce8ace8bfe7484987a1ec6a16c8daf81f7c8bf42774707a9df456",
+
+ "337d25fe9810ebca0de4d4658d3ceb8e0fe4c066aba3bcc48b105d3bf7e0257d44fecea6596f4d0c59a08402833678f70620f9138dfeb7ded905e4a6d5f05c473d55936652e2a5df43c0cfda7bacaf3087f4524b06cf42157d01539739f7fddec9d58125df31a32eab06c19b71f1d5bf",
+ "a37932f8a7494a942d6f767438e724d6d0c0ef18",
+ "0b5b11ad549863ffa9c51a14a1106c2a72cc8b646e5c7262509786105a984776534ca9b54c1cc64bf2d5a44fd7e8a69db699d5ea52087a4748fd2abc1afed1e5d6f7c89025530bdaa2213d7e030fa55df6f34bcf1ce46d2edf4e3ae4f3b01891a068c9e3a44bbc43133edad6ecb9f35400c4252a5762d65744b99cb9f4c559329f",
+
+ "84ec502b072e8287789d8f9235829ea3b187afd4d4c785611bda5f9eb3cb96717efa7007227f1c08cbcb972e667235e0fb7d431a6570326d2ecce35adb373dc753b3be5f829b89175493193fab16badb41371b3aac0ae670076f24bef420c135add7cee8d35fbc944d79fafb9e307a13b0f556cb654a06f973ed22672330197ef5a748bf826a5db2383a25364b686b9372bb2339aeb1ac9e9889327d016f1670776db06201adbdcaf8a5e3b74e108b73",
+ "7b790c1d62f7b84e94df6af28917cf571018110e",
+ "02d71fa9b53e4654fefb7f08385cf6b0ae3a817942ebf66c35ac67f0b069952a3ce9c7e1f1b02e480a9500836de5d64cdb7ecde04542f7a79988787e24c2ba05f5fd482c023ed5c30e04839dc44bed2a3a3a4fee01113c891a47d32eb8025c28cb050b5cdb576c70fe76ef523405c08417faf350b037a43c379339fcb18d3a356b",
+
+ "9906d89f97a9fdedd3ccd824db687326f30f00aa25a7fca2afcb3b0f86cd41e73f0e8ff7d2d83f59e28ed31a5a0d551523374de22e4c7e8ff568b386ee3dc41163f10bf67bb006261c9082f9af90bf1d9049a6b9fae71c7f84fbe6e55f02789de774f230f115026a4b4e96c55b04a95da3aacbb2cece8f81764a1f1c99515411087cf7d34aeded0932c183",
+ "fbbe059025b69b89fb14ae2289e7aaafe60c0fcd",
+ "0a40a16e2fe2b38d1df90546167cf9469c9e3c3681a3442b4b2c2f581deb385ce99fc6188bb02a841d56e76d301891e24560550fcc2a26b55f4ccb26d837d350a154bcaca8392d98fa67959e9727b78cad03269f56968fc56b68bd679926d83cc9cb215550645ccda31c760ff35888943d2d8a1d351e81e5d07b86182e751081ef",
+
+ /* 1030-bit key */
+ "37c9da4a66c8c408b8da27d0c9d79f8ccb1eafc1d2fe48746d940b7c4ef5dee18ad12647cefaa0c4b3188b221c515386759b93f02024b25ab9242f8357d8f3fd49640ee5e643eaf6c64deefa7089727c8ff03993333915c6ef21bf5975b6e50d118b51008ec33e9f01a0a545a10a836a43ddbca9d8b5c5d3548022d7064ea29ab3",
+ "010001",
+ "3bed999052d957bc06d651eef6e3a98094b1621bd38b5449bd6c4aea3de7e084679a4484ded25be0f0826cf3377825414b14d4d61db14de626fbb80e5f4faec956f9a0a2d24f99576380f084eb62e46a57d554278b535626193ce02060575eb66c5798d36f6c5d40fb00d809b42a73102c1c74ee95bd71420fffef6318b52c29",
+ "07eefb424b0e3a40e4208ee5afb280b22317308114dde0b4b64f730184ec68da6ce2867a9f48ed7726d5e2614ed04a5410736c8c714ee702474298c6292af07535",
+ "070830dbf947eac0228de26314b59b66994cc60e8360e75d3876298f8f8a7d141da064e5ca026a973e28f254738cee669c721b034cb5f8e244dadd7cd1e159d547",
+ "0524d20c3d95cff75af2313483227d8702717aa576de155f960515501adb1d70e1c04de91b75b161dbf0398356127ededa7bbc19a32dc1621cc9f53c265d0ce331",
+ "05f984a1f23c938d6a0e89724bcf3dd93f9946926037fe7c6b13a29e5284855f89089591d440975627bf5c9e3a8b5ca79c772ad273e40d321af4a6c97dfded78d3",
+ "ddd918adada29dcab981ff9acba4257023c09a3801ccce098ce268f855d0df570cd6e7b9b14bd9a5a9254cbc315be6f8ba1e2546ddd569c5ea19eed8353bde5e",
+
+ "9ead0e01945640674eb41cad435e2374eaefa8ad7197d97913c44957d8d83f40d76ee60e39bf9c0f9eaf3021421a074d1ade962c6e9d3dc3bb174fe4dfe652b09115495b8fd2794174020a0602b5ca51848cfc96ce5eb57fc0a2adc1dda36a7cc452641a14911b37e45bfa11daa5c7ecdb74f6d0100d1d3e39e752800e203397de0233077b9a88855537fae927f924380d780f98e18dcff39c5ea741b17d6fdd1885bc9d581482d771ceb562d78a8bf88f0c75b11363e5e36cd479ceb0545f9da84203e0e6e508375cc9e844b88b7ac7a0a201ea0f1bee9a2c577920ca02c01b9d8320e974a56f4efb5763b96255abbf8037bf1802cf018f56379493e569a9",
+ "b7867a59958cb54328f8775e6546ec06d27eaa50",
+ "187f390723c8902591f0154bae6d4ecbffe067f0e8b795476ea4f4d51ccc810520bb3ca9bca7d0b1f2ea8a17d873fa27570acd642e3808561cb9e975ccfd80b23dc5771cdb3306a5f23159dacbd3aa2db93d46d766e09ed15d900ad897a8d274dc26b47e994a27e97e2268a766533ae4b5e42a2fcaf755c1c4794b294c60555823",
+
+ "8d80d2d08dbd19c154df3f14673a14bd03735231f24e86bf153d0e69e74cbff7b1836e664de83f680124370fc0f96c9b65c07a366b644c4ab3",
+ "0c09582266df086310821ba7e18df64dfee6de09",
+ "10fd89768a60a67788abb5856a787c8561f3edcf9a83e898f7dc87ab8cce79429b43e56906941a886194f137e591fe7c339555361fbbe1f24feb2d4bcdb80601f3096bc9132deea60ae13082f44f9ad41cd628936a4d51176e42fc59cb76db815ce5ab4db99a104aafea68f5d330329ebf258d4ede16064bd1d00393d5e1570eb8",
+
+ "808405cdfc1a58b9bb0397c720722a81fffb76278f335917ef9c473814b3e016ba2973cd2765f8f3f82d6cc38aa7f8551827fe8d1e3884b7e61c94683b8f82f1843bdae2257eeec9812ad4c2cf283c34e0b0ae0fe3cb990cf88f2ef9",
+ "28039dcfe106d3b8296611258c4a56651c9e92dd",
+ "2b31fde99859b977aa09586d8e274662b25a2a640640b457f594051cb1e7f7a911865455242926cf88fe80dfa3a75ba9689844a11e634a82b075afbd69c12a0df9d25f84ad4945df3dc8fe90c3cefdf26e95f0534304b5bdba20d3e5640a2ebfb898aac35ae40f26fce5563c2f9f24f3042af76f3c7072d687bbfb959a88460af1",
+
+ "f337b9bad937de22a1a052dff11134a8ce26976202981939b91e0715ae5e609649da1adfcef3f4cca59b238360e7d1e496c7bf4b204b5acff9bbd6166a1d87a36ef2247373751039f8a800b8399807b3a85f44893497c0d05fb7017b82228152de6f25e6116dcc7503c786c875c28f3aa607e94ab0f19863ab1b5073770b0cd5f533acde30c6fb953cf3da680264e30fc11bff9a19bffab4779b6223c3fb3fe0f71abade4eb7c09c41e24c22d23fa148e6a173feb63984d1bc6ee3a02d915b752ceaf92a3015eceb38ca586c6801b37c34cefb2cff25ea23c08662dcab26a7a93a285d05d3044c",
+ "a77821ebbbef24628e4e12e1d0ea96de398f7b0f",
+ "32c7ca38ff26949a15000c4ba04b2b13b35a3810e568184d7ecabaa166b7ffabddf2b6cf4ba07124923790f2e5b1a5be040aea36fe132ec130e1f10567982d17ac3e89b8d26c3094034e762d2e031264f01170beecb3d1439e05846f25458367a7d9c02060444672671e64e877864559ca19b2074d588a281b5804d23772fbbe19",
+
+ "45013cebafd960b255476a8e2598b9aa32efbe6dc1f34f4a498d8cf5a2b4548d08c55d5f95f7bcc9619163056f2d58b52fa032",
+ "9d5ad8eb452134b65dc3a98b6a73b5f741609cd6",
+ "07eb651d75f1b52bc263b2e198336e99fbebc4f332049a922a10815607ee2d989db3a4495b7dccd38f58a211fb7e193171a3d891132437ebca44f318b280509e52b5fa98fcce8205d9697c8ee4b7ff59d4c59c79038a1970bd2a0d451ecdc5ef11d9979c9d35f8c70a6163717607890d586a7c6dc01c79f86a8f28e85235f8c2f1",
+
+ "2358097086c899323e75d9c90d0c09f12d9d54edfbdf70a9c2eb5a04d8f36b9b2bdf2aabe0a5bda1968937f9d6ebd3b6b257efb3136d4131f9acb59b85e2602c2a3fcdc835494a1f4e5ec18b226c80232b36a75a45fdf09a7ea9e98efbde1450d1194bf12e15a4c5f9eb5c0bce5269e0c3b28cfab655d81a61a20b4be2f54459bb25a0db94c52218be109a7426de83014424789aaa90e5056e632a698115e282c1a56410f26c2072f193481a9dcd880572005e64f4082ecf",
+ "3f2efc595880a7d47fcf3cba04983ea54c4b73fb",
+ "18da3cdcfe79bfb77fd9c32f377ad399146f0a8e810620233271a6e3ed3248903f5cdc92dc79b55d3e11615aa056a795853792a3998c349ca5c457e8ca7d29d796aa24f83491709befcfb1510ea513c92829a3f00b104f655634f320752e130ec0ccf6754ff893db302932bb025eb60e87822598fc619e0e981737a9a4c4152d33",
+
+ /* 1031-bit key */
+ "495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f778a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e582de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a2b8efab0561b0810344739ada0733f",
+ "010001",
+ "6c66ffe98980c38fcdeab5159898836165f4b4b817c4f6a8d486ee4ea9130fe9b9092bd136d184f95f504a607eac565846d2fdd6597a8967c7396ef95a6eeebb4578a643966dca4d8ee3de842de63279c618159c1ab54a89437b6a6120e4930afb52a4ba6ced8a4947ac64b30a3497cbe701c2d6266d517219ad0ec6d347dbe9",
+ "08dad7f11363faa623d5d6d5e8a319328d82190d7127d2846c439b0ab72619b0a43a95320e4ec34fc3a9cea876422305bd76c5ba7be9e2f410c8060645a1d29edb",
+ "0847e732376fc7900f898ea82eb2b0fc418565fdae62f7d9ec4ce2217b97990dd272db157f99f63c0dcbb9fbacdbd4c4dadb6df67756358ca4174825b48f49706d",
+ "05c2a83c124b3621a2aa57ea2c3efe035eff4560f33ddebb7adab81fce69a0c8c2edc16520dda83d59a23be867963ac65f2cc710bbcfb96ee103deb771d105fd85",
+ "04cae8aa0d9faa165c87b682ec140b8ed3b50b24594b7a3b2c220b3669bb819f984f55310a1ae7823651d4a02e99447972595139363434e5e30a7e7d241551e1b9",
+ "07d3e47bf686600b11ac283ce88dbb3f6051e8efd04680e44c171ef531b80b2b7c39fc766320e2cf15d8d99820e96ff30dc69691839c4b40d7b06e45307dc91f3f",
+
+ "81332f4be62948415ea1d899792eeacf6c6e1db1da8be13b5cea41db2fed467092e1ff398914c714259775f595f8547f735692a575e6923af78f22c6997ddb90fb6f72d7bb0dd5744a31decd3dc3685849836ed34aec596304ad11843c4f88489f209735f5fb7fdaf7cec8addc5818168f880acbf490d51005b7a8e84e43e54287977571dd99eea4b161eb2df1f5108f12a4142a83322edb05a75487a3435c9a78ce53ed93bc550857d7a9fb",
+ "1d65491d79c864b373009be6f6f2467bac4c78fa",
+ "0262ac254bfa77f3c1aca22c5179f8f040422b3c5bafd40a8f21cf0fa5a667ccd5993d42dbafb409c520e25fce2b1ee1e716577f1efa17f3da28052f40f0419b23106d7845aaf01125b698e7a4dfe92d3967bb00c4d0d35ba3552ab9a8b3eef07c7fecdbc5424ac4db1e20cb37d0b2744769940ea907e17fbbca673b20522380c5",
+
+ "e2f96eaf0e05e7ba326ecca0ba7fd2f7c02356f3cede9d0faabf4fcc8e60a973e5595fd9ea08",
+ "435c098aa9909eb2377f1248b091b68987ff1838",
+ "2707b9ad5115c58c94e932e8ec0a280f56339e44a1b58d4ddcff2f312e5f34dcfe39e89c6a94dcee86dbbdae5b79ba4e0819a9e7bfd9d982e7ee6c86ee68396e8b3a14c9c8f34b178eb741f9d3f121109bf5c8172fada2e768f9ea1433032c004a8aa07eb990000a48dc94c8bac8aabe2b09b1aa46c0a2aa0e12f63fbba775ba7e",
+
+ "e35c6ed98f64a6d5a648fcab8adb16331db32e5d15c74a40edf94c3dc4a4de792d190889f20f1e24ed12054a6b28798fcb42d1c548769b734c96373142092aed277603f4738df4dc1446586d0ec64da4fb60536db2ae17fc7e3c04bbfbbbd907bf117c08636fa16f95f51a6216934d3e34f85030f17bbbc5ba69144058aff081e0b19cf03c17195c5e888ba58f6fe0a02e5c3bda9719a7",
+ "c6ebbe76df0c4aea32c474175b2f136862d04529",
+ "2ad20509d78cf26d1b6c406146086e4b0c91a91c2bd164c87b966b8faa42aa0ca446022323ba4b1a1b89706d7f4c3be57d7b69702d168ab5955ee290356b8c4a29ed467d547ec23cbadf286ccb5863c6679da467fc9324a151c7ec55aac6db4084f82726825cfe1aa421bc64049fb42f23148f9c25b2dc300437c38d428aa75f96",
+
+ "dbc5f750a7a14be2b93e838d18d14a8695e52e8add9c0ac733b8f56d2747e529a0cca532dd49b902aefed514447f9e81d16195c2853868cb9b30f7d0d495c69d01b5c5d50b27045db3866c2324a44a110b1717746de457d1c8c45c3cd2a92970c3d59632055d4c98a41d6e99e2a3ddd5f7f9979ab3cd18f37505d25141de2a1bff17b3a7dce9419ecc385cf11d72840f19953fd0509251f6cafde2893d0e75c781ba7a5012ca401a4fa99e04b3c3249f926d5afe82cc87dab22c3c1b105de48e34ace9c9124e59597ac7ebf8",
+ "021fdcc6ebb5e19b1cb16e9c67f27681657fe20a",
+ "1e24e6e58628e5175044a9eb6d837d48af1260b0520e87327de7897ee4d5b9f0df0be3e09ed4dea8c1454ff3423bb08e1793245a9df8bf6ab3968c8eddc3b5328571c77f091cc578576912dfebd164b9de5454fe0be1c1f6385b328360ce67ec7a05f6e30eb45c17c48ac70041d2cab67f0a2ae7aafdcc8d245ea3442a6300ccc7",
+
+ "04dc251be72e88e5723485b6383a637e2fefe07660c519a560b8bc18bdedb86eae2364ea53ba9dca6eb3d2e7d6b806af42b3e87f291b4a8881d5bf572cc9a85e19c86acb28f098f9da0383c566d3c0f58cfd8f395dcf602e5cd40e8c7183f714996e2297ef",
+ "c558d7167cbb4508ada042971e71b1377eea4269",
+ "33341ba3576a130a50e2a5cf8679224388d5693f5accc235ac95add68e5eb1eec31666d0ca7a1cda6f70a1aa762c05752a51950cdb8af3c5379f18cfe6b5bc55a4648226a15e912ef19ad77adeea911d67cfefd69ba43fa4119135ff642117ba985a7e0100325e9519f1ca6a9216bda055b5785015291125e90dcd07a2ca9673ee",
+
+ "0ea37df9a6fea4a8b610373c24cf390c20fa6e2135c400c8a34f5c183a7e8ea4c9ae090ed31759f42dc77719cca400ecdcc517acfc7ac6902675b2ef30c509665f3321482fc69a9fb570d15e01c845d0d8e50d2a24cbf1cf0e714975a5db7b18d9e9e9cb91b5cb16869060ed18b7b56245503f0caf90352b8de81cb5a1d9c6336092f0cd",
+ "76fd4e64fdc98eb927a0403e35a084e76ba9f92a",
+ "1ed1d848fb1edb44129bd9b354795af97a069a7a00d0151048593e0c72c3517ff9ff2a41d0cb5a0ac860d736a199704f7cb6a53986a88bbd8abcc0076a2ce847880031525d449da2ac78356374c536e343faa7cba42a5aaa6506087791c06a8e989335aed19bfab2d5e67e27fb0c2875af896c21b6e8e7309d04e4f6727e69463e",
+
+ /* 1536-bit key */
+ "e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd06c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee315ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddbc2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8de3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6fd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b",
+ "010001",
+ "6a7fd84fb85fad073b34406db74f8d61a6abc12196a961dd79565e9da6e5187bce2d980250f7359575359270d91590bb0e427c71460b55d51410b191bcf309fea131a92c8e702738fa719f1e0041f52e40e91f229f4d96a1e6f172e15596b4510a6daec26105f2bebc53316b87bdf21311666070e8dfee69d52c71a976caae79c72b68d28580dc686d9f5129d225f82b3d615513a882b3db91416b48ce08888213e37eeb9af800d81cab328ce420689903c00c7b5fd31b75503a6d419684d629",
+ "f8eb97e98df12664eefdb761596a69ddcd0e76daece6ed4bf5a1b50ac086f7928a4d2f8726a77e515b74da41988f220b1cc87aa1fc810ce99a82f2d1ce821edced794c6941f42c7a1a0b8c4d28c75ec60b652279f6154a762aed165d47dee367",
+ "ed4d71d0a6e24b93c2e5f6b4bbe05f5fb0afa042d204fe3378d365c2f288b6a8dad7efe45d153eef40cacc7b81ff934002d108994b94a5e4728cd9c963375ae49965bda55cbf0efed8d6553b4027f2d86208a6e6b489c176128092d629e49d3d",
+ "2bb68bddfb0c4f56c8558bffaf892d8043037841e7fa81cfa61a38c5e39b901c8ee71122a5da2227bd6cdeeb481452c12ad3d61d5e4f776a0ab556591befe3e59e5a7fddb8345e1f2f35b9f4cee57c32414c086aec993e9353e480d9eec6289f",
+ "4ff897709fad079746494578e70fd8546130eeab5627c49b080f05ee4ad9f3e4b7cba9d6a5dff113a41c3409336833f190816d8a6bc42e9bec56b7567d0f3c9c696db619b245d901dd856db7c8092e77e9a1cccd56ee4dba42c5fdb61aec2669",
+ "77b9d1137b50404a982729316efafc7dfe66d34e5a182600d5f30a0a8512051c560d081d4d0a1835ec3d25a60f4e4d6aa948b2bf3dbb5b124cbbc3489255a3a948372f6978496745f943e1db4f18382ceaa505dfc65757bb3f857a58dce52156",
+
+ "a88e265855e9d7ca36c68795f0b31b591cd6587c71d060a0b3f7f3eaef43795922028bc2b6ad467cfc2d7f659c5385aa70ba3672cdde4cfe4970cc7904601b278872bf51321c4a972f3c95570f3445d4f57980e0f20df54846e6a52c668f1288c03f95006ea32f562d40d52af9feb32f0fa06db65b588a237b34e592d55cf979f903a642ef64d2ed542aa8c77dc1dd762f45a59303ed75e541ca271e2b60ca709e44fa0661131e8d5d4163fd8d398566ce26de8730e72f9cca737641c244159420637028df0a18079d6208ea8b4711a2c750f5",
+ "c0a425313df8d7564bd2434d311523d5257eed80",
+ "586107226c3ce013a7c8f04d1a6a2959bb4b8e205ba43a27b50f124111bc35ef589b039f5932187cb696d7d9a32c0c38300a5cdda4834b62d2eb240af33f79d13dfbf095bf599e0d9686948c1964747b67e89c9aba5cd85016236f566cc5802cb13ead51bc7ca6bef3b94dcbdbb1d570469771df0e00b1a8a06777472d2316279edae86474668d4e1efff95f1de61c6020da32ae92bbf16520fef3cf4d88f61121f24bbd9fe91b59caf1235b2a93ff81fc403addf4ebdea84934a9cdaf8e1a9e",
+
+ "c8c9c6af04acda414d227ef23e0820c3732c500dc87275e95b0d095413993c2658bc1d988581ba879c2d201f14cb88ced153a01969a7bf0a7be79c84c1486bc12b3fa6c59871b6827c8ce253ca5fefa8a8c690bf326e8e37cdb96d90a82ebab69f86350e1822e8bd536a2e",
+ "b307c43b4850a8dac2f15f32e37839ef8c5c0e91",
+ "80b6d643255209f0a456763897ac9ed259d459b49c2887e5882ecb4434cfd66dd7e1699375381e51cd7f554f2c271704b399d42b4be2540a0eca61951f55267f7c2878c122842dadb28b01bd5f8c025f7e228418a673c03d6bc0c736d0a29546bd67f786d9d692ccea778d71d98c2063b7a71092187a4d35af108111d83e83eae46c46aa34277e06044589903788f1d5e7cee25fb485e92949118814d6f2c3ee361489016f327fb5bc517eb50470bffa1afa5f4ce9aa0ce5b8ee19bf5501b958",
+
+ "0afad42ccd4fc60654a55002d228f52a4a5fe03b8bbb08ca82daca558b44dbe1266e50c0e745a36d9d2904e3408abcd1fd569994063f4a75cc72f2fee2a0cd893a43af1c5b8b487df0a71610024e4f6ddf9f28ad0813c1aab91bcb3c9064d5ff742deffea657094139369e5ea6f4a96319a5cc8224145b545062758fefd1fe3409ae169259c6cdfd6b5f2958e314faecbe69d2cace58ee55179ab9b3e6d1ecc14a557c5febe988595264fc5da1c571462eca798a18a1a4940cdab4a3e92009ccd42e1e947b1314e32238a2dece7d23a89b5b30c751fd0a4a430d2c548594",
+ "9a2b007e80978bbb192c354eb7da9aedfc74dbf5",
+ "484408f3898cd5f53483f80819efbf2708c34d27a8b2a6fae8b322f9240237f981817aca1846f1084daa6d7c0795f6e5bf1af59c38e1858437ce1f7ec419b98c8736adf6dd9a00b1806d2bd3ad0a73775e05f52dfef3a59ab4b08143f0df05cd1ad9d04bececa6daa4a2129803e200cbc77787caf4c1d0663a6c5987b605952019782caf2ec1426d68fb94ed1d4be816a7ed081b77e6ab330b3ffc073820fecde3727fcbe295ee61a050a343658637c3fd659cfb63736de32d9f90d3c2f63eca",
+
+ "1dfd43b46c93db82629bdae2bd0a12b882ea04c3b465f5cf93023f01059626dbbe99f26bb1be949dddd16dc7f3debb19a194627f0b224434df7d8700e9e98b06e360c12fdbe3d19f51c9684eb9089ecbb0a2f0450399d3f59eac7294085d044f5393c6ce737423d8b86c415370d389e30b9f0a3c02d25d0082e8ad6f3f1ef24a45c3cf82b383367063a4d4613e4264f01b2dac2e5aa42043f8fb5f69fa871d14fb273e767a531c40f02f343bc2fb45a0c7e0f6be2561923a77211d66a6e2dbb43c366350beae22da3ac2c1f5077096fcb5c4bf255f7574351ae0b1e1f03632817c0856d4a8ba97afbdc8b85855402bc56926fcec209f9ea8",
+ "70f382bddf4d5d2dd88b3bc7b7308be632b84045",
+ "84ebeb481be59845b46468bafb471c0112e02b235d84b5d911cbd1926ee5074ae0424495cb20e82308b8ebb65f419a03fb40e72b78981d88aad143053685172c97b29c8b7bf0ae73b5b2263c403da0ed2f80ff7450af7828eb8b86f0028bd2a8b176a4d228cccea18394f238b09ff758cc00bc04301152355742f282b54e663a919e709d8da24ade5500a7b9aa50226e0ca52923e6c2d860ec50ff480fa57477e82b0565f4379f79c772d5c2da80af9fbf325ece6fc20b00961614bee89a183e",
+
+ "1bdc6e7c98fb8cf54e9b097b66a831e9cfe52d9d4888448ee4b0978093ba1d7d73ae78b3a62ba4ad95cd289ccb9e005226bb3d178bccaa821fb044a4e21ee97696c14d0678c94c2dae93b0ad73922218553daa7e44ebe57725a7a45cc72b9b2138a6b17c8db411ce8279ee1241aff0a8bec6f77f87edb0c69cb27236e3435a800b192e4f11e519e3fe30fc30eaccca4fbb41769029bf708e817a9e683805be67fa100984683b74838e3bcffa79366eed1d481c76729118838f31ba8a048a93c1be4424598e8df6328b7a77880a3f9c7e2e8dfca8eb5a26fb86bdc556d42bbe01d9fa6ed80646491c9341",
+ "d689257a86effa68212c5e0c619eca295fb91b67",
+ "82102df8cb91e7179919a04d26d335d64fbc2f872c44833943241de8454810274cdf3db5f42d423db152af7135f701420e39b494a67cbfd19f9119da233a23da5c6439b5ba0d2bc373eee3507001378d4a4073856b7fe2aba0b5ee93b27f4afec7d4d120921c83f606765b02c19e4d6a1a3b95fa4c422951be4f52131077ef17179729cddfbdb56950dbaceefe78cb16640a099ea56d24389eef10f8fecb31ba3ea3b227c0a86698bb89e3e9363905bf22777b2a3aa521b65b4cef76d83bde4c",
+
+ "88c7a9f1360401d90e53b101b61c5325c3c75db1b411fbeb8e830b75e96b56670ad245404e16793544ee354bc613a90cc9848715a73db5893e7f6d279815c0c1de83ef8e2956e3a56ed26a888d7a9cdcd042f4b16b7fa51ef1a0573662d16a302d0ec5b285d2e03ad96529c87b3d374db372d95b2443d061b6b1a350ba87807ed083afd1eb05c3f52f4eba5ed2227714fdb50b9d9d9dd6814f62f6272fcd5cdbce7a9ef797",
+ "c25f13bf67d081671a0481a1f1820d613bba2276",
+ "a7fdb0d259165ca2c88d00bbf1028a867d337699d061193b17a9648e14ccbbaadeacaacdec815e7571294ebb8a117af205fa078b47b0712c199e3ad05135c504c24b81705115740802487992ffd511d4afc6b854491eb3f0dd523139542ff15c3101ee85543517c6a3c79417c67e2dd9aa741e9a29b06dcb593c2336b3670ae3afbac7c3e76e215473e866e338ca244de00b62624d6b9426822ceae9f8cc460895f41250073fd45c5a1e7b425c204a423a699159f6903e710b37a7bb2bc8049f",
+
+ /* 2048-bit key */
+ "a5dd867ac4cb02f90b9457d48c14a770ef991c56c39c0ec65fd11afa8937cea57b9be7ac73b45c0017615b82d622e318753b6027c0fd157be12f8090fee2a7adcd0eef759f88ba4997c7a42d58c9aa12cb99ae001fe521c13bb5431445a8d5ae4f5e4c7e948ac227d3604071f20e577e905fbeb15dfaf06d1de5ae6253d63a6a2120b31a5da5dabc9550600e20f27d3739e2627925fea3cc509f21dff04e6eea4549c540d6809ff9307eede91fff58733d8385a237d6d3705a33e391900992070df7adf1357cf7e3700ce3667de83f17b8df1778db381dce09cb4ad058a511001a738198ee27cf55a13b754539906582ec8b174bd58d5d1f3d767c613721ae05",
+ "010001",
+ "2d2ff567b3fe74e06191b7fded6de112290c670692430d5969184047da234c9693deed1673ed429539c969d372c04d6b47e0f5b8cee0843e5c22835dbd3b05a0997984ae6058b11bc4907cbf67ed84fa9ae252dfb0d0cd49e618e35dfdfe59bca3ddd66c33cebbc77ad441aa695e13e324b518f01c60f5a85c994ad179f2a6b5fbe93402b11767be01bf073444d6ba1dd2bca5bd074d4a5fae3531ad1303d84b30d897318cbbba04e03c2e66de6d91f82f96ea1d4bb54a5aae102d594657f5c9789553512b296dea29d8023196357e3e3a6e958f39e3c2344038ea604b31edc6f0f7ff6e7181a57c92826a268f86768e96f878562fc71d85d69e448612f7048f",
+ "cfd50283feeeb97f6f08d73cbc7b3836f82bbcd499479f5e6f76fdfcb8b38c4f71dc9e88bd6a6f76371afd65d2af1862b32afb34a95f71b8b132043ffebe3a952baf7592448148c03f9c69b1d68e4ce5cf32c86baf46fed301ca1ab403069b32f456b91f71898ab081cd8c4252ef5271915c9794b8f295851da7510f99cb73eb",
+ "cc4e90d2a1b3a065d3b2d1f5a8fce31b544475664eab561d2971b99fb7bef844e8ec1f360b8c2ac8359692971ea6a38f723fcc211f5dbcb177a0fdac5164a1d4ff7fbb4e829986353cb983659a148cdd420c7d31ba3822ea90a32be46c030e8c17e1fa0ad37859e06b0aa6fa3b216d9cbe6c0e22339769c0a615913e5da719cf",
+ "1c2d1fc32f6bc4004fd85dfde0fbbf9a4c38f9c7c4e41dea1aa88234a201cd92f3b7da526583a98ad85bb360fb983b711e23449d561d1778d7a515486bcbf47b46c9e9e1a3a1f77000efbeb09a8afe47e5b857cda99cb16d7fff9b712e3bd60ca96d9c7973d616d46934a9c050281c004399ceff1db7dda78766a8a9b9cb0873",
+ "cb3b3c04caa58c60be7d9b2debb3e39643f4f57397be08236a1e9eafaa706536e71c3acfe01cc651f23c9e05858fee13bb6a8afc47df4edc9a4ba30bcecb73d0157852327ee789015c2e8dee7b9f05a0f31ac94eb6173164740c5c95147cd5f3b5ae2cb4a83787f01d8ab31f27c2d0eea2dd8a11ab906aba207c43c6ee125331",
+ "12f6b2cf1374a736fad05616050f96ab4b61d1177c7f9d525a29f3d180e77667e99d99abf0525d0758660f3752655b0f25b8df8431d9a8ff77c16c12a0a5122a9f0bf7cfd5a266a35c159f991208b90316ff444f3e0b6bd0e93b8a7a2448e957e3dda6cfcf2266b106013ac46808d3b3887b3b00344baac9530b4ce708fc32b6",
+
+ "883177e5126b9be2d9a9680327d5370c6f26861f5820c43da67a3ad609",
+ "04e215ee6ff934b9da70d7730c8734abfcecde89",
+ "82c2b160093b8aa3c0f7522b19f87354066c77847abf2a9fce542d0e84e920c5afb49ffdfdace16560ee94a1369601148ebad7a0e151cf16331791a5727d05f21e74e7eb811440206935d744765a15e79f015cb66c532c87a6a05961c8bfad741a9a6657022894393e7223739796c02a77455d0f555b0ec01ddf259b6207fd0fd57614cef1a5573baaff4ec00069951659b85f24300a25160ca8522dc6e6727e57d019d7e63629b8fe5e89e25cc15beb3a647577559299280b9b28f79b0409000be25bbd96408ba3b43cc486184dd1c8e62553fa1af4040f60663de7f5e49c04388e257f1ce89c95dab48a315d9b66b1b7628233876ff2385230d070d07e1666",
+
+ "dd670a01465868adc93f26131957a50c52fb777cdbaa30892c9e12361164ec13979d43048118e4445db87bee58dd987b3425d02071d8dbae80708b039dbb64dbd1de5657d9fed0c118a54143742e0ff3c87f74e45857647af3f79eb0a14c9d75ea9a1a04b7cf478a897a708fd988f48e801edb0b7039df8c23bb3c56f4e821ac",
+ "8b2bdd4b40faf545c778ddf9bc1a49cb57f9b71b",
+ "14ae35d9dd06ba92f7f3b897978aed7cd4bf5ff0b585a40bd46ce1b42cd2703053bb9044d64e813d8f96db2dd7007d10118f6f8f8496097ad75e1ff692341b2892ad55a633a1c55e7f0a0ad59a0e203a5b8278aec54dd8622e2831d87174f8caff43ee6c46445345d84a59659bfb92ecd4c818668695f34706f66828a89959637f2bf3e3251c24bdba4d4b7649da0022218b119c84e79a6527ec5b8a5f861c159952e23ec05e1e717346faefe8b1686825bd2b262fb2531066c0de09acde2e4231690728b5d85e115a2f6b92b79c25abc9bd9399ff8bcf825a52ea1f56ea76dd26f43baafa18bfa92a504cbd35699e26d1dcc5a2887385f3c63232f06f3244c3",
+
+ "48b2b6a57a63c84cea859d65c668284b08d96bdcaabe252db0e4a96cb1bac6019341db6fbefb8d106b0e90eda6bcc6c6262f37e7ea9c7e5d226bd7df85ec5e71efff2f54c5db577ff729ff91b842491de2741d0c631607df586b905b23b91af13da12304bf83eca8a73e871ff9db",
+ "4e96fc1b398f92b44671010c0dc3efd6e20c2d73",
+ "6e3e4d7b6b15d2fb46013b8900aa5bbb3939cf2c095717987042026ee62c74c54cffd5d7d57efbbf950a0f5c574fa09d3fc1c9f513b05b4ff50dd8df7edfa20102854c35e592180119a70ce5b085182aa02d9ea2aa90d1df03f2daae885ba2f5d05afdac97476f06b93b5bc94a1a80aa9116c4d615f333b098892b25fface266f5db5a5a3bcc10a824ed55aad35b727834fb8c07da28fcf416a5d9b2224f1f8b442b36f91e456fdea2d7cfe3367268de0307a4c74e924159ed33393d5e0655531c77327b89821bdedf880161c78cd4196b5419f7acc3f13e5ebf161b6e7c6724716ca33b85c2e25640192ac2859651d50bde7eb976e51cec828b98b6563b86bb",
+
+ "0b8777c7f839baf0a64bbbdbc5ce79755c57a205b845c174e2d2e90546a089c4e6ec8adffa23a7ea97bae6b65d782b82db5d2b5a56d22a29a05e7c4433e2b82a621abba90add05ce393fc48a840542451a",
+ "c7cd698d84b65128d8835e3a8b1eb0e01cb541ec",
+ "34047ff96c4dc0dc90b2d4ff59a1a361a4754b255d2ee0af7d8bf87c9bc9e7ddeede33934c63ca1c0e3d262cb145ef932a1f2c0a997aa6a34f8eaee7477d82ccf09095a6b8acad38d4eec9fb7eab7ad02da1d11d8e54c1825e55bf58c2a23234b902be124f9e9038a8f68fa45dab72f66e0945bf1d8bacc9044c6f07098c9fcec58a3aab100c805178155f030a124c450e5acbda47d0e4f10b80a23f803e774d023b0015c20b9f9bbe7c91296338d5ecb471cafb032007b67a60be5f69504a9f01abb3cb467b260e2bce860be8d95bf92c0c8e1496ed1e528593a4abb6df462dde8a0968dffe4683116857a232f5ebf6c85be238745ad0f38f767a5fdbf486fb",
+
+ "f1036e008e71e964dadc9219ed30e17f06b4b68a955c16b312b1eddf028b74976bed6b3f6a63d4e77859243c9cccdc98016523abb02483b35591c33aad81213bb7c7bb1a470aabc10d44256c4d4559d916",
+ "efa8bff96212b2f4a3f371a10d574152655f5dfb",
+ "7e0935ea18f4d6c1d17ce82eb2b3836c55b384589ce19dfe743363ac9948d1f346b7bfddfe92efd78adb21faefc89ade42b10f374003fe122e67429a1cb8cbd1f8d9014564c44d120116f4990f1a6e38774c194bd1b8213286b077b0499d2e7b3f434ab12289c556684deed78131934bb3dd6537236f7c6f3dcb09d476be07721e37e1ceed9b2f7b406887bd53157305e1c8b4f84d733bc1e186fe06cc59b6edb8f4bd7ffefdf4f7ba9cfb9d570689b5a1a4109a746a690893db3799255a0cb9215d2d1cd490590e952e8c8786aa0011265252470c041dfbc3eec7c3cbf71c24869d115c0cb4a956f56d530b80ab589acfefc690751ddf36e8d383f83cedd2cc",
+
+ "25f10895a87716c137450bb9519dfaa1f207faa942ea88abf71e9c17980085b555aebab76264ae2a3ab93c2d12981191ddac6fb5949eb36aee3c5da940f00752c916d94608fa7d97ba6a2915b688f20323d4e9d96801d89a72ab5892dc2117c07434fcf972e058cf8c41ca4b4ff554f7d5068ad3155fced0f3125bc04f9193378a8f5c4c3b8cb4dd6d1cc69d30ecca6eaa51e36a05730e9e342e855baf099defb8afd7",
+ "ad8b1523703646224b660b550885917ca2d1df28",
+ "6d3b5b87f67ea657af21f75441977d2180f91b2c5f692de82955696a686730d9b9778d970758ccb26071c2209ffbd6125be2e96ea81b67cb9b9308239fda17f7b2b64ecda096b6b935640a5a1cb42a9155b1c9ef7a633a02c59f0d6ee59b852c43b35029e73c940ff0410e8f114eed46bbd0fae165e42be2528a401c3b28fd818ef3232dca9f4d2a0f5166ec59c42396d6c11dbc1215a56fa17169db9575343ef34f9de32a49cdc3174922f229c23e18e45df9353119ec4319cedce7a17c64088c1f6f52be29634100b3919d38f3d1ed94e6891e66a73b8fb849f5874df59459e298c7bbce2eee782a195aa66fe2d0732b25e595f57d3e061b1fc3e4063bf98f",
+
+ NULL
+};
+
+static void
+test_RSA_PSS(const char *name,
+ br_rsa_pss_sign sign, br_rsa_pss_vrfy vrfy)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ u = 0;
+ while (KAT_RSA_PSS[u] != NULL) {
+ unsigned char n[512];
+ unsigned char e[8];
+ unsigned char d[512];
+ unsigned char p[256];
+ unsigned char q[256];
+ unsigned char dp[256];
+ unsigned char dq[256];
+ unsigned char iq[256];
+ br_rsa_public_key pk;
+ br_rsa_private_key sk;
+ size_t v;
+
+ pk.n = n;
+ pk.nlen = hextobin(n, KAT_RSA_PSS[u ++]);
+ pk.e = e;
+ pk.elen = hextobin(e, KAT_RSA_PSS[u ++]);
+
+ /*
+ * 'd' is in the test vectors, but we don't use it.
+ */
+ hextobin(d, KAT_RSA_PSS[u ++]);
+
+ for (v = 0; n[v] == 0; v ++);
+ sk.n_bitlen = BIT_LENGTH(n[v]) + ((pk.nlen - 1 - v) << 3);
+ sk.p = p;
+ sk.plen = hextobin(p, KAT_RSA_PSS[u ++]);
+ sk.q = q;
+ sk.qlen = hextobin(q, KAT_RSA_PSS[u ++]);
+ sk.dp = dp;
+ sk.dplen = hextobin(dp, KAT_RSA_PSS[u ++]);
+ sk.dq = dq;
+ sk.dqlen = hextobin(dq, KAT_RSA_PSS[u ++]);
+ sk.iq = iq;
+ sk.iqlen = hextobin(iq, KAT_RSA_PSS[u ++]);
+
+ for (v = 0; v < 6; v ++) {
+ unsigned char plain[512], salt[128], sig[512];
+ size_t plain_len, salt_len, sig_len;
+ rng_fake_ctx rng;
+ unsigned char hash[20], tmp[513];
+ br_sha1_context sc;
+
+ plain_len = hextobin(plain, KAT_RSA_PSS[u ++]);
+ salt_len = hextobin(salt, KAT_RSA_PSS[u ++]);
+ sig_len = hextobin(sig, KAT_RSA_PSS[u ++]);
+
+ br_sha1_init(&sc);
+ br_sha1_update(&sc, plain, plain_len);
+ br_sha1_out(&sc, hash);
+ rng_fake_init(&rng, NULL, salt, salt_len);
+
+ memset(tmp, 0, sizeof tmp);
+ if (sign(&rng.vtable,
+ &br_sha1_vtable, &br_sha1_vtable,
+ hash, salt_len, &sk, tmp) != 1)
+ {
+ fprintf(stderr, "signature failed\n");
+ }
+ if (rng.ptr != rng.len) {
+ fprintf(stderr, "salt not fully consumed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA/PSS sign", tmp, sig, sig_len);
+
+ if (vrfy(sig, sig_len,
+ &br_sha1_vtable, &br_sha1_vtable,
+ hash, salt_len, &pk) != 1)
+ {
+ fprintf(stderr, "verification failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ sig[sig_len >> 1] ^= 0x01;
+ if (vrfy(sig, sig_len,
+ &br_sha1_vtable, &br_sha1_vtable,
+ hash, salt_len, &pk) != 0)
+ {
+ fprintf(stderr,
+ "verification should have failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+/*
+ * Test vectors from pkcs-1v2-1d2-vec.zip (originally from ftp.rsa.com).
+ * There are ten RSA keys, and for each RSA key, there are 6 messages,
+ * each with an explicit seed.
+ *
+ * Field order:
+ * modulus (n)
+ * public exponent (e)
+ * first factor (p)
+ * second factor (q)
+ * first private exponent (dp)
+ * second private exponent (dq)
+ * CRT coefficient (iq)
+ * cleartext 1
+ * seed 1 (20-byte random value)
+ * ciphertext 1
+ * cleartext 2
+ * seed 2 (20-byte random value)
+ * ciphertext 2
+ * ...
+ * cleartext 6
+ * seed 6 (20-byte random value)
+ * ciphertext 6
+ *
+ * This pattern is repeated for all keys. The array stops on a NULL.
+ */
+static const char *KAT_RSA_OAEP[] = {
+ /* 1024-bit key, from oeap-int.txt */
+ "BBF82F090682CE9C2338AC2B9DA871F7368D07EED41043A440D6B6F07454F51FB8DFBAAF035C02AB61EA48CEEB6FCD4876ED520D60E1EC4619719D8A5B8B807FAFB8E0A3DFC737723EE6B4B7D93A2584EE6A649D060953748834B2454598394EE0AAB12D7B61A51F527A9A41F6C1687FE2537298CA2A8F5946F8E5FD091DBDCB",
+ "11",
+ "EECFAE81B1B9B3C908810B10A1B5600199EB9F44AEF4FDA493B81A9E3D84F632124EF0236E5D1E3B7E28FAE7AA040A2D5B252176459D1F397541BA2A58FB6599",
+ "C97FB1F027F453F6341233EAAAD1D9353F6C42D08866B1D05A0F2035028B9D869840B41666B42E92EA0DA3B43204B5CFCE3352524D0416A5A441E700AF461503",
+ "54494CA63EBA0337E4E24023FCD69A5AEB07DDDC0183A4D0AC9B54B051F2B13ED9490975EAB77414FF59C1F7692E9A2E202B38FC910A474174ADC93C1F67C981",
+ "471E0290FF0AF0750351B7F878864CA961ADBD3A8A7E991C5C0556A94C3146A7F9803F8F6F8AE342E931FD8AE47A220D1B99A495849807FE39F9245A9836DA3D",
+ "B06C4FDABB6301198D265BDBAE9423B380F271F73453885093077FCD39E2119FC98632154F5883B167A967BF402B4E9E2E0F9656E698EA3666EDFB25798039F7",
+
+ /* oaep-int.txt contains only one message, so we repeat it six
+ times to respect our array format. */
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ "D436E99569FD32A7C8A05BBC90D32C49",
+ "AAFD12F659CAE63489B479E5076DDEC2F06CB58F",
+ "1253E04DC0A5397BB44A7AB87E9BF2A039A33D1E996FC82A94CCD30074C95DF763722017069E5268DA5D1C0B4F872CF653C11DF82314A67968DFEAE28DEF04BB6D84B1C31D654A1970E5783BD6EB96A024C2CA2F4A90FE9F2EF5C9C140E5BB48DA9536AD8700C84FC9130ADEA74E558D51A74DDF85D8B50DE96838D6063E0955",
+
+ /* 1024-bit key */
+ "A8B3B284AF8EB50B387034A860F146C4919F318763CD6C5598C8AE4811A1E0ABC4C7E0B082D693A5E7FCED675CF4668512772C0CBC64A742C6C630F533C8CC72F62AE833C40BF25842E984BB78BDBF97C0107D55BDB662F5C4E0FAB9845CB5148EF7392DD3AAFF93AE1E6B667BB3D4247616D4F5BA10D4CFD226DE88D39F16FB",
+ "010001",
+ "D32737E7267FFE1341B2D5C0D150A81B586FB3132BED2F8D5262864A9CB9F30AF38BE448598D413A172EFB802C21ACF1C11C520C2F26A471DCAD212EAC7CA39D",
+ "CC8853D1D54DA630FAC004F471F281C7B8982D8224A490EDBEB33D3E3D5CC93C4765703D1DD791642F1F116A0DD852BE2419B2AF72BFE9A030E860B0288B5D77",
+ "0E12BF1718E9CEF5599BA1C3882FE8046A90874EEFCE8F2CCC20E4F2741FB0A33A3848AEC9C9305FBECBD2D76819967D4671ACC6431E4037968DB37878E695C1",
+ "95297B0F95A2FA67D00707D609DFD4FC05C89DAFC2EF6D6EA55BEC771EA333734D9251E79082ECDA866EFEF13C459E1A631386B7E354C899F5F112CA85D71583",
+ "4F456C502493BDC0ED2AB756A3A6ED4D67352A697D4216E93212B127A63D5411CE6FA98D5DBEFD73263E3728142743818166ED7DD63687DD2A8CA1D2F4FBD8E1",
+
+ "6628194E12073DB03BA94CDA9EF9532397D50DBA79B987004AFEFE34",
+ "18B776EA21069D69776A33E96BAD48E1DDA0A5EF",
+ "354FE67B4A126D5D35FE36C777791A3F7BA13DEF484E2D3908AFF722FAD468FB21696DE95D0BE911C2D3174F8AFCC201035F7B6D8E69402DE5451618C21A535FA9D7BFC5B8DD9FC243F8CF927DB31322D6E881EAA91A996170E657A05A266426D98C88003F8477C1227094A0D9FA1E8C4024309CE1ECCCB5210035D47AC72E8A",
+
+ "750C4047F547E8E41411856523298AC9BAE245EFAF1397FBE56F9DD5",
+ "0CC742CE4A9B7F32F951BCB251EFD925FE4FE35F",
+ "640DB1ACC58E0568FE5407E5F9B701DFF8C3C91E716C536FC7FCEC6CB5B71C1165988D4A279E1577D730FC7A29932E3F00C81515236D8D8E31017A7A09DF4352D904CDEB79AA583ADCC31EA698A4C05283DABA9089BE5491F67C1A4EE48DC74BBBE6643AEF846679B4CB395A352D5ED115912DF696FFE0702932946D71492B44",
+
+ "D94AE0832E6445CE42331CB06D531A82B1DB4BAAD30F746DC916DF24D4E3C2451FFF59A6423EB0E1D02D4FE646CF699DFD818C6E97B051",
+ "2514DF4695755A67B288EAF4905C36EEC66FD2FD",
+ "423736ED035F6026AF276C35C0B3741B365E5F76CA091B4E8C29E2F0BEFEE603595AA8322D602D2E625E95EB81B2F1C9724E822ECA76DB8618CF09C5343503A4360835B5903BC637E3879FB05E0EF32685D5AEC5067CD7CC96FE4B2670B6EAC3066B1FCF5686B68589AAFB7D629B02D8F8625CA3833624D4800FB081B1CF94EB",
+
+ "52E650D98E7F2A048B4F86852153B97E01DD316F346A19F67A85",
+ "C4435A3E1A18A68B6820436290A37CEFB85DB3FB",
+ "45EAD4CA551E662C9800F1ACA8283B0525E6ABAE30BE4B4ABA762FA40FD3D38E22ABEFC69794F6EBBBC05DDBB11216247D2F412FD0FBA87C6E3ACD888813646FD0E48E785204F9C3F73D6D8239562722DDDD8771FEC48B83A31EE6F592C4CFD4BC88174F3B13A112AAE3B9F7B80E0FC6F7255BA880DC7D8021E22AD6A85F0755",
+
+ "8DA89FD9E5F974A29FEFFB462B49180F6CF9E802",
+ "B318C42DF3BE0F83FEA823F5A7B47ED5E425A3B5",
+ "36F6E34D94A8D34DAACBA33A2139D00AD85A9345A86051E73071620056B920E219005855A213A0F23897CDCD731B45257C777FE908202BEFDD0B58386B1244EA0CF539A05D5D10329DA44E13030FD760DCD644CFEF2094D1910D3F433E1C7C6DD18BC1F2DF7F643D662FB9DD37EAD9059190F4FA66CA39E869C4EB449CBDC439",
+
+ "26521050844271",
+ "E4EC0982C2336F3A677F6A356174EB0CE887ABC2",
+ "42CEE2617B1ECEA4DB3F4829386FBD61DAFBF038E180D837C96366DF24C097B4AB0FAC6BDF590D821C9F10642E681AD05B8D78B378C0F46CE2FAD63F74E0AD3DF06B075D7EB5F5636F8D403B9059CA761B5C62BB52AA45002EA70BAACE08DED243B9D8CBD62A68ADE265832B56564E43A6FA42ED199A099769742DF1539E8255",
+
+ /* 1025-bit key */
+ "01947C7FCE90425F47279E70851F25D5E62316FE8A1DF19371E3E628E260543E4901EF6081F68C0B8141190D2AE8DABA7D1250EC6DB636E944EC3722877C7C1D0A67F14B1694C5F0379451A43E49A32DDE83670B73DA91A1C99BC23B436A60055C610F0BAF99C1A079565B95A3F1526632D1D4DA60F20EDA25E653C4F002766F45",
+ "010001",
+ "0159DBDE04A33EF06FB608B80B190F4D3E22BCC13AC8E4A081033ABFA416EDB0B338AA08B57309EA5A5240E7DC6E54378C69414C31D97DDB1F406DB3769CC41A43",
+ "012B652F30403B38B40995FD6FF41A1ACC8ADA70373236B7202D39B2EE30CFB46DB09511F6F307CC61CC21606C18A75B8A62F822DF031BA0DF0DAFD5506F568BD7",
+ "436EF508DE736519C2DA4C580D98C82CB7452A3FB5EFADC3B9C7789A1BC6584F795ADDBBD32439C74686552ECB6C2C307A4D3AF7F539EEC157248C7B31F1A255",
+ "012B15A89F3DFB2B39073E73F02BDD0C1A7B379DD435F05CDDE2EFF9E462948B7CEC62EE9050D5E0816E0785A856B49108DCB75F3683874D1CA6329A19013066FF",
+ "0270DB17D5914B018D76118B24389A7350EC836B0063A21721236FD8EDB6D89B51E7EEB87B611B7132CB7EA7356C23151C1E7751507C786D9EE1794170A8C8E8",
+
+ "8FF00CAA605C702830634D9A6C3D42C652B58CF1D92FEC570BEEE7",
+ "8C407B5EC2899E5099C53E8CE793BF94E71B1782",
+ "0181AF8922B9FCB4D79D92EBE19815992FC0C1439D8BCD491398A0F4AD3A329A5BD9385560DB532683C8B7DA04E4B12AED6AACDF471C34C9CDA891ADDCC2DF3456653AA6382E9AE59B54455257EB099D562BBE10453F2B6D13C59C02E10F1F8ABB5DA0D0570932DACF2D0901DB729D0FEFCC054E70968EA540C81B04BCAEFE720E",
+
+ "2D",
+ "B600CF3C2E506D7F16778C910D3A8B003EEE61D5",
+ "018759FF1DF63B2792410562314416A8AEAF2AC634B46F940AB82D64DBF165EEE33011DA749D4BAB6E2FCD18129C9E49277D8453112B429A222A8471B070993998E758861C4D3F6D749D91C4290D332C7A4AB3F7EA35FF3A07D497C955FF0FFC95006B62C6D296810D9BFAB024196C7934012C2DF978EF299ABA239940CBA10245",
+
+ "74FC88C51BC90F77AF9D5E9A4A70133D4B4E0B34DA3C37C7EF8E",
+ "A73768AEEAA91F9D8C1ED6F9D2B63467F07CCAE3",
+ "018802BAB04C60325E81C4962311F2BE7C2ADCE93041A00719C88F957575F2C79F1B7BC8CED115C706B311C08A2D986CA3B6A9336B147C29C6F229409DDEC651BD1FDD5A0B7F610C9937FDB4A3A762364B8B3206B4EA485FD098D08F63D4AA8BB2697D027B750C32D7F74EAF5180D2E9B66B17CB2FA55523BC280DA10D14BE2053",
+
+ "A7EB2A5036931D27D4E891326D99692FFADDA9BF7EFD3E34E622C4ADC085F721DFE885072C78A203B151739BE540FA8C153A10F00A",
+ "9A7B3B0E708BD96F8190ECAB4FB9B2B3805A8156",
+ "00A4578CBC176318A638FBA7D01DF15746AF44D4F6CD96D7E7C495CBF425B09C649D32BF886DA48FBAF989A2117187CAFB1FB580317690E3CCD446920B7AF82B31DB5804D87D01514ACBFA9156E782F867F6BED9449E0E9A2C09BCECC6AA087636965E34B3EC766F2FE2E43018A2FDDEB140616A0E9D82E5331024EE0652FC7641",
+
+ "2EF2B066F854C33F3BDCBB5994A435E73D6C6C",
+ "EB3CEBBC4ADC16BB48E88C8AEC0E34AF7F427FD3",
+ "00EBC5F5FDA77CFDAD3C83641A9025E77D72D8A6FB33A810F5950F8D74C73E8D931E8634D86AB1246256AE07B6005B71B7F2FB98351218331CE69B8FFBDC9DA08BBC9C704F876DEB9DF9FC2EC065CAD87F9090B07ACC17AA7F997B27ACA48806E897F771D95141FE4526D8A5301B678627EFAB707FD40FBEBD6E792A25613E7AEC",
+
+ "8A7FB344C8B6CB2CF2EF1F643F9A3218F6E19BBA89C0",
+ "4C45CF4D57C98E3D6D2095ADC51C489EB50DFF84",
+ "010839EC20C27B9052E55BEFB9B77E6FC26E9075D7A54378C646ABDF51E445BD5715DE81789F56F1803D9170764A9E93CB78798694023EE7393CE04BC5D8F8C5A52C171D43837E3ACA62F609EB0AA5FFB0960EF04198DD754F57F7FBE6ABF765CF118B4CA443B23B5AAB266F952326AC4581100644325F8B721ACD5D04FF14EF3A",
+
+ /* 2048-bit key */
+ "AE45ED5601CEC6B8CC05F803935C674DDBE0D75C4C09FD7951FC6B0CAEC313A8DF39970C518BFFBA5ED68F3F0D7F22A4029D413F1AE07E4EBE9E4177CE23E7F5404B569E4EE1BDCF3C1FB03EF113802D4F855EB9B5134B5A7C8085ADCAE6FA2FA1417EC3763BE171B0C62B760EDE23C12AD92B980884C641F5A8FAC26BDAD4A03381A22FE1B754885094C82506D4019A535A286AFEB271BB9BA592DE18DCF600C2AEEAE56E02F7CF79FC14CF3BDC7CD84FEBBBF950CA90304B2219A7AA063AEFA2C3C1980E560CD64AFE779585B6107657B957857EFDE6010988AB7DE417FC88D8F384C4E6E72C3F943E0C31C0C4A5CC36F879D8A3AC9D7D59860EAADA6B83BB",
+ "010001",
+ "ECF5AECD1E5515FFFACBD75A2816C6EBF49018CDFB4638E185D66A7396B6F8090F8018C7FD95CC34B857DC17F0CC6516BB1346AB4D582CADAD7B4103352387B70338D084047C9D9539B6496204B3DD6EA442499207BEC01F964287FF6336C3984658336846F56E46861881C10233D2176BF15A5E96DDC780BC868AA77D3CE769",
+ "BC46C464FC6AC4CA783B0EB08A3C841B772F7E9B2F28BABD588AE885E1A0C61E4858A0FB25AC299990F35BE85164C259BA1175CDD7192707135184992B6C29B746DD0D2CABE142835F7D148CC161524B4A09946D48B828473F1CE76B6CB6886C345C03E05F41D51B5C3A90A3F24073C7D74A4FE25D9CF21C75960F3FC3863183",
+ "C73564571D00FB15D08A3DE9957A50915D7126E9442DACF42BC82E862E5673FF6A008ED4D2E374617DF89F17A160B43B7FDA9CB6B6B74218609815F7D45CA263C159AA32D272D127FAF4BC8CA2D77378E8AEB19B0AD7DA3CB3DE0AE7314980F62B6D4B0A875D1DF03C1BAE39CCD833EF6CD7E2D9528BF084D1F969E794E9F6C1",
+ "2658B37F6DF9C1030BE1DB68117FA9D87E39EA2B693B7E6D3A2F70947413EEC6142E18FB8DFCB6AC545D7C86A0AD48F8457170F0EFB26BC48126C53EFD1D16920198DC2A1107DC282DB6A80CD3062360BA3FA13F70E4312FF1A6CD6B8FC4CD9C5C3DB17C6D6A57212F73AE29F619327BAD59B153858585BA4E28B60A62A45E49",
+ "6F38526B3925085534EF3E415A836EDE8B86158A2C7CBFECCB0BD834304FEC683BA8D4F479C433D43416E63269623CEA100776D85AFF401D3FFF610EE65411CE3B1363D63A9709EEDE42647CEA561493D54570A879C18682CD97710B96205EC31117D73B5F36223FADD6E8BA90DD7C0EE61D44E163251E20C7F66EB305117CB8",
+
+ "8BBA6BF82A6C0F86D5F1756E97956870B08953B06B4EB205BC1694EE",
+ "47E1AB7119FEE56C95EE5EAAD86F40D0AA63BD33",
+ "53EA5DC08CD260FB3B858567287FA91552C30B2FEBFBA213F0AE87702D068D19BAB07FE574523DFB42139D68C3C5AFEEE0BFE4CB7969CBF382B804D6E61396144E2D0E60741F8993C3014B58B9B1957A8BABCD23AF854F4C356FB1662AA72BFCC7E586559DC4280D160C126785A723EBEEBEFF71F11594440AAEF87D10793A8774A239D4A04C87FE1467B9DAF85208EC6C7255794A96CC29142F9A8BD418E3C1FD67344B0CD0829DF3B2BEC60253196293C6B34D3F75D32F213DD45C6273D505ADF4CCED1057CB758FC26AEEFA441255ED4E64C199EE075E7F16646182FDB464739B68AB5DAFF0E63E9552016824F054BF4D3C8C90A97BB6B6553284EB429FCC",
+
+ "E6AD181F053B58A904F2457510373E57",
+ "6D17F5B4C1FFAC351D195BF7B09D09F09A4079CF",
+ "A2B1A430A9D657E2FA1C2BB5ED43FFB25C05A308FE9093C01031795F5874400110828AE58FB9B581CE9DDDD3E549AE04A0985459BDE6C626594E7B05DC4278B2A1465C1368408823C85E96DC66C3A30983C639664FC4569A37FE21E5A195B5776EED2DF8D8D361AF686E750229BBD663F161868A50615E0C337BEC0CA35FEC0BB19C36EB2E0BBCC0582FA1D93AACDB061063F59F2CE1EE43605E5D89ECA183D2ACDFE9F81011022AD3B43A3DD417DAC94B4E11EA81B192966E966B182082E71964607B4F8002F36299844A11F2AE0FAEAC2EAE70F8F4F98088ACDCD0AC556E9FCCC511521908FAD26F04C64201450305778758B0538BF8B5BB144A828E629795",
+
+ "510A2CF60E866FA2340553C94EA39FBC256311E83E94454B4124",
+ "385387514DECCC7C740DD8CDF9DAEE49A1CBFD54",
+ "9886C3E6764A8B9A84E84148EBD8C3B1AA8050381A78F668714C16D9CFD2A6EDC56979C535D9DEE3B44B85C18BE8928992371711472216D95DDA98D2EE8347C9B14DFFDFF84AA48D25AC06F7D7E65398AC967B1CE90925F67DCE049B7F812DB0742997A74D44FE81DBE0E7A3FEAF2E5C40AF888D550DDBBE3BC20657A29543F8FC2913B9BD1A61B2AB2256EC409BBD7DC0D17717EA25C43F42ED27DF8738BF4AFC6766FF7AFF0859555EE283920F4C8A63C4A7340CBAFDDC339ECDB4B0515002F96C932B5B79167AF699C0AD3FCCFDF0F44E85A70262BF2E18FE34B850589975E867FF969D48EABF212271546CDC05A69ECB526E52870C836F307BD798780EDE",
+
+ "BCDD190DA3B7D300DF9A06E22CAAE2A75F10C91FF667B7C16BDE8B53064A2649A94045C9",
+ "5CACA6A0F764161A9684F85D92B6E0EF37CA8B65",
+ "6318E9FB5C0D05E5307E1683436E903293AC4642358AAA223D7163013ABA87E2DFDA8E60C6860E29A1E92686163EA0B9175F329CA3B131A1EDD3A77759A8B97BAD6A4F8F4396F28CF6F39CA58112E48160D6E203DAA5856F3ACA5FFED577AF499408E3DFD233E3E604DBE34A9C4C9082DE65527CAC6331D29DC80E0508A0FA7122E7F329F6CCA5CFA34D4D1DA417805457E008BEC549E478FF9E12A763C477D15BBB78F5B69BD57830FC2C4ED686D79BC72A95D85F88134C6B0AFE56A8CCFBC855828BB339BD17909CF1D70DE3335AE07039093E606D655365DE6550B872CD6DE1D440EE031B61945F629AD8A353B0D40939E96A3C450D2A8D5EEE9F678093C8",
+
+ "A7DD6C7DC24B46F9DD5F1E91ADA4C3B3DF947E877232A9",
+ "95BCA9E3859894B3DD869FA7ECD5BBC6401BF3E4",
+ "75290872CCFD4A4505660D651F56DA6DAA09CA1301D890632F6A992F3D565CEE464AFDED40ED3B5BE9356714EA5AA7655F4A1366C2F17C728F6F2C5A5D1F8E28429BC4E6F8F2CFF8DA8DC0E0A9808E45FD09EA2FA40CB2B6CE6FFFF5C0E159D11B68D90A85F7B84E103B09E682666480C657505C0929259468A314786D74EAB131573CF234BF57DB7D9E66CC6748192E002DC0DEEA930585F0831FDCD9BC33D51F79ED2FFC16BCF4D59812FCEBCAA3F9069B0E445686D644C25CCF63B456EE5FA6FFE96F19CDF751FED9EAF35957754DBF4BFEA5216AA1844DC507CB2D080E722EBA150308C2B5FF1193620F1766ECF4481BAFB943BD292877F2136CA494ABA0",
+
+ "EAF1A73A1B0C4609537DE69CD9228BBCFB9A8CA8C6C3EFAF056FE4A7F4634ED00B7C39EC6922D7B8EA2C04EBAC",
+ "9F47DDF42E97EEA856A9BDBC714EB3AC22F6EB32",
+ "2D207A73432A8FB4C03051B3F73B28A61764098DFA34C47A20995F8115AA6816679B557E82DBEE584908C6E69782D7DEB34DBD65AF063D57FCA76A5FD069492FD6068D9984D209350565A62E5C77F23038C12CB10C6634709B547C46F6B4A709BD85CA122D74465EF97762C29763E06DBC7A9E738C78BFCA0102DC5E79D65B973F28240CAAB2E161A78B57D262457ED8195D53E3C7AE9DA021883C6DB7C24AFDD2322EAC972AD3C354C5FCEF1E146C3A0290FB67ADF007066E00428D2CEC18CE58F9328698DEFEF4B2EB5EC76918FDE1C198CBB38B7AFC67626A9AEFEC4322BFD90D2563481C9A221F78C8272C82D1B62AB914E1C69F6AF6EF30CA5260DB4A46",
+
+ NULL
+};
+
+static void
+test_RSA_OAEP(const char *name,
+ br_rsa_oaep_encrypt menc, br_rsa_oaep_decrypt mdec)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ u = 0;
+ while (KAT_RSA_OAEP[u] != NULL) {
+ unsigned char n[512];
+ unsigned char e[8];
+ unsigned char p[256];
+ unsigned char q[256];
+ unsigned char dp[256];
+ unsigned char dq[256];
+ unsigned char iq[256];
+ br_rsa_public_key pk;
+ br_rsa_private_key sk;
+ size_t v;
+
+ pk.n = n;
+ pk.nlen = hextobin(n, KAT_RSA_OAEP[u ++]);
+ pk.e = e;
+ pk.elen = hextobin(e, KAT_RSA_OAEP[u ++]);
+
+ for (v = 0; n[v] == 0; v ++);
+ sk.n_bitlen = BIT_LENGTH(n[v]) + ((pk.nlen - 1 - v) << 3);
+ sk.p = p;
+ sk.plen = hextobin(p, KAT_RSA_OAEP[u ++]);
+ sk.q = q;
+ sk.qlen = hextobin(q, KAT_RSA_OAEP[u ++]);
+ sk.dp = dp;
+ sk.dplen = hextobin(dp, KAT_RSA_OAEP[u ++]);
+ sk.dq = dq;
+ sk.dqlen = hextobin(dq, KAT_RSA_OAEP[u ++]);
+ sk.iq = iq;
+ sk.iqlen = hextobin(iq, KAT_RSA_OAEP[u ++]);
+
+ for (v = 0; v < 6; v ++) {
+ unsigned char plain[512], seed[128], cipher[512];
+ size_t plain_len, seed_len, cipher_len;
+ rng_fake_ctx rng;
+ unsigned char tmp[513];
+ size_t len;
+
+ plain_len = hextobin(plain, KAT_RSA_OAEP[u ++]);
+ seed_len = hextobin(seed, KAT_RSA_OAEP[u ++]);
+ cipher_len = hextobin(cipher, KAT_RSA_OAEP[u ++]);
+ rng_fake_init(&rng, NULL, seed, seed_len);
+
+ len = menc(&rng.vtable, &br_sha1_vtable, NULL, 0, &pk,
+ tmp, sizeof tmp, plain, plain_len);
+ if (len != cipher_len) {
+ fprintf(stderr,
+ "wrong encrypted length: %lu vs %lu\n",
+ (unsigned long)len,
+ (unsigned long)cipher_len);
+ }
+ if (rng.ptr != rng.len) {
+ fprintf(stderr, "seed not fully consumed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA/OAEP encrypt", tmp, cipher, len);
+
+ if (mdec(&br_sha1_vtable, NULL, 0,
+ &sk, tmp, &len) != 1)
+ {
+ fprintf(stderr, "decryption failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (len != plain_len) {
+ fprintf(stderr,
+ "wrong decrypted length: %lu vs %lu\n",
+ (unsigned long)len,
+ (unsigned long)plain_len);
+ }
+ check_equals("KAT RSA/OAEP decrypt", tmp, plain, len);
+
+ /*
+ * Try with a different label; it should fail.
+ */
+ memcpy(tmp, cipher, cipher_len);
+ len = cipher_len;
+ if (mdec(&br_sha1_vtable, "T", 1,
+ &sk, tmp, &len) != 0)
+ {
+ fprintf(stderr, "decryption should have failed"
+ " (wrong label)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Try with a the wrong length; it should fail.
+ */
+ tmp[0] = 0x00;
+ memcpy(tmp + 1, cipher, cipher_len);
+ len = cipher_len + 1;
+ if (mdec(&br_sha1_vtable, "T", 1,
+ &sk, tmp, &len) != 0)
+ {
+ fprintf(stderr, "decryption should have failed"
+ " (wrong length)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_RSA_keygen(const char *name, br_rsa_keygen kg, br_rsa_compute_modulus cm,
+ br_rsa_compute_pubexp ce, br_rsa_compute_privexp cd,
+ br_rsa_public pub, br_rsa_pkcs1_sign sign, br_rsa_pkcs1_vrfy vrfy)
+{
+ br_hmac_drbg_context rng;
+ int i;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, "seed for RSA keygen", 19);
+
+ for (i = 0; i <= 42; i ++) {
+ unsigned size;
+ uint32_t pubexp, z;
+ br_rsa_private_key sk;
+ br_rsa_public_key pk, pk2;
+ unsigned char kbuf_priv[BR_RSA_KBUF_PRIV_SIZE(2048)];
+ unsigned char kbuf_pub[BR_RSA_KBUF_PUB_SIZE(2048)];
+ unsigned char n2[256], d[256], msg1[256], msg2[256];
+ uint32_t mod[256];
+ uint32_t cc;
+ size_t u, v;
+ unsigned char sig[257], hv[32], hv2[32];
+ unsigned mask1, mask2;
+ int j;
+
+ if (i <= 35) {
+ size = 1024 + i;
+ pubexp = 17;
+ } else if (i <= 40) {
+ size = 2048;
+ pubexp = (i << 1) - 69;
+ } else {
+ size = 2048;
+ pubexp = 0xFFFFFFFF;
+ }
+
+ if (!kg(&rng.vtable,
+ &sk, kbuf_priv, &pk, kbuf_pub, size, pubexp))
+ {
+ fprintf(stderr, "RSA key pair generation failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ z = pubexp;
+ for (u = pk.elen; u > 0; u --) {
+ if (pk.e[u - 1] != (z & 0xFF)) {
+ fprintf(stderr, "wrong public exponent\n");
+ exit(EXIT_FAILURE);
+ }
+ z >>= 8;
+ }
+ if (z != 0) {
+ fprintf(stderr, "truncated public exponent\n");
+ exit(EXIT_FAILURE);
+ }
+
+ memset(mod, 0, sizeof mod);
+ for (u = 0; u < sk.plen; u ++) {
+ for (v = 0; v < sk.qlen; v ++) {
+ mod[u + v] += (uint32_t)sk.p[sk.plen - 1 - u]
+ * (uint32_t)sk.q[sk.qlen - 1 - v];
+ }
+ }
+ cc = 0;
+ for (u = 0; u < sk.plen + sk.qlen; u ++) {
+ mod[u] += cc;
+ cc = mod[u] >> 8;
+ mod[u] &= 0xFF;
+ }
+ for (u = 0; u < pk.nlen; u ++) {
+ if (mod[pk.nlen - 1 - u] != pk.n[u]) {
+ fprintf(stderr, "wrong modulus\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (sk.n_bitlen != size) {
+ fprintf(stderr, "wrong key size\n");
+ exit(EXIT_FAILURE);
+ }
+ if (pk.nlen != (size + 7) >> 3) {
+ fprintf(stderr, "wrong modulus size (bytes)\n");
+ exit(EXIT_FAILURE);
+ }
+ mask1 = 0x01 << ((size + 7) & 7);
+ mask2 = 0xFF & -mask1;
+ if ((pk.n[0] & mask2) != mask1) {
+ fprintf(stderr, "wrong modulus size (bits)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (cm(NULL, &sk) != pk.nlen) {
+ fprintf(stderr, "wrong recomputed modulus length\n");
+ exit(EXIT_FAILURE);
+ }
+ if (cm(n2, &sk) != pk.nlen || memcmp(pk.n, n2, pk.nlen) != 0) {
+ fprintf(stderr, "wrong recomputed modulus value\n");
+ exit(EXIT_FAILURE);
+ }
+
+ z = ce(&sk);
+ if (z != pubexp) {
+ fprintf(stderr,
+ "wrong recomputed pubexp: %lu (exp: %lu)\n",
+ (unsigned long)z, (unsigned long)pubexp);
+ exit(EXIT_FAILURE);
+ }
+
+ if (cd(NULL, &sk, pubexp) != pk.nlen) {
+ fprintf(stderr,
+ "wrong recomputed privexp length (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ if (cd(d, &sk, pubexp) != pk.nlen) {
+ fprintf(stderr,
+ "wrong recomputed privexp length (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ /*
+ * To check that the private exponent is correct, we make
+ * it into a _public_ key, and use the public-key operation
+ * to perform the modular exponentiation.
+ */
+ pk2 = pk;
+ pk2.e = d;
+ pk2.elen = pk.nlen;
+ rng.vtable->generate(&rng.vtable, msg1, pk.nlen);
+ msg1[0] = 0x00;
+ memcpy(msg2, msg1, pk.nlen);
+ if (!pub(msg2, pk.nlen, &pk2) || !pub(msg2, pk.nlen, &pk)) {
+ fprintf(stderr, "public-key operation error\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(msg1, msg2, pk.nlen) != 0) {
+ fprintf(stderr, "wrong recomputed privexp\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * We test the RSA operation over a some random messages.
+ */
+ for (j = 0; j < 20; j ++) {
+ rng.vtable->generate(&rng.vtable, hv, sizeof hv);
+ memset(sig, 0, sizeof sig);
+ sig[pk.nlen] = 0x00;
+ if (!sign(BR_HASH_OID_SHA256,
+ hv, sizeof hv, &sk, sig))
+ {
+ fprintf(stderr,
+ "signature error (%d)\n", j);
+ exit(EXIT_FAILURE);
+ }
+ if (sig[pk.nlen] != 0x00) {
+ fprintf(stderr,
+ "signature length error (%d)\n", j);
+ exit(EXIT_FAILURE);
+ }
+ if (!vrfy(sig, pk.nlen, BR_HASH_OID_SHA256, sizeof hv,
+ &pk, hv2))
+ {
+ fprintf(stderr,
+ "signature verif error (%d)\n", j);
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(hv, hv2, sizeof hv) != 0) {
+ fprintf(stderr,
+ "signature extract error (%d)\n", j);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_RSA_i15(void)
+{
+ test_RSA_core("RSA i15 core", &br_rsa_i15_public, &br_rsa_i15_private);
+ test_RSA_sign("RSA i15 sign", &br_rsa_i15_private,
+ &br_rsa_i15_pkcs1_sign, &br_rsa_i15_pkcs1_vrfy);
+ test_RSA_OAEP("RSA i15 OAEP",
+ &br_rsa_i15_oaep_encrypt, &br_rsa_i15_oaep_decrypt);
+ test_RSA_PSS("RSA i15 PSS",
+ &br_rsa_i15_pss_sign, &br_rsa_i15_pss_vrfy);
+ test_RSA_keygen("RSA i15 keygen", &br_rsa_i15_keygen,
+ &br_rsa_i15_compute_modulus, &br_rsa_i15_compute_pubexp,
+ &br_rsa_i15_compute_privexp, &br_rsa_i15_public,
+ &br_rsa_i15_pkcs1_sign, &br_rsa_i15_pkcs1_vrfy);
+}
+
+static void
+test_RSA_i31(void)
+{
+ test_RSA_core("RSA i31 core", &br_rsa_i31_public, &br_rsa_i31_private);
+ test_RSA_sign("RSA i31 sign", &br_rsa_i31_private,
+ &br_rsa_i31_pkcs1_sign, &br_rsa_i31_pkcs1_vrfy);
+ test_RSA_OAEP("RSA i31 OAEP",
+ &br_rsa_i31_oaep_encrypt, &br_rsa_i31_oaep_decrypt);
+ test_RSA_PSS("RSA i31 PSS",
+ &br_rsa_i31_pss_sign, &br_rsa_i31_pss_vrfy);
+ test_RSA_keygen("RSA i31 keygen", &br_rsa_i31_keygen,
+ &br_rsa_i31_compute_modulus, &br_rsa_i31_compute_pubexp,
+ &br_rsa_i31_compute_privexp, &br_rsa_i31_public,
+ &br_rsa_i31_pkcs1_sign, &br_rsa_i31_pkcs1_vrfy);
+}
+
+static void
+test_RSA_i32(void)
+{
+ test_RSA_core("RSA i32 core", &br_rsa_i32_public, &br_rsa_i32_private);
+ test_RSA_sign("RSA i32 sign", &br_rsa_i32_private,
+ &br_rsa_i32_pkcs1_sign, &br_rsa_i32_pkcs1_vrfy);
+ test_RSA_OAEP("RSA i32 OAEP",
+ &br_rsa_i32_oaep_encrypt, &br_rsa_i32_oaep_decrypt);
+ test_RSA_PSS("RSA i32 PSS",
+ &br_rsa_i32_pss_sign, &br_rsa_i32_pss_vrfy);
+}
+
+static void
+test_RSA_i62(void)
+{
+ br_rsa_public pub;
+ br_rsa_private priv;
+ br_rsa_pkcs1_sign sign;
+ br_rsa_pkcs1_vrfy vrfy;
+ br_rsa_pss_sign pss_sign;
+ br_rsa_pss_vrfy pss_vrfy;
+ br_rsa_oaep_encrypt menc;
+ br_rsa_oaep_decrypt mdec;
+ br_rsa_keygen kgen;
+
+ pub = br_rsa_i62_public_get();
+ priv = br_rsa_i62_private_get();
+ sign = br_rsa_i62_pkcs1_sign_get();
+ vrfy = br_rsa_i62_pkcs1_vrfy_get();
+ pss_sign = br_rsa_i62_pss_sign_get();
+ pss_vrfy = br_rsa_i62_pss_vrfy_get();
+ menc = br_rsa_i62_oaep_encrypt_get();
+ mdec = br_rsa_i62_oaep_decrypt_get();
+ kgen = br_rsa_i62_keygen_get();
+ if (pub) {
+ if (!priv || !sign || !vrfy || !pss_sign || !pss_vrfy
+ || !menc || !mdec || !kgen)
+ {
+ fprintf(stderr, "Inconsistent i62 availability\n");
+ exit(EXIT_FAILURE);
+ }
+ test_RSA_core("RSA i62 core", pub, priv);
+ test_RSA_sign("RSA i62 sign", priv, sign, vrfy);
+ test_RSA_OAEP("RSA i62 OAEP", menc, mdec);
+ test_RSA_PSS("RSA i62 PSS", pss_sign, pss_vrfy);
+ test_RSA_keygen("RSA i62 keygen", kgen,
+ &br_rsa_i31_compute_modulus, &br_rsa_i31_compute_pubexp,
+ &br_rsa_i31_compute_privexp, pub,
+ sign, vrfy);
+ } else {
+ if (priv || sign || vrfy || pss_sign || pss_vrfy
+ || menc || mdec || kgen)
+ {
+ fprintf(stderr, "Inconsistent i62 availability\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("Test RSA i62: UNAVAILABLE\n");
+ }
+}
+
+#if 0
+static void
+test_RSA_signatures(void)
+{
+ uint32_t n[40], e[2], p[20], q[20], dp[20], dq[20], iq[20], x[40];
+ unsigned char hv[20], sig[128];
+ unsigned char ref[128], tmp[128];
+ br_sha1_context hc;
+
+ printf("Test RSA signatures: ");
+ fflush(stdout);
+
+ /*
+ * Decode RSA key elements.
+ */
+ br_int_decode(n, sizeof n / sizeof n[0], RSA_N, sizeof RSA_N);
+ br_int_decode(e, sizeof e / sizeof e[0], RSA_E, sizeof RSA_E);
+ br_int_decode(p, sizeof p / sizeof p[0], RSA_P, sizeof RSA_P);
+ br_int_decode(q, sizeof q / sizeof q[0], RSA_Q, sizeof RSA_Q);
+ br_int_decode(dp, sizeof dp / sizeof dp[0], RSA_DP, sizeof RSA_DP);
+ br_int_decode(dq, sizeof dq / sizeof dq[0], RSA_DQ, sizeof RSA_DQ);
+ br_int_decode(iq, sizeof iq / sizeof iq[0], RSA_IQ, sizeof RSA_IQ);
+
+ /*
+ * Decode reference signature (computed with OpenSSL).
+ */
+ hextobin(ref, "45A3DC6A106BCD3BD0E48FB579643AA3FF801E5903E80AA9B43A695A8E7F454E93FA208B69995FF7A6D5617C2FEB8E546375A664977A48931842AAE796B5A0D64393DCA35F3490FC157F5BD83B9D58C2F7926E6AE648A2BD96CAB8FCCD3D35BB11424AD47D973FF6D69CA774841AEC45DFAE99CCF79893E7047FDE6CB00AA76D");
+
+ /*
+ * Recompute signature. Since PKCS#1 v1.5 signatures are
+ * deterministic, we should get the same as the reference signature.
+ */
+ br_sha1_init(&hc);
+ br_sha1_update(&hc, "test", 4);
+ br_sha1_out(&hc, hv);
+ if (!br_rsa_sign(sig, sizeof sig, p, q, dp, dq, iq, br_sha1_ID, hv)) {
+ fprintf(stderr, "RSA-1024/SHA-1 sig generate failed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT RSA-sign 1", sig, ref, sizeof sig);
+
+ /*
+ * Verify signature.
+ */
+ if (!br_rsa_verify(sig, sizeof sig, n, e, br_sha1_ID, hv)) {
+ fprintf(stderr, "RSA-1024/SHA-1 sig verify failed\n");
+ exit(EXIT_FAILURE);
+ }
+ hv[5] ^= 0x01;
+ if (br_rsa_verify(sig, sizeof sig, n, e, br_sha1_ID, hv)) {
+ fprintf(stderr, "RSA-1024/SHA-1 sig verify should have failed\n");
+ exit(EXIT_FAILURE);
+ }
+ hv[5] ^= 0x01;
+
+ /*
+ * Generate a signature with the alternate encoding (no NULL) and
+ * verify it.
+ */
+ hextobin(tmp, "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00301F300706052B0E03021A0414A94A8FE5CCB19BA61C4C0873D391E987982FBBD3");
+ br_int_decode(x, sizeof x / sizeof x[0], tmp, sizeof tmp);
+ x[0] = n[0];
+ br_rsa_private_core(x, p, q, dp, dq, iq);
+ br_int_encode(sig, sizeof sig, x);
+ if (!br_rsa_verify(sig, sizeof sig, n, e, br_sha1_ID, hv)) {
+ fprintf(stderr, "RSA-1024/SHA-1 sig verify (alt) failed\n");
+ exit(EXIT_FAILURE);
+ }
+ hv[5] ^= 0x01;
+ if (br_rsa_verify(sig, sizeof sig, n, e, br_sha1_ID, hv)) {
+ fprintf(stderr, "RSA-1024/SHA-1 sig verify (alt) should have failed\n");
+ exit(EXIT_FAILURE);
+ }
+ hv[5] ^= 0x01;
+
+ printf("done.\n");
+ fflush(stdout);
+}
+#endif
+
+/*
+ * From: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
+ */
+static const char *const KAT_GHASH[] = {
+
+ "66e94bd4ef8a2c3b884cfa59ca342b2e",
+ "",
+ "",
+ "00000000000000000000000000000000",
+
+ "66e94bd4ef8a2c3b884cfa59ca342b2e",
+ "",
+ "0388dace60b6a392f328c2b971b2fe78",
+ "f38cbb1ad69223dcc3457ae5b6b0f885",
+
+ "b83b533708bf535d0aa6e52980d53b78",
+ "",
+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
+ "7f1b32b81b820d02614f8895ac1d4eac",
+
+ "b83b533708bf535d0aa6e52980d53b78",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
+ "698e57f70e6ecc7fd9463b7260a9ae5f",
+
+ "b83b533708bf535d0aa6e52980d53b78",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
+ "df586bb4c249b92cb6922877e444d37b",
+
+ "b83b533708bf535d0aa6e52980d53b78",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
+ "1c5afe9760d3932f3c9a878aac3dc3de",
+
+ "aae06992acbf52a3e8f4a96ec9300bd7",
+ "",
+ "98e7247c07f0fe411c267e4384b0f600",
+ "e2c63f0ac44ad0e02efa05ab6743d4ce",
+
+ "466923ec9ae682214f2c082badb39249",
+ "",
+ "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
+ "51110d40f6c8fff0eb1ae33445a889f0",
+
+ "466923ec9ae682214f2c082badb39249",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
+ "ed2ce3062e4a8ec06db8b4c490e8a268",
+
+ "466923ec9ae682214f2c082badb39249",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
+ "1e6a133806607858ee80eaf237064089",
+
+ "466923ec9ae682214f2c082badb39249",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
+ "82567fb0b4cc371801eadec005968e94",
+
+ "dc95c078a2408989ad48a21492842087",
+ "",
+ "cea7403d4d606b6e074ec5d3baf39d18",
+ "83de425c5edc5d498f382c441041ca92",
+
+ "acbef20579b4b8ebce889bac8732dad7",
+ "",
+ "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
+ "4db870d37cb75fcb46097c36230d1612",
+
+ "acbef20579b4b8ebce889bac8732dad7",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
+ "8bd0c4d8aacd391e67cca447e8c38f65",
+
+ "acbef20579b4b8ebce889bac8732dad7",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
+ "75a34288b8c68f811c52b2e9a2f97f63",
+
+ "acbef20579b4b8ebce889bac8732dad7",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
+ "d5ffcf6fc5ac4d69722187421a7f170b",
+
+ NULL,
+};
+
+static void
+test_GHASH(const char *name, br_ghash gh)
+{
+ size_t u;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ for (u = 0; KAT_GHASH[u]; u += 4) {
+ unsigned char h[16];
+ unsigned char a[100];
+ size_t a_len;
+ unsigned char c[100];
+ size_t c_len;
+ unsigned char p[16];
+ unsigned char y[16];
+ unsigned char ref[16];
+
+ hextobin(h, KAT_GHASH[u]);
+ a_len = hextobin(a, KAT_GHASH[u + 1]);
+ c_len = hextobin(c, KAT_GHASH[u + 2]);
+ hextobin(ref, KAT_GHASH[u + 3]);
+ memset(y, 0, sizeof y);
+ gh(y, h, a, a_len);
+ gh(y, h, c, c_len);
+ memset(p, 0, sizeof p);
+ br_enc32be(p + 4, (uint32_t)a_len << 3);
+ br_enc32be(p + 12, (uint32_t)c_len << 3);
+ gh(y, h, p, sizeof p);
+ check_equals("KAT GHASH", y, ref, sizeof ref);
+ }
+
+ for (u = 0; u <= 1024; u ++) {
+ unsigned char key[32], iv[12];
+ unsigned char buf[1024 + 32];
+ unsigned char y0[16], y1[16];
+ char tmp[100];
+
+ memset(key, 0, sizeof key);
+ memset(iv, 0, sizeof iv);
+ br_enc32be(key, u);
+ memset(buf, 0, sizeof buf);
+ br_chacha20_ct_run(key, iv, 1, buf, sizeof buf);
+
+ memcpy(y0, buf, 16);
+ br_ghash_ctmul32(y0, buf + 16, buf + 32, u);
+ memcpy(y1, buf, 16);
+ gh(y1, buf + 16, buf + 32, u);
+ sprintf(tmp, "XREF %s (len = %u)", name, (unsigned)u);
+ check_equals(tmp, y0, y1, 16);
+
+ if ((u & 31) == 0) {
+ printf(".");
+ fflush(stdout);
+ }
+ }
+
+ printf("done.\n");
+ fflush(stdout);
+}
+
+static void
+test_GHASH_ctmul(void)
+{
+ test_GHASH("GHASH_ctmul", br_ghash_ctmul);
+}
+
+static void
+test_GHASH_ctmul32(void)
+{
+ test_GHASH("GHASH_ctmul32", br_ghash_ctmul32);
+}
+
+static void
+test_GHASH_ctmul64(void)
+{
+ test_GHASH("GHASH_ctmul64", br_ghash_ctmul64);
+}
+
+static void
+test_GHASH_pclmul(void)
+{
+ br_ghash gh;
+
+ gh = br_ghash_pclmul_get();
+ if (gh == 0) {
+ printf("Test GHASH_pclmul: UNAVAILABLE\n");
+ } else {
+ test_GHASH("GHASH_pclmul", gh);
+ }
+}
+
+static void
+test_GHASH_pwr8(void)
+{
+ br_ghash gh;
+
+ gh = br_ghash_pwr8_get();
+ if (gh == 0) {
+ printf("Test GHASH_pwr8: UNAVAILABLE\n");
+ } else {
+ test_GHASH("GHASH_pwr8", gh);
+ }
+}
+
+/*
+ * From: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
+ *
+ * Order: key, plaintext, AAD, IV, ciphertext, tag
+ */
+static const char *const KAT_GCM[] = {
+ "00000000000000000000000000000000",
+ "",
+ "",
+ "000000000000000000000000",
+ "",
+ "58e2fccefa7e3061367f1d57a4e7455a",
+
+ "00000000000000000000000000000000",
+ "00000000000000000000000000000000",
+ "",
+ "000000000000000000000000",
+ "0388dace60b6a392f328c2b971b2fe78",
+ "ab6e47d42cec13bdf53a67b21257bddf",
+
+ "feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
+ "",
+ "cafebabefacedbaddecaf888",
+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
+ "4d5c2af327cd64a62cf35abd2ba6fab4",
+
+ "feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbaddecaf888",
+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
+ "5bc94fbc3221a5db94fae95ae7121a47",
+
+ "feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbad",
+ "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
+ "3612d2e79e3b0785561be14aaca2fccb",
+
+ "feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+ "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
+ "619cc5aefffe0bfa462af43c1699d050",
+
+ "000000000000000000000000000000000000000000000000",
+ "",
+ "",
+ "000000000000000000000000",
+ "",
+ "cd33b28ac773f74ba00ed1f312572435",
+
+ "000000000000000000000000000000000000000000000000",
+ "00000000000000000000000000000000",
+ "",
+ "000000000000000000000000",
+ "98e7247c07f0fe411c267e4384b0f600",
+ "2ff58d80033927ab8ef4d4587514f0fb",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
+ "",
+ "cafebabefacedbaddecaf888",
+ "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
+ "9924a7c8587336bfb118024db8674a14",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbaddecaf888",
+ "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
+ "2519498e80f1478f37ba55bd6d27618c",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbad",
+ "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
+ "65dcc57fcf623a24094fcca40d3533f8",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+ "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
+ "dcf566ff291c25bbb8568fc3d376a6d9",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "",
+ "",
+ "000000000000000000000000",
+ "",
+ "530f8afbc74536b9a963b4f1c4cb738b",
+
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "00000000000000000000000000000000",
+ "",
+ "000000000000000000000000",
+ "cea7403d4d606b6e074ec5d3baf39d18",
+ "d0d1c8a799996bf0265b98b5d48ab919",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
+ "",
+ "cafebabefacedbaddecaf888",
+ "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
+ "b094dac5d93471bdec1a502270e3cc6c",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbaddecaf888",
+ "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
+ "76fc6ece0f4e1768cddf8853bb2d551b",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "cafebabefacedbad",
+ "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
+ "3a337dbf46a792c45e454913fe2ea8f2",
+
+ "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+ "feedfacedeadbeeffeedfacedeadbeefabaddad2",
+ "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+ "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
+ "a44a8266ee1c8eb0c8b5d4cf5ae9f19a",
+
+ NULL
+};
+
+static void
+test_GCM(void)
+{
+ size_t u;
+
+ printf("Test GCM: ");
+ fflush(stdout);
+
+ for (u = 0; KAT_GCM[u]; u += 6) {
+ unsigned char key[32];
+ unsigned char plain[100];
+ unsigned char aad[100];
+ unsigned char iv[100];
+ unsigned char cipher[100];
+ unsigned char tag[100];
+ size_t key_len, plain_len, aad_len, iv_len;
+ br_aes_ct_ctr_keys bc;
+ br_gcm_context gc;
+ unsigned char tmp[100], out[16];
+ size_t v, tag_len;
+
+ key_len = hextobin(key, KAT_GCM[u]);
+ plain_len = hextobin(plain, KAT_GCM[u + 1]);
+ aad_len = hextobin(aad, KAT_GCM[u + 2]);
+ iv_len = hextobin(iv, KAT_GCM[u + 3]);
+ hextobin(cipher, KAT_GCM[u + 4]);
+ hextobin(tag, KAT_GCM[u + 5]);
+
+ br_aes_ct_ctr_init(&bc, key, key_len);
+ br_gcm_init(&gc, &bc.vtable, br_ghash_ctmul32);
+
+ memset(tmp, 0x54, sizeof tmp);
+
+ /*
+ * Basic operation.
+ */
+ memcpy(tmp, plain, plain_len);
+ br_gcm_reset(&gc, iv, iv_len);
+ br_gcm_aad_inject(&gc, aad, aad_len);
+ br_gcm_flip(&gc);
+ br_gcm_run(&gc, 1, tmp, plain_len);
+ br_gcm_get_tag(&gc, out);
+ check_equals("KAT GCM 1", tmp, cipher, plain_len);
+ check_equals("KAT GCM 2", out, tag, 16);
+
+ br_gcm_reset(&gc, iv, iv_len);
+ br_gcm_aad_inject(&gc, aad, aad_len);
+ br_gcm_flip(&gc);
+ br_gcm_run(&gc, 0, tmp, plain_len);
+ check_equals("KAT GCM 3", tmp, plain, plain_len);
+ if (!br_gcm_check_tag(&gc, tag)) {
+ fprintf(stderr, "Tag not verified (1)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (v = plain_len; v < sizeof tmp; v ++) {
+ if (tmp[v] != 0x54) {
+ fprintf(stderr, "overflow on data\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Byte-by-byte injection.
+ */
+ br_gcm_reset(&gc, iv, iv_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_gcm_aad_inject(&gc, aad + v, 1);
+ }
+ br_gcm_flip(&gc);
+ for (v = 0; v < plain_len; v ++) {
+ br_gcm_run(&gc, 1, tmp + v, 1);
+ }
+ check_equals("KAT GCM 4", tmp, cipher, plain_len);
+ if (!br_gcm_check_tag(&gc, tag)) {
+ fprintf(stderr, "Tag not verified (2)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ br_gcm_reset(&gc, iv, iv_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_gcm_aad_inject(&gc, aad + v, 1);
+ }
+ br_gcm_flip(&gc);
+ for (v = 0; v < plain_len; v ++) {
+ br_gcm_run(&gc, 0, tmp + v, 1);
+ }
+ br_gcm_get_tag(&gc, out);
+ check_equals("KAT GCM 5", tmp, plain, plain_len);
+ check_equals("KAT GCM 6", out, tag, 16);
+
+ /*
+ * Check that alterations are detected.
+ */
+ for (v = 0; v < aad_len; v ++) {
+ memcpy(tmp, cipher, plain_len);
+ br_gcm_reset(&gc, iv, iv_len);
+ aad[v] ^= 0x04;
+ br_gcm_aad_inject(&gc, aad, aad_len);
+ aad[v] ^= 0x04;
+ br_gcm_flip(&gc);
+ br_gcm_run(&gc, 0, tmp, plain_len);
+ check_equals("KAT GCM 7", tmp, plain, plain_len);
+ if (br_gcm_check_tag(&gc, tag)) {
+ fprintf(stderr, "Tag should have changed\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Tag truncation.
+ */
+ for (tag_len = 1; tag_len <= 16; tag_len ++) {
+ memset(out, 0x54, sizeof out);
+ memcpy(tmp, plain, plain_len);
+ br_gcm_reset(&gc, iv, iv_len);
+ br_gcm_aad_inject(&gc, aad, aad_len);
+ br_gcm_flip(&gc);
+ br_gcm_run(&gc, 1, tmp, plain_len);
+ br_gcm_get_tag_trunc(&gc, out, tag_len);
+ check_equals("KAT GCM 8", out, tag, tag_len);
+ for (v = tag_len; v < sizeof out; v ++) {
+ if (out[v] != 0x54) {
+ fprintf(stderr, "overflow on tag\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ memcpy(tmp, plain, plain_len);
+ br_gcm_reset(&gc, iv, iv_len);
+ br_gcm_aad_inject(&gc, aad, aad_len);
+ br_gcm_flip(&gc);
+ br_gcm_run(&gc, 1, tmp, plain_len);
+ if (!br_gcm_check_tag_trunc(&gc, out, tag_len)) {
+ fprintf(stderr, "Tag not verified (3)\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+/*
+ * From "The EAX Mode of Operation (A Two-Pass Authenticated Encryption
+ * Scheme Optimized for Simplicity and Efficiency)" (Bellare, Rogaway,
+ * Wagner), presented at FSE 2004. Full article is available at:
+ * http://web.cs.ucdavis.edu/~rogaway/papers/eax.html
+ *
+ * EAX specification concatenates the authentication tag at the end of
+ * the ciphertext; in our API and the vectors below, the tag is separate.
+ *
+ * Order is: plaintext, key, nonce, header, ciphertext, tag.
+ */
+static const char *const KAT_EAX[] = {
+ "",
+ "233952dee4d5ed5f9b9c6d6ff80ff478",
+ "62ec67f9c3a4a407fcb2a8c49031a8b3",
+ "6bfb914fd07eae6b",
+ "",
+ "e037830e8389f27b025a2d6527e79d01",
+
+ "f7fb",
+ "91945d3f4dcbee0bf45ef52255f095a4",
+ "becaf043b0a23d843194ba972c66debd",
+ "fa3bfd4806eb53fa",
+ "19dd",
+ "5c4c9331049d0bdab0277408f67967e5",
+
+ "1a47cb4933",
+ "01f74ad64077f2e704c0f60ada3dd523",
+ "70c3db4f0d26368400a10ed05d2bff5e",
+ "234a3463c1264ac6",
+ "d851d5bae0",
+ "3a59f238a23e39199dc9266626c40f80",
+
+ "481c9e39b1",
+ "d07cf6cbb7f313bdde66b727afd3c5e8",
+ "8408dfff3c1a2b1292dc199e46b7d617",
+ "33cce2eabff5a79d",
+ "632a9d131a",
+ "d4c168a4225d8e1ff755939974a7bede",
+
+ "40d0c07da5e4",
+ "35b6d0580005bbc12b0587124557d2c2",
+ "fdb6b06676eedc5c61d74276e1f8e816",
+ "aeb96eaebe2970e9",
+ "071dfe16c675",
+ "cb0677e536f73afe6a14b74ee49844dd",
+
+ "4de3b35c3fc039245bd1fb7d",
+ "bd8e6e11475e60b268784c38c62feb22",
+ "6eac5c93072d8e8513f750935e46da1b",
+ "d4482d1ca78dce0f",
+ "835bb4f15d743e350e728414",
+ "abb8644fd6ccb86947c5e10590210a4f",
+
+ "8b0a79306c9ce7ed99dae4f87f8dd61636",
+ "7c77d6e813bed5ac98baa417477a2e7d",
+ "1a8c98dcd73d38393b2bf1569deefc19",
+ "65d2017990d62528",
+ "02083e3979da014812f59f11d52630da30",
+ "137327d10649b0aa6e1c181db617d7f2",
+
+ "1bda122bce8a8dbaf1877d962b8592dd2d56",
+ "5fff20cafab119ca2fc73549e20f5b0d",
+ "dde59b97d722156d4d9aff2bc7559826",
+ "54b9f04e6a09189a",
+ "2ec47b2c4954a489afc7ba4897edcdae8cc3",
+ "3b60450599bd02c96382902aef7f832a",
+
+ "6cf36720872b8513f6eab1a8a44438d5ef11",
+ "a4a4782bcffd3ec5e7ef6d8c34a56123",
+ "b781fcf2f75fa5a8de97a9ca48e522ec",
+ "899a175897561d7e",
+ "0de18fd0fdd91e7af19f1d8ee8733938b1e8",
+ "e7f6d2231618102fdb7fe55ff1991700",
+
+ "ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7",
+ "8395fcf1e95bebd697bd010bc766aac3",
+ "22e7add93cfc6393c57ec0b3c17d6b44",
+ "126735fcc320d25a",
+ "cb8920f87a6c75cff39627b56e3ed197c552d295a7",
+ "cfc46afc253b4652b1af3795b124ab6e",
+
+ NULL
+};
+
+static void
+test_EAX_inner(const char *name, const br_block_ctrcbc_class *vt)
+{
+ size_t u;
+
+ printf("Test EAX %s: ", name);
+ fflush(stdout);
+
+ for (u = 0; KAT_EAX[u]; u += 6) {
+ unsigned char plain[100];
+ unsigned char key[32];
+ unsigned char nonce[100];
+ unsigned char aad[100];
+ unsigned char cipher[100];
+ unsigned char tag[100];
+ size_t plain_len, key_len, nonce_len, aad_len;
+ br_aes_gen_ctrcbc_keys bc;
+ br_eax_context ec;
+ br_eax_state st;
+ unsigned char tmp[100], out[16];
+ size_t v, tag_len;
+
+ plain_len = hextobin(plain, KAT_EAX[u]);
+ key_len = hextobin(key, KAT_EAX[u + 1]);
+ nonce_len = hextobin(nonce, KAT_EAX[u + 2]);
+ aad_len = hextobin(aad, KAT_EAX[u + 3]);
+ hextobin(cipher, KAT_EAX[u + 4]);
+ hextobin(tag, KAT_EAX[u + 5]);
+
+ vt->init(&bc.vtable, key, key_len);
+ br_eax_init(&ec, &bc.vtable);
+
+ memset(tmp, 0x54, sizeof tmp);
+
+ /*
+ * Basic operation.
+ */
+ memcpy(tmp, plain, plain_len);
+ br_eax_reset(&ec, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 1, tmp, plain_len);
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 1", tmp, cipher, plain_len);
+ check_equals("KAT EAX 2", out, tag, 16);
+
+ br_eax_reset(&ec, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 0, tmp, plain_len);
+ check_equals("KAT EAX 3", tmp, plain, plain_len);
+ if (!br_eax_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag not verified (1)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (v = plain_len; v < sizeof tmp; v ++) {
+ if (tmp[v] != 0x54) {
+ fprintf(stderr, "overflow on data\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Byte-by-byte injection.
+ */
+ br_eax_reset(&ec, nonce, nonce_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_eax_aad_inject(&ec, aad + v, 1);
+ }
+ br_eax_flip(&ec);
+ for (v = 0; v < plain_len; v ++) {
+ br_eax_run(&ec, 1, tmp + v, 1);
+ }
+ check_equals("KAT EAX 4", tmp, cipher, plain_len);
+ if (!br_eax_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag not verified (2)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ br_eax_reset(&ec, nonce, nonce_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_eax_aad_inject(&ec, aad + v, 1);
+ }
+ br_eax_flip(&ec);
+ for (v = 0; v < plain_len; v ++) {
+ br_eax_run(&ec, 0, tmp + v, 1);
+ }
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 5", tmp, plain, plain_len);
+ check_equals("KAT EAX 6", out, tag, 16);
+
+ /*
+ * Check that alterations are detected.
+ */
+ for (v = 0; v < aad_len; v ++) {
+ memcpy(tmp, cipher, plain_len);
+ br_eax_reset(&ec, nonce, nonce_len);
+ aad[v] ^= 0x04;
+ br_eax_aad_inject(&ec, aad, aad_len);
+ aad[v] ^= 0x04;
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 0, tmp, plain_len);
+ check_equals("KAT EAX 7", tmp, plain, plain_len);
+ if (br_eax_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag should have changed\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Tag truncation.
+ */
+ for (tag_len = 1; tag_len <= 16; tag_len ++) {
+ memset(out, 0x54, sizeof out);
+ memcpy(tmp, plain, plain_len);
+ br_eax_reset(&ec, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 1, tmp, plain_len);
+ br_eax_get_tag_trunc(&ec, out, tag_len);
+ check_equals("KAT EAX 8", out, tag, tag_len);
+ for (v = tag_len; v < sizeof out; v ++) {
+ if (out[v] != 0x54) {
+ fprintf(stderr, "overflow on tag\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ memcpy(tmp, plain, plain_len);
+ br_eax_reset(&ec, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 1, tmp, plain_len);
+ if (!br_eax_check_tag_trunc(&ec, out, tag_len)) {
+ fprintf(stderr, "Tag not verified (3)\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+
+ /*
+ * For capture tests, we need the message to be non-empty.
+ */
+ if (plain_len == 0) {
+ continue;
+ }
+
+ /*
+ * Captured state, pre-AAD. This requires the AAD and the
+ * message to be non-empty.
+ */
+ br_eax_capture(&ec, &st);
+
+ if (aad_len > 0) {
+ br_eax_reset_pre_aad(&ec, &st, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ memcpy(tmp, plain, plain_len);
+ br_eax_run(&ec, 1, tmp, plain_len);
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 9", tmp, cipher, plain_len);
+ check_equals("KAT EAX 10", out, tag, 16);
+
+ br_eax_reset_pre_aad(&ec, &st, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 0, tmp, plain_len);
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 11", tmp, plain, plain_len);
+ check_equals("KAT EAX 12", out, tag, 16);
+ }
+
+ /*
+ * Captured state, post-AAD. This requires the message to
+ * be non-empty.
+ */
+ br_eax_reset(&ec, nonce, nonce_len);
+ br_eax_aad_inject(&ec, aad, aad_len);
+ br_eax_flip(&ec);
+ br_eax_get_aad_mac(&ec, &st);
+
+ br_eax_reset_post_aad(&ec, &st, nonce, nonce_len);
+ memcpy(tmp, plain, plain_len);
+ br_eax_run(&ec, 1, tmp, plain_len);
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 13", tmp, cipher, plain_len);
+ check_equals("KAT EAX 14", out, tag, 16);
+
+ br_eax_reset_post_aad(&ec, &st, nonce, nonce_len);
+ br_eax_run(&ec, 0, tmp, plain_len);
+ br_eax_get_tag(&ec, out);
+ check_equals("KAT EAX 15", tmp, plain, plain_len);
+ check_equals("KAT EAX 16", out, tag, 16);
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_EAX(void)
+{
+ const br_block_ctrcbc_class *x_ctrcbc;
+
+ test_EAX_inner("aes_big", &br_aes_big_ctrcbc_vtable);
+ test_EAX_inner("aes_small", &br_aes_small_ctrcbc_vtable);
+ test_EAX_inner("aes_ct", &br_aes_ct_ctrcbc_vtable);
+ test_EAX_inner("aes_ct64", &br_aes_ct64_ctrcbc_vtable);
+
+ x_ctrcbc = br_aes_x86ni_ctrcbc_get_vtable();
+ if (x_ctrcbc != NULL) {
+ test_EAX_inner("aes_x86ni", x_ctrcbc);
+ } else {
+ printf("Test EAX aes_x86ni: UNAVAILABLE\n");
+ }
+
+ x_ctrcbc = br_aes_pwr8_ctrcbc_get_vtable();
+ if (x_ctrcbc != NULL) {
+ test_EAX_inner("aes_pwr8", x_ctrcbc);
+ } else {
+ printf("Test EAX aes_pwr8: UNAVAILABLE\n");
+ }
+}
+
+/*
+ * From NIST SP 800-38C, appendix C.
+ *
+ * CCM specification concatenates the authentication tag at the end of
+ * the ciphertext; in our API and the vectors below, the tag is separate.
+ *
+ * Order is: key, nonce, aad, plaintext, ciphertext, tag.
+ */
+static const char *const KAT_CCM[] = {
+ "404142434445464748494a4b4c4d4e4f",
+ "10111213141516",
+ "0001020304050607",
+ "20212223",
+ "7162015b",
+ "4dac255d",
+
+ "404142434445464748494a4b4c4d4e4f",
+ "1011121314151617",
+ "000102030405060708090a0b0c0d0e0f",
+ "202122232425262728292a2b2c2d2e2f",
+ "d2a1f0e051ea5f62081a7792073d593d",
+ "1fc64fbfaccd",
+
+ "404142434445464748494a4b4c4d4e4f",
+ "101112131415161718191a1b",
+ "000102030405060708090a0b0c0d0e0f10111213",
+ "202122232425262728292a2b2c2d2e2f3031323334353637",
+ "e3b201a9f5b71a7a9b1ceaeccd97e70b6176aad9a4428aa5",
+ "484392fbc1b09951",
+
+ "404142434445464748494a4b4c4d4e4f",
+ "101112131415161718191a1b1c",
+ NULL,
+ "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
+ "69915dad1e84c6376a68c2967e4dab615ae0fd1faec44cc484828529463ccf72",
+ "b4ac6bec93e8598e7f0dadbcea5b",
+
+ NULL
+};
+
+static void
+test_CCM_inner(const char *name, const br_block_ctrcbc_class *vt)
+{
+ size_t u;
+
+ printf("Test CCM %s: ", name);
+ fflush(stdout);
+
+ for (u = 0; KAT_CCM[u]; u += 6) {
+ unsigned char plain[100];
+ unsigned char key[32];
+ unsigned char nonce[100];
+ unsigned char aad_buf[100], *aad;
+ unsigned char cipher[100];
+ unsigned char tag[100];
+ size_t plain_len, key_len, nonce_len, aad_len, tag_len;
+ br_aes_gen_ctrcbc_keys bc;
+ br_ccm_context ec;
+ unsigned char tmp[100], out[16];
+ size_t v;
+
+ key_len = hextobin(key, KAT_CCM[u]);
+ nonce_len = hextobin(nonce, KAT_CCM[u + 1]);
+ if (KAT_CCM[u + 2] == NULL) {
+ aad_len = 65536;
+ aad = malloc(aad_len);
+ if (aad == NULL) {
+ fprintf(stderr, "OOM error\n");
+ exit(EXIT_FAILURE);
+ }
+ for (v = 0; v < 65536; v ++) {
+ aad[v] = (unsigned char)v;
+ }
+ } else {
+ aad = aad_buf;
+ aad_len = hextobin(aad, KAT_CCM[u + 2]);
+ }
+ plain_len = hextobin(plain, KAT_CCM[u + 3]);
+ hextobin(cipher, KAT_CCM[u + 4]);
+ tag_len = hextobin(tag, KAT_CCM[u + 5]);
+
+ vt->init(&bc.vtable, key, key_len);
+ br_ccm_init(&ec, &bc.vtable);
+
+ memset(tmp, 0x54, sizeof tmp);
+
+ /*
+ * Basic operation.
+ */
+ memcpy(tmp, plain, plain_len);
+ if (!br_ccm_reset(&ec, nonce, nonce_len,
+ aad_len, plain_len, tag_len))
+ {
+ fprintf(stderr, "CCM reset failed\n");
+ exit(EXIT_FAILURE);
+ }
+ br_ccm_aad_inject(&ec, aad, aad_len);
+ br_ccm_flip(&ec);
+ br_ccm_run(&ec, 1, tmp, plain_len);
+ if (br_ccm_get_tag(&ec, out) != tag_len) {
+ fprintf(stderr, "CCM returned wrong tag length\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("KAT CCM 1", tmp, cipher, plain_len);
+ check_equals("KAT CCM 2", out, tag, tag_len);
+
+ br_ccm_reset(&ec, nonce, nonce_len,
+ aad_len, plain_len, tag_len);
+ br_ccm_aad_inject(&ec, aad, aad_len);
+ br_ccm_flip(&ec);
+ br_ccm_run(&ec, 0, tmp, plain_len);
+ check_equals("KAT CCM 3", tmp, plain, plain_len);
+ if (!br_ccm_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag not verified (1)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (v = plain_len; v < sizeof tmp; v ++) {
+ if (tmp[v] != 0x54) {
+ fprintf(stderr, "overflow on data\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Byte-by-byte injection.
+ */
+ br_ccm_reset(&ec, nonce, nonce_len,
+ aad_len, plain_len, tag_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_ccm_aad_inject(&ec, aad + v, 1);
+ }
+ br_ccm_flip(&ec);
+ for (v = 0; v < plain_len; v ++) {
+ br_ccm_run(&ec, 1, tmp + v, 1);
+ }
+ check_equals("KAT CCM 4", tmp, cipher, plain_len);
+ if (!br_ccm_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag not verified (2)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ br_ccm_reset(&ec, nonce, nonce_len,
+ aad_len, plain_len, tag_len);
+ for (v = 0; v < aad_len; v ++) {
+ br_ccm_aad_inject(&ec, aad + v, 1);
+ }
+ br_ccm_flip(&ec);
+ for (v = 0; v < plain_len; v ++) {
+ br_ccm_run(&ec, 0, tmp + v, 1);
+ }
+ br_ccm_get_tag(&ec, out);
+ check_equals("KAT CCM 5", tmp, plain, plain_len);
+ check_equals("KAT CCM 6", out, tag, tag_len);
+
+ /*
+ * Check that alterations are detected.
+ */
+ for (v = 0; v < aad_len; v ++) {
+ memcpy(tmp, cipher, plain_len);
+ br_ccm_reset(&ec, nonce, nonce_len,
+ aad_len, plain_len, tag_len);
+ aad[v] ^= 0x04;
+ br_ccm_aad_inject(&ec, aad, aad_len);
+ aad[v] ^= 0x04;
+ br_ccm_flip(&ec);
+ br_ccm_run(&ec, 0, tmp, plain_len);
+ check_equals("KAT CCM 7", tmp, plain, plain_len);
+ if (br_ccm_check_tag(&ec, tag)) {
+ fprintf(stderr, "Tag should have changed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * When the AAD is really big, we don't want to do
+ * the complete quadratic operation.
+ */
+ if (v >= 32) {
+ break;
+ }
+ }
+
+ if (aad != aad_buf) {
+ free(aad);
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_CCM(void)
+{
+ const br_block_ctrcbc_class *x_ctrcbc;
+
+ test_CCM_inner("aes_big", &br_aes_big_ctrcbc_vtable);
+ test_CCM_inner("aes_small", &br_aes_small_ctrcbc_vtable);
+ test_CCM_inner("aes_ct", &br_aes_ct_ctrcbc_vtable);
+ test_CCM_inner("aes_ct64", &br_aes_ct64_ctrcbc_vtable);
+
+ x_ctrcbc = br_aes_x86ni_ctrcbc_get_vtable();
+ if (x_ctrcbc != NULL) {
+ test_CCM_inner("aes_x86ni", x_ctrcbc);
+ } else {
+ printf("Test CCM aes_x86ni: UNAVAILABLE\n");
+ }
+
+ x_ctrcbc = br_aes_pwr8_ctrcbc_get_vtable();
+ if (x_ctrcbc != NULL) {
+ test_CCM_inner("aes_pwr8", x_ctrcbc);
+ } else {
+ printf("Test CCM aes_pwr8: UNAVAILABLE\n");
+ }
+}
+
+static void
+test_EC_inner(const char *sk, const char *sU,
+ const br_ec_impl *impl, int curve)
+{
+ unsigned char bk[70];
+ unsigned char eG[150], eU[150];
+ uint32_t n[22], n0i;
+ size_t klen, ulen, nlen;
+ const br_ec_curve_def *cd;
+ br_hmac_drbg_context rng;
+ int i;
+
+ klen = hextobin(bk, sk);
+ ulen = hextobin(eU, sU);
+ switch (curve) {
+ case BR_EC_secp256r1:
+ cd = &br_secp256r1;
+ break;
+ case BR_EC_secp384r1:
+ cd = &br_secp384r1;
+ break;
+ case BR_EC_secp521r1:
+ cd = &br_secp521r1;
+ break;
+ default:
+ fprintf(stderr, "Unknown curve: %d\n", curve);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ if (ulen != cd->generator_len) {
+ fprintf(stderr, "KAT vector wrong (%lu / %lu)\n",
+ (unsigned long)ulen,
+ (unsigned long)cd->generator_len);
+ }
+ memcpy(eG, cd->generator, ulen);
+ if (impl->mul(eG, ulen, bk, klen, curve) != 1) {
+ fprintf(stderr, "KAT multiplication failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(eG, eU, ulen) != 0) {
+ fprintf(stderr, "KAT mul: mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Test the two-point-mul function. We want to test the basic
+ * functionality, and the following special cases:
+ * x = y
+ * x + y = curve order
+ */
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ n0i = br_i31_ninv31(n[1]);
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, "seed for EC", 11);
+ for (i = 0; i < 10; i ++) {
+ unsigned char ba[80], bb[80], bx[80], by[80], bz[80];
+ uint32_t a[22], b[22], x[22], y[22], z[22], t1[22], t2[22];
+ uint32_t r;
+ unsigned char eA[160], eB[160], eC[160], eD[160];
+
+ /*
+ * Generate random a and b, and compute A = a*G and B = b*G.
+ */
+ br_hmac_drbg_generate(&rng, ba, sizeof ba);
+ br_i31_decode_reduce(a, ba, sizeof ba, n);
+ br_i31_encode(ba, nlen, a);
+ br_hmac_drbg_generate(&rng, bb, sizeof bb);
+ br_i31_decode_reduce(b, bb, sizeof bb, n);
+ br_i31_encode(bb, nlen, b);
+ memcpy(eA, cd->generator, ulen);
+ impl->mul(eA, ulen, ba, nlen, cd->curve);
+ memcpy(eB, cd->generator, ulen);
+ impl->mul(eB, ulen, bb, nlen, cd->curve);
+
+ /*
+ * Generate random x and y (modulo n).
+ */
+ br_hmac_drbg_generate(&rng, bx, sizeof bx);
+ br_i31_decode_reduce(x, bx, sizeof bx, n);
+ br_i31_encode(bx, nlen, x);
+ br_hmac_drbg_generate(&rng, by, sizeof by);
+ br_i31_decode_reduce(y, by, sizeof by, n);
+ br_i31_encode(by, nlen, y);
+
+ /*
+ * Compute z = a*x + b*y (mod n).
+ */
+ memcpy(t1, x, sizeof x);
+ br_i31_to_monty(t1, n);
+ br_i31_montymul(z, a, t1, n, n0i);
+ memcpy(t1, y, sizeof y);
+ br_i31_to_monty(t1, n);
+ br_i31_montymul(t2, b, t1, n, n0i);
+ r = br_i31_add(z, t2, 1);
+ r |= br_i31_sub(z, n, 0) ^ 1;
+ br_i31_sub(z, n, r);
+ br_i31_encode(bz, nlen, z);
+
+ /*
+ * Compute C = x*A + y*B with muladd(), and also
+ * D = z*G with mul(). The two points must match.
+ */
+ memcpy(eC, eA, ulen);
+ if (impl->muladd(eC, eB, ulen,
+ bx, nlen, by, nlen, cd->curve) != 1)
+ {
+ fprintf(stderr, "muladd() failed (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(eD, cd->generator, ulen);
+ if (impl->mul(eD, ulen, bz, nlen, cd->curve) != 1) {
+ fprintf(stderr, "mul() failed (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(eC, eD, nlen) != 0) {
+ fprintf(stderr, "mul() / muladd() mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Also recomputed D = z*G with mulgen(). This must
+ * again match.
+ */
+ memset(eD, 0, ulen);
+ if (impl->mulgen(eD, bz, nlen, cd->curve) != ulen) {
+ fprintf(stderr, "mulgen() failed: wrong length\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(eC, eD, nlen) != 0) {
+ fprintf(stderr, "mulgen() / muladd() mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Check with x*A = y*B. We do so by setting b = x and y = a.
+ */
+ memcpy(b, x, sizeof x);
+ br_i31_encode(bb, nlen, b);
+ memcpy(eB, cd->generator, ulen);
+ impl->mul(eB, ulen, bb, nlen, cd->curve);
+ memcpy(y, a, sizeof a);
+ br_i31_encode(by, nlen, y);
+
+ memcpy(t1, x, sizeof x);
+ br_i31_to_monty(t1, n);
+ br_i31_montymul(z, a, t1, n, n0i);
+ memcpy(t1, y, sizeof y);
+ br_i31_to_monty(t1, n);
+ br_i31_montymul(t2, b, t1, n, n0i);
+ r = br_i31_add(z, t2, 1);
+ r |= br_i31_sub(z, n, 0) ^ 1;
+ br_i31_sub(z, n, r);
+ br_i31_encode(bz, nlen, z);
+
+ memcpy(eC, eA, ulen);
+ if (impl->muladd(eC, eB, ulen,
+ bx, nlen, by, nlen, cd->curve) != 1)
+ {
+ fprintf(stderr, "muladd() failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(eD, cd->generator, ulen);
+ if (impl->mul(eD, ulen, bz, nlen, cd->curve) != 1) {
+ fprintf(stderr, "mul() failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(eC, eD, nlen) != 0) {
+ fprintf(stderr,
+ "mul() / muladd() mismatch (x*A=y*B)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Check with x*A + y*B = 0. At that point, b = x, so we
+ * just need to set y = -a (mod n).
+ */
+ memcpy(y, n, sizeof n);
+ br_i31_sub(y, a, 1);
+ br_i31_encode(by, nlen, y);
+ memcpy(eC, eA, ulen);
+ if (impl->muladd(eC, eB, ulen,
+ bx, nlen, by, nlen, cd->curve) != 0)
+ {
+ fprintf(stderr, "muladd() should have failed\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf(".");
+ fflush(stdout);
+}
+
+static void
+test_EC_P256_carry_inner(const br_ec_impl *impl, const char *sP, const char *sQ)
+{
+ unsigned char P[65], Q[65], k[1];
+ size_t plen, qlen;
+
+ plen = hextobin(P, sP);
+ qlen = hextobin(Q, sQ);
+ if (plen != sizeof P || qlen != sizeof P) {
+ fprintf(stderr, "KAT is incorrect\n");
+ exit(EXIT_FAILURE);
+ }
+ k[0] = 0x10;
+ if (impl->mul(P, plen, k, 1, BR_EC_secp256r1) != 1) {
+ fprintf(stderr, "P-256 multiplication failed\n");
+ exit(EXIT_FAILURE);
+ }
+ check_equals("P256_carry", P, Q, plen);
+ printf(".");
+ fflush(stdout);
+}
+
+static void
+test_EC_P256_carry(const br_ec_impl *impl)
+{
+ test_EC_P256_carry_inner(impl,
+ "0435BAA24B2B6E1B3C88E22A383BD88CC4B9A3166E7BCF94FF6591663AE066B33B821EBA1B4FC8EA609A87EB9A9C9A1CCD5C9F42FA1365306F64D7CAA718B8C978",
+ "0447752A76CA890328D34E675C4971EC629132D1FC4863EDB61219B72C4E58DC5E9D51E7B293488CFD913C3CF20E438BB65C2BA66A7D09EABB45B55E804260C5EB");
+ test_EC_P256_carry_inner(impl,
+ "04DCAE9D9CE211223602024A6933BD42F77B6BF4EAB9C8915F058C149419FADD2CC9FC0707B270A1B5362BA4D249AFC8AC3DA1EFCA8270176EEACA525B49EE19E6",
+ "048DAC7B0BE9B3206FCE8B24B6B4AEB122F2A67D13E536B390B6585CA193427E63F222388B5F51D744D6F5D47536D89EEEC89552BCB269E7828019C4410DFE980A");
+}
+
+static void
+test_EC_KAT(const char *name, const br_ec_impl *impl, uint32_t curve_mask)
+{
+ printf("Test %s: ", name);
+ fflush(stdout);
+
+ if (curve_mask & ((uint32_t)1 << BR_EC_secp256r1)) {
+ test_EC_inner(
+ "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721",
+ "0460FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB67903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299",
+ impl, BR_EC_secp256r1);
+ test_EC_P256_carry(impl);
+ }
+ if (curve_mask & ((uint32_t)1 << BR_EC_secp384r1)) {
+ test_EC_inner(
+ "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5",
+ "04EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64DEF8F0EA9055866064A254515480BC138015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1288B231C3AE0D4FE7344FD2533264720",
+ impl, BR_EC_secp384r1);
+ }
+ if (curve_mask & ((uint32_t)1 << BR_EC_secp521r1)) {
+ test_EC_inner(
+ "00FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
+ "0401894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD371123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F5023A400493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A28A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDFCF5",
+ impl, BR_EC_secp521r1);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_EC_keygen(const char *name, const br_ec_impl *impl, uint32_t curves)
+{
+ int curve;
+ br_hmac_drbg_context rng;
+
+ printf("Test %s keygen: ", name);
+ fflush(stdout);
+
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, "seed for EC keygen", 18);
+ br_hmac_drbg_update(&rng, name, strlen(name));
+
+ for (curve = -1; curve <= 35; curve ++) {
+ br_ec_private_key sk;
+ br_ec_public_key pk;
+ unsigned char kbuf_priv[BR_EC_KBUF_PRIV_MAX_SIZE];
+ unsigned char kbuf_pub[BR_EC_KBUF_PUB_MAX_SIZE];
+
+ if (curve < 0 || curve >= 32 || ((curves >> curve) & 1) == 0) {
+ if (br_ec_keygen(&rng.vtable, impl,
+ &sk, kbuf_priv, curve) != 0)
+ {
+ fprintf(stderr, "br_ec_keygen() did not"
+ " reject unsupported curve %d\n",
+ curve);
+ exit(EXIT_FAILURE);
+ }
+ sk.curve = curve;
+ if (br_ec_compute_pub(impl, NULL, NULL, &sk) != 0) {
+ fprintf(stderr, "br_ec_keygen() did not"
+ " reject unsupported curve %d\n",
+ curve);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ size_t len, u;
+ unsigned char tmp_priv[sizeof kbuf_priv];
+ unsigned char tmp_pub[sizeof kbuf_pub];
+ unsigned z;
+
+ len = br_ec_keygen(&rng.vtable, impl,
+ NULL, NULL, curve);
+ if (len == 0) {
+ fprintf(stderr, "br_ec_keygen() rejects"
+ " supported curve %d\n", curve);
+ exit(EXIT_FAILURE);
+ }
+ if (len > sizeof kbuf_priv) {
+ fprintf(stderr, "oversized kbuf_priv\n");
+ exit(EXIT_FAILURE);
+ }
+ memset(kbuf_priv, 0, sizeof kbuf_priv);
+ if (br_ec_keygen(&rng.vtable, impl,
+ NULL, kbuf_priv, curve) != len)
+ {
+ fprintf(stderr, "kbuf_priv length mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+ z = 0;
+ for (u = 0; u < len; u ++) {
+ z |= kbuf_priv[u];
+ }
+ if (z == 0) {
+ fprintf(stderr, "kbuf_priv not initialized\n");
+ exit(EXIT_FAILURE);
+ }
+ for (u = len; u < sizeof kbuf_priv; u ++) {
+ if (kbuf_priv[u] != 0) {
+ fprintf(stderr, "kbuf_priv overflow\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (br_ec_keygen(&rng.vtable, impl,
+ NULL, tmp_priv, curve) != len)
+ {
+ fprintf(stderr, "tmp_priv length mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(kbuf_priv, tmp_priv, len) == 0) {
+ fprintf(stderr, "keygen stutter\n");
+ exit(EXIT_FAILURE);
+ }
+ memset(&sk, 0, sizeof sk);
+ if (br_ec_keygen(&rng.vtable, impl,
+ &sk, kbuf_priv, curve) != len)
+ {
+ fprintf(stderr,
+ "kbuf_priv length mismatch (2)\n");
+ exit(EXIT_FAILURE);
+ }
+ if (sk.curve != curve || sk.x != kbuf_priv
+ || sk.xlen != len)
+ {
+ fprintf(stderr, "sk not initialized\n");
+ exit(EXIT_FAILURE);
+ }
+
+ len = br_ec_compute_pub(impl, NULL, NULL, &sk);
+ if (len > sizeof kbuf_pub) {
+ fprintf(stderr, "oversized kbuf_pub\n");
+ exit(EXIT_FAILURE);
+ }
+ memset(kbuf_pub, 0, sizeof kbuf_pub);
+ if (br_ec_compute_pub(impl, NULL,
+ kbuf_pub, &sk) != len)
+ {
+ fprintf(stderr, "kbuf_pub length mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+ for (u = len; u < sizeof kbuf_pub; u ++) {
+ if (kbuf_pub[u] != 0) {
+ fprintf(stderr, "kbuf_pub overflow\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ memset(&pk, 0, sizeof pk);
+ if (br_ec_compute_pub(impl, &pk,
+ tmp_pub, &sk) != len)
+ {
+ fprintf(stderr, "tmp_pub length mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(kbuf_pub, tmp_pub, len) != 0) {
+ fprintf(stderr, "pubkey mismatch\n");
+ exit(EXIT_FAILURE);
+ }
+ if (pk.curve != curve || pk.q != tmp_pub
+ || pk.qlen != len)
+ {
+ fprintf(stderr, "pk not initialized\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (impl->mulgen(kbuf_pub,
+ sk.x, sk.xlen, curve) != len
+ || memcmp(pk.q, kbuf_pub, len) != 0)
+ {
+ fprintf(stderr, "wrong pubkey\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_EC_prime_i15(void)
+{
+ test_EC_KAT("EC_prime_i15", &br_ec_prime_i15,
+ (uint32_t)1 << BR_EC_secp256r1
+ | (uint32_t)1 << BR_EC_secp384r1
+ | (uint32_t)1 << BR_EC_secp521r1);
+ test_EC_keygen("EC_prime_i15", &br_ec_prime_i15,
+ (uint32_t)1 << BR_EC_secp256r1
+ | (uint32_t)1 << BR_EC_secp384r1
+ | (uint32_t)1 << BR_EC_secp521r1);
+}
+
+static void
+test_EC_prime_i31(void)
+{
+ test_EC_KAT("EC_prime_i31", &br_ec_prime_i31,
+ (uint32_t)1 << BR_EC_secp256r1
+ | (uint32_t)1 << BR_EC_secp384r1
+ | (uint32_t)1 << BR_EC_secp521r1);
+ test_EC_keygen("EC_prime_i31", &br_ec_prime_i31,
+ (uint32_t)1 << BR_EC_secp256r1
+ | (uint32_t)1 << BR_EC_secp384r1
+ | (uint32_t)1 << BR_EC_secp521r1);
+}
+
+static void
+test_EC_p256_m15(void)
+{
+ test_EC_KAT("EC_p256_m15", &br_ec_p256_m15,
+ (uint32_t)1 << BR_EC_secp256r1);
+ test_EC_keygen("EC_p256_m15", &br_ec_p256_m15,
+ (uint32_t)1 << BR_EC_secp256r1);
+}
+
+static void
+test_EC_p256_m31(void)
+{
+ test_EC_KAT("EC_p256_m31", &br_ec_p256_m31,
+ (uint32_t)1 << BR_EC_secp256r1);
+ test_EC_keygen("EC_p256_m31", &br_ec_p256_m31,
+ (uint32_t)1 << BR_EC_secp256r1);
+}
+
+static void
+test_EC_p256_m62(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m62_get();
+ if (ec != NULL) {
+ test_EC_KAT("EC_p256_m62", ec,
+ (uint32_t)1 << BR_EC_secp256r1);
+ test_EC_keygen("EC_p256_m62", ec,
+ (uint32_t)1 << BR_EC_secp256r1);
+ } else {
+ printf("Test EC_p256_m62: UNAVAILABLE\n");
+ printf("Test EC_p256_m62 keygen: UNAVAILABLE\n");
+ }
+}
+
+static void
+test_EC_p256_m64(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m64_get();
+ if (ec != NULL) {
+ test_EC_KAT("EC_p256_m64", ec,
+ (uint32_t)1 << BR_EC_secp256r1);
+ test_EC_keygen("EC_p256_m64", ec,
+ (uint32_t)1 << BR_EC_secp256r1);
+ } else {
+ printf("Test EC_p256_m64: UNAVAILABLE\n");
+ printf("Test EC_p256_m64 keygen: UNAVAILABLE\n");
+ }
+}
+
+const struct {
+ const char *scalar_le;
+ const char *u_in;
+ const char *u_out;
+} C25519_KAT[] = {
+ { "A546E36BF0527C9D3B16154B82465EDD62144C0AC1FC5A18506A2244BA449AC4",
+ "E6DB6867583030DB3594C1A424B15F7C726624EC26B3353B10A903A6D0AB1C4C",
+ "C3DA55379DE9C6908E94EA4DF28D084F32ECCF03491C71F754B4075577A28552" },
+ { "4B66E9D4D1B4673C5AD22691957D6AF5C11B6421E0EA01D42CA4169E7918BA0D",
+ "E5210F12786811D3F4B7959D0538AE2C31DBE7106FC03C3EFC4CD549C715A493",
+ "95CBDE9476E8907D7AADE45CB4B873F88B595A68799FA152E6F8F7647AAC7957" },
+ { 0, 0, 0 }
+};
+
+static void
+revbytes(unsigned char *buf, size_t len)
+{
+ size_t u;
+
+ for (u = 0; u < (len >> 1); u ++) {
+ unsigned t;
+
+ t = buf[u];
+ buf[u] = buf[len - 1 - u];
+ buf[len - 1 - u] = t;
+ }
+}
+
+static void
+test_EC_c25519(const char *name, const br_ec_impl *iec)
+{
+ unsigned char bu[32], bk[32], br[32];
+ size_t v;
+ int i;
+
+ printf("Test %s: ", name);
+ fflush(stdout);
+ for (v = 0; C25519_KAT[v].scalar_le; v ++) {
+ hextobin(bk, C25519_KAT[v].scalar_le);
+ revbytes(bk, sizeof bk);
+ hextobin(bu, C25519_KAT[v].u_in);
+ hextobin(br, C25519_KAT[v].u_out);
+ if (!iec->mul(bu, sizeof bu, bk, sizeof bk, BR_EC_curve25519)) {
+ fprintf(stderr, "Curve25519 multiplication failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (memcmp(bu, br, sizeof bu) != 0) {
+ fprintf(stderr, "Curve25519 failed KAT\n");
+ exit(EXIT_FAILURE);
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ printf(" ");
+ fflush(stdout);
+
+ memset(bu, 0, sizeof bu);
+ bu[0] = 0x09;
+ memcpy(bk, bu, sizeof bu);
+ for (i = 1; i <= 1000; i ++) {
+ revbytes(bk, sizeof bk);
+ if (!iec->mul(bu, sizeof bu, bk, sizeof bk, BR_EC_curve25519)) {
+ fprintf(stderr, "Curve25519 multiplication failed"
+ " (iter=%d)\n", i);
+ exit(EXIT_FAILURE);
+ }
+ revbytes(bk, sizeof bk);
+ for (v = 0; v < sizeof bu; v ++) {
+ unsigned t;
+
+ t = bu[v];
+ bu[v] = bk[v];
+ bk[v] = t;
+ }
+ if (i == 1 || i == 1000) {
+ const char *sref;
+
+ sref = (i == 1)
+ ? "422C8E7A6227D7BCA1350B3E2BB7279F7897B87BB6854B783C60E80311AE3079"
+ : "684CF59BA83309552800EF566F2F4D3C1C3887C49360E3875F2EB94D99532C51";
+ hextobin(br, sref);
+ if (memcmp(bk, br, sizeof bk) != 0) {
+ fprintf(stderr,
+ "Curve25519 failed KAT (iter=%d)\n", i);
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (i % 100 == 0) {
+ printf(".");
+ fflush(stdout);
+ }
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_EC_c25519_i15(void)
+{
+ test_EC_c25519("EC_c25519_i15", &br_ec_c25519_i15);
+ test_EC_keygen("EC_c25519_i15", &br_ec_c25519_i15,
+ (uint32_t)1 << BR_EC_curve25519);
+}
+
+static void
+test_EC_c25519_i31(void)
+{
+ test_EC_c25519("EC_c25519_i31", &br_ec_c25519_i31);
+ test_EC_keygen("EC_c25519_i31", &br_ec_c25519_i31,
+ (uint32_t)1 << BR_EC_curve25519);
+}
+
+static void
+test_EC_c25519_m15(void)
+{
+ test_EC_c25519("EC_c25519_m15", &br_ec_c25519_m15);
+ test_EC_keygen("EC_c25519_m15", &br_ec_c25519_m15,
+ (uint32_t)1 << BR_EC_curve25519);
+}
+
+static void
+test_EC_c25519_m31(void)
+{
+ test_EC_c25519("EC_c25519_m31", &br_ec_c25519_m31);
+ test_EC_keygen("EC_c25519_m31", &br_ec_c25519_m31,
+ (uint32_t)1 << BR_EC_curve25519);
+}
+
+static void
+test_EC_c25519_m62(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_c25519_m62_get();
+ if (ec != NULL) {
+ test_EC_c25519("EC_c25519_m62", ec);
+ test_EC_keygen("EC_c25519_m62", ec,
+ (uint32_t)1 << BR_EC_curve25519);
+ } else {
+ printf("Test EC_c25519_m62: UNAVAILABLE\n");
+ printf("Test EC_c25519_m62 keygen: UNAVAILABLE\n");
+ }
+}
+
+static void
+test_EC_c25519_m64(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_c25519_m64_get();
+ if (ec != NULL) {
+ test_EC_c25519("EC_c25519_m64", ec);
+ test_EC_keygen("EC_c25519_m64", ec,
+ (uint32_t)1 << BR_EC_curve25519);
+ } else {
+ printf("Test EC_c25519_m64: UNAVAILABLE\n");
+ printf("Test EC_c25519_m64 keygen: UNAVAILABLE\n");
+ }
+}
+
+static const unsigned char EC_P256_PUB_POINT[] = {
+ 0x04, 0x60, 0xFE, 0xD4, 0xBA, 0x25, 0x5A, 0x9D,
+ 0x31, 0xC9, 0x61, 0xEB, 0x74, 0xC6, 0x35, 0x6D,
+ 0x68, 0xC0, 0x49, 0xB8, 0x92, 0x3B, 0x61, 0xFA,
+ 0x6C, 0xE6, 0x69, 0x62, 0x2E, 0x60, 0xF2, 0x9F,
+ 0xB6, 0x79, 0x03, 0xFE, 0x10, 0x08, 0xB8, 0xBC,
+ 0x99, 0xA4, 0x1A, 0xE9, 0xE9, 0x56, 0x28, 0xBC,
+ 0x64, 0xF2, 0xF1, 0xB2, 0x0C, 0x2D, 0x7E, 0x9F,
+ 0x51, 0x77, 0xA3, 0xC2, 0x94, 0xD4, 0x46, 0x22,
+ 0x99
+};
+
+static const unsigned char EC_P256_PRIV_X[] = {
+ 0xC9, 0xAF, 0xA9, 0xD8, 0x45, 0xBA, 0x75, 0x16,
+ 0x6B, 0x5C, 0x21, 0x57, 0x67, 0xB1, 0xD6, 0x93,
+ 0x4E, 0x50, 0xC3, 0xDB, 0x36, 0xE8, 0x9B, 0x12,
+ 0x7B, 0x8A, 0x62, 0x2B, 0x12, 0x0F, 0x67, 0x21
+};
+
+static const br_ec_public_key EC_P256_PUB = {
+ BR_EC_secp256r1,
+ (unsigned char *)EC_P256_PUB_POINT, sizeof EC_P256_PUB_POINT
+};
+
+static const br_ec_private_key EC_P256_PRIV = {
+ BR_EC_secp256r1,
+ (unsigned char *)EC_P256_PRIV_X, sizeof EC_P256_PRIV_X
+};
+
+static const unsigned char EC_P384_PUB_POINT[] = {
+ 0x04, 0xEC, 0x3A, 0x4E, 0x41, 0x5B, 0x4E, 0x19,
+ 0xA4, 0x56, 0x86, 0x18, 0x02, 0x9F, 0x42, 0x7F,
+ 0xA5, 0xDA, 0x9A, 0x8B, 0xC4, 0xAE, 0x92, 0xE0,
+ 0x2E, 0x06, 0xAA, 0xE5, 0x28, 0x6B, 0x30, 0x0C,
+ 0x64, 0xDE, 0xF8, 0xF0, 0xEA, 0x90, 0x55, 0x86,
+ 0x60, 0x64, 0xA2, 0x54, 0x51, 0x54, 0x80, 0xBC,
+ 0x13, 0x80, 0x15, 0xD9, 0xB7, 0x2D, 0x7D, 0x57,
+ 0x24, 0x4E, 0xA8, 0xEF, 0x9A, 0xC0, 0xC6, 0x21,
+ 0x89, 0x67, 0x08, 0xA5, 0x93, 0x67, 0xF9, 0xDF,
+ 0xB9, 0xF5, 0x4C, 0xA8, 0x4B, 0x3F, 0x1C, 0x9D,
+ 0xB1, 0x28, 0x8B, 0x23, 0x1C, 0x3A, 0xE0, 0xD4,
+ 0xFE, 0x73, 0x44, 0xFD, 0x25, 0x33, 0x26, 0x47,
+ 0x20
+};
+
+static const unsigned char EC_P384_PRIV_X[] = {
+ 0x6B, 0x9D, 0x3D, 0xAD, 0x2E, 0x1B, 0x8C, 0x1C,
+ 0x05, 0xB1, 0x98, 0x75, 0xB6, 0x65, 0x9F, 0x4D,
+ 0xE2, 0x3C, 0x3B, 0x66, 0x7B, 0xF2, 0x97, 0xBA,
+ 0x9A, 0xA4, 0x77, 0x40, 0x78, 0x71, 0x37, 0xD8,
+ 0x96, 0xD5, 0x72, 0x4E, 0x4C, 0x70, 0xA8, 0x25,
+ 0xF8, 0x72, 0xC9, 0xEA, 0x60, 0xD2, 0xED, 0xF5
+};
+
+static const br_ec_public_key EC_P384_PUB = {
+ BR_EC_secp384r1,
+ (unsigned char *)EC_P384_PUB_POINT, sizeof EC_P384_PUB_POINT
+};
+
+static const br_ec_private_key EC_P384_PRIV = {
+ BR_EC_secp384r1,
+ (unsigned char *)EC_P384_PRIV_X, sizeof EC_P384_PRIV_X
+};
+
+static const unsigned char EC_P521_PUB_POINT[] = {
+ 0x04, 0x01, 0x89, 0x45, 0x50, 0xD0, 0x78, 0x59,
+ 0x32, 0xE0, 0x0E, 0xAA, 0x23, 0xB6, 0x94, 0xF2,
+ 0x13, 0xF8, 0xC3, 0x12, 0x1F, 0x86, 0xDC, 0x97,
+ 0xA0, 0x4E, 0x5A, 0x71, 0x67, 0xDB, 0x4E, 0x5B,
+ 0xCD, 0x37, 0x11, 0x23, 0xD4, 0x6E, 0x45, 0xDB,
+ 0x6B, 0x5D, 0x53, 0x70, 0xA7, 0xF2, 0x0F, 0xB6,
+ 0x33, 0x15, 0x5D, 0x38, 0xFF, 0xA1, 0x6D, 0x2B,
+ 0xD7, 0x61, 0xDC, 0xAC, 0x47, 0x4B, 0x9A, 0x2F,
+ 0x50, 0x23, 0xA4, 0x00, 0x49, 0x31, 0x01, 0xC9,
+ 0x62, 0xCD, 0x4D, 0x2F, 0xDD, 0xF7, 0x82, 0x28,
+ 0x5E, 0x64, 0x58, 0x41, 0x39, 0xC2, 0xF9, 0x1B,
+ 0x47, 0xF8, 0x7F, 0xF8, 0x23, 0x54, 0xD6, 0x63,
+ 0x0F, 0x74, 0x6A, 0x28, 0xA0, 0xDB, 0x25, 0x74,
+ 0x1B, 0x5B, 0x34, 0xA8, 0x28, 0x00, 0x8B, 0x22,
+ 0xAC, 0xC2, 0x3F, 0x92, 0x4F, 0xAA, 0xFB, 0xD4,
+ 0xD3, 0x3F, 0x81, 0xEA, 0x66, 0x95, 0x6D, 0xFE,
+ 0xAA, 0x2B, 0xFD, 0xFC, 0xF5
+};
+
+static const unsigned char EC_P521_PRIV_X[] = {
+ 0x00, 0xFA, 0xD0, 0x6D, 0xAA, 0x62, 0xBA, 0x3B,
+ 0x25, 0xD2, 0xFB, 0x40, 0x13, 0x3D, 0xA7, 0x57,
+ 0x20, 0x5D, 0xE6, 0x7F, 0x5B, 0xB0, 0x01, 0x8F,
+ 0xEE, 0x8C, 0x86, 0xE1, 0xB6, 0x8C, 0x7E, 0x75,
+ 0xCA, 0xA8, 0x96, 0xEB, 0x32, 0xF1, 0xF4, 0x7C,
+ 0x70, 0x85, 0x58, 0x36, 0xA6, 0xD1, 0x6F, 0xCC,
+ 0x14, 0x66, 0xF6, 0xD8, 0xFB, 0xEC, 0x67, 0xDB,
+ 0x89, 0xEC, 0x0C, 0x08, 0xB0, 0xE9, 0x96, 0xB8,
+ 0x35, 0x38
+};
+
+static const br_ec_public_key EC_P521_PUB = {
+ BR_EC_secp521r1,
+ (unsigned char *)EC_P521_PUB_POINT, sizeof EC_P521_PUB_POINT
+};
+
+static const br_ec_private_key EC_P521_PRIV = {
+ BR_EC_secp521r1,
+ (unsigned char *)EC_P521_PRIV_X, sizeof EC_P521_PRIV_X
+};
+
+typedef struct {
+ const br_ec_public_key *pub;
+ const br_ec_private_key *priv;
+ const br_hash_class *hf;
+ const char *msg;
+ const char *sk;
+ const char *sraw;
+ const char *sasn1;
+} ecdsa_kat_vector;
+
+const ecdsa_kat_vector ECDSA_KAT[] = {
+
+ /* Test vectors for P-256, from RFC 6979. */
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha1_vtable, "sample",
+ "882905F1227FD620FBF2ABF21244F0BA83D0DC3A9103DBBEE43A1FB858109DB4",
+ "61340C88C3AAEBEB4F6D667F672CA9759A6CCAA9FA8811313039EE4A35471D326D7F147DAC089441BB2E2FE8F7A3FA264B9C475098FDCF6E00D7C996E1B8B7EB",
+ "3044022061340C88C3AAEBEB4F6D667F672CA9759A6CCAA9FA8811313039EE4A35471D3202206D7F147DAC089441BB2E2FE8F7A3FA264B9C475098FDCF6E00D7C996E1B8B7EB"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha224_vtable, "sample",
+ "103F90EE9DC52E5E7FB5132B7033C63066D194321491862059967C715985D473",
+ "53B2FFF5D1752B2C689DF257C04C40A587FABABB3F6FC2702F1343AF7CA9AA3FB9AFB64FDC03DC1A131C7D2386D11E349F070AA432A4ACC918BEA988BF75C74C",
+ "3045022053B2FFF5D1752B2C689DF257C04C40A587FABABB3F6FC2702F1343AF7CA9AA3F022100B9AFB64FDC03DC1A131C7D2386D11E349F070AA432A4ACC918BEA988BF75C74C"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha256_vtable, "sample",
+ "A6E3C57DD01ABE90086538398355DD4C3B17AA873382B0F24D6129493D8AAD60",
+ "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8",
+ "3046022100EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716022100F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha384_vtable, "sample",
+ "09F634B188CEFD98E7EC88B1AA9852D734D0BC272F7D2A47DECC6EBEB375AAD4",
+ "0EAFEA039B20E9B42309FB1D89E213057CBF973DC0CFC8F129EDDDC800EF77194861F0491E6998B9455193E34E7B0D284DDD7149A74B95B9261F13ABDE940954",
+ "304402200EAFEA039B20E9B42309FB1D89E213057CBF973DC0CFC8F129EDDDC800EF771902204861F0491E6998B9455193E34E7B0D284DDD7149A74B95B9261F13ABDE940954"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha512_vtable, "sample",
+ "5FA81C63109BADB88C1F367B47DA606DA28CAD69AA22C4FE6AD7DF73A7173AA5",
+ "8496A60B5E9B47C825488827E0495B0E3FA109EC4568FD3F8D1097678EB97F002362AB1ADBE2B8ADF9CB9EDAB740EA6049C028114F2460F96554F61FAE3302FE",
+ "30450221008496A60B5E9B47C825488827E0495B0E3FA109EC4568FD3F8D1097678EB97F0002202362AB1ADBE2B8ADF9CB9EDAB740EA6049C028114F2460F96554F61FAE3302FE"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha1_vtable, "test",
+ "8C9520267C55D6B980DF741E56B4ADEE114D84FBFA2E62137954164028632A2E",
+ "0CBCC86FD6ABD1D99E703E1EC50069EE5C0B4BA4B9AC60E409E8EC5910D81A8901B9D7B73DFAA60D5651EC4591A0136F87653E0FD780C3B1BC872FFDEAE479B1",
+ "304402200CBCC86FD6ABD1D99E703E1EC50069EE5C0B4BA4B9AC60E409E8EC5910D81A89022001B9D7B73DFAA60D5651EC4591A0136F87653E0FD780C3B1BC872FFDEAE479B1"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha224_vtable, "test",
+ "669F4426F2688B8BE0DB3A6BD1989BDAEFFF84B649EEB84F3DD26080F667FAA7",
+ "C37EDB6F0AE79D47C3C27E962FA269BB4F441770357E114EE511F662EC34A692C820053A05791E521FCAAD6042D40AEA1D6B1A540138558F47D0719800E18F2D",
+ "3046022100C37EDB6F0AE79D47C3C27E962FA269BB4F441770357E114EE511F662EC34A692022100C820053A05791E521FCAAD6042D40AEA1D6B1A540138558F47D0719800E18F2D"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha256_vtable, "test",
+ "D16B6AE827F17175E040871A1C7EC3500192C4C92677336EC2537ACAEE0008E0",
+ "F1ABB023518351CD71D881567B1EA663ED3EFCF6C5132B354F28D3B0B7D38367019F4113742A2B14BD25926B49C649155F267E60D3814B4C0CC84250E46F0083",
+ "3045022100F1ABB023518351CD71D881567B1EA663ED3EFCF6C5132B354F28D3B0B7D383670220019F4113742A2B14BD25926B49C649155F267E60D3814B4C0CC84250E46F0083"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha384_vtable, "test",
+ "16AEFFA357260B04B1DD199693960740066C1A8F3E8EDD79070AA914D361B3B8",
+ "83910E8B48BB0C74244EBDF7F07A1C5413D61472BD941EF3920E623FBCCEBEB68DDBEC54CF8CD5874883841D712142A56A8D0F218F5003CB0296B6B509619F2C",
+ "304602210083910E8B48BB0C74244EBDF7F07A1C5413D61472BD941EF3920E623FBCCEBEB60221008DDBEC54CF8CD5874883841D712142A56A8D0F218F5003CB0296B6B509619F2C"
+ },
+ {
+ &EC_P256_PUB,
+ &EC_P256_PRIV,
+ &br_sha512_vtable, "test",
+ "6915D11632ACA3C40D5D51C08DAF9C555933819548784480E93499000D9F0B7F",
+ "461D93F31B6540894788FD206C07CFA0CC35F46FA3C91816FFF1040AD1581A0439AF9F15DE0DB8D97E72719C74820D304CE5226E32DEDAE67519E840D1194E55",
+ "30440220461D93F31B6540894788FD206C07CFA0CC35F46FA3C91816FFF1040AD1581A04022039AF9F15DE0DB8D97E72719C74820D304CE5226E32DEDAE67519E840D1194E55"
+ },
+
+ /* Test vectors for P-384, from RFC 6979. */
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha1_vtable, "sample",
+ "4471EF7518BB2C7C20F62EAE1C387AD0C5E8E470995DB4ACF694466E6AB096630F29E5938D25106C3C340045A2DB01A7",
+ "EC748D839243D6FBEF4FC5C4859A7DFFD7F3ABDDF72014540C16D73309834FA37B9BA002899F6FDA3A4A9386790D4EB2A3BCFA947BEEF4732BF247AC17F71676CB31A847B9FF0CBC9C9ED4C1A5B3FACF26F49CA031D4857570CCB5CA4424A443",
+ "3066023100EC748D839243D6FBEF4FC5C4859A7DFFD7F3ABDDF72014540C16D73309834FA37B9BA002899F6FDA3A4A9386790D4EB2023100A3BCFA947BEEF4732BF247AC17F71676CB31A847B9FF0CBC9C9ED4C1A5B3FACF26F49CA031D4857570CCB5CA4424A443"
+ },
+
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha224_vtable, "sample",
+ "A4E4D2F0E729EB786B31FC20AD5D849E304450E0AE8E3E341134A5C1AFA03CAB8083EE4E3C45B06A5899EA56C51B5879",
+ "42356E76B55A6D9B4631C865445DBE54E056D3B3431766D0509244793C3F9366450F76EE3DE43F5A125333A6BE0601229DA0C81787064021E78DF658F2FBB0B042BF304665DB721F077A4298B095E4834C082C03D83028EFBF93A3C23940CA8D",
+ "3065023042356E76B55A6D9B4631C865445DBE54E056D3B3431766D0509244793C3F9366450F76EE3DE43F5A125333A6BE0601220231009DA0C81787064021E78DF658F2FBB0B042BF304665DB721F077A4298B095E4834C082C03D83028EFBF93A3C23940CA8D"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha256_vtable, "sample",
+ "180AE9F9AEC5438A44BC159A1FCB277C7BE54FA20E7CF404B490650A8ACC414E375572342863C899F9F2EDF9747A9B60",
+ "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33BDE1E888E63355D92FA2B3C36D8FB2CDF3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEBEFDC63ECCD1AC42EC0CB8668A4FA0AB0",
+ "3065023021B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33BDE1E888E63355D92FA2B3C36D8FB2CD023100F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEBEFDC63ECCD1AC42EC0CB8668A4FA0AB0"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha384_vtable, "sample",
+ "94ED910D1A099DAD3254E9242AE85ABDE4BA15168EAF0CA87A555FD56D10FBCA2907E3E83BA95368623B8C4686915CF9",
+ "94EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C81A648152E44ACF96E36DD1E80FABE4699EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94FA329C145786E679E7B82C71A38628AC8",
+ "306602310094EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C81A648152E44ACF96E36DD1E80FABE4602310099EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94FA329C145786E679E7B82C71A38628AC8"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha512_vtable, "sample",
+ "92FC3C7183A883E24216D1141F1A8976C5B0DD797DFA597E3D7B32198BD35331A4E966532593A52980D0E3AAA5E10EC3",
+ "ED0959D5880AB2D869AE7F6C2915C6D60F96507F9CB3E047C0046861DA4A799CFE30F35CC900056D7C99CD7882433709512C8CCEEE3890A84058CE1E22DBC2198F42323CE8ACA9135329F03C068E5112DC7CC3EF3446DEFCEB01A45C2667FDD5",
+ "3065023100ED0959D5880AB2D869AE7F6C2915C6D60F96507F9CB3E047C0046861DA4A799CFE30F35CC900056D7C99CD78824337090230512C8CCEEE3890A84058CE1E22DBC2198F42323CE8ACA9135329F03C068E5112DC7CC3EF3446DEFCEB01A45C2667FDD5"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha1_vtable, "test",
+ "66CC2C8F4D303FC962E5FF6A27BD79F84EC812DDAE58CF5243B64A4AD8094D47EC3727F3A3C186C15054492E30698497",
+ "4BC35D3A50EF4E30576F58CD96CE6BF638025EE624004A1F7789A8B8E43D0678ACD9D29876DAF46638645F7F404B11C7D5A6326C494ED3FF614703878961C0FDE7B2C278F9A65FD8C4B7186201A2991695BA1C84541327E966FA7B50F7382282",
+ "306502304BC35D3A50EF4E30576F58CD96CE6BF638025EE624004A1F7789A8B8E43D0678ACD9D29876DAF46638645F7F404B11C7023100D5A6326C494ED3FF614703878961C0FDE7B2C278F9A65FD8C4B7186201A2991695BA1C84541327E966FA7B50F7382282"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha224_vtable, "test",
+ "18FA39DB95AA5F561F30FA3591DC59C0FA3653A80DAFFA0B48D1A4C6DFCBFF6E3D33BE4DC5EB8886A8ECD093F2935726",
+ "E8C9D0B6EA72A0E7837FEA1D14A1A9557F29FAA45D3E7EE888FC5BF954B5E62464A9A817C47FF78B8C11066B24080E7207041D4A7A0379AC7232FF72E6F77B6DDB8F09B16CCE0EC3286B2BD43FA8C6141C53EA5ABEF0D8231077A04540A96B66",
+ "3065023100E8C9D0B6EA72A0E7837FEA1D14A1A9557F29FAA45D3E7EE888FC5BF954B5E62464A9A817C47FF78B8C11066B24080E72023007041D4A7A0379AC7232FF72E6F77B6DDB8F09B16CCE0EC3286B2BD43FA8C6141C53EA5ABEF0D8231077A04540A96B66"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha256_vtable, "test",
+ "0CFAC37587532347DC3389FDC98286BBA8C73807285B184C83E62E26C401C0FAA48DD070BA79921A3457ABFF2D630AD7",
+ "6D6DEFAC9AB64DABAFE36C6BF510352A4CC27001263638E5B16D9BB51D451559F918EEDAF2293BE5B475CC8F0188636B2D46F3BECBCC523D5F1A1256BF0C9B024D879BA9E838144C8BA6BAEB4B53B47D51AB373F9845C0514EEFB14024787265",
+ "306402306D6DEFAC9AB64DABAFE36C6BF510352A4CC27001263638E5B16D9BB51D451559F918EEDAF2293BE5B475CC8F0188636B02302D46F3BECBCC523D5F1A1256BF0C9B024D879BA9E838144C8BA6BAEB4B53B47D51AB373F9845C0514EEFB14024787265"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha384_vtable, "test",
+ "015EE46A5BF88773ED9123A5AB0807962D193719503C527B031B4C2D225092ADA71F4A459BC0DA98ADB95837DB8312EA",
+ "8203B63D3C853E8D77227FB377BCF7B7B772E97892A80F36AB775D509D7A5FEB0542A7F0812998DA8F1DD3CA3CF023DBDDD0760448D42D8A43AF45AF836FCE4DE8BE06B485E9B61B827C2F13173923E06A739F040649A667BF3B828246BAA5A5",
+ "30660231008203B63D3C853E8D77227FB377BCF7B7B772E97892A80F36AB775D509D7A5FEB0542A7F0812998DA8F1DD3CA3CF023DB023100DDD0760448D42D8A43AF45AF836FCE4DE8BE06B485E9B61B827C2F13173923E06A739F040649A667BF3B828246BAA5A5"
+ },
+ {
+ &EC_P384_PUB,
+ &EC_P384_PRIV,
+ &br_sha512_vtable, "test",
+ "3780C4F67CB15518B6ACAE34C9F83568D2E12E47DEAB6C50A4E4EE5319D1E8CE0E2CC8A136036DC4B9C00E6888F66B6C",
+ "A0D5D090C9980FAF3C2CE57B7AE951D31977DD11C775D314AF55F76C676447D06FB6495CD21B4B6E340FC236584FB277976984E59B4C77B0E8E4460DCA3D9F20E07B9BB1F63BEEFAF576F6B2E8B224634A2092CD3792E0159AD9CEE37659C736",
+ "3066023100A0D5D090C9980FAF3C2CE57B7AE951D31977DD11C775D314AF55F76C676447D06FB6495CD21B4B6E340FC236584FB277023100976984E59B4C77B0E8E4460DCA3D9F20E07B9BB1F63BEEFAF576F6B2E8B224634A2092CD3792E0159AD9CEE37659C736"
+ },
+
+ /* Test vectors for P-521, from RFC 6979. */
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha1_vtable, "sample",
+ "0089C071B419E1C2820962321787258469511958E80582E95D8378E0C2CCDB3CB42BEDE42F50E3FA3C71F5A76724281D31D9C89F0F91FC1BE4918DB1C03A5838D0F9",
+ "00343B6EC45728975EA5CBA6659BBB6062A5FF89EEA58BE3C80B619F322C87910FE092F7D45BB0F8EEE01ED3F20BABEC079D202AE677B243AB40B5431D497C55D75D00E7B0E675A9B24413D448B8CC119D2BF7B2D2DF032741C096634D6D65D0DBE3D5694625FB9E8104D3B842C1B0E2D0B98BEA19341E8676AEF66AE4EBA3D5475D5D16",
+ "3081870241343B6EC45728975EA5CBA6659BBB6062A5FF89EEA58BE3C80B619F322C87910FE092F7D45BB0F8EEE01ED3F20BABEC079D202AE677B243AB40B5431D497C55D75D024200E7B0E675A9B24413D448B8CC119D2BF7B2D2DF032741C096634D6D65D0DBE3D5694625FB9E8104D3B842C1B0E2D0B98BEA19341E8676AEF66AE4EBA3D5475D5D16"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha224_vtable, "sample",
+ "0121415EC2CD7726330A61F7F3FA5DE14BE9436019C4DB8CB4041F3B54CF31BE0493EE3F427FB906393D895A19C9523F3A1D54BB8702BD4AA9C99DAB2597B92113F3",
+ "01776331CFCDF927D666E032E00CF776187BC9FDD8E69D0DABB4109FFE1B5E2A30715F4CC923A4A5E94D2503E9ACFED92857B7F31D7152E0F8C00C15FF3D87E2ED2E0050CB5265417FE2320BBB5A122B8E1A32BD699089851128E360E620A30C7E17BA41A666AF126CE100E5799B153B60528D5300D08489CA9178FB610A2006C254B41F",
+ "308187024201776331CFCDF927D666E032E00CF776187BC9FDD8E69D0DABB4109FFE1B5E2A30715F4CC923A4A5E94D2503E9ACFED92857B7F31D7152E0F8C00C15FF3D87E2ED2E024150CB5265417FE2320BBB5A122B8E1A32BD699089851128E360E620A30C7E17BA41A666AF126CE100E5799B153B60528D5300D08489CA9178FB610A2006C254B41F"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha256_vtable, "sample",
+ "00EDF38AFCAAECAB4383358B34D67C9F2216C8382AAEA44A3DAD5FDC9C32575761793FEF24EB0FC276DFC4F6E3EC476752F043CF01415387470BCBD8678ED2C7E1A0",
+ "01511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E1A7004A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7ECFC",
+ "308187024201511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E1A702414A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7ECFC"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha384_vtable, "sample",
+ "01546A108BC23A15D6F21872F7DED661FA8431DDBD922D0DCDB77CC878C8553FFAD064C95A920A750AC9137E527390D2D92F153E66196966EA554D9ADFCB109C4211",
+ "01EA842A0E17D2DE4F92C15315C63DDF72685C18195C2BB95E572B9C5136CA4B4B576AD712A52BE9730627D16054BA40CC0B8D3FF035B12AE75168397F5D50C6745101F21A3CEE066E1961025FB048BD5FE2B7924D0CD797BABE0A83B66F1E35EEAF5FDE143FA85DC394A7DEE766523393784484BDF3E00114A1C857CDE1AA203DB65D61",
+ "308188024201EA842A0E17D2DE4F92C15315C63DDF72685C18195C2BB95E572B9C5136CA4B4B576AD712A52BE9730627D16054BA40CC0B8D3FF035B12AE75168397F5D50C67451024201F21A3CEE066E1961025FB048BD5FE2B7924D0CD797BABE0A83B66F1E35EEAF5FDE143FA85DC394A7DEE766523393784484BDF3E00114A1C857CDE1AA203DB65D61"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha512_vtable, "sample",
+ "01DAE2EA071F8110DC26882D4D5EAE0621A3256FC8847FB9022E2B7D28E6F10198B1574FDD03A9053C08A1854A168AA5A57470EC97DD5CE090124EF52A2F7ECBFFD3",
+ "00C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F174E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E377FA00617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF282623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A67A",
+ "308187024200C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F174E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E377FA0241617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF282623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A67A"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha1_vtable, "test",
+ "00BB9F2BF4FE1038CCF4DABD7139A56F6FD8BB1386561BD3C6A4FC818B20DF5DDBA80795A947107A1AB9D12DAA615B1ADE4F7A9DC05E8E6311150F47F5C57CE8B222",
+ "013BAD9F29ABE20DE37EBEB823C252CA0F63361284015A3BF430A46AAA80B87B0693F0694BD88AFE4E661FC33B094CD3B7963BED5A727ED8BD6A3A202ABE009D036701E9BB81FF7944CA409AD138DBBEE228E1AFCC0C890FC78EC8604639CB0DBDC90F717A99EAD9D272855D00162EE9527567DD6A92CBD629805C0445282BBC916797FF",
+ "3081880242013BAD9F29ABE20DE37EBEB823C252CA0F63361284015A3BF430A46AAA80B87B0693F0694BD88AFE4E661FC33B094CD3B7963BED5A727ED8BD6A3A202ABE009D0367024201E9BB81FF7944CA409AD138DBBEE228E1AFCC0C890FC78EC8604639CB0DBDC90F717A99EAD9D272855D00162EE9527567DD6A92CBD629805C0445282BBC916797FF"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha224_vtable, "test",
+ "0040D09FCF3C8A5F62CF4FB223CBBB2B9937F6B0577C27020A99602C25A01136987E452988781484EDBBCF1C47E554E7FC901BC3085E5206D9F619CFF07E73D6F706",
+ "01C7ED902E123E6815546065A2C4AF977B22AA8EADDB68B2C1110E7EA44D42086BFE4A34B67DDC0E17E96536E358219B23A706C6A6E16BA77B65E1C595D43CAE17FB0177336676304FCB343CE028B38E7B4FBA76C1C1B277DA18CAD2A8478B2A9A9F5BEC0F3BA04F35DB3E4263569EC6AADE8C92746E4C82F8299AE1B8F1739F8FD519A4",
+ "308188024201C7ED902E123E6815546065A2C4AF977B22AA8EADDB68B2C1110E7EA44D42086BFE4A34B67DDC0E17E96536E358219B23A706C6A6E16BA77B65E1C595D43CAE17FB02420177336676304FCB343CE028B38E7B4FBA76C1C1B277DA18CAD2A8478B2A9A9F5BEC0F3BA04F35DB3E4263569EC6AADE8C92746E4C82F8299AE1B8F1739F8FD519A4"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha256_vtable, "test",
+ "001DE74955EFAABC4C4F17F8E84D881D1310B5392D7700275F82F145C61E843841AF09035BF7A6210F5A431A6A9E81C9323354A9E69135D44EBD2FCAA7731B909258",
+ "000E871C4A14F993C6C7369501900C4BC1E9C7B0B4BA44E04868B30B41D8071042EB28C4C250411D0CE08CD197E4188EA4876F279F90B3D8D74A3C76E6F1E4656AA800CD52DBAA33B063C3A6CD8058A1FB0A46A4754B034FCC644766CA14DA8CA5CA9FDE00E88C1AD60CCBA759025299079D7A427EC3CC5B619BFBC828E7769BCD694E86",
+ "30818702410E871C4A14F993C6C7369501900C4BC1E9C7B0B4BA44E04868B30B41D8071042EB28C4C250411D0CE08CD197E4188EA4876F279F90B3D8D74A3C76E6F1E4656AA8024200CD52DBAA33B063C3A6CD8058A1FB0A46A4754B034FCC644766CA14DA8CA5CA9FDE00E88C1AD60CCBA759025299079D7A427EC3CC5B619BFBC828E7769BCD694E86"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha384_vtable, "test",
+ "01F1FC4A349A7DA9A9E116BFDD055DC08E78252FF8E23AC276AC88B1770AE0B5DCEB1ED14A4916B769A523CE1E90BA22846AF11DF8B300C38818F713DADD85DE0C88",
+ "014BEE21A18B6D8B3C93FAB08D43E739707953244FDBE924FA926D76669E7AC8C89DF62ED8975C2D8397A65A49DCC09F6B0AC62272741924D479354D74FF6075578C0133330865C067A0EAF72362A65E2D7BC4E461E8C8995C3B6226A21BD1AA78F0ED94FE536A0DCA35534F0CD1510C41525D163FE9D74D134881E35141ED5E8E95B979",
+ "3081880242014BEE21A18B6D8B3C93FAB08D43E739707953244FDBE924FA926D76669E7AC8C89DF62ED8975C2D8397A65A49DCC09F6B0AC62272741924D479354D74FF6075578C02420133330865C067A0EAF72362A65E2D7BC4E461E8C8995C3B6226A21BD1AA78F0ED94FE536A0DCA35534F0CD1510C41525D163FE9D74D134881E35141ED5E8E95B979"
+ },
+ {
+ &EC_P521_PUB,
+ &EC_P521_PRIV,
+ &br_sha512_vtable, "test",
+ "016200813020EC986863BEDFC1B121F605C1215645018AEA1A7B215A564DE9EB1B38A67AA1128B80CE391C4FB71187654AAA3431027BFC7F395766CA988C964DC56D",
+ "013E99020ABF5CEE7525D16B69B229652AB6BDF2AFFCAEF38773B4B7D08725F10CDB93482FDCC54EDCEE91ECA4166B2A7C6265EF0CE2BD7051B7CEF945BABD47EE6D01FBD0013C674AA79CB39849527916CE301C66EA7CE8B80682786AD60F98F7E78A19CA69EFF5C57400E3B3A0AD66CE0978214D13BAF4E9AC60752F7B155E2DE4DCE3",
+ "3081880242013E99020ABF5CEE7525D16B69B229652AB6BDF2AFFCAEF38773B4B7D08725F10CDB93482FDCC54EDCEE91ECA4166B2A7C6265EF0CE2BD7051B7CEF945BABD47EE6D024201FBD0013C674AA79CB39849527916CE301C66EA7CE8B80682786AD60F98F7E78A19CA69EFF5C57400E3B3A0AD66CE0978214D13BAF4E9AC60752F7B155E2DE4DCE3"
+ },
+
+ /* Terminator for list of test vectors. */
+ {
+ 0, 0, 0, 0, 0, 0, 0
+ }
+};
+
+static void
+test_ECDSA_KAT(const br_ec_impl *iec,
+ br_ecdsa_sign sign, br_ecdsa_vrfy vrfy, int asn1)
+{
+ size_t u;
+
+ for (u = 0;; u ++) {
+ const ecdsa_kat_vector *kv;
+ unsigned char hash[64];
+ size_t hash_len;
+ unsigned char sig[150], sig2[150];
+ size_t sig_len, sig2_len;
+ br_hash_compat_context hc;
+
+ kv = &ECDSA_KAT[u];
+ if (kv->pub == 0) {
+ break;
+ }
+ kv->hf->init(&hc.vtable);
+ kv->hf->update(&hc.vtable, kv->msg, strlen(kv->msg));
+ kv->hf->out(&hc.vtable, hash);
+ hash_len = (kv->hf->desc >> BR_HASHDESC_OUT_OFF)
+ & BR_HASHDESC_OUT_MASK;
+ if (asn1) {
+ sig_len = hextobin(sig, kv->sasn1);
+ } else {
+ sig_len = hextobin(sig, kv->sraw);
+ }
+
+ if (vrfy(iec, hash, hash_len,
+ kv->pub, sig, sig_len) != 1)
+ {
+ fprintf(stderr, "ECDSA KAT verify failed (1)\n");
+ exit(EXIT_FAILURE);
+ }
+ hash[0] ^= 0x80;
+ if (vrfy(iec, hash, hash_len,
+ kv->pub, sig, sig_len) != 0)
+ {
+ fprintf(stderr, "ECDSA KAT verify shoud have failed\n");
+ exit(EXIT_FAILURE);
+ }
+ hash[0] ^= 0x80;
+ if (vrfy(iec, hash, hash_len,
+ kv->pub, sig, sig_len) != 1)
+ {
+ fprintf(stderr, "ECDSA KAT verify failed (2)\n");
+ exit(EXIT_FAILURE);
+ }
+
+ sig2_len = sign(iec, kv->hf, hash, kv->priv, sig2);
+ if (sig2_len == 0) {
+ fprintf(stderr, "ECDSA KAT sign failed\n");
+ exit(EXIT_FAILURE);
+ }
+ if (sig2_len != sig_len || memcmp(sig, sig2, sig_len) != 0) {
+ fprintf(stderr, "ECDSA KAT wrong signature value\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf(".");
+ fflush(stdout);
+ }
+}
+
+static void
+test_ECDSA_i31(void)
+{
+ printf("Test ECDSA/i31: ");
+ fflush(stdout);
+ printf("[raw]");
+ fflush(stdout);
+ test_ECDSA_KAT(&br_ec_prime_i31,
+ &br_ecdsa_i31_sign_raw, &br_ecdsa_i31_vrfy_raw, 0);
+ printf(" [asn1]");
+ fflush(stdout);
+ test_ECDSA_KAT(&br_ec_prime_i31,
+ &br_ecdsa_i31_sign_asn1, &br_ecdsa_i31_vrfy_asn1, 1);
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_ECDSA_i15(void)
+{
+ printf("Test ECDSA/i15: ");
+ fflush(stdout);
+ printf("[raw]");
+ fflush(stdout);
+ test_ECDSA_KAT(&br_ec_prime_i15,
+ &br_ecdsa_i15_sign_raw, &br_ecdsa_i15_vrfy_raw, 0);
+ printf(" [asn1]");
+ fflush(stdout);
+ test_ECDSA_KAT(&br_ec_prime_i31,
+ &br_ecdsa_i15_sign_asn1, &br_ecdsa_i15_vrfy_asn1, 1);
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_modpow_i31(void)
+{
+ br_hmac_drbg_context hc;
+ int k;
+
+ printf("Test ModPow/i31: ");
+
+ br_hmac_drbg_init(&hc, &br_sha256_vtable, "seed modpow", 11);
+ for (k = 10; k <= 500; k ++) {
+ size_t blen;
+ unsigned char bm[128], bx[128], bx1[128], bx2[128];
+ unsigned char be[128];
+ unsigned mask;
+ uint32_t x1[35], m1[35];
+ uint16_t x2[70], m2[70];
+ uint32_t tmp1[1000];
+ uint16_t tmp2[2000];
+
+ blen = (k + 7) >> 3;
+ br_hmac_drbg_generate(&hc, bm, blen);
+ br_hmac_drbg_generate(&hc, bx, blen);
+ br_hmac_drbg_generate(&hc, be, blen);
+ bm[blen - 1] |= 0x01;
+ mask = 0xFF >> ((int)(blen << 3) - k);
+ bm[0] &= mask;
+ bm[0] |= (mask - (mask >> 1));
+ bx[0] &= (mask >> 1);
+
+ br_i31_decode(m1, bm, blen);
+ br_i31_decode_mod(x1, bx, blen, m1);
+ br_i31_modpow_opt(x1, be, blen, m1, br_i31_ninv31(m1[1]),
+ tmp1, (sizeof tmp1) / (sizeof tmp1[0]));
+ br_i31_encode(bx1, blen, x1);
+
+ br_i15_decode(m2, bm, blen);
+ br_i15_decode_mod(x2, bx, blen, m2);
+ br_i15_modpow_opt(x2, be, blen, m2, br_i15_ninv15(m2[1]),
+ tmp2, (sizeof tmp2) / (sizeof tmp2[0]));
+ br_i15_encode(bx2, blen, x2);
+
+ check_equals("ModPow i31/i15", bx1, bx2, blen);
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static void
+test_modpow_i62(void)
+{
+ br_hmac_drbg_context hc;
+ int k;
+
+ printf("Test ModPow/i62: ");
+
+ br_hmac_drbg_init(&hc, &br_sha256_vtable, "seed modpow", 11);
+ for (k = 10; k <= 500; k ++) {
+ size_t blen;
+ unsigned char bm[128], bx[128], bx1[128], bx2[128];
+ unsigned char be[128];
+ unsigned mask;
+ uint32_t x1[35], m1[35];
+ uint16_t x2[70], m2[70];
+ uint64_t tmp1[500];
+ uint16_t tmp2[2000];
+
+ blen = (k + 7) >> 3;
+ br_hmac_drbg_generate(&hc, bm, blen);
+ br_hmac_drbg_generate(&hc, bx, blen);
+ br_hmac_drbg_generate(&hc, be, blen);
+ bm[blen - 1] |= 0x01;
+ mask = 0xFF >> ((int)(blen << 3) - k);
+ bm[0] &= mask;
+ bm[0] |= (mask - (mask >> 1));
+ bx[0] &= (mask >> 1);
+
+ br_i31_decode(m1, bm, blen);
+ br_i31_decode_mod(x1, bx, blen, m1);
+ br_i62_modpow_opt(x1, be, blen, m1, br_i31_ninv31(m1[1]),
+ tmp1, (sizeof tmp1) / (sizeof tmp1[0]));
+ br_i31_encode(bx1, blen, x1);
+
+ br_i15_decode(m2, bm, blen);
+ br_i15_decode_mod(x2, bx, blen, m2);
+ br_i15_modpow_opt(x2, be, blen, m2, br_i15_ninv15(m2[1]),
+ tmp2, (sizeof tmp2) / (sizeof tmp2[0]));
+ br_i15_encode(bx2, blen, x2);
+
+ check_equals("ModPow i62/i15", bx1, bx2, blen);
+
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+static int
+eq_name(const char *s1, const char *s2)
+{
+ for (;;) {
+ int c1, c2;
+
+ for (;;) {
+ c1 = *s1 ++;
+ if (c1 >= 'A' && c1 <= 'Z') {
+ c1 += 'a' - 'A';
+ } else {
+ switch (c1) {
+ case '-': case '_': case '.': case ' ':
+ continue;
+ }
+ }
+ break;
+ }
+ for (;;) {
+ c2 = *s2 ++;
+ if (c2 >= 'A' && c2 <= 'Z') {
+ c2 += 'a' - 'A';
+ } else {
+ switch (c2) {
+ case '-': case '_': case '.': case ' ':
+ continue;
+ }
+ }
+ break;
+ }
+ if (c1 != c2) {
+ return 0;
+ }
+ if (c1 == 0) {
+ return 1;
+ }
+ }
+}
+
+#define STU(x) { &test_ ## x, #x }
+
+static const struct {
+ void (*fn)(void);
+ const char *name;
+} tfns[] = {
+ STU(MD5),
+ STU(SHA1),
+ STU(SHA224),
+ STU(SHA256),
+ STU(SHA384),
+ STU(SHA512),
+ STU(MD5_SHA1),
+ STU(multihash),
+ STU(HMAC),
+ STU(HKDF),
+ STU(SHAKE),
+ STU(HMAC_DRBG),
+ STU(AESCTR_DRBG),
+ STU(PRF),
+ STU(AES_big),
+ STU(AES_small),
+ STU(AES_ct),
+ STU(AES_ct64),
+ STU(AES_pwr8),
+ STU(AES_x86ni),
+ STU(AES_CTRCBC_big),
+ STU(AES_CTRCBC_small),
+ STU(AES_CTRCBC_ct),
+ STU(AES_CTRCBC_ct64),
+ STU(AES_CTRCBC_x86ni),
+ STU(AES_CTRCBC_pwr8),
+ STU(DES_tab),
+ STU(DES_ct),
+ STU(ChaCha20_ct),
+ STU(ChaCha20_sse2),
+ STU(Poly1305_ctmul),
+ STU(Poly1305_ctmul32),
+ STU(Poly1305_ctmulq),
+ STU(Poly1305_i15),
+ STU(RSA_i15),
+ STU(RSA_i31),
+ STU(RSA_i32),
+ STU(RSA_i62),
+ STU(GHASH_ctmul),
+ STU(GHASH_ctmul32),
+ STU(GHASH_ctmul64),
+ STU(GHASH_pclmul),
+ STU(GHASH_pwr8),
+ STU(CCM),
+ STU(EAX),
+ STU(GCM),
+ STU(EC_prime_i15),
+ STU(EC_prime_i31),
+ STU(EC_p256_m15),
+ STU(EC_p256_m31),
+ STU(EC_p256_m62),
+ STU(EC_p256_m64),
+ STU(EC_c25519_i15),
+ STU(EC_c25519_i31),
+ STU(EC_c25519_m15),
+ STU(EC_c25519_m31),
+ STU(EC_c25519_m62),
+ STU(EC_c25519_m64),
+ STU(ECDSA_i15),
+ STU(ECDSA_i31),
+ STU(modpow_i31),
+ STU(modpow_i62),
+ { 0, 0 }
+};
+
+int
+main(int argc, char *argv[])
+{
+ size_t u;
+
+ if (argc <= 1) {
+ printf("usage: testcrypto all | name...\n");
+ printf("individual test names:\n");
+ for (u = 0; tfns[u].name; u ++) {
+ printf(" %s\n", tfns[u].name);
+ }
+ } else {
+ clock_prepare();
+ clock_start();
+ for (u = 0; tfns[u].name; u ++) {
+ int i;
+
+ for (i = 1; i < argc; i ++) {
+ if (eq_name(argv[i], tfns[u].name)
+ || eq_name(argv[i], "all"))
+ {
+ tfns[u].fn();
+ break;
+ }
+ }
+ }
+ clock_stop();
+ print_total_clock();
+ }
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/test/test_math.c b/test/monniaux/BearSSL/test/test_math.c
new file mode 100644
index 00000000..b36f9f78
--- /dev/null
+++ b/test/monniaux/BearSSL/test/test_math.c
@@ -0,0 +1,482 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <time.h>
+
+#include <gmp.h>
+
+#include "bearssl.h"
+#include "inner.h"
+
+/*
+ * Pointers to implementations.
+ */
+typedef struct {
+ uint32_t word_size;
+ void (*zero)(uint32_t *x, uint32_t bit_len);
+ void (*decode)(uint32_t *x, const void *src, size_t len);
+ uint32_t (*decode_mod)(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+ void (*reduce)(uint32_t *x, const uint32_t *a, const uint32_t *m);
+ void (*decode_reduce)(uint32_t *x,
+ const void *src, size_t len, const uint32_t *m);
+ void (*encode)(void *dst, size_t len, const uint32_t *x);
+ uint32_t (*add)(uint32_t *a, const uint32_t *b, uint32_t ctl);
+ uint32_t (*sub)(uint32_t *a, const uint32_t *b, uint32_t ctl);
+ uint32_t (*ninv)(uint32_t x);
+ void (*montymul)(uint32_t *d, const uint32_t *x, const uint32_t *y,
+ const uint32_t *m, uint32_t m0i);
+ void (*to_monty)(uint32_t *x, const uint32_t *m);
+ void (*from_monty)(uint32_t *x, const uint32_t *m, uint32_t m0i);
+ void (*modpow)(uint32_t *x, const unsigned char *e, size_t elen,
+ const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
+} int_impl;
+
+static const int_impl i31_impl = {
+ 31,
+ &br_i31_zero,
+ &br_i31_decode,
+ &br_i31_decode_mod,
+ &br_i31_reduce,
+ &br_i31_decode_reduce,
+ &br_i31_encode,
+ &br_i31_add,
+ &br_i31_sub,
+ &br_i31_ninv31,
+ &br_i31_montymul,
+ &br_i31_to_monty,
+ &br_i31_from_monty,
+ &br_i31_modpow
+};
+static const int_impl i32_impl = {
+ 32,
+ &br_i32_zero,
+ &br_i32_decode,
+ &br_i32_decode_mod,
+ &br_i32_reduce,
+ &br_i32_decode_reduce,
+ &br_i32_encode,
+ &br_i32_add,
+ &br_i32_sub,
+ &br_i32_ninv32,
+ &br_i32_montymul,
+ &br_i32_to_monty,
+ &br_i32_from_monty,
+ &br_i32_modpow
+};
+
+static const int_impl *impl;
+
+static gmp_randstate_t RNG;
+
+/*
+ * Get a random prime of length 'size' bits. This function also guarantees
+ * that x-1 is not a multiple of 65537.
+ */
+static void
+rand_prime(mpz_t x, int size)
+{
+ for (;;) {
+ mpz_urandomb(x, RNG, size - 1);
+ mpz_setbit(x, 0);
+ mpz_setbit(x, size - 1);
+ if (mpz_probab_prime_p(x, 50)) {
+ mpz_sub_ui(x, x, 1);
+ if (mpz_divisible_ui_p(x, 65537)) {
+ continue;
+ }
+ mpz_add_ui(x, x, 1);
+ return;
+ }
+ }
+}
+
+/*
+ * Print out a GMP integer (for debug).
+ */
+static void
+print_z(mpz_t z)
+{
+ unsigned char zb[1000];
+ size_t zlen, k;
+
+ mpz_export(zb, &zlen, 1, 1, 0, 0, z);
+ if (zlen == 0) {
+ printf(" 00");
+ return;
+ }
+ if ((zlen & 3) != 0) {
+ k = 4 - (zlen & 3);
+ memmove(zb + k, zb, zlen);
+ memset(zb, 0, k);
+ zlen += k;
+ }
+ for (k = 0; k < zlen; k += 4) {
+ printf(" %02X%02X%02X%02X",
+ zb[k], zb[k + 1], zb[k + 2], zb[k + 3]);
+ }
+}
+
+/*
+ * Print out an i31 or i32 integer (for debug).
+ */
+static void
+print_u(uint32_t *x)
+{
+ size_t k;
+
+ if (x[0] == 0) {
+ printf(" 00000000 (0, 0)");
+ return;
+ }
+ for (k = (x[0] + 31) >> 5; k > 0; k --) {
+ printf(" %08lX", (unsigned long)x[k]);
+ }
+ printf(" (%u, %u)", (unsigned)(x[0] >> 5), (unsigned)(x[0] & 31));
+}
+
+/*
+ * Check that an i31/i32 number and a GMP number are equal.
+ */
+static void
+check_eqz(uint32_t *x, mpz_t z)
+{
+ unsigned char xb[1000];
+ unsigned char zb[1000];
+ size_t xlen, zlen;
+ int good;
+
+ xlen = ((x[0] + 31) & ~(uint32_t)31) >> 3;
+ impl->encode(xb, xlen, x);
+ mpz_export(zb, &zlen, 1, 1, 0, 0, z);
+ good = 1;
+ if (xlen < zlen) {
+ good = 0;
+ } else if (xlen > zlen) {
+ size_t u;
+
+ for (u = xlen; u > zlen; u --) {
+ if (xb[xlen - u] != 0) {
+ good = 0;
+ break;
+ }
+ }
+ }
+ good = good && memcmp(xb + xlen - zlen, zb, zlen) == 0;
+ if (!good) {
+ size_t u;
+
+ printf("Mismatch:\n");
+ printf(" x = ");
+ print_u(x);
+ printf("\n");
+ printf(" ex = ");
+ for (u = 0; u < xlen; u ++) {
+ printf("%02X", xb[u]);
+ }
+ printf("\n");
+ printf(" z = ");
+ print_z(z);
+ printf("\n");
+ exit(EXIT_FAILURE);
+ }
+}
+
+/* obsolete
+static void
+mp_to_br(uint32_t *mx, uint32_t x_bitlen, mpz_t x)
+{
+ uint32_t x_ebitlen;
+ size_t xlen;
+
+ if (mpz_sizeinbase(x, 2) > x_bitlen) {
+ abort();
+ }
+ x_ebitlen = ((x_bitlen / 31) << 5) + (x_bitlen % 31);
+ br_i31_zero(mx, x_ebitlen);
+ mpz_export(mx + 1, &xlen, -1, sizeof *mx, 0, 1, x);
+}
+*/
+
+static void
+test_modint(void)
+{
+ int i, j, k;
+ mpz_t p, a, b, v, t1;
+
+ printf("Test modular integers: ");
+ fflush(stdout);
+
+ gmp_randinit_mt(RNG);
+ mpz_init(p);
+ mpz_init(a);
+ mpz_init(b);
+ mpz_init(v);
+ mpz_init(t1);
+ mpz_set_ui(t1, (unsigned long)time(NULL));
+ gmp_randseed(RNG, t1);
+ for (k = 2; k <= 128; k ++) {
+ for (i = 0; i < 10; i ++) {
+ unsigned char ep[100], ea[100], eb[100], ev[100];
+ size_t plen, alen, blen, vlen;
+ uint32_t mp[40], ma[40], mb[40], mv[60], mx[100];
+ uint32_t mt1[40], mt2[40], mt3[40];
+ uint32_t ctl;
+ uint32_t mp0i;
+
+ rand_prime(p, k);
+ mpz_urandomm(a, RNG, p);
+ mpz_urandomm(b, RNG, p);
+ mpz_urandomb(v, RNG, k + 60);
+ if (mpz_sgn(b) == 0) {
+ mpz_set_ui(b, 1);
+ }
+ mpz_export(ep, &plen, 1, 1, 0, 0, p);
+ mpz_export(ea, &alen, 1, 1, 0, 0, a);
+ mpz_export(eb, &blen, 1, 1, 0, 0, b);
+ mpz_export(ev, &vlen, 1, 1, 0, 0, v);
+
+ impl->decode(mp, ep, plen);
+ if (impl->decode_mod(ma, ea, alen, mp) != 1) {
+ printf("Decode error\n");
+ printf(" ea = ");
+ print_z(a);
+ printf("\n");
+ printf(" p = ");
+ print_u(mp);
+ printf("\n");
+ exit(EXIT_FAILURE);
+ }
+ mp0i = impl->ninv(mp[1]);
+ if (impl->decode_mod(mb, eb, blen, mp) != 1) {
+ printf("Decode error\n");
+ printf(" eb = ");
+ print_z(b);
+ printf("\n");
+ printf(" p = ");
+ print_u(mp);
+ printf("\n");
+ exit(EXIT_FAILURE);
+ }
+ impl->decode(mv, ev, vlen);
+ check_eqz(mp, p);
+ check_eqz(ma, a);
+ check_eqz(mb, b);
+ check_eqz(mv, v);
+
+ impl->decode_mod(ma, ea, alen, mp);
+ impl->decode_mod(mb, eb, blen, mp);
+ ctl = impl->add(ma, mb, 1);
+ ctl |= impl->sub(ma, mp, 0) ^ (uint32_t)1;
+ impl->sub(ma, mp, ctl);
+ mpz_add(t1, a, b);
+ mpz_mod(t1, t1, p);
+ check_eqz(ma, t1);
+
+ impl->decode_mod(ma, ea, alen, mp);
+ impl->decode_mod(mb, eb, blen, mp);
+ impl->add(ma, mp, impl->sub(ma, mb, 1));
+ mpz_sub(t1, a, b);
+ mpz_mod(t1, t1, p);
+ check_eqz(ma, t1);
+
+ impl->decode_reduce(ma, ev, vlen, mp);
+ mpz_mod(t1, v, p);
+ check_eqz(ma, t1);
+
+ impl->decode(mv, ev, vlen);
+ impl->reduce(ma, mv, mp);
+ mpz_mod(t1, v, p);
+ check_eqz(ma, t1);
+
+ impl->decode_mod(ma, ea, alen, mp);
+ impl->to_monty(ma, mp);
+ mpz_mul_2exp(t1, a, ((k + impl->word_size - 1)
+ / impl->word_size) * impl->word_size);
+ mpz_mod(t1, t1, p);
+ check_eqz(ma, t1);
+ impl->from_monty(ma, mp, mp0i);
+ check_eqz(ma, a);
+
+ impl->decode_mod(ma, ea, alen, mp);
+ impl->decode_mod(mb, eb, blen, mp);
+ impl->to_monty(ma, mp);
+ impl->montymul(mt1, ma, mb, mp, mp0i);
+ mpz_mul(t1, a, b);
+ mpz_mod(t1, t1, p);
+ check_eqz(mt1, t1);
+
+ impl->decode_mod(ma, ea, alen, mp);
+ impl->modpow(ma, ev, vlen, mp, mp0i, mt1, mt2);
+ mpz_powm(t1, a, v, p);
+ check_eqz(ma, t1);
+
+ /*
+ br_modint_decode(ma, mp, ea, alen);
+ br_modint_decode(mb, mp, eb, blen);
+ if (!br_modint_div(ma, mb, mp, mt1, mt2, mt3)) {
+ fprintf(stderr, "division failed\n");
+ exit(EXIT_FAILURE);
+ }
+ mpz_sub_ui(t1, p, 2);
+ mpz_powm(t1, b, t1, p);
+ mpz_mul(t1, a, t1);
+ mpz_mod(t1, t1, p);
+ check_eqz(ma, t1);
+
+ br_modint_decode(ma, mp, ea, alen);
+ br_modint_decode(mb, mp, eb, blen);
+ for (j = 0; j <= (2 * k + 5); j ++) {
+ br_int_add(mx, j, ma, mb);
+ mpz_add(t1, a, b);
+ mpz_tdiv_r_2exp(t1, t1, j);
+ check_eqz(mx, t1);
+
+ br_int_mul(mx, j, ma, mb);
+ mpz_mul(t1, a, b);
+ mpz_tdiv_r_2exp(t1, t1, j);
+ check_eqz(mx, t1);
+ }
+ */
+ }
+ printf(".");
+ fflush(stdout);
+ }
+ mpz_clear(p);
+ mpz_clear(a);
+ mpz_clear(b);
+ mpz_clear(v);
+ mpz_clear(t1);
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+
+#if 0
+static void
+test_RSA_core(void)
+{
+ int i, j, k;
+ mpz_t n, e, d, p, q, dp, dq, iq, t1, t2, phi;
+
+ printf("Test RSA core: ");
+ fflush(stdout);
+
+ gmp_randinit_mt(RNG);
+ mpz_init(n);
+ mpz_init(e);
+ mpz_init(d);
+ mpz_init(p);
+ mpz_init(q);
+ mpz_init(dp);
+ mpz_init(dq);
+ mpz_init(iq);
+ mpz_init(t1);
+ mpz_init(t2);
+ mpz_init(phi);
+ mpz_set_ui(t1, (unsigned long)time(NULL));
+ gmp_randseed(RNG, t1);
+
+ /*
+ * To test corner cases, we want to try RSA keys such that the
+ * lengths of both factors can be arbitrary modulo 2^32. Factors
+ * p and q need not be of the same length; p can be greater than
+ * q and q can be greater than p.
+ *
+ * To keep computation time reasonable, we use p and q factors of
+ * less than 128 bits; this is way too small for secure RSA,
+ * but enough to exercise all code paths (since we work only with
+ * 32-bit words).
+ */
+ for (i = 64; i <= 96; i ++) {
+ rand_prime(p, i);
+ for (j = i - 33; j <= i + 33; j ++) {
+ uint32_t mp[40], mq[40], mdp[40], mdq[40], miq[40];
+
+ /*
+ * Generate a RSA key pair, with p of length i bits,
+ * and q of length j bits.
+ */
+ do {
+ rand_prime(q, j);
+ } while (mpz_cmp(p, q) == 0);
+ mpz_mul(n, p, q);
+ mpz_set_ui(e, 65537);
+ mpz_sub_ui(t1, p, 1);
+ mpz_sub_ui(t2, q, 1);
+ mpz_mul(phi, t1, t2);
+ mpz_invert(d, e, phi);
+ mpz_mod(dp, d, t1);
+ mpz_mod(dq, d, t2);
+ mpz_invert(iq, q, p);
+
+ /*
+ * Convert the key pair elements to BearSSL arrays.
+ */
+ mp_to_br(mp, mpz_sizeinbase(p, 2), p);
+ mp_to_br(mq, mpz_sizeinbase(q, 2), q);
+ mp_to_br(mdp, mpz_sizeinbase(dp, 2), dp);
+ mp_to_br(mdq, mpz_sizeinbase(dq, 2), dq);
+ mp_to_br(miq, mp[0], iq);
+
+ /*
+ * Compute and check ten public/private operations.
+ */
+ for (k = 0; k < 10; k ++) {
+ uint32_t mx[40];
+
+ mpz_urandomm(t1, RNG, n);
+ mpz_powm(t2, t1, e, n);
+ mp_to_br(mx, mpz_sizeinbase(n, 2), t2);
+ br_rsa_private_core(mx, mp, mq, mdp, mdq, miq);
+ check_eqz(mx, t1);
+ }
+ }
+ printf(".");
+ fflush(stdout);
+ }
+
+ printf(" done.\n");
+ fflush(stdout);
+}
+#endif
+
+int
+main(void)
+{
+ printf("===== i32 ======\n");
+ impl = &i32_impl;
+ test_modint();
+ printf("===== i31 ======\n");
+ impl = &i31_impl;
+ test_modint();
+ /*
+ test_RSA_core();
+ */
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/test/test_speed.c b/test/monniaux/BearSSL/test/test_speed.c
new file mode 100644
index 00000000..eb1b9646
--- /dev/null
+++ b/test/monniaux/BearSSL/test/test_speed.c
@@ -0,0 +1,1772 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include "inner.h"
+
+#define HASH_SIZE(cname) br_ ## cname ## _SIZE
+
+#define SPEED_HASH(Name, cname) \
+static void \
+test_speed_ ## cname(void) \
+{ \
+ unsigned char buf[8192]; \
+ unsigned char tmp[HASH_SIZE(cname)]; \
+ br_ ## cname ## _context mc; \
+ int i; \
+ long num; \
+ \
+ memset(buf, 'T', sizeof buf); \
+ for (i = 0; i < 10; i ++) { \
+ br_ ## cname ## _init(&mc); \
+ br_ ## cname ## _update(&mc, buf, sizeof buf); \
+ br_ ## cname ## _out(&mc, tmp); \
+ } \
+ num = 10; \
+ for (;;) { \
+ clock_t begin, end; \
+ double tt; \
+ long k; \
+ \
+ br_ ## cname ## _init(&mc); \
+ begin = clock(); \
+ for (k = num; k > 0; k --) { \
+ br_ ## cname ## _update(&mc, buf, sizeof buf); \
+ } \
+ end = clock(); \
+ br_ ## cname ## _out(&mc, tmp); \
+ tt = (double)(end - begin) / CLOCKS_PER_SEC; \
+ if (tt >= 2.0) { \
+ printf("%-30s %8.2f MB/s\n", #Name, \
+ ((double)sizeof buf) * (double)num \
+ / (tt * 1000000.0)); \
+ fflush(stdout); \
+ return; \
+ } \
+ num <<= 1; \
+ } \
+}
+
+#define BLOCK_SIZE(cname) br_ ## cname ## _BLOCK_SIZE
+
+#define SPEED_BLOCKCIPHER_CBC(Name, fname, cname, klen, dir) \
+static void \
+test_speed_ ## fname(void) \
+{ \
+ unsigned char key[klen]; \
+ unsigned char buf[8192 - (8192 % BLOCK_SIZE(cname))]; \
+ unsigned char iv[BLOCK_SIZE(cname)]; \
+ const br_block_cbc ## dir ## _class *vt; \
+ br_ ## cname ## _cbc ## dir ## _keys ec; \
+ int i; \
+ long num; \
+ \
+ memset(key, 'T', sizeof key); \
+ memset(buf, 'P', sizeof buf); \
+ memset(iv, 'X', sizeof iv); \
+ vt = br_ ## cname ## _cbc ## dir ## _get_vtable(); \
+ if (vt == NULL) { \
+ printf("%-30s UNAVAILABLE\n", #Name); \
+ fflush(stdout); \
+ return; \
+ } \
+ for (i = 0; i < 10; i ++) { \
+ vt->init(&ec.vtable, key, sizeof key); \
+ vt->run(&ec.vtable, iv, buf, sizeof buf); \
+ } \
+ num = 10; \
+ for (;;) { \
+ clock_t begin, end; \
+ double tt; \
+ long k; \
+ \
+ vt->init(&ec.vtable, key, sizeof key); \
+ begin = clock(); \
+ for (k = num; k > 0; k --) { \
+ vt->run(&ec.vtable, iv, buf, sizeof buf); \
+ } \
+ end = clock(); \
+ tt = (double)(end - begin) / CLOCKS_PER_SEC; \
+ if (tt >= 2.0) { \
+ printf("%-30s %8.2f MB/s\n", #Name, \
+ ((double)sizeof buf) * (double)num \
+ / (tt * 1000000.0)); \
+ fflush(stdout); \
+ return; \
+ } \
+ num <<= 1; \
+ } \
+}
+
+#define SPEED_BLOCKCIPHER_CTR(Name, fname, cname, klen) \
+static void \
+test_speed_ ## fname(void) \
+{ \
+ unsigned char key[klen]; \
+ unsigned char buf[8192 - (8192 % BLOCK_SIZE(cname))]; \
+ unsigned char iv[BLOCK_SIZE(cname) - 4]; \
+ const br_block_ctr_class *vt; \
+ br_ ## cname ## _ctr_keys ec; \
+ int i; \
+ long num; \
+ \
+ memset(key, 'T', sizeof key); \
+ memset(buf, 'P', sizeof buf); \
+ memset(iv, 'X', sizeof iv); \
+ vt = br_ ## cname ## _ctr_get_vtable(); \
+ if (vt == NULL) { \
+ printf("%-30s UNAVAILABLE\n", #Name); \
+ fflush(stdout); \
+ return; \
+ } \
+ for (i = 0; i < 10; i ++) { \
+ vt->init(&ec.vtable, key, sizeof key); \
+ vt->run(&ec.vtable, iv, 1, buf, sizeof buf); \
+ } \
+ num = 10; \
+ for (;;) { \
+ clock_t begin, end; \
+ double tt; \
+ long k; \
+ \
+ vt->init(&ec.vtable, key, sizeof key); \
+ begin = clock(); \
+ for (k = num; k > 0; k --) { \
+ vt->run(&ec.vtable, iv, 1, buf, sizeof buf); \
+ } \
+ end = clock(); \
+ tt = (double)(end - begin) / CLOCKS_PER_SEC; \
+ if (tt >= 2.0) { \
+ printf("%-30s %8.2f MB/s\n", #Name, \
+ ((double)sizeof buf) * (double)num \
+ / (tt * 1000000.0)); \
+ fflush(stdout); \
+ return; \
+ } \
+ num <<= 1; \
+ } \
+}
+
+#define SPEED_CHACHA20(Name, fname) \
+static void \
+test_speed_ ## fname(void) \
+{ \
+ br_chacha20_run bc; \
+ unsigned char key[32]; \
+ unsigned char buf[8192]; \
+ unsigned char iv[12]; \
+ int i; \
+ long num; \
+ \
+ bc = br_ ## fname ## _get(); \
+ if (bc == 0) { \
+ printf("%-30s UNAVAILABLE\n", #Name); \
+ fflush(stdout); \
+ return; \
+ } \
+ memset(key, 'T', sizeof key); \
+ memset(buf, 'P', sizeof buf); \
+ memset(iv, 'X', sizeof iv); \
+ for (i = 0; i < 10; i ++) { \
+ bc(key, iv, i, buf, sizeof buf); \
+ } \
+ num = 10; \
+ for (;;) { \
+ clock_t begin, end; \
+ double tt; \
+ long k; \
+ \
+ begin = clock(); \
+ for (k = num; k > 0; k --) { \
+ bc(key, iv, (uint32_t)k, buf, sizeof buf); \
+ } \
+ end = clock(); \
+ tt = (double)(end - begin) / CLOCKS_PER_SEC; \
+ if (tt >= 2.0) { \
+ printf("%-30s %8.2f MB/s\n", #Name, \
+ ((double)sizeof buf) * (double)num \
+ / (tt * 1000000.0)); \
+ fflush(stdout); \
+ return; \
+ } \
+ num <<= 1; \
+ } \
+}
+
+SPEED_HASH(MD5, md5)
+SPEED_HASH(SHA-1, sha1)
+SPEED_HASH(SHA-256, sha256)
+SPEED_HASH(SHA-512, sha512)
+
+/*
+ * There are no vtable selection functions for the portable implementations,
+ * so we define some custom macros.
+ */
+#define br_aes_big_cbcenc_get_vtable() (&br_aes_big_cbcenc_vtable)
+#define br_aes_big_cbcdec_get_vtable() (&br_aes_big_cbcdec_vtable)
+#define br_aes_big_ctr_get_vtable() (&br_aes_big_ctr_vtable)
+#define br_aes_big_ctrcbc_get_vtable() (&br_aes_big_ctrcbc_vtable)
+#define br_aes_small_cbcenc_get_vtable() (&br_aes_small_cbcenc_vtable)
+#define br_aes_small_cbcdec_get_vtable() (&br_aes_small_cbcdec_vtable)
+#define br_aes_small_ctr_get_vtable() (&br_aes_small_ctr_vtable)
+#define br_aes_small_ctrcbc_get_vtable() (&br_aes_small_ctrcbc_vtable)
+#define br_aes_ct_cbcenc_get_vtable() (&br_aes_ct_cbcenc_vtable)
+#define br_aes_ct_cbcdec_get_vtable() (&br_aes_ct_cbcdec_vtable)
+#define br_aes_ct_ctr_get_vtable() (&br_aes_ct_ctr_vtable)
+#define br_aes_ct_ctrcbc_get_vtable() (&br_aes_ct_ctrcbc_vtable)
+#define br_aes_ct64_cbcenc_get_vtable() (&br_aes_ct64_cbcenc_vtable)
+#define br_aes_ct64_cbcdec_get_vtable() (&br_aes_ct64_cbcdec_vtable)
+#define br_aes_ct64_ctr_get_vtable() (&br_aes_ct64_ctr_vtable)
+#define br_aes_ct64_ctrcbc_get_vtable() (&br_aes_ct64_ctrcbc_vtable)
+#define br_chacha20_ct_get() (&br_chacha20_ct_run)
+
+#define SPEED_AES(iname) \
+SPEED_BLOCKCIPHER_CBC(AES-128 CBC encrypt (iname), aes128_ ## iname ## _cbcenc, aes_ ## iname, 16, enc) \
+SPEED_BLOCKCIPHER_CBC(AES-128 CBC decrypt (iname), aes128_ ## iname ## _cbcdec, aes_ ## iname, 16, dec) \
+SPEED_BLOCKCIPHER_CBC(AES-192 CBC encrypt (iname), aes192_ ## iname ## _cbcenc, aes_ ## iname, 24, enc) \
+SPEED_BLOCKCIPHER_CBC(AES-192 CBC decrypt (iname), aes192_ ## iname ## _cbcdec, aes_ ## iname, 24, dec) \
+SPEED_BLOCKCIPHER_CBC(AES-256 CBC encrypt (iname), aes256_ ## iname ## _cbcenc, aes_ ## iname, 32, enc) \
+SPEED_BLOCKCIPHER_CBC(AES-256 CBC decrypt (iname), aes256_ ## iname ## _cbcdec, aes_ ## iname, 32, dec) \
+SPEED_BLOCKCIPHER_CTR(AES-128 CTR (iname), aes128_ ## iname ## _ctr, aes_ ## iname, 16) \
+SPEED_BLOCKCIPHER_CTR(AES-192 CTR (iname), aes192_ ## iname ## _ctr, aes_ ## iname, 24) \
+SPEED_BLOCKCIPHER_CTR(AES-256 CTR (iname), aes256_ ## iname ## _ctr, aes_ ## iname, 32)
+
+SPEED_AES(big)
+SPEED_AES(small)
+SPEED_AES(ct)
+SPEED_AES(ct64)
+SPEED_AES(x86ni)
+SPEED_AES(pwr8)
+
+#define br_des_tab_cbcenc_get_vtable() (&br_des_tab_cbcenc_vtable)
+#define br_des_tab_cbcdec_get_vtable() (&br_des_tab_cbcdec_vtable)
+#define br_des_ct_cbcenc_get_vtable() (&br_des_ct_cbcenc_vtable)
+#define br_des_ct_cbcdec_get_vtable() (&br_des_ct_cbcdec_vtable)
+
+#define SPEED_DES(iname) \
+SPEED_BLOCKCIPHER_CBC(DES CBC encrypt (iname), des_ ## iname ## _cbcenc, des_ ## iname, 8, enc) \
+SPEED_BLOCKCIPHER_CBC(DES CBC decrypt (iname), des_ ## iname ## _cbcdec, des_ ## iname, 8, dec) \
+SPEED_BLOCKCIPHER_CBC(3DES CBC encrypt (iname), 3des_ ## iname ## _cbcenc, des_ ## iname, 24, enc) \
+SPEED_BLOCKCIPHER_CBC(3DES CBC decrypt (iname), 3des_ ## iname ## _cbcdec, des_ ## iname, 24, dec)
+
+SPEED_DES(tab)
+SPEED_DES(ct)
+
+SPEED_CHACHA20(ChaCha20 (ct), chacha20_ct)
+SPEED_CHACHA20(ChaCha20 (sse2), chacha20_sse2)
+
+static void
+test_speed_ghash_inner(char *name, br_ghash gh)
+{
+ unsigned char buf[8192], h[16], y[16];
+ int i;
+ long num;
+
+ memset(buf, 'T', sizeof buf);
+ memset(h, 'P', sizeof h);
+ memset(y, 0, sizeof y);
+ for (i = 0; i < 10; i ++) {
+ gh(y, h, buf, sizeof buf);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ gh(y, h, buf, sizeof buf);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f MB/s\n", name,
+ ((double)sizeof buf) * (double)num
+ / (tt * 1000000.0));
+ fflush(stdout);
+ return;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_ghash_ctmul(void)
+{
+ test_speed_ghash_inner("GHASH (ctmul)", &br_ghash_ctmul);
+}
+
+static void
+test_speed_ghash_ctmul32(void)
+{
+ test_speed_ghash_inner("GHASH (ctmul32)", &br_ghash_ctmul32);
+}
+
+static void
+test_speed_ghash_ctmul64(void)
+{
+ test_speed_ghash_inner("GHASH (ctmul64)", &br_ghash_ctmul64);
+}
+
+static void
+test_speed_ghash_pclmul(void)
+{
+ br_ghash gh;
+
+ gh = br_ghash_pclmul_get();
+ if (gh == 0) {
+ printf("%-30s UNAVAILABLE\n", "GHASH (pclmul)");
+ fflush(stdout);
+ } else {
+ test_speed_ghash_inner("GHASH (pclmul)", gh);
+ }
+}
+
+static void
+test_speed_ghash_pwr8(void)
+{
+ br_ghash gh;
+
+ gh = br_ghash_pwr8_get();
+ if (gh == 0) {
+ printf("%-30s UNAVAILABLE\n", "GHASH (pwr8)");
+ fflush(stdout);
+ } else {
+ test_speed_ghash_inner("GHASH (pwr8)", gh);
+ }
+}
+
+static uint32_t
+fake_chacha20(const void *key, const void *iv,
+ uint32_t cc, void *data, size_t len)
+{
+ (void)key;
+ (void)iv;
+ (void)data;
+ (void)len;
+ return cc + (uint32_t)((len + 63) >> 6);
+}
+
+/*
+ * To speed-test Poly1305, we run it with a do-nothing stub instead of
+ * ChaCha20.
+ */
+static void
+test_speed_poly1305_inner(char *name, br_poly1305_run pl)
+{
+ unsigned char buf[8192], key[32], iv[12], aad[13], tag[16];
+ int i;
+ long num;
+
+ memset(key, 'K', sizeof key);
+ memset(iv, 'I', sizeof iv);
+ memset(aad, 'A', sizeof aad);
+ memset(buf, 'T', sizeof buf);
+ for (i = 0; i < 10; i ++) {
+ pl(key, iv, buf, sizeof buf,
+ aad, sizeof aad, tag, &fake_chacha20, 0);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ pl(key, iv, buf, sizeof buf,
+ aad, sizeof aad, tag, &fake_chacha20, 0);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f MB/s\n", name,
+ ((double)sizeof buf) * (double)num
+ / (tt * 1000000.0));
+ fflush(stdout);
+ return;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_poly1305_ctmul(void)
+{
+ test_speed_poly1305_inner("Poly1305 (ctmul)", &br_poly1305_ctmul_run);
+}
+
+static void
+test_speed_poly1305_ctmul32(void)
+{
+ test_speed_poly1305_inner("Poly1305 (ctmul32)",
+ &br_poly1305_ctmul32_run);
+}
+
+static void
+test_speed_poly1305_ctmulq(void)
+{
+ br_poly1305_run bp;
+
+ bp = br_poly1305_ctmulq_get();
+ if (bp == 0) {
+ printf("%-30s UNAVAILABLE\n", "Poly1305 (ctmulq)");
+ } else {
+ test_speed_poly1305_inner("Poly1305 (ctmulq)", bp);
+ }
+}
+
+static void
+test_speed_poly1305_i15(void)
+{
+ test_speed_poly1305_inner("Poly1305 (i15)", &br_poly1305_i15_run);
+}
+
+static void
+test_speed_eax_inner(char *name,
+ const br_block_ctrcbc_class *vt, size_t key_len)
+{
+ unsigned char buf[8192], key[32], nonce[16], aad[16], tag[16];
+ int i;
+ long num;
+ br_aes_gen_ctrcbc_keys ac;
+ br_eax_context ec;
+
+ if (vt == NULL) {
+ printf("%-30s UNAVAILABLE\n", name);
+ fflush(stdout);
+ return;
+ }
+ memset(key, 'K', key_len);
+ memset(nonce, 'N', sizeof nonce);
+ memset(aad, 'A', sizeof aad);
+ memset(buf, 'T', sizeof buf);
+ for (i = 0; i < 10; i ++) {
+ vt->init(&ac.vtable, key, key_len);
+ br_eax_init(&ec, &ac.vtable);
+ br_eax_reset(&ec, nonce, sizeof nonce);
+ br_eax_aad_inject(&ec, aad, sizeof aad);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 1, buf, sizeof buf);
+ br_eax_get_tag(&ec, tag);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ vt->init(&ac.vtable, key, key_len);
+ br_eax_init(&ec, &ac.vtable);
+ br_eax_reset(&ec, nonce, sizeof nonce);
+ br_eax_aad_inject(&ec, aad, sizeof aad);
+ br_eax_flip(&ec);
+ br_eax_run(&ec, 1, buf, sizeof buf);
+ br_eax_get_tag(&ec, tag);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f MB/s\n", name,
+ ((double)sizeof buf) * (double)num
+ / (tt * 1000000.0));
+ fflush(stdout);
+ return;
+ }
+ num <<= 1;
+ }
+}
+
+#define SPEED_EAX(Algo, algo, keysize, impl) \
+static void \
+test_speed_eax_ ## algo ## keysize ## _ ## impl(void) \
+{ \
+ test_speed_eax_inner("EAX " #Algo "-" #keysize "(" #impl ")", \
+ br_ ## algo ## _ ## impl ## _ctrcbc_get_vtable() \
+ , (keysize) >> 3); \
+}
+
+SPEED_EAX(AES, aes, 128, big)
+SPEED_EAX(AES, aes, 128, small)
+SPEED_EAX(AES, aes, 128, ct)
+SPEED_EAX(AES, aes, 128, ct64)
+SPEED_EAX(AES, aes, 128, x86ni)
+SPEED_EAX(AES, aes, 128, pwr8)
+SPEED_EAX(AES, aes, 192, big)
+SPEED_EAX(AES, aes, 192, small)
+SPEED_EAX(AES, aes, 192, ct)
+SPEED_EAX(AES, aes, 192, ct64)
+SPEED_EAX(AES, aes, 192, x86ni)
+SPEED_EAX(AES, aes, 192, pwr8)
+SPEED_EAX(AES, aes, 256, big)
+SPEED_EAX(AES, aes, 256, small)
+SPEED_EAX(AES, aes, 256, ct)
+SPEED_EAX(AES, aes, 256, ct64)
+SPEED_EAX(AES, aes, 256, x86ni)
+SPEED_EAX(AES, aes, 256, pwr8)
+
+static void
+test_speed_shake_inner(int security_level)
+{
+ unsigned char buf[8192];
+ br_shake_context sc;
+ int i;
+ long num;
+
+ memset(buf, 'D', sizeof buf);
+ br_shake_init(&sc, security_level);
+ for (i = 0; i < 10; i ++) {
+ br_shake_inject(&sc, buf, sizeof buf);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_shake_inject(&sc, buf, sizeof buf);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("SHAKE%-3d (inject) %8.2f MB/s\n",
+ security_level,
+ ((double)sizeof buf) * (double)num
+ / (tt * 1000000.0));
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ br_shake_flip(&sc);
+ for (i = 0; i < 10; i ++) {
+ br_shake_produce(&sc, buf, sizeof buf);
+ }
+
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_shake_produce(&sc, buf, sizeof buf);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("SHAKE%-3d (produce) %8.2f MB/s\n",
+ security_level,
+ ((double)sizeof buf) * (double)num
+ / (tt * 1000000.0));
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_shake128(void)
+{
+ test_speed_shake_inner(128);
+}
+
+static void
+test_speed_shake256(void)
+{
+ test_speed_shake_inner(256);
+}
+
+static const unsigned char RSA_N[] = {
+ 0xE9, 0xF2, 0x4A, 0x2F, 0x96, 0xDF, 0x0A, 0x23,
+ 0x01, 0x85, 0xF1, 0x2C, 0xB2, 0xA8, 0xEF, 0x23,
+ 0xCE, 0x2E, 0xB0, 0x4E, 0x18, 0x31, 0x95, 0x5B,
+ 0x98, 0x2D, 0x9B, 0x8C, 0xE3, 0x1A, 0x2B, 0x96,
+ 0xB5, 0xC7, 0xEE, 0xED, 0x72, 0x43, 0x2D, 0xFE,
+ 0x7F, 0x61, 0x33, 0xEA, 0x14, 0xFC, 0xDE, 0x80,
+ 0x17, 0x42, 0xF0, 0xF3, 0xC3, 0xC7, 0x89, 0x47,
+ 0x76, 0x5B, 0xFA, 0x33, 0xC4, 0x8C, 0x94, 0xDE,
+ 0x6A, 0x75, 0xD8, 0x1A, 0xF4, 0x49, 0xBC, 0xF3,
+ 0xB7, 0x9E, 0x2C, 0x8D, 0xEC, 0x5A, 0xEE, 0xBF,
+ 0x4B, 0x5A, 0x7F, 0xEF, 0x21, 0x39, 0xDB, 0x1D,
+ 0x83, 0x5E, 0x7E, 0x2F, 0xAA, 0x5E, 0xBA, 0x28,
+ 0xC3, 0xA2, 0x53, 0x19, 0xFB, 0x2F, 0x78, 0x6B,
+ 0x14, 0x60, 0x49, 0x3C, 0xCC, 0x1B, 0xE9, 0x1E,
+ 0x3D, 0x10, 0xA4, 0xEB, 0x7F, 0x66, 0x98, 0xF6,
+ 0xC3, 0xAC, 0x35, 0xF5, 0x01, 0x84, 0xFF, 0x7D,
+ 0x1F, 0x72, 0xBE, 0xB4, 0xD1, 0x89, 0xC8, 0xDD,
+ 0x44, 0xE7, 0xB5, 0x2E, 0x2C, 0xE1, 0x85, 0xF5,
+ 0x15, 0x50, 0xA9, 0x08, 0xC7, 0x67, 0xD9, 0x2B,
+ 0x6C, 0x11, 0xB3, 0xEB, 0x28, 0x8D, 0xF4, 0xCC,
+ 0xE3, 0xC3, 0xC5, 0x04, 0x0E, 0x7C, 0x8D, 0xDB,
+ 0x39, 0x06, 0x6A, 0x74, 0x75, 0xDF, 0xA8, 0x0F,
+ 0xDA, 0x67, 0x5A, 0x73, 0x1E, 0xFD, 0x8E, 0x4C,
+ 0xEE, 0x17, 0xEE, 0x1E, 0x67, 0xDB, 0x98, 0x70,
+ 0x60, 0xF7, 0xB9, 0xB5, 0x1F, 0x19, 0x93, 0xD6,
+ 0x3F, 0x2F, 0x1F, 0xB6, 0x5B, 0x59, 0xAA, 0x85,
+ 0xBB, 0x25, 0xE4, 0x13, 0xEF, 0xE7, 0xB9, 0x87,
+ 0x9C, 0x3F, 0x5E, 0xE4, 0x08, 0xA3, 0x51, 0xCF,
+ 0x8B, 0xAD, 0xF4, 0xE6, 0x1A, 0x5F, 0x51, 0xDD,
+ 0xA8, 0xBE, 0xE8, 0xD1, 0x20, 0x19, 0x61, 0x6C,
+ 0x18, 0xAB, 0xCA, 0x0A, 0xD9, 0x82, 0xA6, 0x94,
+ 0xD5, 0x69, 0x2A, 0xF6, 0x43, 0x66, 0x31, 0x09
+};
+
+static const unsigned char RSA_E[] = {
+ 0x01, 0x00, 0x01
+};
+
+static const unsigned char RSA_P[] = {
+ 0xFD, 0x39, 0x40, 0x56, 0x20, 0x80, 0xC5, 0x81,
+ 0x4C, 0x5F, 0x0C, 0x1A, 0x52, 0x84, 0x03, 0x2F,
+ 0xCE, 0x82, 0xB0, 0xD8, 0x30, 0x23, 0x7F, 0x77,
+ 0x45, 0xC2, 0x01, 0xC4, 0x68, 0x96, 0x0D, 0xA7,
+ 0x22, 0xA9, 0x6C, 0xA9, 0x1A, 0x33, 0xE5, 0x2F,
+ 0xB5, 0x07, 0x9A, 0xF9, 0xEA, 0x33, 0xA5, 0xC8,
+ 0x96, 0x60, 0x6A, 0xCA, 0xEB, 0xE5, 0x6E, 0x09,
+ 0x46, 0x7E, 0x2D, 0xEF, 0x93, 0x7D, 0x56, 0xED,
+ 0x75, 0x70, 0x3B, 0x96, 0xC4, 0xD5, 0xDB, 0x0B,
+ 0x3F, 0x69, 0xDF, 0x06, 0x18, 0x76, 0xF4, 0xCF,
+ 0xF8, 0x84, 0x22, 0xDF, 0xBD, 0x71, 0x62, 0x7B,
+ 0x67, 0x99, 0xBC, 0x09, 0x95, 0x54, 0xA4, 0x98,
+ 0x83, 0xF5, 0xA9, 0xCF, 0x09, 0xA5, 0x1F, 0x61,
+ 0x25, 0xB4, 0x70, 0x6C, 0x91, 0xB8, 0xB3, 0xD0,
+ 0xCE, 0x9C, 0x45, 0x65, 0x9B, 0xEF, 0xD4, 0x70,
+ 0xBE, 0x86, 0xD2, 0x98, 0x5D, 0xEB, 0xE3, 0xFF
+};
+
+static const unsigned char RSA_Q[] = {
+ 0xEC, 0x82, 0xEE, 0x63, 0x5F, 0x40, 0x52, 0xDB,
+ 0x38, 0x7A, 0x37, 0x6A, 0x54, 0x5B, 0xD9, 0xA0,
+ 0x73, 0xB4, 0xBB, 0x52, 0xB2, 0x84, 0x07, 0xD0,
+ 0xCC, 0x82, 0x0D, 0x20, 0xB3, 0xFA, 0xD5, 0xB6,
+ 0x25, 0x92, 0x35, 0x4D, 0xB4, 0xC7, 0x36, 0x48,
+ 0xCE, 0x5E, 0x21, 0x4A, 0xA6, 0x74, 0x65, 0xF4,
+ 0x7D, 0x1D, 0xBC, 0x3B, 0xE2, 0xF4, 0x3E, 0x11,
+ 0x58, 0x10, 0x6C, 0x04, 0x46, 0x9E, 0x8D, 0x57,
+ 0xE0, 0x04, 0xE2, 0xEC, 0x47, 0xCF, 0xB3, 0x2A,
+ 0xFD, 0x4C, 0x55, 0x18, 0xDB, 0xDE, 0x3B, 0xDC,
+ 0xF4, 0x5B, 0xDA, 0xF3, 0x1A, 0xC8, 0x41, 0x6F,
+ 0x73, 0x3B, 0xFE, 0x3C, 0xA0, 0xDB, 0xBA, 0x6E,
+ 0x65, 0xA5, 0xE8, 0x02, 0xA5, 0x6C, 0xEA, 0x03,
+ 0xF6, 0x99, 0xF7, 0xCB, 0x4B, 0xB7, 0x11, 0x51,
+ 0x93, 0x88, 0x3F, 0xF9, 0x06, 0x85, 0xA9, 0x1E,
+ 0xCA, 0x64, 0xF8, 0x11, 0xA5, 0x1A, 0xCA, 0xF7
+};
+
+static const unsigned char RSA_DP[] = {
+ 0x77, 0x95, 0xE0, 0x02, 0x4C, 0x9B, 0x43, 0xAA,
+ 0xCA, 0x4C, 0x60, 0xC4, 0xD5, 0x8F, 0x2E, 0x8A,
+ 0x17, 0x36, 0xB5, 0x19, 0x83, 0xB2, 0x5F, 0xF2,
+ 0x0D, 0xE9, 0x8F, 0x38, 0x18, 0x44, 0x34, 0xF2,
+ 0x67, 0x76, 0x27, 0xB0, 0xBC, 0x85, 0x21, 0x89,
+ 0x24, 0x2F, 0x11, 0x4B, 0x51, 0x05, 0x4F, 0x17,
+ 0xA9, 0x9C, 0xA3, 0x12, 0x6D, 0xD1, 0x0D, 0xE4,
+ 0x27, 0x7C, 0x53, 0x69, 0x3E, 0xF8, 0x04, 0x63,
+ 0x64, 0x00, 0xBA, 0xC3, 0x7A, 0xF5, 0x9B, 0xDA,
+ 0x75, 0xFA, 0x23, 0xAF, 0x17, 0x42, 0xA6, 0x5E,
+ 0xC8, 0xF8, 0x6E, 0x17, 0xC7, 0xB9, 0x92, 0x4E,
+ 0xC1, 0x20, 0x63, 0x23, 0x0B, 0x78, 0xCB, 0xBA,
+ 0x93, 0x27, 0x23, 0x28, 0x79, 0x5F, 0x97, 0xB0,
+ 0x23, 0x44, 0x51, 0x8B, 0x94, 0x4D, 0xEB, 0xED,
+ 0x82, 0x85, 0x5E, 0x68, 0x9B, 0xF9, 0xE9, 0x13,
+ 0xCD, 0x86, 0x92, 0x52, 0x0E, 0x98, 0xE6, 0x35
+};
+
+static const unsigned char RSA_DQ[] = {
+ 0xD8, 0xDD, 0x71, 0xB3, 0x62, 0xBA, 0xBB, 0x7E,
+ 0xD1, 0xF9, 0x96, 0xE8, 0x83, 0xB3, 0xB9, 0x08,
+ 0x9C, 0x30, 0x03, 0x77, 0xDF, 0xC2, 0x9A, 0xDC,
+ 0x05, 0x39, 0xD6, 0xC9, 0xBE, 0xDE, 0x68, 0xA9,
+ 0xDD, 0x27, 0x84, 0x82, 0xDD, 0x19, 0xB1, 0x97,
+ 0xEE, 0xCA, 0x77, 0x22, 0x59, 0x20, 0xEF, 0xFF,
+ 0xCF, 0xDD, 0xBD, 0x24, 0xF8, 0x84, 0xD6, 0x88,
+ 0xD6, 0xC4, 0x30, 0x17, 0x77, 0x9D, 0x98, 0xA3,
+ 0x14, 0x01, 0xC7, 0x05, 0xBB, 0x0F, 0x23, 0x0D,
+ 0x6F, 0x37, 0x57, 0xEC, 0x34, 0x67, 0x41, 0x62,
+ 0xE8, 0x19, 0x75, 0xD9, 0x66, 0x1C, 0x6B, 0x8B,
+ 0xC3, 0x11, 0x26, 0x9C, 0xF7, 0x2E, 0xA3, 0x72,
+ 0xE8, 0xF7, 0xC8, 0x96, 0xEC, 0x92, 0xC2, 0xBD,
+ 0xA1, 0x98, 0x2A, 0x93, 0x99, 0xB8, 0xA2, 0x43,
+ 0xB7, 0xD0, 0xBE, 0x40, 0x1C, 0x8F, 0xE0, 0xB4,
+ 0x20, 0x07, 0x97, 0x43, 0xAE, 0xAD, 0xB3, 0x9F
+};
+
+static const unsigned char RSA_IQ[] = {
+ 0xB7, 0xE2, 0x60, 0xA9, 0x62, 0xEC, 0xEC, 0x0B,
+ 0x57, 0x02, 0x96, 0xF9, 0x36, 0x35, 0x2C, 0x37,
+ 0xAF, 0xC2, 0xEE, 0x71, 0x49, 0x26, 0x8E, 0x0F,
+ 0x27, 0xB1, 0xFA, 0x0F, 0xEA, 0xDC, 0xF0, 0x8B,
+ 0x53, 0x6C, 0xB2, 0x46, 0x27, 0xCD, 0x29, 0xA2,
+ 0x35, 0x0F, 0x5D, 0x8A, 0x3F, 0x20, 0x8C, 0x13,
+ 0x3D, 0xA1, 0xFF, 0x85, 0x91, 0x99, 0xE8, 0x50,
+ 0xED, 0xF1, 0x29, 0x00, 0xEE, 0x24, 0x90, 0xB5,
+ 0x5F, 0x3A, 0x74, 0x26, 0xD7, 0xA2, 0x24, 0x8D,
+ 0x89, 0x88, 0xD8, 0x35, 0x22, 0x22, 0x8A, 0x66,
+ 0x5D, 0x5C, 0xDE, 0x83, 0x8C, 0xFA, 0x27, 0xE6,
+ 0xB9, 0xEB, 0x72, 0x08, 0xCD, 0x53, 0x4B, 0x93,
+ 0x0F, 0xAD, 0xC3, 0xF8, 0x7C, 0xFE, 0x84, 0xD7,
+ 0x08, 0xF3, 0xBE, 0x3D, 0x60, 0x1E, 0x95, 0x8D,
+ 0x44, 0x5B, 0x65, 0x7E, 0xC1, 0x30, 0xC3, 0x84,
+ 0xC0, 0xB0, 0xFE, 0xBF, 0x28, 0x54, 0x1E, 0xC4
+};
+
+static const br_rsa_public_key RSA_PK = {
+ (void *)RSA_N, sizeof RSA_N,
+ (void *)RSA_E, sizeof RSA_E
+};
+
+static const br_rsa_private_key RSA_SK = {
+ 2048,
+ (void *)RSA_P, sizeof RSA_P,
+ (void *)RSA_Q, sizeof RSA_Q,
+ (void *)RSA_DP, sizeof RSA_DP,
+ (void *)RSA_DQ, sizeof RSA_DQ,
+ (void *)RSA_IQ, sizeof RSA_IQ
+};
+
+static void
+test_speed_rsa_inner(char *name,
+ br_rsa_public fpub, br_rsa_private fpriv, br_rsa_keygen kgen)
+{
+ unsigned char tmp[sizeof RSA_N];
+ int i;
+ long num;
+ /*
+ br_hmac_drbg_context rng;
+ */
+ br_aesctr_drbg_context rng;
+ const br_block_ctr_class *ictr;
+
+ memset(tmp, 'R', sizeof tmp);
+ tmp[0] = 0;
+ for (i = 0; i < 10; i ++) {
+ if (!fpriv(tmp, &RSA_SK)) {
+ abort();
+ }
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ fpriv(tmp, &RSA_SK);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f priv/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+ for (i = 0; i < 10; i ++) {
+ if (!fpub(tmp, sizeof tmp, &RSA_PK)) {
+ abort();
+ }
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ fpub(tmp, sizeof tmp, &RSA_PK);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f pub/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ if (kgen == 0) {
+ printf("%-30s KEYGEN UNAVAILABLE\n", name);
+ fflush(stdout);
+ return;
+ }
+ /*
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, "RSA keygen seed", 15);
+ */
+ ictr = br_aes_x86ni_ctr_get_vtable();
+ if (ictr == NULL) {
+ ictr = br_aes_pwr8_ctr_get_vtable();
+ if (ictr == NULL) {
+#if BR_64
+ ictr = &br_aes_ct64_ctr_vtable;
+#else
+ ictr = &br_aes_ct_ctr_vtable;
+#endif
+ }
+ }
+ br_aesctr_drbg_init(&rng, ictr, "RSA keygen seed", 15);
+
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_rsa_private_key sk;
+ unsigned char kbuf[BR_RSA_KBUF_PRIV_SIZE(1024)];
+
+ kgen(&rng.vtable, &sk, kbuf, NULL, NULL, 1024, 0);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 10.0) {
+ printf("%-30s %8.2f kgen[1024]/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_rsa_private_key sk;
+ unsigned char kbuf[BR_RSA_KBUF_PRIV_SIZE(2048)];
+
+ kgen(&rng.vtable, &sk, kbuf, NULL, NULL, 2048, 0);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 10.0) {
+ printf("%-30s %8.2f kgen[2048]/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_rsa_i15(void)
+{
+ test_speed_rsa_inner("RSA i15",
+ &br_rsa_i15_public, &br_rsa_i15_private, &br_rsa_i15_keygen);
+}
+
+static void
+test_speed_rsa_i31(void)
+{
+ test_speed_rsa_inner("RSA i31",
+ &br_rsa_i31_public, &br_rsa_i31_private, &br_rsa_i31_keygen);
+}
+
+static void
+test_speed_rsa_i32(void)
+{
+ test_speed_rsa_inner("RSA i32",
+ &br_rsa_i32_public, &br_rsa_i32_private, 0);
+}
+
+static void
+test_speed_rsa_i62(void)
+{
+ br_rsa_public pub;
+ br_rsa_private priv;
+ br_rsa_keygen kgen;
+
+ pub = br_rsa_i62_public_get();
+ priv = br_rsa_i62_private_get();
+ kgen = br_rsa_i62_keygen_get();
+ if (pub) {
+ test_speed_rsa_inner("RSA i62", pub, priv, kgen);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "RSA i62");
+ }
+}
+
+static void
+test_speed_ec_inner_1(const char *name,
+ const br_ec_impl *impl, const br_ec_curve_def *cd)
+{
+ unsigned char bx[80], U[160];
+ uint32_t x[22], n[22];
+ size_t nlen, ulen;
+ int i;
+ long num;
+
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ memset(bx, 'T', sizeof bx);
+ br_i31_decode_reduce(x, bx, sizeof bx, n);
+ br_i31_encode(bx, nlen, x);
+ ulen = cd->generator_len;
+ memcpy(U, cd->generator, ulen);
+ for (i = 0; i < 10; i ++) {
+ impl->mul(U, ulen, bx, nlen, cd->curve);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ impl->mul(U, ulen, bx, nlen, cd->curve);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f mul/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_ec_inner_2(const char *name,
+ const br_ec_impl *impl, const br_ec_curve_def *cd)
+{
+ unsigned char bx[80], U[160];
+ uint32_t x[22], n[22];
+ size_t nlen;
+ int i;
+ long num;
+
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ memset(bx, 'T', sizeof bx);
+ br_i31_decode_reduce(x, bx, sizeof bx, n);
+ br_i31_encode(bx, nlen, x);
+ for (i = 0; i < 10; i ++) {
+ impl->mulgen(U, bx, nlen, cd->curve);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ impl->mulgen(U, bx, nlen, cd->curve);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f mul/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_ec_inner(const char *name,
+ const br_ec_impl *impl, const br_ec_curve_def *cd)
+{
+ char tmp[50];
+
+ test_speed_ec_inner_1(name, impl, cd);
+ sprintf(tmp, "%s (FP)", name);
+ test_speed_ec_inner_2(tmp, impl, cd);
+}
+
+static void
+test_speed_ec_p256_m15(void)
+{
+ test_speed_ec_inner("EC p256_m15",
+ &br_ec_p256_m15, &br_secp256r1);
+}
+
+static void
+test_speed_ec_p256_m31(void)
+{
+ test_speed_ec_inner("EC p256_m31",
+ &br_ec_p256_m31, &br_secp256r1);
+}
+
+static void
+test_speed_ec_p256_m62(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m62_get();
+ if (ec != NULL) {
+ test_speed_ec_inner("EC p256_m62", ec, &br_secp256r1);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "EC p256_m62");
+ }
+}
+
+static void
+test_speed_ec_p256_m64(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m64_get();
+ if (ec != NULL) {
+ test_speed_ec_inner("EC p256_m64", ec, &br_secp256r1);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "EC p256_m64");
+ }
+}
+
+static void
+test_speed_ec_prime_i15(void)
+{
+ test_speed_ec_inner("EC prime_i15 P-256",
+ &br_ec_prime_i15, &br_secp256r1);
+ test_speed_ec_inner("EC prime_i15 P-384",
+ &br_ec_prime_i15, &br_secp384r1);
+ test_speed_ec_inner("EC prime_i15 P-521",
+ &br_ec_prime_i15, &br_secp521r1);
+}
+
+static void
+test_speed_ec_prime_i31(void)
+{
+ test_speed_ec_inner("EC prime_i31 P-256",
+ &br_ec_prime_i31, &br_secp256r1);
+ test_speed_ec_inner("EC prime_i31 P-384",
+ &br_ec_prime_i31, &br_secp384r1);
+ test_speed_ec_inner("EC prime_i31 P-521",
+ &br_ec_prime_i31, &br_secp521r1);
+}
+
+static void
+test_speed_ec_c25519_i15(void)
+{
+ test_speed_ec_inner("EC c25519_i15",
+ &br_ec_c25519_i15, &br_curve25519);
+}
+
+static void
+test_speed_ec_c25519_i31(void)
+{
+ test_speed_ec_inner("EC c25519_i31",
+ &br_ec_c25519_i31, &br_curve25519);
+}
+
+static void
+test_speed_ec_c25519_m15(void)
+{
+ test_speed_ec_inner("EC c25519_m15",
+ &br_ec_c25519_m15, &br_curve25519);
+}
+
+static void
+test_speed_ec_c25519_m31(void)
+{
+ test_speed_ec_inner("EC c25519_m31",
+ &br_ec_c25519_m31, &br_curve25519);
+}
+
+static void
+test_speed_ec_c25519_m62(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_c25519_m62_get();
+ if (ec != NULL) {
+ test_speed_ec_inner("EC c25519_m62", ec, &br_curve25519);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "EC c25519_m62");
+ }
+}
+
+static void
+test_speed_ec_c25519_m64(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_c25519_m64_get();
+ if (ec != NULL) {
+ test_speed_ec_inner("EC c25519_m64", ec, &br_curve25519);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "EC c25519_m64");
+ }
+}
+
+static void
+test_speed_ecdsa_inner(const char *name,
+ const br_ec_impl *impl, const br_ec_curve_def *cd,
+ br_ecdsa_sign sign, br_ecdsa_vrfy vrfy)
+{
+ unsigned char bx[80], U[160], hv[32], sig[160];
+ uint32_t x[22], n[22];
+ size_t nlen, ulen, sig_len;
+ int i;
+ long num;
+ br_ec_private_key sk;
+ br_ec_public_key pk;
+
+ nlen = cd->order_len;
+ br_i31_decode(n, cd->order, nlen);
+ memset(bx, 'T', sizeof bx);
+ br_i31_decode_reduce(x, bx, sizeof bx, n);
+ br_i31_encode(bx, nlen, x);
+ ulen = cd->generator_len;
+ memcpy(U, cd->generator, ulen);
+ impl->mul(U, ulen, bx, nlen, cd->curve);
+ sk.curve = cd->curve;
+ sk.x = bx;
+ sk.xlen = nlen;
+ pk.curve = cd->curve;
+ pk.q = U;
+ pk.qlen = ulen;
+
+ memset(hv, 'H', sizeof hv);
+ sig_len = sign(impl, &br_sha256_vtable, hv, &sk, sig);
+ if (vrfy(impl, hv, sizeof hv, &pk, sig, sig_len) != 1) {
+ fprintf(stderr, "self-test sign/verify failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (i = 0; i < 10; i ++) {
+ hv[1] ++;
+ sign(impl, &br_sha256_vtable, hv, &sk, sig);
+ vrfy(impl, hv, sizeof hv, &pk, sig, sig_len);
+ }
+
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ hv[1] ++;
+ sig_len = sign(impl, &br_sha256_vtable, hv, &sk, sig);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f sign/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ vrfy(impl, hv, sizeof hv, &pk, sig, sig_len);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f verify/s\n", name,
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_ecdsa_p256_m15(void)
+{
+ test_speed_ecdsa_inner("ECDSA m15 P-256",
+ &br_ec_p256_m15, &br_secp256r1,
+ &br_ecdsa_i15_sign_asn1,
+ &br_ecdsa_i15_vrfy_asn1);
+}
+
+static void
+test_speed_ecdsa_p256_m31(void)
+{
+ test_speed_ecdsa_inner("ECDSA m31 P-256",
+ &br_ec_p256_m31, &br_secp256r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+}
+
+static void
+test_speed_ecdsa_p256_m62(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m62_get();
+ if (ec != NULL) {
+ test_speed_ecdsa_inner("ECDSA m62 P-256",
+ ec, &br_secp256r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "ECDSA m62 P-256");
+ }
+}
+
+static void
+test_speed_ecdsa_p256_m64(void)
+{
+ const br_ec_impl *ec;
+
+ ec = br_ec_p256_m64_get();
+ if (ec != NULL) {
+ test_speed_ecdsa_inner("ECDSA m64 P-256",
+ ec, &br_secp256r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+ } else {
+ printf("%-30s UNAVAILABLE\n", "ECDSA m64 P-256");
+ }
+}
+
+static void
+test_speed_ecdsa_i15(void)
+{
+ test_speed_ecdsa_inner("ECDSA i15 P-256",
+ &br_ec_prime_i15, &br_secp256r1,
+ &br_ecdsa_i15_sign_asn1,
+ &br_ecdsa_i15_vrfy_asn1);
+ test_speed_ecdsa_inner("ECDSA i15 P-384",
+ &br_ec_prime_i15, &br_secp384r1,
+ &br_ecdsa_i15_sign_asn1,
+ &br_ecdsa_i15_vrfy_asn1);
+ test_speed_ecdsa_inner("ECDSA i15 P-521",
+ &br_ec_prime_i15, &br_secp521r1,
+ &br_ecdsa_i15_sign_asn1,
+ &br_ecdsa_i15_vrfy_asn1);
+}
+
+static void
+test_speed_ecdsa_i31(void)
+{
+ test_speed_ecdsa_inner("ECDSA i31 P-256",
+ &br_ec_prime_i31, &br_secp256r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+ test_speed_ecdsa_inner("ECDSA i31 P-384",
+ &br_ec_prime_i31, &br_secp384r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+ test_speed_ecdsa_inner("ECDSA i31 P-521",
+ &br_ec_prime_i31, &br_secp521r1,
+ &br_ecdsa_i31_sign_asn1,
+ &br_ecdsa_i31_vrfy_asn1);
+}
+
+static void
+test_speed_i31(void)
+{
+ static const unsigned char bp[] = {
+ /* A 521-bit prime integer (order of the P-521 curve). */
+ 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFA, 0x51, 0x86, 0x87, 0x83, 0xBF, 0x2F,
+ 0x96, 0x6B, 0x7F, 0xCC, 0x01, 0x48, 0xF7, 0x09,
+ 0xA5, 0xD0, 0x3B, 0xB5, 0xC9, 0xB8, 0x89, 0x9C,
+ 0x47, 0xAE, 0xBB, 0x6F, 0xB7, 0x1E, 0x91, 0x38,
+ 0x64, 0x09
+ };
+
+ unsigned char tmp[60 + sizeof bp];
+ uint32_t p[20], x[20], y[20], z[20], uu[60], p0i;
+ int i;
+ long num;
+
+ br_i31_decode(p, bp, sizeof bp);
+ p0i = br_i31_ninv31(p[1]);
+ memset(tmp, 'T', sizeof tmp);
+ br_i31_decode_reduce(x, tmp, sizeof tmp, p);
+ memset(tmp, 'U', sizeof tmp);
+ br_i31_decode_reduce(y, tmp, sizeof tmp, p);
+
+ for (i = 0; i < 10; i ++) {
+ br_i31_to_monty(x, p);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_i31_to_monty(x, p);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f ops/s\n", "i31 to_monty",
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ for (i = 0; i < 10; i ++) {
+ br_i31_from_monty(x, p, p0i);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_i31_from_monty(x, p, p0i);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f ops/s\n", "i31 from_monty",
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ for (i = 0; i < 10; i ++) {
+ br_i31_montymul(z, x, y, p, p0i);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_i31_montymul(z, x, y, p, p0i);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f ops/s\n", "i31 montymul",
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+
+ for (i = 0; i < 10; i ++) {
+ br_i31_moddiv(x, y, p, p0i, uu);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_i31_moddiv(x, y, p, p0i, uu);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f ops/s\n", "i31 moddiv",
+ (double)num / tt);
+ fflush(stdout);
+ break;
+ }
+ num <<= 1;
+ }
+}
+
+#if 0
+
+static unsigned char P2048[] = {
+ 0xFD, 0xB6, 0xE0, 0x3E, 0x00, 0x49, 0x4C, 0xF0, 0x69, 0x3A, 0xDD, 0x7D,
+ 0xF8, 0xA2, 0x41, 0xB0, 0x6C, 0x67, 0xC5, 0xBA, 0xB8, 0x46, 0x80, 0xF5,
+ 0xBF, 0xAB, 0x98, 0xFC, 0x84, 0x73, 0xA5, 0x63, 0xC9, 0x52, 0x12, 0xDA,
+ 0x4C, 0xC1, 0x5B, 0x9D, 0x8D, 0xDF, 0xCD, 0xFE, 0xC5, 0xAD, 0x5A, 0x6F,
+ 0xDD, 0x02, 0xD9, 0xEC, 0x71, 0xEF, 0xEB, 0xB6, 0x95, 0xED, 0x94, 0x25,
+ 0x0E, 0x63, 0xDD, 0x6A, 0x52, 0xC7, 0x93, 0xAF, 0x85, 0x9D, 0x2C, 0xBE,
+ 0x5C, 0xBE, 0x35, 0xD8, 0xDD, 0x39, 0xEF, 0x1B, 0xB1, 0x49, 0x67, 0xB2,
+ 0x33, 0xC9, 0x7C, 0xE1, 0x51, 0x79, 0x51, 0x59, 0xCA, 0x6E, 0x2A, 0xDF,
+ 0x0D, 0x76, 0x1C, 0xE7, 0xA5, 0xC0, 0x1E, 0x6C, 0x56, 0x3A, 0x32, 0xE5,
+ 0xB5, 0xC5, 0xD4, 0xDB, 0xFE, 0xFF, 0xF8, 0xF2, 0x96, 0xA9, 0xC9, 0x65,
+ 0x59, 0x9E, 0x01, 0x79, 0x9D, 0x38, 0x68, 0x0F, 0xAD, 0x43, 0x3A, 0xD6,
+ 0x84, 0x0A, 0xE2, 0xEF, 0x96, 0xC1, 0x6D, 0x89, 0x74, 0x19, 0x63, 0x82,
+ 0x3B, 0xA0, 0x9C, 0xBA, 0x78, 0xDE, 0xDC, 0xC2, 0xE7, 0xD4, 0xFA, 0xD6,
+ 0x19, 0x21, 0x29, 0xAE, 0x5E, 0xF4, 0x38, 0x81, 0xC6, 0x9E, 0x0E, 0x3C,
+ 0xCD, 0xC0, 0xDC, 0x93, 0x5D, 0xFD, 0x9A, 0x5C, 0xAB, 0x54, 0x1F, 0xFF,
+ 0x9C, 0x12, 0x1B, 0x4C, 0xDF, 0x2D, 0x9C, 0x85, 0xF9, 0x68, 0x15, 0x89,
+ 0x42, 0x9B, 0x6C, 0x45, 0x89, 0x3A, 0xBC, 0xE9, 0x19, 0x91, 0xBE, 0x0C,
+ 0xEF, 0x90, 0xCC, 0xF6, 0xD6, 0xF0, 0x3D, 0x5C, 0xF5, 0xE5, 0x0F, 0x2F,
+ 0x02, 0x8A, 0x83, 0x4B, 0x93, 0x2F, 0x14, 0x12, 0x1F, 0x56, 0x9A, 0x12,
+ 0x58, 0x88, 0xAE, 0x60, 0xB8, 0x5A, 0xE4, 0xA1, 0xBF, 0x4A, 0x81, 0x84,
+ 0xAB, 0xBB, 0xE4, 0xD0, 0x1D, 0x41, 0xD9, 0x0A, 0xAB, 0x1E, 0x47, 0x5B,
+ 0x31, 0xAC, 0x2B, 0x73
+};
+
+static unsigned char G2048[] = {
+ 0x02
+};
+
+static void
+test_speed_modpow(void)
+{
+ uint32_t mx[65], mp[65], me[65], t1[65], t2[65], len;
+ unsigned char e[64];
+ int i;
+ long num;
+
+ len = br_int_decode(mp, sizeof mp / sizeof mp[0],
+ P2048, sizeof P2048);
+ if (len != 65) {
+ abort();
+ }
+ memset(e, 'P', sizeof e);
+ if (!br_int_decode(me, sizeof me / sizeof me[0], e, sizeof e)) {
+ abort();
+ }
+ if (!br_modint_decode(mx, mp, G2048, sizeof G2048)) {
+ abort();
+ }
+ for (i = 0; i < 10; i ++) {
+ br_modint_to_monty(mx, mp);
+ br_modint_montypow(mx, me, mp, t1, t2);
+ br_modint_from_monty(mx, mp);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_modint_to_monty(mx, mp);
+ br_modint_montypow(mx, me, mp, t1, t2);
+ br_modint_from_monty(mx, mp);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f exp/s\n", "pow[2048:256]",
+ (double)num / tt);
+ fflush(stdout);
+ return;
+ }
+ num <<= 1;
+ }
+}
+
+static void
+test_speed_moddiv(void)
+{
+ uint32_t mx[65], my[65], mp[65], t1[65], t2[65], t3[65], len;
+ unsigned char x[255], y[255];
+ int i;
+ long num;
+
+ len = br_int_decode(mp, sizeof mp / sizeof mp[0],
+ P2048, sizeof P2048);
+ if (len != 65) {
+ abort();
+ }
+ memset(x, 'T', sizeof x);
+ memset(y, 'P', sizeof y);
+ if (!br_modint_decode(mx, mp, x, sizeof x)) {
+ abort();
+ }
+ if (!br_modint_decode(my, mp, y, sizeof y)) {
+ abort();
+ }
+ for (i = 0; i < 10; i ++) {
+ br_modint_div(mx, my, mp, t1, t2, t3);
+ }
+ num = 10;
+ for (;;) {
+ clock_t begin, end;
+ double tt;
+ long k;
+
+ begin = clock();
+ for (k = num; k > 0; k --) {
+ br_modint_div(mx, my, mp, t1, t2, t3);
+ }
+ end = clock();
+ tt = (double)(end - begin) / CLOCKS_PER_SEC;
+ if (tt >= 2.0) {
+ printf("%-30s %8.2f div/s\n", "div[2048]",
+ (double)num / tt);
+ fflush(stdout);
+ return;
+ }
+ num <<= 1;
+ }
+}
+#endif
+
+#define STU(x) { test_speed_ ## x, #x }
+
+static const struct {
+ void (*fn)(void);
+ char *name;
+} tfns[] = {
+ STU(md5),
+ STU(sha1),
+ STU(sha256),
+ STU(sha512),
+
+ STU(aes128_big_cbcenc),
+ STU(aes128_big_cbcdec),
+ STU(aes192_big_cbcenc),
+ STU(aes192_big_cbcdec),
+ STU(aes256_big_cbcenc),
+ STU(aes256_big_cbcdec),
+ STU(aes128_big_ctr),
+ STU(aes192_big_ctr),
+ STU(aes256_big_ctr),
+
+ STU(aes128_small_cbcenc),
+ STU(aes128_small_cbcdec),
+ STU(aes192_small_cbcenc),
+ STU(aes192_small_cbcdec),
+ STU(aes256_small_cbcenc),
+ STU(aes256_small_cbcdec),
+ STU(aes128_small_ctr),
+ STU(aes192_small_ctr),
+ STU(aes256_small_ctr),
+
+ STU(aes128_ct_cbcenc),
+ STU(aes128_ct_cbcdec),
+ STU(aes192_ct_cbcenc),
+ STU(aes192_ct_cbcdec),
+ STU(aes256_ct_cbcenc),
+ STU(aes256_ct_cbcdec),
+ STU(aes128_ct_ctr),
+ STU(aes192_ct_ctr),
+ STU(aes256_ct_ctr),
+
+ STU(aes128_ct64_cbcenc),
+ STU(aes128_ct64_cbcdec),
+ STU(aes192_ct64_cbcenc),
+ STU(aes192_ct64_cbcdec),
+ STU(aes256_ct64_cbcenc),
+ STU(aes256_ct64_cbcdec),
+ STU(aes128_ct64_ctr),
+ STU(aes192_ct64_ctr),
+ STU(aes256_ct64_ctr),
+
+ STU(aes128_x86ni_cbcenc),
+ STU(aes128_x86ni_cbcdec),
+ STU(aes192_x86ni_cbcenc),
+ STU(aes192_x86ni_cbcdec),
+ STU(aes256_x86ni_cbcenc),
+ STU(aes256_x86ni_cbcdec),
+ STU(aes128_x86ni_ctr),
+ STU(aes192_x86ni_ctr),
+ STU(aes256_x86ni_ctr),
+
+ STU(aes128_pwr8_cbcenc),
+ STU(aes128_pwr8_cbcdec),
+ STU(aes192_pwr8_cbcenc),
+ STU(aes192_pwr8_cbcdec),
+ STU(aes256_pwr8_cbcenc),
+ STU(aes256_pwr8_cbcdec),
+ STU(aes128_pwr8_ctr),
+ STU(aes192_pwr8_ctr),
+ STU(aes256_pwr8_ctr),
+
+ STU(des_tab_cbcenc),
+ STU(des_tab_cbcdec),
+ STU(3des_tab_cbcenc),
+ STU(3des_tab_cbcdec),
+
+ STU(des_ct_cbcenc),
+ STU(des_ct_cbcdec),
+ STU(3des_ct_cbcenc),
+ STU(3des_ct_cbcdec),
+
+ STU(chacha20_ct),
+ STU(chacha20_sse2),
+
+ STU(ghash_ctmul),
+ STU(ghash_ctmul32),
+ STU(ghash_ctmul64),
+ STU(ghash_pclmul),
+ STU(ghash_pwr8),
+
+ STU(poly1305_ctmul),
+ STU(poly1305_ctmul32),
+ STU(poly1305_ctmulq),
+ STU(poly1305_i15),
+
+ STU(eax_aes128_big),
+ STU(eax_aes192_big),
+ STU(eax_aes256_big),
+ STU(eax_aes128_small),
+ STU(eax_aes192_small),
+ STU(eax_aes256_small),
+ STU(eax_aes128_ct),
+ STU(eax_aes192_ct),
+ STU(eax_aes256_ct),
+ STU(eax_aes128_ct64),
+ STU(eax_aes192_ct64),
+ STU(eax_aes256_ct64),
+ STU(eax_aes128_x86ni),
+ STU(eax_aes192_x86ni),
+ STU(eax_aes256_x86ni),
+ STU(eax_aes128_pwr8),
+ STU(eax_aes192_pwr8),
+ STU(eax_aes256_pwr8),
+
+ STU(shake128),
+ STU(shake256),
+
+ STU(rsa_i15),
+ STU(rsa_i31),
+ STU(rsa_i32),
+ STU(rsa_i62),
+ STU(ec_prime_i15),
+ STU(ec_prime_i31),
+ STU(ec_p256_m15),
+ STU(ec_p256_m31),
+ STU(ec_p256_m62),
+ STU(ec_p256_m64),
+ STU(ec_c25519_i15),
+ STU(ec_c25519_i31),
+ STU(ec_c25519_m15),
+ STU(ec_c25519_m31),
+ STU(ec_c25519_m62),
+ STU(ec_c25519_m64),
+ STU(ecdsa_p256_m15),
+ STU(ecdsa_p256_m31),
+ STU(ecdsa_p256_m62),
+ STU(ecdsa_p256_m64),
+ STU(ecdsa_i15),
+ STU(ecdsa_i31),
+
+ STU(i31)
+};
+
+static int
+eq_name(const char *s1, const char *s2)
+{
+ for (;;) {
+ int c1, c2;
+
+ for (;;) {
+ c1 = *s1 ++;
+ if (c1 >= 'A' && c1 <= 'Z') {
+ c1 += 'a' - 'A';
+ } else {
+ switch (c1) {
+ case '-': case '_': case '.': case ' ':
+ continue;
+ }
+ }
+ break;
+ }
+ for (;;) {
+ c2 = *s2 ++;
+ if (c2 >= 'A' && c2 <= 'Z') {
+ c2 += 'a' - 'A';
+ } else {
+ switch (c2) {
+ case '-': case '_': case '.': case ' ':
+ continue;
+ }
+ }
+ break;
+ }
+ if (c1 != c2) {
+ return 0;
+ }
+ if (c1 == 0) {
+ return 1;
+ }
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t u;
+
+ if (argc <= 1) {
+ printf("usage: testspeed all | name...\n");
+ printf("individual test names:\n");
+ for (u = 0; u < (sizeof tfns) / (sizeof tfns[0]); u ++) {
+ printf(" %s\n", tfns[u].name);
+ }
+ } else {
+ for (u = 0; u < (sizeof tfns) / (sizeof tfns[0]); u ++) {
+ int i;
+
+ for (i = 1; i < argc; i ++) {
+ if (eq_name(argv[i], tfns[u].name)
+ || eq_name(argv[i], "all"))
+ {
+ tfns[u].fn();
+ break;
+ }
+ }
+ }
+ }
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/test/test_x509.c b/test/monniaux/BearSSL/test/test_x509.c
new file mode 100644
index 00000000..2c61cf5d
--- /dev/null
+++ b/test/monniaux/BearSSL/test/test_x509.c
@@ -0,0 +1,2058 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+#include "bearssl.h"
+
+#define STR(x) STR_(x)
+#define STR_(x) #x
+#ifdef SRCDIRNAME
+#define DIRNAME STR(SRCDIRNAME) "/test/x509"
+#else
+#define DIRNAME "test/x509"
+#endif
+#define CONFFILE DIRNAME "/alltests.txt"
+#define DEFAULT_TIME "2016-08-30T18:00:00Z"
+
+static void *
+xmalloc(size_t len)
+{
+ void *buf;
+
+ if (len == 0) {
+ return NULL;
+ }
+ buf = malloc(len);
+ if (buf == NULL) {
+ fprintf(stderr, "error: cannot allocate %lu byte(s)\n",
+ (unsigned long)len);
+ exit(EXIT_FAILURE);
+ }
+ return buf;
+}
+
+static void
+xfree(void *buf)
+{
+ if (buf != NULL) {
+ free(buf);
+ }
+}
+
+static char *
+xstrdup(const char *name)
+{
+ size_t n;
+ char *s;
+
+ if (name == NULL) {
+ return NULL;
+ }
+ n = strlen(name) + 1;
+ s = xmalloc(n);
+ memcpy(s, name, n);
+ return s;
+}
+
+typedef struct {
+ char *buf;
+ size_t ptr, len;
+} string_builder;
+
+static string_builder *
+SB_new(void)
+{
+ string_builder *sb;
+
+ sb = xmalloc(sizeof *sb);
+ sb->len = 8;
+ sb->buf = xmalloc(sb->len);
+ sb->ptr = 0;
+ return sb;
+}
+
+static void
+SB_expand(string_builder *sb, size_t extra_len)
+{
+ size_t nlen;
+ char *nbuf;
+
+ if (extra_len < (sb->len - sb->ptr)) {
+ return;
+ }
+ nlen = sb->len << 1;
+ if (extra_len > (nlen - sb->ptr)) {
+ nlen = sb->ptr + extra_len;
+ }
+ nbuf = xmalloc(nlen);
+ memcpy(nbuf, sb->buf, sb->ptr);
+ xfree(sb->buf);
+ sb->buf = nbuf;
+ sb->len = nlen;
+}
+
+static void
+SB_append_char(string_builder *sb, int c)
+{
+ SB_expand(sb, 1);
+ sb->buf[sb->ptr ++] = c;
+}
+
+/* unused
+static void
+SB_append_string(string_builder *sb, const char *s)
+{
+ size_t n;
+
+ n = strlen(s);
+ SB_expand(sb, n);
+ memcpy(sb->buf + sb->ptr, s, n);
+ sb->ptr += n;
+}
+*/
+
+/* unused
+static char *
+SB_to_string(string_builder *sb)
+{
+ char *s;
+
+ s = xmalloc(sb->ptr + 1);
+ memcpy(s, sb->buf, sb->ptr);
+ s[sb->ptr] = 0;
+ return s;
+}
+*/
+
+static char *
+SB_contents(string_builder *sb)
+{
+ return sb->buf;
+}
+
+static size_t
+SB_length(string_builder *sb)
+{
+ return sb->ptr;
+}
+
+static void
+SB_set_length(string_builder *sb, size_t len)
+{
+ if (sb->ptr < len) {
+ SB_expand(sb, len - sb->ptr);
+ memset(sb->buf + sb->ptr, ' ', len - sb->ptr);
+ }
+ sb->ptr = len;
+}
+
+static void
+SB_reset(string_builder *sb)
+{
+ SB_set_length(sb, 0);
+}
+
+static void
+SB_free(string_builder *sb)
+{
+ xfree(sb->buf);
+ xfree(sb);
+}
+
+typedef struct ht_elt_ {
+ char *name;
+ void *value;
+ struct ht_elt_ *next;
+} ht_elt;
+
+typedef struct {
+ size_t size;
+ ht_elt **buckets;
+ size_t num_buckets;
+} HT;
+
+static HT *
+HT_new(void)
+{
+ HT *ht;
+ size_t u;
+
+ ht = xmalloc(sizeof *ht);
+ ht->size = 0;
+ ht->num_buckets = 8;
+ ht->buckets = xmalloc(ht->num_buckets * sizeof(ht_elt *));
+ for (u = 0; u < ht->num_buckets; u ++) {
+ ht->buckets[u] = NULL;
+ }
+ return ht;
+}
+
+static uint32_t
+hash_string(const char *name)
+{
+ uint32_t hc;
+
+ hc = 0;
+ while (*name) {
+ int x;
+
+ hc = (hc << 5) - hc;
+ x = *(const unsigned char *)name;
+ if (x >= 'A' && x <= 'Z') {
+ x += 'a' - 'A';
+ }
+ hc += (uint32_t)x;
+ name ++;
+ }
+ return hc;
+}
+
+static int
+eqstring(const char *s1, const char *s2)
+{
+ while (*s1 && *s2) {
+ int x1, x2;
+
+ x1 = *(const unsigned char *)s1;
+ x2 = *(const unsigned char *)s2;
+ if (x1 >= 'A' && x1 <= 'Z') {
+ x1 += 'a' - 'A';
+ }
+ if (x2 >= 'A' && x2 <= 'Z') {
+ x2 += 'a' - 'A';
+ }
+ if (x1 != x2) {
+ return 0;
+ }
+ s1 ++;
+ s2 ++;
+ }
+ return !(*s1 || *s2);
+}
+
+static void
+HT_expand(HT *ht)
+{
+ size_t n, n2, u;
+ ht_elt **new_buckets;
+
+ n = ht->num_buckets;
+ n2 = n << 1;
+ new_buckets = xmalloc(n2 * sizeof *new_buckets);
+ for (u = 0; u < n2; u ++) {
+ new_buckets[u] = NULL;
+ }
+ for (u = 0; u < n; u ++) {
+ ht_elt *e, *f;
+
+ f = NULL;
+ for (e = ht->buckets[u]; e != NULL; e = f) {
+ uint32_t hc;
+ size_t v;
+
+ hc = hash_string(e->name);
+ v = (size_t)(hc & ((uint32_t)n2 - 1));
+ f = e->next;
+ e->next = new_buckets[v];
+ new_buckets[v] = e;
+ }
+ }
+ xfree(ht->buckets);
+ ht->buckets = new_buckets;
+ ht->num_buckets = n2;
+}
+
+static void *
+HT_put(HT *ht, const char *name, void *value)
+{
+ uint32_t hc;
+ size_t k;
+ ht_elt *e, **prev;
+
+ hc = hash_string(name);
+ k = (size_t)(hc & ((uint32_t)ht->num_buckets - 1));
+ prev = &ht->buckets[k];
+ e = *prev;
+ while (e != NULL) {
+ if (eqstring(name, e->name)) {
+ void *old_value;
+
+ old_value = e->value;
+ if (value == NULL) {
+ *prev = e->next;
+ xfree(e->name);
+ xfree(e);
+ ht->size --;
+ } else {
+ e->value = value;
+ }
+ return old_value;
+ }
+ prev = &e->next;
+ e = *prev;
+ }
+ if (value != NULL) {
+ e = xmalloc(sizeof *e);
+ e->name = xstrdup(name);
+ e->value = value;
+ e->next = ht->buckets[k];
+ ht->buckets[k] = e;
+ ht->size ++;
+ if (ht->size > ht->num_buckets) {
+ HT_expand(ht);
+ }
+ }
+ return NULL;
+}
+
+/* unused
+static void *
+HT_remove(HT *ht, const char *name)
+{
+ return HT_put(ht, name, NULL);
+}
+*/
+
+static void *
+HT_get(const HT *ht, const char *name)
+{
+ uint32_t hc;
+ size_t k;
+ ht_elt *e;
+
+ hc = hash_string(name);
+ k = (size_t)(hc & ((uint32_t)ht->num_buckets - 1));
+ for (e = ht->buckets[k]; e != NULL; e = e->next) {
+ if (eqstring(name, e->name)) {
+ return e->value;
+ }
+ }
+ return NULL;
+}
+
+static void
+HT_clear(HT *ht, void (*free_value)(void *value))
+{
+ size_t u;
+
+ for (u = 0; u < ht->num_buckets; u ++) {
+ ht_elt *e, *f;
+
+ f = NULL;
+ for (e = ht->buckets[u]; e != NULL; e = f) {
+ f = e->next;
+ xfree(e->name);
+ if (free_value != 0) {
+ free_value(e->value);
+ }
+ xfree(e);
+ }
+ ht->buckets[u] = NULL;
+ }
+ ht->size = 0;
+}
+
+static void
+HT_free(HT *ht, void (*free_value)(void *value))
+{
+ HT_clear(ht, free_value);
+ xfree(ht->buckets);
+ xfree(ht);
+}
+
+/* unused
+static size_t
+HT_size(HT *ht)
+{
+ return ht->size;
+}
+*/
+
+static unsigned char *
+read_all(FILE *f, size_t *len)
+{
+ unsigned char *buf;
+ size_t ptr, blen;
+
+ blen = 1024;
+ buf = xmalloc(blen);
+ ptr = 0;
+ for (;;) {
+ size_t rlen;
+
+ if (ptr == blen) {
+ unsigned char *buf2;
+
+ blen <<= 1;
+ buf2 = xmalloc(blen);
+ memcpy(buf2, buf, ptr);
+ xfree(buf);
+ buf = buf2;
+ }
+ rlen = fread(buf + ptr, 1, blen - ptr, f);
+ if (rlen == 0) {
+ unsigned char *buf3;
+
+ buf3 = xmalloc(ptr);
+ memcpy(buf3, buf, ptr);
+ xfree(buf);
+ *len = ptr;
+ return buf3;
+ }
+ ptr += rlen;
+ }
+}
+
+static unsigned char *
+read_file(const char *name, size_t *len)
+{
+ FILE *f;
+ unsigned char *buf;
+
+#ifdef DIRNAME
+ char *dname;
+
+ dname = xmalloc(strlen(DIRNAME) + strlen(name) + 2);
+ sprintf(dname, "%s/%s", DIRNAME, name);
+ name = dname;
+#endif
+ f = fopen(name, "rb");
+ if (f == NULL) {
+ fprintf(stderr, "could not open file '%s'\n", name);
+ exit(EXIT_FAILURE);
+ }
+ buf = read_all(f, len);
+ if (ferror(f)) {
+ fprintf(stderr, "read error on file '%s'\n", name);
+ exit(EXIT_FAILURE);
+ }
+ fclose(f);
+#ifdef DIRNAME
+ xfree(dname);
+#endif
+ return buf;
+}
+
+static int
+parse_dec(const char *s, unsigned len, int *val)
+{
+ int acc;
+
+ acc = 0;
+ while (len -- > 0) {
+ int c;
+
+ c = *s ++;
+ if (c >= '0' && c <= '9') {
+ acc = (acc * 10) + (c - '0');
+ } else {
+ return -1;
+ }
+ }
+ *val = acc;
+ return 0;
+}
+
+static int
+parse_choice(const char *s, const char *acceptable)
+{
+ int c;
+
+ c = *s;
+ while (*acceptable) {
+ if (c == *acceptable ++) {
+ return 0;
+ }
+ }
+ return -1;
+}
+
+static int
+month_length(int year, int month)
+{
+ static const int base_month_length[] = {
+ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+ };
+
+ int x;
+
+ x = base_month_length[month - 1];
+ if (month == 2 && year % 4 == 0
+ && (year % 100 != 0 || year % 400 == 0))
+ {
+ x ++;
+ }
+ return x;
+}
+
+/*
+ * Convert a time string to a days+seconds count. Returned value is 0
+ * on success, -1 on error.
+ */
+static int
+string_to_time(const char *s, uint32_t *days, uint32_t *seconds)
+{
+ int year, month, day, hour, minute, second;
+ int day_of_year, leaps, i;
+
+ if (parse_dec(s, 4, &year) < 0) {
+ return -1;
+ }
+ s += 4;
+ if (parse_choice(s ++, "-:/ ") < 0) {
+ return -1;
+ }
+ if (parse_dec(s, 2, &month) < 0) {
+ return -1;
+ }
+ s += 2;
+ if (parse_choice(s ++, "-:/ ") < 0) {
+ return -1;
+ }
+ if (parse_dec(s, 2, &day) < 0) {
+ return -1;
+ }
+ s += 2;
+ if (parse_choice(s ++, " T") < 0) {
+ return -1;
+ }
+ if (parse_dec(s, 2, &hour) < 0) {
+ return -1;
+ }
+ s += 2;
+ if (parse_choice(s ++, "-:/ ") < 0) {
+ return -1;
+ }
+ if (parse_dec(s, 2, &minute) < 0) {
+ return -1;
+ }
+ s += 2;
+ if (parse_choice(s ++, "-:/ ") < 0) {
+ return -1;
+ }
+ if (parse_dec(s, 2, &second) < 0) {
+ return -1;
+ }
+ s += 2;
+ if (*s == '.') {
+ while (*s && *s >= '0' && *s <= '9') {
+ s ++;
+ }
+ }
+ if (*s) {
+ if (*s ++ != 'Z') {
+ return -1;
+ }
+ if (*s) {
+ return -1;
+ }
+ }
+
+ if (month < 1 || month > 12) {
+ return -1;
+ }
+ day_of_year = 0;
+ for (i = 1; i < month; i ++) {
+ day_of_year += month_length(year, i);
+ }
+ if (day < 1 || day > month_length(year, month)) {
+ return -1;
+ }
+ day_of_year += (day - 1);
+ leaps = (year + 3) / 4 - (year + 99) / 100 + (year + 399) / 400;
+
+ if (hour > 23 || minute > 59 || second > 60) {
+ return -1;
+ }
+ *days = (uint32_t)year * 365 + (uint32_t)leaps + day_of_year;
+ *seconds = (uint32_t)hour * 3600 + minute * 60 + second;
+ return 0;
+}
+
+static FILE *conf;
+static int conf_delayed_char;
+static long conf_linenum;
+static string_builder *line_builder;
+static long current_linenum;
+
+static void
+conf_init(const char *fname)
+{
+ conf = fopen(fname, "r");
+ if (conf == NULL) {
+ fprintf(stderr, "could not open file '%s'\n", fname);
+ exit(EXIT_FAILURE);
+ }
+ conf_delayed_char = -1;
+ conf_linenum = 1;
+ line_builder = SB_new();
+}
+
+static void
+conf_close(void)
+{
+ if (conf != NULL) {
+ if (ferror(conf)) {
+ fprintf(stderr, "read error on configuration file\n");
+ exit(EXIT_FAILURE);
+ }
+ fclose(conf);
+ conf = NULL;
+ }
+ if (line_builder != NULL) {
+ SB_free(line_builder);
+ line_builder = NULL;
+ }
+}
+
+/*
+ * Get next character from the config file.
+ */
+static int
+conf_next_low(void)
+{
+ int x;
+
+ x = conf_delayed_char;
+ if (x >= 0) {
+ conf_delayed_char = -1;
+ } else {
+ x = fgetc(conf);
+ if (x == EOF) {
+ x = -1;
+ }
+ }
+ if (x == '\r') {
+ x = fgetc(conf);
+ if (x == EOF) {
+ x = -1;
+ }
+ if (x != '\n') {
+ conf_delayed_char = x;
+ x = '\n';
+ }
+ }
+ if (x == '\n') {
+ conf_linenum ++;
+ }
+ return x;
+}
+
+static int
+is_ws(int x)
+{
+ return x <= 32;
+}
+
+static int
+is_name_char(int c)
+{
+ return (c >= 'A' && c <= 'Z')
+ || (c >= 'a' && c <= 'z')
+ || (c >= '0' && c <= '9')
+ || (c == '_' || c == '-' || c == '.');
+}
+
+/*
+ * Read a complete line. This handles line continuation; empty lines and
+ * comment lines are skipped; leading and trailing whitespace is removed.
+ * Returned value is 0 (line read) or -1 (no line, EOF reached). The line
+ * contents are accumulated in the line_builder.
+ */
+static int
+conf_next_line(void)
+{
+ for (;;) {
+ int c;
+ int lcwb;
+
+ SB_reset(line_builder);
+
+ /*
+ * Get first non-whitespace character. This skips empty
+ * lines. Comment lines (first non-whitespace character
+ * is a semicolon) are also skipped.
+ */
+ for (;;) {
+ c = conf_next_low();
+ if (c < 0) {
+ return -1;
+ }
+ if (is_ws(c)) {
+ continue;
+ }
+ if (c == ';') {
+ for (;;) {
+ c = conf_next_low();
+ if (c < 0) {
+ return -1;
+ }
+ if (c == '\n') {
+ break;
+ }
+ }
+ continue;
+ }
+ break;
+ }
+
+ /*
+ * Read up the remaining of the line. The line continuation
+ * sequence (final backslash) is detected and processed.
+ */
+ current_linenum = conf_linenum;
+ lcwb = (c == '\\');
+ SB_append_char(line_builder, c);
+ for (;;) {
+ c = conf_next_low();
+ if (c < 0) {
+ break;
+ }
+ if (lcwb) {
+ if (c == '\n') {
+ SB_set_length(line_builder,
+ SB_length(line_builder) - 1);
+ }
+ lcwb = 0;
+ continue;
+ }
+ if (c == '\n') {
+ break;
+ } else if (c == '\\') {
+ lcwb = 1;
+ }
+ SB_append_char(line_builder, c);
+ }
+
+ /*
+ * Remove trailing whitespace (if any).
+ */
+ for (;;) {
+ size_t u;
+
+ u = SB_length(line_builder);
+ if (u == 0 || !is_ws(
+ SB_contents(line_builder)[u - 1]))
+ {
+ break;
+ }
+ SB_set_length(line_builder, u - 1);
+ }
+
+ /*
+ * We might end up with a totally empty line (in case there
+ * was a line continuation but nothing else), in which case
+ * we must loop.
+ */
+ if (SB_length(line_builder) > 0) {
+ return 0;
+ }
+ }
+}
+
+/*
+ * Test whether the current line is a section header. If yes, then the
+ * header name is extracted, and returned as a newly allocated string.
+ * Otherwise, NULL is returned.
+ */
+static char *
+parse_header_name(void)
+{
+ char *buf, *name;
+ size_t u, v, w, len;
+
+ buf = SB_contents(line_builder);
+ len = SB_length(line_builder);
+ if (len < 2 || buf[0] != '[' || buf[len - 1] != ']') {
+ return NULL;
+ }
+ u = 1;
+ v = len - 1;
+ while (u < v && is_ws(buf[u])) {
+ u ++;
+ }
+ while (u < v && is_ws(buf[v - 1])) {
+ v --;
+ }
+ if (u == v) {
+ return NULL;
+ }
+ for (w = u; w < v; w ++) {
+ if (!is_name_char(buf[w])) {
+ return NULL;
+ }
+ }
+ len = v - u;
+ name = xmalloc(len + 1);
+ memcpy(name, buf + u, len);
+ name[len] = 0;
+ return name;
+}
+
+/*
+ * Parse the current line as a 'name = value' pair. The pair is pushed into
+ * the provided hash table. On error (including a duplicate key name),
+ * this function returns -1; otherwise, it returns 0.
+ */
+static int
+parse_keyvalue(HT *d)
+{
+ char *buf, *name, *value;
+ size_t u, len;
+
+ buf = SB_contents(line_builder);
+ len = SB_length(line_builder);
+ for (u = 0; u < len; u ++) {
+ if (!is_name_char(buf[u])) {
+ break;
+ }
+ }
+ if (u == 0) {
+ return -1;
+ }
+ name = xmalloc(u + 1);
+ memcpy(name, buf, u);
+ name[u] = 0;
+ if (HT_get(d, name) != NULL) {
+ xfree(name);
+ return -1;
+ }
+ while (u < len && is_ws(buf[u])) {
+ u ++;
+ }
+ if (u >= len || buf[u] != '=') {
+ xfree(name);
+ return -1;
+ }
+ u ++;
+ while (u < len && is_ws(buf[u])) {
+ u ++;
+ }
+ value = xmalloc(len - u + 1);
+ memcpy(value, buf + u, len - u);
+ value[len - u] = 0;
+ HT_put(d, name, value);
+ xfree(name);
+ return 0;
+}
+
+/*
+ * Public keys, indexed by name. Elements are pointers to br_x509_pkey
+ * structures.
+ */
+static HT *keys;
+
+/*
+ * Trust anchors, indexed by name. Elements are pointers to
+ * test_trust_anchor structures.
+ */
+static HT *trust_anchors;
+
+typedef struct {
+ unsigned char *dn;
+ size_t dn_len;
+ unsigned flags;
+ char *key_name;
+} test_trust_anchor;
+
+/*
+ * Test case: trust anchors, certificates (file names), key type and
+ * usage, expected status and EE public key.
+ */
+typedef struct {
+ char *name;
+ char **ta_names;
+ char **cert_names;
+ char *servername;
+ unsigned key_type_usage;
+ unsigned status;
+ char *ee_key_name;
+ unsigned hashes;
+ uint32_t days, seconds;
+} test_case;
+
+static test_case *all_chains;
+static size_t all_chains_ptr, all_chains_len;
+
+static void
+free_key(void *value)
+{
+ br_x509_pkey *pk;
+
+ pk = value;
+ switch (pk->key_type) {
+ case BR_KEYTYPE_RSA:
+ xfree((void *)pk->key.rsa.n);
+ xfree((void *)pk->key.rsa.e);
+ break;
+ case BR_KEYTYPE_EC:
+ xfree((void *)pk->key.ec.q);
+ break;
+ default:
+ fprintf(stderr, "unknown key type: %d\n", pk->key_type);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ xfree(pk);
+}
+
+static void
+free_trust_anchor(void *value)
+{
+ test_trust_anchor *ttc;
+
+ ttc = value;
+ xfree(ttc->dn);
+ xfree(ttc->key_name);
+ xfree(ttc);
+}
+
+static void
+free_test_case_contents(test_case *tc)
+{
+ size_t u;
+
+ xfree(tc->name);
+ for (u = 0; tc->ta_names[u]; u ++) {
+ xfree(tc->ta_names[u]);
+ }
+ xfree(tc->ta_names);
+ for (u = 0; tc->cert_names[u]; u ++) {
+ xfree(tc->cert_names[u]);
+ }
+ xfree(tc->cert_names);
+ xfree(tc->servername);
+ xfree(tc->ee_key_name);
+}
+
+static char *
+get_value(char *objtype, HT *objdata, long linenum, char *name)
+{
+ char *value;
+
+ value = HT_get(objdata, name);
+ if (value == NULL) {
+ fprintf(stderr,
+ "missing property '%s' in section '%s' (line %ld)\n",
+ name, objtype, linenum);
+ exit(EXIT_FAILURE);
+ }
+ return value;
+}
+
+static unsigned char *
+parse_hex(const char *name, long linenum, const char *value, size_t *len)
+{
+ unsigned char *buf;
+
+ buf = NULL;
+ for (;;) {
+ size_t u, ptr;
+ int acc, z;
+
+ ptr = 0;
+ acc = 0;
+ z = 0;
+ for (u = 0; value[u]; u ++) {
+ int c;
+
+ c = value[u];
+ if (c >= '0' && c <= '9') {
+ c -= '0';
+ } else if (c >= 'A' && c <= 'F') {
+ c -= 'A' - 10;
+ } else if (c >= 'a' && c <= 'f') {
+ c -= 'a' - 10;
+ } else if (c == ' ' || c == ':') {
+ continue;
+ } else {
+ fprintf(stderr, "invalid hexadecimal character"
+ " in '%s' (line %ld)\n",
+ name, linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (z) {
+ if (buf != NULL) {
+ buf[ptr] = (acc << 4) + c;
+ }
+ ptr ++;
+ } else {
+ acc = c;
+ }
+ z = !z;
+ }
+ if (z) {
+ fprintf(stderr, "invalid hexadecimal value (partial"
+ " byte) in '%s' (line %ld)\n",
+ name, linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (buf == NULL) {
+ buf = xmalloc(ptr);
+ } else {
+ *len = ptr;
+ return buf;
+ }
+ }
+}
+
+static char **
+split_names(const char *value)
+{
+ char **names;
+ size_t len;
+
+ names = NULL;
+ len = strlen(value);
+ for (;;) {
+ size_t u, ptr;
+
+ ptr = 0;
+ u = 0;
+ while (u < len) {
+ size_t v;
+
+ while (u < len && is_ws(value[u])) {
+ u ++;
+ }
+ v = u;
+ while (v < len && !is_ws(value[v])) {
+ v ++;
+ }
+ if (v > u) {
+ if (names != NULL) {
+ char *name;
+
+ name = xmalloc(v - u + 1);
+ memcpy(name, value + u, v - u);
+ name[v - u] = 0;
+ names[ptr] = name;
+ }
+ ptr ++;
+ }
+ u = v;
+ }
+ if (names == NULL) {
+ names = xmalloc((ptr + 1) * sizeof *names);
+ } else {
+ names[ptr] = NULL;
+ return names;
+ }
+ }
+}
+
+static int
+string_to_hash(const char *name)
+{
+ char tmp[20];
+ size_t u, v;
+
+ for (u = 0, v = 0; name[u]; u ++) {
+ int c;
+
+ c = name[u];
+ if ((c >= '0' && c <= '9')
+ || (c >= 'A' && c <= 'Z')
+ || (c >= 'a' && c <= 'z'))
+ {
+ tmp[v ++] = c;
+ if (v == sizeof tmp) {
+ return -1;
+ }
+ }
+ }
+ tmp[v] = 0;
+ if (eqstring(tmp, "md5")) {
+ return br_md5_ID;
+ } else if (eqstring(tmp, "sha1")) {
+ return br_sha1_ID;
+ } else if (eqstring(tmp, "sha224")) {
+ return br_sha224_ID;
+ } else if (eqstring(tmp, "sha256")) {
+ return br_sha256_ID;
+ } else if (eqstring(tmp, "sha384")) {
+ return br_sha384_ID;
+ } else if (eqstring(tmp, "sha512")) {
+ return br_sha512_ID;
+ } else {
+ return -1;
+ }
+}
+
+static int
+string_to_curve(const char *name)
+{
+ char tmp[20];
+ size_t u, v;
+
+ for (u = 0, v = 0; name[u]; u ++) {
+ int c;
+
+ c = name[u];
+ if ((c >= '0' && c <= '9')
+ || (c >= 'A' && c <= 'Z')
+ || (c >= 'a' && c <= 'z'))
+ {
+ tmp[v ++] = c;
+ if (v == sizeof tmp) {
+ return -1;
+ }
+ }
+ }
+ tmp[v] = 0;
+ if (eqstring(tmp, "p256") || eqstring(tmp, "secp256r1")) {
+ return BR_EC_secp256r1;
+ } else if (eqstring(tmp, "p384") || eqstring(tmp, "secp384r1")) {
+ return BR_EC_secp384r1;
+ } else if (eqstring(tmp, "p521") || eqstring(tmp, "secp521r1")) {
+ return BR_EC_secp521r1;
+ } else {
+ return -1;
+ }
+}
+
+static void
+parse_object(char *objtype, HT *objdata, long linenum)
+{
+ char *name;
+
+ name = get_value(objtype, objdata, linenum, "name");
+ if (eqstring(objtype, "key")) {
+ char *stype;
+ br_x509_pkey *pk;
+
+ stype = get_value(objtype, objdata, linenum, "type");
+ pk = xmalloc(sizeof *pk);
+ if (eqstring(stype, "RSA")) {
+ char *sn, *se;
+
+ sn = get_value(objtype, objdata, linenum, "n");
+ se = get_value(objtype, objdata, linenum, "e");
+ pk->key_type = BR_KEYTYPE_RSA;
+ pk->key.rsa.n = parse_hex("modulus", linenum,
+ sn, &pk->key.rsa.nlen);
+ pk->key.rsa.e = parse_hex("exponent", linenum,
+ se, &pk->key.rsa.elen);
+ } else if (eqstring(stype, "EC")) {
+ char *sc, *sq;
+ int curve;
+
+ sc = get_value(objtype, objdata, linenum, "curve");
+ sq = get_value(objtype, objdata, linenum, "q");
+ curve = string_to_curve(sc);
+ if (curve < 0) {
+ fprintf(stderr, "unknown curve name: '%s'"
+ " (line %ld)\n", sc, linenum);
+ exit(EXIT_FAILURE);
+ }
+ pk->key_type = BR_KEYTYPE_EC;
+ pk->key.ec.curve = curve;
+ pk->key.ec.q = parse_hex("public point", linenum,
+ sq, &pk->key.ec.qlen);
+ } else {
+ fprintf(stderr, "unknown key type '%s' (line %ld)\n",
+ stype, linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (HT_put(keys, name, pk) != NULL) {
+ fprintf(stderr, "duplicate key: '%s' (line %ld)\n",
+ name, linenum);
+ exit(EXIT_FAILURE);
+ }
+ } else if (eqstring(objtype, "anchor")) {
+ char *dnfile, *kname, *tatype;
+ test_trust_anchor *tta;
+
+ dnfile = get_value(objtype, objdata, linenum, "DN_file");
+ kname = get_value(objtype, objdata, linenum, "key");
+ tatype = get_value(objtype, objdata, linenum, "type");
+ tta = xmalloc(sizeof *tta);
+ tta->dn = read_file(dnfile, &tta->dn_len);
+ tta->key_name = xstrdup(kname);
+ if (eqstring(tatype, "CA")) {
+ tta->flags = BR_X509_TA_CA;
+ } else if (eqstring(tatype, "EE")) {
+ tta->flags = 0;
+ } else {
+ fprintf(stderr,
+ "unknown trust anchor type: '%s' (line %ld)\n",
+ tatype, linenum);
+ }
+ if (HT_put(trust_anchors, name, tta) != NULL) {
+ fprintf(stderr,
+ "duplicate trust anchor: '%s' (line %ld)\n",
+ name, linenum);
+ exit(EXIT_FAILURE);
+ }
+ } else if (eqstring(objtype, "chain")) {
+ test_case tc;
+ char *ktype, *kusage, *sstatus, *shashes, *stime;
+
+ ktype = get_value(objtype, objdata, linenum, "keytype");
+ kusage = get_value(objtype, objdata, linenum, "keyusage");
+ sstatus = get_value(objtype, objdata, linenum, "status");
+ tc.name = xstrdup(name);
+ tc.ta_names = split_names(
+ get_value(objtype, objdata, linenum, "anchors"));
+ tc.cert_names = split_names(
+ get_value(objtype, objdata, linenum, "chain"));
+ tc.servername = xstrdup(HT_get(objdata, "servername"));
+ if (eqstring(ktype, "RSA")) {
+ tc.key_type_usage = BR_KEYTYPE_RSA;
+ } else if (eqstring(ktype, "EC")) {
+ tc.key_type_usage = BR_KEYTYPE_EC;
+ } else {
+ fprintf(stderr,
+ "unknown key type: '%s' (line %ld)\n",
+ ktype, linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (eqstring(kusage, "KEYX")) {
+ tc.key_type_usage |= BR_KEYTYPE_KEYX;
+ } else if (eqstring(kusage, "SIGN")) {
+ tc.key_type_usage |= BR_KEYTYPE_SIGN;
+ } else {
+ fprintf(stderr,
+ "unknown key usage: '%s' (line %ld)\n",
+ kusage, linenum);
+ exit(EXIT_FAILURE);
+ }
+ tc.status = (unsigned)atoi(sstatus);
+ if (tc.status == 0) {
+ tc.ee_key_name = xstrdup(
+ get_value(objtype, objdata, linenum, "eekey"));
+ } else {
+ tc.ee_key_name = NULL;
+ }
+ shashes = HT_get(objdata, "hashes");
+ if (shashes == NULL) {
+ tc.hashes = (unsigned)-1;
+ } else {
+ char **hns;
+ size_t u;
+
+ tc.hashes = 0;
+ hns = split_names(shashes);
+ for (u = 0;; u ++) {
+ char *hn;
+ int id;
+
+ hn = hns[u];
+ if (hn == NULL) {
+ break;
+ }
+ id = string_to_hash(hn);
+ if (id < 0) {
+ fprintf(stderr,
+ "unknown hash function '%s'"
+ " (line %ld)\n", hn, linenum);
+ exit(EXIT_FAILURE);
+ }
+ tc.hashes |= (unsigned)1 << id;
+ xfree(hn);
+ }
+ xfree(hns);
+ }
+ stime = HT_get(objdata, "time");
+ if (stime == NULL) {
+ stime = DEFAULT_TIME;
+ }
+ if (string_to_time(stime, &tc.days, &tc.seconds) < 0) {
+ fprintf(stderr, "invalid time string '%s' (line %ld)\n",
+ stime, linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (all_chains_ptr == all_chains_len) {
+ if (all_chains_len == 0) {
+ all_chains_len = 8;
+ all_chains = xmalloc(
+ all_chains_len * sizeof *all_chains);
+ } else {
+ test_case *ntc;
+ size_t nlen;
+
+ nlen = all_chains_len << 1;
+ ntc = xmalloc(nlen * sizeof *ntc);
+ memcpy(ntc, all_chains,
+ all_chains_len * sizeof *all_chains);
+ xfree(all_chains);
+ all_chains = ntc;
+ all_chains_len = nlen;
+ }
+ }
+ all_chains[all_chains_ptr ++] = tc;
+ } else {
+ fprintf(stderr, "unknown section type '%s' (line %ld)\n",
+ objtype, linenum);
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void
+process_conf_file(const char *fname)
+{
+ char *objtype;
+ HT *objdata;
+ long objlinenum;
+
+ keys = HT_new();
+ trust_anchors = HT_new();
+ all_chains = NULL;
+ all_chains_ptr = 0;
+ all_chains_len = 0;
+ conf_init(fname);
+ objtype = NULL;
+ objdata = HT_new();
+ objlinenum = 0;
+ for (;;) {
+ char *hname;
+
+ if (conf_next_line() < 0) {
+ break;
+ }
+ hname = parse_header_name();
+ if (hname != NULL) {
+ if (objtype != NULL) {
+ parse_object(objtype, objdata, objlinenum);
+ HT_clear(objdata, xfree);
+ xfree(objtype);
+ }
+ objtype = hname;
+ objlinenum = current_linenum;
+ continue;
+ }
+ if (objtype == NULL) {
+ fprintf(stderr, "no current section (line %ld)\n",
+ current_linenum);
+ exit(EXIT_FAILURE);
+ }
+ if (parse_keyvalue(objdata) < 0) {
+ fprintf(stderr, "wrong configuration, line %ld\n",
+ current_linenum);
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (objtype != NULL) {
+ parse_object(objtype, objdata, objlinenum);
+ xfree(objtype);
+ }
+ HT_free(objdata, xfree);
+ conf_close();
+}
+
+static const struct {
+ int id;
+ const br_hash_class *impl;
+} hash_impls[] = {
+ { br_md5_ID, &br_md5_vtable },
+ { br_sha1_ID, &br_sha1_vtable },
+ { br_sha224_ID, &br_sha224_vtable },
+ { br_sha256_ID, &br_sha256_vtable },
+ { br_sha384_ID, &br_sha384_vtable },
+ { br_sha512_ID, &br_sha512_vtable },
+ { 0, NULL }
+};
+
+typedef struct {
+ unsigned char *data;
+ size_t len;
+} blob;
+
+static int
+eqbigint(const unsigned char *b1, size_t b1_len,
+ const unsigned char *b2, size_t b2_len)
+{
+ while (b1_len > 0 && *b1 == 0) {
+ b1 ++;
+ b1_len --;
+ }
+ while (b2_len > 0 && *b2 == 0) {
+ b2 ++;
+ b2_len --;
+ }
+ return b1_len == b2_len && memcmp(b1, b2, b1_len) == 0;
+}
+
+static int
+eqpkey(const br_x509_pkey *pk1, const br_x509_pkey *pk2)
+{
+ if (pk1 == pk2) {
+ return 1;
+ }
+ if (pk1 == NULL || pk2 == NULL) {
+ return 0;
+ }
+ if (pk1->key_type != pk2->key_type) {
+ return 0;
+ }
+ switch (pk1->key_type) {
+ case BR_KEYTYPE_RSA:
+ return eqbigint(pk1->key.rsa.n, pk1->key.rsa.nlen,
+ pk2->key.rsa.n, pk2->key.rsa.nlen)
+ && eqbigint(pk1->key.rsa.e, pk1->key.rsa.elen,
+ pk2->key.rsa.e, pk2->key.rsa.elen);
+ case BR_KEYTYPE_EC:
+ return pk1->key.ec.curve == pk2->key.ec.curve
+ && pk1->key.ec.qlen == pk2->key.ec.qlen
+ && memcmp(pk1->key.ec.q,
+ pk2->key.ec.q, pk1->key.ec.qlen) == 0;
+ default:
+ fprintf(stderr, "unknown key type: %d\n", pk1->key_type);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ return 0;
+}
+
+static size_t max_dp_usage;
+static size_t max_rp_usage;
+
+static void
+run_test_case(test_case *tc)
+{
+ br_x509_minimal_context ctx;
+ br_x509_trust_anchor *anchors;
+ size_t num_anchors;
+ size_t u;
+ const br_hash_class *dnhash;
+ size_t num_certs;
+ blob *certs;
+ br_x509_pkey *ee_pkey_ref;
+ const br_x509_pkey *ee_pkey;
+ unsigned usages;
+ unsigned status;
+
+ printf("%s: ", tc->name);
+ fflush(stdout);
+
+ /*
+ * Get the hash function to use for hashing DN. We can use just
+ * any supported hash function, but for the elegance of things,
+ * we will use one of the hash function implementations
+ * supported for this test case (with SHA-1 as fallback).
+ */
+ dnhash = &br_sha1_vtable;
+ for (u = 0; hash_impls[u].id; u ++) {
+ if ((tc->hashes & ((unsigned)1 << (hash_impls[u].id))) != 0) {
+ dnhash = hash_impls[u].impl;
+ }
+ }
+
+ /*
+ * Get trust anchors.
+ */
+ for (num_anchors = 0; tc->ta_names[num_anchors]; num_anchors ++);
+ anchors = xmalloc(num_anchors * sizeof *anchors);
+ for (u = 0; tc->ta_names[u]; u ++) {
+ test_trust_anchor *tta;
+ br_x509_pkey *tak;
+
+ tta = HT_get(trust_anchors, tc->ta_names[u]);
+ if (tta == NULL) {
+ fprintf(stderr, "no such trust anchor: '%s'\n",
+ tc->ta_names[u]);
+ exit(EXIT_FAILURE);
+ }
+ tak = HT_get(keys, tta->key_name);
+ if (tak == NULL) {
+ fprintf(stderr, "no such public key: '%s'\n",
+ tta->key_name);
+ exit(EXIT_FAILURE);
+ }
+ anchors[u].dn.data = tta->dn;
+ anchors[u].dn.len = tta->dn_len;
+ anchors[u].flags = tta->flags;
+ anchors[u].pkey = *tak;
+ }
+
+ /*
+ * Read all relevant certificates.
+ */
+ for (num_certs = 0; tc->cert_names[num_certs]; num_certs ++);
+ certs = xmalloc(num_certs * sizeof *certs);
+ for (u = 0; u < num_certs; u ++) {
+ certs[u].data = read_file(tc->cert_names[u], &certs[u].len);
+ }
+
+ /*
+ * Get expected EE public key (if any).
+ */
+ if (tc->ee_key_name == NULL) {
+ ee_pkey_ref = NULL;
+ } else {
+ ee_pkey_ref = HT_get(keys, tc->ee_key_name);
+ if (ee_pkey_ref == NULL) {
+ fprintf(stderr, "no such public key: '%s'\n",
+ tc->ee_key_name);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Initialise the engine.
+ */
+ br_x509_minimal_init(&ctx, dnhash, anchors, num_anchors);
+ for (u = 0; hash_impls[u].id; u ++) {
+ int id;
+
+ id = hash_impls[u].id;
+ if ((tc->hashes & ((unsigned)1 << id)) != 0) {
+ br_x509_minimal_set_hash(&ctx, id, hash_impls[u].impl);
+ }
+ }
+ br_x509_minimal_set_rsa(&ctx, br_rsa_pkcs1_vrfy_get_default());
+ br_x509_minimal_set_ecdsa(&ctx,
+ br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
+
+ /*
+ * Set the validation date.
+ */
+ br_x509_minimal_set_time(&ctx, tc->days, tc->seconds);
+
+ /*
+ * Put "canaries" to detect actual stack usage.
+ */
+ for (u = 0; u < (sizeof ctx.dp_stack) / sizeof(uint32_t); u ++) {
+ ctx.dp_stack[u] = 0xA7C083FE;
+ }
+ for (u = 0; u < (sizeof ctx.rp_stack) / sizeof(uint32_t); u ++) {
+ ctx.rp_stack[u] = 0xA7C083FE;
+ }
+
+ /*
+ * Run the engine. We inject certificates by chunks of 100 bytes
+ * in order to exercise the coroutine API.
+ */
+ ctx.vtable->start_chain(&ctx.vtable, tc->servername);
+ for (u = 0; u < num_certs; u ++) {
+ size_t v;
+
+ ctx.vtable->start_cert(&ctx.vtable, certs[u].len);
+ v = 0;
+ while (v < certs[u].len) {
+ size_t w;
+
+ w = certs[u].len - v;
+ if (w > 100) {
+ w = 100;
+ }
+ ctx.vtable->append(&ctx.vtable, certs[u].data + v, w);
+ v += w;
+ }
+ ctx.vtable->end_cert(&ctx.vtable);
+ }
+ status = ctx.vtable->end_chain(&ctx.vtable);
+ ee_pkey = ctx.vtable->get_pkey(&ctx.vtable, &usages);
+
+ /*
+ * Check key type and usage.
+ */
+ if (ee_pkey != NULL) {
+ unsigned ktu;
+
+ ktu = ee_pkey->key_type | usages;
+ if (tc->key_type_usage != (ktu & tc->key_type_usage)) {
+ fprintf(stderr, "wrong key type + usage"
+ " (expected 0x%02X, got 0x%02X)\n",
+ tc->key_type_usage, ktu);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /*
+ * Check results. Note that we may still get a public key if
+ * the path is "not trusted" (but otherwise fine).
+ */
+ if (status != tc->status) {
+ fprintf(stderr, "wrong status (got %d, expected %d)\n",
+ status, tc->status);
+ exit(EXIT_FAILURE);
+ }
+ if (status == BR_ERR_X509_NOT_TRUSTED) {
+ ee_pkey = NULL;
+ }
+ if (!eqpkey(ee_pkey, ee_pkey_ref)) {
+ fprintf(stderr, "wrong EE public key\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Check stack usage.
+ */
+ for (u = (sizeof ctx.dp_stack) / sizeof(uint32_t); u > 0; u --) {
+ if (ctx.dp_stack[u - 1] != 0xA7C083FE) {
+ if (max_dp_usage < u) {
+ max_dp_usage = u;
+ }
+ break;
+ }
+ }
+ for (u = (sizeof ctx.rp_stack) / sizeof(uint32_t); u > 0; u --) {
+ if (ctx.rp_stack[u - 1] != 0xA7C083FE) {
+ if (max_rp_usage < u) {
+ max_rp_usage = u;
+ }
+ break;
+ }
+ }
+
+ /*
+ * Release everything.
+ */
+ for (u = 0; u < num_certs; u ++) {
+ xfree(certs[u].data);
+ }
+ xfree(certs);
+ xfree(anchors);
+ printf("OK\n");
+}
+
+/*
+ * A custom structure for tests, synchronised with the test certificate
+ * names.crt.
+ *
+ * If num is 1 or more, then this is a DN element with OID '1.1.1.1.num'.
+ * If num is -1 or less, then this is a SAN element of type -num.
+ * If num is 0, then this is a SAN element of type OtherName with
+ * OID 1.3.6.1.4.1.311.20.2.3 (Microsoft UPN).
+ */
+typedef struct {
+ int num;
+ int status;
+ const char *expected;
+} name_element_test;
+
+static name_element_test names_ref[] = {
+ /* === DN tests === */
+ {
+ /* [12] 66:6f:6f */
+ 1, 1, "foo"
+ },
+ {
+ /* [12] 62:61:72 */
+ 1, 1, "bar"
+ },
+ {
+ /* [18] 31:32:33:34 */
+ 2, 1, "1234"
+ },
+ {
+ /* [19] 66:6f:6f */
+ 3, 1, "foo"
+ },
+ {
+ /* [20] 66:6f:6f */
+ 4, 1, "foo"
+ },
+ {
+ /* [22] 66:6f:6f */
+ 5, 1, "foo"
+ },
+ {
+ /* [30] 00:66:00:6f:00:6f */
+ 6, 1, "foo"
+ },
+ {
+ /* [30] fe:ff:00:66:00:6f:00:6f */
+ 7, 1, "foo"
+ },
+ {
+ /* [30] ff:fe:66:00:6f:00:6f:00 */
+ 8, 1, "foo"
+ },
+ {
+ /* [20] 63:61:66:e9 */
+ 9, 1, "caf\xC3\xA9"
+ },
+ {
+ /* [12] 63:61:66:c3:a9 */
+ 10, 1, "caf\xC3\xA9"
+ },
+ {
+ /* [12] 63:61:66:e0:83:a9 */
+ 11, -1, NULL
+ },
+ {
+ /* [12] 63:61:66:e3:90:8c */
+ 12, 1, "caf\xE3\x90\x8C"
+ },
+ {
+ /* [30] 00:63:00:61:00:66:34:0c */
+ 13, 1, "caf\xE3\x90\x8C"
+ },
+ {
+ /* [12] 63:61:66:c3 */
+ 14, -1, NULL
+ },
+ {
+ /* [30] d8:42:df:f4:00:67:00:6f */
+ 15, 1, "\xF0\xA0\xAF\xB4go"
+ },
+ {
+ /* [30] 00:66:d8:42 */
+ 16, -1, NULL
+ },
+ {
+ /* [30] d8:42:00:66 */
+ 17, -1, NULL
+ },
+ {
+ /* [30] df:f4:00:66 */
+ 18, -1, NULL
+ },
+ {
+ /* [12] 66:00:6f */
+ 19, -1, NULL
+ },
+ {
+ /* [30] 00:00:34:0c */
+ 20, -1, NULL
+ },
+ {
+ /* [30] 34:0c:00:00:00:66 */
+ 21, -1, NULL
+ },
+ {
+ /* [12] ef:bb:bf:66:6f:6f */
+ 22, 1, "foo"
+ },
+ {
+ /* [30] 00:66:ff:fe:00:6f */
+ 23, -1, NULL
+ },
+ {
+ /* [30] 00:66:ff:fd:00:6f */
+ 24, 1, "f\xEF\xBF\xBDo"
+ },
+
+ /* === Value not found in the DN === */
+ {
+ 127, 0, NULL
+ },
+
+ /* === SAN tests === */
+ {
+ /* SAN OtherName (Microsoft UPN) */
+ 0, 1, "foo@bar.com"
+ },
+ {
+ /* SAN rfc822Name */
+ -1, 1, "bar@foo.com"
+ },
+ {
+ /* SAN dNSName */
+ -2, 1, "example.com"
+ },
+ {
+ /* SAN dNSName */
+ -2, 1, "www.example.com"
+ },
+ {
+ /* uniformResourceIdentifier */
+ -6, 1, "http://www.example.com/"
+ }
+};
+
+static void
+free_name_elements(br_name_element *elts, size_t num)
+{
+ size_t u;
+
+ for (u = 0; u < num; u ++) {
+ xfree((void *)elts[u].oid);
+ xfree(elts[u].buf);
+ }
+ xfree(elts);
+}
+
+static void
+test_name_extraction(void)
+{
+ unsigned char *data;
+ size_t len;
+ br_x509_minimal_context ctx;
+ uint32_t days, seconds;
+ size_t u;
+ unsigned status;
+ br_name_element *names;
+ size_t num_names;
+ int good;
+
+ printf("Name extraction: ");
+ fflush(stdout);
+ data = read_file("names.crt", &len);
+ br_x509_minimal_init(&ctx, &br_sha256_vtable, NULL, 0);
+ for (u = 0; hash_impls[u].id; u ++) {
+ int id;
+
+ id = hash_impls[u].id;
+ br_x509_minimal_set_hash(&ctx, id, hash_impls[u].impl);
+ }
+ br_x509_minimal_set_rsa(&ctx, br_rsa_pkcs1_vrfy_get_default());
+ br_x509_minimal_set_ecdsa(&ctx,
+ br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
+ string_to_time(DEFAULT_TIME, &days, &seconds);
+ br_x509_minimal_set_time(&ctx, days, seconds);
+
+ num_names = (sizeof names_ref) / (sizeof names_ref[0]);
+ names = xmalloc(num_names * sizeof *names);
+ for (u = 0; u < num_names; u ++) {
+ int num;
+ unsigned char *oid;
+
+ num = names_ref[u].num;
+ if (num > 0) {
+ oid = xmalloc(5);
+ oid[0] = 4;
+ oid[1] = 0x29;
+ oid[2] = 0x01;
+ oid[3] = 0x01;
+ oid[4] = num;
+ } else if (num == 0) {
+ oid = xmalloc(13);
+ oid[0] = 0x00;
+ oid[1] = 0x00;
+ oid[2] = 0x0A;
+ oid[3] = 0x2B;
+ oid[4] = 0x06;
+ oid[5] = 0x01;
+ oid[6] = 0x04;
+ oid[7] = 0x01;
+ oid[8] = 0x82;
+ oid[9] = 0x37;
+ oid[10] = 0x14;
+ oid[11] = 0x02;
+ oid[12] = 0x03;
+ } else {
+ oid = xmalloc(2);
+ oid[0] = 0x00;
+ oid[1] = -num;
+ }
+ names[u].oid = oid;
+ names[u].buf = xmalloc(256);
+ names[u].len = 256;
+ }
+ br_x509_minimal_set_name_elements(&ctx, names, num_names);
+
+ /*
+ * Put "canaries" to detect actual stack usage.
+ */
+ for (u = 0; u < (sizeof ctx.dp_stack) / sizeof(uint32_t); u ++) {
+ ctx.dp_stack[u] = 0xA7C083FE;
+ }
+ for (u = 0; u < (sizeof ctx.rp_stack) / sizeof(uint32_t); u ++) {
+ ctx.rp_stack[u] = 0xA7C083FE;
+ }
+
+ /*
+ * Run the engine. Since we set no trust anchor, we expect a status
+ * of "not trusted".
+ */
+ ctx.vtable->start_chain(&ctx.vtable, NULL);
+ ctx.vtable->start_cert(&ctx.vtable, len);
+ ctx.vtable->append(&ctx.vtable, data, len);
+ ctx.vtable->end_cert(&ctx.vtable);
+ status = ctx.vtable->end_chain(&ctx.vtable);
+ if (status != BR_ERR_X509_NOT_TRUSTED) {
+ fprintf(stderr, "wrong status: %u\n", status);
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * Check stack usage.
+ */
+ for (u = (sizeof ctx.dp_stack) / sizeof(uint32_t); u > 0; u --) {
+ if (ctx.dp_stack[u - 1] != 0xA7C083FE) {
+ if (max_dp_usage < u) {
+ max_dp_usage = u;
+ }
+ break;
+ }
+ }
+ for (u = (sizeof ctx.rp_stack) / sizeof(uint32_t); u > 0; u --) {
+ if (ctx.rp_stack[u - 1] != 0xA7C083FE) {
+ if (max_rp_usage < u) {
+ max_rp_usage = u;
+ }
+ break;
+ }
+ }
+
+ good = 1;
+ for (u = 0; u < num_names; u ++) {
+ if (names[u].status != names_ref[u].status) {
+ printf("ERR: name %u (id=%d): status=%d, expected=%d\n",
+ (unsigned)u, names_ref[u].num,
+ names[u].status, names_ref[u].status);
+ if (names[u].status > 0) {
+ unsigned char *p;
+
+ printf(" obtained:");
+ p = (unsigned char *)names[u].buf;
+ while (*p) {
+ printf(" %02X", *p ++);
+ }
+ printf("\n");
+ }
+ good = 0;
+ continue;
+ }
+ if (names_ref[u].expected == NULL) {
+ if (names[u].buf[0] != 0) {
+ printf("ERR: name %u not zero-terminated\n",
+ (unsigned)u);
+ good = 0;
+ continue;
+ }
+ } else {
+ if (strcmp(names[u].buf, names_ref[u].expected) != 0) {
+ unsigned char *p;
+
+ printf("ERR: name %u (id=%d): wrong value\n",
+ (unsigned)u, names_ref[u].num);
+ printf(" expected:");
+ p = (unsigned char *)names_ref[u].expected;
+ while (*p) {
+ printf(" %02X", *p ++);
+ }
+ printf("\n");
+ printf(" obtained:");
+ p = (unsigned char *)names[u].buf;
+ while (*p) {
+ printf(" %02X", *p ++);
+ }
+ printf("\n");
+ good = 0;
+ continue;
+ }
+ }
+ }
+ if (!good) {
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ for (u = 0; u < num_names; u ++) {
+ printf("%u: (%d)", (unsigned)u, names[u].status);
+ if (names[u].status > 0) {
+ size_t v;
+
+ for (v = 0; names[u].buf[v]; v ++) {
+ printf(" %02x", names[u].buf[v]);
+ }
+ }
+ printf("\n");
+ }
+ */
+
+ xfree(data);
+ free_name_elements(names, num_names);
+ printf("OK\n");
+}
+
+int
+main(int argc, const char *argv[])
+{
+ size_t u;
+
+#ifdef SRCDIRNAME
+ /*
+ * We want to change the current directory to that of the
+ * executable, so that test files are reliably located. We
+ * do that only if SRCDIRNAME is defined (old Makefile would
+ * not do that).
+ */
+ if (argc >= 1) {
+ const char *arg, *c;
+
+ arg = argv[0];
+ for (c = arg + strlen(arg);; c --) {
+ int sep, r;
+
+#ifdef _WIN32
+ sep = (*c == '/') || (*c == '\\');
+#else
+ sep = (*c == '/');
+#endif
+ if (sep) {
+ size_t len;
+ char *dn;
+
+ len = 1 + (c - arg);
+ dn = xmalloc(len + 1);
+ memcpy(dn, arg, len);
+ dn[len] = 0;
+#ifdef _WIN32
+ r = _chdir(dn);
+#else
+ r = chdir(dn);
+#endif
+ if (r != 0) {
+ fprintf(stderr, "warning: could not"
+ " set directory to '%s'\n", dn);
+ }
+ xfree(dn);
+ break;
+ }
+ if (c == arg) {
+ break;
+ }
+ }
+ }
+#else
+ (void)argc;
+ (void)argv;
+#endif
+
+ process_conf_file(CONFFILE);
+
+ max_dp_usage = 0;
+ max_rp_usage = 0;
+ for (u = 0; u < all_chains_ptr; u ++) {
+ run_test_case(&all_chains[u]);
+ }
+ test_name_extraction();
+
+ printf("Maximum data stack usage: %u\n", (unsigned)max_dp_usage);
+ printf("Maximum return stack usage: %u\n", (unsigned)max_rp_usage);
+
+ HT_free(keys, free_key);
+ HT_free(trust_anchors, free_trust_anchor);
+ for (u = 0; u < all_chains_ptr; u ++) {
+ free_test_case_contents(&all_chains[u]);
+ }
+ xfree(all_chains);
+
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/test/x509/alltests.txt b/test/monniaux/BearSSL/test/x509/alltests.txt
new file mode 100644
index 00000000..a635a639
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/alltests.txt
@@ -0,0 +1,722 @@
+; Most/all of these test chains use the same structure:
+; root -> ica1 -> ica2 -> ee
+; "ica1" is "Intermediate CA 1"
+; "ee" is "end-entity", i.e. the client or server certificate itself
+;
+; In SSL/TLS order, the EE comes first. The root may or may not be included
+; as a self-signed certificate.
+
+[key]
+name = root-rsa2048
+type = RSA
+n = B6D934D450FDB3AF7A73F1CE38BF5D6F45E1FD4EB198C6608326D217D1C5B79AA3C1DE6339979CF05E5CC81C17B988196DF0B62E3050A1546E93C0DBCF30CB9F1E2779F1C3995235AA3DB6DFB0AD7CCB49CDC0EDE766102AE9CE281F2150FA774C2DDAEF3C58EB4EBFCEE9FB1ADAA383A3CDA3CA9380DCDAF317CC7AAB33809CB2D47F463FC53CDC6194B727296E2ABC5B0936D4C63B0DEBBECEDB1D1CBC106A7171B3F2CA289A77F28AEC42EFB14A8EE2F21A322ACDC0A6462C9AC28537917F46A19381A17466DFBAB339209193FA1DA1A885E7E4F907F610F6A82701B67F12C340C3C9E2B0AB49183A64B659B795B59636DF2269AA726A544E2729A30E9715
+e = 010001
+
+[key]
+name = root-p256
+type = EC
+curve = P-256
+q = 047174BAABB9302E81D5E557F9F320680C9CF964DBB4200D6DEA40D04A6E42FDB69A682544F6DF7BC4FCDEDD7BBBC5DB7C763F4166406EDBA787C2E5D8C5F37F8D
+
+[key]
+name = root-p384
+type = EC
+curve = P-384
+q = 040ED28B3F7F0A38A6DB72CB4DAC8198C3D595BFABEE2E4A3CC6797F1A272C57AD715F96B5FDA29C4DD87B75B1438B6A92C4FD0282A3080A857F28AB31FF8B49F805470A01EE551F7F27C914E7E780AE474558D6F5539BAE806626514FE560478B
+
+[key]
+name = root-p521
+type = EC
+curve = P-521
+q = 040168E669615D1B20F2E753D2C86312F51094D3E5C6CF49E8D73418278CD769FE40A84AD4F34865D59D94D5685B389E0CFD0450754CAE81ED1D4A91D0773F7A002ED701DEF2DBDEFC7554E74CD600693DBDE1A7E09CD9044774C744C7CE575BF8B645FF79FCCE06116F61D44FDAE62D3046F4EB41DECB8219B279A5B8CE2A47F3DF0D463B
+
+[key]
+name = root-new
+type = EC
+curve = P-256
+q = 0465D02336D3ACEB9A000B33A6EECA9745EFD72A0F7C0B138FAAA564E705A3269A479BB5A041DC1D244EA1D2BB9639C79187D3D63CEF79EDD1DC65E80027E75997
+
+[key]
+name = ica1-rsa2048
+type = RSA
+n = B3E86BAF9C1652E3810C50AB25CECC0DC7F21F7F50DF2C5C35D6622E632741A7E453A84B27FA1391A3FA094A2F3B5ECF77B38AC1CD49959C750D6474EFE4D74BB9A19B68D2307148EAF74B14DF3F47A9D8BBEC8F28CCFADFB41F947C96FC080528F9E8F42F2FEE629C8A3AE0855860B60F2D30B4C04154914C1F5FADF119F0C022A67DD83F793459427B5BB541C4647F52CF3C3722A12F7925942441C23FFAC775FB48B50D18A7F454F32E6ED84358C4AB50E805AD91B61E0175B3549CDEA09915FBACF15C974951CCEF58126F736BB33414010F5A9DFAAAD693D3E2EAC3ABBC4EEDCC51A1B8F894B6B42CA8862B1FF6514329525E1389B36A78604E4EC01BA5
+e = 010001
+
+[key]
+name = ica2-rsa2048
+type = RSA
+n = AE15F7CBEEE3961BCA63D22681B2D8163423735684FCFDB2E98E9DD0D9D0D706A191EF8D4F604E16BDE6529EE557867B7A7FFCBC34AD86EC9150ADD5C7D18D83E95ABA2FDB0DB92131FAA2FD91EC37836261809C6A82253309DF8F7893EACFDB93B0A2687CEA873E369C4B379A71E52084C3789A2BA42C7E76D561A9131272F14B411BC6A555BA9D8965C06699C0F17C9B61B24E6601B0A9C4FDC8C1D0C789BE2746DE6271BA27F52A850F436026BC2A9D07AAD608DC26D86956A1D308DED858936B0EC2AF783E2574D49F001820BFD7158DB9D13D8900A01264E186C0D580F124B2D2FB4C677CAE3DC9BF47026DE47C0D4490518CCD026237F86FB96701C695
+e = 010001
+
+[key]
+name = ee-rsa2048
+type = RSA
+n = D47A1D27BA2B3A67B2916AFBE78344CAED1C75ADDD4D8362D6AA6895B224217B15AE2A996815ED66F0B858E7D3F52EC6D92A5EE70E2EE7FC6759C0C8617D4BA46FDD9FD9C8858764C7BA1A0F29D496A8789A6B6220A932D0EEA98C286147A2502A63F621DEDAD8D5F07FC5008270E6A3BF5C89274F51927703C3B0CC2E3BEC23F22F5341AF8993FFD280B14397DED619A092127A3D6679E1C1BCE17770A28B3D4684533FE44E424137921E1FFD38B3F7EF873980D356CFF4E013DE64B072A40384C441ED6FFA3EE2CA0420D2D7DC2C822B7AE26DA11C48DBCF894F34973D28A853DAE7C1E17315A330767F8F2342143D5134D25AAD3C9BCBC8FE7F6E8E40F3BD
+e = 010001
+
+[key]
+name = ee-p256
+type = EC
+curve = P-256
+q = 045F389DA7FF4D8AAFF63439461AFC3ADFF423AAA9EAFBC508DE008EBE79A537584C6DDD01CAAB47DF89B6C7171F38FC1D2014DD45C0E08F934E380BFCE999A149
+
+[key]
+name = ee-p384
+type = EC
+curve = P-384
+q = 0415A488877F3D14830E29A1C2F2C0745CE8CF5E684304D1668972389BA615B34E9648D5A7861E49DFFFBFFFEAD7FC6AF11BC4516C3557332DD86DDFDE2A236CCEA844EBD594CCD3ED5B7AE0061BD6595737B59FE754BCDAB6FE38D34D93DBBF30
+
+[key]
+name = ee-p521
+type = EC
+curve = P-521
+q = 040060547ACA9D520FB3272833236CBF8E71AC286A3001FBB1E2C3FD8BAB0817DDE4E4FA53550F120D678F4D55AE4FF36C7C8EAE9E32A08A44FC66F45331E08946077A0139B87FE54B986012A94838C8006034941CD0512E596436D2E8E61CA93585D5C06EAD5094585B5B2A3E013803B3E6AAA1D4156EF09E8352029BB70AC6BF338F918B
+
+; Trust anchor: the root.
+[anchor]
+name = root
+DN_file = dn-root.der
+key = root-rsa2048
+type = CA
+
+; Trust anchor: root with an ECDSA key (in P-256 curve)
+[anchor]
+name = root-p256
+DN_file = dn-root.der
+key = root-p256
+type = CA
+
+; Trust anchor: root with an ECDSA key (in P-384 curve)
+[anchor]
+name = root-p384
+DN_file = dn-root.der
+key = root-p384
+type = CA
+
+; Trust anchor: root with an ECDSA key (in P-521 curve)
+[anchor]
+name = root-p521
+DN_file = dn-root.der
+key = root-p521
+type = CA
+
+; Trust anchor: another root with an ECDSA key (in P-256 curve)
+[anchor]
+name = root-new
+DN_file = dn-root-new.der
+key = root-new
+type = CA
+
+; Intermediate CA 1 as trust anchor.
+[anchor]
+name = ica1
+DN_file = dn-ica1.der
+key = ica1-rsa2048
+type = CA
+
+; Intermediate CA 2 as trust anchor.
+[anchor]
+name = ica2
+DN_file = dn-ica2.der
+key = ica2-rsa2048
+type = CA
+
+; EE certificate as trust anchor (direct trust only).
+[anchor]
+name = ee
+DN_file = dn-ee.der
+key = ee-rsa2048
+type = EE
+
+; Base valid chain.
+[chain]
+name = base
+anchors = root
+chain = ee.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Valid chain except that no trust anchor is provided; this should fail
+; with BR_ERR_X509_NOT_TRUSTED.
+[chain]
+name = noTA
+anchors =
+chain = ee.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 62
+
+; Use of intermediate CA 1 as anchor (extra certificates are ignored).
+[chain]
+name = anchorICA1
+anchors = ica1
+chain = ee.crt ica2.crt junk.crt junk.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Use of intermediate CA 2 as anchor (extra certificates are ignored).
+[chain]
+name = anchorICA2
+anchors = ica2
+chain = ee.crt junk.crt junk.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Direct trust of EE.
+[chain]
+name = directTrust
+anchors = ee
+chain = ee.crt junk.crt junk.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Server name check: name does not match the SAN nor the CN.
+[chain]
+name = wrongName1
+anchors = root
+chain = ee.crt ica2.crt ica1.crt
+servername = foo.example.com
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Server name check: name matches the CN but not the SAN, and there is
+; a SAN so the CN is ignored.
+[chain]
+name = wrongName2
+anchors = root
+chain = ee-names.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Server name check: name does not match CN, but matches the first SAN
+; name.
+[chain]
+name = goodName1
+anchors = root
+chain = ee-names.crt ica2.crt ica1.crt
+servername = foo.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Server name check: name does not match CN, but matches the second SAN
+; name.
+[chain]
+name = goodName2
+anchors = root
+chain = ee-names.crt ica2.crt ica1.crt
+servername = barqux.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Server name check: no SAN, but the CN matches the server name.
+[chain]
+name = goodName3
+anchors = root
+chain = ee-names2.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Server name check: no SAN, and the CN does not match the server name.
+[chain]
+name = wrongName3
+anchors = root
+chain = ee-names2.crt ica2.crt ica1.crt
+servername = foo.example.com
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Server name check: no SAN, and the CN does not match the server name,
+; although its byte contents seem to match (but with BMPString encoding).
+[chain]
+name = wrongName4
+anchors = root
+chain = ee-names3.crt ica2.crt ica1.crt
+servername = www1.example.com
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Server name check: no SAN, and the CN uses BMPString encoding, but we
+; do not actually request a server name check, so this should pass.
+[chain]
+name = ignoreName1
+anchors = root
+chain = ee-names3.crt ica2.crt ica1.crt
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Wildcard processing: the name 'localhost' should not match because
+; the engine recognises the wildcard only in a '*.' starting sequence,
+; so the lone '*' in a SAN will not be accepted.
+[chain]
+name = wildcard1
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = localhost
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Wildcard processing: the name 'example.com' will be matched by '*.com'.
+[chain]
+name = wildcard2
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Wildcard processing: the name 'www.example.com' will be matched by
+; '*.example.com'.
+[chain]
+name = wildcard3
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Wildcard processing: the name 'foo.foo.example.com' will not be matched by
+; 'foo.*.example.com' because we accept the wildcard only in the first name
+; component.
+[chain]
+name = wildcard4
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = foo.foo.example.com
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Wildcard processing: the name 'foo.bar.example.com' will not be matched by
+; 'foo.*.example.com', but '*.bar.example.com' will fit.
+[chain]
+name = wildcard5
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = foo.bar.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Wildcard processing: the name 'foo.bar.example.foobar' will not be matched by
+; '*.*.example.foobar' because we support only a single level of wildcard.
+[chain]
+name = wildcard6
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = foo.bar.example.foobar
+keytype = RSA
+keyusage = KEYX
+status = 56
+
+; Wildcard processing: the name 'foo.*.example.foobar' will be matched
+; by '*.*.example.foobar' because the '*' in the provided server name matches
+; the second '*' in '*.*.example.foobar'. This is a corner case with no
+; practical impact because expected server names are usually extracted from
+; URL and cannot have embedded '*' in them.
+[chain]
+name = wildcard7
+anchors = root
+chain = ee-names4.crt ica2.crt ica1.crt
+servername = foo.*.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: the chain uses only SHA-256.
+[chain]
+name = hashSHA256Only
+anchors = root
+chain = ee.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+hashes = sha256
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: the chain uses only SHA-256.
+[chain]
+name = hashSHA256Unsupported
+anchors = root
+chain = ee.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+hashes = md5 sha1 sha224 sha384 sha512
+status = 49
+
+; Hash function support: signature on EE uses SHA-1.
+[chain]
+name = hashSHA1
+anchors = root
+chain = ee-sha1.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: signature on EE uses SHA-224.
+[chain]
+name = hashSHA224
+anchors = root
+chain = ee-sha224.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: signature on EE uses SHA-384.
+[chain]
+name = hashSHA384
+anchors = root
+chain = ee-sha384.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: signature on EE uses SHA-512.
+[chain]
+name = hashSHA512
+anchors = root
+chain = ee-sha512.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Hash function support: signature on EE uses MD5. This is rejected by
+; the engine (even though MD5 is supported as a hash function).
+[chain]
+name = hashMD5
+anchors = root
+chain = ee-md5.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 49
+
+; EE certificate has trailing garbage (an extra byte), which should be
+; rejected.
+[chain]
+name = trailingGarbage
+anchors = root
+chain = ee-trailing.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 40
+
+; Signature on EE certificate is incorrect (one byte modified in signature).
+[chain]
+name = badSignature1
+anchors = root
+chain = ee-badsig1.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 52
+
+; Signature on EE certificate is incorrect (one byte modified in serial
+; number).
+[chain]
+name = badSignature2
+anchors = root
+chain = ee-badsig2.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 52
+
+; Signature on EE certificate is incorrect but this is ignored because we
+; use a direct trust model here.
+[chain]
+name = ignoredSignature1
+anchors = ee
+chain = ee-badsig1.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Signature on EE certificate is incorrect but this is ignored because we
+; use a direct trust model here.
+[chain]
+name = ignoredSignature2
+anchors = ee
+chain = ee-badsig2.crt ica2.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Intermediate CA 1 has a 1016-bit RSA key, which should be rejected
+; with BR_ERR_X509_WEAK_PUBLIC_KEY.
+[chain]
+name = rsa1016
+anchors = root
+chain = ee.crt ica2-1016.crt ica1-1016.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 60
+
+; Intermediate CA 1 has a 1017-bit RSA key, which should be accepted
+; (because that's 128 bytes, which is the lower limit).
+[chain]
+name = rsa1017
+anchors = root
+chain = ee.crt ica2-1017.crt ica1-1017.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; Intermediate CA 1 has a 4096-bit RSA key, which should be supported.
+[chain]
+name = rsa4096
+anchors = root
+chain = ee.crt ica2-4096.crt ica1-4096.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE is valid from 2010/02/17 11:40:35 to 2098/07/20 15:11:08. The
+; start date is in UTCTime, the end date is in GeneralizedTime.
+[chain]
+name = date1
+anchors = ica2
+chain = ee-dates.crt ica2.crt ica1.crt
+time = 2010-02-17 11:40:34Z
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 54
+
+; EE is valid from 2010/02/17 11:40:35 to 2098/07/20 15:11:08. The
+; start date is in UTCTime, the end date is in GeneralizedTime.
+[chain]
+name = date2
+anchors = ica2
+chain = ee-dates.crt ica2.crt ica1.crt
+time = 2010-02-17 11:40:36Z
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE is valid from 2010/02/17 11:40:35 to 2098/07/20 15:11:08. The
+; start date is in UTCTime, the end date is in GeneralizedTime.
+[chain]
+name = date3
+anchors = ica2
+chain = ee-dates.crt ica2.crt ica1.crt
+time = 2098-07-20 15:11:07Z
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE is valid from 2010/02/17 11:40:35 to 2098/07/20 15:11:08. The
+; start date is in UTCTime, the end date is in GeneralizedTime.
+[chain]
+name = date4
+anchors = ica2
+chain = ee-dates.crt ica2.crt ica1.crt
+time = 2098-07-20 15:11:09Z
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 54
+
+; Intermediate CA 2 certificate is not a CA.
+[chain]
+name = notCA
+anchors = root
+chain = ee-dates.crt ica2-notCA.crt ica1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+status = 58
+
+; A chain using ECDSA with P-256.
+[chain]
+name = secp256r1
+anchors = root-p256
+chain = ee-p256.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; A chain using ECDSA with P-384.
+[chain]
+name = secp384r1
+anchors = root-p384
+chain = ee-p384.crt ica2-p384.crt ica1-p384.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p384
+status = 0
+
+; A chain using ECDSA with P-521.
+[chain]
+name = secp521r1
+anchors = root-p521
+chain = ee-p521.crt ica2-p521.crt ica1-p521.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p521
+status = 0
+
+; A chain using ECDSA with P-256, signature on EE uses SHA-1.
+[chain]
+name = secp256r1-sha1
+anchors = root-p256
+chain = ee-p256-sha1.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; A chain using ECDSA with P-256, signature on EE uses SHA-224.
+[chain]
+name = secp256r1-sha224
+anchors = root-p256
+chain = ee-p256-sha224.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; A chain using ECDSA with P-256, signature on EE uses SHA-256.
+[chain]
+name = secp256r1-sha256
+anchors = root-p256
+chain = ee-p256-sha256.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; A chain using ECDSA with P-256, signature on EE uses SHA-384.
+[chain]
+name = secp256r1-sha384
+anchors = root-p256
+chain = ee-p256-sha384.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; A chain using ECDSA with P-256, signature on EE uses SHA-512.
+[chain]
+name = secp256r1-sha512
+anchors = root-p256
+chain = ee-p256-sha512.crt ica2-p256.crt ica1-p256.crt
+servername = www.example.com
+keytype = EC
+keyusage = SIGN
+eekey = ee-p256
+status = 0
+
+; EE certificate has a Certificate Policies extension, but it is not
+; critical.
+[chain]
+name = certpol-noncrit
+anchors = root-new
+chain = ee-cp1.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE certificate has a critical Certificate Policies extension, but it
+; contains no policy qualifier.
+[chain]
+name = certpol-noqual
+anchors = root-new
+chain = ee-cp2.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE certificate has a critical Certificate Policies extension, and it
+; contains some qualifiers, but they are all id-qt-cps.
+[chain]
+name = certpol-qualcps
+anchors = root-new
+chain = ee-cp3.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 0
+
+; EE certificate has a critical Certificate Policies extension, and it
+; contains a qualifier distinct from id-qt-cps. This implies rejection
+; of the path.
+[chain]
+name = certpol-qualother
+anchors = root-new
+chain = ee-cp4.crt
+servername = www.example.com
+keytype = RSA
+keyusage = KEYX
+eekey = ee-rsa2048
+status = 57
diff --git a/test/monniaux/BearSSL/test/x509/dn-ee.der b/test/monniaux/BearSSL/test/x509/dn-ee.der
new file mode 100644
index 00000000..ca1c9ed4
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/dn-ee.der
@@ -0,0 +1 @@
+0'1 0 UCA10Uwww.example.com \ No newline at end of file
diff --git a/test/monniaux/BearSSL/test/x509/dn-ica1.der b/test/monniaux/BearSSL/test/x509/dn-ica1.der
new file mode 100644
index 00000000..3b0103fc
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/dn-ica1.der
@@ -0,0 +1 @@
+011 0 UCA1"0 UExample Intermediate CA 1 \ No newline at end of file
diff --git a/test/monniaux/BearSSL/test/x509/dn-ica2.der b/test/monniaux/BearSSL/test/x509/dn-ica2.der
new file mode 100644
index 00000000..d8cddad1
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/dn-ica2.der
@@ -0,0 +1 @@
+011 0 UCA1"0 UExample Intermediate CA 2 \ No newline at end of file
diff --git a/test/monniaux/BearSSL/test/x509/dn-root-new.der b/test/monniaux/BearSSL/test/x509/dn-root-new.der
new file mode 100644
index 00000000..2ec55757
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/dn-root-new.der
@@ -0,0 +1 @@
+0(1 0 UCA10UExample Root New \ No newline at end of file
diff --git a/test/monniaux/BearSSL/test/x509/dn-root.der b/test/monniaux/BearSSL/test/x509/dn-root.der
new file mode 100644
index 00000000..ea891f9f
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/dn-root.der
@@ -0,0 +1 @@
+0$1 0 UCA10U Example Root \ No newline at end of file
diff --git a/test/monniaux/BearSSL/test/x509/ee-badsig1.crt b/test/monniaux/BearSSL/test/x509/ee-badsig1.crt
new file mode 100644
index 00000000..03bcc771
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-badsig1.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-badsig2.crt b/test/monniaux/BearSSL/test/x509/ee-badsig2.crt
new file mode 100644
index 00000000..127309dd
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-badsig2.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-cp1.crt b/test/monniaux/BearSSL/test/x509/ee-cp1.crt
new file mode 100644
index 00000000..95f52e3f
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-cp1.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-cp2.crt b/test/monniaux/BearSSL/test/x509/ee-cp2.crt
new file mode 100644
index 00000000..357bc780
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-cp2.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-cp3.crt b/test/monniaux/BearSSL/test/x509/ee-cp3.crt
new file mode 100644
index 00000000..2a319dcc
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-cp3.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-cp4.crt b/test/monniaux/BearSSL/test/x509/ee-cp4.crt
new file mode 100644
index 00000000..d367f991
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-cp4.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-dates.crt b/test/monniaux/BearSSL/test/x509/ee-dates.crt
new file mode 100644
index 00000000..9dfa40a1
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-dates.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-md5.crt b/test/monniaux/BearSSL/test/x509/ee-md5.crt
new file mode 100644
index 00000000..1d5724d6
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-md5.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-names.crt b/test/monniaux/BearSSL/test/x509/ee-names.crt
new file mode 100644
index 00000000..5631de8c
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-names.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-names2.crt b/test/monniaux/BearSSL/test/x509/ee-names2.crt
new file mode 100644
index 00000000..f1f8133a
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-names2.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-names3.crt b/test/monniaux/BearSSL/test/x509/ee-names3.crt
new file mode 100644
index 00000000..2b6bb2a8
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-names3.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-names4.crt b/test/monniaux/BearSSL/test/x509/ee-names4.crt
new file mode 100644
index 00000000..6c24d1f4
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-names4.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256-sha1.crt b/test/monniaux/BearSSL/test/x509/ee-p256-sha1.crt
new file mode 100644
index 00000000..6da9ca7f
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256-sha1.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256-sha224.crt b/test/monniaux/BearSSL/test/x509/ee-p256-sha224.crt
new file mode 100644
index 00000000..165868ba
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256-sha224.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256-sha256.crt b/test/monniaux/BearSSL/test/x509/ee-p256-sha256.crt
new file mode 100644
index 00000000..de3c29e1
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256-sha256.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256-sha384.crt b/test/monniaux/BearSSL/test/x509/ee-p256-sha384.crt
new file mode 100644
index 00000000..c117aca7
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256-sha384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256-sha512.crt b/test/monniaux/BearSSL/test/x509/ee-p256-sha512.crt
new file mode 100644
index 00000000..f8a6016d
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256-sha512.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p256.crt b/test/monniaux/BearSSL/test/x509/ee-p256.crt
new file mode 100644
index 00000000..de3c29e1
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p256.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p384.crt b/test/monniaux/BearSSL/test/x509/ee-p384.crt
new file mode 100644
index 00000000..bda5ea92
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-p521.crt b/test/monniaux/BearSSL/test/x509/ee-p521.crt
new file mode 100644
index 00000000..281160c4
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-p521.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-sha1.crt b/test/monniaux/BearSSL/test/x509/ee-sha1.crt
new file mode 100644
index 00000000..6fbadac9
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-sha1.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-sha224.crt b/test/monniaux/BearSSL/test/x509/ee-sha224.crt
new file mode 100644
index 00000000..7f1fa9d8
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-sha224.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-sha384.crt b/test/monniaux/BearSSL/test/x509/ee-sha384.crt
new file mode 100644
index 00000000..15e3ffce
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-sha384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-sha512.crt b/test/monniaux/BearSSL/test/x509/ee-sha512.crt
new file mode 100644
index 00000000..f5721a84
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-sha512.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee-trailing.crt b/test/monniaux/BearSSL/test/x509/ee-trailing.crt
new file mode 100644
index 00000000..810c94ca
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee-trailing.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ee.crt b/test/monniaux/BearSSL/test/x509/ee.crt
new file mode 100644
index 00000000..8914d31a
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ee.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-1016.crt b/test/monniaux/BearSSL/test/x509/ica1-1016.crt
new file mode 100644
index 00000000..4735602c
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-1016.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-1017.crt b/test/monniaux/BearSSL/test/x509/ica1-1017.crt
new file mode 100644
index 00000000..3f794664
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-1017.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-4096.crt b/test/monniaux/BearSSL/test/x509/ica1-4096.crt
new file mode 100644
index 00000000..9cc49bcf
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-4096.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-p256.crt b/test/monniaux/BearSSL/test/x509/ica1-p256.crt
new file mode 100644
index 00000000..e96d9aff
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-p256.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-p384.crt b/test/monniaux/BearSSL/test/x509/ica1-p384.crt
new file mode 100644
index 00000000..695f8a64
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-p384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1-p521.crt b/test/monniaux/BearSSL/test/x509/ica1-p521.crt
new file mode 100644
index 00000000..017da0b7
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1-p521.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica1.crt b/test/monniaux/BearSSL/test/x509/ica1.crt
new file mode 100644
index 00000000..ea3bfe3b
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica1.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-1016.crt b/test/monniaux/BearSSL/test/x509/ica2-1016.crt
new file mode 100644
index 00000000..55ab9562
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-1016.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-1017.crt b/test/monniaux/BearSSL/test/x509/ica2-1017.crt
new file mode 100644
index 00000000..2b88a5ca
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-1017.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-4096.crt b/test/monniaux/BearSSL/test/x509/ica2-4096.crt
new file mode 100644
index 00000000..efc702dc
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-4096.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-notCA.crt b/test/monniaux/BearSSL/test/x509/ica2-notCA.crt
new file mode 100644
index 00000000..21fb8350
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-notCA.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-p256.crt b/test/monniaux/BearSSL/test/x509/ica2-p256.crt
new file mode 100644
index 00000000..94f8ccad
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-p256.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-p384.crt b/test/monniaux/BearSSL/test/x509/ica2-p384.crt
new file mode 100644
index 00000000..5dcbb0cf
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-p384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2-p521.crt b/test/monniaux/BearSSL/test/x509/ica2-p521.crt
new file mode 100644
index 00000000..d7d47571
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2-p521.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/ica2.crt b/test/monniaux/BearSSL/test/x509/ica2.crt
new file mode 100644
index 00000000..09bdaa66
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/ica2.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/junk.crt b/test/monniaux/BearSSL/test/x509/junk.crt
new file mode 100644
index 00000000..54e2084f
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/junk.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/names.crt b/test/monniaux/BearSSL/test/x509/names.crt
new file mode 100644
index 00000000..fe738c35
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/names.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/root-p256.crt b/test/monniaux/BearSSL/test/x509/root-p256.crt
new file mode 100644
index 00000000..819654af
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/root-p256.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/root-p384.crt b/test/monniaux/BearSSL/test/x509/root-p384.crt
new file mode 100644
index 00000000..bd397d57
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/root-p384.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/root-p521.crt b/test/monniaux/BearSSL/test/x509/root-p521.crt
new file mode 100644
index 00000000..a95e82b3
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/root-p521.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/test/x509/root.crt b/test/monniaux/BearSSL/test/x509/root.crt
new file mode 100644
index 00000000..4352b51c
--- /dev/null
+++ b/test/monniaux/BearSSL/test/x509/root.crt
Binary files differ
diff --git a/test/monniaux/BearSSL/tools/brssl.c b/test/monniaux/BearSSL/tools/brssl.c
new file mode 100644
index 00000000..91372b09
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/brssl.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+/*
+ * Network stuff on Windows requires some specific code.
+ */
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#pragma comment(lib, "Ws2_32.lib")
+#endif
+
+#include "brssl.h"
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: brssl command [ options ]\n");
+ fprintf(stderr, "available commands:\n");
+ fprintf(stderr, " client run SSL client\n");
+ fprintf(stderr, " server run SSL server\n");
+ fprintf(stderr, " verify verify certificate chain\n");
+ fprintf(stderr, " skey decode private key\n");
+ fprintf(stderr, " ta decode trust anchors\n");
+ fprintf(stderr, " chain make C code for certificate chains\n");
+ fprintf(stderr, " twrch run the Twrch protocol\n");
+ fprintf(stderr, " impl report on implementations\n");
+}
+
+int
+main(int argc, char *argv[])
+{
+ char *cmd;
+
+ if (argc < 2) {
+ usage();
+ return EXIT_FAILURE;
+ }
+#ifdef _WIN32
+ {
+ WSADATA wd;
+ int r;
+
+ r = WSAStartup(MAKEWORD(2, 2), &wd);
+ if (r != 0) {
+ fprintf(stderr, "WARNING: network initialisation"
+ " failed (WSAStartup() returned %d)\n", r);
+ }
+ }
+#endif
+ cmd = argv[1];
+ if (eqstr(cmd, "client")) {
+ if (do_client(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "server")) {
+ if (do_server(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "verify")) {
+ if (do_verify(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "skey")) {
+ if (do_skey(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "ta")) {
+ if (do_ta(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "chain")) {
+ if (do_chain(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else if (eqstr(cmd, "twrch")) {
+ int ret;
+
+ ret = do_twrch(argc - 2, argv + 2);
+ if (ret < 0) {
+ return EXIT_FAILURE;
+ } else {
+ return ret;
+ }
+ } else if (eqstr(cmd, "impl")) {
+ if (do_impl(argc - 2, argv + 2) < 0) {
+ return EXIT_FAILURE;
+ }
+ } else {
+ fprintf(stderr, "unknown command: '%s'\n", cmd);
+ usage();
+ return EXIT_FAILURE;
+ }
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/tools/brssl.h b/test/monniaux/BearSSL/tools/brssl.h
new file mode 100644
index 00000000..a23ba00f
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/brssl.h
@@ -0,0 +1,567 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef BRSSL_H__
+#define BRSSL_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "bearssl.h"
+
+/*
+ * malloc() wrapper:
+ * -- If len is 0, then NULL is returned.
+ * -- If len is non-zero, and allocation fails, then an error message is
+ * printed and the process exits with an error code.
+ */
+void *xmalloc(size_t len);
+
+/*
+ * free() wrapper, meant to release blocks allocated with xmalloc().
+ */
+void xfree(void *buf);
+
+/*
+ * Duplicate a character string into a newly allocated block.
+ */
+char *xstrdup(const void *src);
+
+/*
+ * Allocate a new block with the provided length, filled with a copy
+ * of exactly that many bytes starting at address 'src'.
+ */
+void *xblobdup(const void *src, size_t len);
+
+/*
+ * Duplicate a public key, into newly allocated blocks. The returned
+ * key must be later on released with xfreepkey().
+ */
+br_x509_pkey *xpkeydup(const br_x509_pkey *pk);
+
+/*
+ * Release a public key that was allocated with xpkeydup(). If pk is NULL,
+ * this function does nothing.
+ */
+void xfreepkey(br_x509_pkey *pk);
+
+/*
+ * Macros for growable arrays.
+ */
+
+/*
+ * Make a structure type for a vector of 'type'.
+ */
+#define VECTOR(type) struct { \
+ type *buf; \
+ size_t ptr, len; \
+ }
+
+/*
+ * Constant initialiser for a vector.
+ */
+#define VEC_INIT { 0, 0, 0 }
+
+/*
+ * Clear a vector.
+ */
+#define VEC_CLEAR(vec) do { \
+ xfree((vec).buf); \
+ (vec).buf = NULL; \
+ (vec).ptr = 0; \
+ (vec).len = 0; \
+ } while (0)
+
+/*
+ * Clear a vector, first calling the provided function on each vector
+ * element.
+ */
+#define VEC_CLEAREXT(vec, fun) do { \
+ size_t vec_tmp; \
+ for (vec_tmp = 0; vec_tmp < (vec).ptr; vec_tmp ++) { \
+ (fun)(&(vec).buf[vec_tmp]); \
+ } \
+ VEC_CLEAR(vec); \
+ } while (0)
+
+/*
+ * Add a value at the end of a vector.
+ */
+#define VEC_ADD(vec, x) do { \
+ (vec).buf = vector_expand((vec).buf, sizeof *((vec).buf), \
+ &(vec).ptr, &(vec).len, 1); \
+ (vec).buf[(vec).ptr ++] = (x); \
+ } while (0)
+
+/*
+ * Add several values at the end of a vector.
+ */
+#define VEC_ADDMANY(vec, xp, num) do { \
+ size_t vec_num = (num); \
+ (vec).buf = vector_expand((vec).buf, sizeof *((vec).buf), \
+ &(vec).ptr, &(vec).len, vec_num); \
+ memcpy((vec).buf + (vec).ptr, \
+ (xp), vec_num * sizeof *((vec).buf)); \
+ (vec).ptr += vec_num; \
+ } while (0)
+
+/*
+ * Access a vector element by index. This is a lvalue, and can be modified.
+ */
+#define VEC_ELT(vec, idx) ((vec).buf[idx])
+
+/*
+ * Get current vector length.
+ */
+#define VEC_LEN(vec) ((vec).ptr)
+
+/*
+ * Copy all vector elements into a newly allocated block.
+ */
+#define VEC_TOARRAY(vec) xblobdup((vec).buf, sizeof *((vec).buf) * (vec).ptr)
+
+/*
+ * Internal function used to handle memory allocations for vectors.
+ */
+void *vector_expand(void *buf,
+ size_t esize, size_t *ptr, size_t *len, size_t extra);
+
+/*
+ * Type for a vector of bytes.
+ */
+typedef VECTOR(unsigned char) bvector;
+
+/*
+ * Compare two strings for equality; returned value is 1 if the strings
+ * are to be considered equal, 0 otherwise. Comparison is case-insensitive
+ * (ASCII letters only) and skips some characters (all whitespace, defined
+ * as ASCII codes 0 to 32 inclusive, and also '-', '_', '.', '/', '+' and
+ * ':').
+ */
+int eqstr(const char *s1, const char *s2);
+
+/*
+ * Convert a string to a positive integer (size_t). Returned value is
+ * (size_t)-1 on error. On error, an explicit error message is printed.
+ */
+size_t parse_size(const char *s);
+
+/*
+ * Structure for a known protocol version.
+ */
+typedef struct {
+ const char *name;
+ unsigned version;
+ const char *comment;
+} protocol_version;
+
+/*
+ * Known protocol versions. Last element has a NULL name.
+ */
+extern const protocol_version protocol_versions[];
+
+/*
+ * Parse a version name. If the name is not recognized, then an error
+ * message is printed, and 0 is returned.
+ */
+unsigned parse_version(const char *name, size_t len);
+
+/*
+ * Type for a known hash function.
+ */
+typedef struct {
+ const char *name;
+ const br_hash_class *hclass;
+ const char *comment;
+} hash_function;
+
+/*
+ * Known hash functions. Last element has a NULL name.
+ */
+extern const hash_function hash_functions[];
+
+/*
+ * Parse hash function names. This function expects a comma-separated
+ * list of names, and returns a bit mask corresponding to the matched
+ * names. If one of the name does not match, or the list is empty, then
+ * an error message is printed, and 0 is returned.
+ */
+unsigned parse_hash_functions(const char *arg);
+
+/*
+ * Get a curve name (by ID). If the curve ID is not known, this returns
+ * NULL.
+ */
+const char *get_curve_name(int id);
+
+/*
+ * Get a curve name (by ID). The name is written in the provided buffer
+ * (zero-terminated). If the curve ID is not known, the name is
+ * "unknown (***)" where "***" is the decimal value of the identifier.
+ * If the name does not fit in the provided buffer, then dst[0] is set
+ * to 0 (unless len is 0, in which case nothing is written), and -1 is
+ * returned. Otherwise, the name is written in dst[] (with a terminating
+ * 0), and this function returns 0.
+ */
+int get_curve_name_ext(int id, char *dst, size_t len);
+
+/*
+ * Type for a known cipher suite.
+ */
+typedef struct {
+ const char *name;
+ uint16_t suite;
+ unsigned req;
+ const char *comment;
+} cipher_suite;
+
+/*
+ * Known cipher suites. Last element has a NULL name.
+ */
+extern const cipher_suite cipher_suites[];
+
+/*
+ * Flags for cipher suite requirements.
+ */
+#define REQ_TLS12 0x0001 /* suite needs TLS 1.2 */
+#define REQ_SHA1 0x0002 /* suite needs SHA-1 */
+#define REQ_SHA256 0x0004 /* suite needs SHA-256 */
+#define REQ_SHA384 0x0008 /* suite needs SHA-384 */
+#define REQ_AESCBC 0x0010 /* suite needs AES/CBC encryption */
+#define REQ_AESGCM 0x0020 /* suite needs AES/GCM encryption */
+#define REQ_AESCCM 0x0040 /* suite needs AES/CCM encryption */
+#define REQ_CHAPOL 0x0080 /* suite needs ChaCha20+Poly1305 */
+#define REQ_3DESCBC 0x0100 /* suite needs 3DES/CBC encryption */
+#define REQ_RSAKEYX 0x0200 /* suite uses RSA key exchange */
+#define REQ_ECDHE_RSA 0x0400 /* suite uses ECDHE_RSA key exchange */
+#define REQ_ECDHE_ECDSA 0x0800 /* suite uses ECDHE_ECDSA key exchange */
+#define REQ_ECDH 0x1000 /* suite uses static ECDH key exchange */
+
+/*
+ * Parse a list of cipher suite names. The names are comma-separated. If
+ * one of the name is not recognised, or the list is empty, then an
+ * appropriate error message is printed, and NULL is returned.
+ * The returned array is allocated with xmalloc() and must be released
+ * by the caller. That array is terminated with a dummy entry whose 'name'
+ * field is NULL. The number of entries (not counting the dummy entry)
+ * is also written into '*num'.
+ */
+cipher_suite *parse_suites(const char *arg, size_t *num);
+
+/*
+ * Get the name of a cipher suite. Returned value is NULL if the suite is
+ * not recognized.
+ */
+const char *get_suite_name(unsigned suite);
+
+/*
+ * Get the name of a cipher suite. The name is written in the provided
+ * buffer; if the suite is not recognised, then the name is
+ * "unknown (0x****)" where "****" is the hexadecimal value of the suite.
+ * If the name does not fit in the provided buffer, then dst[0] is set
+ * to 0 (unless len is 0, in which case nothing is written), and -1 is
+ * returned. Otherwise, the name is written in dst[] (with a terminating
+ * 0), and this function returns 0.
+ */
+int get_suite_name_ext(unsigned suite, char *dst, size_t len);
+
+/*
+ * Tell whether a cipher suite uses ECDHE key exchange.
+ */
+int uses_ecdhe(unsigned suite);
+
+/*
+ * Print out all known names (for protocol versions, cipher suites...).
+ */
+void list_names(void);
+
+/*
+ * Print out all known elliptic curve names.
+ */
+void list_curves(void);
+
+/*
+ * Get the symbolic name for an elliptic curve (by ID).
+ */
+const char *ec_curve_name(int curve);
+
+/*
+ * Get a curve by symbolic name. If the name is not recognized, -1 is
+ * returned.
+ */
+int get_curve_by_name(const char *str);
+
+/*
+ * Get the symbolic name for a hash function name (by ID).
+ */
+const char *hash_function_name(int id);
+
+/*
+ * Read a file completely. The returned block is allocated with xmalloc()
+ * and must be released by the caller.
+ * If the file cannot be found or read completely, or is empty, then an
+ * appropriate error message is written, and NULL is returned.
+ */
+unsigned char *read_file(const char *fname, size_t *len);
+
+/*
+ * Write a file completely. This returns 0 on success, -1 on error. On
+ * error, an appropriate error message is printed.
+ */
+int write_file(const char *fname, const void *data, size_t len);
+
+/*
+ * This function returns non-zero if the provided buffer "looks like"
+ * a DER-encoded ASN.1 object (criteria: it has the tag for a SEQUENCE
+ * with a definite length that matches the total object length).
+ */
+int looks_like_DER(const unsigned char *buf, size_t len);
+
+/*
+ * Type for a named blob (the 'name' is a normalised PEM header name).
+ */
+typedef struct {
+ char *name;
+ unsigned char *data;
+ size_t data_len;
+} pem_object;
+
+/*
+ * Release the contents of a named blob (buffer and name).
+ */
+void free_pem_object_contents(pem_object *po);
+
+/*
+ * Decode a buffer as a PEM file, and return all objects. On error, NULL
+ * is returned and an error message is printed. Absence of any object
+ * is an error.
+ *
+ * The returned array is terminated by a dummy object whose 'name' is
+ * NULL. The number of objects (not counting the dummy terminator) is
+ * written in '*num'.
+ */
+pem_object *decode_pem(const void *src, size_t len, size_t *num);
+
+/*
+ * Get the certificate(s) from a file. This accepts both a single
+ * DER-encoded certificate, and a text file that contains
+ * PEM-encoded certificates (and possibly other objects, which are
+ * then ignored).
+ *
+ * On decoding error, or if the file turns out to contain no certificate
+ * at all, then an error message is printed and NULL is returned.
+ *
+ * The returned array, and all referenced buffers, are allocated with
+ * xmalloc() and must be released by the caller. The returned array
+ * ends with a dummy entry whose 'data' field is NULL.
+ * The number of decoded certificates (not counting the dummy entry)
+ * is written into '*num'.
+ */
+br_x509_certificate *read_certificates(const char *fname, size_t *num);
+
+/*
+ * Release certificates. This releases all certificate data arrays,
+ * and the whole array as well.
+ */
+void free_certificates(br_x509_certificate *certs, size_t num);
+
+/*
+ * Interpret a certificate as a trust anchor. The trust anchor is
+ * newly allocated with xmalloc() and the caller must release it.
+ * On decoding error, an error message is printed, and this function
+ * returns NULL.
+ */
+br_x509_trust_anchor *certificate_to_trust_anchor(br_x509_certificate *xc);
+
+/*
+ * Type for a vector of trust anchors.
+ */
+typedef VECTOR(br_x509_trust_anchor) anchor_list;
+
+/*
+ * Release contents for a trust anchor (assuming they were dynamically
+ * allocated with xmalloc()). The structure itself is NOT released.
+ */
+void free_ta_contents(br_x509_trust_anchor *ta);
+
+/*
+ * Decode certificates from a file and interpret them as trust anchors.
+ * The trust anchors are added to the provided list. The number of found
+ * anchors is returned; on error, 0 is returned (finding no anchor at
+ * all is considered an error). An appropriate error message is displayed.
+ */
+size_t read_trust_anchors(anchor_list *dst, const char *fname);
+
+/*
+ * Get the "signer key type" for the certificate (key type of the
+ * issuing CA). On error, this prints a message on stderr, and returns 0.
+ */
+int get_cert_signer_algo(br_x509_certificate *xc);
+
+/*
+ * Special "no anchor" X.509 validator that wraps around another X.509
+ * validator and turns "not trusted" error codes into success. This is
+ * by definition insecure, but convenient for debug purposes.
+ */
+typedef struct {
+ const br_x509_class *vtable;
+ const br_x509_class **inner;
+} x509_noanchor_context;
+extern const br_x509_class x509_noanchor_vtable;
+
+/*
+ * Initialise a "no anchor" X.509 validator.
+ */
+void x509_noanchor_init(x509_noanchor_context *xwc,
+ const br_x509_class **inner);
+
+/*
+ * Aggregate type for a private key.
+ */
+typedef struct {
+ int key_type; /* BR_KEYTYPE_RSA or BR_KEYTYPE_EC */
+ union {
+ br_rsa_private_key rsa;
+ br_ec_private_key ec;
+ } key;
+} private_key;
+
+/*
+ * Decode a private key from a file. On error, this prints an error
+ * message and returns NULL.
+ */
+private_key *read_private_key(const char *fname);
+
+/*
+ * Free a private key.
+ */
+void free_private_key(private_key *sk);
+
+/*
+ * Get the encoded OID for a given hash function (to use with PKCS#1
+ * signatures). If the hash function ID is 0 (for MD5+SHA-1), or if
+ * the ID is not one of the SHA-* functions (SHA-1, SHA-224, SHA-256,
+ * SHA-384, SHA-512), then this function returns NULL.
+ */
+const unsigned char *get_hash_oid(int id);
+
+/*
+ * Get a hash implementation by ID. This returns NULL if the hash
+ * implementation is not available.
+ */
+const br_hash_class *get_hash_impl(int id);
+
+/*
+ * Find the symbolic name and the description for an error. If 'err' is
+ * recognised then the error symbolic name is returned; if 'comment' is
+ * not NULL then '*comment' is then set to a descriptive human-readable
+ * message. If the error code 'err' is not recognised, then '*comment' is
+ * untouched and this function returns NULL.
+ */
+const char *find_error_name(int err, const char **comment);
+
+/*
+ * Find the symbolic name for an algorithm implementation. Provided
+ * pointer should be a pointer to a vtable or to a function, where
+ * appropriate. If not recognised, then the string "UNKNOWN" is returned.
+ *
+ * If 'long_name' is non-zero, then the returned name recalls the
+ * algorithm type as well; otherwise, only the core implementation name
+ * is returned (e.g. the long name could be 'aes_big_cbcenc' while the
+ * short name is 'big').
+ */
+const char *get_algo_name(const void *algo, int long_name);
+
+/*
+ * Run a SSL engine, with a socket connected to the peer, and using
+ * stdin/stdout to exchange application data. The socket must be a
+ * non-blocking descriptor.
+ *
+ * To help with Win32 compatibility, the socket descriptor is provided
+ * as an "unsigned long" value.
+ *
+ * Returned value:
+ * 0 SSL connection closed successfully
+ * x > 0 SSL error "x"
+ * -1 early socket close
+ * -2 stdout was closed, or something failed badly
+ */
+int run_ssl_engine(br_ssl_engine_context *eng,
+ unsigned long fd, unsigned flags);
+
+#define RUN_ENGINE_VERBOSE 0x0001 /* enable verbose messages */
+#define RUN_ENGINE_TRACE 0x0002 /* hex dump of records */
+
+/*
+ * Do the "client" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_client(int argc, char *argv[]);
+
+/*
+ * Do the "server" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_server(int argc, char *argv[]);
+
+/*
+ * Do the "verify" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_verify(int argc, char *argv[]);
+
+/*
+ * Do the "skey" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_skey(int argc, char *argv[]);
+
+/*
+ * Do the "ta" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_ta(int argc, char *argv[]);
+
+/*
+ * Do the "chain" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_chain(int argc, char *argv[]);
+
+/*
+ * Do the "twrch" command. Returned value is 0 on success, -1 on failure
+ * (processing or arguments), or a non-zero exit code. Command-line
+ * arguments start _after_ the command name.
+ */
+int do_twrch(int argc, char *argv[]);
+
+/*
+ * Do the "impl" command. Returned value is 0 on success, -1 on failure.
+ * Command-line arguments start _after_ the command name.
+ */
+int do_impl(int argc, char *argv[]);
+
+#endif
diff --git a/test/monniaux/BearSSL/tools/certs.c b/test/monniaux/BearSSL/tools/certs.c
new file mode 100644
index 00000000..8986446e
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/certs.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+
+static void
+dn_append(void *ctx, const void *buf, size_t len)
+{
+ VEC_ADDMANY(*(bvector *)ctx, buf, len);
+}
+
+static int
+certificate_to_trust_anchor_inner(br_x509_trust_anchor *ta,
+ br_x509_certificate *xc)
+{
+ br_x509_decoder_context dc;
+ bvector vdn = VEC_INIT;
+ br_x509_pkey *pk;
+
+ br_x509_decoder_init(&dc, dn_append, &vdn);
+ br_x509_decoder_push(&dc, xc->data, xc->data_len);
+ pk = br_x509_decoder_get_pkey(&dc);
+ if (pk == NULL) {
+ fprintf(stderr, "ERROR: CA decoding failed with error %d\n",
+ br_x509_decoder_last_error(&dc));
+ VEC_CLEAR(vdn);
+ return -1;
+ }
+ ta->dn.data = VEC_TOARRAY(vdn);
+ ta->dn.len = VEC_LEN(vdn);
+ VEC_CLEAR(vdn);
+ ta->flags = 0;
+ if (br_x509_decoder_isCA(&dc)) {
+ ta->flags |= BR_X509_TA_CA;
+ }
+ switch (pk->key_type) {
+ case BR_KEYTYPE_RSA:
+ ta->pkey.key_type = BR_KEYTYPE_RSA;
+ ta->pkey.key.rsa.n = xblobdup(pk->key.rsa.n, pk->key.rsa.nlen);
+ ta->pkey.key.rsa.nlen = pk->key.rsa.nlen;
+ ta->pkey.key.rsa.e = xblobdup(pk->key.rsa.e, pk->key.rsa.elen);
+ ta->pkey.key.rsa.elen = pk->key.rsa.elen;
+ break;
+ case BR_KEYTYPE_EC:
+ ta->pkey.key_type = BR_KEYTYPE_EC;
+ ta->pkey.key.ec.curve = pk->key.ec.curve;
+ ta->pkey.key.ec.q = xblobdup(pk->key.ec.q, pk->key.ec.qlen);
+ ta->pkey.key.ec.qlen = pk->key.ec.qlen;
+ break;
+ default:
+ fprintf(stderr, "ERROR: unsupported public key type in CA\n");
+ xfree(ta->dn.data);
+ return -1;
+ }
+ return 0;
+}
+
+/* see brssl.h */
+br_x509_trust_anchor *
+certificate_to_trust_anchor(br_x509_certificate *xc)
+{
+ br_x509_trust_anchor ta;
+
+ if (certificate_to_trust_anchor_inner(&ta, xc) < 0) {
+ return NULL;
+ } else {
+ return xblobdup(&ta, sizeof ta);
+ }
+}
+
+/* see brssl.h */
+void
+free_ta_contents(br_x509_trust_anchor *ta)
+{
+ xfree(ta->dn.data);
+ switch (ta->pkey.key_type) {
+ case BR_KEYTYPE_RSA:
+ xfree(ta->pkey.key.rsa.n);
+ xfree(ta->pkey.key.rsa.e);
+ break;
+ case BR_KEYTYPE_EC:
+ xfree(ta->pkey.key.ec.q);
+ break;
+ }
+}
+
+/* see brssl.h */
+size_t
+read_trust_anchors(anchor_list *dst, const char *fname)
+{
+ br_x509_certificate *xcs;
+ anchor_list tas = VEC_INIT;
+ size_t u, num;
+
+ xcs = read_certificates(fname, &num);
+ if (xcs == NULL) {
+ return 0;
+ }
+ for (u = 0; u < num; u ++) {
+ br_x509_trust_anchor ta;
+
+ if (certificate_to_trust_anchor_inner(&ta, &xcs[u]) < 0) {
+ VEC_CLEAREXT(tas, free_ta_contents);
+ free_certificates(xcs, num);
+ return 0;
+ }
+ VEC_ADD(tas, ta);
+ }
+ VEC_ADDMANY(*dst, &VEC_ELT(tas, 0), num);
+ VEC_CLEAR(tas);
+ free_certificates(xcs, num);
+ return num;
+}
+
+/* see brssl.h */
+int
+get_cert_signer_algo(br_x509_certificate *xc)
+{
+ br_x509_decoder_context dc;
+ int err;
+
+ br_x509_decoder_init(&dc, 0, 0);
+ br_x509_decoder_push(&dc, xc->data, xc->data_len);
+ err = br_x509_decoder_last_error(&dc);
+ if (err != 0) {
+ fprintf(stderr,
+ "ERROR: certificate decoding failed with error %d\n",
+ -err);
+ return 0;
+ }
+ return br_x509_decoder_get_signer_key_type(&dc);
+}
+
+static void
+xwc_start_chain(const br_x509_class **ctx, const char *server_name)
+{
+ x509_noanchor_context *xwc;
+
+ xwc = (x509_noanchor_context *)ctx;
+ (*xwc->inner)->start_chain(xwc->inner, server_name);
+}
+
+static void
+xwc_start_cert(const br_x509_class **ctx, uint32_t length)
+{
+ x509_noanchor_context *xwc;
+
+ xwc = (x509_noanchor_context *)ctx;
+ (*xwc->inner)->start_cert(xwc->inner, length);
+}
+
+static void
+xwc_append(const br_x509_class **ctx, const unsigned char *buf, size_t len)
+{
+ x509_noanchor_context *xwc;
+
+ xwc = (x509_noanchor_context *)ctx;
+ (*xwc->inner)->append(xwc->inner, buf, len);
+}
+
+static void
+xwc_end_cert(const br_x509_class **ctx)
+{
+ x509_noanchor_context *xwc;
+
+ xwc = (x509_noanchor_context *)ctx;
+ (*xwc->inner)->end_cert(xwc->inner);
+}
+
+static unsigned
+xwc_end_chain(const br_x509_class **ctx)
+{
+ x509_noanchor_context *xwc;
+ unsigned r;
+
+ xwc = (x509_noanchor_context *)ctx;
+ r = (*xwc->inner)->end_chain(xwc->inner);
+ if (r == BR_ERR_X509_NOT_TRUSTED) {
+ r = 0;
+ }
+ return r;
+}
+
+static const br_x509_pkey *
+xwc_get_pkey(const br_x509_class *const *ctx, unsigned *usages)
+{
+ x509_noanchor_context *xwc;
+
+ xwc = (x509_noanchor_context *)ctx;
+ return (*xwc->inner)->get_pkey(xwc->inner, usages);
+}
+
+/* see brssl.h */
+const br_x509_class x509_noanchor_vtable = {
+ sizeof(x509_noanchor_context),
+ xwc_start_chain,
+ xwc_start_cert,
+ xwc_append,
+ xwc_end_cert,
+ xwc_end_chain,
+ xwc_get_pkey
+};
+
+/* see brssl.h */
+void
+x509_noanchor_init(x509_noanchor_context *xwc, const br_x509_class **inner)
+{
+ xwc->vtable = &x509_noanchor_vtable;
+ xwc->inner = inner;
+}
diff --git a/test/monniaux/BearSSL/tools/chain.c b/test/monniaux/BearSSL/tools/chain.c
new file mode 100644
index 00000000..671f5e83
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/chain.c
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+static void
+print_blob(const char *name, const unsigned char *buf, size_t len)
+{
+ size_t u;
+
+ printf("\nstatic const unsigned char %s[] = {", name);
+ for (u = 0; u < len; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", buf[u]);
+ }
+ printf("\n};\n");
+}
+
+static void
+usage_chain(void)
+{
+ fprintf(stderr,
+"usage: brssl chain [ options ] file...\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+}
+
+/* see brssl.h */
+int
+do_chain(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int i, num_files;
+ long k, ctr;
+
+ retcode = 0;
+ verbose = 1;
+ num_files = 0;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ num_files ++;
+ continue;
+ }
+ argv[i] = NULL;
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_chain();
+ goto chain_exit_error;
+ }
+ }
+ if (num_files == 0) {
+ fprintf(stderr, "ERROR: no certificate file provided\n");
+ usage_chain();
+ goto chain_exit_error;
+ }
+
+ ctr = 0;
+ for (i = 0; i < argc; i ++) {
+ const char *fname;
+ br_x509_certificate *xcs;
+ size_t u, num;
+
+ fname = argv[i];
+ if (fname == NULL) {
+ continue;
+ }
+ if (verbose) {
+ fprintf(stderr, "Reading file '%s': ", fname);
+ fflush(stderr);
+ }
+ xcs = read_certificates(fname, &num);
+ if (xcs == NULL) {
+ goto chain_exit_error;
+ }
+ if (verbose) {
+ fprintf(stderr, "%lu certificate%s\n",
+ (unsigned long)num, num > 1 ? "s" : "");
+ }
+ for (u = 0; u < num; u ++) {
+ char tmp[50];
+
+ sprintf(tmp, "CERT%ld", ctr ++);
+ print_blob(tmp, xcs[u].data, xcs[u].data_len);
+ xfree(xcs[u].data);
+ }
+ xfree(xcs);
+ }
+
+ printf("\nstatic const br_x509_certificate CHAIN[] = {");
+ for (k = 0; k < ctr; k ++) {
+ if (k != 0) {
+ printf(",");
+ }
+ printf("\n\t{ (unsigned char *)CERT%ld, sizeof CERT%ld }",
+ k, k);
+ }
+ printf("\n};\n");
+ printf("\n#define CHAIN_LEN %ld\n", ctr);
+
+ /*
+ * Release allocated structures.
+ */
+chain_exit:
+ return retcode;
+
+chain_exit_error:
+ retcode = -1;
+ goto chain_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/client.c b/test/monniaux/BearSSL/tools/client.c
new file mode 100644
index 00000000..98388572
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/client.c
@@ -0,0 +1,1112 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <signal.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define SOCKET int
+#define INVALID_SOCKET (-1)
+#endif
+
+#include "brssl.h"
+
+static int
+host_connect(const char *host, const char *port, int verbose)
+{
+ struct addrinfo hints, *si, *p;
+ SOCKET fd;
+ int err;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ err = getaddrinfo(host, port, &hints, &si);
+ if (err != 0) {
+ fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
+ gai_strerror(err));
+ return INVALID_SOCKET;
+ }
+ fd = INVALID_SOCKET;
+ for (p = si; p != NULL; p = p->ai_next) {
+ if (verbose) {
+ struct sockaddr *sa;
+ void *addr;
+ char tmp[INET6_ADDRSTRLEN + 50];
+
+ sa = (struct sockaddr *)p->ai_addr;
+ if (sa->sa_family == AF_INET) {
+ addr = &((struct sockaddr_in *)
+ (void *)sa)->sin_addr;
+ } else if (sa->sa_family == AF_INET6) {
+ addr = &((struct sockaddr_in6 *)
+ (void *)sa)->sin6_addr;
+ } else {
+ addr = NULL;
+ }
+ if (addr != NULL) {
+ if (!inet_ntop(p->ai_family, addr,
+ tmp, sizeof tmp))
+ {
+ strcpy(tmp, "<invalid>");
+ }
+ } else {
+ sprintf(tmp, "<unknown family: %d>",
+ (int)sa->sa_family);
+ }
+ fprintf(stderr, "connecting to: %s\n", tmp);
+ }
+ fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (fd == INVALID_SOCKET) {
+ if (verbose) {
+ perror("socket()");
+ }
+ continue;
+ }
+ if (connect(fd, p->ai_addr, p->ai_addrlen) == INVALID_SOCKET) {
+ if (verbose) {
+ perror("connect()");
+ }
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ continue;
+ }
+ break;
+ }
+ if (p == NULL) {
+ freeaddrinfo(si);
+ fprintf(stderr, "ERROR: failed to connect\n");
+ return INVALID_SOCKET;
+ }
+ freeaddrinfo(si);
+ if (verbose) {
+ fprintf(stderr, "connected.\n");
+ }
+
+ /*
+ * We make the socket non-blocking, since we are going to use
+ * poll() or select() to organise I/O.
+ */
+#ifdef _WIN32
+ {
+ u_long arg;
+
+ arg = 1;
+ ioctlsocket(fd, FIONBIO, &arg);
+ }
+#else
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+#endif
+ return fd;
+}
+
+typedef struct {
+ const br_ssl_client_certificate_class *vtable;
+ int verbose;
+ br_x509_certificate *chain;
+ size_t chain_len;
+ private_key *sk;
+ int issuer_key_type;
+} ccert_context;
+
+static void
+cc_start_name_list(const br_ssl_client_certificate_class **pctx)
+{
+ ccert_context *zc;
+
+ zc = (ccert_context *)pctx;
+ if (zc->verbose) {
+ fprintf(stderr, "Server requests a client certificate.\n");
+ fprintf(stderr, "--- anchor DN list start ---\n");
+ }
+}
+
+static void
+cc_start_name(const br_ssl_client_certificate_class **pctx, size_t len)
+{
+ ccert_context *zc;
+
+ zc = (ccert_context *)pctx;
+ if (zc->verbose) {
+ fprintf(stderr, "new anchor name, length = %u\n",
+ (unsigned)len);
+ }
+}
+
+static void
+cc_append_name(const br_ssl_client_certificate_class **pctx,
+ const unsigned char *data, size_t len)
+{
+ ccert_context *zc;
+
+ zc = (ccert_context *)pctx;
+ if (zc->verbose) {
+ size_t u;
+
+ for (u = 0; u < len; u ++) {
+ if (u == 0) {
+ fprintf(stderr, " ");
+ } else if (u > 0 && u % 16 == 0) {
+ fprintf(stderr, "\n ");
+ }
+ fprintf(stderr, " %02x", data[u]);
+ }
+ if (len > 0) {
+ fprintf(stderr, "\n");
+ }
+ }
+}
+
+static void
+cc_end_name(const br_ssl_client_certificate_class **pctx)
+{
+ (void)pctx;
+}
+
+static void
+cc_end_name_list(const br_ssl_client_certificate_class **pctx)
+{
+ ccert_context *zc;
+
+ zc = (ccert_context *)pctx;
+ if (zc->verbose) {
+ fprintf(stderr, "--- anchor DN list end ---\n");
+ }
+}
+
+static void
+print_hashes(unsigned hh, unsigned hh2)
+{
+ int i;
+
+ for (i = 0; i < 8; i ++) {
+ const char *name;
+
+ name = hash_function_name(i);
+ if (((hh >> i) & 1) != 0) {
+ fprintf(stderr, " %s", name);
+ } else if (((hh2 >> i) & 1) != 0) {
+ fprintf(stderr, " (%s)", name);
+ }
+ }
+}
+
+static int
+choose_hash(unsigned hh)
+{
+ static const int f[] = {
+ br_sha256_ID, br_sha224_ID, br_sha384_ID, br_sha512_ID,
+ br_sha1_ID, br_md5sha1_ID, -1
+ };
+
+ size_t u;
+
+ for (u = 0; f[u] >= 0; u ++) {
+ if (((hh >> f[u]) & 1) != 0) {
+ return f[u];
+ }
+ }
+ return -1;
+}
+
+static void
+cc_choose(const br_ssl_client_certificate_class **pctx,
+ const br_ssl_client_context *cc, uint32_t auth_types,
+ br_ssl_client_certificate *choices)
+{
+ ccert_context *zc;
+ int scurve;
+
+ zc = (ccert_context *)pctx;
+ scurve = br_ssl_client_get_server_curve(cc);
+ if (zc->verbose) {
+ unsigned hashes;
+
+ hashes = br_ssl_client_get_server_hashes(cc);
+ if ((auth_types & 0x00FF) != 0) {
+ fprintf(stderr, "supported: RSA signatures:");
+ print_hashes(auth_types, hashes);
+ fprintf(stderr, "\n");
+ }
+ if ((auth_types & 0xFF00) != 0) {
+ fprintf(stderr, "supported: ECDSA signatures:");
+ print_hashes(auth_types >> 8, hashes >> 8);
+ fprintf(stderr, "\n");
+ }
+ if ((auth_types & 0x010000) != 0) {
+ fprintf(stderr, "supported:"
+ " fixed ECDH (cert signed with RSA)\n");
+ }
+ if ((auth_types & 0x020000) != 0) {
+ fprintf(stderr, "supported:"
+ " fixed ECDH (cert signed with ECDSA)\n");
+ }
+ if (scurve) {
+ fprintf(stderr, "server key curve: %s (%d)\n",
+ ec_curve_name(scurve), scurve);
+ } else {
+ fprintf(stderr, "server key is not EC\n");
+ }
+ }
+ switch (zc->sk->key_type) {
+ case BR_KEYTYPE_RSA:
+ if ((choices->hash_id = choose_hash(auth_types)) >= 0) {
+ if (zc->verbose) {
+ fprintf(stderr, "using RSA, hash = %d (%s)\n",
+ choices->hash_id,
+ hash_function_name(choices->hash_id));
+ }
+ choices->auth_type = BR_AUTH_RSA;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+ return;
+ }
+ break;
+ case BR_KEYTYPE_EC:
+ if (zc->issuer_key_type != 0
+ && scurve == zc->sk->key.ec.curve)
+ {
+ int x;
+
+ x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;
+ if (((auth_types >> x) & 1) != 0) {
+ if (zc->verbose) {
+ fprintf(stderr, "using static ECDH\n");
+ }
+ choices->auth_type = BR_AUTH_ECDH;
+ choices->hash_id = -1;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+ return;
+ }
+ }
+ if ((choices->hash_id = choose_hash(auth_types >> 8)) >= 0) {
+ if (zc->verbose) {
+ fprintf(stderr, "using ECDSA, hash = %d (%s)\n",
+ choices->hash_id,
+ hash_function_name(choices->hash_id));
+ }
+ choices->auth_type = BR_AUTH_ECDSA;
+ choices->chain = zc->chain;
+ choices->chain_len = zc->chain_len;
+ return;
+ }
+ break;
+ }
+ if (zc->verbose) {
+ fprintf(stderr, "no matching client certificate\n");
+ }
+ choices->chain = NULL;
+ choices->chain_len = 0;
+}
+
+static uint32_t
+cc_do_keyx(const br_ssl_client_certificate_class **pctx,
+ unsigned char *data, size_t *len)
+{
+ const br_ec_impl *iec;
+ ccert_context *zc;
+ size_t xoff, xlen;
+ uint32_t r;
+
+ zc = (ccert_context *)pctx;
+ iec = br_ec_get_default();
+ r = iec->mul(data, *len, zc->sk->key.ec.x,
+ zc->sk->key.ec.xlen, zc->sk->key.ec.curve);
+ xoff = iec->xoff(zc->sk->key.ec.curve, &xlen);
+ memmove(data, data + xoff, xlen);
+ *len = xlen;
+ return r;
+}
+
+static size_t
+cc_do_sign(const br_ssl_client_certificate_class **pctx,
+ int hash_id, size_t hv_len, unsigned char *data, size_t len)
+{
+ ccert_context *zc;
+ unsigned char hv[64];
+
+ zc = (ccert_context *)pctx;
+ memcpy(hv, data, hv_len);
+ switch (zc->sk->key_type) {
+ const br_hash_class *hc;
+ const unsigned char *hash_oid;
+ uint32_t x;
+ size_t sig_len;
+
+ case BR_KEYTYPE_RSA:
+ hash_oid = get_hash_oid(hash_id);
+ if (hash_oid == NULL && hash_id != 0) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: cannot RSA-sign with"
+ " unknown hash function: %d\n",
+ hash_id);
+ }
+ return 0;
+ }
+ sig_len = (zc->sk->key.rsa.n_bitlen + 7) >> 3;
+ if (len < sig_len) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: cannot RSA-sign,"
+ " buffer is too small"
+ " (sig=%lu, buf=%lu)\n",
+ (unsigned long)sig_len,
+ (unsigned long)len);
+ }
+ return 0;
+ }
+ x = br_rsa_pkcs1_sign_get_default()(
+ hash_oid, hv, hv_len, &zc->sk->key.rsa, data);
+ if (!x) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: RSA-sign failure\n");
+ }
+ return 0;
+ }
+ return sig_len;
+
+ case BR_KEYTYPE_EC:
+ hc = get_hash_impl(hash_id);
+ if (hc == NULL) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: cannot ECDSA-sign with"
+ " unknown hash function: %d\n",
+ hash_id);
+ }
+ return 0;
+ }
+ if (len < 139) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: cannot ECDSA-sign"
+ " (output buffer = %lu)\n",
+ (unsigned long)len);
+ }
+ return 0;
+ }
+ sig_len = br_ecdsa_sign_asn1_get_default()(
+ br_ec_get_default(), hc, hv, &zc->sk->key.ec, data);
+ if (sig_len == 0) {
+ if (zc->verbose) {
+ fprintf(stderr, "ERROR: ECDSA-sign failure\n");
+ }
+ return 0;
+ }
+ return sig_len;
+
+ default:
+ return 0;
+ }
+}
+
+static const br_ssl_client_certificate_class ccert_vtable = {
+ sizeof(ccert_context),
+ cc_start_name_list,
+ cc_start_name,
+ cc_append_name,
+ cc_end_name,
+ cc_end_name_list,
+ cc_choose,
+ cc_do_keyx,
+ cc_do_sign
+};
+
+static void
+free_alpn(void *alpn)
+{
+ xfree(*(char **)alpn);
+}
+
+static void
+usage_client(void)
+{
+ fprintf(stderr,
+"usage: brssl client server[:port] [ options ]\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+ fprintf(stderr,
+" -trace activate extra debug messages (dump of all packets)\n");
+ fprintf(stderr,
+" -sni name use this specific name for SNI\n");
+ fprintf(stderr,
+" -nosni do not send any SNI\n");
+ fprintf(stderr,
+" -mono use monodirectional buffering\n");
+ fprintf(stderr,
+" -buf length set the I/O buffer length (in bytes)\n");
+ fprintf(stderr,
+" -CA file add certificates in 'file' to trust anchors\n");
+ fprintf(stderr,
+" -cert file set client certificate chain\n");
+ fprintf(stderr,
+" -key file set client private key (for certificate authentication)\n");
+ fprintf(stderr,
+" -nostaticecdh prohibit full-static ECDH (client certificate)\n");
+ fprintf(stderr,
+" -list list supported names (protocols, algorithms...)\n");
+ fprintf(stderr,
+" -vmin name set minimum supported version (default: TLS-1.0)\n");
+ fprintf(stderr,
+" -vmax name set maximum supported version (default: TLS-1.2)\n");
+ fprintf(stderr,
+" -cs names set list of supported cipher suites (comma-separated)\n");
+ fprintf(stderr,
+" -hf names add support for some hash functions (comma-separated)\n");
+ fprintf(stderr,
+" -minhello len set minimum ClientHello length (in bytes)\n");
+ fprintf(stderr,
+" -fallback send the TLS_FALLBACK_SCSV (i.e. claim a downgrade)\n");
+ fprintf(stderr,
+" -noreneg prohibit renegotiations\n");
+ fprintf(stderr,
+" -alpn name add protocol name to list of protocols (ALPN extension)\n");
+ fprintf(stderr,
+" -strictalpn fail on ALPN mismatch\n");
+}
+
+/* see brssl.h */
+int
+do_client(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int trace;
+ int i, bidi;
+ const char *server_name;
+ char *host;
+ char *port;
+ const char *sni;
+ anchor_list anchors = VEC_INIT;
+ unsigned vmin, vmax;
+ VECTOR(char *) alpn_names = VEC_INIT;
+ cipher_suite *suites;
+ size_t num_suites;
+ uint16_t *suite_ids;
+ unsigned hfuns;
+ size_t u;
+ br_ssl_client_context cc;
+ br_x509_minimal_context xc;
+ x509_noanchor_context xwc;
+ const br_hash_class *dnhash;
+ ccert_context zc;
+ br_x509_certificate *chain;
+ size_t chain_len;
+ private_key *sk;
+ int nostaticecdh;
+ unsigned char *iobuf;
+ size_t iobuf_len;
+ size_t minhello_len;
+ int fallback;
+ uint32_t flags;
+ SOCKET fd;
+
+ retcode = 0;
+ verbose = 1;
+ trace = 0;
+ server_name = NULL;
+ host = NULL;
+ port = NULL;
+ sni = NULL;
+ bidi = 1;
+ vmin = 0;
+ vmax = 0;
+ suites = NULL;
+ num_suites = 0;
+ hfuns = 0;
+ suite_ids = NULL;
+ chain = NULL;
+ chain_len = 0;
+ sk = NULL;
+ nostaticecdh = 0;
+ iobuf = NULL;
+ iobuf_len = 0;
+ minhello_len = (size_t)-1;
+ fallback = 0;
+ flags = 0;
+ fd = INVALID_SOCKET;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ if (server_name != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate server name\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ server_name = arg;
+ continue;
+ }
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else if (eqstr(arg, "-trace")) {
+ trace = 1;
+ } else if (eqstr(arg, "-sni")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-sni'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ if (sni != NULL) {
+ fprintf(stderr, "ERROR: duplicate SNI\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ sni = argv[i];
+ } else if (eqstr(arg, "-nosni")) {
+ if (sni != NULL) {
+ fprintf(stderr, "ERROR: duplicate SNI\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ sni = "";
+ } else if (eqstr(arg, "-mono")) {
+ bidi = 0;
+ } else if (eqstr(arg, "-buf")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-buf'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (iobuf_len != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate I/O buffer length\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ iobuf_len = parse_size(arg);
+ if (iobuf_len == (size_t)-1) {
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-CA")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-CA'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (read_trust_anchors(&anchors, arg) == 0) {
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-cert")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cert'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ if (chain != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate certificate chain\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ chain = read_certificates(arg, &chain_len);
+ if (chain == NULL || chain_len == 0) {
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-key")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-key'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ if (sk != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate private key\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ sk = read_private_key(arg);
+ if (sk == NULL) {
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-nostaticecdh")) {
+ nostaticecdh = 1;
+ } else if (eqstr(arg, "-list")) {
+ list_names();
+ goto client_exit;
+ } else if (eqstr(arg, "-vmin")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmin'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (vmin != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate minimum version\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ vmin = parse_version(arg, strlen(arg));
+ if (vmin == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-vmax")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmax'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (vmax != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate maximum version\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ vmax = parse_version(arg, strlen(arg));
+ if (vmax == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-cs")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cs'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (suites != NULL) {
+ fprintf(stderr, "ERROR: duplicate list"
+ " of cipher suites\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ suites = parse_suites(arg, &num_suites);
+ if (suites == NULL) {
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-hf")) {
+ unsigned x;
+
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-hf'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ x = parse_hash_functions(arg);
+ if (x == 0) {
+ usage_client();
+ goto client_exit_error;
+ }
+ hfuns |= x;
+ } else if (eqstr(arg, "-minhello")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-minhello'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ arg = argv[i];
+ if (minhello_len != (size_t)-1) {
+ fprintf(stderr, "ERROR: duplicate minimum"
+ " ClientHello length\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ minhello_len = parse_size(arg);
+ /*
+ * Minimum ClientHello length must fit on 16 bits.
+ */
+ if (minhello_len == (size_t)-1
+ || (((minhello_len >> 12) >> 4) != 0))
+ {
+ usage_client();
+ goto client_exit_error;
+ }
+ } else if (eqstr(arg, "-fallback")) {
+ fallback = 1;
+ } else if (eqstr(arg, "-noreneg")) {
+ flags |= BR_OPT_NO_RENEGOTIATION;
+ } else if (eqstr(arg, "-alpn")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-alpn'\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ VEC_ADD(alpn_names, xstrdup(argv[i]));
+ } else if (eqstr(arg, "-strictalpn")) {
+ flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_client();
+ goto client_exit_error;
+ }
+ }
+ if (server_name == NULL) {
+ fprintf(stderr, "ERROR: no server name/address provided\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ for (u = strlen(server_name); u > 0; u --) {
+ int c = server_name[u - 1];
+ if (c == ':') {
+ break;
+ }
+ if (c < '0' || c > '9') {
+ u = 0;
+ break;
+ }
+ }
+ if (u == 0) {
+ host = xstrdup(server_name);
+ port = xstrdup("443");
+ } else {
+ port = xstrdup(server_name + u);
+ host = xmalloc(u);
+ memcpy(host, server_name, u - 1);
+ host[u - 1] = 0;
+ }
+ if (sni == NULL) {
+ sni = host;
+ }
+
+ if (chain == NULL && sk != NULL) {
+ fprintf(stderr, "ERROR: private key specified, but"
+ " no certificate chain\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ if (chain != NULL && sk == NULL) {
+ fprintf(stderr, "ERROR: certificate chain specified, but"
+ " no private key\n");
+ usage_client();
+ goto client_exit_error;
+ }
+
+ if (vmin == 0) {
+ vmin = BR_TLS10;
+ }
+ if (vmax == 0) {
+ vmax = BR_TLS12;
+ }
+ if (vmax < vmin) {
+ fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
+ " version combination\n");
+ usage_client();
+ goto client_exit_error;
+ }
+ if (suites == NULL) {
+ num_suites = 0;
+
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ num_suites ++;
+ }
+ }
+ suites = xmalloc(num_suites * sizeof *suites);
+ num_suites = 0;
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ suites[num_suites ++] = cipher_suites[u];
+ }
+ }
+ }
+ if (hfuns == 0) {
+ hfuns = (unsigned)-1;
+ }
+ if (iobuf_len == 0) {
+ if (bidi) {
+ iobuf_len = BR_SSL_BUFSIZE_BIDI;
+ } else {
+ iobuf_len = BR_SSL_BUFSIZE_MONO;
+ }
+ }
+ iobuf = xmalloc(iobuf_len);
+
+ /*
+ * Compute implementation requirements and inject implementations.
+ */
+ suite_ids = xmalloc((num_suites + 1) * sizeof *suite_ids);
+ br_ssl_client_zero(&cc);
+ br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
+ dnhash = NULL;
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ dnhash = hc;
+ }
+ }
+ if (dnhash == NULL) {
+ fprintf(stderr, "ERROR: no supported hash function\n");
+ goto client_exit_error;
+ }
+ br_x509_minimal_init(&xc, dnhash,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ if (vmin <= BR_TLS11) {
+ if (!(hfuns & (1 << br_md5_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
+ goto client_exit_error;
+ }
+ if (!(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
+ goto client_exit_error;
+ }
+ }
+ for (u = 0; u < num_suites; u ++) {
+ unsigned req;
+
+ req = suites[u].req;
+ suite_ids[u] = suites[u].suite;
+ if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires TLS 1.2\n",
+ suites[u].name);
+ goto client_exit_error;
+ }
+ if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-1\n",
+ suites[u].name);
+ goto client_exit_error;
+ }
+ if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-256\n",
+ suites[u].name);
+ goto client_exit_error;
+ }
+ if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-384\n",
+ suites[u].name);
+ goto client_exit_error;
+ }
+ /* TODO: algorithm implementation selection */
+ if ((req & REQ_AESCBC) != 0) {
+ br_ssl_engine_set_default_aes_cbc(&cc.eng);
+ }
+ if ((req & REQ_AESCCM) != 0) {
+ br_ssl_engine_set_default_aes_ccm(&cc.eng);
+ }
+ if ((req & REQ_AESGCM) != 0) {
+ br_ssl_engine_set_default_aes_gcm(&cc.eng);
+ }
+ if ((req & REQ_CHAPOL) != 0) {
+ br_ssl_engine_set_default_chapol(&cc.eng);
+ }
+ if ((req & REQ_3DESCBC) != 0) {
+ br_ssl_engine_set_default_des_cbc(&cc.eng);
+ }
+ if ((req & REQ_RSAKEYX) != 0) {
+ br_ssl_client_set_default_rsapub(&cc);
+ }
+ if ((req & REQ_ECDHE_RSA) != 0) {
+ br_ssl_engine_set_default_ec(&cc.eng);
+ br_ssl_engine_set_default_rsavrfy(&cc.eng);
+ }
+ if ((req & REQ_ECDHE_ECDSA) != 0) {
+ br_ssl_engine_set_default_ecdsa(&cc.eng);
+ }
+ if ((req & REQ_ECDH) != 0) {
+ br_ssl_engine_set_default_ec(&cc.eng);
+ }
+ }
+ if (fallback) {
+ suite_ids[num_suites ++] = 0x5600;
+ }
+ br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
+
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ br_ssl_engine_set_hash(&cc.eng, id, hc);
+ br_x509_minimal_set_hash(&xc, id, hc);
+ }
+ }
+ if (vmin <= BR_TLS11) {
+ br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
+ }
+ if (vmax >= BR_TLS12) {
+ if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
+ br_ssl_engine_set_prf_sha256(&cc.eng,
+ &br_tls12_sha256_prf);
+ }
+ if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
+ br_ssl_engine_set_prf_sha384(&cc.eng,
+ &br_tls12_sha384_prf);
+ }
+ }
+ br_x509_minimal_set_rsa(&xc, br_rsa_pkcs1_vrfy_get_default());
+ br_x509_minimal_set_ecdsa(&xc,
+ br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
+
+ /*
+ * If there is no provided trust anchor, then certificate validation
+ * will always fail. In that situation, we use our custom wrapper
+ * that tolerates unknown anchors.
+ */
+ if (VEC_LEN(anchors) == 0) {
+ if (verbose) {
+ fprintf(stderr,
+ "WARNING: no configured trust anchor\n");
+ }
+ x509_noanchor_init(&xwc, &xc.vtable);
+ br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
+ } else {
+ br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
+ }
+
+ if (minhello_len != (size_t)-1) {
+ br_ssl_client_set_min_clienthello_len(&cc, minhello_len);
+ }
+ br_ssl_engine_set_all_flags(&cc.eng, flags);
+ if (VEC_LEN(alpn_names) != 0) {
+ br_ssl_engine_set_protocol_names(&cc.eng,
+ (const char **)&VEC_ELT(alpn_names, 0),
+ VEC_LEN(alpn_names));
+ }
+
+ if (chain != NULL) {
+ zc.vtable = &ccert_vtable;
+ zc.verbose = verbose;
+ zc.chain = chain;
+ zc.chain_len = chain_len;
+ zc.sk = sk;
+ if (nostaticecdh || sk->key_type != BR_KEYTYPE_EC) {
+ zc.issuer_key_type = 0;
+ } else {
+ zc.issuer_key_type = get_cert_signer_algo(&chain[0]);
+ if (zc.issuer_key_type == 0) {
+ goto client_exit_error;
+ }
+ }
+ br_ssl_client_set_client_certificate(&cc, &zc.vtable);
+ }
+
+ br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
+ br_ssl_client_reset(&cc, sni, 0);
+
+ /*
+ * On Unix systems, we need to avoid SIGPIPE.
+ */
+#ifndef _WIN32
+ signal(SIGPIPE, SIG_IGN);
+#endif
+
+ /*
+ * Connect to the peer.
+ */
+ fd = host_connect(host, port, verbose);
+ if (fd == INVALID_SOCKET) {
+ goto client_exit_error;
+ }
+
+ /*
+ * Run the engine until completion.
+ */
+ if (run_ssl_engine(&cc.eng, fd,
+ (verbose ? RUN_ENGINE_VERBOSE : 0)
+ | (trace ? RUN_ENGINE_TRACE : 0)) != 0)
+ {
+ goto client_exit_error;
+ } else {
+ goto client_exit;
+ }
+
+ /*
+ * Release allocated structures.
+ */
+client_exit:
+ xfree(host);
+ xfree(port);
+ xfree(suites);
+ xfree(suite_ids);
+ VEC_CLEAREXT(anchors, &free_ta_contents);
+ VEC_CLEAREXT(alpn_names, &free_alpn);
+ free_certificates(chain, chain_len);
+ free_private_key(sk);
+ xfree(iobuf);
+ if (fd != INVALID_SOCKET) {
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ }
+ return retcode;
+
+client_exit_error:
+ retcode = -1;
+ goto client_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/errors.c b/test/monniaux/BearSSL/tools/errors.c
new file mode 100644
index 00000000..22f0c305
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/errors.c
@@ -0,0 +1,344 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+static struct {
+ int err;
+ const char *name;
+ const char *comment;
+} errors[] = {
+ {
+ BR_ERR_BAD_PARAM,
+ "BR_ERR_BAD_PARAM",
+ "Caller-provided parameter is incorrect."
+ }, {
+ BR_ERR_BAD_STATE,
+ "BR_ERR_BAD_STATE",
+ "Operation requested by the caller cannot be applied with"
+ " the current context state (e.g. reading data while"
+ " outgoing data is waiting to be sent)."
+ }, {
+ BR_ERR_UNSUPPORTED_VERSION,
+ "BR_ERR_UNSUPPORTED_VERSION",
+ "Incoming protocol or record version is unsupported."
+ }, {
+ BR_ERR_BAD_VERSION,
+ "BR_ERR_BAD_VERSION",
+ "Incoming record version does not match the expected version."
+ }, {
+ BR_ERR_BAD_LENGTH,
+ "BR_ERR_BAD_LENGTH",
+ "Incoming record length is invalid."
+ }, {
+ BR_ERR_TOO_LARGE,
+ "BR_ERR_TOO_LARGE",
+ "Incoming record is too large to be processed, or buffer"
+ " is too small for the handshake message to send."
+ }, {
+ BR_ERR_BAD_MAC,
+ "BR_ERR_BAD_MAC",
+ "Decryption found an invalid padding, or the record MAC is"
+ " not correct."
+ }, {
+ BR_ERR_NO_RANDOM,
+ "BR_ERR_NO_RANDOM",
+ "No initial entropy was provided, and none can be obtained"
+ " from the OS."
+ }, {
+ BR_ERR_UNKNOWN_TYPE,
+ "BR_ERR_UNKNOWN_TYPE",
+ "Incoming record type is unknown."
+ }, {
+ BR_ERR_UNEXPECTED,
+ "BR_ERR_UNEXPECTED",
+ "Incoming record or message has wrong type with regards to"
+ " the current engine state."
+ }, {
+ BR_ERR_BAD_CCS,
+ "BR_ERR_BAD_CCS",
+ "ChangeCipherSpec message from the peer has invalid contents."
+ }, {
+ BR_ERR_BAD_ALERT,
+ "BR_ERR_BAD_ALERT",
+ "Alert message from the peer has invalid contents"
+ " (odd length)."
+ }, {
+ BR_ERR_BAD_HANDSHAKE,
+ "BR_ERR_BAD_HANDSHAKE",
+ "Incoming handshake message decoding failed."
+ }, {
+ BR_ERR_OVERSIZED_ID,
+ "BR_ERR_OVERSIZED_ID",
+ "ServerHello contains a session ID which is larger than"
+ " 32 bytes."
+ }, {
+ BR_ERR_BAD_CIPHER_SUITE,
+ "BR_ERR_BAD_CIPHER_SUITE",
+ "Server wants to use a cipher suite that we did not claim"
+ " to support. This is also reported if we tried to advertise"
+ " a cipher suite that we do not support."
+ }, {
+ BR_ERR_BAD_COMPRESSION,
+ "BR_ERR_BAD_COMPRESSION",
+ "Server wants to use a compression that we did not claim"
+ " to support."
+ }, {
+ BR_ERR_BAD_FRAGLEN,
+ "BR_ERR_BAD_FRAGLEN",
+ "Server's max fragment length does not match client's."
+ }, {
+ BR_ERR_BAD_SECRENEG,
+ "BR_ERR_BAD_SECRENEG",
+ "Secure renegotiation failed."
+ }, {
+ BR_ERR_EXTRA_EXTENSION,
+ "BR_ERR_EXTRA_EXTENSION",
+ "Server sent an extension type that we did not announce,"
+ " or used the same extension type several times in a"
+ " single ServerHello."
+ }, {
+ BR_ERR_BAD_SNI,
+ "BR_ERR_BAD_SNI",
+ "Invalid Server Name Indication contents (when used by"
+ " the server, this extension shall be empty)."
+ }, {
+ BR_ERR_BAD_HELLO_DONE,
+ "BR_ERR_BAD_HELLO_DONE",
+ "Invalid ServerHelloDone from the server (length is not 0)."
+ }, {
+ BR_ERR_LIMIT_EXCEEDED,
+ "BR_ERR_LIMIT_EXCEEDED",
+ "Internal limit exceeded (e.g. server's public key is too"
+ " large)."
+ }, {
+ BR_ERR_BAD_FINISHED,
+ "BR_ERR_BAD_FINISHED",
+ "Finished message from peer does not match the expected"
+ " value."
+ }, {
+ BR_ERR_RESUME_MISMATCH,
+ "BR_ERR_RESUME_MISMATCH",
+ "Session resumption attempt with distinct version or cipher"
+ " suite."
+ }, {
+ BR_ERR_INVALID_ALGORITHM,
+ "BR_ERR_INVALID_ALGORITHM",
+ "Unsupported or invalid algorithm (ECDHE curve, signature"
+ " algorithm, hash function)."
+ }, {
+ BR_ERR_BAD_SIGNATURE,
+ "BR_ERR_BAD_SIGNATURE",
+ "Invalid signature in ServerKeyExchange or"
+ " CertificateVerify message."
+ }, {
+ BR_ERR_WRONG_KEY_USAGE,
+ "BR_ERR_WRONG_KEY_USAGE",
+ "Peer's public key does not have the proper type or is"
+ " not allowed for the requested operation."
+ }, {
+ BR_ERR_NO_CLIENT_AUTH,
+ "BR_ERR_NO_CLIENT_AUTH",
+ "Client did not send a certificate upon request, or the"
+ " client certificate could not be validated."
+ }, {
+ BR_ERR_IO,
+ "BR_ERR_IO",
+ "I/O error or premature close on transport stream."
+ }, {
+ BR_ERR_X509_INVALID_VALUE,
+ "BR_ERR_X509_INVALID_VALUE",
+ "Invalid value in an ASN.1 structure."
+ },
+ {
+ BR_ERR_X509_TRUNCATED,
+ "BR_ERR_X509_TRUNCATED",
+ "Truncated certificate or other ASN.1 object."
+ },
+ {
+ BR_ERR_X509_EMPTY_CHAIN,
+ "BR_ERR_X509_EMPTY_CHAIN",
+ "Empty certificate chain (no certificate at all)."
+ },
+ {
+ BR_ERR_X509_INNER_TRUNC,
+ "BR_ERR_X509_INNER_TRUNC",
+ "Decoding error: inner element extends beyond outer element"
+ " size."
+ },
+ {
+ BR_ERR_X509_BAD_TAG_CLASS,
+ "BR_ERR_X509_BAD_TAG_CLASS",
+ "Decoding error: unsupported tag class (application or"
+ " private)."
+ },
+ {
+ BR_ERR_X509_BAD_TAG_VALUE,
+ "BR_ERR_X509_BAD_TAG_VALUE",
+ "Decoding error: unsupported tag value."
+ },
+ {
+ BR_ERR_X509_INDEFINITE_LENGTH,
+ "BR_ERR_X509_INDEFINITE_LENGTH",
+ "Decoding error: indefinite length."
+ },
+ {
+ BR_ERR_X509_EXTRA_ELEMENT,
+ "BR_ERR_X509_EXTRA_ELEMENT",
+ "Decoding error: extraneous element."
+ },
+ {
+ BR_ERR_X509_UNEXPECTED,
+ "BR_ERR_X509_UNEXPECTED",
+ "Decoding error: unexpected element."
+ },
+ {
+ BR_ERR_X509_NOT_CONSTRUCTED,
+ "BR_ERR_X509_NOT_CONSTRUCTED",
+ "Decoding error: expected constructed element, but is"
+ " primitive."
+ },
+ {
+ BR_ERR_X509_NOT_PRIMITIVE,
+ "BR_ERR_X509_NOT_PRIMITIVE",
+ "Decoding error: expected primitive element, but is"
+ " constructed."
+ },
+ {
+ BR_ERR_X509_PARTIAL_BYTE,
+ "BR_ERR_X509_PARTIAL_BYTE",
+ "Decoding error: BIT STRING length is not multiple of 8."
+ },
+ {
+ BR_ERR_X509_BAD_BOOLEAN,
+ "BR_ERR_X509_BAD_BOOLEAN",
+ "Decoding error: BOOLEAN value has invalid length."
+ },
+ {
+ BR_ERR_X509_OVERFLOW,
+ "BR_ERR_X509_OVERFLOW",
+ "Decoding error: value is off-limits."
+ },
+ {
+ BR_ERR_X509_BAD_DN,
+ "BR_ERR_X509_BAD_DN",
+ "Invalid distinguished name."
+ },
+ {
+ BR_ERR_X509_BAD_TIME,
+ "BR_ERR_X509_BAD_TIME",
+ "Invalid date/time representation."
+ },
+ {
+ BR_ERR_X509_UNSUPPORTED,
+ "BR_ERR_X509_UNSUPPORTED",
+ "Certificate contains unsupported features that cannot be"
+ " ignored."
+ },
+ {
+ BR_ERR_X509_LIMIT_EXCEEDED,
+ "BR_ERR_X509_LIMIT_EXCEEDED",
+ "Key or signature size exceeds internal limits."
+ },
+ {
+ BR_ERR_X509_WRONG_KEY_TYPE,
+ "BR_ERR_X509_WRONG_KEY_TYPE",
+ "Key type does not match that which was expected."
+ },
+ {
+ BR_ERR_X509_BAD_SIGNATURE,
+ "BR_ERR_X509_BAD_SIGNATURE",
+ "Signature is invalid."
+ },
+ {
+ BR_ERR_X509_TIME_UNKNOWN,
+ "BR_ERR_X509_TIME_UNKNOWN",
+ "Validation time is unknown."
+ },
+ {
+ BR_ERR_X509_EXPIRED,
+ "BR_ERR_X509_EXPIRED",
+ "Certificate is expired or not yet valid."
+ },
+ {
+ BR_ERR_X509_DN_MISMATCH,
+ "BR_ERR_X509_DN_MISMATCH",
+ "Issuer/Subject DN mismatch in the chain."
+ },
+ {
+ BR_ERR_X509_BAD_SERVER_NAME,
+ "BR_ERR_X509_BAD_SERVER_NAME",
+ "Expected server name was not found in the chain."
+ },
+ {
+ BR_ERR_X509_CRITICAL_EXTENSION,
+ "BR_ERR_X509_CRITICAL_EXTENSION",
+ "Unknown critical extension in certificate."
+ },
+ {
+ BR_ERR_X509_NOT_CA,
+ "BR_ERR_X509_NOT_CA",
+ "Not a CA, or path length constraint violation."
+ },
+ {
+ BR_ERR_X509_FORBIDDEN_KEY_USAGE,
+ "BR_ERR_X509_FORBIDDEN_KEY_USAGE",
+ "Key Usage extension prohibits intended usage."
+ },
+ {
+ BR_ERR_X509_WEAK_PUBLIC_KEY,
+ "BR_ERR_X509_WEAK_PUBLIC_KEY",
+ "Public key found in certificate is too small."
+ },
+ {
+ BR_ERR_X509_NOT_TRUSTED,
+ "BR_ERR_X509_NOT_TRUSTED",
+ "Chain could not be linked to a trust anchor."
+ },
+ { 0, 0, 0 }
+};
+
+/* see brssl.h */
+const char *
+find_error_name(int err, const char **comment)
+{
+ size_t u;
+
+ for (u = 0; errors[u].name; u ++) {
+ if (errors[u].err == err) {
+ if (comment != NULL) {
+ *comment = errors[u].comment;
+ }
+ return errors[u].name;
+ }
+ }
+ return NULL;
+}
diff --git a/test/monniaux/BearSSL/tools/files.c b/test/monniaux/BearSSL/tools/files.c
new file mode 100644
index 00000000..8bf67cc3
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/files.c
@@ -0,0 +1,329 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+
+/* see brssl.h */
+unsigned char *
+read_file(const char *fname, size_t *len)
+{
+ bvector vbuf = VEC_INIT;
+ FILE *f;
+
+ *len = 0;
+ f = fopen(fname, "rb");
+ if (f == NULL) {
+ fprintf(stderr,
+ "ERROR: could not open file '%s' for reading\n", fname);
+ return NULL;
+ }
+ for (;;) {
+ unsigned char tmp[1024];
+ size_t rlen;
+
+ rlen = fread(tmp, 1, sizeof tmp, f);
+ if (rlen == 0) {
+ unsigned char *buf;
+
+ if (ferror(f)) {
+ fprintf(stderr,
+ "ERROR: read error on file '%s'\n",
+ fname);
+ fclose(f);
+ return NULL;
+ }
+ buf = VEC_TOARRAY(vbuf);
+ *len = VEC_LEN(vbuf);
+ VEC_CLEAR(vbuf);
+ fclose(f);
+ return buf;
+ }
+ VEC_ADDMANY(vbuf, tmp, rlen);
+ }
+}
+
+/* see brssl.h */
+int
+write_file(const char *fname, const void *data, size_t len)
+{
+ FILE *f;
+ const unsigned char *buf;
+
+ f = fopen(fname, "wb");
+ if (f == NULL) {
+ fprintf(stderr,
+ "ERROR: could not open file '%s' for reading\n", fname);
+ return -1;
+ }
+ buf = data;
+ while (len > 0) {
+ size_t wlen;
+
+ wlen = fwrite(buf, 1, len, f);
+ if (wlen == 0) {
+ fprintf(stderr,
+ "ERROR: could not write all bytes to '%s'\n",
+ fname);
+ fclose(f);
+ return -1;
+ }
+ buf += wlen;
+ len -= wlen;
+ }
+ if (ferror(f)) {
+ fprintf(stderr, "ERROR: write error on file '%s'\n", fname);
+ fclose(f);
+ return -1;
+ }
+ fclose(f);
+ return 0;
+}
+
+/* see brssl.h */
+int
+looks_like_DER(const unsigned char *buf, size_t len)
+{
+ int fb;
+ size_t dlen;
+
+ if (len < 2) {
+ return 0;
+ }
+ if (*buf ++ != 0x30) {
+ return 0;
+ }
+ fb = *buf ++;
+ len -= 2;
+ if (fb < 0x80) {
+ return (size_t)fb == len;
+ } else if (fb == 0x80) {
+ return 0;
+ } else {
+ fb -= 0x80;
+ if (len < (size_t)fb + 2) {
+ return 0;
+ }
+ len -= (size_t)fb;
+ dlen = 0;
+ while (fb -- > 0) {
+ if (dlen > (len >> 8)) {
+ return 0;
+ }
+ dlen = (dlen << 8) + (size_t)*buf ++;
+ }
+ return dlen == len;
+ }
+}
+
+static void
+vblob_append(void *cc, const void *data, size_t len)
+{
+ bvector *bv;
+
+ bv = cc;
+ VEC_ADDMANY(*bv, data, len);
+}
+
+/* see brssl.h */
+void
+free_pem_object_contents(pem_object *po)
+{
+ if (po != NULL) {
+ xfree(po->name);
+ xfree(po->data);
+ }
+}
+
+/* see brssl.h */
+pem_object *
+decode_pem(const void *src, size_t len, size_t *num)
+{
+ VECTOR(pem_object) pem_list = VEC_INIT;
+ br_pem_decoder_context pc;
+ pem_object po, *pos;
+ const unsigned char *buf;
+ bvector bv = VEC_INIT;
+ int inobj;
+ int extra_nl;
+
+ *num = 0;
+ br_pem_decoder_init(&pc);
+ buf = src;
+ inobj = 0;
+ po.name = NULL;
+ po.data = NULL;
+ po.data_len = 0;
+ extra_nl = 1;
+ while (len > 0) {
+ size_t tlen;
+
+ tlen = br_pem_decoder_push(&pc, buf, len);
+ buf += tlen;
+ len -= tlen;
+ switch (br_pem_decoder_event(&pc)) {
+
+ case BR_PEM_BEGIN_OBJ:
+ po.name = xstrdup(br_pem_decoder_name(&pc));
+ br_pem_decoder_setdest(&pc, vblob_append, &bv);
+ inobj = 1;
+ break;
+
+ case BR_PEM_END_OBJ:
+ if (inobj) {
+ po.data = VEC_TOARRAY(bv);
+ po.data_len = VEC_LEN(bv);
+ VEC_ADD(pem_list, po);
+ VEC_CLEAR(bv);
+ po.name = NULL;
+ po.data = NULL;
+ po.data_len = 0;
+ inobj = 0;
+ }
+ break;
+
+ case BR_PEM_ERROR:
+ xfree(po.name);
+ VEC_CLEAR(bv);
+ fprintf(stderr,
+ "ERROR: invalid PEM encoding\n");
+ VEC_CLEAREXT(pem_list, &free_pem_object_contents);
+ return NULL;
+ }
+
+ /*
+ * We add an extra newline at the end, in order to
+ * support PEM files that lack the newline on their last
+ * line (this is somwehat invalid, but PEM format is not
+ * standardised and such files do exist in the wild, so
+ * we'd better accept them).
+ */
+ if (len == 0 && extra_nl) {
+ extra_nl = 0;
+ buf = (const unsigned char *)"\n";
+ len = 1;
+ }
+ }
+ if (inobj) {
+ fprintf(stderr, "ERROR: unfinished PEM object\n");
+ xfree(po.name);
+ VEC_CLEAR(bv);
+ VEC_CLEAREXT(pem_list, &free_pem_object_contents);
+ return NULL;
+ }
+
+ *num = VEC_LEN(pem_list);
+ VEC_ADD(pem_list, po);
+ pos = VEC_TOARRAY(pem_list);
+ VEC_CLEAR(pem_list);
+ return pos;
+}
+
+/* see brssl.h */
+br_x509_certificate *
+read_certificates(const char *fname, size_t *num)
+{
+ VECTOR(br_x509_certificate) cert_list = VEC_INIT;
+ unsigned char *buf;
+ size_t len;
+ pem_object *pos;
+ size_t u, num_pos;
+ br_x509_certificate *xcs;
+ br_x509_certificate dummy;
+
+ *num = 0;
+
+ /*
+ * TODO: reading the whole file is crude; we could parse them
+ * in a streamed fashion. But it does not matter much in practice.
+ */
+ buf = read_file(fname, &len);
+ if (buf == NULL) {
+ return NULL;
+ }
+
+ /*
+ * Check for a DER-encoded certificate.
+ */
+ if (looks_like_DER(buf, len)) {
+ xcs = xmalloc(2 * sizeof *xcs);
+ xcs[0].data = buf;
+ xcs[0].data_len = len;
+ xcs[1].data = NULL;
+ xcs[1].data_len = 0;
+ *num = 1;
+ return xcs;
+ }
+
+ pos = decode_pem(buf, len, &num_pos);
+ xfree(buf);
+ if (pos == NULL) {
+ return NULL;
+ }
+ for (u = 0; u < num_pos; u ++) {
+ if (eqstr(pos[u].name, "CERTIFICATE")
+ || eqstr(pos[u].name, "X509 CERTIFICATE"))
+ {
+ br_x509_certificate xc;
+
+ xc.data = pos[u].data;
+ xc.data_len = pos[u].data_len;
+ pos[u].data = NULL;
+ VEC_ADD(cert_list, xc);
+ }
+ }
+ for (u = 0; u < num_pos; u ++) {
+ free_pem_object_contents(&pos[u]);
+ }
+ xfree(pos);
+
+ if (VEC_LEN(cert_list) == 0) {
+ fprintf(stderr, "ERROR: no certificate in file '%s'\n", fname);
+ return NULL;
+ }
+ *num = VEC_LEN(cert_list);
+ dummy.data = NULL;
+ dummy.data_len = 0;
+ VEC_ADD(cert_list, dummy);
+ xcs = VEC_TOARRAY(cert_list);
+ VEC_CLEAR(cert_list);
+ return xcs;
+}
+
+/* see brssl.h */
+void
+free_certificates(br_x509_certificate *certs, size_t num)
+{
+ size_t u;
+
+ for (u = 0; u < num; u ++) {
+ xfree(certs[u].data);
+ }
+ xfree(certs);
+}
diff --git a/test/monniaux/BearSSL/tools/impl.c b/test/monniaux/BearSSL/tools/impl.c
new file mode 100644
index 00000000..e00cc327
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/impl.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+/* see brssl.h */
+int
+do_impl(int argc, char *argv[])
+{
+ const br_config_option *opt;
+
+ (void)argc;
+ (void)argv;
+
+ for (opt = br_get_config(); opt->name != NULL; opt ++) {
+ printf("%-25s %8ld\n", opt->name, opt->value);
+ }
+
+ return 0;
+}
diff --git a/test/monniaux/BearSSL/tools/keys.c b/test/monniaux/BearSSL/tools/keys.c
new file mode 100644
index 00000000..6e546763
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/keys.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+static private_key *
+decode_key(const unsigned char *buf, size_t len)
+{
+ br_skey_decoder_context dc;
+ int err;
+ private_key *sk;
+
+ br_skey_decoder_init(&dc);
+ br_skey_decoder_push(&dc, buf, len);
+ err = br_skey_decoder_last_error(&dc);
+ if (err != 0) {
+ const char *errname, *errmsg;
+
+ fprintf(stderr, "ERROR (decoding): err=%d\n", err);
+ errname = find_error_name(err, &errmsg);
+ if (errname != NULL) {
+ fprintf(stderr, " %s: %s\n", errname, errmsg);
+ } else {
+ fprintf(stderr, " (unknown)\n");
+ }
+ return NULL;
+ }
+ switch (br_skey_decoder_key_type(&dc)) {
+ const br_rsa_private_key *rk;
+ const br_ec_private_key *ek;
+
+ case BR_KEYTYPE_RSA:
+ rk = br_skey_decoder_get_rsa(&dc);
+ sk = xmalloc(sizeof *sk);
+ sk->key_type = BR_KEYTYPE_RSA;
+ sk->key.rsa.n_bitlen = rk->n_bitlen;
+ sk->key.rsa.p = xblobdup(rk->p, rk->plen);
+ sk->key.rsa.plen = rk->plen;
+ sk->key.rsa.q = xblobdup(rk->q, rk->qlen);
+ sk->key.rsa.qlen = rk->qlen;
+ sk->key.rsa.dp = xblobdup(rk->dp, rk->dplen);
+ sk->key.rsa.dplen = rk->dplen;
+ sk->key.rsa.dq = xblobdup(rk->dq, rk->dqlen);
+ sk->key.rsa.dqlen = rk->dqlen;
+ sk->key.rsa.iq = xblobdup(rk->iq, rk->iqlen);
+ sk->key.rsa.iqlen = rk->iqlen;
+ break;
+
+ case BR_KEYTYPE_EC:
+ ek = br_skey_decoder_get_ec(&dc);
+ sk = xmalloc(sizeof *sk);
+ sk->key_type = BR_KEYTYPE_EC;
+ sk->key.ec.curve = ek->curve;
+ sk->key.ec.x = xblobdup(ek->x, ek->xlen);
+ sk->key.ec.xlen = ek->xlen;
+ break;
+
+ default:
+ fprintf(stderr, "Unknown key type: %d\n",
+ br_skey_decoder_key_type(&dc));
+ sk = NULL;
+ break;
+ }
+
+ return sk;
+}
+
+/* see brssl.h */
+private_key *
+read_private_key(const char *fname)
+{
+ unsigned char *buf;
+ size_t len;
+ private_key *sk;
+ pem_object *pos;
+ size_t num, u;
+
+ buf = NULL;
+ pos = NULL;
+ sk = NULL;
+ buf = read_file(fname, &len);
+ if (buf == NULL) {
+ goto deckey_exit;
+ }
+ if (looks_like_DER(buf, len)) {
+ sk = decode_key(buf, len);
+ goto deckey_exit;
+ } else {
+ pos = decode_pem(buf, len, &num);
+ if (pos == NULL) {
+ goto deckey_exit;
+ }
+ for (u = 0; pos[u].name; u ++) {
+ const char *name;
+
+ name = pos[u].name;
+ if (eqstr(name, "RSA PRIVATE KEY")
+ || eqstr(name, "EC PRIVATE KEY")
+ || eqstr(name, "PRIVATE KEY"))
+ {
+ sk = decode_key(pos[u].data, pos[u].data_len);
+ goto deckey_exit;
+ }
+ }
+ fprintf(stderr, "ERROR: no private key in file '%s'\n", fname);
+ goto deckey_exit;
+ }
+
+deckey_exit:
+ if (buf != NULL) {
+ xfree(buf);
+ }
+ if (pos != NULL) {
+ for (u = 0; pos[u].name; u ++) {
+ free_pem_object_contents(&pos[u]);
+ }
+ xfree(pos);
+ }
+ return sk;
+}
+
+/* see brssl.h */
+void
+free_private_key(private_key *sk)
+{
+ if (sk == NULL) {
+ return;
+ }
+ switch (sk->key_type) {
+ case BR_KEYTYPE_RSA:
+ xfree(sk->key.rsa.p);
+ xfree(sk->key.rsa.q);
+ xfree(sk->key.rsa.dp);
+ xfree(sk->key.rsa.dq);
+ xfree(sk->key.rsa.iq);
+ break;
+ case BR_KEYTYPE_EC:
+ xfree(sk->key.ec.x);
+ break;
+ }
+ xfree(sk);
+}
+
+/*
+ * OID for hash functions in RSA signatures.
+ */
+static const unsigned char HASH_OID_SHA1[] = {
+ 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
+};
+
+static const unsigned char HASH_OID_SHA224[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
+};
+
+static const unsigned char HASH_OID_SHA256[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
+};
+
+static const unsigned char HASH_OID_SHA384[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
+};
+
+static const unsigned char HASH_OID_SHA512[] = {
+ 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static const unsigned char *HASH_OID[] = {
+ HASH_OID_SHA1,
+ HASH_OID_SHA224,
+ HASH_OID_SHA256,
+ HASH_OID_SHA384,
+ HASH_OID_SHA512
+};
+
+/* see brssl.h */
+const unsigned char *
+get_hash_oid(int id)
+{
+ if (id >= 2 && id <= 6) {
+ return HASH_OID[id - 2];
+ } else {
+ return NULL;
+ }
+}
+
+/* see brssl.h */
+const br_hash_class *
+get_hash_impl(int hash_id)
+{
+ size_t u;
+
+ if (hash_id == 0) {
+ return &br_md5sha1_vtable;
+ }
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
+ if (id == hash_id) {
+ return hc;
+ }
+ }
+ return NULL;
+}
diff --git a/test/monniaux/BearSSL/tools/names.c b/test/monniaux/BearSSL/tools/names.c
new file mode 100644
index 00000000..3751d93f
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/names.c
@@ -0,0 +1,1056 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "brssl.h"
+#include "bearssl.h"
+
+/* see brssl.h */
+const protocol_version protocol_versions[] = {
+ { "tls10", BR_TLS10, "TLS 1.0" },
+ { "tls11", BR_TLS11, "TLS 1.1" },
+ { "tls12", BR_TLS12, "TLS 1.2" },
+ { NULL, 0, NULL }
+};
+
+/* see brssl.h */
+const hash_function hash_functions[] = {
+ { "md5", &br_md5_vtable, "MD5" },
+ { "sha1", &br_sha1_vtable, "SHA-1" },
+ { "sha224", &br_sha224_vtable, "SHA-224" },
+ { "sha256", &br_sha256_vtable, "SHA-256" },
+ { "sha384", &br_sha384_vtable, "SHA-384" },
+ { "sha512", &br_sha512_vtable, "SHA-512" },
+ { NULL, 0, NULL }
+};
+
+/* see brssl.h */
+const cipher_suite cipher_suites[] = {
+ {
+ "ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
+ BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ REQ_ECDHE_ECDSA | REQ_CHAPOL | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
+ BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ REQ_ECDHE_RSA | REQ_CHAPOL | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with RSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
+ REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-128/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_128_GCM_SHA256",
+ BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+ REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with RSA, AES-128/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
+ REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-256/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_256_GCM_SHA384",
+ BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+ REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
+ "ECDHE with RSA, AES-256/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_128_CCM",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
+ REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-128/CCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_256_CCM",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
+ REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-256/CCM encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_128_CCM_8",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
+ REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-128/CCM_8 encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_256_CCM_8",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
+ REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-256/CCM_8 encryption (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
+ REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_128_CBC_SHA256",
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+ REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "ECDHE with RSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
+ REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
+ "ECDHE with ECDSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_256_CBC_SHA384",
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
+ REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
+ "ECDHE with RSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
+ REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
+ "ECDHE with ECDSA, AES-128/CBC + SHA-1"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_128_CBC_SHA",
+ BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+ REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
+ "ECDHE with RSA, AES-128/CBC + SHA-1"
+ },
+ {
+ "ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
+ BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
+ REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
+ "ECDHE with ECDSA, AES-256/CBC + SHA-1"
+ },
+ {
+ "ECDHE_RSA_WITH_AES_256_CBC_SHA",
+ BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+ REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
+ "ECDHE with RSA, AES-256/CBC + SHA-1"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
+ REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
+ "ECDH key exchange (EC cert), AES-128/GCM (TLS 1.2+)"
+ },
+ {
+ "ECDH_RSA_WITH_AES_128_GCM_SHA256",
+ BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
+ REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
+ "ECDH key exchange (RSA cert), AES-128/GCM (TLS 1.2+)"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
+ REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
+ "ECDH key exchange (EC cert), AES-256/GCM (TLS 1.2+)"
+ },
+ {
+ "ECDH_RSA_WITH_AES_256_GCM_SHA384",
+ BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
+ REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
+ "ECDH key exchange (RSA cert), AES-256/GCM (TLS 1.2+)"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
+ },
+ {
+ "ECDH_RSA_WITH_AES_128_CBC_SHA256",
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
+ "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
+ },
+ {
+ "ECDH_RSA_WITH_AES_256_CBC_SHA384",
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
+ "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_128_CBC_SHA",
+ BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA1,
+ "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-1"
+ },
+ {
+ "ECDH_RSA_WITH_AES_128_CBC_SHA",
+ BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA1,
+ "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-1"
+ },
+ {
+ "ECDH_ECDSA_WITH_AES_256_CBC_SHA",
+ BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA1,
+ "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-1"
+ },
+ {
+ "ECDH_RSA_WITH_AES_256_CBC_SHA",
+ BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
+ REQ_ECDH | REQ_AESCBC | REQ_SHA1,
+ "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-1"
+ },
+ {
+ "RSA_WITH_AES_128_GCM_SHA256",
+ BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
+ REQ_RSAKEYX | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-128/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_256_GCM_SHA384",
+ BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
+ REQ_RSAKEYX | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
+ "RSA key exchange, AES-256/GCM encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_128_CCM",
+ BR_TLS_RSA_WITH_AES_128_CCM,
+ REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-128/CCM encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_256_CCM",
+ BR_TLS_RSA_WITH_AES_256_CCM,
+ REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-256/CCM encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_128_CCM_8",
+ BR_TLS_RSA_WITH_AES_128_CCM_8,
+ REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-128/CCM_8 encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_256_CCM_8",
+ BR_TLS_RSA_WITH_AES_256_CCM_8,
+ REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-256/CCM_8 encryption (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_128_CBC_SHA256",
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
+ REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_256_CBC_SHA256",
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
+ REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
+ "RSA key exchange, AES-256/CBC + HMAC/SHA-256 (TLS 1.2+)"
+ },
+ {
+ "RSA_WITH_AES_128_CBC_SHA",
+ BR_TLS_RSA_WITH_AES_128_CBC_SHA,
+ REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
+ "RSA key exchange, AES-128/CBC + HMAC/SHA-1"
+ },
+ {
+ "RSA_WITH_AES_256_CBC_SHA",
+ BR_TLS_RSA_WITH_AES_256_CBC_SHA,
+ REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
+ "RSA key exchange, AES-256/CBC + HMAC/SHA-1"
+ },
+ {
+ "ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
+ BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ REQ_ECDHE_ECDSA | REQ_3DESCBC | REQ_SHA1,
+ "ECDHE with ECDSA, 3DES/CBC + SHA-1"
+ },
+ {
+ "ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
+ BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ REQ_ECDHE_RSA | REQ_3DESCBC | REQ_SHA1,
+ "ECDHE with RSA, 3DES/CBC + SHA-1"
+ },
+ {
+ "ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",
+ BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
+ REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
+ "ECDH key exchange (EC cert), 3DES/CBC + HMAC/SHA-1"
+ },
+ {
+ "ECDH_RSA_WITH_3DES_EDE_CBC_SHA",
+ BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
+ REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
+ "ECDH key exchange (RSA cert), 3DES/CBC + HMAC/SHA-1"
+ },
+ {
+ "RSA_WITH_3DES_EDE_CBC_SHA",
+ BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
+ REQ_RSAKEYX | REQ_3DESCBC | REQ_SHA1,
+ "RSA key exchange, 3DES/CBC + HMAC/SHA-1"
+ },
+ { NULL, 0, 0, NULL }
+};
+
+static const struct {
+ int id;
+ const char *name;
+ const char *sid[4];
+} curves[] = {
+ { BR_EC_sect163k1,
+ "sect163k1",
+ { "sect163k1", "K-163", NULL, NULL } },
+ { BR_EC_sect163r1,
+ "sect163r1",
+ { "sect163r1", NULL, NULL, NULL } },
+ { BR_EC_sect163r2,
+ "sect163r2",
+ { "sect163r2", "B-163", NULL, NULL } },
+ { BR_EC_sect193r1,
+ "sect193r1",
+ { "sect193r1", NULL, NULL, NULL } },
+ { BR_EC_sect193r2,
+ "sect193r2",
+ { "sect193r2", NULL, NULL, NULL } },
+ { BR_EC_sect233k1,
+ "sect233k1",
+ { "sect233k1", "K-233", NULL, NULL } },
+ { BR_EC_sect233r1,
+ "sect233r1",
+ { "sect233r1", "B-233", NULL, NULL } },
+ { BR_EC_sect239k1,
+ "sect239k1",
+ { "sect239k1", NULL, NULL, NULL } },
+ { BR_EC_sect283k1,
+ "sect283k1",
+ { "sect283k1", "K-283", NULL, NULL } },
+ { BR_EC_sect283r1,
+ "sect283r1",
+ { "sect283r1", "B-283", NULL, NULL } },
+ { BR_EC_sect409k1,
+ "sect409k1",
+ { "sect409k1", "K-409", NULL, NULL } },
+ { BR_EC_sect409r1,
+ "sect409r1",
+ { "sect409r1", "B-409", NULL, NULL } },
+ { BR_EC_sect571k1,
+ "sect571k1",
+ { "sect571k1", "K-571", NULL, NULL } },
+ { BR_EC_sect571r1,
+ "sect571r1",
+ { "sect571r1", "B-571", NULL, NULL } },
+ { BR_EC_secp160k1,
+ "secp160k1",
+ { "secp160k1", NULL, NULL, NULL } },
+ { BR_EC_secp160r1,
+ "secp160r1",
+ { "secp160r1", NULL, NULL, NULL } },
+ { BR_EC_secp160r2,
+ "secp160r2",
+ { "secp160r2", NULL, NULL, NULL } },
+ { BR_EC_secp192k1,
+ "secp192k1",
+ { "secp192k1", NULL, NULL, NULL } },
+ { BR_EC_secp192r1,
+ "secp192r1",
+ { "secp192r1", "P-192", NULL, NULL } },
+ { BR_EC_secp224k1,
+ "secp224k1",
+ { "secp224k1", NULL, NULL, NULL } },
+ { BR_EC_secp224r1,
+ "secp224r1",
+ { "secp224r1", "P-224", NULL, NULL } },
+ { BR_EC_secp256k1,
+ "secp256k1",
+ { "secp256k1", NULL, NULL, NULL } },
+ { BR_EC_secp256r1,
+ "secp256r1 (P-256)",
+ { "secp256r1", "P-256", "prime256v1", NULL } },
+ { BR_EC_secp384r1,
+ "secp384r1 (P-384)",
+ { "secp384r1", "P-384", NULL, NULL } },
+ { BR_EC_secp521r1,
+ "secp521r1 (P-521)",
+ { "secp521r1", "P-521", NULL, NULL } },
+ { BR_EC_brainpoolP256r1,
+ "brainpoolP256r1",
+ { "brainpoolP256r1", NULL, NULL, NULL } },
+ { BR_EC_brainpoolP384r1,
+ "brainpoolP384r1",
+ { "brainpoolP384r1", NULL, NULL, NULL } },
+ { BR_EC_brainpoolP512r1,
+ "brainpoolP512r1",
+ { "brainpoolP512r1", NULL, NULL, NULL } },
+ { BR_EC_curve25519,
+ "Curve25519",
+ { "curve25519", "c25519", NULL, NULL } },
+ { BR_EC_curve448,
+ "Curve448",
+ { "curve448", "c448", NULL, NULL } },
+ { 0, 0, { 0, 0, 0, 0 } }
+};
+
+static const struct {
+ const char *long_name;
+ const char *short_name;
+ const void *impl;
+} algo_names[] = {
+ /* Block ciphers */
+ { "aes_big_cbcenc", "big", &br_aes_big_cbcenc_vtable },
+ { "aes_big_cbcdec", "big", &br_aes_big_cbcdec_vtable },
+ { "aes_big_ctr", "big", &br_aes_big_ctr_vtable },
+ { "aes_big_ctrcbc", "big", &br_aes_big_ctrcbc_vtable },
+ { "aes_small_cbcenc", "small", &br_aes_small_cbcenc_vtable },
+ { "aes_small_cbcdec", "small", &br_aes_small_cbcdec_vtable },
+ { "aes_small_ctr", "small", &br_aes_small_ctr_vtable },
+ { "aes_small_ctrcbc", "small", &br_aes_small_ctrcbc_vtable },
+ { "aes_ct_cbcenc", "ct", &br_aes_ct_cbcenc_vtable },
+ { "aes_ct_cbcdec", "ct", &br_aes_ct_cbcdec_vtable },
+ { "aes_ct_ctr", "ct", &br_aes_ct_ctr_vtable },
+ { "aes_ct_ctrcbc", "ct", &br_aes_ct_ctrcbc_vtable },
+ { "aes_ct64_cbcenc", "ct64", &br_aes_ct64_cbcenc_vtable },
+ { "aes_ct64_cbcdec", "ct64", &br_aes_ct64_cbcdec_vtable },
+ { "aes_ct64_ctr", "ct64", &br_aes_ct64_ctr_vtable },
+ { "aes_ct64_ctrcbc", "ct64", &br_aes_ct64_ctrcbc_vtable },
+
+ { "des_tab_cbcenc", "tab", &br_des_tab_cbcenc_vtable },
+ { "des_tab_cbcdec", "tab", &br_des_tab_cbcdec_vtable },
+ { "des_ct_cbcenc", "ct", &br_des_ct_cbcenc_vtable },
+ { "des_ct_cbcdec", "ct", &br_des_ct_cbcdec_vtable },
+
+ { "chacha20_ct", "ct", &br_chacha20_ct_run },
+
+ { "ghash_ctmul", "ctmul", &br_ghash_ctmul },
+ { "ghash_ctmul32", "ctmul32", &br_ghash_ctmul32 },
+ { "ghash_ctmul64", "ctmul64", &br_ghash_ctmul64 },
+
+ { "poly1305_ctmul", "ctmul", &br_poly1305_ctmul_run },
+ { "poly1305_ctmul32", "ctmul32", &br_poly1305_ctmul32_run },
+
+ { "ec_all_m15", "all_m15", &br_ec_all_m15 },
+ { "ec_all_m31", "all_m31", &br_ec_all_m31 },
+ { "ec_c25519_i15", "c25519_i15", &br_ec_c25519_i15 },
+ { "ec_c25519_i31", "c25519_i31", &br_ec_c25519_i31 },
+ { "ec_c25519_m15", "c25519_m15", &br_ec_c25519_m15 },
+ { "ec_c25519_m31", "c25519_m31", &br_ec_c25519_m31 },
+ { "ec_p256_m15", "p256_m15", &br_ec_p256_m15 },
+ { "ec_p256_m31", "p256_m31", &br_ec_p256_m31 },
+ { "ec_prime_i15", "prime_i15", &br_ec_prime_i15 },
+ { "ec_prime_i31", "prime_i31", &br_ec_prime_i31 },
+
+ { "ecdsa_i15_sign_asn1", "i15_asn1", &br_ecdsa_i15_sign_asn1 },
+ { "ecdsa_i15_sign_raw", "i15_raw", &br_ecdsa_i15_sign_raw },
+ { "ecdsa_i31_sign_asn1", "i31_asn1", &br_ecdsa_i31_sign_asn1 },
+ { "ecdsa_i31_sign_raw", "i31_raw", &br_ecdsa_i31_sign_raw },
+ { "ecdsa_i15_vrfy_asn1", "i15_asn1", &br_ecdsa_i15_vrfy_asn1 },
+ { "ecdsa_i15_vrfy_raw", "i15_raw", &br_ecdsa_i15_vrfy_raw },
+ { "ecdsa_i31_vrfy_asn1", "i31_asn1", &br_ecdsa_i31_vrfy_asn1 },
+ { "ecdsa_i31_vrfy_raw", "i31_raw", &br_ecdsa_i31_vrfy_raw },
+
+ { "rsa_i15_pkcs1_sign", "i15", &br_rsa_i15_pkcs1_sign },
+ { "rsa_i31_pkcs1_sign", "i31", &br_rsa_i31_pkcs1_sign },
+ { "rsa_i32_pkcs1_sign", "i32", &br_rsa_i32_pkcs1_sign },
+ { "rsa_i15_pkcs1_vrfy", "i15", &br_rsa_i15_pkcs1_vrfy },
+ { "rsa_i31_pkcs1_vrfy", "i31", &br_rsa_i31_pkcs1_vrfy },
+ { "rsa_i32_pkcs1_vrfy", "i32", &br_rsa_i32_pkcs1_vrfy },
+
+ { 0, 0, 0 }
+};
+
+static const struct {
+ const char *long_name;
+ const char *short_name;
+ const void *(*get)(void);
+} algo_names_dyn[] = {
+ { "aes_pwr8_cbcenc", "pwr8",
+ (const void *(*)(void))&br_aes_pwr8_cbcenc_get_vtable },
+ { "aes_pwr8_cbcdec", "pwr8",
+ (const void *(*)(void))&br_aes_pwr8_cbcdec_get_vtable },
+ { "aes_pwr8_ctr", "pwr8",
+ (const void *(*)(void))&br_aes_pwr8_ctr_get_vtable },
+ { "aes_pwr8_ctrcbc", "pwr8",
+ (const void *(*)(void))&br_aes_pwr8_ctrcbc_get_vtable },
+ { "aes_x86ni_cbcenc", "x86ni",
+ (const void *(*)(void))&br_aes_x86ni_cbcenc_get_vtable },
+ { "aes_x86ni_cbcdec", "x86ni",
+ (const void *(*)(void))&br_aes_x86ni_cbcdec_get_vtable },
+ { "aes_x86ni_ctr", "x86ni",
+ (const void *(*)(void))&br_aes_x86ni_ctr_get_vtable },
+ { "aes_x86ni_ctrcbc", "x86ni",
+ (const void *(*)(void))&br_aes_x86ni_ctrcbc_get_vtable },
+ { "chacha20_sse2", "sse2",
+ (const void *(*)(void))&br_chacha20_sse2_get },
+ { "ghash_pclmul", "pclmul",
+ (const void *(*)(void))&br_ghash_pclmul_get },
+ { "ghash_pwr8", "pwr8",
+ (const void *(*)(void))&br_ghash_pwr8_get },
+ { "poly1305_ctmulq", "ctmulq",
+ (const void *(*)(void))&br_poly1305_ctmulq_get },
+ { "rsa_i62_pkcs1_sign", "i62",
+ (const void *(*)(void))&br_rsa_i62_pkcs1_sign_get },
+ { "rsa_i62_pkcs1_vrfy", "i62",
+ (const void *(*)(void))&br_rsa_i62_pkcs1_vrfy_get },
+ { "ec_c25519_m62", "m62",
+ (const void *(*)(void))&br_ec_c25519_m62_get },
+ { "ec_c25519_m64", "m64",
+ (const void *(*)(void))&br_ec_c25519_m64_get },
+ { "ec_p256_m62", "m62",
+ (const void *(*)(void))&br_ec_p256_m62_get },
+ { "ec_p256_m64", "m64",
+ (const void *(*)(void))&br_ec_p256_m64_get },
+ { 0, 0, 0, }
+};
+
+/* see brssl.h */
+const char *
+get_algo_name(const void *impl, int long_name)
+{
+ size_t u;
+
+ for (u = 0; algo_names[u].long_name; u ++) {
+ if (impl == algo_names[u].impl) {
+ return long_name
+ ? algo_names[u].long_name
+ : algo_names[u].short_name;
+ }
+ }
+ for (u = 0; algo_names_dyn[u].long_name; u ++) {
+ if (impl == algo_names_dyn[u].get()) {
+ return long_name
+ ? algo_names_dyn[u].long_name
+ : algo_names_dyn[u].short_name;
+ }
+ }
+ return "UNKNOWN";
+}
+
+/* see brssl.h */
+const char *
+get_curve_name(int id)
+{
+ size_t u;
+
+ for (u = 0; curves[u].name; u ++) {
+ if (curves[u].id == id) {
+ return curves[u].name;
+ }
+ }
+ return NULL;
+}
+
+/* see brssl.h */
+int
+get_curve_name_ext(int id, char *dst, size_t len)
+{
+ const char *name;
+ char tmp[30];
+ size_t n;
+
+ name = get_curve_name(id);
+ if (name == NULL) {
+ sprintf(tmp, "unknown (%d)", id);
+ name = tmp;
+ }
+ n = 1 + strlen(name);
+ if (n > len) {
+ if (len > 0) {
+ dst[0] = 0;
+ }
+ return -1;
+ }
+ memcpy(dst, name, n);
+ return 0;
+}
+
+/* see brssl.h */
+const char *
+get_suite_name(unsigned suite)
+{
+ size_t u;
+
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if (cipher_suites[u].suite == suite) {
+ return cipher_suites[u].name;
+ }
+ }
+ return NULL;
+}
+
+/* see brssl.h */
+int
+get_suite_name_ext(unsigned suite, char *dst, size_t len)
+{
+ const char *name;
+ char tmp[30];
+ size_t n;
+
+ name = get_suite_name(suite);
+ if (name == NULL) {
+ sprintf(tmp, "unknown (0x%04X)", suite);
+ name = tmp;
+ }
+ n = 1 + strlen(name);
+ if (n > len) {
+ if (len > 0) {
+ dst[0] = 0;
+ }
+ return -1;
+ }
+ memcpy(dst, name, n);
+ return 0;
+}
+
+/* see brssl.h */
+int
+uses_ecdhe(unsigned suite)
+{
+ size_t u;
+
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if (cipher_suites[u].suite == suite) {
+ return (cipher_suites[u].req
+ & (REQ_ECDHE_RSA | REQ_ECDHE_ECDSA)) != 0;
+ }
+ }
+ return 0;
+}
+
+/* see brssl.h */
+void
+list_names(void)
+{
+ size_t u;
+
+ printf("Protocol versions:\n");
+ for (u = 0; protocol_versions[u].name; u ++) {
+ printf(" %-8s %s\n",
+ protocol_versions[u].name,
+ protocol_versions[u].comment);
+ }
+ printf("Hash functions:\n");
+ for (u = 0; hash_functions[u].name; u ++) {
+ printf(" %-8s %s\n",
+ hash_functions[u].name,
+ hash_functions[u].comment);
+ }
+ printf("Cipher suites:\n");
+ for (u = 0; cipher_suites[u].name; u ++) {
+ printf(" %s\n %s\n",
+ cipher_suites[u].name,
+ cipher_suites[u].comment);
+ }
+}
+
+/* see brssl.h */
+void
+list_curves(void)
+{
+ size_t u;
+ for (u = 0; curves[u].name; u ++) {
+ size_t v;
+
+ for (v = 0; curves[u].sid[v]; v ++) {
+ if (v == 0) {
+ printf(" ");
+ } else if (v == 1) {
+ printf(" (");
+ } else {
+ printf(", ");
+ }
+ printf("%s", curves[u].sid[v]);
+ }
+ if (v > 1) {
+ printf(")");
+ }
+ printf("\n");
+ }
+}
+
+static int
+is_ign(int c)
+{
+ if (c == 0) {
+ return 0;
+ }
+ if (c <= 32 || c == '-' || c == '_' || c == '.'
+ || c == '/' || c == '+' || c == ':')
+ {
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * Get next non-ignored character, normalised:
+ * ASCII letters are converted to lowercase
+ * control characters, space, '-', '_', '.', '/', '+' and ':' are ignored
+ * A terminating zero is returned as 0.
+ */
+static int
+next_char(const char **ps, const char *limit)
+{
+ for (;;) {
+ int c;
+
+ if (*ps == limit) {
+ return 0;
+ }
+ c = *(*ps) ++;
+ if (c == 0) {
+ return 0;
+ }
+ if (c >= 'A' && c <= 'Z') {
+ c += 'a' - 'A';
+ }
+ if (!is_ign(c)) {
+ return c;
+ }
+ }
+}
+
+/*
+ * Partial string equality comparison, with normalisation.
+ */
+static int
+eqstr_chunk(const char *s1, size_t s1_len, const char *s2, size_t s2_len)
+{
+ const char *lim1, *lim2;
+
+ lim1 = s1 + s1_len;
+ lim2 = s2 + s2_len;
+ for (;;) {
+ int c1, c2;
+
+ c1 = next_char(&s1, lim1);
+ c2 = next_char(&s2, lim2);
+ if (c1 != c2) {
+ return 0;
+ }
+ if (c1 == 0) {
+ return 1;
+ }
+ }
+}
+
+/* see brssl.h */
+int
+eqstr(const char *s1, const char *s2)
+{
+ return eqstr_chunk(s1, strlen(s1), s2, strlen(s2));
+}
+
+static int
+hexval(int c)
+{
+ if (c >= '0' && c <= '9') {
+ return c - '0';
+ } else if (c >= 'A' && c <= 'F') {
+ return c - 'A' + 10;
+ } else if (c >= 'a' && c <= 'f') {
+ return c - 'a' + 10;
+ } else {
+ return -1;
+ }
+}
+
+/* see brssl.h */
+size_t
+parse_size(const char *s)
+{
+ int radix;
+ size_t acc;
+ const char *t;
+
+ t = s;
+ if (t[0] == '0' && (t[1] == 'x' || t[1] == 'X')) {
+ radix = 16;
+ t += 2;
+ } else {
+ radix = 10;
+ }
+ acc = 0;
+ for (;;) {
+ int c, d;
+ size_t z;
+
+ c = *t ++;
+ if (c == 0) {
+ return acc;
+ }
+ d = hexval(c);
+ if (d < 0 || d >= radix) {
+ fprintf(stderr, "ERROR: not a valid digit: '%c'\n", c);
+ return (size_t)-1;
+ }
+ z = acc * (size_t)radix + (size_t)d;
+ if (z < (size_t)d || (z / (size_t)radix) != acc
+ || z == (size_t)-1)
+ {
+ fprintf(stderr, "ERROR: value too large: %s\n", s);
+ return (size_t)-1;
+ }
+ acc = z;
+ }
+}
+
+/*
+ * Comma-separated list enumeration. This returns a pointer to the first
+ * word in the string, skipping leading ignored characters. '*len' is
+ * set to the word length (not counting trailing ignored characters).
+ * '*str' is updated to point to immediately after the next comma, or to
+ * the terminating zero, whichever comes first.
+ *
+ * Empty words are skipped. If there is no next non-empty word, then this
+ * function returns NULL and sets *len to 0.
+ */
+static const char *
+next_word(const char **str, size_t *len)
+{
+ int c;
+ const char *begin;
+ size_t u;
+
+ /*
+ * Find next non-ignored character which is not a comma.
+ */
+ for (;;) {
+ c = **str;
+ if (c == 0) {
+ *len = 0;
+ return NULL;
+ }
+ if (!is_ign(c) && c != ',') {
+ break;
+ }
+ (*str) ++;
+ }
+
+ /*
+ * Find next comma or terminator.
+ */
+ begin = *str;
+ for (;;) {
+ c = *(*str);
+ if (c == 0 || c == ',') {
+ break;
+ }
+ (*str) ++;
+ }
+
+ /*
+ * Remove trailing ignored characters.
+ */
+ u = (size_t)(*str - begin);
+ while (u > 0 && is_ign(begin[u - 1])) {
+ u --;
+ }
+ if (c == ',') {
+ (*str) ++;
+ }
+ *len = u;
+ return begin;
+}
+
+/* see brssl.h */
+unsigned
+parse_version(const char *name, size_t len)
+{
+ size_t u;
+
+ for (u = 0;; u ++) {
+ const char *ref;
+
+ ref = protocol_versions[u].name;
+ if (ref == NULL) {
+ fprintf(stderr, "ERROR: unrecognised protocol"
+ " version name: '%s'\n", name);
+ return 0;
+ }
+ if (eqstr_chunk(ref, strlen(ref), name, len)) {
+ return protocol_versions[u].version;
+ }
+ }
+}
+
+/* see brssl.h */
+unsigned
+parse_hash_functions(const char *arg)
+{
+ unsigned r;
+
+ r = 0;
+ for (;;) {
+ const char *name;
+ size_t len;
+ size_t u;
+
+ name = next_word(&arg, &len);
+ if (name == NULL) {
+ break;
+ }
+ for (u = 0;; u ++) {
+ const char *ref;
+
+ ref = hash_functions[u].name;
+ if (ref == 0) {
+ fprintf(stderr, "ERROR: unrecognised"
+ " hash function name: '");
+ fwrite(name, 1, len, stderr);
+ fprintf(stderr, "'\n");
+ return 0;
+ }
+ if (eqstr_chunk(ref, strlen(ref), name, len)) {
+ int id;
+
+ id = (hash_functions[u].hclass->desc
+ >> BR_HASHDESC_ID_OFF)
+ & BR_HASHDESC_ID_MASK;
+ r |= (unsigned)1 << id;
+ break;
+ }
+ }
+ }
+ if (r == 0) {
+ fprintf(stderr, "ERROR: no hash function name provided\n");
+ }
+ return r;
+}
+
+/* see brssl.h */
+cipher_suite *
+parse_suites(const char *arg, size_t *num)
+{
+ VECTOR(cipher_suite) suites = VEC_INIT;
+ cipher_suite *r;
+
+ for (;;) {
+ const char *name;
+ size_t u, len;
+
+ name = next_word(&arg, &len);
+ if (name == NULL) {
+ break;
+ }
+ for (u = 0;; u ++) {
+ const char *ref;
+
+ ref = cipher_suites[u].name;
+ if (ref == NULL) {
+ fprintf(stderr, "ERROR: unrecognised"
+ " cipher suite '");
+ fwrite(name, 1, len, stderr);
+ fprintf(stderr, "'\n");
+ return 0;
+ }
+ if (eqstr_chunk(ref, strlen(ref), name, len)) {
+ VEC_ADD(suites, cipher_suites[u]);
+ break;
+ }
+ }
+ }
+ if (VEC_LEN(suites) == 0) {
+ fprintf(stderr, "ERROR: no cipher suite provided\n");
+ }
+ r = VEC_TOARRAY(suites);
+ *num = VEC_LEN(suites);
+ VEC_CLEAR(suites);
+ return r;
+}
+
+/* see brssl.h */
+const char *
+ec_curve_name(int curve)
+{
+ switch (curve) {
+ case BR_EC_sect163k1: return "sect163k1";
+ case BR_EC_sect163r1: return "sect163r1";
+ case BR_EC_sect163r2: return "sect163r2";
+ case BR_EC_sect193r1: return "sect193r1";
+ case BR_EC_sect193r2: return "sect193r2";
+ case BR_EC_sect233k1: return "sect233k1";
+ case BR_EC_sect233r1: return "sect233r1";
+ case BR_EC_sect239k1: return "sect239k1";
+ case BR_EC_sect283k1: return "sect283k1";
+ case BR_EC_sect283r1: return "sect283r1";
+ case BR_EC_sect409k1: return "sect409k1";
+ case BR_EC_sect409r1: return "sect409r1";
+ case BR_EC_sect571k1: return "sect571k1";
+ case BR_EC_sect571r1: return "sect571r1";
+ case BR_EC_secp160k1: return "secp160k1";
+ case BR_EC_secp160r1: return "secp160r1";
+ case BR_EC_secp160r2: return "secp160r2";
+ case BR_EC_secp192k1: return "secp192k1";
+ case BR_EC_secp192r1: return "secp192r1";
+ case BR_EC_secp224k1: return "secp224k1";
+ case BR_EC_secp224r1: return "secp224r1";
+ case BR_EC_secp256k1: return "secp256k1";
+ case BR_EC_secp256r1: return "secp256r1";
+ case BR_EC_secp384r1: return "secp384r1";
+ case BR_EC_secp521r1: return "secp521r1";
+ case BR_EC_brainpoolP256r1: return "brainpoolP256r1";
+ case BR_EC_brainpoolP384r1: return "brainpoolP384r1";
+ case BR_EC_brainpoolP512r1: return "brainpoolP512r1";
+ default:
+ return "unknown";
+ }
+}
+
+/* see brssl.h */
+int
+get_curve_by_name(const char *str)
+{
+ size_t u, v;
+
+ for (u = 0; curves[u].name; u ++) {
+ for (v = 0; curves[u].sid[v]; v ++) {
+ if (eqstr(curves[u].sid[v], str)) {
+ return curves[u].id;
+ }
+ }
+ }
+ return -1;
+}
+
+/* see brssl.h */
+const char *
+hash_function_name(int id)
+{
+ switch (id) {
+ case br_md5sha1_ID: return "MD5+SHA-1";
+ case br_md5_ID: return "MD5";
+ case br_sha1_ID: return "SHA-1";
+ case br_sha224_ID: return "SHA-224";
+ case br_sha256_ID: return "SHA-256";
+ case br_sha384_ID: return "SHA-384";
+ case br_sha512_ID: return "SHA-512";
+ default:
+ return "unknown";
+ }
+}
diff --git a/test/monniaux/BearSSL/tools/server.c b/test/monniaux/BearSSL/tools/server.c
new file mode 100644
index 00000000..a97de35d
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/server.c
@@ -0,0 +1,1235 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <signal.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define SOCKET int
+#define INVALID_SOCKET (-1)
+#define SOCKADDR_STORAGE struct sockaddr_storage
+#endif
+
+#include "brssl.h"
+
+static SOCKET
+host_bind(const char *host, const char *port, int verbose)
+{
+ struct addrinfo hints, *si, *p;
+ SOCKET fd;
+ int err;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ err = getaddrinfo(host, port, &hints, &si);
+ if (err != 0) {
+ fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
+ gai_strerror(err));
+ return INVALID_SOCKET;
+ }
+ fd = INVALID_SOCKET;
+ for (p = si; p != NULL; p = p->ai_next) {
+ struct sockaddr *sa;
+ struct sockaddr_in sa4;
+ struct sockaddr_in6 sa6;
+ size_t sa_len;
+ void *addr;
+ int opt;
+
+ sa = (struct sockaddr *)p->ai_addr;
+ if (sa->sa_family == AF_INET) {
+ memcpy(&sa4, sa, sizeof sa4);
+ sa = (struct sockaddr *)&sa4;
+ sa_len = sizeof sa4;
+ addr = &sa4.sin_addr;
+ if (host == NULL) {
+ sa4.sin_addr.s_addr = INADDR_ANY;
+ }
+ } else if (sa->sa_family == AF_INET6) {
+ memcpy(&sa6, sa, sizeof sa6);
+ sa = (struct sockaddr *)&sa6;
+ sa_len = sizeof sa6;
+ addr = &sa6.sin6_addr;
+ if (host == NULL) {
+ sa6.sin6_addr = in6addr_any;
+ }
+ } else {
+ addr = NULL;
+ sa_len = p->ai_addrlen;
+ }
+ if (verbose) {
+ char tmp[INET6_ADDRSTRLEN + 50];
+
+ if (addr != NULL) {
+ if (!inet_ntop(p->ai_family, addr,
+ tmp, sizeof tmp))
+ {
+ strcpy(tmp, "<invalid>");
+ }
+ } else {
+ sprintf(tmp, "<unknown family: %d>",
+ (int)sa->sa_family);
+ }
+ fprintf(stderr, "binding to: %s\n", tmp);
+ }
+ fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (fd == INVALID_SOCKET) {
+ if (verbose) {
+ perror("socket()");
+ }
+ continue;
+ }
+ opt = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
+ (void *)&opt, sizeof opt);
+#ifdef IPV6_V6ONLY
+ /*
+ * We want to make sure that the server socket works for
+ * both IPv4 and IPv6. But IPV6_V6ONLY is not defined on
+ * some very old systems.
+ */
+ opt = 0;
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
+ (void *)&opt, sizeof opt);
+#endif
+ if (bind(fd, sa, sa_len) < 0) {
+ if (verbose) {
+ perror("bind()");
+ }
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ continue;
+ }
+ break;
+ }
+ if (p == NULL) {
+ freeaddrinfo(si);
+ fprintf(stderr, "ERROR: failed to bind\n");
+ return INVALID_SOCKET;
+ }
+ freeaddrinfo(si);
+ if (listen(fd, 5) < 0) {
+ if (verbose) {
+ perror("listen()");
+ }
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ return INVALID_SOCKET;
+ }
+ if (verbose) {
+ fprintf(stderr, "bound.\n");
+ }
+ return fd;
+}
+
+static SOCKET
+accept_client(SOCKET server_fd, int verbose, int nonblock)
+{
+ int fd;
+ SOCKADDR_STORAGE sa;
+ socklen_t sa_len;
+
+ sa_len = sizeof sa;
+ fd = accept(server_fd, (struct sockaddr *)&sa, &sa_len);
+ if (fd == INVALID_SOCKET) {
+ if (verbose) {
+ perror("accept()");
+ }
+ return INVALID_SOCKET;
+ }
+ if (verbose) {
+ char tmp[INET6_ADDRSTRLEN + 50];
+ const char *name;
+
+ name = NULL;
+ switch (((struct sockaddr *)&sa)->sa_family) {
+ case AF_INET:
+ name = inet_ntop(AF_INET,
+ &((struct sockaddr_in *)&sa)->sin_addr,
+ tmp, sizeof tmp);
+ break;
+ case AF_INET6:
+ name = inet_ntop(AF_INET6,
+ &((struct sockaddr_in6 *)&sa)->sin6_addr,
+ tmp, sizeof tmp);
+ break;
+ }
+ if (name == NULL) {
+ sprintf(tmp, "<unknown: %lu>", (unsigned long)
+ ((struct sockaddr *)&sa)->sa_family);
+ name = tmp;
+ }
+ fprintf(stderr, "accepting connection from: %s\n", name);
+ }
+
+ /*
+ * We make the socket non-blocking, since we are going to use
+ * poll() or select() to organise I/O.
+ */
+ if (nonblock) {
+#ifdef _WIN32
+ u_long arg;
+
+ arg = 1;
+ ioctlsocket(fd, FIONBIO, &arg);
+#else
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+#endif
+ }
+ return fd;
+}
+
+static void
+usage_server(void)
+{
+ fprintf(stderr,
+"usage: brssl server [ options ]\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+ fprintf(stderr,
+" -trace activate extra debug messages (dump of all packets)\n");
+ fprintf(stderr,
+" -b name bind to a specific address or host name\n");
+ fprintf(stderr,
+" -p port bind to a specific port (default: 4433)\n");
+ fprintf(stderr,
+" -mono use monodirectional buffering\n");
+ fprintf(stderr,
+" -buf length set the I/O buffer length (in bytes)\n");
+ fprintf(stderr,
+" -cache length set the session cache storage length (in bytes)\n");
+ fprintf(stderr,
+" -cert fname read certificate chain from file 'fname'\n");
+ fprintf(stderr,
+" -key fname read private key from file 'fname'\n");
+ fprintf(stderr,
+" -CA file add trust anchors from 'file' (for client auth)\n");
+ fprintf(stderr,
+" -anon_ok request but do not require a client certificate\n");
+ fprintf(stderr,
+" -list list supported names (protocols, algorithms...)\n");
+ fprintf(stderr,
+" -vmin name set minimum supported version (default: TLS-1.0)\n");
+ fprintf(stderr,
+" -vmax name set maximum supported version (default: TLS-1.2)\n");
+ fprintf(stderr,
+" -cs names set list of supported cipher suites (comma-separated)\n");
+ fprintf(stderr,
+" -hf names add support for some hash functions (comma-separated)\n");
+ fprintf(stderr,
+" -cbhash test hashing in policy callback\n");
+ fprintf(stderr,
+" -serverpref enforce server's preferences for cipher suites\n");
+ fprintf(stderr,
+" -noreneg prohibit renegotiations\n");
+ fprintf(stderr,
+" -alpn name add protocol name to list of protocols (ALPN extension)\n");
+ fprintf(stderr,
+" -strictalpn fail on ALPN mismatch\n");
+ exit(EXIT_FAILURE);
+}
+
+typedef struct {
+ const br_ssl_server_policy_class *vtable;
+ int verbose;
+ br_x509_certificate *chain;
+ size_t chain_len;
+ int cert_signer_algo;
+ private_key *sk;
+ int cbhash;
+} policy_context;
+
+static void
+print_hashes(unsigned chashes)
+{
+ int i;
+
+ for (i = 2; i <= 6; i ++) {
+ if ((chashes >> i) & 1) {
+ int z;
+
+ switch (i) {
+ case 3: z = 224; break;
+ case 4: z = 256; break;
+ case 5: z = 384; break;
+ case 6: z = 512; break;
+ default:
+ z = 1;
+ break;
+ }
+ fprintf(stderr, " sha%d", z);
+ }
+ }
+}
+
+static unsigned
+choose_hash(unsigned chashes)
+{
+ unsigned hash_id;
+
+ for (hash_id = 6; hash_id >= 2; hash_id --) {
+ if (((chashes >> hash_id) & 1) != 0) {
+ return hash_id;
+ }
+ }
+ /*
+ * Normally unreachable.
+ */
+ return 0;
+}
+
+static int
+sp_choose(const br_ssl_server_policy_class **pctx,
+ const br_ssl_server_context *cc,
+ br_ssl_server_choices *choices)
+{
+ policy_context *pc;
+ const br_suite_translated *st;
+ size_t u, st_num;
+ unsigned chashes;
+
+ pc = (policy_context *)pctx;
+ st = br_ssl_server_get_client_suites(cc, &st_num);
+ chashes = br_ssl_server_get_client_hashes(cc);
+ if (pc->verbose) {
+ fprintf(stderr, "Client parameters:\n");
+ fprintf(stderr, " Maximum version: ");
+ switch (cc->client_max_version) {
+ case BR_SSL30:
+ fprintf(stderr, "SSL 3.0");
+ break;
+ case BR_TLS10:
+ fprintf(stderr, "TLS 1.0");
+ break;
+ case BR_TLS11:
+ fprintf(stderr, "TLS 1.1");
+ break;
+ case BR_TLS12:
+ fprintf(stderr, "TLS 1.2");
+ break;
+ default:
+ fprintf(stderr, "unknown (0x%04X)",
+ (unsigned)cc->client_max_version);
+ break;
+ }
+ fprintf(stderr, "\n");
+ fprintf(stderr, " Compatible cipher suites:\n");
+ for (u = 0; u < st_num; u ++) {
+ char csn[80];
+
+ get_suite_name_ext(st[u][0], csn, sizeof csn);
+ fprintf(stderr, " %s\n", csn);
+ }
+ fprintf(stderr, " Common sign+hash functions:\n");
+ if ((chashes & 0xFF) != 0) {
+ fprintf(stderr, " with RSA:");
+ print_hashes(chashes);
+ fprintf(stderr, "\n");
+ }
+ if ((chashes >> 8) != 0) {
+ fprintf(stderr, " with ECDSA:");
+ print_hashes(chashes >> 8);
+ fprintf(stderr, "\n");
+ }
+ }
+ for (u = 0; u < st_num; u ++) {
+ unsigned tt;
+
+ tt = st[u][1];
+ switch (tt >> 12) {
+ case BR_SSLKEYX_RSA:
+ if (pc->sk->key_type == BR_KEYTYPE_RSA) {
+ choices->cipher_suite = st[u][0];
+ goto choose_ok;
+ }
+ break;
+ case BR_SSLKEYX_ECDHE_RSA:
+ if (pc->sk->key_type == BR_KEYTYPE_RSA) {
+ choices->cipher_suite = st[u][0];
+ if (br_ssl_engine_get_version(&cc->eng)
+ < BR_TLS12)
+ {
+ if (pc->cbhash) {
+ choices->algo_id = 0x0001;
+ } else {
+ choices->algo_id = 0xFF00;
+ }
+ } else {
+ unsigned id;
+
+ id = choose_hash(chashes);
+ if (pc->cbhash) {
+ choices->algo_id =
+ (id << 8) + 0x01;
+ } else {
+ choices->algo_id = 0xFF00 + id;
+ }
+ }
+ goto choose_ok;
+ }
+ break;
+ case BR_SSLKEYX_ECDHE_ECDSA:
+ if (pc->sk->key_type == BR_KEYTYPE_EC) {
+ choices->cipher_suite = st[u][0];
+ if (br_ssl_engine_get_version(&cc->eng)
+ < BR_TLS12)
+ {
+ if (pc->cbhash) {
+ choices->algo_id = 0x0203;
+ } else {
+ choices->algo_id =
+ 0xFF00 + br_sha1_ID;
+ }
+ } else {
+ unsigned id;
+
+ id = choose_hash(chashes >> 8);
+ if (pc->cbhash) {
+ choices->algo_id =
+ (id << 8) + 0x03;
+ } else {
+ choices->algo_id =
+ 0xFF00 + id;
+ }
+ }
+ goto choose_ok;
+ }
+ break;
+ case BR_SSLKEYX_ECDH_RSA:
+ if (pc->sk->key_type == BR_KEYTYPE_EC
+ && pc->cert_signer_algo == BR_KEYTYPE_RSA)
+ {
+ choices->cipher_suite = st[u][0];
+ goto choose_ok;
+ }
+ break;
+ case BR_SSLKEYX_ECDH_ECDSA:
+ if (pc->sk->key_type == BR_KEYTYPE_EC
+ && pc->cert_signer_algo == BR_KEYTYPE_EC)
+ {
+ choices->cipher_suite = st[u][0];
+ goto choose_ok;
+ }
+ break;
+ }
+ }
+ return 0;
+
+choose_ok:
+ choices->chain = pc->chain;
+ choices->chain_len = pc->chain_len;
+ if (pc->verbose) {
+ char csn[80];
+
+ get_suite_name_ext(choices->cipher_suite, csn, sizeof csn);
+ fprintf(stderr, "Using: %s\n", csn);
+ }
+ return 1;
+}
+
+static uint32_t
+sp_do_keyx(const br_ssl_server_policy_class **pctx,
+ unsigned char *data, size_t *len)
+{
+ policy_context *pc;
+ uint32_t r;
+ size_t xoff, xlen;
+
+ pc = (policy_context *)pctx;
+ switch (pc->sk->key_type) {
+ const br_ec_impl *iec;
+
+ case BR_KEYTYPE_RSA:
+ return br_rsa_ssl_decrypt(
+ br_rsa_private_get_default(),
+ &pc->sk->key.rsa, data, *len);
+ case BR_KEYTYPE_EC:
+ iec = br_ec_get_default();
+ r = iec->mul(data, *len, pc->sk->key.ec.x,
+ pc->sk->key.ec.xlen, pc->sk->key.ec.curve);
+ xoff = iec->xoff(pc->sk->key.ec.curve, &xlen);
+ memmove(data, data + xoff, xlen);
+ *len = xlen;
+ return r;
+ default:
+ fprintf(stderr, "ERROR: unknown private key type (%d)\n",
+ (int)pc->sk->key_type);
+ return 0;
+ }
+}
+
+static size_t
+sp_do_sign(const br_ssl_server_policy_class **pctx,
+ unsigned algo_id, unsigned char *data, size_t hv_len, size_t len)
+{
+ policy_context *pc;
+ unsigned char hv[64];
+
+ pc = (policy_context *)pctx;
+ if (algo_id >= 0xFF00) {
+ algo_id &= 0xFF;
+ memcpy(hv, data, hv_len);
+ } else {
+ const br_hash_class *hc;
+ br_hash_compat_context zc;
+
+ if (pc->verbose) {
+ fprintf(stderr, "Callback hashing, algo = 0x%04X,"
+ " data_len = %lu\n",
+ algo_id, (unsigned long)hv_len);
+ }
+ algo_id >>= 8;
+ hc = get_hash_impl(algo_id);
+ if (hc == NULL) {
+ if (pc->verbose) {
+ fprintf(stderr,
+ "ERROR: unsupported hash function %u\n",
+ algo_id);
+ }
+ return 0;
+ }
+ hc->init(&zc.vtable);
+ hc->update(&zc.vtable, data, hv_len);
+ hc->out(&zc.vtable, hv);
+ hv_len = (hc->desc >> BR_HASHDESC_OUT_OFF)
+ & BR_HASHDESC_OUT_MASK;
+ }
+ switch (pc->sk->key_type) {
+ size_t sig_len;
+ uint32_t x;
+ const unsigned char *hash_oid;
+ const br_hash_class *hc;
+
+ case BR_KEYTYPE_RSA:
+ hash_oid = get_hash_oid(algo_id);
+ if (hash_oid == NULL && algo_id != 0) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: cannot RSA-sign with"
+ " unknown hash function: %u\n",
+ algo_id);
+ }
+ return 0;
+ }
+ sig_len = (pc->sk->key.rsa.n_bitlen + 7) >> 3;
+ if (len < sig_len) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: cannot RSA-sign,"
+ " buffer is too small"
+ " (sig=%lu, buf=%lu)\n",
+ (unsigned long)sig_len,
+ (unsigned long)len);
+ }
+ return 0;
+ }
+ x = br_rsa_pkcs1_sign_get_default()(
+ hash_oid, hv, hv_len, &pc->sk->key.rsa, data);
+ if (!x) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: RSA-sign failure\n");
+ }
+ return 0;
+ }
+ return sig_len;
+
+ case BR_KEYTYPE_EC:
+ hc = get_hash_impl(algo_id);
+ if (hc == NULL) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: cannot ECDSA-sign with"
+ " unknown hash function: %u\n",
+ algo_id);
+ }
+ return 0;
+ }
+ if (len < 139) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: cannot ECDSA-sign"
+ " (output buffer = %lu)\n",
+ (unsigned long)len);
+ }
+ return 0;
+ }
+ sig_len = br_ecdsa_sign_asn1_get_default()(
+ br_ec_get_default(), hc, hv, &pc->sk->key.ec, data);
+ if (sig_len == 0) {
+ if (pc->verbose) {
+ fprintf(stderr, "ERROR: ECDSA-sign failure\n");
+ }
+ return 0;
+ }
+ return sig_len;
+
+ default:
+ return 0;
+ }
+}
+
+static const br_ssl_server_policy_class policy_vtable = {
+ sizeof(policy_context),
+ sp_choose,
+ sp_do_keyx,
+ sp_do_sign
+};
+
+void
+free_alpn(void *alpn)
+{
+ xfree(*(char **)alpn);
+}
+
+/* see brssl.h */
+int
+do_server(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int trace;
+ int i, bidi;
+ const char *bind_name;
+ const char *port;
+ unsigned vmin, vmax;
+ cipher_suite *suites;
+ size_t num_suites;
+ uint16_t *suite_ids;
+ unsigned hfuns;
+ int cbhash;
+ br_x509_certificate *chain;
+ size_t chain_len;
+ int cert_signer_algo;
+ private_key *sk;
+ anchor_list anchors = VEC_INIT;
+ VECTOR(char *) alpn_names = VEC_INIT;
+ br_x509_minimal_context xc;
+ const br_hash_class *dnhash;
+ size_t u;
+ br_ssl_server_context cc;
+ policy_context pc;
+ br_ssl_session_cache_lru lru;
+ unsigned char *iobuf, *cache;
+ size_t iobuf_len, cache_len;
+ uint32_t flags;
+ SOCKET server_fd, fd;
+
+ retcode = 0;
+ verbose = 1;
+ trace = 0;
+ bind_name = NULL;
+ port = NULL;
+ bidi = 1;
+ vmin = 0;
+ vmax = 0;
+ suites = NULL;
+ num_suites = 0;
+ hfuns = 0;
+ cbhash = 0;
+ suite_ids = NULL;
+ chain = NULL;
+ chain_len = 0;
+ sk = NULL;
+ iobuf = NULL;
+ iobuf_len = 0;
+ cache = NULL;
+ cache_len = (size_t)-1;
+ flags = 0;
+ server_fd = INVALID_SOCKET;
+ fd = INVALID_SOCKET;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ usage_server();
+ goto server_exit_error;
+ }
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else if (eqstr(arg, "-trace")) {
+ trace = 1;
+ } else if (eqstr(arg, "-b")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-b'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ if (bind_name != NULL) {
+ fprintf(stderr, "ERROR: duplicate bind host\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ bind_name = argv[i];
+ } else if (eqstr(arg, "-p")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-p'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ if (port != NULL) {
+ fprintf(stderr, "ERROR: duplicate bind port\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ port = argv[i];
+ } else if (eqstr(arg, "-mono")) {
+ bidi = 0;
+ } else if (eqstr(arg, "-buf")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-buf'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (iobuf_len != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate I/O buffer length\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ iobuf_len = parse_size(arg);
+ if (iobuf_len == (size_t)-1) {
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-cache")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cache'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (cache_len != (size_t)-1) {
+ fprintf(stderr, "ERROR: duplicate session"
+ " cache length\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ cache_len = parse_size(arg);
+ if (cache_len == (size_t)-1) {
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-cert")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cert'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ if (chain != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate certificate chain\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ chain = read_certificates(arg, &chain_len);
+ if (chain == NULL || chain_len == 0) {
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-key")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-key'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ if (sk != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate private key\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ sk = read_private_key(arg);
+ if (sk == NULL) {
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-CA")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-CA'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (read_trust_anchors(&anchors, arg) == 0) {
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-anon_ok")) {
+ flags |= BR_OPT_TOLERATE_NO_CLIENT_AUTH;
+ } else if (eqstr(arg, "-list")) {
+ list_names();
+ goto server_exit;
+ } else if (eqstr(arg, "-vmin")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmin'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (vmin != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate minimum version\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ vmin = parse_version(arg, strlen(arg));
+ if (vmin == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-vmax")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmax'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (vmax != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate maximum version\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ vmax = parse_version(arg, strlen(arg));
+ if (vmax == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-cs")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cs'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ if (suites != NULL) {
+ fprintf(stderr, "ERROR: duplicate list"
+ " of cipher suites\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ suites = parse_suites(arg, &num_suites);
+ if (suites == NULL) {
+ usage_server();
+ goto server_exit_error;
+ }
+ } else if (eqstr(arg, "-hf")) {
+ unsigned x;
+
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-hf'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ arg = argv[i];
+ x = parse_hash_functions(arg);
+ if (x == 0) {
+ usage_server();
+ goto server_exit_error;
+ }
+ hfuns |= x;
+ } else if (eqstr(arg, "-cbhash")) {
+ cbhash = 1;
+ } else if (eqstr(arg, "-serverpref")) {
+ flags |= BR_OPT_ENFORCE_SERVER_PREFERENCES;
+ } else if (eqstr(arg, "-noreneg")) {
+ flags |= BR_OPT_NO_RENEGOTIATION;
+ } else if (eqstr(arg, "-alpn")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-alpn'\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ VEC_ADD(alpn_names, xstrdup(argv[i]));
+ } else if (eqstr(arg, "-strictalpn")) {
+ flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_server();
+ goto server_exit_error;
+ }
+ }
+ if (port == NULL) {
+ port = "4433";
+ }
+ if (vmin == 0) {
+ vmin = BR_TLS10;
+ }
+ if (vmax == 0) {
+ vmax = BR_TLS12;
+ }
+ if (vmax < vmin) {
+ fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
+ " version combination\n");
+ usage_server();
+ goto server_exit_error;
+ }
+ if (suites == NULL) {
+ num_suites = 0;
+
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ num_suites ++;
+ }
+ }
+ suites = xmalloc(num_suites * sizeof *suites);
+ num_suites = 0;
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ suites[num_suites ++] = cipher_suites[u];
+ }
+ }
+ }
+ if (hfuns == 0) {
+ hfuns = (unsigned)-1;
+ }
+ if (chain == NULL || chain_len == 0) {
+ fprintf(stderr, "ERROR: no certificate chain provided\n");
+ goto server_exit_error;
+ }
+ if (sk == NULL) {
+ fprintf(stderr, "ERROR: no private key provided\n");
+ goto server_exit_error;
+ }
+ switch (sk->key_type) {
+ int curve;
+ uint32_t supp;
+
+ case BR_KEYTYPE_RSA:
+ break;
+ case BR_KEYTYPE_EC:
+ curve = sk->key.ec.curve;
+ supp = br_ec_get_default()->supported_curves;
+ if (curve > 31 || !((supp >> curve) & 1)) {
+ fprintf(stderr, "ERROR: private key curve (%d)"
+ " is not supported\n", curve);
+ goto server_exit_error;
+ }
+ break;
+ default:
+ fprintf(stderr, "ERROR: unsupported private key type (%d)\n",
+ sk->key_type);
+ break;
+ }
+ cert_signer_algo = get_cert_signer_algo(chain);
+ if (cert_signer_algo == 0) {
+ goto server_exit_error;
+ }
+ if (verbose) {
+ const char *csas;
+
+ switch (cert_signer_algo) {
+ case BR_KEYTYPE_RSA: csas = "RSA"; break;
+ case BR_KEYTYPE_EC: csas = "EC"; break;
+ default:
+ csas = "unknown";
+ break;
+ }
+ fprintf(stderr, "Issuing CA key type: %d (%s)\n",
+ cert_signer_algo, csas);
+ }
+ if (iobuf_len == 0) {
+ if (bidi) {
+ iobuf_len = BR_SSL_BUFSIZE_BIDI;
+ } else {
+ iobuf_len = BR_SSL_BUFSIZE_MONO;
+ }
+ }
+ iobuf = xmalloc(iobuf_len);
+ if (cache_len == (size_t)-1) {
+ cache_len = 5000;
+ }
+ cache = xmalloc(cache_len);
+
+ /*
+ * Compute implementation requirements and inject implementations.
+ */
+ suite_ids = xmalloc(num_suites * sizeof *suite_ids);
+ br_ssl_server_zero(&cc);
+ br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
+ br_ssl_engine_set_all_flags(&cc.eng, flags);
+ if (vmin <= BR_TLS11) {
+ if (!(hfuns & (1 << br_md5_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
+ goto server_exit_error;
+ }
+ if (!(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
+ goto server_exit_error;
+ }
+ }
+ for (u = 0; u < num_suites; u ++) {
+ unsigned req;
+
+ req = suites[u].req;
+ suite_ids[u] = suites[u].suite;
+ if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires TLS 1.2\n",
+ suites[u].name);
+ goto server_exit_error;
+ }
+ if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-1\n",
+ suites[u].name);
+ goto server_exit_error;
+ }
+ if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-256\n",
+ suites[u].name);
+ goto server_exit_error;
+ }
+ if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-384\n",
+ suites[u].name);
+ goto server_exit_error;
+ }
+ /* TODO: algorithm implementation selection */
+ if ((req & REQ_AESCBC) != 0) {
+ br_ssl_engine_set_default_aes_cbc(&cc.eng);
+ }
+ if ((req & REQ_AESCCM) != 0) {
+ br_ssl_engine_set_default_aes_ccm(&cc.eng);
+ }
+ if ((req & REQ_AESGCM) != 0) {
+ br_ssl_engine_set_default_aes_gcm(&cc.eng);
+ }
+ if ((req & REQ_CHAPOL) != 0) {
+ br_ssl_engine_set_default_chapol(&cc.eng);
+ }
+ if ((req & REQ_3DESCBC) != 0) {
+ br_ssl_engine_set_default_des_cbc(&cc.eng);
+ }
+ if ((req & (REQ_ECDHE_RSA | REQ_ECDHE_ECDSA)) != 0) {
+ br_ssl_engine_set_default_ec(&cc.eng);
+ }
+ }
+ br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
+
+ dnhash = NULL;
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ dnhash = hc;
+ br_ssl_engine_set_hash(&cc.eng, id, hc);
+ }
+ }
+ if (vmin <= BR_TLS11) {
+ br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
+ }
+ if (vmax >= BR_TLS12) {
+ if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
+ br_ssl_engine_set_prf_sha256(&cc.eng,
+ &br_tls12_sha256_prf);
+ }
+ if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
+ br_ssl_engine_set_prf_sha384(&cc.eng,
+ &br_tls12_sha384_prf);
+ }
+ }
+
+ br_ssl_session_cache_lru_init(&lru, cache, cache_len);
+ br_ssl_server_set_cache(&cc, &lru.vtable);
+
+ if (VEC_LEN(alpn_names) != 0) {
+ br_ssl_engine_set_protocol_names(&cc.eng,
+ (const char **)&VEC_ELT(alpn_names, 0),
+ VEC_LEN(alpn_names));
+ }
+
+ /*
+ * Set the policy handler (that chooses the actual cipher suite,
+ * selects the certificate chain, and runs the private key
+ * operations).
+ */
+ pc.vtable = &policy_vtable;
+ pc.verbose = verbose;
+ pc.chain = chain;
+ pc.chain_len = chain_len;
+ pc.cert_signer_algo = cert_signer_algo;
+ pc.sk = sk;
+ pc.cbhash = cbhash;
+ br_ssl_server_set_policy(&cc, &pc.vtable);
+
+ /*
+ * If trust anchors have been configured, then set an X.509
+ * validation engine and activate client certificate
+ * authentication.
+ */
+ if (VEC_LEN(anchors) != 0) {
+ br_x509_minimal_init(&xc, dnhash,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF)
+ & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ br_x509_minimal_set_hash(&xc, id, hc);
+ }
+ }
+ br_ssl_engine_set_default_rsavrfy(&cc.eng);
+ br_ssl_engine_set_default_ecdsa(&cc.eng);
+ br_x509_minimal_set_rsa(&xc, br_rsa_pkcs1_vrfy_get_default());
+ br_x509_minimal_set_ecdsa(&xc,
+ br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
+ br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
+ br_ssl_server_set_trust_anchor_names_alt(&cc,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ }
+
+ br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
+
+ /*
+ * On Unix systems, we need to ignore SIGPIPE.
+ */
+#ifndef _WIN32
+ signal(SIGPIPE, SIG_IGN);
+#endif
+
+ /*
+ * Open the server socket.
+ */
+ server_fd = host_bind(bind_name, port, verbose);
+ if (server_fd == INVALID_SOCKET) {
+ goto server_exit_error;
+ }
+
+ /*
+ * Process incoming clients, one at a time. Note that we do not
+ * accept any client until the previous connection has finished:
+ * this is voluntary, since the tool uses stdin/stdout for
+ * application data, and thus cannot really run two connections
+ * simultaneously.
+ */
+ for (;;) {
+ int x;
+ unsigned run_flags;
+
+ fd = accept_client(server_fd, verbose, 1);
+ if (fd == INVALID_SOCKET) {
+ goto server_exit_error;
+ }
+ br_ssl_server_reset(&cc);
+ run_flags = (verbose ? RUN_ENGINE_VERBOSE : 0)
+ | (trace ? RUN_ENGINE_TRACE : 0);
+ x = run_ssl_engine(&cc.eng, fd, run_flags);
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ fd = INVALID_SOCKET;
+ if (x < -1) {
+ goto server_exit_error;
+ }
+ }
+
+ /*
+ * Release allocated structures.
+ */
+server_exit:
+ xfree(suites);
+ xfree(suite_ids);
+ free_certificates(chain, chain_len);
+ free_private_key(sk);
+ VEC_CLEAREXT(anchors, &free_ta_contents);
+ VEC_CLEAREXT(alpn_names, &free_alpn);
+ xfree(iobuf);
+ xfree(cache);
+ if (fd != INVALID_SOCKET) {
+#ifdef _WIN32
+ closesocket(fd);
+#else
+ close(fd);
+#endif
+ }
+ if (server_fd != INVALID_SOCKET) {
+#ifdef _WIN32
+ closesocket(server_fd);
+#else
+ close(server_fd);
+#endif
+ }
+ return retcode;
+
+server_exit_error:
+ retcode = -1;
+ goto server_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/skey.c b/test/monniaux/BearSSL/tools/skey.c
new file mode 100644
index 00000000..90ecf636
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/skey.c
@@ -0,0 +1,784 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+typedef struct {
+ int print_text;
+ int print_C;
+ const char *rawder;
+ const char *rawpem;
+ const char *pk8der;
+ const char *pk8pem;
+} outspec;
+
+static void
+print_int_text(const char *name, const unsigned char *buf, size_t len)
+{
+ size_t u;
+
+ printf("%s = ", name);
+ for (u = 0; u < len; u ++) {
+ printf("%02X", buf[u]);
+ }
+ printf("\n");
+}
+
+static void
+print_int_C(const char *name, const unsigned char *buf, size_t len)
+{
+ size_t u;
+
+ printf("\nstatic const unsigned char %s[] = {", name);
+ for (u = 0; u < len; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", buf[u]);
+ }
+ printf("\n};\n");
+}
+
+static int
+write_to_file(const char *name, const void *data, size_t len)
+{
+ FILE *f;
+
+ f = fopen(name, "wb");
+ if (f == NULL) {
+ fprintf(stderr,
+ "ERROR: cannot open file '%s' for writing\n",
+ name);
+ return 0;
+ }
+ if (fwrite(data, 1, len, f) != len) {
+ fclose(f);
+ fprintf(stderr,
+ "ERROR: cannot write to file '%s'\n",
+ name);
+ return 0;
+ }
+ fclose(f);
+ return 1;
+}
+
+static int
+write_to_pem_file(const char *name,
+ const void *data, size_t len, const char *banner)
+{
+ void *pem;
+ size_t pemlen;
+ int r;
+
+ pemlen = br_pem_encode(NULL, NULL, len, banner, 0);
+ pem = xmalloc(pemlen + 1);
+ br_pem_encode(pem, data, len, banner, 0);
+ r = write_to_file(name, pem, pemlen);
+ xfree(pem);
+ return r;
+}
+
+static int
+print_rsa(const br_rsa_private_key *sk, outspec *os)
+{
+ int ret;
+ unsigned char *n, *d, *buf;
+ uint32_t e;
+ size_t nlen, dlen, len;
+ br_rsa_compute_modulus cm;
+ br_rsa_compute_pubexp ce;
+ br_rsa_compute_privexp cd;
+ br_rsa_public_key pk;
+ unsigned char ebuf[4];
+
+ n = NULL;
+ d = NULL;
+ buf = NULL;
+ ret = 1;
+ if (os->print_text) {
+ print_int_text("p ", sk->p, sk->plen);
+ print_int_text("q ", sk->q, sk->qlen);
+ print_int_text("dp", sk->dp, sk->dplen);
+ print_int_text("dq", sk->dq, sk->dqlen);
+ print_int_text("iq", sk->iq, sk->iqlen);
+ }
+ if (os->print_C) {
+ print_int_C("RSA_P", sk->p, sk->plen);
+ print_int_C("RSA_Q", sk->q, sk->qlen);
+ print_int_C("RSA_DP", sk->dp, sk->dplen);
+ print_int_C("RSA_DQ", sk->dq, sk->dqlen);
+ print_int_C("RSA_IQ", sk->iq, sk->iqlen);
+ printf("\nstatic const br_rsa_private_key RSA = {\n");
+ printf("\t%lu,\n", (unsigned long)sk->n_bitlen);
+ printf("\t(unsigned char *)RSA_P, sizeof RSA_P,\n");
+ printf("\t(unsigned char *)RSA_Q, sizeof RSA_Q,\n");
+ printf("\t(unsigned char *)RSA_DP, sizeof RSA_DP,\n");
+ printf("\t(unsigned char *)RSA_DQ, sizeof RSA_DQ,\n");
+ printf("\t(unsigned char *)RSA_IQ, sizeof RSA_IQ\n");
+ printf("};\n");
+ }
+
+ if (os->rawder == NULL && os->rawpem == NULL
+ && os->pk8der == NULL && os->pk8pem == NULL)
+ {
+ return ret;
+ }
+
+ cm = br_rsa_compute_modulus_get_default();
+ ce = br_rsa_compute_pubexp_get_default();
+ cd = br_rsa_compute_privexp_get_default();
+ nlen = cm(NULL, sk);
+ if (nlen == 0) {
+ goto print_RSA_error;
+ }
+ n = xmalloc(nlen);
+ if (cm(n, sk) != nlen) {
+ goto print_RSA_error;
+ }
+ e = ce(sk);
+ if (e == 0) {
+ goto print_RSA_error;
+ }
+ dlen = cd(NULL, sk, e);
+ if (dlen == 0) {
+ goto print_RSA_error;
+ }
+ d = xmalloc(dlen);
+ if (cd(d, sk, e) != dlen) {
+ goto print_RSA_error;
+ }
+ ebuf[0] = e >> 24;
+ ebuf[1] = e >> 16;
+ ebuf[2] = e >> 8;
+ ebuf[3] = e;
+ pk.n = n;
+ pk.nlen = nlen;
+ pk.e = ebuf;
+ pk.elen = sizeof ebuf;
+
+ if (os->rawder != NULL || os->rawpem != NULL) {
+ len = br_encode_rsa_raw_der(NULL, sk, &pk, d, dlen);
+ if (len == 0) {
+ goto print_RSA_error;
+ }
+ buf = xmalloc(len);
+ if (br_encode_rsa_raw_der(buf, sk, &pk, d, dlen) != len) {
+ goto print_RSA_error;
+ }
+ if (os->rawder != NULL) {
+ ret &= write_to_file(os->rawder, buf, len);
+ }
+ if (os->rawpem != NULL) {
+ ret &= write_to_pem_file(os->rawpem,
+ buf, len, "RSA PRIVATE KEY");
+ }
+ xfree(buf);
+ buf = NULL;
+ }
+
+ if (os->pk8der != NULL || os->pk8pem != NULL) {
+ len = br_encode_rsa_pkcs8_der(NULL, sk, &pk, d, dlen);
+ if (len == 0) {
+ goto print_RSA_error;
+ }
+ buf = xmalloc(len);
+ if (br_encode_rsa_pkcs8_der(buf, sk, &pk, d, dlen) != len) {
+ goto print_RSA_error;
+ }
+ if (os->pk8der != NULL) {
+ ret &= write_to_file(os->pk8der, buf, len);
+ }
+ if (os->pk8pem != NULL) {
+ ret &= write_to_pem_file(os->pk8pem,
+ buf, len, "PRIVATE KEY");
+ }
+ xfree(buf);
+ buf = NULL;
+ }
+
+print_RSA_exit:
+ xfree(n);
+ xfree(d);
+ xfree(buf);
+ return ret;
+
+print_RSA_error:
+ fprintf(stderr, "ERROR: cannot encode RSA key\n");
+ ret = 0;
+ goto print_RSA_exit;
+}
+
+static int
+print_ec(const br_ec_private_key *sk, outspec *os)
+{
+ br_ec_public_key pk;
+ unsigned kbuf[BR_EC_KBUF_PUB_MAX_SIZE];
+ unsigned char *buf;
+ size_t len;
+ int r;
+
+ if (os->print_text) {
+ print_int_text("x", sk->x, sk->xlen);
+ }
+ if (os->print_C) {
+ print_int_C("EC_X", sk->x, sk->xlen);
+ printf("\nstatic const br_ec_private_key EC = {\n");
+ printf("\t%d,\n", sk->curve);
+ printf("\t(unsigned char *)EC_X, sizeof EC_X\n");
+ printf("};\n");
+ }
+
+ if (os->rawder == NULL && os->rawpem == NULL
+ && os->pk8der == NULL && os->pk8pem == NULL)
+ {
+ return 1;
+ }
+ if (br_ec_compute_pub(br_ec_get_default(), &pk, kbuf, sk) == 0) {
+ fprintf(stderr,
+ "ERROR: cannot re-encode (unsupported curve)\n");
+ return 0;
+ }
+
+ r = 1;
+ if (os->rawder != NULL || os->rawpem != NULL) {
+ len = br_encode_ec_raw_der(NULL, sk, &pk);
+ if (len == 0) {
+ fprintf(stderr, "ERROR: cannot re-encode"
+ " (unsupported curve)\n");
+ return 0;
+ }
+ buf = xmalloc(len);
+ if (br_encode_ec_raw_der(buf, sk, &pk) != len) {
+ fprintf(stderr, "ERROR: re-encode failure\n");
+ xfree(buf);
+ return 0;
+ }
+ if (os->rawder != NULL) {
+ r &= write_to_file(os->rawder, buf, len);
+ }
+ if (os->rawpem != NULL) {
+ r &= write_to_pem_file(os->rawpem,
+ buf, len, "EC PRIVATE KEY");
+ }
+ xfree(buf);
+ }
+ if (os->pk8der != NULL || os->pk8pem != NULL) {
+ len = br_encode_ec_pkcs8_der(NULL, sk, &pk);
+ if (len == 0) {
+ fprintf(stderr, "ERROR: cannot re-encode"
+ " (unsupported curve)\n");
+ return 0;
+ }
+ buf = xmalloc(len);
+ if (br_encode_ec_pkcs8_der(buf, sk, &pk) != len) {
+ fprintf(stderr, "ERROR: re-encode failure\n");
+ xfree(buf);
+ return 0;
+ }
+ if (os->pk8der != NULL) {
+ r &= write_to_file(os->pk8der, buf, len);
+ }
+ if (os->pk8pem != NULL) {
+ r &= write_to_pem_file(os->pk8pem,
+ buf, len, "PRIVATE KEY");
+ }
+ xfree(buf);
+ }
+ return r;
+}
+
+static int
+parse_rsa_spec(const char *kgen_spec, unsigned *size, uint32_t *pubexp)
+{
+ const char *p;
+ char *end;
+ unsigned long ul;
+
+ p = kgen_spec;
+ if (*p != 'r' && *p != 'R') {
+ return 0;
+ }
+ p ++;
+ if (*p != 's' && *p != 'S') {
+ return 0;
+ }
+ p ++;
+ if (*p != 'a' && *p != 'A') {
+ return 0;
+ }
+ p ++;
+ if (*p == 0) {
+ *size = 2048;
+ *pubexp = 3;
+ return 1;
+ } else if (*p != ':') {
+ return 0;
+ }
+ p ++;
+ ul = strtoul(p, &end, 10);
+ if (ul < 512 || ul > 32768) {
+ return 0;
+ }
+ *size = ul;
+ p = end;
+ if (*p == 0) {
+ *pubexp = 3;
+ return 1;
+ } else if (*p != ':') {
+ return 0;
+ }
+ p ++;
+ ul = strtoul(p, &end, 10);
+ if ((ul & 1) == 0 || ul == 1 || ((ul >> 30) >> 2) != 0) {
+ return 0;
+ }
+ *pubexp = ul;
+ if (*end != 0) {
+ return 0;
+ }
+ return 1;
+}
+
+static int
+keygen_rsa(unsigned size, uint32_t pubexp, outspec *os)
+{
+ br_hmac_drbg_context rng;
+ br_prng_seeder seeder;
+ br_rsa_keygen kg;
+ br_rsa_private_key sk;
+ unsigned char *kbuf_priv;
+ uint32_t r;
+
+ seeder = br_prng_seeder_system(NULL);
+ if (seeder == 0) {
+ fprintf(stderr, "ERROR: no system source of randomness\n");
+ return 0;
+ }
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, NULL, 0);
+ if (!seeder(&rng.vtable)) {
+ fprintf(stderr, "ERROR: system source of randomness failed\n");
+ return 0;
+ }
+ kbuf_priv = xmalloc(BR_RSA_KBUF_PRIV_SIZE(size));
+ kg = br_rsa_keygen_get_default();
+ r = kg(&rng.vtable, &sk, kbuf_priv, NULL, NULL, size, pubexp);
+ if (!r) {
+ fprintf(stderr, "ERROR: RSA key pair generation failed\n");
+ } else {
+ r = print_rsa(&sk, os);
+ }
+ xfree(kbuf_priv);
+ return r;
+}
+
+static int
+parse_ec_spec(const char *kgen_spec, int *curve)
+{
+ const char *p;
+
+ *curve = 0;
+ p = kgen_spec;
+ if (*p != 'e' && *p != 'E') {
+ return 0;
+ }
+ p ++;
+ if (*p != 'c' && *p != 'C') {
+ return 0;
+ }
+ p ++;
+ if (*p == 0) {
+ *curve = BR_EC_secp256r1;
+ return 1;
+ }
+ if (*p != ':') {
+ return 0;
+ }
+ *curve = get_curve_by_name(p);
+ return *curve > 0;
+}
+
+static int
+keygen_ec(int curve, outspec *os)
+{
+ br_hmac_drbg_context rng;
+ br_prng_seeder seeder;
+ const br_ec_impl *impl;
+ br_ec_private_key sk;
+ unsigned char kbuf_priv[BR_EC_KBUF_PRIV_MAX_SIZE];
+ size_t len;
+
+ seeder = br_prng_seeder_system(NULL);
+ if (seeder == 0) {
+ fprintf(stderr, "ERROR: no system source of randomness\n");
+ return 0;
+ }
+ br_hmac_drbg_init(&rng, &br_sha256_vtable, NULL, 0);
+ if (!seeder(&rng.vtable)) {
+ fprintf(stderr, "ERROR: system source of randomness failed\n");
+ return 0;
+ }
+ impl = br_ec_get_default();
+ len = br_ec_keygen(&rng.vtable, impl, &sk, kbuf_priv, curve);
+ if (len == 0) {
+ fprintf(stderr, "ERROR: curve is not supported\n");
+ return 0;
+ }
+ return print_ec(&sk, os);
+}
+
+static int
+decode_key(const unsigned char *buf, size_t len, outspec *os)
+{
+ br_skey_decoder_context dc;
+ int err, ret;
+
+ br_skey_decoder_init(&dc);
+ br_skey_decoder_push(&dc, buf, len);
+ err = br_skey_decoder_last_error(&dc);
+ if (err != 0) {
+ const char *errname, *errmsg;
+
+ fprintf(stderr, "ERROR (decoding): err=%d\n", err);
+ errname = find_error_name(err, &errmsg);
+ if (errname != NULL) {
+ fprintf(stderr, " %s: %s\n", errname, errmsg);
+ } else {
+ fprintf(stderr, " (unknown)\n");
+ }
+ return 0;
+ }
+ ret = 1;
+ switch (br_skey_decoder_key_type(&dc)) {
+ const br_rsa_private_key *rk;
+ const br_ec_private_key *ek;
+
+ case BR_KEYTYPE_RSA:
+ rk = br_skey_decoder_get_rsa(&dc);
+ printf("RSA key (%lu bits)\n", (unsigned long)rk->n_bitlen);
+ ret = print_rsa(rk, os);
+ break;
+
+ case BR_KEYTYPE_EC:
+ ek = br_skey_decoder_get_ec(&dc);
+ printf("EC key (curve = %d: %s)\n",
+ ek->curve, ec_curve_name(ek->curve));
+ ret = print_ec(ek, os);
+ break;
+
+ default:
+ fprintf(stderr, "Unknown key type: %d\n",
+ br_skey_decoder_key_type(&dc));
+ ret = 0;
+ break;
+ }
+
+ return ret;
+}
+
+static void
+usage_skey(void)
+{
+ fprintf(stderr,
+"usage: brssl skey [ options ] file...\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+ fprintf(stderr,
+" -text print private key details (human-readable)\n");
+ fprintf(stderr,
+" -C print private key details (C code)\n");
+ fprintf(stderr,
+" -rawder file save private key in 'file' (raw format, DER)\n");
+ fprintf(stderr,
+" -rawpem file save private key in 'file' (raw format, PEM)\n");
+ fprintf(stderr,
+" -pk8der file save private key in 'file' (PKCS#8 format, DER)\n");
+ fprintf(stderr,
+" -pk8pem file save private key in 'file' (PKCS#8 format, PEM)\n");
+ fprintf(stderr,
+" -gen spec generate a new key using the provided key specification\n");
+ fprintf(stderr,
+" -list list known elliptic curve names\n");
+ fprintf(stderr,
+"Key specification begins with a key type, followed by optional parameters\n");
+ fprintf(stderr,
+"that depend on the key type, separated by colon characters:\n");
+ fprintf(stderr,
+" rsa[:size[:pubexep]] RSA key (defaults: size = 2048, pubexp = 3)\n");
+ fprintf(stderr,
+" ec[:curvename] EC key (default curve: secp256r1)\n");
+}
+
+/* see brssl.h */
+int
+do_skey(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int i, num_files;
+ outspec os;
+ unsigned char *buf;
+ size_t len;
+ pem_object *pos;
+ const char *kgen_spec;
+
+ retcode = 0;
+ verbose = 1;
+ os.print_text = 0;
+ os.print_C = 0;
+ os.rawder = NULL;
+ os.rawpem = NULL;
+ os.pk8der = NULL;
+ os.pk8pem = NULL;
+ num_files = 0;
+ buf = NULL;
+ pos = NULL;
+ kgen_spec = NULL;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ num_files ++;
+ continue;
+ }
+ argv[i] = NULL;
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else if (eqstr(arg, "-text")) {
+ os.print_text = 1;
+ } else if (eqstr(arg, "-C")) {
+ os.print_C = 1;
+ } else if (eqstr(arg, "-rawder")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-rawder'\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ if (os.rawder != NULL) {
+ fprintf(stderr,
+ "ERROR: multiple '-rawder' options\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ os.rawder = argv[i];
+ argv[i] = NULL;
+ } else if (eqstr(arg, "-rawpem")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-rawpem'\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ if (os.rawpem != NULL) {
+ fprintf(stderr,
+ "ERROR: multiple '-rawpem' options\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ os.rawpem = argv[i];
+ argv[i] = NULL;
+ } else if (eqstr(arg, "-pk8der")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-pk8der'\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ if (os.pk8der != NULL) {
+ fprintf(stderr,
+ "ERROR: multiple '-pk8der' options\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ os.pk8der = argv[i];
+ argv[i] = NULL;
+ } else if (eqstr(arg, "-pk8pem")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-pk8pem'\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ if (os.pk8pem != NULL) {
+ fprintf(stderr,
+ "ERROR: multiple '-pk8pem' options\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ os.pk8pem = argv[i];
+ argv[i] = NULL;
+ } else if (eqstr(arg, "-gen")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-gen'\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ if (kgen_spec != NULL) {
+ fprintf(stderr,
+ "ERROR: multiple '-gen' options\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+ kgen_spec = argv[i];
+ argv[i] = NULL;
+ } else if (eqstr(arg, "-list")) {
+ list_curves();
+ goto skey_exit;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_skey();
+ goto skey_exit_error;
+ }
+ }
+ if (kgen_spec != NULL) {
+ unsigned rsa_size;
+ uint32_t rsa_pubexp;
+ int curve;
+
+ if (num_files != 0) {
+ fprintf(stderr,
+ "ERROR: key files provided while generating\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+
+ if (parse_rsa_spec(kgen_spec, &rsa_size, &rsa_pubexp)) {
+ if (!keygen_rsa(rsa_size, rsa_pubexp, &os)) {
+ goto skey_exit_error;
+ }
+ } else if (parse_ec_spec(kgen_spec, &curve)) {
+ if (!keygen_ec(curve, &os)) {
+ goto skey_exit_error;
+ }
+ } else {
+ fprintf(stderr,
+ "ERROR: unknown key specification: '%s'\n",
+ kgen_spec);
+ usage_skey();
+ goto skey_exit_error;
+ }
+ } else if (num_files == 0) {
+ fprintf(stderr, "ERROR: no private key provided\n");
+ usage_skey();
+ goto skey_exit_error;
+ }
+
+ for (i = 0; i < argc; i ++) {
+ const char *fname;
+
+ fname = argv[i];
+ if (fname == NULL) {
+ continue;
+ }
+ buf = read_file(fname, &len);
+ if (buf == NULL) {
+ goto skey_exit_error;
+ }
+ if (looks_like_DER(buf, len)) {
+ if (verbose) {
+ fprintf(stderr, "File '%s': ASN.1/DER object\n",
+ fname);
+ }
+ if (!decode_key(buf, len, &os)) {
+ goto skey_exit_error;
+ }
+ } else {
+ size_t u, num;
+
+ if (verbose) {
+ fprintf(stderr, "File '%s': decoding as PEM\n",
+ fname);
+ }
+ pos = decode_pem(buf, len, &num);
+ if (pos == NULL) {
+ goto skey_exit_error;
+ }
+ for (u = 0; pos[u].name; u ++) {
+ const char *name;
+
+ name = pos[u].name;
+ if (eqstr(name, "RSA PRIVATE KEY")
+ || eqstr(name, "EC PRIVATE KEY")
+ || eqstr(name, "PRIVATE KEY"))
+ {
+ if (!decode_key(pos[u].data,
+ pos[u].data_len, &os))
+ {
+ goto skey_exit_error;
+ }
+ } else {
+ if (verbose) {
+ fprintf(stderr,
+ "(skipping '%s')\n",
+ name);
+ }
+ }
+ }
+ for (u = 0; pos[u].name; u ++) {
+ free_pem_object_contents(&pos[u]);
+ }
+ xfree(pos);
+ pos = NULL;
+ }
+ xfree(buf);
+ buf = NULL;
+ }
+
+ /*
+ * Release allocated structures.
+ */
+skey_exit:
+ xfree(buf);
+ if (pos != NULL) {
+ size_t u;
+
+ for (u = 0; pos[u].name; u ++) {
+ free_pem_object_contents(&pos[u]);
+ }
+ xfree(pos);
+ }
+ return retcode;
+
+skey_exit_error:
+ retcode = -1;
+ goto skey_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/sslio.c b/test/monniaux/BearSSL/tools/sslio.c
new file mode 100644
index 00000000..ef7dd3f6
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/sslio.c
@@ -0,0 +1,760 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <poll.h>
+
+#define SOCKET int
+#define INVALID_SOCKET (-1)
+#endif
+
+#include "brssl.h"
+
+static void
+dump_blob(const char *name, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t u;
+
+ buf = data;
+ fprintf(stderr, "%s (len = %lu)", name, (unsigned long)len);
+ for (u = 0; u < len; u ++) {
+ if ((u & 15) == 0) {
+ fprintf(stderr, "\n%08lX ", (unsigned long)u);
+ } else if ((u & 7) == 0) {
+ fprintf(stderr, " ");
+ }
+ fprintf(stderr, " %02x", buf[u]);
+ }
+ fprintf(stderr, "\n");
+}
+
+/*
+ * Inspect the provided data in case it is a "command" to trigger a
+ * special behaviour. If the command is recognised, then it is executed
+ * and this function returns 1. Otherwise, this function returns 0.
+ */
+static int
+run_command(br_ssl_engine_context *cc, unsigned char *buf, size_t len)
+{
+ /*
+ * A single static slot for saving session parameters.
+ */
+ static br_ssl_session_parameters slot;
+ static int slot_used = 0;
+
+ size_t u;
+
+ if (len < 2 || len > 3) {
+ return 0;
+ }
+ if (len == 3 && (buf[1] != '\r' || buf[2] != '\n')) {
+ return 0;
+ }
+ if (len == 2 && buf[1] != '\n') {
+ return 0;
+ }
+ switch (buf[0]) {
+ case 'Q':
+ fprintf(stderr, "closing...\n");
+ br_ssl_engine_close(cc);
+ return 1;
+ case 'R':
+ if (br_ssl_engine_renegotiate(cc)) {
+ fprintf(stderr, "renegotiating...\n");
+ } else {
+ fprintf(stderr, "not renegotiating.\n");
+ }
+ return 1;
+ case 'F':
+ /*
+ * Session forget is nominally client-only. But the
+ * session parameters are in the engine structure, which
+ * is the first field of the client context, so the cast
+ * still works properly. On the server, this forgetting
+ * has no effect.
+ */
+ fprintf(stderr, "forgetting session...\n");
+ br_ssl_client_forget_session((br_ssl_client_context *)cc);
+ return 1;
+ case 'S':
+ fprintf(stderr, "saving session parameters...\n");
+ br_ssl_engine_get_session_parameters(cc, &slot);
+ fprintf(stderr, " id = ");
+ for (u = 0; u < slot.session_id_len; u ++) {
+ fprintf(stderr, "%02X", slot.session_id[u]);
+ }
+ fprintf(stderr, "\n");
+ slot_used = 1;
+ return 1;
+ case 'P':
+ if (slot_used) {
+ fprintf(stderr, "restoring session parameters...\n");
+ fprintf(stderr, " id = ");
+ for (u = 0; u < slot.session_id_len; u ++) {
+ fprintf(stderr, "%02X", slot.session_id[u]);
+ }
+ fprintf(stderr, "\n");
+ br_ssl_engine_set_session_parameters(cc, &slot);
+ return 1;
+ }
+ return 0;
+ default:
+ return 0;
+ }
+}
+
+#ifdef _WIN32
+
+typedef struct {
+ unsigned char buf[1024];
+ size_t ptr, len;
+} in_buffer;
+
+static int
+in_return_bytes(in_buffer *bb, unsigned char *buf, size_t len)
+{
+ if (bb->ptr < bb->len) {
+ size_t clen;
+
+ if (buf == NULL) {
+ return 1;
+ }
+ clen = bb->len - bb->ptr;
+ if (clen > len) {
+ clen = len;
+ }
+ memcpy(buf, bb->buf + bb->ptr, clen);
+ bb->ptr += clen;
+ if (bb->ptr == bb->len) {
+ bb->ptr = bb->len = 0;
+ }
+ return (int)clen;
+ }
+ return 0;
+}
+
+/*
+ * A buffered version of in_read(), using a buffer to return only
+ * full lines when feasible.
+ */
+static int
+in_read_buffered(HANDLE h_in, in_buffer *bb, unsigned char *buf, size_t len)
+{
+ int n;
+
+ if (len == 0) {
+ return 0;
+ }
+ n = in_return_bytes(bb, buf, len);
+ if (n != 0) {
+ return n;
+ }
+ for (;;) {
+ INPUT_RECORD inrec;
+ DWORD v;
+
+ if (!PeekConsoleInput(h_in, &inrec, 1, &v)) {
+ fprintf(stderr, "ERROR: PeekConsoleInput()"
+ " failed with 0x%08lX\n",
+ (unsigned long)GetLastError());
+ return -1;
+ }
+ if (v == 0) {
+ return 0;
+ }
+ if (!ReadConsoleInput(h_in, &inrec, 1, &v)) {
+ fprintf(stderr, "ERROR: ReadConsoleInput()"
+ " failed with 0x%08lX\n",
+ (unsigned long)GetLastError());
+ return -1;
+ }
+ if (v == 0) {
+ return 0;
+ }
+ if (inrec.EventType == KEY_EVENT
+ && inrec.Event.KeyEvent.bKeyDown)
+ {
+ int c;
+
+ c = inrec.Event.KeyEvent.uChar.AsciiChar;
+ if (c == '\n' || c == '\r' || c == '\t'
+ || (c >= 32 && c != 127))
+ {
+ if (c == '\r') {
+ c = '\n';
+ }
+ bb->buf[bb->ptr ++] = (unsigned char)c;
+ printf("%c", c);
+ fflush(stdout);
+ bb->len = bb->ptr;
+ if (bb->len == sizeof bb->buf || c == '\n') {
+ bb->ptr = 0;
+ return in_return_bytes(bb, buf, len);
+ }
+ }
+ }
+ }
+}
+
+static int
+in_avail_buffered(HANDLE h_in, in_buffer *bb)
+{
+ return in_read_buffered(h_in, bb, NULL, 1);
+}
+
+#endif
+
+/* see brssl.h */
+int
+run_ssl_engine(br_ssl_engine_context *cc, unsigned long fd, unsigned flags)
+{
+ int hsdetails;
+ int retcode;
+ int verbose;
+ int trace;
+#ifdef _WIN32
+ WSAEVENT fd_event;
+ int can_send, can_recv;
+ HANDLE h_in, h_out;
+ in_buffer bb;
+#endif
+
+ hsdetails = 0;
+ retcode = 0;
+ verbose = (flags & RUN_ENGINE_VERBOSE) != 0;
+ trace = (flags & RUN_ENGINE_TRACE) != 0;
+
+ /*
+ * Print algorithm details.
+ */
+ if (verbose) {
+ const char *rngname;
+
+ fprintf(stderr, "Algorithms:\n");
+ br_prng_seeder_system(&rngname);
+ fprintf(stderr, " RNG: %s\n", rngname);
+ if (cc->iaes_cbcenc != 0) {
+ fprintf(stderr, " AES/CBC (enc): %s\n",
+ get_algo_name(cc->iaes_cbcenc, 0));
+ }
+ if (cc->iaes_cbcdec != 0) {
+ fprintf(stderr, " AES/CBC (dec): %s\n",
+ get_algo_name(cc->iaes_cbcdec, 0));
+ }
+ if (cc->iaes_ctr != 0) {
+ fprintf(stderr, " AES/CTR: %s\n",
+ get_algo_name(cc->iaes_cbcdec, 0));
+ }
+ if (cc->iaes_ctrcbc != 0) {
+ fprintf(stderr, " AES/CCM: %s\n",
+ get_algo_name(cc->iaes_ctrcbc, 0));
+ }
+ if (cc->ides_cbcenc != 0) {
+ fprintf(stderr, " DES/CBC (enc): %s\n",
+ get_algo_name(cc->ides_cbcenc, 0));
+ }
+ if (cc->ides_cbcdec != 0) {
+ fprintf(stderr, " DES/CBC (dec): %s\n",
+ get_algo_name(cc->ides_cbcdec, 0));
+ }
+ if (cc->ighash != 0) {
+ fprintf(stderr, " GHASH (GCM): %s\n",
+ get_algo_name(cc->ighash, 0));
+ }
+ if (cc->ichacha != 0) {
+ fprintf(stderr, " ChaCha20: %s\n",
+ get_algo_name(cc->ichacha, 0));
+ }
+ if (cc->ipoly != 0) {
+ fprintf(stderr, " Poly1305: %s\n",
+ get_algo_name(cc->ipoly, 0));
+ }
+ if (cc->iec != 0) {
+ fprintf(stderr, " EC: %s\n",
+ get_algo_name(cc->iec, 0));
+ }
+ if (cc->iecdsa != 0) {
+ fprintf(stderr, " ECDSA: %s\n",
+ get_algo_name(cc->iecdsa, 0));
+ }
+ if (cc->irsavrfy != 0) {
+ fprintf(stderr, " RSA (vrfy): %s\n",
+ get_algo_name(cc->irsavrfy, 0));
+ }
+ }
+
+#ifdef _WIN32
+ fd_event = WSA_INVALID_EVENT;
+ can_send = 0;
+ can_recv = 0;
+ bb.ptr = bb.len = 0;
+#endif
+
+ /*
+ * On Unix systems, we need to follow three descriptors:
+ * standard input (0), standard output (1), and the socket
+ * itself (for both read and write). This is done with a poll()
+ * call.
+ *
+ * On Windows systems, we use WSAEventSelect() to associate
+ * an event handle with the network activity, and we use
+ * WaitForMultipleObjectsEx() on that handle and the standard
+ * input handle, when appropriate. Standard output is assumed
+ * to be always writeable, and standard input to be the console;
+ * this does not work well (or at all) with redirections (to
+ * pipes or files) but it should be enough for a debug tool
+ * (TODO: make something that handles redirections as well).
+ */
+
+#ifdef _WIN32
+ fd_event = WSACreateEvent();
+ if (fd_event == WSA_INVALID_EVENT) {
+ fprintf(stderr, "ERROR: WSACreateEvent() failed with %d\n",
+ WSAGetLastError());
+ retcode = -2;
+ goto engine_exit;
+ }
+ WSAEventSelect(fd, fd_event, FD_READ | FD_WRITE | FD_CLOSE);
+ h_in = GetStdHandle(STD_INPUT_HANDLE);
+ h_out = GetStdHandle(STD_OUTPUT_HANDLE);
+ SetConsoleMode(h_in, ENABLE_ECHO_INPUT
+ | ENABLE_LINE_INPUT
+ | ENABLE_PROCESSED_INPUT
+ | ENABLE_PROCESSED_OUTPUT
+ | ENABLE_WRAP_AT_EOL_OUTPUT);
+#else
+ /*
+ * Make sure that stdin and stdout are non-blocking.
+ */
+ fcntl(0, F_SETFL, O_NONBLOCK);
+ fcntl(1, F_SETFL, O_NONBLOCK);
+#endif
+
+ /*
+ * Perform the loop.
+ */
+ for (;;) {
+ unsigned st;
+ int sendrec, recvrec, sendapp, recvapp;
+#ifdef _WIN32
+ HANDLE pfd[2];
+ DWORD wt;
+#else
+ struct pollfd pfd[3];
+ int n;
+#endif
+ size_t u, k_fd, k_in, k_out;
+ int sendrec_ok, recvrec_ok, sendapp_ok, recvapp_ok;
+
+ /*
+ * Get current engine state.
+ */
+ st = br_ssl_engine_current_state(cc);
+ if (st == BR_SSL_CLOSED) {
+ int err;
+
+ err = br_ssl_engine_last_error(cc);
+ if (err == BR_ERR_OK) {
+ if (verbose) {
+ fprintf(stderr,
+ "SSL closed normally\n");
+ }
+ retcode = 0;
+ goto engine_exit;
+ } else {
+ fprintf(stderr, "ERROR: SSL error %d", err);
+ retcode = err;
+ if (err >= BR_ERR_SEND_FATAL_ALERT) {
+ err -= BR_ERR_SEND_FATAL_ALERT;
+ fprintf(stderr,
+ " (sent alert %d)\n", err);
+ } else if (err >= BR_ERR_RECV_FATAL_ALERT) {
+ err -= BR_ERR_RECV_FATAL_ALERT;
+ fprintf(stderr,
+ " (received alert %d)\n", err);
+ } else {
+ const char *ename;
+
+ ename = find_error_name(err, NULL);
+ if (ename == NULL) {
+ ename = "unknown";
+ }
+ fprintf(stderr, " (%s)\n", ename);
+ }
+ goto engine_exit;
+ }
+ }
+
+ /*
+ * Compute descriptors that must be polled, depending
+ * on engine state.
+ */
+ sendrec = ((st & BR_SSL_SENDREC) != 0);
+ recvrec = ((st & BR_SSL_RECVREC) != 0);
+ sendapp = ((st & BR_SSL_SENDAPP) != 0);
+ recvapp = ((st & BR_SSL_RECVAPP) != 0);
+ if (verbose && sendapp && !hsdetails) {
+ char csn[80];
+ const char *pname;
+
+ fprintf(stderr, "Handshake completed\n");
+ fprintf(stderr, " version: ");
+ switch (cc->session.version) {
+ case BR_SSL30:
+ fprintf(stderr, "SSL 3.0");
+ break;
+ case BR_TLS10:
+ fprintf(stderr, "TLS 1.0");
+ break;
+ case BR_TLS11:
+ fprintf(stderr, "TLS 1.1");
+ break;
+ case BR_TLS12:
+ fprintf(stderr, "TLS 1.2");
+ break;
+ default:
+ fprintf(stderr, "unknown (0x%04X)",
+ (unsigned)cc->session.version);
+ break;
+ }
+ fprintf(stderr, "\n");
+ get_suite_name_ext(
+ cc->session.cipher_suite, csn, sizeof csn);
+ fprintf(stderr, " cipher suite: %s\n", csn);
+ if (uses_ecdhe(cc->session.cipher_suite)) {
+ get_curve_name_ext(
+ br_ssl_engine_get_ecdhe_curve(cc),
+ csn, sizeof csn);
+ fprintf(stderr,
+ " ECDHE curve: %s\n", csn);
+ }
+ fprintf(stderr, " secure renegotiation: %s\n",
+ cc->reneg == 1 ? "no" : "yes");
+ pname = br_ssl_engine_get_selected_protocol(cc);
+ if (pname != NULL) {
+ fprintf(stderr,
+ " protocol name (ALPN): %s\n",
+ pname);
+ }
+ hsdetails = 1;
+ }
+
+ k_fd = (size_t)-1;
+ k_in = (size_t)-1;
+ k_out = (size_t)-1;
+
+ u = 0;
+#ifdef _WIN32
+ /*
+ * If we recorded that we can send or receive data, and we
+ * want to do exactly that, then we don't wait; we just do
+ * it.
+ */
+ recvapp_ok = 0;
+ sendrec_ok = 0;
+ recvrec_ok = 0;
+ sendapp_ok = 0;
+
+ if (sendrec && can_send) {
+ sendrec_ok = 1;
+ } else if (recvrec && can_recv) {
+ recvrec_ok = 1;
+ } else if (recvapp) {
+ recvapp_ok = 1;
+ } else if (sendapp && in_avail_buffered(h_in, &bb)) {
+ sendapp_ok = 1;
+ } else {
+ /*
+ * If we cannot do I/O right away, then we must
+ * wait for some event, and try again.
+ */
+ pfd[u] = (HANDLE)fd_event;
+ k_fd = u;
+ u ++;
+ if (sendapp) {
+ pfd[u] = h_in;
+ k_in = u;
+ u ++;
+ }
+ wt = WaitForMultipleObjectsEx(u, pfd,
+ FALSE, INFINITE, FALSE);
+ if (wt == WAIT_FAILED) {
+ fprintf(stderr, "ERROR:"
+ " WaitForMultipleObjectsEx()"
+ " failed with 0x%08lX",
+ (unsigned long)GetLastError());
+ retcode = -2;
+ goto engine_exit;
+ }
+ if (wt == k_fd) {
+ WSANETWORKEVENTS e;
+
+ if (WSAEnumNetworkEvents(fd, fd_event, &e)) {
+ fprintf(stderr, "ERROR:"
+ " WSAEnumNetworkEvents()"
+ " failed with %d\n",
+ WSAGetLastError());
+ retcode = -2;
+ goto engine_exit;
+ }
+ if (e.lNetworkEvents & (FD_WRITE | FD_CLOSE)) {
+ can_send = 1;
+ }
+ if (e.lNetworkEvents & (FD_READ | FD_CLOSE)) {
+ can_recv = 1;
+ }
+ }
+ continue;
+ }
+#else
+ if (sendrec || recvrec) {
+ pfd[u].fd = fd;
+ pfd[u].revents = 0;
+ pfd[u].events = 0;
+ if (sendrec) {
+ pfd[u].events |= POLLOUT;
+ }
+ if (recvrec) {
+ pfd[u].events |= POLLIN;
+ }
+ k_fd = u;
+ u ++;
+ }
+ if (sendapp) {
+ pfd[u].fd = 0;
+ pfd[u].revents = 0;
+ pfd[u].events = POLLIN;
+ k_in = u;
+ u ++;
+ }
+ if (recvapp) {
+ pfd[u].fd = 1;
+ pfd[u].revents = 0;
+ pfd[u].events = POLLOUT;
+ k_out = u;
+ u ++;
+ }
+ n = poll(pfd, u, -1);
+ if (n < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ perror("ERROR: poll()");
+ retcode = -2;
+ goto engine_exit;
+ }
+ if (n == 0) {
+ continue;
+ }
+
+ /*
+ * We transform closures/errors into read+write accesses
+ * so as to force the read() or write() call that will
+ * detect the situation.
+ */
+ while (u -- > 0) {
+ if (pfd[u].revents & (POLLERR | POLLHUP)) {
+ pfd[u].revents |= POLLIN | POLLOUT;
+ }
+ }
+
+ recvapp_ok = recvapp && (pfd[k_out].revents & POLLOUT) != 0;
+ sendrec_ok = sendrec && (pfd[k_fd].revents & POLLOUT) != 0;
+ recvrec_ok = recvrec && (pfd[k_fd].revents & POLLIN) != 0;
+ sendapp_ok = sendapp && (pfd[k_in].revents & POLLIN) != 0;
+#endif
+
+ /*
+ * We give preference to outgoing data, on stdout and on
+ * the socket.
+ */
+ if (recvapp_ok) {
+ unsigned char *buf;
+ size_t len;
+#ifdef _WIN32
+ DWORD wlen;
+#else
+ ssize_t wlen;
+#endif
+
+ buf = br_ssl_engine_recvapp_buf(cc, &len);
+#ifdef _WIN32
+ if (!WriteFile(h_out, buf, len, &wlen, NULL)) {
+ if (verbose) {
+ fprintf(stderr, "stdout closed...\n");
+ }
+ retcode = -2;
+ goto engine_exit;
+ }
+#else
+ wlen = write(1, buf, len);
+ if (wlen <= 0) {
+ if (verbose) {
+ fprintf(stderr, "stdout closed...\n");
+ }
+ retcode = -2;
+ goto engine_exit;
+ }
+#endif
+ br_ssl_engine_recvapp_ack(cc, wlen);
+ continue;
+ }
+ if (sendrec_ok) {
+ unsigned char *buf;
+ size_t len;
+ int wlen;
+
+ buf = br_ssl_engine_sendrec_buf(cc, &len);
+ wlen = send(fd, buf, len, 0);
+ if (wlen <= 0) {
+#ifdef _WIN32
+ int err;
+
+ err = WSAGetLastError();
+ if (err == EWOULDBLOCK
+ || err == WSAEWOULDBLOCK)
+ {
+ can_send = 0;
+ continue;
+ }
+#else
+ if (errno == EINTR || errno == EWOULDBLOCK) {
+ continue;
+ }
+#endif
+ if (verbose) {
+ fprintf(stderr, "socket closed...\n");
+ }
+ retcode = -1;
+ goto engine_exit;
+ }
+ if (trace) {
+ dump_blob("Outgoing bytes", buf, wlen);
+ }
+ br_ssl_engine_sendrec_ack(cc, wlen);
+ continue;
+ }
+ if (recvrec_ok) {
+ unsigned char *buf;
+ size_t len;
+ int rlen;
+
+ buf = br_ssl_engine_recvrec_buf(cc, &len);
+ rlen = recv(fd, buf, len, 0);
+ if (rlen == 0) {
+ if (verbose) {
+ fprintf(stderr, "socket closed...\n");
+ }
+ retcode = -1;
+ goto engine_exit;
+ }
+ if (rlen < 0) {
+#ifdef _WIN32
+ int err;
+
+ err = WSAGetLastError();
+ if (err == EWOULDBLOCK
+ || err == WSAEWOULDBLOCK)
+ {
+ can_recv = 0;
+ continue;
+ }
+#else
+ if (errno == EINTR || errno == EWOULDBLOCK) {
+ continue;
+ }
+#endif
+ if (verbose) {
+ fprintf(stderr, "socket broke...\n");
+ }
+ retcode = -1;
+ goto engine_exit;
+ }
+ if (trace) {
+ dump_blob("Incoming bytes", buf, rlen);
+ }
+ br_ssl_engine_recvrec_ack(cc, rlen);
+ continue;
+ }
+ if (sendapp_ok) {
+ unsigned char *buf;
+ size_t len;
+#ifdef _WIN32
+ int rlen;
+#else
+ ssize_t rlen;
+#endif
+
+ buf = br_ssl_engine_sendapp_buf(cc, &len);
+#ifdef _WIN32
+ rlen = in_read_buffered(h_in, &bb, buf, len);
+#else
+ rlen = read(0, buf, len);
+#endif
+ if (rlen <= 0) {
+ if (verbose) {
+ fprintf(stderr, "stdin closed...\n");
+ }
+ br_ssl_engine_close(cc);
+ } else if (!run_command(cc, buf, rlen)) {
+ br_ssl_engine_sendapp_ack(cc, rlen);
+ }
+ br_ssl_engine_flush(cc, 0);
+ continue;
+ }
+
+ /* We should never reach that point. */
+ fprintf(stderr, "ERROR: poll() misbehaves\n");
+ retcode = -2;
+ goto engine_exit;
+ }
+
+ /*
+ * Release allocated structures.
+ */
+engine_exit:
+#ifdef _WIN32
+ if (fd_event != WSA_INVALID_EVENT) {
+ WSACloseEvent(fd_event);
+ }
+#endif
+ return retcode;
+}
diff --git a/test/monniaux/BearSSL/tools/ta.c b/test/monniaux/BearSSL/tools/ta.c
new file mode 100644
index 00000000..df72e2bf
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/ta.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+static const char *
+curve_to_sym(int curve)
+{
+ switch (curve) {
+ case BR_EC_sect163k1: return "BR_EC_sect163k1";
+ case BR_EC_sect163r1: return "BR_EC_sect163r1";
+ case BR_EC_sect163r2: return "BR_EC_sect163r2";
+ case BR_EC_sect193r1: return "BR_EC_sect193r1";
+ case BR_EC_sect193r2: return "BR_EC_sect193r2";
+ case BR_EC_sect233k1: return "BR_EC_sect233k1";
+ case BR_EC_sect233r1: return "BR_EC_sect233r1";
+ case BR_EC_sect239k1: return "BR_EC_sect239k1";
+ case BR_EC_sect283k1: return "BR_EC_sect283k1";
+ case BR_EC_sect283r1: return "BR_EC_sect283r1";
+ case BR_EC_sect409k1: return "BR_EC_sect409k1";
+ case BR_EC_sect409r1: return "BR_EC_sect409r1";
+ case BR_EC_sect571k1: return "BR_EC_sect571k1";
+ case BR_EC_sect571r1: return "BR_EC_sect571r1";
+ case BR_EC_secp160k1: return "BR_EC_secp160k1";
+ case BR_EC_secp160r1: return "BR_EC_secp160r1";
+ case BR_EC_secp160r2: return "BR_EC_secp160r2";
+ case BR_EC_secp192k1: return "BR_EC_secp192k1";
+ case BR_EC_secp192r1: return "BR_EC_secp192r1";
+ case BR_EC_secp224k1: return "BR_EC_secp224k1";
+ case BR_EC_secp224r1: return "BR_EC_secp224r1";
+ case BR_EC_secp256k1: return "BR_EC_secp256k1";
+ case BR_EC_secp256r1: return "BR_EC_secp256r1";
+ case BR_EC_secp384r1: return "BR_EC_secp384r1";
+ case BR_EC_secp521r1: return "BR_EC_secp521r1";
+ case BR_EC_brainpoolP256r1: return "BR_EC_brainpoolP256r1";
+ case BR_EC_brainpoolP384r1: return "BR_EC_brainpoolP384r1";
+ case BR_EC_brainpoolP512r1: return "BR_EC_brainpoolP512r1";
+ }
+ return NULL;
+}
+
+static void
+print_blob(const char *name, const unsigned char *buf, size_t len)
+{
+ size_t u;
+
+ printf("\nstatic const unsigned char %s[] = {", name);
+ for (u = 0; u < len; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", buf[u]);
+ }
+ printf("\n};\n");
+}
+
+static int
+print_ta_internals(br_x509_trust_anchor *ta, long ctr)
+{
+ char tmp[25];
+
+ sprintf(tmp, "TA%ld_DN", ctr);
+ print_blob(tmp, ta->dn.data, ta->dn.len);
+ switch (ta->pkey.key_type) {
+ case BR_KEYTYPE_RSA:
+ sprintf(tmp, "TA%ld_RSA_N", ctr);
+ print_blob(tmp, ta->pkey.key.rsa.n, ta->pkey.key.rsa.nlen);
+ sprintf(tmp, "TA%ld_RSA_E", ctr);
+ print_blob(tmp, ta->pkey.key.rsa.e, ta->pkey.key.rsa.elen);
+ break;
+ case BR_KEYTYPE_EC:
+ sprintf(tmp, "TA%ld_EC_Q", ctr);
+ print_blob(tmp, ta->pkey.key.ec.q, ta->pkey.key.ec.qlen);
+ break;
+ default:
+ fprintf(stderr, "ERROR: unknown anchor key type '%d'\n",
+ ta->pkey.key_type);
+ return -1;
+ }
+ return 0;
+}
+
+static void
+print_ta(br_x509_trust_anchor *ta, long ctr)
+{
+ char tmp[25];
+
+ printf("\t{\n");
+ printf("\t\t{ (unsigned char *)TA%ld_DN, sizeof TA%ld_DN },\n",
+ ctr, ctr);
+ printf("\t\t%s,\n", (ta->flags & BR_X509_TA_CA)
+ ? "BR_X509_TA_CA" : "0");
+ printf("\t\t{\n");
+ switch (ta->pkey.key_type) {
+ const char *cname;
+
+ case BR_KEYTYPE_RSA:
+ printf("\t\t\tBR_KEYTYPE_RSA,\n");
+ printf("\t\t\t{ .rsa = {\n");
+ printf("\t\t\t\t(unsigned char *)TA%ld_RSA_N,"
+ " sizeof TA%ld_RSA_N,\n", ctr, ctr);
+ printf("\t\t\t\t(unsigned char *)TA%ld_RSA_E,"
+ " sizeof TA%ld_RSA_E,\n", ctr, ctr);
+ printf("\t\t\t} }\n");
+ break;
+ case BR_KEYTYPE_EC:
+ printf("\t\t\tBR_KEYTYPE_EC,\n");
+ printf("\t\t\t{ .ec = {\n");
+ cname = curve_to_sym(ta->pkey.key.ec.curve);
+ if (cname == NULL) {
+ sprintf(tmp, "%d", ta->pkey.key.ec.curve);
+ cname = tmp;
+ }
+ printf("\t\t\t\t%s,\n", cname);
+ printf("\t\t\t\t(unsigned char *)TA%ld_EC_Q,"
+ " sizeof TA%ld_EC_Q,\n", ctr, ctr);
+ printf("\t\t\t} }\n");
+ }
+ printf("\t\t}\n");
+ printf("\t}");
+}
+
+static void
+usage_ta(void)
+{
+ fprintf(stderr,
+"usage: brssl ta [ options ] file...\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+}
+
+/* see brssl.h */
+int
+do_ta(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int i, num_files;
+ anchor_list tas = VEC_INIT;
+ size_t u, num;
+
+ retcode = 0;
+ verbose = 1;
+ num_files = 0;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ num_files ++;
+ continue;
+ }
+ argv[i] = NULL;
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_ta();
+ goto ta_exit_error;
+ }
+ }
+ if (num_files == 0) {
+ fprintf(stderr, "ERROR: no certificate file provided\n");
+ usage_ta();
+ goto ta_exit_error;
+ }
+
+ for (i = 0; i < argc; i ++) {
+ const char *fname;
+ size_t len1, len2;
+
+ fname = argv[i];
+ if (fname == NULL) {
+ continue;
+ }
+ if (verbose) {
+ fprintf(stderr, "Reading file '%s': ", fname);
+ fflush(stderr);
+ }
+ len1 = VEC_LEN(tas);
+ if (read_trust_anchors(&tas, fname) == 0) {
+ goto ta_exit_error;
+ }
+ len2 = VEC_LEN(tas) - len1;
+ if (verbose) {
+ fprintf(stderr, "%lu trust anchor%s\n",
+ (unsigned long)len2, len2 > 1 ? "s" : "");
+ }
+ }
+ num = VEC_LEN(tas);
+ for (u = 0; u < num; u ++) {
+ if (print_ta_internals(&VEC_ELT(tas, u), u) < 0) {
+ goto ta_exit_error;
+ }
+ }
+ printf("\nstatic const br_x509_trust_anchor TAs[%ld] = {", (long)num);
+ for (u = 0; u < num; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ printf("\n");
+ print_ta(&VEC_ELT(tas, u), u);
+ }
+ printf("\n};\n");
+ printf("\n#define TAs_NUM %ld\n", (long)num);
+
+ /*
+ * Release allocated structures.
+ */
+ta_exit:
+ VEC_CLEAREXT(tas, free_ta_contents);
+ return retcode;
+
+ta_exit_error:
+ retcode = -1;
+ goto ta_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/twrch.c b/test/monniaux/BearSSL/tools/twrch.c
new file mode 100644
index 00000000..9cce03e5
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/twrch.c
@@ -0,0 +1,1069 @@
+/*
+ * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <signal.h>
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+#include "brssl.h"
+
+static int verbose = 0;
+
+static void
+usage_twrch(void)
+{
+ fprintf(stderr,
+"usage: brssl twrch [ options ]\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -trace dump all packets on stderr\n");
+ fprintf(stderr,
+" -v verbose error messages on stderr\n");
+ fprintf(stderr,
+" -server act as an SSL server\n");
+ fprintf(stderr,
+" -client act as an SSL client\n");
+ fprintf(stderr,
+" -sni name use specified name for SNI\n");
+ fprintf(stderr,
+" -mono use monodirectional buffering\n");
+ fprintf(stderr,
+" -buf length set the I/O buffer length (in bytes)\n");
+ fprintf(stderr,
+" -cache length set the session cache storage length (in bytes)\n");
+ fprintf(stderr,
+" -cert fname read certificate chain from file 'fname'\n");
+ fprintf(stderr,
+" -key fname read private key from file 'fname'\n");
+ fprintf(stderr,
+" -CA file add trust anchors from 'file' (for peer auth)\n");
+ fprintf(stderr,
+" -anon_ok request but do not require a client certificate\n");
+ fprintf(stderr,
+" -nostaticecdh prohibit full-static ECDH (client only)\n");
+ fprintf(stderr,
+" -list list supported names (protocols, algorithms...)\n");
+ fprintf(stderr,
+" -vmin name set minimum supported version (default: TLS-1.0)\n");
+ fprintf(stderr,
+" -vmax name set maximum supported version (default: TLS-1.2)\n");
+ fprintf(stderr,
+" -cs names set list of supported cipher suites (comma-separated)\n");
+ fprintf(stderr,
+" -hf names add support for some hash functions (comma-separated)\n");
+ fprintf(stderr,
+" -minhello len set minimum ClientHello length (in bytes)\n");
+ fprintf(stderr,
+" -serverpref enforce server's preferences for cipher suites\n");
+ fprintf(stderr,
+" -noreneg prohibit renegotiations\n");
+ fprintf(stderr,
+" -alpn name add protocol name to list of protocols (ALPN extension)\n");
+ fprintf(stderr,
+" -strictalpn fail on ALPN mismatch\n");
+}
+
+static void
+free_alpn(void *alpn)
+{
+ xfree(*(char **)alpn);
+}
+
+static void
+dump_blob(const char *name, const void *data, size_t len)
+{
+ const unsigned char *buf;
+ size_t u;
+
+ buf = data;
+ fprintf(stderr, "%s (len = %lu)", name, (unsigned long)len);
+ for (u = 0; u < len; u ++) {
+ if ((u & 15) == 0) {
+ fprintf(stderr, "\n%08lX ", (unsigned long)u);
+ } else if ((u & 7) == 0) {
+ fprintf(stderr, " ");
+ }
+ fprintf(stderr, " %02x", buf[u]);
+ }
+ fprintf(stderr, "\n");
+}
+
+/*
+ * Callback for reading bytes from standard input.
+ */
+static int
+stdin_read(void *ctx, unsigned char *buf, size_t len)
+{
+ for (;;) {
+#ifdef _WIN32
+ DWORD rlen;
+#else
+ ssize_t rlen;
+#endif
+ int eof;
+
+#ifdef _WIN32
+ eof = !ReadFile(GetStdHandle(STD_INPUT_HANDLE),
+ buf, len, &rlen, NULL) || rlen == 0;
+#else
+ rlen = read(0, buf, len);
+ if (rlen <= 0) {
+ if (rlen < 0 && errno == EINTR) {
+ continue;
+ }
+ eof = 1;
+ } else {
+ eof = 0;
+ }
+#endif
+ if (eof) {
+ if (*(int *)ctx) {
+ if (verbose) {
+ fprintf(stderr, "recv: EOF\n");
+ }
+ }
+ return -1;
+ }
+ if (*(int *)ctx) {
+ dump_blob("recv", buf, (size_t)rlen);
+ }
+ return (int)rlen;
+ }
+}
+
+/*
+ * Callback for writing bytes on standard output.
+ */
+static int
+stdout_write(void *ctx, const unsigned char *buf, size_t len)
+{
+ for (;;) {
+#ifdef _WIN32
+ DWORD wlen;
+#else
+ ssize_t wlen;
+#endif
+ int eof;
+
+#ifdef _WIN32
+ eof = !WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
+ buf, len, &wlen, NULL);
+#else
+ wlen = write(1, buf, len);
+ if (wlen <= 0) {
+ if (wlen < 0 && errno == EINTR) {
+ continue;
+ }
+ eof = 1;
+ } else {
+ eof = 0;
+ }
+#endif
+ if (eof) {
+ if (*(int *)ctx) {
+ if (verbose) {
+ fprintf(stderr, "send: EOF\n");
+ }
+ }
+ return -1;
+ }
+ if (*(int *)ctx) {
+ dump_blob("send", buf, (size_t)wlen);
+ }
+ return (int)wlen;
+ }
+}
+
+static void
+print_error(int err)
+{
+ const char *name, *comment;
+
+ name = find_error_name(err, &comment);
+ if (name != NULL) {
+ fprintf(stderr, "ERR %d: %s\n %s\n", err, name, comment);
+ return;
+ }
+ if (err >= BR_ERR_RECV_FATAL_ALERT
+ && err < BR_ERR_RECV_FATAL_ALERT + 256)
+ {
+ fprintf(stderr, "ERR %d: received fatal alert %d\n",
+ err, err - BR_ERR_RECV_FATAL_ALERT);
+ return;
+ }
+ if (err >= BR_ERR_SEND_FATAL_ALERT
+ && err < BR_ERR_SEND_FATAL_ALERT + 256)
+ {
+ fprintf(stderr, "ERR %d: sent fatal alert %d\n",
+ err, err - BR_ERR_SEND_FATAL_ALERT);
+ return;
+ }
+ fprintf(stderr, "ERR %d: UNKNOWN\n", err);
+}
+
+/* see brssl.h */
+int
+do_twrch(int argc, char *argv[])
+{
+ int retcode;
+ int trace;
+ int is_client;
+ int is_server;
+ const char *sni;
+ int i, bidi;
+ unsigned vmin, vmax;
+ cipher_suite *suites;
+ size_t num_suites;
+ uint16_t *suite_ids;
+ unsigned hfuns;
+ br_x509_certificate *chain;
+ size_t chain_len;
+ int cert_signer_algo;
+ private_key *sk;
+ int nostaticecdh;
+ anchor_list anchors = VEC_INIT;
+ VECTOR(char *) alpn_names = VEC_INIT;
+ br_x509_minimal_context xc;
+ x509_noanchor_context xwc;
+ const br_hash_class *dnhash;
+ size_t u;
+ union {
+ br_ssl_engine_context eng;
+ br_ssl_server_context srv;
+ br_ssl_client_context cnt;
+ } cc;
+ br_ssl_session_cache_lru lru;
+ unsigned char *iobuf, *cache;
+ size_t iobuf_len, cache_len, minhello_len;
+ br_sslio_context ioc;
+ uint32_t flags;
+ int reconnect;
+
+ retcode = 0;
+ trace = 0;
+ is_client = 0;
+ is_server = 0;
+ sni = NULL;
+ bidi = 1;
+ vmin = 0;
+ vmax = 0;
+ suites = NULL;
+ num_suites = 0;
+ suite_ids = NULL;
+ hfuns = 0;
+ chain = NULL;
+ chain_len = 0;
+ cert_signer_algo = 0;
+ sk = NULL;
+ nostaticecdh = 0;
+ iobuf = NULL;
+ iobuf_len = 0;
+ cache = NULL;
+ cache_len = (size_t)-1;
+ minhello_len = (size_t)-1;
+ flags = 0;
+ reconnect = 0;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (eqstr(arg, "-trace")) {
+ trace = 1;
+ } else if (eqstr(arg, "-v")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-server")) {
+ is_server = 1;
+ } else if (eqstr(arg, "-client")) {
+ is_client = 1;
+ } else if (eqstr(arg, "-sni")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-sni'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (sni != NULL) {
+ fprintf(stderr, "ERROR: duplicate SNI\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ sni = arg;
+ } else if (eqstr(arg, "-mono")) {
+ bidi = 0;
+ } else if (eqstr(arg, "-buf")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-buf'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (iobuf_len != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate I/O buffer length\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ iobuf_len = parse_size(arg);
+ if (iobuf_len == (size_t)-1) {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-cache")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cache'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (cache_len != (size_t)-1) {
+ fprintf(stderr, "ERROR: duplicate session"
+ " cache length\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ cache_len = parse_size(arg);
+ if (cache_len == (size_t)-1) {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-cert")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cert'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (chain != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate certificate chain\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ chain = read_certificates(arg, &chain_len);
+ if (chain == NULL || chain_len == 0) {
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-key")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-key'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (sk != NULL) {
+ fprintf(stderr,
+ "ERROR: duplicate private key\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ sk = read_private_key(arg);
+ if (sk == NULL) {
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-CA")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-CA'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (read_trust_anchors(&anchors, arg) == 0) {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-anon_ok")) {
+ flags |= BR_OPT_TOLERATE_NO_CLIENT_AUTH;
+ } else if (eqstr(arg, "-nostaticecdh")) {
+ nostaticecdh = 1;
+ } else if (eqstr(arg, "-list")) {
+ list_names();
+ goto twrch_exit;
+ } else if (eqstr(arg, "-vmin")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmin'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (vmin != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate minimum version\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ vmin = parse_version(arg, strlen(arg));
+ if (vmin == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-vmax")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-vmax'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (vmax != 0) {
+ fprintf(stderr,
+ "ERROR: duplicate maximum version\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ vmax = parse_version(arg, strlen(arg));
+ if (vmax == 0) {
+ fprintf(stderr,
+ "ERROR: unrecognised version '%s'\n",
+ arg);
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-cs")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-cs'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (suites != NULL) {
+ fprintf(stderr, "ERROR: duplicate list"
+ " of cipher suites\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ suites = parse_suites(arg, &num_suites);
+ if (suites == NULL) {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-hf")) {
+ unsigned x;
+
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-hf'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ x = parse_hash_functions(arg);
+ if (x == 0) {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ hfuns |= x;
+ } else if (eqstr(arg, "-minhello")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-minhello'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ arg = argv[i];
+ if (minhello_len != (size_t)-1) {
+ fprintf(stderr, "ERROR: duplicate minimum"
+ " ClientHello length\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ minhello_len = parse_size(arg);
+ /*
+ * Minimum ClientHello length must fit on 16 bits.
+ */
+ if (minhello_len == (size_t)-1
+ || (((minhello_len >> 12) >> 4) != 0))
+ {
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else if (eqstr(arg, "-serverpref")) {
+ flags |= BR_OPT_ENFORCE_SERVER_PREFERENCES;
+ } else if (eqstr(arg, "-noreneg")) {
+ flags |= BR_OPT_NO_RENEGOTIATION;
+ } else if (eqstr(arg, "-alpn")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-alpn'\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ VEC_ADD(alpn_names, xstrdup(argv[i]));
+ } else if (eqstr(arg, "-strictalpn")) {
+ flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ }
+
+ /*
+ * Verify consistency of options.
+ */
+ if (!is_client && !is_server) {
+ fprintf(stderr, "ERROR:"
+ " one of -server and -client must be specified\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (is_client && is_server) {
+ fprintf(stderr, "ERROR:"
+ " -server and -client may not be both specified\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+
+ if (vmin == 0) {
+ vmin = BR_TLS10;
+ }
+ if (vmax == 0) {
+ vmax = BR_TLS12;
+ }
+ if (vmax < vmin) {
+ fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
+ " version combination\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (is_server) {
+ if (chain == NULL) {
+ fprintf(stderr, "ERROR: no certificate specified"
+ " for server (-cert)\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (sk == NULL) {
+ fprintf(stderr, "ERROR: no private key specified"
+ " for server (-key)\n");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ } else {
+ if (chain == NULL && sk != NULL) {
+ fprintf(stderr, "ERROR: private key (-key)"
+ " but no certificate (-cert)");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ if (chain != NULL && sk == NULL) {
+ fprintf(stderr, "ERROR: certificate (-cert)"
+ " but no private key (-key)");
+ usage_twrch();
+ goto twrch_exit_error;
+ }
+ }
+ if (suites == NULL) {
+ num_suites = 0;
+
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ num_suites ++;
+ }
+ }
+ suites = xmalloc(num_suites * sizeof *suites);
+ num_suites = 0;
+ for (u = 0; cipher_suites[u].name; u ++) {
+ if ((cipher_suites[u].req & REQ_TLS12) == 0
+ || vmax >= BR_TLS12)
+ {
+ suites[num_suites ++] = cipher_suites[u];
+ }
+ }
+ }
+ if (hfuns == 0) {
+ hfuns = (unsigned)-1;
+ }
+ if (sk != NULL) {
+ switch (sk->key_type) {
+ int curve;
+ uint32_t supp;
+
+ case BR_KEYTYPE_RSA:
+ break;
+ case BR_KEYTYPE_EC:
+ curve = sk->key.ec.curve;
+ supp = br_ec_get_default()->supported_curves;
+ if (curve > 31 || !((supp >> curve) & 1)) {
+ fprintf(stderr, "ERROR: private key curve (%d)"
+ " is not supported\n", curve);
+ goto twrch_exit_error;
+ }
+ break;
+ default:
+ fprintf(stderr, "ERROR: unsupported"
+ " private key type (%d)\n", sk->key_type);
+ goto twrch_exit_error;
+ }
+ }
+ if (chain != NULL) {
+ cert_signer_algo = get_cert_signer_algo(chain);
+ if (cert_signer_algo == 0) {
+ goto twrch_exit_error;
+ }
+ }
+ if (iobuf_len == 0) {
+ if (bidi) {
+ iobuf_len = BR_SSL_BUFSIZE_BIDI;
+ } else {
+ iobuf_len = BR_SSL_BUFSIZE_MONO;
+ }
+ }
+ iobuf = xmalloc(iobuf_len);
+ if (is_server) {
+ if (cache_len == (size_t)-1) {
+ cache_len = 5000;
+ }
+ cache = xmalloc(cache_len);
+ }
+
+ /*
+ * Initialise the relevant context.
+ */
+ if (is_client) {
+ br_ssl_client_zero(&cc.cnt);
+ } else {
+ br_ssl_server_zero(&cc.srv);
+ }
+
+ /*
+ * Compute implementation requirements and inject implementations.
+ */
+ suite_ids = xmalloc(num_suites * sizeof *suite_ids);
+ br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
+ br_ssl_engine_set_all_flags(&cc.eng, flags);
+ if (vmin <= BR_TLS11) {
+ if (!(hfuns & (1 << br_md5_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
+ goto twrch_exit_error;
+ }
+ if (!(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
+ goto twrch_exit_error;
+ }
+ }
+ for (u = 0; u < num_suites; u ++) {
+ unsigned req;
+
+ req = suites[u].req;
+ suite_ids[u] = suites[u].suite;
+ if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires TLS 1.2\n",
+ suites[u].name);
+ goto twrch_exit_error;
+ }
+ if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-1\n",
+ suites[u].name);
+ goto twrch_exit_error;
+ }
+ if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-256\n",
+ suites[u].name);
+ goto twrch_exit_error;
+ }
+ if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
+ fprintf(stderr,
+ "ERROR: cipher suite %s requires SHA-384\n",
+ suites[u].name);
+ goto twrch_exit_error;
+ }
+ /* TODO: algorithm implementation selection */
+ if ((req & REQ_AESCBC) != 0) {
+ br_ssl_engine_set_default_aes_cbc(&cc.eng);
+ }
+ if ((req & REQ_AESCCM) != 0) {
+ br_ssl_engine_set_default_aes_ccm(&cc.eng);
+ }
+ if ((req & REQ_AESGCM) != 0) {
+ br_ssl_engine_set_default_aes_gcm(&cc.eng);
+ }
+ if ((req & REQ_CHAPOL) != 0) {
+ br_ssl_engine_set_default_chapol(&cc.eng);
+ }
+ if ((req & REQ_3DESCBC) != 0) {
+ br_ssl_engine_set_default_des_cbc(&cc.eng);
+ }
+ if (is_client && (req & REQ_RSAKEYX) != 0) {
+ br_ssl_client_set_default_rsapub(&cc.cnt);
+ }
+ if (is_client && (req & REQ_ECDHE_RSA) != 0) {
+ br_ssl_engine_set_default_rsavrfy(&cc.eng);
+ }
+ if (is_client && (req & REQ_ECDH) != 0) {
+ br_ssl_engine_set_default_ec(&cc.eng);
+ }
+ if ((req & (REQ_ECDHE_RSA | REQ_ECDHE_ECDSA)) != 0) {
+ br_ssl_engine_set_default_ec(&cc.eng);
+ }
+ }
+ br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
+
+ dnhash = NULL;
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ dnhash = hc;
+ br_ssl_engine_set_hash(&cc.eng, id, hc);
+ }
+ }
+ if (vmin <= BR_TLS11) {
+ br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
+ }
+ if (vmax >= BR_TLS12) {
+ if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
+ br_ssl_engine_set_prf_sha256(&cc.eng,
+ &br_tls12_sha256_prf);
+ }
+ if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
+ br_ssl_engine_set_prf_sha384(&cc.eng,
+ &br_tls12_sha384_prf);
+ }
+ }
+ if (VEC_LEN(alpn_names) != 0) {
+ br_ssl_engine_set_protocol_names(&cc.eng,
+ (const char **)&VEC_ELT(alpn_names, 0),
+ VEC_LEN(alpn_names));
+ }
+
+ /*
+ * In server role, we use a session cache (size can be
+ * specified; if size is zero, then no cache is set).
+ */
+ if (is_server && cache != NULL) {
+ br_ssl_session_cache_lru_init(&lru, cache, cache_len);
+ br_ssl_server_set_cache(&cc.srv, &lru.vtable);
+ }
+
+ /*
+ * For a server, set the policy handler.
+ */
+ if (is_server) {
+ switch (sk->key_type) {
+ case BR_KEYTYPE_RSA:
+ br_ssl_server_set_single_rsa(&cc.srv,
+ chain, chain_len, &sk->key.rsa,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ br_rsa_private_get_default(),
+ br_rsa_pkcs1_sign_get_default());
+ break;
+ case BR_KEYTYPE_EC:
+ br_ssl_server_set_single_ec(&cc.srv,
+ chain, chain_len, &sk->key.ec,
+ BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
+ cert_signer_algo,
+ br_ec_get_default(),
+ br_ecdsa_sign_asn1_get_default());
+ break;
+ default:
+ fprintf(stderr, "ERROR: unsupported"
+ " private key type (%d)\n", sk->key_type);
+ goto twrch_exit_error;
+ }
+ }
+
+ /*
+ * For a client, if a certificate was specified, use it.
+ */
+ if (is_client && chain != NULL) {
+ switch (sk->key_type) {
+ unsigned usages;
+
+ case BR_KEYTYPE_RSA:
+ br_ssl_client_set_single_rsa(&cc.cnt,
+ chain, chain_len, &sk->key.rsa,
+ br_rsa_pkcs1_sign_get_default());
+ break;
+ case BR_KEYTYPE_EC:
+ if (nostaticecdh) {
+ cert_signer_algo = 0;
+ usages = BR_KEYTYPE_SIGN;
+ } else {
+ usages = BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN;
+ }
+ br_ssl_client_set_single_ec(&cc.cnt,
+ chain, chain_len, &sk->key.ec,
+ usages, cert_signer_algo,
+ br_ec_get_default(),
+ br_ecdsa_sign_asn1_get_default());
+ break;
+ default:
+ fprintf(stderr, "ERROR: unsupported"
+ " private key type (%d)\n", sk->key_type);
+ goto twrch_exit_error;
+ }
+ }
+
+ /*
+ * On a client, or if trust anchors have been configured, then
+ * set an X.509 validation engine. If there are no trust anchors
+ * (client only), then a "no anchor" wrapper will be applied.
+ */
+ if (is_client || VEC_LEN(anchors) != 0) {
+ br_x509_minimal_init(&xc, dnhash,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ for (u = 0; hash_functions[u].name; u ++) {
+ const br_hash_class *hc;
+ int id;
+
+ hc = hash_functions[u].hclass;
+ id = (hc->desc >> BR_HASHDESC_ID_OFF)
+ & BR_HASHDESC_ID_MASK;
+ if ((hfuns & ((unsigned)1 << id)) != 0) {
+ br_x509_minimal_set_hash(&xc, id, hc);
+ }
+ }
+ br_ssl_engine_set_default_rsavrfy(&cc.eng);
+ br_ssl_engine_set_default_ecdsa(&cc.eng);
+ br_x509_minimal_set_rsa(&xc, br_rsa_pkcs1_vrfy_get_default());
+ br_x509_minimal_set_ecdsa(&xc,
+ br_ec_get_default(), br_ecdsa_vrfy_asn1_get_default());
+ br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
+
+ if (VEC_LEN(anchors) == 0) {
+ x509_noanchor_init(&xwc, &xc.vtable);
+ br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
+ } else {
+ br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
+ }
+ if (is_server) {
+ br_ssl_server_set_trust_anchor_names_alt(&cc.srv,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ }
+ }
+
+ /*
+ * Set I/O buffer.
+ */
+ br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
+
+ /*
+ * Start the engine.
+ */
+ if (is_client) {
+ br_ssl_client_reset(&cc.cnt, sni, 0);
+ }
+ if (is_server) {
+ br_ssl_server_reset(&cc.srv);
+ }
+
+ /*
+ * On Unix systems, we want to ignore SIGPIPE: if the peer
+ * closes the connection abruptly, then we want to report it
+ * as a "normal" error (exit code = 1).
+ */
+#ifndef _WIN32
+ signal(SIGPIPE, SIG_IGN);
+#endif
+
+ /*
+ * Initialize the callbacks for exchanging data over stdin and
+ * stdout.
+ */
+ br_sslio_init(&ioc, &cc.eng, stdin_read, &trace, stdout_write, &trace);
+
+ /*
+ * Run the Twrch protocol.
+ */
+ for (;;) {
+ br_sha1_context sc;
+ unsigned char hv[20], tmp[41];
+ uint64_t count;
+ int fb, i;
+
+ /*
+ * Read line, byte by byte, hashing it on the fly.
+ */
+ br_sha1_init(&sc);
+ count = 0;
+ fb = 0;
+ for (;;) {
+ unsigned char x;
+
+ if (br_sslio_read(&ioc, &x, 1) < 0) {
+ if (count == 0 && reconnect) {
+ reconnect = 0;
+ if (br_sslio_close(&ioc) < 0) {
+ goto twrch_loop_finished;
+ }
+ if (is_client) {
+ br_ssl_client_reset(
+ &cc.cnt, sni, 1);
+ }
+ if (is_server) {
+ br_ssl_server_reset(&cc.srv);
+ }
+ br_sslio_init(&ioc, &cc.eng,
+ stdin_read, &trace,
+ stdout_write, &trace);
+ continue;
+ }
+ goto twrch_loop_finished;
+ }
+ if (count == 0) {
+ fb = x;
+ }
+ if (x == 0x0A) {
+ break;
+ }
+ br_sha1_update(&sc, &x, 1);
+ count ++;
+ }
+ if (count == 1) {
+ switch (fb) {
+ case 'C':
+ br_sslio_close(&ioc);
+ goto twrch_loop_finished;
+ case 'T':
+ if (br_sslio_close(&ioc) < 0) {
+ goto twrch_loop_finished;
+ }
+ if (is_client) {
+ br_ssl_client_reset(&cc.cnt, sni, 1);
+ }
+ if (is_server) {
+ br_ssl_server_reset(&cc.srv);
+ }
+ br_sslio_init(&ioc, &cc.eng,
+ stdin_read, &trace,
+ stdout_write, &trace);
+ continue;
+ case 'G':
+ if (!br_ssl_engine_renegotiate(&cc.eng)) {
+ br_sslio_write_all(&ioc, "DENIED\n", 7);
+ br_sslio_flush(&ioc);
+ } else {
+ br_sslio_write_all(&ioc, "OK\n", 3);
+ br_sslio_flush(&ioc);
+ }
+ continue;
+ case 'R':
+ reconnect = 1;
+ br_sslio_write_all(&ioc, "OK\n", 3);
+ br_sslio_flush(&ioc);
+ continue;
+ case 'U':
+ if (is_client) {
+ br_ssl_client_forget_session(&cc.cnt);
+ }
+ if (is_server && cache != NULL) {
+ br_ssl_session_parameters pp;
+
+ br_ssl_engine_get_session_parameters(
+ &cc.eng, &pp);
+ if (pp.session_id_len == 32) {
+ br_ssl_session_cache_lru_forget(
+ &lru, pp.session_id);
+ }
+ }
+ br_sslio_write_all(&ioc, "DONE\n", 5);
+ br_sslio_flush(&ioc);
+ continue;
+ }
+ }
+ br_sha1_out(&sc, hv);
+ for (i = 0; i < 20; i ++) {
+ int x;
+
+ x = hv[i];
+ tmp[(i << 1) + 0] = "0123456789abcdef"[x >> 4];
+ tmp[(i << 1) + 1] = "0123456789abcdef"[x & 15];
+ }
+ tmp[40] = 0x0A;
+ br_sslio_write_all(&ioc, tmp, 41);
+ br_sslio_flush(&ioc);
+ }
+
+twrch_loop_finished:
+ if (br_ssl_engine_current_state(&cc.eng) == BR_SSL_CLOSED) {
+ int err;
+
+ err = br_ssl_engine_last_error(&cc.eng);
+ if (err == 0) {
+ retcode = 0;
+ } else {
+ if (verbose) {
+ print_error(err);
+ }
+ retcode = 1;
+ }
+ } else {
+ if (verbose) {
+ fprintf(stderr, "Engine not closed!\n");
+ }
+ retcode = 1;
+ }
+
+ /*
+ * Release allocated structures.
+ */
+twrch_exit:
+ xfree(suites);
+ xfree(suite_ids);
+ free_certificates(chain, chain_len);
+ free_private_key(sk);
+ VEC_CLEAREXT(anchors, &free_ta_contents);
+ VEC_CLEAREXT(alpn_names, &free_alpn);
+ xfree(iobuf);
+ xfree(cache);
+ return retcode;
+
+twrch_exit_error:
+ retcode = -1;
+ goto twrch_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/vector.c b/test/monniaux/BearSSL/tools/vector.c
new file mode 100644
index 00000000..96df3072
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/vector.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "brssl.h"
+
+/*
+ * Prepare a vector buffer for adding 'extra' elements.
+ * buf current buffer
+ * esize size of a vector element
+ * ptr pointer to the 'ptr' vector field
+ * len pointer to the 'len' vector field
+ * extra number of elements to add
+ *
+ * If the buffer must be enlarged, then this function allocates the new
+ * buffer and releases the old one. The new buffer address is then returned.
+ * If the buffer needs not be enlarged, then the buffer address is returned.
+ *
+ * In case of enlargement, the 'len' field is adjusted accordingly. The
+ * 'ptr' field is not modified.
+ */
+void *
+vector_expand(void *buf,
+ size_t esize, size_t *ptr, size_t *len, size_t extra)
+{
+ size_t nlen;
+ void *nbuf;
+
+ if (*len - *ptr >= extra) {
+ return buf;
+ }
+ nlen = (*len << 1);
+ if (nlen - *ptr < extra) {
+ nlen = extra + *ptr;
+ if (nlen < 8) {
+ nlen = 8;
+ }
+ }
+ nbuf = xmalloc(nlen * esize);
+ if (buf != NULL) {
+ memcpy(nbuf, buf, *len * esize);
+ xfree(buf);
+ }
+ *len = nlen;
+ return nbuf;
+}
diff --git a/test/monniaux/BearSSL/tools/verify.c b/test/monniaux/BearSSL/tools/verify.c
new file mode 100644
index 00000000..74055784
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/verify.c
@@ -0,0 +1,353 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "brssl.h"
+#include "bearssl.h"
+
+static unsigned
+rsa_bit_length(const br_rsa_public_key *pk)
+{
+ size_t u;
+ unsigned x, bl;
+
+ for (u = 0; u < pk->nlen; u ++) {
+ if (pk->n[u] != 0) {
+ break;
+ }
+ }
+ if (u == pk->nlen) {
+ return 0;
+ }
+ bl = (unsigned)(pk->nlen - u - 1) << 3;
+ x = pk->n[u];
+ while (x != 0) {
+ bl ++;
+ x >>= 1;
+ }
+ return bl;
+}
+
+static void
+print_rsa(const br_rsa_public_key *pk, int print_text, int print_C)
+{
+ if (print_text) {
+ size_t u;
+
+ printf("n = ");
+ for (u = 0; u < pk->nlen; u ++) {
+ printf("%02X", pk->n[u]);
+ }
+ printf("\n");
+ printf("e = ");
+ for (u = 0; u < pk->elen; u ++) {
+ printf("%02X", pk->e[u]);
+ }
+ printf("\n");
+ }
+ if (print_C) {
+ size_t u;
+
+ printf("\nstatic const unsigned char RSA_N[] = {");
+ for (u = 0; u < pk->nlen; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", pk->n[u]);
+ }
+ printf("\n};\n");
+ printf("\nstatic const unsigned char RSA_E[] = {");
+ for (u = 0; u < pk->elen; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", pk->e[u]);
+ }
+ printf("\n};\n");
+ printf("\nstatic const br_rsa_public_key RSA = {\n");
+ printf("\t(unsigned char *)RSA_N, sizeof RSA_N,\n");
+ printf("\t(unsigned char *)RSA_E, sizeof RSA_E\n");
+ printf("};\n");
+ }
+}
+
+static void
+print_ec(const br_ec_public_key *pk, int print_text, int print_C)
+{
+ if (print_text) {
+ size_t u;
+
+ printf("Q = ");
+ for (u = 0; u < pk->qlen; u ++) {
+ printf("%02X", pk->q[u]);
+ }
+ printf("\n");
+ }
+ if (print_C) {
+ size_t u;
+
+ printf("\nstatic const unsigned char EC_Q[] = {");
+ for (u = 0; u < pk->qlen; u ++) {
+ if (u != 0) {
+ printf(",");
+ }
+ if (u % 12 == 0) {
+ printf("\n\t");
+ } else {
+ printf(" ");
+ }
+ printf("0x%02X", pk->q[u]);
+ }
+ printf("\n};\n");
+ printf("\nstatic const br_ec_public_key EC = {\n");
+ printf("\t%d,\n", pk->curve);
+ printf("\t(unsigned char *)EC_Q, sizeof EC_Q\n");
+ printf("};\n");
+ }
+}
+
+static void
+usage_verify(void)
+{
+ fprintf(stderr,
+"usage: brssl verify [ options ] file...\n");
+ fprintf(stderr,
+"options:\n");
+ fprintf(stderr,
+" -q suppress verbose messages\n");
+ fprintf(stderr,
+" -sni name check presence of a specific server name\n");
+ fprintf(stderr,
+" -CA file add certificates in 'file' to trust anchors\n");
+ fprintf(stderr,
+" -text print public key details (human-readable)\n");
+ fprintf(stderr,
+" -C print public key details (C code)\n");
+}
+
+typedef VECTOR(br_x509_certificate) cert_list;
+
+static void
+free_cert_contents(br_x509_certificate *xc)
+{
+ xfree(xc->data);
+}
+
+/* see brssl.h */
+int
+do_verify(int argc, char *argv[])
+{
+ int retcode;
+ int verbose;
+ int i;
+ const char *sni;
+ anchor_list anchors = VEC_INIT;
+ cert_list chain = VEC_INIT;
+ size_t u;
+ br_x509_minimal_context mc;
+ int err;
+ int print_text, print_C;
+ br_x509_pkey *pk;
+ const br_x509_pkey *tpk;
+ unsigned usages;
+
+ retcode = 0;
+ verbose = 1;
+ sni = NULL;
+ print_text = 0;
+ print_C = 0;
+ pk = NULL;
+ for (i = 0; i < argc; i ++) {
+ const char *arg;
+
+ arg = argv[i];
+ if (arg[0] != '-') {
+ br_x509_certificate *xcs;
+ size_t num;
+
+ xcs = read_certificates(arg, &num);
+ if (xcs == NULL) {
+ usage_verify();
+ goto verify_exit_error;
+ }
+ VEC_ADDMANY(chain, xcs, num);
+ xfree(xcs);
+ continue;
+ }
+ if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
+ verbose = 1;
+ } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
+ verbose = 0;
+ } else if (eqstr(arg, "-sni")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-sni'\n");
+ usage_verify();
+ goto verify_exit_error;
+ }
+ if (sni != NULL) {
+ fprintf(stderr, "ERROR: duplicate SNI\n");
+ usage_verify();
+ goto verify_exit_error;
+ }
+ sni = argv[i];
+ continue;
+ } else if (eqstr(arg, "-CA")) {
+ if (++ i >= argc) {
+ fprintf(stderr,
+ "ERROR: no argument for '-CA'\n");
+ usage_verify();
+ goto verify_exit_error;
+ }
+ arg = argv[i];
+ if (read_trust_anchors(&anchors, arg) == 0) {
+ usage_verify();
+ goto verify_exit_error;
+ }
+ continue;
+ } else if (eqstr(arg, "-text")) {
+ print_text = 1;
+ } else if (eqstr(arg, "-C")) {
+ print_C = 1;
+ } else {
+ fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
+ usage_verify();
+ goto verify_exit_error;
+ }
+ }
+ if (VEC_LEN(chain) == 0) {
+ fprintf(stderr, "ERROR: no certificate chain provided\n");
+ usage_verify();
+ goto verify_exit_error;
+ }
+ br_x509_minimal_init(&mc, &br_sha256_vtable,
+ &VEC_ELT(anchors, 0), VEC_LEN(anchors));
+ br_x509_minimal_set_hash(&mc, br_sha1_ID, &br_sha1_vtable);
+ br_x509_minimal_set_hash(&mc, br_sha224_ID, &br_sha224_vtable);
+ br_x509_minimal_set_hash(&mc, br_sha256_ID, &br_sha256_vtable);
+ br_x509_minimal_set_hash(&mc, br_sha384_ID, &br_sha384_vtable);
+ br_x509_minimal_set_hash(&mc, br_sha512_ID, &br_sha512_vtable);
+ br_x509_minimal_set_rsa(&mc, &br_rsa_i31_pkcs1_vrfy);
+ br_x509_minimal_set_ecdsa(&mc,
+ &br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
+
+ mc.vtable->start_chain(&mc.vtable, sni);
+ for (u = 0; u < VEC_LEN(chain); u ++) {
+ br_x509_certificate *xc;
+
+ xc = &VEC_ELT(chain, u);
+ mc.vtable->start_cert(&mc.vtable, xc->data_len);
+ mc.vtable->append(&mc.vtable, xc->data, xc->data_len);
+ mc.vtable->end_cert(&mc.vtable);
+ }
+ err = mc.vtable->end_chain(&mc.vtable);
+ tpk = mc.vtable->get_pkey(&mc.vtable, &usages);
+ if (tpk != NULL) {
+ pk = xpkeydup(tpk);
+ }
+
+ if (err == 0) {
+ if (verbose) {
+ int hkx;
+
+ fprintf(stderr, "Validation success; usages:");
+ hkx = 0;
+ if (usages & BR_KEYTYPE_KEYX) {
+ fprintf(stderr, " key exchange");
+ hkx = 1;
+ }
+ if (usages & BR_KEYTYPE_SIGN) {
+ if (hkx) {
+ fprintf(stderr, ",");
+ }
+ fprintf(stderr, " signature");
+ }
+ fprintf(stderr, "\n");
+ }
+ } else {
+ if (verbose) {
+ const char *errname, *errmsg;
+
+ fprintf(stderr, "Validation failed, err = %d", err);
+ errname = find_error_name(err, &errmsg);
+ if (errname != NULL) {
+ fprintf(stderr, " (%s): %s\n", errname, errmsg);
+ } else {
+ fprintf(stderr, " (unknown)\n");
+ }
+ }
+ retcode = -1;
+ }
+ if (pk != NULL) {
+ switch (pk->key_type) {
+ case BR_KEYTYPE_RSA:
+ if (verbose) {
+ fprintf(stderr, "Key type: RSA (%u bits)\n",
+ rsa_bit_length(&pk->key.rsa));
+ }
+ print_rsa(&pk->key.rsa, print_text, print_C);
+ break;
+ case BR_KEYTYPE_EC:
+ if (verbose) {
+ fprintf(stderr, "Key type: EC (%s)\n",
+ ec_curve_name(pk->key.ec.curve));
+ }
+ print_ec(&pk->key.ec, print_text, print_C);
+ break;
+ default:
+ if (verbose) {
+ fprintf(stderr, "Unknown key type\n");
+ break;
+ }
+ }
+ }
+
+ /*
+ * Release allocated structures.
+ */
+verify_exit:
+ VEC_CLEAREXT(anchors, &free_ta_contents);
+ VEC_CLEAREXT(chain, &free_cert_contents);
+ xfreepkey(pk);
+ return retcode;
+
+verify_exit_error:
+ retcode = -1;
+ goto verify_exit;
+}
diff --git a/test/monniaux/BearSSL/tools/xmem.c b/test/monniaux/BearSSL/tools/xmem.c
new file mode 100644
index 00000000..66fca89c
--- /dev/null
+++ b/test/monniaux/BearSSL/tools/xmem.c
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "brssl.h"
+
+/* see brssl.h */
+void *
+xmalloc(size_t len)
+{
+ void *buf;
+
+ if (len == 0) {
+ return NULL;
+ }
+ buf = malloc(len);
+ if (buf == NULL) {
+ fprintf(stderr, "ERROR: could not allocate %lu byte(s)\n",
+ (unsigned long)len);
+ exit(EXIT_FAILURE);
+ }
+ return buf;
+}
+
+/* see brssl.h */
+void
+xfree(void *buf)
+{
+ if (buf != NULL) {
+ free(buf);
+ }
+}
+
+/* see brssl.h */
+void *
+xblobdup(const void *src, size_t len)
+{
+ void *buf;
+
+ buf = xmalloc(len);
+ memcpy(buf, src, len);
+ return buf;
+}
+
+/* see brssl.h */
+char *
+xstrdup(const void *src)
+{
+ return xblobdup(src, strlen(src) + 1);
+}
+
+/* see brssl.h */
+br_x509_pkey *
+xpkeydup(const br_x509_pkey *pk)
+{
+ br_x509_pkey *pk2;
+
+ pk2 = xmalloc(sizeof *pk2);
+ pk2->key_type = pk->key_type;
+ switch (pk->key_type) {
+ case BR_KEYTYPE_RSA:
+ pk2->key.rsa.n = xblobdup(pk->key.rsa.n, pk->key.rsa.nlen);
+ pk2->key.rsa.nlen = pk->key.rsa.nlen;
+ pk2->key.rsa.e = xblobdup(pk->key.rsa.e, pk->key.rsa.elen);
+ pk2->key.rsa.elen = pk->key.rsa.elen;
+ break;
+ case BR_KEYTYPE_EC:
+ pk2->key.ec.curve = pk->key.ec.curve;
+ pk2->key.ec.q = xblobdup(pk->key.ec.q, pk->key.ec.qlen);
+ pk2->key.ec.qlen = pk->key.ec.qlen;
+ break;
+ default:
+ fprintf(stderr, "Unknown public key type: %u\n",
+ (unsigned)pk->key_type);
+ exit(EXIT_FAILURE);
+ }
+ return pk2;
+}
+
+/* see brssl.h */
+void
+xfreepkey(br_x509_pkey *pk)
+{
+ if (pk != NULL) {
+ switch (pk->key_type) {
+ case BR_KEYTYPE_RSA:
+ xfree(pk->key.rsa.n);
+ xfree(pk->key.rsa.e);
+ break;
+ case BR_KEYTYPE_EC:
+ xfree(pk->key.ec.q);
+ break;
+ default:
+ fprintf(stderr, "Unknown public key type: %u\n",
+ (unsigned)pk->key_type);
+ exit(EXIT_FAILURE);
+ }
+ xfree(pk);
+ }
+}
diff --git a/test/monniaux/Makefile b/test/monniaux/Makefile
new file mode 100644
index 00000000..3bceb4ab
--- /dev/null
+++ b/test/monniaux/Makefile
@@ -0,0 +1,39 @@
+# NOTE: do NOT run this makefile with the -j option
+
+CCOMP?=ccomp
+
+#all: verifier_times.txt oracle_times.txt measures.csv
+all: measures.csv
+
+verifier_times.txt: Asmblockdeps.patch
+ (cd ../../ && make -j20 && make install)
+ patch $(realpath ../../extraction/Asmblockdeps.ml) < $<
+ (cd ../../ && make -j20 && make install); patch -R $(realpath ../../extraction/Asmblockdeps.ml) < $<
+ bash clean_benches.sh
+ bash build_benches.sh $@
+
+oracle_times.txt: PostpassSchedulingOracle.patch
+ (cd ../../ && make -j20 && make install)
+ patch $(realpath ../../kvx/PostpassSchedulingOracle.ml) < $<
+ (cd ../../ && make -j20 && make install); patch -R $(realpath ../../kvx/PostpassSchedulingOracle.ml) < $<
+ bash clean_benches.sh
+ bash build_benches.sh $@
+
+measures.csv:
+ @echo "Building compcert.."
+ @(cd ../../ && make -s -j20 && make -s install)
+ @echo "Building benches..."
+ @bash build_benches.sh
+ @echo "Benches built. Running benches..."
+ @bash run_benches.sh $@
+
+#compile_times.pdf: gencompile.py verifier_times.txt oracle_times.txt
+# python3.5 $^ $@
+#
+#measure_times.kvx.pdf: gengraphs.py measures.csv
+# python3.5 $^ $(basename $(basename $@))
+
+.PHONY:
+clean:
+ @bash clean_benches.sh
+ rm -f verifier_times.txt oracle_times.txt compile_times.pdf measure_times.kvx.pdf measures.csv
diff --git a/test/monniaux/PostpassSchedulingOracle.patch b/test/monniaux/PostpassSchedulingOracle.patch
new file mode 100644
index 00000000..11a36c1b
--- /dev/null
+++ b/test/monniaux/PostpassSchedulingOracle.patch
@@ -0,0 +1,33 @@
+diff --git a/kvx/PostpassSchedulingOracle.ml b/kvx/PostpassSchedulingOracle.ml
+index 2fc561e..d3748e8 100644
+--- a/kvx/PostpassSchedulingOracle.ml
++++ b/kvx/PostpassSchedulingOracle.ml
+@@ -808,7 +808,7 @@ let print_bb oc bb =
+ let asm_instructions = Asm.unfold_bblock bb
+ in List.iter (print_inst oc) asm_instructions
+
+-let do_schedule bb =
++let real_do_schedule bb =
+ let problem = build_problem bb
+ in let solution = (if !Clflags.option_fpostpass_sched = "ilp" then
+ validated_scheduler cascaded_scheduler
+@@ -831,6 +831,19 @@ let do_schedule bb =
+ end;
+ bundles)
+
++let do_schedule bb =
++ let nb_instructions = Camlcoq.Z.to_int64 @@ Asmvliw.size bb
++ in let start_time = (Gc.major(); (Unix.times ()).Unix.tms_utime)
++ in let sched = real_do_schedule bb
++ in let refer = ref sched
++ in begin
++ for i = 1 to 1000-1 do
++ refer := (if i > 0 then real_do_schedule bb else real_do_schedule bb);
++ done;
++ Printf.printf "%Ld: %f\n" nb_instructions ((Unix.times ()).Unix.tms_utime -. start_time);
++ sched
++ end
++
+ (**
+ * Dumb schedule if the above doesn't work
+ *)
diff --git a/test/monniaux/README.md b/test/monniaux/README.md
new file mode 100644
index 00000000..c4978465
--- /dev/null
+++ b/test/monniaux/README.md
@@ -0,0 +1,107 @@
+# Benchmarking `CompCert` and GCC
+
+## Compiling `CompCert`
+
+The first step to benchmark `CompCert` is to compile it - the `INSTALL.md` instructions of the project root folder should guide you on installing it.
+
+For the benchmarks to work, the compiler `ccomp` should be on your `$PATH`, with the runtime libraries installed correctly (with a successful `make install` on the project root directory).
+
+## Using the harness
+
+`rules.mk` contains generic rules to compile with `gcc` and `ccomp`, with different optimizations, and producing different binaries. It also produces a `measures.csv` file containing the different timings given by the bench.
+
+Up to 5 different sets of optimizations per compiler can be used.
+
+To use this `rules.mk`, create a folder, put inside all the .c/.h source files, and write a Makefile resembling:
+```make
+TARGET=float_mat
+MEASURES="c1 c2 c3 c4 c5 c6 c7 c8"
+
+include ../rules.mk
+```
+
+This is all that is required to write, the `rules.mk` handles everything.
+
+There is the possibility to define some variables to fine tune what you want. For instance, `ALL_CFILES` describes the .c source files whose objects are to be linked.
+
+Here is an exhaustive list of the variables:
+- `TARGET`: name of the binary to produce
+- `MEASURES`: list of the different timings. This supposes that the program
+prints something of the form `c3 cycles: 44131`.
+- `ALL_CFILES`: list of .c files to compile. By default, `$(wildcard *.c)`
+- `CLOCK`: `basename` of the clock file to compile. Default `../clock`
+- `ALL_CFLAGS`: `cflags` that are to be included for all compilers
+- `ALL_GCCFLAGS`: same, but GCC specific
+- `ALL_CCOMPFLAGS`: same, but `ccomp` specific
+- `KVX_CC`: GCC compiler (default `k1-cos-gcc`)
+- `KVX_CCOMP`: `CompCert` compiler (default `ccomp`)
+- `EXECUTE_CYCLES`: running command (default is `k1-cluster --syscall=libstd_scalls.so --cycle-based --`)
+- `EXECUTE_ARGS`: execution arguments. You can use a macro `__BASE__` which expands to the name of the binary being executed.
+- `GCCiFLAGS` with `i` from 0 to 4: the wanted optimizations. If one of these flags is empty, nothing is done. Same for `CCOMPiFLAGS`. Look at `rules.mk` to see the default values. You might find something like this:
+
+ # You can define up to GCC4FLAGS and CCOMP4FLAGS
+ GCC0FLAGS?=
+ GCC1FLAGS?=$(ALL_GCCFLAGS) -O1
+ GCC2FLAGS?=$(ALL_GCCFLAGS) -O2
+ GCC3FLAGS?=$(ALL_GCCFLAGS) -O3
+ GCC4FLAGS?=
+ CCOMP0FLAGS?=
+ CCOMP1FLAGS?=$(ALL_CCOMPFLAGS) -fno-postpass
+ CCOMP2FLAGS?=$(ALL_CCOMPFLAGS)
+ CCOMP3FLAGS?=
+ CCOMP4FLAGS?=
+
+ # Prefix names
+ GCC0PREFIX?=
+ GCC1PREFIX?=.gcc.o1
+ GCC2PREFIX?=.gcc.o2
+ GCC3PREFIX?=.gcc.o3
+ GCC4PREFIX?=
+ CCOMP0PREFIX?=
+ CCOMP1PREFIX?=.ccomp.o1
+ CCOMP2PREFIX?=.ccomp.o2
+ CCOMP3PREFIX?=
+ CCOMP4PREFIX?=
+
+The `PREFIX` are the prefixes to add to the secondary produced files (assembly, object, executable, ..). You should be careful that if a `FLAGS` is set, then the according `PREFIX` should be set as well.
+
+Assembly files are generated in `asm/`, objects in `obj/`, binaries in `bin/` and outputs in `out/`.
+
+To compile and execute all the benches : `make` while in the `monniaux` directory (without any `-j` flag). Doing so will compile CompCert, install it, and then proceed to execute each bench.
+
+To compile and/or execute a single bench, `cd` to the bench directory, then:
+- `make` for compiling the bench
+- `make run` for running it
+
+You can use `-j` flag when in a single bench directory.
+
+## Individual scripts
+
+If you want to run the building and running scripts individually without having to use the `Makefile` from `test/monniaux`, you can run the `build_benches.sh` script which builds each bench using all the available cores on your machine.
+
+Once the benches are built, you can then run `run_benches.sh file.csv` where `file.csv` is where you want to store the timings of the benchmarks. `run_benches.sh` also uses all the available cores of your machine.
+
+## Adding timings to a benchmark
+
+If you just add a benchmark without any timing function, the resulting `measures.csv` file will be empty for lack of timing output.
+
+To add a timing, you must use the functions whose prototypes are in `clock.h`
+
+ #include "../clock.h"
+ /* ... */
+ clock_prepare();
+ /* ... */
+ clock_start();
+ /* .. computations .. */
+ clock_stop();
+ /* ... */
+ print_total_clock(); // print to stdout
+ printerr_total_clock(); // print to stderr
+
+If the benchmark doesn't use `stdout` in a binary way you can use `print_total_clock()`. However, some benchmarks like `jpeg-6b` print their binary content to `stdout`, which then messes up the `grep` command when attempting to use it to extract the cycles from `stdout`.
+
+The solution is then to use `printerr_total_clock()` which will print the cycles to `stderr`, and use `EXECUTE_ARGS` ressembling this:
+
+ EXECUTE_ARGS=-dct int -outfile __BASE__.jpg testimg.ppm 2> __BASE__.out
+
+`__BASE__` is a macro that gets expanded to the base name - that is, the `TARGET` concatenated with one of the `GCCiPREFIX` or `CCOMPiPREFIX`. For instance, in `jpeg-6b`, `__BASE__` could be `jpeg-6b.ccomp.o2`.
diff --git a/test/monniaux/acswap/test_swapd.c b/test/monniaux/acswap/test_swapd.c
new file mode 100644
index 00000000..02dd8b06
--- /dev/null
+++ b/test/monniaux/acswap/test_swapd.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int main() {
+ unsigned long long loc=10, next=12, current=11;
+ union {
+ __int128 i128;
+ struct {
+ unsigned long low, high;
+ } i64_2;
+ } ret;
+ ret.i128 = __builtin_kvx_acswapd(&loc, next, current);
+ printf("%lx %lx\n", ret.i64_2.low, ret.i64_2.high);
+}
diff --git a/test/monniaux/acswap/test_swapw.c b/test/monniaux/acswap/test_swapw.c
new file mode 100644
index 00000000..6fb7d1cd
--- /dev/null
+++ b/test/monniaux/acswap/test_swapw.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int main() {
+ unsigned loc=11, next=12, current=11;
+ union {
+ __int128 i128;
+ struct {
+ unsigned long low, high;
+ } i64_2;
+ } ret;
+ ret.i128 = __builtin_kvx_acswapw(&loc, next, current);
+ printf("%lx %lx\n", ret.i64_2.low, ret.i64_2.high);
+}
diff --git a/test/monniaux/benches.sh b/test/monniaux/benches.sh
new file mode 100644
index 00000000..434e1b15
--- /dev/null
+++ b/test/monniaux/benches.sh
@@ -0,0 +1,3 @@
+benches="binary_search bitsliced-aes bitsliced-tea complex float_mat glibc_qsort heapsort idea number_theoretic_transform quicksort sha-2 tacle-bench-lift tacle-bench-powerwindow too_slow heptagon_radio_transmitter lustrev4_lustrec_heater_control lustrev4_lv4_heater_control lustrev4_lv6-en-2cgc_heater_control lustrev6-convertible-en-2cgc xor_and_mat glpk-4.65 picosat-965 genann jpeg-6b zlib-1.2.11 ocaml tiff-4.0.10 ncompress"
+
+# Removed for now : ternary
diff --git a/test/monniaux/binary_search/Makefile b/test/monniaux/binary_search/Makefile
new file mode 100644
index 00000000..924f4217
--- /dev/null
+++ b/test/monniaux/binary_search/Makefile
@@ -0,0 +1,4 @@
+TARGET=binary_search
+MEASURES="randomfill search1 search2 search3 search4"
+
+include ../rules.mk
diff --git a/test/monniaux/binary_search/binary_search.c b/test/monniaux/binary_search/binary_search.c
new file mode 100644
index 00000000..f16d15b8
--- /dev/null
+++ b/test/monniaux/binary_search/binary_search.c
@@ -0,0 +1,130 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "../clock.h"
+
+typedef int data;
+typedef unsigned index;
+
+/* from Rosetta code */
+int my_bsearch (data *a, index n, data x) {
+ index i = 0, j = n - 1;
+ while (i <= j) {
+ index k = (i + j) / 2;
+ if (a[k] == x) {
+ return k;
+ }
+ else if (a[k] < x) {
+ i = k + 1;
+ }
+ else {
+ j = k - 1;
+ }
+ }
+ return -1;
+}
+
+int my_bsearch2 (data *a, index n, data x) {
+ index i = 0, j = n - 1;
+ while (i <= j) {
+ index k = (i + j) / 2;
+ index kp1 = k+1, km1 = k-1;
+ data ak = a[k];
+ i = ak < x ? kp1 : i;
+ j = ak > x ? km1 : j;
+ if (ak == x) {
+ return k;
+ }
+ }
+ return -1;
+}
+
+int my_bsearch3 (data *a, index n, data x) {
+ index i = 0, j = n - 1;
+ while (i <= j) {
+ index k = (i + j) / 2;
+ index kp1 = k+1, km1 = k-1;
+ data ak = a[k];
+ _Bool lt = ak < x, gt = ak > x;
+ i = lt ? kp1 : i;
+ j = gt ? km1 : j;
+ if (ak == x) {
+ return k;
+ }
+ }
+ return -1;
+}
+
+int my_bsearch4 (data *a, index n, data x) {
+ index i = 0, j = n - 1, k;
+ k = (i + j) / 2;
+ while (i <= j) {
+ index kp1 = k+1, km1 = k-1;
+ data ak = a[k];
+ _Bool lt = ak < x, gt = ak > x;
+ i = lt ? kp1 : i;
+ j = gt ? km1 : j;
+ if (ak == x) {
+ goto end;
+ }
+ k = (i + j) / 2;
+ }
+ k=-1;
+ end:
+ return k;
+}
+
+void random_ascending_fill(data *a, index n) {
+ unsigned r = 41;
+ data v = 0;
+ for(index i=0; i<n; i++) {
+ a[i] = v;
+ v++;
+ v = (r & 0x40000000) ? (v+1) : v;
+ r = r * 97 + 5;
+ }
+}
+
+int main () {
+ index n=25000;
+ data v=1502;
+ data *buf=malloc(n*sizeof(data));
+
+ cycle_t timestamp0 = get_current_cycle();
+ random_ascending_fill(buf, n);
+ timestamp0 = get_current_cycle()-timestamp0;
+
+ my_bsearch(buf, n, v);
+ cycle_t timestamp1 = get_current_cycle();
+ index pos1 = my_bsearch(buf, n, v);
+ timestamp1 = get_current_cycle()-timestamp1;
+
+ my_bsearch2(buf, n, v);
+ cycle_t timestamp2 = get_current_cycle();
+ index pos2 = my_bsearch2(buf, n, v);
+ timestamp2 = get_current_cycle()-timestamp2;
+
+ my_bsearch3(buf, n, v);
+ cycle_t timestamp3 = get_current_cycle();
+ index pos3 = my_bsearch3(buf, n, v);
+ timestamp3 = get_current_cycle()-timestamp3;
+
+ my_bsearch4(buf, n, v);
+ cycle_t timestamp4 = get_current_cycle();
+ index pos4 = my_bsearch4(buf, n, v);
+ timestamp4 = get_current_cycle()-timestamp4;
+
+ printf("position1: %d\n"
+ "position2: %d\n"
+ "position3: %d\n"
+ "position4: %d\n"
+ "randomfill cycles: %" PRIu64 "\n"
+ "search1 cycles: %" PRIu64 "\n"
+ "search2 cycles: %" PRIu64 "\n"
+ "search3 cycles: %" PRIu64 "\n"
+ "search4 cycles: %" PRIu64 "\n",
+ pos1, pos2, pos3, pos4,
+ timestamp0, timestamp1, timestamp2, timestamp3, timestamp4);
+
+ free(buf);
+}
diff --git a/test/monniaux/bitfields/bitfields.c b/test/monniaux/bitfields/bitfields.c
new file mode 100644
index 00000000..16ad5a61
--- /dev/null
+++ b/test/monniaux/bitfields/bitfields.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+
+struct fields {
+ unsigned f0 : 3;
+ unsigned f1 : 5;
+ signed f2 : 3;
+ unsigned toto1: 16;
+ unsigned toto2: 16;
+};
+
+unsigned get_toto1(struct fields x) {
+ return x.toto1;
+}
+
+unsigned get_toto2(struct fields x) {
+ return x.toto2;
+}
+
+int get_f1(struct fields x) {
+ return x.f1;
+}
+
+int get_f2(struct fields x) {
+ return x.f2;
+}
+
+void set_f1(struct fields *x, unsigned v) {
+ x->f1 = v;
+}
+
+int main() {
+ struct fields x = {1, 2, -1};
+ printf("%d %d\n", get_f1(x), get_f2(x));
+ set_f1(&x, 4);
+ printf("%d %d\n", get_f1(x), get_f2(x));
+}
diff --git a/test/monniaux/bitfields/bitfields_long.c b/test/monniaux/bitfields/bitfields_long.c
new file mode 100644
index 00000000..93bba8d0
--- /dev/null
+++ b/test/monniaux/bitfields/bitfields_long.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#define GET_FIELD_L(x, stop, start) (((x) << (63-(stop))) >> (63-(stop)+(start)))
+#define FIELD_MASK(stop, start) ((1ULL<<(stop)) - (1ULL<<(start)) + (1ULL<<(stop)))
+
+#define SET_FIELD_L(x, stop, start, y) (((x) & ~FIELD_MASK(stop, start)) | ((y << start) & FIELD_MASK(stop, start)))
+
+uint64_t get_f2(uint64_t x) {
+ return GET_FIELD_L(x, 47, 16);
+}
+
+uint64_t set_f2(uint64_t x, uint64_t y) {
+ return SET_FIELD_L(x, 47, 16, y);
+}
+
+int main() {
+ printf("%lx %lx\n", FIELD_MASK(47, 16), set_f2(0x12345678ABCD1234ULL, 0x11112222ULL));
+}
diff --git a/test/monniaux/bitsliced-aes/Makefile b/test/monniaux/bitsliced-aes/Makefile
new file mode 100644
index 00000000..0fef17be
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/Makefile
@@ -0,0 +1,4 @@
+ALL_CFILES=$(wildcard *.c) tests/tests.c
+TARGET=bitsliced-aes
+
+include ../rules.mk
diff --git a/test/monniaux/bitsliced-aes/README.md b/test/monniaux/bitsliced-aes/README.md
new file mode 100644
index 00000000..eb35b2af
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/README.md
@@ -0,0 +1,59 @@
+Bitsliced AES
+-------------
+
+Bitslicing is a technique to compute steps in an algorithm 1 bit at a time. Each bit in a processor word would be a part
+of a different data stream for that particular algorithm. It is attractive because then it can run many different streams
+in parallel (depending on the word length). E.g. a 32 bit word length can compute 32 different streams in parallel.
+
+Some encryption algorithms allow blocks to be operated on in parallel like AES CTR. So if there is enough input, say 32 blocks,
+then a 32 bit processor can achieve full utilitization by filling all bits in word and enciphering all blocks in parallel.
+
+
+Research
+--------
+
+
+Bitslicing is not a new idea. It was first done on DES by Eli Biham in 1997 as described by his paper "A Fast New DES Implementation in Software".
+Since then many implementations have been done on AES and other encryption algorithms. However none are either pubicly available or easily portable.
+
+
+This bitsliced implementation
+-----------------------------
+
+This work exists for education and research. This repository exists as a reference for people working on bitslicing.
+It is written entirely in C.
+
+Performance measurements done for AES-CTR on a 64 bit 4 GHz Intel 4790 and compiled with GCC 4.8.4.
+
+| |footprint | throughput |
+|----------------------|----------|------------|
+|Performance optimized | 12,150 bytes | 51 cycles/byte |
+|Footprint optimized | 8,526 bytes | 81 cycles/byte |
+
+Performance could be improved by about 5-10x by writting in assembly and ensuring
+more operations stay in registers rather then spill to memory.
+
+
+Compiling
+----------
+
+Compile the benchmarking program by running:
+
+```bash
+make
+```
+
+Benchmark program requires OpenSSL.
+
+
+Compile the test program by running:
+
+```bash
+make test
+```
+
+Change to the word length of your processor by editing the `WORD_SIZE` macro in bs.h. Optimize for
+footprint by using `-O2` instead of `-O3` in the Makefile and also deleting the `-DUNROLL_TRANSPOSE`
+flag.
+
+
diff --git a/test/monniaux/bitsliced-aes/aes.c b/test/monniaux/bitsliced-aes/aes.c
new file mode 100644
index 00000000..d4474e14
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/aes.c
@@ -0,0 +1,129 @@
+
+#include <string.h>
+#include "aes.h"
+#include "bs.h"
+#include "utils.h"
+
+void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher_rev(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher_rev(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+static void INC_CTR(uint8_t * ctr, uint8_t i)
+{
+ ctr += BLOCK_SIZE/8 - 1;
+ uint8_t n = *(ctr);
+ *ctr += i;
+ while(*ctr < n)
+ {
+ ctr--;
+ n = *ctr;
+ (*ctr)++;
+ }
+}
+
+void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv)
+{
+ word_t rk[11][BLOCK_SIZE];
+ word_t ctr[BLOCK_SIZE];
+ uint8_t iv_copy[BLOCK_SIZE/8];
+
+ memset(outputb,0,size);
+ memset(ctr,0,sizeof(ctr));
+ memmove(iv_copy,iv,BLOCK_SIZE/8);
+
+ word_t * state = (word_t *)outputb;
+ bs_expand_key(rk, key);
+
+ do
+ {
+ int chunk = MIN(size, BS_BLOCK_SIZE);
+ int blocks = chunk / (BLOCK_SIZE/8);
+ if (chunk % (BLOCK_SIZE/8))
+ {
+ blocks++;
+ }
+
+ int i;
+ for (i = 0; i < blocks; i++)
+ {
+ memmove(ctr + (i * WORDS_PER_BLOCK), iv_copy, BLOCK_SIZE/8);
+ INC_CTR(iv_copy,1);
+ }
+
+ bs_cipher(ctr, rk);
+ size -= chunk;
+
+ uint8_t * ctr_p = (uint8_t *) ctr;
+ while(chunk--)
+ {
+ *outputb++ = *ctr_p++ ^ *inputb++;
+ }
+
+ }
+ while(size);
+
+}
+
+
+
+
diff --git a/test/monniaux/bitsliced-aes/aes.h b/test/monniaux/bitsliced-aes/aes.h
new file mode 100644
index 00000000..60e3e5d7
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/aes.h
@@ -0,0 +1,12 @@
+#ifndef _AES_H_
+#define _AES_H_
+
+#include <stdint.h>
+
+void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key);
+void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key);
+
+void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv);
+#define aes_ctr_decrypt(outputb,inputb,size,key,iv) aes_ctr_encrypt(outputb,inputb,size,key,iv)
+
+#endif
diff --git a/test/monniaux/bitsliced-aes/bs.c b/test/monniaux/bitsliced-aes/bs.c
new file mode 100644
index 00000000..a172aca5
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/bs.c
@@ -0,0 +1,1150 @@
+
+#include <string.h>
+#include "bs.h"
+
+/* TEMPORARY */
+#define TERNARY(x, v0, v1) ((x) ? (v1) : (v0))
+
+#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ||\
+ defined(__amd64__) || defined(__amd32__)|| defined(__amd16__)
+#define bs2le(x) (x)
+#define bs2be(x) (x)
+#elif (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ||\
+ (defined(__sparc__))
+#define bs2le(x) __builtin_bswap_wordsize(x)
+#define bs2be(x) __builtin_bswap_wordsize(x)
+#else
+#error "endianness not supported"
+#endif
+
+void bs_addroundkey(word_t * B, word_t * rk)
+{
+ int i;
+ for (i = 0; i < BLOCK_SIZE; i++)
+ B[i] ^= rk[i];
+}
+
+void bs_apply_sbox(word_t * input)
+{
+ int i;
+ for(i=0; i < BLOCK_SIZE; i+=8)
+ {
+ bs_sbox(input+i);
+ }
+}
+
+void bs_apply_sbox_rev(word_t * input)
+{
+ int i;
+ for(i=0; i < BLOCK_SIZE; i+=8)
+ {
+ bs_sbox_rev(input+i);
+ }
+}
+
+/*July 2011*/
+/*Straight-line program for AES s box*/
+
+/*Input is U[0], U[1],...,U[7]*/
+/*Output is S[0], S[1],...,S[7]*/
+// http://cs-www.cs.yale.edu/homes/peralta/CircuitStuff/CMT.html
+void bs_sbox_rev(word_t U[8])
+{
+ word_t W[8];
+ word_t
+ T1,T2,T3,T4,T5,T6,T8,
+ T9,T10,T13,T14,T15,T16,
+ T17,T18,T19,T20,T22,T23,T24,
+ T25, T26, T27;
+
+ word_t
+ M1,M2,M3,M4,M5,M6,M7,M8,
+ M9,M10,M11,M12,M13,M14,M15,
+ M16,M17,M18,M19,M20,M21,M22,
+ M23,M24,M25,M26,M27,M28,M29,
+ M30,M31,M32,M33,M34,M35,M36,
+ M37,M38,M39,M40,M41,M42,M43,
+ M44,M45,M46,M47,M48,M49,M50,
+ M51,M52,M53,M54,M55,M56,M57,
+ M58,M59,M60,M61,M62,M63;
+
+ word_t
+ P0,P1,P2,P3,P4,P5,P6,P7,P8,
+ P9,P10,P11,P12,P13,P14,
+ P15,P16,P17,P18,P19,P20,
+ P21,P22,P23,P24,P25,P26,
+ P27,P28,P29;
+
+ word_t Y5,
+ R5, R13, R17, R18, R19;
+
+
+ T23 = U[7] ^ U[4];
+ T22 = ~(U[6] ^ U[4]);
+ T2 = ~(U[7] ^ U[6]);
+ T1 = U[4] ^ U[3];
+ T24 = ~(U[3] ^ U[0]);
+ R5 = U[1] ^ U[0];
+ T8 = ~(U[6] ^ T23);
+ T19 = T22 ^ R5;
+ T9 = ~(U[0] ^ T1);
+ T10 = T2 ^ T24;
+ T13 = T2 ^ R5;
+ T3 = T1 ^ R5;
+ T25 = ~(U[5] ^ T1);
+ R13 = U[6] ^ U[1];
+ T17 = ~(U[5] ^ T19);
+ T20 = T24 ^ R13;
+ T4 = U[3] ^ T8;
+ R17 = ~(U[5] ^ U[2]);
+ R18 = ~(U[2] ^ U[1]);
+ R19 = ~(U[5] ^ U[3]);
+ Y5 = U[7] ^ R17;
+ T6 = T22 ^ R17;
+ T16 = R13 ^ R19;
+ T27 = T1 ^ R18;
+ T15 = T10 ^ T27;
+ T14 = T10 ^ R18;
+ T26 = T3 ^ T16;
+ M1 = T13 & T6;
+ M2 = T23 & T8;
+ M3 = T14 ^ M1;
+ M4 = T19 & Y5;
+ M5 = M4 ^ M1;
+ M6 = T3 & T16;
+ M7 = T22 & T9;
+ M8 = T26 ^ M6;
+ M9 = T20 & T17;
+ M10 = M9 ^ M6;
+ M11 = T1 & T15;
+ M12 = T4 & T27;
+ M13 = M12 ^ M11;
+ M14 = T2 & T10;
+ M15 = M14 ^ M11;
+ M16 = M3 ^ M2;
+ M17 = M5 ^ T24;
+ M18 = M8 ^ M7;
+ M19 = M10 ^ M15;
+ M20 = M16 ^ M13;
+ M21 = M17 ^ M15;
+ M22 = M18 ^ M13;
+ M23 = M19 ^ T25;
+ M24 = M22 ^ M23;
+ M25 = M22 & M20;
+ M26 = M21 ^ M25;
+ M27 = M20 ^ M21;
+ M28 = M23 ^ M25;
+ M29 = M28 & M27;
+ M30 = M26 & M24;
+ M31 = M20 & M23;
+ M32 = M27 & M31;
+ M33 = M27 ^ M25;
+ M34 = M21 & M22;
+ M35 = M24 & M34;
+ M36 = M24 ^ M25;
+ M37 = M21 ^ M29;
+ M38 = M32 ^ M33;
+ M39 = M23 ^ M30;
+ M40 = M35 ^ M36;
+ M41 = M38 ^ M40;
+ M42 = M37 ^ M39;
+ M43 = M37 ^ M38;
+ M44 = M39 ^ M40;
+ M45 = M42 ^ M41;
+ M46 = M44 & T6;
+ M47 = M40 & T8;
+ M48 = M39 & Y5;
+ M49 = M43 & T16;
+ M50 = M38 & T9;
+ M51 = M37 & T17;
+ M52 = M42 & T15;
+ M53 = M45 & T27;
+ M54 = M41 & T10;
+ M55 = M44 & T13;
+ M56 = M40 & T23;
+ M57 = M39 & T19;
+ M58 = M43 & T3;
+ M59 = M38 & T22;
+ M60 = M37 & T20;
+ M61 = M42 & T1;
+ M62 = M45 & T4;
+ M63 = M41 & T2;
+ P0 = M52 ^ M61;
+ P1 = M58 ^ M59;
+ P2 = M54 ^ M62;
+ P3 = M47 ^ M50;
+ P4 = M48 ^ M56;
+ P5 = M46 ^ M51;
+ P6 = M49 ^ M60;
+ P7 = P0 ^ P1;
+ P8 = M50 ^ M53;
+ P9 = M55 ^ M63;
+ P10 = M57 ^ P4;
+ P11 = P0 ^ P3;
+ P12 = M46 ^ M48;
+ P13 = M49 ^ M51;
+ P14 = M49 ^ M62;
+ P15 = M54 ^ M59;
+ P16 = M57 ^ M61;
+ P17 = M58 ^ P2;
+ P18 = M63 ^ P5;
+ P19 = P2 ^ P3;
+ P20 = P4 ^ P6;
+ P22 = P2 ^ P7;
+ P23 = P7 ^ P8;
+ P24 = P5 ^ P7;
+ P25 = P6 ^ P10;
+ P26 = P9 ^ P11;
+ P27 = P10 ^ P18;
+ P28 = P11 ^ P25;
+ P29 = P15 ^ P20;
+ W[7] = P13 ^ P22;
+ W[6] = P26 ^ P29;
+ W[5] = P17 ^ P28;
+ W[4] = P12 ^ P22;
+ W[3] = P23 ^ P27;
+ W[2] = P19 ^ P24;
+ W[1] = P14 ^ P23;
+ W[0] = P9 ^ P16;
+
+ memmove(U,W,sizeof(W));
+}
+
+void bs_sbox(word_t U[8])
+{
+ word_t S[8];
+ word_t
+ T1,T2,T3,T4,T5,T6,T7,T8,
+ T9,T10,T11,T12,T13,T14,T15,T16,
+ T17,T18,T19,T20,T21,T22,T23,T24,
+ T25, T26, T27;
+
+ word_t
+ M1,M2,M3,M4,M5,M6,M7,M8,
+ M9,M10,M11,M12,M13,M14,M15,
+ M16,M17,M18,M19,M20,M21,M22,
+ M23,M24,M25,M26,M27,M28,M29,
+ M30,M31,M32,M33,M34,M35,M36,
+ M37,M38,M39,M40,M41,M42,M43,
+ M44,M45,M46,M47,M48,M49,M50,
+ M51,M52,M53,M54,M55,M56,M57,
+ M58,M59,M60,M61,M62,M63;
+
+ word_t
+ L0,L1,L2,L3,L4,L5,L6,L7,L8,
+ L9,L10,L11,L12,L13,L14,
+ L15,L16,L17,L18,L19,L20,
+ L21,L22,L23,L24,L25,L26,
+ L27,L28,L29;
+
+ T1 = U[7] ^ U[4];
+ T2 = U[7] ^ U[2];
+ T3 = U[7] ^ U[1];
+ T4 = U[4] ^ U[2];
+ T5 = U[3] ^ U[1];
+ T6 = T1 ^ T5;
+ T7 = U[6] ^ U[5];
+ T8 = U[0] ^ T6;
+ T9 = U[0] ^ T7;
+ T10 = T6 ^ T7;
+ T11 = U[6] ^ U[2];
+ T12 = U[5] ^ U[2];
+ T13 = T3 ^ T4;
+ T14 = T6 ^ T11;
+ T15 = T5 ^ T11;
+ T16 = T5 ^ T12;
+ T17 = T9 ^ T16;
+ T18 = U[4] ^ U[0];
+ T19 = T7 ^ T18;
+ T20 = T1 ^ T19;
+ T21 = U[1] ^ U[0];
+ T22 = T7 ^ T21;
+ T23 = T2 ^ T22;
+ T24 = T2 ^ T10;
+ T25 = T20 ^ T17;
+ T26 = T3 ^ T16;
+ T27 = T1 ^ T12;
+ M1 = T13 & T6;
+ M2 = T23 & T8;
+ M3 = T14 ^ M1;
+ M4 = T19 & U[0];
+ M5 = M4 ^ M1;
+ M6 = T3 & T16;
+ M7 = T22 & T9;
+ M8 = T26 ^ M6;
+ M9 = T20 & T17;
+ M10 = M9 ^ M6;
+ M11 = T1 & T15;
+ M12 = T4 & T27;
+ M13 = M12 ^ M11;
+ M14 = T2 & T10;
+ M15 = M14 ^ M11;
+ M16 = M3 ^ M2;
+ M17 = M5 ^ T24;
+ M18 = M8 ^ M7;
+ M19 = M10 ^ M15;
+ M20 = M16 ^ M13;
+ M21 = M17 ^ M15;
+ M22 = M18 ^ M13;
+ M23 = M19 ^ T25;
+ M24 = M22 ^ M23;
+ M25 = M22 & M20;
+ M26 = M21 ^ M25;
+ M27 = M20 ^ M21;
+ M28 = M23 ^ M25;
+ M29 = M28 & M27;
+ M30 = M26 & M24;
+ M31 = M20 & M23;
+ M32 = M27 & M31;
+ M33 = M27 ^ M25;
+ M34 = M21 & M22;
+ M35 = M24 & M34;
+ M36 = M24 ^ M25;
+ M37 = M21 ^ M29;
+ M38 = M32 ^ M33;
+ M39 = M23 ^ M30;
+ M40 = M35 ^ M36;
+ M41 = M38 ^ M40;
+ M42 = M37 ^ M39;
+ M43 = M37 ^ M38;
+ M44 = M39 ^ M40;
+ M45 = M42 ^ M41;
+ M46 = M44 & T6;
+ M47 = M40 & T8;
+ M48 = M39 & U[0];
+ M49 = M43 & T16;
+ M50 = M38 & T9;
+ M51 = M37 & T17;
+ M52 = M42 & T15;
+ M53 = M45 & T27;
+ M54 = M41 & T10;
+ M55 = M44 & T13;
+ M56 = M40 & T23;
+ M57 = M39 & T19;
+ M58 = M43 & T3;
+ M59 = M38 & T22;
+ M60 = M37 & T20;
+ M61 = M42 & T1;
+ M62 = M45 & T4;
+ M63 = M41 & T2;
+ L0 = M61 ^ M62;
+ L1 = M50 ^ M56;
+ L2 = M46 ^ M48;
+ L3 = M47 ^ M55;
+ L4 = M54 ^ M58;
+ L5 = M49 ^ M61;
+ L6 = M62 ^ L5;
+ L7 = M46 ^ L3;
+ L8 = M51 ^ M59;
+ L9 = M52 ^ M53;
+ L10 = M53 ^ L4;
+ L11 = M60 ^ L2;
+ L12 = M48 ^ M51;
+ L13 = M50 ^ L0;
+ L14 = M52 ^ M61;
+ L15 = M55 ^ L1;
+ L16 = M56 ^ L0;
+ L17 = M57 ^ L1;
+ L18 = M58 ^ L8;
+ L19 = M63 ^ L4;
+ L20 = L0 ^ L1;
+ L21 = L1 ^ L7;
+ L22 = L3 ^ L12;
+ L23 = L18 ^ L2;
+ L24 = L15 ^ L9;
+ L25 = L6 ^ L10;
+ L26 = L7 ^ L9;
+ L27 = L8 ^ L10;
+ L28 = L11 ^ L14;
+ L29 = L11 ^ L17;
+ S[7] = L6 ^ L24;
+ S[6] = ~(L16 ^ L26);
+ S[5] = ~(L19 ^ L28);
+ S[4] = L6 ^ L21;
+ S[3] = L20 ^ L22;
+ S[2] = L25 ^ L29;
+ S[1] = ~(L13 ^ L27);
+ S[0] = ~(L6 ^ L23);
+
+ memmove(U,S,sizeof(S));
+}
+
+void bs_transpose(word_t * blocks)
+{
+ word_t transpose[BLOCK_SIZE];
+ memset(transpose, 0, sizeof(transpose));
+ bs_transpose_dst(transpose,blocks);
+ memmove(blocks,transpose,sizeof(transpose));
+}
+
+void bs_transpose_dst(word_t * transpose, word_t * blocks)
+{
+ int i,k;
+ word_t w;
+ for(k=0; k < WORD_SIZE; k++)
+ {
+ int bitpos = ONE << k;
+ for (i=0; i < WORDS_PER_BLOCK; i++)
+ {
+ w = bs2le(blocks[k * WORDS_PER_BLOCK + i]);
+ int offset = i << MUL_SHIFT;
+
+#ifndef UNROLL_TRANSPOSE
+ int j;
+#ifdef __COMPCERT__
+ word_t *transptr = transpose+offset;
+ word_t bitmask = ONE;
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t old = *transptr;
+ *(transptr++) = TERNARY(w & bitmask, old, old|bitpos);
+ bitmask <<= 1;
+ }
+#else
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ // TODO make const time
+ transpose[offset + j] |= (w & (ONE << j)) ? bitpos : 0;
+ }
+#endif
+#else
+
+ transpose[(offset)+ 0 ] |= (w & (ONE << 0 )) ? (bitpos) : 0;
+ transpose[(offset)+ 1 ] |= (w & (ONE << 1 )) ? (bitpos) : 0;
+ transpose[(offset)+ 2 ] |= (w & (ONE << 2 )) ? (bitpos) : 0;
+ transpose[(offset)+ 3 ] |= (w & (ONE << 3 )) ? (bitpos) : 0;
+ transpose[(offset)+ 4 ] |= (w & (ONE << 4 )) ? (bitpos) : 0;
+ transpose[(offset)+ 5 ] |= (w & (ONE << 5 )) ? (bitpos) : 0;
+ transpose[(offset)+ 6 ] |= (w & (ONE << 6 )) ? (bitpos) : 0;
+ transpose[(offset)+ 7 ] |= (w & (ONE << 7 )) ? (bitpos) : 0;
+#if WORD_SIZE > 8
+ transpose[(offset)+ 8 ] |= (w & (ONE << 8 )) ? (bitpos) : 0;
+ transpose[(offset)+ 9 ] |= (w & (ONE << 9 )) ? (bitpos) : 0;
+ transpose[(offset)+ 10] |= (w & (ONE << 10)) ? (bitpos) : 0;
+ transpose[(offset)+ 11] |= (w & (ONE << 11)) ? (bitpos) : 0;
+ transpose[(offset)+ 12] |= (w & (ONE << 12)) ? (bitpos) : 0;
+ transpose[(offset)+ 13] |= (w & (ONE << 13)) ? (bitpos) : 0;
+ transpose[(offset)+ 14] |= (w & (ONE << 14)) ? (bitpos) : 0;
+ transpose[(offset)+ 15] |= (w & (ONE << 15)) ? (bitpos) : 0;
+#endif
+#if WORD_SIZE > 16
+ transpose[(offset)+ 16] |= (w & (ONE << 16)) ? (bitpos) : 0;
+ transpose[(offset)+ 17] |= (w & (ONE << 17)) ? (bitpos) : 0;
+ transpose[(offset)+ 18] |= (w & (ONE << 18)) ? (bitpos) : 0;
+ transpose[(offset)+ 19] |= (w & (ONE << 19)) ? (bitpos) : 0;
+ transpose[(offset)+ 20] |= (w & (ONE << 20)) ? (bitpos) : 0;
+ transpose[(offset)+ 21] |= (w & (ONE << 21)) ? (bitpos) : 0;
+ transpose[(offset)+ 22] |= (w & (ONE << 22)) ? (bitpos) : 0;
+ transpose[(offset)+ 23] |= (w & (ONE << 23)) ? (bitpos) : 0;
+ transpose[(offset)+ 24] |= (w & (ONE << 24)) ? (bitpos) : 0;
+ transpose[(offset)+ 25] |= (w & (ONE << 25)) ? (bitpos) : 0;
+ transpose[(offset)+ 26] |= (w & (ONE << 26)) ? (bitpos) : 0;
+ transpose[(offset)+ 27] |= (w & (ONE << 27)) ? (bitpos) : 0;
+ transpose[(offset)+ 28] |= (w & (ONE << 28)) ? (bitpos) : 0;
+ transpose[(offset)+ 29] |= (w & (ONE << 29)) ? (bitpos) : 0;
+ transpose[(offset)+ 30] |= (w & (ONE << 30)) ? (bitpos) : 0;
+ transpose[(offset)+ 31] |= (w & (ONE << 31)) ? (bitpos) : 0;
+#endif
+#if WORD_SIZE > 32
+ transpose[(offset)+ 32] |= (w & (ONE << 32)) ? (bitpos) : 0;
+ transpose[(offset)+ 33] |= (w & (ONE << 33)) ? (bitpos) : 0;
+ transpose[(offset)+ 34] |= (w & (ONE << 34)) ? (bitpos) : 0;
+ transpose[(offset)+ 35] |= (w & (ONE << 35)) ? (bitpos) : 0;
+ transpose[(offset)+ 36] |= (w & (ONE << 36)) ? (bitpos) : 0;
+ transpose[(offset)+ 37] |= (w & (ONE << 37)) ? (bitpos) : 0;
+ transpose[(offset)+ 38] |= (w & (ONE << 38)) ? (bitpos) : 0;
+ transpose[(offset)+ 39] |= (w & (ONE << 39)) ? (bitpos) : 0;
+ transpose[(offset)+ 40] |= (w & (ONE << 40)) ? (bitpos) : 0;
+ transpose[(offset)+ 41] |= (w & (ONE << 41)) ? (bitpos) : 0;
+ transpose[(offset)+ 42] |= (w & (ONE << 42)) ? (bitpos) : 0;
+ transpose[(offset)+ 43] |= (w & (ONE << 43)) ? (bitpos) : 0;
+ transpose[(offset)+ 44] |= (w & (ONE << 44)) ? (bitpos) : 0;
+ transpose[(offset)+ 45] |= (w & (ONE << 45)) ? (bitpos) : 0;
+ transpose[(offset)+ 46] |= (w & (ONE << 46)) ? (bitpos) : 0;
+ transpose[(offset)+ 47] |= (w & (ONE << 47)) ? (bitpos) : 0;
+ transpose[(offset)+ 48] |= (w & (ONE << 48)) ? (bitpos) : 0;
+ transpose[(offset)+ 49] |= (w & (ONE << 49)) ? (bitpos) : 0;
+ transpose[(offset)+ 50] |= (w & (ONE << 50)) ? (bitpos) : 0;
+ transpose[(offset)+ 51] |= (w & (ONE << 51)) ? (bitpos) : 0;
+ transpose[(offset)+ 52] |= (w & (ONE << 52)) ? (bitpos) : 0;
+ transpose[(offset)+ 53] |= (w & (ONE << 53)) ? (bitpos) : 0;
+ transpose[(offset)+ 54] |= (w & (ONE << 54)) ? (bitpos) : 0;
+ transpose[(offset)+ 55] |= (w & (ONE << 55)) ? (bitpos) : 0;
+ transpose[(offset)+ 56] |= (w & (ONE << 56)) ? (bitpos) : 0;
+ transpose[(offset)+ 57] |= (w & (ONE << 57)) ? (bitpos) : 0;
+ transpose[(offset)+ 58] |= (w & (ONE << 58)) ? (bitpos) : 0;
+ transpose[(offset)+ 59] |= (w & (ONE << 59)) ? (bitpos) : 0;
+ transpose[(offset)+ 60] |= (w & (ONE << 60)) ? (bitpos) : 0;
+ transpose[(offset)+ 61] |= (w & (ONE << 61)) ? (bitpos) : 0;
+ transpose[(offset)+ 62] |= (w & (ONE << 62)) ? (bitpos) : 0;
+ transpose[(offset)+ 63] |= (w & (ONE << 63)) ? (bitpos) : 0;
+#endif
+#endif
+ // constant time:
+ //transpose[(i<<MUL_SHIFT)+ j] |= (((int64_t)((w & (ONE << j)) << (WORD_SIZE-1-j)))>>(WORD_SIZE-1)) & (ONE<<k);
+ }
+ }
+}
+
+void bs_transpose_rev(word_t * blocks)
+{
+ int i,k;
+ word_t w;
+ word_t transpose[BLOCK_SIZE];
+ memset(transpose, 0, sizeof(transpose));
+ for(k=0; k < BLOCK_SIZE; k++)
+ {
+ w = blocks[k];
+ word_t bitpos = bs2be(ONE << (k % WORD_SIZE));
+ word_t offset = k / WORD_SIZE;
+#ifndef UNROLL_TRANSPOSE
+ int j;
+#ifdef __COMPCERT__
+ word_t *transptr = transpose + offset;
+ word_t bitmask = ONE;
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t old = *transptr;
+ *transptr = TERNARY(w & bitmask, old, old | bitpos);
+ transptr += WORDS_PER_BLOCK;
+ bitmask <<= 1;
+ }
+#else
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t bit = (w & (ONE << j)) ? (ONE << (k % WORD_SIZE)) : 0;
+ transpose[j * WORDS_PER_BLOCK + (offset)] |= bit;
+ }
+#endif
+#else
+ transpose[0 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 0 )) ? bitpos : 0;
+ transpose[1 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 1 )) ? bitpos : 0;
+ transpose[2 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 2 )) ? bitpos : 0;
+ transpose[3 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 3 )) ? bitpos : 0;
+ transpose[4 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 4 )) ? bitpos : 0;
+ transpose[5 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 5 )) ? bitpos : 0;
+ transpose[6 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 6 )) ? bitpos : 0;
+ transpose[7 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 7 )) ? bitpos : 0;
+#if WORD_SIZE > 8
+ transpose[8 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 8 )) ? bitpos : 0;
+ transpose[9 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 9 )) ? bitpos : 0;
+ transpose[10 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 10)) ? bitpos : 0;
+ transpose[11 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 11)) ? bitpos : 0;
+ transpose[12 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 12)) ? bitpos : 0;
+ transpose[13 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 13)) ? bitpos : 0;
+ transpose[14 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 14)) ? bitpos : 0;
+ transpose[15 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 15)) ? bitpos : 0;
+#endif
+#if WORD_SIZE > 16
+ transpose[16 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 16)) ? bitpos : 0;
+ transpose[17 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 17)) ? bitpos : 0;
+ transpose[18 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 18)) ? bitpos : 0;
+ transpose[19 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 19)) ? bitpos : 0;
+ transpose[20 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 20)) ? bitpos : 0;
+ transpose[21 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 21)) ? bitpos : 0;
+ transpose[22 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 22)) ? bitpos : 0;
+ transpose[23 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 23)) ? bitpos : 0;
+ transpose[24 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 24)) ? bitpos : 0;
+ transpose[25 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 25)) ? bitpos : 0;
+ transpose[26 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 26)) ? bitpos : 0;
+ transpose[27 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 27)) ? bitpos : 0;
+ transpose[28 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 28)) ? bitpos : 0;
+ transpose[29 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 29)) ? bitpos : 0;
+ transpose[30 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 30)) ? bitpos : 0;
+ transpose[31 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 31)) ? bitpos : 0;
+#endif
+#if WORD_SIZE > 32
+ transpose[32 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 32)) ? bitpos : 0;
+ transpose[33 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 33)) ? bitpos : 0;
+ transpose[34 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 34)) ? bitpos : 0;
+ transpose[35 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 35)) ? bitpos : 0;
+ transpose[36 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 36)) ? bitpos : 0;
+ transpose[37 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 37)) ? bitpos : 0;
+ transpose[38 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 38)) ? bitpos : 0;
+ transpose[39 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 39)) ? bitpos : 0;
+ transpose[40 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 40)) ? bitpos : 0;
+ transpose[41 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 41)) ? bitpos : 0;
+ transpose[42 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 42)) ? bitpos : 0;
+ transpose[43 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 43)) ? bitpos : 0;
+ transpose[44 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 44)) ? bitpos : 0;
+ transpose[45 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 45)) ? bitpos : 0;
+ transpose[46 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 46)) ? bitpos : 0;
+ transpose[47 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 47)) ? bitpos : 0;
+ transpose[48 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 48)) ? bitpos : 0;
+ transpose[49 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 49)) ? bitpos : 0;
+ transpose[50 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 50)) ? bitpos : 0;
+ transpose[51 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 51)) ? bitpos : 0;
+ transpose[52 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 52)) ? bitpos : 0;
+ transpose[53 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 53)) ? bitpos : 0;
+ transpose[54 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 54)) ? bitpos : 0;
+ transpose[55 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 55)) ? bitpos : 0;
+ transpose[56 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 56)) ? bitpos : 0;
+ transpose[57 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 57)) ? bitpos : 0;
+ transpose[58 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 58)) ? bitpos : 0;
+ transpose[59 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 59)) ? bitpos : 0;
+ transpose[60 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 60)) ? bitpos : 0;
+ transpose[61 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 61)) ? bitpos : 0;
+ transpose[62 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 62)) ? bitpos : 0;
+ transpose[63 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 63)) ? bitpos : 0;
+#endif
+#endif
+ }
+ memmove(blocks,transpose,sizeof(transpose));
+}
+
+
+#define R0 0
+#define R1 8
+#define R2 16
+#define R3 24
+
+#define B0 0
+#define B1 32
+#define B2 64
+#define B3 96
+
+#define R0_shift (BLOCK_SIZE/4)*0
+#define R1_shift (BLOCK_SIZE/4)*1
+#define R2_shift (BLOCK_SIZE/4)*2
+#define R3_shift (BLOCK_SIZE/4)*3
+#define B_MOD (BLOCK_SIZE)
+
+
+void bs_shiftrows(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ word_t * Br0 = B + 0;
+ word_t * Br1 = B + 32;
+ word_t * Br2 = B + 64;
+ word_t * Br3 = B + 96;
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+
+ int i;
+ for(i=0; i<4; i++)
+ {
+ Bp[B0 + 0] = Br0[0];
+ Bp[B0 + 1] = Br0[1];
+ Bp[B0 + 2] = Br0[2];
+ Bp[B0 + 3] = Br0[3];
+ Bp[B0 + 4] = Br0[4];
+ Bp[B0 + 5] = Br0[5];
+ Bp[B0 + 6] = Br0[6];
+ Bp[B0 + 7] = Br0[7];
+ Bp[B1 + 0] = Br1[0];
+ Bp[B1 + 1] = Br1[1];
+ Bp[B1 + 2] = Br1[2];
+ Bp[B1 + 3] = Br1[3];
+ Bp[B1 + 4] = Br1[4];
+ Bp[B1 + 5] = Br1[5];
+ Bp[B1 + 6] = Br1[6];
+ Bp[B1 + 7] = Br1[7];
+ Bp[B2 + 0] = Br2[0];
+ Bp[B2 + 1] = Br2[1];
+ Bp[B2 + 2] = Br2[2];
+ Bp[B2 + 3] = Br2[3];
+ Bp[B2 + 4] = Br2[4];
+ Bp[B2 + 5] = Br2[5];
+ Bp[B2 + 6] = Br2[6];
+ Bp[B2 + 7] = Br2[7];
+ Bp[B3 + 0] = Br3[0];
+ Bp[B3 + 1] = Br3[1];
+ Bp[B3 + 2] = Br3[2];
+ Bp[B3 + 3] = Br3[3];
+ Bp[B3 + 4] = Br3[4];
+ Bp[B3 + 5] = Br3[5];
+ Bp[B3 + 6] = Br3[6];
+ Bp[B3 + 7] = Br3[7];
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+
+ Bp += 8;
+ }
+ memmove(B,Bp_space,sizeof(Bp_space));
+}
+
+
+void bs_shiftrows_rev(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ word_t * Br0 = Bp + 0;
+ word_t * Br1 = Bp + 32;
+ word_t * Br2 = Bp + 64;
+ word_t * Br3 = Bp + 96;
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+
+ int i;
+ for(i=0; i<4; i++)
+ {
+ Br0[0] = B[B0 + 0];
+ Br0[1] = B[B0 + 1];
+ Br0[2] = B[B0 + 2];
+ Br0[3] = B[B0 + 3];
+ Br0[4] = B[B0 + 4];
+ Br0[5] = B[B0 + 5];
+ Br0[6] = B[B0 + 6];
+ Br0[7] = B[B0 + 7];
+ Br1[0] = B[B1 + 0];
+ Br1[1] = B[B1 + 1];
+ Br1[2] = B[B1 + 2];
+ Br1[3] = B[B1 + 3];
+ Br1[4] = B[B1 + 4];
+ Br1[5] = B[B1 + 5];
+ Br1[6] = B[B1 + 6];
+ Br1[7] = B[B1 + 7];
+ Br2[0] = B[B2 + 0];
+ Br2[1] = B[B2 + 1];
+ Br2[2] = B[B2 + 2];
+ Br2[3] = B[B2 + 3];
+ Br2[4] = B[B2 + 4];
+ Br2[5] = B[B2 + 5];
+ Br2[6] = B[B2 + 6];
+ Br2[7] = B[B2 + 7];
+ Br3[0] = B[B3 + 0];
+ Br3[1] = B[B3 + 1];
+ Br3[2] = B[B3 + 2];
+ Br3[3] = B[B3 + 3];
+ Br3[4] = B[B3 + 4];
+ Br3[5] = B[B3 + 5];
+ Br3[6] = B[B3 + 6];
+ Br3[7] = B[B3 + 7];
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = Bp + offsetr0;
+ Br1 = Bp + offsetr1;
+ Br2 = Bp + offsetr2;
+ Br3 = Bp + offsetr3;
+
+ B += 8;
+ }
+ memmove(B - 8 * 4,Bp_space,sizeof(Bp_space));
+}
+
+
+#define A0 0
+#define A1 8
+#define A2 16
+#define A3 24
+
+// Does shift rows and mix columns in same step
+void bs_shiftmix(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+
+ word_t * Br0 = B + 0;
+ word_t * Br1 = B + 32;
+ word_t * Br2 = B + 64;
+ word_t * Br3 = B + 96;
+
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+
+
+ int i;
+ for (i = 0; i < 4; i++)
+ {
+ // B0
+ // 2*A0 2*A1 A1 A2 A3
+ word_t of =Br0[R0+7]^ Br1[R1+7];
+ Bp[A0+0] = Br1[R1+0] ^ Br2[R2+0] ^ Br3[R3+0] ^ of;
+ Bp[A0+1] = Br0[R0+0] ^ Br1[R1+0] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br3[R3+1] ^ of;
+ Bp[A0+2] = Br0[R0+1] ^ Br1[R1+1] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br3[R3+2];
+ Bp[A0+3] = Br0[R0+2] ^ Br1[R1+2] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br3[R3+3] ^ of;
+ Bp[A0+4] = Br0[R0+3] ^ Br1[R1+3] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br3[R3+4] ^ of;
+ Bp[A0+5] = Br0[R0+4] ^ Br1[R1+4] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br3[R3+5];
+ Bp[A0+6] = Br0[R0+5] ^ Br1[R1+5] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br3[R3+6];
+ Bp[A0+7] = Br0[R0+6] ^ Br1[R1+6] ^ Br1[R1+7] ^ Br2[R2+7] ^ Br3[R3+7];
+
+ // A0 2*A1 2*A2 A2 A3
+ of = Br1[R1+7] ^ Br2[R2+7];
+ Bp[A1+0] = Br0[R0+0] ^ Br2[R2+0] ^ Br3[R3+0] ^ of;
+ Bp[A1+1] = Br0[R0+1] ^ Br1[R1+0] ^ Br2[R2+0] ^ Br2[R2+1] ^ Br3[R3+1] ^ of;
+ Bp[A1+2] = Br0[R0+2] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br2[R2+2] ^ Br3[R3+2];
+ Bp[A1+3] = Br0[R0+3] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br2[R2+3] ^ Br3[R3+3] ^ of;
+ Bp[A1+4] = Br0[R0+4] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br2[R2+4] ^ Br3[R3+4] ^ of;
+ Bp[A1+5] = Br0[R0+5] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br2[R2+5] ^ Br3[R3+5];
+ Bp[A1+6] = Br0[R0+6] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br2[R2+6] ^ Br3[R3+6];
+ Bp[A1+7] = Br0[R0+7] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br2[R2+7] ^ Br3[R3+7];
+
+ // A0 A1 2*A2 2*A3 A3
+ of = Br2[R2+7] ^ Br3[R3+7];
+ Bp[A2+0] = Br0[R0+0] ^ Br1[R1+0] ^ Br3[R3+0] ^ of;
+ Bp[A2+1] = Br0[R0+1] ^ Br1[R1+1] ^ Br2[R2+0] ^ Br3[R3+0] ^ Br3[R3+1] ^ of;
+ Bp[A2+2] = Br0[R0+2] ^ Br1[R1+2] ^ Br2[R2+1] ^ Br3[R3+1] ^ Br3[R3+2];
+ Bp[A2+3] = Br0[R0+3] ^ Br1[R1+3] ^ Br2[R2+2] ^ Br3[R3+2] ^ Br3[R3+3] ^ of;
+ Bp[A2+4] = Br0[R0+4] ^ Br1[R1+4] ^ Br2[R2+3] ^ Br3[R3+3] ^ Br3[R3+4] ^ of;
+ Bp[A2+5] = Br0[R0+5] ^ Br1[R1+5] ^ Br2[R2+4] ^ Br3[R3+4] ^ Br3[R3+5];
+ Bp[A2+6] = Br0[R0+6] ^ Br1[R1+6] ^ Br2[R2+5] ^ Br3[R3+5] ^ Br3[R3+6];
+ Bp[A2+7] = Br0[R0+7] ^ Br1[R1+7] ^ Br2[R2+6] ^ Br3[R3+6] ^ Br3[R3+7];
+
+ // A0 2*A0 A1 A2 2*A3
+ of = Br0[R0+7] ^ Br3[R3+7];
+ Bp[A3+0] = Br0[R0+0] ^ Br1[R1+0] ^ Br2[R2+0] ^ of;
+ Bp[A3+1] = Br0[R0+1] ^ Br0[R0+0] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br3[R3+0] ^ of;
+ Bp[A3+2] = Br0[R0+2] ^ Br0[R0+1] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br3[R3+1];
+ Bp[A3+3] = Br0[R0+3] ^ Br0[R0+2] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br3[R3+2] ^ of;
+ Bp[A3+4] = Br0[R0+4] ^ Br0[R0+3] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br3[R3+3] ^ of;
+ Bp[A3+5] = Br0[R0+5] ^ Br0[R0+4] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br3[R3+4];
+ Bp[A3+6] = Br0[R0+6] ^ Br0[R0+5] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br3[R3+5];
+ Bp[A3+7] = Br0[R0+7] ^ Br0[R0+6] ^ Br1[R1+7] ^ Br2[R2+7] ^ Br3[R3+6];
+
+ Bp += BLOCK_SIZE/4;
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+ }
+
+ memmove(B,Bp_space,sizeof(Bp_space));
+}
+
+
+
+void bs_mixcolumns(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ // to understand this, see
+ // https://en.wikipedia.org/wiki/Rijndael_mix_columns
+
+ int i = 0;
+ for (; i < 4; i++)
+ {
+ // of = A0 ^ A1;
+ // A0 = A0 ^ (0x1b & ((signed char)of>>7));
+
+ //// 2 * A0
+ // A0 = A0 ^ (A0 << 1)
+
+ //// + 3 * A1
+ // A0 = A0 ^ (A1)
+ // A0 = A0 ^ (A1<<1)
+
+ //// + A2 + A3
+ // A0 = A0 ^ (A2)
+ // A0 = A0 ^ (A3)
+ // A0.7 A1.7
+ word_t of = B[A0+7] ^ B[A1+7];
+
+ // 2*A0 2*A1 A1 A2 A3
+ Bp[A0+0] = B[A1+0] ^ B[A2+0] ^ B[A3+0] ^ of;
+ Bp[A0+1] = B[A0+0] ^ B[A1+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+1] ^ of;
+ Bp[A0+2] = B[A0+1] ^ B[A1+1] ^ B[A1+2] ^ B[A2+2] ^ B[A3+2];
+ Bp[A0+3] = B[A0+2] ^ B[A1+2] ^ B[A1+3] ^ B[A2+3] ^ B[A3+3] ^ of;
+ Bp[A0+4] = B[A0+3] ^ B[A1+3] ^ B[A1+4] ^ B[A2+4] ^ B[A3+4] ^ of;
+ Bp[A0+5] = B[A0+4] ^ B[A1+4] ^ B[A1+5] ^ B[A2+5] ^ B[A3+5];
+ Bp[A0+6] = B[A0+5] ^ B[A1+5] ^ B[A1+6] ^ B[A2+6] ^ B[A3+6];
+ Bp[A0+7] = B[A0+6] ^ B[A1+6] ^ B[A1+7] ^ B[A2+7] ^ B[A3+7];
+
+
+
+ // of = A1 ^ A2
+ // A1 = A1 ^ (0x1b & ((signed char)of>>7));
+
+ //// A0
+ // A1 = A1 ^ (A0)
+
+ //// + 2 * A1
+ // A1 = A1 ^ (A1 << 1)
+
+ //// + 3 * A2
+ // A1 = A1 ^ (A2)
+ // A1 = A1 ^ (A2<<1)
+
+ //// + A3
+ // A1 = A1 ^ (A3)
+
+ of = B[A1+7] ^ B[A2+7];
+
+ // A0 2*A1 2*A2 A2 A3
+ Bp[A1+0] = B[A0+0] ^ B[A2+0] ^ B[A3+0] ^ of;
+ Bp[A1+1] = B[A0+1] ^ B[A1+0] ^ B[A2+0] ^ B[A2+1] ^ B[A3+1] ^ of;
+ Bp[A1+2] = B[A0+2] ^ B[A1+1] ^ B[A2+1] ^ B[A2+2] ^ B[A3+2];
+ Bp[A1+3] = B[A0+3] ^ B[A1+2] ^ B[A2+2] ^ B[A2+3] ^ B[A3+3] ^ of;
+ Bp[A1+4] = B[A0+4] ^ B[A1+3] ^ B[A2+3] ^ B[A2+4] ^ B[A3+4] ^ of;
+ Bp[A1+5] = B[A0+5] ^ B[A1+4] ^ B[A2+4] ^ B[A2+5] ^ B[A3+5];
+ Bp[A1+6] = B[A0+6] ^ B[A1+5] ^ B[A2+5] ^ B[A2+6] ^ B[A3+6];
+ Bp[A1+7] = B[A0+7] ^ B[A1+6] ^ B[A2+6] ^ B[A2+7] ^ B[A3+7];
+
+
+ // of = A2 ^ A3
+ // A2 = A2 ^ (0x1b & ((signed char)of>>7));
+
+ //// A0 + A1
+ // A2 = A2 ^ (A0)
+ // A2 = A2 ^ (A1)
+
+ //// + 2 * A2
+ // A2 = A2 ^ (A2 << 1)
+
+ //// + 3 * A3
+ // A2 = A2 ^ (A3)
+ // A2 = A2 ^ (A3<<1)
+
+
+ of = B[A2+7] ^ B[A3+7];
+
+ // A0 A1 2*A2 2*A3 A3
+ Bp[A2+0] = B[A0+0] ^ B[A1+0] ^ B[A3+0] ^ of;
+ Bp[A2+1] = B[A0+1] ^ B[A1+1] ^ B[A2+0] ^ B[A3+0] ^ B[A3+1] ^ of;
+ Bp[A2+2] = B[A0+2] ^ B[A1+2] ^ B[A2+1] ^ B[A3+1] ^ B[A3+2];
+ Bp[A2+3] = B[A0+3] ^ B[A1+3] ^ B[A2+2] ^ B[A3+2] ^ B[A3+3] ^ of;
+ Bp[A2+4] = B[A0+4] ^ B[A1+4] ^ B[A2+3] ^ B[A3+3] ^ B[A3+4] ^ of;
+ Bp[A2+5] = B[A0+5] ^ B[A1+5] ^ B[A2+4] ^ B[A3+4] ^ B[A3+5];
+ Bp[A2+6] = B[A0+6] ^ B[A1+6] ^ B[A2+5] ^ B[A3+5] ^ B[A3+6];
+ Bp[A2+7] = B[A0+7] ^ B[A1+7] ^ B[A2+6] ^ B[A3+6] ^ B[A3+7];
+
+
+ // A3 = A0 ^ A3
+ // A3 = A3 ^ (0x1b & ((signed char)of>>7));
+
+ //// 3 * A0
+ // A3 = A3 ^ (A0)
+ // A3 = A3 ^ (A0 << 1)
+
+ //// + A1 + A2
+ // A3 = A3 ^ A1
+ // A3 = A3 ^ A2
+
+ //// + 2 * A3
+ // A3 = A3 ^ (A3<<1)
+
+ of = B[A0+7] ^ B[A3+7];
+
+ // 2*A0 A0 A1 A2 2*A3
+ Bp[A3+0] = B[A0+0] ^ B[A1+0] ^ B[A2+0] ^ of;
+ Bp[A3+1] = B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+0] ^ of;
+ Bp[A3+2] = B[A0+2] ^ B[A0+1] ^ B[A1+2] ^ B[A2+2] ^ B[A3+1];
+ Bp[A3+3] = B[A0+3] ^ B[A0+2] ^ B[A1+3] ^ B[A2+3] ^ B[A3+2] ^ of;
+ Bp[A3+4] = B[A0+4] ^ B[A0+3] ^ B[A1+4] ^ B[A2+4] ^ B[A3+3] ^ of;
+ Bp[A3+5] = B[A0+5] ^ B[A0+4] ^ B[A1+5] ^ B[A2+5] ^ B[A3+4];
+ Bp[A3+6] = B[A0+6] ^ B[A0+5] ^ B[A1+6] ^ B[A2+6] ^ B[A3+5];
+ Bp[A3+7] = B[A0+7] ^ B[A0+6] ^ B[A1+7] ^ B[A2+7] ^ B[A3+6];
+
+
+ //
+ Bp += BLOCK_SIZE/4;
+ B += BLOCK_SIZE/4;
+ }
+
+
+ memmove(B - BLOCK_SIZE,Bp - BLOCK_SIZE,sizeof(Bp_space));
+}
+
+void bs_mixcolumns_rev(word_t * B)
+{
+ // to understand this, see
+ // https://en.wikipedia.org/wiki/Rijndael_mix_columns
+ // TODO combine with shiftrows for performance on decryption
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+
+
+ int i = 0;
+ for (; i < BLOCK_SIZE / 4; i += BLOCK_SIZE / 16)
+ {
+
+ //// state[i][0] = A0*0x0e + A1*0x0b + A2*0x0d + A3*0x09
+ // overflow:
+ /* A0 * 0b1110 */ /* A1 * 0b1011 */ /* A2 * 0b1101 */ /* A3 * 0b1001 */
+ word_t of0 = ( (B[A0+7] ^ B[A0+6] ^ B[A0+5]) ^ (B[A1 + 7] ^ B[A1+5]) ^ (B[A2+6] ^ B[A2+5]) ^ ( B[A3+5] )); // 2 bit
+ word_t of1 = ( (B[A0+7] ^ B[A0+6]) ^ ( B[A1+6]) ^ (B[A2+7] ^ B[A2+6]) ^ ( B[A3+6] )); // 3 bit
+ word_t of2 = ( (B[A0+7]) ^ ( B[A1+7]) ^ ( B[A2+7]) ^ ( B[A3+7] )); // 4 bit
+
+ // inverse:
+ // 1110 1011 1101 1001
+ // A0 = A0 * 14 + A1 * 11 + A2 * 13 + A3 * 9
+ // A0 = A0 * (2+4+8) + A1 * (1+2+8) + A2 * (1+4+8) + A3 * (1+8)
+
+ // (2*A0 + 4*A0 + 8*A0 ) + (8*A1 + 2*A1 + A1 ) + ( A2 + 4*A2 + 8*A2 ) + ( A3 + 8*A3)
+ Bp[A0+0] = B[A1+0] ^ B[A2+0] ^ B[A3+0] ^ of0;
+ Bp[A0+1] = B[A0+0] ^ B[A1+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A0+2] = B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ B[A1+2] ^ B[A2+2] ^ B[A2+0] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A0+3] = B[A0+2] ^ B[A0+1] ^ B[A0+0] ^ B[A1+0] ^ B[A1+2] ^ B[A1+3] ^ B[A2+3] ^ B[A2+1] ^ B[A2+0] ^ B[A3+3] ^ B[A3+0] ^ of0 ^ of2;
+ Bp[A0+4] = B[A0+3] ^ B[A0+2] ^ B[A0+1] ^ B[A1+1] ^ B[A1+3] ^ B[A1+4] ^ B[A2+4] ^ B[A2+2] ^ B[A2+1] ^ B[A3+4] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A0+5] = B[A0+4] ^ B[A0+3] ^ B[A0+2] ^ B[A1+2] ^ B[A1+4] ^ B[A1+5] ^ B[A2+5] ^ B[A2+3] ^ B[A2+2] ^ B[A3+5] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A0+6] = B[A0+5] ^ B[A0+4] ^ B[A0+3] ^ B[A1+3] ^ B[A1+5] ^ B[A1+6] ^ B[A2+6] ^ B[A2+4] ^ B[A2+3] ^ B[A3+6] ^ B[A3+3] ^ of2;
+ Bp[A0+7] = B[A0+6] ^ B[A0+5] ^ B[A0+4] ^ B[A1+4] ^ B[A1+6] ^ B[A1+7] ^ B[A2+7] ^ B[A2+5] ^ B[A2+4] ^ B[A3+7] ^ B[A3+4];
+
+
+
+ //// state[i][1] = A0*0x09 + A1*0xe + A2*0x0b + A3*0x0d
+ // overflow:
+ /* A0 * 0b1001 */ /* A1 * 0b1110 */ /* A2 * 0b101 1 */ /* A3 * 0b1101 */
+ of0 = ( (B[A0+5]) ^ (B[A1+7] ^ B[A1+6] ^ B[A1+5]) ^ (B[A2 + 7] ^ B[A2+5]) ^ (B[A3+6] ^ B[A3+5])); // 2 bit
+ of1 = ( (B[A0+6]) ^ (B[A1+7] ^ B[A1+6]) ^ ( B[A2+6]) ^ (B[A3+7] ^ B[A3+6])); // 3 bit
+ of2 = ( (B[A0+7]) ^ (B[A1+7]) ^ ( B[A2+7]) ^ ( B[A3+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A1 = A0 * 9 + A1 * 14 + A2 * 11 + A3 * 13
+ // A1 = A0 * (1+8) + A1 * (2+4+8) + A2 * (1+2+8) + A3 * (1+4+8)
+
+ // (1*A0 + 8*A0 ) +(2*A1 + 4*A1 + 8*A1 ) + (1*A2 + 2*A2 + 8*A2 ) + (1*A3 + 4*A3 + 8*A3)
+ Bp[A1+0] = B[A0+0] ^ B[A2+0] ^ B[A3+0] ^ of0;
+ Bp[A1+1] = B[A0+1] ^ B[A1+0] ^ B[A2+1] ^ B[A2+0] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A1+2] = B[A0+2] ^ B[A1+1] ^ B[A1+0] ^ B[A2+2] ^ B[A2+1] ^ B[A3+2] ^ B[A3+0] ^ of1 ^ of2;
+ Bp[A1+3] = B[A0+3] ^ B[A0+0] ^ B[A1+2] ^ B[A1+1] ^ B[A1+0] ^ B[A2+3] ^ B[A2+2] ^ B[A2+0] ^ B[A3+3] ^ B[A3+1] ^ B[A3+0] ^ of0 ^ of2;
+ Bp[A1+4] = B[A0+4] ^ B[A0+1] ^ B[A1+3] ^ B[A1+2] ^ B[A1+1] ^ B[A2+4] ^ B[A2+3] ^ B[A2+1] ^ B[A3+4] ^ B[A3+2] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A1+5] = B[A0+5] ^ B[A0+2] ^ B[A1+4] ^ B[A1+3] ^ B[A1+2] ^ B[A2+5] ^ B[A2+4] ^ B[A2+2] ^ B[A3+5] ^ B[A3+3] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A1+6] = B[A0+6] ^ B[A0+3] ^ B[A1+5] ^ B[A1+4] ^ B[A1+3] ^ B[A2+6] ^ B[A2+5] ^ B[A2+3] ^ B[A3+6] ^ B[A3+4] ^ B[A3+3] ^ of2;
+ Bp[A1+7] = B[A0+7] ^ B[A0+4] ^ B[A1+6] ^ B[A1+5] ^ B[A1+4] ^ B[A2+7] ^ B[A2+6] ^ B[A2+4] ^ B[A3+7] ^ B[A3+5] ^ B[A3+4];
+
+
+ //// state[i][2] = A0*0x0d + A1*0x09 + A2*0x0e + A3*0x0b
+ // overflow:
+ /* A1 * 0b1001 */ /* A2 * 0b1110 */ /* A3 * 0b1011 */ /* A0 * 0b1101 */
+ of0 = ( (B[A1+5]) ^ (B[A2+7] ^ B[A2+6] ^ B[A2+5]) ^ (B[A3 + 7] ^ B[A3+5]) ^ (B[A0+6] ^ B[A0+5])); // 2 bit
+ of1 = ( (B[A1+6]) ^ (B[A2+7] ^ B[A2+6]) ^ ( B[A3+6]) ^ (B[A0+7] ^ B[A0+6])); // 3 bit
+ of2 = ( (B[A1+7]) ^ (B[A2+7]) ^ ( B[A3+7]) ^ ( B[A0+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A2 = A1 * 9 + A2 * 14 + A3 * 11 + A0 * 13
+ // A2 = A1 * (1+8) + A2 * (2+4+8) + A3 * (1+2+8) + A0 * (1+4+8)
+
+ // (1*A1 + 8*A1) + ( 2*A2 + 4*A2 + 8*A2) + (1*A3 2*A2 + 8*A2) + (1*A0 + 4*A0 + 8*A0)
+ Bp[A2+0] = B[A1+0] ^ B[A3+0] ^ B[A0+0] ^ of0;
+ Bp[A2+1] = B[A1+1] ^ B[A2+0] ^ B[A3+1] ^ B[A3+0] ^ B[A0+1] ^ of0 ^ of1;
+ Bp[A2+2] = B[A1+2] ^ B[A2+1] ^ B[A2+0] ^ B[A3+2] ^ B[A3+1] ^ B[A0+2] ^ B[A0+0] ^ of1 ^ of2;
+ Bp[A2+3] = B[A1+3] ^ B[A1+0] ^ B[A2+2] ^ B[A2+1] ^ B[A2+0] ^ B[A3+3] ^ B[A3+2] ^ B[A3+0] ^ B[A0+3] ^ B[A0+1] ^ B[A0+0] ^ of0 ^ of2;
+ Bp[A2+4] = B[A1+4] ^ B[A1+1] ^ B[A2+3] ^ B[A2+2] ^ B[A2+1] ^ B[A3+4] ^ B[A3+3] ^ B[A3+1] ^ B[A0+4] ^ B[A0+2] ^ B[A0+1] ^ of0 ^ of1;
+ Bp[A2+5] = B[A1+5] ^ B[A1+2] ^ B[A2+4] ^ B[A2+3] ^ B[A2+2] ^ B[A3+5] ^ B[A3+4] ^ B[A3+2] ^ B[A0+5] ^ B[A0+3] ^ B[A0+2] ^ of1 ^ of2;
+ Bp[A2+6] = B[A1+6] ^ B[A1+3] ^ B[A2+5] ^ B[A2+4] ^ B[A2+3] ^ B[A3+6] ^ B[A3+5] ^ B[A3+3] ^ B[A0+6] ^ B[A0+4] ^ B[A0+3] ^ of2;
+ Bp[A2+7] = B[A1+7] ^ B[A1+4] ^ B[A2+6] ^ B[A2+5] ^ B[A2+4] ^ B[A3+7] ^ B[A3+6] ^ B[A3+4] ^ B[A0+7] ^ B[A0+5] ^ B[A0+4];
+
+
+
+ //// state[i][3] = A0*0x0b + A1*0x0d + A2*0x09 + A3*0x0e
+ // overflow:
+ /* A2 * 0b1001 */ /* A3 * 0b1110 */ /* A0 * 0b1011 */ /* A1 * 0b1101 */
+ of0 = ( (B[A2+5]) ^ (B[A3+7] ^ B[A3+6] ^ B[A3+5]) ^ (B[A0 + 7] ^ B[A0+5]) ^ (B[A1+6] ^ B[A1+5])); // 2 bit
+ of1 = ( (B[A2+6]) ^ (B[A3+7] ^ B[A3+6]) ^ ( B[A0+6]) ^ (B[A1+7] ^ B[A1+6])); // 3 bit
+ of2 = ( (B[A2+7]) ^ (B[A3+7]) ^ ( B[A0+7]) ^ ( B[A1+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A2 = A2 * 9 + A3 * 14 + A0 * 11 + A1 * 13
+ // A2 = A2 * (1+8) + A3 * (2+4+8) + A0 * (1+2+8) + A1 * (1+4+8)
+
+ // (1*A2 + 8*A2) + ( 2*A3 + 4*A3 + 8*A3) + (1*A0 2*A0 + 8*A0) + (1*A1 + 4*A1 + 8*A1)
+ Bp[A3+0] = B[A2+0] ^ B[A0+0] ^ B[A1+0] ^ of0;
+ Bp[A3+1] = B[A2+1] ^ B[A3+0] ^ B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ of0 ^ of1;
+ Bp[A3+2] = B[A2+2] ^ B[A3+1] ^ B[A3+0] ^ B[A0+2] ^ B[A0+1] ^ B[A1+2] ^ B[A1+0] ^ of1 ^ of2;
+ Bp[A3+3] = B[A2+3] ^ B[A2+0] ^ B[A3+2] ^ B[A3+1] ^ B[A3+0] ^ B[A0+3] ^ B[A0+2] ^ B[A0+0] ^ B[A1+3] ^ B[A1+1] ^ B[A1+0] ^ of0 ^ of2;
+ Bp[A3+4] = B[A2+4] ^ B[A2+1] ^ B[A3+3] ^ B[A3+2] ^ B[A3+1] ^ B[A0+4] ^ B[A0+3] ^ B[A0+1] ^ B[A1+4] ^ B[A1+2] ^ B[A1+1] ^ of0 ^ of1;
+ Bp[A3+5] = B[A2+5] ^ B[A2+2] ^ B[A3+4] ^ B[A3+3] ^ B[A3+2] ^ B[A0+5] ^ B[A0+4] ^ B[A0+2] ^ B[A1+5] ^ B[A1+3] ^ B[A1+2] ^ of1 ^ of2;
+ Bp[A3+6] = B[A2+6] ^ B[A2+3] ^ B[A3+5] ^ B[A3+4] ^ B[A3+3] ^ B[A0+6] ^ B[A0+5] ^ B[A0+3] ^ B[A1+6] ^ B[A1+4] ^ B[A1+3] ^ of2;
+ Bp[A3+7] = B[A2+7] ^ B[A2+4] ^ B[A3+6] ^ B[A3+5] ^ B[A3+4] ^ B[A0+7] ^ B[A0+6] ^ B[A0+4] ^ B[A1+7] ^ B[A1+5] ^ B[A1+4];
+
+ Bp += BLOCK_SIZE/4;
+ B += BLOCK_SIZE/4;
+ }
+
+ memmove(B - BLOCK_SIZE, Bp - BLOCK_SIZE,sizeof(Bp_space));
+
+}
+
+void bs_expand_key(word_t (* rk)[BLOCK_SIZE], uint8_t * _key)
+{
+ // TODO integrate this better
+ uint8_t key[KEY_SCHEDULE_SIZE];
+ memmove(key,_key,BLOCK_SIZE/8);
+ expand_key(key);
+
+ int i, j = 0, k, l;
+ for (i = 0; i < KEY_SCHEDULE_SIZE; i += (BLOCK_SIZE/8))
+ {
+ memmove(rk[j], key + i, BLOCK_SIZE / 8);
+
+ for (k = WORDS_PER_BLOCK; k < 128; k += WORDS_PER_BLOCK)
+ {
+ for (l = 0; l < WORDS_PER_BLOCK; l++)
+ {
+ rk[j][k + l] = rk[j][l];
+ }
+ }
+ bs_transpose(rk[j]);
+ j++;
+ }
+
+}
+
+void bs_cipher(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE])
+{
+ int round;
+ bs_transpose(state);
+
+
+ bs_addroundkey(state,rk[0]);
+ for (round = 1; round < 10; round++)
+ {
+ bs_apply_sbox(state);
+ /*bs_shiftrows(state);*/
+ /*bs_mixcolumns(state);*/
+ bs_shiftmix(state);
+ bs_addroundkey(state,rk[round]);
+ }
+ bs_apply_sbox(state);
+ bs_shiftrows(state);
+ bs_addroundkey(state,rk[10]);
+ bs_transpose_rev(state);
+}
+
+void bs_cipher_rev(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE])
+{
+ int round;
+ bs_transpose(state);
+
+ bs_addroundkey(state,rk[10]);
+ for (round = 9; round > 0; round--)
+ {
+ bs_shiftrows_rev(state);
+ bs_apply_sbox_rev(state);
+ bs_addroundkey(state,rk[round]);
+ bs_mixcolumns_rev(state);
+ }
+ bs_shiftrows_rev(state);
+ bs_apply_sbox_rev(state);
+ bs_addroundkey(state,rk[0]);
+
+ bs_transpose_rev(state);
+}
+
+
+
diff --git a/test/monniaux/bitsliced-aes/bs.h b/test/monniaux/bitsliced-aes/bs.h
new file mode 100644
index 00000000..d2110f85
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/bs.h
@@ -0,0 +1,70 @@
+#ifndef _BS_H_
+#define _BS_H_
+
+#include <stdint.h>
+
+#define BLOCK_SIZE 128
+#define KEY_SCHEDULE_SIZE 176
+#define WORD_SIZE 64
+#define BS_BLOCK_SIZE (BLOCK_SIZE * WORD_SIZE / 8)
+#define WORDS_PER_BLOCK (BLOCK_SIZE / WORD_SIZE)
+
+#if (WORD_SIZE==64)
+ typedef uint64_t word_t;
+ #define ONE 1ULL
+ #define MUL_SHIFT 6
+ #define WFMT "lx"
+ #define WPAD "016"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap64(x)
+#elif (WORD_SIZE==32)
+ typedef uint32_t word_t;
+ #define ONE 1UL
+ #define MUL_SHIFT 5
+ #define WFMT "x"
+ #define WPAD "08"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap32(x)
+#elif (WORD_SIZE==16)
+ typedef uint16_t word_t;
+ #define ONE 1
+ #define MUL_SHIFT 4
+ #define WFMT "hx"
+ #define WPAD "04"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap16(x)
+#elif (WORD_SIZE==8)
+ typedef uint8_t word_t;
+ #define ONE 1
+ #define MUL_SHIFT 3
+ #define WFMT "hhx"
+ #define WPAD "02"
+ #define __builtin_bswap_wordsize(x) (x)
+#else
+#error "invalid word size"
+#endif
+
+void bs_transpose(word_t * blocks);
+void bs_transpose_rev(word_t * blocks);
+void bs_transpose_dst(word_t * transpose, word_t * blocks);
+
+void bs_sbox(word_t U[8]);
+void bs_sbox_rev(word_t U[8]);
+
+void bs_shiftrows(word_t * B);
+void bs_shiftrows_rev(word_t * B);
+
+void bs_mixcolumns(word_t * B);
+void bs_mixcolumns_rev(word_t * B);
+
+void bs_shiftmix(word_t * B);
+
+void bs_addroundkey(word_t * B, word_t * rk);
+void bs_apply_sbox(word_t * input);
+void bs_apply_sbox_rev(word_t * input);
+
+
+void expand_key(unsigned char *in);
+void bs_expand_key(word_t (* rk)[BLOCK_SIZE], uint8_t * key);
+
+void bs_cipher(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE]);
+void bs_cipher_rev(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE]);
+
+#endif
diff --git a/test/monniaux/bitsliced-aes/key_schedule.c b/test/monniaux/bitsliced-aes/key_schedule.c
new file mode 100644
index 00000000..7221433a
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/key_schedule.c
@@ -0,0 +1,87 @@
+
+// Thanks to http://www.samiam.org/key-schedule.html
+#include <stdint.h>
+
+static const uint8_t sbox[256] = {
+ //0 1 2 3 4 5 6 7 8 9 A B C D E F
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
+ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
+ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
+ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
+ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
+ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
+ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
+
+static void rotate(unsigned char *in) {
+ unsigned char a,c;
+ a = in[0];
+ for(c=0;c<3;c++)
+ in[c] = in[c + 1];
+ in[3] = a;
+ return;
+}
+
+/* Calculate the rcon used in key expansion */
+static unsigned char rcon(unsigned char in) {
+ unsigned char c=1;
+ if(in == 0)
+ return 0;
+ while(in != 1) {
+ unsigned char b;
+ b = c & 0x80;
+ c <<= 1;
+ if(b == 0x80) {
+ c ^= 0x1b;
+ }
+ in--;
+ }
+ return c;
+}
+
+/* This is the core key expansion, which, given a 4-byte value,
+ * does some scrambling */
+static void schedule_core(unsigned char *in, unsigned char i) {
+ char a;
+ /* Rotate the input 8 bits to the left */
+ rotate(in);
+ /* Apply Rijndael's s-box on all 4 bytes */
+ for(a = 0; a < 4; a++)
+ in[a] = sbox[in[a]];
+ /* On just the first byte, add 2^i to the byte */
+ in[0] ^= rcon(i);
+}
+
+void expand_key(unsigned char *in) {
+ unsigned char t[4];
+ /* c is 16 because the first sub-key is the user-supplied key */
+ unsigned char c = 16;
+ unsigned char i = 1;
+ unsigned char a;
+
+ /* We need 11 sets of sixteen bytes each for 128-bit mode */
+ while(c < 176) {
+ /* Copy the temporary variable over from the last 4-byte
+ * block */
+ for(a = 0; a < 4; a++)
+ t[a] = in[a + c - 4];
+ /* Every four blocks (of four bytes),
+ * do a complex calculation */
+ if(c % 16 == 0) {
+ schedule_core(t,i);
+ i++;
+ }
+ for(a = 0; a < 4; a++) {
+ in[c] = in[c - 16] ^ t[a];
+ c++;
+ }
+ }
+}
diff --git a/test/monniaux/bitsliced-aes/main.c b/test/monniaux/bitsliced-aes/main.c
new file mode 100644
index 00000000..83588872
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/main.c
@@ -0,0 +1,47 @@
+/*#include <stdio.h>*/
+#include <stdlib.h>
+#include <string.h>
+#include "bs.h"
+#include "aes.h"
+#include "utils.h"
+#include "../clock.h"
+
+#define RUN_TESTS
+
+#include "testbench/app.h"
+#ifdef RUN_TESTS
+#include "tests/tests.h"
+#endif
+
+#ifdef TEST_FOOTPRINT
+#define printf(fmt, ...) (0)
+#define fprintf(f,fmt, ...) (0)
+#else
+#include <stdio.h>
+#endif
+
+int main(int argc, char * argv[])
+{
+ clock_prepare();
+
+ clock_start();
+
+#ifdef RUN_TESTS
+#ifndef TEST_FOOTPRINT
+ aes_ecb_test();
+#endif
+ aes_ctr_test();
+
+#else
+
+ cli_app(argc,argv);
+
+#endif
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+}
+
+
diff --git a/test/monniaux/bitsliced-aes/notes.org b/test/monniaux/bitsliced-aes/notes.org
new file mode 100644
index 00000000..c9a6fea2
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/notes.org
@@ -0,0 +1,59 @@
+* bs_transpose_dst only
+** original
+==> test.ccomp.host.out <==
+cycles: 3080223
+
+==> test.ccomp.kvx.out <==
+cycles: 10145951
+
+==> test.gcc.host.out <==
+cycles: 1485887
+
+==> test.gcc.kvx.out <==
+cycles: 4078535
+
+** neg and
+==> test.ccomp.host.out <==
+cycles: 2905049
+
+==> test.ccomp.kvx.out <==
+cycles: 7995063
+
+==> test.gcc.host.out <==
+cycles: 1858263
+
+==> test.gcc.kvx.out <==
+cycles: 5255763
+
+** cmove mais mauvais scheduling de registres
+==> test.ccomp.host.out <==
+cycles: 4363682
+
+==> test.ccomp.kvx.out <==
+cycles: 7208629
+
+==> test.gcc.host.out <==
+cycles: 2916854
+
+==> test.gcc.kvx.out <==
+cycles: 5646730
+
+** cmove via match du and
+==> test.ccomp.host.out <==
+cycles: 2553732
+
+==> test.ccomp.kvx.out <==
+cycles: 7208629
+
+==> test.gcc.host.out <==
+cycles: 1849125
+
+==> test.gcc.kvx.out <==
+cycles: 5255763
+
+** hand optimized loads
+cycles: 6027072
+
+* both bs_transpose_dst and bs_transpose_rev
+** with both cmove
+6890902
diff --git a/test/monniaux/bitsliced-aes/one_file/bitsliced-aes.c b/test/monniaux/bitsliced-aes/one_file/bitsliced-aes.c
new file mode 100644
index 00000000..bfa9dba8
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/one_file/bitsliced-aes.c
@@ -0,0 +1,1542 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "/home/monniaux/work/Kalray/CompCert/test/monniaux/clock.h"
+
+#define EXIT1
+
+void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key);
+void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key);
+
+void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv);
+#define aes_ctr_decrypt(outputb,inputb,size,key,iv) aes_ctr_encrypt(outputb,inputb,size,key,iv)
+
+#define BLOCK_SIZE 128
+#define KEY_SCHEDULE_SIZE 176
+#define WORD_SIZE 64
+#define BS_BLOCK_SIZE (BLOCK_SIZE * WORD_SIZE / 8)
+#define WORDS_PER_BLOCK (BLOCK_SIZE / WORD_SIZE)
+
+#if (WORD_SIZE==64)
+ typedef uint64_t word_t;
+ #define ONE 1ULL
+ #define MUL_SHIFT 6
+ #define WFMT "lx"
+ #define WPAD "016"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap64(x)
+#elif (WORD_SIZE==32)
+ typedef uint32_t word_t;
+ #define ONE 1UL
+ #define MUL_SHIFT 5
+ #define WFMT "x"
+ #define WPAD "08"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap32(x)
+#elif (WORD_SIZE==16)
+ typedef uint16_t word_t;
+ #define ONE 1
+ #define MUL_SHIFT 4
+ #define WFMT "hx"
+ #define WPAD "04"
+ #define __builtin_bswap_wordsize(x) __builtin_bswap16(x)
+#elif (WORD_SIZE==8)
+ typedef uint8_t word_t;
+ #define ONE 1
+ #define MUL_SHIFT 3
+ #define WFMT "hhx"
+ #define WPAD "02"
+ #define __builtin_bswap_wordsize(x) (x)
+#else
+#error "invalid word size"
+#endif
+
+void bs_transpose(word_t * blocks);
+void bs_transpose_rev(word_t * blocks);
+void bs_transpose_dst(word_t * transpose, word_t * blocks);
+
+void bs_sbox(word_t U[8]);
+void bs_sbox_rev(word_t U[8]);
+
+void bs_shiftrows(word_t * B);
+void bs_shiftrows_rev(word_t * B);
+
+void bs_mixcolumns(word_t * B);
+void bs_mixcolumns_rev(word_t * B);
+
+void bs_shiftmix(word_t * B);
+
+void bs_addroundkey(word_t * B, word_t * rk);
+void bs_apply_sbox(word_t * input);
+void bs_apply_sbox_rev(word_t * input);
+
+
+void expand_key(unsigned char *in);
+void bs_expand_key(word_t (* rk)[BLOCK_SIZE], uint8_t * key);
+
+void bs_cipher(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE]);
+void bs_cipher_rev(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE]);
+
+
+void dump_hex(uint8_t * h, int len);
+void dump_word(word_t * h, int len);
+void dump_block(word_t * h, int len);
+
+#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
+#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+
+void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher_rev(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher_rev(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+static void INC_CTR(uint8_t * ctr, uint8_t i)
+{
+ ctr += BLOCK_SIZE/8 - 1;
+ uint8_t n = *(ctr);
+ *ctr += i;
+ while(*ctr < n)
+ {
+ ctr--;
+ n = *ctr;
+ (*ctr)++;
+ }
+}
+
+void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv)
+{
+ word_t rk[11][BLOCK_SIZE];
+ word_t ctr[BLOCK_SIZE];
+ uint8_t iv_copy[BLOCK_SIZE/8];
+
+ memset(outputb,0,size);
+ memset(ctr,0,sizeof(ctr));
+ memmove(iv_copy,iv,BLOCK_SIZE/8);
+
+ word_t * state = (word_t *)outputb;
+ bs_expand_key(rk, key);
+
+ do
+ {
+ int chunk = MIN(size, BS_BLOCK_SIZE);
+ int blocks = chunk / (BLOCK_SIZE/8);
+ if (chunk % (BLOCK_SIZE/8))
+ {
+ blocks++;
+ }
+
+ int i;
+ for (i = 0; i < blocks; i++)
+ {
+ memmove(ctr + (i * WORDS_PER_BLOCK), iv_copy, BLOCK_SIZE/8);
+ INC_CTR(iv_copy,1);
+ }
+
+ bs_cipher(ctr, rk);
+ size -= chunk;
+
+ uint8_t * ctr_p = (uint8_t *) ctr;
+ while(chunk--)
+ {
+ *outputb++ = *ctr_p++ ^ *inputb++;
+ }
+
+ }
+ while(size);
+
+}
+
+void dump_hex(uint8_t * h, int len)
+{
+ while(len--)
+ printf("%02hhx",*h++);
+ printf("\n");
+}
+
+void dump_word(word_t * h, int len)
+{
+ while(len--)
+ if ((len+1) % 8) printf("%" WPAD WFMT "\n",*h++);
+ else printf("%d:\n%" WPAD WFMT "\n",128-len-1,*h++);
+
+ printf("\n");
+}
+
+void dump_block(word_t * h, int len)
+{
+ while(len-=2 >= 0)
+ printf("%" WPAD WFMT"%" WPAD WFMT "\n",*h++,*h++);
+ printf("\n");
+}
+
+static const uint8_t sbox[256] = {
+ //0 1 2 3 4 5 6 7 8 9 A B C D E F
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
+ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
+ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
+ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
+ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
+ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
+ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
+
+static void rotate(unsigned char *in) {
+ unsigned char a,c;
+ a = in[0];
+ for(c=0;c<3;c++)
+ in[c] = in[c + 1];
+ in[3] = a;
+ return;
+}
+
+/* Calculate the rcon used in key expansion */
+static unsigned char rcon(unsigned char in) {
+ unsigned char c=1;
+ if(in == 0)
+ return 0;
+ while(in != 1) {
+ unsigned char b;
+ b = c & 0x80;
+ c <<= 1;
+ if(b == 0x80) {
+ c ^= 0x1b;
+ }
+ in--;
+ }
+ return c;
+}
+
+/* This is the core key expansion, which, given a 4-byte value,
+ * does some scrambling */
+static void schedule_core(unsigned char *in, unsigned char i) {
+ char a;
+ /* Rotate the input 8 bits to the left */
+ rotate(in);
+ /* Apply Rijndael's s-box on all 4 bytes */
+ for(a = 0; a < 4; a++)
+ in[a] = sbox[in[a]];
+ /* On just the first byte, add 2^i to the byte */
+ in[0] ^= rcon(i);
+}
+
+void expand_key(unsigned char *in) {
+ unsigned char t[4];
+ /* c is 16 because the first sub-key is the user-supplied key */
+ unsigned char c = 16;
+ unsigned char i = 1;
+ unsigned char a;
+
+ /* We need 11 sets of sixteen bytes each for 128-bit mode */
+ while(c < 176) {
+ /* Copy the temporary variable over from the last 4-byte
+ * block */
+ for(a = 0; a < 4; a++)
+ t[a] = in[a + c - 4];
+ /* Every four blocks (of four bytes),
+ * do a complex calculation */
+ if(c % 16 == 0) {
+ schedule_core(t,i);
+ i++;
+ }
+ for(a = 0; a < 4; a++) {
+ in[c] = in[c - 16] ^ t[a];
+ c++;
+ }
+ }
+}
+
+#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ||\
+ defined(__amd64__) || defined(__amd32__)|| defined(__amd16__)
+#define bs2le(x) (x)
+#define bs2be(x) (x)
+#elif (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ||\
+ (defined(__sparc__))
+#define bs2le(x) __builtin_bswap_wordsize(x)
+#define bs2be(x) __builtin_bswap_wordsize(x)
+#else
+#error "endianness not supported"
+#endif
+
+
+void bs_addroundkey(word_t * B, word_t * rk)
+{
+ int i;
+ for (i = 0; i < BLOCK_SIZE; i++)
+ B[i] ^= rk[i];
+}
+
+void bs_apply_sbox(word_t * input)
+{
+ int i;
+ for(i=0; i < BLOCK_SIZE; i+=8)
+ {
+ bs_sbox(input+i);
+ }
+}
+
+void bs_apply_sbox_rev(word_t * input)
+{
+ int i;
+ for(i=0; i < BLOCK_SIZE; i+=8)
+ {
+ bs_sbox_rev(input+i);
+ }
+}
+
+/*July 2011*/
+/*Straight-line program for AES s box*/
+
+/*Input is U[0], U[1],...,U[7]*/
+/*Output is S[0], S[1],...,S[7]*/
+// http://cs-www.cs.yale.edu/homes/peralta/CircuitStuff/CMT.html
+void bs_sbox_rev(word_t U[8])
+{
+ word_t W[8];
+ word_t
+ T1,T2,T3,T4,T5,T6,T8,
+ T9,T10,T13,T14,T15,T16,
+ T17,T18,T19,T20,T22,T23,T24,
+ T25, T26, T27;
+
+ word_t
+ M1,M2,M3,M4,M5,M6,M7,M8,
+ M9,M10,M11,M12,M13,M14,M15,
+ M16,M17,M18,M19,M20,M21,M22,
+ M23,M24,M25,M26,M27,M28,M29,
+ M30,M31,M32,M33,M34,M35,M36,
+ M37,M38,M39,M40,M41,M42,M43,
+ M44,M45,M46,M47,M48,M49,M50,
+ M51,M52,M53,M54,M55,M56,M57,
+ M58,M59,M60,M61,M62,M63;
+
+ word_t
+ P0,P1,P2,P3,P4,P5,P6,P7,P8,
+ P9,P10,P11,P12,P13,P14,
+ P15,P16,P17,P18,P19,P20,
+ P21,P22,P23,P24,P25,P26,
+ P27,P28,P29;
+
+ word_t Y5,
+ R5, R13, R17, R18, R19;
+
+
+ T23 = U[7] ^ U[4];
+ T22 = ~(U[6] ^ U[4]);
+ T2 = ~(U[7] ^ U[6]);
+ T1 = U[4] ^ U[3];
+ T24 = ~(U[3] ^ U[0]);
+ R5 = U[1] ^ U[0];
+ T8 = ~(U[6] ^ T23);
+ T19 = T22 ^ R5;
+ T9 = ~(U[0] ^ T1);
+ T10 = T2 ^ T24;
+ T13 = T2 ^ R5;
+ T3 = T1 ^ R5;
+ T25 = ~(U[5] ^ T1);
+ R13 = U[6] ^ U[1];
+ T17 = ~(U[5] ^ T19);
+ T20 = T24 ^ R13;
+ T4 = U[3] ^ T8;
+ R17 = ~(U[5] ^ U[2]);
+ R18 = ~(U[2] ^ U[1]);
+ R19 = ~(U[5] ^ U[3]);
+ Y5 = U[7] ^ R17;
+ T6 = T22 ^ R17;
+ T16 = R13 ^ R19;
+ T27 = T1 ^ R18;
+ T15 = T10 ^ T27;
+ T14 = T10 ^ R18;
+ T26 = T3 ^ T16;
+ M1 = T13 & T6;
+ M2 = T23 & T8;
+ M3 = T14 ^ M1;
+ M4 = T19 & Y5;
+ M5 = M4 ^ M1;
+ M6 = T3 & T16;
+ M7 = T22 & T9;
+ M8 = T26 ^ M6;
+ M9 = T20 & T17;
+ M10 = M9 ^ M6;
+ M11 = T1 & T15;
+ M12 = T4 & T27;
+ M13 = M12 ^ M11;
+ M14 = T2 & T10;
+ M15 = M14 ^ M11;
+ M16 = M3 ^ M2;
+ M17 = M5 ^ T24;
+ M18 = M8 ^ M7;
+ M19 = M10 ^ M15;
+ M20 = M16 ^ M13;
+ M21 = M17 ^ M15;
+ M22 = M18 ^ M13;
+ M23 = M19 ^ T25;
+ M24 = M22 ^ M23;
+ M25 = M22 & M20;
+ M26 = M21 ^ M25;
+ M27 = M20 ^ M21;
+ M28 = M23 ^ M25;
+ M29 = M28 & M27;
+ M30 = M26 & M24;
+ M31 = M20 & M23;
+ M32 = M27 & M31;
+ M33 = M27 ^ M25;
+ M34 = M21 & M22;
+ M35 = M24 & M34;
+ M36 = M24 ^ M25;
+ M37 = M21 ^ M29;
+ M38 = M32 ^ M33;
+ M39 = M23 ^ M30;
+ M40 = M35 ^ M36;
+ M41 = M38 ^ M40;
+ M42 = M37 ^ M39;
+ M43 = M37 ^ M38;
+ M44 = M39 ^ M40;
+ M45 = M42 ^ M41;
+ M46 = M44 & T6;
+ M47 = M40 & T8;
+ M48 = M39 & Y5;
+ M49 = M43 & T16;
+ M50 = M38 & T9;
+ M51 = M37 & T17;
+ M52 = M42 & T15;
+ M53 = M45 & T27;
+ M54 = M41 & T10;
+ M55 = M44 & T13;
+ M56 = M40 & T23;
+ M57 = M39 & T19;
+ M58 = M43 & T3;
+ M59 = M38 & T22;
+ M60 = M37 & T20;
+ M61 = M42 & T1;
+ M62 = M45 & T4;
+ M63 = M41 & T2;
+ P0 = M52 ^ M61;
+ P1 = M58 ^ M59;
+ P2 = M54 ^ M62;
+ P3 = M47 ^ M50;
+ P4 = M48 ^ M56;
+ P5 = M46 ^ M51;
+ P6 = M49 ^ M60;
+ P7 = P0 ^ P1;
+ P8 = M50 ^ M53;
+ P9 = M55 ^ M63;
+ P10 = M57 ^ P4;
+ P11 = P0 ^ P3;
+ P12 = M46 ^ M48;
+ P13 = M49 ^ M51;
+ P14 = M49 ^ M62;
+ P15 = M54 ^ M59;
+ P16 = M57 ^ M61;
+ P17 = M58 ^ P2;
+ P18 = M63 ^ P5;
+ P19 = P2 ^ P3;
+ P20 = P4 ^ P6;
+ P22 = P2 ^ P7;
+ P23 = P7 ^ P8;
+ P24 = P5 ^ P7;
+ P25 = P6 ^ P10;
+ P26 = P9 ^ P11;
+ P27 = P10 ^ P18;
+ P28 = P11 ^ P25;
+ P29 = P15 ^ P20;
+ W[7] = P13 ^ P22;
+ W[6] = P26 ^ P29;
+ W[5] = P17 ^ P28;
+ W[4] = P12 ^ P22;
+ W[3] = P23 ^ P27;
+ W[2] = P19 ^ P24;
+ W[1] = P14 ^ P23;
+ W[0] = P9 ^ P16;
+
+ memmove(U,W,sizeof(W));
+}
+
+void bs_sbox(word_t U[8])
+{
+ word_t S[8];
+ word_t
+ T1,T2,T3,T4,T5,T6,T7,T8,
+ T9,T10,T11,T12,T13,T14,T15,T16,
+ T17,T18,T19,T20,T21,T22,T23,T24,
+ T25, T26, T27;
+
+ word_t
+ M1,M2,M3,M4,M5,M6,M7,M8,
+ M9,M10,M11,M12,M13,M14,M15,
+ M16,M17,M18,M19,M20,M21,M22,
+ M23,M24,M25,M26,M27,M28,M29,
+ M30,M31,M32,M33,M34,M35,M36,
+ M37,M38,M39,M40,M41,M42,M43,
+ M44,M45,M46,M47,M48,M49,M50,
+ M51,M52,M53,M54,M55,M56,M57,
+ M58,M59,M60,M61,M62,M63;
+
+ word_t
+ L0,L1,L2,L3,L4,L5,L6,L7,L8,
+ L9,L10,L11,L12,L13,L14,
+ L15,L16,L17,L18,L19,L20,
+ L21,L22,L23,L24,L25,L26,
+ L27,L28,L29;
+
+ T1 = U[7] ^ U[4];
+ T2 = U[7] ^ U[2];
+ T3 = U[7] ^ U[1];
+ T4 = U[4] ^ U[2];
+ T5 = U[3] ^ U[1];
+ T6 = T1 ^ T5;
+ T7 = U[6] ^ U[5];
+ T8 = U[0] ^ T6;
+ T9 = U[0] ^ T7;
+ T10 = T6 ^ T7;
+ T11 = U[6] ^ U[2];
+ T12 = U[5] ^ U[2];
+ T13 = T3 ^ T4;
+ T14 = T6 ^ T11;
+ T15 = T5 ^ T11;
+ T16 = T5 ^ T12;
+ T17 = T9 ^ T16;
+ T18 = U[4] ^ U[0];
+ T19 = T7 ^ T18;
+ T20 = T1 ^ T19;
+ T21 = U[1] ^ U[0];
+ T22 = T7 ^ T21;
+ T23 = T2 ^ T22;
+ T24 = T2 ^ T10;
+ T25 = T20 ^ T17;
+ T26 = T3 ^ T16;
+ T27 = T1 ^ T12;
+ M1 = T13 & T6;
+ M2 = T23 & T8;
+ M3 = T14 ^ M1;
+ M4 = T19 & U[0];
+ M5 = M4 ^ M1;
+ M6 = T3 & T16;
+ M7 = T22 & T9;
+ M8 = T26 ^ M6;
+ M9 = T20 & T17;
+ M10 = M9 ^ M6;
+ M11 = T1 & T15;
+ M12 = T4 & T27;
+ M13 = M12 ^ M11;
+ M14 = T2 & T10;
+ M15 = M14 ^ M11;
+ M16 = M3 ^ M2;
+ M17 = M5 ^ T24;
+ M18 = M8 ^ M7;
+ M19 = M10 ^ M15;
+ M20 = M16 ^ M13;
+ M21 = M17 ^ M15;
+ M22 = M18 ^ M13;
+ M23 = M19 ^ T25;
+ M24 = M22 ^ M23;
+ M25 = M22 & M20;
+ M26 = M21 ^ M25;
+ M27 = M20 ^ M21;
+ M28 = M23 ^ M25;
+ M29 = M28 & M27;
+ M30 = M26 & M24;
+ M31 = M20 & M23;
+ M32 = M27 & M31;
+ M33 = M27 ^ M25;
+ M34 = M21 & M22;
+ M35 = M24 & M34;
+ M36 = M24 ^ M25;
+ M37 = M21 ^ M29;
+ M38 = M32 ^ M33;
+ M39 = M23 ^ M30;
+ M40 = M35 ^ M36;
+ M41 = M38 ^ M40;
+ M42 = M37 ^ M39;
+ M43 = M37 ^ M38;
+ M44 = M39 ^ M40;
+ M45 = M42 ^ M41;
+ M46 = M44 & T6;
+ M47 = M40 & T8;
+ M48 = M39 & U[0];
+ M49 = M43 & T16;
+ M50 = M38 & T9;
+ M51 = M37 & T17;
+ M52 = M42 & T15;
+ M53 = M45 & T27;
+ M54 = M41 & T10;
+ M55 = M44 & T13;
+ M56 = M40 & T23;
+ M57 = M39 & T19;
+ M58 = M43 & T3;
+ M59 = M38 & T22;
+ M60 = M37 & T20;
+ M61 = M42 & T1;
+ M62 = M45 & T4;
+ M63 = M41 & T2;
+ L0 = M61 ^ M62;
+ L1 = M50 ^ M56;
+ L2 = M46 ^ M48;
+ L3 = M47 ^ M55;
+ L4 = M54 ^ M58;
+ L5 = M49 ^ M61;
+ L6 = M62 ^ L5;
+ L7 = M46 ^ L3;
+ L8 = M51 ^ M59;
+ L9 = M52 ^ M53;
+ L10 = M53 ^ L4;
+ L11 = M60 ^ L2;
+ L12 = M48 ^ M51;
+ L13 = M50 ^ L0;
+ L14 = M52 ^ M61;
+ L15 = M55 ^ L1;
+ L16 = M56 ^ L0;
+ L17 = M57 ^ L1;
+ L18 = M58 ^ L8;
+ L19 = M63 ^ L4;
+ L20 = L0 ^ L1;
+ L21 = L1 ^ L7;
+ L22 = L3 ^ L12;
+ L23 = L18 ^ L2;
+ L24 = L15 ^ L9;
+ L25 = L6 ^ L10;
+ L26 = L7 ^ L9;
+ L27 = L8 ^ L10;
+ L28 = L11 ^ L14;
+ L29 = L11 ^ L17;
+ S[7] = L6 ^ L24;
+ S[6] = ~(L16 ^ L26);
+ S[5] = ~(L19 ^ L28);
+ S[4] = L6 ^ L21;
+ S[3] = L20 ^ L22;
+ S[2] = L25 ^ L29;
+ S[1] = ~(L13 ^ L27);
+ S[0] = ~(L6 ^ L23);
+
+ memmove(U,S,sizeof(S));
+}
+
+void bs_transpose(word_t * blocks)
+{
+ word_t transpose[BLOCK_SIZE];
+ memset(transpose, 0, sizeof(transpose));
+ bs_transpose_dst(transpose,blocks);
+ memmove(blocks,transpose,sizeof(transpose));
+}
+
+void bs_transpose_dst(word_t * transpose, word_t * blocks)
+{
+ int i,k;
+ word_t w;
+ for(k=0; k < WORD_SIZE; k++)
+ {
+ int bitpos = ONE << k;
+ for (i=0; i < WORDS_PER_BLOCK; i++)
+ {
+ w = bs2le(blocks[k * WORDS_PER_BLOCK + i]);
+ int offset = i << MUL_SHIFT;
+
+#ifndef UNROLL_TRANSPOSE
+ int j;
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ // TODO make const time
+ transpose[offset + j] |= (w & (ONE << j)) ? bitpos : 0;
+ }
+#else
+
+ transpose[(offset)+ 0 ] |= (w & (ONE << 0 )) ? (bitpos) : 0;
+ transpose[(offset)+ 1 ] |= (w & (ONE << 1 )) ? (bitpos) : 0;
+ transpose[(offset)+ 2 ] |= (w & (ONE << 2 )) ? (bitpos) : 0;
+ transpose[(offset)+ 3 ] |= (w & (ONE << 3 )) ? (bitpos) : 0;
+ transpose[(offset)+ 4 ] |= (w & (ONE << 4 )) ? (bitpos) : 0;
+ transpose[(offset)+ 5 ] |= (w & (ONE << 5 )) ? (bitpos) : 0;
+ transpose[(offset)+ 6 ] |= (w & (ONE << 6 )) ? (bitpos) : 0;
+ transpose[(offset)+ 7 ] |= (w & (ONE << 7 )) ? (bitpos) : 0;
+#if WORD_SIZE > 8
+ transpose[(offset)+ 8 ] |= (w & (ONE << 8 )) ? (bitpos) : 0;
+ transpose[(offset)+ 9 ] |= (w & (ONE << 9 )) ? (bitpos) : 0;
+ transpose[(offset)+ 10] |= (w & (ONE << 10)) ? (bitpos) : 0;
+ transpose[(offset)+ 11] |= (w & (ONE << 11)) ? (bitpos) : 0;
+ transpose[(offset)+ 12] |= (w & (ONE << 12)) ? (bitpos) : 0;
+ transpose[(offset)+ 13] |= (w & (ONE << 13)) ? (bitpos) : 0;
+ transpose[(offset)+ 14] |= (w & (ONE << 14)) ? (bitpos) : 0;
+ transpose[(offset)+ 15] |= (w & (ONE << 15)) ? (bitpos) : 0;
+#endif
+#if WORD_SIZE > 16
+ transpose[(offset)+ 16] |= (w & (ONE << 16)) ? (bitpos) : 0;
+ transpose[(offset)+ 17] |= (w & (ONE << 17)) ? (bitpos) : 0;
+ transpose[(offset)+ 18] |= (w & (ONE << 18)) ? (bitpos) : 0;
+ transpose[(offset)+ 19] |= (w & (ONE << 19)) ? (bitpos) : 0;
+ transpose[(offset)+ 20] |= (w & (ONE << 20)) ? (bitpos) : 0;
+ transpose[(offset)+ 21] |= (w & (ONE << 21)) ? (bitpos) : 0;
+ transpose[(offset)+ 22] |= (w & (ONE << 22)) ? (bitpos) : 0;
+ transpose[(offset)+ 23] |= (w & (ONE << 23)) ? (bitpos) : 0;
+ transpose[(offset)+ 24] |= (w & (ONE << 24)) ? (bitpos) : 0;
+ transpose[(offset)+ 25] |= (w & (ONE << 25)) ? (bitpos) : 0;
+ transpose[(offset)+ 26] |= (w & (ONE << 26)) ? (bitpos) : 0;
+ transpose[(offset)+ 27] |= (w & (ONE << 27)) ? (bitpos) : 0;
+ transpose[(offset)+ 28] |= (w & (ONE << 28)) ? (bitpos) : 0;
+ transpose[(offset)+ 29] |= (w & (ONE << 29)) ? (bitpos) : 0;
+ transpose[(offset)+ 30] |= (w & (ONE << 30)) ? (bitpos) : 0;
+ transpose[(offset)+ 31] |= (w & (ONE << 31)) ? (bitpos) : 0;
+#endif
+#if WORD_SIZE > 32
+ transpose[(offset)+ 32] |= (w & (ONE << 32)) ? (bitpos) : 0;
+ transpose[(offset)+ 33] |= (w & (ONE << 33)) ? (bitpos) : 0;
+ transpose[(offset)+ 34] |= (w & (ONE << 34)) ? (bitpos) : 0;
+ transpose[(offset)+ 35] |= (w & (ONE << 35)) ? (bitpos) : 0;
+ transpose[(offset)+ 36] |= (w & (ONE << 36)) ? (bitpos) : 0;
+ transpose[(offset)+ 37] |= (w & (ONE << 37)) ? (bitpos) : 0;
+ transpose[(offset)+ 38] |= (w & (ONE << 38)) ? (bitpos) : 0;
+ transpose[(offset)+ 39] |= (w & (ONE << 39)) ? (bitpos) : 0;
+ transpose[(offset)+ 40] |= (w & (ONE << 40)) ? (bitpos) : 0;
+ transpose[(offset)+ 41] |= (w & (ONE << 41)) ? (bitpos) : 0;
+ transpose[(offset)+ 42] |= (w & (ONE << 42)) ? (bitpos) : 0;
+ transpose[(offset)+ 43] |= (w & (ONE << 43)) ? (bitpos) : 0;
+ transpose[(offset)+ 44] |= (w & (ONE << 44)) ? (bitpos) : 0;
+ transpose[(offset)+ 45] |= (w & (ONE << 45)) ? (bitpos) : 0;
+ transpose[(offset)+ 46] |= (w & (ONE << 46)) ? (bitpos) : 0;
+ transpose[(offset)+ 47] |= (w & (ONE << 47)) ? (bitpos) : 0;
+ transpose[(offset)+ 48] |= (w & (ONE << 48)) ? (bitpos) : 0;
+ transpose[(offset)+ 49] |= (w & (ONE << 49)) ? (bitpos) : 0;
+ transpose[(offset)+ 50] |= (w & (ONE << 50)) ? (bitpos) : 0;
+ transpose[(offset)+ 51] |= (w & (ONE << 51)) ? (bitpos) : 0;
+ transpose[(offset)+ 52] |= (w & (ONE << 52)) ? (bitpos) : 0;
+ transpose[(offset)+ 53] |= (w & (ONE << 53)) ? (bitpos) : 0;
+ transpose[(offset)+ 54] |= (w & (ONE << 54)) ? (bitpos) : 0;
+ transpose[(offset)+ 55] |= (w & (ONE << 55)) ? (bitpos) : 0;
+ transpose[(offset)+ 56] |= (w & (ONE << 56)) ? (bitpos) : 0;
+ transpose[(offset)+ 57] |= (w & (ONE << 57)) ? (bitpos) : 0;
+ transpose[(offset)+ 58] |= (w & (ONE << 58)) ? (bitpos) : 0;
+ transpose[(offset)+ 59] |= (w & (ONE << 59)) ? (bitpos) : 0;
+ transpose[(offset)+ 60] |= (w & (ONE << 60)) ? (bitpos) : 0;
+ transpose[(offset)+ 61] |= (w & (ONE << 61)) ? (bitpos) : 0;
+ transpose[(offset)+ 62] |= (w & (ONE << 62)) ? (bitpos) : 0;
+ transpose[(offset)+ 63] |= (w & (ONE << 63)) ? (bitpos) : 0;
+#endif
+#endif
+ // constant time:
+ //transpose[(i<<MUL_SHIFT)+ j] |= (((int64_t)((w & (ONE << j)) << (WORD_SIZE-1-j)))>>(WORD_SIZE-1)) & (ONE<<k);
+ }
+ }
+}
+
+void bs_transpose_rev(word_t * blocks)
+{
+ int i,k;
+ word_t w;
+ word_t transpose[BLOCK_SIZE];
+ memset(transpose, 0, sizeof(transpose));
+ for(k=0; k < BLOCK_SIZE; k++)
+ {
+ w = blocks[k];
+ word_t bitpos = bs2be(ONE << (k % WORD_SIZE));
+ word_t offset = k / WORD_SIZE;
+#ifndef UNROLL_TRANSPOSE
+ int j;
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t bit = (w & (ONE << j)) ? (ONE << (k % WORD_SIZE)) : 0;
+ transpose[j * WORDS_PER_BLOCK + (offset)] |= bit;
+ }
+#else
+ transpose[0 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 0 )) ? bitpos : 0;
+ transpose[1 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 1 )) ? bitpos : 0;
+ transpose[2 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 2 )) ? bitpos : 0;
+ transpose[3 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 3 )) ? bitpos : 0;
+ transpose[4 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 4 )) ? bitpos : 0;
+ transpose[5 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 5 )) ? bitpos : 0;
+ transpose[6 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 6 )) ? bitpos : 0;
+ transpose[7 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 7 )) ? bitpos : 0;
+#if WORD_SIZE > 8
+ transpose[8 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 8 )) ? bitpos : 0;
+ transpose[9 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 9 )) ? bitpos : 0;
+ transpose[10 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 10)) ? bitpos : 0;
+ transpose[11 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 11)) ? bitpos : 0;
+ transpose[12 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 12)) ? bitpos : 0;
+ transpose[13 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 13)) ? bitpos : 0;
+ transpose[14 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 14)) ? bitpos : 0;
+ transpose[15 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 15)) ? bitpos : 0;
+#endif
+#if WORD_SIZE > 16
+ transpose[16 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 16)) ? bitpos : 0;
+ transpose[17 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 17)) ? bitpos : 0;
+ transpose[18 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 18)) ? bitpos : 0;
+ transpose[19 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 19)) ? bitpos : 0;
+ transpose[20 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 20)) ? bitpos : 0;
+ transpose[21 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 21)) ? bitpos : 0;
+ transpose[22 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 22)) ? bitpos : 0;
+ transpose[23 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 23)) ? bitpos : 0;
+ transpose[24 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 24)) ? bitpos : 0;
+ transpose[25 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 25)) ? bitpos : 0;
+ transpose[26 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 26)) ? bitpos : 0;
+ transpose[27 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 27)) ? bitpos : 0;
+ transpose[28 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 28)) ? bitpos : 0;
+ transpose[29 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 29)) ? bitpos : 0;
+ transpose[30 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 30)) ? bitpos : 0;
+ transpose[31 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 31)) ? bitpos : 0;
+#endif
+#if WORD_SIZE > 32
+ transpose[32 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 32)) ? bitpos : 0;
+ transpose[33 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 33)) ? bitpos : 0;
+ transpose[34 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 34)) ? bitpos : 0;
+ transpose[35 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 35)) ? bitpos : 0;
+ transpose[36 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 36)) ? bitpos : 0;
+ transpose[37 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 37)) ? bitpos : 0;
+ transpose[38 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 38)) ? bitpos : 0;
+ transpose[39 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 39)) ? bitpos : 0;
+ transpose[40 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 40)) ? bitpos : 0;
+ transpose[41 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 41)) ? bitpos : 0;
+ transpose[42 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 42)) ? bitpos : 0;
+ transpose[43 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 43)) ? bitpos : 0;
+ transpose[44 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 44)) ? bitpos : 0;
+ transpose[45 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 45)) ? bitpos : 0;
+ transpose[46 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 46)) ? bitpos : 0;
+ transpose[47 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 47)) ? bitpos : 0;
+ transpose[48 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 48)) ? bitpos : 0;
+ transpose[49 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 49)) ? bitpos : 0;
+ transpose[50 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 50)) ? bitpos : 0;
+ transpose[51 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 51)) ? bitpos : 0;
+ transpose[52 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 52)) ? bitpos : 0;
+ transpose[53 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 53)) ? bitpos : 0;
+ transpose[54 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 54)) ? bitpos : 0;
+ transpose[55 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 55)) ? bitpos : 0;
+ transpose[56 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 56)) ? bitpos : 0;
+ transpose[57 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 57)) ? bitpos : 0;
+ transpose[58 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 58)) ? bitpos : 0;
+ transpose[59 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 59)) ? bitpos : 0;
+ transpose[60 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 60)) ? bitpos : 0;
+ transpose[61 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 61)) ? bitpos : 0;
+ transpose[62 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 62)) ? bitpos : 0;
+ transpose[63 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 63)) ? bitpos : 0;
+#endif
+#endif
+ }
+ memmove(blocks,transpose,sizeof(transpose));
+}
+
+
+#define R0 0
+#define R1 8
+#define R2 16
+#define R3 24
+
+#define B0 0
+#define B1 32
+#define B2 64
+#define B3 96
+
+#define R0_shift (BLOCK_SIZE/4)*0
+#define R1_shift (BLOCK_SIZE/4)*1
+#define R2_shift (BLOCK_SIZE/4)*2
+#define R3_shift (BLOCK_SIZE/4)*3
+#define B_MOD (BLOCK_SIZE)
+
+
+void bs_shiftrows(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ word_t * Br0 = B + 0;
+ word_t * Br1 = B + 32;
+ word_t * Br2 = B + 64;
+ word_t * Br3 = B + 96;
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+
+ int i;
+ for(i=0; i<4; i++)
+ {
+ Bp[B0 + 0] = Br0[0];
+ Bp[B0 + 1] = Br0[1];
+ Bp[B0 + 2] = Br0[2];
+ Bp[B0 + 3] = Br0[3];
+ Bp[B0 + 4] = Br0[4];
+ Bp[B0 + 5] = Br0[5];
+ Bp[B0 + 6] = Br0[6];
+ Bp[B0 + 7] = Br0[7];
+ Bp[B1 + 0] = Br1[0];
+ Bp[B1 + 1] = Br1[1];
+ Bp[B1 + 2] = Br1[2];
+ Bp[B1 + 3] = Br1[3];
+ Bp[B1 + 4] = Br1[4];
+ Bp[B1 + 5] = Br1[5];
+ Bp[B1 + 6] = Br1[6];
+ Bp[B1 + 7] = Br1[7];
+ Bp[B2 + 0] = Br2[0];
+ Bp[B2 + 1] = Br2[1];
+ Bp[B2 + 2] = Br2[2];
+ Bp[B2 + 3] = Br2[3];
+ Bp[B2 + 4] = Br2[4];
+ Bp[B2 + 5] = Br2[5];
+ Bp[B2 + 6] = Br2[6];
+ Bp[B2 + 7] = Br2[7];
+ Bp[B3 + 0] = Br3[0];
+ Bp[B3 + 1] = Br3[1];
+ Bp[B3 + 2] = Br3[2];
+ Bp[B3 + 3] = Br3[3];
+ Bp[B3 + 4] = Br3[4];
+ Bp[B3 + 5] = Br3[5];
+ Bp[B3 + 6] = Br3[6];
+ Bp[B3 + 7] = Br3[7];
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+
+ Bp += 8;
+ }
+ memmove(B,Bp_space,sizeof(Bp_space));
+}
+
+
+void bs_shiftrows_rev(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ word_t * Br0 = Bp + 0;
+ word_t * Br1 = Bp + 32;
+ word_t * Br2 = Bp + 64;
+ word_t * Br3 = Bp + 96;
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+
+ int i;
+ for(i=0; i<4; i++)
+ {
+ Br0[0] = B[B0 + 0];
+ Br0[1] = B[B0 + 1];
+ Br0[2] = B[B0 + 2];
+ Br0[3] = B[B0 + 3];
+ Br0[4] = B[B0 + 4];
+ Br0[5] = B[B0 + 5];
+ Br0[6] = B[B0 + 6];
+ Br0[7] = B[B0 + 7];
+ Br1[0] = B[B1 + 0];
+ Br1[1] = B[B1 + 1];
+ Br1[2] = B[B1 + 2];
+ Br1[3] = B[B1 + 3];
+ Br1[4] = B[B1 + 4];
+ Br1[5] = B[B1 + 5];
+ Br1[6] = B[B1 + 6];
+ Br1[7] = B[B1 + 7];
+ Br2[0] = B[B2 + 0];
+ Br2[1] = B[B2 + 1];
+ Br2[2] = B[B2 + 2];
+ Br2[3] = B[B2 + 3];
+ Br2[4] = B[B2 + 4];
+ Br2[5] = B[B2 + 5];
+ Br2[6] = B[B2 + 6];
+ Br2[7] = B[B2 + 7];
+ Br3[0] = B[B3 + 0];
+ Br3[1] = B[B3 + 1];
+ Br3[2] = B[B3 + 2];
+ Br3[3] = B[B3 + 3];
+ Br3[4] = B[B3 + 4];
+ Br3[5] = B[B3 + 5];
+ Br3[6] = B[B3 + 6];
+ Br3[7] = B[B3 + 7];
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/16 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = Bp + offsetr0;
+ Br1 = Bp + offsetr1;
+ Br2 = Bp + offsetr2;
+ Br3 = Bp + offsetr3;
+
+ B += 8;
+ }
+ memmove(B - 8 * 4,Bp_space,sizeof(Bp_space));
+}
+
+
+#define A0 0
+#define A1 8
+#define A2 16
+#define A3 24
+
+// Does shift rows and mix columns in same step
+void bs_shiftmix(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+
+ word_t * Br0 = B + 0;
+ word_t * Br1 = B + 32;
+ word_t * Br2 = B + 64;
+ word_t * Br3 = B + 96;
+
+ uint8_t offsetr0 = 0;
+ uint8_t offsetr1 = 32;
+ uint8_t offsetr2 = 64;
+ uint8_t offsetr3 = 96;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+
+
+ int i;
+ for (i = 0; i < 4; i++)
+ {
+ // B0
+ // 2*A0 2*A1 A1 A2 A3
+ word_t of =Br0[R0+7]^ Br1[R1+7];
+ Bp[A0+0] = Br1[R1+0] ^ Br2[R2+0] ^ Br3[R3+0] ^ of;
+ Bp[A0+1] = Br0[R0+0] ^ Br1[R1+0] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br3[R3+1] ^ of;
+ Bp[A0+2] = Br0[R0+1] ^ Br1[R1+1] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br3[R3+2];
+ Bp[A0+3] = Br0[R0+2] ^ Br1[R1+2] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br3[R3+3] ^ of;
+ Bp[A0+4] = Br0[R0+3] ^ Br1[R1+3] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br3[R3+4] ^ of;
+ Bp[A0+5] = Br0[R0+4] ^ Br1[R1+4] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br3[R3+5];
+ Bp[A0+6] = Br0[R0+5] ^ Br1[R1+5] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br3[R3+6];
+ Bp[A0+7] = Br0[R0+6] ^ Br1[R1+6] ^ Br1[R1+7] ^ Br2[R2+7] ^ Br3[R3+7];
+
+ // A0 2*A1 2*A2 A2 A3
+ of = Br1[R1+7] ^ Br2[R2+7];
+ Bp[A1+0] = Br0[R0+0] ^ Br2[R2+0] ^ Br3[R3+0] ^ of;
+ Bp[A1+1] = Br0[R0+1] ^ Br1[R1+0] ^ Br2[R2+0] ^ Br2[R2+1] ^ Br3[R3+1] ^ of;
+ Bp[A1+2] = Br0[R0+2] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br2[R2+2] ^ Br3[R3+2];
+ Bp[A1+3] = Br0[R0+3] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br2[R2+3] ^ Br3[R3+3] ^ of;
+ Bp[A1+4] = Br0[R0+4] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br2[R2+4] ^ Br3[R3+4] ^ of;
+ Bp[A1+5] = Br0[R0+5] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br2[R2+5] ^ Br3[R3+5];
+ Bp[A1+6] = Br0[R0+6] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br2[R2+6] ^ Br3[R3+6];
+ Bp[A1+7] = Br0[R0+7] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br2[R2+7] ^ Br3[R3+7];
+
+ // A0 A1 2*A2 2*A3 A3
+ of = Br2[R2+7] ^ Br3[R3+7];
+ Bp[A2+0] = Br0[R0+0] ^ Br1[R1+0] ^ Br3[R3+0] ^ of;
+ Bp[A2+1] = Br0[R0+1] ^ Br1[R1+1] ^ Br2[R2+0] ^ Br3[R3+0] ^ Br3[R3+1] ^ of;
+ Bp[A2+2] = Br0[R0+2] ^ Br1[R1+2] ^ Br2[R2+1] ^ Br3[R3+1] ^ Br3[R3+2];
+ Bp[A2+3] = Br0[R0+3] ^ Br1[R1+3] ^ Br2[R2+2] ^ Br3[R3+2] ^ Br3[R3+3] ^ of;
+ Bp[A2+4] = Br0[R0+4] ^ Br1[R1+4] ^ Br2[R2+3] ^ Br3[R3+3] ^ Br3[R3+4] ^ of;
+ Bp[A2+5] = Br0[R0+5] ^ Br1[R1+5] ^ Br2[R2+4] ^ Br3[R3+4] ^ Br3[R3+5];
+ Bp[A2+6] = Br0[R0+6] ^ Br1[R1+6] ^ Br2[R2+5] ^ Br3[R3+5] ^ Br3[R3+6];
+ Bp[A2+7] = Br0[R0+7] ^ Br1[R1+7] ^ Br2[R2+6] ^ Br3[R3+6] ^ Br3[R3+7];
+
+ // A0 2*A0 A1 A2 2*A3
+ of = Br0[R0+7] ^ Br3[R3+7];
+ Bp[A3+0] = Br0[R0+0] ^ Br1[R1+0] ^ Br2[R2+0] ^ of;
+ Bp[A3+1] = Br0[R0+1] ^ Br0[R0+0] ^ Br1[R1+1] ^ Br2[R2+1] ^ Br3[R3+0] ^ of;
+ Bp[A3+2] = Br0[R0+2] ^ Br0[R0+1] ^ Br1[R1+2] ^ Br2[R2+2] ^ Br3[R3+1];
+ Bp[A3+3] = Br0[R0+3] ^ Br0[R0+2] ^ Br1[R1+3] ^ Br2[R2+3] ^ Br3[R3+2] ^ of;
+ Bp[A3+4] = Br0[R0+4] ^ Br0[R0+3] ^ Br1[R1+4] ^ Br2[R2+4] ^ Br3[R3+3] ^ of;
+ Bp[A3+5] = Br0[R0+5] ^ Br0[R0+4] ^ Br1[R1+5] ^ Br2[R2+5] ^ Br3[R3+4];
+ Bp[A3+6] = Br0[R0+6] ^ Br0[R0+5] ^ Br1[R1+6] ^ Br2[R2+6] ^ Br3[R3+5];
+ Bp[A3+7] = Br0[R0+7] ^ Br0[R0+6] ^ Br1[R1+7] ^ Br2[R2+7] ^ Br3[R3+6];
+
+ Bp += BLOCK_SIZE/4;
+
+ offsetr0 = (offsetr0 + BLOCK_SIZE/4) & 0x7f;
+ offsetr1 = (offsetr1 + BLOCK_SIZE/4) & 0x7f;
+ offsetr2 = (offsetr2 + BLOCK_SIZE/4) & 0x7f;
+ offsetr3 = (offsetr3 + BLOCK_SIZE/4) & 0x7f;
+
+ Br0 = B + offsetr0;
+ Br1 = B + offsetr1;
+ Br2 = B + offsetr2;
+ Br3 = B + offsetr3;
+ }
+
+ memmove(B,Bp_space,sizeof(Bp_space));
+}
+
+
+
+void bs_mixcolumns(word_t * B)
+{
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+ // to understand this, see
+ // https://en.wikipedia.org/wiki/Rijndael_mix_columns
+
+ int i = 0;
+ for (; i < 4; i++)
+ {
+ // of = A0 ^ A1;
+ // A0 = A0 ^ (0x1b & ((signed char)of>>7));
+
+ //// 2 * A0
+ // A0 = A0 ^ (A0 << 1)
+
+ //// + 3 * A1
+ // A0 = A0 ^ (A1)
+ // A0 = A0 ^ (A1<<1)
+
+ //// + A2 + A3
+ // A0 = A0 ^ (A2)
+ // A0 = A0 ^ (A3)
+ // A0.7 A1.7
+ word_t of = B[A0+7] ^ B[A1+7];
+
+ // 2*A0 2*A1 A1 A2 A3
+ Bp[A0+0] = B[A1+0] ^ B[A2+0] ^ B[A3+0] ^ of;
+ Bp[A0+1] = B[A0+0] ^ B[A1+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+1] ^ of;
+ Bp[A0+2] = B[A0+1] ^ B[A1+1] ^ B[A1+2] ^ B[A2+2] ^ B[A3+2];
+ Bp[A0+3] = B[A0+2] ^ B[A1+2] ^ B[A1+3] ^ B[A2+3] ^ B[A3+3] ^ of;
+ Bp[A0+4] = B[A0+3] ^ B[A1+3] ^ B[A1+4] ^ B[A2+4] ^ B[A3+4] ^ of;
+ Bp[A0+5] = B[A0+4] ^ B[A1+4] ^ B[A1+5] ^ B[A2+5] ^ B[A3+5];
+ Bp[A0+6] = B[A0+5] ^ B[A1+5] ^ B[A1+6] ^ B[A2+6] ^ B[A3+6];
+ Bp[A0+7] = B[A0+6] ^ B[A1+6] ^ B[A1+7] ^ B[A2+7] ^ B[A3+7];
+
+
+
+ // of = A1 ^ A2
+ // A1 = A1 ^ (0x1b & ((signed char)of>>7));
+
+ //// A0
+ // A1 = A1 ^ (A0)
+
+ //// + 2 * A1
+ // A1 = A1 ^ (A1 << 1)
+
+ //// + 3 * A2
+ // A1 = A1 ^ (A2)
+ // A1 = A1 ^ (A2<<1)
+
+ //// + A3
+ // A1 = A1 ^ (A3)
+
+ of = B[A1+7] ^ B[A2+7];
+
+ // A0 2*A1 2*A2 A2 A3
+ Bp[A1+0] = B[A0+0] ^ B[A2+0] ^ B[A3+0] ^ of;
+ Bp[A1+1] = B[A0+1] ^ B[A1+0] ^ B[A2+0] ^ B[A2+1] ^ B[A3+1] ^ of;
+ Bp[A1+2] = B[A0+2] ^ B[A1+1] ^ B[A2+1] ^ B[A2+2] ^ B[A3+2];
+ Bp[A1+3] = B[A0+3] ^ B[A1+2] ^ B[A2+2] ^ B[A2+3] ^ B[A3+3] ^ of;
+ Bp[A1+4] = B[A0+4] ^ B[A1+3] ^ B[A2+3] ^ B[A2+4] ^ B[A3+4] ^ of;
+ Bp[A1+5] = B[A0+5] ^ B[A1+4] ^ B[A2+4] ^ B[A2+5] ^ B[A3+5];
+ Bp[A1+6] = B[A0+6] ^ B[A1+5] ^ B[A2+5] ^ B[A2+6] ^ B[A3+6];
+ Bp[A1+7] = B[A0+7] ^ B[A1+6] ^ B[A2+6] ^ B[A2+7] ^ B[A3+7];
+
+
+ // of = A2 ^ A3
+ // A2 = A2 ^ (0x1b & ((signed char)of>>7));
+
+ //// A0 + A1
+ // A2 = A2 ^ (A0)
+ // A2 = A2 ^ (A1)
+
+ //// + 2 * A2
+ // A2 = A2 ^ (A2 << 1)
+
+ //// + 3 * A3
+ // A2 = A2 ^ (A3)
+ // A2 = A2 ^ (A3<<1)
+
+
+ of = B[A2+7] ^ B[A3+7];
+
+ // A0 A1 2*A2 2*A3 A3
+ Bp[A2+0] = B[A0+0] ^ B[A1+0] ^ B[A3+0] ^ of;
+ Bp[A2+1] = B[A0+1] ^ B[A1+1] ^ B[A2+0] ^ B[A3+0] ^ B[A3+1] ^ of;
+ Bp[A2+2] = B[A0+2] ^ B[A1+2] ^ B[A2+1] ^ B[A3+1] ^ B[A3+2];
+ Bp[A2+3] = B[A0+3] ^ B[A1+3] ^ B[A2+2] ^ B[A3+2] ^ B[A3+3] ^ of;
+ Bp[A2+4] = B[A0+4] ^ B[A1+4] ^ B[A2+3] ^ B[A3+3] ^ B[A3+4] ^ of;
+ Bp[A2+5] = B[A0+5] ^ B[A1+5] ^ B[A2+4] ^ B[A3+4] ^ B[A3+5];
+ Bp[A2+6] = B[A0+6] ^ B[A1+6] ^ B[A2+5] ^ B[A3+5] ^ B[A3+6];
+ Bp[A2+7] = B[A0+7] ^ B[A1+7] ^ B[A2+6] ^ B[A3+6] ^ B[A3+7];
+
+
+ // A3 = A0 ^ A3
+ // A3 = A3 ^ (0x1b & ((signed char)of>>7));
+
+ //// 3 * A0
+ // A3 = A3 ^ (A0)
+ // A3 = A3 ^ (A0 << 1)
+
+ //// + A1 + A2
+ // A3 = A3 ^ A1
+ // A3 = A3 ^ A2
+
+ //// + 2 * A3
+ // A3 = A3 ^ (A3<<1)
+
+ of = B[A0+7] ^ B[A3+7];
+
+ // 2*A0 A0 A1 A2 2*A3
+ Bp[A3+0] = B[A0+0] ^ B[A1+0] ^ B[A2+0] ^ of;
+ Bp[A3+1] = B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+0] ^ of;
+ Bp[A3+2] = B[A0+2] ^ B[A0+1] ^ B[A1+2] ^ B[A2+2] ^ B[A3+1];
+ Bp[A3+3] = B[A0+3] ^ B[A0+2] ^ B[A1+3] ^ B[A2+3] ^ B[A3+2] ^ of;
+ Bp[A3+4] = B[A0+4] ^ B[A0+3] ^ B[A1+4] ^ B[A2+4] ^ B[A3+3] ^ of;
+ Bp[A3+5] = B[A0+5] ^ B[A0+4] ^ B[A1+5] ^ B[A2+5] ^ B[A3+4];
+ Bp[A3+6] = B[A0+6] ^ B[A0+5] ^ B[A1+6] ^ B[A2+6] ^ B[A3+5];
+ Bp[A3+7] = B[A0+7] ^ B[A0+6] ^ B[A1+7] ^ B[A2+7] ^ B[A3+6];
+
+
+ //
+ Bp += BLOCK_SIZE/4;
+ B += BLOCK_SIZE/4;
+ }
+
+
+ memmove(B - BLOCK_SIZE,Bp - BLOCK_SIZE,sizeof(Bp_space));
+}
+
+void bs_mixcolumns_rev(word_t * B)
+{
+ // to understand this, see
+ // https://en.wikipedia.org/wiki/Rijndael_mix_columns
+ // TODO combine with shiftrows for performance on decryption
+ word_t Bp_space[BLOCK_SIZE];
+ word_t * Bp = Bp_space;
+
+
+ int i = 0;
+ for (; i < BLOCK_SIZE / 4; i += BLOCK_SIZE / 16)
+ {
+
+ //// state[i][0] = A0*0x0e + A1*0x0b + A2*0x0d + A3*0x09
+ // overflow:
+ /* A0 * 0b1110 */ /* A1 * 0b1011 */ /* A2 * 0b1101 */ /* A3 * 0b1001 */
+ word_t of0 = ( (B[A0+7] ^ B[A0+6] ^ B[A0+5]) ^ (B[A1 + 7] ^ B[A1+5]) ^ (B[A2+6] ^ B[A2+5]) ^ ( B[A3+5] )); // 2 bit
+ word_t of1 = ( (B[A0+7] ^ B[A0+6]) ^ ( B[A1+6]) ^ (B[A2+7] ^ B[A2+6]) ^ ( B[A3+6] )); // 3 bit
+ word_t of2 = ( (B[A0+7]) ^ ( B[A1+7]) ^ ( B[A2+7]) ^ ( B[A3+7] )); // 4 bit
+
+ // inverse:
+ // 1110 1011 1101 1001
+ // A0 = A0 * 14 + A1 * 11 + A2 * 13 + A3 * 9
+ // A0 = A0 * (2+4+8) + A1 * (1+2+8) + A2 * (1+4+8) + A3 * (1+8)
+
+ // (2*A0 + 4*A0 + 8*A0 ) + (8*A1 + 2*A1 + A1 ) + ( A2 + 4*A2 + 8*A2 ) + ( A3 + 8*A3)
+ Bp[A0+0] = B[A1+0] ^ B[A2+0] ^ B[A3+0] ^ of0;
+ Bp[A0+1] = B[A0+0] ^ B[A1+0] ^ B[A1+1] ^ B[A2+1] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A0+2] = B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ B[A1+2] ^ B[A2+2] ^ B[A2+0] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A0+3] = B[A0+2] ^ B[A0+1] ^ B[A0+0] ^ B[A1+0] ^ B[A1+2] ^ B[A1+3] ^ B[A2+3] ^ B[A2+1] ^ B[A2+0] ^ B[A3+3] ^ B[A3+0] ^ of0 ^ of2;
+ Bp[A0+4] = B[A0+3] ^ B[A0+2] ^ B[A0+1] ^ B[A1+1] ^ B[A1+3] ^ B[A1+4] ^ B[A2+4] ^ B[A2+2] ^ B[A2+1] ^ B[A3+4] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A0+5] = B[A0+4] ^ B[A0+3] ^ B[A0+2] ^ B[A1+2] ^ B[A1+4] ^ B[A1+5] ^ B[A2+5] ^ B[A2+3] ^ B[A2+2] ^ B[A3+5] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A0+6] = B[A0+5] ^ B[A0+4] ^ B[A0+3] ^ B[A1+3] ^ B[A1+5] ^ B[A1+6] ^ B[A2+6] ^ B[A2+4] ^ B[A2+3] ^ B[A3+6] ^ B[A3+3] ^ of2;
+ Bp[A0+7] = B[A0+6] ^ B[A0+5] ^ B[A0+4] ^ B[A1+4] ^ B[A1+6] ^ B[A1+7] ^ B[A2+7] ^ B[A2+5] ^ B[A2+4] ^ B[A3+7] ^ B[A3+4];
+
+
+
+ //// state[i][1] = A0*0x09 + A1*0xe + A2*0x0b + A3*0x0d
+ // overflow:
+ /* A0 * 0b1001 */ /* A1 * 0b1110 */ /* A2 * 0b101 1 */ /* A3 * 0b1101 */
+ of0 = ( (B[A0+5]) ^ (B[A1+7] ^ B[A1+6] ^ B[A1+5]) ^ (B[A2 + 7] ^ B[A2+5]) ^ (B[A3+6] ^ B[A3+5])); // 2 bit
+ of1 = ( (B[A0+6]) ^ (B[A1+7] ^ B[A1+6]) ^ ( B[A2+6]) ^ (B[A3+7] ^ B[A3+6])); // 3 bit
+ of2 = ( (B[A0+7]) ^ (B[A1+7]) ^ ( B[A2+7]) ^ ( B[A3+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A1 = A0 * 9 + A1 * 14 + A2 * 11 + A3 * 13
+ // A1 = A0 * (1+8) + A1 * (2+4+8) + A2 * (1+2+8) + A3 * (1+4+8)
+
+ // (1*A0 + 8*A0 ) +(2*A1 + 4*A1 + 8*A1 ) + (1*A2 + 2*A2 + 8*A2 ) + (1*A3 + 4*A3 + 8*A3)
+ Bp[A1+0] = B[A0+0] ^ B[A2+0] ^ B[A3+0] ^ of0;
+ Bp[A1+1] = B[A0+1] ^ B[A1+0] ^ B[A2+1] ^ B[A2+0] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A1+2] = B[A0+2] ^ B[A1+1] ^ B[A1+0] ^ B[A2+2] ^ B[A2+1] ^ B[A3+2] ^ B[A3+0] ^ of1 ^ of2;
+ Bp[A1+3] = B[A0+3] ^ B[A0+0] ^ B[A1+2] ^ B[A1+1] ^ B[A1+0] ^ B[A2+3] ^ B[A2+2] ^ B[A2+0] ^ B[A3+3] ^ B[A3+1] ^ B[A3+0] ^ of0 ^ of2;
+ Bp[A1+4] = B[A0+4] ^ B[A0+1] ^ B[A1+3] ^ B[A1+2] ^ B[A1+1] ^ B[A2+4] ^ B[A2+3] ^ B[A2+1] ^ B[A3+4] ^ B[A3+2] ^ B[A3+1] ^ of0 ^ of1;
+ Bp[A1+5] = B[A0+5] ^ B[A0+2] ^ B[A1+4] ^ B[A1+3] ^ B[A1+2] ^ B[A2+5] ^ B[A2+4] ^ B[A2+2] ^ B[A3+5] ^ B[A3+3] ^ B[A3+2] ^ of1 ^ of2;
+ Bp[A1+6] = B[A0+6] ^ B[A0+3] ^ B[A1+5] ^ B[A1+4] ^ B[A1+3] ^ B[A2+6] ^ B[A2+5] ^ B[A2+3] ^ B[A3+6] ^ B[A3+4] ^ B[A3+3] ^ of2;
+ Bp[A1+7] = B[A0+7] ^ B[A0+4] ^ B[A1+6] ^ B[A1+5] ^ B[A1+4] ^ B[A2+7] ^ B[A2+6] ^ B[A2+4] ^ B[A3+7] ^ B[A3+5] ^ B[A3+4];
+
+
+ //// state[i][2] = A0*0x0d + A1*0x09 + A2*0x0e + A3*0x0b
+ // overflow:
+ /* A1 * 0b1001 */ /* A2 * 0b1110 */ /* A3 * 0b1011 */ /* A0 * 0b1101 */
+ of0 = ( (B[A1+5]) ^ (B[A2+7] ^ B[A2+6] ^ B[A2+5]) ^ (B[A3 + 7] ^ B[A3+5]) ^ (B[A0+6] ^ B[A0+5])); // 2 bit
+ of1 = ( (B[A1+6]) ^ (B[A2+7] ^ B[A2+6]) ^ ( B[A3+6]) ^ (B[A0+7] ^ B[A0+6])); // 3 bit
+ of2 = ( (B[A1+7]) ^ (B[A2+7]) ^ ( B[A3+7]) ^ ( B[A0+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A2 = A1 * 9 + A2 * 14 + A3 * 11 + A0 * 13
+ // A2 = A1 * (1+8) + A2 * (2+4+8) + A3 * (1+2+8) + A0 * (1+4+8)
+
+ // (1*A1 + 8*A1) + ( 2*A2 + 4*A2 + 8*A2) + (1*A3 2*A2 + 8*A2) + (1*A0 + 4*A0 + 8*A0)
+ Bp[A2+0] = B[A1+0] ^ B[A3+0] ^ B[A0+0] ^ of0;
+ Bp[A2+1] = B[A1+1] ^ B[A2+0] ^ B[A3+1] ^ B[A3+0] ^ B[A0+1] ^ of0 ^ of1;
+ Bp[A2+2] = B[A1+2] ^ B[A2+1] ^ B[A2+0] ^ B[A3+2] ^ B[A3+1] ^ B[A0+2] ^ B[A0+0] ^ of1 ^ of2;
+ Bp[A2+3] = B[A1+3] ^ B[A1+0] ^ B[A2+2] ^ B[A2+1] ^ B[A2+0] ^ B[A3+3] ^ B[A3+2] ^ B[A3+0] ^ B[A0+3] ^ B[A0+1] ^ B[A0+0] ^ of0 ^ of2;
+ Bp[A2+4] = B[A1+4] ^ B[A1+1] ^ B[A2+3] ^ B[A2+2] ^ B[A2+1] ^ B[A3+4] ^ B[A3+3] ^ B[A3+1] ^ B[A0+4] ^ B[A0+2] ^ B[A0+1] ^ of0 ^ of1;
+ Bp[A2+5] = B[A1+5] ^ B[A1+2] ^ B[A2+4] ^ B[A2+3] ^ B[A2+2] ^ B[A3+5] ^ B[A3+4] ^ B[A3+2] ^ B[A0+5] ^ B[A0+3] ^ B[A0+2] ^ of1 ^ of2;
+ Bp[A2+6] = B[A1+6] ^ B[A1+3] ^ B[A2+5] ^ B[A2+4] ^ B[A2+3] ^ B[A3+6] ^ B[A3+5] ^ B[A3+3] ^ B[A0+6] ^ B[A0+4] ^ B[A0+3] ^ of2;
+ Bp[A2+7] = B[A1+7] ^ B[A1+4] ^ B[A2+6] ^ B[A2+5] ^ B[A2+4] ^ B[A3+7] ^ B[A3+6] ^ B[A3+4] ^ B[A0+7] ^ B[A0+5] ^ B[A0+4];
+
+
+
+ //// state[i][3] = A0*0x0b + A1*0x0d + A2*0x09 + A3*0x0e
+ // overflow:
+ /* A2 * 0b1001 */ /* A3 * 0b1110 */ /* A0 * 0b1011 */ /* A1 * 0b1101 */
+ of0 = ( (B[A2+5]) ^ (B[A3+7] ^ B[A3+6] ^ B[A3+5]) ^ (B[A0 + 7] ^ B[A0+5]) ^ (B[A1+6] ^ B[A1+5])); // 2 bit
+ of1 = ( (B[A2+6]) ^ (B[A3+7] ^ B[A3+6]) ^ ( B[A0+6]) ^ (B[A1+7] ^ B[A1+6])); // 3 bit
+ of2 = ( (B[A2+7]) ^ (B[A3+7]) ^ ( B[A0+7]) ^ ( B[A1+7])); // 4 bit
+
+ // inverse:
+ // 1001 1110 1011 1101
+ // A2 = A2 * 9 + A3 * 14 + A0 * 11 + A1 * 13
+ // A2 = A2 * (1+8) + A3 * (2+4+8) + A0 * (1+2+8) + A1 * (1+4+8)
+
+ // (1*A2 + 8*A2) + ( 2*A3 + 4*A3 + 8*A3) + (1*A0 2*A0 + 8*A0) + (1*A1 + 4*A1 + 8*A1)
+ Bp[A3+0] = B[A2+0] ^ B[A0+0] ^ B[A1+0] ^ of0;
+ Bp[A3+1] = B[A2+1] ^ B[A3+0] ^ B[A0+1] ^ B[A0+0] ^ B[A1+1] ^ of0 ^ of1;
+ Bp[A3+2] = B[A2+2] ^ B[A3+1] ^ B[A3+0] ^ B[A0+2] ^ B[A0+1] ^ B[A1+2] ^ B[A1+0] ^ of1 ^ of2;
+ Bp[A3+3] = B[A2+3] ^ B[A2+0] ^ B[A3+2] ^ B[A3+1] ^ B[A3+0] ^ B[A0+3] ^ B[A0+2] ^ B[A0+0] ^ B[A1+3] ^ B[A1+1] ^ B[A1+0] ^ of0 ^ of2;
+ Bp[A3+4] = B[A2+4] ^ B[A2+1] ^ B[A3+3] ^ B[A3+2] ^ B[A3+1] ^ B[A0+4] ^ B[A0+3] ^ B[A0+1] ^ B[A1+4] ^ B[A1+2] ^ B[A1+1] ^ of0 ^ of1;
+ Bp[A3+5] = B[A2+5] ^ B[A2+2] ^ B[A3+4] ^ B[A3+3] ^ B[A3+2] ^ B[A0+5] ^ B[A0+4] ^ B[A0+2] ^ B[A1+5] ^ B[A1+3] ^ B[A1+2] ^ of1 ^ of2;
+ Bp[A3+6] = B[A2+6] ^ B[A2+3] ^ B[A3+5] ^ B[A3+4] ^ B[A3+3] ^ B[A0+6] ^ B[A0+5] ^ B[A0+3] ^ B[A1+6] ^ B[A1+4] ^ B[A1+3] ^ of2;
+ Bp[A3+7] = B[A2+7] ^ B[A2+4] ^ B[A3+6] ^ B[A3+5] ^ B[A3+4] ^ B[A0+7] ^ B[A0+6] ^ B[A0+4] ^ B[A1+7] ^ B[A1+5] ^ B[A1+4];
+
+ Bp += BLOCK_SIZE/4;
+ B += BLOCK_SIZE/4;
+ }
+
+ memmove(B - BLOCK_SIZE, Bp - BLOCK_SIZE,sizeof(Bp_space));
+
+}
+
+void bs_expand_key(word_t (* rk)[BLOCK_SIZE], uint8_t * _key)
+{
+ // TODO integrate this better
+ uint8_t key[KEY_SCHEDULE_SIZE];
+ memmove(key,_key,BLOCK_SIZE/8);
+ expand_key(key);
+
+ int i, j = 0, k, l;
+ for (i = 0; i < KEY_SCHEDULE_SIZE; i += (BLOCK_SIZE/8))
+ {
+ memmove(rk[j], key + i, BLOCK_SIZE / 8);
+
+ for (k = WORDS_PER_BLOCK; k < 128; k += WORDS_PER_BLOCK)
+ {
+ for (l = 0; l < WORDS_PER_BLOCK; l++)
+ {
+ rk[j][k + l] = rk[j][l];
+ }
+ }
+ bs_transpose(rk[j]);
+ j++;
+ }
+
+}
+
+void bs_cipher(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE])
+{
+ int round;
+ bs_transpose(state);
+
+
+ bs_addroundkey(state,rk[0]);
+ for (round = 1; round < 10; round++)
+ {
+ bs_apply_sbox(state);
+ /*bs_shiftrows(state);*/
+ /*bs_mixcolumns(state);*/
+ bs_shiftmix(state);
+ bs_addroundkey(state,rk[round]);
+ }
+ bs_apply_sbox(state);
+ bs_shiftrows(state);
+ bs_addroundkey(state,rk[10]);
+ bs_transpose_rev(state);
+}
+
+void bs_cipher_rev(word_t state[BLOCK_SIZE], word_t (* rk)[BLOCK_SIZE])
+{
+ int round;
+ bs_transpose(state);
+
+ bs_addroundkey(state,rk[10]);
+ for (round = 9; round > 0; round--)
+ {
+ bs_shiftrows_rev(state);
+ bs_apply_sbox_rev(state);
+ bs_addroundkey(state,rk[round]);
+ bs_mixcolumns_rev(state);
+ }
+ bs_shiftrows_rev(state);
+ bs_apply_sbox_rev(state);
+ bs_addroundkey(state,rk[0]);
+
+ bs_transpose_rev(state);
+}
+
+void aes_ecb_test()
+{
+ uint8_t key_vector[16] = "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c";
+ uint8_t pt_vector[16] = "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a";
+ uint8_t ct_vector[16] = "\x3a\xd7\x7b\xb4\x0d\x7a\x36\x60\xa8\x9e\xca\xf3\x24\x66\xef\x97";
+ uint8_t output[16];
+ uint8_t input[16];
+
+ printf("AES ECB\n");
+
+ aes_ecb_encrypt(output, pt_vector,16,key_vector);
+
+
+ printf("cipher text: \n");
+ dump_hex(output, 16);
+
+ aes_ecb_decrypt(input, output, 16, key_vector);
+
+ printf("plain text: \n");
+ dump_hex((uint8_t * )input,16);
+
+ if (memcmp(pt_vector, input, 16) != 0)
+ {
+ fprintf(stderr,"error: decrypted ciphertext is not the same as the input plaintext\n");
+ EXIT1;
+ }
+ else if (memcmp(ct_vector, output, 16) != 0)
+ {
+ fprintf(stderr,"error: ciphertext is not the same as the test vector\n");
+ EXIT1;
+ }
+ else
+ {
+ printf("ECB passes test vector\n\n");
+ }
+}
+
+void aes_ctr_test()
+{
+// Test vector from NIST for 4 input blocks
+#define AES_CTR_TESTS_BYTES 64
+
+ uint8_t key_vector[16] =
+ "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c";
+
+ uint8_t iv_vector[16] =
+ "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
+
+ uint8_t pt_vector[AES_CTR_TESTS_BYTES] =
+ "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
+ "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
+ "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
+ "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10"
+ ;
+
+ uint8_t ct_vector[AES_CTR_TESTS_BYTES] =
+ "\x87\x4d\x61\x91\xb6\x20\xe3\x26\x1b\xef\x68\x64\x99\x0d\xb6\xce"
+ "\x98\x06\xf6\x6b\x79\x70\xfd\xff\x86\x17\x18\x7b\xb9\xff\xfd\xff"
+ "\x5a\xe4\xdf\x3e\xdb\xd5\xd3\x5e\x5b\x4f\x09\x02\x0d\xb0\x3e\xab"
+ "\x1e\x03\x1d\xda\x2f\xbe\x03\xd1\x79\x21\x70\xa0\xf3\x00\x9c\xee"
+ ;
+
+ uint8_t output[AES_CTR_TESTS_BYTES];
+ uint8_t input[AES_CTR_TESTS_BYTES];
+
+ printf("AES CTR\n");
+
+ aes_ctr_encrypt(output,pt_vector,AES_CTR_TESTS_BYTES,key_vector, iv_vector);
+
+ printf("cipher text: \n");
+ dump_hex(output,AES_CTR_TESTS_BYTES);
+
+ aes_ctr_decrypt(input,output,AES_CTR_TESTS_BYTES,key_vector, iv_vector);
+
+ printf("plain text: \n");
+ dump_hex(input,AES_CTR_TESTS_BYTES);
+
+ if (memcmp(pt_vector, input, AES_CTR_TESTS_BYTES) != 0)
+ {
+ fprintf(stderr,"error: decrypted ciphertext is not the same as the input plaintext\n");
+ EXIT1;
+ }
+ else if (memcmp(ct_vector, output, AES_CTR_TESTS_BYTES) != 0)
+ {
+ fprintf(stderr,"error: ciphertext is not the same as the test vector\n");
+ EXIT1;
+ }
+ else
+ {
+ printf("CTR passes test vector\n\n");
+ }
+
+}
+
+
+int main(int argc, char * argv[])
+{
+ clock_prepare();
+
+ clock_start();
+
+ aes_ecb_test();
+ aes_ctr_test();
+
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+}
diff --git a/test/monniaux/bitsliced-aes/one_file/compare.sh b/test/monniaux/bitsliced-aes/one_file/compare.sh
new file mode 100755
index 00000000..314c1718
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/one_file/compare.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+ROOT=/home/monniaux/work/Kalray/CompCert
+SRC=bitsliced-aes.c
+k1-cos-gcc -Werror=implicit -Werror=uninitialized -O3 $SRC $ROOT/test/monniaux/clock.gcc.kvx.o -o bitsliced-aes.gcc.kvx &&
+$ROOT/ccomp -O3 -fno-unprototyped -O3 $SRC $ROOT/test/monniaux/clock.gcc.kvx.o -o bitsliced-aes.ccomp.kvx &&
+gcc -Werror=implicit -Werror=uninitialized -O3 $SRC $ROOT/test/monniaux/clock.gcc.host.o -o bitsliced-aes.gcc.host &&
+valgrind ./bitsliced-aes.gcc.host &&
+k1-cluster -- ./bitsliced-aes.gcc.kvx > ./bitsliced-aes.gcc.kvx.out &&
+k1-cluster -- ./bitsliced-aes.ccomp.kvx > ./bitsliced-aes.ccomp.kvx.out &&
+grep cycles ./bitsliced-aes.gcc.kvx.out | sed -e 's/cycles: //' > ./bitsliced-aes.gcc.kvx.cycles &&
+grep cycles ./bitsliced-aes.ccomp.kvx.out | sed -e 's/cycles: //' > ./bitsliced-aes.ccomp.kvx.cycles &&
+test $(cat ./bitsliced-aes.ccomp.kvx.cycles) -gt $(expr 2 '*' $(cat ./bitsliced-aes.gcc.kvx.cycles))
diff --git a/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_compute.c b/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_compute.c
new file mode 100644
index 00000000..5294ff1d
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_compute.c
@@ -0,0 +1,32 @@
+#include <stdint.h>
+#include <string.h>
+int i[1];
+int j, bs_transpose_dst_k, k, s, o;
+void a(int (*)[], uint8_t *);
+void b(uint8_t c, uint8_t d, size_t e, uint8_t f, uint8_t g) {
+ int l[1];
+ a(l, f);
+}
+void a(int (*l)[], uint8_t *m) {
+ for (; o < 76; o += 8) {
+ {
+ int *n = i;
+ bs_transpose_dst_k = 0;
+ for (; bs_transpose_dst_k < 64; bs_transpose_dst_k++) {
+ j = 0;
+ for (; j < 64; j++) {
+ k = &s;
+ n[j] = k & 1;
+ }
+ }
+ }
+ }
+}
+void aes_ecb_test() {}
+void aes_ctr_test() {
+ uint8_t p = "";
+ uint8_t q = "";
+ uint8_t r = "";
+ uint8_t output[4];
+ b(output, r, 4, p, q);
+}
diff --git a/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_main.c b/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_main.c
new file mode 100644
index 00000000..0d48b3b8
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_main.c
@@ -0,0 +1,20 @@
+#include "/home/monniaux/work/Kalray/CompCert/test/monniaux/clock.h"
+
+void aes_ecb_test(void);
+void aes_ctr_test(void);
+
+int main(int argc, char * argv[])
+{
+ clock_prepare();
+
+ clock_start();
+
+ aes_ecb_test();
+ aes_ctr_test();
+
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+}
diff --git a/test/monniaux/bitsliced-aes/one_file/reduce/compare.sh b/test/monniaux/bitsliced-aes/one_file/reduce/compare.sh
new file mode 100755
index 00000000..0f61ad00
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/one_file/reduce/compare.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+ROOT=/home/monniaux/work/Kalray/CompCert
+SRC=bitsliced-aes_compute.c
+MAIN=/home/monniaux/work/Kalray/CompCert/test/monniaux/bitsliced-aes/one_file/reduce/bitsliced-aes_main
+k1-cos-gcc -Werror=implicit -Werror=uninitialized -O3 $SRC $ROOT/test/monniaux/clock.gcc.kvx.o $MAIN.gcc.kvx.o -o bitsliced-aes.gcc.kvx &&
+$ROOT/ccomp -O3 -fno-unprototyped -O3 $SRC $ROOT/test/monniaux/clock.gcc.kvx.o $MAIN.gcc.kvx.o -o bitsliced-aes.ccomp.kvx &&
+gcc -Werror=implicit -Werror=uninitialized -O3 $SRC $ROOT/test/monniaux/clock.gcc.host.o $MAIN.c -o bitsliced-aes.gcc.host &&
+valgrind ./bitsliced-aes.gcc.host &&
+k1-cluster --cycle-based -- ./bitsliced-aes.gcc.kvx > ./bitsliced-aes.gcc.kvx.out &&
+k1-cluster --cycle-based -- ./bitsliced-aes.ccomp.kvx > ./bitsliced-aes.ccomp.kvx.out &&
+grep cycles ./bitsliced-aes.gcc.kvx.out > ./bitsliced-aes.gcc.kvx.cycles &&
+grep cycles ./bitsliced-aes.ccomp.kvx.out > ./bitsliced-aes.ccomp.kvx.cycles &&
+sed -i -e 's/cycles: //' ./bitsliced-aes.gcc.kvx.cycles &&
+sed -i -e 's/cycles: //' ./bitsliced-aes.ccomp.kvx.cycles &&
+test $(cat ./bitsliced-aes.gcc.kvx.cycles) -gt 100000 &&
+test $(cat ./bitsliced-aes.ccomp.kvx.cycles) -gt $(expr 2 '*' $(cat ./bitsliced-aes.gcc.kvx.cycles))
diff --git a/test/monniaux/bitsliced-aes/testbench/app.c b/test/monniaux/bitsliced-aes/testbench/app.c
new file mode 100644
index 00000000..71e2c3ce
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/testbench/app.c
@@ -0,0 +1,144 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <openssl/bn.h>
+#include <openssl/err.h>
+
+#include "app.h"
+#include "../bs.h"
+#include "../aes.h"
+
+static void openssl_die(void)
+{
+ fprintf(stderr,"error: %s\n",
+ ERR_error_string(ERR_get_error(),NULL) );
+ exit(2);
+}
+
+static unsigned int hex2bin(unsigned char ** bin, const char * hex)
+{
+ int len;
+ BIGNUM * bn = NULL;
+ if(BN_hex2bn(&bn, (char*)hex) == 0)
+ { openssl_die(); }
+
+ len = BN_num_bytes(bn);
+ *bin = (unsigned char *)malloc(len);
+
+ if(BN_bn2bin(bn, *bin) == 0)
+ { openssl_die(); }
+ return len;
+}
+
+
+int cli_app(int argc, char * argv[])
+{
+ uint8_t key[16];
+ uint8_t iv[16];
+
+ uint8_t * key_p = NULL, * iv_p = NULL;
+
+ int keylen, ivlen;
+
+ if (argc != 5)
+ {
+ fprintf(stderr,
+ "Bitsliced AES-CTR\n"
+ "usage: %s <key-hex> <iv-hex> <input-file> <output-file>\n", argv[0]);
+ exit(1);
+ }
+
+ char * key_s = argv[1];
+ char * iv_s = argv[2];
+ char * input_name = argv[3];
+ char * output_name = argv[4];
+
+ FILE * input = fopen(input_name, "r");
+ if (input == NULL)
+ {
+ perror("fopen");
+ exit(2);
+ }
+
+ FILE * output = fopen(output_name, "w+");
+ if (output == NULL)
+ {
+ perror("fopen");
+ exit(2);
+ }
+
+
+ ERR_load_crypto_strings();
+
+ keylen = hex2bin(&key_p,key_s);
+ if (keylen > 16)
+ {
+ free(key_p);
+ fprintf(stderr,"key must be 16 bytes or less\n");
+ exit(2);
+ }
+
+ ivlen = hex2bin(&iv_p,iv_s);
+ if (ivlen > 16)
+ {
+ free(iv_p);
+ free(key_p);
+ fprintf(stderr,"iv must be 16 bytes or less\n");
+ exit(2);
+ }
+
+ memset(iv,0,16);
+ memset(key,0,16);
+
+ memmove(iv, iv_p, ivlen);
+ memmove(key, key_p, keylen);
+
+ free(iv_p);
+ free(key_p);
+
+ fseek(input, 0L, SEEK_END);
+ size_t amt = ftell(input);
+ fseek(input, 0L, SEEK_SET);
+
+ uint8_t * pt = (uint8_t *) malloc(amt);
+ uint8_t * ct = (uint8_t *) malloc(amt);
+
+ int ptlen = fread(pt, 1, amt, input);
+ if (ptlen != amt)
+ {
+ perror("fread");
+ exit(2);
+ }
+
+ struct timespec tstart,tend;
+ clock_gettime(CLOCK_MONOTONIC, &tstart);
+ {
+ aes_ctr_encrypt(ct, pt, amt, key, iv);
+ }
+ clock_gettime(CLOCK_MONOTONIC, &tend);
+
+ double total = (((double)tend.tv_sec + 1.0e-9 * (double)tend.tv_nsec) -
+ ((double)tstart.tv_sec + 1.0e-9 * (double)tstart.tv_nsec));
+
+ printf("performance for %d word length\n", WORD_SIZE);
+ printf("-------------------------------\n");
+ printf("%.5f s\n", total);
+ printf("%.15f s/byte\n", total/amt);
+ printf("%.5f cycles/byte (for 4 GHz)\n", 4000ull * (1ull<<20) * total/amt);
+
+
+ if (write(fileno(output), ct, amt) == -1)
+ {
+ perror("write");
+ exit(2);
+ }
+
+ free(ct);
+ free(pt);
+
+ return 0;
+}
diff --git a/test/monniaux/bitsliced-aes/testbench/app.h b/test/monniaux/bitsliced-aes/testbench/app.h
new file mode 100644
index 00000000..29fdc279
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/testbench/app.h
@@ -0,0 +1,7 @@
+#ifndef _APP_H_
+#define _APP_H_
+
+
+int cli_app(int argc, char * argv[]);
+
+#endif
diff --git a/test/monniaux/bitsliced-aes/tests/tests.c b/test/monniaux/bitsliced-aes/tests/tests.c
new file mode 100644
index 00000000..d9218685
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/tests/tests.c
@@ -0,0 +1,108 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../utils.h"
+#include "../aes.h"
+
+#ifdef TEST_FOOTPRINT
+#define printf(fmt, ...) (0)
+#define fprintf(f,fmt, ...) (0)
+#else
+#include <stdio.h>
+#endif
+
+void aes_ecb_test()
+{
+ uint8_t key_vector[16] = "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c";
+ uint8_t pt_vector[16] = "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a";
+ uint8_t ct_vector[16] = "\x3a\xd7\x7b\xb4\x0d\x7a\x36\x60\xa8\x9e\xca\xf3\x24\x66\xef\x97";
+ uint8_t output[16];
+ uint8_t input[16];
+
+ printf("AES ECB\n");
+
+ aes_ecb_encrypt(output, pt_vector,16,key_vector);
+
+
+ printf("cipher text: \n");
+ dump_hex(output, 16);
+
+ aes_ecb_decrypt(input, output, 16, key_vector);
+
+ printf("plain text: \n");
+ dump_hex((uint8_t * )input,16);
+
+ if (memcmp(pt_vector, input, 16) != 0)
+ {
+ fprintf(stderr,"error: decrypted ciphertext is not the same as the input plaintext\n");
+ exit(1);
+ }
+ else if (memcmp(ct_vector, output, 16) != 0)
+ {
+ fprintf(stderr,"error: ciphertext is not the same as the test vector\n");
+ exit(1);
+ }
+ else
+ {
+ printf("ECB passes test vector\n\n");
+ }
+}
+
+void aes_ctr_test()
+{
+// Test vector from NIST for 4 input blocks
+#define AES_CTR_TESTS_BYTES 64
+
+ uint8_t key_vector[16] =
+ "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c";
+
+ uint8_t iv_vector[16] =
+ "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
+
+ uint8_t pt_vector[AES_CTR_TESTS_BYTES] =
+ "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
+ "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
+ "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
+ "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10"
+ ;
+
+ uint8_t ct_vector[AES_CTR_TESTS_BYTES] =
+ "\x87\x4d\x61\x91\xb6\x20\xe3\x26\x1b\xef\x68\x64\x99\x0d\xb6\xce"
+ "\x98\x06\xf6\x6b\x79\x70\xfd\xff\x86\x17\x18\x7b\xb9\xff\xfd\xff"
+ "\x5a\xe4\xdf\x3e\xdb\xd5\xd3\x5e\x5b\x4f\x09\x02\x0d\xb0\x3e\xab"
+ "\x1e\x03\x1d\xda\x2f\xbe\x03\xd1\x79\x21\x70\xa0\xf3\x00\x9c\xee"
+ ;
+
+ uint8_t output[AES_CTR_TESTS_BYTES];
+ uint8_t input[AES_CTR_TESTS_BYTES];
+
+ printf("AES CTR\n");
+
+ aes_ctr_encrypt(output,pt_vector,AES_CTR_TESTS_BYTES,key_vector, iv_vector);
+
+ printf("cipher text: \n");
+ dump_hex(output,AES_CTR_TESTS_BYTES);
+
+ aes_ctr_decrypt(input,output,AES_CTR_TESTS_BYTES,key_vector, iv_vector);
+
+ printf("plain text: \n");
+ dump_hex(input,AES_CTR_TESTS_BYTES);
+
+ if (memcmp(pt_vector, input, AES_CTR_TESTS_BYTES) != 0)
+ {
+ fprintf(stderr,"error: decrypted ciphertext is not the same as the input plaintext\n");
+ exit(1);
+ }
+ else if (memcmp(ct_vector, output, AES_CTR_TESTS_BYTES) != 0)
+ {
+ fprintf(stderr,"error: ciphertext is not the same as the test vector\n");
+ exit(1);
+ }
+ else
+ {
+ printf("CTR passes test vector\n\n");
+ }
+
+}
+
+
diff --git a/test/monniaux/bitsliced-aes/tests/tests.h b/test/monniaux/bitsliced-aes/tests/tests.h
new file mode 100644
index 00000000..a8710505
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/tests/tests.h
@@ -0,0 +1,8 @@
+#ifndef _TESTS_H_
+#define _TESTS_H_
+
+
+void aes_ecb_test(void);
+void aes_ctr_test(void);
+
+#endif
diff --git a/test/monniaux/bitsliced-aes/utils.c b/test/monniaux/bitsliced-aes/utils.c
new file mode 100644
index 00000000..3b375bb1
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/utils.c
@@ -0,0 +1,30 @@
+
+#ifndef TEST_FOOTPRINT
+#include <stdio.h>
+#include "utils.h"
+
+
+void dump_hex(uint8_t * h, int len)
+{
+ while(len--)
+ printf("%02hhx",*h++);
+ printf("\n");
+}
+
+void dump_word(word_t * h, int len)
+{
+ while(len--)
+ if ((len+1) % 8) printf("%" WPAD WFMT "\n",*h++);
+ else printf("%d:\n%" WPAD WFMT "\n",128-len-1,*h++);
+
+ printf("\n");
+}
+
+void dump_block(word_t * h, int len)
+{
+ while(len-=2 >= 0)
+ printf("%" WPAD WFMT"%" WPAD WFMT "\n",*h++,*h++);
+ printf("\n");
+}
+
+#endif
diff --git a/test/monniaux/bitsliced-aes/utils.h b/test/monniaux/bitsliced-aes/utils.h
new file mode 100644
index 00000000..167f0d95
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/utils.h
@@ -0,0 +1,20 @@
+#ifndef _UTILS_H_
+#define _UTILS_H_
+
+#include <stdint.h>
+#include "bs.h"
+
+#ifndef TEST_FOOTPRINT
+void dump_hex(uint8_t * h, int len);
+void dump_word(word_t * h, int len);
+void dump_block(word_t * h, int len);
+#else
+#define dump_hex(h,len) (0)
+#define dump_word(h,len) (0)
+#define dump_block(h,len) (0)
+#endif
+
+#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
+#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+
+#endif
diff --git a/test/monniaux/bitsliced-tea/Makefile b/test/monniaux/bitsliced-tea/Makefile
new file mode 100644
index 00000000..02b35381
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/Makefile
@@ -0,0 +1,3 @@
+TARGET=bitsliced-tea
+
+include ../rules.mk
diff --git a/test/monniaux/bitsliced-tea/README.md b/test/monniaux/bitsliced-tea/README.md
new file mode 100644
index 00000000..476880c2
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/README.md
@@ -0,0 +1,3 @@
+Bit-sliced TEA encryption
+from Alfonso De Gregorio, 06 December 2010
+https://web.archive.org/web/20131021214351/http://plaintext.crypto.lo.gy/article/378/untwisted-bit-sliced-tea-time
diff --git a/test/monniaux/bitsliced-tea/bstea.c b/test/monniaux/bitsliced-tea/bstea.c
new file mode 100644
index 00000000..43e29d45
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/bstea.c
@@ -0,0 +1,368 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include "bstea.h"
+
+/* a key schedule constant - 32/golden-ratio */
+static const uint32_t delta = 0x9e3779b9;
+
+/* v points to the wordsize-way vectorized plaintext,
+ * k to the vectorized key */
+/* input quantities are disposed in the following way:
+ v0 <- v[0..31] k0 <- k[0..31] k2 <- k[64..95]
+ v1 <- v[32..63] k1 <- k[32..63] k3 <- k[96..127]
+ */
+void encrypt(parallel_blocks_t v, const parallel_keys_t k, unsigned int r)
+{
+ /* Stride 32 between consecutive words in input quantities */
+# define offset_v0 0
+# define offset_v1 32
+# define offset_k0 0
+# define offset_k1 32
+# define offset_k2 64
+# define offset_k3 96
+
+ vector_width_t carry;
+ vector_width_t axorb;
+ vector_width_t aandb;
+ vector_width_t ai;
+ vector_width_t bi;
+ vector_width_t borrow;
+ vector_width_t notaandb;
+
+ vector_width_t v1_lshift_4[32];
+ vector_width_t v1_plus_sum[32]; /* term two */
+ vector_width_t v1_rshift_5[32];
+ vector_width_t v1_lshift_4_plus_k0[32]; /* term one */
+ vector_width_t v1_rshift_5_plus_k1[32]; /* term three */
+
+ vector_width_t v0_lshift_4[32];
+ vector_width_t v0_plus_sum[32]; /* term two */
+ vector_width_t v0_rshift_5[32];
+ vector_width_t v0_lshift_4_plus_k2[32]; /* term one */
+ vector_width_t v0_rshift_5_plus_k3[32]; /* term three */
+
+ vector_width_t shift;
+
+ int i;
+
+
+ /* setup */
+ uint32_t sum = 0;
+ for (i = 0; i < 32; ++i)
+ v1_lshift_4[i] = v1_plus_sum[i] = v1_rshift_5[i] = \
+ v1_lshift_4_plus_k0[i] = v1_rshift_5_plus_k1[i] = \
+ v0_lshift_4[i] = v0_plus_sum[i] = v0_rshift_5[i] = \
+ v0_lshift_4_plus_k2[i] = v0_rshift_5_plus_k3[i] = 0;
+
+
+ while (r > 0) {
+ sum += delta;
+
+ /* lshift v1 by 4 */
+ shift = 4;
+ for (i = 31; i >= 0; i--)
+ v1_lshift_4[i] = (i >= shift) ? v[offset_v1 + i - shift] : 0;
+
+ /* add k0 to v1_lshift_4 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v1_lshift_4[i];
+ bi = k[offset_k0 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_lshift_4_plus_k0[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* add delta sum to v1 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ /* VECTOR_AT_ONE where the ith bit of the sum is set */
+ /*
+ * Each iteration follows the first 32 elements
+ * in the expansion of multiples of 32/golden-ratio,
+ * or 32/(1+sqrt(5)/2
+ */
+ ai = TERNARY((sum & (1<<i)), VECTOR_AT_ONE, VECTOR_AT_ZERO);
+ bi = v[offset_v1 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_plus_sum[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* rshift v1 by 5 */
+ shift = 5;
+ for (i = 0; i < 32; ++i)
+ v1_rshift_5[i] = (i < (32 - shift)) ? v[offset_v1 + i + shift] : 0;
+
+ /* add k1 to v1_rshift_5 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v1_rshift_5[i];
+ bi = k[offset_k1 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_rshift_5_plus_k1[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* xor the three terms and increment v0 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v1_lshift_4_plus_k0[i] ^ v1_plus_sum[i] ^ v1_rshift_5_plus_k1[i];
+ bi = v[offset_v0 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v[offset_v0 + i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+
+
+ /* lshift v0 by 4 */
+ shift = 4;
+ for (i = 31; i >= 0; i--)
+ v0_lshift_4[i] = (i >= shift) ? v[offset_v0 + i - shift] : 0;
+
+ /* add k2 and v0_lshift_4 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v0_lshift_4[i];
+ bi = k[offset_k2 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_lshift_4_plus_k2[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* add delta sum to v0 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ /* VECTOR_AT_ONE where the ith bit of the sum is set */
+ ai = TERNARY((sum & (1<<i)), VECTOR_AT_ONE, VECTOR_AT_ZERO);
+ bi = v[offset_v0 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_plus_sum[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+
+ /* rshift v0 by 5 */
+ shift = 5;
+ for (i = 0; i < 32; ++i)
+ v0_rshift_5[i] = (i < (32 - shift)) ? v[offset_v0 + i + shift] : 0;
+
+ /* add k3 to v0_rshift_5 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v0_rshift_5[i];
+ bi = k[offset_k3 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_rshift_5_plus_k3[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* xor the three terms and increment v1 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v0_lshift_4_plus_k2[i] ^ v0_plus_sum[i] ^ v0_rshift_5_plus_k3[i];
+ bi = v[offset_v1 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v[offset_v1 + i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+
+ --r;
+ }
+}
+
+/* v points to the wordsize-way vectorized ciphertext,
+ * k to the vectorized key */
+/* input quantities are disposed in the following way:
+ v0 <- v[0..31] k0 <- k[0..31] k2 <- k[64..95]
+ v1 <- v[32..63] k1 <- k[32..63] k3 <- k[96..127]
+ */
+void decrypt(parallel_blocks_t v, const parallel_keys_t k, unsigned int r)
+{
+# define offset_v0 0
+# define offset_v1 32
+# define offset_k0 0
+# define offset_k1 32
+# define offset_k2 64
+# define offset_k3 96
+
+ vector_width_t carry;
+ vector_width_t axorb;
+ vector_width_t aandb;
+ vector_width_t ai;
+ vector_width_t bi;
+ vector_width_t borrow;
+ vector_width_t notaandb;
+
+ vector_width_t v1_lshift_4[32];
+ vector_width_t v1_plus_sum[32]; /* term two */
+ vector_width_t v1_rshift_5[32];
+ vector_width_t v1_lshift_4_plus_k0[32]; /* term one */
+ vector_width_t v1_rshift_5_plus_k1[32]; /* term three */
+
+ vector_width_t v0_lshift_4[32];
+ vector_width_t v0_plus_sum[32]; /* term two */
+ vector_width_t v0_rshift_5[32];
+ vector_width_t v0_lshift_4_plus_k2[32]; /* term one */
+ vector_width_t v0_rshift_5_plus_k3[32]; /* term three */
+
+ vector_width_t shift;
+
+ int i;
+
+ /* setup */
+ uint32_t sum = delta * r;
+
+ for (i = 0; i < 32; ++i)
+ v1_lshift_4[i] = v1_plus_sum[i] = v1_rshift_5[i] = \
+ v1_lshift_4_plus_k0[i] = v1_rshift_5_plus_k1[i] = \
+ v0_lshift_4[i] = v0_plus_sum[i] = v0_rshift_5[i] = \
+ v0_lshift_4_plus_k2[i] = v0_rshift_5_plus_k3[i] = 0;
+
+ while (r > 0) {
+ /* lshift v0 by 4 */
+ shift = 4;
+ for (i = 31; i >= 0; i--)
+ v0_lshift_4[i] = (i >= shift) ? v[offset_v0 + i - shift] : 0;
+
+ /* add k2 and v0_lshift_4 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v0_lshift_4[i];
+ bi = k[offset_k2 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_lshift_4_plus_k2[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* add delta sum to v0 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ /* VECTOR_AT_ONE where the ith bit of the sum is set */
+ ai = TERNARY((sum & (1<<i)), VECTOR_AT_ONE, VECTOR_AT_ZERO);
+ bi = v[offset_v0 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_plus_sum[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+
+ /* rshift v0 by 5 */
+ shift = 5;
+ for (i = 0; i < 32; ++i)
+ v0_rshift_5[i] = (i < (32 - shift)) ? v[offset_v0 + i + shift] : 0;
+
+ /* add k3 to v0_rshift_5 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v0_rshift_5[i];
+ bi = k[offset_k3 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v0_rshift_5_plus_k3[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* xor the three terms and decrement v1 */
+ borrow = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v[offset_v1 + i];
+ bi = v0_lshift_4_plus_k2[i] ^ v0_plus_sum[i] ^ v0_rshift_5_plus_k3[i];
+ notaandb = (ai ^ VECTOR_AT_ONE) & bi;
+ axorb = ai ^ bi;
+ v[offset_v1 + i] = axorb ^ borrow;
+ borrow = notaandb | ((ai ^ VECTOR_AT_ONE) & borrow) | (bi & borrow);
+ }
+
+
+
+
+ /* lshift v1 by 4 */
+ shift = 4;
+ for (i = 31; i >= 0; i--)
+ v1_lshift_4[i] = (i >= shift) ? v[offset_v1 + i - shift] : 0;
+
+ /* add k0 to v1_lshift_4 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v1_lshift_4[i];
+ bi = k[offset_k0 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_lshift_4_plus_k0[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* add delta sum to v1 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ /* VECTOR_AT_ONE where the ith bit of the sum is set */
+ ai = TERNARY((sum & (1<<i)), VECTOR_AT_ONE, VECTOR_AT_ZERO);
+ bi = v[offset_v1 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_plus_sum[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* rshift v1 by 5 */
+ shift = 5;
+ for (i = 0; i < 32; ++i)
+ v1_rshift_5[i] = (i < (32 - shift)) ? v[offset_v1 + i + shift] : 0;
+
+ /* add k1 to v1_rshift_5 */
+ carry = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v1_rshift_5[i];
+ bi = k[offset_k1 + i];
+ aandb = ai & bi;
+ axorb = ai ^ bi;
+ v1_rshift_5_plus_k1[i] = axorb ^ carry;
+ carry &= axorb;
+ carry |= aandb;
+ }
+
+ /* xor the three terms and decrement v0 */
+ borrow = 0;
+ for (i = 0;i < 32;++i) {
+ ai = v[offset_v0 + i];
+ bi = v1_lshift_4_plus_k0[i] ^ v1_plus_sum[i] ^ v1_rshift_5_plus_k1[i];
+ notaandb = (ai ^ VECTOR_AT_ONE) & bi;
+ axorb = ai ^ bi;
+ v[offset_v0 + i] = axorb ^ borrow;
+ borrow = notaandb | ((ai ^ VECTOR_AT_ONE) & borrow) | (bi & borrow);
+ }
+
+
+ sum -= delta;
+ --r;
+ }
+}
+
diff --git a/test/monniaux/bitsliced-tea/bstea.h b/test/monniaux/bitsliced-tea/bstea.h
new file mode 100644
index 00000000..15607464
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/bstea.h
@@ -0,0 +1,149 @@
+#ifndef __BSTEA_H
+#define __BSTEA_H
+
+#include <stdint.h>
+#include <limits.h>
+
+#include "bstea_wordsize.h"
+
+#define TERNARY(x, v1, v0) ((x) ? (v1) : (v0))
+
+#define TEA_ROUNDS 32
+
+#define TEA_BLOCK_SIZE 64
+#define TEA_KEY_SIZE 128
+
+#if __BSTEA_WORDSIZE == 64
+typedef uint64_t vector_width_t; /* 64-way bit-level vectorization */
+#define VECTOR_AT_ONE 0xffffffffffffffff
+#define VECTOR_AT_ZERO 0x0000000000000000
+#elif __BSTEA_WORDSIZE == 32
+typedef uint32_t vector_width_t; /* 32-way bit-level vectorization */
+#define VECTOR_AT_ONE 0xffffffff
+#define VECTOR_AT_ZERO 0x00000000
+#elif __BSTEA_WORDSIZE == 16
+typedef uint32_t vector_width_t; /* 16-way bit-level vectorization */
+#define VECTOR_AT_ONE 0xffff
+#define VECTOR_AT_ZERO 0x0000
+#elif __BSTEA_WORDSIZE == 8
+typedef uint32_t vector_width_t; /* 8-way bit-level vectorization */
+#define VECTOR_AT_ONE 0xff
+#define VECTOR_AT_ZERO 0x00
+#else
+typedef unsigned long int vector_width_t; /* word-way bit-level vectorization */
+#define VECTOR_AT_ONE ULONG_MAX
+#define VECTOR_AT_ZERO 0
+#endif
+
+
+typedef vector_width_t parallel_blocks_t[TEA_BLOCK_SIZE];
+typedef vector_width_t parallel_keys_t[TEA_KEY_SIZE];
+
+
+/* __P is a macro used to wrap function prototypes, so that compilers
+ that don't understand ANSI C prototypes still work, and ANSI C
+ compilers can issue warnings about type mismatches. */
+#undef __P
+#if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(WIN32) || defined(__cplusplus)
+ # define __P(protos) protos
+#else
+ # define __P(protos) ()
+#endif
+
+
+/* __BEGIN_DECLS should be used at the beginning of your declarations,
+ so that C++ compilers don't mangle their names. Use __END_DECLS at
+ the end of C declarations. */
+#undef __BEGIN_DECLS
+#undef __END_DECLS
+#ifdef __cplusplus
+# define __BEGIN_DECLS extern "C" {
+# define __END_DECLS }
+#else
+# define __BEGIN_DECLS /* empty */
+# define __END_DECLS /* empty */
+#endif
+
+/* The following definitions for FAR are needed only for MSDOS mixed
+ * model programming (small or medium model with some far allocations).
+ * This was tested only with MSC. If you don't need the mixed model,
+ * just define FAR to be empty.
+ */
+#ifdef SYS16BIT
+# if defined(M_I86SM) || defined(M_I86MM)
+ /* MSC small or medium model */
+# define SMALL_MEDIUM
+# ifdef _MSC_VER
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+# if (defined(__SMALL__) || defined(__MEDIUM__))
+ /* Turbo C small or medium model */
+# define SMALL_MEDIUM
+# ifdef __BORLANDC__
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+#endif
+
+
+#if defined(WINDOWS) || defined(WIN32)
+# ifdef BSTEA_DLL
+# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
+# ifdef BSTEA_INTERNAL
+# define BSTEA_EXTERN extern __declspec(dllexport)
+# else
+# define BSTEA_EXTERN extern __declspec(dllimport)
+# endif
+# endif
+# endif /* BSTEA_DLL */
+
+ /* If building or using bstea with the WINAPI/WINAPIV calling convention,
+ * define BSTEA_WINAPI.
+ * Caution: the standard BSTEA.DLL is NOT compiled using BSTEA_WINAPI.
+ */
+# ifdef BSTEA_WINAPI
+# ifdef FAR
+# undef FAR
+# endif
+# include <windows.h>
+ /* No need for _export, use BSTEA_LIB.DEF instead. */
+ /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+# define BSTEA_EXPORT WINAPI
+# ifdef WIN32
+# define BSTEA_EXPORTVA WINAPIV
+# else
+# define BSTEA_EXPORTVA FAR CDECL
+# endif
+# endif
+
+#else
+# include <stdbool.h>
+#endif
+
+#ifndef BSTEA_EXTERN
+# define BSTEA_EXTERN extern
+#endif
+#ifndef BSTEA_EXPORT
+# define BSTEA_EXPORT
+#endif
+#ifndef BSTEA_EXPORTVA
+# define BSTEA_EXPORTVA
+#endif
+
+#ifndef FAR
+# define FAR
+#endif
+
+__BEGIN_DECLS
+
+BSTEA_EXTERN void encrypt __P((parallel_blocks_t, const parallel_keys_t, unsigned int));
+BSTEA_EXTERN void decrypt __P((parallel_blocks_t, const parallel_keys_t, unsigned int));
+
+__END_DECLS
+
+#endif /* __BSTEA_H */
diff --git a/test/monniaux/bitsliced-tea/bstea_run.c b/test/monniaux/bitsliced-tea/bstea_run.c
new file mode 100644
index 00000000..782aa632
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/bstea_run.c
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+
+#include "bstea.h"
+
+#include "../clock.h"
+
+/* pack and unpack a single value all over the data path */
+static void pack(uint32_t *v, size_t len, vector_width_t *bv) {
+ size_t i, p, offset = 0;
+
+ for (i=0; i<len; ++i, offset += 32)
+ for (p = 0; p < 32; ++p)
+ bv[offset + p] = (v[i] & (1<<p)) ? VECTOR_AT_ONE : VECTOR_AT_ZERO;
+}
+
+
+static void unpack(vector_width_t *bv, int len, uint32_t *v) {
+ int i;
+
+ for (i=0; i<len; i++)
+ if (bv[i]) v[i>>5] |= 1<<(i%32);
+}
+
+/* pack and unpack one element at a time */
+static void pack_elem(uint32_t *v, size_t len, vector_width_t *bv, int elem) {
+ size_t i, p, offset = 0;
+
+ for (i=0; i<len; ++i, offset += 32)
+ for (p = 0; p < 32; ++p)
+ bv[offset + p] |= (v[i] & (1<<p)) ? (1<<(elem)) : 0;
+}
+
+static void unpack_elem(vector_width_t *bv, int len, uint32_t *v, int elem) {
+ int i;
+
+ for (i=0; i<len; i++)
+ if (bv[i] & (1<<elem)) v[i>>5] |= 1<<(i%32);
+}
+
+typedef struct tvector_s {
+ uint32_t ptext[TEA_BLOCK_SIZE >> 5];
+ uint32_t ctext[TEA_BLOCK_SIZE >> 5];
+ uint32_t key[TEA_KEY_SIZE >> 5];
+} tvector_t;
+
+static void test_vectors() {
+ int i, j;
+ parallel_blocks_t v;
+ parallel_keys_t k;
+ uint32_t ctext[TEA_BLOCK_SIZE >> 5];
+ uint32_t ptext[TEA_BLOCK_SIZE >> 5];
+ uint32_t key[TEA_KEY_SIZE >> 5];
+
+ tvector_t testv [] = { { {0x00000000, 0x00000000}, \
+ {0x41ea3a0a, 0x94baa940}, \
+ {0x00000000, 0x00000000, \
+ 0x00000000, 0x00000000} }, \
+ { {0x74736574, 0x2e656d20}, \
+ {0x6a2a5d77, 0x0992cef6}, \
+ {0x6805022b, 0x76491406, \
+ 0x260e5d77, 0x4378286c} }, \
+ { {0x94baa940, 0x00000000}, \
+ {0x4e8e7829, 0x7d8236d8}, \
+ {0x00000000, 0x00000000, \
+ 0x00000000, 0x41ea3a0a} }, \
+ { {0x7d8236d8, 0x00000000}, \
+ {0xc88ba95e, 0xe7edac02}, \
+ {0x00000000, 0x00000000, \
+ 0x41ea3a0a, 0x4e8e7829} } };
+
+ for (i = 0; i < sizeof(testv)/sizeof(tvector_t); ++i) {
+ clock_start();
+
+ for (j = 0;j < TEA_BLOCK_SIZE;++j) v[j] = 0;
+ for (j = 0;j < TEA_KEY_SIZE;++j) k[j] = 0;
+
+ (void) memset(&ctext, 0, 8);
+ (void) memset(&ptext, 0, 8);
+ (void) memset(&key, 0, 16);
+
+ pack(testv[i].ptext, 2, v);
+ pack(testv[i].key, 4, k);
+
+ encrypt(v,k,TEA_ROUNDS);
+
+ unpack(v, 64, ctext);
+
+ decrypt(v,k,TEA_ROUNDS);
+
+ unpack(v, 64, ptext);
+ unpack(k, 128, key);
+
+#if 0
+ printf("key[0]: 0x%8x, key[1]: 0x%8x, key[2]: 0x%8x, key[3]: 0x%8x\n", key[0], key[1], key[2], key[3]);
+ printf("ctext[0]: 0x%8x, ctext[1]: 0x%8x\n", ctext[0], ctext[1]);
+ printf("ptext[0]: 0x%8x, ptext[1]: 0x%8x\n", ptext[0], ptext[1]);
+
+ printf("t1_ctext[0]: 0x%8x, t1_ctext[1]: 0x%8x\n", testv[i].ctext[0], testv[i].ctext[1]);
+ printf("t1_ptext[0]: 0x%8x, t1_ptext[1]: 0x%8x\n\n\n", testv[i].ptext[0], testv[i].ptext[1]);
+#endif
+
+ assert(testv[i].ctext[0] == ctext[0] \
+ && testv[i].ctext[1] == ctext[1]);
+ assert(testv[i].ptext[0] == ptext[0] \
+ && testv[i].ptext[1] == ptext[1]);
+ assert(testv[i].key[0] == key[0] \
+ && testv[i].key[1] == key[1] \
+ && testv[i].key[2] == key[2] \
+ && testv[i].key[3] == key[3] );
+
+ clock_stop();
+
+ printf("test vector, %i,\t[PASSED]\n", i);
+ }
+}
+
+int main(int argc, char *argv[]) {
+ test_vectors();
+ print_total_clock();
+ return 0;
+}
diff --git a/test/monniaux/bitsliced-tea/bstea_wordsize.h b/test/monniaux/bitsliced-tea/bstea_wordsize.h
new file mode 100644
index 00000000..4305db58
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/bstea_wordsize.h
@@ -0,0 +1,16 @@
+#ifndef __BSTEA_WORDSIZE_H
+#define __BSTEA_WORDSIZE_H
+
+/* Determine the wordsize from the preprocessor defines. */
+
+#if defined __x86_64__ || defined __amd64__ || defined __x86_64 || \
+ defined __amd64 || defined _M_X64 || defined __ia64__ || \
+ defined __ia64__ || defined __IA64__ || defined __ia64 || \
+ defined _M_IA64 || defined __KVX__
+# define __BSTEA_WORDSIZE 64
+#else
+# define __BSTEA_WORDSIZE 32
+#endif
+
+
+#endif /* __BSTEA_WORDSIZE_H */
diff --git a/test/monniaux/bitsliced-tea/reduce/compare.sh b/test/monniaux/bitsliced-tea/reduce/compare.sh
new file mode 100755
index 00000000..7f96491b
--- /dev/null
+++ b/test/monniaux/bitsliced-tea/reduce/compare.sh
@@ -0,0 +1,20 @@
+PREFIX=/home/monniaux/work/Kalray/ternary/CompCert
+INCLUDES=-I$PREFIX/test/monniaux/bitsliced-tea
+CCOMP_K1="$PREFIX/ccomp -fno-unprototyped -O3 $INCLUDES"
+GCC_K1="k1-cos-gcc -Werror=implicit -O3 $INCLUDES"
+GCC_HOST="gcc -Werror=implicit -O3 $INCLUDES"
+FILE=bstea.c
+
+OTHERS_K1="$PREFIX/test/monniaux/bitsliced-tea/bstea_run.gcc.kvx.o $PREFIX/test/monniaux/clock.gcc.kvx.o"
+OTHERS_HOST="$PREFIX/test/monniaux/bitsliced-tea/bstea_run.gcc.host.o $PREFIX/test/monniaux/clock.gcc.host.o"
+
+$CCOMP_K1 $FILE $OTHERS_K1 -o bstead.ccomp.kvx &&
+$GCC_K1 $FILE $OTHERS_K1 -o bstead.gcc.kvx &&
+$GCC_HOST $FILE $OTHERS_HOST -o bstead.gcc.host &&
+valgrind -q ./bstead.gcc.host &&
+k1-cluster --cycle-based -- bstead.ccomp.kvx > bstead.ccomp.kvx.out &&
+k1-cluster --cycle-based -- bstead.gcc.kvx > bstead.gcc.kvx.out &&
+grep cycles bstead.ccomp.kvx.out|sed -e 's/cycles: //' > bstead.ccomp.kvx.cycles &&
+grep cycles bstead.gcc.kvx.out|sed -e 's/cycles: //' > bstead.gcc.kvx.cycles &&
+test `cat bstead.gcc.kvx.cycles` -gt 100000 &&
+test `cat bstead.ccomp.kvx.cycles` -gt 200000
diff --git a/test/monniaux/build_benches.sh b/test/monniaux/build_benches.sh
new file mode 100755
index 00000000..01abf55d
--- /dev/null
+++ b/test/monniaux/build_benches.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+TMPFILE=/tmp/1513times.txt
+
+cores=$(grep -c ^processor /proc/cpuinfo)
+source benches.sh
+
+default="\e[39m"
+magenta="\e[35m"
+red="\e[31m"
+
+rm -f commands.txt
+rm -f $TMPFILE
+for bench in $benches; do
+ echo -e "${magenta}Building $bench..${default}"
+ if [ "$1" == "" ]; then
+ (cd $bench && make -s -j$cores > /dev/null &> /dev/null) || { echo -e "${red}Build failed" && break; }
+ else
+ (cd $bench && make -j$cores) | grep -P "\d+: \d+\.\d+" >> $TMPFILE
+ fi
+done
+
+if [ "$1" != "" ]; then
+ cat $TMPFILE | sort -n -k 1 > $1
+fi
+
diff --git a/test/monniaux/builtins/fma.c b/test/monniaux/builtins/fma.c
new file mode 100644
index 00000000..4083e781
--- /dev/null
+++ b/test/monniaux/builtins/fma.c
@@ -0,0 +1,14 @@
+#define COMPCERT_NO_FP_MACROS
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+ if (argc < 4) return 1;
+ double x = strtod(argv[1], NULL);
+ double y = strtod(argv[2], NULL);
+ double z = strtod(argv[3], NULL);
+ printf("%g %g %g\n", __builtin_fma(x, y, z), fma(x, y, z), x*y + z);
+ printf("%g %g %g\n", __builtin_fma(-x, y, z), fma(-x, y, z), (-x)*y + z);
+ return 0;
+}
diff --git a/test/monniaux/clean_benches.sh b/test/monniaux/clean_benches.sh
new file mode 100755
index 00000000..dff15fd4
--- /dev/null
+++ b/test/monniaux/clean_benches.sh
@@ -0,0 +1,12 @@
+
+source benches.sh
+
+blue="\e[34m"
+default="\e[39m"
+
+rm -f commands.txt
+for bench in $benches; do
+ echo -e "${blue}Cleaning $bench..${default}"
+ (cd $bench && make -s clean)
+done
+rm -f *.o
diff --git a/test/monniaux/clean_csv.sh b/test/monniaux/clean_csv.sh
new file mode 100755
index 00000000..e480aa5a
--- /dev/null
+++ b/test/monniaux/clean_csv.sh
@@ -0,0 +1,9 @@
+
+source benches.sh
+
+rm -f commands.txt
+for bench in $benches; do
+ (cd $bench && rm *.csv)
+done
+
+rm measures.csv
diff --git a/test/monniaux/clock.c b/test/monniaux/clock.c
new file mode 100644
index 00000000..4ec679f6
--- /dev/null
+++ b/test/monniaux/clock.c
@@ -0,0 +1,32 @@
+#include "cycles.h"
+#include <stdio.h>
+
+static cycle_t total_clock, last_start;
+
+void clock_prepare(void) {
+ cycle_count_config();
+}
+
+void clock_start(void) {
+ last_start = get_cycle();
+}
+
+void clock_stop(void) {
+ total_clock += get_cycle() - last_start;
+}
+
+cycle_t get_total_clock(void) {
+ return total_clock;
+}
+
+cycle_t get_current_cycle(void) {
+ return get_cycle();
+}
+
+void print_total_clock(void) {
+ printf("time cycles: %" PRcycle "\n", total_clock);
+}
+
+void printerr_total_clock(void) {
+ fprintf(stderr, "time cycles: %" PRcycle "\n", total_clock);
+}
diff --git a/test/monniaux/clock.h b/test/monniaux/clock.h
new file mode 100644
index 00000000..be8fa172
--- /dev/null
+++ b/test/monniaux/clock.h
@@ -0,0 +1,9 @@
+typedef unsigned long cycle_t;
+
+void clock_prepare(void);
+void clock_stop(void);
+void clock_start(void);
+cycle_t get_total_clock(void);
+cycle_t get_current_cycle(void);
+void print_total_clock(void);
+void printerr_total_clock(void);
diff --git a/test/monniaux/cmov/cmov.c b/test/monniaux/cmov/cmov.c
new file mode 100644
index 00000000..2e388834
--- /dev/null
+++ b/test/monniaux/cmov/cmov.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+long cmovl(int x, long y, long z) {
+ return __builtin_sel(x, y, z);
+}
+
+int cmovi(int x, int y, int z) {
+ return __builtin_sel(x, y, z);
+}
+
+double cmovd(int x, double y, double z) {
+ return __builtin_sel(x, y, z);
+}
+
+int main() {
+ printf("%ld\n", cmovl(1, 42, 65));
+ printf("%ld\n", cmovl(0, 42, 65));
+ printf("%d\n", cmovi(1, 42, 65));
+ printf("%d\n", cmovi(0, 42, 65));
+ printf("%f\n", cmovd(1, 42., 65.));
+ printf("%f\n", cmovd(0, 42., 65.));
+}
diff --git a/test/monniaux/cmov/cmov2.c b/test/monniaux/cmov/cmov2.c
new file mode 100644
index 00000000..6ecab61b
--- /dev/null
+++ b/test/monniaux/cmov/cmov2.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+
+long cmovl(int x, long y, long z) {
+ return x ? y : z;
+}
+
+int cmovi(int x, int y, int z) {
+ return x ? y : z;
+}
+
+double cmovd(int x, double y, double z) {
+ return x ? y : z;
+}
+
+float cmovf(int x, float y, float z) {
+ return x ? y : z;
+}
+
+int main() {
+ printf("%ld\n", cmovl(1, 42, 65));
+ printf("%ld\n", cmovl(0, 42, 65));
+ printf("%d\n", cmovi(1, 42, 65));
+ printf("%d\n", cmovi(0, 42, 65));
+ printf("%f\n", cmovd(1, 42., 65.));
+ printf("%f\n", cmovd(0, 42., 65.));
+ printf("%f\n", cmovf(1, 42.f, 65.f));
+ printf("%f\n", cmovf(0, 42.f, 65.f));
+}
diff --git a/test/monniaux/codegen/comp0.c b/test/monniaux/codegen/comp0.c
new file mode 100644
index 00000000..60f8ba77
--- /dev/null
+++ b/test/monniaux/codegen/comp0.c
@@ -0,0 +1,3 @@
+int toto(unsigned x) {
+ return (x < 0) ? 1 : 2;
+}
diff --git a/test/monniaux/complex/Makefile b/test/monniaux/complex/Makefile
new file mode 100644
index 00000000..38c10eab
--- /dev/null
+++ b/test/monniaux/complex/Makefile
@@ -0,0 +1,4 @@
+TARGET=complex_mat
+MEASURES="c1 c8 c9"
+
+include ../rules.mk
diff --git a/test/monniaux/complex/complex_mat.c b/test/monniaux/complex/complex_mat.c
new file mode 100644
index 00000000..f39dccf0
--- /dev/null
+++ b/test/monniaux/complex/complex_mat.c
@@ -0,0 +1,247 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "../clock.h"
+
+typedef double REAL;
+
+typedef struct {
+ REAL re, im;
+} COMPLEX;
+
+static inline void COMPLEX_zero(COMPLEX *r) {
+ r->re = r->im = 0.0;
+}
+
+static inline void COMPLEX_add(COMPLEX *r,
+ const COMPLEX *x,
+ const COMPLEX *y) {
+ double re = x->re + y->re;
+ double im = x->im + y->im;
+ r->re = re;
+ r->im = im;
+}
+
+static inline void COMPLEX_mul(COMPLEX *r,
+ const COMPLEX *x,
+ const COMPLEX *y) {
+ double re = x->re * y->re - x->im * y->im;
+ double im = x->im * y->re + x->re * y->im;
+ r->re = re;
+ r->im = im;
+}
+
+static inline void COMPLEX_mul_addto(COMPLEX *r,
+ const COMPLEX *x,
+ const COMPLEX *y) {
+ double re = r->re + x->re * y->re - x->im * y->im;
+ double im = r->im + x->im * y->re + x->re * y->im;
+ r->re = re;
+ r->im = im;
+}
+
+#define MACRO_COMPLEX_mul_addto(rre, rim, x, y) \
+ { \
+ double xre = (x).re, xim=(x).im, \
+ yre = (y).re, yim=(y).im; \
+ (rre) += xre * yre - xim * yim; \
+ (rim) += xim * yre + xre * yim; \
+ }
+
+
+void COMPLEX_mat_mul1(unsigned m, unsigned n, unsigned p,
+ COMPLEX * restrict c, unsigned stride_c,
+ const COMPLEX *a, unsigned stride_a,
+ const COMPLEX *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ COMPLEX_zero(c+i*stride_c+k);
+ }
+ }
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ for(unsigned j=0; j<n; j++) {
+ COMPLEX_mul_addto(c+i*stride_c+k, a+i*stride_a+j, b+j*stride_b+k);
+ }
+ }
+ }
+}
+
+#define UNROLL 4
+
+#undef CHUNK
+#define CHUNK \
+ COMPLEX_mul_addto(&total, pa_i_j, pb_j_k); \
+ pa_i_j ++; \
+ pb_j_k = (COMPLEX*) ((char*) pb_j_k + stride_b_scaled);
+
+void COMPLEX_mat_mul8(unsigned m, unsigned n, unsigned p,
+ COMPLEX * c, unsigned stride_c,
+ const COMPLEX *a, unsigned stride_a,
+ const COMPLEX *b, unsigned stride_b) {
+ const COMPLEX *pa_i = a;
+ COMPLEX * pc_i = c;
+ size_t stride_b_scaled = sizeof(COMPLEX) * stride_b;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const COMPLEX *pb_j_k = b+k, *pa_i_j = pa_i;
+ COMPLEX total;
+ COMPLEX_zero(&total);
+ {
+ unsigned j4=0, n4=n/UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ CHUNK
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#undef CHUNK
+#define CHUNK \
+ MACRO_COMPLEX_mul_addto(totalre, totalim, *pa_i_j, *pb_j_k) \
+ pa_i_j ++; \
+ pb_j_k = (COMPLEX*) ((char*) pb_j_k + stride_b_scaled);
+
+void COMPLEX_mat_mul9(unsigned m, unsigned n, unsigned p,
+ COMPLEX * c, unsigned stride_c,
+ const COMPLEX *a, unsigned stride_a,
+ const COMPLEX *b, unsigned stride_b) {
+ const COMPLEX *pa_i = a;
+ COMPLEX * pc_i = c;
+ size_t stride_b_scaled = sizeof(COMPLEX) * stride_b;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const COMPLEX *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL totalre=0.0, totalim=0.0;
+ {
+ unsigned j4=0, n4=n/UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ CHUNK
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k].re = totalre;
+ pc_i[k].im = totalim;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+bool COMPLEX_equal(const COMPLEX *a,
+ const COMPLEX *b) {
+ return a->re==b->re && a->im==b->im;
+}
+
+bool COMPLEX_mat_equal(unsigned m,
+ unsigned n,
+ const COMPLEX *a, unsigned stride_a,
+ const COMPLEX *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ if (! COMPLEX_equal(a+i*stride_a + j, b+i*stride_b + j)) {
+ printf("at %u,%u: (%g,%g) vs (%g,%g)\n", i, j,
+ a[i*stride_a + j].re, a[i*stride_a + j].im,
+ b[i*stride_b + j].re, b[i*stride_b + j].im);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+REAL REAL_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next % 1000;
+}
+
+void COMPLEX_mat_random(unsigned m,
+ unsigned n,
+ COMPLEX *a, unsigned stride_a) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ a[i*stride_a + j].re = REAL_random();
+ a[i*stride_a + j].im = REAL_random();
+ }
+ }
+}
+
+
+int main() {
+ const unsigned m = 60, n = 31, p = 50;
+ clock_prepare();
+ COMPLEX *a = malloc(sizeof(COMPLEX) * m * n);
+ COMPLEX_mat_random(m, n, a, n);
+ COMPLEX *b = malloc(sizeof(COMPLEX) * n * p);
+ COMPLEX_mat_random(n, p, b, p);
+
+ COMPLEX *c1 = malloc(sizeof(COMPLEX) * m * p);
+ cycle_t c1_time = get_current_cycle();
+ COMPLEX_mat_mul1(m, n, p, c1, p, a, n, b, p);
+ c1_time = get_current_cycle()-c1_time;
+
+ COMPLEX *c8 = malloc(sizeof(COMPLEX) * m * p);
+ cycle_t c8_time = get_current_cycle();
+ COMPLEX_mat_mul8(m, n, p, c8, p, a, n, b, p);
+ c8_time = get_current_cycle()-c8_time;
+
+ COMPLEX *c9 = malloc(sizeof(COMPLEX) * m * p);
+ cycle_t c9_time = get_current_cycle();
+ COMPLEX_mat_mul9(m, n, p, c9, p, a, n, b, p);
+ c9_time = get_current_cycle()-c9_time;
+
+ printf("c1==c8: %s\n"
+ "c1==c9: %s\n"
+ "c1 cycles: %" PRIu64 "\n"
+ "c8 cycles: %" PRIu64 "\n"
+ "c9 cycles: %" PRIu64 "\n",
+
+ COMPLEX_mat_equal(m, n, c1, p, c8, p)?"true":"false",
+ COMPLEX_mat_equal(m, n, c1, p, c9, p)?"true":"false",
+
+ c1_time,
+ c8_time,
+ c9_time);
+
+ free(a);
+ free(b);
+ free(c1);
+ free(c8);
+ free(c9);
+ return 0;
+}
diff --git a/test/monniaux/crypto-algorithms/Makefile b/test/monniaux/crypto-algorithms/Makefile
new file mode 100644
index 00000000..fde22f38
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/Makefile
@@ -0,0 +1,28 @@
+include ../rules.mk
+
+all: md2.all md5.all sha1.all sha256.all blowfish.all des.all
+kvx: md2_test.ccomp.kvx md5_test.ccomp.kvx sha1_test.ccomp.kvx sha256_test.ccomp.kvx blowfish_test.ccomp.kvx des_test.ccomp.kvx
+
+md2.all : md2_test.ccomp.kvx.out md2_test.gcc.kvx.out
+md5.all : md5_test.ccomp.kvx.out md5_test.gcc.kvx.out
+arcfour.all : arcfour_test.ccomp.kvx.out arcfour_test.gcc.kvx.out
+blowfish.all : blowfish_test.ccomp.kvx.out blowfish_test.gcc.kvx.out
+rot-13.all : rot-13_test.ccomp.kvx.out rot-13_test.gcc.kvx.out
+sha1.all : sha1_test.ccomp.kvx.out sha1_test.gcc.kvx.out
+sha256.all : sha256_test.ccomp.kvx.out sha256_test.gcc.kvx.out
+des.all: des_test.ccomp.kvx.out des_test.gcc.kvx.out
+base64.all: base64_test.ccomp.kvx.out base64_test.gcc.kvx.out
+aes.all : aes_test.ccomp.kvx.out aes_test.gcc.kvx.out
+
+%.gcc.kvx.s %.ccomp.kvx.s %_test.gcc.kvx.s: %.h
+
+%_test.gcc.kvx: %.gcc.kvx.o %_test.gcc.kvx.o
+ $(KVX_CC) $(KVX_CFLAGS) $+ -o $@
+
+%_test.ccomp.kvx: %.ccomp.kvx.o %_test.gcc.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+clean:
+ $(RM) -f *.s *.o *.out *.kvx *.host
+
+.PHONY: clean all md2.all md5.all rot-13.all sha1.all sha256.all md5.all blowfish.all arcfour.all des.all base64.all aes.all
diff --git a/test/monniaux/crypto-algorithms/README.md b/test/monniaux/crypto-algorithms/README.md
new file mode 100644
index 00000000..067c8043
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/README.md
@@ -0,0 +1,18 @@
+crypto-algorithms
+=================
+
+https://github.com/B-Con/crypto-algorithms/
+
+About
+---
+These are basic implementations of standard cryptography algorithms, written by Brad Conte (brad@bradconte.com) from scratch and without any cross-licensing. They exist to provide publically accessible, restriction-free implementations of popular cryptographic algorithms, like AES and SHA-1. These are primarily intended for educational and pragmatic purposes (such as comparing a specification to actual implementation code, or for building an internal application that computes test vectors for a product). The algorithms have been tested against standard test vectors.
+
+This code is released into the public domain free of any restrictions. The author requests acknowledgement if the code is used, but does not require it. This code is provided free of any liability and without any quality claims by the author.
+
+Note that these are *not* cryptographically secure implementations. They have no resistence to side-channel attacks and should not be used in contexts that need cryptographically secure implementations.
+
+These algorithms are not optimized for speed or space. They are primarily designed to be easy to read, although some basic optimization techniques have been employed.
+
+Building
+---
+The source code for each algorithm will come in a pair of a source code file and a header file. There should be no inter-header file dependencies, no additional libraries, no platform-specific header files, or any other complicating matters. Compiling them should be as easy as adding the relevent source code to the project.
diff --git a/test/monniaux/crypto-algorithms/aes.c b/test/monniaux/crypto-algorithms/aes.c
new file mode 100644
index 00000000..800395b1
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/aes.c
@@ -0,0 +1,1095 @@
+/*********************************************************************
+* Filename: aes.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: This code is the implementation of the AES algorithm and
+ the CTR, CBC, and CCM modes of operation it can be used in.
+ AES is, specified by the NIST in in publication FIPS PUB 197,
+ availible at:
+ * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf .
+ The CBC and CTR modes of operation are specified by
+ NIST SP 800-38 A, available at:
+ * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf .
+ The CCM mode of operation is specified by NIST SP80-38 C, available at:
+ * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "aes.h"
+
+#include <stdio.h>
+
+/****************************** MACROS ******************************/
+// The least significant byte of the word is rotated to the end.
+#define KE_ROTWORD(x) (((x) << 8) | ((x) >> 24))
+
+#define TRUE 1
+#define FALSE 0
+
+/**************************** DATA TYPES ****************************/
+#define AES_128_ROUNDS 10
+#define AES_192_ROUNDS 12
+#define AES_256_ROUNDS 14
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void ccm_prepare_first_ctr_blk(BYTE counter[], const BYTE nonce[], int nonce_len, int payload_len_store_size);
+void ccm_prepare_first_format_blk(BYTE buf[], int assoc_len, int payload_len, int payload_len_store_size, int mac_len, const BYTE nonce[], int nonce_len);
+void ccm_format_assoc_data(BYTE buf[], int *end_of_buf, const BYTE assoc[], int assoc_len);
+void ccm_format_payload_data(BYTE buf[], int *end_of_buf, const BYTE payload[], int payload_len);
+
+/**************************** VARIABLES *****************************/
+// This is the specified AES SBox. To look up a substitution value, put the first
+// nibble in the first index (row) and the second nibble in the second index (column).
+static const BYTE aes_sbox[16][16] = {
+ {0x63,0x7C,0x77,0x7B,0xF2,0x6B,0x6F,0xC5,0x30,0x01,0x67,0x2B,0xFE,0xD7,0xAB,0x76},
+ {0xCA,0x82,0xC9,0x7D,0xFA,0x59,0x47,0xF0,0xAD,0xD4,0xA2,0xAF,0x9C,0xA4,0x72,0xC0},
+ {0xB7,0xFD,0x93,0x26,0x36,0x3F,0xF7,0xCC,0x34,0xA5,0xE5,0xF1,0x71,0xD8,0x31,0x15},
+ {0x04,0xC7,0x23,0xC3,0x18,0x96,0x05,0x9A,0x07,0x12,0x80,0xE2,0xEB,0x27,0xB2,0x75},
+ {0x09,0x83,0x2C,0x1A,0x1B,0x6E,0x5A,0xA0,0x52,0x3B,0xD6,0xB3,0x29,0xE3,0x2F,0x84},
+ {0x53,0xD1,0x00,0xED,0x20,0xFC,0xB1,0x5B,0x6A,0xCB,0xBE,0x39,0x4A,0x4C,0x58,0xCF},
+ {0xD0,0xEF,0xAA,0xFB,0x43,0x4D,0x33,0x85,0x45,0xF9,0x02,0x7F,0x50,0x3C,0x9F,0xA8},
+ {0x51,0xA3,0x40,0x8F,0x92,0x9D,0x38,0xF5,0xBC,0xB6,0xDA,0x21,0x10,0xFF,0xF3,0xD2},
+ {0xCD,0x0C,0x13,0xEC,0x5F,0x97,0x44,0x17,0xC4,0xA7,0x7E,0x3D,0x64,0x5D,0x19,0x73},
+ {0x60,0x81,0x4F,0xDC,0x22,0x2A,0x90,0x88,0x46,0xEE,0xB8,0x14,0xDE,0x5E,0x0B,0xDB},
+ {0xE0,0x32,0x3A,0x0A,0x49,0x06,0x24,0x5C,0xC2,0xD3,0xAC,0x62,0x91,0x95,0xE4,0x79},
+ {0xE7,0xC8,0x37,0x6D,0x8D,0xD5,0x4E,0xA9,0x6C,0x56,0xF4,0xEA,0x65,0x7A,0xAE,0x08},
+ {0xBA,0x78,0x25,0x2E,0x1C,0xA6,0xB4,0xC6,0xE8,0xDD,0x74,0x1F,0x4B,0xBD,0x8B,0x8A},
+ {0x70,0x3E,0xB5,0x66,0x48,0x03,0xF6,0x0E,0x61,0x35,0x57,0xB9,0x86,0xC1,0x1D,0x9E},
+ {0xE1,0xF8,0x98,0x11,0x69,0xD9,0x8E,0x94,0x9B,0x1E,0x87,0xE9,0xCE,0x55,0x28,0xDF},
+ {0x8C,0xA1,0x89,0x0D,0xBF,0xE6,0x42,0x68,0x41,0x99,0x2D,0x0F,0xB0,0x54,0xBB,0x16}
+};
+
+static const BYTE aes_invsbox[16][16] = {
+ {0x52,0x09,0x6A,0xD5,0x30,0x36,0xA5,0x38,0xBF,0x40,0xA3,0x9E,0x81,0xF3,0xD7,0xFB},
+ {0x7C,0xE3,0x39,0x82,0x9B,0x2F,0xFF,0x87,0x34,0x8E,0x43,0x44,0xC4,0xDE,0xE9,0xCB},
+ {0x54,0x7B,0x94,0x32,0xA6,0xC2,0x23,0x3D,0xEE,0x4C,0x95,0x0B,0x42,0xFA,0xC3,0x4E},
+ {0x08,0x2E,0xA1,0x66,0x28,0xD9,0x24,0xB2,0x76,0x5B,0xA2,0x49,0x6D,0x8B,0xD1,0x25},
+ {0x72,0xF8,0xF6,0x64,0x86,0x68,0x98,0x16,0xD4,0xA4,0x5C,0xCC,0x5D,0x65,0xB6,0x92},
+ {0x6C,0x70,0x48,0x50,0xFD,0xED,0xB9,0xDA,0x5E,0x15,0x46,0x57,0xA7,0x8D,0x9D,0x84},
+ {0x90,0xD8,0xAB,0x00,0x8C,0xBC,0xD3,0x0A,0xF7,0xE4,0x58,0x05,0xB8,0xB3,0x45,0x06},
+ {0xD0,0x2C,0x1E,0x8F,0xCA,0x3F,0x0F,0x02,0xC1,0xAF,0xBD,0x03,0x01,0x13,0x8A,0x6B},
+ {0x3A,0x91,0x11,0x41,0x4F,0x67,0xDC,0xEA,0x97,0xF2,0xCF,0xCE,0xF0,0xB4,0xE6,0x73},
+ {0x96,0xAC,0x74,0x22,0xE7,0xAD,0x35,0x85,0xE2,0xF9,0x37,0xE8,0x1C,0x75,0xDF,0x6E},
+ {0x47,0xF1,0x1A,0x71,0x1D,0x29,0xC5,0x89,0x6F,0xB7,0x62,0x0E,0xAA,0x18,0xBE,0x1B},
+ {0xFC,0x56,0x3E,0x4B,0xC6,0xD2,0x79,0x20,0x9A,0xDB,0xC0,0xFE,0x78,0xCD,0x5A,0xF4},
+ {0x1F,0xDD,0xA8,0x33,0x88,0x07,0xC7,0x31,0xB1,0x12,0x10,0x59,0x27,0x80,0xEC,0x5F},
+ {0x60,0x51,0x7F,0xA9,0x19,0xB5,0x4A,0x0D,0x2D,0xE5,0x7A,0x9F,0x93,0xC9,0x9C,0xEF},
+ {0xA0,0xE0,0x3B,0x4D,0xAE,0x2A,0xF5,0xB0,0xC8,0xEB,0xBB,0x3C,0x83,0x53,0x99,0x61},
+ {0x17,0x2B,0x04,0x7E,0xBA,0x77,0xD6,0x26,0xE1,0x69,0x14,0x63,0x55,0x21,0x0C,0x7D}
+};
+
+// This table stores pre-calculated values for all possible GF(2^8) calculations.This
+// table is only used by the (Inv)MixColumns steps.
+// USAGE: The second index (column) is the coefficient of multiplication. Only 7 different
+// coefficients are used: 0x01, 0x02, 0x03, 0x09, 0x0b, 0x0d, 0x0e, but multiplication by
+// 1 is negligible leaving only 6 coefficients. Each column of the table is devoted to one
+// of these coefficients, in the ascending order of value, from values 0x00 to 0xFF.
+static const BYTE gf_mul[256][6] = {
+ {0x00,0x00,0x00,0x00,0x00,0x00},{0x02,0x03,0x09,0x0b,0x0d,0x0e},
+ {0x04,0x06,0x12,0x16,0x1a,0x1c},{0x06,0x05,0x1b,0x1d,0x17,0x12},
+ {0x08,0x0c,0x24,0x2c,0x34,0x38},{0x0a,0x0f,0x2d,0x27,0x39,0x36},
+ {0x0c,0x0a,0x36,0x3a,0x2e,0x24},{0x0e,0x09,0x3f,0x31,0x23,0x2a},
+ {0x10,0x18,0x48,0x58,0x68,0x70},{0x12,0x1b,0x41,0x53,0x65,0x7e},
+ {0x14,0x1e,0x5a,0x4e,0x72,0x6c},{0x16,0x1d,0x53,0x45,0x7f,0x62},
+ {0x18,0x14,0x6c,0x74,0x5c,0x48},{0x1a,0x17,0x65,0x7f,0x51,0x46},
+ {0x1c,0x12,0x7e,0x62,0x46,0x54},{0x1e,0x11,0x77,0x69,0x4b,0x5a},
+ {0x20,0x30,0x90,0xb0,0xd0,0xe0},{0x22,0x33,0x99,0xbb,0xdd,0xee},
+ {0x24,0x36,0x82,0xa6,0xca,0xfc},{0x26,0x35,0x8b,0xad,0xc7,0xf2},
+ {0x28,0x3c,0xb4,0x9c,0xe4,0xd8},{0x2a,0x3f,0xbd,0x97,0xe9,0xd6},
+ {0x2c,0x3a,0xa6,0x8a,0xfe,0xc4},{0x2e,0x39,0xaf,0x81,0xf3,0xca},
+ {0x30,0x28,0xd8,0xe8,0xb8,0x90},{0x32,0x2b,0xd1,0xe3,0xb5,0x9e},
+ {0x34,0x2e,0xca,0xfe,0xa2,0x8c},{0x36,0x2d,0xc3,0xf5,0xaf,0x82},
+ {0x38,0x24,0xfc,0xc4,0x8c,0xa8},{0x3a,0x27,0xf5,0xcf,0x81,0xa6},
+ {0x3c,0x22,0xee,0xd2,0x96,0xb4},{0x3e,0x21,0xe7,0xd9,0x9b,0xba},
+ {0x40,0x60,0x3b,0x7b,0xbb,0xdb},{0x42,0x63,0x32,0x70,0xb6,0xd5},
+ {0x44,0x66,0x29,0x6d,0xa1,0xc7},{0x46,0x65,0x20,0x66,0xac,0xc9},
+ {0x48,0x6c,0x1f,0x57,0x8f,0xe3},{0x4a,0x6f,0x16,0x5c,0x82,0xed},
+ {0x4c,0x6a,0x0d,0x41,0x95,0xff},{0x4e,0x69,0x04,0x4a,0x98,0xf1},
+ {0x50,0x78,0x73,0x23,0xd3,0xab},{0x52,0x7b,0x7a,0x28,0xde,0xa5},
+ {0x54,0x7e,0x61,0x35,0xc9,0xb7},{0x56,0x7d,0x68,0x3e,0xc4,0xb9},
+ {0x58,0x74,0x57,0x0f,0xe7,0x93},{0x5a,0x77,0x5e,0x04,0xea,0x9d},
+ {0x5c,0x72,0x45,0x19,0xfd,0x8f},{0x5e,0x71,0x4c,0x12,0xf0,0x81},
+ {0x60,0x50,0xab,0xcb,0x6b,0x3b},{0x62,0x53,0xa2,0xc0,0x66,0x35},
+ {0x64,0x56,0xb9,0xdd,0x71,0x27},{0x66,0x55,0xb0,0xd6,0x7c,0x29},
+ {0x68,0x5c,0x8f,0xe7,0x5f,0x03},{0x6a,0x5f,0x86,0xec,0x52,0x0d},
+ {0x6c,0x5a,0x9d,0xf1,0x45,0x1f},{0x6e,0x59,0x94,0xfa,0x48,0x11},
+ {0x70,0x48,0xe3,0x93,0x03,0x4b},{0x72,0x4b,0xea,0x98,0x0e,0x45},
+ {0x74,0x4e,0xf1,0x85,0x19,0x57},{0x76,0x4d,0xf8,0x8e,0x14,0x59},
+ {0x78,0x44,0xc7,0xbf,0x37,0x73},{0x7a,0x47,0xce,0xb4,0x3a,0x7d},
+ {0x7c,0x42,0xd5,0xa9,0x2d,0x6f},{0x7e,0x41,0xdc,0xa2,0x20,0x61},
+ {0x80,0xc0,0x76,0xf6,0x6d,0xad},{0x82,0xc3,0x7f,0xfd,0x60,0xa3},
+ {0x84,0xc6,0x64,0xe0,0x77,0xb1},{0x86,0xc5,0x6d,0xeb,0x7a,0xbf},
+ {0x88,0xcc,0x52,0xda,0x59,0x95},{0x8a,0xcf,0x5b,0xd1,0x54,0x9b},
+ {0x8c,0xca,0x40,0xcc,0x43,0x89},{0x8e,0xc9,0x49,0xc7,0x4e,0x87},
+ {0x90,0xd8,0x3e,0xae,0x05,0xdd},{0x92,0xdb,0x37,0xa5,0x08,0xd3},
+ {0x94,0xde,0x2c,0xb8,0x1f,0xc1},{0x96,0xdd,0x25,0xb3,0x12,0xcf},
+ {0x98,0xd4,0x1a,0x82,0x31,0xe5},{0x9a,0xd7,0x13,0x89,0x3c,0xeb},
+ {0x9c,0xd2,0x08,0x94,0x2b,0xf9},{0x9e,0xd1,0x01,0x9f,0x26,0xf7},
+ {0xa0,0xf0,0xe6,0x46,0xbd,0x4d},{0xa2,0xf3,0xef,0x4d,0xb0,0x43},
+ {0xa4,0xf6,0xf4,0x50,0xa7,0x51},{0xa6,0xf5,0xfd,0x5b,0xaa,0x5f},
+ {0xa8,0xfc,0xc2,0x6a,0x89,0x75},{0xaa,0xff,0xcb,0x61,0x84,0x7b},
+ {0xac,0xfa,0xd0,0x7c,0x93,0x69},{0xae,0xf9,0xd9,0x77,0x9e,0x67},
+ {0xb0,0xe8,0xae,0x1e,0xd5,0x3d},{0xb2,0xeb,0xa7,0x15,0xd8,0x33},
+ {0xb4,0xee,0xbc,0x08,0xcf,0x21},{0xb6,0xed,0xb5,0x03,0xc2,0x2f},
+ {0xb8,0xe4,0x8a,0x32,0xe1,0x05},{0xba,0xe7,0x83,0x39,0xec,0x0b},
+ {0xbc,0xe2,0x98,0x24,0xfb,0x19},{0xbe,0xe1,0x91,0x2f,0xf6,0x17},
+ {0xc0,0xa0,0x4d,0x8d,0xd6,0x76},{0xc2,0xa3,0x44,0x86,0xdb,0x78},
+ {0xc4,0xa6,0x5f,0x9b,0xcc,0x6a},{0xc6,0xa5,0x56,0x90,0xc1,0x64},
+ {0xc8,0xac,0x69,0xa1,0xe2,0x4e},{0xca,0xaf,0x60,0xaa,0xef,0x40},
+ {0xcc,0xaa,0x7b,0xb7,0xf8,0x52},{0xce,0xa9,0x72,0xbc,0xf5,0x5c},
+ {0xd0,0xb8,0x05,0xd5,0xbe,0x06},{0xd2,0xbb,0x0c,0xde,0xb3,0x08},
+ {0xd4,0xbe,0x17,0xc3,0xa4,0x1a},{0xd6,0xbd,0x1e,0xc8,0xa9,0x14},
+ {0xd8,0xb4,0x21,0xf9,0x8a,0x3e},{0xda,0xb7,0x28,0xf2,0x87,0x30},
+ {0xdc,0xb2,0x33,0xef,0x90,0x22},{0xde,0xb1,0x3a,0xe4,0x9d,0x2c},
+ {0xe0,0x90,0xdd,0x3d,0x06,0x96},{0xe2,0x93,0xd4,0x36,0x0b,0x98},
+ {0xe4,0x96,0xcf,0x2b,0x1c,0x8a},{0xe6,0x95,0xc6,0x20,0x11,0x84},
+ {0xe8,0x9c,0xf9,0x11,0x32,0xae},{0xea,0x9f,0xf0,0x1a,0x3f,0xa0},
+ {0xec,0x9a,0xeb,0x07,0x28,0xb2},{0xee,0x99,0xe2,0x0c,0x25,0xbc},
+ {0xf0,0x88,0x95,0x65,0x6e,0xe6},{0xf2,0x8b,0x9c,0x6e,0x63,0xe8},
+ {0xf4,0x8e,0x87,0x73,0x74,0xfa},{0xf6,0x8d,0x8e,0x78,0x79,0xf4},
+ {0xf8,0x84,0xb1,0x49,0x5a,0xde},{0xfa,0x87,0xb8,0x42,0x57,0xd0},
+ {0xfc,0x82,0xa3,0x5f,0x40,0xc2},{0xfe,0x81,0xaa,0x54,0x4d,0xcc},
+ {0x1b,0x9b,0xec,0xf7,0xda,0x41},{0x19,0x98,0xe5,0xfc,0xd7,0x4f},
+ {0x1f,0x9d,0xfe,0xe1,0xc0,0x5d},{0x1d,0x9e,0xf7,0xea,0xcd,0x53},
+ {0x13,0x97,0xc8,0xdb,0xee,0x79},{0x11,0x94,0xc1,0xd0,0xe3,0x77},
+ {0x17,0x91,0xda,0xcd,0xf4,0x65},{0x15,0x92,0xd3,0xc6,0xf9,0x6b},
+ {0x0b,0x83,0xa4,0xaf,0xb2,0x31},{0x09,0x80,0xad,0xa4,0xbf,0x3f},
+ {0x0f,0x85,0xb6,0xb9,0xa8,0x2d},{0x0d,0x86,0xbf,0xb2,0xa5,0x23},
+ {0x03,0x8f,0x80,0x83,0x86,0x09},{0x01,0x8c,0x89,0x88,0x8b,0x07},
+ {0x07,0x89,0x92,0x95,0x9c,0x15},{0x05,0x8a,0x9b,0x9e,0x91,0x1b},
+ {0x3b,0xab,0x7c,0x47,0x0a,0xa1},{0x39,0xa8,0x75,0x4c,0x07,0xaf},
+ {0x3f,0xad,0x6e,0x51,0x10,0xbd},{0x3d,0xae,0x67,0x5a,0x1d,0xb3},
+ {0x33,0xa7,0x58,0x6b,0x3e,0x99},{0x31,0xa4,0x51,0x60,0x33,0x97},
+ {0x37,0xa1,0x4a,0x7d,0x24,0x85},{0x35,0xa2,0x43,0x76,0x29,0x8b},
+ {0x2b,0xb3,0x34,0x1f,0x62,0xd1},{0x29,0xb0,0x3d,0x14,0x6f,0xdf},
+ {0x2f,0xb5,0x26,0x09,0x78,0xcd},{0x2d,0xb6,0x2f,0x02,0x75,0xc3},
+ {0x23,0xbf,0x10,0x33,0x56,0xe9},{0x21,0xbc,0x19,0x38,0x5b,0xe7},
+ {0x27,0xb9,0x02,0x25,0x4c,0xf5},{0x25,0xba,0x0b,0x2e,0x41,0xfb},
+ {0x5b,0xfb,0xd7,0x8c,0x61,0x9a},{0x59,0xf8,0xde,0x87,0x6c,0x94},
+ {0x5f,0xfd,0xc5,0x9a,0x7b,0x86},{0x5d,0xfe,0xcc,0x91,0x76,0x88},
+ {0x53,0xf7,0xf3,0xa0,0x55,0xa2},{0x51,0xf4,0xfa,0xab,0x58,0xac},
+ {0x57,0xf1,0xe1,0xb6,0x4f,0xbe},{0x55,0xf2,0xe8,0xbd,0x42,0xb0},
+ {0x4b,0xe3,0x9f,0xd4,0x09,0xea},{0x49,0xe0,0x96,0xdf,0x04,0xe4},
+ {0x4f,0xe5,0x8d,0xc2,0x13,0xf6},{0x4d,0xe6,0x84,0xc9,0x1e,0xf8},
+ {0x43,0xef,0xbb,0xf8,0x3d,0xd2},{0x41,0xec,0xb2,0xf3,0x30,0xdc},
+ {0x47,0xe9,0xa9,0xee,0x27,0xce},{0x45,0xea,0xa0,0xe5,0x2a,0xc0},
+ {0x7b,0xcb,0x47,0x3c,0xb1,0x7a},{0x79,0xc8,0x4e,0x37,0xbc,0x74},
+ {0x7f,0xcd,0x55,0x2a,0xab,0x66},{0x7d,0xce,0x5c,0x21,0xa6,0x68},
+ {0x73,0xc7,0x63,0x10,0x85,0x42},{0x71,0xc4,0x6a,0x1b,0x88,0x4c},
+ {0x77,0xc1,0x71,0x06,0x9f,0x5e},{0x75,0xc2,0x78,0x0d,0x92,0x50},
+ {0x6b,0xd3,0x0f,0x64,0xd9,0x0a},{0x69,0xd0,0x06,0x6f,0xd4,0x04},
+ {0x6f,0xd5,0x1d,0x72,0xc3,0x16},{0x6d,0xd6,0x14,0x79,0xce,0x18},
+ {0x63,0xdf,0x2b,0x48,0xed,0x32},{0x61,0xdc,0x22,0x43,0xe0,0x3c},
+ {0x67,0xd9,0x39,0x5e,0xf7,0x2e},{0x65,0xda,0x30,0x55,0xfa,0x20},
+ {0x9b,0x5b,0x9a,0x01,0xb7,0xec},{0x99,0x58,0x93,0x0a,0xba,0xe2},
+ {0x9f,0x5d,0x88,0x17,0xad,0xf0},{0x9d,0x5e,0x81,0x1c,0xa0,0xfe},
+ {0x93,0x57,0xbe,0x2d,0x83,0xd4},{0x91,0x54,0xb7,0x26,0x8e,0xda},
+ {0x97,0x51,0xac,0x3b,0x99,0xc8},{0x95,0x52,0xa5,0x30,0x94,0xc6},
+ {0x8b,0x43,0xd2,0x59,0xdf,0x9c},{0x89,0x40,0xdb,0x52,0xd2,0x92},
+ {0x8f,0x45,0xc0,0x4f,0xc5,0x80},{0x8d,0x46,0xc9,0x44,0xc8,0x8e},
+ {0x83,0x4f,0xf6,0x75,0xeb,0xa4},{0x81,0x4c,0xff,0x7e,0xe6,0xaa},
+ {0x87,0x49,0xe4,0x63,0xf1,0xb8},{0x85,0x4a,0xed,0x68,0xfc,0xb6},
+ {0xbb,0x6b,0x0a,0xb1,0x67,0x0c},{0xb9,0x68,0x03,0xba,0x6a,0x02},
+ {0xbf,0x6d,0x18,0xa7,0x7d,0x10},{0xbd,0x6e,0x11,0xac,0x70,0x1e},
+ {0xb3,0x67,0x2e,0x9d,0x53,0x34},{0xb1,0x64,0x27,0x96,0x5e,0x3a},
+ {0xb7,0x61,0x3c,0x8b,0x49,0x28},{0xb5,0x62,0x35,0x80,0x44,0x26},
+ {0xab,0x73,0x42,0xe9,0x0f,0x7c},{0xa9,0x70,0x4b,0xe2,0x02,0x72},
+ {0xaf,0x75,0x50,0xff,0x15,0x60},{0xad,0x76,0x59,0xf4,0x18,0x6e},
+ {0xa3,0x7f,0x66,0xc5,0x3b,0x44},{0xa1,0x7c,0x6f,0xce,0x36,0x4a},
+ {0xa7,0x79,0x74,0xd3,0x21,0x58},{0xa5,0x7a,0x7d,0xd8,0x2c,0x56},
+ {0xdb,0x3b,0xa1,0x7a,0x0c,0x37},{0xd9,0x38,0xa8,0x71,0x01,0x39},
+ {0xdf,0x3d,0xb3,0x6c,0x16,0x2b},{0xdd,0x3e,0xba,0x67,0x1b,0x25},
+ {0xd3,0x37,0x85,0x56,0x38,0x0f},{0xd1,0x34,0x8c,0x5d,0x35,0x01},
+ {0xd7,0x31,0x97,0x40,0x22,0x13},{0xd5,0x32,0x9e,0x4b,0x2f,0x1d},
+ {0xcb,0x23,0xe9,0x22,0x64,0x47},{0xc9,0x20,0xe0,0x29,0x69,0x49},
+ {0xcf,0x25,0xfb,0x34,0x7e,0x5b},{0xcd,0x26,0xf2,0x3f,0x73,0x55},
+ {0xc3,0x2f,0xcd,0x0e,0x50,0x7f},{0xc1,0x2c,0xc4,0x05,0x5d,0x71},
+ {0xc7,0x29,0xdf,0x18,0x4a,0x63},{0xc5,0x2a,0xd6,0x13,0x47,0x6d},
+ {0xfb,0x0b,0x31,0xca,0xdc,0xd7},{0xf9,0x08,0x38,0xc1,0xd1,0xd9},
+ {0xff,0x0d,0x23,0xdc,0xc6,0xcb},{0xfd,0x0e,0x2a,0xd7,0xcb,0xc5},
+ {0xf3,0x07,0x15,0xe6,0xe8,0xef},{0xf1,0x04,0x1c,0xed,0xe5,0xe1},
+ {0xf7,0x01,0x07,0xf0,0xf2,0xf3},{0xf5,0x02,0x0e,0xfb,0xff,0xfd},
+ {0xeb,0x13,0x79,0x92,0xb4,0xa7},{0xe9,0x10,0x70,0x99,0xb9,0xa9},
+ {0xef,0x15,0x6b,0x84,0xae,0xbb},{0xed,0x16,0x62,0x8f,0xa3,0xb5},
+ {0xe3,0x1f,0x5d,0xbe,0x80,0x9f},{0xe1,0x1c,0x54,0xb5,0x8d,0x91},
+ {0xe7,0x19,0x4f,0xa8,0x9a,0x83},{0xe5,0x1a,0x46,0xa3,0x97,0x8d}
+};
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+// XORs the in and out buffers, storing the result in out. Length is in bytes.
+void xor_buf(const BYTE in[], BYTE out[], size_t len)
+{
+ size_t idx;
+
+ for (idx = 0; idx < len; idx++)
+ out[idx] ^= in[idx];
+}
+
+/*******************
+* AES - CBC
+*******************/
+int aes_encrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
+{
+ BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
+ int blocks, idx;
+
+ if (in_len % AES_BLOCK_SIZE != 0)
+ return(FALSE);
+
+ blocks = in_len / AES_BLOCK_SIZE;
+
+ memcpy(iv_buf, iv, AES_BLOCK_SIZE);
+
+ for (idx = 0; idx < blocks; idx++) {
+ memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
+ xor_buf(iv_buf, buf_in, AES_BLOCK_SIZE);
+ aes_encrypt(buf_in, buf_out, key, keysize);
+ memcpy(&out[idx * AES_BLOCK_SIZE], buf_out, AES_BLOCK_SIZE);
+ memcpy(iv_buf, buf_out, AES_BLOCK_SIZE);
+ }
+
+ return(TRUE);
+}
+
+int aes_encrypt_cbc_mac(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
+{
+ BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
+ int blocks, idx;
+
+ if (in_len % AES_BLOCK_SIZE != 0)
+ return(FALSE);
+
+ blocks = in_len / AES_BLOCK_SIZE;
+
+ memcpy(iv_buf, iv, AES_BLOCK_SIZE);
+
+ for (idx = 0; idx < blocks; idx++) {
+ memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
+ xor_buf(iv_buf, buf_in, AES_BLOCK_SIZE);
+ aes_encrypt(buf_in, buf_out, key, keysize);
+ memcpy(iv_buf, buf_out, AES_BLOCK_SIZE);
+ // Do not output all encrypted blocks.
+ }
+
+ memcpy(out, buf_out, AES_BLOCK_SIZE); // Only output the last block.
+
+ return(TRUE);
+}
+
+int aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
+{
+ BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
+ int blocks, idx;
+
+ if (in_len % AES_BLOCK_SIZE != 0)
+ return(FALSE);
+
+ blocks = in_len / AES_BLOCK_SIZE;
+
+ memcpy(iv_buf, iv, AES_BLOCK_SIZE);
+
+ for (idx = 0; idx < blocks; idx++) {
+ memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
+ aes_decrypt(buf_in, buf_out, key, keysize);
+ xor_buf(iv_buf, buf_out, AES_BLOCK_SIZE);
+ memcpy(&out[idx * AES_BLOCK_SIZE], buf_out, AES_BLOCK_SIZE);
+ memcpy(iv_buf, buf_in, AES_BLOCK_SIZE);
+ }
+
+ return(TRUE);
+}
+
+/*******************
+* AES - CTR
+*******************/
+void increment_iv(BYTE iv[], int counter_size)
+{
+ int idx;
+
+ // Use counter_size bytes at the end of the IV as the big-endian integer to increment.
+ for (idx = AES_BLOCK_SIZE - 1; idx >= AES_BLOCK_SIZE - counter_size; idx--) {
+ iv[idx]++;
+ if (iv[idx] != 0 || idx == AES_BLOCK_SIZE - counter_size)
+ break;
+ }
+}
+
+// Performs the encryption in-place, the input and output buffers may be the same.
+// Input may be an arbitrary length (in bytes).
+void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
+{
+ size_t idx = 0, last_block_length;
+ BYTE iv_buf[AES_BLOCK_SIZE], out_buf[AES_BLOCK_SIZE];
+
+ if (in != out)
+ memcpy(out, in, in_len);
+
+ memcpy(iv_buf, iv, AES_BLOCK_SIZE);
+ last_block_length = in_len - AES_BLOCK_SIZE;
+
+ if (in_len > AES_BLOCK_SIZE) {
+ for (idx = 0; idx < last_block_length; idx += AES_BLOCK_SIZE) {
+ aes_encrypt(iv_buf, out_buf, key, keysize);
+ xor_buf(out_buf, &out[idx], AES_BLOCK_SIZE);
+ increment_iv(iv_buf, AES_BLOCK_SIZE);
+ }
+ }
+
+ aes_encrypt(iv_buf, out_buf, key, keysize);
+ xor_buf(out_buf, &out[idx], in_len - idx); // Use the Most Significant bytes.
+}
+
+void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[])
+{
+ // CTR encryption is its own inverse function.
+ aes_encrypt_ctr(in, in_len, out, key, keysize, iv);
+}
+
+/*******************
+* AES - CCM
+*******************/
+// out_len = payload_len + assoc_len
+int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], unsigned short assoc_len,
+ const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD *out_len,
+ WORD mac_len, const BYTE key_str[], int keysize)
+{
+ BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], *buf;
+ int end_of_buf, payload_len_store_size;
+ WORD key[60];
+
+ if (mac_len != 4 && mac_len != 6 && mac_len != 8 && mac_len != 10 &&
+ mac_len != 12 && mac_len != 14 && mac_len != 16)
+ return(FALSE);
+
+ if (nonce_len < 7 || nonce_len > 13)
+ return(FALSE);
+
+ if (assoc_len > 32768 /* = 2^15 */)
+ return(FALSE);
+
+ buf = (BYTE*)malloc(payload_len + assoc_len + 48 /*Round both payload and associated data up a block size and add an extra block.*/);
+ if (! buf)
+ return(FALSE);
+
+ // Prepare the key for usage.
+ aes_key_setup(key_str, key, keysize);
+
+ // Format the first block of the formatted data.
+ payload_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
+ ccm_prepare_first_format_blk(buf, assoc_len, payload_len, payload_len_store_size, mac_len, nonce, nonce_len);
+ end_of_buf = AES_BLOCK_SIZE;
+
+ // Format the Associated Data, aka, assoc[].
+ ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);
+
+ // Format the Payload, aka payload[].
+ ccm_format_payload_data(buf, &end_of_buf, payload, payload_len);
+
+ // Create the first counter block.
+ ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, payload_len_store_size);
+
+ // Perform the CBC operation with an IV of zeros on the formatted buffer to calculate the MAC.
+ memset(temp_iv, 0, AES_BLOCK_SIZE);
+ aes_encrypt_cbc_mac(buf, end_of_buf, mac, key, keysize, temp_iv);
+
+ // Copy the Payload and MAC to the output buffer.
+ memcpy(out, payload, payload_len);
+ memcpy(&out[payload_len], mac, mac_len);
+
+ // Encrypt the Payload with CTR mode with a counter starting at 1.
+ memcpy(temp_iv, counter, AES_BLOCK_SIZE);
+ increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // Last argument is the byte size of the counting portion of the counter block. /*BUG?*/
+ aes_encrypt_ctr(out, payload_len, out, key, keysize, temp_iv);
+
+ // Encrypt the MAC with CTR mode with a counter starting at 0.
+ aes_encrypt_ctr(&out[payload_len], mac_len, &out[payload_len], key, keysize, counter);
+
+ free(buf);
+ *out_len = payload_len + mac_len;
+
+ return(TRUE);
+}
+
+// plaintext_len = ciphertext_len - mac_len
+// Needs a flag for whether the MAC matches.
+int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE assoc[], unsigned short assoc_len,
+ const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD *plaintext_len,
+ WORD mac_len, int *mac_auth, const BYTE key_str[], int keysize)
+{
+ BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], mac_buf[16], *buf;
+ int end_of_buf, plaintext_len_store_size;
+ WORD key[60];
+
+ if (ciphertext_len <= mac_len)
+ return(FALSE);
+
+ buf = (BYTE*)malloc(assoc_len + ciphertext_len /*ciphertext_len = plaintext_len + mac_len*/ + 48);
+ if (! buf)
+ return(FALSE);
+
+ // Prepare the key for usage.
+ aes_key_setup(key_str, key, keysize);
+
+ // Copy the plaintext and MAC to the output buffers.
+ *plaintext_len = ciphertext_len - mac_len;
+ plaintext_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
+ memcpy(plaintext, ciphertext, *plaintext_len);
+ memcpy(mac, &ciphertext[*plaintext_len], mac_len);
+
+ // Prepare the first counter block for use in decryption.
+ ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, plaintext_len_store_size);
+
+ // Decrypt the Payload with CTR mode with a counter starting at 1.
+ memcpy(temp_iv, counter, AES_BLOCK_SIZE);
+ increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // (AES_BLOCK_SIZE - 1 - mac_len) is the byte size of the counting portion of the counter block.
+ aes_decrypt_ctr(plaintext, *plaintext_len, plaintext, key, keysize, temp_iv);
+
+ // Setting mac_auth to NULL disables the authentication check.
+ if (mac_auth != NULL) {
+ // Decrypt the MAC with CTR mode with a counter starting at 0.
+ aes_decrypt_ctr(mac, mac_len, mac, key, keysize, counter);
+
+ // Format the first block of the formatted data.
+ plaintext_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
+ ccm_prepare_first_format_blk(buf, assoc_len, *plaintext_len, plaintext_len_store_size, mac_len, nonce, nonce_len);
+ end_of_buf = AES_BLOCK_SIZE;
+
+ // Format the Associated Data into the authentication buffer.
+ ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);
+
+ // Format the Payload into the authentication buffer.
+ ccm_format_payload_data(buf, &end_of_buf, plaintext, *plaintext_len);
+
+ // Perform the CBC operation with an IV of zeros on the formatted buffer to calculate the MAC.
+ memset(temp_iv, 0, AES_BLOCK_SIZE);
+ aes_encrypt_cbc_mac(buf, end_of_buf, mac_buf, key, keysize, temp_iv);
+
+ // Compare the calculated MAC against the MAC embedded in the ciphertext to see if they are the same.
+ if (! memcmp(mac, mac_buf, mac_len)) {
+ *mac_auth = TRUE;
+ }
+ else {
+ *mac_auth = FALSE;
+ memset(plaintext, 0, *plaintext_len);
+ }
+ }
+
+ free(buf);
+
+ return(TRUE);
+}
+
+// Creates the first counter block. First byte is flags, then the nonce, then the incremented part.
+void ccm_prepare_first_ctr_blk(BYTE counter[], const BYTE nonce[], int nonce_len, int payload_len_store_size)
+{
+ memset(counter, 0, AES_BLOCK_SIZE);
+ counter[0] = (payload_len_store_size - 1) & 0x07;
+ memcpy(&counter[1], nonce, nonce_len);
+}
+
+void ccm_prepare_first_format_blk(BYTE buf[], int assoc_len, int payload_len, int payload_len_store_size, int mac_len, const BYTE nonce[], int nonce_len)
+{
+ // Set the flags for the first byte of the first block.
+ buf[0] = ((((mac_len - 2) / 2) & 0x07) << 3) | ((payload_len_store_size - 1) & 0x07);
+ if (assoc_len > 0)
+ buf[0] += 0x40;
+ // Format the rest of the first block, storing the nonce and the size of the payload.
+ memcpy(&buf[1], nonce, nonce_len);
+ memset(&buf[1 + nonce_len], 0, AES_BLOCK_SIZE - 1 - nonce_len);
+ buf[15] = payload_len & 0x000000FF;
+ buf[14] = (payload_len >> 8) & 0x000000FF;
+}
+
+void ccm_format_assoc_data(BYTE buf[], int *end_of_buf, const BYTE assoc[], int assoc_len)
+{
+ int pad;
+
+ buf[*end_of_buf + 1] = assoc_len & 0x00FF;
+ buf[*end_of_buf] = (assoc_len >> 8) & 0x00FF;
+ *end_of_buf += 2;
+ memcpy(&buf[*end_of_buf], assoc, assoc_len);
+ *end_of_buf += assoc_len;
+ pad = AES_BLOCK_SIZE - (*end_of_buf % AES_BLOCK_SIZE); /*BUG?*/
+ memset(&buf[*end_of_buf], 0, pad);
+ *end_of_buf += pad;
+}
+
+void ccm_format_payload_data(BYTE buf[], int *end_of_buf, const BYTE payload[], int payload_len)
+{
+ int pad;
+
+ memcpy(&buf[*end_of_buf], payload, payload_len);
+ *end_of_buf += payload_len;
+ pad = *end_of_buf % AES_BLOCK_SIZE;
+ if (pad != 0)
+ pad = AES_BLOCK_SIZE - pad;
+ memset(&buf[*end_of_buf], 0, pad);
+ *end_of_buf += pad;
+}
+
+/*******************
+* AES
+*******************/
+/////////////////
+// KEY EXPANSION
+/////////////////
+
+// Substitutes a word using the AES S-Box.
+WORD SubWord(WORD word)
+{
+ unsigned int result;
+
+ result = (int)aes_sbox[(word >> 4) & 0x0000000F][word & 0x0000000F];
+ result += (int)aes_sbox[(word >> 12) & 0x0000000F][(word >> 8) & 0x0000000F] << 8;
+ result += (int)aes_sbox[(word >> 20) & 0x0000000F][(word >> 16) & 0x0000000F] << 16;
+ result += (int)aes_sbox[(word >> 28) & 0x0000000F][(word >> 24) & 0x0000000F] << 24;
+ return(result);
+}
+
+// Performs the action of generating the keys that will be used in every round of
+// encryption. "key" is the user-supplied input key, "w" is the output key schedule,
+// "keysize" is the length in bits of "key", must be 128, 192, or 256.
+void aes_key_setup(const BYTE key[], WORD w[], int keysize)
+{
+ int Nb=4,Nr,Nk,idx;
+ WORD temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,
+ 0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,
+ 0xab000000,0x4d000000,0x9a000000};
+
+ switch (keysize) {
+ case 128: Nr = 10; Nk = 4; break;
+ case 192: Nr = 12; Nk = 6; break;
+ case 256: Nr = 14; Nk = 8; break;
+ default: return;
+ }
+
+ for (idx=0; idx < Nk; ++idx) {
+ w[idx] = ((key[4 * idx]) << 24) | ((key[4 * idx + 1]) << 16) |
+ ((key[4 * idx + 2]) << 8) | ((key[4 * idx + 3]));
+ }
+
+ for (idx = Nk; idx < Nb * (Nr+1); ++idx) {
+ temp = w[idx - 1];
+ if ((idx % Nk) == 0)
+ temp = SubWord(KE_ROTWORD(temp)) ^ Rcon[(idx-1)/Nk];
+ else if (Nk > 6 && (idx % Nk) == 4)
+ temp = SubWord(temp);
+ w[idx] = w[idx-Nk] ^ temp;
+ }
+}
+
+/////////////////
+// ADD ROUND KEY
+/////////////////
+
+// Performs the AddRoundKey step. Each round has its own pre-generated 16-byte key in the
+// form of 4 integers (the "w" array). Each integer is XOR'd by one column of the state.
+// Also performs the job of InvAddRoundKey(); since the function is a simple XOR process,
+// it is its own inverse.
+void AddRoundKey(BYTE state[][4], const WORD w[])
+{
+ BYTE subkey[4];
+
+ // memcpy(subkey,&w[idx],4); // Not accurate for big endian machines
+ // Subkey 1
+ subkey[0] = w[0] >> 24;
+ subkey[1] = w[0] >> 16;
+ subkey[2] = w[0] >> 8;
+ subkey[3] = w[0];
+ state[0][0] ^= subkey[0];
+ state[1][0] ^= subkey[1];
+ state[2][0] ^= subkey[2];
+ state[3][0] ^= subkey[3];
+ // Subkey 2
+ subkey[0] = w[1] >> 24;
+ subkey[1] = w[1] >> 16;
+ subkey[2] = w[1] >> 8;
+ subkey[3] = w[1];
+ state[0][1] ^= subkey[0];
+ state[1][1] ^= subkey[1];
+ state[2][1] ^= subkey[2];
+ state[3][1] ^= subkey[3];
+ // Subkey 3
+ subkey[0] = w[2] >> 24;
+ subkey[1] = w[2] >> 16;
+ subkey[2] = w[2] >> 8;
+ subkey[3] = w[2];
+ state[0][2] ^= subkey[0];
+ state[1][2] ^= subkey[1];
+ state[2][2] ^= subkey[2];
+ state[3][2] ^= subkey[3];
+ // Subkey 4
+ subkey[0] = w[3] >> 24;
+ subkey[1] = w[3] >> 16;
+ subkey[2] = w[3] >> 8;
+ subkey[3] = w[3];
+ state[0][3] ^= subkey[0];
+ state[1][3] ^= subkey[1];
+ state[2][3] ^= subkey[2];
+ state[3][3] ^= subkey[3];
+}
+
+/////////////////
+// (Inv)SubBytes
+/////////////////
+
+// Performs the SubBytes step. All bytes in the state are substituted with a
+// pre-calculated value from a lookup table.
+void SubBytes(BYTE state[][4])
+{
+ state[0][0] = aes_sbox[state[0][0] >> 4][state[0][0] & 0x0F];
+ state[0][1] = aes_sbox[state[0][1] >> 4][state[0][1] & 0x0F];
+ state[0][2] = aes_sbox[state[0][2] >> 4][state[0][2] & 0x0F];
+ state[0][3] = aes_sbox[state[0][3] >> 4][state[0][3] & 0x0F];
+ state[1][0] = aes_sbox[state[1][0] >> 4][state[1][0] & 0x0F];
+ state[1][1] = aes_sbox[state[1][1] >> 4][state[1][1] & 0x0F];
+ state[1][2] = aes_sbox[state[1][2] >> 4][state[1][2] & 0x0F];
+ state[1][3] = aes_sbox[state[1][3] >> 4][state[1][3] & 0x0F];
+ state[2][0] = aes_sbox[state[2][0] >> 4][state[2][0] & 0x0F];
+ state[2][1] = aes_sbox[state[2][1] >> 4][state[2][1] & 0x0F];
+ state[2][2] = aes_sbox[state[2][2] >> 4][state[2][2] & 0x0F];
+ state[2][3] = aes_sbox[state[2][3] >> 4][state[2][3] & 0x0F];
+ state[3][0] = aes_sbox[state[3][0] >> 4][state[3][0] & 0x0F];
+ state[3][1] = aes_sbox[state[3][1] >> 4][state[3][1] & 0x0F];
+ state[3][2] = aes_sbox[state[3][2] >> 4][state[3][2] & 0x0F];
+ state[3][3] = aes_sbox[state[3][3] >> 4][state[3][3] & 0x0F];
+}
+
+void InvSubBytes(BYTE state[][4])
+{
+ state[0][0] = aes_invsbox[state[0][0] >> 4][state[0][0] & 0x0F];
+ state[0][1] = aes_invsbox[state[0][1] >> 4][state[0][1] & 0x0F];
+ state[0][2] = aes_invsbox[state[0][2] >> 4][state[0][2] & 0x0F];
+ state[0][3] = aes_invsbox[state[0][3] >> 4][state[0][3] & 0x0F];
+ state[1][0] = aes_invsbox[state[1][0] >> 4][state[1][0] & 0x0F];
+ state[1][1] = aes_invsbox[state[1][1] >> 4][state[1][1] & 0x0F];
+ state[1][2] = aes_invsbox[state[1][2] >> 4][state[1][2] & 0x0F];
+ state[1][3] = aes_invsbox[state[1][3] >> 4][state[1][3] & 0x0F];
+ state[2][0] = aes_invsbox[state[2][0] >> 4][state[2][0] & 0x0F];
+ state[2][1] = aes_invsbox[state[2][1] >> 4][state[2][1] & 0x0F];
+ state[2][2] = aes_invsbox[state[2][2] >> 4][state[2][2] & 0x0F];
+ state[2][3] = aes_invsbox[state[2][3] >> 4][state[2][3] & 0x0F];
+ state[3][0] = aes_invsbox[state[3][0] >> 4][state[3][0] & 0x0F];
+ state[3][1] = aes_invsbox[state[3][1] >> 4][state[3][1] & 0x0F];
+ state[3][2] = aes_invsbox[state[3][2] >> 4][state[3][2] & 0x0F];
+ state[3][3] = aes_invsbox[state[3][3] >> 4][state[3][3] & 0x0F];
+}
+
+/////////////////
+// (Inv)ShiftRows
+/////////////////
+
+// Performs the ShiftRows step. All rows are shifted cylindrically to the left.
+void ShiftRows(BYTE state[][4])
+{
+ int t;
+
+ // Shift left by 1
+ t = state[1][0];
+ state[1][0] = state[1][1];
+ state[1][1] = state[1][2];
+ state[1][2] = state[1][3];
+ state[1][3] = t;
+ // Shift left by 2
+ t = state[2][0];
+ state[2][0] = state[2][2];
+ state[2][2] = t;
+ t = state[2][1];
+ state[2][1] = state[2][3];
+ state[2][3] = t;
+ // Shift left by 3
+ t = state[3][0];
+ state[3][0] = state[3][3];
+ state[3][3] = state[3][2];
+ state[3][2] = state[3][1];
+ state[3][1] = t;
+}
+
+// All rows are shifted cylindrically to the right.
+void InvShiftRows(BYTE state[][4])
+{
+ int t;
+
+ // Shift right by 1
+ t = state[1][3];
+ state[1][3] = state[1][2];
+ state[1][2] = state[1][1];
+ state[1][1] = state[1][0];
+ state[1][0] = t;
+ // Shift right by 2
+ t = state[2][3];
+ state[2][3] = state[2][1];
+ state[2][1] = t;
+ t = state[2][2];
+ state[2][2] = state[2][0];
+ state[2][0] = t;
+ // Shift right by 3
+ t = state[3][3];
+ state[3][3] = state[3][0];
+ state[3][0] = state[3][1];
+ state[3][1] = state[3][2];
+ state[3][2] = t;
+}
+
+/////////////////
+// (Inv)MixColumns
+/////////////////
+
+// Performs the MixColums step. The state is multiplied by itself using matrix
+// multiplication in a Galios Field 2^8. All multiplication is pre-computed in a table.
+// Addition is equivilent to XOR. (Must always make a copy of the column as the original
+// values will be destoyed.)
+void MixColumns(BYTE state[][4])
+{
+ BYTE col[4];
+
+ // Column 1
+ col[0] = state[0][0];
+ col[1] = state[1][0];
+ col[2] = state[2][0];
+ col[3] = state[3][0];
+ state[0][0] = gf_mul[col[0]][0];
+ state[0][0] ^= gf_mul[col[1]][1];
+ state[0][0] ^= col[2];
+ state[0][0] ^= col[3];
+ state[1][0] = col[0];
+ state[1][0] ^= gf_mul[col[1]][0];
+ state[1][0] ^= gf_mul[col[2]][1];
+ state[1][0] ^= col[3];
+ state[2][0] = col[0];
+ state[2][0] ^= col[1];
+ state[2][0] ^= gf_mul[col[2]][0];
+ state[2][0] ^= gf_mul[col[3]][1];
+ state[3][0] = gf_mul[col[0]][1];
+ state[3][0] ^= col[1];
+ state[3][0] ^= col[2];
+ state[3][0] ^= gf_mul[col[3]][0];
+ // Column 2
+ col[0] = state[0][1];
+ col[1] = state[1][1];
+ col[2] = state[2][1];
+ col[3] = state[3][1];
+ state[0][1] = gf_mul[col[0]][0];
+ state[0][1] ^= gf_mul[col[1]][1];
+ state[0][1] ^= col[2];
+ state[0][1] ^= col[3];
+ state[1][1] = col[0];
+ state[1][1] ^= gf_mul[col[1]][0];
+ state[1][1] ^= gf_mul[col[2]][1];
+ state[1][1] ^= col[3];
+ state[2][1] = col[0];
+ state[2][1] ^= col[1];
+ state[2][1] ^= gf_mul[col[2]][0];
+ state[2][1] ^= gf_mul[col[3]][1];
+ state[3][1] = gf_mul[col[0]][1];
+ state[3][1] ^= col[1];
+ state[3][1] ^= col[2];
+ state[3][1] ^= gf_mul[col[3]][0];
+ // Column 3
+ col[0] = state[0][2];
+ col[1] = state[1][2];
+ col[2] = state[2][2];
+ col[3] = state[3][2];
+ state[0][2] = gf_mul[col[0]][0];
+ state[0][2] ^= gf_mul[col[1]][1];
+ state[0][2] ^= col[2];
+ state[0][2] ^= col[3];
+ state[1][2] = col[0];
+ state[1][2] ^= gf_mul[col[1]][0];
+ state[1][2] ^= gf_mul[col[2]][1];
+ state[1][2] ^= col[3];
+ state[2][2] = col[0];
+ state[2][2] ^= col[1];
+ state[2][2] ^= gf_mul[col[2]][0];
+ state[2][2] ^= gf_mul[col[3]][1];
+ state[3][2] = gf_mul[col[0]][1];
+ state[3][2] ^= col[1];
+ state[3][2] ^= col[2];
+ state[3][2] ^= gf_mul[col[3]][0];
+ // Column 4
+ col[0] = state[0][3];
+ col[1] = state[1][3];
+ col[2] = state[2][3];
+ col[3] = state[3][3];
+ state[0][3] = gf_mul[col[0]][0];
+ state[0][3] ^= gf_mul[col[1]][1];
+ state[0][3] ^= col[2];
+ state[0][3] ^= col[3];
+ state[1][3] = col[0];
+ state[1][3] ^= gf_mul[col[1]][0];
+ state[1][3] ^= gf_mul[col[2]][1];
+ state[1][3] ^= col[3];
+ state[2][3] = col[0];
+ state[2][3] ^= col[1];
+ state[2][3] ^= gf_mul[col[2]][0];
+ state[2][3] ^= gf_mul[col[3]][1];
+ state[3][3] = gf_mul[col[0]][1];
+ state[3][3] ^= col[1];
+ state[3][3] ^= col[2];
+ state[3][3] ^= gf_mul[col[3]][0];
+}
+
+void InvMixColumns(BYTE state[][4])
+{
+ BYTE col[4];
+
+ // Column 1
+ col[0] = state[0][0];
+ col[1] = state[1][0];
+ col[2] = state[2][0];
+ col[3] = state[3][0];
+ state[0][0] = gf_mul[col[0]][5];
+ state[0][0] ^= gf_mul[col[1]][3];
+ state[0][0] ^= gf_mul[col[2]][4];
+ state[0][0] ^= gf_mul[col[3]][2];
+ state[1][0] = gf_mul[col[0]][2];
+ state[1][0] ^= gf_mul[col[1]][5];
+ state[1][0] ^= gf_mul[col[2]][3];
+ state[1][0] ^= gf_mul[col[3]][4];
+ state[2][0] = gf_mul[col[0]][4];
+ state[2][0] ^= gf_mul[col[1]][2];
+ state[2][0] ^= gf_mul[col[2]][5];
+ state[2][0] ^= gf_mul[col[3]][3];
+ state[3][0] = gf_mul[col[0]][3];
+ state[3][0] ^= gf_mul[col[1]][4];
+ state[3][0] ^= gf_mul[col[2]][2];
+ state[3][0] ^= gf_mul[col[3]][5];
+ // Column 2
+ col[0] = state[0][1];
+ col[1] = state[1][1];
+ col[2] = state[2][1];
+ col[3] = state[3][1];
+ state[0][1] = gf_mul[col[0]][5];
+ state[0][1] ^= gf_mul[col[1]][3];
+ state[0][1] ^= gf_mul[col[2]][4];
+ state[0][1] ^= gf_mul[col[3]][2];
+ state[1][1] = gf_mul[col[0]][2];
+ state[1][1] ^= gf_mul[col[1]][5];
+ state[1][1] ^= gf_mul[col[2]][3];
+ state[1][1] ^= gf_mul[col[3]][4];
+ state[2][1] = gf_mul[col[0]][4];
+ state[2][1] ^= gf_mul[col[1]][2];
+ state[2][1] ^= gf_mul[col[2]][5];
+ state[2][1] ^= gf_mul[col[3]][3];
+ state[3][1] = gf_mul[col[0]][3];
+ state[3][1] ^= gf_mul[col[1]][4];
+ state[3][1] ^= gf_mul[col[2]][2];
+ state[3][1] ^= gf_mul[col[3]][5];
+ // Column 3
+ col[0] = state[0][2];
+ col[1] = state[1][2];
+ col[2] = state[2][2];
+ col[3] = state[3][2];
+ state[0][2] = gf_mul[col[0]][5];
+ state[0][2] ^= gf_mul[col[1]][3];
+ state[0][2] ^= gf_mul[col[2]][4];
+ state[0][2] ^= gf_mul[col[3]][2];
+ state[1][2] = gf_mul[col[0]][2];
+ state[1][2] ^= gf_mul[col[1]][5];
+ state[1][2] ^= gf_mul[col[2]][3];
+ state[1][2] ^= gf_mul[col[3]][4];
+ state[2][2] = gf_mul[col[0]][4];
+ state[2][2] ^= gf_mul[col[1]][2];
+ state[2][2] ^= gf_mul[col[2]][5];
+ state[2][2] ^= gf_mul[col[3]][3];
+ state[3][2] = gf_mul[col[0]][3];
+ state[3][2] ^= gf_mul[col[1]][4];
+ state[3][2] ^= gf_mul[col[2]][2];
+ state[3][2] ^= gf_mul[col[3]][5];
+ // Column 4
+ col[0] = state[0][3];
+ col[1] = state[1][3];
+ col[2] = state[2][3];
+ col[3] = state[3][3];
+ state[0][3] = gf_mul[col[0]][5];
+ state[0][3] ^= gf_mul[col[1]][3];
+ state[0][3] ^= gf_mul[col[2]][4];
+ state[0][3] ^= gf_mul[col[3]][2];
+ state[1][3] = gf_mul[col[0]][2];
+ state[1][3] ^= gf_mul[col[1]][5];
+ state[1][3] ^= gf_mul[col[2]][3];
+ state[1][3] ^= gf_mul[col[3]][4];
+ state[2][3] = gf_mul[col[0]][4];
+ state[2][3] ^= gf_mul[col[1]][2];
+ state[2][3] ^= gf_mul[col[2]][5];
+ state[2][3] ^= gf_mul[col[3]][3];
+ state[3][3] = gf_mul[col[0]][3];
+ state[3][3] ^= gf_mul[col[1]][4];
+ state[3][3] ^= gf_mul[col[2]][2];
+ state[3][3] ^= gf_mul[col[3]][5];
+}
+
+/////////////////
+// (En/De)Crypt
+/////////////////
+
+void aes_encrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize)
+{
+ BYTE state[4][4];
+
+ // Copy input array (should be 16 bytes long) to a matrix (sequential bytes are ordered
+ // by row, not col) called "state" for processing.
+ // *** Implementation note: The official AES documentation references the state by
+ // column, then row. Accessing an element in C requires row then column. Thus, all state
+ // references in AES must have the column and row indexes reversed for C implementation.
+ state[0][0] = in[0];
+ state[1][0] = in[1];
+ state[2][0] = in[2];
+ state[3][0] = in[3];
+ state[0][1] = in[4];
+ state[1][1] = in[5];
+ state[2][1] = in[6];
+ state[3][1] = in[7];
+ state[0][2] = in[8];
+ state[1][2] = in[9];
+ state[2][2] = in[10];
+ state[3][2] = in[11];
+ state[0][3] = in[12];
+ state[1][3] = in[13];
+ state[2][3] = in[14];
+ state[3][3] = in[15];
+
+ // Perform the necessary number of rounds. The round key is added first.
+ // The last round does not perform the MixColumns step.
+ AddRoundKey(state,&key[0]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[4]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[8]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[12]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[16]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[20]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[24]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[28]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[32]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[36]);
+ if (keysize != 128) {
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[40]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[44]);
+ if (keysize != 192) {
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[48]);
+ SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[52]);
+ SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[56]);
+ }
+ else {
+ SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[48]);
+ }
+ }
+ else {
+ SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[40]);
+ }
+
+ // Copy the state to the output array.
+ out[0] = state[0][0];
+ out[1] = state[1][0];
+ out[2] = state[2][0];
+ out[3] = state[3][0];
+ out[4] = state[0][1];
+ out[5] = state[1][1];
+ out[6] = state[2][1];
+ out[7] = state[3][1];
+ out[8] = state[0][2];
+ out[9] = state[1][2];
+ out[10] = state[2][2];
+ out[11] = state[3][2];
+ out[12] = state[0][3];
+ out[13] = state[1][3];
+ out[14] = state[2][3];
+ out[15] = state[3][3];
+}
+
+void aes_decrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize)
+{
+ BYTE state[4][4];
+
+ // Copy the input to the state.
+ state[0][0] = in[0];
+ state[1][0] = in[1];
+ state[2][0] = in[2];
+ state[3][0] = in[3];
+ state[0][1] = in[4];
+ state[1][1] = in[5];
+ state[2][1] = in[6];
+ state[3][1] = in[7];
+ state[0][2] = in[8];
+ state[1][2] = in[9];
+ state[2][2] = in[10];
+ state[3][2] = in[11];
+ state[0][3] = in[12];
+ state[1][3] = in[13];
+ state[2][3] = in[14];
+ state[3][3] = in[15];
+
+ // Perform the necessary number of rounds. The round key is added first.
+ // The last round does not perform the MixColumns step.
+ if (keysize > 128) {
+ if (keysize > 192) {
+ AddRoundKey(state,&key[56]);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[52]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[48]);InvMixColumns(state);
+ }
+ else {
+ AddRoundKey(state,&key[48]);
+ }
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[44]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[40]);InvMixColumns(state);
+ }
+ else {
+ AddRoundKey(state,&key[40]);
+ }
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[36]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[32]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[28]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[24]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[20]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[16]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[12]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[8]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[4]);InvMixColumns(state);
+ InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[0]);
+
+ // Copy the state to the output array.
+ out[0] = state[0][0];
+ out[1] = state[1][0];
+ out[2] = state[2][0];
+ out[3] = state[3][0];
+ out[4] = state[0][1];
+ out[5] = state[1][1];
+ out[6] = state[2][1];
+ out[7] = state[3][1];
+ out[8] = state[0][2];
+ out[9] = state[1][2];
+ out[10] = state[2][2];
+ out[11] = state[3][2];
+ out[12] = state[0][3];
+ out[13] = state[1][3];
+ out[14] = state[2][3];
+ out[15] = state[3][3];
+}
+
+/*******************
+** AES DEBUGGING FUNCTIONS
+*******************/
+/*
+// This prints the "state" grid as a linear hex string.
+void print_state(BYTE state[][4])
+{
+ int idx,idx2;
+
+ for (idx=0; idx < 4; idx++)
+ for (idx2=0; idx2 < 4; idx2++)
+ printf("%02x",state[idx2][idx]);
+ printf("\n");
+}
+
+// This prints the key (4 consecutive ints) used for a given round as a linear hex string.
+void print_rnd_key(WORD key[])
+{
+ int idx;
+
+ for (idx=0; idx < 4; idx++)
+ printf("%08x",key[idx]);
+ printf("\n");
+}
+*/
diff --git a/test/monniaux/crypto-algorithms/aes.h b/test/monniaux/crypto-algorithms/aes.h
new file mode 100644
index 00000000..25721c8c
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/aes.h
@@ -0,0 +1,123 @@
+/*********************************************************************
+* Filename: aes.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding AES implementation.
+*********************************************************************/
+
+#ifndef AES_H
+#define AES_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+/*********************** FUNCTION DECLARATIONS **********************/
+///////////////////
+// AES
+///////////////////
+// Key setup must be done before any AES en/de-cryption functions can be used.
+void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
+ WORD w[], // Output key schedule to be used later
+ int keysize); // Bit length of the key, 128, 192, or 256
+
+void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
+ BYTE out[], // 16 bytes of ciphertext
+ const WORD key[], // From the key setup
+ int keysize); // Bit length of the key, 128, 192, or 256
+
+void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
+ BYTE out[], // 16 bytes of plaintext
+ const WORD key[], // From the key setup
+ int keysize); // Bit length of the key, 128, 192, or 256
+
+///////////////////
+// AES - CBC
+///////////////////
+int aes_encrypt_cbc(const BYTE in[], // Plaintext
+ size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
+ BYTE out[], // Ciphertext, same length as plaintext
+ const WORD key[], // From the key setup
+ int keysize, // Bit length of the key, 128, 192, or 256
+ const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
+
+// Only output the CBC-MAC of the input.
+int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
+ size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
+ BYTE out[], // Output MAC
+ const WORD key[], // From the key setup
+ int keysize, // Bit length of the key, 128, 192, or 256
+ const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
+
+///////////////////
+// AES - CTR
+///////////////////
+void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE
+ int counter_size); // Bytes of the IV used for counting (low end)
+
+void aes_encrypt_ctr(const BYTE in[], // Plaintext
+ size_t in_len, // Any byte length
+ BYTE out[], // Ciphertext, same length as plaintext
+ const WORD key[], // From the key setup
+ int keysize, // Bit length of the key, 128, 192, or 256
+ const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
+
+void aes_decrypt_ctr(const BYTE in[], // Ciphertext
+ size_t in_len, // Any byte length
+ BYTE out[], // Plaintext, same length as ciphertext
+ const WORD key[], // From the key setup
+ int keysize, // Bit length of the key, 128, 192, or 256
+ const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
+
+///////////////////
+// AES - CCM
+///////////////////
+// Returns True if the input parameters do not violate any constraint.
+int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
+ WORD plaintext_len, // IN - Plaintext length.
+ const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
+ unsigned short associated_data_len, // IN - Associated Data length in bytes.
+ const BYTE nonce[], // IN - The Nonce to be used for encryption.
+ unsigned short nonce_len, // IN - Nonce length in bytes.
+ BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
+ WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
+ WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
+ const BYTE key[], // IN - The AES key for encryption.
+ int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
+
+// Returns True if the input parameters do not violate any constraint.
+// Use mac_auth to ensure decryption/validation was preformed correctly.
+// If authentication does not succeed, the plaintext is zeroed out. To overwride
+// this, call with mac_auth = NULL. The proper proceedure is to decrypt with
+// authentication enabled (mac_auth != NULL) and make a second call to that
+// ignores authentication explicitly if the first call failes.
+int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
+ WORD ciphertext_len, // IN - Ciphertext length in bytes.
+ const BYTE assoc[], // IN - The Associated Data, required for authentication.
+ unsigned short assoc_len, // IN - Associated Data length in bytes.
+ const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
+ unsigned short nonce_len, // IN - Nonce length in bytes.
+ BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
+ WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
+ WORD mac_len, // IN - The length of the MAC that was calculated.
+ int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
+ const BYTE key[], // IN - The AES key for decryption.
+ int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
+
+///////////////////
+// Test functions
+///////////////////
+int aes_test();
+int aes_ecb_test();
+int aes_cbc_test();
+int aes_ctr_test();
+int aes_ccm_test();
+
+#endif // AES_H
diff --git a/test/monniaux/crypto-algorithms/aes_test.c b/test/monniaux/crypto-algorithms/aes_test.c
new file mode 100644
index 00000000..b8da963b
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/aes_test.c
@@ -0,0 +1,285 @@
+/*********************************************************************
+* Filename: aes_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding AES
+ implementation. These tests do not encompass the full
+ range of available test vectors and are not sufficient
+ for FIPS-140 certification. However, if the tests pass
+ it is very, very likely that the code is correct and was
+ compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include "aes.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void print_hex(BYTE str[], int len)
+{
+ int idx;
+
+ for(idx = 0; idx < len; idx++)
+ printf("%02x", str[idx]);
+}
+
+int aes_ecb_test()
+{
+ WORD key_schedule[60], idx;
+ BYTE enc_buf[128];
+ BYTE plaintext[2][16] = {
+ {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a},
+ {0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
+ };
+ BYTE ciphertext[2][16] = {
+ {0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8},
+ {0x59,0x1c,0xcb,0x10,0xd4,0x10,0xed,0x26,0xdc,0x5b,0xa7,0x4a,0x31,0x36,0x28,0x70}
+ };
+ BYTE key[1][32] = {
+ {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
+ };
+ int pass = 1;
+
+ // Raw ECB mode.
+ //printf("* ECB mode:\n");
+ aes_key_setup(key[0], key_schedule, 256);
+ //printf( "Key : ");
+ //print_hex(key[0], 32);
+
+ for(idx = 0; idx < 2; idx++) {
+ aes_encrypt(plaintext[idx], enc_buf, key_schedule, 256);
+ //printf("\nPlaintext : ");
+ //print_hex(plaintext[idx], 16);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, 16);
+ pass = pass && !memcmp(enc_buf, ciphertext[idx], 16);
+
+ aes_decrypt(ciphertext[idx], enc_buf, key_schedule, 256);
+ //printf("\nCiphertext : ");
+ //print_hex(ciphertext[idx], 16);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, 16);
+ pass = pass && !memcmp(enc_buf, plaintext[idx], 16);
+
+ //printf("\n\n");
+ }
+
+ return(pass);
+}
+
+int aes_cbc_test()
+{
+ WORD key_schedule[60];
+ BYTE enc_buf[128];
+ BYTE plaintext[1][32] = {
+ {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
+ };
+ BYTE ciphertext[1][32] = {
+ {0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6,0x9c,0xfc,0x4e,0x96,0x7e,0xdb,0x80,0x8d,0x67,0x9f,0x77,0x7b,0xc6,0x70,0x2c,0x7d}
+ };
+ BYTE iv[1][16] = {
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
+ };
+ BYTE key[1][32] = {
+ {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
+ };
+ int pass = 1;
+
+ //printf("* CBC mode:\n");
+ aes_key_setup(key[0], key_schedule, 256);
+
+ //printf( "Key : ");
+ //print_hex(key[0], 32);
+ //printf("\nIV : ");
+ //print_hex(iv[0], 16);
+
+ aes_encrypt_cbc(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
+ //printf("\nPlaintext : ");
+ //print_hex(plaintext[0], 32);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, 32);
+ //printf("\nCiphertext : ");
+ //print_hex(ciphertext[0], 32);
+ pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
+
+ aes_decrypt_cbc(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]);
+ //printf("\nCiphertext : ");
+ //print_hex(ciphertext[0], 32);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, 32);
+ //printf("\nPlaintext : ");
+ //print_hex(plaintext[0], 32);
+ pass = pass && !memcmp(enc_buf, plaintext[0], 32);
+
+ //printf("\n\n");
+ return(pass);
+}
+
+int aes_ctr_test()
+{
+ WORD key_schedule[60];
+ BYTE enc_buf[128];
+ BYTE plaintext[1][32] = {
+ {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
+ };
+ BYTE ciphertext[1][32] = {
+ {0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5}
+ };
+ BYTE iv[1][16] = {
+ {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff},
+ };
+ BYTE key[1][32] = {
+ {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
+ };
+ int pass = 1;
+
+ //printf("* CTR mode:\n");
+ aes_key_setup(key[0], key_schedule, 256);
+
+ //printf( "Key : ");
+ //print_hex(key[0], 32);
+ //printf("\nIV : ");
+ //print_hex(iv[0], 16);
+
+ aes_encrypt_ctr(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
+ //printf("\nPlaintext : ");
+ //print_hex(plaintext[0], 32);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, 32);
+ pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
+
+ aes_decrypt_ctr(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]);
+ //printf("\nCiphertext : ");
+ //print_hex(ciphertext[0], 32);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, 32);
+ pass = pass && !memcmp(enc_buf, plaintext[0], 32);
+
+ //printf("\n\n");
+ return(pass);
+}
+
+int aes_ccm_test()
+{
+ int mac_auth;
+ WORD enc_buf_len;
+ BYTE enc_buf[128];
+ BYTE plaintext[3][32] = {
+ {0x20,0x21,0x22,0x23},
+ {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f},
+ {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37}
+ };
+ BYTE assoc[3][32] = {
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07},
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f},
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13}
+ };
+ BYTE ciphertext[3][32 + 16] = {
+ {0x71,0x62,0x01,0x5b,0x4d,0xac,0x25,0x5d},
+ {0xd2,0xa1,0xf0,0xe0,0x51,0xea,0x5f,0x62,0x08,0x1a,0x77,0x92,0x07,0x3d,0x59,0x3d,0x1f,0xc6,0x4f,0xbf,0xac,0xcd},
+ {0xe3,0xb2,0x01,0xa9,0xf5,0xb7,0x1a,0x7a,0x9b,0x1c,0xea,0xec,0xcd,0x97,0xe7,0x0b,0x61,0x76,0xaa,0xd9,0xa4,0x42,0x8a,0xa5,0x48,0x43,0x92,0xfb,0xc1,0xb0,0x99,0x51}
+ };
+ BYTE iv[3][16] = {
+ {0x10,0x11,0x12,0x13,0x14,0x15,0x16},
+ {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17},
+ {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b}
+ };
+ BYTE key[1][32] = {
+ {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f}
+ };
+ int pass = 1;
+
+ //printf("* CCM mode:\n");
+ //printf("Key : ");
+ //print_hex(key[0], 16);
+
+ //print_hex(plaintext[0], 4);
+ //print_hex(assoc[0], 8);
+ //print_hex(ciphertext[0], 8);
+ //print_hex(iv[0], 7);
+ //print_hex(key[0], 16);
+
+ aes_encrypt_ccm(plaintext[0], 4, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, key[0], 128);
+ //printf("\nNONCE : ");
+ //print_hex(iv[0], 7);
+ //printf("\nAssoc. Data : ");
+ //print_hex(assoc[0], 8);
+ //printf("\nPayload : ");
+ //print_hex(plaintext[0], 4);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ pass = pass && !memcmp(enc_buf, ciphertext[0], enc_buf_len);
+
+ aes_decrypt_ccm(ciphertext[0], 8, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, &mac_auth, key[0], 128);
+ //printf("\n-Ciphertext : ");
+ //print_hex(ciphertext[0], 8);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ //printf("\nAuthenticated: %d ", mac_auth);
+ pass = pass && !memcmp(enc_buf, plaintext[0], enc_buf_len) && mac_auth;
+
+
+ aes_encrypt_ccm(plaintext[1], 16, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, key[0], 128);
+ //printf("\n\nNONCE : ");
+ //print_hex(iv[1], 8);
+ //printf("\nAssoc. Data : ");
+ //print_hex(assoc[1], 16);
+ //printf("\nPayload : ");
+ //print_hex(plaintext[1], 16);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ pass = pass && !memcmp(enc_buf, ciphertext[1], enc_buf_len);
+
+ aes_decrypt_ccm(ciphertext[1], 22, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, &mac_auth, key[0], 128);
+ //printf("\n-Ciphertext : ");
+ //print_hex(ciphertext[1], 22);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ //printf("\nAuthenticated: %d ", mac_auth);
+ pass = pass && !memcmp(enc_buf, plaintext[1], enc_buf_len) && mac_auth;
+
+
+ aes_encrypt_ccm(plaintext[2], 24, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, key[0], 128);
+ //printf("\n\nNONCE : ");
+ //print_hex(iv[2], 12);
+ //printf("\nAssoc. Data : ");
+ //print_hex(assoc[2], 20);
+ //printf("\nPayload : ");
+ //print_hex(plaintext[2], 24);
+ //printf("\n-encrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ pass = pass && !memcmp(enc_buf, ciphertext[2], enc_buf_len);
+
+ aes_decrypt_ccm(ciphertext[2], 32, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, &mac_auth, key[0], 128);
+ //printf("\n-Ciphertext : ");
+ //print_hex(ciphertext[2], 32);
+ //printf("\n-decrypted to: ");
+ //print_hex(enc_buf, enc_buf_len);
+ //printf("\nAuthenticated: %d ", mac_auth);
+ pass = pass && !memcmp(enc_buf, plaintext[2], enc_buf_len) && mac_auth;
+
+ //printf("\n\n");
+ return(pass);
+}
+
+int aes_test()
+{
+ int pass = 1;
+
+ pass = pass && aes_ecb_test();
+ pass = pass && aes_cbc_test();
+ pass = pass && aes_ctr_test();
+ pass = pass && aes_ccm_test();
+
+ return(pass);
+}
+
+int main(int argc, char *argv[])
+{
+ printf("AES Tests: %s\n", aes_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/arcfour.c b/test/monniaux/crypto-algorithms/arcfour.c
new file mode 100644
index 00000000..37a88e10
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/arcfour.c
@@ -0,0 +1,47 @@
+/*********************************************************************
+* Filename: arcfour.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the ARCFOUR encryption algorithm.
+ Algorithm specification can be found here:
+ * http://en.wikipedia.org/wiki/RC4
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include "arcfour.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void arcfour_key_setup(BYTE state[], const BYTE key[], int len)
+{
+ int i, j;
+ BYTE t;
+
+ for (i = 0; i < 256; ++i)
+ state[i] = i;
+ for (i = 0, j = 0; i < 256; ++i) {
+ j = (j + state[i] + key[i % len]) % 256;
+ t = state[i];
+ state[i] = state[j];
+ state[j] = t;
+ }
+}
+
+// This does not hold state between calls. It always generates the
+// stream starting from the first output byte.
+void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len)
+{
+ int i, j;
+ size_t idx;
+ BYTE t;
+
+ for (idx = 0, i = 0, j = 0; idx < len; ++idx) {
+ i = (i + 1) % 256;
+ j = (j + state[i]) % 256;
+ t = state[i];
+ state[i] = state[j];
+ state[j] = t;
+ out[idx] = state[(state[i] + state[j]) % 256];
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/arcfour.h b/test/monniaux/crypto-algorithms/arcfour.h
new file mode 100644
index 00000000..f9f1e87d
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/arcfour.h
@@ -0,0 +1,30 @@
+/*********************************************************************
+* Filename: arcfour.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding ARCFOUR implementation.
+*********************************************************************/
+
+#ifndef ARCFOUR_H
+#define ARCFOUR_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+
+/*********************** FUNCTION DECLARATIONS **********************/
+// Input: state - the state used to generate the keystream
+// key - Key to use to initialize the state
+// len - length of key in bytes (valid lenth is 1 to 256)
+void arcfour_key_setup(BYTE state[], const BYTE key[], int len);
+
+// Pseudo-Random Generator Algorithm
+// Input: state - the state used to generate the keystream
+// out - Must be allocated to be of at least "len" length
+// len - number of bytes to generate
+void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len);
+
+#endif // ARCFOUR_H
diff --git a/test/monniaux/crypto-algorithms/arcfour_test.c b/test/monniaux/crypto-algorithms/arcfour_test.c
new file mode 100644
index 00000000..985f8a78
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/arcfour_test.c
@@ -0,0 +1,47 @@
+/*********************************************************************
+* Filename: arcfour_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding ARCFOUR
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include "arcfour.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int rc4_test()
+{
+ BYTE state[256];
+ BYTE key[3][10] = {{"Key"}, {"Wiki"}, {"Secret"}};
+ BYTE stream[3][10] = {{0xEB,0x9F,0x77,0x81,0xB7,0x34,0xCA,0x72,0xA7,0x19},
+ {0x60,0x44,0xdb,0x6d,0x41,0xb7},
+ {0x04,0xd4,0x6b,0x05,0x3c,0xa8,0x7b,0x59}};
+ int stream_len[3] = {10,6,8};
+ BYTE buf[1024];
+ int idx;
+ int pass = 1;
+
+ // Only test the output stream. Note that the state can be reused.
+ for (idx = 0; idx < 3; idx++) {
+ arcfour_key_setup(state, key[idx], strlen(key[idx]));
+ arcfour_generate_stream(state, buf, stream_len[idx]);
+ pass = pass && !memcmp(stream[idx], buf, stream_len[idx]);
+ }
+
+ return(pass);
+}
+
+int main()
+{
+ printf("ARCFOUR tests: %s\n", rc4_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/base64.c b/test/monniaux/crypto-algorithms/base64.c
new file mode 100644
index 00000000..5e89808c
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/base64.c
@@ -0,0 +1,135 @@
+/*********************************************************************
+* Filename: base64.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the Base64 encoding algorithm.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include "base64.h"
+
+/****************************** MACROS ******************************/
+#define NEWLINE_INVL 76
+
+/**************************** VARIABLES *****************************/
+// Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-'
+static const BYTE charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+BYTE revchar(char ch)
+{
+ if (ch >= 'A' && ch <= 'Z')
+ ch -= 'A';
+ else if (ch >= 'a' && ch <='z')
+ ch = ch - 'a' + 26;
+ else if (ch >= '0' && ch <='9')
+ ch = ch - '0' + 52;
+ else if (ch == '+')
+ ch = 62;
+ else if (ch == '/')
+ ch = 63;
+
+ return(ch);
+}
+
+size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag)
+{
+ size_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0;
+
+ blks = (len / 3);
+ left_over = len % 3;
+
+ if (out == NULL) {
+ idx2 = blks * 4 ;
+ if (left_over)
+ idx2 += 4;
+ if (newline_flag)
+ idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input bytes.
+ }
+ else {
+ // Since 3 input bytes = 4 output bytes, determine out how many even sets of
+ // 3 bytes the input has.
+ blk_ceiling = blks * 3;
+ for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) {
+ out[idx2] = charset[in[idx] >> 2];
+ out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
+ out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)];
+ out[idx2 + 3] = charset[in[idx + 2] & 0x3F];
+ // The offical standard requires a newline every 76 characters.
+ // (Eg, first newline is character 77 of the output.)
+ if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) {
+ out[idx2 + 4] = '\n';
+ idx2++;
+ newline_count++;
+ }
+ }
+
+ if (left_over == 1) {
+ out[idx2] = charset[in[idx] >> 2];
+ out[idx2 + 1] = charset[(in[idx] & 0x03) << 4];
+ out[idx2 + 2] = '=';
+ out[idx2 + 3] = '=';
+ idx2 += 4;
+ }
+ else if (left_over == 2) {
+ out[idx2] = charset[in[idx] >> 2];
+ out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
+ out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2];
+ out[idx2 + 3] = '=';
+ idx2 += 4;
+ }
+ }
+
+ return(idx2);
+}
+
+size_t base64_decode(const BYTE in[], BYTE out[], size_t len)
+{
+ BYTE ch;
+ size_t idx, idx2, blks, blk_ceiling, left_over;
+
+ if (in[len - 1] == '=')
+ len--;
+ if (in[len - 1] == '=')
+ len--;
+
+ blks = len / 4;
+ left_over = len % 4;
+
+ if (out == NULL) {
+ if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used.
+ len -= len / (NEWLINE_INVL + 1);
+ blks = len / 4;
+ left_over = len % 4;
+
+ idx = blks * 3;
+ if (left_over == 2)
+ idx ++;
+ else if (left_over == 3)
+ idx += 2;
+ }
+ else {
+ blk_ceiling = blks * 4;
+ for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) {
+ if (in[idx2] == '\n')
+ idx2++;
+ out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
+ out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
+ out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]);
+ }
+
+ if (left_over == 2) {
+ out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
+ idx++;
+ }
+ else if (left_over == 3) {
+ out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
+ out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
+ idx += 2;
+ }
+ }
+
+ return(idx);
+}
diff --git a/test/monniaux/crypto-algorithms/base64.h b/test/monniaux/crypto-algorithms/base64.h
new file mode 100644
index 00000000..e35c6c7d
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/base64.h
@@ -0,0 +1,27 @@
+/*********************************************************************
+* Filename: base64.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding Base64 implementation.
+*********************************************************************/
+
+#ifndef BASE64_H
+#define BASE64_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+
+/*********************** FUNCTION DECLARATIONS **********************/
+// Returns the size of the output. If called with out = NULL, will just return
+// the size of what the output would have been (without a terminating NULL).
+size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag);
+
+// Returns the size of the output. If called with out = NULL, will just return
+// the size of what the output would have been (without a terminating NULL).
+size_t base64_decode(const BYTE in[], BYTE out[], size_t len);
+
+#endif // BASE64_H
diff --git a/test/monniaux/crypto-algorithms/base64_test.c b/test/monniaux/crypto-algorithms/base64_test.c
new file mode 100644
index 00000000..c59cc98d
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/base64_test.c
@@ -0,0 +1,54 @@
+/*********************************************************************
+* Filename: blowfish_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding Base64
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include "base64.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int base64_test()
+{
+ BYTE text[3][1024] = {{"fo"},
+ {"foobar"},
+ {"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."}};
+ BYTE code[3][1024] = {{"Zm8="},
+ {"Zm9vYmFy"},
+ {"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="}};
+ BYTE buf[1024];
+ size_t buf_len;
+ int pass = 1;
+ int idx;
+
+ for (idx = 0; idx < 3; idx++) {
+ buf_len = base64_encode(text[idx], buf, strlen(text[idx]), 1);
+ pass = pass && ((buf_len == strlen(code[idx])) &&
+ (buf_len == base64_encode(text[idx], NULL, strlen(text[idx]), 1)));
+ pass = pass && !strcmp(code[idx], buf);
+
+ memset(buf, 0, sizeof(buf));
+ buf_len = base64_decode(code[idx], buf, strlen(code[idx]));
+ pass = pass && ((buf_len == strlen(text[idx])) &&
+ (buf_len == base64_decode(code[idx], NULL, strlen(code[idx]))));
+ pass = pass && !strcmp(text[idx], buf);
+ }
+
+ return(pass);
+}
+
+int main()
+{
+ printf("Base64 tests: %s\n", base64_test() ? "PASSED" : "FAILED");
+
+ return 0;
+}
diff --git a/test/monniaux/crypto-algorithms/blowfish.c b/test/monniaux/crypto-algorithms/blowfish.c
new file mode 100644
index 00000000..cea8f280
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/blowfish.c
@@ -0,0 +1,269 @@
+/*********************************************************************
+* Filename: blowfish.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the Blowfish encryption algorithm.
+ Modes of operation (such as CBC) are not included.
+ Algorithm specification can be found here:
+ * http://www.schneier.com/blowfish.html
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "blowfish.h"
+
+/****************************** MACROS ******************************/
+#define F(x,t) t = keystruct->s[0][(x) >> 24]; \
+ t += keystruct->s[1][((x) >> 16) & 0xff]; \
+ t ^= keystruct->s[2][((x) >> 8) & 0xff]; \
+ t += keystruct->s[3][(x) & 0xff];
+#define swap(r,l,t) t = l; l = r; r = t;
+#define ITERATION(l,r,t,pval) l ^= keystruct->p[pval]; F(l,t); r^= t; swap(r,l,t);
+
+/**************************** VARIABLES *****************************/
+static const WORD p_perm[18] = {
+ 0x243F6A88,0x85A308D3,0x13198A2E,0x03707344,0xA4093822,0x299F31D0,0x082EFA98,
+ 0xEC4E6C89,0x452821E6,0x38D01377,0xBE5466CF,0x34E90C6C,0xC0AC29B7,0xC97C50DD,
+ 0x3F84D5B5,0xB5470917,0x9216D5D9,0x8979FB1B
+};
+
+static const WORD s_perm[4][256] = { {
+ 0xD1310BA6,0x98DFB5AC,0x2FFD72DB,0xD01ADFB7,0xB8E1AFED,0x6A267E96,0xBA7C9045,0xF12C7F99,
+ 0x24A19947,0xB3916CF7,0x0801F2E2,0x858EFC16,0x636920D8,0x71574E69,0xA458FEA3,0xF4933D7E,
+ 0x0D95748F,0x728EB658,0x718BCD58,0x82154AEE,0x7B54A41D,0xC25A59B5,0x9C30D539,0x2AF26013,
+ 0xC5D1B023,0x286085F0,0xCA417918,0xB8DB38EF,0x8E79DCB0,0x603A180E,0x6C9E0E8B,0xB01E8A3E,
+ 0xD71577C1,0xBD314B27,0x78AF2FDA,0x55605C60,0xE65525F3,0xAA55AB94,0x57489862,0x63E81440,
+ 0x55CA396A,0x2AAB10B6,0xB4CC5C34,0x1141E8CE,0xA15486AF,0x7C72E993,0xB3EE1411,0x636FBC2A,
+ 0x2BA9C55D,0x741831F6,0xCE5C3E16,0x9B87931E,0xAFD6BA33,0x6C24CF5C,0x7A325381,0x28958677,
+ 0x3B8F4898,0x6B4BB9AF,0xC4BFE81B,0x66282193,0x61D809CC,0xFB21A991,0x487CAC60,0x5DEC8032,
+ 0xEF845D5D,0xE98575B1,0xDC262302,0xEB651B88,0x23893E81,0xD396ACC5,0x0F6D6FF3,0x83F44239,
+ 0x2E0B4482,0xA4842004,0x69C8F04A,0x9E1F9B5E,0x21C66842,0xF6E96C9A,0x670C9C61,0xABD388F0,
+ 0x6A51A0D2,0xD8542F68,0x960FA728,0xAB5133A3,0x6EEF0B6C,0x137A3BE4,0xBA3BF050,0x7EFB2A98,
+ 0xA1F1651D,0x39AF0176,0x66CA593E,0x82430E88,0x8CEE8619,0x456F9FB4,0x7D84A5C3,0x3B8B5EBE,
+ 0xE06F75D8,0x85C12073,0x401A449F,0x56C16AA6,0x4ED3AA62,0x363F7706,0x1BFEDF72,0x429B023D,
+ 0x37D0D724,0xD00A1248,0xDB0FEAD3,0x49F1C09B,0x075372C9,0x80991B7B,0x25D479D8,0xF6E8DEF7,
+ 0xE3FE501A,0xB6794C3B,0x976CE0BD,0x04C006BA,0xC1A94FB6,0x409F60C4,0x5E5C9EC2,0x196A2463,
+ 0x68FB6FAF,0x3E6C53B5,0x1339B2EB,0x3B52EC6F,0x6DFC511F,0x9B30952C,0xCC814544,0xAF5EBD09,
+ 0xBEE3D004,0xDE334AFD,0x660F2807,0x192E4BB3,0xC0CBA857,0x45C8740F,0xD20B5F39,0xB9D3FBDB,
+ 0x5579C0BD,0x1A60320A,0xD6A100C6,0x402C7279,0x679F25FE,0xFB1FA3CC,0x8EA5E9F8,0xDB3222F8,
+ 0x3C7516DF,0xFD616B15,0x2F501EC8,0xAD0552AB,0x323DB5FA,0xFD238760,0x53317B48,0x3E00DF82,
+ 0x9E5C57BB,0xCA6F8CA0,0x1A87562E,0xDF1769DB,0xD542A8F6,0x287EFFC3,0xAC6732C6,0x8C4F5573,
+ 0x695B27B0,0xBBCA58C8,0xE1FFA35D,0xB8F011A0,0x10FA3D98,0xFD2183B8,0x4AFCB56C,0x2DD1D35B,
+ 0x9A53E479,0xB6F84565,0xD28E49BC,0x4BFB9790,0xE1DDF2DA,0xA4CB7E33,0x62FB1341,0xCEE4C6E8,
+ 0xEF20CADA,0x36774C01,0xD07E9EFE,0x2BF11FB4,0x95DBDA4D,0xAE909198,0xEAAD8E71,0x6B93D5A0,
+ 0xD08ED1D0,0xAFC725E0,0x8E3C5B2F,0x8E7594B7,0x8FF6E2FB,0xF2122B64,0x8888B812,0x900DF01C,
+ 0x4FAD5EA0,0x688FC31C,0xD1CFF191,0xB3A8C1AD,0x2F2F2218,0xBE0E1777,0xEA752DFE,0x8B021FA1,
+ 0xE5A0CC0F,0xB56F74E8,0x18ACF3D6,0xCE89E299,0xB4A84FE0,0xFD13E0B7,0x7CC43B81,0xD2ADA8D9,
+ 0x165FA266,0x80957705,0x93CC7314,0x211A1477,0xE6AD2065,0x77B5FA86,0xC75442F5,0xFB9D35CF,
+ 0xEBCDAF0C,0x7B3E89A0,0xD6411BD3,0xAE1E7E49,0x00250E2D,0x2071B35E,0x226800BB,0x57B8E0AF,
+ 0x2464369B,0xF009B91E,0x5563911D,0x59DFA6AA,0x78C14389,0xD95A537F,0x207D5BA2,0x02E5B9C5,
+ 0x83260376,0x6295CFA9,0x11C81968,0x4E734A41,0xB3472DCA,0x7B14A94A,0x1B510052,0x9A532915,
+ 0xD60F573F,0xBC9BC6E4,0x2B60A476,0x81E67400,0x08BA6FB5,0x571BE91F,0xF296EC6B,0x2A0DD915,
+ 0xB6636521,0xE7B9F9B6,0xFF34052E,0xC5855664,0x53B02D5D,0xA99F8FA1,0x08BA4799,0x6E85076A
+},{
+ 0x4B7A70E9,0xB5B32944,0xDB75092E,0xC4192623,0xAD6EA6B0,0x49A7DF7D,0x9CEE60B8,0x8FEDB266,
+ 0xECAA8C71,0x699A17FF,0x5664526C,0xC2B19EE1,0x193602A5,0x75094C29,0xA0591340,0xE4183A3E,
+ 0x3F54989A,0x5B429D65,0x6B8FE4D6,0x99F73FD6,0xA1D29C07,0xEFE830F5,0x4D2D38E6,0xF0255DC1,
+ 0x4CDD2086,0x8470EB26,0x6382E9C6,0x021ECC5E,0x09686B3F,0x3EBAEFC9,0x3C971814,0x6B6A70A1,
+ 0x687F3584,0x52A0E286,0xB79C5305,0xAA500737,0x3E07841C,0x7FDEAE5C,0x8E7D44EC,0x5716F2B8,
+ 0xB03ADA37,0xF0500C0D,0xF01C1F04,0x0200B3FF,0xAE0CF51A,0x3CB574B2,0x25837A58,0xDC0921BD,
+ 0xD19113F9,0x7CA92FF6,0x94324773,0x22F54701,0x3AE5E581,0x37C2DADC,0xC8B57634,0x9AF3DDA7,
+ 0xA9446146,0x0FD0030E,0xECC8C73E,0xA4751E41,0xE238CD99,0x3BEA0E2F,0x3280BBA1,0x183EB331,
+ 0x4E548B38,0x4F6DB908,0x6F420D03,0xF60A04BF,0x2CB81290,0x24977C79,0x5679B072,0xBCAF89AF,
+ 0xDE9A771F,0xD9930810,0xB38BAE12,0xDCCF3F2E,0x5512721F,0x2E6B7124,0x501ADDE6,0x9F84CD87,
+ 0x7A584718,0x7408DA17,0xBC9F9ABC,0xE94B7D8C,0xEC7AEC3A,0xDB851DFA,0x63094366,0xC464C3D2,
+ 0xEF1C1847,0x3215D908,0xDD433B37,0x24C2BA16,0x12A14D43,0x2A65C451,0x50940002,0x133AE4DD,
+ 0x71DFF89E,0x10314E55,0x81AC77D6,0x5F11199B,0x043556F1,0xD7A3C76B,0x3C11183B,0x5924A509,
+ 0xF28FE6ED,0x97F1FBFA,0x9EBABF2C,0x1E153C6E,0x86E34570,0xEAE96FB1,0x860E5E0A,0x5A3E2AB3,
+ 0x771FE71C,0x4E3D06FA,0x2965DCB9,0x99E71D0F,0x803E89D6,0x5266C825,0x2E4CC978,0x9C10B36A,
+ 0xC6150EBA,0x94E2EA78,0xA5FC3C53,0x1E0A2DF4,0xF2F74EA7,0x361D2B3D,0x1939260F,0x19C27960,
+ 0x5223A708,0xF71312B6,0xEBADFE6E,0xEAC31F66,0xE3BC4595,0xA67BC883,0xB17F37D1,0x018CFF28,
+ 0xC332DDEF,0xBE6C5AA5,0x65582185,0x68AB9802,0xEECEA50F,0xDB2F953B,0x2AEF7DAD,0x5B6E2F84,
+ 0x1521B628,0x29076170,0xECDD4775,0x619F1510,0x13CCA830,0xEB61BD96,0x0334FE1E,0xAA0363CF,
+ 0xB5735C90,0x4C70A239,0xD59E9E0B,0xCBAADE14,0xEECC86BC,0x60622CA7,0x9CAB5CAB,0xB2F3846E,
+ 0x648B1EAF,0x19BDF0CA,0xA02369B9,0x655ABB50,0x40685A32,0x3C2AB4B3,0x319EE9D5,0xC021B8F7,
+ 0x9B540B19,0x875FA099,0x95F7997E,0x623D7DA8,0xF837889A,0x97E32D77,0x11ED935F,0x16681281,
+ 0x0E358829,0xC7E61FD6,0x96DEDFA1,0x7858BA99,0x57F584A5,0x1B227263,0x9B83C3FF,0x1AC24696,
+ 0xCDB30AEB,0x532E3054,0x8FD948E4,0x6DBC3128,0x58EBF2EF,0x34C6FFEA,0xFE28ED61,0xEE7C3C73,
+ 0x5D4A14D9,0xE864B7E3,0x42105D14,0x203E13E0,0x45EEE2B6,0xA3AAABEA,0xDB6C4F15,0xFACB4FD0,
+ 0xC742F442,0xEF6ABBB5,0x654F3B1D,0x41CD2105,0xD81E799E,0x86854DC7,0xE44B476A,0x3D816250,
+ 0xCF62A1F2,0x5B8D2646,0xFC8883A0,0xC1C7B6A3,0x7F1524C3,0x69CB7492,0x47848A0B,0x5692B285,
+ 0x095BBF00,0xAD19489D,0x1462B174,0x23820E00,0x58428D2A,0x0C55F5EA,0x1DADF43E,0x233F7061,
+ 0x3372F092,0x8D937E41,0xD65FECF1,0x6C223BDB,0x7CDE3759,0xCBEE7460,0x4085F2A7,0xCE77326E,
+ 0xA6078084,0x19F8509E,0xE8EFD855,0x61D99735,0xA969A7AA,0xC50C06C2,0x5A04ABFC,0x800BCADC,
+ 0x9E447A2E,0xC3453484,0xFDD56705,0x0E1E9EC9,0xDB73DBD3,0x105588CD,0x675FDA79,0xE3674340,
+ 0xC5C43465,0x713E38D8,0x3D28F89E,0xF16DFF20,0x153E21E7,0x8FB03D4A,0xE6E39F2B,0xDB83ADF7
+},{
+ 0xE93D5A68,0x948140F7,0xF64C261C,0x94692934,0x411520F7,0x7602D4F7,0xBCF46B2E,0xD4A20068,
+ 0xD4082471,0x3320F46A,0x43B7D4B7,0x500061AF,0x1E39F62E,0x97244546,0x14214F74,0xBF8B8840,
+ 0x4D95FC1D,0x96B591AF,0x70F4DDD3,0x66A02F45,0xBFBC09EC,0x03BD9785,0x7FAC6DD0,0x31CB8504,
+ 0x96EB27B3,0x55FD3941,0xDA2547E6,0xABCA0A9A,0x28507825,0x530429F4,0x0A2C86DA,0xE9B66DFB,
+ 0x68DC1462,0xD7486900,0x680EC0A4,0x27A18DEE,0x4F3FFEA2,0xE887AD8C,0xB58CE006,0x7AF4D6B6,
+ 0xAACE1E7C,0xD3375FEC,0xCE78A399,0x406B2A42,0x20FE9E35,0xD9F385B9,0xEE39D7AB,0x3B124E8B,
+ 0x1DC9FAF7,0x4B6D1856,0x26A36631,0xEAE397B2,0x3A6EFA74,0xDD5B4332,0x6841E7F7,0xCA7820FB,
+ 0xFB0AF54E,0xD8FEB397,0x454056AC,0xBA489527,0x55533A3A,0x20838D87,0xFE6BA9B7,0xD096954B,
+ 0x55A867BC,0xA1159A58,0xCCA92963,0x99E1DB33,0xA62A4A56,0x3F3125F9,0x5EF47E1C,0x9029317C,
+ 0xFDF8E802,0x04272F70,0x80BB155C,0x05282CE3,0x95C11548,0xE4C66D22,0x48C1133F,0xC70F86DC,
+ 0x07F9C9EE,0x41041F0F,0x404779A4,0x5D886E17,0x325F51EB,0xD59BC0D1,0xF2BCC18F,0x41113564,
+ 0x257B7834,0x602A9C60,0xDFF8E8A3,0x1F636C1B,0x0E12B4C2,0x02E1329E,0xAF664FD1,0xCAD18115,
+ 0x6B2395E0,0x333E92E1,0x3B240B62,0xEEBEB922,0x85B2A20E,0xE6BA0D99,0xDE720C8C,0x2DA2F728,
+ 0xD0127845,0x95B794FD,0x647D0862,0xE7CCF5F0,0x5449A36F,0x877D48FA,0xC39DFD27,0xF33E8D1E,
+ 0x0A476341,0x992EFF74,0x3A6F6EAB,0xF4F8FD37,0xA812DC60,0xA1EBDDF8,0x991BE14C,0xDB6E6B0D,
+ 0xC67B5510,0x6D672C37,0x2765D43B,0xDCD0E804,0xF1290DC7,0xCC00FFA3,0xB5390F92,0x690FED0B,
+ 0x667B9FFB,0xCEDB7D9C,0xA091CF0B,0xD9155EA3,0xBB132F88,0x515BAD24,0x7B9479BF,0x763BD6EB,
+ 0x37392EB3,0xCC115979,0x8026E297,0xF42E312D,0x6842ADA7,0xC66A2B3B,0x12754CCC,0x782EF11C,
+ 0x6A124237,0xB79251E7,0x06A1BBE6,0x4BFB6350,0x1A6B1018,0x11CAEDFA,0x3D25BDD8,0xE2E1C3C9,
+ 0x44421659,0x0A121386,0xD90CEC6E,0xD5ABEA2A,0x64AF674E,0xDA86A85F,0xBEBFE988,0x64E4C3FE,
+ 0x9DBC8057,0xF0F7C086,0x60787BF8,0x6003604D,0xD1FD8346,0xF6381FB0,0x7745AE04,0xD736FCCC,
+ 0x83426B33,0xF01EAB71,0xB0804187,0x3C005E5F,0x77A057BE,0xBDE8AE24,0x55464299,0xBF582E61,
+ 0x4E58F48F,0xF2DDFDA2,0xF474EF38,0x8789BDC2,0x5366F9C3,0xC8B38E74,0xB475F255,0x46FCD9B9,
+ 0x7AEB2661,0x8B1DDF84,0x846A0E79,0x915F95E2,0x466E598E,0x20B45770,0x8CD55591,0xC902DE4C,
+ 0xB90BACE1,0xBB8205D0,0x11A86248,0x7574A99E,0xB77F19B6,0xE0A9DC09,0x662D09A1,0xC4324633,
+ 0xE85A1F02,0x09F0BE8C,0x4A99A025,0x1D6EFE10,0x1AB93D1D,0x0BA5A4DF,0xA186F20F,0x2868F169,
+ 0xDCB7DA83,0x573906FE,0xA1E2CE9B,0x4FCD7F52,0x50115E01,0xA70683FA,0xA002B5C4,0x0DE6D027,
+ 0x9AF88C27,0x773F8641,0xC3604C06,0x61A806B5,0xF0177A28,0xC0F586E0,0x006058AA,0x30DC7D62,
+ 0x11E69ED7,0x2338EA63,0x53C2DD94,0xC2C21634,0xBBCBEE56,0x90BCB6DE,0xEBFC7DA1,0xCE591D76,
+ 0x6F05E409,0x4B7C0188,0x39720A3D,0x7C927C24,0x86E3725F,0x724D9DB9,0x1AC15BB4,0xD39EB8FC,
+ 0xED545578,0x08FCA5B5,0xD83D7CD3,0x4DAD0FC4,0x1E50EF5E,0xB161E6F8,0xA28514D9,0x6C51133C,
+ 0x6FD5C7E7,0x56E14EC4,0x362ABFCE,0xDDC6C837,0xD79A3234,0x92638212,0x670EFA8E,0x406000E0
+},{
+ 0x3A39CE37,0xD3FAF5CF,0xABC27737,0x5AC52D1B,0x5CB0679E,0x4FA33742,0xD3822740,0x99BC9BBE,
+ 0xD5118E9D,0xBF0F7315,0xD62D1C7E,0xC700C47B,0xB78C1B6B,0x21A19045,0xB26EB1BE,0x6A366EB4,
+ 0x5748AB2F,0xBC946E79,0xC6A376D2,0x6549C2C8,0x530FF8EE,0x468DDE7D,0xD5730A1D,0x4CD04DC6,
+ 0x2939BBDB,0xA9BA4650,0xAC9526E8,0xBE5EE304,0xA1FAD5F0,0x6A2D519A,0x63EF8CE2,0x9A86EE22,
+ 0xC089C2B8,0x43242EF6,0xA51E03AA,0x9CF2D0A4,0x83C061BA,0x9BE96A4D,0x8FE51550,0xBA645BD6,
+ 0x2826A2F9,0xA73A3AE1,0x4BA99586,0xEF5562E9,0xC72FEFD3,0xF752F7DA,0x3F046F69,0x77FA0A59,
+ 0x80E4A915,0x87B08601,0x9B09E6AD,0x3B3EE593,0xE990FD5A,0x9E34D797,0x2CF0B7D9,0x022B8B51,
+ 0x96D5AC3A,0x017DA67D,0xD1CF3ED6,0x7C7D2D28,0x1F9F25CF,0xADF2B89B,0x5AD6B472,0x5A88F54C,
+ 0xE029AC71,0xE019A5E6,0x47B0ACFD,0xED93FA9B,0xE8D3C48D,0x283B57CC,0xF8D56629,0x79132E28,
+ 0x785F0191,0xED756055,0xF7960E44,0xE3D35E8C,0x15056DD4,0x88F46DBA,0x03A16125,0x0564F0BD,
+ 0xC3EB9E15,0x3C9057A2,0x97271AEC,0xA93A072A,0x1B3F6D9B,0x1E6321F5,0xF59C66FB,0x26DCF319,
+ 0x7533D928,0xB155FDF5,0x03563482,0x8ABA3CBB,0x28517711,0xC20AD9F8,0xABCC5167,0xCCAD925F,
+ 0x4DE81751,0x3830DC8E,0x379D5862,0x9320F991,0xEA7A90C2,0xFB3E7BCE,0x5121CE64,0x774FBE32,
+ 0xA8B6E37E,0xC3293D46,0x48DE5369,0x6413E680,0xA2AE0810,0xDD6DB224,0x69852DFD,0x09072166,
+ 0xB39A460A,0x6445C0DD,0x586CDECF,0x1C20C8AE,0x5BBEF7DD,0x1B588D40,0xCCD2017F,0x6BB4E3BB,
+ 0xDDA26A7E,0x3A59FF45,0x3E350A44,0xBCB4CDD5,0x72EACEA8,0xFA6484BB,0x8D6612AE,0xBF3C6F47,
+ 0xD29BE463,0x542F5D9E,0xAEC2771B,0xF64E6370,0x740E0D8D,0xE75B1357,0xF8721671,0xAF537D5D,
+ 0x4040CB08,0x4EB4E2CC,0x34D2466A,0x0115AF84,0xE1B00428,0x95983A1D,0x06B89FB4,0xCE6EA048,
+ 0x6F3F3B82,0x3520AB82,0x011A1D4B,0x277227F8,0x611560B1,0xE7933FDC,0xBB3A792B,0x344525BD,
+ 0xA08839E1,0x51CE794B,0x2F32C9B7,0xA01FBAC9,0xE01CC87E,0xBCC7D1F6,0xCF0111C3,0xA1E8AAC7,
+ 0x1A908749,0xD44FBD9A,0xD0DADECB,0xD50ADA38,0x0339C32A,0xC6913667,0x8DF9317C,0xE0B12B4F,
+ 0xF79E59B7,0x43F5BB3A,0xF2D519FF,0x27D9459C,0xBF97222C,0x15E6FC2A,0x0F91FC71,0x9B941525,
+ 0xFAE59361,0xCEB69CEB,0xC2A86459,0x12BAA8D1,0xB6C1075E,0xE3056A0C,0x10D25065,0xCB03A442,
+ 0xE0EC6E0E,0x1698DB3B,0x4C98A0BE,0x3278E964,0x9F1F9532,0xE0D392DF,0xD3A0342B,0x8971F21E,
+ 0x1B0A7441,0x4BA3348C,0xC5BE7120,0xC37632D8,0xDF359F8D,0x9B992F2E,0xE60B6F47,0x0FE3F11D,
+ 0xE54CDA54,0x1EDAD891,0xCE6279CF,0xCD3E7E6F,0x1618B166,0xFD2C1D05,0x848FD2C5,0xF6FB2299,
+ 0xF523F357,0xA6327623,0x93A83531,0x56CCCD02,0xACF08162,0x5A75EBB5,0x6E163697,0x88D273CC,
+ 0xDE966292,0x81B949D0,0x4C50901B,0x71C65614,0xE6C6C7BD,0x327A140A,0x45E1D006,0xC3F27B9A,
+ 0xC9AA53FD,0x62A80F00,0xBB25BFE2,0x35BDD2F6,0x71126905,0xB2040222,0xB6CBCF7C,0xCD769C2B,
+ 0x53113EC0,0x1640E3D3,0x38ABBD60,0x2547ADF0,0xBA38209C,0xF746CE76,0x77AFA1C5,0x20756060,
+ 0x85CBFE4E,0x8AE88DD8,0x7AAAF9B0,0x4CF9AA7E,0x1948C25C,0x02FB8A8C,0x01C36AE4,0xD6EBE1F9,
+ 0x90D4F869,0xA65CDEA0,0x3F09252D,0xC208E69F,0xB74E6132,0xCE77E25B,0x578FDFE3,0x3AC372E6
+} };
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void blowfish_encrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct)
+{
+ WORD l,r,t; //,i;
+
+ l = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | (in[3]);
+ r = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | (in[7]);
+
+ ITERATION(l,r,t,0);
+ ITERATION(l,r,t,1);
+ ITERATION(l,r,t,2);
+ ITERATION(l,r,t,3);
+ ITERATION(l,r,t,4);
+ ITERATION(l,r,t,5);
+ ITERATION(l,r,t,6);
+ ITERATION(l,r,t,7);
+ ITERATION(l,r,t,8);
+ ITERATION(l,r,t,9);
+ ITERATION(l,r,t,10);
+ ITERATION(l,r,t,11);
+ ITERATION(l,r,t,12);
+ ITERATION(l,r,t,13);
+ ITERATION(l,r,t,14);
+ l ^= keystruct->p[15]; F(l,t); r^= t; //Last iteration has no swap()
+ r ^= keystruct->p[16];
+ l ^= keystruct->p[17];
+
+ out[0] = l >> 24;
+ out[1] = l >> 16;
+ out[2] = l >> 8;
+ out[3] = l;
+ out[4] = r >> 24;
+ out[5] = r >> 16;
+ out[6] = r >> 8;
+ out[7] = r;
+}
+
+void blowfish_decrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct)
+{
+ WORD l,r,t; //,i;
+
+ l = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | (in[3]);
+ r = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | (in[7]);
+
+ ITERATION(l,r,t,17);
+ ITERATION(l,r,t,16);
+ ITERATION(l,r,t,15);
+ ITERATION(l,r,t,14);
+ ITERATION(l,r,t,13);
+ ITERATION(l,r,t,12);
+ ITERATION(l,r,t,11);
+ ITERATION(l,r,t,10);
+ ITERATION(l,r,t,9);
+ ITERATION(l,r,t,8);
+ ITERATION(l,r,t,7);
+ ITERATION(l,r,t,6);
+ ITERATION(l,r,t,5);
+ ITERATION(l,r,t,4);
+ ITERATION(l,r,t,3);
+ l ^= keystruct->p[2]; F(l,t); r^= t; //Last iteration has no swap()
+ r ^= keystruct->p[1];
+ l ^= keystruct->p[0];
+
+ out[0] = l >> 24;
+ out[1] = l >> 16;
+ out[2] = l >> 8;
+ out[3] = l;
+ out[4] = r >> 24;
+ out[5] = r >> 16;
+ out[6] = r >> 8;
+ out[7] = r;
+}
+
+void blowfish_key_setup(const BYTE user_key[], BLOWFISH_KEY *keystruct, size_t len)
+{
+ BYTE block[8];
+ int idx,idx2;
+
+ // Copy over the constant init array vals (so the originals aren't destroyed).
+ memcpy(keystruct->p,p_perm,sizeof(WORD) * 18);
+ memcpy(keystruct->s,s_perm,sizeof(WORD) * 1024);
+
+ // Combine the key with the P box. Assume key is standard 448 bits (56 bytes) or less.
+ for (idx = 0, idx2 = 0; idx < 18; ++idx, idx2 += 4)
+ keystruct->p[idx] ^= (user_key[idx2 % len] << 24) | (user_key[(idx2+1) % len] << 16)
+ | (user_key[(idx2+2) % len] << 8) | (user_key[(idx2+3) % len]);
+ // Re-calculate the P box.
+ memset(block, 0, 8);
+ for (idx = 0; idx < 18; idx += 2) {
+ blowfish_encrypt(block,block,keystruct);
+ keystruct->p[idx] = (block[0] << 24) | (block[1] << 16) | (block[2] << 8) | block[3];
+ keystruct->p[idx+1]=(block[4] << 24) | (block[5] << 16) | (block[6] << 8) | block[7];
+ }
+ // Recalculate the S-boxes.
+ for (idx = 0; idx < 4; ++idx) {
+ for (idx2 = 0; idx2 < 256; idx2 += 2) {
+ blowfish_encrypt(block,block,keystruct);
+ keystruct->s[idx][idx2] = (block[0] << 24) | (block[1] << 16) |
+ (block[2] << 8) | block[3];
+ keystruct->s[idx][idx2+1] = (block[4] << 24) | (block[5] << 16) |
+ (block[6] << 8) | block[7];
+ }
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/blowfish.h b/test/monniaux/crypto-algorithms/blowfish.h
new file mode 100644
index 00000000..d8e9d4a6
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/blowfish.h
@@ -0,0 +1,32 @@
+/*********************************************************************
+* Filename: blowfish.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding Blowfish implementation.
+*********************************************************************/
+
+#ifndef BLOWFISH_H
+#define BLOWFISH_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define BLOWFISH_BLOCK_SIZE 8 // Blowfish operates on 8 bytes at a time
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+typedef struct {
+ WORD p[18];
+ WORD s[4][256];
+} BLOWFISH_KEY;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void blowfish_key_setup(const BYTE user_key[], BLOWFISH_KEY *keystruct, size_t len);
+void blowfish_encrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct);
+void blowfish_decrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct);
+
+#endif // BLOWFISH_H
diff --git a/test/monniaux/crypto-algorithms/blowfish_test.c b/test/monniaux/crypto-algorithms/blowfish_test.c
new file mode 100644
index 00000000..0f0aa387
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/blowfish_test.c
@@ -0,0 +1,68 @@
+/*********************************************************************
+* Filename: blowfish_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding Blowfish
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include "blowfish.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int blowfish_test()
+{
+ BYTE key1[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ BYTE key2[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
+ BYTE key3[24] = {0xF0,0xE1,0xD2,0xC3,0xB4,0xA5,0x96,0x87,
+ 0x78,0x69,0x5A,0x4B,0x3C,0x2D,0x1E,0x0F,
+ 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
+ BYTE p1[BLOWFISH_BLOCK_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ BYTE p2[BLOWFISH_BLOCK_SIZE] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
+ BYTE p3[BLOWFISH_BLOCK_SIZE] = {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10};
+
+ BYTE c1[BLOWFISH_BLOCK_SIZE] = {0x4e,0xf9,0x97,0x45,0x61,0x98,0xdd,0x78};
+ BYTE c2[BLOWFISH_BLOCK_SIZE] = {0x51,0x86,0x6f,0xd5,0xb8,0x5e,0xcb,0x8a};
+ BYTE c3[BLOWFISH_BLOCK_SIZE] = {0x05,0x04,0x4b,0x62,0xfa,0x52,0xd0,0x80};
+
+ BYTE enc_buf[BLOWFISH_BLOCK_SIZE];
+ BLOWFISH_KEY key;
+ int pass = 1;
+
+ // Test vector 1.
+ blowfish_key_setup(key1, &key, BLOWFISH_BLOCK_SIZE);
+ blowfish_encrypt(p1, enc_buf, &key);
+ pass = pass && !memcmp(c1, enc_buf, BLOWFISH_BLOCK_SIZE);
+ blowfish_decrypt(c1, enc_buf, &key);
+ pass = pass && !memcmp(p1, enc_buf, BLOWFISH_BLOCK_SIZE);
+
+ // Test vector 2.
+ blowfish_key_setup(key2, &key, BLOWFISH_BLOCK_SIZE);
+ blowfish_encrypt(p2, enc_buf, &key);
+ pass = pass && !memcmp(c2, enc_buf, BLOWFISH_BLOCK_SIZE);
+ blowfish_decrypt(c2, enc_buf, &key);
+ pass = pass && !memcmp(p2, enc_buf, BLOWFISH_BLOCK_SIZE);
+
+ // Test vector 3.
+ blowfish_key_setup(key3, &key, 24);
+ blowfish_encrypt(p3, enc_buf, &key);
+ pass = pass && !memcmp(c3, enc_buf, BLOWFISH_BLOCK_SIZE);
+ blowfish_decrypt(c3, enc_buf, &key);
+ pass = pass && !memcmp(p3, enc_buf, BLOWFISH_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("Blowfish tests: %s\n", blowfish_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/des.c b/test/monniaux/crypto-algorithms/des.c
new file mode 100644
index 00000000..d20db6f1
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/des.c
@@ -0,0 +1,269 @@
+/*********************************************************************
+* Filename: des.c
+* Author: Brad Conte (brad AT radconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the DES encryption algorithm.
+ Modes of operation (such as CBC) are not included.
+ The formal NIST algorithm specification can be found here:
+ * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "des.h"
+
+/****************************** MACROS ******************************/
+// Obtain bit "b" from the left and shift it "c" places from the right
+#define BITNUM(a,b,c) (((a[(b)/8] >> (7 - (b%8))) & 0x01) << (c))
+#define BITNUMINTR(a,b,c) ((((a) >> (31 - (b))) & 0x00000001) << (c))
+#define BITNUMINTL(a,b,c) ((((a) << (b)) & 0x80000000) >> (c))
+
+// This macro converts a 6 bit block with the S-Box row defined as the first and last
+// bits to a 6 bit block with the row defined by the first two bits.
+#define SBOXBIT(a) (((a) & 0x20) | (((a) & 0x1f) >> 1) | (((a) & 0x01) << 4))
+
+/**************************** VARIABLES *****************************/
+static const BYTE sbox1[64] = {
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
+ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
+ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
+ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
+};
+static const BYTE sbox2[64] = {
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
+ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
+ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
+ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
+};
+static const BYTE sbox3[64] = {
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
+ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
+ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
+ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
+};
+static const BYTE sbox4[64] = {
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
+ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
+ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
+ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
+};
+static const BYTE sbox5[64] = {
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
+ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
+ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
+ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
+};
+static const BYTE sbox6[64] = {
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
+ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
+ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
+ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
+};
+static const BYTE sbox7[64] = {
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
+ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
+ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
+ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
+};
+static const BYTE sbox8[64] = {
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
+ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
+ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
+ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
+};
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+// Initial (Inv)Permutation step
+void IP(WORD state[], const BYTE in[])
+{
+ state[0] = BITNUM(in,57,31) | BITNUM(in,49,30) | BITNUM(in,41,29) | BITNUM(in,33,28) |
+ BITNUM(in,25,27) | BITNUM(in,17,26) | BITNUM(in,9,25) | BITNUM(in,1,24) |
+ BITNUM(in,59,23) | BITNUM(in,51,22) | BITNUM(in,43,21) | BITNUM(in,35,20) |
+ BITNUM(in,27,19) | BITNUM(in,19,18) | BITNUM(in,11,17) | BITNUM(in,3,16) |
+ BITNUM(in,61,15) | BITNUM(in,53,14) | BITNUM(in,45,13) | BITNUM(in,37,12) |
+ BITNUM(in,29,11) | BITNUM(in,21,10) | BITNUM(in,13,9) | BITNUM(in,5,8) |
+ BITNUM(in,63,7) | BITNUM(in,55,6) | BITNUM(in,47,5) | BITNUM(in,39,4) |
+ BITNUM(in,31,3) | BITNUM(in,23,2) | BITNUM(in,15,1) | BITNUM(in,7,0);
+
+ state[1] = BITNUM(in,56,31) | BITNUM(in,48,30) | BITNUM(in,40,29) | BITNUM(in,32,28) |
+ BITNUM(in,24,27) | BITNUM(in,16,26) | BITNUM(in,8,25) | BITNUM(in,0,24) |
+ BITNUM(in,58,23) | BITNUM(in,50,22) | BITNUM(in,42,21) | BITNUM(in,34,20) |
+ BITNUM(in,26,19) | BITNUM(in,18,18) | BITNUM(in,10,17) | BITNUM(in,2,16) |
+ BITNUM(in,60,15) | BITNUM(in,52,14) | BITNUM(in,44,13) | BITNUM(in,36,12) |
+ BITNUM(in,28,11) | BITNUM(in,20,10) | BITNUM(in,12,9) | BITNUM(in,4,8) |
+ BITNUM(in,62,7) | BITNUM(in,54,6) | BITNUM(in,46,5) | BITNUM(in,38,4) |
+ BITNUM(in,30,3) | BITNUM(in,22,2) | BITNUM(in,14,1) | BITNUM(in,6,0);
+}
+
+void InvIP(WORD state[], BYTE in[])
+{
+ in[0] = BITNUMINTR(state[1],7,7) | BITNUMINTR(state[0],7,6) | BITNUMINTR(state[1],15,5) |
+ BITNUMINTR(state[0],15,4) | BITNUMINTR(state[1],23,3) | BITNUMINTR(state[0],23,2) |
+ BITNUMINTR(state[1],31,1) | BITNUMINTR(state[0],31,0);
+
+ in[1] = BITNUMINTR(state[1],6,7) | BITNUMINTR(state[0],6,6) | BITNUMINTR(state[1],14,5) |
+ BITNUMINTR(state[0],14,4) | BITNUMINTR(state[1],22,3) | BITNUMINTR(state[0],22,2) |
+ BITNUMINTR(state[1],30,1) | BITNUMINTR(state[0],30,0);
+
+ in[2] = BITNUMINTR(state[1],5,7) | BITNUMINTR(state[0],5,6) | BITNUMINTR(state[1],13,5) |
+ BITNUMINTR(state[0],13,4) | BITNUMINTR(state[1],21,3) | BITNUMINTR(state[0],21,2) |
+ BITNUMINTR(state[1],29,1) | BITNUMINTR(state[0],29,0);
+
+ in[3] = BITNUMINTR(state[1],4,7) | BITNUMINTR(state[0],4,6) | BITNUMINTR(state[1],12,5) |
+ BITNUMINTR(state[0],12,4) | BITNUMINTR(state[1],20,3) | BITNUMINTR(state[0],20,2) |
+ BITNUMINTR(state[1],28,1) | BITNUMINTR(state[0],28,0);
+
+ in[4] = BITNUMINTR(state[1],3,7) | BITNUMINTR(state[0],3,6) | BITNUMINTR(state[1],11,5) |
+ BITNUMINTR(state[0],11,4) | BITNUMINTR(state[1],19,3) | BITNUMINTR(state[0],19,2) |
+ BITNUMINTR(state[1],27,1) | BITNUMINTR(state[0],27,0);
+
+ in[5] = BITNUMINTR(state[1],2,7) | BITNUMINTR(state[0],2,6) | BITNUMINTR(state[1],10,5) |
+ BITNUMINTR(state[0],10,4) | BITNUMINTR(state[1],18,3) | BITNUMINTR(state[0],18,2) |
+ BITNUMINTR(state[1],26,1) | BITNUMINTR(state[0],26,0);
+
+ in[6] = BITNUMINTR(state[1],1,7) | BITNUMINTR(state[0],1,6) | BITNUMINTR(state[1],9,5) |
+ BITNUMINTR(state[0],9,4) | BITNUMINTR(state[1],17,3) | BITNUMINTR(state[0],17,2) |
+ BITNUMINTR(state[1],25,1) | BITNUMINTR(state[0],25,0);
+
+ in[7] = BITNUMINTR(state[1],0,7) | BITNUMINTR(state[0],0,6) | BITNUMINTR(state[1],8,5) |
+ BITNUMINTR(state[0],8,4) | BITNUMINTR(state[1],16,3) | BITNUMINTR(state[0],16,2) |
+ BITNUMINTR(state[1],24,1) | BITNUMINTR(state[0],24,0);
+}
+
+WORD f(WORD state, const BYTE key[])
+{
+ BYTE lrgstate[6]; //,i;
+ WORD t1,t2;
+
+ // Expantion Permutation
+ t1 = BITNUMINTL(state,31,0) | ((state & 0xf0000000) >> 1) | BITNUMINTL(state,4,5) |
+ BITNUMINTL(state,3,6) | ((state & 0x0f000000) >> 3) | BITNUMINTL(state,8,11) |
+ BITNUMINTL(state,7,12) | ((state & 0x00f00000) >> 5) | BITNUMINTL(state,12,17) |
+ BITNUMINTL(state,11,18) | ((state & 0x000f0000) >> 7) | BITNUMINTL(state,16,23);
+
+ t2 = BITNUMINTL(state,15,0) | ((state & 0x0000f000) << 15) | BITNUMINTL(state,20,5) |
+ BITNUMINTL(state,19,6) | ((state & 0x00000f00) << 13) | BITNUMINTL(state,24,11) |
+ BITNUMINTL(state,23,12) | ((state & 0x000000f0) << 11) | BITNUMINTL(state,28,17) |
+ BITNUMINTL(state,27,18) | ((state & 0x0000000f) << 9) | BITNUMINTL(state,0,23);
+
+ lrgstate[0] = (t1 >> 24) & 0x000000ff;
+ lrgstate[1] = (t1 >> 16) & 0x000000ff;
+ lrgstate[2] = (t1 >> 8) & 0x000000ff;
+ lrgstate[3] = (t2 >> 24) & 0x000000ff;
+ lrgstate[4] = (t2 >> 16) & 0x000000ff;
+ lrgstate[5] = (t2 >> 8) & 0x000000ff;
+
+ // Key XOR
+ lrgstate[0] ^= key[0];
+ lrgstate[1] ^= key[1];
+ lrgstate[2] ^= key[2];
+ lrgstate[3] ^= key[3];
+ lrgstate[4] ^= key[4];
+ lrgstate[5] ^= key[5];
+
+ // S-Box Permutation
+ state = (sbox1[SBOXBIT(lrgstate[0] >> 2)] << 28) |
+ (sbox2[SBOXBIT(((lrgstate[0] & 0x03) << 4) | (lrgstate[1] >> 4))] << 24) |
+ (sbox3[SBOXBIT(((lrgstate[1] & 0x0f) << 2) | (lrgstate[2] >> 6))] << 20) |
+ (sbox4[SBOXBIT(lrgstate[2] & 0x3f)] << 16) |
+ (sbox5[SBOXBIT(lrgstate[3] >> 2)] << 12) |
+ (sbox6[SBOXBIT(((lrgstate[3] & 0x03) << 4) | (lrgstate[4] >> 4))] << 8) |
+ (sbox7[SBOXBIT(((lrgstate[4] & 0x0f) << 2) | (lrgstate[5] >> 6))] << 4) |
+ sbox8[SBOXBIT(lrgstate[5] & 0x3f)];
+
+ // P-Box Permutation
+ state = BITNUMINTL(state,15,0) | BITNUMINTL(state,6,1) | BITNUMINTL(state,19,2) |
+ BITNUMINTL(state,20,3) | BITNUMINTL(state,28,4) | BITNUMINTL(state,11,5) |
+ BITNUMINTL(state,27,6) | BITNUMINTL(state,16,7) | BITNUMINTL(state,0,8) |
+ BITNUMINTL(state,14,9) | BITNUMINTL(state,22,10) | BITNUMINTL(state,25,11) |
+ BITNUMINTL(state,4,12) | BITNUMINTL(state,17,13) | BITNUMINTL(state,30,14) |
+ BITNUMINTL(state,9,15) | BITNUMINTL(state,1,16) | BITNUMINTL(state,7,17) |
+ BITNUMINTL(state,23,18) | BITNUMINTL(state,13,19) | BITNUMINTL(state,31,20) |
+ BITNUMINTL(state,26,21) | BITNUMINTL(state,2,22) | BITNUMINTL(state,8,23) |
+ BITNUMINTL(state,18,24) | BITNUMINTL(state,12,25) | BITNUMINTL(state,29,26) |
+ BITNUMINTL(state,5,27) | BITNUMINTL(state,21,28) | BITNUMINTL(state,10,29) |
+ BITNUMINTL(state,3,30) | BITNUMINTL(state,24,31);
+
+ // Return the final state value
+ return(state);
+}
+
+void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode)
+{
+ WORD i, j, to_gen, C, D;
+ const WORD key_rnd_shift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
+ const WORD key_perm_c[28] = {56,48,40,32,24,16,8,0,57,49,41,33,25,17,
+ 9,1,58,50,42,34,26,18,10,2,59,51,43,35};
+ const WORD key_perm_d[28] = {62,54,46,38,30,22,14,6,61,53,45,37,29,21,
+ 13,5,60,52,44,36,28,20,12,4,27,19,11,3};
+ const WORD key_compression[48] = {13,16,10,23,0,4,2,27,14,5,20,9,
+ 22,18,11,3,25,7,15,6,26,19,12,1,
+ 40,51,30,36,46,54,29,39,50,44,32,47,
+ 43,48,38,55,33,52,45,41,49,35,28,31};
+
+ // Permutated Choice #1 (copy the key in, ignoring parity bits).
+ for (i = 0, j = 31, C = 0; i < 28; ++i, --j)
+ C |= BITNUM(key,key_perm_c[i],j);
+ for (i = 0, j = 31, D = 0; i < 28; ++i, --j)
+ D |= BITNUM(key,key_perm_d[i],j);
+
+ // Generate the 16 subkeys.
+ for (i = 0; i < 16; ++i) {
+ C = ((C << key_rnd_shift[i]) | (C >> (28-key_rnd_shift[i]))) & 0xfffffff0;
+ D = ((D << key_rnd_shift[i]) | (D >> (28-key_rnd_shift[i]))) & 0xfffffff0;
+
+ // Decryption subkeys are reverse order of encryption subkeys so
+ // generate them in reverse if the key schedule is for decryption useage.
+ if (mode == DES_DECRYPT)
+ to_gen = 15 - i;
+ else /*(if mode == DES_ENCRYPT)*/
+ to_gen = i;
+ // Initialize the array
+ for (j = 0; j < 6; ++j)
+ schedule[to_gen][j] = 0;
+ for (j = 0; j < 24; ++j)
+ schedule[to_gen][j/8] |= BITNUMINTR(C,key_compression[j],7 - (j%8));
+ for ( ; j < 48; ++j)
+ schedule[to_gen][j/8] |= BITNUMINTR(D,key_compression[j] - 28,7 - (j%8));
+ }
+}
+
+void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6])
+{
+ WORD state[2],idx,t;
+
+ IP(state,in);
+
+ for (idx=0; idx < 15; ++idx) {
+ t = state[1];
+ state[1] = f(state[1],key[idx]) ^ state[0];
+ state[0] = t;
+ }
+ // Perform the final loop manually as it doesn't switch sides
+ state[0] = f(state[1],key[15]) ^ state[0];
+
+ InvIP(state,out);
+}
+
+void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode)
+{
+ if (mode == DES_ENCRYPT) {
+ des_key_setup(&key[0],schedule[0],mode);
+ des_key_setup(&key[8],schedule[1],!mode);
+ des_key_setup(&key[16],schedule[2],mode);
+ }
+ else /*if (mode == DES_DECRYPT*/ {
+ des_key_setup(&key[16],schedule[0],mode);
+ des_key_setup(&key[8],schedule[1],!mode);
+ des_key_setup(&key[0],schedule[2],mode);
+ }
+}
+
+void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6])
+{
+ des_crypt(in,out,key[0]);
+ des_crypt(out,out,key[1]);
+ des_crypt(out,out,key[2]);
+}
diff --git a/test/monniaux/crypto-algorithms/des.h b/test/monniaux/crypto-algorithms/des.h
new file mode 100644
index 00000000..1503772a
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/des.h
@@ -0,0 +1,37 @@
+/*********************************************************************
+* Filename: des.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding DES implementation.
+ Note that encryption and decryption are defined by how
+ the key setup is performed, the actual en/de-cryption is
+ performed by the same function.
+*********************************************************************/
+
+#ifndef DES_H
+#define DESH
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define DES_BLOCK_SIZE 8 // DES operates on 8 bytes at a time
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+typedef enum {
+ DES_ENCRYPT,
+ DES_DECRYPT
+} DES_MODE;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode);
+void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6]);
+
+void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode);
+void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6]);
+
+#endif // DES_H
diff --git a/test/monniaux/crypto-algorithms/des_test.c b/test/monniaux/crypto-algorithms/des_test.c
new file mode 100644
index 00000000..3e46134f
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/des_test.c
@@ -0,0 +1,83 @@
+/*********************************************************************
+* Filename: des_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding DES
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include "des.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int des_test()
+{
+ BYTE pt1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xE7};
+ BYTE pt2[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
+ BYTE pt3[DES_BLOCK_SIZE] = {0x54,0x68,0x65,0x20,0x71,0x75,0x66,0x63};
+ BYTE ct1[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
+ BYTE ct2[DES_BLOCK_SIZE] = {0x85,0xe8,0x13,0x54,0x0f,0x0a,0xb4,0x05};
+ BYTE ct3[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
+ BYTE ct4[DES_BLOCK_SIZE] = {0xA8,0x26,0xFD,0x8C,0xE5,0x3B,0x85,0x5F};
+ BYTE key1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
+ BYTE key2[DES_BLOCK_SIZE] = {0x13,0x34,0x57,0x79,0x9B,0xBC,0xDF,0xF1};
+ BYTE three_key1[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
+ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
+ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
+ BYTE three_key2[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
+ 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,
+ 0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23};
+
+ BYTE schedule[16][6];
+ BYTE three_schedule[3][16][6];
+ BYTE buf[DES_BLOCK_SIZE];
+ int pass = 1;
+
+ des_key_setup(key1, schedule, DES_ENCRYPT);
+ des_crypt(pt1, buf, schedule);
+ pass = pass && !memcmp(ct1, buf, DES_BLOCK_SIZE);
+
+ des_key_setup(key1, schedule, DES_DECRYPT);
+ des_crypt(ct1, buf, schedule);
+ pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
+
+ des_key_setup(key2, schedule, DES_ENCRYPT);
+ des_crypt(pt2, buf, schedule);
+ pass = pass && !memcmp(ct2, buf, DES_BLOCK_SIZE);
+
+ des_key_setup(key2, schedule, DES_DECRYPT);
+ des_crypt(ct2, buf, schedule);
+ pass = pass && !memcmp(pt2, buf, DES_BLOCK_SIZE);
+
+ three_des_key_setup(three_key1, three_schedule, DES_ENCRYPT);
+ three_des_crypt(pt1, buf, three_schedule);
+ pass = pass && !memcmp(ct3, buf, DES_BLOCK_SIZE);
+
+ three_des_key_setup(three_key1, three_schedule, DES_DECRYPT);
+ three_des_crypt(ct3, buf, three_schedule);
+ pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
+
+ three_des_key_setup(three_key2, three_schedule, DES_ENCRYPT);
+ three_des_crypt(pt3, buf, three_schedule);
+ pass = pass && !memcmp(ct4, buf, DES_BLOCK_SIZE);
+
+ three_des_key_setup(three_key2, three_schedule, DES_DECRYPT);
+ three_des_crypt(ct4, buf, three_schedule);
+ pass = pass && !memcmp(pt3, buf, DES_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("DES test: %s\n", des_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/md2.c b/test/monniaux/crypto-algorithms/md2.c
new file mode 100644
index 00000000..de6bb6b9
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md2.c
@@ -0,0 +1,104 @@
+/*********************************************************************
+* Filename: md2.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the MD2 hashing algorithm.
+ Algorithm specification can be found here:
+ * http://tools.ietf.org/html/rfc1319 .
+ Input is little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "md2.h"
+
+/**************************** VARIABLES *****************************/
+static const BYTE s[256] = {
+ 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
+ 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
+ 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
+ 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
+ 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
+ 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
+ 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
+ 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
+ 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
+ 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
+ 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
+ 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
+ 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
+ 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
+ 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
+ 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
+ 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
+ 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
+};
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void md2_transform(MD2_CTX *ctx, BYTE data[])
+{
+ int j,k,t;
+
+ //memcpy(&ctx->state[16], data);
+ for (j=0; j < 16; ++j) {
+ ctx->state[j + 16] = data[j];
+ ctx->state[j + 32] = (ctx->state[j+16] ^ ctx->state[j]);
+ }
+
+ t = 0;
+ for (j = 0; j < 18; ++j) {
+ for (k = 0; k < 48; ++k) {
+ ctx->state[k] ^= s[t];
+ t = ctx->state[k];
+ }
+ t = (t+j) & 0xFF;
+ }
+
+ t = ctx->checksum[15];
+ for (j=0; j < 16; ++j) {
+ ctx->checksum[j] ^= s[data[j] ^ t];
+ t = ctx->checksum[j];
+ }
+}
+
+void md2_init(MD2_CTX *ctx)
+{
+ int i;
+
+ for (i=0; i < 48; ++i)
+ ctx->state[i] = 0;
+ for (i=0; i < 16; ++i)
+ ctx->checksum[i] = 0;
+ ctx->len = 0;
+}
+
+void md2_update(MD2_CTX *ctx, const BYTE data[], size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; ++i) {
+ ctx->data[ctx->len] = data[i];
+ ctx->len++;
+ if (ctx->len == MD2_BLOCK_SIZE) {
+ md2_transform(ctx, ctx->data);
+ ctx->len = 0;
+ }
+ }
+}
+
+void md2_final(MD2_CTX *ctx, BYTE hash[])
+{
+ int to_pad;
+
+ to_pad = MD2_BLOCK_SIZE - ctx->len;
+
+ while (ctx->len < MD2_BLOCK_SIZE)
+ ctx->data[ctx->len++] = to_pad;
+
+ md2_transform(ctx, ctx->data);
+ md2_transform(ctx, ctx->checksum);
+
+ memcpy(hash, ctx->state, MD2_BLOCK_SIZE);
+}
diff --git a/test/monniaux/crypto-algorithms/md2.h b/test/monniaux/crypto-algorithms/md2.h
new file mode 100644
index 00000000..97706af1
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md2.h
@@ -0,0 +1,33 @@
+/*********************************************************************
+* Filename: md2.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding MD2 implementation.
+*********************************************************************/
+
+#ifndef MD2_H
+#define MD2_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define MD2_BLOCK_SIZE 16
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+
+typedef struct {
+ BYTE data[16];
+ BYTE state[48];
+ BYTE checksum[16];
+ int len;
+} MD2_CTX;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void md2_init(MD2_CTX *ctx);
+void md2_update(MD2_CTX *ctx, const BYTE data[], size_t len);
+void md2_final(MD2_CTX *ctx, BYTE hash[]); // size of hash must be MD2_BLOCK_SIZE
+
+#endif // MD2_H
diff --git a/test/monniaux/crypto-algorithms/md2_test.c b/test/monniaux/crypto-algorithms/md2_test.c
new file mode 100644
index 00000000..883f20a2
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md2_test.c
@@ -0,0 +1,58 @@
+/*********************************************************************
+* Filename: md2_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding MD2
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <string.h>
+#include <memory.h>
+#include "md2.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int md2_test()
+{
+ BYTE text1[] = {"abc"};
+ BYTE text2[] = {"abcdefghijklmnopqrstuvwxyz"};
+ BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"};
+ BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"};
+ BYTE hash1[MD2_BLOCK_SIZE] = {0xda,0x85,0x3b,0x0d,0x3f,0x88,0xd9,0x9b,0x30,0x28,0x3a,0x69,0xe6,0xde,0xd6,0xbb};
+ BYTE hash2[MD2_BLOCK_SIZE] = {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab,0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b};
+ BYTE hash3[MD2_BLOCK_SIZE] = {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39,0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd};
+ BYTE buf[16];
+ MD2_CTX ctx;
+ int pass = 1;
+
+ md2_init(&ctx);
+ md2_update(&ctx, text1, strlen(text1));
+ md2_final(&ctx, buf);
+ pass = pass && !memcmp(hash1, buf, MD2_BLOCK_SIZE);
+
+ // Note that the MD2 object can be re-used.
+ md2_init(&ctx);
+ md2_update(&ctx, text2, strlen(text2));
+ md2_final(&ctx, buf);
+ pass = pass && !memcmp(hash2, buf, MD2_BLOCK_SIZE);
+
+ // Note that the data is added in two chunks.
+ md2_init(&ctx);
+ md2_update(&ctx, text3_1, strlen(text3_1));
+ md2_update(&ctx, text3_2, strlen(text3_2));
+ md2_final(&ctx, buf);
+ pass = pass && !memcmp(hash3, buf, MD2_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("MD2 tests: %s\n", md2_test() ? "SUCCEEDED" : "FAILED");
+}
diff --git a/test/monniaux/crypto-algorithms/md5.c b/test/monniaux/crypto-algorithms/md5.c
new file mode 100644
index 00000000..cdba052e
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md5.c
@@ -0,0 +1,189 @@
+/*********************************************************************
+* Filename: md5.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the MD5 hashing algorithm.
+ Algorithm specification can be found here:
+ * http://tools.ietf.org/html/rfc1321
+ This implementation uses little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "md5.h"
+
+/****************************** MACROS ******************************/
+#define ROTLEFT(a,b) ((a << b) | (a >> (32-b)))
+
+#define F(x,y,z) ((x & y) | (~x & z))
+#define G(x,y,z) ((x & z) | (y & ~z))
+#define H(x,y,z) (x ^ y ^ z)
+#define I(x,y,z) (y ^ (x | ~z))
+
+#define FF(a,b,c,d,m,s,t) { a += F(b,c,d) + m + t; \
+ a = b + ROTLEFT(a,s); }
+#define GG(a,b,c,d,m,s,t) { a += G(b,c,d) + m + t; \
+ a = b + ROTLEFT(a,s); }
+#define HH(a,b,c,d,m,s,t) { a += H(b,c,d) + m + t; \
+ a = b + ROTLEFT(a,s); }
+#define II(a,b,c,d,m,s,t) { a += I(b,c,d) + m + t; \
+ a = b + ROTLEFT(a,s); }
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void md5_transform(MD5_CTX *ctx, const BYTE data[])
+{
+ WORD a, b, c, d, m[16], i, j;
+
+ // MD5 specifies big endian byte order, but this implementation assumes a little
+ // endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
+ // on output (in md5_final()).
+ for (i = 0, j = 0; i < 16; ++i, j += 4)
+ m[i] = (data[j]) + (data[j + 1] << 8) + (data[j + 2] << 16) + (data[j + 3] << 24);
+
+ a = ctx->state[0];
+ b = ctx->state[1];
+ c = ctx->state[2];
+ d = ctx->state[3];
+
+ FF(a,b,c,d,m[0], 7,0xd76aa478);
+ FF(d,a,b,c,m[1], 12,0xe8c7b756);
+ FF(c,d,a,b,m[2], 17,0x242070db);
+ FF(b,c,d,a,m[3], 22,0xc1bdceee);
+ FF(a,b,c,d,m[4], 7,0xf57c0faf);
+ FF(d,a,b,c,m[5], 12,0x4787c62a);
+ FF(c,d,a,b,m[6], 17,0xa8304613);
+ FF(b,c,d,a,m[7], 22,0xfd469501);
+ FF(a,b,c,d,m[8], 7,0x698098d8);
+ FF(d,a,b,c,m[9], 12,0x8b44f7af);
+ FF(c,d,a,b,m[10],17,0xffff5bb1);
+ FF(b,c,d,a,m[11],22,0x895cd7be);
+ FF(a,b,c,d,m[12], 7,0x6b901122);
+ FF(d,a,b,c,m[13],12,0xfd987193);
+ FF(c,d,a,b,m[14],17,0xa679438e);
+ FF(b,c,d,a,m[15],22,0x49b40821);
+
+ GG(a,b,c,d,m[1], 5,0xf61e2562);
+ GG(d,a,b,c,m[6], 9,0xc040b340);
+ GG(c,d,a,b,m[11],14,0x265e5a51);
+ GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
+ GG(a,b,c,d,m[5], 5,0xd62f105d);
+ GG(d,a,b,c,m[10], 9,0x02441453);
+ GG(c,d,a,b,m[15],14,0xd8a1e681);
+ GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
+ GG(a,b,c,d,m[9], 5,0x21e1cde6);
+ GG(d,a,b,c,m[14], 9,0xc33707d6);
+ GG(c,d,a,b,m[3], 14,0xf4d50d87);
+ GG(b,c,d,a,m[8], 20,0x455a14ed);
+ GG(a,b,c,d,m[13], 5,0xa9e3e905);
+ GG(d,a,b,c,m[2], 9,0xfcefa3f8);
+ GG(c,d,a,b,m[7], 14,0x676f02d9);
+ GG(b,c,d,a,m[12],20,0x8d2a4c8a);
+
+ HH(a,b,c,d,m[5], 4,0xfffa3942);
+ HH(d,a,b,c,m[8], 11,0x8771f681);
+ HH(c,d,a,b,m[11],16,0x6d9d6122);
+ HH(b,c,d,a,m[14],23,0xfde5380c);
+ HH(a,b,c,d,m[1], 4,0xa4beea44);
+ HH(d,a,b,c,m[4], 11,0x4bdecfa9);
+ HH(c,d,a,b,m[7], 16,0xf6bb4b60);
+ HH(b,c,d,a,m[10],23,0xbebfbc70);
+ HH(a,b,c,d,m[13], 4,0x289b7ec6);
+ HH(d,a,b,c,m[0], 11,0xeaa127fa);
+ HH(c,d,a,b,m[3], 16,0xd4ef3085);
+ HH(b,c,d,a,m[6], 23,0x04881d05);
+ HH(a,b,c,d,m[9], 4,0xd9d4d039);
+ HH(d,a,b,c,m[12],11,0xe6db99e5);
+ HH(c,d,a,b,m[15],16,0x1fa27cf8);
+ HH(b,c,d,a,m[2], 23,0xc4ac5665);
+
+ II(a,b,c,d,m[0], 6,0xf4292244);
+ II(d,a,b,c,m[7], 10,0x432aff97);
+ II(c,d,a,b,m[14],15,0xab9423a7);
+ II(b,c,d,a,m[5], 21,0xfc93a039);
+ II(a,b,c,d,m[12], 6,0x655b59c3);
+ II(d,a,b,c,m[3], 10,0x8f0ccc92);
+ II(c,d,a,b,m[10],15,0xffeff47d);
+ II(b,c,d,a,m[1], 21,0x85845dd1);
+ II(a,b,c,d,m[8], 6,0x6fa87e4f);
+ II(d,a,b,c,m[15],10,0xfe2ce6e0);
+ II(c,d,a,b,m[6], 15,0xa3014314);
+ II(b,c,d,a,m[13],21,0x4e0811a1);
+ II(a,b,c,d,m[4], 6,0xf7537e82);
+ II(d,a,b,c,m[11],10,0xbd3af235);
+ II(c,d,a,b,m[2], 15,0x2ad7d2bb);
+ II(b,c,d,a,m[9], 21,0xeb86d391);
+
+ ctx->state[0] += a;
+ ctx->state[1] += b;
+ ctx->state[2] += c;
+ ctx->state[3] += d;
+}
+
+void md5_init(MD5_CTX *ctx)
+{
+ ctx->datalen = 0;
+ ctx->bitlen = 0;
+ ctx->state[0] = 0x67452301;
+ ctx->state[1] = 0xEFCDAB89;
+ ctx->state[2] = 0x98BADCFE;
+ ctx->state[3] = 0x10325476;
+}
+
+void md5_update(MD5_CTX *ctx, const BYTE data[], size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; ++i) {
+ ctx->data[ctx->datalen] = data[i];
+ ctx->datalen++;
+ if (ctx->datalen == 64) {
+ md5_transform(ctx, ctx->data);
+ ctx->bitlen += 512;
+ ctx->datalen = 0;
+ }
+ }
+}
+
+void md5_final(MD5_CTX *ctx, BYTE hash[])
+{
+ size_t i;
+
+ i = ctx->datalen;
+
+ // Pad whatever data is left in the buffer.
+ if (ctx->datalen < 56) {
+ ctx->data[i++] = 0x80;
+ while (i < 56)
+ ctx->data[i++] = 0x00;
+ }
+ else if (ctx->datalen >= 56) {
+ ctx->data[i++] = 0x80;
+ while (i < 64)
+ ctx->data[i++] = 0x00;
+ md5_transform(ctx, ctx->data);
+ memset(ctx->data, 0, 56);
+ }
+
+ // Append to the padding the total message's length in bits and transform.
+ ctx->bitlen += ctx->datalen * 8;
+ ctx->data[56] = ctx->bitlen;
+ ctx->data[57] = ctx->bitlen >> 8;
+ ctx->data[58] = ctx->bitlen >> 16;
+ ctx->data[59] = ctx->bitlen >> 24;
+ ctx->data[60] = ctx->bitlen >> 32;
+ ctx->data[61] = ctx->bitlen >> 40;
+ ctx->data[62] = ctx->bitlen >> 48;
+ ctx->data[63] = ctx->bitlen >> 56;
+ md5_transform(ctx, ctx->data);
+
+ // Since this implementation uses little endian byte ordering and MD uses big endian,
+ // reverse all the bytes when copying the final state to the output hash.
+ for (i = 0; i < 4; ++i) {
+ hash[i] = (ctx->state[0] >> (i * 8)) & 0x000000ff;
+ hash[i + 4] = (ctx->state[1] >> (i * 8)) & 0x000000ff;
+ hash[i + 8] = (ctx->state[2] >> (i * 8)) & 0x000000ff;
+ hash[i + 12] = (ctx->state[3] >> (i * 8)) & 0x000000ff;
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/md5.h b/test/monniaux/crypto-algorithms/md5.h
new file mode 100644
index 00000000..1370387c
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md5.h
@@ -0,0 +1,34 @@
+/*********************************************************************
+* Filename: md5.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding MD5 implementation.
+*********************************************************************/
+
+#ifndef MD5_H
+#define MD5_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+typedef struct {
+ BYTE data[64];
+ WORD datalen;
+ unsigned long long bitlen;
+ WORD state[4];
+} MD5_CTX;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void md5_init(MD5_CTX *ctx);
+void md5_update(MD5_CTX *ctx, const BYTE data[], size_t len);
+void md5_final(MD5_CTX *ctx, BYTE hash[]);
+
+#endif // MD5_H
diff --git a/test/monniaux/crypto-algorithms/md5_test.c b/test/monniaux/crypto-algorithms/md5_test.c
new file mode 100644
index 00000000..e945c8b2
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/md5_test.c
@@ -0,0 +1,60 @@
+/*********************************************************************
+* Filename: md5_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding MD5
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include <string.h>
+#include "md5.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int md5_test()
+{
+ BYTE text1[] = {""};
+ BYTE text2[] = {"abc"};
+ BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"};
+ BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"};
+ BYTE hash1[MD5_BLOCK_SIZE] = {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e};
+ BYTE hash2[MD5_BLOCK_SIZE] = {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72};
+ BYTE hash3[MD5_BLOCK_SIZE] = {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f};
+ BYTE buf[16];
+ MD5_CTX ctx;
+ int pass = 1;
+
+ md5_init(&ctx);
+ md5_update(&ctx, text1, strlen(text1));
+ md5_final(&ctx, buf);
+ pass = pass && !memcmp(hash1, buf, MD5_BLOCK_SIZE);
+
+ // Note the MD5 object can be reused.
+ md5_init(&ctx);
+ md5_update(&ctx, text2, strlen(text2));
+ md5_final(&ctx, buf);
+ pass = pass && !memcmp(hash2, buf, MD5_BLOCK_SIZE);
+
+ // Note the data is being added in two chunks.
+ md5_init(&ctx);
+ md5_update(&ctx, text3_1, strlen(text3_1));
+ md5_update(&ctx, text3_2, strlen(text3_2));
+ md5_final(&ctx, buf);
+ pass = pass && !memcmp(hash3, buf, MD5_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("MD5 tests: %s\n", md5_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/rot-13.c b/test/monniaux/crypto-algorithms/rot-13.c
new file mode 100644
index 00000000..0ab84975
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/rot-13.c
@@ -0,0 +1,35 @@
+/*********************************************************************
+* Filename: rot-13.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the ROT-13 encryption algorithm.
+ Algorithm specification can be found here:
+ *
+ This implementation uses little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <string.h>
+#include "rot-13.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void rot13(char str[])
+{
+ int case_type, idx, len;
+
+ for (idx = 0, len = strlen(str); idx < len; idx++) {
+ // Only process alphabetic characters.
+ if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z')
+ continue;
+ // Determine if the char is upper or lower case.
+ if (str[idx] >= 'a')
+ case_type = 'a';
+ else
+ case_type = 'A';
+ // Rotate the char's value, ensuring it doesn't accidentally "fall off" the end.
+ str[idx] = (str[idx] + 13) % (case_type + 26);
+ if (str[idx] < 26)
+ str[idx] += case_type;
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/rot-13.h b/test/monniaux/crypto-algorithms/rot-13.h
new file mode 100644
index 00000000..4c581c39
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/rot-13.h
@@ -0,0 +1,20 @@
+/*********************************************************************
+* Filename: rot-13.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding ROT-13 implementation.
+*********************************************************************/
+
+#ifndef ROT13_H
+#define ROT13_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/*********************** FUNCTION DECLARATIONS **********************/
+// Performs IN PLACE rotation of the input. Assumes input is NULL terminated.
+// Preserves each charcter's case. Ignores non alphabetic characters.
+void rot13(char str[]);
+
+#endif // ROT13_H
diff --git a/test/monniaux/crypto-algorithms/rot-13_test.c b/test/monniaux/crypto-algorithms/rot-13_test.c
new file mode 100644
index 00000000..a6fd01d7
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/rot-13_test.c
@@ -0,0 +1,44 @@
+/*********************************************************************
+* Filename: rot-13_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding ROT-13
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <string.h>
+#include "rot-13.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int rot13_test()
+{
+ char text[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
+ char code[] = {"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"};
+ char buf[1024];
+ int pass = 1;
+
+ // To encode, just apply ROT-13.
+ strcpy(buf, text);
+ rot13(buf);
+ pass = pass && !strcmp(code, buf);
+
+ // To decode, just re-apply ROT-13.
+ rot13(buf);
+ pass = pass && !strcmp(text, buf);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("ROT-13 tests: %s\n", rot13_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/sha1.c b/test/monniaux/crypto-algorithms/sha1.c
new file mode 100644
index 00000000..2f9622d4
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha1.c
@@ -0,0 +1,149 @@
+/*********************************************************************
+* Filename: sha1.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the SHA1 hashing algorithm.
+ Algorithm specification can be found here:
+ * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
+ This implementation uses little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "sha1.h"
+
+/****************************** MACROS ******************************/
+#define ROTLEFT(a, b) ((a << b) | (a >> (32 - b)))
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void sha1_transform(SHA1_CTX *ctx, const BYTE data[])
+{
+ WORD a, b, c, d, e, i, j, t, m[80];
+
+ for (i = 0, j = 0; i < 16; ++i, j += 4)
+ m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8) + (data[j + 3]);
+ for ( ; i < 80; ++i) {
+ m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]);
+ m[i] = (m[i] << 1) | (m[i] >> 31);
+ }
+
+ a = ctx->state[0];
+ b = ctx->state[1];
+ c = ctx->state[2];
+ d = ctx->state[3];
+ e = ctx->state[4];
+
+ for (i = 0; i < 20; ++i) {
+ t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i];
+ e = d;
+ d = c;
+ c = ROTLEFT(b, 30);
+ b = a;
+ a = t;
+ }
+ for ( ; i < 40; ++i) {
+ t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i];
+ e = d;
+ d = c;
+ c = ROTLEFT(b, 30);
+ b = a;
+ a = t;
+ }
+ for ( ; i < 60; ++i) {
+ t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i];
+ e = d;
+ d = c;
+ c = ROTLEFT(b, 30);
+ b = a;
+ a = t;
+ }
+ for ( ; i < 80; ++i) {
+ t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i];
+ e = d;
+ d = c;
+ c = ROTLEFT(b, 30);
+ b = a;
+ a = t;
+ }
+
+ ctx->state[0] += a;
+ ctx->state[1] += b;
+ ctx->state[2] += c;
+ ctx->state[3] += d;
+ ctx->state[4] += e;
+}
+
+void sha1_init(SHA1_CTX *ctx)
+{
+ ctx->datalen = 0;
+ ctx->bitlen = 0;
+ ctx->state[0] = 0x67452301;
+ ctx->state[1] = 0xEFCDAB89;
+ ctx->state[2] = 0x98BADCFE;
+ ctx->state[3] = 0x10325476;
+ ctx->state[4] = 0xc3d2e1f0;
+ ctx->k[0] = 0x5a827999;
+ ctx->k[1] = 0x6ed9eba1;
+ ctx->k[2] = 0x8f1bbcdc;
+ ctx->k[3] = 0xca62c1d6;
+}
+
+void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; ++i) {
+ ctx->data[ctx->datalen] = data[i];
+ ctx->datalen++;
+ if (ctx->datalen == 64) {
+ sha1_transform(ctx, ctx->data);
+ ctx->bitlen += 512;
+ ctx->datalen = 0;
+ }
+ }
+}
+
+void sha1_final(SHA1_CTX *ctx, BYTE hash[])
+{
+ WORD i;
+
+ i = ctx->datalen;
+
+ // Pad whatever data is left in the buffer.
+ if (ctx->datalen < 56) {
+ ctx->data[i++] = 0x80;
+ while (i < 56)
+ ctx->data[i++] = 0x00;
+ }
+ else {
+ ctx->data[i++] = 0x80;
+ while (i < 64)
+ ctx->data[i++] = 0x00;
+ sha1_transform(ctx, ctx->data);
+ memset(ctx->data, 0, 56);
+ }
+
+ // Append to the padding the total message's length in bits and transform.
+ ctx->bitlen += ctx->datalen * 8;
+ ctx->data[63] = ctx->bitlen;
+ ctx->data[62] = ctx->bitlen >> 8;
+ ctx->data[61] = ctx->bitlen >> 16;
+ ctx->data[60] = ctx->bitlen >> 24;
+ ctx->data[59] = ctx->bitlen >> 32;
+ ctx->data[58] = ctx->bitlen >> 40;
+ ctx->data[57] = ctx->bitlen >> 48;
+ ctx->data[56] = ctx->bitlen >> 56;
+ sha1_transform(ctx, ctx->data);
+
+ // Since this implementation uses little endian byte ordering and MD uses big endian,
+ // reverse all the bytes when copying the final state to the output hash.
+ for (i = 0; i < 4; ++i) {
+ hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/sha1.h b/test/monniaux/crypto-algorithms/sha1.h
new file mode 100644
index 00000000..f32bb7c0
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha1.h
@@ -0,0 +1,35 @@
+/*********************************************************************
+* Filename: sha1.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding SHA1 implementation.
+*********************************************************************/
+
+#ifndef SHA1_H
+#define SHA1_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+typedef struct {
+ BYTE data[64];
+ WORD datalen;
+ unsigned long long bitlen;
+ WORD state[5];
+ WORD k[4];
+} SHA1_CTX;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void sha1_init(SHA1_CTX *ctx);
+void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len);
+void sha1_final(SHA1_CTX *ctx, BYTE hash[]);
+
+#endif // SHA1_H
diff --git a/test/monniaux/crypto-algorithms/sha1_test.c b/test/monniaux/crypto-algorithms/sha1_test.c
new file mode 100644
index 00000000..6c78f7d2
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha1_test.c
@@ -0,0 +1,58 @@
+/*********************************************************************
+* Filename: sha1_test.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding SHA1
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include <string.h>
+#include "sha1.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int sha1_test()
+{
+ BYTE text1[] = {"abc"};
+ BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
+ BYTE text3[] = {"aaaaaaaaaa"};
+ BYTE hash1[SHA1_BLOCK_SIZE] = {0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d};
+ BYTE hash2[SHA1_BLOCK_SIZE] = {0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e,0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5,0xe5,0x46,0x70,0xf1};
+ BYTE hash3[SHA1_BLOCK_SIZE] = {0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4,0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31,0x65,0x34,0x01,0x6f};
+ BYTE buf[SHA1_BLOCK_SIZE];
+ int idx;
+ SHA1_CTX ctx;
+ int pass = 1;
+
+ sha1_init(&ctx);
+ sha1_update(&ctx, text1, strlen(text1));
+ sha1_final(&ctx, buf);
+ pass = pass && !memcmp(hash1, buf, SHA1_BLOCK_SIZE);
+
+ sha1_init(&ctx);
+ sha1_update(&ctx, text2, strlen(text2));
+ sha1_final(&ctx, buf);
+ pass = pass && !memcmp(hash2, buf, SHA1_BLOCK_SIZE);
+
+ sha1_init(&ctx);
+ for (idx = 0; idx < 100000; ++idx)
+ sha1_update(&ctx, text3, strlen(text3));
+ sha1_final(&ctx, buf);
+ pass = pass && !memcmp(hash3, buf, SHA1_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("SHA1 tests: %s\n", sha1_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/crypto-algorithms/sha256.c b/test/monniaux/crypto-algorithms/sha256.c
new file mode 100644
index 00000000..eb9c5c07
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha256.c
@@ -0,0 +1,158 @@
+/*********************************************************************
+* Filename: sha256.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the SHA-256 hashing algorithm.
+ SHA-256 is one of the three algorithms in the SHA2
+ specification. The others, SHA-384 and SHA-512, are not
+ offered in this implementation.
+ Algorithm specification can be found here:
+ * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
+ This implementation uses little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdlib.h>
+#include <memory.h>
+#include "sha256.h"
+
+/****************************** MACROS ******************************/
+#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
+#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
+
+#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
+#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
+#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
+#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
+#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
+#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
+
+/**************************** VARIABLES *****************************/
+static const WORD k[64] = {
+ 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
+ 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
+ 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
+ 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
+ 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
+ 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
+ 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
+ 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
+};
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
+{
+ WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
+
+ for (i = 0, j = 0; i < 16; ++i, j += 4)
+ m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
+ for ( ; i < 64; ++i)
+ m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
+
+ a = ctx->state[0];
+ b = ctx->state[1];
+ c = ctx->state[2];
+ d = ctx->state[3];
+ e = ctx->state[4];
+ f = ctx->state[5];
+ g = ctx->state[6];
+ h = ctx->state[7];
+
+ for (i = 0; i < 64; ++i) {
+ t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
+ t2 = EP0(a) + MAJ(a,b,c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + t1;
+ d = c;
+ c = b;
+ b = a;
+ a = t1 + t2;
+ }
+
+ ctx->state[0] += a;
+ ctx->state[1] += b;
+ ctx->state[2] += c;
+ ctx->state[3] += d;
+ ctx->state[4] += e;
+ ctx->state[5] += f;
+ ctx->state[6] += g;
+ ctx->state[7] += h;
+}
+
+void sha256_init(SHA256_CTX *ctx)
+{
+ ctx->datalen = 0;
+ ctx->bitlen = 0;
+ ctx->state[0] = 0x6a09e667;
+ ctx->state[1] = 0xbb67ae85;
+ ctx->state[2] = 0x3c6ef372;
+ ctx->state[3] = 0xa54ff53a;
+ ctx->state[4] = 0x510e527f;
+ ctx->state[5] = 0x9b05688c;
+ ctx->state[6] = 0x1f83d9ab;
+ ctx->state[7] = 0x5be0cd19;
+}
+
+void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
+{
+ WORD i;
+
+ for (i = 0; i < len; ++i) {
+ ctx->data[ctx->datalen] = data[i];
+ ctx->datalen++;
+ if (ctx->datalen == 64) {
+ sha256_transform(ctx, ctx->data);
+ ctx->bitlen += 512;
+ ctx->datalen = 0;
+ }
+ }
+}
+
+void sha256_final(SHA256_CTX *ctx, BYTE hash[])
+{
+ WORD i;
+
+ i = ctx->datalen;
+
+ // Pad whatever data is left in the buffer.
+ if (ctx->datalen < 56) {
+ ctx->data[i++] = 0x80;
+ while (i < 56)
+ ctx->data[i++] = 0x00;
+ }
+ else {
+ ctx->data[i++] = 0x80;
+ while (i < 64)
+ ctx->data[i++] = 0x00;
+ sha256_transform(ctx, ctx->data);
+ memset(ctx->data, 0, 56);
+ }
+
+ // Append to the padding the total message's length in bits and transform.
+ ctx->bitlen += ctx->datalen * 8;
+ ctx->data[63] = ctx->bitlen;
+ ctx->data[62] = ctx->bitlen >> 8;
+ ctx->data[61] = ctx->bitlen >> 16;
+ ctx->data[60] = ctx->bitlen >> 24;
+ ctx->data[59] = ctx->bitlen >> 32;
+ ctx->data[58] = ctx->bitlen >> 40;
+ ctx->data[57] = ctx->bitlen >> 48;
+ ctx->data[56] = ctx->bitlen >> 56;
+ sha256_transform(ctx, ctx->data);
+
+ // Since this implementation uses little endian byte ordering and SHA uses big endian,
+ // reverse all the bytes when copying the final state to the output hash.
+ for (i = 0; i < 4; ++i) {
+ hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
+ hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
+ }
+}
diff --git a/test/monniaux/crypto-algorithms/sha256.h b/test/monniaux/crypto-algorithms/sha256.h
new file mode 100644
index 00000000..7123a30d
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha256.h
@@ -0,0 +1,34 @@
+/*********************************************************************
+* Filename: sha256.h
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Defines the API for the corresponding SHA1 implementation.
+*********************************************************************/
+
+#ifndef SHA256_H
+#define SHA256_H
+
+/*************************** HEADER FILES ***************************/
+#include <stddef.h>
+
+/****************************** MACROS ******************************/
+#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
+
+/**************************** DATA TYPES ****************************/
+typedef unsigned char BYTE; // 8-bit byte
+typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
+
+typedef struct {
+ BYTE data[64];
+ WORD datalen;
+ unsigned long long bitlen;
+ WORD state[8];
+} SHA256_CTX;
+
+/*********************** FUNCTION DECLARATIONS **********************/
+void sha256_init(SHA256_CTX *ctx);
+void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
+void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
+
+#endif // SHA256_H
diff --git a/test/monniaux/crypto-algorithms/sha256_test.c b/test/monniaux/crypto-algorithms/sha256_test.c
new file mode 100644
index 00000000..726a6686
--- /dev/null
+++ b/test/monniaux/crypto-algorithms/sha256_test.c
@@ -0,0 +1,61 @@
+/*********************************************************************
+* Filename: sha256.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Performs known-answer tests on the corresponding SHA1
+ implementation. These tests do not encompass the full
+ range of available test vectors, however, if the tests
+ pass it is very, very likely that the code is correct
+ and was compiled properly. This code also serves as
+ example usage of the functions.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <stdio.h>
+#include <memory.h>
+#include <string.h>
+#include "sha256.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+int sha256_test()
+{
+ BYTE text1[] = {"abc"};
+ BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
+ BYTE text3[] = {"aaaaaaaaaa"};
+ BYTE hash1[SHA256_BLOCK_SIZE] = {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
+ 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad};
+ BYTE hash2[SHA256_BLOCK_SIZE] = {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
+ 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1};
+ BYTE hash3[SHA256_BLOCK_SIZE] = {0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67,
+ 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0};
+ BYTE buf[SHA256_BLOCK_SIZE];
+ SHA256_CTX ctx;
+ int idx;
+ int pass = 1;
+
+ sha256_init(&ctx);
+ sha256_update(&ctx, text1, strlen(text1));
+ sha256_final(&ctx, buf);
+ pass = pass && !memcmp(hash1, buf, SHA256_BLOCK_SIZE);
+
+ sha256_init(&ctx);
+ sha256_update(&ctx, text2, strlen(text2));
+ sha256_final(&ctx, buf);
+ pass = pass && !memcmp(hash2, buf, SHA256_BLOCK_SIZE);
+
+ sha256_init(&ctx);
+ for (idx = 0; idx < 100000; ++idx)
+ sha256_update(&ctx, text3, strlen(text3));
+ sha256_final(&ctx, buf);
+ pass = pass && !memcmp(hash3, buf, SHA256_BLOCK_SIZE);
+
+ return(pass);
+}
+
+int main()
+{
+ printf("SHA-256 tests: %s\n", sha256_test() ? "SUCCEEDED" : "FAILED");
+
+ return(0);
+}
diff --git a/test/monniaux/cse2/loopaccess.c b/test/monniaux/cse2/loopaccess.c
new file mode 100644
index 00000000..5ddaeb66
--- /dev/null
+++ b/test/monniaux/cse2/loopaccess.c
@@ -0,0 +1,7 @@
+double toto(double x, int count) {
+ double r = 5*x + 3;
+ while (count > r) {
+ count --;
+ }
+ return 5*x + 3;
+}
diff --git a/test/monniaux/cse2/loopinvariant.c b/test/monniaux/cse2/loopinvariant.c
new file mode 100644
index 00000000..64caf80b
--- /dev/null
+++ b/test/monniaux/cse2/loopinvariant.c
@@ -0,0 +1,7 @@
+int toto(int *t, int n) {
+ int x = t[0];
+ for(int i=1; i<n; i++) {
+ if (t[i] > t[0]) return i;
+ }
+ return 0;
+}
diff --git a/test/monniaux/cse2/loopload.c b/test/monniaux/cse2/loopload.c
new file mode 100644
index 00000000..6e0925f7
--- /dev/null
+++ b/test/monniaux/cse2/loopload.c
@@ -0,0 +1,5 @@
+int find_index(int *t, int n) {
+ if (t[0] > 0) return 3;
+ while (n > 0) n--;
+ return t[0];
+}
diff --git a/test/monniaux/cse2/noloopinvariant.c b/test/monniaux/cse2/noloopinvariant.c
new file mode 100644
index 00000000..5c7789bf
--- /dev/null
+++ b/test/monniaux/cse2/noloopinvariant.c
@@ -0,0 +1,6 @@
+int toto(int *t, int n) {
+ for(int i=1; i<n; i++) {
+ if (t[i] > t[0]) return i;
+ }
+ return 0;
+}
diff --git a/test/monniaux/cse2/storeload.c b/test/monniaux/cse2/storeload.c
new file mode 100644
index 00000000..028fb835
--- /dev/null
+++ b/test/monniaux/cse2/storeload.c
@@ -0,0 +1,5 @@
+int toto(int *p, int x) {
+ p[0] = x;
+ p[1] = 3;
+ return *p;
+}
diff --git a/test/monniaux/csmith/Makefile b/test/monniaux/csmith/Makefile
new file mode 100644
index 00000000..2425435d
--- /dev/null
+++ b/test/monniaux/csmith/Makefile
@@ -0,0 +1,116 @@
+all:
+
+.SECONDARY:
+
+INCLUDES=-I csmith/include/csmith-2.3.0/
+TARGET_CCOMP=../../../ccomp
+
+ifndef TARGET_CC
+TARGET_CC=gcc
+endif
+
+ifndef EXECUTE
+EXECUTE=timeout 10s
+endif
+
+CFLAGS += -Wno-incompatible-pointer-types
+
+ifndef CSMITH
+CSMITH=csmith/bin/csmith
+endif
+CSMITHOPT=--max-funcs 10 --no-packed-struct
+
+MAX=500
+PREFIX=ran%06.f
+
+# CCOMPOPTS=-Wl,--defsym=USER_STACK_SIZE=0x80000
+CCOMPFLAGS+= -fstruct-passing -fbitfields -fno-cse2 -fno-cse -fno-cse3 -stdlib ../../../runtime
+
+TESTS_C=$(shell seq --format $(PREFIX)/source.c 1 $(MAX))
+
+TESTS_CCOMP_TARGET_S=$(TEST_C:.c=.ccomp.target.s)
+TESTS_GCC_TARGET_S=$(TEST_C:.c=.gcc.target.s)
+TESTS_GCC_HOST_S=$(TEST_C:.c=.gcc.host.s)
+TESTS_GCC_O3_TARGET_S=$(TEST_C:.c=.gcc_O3.target.s)
+TESTS_GCC_O3_HOST_S=$(TEST_C:.c=.gcc_O3.host.s)
+TESTS_CCOMP_TARGET_OUT=$(shell seq --format $(PREFIX)/example.ccomp.target.out 1 $(MAX))
+TESTS_GCC_TARGET_OUT=$(shell seq --format $(PREFIX)/example.gcc.target.out 1 $(MAX))
+TESTS_GCC_O3_TARGET_OUT=$(shell seq --format $(PREFIX)/example.gcc_O3.target.out 1 $(MAX))
+TESTS_GCC_HOST_OUT=$(shell seq --format $(PREFIX)/example.gcc.host.out 1 $(MAX))
+TESTS_GCC_O3_HOST_OUT=$(shell seq --format $(PREFIX)/example.gcc_O3.host.out 1 $(MAX))
+TESTS_CMP=$(shell seq --format $(PREFIX)/example.target.cmp 1 $(MAX))
+TESTS_CMP_TARGET_HOST=$(shell seq --format $(PREFIX)/example.host_target.cmp 1 $(MAX))
+TESTS_CMP_O3=$(shell seq --format $(PREFIX)/example.target.cmp_O3 1 $(MAX))
+
+
+all: $(TESTS_CCOMP_TARGET_OUT) $(TESTS_GCC_TARGET_OUT) $(TESTS_CCOMP_TARGET_S) $(TESTS_GCC_TARGET_S) $(TESTS_CMP) $(TESTS_C)
+
+$(TESTS_C): $(CSMITH)
+
+tests_c: $(TESTS_C)
+
+tests_s: $(TESTS_CCOMP_TARGET_S)
+
+tests_O3: $(TESTS_CMP_O3)
+
+%.ccomp.target.s : %.c
+ $(TARGET_CCOMP) $(INCLUDES) $(CCOMPOPTS) $(CCOMPFLAGS) -S -o $@ $<
+
+%.gcc.target.s : %.c
+ $(TARGET_CC) $(INCLUDES) $(CFLAGS) -S -o $@ $<
+
+%.gcc_O3.target.s : %.c
+ $(TARGET_CC) $(INCLUDES) $(CFLAGS) -O3 -S -o $@ $<
+
+%.gcc.host.s : %.c
+ $(CC) $(INCLUDES) $(CFLAGS) -S -o $@ $<
+
+%.target.o : %.target.s
+ $(TARGET_CC) -c -o $@ $<
+
+%.host.o : %.host.s
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+%.target.out : %.target
+ $(EXECUTE) $< | tee $@
+
+%.host.out : %.host
+ ./$< | tee $@
+
+ran%/example.ccomp.target: ran%/source.ccomp.target.o
+ $(TARGET_CCOMP) $(CCOMPOPTS) $(CCOMPFLAGS) $+ -o $@
+
+ran%/example.gcc.target: ran%/source.gcc.target.o
+ $(TARGET_CC) $(TARGET_CFLAGS) $+ -o $@
+
+ran%/example.gcc_O3.target: ran%/source.gcc_O3.target.o
+ $(TARGET_CC) $(TARGET_CFLAGS) $+ -o $@
+
+ran%/example.gcc.host: ran%/source.gcc.host.o
+ $(CC) $(CFLAGS) $+ -o $@
+
+ran%/source.c:
+ mkdir -p ran$*
+ $(CSMITH) $(CSMITHOPT) --seed $* --output ran$*/source.c
+
+ran%/example.target.cmp : ran%/example.ccomp.target.out ran%/example.gcc.target.out
+ cmp $+ > $@
+ cat $<
+
+ran%/example.target.cmp_O3 : ran%/example.gcc.target.out ran%/example.gcc_O3.target.out
+ cmp $+
+
+ran%/example.host_target.cmp : ran%/example.ccomp.target.out ran%/example.gcc.host.out
+ cmp $+ > $@
+
+.PHONY: all clean tests_c tests_c
+
+clean:
+ -rm -rf ran*
+
+csmith-2.3.0.tar.gz:
+ curl -L -o $@ https://embed.cs.utah.edu/csmith/csmith-2.3.0.tar.gz
+
+csmith/bin/csmith: csmith-2.3.0.tar.gz
+ tar xfz csmith-2.3.0.tar.gz
+ WD=`pwd` && cd csmith-2.3.0 && ./configure --prefix=$$WD/csmith && make install
diff --git a/test/monniaux/csmith/Makefile.old b/test/monniaux/csmith/Makefile.old
new file mode 100644
index 00000000..56313452
--- /dev/null
+++ b/test/monniaux/csmith/Makefile.old
@@ -0,0 +1,23 @@
+CSMITH?=/local/monniaux/packages/csmith-2.3.0/bin/csmith
+MAX=1000
+
+include ../rules.mk
+KVX_CCOMPFLAGS+=-I/local/monniaux/packages/csmith-2.3.0/include/csmith-2.3.0 -fstruct-passing -fbitfields
+
+TARGETS_S=$(shell seq --format src%06.f.ccomp.kvx.s 0 $(MAX))
+TARGETS_C=$(shell seq --format src%06.f.c 0 $(MAX))
+TARGETS_O=$(shell seq --format src%06.f.ccomp.kvx.o 0 $(MAX))
+
+all: c s o
+
+s: $(TARGETS_S)
+c: $(TARGETS_C)
+o: $(TARGETS_O)
+
+src%.c :
+ $(CSMITH) --output $@ --seed $*
+
+clean:
+ -rm -f $(TARGETS_C) $(TARGETS_S) $(TARGETS_O)
+
+.PHONY: s c o clean
diff --git a/test/monniaux/csmith/reduce/reduce.sh b/test/monniaux/csmith/reduce/reduce.sh
new file mode 100755
index 00000000..d8df11a5
--- /dev/null
+++ b/test/monniaux/csmith/reduce/reduce.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+INCLUDE=-I$COMPCERT/test/monniaux/csmith/csmith/include/csmith-2.3.0
+if kvx-cos-gcc $INCLUDE -Werror=uninitialized -Werror=implicit source.c -o source.gcc.target && kvx-cluster -- source.gcc.target >& source.gcc.log
+then
+ if $COMPCERT/ccomp $INCLUDE -fall -fno-cse -fno-cse2 -fno-cse3 source.c -o source.ccomp.target
+ then
+ kvx-cluster -- source.ccomp.target >& source.ccomp.log
+ grep OPCODE source.ccomp.log
+ else
+ exit 2
+ fi
+else
+ exit 1
+fi
+
diff --git a/test/monniaux/csmith/reduce/reduce_wrt_host.sh b/test/monniaux/csmith/reduce/reduce_wrt_host.sh
new file mode 100755
index 00000000..7a11596f
--- /dev/null
+++ b/test/monniaux/csmith/reduce/reduce_wrt_host.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+INCLUDE=-I$COMPCERT/test/monniaux/csmith/csmith/include/csmith-2.3.0
+if /usr/bin/clang $INCLUDE -O -Werror=int-conversion -Werror=format -Werror=strict-prototypes -Werror=uninitialized -Werror=implicit -Werror=return-type source.c -o source.clang.host && valgrind --exit-on-first-error=yes --error-exitcode=120 --log-file=valgrind.out ./source.clang.host > source.clang.host.out ;
+then
+ if gcc $INCLUDE -Werror=int-conversion -Werror=format -Werror=strict-prototypes -Werror=uninitialized -Werror=implicit -Werror=return-type -fsanitize=undefined -fsanitize=address source.c -o source.gcc+san.host && ./source.gcc+san.host > source.gcc+san.host.out ;
+ then
+ if grep "runtime error" source.gcc+san.host.out
+ then
+ exit 66
+ fi
+ if cmp source.clang.host.out source.gcc+san.host.out && riscv64-linux-gnu-gcc $INCLUDE -Werror=format -Werror=uninitialized -Werror=implicit source.c -Werror=return-type -o source.gcc.target && qemu-riscv64 -L /usr/riscv64-linux-gnu source.gcc.target >& source.gcc.target.out && grep checksum source.gcc.target.out > source.gcc.target.check && diff source.clang.host.out source.gcc.target.out
+ then
+ if $COMPCERT/ccomp $INCLUDE -Werror=missing-declarations -fall -fno-cse -fno-cse2 -fno-cse3 source.c -o source.ccomp.target
+ then
+ qemu-riscv64 -L /usr/riscv64-linux-gnu source.ccomp.target >& source.ccomp.target.out
+ if grep checksum source.ccomp.target.out > source.ccomp.target.check
+ then
+ if diff source.ccomp.target.check source.gcc.target.check
+ then exit 40
+ else exit 0
+ fi
+ else
+ exit 50
+ fi
+ else
+ exit 2
+ fi
+ else
+ exit 1
+ fi
+ else
+ exit 4
+ fi
+else
+ exit 5
+fi
+
diff --git a/test/monniaux/cycles.h b/test/monniaux/cycles.h
new file mode 100644
index 00000000..2905938b
--- /dev/null
+++ b/test/monniaux/cycles.h
@@ -0,0 +1,105 @@
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+#ifdef __KVX__
+typedef uint64_t cycle_t;
+#define PRcycle PRId64
+
+#include "/opt/kalray/accesscore/kvx-cos/include/hal/cos_registers.h"
+
+static inline void cycle_count_config(void)
+{
+ /* config pmc for cycle count */
+ cycle_t pmc_value = __builtin_kvx_get(COS_SFR_PMC);
+
+ pmc_value &= ~(0xfULL);
+ __builtin_kvx_set(COS_SFR_PMC, pmc_value);
+}
+
+static inline cycle_t get_cycle(void)
+{
+ return __builtin_kvx_get(COS_SFR_PM0);
+}
+
+#else // not KVX
+static inline void cycle_count_config(void) { }
+
+#if defined(__i386__) || defined( __x86_64__)
+#define PRcycle PRId64
+typedef uint64_t cycle_t;
+#include <x86intrin.h>
+static inline cycle_t get_cycle(void) { return __rdtsc(); }
+
+#elif __riscv
+#ifdef __riscv32
+#define PRcycle PRId32
+typedef uint32_t cycle_t;
+#else
+#define PRcycle PRId64
+typedef uint64_t cycle_t;
+#endif
+static inline cycle_t get_cycle(void) {
+ cycle_t cycles;
+ asm volatile ("rdcycle %0" : "=r" (cycles));
+ return cycles;
+}
+
+#elif defined (__ARM_ARCH) // && (__ARM_ARCH >= 6)
+#if (__ARM_ARCH < 8)
+typedef uint32_t cycle_t;
+#define PRcycle PRId32
+
+#ifdef ARM_NO_PRIVILEGE
+static inline cycle_t get_cycle(void) {
+ return 0;
+}
+#else
+/* need this kernel module
+https://github.com/zertyz/MTL/tree/master/cpp/time/kernel/arm */
+static inline cycle_t get_cycle(void) {
+ cycle_t cycles;
+ __asm__ volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (cycles));
+ return cycles;
+}
+#endif
+#else
+#define PRcycle PRId64
+typedef uint64_t cycle_t;
+
+#ifdef ARM_NO_PRIVILEGE
+static inline cycle_t get_cycle(void) {
+ return 0;
+}
+#else
+/* need this kernel module:
+https://github.com/jerinjacobk/armv8_pmu_cycle_counter_el0
+
+on 5+ kernels, remove first argument of access_ok macro */
+static inline cycle_t get_cycle(void)
+{
+ uint64_t val;
+ __asm__ volatile("mrs %0, pmccntr_el0" : "=r"(val));
+ return val;
+}
+#endif
+#endif
+
+#else
+#define PRcycle PRId32
+typedef uint32_t cycle_t;
+static inline cycle_t get_cycle(void) { return 0; }
+#endif
+#endif
+
+#ifdef MAX_MEASURES
+ #define TIMEINIT(i) {_last_stop[i] = get_cycle();}
+ #define TIMESTOP(i) {cycle_t cur = get_cycle(); _total_cycles[i] += cur - _last_stop[i]; _last_stop[i] = cur;}
+ #define TIMEPRINT(n) { for (int i = 0; i <= n; i++) printf("%d cycles: %" PRIu64 "\n", i, _total_cycles[i]); }
+#endif
+
+
+#ifdef MAX_MEASURES
+ static cycle_t _last_stop[MAX_MEASURES] = {0};
+ static cycle_t _total_cycles[MAX_MEASURES] = {0};
+#endif
diff --git a/test/monniaux/des/des.c b/test/monniaux/des/des.c
new file mode 100644
index 00000000..64ccdc5e
--- /dev/null
+++ b/test/monniaux/des/des.c
@@ -0,0 +1,501 @@
+/* From Rosetta Code */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned char ubyte;
+
+#define KEY_LEN 8
+typedef ubyte des_key_t[KEY_LEN];
+
+const static ubyte PC1[] = {
+ 57, 49, 41, 33, 25, 17, 9,
+ 1, 58, 50, 42, 34, 26, 18,
+ 10, 2, 59, 51, 43, 35, 27,
+ 19, 11, 3, 60, 52, 44, 36,
+ 63, 55, 47, 39, 31, 23, 15,
+ 7, 62, 54, 46, 38, 30, 22,
+ 14, 6, 61, 53, 45, 37, 29,
+ 21, 13, 5, 28, 20, 12, 4
+};
+
+const static ubyte PC2[] = {
+ 14, 17, 11, 24, 1, 5,
+ 3, 28, 15, 6, 21, 10,
+ 23, 19, 12, 4, 26, 8,
+ 16, 7, 27, 20, 13, 2,
+ 41, 52, 31, 37, 47, 55,
+ 30, 40, 51, 45, 33, 48,
+ 44, 49, 39, 56, 34, 53,
+ 46, 42, 50, 36, 29, 32
+};
+
+const static ubyte IP[] = {
+ 58, 50, 42, 34, 26, 18, 10, 2,
+ 60, 52, 44, 36, 28, 20, 12, 4,
+ 62, 54, 46, 38, 30, 22, 14, 6,
+ 64, 56, 48, 40, 32, 24, 16, 8,
+ 57, 49, 41, 33, 25, 17, 9, 1,
+ 59, 51, 43, 35, 27, 19, 11, 3,
+ 61, 53, 45, 37, 29, 21, 13, 5,
+ 63, 55, 47, 39, 31, 23, 15, 7
+};
+
+const static ubyte E[] = {
+ 32, 1, 2, 3, 4, 5,
+ 4, 5, 6, 7, 8, 9,
+ 8, 9, 10, 11, 12, 13,
+ 12, 13, 14, 15, 16, 17,
+ 16, 17, 18, 19, 20, 21,
+ 20, 21, 22, 23, 24, 25,
+ 24, 25, 26, 27, 28, 29,
+ 28, 29, 30, 31, 32, 1
+};
+
+const static ubyte S[][64] = {
+ {
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
+ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
+ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
+ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
+ },
+ {
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
+ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
+ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
+ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
+ },
+ {
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
+ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
+ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
+ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
+ },
+ {
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
+ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
+ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
+ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
+ },
+ {
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
+ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
+ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
+ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
+ },
+ {
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
+ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
+ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
+ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
+ },
+ {
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
+ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
+ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
+ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
+ },
+ {
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
+ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
+ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
+ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
+ }
+};
+
+const static ubyte P[] = {
+ 16, 7, 20, 21,
+ 29, 12, 28, 17,
+ 1, 15, 23, 26,
+ 5, 18, 31, 10,
+ 2, 8, 24, 14,
+ 32, 27, 3, 9,
+ 19, 13, 30, 6,
+ 22, 11, 4, 25
+};
+
+const static ubyte IP2[] = {
+ 40, 8, 48, 16, 56, 24, 64, 32,
+ 39, 7, 47, 15, 55, 23, 63, 31,
+ 38, 6, 46, 14, 54, 22, 62, 30,
+ 37, 5, 45, 13, 53, 21, 61, 29,
+ 36, 4, 44, 12, 52, 20, 60, 28,
+ 35, 3, 43, 11, 51, 19, 59, 27,
+ 34, 2, 42, 10, 50, 18, 58, 26,
+ 33, 1, 41, 9, 49, 17, 57, 25
+};
+
+const static ubyte SHIFTS[] = {
+ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
+};
+
+typedef struct {
+ ubyte *data;
+ int len;
+} String;
+
+/*
+ * Transform a single nibble into a hex character
+ *
+ * in: a value < 0x10
+ *
+ * returns: the character that represents the nibble
+ */
+static char toHex(ubyte in) {
+ if (0x00 <= in && in < 0x0A) {
+ return '0' + in;
+ }
+ if (0x0A <= in && in <= 0x0F) {
+ return 'A' + in - 0x0A;
+ }
+ return 0;
+}
+
+/*
+ * Convert an array of bytes into a string
+ *
+ * ptr: the array of bytes
+ * len: the number of bytes
+ * out: a buffer allocated by the caller with enough space for 2*len+1 characters
+ */
+static void printBytes(const ubyte *ptr, int len, char *out) {
+ while (len-- > 0) {
+ *out++ = toHex(*ptr >> 4);
+ *out++ = toHex(*ptr & 0x0F);
+
+ ptr++;
+ }
+ *out = 0;
+}
+
+/*
+ * Gets the value of a bit in an array of bytes
+ *
+ * src: the array of bytes to index
+ * index: the desired bit to test the value of
+ *
+ * returns: the bit at the specified position in the array
+ */
+static int peekBit(const ubyte *src, int index) {
+ int cell = index / 8;
+ int bit = 7 - index % 8;
+ return (src[cell] & (1 << bit)) != 0;
+}
+
+/*
+ * Sets the value of a bit in an array of bytes
+ *
+ * dst: the array of bits to set a bit in
+ * index: the position of the bit to set
+ * value: the value for the bit to set
+ */
+static void pokeBit(ubyte *dst, int index, int value) {
+ int cell = index / 8;
+ int bit = 7 - index % 8;
+ if (value == 0) {
+ dst[cell] &= ~(1 << bit);
+ } else {
+ dst[cell] |= (1 << bit);
+ }
+}
+
+/*
+ * Transforms one array of bytes by shifting the bits the specified number of positions
+ *
+ * src: the array to shift bits from
+ * len: the length of the src array
+ * times: the number of positions that the bits should be shifted
+ * dst: a bytes array allocated by the caller to store the shifted values
+ */
+static void shiftLeft(const ubyte *src, int len, int times, ubyte *dst) {
+ int i, t;
+ for (i = 0; i <= len; ++i) {
+ pokeBit(dst, i, peekBit(src, i));
+ }
+ for (t = 1; t <= times; ++t) {
+ int temp = peekBit(dst, 0);
+ for (i = 1; i <= len; ++i) {
+ pokeBit(dst, i - 1, peekBit(dst, i));
+ }
+ pokeBit(dst, len - 1, temp);
+ }
+}
+
+/*
+ * Calculates the sub keys to be used in processing the messages
+ *
+ * key: the array of bytes representing the key
+ * ks: the subkeys that have been allocated by the caller
+ */
+typedef ubyte subdes_key_t[17][6]; /* 17 sets of 48 bits */
+static void getSubKeys(const des_key_t key, subdes_key_t ks) {
+ ubyte c[17][7]; /* 56 bits */
+ ubyte d[17][4]; /* 28 bits */
+ ubyte kp[7];
+ int i, j;
+
+ /* intialize */
+ memset(c, 0, sizeof(c));
+ memset(d, 0, sizeof(d));
+ memset(ks, 0, sizeof(subdes_key_t));
+
+ /* permute 'key' using table PC1 */
+ for (i = 0; i < 56; ++i) {
+ pokeBit(kp, i, peekBit(key, PC1[i] - 1));
+ }
+
+ /* split 'kp' in half and process the resulting series of 'c' and 'd' */
+ for (i = 0; i < 28; ++i) {
+ pokeBit(c[0], i, peekBit(kp, i));
+ pokeBit(d[0], i, peekBit(kp, i + 28));
+ }
+
+ /* shift the components of c and d */
+ for (i = 1; i < 17; ++i) {
+ shiftLeft(c[i - 1], 28, SHIFTS[i - 1], c[i]);
+ shiftLeft(d[i - 1], 28, SHIFTS[i - 1], d[i]);
+ }
+
+ /* merge 'd' into 'c' */
+ for (i = 1; i < 17; ++i) {
+ for (j = 28; j < 56; ++j) {
+ pokeBit(c[i], j, peekBit(d[i], j - 28));
+ }
+ }
+
+ /* form the sub-keys and store them in 'ks'
+ * permute 'c' using table PC2 */
+ for (i = 1; i < 17; ++i) {
+ for (j = 0; j < 48; ++j) {
+ pokeBit(ks[i], j, peekBit(c[i], PC2[j] - 1));
+ }
+ }
+}
+
+/*
+ * Function used in processing the messages
+ *
+ * r: an array of bytes to be processed
+ * ks: one of the subkeys to be used for processing
+ * sp: output from the processing
+ */
+static void f(ubyte *r, ubyte *ks, ubyte *sp) {
+ ubyte er[6]; /* 48 bits */
+ ubyte sr[4]; /* 32 bits */
+ int i;
+
+ /* initialize */
+ memset(er, 0, sizeof(er));
+ memset(sr, 0, sizeof(sr));
+
+ /* permute 'r' using table E */
+ for (i = 0; i < 48; ++i) {
+ pokeBit(er, i, peekBit(r, E[i] - 1));
+ }
+
+ /* xor 'er' with 'ks' and store back into 'er' */
+ for (i = 0; i < 6; ++i) {
+ er[i] ^= ks[i];
+ }
+
+ /* process 'er' six bits at a time and store resulting four bits in 'sr' */
+ for (i = 0; i < 8; ++i) {
+ int j = i * 6;
+ int b[6];
+ int k, row, col, m, n;
+
+ for (k = 0; k < 6; ++k) {
+ b[k] = peekBit(er, j + k) != 0 ? 1 : 0;
+ }
+
+ row = 2 * b[0] + b[5];
+ col = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];
+ m = S[i][row * 16 + col]; /* apply table s */
+ n = 1;
+
+ while (m > 0) {
+ int p = m % 2;
+ pokeBit(sr, (i + 1) * 4 - n, p == 1);
+ m /= 2;
+ n++;
+ }
+ }
+
+ /* permute sr using table P */
+ for (i = 0; i < 32; ++i) {
+ pokeBit(sp, i, peekBit(sr, P[i] - 1));
+ }
+}
+
+/*
+ * Processing of block of the message
+ *
+ * message: an 8 byte block from the message
+ * ks: the subkeys to use in processing
+ * ep: space for an encoded 8 byte block allocated by the caller
+ */
+static void processMessage(const ubyte *message, subdes_key_t ks, ubyte *ep) {
+ ubyte left[17][4]; /* 32 bits */
+ ubyte right[17][4]; /* 32 bits */
+ ubyte mp[8]; /* 64 bits */
+ ubyte e[8]; /* 64 bits */
+ int i, j;
+
+ /* permute 'message' using table IP */
+ for (i = 0; i < 64; ++i) {
+ pokeBit(mp, i, peekBit(message, IP[i] - 1));
+ }
+
+ /* split 'mp' in half and process the resulting series of 'l' and 'r */
+ for (i = 0; i < 32; ++i) {
+ pokeBit(left[0], i, peekBit(mp, i));
+ pokeBit(right[0], i, peekBit(mp, i + 32));
+ }
+ for (i = 1; i < 17; ++i) {
+ ubyte fs[4]; /* 32 bits */
+
+ memcpy(left[i], right[i - 1], 4);
+ f(right[i - 1], ks[i], fs);
+ for (j = 0; j < 4; ++j) {
+ left[i - 1][j] ^= fs[j];
+ }
+ memcpy(right[i], left[i - 1], 4);
+ }
+
+ /* amalgamate r[16] and l[16] (in that order) into 'e' */
+ for (i = 0; i < 32; ++i) {
+ pokeBit(e, i, peekBit(right[16], i));
+ }
+ for (i = 32; i < 64; ++i) {
+ pokeBit(e, i, peekBit(left[16], i - 32));
+ }
+
+ /* permute 'e' using table IP2 ad return result as a hex string */
+ for (i = 0; i < 64; ++i) {
+ pokeBit(ep, i, peekBit(e, IP2[i] - 1));
+ }
+}
+
+/*
+ * Encrypts a message using DES
+ *
+ * key: the key to use to encrypt the message
+ * message: the message to be encrypted
+ * len: the length of the message
+ *
+ * returns: a paring of dynamically allocated memory for the encoded message,
+ * and the length of the encoded message.
+ * the caller will need to free the memory after use.
+ */
+String encrypt(const des_key_t key, const ubyte *message, int len) {
+ String result = { 0, 0 };
+ subdes_key_t ks;
+ ubyte padByte;
+ int i;
+
+ getSubKeys(key, ks);
+
+ padByte = 8 - len % 8;
+ result.len = len + padByte;
+ result.data = (ubyte*)malloc(result.len);
+ memcpy(result.data, message, len);
+ memset(&result.data[len], padByte, padByte);
+
+ for (i = 0; i < result.len; i += 8) {
+ processMessage(&result.data[i], ks, &result.data[i]);
+ }
+
+ return result;
+}
+
+/*
+ * Decrypts a message using DES
+ *
+ * key: the key to use to decrypt the message
+ * message: the message to be decrypted
+ * len: the length of the message
+ *
+ * returns: a paring of dynamically allocated memory for the decoded message,
+ * and the length of the decoded message.
+ * the caller will need to free the memory after use.
+ */
+String decrypt(const des_key_t key, const ubyte *message, int len) {
+ String result = { 0, 0 };
+ subdes_key_t ks;
+ int i, j;
+ ubyte padByte;
+
+ getSubKeys(key, ks);
+ /* reverse the subkeys */
+ for (i = 1; i < 9; ++i) {
+ for (j = 0; j < 6; ++j) {
+ ubyte temp = ks[i][j];
+ ks[i][j] = ks[17 - i][j];
+ ks[17 - i][j] = temp;
+ }
+ }
+
+ result.data = (ubyte*)malloc(len);
+ memcpy(result.data, message, len);
+ result.len = len;
+ for (i = 0; i < result.len; i += 8) {
+ processMessage(&result.data[i], ks, &result.data[i]);
+ }
+
+ padByte = result.data[len - 1];
+ result.len -= padByte;
+ return result;
+}
+
+/*
+ * Convienience method for showing the round trip processing of a message
+ */
+void driver(const des_key_t key, const ubyte *message, int len) {
+ String encoded, decoded;
+ char buffer[128];
+
+ printBytes(key, KEY_LEN, buffer);
+ printf("Key : %s\n", buffer);
+
+ printBytes(message, len, buffer);
+ printf("Message : %s\n", buffer);
+
+ encoded = encrypt(key, message, len);
+ printBytes(encoded.data, encoded.len, buffer);
+ printf("Encoded : %s\n", buffer);
+
+ decoded = decrypt(key, encoded.data, encoded.len);
+ printBytes(decoded.data, decoded.len, buffer);
+ printf("Decoded : %s\n\n", buffer);
+
+ /* release allocated memory */
+ if (encoded.len > 0) {
+ free(encoded.data);
+ encoded.data = 0;
+ }
+ if (decoded.len > 0) {
+ free(decoded.data);
+ decoded.data = 0;
+ }
+}
+
+int main() {
+ const des_key_t keys[] = {
+ {0x13, 0x34, 0x57, 0x79, 0x9B, 0xBC, 0xDF, 0xF1},
+ {0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73},
+ {0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73}
+ };
+ const ubyte message1[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
+ const ubyte message2[] = { 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87 };
+ const ubyte message3[] = { 0x59, 0x6F, 0x75, 0x72, 0x20, 0x6C, 0x69, 0x70, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x73, 0x6D, 0x6F, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x76, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x0D, 0x0A };
+ int len;
+
+ len = sizeof(message1) / sizeof(ubyte);
+ driver(keys[0], message1, len);
+
+ len = sizeof(message2) / sizeof(ubyte);
+ driver(keys[1], message2, len);
+
+ len = sizeof(message3) / sizeof(ubyte);
+ driver(keys[2], message3, len);
+}
diff --git a/test/monniaux/division/harness.c b/test/monniaux/division/harness.c
new file mode 100644
index 00000000..b6ce674d
--- /dev/null
+++ b/test/monniaux/division/harness.c
@@ -0,0 +1,82 @@
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <assert.h>
+#include "../cycles.h"
+
+static uint32_t dm_random_uint32(void) {
+ static uint32_t current=UINT32_C(0xDEADBEEF);
+ current = ((uint64_t) current << 6) % UINT32_C(4294967291);
+ return current;
+}
+
+static uint64_t dm_biased_random_uint32(void) {
+ uint32_t flags = dm_random_uint32();
+ uint32_t r;
+ switch (flags & 15) {
+ case 0:
+ r = dm_random_uint32() & 0xFU;
+ break;
+ case 1:
+ r = dm_random_uint32() & 0xFFU;
+ break;
+ case 2:
+ r = dm_random_uint32() & 0xFFFU;
+ break;
+ case 3:
+ r = dm_random_uint32() & 0xFFFFU;
+ break;
+ case 4:
+ r = dm_random_uint32() & 0xFFFFFU;
+ break;
+ case 5:
+ r = dm_random_uint32() & 0xFFFFFFU;
+ break;
+ case 6:
+ r = dm_random_uint32() & 0xFFFFFFFU;
+ break;
+ case 7:
+ r = dm_random_uint32() & 0x3;
+ break;
+ default:
+ r = dm_random_uint32();
+ }
+ return r;
+}
+
+inline uint32_t native_udiv32(uint32_t x, uint32_t y) {
+ return x/y;
+}
+extern uint32_t my_udiv32(uint32_t x, uint32_t y);
+
+int main() {
+ cycle_t time_me=0, time_native=0;
+ cycle_count_config();
+
+ for(int i=0; i<1000; i++) {
+ uint32_t x = dm_biased_random_uint32();
+ uint32_t y = dm_biased_random_uint32();
+ if (y == 0) continue;
+
+ cycle_t cycle_a, cycle_b, cycle_c;
+
+ uint32_t q1, q2;
+ cycle_a = get_cycle();
+ q1 = native_udiv32(x, y);
+ cycle_b = get_cycle();
+ q2 = my_udiv32(x, y);
+ cycle_c = get_cycle();
+
+ if(q1 != q2) {
+ printf("ERREUR %u %u\n", q1, q2);
+ }
+
+ time_native += cycle_b - cycle_a;
+ time_me += cycle_c - cycle_b;
+ }
+
+ printf("%" PRcycle "\t%" PRcycle "\n", time_native, time_me);
+
+ return 0;
+}
diff --git a/test/monniaux/division/my_udiv32.s b/test/monniaux/division/my_udiv32.s
new file mode 100644
index 00000000..0f4fd127
--- /dev/null
+++ b/test/monniaux/division/my_udiv32.s
@@ -0,0 +1,36 @@
+ .align 8
+ .global my_udiv32
+ .type my_udiv32, @function
+my_udiv32:
+ zxwd $r1 = $r1
+ make $r3 = 0x3ff0000000000000 # 1.0
+ zxwd $r0 = $r0
+ ;;
+ floatud.rn $r5 = $r1, 0
+ ;;
+ floatuw.rn $r2 = $r1, 0
+ ;;
+ finvw $r2 = $r2
+ ;;
+
+ fwidenlwd $r2 = $r2
+ floatud.rn $r4 = $r0, 0
+ ;;
+ ffmsd $r3 = $r2, $r5
+ ;;
+ ffmad $r2 = $r2, $r3
+ ;;
+ fmuld $r2 = $r2, $r4
+ ;;
+ fixedud.rn $r2 = $r2, 0
+ ;;
+ msbfw $r0 = $r2, $r1
+ zxwd $r1 = $r2
+ addw $r2 = $r2, -1
+ ;;
+ cmoved.wltz $r0? $r1 = $r2
+ ;;
+ copyd $r0 = $r1
+ ret
+ ;;
+ .size my_udiv32, .-my_udiv32
diff --git a/test/monniaux/dm_random.c b/test/monniaux/dm_random.c
new file mode 100644
index 00000000..6643b79f
--- /dev/null
+++ b/test/monniaux/dm_random.c
@@ -0,0 +1,7 @@
+#include <stdint.h>
+
+static uint32_t dm_random_uint32(void) {
+ static uint32_t current=UINT32_C(0xDEADBEEF);
+ current = ((uint64_t) current << 6) % UINT32_C(4294967291);
+ return current;
+}
diff --git a/test/monniaux/expect/expect.c b/test/monniaux/expect/expect.c
new file mode 100644
index 00000000..30e0742a
--- /dev/null
+++ b/test/monniaux/expect/expect.c
@@ -0,0 +1,7 @@
+#ifndef PREDICTED
+#define PREDICTED 0
+#endif
+
+int expect(int x, int *y, int *z) {
+ return __builtin_expect(x, PREDICTED) ? *y : *z;
+}
diff --git a/test/monniaux/fill_buffer/fill_buffer.c b/test/monniaux/fill_buffer/fill_buffer.c
new file mode 100644
index 00000000..a83267f5
--- /dev/null
+++ b/test/monniaux/fill_buffer/fill_buffer.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "../clock.h"
+
+typedef unsigned char data;
+
+void fill_buffer(data x, unsigned n, data *buf) {
+ for(unsigned i=0; i<n; i++) buf[i] = x;
+}
+
+void fill_buffer2(data x, unsigned n, data *buf) {
+ unsigned i=0;
+ if (i<n) {
+ do {
+ buf[i] = x;
+ i++;
+ } while (i<n);
+ }
+}
+
+void fill_buffer10(data x, data *buf) {
+ for(unsigned i=0; i<10; i++) buf[i] = x;
+}
+
+int main() {
+ const size_t n = 10000;
+ data *buf = malloc(n * sizeof(data));
+ clock_prepare();
+ clock_start();
+ fill_buffer2(42, n, buf);
+ clock_stop();
+ free(buf);
+ print_total_clock();
+ return 0;
+}
diff --git a/test/monniaux/float_mat/Makefile b/test/monniaux/float_mat/Makefile
new file mode 100644
index 00000000..69621159
--- /dev/null
+++ b/test/monniaux/float_mat/Makefile
@@ -0,0 +1,4 @@
+TARGET=float_mat
+MEASURES="c1 c2 c3 c4 c5 c6 c7 c8"
+
+include ../rules.mk
diff --git a/test/monniaux/float_mat/float_mat.c b/test/monniaux/float_mat/float_mat.c
new file mode 100644
index 00000000..45612635
--- /dev/null
+++ b/test/monniaux/float_mat/float_mat.c
@@ -0,0 +1,228 @@
+#include "float_mat.h"
+#include <stddef.h>
+
+#define ADD +=
+#define MUL *
+
+void REAL_mat_mul1(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ c[i*stride_c+k] = 0;
+ }
+ }
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ for(unsigned j=0; j<n; j++) {
+ c[i*stride_c+k] ADD (a[i*stride_a+j] MUL b[j*stride_b+k]);
+ }
+ }
+ }
+}
+
+void REAL_mat_mul2(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ REAL total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total ADD (a[i*stride_a + j] MUL b[j*stride_b + k]);
+ }
+ c[i*stride_c+k] = total;
+ }
+ }
+}
+
+void REAL_mat_mul3(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ REAL total0 = 0, total1 = 0;
+ unsigned j;
+ for(j=0; j+1<n; j+=2) {
+ total0 ADD (a[i*stride_a + j] MUL b[j*stride_b + k]);
+ total1 ADD (a[i*stride_a + (j+1)] MUL b[(j+1)*stride_b + k]);
+ }
+ if (j < n) {
+ total0 ADD a[i*stride_a + j] MUL b[j*stride_b + k];
+ }
+ total0 ADD total1;
+ c[i*stride_c+k] = total0;
+ }
+ }
+}
+
+void REAL_mat_mul4(unsigned m, unsigned n, unsigned p,
+ REAL * c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ const REAL *pa_i = a;
+ REAL * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const REAL *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total ADD (*pa_i_j MUL *pb_j_k);
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+void REAL_mat_mul5(unsigned m, unsigned n, unsigned p,
+ REAL * c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ const REAL *pa_i = a;
+ REAL * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const REAL *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL total = 0;
+ for(unsigned j2=0, n2=n/2; j2<n2; j2++) {
+ REAL p0 = *pa_i_j MUL *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ REAL p1 = *pa_i_j MUL *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total ADD p0;
+ total ADD p1;
+ }
+ if (n%2) {
+ total ADD *pa_i_j MUL *pb_j_k;
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#define CHUNK \
+ total ADD (*pa_i_j MUL *pb_j_k); \
+ pa_i_j ++; \
+ pb_j_k += stride_b;
+
+void REAL_mat_mul6(unsigned m, unsigned n, unsigned p,
+ REAL * c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ const REAL *pa_i = a;
+ REAL * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const REAL *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL total = 0;
+ unsigned j2=0, n2=n/2;
+ if (n2 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ j2++;
+ } while (j2 < n2);
+ }
+ if (n%2) {
+ total ADD (*pa_i_j MUL *pb_j_k);
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#define UNROLL 4
+void REAL_mat_mul7(unsigned m, unsigned n, unsigned p,
+ REAL * c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ const REAL *pa_i = a;
+ REAL * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const REAL *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL total = 0;
+ {
+ unsigned j4=0, n4=n/UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ CHUNK
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#undef CHUNK
+#define CHUNK \
+ total ADD (*pa_i_j MUL *pb_j_k); \
+ pa_i_j ++; \
+ pb_j_k = (REAL*) ((char*) pb_j_k + stride_b_scaled);
+
+void REAL_mat_mul8(unsigned m, unsigned n, unsigned p,
+ REAL * c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ const REAL *pa_i = a;
+ REAL * pc_i = c;
+ size_t stride_b_scaled = sizeof(REAL) * stride_b;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const REAL *pb_j_k = b+k, *pa_i_j = pa_i;
+ REAL total = 0;
+ {
+ unsigned j4=0, n4=n/UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ CHUNK
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
diff --git a/test/monniaux/float_mat/float_mat.h b/test/monniaux/float_mat/float_mat.h
new file mode 100644
index 00000000..3b787b1f
--- /dev/null
+++ b/test/monniaux/float_mat/float_mat.h
@@ -0,0 +1,55 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef double REAL;
+
+void REAL_mat_mul1(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul2(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul3(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul4(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul5(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul6(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul7(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+void REAL_mat_mul8(unsigned m, unsigned n, unsigned p,
+ REAL * restrict c, unsigned stride_c,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
+
+REAL REAL_random(void);
+
+void REAL_mat_random(unsigned m,
+ unsigned n,
+ REAL *a, unsigned stride_a);
+
+bool REAL_mat_equal(unsigned m,
+ unsigned n,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b);
diff --git a/test/monniaux/float_mat/float_mat_run.c b/test/monniaux/float_mat/float_mat_run.c
new file mode 100644
index 00000000..2f590f98
--- /dev/null
+++ b/test/monniaux/float_mat/float_mat_run.c
@@ -0,0 +1,133 @@
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "float_mat.h"
+#include "../cycles.h"
+
+/* FIXME DMonniaux should be in the other but branches and float_of_int not implemented */
+bool REAL_mat_equal(unsigned m,
+ unsigned n,
+ const REAL *a, unsigned stride_a,
+ const REAL *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ if (a[i*stride_a + j] != b[i*stride_b + j]) {
+ printf("at %u,%u: %g vs %g\n", i, j,
+ a[i*stride_a + j], b[i*stride_b + j]);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+REAL REAL_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next % 1000;
+}
+
+void REAL_mat_random(unsigned m,
+ unsigned n,
+ REAL *a, unsigned stride_a) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ a[i*stride_a + j] = REAL_random();
+ }
+ }
+}
+
+int main() {
+ const unsigned m = 60, n = 31, p = 50;
+ cycle_count_config();
+ REAL *a = malloc(sizeof(REAL) * m * n);
+ REAL_mat_random(m, n, a, n);
+ REAL *b = malloc(sizeof(REAL) * n * p);
+ REAL_mat_random(n, p, b, p);
+
+ REAL *c1 = malloc(sizeof(REAL) * m * p);
+ cycle_t c1_time = get_cycle();
+ REAL_mat_mul1(m, n, p, c1, p, a, n, b, p);
+ c1_time = get_cycle()-c1_time;
+
+ REAL *c2 = malloc(sizeof(REAL) * m * p);
+ cycle_t c2_time = get_cycle();
+ REAL_mat_mul2(m, n, p, c2, p, a, n, b, p);
+ c2_time = get_cycle()-c2_time;
+
+ REAL *c3 = malloc(sizeof(REAL) * m * p);
+ cycle_t c3_time = get_cycle();
+ REAL_mat_mul3(m, n, p, c3, p, a, n, b, p);
+ c3_time = get_cycle()-c3_time;
+
+ REAL *c4 = malloc(sizeof(REAL) * m * p);
+ cycle_t c4_time = get_cycle();
+ REAL_mat_mul4(m, n, p, c4, p, a, n, b, p);
+ c4_time = get_cycle()-c4_time;
+
+ REAL *c5 = malloc(sizeof(REAL) * m * p);
+ cycle_t c5_time = get_cycle();
+ REAL_mat_mul5(m, n, p, c5, p, a, n, b, p);
+ c5_time = get_cycle()-c5_time;
+
+ REAL *c6 = malloc(sizeof(REAL) * m * p);
+ cycle_t c6_time = get_cycle();
+ REAL_mat_mul6(m, n, p, c6, p, a, n, b, p);
+ c6_time = get_cycle()-c6_time;
+
+ REAL *c7 = malloc(sizeof(REAL) * m * p);
+ cycle_t c7_time = get_cycle();
+ REAL_mat_mul7(m, n, p, c7, p, a, n, b, p);
+ c7_time = get_cycle()-c7_time;
+
+ REAL *c8 = malloc(sizeof(REAL) * m * p);
+ cycle_t c8_time = get_cycle();
+ REAL_mat_mul8(m, n, p, c8, p, a, n, b, p);
+ c8_time = get_cycle()-c8_time;
+
+ printf("c1==c2: %s\n"
+ "c1==c3: %s\n"
+ "c1==c4: %s\n"
+ "c1==c5: %s\n"
+ "c1==c6: %s\n"
+ "c1==c7: %s\n"
+ "c1==c8: %s\n"
+ "c1 cycles: %" PRIu64 "\n"
+ "c2 cycles: %" PRIu64 "\n"
+ "c3 cycles: %" PRIu64 "\n"
+ "c4 cycles: %" PRIu64 "\n"
+ "c5 cycles: %" PRIu64 "\n"
+ "c6 cycles: %" PRIu64 "\n"
+ "c7 cycles: %" PRIu64 "\n"
+ "c8 cycles: %" PRIu64 "\n",
+
+ REAL_mat_equal(m, n, c1, p, c2, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c3, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c4, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c5, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c6, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c7, p)?"true":"false",
+ REAL_mat_equal(m, n, c1, p, c8, p)?"true":"false",
+
+ c1_time,
+ c2_time,
+ c3_time,
+ c4_time,
+ c5_time,
+ c6_time,
+ c7_time,
+ c8_time);
+
+ free(a);
+ free(b);
+ free(c1);
+ free(c2);
+ free(c3);
+ free(c4);
+ free(c5);
+ free(c6);
+ free(c7);
+ free(c8);
+ return 0;
+}
diff --git a/test/monniaux/frame_pointer/Makefile b/test/monniaux/frame_pointer/Makefile
new file mode 100644
index 00000000..ff4b187d
--- /dev/null
+++ b/test/monniaux/frame_pointer/Makefile
@@ -0,0 +1,16 @@
+whole : a.o b.o
+ ../../../ccomp $+ -o $@
+
+a.s : a.c
+ k1-cos-gcc -std=c99 -Wall -S $<
+
+b.s : b.c
+ ../../../ccomp -Wall -Wno-c11-extensions -S $<
+
+%.o : %.s
+ k1-cos-gcc -c $<
+
+clean:
+ -rm -f a.o b.o a.s b.s whole
+
+.PHONY: clean
diff --git a/test/monniaux/frame_pointer/a.c b/test/monniaux/frame_pointer/a.c
new file mode 100644
index 00000000..644ccc9b
--- /dev/null
+++ b/test/monniaux/frame_pointer/a.c
@@ -0,0 +1,9 @@
+extern unsigned get_size(void);
+extern void print_array(unsigned n, const int *t);
+
+int main() {
+ unsigned n = get_size();
+ int tab[n];
+ for(unsigned i=0; i<n; i++) tab[i] = i;
+ print_array(n, tab);
+}
diff --git a/test/monniaux/frame_pointer/b.c b/test/monniaux/frame_pointer/b.c
new file mode 100644
index 00000000..dca8fbec
--- /dev/null
+++ b/test/monniaux/frame_pointer/b.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+unsigned get_size(void) {
+ return 12;
+}
+
+void print_array(unsigned n, const int *t) {
+ for(unsigned i=0; i<n; i++) {
+ printf("%d\n", t[i]);
+ }
+}
diff --git a/test/monniaux/genann/Makefile b/test/monniaux/genann/Makefile
new file mode 100644
index 00000000..2e76ec63
--- /dev/null
+++ b/test/monniaux/genann/Makefile
@@ -0,0 +1,4 @@
+ALL_CFILES= example4shorter.c genann.c
+TARGET=genann4
+
+include ../rules.mk
diff --git a/test/monniaux/genann/example/iris.data b/test/monniaux/genann/example/iris.data
new file mode 100644
index 00000000..a3490e0e
--- /dev/null
+++ b/test/monniaux/genann/example/iris.data
@@ -0,0 +1,150 @@
+5.1,3.5,1.4,0.2,Iris-setosa
+4.9,3.0,1.4,0.2,Iris-setosa
+4.7,3.2,1.3,0.2,Iris-setosa
+4.6,3.1,1.5,0.2,Iris-setosa
+5.0,3.6,1.4,0.2,Iris-setosa
+5.4,3.9,1.7,0.4,Iris-setosa
+4.6,3.4,1.4,0.3,Iris-setosa
+5.0,3.4,1.5,0.2,Iris-setosa
+4.4,2.9,1.4,0.2,Iris-setosa
+4.9,3.1,1.5,0.1,Iris-setosa
+5.4,3.7,1.5,0.2,Iris-setosa
+4.8,3.4,1.6,0.2,Iris-setosa
+4.8,3.0,1.4,0.1,Iris-setosa
+4.3,3.0,1.1,0.1,Iris-setosa
+5.8,4.0,1.2,0.2,Iris-setosa
+5.7,4.4,1.5,0.4,Iris-setosa
+5.4,3.9,1.3,0.4,Iris-setosa
+5.1,3.5,1.4,0.3,Iris-setosa
+5.7,3.8,1.7,0.3,Iris-setosa
+5.1,3.8,1.5,0.3,Iris-setosa
+5.4,3.4,1.7,0.2,Iris-setosa
+5.1,3.7,1.5,0.4,Iris-setosa
+4.6,3.6,1.0,0.2,Iris-setosa
+5.1,3.3,1.7,0.5,Iris-setosa
+4.8,3.4,1.9,0.2,Iris-setosa
+5.0,3.0,1.6,0.2,Iris-setosa
+5.0,3.4,1.6,0.4,Iris-setosa
+5.2,3.5,1.5,0.2,Iris-setosa
+5.2,3.4,1.4,0.2,Iris-setosa
+4.7,3.2,1.6,0.2,Iris-setosa
+4.8,3.1,1.6,0.2,Iris-setosa
+5.4,3.4,1.5,0.4,Iris-setosa
+5.2,4.1,1.5,0.1,Iris-setosa
+5.5,4.2,1.4,0.2,Iris-setosa
+4.9,3.1,1.5,0.1,Iris-setosa
+5.0,3.2,1.2,0.2,Iris-setosa
+5.5,3.5,1.3,0.2,Iris-setosa
+4.9,3.1,1.5,0.1,Iris-setosa
+4.4,3.0,1.3,0.2,Iris-setosa
+5.1,3.4,1.5,0.2,Iris-setosa
+5.0,3.5,1.3,0.3,Iris-setosa
+4.5,2.3,1.3,0.3,Iris-setosa
+4.4,3.2,1.3,0.2,Iris-setosa
+5.0,3.5,1.6,0.6,Iris-setosa
+5.1,3.8,1.9,0.4,Iris-setosa
+4.8,3.0,1.4,0.3,Iris-setosa
+5.1,3.8,1.6,0.2,Iris-setosa
+4.6,3.2,1.4,0.2,Iris-setosa
+5.3,3.7,1.5,0.2,Iris-setosa
+5.0,3.3,1.4,0.2,Iris-setosa
+7.0,3.2,4.7,1.4,Iris-versicolor
+6.4,3.2,4.5,1.5,Iris-versicolor
+6.9,3.1,4.9,1.5,Iris-versicolor
+5.5,2.3,4.0,1.3,Iris-versicolor
+6.5,2.8,4.6,1.5,Iris-versicolor
+5.7,2.8,4.5,1.3,Iris-versicolor
+6.3,3.3,4.7,1.6,Iris-versicolor
+4.9,2.4,3.3,1.0,Iris-versicolor
+6.6,2.9,4.6,1.3,Iris-versicolor
+5.2,2.7,3.9,1.4,Iris-versicolor
+5.0,2.0,3.5,1.0,Iris-versicolor
+5.9,3.0,4.2,1.5,Iris-versicolor
+6.0,2.2,4.0,1.0,Iris-versicolor
+6.1,2.9,4.7,1.4,Iris-versicolor
+5.6,2.9,3.6,1.3,Iris-versicolor
+6.7,3.1,4.4,1.4,Iris-versicolor
+5.6,3.0,4.5,1.5,Iris-versicolor
+5.8,2.7,4.1,1.0,Iris-versicolor
+6.2,2.2,4.5,1.5,Iris-versicolor
+5.6,2.5,3.9,1.1,Iris-versicolor
+5.9,3.2,4.8,1.8,Iris-versicolor
+6.1,2.8,4.0,1.3,Iris-versicolor
+6.3,2.5,4.9,1.5,Iris-versicolor
+6.1,2.8,4.7,1.2,Iris-versicolor
+6.4,2.9,4.3,1.3,Iris-versicolor
+6.6,3.0,4.4,1.4,Iris-versicolor
+6.8,2.8,4.8,1.4,Iris-versicolor
+6.7,3.0,5.0,1.7,Iris-versicolor
+6.0,2.9,4.5,1.5,Iris-versicolor
+5.7,2.6,3.5,1.0,Iris-versicolor
+5.5,2.4,3.8,1.1,Iris-versicolor
+5.5,2.4,3.7,1.0,Iris-versicolor
+5.8,2.7,3.9,1.2,Iris-versicolor
+6.0,2.7,5.1,1.6,Iris-versicolor
+5.4,3.0,4.5,1.5,Iris-versicolor
+6.0,3.4,4.5,1.6,Iris-versicolor
+6.7,3.1,4.7,1.5,Iris-versicolor
+6.3,2.3,4.4,1.3,Iris-versicolor
+5.6,3.0,4.1,1.3,Iris-versicolor
+5.5,2.5,4.0,1.3,Iris-versicolor
+5.5,2.6,4.4,1.2,Iris-versicolor
+6.1,3.0,4.6,1.4,Iris-versicolor
+5.8,2.6,4.0,1.2,Iris-versicolor
+5.0,2.3,3.3,1.0,Iris-versicolor
+5.6,2.7,4.2,1.3,Iris-versicolor
+5.7,3.0,4.2,1.2,Iris-versicolor
+5.7,2.9,4.2,1.3,Iris-versicolor
+6.2,2.9,4.3,1.3,Iris-versicolor
+5.1,2.5,3.0,1.1,Iris-versicolor
+5.7,2.8,4.1,1.3,Iris-versicolor
+6.3,3.3,6.0,2.5,Iris-virginica
+5.8,2.7,5.1,1.9,Iris-virginica
+7.1,3.0,5.9,2.1,Iris-virginica
+6.3,2.9,5.6,1.8,Iris-virginica
+6.5,3.0,5.8,2.2,Iris-virginica
+7.6,3.0,6.6,2.1,Iris-virginica
+4.9,2.5,4.5,1.7,Iris-virginica
+7.3,2.9,6.3,1.8,Iris-virginica
+6.7,2.5,5.8,1.8,Iris-virginica
+7.2,3.6,6.1,2.5,Iris-virginica
+6.5,3.2,5.1,2.0,Iris-virginica
+6.4,2.7,5.3,1.9,Iris-virginica
+6.8,3.0,5.5,2.1,Iris-virginica
+5.7,2.5,5.0,2.0,Iris-virginica
+5.8,2.8,5.1,2.4,Iris-virginica
+6.4,3.2,5.3,2.3,Iris-virginica
+6.5,3.0,5.5,1.8,Iris-virginica
+7.7,3.8,6.7,2.2,Iris-virginica
+7.7,2.6,6.9,2.3,Iris-virginica
+6.0,2.2,5.0,1.5,Iris-virginica
+6.9,3.2,5.7,2.3,Iris-virginica
+5.6,2.8,4.9,2.0,Iris-virginica
+7.7,2.8,6.7,2.0,Iris-virginica
+6.3,2.7,4.9,1.8,Iris-virginica
+6.7,3.3,5.7,2.1,Iris-virginica
+7.2,3.2,6.0,1.8,Iris-virginica
+6.2,2.8,4.8,1.8,Iris-virginica
+6.1,3.0,4.9,1.8,Iris-virginica
+6.4,2.8,5.6,2.1,Iris-virginica
+7.2,3.0,5.8,1.6,Iris-virginica
+7.4,2.8,6.1,1.9,Iris-virginica
+7.9,3.8,6.4,2.0,Iris-virginica
+6.4,2.8,5.6,2.2,Iris-virginica
+6.3,2.8,5.1,1.5,Iris-virginica
+6.1,2.6,5.6,1.4,Iris-virginica
+7.7,3.0,6.1,2.3,Iris-virginica
+6.3,3.4,5.6,2.4,Iris-virginica
+6.4,3.1,5.5,1.8,Iris-virginica
+6.0,3.0,4.8,1.8,Iris-virginica
+6.9,3.1,5.4,2.1,Iris-virginica
+6.7,3.1,5.6,2.4,Iris-virginica
+6.9,3.1,5.1,2.3,Iris-virginica
+5.8,2.7,5.1,1.9,Iris-virginica
+6.8,3.2,5.9,2.3,Iris-virginica
+6.7,3.3,5.7,2.5,Iris-virginica
+6.7,3.0,5.2,2.3,Iris-virginica
+6.3,2.5,5.0,1.9,Iris-virginica
+6.5,3.0,5.2,2.0,Iris-virginica
+6.2,3.4,5.4,2.3,Iris-virginica
+5.9,3.0,5.1,1.8,Iris-virginica
diff --git a/test/monniaux/genann/example4shorter.c b/test/monniaux/genann/example4shorter.c
new file mode 100644
index 00000000..ff4ce402
--- /dev/null
+++ b/test/monniaux/genann/example4shorter.c
@@ -0,0 +1,141 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <math.h>
+#include "genann.h"
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+/* This example is to illustrate how to use GENANN.
+ * It is NOT an example of good machine learning techniques.
+ */
+
+const char *iris_data = "example/iris.data";
+
+double *input, *class;
+int samples;
+const char *class_names[] = {"Iris-setosa", "Iris-versicolor", "Iris-virginica"};
+
+void load_data() {
+ /* Load the iris data-set. */
+ FILE *in = fopen("example/iris.data", "r");
+ if (!in) {
+ printf("Could not open file: %s\n", iris_data);
+ exit(1);
+ }
+
+ /* Loop through the data to get a count. */
+ char line[1024];
+ while (!feof(in) && fgets(line, 1024, in)) {
+ ++samples;
+ }
+ fseek(in, 0, SEEK_SET);
+
+ printf("Loading %d data points from %s\n", samples, iris_data);
+
+ /* Allocate memory for input and output data. */
+ input = malloc(sizeof(double) * samples * 4);
+ class = malloc(sizeof(double) * samples * 3);
+
+ /* Read the file into our arrays. */
+ int i, j;
+ for (i = 0; i < samples; ++i) {
+ double *p = input + i * 4;
+ double *c = class + i * 3;
+ c[0] = c[1] = c[2] = 0.0;
+
+ if (fgets(line, 1024, in) == NULL) {
+ perror("fgets");
+ exit(1);
+ }
+
+ char *split = strtok(line, ",");
+ for (j = 0; j < 4; ++j) {
+ p[j] = atof(split);
+ split = strtok(0, ",");
+ }
+
+ split[strlen(split)-1] = 0;
+ if (strcmp(split, class_names[0]) == 0) {c[0] = 1.0;}
+ else if (strcmp(split, class_names[1]) == 0) {c[1] = 1.0;}
+ else if (strcmp(split, class_names[2]) == 0) {c[2] = 1.0;}
+ else {
+ printf("Unknown class %s.\n", split);
+ exit(1);
+ }
+
+ /* printf("Data point %d is %f %f %f %f -> %f %f %f\n", i, p[0], p[1], p[2], p[3], c[0], c[1], c[2]); */
+ }
+
+ fclose(in);
+}
+
+
+int main(int argc, char *argv[])
+{
+ printf("GENANN example 4.\n");
+ printf("Train an ANN on the IRIS dataset using backpropagation.\n");
+
+#ifdef VERIMAG
+ srand(42);
+#else
+ srand(time(0));
+#endif
+
+ /* Load the data from file. */
+ load_data();
+
+ /* 4 inputs.
+ * 1 hidden layer(s) of 4 neurons.
+ * 3 outputs (1 per class)
+ */
+ genann *ann = genann_init(4, 1, 4, 3);
+
+ int i, j;
+#ifdef VERIMAG
+ int loops = 500;
+#else
+ int loops = 5000;
+#endif
+
+ /* Train the network with backpropagation. */
+ printf("Training for %d loops over data.\n", loops);
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ for (i = 0; i < loops; ++i) {
+ for (j = 0; j < samples; ++j) {
+ genann_train(ann, input + j*4, class + j*3, .01);
+ }
+ /* printf("%1.2f ", xor_score(ann)); */
+ }
+
+ int correct = 0;
+ for (j = 0; j < samples; ++j) {
+ const double *guess = genann_run(ann, input + j*4);
+ if (class[j*3+0] == 1.0) {if (guess[0] > guess[1] && guess[0] > guess[2]) ++correct;}
+ else if (class[j*3+1] == 1.0) {if (guess[1] > guess[0] && guess[1] > guess[2]) ++correct;}
+ else if (class[j*3+2] == 1.0) {if (guess[2] > guess[0] && guess[2] > guess[1]) ++correct;}
+ else {printf("Logic error.\n"); exit(1);}
+ }
+#ifdef VERIMAG
+ clock_stop();
+#endif
+
+ printf("%d/%d correct (%0.1f%%).\n", correct, samples, (double)correct / samples * 100.0);
+
+#ifdef VERIMAG
+ print_total_clock();
+#endif
+
+ genann_free(ann);
+ free(input);
+ free(class);
+
+ return 0;
+}
diff --git a/test/monniaux/genann/genann.c b/test/monniaux/genann/genann.c
new file mode 100644
index 00000000..98af736b
--- /dev/null
+++ b/test/monniaux/genann/genann.c
@@ -0,0 +1,415 @@
+/*
+ * GENANN - Minimal C Artificial Neural Network
+ *
+ * Copyright (c) 2015-2018 Lewis Van Winkle
+ *
+ * http://CodePlea.com
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgement in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ */
+
+#define VERIMAG
+
+#include "genann.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef genann_act
+#define genann_act_hidden genann_act_hidden_indirect
+#define genann_act_output genann_act_output_indirect
+#else
+#define genann_act_hidden genann_act
+#define genann_act_output genann_act
+#endif
+
+#define LOOKUP_SIZE 4096
+
+double genann_act_hidden_indirect(const struct genann *ann, double a) {
+ return ann->activation_hidden(ann, a);
+}
+
+double genann_act_output_indirect(const struct genann *ann, double a) {
+ return ann->activation_output(ann, a);
+}
+
+const double sigmoid_dom_min = -15.0;
+const double sigmoid_dom_max = 15.0;
+double interval;
+double lookup[LOOKUP_SIZE];
+
+#ifdef __GNUC__
+#define likely(x) __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#define unused __attribute__((unused))
+#else
+#define likely(x) x
+#define unlikely(x) x
+#define unused
+#pragma warning(disable : 4996) /* For fscanf */
+#endif
+
+
+double static inline genann_act_sigmoid(const genann *ann unused, double a) {
+ if (a < -45.0) return 0;
+ if (a > 45.0) return 1;
+ return 1.0 / (1 + exp(-a));
+}
+
+void genann_init_sigmoid_lookup(const genann *ann) {
+ const double f = (sigmoid_dom_max - sigmoid_dom_min)
+#ifdef VERIMAG
+ * 0x1.0p-12
+#else
+ / LOOKUP_SIZE
+#endif
+ ;
+ int i;
+
+ interval = LOOKUP_SIZE / (sigmoid_dom_max - sigmoid_dom_min);
+ for (i = 0; i < LOOKUP_SIZE; ++i) {
+ lookup[i] = genann_act_sigmoid(ann, sigmoid_dom_min + f * i);
+ }
+}
+
+double static inline genann_act_sigmoid_cached(const genann *ann unused, double a) {
+#ifndef VERIMAG
+ assert(!isnan(a));
+#endif
+
+ if (a < sigmoid_dom_min) return lookup[0];
+ if (a >= sigmoid_dom_max) return lookup[LOOKUP_SIZE - 1];
+
+ size_t j = (size_t)((a-sigmoid_dom_min)*interval+0.5);
+
+ /* Because floating point... */
+ if (unlikely(j >= LOOKUP_SIZE)) return lookup[LOOKUP_SIZE - 1];
+
+ return lookup[j];
+}
+
+double static inline genann_act_linear(const struct genann *ann unused, double a) {
+ return a;
+}
+
+double static inline genann_act_threshold(const struct genann *ann unused, double a) {
+ return a > 0;
+}
+
+genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs) {
+ if (hidden_layers < 0) return 0;
+ if (inputs < 1) return 0;
+ if (outputs < 1) return 0;
+ if (hidden_layers > 0 && hidden < 1) return 0;
+
+
+ const int hidden_weights = hidden_layers ? (inputs+1) * hidden + (hidden_layers-1) * (hidden+1) * hidden : 0;
+ const int output_weights = (hidden_layers ? (hidden+1) : (inputs+1)) * outputs;
+ const int total_weights = (hidden_weights + output_weights);
+
+ const int total_neurons = (inputs + hidden * hidden_layers + outputs);
+
+ /* Allocate extra size for weights, outputs, and deltas. */
+ const int size = sizeof(genann) + sizeof(double) * (total_weights + total_neurons + (total_neurons - inputs));
+ genann *ret = malloc(size);
+ if (!ret) return 0;
+
+ ret->inputs = inputs;
+ ret->hidden_layers = hidden_layers;
+ ret->hidden = hidden;
+ ret->outputs = outputs;
+
+ ret->total_weights = total_weights;
+ ret->total_neurons = total_neurons;
+
+ /* Set pointers. */
+ ret->weight = (double*)((char*)ret + sizeof(genann));
+ ret->output = ret->weight + ret->total_weights;
+ ret->delta = ret->output + ret->total_neurons;
+
+ genann_randomize(ret);
+
+ ret->activation_hidden = genann_act_sigmoid_cached;
+ ret->activation_output = genann_act_sigmoid_cached;
+
+ genann_init_sigmoid_lookup(ret);
+
+ return ret;
+}
+
+
+genann *genann_read(FILE *in) {
+ int inputs, hidden_layers, hidden, outputs;
+ int rc;
+
+ errno = 0;
+ rc = fscanf(in, "%d %d %d %d", &inputs, &hidden_layers, &hidden, &outputs);
+ if (rc < 4 || errno != 0) {
+ perror("fscanf");
+ return NULL;
+ }
+
+ genann *ann = genann_init(inputs, hidden_layers, hidden, outputs);
+
+ int i;
+ for (i = 0; i < ann->total_weights; ++i) {
+ errno = 0;
+ rc = fscanf(in, " %le", ann->weight + i);
+ if (rc < 1 || errno != 0) {
+ perror("fscanf");
+ genann_free(ann);
+
+ return NULL;
+ }
+ }
+
+ return ann;
+}
+
+
+genann *genann_copy(genann const *ann) {
+ const int size = sizeof(genann) + sizeof(double) * (ann->total_weights + ann->total_neurons + (ann->total_neurons - ann->inputs));
+ genann *ret = malloc(size);
+ if (!ret) return 0;
+
+ memcpy(ret, ann, size);
+
+ /* Set pointers. */
+ ret->weight = (double*)((char*)ret + sizeof(genann));
+ ret->output = ret->weight + ret->total_weights;
+ ret->delta = ret->output + ret->total_neurons;
+
+ return ret;
+}
+
+
+void genann_randomize(genann *ann) {
+ int i;
+ for (i = 0; i < ann->total_weights; ++i) {
+ double r = GENANN_RANDOM();
+ /* Sets weights from -0.5 to 0.5. */
+ ann->weight[i] = r - 0.5;
+ }
+}
+
+
+void genann_free(genann *ann) {
+ /* The weight, output, and delta pointers go to the same buffer. */
+ free(ann);
+}
+
+
+double const *genann_run(genann const *ann, double const *inputs) {
+ double const *w = ann->weight;
+ double *o = ann->output + ann->inputs;
+ double const *i = ann->output;
+
+ /* Copy the inputs to the scratch area, where we also store each neuron's
+ * output, for consistency. This way the first layer isn't a special case. */
+ memcpy(ann->output, inputs, sizeof(double) * ann->inputs);
+
+ int h, j, k;
+
+ if (!ann->hidden_layers) {
+ double *ret = o;
+ for (j = 0; j < ann->outputs; ++j) {
+ double sum = *w++ * -1.0;
+ for (k = 0; k < ann->inputs; ++k) {
+ sum += *w++ * i[k];
+ }
+ *o++ = genann_act_output(ann, sum);
+ }
+
+ return ret;
+ }
+
+ /* Figure input layer */
+ for (j = 0; j < ann->hidden; ++j) {
+ double sum = *w++ * -1.0;
+ for (k = 0; k < ann->inputs; ++k) {
+ sum += *w++ * i[k];
+ }
+ *o++ = genann_act_hidden(ann, sum);
+ }
+
+ i += ann->inputs;
+
+ /* Figure hidden layers, if any. */
+ for (h = 1; h < ann->hidden_layers; ++h) {
+ for (j = 0; j < ann->hidden; ++j) {
+ double sum = *w++ * -1.0;
+ for (k = 0; k < ann->hidden; ++k) {
+ sum += *w++ * i[k];
+ }
+ *o++ = genann_act_hidden(ann, sum);
+ }
+
+ i += ann->hidden;
+ }
+
+ double const *ret = o;
+
+ /* Figure output layer. */
+ for (j = 0; j < ann->outputs; ++j) {
+ double sum = *w++ * -1.0;
+ for (k = 0; k < ann->hidden; ++k) {
+ sum += *w++ * i[k];
+ }
+ *o++ = genann_act_output(ann, sum);
+ }
+
+ /* Sanity check that we used all weights and wrote all outputs. */
+ assert(w - ann->weight == ann->total_weights);
+ assert(o - ann->output == ann->total_neurons);
+
+ return ret;
+}
+
+
+void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate) {
+ /* To begin with, we must run the network forward. */
+ genann_run(ann, inputs);
+
+ int h, j, k;
+
+ /* First set the output layer deltas. */
+ {
+ double const *o = ann->output + ann->inputs + ann->hidden * ann->hidden_layers; /* First output. */
+ double *d = ann->delta + ann->hidden * ann->hidden_layers; /* First delta. */
+ double const *t = desired_outputs; /* First desired output. */
+
+
+ /* Set output layer deltas. */
+ if (genann_act_output == genann_act_linear ||
+ ann->activation_output == genann_act_linear) {
+ for (j = 0; j < ann->outputs; ++j) {
+ *d++ = *t++ - *o++;
+ }
+ } else {
+ for (j = 0; j < ann->outputs; ++j) {
+ *d++ = (*t - *o) * *o * (1.0 - *o);
+ ++o; ++t;
+ }
+ }
+ }
+
+
+ /* Set hidden layer deltas, start on last layer and work backwards. */
+ /* Note that loop is skipped in the case of hidden_layers == 0. */
+ for (h = ann->hidden_layers - 1; h >= 0; --h) {
+
+ /* Find first output and delta in this layer. */
+ double const *o = ann->output + ann->inputs + (h * ann->hidden);
+ double *d = ann->delta + (h * ann->hidden);
+
+ /* Find first delta in following layer (which may be hidden or output). */
+ double const * const dd = ann->delta + ((h+1) * ann->hidden);
+
+ /* Find first weight in following layer (which may be hidden or output). */
+ double const * const ww = ann->weight + ((ann->inputs+1) * ann->hidden) + ((ann->hidden+1) * ann->hidden * (h));
+
+ for (j = 0; j < ann->hidden; ++j) {
+
+ double delta = 0;
+
+ for (k = 0; k < (h == ann->hidden_layers-1 ? ann->outputs : ann->hidden); ++k) {
+ const double forward_delta = dd[k];
+ const int windex = k * (ann->hidden + 1) + (j + 1);
+ const double forward_weight = ww[windex];
+ delta += forward_delta * forward_weight;
+ }
+
+ *d = *o * (1.0-*o) * delta;
+ ++d; ++o;
+ }
+ }
+
+
+ /* Train the outputs. */
+ {
+ /* Find first output delta. */
+ double const *d = ann->delta + ann->hidden * ann->hidden_layers; /* First output delta. */
+
+ /* Find first weight to first output delta. */
+ double *w = ann->weight + (ann->hidden_layers
+ ? ((ann->inputs+1) * ann->hidden + (ann->hidden+1) * ann->hidden * (ann->hidden_layers-1))
+ : (0));
+
+ /* Find first output in previous layer. */
+ double const * const i = ann->output + (ann->hidden_layers
+ ? (ann->inputs + (ann->hidden) * (ann->hidden_layers-1))
+ : 0);
+
+ /* Set output layer weights. */
+ for (j = 0; j < ann->outputs; ++j) {
+ *w++ += *d * learning_rate * -1.0;
+ for (k = 1; k < (ann->hidden_layers ? ann->hidden : ann->inputs) + 1; ++k) {
+ *w++ += *d * learning_rate * i[k-1];
+ }
+
+ ++d;
+ }
+
+ assert(w - ann->weight == ann->total_weights);
+ }
+
+
+ /* Train the hidden layers. */
+ for (h = ann->hidden_layers - 1; h >= 0; --h) {
+
+ /* Find first delta in this layer. */
+ double const *d = ann->delta + (h * ann->hidden);
+
+ /* Find first input to this layer. */
+ double const *i = ann->output + (h
+ ? (ann->inputs + ann->hidden * (h-1))
+ : 0);
+
+ /* Find first weight to this layer. */
+ double *w = ann->weight + (h
+ ? ((ann->inputs+1) * ann->hidden + (ann->hidden+1) * (ann->hidden) * (h-1))
+ : 0);
+
+
+ for (j = 0; j < ann->hidden; ++j) {
+ *w++ += *d * learning_rate * -1.0;
+ for (k = 1; k < (h == 0 ? ann->inputs : ann->hidden) + 1; ++k) {
+ *w++ += *d * learning_rate * i[k-1];
+ }
+ ++d;
+ }
+
+ }
+
+}
+
+
+void genann_write(genann const *ann, FILE *out) {
+ fprintf(out, "%d %d %d %d", ann->inputs, ann->hidden_layers, ann->hidden, ann->outputs);
+
+ int i;
+ for (i = 0; i < ann->total_weights; ++i) {
+ fprintf(out, " %.20e", ann->weight[i]);
+ }
+}
+
+
diff --git a/test/monniaux/genann/genann.h b/test/monniaux/genann/genann.h
new file mode 100644
index 00000000..7eeb1cdc
--- /dev/null
+++ b/test/monniaux/genann/genann.h
@@ -0,0 +1,109 @@
+/*
+ * GENANN - Minimal C Artificial Neural Network
+ *
+ * Copyright (c) 2015-2018 Lewis Van Winkle
+ *
+ * http://CodePlea.com
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgement in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ */
+
+
+#ifndef GENANN_H
+#define GENANN_H
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef GENANN_RANDOM
+/* We use the following for uniform random numbers between 0 and 1.
+ * If you have a better function, redefine this macro. */
+#define GENANN_RANDOM() (((double)rand())/RAND_MAX)
+#endif
+
+struct genann;
+
+typedef double (*genann_actfun)(const struct genann *ann, double a);
+
+typedef struct genann {
+ /* How many inputs, outputs, and hidden neurons. */
+ int inputs, hidden_layers, hidden, outputs;
+
+ /* Which activation function to use for hidden neurons. Default: gennann_act_sigmoid_cached*/
+ genann_actfun activation_hidden;
+
+ /* Which activation function to use for output. Default: gennann_act_sigmoid_cached*/
+ genann_actfun activation_output;
+
+ /* Total number of weights, and size of weights buffer. */
+ int total_weights;
+
+ /* Total number of neurons + inputs and size of output buffer. */
+ int total_neurons;
+
+ /* All weights (total_weights long). */
+ double *weight;
+
+ /* Stores input array and output of each neuron (total_neurons long). */
+ double *output;
+
+ /* Stores delta of each hidden and output neuron (total_neurons - inputs long). */
+ double *delta;
+
+} genann;
+
+/* Creates and returns a new ann. */
+genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);
+
+/* Creates ANN from file saved with genann_write. */
+genann *genann_read(FILE *in);
+
+/* Sets weights randomly. Called by init. */
+void genann_randomize(genann *ann);
+
+/* Returns a new copy of ann. */
+genann *genann_copy(genann const *ann);
+
+/* Frees the memory used by an ann. */
+void genann_free(genann *ann);
+
+/* Runs the feedforward algorithm to calculate the ann's output. */
+double const *genann_run(genann const *ann, double const *inputs);
+
+/* Does a single backprop update. */
+void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate);
+
+/* Saves the ann. */
+void genann_write(genann const *ann, FILE *out);
+
+void genann_init_sigmoid_lookup(const genann *ann);
+#ifndef VERIMAG
+double genann_act_sigmoid(const genann *ann, double a);
+double genann_act_sigmoid_cached(const genann *ann, double a);
+double genann_act_threshold(const genann *ann, double a);
+double genann_act_linear(const genann *ann, double a);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*GENANN_H*/
diff --git a/test/monniaux/glibc_qsort/Makefile b/test/monniaux/glibc_qsort/Makefile
new file mode 100644
index 00000000..ca029339
--- /dev/null
+++ b/test/monniaux/glibc_qsort/Makefile
@@ -0,0 +1,3 @@
+TARGET=glibc_qsort
+
+include ../rules.mk
diff --git a/test/monniaux/glibc_qsort/glibc_qsort.c b/test/monniaux/glibc_qsort/glibc_qsort.c
new file mode 100644
index 00000000..6dc64ab6
--- /dev/null
+++ b/test/monniaux/glibc_qsort/glibc_qsort.c
@@ -0,0 +1,209 @@
+/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+/* If you consider tuning this algorithm, you should consult first:
+ Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
+ Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993. */
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include "glibc_qsort.h"
+
+/* Byte-wise swap two items of size SIZE. */
+#define SWAP(a, b, size) \
+ do \
+ { \
+ size_t __size = (size); \
+ char *__a = (a), *__b = (b); \
+ do \
+ { \
+ char __tmp = *__a; \
+ *__a++ = *__b; \
+ *__b++ = __tmp; \
+ } while (--__size > 0); \
+ } while (0)
+/* Discontinue quicksort algorithm when partition gets below this size.
+ This particular magic number was chosen to work best on a Sun 4/260. */
+#define MAX_THRESH 4
+/* Stack node declarations used to store unfulfilled partition obligations. */
+typedef struct
+ {
+ char *lo;
+ char *hi;
+ } stack_node;
+/* The next 4 #defines implement a very fast in-line stack abstraction. */
+/* The stack needs log (total_elements) entries (we could even subtract
+ log(MAX_THRESH)). Since total_elements has type size_t, we get as
+ upper bound for log (total_elements):
+ bits per byte (CHAR_BIT) * sizeof(size_t). */
+#define STACK_SIZE (CHAR_BIT * sizeof(size_t))
+#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
+#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
+#define STACK_NOT_EMPTY (stack < top)
+/* Order size using quicksort. This implementation incorporates
+ four optimizations discussed in Sedgewick:
+ 1. Non-recursive, using an explicit stack of pointer that store the
+ next array partition to sort. To save time, this maximum amount
+ of space required to store an array of SIZE_MAX is allocated on the
+ stack. Assuming a 32-bit (64 bit) integer for size_t, this needs
+ only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
+ Pretty cheap, actually.
+ 2. Chose the pivot element using a median-of-three decision tree.
+ This reduces the probability of selecting a bad pivot value and
+ eliminates certain extraneous comparisons.
+ 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
+ insertion sort to order the MAX_THRESH items within each partition.
+ This is a big win, since insertion sort is faster for small, mostly
+ sorted array segments.
+ 4. The larger of the two sub-partitions is always pushed onto the
+ stack first, with the algorithm then concentrating on the
+ smaller partition. This *guarantees* no more than log (total_elems)
+ stack size is needed (actually O(1) in this case)! */
+
+void quicksort (void *const pbase, size_t total_elems, size_t size,
+ comparison *cmp, void *arg)
+{
+ char *base_ptr = (char *) pbase;
+ const size_t max_thresh = MAX_THRESH * size;
+ if (total_elems == 0)
+ /* Avoid lossage with unsigned arithmetic below. */
+ return;
+ if (total_elems > MAX_THRESH)
+ {
+ char *lo = base_ptr;
+ char *hi = &lo[size * (total_elems - 1)];
+ stack_node stack[STACK_SIZE];
+ stack_node *top = stack;
+ PUSH (NULL, NULL);
+ while (STACK_NOT_EMPTY)
+ {
+ char *left_ptr;
+ char *right_ptr;
+ /* Select median value from among LO, MID, and HI. Rearrange
+ LO and HI so the three values are sorted. This lowers the
+ probability of picking a pathological pivot value and
+ skips a comparison for both the LEFT_PTR and RIGHT_PTR in
+ the while loops. */
+ char *mid = lo + size * ((hi - lo) / size >> 1);
+ if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
+ SWAP (mid, lo, size);
+ if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
+ SWAP (mid, hi, size);
+ else
+ goto jump_over;
+ if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
+ SWAP (mid, lo, size);
+ jump_over:;
+ left_ptr = lo + size;
+ right_ptr = hi - size;
+ /* Here's the famous ``collapse the walls'' section of quicksort.
+ Gotta like those tight inner loops! They are the main reason
+ that this algorithm runs much faster than others. */
+ do
+ {
+ while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
+ left_ptr += size;
+ while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
+ right_ptr -= size;
+ if (left_ptr < right_ptr)
+ {
+ SWAP (left_ptr, right_ptr, size);
+ if (mid == left_ptr)
+ mid = right_ptr;
+ else if (mid == right_ptr)
+ mid = left_ptr;
+ left_ptr += size;
+ right_ptr -= size;
+ }
+ else if (left_ptr == right_ptr)
+ {
+ left_ptr += size;
+ right_ptr -= size;
+ break;
+ }
+ }
+ while (left_ptr <= right_ptr);
+ /* Set up pointers for next iteration. First determine whether
+ left and right partitions are below the threshold size. If so,
+ ignore one or both. Otherwise, push the larger partition's
+ bounds on the stack and continue sorting the smaller one. */
+ if ((size_t) (right_ptr - lo) <= max_thresh)
+ {
+ if ((size_t) (hi - left_ptr) <= max_thresh)
+ /* Ignore both small partitions. */
+ POP (lo, hi);
+ else
+ /* Ignore small left partition. */
+ lo = left_ptr;
+ }
+ else if ((size_t) (hi - left_ptr) <= max_thresh)
+ /* Ignore small right partition. */
+ hi = right_ptr;
+ else if ((right_ptr - lo) > (hi - left_ptr))
+ {
+ /* Push larger left partition indices. */
+ PUSH (lo, right_ptr);
+ lo = left_ptr;
+ }
+ else
+ {
+ /* Push larger right partition indices. */
+ PUSH (left_ptr, hi);
+ hi = right_ptr;
+ }
+ }
+ }
+ /* Once the BASE_PTR array is partially sorted by quicksort the rest
+ is completely sorted using insertion sort, since this is efficient
+ for partitions below MAX_THRESH size. BASE_PTR points to the beginning
+ of the array to sort, and END_PTR points at the very last element in
+ the array (*not* one beyond it!). */
+#define min(x, y) ((x) < (y) ? (x) : (y))
+ {
+ char *const end_ptr = &base_ptr[size * (total_elems - 1)];
+ char *tmp_ptr = base_ptr;
+ char *thresh = min(end_ptr, base_ptr + max_thresh);
+ char *run_ptr;
+ /* Find smallest element in first threshold and place it at the
+ array's beginning. This is the smallest array element,
+ and the operation speeds up insertion sort's inner loop. */
+ for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
+ if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
+ tmp_ptr = run_ptr;
+ if (tmp_ptr != base_ptr)
+ SWAP (tmp_ptr, base_ptr, size);
+ /* Insertion sort, running from left-hand-side up to right-hand-side. */
+ run_ptr = base_ptr + size;
+ while ((run_ptr += size) <= end_ptr)
+ {
+ tmp_ptr = run_ptr - size;
+ while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
+ tmp_ptr -= size;
+ tmp_ptr += size;
+ if (tmp_ptr != run_ptr)
+ {
+ char *trav;
+ trav = run_ptr + size;
+ while (--trav >= run_ptr)
+ {
+ char c = *trav;
+ char *hi, *lo;
+ for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
+ *hi = *lo;
+ *hi = c;
+ }
+ }
+ }
+ }
+}
diff --git a/test/monniaux/glibc_qsort/glibc_qsort.h b/test/monniaux/glibc_qsort/glibc_qsort.h
new file mode 100644
index 00000000..98719b6c
--- /dev/null
+++ b/test/monniaux/glibc_qsort/glibc_qsort.h
@@ -0,0 +1,4 @@
+typedef int comparison(const void *, const void *, void *);
+
+void quicksort (void *const pbase, size_t total_elems, size_t size,
+ comparison *cmp, void *arg);
diff --git a/test/monniaux/glibc_qsort/glibc_qsort_run.c b/test/monniaux/glibc_qsort/glibc_qsort_run.c
new file mode 100644
index 00000000..fee5a2ff
--- /dev/null
+++ b/test/monniaux/glibc_qsort/glibc_qsort_run.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include "glibc_qsort.h"
+#include "../cycles.h"
+
+typedef uint64_t data;
+
+static data data_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next;
+}
+
+static void data_vec_random(data *a, unsigned len) {
+ for(unsigned i=0; i<len; i++) {
+ a[i] = data_random();
+ }
+}
+
+static bool data_vec_is_sorted(const data *a, unsigned len) {
+ for(unsigned i=0; i<len-1; i++) {
+ if (a[i] > a[i+1]) return false;
+ }
+ return true;
+}
+
+static int data_compare(const void *px, const void *py, void *dummy) {
+ data x = *((data*) px);
+ data y = *((data*) py);
+ return x < y ? -1 : (x > y ? 1 : 0);
+}
+
+int main (void) {
+ cycle_count_config();
+ unsigned len=3000;
+ data *vec = malloc(sizeof(data) * len);
+ data_vec_random(vec, len);
+ cycle_t quicksort_time = get_cycle();
+ quicksort(vec, len, sizeof(data), data_compare, NULL);
+ quicksort_time = get_cycle() - quicksort_time;
+ printf("sorted=%s\n"
+ "time cycles:%" PRIu64 "\n",
+ data_vec_is_sorted(vec, len)?"true":"false",
+ quicksort_time);
+ free(vec);
+ return 0;
+}
diff --git a/test/monniaux/glibc_qsort/lgpl-2.1.txt b/test/monniaux/glibc_qsort/lgpl-2.1.txt
new file mode 100644
index 00000000..4362b491
--- /dev/null
+++ b/test/monniaux/glibc_qsort/lgpl-2.1.txt
@@ -0,0 +1,502 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/test/monniaux/glpk-4.65/Makefile b/test/monniaux/glpk-4.65/Makefile
new file mode 100644
index 00000000..eaa3f4b0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/Makefile
@@ -0,0 +1,6 @@
+ALL_CFLAGS += -I src/amd -I src/colamd -I src/mpl -I src/simplex -I src/api -I src/intopt -I src/minisat -I src/npp -I src/zlib -I src/bflib -I src/env -I src/misc -I src/draft -I src
+ALL_CFILES=examples/glpsol.c $(wildcard src/*/*.c)
+TARGET=glpk
+EXECUTE_ARGS=--math examples/prod.mod
+
+include ../rules.mk
diff --git a/test/monniaux/glpk-4.65/config.h b/test/monniaux/glpk-4.65/config.h
new file mode 100644
index 00000000..35a44f00
--- /dev/null
+++ b/test/monniaux/glpk-4.65/config.h
@@ -0,0 +1,31 @@
+/* config.h. Generated from config.h.in by configure. */
+/* config.h.in (GLPK configuration template file) */
+
+#define HAVE_SYS_TIME_H 1
+/* defined if the <sys/time.h> header can be used */
+
+#define HAVE_GETTIMEOFDAY 1
+/* defined if the gettimeofday function can be used */
+
+/* #undef HAVE_GMP */
+/* defined if the GNU MP bignum library is available */
+/* requires <gmp.h> and -lgmp */
+
+/* #undef HAVE_LTDL */
+/* defined if the GNU Libtool shared library support is enabled */
+/* requires <ltdl.h> and -lltdl */
+
+/* #undef HAVE_DLFCN */
+/* defined if the POSIX shared library support is enabled */
+/* requires <dlfcn.h> */
+
+/* #undef ODBC_DLNAME */
+/* ODBC shared library name if this feature is enabled */
+
+/* #undef MYSQL_DLNAME */
+/* MySQL shared library name if this feature is enabled */
+
+/* #undef TLS */
+/* thread local storage-class specifier for re-entrancy (if any) */
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/examples/glpsol.c b/test/monniaux/glpk-4.65/examples/glpsol.c
new file mode 100644
index 00000000..17df3380
--- /dev/null
+++ b/test/monniaux/glpk-4.65/examples/glpsol.c
@@ -0,0 +1,1598 @@
+/* glpsol.c (stand-alone GLPK LP/MIP solver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../../clock.h"
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <ctype.h>
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glpk.h>
+
+#define xassert glp_assert
+#define xerror glp_error
+#define xprintf glp_printf
+
+struct csa
+{ /* common storage area */
+ glp_prob *prob;
+ /* LP/MIP problem object */
+ glp_bfcp bfcp;
+ /* basis factorization control parameters */
+ glp_smcp smcp;
+ /* simplex method control parameters */
+ glp_iptcp iptcp;
+ /* interior-point method control parameters */
+ glp_iocp iocp;
+ /* integer optimizer control parameters */
+ glp_tran *tran;
+ /* model translator workspace */
+ glp_graph *graph;
+ /* network problem object */
+ int format;
+ /* problem file format: */
+#define FMT_MPS_DECK 1 /* fixed MPS */
+#define FMT_MPS_FILE 2 /* free MPS */
+#define FMT_LP 3 /* CPLEX LP */
+#define FMT_GLP 4 /* GLPK LP/MIP */
+#define FMT_MATHPROG 5 /* MathProg */
+#define FMT_MIN_COST 6 /* DIMACS min-cost flow */
+#define FMT_MAX_FLOW 7 /* DIMACS maximum flow */
+#if 1 /* 06/VIII-2011 */
+#define FMT_CNF 8 /* DIMACS CNF-SAT */
+#endif
+ const char *in_file;
+ /* name of input problem file */
+#define DATA_MAX 10
+ /* maximal number of input data files */
+ int ndf;
+ /* number of input data files specified */
+ const char *in_data[1+DATA_MAX];
+ /* name(s) of input data file(s) */
+ const char *out_dpy;
+ /* name of output file to send display output; NULL means the
+ display output is sent to the terminal */
+ int seed;
+ /* seed value to be passed to the MathProg translator; initially
+ set to 1; 0x80000000 means the value is omitted */
+ int solution;
+ /* solution type flag: */
+#define SOL_BASIC 1 /* basic */
+#define SOL_INTERIOR 2 /* interior-point */
+#define SOL_INTEGER 3 /* mixed integer */
+ const char *in_res;
+ /* name of input solution file in raw format */
+ int dir;
+ /* optimization direction flag:
+ 0 - not specified
+ GLP_MIN - minimization
+ GLP_MAX - maximization */
+ int scale;
+ /* automatic problem scaling flag */
+ const char *out_sol;
+ /* name of output solution file in printable format */
+ const char *out_res;
+ /* name of output solution file in raw format */
+ const char *out_ranges;
+ /* name of output file to write sensitivity analysis report */
+ int check;
+ /* input data checking flag; no solution is performed */
+ const char *new_name;
+ /* new name to be assigned to the problem */
+#if 1 /* 18/I-2018 */
+ int hide;
+ /* clear all symbolic names in the problem object */
+#endif
+ const char *out_mps;
+ /* name of output problem file in fixed MPS format */
+ const char *out_freemps;
+ /* name of output problem file in free MPS format */
+ const char *out_cpxlp;
+ /* name of output problem file in CPLEX LP format */
+ const char *out_glp;
+ /* name of output problem file in GLPK format */
+#if 0
+ const char *out_pb;
+ /* name of output problem file in OPB format */
+ const char *out_npb;
+ /* name of output problem file in normalized OPB format */
+#endif
+#if 1 /* 06/VIII-2011 */
+ const char *out_cnf;
+ /* name of output problem file in DIMACS CNF-SAT format */
+#endif
+ const char *log_file;
+ /* name of output file to hardcopy terminal output */
+ int crash;
+ /* initial basis option: */
+#define USE_STD_BASIS 1 /* use standard basis */
+#define USE_ADV_BASIS 2 /* use advanced basis */
+#define USE_CPX_BASIS 3 /* use Bixby's basis */
+#define USE_INI_BASIS 4 /* use initial basis from ini_file */
+ const char *ini_file;
+ /* name of input file containing initial basis */
+ int exact;
+ /* flag to use glp_exact rather than glp_simplex */
+ int xcheck;
+ /* flag to check final basis with glp_exact */
+ int nomip;
+ /* flag to consider MIP as pure LP */
+#if 1 /* 15/VIII-2011 */
+ int minisat;
+ /* option to solve feasibility problem with MiniSat solver */
+ int use_bnd;
+ /* option to bound objective function */
+ int obj_bnd;
+ /* upper (minization) or lower (maximization) objective bound */
+#endif
+#if 1 /* 11/VII-2013 */
+ const char *use_sol;
+ /* name of input mip solution file in GLPK format */
+#endif
+};
+
+static int str2int(const char *s, int *x)
+{ /* convert string to integer */
+ long t;
+ char *endptr;
+ t = strtol(s, &endptr, 10);
+ if (*endptr != '\0')
+ return 2;
+ if (!(INT_MIN <= t && t <= INT_MAX))
+ return 1;
+ *x = t;
+#if 0
+ xprintf("str2int: x = %d\n", *x);
+#endif
+ return 0;
+}
+
+static int str2num(const char *s, double *x)
+{ /* convert string to floating point */
+ double t;
+ char *endptr;
+ t = strtod(s, &endptr);
+ if (*endptr != '\0')
+ return 2;
+ if (!(-DBL_MAX <= t && t <= +DBL_MAX))
+ return 1;
+ *x = t;
+#if 0
+ xprintf("str2num: x = %g\n", *x);
+#endif
+ return 0;
+}
+
+static void print_help(const char *my_name)
+{ /* print help information */
+ xprintf("Usage: %s [options...] filename\n", my_name);
+ xprintf("\n");
+ xprintf("General options:\n");
+ xprintf(" --mps read LP/MIP problem in fixed MPS fo"
+ "rmat\n");
+ xprintf(" --freemps read LP/MIP problem in free MPS for"
+ "mat (default)\n");
+ xprintf(" --lp read LP/MIP problem in CPLEX LP for"
+ "mat\n");
+ xprintf(" --glp read LP/MIP problem in GLPK format "
+ "\n");
+ xprintf(" --math read LP/MIP model written in GNU Ma"
+ "thProg modeling\n");
+ xprintf(" language\n");
+ xprintf(" -m filename, --model filename\n");
+ xprintf(" read model section and optional dat"
+ "a section from\n");
+ xprintf(" filename (same as --math)\n");
+ xprintf(" -d filename, --data filename\n");
+ xprintf(" read data section from filename (fo"
+ "r --math only);\n");
+ xprintf(" if model file also has data section"
+ ", it is ignored\n");
+ xprintf(" -y filename, --display filename\n");
+ xprintf(" send display output to filename (fo"
+ "r --math only);\n");
+ xprintf(" by default the output is sent to te"
+ "rminal\n");
+ xprintf(" --seed value initialize pseudo-random number gen"
+ "erator used in\n");
+ xprintf(" MathProg model with specified seed "
+ "(any integer);\n");
+ xprintf(" if seed value is ?, some random see"
+ "d will be used\n");
+ xprintf(" --mincost read min-cost flow problem in DIMAC"
+ "S format\n");
+ xprintf(" --maxflow read maximum flow problem in DIMACS"
+ " format\n");
+#if 1 /* 06/VIII-2011 */
+ xprintf(" --cnf read CNF-SAT problem in DIMACS form"
+ "at\n");
+#endif
+ xprintf(" --simplex use simplex method (default)\n");
+ xprintf(" --interior use interior point method (LP only)"
+ "\n");
+ xprintf(" -r filename, --read filename\n");
+ xprintf(" read solution from filename rather "
+ "to find it with\n");
+ xprintf(" the solver\n");
+ xprintf(" --min minimization\n");
+ xprintf(" --max maximization\n");
+ xprintf(" --scale scale problem (default)\n");
+ xprintf(" --noscale do not scale problem\n");
+ xprintf(" -o filename, --output filename\n");
+ xprintf(" write solution to filename in print"
+ "able format\n");
+ xprintf(" -w filename, --write filename\n");
+ xprintf(" write solution to filename in plain"
+ " text format\n");
+ xprintf(" --ranges filename\n");
+ xprintf(" write sensitivity analysis report t"
+ "o filename in\n");
+ xprintf(" printable format (simplex only)\n");
+ xprintf(" --tmlim nnn limit solution time to nnn seconds "
+ "\n");
+ xprintf(" --memlim nnn limit available memory to nnn megab"
+ "ytes\n");
+ xprintf(" --check do not solve problem, check input d"
+ "ata only\n");
+ xprintf(" --name probname change problem name to probname\n");
+#if 1 /* 18/I-2018 */
+ xprintf(" --hide remove all symbolic names from prob"
+ "lem object\n");
+#endif
+ xprintf(" --wmps filename write problem to filename in fixed "
+ "MPS format\n");
+ xprintf(" --wfreemps filename\n");
+ xprintf(" write problem to filename in free M"
+ "PS format\n");
+ xprintf(" --wlp filename write problem to filename in CPLEX "
+ "LP format\n");
+ xprintf(" --wglp filename write problem to filename in GLPK f"
+ "ormat\n");
+#if 0
+ xprintf(" --wpb filename write problem to filename in OPB fo"
+ "rmat\n");
+ xprintf(" --wnpb filename write problem to filename in normal"
+ "ized OPB format\n");
+#endif
+#if 1 /* 06/VIII-2011 */
+ xprintf(" --wcnf filename write problem to filename in DIMACS"
+ " CNF-SAT format\n");
+#endif
+ xprintf(" --log filename write copy of terminal output to fi"
+ "lename\n");
+ xprintf(" -h, --help display this help information and e"
+ "xit\n");
+ xprintf(" -v, --version display program version and exit\n")
+ ;
+ xprintf("\n");
+ xprintf("LP basis factorization options:\n");
+#if 0 /* 08/III-2014 */
+ xprintf(" --luf LU + Forrest-Tomlin update\n");
+ xprintf(" (faster, less stable; default)\n");
+ xprintf(" --cbg LU + Schur complement + Bartels-Gol"
+ "ub update\n");
+ xprintf(" (slower, more stable)\n");
+ xprintf(" --cgr LU + Schur complement + Givens rota"
+ "tion update\n");
+ xprintf(" (slower, more stable)\n");
+#else
+ xprintf(" --luf plain LU-factorization (default)\n")
+ ;
+ xprintf(" --btf block triangular LU-factorization\n"
+ );
+ xprintf(" --ft Forrest-Tomlin update (requires --l"
+ "uf; default)\n");
+ xprintf(" --cbg Schur complement + Bartels-Golub up"
+ "date\n");
+ xprintf(" --cgr Schur complement + Givens rotation "
+ "update\n");
+#endif
+ xprintf("\n");
+ xprintf("Options specific to simplex solver:\n");
+ xprintf(" --primal use primal simplex (default)\n");
+ xprintf(" --dual use dual simplex\n");
+ xprintf(" --std use standard initial basis of all s"
+ "lacks\n");
+ xprintf(" --adv use advanced initial basis (default"
+ ")\n");
+ xprintf(" --bib use Bixby's initial basis\n");
+ xprintf(" --ini filename use as initial basis previously sav"
+ "ed with -w\n");
+ xprintf(" (disables LP presolver)\n");
+ xprintf(" --steep use steepest edge technique (defaul"
+ "t)\n");
+ xprintf(" --nosteep use standard \"textbook\" pricing\n"
+ );
+ xprintf(" --relax use Harris' two-pass ratio test (de"
+ "fault)\n");
+ xprintf(" --norelax use standard \"textbook\" ratio tes"
+ "t\n");
+#if 0 /* 23/VI-2017 */
+#if 1 /* 28/III-2016 */
+ xprintf(" --flip use flip-flop ratio test (assumes -"
+ "-dual)\n");
+#endif
+#else
+ /* now this option is implemented in both primal and dual */
+ xprintf(" --flip use long-step ratio test\n");
+#endif
+ xprintf(" --presol use presolver (default; assumes --s"
+ "cale and --adv)\n");
+ xprintf(" --nopresol do not use presolver\n");
+ xprintf(" --exact use simplex method based on exact a"
+ "rithmetic\n");
+ xprintf(" --xcheck check final basis using exact arith"
+ "metic\n");
+ xprintf("\n");
+ xprintf("Options specific to interior-point solver:\n");
+ xprintf(" --nord use natural (original) ordering\n");
+ xprintf(" --qmd use quotient minimum degree orderin"
+ "g\n");
+ xprintf(" --amd use approximate minimum degree orde"
+ "ring (default)\n");
+ xprintf(" --symamd use approximate minimum degree orde"
+ "ring\n");
+ xprintf("\n");
+ xprintf("Options specific to MIP solver:\n");
+ xprintf(" --nomip consider all integer variables as c"
+ "ontinuous\n");
+ xprintf(" (allows solving MIP as pure LP)\n");
+ xprintf(" --first branch on first integer variable\n")
+ ;
+ xprintf(" --last branch on last integer variable\n");
+ xprintf(" --mostf branch on most fractional variable "
+ "\n");
+ xprintf(" --drtom branch using heuristic by Driebeck "
+ "and Tomlin\n");
+ xprintf(" (default)\n");
+ xprintf(" --pcost branch using hybrid pseudocost heur"
+ "istic (may be\n");
+ xprintf(" useful for hard instances)\n");
+ xprintf(" --dfs backtrack using depth first search "
+ "\n");
+ xprintf(" --bfs backtrack using breadth first searc"
+ "h\n");
+ xprintf(" --bestp backtrack using the best projection"
+ " heuristic\n");
+ xprintf(" --bestb backtrack using node with best loca"
+ "l bound\n");
+ xprintf(" (default)\n");
+ xprintf(" --intopt use MIP presolver (default)\n");
+ xprintf(" --nointopt do not use MIP presolver\n");
+ xprintf(" --binarize replace general integer variables b"
+ "y binary ones\n");
+ xprintf(" (assumes --intopt)\n");
+ xprintf(" --fpump apply feasibility pump heuristic\n")
+ ;
+#if 1 /* 29/VI-2013 */
+ xprintf(" --proxy [nnn] apply proximity search heuristic (n"
+ "nn is time limit\n");
+ xprintf(" in seconds; default is 60)\n");
+#endif
+ xprintf(" --gomory generate Gomory's mixed integer cut"
+ "s\n");
+ xprintf(" --mir generate MIR (mixed integer roundin"
+ "g) cuts\n");
+ xprintf(" --cover generate mixed cover cuts\n");
+ xprintf(" --clique generate clique cuts\n");
+ xprintf(" --cuts generate all cuts above\n");
+ xprintf(" --mipgap tol set relative mip gap tolerance to t"
+ "ol\n");
+#if 1 /* 15/VIII-2011 */
+ xprintf(" --minisat translate integer feasibility probl"
+ "em to CNF-SAT\n");
+ xprintf(" and solve it with MiniSat solver\n")
+ ;
+ xprintf(" --objbnd bound add inequality obj <= bound (minimi"
+ "zation) or\n");
+ xprintf(" obj >= bound (maximization) to inte"
+ "ger feasibility\n");
+ xprintf(" problem (assumes --minisat)\n");
+#endif
+ xprintf("\n");
+ xprintf("For description of the MPS and CPLEX LP formats see Refe"
+ "rence Manual.\n");
+ xprintf("For description of the modeling language see \"GLPK: Mod"
+ "eling Language\n");
+ xprintf("GNU MathProg\". Both documents are included in the GLPK "
+ "distribution.\n");
+ xprintf("\n");
+ xprintf("See GLPK web page at <http://www.gnu.org/software/glpk/g"
+ "lpk.html>.\n");
+ xprintf("\n");
+ xprintf("Please report bugs to <bug-glpk@gnu.org>.\n");
+ return;
+}
+
+static void print_version(int briefly)
+{ /* print version information */
+ xprintf("GLPSOL: GLPK LP/MIP Solver, v%s\n", glp_version());
+ if (briefly) goto done;
+ xprintf("Copyright (C) 2000-2017 Andrew Makhorin, Department for "
+ "Applied\n");
+ xprintf("Informatics, Moscow Aviation Institute, Moscow, Russia. "
+ "All rights\n");
+ xprintf("reserved. E-mail: <mao@gnu.org>.\n");
+ xprintf("\n");
+ xprintf("This program has ABSOLUTELY NO WARRANTY.\n");
+ xprintf("\n");
+ xprintf("This program is free software; you may re-distribute it "
+ "under the terms\n");
+ xprintf("of the GNU General Public License version 3 or later.\n")
+ ;
+done: return;
+}
+
+static int parse_cmdline(struct csa *csa, int argc, char *argv[])
+{ /* parse command-line parameters */
+ int k;
+#define p(str) (strcmp(argv[k], str) == 0)
+ for (k = 1; k < argc; k++)
+ { if (p("--mps"))
+ csa->format = FMT_MPS_DECK;
+ else if (p("--freemps"))
+ csa->format = FMT_MPS_FILE;
+ else if (p("--lp") || p("--cpxlp"))
+ csa->format = FMT_LP;
+ else if (p("--glp"))
+ csa->format = FMT_GLP;
+ else if (p("--math") || p("-m") || p("--model"))
+ csa->format = FMT_MATHPROG;
+ else if (p("-d") || p("--data"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No input data file specified\n");
+ return 1;
+ }
+ if (csa->ndf == DATA_MAX)
+ { xprintf("Too many input data files\n");
+ return 1;
+ }
+ csa->in_data[++(csa->ndf)] = argv[k];
+ }
+ else if (p("-y") || p("--display"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No display output file specified\n");
+ return 1;
+ }
+ if (csa->out_dpy != NULL)
+ { xprintf("Only one display output file allowed\n");
+ return 1;
+ }
+ csa->out_dpy = argv[k];
+ }
+ else if (p("--seed"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' ||
+ argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
+ { xprintf("No seed value specified\n");
+ return 1;
+ }
+ if (strcmp(argv[k], "?") == 0)
+ csa->seed = 0x80000000;
+ else if (str2int(argv[k], &csa->seed))
+ { xprintf("Invalid seed value '%s'\n", argv[k]);
+ return 1;
+ }
+ }
+ else if (p("--mincost"))
+ csa->format = FMT_MIN_COST;
+ else if (p("--maxflow"))
+ csa->format = FMT_MAX_FLOW;
+#if 1 /* 06/VIII-2011 */
+ else if (p("--cnf"))
+ csa->format = FMT_CNF;
+#endif
+ else if (p("--simplex"))
+ csa->solution = SOL_BASIC;
+ else if (p("--interior"))
+ csa->solution = SOL_INTERIOR;
+#if 1 /* 28/V-2010 */
+ else if (p("--alien"))
+ csa->iocp.alien = GLP_ON;
+#endif
+ else if (p("-r") || p("--read"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No input solution file specified\n");
+ return 1;
+ }
+ if (csa->in_res != NULL)
+ { xprintf("Only one input solution file allowed\n");
+ return 1;
+ }
+ csa->in_res = argv[k];
+ }
+ else if (p("--min"))
+ csa->dir = GLP_MIN;
+ else if (p("--max"))
+ csa->dir = GLP_MAX;
+ else if (p("--scale"))
+ csa->scale = 1;
+ else if (p("--noscale"))
+ csa->scale = 0;
+ else if (p("-o") || p("--output"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No output solution file specified\n");
+ return 1;
+ }
+ if (csa->out_sol != NULL)
+ { xprintf("Only one output solution file allowed\n");
+ return 1;
+ }
+ csa->out_sol = argv[k];
+ }
+ else if (p("-w") || p("--write"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No output solution file specified\n");
+ return 1;
+ }
+ if (csa->out_res != NULL)
+ { xprintf("Only one output solution file allowed\n");
+ return 1;
+ }
+ csa->out_res = argv[k];
+ }
+ else if (p("--ranges") || p("--bounds"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No output file specified to write sensitivity a"
+ "nalysis report\n");
+ return 1;
+ }
+ if (csa->out_ranges != NULL)
+ { xprintf("Only one output file allowed to write sensitivi"
+ "ty analysis report\n");
+ return 1;
+ }
+ csa->out_ranges = argv[k];
+ }
+ else if (p("--tmlim"))
+ { int tm_lim;
+ k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No time limit specified\n");
+ return 1;
+ }
+ if (str2int(argv[k], &tm_lim) || tm_lim < 0)
+ { xprintf("Invalid time limit '%s'\n", argv[k]);
+ return 1;
+ }
+ if (tm_lim <= INT_MAX / 1000)
+ csa->smcp.tm_lim = csa->iocp.tm_lim = 1000 * tm_lim;
+ else
+ csa->smcp.tm_lim = csa->iocp.tm_lim = INT_MAX;
+ }
+ else if (p("--memlim"))
+ { int mem_lim;
+ k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No memory limit specified\n");
+ return 1;
+ }
+ if (str2int(argv[k], &mem_lim) || mem_lim < 1)
+ { xprintf("Invalid memory limit '%s'\n", argv[k]);
+ return 1;
+ }
+ glp_mem_limit(mem_lim);
+ }
+ else if (p("--check"))
+ csa->check = 1;
+ else if (p("--name"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No problem name specified\n");
+ return 1;
+ }
+ if (csa->new_name != NULL)
+ { xprintf("Only one problem name allowed\n");
+ return 1;
+ }
+ csa->new_name = argv[k];
+ }
+#if 1 /* 18/I-2018 */
+ else if (p("--hide"))
+ csa->hide = 1;
+#endif
+ else if (p("--wmps"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No fixed MPS output file specified\n");
+ return 1;
+ }
+ if (csa->out_mps != NULL)
+ { xprintf("Only one fixed MPS output file allowed\n");
+ return 1;
+ }
+ csa->out_mps = argv[k];
+ }
+ else if (p("--wfreemps"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No free MPS output file specified\n");
+ return 1;
+ }
+ if (csa->out_freemps != NULL)
+ { xprintf("Only one free MPS output file allowed\n");
+ return 1;
+ }
+ csa->out_freemps = argv[k];
+ }
+ else if (p("--wlp") || p("--wcpxlp") || p("--wlpt"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No CPLEX LP output file specified\n");
+ return 1;
+ }
+ if (csa->out_cpxlp != NULL)
+ { xprintf("Only one CPLEX LP output file allowed\n");
+ return 1;
+ }
+ csa->out_cpxlp = argv[k];
+ }
+ else if (p("--wglp"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No GLPK LP/MIP output file specified\n");
+ return 1;
+ }
+ if (csa->out_glp != NULL)
+ { xprintf("Only one GLPK LP/MIP output file allowed\n");
+ return 1;
+ }
+ csa->out_glp = argv[k];
+ }
+#if 0
+ else if (p("--wpb"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No problem output file specified\n");
+ return 1;
+ }
+ if (csa->out_pb != NULL)
+ { xprintf("Only one OPB output file allowed\n");
+ return 1;
+ }
+ csa->out_pb = argv[k];
+ }
+ else if (p("--wnpb"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No problem output file specified\n");
+ return 1;
+ }
+ if (csa->out_npb != NULL)
+ { xprintf("Only one normalized OPB output file allowed\n");
+ return 1;
+ }
+ csa->out_npb = argv[k];
+ }
+#endif
+#if 1 /* 06/VIII-2011 */
+ else if (p("--wcnf"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No problem output file specified\n");
+ return 1;
+ }
+ if (csa->out_cnf != NULL)
+ { xprintf("Only one output DIMACS CNF-SAT file allowed\n");
+ return 1;
+ }
+ csa->out_cnf = argv[k];
+ }
+#endif
+ else if (p("--log"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No log file specified\n");
+ return 1;
+ }
+ if (csa->log_file != NULL)
+ { xprintf("Only one log file allowed\n");
+ return 1;
+ }
+ csa->log_file = argv[k];
+ }
+ else if (p("-h") || p("--help"))
+ { print_help(argv[0]);
+ return -1;
+ }
+ else if (p("-v") || p("--version"))
+ { print_version(0);
+ return -1;
+ }
+#if 0 /* 08/III-2014 */
+ else if (p("--luf"))
+ csa->bfcp.type = GLP_BF_FT;
+ else if (p("--cbg"))
+ csa->bfcp.type = GLP_BF_BG;
+ else if (p("--cgr"))
+ csa->bfcp.type = GLP_BF_GR;
+#else
+ else if (p("--luf"))
+ { csa->bfcp.type &= 0x0F;
+ csa->bfcp.type |= GLP_BF_LUF;
+ }
+ else if (p("--btf"))
+ { csa->bfcp.type &= 0x0F;
+ csa->bfcp.type |= GLP_BF_BTF;
+ }
+ else if (p("--ft"))
+ { csa->bfcp.type &= 0xF0;
+ csa->bfcp.type |= GLP_BF_FT;
+ }
+ else if (p("--cbg"))
+ { csa->bfcp.type &= 0xF0;
+ csa->bfcp.type |= GLP_BF_BG;
+ }
+ else if (p("--cgr"))
+ { csa->bfcp.type &= 0xF0;
+ csa->bfcp.type |= GLP_BF_GR;
+ }
+#endif
+ else if (p("--primal"))
+ csa->smcp.meth = GLP_PRIMAL;
+ else if (p("--dual"))
+ csa->smcp.meth = GLP_DUAL;
+ else if (p("--std"))
+ csa->crash = USE_STD_BASIS;
+ else if (p("--adv"))
+ csa->crash = USE_ADV_BASIS;
+ else if (p("--bib"))
+ csa->crash = USE_CPX_BASIS;
+ else if (p("--ini"))
+ { csa->crash = USE_INI_BASIS;
+ csa->smcp.presolve = GLP_OFF;
+ k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No initial basis file specified\n");
+ return 1;
+ }
+ if (csa->ini_file != NULL)
+ { xprintf("Only one initial basis file allowed\n");
+ return 1;
+ }
+ csa->ini_file = argv[k];
+ }
+ else if (p("--steep"))
+ csa->smcp.pricing = GLP_PT_PSE;
+ else if (p("--nosteep"))
+ csa->smcp.pricing = GLP_PT_STD;
+ else if (p("--relax"))
+ csa->smcp.r_test = GLP_RT_HAR;
+ else if (p("--norelax"))
+ csa->smcp.r_test = GLP_RT_STD;
+#if 1 /* 28/III-2016 */
+ else if (p("--flip"))
+#if 0 /* 23/VI-2017 */
+ { csa->smcp.meth = GLP_DUAL;
+#else
+ /* now this option is implemented in both primal and dual */
+ {
+#endif
+ csa->smcp.r_test = GLP_RT_FLIP;
+ csa->iocp.flip = GLP_ON;
+ }
+#endif
+ else if (p("--presol"))
+ csa->smcp.presolve = GLP_ON;
+ else if (p("--nopresol"))
+ csa->smcp.presolve = GLP_OFF;
+ else if (p("--exact"))
+ csa->exact = 1;
+ else if (p("--xcheck"))
+ csa->xcheck = 1;
+ else if (p("--nord"))
+ csa->iptcp.ord_alg = GLP_ORD_NONE;
+ else if (p("--qmd"))
+ csa->iptcp.ord_alg = GLP_ORD_QMD;
+ else if (p("--amd"))
+ csa->iptcp.ord_alg = GLP_ORD_AMD;
+ else if (p("--symamd"))
+ csa->iptcp.ord_alg = GLP_ORD_SYMAMD;
+ else if (p("--nomip"))
+ csa->nomip = 1;
+ else if (p("--first"))
+ csa->iocp.br_tech = GLP_BR_FFV;
+ else if (p("--last"))
+ csa->iocp.br_tech = GLP_BR_LFV;
+ else if (p("--drtom"))
+ csa->iocp.br_tech = GLP_BR_DTH;
+ else if (p("--mostf"))
+ csa->iocp.br_tech = GLP_BR_MFV;
+ else if (p("--pcost"))
+ csa->iocp.br_tech = GLP_BR_PCH;
+ else if (p("--dfs"))
+ csa->iocp.bt_tech = GLP_BT_DFS;
+ else if (p("--bfs"))
+ csa->iocp.bt_tech = GLP_BT_BFS;
+ else if (p("--bestp"))
+ csa->iocp.bt_tech = GLP_BT_BPH;
+ else if (p("--bestb"))
+ csa->iocp.bt_tech = GLP_BT_BLB;
+ else if (p("--intopt"))
+ csa->iocp.presolve = GLP_ON;
+ else if (p("--nointopt"))
+ csa->iocp.presolve = GLP_OFF;
+ else if (p("--binarize"))
+ csa->iocp.presolve = csa->iocp.binarize = GLP_ON;
+ else if (p("--fpump"))
+ csa->iocp.fp_heur = GLP_ON;
+#if 1 /* 29/VI-2013 */
+ else if (p("--proxy"))
+ { csa->iocp.ps_heur = GLP_ON;
+ if (argv[k+1] && isdigit((unsigned char)argv[k+1][0]))
+ { int nnn;
+ k++;
+ if (str2int(argv[k], &nnn) || nnn < 1)
+ { xprintf("Invalid proxy time limit '%s'\n", argv[k]);
+ return 1;
+ }
+ csa->iocp.ps_tm_lim = 1000 * nnn;
+ }
+ }
+#endif
+ else if (p("--gomory"))
+ csa->iocp.gmi_cuts = GLP_ON;
+ else if (p("--mir"))
+ csa->iocp.mir_cuts = GLP_ON;
+ else if (p("--cover"))
+ csa->iocp.cov_cuts = GLP_ON;
+ else if (p("--clique"))
+ csa->iocp.clq_cuts = GLP_ON;
+ else if (p("--cuts"))
+ csa->iocp.gmi_cuts = csa->iocp.mir_cuts =
+ csa->iocp.cov_cuts = csa->iocp.clq_cuts = GLP_ON;
+ else if (p("--mipgap"))
+ { double mip_gap;
+ k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No relative gap tolerance specified\n");
+ return 1;
+ }
+ if (str2num(argv[k], &mip_gap) || mip_gap < 0.0)
+ { xprintf("Invalid relative mip gap tolerance '%s'\n",
+ argv[k]);
+ return 1;
+ }
+ csa->iocp.mip_gap = mip_gap;
+ }
+#if 1 /* 15/VIII-2011 */
+ else if (p("--minisat"))
+ csa->minisat = 1;
+ else if (p("--objbnd"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' ||
+ argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
+ { xprintf("No objective bound specified\n");
+ return 1;
+ }
+ csa->minisat = 1;
+ csa->use_bnd = 1;
+ if (str2int(argv[k], &csa->obj_bnd))
+ { xprintf("Invalid objective bound '%s' (should be integer"
+ " value)\n", argv[k]);
+ return 1;
+ }
+ }
+#endif
+#if 1 /* 11/VII-2013 */
+ else if (p("--use"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No input MIP solution file specified\n");
+ return 1;
+ }
+ if (csa->use_sol != NULL)
+ { xprintf("Only one input MIP solution file allowed\n");
+ return 1;
+ }
+ csa->use_sol = argv[k];
+ }
+ else if (p("--save"))
+ { k++;
+ if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+ { xprintf("No output MIP solution file specified\n");
+ return 1;
+ }
+ if (csa->iocp.save_sol != NULL)
+ { xprintf("Only one output MIP solution file allowed\n");
+ return 1;
+ }
+ csa->iocp.save_sol = argv[k];
+ }
+#endif
+ else if (argv[k][0] == '-' ||
+ (argv[k][0] == '-' && argv[k][1] == '-'))
+ { xprintf("Invalid option '%s'; try %s --help\n",
+ argv[k], argv[0]);
+ return 1;
+ }
+ else
+ { if (csa->in_file != NULL)
+ { xprintf("Only one input problem file allowed\n");
+ return 1;
+ }
+ csa->in_file = argv[k];
+ }
+ }
+#undef p
+ return 0;
+}
+
+typedef struct { double rhs, pi; } v_data;
+typedef struct { double low, cap, cost, x; } a_data;
+
+#ifndef __WOE__
+int main(int argc, char *argv[])
+#else
+int __cdecl main(int argc, char *argv[])
+#endif
+{ /* stand-alone LP/MIP solver */
+ struct csa _csa, *csa = &_csa;
+ int ret;
+#if 0 /* 10/VI-2013 */
+ glp_long start;
+#else
+ double start;
+#endif
+ /* perform initialization */
+ csa->prob = glp_create_prob();
+ glp_get_bfcp(csa->prob, &csa->bfcp);
+ glp_init_smcp(&csa->smcp);
+ csa->smcp.presolve = GLP_ON;
+ glp_init_iptcp(&csa->iptcp);
+ glp_init_iocp(&csa->iocp);
+ csa->iocp.presolve = GLP_ON;
+ csa->tran = NULL;
+ csa->graph = NULL;
+ csa->format = FMT_MPS_FILE;
+ csa->in_file = NULL;
+ csa->ndf = 0;
+ csa->out_dpy = NULL;
+ csa->seed = 1;
+ csa->solution = SOL_BASIC;
+ csa->in_res = NULL;
+ csa->dir = 0;
+ csa->scale = 1;
+ csa->out_sol = NULL;
+ csa->out_res = NULL;
+ csa->out_ranges = NULL;
+ csa->check = 0;
+ csa->new_name = NULL;
+#if 1 /* 18/I-2018 */
+ csa->hide = 0;
+#endif
+ csa->out_mps = NULL;
+ csa->out_freemps = NULL;
+ csa->out_cpxlp = NULL;
+ csa->out_glp = NULL;
+#if 0
+ csa->out_pb = NULL;
+ csa->out_npb = NULL;
+#endif
+#if 1 /* 06/VIII-2011 */
+ csa->out_cnf = NULL;
+#endif
+ csa->log_file = NULL;
+ csa->crash = USE_ADV_BASIS;
+ csa->ini_file = NULL;
+ csa->exact = 0;
+ csa->xcheck = 0;
+ csa->nomip = 0;
+#if 1 /* 15/VIII-2011 */
+ csa->minisat = 0;
+ csa->use_bnd = 0;
+ csa->obj_bnd = 0;
+#endif
+#if 1 /* 11/VII-2013 */
+ csa->use_sol = NULL;
+#endif
+ /* parse command-line parameters */
+ ret = parse_cmdline(csa, argc, argv);
+ if (ret < 0)
+ { ret = EXIT_SUCCESS;
+ goto done;
+ }
+ if (ret > 0)
+ { ret = EXIT_FAILURE;
+ goto done;
+ }
+ /*--------------------------------------------------------------*/
+ /* remove all output files specified in the command line */
+ if (csa->out_dpy != NULL) remove(csa->out_dpy);
+ if (csa->out_sol != NULL) remove(csa->out_sol);
+ if (csa->out_res != NULL) remove(csa->out_res);
+ if (csa->out_ranges != NULL) remove(csa->out_ranges);
+ if (csa->out_mps != NULL) remove(csa->out_mps);
+ if (csa->out_freemps != NULL) remove(csa->out_freemps);
+ if (csa->out_cpxlp != NULL) remove(csa->out_cpxlp);
+ if (csa->out_glp != NULL) remove(csa->out_glp);
+#if 0
+ if (csa->out_pb != NULL) remove(csa->out_pb);
+ if (csa->out_npb != NULL) remove(csa->out_npb);
+#endif
+#if 1 /* 06/VIII-2011 */
+ if (csa->out_cnf != NULL) remove(csa->out_cnf);
+#endif
+ if (csa->log_file != NULL) remove(csa->log_file);
+ /*--------------------------------------------------------------*/
+ /* open log file, if required */
+ if (csa->log_file != NULL)
+ { if (glp_open_tee(csa->log_file))
+ { xprintf("Unable to create log file\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* print version information */
+ print_version(1);
+ /*--------------------------------------------------------------*/
+ /* print parameters specified in the command line */
+ if (argc > 1)
+ { int k, len = INT_MAX;
+ xprintf("Parameter(s) specified in the command line:");
+ for (k = 1; k < argc; k++)
+ { if (len > 72)
+ xprintf("\n"), len = 0;
+ xprintf(" %s", argv[k]);
+ len += 1 + strlen(argv[k]);
+ }
+ xprintf("\n");
+ }
+ /*--------------------------------------------------------------*/
+ /* read problem data from the input file */
+ if (csa->in_file == NULL)
+ { xprintf("No input problem file specified; try %s --help\n",
+ argv[0]);
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ if (csa->format == FMT_MPS_DECK)
+ { ret = glp_read_mps(csa->prob, GLP_MPS_DECK, NULL,
+ csa->in_file);
+ if (ret != 0)
+err1: { xprintf("MPS file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ else if (csa->format == FMT_MPS_FILE)
+ { ret = glp_read_mps(csa->prob, GLP_MPS_FILE, NULL,
+ csa->in_file);
+ if (ret != 0) goto err1;
+ }
+ else if (csa->format == FMT_LP)
+ { ret = glp_read_lp(csa->prob, NULL, csa->in_file);
+ if (ret != 0)
+ { xprintf("CPLEX LP file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ else if (csa->format == FMT_GLP)
+ { ret = glp_read_prob(csa->prob, 0, csa->in_file);
+ if (ret != 0)
+ { xprintf("GLPK LP/MIP file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ else if (csa->format == FMT_MATHPROG)
+ { int k;
+ /* allocate the translator workspace */
+ csa->tran = glp_mpl_alloc_wksp();
+ /* set seed value */
+ if (csa->seed == 0x80000000)
+#if 0 /* 10/VI-2013 */
+ { csa->seed = glp_time().lo;
+#else
+ { csa->seed = (int)fmod(glp_time(), 1000000000.0);
+#endif
+ xprintf("Seed value %d will be used\n", csa->seed);
+ }
+ glp_mpl_init_rand(csa->tran, csa->seed);
+ /* read model section and optional data section */
+ if (glp_mpl_read_model(csa->tran, csa->in_file, csa->ndf > 0))
+err2: { xprintf("MathProg model processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ /* read optional data section(s), if necessary */
+ for (k = 1; k <= csa->ndf; k++)
+ { if (glp_mpl_read_data(csa->tran, csa->in_data[k]))
+ goto err2;
+ }
+ /* generate the model */
+ if (glp_mpl_generate(csa->tran, csa->out_dpy)) goto err2;
+ /* build the problem instance from the model */
+ glp_mpl_build_prob(csa->tran, csa->prob);
+ }
+ else if (csa->format == FMT_MIN_COST)
+ { csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
+ ret = glp_read_mincost(csa->graph, offsetof(v_data, rhs),
+ offsetof(a_data, low), offsetof(a_data, cap),
+ offsetof(a_data, cost), csa->in_file);
+ if (ret != 0)
+ { xprintf("DIMACS file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ glp_mincost_lp(csa->prob, csa->graph, GLP_ON,
+ offsetof(v_data, rhs), offsetof(a_data, low),
+ offsetof(a_data, cap), offsetof(a_data, cost));
+ glp_set_prob_name(csa->prob, csa->in_file);
+ }
+ else if (csa->format == FMT_MAX_FLOW)
+ { int s, t;
+ csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
+ ret = glp_read_maxflow(csa->graph, &s, &t,
+ offsetof(a_data, cap), csa->in_file);
+ if (ret != 0)
+ { xprintf("DIMACS file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ glp_maxflow_lp(csa->prob, csa->graph, GLP_ON, s, t,
+ offsetof(a_data, cap));
+ glp_set_prob_name(csa->prob, csa->in_file);
+ }
+#if 1 /* 06/VIII-2011 */
+ else if (csa->format == FMT_CNF)
+ { ret = glp_read_cnfsat(csa->prob, csa->in_file);
+ if (ret != 0)
+ { xprintf("DIMACS file processing error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ glp_set_prob_name(csa->prob, csa->in_file);
+ }
+#endif
+ else
+ xassert(csa != csa);
+ /*--------------------------------------------------------------*/
+ /* change problem name, if required */
+ if (csa->new_name != NULL)
+ glp_set_prob_name(csa->prob, csa->new_name);
+ /* change optimization direction, if required */
+ if (csa->dir != 0)
+ glp_set_obj_dir(csa->prob, csa->dir);
+ /* sort elements of the constraint matrix */
+ glp_sort_matrix(csa->prob);
+#if 1 /* 18/I-2018 */
+ /*--------------------------------------------------------------*/
+ /* remove all symbolic names from problem object, if required */
+ if (csa->hide)
+ { int i, j;
+ glp_set_obj_name(csa->prob, NULL);
+ glp_delete_index(csa->prob);
+ for (i = glp_get_num_rows(csa->prob); i >= 1; i--)
+ glp_set_row_name(csa->prob, i, NULL);
+ for (j = glp_get_num_cols(csa->prob); j >= 1; j--)
+ glp_set_col_name(csa->prob, j, NULL);
+ }
+#endif
+ /*--------------------------------------------------------------*/
+ /* write problem data in fixed MPS format, if required */
+ if (csa->out_mps != NULL)
+ { ret = glp_write_mps(csa->prob, GLP_MPS_DECK, NULL,
+ csa->out_mps);
+ if (ret != 0)
+ { xprintf("Unable to write problem in fixed MPS format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write problem data in free MPS format, if required */
+ if (csa->out_freemps != NULL)
+ { ret = glp_write_mps(csa->prob, GLP_MPS_FILE, NULL,
+ csa->out_freemps);
+ if (ret != 0)
+ { xprintf("Unable to write problem in free MPS format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write problem data in CPLEX LP format, if required */
+ if (csa->out_cpxlp != NULL)
+ { ret = glp_write_lp(csa->prob, NULL, csa->out_cpxlp);
+ if (ret != 0)
+ { xprintf("Unable to write problem in CPLEX LP format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write problem data in GLPK format, if required */
+ if (csa->out_glp != NULL)
+ { ret = glp_write_prob(csa->prob, 0, csa->out_glp);
+ if (ret != 0)
+ { xprintf("Unable to write problem in GLPK format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+#if 0
+ /* write problem data in OPB format, if required */
+ if (csa->out_pb != NULL)
+ { ret = lpx_write_pb(csa->prob, csa->out_pb, 0, 0);
+ if (ret != 0)
+ { xprintf("Unable to write problem in OPB format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write problem data in normalized OPB format, if required */
+ if (csa->out_npb != NULL)
+ { ret = lpx_write_pb(csa->prob, csa->out_npb, 1, 1);
+ if (ret != 0)
+ { xprintf(
+ "Unable to write problem in normalized OPB format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+#endif
+#if 1 /* 06/VIII-2011 */
+ /* write problem data in DIMACS CNF-SAT format, if required */
+ if (csa->out_cnf != NULL)
+ { ret = glp_write_cnfsat(csa->prob, csa->out_cnf);
+ if (ret != 0)
+ { xprintf(
+ "Unable to write problem in DIMACS CNF-SAT format\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+#endif
+ /*--------------------------------------------------------------*/
+ /* if only problem data check is required, skip computations */
+ if (csa->check)
+ {
+#if 1 /* 29/III-2016 */
+ /* report problem characteristics */
+ int j, cnt = 0;
+ xprintf("--- Problem Characteristics ---\n");
+ xprintf("Number of rows = %8d\n",
+ glp_get_num_rows(csa->prob));
+ xprintf("Number of columns = %8d\n",
+ glp_get_num_cols(csa->prob));
+ xprintf("Number of non-zeros (matrix) = %8d\n",
+ glp_get_num_nz(csa->prob));
+ for (j = glp_get_num_cols(csa->prob); j >= 1; j--)
+ { if (glp_get_obj_coef(csa->prob, j) != 0.0)
+ cnt++;
+ }
+ xprintf("Number of non-zeros (objrow) = %8d\n",
+ cnt);
+#endif
+ ret = EXIT_SUCCESS;
+ goto done;
+ }
+ /*--------------------------------------------------------------*/
+ /* determine the solution type */
+ if (!csa->nomip &&
+ glp_get_num_int(csa->prob) + glp_get_num_bin(csa->prob) > 0)
+ { if (csa->solution == SOL_INTERIOR)
+ { xprintf("Interior-point method is not able to solve MIP pro"
+ "blem; use --simplex\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ csa->solution = SOL_INTEGER;
+ }
+ /*--------------------------------------------------------------*/
+ /* if solution is provided, read it and skip computations */
+ if (csa->in_res != NULL)
+ { if (csa->solution == SOL_BASIC)
+ ret = glp_read_sol(csa->prob, csa->in_res);
+ else if (csa->solution == SOL_INTERIOR)
+ ret = glp_read_ipt(csa->prob, csa->in_res);
+ else if (csa->solution == SOL_INTEGER)
+ ret = glp_read_mip(csa->prob, csa->in_res);
+ else
+ xassert(csa != csa);
+ if (ret != 0)
+ { xprintf("Unable to read problem solution\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ goto skip;
+ }
+#if 1 /* 11/VII-2013 */
+ /*--------------------------------------------------------------*/
+ /* if initial MIP solution is provided, read it */
+ if (csa->solution == SOL_INTEGER && csa->use_sol != NULL)
+ { ret = glp_read_mip(csa->prob, csa->use_sol);
+ if (ret != 0)
+ { xprintf("Unable to read initial MIP solution\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ csa->iocp.use_sol = GLP_ON;
+ }
+#endif
+ /*--------------------------------------------------------------*/
+ /* scale the problem data, if required */
+ if (csa->scale)
+ { if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||
+ csa->solution == SOL_INTERIOR ||
+ csa->solution == SOL_INTEGER && !csa->iocp.presolve)
+ glp_scale_prob(csa->prob, GLP_SF_AUTO);
+ }
+ /*--------------------------------------------------------------*/
+ /* construct starting LP basis */
+ if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||
+ csa->solution == SOL_INTEGER && !csa->iocp.presolve)
+ { if (csa->crash == USE_STD_BASIS)
+ glp_std_basis(csa->prob);
+ else if (csa->crash == USE_ADV_BASIS)
+ glp_adv_basis(csa->prob, 0);
+ else if (csa->crash == USE_CPX_BASIS)
+ glp_cpx_basis(csa->prob);
+ else if (csa->crash == USE_INI_BASIS)
+ { ret = glp_read_sol(csa->prob, csa->ini_file);
+ if (ret != 0)
+ { xprintf("Unable to read initial basis\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ else
+ xassert(csa != csa);
+ }
+ /*--------------------------------------------------------------*/
+ /* solve the problem */
+
+ start = glp_time();
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ if (csa->solution == SOL_BASIC)
+ { if (!csa->exact)
+ { glp_set_bfcp(csa->prob, &csa->bfcp);
+ glp_simplex(csa->prob, &csa->smcp);
+ if (csa->xcheck)
+ { if (csa->smcp.presolve &&
+ glp_get_status(csa->prob) != GLP_OPT)
+ xprintf("If you need to check final basis for non-opt"
+ "imal solution, use --nopresol\n");
+ else
+ glp_exact(csa->prob, &csa->smcp);
+ }
+ if (csa->out_sol != NULL || csa->out_res != NULL)
+ { if (csa->smcp.presolve &&
+ glp_get_status(csa->prob) != GLP_OPT)
+ xprintf("If you need actual output for non-optimal solut"
+ "ion, use --nopresol\n");
+ }
+ }
+ else
+ glp_exact(csa->prob, &csa->smcp);
+ }
+ else if (csa->solution == SOL_INTERIOR)
+ glp_interior(csa->prob, &csa->iptcp);
+#if 1 /* 15/VIII-2011 */
+ else if (csa->solution == SOL_INTEGER && csa->minisat)
+ { if (glp_check_cnfsat(csa->prob) == 0)
+ glp_minisat1(csa->prob);
+ else
+ glp_intfeas1(csa->prob, csa->use_bnd, csa->obj_bnd);
+ }
+#endif
+ else if (csa->solution == SOL_INTEGER)
+ { glp_set_bfcp(csa->prob, &csa->bfcp);
+ if (!csa->iocp.presolve)
+ glp_simplex(csa->prob, &csa->smcp);
+#if 0
+ csa->iocp.msg_lev = GLP_MSG_DBG;
+ csa->iocp.pp_tech = GLP_PP_NONE;
+#endif
+#ifdef GLP_CB_FUNC /* 05/IV-2016 */
+ { extern void GLP_CB_FUNC(glp_tree *, void *);
+ csa->iocp.cb_func = GLP_CB_FUNC;
+ csa->iocp.cb_info = NULL;
+ }
+#endif
+ glp_intopt(csa->prob, &csa->iocp);
+ }
+ else
+ xassert(csa != csa);
+ /*--------------------------------------------------------------*/
+ /* display statistics */
+#ifdef VERIMAG
+ clock_stop();
+ print_total_clock();
+#endif
+ xprintf("Time used: %.1f secs\n", glp_difftime(glp_time(),
+ start));
+#if 0 /* 16/II-2012 */
+ { glp_long tpeak;
+ char buf[50];
+ glp_mem_usage(NULL, NULL, NULL, &tpeak);
+ xprintf("Memory used: %.1f Mb (%s bytes)\n",
+ xltod(tpeak) / 1048576.0, xltoa(tpeak, buf));
+ }
+#else
+ { size_t tpeak;
+ glp_mem_usage(NULL, NULL, NULL, &tpeak);
+ xprintf("Memory used: %.1f Mb (%.0f bytes)\n",
+ (double)tpeak / 1048576.0, (double)tpeak);
+ }
+#endif
+ /*--------------------------------------------------------------*/
+skip: /* postsolve the model, if necessary */
+ if (csa->tran != NULL)
+ { if (csa->solution == SOL_BASIC)
+ { if (!(glp_get_status(csa->prob) == GLP_OPT ||
+ glp_get_status(csa->prob) == GLP_FEAS))
+ ret = -1;
+ else
+ ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_SOL);
+ }
+ else if (csa->solution == SOL_INTERIOR)
+ { if (!(glp_ipt_status(csa->prob) == GLP_OPT ||
+ glp_ipt_status(csa->prob) == GLP_FEAS))
+ ret = -1;
+ else
+ ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_IPT);
+ }
+ else if (csa->solution == SOL_INTEGER)
+ { if (!(glp_mip_status(csa->prob) == GLP_OPT ||
+ glp_mip_status(csa->prob) == GLP_FEAS))
+ ret = -1;
+ else
+ ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_MIP);
+ }
+ else
+ xassert(csa != csa);
+ if (ret > 0)
+ { xprintf("Model postsolving error\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* write problem solution in printable format, if required */
+ if (csa->out_sol != NULL)
+ { if (csa->solution == SOL_BASIC)
+ ret = glp_print_sol(csa->prob, csa->out_sol);
+ else if (csa->solution == SOL_INTERIOR)
+ ret = glp_print_ipt(csa->prob, csa->out_sol);
+ else if (csa->solution == SOL_INTEGER)
+ ret = glp_print_mip(csa->prob, csa->out_sol);
+ else
+ xassert(csa != csa);
+ if (ret != 0)
+ { xprintf("Unable to write problem solution\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write problem solution in printable format, if required */
+ if (csa->out_res != NULL)
+ { if (csa->solution == SOL_BASIC)
+ ret = glp_write_sol(csa->prob, csa->out_res);
+ else if (csa->solution == SOL_INTERIOR)
+ ret = glp_write_ipt(csa->prob, csa->out_res);
+ else if (csa->solution == SOL_INTEGER)
+ ret = glp_write_mip(csa->prob, csa->out_res);
+ else
+ xassert(csa != csa);
+ if (ret != 0)
+ { xprintf("Unable to write problem solution\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ /* write sensitivity analysis report, if required */
+ if (csa->out_ranges != NULL)
+ { if (csa->solution == SOL_BASIC)
+ { if (glp_get_status(csa->prob) == GLP_OPT)
+ { if (glp_bf_exists(csa->prob))
+ranges: { ret = glp_print_ranges(csa->prob, 0, NULL, 0,
+ csa->out_ranges);
+ if (ret != 0)
+ { xprintf("Unable to write sensitivity analysis repo"
+ "rt\n");
+ ret = EXIT_FAILURE;
+ goto done;
+ }
+ }
+ else
+ { ret = glp_factorize(csa->prob);
+ if (ret == 0) goto ranges;
+ xprintf("Cannot produce sensitivity analysis report d"
+ "ue to error in basis factorization (glp_factorize"
+ " returned %d); try --nopresol\n", ret);
+ }
+ }
+ else
+ xprintf("Cannot produce sensitivity analysis report for "
+ "non-optimal basic solution\n");
+ }
+ else
+ xprintf("Cannot produce sensitivity analysis report for int"
+ "erior-point or MIP solution\n");
+ }
+ /*--------------------------------------------------------------*/
+ /* all seems to be ok */
+ ret = EXIT_SUCCESS;
+ /*--------------------------------------------------------------*/
+done: /* delete the LP/MIP problem object */
+ if (csa->prob != NULL)
+ glp_delete_prob(csa->prob);
+ /* free the translator workspace, if necessary */
+ if (csa->tran != NULL)
+ glp_mpl_free_wksp(csa->tran);
+ /* delete the network problem object, if necessary */
+ if (csa->graph != NULL)
+ glp_delete_graph(csa->graph);
+#if 0 /* 23/XI-2015 */
+ xassert(gmp_pool_count() == 0);
+ gmp_free_mem();
+#endif
+ /* close log file, if necessary */
+ if (csa->log_file != NULL) glp_close_tee();
+ /* check that no memory blocks are still allocated */
+#if 0 /* 16/II-2012 */
+ { int count;
+ glp_long total;
+ glp_mem_usage(&count, NULL, &total, NULL);
+ if (count != 0)
+ xerror("Error: %d memory block(s) were lost\n", count);
+ xassert(count == 0);
+ xassert(total.lo == 0 && total.hi == 0);
+ }
+#else
+ { int count;
+ size_t total;
+ glp_mem_usage(&count, NULL, &total, NULL);
+ if (count != 0)
+ xerror("Error: %d memory block(s) were lost\n", count);
+ xassert(total == 0);
+ }
+#endif
+ /* free the GLPK environment */
+ glp_free_env();
+ /* return to the control program */
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/examples/prod.mod b/test/monniaux/glpk-4.65/examples/prod.mod
new file mode 100644
index 00000000..aa793f76
--- /dev/null
+++ b/test/monniaux/glpk-4.65/examples/prod.mod
@@ -0,0 +1,331 @@
+# PROD, a multiperiod production model
+#
+# References:
+# Robert Fourer, David M. Gay and Brian W. Kernighan, "A Modeling Language
+# for Mathematical Programming." Management Science 36 (1990) 519-554.
+
+### PRODUCTION SETS AND PARAMETERS ###
+
+set prd 'products'; # Members of the product group
+
+param pt 'production time' {prd} > 0;
+
+ # Crew-hours to produce 1000 units
+
+param pc 'production cost' {prd} > 0;
+
+ # Nominal production cost per 1000, used
+ # to compute inventory and shortage costs
+
+### TIME PERIOD SETS AND PARAMETERS ###
+
+param first > 0 integer;
+ # Index of first production period to be modeled
+
+param last > first integer;
+
+ # Index of last production period to be modeled
+
+set time 'planning horizon' := first..last;
+
+### EMPLOYMENT PARAMETERS ###
+
+param cs 'crew size' > 0 integer;
+
+ # Workers per crew
+
+param sl 'shift length' > 0;
+
+ # Regular-time hours per shift
+
+param rtr 'regular time rate' > 0;
+
+ # Wage per hour for regular-time labor
+
+param otr 'overtime rate' > rtr;
+
+ # Wage per hour for overtime labor
+
+param iw 'initial workforce' >= 0 integer;
+
+ # Crews employed at start of first period
+
+param dpp 'days per period' {time} > 0;
+
+ # Regular working days in a production period
+
+param ol 'overtime limit' {time} >= 0;
+
+ # Maximum crew-hours of overtime in a period
+
+param cmin 'crew minimum' {time} >= 0;
+
+ # Lower limit on average employment in a period
+
+param cmax 'crew maximum' {t in time} >= cmin[t];
+
+ # Upper limit on average employment in a period
+
+param hc 'hiring cost' {time} >= 0;
+
+ # Penalty cost of hiring a crew
+
+param lc 'layoff cost' {time} >= 0;
+
+ # Penalty cost of laying off a crew
+
+### DEMAND PARAMETERS ###
+
+param dem 'demand' {prd,first..last+1} >= 0;
+
+ # Requirements (in 1000s)
+ # to be met from current production and inventory
+
+param pro 'promoted' {prd,first..last+1} logical;
+
+ # true if product will be the subject
+ # of a special promotion in the period
+
+### INVENTORY AND SHORTAGE PARAMETERS ###
+
+param rir 'regular inventory ratio' >= 0;
+
+ # Proportion of non-promoted demand
+ # that must be in inventory the previous period
+
+param pir 'promotional inventory ratio' >= 0;
+
+ # Proportion of promoted demand
+ # that must be in inventory the previous period
+
+param life 'inventory lifetime' > 0 integer;
+
+ # Upper limit on number of periods that
+ # any product may sit in inventory
+
+param cri 'inventory cost ratio' {prd} > 0;
+
+ # Inventory cost per 1000 units is
+ # cri times nominal production cost
+
+param crs 'shortage cost ratio' {prd} > 0;
+
+ # Shortage cost per 1000 units is
+ # crs times nominal production cost
+
+param iinv 'initial inventory' {prd} >= 0;
+
+ # Inventory at start of first period; age unknown
+
+param iil 'initial inventory left' {p in prd, t in time}
+ := iinv[p] less sum {v in first..t} dem[p,v];
+
+ # Initial inventory still available for allocation
+ # at end of period t
+
+param minv 'minimum inventory' {p in prd, t in time}
+ := dem[p,t+1] * (if pro[p,t+1] then pir else rir);
+
+ # Lower limit on inventory at end of period t
+
+### VARIABLES ###
+
+var Crews{first-1..last} >= 0;
+
+ # Average number of crews employed in each period
+
+var Hire{time} >= 0; # Crews hired from previous to current period
+
+var Layoff{time} >= 0; # Crews laid off from previous to current period
+
+var Rprd 'regular production' {prd,time} >= 0;
+
+ # Production using regular-time labor, in 1000s
+
+var Oprd 'overtime production' {prd,time} >= 0;
+
+ # Production using overtime labor, in 1000s
+
+var Inv 'inventory' {prd,time,1..life} >= 0;
+
+ # Inv[p,t,a] is the amount of product p that is
+ # a periods old -- produced in period (t+1)-a --
+ # and still in storage at the end of period t
+
+var Short 'shortage' {prd,time} >= 0;
+
+ # Accumulated unsatisfied demand at the end of period t
+
+### OBJECTIVE ###
+
+minimize cost:
+
+ sum {t in time} rtr * sl * dpp[t] * cs * Crews[t] +
+ sum {t in time} hc[t] * Hire[t] +
+ sum {t in time} lc[t] * Layoff[t] +
+ sum {t in time, p in prd} otr * cs * pt[p] * Oprd[p,t] +
+ sum {t in time, p in prd, a in 1..life} cri[p] * pc[p] * Inv[p,t,a] +
+ sum {t in time, p in prd} crs[p] * pc[p] * Short[p,t];
+
+ # Full regular wages for all crews employed, plus
+ # penalties for hiring and layoffs, plus
+ # wages for any overtime worked, plus
+ # inventory and shortage costs
+
+ # (All other production costs are assumed
+ # to depend on initial inventory and on demands,
+ # and so are not included explicitly.)
+
+### CONSTRAINTS ###
+
+rlim 'regular-time limit' {t in time}:
+
+ sum {p in prd} pt[p] * Rprd[p,t] <= sl * dpp[t] * Crews[t];
+
+ # Hours needed to accomplish all regular-time
+ # production in a period must not exceed
+ # hours available on all shifts
+
+olim 'overtime limit' {t in time}:
+
+ sum {p in prd} pt[p] * Oprd[p,t] <= ol[t];
+
+ # Hours needed to accomplish all overtime
+ # production in a period must not exceed
+ # the specified overtime limit
+
+empl0 'initial crew level': Crews[first-1] = iw;
+
+ # Use given initial workforce
+
+empl 'crew levels' {t in time}: Crews[t] = Crews[t-1] + Hire[t] - Layoff[t];
+
+ # Workforce changes by hiring or layoffs
+
+emplbnd 'crew limits' {t in time}: cmin[t] <= Crews[t] <= cmax[t];
+
+ # Workforce must remain within specified bounds
+
+dreq1 'first demand requirement' {p in prd}:
+
+ Rprd[p,first] + Oprd[p,first] + Short[p,first]
+ - Inv[p,first,1] = dem[p,first] less iinv[p];
+
+dreq 'demand requirements' {p in prd, t in first+1..last}:
+
+ Rprd[p,t] + Oprd[p,t] + Short[p,t] - Short[p,t-1]
+ + sum {a in 1..life} (Inv[p,t-1,a] - Inv[p,t,a])
+ = dem[p,t] less iil[p,t-1];
+
+ # Production plus increase in shortage plus
+ # decrease in inventory must equal demand
+
+ireq 'inventory requirements' {p in prd, t in time}:
+
+ sum {a in 1..life} Inv[p,t,a] + iil[p,t] >= minv[p,t];
+
+ # Inventory in storage at end of period t
+ # must meet specified minimum
+
+izero 'impossible inventories' {p in prd, v in 1..life-1, a in v+1..life}:
+
+ Inv[p,first+v-1,a] = 0;
+
+ # In the vth period (starting from first)
+ # no inventory may be more than v periods old
+ # (initial inventories are handled separately)
+
+ilim1 'new-inventory limits' {p in prd, t in time}:
+
+ Inv[p,t,1] <= Rprd[p,t] + Oprd[p,t];
+
+ # New inventory cannot exceed
+ # production in the most recent period
+
+ilim 'inventory limits' {p in prd, t in first+1..last, a in 2..life}:
+
+ Inv[p,t,a] <= Inv[p,t-1,a-1];
+
+ # Inventory left from period (t+1)-p
+ # can only decrease as time goes on
+
+### DATA ###
+
+data;
+
+set prd := 18REG 24REG 24PRO ;
+
+param first := 1 ;
+param last := 13 ;
+param life := 2 ;
+
+param cs := 18 ;
+param sl := 8 ;
+param iw := 8 ;
+
+param rtr := 16.00 ;
+param otr := 43.85 ;
+param rir := 0.75 ;
+param pir := 0.80 ;
+
+param : pt pc cri crs iinv :=
+
+ 18REG 1.194 2304. 0.015 1.100 82.0
+ 24REG 1.509 2920. 0.015 1.100 792.2
+ 24PRO 1.509 2910. 0.015 1.100 0.0 ;
+
+param : dpp ol cmin cmax hc lc :=
+
+ 1 19.5 96.0 0.0 8.0 7500 7500
+ 2 19.0 96.0 0.0 8.0 7500 7500
+ 3 20.0 96.0 0.0 8.0 7500 7500
+ 4 19.0 96.0 0.0 8.0 7500 7500
+ 5 19.5 96.0 0.0 8.0 15000 15000
+ 6 19.0 96.0 0.0 8.0 15000 15000
+ 7 19.0 96.0 0.0 8.0 15000 15000
+ 8 20.0 96.0 0.0 8.0 15000 15000
+ 9 19.0 96.0 0.0 8.0 15000 15000
+ 10 20.0 96.0 0.0 8.0 15000 15000
+ 11 20.0 96.0 0.0 8.0 7500 7500
+ 12 18.0 96.0 0.0 8.0 7500 7500
+ 13 18.0 96.0 0.0 8.0 7500 7500 ;
+
+param dem (tr) :
+
+ 18REG 24REG 24PRO :=
+
+ 1 63.8 1212.0 0.0
+ 2 76.0 306.2 0.0
+ 3 88.4 319.0 0.0
+ 4 913.8 208.4 0.0
+ 5 115.0 298.0 0.0
+ 6 133.8 328.2 0.0
+ 7 79.6 959.6 0.0
+ 8 111.0 257.6 0.0
+ 9 121.6 335.6 0.0
+ 10 470.0 118.0 1102.0
+ 11 78.4 284.8 0.0
+ 12 99.4 970.0 0.0
+ 13 140.4 343.8 0.0
+ 14 63.8 1212.0 0.0 ;
+
+param pro (tr) :
+
+ 18REG 24REG 24PRO :=
+
+ 1 0 1 0
+ 2 0 0 0
+ 3 0 0 0
+ 4 1 0 0
+ 5 0 0 0
+ 6 0 0 0
+ 7 0 1 0
+ 8 0 0 0
+ 9 0 0 0
+ 10 1 0 1
+ 11 0 0 0
+ 12 0 0 0
+ 13 0 1 0
+ 14 0 1 0 ;
+
+end;
diff --git a/test/monniaux/glpk-4.65/src/amd/COPYING b/test/monniaux/glpk-4.65/src/amd/COPYING
new file mode 100644
index 00000000..84bba36d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/COPYING
@@ -0,0 +1,502 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/test/monniaux/glpk-4.65/src/amd/README b/test/monniaux/glpk-4.65/src/amd/README
new file mode 100644
index 00000000..de950eb4
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/README
@@ -0,0 +1,58 @@
+NOTE: Files in this subdirectory are NOT part of the GLPK package, but
+ are used with GLPK.
+
+ The original code was modified according to GLPK requirements by
+ Andrew Makhorin <mao@gnu.org>.
+************************************************************************
+AMD Version 2.2, Copyright (C) 2007 by Timothy A. Davis,
+Patrick R. Amestoy, and Iain S. Duff. All Rights Reserved.
+
+Description:
+
+ AMD is a set of routines for pre-ordering sparse matrices prior to
+ Cholesky or LU factorization, using the approximate minimum degree
+ ordering algorithm. Written in ANSI/ISO C with a MATLAB interface,
+ and in Fortran 77.
+
+Authors:
+
+ Timothy A. Davis (davis at cise.ufl.edu), University of Florida.
+ Patrick R. Amestoy, ENSEEIHT, Toulouse, France.
+ Iain S. Duff, Rutherford Appleton Laboratory, UK.
+
+AMD License:
+
+ Your use or distribution of AMD or any modified version of AMD
+ implies that you agree to this License.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation; either version 2.1 of
+ the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA.
+
+ Permission is hereby granted to use or copy this program under the
+ terms of the GNU LGPL, provided that the Copyright, this License,
+ and the Availability of the original version is retained on all
+ copies. User documentation of any code that uses this code or any
+ modified version of this code must cite the Copyright, this License,
+ the Availability note, and "Used by permission." Permission to
+ modify the code and to distribute modified code is granted, provided
+ the Copyright, this License, and the Availability note are retained,
+ and a notice that the code was modified is included.
+
+ AMD is available under alternate licences; contact T. Davis for
+ details.
+
+Availability:
+
+ http://www.cise.ufl.edu/research/sparse/amd
diff --git a/test/monniaux/glpk-4.65/src/amd/amd.h b/test/monniaux/glpk-4.65/src/amd/amd.h
new file mode 100644
index 00000000..be662d95
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd.h
@@ -0,0 +1,67 @@
+/* amd.h */
+
+/* Written by Andrew Makhorin <mao@gnu.org>. */
+
+#ifndef GLPAMD_H
+#define GLPAMD_H
+
+#define AMD_DATE "May 31, 2007"
+#define AMD_VERSION_CODE(main, sub) ((main) * 1000 + (sub))
+#define AMD_MAIN_VERSION 2
+#define AMD_SUB_VERSION 2
+#define AMD_SUBSUB_VERSION 0
+#define AMD_VERSION AMD_VERSION_CODE(AMD_MAIN_VERSION, AMD_SUB_VERSION)
+
+#define AMD_CONTROL 5
+#define AMD_INFO 20
+
+#define AMD_DENSE 0
+#define AMD_AGGRESSIVE 1
+
+#define AMD_DEFAULT_DENSE 10.0
+#define AMD_DEFAULT_AGGRESSIVE 1
+
+#define AMD_STATUS 0
+#define AMD_N 1
+#define AMD_NZ 2
+#define AMD_SYMMETRY 3
+#define AMD_NZDIAG 4
+#define AMD_NZ_A_PLUS_AT 5
+#define AMD_NDENSE 6
+#define AMD_MEMORY 7
+#define AMD_NCMPA 8
+#define AMD_LNZ 9
+#define AMD_NDIV 10
+#define AMD_NMULTSUBS_LDL 11
+#define AMD_NMULTSUBS_LU 12
+#define AMD_DMAX 13
+
+#define AMD_OK 0
+#define AMD_OUT_OF_MEMORY (-1)
+#define AMD_INVALID (-2)
+#define AMD_OK_BUT_JUMBLED 1
+
+#define amd_order _glp_amd_order
+int amd_order(int n, const int Ap[], const int Ai[], int P[],
+ double Control[], double Info[]);
+
+#define amd_2 _glp_amd_2
+void amd_2(int n, int Pe[], int Iw[], int Len[], int iwlen, int pfree,
+ int Nv[], int Next[], int Last[], int Head[], int Elen[],
+ int Degree[], int W[], double Control[], double Info[]);
+
+#define amd_valid _glp_amd_valid
+int amd_valid(int n_row, int n_col, const int Ap[], const int Ai[]);
+
+#define amd_defaults _glp_amd_defaults
+void amd_defaults(double Control[]);
+
+#define amd_control _glp_amd_control
+void amd_control(double Control[]);
+
+#define amd_info _glp_amd_info
+void amd_info(double Info[]);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_1.c b/test/monniaux/glpk-4.65/src/amd/amd_1.c
new file mode 100644
index 00000000..4f9b07d7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_1.c
@@ -0,0 +1,181 @@
+/* ========================================================================= */
+/* === AMD_1 =============================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* AMD_1: Construct A+A' for a sparse matrix A and perform the AMD ordering.
+ *
+ * The n-by-n sparse matrix A can be unsymmetric. It is stored in MATLAB-style
+ * compressed-column form, with sorted row indices in each column, and no
+ * duplicate entries. Diagonal entries may be present, but they are ignored.
+ * Row indices of column j of A are stored in Ai [Ap [j] ... Ap [j+1]-1].
+ * Ap [0] must be zero, and nz = Ap [n] is the number of entries in A. The
+ * size of the matrix, n, must be greater than or equal to zero.
+ *
+ * This routine must be preceded by a call to AMD_aat, which computes the
+ * number of entries in each row/column in A+A', excluding the diagonal.
+ * Len [j], on input, is the number of entries in row/column j of A+A'. This
+ * routine constructs the matrix A+A' and then calls AMD_2. No error checking
+ * is performed (this was done in AMD_valid).
+ */
+
+#include "amd_internal.h"
+
+GLOBAL void AMD_1
+(
+ Int n, /* n > 0 */
+ const Int Ap [ ], /* input of size n+1, not modified */
+ const Int Ai [ ], /* input of size nz = Ap [n], not modified */
+ Int P [ ], /* size n output permutation */
+ Int Pinv [ ], /* size n output inverse permutation */
+ Int Len [ ], /* size n input, undefined on output */
+ Int slen, /* slen >= sum (Len [0..n-1]) + 7n,
+ * ideally slen = 1.2 * sum (Len) + 8n */
+ Int S [ ], /* size slen workspace */
+ double Control [ ], /* input array of size AMD_CONTROL */
+ double Info [ ] /* output array of size AMD_INFO */
+)
+{
+ Int i, j, k, p, pfree, iwlen, pj, p1, p2, pj2, *Iw, *Pe, *Nv, *Head,
+ *Elen, *Degree, *s, *W, *Sp, *Tp ;
+
+ /* --------------------------------------------------------------------- */
+ /* construct the matrix for AMD_2 */
+ /* --------------------------------------------------------------------- */
+
+ ASSERT (n > 0) ;
+
+ iwlen = slen - 6*n ;
+ s = S ;
+ Pe = s ; s += n ;
+ Nv = s ; s += n ;
+ Head = s ; s += n ;
+ Elen = s ; s += n ;
+ Degree = s ; s += n ;
+ W = s ; s += n ;
+ Iw = s ; s += iwlen ;
+
+ ASSERT (AMD_valid (n, n, Ap, Ai) == AMD_OK) ;
+
+ /* construct the pointers for A+A' */
+ Sp = Nv ; /* use Nv and W as workspace for Sp and Tp [ */
+ Tp = W ;
+ pfree = 0 ;
+ for (j = 0 ; j < n ; j++)
+ {
+ Pe [j] = pfree ;
+ Sp [j] = pfree ;
+ pfree += Len [j] ;
+ }
+
+ /* Note that this restriction on iwlen is slightly more restrictive than
+ * what is strictly required in AMD_2. AMD_2 can operate with no elbow
+ * room at all, but it will be very slow. For better performance, at
+ * least size-n elbow room is enforced. */
+ ASSERT (iwlen >= pfree + n) ;
+
+#ifndef NDEBUG
+ for (p = 0 ; p < iwlen ; p++) Iw [p] = EMPTY ;
+#endif
+
+ for (k = 0 ; k < n ; k++)
+ {
+ AMD_DEBUG1 (("Construct row/column k= "ID" of A+A'\n", k)) ;
+ p1 = Ap [k] ;
+ p2 = Ap [k+1] ;
+
+ /* construct A+A' */
+ for (p = p1 ; p < p2 ; )
+ {
+ /* scan the upper triangular part of A */
+ j = Ai [p] ;
+ ASSERT (j >= 0 && j < n) ;
+ if (j < k)
+ {
+ /* entry A (j,k) in the strictly upper triangular part */
+ ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
+ ASSERT (Sp [k] < (k == n-1 ? pfree : Pe [k+1])) ;
+ Iw [Sp [j]++] = k ;
+ Iw [Sp [k]++] = j ;
+ p++ ;
+ }
+ else if (j == k)
+ {
+ /* skip the diagonal */
+ p++ ;
+ break ;
+ }
+ else /* j > k */
+ {
+ /* first entry below the diagonal */
+ break ;
+ }
+ /* scan lower triangular part of A, in column j until reaching
+ * row k. Start where last scan left off. */
+ ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;
+ pj2 = Ap [j+1] ;
+ for (pj = Tp [j] ; pj < pj2 ; )
+ {
+ i = Ai [pj] ;
+ ASSERT (i >= 0 && i < n) ;
+ if (i < k)
+ {
+ /* A (i,j) is only in the lower part, not in upper */
+ ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
+ ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
+ Iw [Sp [i]++] = j ;
+ Iw [Sp [j]++] = i ;
+ pj++ ;
+ }
+ else if (i == k)
+ {
+ /* entry A (k,j) in lower part and A (j,k) in upper */
+ pj++ ;
+ break ;
+ }
+ else /* i > k */
+ {
+ /* consider this entry later, when k advances to i */
+ break ;
+ }
+ }
+ Tp [j] = pj ;
+ }
+ Tp [k] = p ;
+ }
+
+ /* clean up, for remaining mismatched entries */
+ for (j = 0 ; j < n ; j++)
+ {
+ for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)
+ {
+ i = Ai [pj] ;
+ ASSERT (i >= 0 && i < n) ;
+ /* A (i,j) is only in the lower part, not in upper */
+ ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
+ ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
+ Iw [Sp [i]++] = j ;
+ Iw [Sp [j]++] = i ;
+ }
+ }
+
+#ifndef NDEBUG
+ for (j = 0 ; j < n-1 ; j++) ASSERT (Sp [j] == Pe [j+1]) ;
+ ASSERT (Sp [n-1] == pfree) ;
+#endif
+
+ /* Tp and Sp no longer needed ] */
+
+ /* --------------------------------------------------------------------- */
+ /* order the matrix */
+ /* --------------------------------------------------------------------- */
+
+ AMD_2 (n, Pe, Iw, Len, iwlen, pfree,
+ Nv, Pinv, P, Head, Elen, Degree, W, Control, Info) ;
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_2.c b/test/monniaux/glpk-4.65/src/amd/amd_2.c
new file mode 100644
index 00000000..36ae828a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_2.c
@@ -0,0 +1,1842 @@
+/* ========================================================================= */
+/* === AMD_2 =============================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* AMD_2: performs the AMD ordering on a symmetric sparse matrix A, followed
+ * by a postordering (via depth-first search) of the assembly tree using the
+ * AMD_postorder routine.
+ */
+
+#include "amd_internal.h"
+
+/* ========================================================================= */
+/* === clear_flag ========================================================== */
+/* ========================================================================= */
+
+static Int clear_flag (Int wflg, Int wbig, Int W [ ], Int n)
+{
+ Int x ;
+ if (wflg < 2 || wflg >= wbig)
+ {
+ for (x = 0 ; x < n ; x++)
+ {
+ if (W [x] != 0) W [x] = 1 ;
+ }
+ wflg = 2 ;
+ }
+ /* at this point, W [0..n-1] < wflg holds */
+ return (wflg) ;
+}
+
+
+/* ========================================================================= */
+/* === AMD_2 =============================================================== */
+/* ========================================================================= */
+
+GLOBAL void AMD_2
+(
+ Int n, /* A is n-by-n, where n > 0 */
+ Int Pe [ ], /* Pe [0..n-1]: index in Iw of row i on input */
+ Int Iw [ ], /* workspace of size iwlen. Iw [0..pfree-1]
+ * holds the matrix on input */
+ Int Len [ ], /* Len [0..n-1]: length for row/column i on input */
+ Int iwlen, /* length of Iw. iwlen >= pfree + n */
+ Int pfree, /* Iw [pfree ... iwlen-1] is empty on input */
+
+ /* 7 size-n workspaces, not defined on input: */
+ Int Nv [ ], /* the size of each supernode on output */
+ Int Next [ ], /* the output inverse permutation */
+ Int Last [ ], /* the output permutation */
+ Int Head [ ],
+ Int Elen [ ], /* the size columns of L for each supernode */
+ Int Degree [ ],
+ Int W [ ],
+
+ /* control parameters and output statistics */
+ double Control [ ], /* array of size AMD_CONTROL */
+ double Info [ ] /* array of size AMD_INFO */
+)
+{
+
+/*
+ * Given a representation of the nonzero pattern of a symmetric matrix, A,
+ * (excluding the diagonal) perform an approximate minimum (UMFPACK/MA38-style)
+ * degree ordering to compute a pivot order such that the introduction of
+ * nonzeros (fill-in) in the Cholesky factors A = LL' is kept low. At each
+ * step, the pivot selected is the one with the minimum UMFAPACK/MA38-style
+ * upper-bound on the external degree. This routine can optionally perform
+ * aggresive absorption (as done by MC47B in the Harwell Subroutine
+ * Library).
+ *
+ * The approximate degree algorithm implemented here is the symmetric analog of
+ * the degree update algorithm in MA38 and UMFPACK (the Unsymmetric-pattern
+ * MultiFrontal PACKage, both by Davis and Duff). The routine is based on the
+ * MA27 minimum degree ordering algorithm by Iain Duff and John Reid.
+ *
+ * This routine is a translation of the original AMDBAR and MC47B routines,
+ * in Fortran, with the following modifications:
+ *
+ * (1) dense rows/columns are removed prior to ordering the matrix, and placed
+ * last in the output order. The presence of a dense row/column can
+ * increase the ordering time by up to O(n^2), unless they are removed
+ * prior to ordering.
+ *
+ * (2) the minimum degree ordering is followed by a postordering (depth-first
+ * search) of the assembly tree. Note that mass elimination (discussed
+ * below) combined with the approximate degree update can lead to the mass
+ * elimination of nodes with lower exact degree than the current pivot
+ * element. No additional fill-in is caused in the representation of the
+ * Schur complement. The mass-eliminated nodes merge with the current
+ * pivot element. They are ordered prior to the current pivot element.
+ * Because they can have lower exact degree than the current element, the
+ * merger of two or more of these nodes in the current pivot element can
+ * lead to a single element that is not a "fundamental supernode". The
+ * diagonal block can have zeros in it. Thus, the assembly tree used here
+ * is not guaranteed to be the precise supernodal elemination tree (with
+ * "funadmental" supernodes), and the postordering performed by this
+ * routine is not guaranteed to be a precise postordering of the
+ * elimination tree.
+ *
+ * (3) input parameters are added, to control aggressive absorption and the
+ * detection of "dense" rows/columns of A.
+ *
+ * (4) additional statistical information is returned, such as the number of
+ * nonzeros in L, and the flop counts for subsequent LDL' and LU
+ * factorizations. These are slight upper bounds, because of the mass
+ * elimination issue discussed above.
+ *
+ * (5) additional routines are added to interface this routine to MATLAB
+ * to provide a simple C-callable user-interface, to check inputs for
+ * errors, compute the symmetry of the pattern of A and the number of
+ * nonzeros in each row/column of A+A', to compute the pattern of A+A',
+ * to perform the assembly tree postordering, and to provide debugging
+ * ouput. Many of these functions are also provided by the Fortran
+ * Harwell Subroutine Library routine MC47A.
+ *
+ * (6) both int and UF_long versions are provided. In the descriptions below
+ * and integer is and int or UF_long depending on which version is
+ * being used.
+
+ **********************************************************************
+ ***** CAUTION: ARGUMENTS ARE NOT CHECKED FOR ERRORS ON INPUT. ******
+ **********************************************************************
+ ** If you want error checking, a more versatile input format, and a **
+ ** simpler user interface, use amd_order or amd_l_order instead. **
+ ** This routine is not meant to be user-callable. **
+ **********************************************************************
+
+ * ----------------------------------------------------------------------------
+ * References:
+ * ----------------------------------------------------------------------------
+ *
+ * [1] Timothy A. Davis and Iain Duff, "An unsymmetric-pattern multifrontal
+ * method for sparse LU factorization", SIAM J. Matrix Analysis and
+ * Applications, vol. 18, no. 1, pp. 140-158. Discusses UMFPACK / MA38,
+ * which first introduced the approximate minimum degree used by this
+ * routine.
+ *
+ * [2] Patrick Amestoy, Timothy A. Davis, and Iain S. Duff, "An approximate
+ * minimum degree ordering algorithm," SIAM J. Matrix Analysis and
+ * Applications, vol. 17, no. 4, pp. 886-905, 1996. Discusses AMDBAR and
+ * MC47B, which are the Fortran versions of this routine.
+ *
+ * [3] Alan George and Joseph Liu, "The evolution of the minimum degree
+ * ordering algorithm," SIAM Review, vol. 31, no. 1, pp. 1-19, 1989.
+ * We list below the features mentioned in that paper that this code
+ * includes:
+ *
+ * mass elimination:
+ * Yes. MA27 relied on supervariable detection for mass elimination.
+ *
+ * indistinguishable nodes:
+ * Yes (we call these "supervariables"). This was also in the MA27
+ * code - although we modified the method of detecting them (the
+ * previous hash was the true degree, which we no longer keep track
+ * of). A supervariable is a set of rows with identical nonzero
+ * pattern. All variables in a supervariable are eliminated together.
+ * Each supervariable has as its numerical name that of one of its
+ * variables (its principal variable).
+ *
+ * quotient graph representation:
+ * Yes. We use the term "element" for the cliques formed during
+ * elimination. This was also in the MA27 code. The algorithm can
+ * operate in place, but it will work more efficiently if given some
+ * "elbow room."
+ *
+ * element absorption:
+ * Yes. This was also in the MA27 code.
+ *
+ * external degree:
+ * Yes. The MA27 code was based on the true degree.
+ *
+ * incomplete degree update and multiple elimination:
+ * No. This was not in MA27, either. Our method of degree update
+ * within MC47B is element-based, not variable-based. It is thus
+ * not well-suited for use with incomplete degree update or multiple
+ * elimination.
+ *
+ * Authors, and Copyright (C) 2004 by:
+ * Timothy A. Davis, Patrick Amestoy, Iain S. Duff, John K. Reid.
+ *
+ * Acknowledgements: This work (and the UMFPACK package) was supported by the
+ * National Science Foundation (ASC-9111263, DMS-9223088, and CCR-0203270).
+ * The UMFPACK/MA38 approximate degree update algorithm, the unsymmetric analog
+ * which forms the basis of AMD, was developed while Tim Davis was supported by
+ * CERFACS (Toulouse, France) in a post-doctoral position. This C version, and
+ * the etree postorder, were written while Tim Davis was on sabbatical at
+ * Stanford University and Lawrence Berkeley National Laboratory.
+
+ * ----------------------------------------------------------------------------
+ * INPUT ARGUMENTS (unaltered):
+ * ----------------------------------------------------------------------------
+
+ * n: The matrix order. Restriction: n >= 1.
+ *
+ * iwlen: The size of the Iw array. On input, the matrix is stored in
+ * Iw [0..pfree-1]. However, Iw [0..iwlen-1] should be slightly larger
+ * than what is required to hold the matrix, at least iwlen >= pfree + n.
+ * Otherwise, excessive compressions will take place. The recommended
+ * value of iwlen is 1.2 * pfree + n, which is the value used in the
+ * user-callable interface to this routine (amd_order.c). The algorithm
+ * will not run at all if iwlen < pfree. Restriction: iwlen >= pfree + n.
+ * Note that this is slightly more restrictive than the actual minimum
+ * (iwlen >= pfree), but AMD_2 will be very slow with no elbow room.
+ * Thus, this routine enforces a bare minimum elbow room of size n.
+ *
+ * pfree: On input the tail end of the array, Iw [pfree..iwlen-1], is empty,
+ * and the matrix is stored in Iw [0..pfree-1]. During execution,
+ * additional data is placed in Iw, and pfree is modified so that
+ * Iw [pfree..iwlen-1] is always the unused part of Iw.
+ *
+ * Control: A double array of size AMD_CONTROL containing input parameters
+ * that affect how the ordering is computed. If NULL, then default
+ * settings are used.
+ *
+ * Control [AMD_DENSE] is used to determine whether or not a given input
+ * row is "dense". A row is "dense" if the number of entries in the row
+ * exceeds Control [AMD_DENSE] times sqrt (n), except that rows with 16 or
+ * fewer entries are never considered "dense". To turn off the detection
+ * of dense rows, set Control [AMD_DENSE] to a negative number, or to a
+ * number larger than sqrt (n). The default value of Control [AMD_DENSE]
+ * is AMD_DEFAULT_DENSE, which is defined in amd.h as 10.
+ *
+ * Control [AMD_AGGRESSIVE] is used to determine whether or not aggressive
+ * absorption is to be performed. If nonzero, then aggressive absorption
+ * is performed (this is the default).
+
+ * ----------------------------------------------------------------------------
+ * INPUT/OUPUT ARGUMENTS:
+ * ----------------------------------------------------------------------------
+ *
+ * Pe: An integer array of size n. On input, Pe [i] is the index in Iw of
+ * the start of row i. Pe [i] is ignored if row i has no off-diagonal
+ * entries. Thus Pe [i] must be in the range 0 to pfree-1 for non-empty
+ * rows.
+ *
+ * During execution, it is used for both supervariables and elements:
+ *
+ * Principal supervariable i: index into Iw of the description of
+ * supervariable i. A supervariable represents one or more rows of
+ * the matrix with identical nonzero pattern. In this case,
+ * Pe [i] >= 0.
+ *
+ * Non-principal supervariable i: if i has been absorbed into another
+ * supervariable j, then Pe [i] = FLIP (j), where FLIP (j) is defined
+ * as (-(j)-2). Row j has the same pattern as row i. Note that j
+ * might later be absorbed into another supervariable j2, in which
+ * case Pe [i] is still FLIP (j), and Pe [j] = FLIP (j2) which is
+ * < EMPTY, where EMPTY is defined as (-1) in amd_internal.h.
+ *
+ * Unabsorbed element e: the index into Iw of the description of element
+ * e, if e has not yet been absorbed by a subsequent element. Element
+ * e is created when the supervariable of the same name is selected as
+ * the pivot. In this case, Pe [i] >= 0.
+ *
+ * Absorbed element e: if element e is absorbed into element e2, then
+ * Pe [e] = FLIP (e2). This occurs when the pattern of e (which we
+ * refer to as Le) is found to be a subset of the pattern of e2 (that
+ * is, Le2). In this case, Pe [i] < EMPTY. If element e is "null"
+ * (it has no nonzeros outside its pivot block), then Pe [e] = EMPTY,
+ * and e is the root of an assembly subtree (or the whole tree if
+ * there is just one such root).
+ *
+ * Dense variable i: if i is "dense", then Pe [i] = EMPTY.
+ *
+ * On output, Pe holds the assembly tree/forest, which implicitly
+ * represents a pivot order with identical fill-in as the actual order
+ * (via a depth-first search of the tree), as follows. If Nv [i] > 0,
+ * then i represents a node in the assembly tree, and the parent of i is
+ * Pe [i], or EMPTY if i is a root. If Nv [i] = 0, then (i, Pe [i])
+ * represents an edge in a subtree, the root of which is a node in the
+ * assembly tree. Note that i refers to a row/column in the original
+ * matrix, not the permuted matrix.
+ *
+ * Info: A double array of size AMD_INFO. If present, (that is, not NULL),
+ * then statistics about the ordering are returned in the Info array.
+ * See amd.h for a description.
+
+ * ----------------------------------------------------------------------------
+ * INPUT/MODIFIED (undefined on output):
+ * ----------------------------------------------------------------------------
+ *
+ * Len: An integer array of size n. On input, Len [i] holds the number of
+ * entries in row i of the matrix, excluding the diagonal. The contents
+ * of Len are undefined on output.
+ *
+ * Iw: An integer array of size iwlen. On input, Iw [0..pfree-1] holds the
+ * description of each row i in the matrix. The matrix must be symmetric,
+ * and both upper and lower triangular parts must be present. The
+ * diagonal must not be present. Row i is held as follows:
+ *
+ * Len [i]: the length of the row i data structure in the Iw array.
+ * Iw [Pe [i] ... Pe [i] + Len [i] - 1]:
+ * the list of column indices for nonzeros in row i (simple
+ * supervariables), excluding the diagonal. All supervariables
+ * start with one row/column each (supervariable i is just row i).
+ * If Len [i] is zero on input, then Pe [i] is ignored on input.
+ *
+ * Note that the rows need not be in any particular order, and there
+ * may be empty space between the rows.
+ *
+ * During execution, the supervariable i experiences fill-in. This is
+ * represented by placing in i a list of the elements that cause fill-in
+ * in supervariable i:
+ *
+ * Len [i]: the length of supervariable i in the Iw array.
+ * Iw [Pe [i] ... Pe [i] + Elen [i] - 1]:
+ * the list of elements that contain i. This list is kept short
+ * by removing absorbed elements.
+ * Iw [Pe [i] + Elen [i] ... Pe [i] + Len [i] - 1]:
+ * the list of supervariables in i. This list is kept short by
+ * removing nonprincipal variables, and any entry j that is also
+ * contained in at least one of the elements (j in Le) in the list
+ * for i (e in row i).
+ *
+ * When supervariable i is selected as pivot, we create an element e of
+ * the same name (e=i):
+ *
+ * Len [e]: the length of element e in the Iw array.
+ * Iw [Pe [e] ... Pe [e] + Len [e] - 1]:
+ * the list of supervariables in element e.
+ *
+ * An element represents the fill-in that occurs when supervariable i is
+ * selected as pivot (which represents the selection of row i and all
+ * non-principal variables whose principal variable is i). We use the
+ * term Le to denote the set of all supervariables in element e. Absorbed
+ * supervariables and elements are pruned from these lists when
+ * computationally convenient.
+ *
+ * CAUTION: THE INPUT MATRIX IS OVERWRITTEN DURING COMPUTATION.
+ * The contents of Iw are undefined on output.
+
+ * ----------------------------------------------------------------------------
+ * OUTPUT (need not be set on input):
+ * ----------------------------------------------------------------------------
+ *
+ * Nv: An integer array of size n. During execution, ABS (Nv [i]) is equal to
+ * the number of rows that are represented by the principal supervariable
+ * i. If i is a nonprincipal or dense variable, then Nv [i] = 0.
+ * Initially, Nv [i] = 1 for all i. Nv [i] < 0 signifies that i is a
+ * principal variable in the pattern Lme of the current pivot element me.
+ * After element me is constructed, Nv [i] is set back to a positive
+ * value.
+ *
+ * On output, Nv [i] holds the number of pivots represented by super
+ * row/column i of the original matrix, or Nv [i] = 0 for non-principal
+ * rows/columns. Note that i refers to a row/column in the original
+ * matrix, not the permuted matrix.
+ *
+ * Elen: An integer array of size n. See the description of Iw above. At the
+ * start of execution, Elen [i] is set to zero for all rows i. During
+ * execution, Elen [i] is the number of elements in the list for
+ * supervariable i. When e becomes an element, Elen [e] = FLIP (esize) is
+ * set, where esize is the size of the element (the number of pivots, plus
+ * the number of nonpivotal entries). Thus Elen [e] < EMPTY.
+ * Elen (i) = EMPTY set when variable i becomes nonprincipal.
+ *
+ * For variables, Elen (i) >= EMPTY holds until just before the
+ * postordering and permutation vectors are computed. For elements,
+ * Elen [e] < EMPTY holds.
+ *
+ * On output, Elen [i] is the degree of the row/column in the Cholesky
+ * factorization of the permuted matrix, corresponding to the original row
+ * i, if i is a super row/column. It is equal to EMPTY if i is
+ * non-principal. Note that i refers to a row/column in the original
+ * matrix, not the permuted matrix.
+ *
+ * Note that the contents of Elen on output differ from the Fortran
+ * version (Elen holds the inverse permutation in the Fortran version,
+ * which is instead returned in the Next array in this C version,
+ * described below).
+ *
+ * Last: In a degree list, Last [i] is the supervariable preceding i, or EMPTY
+ * if i is the head of the list. In a hash bucket, Last [i] is the hash
+ * key for i.
+ *
+ * Last [Head [hash]] is also used as the head of a hash bucket if
+ * Head [hash] contains a degree list (see the description of Head,
+ * below).
+ *
+ * On output, Last [0..n-1] holds the permutation. That is, if
+ * i = Last [k], then row i is the kth pivot row (where k ranges from 0 to
+ * n-1). Row Last [k] of A is the kth row in the permuted matrix, PAP'.
+ *
+ * Next: Next [i] is the supervariable following i in a link list, or EMPTY if
+ * i is the last in the list. Used for two kinds of lists: degree lists
+ * and hash buckets (a supervariable can be in only one kind of list at a
+ * time).
+ *
+ * On output Next [0..n-1] holds the inverse permutation. That is, if
+ * k = Next [i], then row i is the kth pivot row. Row i of A appears as
+ * the (Next[i])-th row in the permuted matrix, PAP'.
+ *
+ * Note that the contents of Next on output differ from the Fortran
+ * version (Next is undefined on output in the Fortran version).
+
+ * ----------------------------------------------------------------------------
+ * LOCAL WORKSPACE (not input or output - used only during execution):
+ * ----------------------------------------------------------------------------
+ *
+ * Degree: An integer array of size n. If i is a supervariable, then
+ * Degree [i] holds the current approximation of the external degree of
+ * row i (an upper bound). The external degree is the number of nonzeros
+ * in row i, minus ABS (Nv [i]), the diagonal part. The bound is equal to
+ * the exact external degree if Elen [i] is less than or equal to two.
+ *
+ * We also use the term "external degree" for elements e to refer to
+ * |Le \ Lme|. If e is an element, then Degree [e] is |Le|, which is the
+ * degree of the off-diagonal part of the element e (not including the
+ * diagonal part).
+ *
+ * Head: An integer array of size n. Head is used for degree lists.
+ * Head [deg] is the first supervariable in a degree list. All
+ * supervariables i in a degree list Head [deg] have the same approximate
+ * degree, namely, deg = Degree [i]. If the list Head [deg] is empty then
+ * Head [deg] = EMPTY.
+ *
+ * During supervariable detection Head [hash] also serves as a pointer to
+ * a hash bucket. If Head [hash] >= 0, there is a degree list of degree
+ * hash. The hash bucket head pointer is Last [Head [hash]]. If
+ * Head [hash] = EMPTY, then the degree list and hash bucket are both
+ * empty. If Head [hash] < EMPTY, then the degree list is empty, and
+ * FLIP (Head [hash]) is the head of the hash bucket. After supervariable
+ * detection is complete, all hash buckets are empty, and the
+ * (Last [Head [hash]] = EMPTY) condition is restored for the non-empty
+ * degree lists.
+ *
+ * W: An integer array of size n. The flag array W determines the status of
+ * elements and variables, and the external degree of elements.
+ *
+ * for elements:
+ * if W [e] = 0, then the element e is absorbed.
+ * if W [e] >= wflg, then W [e] - wflg is the size of the set
+ * |Le \ Lme|, in terms of nonzeros (the sum of ABS (Nv [i]) for
+ * each principal variable i that is both in the pattern of
+ * element e and NOT in the pattern of the current pivot element,
+ * me).
+ * if wflg > W [e] > 0, then e is not absorbed and has not yet been
+ * seen in the scan of the element lists in the computation of
+ * |Le\Lme| in Scan 1 below.
+ *
+ * for variables:
+ * during supervariable detection, if W [j] != wflg then j is
+ * not in the pattern of variable i.
+ *
+ * The W array is initialized by setting W [i] = 1 for all i, and by
+ * setting wflg = 2. It is reinitialized if wflg becomes too large (to
+ * ensure that wflg+n does not cause integer overflow).
+
+ * ----------------------------------------------------------------------------
+ * LOCAL INTEGERS:
+ * ----------------------------------------------------------------------------
+ */
+
+ Int deg, degme, dext, lemax, e, elenme, eln, i, ilast, inext, j,
+ jlast, jnext, k, knt1, knt2, knt3, lenj, ln, me, mindeg, nel, nleft,
+ nvi, nvj, nvpiv, slenme, wbig, we, wflg, wnvi, ok, ndense, ncmpa,
+ dense, aggressive ;
+
+ unsigned Int hash ; /* unsigned, so that hash % n is well defined.*/
+
+/*
+ * deg: the degree of a variable or element
+ * degme: size, |Lme|, of the current element, me (= Degree [me])
+ * dext: external degree, |Le \ Lme|, of some element e
+ * lemax: largest |Le| seen so far (called dmax in Fortran version)
+ * e: an element
+ * elenme: the length, Elen [me], of element list of pivotal variable
+ * eln: the length, Elen [...], of an element list
+ * hash: the computed value of the hash function
+ * i: a supervariable
+ * ilast: the entry in a link list preceding i
+ * inext: the entry in a link list following i
+ * j: a supervariable
+ * jlast: the entry in a link list preceding j
+ * jnext: the entry in a link list, or path, following j
+ * k: the pivot order of an element or variable
+ * knt1: loop counter used during element construction
+ * knt2: loop counter used during element construction
+ * knt3: loop counter used during compression
+ * lenj: Len [j]
+ * ln: length of a supervariable list
+ * me: current supervariable being eliminated, and the current
+ * element created by eliminating that supervariable
+ * mindeg: current minimum degree
+ * nel: number of pivots selected so far
+ * nleft: n - nel, the number of nonpivotal rows/columns remaining
+ * nvi: the number of variables in a supervariable i (= Nv [i])
+ * nvj: the number of variables in a supervariable j (= Nv [j])
+ * nvpiv: number of pivots in current element
+ * slenme: number of variables in variable list of pivotal variable
+ * wbig: = INT_MAX - n for the int version, UF_long_max - n for the
+ * UF_long version. wflg is not allowed to be >= wbig.
+ * we: W [e]
+ * wflg: used for flagging the W array. See description of Iw.
+ * wnvi: wflg - Nv [i]
+ * x: either a supervariable or an element
+ *
+ * ok: true if supervariable j can be absorbed into i
+ * ndense: number of "dense" rows/columns
+ * dense: rows/columns with initial degree > dense are considered "dense"
+ * aggressive: true if aggressive absorption is being performed
+ * ncmpa: number of garbage collections
+
+ * ----------------------------------------------------------------------------
+ * LOCAL DOUBLES, used for statistical output only (except for alpha):
+ * ----------------------------------------------------------------------------
+ */
+
+ double f, r, ndiv, s, nms_lu, nms_ldl, dmax, alpha, lnz, lnzme ;
+
+/*
+ * f: nvpiv
+ * r: degme + nvpiv
+ * ndiv: number of divisions for LU or LDL' factorizations
+ * s: number of multiply-subtract pairs for LU factorization, for the
+ * current element me
+ * nms_lu number of multiply-subtract pairs for LU factorization
+ * nms_ldl number of multiply-subtract pairs for LDL' factorization
+ * dmax: the largest number of entries in any column of L, including the
+ * diagonal
+ * alpha: "dense" degree ratio
+ * lnz: the number of nonzeros in L (excluding the diagonal)
+ * lnzme: the number of nonzeros in L (excl. the diagonal) for the
+ * current element me
+
+ * ----------------------------------------------------------------------------
+ * LOCAL "POINTERS" (indices into the Iw array)
+ * ----------------------------------------------------------------------------
+*/
+
+ Int p, p1, p2, p3, p4, pdst, pend, pj, pme, pme1, pme2, pn, psrc ;
+
+/*
+ * Any parameter (Pe [...] or pfree) or local variable starting with "p" (for
+ * Pointer) is an index into Iw, and all indices into Iw use variables starting
+ * with "p." The only exception to this rule is the iwlen input argument.
+ *
+ * p: pointer into lots of things
+ * p1: Pe [i] for some variable i (start of element list)
+ * p2: Pe [i] + Elen [i] - 1 for some variable i
+ * p3: index of first supervariable in clean list
+ * p4:
+ * pdst: destination pointer, for compression
+ * pend: end of memory to compress
+ * pj: pointer into an element or variable
+ * pme: pointer into the current element (pme1...pme2)
+ * pme1: the current element, me, is stored in Iw [pme1...pme2]
+ * pme2: the end of the current element
+ * pn: pointer into a "clean" variable, also used to compress
+ * psrc: source pointer, for compression
+*/
+
+/* ========================================================================= */
+/* INITIALIZATIONS */
+/* ========================================================================= */
+
+ /* Note that this restriction on iwlen is slightly more restrictive than
+ * what is actually required in AMD_2. AMD_2 can operate with no elbow
+ * room at all, but it will be slow. For better performance, at least
+ * size-n elbow room is enforced. */
+ ASSERT (iwlen >= pfree + n) ;
+ ASSERT (n > 0) ;
+
+ /* initialize output statistics */
+ lnz = 0 ;
+ ndiv = 0 ;
+ nms_lu = 0 ;
+ nms_ldl = 0 ;
+ dmax = 1 ;
+ me = EMPTY ;
+
+ mindeg = 0 ;
+ ncmpa = 0 ;
+ nel = 0 ;
+ lemax = 0 ;
+
+ /* get control parameters */
+ if (Control != (double *) NULL)
+ {
+ alpha = Control [AMD_DENSE] ;
+ aggressive = (Control [AMD_AGGRESSIVE] != 0) ;
+ }
+ else
+ {
+ alpha = AMD_DEFAULT_DENSE ;
+ aggressive = AMD_DEFAULT_AGGRESSIVE ;
+ }
+ /* Note: if alpha is NaN, this is undefined: */
+ if (alpha < 0)
+ {
+ /* only remove completely dense rows/columns */
+ dense = n-2 ;
+ }
+ else
+ {
+ dense = alpha * sqrt ((double) n) ;
+ }
+ dense = MAX (16, dense) ;
+ dense = MIN (n, dense) ;
+ AMD_DEBUG1 (("\n\nAMD (debug), alpha %g, aggr. "ID"\n",
+ alpha, aggressive)) ;
+
+ for (i = 0 ; i < n ; i++)
+ {
+ Last [i] = EMPTY ;
+ Head [i] = EMPTY ;
+ Next [i] = EMPTY ;
+ /* if separate Hhead array is used for hash buckets: *
+ Hhead [i] = EMPTY ;
+ */
+ Nv [i] = 1 ;
+ W [i] = 1 ;
+ Elen [i] = 0 ;
+ Degree [i] = Len [i] ;
+ }
+
+#ifndef NDEBUG
+ AMD_DEBUG1 (("\n======Nel "ID" initial\n", nel)) ;
+ AMD_dump (n, Pe, Iw, Len, iwlen, pfree, Nv, Next, Last,
+ Head, Elen, Degree, W, -1) ;
+#endif
+
+ /* initialize wflg */
+ wbig = Int_MAX - n ;
+ wflg = clear_flag (0, wbig, W, n) ;
+
+ /* --------------------------------------------------------------------- */
+ /* initialize degree lists and eliminate dense and empty rows */
+ /* --------------------------------------------------------------------- */
+
+ ndense = 0 ;
+
+ for (i = 0 ; i < n ; i++)
+ {
+ deg = Degree [i] ;
+ ASSERT (deg >= 0 && deg < n) ;
+ if (deg == 0)
+ {
+
+ /* -------------------------------------------------------------
+ * we have a variable that can be eliminated at once because
+ * there is no off-diagonal non-zero in its row. Note that
+ * Nv [i] = 1 for an empty variable i. It is treated just
+ * the same as an eliminated element i.
+ * ------------------------------------------------------------- */
+
+ Elen [i] = FLIP (1) ;
+ nel++ ;
+ Pe [i] = EMPTY ;
+ W [i] = 0 ;
+
+ }
+ else if (deg > dense)
+ {
+
+ /* -------------------------------------------------------------
+ * Dense variables are not treated as elements, but as unordered,
+ * non-principal variables that have no parent. They do not take
+ * part in the postorder, since Nv [i] = 0. Note that the Fortran
+ * version does not have this option.
+ * ------------------------------------------------------------- */
+
+ AMD_DEBUG1 (("Dense node "ID" degree "ID"\n", i, deg)) ;
+ ndense++ ;
+ Nv [i] = 0 ; /* do not postorder this node */
+ Elen [i] = EMPTY ;
+ nel++ ;
+ Pe [i] = EMPTY ;
+
+ }
+ else
+ {
+
+ /* -------------------------------------------------------------
+ * place i in the degree list corresponding to its degree
+ * ------------------------------------------------------------- */
+
+ inext = Head [deg] ;
+ ASSERT (inext >= EMPTY && inext < n) ;
+ if (inext != EMPTY) Last [inext] = i ;
+ Next [i] = inext ;
+ Head [deg] = i ;
+
+ }
+ }
+
+/* ========================================================================= */
+/* WHILE (selecting pivots) DO */
+/* ========================================================================= */
+
+ while (nel < n)
+ {
+
+#ifndef NDEBUG
+ AMD_DEBUG1 (("\n======Nel "ID"\n", nel)) ;
+ if (AMD_debug >= 2)
+ {
+ AMD_dump (n, Pe, Iw, Len, iwlen, pfree, Nv, Next,
+ Last, Head, Elen, Degree, W, nel) ;
+ }
+#endif
+
+/* ========================================================================= */
+/* GET PIVOT OF MINIMUM DEGREE */
+/* ========================================================================= */
+
+ /* ----------------------------------------------------------------- */
+ /* find next supervariable for elimination */
+ /* ----------------------------------------------------------------- */
+
+ ASSERT (mindeg >= 0 && mindeg < n) ;
+ for (deg = mindeg ; deg < n ; deg++)
+ {
+ me = Head [deg] ;
+ if (me != EMPTY) break ;
+ }
+ mindeg = deg ;
+ ASSERT (me >= 0 && me < n) ;
+ AMD_DEBUG1 (("=================me: "ID"\n", me)) ;
+
+ /* ----------------------------------------------------------------- */
+ /* remove chosen variable from link list */
+ /* ----------------------------------------------------------------- */
+
+ inext = Next [me] ;
+ ASSERT (inext >= EMPTY && inext < n) ;
+ if (inext != EMPTY) Last [inext] = EMPTY ;
+ Head [deg] = inext ;
+
+ /* ----------------------------------------------------------------- */
+ /* me represents the elimination of pivots nel to nel+Nv[me]-1. */
+ /* place me itself as the first in this set. */
+ /* ----------------------------------------------------------------- */
+
+ elenme = Elen [me] ;
+ nvpiv = Nv [me] ;
+ ASSERT (nvpiv > 0) ;
+ nel += nvpiv ;
+
+/* ========================================================================= */
+/* CONSTRUCT NEW ELEMENT */
+/* ========================================================================= */
+
+ /* -----------------------------------------------------------------
+ * At this point, me is the pivotal supervariable. It will be
+ * converted into the current element. Scan list of the pivotal
+ * supervariable, me, setting tree pointers and constructing new list
+ * of supervariables for the new element, me. p is a pointer to the
+ * current position in the old list.
+ * ----------------------------------------------------------------- */
+
+ /* flag the variable "me" as being in Lme by negating Nv [me] */
+ Nv [me] = -nvpiv ;
+ degme = 0 ;
+ ASSERT (Pe [me] >= 0 && Pe [me] < iwlen) ;
+
+ if (elenme == 0)
+ {
+
+ /* ------------------------------------------------------------- */
+ /* construct the new element in place */
+ /* ------------------------------------------------------------- */
+
+ pme1 = Pe [me] ;
+ pme2 = pme1 - 1 ;
+
+ for (p = pme1 ; p <= pme1 + Len [me] - 1 ; p++)
+ {
+ i = Iw [p] ;
+ ASSERT (i >= 0 && i < n && Nv [i] >= 0) ;
+ nvi = Nv [i] ;
+ if (nvi > 0)
+ {
+
+ /* ----------------------------------------------------- */
+ /* i is a principal variable not yet placed in Lme. */
+ /* store i in new list */
+ /* ----------------------------------------------------- */
+
+ /* flag i as being in Lme by negating Nv [i] */
+ degme += nvi ;
+ Nv [i] = -nvi ;
+ Iw [++pme2] = i ;
+
+ /* ----------------------------------------------------- */
+ /* remove variable i from degree list. */
+ /* ----------------------------------------------------- */
+
+ ilast = Last [i] ;
+ inext = Next [i] ;
+ ASSERT (ilast >= EMPTY && ilast < n) ;
+ ASSERT (inext >= EMPTY && inext < n) ;
+ if (inext != EMPTY) Last [inext] = ilast ;
+ if (ilast != EMPTY)
+ {
+ Next [ilast] = inext ;
+ }
+ else
+ {
+ /* i is at the head of the degree list */
+ ASSERT (Degree [i] >= 0 && Degree [i] < n) ;
+ Head [Degree [i]] = inext ;
+ }
+ }
+ }
+ }
+ else
+ {
+
+ /* ------------------------------------------------------------- */
+ /* construct the new element in empty space, Iw [pfree ...] */
+ /* ------------------------------------------------------------- */
+
+ p = Pe [me] ;
+ pme1 = pfree ;
+ slenme = Len [me] - elenme ;
+
+ for (knt1 = 1 ; knt1 <= elenme + 1 ; knt1++)
+ {
+
+ if (knt1 > elenme)
+ {
+ /* search the supervariables in me. */
+ e = me ;
+ pj = p ;
+ ln = slenme ;
+ AMD_DEBUG2 (("Search sv: "ID" "ID" "ID"\n", me,pj,ln)) ;
+ }
+ else
+ {
+ /* search the elements in me. */
+ e = Iw [p++] ;
+ ASSERT (e >= 0 && e < n) ;
+ pj = Pe [e] ;
+ ln = Len [e] ;
+ AMD_DEBUG2 (("Search element e "ID" in me "ID"\n", e,me)) ;
+ ASSERT (Elen [e] < EMPTY && W [e] > 0 && pj >= 0) ;
+ }
+ ASSERT (ln >= 0 && (ln == 0 || (pj >= 0 && pj < iwlen))) ;
+
+ /* ---------------------------------------------------------
+ * search for different supervariables and add them to the
+ * new list, compressing when necessary. this loop is
+ * executed once for each element in the list and once for
+ * all the supervariables in the list.
+ * --------------------------------------------------------- */
+
+ for (knt2 = 1 ; knt2 <= ln ; knt2++)
+ {
+ i = Iw [pj++] ;
+ ASSERT (i >= 0 && i < n && (i == me || Elen [i] >= EMPTY));
+ nvi = Nv [i] ;
+ AMD_DEBUG2 ((": "ID" "ID" "ID" "ID"\n",
+ i, Elen [i], Nv [i], wflg)) ;
+
+ if (nvi > 0)
+ {
+
+ /* ------------------------------------------------- */
+ /* compress Iw, if necessary */
+ /* ------------------------------------------------- */
+
+ if (pfree >= iwlen)
+ {
+
+ AMD_DEBUG1 (("GARBAGE COLLECTION\n")) ;
+
+ /* prepare for compressing Iw by adjusting pointers
+ * and lengths so that the lists being searched in
+ * the inner and outer loops contain only the
+ * remaining entries. */
+
+ Pe [me] = p ;
+ Len [me] -= knt1 ;
+ /* check if nothing left of supervariable me */
+ if (Len [me] == 0) Pe [me] = EMPTY ;
+ Pe [e] = pj ;
+ Len [e] = ln - knt2 ;
+ /* nothing left of element e */
+ if (Len [e] == 0) Pe [e] = EMPTY ;
+
+ ncmpa++ ; /* one more garbage collection */
+
+ /* store first entry of each object in Pe */
+ /* FLIP the first entry in each object */
+ for (j = 0 ; j < n ; j++)
+ {
+ pn = Pe [j] ;
+ if (pn >= 0)
+ {
+ ASSERT (pn >= 0 && pn < iwlen) ;
+ Pe [j] = Iw [pn] ;
+ Iw [pn] = FLIP (j) ;
+ }
+ }
+
+ /* psrc/pdst point to source/destination */
+ psrc = 0 ;
+ pdst = 0 ;
+ pend = pme1 - 1 ;
+
+ while (psrc <= pend)
+ {
+ /* search for next FLIP'd entry */
+ j = FLIP (Iw [psrc++]) ;
+ if (j >= 0)
+ {
+ AMD_DEBUG2 (("Got object j: "ID"\n", j)) ;
+ Iw [pdst] = Pe [j] ;
+ Pe [j] = pdst++ ;
+ lenj = Len [j] ;
+ /* copy from source to destination */
+ for (knt3 = 0 ; knt3 <= lenj - 2 ; knt3++)
+ {
+ Iw [pdst++] = Iw [psrc++] ;
+ }
+ }
+ }
+
+ /* move the new partially-constructed element */
+ p1 = pdst ;
+ for (psrc = pme1 ; psrc <= pfree-1 ; psrc++)
+ {
+ Iw [pdst++] = Iw [psrc] ;
+ }
+ pme1 = p1 ;
+ pfree = pdst ;
+ pj = Pe [e] ;
+ p = Pe [me] ;
+
+ }
+
+ /* ------------------------------------------------- */
+ /* i is a principal variable not yet placed in Lme */
+ /* store i in new list */
+ /* ------------------------------------------------- */
+
+ /* flag i as being in Lme by negating Nv [i] */
+ degme += nvi ;
+ Nv [i] = -nvi ;
+ Iw [pfree++] = i ;
+ AMD_DEBUG2 ((" s: "ID" nv "ID"\n", i, Nv [i]));
+
+ /* ------------------------------------------------- */
+ /* remove variable i from degree link list */
+ /* ------------------------------------------------- */
+
+ ilast = Last [i] ;
+ inext = Next [i] ;
+ ASSERT (ilast >= EMPTY && ilast < n) ;
+ ASSERT (inext >= EMPTY && inext < n) ;
+ if (inext != EMPTY) Last [inext] = ilast ;
+ if (ilast != EMPTY)
+ {
+ Next [ilast] = inext ;
+ }
+ else
+ {
+ /* i is at the head of the degree list */
+ ASSERT (Degree [i] >= 0 && Degree [i] < n) ;
+ Head [Degree [i]] = inext ;
+ }
+ }
+ }
+
+ if (e != me)
+ {
+ /* set tree pointer and flag to indicate element e is
+ * absorbed into new element me (the parent of e is me) */
+ AMD_DEBUG1 ((" Element "ID" => "ID"\n", e, me)) ;
+ Pe [e] = FLIP (me) ;
+ W [e] = 0 ;
+ }
+ }
+
+ pme2 = pfree - 1 ;
+ }
+
+ /* ----------------------------------------------------------------- */
+ /* me has now been converted into an element in Iw [pme1..pme2] */
+ /* ----------------------------------------------------------------- */
+
+ /* degme holds the external degree of new element */
+ Degree [me] = degme ;
+ Pe [me] = pme1 ;
+ Len [me] = pme2 - pme1 + 1 ;
+ ASSERT (Pe [me] >= 0 && Pe [me] < iwlen) ;
+
+ Elen [me] = FLIP (nvpiv + degme) ;
+ /* FLIP (Elen (me)) is now the degree of pivot (including
+ * diagonal part). */
+
+#ifndef NDEBUG
+ AMD_DEBUG2 (("New element structure: length= "ID"\n", pme2-pme1+1)) ;
+ for (pme = pme1 ; pme <= pme2 ; pme++) AMD_DEBUG3 ((" "ID"", Iw[pme]));
+ AMD_DEBUG3 (("\n")) ;
+#endif
+
+ /* ----------------------------------------------------------------- */
+ /* make sure that wflg is not too large. */
+ /* ----------------------------------------------------------------- */
+
+ /* With the current value of wflg, wflg+n must not cause integer
+ * overflow */
+
+ wflg = clear_flag (wflg, wbig, W, n) ;
+
+/* ========================================================================= */
+/* COMPUTE (W [e] - wflg) = |Le\Lme| FOR ALL ELEMENTS */
+/* ========================================================================= */
+
+ /* -----------------------------------------------------------------
+ * Scan 1: compute the external degrees of previous elements with
+ * respect to the current element. That is:
+ * (W [e] - wflg) = |Le \ Lme|
+ * for each element e that appears in any supervariable in Lme. The
+ * notation Le refers to the pattern (list of supervariables) of a
+ * previous element e, where e is not yet absorbed, stored in
+ * Iw [Pe [e] + 1 ... Pe [e] + Len [e]]. The notation Lme
+ * refers to the pattern of the current element (stored in
+ * Iw [pme1..pme2]). If aggressive absorption is enabled, and
+ * (W [e] - wflg) becomes zero, then the element e will be absorbed
+ * in Scan 2.
+ * ----------------------------------------------------------------- */
+
+ AMD_DEBUG2 (("me: ")) ;
+ for (pme = pme1 ; pme <= pme2 ; pme++)
+ {
+ i = Iw [pme] ;
+ ASSERT (i >= 0 && i < n) ;
+ eln = Elen [i] ;
+ AMD_DEBUG3 ((""ID" Elen "ID": \n", i, eln)) ;
+ if (eln > 0)
+ {
+ /* note that Nv [i] has been negated to denote i in Lme: */
+ nvi = -Nv [i] ;
+ ASSERT (nvi > 0 && Pe [i] >= 0 && Pe [i] < iwlen) ;
+ wnvi = wflg - nvi ;
+ for (p = Pe [i] ; p <= Pe [i] + eln - 1 ; p++)
+ {
+ e = Iw [p] ;
+ ASSERT (e >= 0 && e < n) ;
+ we = W [e] ;
+ AMD_DEBUG4 ((" e "ID" we "ID" ", e, we)) ;
+ if (we >= wflg)
+ {
+ /* unabsorbed element e has been seen in this loop */
+ AMD_DEBUG4 ((" unabsorbed, first time seen")) ;
+ we -= nvi ;
+ }
+ else if (we != 0)
+ {
+ /* e is an unabsorbed element */
+ /* this is the first we have seen e in all of Scan 1 */
+ AMD_DEBUG4 ((" unabsorbed")) ;
+ we = Degree [e] + wnvi ;
+ }
+ AMD_DEBUG4 (("\n")) ;
+ W [e] = we ;
+ }
+ }
+ }
+ AMD_DEBUG2 (("\n")) ;
+
+/* ========================================================================= */
+/* DEGREE UPDATE AND ELEMENT ABSORPTION */
+/* ========================================================================= */
+
+ /* -----------------------------------------------------------------
+ * Scan 2: for each i in Lme, sum up the degree of Lme (which is
+ * degme), plus the sum of the external degrees of each Le for the
+ * elements e appearing within i, plus the supervariables in i.
+ * Place i in hash list.
+ * ----------------------------------------------------------------- */
+
+ for (pme = pme1 ; pme <= pme2 ; pme++)
+ {
+ i = Iw [pme] ;
+ ASSERT (i >= 0 && i < n && Nv [i] < 0 && Elen [i] >= 0) ;
+ AMD_DEBUG2 (("Updating: i "ID" "ID" "ID"\n", i, Elen[i], Len [i]));
+ p1 = Pe [i] ;
+ p2 = p1 + Elen [i] - 1 ;
+ pn = p1 ;
+ hash = 0 ;
+ deg = 0 ;
+ ASSERT (p1 >= 0 && p1 < iwlen && p2 >= -1 && p2 < iwlen) ;
+
+ /* ------------------------------------------------------------- */
+ /* scan the element list associated with supervariable i */
+ /* ------------------------------------------------------------- */
+
+ /* UMFPACK/MA38-style approximate degree: */
+ if (aggressive)
+ {
+ for (p = p1 ; p <= p2 ; p++)
+ {
+ e = Iw [p] ;
+ ASSERT (e >= 0 && e < n) ;
+ we = W [e] ;
+ if (we != 0)
+ {
+ /* e is an unabsorbed element */
+ /* dext = | Le \ Lme | */
+ dext = we - wflg ;
+ if (dext > 0)
+ {
+ deg += dext ;
+ Iw [pn++] = e ;
+ hash += e ;
+ AMD_DEBUG4 ((" e: "ID" hash = "ID"\n",e,hash)) ;
+ }
+ else
+ {
+ /* external degree of e is zero, absorb e into me*/
+ AMD_DEBUG1 ((" Element "ID" =>"ID" (aggressive)\n",
+ e, me)) ;
+ ASSERT (dext == 0) ;
+ Pe [e] = FLIP (me) ;
+ W [e] = 0 ;
+ }
+ }
+ }
+ }
+ else
+ {
+ for (p = p1 ; p <= p2 ; p++)
+ {
+ e = Iw [p] ;
+ ASSERT (e >= 0 && e < n) ;
+ we = W [e] ;
+ if (we != 0)
+ {
+ /* e is an unabsorbed element */
+ dext = we - wflg ;
+ ASSERT (dext >= 0) ;
+ deg += dext ;
+ Iw [pn++] = e ;
+ hash += e ;
+ AMD_DEBUG4 ((" e: "ID" hash = "ID"\n",e,hash)) ;
+ }
+ }
+ }
+
+ /* count the number of elements in i (including me): */
+ Elen [i] = pn - p1 + 1 ;
+
+ /* ------------------------------------------------------------- */
+ /* scan the supervariables in the list associated with i */
+ /* ------------------------------------------------------------- */
+
+ /* The bulk of the AMD run time is typically spent in this loop,
+ * particularly if the matrix has many dense rows that are not
+ * removed prior to ordering. */
+ p3 = pn ;
+ p4 = p1 + Len [i] ;
+ for (p = p2 + 1 ; p < p4 ; p++)
+ {
+ j = Iw [p] ;
+ ASSERT (j >= 0 && j < n) ;
+ nvj = Nv [j] ;
+ if (nvj > 0)
+ {
+ /* j is unabsorbed, and not in Lme. */
+ /* add to degree and add to new list */
+ deg += nvj ;
+ Iw [pn++] = j ;
+ hash += j ;
+ AMD_DEBUG4 ((" s: "ID" hash "ID" Nv[j]= "ID"\n",
+ j, hash, nvj)) ;
+ }
+ }
+
+ /* ------------------------------------------------------------- */
+ /* update the degree and check for mass elimination */
+ /* ------------------------------------------------------------- */
+
+ /* with aggressive absorption, deg==0 is identical to the
+ * Elen [i] == 1 && p3 == pn test, below. */
+ ASSERT (IMPLIES (aggressive, (deg==0) == (Elen[i]==1 && p3==pn))) ;
+
+ if (Elen [i] == 1 && p3 == pn)
+ {
+
+ /* --------------------------------------------------------- */
+ /* mass elimination */
+ /* --------------------------------------------------------- */
+
+ /* There is nothing left of this node except for an edge to
+ * the current pivot element. Elen [i] is 1, and there are
+ * no variables adjacent to node i. Absorb i into the
+ * current pivot element, me. Note that if there are two or
+ * more mass eliminations, fillin due to mass elimination is
+ * possible within the nvpiv-by-nvpiv pivot block. It is this
+ * step that causes AMD's analysis to be an upper bound.
+ *
+ * The reason is that the selected pivot has a lower
+ * approximate degree than the true degree of the two mass
+ * eliminated nodes. There is no edge between the two mass
+ * eliminated nodes. They are merged with the current pivot
+ * anyway.
+ *
+ * No fillin occurs in the Schur complement, in any case,
+ * and this effect does not decrease the quality of the
+ * ordering itself, just the quality of the nonzero and
+ * flop count analysis. It also means that the post-ordering
+ * is not an exact elimination tree post-ordering. */
+
+ AMD_DEBUG1 ((" MASS i "ID" => parent e "ID"\n", i, me)) ;
+ Pe [i] = FLIP (me) ;
+ nvi = -Nv [i] ;
+ degme -= nvi ;
+ nvpiv += nvi ;
+ nel += nvi ;
+ Nv [i] = 0 ;
+ Elen [i] = EMPTY ;
+
+ }
+ else
+ {
+
+ /* --------------------------------------------------------- */
+ /* update the upper-bound degree of i */
+ /* --------------------------------------------------------- */
+
+ /* the following degree does not yet include the size
+ * of the current element, which is added later: */
+
+ Degree [i] = MIN (Degree [i], deg) ;
+
+ /* --------------------------------------------------------- */
+ /* add me to the list for i */
+ /* --------------------------------------------------------- */
+
+ /* move first supervariable to end of list */
+ Iw [pn] = Iw [p3] ;
+ /* move first element to end of element part of list */
+ Iw [p3] = Iw [p1] ;
+ /* add new element, me, to front of list. */
+ Iw [p1] = me ;
+ /* store the new length of the list in Len [i] */
+ Len [i] = pn - p1 + 1 ;
+
+ /* --------------------------------------------------------- */
+ /* place in hash bucket. Save hash key of i in Last [i]. */
+ /* --------------------------------------------------------- */
+
+ /* NOTE: this can fail if hash is negative, because the ANSI C
+ * standard does not define a % b when a and/or b are negative.
+ * That's why hash is defined as an unsigned Int, to avoid this
+ * problem. */
+ hash = hash % n ;
+ ASSERT (((Int) hash) >= 0 && ((Int) hash) < n) ;
+
+ /* if the Hhead array is not used: */
+ j = Head [hash] ;
+ if (j <= EMPTY)
+ {
+ /* degree list is empty, hash head is FLIP (j) */
+ Next [i] = FLIP (j) ;
+ Head [hash] = FLIP (i) ;
+ }
+ else
+ {
+ /* degree list is not empty, use Last [Head [hash]] as
+ * hash head. */
+ Next [i] = Last [j] ;
+ Last [j] = i ;
+ }
+
+ /* if a separate Hhead array is used: *
+ Next [i] = Hhead [hash] ;
+ Hhead [hash] = i ;
+ */
+
+ Last [i] = hash ;
+ }
+ }
+
+ Degree [me] = degme ;
+
+ /* ----------------------------------------------------------------- */
+ /* Clear the counter array, W [...], by incrementing wflg. */
+ /* ----------------------------------------------------------------- */
+
+ /* make sure that wflg+n does not cause integer overflow */
+ lemax = MAX (lemax, degme) ;
+ wflg += lemax ;
+ wflg = clear_flag (wflg, wbig, W, n) ;
+ /* at this point, W [0..n-1] < wflg holds */
+
+/* ========================================================================= */
+/* SUPERVARIABLE DETECTION */
+/* ========================================================================= */
+
+ AMD_DEBUG1 (("Detecting supervariables:\n")) ;
+ for (pme = pme1 ; pme <= pme2 ; pme++)
+ {
+ i = Iw [pme] ;
+ ASSERT (i >= 0 && i < n) ;
+ AMD_DEBUG2 (("Consider i "ID" nv "ID"\n", i, Nv [i])) ;
+ if (Nv [i] < 0)
+ {
+ /* i is a principal variable in Lme */
+
+ /* ---------------------------------------------------------
+ * examine all hash buckets with 2 or more variables. We do
+ * this by examing all unique hash keys for supervariables in
+ * the pattern Lme of the current element, me
+ * --------------------------------------------------------- */
+
+ /* let i = head of hash bucket, and empty the hash bucket */
+ ASSERT (Last [i] >= 0 && Last [i] < n) ;
+ hash = Last [i] ;
+
+ /* if Hhead array is not used: */
+ j = Head [hash] ;
+ if (j == EMPTY)
+ {
+ /* hash bucket and degree list are both empty */
+ i = EMPTY ;
+ }
+ else if (j < EMPTY)
+ {
+ /* degree list is empty */
+ i = FLIP (j) ;
+ Head [hash] = EMPTY ;
+ }
+ else
+ {
+ /* degree list is not empty, restore Last [j] of head j */
+ i = Last [j] ;
+ Last [j] = EMPTY ;
+ }
+
+ /* if separate Hhead array is used: *
+ i = Hhead [hash] ;
+ Hhead [hash] = EMPTY ;
+ */
+
+ ASSERT (i >= EMPTY && i < n) ;
+ AMD_DEBUG2 (("----i "ID" hash "ID"\n", i, hash)) ;
+
+ while (i != EMPTY && Next [i] != EMPTY)
+ {
+
+ /* -----------------------------------------------------
+ * this bucket has one or more variables following i.
+ * scan all of them to see if i can absorb any entries
+ * that follow i in hash bucket. Scatter i into w.
+ * ----------------------------------------------------- */
+
+ ln = Len [i] ;
+ eln = Elen [i] ;
+ ASSERT (ln >= 0 && eln >= 0) ;
+ ASSERT (Pe [i] >= 0 && Pe [i] < iwlen) ;
+ /* do not flag the first element in the list (me) */
+ for (p = Pe [i] + 1 ; p <= Pe [i] + ln - 1 ; p++)
+ {
+ ASSERT (Iw [p] >= 0 && Iw [p] < n) ;
+ W [Iw [p]] = wflg ;
+ }
+
+ /* ----------------------------------------------------- */
+ /* scan every other entry j following i in bucket */
+ /* ----------------------------------------------------- */
+
+ jlast = i ;
+ j = Next [i] ;
+ ASSERT (j >= EMPTY && j < n) ;
+
+ while (j != EMPTY)
+ {
+ /* ------------------------------------------------- */
+ /* check if j and i have identical nonzero pattern */
+ /* ------------------------------------------------- */
+
+ AMD_DEBUG3 (("compare i "ID" and j "ID"\n", i,j)) ;
+
+ /* check if i and j have the same Len and Elen */
+ ASSERT (Len [j] >= 0 && Elen [j] >= 0) ;
+ ASSERT (Pe [j] >= 0 && Pe [j] < iwlen) ;
+ ok = (Len [j] == ln) && (Elen [j] == eln) ;
+ /* skip the first element in the list (me) */
+ for (p = Pe [j] + 1 ; ok && p <= Pe [j] + ln - 1 ; p++)
+ {
+ ASSERT (Iw [p] >= 0 && Iw [p] < n) ;
+ if (W [Iw [p]] != wflg) ok = 0 ;
+ }
+ if (ok)
+ {
+ /* --------------------------------------------- */
+ /* found it! j can be absorbed into i */
+ /* --------------------------------------------- */
+
+ AMD_DEBUG1 (("found it! j "ID" => i "ID"\n", j,i));
+ Pe [j] = FLIP (i) ;
+ /* both Nv [i] and Nv [j] are negated since they */
+ /* are in Lme, and the absolute values of each */
+ /* are the number of variables in i and j: */
+ Nv [i] += Nv [j] ;
+ Nv [j] = 0 ;
+ Elen [j] = EMPTY ;
+ /* delete j from hash bucket */
+ ASSERT (j != Next [j]) ;
+ j = Next [j] ;
+ Next [jlast] = j ;
+
+ }
+ else
+ {
+ /* j cannot be absorbed into i */
+ jlast = j ;
+ ASSERT (j != Next [j]) ;
+ j = Next [j] ;
+ }
+ ASSERT (j >= EMPTY && j < n) ;
+ }
+
+ /* -----------------------------------------------------
+ * no more variables can be absorbed into i
+ * go to next i in bucket and clear flag array
+ * ----------------------------------------------------- */
+
+ wflg++ ;
+ i = Next [i] ;
+ ASSERT (i >= EMPTY && i < n) ;
+
+ }
+ }
+ }
+ AMD_DEBUG2 (("detect done\n")) ;
+
+/* ========================================================================= */
+/* RESTORE DEGREE LISTS AND REMOVE NONPRINCIPAL SUPERVARIABLES FROM ELEMENT */
+/* ========================================================================= */
+
+ p = pme1 ;
+ nleft = n - nel ;
+ for (pme = pme1 ; pme <= pme2 ; pme++)
+ {
+ i = Iw [pme] ;
+ ASSERT (i >= 0 && i < n) ;
+ nvi = -Nv [i] ;
+ AMD_DEBUG3 (("Restore i "ID" "ID"\n", i, nvi)) ;
+ if (nvi > 0)
+ {
+ /* i is a principal variable in Lme */
+ /* restore Nv [i] to signify that i is principal */
+ Nv [i] = nvi ;
+
+ /* --------------------------------------------------------- */
+ /* compute the external degree (add size of current element) */
+ /* --------------------------------------------------------- */
+
+ deg = Degree [i] + degme - nvi ;
+ deg = MIN (deg, nleft - nvi) ;
+ ASSERT (IMPLIES (aggressive, deg > 0) && deg >= 0 && deg < n) ;
+
+ /* --------------------------------------------------------- */
+ /* place the supervariable at the head of the degree list */
+ /* --------------------------------------------------------- */
+
+ inext = Head [deg] ;
+ ASSERT (inext >= EMPTY && inext < n) ;
+ if (inext != EMPTY) Last [inext] = i ;
+ Next [i] = inext ;
+ Last [i] = EMPTY ;
+ Head [deg] = i ;
+
+ /* --------------------------------------------------------- */
+ /* save the new degree, and find the minimum degree */
+ /* --------------------------------------------------------- */
+
+ mindeg = MIN (mindeg, deg) ;
+ Degree [i] = deg ;
+
+ /* --------------------------------------------------------- */
+ /* place the supervariable in the element pattern */
+ /* --------------------------------------------------------- */
+
+ Iw [p++] = i ;
+
+ }
+ }
+ AMD_DEBUG2 (("restore done\n")) ;
+
+/* ========================================================================= */
+/* FINALIZE THE NEW ELEMENT */
+/* ========================================================================= */
+
+ AMD_DEBUG2 (("ME = "ID" DONE\n", me)) ;
+ Nv [me] = nvpiv ;
+ /* save the length of the list for the new element me */
+ Len [me] = p - pme1 ;
+ if (Len [me] == 0)
+ {
+ /* there is nothing left of the current pivot element */
+ /* it is a root of the assembly tree */
+ Pe [me] = EMPTY ;
+ W [me] = 0 ;
+ }
+ if (elenme != 0)
+ {
+ /* element was not constructed in place: deallocate part of */
+ /* it since newly nonprincipal variables may have been removed */
+ pfree = p ;
+ }
+
+ /* The new element has nvpiv pivots and the size of the contribution
+ * block for a multifrontal method is degme-by-degme, not including
+ * the "dense" rows/columns. If the "dense" rows/columns are included,
+ * the frontal matrix is no larger than
+ * (degme+ndense)-by-(degme+ndense).
+ */
+
+ if (Info != (double *) NULL)
+ {
+ f = nvpiv ;
+ r = degme + ndense ;
+ dmax = MAX (dmax, f + r) ;
+
+ /* number of nonzeros in L (excluding the diagonal) */
+ lnzme = f*r + (f-1)*f/2 ;
+ lnz += lnzme ;
+
+ /* number of divide operations for LDL' and for LU */
+ ndiv += lnzme ;
+
+ /* number of multiply-subtract pairs for LU */
+ s = f*r*r + r*(f-1)*f + (f-1)*f*(2*f-1)/6 ;
+ nms_lu += s ;
+
+ /* number of multiply-subtract pairs for LDL' */
+ nms_ldl += (s + lnzme)/2 ;
+ }
+
+#ifndef NDEBUG
+ AMD_DEBUG2 (("finalize done nel "ID" n "ID"\n ::::\n", nel, n)) ;
+ for (pme = Pe [me] ; pme <= Pe [me] + Len [me] - 1 ; pme++)
+ {
+ AMD_DEBUG3 ((" "ID"", Iw [pme])) ;
+ }
+ AMD_DEBUG3 (("\n")) ;
+#endif
+
+ }
+
+/* ========================================================================= */
+/* DONE SELECTING PIVOTS */
+/* ========================================================================= */
+
+ if (Info != (double *) NULL)
+ {
+
+ /* count the work to factorize the ndense-by-ndense submatrix */
+ f = ndense ;
+ dmax = MAX (dmax, (double) ndense) ;
+
+ /* number of nonzeros in L (excluding the diagonal) */
+ lnzme = (f-1)*f/2 ;
+ lnz += lnzme ;
+
+ /* number of divide operations for LDL' and for LU */
+ ndiv += lnzme ;
+
+ /* number of multiply-subtract pairs for LU */
+ s = (f-1)*f*(2*f-1)/6 ;
+ nms_lu += s ;
+
+ /* number of multiply-subtract pairs for LDL' */
+ nms_ldl += (s + lnzme)/2 ;
+
+ /* number of nz's in L (excl. diagonal) */
+ Info [AMD_LNZ] = lnz ;
+
+ /* number of divide ops for LU and LDL' */
+ Info [AMD_NDIV] = ndiv ;
+
+ /* number of multiply-subtract pairs for LDL' */
+ Info [AMD_NMULTSUBS_LDL] = nms_ldl ;
+
+ /* number of multiply-subtract pairs for LU */
+ Info [AMD_NMULTSUBS_LU] = nms_lu ;
+
+ /* number of "dense" rows/columns */
+ Info [AMD_NDENSE] = ndense ;
+
+ /* largest front is dmax-by-dmax */
+ Info [AMD_DMAX] = dmax ;
+
+ /* number of garbage collections in AMD */
+ Info [AMD_NCMPA] = ncmpa ;
+
+ /* successful ordering */
+ Info [AMD_STATUS] = AMD_OK ;
+ }
+
+/* ========================================================================= */
+/* POST-ORDERING */
+/* ========================================================================= */
+
+/* -------------------------------------------------------------------------
+ * Variables at this point:
+ *
+ * Pe: holds the elimination tree. The parent of j is FLIP (Pe [j]),
+ * or EMPTY if j is a root. The tree holds both elements and
+ * non-principal (unordered) variables absorbed into them.
+ * Dense variables are non-principal and unordered.
+ *
+ * Elen: holds the size of each element, including the diagonal part.
+ * FLIP (Elen [e]) > 0 if e is an element. For unordered
+ * variables i, Elen [i] is EMPTY.
+ *
+ * Nv: Nv [e] > 0 is the number of pivots represented by the element e.
+ * For unordered variables i, Nv [i] is zero.
+ *
+ * Contents no longer needed:
+ * W, Iw, Len, Degree, Head, Next, Last.
+ *
+ * The matrix itself has been destroyed.
+ *
+ * n: the size of the matrix.
+ * No other scalars needed (pfree, iwlen, etc.)
+ * ------------------------------------------------------------------------- */
+
+ /* restore Pe */
+ for (i = 0 ; i < n ; i++)
+ {
+ Pe [i] = FLIP (Pe [i]) ;
+ }
+
+ /* restore Elen, for output information, and for postordering */
+ for (i = 0 ; i < n ; i++)
+ {
+ Elen [i] = FLIP (Elen [i]) ;
+ }
+
+/* Now the parent of j is Pe [j], or EMPTY if j is a root. Elen [e] > 0
+ * is the size of element e. Elen [i] is EMPTY for unordered variable i. */
+
+#ifndef NDEBUG
+ AMD_DEBUG2 (("\nTree:\n")) ;
+ for (i = 0 ; i < n ; i++)
+ {
+ AMD_DEBUG2 ((" "ID" parent: "ID" ", i, Pe [i])) ;
+ ASSERT (Pe [i] >= EMPTY && Pe [i] < n) ;
+ if (Nv [i] > 0)
+ {
+ /* this is an element */
+ e = i ;
+ AMD_DEBUG2 ((" element, size is "ID"\n", Elen [i])) ;
+ ASSERT (Elen [e] > 0) ;
+ }
+ AMD_DEBUG2 (("\n")) ;
+ }
+ AMD_DEBUG2 (("\nelements:\n")) ;
+ for (e = 0 ; e < n ; e++)
+ {
+ if (Nv [e] > 0)
+ {
+ AMD_DEBUG3 (("Element e= "ID" size "ID" nv "ID" \n", e,
+ Elen [e], Nv [e])) ;
+ }
+ }
+ AMD_DEBUG2 (("\nvariables:\n")) ;
+ for (i = 0 ; i < n ; i++)
+ {
+ Int cnt ;
+ if (Nv [i] == 0)
+ {
+ AMD_DEBUG3 (("i unordered: "ID"\n", i)) ;
+ j = Pe [i] ;
+ cnt = 0 ;
+ AMD_DEBUG3 ((" j: "ID"\n", j)) ;
+ if (j == EMPTY)
+ {
+ AMD_DEBUG3 ((" i is a dense variable\n")) ;
+ }
+ else
+ {
+ ASSERT (j >= 0 && j < n) ;
+ while (Nv [j] == 0)
+ {
+ AMD_DEBUG3 ((" j : "ID"\n", j)) ;
+ j = Pe [j] ;
+ AMD_DEBUG3 ((" j:: "ID"\n", j)) ;
+ cnt++ ;
+ if (cnt > n) break ;
+ }
+ e = j ;
+ AMD_DEBUG3 ((" got to e: "ID"\n", e)) ;
+ }
+ }
+ }
+#endif
+
+/* ========================================================================= */
+/* compress the paths of the variables */
+/* ========================================================================= */
+
+ for (i = 0 ; i < n ; i++)
+ {
+ if (Nv [i] == 0)
+ {
+
+ /* -------------------------------------------------------------
+ * i is an un-ordered row. Traverse the tree from i until
+ * reaching an element, e. The element, e, was the principal
+ * supervariable of i and all nodes in the path from i to when e
+ * was selected as pivot.
+ * ------------------------------------------------------------- */
+
+ AMD_DEBUG1 (("Path compression, i unordered: "ID"\n", i)) ;
+ j = Pe [i] ;
+ ASSERT (j >= EMPTY && j < n) ;
+ AMD_DEBUG3 ((" j: "ID"\n", j)) ;
+ if (j == EMPTY)
+ {
+ /* Skip a dense variable. It has no parent. */
+ AMD_DEBUG3 ((" i is a dense variable\n")) ;
+ continue ;
+ }
+
+ /* while (j is a variable) */
+ while (Nv [j] == 0)
+ {
+ AMD_DEBUG3 ((" j : "ID"\n", j)) ;
+ j = Pe [j] ;
+ AMD_DEBUG3 ((" j:: "ID"\n", j)) ;
+ ASSERT (j >= 0 && j < n) ;
+ }
+ /* got to an element e */
+ e = j ;
+ AMD_DEBUG3 (("got to e: "ID"\n", e)) ;
+
+ /* -------------------------------------------------------------
+ * traverse the path again from i to e, and compress the path
+ * (all nodes point to e). Path compression allows this code to
+ * compute in O(n) time.
+ * ------------------------------------------------------------- */
+
+ j = i ;
+ /* while (j is a variable) */
+ while (Nv [j] == 0)
+ {
+ jnext = Pe [j] ;
+ AMD_DEBUG3 (("j "ID" jnext "ID"\n", j, jnext)) ;
+ Pe [j] = e ;
+ j = jnext ;
+ ASSERT (j >= 0 && j < n) ;
+ }
+ }
+ }
+
+/* ========================================================================= */
+/* postorder the assembly tree */
+/* ========================================================================= */
+
+ AMD_postorder (n, Pe, Nv, Elen,
+ W, /* output order */
+ Head, Next, Last) ; /* workspace */
+
+/* ========================================================================= */
+/* compute output permutation and inverse permutation */
+/* ========================================================================= */
+
+ /* W [e] = k means that element e is the kth element in the new
+ * order. e is in the range 0 to n-1, and k is in the range 0 to
+ * the number of elements. Use Head for inverse order. */
+
+ for (k = 0 ; k < n ; k++)
+ {
+ Head [k] = EMPTY ;
+ Next [k] = EMPTY ;
+ }
+ for (e = 0 ; e < n ; e++)
+ {
+ k = W [e] ;
+ ASSERT ((k == EMPTY) == (Nv [e] == 0)) ;
+ if (k != EMPTY)
+ {
+ ASSERT (k >= 0 && k < n) ;
+ Head [k] = e ;
+ }
+ }
+
+ /* construct output inverse permutation in Next,
+ * and permutation in Last */
+ nel = 0 ;
+ for (k = 0 ; k < n ; k++)
+ {
+ e = Head [k] ;
+ if (e == EMPTY) break ;
+ ASSERT (e >= 0 && e < n && Nv [e] > 0) ;
+ Next [e] = nel ;
+ nel += Nv [e] ;
+ }
+ ASSERT (nel == n - ndense) ;
+
+ /* order non-principal variables (dense, & those merged into supervar's) */
+ for (i = 0 ; i < n ; i++)
+ {
+ if (Nv [i] == 0)
+ {
+ e = Pe [i] ;
+ ASSERT (e >= EMPTY && e < n) ;
+ if (e != EMPTY)
+ {
+ /* This is an unordered variable that was merged
+ * into element e via supernode detection or mass
+ * elimination of i when e became the pivot element.
+ * Place i in order just before e. */
+ ASSERT (Next [i] == EMPTY && Nv [e] > 0) ;
+ Next [i] = Next [e] ;
+ Next [e]++ ;
+ }
+ else
+ {
+ /* This is a dense unordered variable, with no parent.
+ * Place it last in the output order. */
+ Next [i] = nel++ ;
+ }
+ }
+ }
+ ASSERT (nel == n) ;
+
+ AMD_DEBUG2 (("\n\nPerm:\n")) ;
+ for (i = 0 ; i < n ; i++)
+ {
+ k = Next [i] ;
+ ASSERT (k >= 0 && k < n) ;
+ Last [k] = i ;
+ AMD_DEBUG2 ((" perm ["ID"] = "ID"\n", k, i)) ;
+ }
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_aat.c b/test/monniaux/glpk-4.65/src/amd/amd_aat.c
new file mode 100644
index 00000000..63bf55f5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_aat.c
@@ -0,0 +1,185 @@
+/* ========================================================================= */
+/* === AMD_aat ============================================================= */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* AMD_aat: compute the symmetry of the pattern of A, and count the number of
+ * nonzeros each column of A+A' (excluding the diagonal). Assumes the input
+ * matrix has no errors, with sorted columns and no duplicates
+ * (AMD_valid (n, n, Ap, Ai) must be AMD_OK, but this condition is not
+ * checked).
+ */
+
+#include "amd_internal.h"
+
+GLOBAL size_t AMD_aat /* returns nz in A+A' */
+(
+ Int n,
+ const Int Ap [ ],
+ const Int Ai [ ],
+ Int Len [ ], /* Len [j]: length of column j of A+A', excl diagonal*/
+ Int Tp [ ], /* workspace of size n */
+ double Info [ ]
+)
+{
+ Int p1, p2, p, i, j, pj, pj2, k, nzdiag, nzboth, nz ;
+ double sym ;
+ size_t nzaat ;
+
+#ifndef NDEBUG
+ AMD_debug_init ("AMD AAT") ;
+ for (k = 0 ; k < n ; k++) Tp [k] = EMPTY ;
+ ASSERT (AMD_valid (n, n, Ap, Ai) == AMD_OK) ;
+#endif
+
+ if (Info != (double *) NULL)
+ {
+ /* clear the Info array, if it exists */
+ for (i = 0 ; i < AMD_INFO ; i++)
+ {
+ Info [i] = EMPTY ;
+ }
+ Info [AMD_STATUS] = AMD_OK ;
+ }
+
+ for (k = 0 ; k < n ; k++)
+ {
+ Len [k] = 0 ;
+ }
+
+ nzdiag = 0 ;
+ nzboth = 0 ;
+ nz = Ap [n] ;
+
+ for (k = 0 ; k < n ; k++)
+ {
+ p1 = Ap [k] ;
+ p2 = Ap [k+1] ;
+ AMD_DEBUG2 (("\nAAT Column: "ID" p1: "ID" p2: "ID"\n", k, p1, p2)) ;
+
+ /* construct A+A' */
+ for (p = p1 ; p < p2 ; )
+ {
+ /* scan the upper triangular part of A */
+ j = Ai [p] ;
+ if (j < k)
+ {
+ /* entry A (j,k) is in the strictly upper triangular part,
+ * add both A (j,k) and A (k,j) to the matrix A+A' */
+ Len [j]++ ;
+ Len [k]++ ;
+ AMD_DEBUG3 ((" upper ("ID","ID") ("ID","ID")\n", j,k, k,j));
+ p++ ;
+ }
+ else if (j == k)
+ {
+ /* skip the diagonal */
+ p++ ;
+ nzdiag++ ;
+ break ;
+ }
+ else /* j > k */
+ {
+ /* first entry below the diagonal */
+ break ;
+ }
+ /* scan lower triangular part of A, in column j until reaching
+ * row k. Start where last scan left off. */
+ ASSERT (Tp [j] != EMPTY) ;
+ ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;
+ pj2 = Ap [j+1] ;
+ for (pj = Tp [j] ; pj < pj2 ; )
+ {
+ i = Ai [pj] ;
+ if (i < k)
+ {
+ /* A (i,j) is only in the lower part, not in upper.
+ * add both A (i,j) and A (j,i) to the matrix A+A' */
+ Len [i]++ ;
+ Len [j]++ ;
+ AMD_DEBUG3 ((" lower ("ID","ID") ("ID","ID")\n",
+ i,j, j,i)) ;
+ pj++ ;
+ }
+ else if (i == k)
+ {
+ /* entry A (k,j) in lower part and A (j,k) in upper */
+ pj++ ;
+ nzboth++ ;
+ break ;
+ }
+ else /* i > k */
+ {
+ /* consider this entry later, when k advances to i */
+ break ;
+ }
+ }
+ Tp [j] = pj ;
+ }
+ /* Tp [k] points to the entry just below the diagonal in column k */
+ Tp [k] = p ;
+ }
+
+ /* clean up, for remaining mismatched entries */
+ for (j = 0 ; j < n ; j++)
+ {
+ for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)
+ {
+ i = Ai [pj] ;
+ /* A (i,j) is only in the lower part, not in upper.
+ * add both A (i,j) and A (j,i) to the matrix A+A' */
+ Len [i]++ ;
+ Len [j]++ ;
+ AMD_DEBUG3 ((" lower cleanup ("ID","ID") ("ID","ID")\n",
+ i,j, j,i)) ;
+ }
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* compute the symmetry of the nonzero pattern of A */
+ /* --------------------------------------------------------------------- */
+
+ /* Given a matrix A, the symmetry of A is:
+ * B = tril (spones (A), -1) + triu (spones (A), 1) ;
+ * sym = nnz (B & B') / nnz (B) ;
+ * or 1 if nnz (B) is zero.
+ */
+
+ if (nz == nzdiag)
+ {
+ sym = 1 ;
+ }
+ else
+ {
+ sym = (2 * (double) nzboth) / ((double) (nz - nzdiag)) ;
+ }
+
+ nzaat = 0 ;
+ for (k = 0 ; k < n ; k++)
+ {
+ nzaat += Len [k] ;
+ }
+
+ AMD_DEBUG1 (("AMD nz in A+A', excluding diagonal (nzaat) = %g\n",
+ (double) nzaat)) ;
+ AMD_DEBUG1 ((" nzboth: "ID" nz: "ID" nzdiag: "ID" symmetry: %g\n",
+ nzboth, nz, nzdiag, sym)) ;
+
+ if (Info != (double *) NULL)
+ {
+ Info [AMD_STATUS] = AMD_OK ;
+ Info [AMD_N] = n ;
+ Info [AMD_NZ] = nz ;
+ Info [AMD_SYMMETRY] = sym ; /* symmetry of pattern of A */
+ Info [AMD_NZDIAG] = nzdiag ; /* nonzeros on diagonal of A */
+ Info [AMD_NZ_A_PLUS_AT] = nzaat ; /* nonzeros in A+A' */
+ }
+
+ return (nzaat) ;
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_control.c b/test/monniaux/glpk-4.65/src/amd/amd_control.c
new file mode 100644
index 00000000..f4d4f0df
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_control.c
@@ -0,0 +1,64 @@
+/* ========================================================================= */
+/* === AMD_control ========================================================= */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* User-callable. Prints the control parameters for AMD. See amd.h
+ * for details. If the Control array is not present, the defaults are
+ * printed instead.
+ */
+
+#include "amd_internal.h"
+
+GLOBAL void AMD_control
+(
+ double Control [ ]
+)
+{
+ double alpha ;
+ Int aggressive ;
+
+ if (Control != (double *) NULL)
+ {
+ alpha = Control [AMD_DENSE] ;
+ aggressive = Control [AMD_AGGRESSIVE] != 0 ;
+ }
+ else
+ {
+ alpha = AMD_DEFAULT_DENSE ;
+ aggressive = AMD_DEFAULT_AGGRESSIVE ;
+ }
+
+ PRINTF (("\nAMD version %d.%d.%d, %s: approximate minimum degree ordering\n"
+ " dense row parameter: %g\n", AMD_MAIN_VERSION, AMD_SUB_VERSION,
+ AMD_SUBSUB_VERSION, AMD_DATE, alpha)) ;
+
+ if (alpha < 0)
+ {
+ PRINTF ((" no rows treated as dense\n")) ;
+ }
+ else
+ {
+ PRINTF ((
+ " (rows with more than max (%g * sqrt (n), 16) entries are\n"
+ " considered \"dense\", and placed last in output permutation)\n",
+ alpha)) ;
+ }
+
+ if (aggressive)
+ {
+ PRINTF ((" aggressive absorption: yes\n")) ;
+ }
+ else
+ {
+ PRINTF ((" aggressive absorption: no\n")) ;
+ }
+
+ PRINTF ((" size of AMD integer: %d\n\n", sizeof (Int))) ;
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_defaults.c b/test/monniaux/glpk-4.65/src/amd/amd_defaults.c
new file mode 100644
index 00000000..820e8942
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_defaults.c
@@ -0,0 +1,38 @@
+/* ========================================================================= */
+/* === AMD_defaults ======================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* User-callable. Sets default control parameters for AMD. See amd.h
+ * for details.
+ */
+
+#include "amd_internal.h"
+
+/* ========================================================================= */
+/* === AMD defaults ======================================================== */
+/* ========================================================================= */
+
+GLOBAL void AMD_defaults
+(
+ double Control [ ]
+)
+{
+ Int i ;
+
+ if (Control != (double *) NULL)
+ {
+ for (i = 0 ; i < AMD_CONTROL ; i++)
+ {
+ Control [i] = 0 ;
+ }
+ Control [AMD_DENSE] = AMD_DEFAULT_DENSE ;
+ Control [AMD_AGGRESSIVE] = AMD_DEFAULT_AGGRESSIVE ;
+ }
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_dump.c b/test/monniaux/glpk-4.65/src/amd/amd_dump.c
new file mode 100644
index 00000000..39bbe1d8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_dump.c
@@ -0,0 +1,180 @@
+/* ========================================================================= */
+/* === AMD_dump ============================================================ */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* Debugging routines for AMD. Not used if NDEBUG is not defined at compile-
+ * time (the default). See comments in amd_internal.h on how to enable
+ * debugging. Not user-callable.
+ */
+
+#include "amd_internal.h"
+
+#ifndef NDEBUG
+
+/* This global variable is present only when debugging */
+GLOBAL Int AMD_debug = -999 ; /* default is no debug printing */
+
+/* ========================================================================= */
+/* === AMD_debug_init ====================================================== */
+/* ========================================================================= */
+
+/* Sets the debug print level, by reading the file debug.amd (if it exists) */
+
+GLOBAL void AMD_debug_init ( char *s )
+{
+ FILE *f ;
+ f = fopen ("debug.amd", "r") ;
+ if (f == (FILE *) NULL)
+ {
+ AMD_debug = -999 ;
+ }
+ else
+ {
+ fscanf (f, ID, &AMD_debug) ;
+ fclose (f) ;
+ }
+ if (AMD_debug >= 0)
+ {
+ printf ("%s: AMD_debug_init, D= "ID"\n", s, AMD_debug) ;
+ }
+}
+
+/* ========================================================================= */
+/* === AMD_dump ============================================================ */
+/* ========================================================================= */
+
+/* Dump AMD's data structure, except for the hash buckets. This routine
+ * cannot be called when the hash buckets are non-empty.
+ */
+
+GLOBAL void AMD_dump (
+ Int n, /* A is n-by-n */
+ Int Pe [ ], /* pe [0..n-1]: index in iw of start of row i */
+ Int Iw [ ], /* workspace of size iwlen, iwlen [0..pfree-1]
+ * holds the matrix on input */
+ Int Len [ ], /* len [0..n-1]: length for row i */
+ Int iwlen, /* length of iw */
+ Int pfree, /* iw [pfree ... iwlen-1] is empty on input */
+ Int Nv [ ], /* nv [0..n-1] */
+ Int Next [ ], /* next [0..n-1] */
+ Int Last [ ], /* last [0..n-1] */
+ Int Head [ ], /* head [0..n-1] */
+ Int Elen [ ], /* size n */
+ Int Degree [ ], /* size n */
+ Int W [ ], /* size n */
+ Int nel
+)
+{
+ Int i, pe, elen, nv, len, e, p, k, j, deg, w, cnt, ilast ;
+
+ if (AMD_debug < 0) return ;
+ ASSERT (pfree <= iwlen) ;
+ AMD_DEBUG3 (("\nAMD dump, pfree: "ID"\n", pfree)) ;
+ for (i = 0 ; i < n ; i++)
+ {
+ pe = Pe [i] ;
+ elen = Elen [i] ;
+ nv = Nv [i] ;
+ len = Len [i] ;
+ w = W [i] ;
+
+ if (elen >= EMPTY)
+ {
+ if (nv == 0)
+ {
+ AMD_DEBUG3 (("\nI "ID": nonprincipal: ", i)) ;
+ ASSERT (elen == EMPTY) ;
+ if (pe == EMPTY)
+ {
+ AMD_DEBUG3 ((" dense node\n")) ;
+ ASSERT (w == 1) ;
+ }
+ else
+ {
+ ASSERT (pe < EMPTY) ;
+ AMD_DEBUG3 ((" i "ID" -> parent "ID"\n", i, FLIP (Pe[i])));
+ }
+ }
+ else
+ {
+ AMD_DEBUG3 (("\nI "ID": active principal supervariable:\n",i));
+ AMD_DEBUG3 ((" nv(i): "ID" Flag: %d\n", nv, (nv < 0))) ;
+ ASSERT (elen >= 0) ;
+ ASSERT (nv > 0 && pe >= 0) ;
+ p = pe ;
+ AMD_DEBUG3 ((" e/s: ")) ;
+ if (elen == 0) AMD_DEBUG3 ((" : ")) ;
+ ASSERT (pe + len <= pfree) ;
+ for (k = 0 ; k < len ; k++)
+ {
+ j = Iw [p] ;
+ AMD_DEBUG3 ((" "ID"", j)) ;
+ ASSERT (j >= 0 && j < n) ;
+ if (k == elen-1) AMD_DEBUG3 ((" : ")) ;
+ p++ ;
+ }
+ AMD_DEBUG3 (("\n")) ;
+ }
+ }
+ else
+ {
+ e = i ;
+ if (w == 0)
+ {
+ AMD_DEBUG3 (("\nE "ID": absorbed element: w "ID"\n", e, w)) ;
+ ASSERT (nv > 0 && pe < 0) ;
+ AMD_DEBUG3 ((" e "ID" -> parent "ID"\n", e, FLIP (Pe [e]))) ;
+ }
+ else
+ {
+ AMD_DEBUG3 (("\nE "ID": unabsorbed element: w "ID"\n", e, w)) ;
+ ASSERT (nv > 0 && pe >= 0) ;
+ p = pe ;
+ AMD_DEBUG3 ((" : ")) ;
+ ASSERT (pe + len <= pfree) ;
+ for (k = 0 ; k < len ; k++)
+ {
+ j = Iw [p] ;
+ AMD_DEBUG3 ((" "ID"", j)) ;
+ ASSERT (j >= 0 && j < n) ;
+ p++ ;
+ }
+ AMD_DEBUG3 (("\n")) ;
+ }
+ }
+ }
+
+ /* this routine cannot be called when the hash buckets are non-empty */
+ AMD_DEBUG3 (("\nDegree lists:\n")) ;
+ if (nel >= 0)
+ {
+ cnt = 0 ;
+ for (deg = 0 ; deg < n ; deg++)
+ {
+ if (Head [deg] == EMPTY) continue ;
+ ilast = EMPTY ;
+ AMD_DEBUG3 ((ID": \n", deg)) ;
+ for (i = Head [deg] ; i != EMPTY ; i = Next [i])
+ {
+ AMD_DEBUG3 ((" "ID" : next "ID" last "ID" deg "ID"\n",
+ i, Next [i], Last [i], Degree [i])) ;
+ ASSERT (i >= 0 && i < n && ilast == Last [i] &&
+ deg == Degree [i]) ;
+ cnt += Nv [i] ;
+ ilast = i ;
+ }
+ AMD_DEBUG3 (("\n")) ;
+ }
+ ASSERT (cnt == n - nel) ;
+ }
+
+}
+
+#endif
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_info.c b/test/monniaux/glpk-4.65/src/amd/amd_info.c
new file mode 100644
index 00000000..e7b806a9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_info.c
@@ -0,0 +1,120 @@
+/* ========================================================================= */
+/* === AMD_info ============================================================ */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* User-callable. Prints the output statistics for AMD. See amd.h
+ * for details. If the Info array is not present, nothing is printed.
+ */
+
+#include "amd_internal.h"
+
+#define PRI(format,x) { if (x >= 0) { PRINTF ((format, x)) ; }}
+
+GLOBAL void AMD_info
+(
+ double Info [ ]
+)
+{
+ double n, ndiv, nmultsubs_ldl, nmultsubs_lu, lnz, lnzd ;
+
+ PRINTF (("\nAMD version %d.%d.%d, %s, results:\n",
+ AMD_MAIN_VERSION, AMD_SUB_VERSION, AMD_SUBSUB_VERSION, AMD_DATE)) ;
+
+ if (!Info)
+ {
+ return ;
+ }
+
+ n = Info [AMD_N] ;
+ ndiv = Info [AMD_NDIV] ;
+ nmultsubs_ldl = Info [AMD_NMULTSUBS_LDL] ;
+ nmultsubs_lu = Info [AMD_NMULTSUBS_LU] ;
+ lnz = Info [AMD_LNZ] ;
+ lnzd = (n >= 0 && lnz >= 0) ? (n + lnz) : (-1) ;
+
+ /* AMD return status */
+ PRINTF ((" status: ")) ;
+ if (Info [AMD_STATUS] == AMD_OK)
+ {
+ PRINTF (("OK\n")) ;
+ }
+ else if (Info [AMD_STATUS] == AMD_OUT_OF_MEMORY)
+ {
+ PRINTF (("out of memory\n")) ;
+ }
+ else if (Info [AMD_STATUS] == AMD_INVALID)
+ {
+ PRINTF (("invalid matrix\n")) ;
+ }
+ else if (Info [AMD_STATUS] == AMD_OK_BUT_JUMBLED)
+ {
+ PRINTF (("OK, but jumbled\n")) ;
+ }
+ else
+ {
+ PRINTF (("unknown\n")) ;
+ }
+
+ /* statistics about the input matrix */
+ PRI (" n, dimension of A: %.20g\n", n);
+ PRI (" nz, number of nonzeros in A: %.20g\n",
+ Info [AMD_NZ]) ;
+ PRI (" symmetry of A: %.4f\n",
+ Info [AMD_SYMMETRY]) ;
+ PRI (" number of nonzeros on diagonal: %.20g\n",
+ Info [AMD_NZDIAG]) ;
+ PRI (" nonzeros in pattern of A+A' (excl. diagonal): %.20g\n",
+ Info [AMD_NZ_A_PLUS_AT]) ;
+ PRI (" # dense rows/columns of A+A': %.20g\n",
+ Info [AMD_NDENSE]) ;
+
+ /* statistics about AMD's behavior */
+ PRI (" memory used, in bytes: %.20g\n",
+ Info [AMD_MEMORY]) ;
+ PRI (" # of memory compactions: %.20g\n",
+ Info [AMD_NCMPA]) ;
+
+ /* statistics about the ordering quality */
+ PRINTF (("\n"
+ " The following approximate statistics are for a subsequent\n"
+ " factorization of A(P,P) + A(P,P)'. They are slight upper\n"
+ " bounds if there are no dense rows/columns in A+A', and become\n"
+ " looser if dense rows/columns exist.\n\n")) ;
+
+ PRI (" nonzeros in L (excluding diagonal): %.20g\n",
+ lnz) ;
+ PRI (" nonzeros in L (including diagonal): %.20g\n",
+ lnzd) ;
+ PRI (" # divide operations for LDL' or LU: %.20g\n",
+ ndiv) ;
+ PRI (" # multiply-subtract operations for LDL': %.20g\n",
+ nmultsubs_ldl) ;
+ PRI (" # multiply-subtract operations for LU: %.20g\n",
+ nmultsubs_lu) ;
+ PRI (" max nz. in any column of L (incl. diagonal): %.20g\n",
+ Info [AMD_DMAX]) ;
+
+ /* total flop counts for various factorizations */
+
+ if (n >= 0 && ndiv >= 0 && nmultsubs_ldl >= 0 && nmultsubs_lu >= 0)
+ {
+ PRINTF (("\n"
+ " chol flop count for real A, sqrt counted as 1 flop: %.20g\n"
+ " LDL' flop count for real A: %.20g\n"
+ " LDL' flop count for complex A: %.20g\n"
+ " LU flop count for real A (with no pivoting): %.20g\n"
+ " LU flop count for complex A (with no pivoting): %.20g\n\n",
+ n + ndiv + 2*nmultsubs_ldl,
+ ndiv + 2*nmultsubs_ldl,
+ 9*ndiv + 8*nmultsubs_ldl,
+ ndiv + 2*nmultsubs_lu,
+ 9*ndiv + 8*nmultsubs_lu)) ;
+ }
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_internal.h b/test/monniaux/glpk-4.65/src/amd/amd_internal.h
new file mode 100644
index 00000000..b08f8436
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_internal.h
@@ -0,0 +1,117 @@
+/* amd_internal.h */
+
+/* Written by Andrew Makhorin <mao@gnu.org>. */
+
+#ifndef AMD_INTERNAL_H
+#define AMD_INTERNAL_H
+
+/* AMD will be exceedingly slow when running in debug mode. */
+#if 1
+#define NDEBUG
+#endif
+
+#include "amd.h"
+#define _GLPSTD_STDIO
+#include "env.h"
+
+#define Int int
+#define ID "%d"
+#define Int_MAX INT_MAX
+
+#if 0 /* 15/II-2012 */
+/* now this macro is defined in glpenv.h; besides, the definiton below
+ depends on implementation, because size_t is an unsigned type */
+#define SIZE_T_MAX ((size_t)(-1))
+#endif
+
+#define EMPTY (-1)
+#define FLIP(i) (-(i)-2)
+#define UNFLIP(i) ((i < EMPTY) ? FLIP (i) : (i))
+
+#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
+
+#define IMPLIES(p, q) (!(p) || (q))
+
+#define GLOBAL
+
+#define AMD_order amd_order
+#define AMD_defaults amd_defaults
+#define AMD_control amd_control
+#define AMD_info amd_info
+#define AMD_1 amd_1
+#define AMD_2 amd_2
+#define AMD_valid amd_valid
+#define AMD_aat amd_aat
+#define AMD_postorder amd_postorder
+#define AMD_post_tree amd_post_tree
+#define AMD_dump amd_dump
+#define AMD_debug amd_debug
+#define AMD_debug_init amd_debug_init
+#define AMD_preprocess amd_preprocess
+
+#define amd_malloc xmalloc
+#if 0 /* 24/V-2009 */
+#define amd_free xfree
+#else
+#define amd_free(ptr) { if ((ptr) != NULL) xfree(ptr); }
+#endif
+#define amd_printf xprintf
+
+#define PRINTF(params) { amd_printf params; }
+
+#ifndef NDEBUG
+#define ASSERT(expr) xassert(expr)
+#define AMD_DEBUG0(params) { PRINTF(params); }
+#define AMD_DEBUG1(params) { if (AMD_debug >= 1) PRINTF(params); }
+#define AMD_DEBUG2(params) { if (AMD_debug >= 2) PRINTF(params); }
+#define AMD_DEBUG3(params) { if (AMD_debug >= 3) PRINTF(params); }
+#define AMD_DEBUG4(params) { if (AMD_debug >= 4) PRINTF(params); }
+#else
+#define ASSERT(expression)
+#define AMD_DEBUG0(params)
+#define AMD_DEBUG1(params)
+#define AMD_DEBUG2(params)
+#define AMD_DEBUG3(params)
+#define AMD_DEBUG4(params)
+#endif
+
+#define amd_aat _glp_amd_aat
+size_t AMD_aat(Int n, const Int Ap[], const Int Ai[], Int Len[],
+ Int Tp[], double Info[]);
+
+#define amd_1 _glp_amd_1
+void AMD_1(Int n, const Int Ap[], const Int Ai[], Int P[], Int Pinv[],
+ Int Len[], Int slen, Int S[], double Control[], double Info[]);
+
+#define amd_postorder _glp_amd_postorder
+void AMD_postorder(Int nn, Int Parent[], Int Npiv[], Int Fsize[],
+ Int Order[], Int Child[], Int Sibling[], Int Stack[]);
+
+#define amd_post_tree _glp_amd_post_tree
+#ifndef NDEBUG
+Int AMD_post_tree(Int root, Int k, Int Child[], const Int Sibling[],
+ Int Order[], Int Stack[], Int nn);
+#else
+Int AMD_post_tree(Int root, Int k, Int Child[], const Int Sibling[],
+ Int Order[], Int Stack[]);
+#endif
+
+#define amd_preprocess _glp_amd_preprocess
+void AMD_preprocess(Int n, const Int Ap[], const Int Ai[], Int Rp[],
+ Int Ri[], Int W[], Int Flag[]);
+
+#define amd_debug _glp_amd_debug
+extern Int AMD_debug;
+
+#define amd_debug_init _glp_amd_debug_init
+void AMD_debug_init(char *s);
+
+#define amd_dump _glp_amd_dump
+void AMD_dump(Int n, Int Pe[], Int Iw[], Int Len[], Int iwlen,
+ Int pfree, Int Nv[], Int Next[], Int Last[], Int Head[],
+ Int Elen[], Int Degree[], Int W[], Int nel);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_order.c b/test/monniaux/glpk-4.65/src/amd/amd_order.c
new file mode 100644
index 00000000..332d5663
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_order.c
@@ -0,0 +1,200 @@
+/* ========================================================================= */
+/* === AMD_order =========================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* User-callable AMD minimum degree ordering routine. See amd.h for
+ * documentation.
+ */
+
+#include "amd_internal.h"
+
+/* ========================================================================= */
+/* === AMD_order =========================================================== */
+/* ========================================================================= */
+
+GLOBAL Int AMD_order
+(
+ Int n,
+ const Int Ap [ ],
+ const Int Ai [ ],
+ Int P [ ],
+ double Control [ ],
+ double Info [ ]
+)
+{
+ Int *Len, *S, nz, i, *Pinv, info, status, *Rp, *Ri, *Cp, *Ci, ok ;
+ size_t nzaat, slen ;
+ double mem = 0 ;
+
+#ifndef NDEBUG
+ AMD_debug_init ("amd") ;
+#endif
+
+ /* clear the Info array, if it exists */
+ info = Info != (double *) NULL ;
+ if (info)
+ {
+ for (i = 0 ; i < AMD_INFO ; i++)
+ {
+ Info [i] = EMPTY ;
+ }
+ Info [AMD_N] = n ;
+ Info [AMD_STATUS] = AMD_OK ;
+ }
+
+ /* make sure inputs exist and n is >= 0 */
+ if (Ai == (Int *) NULL || Ap == (Int *) NULL || P == (Int *) NULL || n < 0)
+ {
+ if (info) Info [AMD_STATUS] = AMD_INVALID ;
+ return (AMD_INVALID) ; /* arguments are invalid */
+ }
+
+ if (n == 0)
+ {
+ return (AMD_OK) ; /* n is 0 so there's nothing to do */
+ }
+
+ nz = Ap [n] ;
+ if (info)
+ {
+ Info [AMD_NZ] = nz ;
+ }
+ if (nz < 0)
+ {
+ if (info) Info [AMD_STATUS] = AMD_INVALID ;
+ return (AMD_INVALID) ;
+ }
+
+ /* check if n or nz will cause size_t overflow */
+ if (((size_t) n) >= SIZE_T_MAX / sizeof (Int)
+ || ((size_t) nz) >= SIZE_T_MAX / sizeof (Int))
+ {
+ if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
+ return (AMD_OUT_OF_MEMORY) ; /* problem too large */
+ }
+
+ /* check the input matrix: AMD_OK, AMD_INVALID, or AMD_OK_BUT_JUMBLED */
+ status = AMD_valid (n, n, Ap, Ai) ;
+
+ if (status == AMD_INVALID)
+ {
+ if (info) Info [AMD_STATUS] = AMD_INVALID ;
+ return (AMD_INVALID) ; /* matrix is invalid */
+ }
+
+ /* allocate two size-n integer workspaces */
+ Len = amd_malloc (n * sizeof (Int)) ;
+ Pinv = amd_malloc (n * sizeof (Int)) ;
+ mem += n ;
+ mem += n ;
+ if (!Len || !Pinv)
+ {
+ /* :: out of memory :: */
+ amd_free (Len) ;
+ amd_free (Pinv) ;
+ if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
+ return (AMD_OUT_OF_MEMORY) ;
+ }
+
+ if (status == AMD_OK_BUT_JUMBLED)
+ {
+ /* sort the input matrix and remove duplicate entries */
+ AMD_DEBUG1 (("Matrix is jumbled\n")) ;
+ Rp = amd_malloc ((n+1) * sizeof (Int)) ;
+ Ri = amd_malloc (MAX (nz,1) * sizeof (Int)) ;
+ mem += (n+1) ;
+ mem += MAX (nz,1) ;
+ if (!Rp || !Ri)
+ {
+ /* :: out of memory :: */
+ amd_free (Rp) ;
+ amd_free (Ri) ;
+ amd_free (Len) ;
+ amd_free (Pinv) ;
+ if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
+ return (AMD_OUT_OF_MEMORY) ;
+ }
+ /* use Len and Pinv as workspace to create R = A' */
+ AMD_preprocess (n, Ap, Ai, Rp, Ri, Len, Pinv) ;
+ Cp = Rp ;
+ Ci = Ri ;
+ }
+ else
+ {
+ /* order the input matrix as-is. No need to compute R = A' first */
+ Rp = NULL ;
+ Ri = NULL ;
+ Cp = (Int *) Ap ;
+ Ci = (Int *) Ai ;
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* determine the symmetry and count off-diagonal nonzeros in A+A' */
+ /* --------------------------------------------------------------------- */
+
+ nzaat = AMD_aat (n, Cp, Ci, Len, P, Info) ;
+ AMD_DEBUG1 (("nzaat: %g\n", (double) nzaat)) ;
+ ASSERT ((MAX (nz-n, 0) <= nzaat) && (nzaat <= 2 * (size_t) nz)) ;
+
+ /* --------------------------------------------------------------------- */
+ /* allocate workspace for matrix, elbow room, and 6 size-n vectors */
+ /* --------------------------------------------------------------------- */
+
+ S = NULL ;
+ slen = nzaat ; /* space for matrix */
+ ok = ((slen + nzaat/5) >= slen) ; /* check for size_t overflow */
+ slen += nzaat/5 ; /* add elbow room */
+ for (i = 0 ; ok && i < 7 ; i++)
+ {
+ ok = ((slen + n) > slen) ; /* check for size_t overflow */
+ slen += n ; /* size-n elbow room, 6 size-n work */
+ }
+ mem += slen ;
+ ok = ok && (slen < SIZE_T_MAX / sizeof (Int)) ; /* check for overflow */
+ ok = ok && (slen < Int_MAX) ; /* S[i] for Int i must be OK */
+ if (ok)
+ {
+ S = amd_malloc (slen * sizeof (Int)) ;
+ }
+ AMD_DEBUG1 (("slen %g\n", (double) slen)) ;
+ if (!S)
+ {
+ /* :: out of memory :: (or problem too large) */
+ amd_free (Rp) ;
+ amd_free (Ri) ;
+ amd_free (Len) ;
+ amd_free (Pinv) ;
+ if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
+ return (AMD_OUT_OF_MEMORY) ;
+ }
+ if (info)
+ {
+ /* memory usage, in bytes. */
+ Info [AMD_MEMORY] = mem * sizeof (Int) ;
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* order the matrix */
+ /* --------------------------------------------------------------------- */
+
+ AMD_1 (n, Cp, Ci, P, Pinv, Len, slen, S, Control, Info) ;
+
+ /* --------------------------------------------------------------------- */
+ /* free the workspace */
+ /* --------------------------------------------------------------------- */
+
+ amd_free (Rp) ;
+ amd_free (Ri) ;
+ amd_free (Len) ;
+ amd_free (Pinv) ;
+ amd_free (S) ;
+ if (info) Info [AMD_STATUS] = status ;
+ return (status) ; /* successful ordering */
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_post_tree.c b/test/monniaux/glpk-4.65/src/amd/amd_post_tree.c
new file mode 100644
index 00000000..bff0e263
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_post_tree.c
@@ -0,0 +1,121 @@
+/* ========================================================================= */
+/* === AMD_post_tree ======================================================= */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* Post-ordering of a supernodal elimination tree. */
+
+#include "amd_internal.h"
+
+GLOBAL Int AMD_post_tree
+(
+ Int root, /* root of the tree */
+ Int k, /* start numbering at k */
+ Int Child [ ], /* input argument of size nn, undefined on
+ * output. Child [i] is the head of a link
+ * list of all nodes that are children of node
+ * i in the tree. */
+ const Int Sibling [ ], /* input argument of size nn, not modified.
+ * If f is a node in the link list of the
+ * children of node i, then Sibling [f] is the
+ * next child of node i.
+ */
+ Int Order [ ], /* output order, of size nn. Order [i] = k
+ * if node i is the kth node of the reordered
+ * tree. */
+ Int Stack [ ] /* workspace of size nn */
+#ifndef NDEBUG
+ , Int nn /* nodes are in the range 0..nn-1. */
+#endif
+)
+{
+ Int f, head, h, i ;
+
+#if 0
+ /* --------------------------------------------------------------------- */
+ /* recursive version (Stack [ ] is not used): */
+ /* --------------------------------------------------------------------- */
+
+ /* this is simple, but can caouse stack overflow if nn is large */
+ i = root ;
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ k = AMD_post_tree (f, k, Child, Sibling, Order, Stack, nn) ;
+ }
+ Order [i] = k++ ;
+ return (k) ;
+#endif
+
+ /* --------------------------------------------------------------------- */
+ /* non-recursive version, using an explicit stack */
+ /* --------------------------------------------------------------------- */
+
+ /* push root on the stack */
+ head = 0 ;
+ Stack [0] = root ;
+
+ while (head >= 0)
+ {
+ /* get head of stack */
+ ASSERT (head < nn) ;
+ i = Stack [head] ;
+ AMD_DEBUG1 (("head of stack "ID" \n", i)) ;
+ ASSERT (i >= 0 && i < nn) ;
+
+ if (Child [i] != EMPTY)
+ {
+ /* the children of i are not yet ordered */
+ /* push each child onto the stack in reverse order */
+ /* so that small ones at the head of the list get popped first */
+ /* and the biggest one at the end of the list gets popped last */
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ head++ ;
+ ASSERT (head < nn) ;
+ ASSERT (f >= 0 && f < nn) ;
+ }
+ h = head ;
+ ASSERT (head < nn) ;
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ ASSERT (h > 0) ;
+ Stack [h--] = f ;
+ AMD_DEBUG1 (("push "ID" on stack\n", f)) ;
+ ASSERT (f >= 0 && f < nn) ;
+ }
+ ASSERT (Stack [h] == i) ;
+
+ /* delete child list so that i gets ordered next time we see it */
+ Child [i] = EMPTY ;
+ }
+ else
+ {
+ /* the children of i (if there were any) are already ordered */
+ /* remove i from the stack and order it. Front i is kth front */
+ head-- ;
+ AMD_DEBUG1 (("pop "ID" order "ID"\n", i, k)) ;
+ Order [i] = k++ ;
+ ASSERT (k <= nn) ;
+ }
+
+#ifndef NDEBUG
+ AMD_DEBUG1 (("\nStack:")) ;
+ for (h = head ; h >= 0 ; h--)
+ {
+ Int j = Stack [h] ;
+ AMD_DEBUG1 ((" "ID, j)) ;
+ ASSERT (j >= 0 && j < nn) ;
+ }
+ AMD_DEBUG1 (("\n\n")) ;
+ ASSERT (head < nn) ;
+#endif
+
+ }
+ return (k) ;
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_postorder.c b/test/monniaux/glpk-4.65/src/amd/amd_postorder.c
new file mode 100644
index 00000000..a3ece915
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_postorder.c
@@ -0,0 +1,207 @@
+/* ========================================================================= */
+/* === AMD_postorder ======================================================= */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* Perform a postordering (via depth-first search) of an assembly tree. */
+
+#include "amd_internal.h"
+
+GLOBAL void AMD_postorder
+(
+ /* inputs, not modified on output: */
+ Int nn, /* nodes are in the range 0..nn-1 */
+ Int Parent [ ], /* Parent [j] is the parent of j, or EMPTY if root */
+ Int Nv [ ], /* Nv [j] > 0 number of pivots represented by node j,
+ * or zero if j is not a node. */
+ Int Fsize [ ], /* Fsize [j]: size of node j */
+
+ /* output, not defined on input: */
+ Int Order [ ], /* output post-order */
+
+ /* workspaces of size nn: */
+ Int Child [ ],
+ Int Sibling [ ],
+ Int Stack [ ]
+)
+{
+ Int i, j, k, parent, frsize, f, fprev, maxfrsize, bigfprev, bigf, fnext ;
+
+ for (j = 0 ; j < nn ; j++)
+ {
+ Child [j] = EMPTY ;
+ Sibling [j] = EMPTY ;
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* place the children in link lists - bigger elements tend to be last */
+ /* --------------------------------------------------------------------- */
+
+ for (j = nn-1 ; j >= 0 ; j--)
+ {
+ if (Nv [j] > 0)
+ {
+ /* this is an element */
+ parent = Parent [j] ;
+ if (parent != EMPTY)
+ {
+ /* place the element in link list of the children its parent */
+ /* bigger elements will tend to be at the end of the list */
+ Sibling [j] = Child [parent] ;
+ Child [parent] = j ;
+ }
+ }
+ }
+
+#ifndef NDEBUG
+ {
+ Int nels, ff, nchild ;
+ AMD_DEBUG1 (("\n\n================================ AMD_postorder:\n"));
+ nels = 0 ;
+ for (j = 0 ; j < nn ; j++)
+ {
+ if (Nv [j] > 0)
+ {
+ AMD_DEBUG1 (( ""ID" : nels "ID" npiv "ID" size "ID
+ " parent "ID" maxfr "ID"\n", j, nels,
+ Nv [j], Fsize [j], Parent [j], Fsize [j])) ;
+ /* this is an element */
+ /* dump the link list of children */
+ nchild = 0 ;
+ AMD_DEBUG1 ((" Children: ")) ;
+ for (ff = Child [j] ; ff != EMPTY ; ff = Sibling [ff])
+ {
+ AMD_DEBUG1 ((ID" ", ff)) ;
+ ASSERT (Parent [ff] == j) ;
+ nchild++ ;
+ ASSERT (nchild < nn) ;
+ }
+ AMD_DEBUG1 (("\n")) ;
+ parent = Parent [j] ;
+ if (parent != EMPTY)
+ {
+ ASSERT (Nv [parent] > 0) ;
+ }
+ nels++ ;
+ }
+ }
+ }
+ AMD_DEBUG1 (("\n\nGo through the children of each node, and put\n"
+ "the biggest child last in each list:\n")) ;
+#endif
+
+ /* --------------------------------------------------------------------- */
+ /* place the largest child last in the list of children for each node */
+ /* --------------------------------------------------------------------- */
+
+ for (i = 0 ; i < nn ; i++)
+ {
+ if (Nv [i] > 0 && Child [i] != EMPTY)
+ {
+
+#ifndef NDEBUG
+ Int nchild ;
+ AMD_DEBUG1 (("Before partial sort, element "ID"\n", i)) ;
+ nchild = 0 ;
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ ASSERT (f >= 0 && f < nn) ;
+ AMD_DEBUG1 ((" f: "ID" size: "ID"\n", f, Fsize [f])) ;
+ nchild++ ;
+ ASSERT (nchild <= nn) ;
+ }
+#endif
+
+ /* find the biggest element in the child list */
+ fprev = EMPTY ;
+ maxfrsize = EMPTY ;
+ bigfprev = EMPTY ;
+ bigf = EMPTY ;
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ ASSERT (f >= 0 && f < nn) ;
+ frsize = Fsize [f] ;
+ if (frsize >= maxfrsize)
+ {
+ /* this is the biggest seen so far */
+ maxfrsize = frsize ;
+ bigfprev = fprev ;
+ bigf = f ;
+ }
+ fprev = f ;
+ }
+ ASSERT (bigf != EMPTY) ;
+
+ fnext = Sibling [bigf] ;
+
+ AMD_DEBUG1 (("bigf "ID" maxfrsize "ID" bigfprev "ID" fnext "ID
+ " fprev " ID"\n", bigf, maxfrsize, bigfprev, fnext, fprev)) ;
+
+ if (fnext != EMPTY)
+ {
+ /* if fnext is EMPTY then bigf is already at the end of list */
+
+ if (bigfprev == EMPTY)
+ {
+ /* delete bigf from the element of the list */
+ Child [i] = fnext ;
+ }
+ else
+ {
+ /* delete bigf from the middle of the list */
+ Sibling [bigfprev] = fnext ;
+ }
+
+ /* put bigf at the end of the list */
+ Sibling [bigf] = EMPTY ;
+ ASSERT (Child [i] != EMPTY) ;
+ ASSERT (fprev != bigf) ;
+ ASSERT (fprev != EMPTY) ;
+ Sibling [fprev] = bigf ;
+ }
+
+#ifndef NDEBUG
+ AMD_DEBUG1 (("After partial sort, element "ID"\n", i)) ;
+ for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
+ {
+ ASSERT (f >= 0 && f < nn) ;
+ AMD_DEBUG1 ((" "ID" "ID"\n", f, Fsize [f])) ;
+ ASSERT (Nv [f] > 0) ;
+ nchild-- ;
+ }
+ ASSERT (nchild == 0) ;
+#endif
+
+ }
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* postorder the assembly tree */
+ /* --------------------------------------------------------------------- */
+
+ for (i = 0 ; i < nn ; i++)
+ {
+ Order [i] = EMPTY ;
+ }
+
+ k = 0 ;
+
+ for (i = 0 ; i < nn ; i++)
+ {
+ if (Parent [i] == EMPTY && Nv [i] > 0)
+ {
+ AMD_DEBUG1 (("Root of assembly tree "ID"\n", i)) ;
+ k = AMD_post_tree (i, k, Child, Sibling, Order, Stack
+#ifndef NDEBUG
+ , nn
+#endif
+ ) ;
+ }
+ }
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_preprocess.c b/test/monniaux/glpk-4.65/src/amd/amd_preprocess.c
new file mode 100644
index 00000000..fc223fb5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_preprocess.c
@@ -0,0 +1,119 @@
+/* ========================================================================= */
+/* === AMD_preprocess ====================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* Sorts, removes duplicate entries, and transposes from the nonzero pattern of
+ * a column-form matrix A, to obtain the matrix R. The input matrix can have
+ * duplicate entries and/or unsorted columns (AMD_valid (n,Ap,Ai) must not be
+ * AMD_INVALID).
+ *
+ * This input condition is NOT checked. This routine is not user-callable.
+ */
+
+#include "amd_internal.h"
+
+/* ========================================================================= */
+/* === AMD_preprocess ====================================================== */
+/* ========================================================================= */
+
+/* AMD_preprocess does not check its input for errors or allocate workspace.
+ * On input, the condition (AMD_valid (n,n,Ap,Ai) != AMD_INVALID) must hold.
+ */
+
+GLOBAL void AMD_preprocess
+(
+ Int n, /* input matrix: A is n-by-n */
+ const Int Ap [ ], /* size n+1 */
+ const Int Ai [ ], /* size nz = Ap [n] */
+
+ /* output matrix R: */
+ Int Rp [ ], /* size n+1 */
+ Int Ri [ ], /* size nz (or less, if duplicates present) */
+
+ Int W [ ], /* workspace of size n */
+ Int Flag [ ] /* workspace of size n */
+)
+{
+
+ /* --------------------------------------------------------------------- */
+ /* local variables */
+ /* --------------------------------------------------------------------- */
+
+ Int i, j, p, p2 ;
+
+ ASSERT (AMD_valid (n, n, Ap, Ai) != AMD_INVALID) ;
+
+ /* --------------------------------------------------------------------- */
+ /* count the entries in each row of A (excluding duplicates) */
+ /* --------------------------------------------------------------------- */
+
+ for (i = 0 ; i < n ; i++)
+ {
+ W [i] = 0 ; /* # of nonzeros in row i (excl duplicates) */
+ Flag [i] = EMPTY ; /* Flag [i] = j if i appears in column j */
+ }
+ for (j = 0 ; j < n ; j++)
+ {
+ p2 = Ap [j+1] ;
+ for (p = Ap [j] ; p < p2 ; p++)
+ {
+ i = Ai [p] ;
+ if (Flag [i] != j)
+ {
+ /* row index i has not yet appeared in column j */
+ W [i]++ ; /* one more entry in row i */
+ Flag [i] = j ; /* flag row index i as appearing in col j*/
+ }
+ }
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* compute the row pointers for R */
+ /* --------------------------------------------------------------------- */
+
+ Rp [0] = 0 ;
+ for (i = 0 ; i < n ; i++)
+ {
+ Rp [i+1] = Rp [i] + W [i] ;
+ }
+ for (i = 0 ; i < n ; i++)
+ {
+ W [i] = Rp [i] ;
+ Flag [i] = EMPTY ;
+ }
+
+ /* --------------------------------------------------------------------- */
+ /* construct the row form matrix R */
+ /* --------------------------------------------------------------------- */
+
+ /* R = row form of pattern of A */
+ for (j = 0 ; j < n ; j++)
+ {
+ p2 = Ap [j+1] ;
+ for (p = Ap [j] ; p < p2 ; p++)
+ {
+ i = Ai [p] ;
+ if (Flag [i] != j)
+ {
+ /* row index i has not yet appeared in column j */
+ Ri [W [i]++] = j ; /* put col j in row i */
+ Flag [i] = j ; /* flag row index i as appearing in col j*/
+ }
+ }
+ }
+
+#ifndef NDEBUG
+ ASSERT (AMD_valid (n, n, Rp, Ri) == AMD_OK) ;
+ for (j = 0 ; j < n ; j++)
+ {
+ ASSERT (W [j] == Rp [j+1]) ;
+ }
+#endif
+}
diff --git a/test/monniaux/glpk-4.65/src/amd/amd_valid.c b/test/monniaux/glpk-4.65/src/amd/amd_valid.c
new file mode 100644
index 00000000..e9e2e5ab
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/amd/amd_valid.c
@@ -0,0 +1,93 @@
+/* ========================================================================= */
+/* === AMD_valid =========================================================== */
+/* ========================================================================= */
+
+/* ------------------------------------------------------------------------- */
+/* AMD, Copyright (c) Timothy A. Davis, */
+/* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
+/* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
+/* web: http://www.cise.ufl.edu/research/sparse/amd */
+/* ------------------------------------------------------------------------- */
+
+/* Check if a column-form matrix is valid or not. The matrix A is
+ * n_row-by-n_col. The row indices of entries in column j are in
+ * Ai [Ap [j] ... Ap [j+1]-1]. Required conditions are:
+ *
+ * n_row >= 0
+ * n_col >= 0
+ * nz = Ap [n_col] >= 0 number of entries in the matrix
+ * Ap [0] == 0
+ * Ap [j] <= Ap [j+1] for all j in the range 0 to n_col.
+ * Ai [0 ... nz-1] must be in the range 0 to n_row-1.
+ *
+ * If any of the above conditions hold, AMD_INVALID is returned. If the
+ * following condition holds, AMD_OK_BUT_JUMBLED is returned (a warning,
+ * not an error):
+ *
+ * row indices in Ai [Ap [j] ... Ap [j+1]-1] are not sorted in ascending
+ * order, and/or duplicate entries exist.
+ *
+ * Otherwise, AMD_OK is returned.
+ *
+ * In v1.2 and earlier, this function returned TRUE if the matrix was valid
+ * (now returns AMD_OK), or FALSE otherwise (now returns AMD_INVALID or
+ * AMD_OK_BUT_JUMBLED).
+ */
+
+#include "amd_internal.h"
+
+GLOBAL Int AMD_valid
+(
+ /* inputs, not modified on output: */
+ Int n_row, /* A is n_row-by-n_col */
+ Int n_col,
+ const Int Ap [ ], /* column pointers of A, of size n_col+1 */
+ const Int Ai [ ] /* row indices of A, of size nz = Ap [n_col] */
+)
+{
+ Int nz, j, p1, p2, ilast, i, p, result = AMD_OK ;
+
+ if (n_row < 0 || n_col < 0 || Ap == NULL || Ai == NULL)
+ {
+ return (AMD_INVALID) ;
+ }
+ nz = Ap [n_col] ;
+ if (Ap [0] != 0 || nz < 0)
+ {
+ /* column pointers must start at Ap [0] = 0, and Ap [n] must be >= 0 */
+ AMD_DEBUG0 (("column 0 pointer bad or nz < 0\n")) ;
+ return (AMD_INVALID) ;
+ }
+ for (j = 0 ; j < n_col ; j++)
+ {
+ p1 = Ap [j] ;
+ p2 = Ap [j+1] ;
+ AMD_DEBUG2 (("\nColumn: "ID" p1: "ID" p2: "ID"\n", j, p1, p2)) ;
+ if (p1 > p2)
+ {
+ /* column pointers must be ascending */
+ AMD_DEBUG0 (("column "ID" pointer bad\n", j)) ;
+ return (AMD_INVALID) ;
+ }
+ ilast = EMPTY ;
+ for (p = p1 ; p < p2 ; p++)
+ {
+ i = Ai [p] ;
+ AMD_DEBUG3 (("row: "ID"\n", i)) ;
+ if (i < 0 || i >= n_row)
+ {
+ /* row index out of range */
+ AMD_DEBUG0 (("index out of range, col "ID" row "ID"\n", j, i));
+ return (AMD_INVALID) ;
+ }
+ if (i <= ilast)
+ {
+ /* row index unsorted, or duplicate entry present */
+ AMD_DEBUG1 (("index unsorted/dupl col "ID" row "ID"\n", j, i));
+ result = AMD_OK_BUT_JUMBLED ;
+ }
+ ilast = i ;
+ }
+ }
+ return (result) ;
+}
diff --git a/test/monniaux/glpk-4.65/src/api/advbas.c b/test/monniaux/glpk-4.65/src/api/advbas.c
new file mode 100644
index 00000000..23067624
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/advbas.c
@@ -0,0 +1,155 @@
+/* advbas.c (construct advanced initial LP basis) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+#include "triang.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_adv_basis - construct advanced initial LP basis
+*
+* SYNOPSIS
+*
+* void glp_adv_basis(glp_prob *P, int flags);
+*
+* DESCRIPTION
+*
+* The routine glp_adv_basis constructs an advanced initial LP basis
+* for the specified problem object.
+*
+* The parameter flag is reserved for use in the future and should be
+* specified as zero.
+*
+* NOTE
+*
+* The routine glp_adv_basis should be called after the constraint
+* matrix has been scaled (if scaling is used). */
+
+static int mat(void *info, int k, int ind[], double val[])
+{ glp_prob *P = info;
+ int m = P->m;
+ int n = P->n;
+ GLPROW **row = P->row;
+ GLPCOL **col = P->col;
+ GLPAIJ *aij;
+ int i, j, len;
+ if (k > 0)
+ { /* retrieve scaled row of constraint matrix */
+ i = +k;
+ xassert(1 <= i && i <= m);
+ len = 0;
+ if (row[i]->type == GLP_FX)
+ { for (aij = row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { j = aij->col->j;
+ if (col[j]->type != GLP_FX)
+ { len++;
+ ind[len] = j;
+ val[len] = aij->row->rii * aij->val * aij->col->sjj;
+ }
+ }
+ }
+ }
+ else
+ { /* retrieve scaled column of constraint matrix */
+ j = -k;
+ xassert(1 <= j && j <= n);
+ len = 0;
+ if (col[j]->type != GLP_FX)
+ { for (aij = col[j]->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row->i;
+ if (row[i]->type == GLP_FX)
+ { len++;
+ ind[len] = i;
+ val[len] = aij->row->rii * aij->val * aij->col->sjj;
+ }
+ }
+ }
+ }
+ return len;
+}
+
+void glp_adv_basis(glp_prob *P, int flags)
+{ int i, j, k, m, n, min_mn, size, *rn, *cn;
+ char *flag;
+ if (flags != 0)
+ xerror("glp_adv_basis: flags = %d; invalid flags\n", flags);
+ m = P->m; /* number of rows */
+ n = P->n; /* number of columns */
+ if (m == 0 || n == 0)
+ { /* trivial case */
+ glp_std_basis(P);
+ goto done;
+ }
+ xprintf("Constructing initial basis...\n");
+ /* allocate working arrays */
+ min_mn = (m < n ? m : n);
+ rn = talloc(1+min_mn, int);
+ cn = talloc(1+min_mn, int);
+ flag = talloc(1+m, char);
+ /* make the basis empty */
+ for (i = 1; i <= m; i++)
+ { flag[i] = 0;
+ glp_set_row_stat(P, i, GLP_NS);
+ }
+ for (j = 1; j <= n; j++)
+ glp_set_col_stat(P, j, GLP_NS);
+ /* find maximal triangular part of the constraint matrix;
+ to prevent including non-fixed rows and fixed columns in the
+ triangular part, such rows and columns are temporarily made
+ empty by the routine mat */
+#if 1 /* FIXME: tolerance */
+ size = triang(m, n, mat, P, 0.001, rn, cn);
+#endif
+ xassert(0 <= size && size <= min_mn);
+ /* include in the basis non-fixed structural variables, whose
+ columns constitute the triangular part */
+ for (k = 1; k <= size; k++)
+ { i = rn[k];
+ xassert(1 <= i && i <= m);
+ flag[i] = 1;
+ j = cn[k];
+ xassert(1 <= j && j <= n);
+ glp_set_col_stat(P, j, GLP_BS);
+ }
+ /* include in the basis appropriate auxiliary variables, whose
+ unity columns preserve triangular form of the basis matrix */
+ for (i = 1; i <= m; i++)
+ { if (flag[i] == 0)
+ { glp_set_row_stat(P, i, GLP_BS);
+ if (P->row[i]->type != GLP_FX)
+ size++;
+ }
+ }
+ /* size of triangular part = (number of rows) - (number of basic
+ fixed auxiliary variables) */
+ xprintf("Size of triangular part is %d\n", size);
+ /* deallocate working arrays */
+ tfree(rn);
+ tfree(cn);
+ tfree(flag);
+done: return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/asnhall.c b/test/monniaux/glpk-4.65/src/api/asnhall.c
new file mode 100644
index 00000000..d7112a10
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/asnhall.c
@@ -0,0 +1,163 @@
+/* asnhall.c (find bipartite matching of maximum cardinality) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "mc21a.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_asnprob_hall - find bipartite matching of maximum cardinality
+*
+* SYNOPSIS
+*
+* int glp_asnprob_hall(glp_graph *G, int v_set, int a_x);
+*
+* DESCRIPTION
+*
+* The routine glp_asnprob_hall finds a matching of maximal cardinality
+* in the specified bipartite graph G. It uses a version of the Fortran
+* routine MC21A developed by I.S.Duff [1], which implements Hall's
+* algorithm [2].
+*
+* RETURNS
+*
+* The routine glp_asnprob_hall returns the cardinality of the matching
+* found. However, if the specified graph is incorrect (as detected by
+* the routine glp_check_asnprob), the routine returns negative value.
+*
+* REFERENCES
+*
+* 1. I.S.Duff, Algorithm 575: Permutations for zero-free diagonal, ACM
+* Trans. on Math. Softw. 7 (1981), 387-390.
+*
+* 2. M.Hall, "An Algorithm for distinct representatives," Amer. Math.
+* Monthly 63 (1956), 716-717. */
+
+int glp_asnprob_hall(glp_graph *G, int v_set, int a_x)
+{ glp_vertex *v;
+ glp_arc *a;
+ int card, i, k, loc, n, n1, n2, xij;
+ int *num, *icn, *ip, *lenr, *iperm, *pr, *arp, *cv, *out;
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_asnprob_hall: v_set = %d; invalid offset\n",
+ v_set);
+ if (a_x >= 0 && a_x > G->a_size - (int)sizeof(int))
+ xerror("glp_asnprob_hall: a_x = %d; invalid offset\n", a_x);
+ if (glp_check_asnprob(G, v_set))
+ return -1;
+ /* determine the number of vertices in sets R and S and renumber
+ vertices in S which correspond to columns of the matrix; skip
+ all isolated vertices */
+ num = xcalloc(1+G->nv, sizeof(int));
+ n1 = n2 = 0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (v->in == NULL && v->out != NULL)
+ n1++, num[i] = 0; /* vertex in R */
+ else if (v->in != NULL && v->out == NULL)
+ n2++, num[i] = n2; /* vertex in S */
+ else
+ { xassert(v->in == NULL && v->out == NULL);
+ num[i] = -1; /* isolated vertex */
+ }
+ }
+ /* the matrix must be square, thus, if it has more columns than
+ rows, extra rows will be just empty, and vice versa */
+ n = (n1 >= n2 ? n1 : n2);
+ /* allocate working arrays */
+ icn = xcalloc(1+G->na, sizeof(int));
+ ip = xcalloc(1+n, sizeof(int));
+ lenr = xcalloc(1+n, sizeof(int));
+ iperm = xcalloc(1+n, sizeof(int));
+ pr = xcalloc(1+n, sizeof(int));
+ arp = xcalloc(1+n, sizeof(int));
+ cv = xcalloc(1+n, sizeof(int));
+ out = xcalloc(1+n, sizeof(int));
+ /* build the adjacency matrix of the bipartite graph in row-wise
+ format (rows are vertices in R, columns are vertices in S) */
+ k = 0, loc = 1;
+ for (i = 1; i <= G->nv; i++)
+ { if (num[i] != 0) continue;
+ /* vertex i in R */
+ ip[++k] = loc;
+ v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { xassert(num[a->head->i] != 0);
+ icn[loc++] = num[a->head->i];
+ }
+ lenr[k] = loc - ip[k];
+ }
+ xassert(loc-1 == G->na);
+ /* make all extra rows empty (all extra columns are empty due to
+ the row-wise format used) */
+ for (k++; k <= n; k++)
+ ip[k] = loc, lenr[k] = 0;
+ /* find a row permutation that maximizes the number of non-zeros
+ on the main diagonal */
+ card = mc21a(n, icn, ip, lenr, iperm, pr, arp, cv, out);
+#if 1 /* 18/II-2010 */
+ /* FIXED: if card = n, arp remains clobbered on exit */
+ for (i = 1; i <= n; i++)
+ arp[i] = 0;
+ for (i = 1; i <= card; i++)
+ { k = iperm[i];
+ xassert(1 <= k && k <= n);
+ xassert(arp[k] == 0);
+ arp[k] = i;
+ }
+#endif
+ /* store solution, if necessary */
+ if (a_x < 0) goto skip;
+ k = 0;
+ for (i = 1; i <= G->nv; i++)
+ { if (num[i] != 0) continue;
+ /* vertex i in R */
+ k++;
+ v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { /* arp[k] is the number of matched column or zero */
+ if (arp[k] == num[a->head->i])
+ { xassert(arp[k] != 0);
+ xij = 1;
+ }
+ else
+ xij = 0;
+ memcpy((char *)a->data + a_x, &xij, sizeof(int));
+ }
+ }
+skip: /* free working arrays */
+ xfree(num);
+ xfree(icn);
+ xfree(ip);
+ xfree(lenr);
+ xfree(iperm);
+ xfree(pr);
+ xfree(arp);
+ xfree(cv);
+ xfree(out);
+ return card;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/asnlp.c b/test/monniaux/glpk-4.65/src/api/asnlp.c
new file mode 100644
index 00000000..cfa925d0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/asnlp.c
@@ -0,0 +1,104 @@
+/* asnlp.c (convert assignment problem to LP) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_asnprob_lp - convert assignment problem to LP
+*
+* SYNOPSIS
+*
+* int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names,
+* int v_set, int a_cost);
+*
+* DESCRIPTION
+*
+* The routine glp_asnprob_lp builds an LP problem, which corresponds
+* to the assignment problem on the specified graph G.
+*
+* RETURNS
+*
+* If the LP problem has been successfully built, the routine returns
+* zero, otherwise, non-zero. */
+
+int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names,
+ int v_set, int a_cost)
+{ glp_vertex *v;
+ glp_arc *a;
+ int i, j, ret, ind[1+2];
+ double cost, val[1+2];
+ if (!(form == GLP_ASN_MIN || form == GLP_ASN_MAX ||
+ form == GLP_ASN_MMP))
+ xerror("glp_asnprob_lp: form = %d; invalid parameter\n",
+ form);
+ if (!(names == GLP_ON || names == GLP_OFF))
+ xerror("glp_asnprob_lp: names = %d; invalid parameter\n",
+ names);
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_asnprob_lp: v_set = %d; invalid offset\n",
+ v_set);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_asnprob_lp: a_cost = %d; invalid offset\n",
+ a_cost);
+ ret = glp_check_asnprob(G, v_set);
+ if (ret != 0) goto done;
+ glp_erase_prob(P);
+ if (names) glp_set_prob_name(P, G->name);
+ glp_set_obj_dir(P, form == GLP_ASN_MIN ? GLP_MIN : GLP_MAX);
+ if (G->nv > 0) glp_add_rows(P, G->nv);
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (names) glp_set_row_name(P, i, v->name);
+ glp_set_row_bnds(P, i, form == GLP_ASN_MMP ? GLP_UP : GLP_FX,
+ 1.0, 1.0);
+ }
+ if (G->na > 0) glp_add_cols(P, G->na);
+ for (i = 1, j = 0; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { j++;
+ if (names)
+ { char name[50+1];
+ sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
+ xassert(strlen(name) < sizeof(name));
+ glp_set_col_name(P, j, name);
+ }
+ ind[1] = a->tail->i, val[1] = +1.0;
+ ind[2] = a->head->i, val[2] = +1.0;
+ glp_set_mat_col(P, j, 2, ind, val);
+ glp_set_col_bnds(P, j, GLP_DB, 0.0, 1.0);
+ if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 1.0;
+ glp_set_obj_coef(P, j, cost);
+ }
+ }
+ xassert(j == G->na);
+done: return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/asnokalg.c b/test/monniaux/glpk-4.65/src/api/asnokalg.c
new file mode 100644
index 00000000..d55dbac7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/asnokalg.c
@@ -0,0 +1,154 @@
+/* asnokalg.c (solve assignment problem with out-of-kilter alg.) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "okalg.h"
+
+int glp_asnprob_okalg(int form, glp_graph *G, int v_set, int a_cost,
+ double *sol, int a_x)
+{ /* solve assignment problem with out-of-kilter algorithm */
+ glp_vertex *v;
+ glp_arc *a;
+ int nv, na, i, k, *tail, *head, *low, *cap, *cost, *x, *pi, ret;
+ double temp;
+ if (!(form == GLP_ASN_MIN || form == GLP_ASN_MAX ||
+ form == GLP_ASN_MMP))
+ xerror("glp_asnprob_okalg: form = %d; invalid parameter\n",
+ form);
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_asnprob_okalg: v_set = %d; invalid offset\n",
+ v_set);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_asnprob_okalg: a_cost = %d; invalid offset\n",
+ a_cost);
+ if (a_x >= 0 && a_x > G->a_size - (int)sizeof(int))
+ xerror("glp_asnprob_okalg: a_x = %d; invalid offset\n", a_x);
+ if (glp_check_asnprob(G, v_set))
+ return GLP_EDATA;
+ /* nv is the total number of nodes in the resulting network */
+ nv = G->nv + 1;
+ /* na is the total number of arcs in the resulting network */
+ na = G->na + G->nv;
+ /* allocate working arrays */
+ tail = xcalloc(1+na, sizeof(int));
+ head = xcalloc(1+na, sizeof(int));
+ low = xcalloc(1+na, sizeof(int));
+ cap = xcalloc(1+na, sizeof(int));
+ cost = xcalloc(1+na, sizeof(int));
+ x = xcalloc(1+na, sizeof(int));
+ pi = xcalloc(1+nv, sizeof(int));
+ /* construct the resulting network */
+ k = 0;
+ /* (original arcs) */
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ tail[k] = a->tail->i;
+ head[k] = a->head->i;
+ low[k] = 0;
+ cap[k] = 1;
+ if (a_cost >= 0)
+ memcpy(&temp, (char *)a->data + a_cost, sizeof(double));
+ else
+ temp = 1.0;
+ if (!(fabs(temp) <= (double)INT_MAX && temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ cost[k] = (int)temp;
+ if (form != GLP_ASN_MIN) cost[k] = - cost[k];
+ }
+ }
+ /* (artificial arcs) */
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ k++;
+ if (v->out == NULL)
+ tail[k] = i, head[k] = nv;
+ else if (v->in == NULL)
+ tail[k] = nv, head[k] = i;
+ else
+ xassert(v != v);
+ low[k] = (form == GLP_ASN_MMP ? 0 : 1);
+ cap[k] = 1;
+ cost[k] = 0;
+ }
+ xassert(k == na);
+ /* find minimal-cost circulation in the resulting network */
+ ret = okalg(nv, na, tail, head, low, cap, cost, x, pi);
+ switch (ret)
+ { case 0:
+ /* optimal circulation found */
+ ret = 0;
+ break;
+ case 1:
+ /* no feasible circulation exists */
+ ret = GLP_ENOPFS;
+ break;
+ case 2:
+ /* integer overflow occured */
+ ret = GLP_ERANGE;
+ goto done;
+ case 3:
+ /* optimality test failed (logic error) */
+ ret = GLP_EFAIL;
+ goto done;
+ default:
+ xassert(ret != ret);
+ }
+ /* store solution components */
+ /* (objective function = the total cost) */
+ if (sol != NULL)
+ { temp = 0.0;
+ for (k = 1; k <= na; k++)
+ temp += (double)cost[k] * (double)x[k];
+ if (form != GLP_ASN_MIN) temp = - temp;
+ *sol = temp;
+ }
+ /* (arc flows) */
+ if (a_x >= 0)
+ { k = 0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ if (ret == 0)
+ xassert(x[k] == 0 || x[k] == 1);
+ memcpy((char *)a->data + a_x, &x[k], sizeof(int));
+ }
+ }
+ }
+done: /* free working arrays */
+ xfree(tail);
+ xfree(head);
+ xfree(low);
+ xfree(cap);
+ xfree(cost);
+ xfree(x);
+ xfree(pi);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/ckasn.c b/test/monniaux/glpk-4.65/src/api/ckasn.c
new file mode 100644
index 00000000..56221a8a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/ckasn.c
@@ -0,0 +1,78 @@
+/* ckasn.c (check correctness of assignment problem data) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_check_asnprob - check correctness of assignment problem data
+*
+* SYNOPSIS
+*
+* int glp_check_asnprob(glp_graph *G, int v_set);
+*
+* RETURNS
+*
+* If the specified assignment problem data are correct, the routine
+* glp_check_asnprob returns zero, otherwise, non-zero. */
+
+int glp_check_asnprob(glp_graph *G, int v_set)
+{ glp_vertex *v;
+ int i, k, ret = 0;
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_check_asnprob: v_set = %d; invalid offset\n",
+ v_set);
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (v_set >= 0)
+ { memcpy(&k, (char *)v->data + v_set, sizeof(int));
+ if (k == 0)
+ { if (v->in != NULL)
+ { ret = 1;
+ break;
+ }
+ }
+ else if (k == 1)
+ { if (v->out != NULL)
+ { ret = 2;
+ break;
+ }
+ }
+ else
+ { ret = 3;
+ break;
+ }
+ }
+ else
+ { if (v->in != NULL && v->out != NULL)
+ { ret = 4;
+ break;
+ }
+ }
+ }
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/ckcnf.c b/test/monniaux/glpk-4.65/src/api/ckcnf.c
new file mode 100644
index 00000000..0ee47ed9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/ckcnf.c
@@ -0,0 +1,82 @@
+/* ckcnf.c (check for CNF-SAT problem instance) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+int glp_check_cnfsat(glp_prob *P)
+{ /* check for CNF-SAT problem instance */
+ int m = P->m;
+ int n = P->n;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, j, neg;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_check_cnfsat: P = %p; invalid problem object\n",
+ P);
+#endif
+ /* check columns */
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ /* the variable should be binary */
+ if (!(col->kind == GLP_IV && col->type == GLP_DB &&
+ col->lb == 0.0 && col->ub == 1.0))
+ return 1;
+ }
+ /* objective function should be zero */
+ if (P->c0 != 0.0)
+ return 2;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ if (col->coef != 0.0)
+ return 3;
+ }
+ /* check rows */
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ /* the row should be of ">=" type */
+ if (row->type != GLP_LO)
+ return 4;
+ /* check constraint coefficients */
+ neg = 0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { /* the constraint coefficient should be +1 or -1 */
+ if (aij->val == +1.0)
+ ;
+ else if (aij->val == -1.0)
+ neg++;
+ else
+ return 5;
+ }
+ /* the right-hand side should be (1 - neg), where neg is the
+ number of negative constraint coefficients in the row */
+ if (row->lb != (double)(1 - neg))
+ return 6;
+ }
+ /* congratulations; this is CNF-SAT */
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/cplex.c b/test/monniaux/glpk-4.65/src/api/cplex.c
new file mode 100644
index 00000000..8403a646
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/cplex.c
@@ -0,0 +1,1283 @@
+/* cplex.c (CPLEX LP format routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_init_cpxcp - initialize CPLEX LP format control parameters
+*
+* SYNOPSIS
+*
+* void glp_init_cpxcp(glp_cpxcp *parm):
+*
+* The routine glp_init_cpxcp initializes control parameters used by
+* the CPLEX LP input/output routines glp_read_lp and glp_write_lp with
+* default values.
+*
+* Default values of the control parameters are stored in the glp_cpxcp
+* structure, which the parameter parm points to. */
+
+void glp_init_cpxcp(glp_cpxcp *parm)
+{ xassert(parm != NULL);
+ return;
+}
+
+static void check_parm(const char *func, const glp_cpxcp *parm)
+{ /* check control parameters */
+ xassert(func != NULL);
+ xassert(parm != NULL);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_read_lp - read problem data in CPLEX LP format
+*
+* SYNOPSIS
+*
+* int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char
+* *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_lp reads problem data in CPLEX LP format from
+* a text file.
+*
+* The parameter parm is a pointer to the structure glp_cpxcp, which
+* specifies control parameters used by the routine. If parm is NULL,
+* the routine uses default settings.
+*
+* The character string fname specifies a name of the text file to be
+* read.
+*
+* Note that before reading data the current content of the problem
+* object is completely erased with the routine glp_erase_prob.
+*
+* RETURNS
+*
+* If the operation was successful, the routine glp_read_lp returns
+* zero. Otherwise, it prints an error message and returns non-zero. */
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* LP/MIP problem object */
+ const glp_cpxcp *parm;
+ /* pointer to control parameters */
+ const char *fname;
+ /* name of input CPLEX LP file */
+ glp_file *fp;
+ /* stream assigned to input CPLEX LP file */
+ jmp_buf jump;
+ /* label for go to in case of error */
+ int count;
+ /* line count */
+ int c;
+ /* current character or EOF */
+ int token;
+ /* current token: */
+#define T_EOF 0x00 /* end of file */
+#define T_MINIMIZE 0x01 /* keyword 'minimize' */
+#define T_MAXIMIZE 0x02 /* keyword 'maximize' */
+#define T_SUBJECT_TO 0x03 /* keyword 'subject to' */
+#define T_BOUNDS 0x04 /* keyword 'bounds' */
+#define T_GENERAL 0x05 /* keyword 'general' */
+#define T_INTEGER 0x06 /* keyword 'integer' */
+#define T_BINARY 0x07 /* keyword 'binary' */
+#define T_END 0x08 /* keyword 'end' */
+#define T_NAME 0x09 /* symbolic name */
+#define T_NUMBER 0x0A /* numeric constant */
+#define T_PLUS 0x0B /* delimiter '+' */
+#define T_MINUS 0x0C /* delimiter '-' */
+#define T_COLON 0x0D /* delimiter ':' */
+#define T_LE 0x0E /* delimiter '<=' */
+#define T_GE 0x0F /* delimiter '>=' */
+#define T_EQ 0x10 /* delimiter '=' */
+ char image[255+1];
+ /* image of current token */
+ int imlen;
+ /* length of token image */
+ double value;
+ /* value of numeric constant */
+ int n_max;
+ /* length of the following five arrays (enlarged automatically,
+ if necessary) */
+ int *ind; /* int ind[1+n_max]; */
+ double *val; /* double val[1+n_max]; */
+ char *flag; /* char flag[1+n_max]; */
+ /* working arrays used to construct linear forms */
+ double *lb; /* double lb[1+n_max]; */
+ double *ub; /* double ub[1+n_max]; */
+ /* lower and upper bounds of variables (columns) */
+#if 1 /* 27/VII-2013 */
+ int lb_warn, ub_warn;
+ /* warning 'lower/upper bound redefined' already issued */
+#endif
+};
+
+#define CHAR_SET "!\"#$%&()/,.;?@_`'{}|~"
+/* characters that may appear in symbolic names */
+
+static void error(struct csa *csa, const char *fmt, ...)
+{ /* print error message and terminate processing */
+ va_list arg;
+ xprintf("%s:%d: ", csa->fname, csa->count);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ longjmp(csa->jump, 1);
+ /* no return */
+}
+
+static void warning(struct csa *csa, const char *fmt, ...)
+{ /* print warning message and continue processing */
+ va_list arg;
+ xprintf("%s:%d: warning: ", csa->fname, csa->count);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ return;
+}
+
+static void read_char(struct csa *csa)
+{ /* read next character from input file */
+ int c;
+ xassert(csa->c != EOF);
+ if (csa->c == '\n') csa->count++;
+ c = glp_getc(csa->fp);
+ if (c < 0)
+ { if (glp_ioerr(csa->fp))
+ error(csa, "read error - %s\n", get_err_msg());
+ else if (csa->c == '\n')
+ { csa->count--;
+ c = EOF;
+ }
+ else
+ { warning(csa, "missing final end of line\n");
+ c = '\n';
+ }
+ }
+ else if (c == '\n')
+ ;
+ else if (isspace(c))
+ c = ' ';
+ else if (iscntrl(c))
+ error(csa, "invalid control character 0x%02X\n", c);
+ csa->c = c;
+ return;
+}
+
+static void add_char(struct csa *csa)
+{ /* append current character to current token */
+ if (csa->imlen == sizeof(csa->image)-1)
+ error(csa, "token '%.15s...' too long\n", csa->image);
+ csa->image[csa->imlen++] = (char)csa->c;
+ csa->image[csa->imlen] = '\0';
+ read_char(csa);
+ return;
+}
+
+static int the_same(char *s1, char *s2)
+{ /* compare two character strings ignoring case sensitivity */
+ for (; *s1 != '\0'; s1++, s2++)
+ { if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2))
+ return 0;
+ }
+ return 1;
+}
+
+static void scan_token(struct csa *csa)
+{ /* scan next token */
+ int flag;
+ csa->token = -1;
+ csa->image[0] = '\0';
+ csa->imlen = 0;
+ csa->value = 0.0;
+loop: flag = 0;
+ /* skip non-significant characters */
+ while (csa->c == ' ') read_char(csa);
+ /* recognize and scan current token */
+ if (csa->c == EOF)
+ csa->token = T_EOF;
+ else if (csa->c == '\n')
+ { read_char(csa);
+ /* if the next character is letter, it may begin a keyword */
+ if (isalpha(csa->c))
+ { flag = 1;
+ goto name;
+ }
+ goto loop;
+ }
+ else if (csa->c == '\\')
+ { /* comment; ignore everything until end-of-line */
+ while (csa->c != '\n') read_char(csa);
+ goto loop;
+ }
+ else if (isalpha(csa->c) || csa->c != '.' && strchr(CHAR_SET,
+ csa->c) != NULL)
+name: { /* symbolic name */
+ csa->token = T_NAME;
+ while (isalnum(csa->c) || strchr(CHAR_SET, csa->c) != NULL)
+ add_char(csa);
+ if (flag)
+ { /* check for keyword */
+ if (the_same(csa->image, "minimize"))
+ csa->token = T_MINIMIZE;
+ else if (the_same(csa->image, "minimum"))
+ csa->token = T_MINIMIZE;
+ else if (the_same(csa->image, "min"))
+ csa->token = T_MINIMIZE;
+ else if (the_same(csa->image, "maximize"))
+ csa->token = T_MAXIMIZE;
+ else if (the_same(csa->image, "maximum"))
+ csa->token = T_MAXIMIZE;
+ else if (the_same(csa->image, "max"))
+ csa->token = T_MAXIMIZE;
+ else if (the_same(csa->image, "subject"))
+ { if (csa->c == ' ')
+ { read_char(csa);
+ if (tolower(csa->c) == 't')
+ { csa->token = T_SUBJECT_TO;
+ csa->image[csa->imlen++] = ' ';
+ csa->image[csa->imlen] = '\0';
+ add_char(csa);
+ if (tolower(csa->c) != 'o')
+ error(csa, "keyword 'subject to' incomplete\n");
+ add_char(csa);
+ if (isalpha(csa->c))
+ error(csa, "keyword '%s%c...' not recognized\n",
+ csa->image, csa->c);
+ }
+ }
+ }
+ else if (the_same(csa->image, "such"))
+ { if (csa->c == ' ')
+ { read_char(csa);
+ if (tolower(csa->c) == 't')
+ { csa->token = T_SUBJECT_TO;
+ csa->image[csa->imlen++] = ' ';
+ csa->image[csa->imlen] = '\0';
+ add_char(csa);
+ if (tolower(csa->c) != 'h')
+err: error(csa, "keyword 'such that' incomplete\n");
+ add_char(csa);
+ if (tolower(csa->c) != 'a') goto err;
+ add_char(csa);
+ if (tolower(csa->c) != 't') goto err;
+ add_char(csa);
+ if (isalpha(csa->c))
+ error(csa, "keyword '%s%c...' not recognized\n",
+ csa->image, csa->c);
+ }
+ }
+ }
+ else if (the_same(csa->image, "st"))
+ csa->token = T_SUBJECT_TO;
+ else if (the_same(csa->image, "s.t."))
+ csa->token = T_SUBJECT_TO;
+ else if (the_same(csa->image, "st."))
+ csa->token = T_SUBJECT_TO;
+ else if (the_same(csa->image, "bounds"))
+ csa->token = T_BOUNDS;
+ else if (the_same(csa->image, "bound"))
+ csa->token = T_BOUNDS;
+ else if (the_same(csa->image, "general"))
+ csa->token = T_GENERAL;
+ else if (the_same(csa->image, "generals"))
+ csa->token = T_GENERAL;
+ else if (the_same(csa->image, "gen"))
+ csa->token = T_GENERAL;
+ else if (the_same(csa->image, "integer"))
+ csa->token = T_INTEGER;
+ else if (the_same(csa->image, "integers"))
+ csa->token = T_INTEGER;
+ else if (the_same(csa->image, "int"))
+ csa->token = T_INTEGER;
+ else if (the_same(csa->image, "binary"))
+ csa->token = T_BINARY;
+ else if (the_same(csa->image, "binaries"))
+ csa->token = T_BINARY;
+ else if (the_same(csa->image, "bin"))
+ csa->token = T_BINARY;
+ else if (the_same(csa->image, "end"))
+ csa->token = T_END;
+ }
+ }
+ else if (isdigit(csa->c) || csa->c == '.')
+ { /* numeric constant */
+ csa->token = T_NUMBER;
+ /* scan integer part */
+ while (isdigit(csa->c)) add_char(csa);
+ /* scan optional fractional part (it is mandatory, if there is
+ no integer part) */
+ if (csa->c == '.')
+ { add_char(csa);
+ if (csa->imlen == 1 && !isdigit(csa->c))
+ error(csa, "invalid use of decimal point\n");
+ while (isdigit(csa->c)) add_char(csa);
+ }
+ /* scan optional decimal exponent */
+ if (csa->c == 'e' || csa->c == 'E')
+ { add_char(csa);
+ if (csa->c == '+' || csa->c == '-') add_char(csa);
+ if (!isdigit(csa->c))
+ error(csa, "numeric constant '%s' incomplete\n",
+ csa->image);
+ while (isdigit(csa->c)) add_char(csa);
+ }
+ /* convert the numeric constant to floating-point */
+ if (str2num(csa->image, &csa->value))
+ error(csa, "numeric constant '%s' out of range\n",
+ csa->image);
+ }
+ else if (csa->c == '+')
+ csa->token = T_PLUS, add_char(csa);
+ else if (csa->c == '-')
+ csa->token = T_MINUS, add_char(csa);
+ else if (csa->c == ':')
+ csa->token = T_COLON, add_char(csa);
+ else if (csa->c == '<')
+ { csa->token = T_LE, add_char(csa);
+ if (csa->c == '=') add_char(csa);
+ }
+ else if (csa->c == '>')
+ { csa->token = T_GE, add_char(csa);
+ if (csa->c == '=') add_char(csa);
+ }
+ else if (csa->c == '=')
+ { csa->token = T_EQ, add_char(csa);
+ if (csa->c == '<')
+ csa->token = T_LE, add_char(csa);
+ else if (csa->c == '>')
+ csa->token = T_GE, add_char(csa);
+ }
+ else
+ error(csa, "character '%c' not recognized\n", csa->c);
+ /* skip non-significant characters */
+ while (csa->c == ' ') read_char(csa);
+ return;
+}
+
+static int find_col(struct csa *csa, char *name)
+{ /* find column by its symbolic name */
+ int j;
+ j = glp_find_col(csa->P, name);
+ if (j == 0)
+ { /* not found; create new column */
+ j = glp_add_cols(csa->P, 1);
+ glp_set_col_name(csa->P, j, name);
+ /* enlarge working arrays, if necessary */
+ if (csa->n_max < j)
+ { int n_max = csa->n_max;
+ int *ind = csa->ind;
+ double *val = csa->val;
+ char *flag = csa->flag;
+ double *lb = csa->lb;
+ double *ub = csa->ub;
+ csa->n_max += csa->n_max;
+ csa->ind = xcalloc(1+csa->n_max, sizeof(int));
+ memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int));
+ xfree(ind);
+ csa->val = xcalloc(1+csa->n_max, sizeof(double));
+ memcpy(&csa->val[1], &val[1], n_max * sizeof(double));
+ xfree(val);
+ csa->flag = xcalloc(1+csa->n_max, sizeof(char));
+ memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
+ memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char));
+ xfree(flag);
+ csa->lb = xcalloc(1+csa->n_max, sizeof(double));
+ memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double));
+ xfree(lb);
+ csa->ub = xcalloc(1+csa->n_max, sizeof(double));
+ memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double));
+ xfree(ub);
+ }
+ csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX;
+ }
+ return j;
+}
+
+/***********************************************************************
+* parse_linear_form - parse linear form
+*
+* This routine parses the linear form using the following syntax:
+*
+* <variable> ::= <symbolic name>
+* <coefficient> ::= <numeric constant>
+* <term> ::= <variable> | <numeric constant> <variable>
+* <linear form> ::= <term> | + <term> | - <term> |
+* <linear form> + <term> | <linear form> - <term>
+*
+* The routine returns the number of terms in the linear form. */
+
+static int parse_linear_form(struct csa *csa)
+{ int j, k, len = 0, newlen;
+ double s, coef;
+loop: /* parse an optional sign */
+ if (csa->token == T_PLUS)
+ s = +1.0, scan_token(csa);
+ else if (csa->token == T_MINUS)
+ s = -1.0, scan_token(csa);
+ else
+ s = +1.0;
+ /* parse an optional coefficient */
+ if (csa->token == T_NUMBER)
+ coef = csa->value, scan_token(csa);
+ else
+ coef = 1.0;
+ /* parse a variable name */
+ if (csa->token != T_NAME)
+ error(csa, "missing variable name\n");
+ /* find the corresponding column */
+ j = find_col(csa, csa->image);
+ /* check if the variable is already used in the linear form */
+ if (csa->flag[j])
+ error(csa, "multiple use of variable '%s' not allowed\n",
+ csa->image);
+ /* add new term to the linear form */
+ len++, csa->ind[len] = j, csa->val[len] = s * coef;
+ /* and mark that the variable is used in the linear form */
+ csa->flag[j] = 1;
+ scan_token(csa);
+ /* if the next token is a sign, there is another term */
+ if (csa->token == T_PLUS || csa->token == T_MINUS) goto loop;
+ /* clear marks of the variables used in the linear form */
+ for (k = 1; k <= len; k++) csa->flag[csa->ind[k]] = 0;
+ /* remove zero coefficients */
+ newlen = 0;
+ for (k = 1; k <= len; k++)
+ { if (csa->val[k] != 0.0)
+ { newlen++;
+ csa->ind[newlen] = csa->ind[k];
+ csa->val[newlen] = csa->val[k];
+ }
+ }
+ return newlen;
+}
+
+/***********************************************************************
+* parse_objective - parse objective function
+*
+* This routine parses definition of the objective function using the
+* following syntax:
+*
+* <obj sense> ::= minimize | minimum | min | maximize | maximum | max
+* <obj name> ::= <empty> | <symbolic name> :
+* <obj function> ::= <obj sense> <obj name> <linear form> */
+
+static void parse_objective(struct csa *csa)
+{ /* parse objective sense */
+ int k, len;
+ /* parse the keyword 'minimize' or 'maximize' */
+ if (csa->token == T_MINIMIZE)
+ glp_set_obj_dir(csa->P, GLP_MIN);
+ else if (csa->token == T_MAXIMIZE)
+ glp_set_obj_dir(csa->P, GLP_MAX);
+ else
+ xassert(csa != csa);
+ scan_token(csa);
+ /* parse objective name */
+ if (csa->token == T_NAME && csa->c == ':')
+ { /* objective name is followed by a colon */
+ glp_set_obj_name(csa->P, csa->image);
+ scan_token(csa);
+ xassert(csa->token == T_COLON);
+ scan_token(csa);
+ }
+ else
+ { /* objective name is not specified; use default */
+ glp_set_obj_name(csa->P, "obj");
+ }
+ /* parse linear form */
+ len = parse_linear_form(csa);
+ for (k = 1; k <= len; k++)
+ glp_set_obj_coef(csa->P, csa->ind[k], csa->val[k]);
+ return;
+}
+
+/***********************************************************************
+* parse_constraints - parse constraints section
+*
+* This routine parses the constraints section using the following
+* syntax:
+*
+* <row name> ::= <empty> | <symbolic name> :
+* <row sense> ::= < | <= | =< | > | >= | => | =
+* <right-hand side> ::= <numeric constant> | + <numeric constant> |
+* - <numeric constant>
+* <constraint> ::= <row name> <linear form> <row sense>
+* <right-hand side>
+* <subject to> ::= subject to | such that | st | s.t. | st.
+* <constraints section> ::= <subject to> <constraint> |
+* <constraints section> <constraint> */
+
+static void parse_constraints(struct csa *csa)
+{ int i, len, type;
+ double s;
+ /* parse the keyword 'subject to' */
+ xassert(csa->token == T_SUBJECT_TO);
+ scan_token(csa);
+loop: /* create new row (constraint) */
+ i = glp_add_rows(csa->P, 1);
+ /* parse row name */
+ if (csa->token == T_NAME && csa->c == ':')
+ { /* row name is followed by a colon */
+ if (glp_find_row(csa->P, csa->image) != 0)
+ error(csa, "constraint '%s' multiply defined\n",
+ csa->image);
+ glp_set_row_name(csa->P, i, csa->image);
+ scan_token(csa);
+ xassert(csa->token == T_COLON);
+ scan_token(csa);
+ }
+ else
+ { /* row name is not specified; use default */
+ char name[50];
+ sprintf(name, "r.%d", csa->count);
+ glp_set_row_name(csa->P, i, name);
+ }
+ /* parse linear form */
+ len = parse_linear_form(csa);
+ glp_set_mat_row(csa->P, i, len, csa->ind, csa->val);
+ /* parse constraint sense */
+ if (csa->token == T_LE)
+ type = GLP_UP, scan_token(csa);
+ else if (csa->token == T_GE)
+ type = GLP_LO, scan_token(csa);
+ else if (csa->token == T_EQ)
+ type = GLP_FX, scan_token(csa);
+ else
+ error(csa, "missing constraint sense\n");
+ /* parse right-hand side */
+ if (csa->token == T_PLUS)
+ s = +1.0, scan_token(csa);
+ else if (csa->token == T_MINUS)
+ s = -1.0, scan_token(csa);
+ else
+ s = +1.0;
+ if (csa->token != T_NUMBER)
+ error(csa, "missing right-hand side\n");
+ glp_set_row_bnds(csa->P, i, type, s * csa->value, s * csa->value);
+ /* the rest of the current line must be empty */
+ if (!(csa->c == '\n' || csa->c == EOF))
+ error(csa, "invalid symbol(s) beyond right-hand side\n");
+ scan_token(csa);
+ /* if the next token is a sign, numeric constant, or a symbolic
+ name, here is another constraint */
+ if (csa->token == T_PLUS || csa->token == T_MINUS ||
+ csa->token == T_NUMBER || csa->token == T_NAME) goto loop;
+ return;
+}
+
+static void set_lower_bound(struct csa *csa, int j, double lb)
+{ /* set lower bound of j-th variable */
+ if (csa->lb[j] != +DBL_MAX && !csa->lb_warn)
+ { warning(csa, "lower bound of variable '%s' redefined\n",
+ glp_get_col_name(csa->P, j));
+ csa->lb_warn = 1;
+ }
+ csa->lb[j] = lb;
+ return;
+}
+
+static void set_upper_bound(struct csa *csa, int j, double ub)
+{ /* set upper bound of j-th variable */
+ if (csa->ub[j] != -DBL_MAX && !csa->ub_warn)
+ { warning(csa, "upper bound of variable '%s' redefined\n",
+ glp_get_col_name(csa->P, j));
+ csa->ub_warn = 1;
+ }
+ csa->ub[j] = ub;
+ return;
+}
+
+/***********************************************************************
+* parse_bounds - parse bounds section
+*
+* This routine parses the bounds section using the following syntax:
+*
+* <variable> ::= <symbolic name>
+* <infinity> ::= infinity | inf
+* <bound> ::= <numeric constant> | + <numeric constant> |
+* - <numeric constant> | + <infinity> | - <infinity>
+* <lt> ::= < | <= | =<
+* <gt> ::= > | >= | =>
+* <bound definition> ::= <bound> <lt> <variable> <lt> <bound> |
+* <bound> <lt> <variable> | <variable> <lt> <bound> |
+* <variable> <gt> <bound> | <variable> = <bound> | <variable> free
+* <bounds> ::= bounds | bound
+* <bounds section> ::= <bounds> |
+* <bounds section> <bound definition> */
+
+static void parse_bounds(struct csa *csa)
+{ int j, lb_flag;
+ double lb, s;
+ /* parse the keyword 'bounds' */
+ xassert(csa->token == T_BOUNDS);
+ scan_token(csa);
+loop: /* bound definition can start with a sign, numeric constant, or
+ a symbolic name */
+ if (!(csa->token == T_PLUS || csa->token == T_MINUS ||
+ csa->token == T_NUMBER || csa->token == T_NAME)) goto done;
+ /* parse bound definition */
+ if (csa->token == T_PLUS || csa->token == T_MINUS)
+ { /* parse signed lower bound */
+ lb_flag = 1;
+ s = (csa->token == T_PLUS ? +1.0 : -1.0);
+ scan_token(csa);
+ if (csa->token == T_NUMBER)
+ lb = s * csa->value, scan_token(csa);
+ else if (the_same(csa->image, "infinity") ||
+ the_same(csa->image, "inf"))
+ { if (s > 0.0)
+ error(csa, "invalid use of '+inf' as lower bound\n");
+ lb = -DBL_MAX, scan_token(csa);
+ }
+ else
+ error(csa, "missing lower bound\n");
+ }
+ else if (csa->token == T_NUMBER)
+ { /* parse unsigned lower bound */
+ lb_flag = 1;
+ lb = csa->value, scan_token(csa);
+ }
+ else
+ { /* lower bound is not specified */
+ lb_flag = 0;
+ }
+ /* parse the token that should follow the lower bound */
+ if (lb_flag)
+ { if (csa->token != T_LE)
+ error(csa, "missing '<', '<=', or '=<' after lower bound\n")
+ ;
+ scan_token(csa);
+ }
+ /* parse variable name */
+ if (csa->token != T_NAME)
+ error(csa, "missing variable name\n");
+ j = find_col(csa, csa->image);
+ /* set lower bound */
+ if (lb_flag) set_lower_bound(csa, j, lb);
+ scan_token(csa);
+ /* parse the context that follows the variable name */
+ if (csa->token == T_LE)
+ { /* parse upper bound */
+ scan_token(csa);
+ if (csa->token == T_PLUS || csa->token == T_MINUS)
+ { /* parse signed upper bound */
+ s = (csa->token == T_PLUS ? +1.0 : -1.0);
+ scan_token(csa);
+ if (csa->token == T_NUMBER)
+ { set_upper_bound(csa, j, s * csa->value);
+ scan_token(csa);
+ }
+ else if (the_same(csa->image, "infinity") ||
+ the_same(csa->image, "inf"))
+ { if (s < 0.0)
+ error(csa, "invalid use of '-inf' as upper bound\n");
+ set_upper_bound(csa, j, +DBL_MAX);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing upper bound\n");
+ }
+ else if (csa->token == T_NUMBER)
+ { /* parse unsigned upper bound */
+ set_upper_bound(csa, j, csa->value);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing upper bound\n");
+ }
+ else if (csa->token == T_GE)
+ { /* parse lower bound */
+ if (lb_flag)
+ { /* the context '... <= x >= ...' is invalid */
+ error(csa, "invalid bound definition\n");
+ }
+ scan_token(csa);
+ if (csa->token == T_PLUS || csa->token == T_MINUS)
+ { /* parse signed lower bound */
+ s = (csa->token == T_PLUS ? +1.0 : -1.0);
+ scan_token(csa);
+ if (csa->token == T_NUMBER)
+ { set_lower_bound(csa, j, s * csa->value);
+ scan_token(csa);
+ }
+ else if (the_same(csa->image, "infinity") ||
+ the_same(csa->image, "inf") == 0)
+ { if (s > 0.0)
+ error(csa, "invalid use of '+inf' as lower bound\n");
+ set_lower_bound(csa, j, -DBL_MAX);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing lower bound\n");
+ }
+ else if (csa->token == T_NUMBER)
+ { /* parse unsigned lower bound */
+ set_lower_bound(csa, j, csa->value);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing lower bound\n");
+ }
+ else if (csa->token == T_EQ)
+ { /* parse fixed value */
+ if (lb_flag)
+ { /* the context '... <= x = ...' is invalid */
+ error(csa, "invalid bound definition\n");
+ }
+ scan_token(csa);
+ if (csa->token == T_PLUS || csa->token == T_MINUS)
+ { /* parse signed fixed value */
+ s = (csa->token == T_PLUS ? +1.0 : -1.0);
+ scan_token(csa);
+ if (csa->token == T_NUMBER)
+ { set_lower_bound(csa, j, s * csa->value);
+ set_upper_bound(csa, j, s * csa->value);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing fixed value\n");
+ }
+ else if (csa->token == T_NUMBER)
+ { /* parse unsigned fixed value */
+ set_lower_bound(csa, j, csa->value);
+ set_upper_bound(csa, j, csa->value);
+ scan_token(csa);
+ }
+ else
+ error(csa, "missing fixed value\n");
+ }
+ else if (the_same(csa->image, "free"))
+ { /* parse the keyword 'free' */
+ if (lb_flag)
+ { /* the context '... <= x free ...' is invalid */
+ error(csa, "invalid bound definition\n");
+ }
+ set_lower_bound(csa, j, -DBL_MAX);
+ set_upper_bound(csa, j, +DBL_MAX);
+ scan_token(csa);
+ }
+ else if (!lb_flag)
+ { /* neither lower nor upper bounds are specified */
+ error(csa, "invalid bound definition\n");
+ }
+ goto loop;
+done: return;
+}
+
+/***********************************************************************
+* parse_integer - parse general, integer, or binary section
+*
+* <variable> ::= <symbolic name>
+* <general> ::= general | generals | gen
+* <integer> ::= integer | integers | int
+* <binary> ::= binary | binaries | bin
+* <section head> ::= <general> <integer> <binary>
+* <additional section> ::= <section head> |
+* <additional section> <variable> */
+
+static void parse_integer(struct csa *csa)
+{ int j, binary;
+ /* parse the keyword 'general', 'integer', or 'binary' */
+ if (csa->token == T_GENERAL)
+ binary = 0, scan_token(csa);
+ else if (csa->token == T_INTEGER)
+ binary = 0, scan_token(csa);
+ else if (csa->token == T_BINARY)
+ binary = 1, scan_token(csa);
+ else
+ xassert(csa != csa);
+ /* parse list of variables (may be empty) */
+ while (csa->token == T_NAME)
+ { /* find the corresponding column */
+ j = find_col(csa, csa->image);
+ /* change kind of the variable */
+ glp_set_col_kind(csa->P, j, GLP_IV);
+ /* set bounds for the binary variable */
+ if (binary)
+#if 0 /* 07/VIII-2013 */
+ { set_lower_bound(csa, j, 0.0);
+ set_upper_bound(csa, j, 1.0);
+ }
+#else
+ { set_lower_bound(csa, j,
+ csa->lb[j] == +DBL_MAX ? 0.0 : csa->lb[j]);
+ set_upper_bound(csa, j,
+ csa->ub[j] == -DBL_MAX ? 1.0 : csa->ub[j]);
+ }
+#endif
+ scan_token(csa);
+ }
+ return;
+}
+
+int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
+{ /* read problem data in CPLEX LP format */
+ glp_cpxcp _parm;
+ struct csa _csa, *csa = &_csa;
+ int ret;
+ xprintf("Reading problem data from '%s'...\n", fname);
+ if (parm == NULL)
+ glp_init_cpxcp(&_parm), parm = &_parm;
+ /* check control parameters */
+ check_parm("glp_read_lp", parm);
+ /* initialize common storage area */
+ csa->P = P;
+ csa->parm = parm;
+ csa->fname = fname;
+ csa->fp = NULL;
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->count = 0;
+ csa->c = '\n';
+ csa->token = T_EOF;
+ csa->image[0] = '\0';
+ csa->imlen = 0;
+ csa->value = 0.0;
+ csa->n_max = 100;
+ csa->ind = xcalloc(1+csa->n_max, sizeof(int));
+ csa->val = xcalloc(1+csa->n_max, sizeof(double));
+ csa->flag = xcalloc(1+csa->n_max, sizeof(char));
+ memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
+ csa->lb = xcalloc(1+csa->n_max, sizeof(double));
+ csa->ub = xcalloc(1+csa->n_max, sizeof(double));
+#if 1 /* 27/VII-2013 */
+ csa->lb_warn = csa->ub_warn = 0;
+#endif
+ /* erase problem object */
+ glp_erase_prob(P);
+ glp_create_index(P);
+ /* open input CPLEX LP file */
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* scan very first token */
+ scan_token(csa);
+ /* parse definition of the objective function */
+ if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE))
+ error(csa, "'minimize' or 'maximize' keyword missing\n");
+ parse_objective(csa);
+ /* parse constraints section */
+ if (csa->token != T_SUBJECT_TO)
+ error(csa, "constraints section missing\n");
+ parse_constraints(csa);
+ /* parse optional bounds section */
+ if (csa->token == T_BOUNDS) parse_bounds(csa);
+ /* parse optional general, integer, and binary sections */
+ while (csa->token == T_GENERAL ||
+ csa->token == T_INTEGER ||
+ csa->token == T_BINARY) parse_integer(csa);
+ /* check for the keyword 'end' */
+ if (csa->token == T_END)
+ scan_token(csa);
+ else if (csa->token == T_EOF)
+ warning(csa, "keyword 'end' missing\n");
+ else
+ error(csa, "symbol '%s' in wrong position\n", csa->image);
+ /* nothing must follow the keyword 'end' (except comments) */
+ if (csa->token != T_EOF)
+ error(csa, "extra symbol(s) detected beyond 'end'\n");
+ /* set bounds of variables */
+ { int j, type;
+ double lb, ub;
+ for (j = 1; j <= P->n; j++)
+ { lb = csa->lb[j];
+ ub = csa->ub[j];
+ if (lb == +DBL_MAX) lb = 0.0; /* default lb */
+ if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */
+ if (lb == -DBL_MAX && ub == +DBL_MAX)
+ type = GLP_FR;
+ else if (ub == +DBL_MAX)
+ type = GLP_LO;
+ else if (lb == -DBL_MAX)
+ type = GLP_UP;
+ else if (lb != ub)
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_col_bnds(csa->P, j, type, lb, ub);
+ }
+ }
+ /* print some statistics */
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
+ P->nnz, P->nnz == 1 ? "" : "s");
+ if (glp_get_num_int(P) > 0)
+ { int ni = glp_get_num_int(P);
+ int nb = glp_get_num_bin(P);
+ if (ni == 1)
+ { if (nb == 0)
+ xprintf("One variable is integer\n");
+ else
+ xprintf("One variable is binary\n");
+ }
+ else
+ { xprintf("%d integer variables, ", ni);
+ if (nb == 0)
+ xprintf("none");
+ else if (nb == 1)
+ xprintf("one");
+ else if (nb == ni)
+ xprintf("all");
+ else
+ xprintf("%d", nb);
+ xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
+ }
+ }
+ xprintf("%d lines were read\n", csa->count);
+ /* problem data has been successfully read */
+ glp_delete_index(P);
+ glp_sort_matrix(P);
+ ret = 0;
+done: if (csa->fp != NULL) glp_close(csa->fp);
+ xfree(csa->ind);
+ xfree(csa->val);
+ xfree(csa->flag);
+ xfree(csa->lb);
+ xfree(csa->ub);
+ if (ret != 0) glp_erase_prob(P);
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_write_lp - write problem data in CPLEX LP format
+*
+* SYNOPSIS
+*
+* int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char
+* *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_lp writes problem data in CPLEX LP format to
+* a text file.
+*
+* The parameter parm is a pointer to the structure glp_cpxcp, which
+* specifies control parameters used by the routine. If parm is NULL,
+* the routine uses default settings.
+*
+* The character string fname specifies a name of the text file to be
+* written.
+*
+* RETURNS
+*
+* If the operation was successful, the routine glp_write_lp returns
+* zero. Otherwise, it prints an error message and returns non-zero. */
+
+#define csa csa1
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* pointer to problem object */
+ const glp_cpxcp *parm;
+ /* pointer to control parameters */
+};
+
+static int check_name(char *name)
+{ /* check if specified name is valid for CPLEX LP format */
+ if (*name == '.') return 1;
+ if (isdigit((unsigned char)*name)) return 1;
+ for (; *name; name++)
+ { if (!isalnum((unsigned char)*name) &&
+ strchr(CHAR_SET, (unsigned char)*name) == NULL) return 1;
+ }
+ return 0; /* name is ok */
+}
+
+static void adjust_name(char *name)
+{ /* attempt to adjust specified name to make it valid for CPLEX LP
+ format */
+ for (; *name; name++)
+ { if (*name == ' ')
+ *name = '_';
+ else if (*name == '-')
+ *name = '~';
+ else if (*name == '[')
+ *name = '(';
+ else if (*name == ']')
+ *name = ')';
+ }
+ return;
+}
+
+static char *row_name(struct csa *csa, int i, char rname[255+1])
+{ /* construct symbolic name of i-th row (constraint) */
+ const char *name;
+ if (i == 0)
+ name = glp_get_obj_name(csa->P);
+ else
+ name = glp_get_row_name(csa->P, i);
+ if (name == NULL) goto fake;
+ strcpy(rname, name);
+ adjust_name(rname);
+ if (check_name(rname)) goto fake;
+ return rname;
+fake: if (i == 0)
+ strcpy(rname, "obj");
+ else
+ sprintf(rname, "r_%d", i);
+ return rname;
+}
+
+static char *col_name(struct csa *csa, int j, char cname[255+1])
+{ /* construct symbolic name of j-th column (variable) */
+ const char *name;
+ name = glp_get_col_name(csa->P, j);
+ if (name == NULL) goto fake;
+ strcpy(cname, name);
+ adjust_name(cname);
+ if (check_name(cname)) goto fake;
+ return cname;
+#if 0 /* 18/I-2018 */
+fake: sprintf(cname, "x_%d", j);
+#else
+fake: /* construct fake name depending on column's attributes */
+ { GLPCOL *col = csa->P->col[j];
+ if (col->type == GLP_FX)
+ { /* fixed column */
+ sprintf(cname, "s_%d", j);
+ }
+ else if (col->kind == GLP_CV)
+ { /* continuous variable */
+ sprintf(cname, "x_%d", j);
+ }
+ else if (!(col->lb == 0 && col->ub == 1))
+ { /* general (non-binary) integer variable */
+ sprintf(cname, "y_%d", j);
+ }
+ else
+ { /* binary variable */
+ sprintf(cname, "z_%d", j);
+ }
+ }
+#endif
+ return cname;
+}
+
+int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
+{ /* write problem data in CPLEX LP format */
+ glp_cpxcp _parm;
+ struct csa _csa, *csa = &_csa;
+ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, j, len, flag, count, ret;
+ char line[1000+1], term[500+1], name[255+1];
+ xprintf("Writing problem data to '%s'...\n", fname);
+ if (parm == NULL)
+ glp_init_cpxcp(&_parm), parm = &_parm;
+ /* check control parameters */
+ check_parm("glp_write_lp", parm);
+ /* initialize common storage area */
+ csa->P = P;
+ csa->parm = parm;
+ /* create output CPLEX LP file */
+ fp = glp_open(fname, "w"), count = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* write problem name */
+ xfprintf(fp, "\\* Problem: %s *\\\n",
+ P->name == NULL ? "Unknown" : P->name), count++;
+ xfprintf(fp, "\n"), count++;
+ /* the problem should contain at least one row and one column */
+ if (!(P->m > 0 && P->n > 0))
+ { xprintf("Warning: problem has no rows/columns\n");
+ xfprintf(fp, "\\* WARNING: PROBLEM HAS NO ROWS/COLUMNS *\\\n"),
+ count++;
+ xfprintf(fp, "\n"), count++;
+ goto skip;
+ }
+ /* write the objective function definition */
+ if (P->dir == GLP_MIN)
+ xfprintf(fp, "Minimize\n"), count++;
+ else if (P->dir == GLP_MAX)
+ xfprintf(fp, "Maximize\n"), count++;
+ else
+ xassert(P != P);
+ row_name(csa, 0, name);
+ sprintf(line, " %s:", name);
+ len = 0;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->coef != 0.0 || col->ptr == NULL)
+ { len++;
+ col_name(csa, j, name);
+ if (col->coef == 0.0)
+ sprintf(term, " + 0 %s", name); /* empty column */
+ else if (col->coef == +1.0)
+ sprintf(term, " + %s", name);
+ else if (col->coef == -1.0)
+ sprintf(term, " - %s", name);
+ else if (col->coef > 0.0)
+ sprintf(term, " + %.*g %s", DBL_DIG, +col->coef, name);
+ else
+ sprintf(term, " - %.*g %s", DBL_DIG, -col->coef, name);
+ if (strlen(line) + strlen(term) > 72)
+ xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
+ strcat(line, term);
+ }
+ }
+ if (len == 0)
+ { /* empty objective */
+ sprintf(term, " 0 %s", col_name(csa, 1, name));
+ strcat(line, term);
+ }
+ xfprintf(fp, "%s\n", line), count++;
+ if (P->c0 != 0.0)
+ xfprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, P->c0),
+ count++;
+ xfprintf(fp, "\n"), count++;
+ /* write the constraints section */
+ xfprintf(fp, "Subject To\n"), count++;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->type == GLP_FR) continue; /* skip free row */
+ row_name(csa, i, name);
+ sprintf(line, " %s:", name);
+ /* linear form */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col_name(csa, aij->col->j, name);
+ if (aij->val == +1.0)
+ sprintf(term, " + %s", name);
+ else if (aij->val == -1.0)
+ sprintf(term, " - %s", name);
+ else if (aij->val > 0.0)
+ sprintf(term, " + %.*g %s", DBL_DIG, +aij->val, name);
+ else
+ sprintf(term, " - %.*g %s", DBL_DIG, -aij->val, name);
+ if (strlen(line) + strlen(term) > 72)
+ xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
+ strcat(line, term);
+ }
+ if (row->type == GLP_DB)
+ { /* double-bounded (ranged) constraint */
+ sprintf(term, " - ~r_%d", i);
+ if (strlen(line) + strlen(term) > 72)
+ xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
+ strcat(line, term);
+ }
+ else if (row->ptr == NULL)
+ { /* empty constraint */
+ sprintf(term, " 0 %s", col_name(csa, 1, name));
+ strcat(line, term);
+ }
+ /* right hand-side */
+ if (row->type == GLP_LO)
+ sprintf(term, " >= %.*g", DBL_DIG, row->lb);
+ else if (row->type == GLP_UP)
+ sprintf(term, " <= %.*g", DBL_DIG, row->ub);
+ else if (row->type == GLP_DB || row->type == GLP_FX)
+ sprintf(term, " = %.*g", DBL_DIG, row->lb);
+ else
+ xassert(row != row);
+ if (strlen(line) + strlen(term) > 72)
+ xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
+ strcat(line, term);
+ xfprintf(fp, "%s\n", line), count++;
+ }
+ xfprintf(fp, "\n"), count++;
+ /* write the bounds section */
+ flag = 0;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->type != GLP_DB) continue;
+ if (!flag)
+ xfprintf(fp, "Bounds\n"), flag = 1, count++;
+ xfprintf(fp, " 0 <= ~r_%d <= %.*g\n",
+ i, DBL_DIG, row->ub - row->lb), count++;
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->type == GLP_LO && col->lb == 0.0) continue;
+ if (!flag)
+ xfprintf(fp, "Bounds\n"), flag = 1, count++;
+ col_name(csa, j, name);
+ if (col->type == GLP_FR)
+ xfprintf(fp, " %s free\n", name), count++;
+ else if (col->type == GLP_LO)
+ xfprintf(fp, " %s >= %.*g\n",
+ name, DBL_DIG, col->lb), count++;
+ else if (col->type == GLP_UP)
+ xfprintf(fp, " -Inf <= %s <= %.*g\n",
+ name, DBL_DIG, col->ub), count++;
+ else if (col->type == GLP_DB)
+ xfprintf(fp, " %.*g <= %s <= %.*g\n",
+ DBL_DIG, col->lb, name, DBL_DIG, col->ub), count++;
+ else if (col->type == GLP_FX)
+ xfprintf(fp, " %s = %.*g\n",
+ name, DBL_DIG, col->lb), count++;
+ else
+ xassert(col != col);
+ }
+ if (flag) xfprintf(fp, "\n"), count++;
+ /* write the integer section */
+ flag = 0;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->kind == GLP_CV) continue;
+ xassert(col->kind == GLP_IV);
+ if (!flag)
+ xfprintf(fp, "Generals\n"), flag = 1, count++;
+ xfprintf(fp, " %s\n", col_name(csa, j, name)), count++;
+ }
+ if (flag) xfprintf(fp, "\n"), count++;
+skip: /* write the end keyword */
+ xfprintf(fp, "End\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* problem data has been successfully written */
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/cpp.c b/test/monniaux/glpk-4.65/src/api/cpp.c
new file mode 100644
index 00000000..ac3d63ef
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/cpp.c
@@ -0,0 +1,185 @@
+/* cpp.c (solve critical path problem) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_cpp - solve critical path problem
+*
+* SYNOPSIS
+*
+* double glp_cpp(glp_graph *G, int v_t, int v_es, int v_ls);
+*
+* DESCRIPTION
+*
+* The routine glp_cpp solves the critical path problem represented in
+* the form of the project network.
+*
+* The parameter G is a pointer to the graph object, which specifies
+* the project network. This graph must be acyclic. Multiple arcs are
+* allowed being considered as single arcs.
+*
+* The parameter v_t specifies an offset of the field of type double
+* in the vertex data block, which contains time t[i] >= 0 needed to
+* perform corresponding job j. If v_t < 0, it is assumed that t[i] = 1
+* for all jobs.
+*
+* The parameter v_es specifies an offset of the field of type double
+* in the vertex data block, to which the routine stores earliest start
+* time for corresponding job. If v_es < 0, this time is not stored.
+*
+* The parameter v_ls specifies an offset of the field of type double
+* in the vertex data block, to which the routine stores latest start
+* time for corresponding job. If v_ls < 0, this time is not stored.
+*
+* RETURNS
+*
+* The routine glp_cpp returns the minimal project duration, that is,
+* minimal time needed to perform all jobs in the project. */
+
+static void sorting(glp_graph *G, int list[]);
+
+double glp_cpp(glp_graph *G, int v_t, int v_es, int v_ls)
+{ glp_vertex *v;
+ glp_arc *a;
+ int i, j, k, nv, *list;
+ double temp, total, *t, *es, *ls;
+ if (v_t >= 0 && v_t > G->v_size - (int)sizeof(double))
+ xerror("glp_cpp: v_t = %d; invalid offset\n", v_t);
+ if (v_es >= 0 && v_es > G->v_size - (int)sizeof(double))
+ xerror("glp_cpp: v_es = %d; invalid offset\n", v_es);
+ if (v_ls >= 0 && v_ls > G->v_size - (int)sizeof(double))
+ xerror("glp_cpp: v_ls = %d; invalid offset\n", v_ls);
+ nv = G->nv;
+ if (nv == 0)
+ { total = 0.0;
+ goto done;
+ }
+ /* allocate working arrays */
+ t = xcalloc(1+nv, sizeof(double));
+ es = xcalloc(1+nv, sizeof(double));
+ ls = xcalloc(1+nv, sizeof(double));
+ list = xcalloc(1+nv, sizeof(int));
+ /* retrieve job times */
+ for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ if (v_t >= 0)
+ { memcpy(&t[i], (char *)v->data + v_t, sizeof(double));
+ if (t[i] < 0.0)
+ xerror("glp_cpp: t[%d] = %g; invalid time\n", i, t[i]);
+ }
+ else
+ t[i] = 1.0;
+ }
+ /* perform topological sorting to determine the list of nodes
+ (jobs) such that if list[k] = i and list[kk] = j and there
+ exists arc (i->j), then k < kk */
+ sorting(G, list);
+ /* FORWARD PASS */
+ /* determine earliest start times */
+ for (k = 1; k <= nv; k++)
+ { j = list[k];
+ es[j] = 0.0;
+ for (a = G->v[j]->in; a != NULL; a = a->h_next)
+ { i = a->tail->i;
+ /* there exists arc (i->j) in the project network */
+ temp = es[i] + t[i];
+ if (es[j] < temp) es[j] = temp;
+ }
+ }
+ /* determine the minimal project duration */
+ total = 0.0;
+ for (i = 1; i <= nv; i++)
+ { temp = es[i] + t[i];
+ if (total < temp) total = temp;
+ }
+ /* BACKWARD PASS */
+ /* determine latest start times */
+ for (k = nv; k >= 1; k--)
+ { i = list[k];
+ ls[i] = total - t[i];
+ for (a = G->v[i]->out; a != NULL; a = a->t_next)
+ { j = a->head->i;
+ /* there exists arc (i->j) in the project network */
+ temp = ls[j] - t[i];
+ if (ls[i] > temp) ls[i] = temp;
+ }
+ /* avoid possible round-off errors */
+ if (ls[i] < es[i]) ls[i] = es[i];
+ }
+ /* store results, if necessary */
+ if (v_es >= 0)
+ { for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_es, &es[i], sizeof(double));
+ }
+ }
+ if (v_ls >= 0)
+ { for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_ls, &ls[i], sizeof(double));
+ }
+ }
+ /* free working arrays */
+ xfree(t);
+ xfree(es);
+ xfree(ls);
+ xfree(list);
+done: return total;
+}
+
+static void sorting(glp_graph *G, int list[])
+{ /* perform topological sorting to determine the list of nodes
+ (jobs) such that if list[k] = i and list[kk] = j and there
+ exists arc (i->j), then k < kk */
+ int i, k, nv, v_size, *num;
+ void **save;
+ nv = G->nv;
+ v_size = G->v_size;
+ save = xcalloc(1+nv, sizeof(void *));
+ num = xcalloc(1+nv, sizeof(int));
+ G->v_size = sizeof(int);
+ for (i = 1; i <= nv; i++)
+ { save[i] = G->v[i]->data;
+ G->v[i]->data = &num[i];
+ list[i] = 0;
+ }
+ if (glp_top_sort(G, 0) != 0)
+ xerror("glp_cpp: project network is not acyclic\n");
+ G->v_size = v_size;
+ for (i = 1; i <= nv; i++)
+ { G->v[i]->data = save[i];
+ k = num[i];
+ xassert(1 <= k && k <= nv);
+ xassert(list[k] == 0);
+ list[k] = i;
+ }
+ xfree(save);
+ xfree(num);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/cpxbas.c b/test/monniaux/glpk-4.65/src/api/cpxbas.c
new file mode 100644
index 00000000..e1c656a7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/cpxbas.c
@@ -0,0 +1,269 @@
+/* cpxbas.c (construct Bixby's initial LP basis) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+struct var
+{ /* structural variable */
+ int j;
+ /* ordinal number */
+ double q;
+ /* penalty value */
+};
+
+static int CDECL fcmp(const void *ptr1, const void *ptr2)
+{ /* this routine is passed to the qsort() function */
+ struct var *col1 = (void *)ptr1, *col2 = (void *)ptr2;
+ if (col1->q < col2->q) return -1;
+ if (col1->q > col2->q) return +1;
+ return 0;
+}
+
+static int get_column(glp_prob *lp, int j, int ind[], double val[])
+{ /* Bixby's algorithm assumes that the constraint matrix is scaled
+ such that the maximum absolute value in every non-zero row and
+ column is 1 */
+ int k, len;
+ double big;
+ len = glp_get_mat_col(lp, j, ind, val);
+ big = 0.0;
+ for (k = 1; k <= len; k++)
+ if (big < fabs(val[k])) big = fabs(val[k]);
+ if (big == 0.0) big = 1.0;
+ for (k = 1; k <= len; k++) val[k] /= big;
+ return len;
+}
+
+static void cpx_basis(glp_prob *lp)
+{ /* main routine */
+ struct var *C, *C2, *C3, *C4;
+ int m, n, i, j, jk, k, l, ll, t, n2, n3, n4, type, len, *I, *r,
+ *ind;
+ double alpha, gamma, cmax, temp, *v, *val;
+ xprintf("Constructing initial basis...\n");
+ /* determine the number of rows and columns */
+ m = glp_get_num_rows(lp);
+ n = glp_get_num_cols(lp);
+ /* allocate working arrays */
+ C = xcalloc(1+n, sizeof(struct var));
+ I = xcalloc(1+m, sizeof(int));
+ r = xcalloc(1+m, sizeof(int));
+ v = xcalloc(1+m, sizeof(double));
+ ind = xcalloc(1+m, sizeof(int));
+ val = xcalloc(1+m, sizeof(double));
+ /* make all auxiliary variables non-basic */
+ for (i = 1; i <= m; i++)
+ { if (glp_get_row_type(lp, i) != GLP_DB)
+ glp_set_row_stat(lp, i, GLP_NS);
+ else if (fabs(glp_get_row_lb(lp, i)) <=
+ fabs(glp_get_row_ub(lp, i)))
+ glp_set_row_stat(lp, i, GLP_NL);
+ else
+ glp_set_row_stat(lp, i, GLP_NU);
+ }
+ /* make all structural variables non-basic */
+ for (j = 1; j <= n; j++)
+ { if (glp_get_col_type(lp, j) != GLP_DB)
+ glp_set_col_stat(lp, j, GLP_NS);
+ else if (fabs(glp_get_col_lb(lp, j)) <=
+ fabs(glp_get_col_ub(lp, j)))
+ glp_set_col_stat(lp, j, GLP_NL);
+ else
+ glp_set_col_stat(lp, j, GLP_NU);
+ }
+ /* C2 is a set of free structural variables */
+ n2 = 0, C2 = C + 0;
+ for (j = 1; j <= n; j++)
+ { type = glp_get_col_type(lp, j);
+ if (type == GLP_FR)
+ { n2++;
+ C2[n2].j = j;
+ C2[n2].q = 0.0;
+ }
+ }
+ /* C3 is a set of structural variables having excatly one (lower
+ or upper) bound */
+ n3 = 0, C3 = C2 + n2;
+ for (j = 1; j <= n; j++)
+ { type = glp_get_col_type(lp, j);
+ if (type == GLP_LO)
+ { n3++;
+ C3[n3].j = j;
+ C3[n3].q = + glp_get_col_lb(lp, j);
+ }
+ else if (type == GLP_UP)
+ { n3++;
+ C3[n3].j = j;
+ C3[n3].q = - glp_get_col_ub(lp, j);
+ }
+ }
+ /* C4 is a set of structural variables having both (lower and
+ upper) bounds */
+ n4 = 0, C4 = C3 + n3;
+ for (j = 1; j <= n; j++)
+ { type = glp_get_col_type(lp, j);
+ if (type == GLP_DB)
+ { n4++;
+ C4[n4].j = j;
+ C4[n4].q = glp_get_col_lb(lp, j) - glp_get_col_ub(lp, j);
+ }
+ }
+ /* compute gamma = max{|c[j]|: 1 <= j <= n} */
+ gamma = 0.0;
+ for (j = 1; j <= n; j++)
+ { temp = fabs(glp_get_obj_coef(lp, j));
+ if (gamma < temp) gamma = temp;
+ }
+ /* compute cmax */
+ cmax = (gamma == 0.0 ? 1.0 : 1000.0 * gamma);
+ /* compute final penalty for all structural variables within sets
+ C2, C3, and C4 */
+ switch (glp_get_obj_dir(lp))
+ { case GLP_MIN: temp = +1.0; break;
+ case GLP_MAX: temp = -1.0; break;
+ default: xassert(lp != lp);
+ }
+ for (k = 1; k <= n2+n3+n4; k++)
+ { j = C[k].j;
+ C[k].q += (temp * glp_get_obj_coef(lp, j)) / cmax;
+ }
+ /* sort structural variables within C2, C3, and C4 in ascending
+ order of penalty value */
+ qsort(C2+1, n2, sizeof(struct var), fcmp);
+ for (k = 1; k < n2; k++) xassert(C2[k].q <= C2[k+1].q);
+ qsort(C3+1, n3, sizeof(struct var), fcmp);
+ for (k = 1; k < n3; k++) xassert(C3[k].q <= C3[k+1].q);
+ qsort(C4+1, n4, sizeof(struct var), fcmp);
+ for (k = 1; k < n4; k++) xassert(C4[k].q <= C4[k+1].q);
+ /*** STEP 1 ***/
+ for (i = 1; i <= m; i++)
+ { type = glp_get_row_type(lp, i);
+ if (type != GLP_FX)
+ { /* row i is either free or inequality constraint */
+ glp_set_row_stat(lp, i, GLP_BS);
+ I[i] = 1;
+ r[i] = 1;
+ }
+ else
+ { /* row i is equality constraint */
+ I[i] = 0;
+ r[i] = 0;
+ }
+ v[i] = +DBL_MAX;
+ }
+ /*** STEP 2 ***/
+ for (k = 1; k <= n2+n3+n4; k++)
+ { jk = C[k].j;
+ len = get_column(lp, jk, ind, val);
+ /* let alpha = max{|A[l,jk]|: r[l] = 0} and let l' be such
+ that alpha = |A[l',jk]| */
+ alpha = 0.0, ll = 0;
+ for (t = 1; t <= len; t++)
+ { l = ind[t];
+ if (r[l] == 0 && alpha < fabs(val[t]))
+ alpha = fabs(val[t]), ll = l;
+ }
+ if (alpha >= 0.99)
+ { /* B := B union {jk} */
+ glp_set_col_stat(lp, jk, GLP_BS);
+ I[ll] = 1;
+ v[ll] = alpha;
+ /* r[l] := r[l] + 1 for all l such that |A[l,jk]| != 0 */
+ for (t = 1; t <= len; t++)
+ { l = ind[t];
+ if (val[t] != 0.0) r[l]++;
+ }
+ /* continue to the next k */
+ continue;
+ }
+ /* if |A[l,jk]| > 0.01 * v[l] for some l, continue to the
+ next k */
+ for (t = 1; t <= len; t++)
+ { l = ind[t];
+ if (fabs(val[t]) > 0.01 * v[l]) break;
+ }
+ if (t <= len) continue;
+ /* otherwise, let alpha = max{|A[l,jk]|: I[l] = 0} and let l'
+ be such that alpha = |A[l',jk]| */
+ alpha = 0.0, ll = 0;
+ for (t = 1; t <= len; t++)
+ { l = ind[t];
+ if (I[l] == 0 && alpha < fabs(val[t]))
+ alpha = fabs(val[t]), ll = l;
+ }
+ /* if alpha = 0, continue to the next k */
+ if (alpha == 0.0) continue;
+ /* B := B union {jk} */
+ glp_set_col_stat(lp, jk, GLP_BS);
+ I[ll] = 1;
+ v[ll] = alpha;
+ /* r[l] := r[l] + 1 for all l such that |A[l,jk]| != 0 */
+ for (t = 1; t <= len; t++)
+ { l = ind[t];
+ if (val[t] != 0.0) r[l]++;
+ }
+ }
+ /*** STEP 3 ***/
+ /* add an artificial variable (auxiliary variable for equality
+ constraint) to cover each remaining uncovered row */
+ for (i = 1; i <= m; i++)
+ if (I[i] == 0) glp_set_row_stat(lp, i, GLP_BS);
+ /* free working arrays */
+ xfree(C);
+ xfree(I);
+ xfree(r);
+ xfree(v);
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_cpx_basis - construct Bixby's initial LP basis
+*
+* SYNOPSIS
+*
+* void glp_cpx_basis(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_cpx_basis constructs an advanced initial basis for
+* the specified problem object.
+*
+* The routine is based on Bixby's algorithm described in the paper:
+*
+* Robert E. Bixby. Implementing the Simplex Method: The Initial Basis.
+* ORSA Journal on Computing, Vol. 4, No. 3, 1992, pp. 267-84. */
+
+void glp_cpx_basis(glp_prob *lp)
+{ if (lp->m == 0 || lp->n == 0)
+ glp_std_basis(lp);
+ else
+ cpx_basis(lp);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/graph.c b/test/monniaux/glpk-4.65/src/api/graph.c
new file mode 100644
index 00000000..82994c84
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/graph.c
@@ -0,0 +1,504 @@
+/* graph.c (basic graph routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "avl.h"
+#include "dmp.h"
+#include "env.h"
+#include "glpk.h"
+
+/* CAUTION: DO NOT CHANGE THE LIMITS BELOW */
+
+#define NV_MAX 100000000 /* = 100*10^6 */
+/* maximal number of vertices in the graph */
+
+#define NA_MAX 500000000 /* = 500*10^6 */
+/* maximal number of arcs in the graph */
+
+/***********************************************************************
+* NAME
+*
+* glp_create_graph - create graph
+*
+* SYNOPSIS
+*
+* glp_graph *glp_create_graph(int v_size, int a_size);
+*
+* DESCRIPTION
+*
+* The routine creates a new graph, which initially is empty, i.e. has
+* no vertices and arcs.
+*
+* The parameter v_size specifies the size of data associated with each
+* vertex of the graph (0 to 256 bytes).
+*
+* The parameter a_size specifies the size of data associated with each
+* arc of the graph (0 to 256 bytes).
+*
+* RETURNS
+*
+* The routine returns a pointer to the graph created. */
+
+static void create_graph(glp_graph *G, int v_size, int a_size)
+{ G->pool = dmp_create_pool();
+ G->name = NULL;
+ G->nv_max = 50;
+ G->nv = G->na = 0;
+ G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
+ G->index = NULL;
+ G->v_size = v_size;
+ G->a_size = a_size;
+ return;
+}
+
+glp_graph *glp_create_graph(int v_size, int a_size)
+{ glp_graph *G;
+ if (!(0 <= v_size && v_size <= 256))
+ xerror("glp_create_graph: v_size = %d; invalid size of vertex "
+ "data\n", v_size);
+ if (!(0 <= a_size && a_size <= 256))
+ xerror("glp_create_graph: a_size = %d; invalid size of arc dat"
+ "a\n", a_size);
+ G = xmalloc(sizeof(glp_graph));
+ create_graph(G, v_size, a_size);
+ return G;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_graph_name - assign (change) graph name
+*
+* SYNOPSIS
+*
+* void glp_set_graph_name(glp_graph *G, const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_set_graph_name assigns a symbolic name specified by
+* the character string name (1 to 255 chars) to the graph.
+*
+* If the parameter name is NULL or an empty string, the routine erases
+* the existing symbolic name of the graph. */
+
+void glp_set_graph_name(glp_graph *G, const char *name)
+{ if (G->name != NULL)
+ { dmp_free_atom(G->pool, G->name, strlen(G->name)+1);
+ G->name = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int j;
+ for (j = 0; name[j] != '\0'; j++)
+ { if (j == 256)
+ xerror("glp_set_graph_name: graph name too long\n");
+ if (iscntrl((unsigned char)name[j]))
+ xerror("glp_set_graph_name: graph name contains invalid "
+ "character(s)\n");
+ }
+ G->name = dmp_get_atom(G->pool, strlen(name)+1);
+ strcpy(G->name, name);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_add_vertices - add new vertices to graph
+*
+* SYNOPSIS
+*
+* int glp_add_vertices(glp_graph *G, int nadd);
+*
+* DESCRIPTION
+*
+* The routine glp_add_vertices adds nadd vertices to the specified
+* graph. New vertices are always added to the end of the vertex list,
+* so ordinal numbers of existing vertices remain unchanged.
+*
+* Being added each new vertex is isolated (has no incident arcs).
+*
+* RETURNS
+*
+* The routine glp_add_vertices returns an ordinal number of the first
+* new vertex added to the graph. */
+
+int glp_add_vertices(glp_graph *G, int nadd)
+{ int i, nv_new;
+ if (nadd < 1)
+ xerror("glp_add_vertices: nadd = %d; invalid number of vertice"
+ "s\n", nadd);
+ if (nadd > NV_MAX - G->nv)
+ xerror("glp_add_vertices: nadd = %d; too many vertices\n",
+ nadd);
+ /* determine new number of vertices */
+ nv_new = G->nv + nadd;
+ /* increase the room, if necessary */
+ if (G->nv_max < nv_new)
+ { glp_vertex **save = G->v;
+ while (G->nv_max < nv_new)
+ { G->nv_max += G->nv_max;
+ xassert(G->nv_max > 0);
+ }
+ G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
+ memcpy(&G->v[1], &save[1], G->nv * sizeof(glp_vertex *));
+ xfree(save);
+ }
+ /* add new vertices to the end of the vertex list */
+ for (i = G->nv+1; i <= nv_new; i++)
+ { glp_vertex *v;
+ G->v[i] = v = dmp_get_atom(G->pool, sizeof(glp_vertex));
+ v->i = i;
+ v->name = NULL;
+ v->entry = NULL;
+ if (G->v_size == 0)
+ v->data = NULL;
+ else
+ { v->data = dmp_get_atom(G->pool, G->v_size);
+ memset(v->data, 0, G->v_size);
+ }
+ v->temp = NULL;
+ v->in = v->out = NULL;
+ }
+ /* set new number of vertices */
+ G->nv = nv_new;
+ /* return the ordinal number of the first vertex added */
+ return nv_new - nadd + 1;
+}
+
+/**********************************************************************/
+
+void glp_set_vertex_name(glp_graph *G, int i, const char *name)
+{ /* assign (change) vertex name */
+ glp_vertex *v;
+ if (!(1 <= i && i <= G->nv))
+ xerror("glp_set_vertex_name: i = %d; vertex number out of rang"
+ "e\n", i);
+ v = G->v[i];
+ if (v->name != NULL)
+ { if (v->entry != NULL)
+ { xassert(G->index != NULL);
+ avl_delete_node(G->index, v->entry);
+ v->entry = NULL;
+ }
+ dmp_free_atom(G->pool, v->name, strlen(v->name)+1);
+ v->name = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int k;
+ for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_set_vertex_name: i = %d; vertex name too lon"
+ "g\n", i);
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_set_vertex_name: i = %d; vertex name contain"
+ "s invalid character(s)\n", i);
+ }
+ v->name = dmp_get_atom(G->pool, strlen(name)+1);
+ strcpy(v->name, name);
+ if (G->index != NULL)
+ { xassert(v->entry == NULL);
+ v->entry = avl_insert_node(G->index, v->name);
+ avl_set_node_link(v->entry, v);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_add_arc - add new arc to graph
+*
+* SYNOPSIS
+*
+* glp_arc *glp_add_arc(glp_graph *G, int i, int j);
+*
+* DESCRIPTION
+*
+* The routine glp_add_arc adds a new arc to the specified graph.
+*
+* The parameters i and j specify the ordinal numbers of, resp., tail
+* and head vertices of the arc. Note that self-loops and multiple arcs
+* are allowed.
+*
+* RETURNS
+*
+* The routine glp_add_arc returns a pointer to the arc added. */
+
+glp_arc *glp_add_arc(glp_graph *G, int i, int j)
+{ glp_arc *a;
+ if (!(1 <= i && i <= G->nv))
+ xerror("glp_add_arc: i = %d; tail vertex number out of range\n"
+ , i);
+ if (!(1 <= j && j <= G->nv))
+ xerror("glp_add_arc: j = %d; head vertex number out of range\n"
+ , j);
+ if (G->na == NA_MAX)
+ xerror("glp_add_arc: too many arcs\n");
+ a = dmp_get_atom(G->pool, sizeof(glp_arc));
+ a->tail = G->v[i];
+ a->head = G->v[j];
+ if (G->a_size == 0)
+ a->data = NULL;
+ else
+ { a->data = dmp_get_atom(G->pool, G->a_size);
+ memset(a->data, 0, G->a_size);
+ }
+ a->temp = NULL;
+ a->t_prev = NULL;
+ a->t_next = G->v[i]->out;
+ if (a->t_next != NULL) a->t_next->t_prev = a;
+ a->h_prev = NULL;
+ a->h_next = G->v[j]->in;
+ if (a->h_next != NULL) a->h_next->h_prev = a;
+ G->v[i]->out = G->v[j]->in = a;
+ G->na++;
+ return a;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_del_vertices - delete vertices from graph
+*
+* SYNOPSIS
+*
+* void glp_del_vertices(glp_graph *G, int ndel, const int num[]);
+*
+* DESCRIPTION
+*
+* The routine glp_del_vertices deletes vertices along with all
+* incident arcs from the specified graph. Ordinal numbers of vertices
+* to be deleted should be placed in locations num[1], ..., num[ndel],
+* ndel > 0.
+*
+* Note that deleting vertices involves changing ordinal numbers of
+* other vertices remaining in the graph. New ordinal numbers of the
+* remaining vertices are assigned under the assumption that the
+* original order of vertices is not changed. */
+
+void glp_del_vertices(glp_graph *G, int ndel, const int num[])
+{ glp_vertex *v;
+ int i, k, nv_new;
+ /* scan the list of vertices to be deleted */
+ if (!(1 <= ndel && ndel <= G->nv))
+ xerror("glp_del_vertices: ndel = %d; invalid number of vertice"
+ "s\n", ndel);
+ for (k = 1; k <= ndel; k++)
+ { /* take the number of vertex to be deleted */
+ i = num[k];
+ /* obtain pointer to i-th vertex */
+ if (!(1 <= i && i <= G->nv))
+ xerror("glp_del_vertices: num[%d] = %d; vertex number out o"
+ "f range\n", k, i);
+ v = G->v[i];
+ /* check that the vertex is not marked yet */
+ if (v->i == 0)
+ xerror("glp_del_vertices: num[%d] = %d; duplicate vertex nu"
+ "mbers not allowed\n", k, i);
+ /* erase symbolic name assigned to the vertex */
+ glp_set_vertex_name(G, i, NULL);
+ xassert(v->name == NULL);
+ xassert(v->entry == NULL);
+ /* free vertex data, if allocated */
+ if (v->data != NULL)
+ dmp_free_atom(G->pool, v->data, G->v_size);
+ /* delete all incoming arcs */
+ while (v->in != NULL)
+ glp_del_arc(G, v->in);
+ /* delete all outgoing arcs */
+ while (v->out != NULL)
+ glp_del_arc(G, v->out);
+ /* mark the vertex to be deleted */
+ v->i = 0;
+ }
+ /* delete all marked vertices from the vertex list */
+ nv_new = 0;
+ for (i = 1; i <= G->nv; i++)
+ { /* obtain pointer to i-th vertex */
+ v = G->v[i];
+ /* check if the vertex is marked */
+ if (v->i == 0)
+ { /* it is marked, delete it */
+ dmp_free_atom(G->pool, v, sizeof(glp_vertex));
+ }
+ else
+ { /* it is not marked, keep it */
+ v->i = ++nv_new;
+ G->v[v->i] = v;
+ }
+ }
+ /* set new number of vertices in the graph */
+ G->nv = nv_new;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_del_arc - delete arc from graph
+*
+* SYNOPSIS
+*
+* void glp_del_arc(glp_graph *G, glp_arc *a);
+*
+* DESCRIPTION
+*
+* The routine glp_del_arc deletes an arc from the specified graph.
+* The arc to be deleted must exist. */
+
+void glp_del_arc(glp_graph *G, glp_arc *a)
+{ /* some sanity checks */
+ xassert(G->na > 0);
+ xassert(1 <= a->tail->i && a->tail->i <= G->nv);
+ xassert(a->tail == G->v[a->tail->i]);
+ xassert(1 <= a->head->i && a->head->i <= G->nv);
+ xassert(a->head == G->v[a->head->i]);
+ /* remove the arc from the list of incoming arcs */
+ if (a->h_prev == NULL)
+ a->head->in = a->h_next;
+ else
+ a->h_prev->h_next = a->h_next;
+ if (a->h_next == NULL)
+ ;
+ else
+ a->h_next->h_prev = a->h_prev;
+ /* remove the arc from the list of outgoing arcs */
+ if (a->t_prev == NULL)
+ a->tail->out = a->t_next;
+ else
+ a->t_prev->t_next = a->t_next;
+ if (a->t_next == NULL)
+ ;
+ else
+ a->t_next->t_prev = a->t_prev;
+ /* free arc data, if allocated */
+ if (a->data != NULL)
+ dmp_free_atom(G->pool, a->data, G->a_size);
+ /* delete the arc from the graph */
+ dmp_free_atom(G->pool, a, sizeof(glp_arc));
+ G->na--;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_erase_graph - erase graph content
+*
+* SYNOPSIS
+*
+* void glp_erase_graph(glp_graph *G, int v_size, int a_size);
+*
+* DESCRIPTION
+*
+* The routine glp_erase_graph erases the content of the specified
+* graph. The effect of this operation is the same as if the graph
+* would be deleted with the routine glp_delete_graph and then created
+* anew with the routine glp_create_graph, with exception that the
+* handle (pointer) to the graph remains valid. */
+
+static void delete_graph(glp_graph *G)
+{ dmp_delete_pool(G->pool);
+ xfree(G->v);
+ if (G->index != NULL) avl_delete_tree(G->index);
+ return;
+}
+
+void glp_erase_graph(glp_graph *G, int v_size, int a_size)
+{ if (!(0 <= v_size && v_size <= 256))
+ xerror("glp_erase_graph: v_size = %d; invalid size of vertex d"
+ "ata\n", v_size);
+ if (!(0 <= a_size && a_size <= 256))
+ xerror("glp_erase_graph: a_size = %d; invalid size of arc data"
+ "\n", a_size);
+ delete_graph(G);
+ create_graph(G, v_size, a_size);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_delete_graph - delete graph
+*
+* SYNOPSIS
+*
+* void glp_delete_graph(glp_graph *G);
+*
+* DESCRIPTION
+*
+* The routine glp_delete_graph deletes the specified graph and frees
+* all the memory allocated to this program object. */
+
+void glp_delete_graph(glp_graph *G)
+{ delete_graph(G);
+ xfree(G);
+ return;
+}
+
+/**********************************************************************/
+
+void glp_create_v_index(glp_graph *G)
+{ /* create vertex name index */
+ glp_vertex *v;
+ int i;
+ if (G->index == NULL)
+ { G->index = avl_create_tree(avl_strcmp, NULL);
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ xassert(v->entry == NULL);
+ if (v->name != NULL)
+ { v->entry = avl_insert_node(G->index, v->name);
+ avl_set_node_link(v->entry, v);
+ }
+ }
+ }
+ return;
+}
+
+int glp_find_vertex(glp_graph *G, const char *name)
+{ /* find vertex by its name */
+ AVLNODE *node;
+ int i = 0;
+ if (G->index == NULL)
+ xerror("glp_find_vertex: vertex name index does not exist\n");
+ if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
+ { node = avl_find_node(G->index, name);
+ if (node != NULL)
+ i = ((glp_vertex *)avl_get_node_link(node))->i;
+ }
+ return i;
+}
+
+void glp_delete_v_index(glp_graph *G)
+{ /* delete vertex name index */
+ int i;
+ if (G->index != NULL)
+ { avl_delete_tree(G->index), G->index = NULL;
+ for (i = 1; i <= G->nv; i++) G->v[i]->entry = NULL;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/gridgen.c b/test/monniaux/glpk-4.65/src/api/gridgen.c
new file mode 100644
index 00000000..8cd3517f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/gridgen.c
@@ -0,0 +1,769 @@
+/* gridgen.c (grid-like network problem generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is a modified version of the program GRIDGEN, a grid-like
+* network problem generator developed by Yusin Lee and Jim Orlin.
+* The original code is publically available on the DIMACS ftp site at:
+* <ftp://dimacs.rutgers.edu/pub/netflow/generators/network/gridgen>.
+*
+* All changes concern only the program interface, so this modified
+* version produces exactly the same instances as the original version.
+*
+* Changes were made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_gridgen - grid-like network problem generator
+*
+* SYNOPSIS
+*
+* int glp_gridgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
+* const int parm[1+14]);
+*
+* DESCRIPTION
+*
+* The routine glp_gridgen is a grid-like network problem generator
+* developed by Yusin Lee and Jim Orlin.
+*
+* The parameter G specifies the graph object, to which the generated
+* problem data have to be stored. Note that on entry the graph object
+* is erased with the routine glp_erase_graph.
+*
+* The parameter v_rhs specifies an offset of the field of type double
+* in the vertex data block, to which the routine stores the supply or
+* demand value. If v_rhs < 0, the value is not stored.
+*
+* The parameter a_cap specifies an offset of the field of type double
+* in the arc data block, to which the routine stores the arc capacity.
+* If a_cap < 0, the capacity is not stored.
+*
+* The parameter a_cost specifies an offset of the field of type double
+* in the arc data block, to which the routine stores the per-unit cost
+* if the arc flow. If a_cost < 0, the cost is not stored.
+*
+* The array parm contains description of the network to be generated:
+*
+* parm[0] not used
+* parm[1] two-ways arcs indicator:
+* 1 - if links in both direction should be generated
+* 0 - otherwise
+* parm[2] random number seed (a positive integer)
+* parm[3] number of nodes (the number of nodes generated might be
+* slightly different to make the network a grid)
+* parm[4] grid width
+* parm[5] number of sources
+* parm[6] number of sinks
+* parm[7] average degree
+* parm[8] total flow
+* parm[9] distribution of arc costs:
+* 1 - uniform
+* 2 - exponential
+* parm[10] lower bound for arc cost (uniform)
+* 100 * lambda (exponential)
+* parm[11] upper bound for arc cost (uniform)
+* not used (exponential)
+* parm[12] distribution of arc capacities:
+* 1 - uniform
+* 2 - exponential
+* parm[13] lower bound for arc capacity (uniform)
+* 100 * lambda (exponential)
+* parm[14] upper bound for arc capacity (uniform)
+* not used (exponential)
+*
+* RETURNS
+*
+* If the instance was successfully generated, the routine glp_gridgen
+* returns zero; otherwise, if specified parameters are inconsistent,
+* the routine returns a non-zero error code.
+*
+* COMMENTS
+*
+* This network generator generates a grid-like network plus a super
+* node. In additional to the arcs connecting the nodes in the grid,
+* there is an arc from each supply node to the super node and from the
+* super node to each demand node to guarantee feasiblity. These arcs
+* have very high costs and very big capacities.
+*
+* The idea of this network generator is as follows: First, a grid of
+* n1 * n2 is generated. For example, 5 * 3. The nodes are numbered as
+* 1 to 15, and the supernode is numbered as n1*n2+1. Then arcs between
+* adjacent nodes are generated. For these arcs, the user is allowed to
+* specify either to generate two-way arcs or one-way arcs. If two-way
+* arcs are to be generated, two arcs, one in each direction, will be
+* generated between each adjacent node pairs. Otherwise, only one arc
+* will be generated. If this is the case, the arcs will be generated
+* in alterntive directions as shown below.
+*
+* 1 ---> 2 ---> 3 ---> 4 ---> 5
+* | ^ | ^ |
+* | | | | |
+* V | V | V
+* 6 <--- 7 <--- 8 <--- 9 <--- 10
+* | ^ | ^ |
+* | | | | |
+* V | V | V
+* 11 --->12 --->13 --->14 ---> 15
+*
+* Then the arcs between the super node and the source/sink nodes are
+* added as mentioned before. If the number of arcs still doesn't reach
+* the requirement, additional arcs will be added by uniformly picking
+* random node pairs. There is no checking to prevent multiple arcs
+* between any pair of nodes. However, there will be no self-arcs (arcs
+* that poins back to its tail node) in the network.
+*
+* The source and sink nodes are selected uniformly in the network, and
+* the imbalances of each source/sink node are also assigned by uniform
+* distribution. */
+
+struct stat_para
+{ /* structure for statistical distributions */
+ int distribution;
+ /* the distribution: */
+#define UNIFORM 1 /* uniform distribution */
+#define EXPONENTIAL 2 /* exponential distribution */
+ double parameter[5];
+ /* the parameters of the distribution */
+};
+
+struct arcs
+{ int from;
+ /* the FROM node of that arc */
+ int to;
+ /* the TO node of that arc */
+ int cost;
+ /* original cost of that arc */
+ int u;
+ /* capacity of the arc */
+};
+
+struct imbalance
+{ int node;
+ /* Node ID */
+ int supply;
+ /* Supply of that node */
+};
+
+struct csa
+{ /* common storage area */
+ glp_graph *G;
+ int v_rhs, a_cap, a_cost;
+ int seed;
+ /* random number seed */
+ int seed_original;
+ /* the original seed from input */
+ int two_way;
+ /* 0: generate arcs in both direction for the basic grid, except
+ for the arcs to/from the super node. 1: o/w */
+ int n_node;
+ /* total number of nodes in the network, numbered 1 to n_node,
+ including the super node, which is the last one */
+ int n_arc;
+ /* total number of arcs in the network, counting EVERY arc. */
+ int n_grid_arc;
+ /* number of arcs in the basic grid, including the arcs to/from
+ the super node */
+ int n_source, n_sink;
+ /* number of source and sink nodes */
+ int avg_degree;
+ /* average degree, arcs to and from the super node are counted */
+ int t_supply;
+ /* total supply in the network */
+ int n1, n2;
+ /* the two edges of the network grid. n1 >= n2 */
+ struct imbalance *source_list, *sink_list;
+ /* head of the array of source/sink nodes */
+ struct stat_para arc_costs;
+ /* the distribution of arc costs */
+ struct stat_para capacities;
+ /* distribution of the capacities of the arcs */
+ struct arcs *arc_list;
+ /* head of the arc list array. Arcs in this array are in the
+ order of grid_arcs, arcs to/from super node, and other arcs */
+};
+
+#define G (csa->G)
+#define v_rhs (csa->v_rhs)
+#define a_cap (csa->a_cap)
+#define a_cost (csa->a_cost)
+#define seed (csa->seed)
+#define seed_original (csa->seed_original)
+#define two_way (csa->two_way)
+#define n_node (csa->n_node)
+#define n_arc (csa->n_arc)
+#define n_grid_arc (csa->n_grid_arc)
+#define n_source (csa->n_source)
+#define n_sink (csa->n_sink)
+#define avg_degree (csa->avg_degree)
+#define t_supply (csa->t_supply)
+#define n1 (csa->n1)
+#define n2 (csa->n2)
+#define source_list (csa->source_list)
+#define sink_list (csa->sink_list)
+#define arc_costs (csa->arc_costs)
+#define capacities (csa->capacities)
+#define arc_list (csa->arc_list)
+
+static void assign_capacities(struct csa *csa);
+static void assign_costs(struct csa *csa);
+static void assign_imbalance(struct csa *csa);
+static int exponential(struct csa *csa, double lambda[1]);
+static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs
+ *arc_ptr);
+static struct arcs *gen_basic_grid(struct csa *csa, struct arcs
+ *arc_ptr);
+static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr);
+static void generate(struct csa *csa);
+static void output(struct csa *csa);
+static double randy(struct csa *csa);
+static void select_source_sinks(struct csa *csa);
+static int uniform(struct csa *csa, double a[2]);
+
+int glp_gridgen(glp_graph *G_, int _v_rhs, int _a_cap, int _a_cost,
+ const int parm[1+14])
+{ struct csa _csa, *csa = &_csa;
+ int n, ret;
+ G = G_;
+ v_rhs = _v_rhs;
+ a_cap = _a_cap;
+ a_cost = _a_cost;
+ if (G != NULL)
+ { if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_gridgen: v_rhs = %d; invalid offset\n", v_rhs);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_gridgen: a_cap = %d; invalid offset\n", a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_gridgen: a_cost = %d; invalid offset\n", a_cost)
+ ;
+ }
+ /* Check the parameters for consistency. */
+ if (!(parm[1] == 0 || parm[1] == 1))
+ { ret = 1;
+ goto done;
+ }
+ if (parm[2] < 1)
+ { ret = 2;
+ goto done;
+ }
+ if (!(10 <= parm[3] && parm[3] <= 40000))
+ { ret = 3;
+ goto done;
+ }
+ if (!(1 <= parm[4] && parm[4] <= 40000))
+ { ret = 4;
+ goto done;
+ }
+ if (!(parm[5] >= 0 && parm[6] >= 0 && parm[5] + parm[6] <=
+ parm[3]))
+ { ret = 5;
+ goto done;
+ }
+ if (!(1 <= parm[7] && parm[7] <= parm[3]))
+ { ret = 6;
+ goto done;
+ }
+ if (parm[8] < 0)
+ { ret = 7;
+ goto done;
+ }
+ if (!(parm[9] == 1 || parm[9] == 2))
+ { ret = 8;
+ goto done;
+ }
+ if (parm[9] == 1 && parm[10] > parm[11] ||
+ parm[9] == 2 && parm[10] < 1)
+ { ret = 9;
+ goto done;
+ }
+ if (!(parm[12] == 1 || parm[12] == 2))
+ { ret = 10;
+ goto done;
+ }
+ if (parm[12] == 1 && !(0 <= parm[13] && parm[13] <= parm[14]) ||
+ parm[12] == 2 && parm[13] < 1)
+ { ret = 11;
+ goto done;
+ }
+ /* Initialize the graph object. */
+ if (G != NULL)
+ { glp_erase_graph(G, G->v_size, G->a_size);
+ glp_set_graph_name(G, "GRIDGEN");
+ }
+ /* Copy the generator parameters. */
+ two_way = parm[1];
+ seed_original = seed = parm[2];
+ n_node = parm[3];
+ n = parm[4];
+ n_source = parm[5];
+ n_sink = parm[6];
+ avg_degree = parm[7];
+ t_supply = parm[8];
+ arc_costs.distribution = parm[9];
+ if (parm[9] == 1)
+ { arc_costs.parameter[0] = parm[10];
+ arc_costs.parameter[1] = parm[11];
+ }
+ else
+ { arc_costs.parameter[0] = (double)parm[10] / 100.0;
+ arc_costs.parameter[1] = 0.0;
+ }
+ capacities.distribution = parm[12];
+ if (parm[12] == 1)
+ { capacities.parameter[0] = parm[13];
+ capacities.parameter[1] = parm[14];
+ }
+ else
+ { capacities.parameter[0] = (double)parm[13] / 100.0;
+ capacities.parameter[1] = 0.0;
+ }
+ /* Calculate the edge lengths of the grid according to the
+ input. */
+ if (n * n >= n_node)
+ { n1 = n;
+ n2 = (int)((double)n_node / (double)n + 0.5);
+ }
+ else
+ { n2 = n;
+ n1 = (int)((double)n_node / (double)n + 0.5);
+ }
+ /* Recalculate the total number of nodes and plus 1 for the super
+ node. */
+ n_node = n1 * n2 + 1;
+ n_arc = n_node * avg_degree;
+ n_grid_arc = (two_way + 1) * ((n1 - 1) * n2 + (n2 - 1) * n1) +
+ n_source + n_sink;
+ if (n_grid_arc > n_arc) n_arc = n_grid_arc;
+ arc_list = xcalloc(n_arc, sizeof(struct arcs));
+ source_list = xcalloc(n_source, sizeof(struct imbalance));
+ sink_list = xcalloc(n_sink, sizeof(struct imbalance));
+ /* Generate a random network. */
+ generate(csa);
+ /* Output the network. */
+ output(csa);
+ /* Free all allocated memory. */
+ xfree(arc_list);
+ xfree(source_list);
+ xfree(sink_list);
+ /* The instance has been successfully generated. */
+ ret = 0;
+done: return ret;
+}
+
+#undef random
+
+static void assign_capacities(struct csa *csa)
+{ /* Assign a capacity to each arc. */
+ struct arcs *arc_ptr = arc_list;
+ int (*random)(struct csa *csa, double *);
+ int i;
+ /* Determine the random number generator to use. */
+ switch (arc_costs.distribution)
+ { case UNIFORM:
+ random = uniform;
+ break;
+ case EXPONENTIAL:
+ random = exponential;
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ /* Assign capacities to grid arcs. */
+ for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++)
+ arc_ptr->u = random(csa, capacities.parameter);
+ i = i - n_source - n_sink;
+ /* Assign capacities to arcs to/from supernode. */
+ for (; i < n_grid_arc; i++, arc_ptr++)
+ arc_ptr->u = t_supply;
+ /* Assign capacities to all other arcs. */
+ for (; i < n_arc; i++, arc_ptr++)
+ arc_ptr->u = random(csa, capacities.parameter);
+ return;
+}
+
+static void assign_costs(struct csa *csa)
+{ /* Assign a cost to each arc. */
+ struct arcs *arc_ptr = arc_list;
+ int (*random)(struct csa *csa, double *);
+ int i;
+ /* A high cost assigned to arcs to/from the supernode. */
+ int high_cost;
+ /* The maximum cost assigned to arcs in the base grid. */
+ int max_cost = 0;
+ /* Determine the random number generator to use. */
+ switch (arc_costs.distribution)
+ { case UNIFORM:
+ random = uniform;
+ break;
+ case EXPONENTIAL:
+ random = exponential;
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ /* Assign costs to arcs in the base grid. */
+ for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++)
+ { arc_ptr->cost = random(csa, arc_costs.parameter);
+ if (max_cost < arc_ptr->cost) max_cost = arc_ptr->cost;
+ }
+ i = i - n_source - n_sink;
+ /* Assign costs to arcs to/from the super node. */
+ high_cost = max_cost * 2;
+ for (; i < n_grid_arc; i++, arc_ptr++)
+ arc_ptr->cost = high_cost;
+ /* Assign costs to all other arcs. */
+ for (; i < n_arc; i++, arc_ptr++)
+ arc_ptr->cost = random(csa, arc_costs.parameter);
+ return;
+}
+
+static void assign_imbalance(struct csa *csa)
+{ /* Assign an imbalance to each node. */
+ int total, i;
+ double avg;
+ struct imbalance *ptr;
+ /* assign the supply nodes */
+ avg = 2.0 * t_supply / n_source;
+ do
+ { for (i = 1, total = t_supply, ptr = source_list + 1;
+ i < n_source; i++, ptr++)
+ { ptr->supply = (int)(randy(csa) * avg + 0.5);
+ total -= ptr->supply;
+ }
+ source_list->supply = total;
+ }
+ /* redo all if the assignment "overshooted" */
+ while (total <= 0);
+ /* assign the demand nodes */
+ avg = -2.0 * t_supply / n_sink;
+ do
+ { for (i = 1, total = t_supply, ptr = sink_list + 1;
+ i < n_sink; i++, ptr++)
+ { ptr->supply = (int)(randy(csa) * avg - 0.5);
+ total += ptr->supply;
+ }
+ sink_list->supply = - total;
+ }
+ while (total <= 0);
+ return;
+}
+
+static int exponential(struct csa *csa, double lambda[1])
+{ /* Returns an "exponentially distributed" integer with parameter
+ lambda. */
+ return ((int)(- lambda[0] * log((double)randy(csa)) + 0.5));
+}
+
+static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs
+ *arc_ptr)
+{ /* Generate an arc from each source to the supernode and from
+ supernode to each sink. */
+ int i;
+ for (i = 0; i < n_source; i++, arc_ptr++)
+ { arc_ptr->from = source_list[i].node;
+ arc_ptr->to = n_node;
+ }
+ for (i = 0; i < n_sink; i++, arc_ptr++)
+ { arc_ptr->to = sink_list[i].node;
+ arc_ptr->from = n_node;
+ }
+ return arc_ptr;
+}
+
+static struct arcs *gen_basic_grid(struct csa *csa, struct arcs
+ *arc_ptr)
+{ /* Generate the basic grid. */
+ int direction = 1, i, j, k;
+ if (two_way)
+ { /* Generate an arc in each direction. */
+ for (i = 1; i < n_node; i += n1)
+ { for (j = i, k = j + n1 - 1; j < k; j++)
+ { arc_ptr->from = j;
+ arc_ptr->to = j + 1;
+ arc_ptr++;
+ arc_ptr->from = j + 1;
+ arc_ptr->to = j;
+ arc_ptr++;
+ }
+ }
+ for (i = 1; i <= n1; i++)
+ { for (j = i + n1; j < n_node; j += n1)
+ { arc_ptr->from = j;
+ arc_ptr->to = j - n1;
+ arc_ptr++;
+ arc_ptr->from = j - n1;
+ arc_ptr->to = j;
+ arc_ptr++;
+ }
+ }
+ }
+ else
+ { /* Generate one arc in each direction. */
+ for (i = 1; i < n_node; i += n1)
+ { if (direction == 1)
+ j = i;
+ else
+ j = i + 1;
+ for (k = j + n1 - 1; j < k; j++)
+ { arc_ptr->from = j;
+ arc_ptr->to = j + direction;
+ arc_ptr++;
+ }
+ direction = - direction;
+ }
+ for (i = 1; i <= n1; i++)
+ { j = i + n1;
+ if (direction == 1)
+ { for (; j < n_node; j += n1)
+ { arc_ptr->from = j - n1;
+ arc_ptr->to = j;
+ arc_ptr++;
+ }
+ }
+ else
+ { for (; j < n_node; j += n1)
+ { arc_ptr->from = j - n1;
+ arc_ptr->to = j;
+ arc_ptr++;
+ }
+ }
+ direction = - direction;
+ }
+ }
+ return arc_ptr;
+}
+
+static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr)
+{ /* Generate random arcs to meet the specified density. */
+ int i;
+ double ab[2];
+ ab[0] = 0.9;
+ ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the
+ supernode cannot be selected */
+ for (i = n_grid_arc; i < n_arc; i++, arc_ptr++)
+ { arc_ptr->from = uniform(csa, ab);
+ arc_ptr->to = uniform(csa, ab);
+ if (arc_ptr->from == arc_ptr->to)
+ { arc_ptr--;
+ i--;
+ }
+ }
+ return;
+}
+
+static void generate(struct csa *csa)
+{ /* Generate a random network. */
+ struct arcs *arc_ptr = arc_list;
+ arc_ptr = gen_basic_grid(csa, arc_ptr);
+ select_source_sinks(csa);
+ arc_ptr = gen_additional_arcs(csa, arc_ptr);
+ gen_more_arcs(csa, arc_ptr);
+ assign_costs(csa);
+ assign_capacities(csa);
+ assign_imbalance(csa);
+ return;
+}
+
+static void output(struct csa *csa)
+{ /* Output the network in DIMACS format. */
+ struct arcs *arc_ptr;
+ struct imbalance *imb_ptr;
+ int i;
+ if (G != NULL) goto skip;
+ /* Output "c", "p" records. */
+ xprintf("c generated by GRIDGEN\n");
+ xprintf("c seed %d\n", seed_original);
+ xprintf("c nodes %d\n", n_node);
+ xprintf("c grid size %d X %d\n", n1, n2);
+ xprintf("c sources %d sinks %d\n", n_source, n_sink);
+ xprintf("c avg. degree %d\n", avg_degree);
+ xprintf("c supply %d\n", t_supply);
+ switch (arc_costs.distribution)
+ { case UNIFORM:
+ xprintf("c arc costs: UNIFORM distr. min %d max %d\n",
+ (int)arc_costs.parameter[0],
+ (int)arc_costs.parameter[1]);
+ break;
+ case EXPONENTIAL:
+ xprintf("c arc costs: EXPONENTIAL distr. lambda %d\n",
+ (int)arc_costs.parameter[0]);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ switch (capacities.distribution)
+ { case UNIFORM:
+ xprintf("c arc caps : UNIFORM distr. min %d max %d\n",
+ (int)capacities.parameter[0],
+ (int)capacities.parameter[1]);
+ break;
+ case EXPONENTIAL:
+ xprintf("c arc caps : EXPONENTIAL distr. %d lambda %d\n",
+ (int)capacities.parameter[0]);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+skip: if (G == NULL)
+ xprintf("p min %d %d\n", n_node, n_arc);
+ else
+ { glp_add_vertices(G, n_node);
+ if (v_rhs >= 0)
+ { double zero = 0.0;
+ for (i = 1; i <= n_node; i++)
+ { glp_vertex *v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &zero, sizeof(double));
+ }
+ }
+ }
+ /* Output "n node supply". */
+ for (i = 0, imb_ptr = source_list; i < n_source; i++, imb_ptr++)
+ { if (G == NULL)
+ xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply);
+ else
+ { if (v_rhs >= 0)
+ { double temp = (double)imb_ptr->supply;
+ glp_vertex *v = G->v[imb_ptr->node];
+ memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
+ }
+ }
+ }
+ for (i = 0, imb_ptr = sink_list; i < n_sink; i++, imb_ptr++)
+ { if (G == NULL)
+ xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply);
+ else
+ { if (v_rhs >= 0)
+ { double temp = (double)imb_ptr->supply;
+ glp_vertex *v = G->v[imb_ptr->node];
+ memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
+ }
+ }
+ }
+ /* Output "a from to lowcap=0 hicap cost". */
+ for (i = 0, arc_ptr = arc_list; i < n_arc; i++, arc_ptr++)
+ { if (G == NULL)
+ xprintf("a %d %d 0 %d %d\n", arc_ptr->from, arc_ptr->to,
+ arc_ptr->u, arc_ptr->cost);
+ else
+ { glp_arc *a = glp_add_arc(G, arc_ptr->from, arc_ptr->to);
+ if (a_cap >= 0)
+ { double temp = (double)arc_ptr->u;
+ memcpy((char *)a->data + a_cap, &temp, sizeof(double));
+ }
+ if (a_cost >= 0)
+ { double temp = (double)arc_ptr->cost;
+ memcpy((char *)a->data + a_cost, &temp, sizeof(double));
+ }
+ }
+ }
+ return;
+}
+
+static double randy(struct csa *csa)
+{ /* Returns a random number between 0.0 and 1.0.
+ See Ward Cheney & David Kincaid, "Numerical Mathematics and
+ Computing," 2Ed, pp. 335. */
+ seed = 16807 * seed % 2147483647;
+ if (seed < 0) seed = - seed;
+ return seed * 4.6566128752459e-10;
+}
+
+static void select_source_sinks(struct csa *csa)
+{ /* Randomly select the source nodes and sink nodes. */
+ int i, *int_ptr;
+ int *temp_list; /* a temporary list of nodes */
+ struct imbalance *ptr;
+ double ab[2]; /* parameter for random number generator */
+ ab[0] = 0.9;
+ ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the
+ supernode cannot be selected */
+ temp_list = xcalloc(n_node, sizeof(int));
+ for (i = 0, int_ptr = temp_list; i < n_node; i++, int_ptr++)
+ *int_ptr = 0;
+ /* Select the source nodes. */
+ for (i = 0, ptr = source_list; i < n_source; i++, ptr++)
+ { ptr->node = uniform(csa, ab);
+ if (temp_list[ptr->node] == 1) /* check for duplicates */
+ { ptr--;
+ i--;
+ }
+ else
+ temp_list[ptr->node] = 1;
+ }
+ /* Select the sink nodes. */
+ for (i = 0, ptr = sink_list; i < n_sink; i++, ptr++)
+ { ptr->node = uniform(csa, ab);
+ if (temp_list[ptr->node] == 1)
+ { ptr--;
+ i--;
+ }
+ else
+ temp_list[ptr->node] = 1;
+ }
+ xfree(temp_list);
+ return;
+}
+
+int uniform(struct csa *csa, double a[2])
+{ /* Generates an integer uniformly selected from [a[0],a[1]]. */
+ return (int)((a[1] - a[0]) * randy(csa) + a[0] + 0.5);
+}
+
+/**********************************************************************/
+
+#if 0
+int main(void)
+{ int parm[1+14];
+ double temp;
+ scanf("%d", &parm[1]);
+ scanf("%d", &parm[2]);
+ scanf("%d", &parm[3]);
+ scanf("%d", &parm[4]);
+ scanf("%d", &parm[5]);
+ scanf("%d", &parm[6]);
+ scanf("%d", &parm[7]);
+ scanf("%d", &parm[8]);
+ scanf("%d", &parm[9]);
+ if (parm[9] == 1)
+ { scanf("%d", &parm[10]);
+ scanf("%d", &parm[11]);
+ }
+ else
+ { scanf("%le", &temp);
+ parm[10] = (int)(100.0 * temp + .5);
+ parm[11] = 0;
+ }
+ scanf("%d", &parm[12]);
+ if (parm[12] == 1)
+ { scanf("%d", &parm[13]);
+ scanf("%d", &parm[14]);
+ }
+ else
+ { scanf("%le", &temp);
+ parm[13] = (int)(100.0 * temp + .5);
+ parm[14] = 0;
+ }
+ glp_gridgen(NULL, 0, 0, 0, parm);
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/intfeas1.c b/test/monniaux/glpk-4.65/src/api/intfeas1.c
new file mode 100644
index 00000000..43064351
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/intfeas1.c
@@ -0,0 +1,267 @@
+/* intfeas1.c (solve integer feasibility problem) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2011-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+int glp_intfeas1(glp_prob *P, int use_bound, int obj_bound)
+{ /* solve integer feasibility problem */
+ NPP *npp = NULL;
+ glp_prob *mip = NULL;
+ int *obj_ind = NULL;
+ double *obj_val = NULL;
+ int obj_row = 0;
+ int i, j, k, obj_len, temp, ret;
+#if 0 /* 04/IV-2016 */
+ /* check the problem object */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_intfeas1: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (P->tree != NULL)
+ xerror("glp_intfeas1: operation not allowed\n");
+ /* integer solution is currently undefined */
+ P->mip_stat = GLP_UNDEF;
+ P->mip_obj = 0.0;
+ /* check columns (variables) */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+#if 0 /* binarization is not yet implemented */
+ if (!(col->kind == GLP_IV || col->type == GLP_FX))
+ { xprintf("glp_intfeas1: column %d: non-integer non-fixed var"
+ "iable not allowed\n", j);
+#else
+ if (!((col->kind == GLP_IV && col->lb == 0.0 && col->ub == 1.0)
+ || col->type == GLP_FX))
+ { xprintf("glp_intfeas1: column %d: non-binary non-fixed vari"
+ "able not allowed\n", j);
+#endif
+ ret = GLP_EDATA;
+ goto done;
+ }
+ temp = (int)col->lb;
+ if ((double)temp != col->lb)
+ { if (col->type == GLP_FX)
+ xprintf("glp_intfeas1: column %d: fixed value %g is non-"
+ "integer or out of range\n", j, col->lb);
+ else
+ xprintf("glp_intfeas1: column %d: lower bound %g is non-"
+ "integer or out of range\n", j, col->lb);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ temp = (int)col->ub;
+ if ((double)temp != col->ub)
+ { xprintf("glp_intfeas1: column %d: upper bound %g is non-int"
+ "eger or out of range\n", j, col->ub);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ if (col->type == GLP_DB && col->lb > col->ub)
+ { xprintf("glp_intfeas1: column %d: lower bound %g is greater"
+ " than upper bound %g\n", j, col->lb, col->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ /* check rows (constraints) */
+ for (i = 1; i <= P->m; i++)
+ { GLPROW *row = P->row[i];
+ GLPAIJ *aij;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { temp = (int)aij->val;
+ if ((double)temp != aij->val)
+ { xprintf("glp_intfeas1: row = %d, column %d: constraint c"
+ "oefficient %g is non-integer or out of range\n",
+ i, aij->col->j, aij->val);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ }
+ temp = (int)row->lb;
+ if ((double)temp != row->lb)
+ { if (row->type == GLP_FX)
+ xprintf("glp_intfeas1: row = %d: fixed value %g is non-i"
+ "nteger or out of range\n", i, row->lb);
+ else
+ xprintf("glp_intfeas1: row = %d: lower bound %g is non-i"
+ "nteger or out of range\n", i, row->lb);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ temp = (int)row->ub;
+ if ((double)temp != row->ub)
+ { xprintf("glp_intfeas1: row = %d: upper bound %g is non-inte"
+ "ger or out of range\n", i, row->ub);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ if (row->type == GLP_DB && row->lb > row->ub)
+ { xprintf("glp_intfeas1: row %d: lower bound %g is greater th"
+ "an upper bound %g\n", i, row->lb, row->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ /* check the objective function */
+#if 1 /* 08/I-2017 by cmatraki & mao */
+ if (!use_bound)
+ { /* skip check if no obj. bound is specified */
+ goto skip;
+ }
+#endif
+ temp = (int)P->c0;
+ if ((double)temp != P->c0)
+ { xprintf("glp_intfeas1: objective constant term %g is non-integ"
+ "er or out of range\n", P->c0);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ for (j = 1; j <= P->n; j++)
+ { temp = (int)P->col[j]->coef;
+ if ((double)temp != P->col[j]->coef)
+ { xprintf("glp_intfeas1: column %d: objective coefficient is "
+ "non-integer or out of range\n", j, P->col[j]->coef);
+ ret = GLP_EDATA;
+ goto done;
+ }
+ }
+#if 1 /* 08/I-2017 by cmatraki & mao */
+skip: ;
+#endif
+ /* save the objective function and set it to zero */
+ obj_ind = xcalloc(1+P->n, sizeof(int));
+ obj_val = xcalloc(1+P->n, sizeof(double));
+ obj_len = 0;
+ obj_ind[0] = 0;
+ obj_val[0] = P->c0;
+ P->c0 = 0.0;
+ for (j = 1; j <= P->n; j++)
+ { if (P->col[j]->coef != 0.0)
+ { obj_len++;
+ obj_ind[obj_len] = j;
+ obj_val[obj_len] = P->col[j]->coef;
+ P->col[j]->coef = 0.0;
+ }
+ }
+ /* add inequality to bound the objective function, if required */
+ if (!use_bound)
+ xprintf("Will search for ANY feasible solution\n");
+ else
+ { xprintf("Will search only for solution not worse than %d\n",
+ obj_bound);
+ obj_row = glp_add_rows(P, 1);
+ glp_set_mat_row(P, obj_row, obj_len, obj_ind, obj_val);
+ if (P->dir == GLP_MIN)
+ glp_set_row_bnds(P, obj_row,
+ GLP_UP, 0.0, (double)obj_bound - obj_val[0]);
+ else if (P->dir == GLP_MAX)
+ glp_set_row_bnds(P, obj_row,
+ GLP_LO, (double)obj_bound - obj_val[0], 0.0);
+ else
+ xassert(P != P);
+ }
+ /* create preprocessor workspace */
+ xprintf("Translating to CNF-SAT...\n");
+ xprintf("Original problem has %d row%s, %d column%s, and %d non-z"
+ "ero%s\n", P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" :
+ "s", P->nnz, P->nnz == 1 ? "" : "s");
+ npp = npp_create_wksp();
+ /* load the original problem into the preprocessor workspace */
+ npp_load_prob(npp, P, GLP_OFF, GLP_MIP, GLP_OFF);
+ /* perform translation to SAT-CNF problem instance */
+ ret = npp_sat_encode_prob(npp);
+ if (ret == 0)
+ ;
+ else if (ret == GLP_ENOPFS)
+ xprintf("PROBLEM HAS NO INTEGER FEASIBLE SOLUTION\n");
+ else if (ret == GLP_ERANGE)
+ xprintf("glp_intfeas1: translation to SAT-CNF failed because o"
+ "f integer overflow\n");
+ else
+ xassert(ret != ret);
+ if (ret != 0)
+ goto done;
+ /* build SAT-CNF problem instance and try to solve it */
+ mip = glp_create_prob();
+ npp_build_prob(npp, mip);
+ ret = glp_minisat1(mip);
+ /* only integer feasible solution can be postprocessed */
+ if (!(mip->mip_stat == GLP_OPT || mip->mip_stat == GLP_FEAS))
+ { P->mip_stat = mip->mip_stat;
+ goto done;
+ }
+ /* postprocess the solution found */
+ npp_postprocess(npp, mip);
+ /* the transformed problem is no longer needed */
+ glp_delete_prob(mip), mip = NULL;
+ /* store solution to the original problem object */
+ npp_unload_sol(npp, P);
+ /* change the solution status to 'integer feasible' */
+ P->mip_stat = GLP_FEAS;
+ /* check integer feasibility */
+ for (i = 1; i <= P->m; i++)
+ { GLPROW *row;
+ GLPAIJ *aij;
+ double sum;
+ row = P->row[i];
+ sum = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ sum += aij->val * aij->col->mipx;
+ xassert(sum == row->mipx);
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ xassert(sum >= row->lb);
+ if (row->type == GLP_UP || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ xassert(sum <= row->ub);
+ }
+ /* compute value of the original objective function */
+ P->mip_obj = obj_val[0];
+ for (k = 1; k <= obj_len; k++)
+ P->mip_obj += obj_val[k] * P->col[obj_ind[k]]->mipx;
+ xprintf("Objective value = %17.9e\n", P->mip_obj);
+done: /* delete the transformed problem, if it exists */
+ if (mip != NULL)
+ glp_delete_prob(mip);
+ /* delete the preprocessor workspace, if it exists */
+ if (npp != NULL)
+ npp_delete_wksp(npp);
+ /* remove inequality used to bound the objective function */
+ if (obj_row > 0)
+ { int ind[1+1];
+ ind[1] = obj_row;
+ glp_del_rows(P, 1, ind);
+ }
+ /* restore the original objective function */
+ if (obj_ind != NULL)
+ { P->c0 = obj_val[0];
+ for (k = 1; k <= obj_len; k++)
+ P->col[obj_ind[k]]->coef = obj_val[k];
+ xfree(obj_ind);
+ xfree(obj_val);
+ }
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/maxffalg.c b/test/monniaux/glpk-4.65/src/api/maxffalg.c
new file mode 100644
index 00000000..0f3f9b04
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/maxffalg.c
@@ -0,0 +1,130 @@
+/* maxffalg.c (find maximal flow with Ford-Fulkerson algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ffalg.h"
+#include "glpk.h"
+
+int glp_maxflow_ffalg(glp_graph *G, int s, int t, int a_cap,
+ double *sol, int a_x, int v_cut)
+{ /* find maximal flow with Ford-Fulkerson algorithm */
+ glp_vertex *v;
+ glp_arc *a;
+ int nv, na, i, k, flag, *tail, *head, *cap, *x, ret;
+ char *cut;
+ double temp;
+ if (!(1 <= s && s <= G->nv))
+ xerror("glp_maxflow_ffalg: s = %d; source node number out of r"
+ "ange\n", s);
+ if (!(1 <= t && t <= G->nv))
+ xerror("glp_maxflow_ffalg: t = %d: sink node number out of ran"
+ "ge\n", t);
+ if (s == t)
+ xerror("glp_maxflow_ffalg: s = t = %d; source and sink nodes m"
+ "ust be distinct\n", s);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_maxflow_ffalg: a_cap = %d; invalid offset\n",
+ a_cap);
+ if (v_cut >= 0 && v_cut > G->v_size - (int)sizeof(int))
+ xerror("glp_maxflow_ffalg: v_cut = %d; invalid offset\n",
+ v_cut);
+ /* allocate working arrays */
+ nv = G->nv;
+ na = G->na;
+ tail = xcalloc(1+na, sizeof(int));
+ head = xcalloc(1+na, sizeof(int));
+ cap = xcalloc(1+na, sizeof(int));
+ x = xcalloc(1+na, sizeof(int));
+ if (v_cut < 0)
+ cut = NULL;
+ else
+ cut = xcalloc(1+nv, sizeof(char));
+ /* copy the flow network */
+ k = 0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ tail[k] = a->tail->i;
+ head[k] = a->head->i;
+ if (tail[k] == head[k])
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ if (a_cap >= 0)
+ memcpy(&temp, (char *)a->data + a_cap, sizeof(double));
+ else
+ temp = 1.0;
+ if (!(0.0 <= temp && temp <= (double)INT_MAX &&
+ temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ cap[k] = (int)temp;
+ }
+ }
+ xassert(k == na);
+ /* find maximal flow in the flow network */
+ ffalg(nv, na, tail, head, s, t, cap, x, cut);
+ ret = 0;
+ /* store solution components */
+ /* (objective function = total flow through the network) */
+ if (sol != NULL)
+ { temp = 0.0;
+ for (k = 1; k <= na; k++)
+ { if (tail[k] == s)
+ temp += (double)x[k];
+ else if (head[k] == s)
+ temp -= (double)x[k];
+ }
+ *sol = temp;
+ }
+ /* (arc flows) */
+ if (a_x >= 0)
+ { k = 0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { temp = (double)x[++k];
+ memcpy((char *)a->data + a_x, &temp, sizeof(double));
+ }
+ }
+ }
+ /* (node flags) */
+ if (v_cut >= 0)
+ { for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ flag = cut[i];
+ memcpy((char *)v->data + v_cut, &flag, sizeof(int));
+ }
+ }
+done: /* free working arrays */
+ xfree(tail);
+ xfree(head);
+ xfree(cap);
+ xfree(x);
+ if (cut != NULL) xfree(cut);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/maxflp.c b/test/monniaux/glpk-4.65/src/api/maxflp.c
new file mode 100644
index 00000000..1135b78c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/maxflp.c
@@ -0,0 +1,114 @@
+/* maxflp.c (convert maximum flow problem to LP) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_maxflow_lp - convert maximum flow problem to LP
+*
+* SYNOPSIS
+*
+* void glp_maxflow_lp(glp_prob *lp, glp_graph *G, int names, int s,
+* int t, int a_cap);
+*
+* DESCRIPTION
+*
+* The routine glp_maxflow_lp builds an LP problem, which corresponds
+* to the maximum flow problem on the specified network G. */
+
+void glp_maxflow_lp(glp_prob *lp, glp_graph *G, int names, int s,
+ int t, int a_cap)
+{ glp_vertex *v;
+ glp_arc *a;
+ int i, j, type, ind[1+2];
+ double cap, val[1+2];
+ if (!(names == GLP_ON || names == GLP_OFF))
+ xerror("glp_maxflow_lp: names = %d; invalid parameter\n",
+ names);
+ if (!(1 <= s && s <= G->nv))
+ xerror("glp_maxflow_lp: s = %d; source node number out of rang"
+ "e\n", s);
+ if (!(1 <= t && t <= G->nv))
+ xerror("glp_maxflow_lp: t = %d: sink node number out of range "
+ "\n", t);
+ if (s == t)
+ xerror("glp_maxflow_lp: s = t = %d; source and sink nodes must"
+ " be distinct\n", s);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_maxflow_lp: a_cap = %d; invalid offset\n", a_cap);
+ glp_erase_prob(lp);
+ if (names) glp_set_prob_name(lp, G->name);
+ glp_set_obj_dir(lp, GLP_MAX);
+ glp_add_rows(lp, G->nv);
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (names) glp_set_row_name(lp, i, v->name);
+ if (i == s)
+ type = GLP_LO;
+ else if (i == t)
+ type = GLP_UP;
+ else
+ type = GLP_FX;
+ glp_set_row_bnds(lp, i, type, 0.0, 0.0);
+ }
+ if (G->na > 0) glp_add_cols(lp, G->na);
+ for (i = 1, j = 0; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { j++;
+ if (names)
+ { char name[50+1];
+ sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
+ xassert(strlen(name) < sizeof(name));
+ glp_set_col_name(lp, j, name);
+ }
+ if (a->tail->i != a->head->i)
+ { ind[1] = a->tail->i, val[1] = +1.0;
+ ind[2] = a->head->i, val[2] = -1.0;
+ glp_set_mat_col(lp, j, 2, ind, val);
+ }
+ if (a_cap >= 0)
+ memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
+ else
+ cap = 1.0;
+ if (cap == DBL_MAX)
+ type = GLP_LO;
+ else if (cap != 0.0)
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_col_bnds(lp, j, type, 0.0, cap);
+ if (a->tail->i == s)
+ glp_set_obj_coef(lp, j, +1.0);
+ else if (a->head->i == s)
+ glp_set_obj_coef(lp, j, -1.0);
+ }
+ }
+ xassert(j == G->na);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/mcflp.c b/test/monniaux/glpk-4.65/src/api/mcflp.c
new file mode 100644
index 00000000..5cd24060
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/mcflp.c
@@ -0,0 +1,114 @@
+/* mcflp.c (convert minimum cost flow problem to LP) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_mincost_lp - convert minimum cost flow problem to LP
+*
+* SYNOPSIS
+*
+* void glp_mincost_lp(glp_prob *lp, glp_graph *G, int names,
+* int v_rhs, int a_low, int a_cap, int a_cost);
+*
+* DESCRIPTION
+*
+* The routine glp_mincost_lp builds an LP problem, which corresponds
+* to the minimum cost flow problem on the specified network G. */
+
+void glp_mincost_lp(glp_prob *lp, glp_graph *G, int names, int v_rhs,
+ int a_low, int a_cap, int a_cost)
+{ glp_vertex *v;
+ glp_arc *a;
+ int i, j, type, ind[1+2];
+ double rhs, low, cap, cost, val[1+2];
+ if (!(names == GLP_ON || names == GLP_OFF))
+ xerror("glp_mincost_lp: names = %d; invalid parameter\n",
+ names);
+ if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_mincost_lp: v_rhs = %d; invalid offset\n", v_rhs);
+ if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_lp: a_low = %d; invalid offset\n", a_low);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_lp: a_cap = %d; invalid offset\n", a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_lp: a_cost = %d; invalid offset\n", a_cost)
+ ;
+ glp_erase_prob(lp);
+ if (names) glp_set_prob_name(lp, G->name);
+ if (G->nv > 0) glp_add_rows(lp, G->nv);
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (names) glp_set_row_name(lp, i, v->name);
+ if (v_rhs >= 0)
+ memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double));
+ else
+ rhs = 0.0;
+ glp_set_row_bnds(lp, i, GLP_FX, rhs, rhs);
+ }
+ if (G->na > 0) glp_add_cols(lp, G->na);
+ for (i = 1, j = 0; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { j++;
+ if (names)
+ { char name[50+1];
+ sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
+ xassert(strlen(name) < sizeof(name));
+ glp_set_col_name(lp, j, name);
+ }
+ if (a->tail->i != a->head->i)
+ { ind[1] = a->tail->i, val[1] = +1.0;
+ ind[2] = a->head->i, val[2] = -1.0;
+ glp_set_mat_col(lp, j, 2, ind, val);
+ }
+ if (a_low >= 0)
+ memcpy(&low, (char *)a->data + a_low, sizeof(double));
+ else
+ low = 0.0;
+ if (a_cap >= 0)
+ memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
+ else
+ cap = 1.0;
+ if (cap == DBL_MAX)
+ type = GLP_LO;
+ else if (low != cap)
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_col_bnds(lp, j, type, low, cap);
+ if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 0.0;
+ glp_set_obj_coef(lp, j, cost);
+ }
+ }
+ xassert(j == G->na);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/mcfokalg.c b/test/monniaux/glpk-4.65/src/api/mcfokalg.c
new file mode 100644
index 00000000..786dc71b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/mcfokalg.c
@@ -0,0 +1,221 @@
+/* mcfokalg.c (find minimum-cost flow with out-of-kilter algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "okalg.h"
+
+int glp_mincost_okalg(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, double *sol, int a_x, int v_pi)
+{ /* find minimum-cost flow with out-of-kilter algorithm */
+ glp_vertex *v;
+ glp_arc *a;
+ int nv, na, i, k, s, t, *tail, *head, *low, *cap, *cost, *x, *pi,
+ ret;
+ double sum, temp;
+ if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: v_rhs = %d; invalid offset\n",
+ v_rhs);
+ if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: a_low = %d; invalid offset\n",
+ a_low);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: a_cap = %d; invalid offset\n",
+ a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: a_cost = %d; invalid offset\n",
+ a_cost);
+ if (a_x >= 0 && a_x > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: a_x = %d; invalid offset\n", a_x);
+ if (v_pi >= 0 && v_pi > G->v_size - (int)sizeof(double))
+ xerror("glp_mincost_okalg: v_pi = %d; invalid offset\n", v_pi);
+ /* s is artificial source node */
+ s = G->nv + 1;
+ /* t is artificial sink node */
+ t = s + 1;
+ /* nv is the total number of nodes in the resulting network */
+ nv = t;
+ /* na is the total number of arcs in the resulting network */
+ na = G->na + 1;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (v_rhs >= 0)
+ memcpy(&temp, (char *)v->data + v_rhs, sizeof(double));
+ else
+ temp = 0.0;
+ if (temp != 0.0) na++;
+ }
+ /* allocate working arrays */
+ tail = xcalloc(1+na, sizeof(int));
+ head = xcalloc(1+na, sizeof(int));
+ low = xcalloc(1+na, sizeof(int));
+ cap = xcalloc(1+na, sizeof(int));
+ cost = xcalloc(1+na, sizeof(int));
+ x = xcalloc(1+na, sizeof(int));
+ pi = xcalloc(1+nv, sizeof(int));
+ /* construct the resulting network */
+ k = 0;
+ /* (original arcs) */
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ tail[k] = a->tail->i;
+ head[k] = a->head->i;
+ if (tail[k] == head[k])
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ if (a_low >= 0)
+ memcpy(&temp, (char *)a->data + a_low, sizeof(double));
+ else
+ temp = 0.0;
+ if (!(0.0 <= temp && temp <= (double)INT_MAX &&
+ temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ low[k] = (int)temp;
+ if (a_cap >= 0)
+ memcpy(&temp, (char *)a->data + a_cap, sizeof(double));
+ else
+ temp = 1.0;
+ if (!((double)low[k] <= temp && temp <= (double)INT_MAX &&
+ temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ cap[k] = (int)temp;
+ if (a_cost >= 0)
+ memcpy(&temp, (char *)a->data + a_cost, sizeof(double));
+ else
+ temp = 0.0;
+ if (!(fabs(temp) <= (double)INT_MAX && temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ cost[k] = (int)temp;
+ }
+ }
+ /* (artificial arcs) */
+ sum = 0.0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (v_rhs >= 0)
+ memcpy(&temp, (char *)v->data + v_rhs, sizeof(double));
+ else
+ temp = 0.0;
+ if (!(fabs(temp) <= (double)INT_MAX && temp == floor(temp)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ if (temp > 0.0)
+ { /* artificial arc from s to original source i */
+ k++;
+ tail[k] = s;
+ head[k] = i;
+ low[k] = cap[k] = (int)(+temp); /* supply */
+ cost[k] = 0;
+ sum += (double)temp;
+ }
+ else if (temp < 0.0)
+ { /* artificial arc from original sink i to t */
+ k++;
+ tail[k] = i;
+ head[k] = t;
+ low[k] = cap[k] = (int)(-temp); /* demand */
+ cost[k] = 0;
+ }
+ }
+ /* (feedback arc from t to s) */
+ k++;
+ xassert(k == na);
+ tail[k] = t;
+ head[k] = s;
+ if (sum > (double)INT_MAX)
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ low[k] = cap[k] = (int)sum; /* total supply/demand */
+ cost[k] = 0;
+ /* find minimal-cost circulation in the resulting network */
+ ret = okalg(nv, na, tail, head, low, cap, cost, x, pi);
+ switch (ret)
+ { case 0:
+ /* optimal circulation found */
+ ret = 0;
+ break;
+ case 1:
+ /* no feasible circulation exists */
+ ret = GLP_ENOPFS;
+ break;
+ case 2:
+ /* integer overflow occured */
+ ret = GLP_ERANGE;
+ goto done;
+ case 3:
+ /* optimality test failed (logic error) */
+ ret = GLP_EFAIL;
+ goto done;
+ default:
+ xassert(ret != ret);
+ }
+ /* store solution components */
+ /* (objective function = the total cost) */
+ if (sol != NULL)
+ { temp = 0.0;
+ for (k = 1; k <= na; k++)
+ temp += (double)cost[k] * (double)x[k];
+ *sol = temp;
+ }
+ /* (arc flows) */
+ if (a_x >= 0)
+ { k = 0;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { temp = (double)x[++k];
+ memcpy((char *)a->data + a_x, &temp, sizeof(double));
+ }
+ }
+ }
+ /* (node potentials = Lagrange multipliers) */
+ if (v_pi >= 0)
+ { for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ temp = - (double)pi[i];
+ memcpy((char *)v->data + v_pi, &temp, sizeof(double));
+ }
+ }
+done: /* free working arrays */
+ xfree(tail);
+ xfree(head);
+ xfree(low);
+ xfree(cap);
+ xfree(cost);
+ xfree(x);
+ xfree(pi);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/mcfrelax.c b/test/monniaux/glpk-4.65/src/api/mcfrelax.c
new file mode 100644
index 00000000..9b34949a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/mcfrelax.c
@@ -0,0 +1,251 @@
+/* mcfrelax.c (find minimum-cost flow with RELAX-IV) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "relax4.h"
+
+static int overflow(int u, int v)
+{ /* check for integer overflow on computing u + v */
+ if (u > 0 && v > 0 && u + v < 0) return 1;
+ if (u < 0 && v < 0 && u + v > 0) return 1;
+ return 0;
+}
+
+int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, int crash, double *sol, int a_x, int a_rc)
+{ /* find minimum-cost flow with Bertsekas-Tseng relaxation method
+ (RELAX-IV) */
+ glp_vertex *v;
+ glp_arc *a;
+ struct relax4_csa csa;
+ int i, k, large, n, na, ret;
+ double cap, cost, low, rc, rhs, sum, x;
+ if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: v_rhs = %d; invalid offset\n",
+ v_rhs);
+ if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: a_low = %d; invalid offset\n",
+ a_low);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: a_cap = %d; invalid offset\n",
+ a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: a_cost = %d; invalid offset\n",
+ a_cost);
+ if (a_x >= 0 && a_x > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: a_x = %d; invalid offset\n",
+ a_x);
+ if (a_rc >= 0 && a_rc > G->a_size - (int)sizeof(double))
+ xerror("glp_mincost_relax4: a_rc = %d; invalid offset\n",
+ a_rc);
+ csa.n = n = G->nv; /* number of nodes */
+ csa.na = na = G->na; /* number of arcs */
+ csa.large = large = INT_MAX / 4;
+ csa.repeat = 0;
+ csa.crash = crash;
+ /* allocate working arrays */
+ csa.startn = xcalloc(1+na, sizeof(int));
+ csa.endn = xcalloc(1+na, sizeof(int));
+ csa.fou = xcalloc(1+n, sizeof(int));
+ csa.nxtou = xcalloc(1+na, sizeof(int));
+ csa.fin = xcalloc(1+n, sizeof(int));
+ csa.nxtin = xcalloc(1+na, sizeof(int));
+ csa.rc = xcalloc(1+na, sizeof(int));
+ csa.u = xcalloc(1+na, sizeof(int));
+ csa.dfct = xcalloc(1+n, sizeof(int));
+ csa.x = xcalloc(1+na, sizeof(int));
+ csa.label = xcalloc(1+n, sizeof(int));
+ csa.prdcsr = xcalloc(1+n, sizeof(int));
+ csa.save = xcalloc(1+na, sizeof(int));
+ csa.tfstou = xcalloc(1+n, sizeof(int));
+ csa.tnxtou = xcalloc(1+na, sizeof(int));
+ csa.tfstin = xcalloc(1+n, sizeof(int));
+ csa.tnxtin = xcalloc(1+na, sizeof(int));
+ csa.nxtqueue = xcalloc(1+n, sizeof(int));
+ csa.scan = xcalloc(1+n, sizeof(char));
+ csa.mark = xcalloc(1+n, sizeof(char));
+ if (crash)
+ { csa.extend_arc = xcalloc(1+n, sizeof(int));
+ csa.sb_level = xcalloc(1+n, sizeof(int));
+ csa.sb_arc = xcalloc(1+n, sizeof(int));
+ }
+ else
+ { csa.extend_arc = NULL;
+ csa.sb_level = NULL;
+ csa.sb_arc = NULL;
+ }
+ /* scan nodes */
+ for (i = 1; i <= n; i++)
+ { v = G->v[i];
+ /* get supply at i-th node */
+ if (v_rhs >= 0)
+ memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double));
+ else
+ rhs = 0.0;
+ if (!(fabs(rhs) <= (double)large && rhs == floor(rhs)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ /* set demand at i-th node */
+ csa.dfct[i] = -(int)rhs;
+ }
+ /* scan arcs */
+ k = 0;
+ for (i = 1; i <= n; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ /* set endpoints of k-th arc */
+ if (a->tail->i == a->head->i)
+ { /* self-loops not allowed */
+ ret = GLP_EDATA;
+ goto done;
+ }
+ csa.startn[k] = a->tail->i;
+ csa.endn[k] = a->head->i;
+ /* set per-unit cost for k-th arc flow */
+ if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 0.0;
+ if (!(fabs(cost) <= (double)large && cost == floor(cost)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ csa.rc[k] = (int)cost;
+ /* get lower bound for k-th arc flow */
+ if (a_low >= 0)
+ memcpy(&low, (char *)a->data + a_low, sizeof(double));
+ else
+ low = 0.0;
+ if (!(0.0 <= low && low <= (double)large &&
+ low == floor(low)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ /* get upper bound for k-th arc flow */
+ if (a_cap >= 0)
+ memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
+ else
+ cap = 1.0;
+ if (!(low <= cap && cap <= (double)large &&
+ cap == floor(cap)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ /* substitute x = x' + low, where 0 <= x' <= cap - low */
+ csa.u[k] = (int)(cap - low);
+ /* correct demands at endpoints of k-th arc */
+ if (overflow(csa.dfct[a->tail->i], +low))
+ { ret = GLP_ERANGE;
+ goto done;
+ }
+#if 0 /* 29/IX-2017 */
+ csa.dfct[a->tail->i] += low;
+#else
+ csa.dfct[a->tail->i] += (int)low;
+#endif
+ if (overflow(csa.dfct[a->head->i], -low))
+ { ret = GLP_ERANGE;
+ goto done;
+ }
+#if 0 /* 29/IX-2017 */
+ csa.dfct[a->head->i] -= low;
+#else
+ csa.dfct[a->head->i] -= (int)low;
+#endif
+ }
+ }
+ /* construct linked list for network topology */
+ relax4_inidat(&csa);
+ /* find minimum-cost flow */
+ ret = relax4(&csa);
+ if (ret != 0)
+ { /* problem is found to be infeasible */
+ xassert(1 <= ret && ret <= 8);
+ ret = GLP_ENOPFS;
+ goto done;
+ }
+ /* store solution */
+ sum = 0.0;
+ k = 0;
+ for (i = 1; i <= n; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { k++;
+ /* get lower bound for k-th arc flow */
+ if (a_low >= 0)
+ memcpy(&low, (char *)a->data + a_low, sizeof(double));
+ else
+ low = 0.0;
+ /* store original flow x = x' + low thru k-th arc */
+ x = (double)csa.x[k] + low;
+ if (a_x >= 0)
+ memcpy((char *)a->data + a_x, &x, sizeof(double));
+ /* store reduced cost for k-th arc flow */
+ rc = (double)csa.rc[k];
+ if (a_rc >= 0)
+ memcpy((char *)a->data + a_rc, &rc, sizeof(double));
+ /* get per-unit cost for k-th arc flow */
+ if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 0.0;
+ /* compute the total cost */
+ sum += cost * x;
+ }
+ }
+ /* store the total cost */
+ if (sol != NULL)
+ *sol = sum;
+done: /* free working arrays */
+ xfree(csa.startn);
+ xfree(csa.endn);
+ xfree(csa.fou);
+ xfree(csa.nxtou);
+ xfree(csa.fin);
+ xfree(csa.nxtin);
+ xfree(csa.rc);
+ xfree(csa.u);
+ xfree(csa.dfct);
+ xfree(csa.x);
+ xfree(csa.label);
+ xfree(csa.prdcsr);
+ xfree(csa.save);
+ xfree(csa.tfstou);
+ xfree(csa.tnxtou);
+ xfree(csa.tfstin);
+ xfree(csa.tnxtin);
+ xfree(csa.nxtqueue);
+ xfree(csa.scan);
+ xfree(csa.mark);
+ if (crash)
+ { xfree(csa.extend_arc);
+ xfree(csa.sb_level);
+ xfree(csa.sb_arc);
+ }
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/minisat1.c b/test/monniaux/glpk-4.65/src/api/minisat1.c
new file mode 100644
index 00000000..a669c487
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/minisat1.c
@@ -0,0 +1,161 @@
+/* minisat1.c (driver to MiniSat solver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2011-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "minisat.h"
+#include "prob.h"
+
+int glp_minisat1(glp_prob *P)
+{ /* solve CNF-SAT problem with MiniSat solver */
+ solver *s;
+ GLPAIJ *aij;
+ int i, j, len, ret, *ind;
+ double sum;
+#if 0 /* 04/IV-2016 */
+ /* check problem object */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_minisat1: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (P->tree != NULL)
+ xerror("glp_minisat1: operation not allowed\n");
+ /* integer solution is currently undefined */
+ P->mip_stat = GLP_UNDEF;
+ P->mip_obj = 0.0;
+ /* check that problem object encodes CNF-SAT instance */
+ if (glp_check_cnfsat(P) != 0)
+ { xprintf("glp_minisat1: problem object does not encode CNF-SAT "
+ "instance\n");
+ ret = GLP_EDATA;
+ goto done;
+ }
+#if 0 /* 08/I-2017 by cmatraki */
+#if 1 /* 07/XI-2015 */
+ if (sizeof(void *) != sizeof(int))
+ { xprintf("glp_minisat1: sorry, MiniSat solver is not supported "
+ "on 64-bit platforms\n");
+ ret = GLP_EFAIL;
+ goto done;
+ }
+#endif
+#else
+ if (sizeof(void *) != sizeof(size_t))
+ { xprintf("glp_minisat1: sorry, MiniSat solver is not supported "
+ "on this platform\n");
+ ret = GLP_EFAIL;
+ goto done;
+ }
+#endif
+ /* solve CNF-SAT problem */
+ xprintf("Solving CNF-SAT problem...\n");
+ xprintf("Instance has %d variable%s, %d clause%s, and %d literal%"
+ "s\n", P->n, P->n == 1 ? "" : "s", P->m, P->m == 1 ? "" : "s",
+ P->nnz, P->nnz == 1 ? "" : "s");
+ /* if CNF-SAT has no clauses, it is satisfiable */
+ if (P->m == 0)
+ { P->mip_stat = GLP_OPT;
+ for (j = 1; j <= P->n; j++)
+ P->col[j]->mipx = 0.0;
+ goto fini;
+ }
+ /* if CNF-SAT has an empty clause, it is unsatisfiable */
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->ptr == NULL)
+ { P->mip_stat = GLP_NOFEAS;
+ goto fini;
+ }
+ }
+ /* prepare input data for the solver */
+ s = solver_new();
+ solver_setnvars(s, P->n);
+ ind = xcalloc(1+P->n, sizeof(int));
+ for (i = 1; i <= P->m; i++)
+ { len = 0;
+ for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { ind[++len] = toLit(aij->col->j-1);
+ if (aij->val < 0.0)
+ ind[len] = lit_neg(ind[len]);
+ }
+ xassert(len > 0);
+#if 0 /* 08/I-2017 by cmatraki */
+ xassert(solver_addclause(s, &ind[1], &ind[1+len]));
+#else
+ if (!solver_addclause(s, &ind[1], &ind[1+len]))
+ { /* found trivial conflict */
+ xfree(ind);
+ solver_delete(s);
+ P->mip_stat = GLP_NOFEAS;
+ goto fini;
+ }
+#endif
+ }
+ xfree(ind);
+ /* call the solver */
+ s->verbosity = 1;
+ if (solver_solve(s, 0, 0))
+ { /* instance is reported as satisfiable */
+ P->mip_stat = GLP_OPT;
+ /* copy solution to the problem object */
+ xassert(s->model.size == P->n);
+ for (j = 1; j <= P->n; j++)
+ { P->col[j]->mipx =
+ s->model.ptr[j-1] == l_True ? 1.0 : 0.0;
+ }
+ /* compute row values */
+ for (i = 1; i <= P->m; i++)
+ { sum = 0;
+ for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ sum += aij->val * aij->col->mipx;
+ P->row[i]->mipx = sum;
+ }
+ /* check integer feasibility */
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->mipx < P->row[i]->lb)
+ { /* solution is wrong */
+ P->mip_stat = GLP_UNDEF;
+ break;
+ }
+ }
+ }
+ else
+ { /* instance is reported as unsatisfiable */
+ P->mip_stat = GLP_NOFEAS;
+ }
+ solver_delete(s);
+fini: /* report the instance status */
+ if (P->mip_stat == GLP_OPT)
+ { xprintf("SATISFIABLE\n");
+ ret = 0;
+ }
+ else if (P->mip_stat == GLP_NOFEAS)
+ { xprintf("UNSATISFIABLE\n");
+ ret = 0;
+ }
+ else
+ { xprintf("glp_minisat1: solver failed\n");
+ ret = GLP_EFAIL;
+ }
+done: return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/mpl.c b/test/monniaux/glpk-4.65/src/api/mpl.c
new file mode 100644
index 00000000..cfa6f75b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/mpl.c
@@ -0,0 +1,269 @@
+/* mpl.c (processing model in GNU MathProg language) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+#include "prob.h"
+
+glp_tran *glp_mpl_alloc_wksp(void)
+{ /* allocate the MathProg translator workspace */
+ glp_tran *tran;
+ tran = mpl_initialize();
+ return tran;
+}
+
+void glp_mpl_init_rand(glp_tran *tran, int seed)
+{ /* initialize pseudo-random number generator */
+ if (tran->phase != 0)
+ xerror("glp_mpl_init_rand: invalid call sequence\n");
+ rng_init_rand(tran->rand, seed);
+ return;
+}
+
+int glp_mpl_read_model(glp_tran *tran, const char *fname, int skip)
+{ /* read and translate model section */
+ int ret;
+ if (tran->phase != 0)
+ xerror("glp_mpl_read_model: invalid call sequence\n");
+ ret = mpl_read_model(tran, (char *)fname, skip);
+ if (ret == 1 || ret == 2)
+ ret = 0;
+ else if (ret == 4)
+ ret = 1;
+ else
+ xassert(ret != ret);
+ return ret;
+}
+
+int glp_mpl_read_data(glp_tran *tran, const char *fname)
+{ /* read and translate data section */
+ int ret;
+ if (!(tran->phase == 1 || tran->phase == 2))
+ xerror("glp_mpl_read_data: invalid call sequence\n");
+ ret = mpl_read_data(tran, (char *)fname);
+ if (ret == 2)
+ ret = 0;
+ else if (ret == 4)
+ ret = 1;
+ else
+ xassert(ret != ret);
+ return ret;
+}
+
+int glp_mpl_generate(glp_tran *tran, const char *fname)
+{ /* generate the model */
+ int ret;
+ if (!(tran->phase == 1 || tran->phase == 2))
+ xerror("glp_mpl_generate: invalid call sequence\n");
+ ret = mpl_generate(tran, (char *)fname);
+ if (ret == 3)
+ ret = 0;
+ else if (ret == 4)
+ ret = 1;
+ return ret;
+}
+
+void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob)
+{ /* build LP/MIP problem instance from the model */
+ int m, n, i, j, t, kind, type, len, *ind;
+ double lb, ub, *val;
+ if (tran->phase != 3)
+ xerror("glp_mpl_build_prob: invalid call sequence\n");
+ /* erase the problem object */
+ glp_erase_prob(prob);
+ /* set problem name */
+ glp_set_prob_name(prob, mpl_get_prob_name(tran));
+ /* build rows (constraints) */
+ m = mpl_get_num_rows(tran);
+ if (m > 0)
+ glp_add_rows(prob, m);
+ for (i = 1; i <= m; i++)
+ { /* set row name */
+ glp_set_row_name(prob, i, mpl_get_row_name(tran, i));
+ /* set row bounds */
+ type = mpl_get_row_bnds(tran, i, &lb, &ub);
+ switch (type)
+ { case MPL_FR: type = GLP_FR; break;
+ case MPL_LO: type = GLP_LO; break;
+ case MPL_UP: type = GLP_UP; break;
+ case MPL_DB: type = GLP_DB; break;
+ case MPL_FX: type = GLP_FX; break;
+ default: xassert(type != type);
+ }
+ if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
+ { type = GLP_FX;
+ if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
+ }
+ glp_set_row_bnds(prob, i, type, lb, ub);
+ /* warn about non-zero constant term */
+ if (mpl_get_row_c0(tran, i) != 0.0)
+ xprintf("glp_mpl_build_prob: row %s; constant term %.12g ig"
+ "nored\n",
+ mpl_get_row_name(tran, i), mpl_get_row_c0(tran, i));
+ }
+ /* build columns (variables) */
+ n = mpl_get_num_cols(tran);
+ if (n > 0)
+ glp_add_cols(prob, n);
+ for (j = 1; j <= n; j++)
+ { /* set column name */
+ glp_set_col_name(prob, j, mpl_get_col_name(tran, j));
+ /* set column kind */
+ kind = mpl_get_col_kind(tran, j);
+ switch (kind)
+ { case MPL_NUM:
+ break;
+ case MPL_INT:
+ case MPL_BIN:
+ glp_set_col_kind(prob, j, GLP_IV);
+ break;
+ default:
+ xassert(kind != kind);
+ }
+ /* set column bounds */
+ type = mpl_get_col_bnds(tran, j, &lb, &ub);
+ switch (type)
+ { case MPL_FR: type = GLP_FR; break;
+ case MPL_LO: type = GLP_LO; break;
+ case MPL_UP: type = GLP_UP; break;
+ case MPL_DB: type = GLP_DB; break;
+ case MPL_FX: type = GLP_FX; break;
+ default: xassert(type != type);
+ }
+ if (kind == MPL_BIN)
+ { if (type == GLP_FR || type == GLP_UP || lb < 0.0) lb = 0.0;
+ if (type == GLP_FR || type == GLP_LO || ub > 1.0) ub = 1.0;
+ type = GLP_DB;
+ }
+ if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
+ { type = GLP_FX;
+ if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
+ }
+ glp_set_col_bnds(prob, j, type, lb, ub);
+ }
+ /* load the constraint matrix */
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ for (i = 1; i <= m; i++)
+ { len = mpl_get_mat_row(tran, i, ind, val);
+ glp_set_mat_row(prob, i, len, ind, val);
+ }
+ /* build objective function (the first objective is used) */
+ for (i = 1; i <= m; i++)
+ { kind = mpl_get_row_kind(tran, i);
+ if (kind == MPL_MIN || kind == MPL_MAX)
+ { /* set objective name */
+ glp_set_obj_name(prob, mpl_get_row_name(tran, i));
+ /* set optimization direction */
+ glp_set_obj_dir(prob, kind == MPL_MIN ? GLP_MIN : GLP_MAX);
+ /* set constant term */
+ glp_set_obj_coef(prob, 0, mpl_get_row_c0(tran, i));
+ /* set objective coefficients */
+ len = mpl_get_mat_row(tran, i, ind, val);
+ for (t = 1; t <= len; t++)
+ glp_set_obj_coef(prob, ind[t], val[t]);
+ break;
+ }
+ }
+ /* free working arrays */
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+int glp_mpl_postsolve(glp_tran *tran, glp_prob *prob, int sol)
+{ /* postsolve the model */
+ int i, j, m, n, stat, ret;
+ double prim, dual;
+ if (!(tran->phase == 3 && !tran->flag_p))
+ xerror("glp_mpl_postsolve: invalid call sequence\n");
+ if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP))
+ xerror("glp_mpl_postsolve: sol = %d; invalid parameter\n",
+ sol);
+ m = mpl_get_num_rows(tran);
+ n = mpl_get_num_cols(tran);
+ if (!(m == glp_get_num_rows(prob) &&
+ n == glp_get_num_cols(prob)))
+ xerror("glp_mpl_postsolve: wrong problem object\n");
+ if (!mpl_has_solve_stmt(tran))
+ { ret = 0;
+ goto done;
+ }
+ for (i = 1; i <= m; i++)
+ { if (sol == GLP_SOL)
+ { stat = glp_get_row_stat(prob, i);
+ prim = glp_get_row_prim(prob, i);
+ dual = glp_get_row_dual(prob, i);
+ }
+ else if (sol == GLP_IPT)
+ { stat = 0;
+ prim = glp_ipt_row_prim(prob, i);
+ dual = glp_ipt_row_dual(prob, i);
+ }
+ else if (sol == GLP_MIP)
+ { stat = 0;
+ prim = glp_mip_row_val(prob, i);
+ dual = 0.0;
+ }
+ else
+ xassert(sol != sol);
+ if (fabs(prim) < 1e-9) prim = 0.0;
+ if (fabs(dual) < 1e-9) dual = 0.0;
+ mpl_put_row_soln(tran, i, stat, prim, dual);
+ }
+ for (j = 1; j <= n; j++)
+ { if (sol == GLP_SOL)
+ { stat = glp_get_col_stat(prob, j);
+ prim = glp_get_col_prim(prob, j);
+ dual = glp_get_col_dual(prob, j);
+ }
+ else if (sol == GLP_IPT)
+ { stat = 0;
+ prim = glp_ipt_col_prim(prob, j);
+ dual = glp_ipt_col_dual(prob, j);
+ }
+ else if (sol == GLP_MIP)
+ { stat = 0;
+ prim = glp_mip_col_val(prob, j);
+ dual = 0.0;
+ }
+ else
+ xassert(sol != sol);
+ if (fabs(prim) < 1e-9) prim = 0.0;
+ if (fabs(dual) < 1e-9) dual = 0.0;
+ mpl_put_col_soln(tran, j, stat, prim, dual);
+ }
+ ret = mpl_postsolve(tran);
+ if (ret == 3)
+ ret = 0;
+ else if (ret == 4)
+ ret = 1;
+done: return ret;
+}
+
+void glp_mpl_free_wksp(glp_tran *tran)
+{ /* free the MathProg translator workspace */
+ mpl_terminate(tran);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/mps.c b/test/monniaux/glpk-4.65/src/api/mps.c
new file mode 100644
index 00000000..3bdc6db1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/mps.c
@@ -0,0 +1,1452 @@
+/* mps.c (MPS format routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_init_mpscp - initialize MPS format control parameters
+*
+* SYNOPSIS
+*
+* void glp_init_mpscp(glp_mpscp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_init_mpscp initializes control parameters, which are
+* used by the MPS input/output routines glp_read_mps and glp_write_mps,
+* with default values.
+*
+* Default values of the control parameters are stored in the glp_mpscp
+* structure, which the parameter parm points to. */
+
+void glp_init_mpscp(glp_mpscp *parm)
+{ parm->blank = '\0';
+ parm->obj_name = NULL;
+ parm->tol_mps = 1e-12;
+ return;
+}
+
+static void check_parm(const char *func, const glp_mpscp *parm)
+{ /* check control parameters */
+ if (!(0x00 <= parm->blank && parm->blank <= 0xFF) ||
+ !(parm->blank == '\0' || isprint(parm->blank)))
+ xerror("%s: blank = 0x%02X; invalid parameter\n",
+ func, parm->blank);
+ if (!(parm->obj_name == NULL || strlen(parm->obj_name) <= 255))
+ xerror("%s: obj_name = \"%.12s...\"; parameter too long\n",
+ func, parm->obj_name);
+ if (!(0.0 <= parm->tol_mps && parm->tol_mps < 1.0))
+ xerror("%s: tol_mps = %g; invalid parameter\n",
+ func, parm->tol_mps);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_read_mps - read problem data in MPS format
+*
+* SYNOPSIS
+*
+* int glp_read_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_mps reads problem data in MPS format from a
+* text file.
+*
+* The parameter fmt specifies the version of MPS format:
+*
+* GLP_MPS_DECK - fixed (ancient) MPS format;
+* GLP_MPS_FILE - free (modern) MPS format.
+*
+* The parameter parm is a pointer to the structure glp_mpscp, which
+* specifies control parameters used by the routine. If parm is NULL,
+* the routine uses default settings.
+*
+* The character string fname specifies a name of the text file to be
+* read.
+*
+* Note that before reading data the current content of the problem
+* object is completely erased with the routine glp_erase_prob.
+*
+* RETURNS
+*
+* If the operation was successful, the routine glp_read_mps returns
+* zero. Otherwise, it prints an error message and returns non-zero. */
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* pointer to problem object */
+ int deck;
+ /* MPS format (0 - free, 1 - fixed) */
+ const glp_mpscp *parm;
+ /* pointer to control parameters */
+ const char *fname;
+ /* name of input MPS file */
+ glp_file *fp;
+ /* stream assigned to input MPS file */
+ jmp_buf jump;
+ /* label for go to in case of error */
+ int recno;
+ /* current record (card) number */
+ int recpos;
+ /* current record (card) position */
+ int c;
+ /* current character */
+ int fldno;
+ /* current field number */
+ char field[255+1];
+ /* current field content */
+ int w80;
+ /* warning 'record must not be longer than 80 chars' issued */
+ int wef;
+ /* warning 'extra fields detected beyond field 6' issued */
+ int obj_row;
+ /* objective row number */
+ void *work1, *work2, *work3;
+ /* working arrays */
+};
+
+static void error(struct csa *csa, const char *fmt, ...)
+{ /* print error message and terminate processing */
+ va_list arg;
+ xprintf("%s:%d: ", csa->fname, csa->recno);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ longjmp(csa->jump, 1);
+ /* no return */
+}
+
+static void warning(struct csa *csa, const char *fmt, ...)
+{ /* print warning message and continue processing */
+ va_list arg;
+ xprintf("%s:%d: warning: ", csa->fname, csa->recno);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ return;
+}
+
+static void read_char(struct csa *csa)
+{ /* read next character */
+ int c;
+ if (csa->c == '\n')
+ csa->recno++, csa->recpos = 0;
+ csa->recpos++;
+read: c = glp_getc(csa->fp);
+ if (c < 0)
+ { if (glp_ioerr(csa->fp))
+ error(csa, "read error - %s\n", get_err_msg());
+ else if (csa->c == '\n')
+ error(csa, "unexpected end of file\n");
+ else
+ { warning(csa, "missing final end of line\n");
+ c = '\n';
+ }
+ }
+ else if (c == '\n')
+ ;
+ else if (csa->c == '\r')
+ { c = '\r';
+ goto badc;
+ }
+ else if (csa->deck && c == '\r')
+ { csa->c = '\r';
+ goto read;
+ }
+ else if (c == ' ')
+ ;
+ else if (isspace(c))
+ { if (csa->deck)
+badc: error(csa, "in fixed MPS format white-space character 0x%02"
+ "X is not allowed\n", c);
+ c = ' ';
+ }
+ else if (iscntrl(c))
+ error(csa, "invalid control character 0x%02X\n", c);
+ if (csa->deck && csa->recpos == 81 && c != '\n' && csa->w80 < 1)
+ { warning(csa, "in fixed MPS format record must not be longer th"
+ "an 80 characters\n");
+ csa->w80++;
+ }
+ csa->c = c;
+ return;
+}
+
+static int indicator(struct csa *csa, int name)
+{ /* skip comment records and read possible indicator record */
+ int ret;
+ /* reset current field number */
+ csa->fldno = 0;
+loop: /* read the very first character of the next record */
+ xassert(csa->c == '\n');
+ read_char(csa);
+ if (csa->c == ' ' || csa->c == '\n')
+ { /* data record */
+ ret = 0;
+ }
+ else if (csa->c == '*')
+ { /* comment record */
+ while (csa->c != '\n')
+ read_char(csa);
+ goto loop;
+ }
+ else
+ { /* indicator record */
+ int len = 0;
+ while (csa->c != ' ' && csa->c != '\n' && len < 12)
+ { csa->field[len++] = (char)csa->c;
+ read_char(csa);
+ }
+ csa->field[len] = '\0';
+ if (!(strcmp(csa->field, "NAME") == 0 ||
+ strcmp(csa->field, "ROWS") == 0 ||
+ strcmp(csa->field, "COLUMNS") == 0 ||
+ strcmp(csa->field, "RHS") == 0 ||
+ strcmp(csa->field, "RANGES") == 0 ||
+ strcmp(csa->field, "BOUNDS") == 0 ||
+ strcmp(csa->field, "ENDATA") == 0))
+ error(csa, "invalid indicator record\n");
+ if (!name)
+ { while (csa->c != '\n')
+ read_char(csa);
+ }
+ ret = 1;
+ }
+ return ret;
+}
+
+static void read_field(struct csa *csa)
+{ /* read next field of the current data record */
+ csa->fldno++;
+ if (csa->deck)
+ { /* fixed MPS format */
+ int beg, end, pos;
+ /* determine predefined field positions */
+ if (csa->fldno == 1)
+ beg = 2, end = 3;
+ else if (csa->fldno == 2)
+ beg = 5, end = 12;
+ else if (csa->fldno == 3)
+ beg = 15, end = 22;
+ else if (csa->fldno == 4)
+ beg = 25, end = 36;
+ else if (csa->fldno == 5)
+ beg = 40, end = 47;
+ else if (csa->fldno == 6)
+ beg = 50, end = 61;
+ else
+ xassert(csa != csa);
+ /* skip blanks preceding the current field */
+ if (csa->c != '\n')
+ { pos = csa->recpos;
+ while (csa->recpos < beg)
+ { if (csa->c == ' ')
+ ;
+ else if (csa->c == '\n')
+ break;
+ else
+ error(csa, "in fixed MPS format positions %d-%d must "
+ "be blank\n", pos, beg-1);
+ read_char(csa);
+ }
+ }
+ /* skip possible comment beginning in the field 3 or 5 */
+ if ((csa->fldno == 3 || csa->fldno == 5) && csa->c == '$')
+ { while (csa->c != '\n')
+ read_char(csa);
+ }
+ /* read the current field */
+ for (pos = beg; pos <= end; pos++)
+ { if (csa->c == '\n') break;
+ csa->field[pos-beg] = (char)csa->c;
+ read_char(csa);
+ }
+ csa->field[pos-beg] = '\0';
+ strtrim(csa->field);
+ /* skip blanks following the last field */
+ if (csa->fldno == 6 && csa->c != '\n')
+ { while (csa->recpos <= 72)
+ { if (csa->c == ' ')
+ ;
+ else if (csa->c == '\n')
+ break;
+ else
+ error(csa, "in fixed MPS format positions 62-72 must "
+ "be blank\n");
+ read_char(csa);
+ }
+ while (csa->c != '\n')
+ read_char(csa);
+ }
+ }
+ else
+ { /* free MPS format */
+ int len;
+ /* skip blanks preceding the current field */
+ while (csa->c == ' ')
+ read_char(csa);
+ /* skip possible comment */
+ if (csa->c == '$')
+ { while (csa->c != '\n')
+ read_char(csa);
+ }
+ /* read the current field */
+ len = 0;
+ while (!(csa->c == ' ' || csa->c == '\n'))
+ { if (len == 255)
+ error(csa, "length of field %d exceeds 255 characters\n",
+ csa->fldno++);
+ csa->field[len++] = (char)csa->c;
+ read_char(csa);
+ }
+ csa->field[len] = '\0';
+ /* skip anything following the last field (any extra fields
+ are considered to be comments) */
+ if (csa->fldno == 6)
+ { while (csa->c == ' ')
+ read_char(csa);
+ if (csa->c != '$' && csa->c != '\n' && csa->wef < 1)
+ { warning(csa, "some extra field(s) detected beyond field "
+ "6; field(s) ignored\n");
+ csa->wef++;
+ }
+ while (csa->c != '\n')
+ read_char(csa);
+ }
+ }
+ return;
+}
+
+static void patch_name(struct csa *csa, char *name)
+{ /* process embedded blanks in symbolic name */
+ int blank = csa->parm->blank;
+ if (blank == '\0')
+ { /* remove emedded blanks */
+ strspx(name);
+ }
+ else
+ { /* replace embedded blanks by specified character */
+ for (; *name != '\0'; name++)
+ if (*name == ' ') *name = (char)blank;
+ }
+ return;
+}
+
+static double read_number(struct csa *csa)
+{ /* read next field and convert it to floating-point number */
+ double x;
+ char *s;
+ /* read next field */
+ read_field(csa);
+ xassert(csa->fldno == 4 || csa->fldno == 6);
+ if (csa->field[0] == '\0')
+ error(csa, "missing numeric value in field %d\n", csa->fldno);
+ /* skip initial spaces of the field */
+ for (s = csa->field; *s == ' '; s++);
+ /* perform conversion */
+ if (str2num(s, &x) != 0)
+ error(csa, "cannot convert '%s' to floating-point number\n",
+ s);
+ return x;
+}
+
+static void skip_field(struct csa *csa)
+{ /* read and skip next field (assumed to be blank) */
+ read_field(csa);
+ if (csa->field[0] != '\0')
+ error(csa, "field %d must be blank\n", csa->fldno);
+ return;
+}
+
+static void read_name(struct csa *csa)
+{ /* read NAME indicator record */
+ if (!(indicator(csa, 1) && strcmp(csa->field, "NAME") == 0))
+ error(csa, "missing NAME indicator record\n");
+ /* this indicator record looks like a data record; simulate that
+ fields 1 and 2 were read */
+ csa->fldno = 2;
+ /* field 3: model name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ warning(csa, "missing model name in field 3\n");
+ else
+ glp_set_prob_name(csa->P, csa->field);
+ /* skip anything following field 3 */
+ while (csa->c != '\n')
+ read_char(csa);
+ return;
+}
+
+static void read_rows(struct csa *csa)
+{ /* read ROWS section */
+ int i, type;
+loop: if (indicator(csa, 0)) goto done;
+ /* field 1: row type */
+ read_field(csa), strspx(csa->field);
+ if (strcmp(csa->field, "N") == 0)
+ type = GLP_FR;
+ else if (strcmp(csa->field, "G") == 0)
+ type = GLP_LO;
+ else if (strcmp(csa->field, "L") == 0)
+ type = GLP_UP;
+ else if (strcmp(csa->field, "E") == 0)
+ type = GLP_FX;
+ else if (csa->field[0] == '\0')
+ error(csa, "missing row type in field 1\n");
+ else
+ error(csa, "invalid row type in field 1\n");
+ /* field 2: row name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ error(csa, "missing row name in field 2\n");
+ if (glp_find_row(csa->P, csa->field) != 0)
+ error(csa, "row '%s' multiply specified\n", csa->field);
+ i = glp_add_rows(csa->P, 1);
+ glp_set_row_name(csa->P, i, csa->field);
+ glp_set_row_bnds(csa->P, i, type, 0.0, 0.0);
+ /* fields 3, 4, 5, and 6 must be blank */
+ skip_field(csa);
+ skip_field(csa);
+ skip_field(csa);
+ skip_field(csa);
+ goto loop;
+done: return;
+}
+
+static void read_columns(struct csa *csa)
+{ /* read COLUMNS section */
+ int i, j, f, len, kind = GLP_CV, *ind;
+ double aij, *val;
+ char name[255+1], *flag;
+ /* allocate working arrays */
+ csa->work1 = ind = xcalloc(1+csa->P->m, sizeof(int));
+ csa->work2 = val = xcalloc(1+csa->P->m, sizeof(double));
+ csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
+ memset(&flag[1], 0, csa->P->m);
+ /* no current column exists */
+ j = 0, len = 0;
+loop: if (indicator(csa, 0)) goto done;
+ /* field 1 must be blank */
+ if (csa->deck)
+ { read_field(csa);
+ if (csa->field[0] != '\0')
+ error(csa, "field 1 must be blank\n");
+ }
+ else
+ csa->fldno++;
+ /* field 2: column or kind name */
+ read_field(csa), patch_name(csa, csa->field);
+ strcpy(name, csa->field);
+ /* field 3: row name or keyword 'MARKER' */
+ read_field(csa), patch_name(csa, csa->field);
+ if (strcmp(csa->field, "'MARKER'") == 0)
+ { /* process kind data record */
+ /* field 4 must be blank */
+ if (csa->deck)
+ { read_field(csa);
+ if (csa->field[0] != '\0')
+ error(csa, "field 4 must be blank\n");
+ }
+ else
+ csa->fldno++;
+ /* field 5: keyword 'INTORG' or 'INTEND' */
+ read_field(csa), patch_name(csa, csa->field);
+ if (strcmp(csa->field, "'INTORG'") == 0)
+ kind = GLP_IV;
+ else if (strcmp(csa->field, "'INTEND'") == 0)
+ kind = GLP_CV;
+ else if (csa->field[0] == '\0')
+ error(csa, "missing keyword in field 5\n");
+ else
+ error(csa, "invalid keyword in field 5\n");
+ /* field 6 must be blank */
+ skip_field(csa);
+ goto loop;
+ }
+ /* process column name specified in field 2 */
+ if (name[0] == '\0')
+ { /* the same column as in previous data record */
+ if (j == 0)
+ error(csa, "missing column name in field 2\n");
+ }
+ else if (j != 0 && strcmp(name, csa->P->col[j]->name) == 0)
+ { /* the same column as in previous data record */
+ xassert(j != 0);
+ }
+ else
+ { /* store the current column */
+ if (j != 0)
+ { glp_set_mat_col(csa->P, j, len, ind, val);
+ while (len > 0) flag[ind[len--]] = 0;
+ }
+ /* create new column */
+ if (glp_find_col(csa->P, name) != 0)
+ error(csa, "column '%s' multiply specified\n", name);
+ j = glp_add_cols(csa->P, 1);
+ glp_set_col_name(csa->P, j, name);
+ glp_set_col_kind(csa->P, j, kind);
+ if (kind == GLP_CV)
+ glp_set_col_bnds(csa->P, j, GLP_LO, 0.0, 0.0);
+ else if (kind == GLP_IV)
+ glp_set_col_bnds(csa->P, j, GLP_DB, 0.0, 1.0);
+ else
+ xassert(kind != kind);
+ }
+ /* process fields 3-4 and 5-6 */
+ for (f = 3; f <= 5; f += 2)
+ { /* field 3 or 5: row name */
+ if (f == 3)
+ { if (csa->field[0] == '\0')
+ error(csa, "missing row name in field 3\n");
+ }
+ else
+ { read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { /* if field 5 is blank, field 6 also must be blank */
+ skip_field(csa);
+ continue;
+ }
+ }
+ i = glp_find_row(csa->P, csa->field);
+ if (i == 0)
+ error(csa, "row '%s' not found\n", csa->field);
+ if (flag[i])
+ error(csa, "duplicate coefficient in row '%s'\n",
+ csa->field);
+ /* field 4 or 6: coefficient value */
+ aij = read_number(csa);
+ if (fabs(aij) < csa->parm->tol_mps) aij = 0.0;
+ len++, ind[len] = i, val[len] = aij, flag[i] = 1;
+ }
+ goto loop;
+done: /* store the last column */
+ if (j != 0)
+ glp_set_mat_col(csa->P, j, len, ind, val);
+ /* free working arrays */
+ xfree(ind);
+ xfree(val);
+ xfree(flag);
+ csa->work1 = csa->work2 = csa->work3 = NULL;
+ return;
+}
+
+static void read_rhs(struct csa *csa)
+{ /* read RHS section */
+ int i, f, v, type;
+ double rhs;
+ char name[255+1], *flag;
+ /* allocate working array */
+ csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
+ memset(&flag[1], 0, csa->P->m);
+ /* no current RHS vector exists */
+ v = 0;
+loop: if (indicator(csa, 0)) goto done;
+ /* field 1 must be blank */
+ if (csa->deck)
+ { read_field(csa);
+ if (csa->field[0] != '\0')
+ error(csa, "field 1 must be blank\n");
+ }
+ else
+ csa->fldno++;
+ /* field 2: RHS vector name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { /* the same RHS vector as in previous data record */
+ if (v == 0)
+ { warning(csa, "missing RHS vector name in field 2\n");
+ goto blnk;
+ }
+ }
+ else if (v != 0 && strcmp(csa->field, name) == 0)
+ { /* the same RHS vector as in previous data record */
+ xassert(v != 0);
+ }
+ else
+blnk: { /* new RHS vector */
+ if (v != 0)
+ error(csa, "multiple RHS vectors not supported\n");
+ v++;
+ strcpy(name, csa->field);
+ }
+ /* process fields 3-4 and 5-6 */
+ for (f = 3; f <= 5; f += 2)
+ { /* field 3 or 5: row name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { if (f == 3)
+ error(csa, "missing row name in field 3\n");
+ else
+ { /* if field 5 is blank, field 6 also must be blank */
+ skip_field(csa);
+ continue;
+ }
+ }
+ i = glp_find_row(csa->P, csa->field);
+ if (i == 0)
+ error(csa, "row '%s' not found\n", csa->field);
+ if (flag[i])
+ error(csa, "duplicate right-hand side for row '%s'\n",
+ csa->field);
+ /* field 4 or 6: right-hand side value */
+ rhs = read_number(csa);
+ if (fabs(rhs) < csa->parm->tol_mps) rhs = 0.0;
+ type = csa->P->row[i]->type;
+ if (type == GLP_FR)
+ { if (i == csa->obj_row)
+ glp_set_obj_coef(csa->P, 0, rhs);
+ else if (rhs != 0.0)
+ warning(csa, "non-zero right-hand side for free row '%s'"
+ " ignored\n", csa->P->row[i]->name);
+ }
+ else
+ glp_set_row_bnds(csa->P, i, type, rhs, rhs);
+ flag[i] = 1;
+ }
+ goto loop;
+done: /* free working array */
+ xfree(flag);
+ csa->work3 = NULL;
+ return;
+}
+
+static void read_ranges(struct csa *csa)
+{ /* read RANGES section */
+ int i, f, v, type;
+ double rhs, rng;
+ char name[255+1], *flag;
+ /* allocate working array */
+ csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
+ memset(&flag[1], 0, csa->P->m);
+ /* no current RANGES vector exists */
+ v = 0;
+loop: if (indicator(csa, 0)) goto done;
+ /* field 1 must be blank */
+ if (csa->deck)
+ { read_field(csa);
+ if (csa->field[0] != '\0')
+ error(csa, "field 1 must be blank\n");
+ }
+ else
+ csa->fldno++;
+ /* field 2: RANGES vector name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { /* the same RANGES vector as in previous data record */
+ if (v == 0)
+ { warning(csa, "missing RANGES vector name in field 2\n");
+ goto blnk;
+ }
+ }
+ else if (v != 0 && strcmp(csa->field, name) == 0)
+ { /* the same RANGES vector as in previous data record */
+ xassert(v != 0);
+ }
+ else
+blnk: { /* new RANGES vector */
+ if (v != 0)
+ error(csa, "multiple RANGES vectors not supported\n");
+ v++;
+ strcpy(name, csa->field);
+ }
+ /* process fields 3-4 and 5-6 */
+ for (f = 3; f <= 5; f += 2)
+ { /* field 3 or 5: row name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { if (f == 3)
+ error(csa, "missing row name in field 3\n");
+ else
+ { /* if field 5 is blank, field 6 also must be blank */
+ skip_field(csa);
+ continue;
+ }
+ }
+ i = glp_find_row(csa->P, csa->field);
+ if (i == 0)
+ error(csa, "row '%s' not found\n", csa->field);
+ if (flag[i])
+ error(csa, "duplicate range for row '%s'\n", csa->field);
+ /* field 4 or 6: range value */
+ rng = read_number(csa);
+ if (fabs(rng) < csa->parm->tol_mps) rng = 0.0;
+ type = csa->P->row[i]->type;
+ if (type == GLP_FR)
+ warning(csa, "range for free row '%s' ignored\n",
+ csa->P->row[i]->name);
+ else if (type == GLP_LO)
+ { rhs = csa->P->row[i]->lb;
+#if 0 /* 26/V-2017 by cmatraki */
+ glp_set_row_bnds(csa->P, i, rhs == 0.0 ? GLP_FX : GLP_DB,
+#else
+ glp_set_row_bnds(csa->P, i, rng == 0.0 ? GLP_FX : GLP_DB,
+#endif
+ rhs, rhs + fabs(rng));
+ }
+ else if (type == GLP_UP)
+ { rhs = csa->P->row[i]->ub;
+#if 0 /* 26/V-2017 by cmatraki */
+ glp_set_row_bnds(csa->P, i, rhs == 0.0 ? GLP_FX : GLP_DB,
+#else
+ glp_set_row_bnds(csa->P, i, rng == 0.0 ? GLP_FX : GLP_DB,
+#endif
+ rhs - fabs(rng), rhs);
+ }
+ else if (type == GLP_FX)
+ { rhs = csa->P->row[i]->lb;
+ if (rng > 0.0)
+ glp_set_row_bnds(csa->P, i, GLP_DB, rhs, rhs + rng);
+ else if (rng < 0.0)
+ glp_set_row_bnds(csa->P, i, GLP_DB, rhs + rng, rhs);
+ }
+ else
+ xassert(type != type);
+ flag[i] = 1;
+ }
+ goto loop;
+done: /* free working array */
+ xfree(flag);
+ csa->work3 = NULL;
+ return;
+}
+
+static void read_bounds(struct csa *csa)
+{ /* read BOUNDS section */
+ GLPCOL *col;
+ int j, v, mask, data;
+ double bnd, lb, ub;
+ char type[2+1], name[255+1], *flag;
+ /* allocate working array */
+ csa->work3 = flag = xcalloc(1+csa->P->n, sizeof(char));
+ memset(&flag[1], 0, csa->P->n);
+ /* no current BOUNDS vector exists */
+ v = 0;
+loop: if (indicator(csa, 0)) goto done;
+ /* field 1: bound type */
+ read_field(csa);
+ if (strcmp(csa->field, "LO") == 0)
+ mask = 0x01, data = 1;
+ else if (strcmp(csa->field, "UP") == 0)
+ mask = 0x10, data = 1;
+ else if (strcmp(csa->field, "FX") == 0)
+ mask = 0x11, data = 1;
+ else if (strcmp(csa->field, "FR") == 0)
+ mask = 0x11, data = 0;
+ else if (strcmp(csa->field, "MI") == 0)
+ mask = 0x01, data = 0;
+ else if (strcmp(csa->field, "PL") == 0)
+ mask = 0x10, data = 0;
+ else if (strcmp(csa->field, "LI") == 0)
+ mask = 0x01, data = 1;
+ else if (strcmp(csa->field, "UI") == 0)
+ mask = 0x10, data = 1;
+ else if (strcmp(csa->field, "BV") == 0)
+ mask = 0x11, data = 0;
+ else if (csa->field[0] == '\0')
+ error(csa, "missing bound type in field 1\n");
+ else
+ error(csa, "invalid bound type in field 1\n");
+ strcpy(type, csa->field);
+ /* field 2: BOUNDS vector name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ { /* the same BOUNDS vector as in previous data record */
+ if (v == 0)
+ { warning(csa, "missing BOUNDS vector name in field 2\n");
+ goto blnk;
+ }
+ }
+ else if (v != 0 && strcmp(csa->field, name) == 0)
+ { /* the same BOUNDS vector as in previous data record */
+ xassert(v != 0);
+ }
+ else
+blnk: { /* new BOUNDS vector */
+ if (v != 0)
+ error(csa, "multiple BOUNDS vectors not supported\n");
+ v++;
+ strcpy(name, csa->field);
+ }
+ /* field 3: column name */
+ read_field(csa), patch_name(csa, csa->field);
+ if (csa->field[0] == '\0')
+ error(csa, "missing column name in field 3\n");
+ j = glp_find_col(csa->P, csa->field);
+ if (j == 0)
+ error(csa, "column '%s' not found\n", csa->field);
+ if ((flag[j] & mask) == 0x01)
+ error(csa, "duplicate lower bound for column '%s'\n",
+ csa->field);
+ if ((flag[j] & mask) == 0x10)
+ error(csa, "duplicate upper bound for column '%s'\n",
+ csa->field);
+ xassert((flag[j] & mask) == 0x00);
+ /* field 4: bound value */
+ if (data)
+ { bnd = read_number(csa);
+ if (fabs(bnd) < csa->parm->tol_mps) bnd = 0.0;
+ }
+ else
+ read_field(csa), bnd = 0.0;
+ /* get current column bounds */
+ col = csa->P->col[j];
+ if (col->type == GLP_FR)
+ lb = -DBL_MAX, ub = +DBL_MAX;
+ else if (col->type == GLP_LO)
+ lb = col->lb, ub = +DBL_MAX;
+ else if (col->type == GLP_UP)
+ lb = -DBL_MAX, ub = col->ub;
+ else if (col->type == GLP_DB)
+ lb = col->lb, ub = col->ub;
+ else if (col->type == GLP_FX)
+ lb = ub = col->lb;
+ else
+ xassert(col != col);
+ /* change column bounds */
+ if (strcmp(type, "LO") == 0)
+ lb = bnd;
+ else if (strcmp(type, "UP") == 0)
+ ub = bnd;
+ else if (strcmp(type, "FX") == 0)
+ lb = ub = bnd;
+ else if (strcmp(type, "FR") == 0)
+ lb = -DBL_MAX, ub = +DBL_MAX;
+ else if (strcmp(type, "MI") == 0)
+ lb = -DBL_MAX;
+ else if (strcmp(type, "PL") == 0)
+ ub = +DBL_MAX;
+ else if (strcmp(type, "LI") == 0)
+ { glp_set_col_kind(csa->P, j, GLP_IV);
+ lb = ceil(bnd);
+#if 1 /* 16/VII-2013 */
+ /* if column upper bound has not been explicitly specified,
+ take it as +inf */
+ if (!(flag[j] & 0x10))
+ ub = +DBL_MAX;
+#endif
+ }
+ else if (strcmp(type, "UI") == 0)
+ { glp_set_col_kind(csa->P, j, GLP_IV);
+ ub = floor(bnd);
+ }
+ else if (strcmp(type, "BV") == 0)
+ { glp_set_col_kind(csa->P, j, GLP_IV);
+ lb = 0.0, ub = 1.0;
+ }
+ else
+ xassert(type != type);
+ /* set new column bounds */
+ if (lb == -DBL_MAX && ub == +DBL_MAX)
+ glp_set_col_bnds(csa->P, j, GLP_FR, lb, ub);
+ else if (ub == +DBL_MAX)
+ glp_set_col_bnds(csa->P, j, GLP_LO, lb, ub);
+ else if (lb == -DBL_MAX)
+ glp_set_col_bnds(csa->P, j, GLP_UP, lb, ub);
+ else if (lb != ub)
+ glp_set_col_bnds(csa->P, j, GLP_DB, lb, ub);
+ else
+ glp_set_col_bnds(csa->P, j, GLP_FX, lb, ub);
+ flag[j] |= (char)mask;
+ /* fields 5 and 6 must be blank */
+ skip_field(csa);
+ skip_field(csa);
+ goto loop;
+done: /* free working array */
+ xfree(flag);
+ csa->work3 = NULL;
+ return;
+}
+
+int glp_read_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+ const char *fname)
+{ /* read problem data in MPS format */
+ glp_mpscp _parm;
+ struct csa _csa, *csa = &_csa;
+ int ret;
+ xprintf("Reading problem data from '%s'...\n", fname);
+ if (!(fmt == GLP_MPS_DECK || fmt == GLP_MPS_FILE))
+ xerror("glp_read_mps: fmt = %d; invalid parameter\n", fmt);
+ if (parm == NULL)
+ glp_init_mpscp(&_parm), parm = &_parm;
+ /* check control parameters */
+ check_parm("glp_read_mps", parm);
+ /* initialize common storage area */
+ csa->P = P;
+ csa->deck = (fmt == GLP_MPS_DECK);
+ csa->parm = parm;
+ csa->fname = fname;
+ csa->fp = NULL;
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->recno = csa->recpos = 0;
+ csa->c = '\n';
+ csa->fldno = 0;
+ csa->field[0] = '\0';
+ csa->w80 = csa->wef = 0;
+ csa->obj_row = 0;
+ csa->work1 = csa->work2 = csa->work3 = NULL;
+ /* erase problem object */
+ glp_erase_prob(P);
+ glp_create_index(P);
+ /* open input MPS file */
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* read NAME indicator record */
+ read_name(csa);
+ if (P->name != NULL)
+ xprintf("Problem: %s\n", P->name);
+ /* read ROWS section */
+ if (!(indicator(csa, 0) && strcmp(csa->field, "ROWS") == 0))
+ error(csa, "missing ROWS indicator record\n");
+ read_rows(csa);
+ /* determine objective row */
+ if (parm->obj_name == NULL || parm->obj_name[0] == '\0')
+ { /* use the first row of N type */
+ int i;
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->type == GLP_FR)
+ { csa->obj_row = i;
+ break;
+ }
+ }
+ if (csa->obj_row == 0)
+ warning(csa, "unable to determine objective row\n");
+ }
+ else
+ { /* use a row with specified name */
+ int i;
+ for (i = 1; i <= P->m; i++)
+ { xassert(P->row[i]->name != NULL);
+ if (strcmp(parm->obj_name, P->row[i]->name) == 0)
+ { csa->obj_row = i;
+ break;
+ }
+ }
+ if (csa->obj_row == 0)
+ error(csa, "objective row '%s' not found\n",
+ parm->obj_name);
+ }
+ if (csa->obj_row != 0)
+ { glp_set_obj_name(P, P->row[csa->obj_row]->name);
+ xprintf("Objective: %s\n", P->obj);
+ }
+ /* read COLUMNS section */
+ if (strcmp(csa->field, "COLUMNS") != 0)
+ error(csa, "missing COLUMNS indicator record\n");
+ read_columns(csa);
+ /* set objective coefficients */
+ if (csa->obj_row != 0)
+ { GLPAIJ *aij;
+ for (aij = P->row[csa->obj_row]->ptr; aij != NULL; aij =
+ aij->r_next) glp_set_obj_coef(P, aij->col->j, aij->val);
+ }
+ /* read optional RHS section */
+ if (strcmp(csa->field, "RHS") == 0)
+ read_rhs(csa);
+ /* read optional RANGES section */
+ if (strcmp(csa->field, "RANGES") == 0)
+ read_ranges(csa);
+ /* read optional BOUNDS section */
+ if (strcmp(csa->field, "BOUNDS") == 0)
+ read_bounds(csa);
+ /* read ENDATA indicator record */
+ if (strcmp(csa->field, "ENDATA") != 0)
+ error(csa, "invalid use of %s indicator record\n",
+ csa->field);
+ /* print some statistics */
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
+ P->nnz, P->nnz == 1 ? "" : "s");
+ if (glp_get_num_int(P) > 0)
+ { int ni = glp_get_num_int(P);
+ int nb = glp_get_num_bin(P);
+ if (ni == 1)
+ { if (nb == 0)
+ xprintf("One variable is integer\n");
+ else
+ xprintf("One variable is binary\n");
+ }
+ else
+ { xprintf("%d integer variables, ", ni);
+ if (nb == 0)
+ xprintf("none");
+ else if (nb == 1)
+ xprintf("one");
+ else if (nb == ni)
+ xprintf("all");
+ else
+ xprintf("%d", nb);
+ xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
+ }
+ }
+ xprintf("%d records were read\n", csa->recno);
+#if 1 /* 31/III-2016 */
+ /* free (unbounded) row(s) in MPS file are intended to specify
+ * objective function(s), so all such rows can be removed */
+#if 1 /* 08/VIII-2013 */
+ /* remove free rows */
+ { int i, nrs, *num;
+ num = talloc(1+P->m, int);
+ nrs = 0;
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->type == GLP_FR)
+ num[++nrs] = i;
+ }
+ if (nrs > 0)
+ { glp_del_rows(P, nrs, num);
+ if (nrs == 1)
+ xprintf("One free row was removed\n");
+ else
+ xprintf("%d free rows were removed\n", nrs);
+ }
+ tfree(num);
+ }
+#endif
+#else
+ /* if objective function row is free, remove it */
+ if (csa->obj_row != 0 && P->row[csa->obj_row]->type == GLP_FR)
+ { int num[1+1];
+ num[1] = csa->obj_row;
+ glp_del_rows(P, 1, num);
+ xprintf("Free objective row was removed\n");
+ }
+#endif
+ /* problem data has been successfully read */
+ glp_delete_index(P);
+ glp_sort_matrix(P);
+ ret = 0;
+done: if (csa->fp != NULL) glp_close(csa->fp);
+ if (csa->work1 != NULL) xfree(csa->work1);
+ if (csa->work2 != NULL) xfree(csa->work2);
+ if (csa->work3 != NULL) xfree(csa->work3);
+ if (ret != 0) glp_erase_prob(P);
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_write_mps - write problem data in MPS format
+*
+* SYNOPSIS
+*
+* int glp_write_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_mps writes problem data in MPS format to a
+* text file.
+*
+* The parameter fmt specifies the version of MPS format:
+*
+* GLP_MPS_DECK - fixed (ancient) MPS format;
+* GLP_MPS_FILE - free (modern) MPS format.
+*
+* The parameter parm is a pointer to the structure glp_mpscp, which
+* specifies control parameters used by the routine. If parm is NULL,
+* the routine uses default settings.
+*
+* The character string fname specifies a name of the text file to be
+* written.
+*
+* RETURNS
+*
+* If the operation was successful, the routine glp_read_mps returns
+* zero. Otherwise, it prints an error message and returns non-zero. */
+
+#define csa csa1
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* pointer to problem object */
+ int deck;
+ /* MPS format (0 - free, 1 - fixed) */
+ const glp_mpscp *parm;
+ /* pointer to control parameters */
+ char field[255+1];
+ /* field buffer */
+};
+
+static char *mps_name(struct csa *csa)
+{ /* make problem name */
+ char *f;
+ if (csa->P->name == NULL)
+ csa->field[0] = '\0';
+ else if (csa->deck)
+ { strncpy(csa->field, csa->P->name, 8);
+ csa->field[8] = '\0';
+ }
+ else
+ strcpy(csa->field, csa->P->name);
+ for (f = csa->field; *f != '\0'; f++)
+ if (*f == ' ') *f = '_';
+ return csa->field;
+}
+
+static char *row_name(struct csa *csa, int i)
+{ /* make i-th row name */
+ char *f;
+ xassert(0 <= i && i <= csa->P->m);
+ if (i == 0 || csa->P->row[i]->name == NULL ||
+ csa->deck && strlen(csa->P->row[i]->name) > 8)
+ sprintf(csa->field, "R%07d", i);
+ else
+ { strcpy(csa->field, csa->P->row[i]->name);
+ for (f = csa->field; *f != '\0'; f++)
+ if (*f == ' ') *f = '_';
+ }
+ return csa->field;
+}
+
+static char *col_name(struct csa *csa, int j)
+{ /* make j-th column name */
+ char *f;
+ xassert(1 <= j && j <= csa->P->n);
+ if (csa->P->col[j]->name == NULL ||
+ csa->deck && strlen(csa->P->col[j]->name) > 8)
+ sprintf(csa->field, "C%07d", j);
+ else
+ { strcpy(csa->field, csa->P->col[j]->name);
+ for (f = csa->field; *f != '\0'; f++)
+ if (*f == ' ') *f = '_';
+ }
+ return csa->field;
+}
+
+static char *mps_numb(struct csa *csa, double val)
+{ /* format floating-point number */
+ int dig;
+ char *exp;
+ for (dig = 12; dig >= 6; dig--)
+ { if (val != 0.0 && fabs(val) < 0.002)
+ sprintf(csa->field, "%.*E", dig-1, val);
+ else
+ sprintf(csa->field, "%.*G", dig, val);
+ exp = strchr(csa->field, 'E');
+ if (exp != NULL)
+ sprintf(exp+1, "%d", atoi(exp+1));
+ if (strlen(csa->field) <= 12) break;
+ }
+ xassert(strlen(csa->field) <= 12);
+ return csa->field;
+}
+
+int glp_write_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+ const char *fname)
+{ /* write problem data in MPS format */
+ glp_mpscp _parm;
+ struct csa _csa, *csa = &_csa;
+ glp_file *fp;
+ int out_obj, one_col = 0, empty = 0;
+ int i, j, recno, marker, count, gap, ret;
+ xprintf("Writing problem data to '%s'...\n", fname);
+ if (!(fmt == GLP_MPS_DECK || fmt == GLP_MPS_FILE))
+ xerror("glp_write_mps: fmt = %d; invalid parameter\n", fmt);
+ if (parm == NULL)
+ glp_init_mpscp(&_parm), parm = &_parm;
+ /* check control parameters */
+ check_parm("glp_write_mps", parm);
+ /* initialize common storage area */
+ csa->P = P;
+ csa->deck = (fmt == GLP_MPS_DECK);
+ csa->parm = parm;
+ /* create output MPS file */
+ fp = glp_open(fname, "w"), recno = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* write comment records */
+ xfprintf(fp, "* %-*s%s\n", P->name == NULL ? 1 : 12, "Problem:",
+ P->name == NULL ? "" : P->name), recno++;
+ xfprintf(fp, "* %-12s%s\n", "Class:", glp_get_num_int(P) == 0 ?
+ "LP" : "MIP"), recno++;
+ xfprintf(fp, "* %-12s%d\n", "Rows:", P->m), recno++;
+ if (glp_get_num_int(P) == 0)
+ xfprintf(fp, "* %-12s%d\n", "Columns:", P->n), recno++;
+ else
+ xfprintf(fp, "* %-12s%d (%d integer, %d binary)\n",
+ "Columns:", P->n, glp_get_num_int(P), glp_get_num_bin(P)),
+ recno++;
+ xfprintf(fp, "* %-12s%d\n", "Non-zeros:", P->nnz), recno++;
+ xfprintf(fp, "* %-12s%s\n", "Format:", csa->deck ? "Fixed MPS" :
+ "Free MPS"), recno++;
+ xfprintf(fp, "*\n", recno++);
+ /* write NAME indicator record */
+ xfprintf(fp, "NAME%*s%s\n",
+ P->name == NULL ? 0 : csa->deck ? 10 : 1, "", mps_name(csa)),
+ recno++;
+#if 1
+ /* determine whether to write the objective row */
+ out_obj = 1;
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->type == GLP_FR)
+ { out_obj = 0;
+ break;
+ }
+ }
+#endif
+ /* write ROWS section */
+ xfprintf(fp, "ROWS\n"), recno++;
+ for (i = (out_obj ? 0 : 1); i <= P->m; i++)
+ { int type;
+ type = (i == 0 ? GLP_FR : P->row[i]->type);
+ if (type == GLP_FR)
+ type = 'N';
+ else if (type == GLP_LO)
+ type = 'G';
+ else if (type == GLP_UP)
+ type = 'L';
+ else if (type == GLP_DB || type == GLP_FX)
+ type = 'E';
+ else
+ xassert(type != type);
+ xfprintf(fp, " %c%*s%s\n", type, csa->deck ? 2 : 1, "",
+ row_name(csa, i)), recno++;
+ }
+ /* write COLUMNS section */
+ xfprintf(fp, "COLUMNS\n"), recno++;
+ marker = 0;
+ for (j = 1; j <= P->n; j++)
+ { GLPAIJ cj, *aij;
+ int kind;
+ kind = P->col[j]->kind;
+ if (kind == GLP_CV)
+ { if (marker % 2 == 1)
+ { /* close current integer block */
+ marker++;
+ xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTEND'\n",
+ csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
+ csa->deck ? 17 : 1, ""), recno++;
+ }
+ }
+ else if (kind == GLP_IV)
+ { if (marker % 2 == 0)
+ { /* open new integer block */
+ marker++;
+ xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTORG'\n",
+ csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
+ csa->deck ? 17 : 1, ""), recno++;
+ }
+ }
+ else
+ xassert(kind != kind);
+ if (out_obj && P->col[j]->coef != 0.0)
+ { /* make fake objective coefficient */
+ aij = &cj;
+ aij->row = NULL;
+ aij->val = P->col[j]->coef;
+ aij->c_next = P->col[j]->ptr;
+ }
+ else
+ aij = P->col[j]->ptr;
+#if 1 /* FIXME */
+ if (aij == NULL)
+ { /* empty column */
+ empty++;
+ xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
+ csa->deck ? 8 : 1, col_name(csa, j));
+ /* we need a row */
+ xassert(P->m > 0);
+ xfprintf(fp, "%*s%-*s",
+ csa->deck ? 2 : 1, "", csa->deck ? 8 : 1,
+ row_name(csa, 1));
+ xfprintf(fp, "%*s0%*s$ empty column\n",
+ csa->deck ? 13 : 1, "", csa->deck ? 3 : 1, ""), recno++;
+ }
+#endif
+ count = 0;
+ for (aij = aij; aij != NULL; aij = aij->c_next)
+ { if (one_col || count % 2 == 0)
+ xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
+ csa->deck ? 8 : 1, col_name(csa, j));
+ gap = (one_col || count % 2 == 0 ? 2 : 3);
+ xfprintf(fp, "%*s%-*s",
+ csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
+ row_name(csa, aij->row == NULL ? 0 : aij->row->i));
+ xfprintf(fp, "%*s%*s",
+ csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
+ mps_numb(csa, aij->val)), count++;
+ if (one_col || count % 2 == 0)
+ xfprintf(fp, "\n"), recno++;
+ }
+ if (!(one_col || count % 2 == 0))
+ xfprintf(fp, "\n"), recno++;
+ }
+ if (marker % 2 == 1)
+ { /* close last integer block */
+ marker++;
+ xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTEND'\n",
+ csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
+ csa->deck ? 17 : 1, ""), recno++;
+ }
+#if 1
+ if (empty > 0)
+ xprintf("Warning: problem has %d empty column(s)\n", empty);
+#endif
+ /* write RHS section */
+ xfprintf(fp, "RHS\n"), recno++;
+ count = 0;
+ for (i = (out_obj ? 0 : 1); i <= P->m; i++)
+ { int type;
+ double rhs;
+ if (i == 0)
+ rhs = P->c0;
+ else
+ { type = P->row[i]->type;
+ if (type == GLP_FR)
+ rhs = 0.0;
+ else if (type == GLP_LO)
+ rhs = P->row[i]->lb;
+ else if (type == GLP_UP)
+ rhs = P->row[i]->ub;
+ else if (type == GLP_DB || type == GLP_FX)
+ rhs = P->row[i]->lb;
+ else
+ xassert(type != type);
+ }
+ if (rhs != 0.0)
+ { if (one_col || count % 2 == 0)
+ xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
+ csa->deck ? 8 : 1, "RHS1");
+ gap = (one_col || count % 2 == 0 ? 2 : 3);
+ xfprintf(fp, "%*s%-*s",
+ csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
+ row_name(csa, i));
+ xfprintf(fp, "%*s%*s",
+ csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
+ mps_numb(csa, rhs)), count++;
+ if (one_col || count % 2 == 0)
+ xfprintf(fp, "\n"), recno++;
+ }
+ }
+ if (!(one_col || count % 2 == 0))
+ xfprintf(fp, "\n"), recno++;
+ /* write RANGES section */
+ for (i = P->m; i >= 1; i--)
+ if (P->row[i]->type == GLP_DB) break;
+ if (i == 0) goto bnds;
+ xfprintf(fp, "RANGES\n"), recno++;
+ count = 0;
+ for (i = 1; i <= P->m; i++)
+ { if (P->row[i]->type == GLP_DB)
+ { if (one_col || count % 2 == 0)
+ xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
+ csa->deck ? 8 : 1, "RNG1");
+ gap = (one_col || count % 2 == 0 ? 2 : 3);
+ xfprintf(fp, "%*s%-*s",
+ csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
+ row_name(csa, i));
+ xfprintf(fp, "%*s%*s",
+ csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
+ mps_numb(csa, P->row[i]->ub - P->row[i]->lb)), count++;
+ if (one_col || count % 2 == 0)
+ xfprintf(fp, "\n"), recno++;
+ }
+ }
+ if (!(one_col || count % 2 == 0))
+ xfprintf(fp, "\n"), recno++;
+bnds: /* write BOUNDS section */
+ for (j = P->n; j >= 1; j--)
+ if (!(P->col[j]->kind == GLP_CV &&
+ P->col[j]->type == GLP_LO && P->col[j]->lb == 0.0))
+ break;
+ if (j == 0) goto endt;
+ xfprintf(fp, "BOUNDS\n"), recno++;
+ for (j = 1; j <= P->n; j++)
+ { int type, data[2];
+ double bnd[2];
+ char *spec[2];
+ spec[0] = spec[1] = NULL;
+ type = P->col[j]->type;
+ if (type == GLP_FR)
+ spec[0] = "FR", data[0] = 0;
+ else if (type == GLP_LO)
+ { if (P->col[j]->lb != 0.0)
+ spec[0] = "LO", data[0] = 1, bnd[0] = P->col[j]->lb;
+ if (P->col[j]->kind == GLP_IV)
+ spec[1] = "PL", data[1] = 0;
+ }
+ else if (type == GLP_UP)
+ { spec[0] = "MI", data[0] = 0;
+ spec[1] = "UP", data[1] = 1, bnd[1] = P->col[j]->ub;
+ }
+ else if (type == GLP_DB)
+ { if (P->col[j]->lb != 0.0)
+ spec[0] = "LO", data[0] = 1, bnd[0] = P->col[j]->lb;
+ spec[1] = "UP", data[1] = 1, bnd[1] = P->col[j]->ub;
+ }
+ else if (type == GLP_FX)
+ spec[0] = "FX", data[0] = 1, bnd[0] = P->col[j]->lb;
+ else
+ xassert(type != type);
+ for (i = 0; i <= 1; i++)
+ { if (spec[i] != NULL)
+ { xfprintf(fp, " %s %-*s%*s%-*s", spec[i],
+ csa->deck ? 8 : 1, "BND1", csa->deck ? 2 : 1, "",
+ csa->deck ? 8 : 1, col_name(csa, j));
+ if (data[i])
+ xfprintf(fp, "%*s%*s", csa->deck ? 2 : 1, "",
+ csa->deck ? 12 : 1, mps_numb(csa, bnd[i]));
+ xfprintf(fp, "\n"), recno++;
+ }
+ }
+ }
+endt: /* write ENDATA indicator record */
+ xfprintf(fp, "ENDATA\n"), recno++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* problem data has been successfully written */
+ xprintf("%d records were written\n", recno);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/netgen.c b/test/monniaux/glpk-4.65/src/api/netgen.c
new file mode 100644
index 00000000..519fd609
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/netgen.c
@@ -0,0 +1,1020 @@
+/* netgen.c (Klingman's network problem generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is the result of translation of the Fortran program NETGEN
+* developed by Dr. Darwin Klingman, which is publically available from
+* NETLIB at <http://www.netlib.org/lp/generators>.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_netgen - Klingman's network problem generator
+*
+* SYNOPSIS
+*
+* int glp_netgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
+* const int parm[1+15]);
+*
+* DESCRIPTION
+*
+* The routine glp_netgen is a network problem generator developed by
+* Dr. Darwin Klingman. It can create capacitated and uncapacitated
+* minimum cost flow (or transshipment), transportation, and assignment
+* problems.
+*
+* The parameter G specifies the graph object, to which the generated
+* problem data have to be stored. Note that on entry the graph object
+* is erased with the routine glp_erase_graph.
+*
+* The parameter v_rhs specifies an offset of the field of type double
+* in the vertex data block, to which the routine stores the supply or
+* demand value. If v_rhs < 0, the value is not stored.
+*
+* The parameter a_cap specifies an offset of the field of type double
+* in the arc data block, to which the routine stores the arc capacity.
+* If a_cap < 0, the capacity is not stored.
+*
+* The parameter a_cost specifies an offset of the field of type double
+* in the arc data block, to which the routine stores the per-unit cost
+* if the arc flow. If a_cost < 0, the cost is not stored.
+*
+* The array parm contains description of the network to be generated:
+*
+* parm[0] not used
+* parm[1] (iseed) 8-digit positive random number seed
+* parm[2] (nprob) 8-digit problem id number
+* parm[3] (nodes) total number of nodes
+* parm[4] (nsorc) total number of source nodes (including
+* transshipment nodes)
+* parm[5] (nsink) total number of sink nodes (including
+* transshipment nodes)
+* parm[6] (iarcs) number of arcs
+* parm[7] (mincst) minimum cost for arcs
+* parm[8] (maxcst) maximum cost for arcs
+* parm[9] (itsup) total supply
+* parm[10] (ntsorc) number of transshipment source nodes
+* parm[11] (ntsink) number of transshipment sink nodes
+* parm[12] (iphic) percentage of skeleton arcs to be given
+* the maximum cost
+* parm[13] (ipcap) percentage of arcs to be capacitated
+* parm[14] (mincap) minimum upper bound for capacitated arcs
+* parm[15] (maxcap) maximum upper bound for capacitated arcs
+*
+* The routine generates a transportation problem if:
+*
+* nsorc + nsink = nodes, ntsorc = 0, and ntsink = 0.
+*
+* The routine generates an assignment problem if the requirements for
+* a transportation problem are met and:
+*
+* nsorc = nsink and itsup = nsorc.
+*
+* RETURNS
+*
+* If the instance was successfully generated, the routine glp_netgen
+* returns zero; otherwise, if specified parameters are inconsistent,
+* the routine returns a non-zero error code.
+*
+* REFERENCES
+*
+* D.Klingman, A.Napier, and J.Stutz. NETGEN: A program for generating
+* large scale capacitated assignment, transportation, and minimum cost
+* flow networks. Management Science 20 (1974), 814-20. */
+
+struct csa
+{ /* common storage area */
+ glp_graph *G;
+ int v_rhs, a_cap, a_cost;
+ int nodes, iarcs, mincst, maxcst, itsup, nsorc, nsink, nonsor,
+ nfsink, narcs, nsort, nftsor, ipcap, mincap, maxcap, ktl,
+ nodlft, *ipred, *ihead, *itail, *iflag, *isup, *lsinks, mult,
+ modul, i15, i16, jran;
+};
+
+#define G (csa->G)
+#define v_rhs (csa->v_rhs)
+#define a_cap (csa->a_cap)
+#define a_cost (csa->a_cost)
+#define nodes (csa->nodes)
+#define iarcs (csa->iarcs)
+#define mincst (csa->mincst)
+#define maxcst (csa->maxcst)
+#define itsup (csa->itsup)
+#define nsorc (csa->nsorc)
+#define nsink (csa->nsink)
+#define nonsor (csa->nonsor)
+#define nfsink (csa->nfsink)
+#define narcs (csa->narcs)
+#define nsort (csa->nsort)
+#define nftsor (csa->nftsor)
+#define ipcap (csa->ipcap)
+#define mincap (csa->mincap)
+#define maxcap (csa->maxcap)
+#define ktl (csa->ktl)
+#define nodlft (csa->nodlft)
+#if 0
+/* spent a day to find out this bug */
+#define ist (csa->ist)
+#else
+#define ist (ipred[0])
+#endif
+#define ipred (csa->ipred)
+#define ihead (csa->ihead)
+#define itail (csa->itail)
+#define iflag (csa->iflag)
+#define isup (csa->isup)
+#define lsinks (csa->lsinks)
+#define mult (csa->mult)
+#define modul (csa->modul)
+#define i15 (csa->i15)
+#define i16 (csa->i16)
+#define jran (csa->jran)
+
+static void cresup(struct csa *csa);
+static void chain(struct csa *csa, int lpick, int lsorc);
+static void chnarc(struct csa *csa, int lsorc);
+static void sort(struct csa *csa);
+static void pickj(struct csa *csa, int it);
+static void assign(struct csa *csa);
+static void setran(struct csa *csa, int iseed);
+static int iran(struct csa *csa, int ilow, int ihigh);
+
+int glp_netgen(glp_graph *G_, int _v_rhs, int _a_cap, int _a_cost,
+ const int parm[1+15])
+{ struct csa _csa, *csa = &_csa;
+ int iseed, nprob, ntsorc, ntsink, iphic, i, nskel, nltr, ltsink,
+ ntrans, npsink, nftr, npsorc, ntravl, ntrrem, lsorc, lpick,
+ nsksr, nsrchn, j, item, l, ks, k, ksp, li, n, ii, it, ih, icap,
+ jcap, icost, jcost, ret;
+ G = G_;
+ v_rhs = _v_rhs;
+ a_cap = _a_cap;
+ a_cost = _a_cost;
+ if (G != NULL)
+ { if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_netgen: v_rhs = %d; invalid offset\n", v_rhs);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_netgen: a_cap = %d; invalid offset\n", a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_netgen: a_cost = %d; invalid offset\n", a_cost);
+ }
+ /* Input the user's random number seed and fix it if
+ non-positive. */
+ iseed = parm[1];
+ nprob = parm[2];
+ if (iseed <= 0) iseed = 13502460;
+ setran(csa, iseed);
+ /* Input the user's problem characteristics. */
+ nodes = parm[3];
+ nsorc = parm[4];
+ nsink = parm[5];
+ iarcs = parm[6];
+ mincst = parm[7];
+ maxcst = parm[8];
+ itsup = parm[9];
+ ntsorc = parm[10];
+ ntsink = parm[11];
+ iphic = parm[12];
+ ipcap = parm[13];
+ mincap = parm[14];
+ maxcap = parm[15];
+ /* Check the size of the problem. */
+ if (!(10 <= nodes && nodes <= 100000))
+ { ret = 1;
+ goto done;
+ }
+ /* Check user supplied parameters for consistency. */
+ if (!(nsorc >= 0 && nsink >= 0 && nsorc + nsink <= nodes))
+ { ret = 2;
+ goto done;
+ }
+ if (iarcs < 0)
+ { ret = 3;
+ goto done;
+ }
+ if (mincst > maxcst)
+ { ret = 4;
+ goto done;
+ }
+ if (itsup < 0)
+ { ret = 5;
+ goto done;
+ }
+ if (!(0 <= ntsorc && ntsorc <= nsorc))
+ { ret = 6;
+ goto done;
+ }
+ if (!(0 <= ntsink && ntsink <= nsink))
+ { ret = 7;
+ goto done;
+ }
+ if (!(0 <= iphic && iphic <= 100))
+ { ret = 8;
+ goto done;
+ }
+ if (!(0 <= ipcap && ipcap <= 100))
+ { ret = 9;
+ goto done;
+ }
+ if (mincap > maxcap)
+ { ret = 10;
+ goto done;
+ }
+ /* Initailize the graph object. */
+ if (G != NULL)
+ { glp_erase_graph(G, G->v_size, G->a_size);
+ glp_add_vertices(G, nodes);
+ if (v_rhs >= 0)
+ { double zero = 0.0;
+ for (i = 1; i <= nodes; i++)
+ { glp_vertex *v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &zero, sizeof(double));
+ }
+ }
+ }
+ /* Allocate working arrays. */
+ ipred = xcalloc(1+nodes, sizeof(int));
+ ihead = xcalloc(1+nodes, sizeof(int));
+ itail = xcalloc(1+nodes, sizeof(int));
+ iflag = xcalloc(1+nodes, sizeof(int));
+ isup = xcalloc(1+nodes, sizeof(int));
+ lsinks = xcalloc(1+nodes, sizeof(int));
+ /* Print the problem documentation records. */
+ if (G == NULL)
+ { xprintf("BEGIN\n");
+ xprintf("NETGEN PROBLEM%8d%10s%10d NODES AND%10d ARCS\n",
+ nprob, "", nodes, iarcs);
+ xprintf("USER:%11d%11d%11d%11d%11d%11d\nDATA:%11d%11d%11d%11d%"
+ "11d%11d\n", iseed, nsorc, nsink, mincst,
+ maxcst, itsup, ntsorc, ntsink, iphic, ipcap,
+ mincap, maxcap);
+ }
+ else
+ glp_set_graph_name(G, "NETGEN");
+ /* Set various constants used in the program. */
+ narcs = 0;
+ nskel = 0;
+ nltr = nodes - nsink;
+ ltsink = nltr + ntsink;
+ ntrans = nltr - nsorc;
+ nfsink = nltr + 1;
+ nonsor = nodes - nsorc + ntsorc;
+ npsink = nsink - ntsink;
+ nodlft = nodes - nsink + ntsink;
+ nftr = nsorc + 1;
+ nftsor = nsorc - ntsorc + 1;
+ npsorc = nsorc - ntsorc;
+ /* Randomly distribute the supply among the source nodes. */
+ if (npsorc + npsink == nodes && npsorc == npsink &&
+ itsup == nsorc)
+ { assign(csa);
+ nskel = nsorc;
+ goto L390;
+ }
+ cresup(csa);
+ /* Print the supply records. */
+ if (G == NULL)
+ { xprintf("SUPPLY\n");
+ for (i = 1; i <= nsorc; i++)
+ xprintf("%6s%6d%18s%10d\n", "", i, "", isup[i]);
+ xprintf("ARCS\n");
+ }
+ else
+ { if (v_rhs >= 0)
+ { for (i = 1; i <= nsorc; i++)
+ { double temp = (double)isup[i];
+ glp_vertex *v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
+ }
+ }
+ }
+ /* Make the sources point to themselves in ipred array. */
+ for (i = 1; i <= nsorc; i++)
+ ipred[i] = i;
+ if (ntrans == 0) goto L170;
+ /* Chain the transshipment nodes together in the ipred array. */
+ ist = nftr;
+ ipred[nltr] = 0;
+ for (i = nftr; i < nltr; i++)
+ ipred[i] = i+1;
+ /* Form even length chains for 60 percent of the transshipments.*/
+ ntravl = 6 * ntrans / 10;
+ ntrrem = ntrans - ntravl;
+L140: lsorc = 1;
+ while (ntravl != 0)
+ { lpick = iran(csa, 1, ntravl + ntrrem);
+ ntravl--;
+ chain(csa, lpick, lsorc);
+ if (lsorc == nsorc) goto L140;
+ lsorc++;
+ }
+ /* Add the remaining transshipments to the chains. */
+ while (ntrrem != 0)
+ {
+ lpick = iran(csa, 1, ntrrem);
+ ntrrem--;
+ lsorc = iran(csa, 1, nsorc);
+ chain(csa, lpick, lsorc);
+ }
+L170: /* Set all demands equal to zero. */
+ for (i = nfsink; i <= nodes; i++)
+ ipred[i] = 0;
+ /* The following loop takes one chain at a time (through the use
+ of logic contained in the loop and calls to other routines) and
+ creates the remaining network arcs. */
+ for (lsorc = 1; lsorc <= nsorc; lsorc++)
+ { chnarc(csa, lsorc);
+ for (i = nfsink; i <= nodes; i++)
+ iflag[i] = 0;
+ /* Choose the number of sinks to be hooked up to the current
+ chain. */
+ if (ntrans != 0)
+ nsksr = (nsort * 2 * nsink) / ntrans;
+ else
+ nsksr = nsink / nsorc + 1;
+ if (nsksr < 2) nsksr = 2;
+ if (nsksr > nsink) nsksr = nsink;
+ nsrchn = nsort;
+ /* Randomly pick nsksr sinks and put their names in lsinks. */
+ ktl = nsink;
+ for (j = 1; j <= nsksr; j++)
+ { item = iran(csa, 1, ktl);
+ ktl--;
+ for (l = nfsink; l <= nodes; l++)
+ { if (iflag[l] != 1)
+ { item--;
+ if (item == 0) goto L230;
+ }
+ }
+ break;
+L230: lsinks[j] = l;
+ iflag[l] = 1;
+ }
+ /* If last source chain, add all sinks with zero demand to
+ lsinks list. */
+ if (lsorc == nsorc)
+ { for (j = nfsink; j <= nodes; j++)
+ { if (ipred[j] == 0 && iflag[j] != 1)
+ { nsksr++;
+ lsinks[nsksr] = j;
+ iflag[j] = 1;
+ }
+ }
+ }
+ /* Create demands for group of sinks in lsinks. */
+ ks = isup[lsorc] / nsksr;
+ k = ipred[lsorc];
+ for (i = 1; i <= nsksr; i++)
+ { nsort++;
+ ksp = iran(csa, 1, ks);
+ j = iran(csa, 1, nsksr);
+ itail[nsort] = k;
+ li = lsinks[i];
+ ihead[nsort] = li;
+ ipred[li] += ksp;
+ li = lsinks[j];
+ ipred[li] += ks - ksp;
+ n = iran(csa, 1, nsrchn);
+ k = lsorc;
+ for (ii = 1; ii <= n; ii++)
+ k = ipred[k];
+ }
+ li = lsinks[1];
+ ipred[li] += isup[lsorc] - ks * nsksr;
+ nskel += nsort;
+ /* Sort the arcs in the chain from source lsorc using itail as
+ sort key. */
+ sort(csa);
+ /* Print this part of skeleton and create the arcs for these
+ nodes. */
+ i = 1;
+ itail[nsort+1] = 0;
+L300: for (j = nftsor; j <= nodes; j++)
+ iflag[j] = 0;
+ ktl = nonsor - 1;
+ it = itail[i];
+ iflag[it] = 1;
+L320: ih = ihead[i];
+ iflag[ih] = 1;
+ narcs++;
+ ktl--;
+ /* Determine if this skeleton arc should be capacitated. */
+ icap = itsup;
+ jcap = iran(csa, 1, 100);
+ if (jcap <= ipcap)
+ { icap = isup[lsorc];
+ if (mincap > icap) icap = mincap;
+ }
+ /* Determine if this skeleton arc should have the maximum
+ cost. */
+ icost = maxcst;
+ jcost = iran(csa, 1, 100);
+ if (jcost > iphic)
+ icost = iran(csa, mincst, maxcst);
+ if (G == NULL)
+ xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, ih, "", icost,
+ icap);
+ else
+ { glp_arc *a = glp_add_arc(G, it, ih);
+ if (a_cap >= 0)
+ { double temp = (double)icap;
+ memcpy((char *)a->data + a_cap, &temp, sizeof(double));
+ }
+ if (a_cost >= 0)
+ { double temp = (double)icost;
+ memcpy((char *)a->data + a_cost, &temp, sizeof(double));
+ }
+ }
+ i++;
+ if (itail[i] == it) goto L320;
+ pickj(csa, it);
+ if (i <= nsort) goto L300;
+ }
+ /* Create arcs from the transshipment sinks. */
+ if (ntsink != 0)
+ { for (i = nfsink; i <= ltsink; i++)
+ { for (j = nftsor; j <= nodes; j++)
+ iflag[j] = 0;
+ ktl = nonsor - 1;
+ iflag[i] = 1;
+ pickj(csa, i);
+ }
+ }
+L390: /* Print the demand records and end record. */
+ if (G == NULL)
+ { xprintf("DEMAND\n");
+ for (i = nfsink; i <= nodes; i++)
+ xprintf("%6s%6d%18s%10d\n", "", i, "", ipred[i]);
+ xprintf("END\n");
+ }
+ else
+ { if (v_rhs >= 0)
+ { for (i = nfsink; i <= nodes; i++)
+ { double temp = - (double)ipred[i];
+ glp_vertex *v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
+ }
+ }
+ }
+ /* Free working arrays. */
+ xfree(ipred);
+ xfree(ihead);
+ xfree(itail);
+ xfree(iflag);
+ xfree(isup);
+ xfree(lsinks);
+ /* The instance has been successfully generated. */
+ ret = 0;
+done: return ret;
+}
+
+/***********************************************************************
+* The routine cresup randomly distributes the total supply among the
+* source nodes. */
+
+static void cresup(struct csa *csa)
+{ int i, j, ks, ksp;
+ xassert(itsup > nsorc);
+ ks = itsup / nsorc;
+ for (i = 1; i <= nsorc; i++)
+ isup[i] = 0;
+ for (i = 1; i <= nsorc; i++)
+ { ksp = iran(csa, 1, ks);
+ j = iran(csa, 1, nsorc);
+ isup[i] += ksp;
+ isup[j] += ks - ksp;
+ }
+ j = iran(csa, 1, nsorc);
+ isup[j] += itsup - ks * nsorc;
+ return;
+}
+
+/***********************************************************************
+* The routine chain adds node lpick to the end of the chain with source
+* node lsorc. */
+
+static void chain(struct csa *csa, int lpick, int lsorc)
+{ int i, j, k, l, m;
+ k = 0;
+ m = ist;
+ for (i = 1; i <= lpick; i++)
+ { l = k;
+ k = m;
+ m = ipred[k];
+ }
+ ipred[l] = m;
+ j = ipred[lsorc];
+ ipred[k] = j;
+ ipred[lsorc] = k;
+ return;
+}
+
+/***********************************************************************
+* The routine chnarc puts the arcs in the chain from source lsorc into
+* the ihead and itail arrays for sorting. */
+
+static void chnarc(struct csa *csa, int lsorc)
+{ int ito, ifrom;
+ nsort = 0;
+ ito = ipred[lsorc];
+L10: if (ito == lsorc) return;
+ nsort++;
+ ifrom = ipred[ito];
+ ihead[nsort] = ito;
+ itail[nsort] = ifrom;
+ ito = ifrom;
+ goto L10;
+}
+
+/***********************************************************************
+* The routine sort sorts the nsort arcs in the ihead and itail arrays.
+* ihead is used as the sort key (i.e. forward star sort order). */
+
+static void sort(struct csa *csa)
+{ int i, j, k, l, m, n, it;
+ n = nsort;
+ m = n;
+L10: m /= 2;
+ if (m == 0) return;
+ k = n - m;
+ j = 1;
+L20: i = j;
+L30: l = i + m;
+ if (itail[i] <= itail[l]) goto L40;
+ it = itail[i];
+ itail[i] = itail[l];
+ itail[l] = it;
+ it = ihead[i];
+ ihead[i] = ihead[l];
+ ihead[l] = it;
+ i -= m;
+ if (i >= 1) goto L30;
+L40: j++;
+ if (j <= k) goto L20;
+ goto L10;
+}
+
+/***********************************************************************
+* The routine pickj creates a random number of arcs out of node 'it'.
+* Various parameters are dynamically adjusted in an attempt to ensure
+* that the generated network has the correct number of arcs. */
+
+static void pickj(struct csa *csa, int it)
+{ int j, k, l, nn, nupbnd, icap, jcap, icost;
+ if ((nodlft - 1) * 2 > iarcs - narcs - 1)
+ { nodlft--;
+ return;
+ }
+ if ((iarcs - narcs + nonsor - ktl - 1) / nodlft - nonsor + 1 >= 0)
+ k = nonsor;
+ else
+ { nupbnd = (iarcs - narcs - nodlft) / nodlft * 2;
+L40: k = iran(csa, 1, nupbnd);
+ if (nodlft == 1) k = iarcs - narcs;
+ if ((nodlft - 1) * (nonsor - 1) < iarcs - narcs - k) goto L40;
+ }
+ nodlft--;
+ for (j = 1; j <= k; j++)
+ { nn = iran(csa, 1, ktl);
+ ktl--;
+ for (l = nftsor; l <= nodes; l++)
+ { if (iflag[l] != 1)
+ { nn--;
+ if (nn == 0) goto L70;
+ }
+ }
+ return;
+L70: iflag[l] = 1;
+ icap = itsup;
+ jcap = iran(csa, 1, 100);
+ if (jcap <= ipcap)
+ icap = iran(csa, mincap, maxcap);
+ icost = iran(csa, mincst, maxcst);
+ if (G == NULL)
+ xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, l, "", icost,
+ icap);
+ else
+ { glp_arc *a = glp_add_arc(G, it, l);
+ if (a_cap >= 0)
+ { double temp = (double)icap;
+ memcpy((char *)a->data + a_cap, &temp, sizeof(double));
+ }
+ if (a_cost >= 0)
+ { double temp = (double)icost;
+ memcpy((char *)a->data + a_cost, &temp, sizeof(double));
+ }
+ }
+ narcs++;
+ }
+ return;
+}
+
+/***********************************************************************
+* The routine assign generate assignment problems. It defines the unit
+* supplies, builds a skeleton, then calls pickj to create the arcs. */
+
+static void assign(struct csa *csa)
+{ int i, it, nn, l, ll, icost;
+ if (G == NULL)
+ xprintf("SUPPLY\n");
+ for (i = 1; i <= nsorc; i++)
+ { isup[i] = 1;
+ iflag[i] = 0;
+ if (G == NULL)
+ xprintf("%6s%6d%18s%10d\n", "", i, "", isup[i]);
+ else
+ { if (v_rhs >= 0)
+ { double temp = (double)isup[i];
+ glp_vertex *v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
+ }
+ }
+ }
+ if (G == NULL)
+ xprintf("ARCS\n");
+ for (i = nfsink; i <= nodes; i++)
+ ipred[i] = 1;
+ for (it = 1; it <= nsorc; it++)
+ { for (i = nfsink; i <= nodes; i++)
+ iflag[i] = 0;
+ ktl = nsink - 1;
+ nn = iran(csa, 1, nsink - it + 1);
+ for (l = 1; l <= nsorc; l++)
+ { if (iflag[l] != 1)
+ { nn--;
+ if (nn == 0) break;
+ }
+ }
+ narcs++;
+ ll = nsorc + l;
+ icost = iran(csa, mincst, maxcst);
+ if (G == NULL)
+ xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, ll, "", icost,
+ isup[1]);
+ else
+ { glp_arc *a = glp_add_arc(G, it, ll);
+ if (a_cap >= 0)
+ { double temp = (double)isup[1];
+ memcpy((char *)a->data + a_cap, &temp, sizeof(double));
+ }
+ if (a_cost >= 0)
+ { double temp = (double)icost;
+ memcpy((char *)a->data + a_cost, &temp, sizeof(double));
+ }
+ }
+ iflag[l] = 1;
+ iflag[ll] = 1;
+ pickj(csa, it);
+ }
+ return;
+}
+
+/***********************************************************************
+* Portable congruential (uniform) random number generator:
+*
+* next_value = ((7**5) * previous_value) modulo ((2**31)-1)
+*
+* This generator consists of three routines:
+*
+* (1) setran - initializes constants and seed
+* (2) iran - generates an integer random number
+* (3) rran - generates a real random number
+*
+* The generator requires a machine with at least 32 bits of precision.
+* The seed (iseed) must be in the range [1,(2**31)-1]. */
+
+static void setran(struct csa *csa, int iseed)
+{ xassert(iseed >= 1);
+ mult = 16807;
+ modul = 2147483647;
+ i15 = 1 << 15;
+ i16 = 1 << 16;
+ jran = iseed;
+ return;
+}
+
+/***********************************************************************
+* The routine iran generates an integer random number between ilow and
+* ihigh. If ilow > ihigh then iran returns ihigh. */
+
+static int iran(struct csa *csa, int ilow, int ihigh)
+{ int ixhi, ixlo, ixalo, leftlo, ixahi, ifulhi, irtlo, iover,
+ irthi, j;
+ ixhi = jran / i16;
+ ixlo = jran - ixhi * i16;
+ ixalo = ixlo * mult;
+ leftlo = ixalo / i16;
+ ixahi = ixhi * mult;
+ ifulhi = ixahi + leftlo;
+ irtlo = ixalo - leftlo * i16;
+ iover = ifulhi / i15;
+ irthi = ifulhi - iover * i15;
+ jran = ((irtlo - modul) + irthi * i16) + iover;
+ if (jran < 0) jran += modul;
+ j = ihigh - ilow + 1;
+ if (j > 0)
+ return jran % j + ilow;
+ else
+ return ihigh;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_netgen_prob - Klingman's standard network problem instance
+*
+* SYNOPSIS
+*
+* void glp_netgen_prob(int nprob, int parm[1+15]);
+*
+* DESCRIPTION
+*
+* The routine glp_netgen_prob provides the set of parameters for
+* Klingman's network problem generator (see the routine glp_netgen),
+* which describe a standard network problem instance.
+*
+* The parameter nprob (101 <= nprob <= 150) specifies the problem
+* instance number.
+*
+* The array parm contains description of the network, provided by the
+* routine. (For detailed description of these parameters see comments
+* to the routine glp_netgen.)
+*
+* PROBLEM CHARACTERISTICS
+*
+* The table below shows characteristics of Klingman's standard network
+* problem instances.
+*
+* Problem Nodes Arcs Optimum
+* ------- ----- ----- ----------
+* 101 5000 25336 6191726
+* 102 5000 25387 72337144
+* 103 5000 25355 218947553
+* 104 5000 25344 -19100371
+* 105 5000 25332 31192578
+* 106 5000 12870 4314276
+* 107 5000 37832 7393769
+* 108 5000 50309 8405738
+* 109 5000 75299 9190300
+* 110 5000 12825 8975048
+* 111 5000 37828 4747532
+* 112 5000 50325 4012671
+* 113 5000 75318 2979725
+* 114 5000 26514 5821181
+* 115 5000 25962 6353310
+* 116 5000 25304 5915426
+* 117 5000 12816 4420560
+* 118 5000 37797 7045842
+* 119 5000 50301 7724179
+* 120 5000 75330 8455200
+* 121 5000 25000 66366360
+* 122 5000 25000 30997529
+* 123 5000 25000 23388777
+* 124 5000 25000 17803443
+* 125 5000 25000 14119622
+* 126 5000 12500 18802218
+* 127 5000 37500 27674647
+* 128 5000 50000 30906194
+* 129 5000 75000 40905209
+* 130 5000 12500 38939608
+* 131 5000 37500 16752978
+* 132 5000 50000 13302951
+* 133 5000 75000 9830268
+* 134 1000 25000 3804874
+* 135 2500 25000 11729616
+* 136 7500 25000 33318101
+* 137 10000 25000 46426030
+* 138 5000 25000 60710879
+* 139 5000 25000 32729682
+* 140 5000 25000 27183831
+* 141 5000 25000 19963286
+* 142 5000 25000 20243457
+* 143 5000 25000 18586777
+* 144 5000 25000 2504591
+* 145 5000 25000 215956138
+* 146 5000 25000 2253113811
+* 147 5000 25000 -427908373
+* 148 5000 25000 -92965318
+* 149 5000 25000 86051224
+* 150 5000 25000 619314919 */
+
+static const int data[50][1+15] =
+{ { 0, 13502460, 101, 5000, 2500, 2500, 25000,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 4281922, 102, 5000, 2500, 2500, 25000,
+ 1, 100, 2500000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 44820113, 103, 5000, 2500, 2500, 25000,
+ 1, 100, 6250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 13450451, 104, 5000, 2500, 2500, 25000,
+ -100, -1, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 14719436, 105, 5000, 2500, 2500, 25000,
+ 101, 200, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 17365786, 106, 5000, 2500, 2500, 12500,
+ 1, 100, 125000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 19540113, 107, 5000, 2500, 2500, 37500,
+ 1, 100, 375000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 19560313, 108, 5000, 2500, 2500, 50000,
+ 1, 100, 500000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 2403509, 109, 5000, 2500, 2500, 75000,
+ 1, 100, 750000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 92480414, 110, 5000, 2500, 2500, 12500,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 4230140, 111, 5000, 2500, 2500, 37500,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 10032490, 112, 5000, 2500, 2500, 50000,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 17307474, 113, 5000, 2500, 2500, 75000,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 4925114, 114, 5000, 500, 4500, 25000,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 19842704, 115, 5000, 1500, 3500, 25000,
+ 1, 100, 250000, 0, 0, 0, 100, 1, 1000
+ },
+ { 0, 88392060, 116, 5000, 2500, 2500, 25000,
+ 1, 100, 250000, 0, 0, 0, 0, 1, 1000
+ },
+ { 0, 12904407, 117, 5000, 2500, 2500, 12500,
+ 1, 100, 125000, 0, 0, 0, 0, 1, 1000
+ },
+ { 0, 11811811, 118, 5000, 2500, 2500, 37500,
+ 1, 100, 375000, 0, 0, 0, 0, 1, 1000
+ },
+ { 0, 90023593, 119, 5000, 2500, 2500, 50000,
+ 1, 100, 500000, 0, 0, 0, 0, 1, 1000
+ },
+ { 0, 93028922, 120, 5000, 2500, 2500, 75000,
+ 1, 100, 750000, 0, 0, 0, 0, 1, 1000
+ },
+ { 0, 72707401, 121, 5000, 50, 50, 25000,
+ 1, 100, 250000, 50, 50, 0, 100, 1, 1000
+ },
+ { 0, 93040771, 122, 5000, 250, 250, 25000,
+ 1, 100, 250000, 250, 250, 0, 100, 1, 1000
+ },
+ { 0, 70220611, 123, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 52774811, 124, 5000, 1000, 1000, 25000,
+ 1, 100, 250000, 1000, 1000, 0, 100, 1, 1000
+ },
+ { 0, 22492311, 125, 5000, 1500, 1500, 25000,
+ 1, 100, 250000, 1500, 1500, 0, 100, 1, 1000
+ },
+ { 0, 35269337, 126, 5000, 500, 500, 12500,
+ 1, 100, 125000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 30140502, 127, 5000, 500, 500, 37500,
+ 1, 100, 375000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 49205455, 128, 5000, 500, 500, 50000,
+ 1, 100, 500000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 42958341, 129, 5000, 500, 500, 75000,
+ 1, 100, 750000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 25440925, 130, 5000, 500, 500, 12500,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 75294924, 131, 5000, 500, 500, 37500,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 4463965, 132, 5000, 500, 500, 50000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 13390427, 133, 5000, 500, 500, 75000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 95250971, 134, 1000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 54830522, 135, 2500, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 520593, 136, 7500, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 52900925, 137, 10000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 22603395, 138, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 50
+ },
+ { 0, 55253099, 139, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 250
+ },
+ { 0, 75357001, 140, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 500
+ },
+ { 0, 10072459, 141, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 2500
+ },
+ { 0, 55728492, 142, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 100, 1, 5000
+ },
+ { 0, 593043, 143, 5000, 500, 500, 25000,
+ 1, 100, 250000, 500, 500, 0, 0, 1, 1000
+ },
+ { 0, 94236572, 144, 5000, 500, 500, 25000,
+ 1, 10, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 94882955, 145, 5000, 500, 500, 25000,
+ 1, 1000, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 48489922, 146, 5000, 500, 500, 25000,
+ 1, 10000, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 75578374, 147, 5000, 500, 500, 25000,
+ -100, -1, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 44821152, 148, 5000, 500, 500, 25000,
+ -50, 49, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 45224103, 149, 5000, 500, 500, 25000,
+ 101, 200, 250000, 500, 500, 0, 100, 1, 1000
+ },
+ { 0, 63491741, 150, 5000, 500, 500, 25000,
+ 1001, 1100, 250000, 500, 500, 0, 100, 1, 1000
+ },
+};
+
+void glp_netgen_prob(int nprob, int parm[1+15])
+{ int k;
+ if (!(101 <= nprob && nprob <= 150))
+ xerror("glp_netgen_prob: nprob = %d; invalid problem instance "
+ "number\n", nprob);
+ for (k = 1; k <= 15; k++)
+ parm[k] = data[nprob-101][k];
+ return;
+}
+
+/**********************************************************************/
+
+#if 0
+static int scan(char card[80+1], int pos, int len)
+{ char buf[10+1];
+ memcpy(buf, &card[pos-1], len);
+ buf[len] = '\0';
+ return atoi(buf);
+}
+
+int main(void)
+{ int parm[1+15];
+ char card[80+1];
+ xassert(fgets(card, sizeof(card), stdin) == card);
+ parm[1] = scan(card, 1, 8);
+ parm[2] = scan(card, 9, 8);
+ xassert(fgets(card, sizeof(card), stdin) == card);
+ parm[3] = scan(card, 1, 5);
+ parm[4] = scan(card, 6, 5);
+ parm[5] = scan(card, 11, 5);
+ parm[6] = scan(card, 16, 5);
+ parm[7] = scan(card, 21, 5);
+ parm[8] = scan(card, 26, 5);
+ parm[9] = scan(card, 31, 10);
+ parm[10] = scan(card, 41, 5);
+ parm[11] = scan(card, 46, 5);
+ parm[12] = scan(card, 51, 5);
+ parm[13] = scan(card, 56, 5);
+ parm[14] = scan(card, 61, 10);
+ parm[15] = scan(card, 71, 10);
+ glp_netgen(NULL, 0, 0, 0, parm);
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/npp.c b/test/monniaux/glpk-4.65/src/api/npp.c
new file mode 100644
index 00000000..a7ae07c1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/npp.c
@@ -0,0 +1,143 @@
+/* npp.c (LP/MIP preprocessing) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+glp_prep *glp_npp_alloc_wksp(void)
+{ /* allocate the preprocessor workspace */
+ glp_prep *prep;
+ prep = npp_create_wksp();
+ return prep;
+}
+
+void glp_npp_load_prob(glp_prep *prep, glp_prob *P, int sol, int names)
+{ /* load original problem instance */
+ if (prep->sol != 0)
+ xerror("glp_npp_load_prob: invalid call sequence (original ins"
+ "tance already loaded)\n");
+ if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP))
+ xerror("glp_npp_load_prob: sol = %d; invalid parameter\n",
+ sol);
+ if (!(names == GLP_ON || names == GLP_OFF))
+ xerror("glp_npp_load_prob: names = %d; invalid parameter\n",
+ names);
+ npp_load_prob(prep, P, names, sol, GLP_OFF);
+ return;
+}
+
+int glp_npp_preprocess1(glp_prep *prep, int hard)
+{ /* perform basic LP/MIP preprocessing */
+ if (prep->sol == 0)
+ xerror("glp_npp_preprocess1: invalid call sequence (original i"
+ "nstance not loaded yet)\n");
+ if (prep->pool == NULL)
+ xerror("glp_npp_preprocess1: invalid call sequence (preprocess"
+ "ing already finished)\n");
+ if (!(hard == GLP_ON || hard == GLP_OFF))
+ xerror("glp_npp_preprocess1: hard = %d; invalid parameter\n",
+ hard);
+ return npp_process_prob(prep, hard);
+}
+
+void glp_npp_build_prob(glp_prep *prep, glp_prob *Q)
+{ /* build resultant problem instance */
+ if (prep->sol == 0)
+ xerror("glp_npp_build_prob: invalid call sequence (original in"
+ "stance not loaded yet)\n");
+ if (prep->pool == NULL)
+ xerror("glp_npp_build_prob: invalid call sequence (resultant i"
+ "nstance already built)\n");
+ npp_build_prob(prep, Q);
+ return;
+}
+
+void glp_npp_postprocess(glp_prep *prep, glp_prob *Q)
+{ /* postprocess solution to resultant problem */
+ if (prep->pool != NULL)
+ xerror("glp_npp_postprocess: invalid call sequence (resultant "
+ "instance not built yet)\n");
+ if (!(prep->m == Q->m && prep->n == Q->n && prep->nnz == Q->nnz))
+ xerror("glp_npp_postprocess: resultant instance mismatch\n");
+ switch (prep->sol)
+ { case GLP_SOL:
+ if (glp_get_status(Q) != GLP_OPT)
+ xerror("glp_npp_postprocess: unable to recover non-optim"
+ "al basic solution\n");
+ break;
+ case GLP_IPT:
+ if (glp_ipt_status(Q) != GLP_OPT)
+ xerror("glp_npp_postprocess: unable to recover non-optim"
+ "al interior-point solution\n");
+ break;
+ case GLP_MIP:
+ if (!(glp_mip_status(Q) == GLP_OPT || glp_mip_status(Q) ==
+ GLP_FEAS))
+ xerror("glp_npp_postprocess: unable to recover integer n"
+ "on-feasible solution\n");
+ break;
+ default:
+ xassert(prep != prep);
+ }
+ npp_postprocess(prep, Q);
+ return;
+}
+
+void glp_npp_obtain_sol(glp_prep *prep, glp_prob *P)
+{ /* obtain solution to original problem */
+ if (prep->pool != NULL)
+ xerror("glp_npp_obtain_sol: invalid call sequence (resultant i"
+ "nstance not built yet)\n");
+ switch (prep->sol)
+ { case GLP_SOL:
+ if (prep->p_stat == 0 || prep->d_stat == 0)
+ xerror("glp_npp_obtain_sol: invalid call sequence (basic"
+ " solution not provided yet)\n");
+ break;
+ case GLP_IPT:
+ if (prep->t_stat == 0)
+ xerror("glp_npp_obtain_sol: invalid call sequence (inter"
+ "ior-point solution not provided yet)\n");
+ break;
+ case GLP_MIP:
+ if (prep->i_stat == 0)
+ xerror("glp_npp_obtain_sol: invalid call sequence (MIP s"
+ "olution not provided yet)\n");
+ break;
+ default:
+ xassert(prep != prep);
+ }
+ if (!(prep->orig_dir == P->dir && prep->orig_m == P->m &&
+ prep->orig_n == P->n && prep->orig_nnz == P->nnz))
+ xerror("glp_npp_obtain_sol: original instance mismatch\n");
+ npp_unload_sol(prep, P);
+ return;
+}
+
+void glp_npp_free_wksp(glp_prep *prep)
+{ /* free the preprocessor workspace */
+ npp_delete_wksp(prep);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/pript.c b/test/monniaux/glpk-4.65/src/api/pript.c
new file mode 100644
index 00000000..f123089d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/pript.c
@@ -0,0 +1,186 @@
+/* pript.c (write interior-point solution in printable format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+int glp_print_ipt(glp_prob *P, const char *fname)
+{ /* write interior-point solution in printable format */
+ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, t, ae_ind, re_ind, ret;
+ double ae_max, re_max;
+ xprintf("Writing interior-point solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "%-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name);
+ xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
+ xfprintf(fp, "%-12s%d\n", "Columns:", P->n);
+ xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
+ t = glp_ipt_status(P);
+ xfprintf(fp, "%-12s%s\n", "Status:",
+ t == GLP_OPT ? "OPTIMAL" :
+ t == GLP_UNDEF ? "UNDEFINED" :
+ t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
+ t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : "???");
+ xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->ipt_obj,
+ P->dir == GLP_MIN ? "MINimum" :
+ P->dir == GLP_MAX ? "MAXimum" : "???");
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Row name Activity Lower bound "
+ " Upper bound Marginal\n");
+ xfprintf(fp, "------ ------------ ------------- ------------- "
+ "------------- -------------\n");
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ xfprintf(fp, "%6d ", i);
+ if (row->name == NULL || strlen(row->name) <= 12)
+ xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
+ else
+ xfprintf(fp, "%s\n%20s", row->name, "");
+ xfprintf(fp, "%3s", "");
+ xfprintf(fp, "%13.6g ",
+ fabs(row->pval) <= 1e-9 ? 0.0 : row->pval);
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", row->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (row->type == GLP_UP || row->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", row->ub);
+ else
+ xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
+ if (fabs(row->dval) <= 1e-9)
+ xfprintf(fp, "%13s", "< eps");
+ else
+ xfprintf(fp, "%13.6g ", row->dval);
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Column name Activity Lower bound "
+ " Upper bound Marginal\n");
+ xfprintf(fp, "------ ------------ ------------- ------------- "
+ "------------- -------------\n");
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ xfprintf(fp, "%6d ", j);
+ if (col->name == NULL || strlen(col->name) <= 12)
+ xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
+ else
+ xfprintf(fp, "%s\n%20s", col->name, "");
+ xfprintf(fp, "%3s", "");
+ xfprintf(fp, "%13.6g ",
+ fabs(col->pval) <= 1e-9 ? 0.0 : col->pval);
+ if (col->type == GLP_LO || col->type == GLP_DB ||
+ col->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", col->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (col->type == GLP_UP || col->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", col->ub);
+ else
+ xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
+ if (fabs(col->dval) <= 1e-9)
+ xfprintf(fp, "%13s", "< eps");
+ else
+ xfprintf(fp, "%13.6g ", col->dval);
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_IPT, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
+ ae_max, ae_ind);
+ xfprintf(fp, " max.rel.err = %.2e on row %d\n",
+ re_max, re_ind);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_IPT, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
+ ae_max, ae_ind <= P->m ? "row" : "column",
+ ae_ind <= P->m ? ae_ind : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
+ re_max, re_ind <= P->m ? "row" : "column",
+ re_ind <= P->m ? re_ind : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
+ "E");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_IPT, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
+ ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on column %d\n",
+ re_max, re_ind == 0 ? 0 : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_IPT, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
+ ae_max, ae_ind <= P->m ? "row" : "column",
+ ae_ind <= P->m ? ae_ind : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
+ re_max, re_ind <= P->m ? "row" : "column",
+ re_ind <= P->m ? re_ind : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
+ ;
+ xfprintf(fp, "\n");
+ xfprintf(fp, "End of output\n");
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prmip.c b/test/monniaux/glpk-4.65/src/api/prmip.c
new file mode 100644
index 00000000..885ed82a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prmip.c
@@ -0,0 +1,155 @@
+/* prmip.c (write MIP solution in printable format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+int glp_print_mip(glp_prob *P, const char *fname)
+{ /* write MIP solution in printable format */
+ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, t, ae_ind, re_ind, ret;
+ double ae_max, re_max;
+ xprintf("Writing MIP solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "%-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name);
+ xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
+ xfprintf(fp, "%-12s%d (%d integer, %d binary)\n", "Columns:",
+ P->n, glp_get_num_int(P), glp_get_num_bin(P));
+ xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
+ t = glp_mip_status(P);
+ xfprintf(fp, "%-12s%s\n", "Status:",
+ t == GLP_OPT ? "INTEGER OPTIMAL" :
+ t == GLP_FEAS ? "INTEGER NON-OPTIMAL" :
+ t == GLP_NOFEAS ? "INTEGER EMPTY" :
+ t == GLP_UNDEF ? "INTEGER UNDEFINED" : "???");
+ xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->mip_obj,
+ P->dir == GLP_MIN ? "MINimum" :
+ P->dir == GLP_MAX ? "MAXimum" : "???");
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Row name Activity Lower bound "
+ " Upper bound\n");
+ xfprintf(fp, "------ ------------ ------------- ------------- "
+ "-------------\n");
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ xfprintf(fp, "%6d ", i);
+ if (row->name == NULL || strlen(row->name) <= 12)
+ xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
+ else
+ xfprintf(fp, "%s\n%20s", row->name, "");
+ xfprintf(fp, "%3s", "");
+ xfprintf(fp, "%13.6g ",
+ fabs(row->mipx) <= 1e-9 ? 0.0 : row->mipx);
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", row->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (row->type == GLP_UP || row->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", row->ub);
+ else
+ xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Column name Activity Lower bound "
+ " Upper bound\n");
+ xfprintf(fp, "------ ------------ ------------- ------------- "
+ "-------------\n");
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ xfprintf(fp, "%6d ", j);
+ if (col->name == NULL || strlen(col->name) <= 12)
+ xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
+ else
+ xfprintf(fp, "%s\n%20s", col->name, "");
+ xfprintf(fp, "%s ",
+ col->kind == GLP_CV ? " " :
+ col->kind == GLP_IV ? "*" : "?");
+ xfprintf(fp, "%13.6g ",
+ fabs(col->mipx) <= 1e-9 ? 0.0 : col->mipx);
+ if (col->type == GLP_LO || col->type == GLP_DB ||
+ col->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", col->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (col->type == GLP_UP || col->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", col->ub);
+ else
+ xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, "Integer feasibility conditions:\n");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
+ ae_max, ae_ind);
+ xfprintf(fp, " max.rel.err = %.2e on row %d\n",
+ re_max, re_ind);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "SOLUTION IS WRONG");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
+ ae_max, ae_ind <= P->m ? "row" : "column",
+ ae_ind <= P->m ? ae_ind : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
+ re_max, re_ind <= P->m ? "row" : "column",
+ re_ind <= P->m ? re_ind : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "SOLUTION IS INFEASIBLE");
+ xfprintf(fp, "\n");
+ xfprintf(fp, "End of output\n");
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob.h b/test/monniaux/glpk-4.65/src/api/prob.h
new file mode 100644
index 00000000..cc9389b5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob.h
@@ -0,0 +1,286 @@
+/* prob.h (LP/MIP problem object) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef PROB_H
+#define PROB_H
+
+#include "avl.h"
+#include "bfd.h"
+#include "dmp.h"
+#if 1 /* 28/III-2016 */
+#define GLP_UNDOC 1
+#endif
+#include "glpk.h"
+
+typedef struct GLPROW GLPROW;
+typedef struct GLPCOL GLPCOL;
+typedef struct GLPAIJ GLPAIJ;
+
+#if 0 /* 04/IV-2016 */
+#define GLP_PROB_MAGIC 0xD7D9D6C2
+#endif
+
+struct glp_prob
+{ /* LP/MIP problem object */
+#if 0 /* 04/IV-2016 */
+ unsigned magic;
+ /* magic value used for debugging */
+#endif
+ DMP *pool;
+ /* memory pool to store problem object components */
+ glp_tree *tree;
+ /* pointer to the search tree; set by the MIP solver when this
+ object is used in the tree as a core MIP object */
+#if 0 /* 08/III-2014 */
+ void *parms;
+ /* reserved for backward compatibility */
+#endif
+ /*--------------------------------------------------------------*/
+ /* LP/MIP data */
+ char *name;
+ /* problem name (1 to 255 chars); NULL means no name is assigned
+ to the problem */
+ char *obj;
+ /* objective function name (1 to 255 chars); NULL means no name
+ is assigned to the objective function */
+ int dir;
+ /* optimization direction flag (objective "sense"):
+ GLP_MIN - minimization
+ GLP_MAX - maximization */
+ double c0;
+ /* constant term of the objective function ("shift") */
+ int m_max;
+ /* length of the array of rows (enlarged automatically) */
+ int n_max;
+ /* length of the array of columns (enlarged automatically) */
+ int m;
+ /* number of rows, 0 <= m <= m_max */
+ int n;
+ /* number of columns, 0 <= n <= n_max */
+ int nnz;
+ /* number of non-zero constraint coefficients, nnz >= 0 */
+ GLPROW **row; /* GLPROW *row[1+m_max]; */
+ /* row[i], 1 <= i <= m, is a pointer to i-th row */
+ GLPCOL **col; /* GLPCOL *col[1+n_max]; */
+ /* col[j], 1 <= j <= n, is a pointer to j-th column */
+ AVL *r_tree;
+ /* row index to find rows by their names; NULL means this index
+ does not exist */
+ AVL *c_tree;
+ /* column index to find columns by their names; NULL means this
+ index does not exist */
+ /*--------------------------------------------------------------*/
+ /* basis factorization (LP) */
+ int valid;
+ /* the factorization is valid only if this flag is set */
+ int *head; /* int head[1+m_max]; */
+ /* basis header (valid only if the factorization is valid);
+ head[i] = k is the ordinal number of auxiliary (1 <= k <= m)
+ or structural (m+1 <= k <= m+n) variable which corresponds to
+ i-th basic variable xB[i], 1 <= i <= m */
+#if 0 /* 08/III-2014 */
+ glp_bfcp *bfcp;
+ /* basis factorization control parameters; may be NULL */
+#endif
+ BFD *bfd; /* BFD bfd[1:m,1:m]; */
+ /* basis factorization driver; may be NULL */
+ /*--------------------------------------------------------------*/
+ /* basic solution (LP) */
+ int pbs_stat;
+ /* primal basic solution status:
+ GLP_UNDEF - primal solution is undefined
+ GLP_FEAS - primal solution is feasible
+ GLP_INFEAS - primal solution is infeasible
+ GLP_NOFEAS - no primal feasible solution exists */
+ int dbs_stat;
+ /* dual basic solution status:
+ GLP_UNDEF - dual solution is undefined
+ GLP_FEAS - dual solution is feasible
+ GLP_INFEAS - dual solution is infeasible
+ GLP_NOFEAS - no dual feasible solution exists */
+ double obj_val;
+ /* objective function value */
+ int it_cnt;
+ /* simplex method iteration count; increases by one on performing
+ one simplex iteration */
+ int some;
+ /* ordinal number of some auxiliary or structural variable having
+ certain property, 0 <= some <= m+n */
+ /*--------------------------------------------------------------*/
+ /* interior-point solution (LP) */
+ int ipt_stat;
+ /* interior-point solution status:
+ GLP_UNDEF - interior solution is undefined
+ GLP_OPT - interior solution is optimal
+ GLP_INFEAS - interior solution is infeasible
+ GLP_NOFEAS - no feasible solution exists */
+ double ipt_obj;
+ /* objective function value */
+ /*--------------------------------------------------------------*/
+ /* integer solution (MIP) */
+ int mip_stat;
+ /* integer solution status:
+ GLP_UNDEF - integer solution is undefined
+ GLP_OPT - integer solution is optimal
+ GLP_FEAS - integer solution is feasible
+ GLP_NOFEAS - no integer solution exists */
+ double mip_obj;
+ /* objective function value */
+};
+
+struct GLPROW
+{ /* LP/MIP row (auxiliary variable) */
+ int i;
+ /* ordinal number (1 to m) assigned to this row */
+ char *name;
+ /* row name (1 to 255 chars); NULL means no name is assigned to
+ this row */
+ AVLNODE *node;
+ /* pointer to corresponding node in the row index; NULL means
+ that either the row index does not exist or this row has no
+ name assigned */
+#if 1 /* 20/IX-2008 */
+ int level;
+ unsigned char origin;
+ unsigned char klass;
+#endif
+ int type;
+ /* type of the auxiliary variable:
+ GLP_FR - free variable
+ GLP_LO - variable with lower bound
+ GLP_UP - variable with upper bound
+ GLP_DB - double-bounded variable
+ GLP_FX - fixed variable */
+ double lb; /* non-scaled */
+ /* lower bound; if the row has no lower bound, lb is zero */
+ double ub; /* non-scaled */
+ /* upper bound; if the row has no upper bound, ub is zero */
+ /* if the row type is GLP_FX, ub is equal to lb */
+ GLPAIJ *ptr; /* non-scaled */
+ /* pointer to doubly linked list of constraint coefficients which
+ are placed in this row */
+ double rii;
+ /* diagonal element r[i,i] of scaling matrix R for this row;
+ if the scaling is not used, r[i,i] is 1 */
+ int stat;
+ /* status of the auxiliary variable:
+ GLP_BS - basic variable
+ GLP_NL - non-basic variable on lower bound
+ GLP_NU - non-basic variable on upper bound
+ GLP_NF - non-basic free variable
+ GLP_NS - non-basic fixed variable */
+ int bind;
+ /* if the auxiliary variable is basic, head[bind] refers to this
+ row, otherwise, bind is 0; this attribute is valid only if the
+ basis factorization is valid */
+ double prim; /* non-scaled */
+ /* primal value of the auxiliary variable in basic solution */
+ double dual; /* non-scaled */
+ /* dual value of the auxiliary variable in basic solution */
+ double pval; /* non-scaled */
+ /* primal value of the auxiliary variable in interior solution */
+ double dval; /* non-scaled */
+ /* dual value of the auxiliary variable in interior solution */
+ double mipx; /* non-scaled */
+ /* primal value of the auxiliary variable in integer solution */
+};
+
+struct GLPCOL
+{ /* LP/MIP column (structural variable) */
+ int j;
+ /* ordinal number (1 to n) assigned to this column */
+ char *name;
+ /* column name (1 to 255 chars); NULL means no name is assigned
+ to this column */
+ AVLNODE *node;
+ /* pointer to corresponding node in the column index; NULL means
+ that either the column index does not exist or the column has
+ no name assigned */
+ int kind;
+ /* kind of the structural variable:
+ GLP_CV - continuous variable
+ GLP_IV - integer or binary variable */
+ int type;
+ /* type of the structural variable:
+ GLP_FR - free variable
+ GLP_LO - variable with lower bound
+ GLP_UP - variable with upper bound
+ GLP_DB - double-bounded variable
+ GLP_FX - fixed variable */
+ double lb; /* non-scaled */
+ /* lower bound; if the column has no lower bound, lb is zero */
+ double ub; /* non-scaled */
+ /* upper bound; if the column has no upper bound, ub is zero */
+ /* if the column type is GLP_FX, ub is equal to lb */
+ double coef; /* non-scaled */
+ /* objective coefficient at the structural variable */
+ GLPAIJ *ptr; /* non-scaled */
+ /* pointer to doubly linked list of constraint coefficients which
+ are placed in this column */
+ double sjj;
+ /* diagonal element s[j,j] of scaling matrix S for this column;
+ if the scaling is not used, s[j,j] is 1 */
+ int stat;
+ /* status of the structural variable:
+ GLP_BS - basic variable
+ GLP_NL - non-basic variable on lower bound
+ GLP_NU - non-basic variable on upper bound
+ GLP_NF - non-basic free variable
+ GLP_NS - non-basic fixed variable */
+ int bind;
+ /* if the structural variable is basic, head[bind] refers to
+ this column; otherwise, bind is 0; this attribute is valid only
+ if the basis factorization is valid */
+ double prim; /* non-scaled */
+ /* primal value of the structural variable in basic solution */
+ double dual; /* non-scaled */
+ /* dual value of the structural variable in basic solution */
+ double pval; /* non-scaled */
+ /* primal value of the structural variable in interior solution */
+ double dval; /* non-scaled */
+ /* dual value of the structural variable in interior solution */
+ double mipx; /* non-scaled */
+ /* primal value of the structural variable in integer solution */
+};
+
+struct GLPAIJ
+{ /* constraint coefficient a[i,j] */
+ GLPROW *row;
+ /* pointer to row, where this coefficient is placed */
+ GLPCOL *col;
+ /* pointer to column, where this coefficient is placed */
+ double val;
+ /* numeric (non-zero) value of this coefficient */
+ GLPAIJ *r_prev;
+ /* pointer to previous coefficient in the same row */
+ GLPAIJ *r_next;
+ /* pointer to next coefficient in the same row */
+ GLPAIJ *c_prev;
+ /* pointer to previous coefficient in the same column */
+ GLPAIJ *c_next;
+ /* pointer to next coefficient in the same column */
+};
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob1.c b/test/monniaux/glpk-4.65/src/api/prob1.c
new file mode 100644
index 00000000..6afad442
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob1.c
@@ -0,0 +1,1588 @@
+/* prob1.c (problem creating and modifying routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/* CAUTION: DO NOT CHANGE THE LIMITS BELOW */
+
+#define M_MAX 100000000 /* = 100*10^6 */
+/* maximal number of rows in the problem object */
+
+#define N_MAX 100000000 /* = 100*10^6 */
+/* maximal number of columns in the problem object */
+
+#define NNZ_MAX 500000000 /* = 500*10^6 */
+/* maximal number of constraint coefficients in the problem object */
+
+/***********************************************************************
+* NAME
+*
+* glp_create_prob - create problem object
+*
+* SYNOPSIS
+*
+* glp_prob *glp_create_prob(void);
+*
+* DESCRIPTION
+*
+* The routine glp_create_prob creates a new problem object, which is
+* initially "empty", i.e. has no rows and columns.
+*
+* RETURNS
+*
+* The routine returns a pointer to the object created, which should be
+* used in any subsequent operations on this object. */
+
+static void create_prob(glp_prob *lp)
+#if 0 /* 04/IV-2016 */
+{ lp->magic = GLP_PROB_MAGIC;
+#else
+{
+#endif
+ lp->pool = dmp_create_pool();
+#if 0 /* 08/III-2014 */
+#if 0 /* 17/XI-2009 */
+ lp->cps = xmalloc(sizeof(struct LPXCPS));
+ lpx_reset_parms(lp);
+#else
+ lp->parms = NULL;
+#endif
+#endif
+ lp->tree = NULL;
+#if 0
+ lp->lwa = 0;
+ lp->cwa = NULL;
+#endif
+ /* LP/MIP data */
+ lp->name = NULL;
+ lp->obj = NULL;
+ lp->dir = GLP_MIN;
+ lp->c0 = 0.0;
+ lp->m_max = 100;
+ lp->n_max = 200;
+ lp->m = lp->n = 0;
+ lp->nnz = 0;
+ lp->row = xcalloc(1+lp->m_max, sizeof(GLPROW *));
+ lp->col = xcalloc(1+lp->n_max, sizeof(GLPCOL *));
+ lp->r_tree = lp->c_tree = NULL;
+ /* basis factorization */
+ lp->valid = 0;
+ lp->head = xcalloc(1+lp->m_max, sizeof(int));
+#if 0 /* 08/III-2014 */
+ lp->bfcp = NULL;
+#endif
+ lp->bfd = NULL;
+ /* basic solution (LP) */
+ lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
+ lp->obj_val = 0.0;
+ lp->it_cnt = 0;
+ lp->some = 0;
+ /* interior-point solution (LP) */
+ lp->ipt_stat = GLP_UNDEF;
+ lp->ipt_obj = 0.0;
+ /* integer solution (MIP) */
+ lp->mip_stat = GLP_UNDEF;
+ lp->mip_obj = 0.0;
+ return;
+}
+
+glp_prob *glp_create_prob(void)
+{ glp_prob *lp;
+ lp = xmalloc(sizeof(glp_prob));
+ create_prob(lp);
+ return lp;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_prob_name - assign (change) problem name
+*
+* SYNOPSIS
+*
+* void glp_set_prob_name(glp_prob *lp, const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_set_prob_name assigns a given symbolic name (1 up to
+* 255 characters) to the specified problem object.
+*
+* If the parameter name is NULL or empty string, the routine erases an
+* existing symbolic name of the problem object. */
+
+void glp_set_prob_name(glp_prob *lp, const char *name)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_prob_name: operation not allowed\n");
+ if (lp->name != NULL)
+ { dmp_free_atom(lp->pool, lp->name, strlen(lp->name)+1);
+ lp->name = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int k;
+ for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_set_prob_name: problem name too long\n");
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_set_prob_name: problem name contains invalid"
+ " character(s)\n");
+ }
+ lp->name = dmp_get_atom(lp->pool, strlen(name)+1);
+ strcpy(lp->name, name);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_obj_name - assign (change) objective function name
+*
+* SYNOPSIS
+*
+* void glp_set_obj_name(glp_prob *lp, const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_set_obj_name assigns a given symbolic name (1 up to
+* 255 characters) to the objective function of the specified problem
+* object.
+*
+* If the parameter name is NULL or empty string, the routine erases an
+* existing name of the objective function. */
+
+void glp_set_obj_name(glp_prob *lp, const char *name)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_obj_name: operation not allowed\n");
+ if (lp->obj != NULL)
+ { dmp_free_atom(lp->pool, lp->obj, strlen(lp->obj)+1);
+ lp->obj = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int k;
+ for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_set_obj_name: objective name too long\n");
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_set_obj_name: objective name contains invali"
+ "d character(s)\n");
+ }
+ lp->obj = dmp_get_atom(lp->pool, strlen(name)+1);
+ strcpy(lp->obj, name);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_obj_dir - set (change) optimization direction flag
+*
+* SYNOPSIS
+*
+* void glp_set_obj_dir(glp_prob *lp, int dir);
+*
+* DESCRIPTION
+*
+* The routine glp_set_obj_dir sets (changes) optimization direction
+* flag (i.e. "sense" of the objective function) as specified by the
+* parameter dir:
+*
+* GLP_MIN - minimization;
+* GLP_MAX - maximization. */
+
+void glp_set_obj_dir(glp_prob *lp, int dir)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_obj_dir: operation not allowed\n");
+ if (!(dir == GLP_MIN || dir == GLP_MAX))
+ xerror("glp_set_obj_dir: dir = %d; invalid direction flag\n",
+ dir);
+ lp->dir = dir;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_add_rows - add new rows to problem object
+*
+* SYNOPSIS
+*
+* int glp_add_rows(glp_prob *lp, int nrs);
+*
+* DESCRIPTION
+*
+* The routine glp_add_rows adds nrs rows (constraints) to the specified
+* problem object. New rows are always added to the end of the row list,
+* so the ordinal numbers of existing rows remain unchanged.
+*
+* Being added each new row is initially free (unbounded) and has empty
+* list of the constraint coefficients.
+*
+* RETURNS
+*
+* The routine glp_add_rows returns the ordinal number of the first new
+* row added to the problem object. */
+
+int glp_add_rows(glp_prob *lp, int nrs)
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ int m_new, i;
+ /* determine new number of rows */
+ if (nrs < 1)
+ xerror("glp_add_rows: nrs = %d; invalid number of rows\n",
+ nrs);
+ if (nrs > M_MAX - lp->m)
+ xerror("glp_add_rows: nrs = %d; too many rows\n", nrs);
+ m_new = lp->m + nrs;
+ /* increase the room, if necessary */
+ if (lp->m_max < m_new)
+ { GLPROW **save = lp->row;
+ while (lp->m_max < m_new)
+ { lp->m_max += lp->m_max;
+ xassert(lp->m_max > 0);
+ }
+ lp->row = xcalloc(1+lp->m_max, sizeof(GLPROW *));
+ memcpy(&lp->row[1], &save[1], lp->m * sizeof(GLPROW *));
+ xfree(save);
+ /* do not forget about the basis header */
+ xfree(lp->head);
+ lp->head = xcalloc(1+lp->m_max, sizeof(int));
+ }
+ /* add new rows to the end of the row list */
+ for (i = lp->m+1; i <= m_new; i++)
+ { /* create row descriptor */
+ lp->row[i] = row = dmp_get_atom(lp->pool, sizeof(GLPROW));
+ row->i = i;
+ row->name = NULL;
+ row->node = NULL;
+#if 1 /* 20/IX-2008 */
+ row->level = 0;
+ row->origin = 0;
+ row->klass = 0;
+ if (tree != NULL)
+ { switch (tree->reason)
+ { case 0:
+ break;
+ case GLP_IROWGEN:
+ xassert(tree->curr != NULL);
+ row->level = tree->curr->level;
+ row->origin = GLP_RF_LAZY;
+ break;
+ case GLP_ICUTGEN:
+ xassert(tree->curr != NULL);
+ row->level = tree->curr->level;
+ row->origin = GLP_RF_CUT;
+ break;
+ default:
+ xassert(tree != tree);
+ }
+ }
+#endif
+ row->type = GLP_FR;
+ row->lb = row->ub = 0.0;
+ row->ptr = NULL;
+ row->rii = 1.0;
+ row->stat = GLP_BS;
+#if 0
+ row->bind = -1;
+#else
+ row->bind = 0;
+#endif
+ row->prim = row->dual = 0.0;
+ row->pval = row->dval = 0.0;
+ row->mipx = 0.0;
+ }
+ /* set new number of rows */
+ lp->m = m_new;
+ /* invalidate the basis factorization */
+ lp->valid = 0;
+#if 1
+ if (tree != NULL && tree->reason != 0) tree->reopt = 1;
+#endif
+ /* return the ordinal number of the first row added */
+ return m_new - nrs + 1;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_add_cols - add new columns to problem object
+*
+* SYNOPSIS
+*
+* int glp_add_cols(glp_prob *lp, int ncs);
+*
+* DESCRIPTION
+*
+* The routine glp_add_cols adds ncs columns (structural variables) to
+* the specified problem object. New columns are always added to the end
+* of the column list, so the ordinal numbers of existing columns remain
+* unchanged.
+*
+* Being added each new column is initially fixed at zero and has empty
+* list of the constraint coefficients.
+*
+* RETURNS
+*
+* The routine glp_add_cols returns the ordinal number of the first new
+* column added to the problem object. */
+
+int glp_add_cols(glp_prob *lp, int ncs)
+{ glp_tree *tree = lp->tree;
+ GLPCOL *col;
+ int n_new, j;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_add_cols: operation not allowed\n");
+ /* determine new number of columns */
+ if (ncs < 1)
+ xerror("glp_add_cols: ncs = %d; invalid number of columns\n",
+ ncs);
+ if (ncs > N_MAX - lp->n)
+ xerror("glp_add_cols: ncs = %d; too many columns\n", ncs);
+ n_new = lp->n + ncs;
+ /* increase the room, if necessary */
+ if (lp->n_max < n_new)
+ { GLPCOL **save = lp->col;
+ while (lp->n_max < n_new)
+ { lp->n_max += lp->n_max;
+ xassert(lp->n_max > 0);
+ }
+ lp->col = xcalloc(1+lp->n_max, sizeof(GLPCOL *));
+ memcpy(&lp->col[1], &save[1], lp->n * sizeof(GLPCOL *));
+ xfree(save);
+ }
+ /* add new columns to the end of the column list */
+ for (j = lp->n+1; j <= n_new; j++)
+ { /* create column descriptor */
+ lp->col[j] = col = dmp_get_atom(lp->pool, sizeof(GLPCOL));
+ col->j = j;
+ col->name = NULL;
+ col->node = NULL;
+ col->kind = GLP_CV;
+ col->type = GLP_FX;
+ col->lb = col->ub = 0.0;
+ col->coef = 0.0;
+ col->ptr = NULL;
+ col->sjj = 1.0;
+ col->stat = GLP_NS;
+#if 0
+ col->bind = -1;
+#else
+ col->bind = 0; /* the basis may remain valid */
+#endif
+ col->prim = col->dual = 0.0;
+ col->pval = col->dval = 0.0;
+ col->mipx = 0.0;
+ }
+ /* set new number of columns */
+ lp->n = n_new;
+ /* return the ordinal number of the first column added */
+ return n_new - ncs + 1;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_row_name - assign (change) row name
+*
+* SYNOPSIS
+*
+* void glp_set_row_name(glp_prob *lp, int i, const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_set_row_name assigns a given symbolic name (1 up to
+* 255 characters) to i-th row (auxiliary variable) of the specified
+* problem object.
+*
+* If the parameter name is NULL or empty string, the routine erases an
+* existing name of i-th row. */
+
+void glp_set_row_name(glp_prob *lp, int i, const char *name)
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_row_name: i = %d; row number out of range\n",
+ i);
+ row = lp->row[i];
+ if (tree != NULL && tree->reason != 0)
+ { xassert(tree->curr != NULL);
+ xassert(row->level == tree->curr->level);
+ }
+ if (row->name != NULL)
+ { if (row->node != NULL)
+ { xassert(lp->r_tree != NULL);
+ avl_delete_node(lp->r_tree, row->node);
+ row->node = NULL;
+ }
+ dmp_free_atom(lp->pool, row->name, strlen(row->name)+1);
+ row->name = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int k;
+ for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_set_row_name: i = %d; row name too long\n",
+ i);
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_set_row_name: i = %d: row name contains inva"
+ "lid character(s)\n", i);
+ }
+ row->name = dmp_get_atom(lp->pool, strlen(name)+1);
+ strcpy(row->name, name);
+ if (lp->r_tree != NULL)
+ { xassert(row->node == NULL);
+ row->node = avl_insert_node(lp->r_tree, row->name);
+ avl_set_node_link(row->node, row);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_col_name - assign (change) column name
+*
+* SYNOPSIS
+*
+* void glp_set_col_name(glp_prob *lp, int j, const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_set_col_name assigns a given symbolic name (1 up to
+* 255 characters) to j-th column (structural variable) of the specified
+* problem object.
+*
+* If the parameter name is NULL or empty string, the routine erases an
+* existing name of j-th column. */
+
+void glp_set_col_name(glp_prob *lp, int j, const char *name)
+{ glp_tree *tree = lp->tree;
+ GLPCOL *col;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_col_name: operation not allowed\n");
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_col_name: j = %d; column number out of range\n"
+ , j);
+ col = lp->col[j];
+ if (col->name != NULL)
+ { if (col->node != NULL)
+ { xassert(lp->c_tree != NULL);
+ avl_delete_node(lp->c_tree, col->node);
+ col->node = NULL;
+ }
+ dmp_free_atom(lp->pool, col->name, strlen(col->name)+1);
+ col->name = NULL;
+ }
+ if (!(name == NULL || name[0] == '\0'))
+ { int k;
+ for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_set_col_name: j = %d; column name too long\n"
+ , j);
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_set_col_name: j = %d: column name contains i"
+ "nvalid character(s)\n", j);
+ }
+ col->name = dmp_get_atom(lp->pool, strlen(name)+1);
+ strcpy(col->name, name);
+ if (lp->c_tree != NULL && col->name != NULL)
+ { xassert(col->node == NULL);
+ col->node = avl_insert_node(lp->c_tree, col->name);
+ avl_set_node_link(col->node, col);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_row_bnds - set (change) row bounds
+*
+* SYNOPSIS
+*
+* void glp_set_row_bnds(glp_prob *lp, int i, int type, double lb,
+* double ub);
+*
+* DESCRIPTION
+*
+* The routine glp_set_row_bnds sets (changes) the type and bounds of
+* i-th row (auxiliary variable) of the specified problem object.
+*
+* Parameters type, lb, and ub specify the type, lower bound, and upper
+* bound, respectively, as follows:
+*
+* Type Bounds Comments
+* ------------------------------------------------------
+* GLP_FR -inf < x < +inf Free variable
+* GLP_LO lb <= x < +inf Variable with lower bound
+* GLP_UP -inf < x <= ub Variable with upper bound
+* GLP_DB lb <= x <= ub Double-bounded variable
+* GLP_FX x = lb Fixed variable
+*
+* where x is the auxiliary variable associated with i-th row.
+*
+* If the row has no lower bound, the parameter lb is ignored. If the
+* row has no upper bound, the parameter ub is ignored. If the row is
+* an equality constraint (i.e. the corresponding auxiliary variable is
+* of fixed type), only the parameter lb is used while the parameter ub
+* is ignored. */
+
+void glp_set_row_bnds(glp_prob *lp, int i, int type, double lb,
+ double ub)
+{ GLPROW *row;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_row_bnds: i = %d; row number out of range\n",
+ i);
+ row = lp->row[i];
+ row->type = type;
+ switch (type)
+ { case GLP_FR:
+ row->lb = row->ub = 0.0;
+ if (row->stat != GLP_BS) row->stat = GLP_NF;
+ break;
+ case GLP_LO:
+ row->lb = lb, row->ub = 0.0;
+ if (row->stat != GLP_BS) row->stat = GLP_NL;
+ break;
+ case GLP_UP:
+ row->lb = 0.0, row->ub = ub;
+ if (row->stat != GLP_BS) row->stat = GLP_NU;
+ break;
+ case GLP_DB:
+ row->lb = lb, row->ub = ub;
+ if (!(row->stat == GLP_BS ||
+ row->stat == GLP_NL || row->stat == GLP_NU))
+ row->stat = (fabs(lb) <= fabs(ub) ? GLP_NL : GLP_NU);
+ break;
+ case GLP_FX:
+ row->lb = row->ub = lb;
+ if (row->stat != GLP_BS) row->stat = GLP_NS;
+ break;
+ default:
+ xerror("glp_set_row_bnds: i = %d; type = %d; invalid row ty"
+ "pe\n", i, type);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_col_bnds - set (change) column bounds
+*
+* SYNOPSIS
+*
+* void glp_set_col_bnds(glp_prob *lp, int j, int type, double lb,
+* double ub);
+*
+* DESCRIPTION
+*
+* The routine glp_set_col_bnds sets (changes) the type and bounds of
+* j-th column (structural variable) of the specified problem object.
+*
+* Parameters type, lb, and ub specify the type, lower bound, and upper
+* bound, respectively, as follows:
+*
+* Type Bounds Comments
+* ------------------------------------------------------
+* GLP_FR -inf < x < +inf Free variable
+* GLP_LO lb <= x < +inf Variable with lower bound
+* GLP_UP -inf < x <= ub Variable with upper bound
+* GLP_DB lb <= x <= ub Double-bounded variable
+* GLP_FX x = lb Fixed variable
+*
+* where x is the structural variable associated with j-th column.
+*
+* If the column has no lower bound, the parameter lb is ignored. If the
+* column has no upper bound, the parameter ub is ignored. If the column
+* is of fixed type, only the parameter lb is used while the parameter
+* ub is ignored. */
+
+void glp_set_col_bnds(glp_prob *lp, int j, int type, double lb,
+ double ub)
+{ GLPCOL *col;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_col_bnds: j = %d; column number out of range\n"
+ , j);
+ col = lp->col[j];
+ col->type = type;
+ switch (type)
+ { case GLP_FR:
+ col->lb = col->ub = 0.0;
+ if (col->stat != GLP_BS) col->stat = GLP_NF;
+ break;
+ case GLP_LO:
+ col->lb = lb, col->ub = 0.0;
+ if (col->stat != GLP_BS) col->stat = GLP_NL;
+ break;
+ case GLP_UP:
+ col->lb = 0.0, col->ub = ub;
+ if (col->stat != GLP_BS) col->stat = GLP_NU;
+ break;
+ case GLP_DB:
+ col->lb = lb, col->ub = ub;
+ if (!(col->stat == GLP_BS ||
+ col->stat == GLP_NL || col->stat == GLP_NU))
+ col->stat = (fabs(lb) <= fabs(ub) ? GLP_NL : GLP_NU);
+ break;
+ case GLP_FX:
+ col->lb = col->ub = lb;
+ if (col->stat != GLP_BS) col->stat = GLP_NS;
+ break;
+ default:
+ xerror("glp_set_col_bnds: j = %d; type = %d; invalid column"
+ " type\n", j, type);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_obj_coef - set (change) obj. coefficient or constant term
+*
+* SYNOPSIS
+*
+* void glp_set_obj_coef(glp_prob *lp, int j, double coef);
+*
+* DESCRIPTION
+*
+* The routine glp_set_obj_coef sets (changes) objective coefficient at
+* j-th column (structural variable) of the specified problem object.
+*
+* If the parameter j is 0, the routine sets (changes) the constant term
+* ("shift") of the objective function. */
+
+void glp_set_obj_coef(glp_prob *lp, int j, double coef)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_obj_coef: operation not allowed\n");
+ if (!(0 <= j && j <= lp->n))
+ xerror("glp_set_obj_coef: j = %d; column number out of range\n"
+ , j);
+ if (j == 0)
+ lp->c0 = coef;
+ else
+ lp->col[j]->coef = coef;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_mat_row - set (replace) row of the constraint matrix
+*
+* SYNOPSIS
+*
+* void glp_set_mat_row(glp_prob *lp, int i, int len, const int ind[],
+* const double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_set_mat_row stores (replaces) the contents of i-th
+* row of the constraint matrix of the specified problem object.
+*
+* Column indices and numeric values of new row elements must be placed
+* in locations ind[1], ..., ind[len] and val[1], ..., val[len], where
+* 0 <= len <= n is the new length of i-th row, n is the current number
+* of columns in the problem object. Elements with identical column
+* indices are not allowed. Zero elements are allowed, but they are not
+* stored in the constraint matrix.
+*
+* If the parameter len is zero, the parameters ind and/or val can be
+* specified as NULL. */
+
+void glp_set_mat_row(glp_prob *lp, int i, int len, const int ind[],
+ const double val[])
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij, *next;
+ int j, k;
+ /* obtain pointer to i-th row */
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_mat_row: i = %d; row number out of range\n",
+ i);
+ row = lp->row[i];
+ if (tree != NULL && tree->reason != 0)
+ { xassert(tree->curr != NULL);
+ xassert(row->level == tree->curr->level);
+ }
+ /* remove all existing elements from i-th row */
+ while (row->ptr != NULL)
+ { /* take next element in the row */
+ aij = row->ptr;
+ /* remove the element from the row list */
+ row->ptr = aij->r_next;
+ /* obtain pointer to corresponding column */
+ col = aij->col;
+ /* remove the element from the column list */
+ if (aij->c_prev == NULL)
+ col->ptr = aij->c_next;
+ else
+ aij->c_prev->c_next = aij->c_next;
+ if (aij->c_next == NULL)
+ ;
+ else
+ aij->c_next->c_prev = aij->c_prev;
+ /* return the element to the memory pool */
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ /* if the corresponding column is basic, invalidate the basis
+ factorization */
+ if (col->stat == GLP_BS) lp->valid = 0;
+ }
+ /* store new contents of i-th row */
+ if (!(0 <= len && len <= lp->n))
+ xerror("glp_set_mat_row: i = %d; len = %d; invalid row length "
+ "\n", i, len);
+ if (len > NNZ_MAX - lp->nnz)
+ xerror("glp_set_mat_row: i = %d; len = %d; too many constraint"
+ " coefficients\n", i, len);
+ for (k = 1; k <= len; k++)
+ { /* take number j of corresponding column */
+ j = ind[k];
+ /* obtain pointer to j-th column */
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_mat_row: i = %d; ind[%d] = %d; column index"
+ " out of range\n", i, k, j);
+ col = lp->col[j];
+ /* if there is element with the same column index, it can only
+ be found in the beginning of j-th column list */
+ if (col->ptr != NULL && col->ptr->row->i == i)
+ xerror("glp_set_mat_row: i = %d; ind[%d] = %d; duplicate co"
+ "lumn indices not allowed\n", i, k, j);
+ /* create new element */
+ aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++;
+ aij->row = row;
+ aij->col = col;
+ aij->val = val[k];
+ /* add the new element to the beginning of i-th row and j-th
+ column lists */
+ aij->r_prev = NULL;
+ aij->r_next = row->ptr;
+ aij->c_prev = NULL;
+ aij->c_next = col->ptr;
+ if (aij->r_next != NULL) aij->r_next->r_prev = aij;
+ if (aij->c_next != NULL) aij->c_next->c_prev = aij;
+ row->ptr = col->ptr = aij;
+ /* if the corresponding column is basic, invalidate the basis
+ factorization */
+ if (col->stat == GLP_BS && aij->val != 0.0) lp->valid = 0;
+ }
+ /* remove zero elements from i-th row */
+ for (aij = row->ptr; aij != NULL; aij = next)
+ { next = aij->r_next;
+ if (aij->val == 0.0)
+ { /* remove the element from the row list */
+ if (aij->r_prev == NULL)
+ row->ptr = next;
+ else
+ aij->r_prev->r_next = next;
+ if (next == NULL)
+ ;
+ else
+ next->r_prev = aij->r_prev;
+ /* remove the element from the column list */
+ xassert(aij->c_prev == NULL);
+ aij->col->ptr = aij->c_next;
+ if (aij->c_next != NULL) aij->c_next->c_prev = NULL;
+ /* return the element to the memory pool */
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_mat_col - set (replace) column of the constraint matrix
+*
+* SYNOPSIS
+*
+* void glp_set_mat_col(glp_prob *lp, int j, int len, const int ind[],
+* const double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_set_mat_col stores (replaces) the contents of j-th
+* column of the constraint matrix of the specified problem object.
+*
+* Row indices and numeric values of new column elements must be placed
+* in locations ind[1], ..., ind[len] and val[1], ..., val[len], where
+* 0 <= len <= m is the new length of j-th column, m is the current
+* number of rows in the problem object. Elements with identical column
+* indices are not allowed. Zero elements are allowed, but they are not
+* stored in the constraint matrix.
+*
+* If the parameter len is zero, the parameters ind and/or val can be
+* specified as NULL. */
+
+void glp_set_mat_col(glp_prob *lp, int j, int len, const int ind[],
+ const double val[])
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij, *next;
+ int i, k;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_set_mat_col: operation not allowed\n");
+ /* obtain pointer to j-th column */
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_mat_col: j = %d; column number out of range\n",
+ j);
+ col = lp->col[j];
+ /* remove all existing elements from j-th column */
+ while (col->ptr != NULL)
+ { /* take next element in the column */
+ aij = col->ptr;
+ /* remove the element from the column list */
+ col->ptr = aij->c_next;
+ /* obtain pointer to corresponding row */
+ row = aij->row;
+ /* remove the element from the row list */
+ if (aij->r_prev == NULL)
+ row->ptr = aij->r_next;
+ else
+ aij->r_prev->r_next = aij->r_next;
+ if (aij->r_next == NULL)
+ ;
+ else
+ aij->r_next->r_prev = aij->r_prev;
+ /* return the element to the memory pool */
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ }
+ /* store new contents of j-th column */
+ if (!(0 <= len && len <= lp->m))
+ xerror("glp_set_mat_col: j = %d; len = %d; invalid column leng"
+ "th\n", j, len);
+ if (len > NNZ_MAX - lp->nnz)
+ xerror("glp_set_mat_col: j = %d; len = %d; too many constraint"
+ " coefficients\n", j, len);
+ for (k = 1; k <= len; k++)
+ { /* take number i of corresponding row */
+ i = ind[k];
+ /* obtain pointer to i-th row */
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_mat_col: j = %d; ind[%d] = %d; row index ou"
+ "t of range\n", j, k, i);
+ row = lp->row[i];
+ /* if there is element with the same row index, it can only be
+ found in the beginning of i-th row list */
+ if (row->ptr != NULL && row->ptr->col->j == j)
+ xerror("glp_set_mat_col: j = %d; ind[%d] = %d; duplicate ro"
+ "w indices not allowed\n", j, k, i);
+ /* create new element */
+ aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++;
+ aij->row = row;
+ aij->col = col;
+ aij->val = val[k];
+ /* add the new element to the beginning of i-th row and j-th
+ column lists */
+ aij->r_prev = NULL;
+ aij->r_next = row->ptr;
+ aij->c_prev = NULL;
+ aij->c_next = col->ptr;
+ if (aij->r_next != NULL) aij->r_next->r_prev = aij;
+ if (aij->c_next != NULL) aij->c_next->c_prev = aij;
+ row->ptr = col->ptr = aij;
+ }
+ /* remove zero elements from j-th column */
+ for (aij = col->ptr; aij != NULL; aij = next)
+ { next = aij->c_next;
+ if (aij->val == 0.0)
+ { /* remove the element from the row list */
+ xassert(aij->r_prev == NULL);
+ aij->row->ptr = aij->r_next;
+ if (aij->r_next != NULL) aij->r_next->r_prev = NULL;
+ /* remove the element from the column list */
+ if (aij->c_prev == NULL)
+ col->ptr = next;
+ else
+ aij->c_prev->c_next = next;
+ if (next == NULL)
+ ;
+ else
+ next->c_prev = aij->c_prev;
+ /* return the element to the memory pool */
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ }
+ }
+ /* if j-th column is basic, invalidate the basis factorization */
+ if (col->stat == GLP_BS) lp->valid = 0;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_load_matrix - load (replace) the whole constraint matrix
+*
+* SYNOPSIS
+*
+* void glp_load_matrix(glp_prob *lp, int ne, const int ia[],
+* const int ja[], const double ar[]);
+*
+* DESCRIPTION
+*
+* The routine glp_load_matrix loads the constraint matrix passed in
+* the arrays ia, ja, and ar into the specified problem object. Before
+* loading the current contents of the constraint matrix is destroyed.
+*
+* Constraint coefficients (elements of the constraint matrix) must be
+* specified as triplets (ia[k], ja[k], ar[k]) for k = 1, ..., ne,
+* where ia[k] is the row index, ja[k] is the column index, ar[k] is a
+* numeric value of corresponding constraint coefficient. The parameter
+* ne specifies the total number of (non-zero) elements in the matrix
+* to be loaded. Coefficients with identical indices are not allowed.
+* Zero coefficients are allowed, however, they are not stored in the
+* constraint matrix.
+*
+* If the parameter ne is zero, the parameters ia, ja, and ar can be
+* specified as NULL. */
+
+void glp_load_matrix(glp_prob *lp, int ne, const int ia[],
+ const int ja[], const double ar[])
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij, *next;
+ int i, j, k;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_load_matrix: operation not allowed\n");
+ /* clear the constraint matrix */
+ for (i = 1; i <= lp->m; i++)
+ { row = lp->row[i];
+ while (row->ptr != NULL)
+ { aij = row->ptr;
+ row->ptr = aij->r_next;
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ }
+ }
+ xassert(lp->nnz == 0);
+ for (j = 1; j <= lp->n; j++) lp->col[j]->ptr = NULL;
+ /* load the new contents of the constraint matrix and build its
+ row lists */
+ if (ne < 0)
+ xerror("glp_load_matrix: ne = %d; invalid number of constraint"
+ " coefficients\n", ne);
+ if (ne > NNZ_MAX)
+ xerror("glp_load_matrix: ne = %d; too many constraint coeffici"
+ "ents\n", ne);
+ for (k = 1; k <= ne; k++)
+ { /* take indices of new element */
+ i = ia[k], j = ja[k];
+ /* obtain pointer to i-th row */
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_load_matrix: ia[%d] = %d; row index out of rang"
+ "e\n", k, i);
+ row = lp->row[i];
+ /* obtain pointer to j-th column */
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_load_matrix: ja[%d] = %d; column index out of r"
+ "ange\n", k, j);
+ col = lp->col[j];
+ /* create new element */
+ aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++;
+ aij->row = row;
+ aij->col = col;
+ aij->val = ar[k];
+ /* add the new element to the beginning of i-th row list */
+ aij->r_prev = NULL;
+ aij->r_next = row->ptr;
+ if (aij->r_next != NULL) aij->r_next->r_prev = aij;
+ row->ptr = aij;
+ }
+ xassert(lp->nnz == ne);
+ /* build column lists of the constraint matrix and check elements
+ with identical indices */
+ for (i = 1; i <= lp->m; i++)
+ { for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { /* obtain pointer to corresponding column */
+ col = aij->col;
+ /* if there is element with identical indices, it can only
+ be found in the beginning of j-th column list */
+ if (col->ptr != NULL && col->ptr->row->i == i)
+ { for (k = 1; k <= ne; k++)
+ if (ia[k] == i && ja[k] == col->j) break;
+ xerror("glp_load_mat: ia[%d] = %d; ja[%d] = %d; duplicat"
+ "e indices not allowed\n", k, i, k, col->j);
+ }
+ /* add the element to the beginning of j-th column list */
+ aij->c_prev = NULL;
+ aij->c_next = col->ptr;
+ if (aij->c_next != NULL) aij->c_next->c_prev = aij;
+ col->ptr = aij;
+ }
+ }
+ /* remove zero elements from the constraint matrix */
+ for (i = 1; i <= lp->m; i++)
+ { row = lp->row[i];
+ for (aij = row->ptr; aij != NULL; aij = next)
+ { next = aij->r_next;
+ if (aij->val == 0.0)
+ { /* remove the element from the row list */
+ if (aij->r_prev == NULL)
+ row->ptr = next;
+ else
+ aij->r_prev->r_next = next;
+ if (next == NULL)
+ ;
+ else
+ next->r_prev = aij->r_prev;
+ /* remove the element from the column list */
+ if (aij->c_prev == NULL)
+ aij->col->ptr = aij->c_next;
+ else
+ aij->c_prev->c_next = aij->c_next;
+ if (aij->c_next == NULL)
+ ;
+ else
+ aij->c_next->c_prev = aij->c_prev;
+ /* return the element to the memory pool */
+ dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--;
+ }
+ }
+ }
+ /* invalidate the basis factorization */
+ lp->valid = 0;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_check_dup - check for duplicate elements in sparse matrix
+*
+* SYNOPSIS
+*
+* int glp_check_dup(int m, int n, int ne, const int ia[],
+* const int ja[]);
+*
+* DESCRIPTION
+*
+* The routine glp_check_dup checks for duplicate elements (that is,
+* elements with identical indices) in a sparse matrix specified in the
+* coordinate format.
+*
+* The parameters m and n specifies, respectively, the number of rows
+* and columns in the matrix, m >= 0, n >= 0.
+*
+* The parameter ne specifies the number of (structurally) non-zero
+* elements in the matrix, ne >= 0.
+*
+* Elements of the matrix are specified as doublets (ia[k],ja[k]) for
+* k = 1,...,ne, where ia[k] is a row index, ja[k] is a column index.
+*
+* The routine glp_check_dup can be used prior to a call to the routine
+* glp_load_matrix to check that the constraint matrix to be loaded has
+* no duplicate elements.
+*
+* RETURNS
+*
+* The routine glp_check_dup returns one of the following values:
+*
+* 0 - the matrix has no duplicate elements;
+*
+* -k - indices ia[k] or/and ja[k] are out of range;
+*
+* +k - element (ia[k],ja[k]) is duplicate. */
+
+int glp_check_dup(int m, int n, int ne, const int ia[], const int ja[])
+{ int i, j, k, *ptr, *next, ret;
+ char *flag;
+ if (m < 0)
+ xerror("glp_check_dup: m = %d; invalid parameter\n");
+ if (n < 0)
+ xerror("glp_check_dup: n = %d; invalid parameter\n");
+ if (ne < 0)
+ xerror("glp_check_dup: ne = %d; invalid parameter\n");
+ if (ne > 0 && ia == NULL)
+ xerror("glp_check_dup: ia = %p; invalid parameter\n", ia);
+ if (ne > 0 && ja == NULL)
+ xerror("glp_check_dup: ja = %p; invalid parameter\n", ja);
+ for (k = 1; k <= ne; k++)
+ { i = ia[k], j = ja[k];
+ if (!(1 <= i && i <= m && 1 <= j && j <= n))
+ { ret = -k;
+ goto done;
+ }
+ }
+ if (m == 0 || n == 0)
+ { ret = 0;
+ goto done;
+ }
+ /* allocate working arrays */
+ ptr = xcalloc(1+m, sizeof(int));
+ next = xcalloc(1+ne, sizeof(int));
+ flag = xcalloc(1+n, sizeof(char));
+ /* build row lists */
+ for (i = 1; i <= m; i++)
+ ptr[i] = 0;
+ for (k = 1; k <= ne; k++)
+ { i = ia[k];
+ next[k] = ptr[i];
+ ptr[i] = k;
+ }
+ /* clear column flags */
+ for (j = 1; j <= n; j++)
+ flag[j] = 0;
+ /* check for duplicate elements */
+ for (i = 1; i <= m; i++)
+ { for (k = ptr[i]; k != 0; k = next[k])
+ { j = ja[k];
+ if (flag[j])
+ { /* find first element (i,j) */
+ for (k = 1; k <= ne; k++)
+ if (ia[k] == i && ja[k] == j) break;
+ xassert(k <= ne);
+ /* find next (duplicate) element (i,j) */
+ for (k++; k <= ne; k++)
+ if (ia[k] == i && ja[k] == j) break;
+ xassert(k <= ne);
+ ret = +k;
+ goto skip;
+ }
+ flag[j] = 1;
+ }
+ /* clear column flags */
+ for (k = ptr[i]; k != 0; k = next[k])
+ flag[ja[k]] = 0;
+ }
+ /* no duplicate element found */
+ ret = 0;
+skip: /* free working arrays */
+ xfree(ptr);
+ xfree(next);
+ xfree(flag);
+done: return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_sort_matrix - sort elements of the constraint matrix
+*
+* SYNOPSIS
+*
+* void glp_sort_matrix(glp_prob *P);
+*
+* DESCRIPTION
+*
+* The routine glp_sort_matrix sorts elements of the constraint matrix
+* rebuilding its row and column linked lists. On exit from the routine
+* the constraint matrix is not changed, however, elements in the row
+* linked lists become ordered by ascending column indices, and the
+* elements in the column linked lists become ordered by ascending row
+* indices. */
+
+void glp_sort_matrix(glp_prob *P)
+{ GLPAIJ *aij;
+ int i, j;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_sort_matrix: P = %p; invalid problem object\n",
+ P);
+#endif
+ /* rebuild row linked lists */
+ for (i = P->m; i >= 1; i--)
+ P->row[i]->ptr = NULL;
+ for (j = P->n; j >= 1; j--)
+ { for (aij = P->col[j]->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row->i;
+ aij->r_prev = NULL;
+ aij->r_next = P->row[i]->ptr;
+ if (aij->r_next != NULL) aij->r_next->r_prev = aij;
+ P->row[i]->ptr = aij;
+ }
+ }
+ /* rebuild column linked lists */
+ for (j = P->n; j >= 1; j--)
+ P->col[j]->ptr = NULL;
+ for (i = P->m; i >= 1; i--)
+ { for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { j = aij->col->j;
+ aij->c_prev = NULL;
+ aij->c_next = P->col[j]->ptr;
+ if (aij->c_next != NULL) aij->c_next->c_prev = aij;
+ P->col[j]->ptr = aij;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_del_rows - delete rows from problem object
+*
+* SYNOPSIS
+*
+* void glp_del_rows(glp_prob *lp, int nrs, const int num[]);
+*
+* DESCRIPTION
+*
+* The routine glp_del_rows deletes rows from the specified problem
+* object. Ordinal numbers of rows to be deleted should be placed in
+* locations num[1], ..., num[nrs], where nrs > 0.
+*
+* Note that deleting rows involves changing ordinal numbers of other
+* rows remaining in the problem object. New ordinal numbers of the
+* remaining rows are assigned under the assumption that the original
+* order of rows is not changed. */
+
+void glp_del_rows(glp_prob *lp, int nrs, const int num[])
+{ glp_tree *tree = lp->tree;
+ GLPROW *row;
+ int i, k, m_new;
+ /* mark rows to be deleted */
+ if (!(1 <= nrs && nrs <= lp->m))
+ xerror("glp_del_rows: nrs = %d; invalid number of rows\n",
+ nrs);
+ for (k = 1; k <= nrs; k++)
+ { /* take the number of row to be deleted */
+ i = num[k];
+ /* obtain pointer to i-th row */
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_del_rows: num[%d] = %d; row number out of range"
+ "\n", k, i);
+ row = lp->row[i];
+ if (tree != NULL && tree->reason != 0)
+ { if (!(tree->reason == GLP_IROWGEN ||
+ tree->reason == GLP_ICUTGEN))
+ xerror("glp_del_rows: operation not allowed\n");
+ xassert(tree->curr != NULL);
+ if (row->level != tree->curr->level)
+ xerror("glp_del_rows: num[%d] = %d; invalid attempt to d"
+ "elete row created not in current subproblem\n", k,i);
+ if (row->stat != GLP_BS)
+ xerror("glp_del_rows: num[%d] = %d; invalid attempt to d"
+ "elete active row (constraint)\n", k, i);
+ tree->reinv = 1;
+ }
+ /* check that the row is not marked yet */
+ if (row->i == 0)
+ xerror("glp_del_rows: num[%d] = %d; duplicate row numbers n"
+ "ot allowed\n", k, i);
+ /* erase symbolic name assigned to the row */
+ glp_set_row_name(lp, i, NULL);
+ xassert(row->node == NULL);
+ /* erase corresponding row of the constraint matrix */
+ glp_set_mat_row(lp, i, 0, NULL, NULL);
+ xassert(row->ptr == NULL);
+ /* mark the row to be deleted */
+ row->i = 0;
+ }
+ /* delete all marked rows from the row list */
+ m_new = 0;
+ for (i = 1; i <= lp->m; i++)
+ { /* obtain pointer to i-th row */
+ row = lp->row[i];
+ /* check if the row is marked */
+ if (row->i == 0)
+ { /* it is marked, delete it */
+ dmp_free_atom(lp->pool, row, sizeof(GLPROW));
+ }
+ else
+ { /* it is not marked; keep it */
+ row->i = ++m_new;
+ lp->row[row->i] = row;
+ }
+ }
+ /* set new number of rows */
+ lp->m = m_new;
+ /* invalidate the basis factorization */
+ lp->valid = 0;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_del_cols - delete columns from problem object
+*
+* SYNOPSIS
+*
+* void glp_del_cols(glp_prob *lp, int ncs, const int num[]);
+*
+* DESCRIPTION
+*
+* The routine glp_del_cols deletes columns from the specified problem
+* object. Ordinal numbers of columns to be deleted should be placed in
+* locations num[1], ..., num[ncs], where ncs > 0.
+*
+* Note that deleting columns involves changing ordinal numbers of
+* other columns remaining in the problem object. New ordinal numbers
+* of the remaining columns are assigned under the assumption that the
+* original order of columns is not changed. */
+
+void glp_del_cols(glp_prob *lp, int ncs, const int num[])
+{ glp_tree *tree = lp->tree;
+ GLPCOL *col;
+ int j, k, n_new;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_del_cols: operation not allowed\n");
+ /* mark columns to be deleted */
+ if (!(1 <= ncs && ncs <= lp->n))
+ xerror("glp_del_cols: ncs = %d; invalid number of columns\n",
+ ncs);
+ for (k = 1; k <= ncs; k++)
+ { /* take the number of column to be deleted */
+ j = num[k];
+ /* obtain pointer to j-th column */
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_del_cols: num[%d] = %d; column number out of ra"
+ "nge", k, j);
+ col = lp->col[j];
+ /* check that the column is not marked yet */
+ if (col->j == 0)
+ xerror("glp_del_cols: num[%d] = %d; duplicate column number"
+ "s not allowed\n", k, j);
+ /* erase symbolic name assigned to the column */
+ glp_set_col_name(lp, j, NULL);
+ xassert(col->node == NULL);
+ /* erase corresponding column of the constraint matrix */
+ glp_set_mat_col(lp, j, 0, NULL, NULL);
+ xassert(col->ptr == NULL);
+ /* mark the column to be deleted */
+ col->j = 0;
+ /* if it is basic, invalidate the basis factorization */
+ if (col->stat == GLP_BS) lp->valid = 0;
+ }
+ /* delete all marked columns from the column list */
+ n_new = 0;
+ for (j = 1; j <= lp->n; j++)
+ { /* obtain pointer to j-th column */
+ col = lp->col[j];
+ /* check if the column is marked */
+ if (col->j == 0)
+ { /* it is marked; delete it */
+ dmp_free_atom(lp->pool, col, sizeof(GLPCOL));
+ }
+ else
+ { /* it is not marked; keep it */
+ col->j = ++n_new;
+ lp->col[col->j] = col;
+ }
+ }
+ /* set new number of columns */
+ lp->n = n_new;
+ /* if the basis header is still valid, adjust it */
+ if (lp->valid)
+ { int m = lp->m;
+ int *head = lp->head;
+ for (j = 1; j <= n_new; j++)
+ { k = lp->col[j]->bind;
+ if (k != 0)
+ { xassert(1 <= k && k <= m);
+ head[k] = m + j;
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_copy_prob - copy problem object content
+*
+* SYNOPSIS
+*
+* void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names);
+*
+* DESCRIPTION
+*
+* The routine glp_copy_prob copies the content of the problem object
+* prob to the problem object dest.
+*
+* The parameter names is a flag. If it is non-zero, the routine also
+* copies all symbolic names; otherwise, if it is zero, symbolic names
+* are not copied. */
+
+void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names)
+{ glp_tree *tree = dest->tree;
+ glp_bfcp bfcp;
+ int i, j, len, *ind;
+ double *val;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_copy_prob: operation not allowed\n");
+ if (dest == prob)
+ xerror("glp_copy_prob: copying problem object to itself not al"
+ "lowed\n");
+ if (!(names == GLP_ON || names == GLP_OFF))
+ xerror("glp_copy_prob: names = %d; invalid parameter\n",
+ names);
+ glp_erase_prob(dest);
+ if (names && prob->name != NULL)
+ glp_set_prob_name(dest, prob->name);
+ if (names && prob->obj != NULL)
+ glp_set_obj_name(dest, prob->obj);
+ dest->dir = prob->dir;
+ dest->c0 = prob->c0;
+ if (prob->m > 0)
+ glp_add_rows(dest, prob->m);
+ if (prob->n > 0)
+ glp_add_cols(dest, prob->n);
+ glp_get_bfcp(prob, &bfcp);
+ glp_set_bfcp(dest, &bfcp);
+ dest->pbs_stat = prob->pbs_stat;
+ dest->dbs_stat = prob->dbs_stat;
+ dest->obj_val = prob->obj_val;
+ dest->some = prob->some;
+ dest->ipt_stat = prob->ipt_stat;
+ dest->ipt_obj = prob->ipt_obj;
+ dest->mip_stat = prob->mip_stat;
+ dest->mip_obj = prob->mip_obj;
+ for (i = 1; i <= prob->m; i++)
+ { GLPROW *to = dest->row[i];
+ GLPROW *from = prob->row[i];
+ if (names && from->name != NULL)
+ glp_set_row_name(dest, i, from->name);
+ to->type = from->type;
+ to->lb = from->lb;
+ to->ub = from->ub;
+ to->rii = from->rii;
+ to->stat = from->stat;
+ to->prim = from->prim;
+ to->dual = from->dual;
+ to->pval = from->pval;
+ to->dval = from->dval;
+ to->mipx = from->mipx;
+ }
+ ind = xcalloc(1+prob->m, sizeof(int));
+ val = xcalloc(1+prob->m, sizeof(double));
+ for (j = 1; j <= prob->n; j++)
+ { GLPCOL *to = dest->col[j];
+ GLPCOL *from = prob->col[j];
+ if (names && from->name != NULL)
+ glp_set_col_name(dest, j, from->name);
+ to->kind = from->kind;
+ to->type = from->type;
+ to->lb = from->lb;
+ to->ub = from->ub;
+ to->coef = from->coef;
+ len = glp_get_mat_col(prob, j, ind, val);
+ glp_set_mat_col(dest, j, len, ind, val);
+ to->sjj = from->sjj;
+ to->stat = from->stat;
+ to->prim = from->prim;
+ to->dual = from->dual;
+ to->pval = from->pval;
+ to->dval = from->dval;
+ to->mipx = from->mipx;
+ }
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_erase_prob - erase problem object content
+*
+* SYNOPSIS
+*
+* void glp_erase_prob(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_erase_prob erases the content of the specified
+* problem object. The effect of this operation is the same as if the
+* problem object would be deleted with the routine glp_delete_prob and
+* then created anew with the routine glp_create_prob, with exception
+* that the handle (pointer) to the problem object remains valid. */
+
+static void delete_prob(glp_prob *lp);
+
+void glp_erase_prob(glp_prob *lp)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_erase_prob: operation not allowed\n");
+ delete_prob(lp);
+ create_prob(lp);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_delete_prob - delete problem object
+*
+* SYNOPSIS
+*
+* void glp_delete_prob(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_delete_prob deletes the specified problem object and
+* frees all the memory allocated to it. */
+
+static void delete_prob(glp_prob *lp)
+#if 0 /* 04/IV-2016 */
+{ lp->magic = 0x3F3F3F3F;
+#else
+{
+#endif
+ dmp_delete_pool(lp->pool);
+#if 0 /* 08/III-2014 */
+#if 0 /* 17/XI-2009 */
+ xfree(lp->cps);
+#else
+ if (lp->parms != NULL) xfree(lp->parms);
+#endif
+#endif
+ xassert(lp->tree == NULL);
+#if 0
+ if (lp->cwa != NULL) xfree(lp->cwa);
+#endif
+ xfree(lp->row);
+ xfree(lp->col);
+ if (lp->r_tree != NULL) avl_delete_tree(lp->r_tree);
+ if (lp->c_tree != NULL) avl_delete_tree(lp->c_tree);
+ xfree(lp->head);
+#if 0 /* 08/III-2014 */
+ if (lp->bfcp != NULL) xfree(lp->bfcp);
+#endif
+ if (lp->bfd != NULL) bfd_delete_it(lp->bfd);
+ return;
+}
+
+void glp_delete_prob(glp_prob *lp)
+{ glp_tree *tree = lp->tree;
+ if (tree != NULL && tree->reason != 0)
+ xerror("glp_delete_prob: operation not allowed\n");
+ delete_prob(lp);
+ xfree(lp);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob2.c b/test/monniaux/glpk-4.65/src/api/prob2.c
new file mode 100644
index 00000000..d352db12
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob2.c
@@ -0,0 +1,491 @@
+/* prob2.c (problem retrieving routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_get_prob_name - retrieve problem name
+*
+* SYNOPSIS
+*
+* const char *glp_get_prob_name(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_prob_name returns a pointer to an internal
+* buffer, which contains symbolic name of the problem. However, if the
+* problem has no assigned name, the routine returns NULL. */
+
+const char *glp_get_prob_name(glp_prob *lp)
+{ char *name;
+ name = lp->name;
+ return name;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_obj_name - retrieve objective function name
+*
+* SYNOPSIS
+*
+* const char *glp_get_obj_name(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_obj_name returns a pointer to an internal
+* buffer, which contains a symbolic name of the objective function.
+* However, if the objective function has no assigned name, the routine
+* returns NULL. */
+
+const char *glp_get_obj_name(glp_prob *lp)
+{ char *name;
+ name = lp->obj;
+ return name;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_obj_dir - retrieve optimization direction flag
+*
+* SYNOPSIS
+*
+* int glp_get_obj_dir(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_obj_dir returns the optimization direction flag
+* (i.e. "sense" of the objective function):
+*
+* GLP_MIN - minimization;
+* GLP_MAX - maximization. */
+
+int glp_get_obj_dir(glp_prob *lp)
+{ int dir = lp->dir;
+ return dir;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_num_rows - retrieve number of rows
+*
+* SYNOPSIS
+*
+* int glp_get_num_rows(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_num_rows returns the current number of rows in
+* the specified problem object. */
+
+int glp_get_num_rows(glp_prob *lp)
+{ int m = lp->m;
+ return m;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_num_cols - retrieve number of columns
+*
+* SYNOPSIS
+*
+* int glp_get_num_cols(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_num_cols returns the current number of columns
+* in the specified problem object. */
+
+int glp_get_num_cols(glp_prob *lp)
+{ int n = lp->n;
+ return n;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_name - retrieve row name
+*
+* SYNOPSIS
+*
+* const char *glp_get_row_name(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_name returns a pointer to an internal
+* buffer, which contains symbolic name of i-th row. However, if i-th
+* row has no assigned name, the routine returns NULL. */
+
+const char *glp_get_row_name(glp_prob *lp, int i)
+{ char *name;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_name: i = %d; row number out of range\n",
+ i);
+ name = lp->row[i]->name;
+ return name;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_name - retrieve column name
+*
+* SYNOPSIS
+*
+* const char *glp_get_col_name(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_name returns a pointer to an internal
+* buffer, which contains symbolic name of j-th column. However, if j-th
+* column has no assigned name, the routine returns NULL. */
+
+const char *glp_get_col_name(glp_prob *lp, int j)
+{ char *name;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_name: j = %d; column number out of range\n"
+ , j);
+ name = lp->col[j]->name;
+ return name;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_type - retrieve row type
+*
+* SYNOPSIS
+*
+* int glp_get_row_type(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_type returns the type of i-th row, i.e. the
+* type of corresponding auxiliary variable, as follows:
+*
+* GLP_FR - free (unbounded) variable;
+* GLP_LO - variable with lower bound;
+* GLP_UP - variable with upper bound;
+* GLP_DB - double-bounded variable;
+* GLP_FX - fixed variable. */
+
+int glp_get_row_type(glp_prob *lp, int i)
+{ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_type: i = %d; row number out of range\n",
+ i);
+ return lp->row[i]->type;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_lb - retrieve row lower bound
+*
+* SYNOPSIS
+*
+* double glp_get_row_lb(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_lb returns the lower bound of i-th row, i.e.
+* the lower bound of corresponding auxiliary variable. However, if the
+* row has no lower bound, the routine returns -DBL_MAX. */
+
+double glp_get_row_lb(glp_prob *lp, int i)
+{ double lb;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_lb: i = %d; row number out of range\n", i);
+ switch (lp->row[i]->type)
+ { case GLP_FR:
+ case GLP_UP:
+ lb = -DBL_MAX; break;
+ case GLP_LO:
+ case GLP_DB:
+ case GLP_FX:
+ lb = lp->row[i]->lb; break;
+ default:
+ xassert(lp != lp);
+ }
+ return lb;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_ub - retrieve row upper bound
+*
+* SYNOPSIS
+*
+* double glp_get_row_ub(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_ub returns the upper bound of i-th row, i.e.
+* the upper bound of corresponding auxiliary variable. However, if the
+* row has no upper bound, the routine returns +DBL_MAX. */
+
+double glp_get_row_ub(glp_prob *lp, int i)
+{ double ub;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_ub: i = %d; row number out of range\n", i);
+ switch (lp->row[i]->type)
+ { case GLP_FR:
+ case GLP_LO:
+ ub = +DBL_MAX; break;
+ case GLP_UP:
+ case GLP_DB:
+ case GLP_FX:
+ ub = lp->row[i]->ub; break;
+ default:
+ xassert(lp != lp);
+ }
+ return ub;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_type - retrieve column type
+*
+* SYNOPSIS
+*
+* int glp_get_col_type(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_type returns the type of j-th column, i.e.
+* the type of corresponding structural variable, as follows:
+*
+* GLP_FR - free (unbounded) variable;
+* GLP_LO - variable with lower bound;
+* GLP_UP - variable with upper bound;
+* GLP_DB - double-bounded variable;
+* GLP_FX - fixed variable. */
+
+int glp_get_col_type(glp_prob *lp, int j)
+{ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_type: j = %d; column number out of range\n"
+ , j);
+ return lp->col[j]->type;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_lb - retrieve column lower bound
+*
+* SYNOPSIS
+*
+* double glp_get_col_lb(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_lb returns the lower bound of j-th column,
+* i.e. the lower bound of corresponding structural variable. However,
+* if the column has no lower bound, the routine returns -DBL_MAX. */
+
+double glp_get_col_lb(glp_prob *lp, int j)
+{ double lb;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_lb: j = %d; column number out of range\n",
+ j);
+ switch (lp->col[j]->type)
+ { case GLP_FR:
+ case GLP_UP:
+ lb = -DBL_MAX; break;
+ case GLP_LO:
+ case GLP_DB:
+ case GLP_FX:
+ lb = lp->col[j]->lb; break;
+ default:
+ xassert(lp != lp);
+ }
+ return lb;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_ub - retrieve column upper bound
+*
+* SYNOPSIS
+*
+* double glp_get_col_ub(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_ub returns the upper bound of j-th column,
+* i.e. the upper bound of corresponding structural variable. However,
+* if the column has no upper bound, the routine returns +DBL_MAX. */
+
+double glp_get_col_ub(glp_prob *lp, int j)
+{ double ub;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_ub: j = %d; column number out of range\n",
+ j);
+ switch (lp->col[j]->type)
+ { case GLP_FR:
+ case GLP_LO:
+ ub = +DBL_MAX; break;
+ case GLP_UP:
+ case GLP_DB:
+ case GLP_FX:
+ ub = lp->col[j]->ub; break;
+ default:
+ xassert(lp != lp);
+ }
+ return ub;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_obj_coef - retrieve obj. coefficient or constant term
+*
+* SYNOPSIS
+*
+* double glp_get_obj_coef(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_obj_coef returns the objective coefficient at
+* j-th structural variable (column) of the specified problem object.
+*
+* If the parameter j is zero, the routine returns the constant term
+* ("shift") of the objective function. */
+
+double glp_get_obj_coef(glp_prob *lp, int j)
+{ if (!(0 <= j && j <= lp->n))
+ xerror("glp_get_obj_coef: j = %d; column number out of range\n"
+ , j);
+ return j == 0 ? lp->c0 : lp->col[j]->coef;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_num_nz - retrieve number of constraint coefficients
+*
+* SYNOPSIS
+*
+* int glp_get_num_nz(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_num_nz returns the number of (non-zero) elements
+* in the constraint matrix of the specified problem object. */
+
+int glp_get_num_nz(glp_prob *lp)
+{ int nnz = lp->nnz;
+ return nnz;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_mat_row - retrieve row of the constraint matrix
+*
+* SYNOPSIS
+*
+* int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_get_mat_row scans (non-zero) elements of i-th row
+* of the constraint matrix of the specified problem object and stores
+* their column indices and numeric values to locations ind[1], ...,
+* ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= n
+* is the number of elements in i-th row, n is the number of columns.
+*
+* The parameter ind and/or val can be specified as NULL, in which case
+* corresponding information is not stored.
+*
+* RETURNS
+*
+* The routine glp_get_mat_row returns the length len, i.e. the number
+* of (non-zero) elements in i-th row. */
+
+int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[])
+{ GLPAIJ *aij;
+ int len;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_mat_row: i = %d; row number out of range\n",
+ i);
+ len = 0;
+ for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { len++;
+ if (ind != NULL) ind[len] = aij->col->j;
+ if (val != NULL) val[len] = aij->val;
+ }
+ xassert(len <= lp->n);
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_mat_col - retrieve column of the constraint matrix
+*
+* SYNOPSIS
+*
+* int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_get_mat_col scans (non-zero) elements of j-th column
+* of the constraint matrix of the specified problem object and stores
+* their row indices and numeric values to locations ind[1], ...,
+* ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= m
+* is the number of elements in j-th column, m is the number of rows.
+*
+* The parameter ind or/and val can be specified as NULL, in which case
+* corresponding information is not stored.
+*
+* RETURNS
+*
+* The routine glp_get_mat_col returns the length len, i.e. the number
+* of (non-zero) elements in j-th column. */
+
+int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[])
+{ GLPAIJ *aij;
+ int len;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_mat_col: j = %d; column number out of range\n",
+ j);
+ len = 0;
+ for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
+ { len++;
+ if (ind != NULL) ind[len] = aij->row->i;
+ if (val != NULL) val[len] = aij->val;
+ }
+ xassert(len <= lp->m);
+ return len;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob3.c b/test/monniaux/glpk-4.65/src/api/prob3.c
new file mode 100644
index 00000000..d7edbd33
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob3.c
@@ -0,0 +1,166 @@
+/* prob3.c (problem row/column searching routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_create_index - create the name index
+*
+* SYNOPSIS
+*
+* void glp_create_index(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_create_index creates the name index for the
+* specified problem object. The name index is an auxiliary data
+* structure, which is intended to quickly (i.e. for logarithmic time)
+* find rows and columns by their names.
+*
+* This routine can be called at any time. If the name index already
+* exists, the routine does nothing. */
+
+void glp_create_index(glp_prob *lp)
+{ GLPROW *row;
+ GLPCOL *col;
+ int i, j;
+ /* create row name index */
+ if (lp->r_tree == NULL)
+ { lp->r_tree = avl_create_tree(avl_strcmp, NULL);
+ for (i = 1; i <= lp->m; i++)
+ { row = lp->row[i];
+ xassert(row->node == NULL);
+ if (row->name != NULL)
+ { row->node = avl_insert_node(lp->r_tree, row->name);
+ avl_set_node_link(row->node, row);
+ }
+ }
+ }
+ /* create column name index */
+ if (lp->c_tree == NULL)
+ { lp->c_tree = avl_create_tree(avl_strcmp, NULL);
+ for (j = 1; j <= lp->n; j++)
+ { col = lp->col[j];
+ xassert(col->node == NULL);
+ if (col->name != NULL)
+ { col->node = avl_insert_node(lp->c_tree, col->name);
+ avl_set_node_link(col->node, col);
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_find_row - find row by its name
+*
+* SYNOPSIS
+*
+* int glp_find_row(glp_prob *lp, const char *name);
+*
+* RETURNS
+*
+* The routine glp_find_row returns the ordinal number of a row,
+* which is assigned (by the routine glp_set_row_name) the specified
+* symbolic name. If no such row exists, the routine returns 0. */
+
+int glp_find_row(glp_prob *lp, const char *name)
+{ AVLNODE *node;
+ int i = 0;
+ if (lp->r_tree == NULL)
+ xerror("glp_find_row: row name index does not exist\n");
+ if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
+ { node = avl_find_node(lp->r_tree, name);
+ if (node != NULL)
+ i = ((GLPROW *)avl_get_node_link(node))->i;
+ }
+ return i;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_find_col - find column by its name
+*
+* SYNOPSIS
+*
+* int glp_find_col(glp_prob *lp, const char *name);
+*
+* RETURNS
+*
+* The routine glp_find_col returns the ordinal number of a column,
+* which is assigned (by the routine glp_set_col_name) the specified
+* symbolic name. If no such column exists, the routine returns 0. */
+
+int glp_find_col(glp_prob *lp, const char *name)
+{ AVLNODE *node;
+ int j = 0;
+ if (lp->c_tree == NULL)
+ xerror("glp_find_col: column name index does not exist\n");
+ if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
+ { node = avl_find_node(lp->c_tree, name);
+ if (node != NULL)
+ j = ((GLPCOL *)avl_get_node_link(node))->j;
+ }
+ return j;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_delete_index - delete the name index
+*
+* SYNOPSIS
+*
+* void glp_delete_index(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_delete_index deletes the name index previously
+* created by the routine glp_create_index and frees the memory
+* allocated to this auxiliary data structure.
+*
+* This routine can be called at any time. If the name index does not
+* exist, the routine does nothing. */
+
+void glp_delete_index(glp_prob *lp)
+{ int i, j;
+ /* delete row name index */
+ if (lp->r_tree != NULL)
+ { for (i = 1; i <= lp->m; i++) lp->row[i]->node = NULL;
+ avl_delete_tree(lp->r_tree), lp->r_tree = NULL;
+ }
+ /* delete column name index */
+ if (lp->c_tree != NULL)
+ { for (j = 1; j <= lp->n; j++) lp->col[j]->node = NULL;
+ avl_delete_tree(lp->c_tree), lp->c_tree = NULL;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob4.c b/test/monniaux/glpk-4.65/src/api/prob4.c
new file mode 100644
index 00000000..8c2b5ae5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob4.c
@@ -0,0 +1,156 @@
+/* prob4.c (problem scaling routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_set_rii - set (change) row scale factor
+*
+* SYNOPSIS
+*
+* void glp_set_rii(glp_prob *lp, int i, double rii);
+*
+* DESCRIPTION
+*
+* The routine glp_set_rii sets (changes) the scale factor r[i,i] for
+* i-th row of the specified problem object. */
+
+void glp_set_rii(glp_prob *lp, int i, double rii)
+{ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_rii: i = %d; row number out of range\n", i);
+ if (rii <= 0.0)
+ xerror("glp_set_rii: i = %d; rii = %g; invalid scale factor\n",
+ i, rii);
+ if (lp->valid && lp->row[i]->rii != rii)
+ { GLPAIJ *aij;
+ for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->col->stat == GLP_BS)
+ { /* invalidate the basis factorization */
+ lp->valid = 0;
+ break;
+ }
+ }
+ }
+ lp->row[i]->rii = rii;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set sjj - set (change) column scale factor
+*
+* SYNOPSIS
+*
+* void glp_set_sjj(glp_prob *lp, int j, double sjj);
+*
+* DESCRIPTION
+*
+* The routine glp_set_sjj sets (changes) the scale factor s[j,j] for
+* j-th column of the specified problem object. */
+
+void glp_set_sjj(glp_prob *lp, int j, double sjj)
+{ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_sjj: j = %d; column number out of range\n", j);
+ if (sjj <= 0.0)
+ xerror("glp_set_sjj: j = %d; sjj = %g; invalid scale factor\n",
+ j, sjj);
+ if (lp->valid && lp->col[j]->sjj != sjj && lp->col[j]->stat ==
+ GLP_BS)
+ { /* invalidate the basis factorization */
+ lp->valid = 0;
+ }
+ lp->col[j]->sjj = sjj;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_rii - retrieve row scale factor
+*
+* SYNOPSIS
+*
+* double glp_get_rii(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_rii returns current scale factor r[i,i] for i-th
+* row of the specified problem object. */
+
+double glp_get_rii(glp_prob *lp, int i)
+{ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_rii: i = %d; row number out of range\n", i);
+ return lp->row[i]->rii;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_sjj - retrieve column scale factor
+*
+* SYNOPSIS
+*
+* double glp_get_sjj(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_sjj returns current scale factor s[j,j] for j-th
+* column of the specified problem object. */
+
+double glp_get_sjj(glp_prob *lp, int j)
+{ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_sjj: j = %d; column number out of range\n", j);
+ return lp->col[j]->sjj;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_unscale_prob - unscale problem data
+*
+* SYNOPSIS
+*
+* void glp_unscale_prob(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_unscale_prob performs unscaling of problem data for
+* the specified problem object.
+*
+* "Unscaling" means replacing the current scaling matrices R and S by
+* unity matrices that cancels the scaling effect. */
+
+void glp_unscale_prob(glp_prob *lp)
+{ int m = glp_get_num_rows(lp);
+ int n = glp_get_num_cols(lp);
+ int i, j;
+ for (i = 1; i <= m; i++) glp_set_rii(lp, i, 1.0);
+ for (j = 1; j <= n; j++) glp_set_sjj(lp, j, 1.0);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prob5.c b/test/monniaux/glpk-4.65/src/api/prob5.c
new file mode 100644
index 00000000..1c1d3160
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prob5.c
@@ -0,0 +1,168 @@
+/* prob5.c (LP problem basis constructing routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_set_row_stat - set (change) row status
+*
+* SYNOPSIS
+*
+* void glp_set_row_stat(glp_prob *lp, int i, int stat);
+*
+* DESCRIPTION
+*
+* The routine glp_set_row_stat sets (changes) status of the auxiliary
+* variable associated with i-th row.
+*
+* The new status of the auxiliary variable should be specified by the
+* parameter stat as follows:
+*
+* GLP_BS - basic variable;
+* GLP_NL - non-basic variable;
+* GLP_NU - non-basic variable on its upper bound; if the variable is
+* not double-bounded, this means the same as GLP_NL (only in
+* case of this routine);
+* GLP_NF - the same as GLP_NL (only in case of this routine);
+* GLP_NS - the same as GLP_NL (only in case of this routine). */
+
+void glp_set_row_stat(glp_prob *lp, int i, int stat)
+{ GLPROW *row;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_set_row_stat: i = %d; row number out of range\n",
+ i);
+ if (!(stat == GLP_BS || stat == GLP_NL || stat == GLP_NU ||
+ stat == GLP_NF || stat == GLP_NS))
+ xerror("glp_set_row_stat: i = %d; stat = %d; invalid status\n",
+ i, stat);
+ row = lp->row[i];
+ if (stat != GLP_BS)
+ { switch (row->type)
+ { case GLP_FR: stat = GLP_NF; break;
+ case GLP_LO: stat = GLP_NL; break;
+ case GLP_UP: stat = GLP_NU; break;
+ case GLP_DB: if (stat != GLP_NU) stat = GLP_NL; break;
+ case GLP_FX: stat = GLP_NS; break;
+ default: xassert(row != row);
+ }
+ }
+ if (row->stat == GLP_BS && stat != GLP_BS ||
+ row->stat != GLP_BS && stat == GLP_BS)
+ { /* invalidate the basis factorization */
+ lp->valid = 0;
+ }
+ row->stat = stat;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_set_col_stat - set (change) column status
+*
+* SYNOPSIS
+*
+* void glp_set_col_stat(glp_prob *lp, int j, int stat);
+*
+* DESCRIPTION
+*
+* The routine glp_set_col_stat sets (changes) status of the structural
+* variable associated with j-th column.
+*
+* The new status of the structural variable should be specified by the
+* parameter stat as follows:
+*
+* GLP_BS - basic variable;
+* GLP_NL - non-basic variable;
+* GLP_NU - non-basic variable on its upper bound; if the variable is
+* not double-bounded, this means the same as GLP_NL (only in
+* case of this routine);
+* GLP_NF - the same as GLP_NL (only in case of this routine);
+* GLP_NS - the same as GLP_NL (only in case of this routine). */
+
+void glp_set_col_stat(glp_prob *lp, int j, int stat)
+{ GLPCOL *col;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_set_col_stat: j = %d; column number out of range\n"
+ , j);
+ if (!(stat == GLP_BS || stat == GLP_NL || stat == GLP_NU ||
+ stat == GLP_NF || stat == GLP_NS))
+ xerror("glp_set_col_stat: j = %d; stat = %d; invalid status\n",
+ j, stat);
+ col = lp->col[j];
+ if (stat != GLP_BS)
+ { switch (col->type)
+ { case GLP_FR: stat = GLP_NF; break;
+ case GLP_LO: stat = GLP_NL; break;
+ case GLP_UP: stat = GLP_NU; break;
+ case GLP_DB: if (stat != GLP_NU) stat = GLP_NL; break;
+ case GLP_FX: stat = GLP_NS; break;
+ default: xassert(col != col);
+ }
+ }
+ if (col->stat == GLP_BS && stat != GLP_BS ||
+ col->stat != GLP_BS && stat == GLP_BS)
+ { /* invalidate the basis factorization */
+ lp->valid = 0;
+ }
+ col->stat = stat;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_std_basis - construct standard initial LP basis
+*
+* SYNOPSIS
+*
+* void glp_std_basis(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_std_basis builds the "standard" (trivial) initial
+* basis for the specified problem object.
+*
+* In the "standard" basis all auxiliary variables are basic, and all
+* structural variables are non-basic. */
+
+void glp_std_basis(glp_prob *lp)
+{ int i, j;
+ /* make all auxiliary variables basic */
+ for (i = 1; i <= lp->m; i++)
+ glp_set_row_stat(lp, i, GLP_BS);
+ /* make all structural variables non-basic */
+ for (j = 1; j <= lp->n; j++)
+ { GLPCOL *col = lp->col[j];
+ if (col->type == GLP_DB && fabs(col->lb) > fabs(col->ub))
+ glp_set_col_stat(lp, j, GLP_NU);
+ else
+ glp_set_col_stat(lp, j, GLP_NL);
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prrngs.c b/test/monniaux/glpk-4.65/src/api/prrngs.c
new file mode 100644
index 00000000..41a141ff
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prrngs.c
@@ -0,0 +1,302 @@
+/* prrngs.c (print sensitivity analysis report) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+static char *format(char buf[13+1], double x)
+{ /* format floating-point number in MPS/360-like style */
+ if (x == -DBL_MAX)
+ strcpy(buf, " -Inf");
+ else if (x == +DBL_MAX)
+ strcpy(buf, " +Inf");
+ else if (fabs(x) <= 999999.99998)
+ { sprintf(buf, "%13.5f", x);
+#if 1
+ if (strcmp(buf, " 0.00000") == 0 ||
+ strcmp(buf, " -0.00000") == 0)
+ strcpy(buf, " . ");
+ else if (memcmp(buf, " 0.", 8) == 0)
+ memcpy(buf, " .", 8);
+ else if (memcmp(buf, " -0.", 8) == 0)
+ memcpy(buf, " -.", 8);
+#endif
+ }
+ else
+ sprintf(buf, "%13.6g", x);
+ return buf;
+}
+
+int glp_print_ranges(glp_prob *P, int len, const int list[],
+ int flags, const char *fname)
+{ /* print sensitivity analysis report */
+ glp_file *fp = NULL;
+ GLPROW *row;
+ GLPCOL *col;
+ int m, n, pass, k, t, numb, type, stat, var1, var2, count, page,
+ ret;
+ double lb, ub, slack, coef, prim, dual, value1, value2, coef1,
+ coef2, obj1, obj2;
+ const char *name, *limit;
+ char buf[13+1];
+ /* sanity checks */
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_print_ranges: P = %p; invalid problem object\n",
+ P);
+#endif
+ m = P->m, n = P->n;
+ if (len < 0)
+ xerror("glp_print_ranges: len = %d; invalid list length\n",
+ len);
+ if (len > 0)
+ { if (list == NULL)
+ xerror("glp_print_ranges: list = %p: invalid parameter\n",
+ list);
+ for (t = 1; t <= len; t++)
+ { k = list[t];
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_print_ranges: list[%d] = %d; row/column numb"
+ "er out of range\n", t, k);
+ }
+ }
+ if (flags != 0)
+ xerror("glp_print_ranges: flags = %d; invalid parameter\n",
+ flags);
+ if (fname == NULL)
+ xerror("glp_print_ranges: fname = %p; invalid parameter\n",
+ fname);
+ if (glp_get_status(P) != GLP_OPT)
+ { xprintf("glp_print_ranges: optimal basic solution required\n");
+ ret = 1;
+ goto done;
+ }
+ if (!glp_bf_exists(P))
+ { xprintf("glp_print_ranges: basis factorization required\n");
+ ret = 2;
+ goto done;
+ }
+ /* start reporting */
+ xprintf("Write sensitivity analysis report to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 3;
+ goto done;
+ }
+ page = count = 0;
+ for (pass = 1; pass <= 2; pass++)
+ for (t = 1; t <= (len == 0 ? m+n : len); t++)
+ { if (t == 1) count = 0;
+ k = (len == 0 ? t : list[t]);
+ if (pass == 1 && k > m || pass == 2 && k <= m)
+ continue;
+ if (count == 0)
+ { xfprintf(fp, "GLPK %-4s - SENSITIVITY ANALYSIS REPORT%73sPa"
+ "ge%4d\n", glp_version(), "", ++page);
+ xfprintf(fp, "\n");
+ xfprintf(fp, "%-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name);
+ xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->obj_val,
+ P->dir == GLP_MIN ? "MINimum" :
+ P->dir == GLP_MAX ? "MAXimum" : "???");
+ xfprintf(fp, "\n");
+ xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s "
+ "%s\n", "No.", pass == 1 ? "Row name" : "Column name",
+ "St", "Activity", pass == 1 ? "Slack" : "Obj coef",
+ "Lower bound", "Activity", "Obj coef", "Obj value at",
+ "Limiting");
+ xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s "
+ "%s\n", "", "", "", "", "Marginal", "Upper bound",
+ "range", "range", "break point", "variable");
+ xfprintf(fp, "------ ------------ -- ------------- --------"
+ "----- ------------- ------------- ------------- ------"
+ "------- ------------\n");
+ }
+ if (pass == 1)
+ { numb = k;
+ xassert(1 <= numb && numb <= m);
+ row = P->row[numb];
+ name = row->name;
+ type = row->type;
+ lb = glp_get_row_lb(P, numb);
+ ub = glp_get_row_ub(P, numb);
+ coef = 0.0;
+ stat = row->stat;
+ prim = row->prim;
+ if (type == GLP_FR)
+ slack = - prim;
+ else if (type == GLP_LO)
+ slack = lb - prim;
+ else if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ slack = ub - prim;
+ dual = row->dual;
+ }
+ else
+ { numb = k - m;
+ xassert(1 <= numb && numb <= n);
+ col = P->col[numb];
+ name = col->name;
+ lb = glp_get_col_lb(P, numb);
+ ub = glp_get_col_ub(P, numb);
+ coef = col->coef;
+ stat = col->stat;
+ prim = col->prim;
+ slack = 0.0;
+ dual = col->dual;
+ }
+ if (stat != GLP_BS)
+ { glp_analyze_bound(P, k, &value1, &var1, &value2, &var2);
+ if (stat == GLP_NF)
+ coef1 = coef2 = coef;
+ else if (stat == GLP_NS)
+ coef1 = -DBL_MAX, coef2 = +DBL_MAX;
+ else if (stat == GLP_NL && P->dir == GLP_MIN ||
+ stat == GLP_NU && P->dir == GLP_MAX)
+ coef1 = coef - dual, coef2 = +DBL_MAX;
+ else
+ coef1 = -DBL_MAX, coef2 = coef - dual;
+ if (value1 == -DBL_MAX)
+ { if (dual < -1e-9)
+ obj1 = +DBL_MAX;
+ else if (dual > +1e-9)
+ obj1 = -DBL_MAX;
+ else
+ obj1 = P->obj_val;
+ }
+ else
+ obj1 = P->obj_val + dual * (value1 - prim);
+ if (value2 == +DBL_MAX)
+ { if (dual < -1e-9)
+ obj2 = -DBL_MAX;
+ else if (dual > +1e-9)
+ obj2 = +DBL_MAX;
+ else
+ obj2 = P->obj_val;
+ }
+ else
+ obj2 = P->obj_val + dual * (value2 - prim);
+ }
+ else
+ { glp_analyze_coef(P, k, &coef1, &var1, &value1, &coef2,
+ &var2, &value2);
+ if (coef1 == -DBL_MAX)
+ { if (prim < -1e-9)
+ obj1 = +DBL_MAX;
+ else if (prim > +1e-9)
+ obj1 = -DBL_MAX;
+ else
+ obj1 = P->obj_val;
+ }
+ else
+ obj1 = P->obj_val + (coef1 - coef) * prim;
+ if (coef2 == +DBL_MAX)
+ { if (prim < -1e-9)
+ obj2 = -DBL_MAX;
+ else if (prim > +1e-9)
+ obj2 = +DBL_MAX;
+ else
+ obj2 = P->obj_val;
+ }
+ else
+ obj2 = P->obj_val + (coef2 - coef) * prim;
+ }
+ /*** first line ***/
+ /* row/column number */
+ xfprintf(fp, "%6d", numb);
+ /* row/column name */
+ xfprintf(fp, " %-12.12s", name == NULL ? "" : name);
+ if (name != NULL && strlen(name) > 12)
+ xfprintf(fp, "%s\n%6s %12s", name+12, "", "");
+ /* row/column status */
+ xfprintf(fp, " %2s",
+ stat == GLP_BS ? "BS" : stat == GLP_NL ? "NL" :
+ stat == GLP_NU ? "NU" : stat == GLP_NF ? "NF" :
+ stat == GLP_NS ? "NS" : "??");
+ /* row/column activity */
+ xfprintf(fp, " %s", format(buf, prim));
+ /* row slack, column objective coefficient */
+ xfprintf(fp, " %s", format(buf, k <= m ? slack : coef));
+ /* row/column lower bound */
+ xfprintf(fp, " %s", format(buf, lb));
+ /* row/column activity range */
+ xfprintf(fp, " %s", format(buf, value1));
+ /* row/column objective coefficient range */
+ xfprintf(fp, " %s", format(buf, coef1));
+ /* objective value at break point */
+ xfprintf(fp, " %s", format(buf, obj1));
+ /* limiting variable name */
+ if (var1 != 0)
+ { if (var1 <= m)
+ limit = glp_get_row_name(P, var1);
+ else
+ limit = glp_get_col_name(P, var1 - m);
+ if (limit != NULL)
+ xfprintf(fp, " %s", limit);
+ }
+ xfprintf(fp, "\n");
+ /*** second line ***/
+ xfprintf(fp, "%6s %-12s %2s %13s", "", "", "", "");
+ /* row/column reduced cost */
+ xfprintf(fp, " %s", format(buf, dual));
+ /* row/column upper bound */
+ xfprintf(fp, " %s", format(buf, ub));
+ /* row/column activity range */
+ xfprintf(fp, " %s", format(buf, value2));
+ /* row/column objective coefficient range */
+ xfprintf(fp, " %s", format(buf, coef2));
+ /* objective value at break point */
+ xfprintf(fp, " %s", format(buf, obj2));
+ /* limiting variable name */
+ if (var2 != 0)
+ { if (var2 <= m)
+ limit = glp_get_row_name(P, var2);
+ else
+ limit = glp_get_col_name(P, var2 - m);
+ if (limit != NULL)
+ xfprintf(fp, " %s", limit);
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, "\n");
+ /* print 10 items per page */
+ count = (count + 1) % 10;
+ }
+ xfprintf(fp, "End of report\n");
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 4;
+ goto done;
+ }
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/prsol.c b/test/monniaux/glpk-4.65/src/api/prsol.c
new file mode 100644
index 00000000..d785dc2e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/prsol.c
@@ -0,0 +1,202 @@
+/* prsol.c (write basic solution in printable format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+int glp_print_sol(glp_prob *P, const char *fname)
+{ /* write basic solution in printable format */
+ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, t, ae_ind, re_ind, ret;
+ double ae_max, re_max;
+ xprintf("Writing basic solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "%-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name);
+ xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
+ xfprintf(fp, "%-12s%d\n", "Columns:", P->n);
+ xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
+ t = glp_get_status(P);
+ xfprintf(fp, "%-12s%s\n", "Status:",
+ t == GLP_OPT ? "OPTIMAL" :
+ t == GLP_FEAS ? "FEASIBLE" :
+ t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
+ t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" :
+ t == GLP_UNBND ? "UNBOUNDED" :
+ t == GLP_UNDEF ? "UNDEFINED" : "???");
+ xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->obj_val,
+ P->dir == GLP_MIN ? "MINimum" :
+ P->dir == GLP_MAX ? "MAXimum" : "???");
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Row name St Activity Lower bound "
+ " Upper bound Marginal\n");
+ xfprintf(fp, "------ ------------ -- ------------- ------------- "
+ "------------- -------------\n");
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ xfprintf(fp, "%6d ", i);
+ if (row->name == NULL || strlen(row->name) <= 12)
+ xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
+ else
+ xfprintf(fp, "%s\n%20s", row->name, "");
+ xfprintf(fp, "%s ",
+ row->stat == GLP_BS ? "B " :
+ row->stat == GLP_NL ? "NL" :
+ row->stat == GLP_NU ? "NU" :
+ row->stat == GLP_NF ? "NF" :
+ row->stat == GLP_NS ? "NS" : "??");
+ xfprintf(fp, "%13.6g ",
+ fabs(row->prim) <= 1e-9 ? 0.0 : row->prim);
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", row->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (row->type == GLP_UP || row->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", row->ub);
+ else
+ xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
+ if (row->stat != GLP_BS)
+ { if (fabs(row->dual) <= 1e-9)
+ xfprintf(fp, "%13s", "< eps");
+ else
+ xfprintf(fp, "%13.6g ", row->dual);
+ }
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, " No. Column name St Activity Lower bound "
+ " Upper bound Marginal\n");
+ xfprintf(fp, "------ ------------ -- ------------- ------------- "
+ "------------- -------------\n");
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ xfprintf(fp, "%6d ", j);
+ if (col->name == NULL || strlen(col->name) <= 12)
+ xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
+ else
+ xfprintf(fp, "%s\n%20s", col->name, "");
+ xfprintf(fp, "%s ",
+ col->stat == GLP_BS ? "B " :
+ col->stat == GLP_NL ? "NL" :
+ col->stat == GLP_NU ? "NU" :
+ col->stat == GLP_NF ? "NF" :
+ col->stat == GLP_NS ? "NS" : "??");
+ xfprintf(fp, "%13.6g ",
+ fabs(col->prim) <= 1e-9 ? 0.0 : col->prim);
+ if (col->type == GLP_LO || col->type == GLP_DB ||
+ col->type == GLP_FX)
+ xfprintf(fp, "%13.6g ", col->lb);
+ else
+ xfprintf(fp, "%13s ", "");
+ if (col->type == GLP_UP || col->type == GLP_DB)
+ xfprintf(fp, "%13.6g ", col->ub);
+ else
+ xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
+ if (col->stat != GLP_BS)
+ { if (fabs(col->dual) <= 1e-9)
+ xfprintf(fp, "%13s", "< eps");
+ else
+ xfprintf(fp, "%13.6g ", col->dual);
+ }
+ xfprintf(fp, "\n");
+ }
+ xfprintf(fp, "\n");
+ xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_SOL, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
+ ae_max, ae_ind);
+ xfprintf(fp, " max.rel.err = %.2e on row %d\n",
+ re_max, re_ind);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_SOL, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
+ ae_max, ae_ind <= P->m ? "row" : "column",
+ ae_ind <= P->m ? ae_ind : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
+ re_max, re_ind <= P->m ? "row" : "column",
+ re_ind <= P->m ? re_ind : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
+ "E");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_SOL, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
+ ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on column %d\n",
+ re_max, re_ind == 0 ? 0 : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG");
+ xfprintf(fp, "\n");
+ glp_check_kkt(P, GLP_SOL, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
+ &re_ind);
+ xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
+ ae_max, ae_ind <= P->m ? "row" : "column",
+ ae_ind <= P->m ? ae_ind : ae_ind - P->m);
+ xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
+ re_max, re_ind <= P->m ? "row" : "column",
+ re_ind <= P->m ? re_ind : re_ind - P->m);
+ xfprintf(fp, "%8s%s\n", "",
+ re_max <= 1e-9 ? "High quality" :
+ re_max <= 1e-6 ? "Medium quality" :
+ re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
+ ;
+ xfprintf(fp, "\n");
+ xfprintf(fp, "End of output\n");
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdasn.c b/test/monniaux/glpk-4.65/src/api/rdasn.c
new file mode 100644
index 00000000..05dcb9fc
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdasn.c
@@ -0,0 +1,164 @@
+/* rdasn.c (read assignment problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "glpk.h"
+#include "misc.h"
+
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+/***********************************************************************
+* NAME
+*
+* glp_read_asnprob - read assignment problem data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_read_asnprob(glp_graph *G, int v_set, int a_cost,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_asnprob reads assignment problem data in DIMACS
+* format from a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_asnprob(glp_graph *G, int v_set, int a_cost, const char
+ *fname)
+{ DMX _csa, *csa = &_csa;
+ glp_vertex *v;
+ glp_arc *a;
+ int nv, na, n1, i, j, k, ret = 0;
+ double cost;
+ char *flag = NULL;
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_read_asnprob: v_set = %d; invalid offset\n",
+ v_set);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_read_asnprob: a_cost = %d; invalid offset\n",
+ a_cost);
+ glp_erase_graph(G, G->v_size, G->a_size);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading assignment problem data from '%s'...\n", fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "asn") != 0)
+ error(csa, "wrong problem designator; 'asn' expected");
+ read_field(csa);
+ if (!(str2int(csa->field, &nv) == 0 && nv >= 0))
+ error(csa, "number of nodes missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &na) == 0 && na >= 0))
+ error(csa, "number of arcs missing or invalid");
+ if (nv > 0) glp_add_vertices(G, nv);
+ end_of_line(csa);
+ /* read node descriptor lines */
+ flag = xcalloc(1+nv, sizeof(char));
+ memset(&flag[1], 0, nv * sizeof(char));
+ n1 = 0;
+ for (;;)
+ { read_designator(csa);
+ if (strcmp(csa->field, "n") != 0) break;
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "node number %d out of range", i);
+ if (flag[i])
+ error(csa, "duplicate descriptor of node %d", i);
+ flag[i] = 1, n1++;
+ end_of_line(csa);
+ }
+ xprintf(
+ "Assignment problem has %d + %d = %d node%s and %d arc%s\n",
+ n1, nv - n1, nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s");
+ if (v_set >= 0)
+ { for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ k = (flag[i] ? 0 : 1);
+ memcpy((char *)v->data + v_set, &k, sizeof(int));
+ }
+ }
+ /* read arc descriptor lines */
+ for (k = 1; k <= na; k++)
+ { if (k > 1) read_designator(csa);
+ if (strcmp(csa->field, "a") != 0)
+ error(csa, "wrong line designator; 'a' expected");
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "starting node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "starting node number %d out of range", i);
+ if (!flag[i])
+ error(csa, "node %d cannot be a starting node", i);
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "ending node number missing or invalid");
+ if (!(1 <= j && j <= nv))
+ error(csa, "ending node number %d out of range", j);
+ if (flag[j])
+ error(csa, "node %d cannot be an ending node", j);
+ read_field(csa);
+ if (str2num(csa->field, &cost) != 0)
+ error(csa, "arc cost missing or invalid");
+ check_int(csa, cost);
+ a = glp_add_arc(G, i, j);
+ if (a_cost >= 0)
+ memcpy((char *)a->data + a_cost, &cost, sizeof(double));
+ end_of_line(csa);
+ }
+ xprintf("%d lines were read\n", csa->count);
+done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
+ if (csa->fp != NULL) glp_close(csa->fp);
+ if (flag != NULL) xfree(flag);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdcc.c b/test/monniaux/glpk-4.65/src/api/rdcc.c
new file mode 100644
index 00000000..c63d60d8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdcc.c
@@ -0,0 +1,162 @@
+/* rdcc.c (read graph in DIMACS clique/coloring format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "glpk.h"
+#include "misc.h"
+
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+/***********************************************************************
+* NAME
+*
+* glp_read_ccdata - read graph in DIMACS clique/coloring format
+*
+* SYNOPSIS
+*
+* int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_ccdata reads an (undirected) graph in DIMACS
+* clique/coloring format from a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname)
+{ DMX _csa, *csa = &_csa;
+ glp_vertex *v;
+ int i, j, k, nv, ne, ret = 0;
+ double w;
+ char *flag = NULL;
+ if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
+ xerror("glp_read_ccdata: v_wgt = %d; invalid offset\n",
+ v_wgt);
+ glp_erase_graph(G, G->v_size, G->a_size);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading graph from '%s'...\n", fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "edge") != 0)
+ error(csa, "wrong problem designator; 'edge' expected");
+ read_field(csa);
+ if (!(str2int(csa->field, &nv) == 0 && nv >= 0))
+ error(csa, "number of vertices missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &ne) == 0 && ne >= 0))
+ error(csa, "number of edges missing or invalid");
+ xprintf("Graph has %d vert%s and %d edge%s\n",
+ nv, nv == 1 ? "ex" : "ices", ne, ne == 1 ? "" : "s");
+ if (nv > 0) glp_add_vertices(G, nv);
+ end_of_line(csa);
+ /* read node descriptor lines */
+ flag = xcalloc(1+nv, sizeof(char));
+ memset(&flag[1], 0, nv * sizeof(char));
+ if (v_wgt >= 0)
+ { w = 1.0;
+ for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_wgt, &w, sizeof(double));
+ }
+ }
+ for (;;)
+ { read_designator(csa);
+ if (strcmp(csa->field, "n") != 0) break;
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "vertex number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "vertex number %d out of range", i);
+ if (flag[i])
+ error(csa, "duplicate descriptor of vertex %d", i);
+ read_field(csa);
+ if (str2num(csa->field, &w) != 0)
+ error(csa, "vertex weight missing or invalid");
+ check_int(csa, w);
+ if (v_wgt >= 0)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_wgt, &w, sizeof(double));
+ }
+ flag[i] = 1;
+ end_of_line(csa);
+ }
+ xfree(flag), flag = NULL;
+ /* read edge descriptor lines */
+ for (k = 1; k <= ne; k++)
+ { if (k > 1) read_designator(csa);
+ if (strcmp(csa->field, "e") != 0)
+ error(csa, "wrong line designator; 'e' expected");
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "first vertex number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "first vertex number %d out of range", i);
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "second vertex number missing or invalid");
+ if (!(1 <= j && j <= nv))
+ error(csa, "second vertex number %d out of range", j);
+ glp_add_arc(G, i, j);
+ end_of_line(csa);
+ }
+ xprintf("%d lines were read\n", csa->count);
+done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
+ if (csa->fp != NULL) glp_close(csa->fp);
+ if (flag != NULL) xfree(flag);
+ return ret;
+}
+
+/**********************************************************************/
+
+int glp_read_graph(glp_graph *G, const char *fname)
+{ return
+ glp_read_ccdata(G, -1, fname);
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdcnf.c b/test/monniaux/glpk-4.65/src/api/rdcnf.c
new file mode 100644
index 00000000..acab50fe
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdcnf.c
@@ -0,0 +1,136 @@
+/* rdcnf.c (read CNF-SAT problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "misc.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+int glp_read_cnfsat(glp_prob *P, const char *fname)
+{ /* read CNF-SAT problem data in DIMACS format */
+ DMX _csa, *csa = &_csa;
+ int m, n, i, j, len, neg, rhs, ret = 0, *ind = NULL;
+ double *val = NULL;
+ char *map = NULL;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_read_cnfsat: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (fname == NULL)
+ xerror("glp_read_cnfsat: fname = %p; invalid parameter\n",
+ fname);
+ glp_erase_prob(P);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading CNF-SAT problem data from '%s'...\n", fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "cnf") != 0)
+ error(csa, "wrong problem designator; 'cnf' expected\n");
+ read_field(csa);
+ if (!(str2int(csa->field, &n) == 0 && n >= 0))
+ error(csa, "number of variables missing or invalid\n");
+ read_field(csa);
+ if (!(str2int(csa->field, &m) == 0 && m >= 0))
+ error(csa, "number of clauses missing or invalid\n");
+ xprintf("Instance has %d variable%s and %d clause%s\n",
+ n, n == 1 ? "" : "s", m, m == 1 ? "" : "s");
+ end_of_line(csa);
+ if (m > 0)
+ glp_add_rows(P, m);
+ if (n > 0)
+ { glp_add_cols(P, n);
+ for (j = 1; j <= n; j++)
+ glp_set_col_kind(P, j, GLP_BV);
+ }
+ /* allocate working arrays */
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ map = xcalloc(1+n, sizeof(char));
+ for (j = 1; j <= n; j++) map[j] = 0;
+ /* read clauses */
+ for (i = 1; i <= m; i++)
+ { /* read i-th clause */
+ len = 0, rhs = 1;
+ for (;;)
+ { /* skip white-space characters */
+ while (csa->c == ' ' || csa->c == '\n')
+ read_char(csa);
+ /* read term */
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "variable number missing or invalid\n");
+ if (j > 0)
+ neg = 0;
+ else if (j < 0)
+ neg = 1, j = -j, rhs--;
+ else
+ break;
+ if (!(1 <= j && j <= n))
+ error(csa, "variable number out of range\n");
+ if (map[j])
+ error(csa, "duplicate variable number\n");
+ len++, ind[len] = j, val[len] = (neg ? -1.0 : +1.0);
+ map[j] = 1;
+ }
+ glp_set_row_bnds(P, i, GLP_LO, (double)rhs, 0.0);
+ glp_set_mat_row(P, i, len, ind, val);
+ while (len > 0) map[ind[len--]] = 0;
+ }
+ xprintf("%d lines were read\n", csa->count);
+ /* problem data has been successfully read */
+ glp_sort_matrix(P);
+done: if (csa->fp != NULL) glp_close(csa->fp);
+ if (ind != NULL) xfree(ind);
+ if (val != NULL) xfree(val);
+ if (map != NULL) xfree(map);
+ if (ret) glp_erase_prob(P);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdipt.c b/test/monniaux/glpk-4.65/src/api/rdipt.c
new file mode 100644
index 00000000..aaf8e9d4
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdipt.c
@@ -0,0 +1,185 @@
+/* rdipt.c (read interior-point solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_read_ipt - read interior-point solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_read_ipt(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_ipt reads interior-point solution from a text
+* file in GLPK format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_ipt(glp_prob *P, const char *fname)
+{ DMX dmx_, *dmx = &dmx_;
+ int i, j, k, m, n, sst, ret = 1;
+ char *stat = NULL;
+ double obj, *prim = NULL, *dual = NULL;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_read_ipt: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_read_ipt: fname = %d; invalid parameter\n", fname);
+ if (setjmp(dmx->jump))
+ goto done;
+ dmx->fname = fname;
+ dmx->fp = NULL;
+ dmx->count = 0;
+ dmx->c = '\n';
+ dmx->field[0] = '\0';
+ dmx->empty = dmx->nonint = 0;
+ xprintf("Reading interior-point solution from '%s'...\n", fname);
+ dmx->fp = glp_open(fname, "r");
+ if (dmx->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* read solution line */
+ dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "s") != 0)
+ dmx_error(dmx, "solution line missing or invalid");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "ipt") != 0)
+ dmx_error(dmx, "wrong solution designator; 'ipt' expected");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &m) == 0 && m >= 0))
+ dmx_error(dmx, "number of rows missing or invalid");
+ if (m != P->m)
+ dmx_error(dmx, "number of rows mismatch");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &n) == 0 && n >= 0))
+ dmx_error(dmx, "number of columns missing or invalid");
+ if (n != P->n)
+ dmx_error(dmx, "number of columns mismatch");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "o") == 0)
+ sst = GLP_OPT;
+ else if (strcmp(dmx->field, "i") == 0)
+ sst = GLP_INFEAS;
+ else if (strcmp(dmx->field, "n") == 0)
+ sst = GLP_NOFEAS;
+ else if (strcmp(dmx->field, "u") == 0)
+ sst = GLP_UNDEF;
+ else
+ dmx_error(dmx, "solution status missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &obj) != 0)
+ dmx_error(dmx, "objective value missing or invalid");
+ dmx_end_of_line(dmx);
+ /* allocate working arrays */
+ stat = xalloc(1+m+n, sizeof(stat[0]));
+ for (k = 1; k <= m+n; k++)
+ stat[k] = '?';
+ prim = xalloc(1+m+n, sizeof(prim[0]));
+ dual = xalloc(1+m+n, sizeof(dual[0]));
+ /* read solution descriptor lines */
+ for (;;)
+ { dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "i") == 0)
+ { /* row solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &i) != 0)
+ dmx_error(dmx, "row number missing or invalid");
+ if (!(1 <= i && i <= m))
+ dmx_error(dmx, "row number out of range");
+ if (stat[i] != '?')
+ dmx_error(dmx, "duplicate row solution descriptor");
+ stat[i] = GLP_BS;
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[i]) != 0)
+ dmx_error(dmx, "row primal value missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &dual[i]) != 0)
+ dmx_error(dmx, "row dual value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "j") == 0)
+ { /* column solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &j) != 0)
+ dmx_error(dmx, "column number missing or invalid");
+ if (!(1 <= j && j <= n))
+ dmx_error(dmx, "column number out of range");
+ if (stat[m+j] != '?')
+ dmx_error(dmx, "duplicate column solution descriptor");
+ stat[m+j] = GLP_BS;
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[m+j]) != 0)
+ dmx_error(dmx, "column primal value missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &dual[m+j]) != 0)
+ dmx_error(dmx, "column dual value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "e") == 0)
+ break;
+ else
+ dmx_error(dmx, "line designator missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ /* store solution components into problem object */
+ for (k = 1; k <= m+n; k++)
+ { if (stat[k] == '?')
+ dmx_error(dmx, "incomplete interior-point solution");
+ }
+ P->ipt_stat = sst;
+ P->ipt_obj = obj;
+ for (i = 1; i <= m; i++)
+ { P->row[i]->pval = prim[i];
+ P->row[i]->dval = dual[i];
+ }
+ for (j = 1; j <= n; j++)
+ { P->col[j]->pval = prim[m+j];
+ P->col[j]->dval = dual[m+j];
+ }
+ /* interior-point solution has been successfully read */
+ xprintf("%d lines were read\n", dmx->count);
+ ret = 0;
+done: if (dmx->fp != NULL)
+ glp_close(dmx->fp);
+ if (stat != NULL)
+ xfree(stat);
+ if (prim != NULL)
+ xfree(prim);
+ if (dual != NULL)
+ xfree(dual);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdmaxf.c b/test/monniaux/glpk-4.65/src/api/rdmaxf.c
new file mode 100644
index 00000000..a45405c9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdmaxf.c
@@ -0,0 +1,163 @@
+/* rdmaxf.c (read maximum flow problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "glpk.h"
+#include "misc.h"
+
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+/***********************************************************************
+* NAME
+*
+* glp_read_maxflow - read maximum flow problem data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_read_maxflow(glp_graph *G, int *s, int *t, int a_cap,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_maxflow reads maximum flow problem data in
+* DIMACS format from a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_maxflow(glp_graph *G, int *_s, int *_t, int a_cap,
+ const char *fname)
+{ DMX _csa, *csa = &_csa;
+ glp_arc *a;
+ int i, j, k, s, t, nv, na, ret = 0;
+ double cap;
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_read_maxflow: a_cap = %d; invalid offset\n",
+ a_cap);
+ glp_erase_graph(G, G->v_size, G->a_size);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading maximum flow problem data from '%s'...\n",
+ fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "max") != 0)
+ error(csa, "wrong problem designator; 'max' expected");
+ read_field(csa);
+ if (!(str2int(csa->field, &nv) == 0 && nv >= 2))
+ error(csa, "number of nodes missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &na) == 0 && na >= 0))
+ error(csa, "number of arcs missing or invalid");
+ xprintf("Flow network has %d node%s and %d arc%s\n",
+ nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s");
+ if (nv > 0) glp_add_vertices(G, nv);
+ end_of_line(csa);
+ /* read node descriptor lines */
+ s = t = 0;
+ for (;;)
+ { read_designator(csa);
+ if (strcmp(csa->field, "n") != 0) break;
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "node number %d out of range", i);
+ read_field(csa);
+ if (strcmp(csa->field, "s") == 0)
+ { if (s > 0)
+ error(csa, "only one source node allowed");
+ s = i;
+ }
+ else if (strcmp(csa->field, "t") == 0)
+ { if (t > 0)
+ error(csa, "only one sink node allowed");
+ t = i;
+ }
+ else
+ error(csa, "wrong node designator; 's' or 't' expected");
+ if (s > 0 && s == t)
+ error(csa, "source and sink nodes must be distinct");
+ end_of_line(csa);
+ }
+ if (s == 0)
+ error(csa, "source node descriptor missing\n");
+ if (t == 0)
+ error(csa, "sink node descriptor missing\n");
+ if (_s != NULL) *_s = s;
+ if (_t != NULL) *_t = t;
+ /* read arc descriptor lines */
+ for (k = 1; k <= na; k++)
+ { if (k > 1) read_designator(csa);
+ if (strcmp(csa->field, "a") != 0)
+ error(csa, "wrong line designator; 'a' expected");
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "starting node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "starting node number %d out of range", i);
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "ending node number missing or invalid");
+ if (!(1 <= j && j <= nv))
+ error(csa, "ending node number %d out of range", j);
+ read_field(csa);
+ if (!(str2num(csa->field, &cap) == 0 && cap >= 0.0))
+ error(csa, "arc capacity missing or invalid");
+ check_int(csa, cap);
+ a = glp_add_arc(G, i, j);
+ if (a_cap >= 0)
+ memcpy((char *)a->data + a_cap, &cap, sizeof(double));
+ end_of_line(csa);
+ }
+ xprintf("%d lines were read\n", csa->count);
+done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
+ if (csa->fp != NULL) glp_close(csa->fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdmcf.c b/test/monniaux/glpk-4.65/src/api/rdmcf.c
new file mode 100644
index 00000000..bab1ec79
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdmcf.c
@@ -0,0 +1,186 @@
+/* rdmcf.c (read min-cost flow problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "glpk.h"
+#include "misc.h"
+
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+/***********************************************************************
+* NAME
+*
+* glp_read_mincost - read min-cost flow problem data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+* int a_cost, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_mincost reads minimum cost flow problem data in
+* DIMACS format from a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, const char *fname)
+{ DMX _csa, *csa = &_csa;
+ glp_vertex *v;
+ glp_arc *a;
+ int i, j, k, nv, na, ret = 0;
+ double rhs, low, cap, cost;
+ char *flag = NULL;
+ if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_read_mincost: v_rhs = %d; invalid offset\n",
+ v_rhs);
+ if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
+ xerror("glp_read_mincost: a_low = %d; invalid offset\n",
+ a_low);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_read_mincost: a_cap = %d; invalid offset\n",
+ a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_read_mincost: a_cost = %d; invalid offset\n",
+ a_cost);
+ glp_erase_graph(G, G->v_size, G->a_size);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading min-cost flow problem data from '%s'...\n",
+ fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "min") != 0)
+ error(csa, "wrong problem designator; 'min' expected");
+ read_field(csa);
+ if (!(str2int(csa->field, &nv) == 0 && nv >= 0))
+ error(csa, "number of nodes missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &na) == 0 && na >= 0))
+ error(csa, "number of arcs missing or invalid");
+ xprintf("Flow network has %d node%s and %d arc%s\n",
+ nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s");
+ if (nv > 0) glp_add_vertices(G, nv);
+ end_of_line(csa);
+ /* read node descriptor lines */
+ flag = xcalloc(1+nv, sizeof(char));
+ memset(&flag[1], 0, nv * sizeof(char));
+ if (v_rhs >= 0)
+ { rhs = 0.0;
+ for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &rhs, sizeof(double));
+ }
+ }
+ for (;;)
+ { read_designator(csa);
+ if (strcmp(csa->field, "n") != 0) break;
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "node number %d out of range", i);
+ if (flag[i])
+ error(csa, "duplicate descriptor of node %d", i);
+ read_field(csa);
+ if (str2num(csa->field, &rhs) != 0)
+ error(csa, "node supply/demand missing or invalid");
+ check_int(csa, rhs);
+ if (v_rhs >= 0)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_rhs, &rhs, sizeof(double));
+ }
+ flag[i] = 1;
+ end_of_line(csa);
+ }
+ xfree(flag), flag = NULL;
+ /* read arc descriptor lines */
+ for (k = 1; k <= na; k++)
+ { if (k > 1) read_designator(csa);
+ if (strcmp(csa->field, "a") != 0)
+ error(csa, "wrong line designator; 'a' expected");
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "starting node number missing or invalid");
+ if (!(1 <= i && i <= nv))
+ error(csa, "starting node number %d out of range", i);
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "ending node number missing or invalid");
+ if (!(1 <= j && j <= nv))
+ error(csa, "ending node number %d out of range", j);
+ read_field(csa);
+ if (!(str2num(csa->field, &low) == 0 && low >= 0.0))
+ error(csa, "lower bound of arc flow missing or invalid");
+ check_int(csa, low);
+ read_field(csa);
+ if (!(str2num(csa->field, &cap) == 0 && cap >= low))
+ error(csa, "upper bound of arc flow missing or invalid");
+ check_int(csa, cap);
+ read_field(csa);
+ if (str2num(csa->field, &cost) != 0)
+ error(csa, "per-unit cost of arc flow missing or invalid");
+ check_int(csa, cost);
+ a = glp_add_arc(G, i, j);
+ if (a_low >= 0)
+ memcpy((char *)a->data + a_low, &low, sizeof(double));
+ if (a_cap >= 0)
+ memcpy((char *)a->data + a_cap, &cap, sizeof(double));
+ if (a_cost >= 0)
+ memcpy((char *)a->data + a_cost, &cost, sizeof(double));
+ end_of_line(csa);
+ }
+ xprintf("%d lines were read\n", csa->count);
+done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
+ if (csa->fp != NULL) glp_close(csa->fp);
+ if (flag != NULL) xfree(flag);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdmip.c b/test/monniaux/glpk-4.65/src/api/rdmip.c
new file mode 100644
index 00000000..7aec26b3
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdmip.c
@@ -0,0 +1,172 @@
+/* rdmip.c (read MIP solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_read_mip - read MIP solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_read_mip(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_mip reads MIP solution from a text file in GLPK
+* format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_mip(glp_prob *P, const char *fname)
+{ DMX dmx_, *dmx = &dmx_;
+ int i, j, k, m, n, sst, ret = 1;
+ char *stat = NULL;
+ double obj, *prim = NULL;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_read_mip: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_read_mip: fname = %d; invalid parameter\n", fname);
+ if (setjmp(dmx->jump))
+ goto done;
+ dmx->fname = fname;
+ dmx->fp = NULL;
+ dmx->count = 0;
+ dmx->c = '\n';
+ dmx->field[0] = '\0';
+ dmx->empty = dmx->nonint = 0;
+ xprintf("Reading MIP solution from '%s'...\n", fname);
+ dmx->fp = glp_open(fname, "r");
+ if (dmx->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* read solution line */
+ dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "s") != 0)
+ dmx_error(dmx, "solution line missing or invalid");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "mip") != 0)
+ dmx_error(dmx, "wrong solution designator; 'mip' expected");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &m) == 0 && m >= 0))
+ dmx_error(dmx, "number of rows missing or invalid");
+ if (m != P->m)
+ dmx_error(dmx, "number of rows mismatch");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &n) == 0 && n >= 0))
+ dmx_error(dmx, "number of columns missing or invalid");
+ if (n != P->n)
+ dmx_error(dmx, "number of columns mismatch");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "o") == 0)
+ sst = GLP_OPT;
+ else if (strcmp(dmx->field, "f") == 0)
+ sst = GLP_FEAS;
+ else if (strcmp(dmx->field, "n") == 0)
+ sst = GLP_NOFEAS;
+ else if (strcmp(dmx->field, "u") == 0)
+ sst = GLP_UNDEF;
+ else
+ dmx_error(dmx, "solution status missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &obj) != 0)
+ dmx_error(dmx, "objective value missing or invalid");
+ dmx_end_of_line(dmx);
+ /* allocate working arrays */
+ stat = xalloc(1+m+n, sizeof(stat[0]));
+ for (k = 1; k <= m+n; k++)
+ stat[k] = '?';
+ prim = xalloc(1+m+n, sizeof(prim[0]));
+ /* read solution descriptor lines */
+ for (;;)
+ { dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "i") == 0)
+ { /* row solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &i) != 0)
+ dmx_error(dmx, "row number missing or invalid");
+ if (!(1 <= i && i <= m))
+ dmx_error(dmx, "row number out of range");
+ if (stat[i] != '?')
+ dmx_error(dmx, "duplicate row solution descriptor");
+ stat[i] = GLP_BS;
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[i]) != 0)
+ dmx_error(dmx, "row value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "j") == 0)
+ { /* column solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &j) != 0)
+ dmx_error(dmx, "column number missing or invalid");
+ if (!(1 <= j && j <= n))
+ dmx_error(dmx, "column number out of range");
+ if (stat[m+j] != '?')
+ dmx_error(dmx, "duplicate column solution descriptor");
+ stat[m+j] = GLP_BS;
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[m+j]) != 0)
+ dmx_error(dmx, "column value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "e") == 0)
+ break;
+ else
+ dmx_error(dmx, "line designator missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ /* store solution components into problem object */
+ for (k = 1; k <= m+n; k++)
+ { if (stat[k] == '?')
+ dmx_error(dmx, "incomplete MIP solution");
+ }
+ P->mip_stat = sst;
+ P->mip_obj = obj;
+ for (i = 1; i <= m; i++)
+ P->row[i]->mipx = prim[i];
+ for (j = 1; j <= n; j++)
+ P->col[j]->mipx = prim[m+j];
+ /* MIP solution has been successfully read */
+ xprintf("%d lines were read\n", dmx->count);
+ ret = 0;
+done: if (dmx->fp != NULL)
+ glp_close(dmx->fp);
+ if (stat != NULL)
+ xfree(stat);
+ if (prim != NULL)
+ xfree(prim);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdprob.c b/test/monniaux/glpk-4.65/src/api/rdprob.c
new file mode 100644
index 00000000..1ad544a5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdprob.c
@@ -0,0 +1,377 @@
+/* rdprob.c (read problem data in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "misc.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+#define error dmx_error
+#define warning dmx_warning
+#define read_char dmx_read_char
+#define read_designator dmx_read_designator
+#define read_field dmx_read_field
+#define end_of_line dmx_end_of_line
+#define check_int dmx_check_int
+
+/***********************************************************************
+* NAME
+*
+* glp_read_prob - read problem data in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_read_prob(glp_prob *P, int flags, const char *fname);
+*
+* The routine glp_read_prob reads problem data in GLPK LP/MIP format
+* from a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_prob(glp_prob *P, int flags, const char *fname)
+{ DMX _csa, *csa = &_csa;
+ int mip, m, n, nnz, ne, i, j, k, type, kind, ret, *ln = NULL,
+ *ia = NULL, *ja = NULL;
+ double lb, ub, temp, *ar = NULL;
+ char *rf = NULL, *cf = NULL;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_read_prob: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (flags != 0)
+ xerror("glp_read_prob: flags = %d; invalid parameter\n",
+ flags);
+ if (fname == NULL)
+ xerror("glp_read_prob: fname = %d; invalid parameter\n",
+ fname);
+ glp_erase_prob(P);
+ if (setjmp(csa->jump))
+ { ret = 1;
+ goto done;
+ }
+ csa->fname = fname;
+ csa->fp = NULL;
+ csa->count = 0;
+ csa->c = '\n';
+ csa->field[0] = '\0';
+ csa->empty = csa->nonint = 0;
+ xprintf("Reading problem data from '%s'...\n", fname);
+ csa->fp = glp_open(fname, "r");
+ if (csa->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ longjmp(csa->jump, 1);
+ }
+ /* read problem line */
+ read_designator(csa);
+ if (strcmp(csa->field, "p") != 0)
+ error(csa, "problem line missing or invalid");
+ read_field(csa);
+ if (strcmp(csa->field, "lp") == 0)
+ mip = 0;
+ else if (strcmp(csa->field, "mip") == 0)
+ mip = 1;
+ else
+ error(csa, "wrong problem designator; 'lp' or 'mip' expected");
+ read_field(csa);
+ if (strcmp(csa->field, "min") == 0)
+ glp_set_obj_dir(P, GLP_MIN);
+ else if (strcmp(csa->field, "max") == 0)
+ glp_set_obj_dir(P, GLP_MAX);
+ else
+ error(csa, "objective sense missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &m) == 0 && m >= 0))
+ error(csa, "number of rows missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &n) == 0 && n >= 0))
+ error(csa, "number of columns missing or invalid");
+ read_field(csa);
+ if (!(str2int(csa->field, &nnz) == 0 && nnz >= 0))
+ error(csa, "number of constraint coefficients missing or inval"
+ "id");
+ if (m > 0)
+ { glp_add_rows(P, m);
+ for (i = 1; i <= m; i++)
+ glp_set_row_bnds(P, i, GLP_FX, 0.0, 0.0);
+ }
+ if (n > 0)
+ { glp_add_cols(P, n);
+ for (j = 1; j <= n; j++)
+ { if (!mip)
+ glp_set_col_bnds(P, j, GLP_LO, 0.0, 0.0);
+ else
+ glp_set_col_kind(P, j, GLP_BV);
+ }
+ }
+ end_of_line(csa);
+ /* allocate working arrays */
+ rf = xcalloc(1+m, sizeof(char));
+ memset(rf, 0, 1+m);
+ cf = xcalloc(1+n, sizeof(char));
+ memset(cf, 0, 1+n);
+ ln = xcalloc(1+nnz, sizeof(int));
+ ia = xcalloc(1+nnz, sizeof(int));
+ ja = xcalloc(1+nnz, sizeof(int));
+ ar = xcalloc(1+nnz, sizeof(double));
+ /* read descriptor lines */
+ ne = 0;
+ for (;;)
+ { read_designator(csa);
+ if (strcmp(csa->field, "i") == 0)
+ { /* row descriptor */
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "row number missing or invalid");
+ if (!(1 <= i && i <= m))
+ error(csa, "row number out of range");
+ read_field(csa);
+ if (strcmp(csa->field, "f") == 0)
+ type = GLP_FR;
+ else if (strcmp(csa->field, "l") == 0)
+ type = GLP_LO;
+ else if (strcmp(csa->field, "u") == 0)
+ type = GLP_UP;
+ else if (strcmp(csa->field, "d") == 0)
+ type = GLP_DB;
+ else if (strcmp(csa->field, "s") == 0)
+ type = GLP_FX;
+ else
+ error(csa, "row type missing or invalid");
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { read_field(csa);
+ if (str2num(csa->field, &lb) != 0)
+ error(csa, "row lower bound/fixed value missing or in"
+ "valid");
+ }
+ else
+ lb = 0.0;
+ if (type == GLP_UP || type == GLP_DB)
+ { read_field(csa);
+ if (str2num(csa->field, &ub) != 0)
+ error(csa, "row upper bound missing or invalid");
+ }
+ else
+ ub = 0.0;
+ if (rf[i] & 0x01)
+ error(csa, "duplicate row descriptor");
+ glp_set_row_bnds(P, i, type, lb, ub), rf[i] |= 0x01;
+ }
+ else if (strcmp(csa->field, "j") == 0)
+ { /* column descriptor */
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "column number missing or invalid");
+ if (!(1 <= j && j <= n))
+ error(csa, "column number out of range");
+ if (!mip)
+ kind = GLP_CV;
+ else
+ { read_field(csa);
+ if (strcmp(csa->field, "c") == 0)
+ kind = GLP_CV;
+ else if (strcmp(csa->field, "i") == 0)
+ kind = GLP_IV;
+ else if (strcmp(csa->field, "b") == 0)
+ { kind = GLP_IV;
+ type = GLP_DB, lb = 0.0, ub = 1.0;
+ goto skip;
+ }
+ else
+ error(csa, "column kind missing or invalid");
+ }
+ read_field(csa);
+ if (strcmp(csa->field, "f") == 0)
+ type = GLP_FR;
+ else if (strcmp(csa->field, "l") == 0)
+ type = GLP_LO;
+ else if (strcmp(csa->field, "u") == 0)
+ type = GLP_UP;
+ else if (strcmp(csa->field, "d") == 0)
+ type = GLP_DB;
+ else if (strcmp(csa->field, "s") == 0)
+ type = GLP_FX;
+ else
+ error(csa, "column type missing or invalid");
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { read_field(csa);
+ if (str2num(csa->field, &lb) != 0)
+ error(csa, "column lower bound/fixed value missing or"
+ " invalid");
+ }
+ else
+ lb = 0.0;
+ if (type == GLP_UP || type == GLP_DB)
+ { read_field(csa);
+ if (str2num(csa->field, &ub) != 0)
+ error(csa, "column upper bound missing or invalid");
+ }
+ else
+ ub = 0.0;
+skip: if (cf[j] & 0x01)
+ error(csa, "duplicate column descriptor");
+ glp_set_col_kind(P, j, kind);
+ glp_set_col_bnds(P, j, type, lb, ub), cf[j] |= 0x01;
+ }
+ else if (strcmp(csa->field, "a") == 0)
+ { /* coefficient descriptor */
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "row number missing or invalid");
+ if (!(0 <= i && i <= m))
+ error(csa, "row number out of range");
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "column number missing or invalid");
+ if (!((i == 0 ? 0 : 1) <= j && j <= n))
+ error(csa, "column number out of range");
+ read_field(csa);
+ if (i == 0)
+ { if (str2num(csa->field, &temp) != 0)
+ error(csa, "objective %s missing or invalid",
+ j == 0 ? "constant term" : "coefficient");
+ if (cf[j] & 0x10)
+ error(csa, "duplicate objective %s",
+ j == 0 ? "constant term" : "coefficient");
+ glp_set_obj_coef(P, j, temp), cf[j] |= 0x10;
+ }
+ else
+ { if (str2num(csa->field, &temp) != 0)
+ error(csa, "constraint coefficient missing or invalid"
+ );
+ if (ne == nnz)
+ error(csa, "too many constraint coefficient descripto"
+ "rs");
+ ln[++ne] = csa->count;
+ ia[ne] = i, ja[ne] = j, ar[ne] = temp;
+ }
+ }
+ else if (strcmp(csa->field, "n") == 0)
+ { /* symbolic name descriptor */
+ read_field(csa);
+ if (strcmp(csa->field, "p") == 0)
+ { /* problem name */
+ read_field(csa);
+ if (P->name != NULL)
+ error(csa, "duplicate problem name");
+ glp_set_prob_name(P, csa->field);
+ }
+ else if (strcmp(csa->field, "z") == 0)
+ { /* objective name */
+ read_field(csa);
+ if (P->obj != NULL)
+ error(csa, "duplicate objective name");
+ glp_set_obj_name(P, csa->field);
+ }
+ else if (strcmp(csa->field, "i") == 0)
+ { /* row name */
+ read_field(csa);
+ if (str2int(csa->field, &i) != 0)
+ error(csa, "row number missing or invalid");
+ if (!(1 <= i && i <= m))
+ error(csa, "row number out of range");
+ read_field(csa);
+ if (P->row[i]->name != NULL)
+ error(csa, "duplicate row name");
+ glp_set_row_name(P, i, csa->field);
+ }
+ else if (strcmp(csa->field, "j") == 0)
+ { /* column name */
+ read_field(csa);
+ if (str2int(csa->field, &j) != 0)
+ error(csa, "column number missing or invalid");
+ if (!(1 <= j && j <= n))
+ error(csa, "column number out of range");
+ read_field(csa);
+ if (P->col[j]->name != NULL)
+ error(csa, "duplicate column name");
+ glp_set_col_name(P, j, csa->field);
+ }
+ else
+ error(csa, "object designator missing or invalid");
+ }
+ else if (strcmp(csa->field, "e") == 0)
+ break;
+ else
+ error(csa, "line designator missing or invalid");
+ end_of_line(csa);
+ }
+ if (ne < nnz)
+ error(csa, "too few constraint coefficient descriptors");
+ xassert(ne == nnz);
+ k = glp_check_dup(m, n, ne, ia, ja);
+ xassert(0 <= k && k <= nnz);
+ if (k > 0)
+ { csa->count = ln[k];
+ error(csa, "duplicate constraint coefficient");
+ }
+ glp_load_matrix(P, ne, ia, ja, ar);
+ /* print some statistics */
+ if (P->name != NULL)
+ xprintf("Problem: %s\n", P->name);
+ if (P->obj != NULL)
+ xprintf("Objective: %s\n", P->obj);
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ m, m == 1 ? "" : "s", n, n == 1 ? "" : "s", nnz, nnz == 1 ?
+ "" : "s");
+ if (glp_get_num_int(P) > 0)
+ { int ni = glp_get_num_int(P);
+ int nb = glp_get_num_bin(P);
+ if (ni == 1)
+ { if (nb == 0)
+ xprintf("One variable is integer\n");
+ else
+ xprintf("One variable is binary\n");
+ }
+ else
+ { xprintf("%d integer variables, ", ni);
+ if (nb == 0)
+ xprintf("none");
+ else if (nb == 1)
+ xprintf("one");
+ else if (nb == ni)
+ xprintf("all");
+ else
+ xprintf("%d", nb);
+ xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
+ }
+ }
+ xprintf("%d lines were read\n", csa->count);
+ /* problem data has been successfully read */
+ glp_sort_matrix(P);
+ ret = 0;
+done: if (csa->fp != NULL) glp_close(csa->fp);
+ if (rf != NULL) xfree(rf);
+ if (cf != NULL) xfree(cf);
+ if (ln != NULL) xfree(ln);
+ if (ia != NULL) xfree(ia);
+ if (ja != NULL) xfree(ja);
+ if (ar != NULL) xfree(ar);
+ if (ret) glp_erase_prob(P);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rdsol.c b/test/monniaux/glpk-4.65/src/api/rdsol.c
new file mode 100644
index 00000000..d85a2562
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rdsol.c
@@ -0,0 +1,225 @@
+/* rdsol.c (read basic solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_read_sol - read basic solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_read_sol(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_read_sol reads basic solution from a text file in
+* GLPK format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_read_sol(glp_prob *P, const char *fname)
+{ DMX dmx_, *dmx = &dmx_;
+ int i, j, k, m, n, pst, dst, ret = 1;
+ char *stat = NULL;
+ double obj, *prim = NULL, *dual = NULL;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_read_sol: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_read_sol: fname = %d; invalid parameter\n", fname);
+ if (setjmp(dmx->jump))
+ goto done;
+ dmx->fname = fname;
+ dmx->fp = NULL;
+ dmx->count = 0;
+ dmx->c = '\n';
+ dmx->field[0] = '\0';
+ dmx->empty = dmx->nonint = 0;
+ xprintf("Reading basic solution from '%s'...\n", fname);
+ dmx->fp = glp_open(fname, "r");
+ if (dmx->fp == NULL)
+ { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* read solution line */
+ dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "s") != 0)
+ dmx_error(dmx, "solution line missing or invalid");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "bas") != 0)
+ dmx_error(dmx, "wrong solution designator; 'bas' expected");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &m) == 0 && m >= 0))
+ dmx_error(dmx, "number of rows missing or invalid");
+ if (m != P->m)
+ dmx_error(dmx, "number of rows mismatch");
+ dmx_read_field(dmx);
+ if (!(str2int(dmx->field, &n) == 0 && n >= 0))
+ dmx_error(dmx, "number of columns missing or invalid");
+ if (n != P->n)
+ dmx_error(dmx, "number of columns mismatch");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "u") == 0)
+ pst = GLP_UNDEF;
+ else if (strcmp(dmx->field, "f") == 0)
+ pst = GLP_FEAS;
+ else if (strcmp(dmx->field, "i") == 0)
+ pst = GLP_INFEAS;
+ else if (strcmp(dmx->field, "n") == 0)
+ pst = GLP_NOFEAS;
+ else
+ dmx_error(dmx, "primal solution status missing or invalid");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "u") == 0)
+ dst = GLP_UNDEF;
+ else if (strcmp(dmx->field, "f") == 0)
+ dst = GLP_FEAS;
+ else if (strcmp(dmx->field, "i") == 0)
+ dst = GLP_INFEAS;
+ else if (strcmp(dmx->field, "n") == 0)
+ dst = GLP_NOFEAS;
+ else
+ dmx_error(dmx, "dual solution status missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &obj) != 0)
+ dmx_error(dmx, "objective value missing or invalid");
+ dmx_end_of_line(dmx);
+ /* allocate working arrays */
+ stat = xalloc(1+m+n, sizeof(stat[0]));
+ for (k = 1; k <= m+n; k++)
+ stat[k] = '?';
+ prim = xalloc(1+m+n, sizeof(prim[0]));
+ dual = xalloc(1+m+n, sizeof(dual[0]));
+ /* read solution descriptor lines */
+ for (;;)
+ { dmx_read_designator(dmx);
+ if (strcmp(dmx->field, "i") == 0)
+ { /* row solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &i) != 0)
+ dmx_error(dmx, "row number missing or invalid");
+ if (!(1 <= i && i <= m))
+ dmx_error(dmx, "row number out of range");
+ if (stat[i] != '?')
+ dmx_error(dmx, "duplicate row solution descriptor");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "b") == 0)
+ stat[i] = GLP_BS;
+ else if (strcmp(dmx->field, "l") == 0)
+ stat[i] = GLP_NL;
+ else if (strcmp(dmx->field, "u") == 0)
+ stat[i] = GLP_NU;
+ else if (strcmp(dmx->field, "f") == 0)
+ stat[i] = GLP_NF;
+ else if (strcmp(dmx->field, "s") == 0)
+ stat[i] = GLP_NS;
+ else
+ dmx_error(dmx, "row status missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[i]) != 0)
+ dmx_error(dmx, "row primal value missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &dual[i]) != 0)
+ dmx_error(dmx, "row dual value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "j") == 0)
+ { /* column solution descriptor */
+ dmx_read_field(dmx);
+ if (str2int(dmx->field, &j) != 0)
+ dmx_error(dmx, "column number missing or invalid");
+ if (!(1 <= j && j <= n))
+ dmx_error(dmx, "column number out of range");
+ if (stat[m+j] != '?')
+ dmx_error(dmx, "duplicate column solution descriptor");
+ dmx_read_field(dmx);
+ if (strcmp(dmx->field, "b") == 0)
+ stat[m+j] = GLP_BS;
+ else if (strcmp(dmx->field, "l") == 0)
+ stat[m+j] = GLP_NL;
+ else if (strcmp(dmx->field, "u") == 0)
+ stat[m+j] = GLP_NU;
+ else if (strcmp(dmx->field, "f") == 0)
+ stat[m+j] = GLP_NF;
+ else if (strcmp(dmx->field, "s") == 0)
+ stat[m+j] = GLP_NS;
+ else
+ dmx_error(dmx, "column status missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &prim[m+j]) != 0)
+ dmx_error(dmx, "column primal value missing or invalid");
+ dmx_read_field(dmx);
+ if (str2num(dmx->field, &dual[m+j]) != 0)
+ dmx_error(dmx, "column dual value missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ else if (strcmp(dmx->field, "e") == 0)
+ break;
+ else
+ dmx_error(dmx, "line designator missing or invalid");
+ dmx_end_of_line(dmx);
+ }
+ /* store solution components into problem object */
+ for (k = 1; k <= m+n; k++)
+ { if (stat[k] == '?')
+ dmx_error(dmx, "incomplete basic solution");
+ }
+ P->pbs_stat = pst;
+ P->dbs_stat = dst;
+ P->obj_val = obj;
+ P->it_cnt = 0;
+ P->some = 0;
+ for (i = 1; i <= m; i++)
+ { glp_set_row_stat(P, i, stat[i]);
+ P->row[i]->prim = prim[i];
+ P->row[i]->dual = dual[i];
+ }
+ for (j = 1; j <= n; j++)
+ { glp_set_col_stat(P, j, stat[m+j]);
+ P->col[j]->prim = prim[m+j];
+ P->col[j]->dual = dual[m+j];
+ }
+ /* basic solution has been successfully read */
+ xprintf("%d lines were read\n", dmx->count);
+ ret = 0;
+done: if (dmx->fp != NULL)
+ glp_close(dmx->fp);
+ if (stat != NULL)
+ xfree(stat);
+ if (prim != NULL)
+ xfree(prim);
+ if (dual != NULL)
+ xfree(dual);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/rmfgen.c b/test/monniaux/glpk-4.65/src/api/rmfgen.c
new file mode 100644
index 00000000..a1ba27bb
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/rmfgen.c
@@ -0,0 +1,368 @@
+/* rmfgen.c (Goldfarb's maximum flow problem generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is a modified version of the program RMFGEN, a maxflow
+* problem generator developed by D.Goldfarb and M.Grigoriadis, and
+* originally implemented by Tamas Badics <badics@rutcor.rutgers.edu>.
+* The original code is publically available on the DIMACS ftp site at:
+* <ftp://dimacs.rutgers.edu/pub/netflow/generators/network/genrmf>.
+*
+* All changes concern only the program interface, so this modified
+* version produces exactly the same instances as the original version.
+*
+* Changes were made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "rng.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_rmfgen - Goldfarb's maximum flow problem generator
+*
+* SYNOPSIS
+*
+* int glp_rmfgen(glp_graph *G, int *s, int *t, int a_cap,
+* const int parm[1+5]);
+*
+* DESCRIPTION
+*
+* The routine glp_rmfgen is a maximum flow problem generator developed
+* by D.Goldfarb and M.Grigoriadis.
+*
+* The parameter G specifies the graph object, to which the generated
+* problem data have to be stored. Note that on entry the graph object
+* is erased with the routine glp_erase_graph.
+*
+* The pointer s specifies a location, to which the routine stores the
+* source node number. If s is NULL, the node number is not stored.
+*
+* The pointer t specifies a location, to which the routine stores the
+* sink node number. If t is NULL, the node number is not stored.
+*
+* The parameter a_cap specifies an offset of the field of type double
+* in the arc data block, to which the routine stores the arc capacity.
+* If a_cap < 0, the capacity is not stored.
+*
+* The array parm contains description of the network to be generated:
+*
+* parm[0] not used
+* parm[1] (seed) random number seed (a positive integer)
+* parm[2] (a) frame size
+* parm[3] (b) depth
+* parm[4] (c1) minimal arc capacity
+* parm[5] (c2) maximal arc capacity
+*
+* RETURNS
+*
+* If the instance was successfully generated, the routine glp_netgen
+* returns zero; otherwise, if specified parameters are inconsistent,
+* the routine returns a non-zero error code.
+*
+* COMMENTS
+*
+* The generated network is as follows. It has b pieces of frames of
+* size a * a. (So alltogether the number of vertices is a * a * b)
+*
+* In each frame all the vertices are connected with their neighbours
+* (forth and back). In addition the vertices of a frame are connected
+* one to one with the vertices of next frame using a random permutation
+* of those vertices.
+*
+* The source is the lower left vertex of the first frame, the sink is
+* the upper right vertex of the b'th frame.
+*
+* t
+* +-------+
+* | .|
+* | . |
+* / | / |
+* +-------+/ -+ b
+* | | |/.
+* a | -v- |/
+* | | |/
+* +-------+ 1
+* s a
+*
+* The capacities are randomly chosen integers from the range of [c1,c2]
+* in the case of interconnecting edges, and c2 * a * a for the in-frame
+* edges.
+*
+* REFERENCES
+*
+* D.Goldfarb and M.D.Grigoriadis, "A computational comparison of the
+* Dinic and network simplex methods for maximum flow." Annals of Op.
+* Res. 13 (1988), pp. 83-123.
+*
+* U.Derigs and W.Meier, "Implementing Goldberg's max-flow algorithm:
+* A computational investigation." Zeitschrift fuer Operations Research
+* 33 (1989), pp. 383-403. */
+
+typedef struct VERTEX
+{ struct EDGE **edgelist;
+ /* Pointer to the list of pointers to the adjacent edges.
+ (No matter that to or from edges) */
+ struct EDGE **current;
+ /* Pointer to the current edge */
+ int degree;
+ /* Number of adjacent edges (both direction) */
+ int index;
+} vertex;
+
+typedef struct EDGE
+{ int from;
+ int to;
+ int cap;
+ /* Capacity */
+} edge;
+
+typedef struct NETWORK
+{ struct NETWORK *next, *prev;
+ int vertnum;
+ int edgenum;
+ vertex *verts;
+ /* Vertex array[1..vertnum] */
+ edge *edges;
+ /* Edge array[1..edgenum] */
+ int source;
+ /* Pointer to the source */
+ int sink;
+ /* Pointer to the sink */
+} network;
+
+struct csa
+{ /* common storage area */
+ glp_graph *G;
+ int *s, *t, a_cap;
+ RNG *rand;
+ network *N;
+ int *Parr;
+ int A, AA, C2AA, Ec;
+};
+
+#define G (csa->G)
+#define s (csa->s)
+#define t (csa->t)
+#define a_cap (csa->a_cap)
+#define N (csa->N)
+#define Parr (csa->Parr)
+#define A (csa->A)
+#define AA (csa->AA)
+#define C2AA (csa->C2AA)
+#define Ec (csa->Ec)
+
+#undef random
+#define random(A) (int)(rng_unif_01(csa->rand) * (double)(A))
+#define RANDOM(A, B) (int)(random((B) - (A) + 1) + (A))
+#define sgn(A) (((A) > 0) ? 1 : ((A) == 0) ? 0 : -1)
+
+static void make_edge(struct csa *csa, int from, int to, int c1, int c2)
+{ Ec++;
+ N->edges[Ec].from = from;
+ N->edges[Ec].to = to;
+ N->edges[Ec].cap = RANDOM(c1, c2);
+ return;
+}
+
+static void permute(struct csa *csa)
+{ int i, j, tmp;
+ for (i = 1; i < AA; i++)
+ { j = RANDOM(i, AA);
+ tmp = Parr[i];
+ Parr[i] = Parr[j];
+ Parr[j] = tmp;
+ }
+ return;
+}
+
+static void connect(struct csa *csa, int offset, int cv, int x1, int y1)
+{ int cv1;
+ cv1 = offset + (x1 - 1) * A + y1;
+ Ec++;
+ N->edges[Ec].from = cv;
+ N->edges[Ec].to = cv1;
+ N->edges[Ec].cap = C2AA;
+ return;
+}
+
+static network *gen_rmf(struct csa *csa, int a, int b, int c1, int c2)
+{ /* generates a network with a*a*b nodes and 6a*a*b-4ab-2a*a edges
+ random_frame network:
+ Derigs & Meier, Methods & Models of OR (1989), 33:383-403 */
+ int x, y, z, offset, cv;
+ A = a;
+ AA = a * a;
+ C2AA = c2 * AA;
+ Ec = 0;
+ N = (network *)xmalloc(sizeof(network));
+ N->vertnum = AA * b;
+ N->edgenum = 5 * AA * b - 4 * A * b - AA;
+ N->edges = (edge *)xcalloc(N->edgenum + 1, sizeof(edge));
+ N->source = 1;
+ N->sink = N->vertnum;
+ Parr = (int *)xcalloc(AA + 1, sizeof(int));
+ for (x = 1; x <= AA; x++)
+ Parr[x] = x;
+ for (z = 1; z <= b; z++)
+ { offset = AA * (z - 1);
+ if (z != b)
+ permute(csa);
+ for (x = 1; x <= A; x++)
+ { for (y = 1; y <= A; y++)
+ { cv = offset + (x - 1) * A + y;
+ if (z != b)
+ make_edge(csa, cv, offset + AA + Parr[cv - offset],
+ c1, c2); /* the intermediate edges */
+ if (y < A)
+ connect(csa, offset, cv, x, y + 1);
+ if (y > 1)
+ connect(csa, offset, cv, x, y - 1);
+ if (x < A)
+ connect(csa, offset, cv, x + 1, y);
+ if (x > 1)
+ connect(csa, offset, cv, x - 1, y);
+ }
+ }
+ }
+ xfree(Parr);
+ return N;
+}
+
+static void print_max_format(struct csa *csa, network *n, char *comm[],
+ int dim)
+{ /* prints a network heading with dim lines of comments (no \n
+ needs at the ends) */
+ int i, vnum, e_num;
+ edge *e;
+ vnum = n->vertnum;
+ e_num = n->edgenum;
+ if (G == NULL)
+ { for (i = 0; i < dim; i++)
+ xprintf("c %s\n", comm[i]);
+ xprintf("p max %7d %10d\n", vnum, e_num);
+ xprintf("n %7d s\n", n->source);
+ xprintf("n %7d t\n", n->sink);
+ }
+ else
+ { glp_add_vertices(G, vnum);
+ if (s != NULL) *s = n->source;
+ if (t != NULL) *t = n->sink;
+ }
+ for (i = 1; i <= e_num; i++)
+ { e = &n->edges[i];
+ if (G == NULL)
+ xprintf("a %7d %7d %10d\n", e->from, e->to, (int)e->cap);
+ else
+ { glp_arc *a = glp_add_arc(G, e->from, e->to);
+ if (a_cap >= 0)
+ { double temp = (double)e->cap;
+ memcpy((char *)a->data + a_cap, &temp, sizeof(double));
+ }
+ }
+ }
+ return;
+}
+
+static void gen_free_net(network *n)
+{ xfree(n->edges);
+ xfree(n);
+ return;
+}
+
+int glp_rmfgen(glp_graph *G_, int *_s, int *_t, int _a_cap,
+ const int parm[1+5])
+{ struct csa _csa, *csa = &_csa;
+ network *n;
+ char comm[10][80], *com1[10];
+ int seed, a, b, c1, c2, ret;
+ G = G_;
+ s = _s;
+ t = _t;
+ a_cap = _a_cap;
+ if (G != NULL)
+ { if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_rmfgen: a_cap = %d; invalid offset\n", a_cap);
+ }
+ seed = parm[1];
+ a = parm[2];
+ b = parm[3];
+ c1 = parm[4];
+ c2 = parm[5];
+ if (!(seed > 0 && 1 <= a && a <= 1000 && 1 <= b && b <= 1000 &&
+ 0 <= c1 && c1 <= c2 && c2 <= 1000))
+ { ret = 1;
+ goto done;
+ }
+ if (G != NULL)
+ { glp_erase_graph(G, G->v_size, G->a_size);
+ glp_set_graph_name(G, "RMFGEN");
+ }
+ csa->rand = rng_create_rand();
+ rng_init_rand(csa->rand, seed);
+ n = gen_rmf(csa, a, b, c1, c2);
+ sprintf(comm[0], "This file was generated by genrmf.");
+ sprintf(comm[1], "The parameters are: a: %d b: %d c1: %d c2: %d",
+ a, b, c1, c2);
+ com1[0] = comm[0];
+ com1[1] = comm[1];
+ print_max_format(csa, n, com1, 2);
+ gen_free_net(n);
+ rng_delete_rand(csa->rand);
+ ret = 0;
+done: return ret;
+}
+
+/**********************************************************************/
+
+#if 0
+int main(int argc, char *argv[])
+{ int seed, a, b, c1, c2, i, parm[1+5];
+ seed = 123;
+ a = b = c1 = c2 = -1;
+ for (i = 1; i < argc; i++)
+ { if (strcmp(argv[i], "-seed") == 0)
+ seed = atoi(argv[++i]);
+ else if (strcmp(argv[i], "-a") == 0)
+ a = atoi(argv[++i]);
+ else if (strcmp(argv[i], "-b") == 0)
+ b = atoi(argv[++i]);
+ else if (strcmp(argv[i], "-c1") == 0)
+ c1 = atoi(argv[++i]);
+ else if (strcmp(argv[i], "-c2") == 0)
+ c2 = atoi(argv[++i]);
+ }
+ if (a < 0 || b < 0 || c1 < 0 || c2 < 0)
+ { xprintf("Usage:\n");
+ xprintf("genrmf [-seed seed] -a frame_size -b depth\n");
+ xprintf(" -c1 cap_range1 -c2 cap_range2\n");
+ }
+ else
+ { parm[1] = seed;
+ parm[2] = a;
+ parm[3] = b;
+ parm[4] = c1;
+ parm[5] = c2;
+ glp_rmfgen(NULL, NULL, NULL, 0, parm);
+ }
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/strong.c b/test/monniaux/glpk-4.65/src/api/strong.c
new file mode 100644
index 00000000..9ddcacfb
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/strong.c
@@ -0,0 +1,110 @@
+/* strong.c (find all strongly connected components of graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "mc13d.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_strong_comp - find all strongly connected components of graph
+*
+* SYNOPSIS
+*
+* int glp_strong_comp(glp_graph *G, int v_num);
+*
+* DESCRIPTION
+*
+* The routine glp_strong_comp finds all strongly connected components
+* of the specified graph.
+*
+* The parameter v_num specifies an offset of the field of type int
+* in the vertex data block, to which the routine stores the number of
+* a strongly connected component containing that vertex. If v_num < 0,
+* no component numbers are stored.
+*
+* The components are numbered in arbitrary order from 1 to nc, where
+* nc is the total number of components found, 0 <= nc <= |V|. However,
+* the component numbering has the property that for every arc (i->j)
+* in the graph the condition num(i) >= num(j) holds.
+*
+* RETURNS
+*
+* The routine returns nc, the total number of components found. */
+
+int glp_strong_comp(glp_graph *G, int v_num)
+{ glp_vertex *v;
+ glp_arc *a;
+ int i, k, last, n, na, nc, *icn, *ip, *lenr, *ior, *ib, *lowl,
+ *numb, *prev;
+ if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
+ xerror("glp_strong_comp: v_num = %d; invalid offset\n",
+ v_num);
+ n = G->nv;
+ if (n == 0)
+ { nc = 0;
+ goto done;
+ }
+ na = G->na;
+ icn = xcalloc(1+na, sizeof(int));
+ ip = xcalloc(1+n, sizeof(int));
+ lenr = xcalloc(1+n, sizeof(int));
+ ior = xcalloc(1+n, sizeof(int));
+ ib = xcalloc(1+n, sizeof(int));
+ lowl = xcalloc(1+n, sizeof(int));
+ numb = xcalloc(1+n, sizeof(int));
+ prev = xcalloc(1+n, sizeof(int));
+ k = 1;
+ for (i = 1; i <= n; i++)
+ { v = G->v[i];
+ ip[i] = k;
+ for (a = v->out; a != NULL; a = a->t_next)
+ icn[k++] = a->head->i;
+ lenr[i] = k - ip[i];
+ }
+ xassert(na == k-1);
+ nc = mc13d(n, icn, ip, lenr, ior, ib, lowl, numb, prev);
+ if (v_num >= 0)
+ { xassert(ib[1] == 1);
+ for (k = 1; k <= nc; k++)
+ { last = (k < nc ? ib[k+1] : n+1);
+ xassert(ib[k] < last);
+ for (i = ib[k]; i < last; i++)
+ { v = G->v[ior[i]];
+ memcpy((char *)v->data + v_num, &k, sizeof(int));
+ }
+ }
+ }
+ xfree(icn);
+ xfree(ip);
+ xfree(lenr);
+ xfree(ior);
+ xfree(ib);
+ xfree(lowl);
+ xfree(numb);
+ xfree(prev);
+done: return nc;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/topsort.c b/test/monniaux/glpk-4.65/src/api/topsort.c
new file mode 100644
index 00000000..971937f2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/topsort.c
@@ -0,0 +1,123 @@
+/* topsort.c (topological sorting of acyclic digraph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_top_sort - topological sorting of acyclic digraph
+*
+* SYNOPSIS
+*
+* int glp_top_sort(glp_graph *G, int v_num);
+*
+* DESCRIPTION
+*
+* The routine glp_top_sort performs topological sorting of vertices of
+* the specified acyclic digraph.
+*
+* The parameter v_num specifies an offset of the field of type int in
+* the vertex data block, to which the routine stores the vertex number
+* assigned. If v_num < 0, vertex numbers are not stored.
+*
+* The vertices are numbered from 1 to n, where n is the total number
+* of vertices in the graph. The vertex numbering has the property that
+* for every arc (i->j) in the graph the condition num(i) < num(j)
+* holds. Special case num(i) = 0 means that vertex i is not assigned a
+* number, because the graph is *not* acyclic.
+*
+* RETURNS
+*
+* If the graph is acyclic and therefore all the vertices have been
+* assigned numbers, the routine glp_top_sort returns zero. Otherwise,
+* if the graph is not acyclic, the routine returns the number of
+* vertices which have not been numbered, i.e. for which num(i) = 0. */
+
+static int top_sort(glp_graph *G, int num[])
+{ glp_arc *a;
+ int i, j, cnt, top, *stack, *indeg;
+ /* allocate working arrays */
+ indeg = xcalloc(1+G->nv, sizeof(int));
+ stack = xcalloc(1+G->nv, sizeof(int));
+ /* determine initial indegree of each vertex; push into the stack
+ the vertices having zero indegree */
+ top = 0;
+ for (i = 1; i <= G->nv; i++)
+ { num[i] = indeg[i] = 0;
+ for (a = G->v[i]->in; a != NULL; a = a->h_next)
+ indeg[i]++;
+ if (indeg[i] == 0)
+ stack[++top] = i;
+ }
+ /* assign numbers to vertices in the sorted order */
+ cnt = 0;
+ while (top > 0)
+ { /* pull vertex i from the stack */
+ i = stack[top--];
+ /* it has zero indegree in the current graph */
+ xassert(indeg[i] == 0);
+ /* so assign it a next number */
+ xassert(num[i] == 0);
+ num[i] = ++cnt;
+ /* remove vertex i from the current graph, update indegree of
+ its adjacent vertices, and push into the stack new vertices
+ whose indegree becomes zero */
+ for (a = G->v[i]->out; a != NULL; a = a->t_next)
+ { j = a->head->i;
+ /* there exists arc (i->j) in the graph */
+ xassert(indeg[j] > 0);
+ indeg[j]--;
+ if (indeg[j] == 0)
+ stack[++top] = j;
+ }
+ }
+ /* free working arrays */
+ xfree(indeg);
+ xfree(stack);
+ return G->nv - cnt;
+}
+
+int glp_top_sort(glp_graph *G, int v_num)
+{ glp_vertex *v;
+ int i, cnt, *num;
+ if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
+ xerror("glp_top_sort: v_num = %d; invalid offset\n", v_num);
+ if (G->nv == 0)
+ { cnt = 0;
+ goto done;
+ }
+ num = xcalloc(1+G->nv, sizeof(int));
+ cnt = top_sort(G, num);
+ if (v_num >= 0)
+ { for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_num, &num[i], sizeof(int));
+ }
+ }
+ xfree(num);
+done: return cnt;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wcliqex.c b/test/monniaux/glpk-4.65/src/api/wcliqex.c
new file mode 100644
index 00000000..53c2d521
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wcliqex.c
@@ -0,0 +1,122 @@
+/* wcliqex.c (find maximum weight clique with exact algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+#include "wclique.h"
+
+static void set_edge(int nv, unsigned char a[], int i, int j)
+{ int k;
+ xassert(1 <= j && j < i && i <= nv);
+ k = ((i - 1) * (i - 2)) / 2 + (j - 1);
+ a[k / CHAR_BIT] |=
+ (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
+ return;
+}
+
+int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set)
+{ /* find maximum weight clique with exact algorithm */
+ glp_arc *e;
+ int i, j, k, len, x, *w, *ind, ret = 0;
+ unsigned char *a;
+ double s, t;
+ if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
+ xerror("glp_wclique_exact: v_wgt = %d; invalid parameter\n",
+ v_wgt);
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_wclique_exact: v_set = %d; invalid parameter\n",
+ v_set);
+ if (G->nv == 0)
+ { /* empty graph has only empty clique */
+ if (sol != NULL) *sol = 0.0;
+ return 0;
+ }
+ /* allocate working arrays */
+ w = xcalloc(1+G->nv, sizeof(int));
+ ind = xcalloc(1+G->nv, sizeof(int));
+ len = G->nv; /* # vertices */
+ len = len * (len - 1) / 2; /* # entries in lower triangle */
+ len = (len + (CHAR_BIT - 1)) / CHAR_BIT; /* # bytes needed */
+ a = xcalloc(len, sizeof(char));
+ memset(a, 0, len * sizeof(char));
+ /* determine vertex weights */
+ s = 0.0;
+ for (i = 1; i <= G->nv; i++)
+ { if (v_wgt >= 0)
+ { memcpy(&t, (char *)G->v[i]->data + v_wgt, sizeof(double));
+ if (!(0.0 <= t && t <= (double)INT_MAX && t == floor(t)))
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ w[i] = (int)t;
+ }
+ else
+ w[i] = 1;
+ s += (double)w[i];
+ }
+ if (s > (double)INT_MAX)
+ { ret = GLP_EDATA;
+ goto done;
+ }
+ /* build the adjacency matrix */
+ for (i = 1; i <= G->nv; i++)
+ { for (e = G->v[i]->in; e != NULL; e = e->h_next)
+ { j = e->tail->i;
+ /* there exists edge (j,i) in the graph */
+ if (i > j) set_edge(G->nv, a, i, j);
+ }
+ for (e = G->v[i]->out; e != NULL; e = e->t_next)
+ { j = e->head->i;
+ /* there exists edge (i,j) in the graph */
+ if (i > j) set_edge(G->nv, a, i, j);
+ }
+ }
+ /* find maximum weight clique in the graph */
+ len = wclique(G->nv, w, a, ind);
+ /* compute the clique weight */
+ s = 0.0;
+ for (k = 1; k <= len; k++)
+ { i = ind[k];
+ xassert(1 <= i && i <= G->nv);
+ s += (double)w[i];
+ }
+ if (sol != NULL) *sol = s;
+ /* mark vertices included in the clique */
+ if (v_set >= 0)
+ { x = 0;
+ for (i = 1; i <= G->nv; i++)
+ memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
+ x = 1;
+ for (k = 1; k <= len; k++)
+ { i = ind[k];
+ memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
+ }
+ }
+done: /* free working arrays */
+ xfree(w);
+ xfree(ind);
+ xfree(a);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/weak.c b/test/monniaux/glpk-4.65/src/api/weak.c
new file mode 100644
index 00000000..027c09c1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/weak.c
@@ -0,0 +1,150 @@
+/* weak.c (find all weakly connected components of graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_weak_comp - find all weakly connected components of graph
+*
+* SYNOPSIS
+*
+* int glp_weak_comp(glp_graph *G, int v_num);
+*
+* DESCRIPTION
+*
+* The routine glp_weak_comp finds all weakly connected components of
+* the specified graph.
+*
+* The parameter v_num specifies an offset of the field of type int
+* in the vertex data block, to which the routine stores the number of
+* a (weakly) connected component containing that vertex. If v_num < 0,
+* no component numbers are stored.
+*
+* The components are numbered in arbitrary order from 1 to nc, where
+* nc is the total number of components found, 0 <= nc <= |V|.
+*
+* RETURNS
+*
+* The routine returns nc, the total number of components found. */
+
+int glp_weak_comp(glp_graph *G, int v_num)
+{ glp_vertex *v;
+ glp_arc *a;
+ int f, i, j, nc, nv, pos1, pos2, *prev, *next, *list;
+ if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
+ xerror("glp_weak_comp: v_num = %d; invalid offset\n", v_num);
+ nv = G->nv;
+ if (nv == 0)
+ { nc = 0;
+ goto done;
+ }
+ /* allocate working arrays */
+ prev = xcalloc(1+nv, sizeof(int));
+ next = xcalloc(1+nv, sizeof(int));
+ list = xcalloc(1+nv, sizeof(int));
+ /* if vertex i is unlabelled, prev[i] is the index of previous
+ unlabelled vertex, and next[i] is the index of next unlabelled
+ vertex; if vertex i is labelled, then prev[i] < 0, and next[i]
+ is the connected component number */
+ /* initially all vertices are unlabelled */
+ f = 1;
+ for (i = 1; i <= nv; i++)
+ prev[i] = i - 1, next[i] = i + 1;
+ next[nv] = 0;
+ /* main loop (until all vertices have been labelled) */
+ nc = 0;
+ while (f != 0)
+ { /* take an unlabelled vertex */
+ i = f;
+ /* and remove it from the list of unlabelled vertices */
+ f = next[i];
+ if (f != 0) prev[f] = 0;
+ /* label the vertex; it begins a new component */
+ prev[i] = -1, next[i] = ++nc;
+ /* breadth first search */
+ list[1] = i, pos1 = pos2 = 1;
+ while (pos1 <= pos2)
+ { /* dequeue vertex i */
+ i = list[pos1++];
+ /* consider all arcs incoming to vertex i */
+ for (a = G->v[i]->in; a != NULL; a = a->h_next)
+ { /* vertex j is adjacent to vertex i */
+ j = a->tail->i;
+ if (prev[j] >= 0)
+ { /* vertex j is unlabelled */
+ /* remove it from the list of unlabelled vertices */
+ if (prev[j] == 0)
+ f = next[j];
+ else
+ next[prev[j]] = next[j];
+ if (next[j] == 0)
+ ;
+ else
+ prev[next[j]] = prev[j];
+ /* label the vertex */
+ prev[j] = -1, next[j] = nc;
+ /* and enqueue it for further consideration */
+ list[++pos2] = j;
+ }
+ }
+ /* consider all arcs outgoing from vertex i */
+ for (a = G->v[i]->out; a != NULL; a = a->t_next)
+ { /* vertex j is adjacent to vertex i */
+ j = a->head->i;
+ if (prev[j] >= 0)
+ { /* vertex j is unlabelled */
+ /* remove it from the list of unlabelled vertices */
+ if (prev[j] == 0)
+ f = next[j];
+ else
+ next[prev[j]] = next[j];
+ if (next[j] == 0)
+ ;
+ else
+ prev[next[j]] = prev[j];
+ /* label the vertex */
+ prev[j] = -1, next[j] = nc;
+ /* and enqueue it for further consideration */
+ list[++pos2] = j;
+ }
+ }
+ }
+ }
+ /* store component numbers */
+ if (v_num >= 0)
+ { for (i = 1; i <= nv; i++)
+ { v = G->v[i];
+ memcpy((char *)v->data + v_num, &next[i], sizeof(int));
+ }
+ }
+ /* free working arrays */
+ xfree(prev);
+ xfree(next);
+ xfree(list);
+done: return nc;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrasn.c b/test/monniaux/glpk-4.65/src/api/wrasn.c
new file mode 100644
index 00000000..81433da8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrasn.c
@@ -0,0 +1,107 @@
+/* wrasn.c (write assignment problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_write_asnprob - write assignment problem data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_write_asnprob(glp_graph *G, int v_set, int a_cost,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_asnprob writes assignment problem data in
+* DIMACS format to a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, const char
+ *fname)
+{ glp_file *fp;
+ glp_vertex *v;
+ glp_arc *a;
+ int i, k, count = 0, ret;
+ double cost;
+ if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
+ xerror("glp_write_asnprob: v_set = %d; invalid offset\n",
+ v_set);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_write_asnprob: a_cost = %d; invalid offset\n",
+ a_cost);
+ xprintf("Writing assignment problem data to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "c %s\n",
+ G->name == NULL ? "unknown" : G->name), count++;
+ xfprintf(fp, "p asn %d %d\n", G->nv, G->na), count++;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ if (v_set >= 0)
+ memcpy(&k, (char *)v->data + v_set, sizeof(int));
+ else
+ k = (v->out != NULL ? 0 : 1);
+ if (k == 0)
+ xfprintf(fp, "n %d\n", i), count++;
+ }
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 1.0;
+ xfprintf(fp, "a %d %d %.*g\n",
+ a->tail->i, a->head->i, DBL_DIG, cost), count++;
+ }
+ }
+ xfprintf(fp, "c eof\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrcc.c b/test/monniaux/glpk-4.65/src/api/wrcc.c
new file mode 100644
index 00000000..2069c8ac
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrcc.c
@@ -0,0 +1,102 @@
+/* wrcc.c (write graph in DIMACS clique/coloring format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_write_ccdata - write graph in DIMACS clique/coloring format
+*
+* SYNOPSIS
+*
+* int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_ccdata writes the specified graph in DIMACS
+* clique/coloring format to a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname)
+{ glp_file *fp;
+ glp_vertex *v;
+ glp_arc *e;
+ int i, count = 0, ret;
+ double w;
+ if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
+ xerror("glp_write_ccdata: v_wgt = %d; invalid offset\n",
+ v_wgt);
+ xprintf("Writing graph to '%s'\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "c %s\n",
+ G->name == NULL ? "unknown" : G->name), count++;
+ xfprintf(fp, "p edge %d %d\n", G->nv, G->na), count++;
+ if (v_wgt >= 0)
+ { for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ memcpy(&w, (char *)v->data + v_wgt, sizeof(double));
+ if (w != 1.0)
+ xfprintf(fp, "n %d %.*g\n", i, DBL_DIG, w), count++;
+ }
+ }
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (e = v->out; e != NULL; e = e->t_next)
+ xfprintf(fp, "e %d %d\n", e->tail->i, e->head->i), count++;
+ }
+ xfprintf(fp, "c eof\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/**********************************************************************/
+
+int glp_write_graph(glp_graph *G, const char *fname)
+{ return
+ glp_write_ccdata(G, -1, fname);
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrcnf.c b/test/monniaux/glpk-4.65/src/api/wrcnf.c
new file mode 100644
index 00000000..c7974386
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrcnf.c
@@ -0,0 +1,87 @@
+/* wrcnf.c (write CNF-SAT problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+int glp_write_cnfsat(glp_prob *P, const char *fname)
+{ /* write CNF-SAT problem data in DIMACS format */
+ glp_file *fp = NULL;
+ GLPAIJ *aij;
+ int i, j, len, count = 0, ret;
+ char s[50];
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_write_cnfsat: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (glp_check_cnfsat(P) != 0)
+ { xprintf("glp_write_cnfsat: problem object does not encode CNF-"
+ "SAT instance\n");
+ ret = 1;
+ goto done;
+ }
+ xprintf("Writing CNF-SAT problem data to '%s'...\n", fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "c %s\n",
+ P->name == NULL ? "unknown" : P->name), count++;
+ xfprintf(fp, "p cnf %d %d\n", P->n, P->m), count++;
+ for (i = 1; i <= P->m; i++)
+ { len = 0;
+ for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { j = aij->col->j;
+ if (aij->val < 0.0) j = -j;
+ sprintf(s, "%d", j);
+ if (len > 0 && len + 1 + strlen(s) > 72)
+ xfprintf(fp, "\n"), count++, len = 0;
+ xfprintf(fp, "%s%s", len == 0 ? "" : " ", s);
+ if (len > 0) len++;
+ len += strlen(s);
+ }
+ if (len > 0 && len + 1 + 1 > 72)
+ xfprintf(fp, "\n"), count++, len = 0;
+ xfprintf(fp, "%s0\n", len == 0 ? "" : " "), count++;
+ }
+ xfprintf(fp, "c eof\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wript.c b/test/monniaux/glpk-4.65/src/api/wript.c
new file mode 100644
index 00000000..f2ca802c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wript.c
@@ -0,0 +1,124 @@
+/* wript.c (write interior-point solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_write_ipt - write interior-point solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_write_ipt(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_ipt writes interior-point solution to a text
+* file in GLPK format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_ipt(glp_prob *P, const char *fname)
+{ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, count, ret = 1;
+ char *s;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_write_ipt: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_write_ipt: fname = %d; invalid parameter\n", fname)
+ ;
+ xprintf("Writing interior-point solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w"), count = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* write comment lines */
+ glp_format(fp, "c %-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name), count++;
+ glp_format(fp, "c %-12s%d\n", "Rows:", P->m), count++;
+ glp_format(fp, "c %-12s%d\n", "Columns:", P->n), count++;
+ glp_format(fp, "c %-12s%d\n", "Non-zeros:", P->nnz), count++;
+ switch (P->ipt_stat)
+ { case GLP_OPT: s = "OPTIMAL"; break;
+ case GLP_INFEAS: s = "INFEASIBLE (INTERMEDIATE)"; break;
+ case GLP_NOFEAS: s = "INFEASIBLE (FINAL)"; break;
+ case GLP_UNDEF: s = "UNDEFINED"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s\n", "Status:", s), count++;
+ switch (P->dir)
+ { case GLP_MIN: s = "MINimum"; break;
+ case GLP_MAX: s = "MAXimum"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->ipt_obj, s), count++;
+ glp_format(fp, "c\n"), count++;
+ /* write solution line */
+ glp_format(fp, "s ipt %d %d ", P->m, P->n), count++;
+ switch (P->ipt_stat)
+ { case GLP_OPT: glp_format(fp, "o"); break;
+ case GLP_INFEAS: glp_format(fp, "i"); break;
+ case GLP_NOFEAS: glp_format(fp, "n"); break;
+ case GLP_UNDEF: glp_format(fp, "u"); break;
+ default: glp_format(fp, "?"); break;
+ }
+ glp_format(fp, " %.*g\n", DBL_DIG, P->ipt_obj);
+ /* write row solution descriptor lines */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ glp_format(fp, "i %d %.*g %.*g\n", i, DBL_DIG, row->pval,
+ DBL_DIG, row->dval), count++;
+ }
+ /* write column solution descriptor lines */
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ glp_format(fp, "j %d %.*g %.*g\n", j, DBL_DIG, col->pval,
+ DBL_DIG, col->dval), count++;
+ }
+ /* write end line */
+ glp_format(fp, "e o f\n"), count++;
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* interior-point solution has been successfully written */
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL)
+ glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrmaxf.c b/test/monniaux/glpk-4.65/src/api/wrmaxf.c
new file mode 100644
index 00000000..d3101ca8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrmaxf.c
@@ -0,0 +1,104 @@
+/* wrmaxf.c (write maximum flow problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_write_maxflow - write maximum flow problem data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap,
+* const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_maxflow writes maximum flow problem data in
+* DIMACS format to a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap,
+ const char *fname)
+{ glp_file *fp;
+ glp_vertex *v;
+ glp_arc *a;
+ int i, count = 0, ret;
+ double cap;
+ if (!(1 <= s && s <= G->nv))
+ xerror("glp_write_maxflow: s = %d; source node number out of r"
+ "ange\n", s);
+ if (!(1 <= t && t <= G->nv))
+ xerror("glp_write_maxflow: t = %d: sink node number out of ran"
+ "ge\n", t);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_write_mincost: a_cap = %d; invalid offset\n",
+ a_cap);
+ xprintf("Writing maximum flow problem data to '%s'...\n",
+ fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "c %s\n",
+ G->name == NULL ? "unknown" : G->name), count++;
+ xfprintf(fp, "p max %d %d\n", G->nv, G->na), count++;
+ xfprintf(fp, "n %d s\n", s), count++;
+ xfprintf(fp, "n %d t\n", t), count++;
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { if (a_cap >= 0)
+ memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
+ else
+ cap = 1.0;
+ xfprintf(fp, "a %d %d %.*g\n",
+ a->tail->i, a->head->i, DBL_DIG, cap), count++;
+ }
+ }
+ xfprintf(fp, "c eof\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrmcf.c b/test/monniaux/glpk-4.65/src/api/wrmcf.c
new file mode 100644
index 00000000..0da37f42
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrmcf.c
@@ -0,0 +1,122 @@
+/* wrmcf.c (write min-cost flow problem data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpk.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_write_mincost - write min-cost flow probl. data in DIMACS format
+*
+* SYNOPSIS
+*
+* int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+* int a_cost, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_mincost writes minimum cost flow problem data
+* in DIMACS format to a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, const char *fname)
+{ glp_file *fp;
+ glp_vertex *v;
+ glp_arc *a;
+ int i, count = 0, ret;
+ double rhs, low, cap, cost;
+ if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
+ xerror("glp_write_mincost: v_rhs = %d; invalid offset\n",
+ v_rhs);
+ if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
+ xerror("glp_write_mincost: a_low = %d; invalid offset\n",
+ a_low);
+ if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
+ xerror("glp_write_mincost: a_cap = %d; invalid offset\n",
+ a_cap);
+ if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
+ xerror("glp_write_mincost: a_cost = %d; invalid offset\n",
+ a_cost);
+ xprintf("Writing min-cost flow problem data to '%s'...\n",
+ fname);
+ fp = glp_open(fname, "w");
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xfprintf(fp, "c %s\n",
+ G->name == NULL ? "unknown" : G->name), count++;
+ xfprintf(fp, "p min %d %d\n", G->nv, G->na), count++;
+ if (v_rhs >= 0)
+ { for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double));
+ if (rhs != 0.0)
+ xfprintf(fp, "n %d %.*g\n", i, DBL_DIG, rhs), count++;
+ }
+ }
+ for (i = 1; i <= G->nv; i++)
+ { v = G->v[i];
+ for (a = v->out; a != NULL; a = a->t_next)
+ { if (a_low >= 0)
+ memcpy(&low, (char *)a->data + a_low, sizeof(double));
+ else
+ low = 0.0;
+ if (a_cap >= 0)
+ memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
+ else
+ cap = 1.0;
+ if (a_cost >= 0)
+ memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
+ else
+ cost = 0.0;
+ xfprintf(fp, "a %d %d %.*g %.*g %.*g\n",
+ a->tail->i, a->head->i, DBL_DIG, low, DBL_DIG, cap,
+ DBL_DIG, cost), count++;
+ }
+ }
+ xfprintf(fp, "c eof\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrmip.c b/test/monniaux/glpk-4.65/src/api/wrmip.c
new file mode 100644
index 00000000..407a5fec
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrmip.c
@@ -0,0 +1,122 @@
+/* wrmip.c (write MIP solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_write_mip - write MIP solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_write_mip(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_mip writes MIP solution to a text file in GLPK
+* format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_mip(glp_prob *P, const char *fname)
+{ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, count, ret = 1;
+ char *s;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_write_mip: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_write_mip: fname = %d; invalid parameter\n", fname)
+ ;
+ xprintf("Writing MIP solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w"), count = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* write comment lines */
+ glp_format(fp, "c %-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name), count++;
+ glp_format(fp, "c %-12s%d\n", "Rows:", P->m), count++;
+ glp_format(fp, "c %-12s%d\n", "Columns:", P->n), count++;
+ glp_format(fp, "c %-12s%d\n", "Non-zeros:", P->nnz), count++;
+ switch (P->mip_stat)
+ { case GLP_OPT: s = "INTEGER OPTIMAL"; break;
+ case GLP_FEAS: s = "INTEGER NON-OPTIMAL"; break;
+ case GLP_NOFEAS: s = "INTEGER EMPTY"; break;
+ case GLP_UNDEF: s = "INTEGER UNDEFINED"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s\n", "Status:", s), count++;
+ switch (P->dir)
+ { case GLP_MIN: s = "MINimum"; break;
+ case GLP_MAX: s = "MAXimum"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->mip_obj, s), count++;
+ glp_format(fp, "c\n"), count++;
+ /* write solution line */
+ glp_format(fp, "s mip %d %d ", P->m, P->n), count++;
+ switch (P->mip_stat)
+ { case GLP_OPT: glp_format(fp, "o"); break;
+ case GLP_FEAS: glp_format(fp, "f"); break;
+ case GLP_NOFEAS: glp_format(fp, "n"); break;
+ case GLP_UNDEF: glp_format(fp, "u"); break;
+ default: glp_format(fp, "?"); break;
+ }
+ glp_format(fp, " %.*g\n", DBL_DIG, P->mip_obj);
+ /* write row solution descriptor lines */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ glp_format(fp, "i %d %.*g\n", i, DBL_DIG, row->mipx), count++;
+ }
+ /* write column solution descriptor lines */
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ glp_format(fp, "j %d %.*g\n", j, DBL_DIG, col->mipx), count++;
+ }
+ /* write end line */
+ glp_format(fp, "e o f\n"), count++;
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* MIP solution has been successfully written */
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL)
+ glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrprob.c b/test/monniaux/glpk-4.65/src/api/wrprob.c
new file mode 100644
index 00000000..99983d35
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrprob.c
@@ -0,0 +1,166 @@
+/* wrprob.c (write problem data in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+#define xfprintf glp_format
+
+/***********************************************************************
+* NAME
+*
+* glp_write_prob - write problem data in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_write_prob(glp_prob *P, int flags, const char *fname);
+*
+* The routine glp_write_prob writes problem data in GLPK LP/MIP format
+* to a text file.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_prob(glp_prob *P, int flags, const char *fname)
+{ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int mip, i, j, count, ret;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_write_prob: P = %p; invalid problem object\n",
+ P);
+#endif
+ if (flags != 0)
+ xerror("glp_write_prob: flags = %d; invalid parameter\n",
+ flags);
+ if (fname == NULL)
+ xerror("glp_write_prob: fname = %d; invalid parameter\n",
+ fname);
+ xprintf("Writing problem data to '%s'...\n", fname);
+ fp = glp_open(fname, "w"), count = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ /* write problem line */
+ mip = (glp_get_num_int(P) > 0);
+ xfprintf(fp, "p %s %s %d %d %d\n", !mip ? "lp" : "mip",
+ P->dir == GLP_MIN ? "min" : P->dir == GLP_MAX ? "max" : "???",
+ P->m, P->n, P->nnz), count++;
+ if (P->name != NULL)
+ xfprintf(fp, "n p %s\n", P->name), count++;
+ if (P->obj != NULL)
+ xfprintf(fp, "n z %s\n", P->obj), count++;
+ /* write row descriptors */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->type == GLP_FX && row->lb == 0.0)
+ goto skip1;
+ xfprintf(fp, "i %d ", i), count++;
+ if (row->type == GLP_FR)
+ xfprintf(fp, "f\n");
+ else if (row->type == GLP_LO)
+ xfprintf(fp, "l %.*g\n", DBL_DIG, row->lb);
+ else if (row->type == GLP_UP)
+ xfprintf(fp, "u %.*g\n", DBL_DIG, row->ub);
+ else if (row->type == GLP_DB)
+ xfprintf(fp, "d %.*g %.*g\n", DBL_DIG, row->lb, DBL_DIG,
+ row->ub);
+ else if (row->type == GLP_FX)
+ xfprintf(fp, "s %.*g\n", DBL_DIG, row->lb);
+ else
+ xassert(row != row);
+skip1: if (row->name != NULL)
+ xfprintf(fp, "n i %d %s\n", i, row->name), count++;
+ }
+ /* write column descriptors */
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (!mip && col->type == GLP_LO && col->lb == 0.0)
+ goto skip2;
+ if (mip && col->kind == GLP_IV && col->type == GLP_DB &&
+ col->lb == 0.0 && col->ub == 1.0)
+ goto skip2;
+ xfprintf(fp, "j %d ", j), count++;
+ if (mip)
+ { if (col->kind == GLP_CV)
+ xfprintf(fp, "c ");
+ else if (col->kind == GLP_IV)
+ xfprintf(fp, "i ");
+ else
+ xassert(col != col);
+ }
+ if (col->type == GLP_FR)
+ xfprintf(fp, "f\n");
+ else if (col->type == GLP_LO)
+ xfprintf(fp, "l %.*g\n", DBL_DIG, col->lb);
+ else if (col->type == GLP_UP)
+ xfprintf(fp, "u %.*g\n", DBL_DIG, col->ub);
+ else if (col->type == GLP_DB)
+ xfprintf(fp, "d %.*g %.*g\n", DBL_DIG, col->lb, DBL_DIG,
+ col->ub);
+ else if (col->type == GLP_FX)
+ xfprintf(fp, "s %.*g\n", DBL_DIG, col->lb);
+ else
+ xassert(col != col);
+skip2: if (col->name != NULL)
+ xfprintf(fp, "n j %d %s\n", j, col->name), count++;
+ }
+ /* write objective coefficient descriptors */
+ if (P->c0 != 0.0)
+ xfprintf(fp, "a 0 0 %.*g\n", DBL_DIG, P->c0), count++;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->coef != 0.0)
+ xfprintf(fp, "a 0 %d %.*g\n", j, DBL_DIG, col->coef),
+ count++;
+ }
+ /* write constraint coefficient descriptors */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ xfprintf(fp, "a %d %d %.*g\n", i, aij->col->j, DBL_DIG,
+ aij->val), count++;
+ }
+ /* write end line */
+ xfprintf(fp, "e o f\n"), count++;
+#if 0 /* FIXME */
+ xfflush(fp);
+#endif
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ ret = 1;
+ goto done;
+ }
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL) glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/api/wrsol.c b/test/monniaux/glpk-4.65/src/api/wrsol.c
new file mode 100644
index 00000000..66c69233
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/api/wrsol.c
@@ -0,0 +1,174 @@
+/* wrsol.c (write basic solution in GLPK format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2010-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_write_sol - write basic solution in GLPK format
+*
+* SYNOPSIS
+*
+* int glp_write_sol(glp_prob *P, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine glp_write_sol writes basic solution to a text file in
+* GLPK format.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero. Otherwise
+* it prints an error message and returns non-zero. */
+
+int glp_write_sol(glp_prob *P, const char *fname)
+{ glp_file *fp;
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j, count, ret = 1;
+ char *s;
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_write_sol: P = %p; invalid problem object\n", P);
+#endif
+ if (fname == NULL)
+ xerror("glp_write_sol: fname = %d; invalid parameter\n", fname)
+ ;
+ xprintf("Writing basic solution to '%s'...\n", fname);
+ fp = glp_open(fname, "w"), count = 0;
+ if (fp == NULL)
+ { xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* write comment lines */
+ glp_format(fp, "c %-12s%s\n", "Problem:",
+ P->name == NULL ? "" : P->name), count++;
+ glp_format(fp, "c %-12s%d\n", "Rows:", P->m), count++;
+ glp_format(fp, "c %-12s%d\n", "Columns:", P->n), count++;
+ glp_format(fp, "c %-12s%d\n", "Non-zeros:", P->nnz), count++;
+ switch (glp_get_status(P))
+ { case GLP_OPT: s = "OPTIMAL"; break;
+ case GLP_FEAS: s = "FEASIBLE"; break;
+ case GLP_INFEAS: s = "INFEASIBLE (INTERMEDIATE)"; break;
+ case GLP_NOFEAS: s = "INFEASIBLE (FINAL)"; break;
+ case GLP_UNBND: s = "UNBOUNDED"; break;
+ case GLP_UNDEF: s = "UNDEFINED"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s\n", "Status:", s), count++;
+ switch (P->dir)
+ { case GLP_MIN: s = "MINimum"; break;
+ case GLP_MAX: s = "MAXimum"; break;
+ default: s = "???"; break;
+ }
+ glp_format(fp, "c %-12s%s%s%.10g (%s)\n", "Objective:",
+ P->obj == NULL ? "" : P->obj,
+ P->obj == NULL ? "" : " = ", P->obj_val, s), count++;
+ glp_format(fp, "c\n"), count++;
+ /* write solution line */
+ glp_format(fp, "s bas %d %d ", P->m, P->n), count++;
+ switch (P->pbs_stat)
+ { case GLP_UNDEF: glp_format(fp, "u"); break;
+ case GLP_FEAS: glp_format(fp, "f"); break;
+ case GLP_INFEAS: glp_format(fp, "i"); break;
+ case GLP_NOFEAS: glp_format(fp, "n"); break;
+ default: glp_format(fp, "?"); break;
+ }
+ glp_format(fp, " ");
+ switch (P->dbs_stat)
+ { case GLP_UNDEF: glp_format(fp, "u"); break;
+ case GLP_FEAS: glp_format(fp, "f"); break;
+ case GLP_INFEAS: glp_format(fp, "i"); break;
+ case GLP_NOFEAS: glp_format(fp, "n"); break;
+ default: glp_format(fp, "?"); break;
+ }
+ glp_format(fp, " %.*g\n", DBL_DIG, P->obj_val);
+ /* write row solution descriptor lines */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ glp_format(fp, "i %d ", i), count++;
+ switch (row->stat)
+ { case GLP_BS:
+ glp_format(fp, "b");
+ break;
+ case GLP_NL:
+ glp_format(fp, "l");
+ break;
+ case GLP_NU:
+ glp_format(fp, "u");
+ break;
+ case GLP_NF:
+ glp_format(fp, "f");
+ break;
+ case GLP_NS:
+ glp_format(fp, "s");
+ break;
+ default:
+ xassert(row != row);
+ }
+ glp_format(fp, " %.*g %.*g\n", DBL_DIG, row->prim, DBL_DIG,
+ row->dual);
+ }
+ /* write column solution descriptor lines */
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ glp_format(fp, "j %d ", j), count++;
+ switch (col->stat)
+ { case GLP_BS:
+ glp_format(fp, "b");
+ break;
+ case GLP_NL:
+ glp_format(fp, "l");
+ break;
+ case GLP_NU:
+ glp_format(fp, "u");
+ break;
+ case GLP_NF:
+ glp_format(fp, "f");
+ break;
+ case GLP_NS:
+ glp_format(fp, "s");
+ break;
+ default:
+ xassert(col != col);
+ }
+ glp_format(fp, " %.*g %.*g\n", DBL_DIG, col->prim, DBL_DIG,
+ col->dual);
+ }
+ /* write end line */
+ glp_format(fp, "e o f\n"), count++;
+ if (glp_ioerr(fp))
+ { xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
+ goto done;
+ }
+ /* basic solution has been successfully written */
+ xprintf("%d lines were written\n", count);
+ ret = 0;
+done: if (fp != NULL)
+ glp_close(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/btf.c b/test/monniaux/glpk-4.65/src/bflib/btf.c
new file mode 100644
index 00000000..993c9ca1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/btf.c
@@ -0,0 +1,569 @@
+/* btf.c (sparse block triangular LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "btf.h"
+#include "env.h"
+#include "luf.h"
+#include "mc13d.h"
+#include "mc21a.h"
+
+/***********************************************************************
+* btf_store_a_cols - store pattern of matrix A in column-wise format
+*
+* This routine stores the pattern (that is, only indices of non-zero
+* elements) of the original matrix A in column-wise format.
+*
+* On exit the routine returns the number of non-zeros in matrix A. */
+
+int btf_store_a_cols(BTF *btf, int (*col)(void *info, int j, int ind[],
+ double val[]), void *info, int ind[], double val[])
+{ int n = btf->n;
+ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ int j, len, ptr, nnz;
+ nnz = 0;
+ for (j = 1; j <= n; j++)
+ { /* get j-th column */
+ len = col(info, j, ind, val);
+ xassert(0 <= len && len <= n);
+ /* reserve locations for j-th column */
+ if (len > 0)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ }
+ sva_reserve_cap(sva, ac_ref+(j-1), len);
+ }
+ /* store pattern of j-th column */
+ ptr = ac_ptr[j];
+ memcpy(&sv_ind[ptr], &ind[1], len * sizeof(int));
+ ac_len[j] = len;
+ nnz += len;
+ }
+ return nnz;
+}
+
+/***********************************************************************
+* btf_make_blocks - permutations to block triangular form
+*
+* This routine analyzes the pattern of the original matrix A and
+* determines permutation matrices P and Q such that A = P * A~* Q,
+* where A~ is an upper block triangular matrix.
+*
+* On exit the routine returns symbolic rank of matrix A. */
+
+int btf_make_blocks(BTF *btf)
+{ int n = btf->n;
+ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ int *pp_ind = btf->pp_ind;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int *qq_inv = btf->qq_inv;
+ int *beg = btf->beg;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ int i, j, rank, *iperm, *pr, *arp, *cv, *out, *ip, *lenr, *lowl,
+ *numb, *prev;
+ /* determine column permutation matrix M such that matrix A * M
+ * has zero-free diagonal */
+ iperm = qq_inv; /* matrix M */
+ pr = btf->p1_ind; /* working array */
+ arp = btf->p1_inv; /* working array */
+ cv = btf->q1_ind; /* working array */
+ out = btf->q1_inv; /* working array */
+ rank = mc21a(n, sv_ind, ac_ptr, ac_len, iperm, pr, arp, cv, out);
+ xassert(0 <= rank && rank <= n);
+ if (rank < n)
+ { /* A is structurally singular (rank is its symbolic rank) */
+ goto done;
+ }
+ /* build pattern of matrix A * M */
+ ip = pp_ind; /* working array */
+ lenr = qq_ind; /* working array */
+ for (j = 1; j <= n; j++)
+ { ip[j] = ac_ptr[iperm[j]];
+ lenr[j] = ac_len[iperm[j]];
+ }
+ /* determine symmetric permutation matrix S such that matrix
+ * S * (A * M) * S' = A~ is upper block triangular */
+ lowl = btf->p1_ind; /* working array */
+ numb = btf->p1_inv; /* working array */
+ prev = btf->q1_ind; /* working array */
+ btf->num =
+ mc13d(n, sv_ind, ip, lenr, pp_inv, beg, lowl, numb, prev);
+ xassert(beg[1] == 1);
+ beg[btf->num+1] = n+1;
+ /* A * M = S' * A~ * S ==> A = S' * A~ * (S * M') */
+ /* determine permutation matrix P = S' */
+ for (j = 1; j <= n; j++)
+ pp_ind[pp_inv[j]] = j;
+ /* determine permutation matrix Q = S * M' = P' * M' */
+ for (i = 1; i <= n; i++)
+ qq_ind[i] = iperm[pp_inv[i]];
+ for (i = 1; i <= n; i++)
+ qq_inv[qq_ind[i]] = i;
+done: return rank;
+}
+
+/***********************************************************************
+* btf_check_blocks - check structure of matrix A~
+*
+* This routine checks that structure of upper block triangular matrix
+* A~ is correct.
+*
+* NOTE: For testing/debugging only. */
+
+void btf_check_blocks(BTF *btf)
+{ int n = btf->n;
+ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ int *pp_ind = btf->pp_ind;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int *qq_inv = btf->qq_inv;
+ int num = btf->num;
+ int *beg = btf->beg;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ int i, ii, j, jj, k, size, ptr, end, diag;
+ xassert(n > 0);
+ /* check permutation matrices P and Q */
+ for (k = 1; k <= n; k++)
+ { xassert(1 <= pp_ind[k] && pp_ind[k] <= n);
+ xassert(pp_inv[pp_ind[k]] == k);
+ xassert(1 <= qq_ind[k] && qq_ind[k] <= n);
+ xassert(qq_inv[qq_ind[k]] == k);
+ }
+ /* check that matrix A~ is upper block triangular with non-zero
+ * diagonal */
+ xassert(1 <= num && num <= n);
+ xassert(beg[1] == 1);
+ xassert(beg[num+1] == n+1);
+ /* walk thru blocks of A~ */
+ for (k = 1; k <= num; k++)
+ { /* determine size of k-th block */
+ size = beg[k+1] - beg[k];
+ xassert(size >= 1);
+ /* walk thru columns of k-th block */
+ for (jj = beg[k]; jj < beg[k+1]; jj++)
+ { diag = 0;
+ /* jj-th column of A~ = j-th column of A */
+ j = qq_ind[jj];
+ /* walk thru elements of j-th column of A */
+ ptr = ac_ptr[j];
+ end = ptr + ac_len[j];
+ for (; ptr < end; ptr++)
+ { /* determine row index of a[i,j] */
+ i = sv_ind[ptr];
+ /* i-th row of A = ii-th row of A~ */
+ ii = pp_ind[i];
+ /* a~[ii,jj] should not be below k-th block */
+ xassert(ii < beg[k+1]);
+ if (ii == jj)
+ { /* non-zero diagonal element of A~ encountered */
+ diag = 1;
+ }
+ }
+ xassert(diag);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* btf_build_a_rows - build matrix A in row-wise format
+*
+* This routine builds the row-wise representation of matrix A in the
+* right part of SVA using its column-wise representation.
+*
+* The working array len should have at least 1+n elements (len[0] is
+* not used). */
+
+void btf_build_a_rows(BTF *btf, int len[/*1+n*/])
+{ int n = btf->n;
+ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int ar_ref = btf->ar_ref;
+ int *ar_ptr = &sva->ptr[ar_ref-1];
+ int *ar_len = &sva->len[ar_ref-1];
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ int i, j, end, nnz, ptr, ptr1;
+ /* calculate the number of non-zeros in each row of matrix A and
+ * the total number of non-zeros */
+ nnz = 0;
+ for (i = 1; i <= n; i++)
+ len[i] = 0;
+ for (j = 1; j <= n; j++)
+ { nnz += ac_len[j];
+ for (end = (ptr = ac_ptr[j]) + ac_len[j]; ptr < end; ptr++)
+ len[sv_ind[ptr]]++;
+ }
+ /* we need at least nnz free locations in SVA */
+ if (sva->r_ptr - sva->m_ptr < nnz)
+ { sva_more_space(sva, nnz);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ /* reserve locations for rows of matrix A */
+ for (i = 1; i <= n; i++)
+ { if (len[i] > 0)
+ sva_reserve_cap(sva, ar_ref-1+i, len[i]);
+ ar_len[i] = len[i];
+ }
+ /* walk thru columns of matrix A and build its rows */
+ for (j = 1; j <= n; j++)
+ { for (end = (ptr = ac_ptr[j]) + ac_len[j]; ptr < end; ptr++)
+ { i = sv_ind[ptr];
+ sv_ind[ptr1 = ar_ptr[i] + (--len[i])] = j;
+ sv_val[ptr1] = sv_val[ptr];
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* btf_a_solve - solve system A * x = b
+*
+* This routine solves the system A * x = b, where A is the original
+* matrix.
+*
+* On entry the array b should contain elements of the right-hand size
+* vector b in locations b[1], ..., b[n], where n is the order of the
+* matrix A. On exit the array x will contain elements of the solution
+* vector in locations x[1], ..., x[n]. Note that the array b will be
+* clobbered on exit.
+*
+* The routine also uses locations [1], ..., [max_size] of two working
+* arrays w1 and w2, where max_size is the maximal size of diagonal
+* blocks in BT-factorization (max_size <= n). */
+
+void btf_a_solve(BTF *btf, double b[/*1+n*/], double x[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/])
+{ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int num = btf->num;
+ int *beg = btf->beg;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ double *bb = w1;
+ double *xx = w2;
+ LUF luf;
+ int i, j, jj, k, beg_k, flag;
+ double t;
+ for (k = num; k >= 1; k--)
+ { /* determine order of diagonal block A~[k,k] */
+ luf.n = beg[k+1] - (beg_k = beg[k]);
+ if (luf.n == 1)
+ { /* trivial case */
+ /* solve system A~[k,k] * X[k] = B[k] */
+ t = x[qq_ind[beg_k]] =
+ b[pp_inv[beg_k]] / btf->vr_piv[beg_k];
+ /* substitute X[k] into other equations */
+ if (t != 0.0)
+ { int ptr = ac_ptr[qq_ind[beg_k]];
+ int end = ptr + ac_len[qq_ind[beg_k]];
+ for (; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * t;
+ }
+ }
+ else
+ { /* general case */
+ /* construct B[k] */
+ flag = 0;
+ for (i = 1; i <= luf.n; i++)
+ { if ((bb[i] = b[pp_inv[i + (beg_k-1)]]) != 0.0)
+ flag = 1;
+ }
+ /* solve system A~[k,k] * X[k] = B[k] */
+ if (!flag)
+ { /* B[k] = 0, so X[k] = 0 */
+ for (j = 1; j <= luf.n; j++)
+ x[qq_ind[j + (beg_k-1)]] = 0.0;
+ continue;
+ }
+ luf.sva = sva;
+ luf.fr_ref = btf->fr_ref + (beg_k-1);
+ luf.fc_ref = btf->fc_ref + (beg_k-1);
+ luf.vr_ref = btf->vr_ref + (beg_k-1);
+ luf.vr_piv = btf->vr_piv + (beg_k-1);
+ luf.vc_ref = btf->vc_ref + (beg_k-1);
+ luf.pp_ind = btf->p1_ind + (beg_k-1);
+ luf.pp_inv = btf->p1_inv + (beg_k-1);
+ luf.qq_ind = btf->q1_ind + (beg_k-1);
+ luf.qq_inv = btf->q1_inv + (beg_k-1);
+ luf_f_solve(&luf, bb);
+ luf_v_solve(&luf, bb, xx);
+ /* store X[k] and substitute it into other equations */
+ for (j = 1; j <= luf.n; j++)
+ { jj = j + (beg_k-1);
+ t = x[qq_ind[jj]] = xx[j];
+ if (t != 0.0)
+ { int ptr = ac_ptr[qq_ind[jj]];
+ int end = ptr + ac_len[qq_ind[jj]];
+ for (; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * t;
+ }
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* btf_at_solve - solve system A'* x = b
+*
+* This routine solves the system A'* x = b, where A' is a matrix
+* transposed to the original matrix A.
+*
+* On entry the array b should contain elements of the right-hand size
+* vector b in locations b[1], ..., b[n], where n is the order of the
+* matrix A. On exit the array x will contain elements of the solution
+* vector in locations x[1], ..., x[n]. Note that the array b will be
+* clobbered on exit.
+*
+* The routine also uses locations [1], ..., [max_size] of two working
+* arrays w1 and w2, where max_size is the maximal size of diagonal
+* blocks in BT-factorization (max_size <= n). */
+
+void btf_at_solve(BTF *btf, double b[/*1+n*/], double x[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/])
+{ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int num = btf->num;
+ int *beg = btf->beg;
+ int ar_ref = btf->ar_ref;
+ int *ar_ptr = &sva->ptr[ar_ref-1];
+ int *ar_len = &sva->len[ar_ref-1];
+ double *bb = w1;
+ double *xx = w2;
+ LUF luf;
+ int i, j, jj, k, beg_k, flag;
+ double t;
+ for (k = 1; k <= num; k++)
+ { /* determine order of diagonal block A~[k,k] */
+ luf.n = beg[k+1] - (beg_k = beg[k]);
+ if (luf.n == 1)
+ { /* trivial case */
+ /* solve system A~'[k,k] * X[k] = B[k] */
+ t = x[pp_inv[beg_k]] =
+ b[qq_ind[beg_k]] / btf->vr_piv[beg_k];
+ /* substitute X[k] into other equations */
+ if (t != 0.0)
+ { int ptr = ar_ptr[pp_inv[beg_k]];
+ int end = ptr + ar_len[pp_inv[beg_k]];
+ for (; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * t;
+ }
+ }
+ else
+ { /* general case */
+ /* construct B[k] */
+ flag = 0;
+ for (i = 1; i <= luf.n; i++)
+ { if ((bb[i] = b[qq_ind[i + (beg_k-1)]]) != 0.0)
+ flag = 1;
+ }
+ /* solve system A~'[k,k] * X[k] = B[k] */
+ if (!flag)
+ { /* B[k] = 0, so X[k] = 0 */
+ for (j = 1; j <= luf.n; j++)
+ x[pp_inv[j + (beg_k-1)]] = 0.0;
+ continue;
+ }
+ luf.sva = sva;
+ luf.fr_ref = btf->fr_ref + (beg_k-1);
+ luf.fc_ref = btf->fc_ref + (beg_k-1);
+ luf.vr_ref = btf->vr_ref + (beg_k-1);
+ luf.vr_piv = btf->vr_piv + (beg_k-1);
+ luf.vc_ref = btf->vc_ref + (beg_k-1);
+ luf.pp_ind = btf->p1_ind + (beg_k-1);
+ luf.pp_inv = btf->p1_inv + (beg_k-1);
+ luf.qq_ind = btf->q1_ind + (beg_k-1);
+ luf.qq_inv = btf->q1_inv + (beg_k-1);
+ luf_vt_solve(&luf, bb, xx);
+ luf_ft_solve(&luf, xx);
+ /* store X[k] and substitute it into other equations */
+ for (j = 1; j <= luf.n; j++)
+ { jj = j + (beg_k-1);
+ t = x[pp_inv[jj]] = xx[j];
+ if (t != 0.0)
+ { int ptr = ar_ptr[pp_inv[jj]];
+ int end = ptr + ar_len[pp_inv[jj]];
+ for (; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * t;
+ }
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* btf_at_solve1 - solve system A'* y = e' to cause growth in y
+*
+* This routine is a special version of btf_at_solve. It solves the
+* system A'* y = e' = e + delta e, where A' is a matrix transposed to
+* the original matrix A, e is the specified right-hand side vector,
+* and delta e is a vector of +1 and -1 chosen to cause growth in the
+* solution vector y.
+*
+* On entry the array e should contain elements of the right-hand size
+* vector e in locations e[1], ..., e[n], where n is the order of the
+* matrix A. On exit the array y will contain elements of the solution
+* vector in locations y[1], ..., y[n]. Note that the array e will be
+* clobbered on exit.
+*
+* The routine also uses locations [1], ..., [max_size] of two working
+* arrays w1 and w2, where max_size is the maximal size of diagonal
+* blocks in BT-factorization (max_size <= n). */
+
+void btf_at_solve1(BTF *btf, double e[/*1+n*/], double y[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/])
+{ SVA *sva = btf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int num = btf->num;
+ int *beg = btf->beg;
+ int ar_ref = btf->ar_ref;
+ int *ar_ptr = &sva->ptr[ar_ref-1];
+ int *ar_len = &sva->len[ar_ref-1];
+ double *ee = w1;
+ double *yy = w2;
+ LUF luf;
+ int i, j, jj, k, beg_k, ptr, end;
+ double e_k, y_k;
+ for (k = 1; k <= num; k++)
+ { /* determine order of diagonal block A~[k,k] */
+ luf.n = beg[k+1] - (beg_k = beg[k]);
+ if (luf.n == 1)
+ { /* trivial case */
+ /* determine E'[k] = E[k] + delta E[k] */
+ e_k = e[qq_ind[beg_k]];
+ e_k = (e_k >= 0.0 ? e_k + 1.0 : e_k - 1.0);
+ /* solve system A~'[k,k] * Y[k] = E[k] */
+ y_k = y[pp_inv[beg_k]] = e_k / btf->vr_piv[beg_k];
+ /* substitute Y[k] into other equations */
+ ptr = ar_ptr[pp_inv[beg_k]];
+ end = ptr + ar_len[pp_inv[beg_k]];
+ for (; ptr < end; ptr++)
+ e[sv_ind[ptr]] -= sv_val[ptr] * y_k;
+ }
+ else
+ { /* general case */
+ /* construct E[k] */
+ for (i = 1; i <= luf.n; i++)
+ ee[i] = e[qq_ind[i + (beg_k-1)]];
+ /* solve system A~'[k,k] * Y[k] = E[k] + delta E[k] */
+ luf.sva = sva;
+ luf.fr_ref = btf->fr_ref + (beg_k-1);
+ luf.fc_ref = btf->fc_ref + (beg_k-1);
+ luf.vr_ref = btf->vr_ref + (beg_k-1);
+ luf.vr_piv = btf->vr_piv + (beg_k-1);
+ luf.vc_ref = btf->vc_ref + (beg_k-1);
+ luf.pp_ind = btf->p1_ind + (beg_k-1);
+ luf.pp_inv = btf->p1_inv + (beg_k-1);
+ luf.qq_ind = btf->q1_ind + (beg_k-1);
+ luf.qq_inv = btf->q1_inv + (beg_k-1);
+ luf_vt_solve1(&luf, ee, yy);
+ luf_ft_solve(&luf, yy);
+ /* store Y[k] and substitute it into other equations */
+ for (j = 1; j <= luf.n; j++)
+ { jj = j + (beg_k-1);
+ y_k = y[pp_inv[jj]] = yy[j];
+ ptr = ar_ptr[pp_inv[jj]];
+ end = ptr + ar_len[pp_inv[jj]];
+ for (; ptr < end; ptr++)
+ e[sv_ind[ptr]] -= sv_val[ptr] * y_k;
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* btf_estimate_norm - estimate 1-norm of inv(A)
+*
+* This routine estimates 1-norm of inv(A) by one step of inverse
+* iteration for the small singular vector as described in [1]. This
+* involves solving two systems of equations:
+*
+* A'* y = e,
+*
+* A * z = y,
+*
+* where A' is a matrix transposed to A, and e is a vector of +1 and -1
+* chosen to cause growth in y. Then
+*
+* estimate 1-norm of inv(A) = (1-norm of z) / (1-norm of y)
+*
+* REFERENCES
+*
+* 1. G.E.Forsythe, M.A.Malcolm, C.B.Moler. Computer Methods for
+* Mathematical Computations. Prentice-Hall, Englewood Cliffs, N.J.,
+* pp. 30-62 (subroutines DECOMP and SOLVE). */
+
+double btf_estimate_norm(BTF *btf, double w1[/*1+n*/], double
+ w2[/*1+n*/], double w3[/*1+n*/], double w4[/*1+n*/])
+{ int n = btf->n;
+ double *e = w1;
+ double *y = w2;
+ double *z = w1;
+ int i;
+ double y_norm, z_norm;
+ /* compute y = inv(A') * e to cause growth in y */
+ for (i = 1; i <= n; i++)
+ e[i] = 0.0;
+ btf_at_solve1(btf, e, y, w3, w4);
+ /* compute 1-norm of y = sum |y[i]| */
+ y_norm = 0.0;
+ for (i = 1; i <= n; i++)
+ y_norm += (y[i] >= 0.0 ? +y[i] : -y[i]);
+ /* compute z = inv(A) * y */
+ btf_a_solve(btf, y, z, w3, w4);
+ /* compute 1-norm of z = sum |z[i]| */
+ z_norm = 0.0;
+ for (i = 1; i <= n; i++)
+ z_norm += (z[i] >= 0.0 ? +z[i] : -z[i]);
+ /* estimate 1-norm of inv(A) = (1-norm of z) / (1-norm of y) */
+ return z_norm / y_norm;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/btf.h b/test/monniaux/glpk-4.65/src/bflib/btf.h
new file mode 100644
index 00000000..3f1b5926
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/btf.h
@@ -0,0 +1,207 @@
+/* btf.h (sparse block triangular LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef BTF_H
+#define BTF_H
+
+#include "sva.h"
+
+/***********************************************************************
+* The structure BTF describes BT-factorization, which is sparse block
+* triangular LU-factorization.
+*
+* The BT-factorization has the following format:
+*
+* A = P * A~ * Q, (1)
+*
+* where A is a given (unsymmetric) square matrix, A~ is an upper block
+* triangular matrix (see below), P and Q are permutation matrices. All
+* the matrices have the same order n.
+*
+* The matrix A~, which is a permuted version of the original matrix A,
+* has the following structure:
+*
+* A~[1,1] A~[1,2] ... A~[1,num-1] A~[1,num]
+*
+* A~[2,2] ... A~[2,num-1] A~[2,num]
+*
+* . . . . . . . . . (2)
+*
+* A~[num-1,num-1] A~[num-1,num]
+*
+* A~[num,num]
+*
+* where A~[i,j] is a submatrix called a "block," num is the number of
+* blocks. Each diagonal block A~[k,k] is a non-singular square matrix,
+* and each subdiagonal block A~[i,j], i > j, is a zero submatrix, thus
+* A~ is an upper block triangular matrix.
+*
+* Permutation matrices P and Q are stored in ordinary arrays in both
+* row- and column-like formats.
+*
+* The original matrix A is stored in both row- and column-wise sparse
+* formats in the associated sparse vector area (SVA). Should note that
+* elements of all diagonal blocks A~[k,k] in matrix A are set to zero
+* (i.e. removed), so only elements of non-diagonal blocks are stored.
+*
+* Each diagonal block A~[k,k], 1 <= k <= num, is stored in the form of
+* LU-factorization (see the module LUF). */
+
+typedef struct BTF BTF;
+
+struct BTF
+{ /* sparse block triangular LU-factorization */
+ int n;
+ /* order of matrices A, A~, P, Q */
+ SVA *sva;
+ /* associated sparse vector area used to store rows and columns
+ * of matrix A as well as sparse vectors for LU-factorizations of
+ * all diagonal blocks A~[k,k] */
+ /*--------------------------------------------------------------*/
+ /* matrix P */
+ int *pp_ind; /* int pp_ind[1+n]; */
+ /* pp_ind[i] = j means that P[i,j] = 1 */
+ int *pp_inv; /* int pp_inv[1+n]; */
+ /* pp_inv[j] = i means that P[i,j] = 1 */
+ /* if i-th row of matrix A is i'-th row of matrix A~, then
+ * pp_ind[i] = i' and pp_inv[i'] = i */
+ /*--------------------------------------------------------------*/
+ /* matrix Q */
+ int *qq_ind; /* int qq_ind[1+n]; */
+ /* qq_ind[i] = j means that Q[i,j] = 1 */
+ int *qq_inv; /* int qq_inv[1+n]; */
+ /* qq_inv[j] = i means that Q[i,j] = 1 */
+ /* if j-th column of matrix A is j'-th column of matrix A~, then
+ * qq_ind[j'] = j and qq_inv[j] = j' */
+ /*--------------------------------------------------------------*/
+ /* block triangular structure of matrix A~ */
+ int num;
+ /* number of diagonal blocks, 1 <= num <= n */
+ int *beg; /* int beg[1+num+1]; */
+ /* beg[0] is not used;
+ * beg[k], 1 <= k <= num, is index of first row/column of k-th
+ * block of matrix A~;
+ * beg[num+1] is always n+1;
+ * note that order (size) of k-th diagonal block can be computed
+ * as beg[k+1] - beg[k] */
+ /*--------------------------------------------------------------*/
+ /* original matrix A in row-wise format */
+ /* NOTE: elements of all diagonal blocks A~[k,k] are removed */
+ int ar_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * row of matrix A */
+#if 0 + 0
+ int *ar_ptr = &sva->ptr[ar_ref-1];
+ /* ar_ptr[0] is not used;
+ * ar_ptr[i], 1 <= i <= n, is pointer to i-th row in SVA */
+ int *ar_len = &sva->ptr[ar_ref-1];
+ /* ar_len[0] is not used;
+ * ar_len[i], 1 <= i <= n, is length of i-th row */
+#endif
+ /*--------------------------------------------------------------*/
+ /* original matrix A in column-wise format */
+ /* NOTE: elements of all diagonal blocks A~[k,k] are removed */
+ int ac_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * column of matrix A */
+#if 0 + 0
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ /* ac_ptr[0] is not used;
+ * ac_ptr[j], 1 <= j <= n, is pointer to j-th column in SVA */
+ int *ac_len = &sva->ptr[ac_ref-1];
+ /* ac_len[0] is not used;
+ * ac_len[j], 1 <= j <= n, is length of j-th column */
+#endif
+ /*--------------------------------------------------------------*/
+ /* LU-factorizations of diagonal blocks A~[k,k] */
+ /* to decrease overhead expenses similar arrays for all LUFs are
+ * packed into a single array; for example, elements fr_ptr[1],
+ * ..., fr_ptr[n1], where n1 = beg[2] - beg[1], are related to
+ * LUF for first diagonal block A~[1,1], elements fr_ptr[n1+1],
+ * ..., fr_ptr[n1+n2], where n2 = beg[3] - beg[2], are related to
+ * LUF for second diagonal block A~[2,2], etc.; in other words,
+ * elements related to LUF for k-th diagonal block A~[k,k] have
+ * indices beg[k], beg[k]+1, ..., beg[k+1]-1 */
+ /* for details about LUF see description of the LUF module */
+ int fr_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ row of matrix F for first diagonal block A~[1,1] */
+ int fc_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ column of matrix F for first diagonal block A~[1,1] */
+ int vr_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ row of matrix V for first diagonal block A~[1,1] */
+ double *vr_piv; /* double vr_piv[1+n]; */
+ /* vr_piv[0] is not used;
+ vr_piv[1,...,n] are pivot elements for all diagonal blocks */
+ int vc_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ column of matrix V for first diagonal block A~[1,1] */
+ int *p1_ind; /* int p1_ind[1+n]; */
+ int *p1_inv; /* int p1_inv[1+n]; */
+ int *q1_ind; /* int q1_ind[1+n]; */
+ int *q1_inv; /* int q1_inv[1+n]; */
+ /* permutation matrices P and Q for all diagonal blocks */
+};
+
+#define btf_store_a_cols _glp_btf_store_a_cols
+int btf_store_a_cols(BTF *btf, int (*col)(void *info, int j, int ind[],
+ double val[]), void *info, int ind[], double val[]);
+/* store pattern of matrix A in column-wise format */
+
+#define btf_make_blocks _glp_btf_make_blocks
+int btf_make_blocks(BTF *btf);
+/* permutations to block triangular form */
+
+#define btf_check_blocks _glp_btf_check_blocks
+void btf_check_blocks(BTF *btf);
+/* check structure of matrix A~ */
+
+#define btf_build_a_rows _glp_btf_build_a_rows
+void btf_build_a_rows(BTF *btf, int len[/*1+n*/]);
+/* build matrix A in row-wise format */
+
+#define btf_a_solve _glp_btf_a_solve
+void btf_a_solve(BTF *btf, double b[/*1+n*/], double x[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/]);
+/* solve system A * x = b */
+
+#define btf_at_solve _glp_btf_at_solve
+void btf_at_solve(BTF *btf, double b[/*1+n*/], double x[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/]);
+/* solve system A'* x = b */
+
+#define btf_at_solve1 _glp_btf_at_solve1
+void btf_at_solve1(BTF *btf, double e[/*1+n*/], double y[/*1+n*/],
+ double w1[/*1+n*/], double w2[/*1+n*/]);
+/* solve system A'* y = e' to cause growth in y */
+
+#define btf_estimate_norm _glp_btf_estimate_norm
+double btf_estimate_norm(BTF *btf, double w1[/*1+n*/], double
+ w2[/*1+n*/], double w3[/*1+n*/], double w4[/*1+n*/]);
+/* estimate 1-norm of inv(A) */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/btfint.c b/test/monniaux/glpk-4.65/src/bflib/btfint.c
new file mode 100644
index 00000000..378d3a81
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/btfint.c
@@ -0,0 +1,407 @@
+/* btfint.c (interface to BT-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "btfint.h"
+
+BTFINT *btfint_create(void)
+{ /* create interface to BT-factorization */
+ BTFINT *fi;
+ fi = talloc(1, BTFINT);
+ fi->n_max = 0;
+ fi->valid = 0;
+ fi->sva = NULL;
+ fi->btf = NULL;
+ fi->sgf = NULL;
+ fi->sva_n_max = fi->sva_size = 0;
+ fi->delta_n0 = fi->delta_n = 0;
+ fi->sgf_piv_tol = 0.10;
+ fi->sgf_piv_lim = 4;
+ fi->sgf_suhl = 1;
+ fi->sgf_eps_tol = DBL_EPSILON;
+ return fi;
+}
+
+static void factorize_triv(BTFINT *fi, int k, int (*col)(void *info,
+ int j, int ind[], double val[]), void *info)
+{ /* compute LU-factorization of diagonal block A~[k,k] and store
+ * corresponding columns of matrix A except elements of A~[k,k]
+ * (trivial case when the block has unity size) */
+ SVA *sva = fi->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ BTF *btf = fi->btf;
+ int *pp_inv = btf->pp_inv;
+ int *qq_ind = btf->qq_ind;
+ int *beg = btf->beg;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ SGF *sgf = fi->sgf;
+ int *ind = (int *)sgf->vr_max; /* working array */
+ double *val = sgf->work; /* working array */
+ int i, j, t, len, ptr, beg_k;
+ /* diagonal block A~[k,k] has the only element in matrix A~,
+ * which is a~[beg[k],beg[k]] = a[i,j] */
+ beg_k = beg[k];
+ i = pp_inv[beg_k];
+ j = qq_ind[beg_k];
+ /* get j-th column of A */
+ len = col(info, j, ind, val);
+ /* find element a[i,j] = a~[beg[k],beg[k]] in j-th column */
+ for (t = 1; t <= len; t++)
+ { if (ind[t] == i)
+ break;
+ }
+ xassert(t <= len);
+ /* compute LU-factorization of diagonal block A~[k,k], where
+ * F = (1), V = (a[i,j]), P = Q = (1) (see the module LUF) */
+#if 1 /* FIXME */
+ xassert(val[t] != 0.0);
+#endif
+ btf->vr_piv[beg_k] = val[t];
+ btf->p1_ind[beg_k] = btf->p1_inv[beg_k] = 1;
+ btf->q1_ind[beg_k] = btf->q1_inv[beg_k] = 1;
+ /* remove element a[i,j] = a~[beg[k],beg[k]] from j-th column */
+ memmove(&ind[t], &ind[t+1], (len-t) * sizeof(int));
+ memmove(&val[t], &val[t+1], (len-t) * sizeof(double));
+ len--;
+ /* and store resulting j-th column of A into BTF */
+ if (len > 0)
+ { /* reserve locations for j-th column of A */
+ if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, ac_ref+(j-1), len);
+ /* store j-th column of A (except elements of A~[k,k]) */
+ ptr = ac_ptr[j];
+ memcpy(&sv_ind[ptr], &ind[1], len * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], len * sizeof(double));
+ ac_len[j] = len;
+ }
+ return;
+}
+
+static int factorize_block(BTFINT *fi, int k, int (*col)(void *info,
+ int j, int ind[], double val[]), void *info)
+{ /* compute LU-factorization of diagonal block A~[k,k] and store
+ * corresponding columns of matrix A except elements of A~[k,k]
+ * (general case) */
+ SVA *sva = fi->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ BTF *btf = fi->btf;
+ int *pp_ind = btf->pp_ind;
+ int *qq_ind = btf->qq_ind;
+ int *beg = btf->beg;
+ int ac_ref = btf->ac_ref;
+ int *ac_ptr = &sva->ptr[ac_ref-1];
+ int *ac_len = &sva->len[ac_ref-1];
+ SGF *sgf = fi->sgf;
+ int *ind = (int *)sgf->vr_max; /* working array */
+ double *val = sgf->work; /* working array */
+ LUF luf;
+ int *vc_ptr, *vc_len, *vc_cap;
+ int i, ii, j, jj, t, len, cnt, ptr, beg_k;
+ /* construct fake LUF for LU-factorization of A~[k,k] */
+ sgf->luf = &luf;
+ luf.n = beg[k+1] - (beg_k = beg[k]);
+ luf.sva = sva;
+ luf.fr_ref = btf->fr_ref + (beg_k-1);
+ luf.fc_ref = btf->fc_ref + (beg_k-1);
+ luf.vr_ref = btf->vr_ref + (beg_k-1);
+ luf.vr_piv = btf->vr_piv + (beg_k-1);
+ luf.vc_ref = btf->vc_ref + (beg_k-1);
+ luf.pp_ind = btf->p1_ind + (beg_k-1);
+ luf.pp_inv = btf->p1_inv + (beg_k-1);
+ luf.qq_ind = btf->q1_ind + (beg_k-1);
+ luf.qq_inv = btf->q1_inv + (beg_k-1);
+ /* process columns of k-th block of matrix A~ */
+ vc_ptr = &sva->ptr[luf.vc_ref-1];
+ vc_len = &sva->len[luf.vc_ref-1];
+ vc_cap = &sva->cap[luf.vc_ref-1];
+ for (jj = 1; jj <= luf.n; jj++)
+ { /* jj-th column of A~ = j-th column of A */
+ j = qq_ind[jj + (beg_k-1)];
+ /* get j-th column of A */
+ len = col(info, j, ind, val);
+ /* move elements of diagonal block A~[k,k] to the beginning of
+ * the column list */
+ cnt = 0;
+ for (t = 1; t <= len; t++)
+ { /* i = row index of element a[i,j] */
+ i = ind[t];
+ /* i-th row of A = ii-th row of A~ */
+ ii = pp_ind[i];
+ if (ii >= beg_k)
+ { /* a~[ii,jj] = a[i,j] is in diagonal block A~[k,k] */
+ double temp;
+ cnt++;
+ ind[t] = ind[cnt];
+ ind[cnt] = ii - (beg_k-1); /* local index */
+ temp = val[t], val[t] = val[cnt], val[cnt] = temp;
+ }
+ }
+ /* first cnt elements in the column list give jj-th column of
+ * diagonal block A~[k,k], which is initial matrix V in LUF */
+ /* enlarge capacity of jj-th column of V = A~[k,k] */
+ if (vc_cap[jj] < cnt)
+ { if (sva->r_ptr - sva->m_ptr < cnt)
+ { sva_more_space(sva, cnt);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, luf.vc_ref+(jj-1), cnt, 0);
+ }
+ /* store jj-th column of V = A~[k,k] */
+ ptr = vc_ptr[jj];
+ memcpy(&sv_ind[ptr], &ind[1], cnt * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], cnt * sizeof(double));
+ vc_len[jj] = cnt;
+ /* other (len-cnt) elements in the column list are stored in
+ * j-th column of the original matrix A */
+ len -= cnt;
+ if (len > 0)
+ { /* reserve locations for j-th column of A */
+ if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, ac_ref-1+j, len);
+ /* store j-th column of A (except elements of A~[k,k]) */
+ ptr = ac_ptr[j];
+ memcpy(&sv_ind[ptr], &ind[cnt+1], len * sizeof(int));
+ memcpy(&sv_val[ptr], &val[cnt+1], len * sizeof(double));
+ ac_len[j] = len;
+ }
+ }
+ /* compute LU-factorization of diagonal block A~[k,k]; may note
+ * that A~[k,k] is irreducible (strongly connected), so singleton
+ * phase will have no effect */
+ k = sgf_factorize(sgf, 0 /* disable singleton phase */);
+ /* now left (dynamic) part of SVA should be empty (wichtig!) */
+ xassert(sva->m_ptr == 1);
+ return k;
+}
+
+int btfint_factorize(BTFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info)
+{ /* compute BT-factorization of specified matrix A */
+ SVA *sva;
+ BTF *btf;
+ SGF *sgf;
+ int k, rank;
+ xassert(n > 0);
+ fi->valid = 0;
+ /* create sparse vector area (SVA), if necessary */
+ sva = fi->sva;
+ if (sva == NULL)
+ { int sva_n_max = fi->sva_n_max;
+ int sva_size = fi->sva_size;
+ if (sva_n_max == 0)
+ sva_n_max = 6 * n;
+ if (sva_size == 0)
+ sva_size = 10 * n;
+ sva = fi->sva = sva_create_area(sva_n_max, sva_size);
+ }
+ /* allocate/reallocate underlying objects, if necessary */
+ if (fi->n_max < n)
+ { int n_max = fi->n_max;
+ if (n_max == 0)
+ n_max = fi->n_max = n + fi->delta_n0;
+ else
+ n_max = fi->n_max = n + fi->delta_n;
+ xassert(n_max >= n);
+ /* allocate/reallocate block triangular factorization (BTF) */
+ btf = fi->btf;
+ if (btf == NULL)
+ { btf = fi->btf = talloc(1, BTF);
+ memset(btf, 0, sizeof(BTF));
+ btf->sva = sva;
+ }
+ else
+ { tfree(btf->pp_ind);
+ tfree(btf->pp_inv);
+ tfree(btf->qq_ind);
+ tfree(btf->qq_inv);
+ tfree(btf->beg);
+ tfree(btf->vr_piv);
+ tfree(btf->p1_ind);
+ tfree(btf->p1_inv);
+ tfree(btf->q1_ind);
+ tfree(btf->q1_inv);
+ }
+ btf->pp_ind = talloc(1+n_max, int);
+ btf->pp_inv = talloc(1+n_max, int);
+ btf->qq_ind = talloc(1+n_max, int);
+ btf->qq_inv = talloc(1+n_max, int);
+ btf->beg = talloc(1+n_max+1, int);
+ btf->vr_piv = talloc(1+n_max, double);
+ btf->p1_ind = talloc(1+n_max, int);
+ btf->p1_inv = talloc(1+n_max, int);
+ btf->q1_ind = talloc(1+n_max, int);
+ btf->q1_inv = talloc(1+n_max, int);
+ /* allocate/reallocate factorizer workspace (SGF) */
+ /* (note that for SGF we could use the size of largest block
+ * rather than n_max) */
+ sgf = fi->sgf;
+ sgf = fi->sgf;
+ if (sgf == NULL)
+ { sgf = fi->sgf = talloc(1, SGF);
+ memset(sgf, 0, sizeof(SGF));
+ }
+ else
+ { tfree(sgf->rs_head);
+ tfree(sgf->rs_prev);
+ tfree(sgf->rs_next);
+ tfree(sgf->cs_head);
+ tfree(sgf->cs_prev);
+ tfree(sgf->cs_next);
+ tfree(sgf->vr_max);
+ tfree(sgf->flag);
+ tfree(sgf->work);
+ }
+ sgf->rs_head = talloc(1+n_max, int);
+ sgf->rs_prev = talloc(1+n_max, int);
+ sgf->rs_next = talloc(1+n_max, int);
+ sgf->cs_head = talloc(1+n_max, int);
+ sgf->cs_prev = talloc(1+n_max, int);
+ sgf->cs_next = talloc(1+n_max, int);
+ sgf->vr_max = talloc(1+n_max, double);
+ sgf->flag = talloc(1+n_max, char);
+ sgf->work = talloc(1+n_max, double);
+ }
+ btf = fi->btf;
+ btf->n = n;
+ sgf = fi->sgf;
+#if 1 /* FIXME */
+ /* initialize SVA */
+ sva->n = 0;
+ sva->m_ptr = 1;
+ sva->r_ptr = sva->size + 1;
+ sva->head = sva->tail = 0;
+#endif
+ /* store pattern of original matrix A in column-wise format */
+ btf->ac_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf_store_a_cols(btf, col, info, btf->pp_ind, btf->vr_piv);
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+#endif
+ /* analyze pattern of original matrix A and determine permutation
+ * matrices P and Q such that A = P * A~* Q, where A~ is an upper
+ * block triangular matrix */
+ rank = btf_make_blocks(btf);
+ if (rank != n)
+ { /* original matrix A is structurally singular */
+ return 1;
+ }
+#ifdef GLP_DEBUG
+ btf_check_blocks(btf);
+#endif
+#if 1 /* FIXME */
+ /* initialize SVA */
+ sva->n = 0;
+ sva->m_ptr = 1;
+ sva->r_ptr = sva->size + 1;
+ sva->head = sva->tail = 0;
+#endif
+ /* allocate sparse vectors in SVA */
+ btf->ar_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf->ac_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf->fr_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf->fc_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf->vr_ref = sva_alloc_vecs(btf->sva, btf->n);
+ btf->vc_ref = sva_alloc_vecs(btf->sva, btf->n);
+ /* setup factorizer control parameters */
+ sgf->updat = 0; /* wichtig! */
+ sgf->piv_tol = fi->sgf_piv_tol;
+ sgf->piv_lim = fi->sgf_piv_lim;
+ sgf->suhl = fi->sgf_suhl;
+ sgf->eps_tol = fi->sgf_eps_tol;
+ /* compute LU-factorizations of diagonal blocks A~[k,k] and also
+ * store corresponding columns of matrix A except elements of all
+ * blocks A~[k,k] */
+ for (k = 1; k <= btf->num; k++)
+ { if (btf->beg[k+1] - btf->beg[k] == 1)
+ { /* trivial case (A~[k,k] has unity order) */
+ factorize_triv(fi, k, col, info);
+ }
+ else
+ { /* general case */
+ if (factorize_block(fi, k, col, info) != 0)
+ return 2; /* factorization of A~[k,k] failed */
+ }
+ }
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+#endif
+ /* build row-wise representation of matrix A */
+ btf_build_a_rows(fi->btf, fi->sgf->rs_head);
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+#endif
+ /* BT-factorization has been successfully computed */
+ fi->valid = 1;
+ return 0;
+}
+
+void btfint_delete(BTFINT *fi)
+{ /* delete interface to BT-factorization */
+ SVA *sva = fi->sva;
+ BTF *btf = fi->btf;
+ SGF *sgf = fi->sgf;
+ if (sva != NULL)
+ sva_delete_area(sva);
+ if (btf != NULL)
+ { tfree(btf->pp_ind);
+ tfree(btf->pp_inv);
+ tfree(btf->qq_ind);
+ tfree(btf->qq_inv);
+ tfree(btf->beg);
+ tfree(btf->vr_piv);
+ tfree(btf->p1_ind);
+ tfree(btf->p1_inv);
+ tfree(btf->q1_ind);
+ tfree(btf->q1_inv);
+ tfree(btf);
+ }
+ if (sgf != NULL)
+ { tfree(sgf->rs_head);
+ tfree(sgf->rs_prev);
+ tfree(sgf->rs_next);
+ tfree(sgf->cs_head);
+ tfree(sgf->cs_prev);
+ tfree(sgf->cs_next);
+ tfree(sgf->vr_max);
+ tfree(sgf->flag);
+ tfree(sgf->work);
+ tfree(sgf);
+ }
+ tfree(fi);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/btfint.h b/test/monniaux/glpk-4.65/src/bflib/btfint.h
new file mode 100644
index 00000000..8d0e70e2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/btfint.h
@@ -0,0 +1,73 @@
+/* btfint.h (interface to BT-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef BTFINT_H
+#define BTFINT_H
+
+#include "btf.h"
+#include "sgf.h"
+
+typedef struct BTFINT BTFINT;
+
+struct BTFINT
+{ /* interface to BT-factorization */
+ int n_max;
+ /* maximal value of n (increased automatically) */
+ int valid;
+ /* factorization is valid only if this flag is set */
+ SVA *sva;
+ /* sparse vector area (SVA) */
+ BTF *btf;
+ /* sparse block triangular LU-factorization */
+ SGF *sgf;
+ /* sparse Gaussian factorizer workspace */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ int sva_n_max, sva_size;
+ /* parameters passed to sva_create_area */
+ int delta_n0, delta_n;
+ /* if n_max = 0, set n_max = n + delta_n0
+ * if n_max < n, set n_max = n + delta_n */
+ double sgf_piv_tol;
+ int sgf_piv_lim;
+ int sgf_suhl;
+ double sgf_eps_tol;
+ /* factorizer control parameters */
+};
+
+#define btfint_create _glp_btfint_create
+BTFINT *btfint_create(void);
+/* create interface to BT-factorization */
+
+#define btfint_factorize _glp_btfint_factorize
+int btfint_factorize(BTFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info);
+/* compute BT-factorization of specified matrix A */
+
+#define btfint_delete _glp_btfint_delete
+void btfint_delete(BTFINT *fi);
+/* delete interface to BT-factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/fhv.c b/test/monniaux/glpk-4.65/src/bflib/fhv.c
new file mode 100644
index 00000000..e4bdf855
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/fhv.c
@@ -0,0 +1,586 @@
+/* fhv.c (sparse updatable FHV-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "fhv.h"
+
+/***********************************************************************
+* fhv_ft_update - update FHV-factorization (Forrest-Tomlin)
+*
+* This routine updates FHV-factorization of the original matrix A
+* after replacing its j-th column by a new one. The routine is based
+* on the method proposed by Forrest and Tomlin [1].
+*
+* The parameter q specifies the number of column of A, which has been
+* replaced, 1 <= q <= n, where n is the order of A.
+*
+* Row indices and numerical values of non-zero elements of the new
+* j-th column of A should be placed in locations aq_ind[1], ...,
+* aq_ind[aq_len] and aq_val[1], ..., aq_val[aq_len], respectively,
+* where aq_len is the number of non-zeros. Neither zero nor duplicate
+* elements are allowed.
+*
+* The working arrays ind, val, and work should have at least 1+n
+* elements (0-th elements are not used).
+*
+* RETURNS
+*
+* 0 The factorization has been successfully updated.
+*
+* 1 New matrix U = P'* V * Q' is upper triangular with zero diagonal
+* element u[s,s]. (Elimination was not performed.)
+*
+* 2 New matrix U = P'* V * Q' is upper triangular, and its diagonal
+* element u[s,s] or u[t,t] is too small in magnitude. (Elimination
+* was not performed.)
+*
+* 3 The same as 2, but after performing elimination.
+*
+* 4 The factorization has not been updated, because maximal number of
+* updates has been reached.
+*
+* 5 Accuracy test failed for the updated factorization.
+*
+* BACKGROUND
+*
+* The routine is based on the updating method proposed by Forrest and
+* Tomlin [1].
+*
+* Let q-th column of the original matrix A have been replaced by new
+* column A[q]. Then, to keep the equality A = F * H * V, q-th column
+* of matrix V should be replaced by column V[q] = inv(F * H) * A[q].
+* From the standpoint of matrix U = P'* V * Q' such replacement is
+* equivalent to replacement of s-th column of matrix U, where s is
+* determined from q by permutation matrix Q. Thus, matrix U loses its
+* upper triangular form and becomes the following:
+*
+* 1 s t n
+* 1 x x * x x x x x x
+* . x * x x x x x x
+* s . . * x x x x x x
+* . . * x x x x x x
+* . . * . x x x x x
+* . . * . . x x x x
+* t . . * . . . x x x
+* . . . . . . . x x
+* n . . . . . . . . x
+*
+* where t is largest row index of a non-zero element in s-th column.
+*
+* The routine makes matrix U upper triangular as follows. First, it
+* moves rows and columns s+1, ..., t by one position to the left and
+* upwards, resp., and moves s-th row and s-th column to position t.
+* Due to such symmetric permutations matrix U becomes the following
+* (note that all diagonal elements remain on the diagonal, and element
+* u[s,s] becomes u[t,t]):
+*
+* 1 s t n
+* 1 x x x x x x * x x
+* . x x x x x * x x
+* s . . x x x x * x x
+* . . . x x x * x x
+* . . . . x x * x x
+* . . . . . x * x x
+* t . . x x x x * x x
+* . . . . . . . x x
+* n . . . . . . . . x
+*
+* Then the routine performs gaussian elimination to eliminate
+* subdiagonal elements u[t,s], ..., u[t,t-1] using diagonal elements
+* u[s,s], ..., u[t-1,t-1] as pivots. During the elimination process
+* the routine permutes neither rows nor columns, so only t-th row is
+* changed. Should note that actually all operations are performed on
+* matrix V = P * U * Q, since matrix U is not stored.
+*
+* To keep the equality A = F * H * V, the routine appends new row-like
+* factor H[k] to matrix H, and every time it applies elementary
+* gaussian transformation to eliminate u[t,j'] = v[p,j] using pivot
+* u[j',j'] = v[i,j], it also adds new element f[p,j] = v[p,j] / v[i,j]
+* (gaussian multiplier) to factor H[k], which initially is a unity
+* matrix. At the end of elimination process the row-like factor H[k]
+* may look as follows:
+*
+* 1 n 1 s t n
+* 1 1 . . . . . . . . 1 1 . . . . . . . .
+* . 1 . . . . . . . . 1 . . . . . . .
+* . . 1 . . . . . . s . . 1 . . . . . .
+* p . x x 1 . x . x . . . . 1 . . . . .
+* . . . . 1 . . . . . . . . 1 . . . .
+* . . . . . 1 . . . . . . . . 1 . . .
+* . . . . . . 1 . . t . . x x x x 1 . .
+* . . . . . . . 1 . . . . . . . . 1 .
+* n . . . . . . . . 1 n . . . . . . . . 1
+*
+* H[k] inv(P) * H[k] * P
+*
+* If, however, s = t, no elimination is needed, in which case no new
+* row-like factor is created.
+*
+* REFERENCES
+*
+* 1. J.J.H.Forrest and J.A.Tomlin, "Updated triangular factors of the
+* basis to maintain sparsity in the product form simplex method,"
+* Math. Prog. 2 (1972), pp. 263-78. */
+
+int fhv_ft_update(FHV *fhv, int q, int aq_len, const int aq_ind[],
+ const double aq_val[], int ind[/*1+n*/], double val[/*1+n*/],
+ double work[/*1+n*/])
+{ LUF *luf = fhv->luf;
+ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int *vr_cap = &sva->cap[vr_ref-1];
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *vc_cap = &sva->cap[vc_ref-1];
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int *hh_ind = fhv->hh_ind;
+ int hh_ref = fhv->hh_ref;
+ int *hh_ptr = &sva->ptr[hh_ref-1];
+ int *hh_len = &sva->len[hh_ref-1];
+#if 1 /* FIXME */
+ const double eps_tol = DBL_EPSILON;
+ const double vpq_tol = 1e-5;
+ const double err_tol = 1e-10;
+#endif
+ int end, i, i_end, i_ptr, j, j_end, j_ptr, k, len, nnz, p, p_end,
+ p_ptr, ptr, q_end, q_ptr, s, t;
+ double f, vpq, temp;
+ /*--------------------------------------------------------------*/
+ /* replace current q-th column of matrix V by new one */
+ /*--------------------------------------------------------------*/
+ xassert(1 <= q && q <= n);
+ /* convert new q-th column of matrix A to dense format */
+ for (i = 1; i <= n; i++)
+ val[i] = 0.0;
+ xassert(0 <= aq_len && aq_len <= n);
+ for (k = 1; k <= aq_len; k++)
+ { i = aq_ind[k];
+ xassert(1 <= i && i <= n);
+ xassert(val[i] == 0.0);
+ xassert(aq_val[k] != 0.0);
+ val[i] = aq_val[k];
+ }
+ /* compute new q-th column of matrix V:
+ * new V[q] = inv(F * H) * (new A[q]) */
+ luf->pp_ind = fhv->p0_ind;
+ luf->pp_inv = fhv->p0_inv;
+ luf_f_solve(luf, val);
+ luf->pp_ind = pp_ind;
+ luf->pp_inv = pp_inv;
+ fhv_h_solve(fhv, val);
+ /* q-th column of V = s-th column of U */
+ s = qq_inv[q];
+ /* determine row number of element v[p,q] that corresponds to
+ * diagonal element u[s,s] */
+ p = pp_inv[s];
+ /* convert new q-th column of V to sparse format;
+ * element v[p,q] = u[s,s] is not included in the element list
+ * and stored separately */
+ vpq = 0.0;
+ len = 0;
+ for (i = 1; i <= n; i++)
+ { temp = val[i];
+#if 1 /* FIXME */
+ if (-eps_tol < temp && temp < +eps_tol)
+#endif
+ /* nop */;
+ else if (i == p)
+ vpq = temp;
+ else
+ { ind[++len] = i;
+ val[len] = temp;
+ }
+ }
+ /* clear q-th column of matrix V */
+ for (q_end = (q_ptr = vc_ptr[q]) + vc_len[q];
+ q_ptr < q_end; q_ptr++)
+ { /* get row index of v[i,q] */
+ i = sv_ind[q_ptr];
+ /* find and remove v[i,q] from i-th row */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ sv_ind[i_ptr] != q; i_ptr++)
+ /* nop */;
+ xassert(i_ptr < i_end);
+ sv_ind[i_ptr] = sv_ind[i_end-1];
+ sv_val[i_ptr] = sv_val[i_end-1];
+ vr_len[i]--;
+ }
+ /* now q-th column of matrix V is empty */
+ vc_len[q] = 0;
+ /* put new q-th column of V (except element v[p,q] = u[s,s]) in
+ * column-wise format */
+ if (len > 0)
+ { if (vc_cap[q] < len)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vc_ref-1+q, len, 0);
+ }
+ ptr = vc_ptr[q];
+ memcpy(&sv_ind[ptr], &ind[1], len * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], len * sizeof(double));
+ vc_len[q] = len;
+ }
+ /* put new q-th column of V (except element v[p,q] = u[s,s]) in
+ * row-wise format, and determine largest row number t such that
+ * u[s,t] != 0 */
+ t = (vpq == 0.0 ? 0 : s);
+ for (k = 1; k <= len; k++)
+ { /* get row index of v[i,q] */
+ i = ind[k];
+ /* put v[i,q] to i-th row */
+ if (vr_cap[i] == vr_len[i])
+ { /* reserve extra locations in i-th row to reduce further
+ * relocations of that row */
+#if 1 /* FIXME */
+ int need = vr_len[i] + 5;
+#endif
+ if (sva->r_ptr - sva->m_ptr < need)
+ { sva_more_space(sva, need);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vr_ref-1+i, need, 0);
+ }
+ sv_ind[ptr = vr_ptr[i] + (vr_len[i]++)] = q;
+ sv_val[ptr] = val[k];
+ /* v[i,q] is non-zero; increase t */
+ if (t < pp_ind[i])
+ t = pp_ind[i];
+ }
+ /*--------------------------------------------------------------*/
+ /* check if matrix U is already upper triangular */
+ /*--------------------------------------------------------------*/
+ /* check if there is a spike in s-th column of matrix U, which
+ * is q-th column of matrix V */
+ if (s >= t)
+ { /* no spike; matrix U is already upper triangular */
+ /* store its diagonal element u[s,s] = v[p,q] */
+ vr_piv[p] = vpq;
+ if (s > t)
+ { /* matrix U is structurally singular, because its diagonal
+ * element u[s,s] = v[p,q] is exact zero */
+ xassert(vpq == 0.0);
+ return 1;
+ }
+#if 1 /* FIXME */
+ else if (-vpq_tol < vpq && vpq < +vpq_tol)
+#endif
+ { /* matrix U is not well conditioned, because its diagonal
+ * element u[s,s] = v[p,q] is too small in magnitude */
+ return 2;
+ }
+ else
+ { /* normal case */
+ return 0;
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* perform implicit symmetric permutations of rows and columns */
+ /* of matrix U */
+ /*--------------------------------------------------------------*/
+ /* currently v[p,q] = u[s,s] */
+ xassert(p == pp_inv[s] && q == qq_ind[s]);
+ for (k = s; k < t; k++)
+ { pp_ind[pp_inv[k] = pp_inv[k+1]] = k;
+ qq_inv[qq_ind[k] = qq_ind[k+1]] = k;
+ }
+ /* now v[p,q] = u[t,t] */
+ pp_ind[pp_inv[t] = p] = qq_inv[qq_ind[t] = q] = t;
+ /*--------------------------------------------------------------*/
+ /* check if matrix U is already upper triangular */
+ /*--------------------------------------------------------------*/
+ /* check if there is a spike in t-th row of matrix U, which is
+ * p-th row of matrix V */
+ for (p_end = (p_ptr = vr_ptr[p]) + vr_len[p];
+ p_ptr < p_end; p_ptr++)
+ { if (qq_inv[sv_ind[p_ptr]] < t)
+ break; /* spike detected */
+ }
+ if (p_ptr == p_end)
+ { /* no spike; matrix U is already upper triangular */
+ /* store its diagonal element u[t,t] = v[p,q] */
+ vr_piv[p] = vpq;
+#if 1 /* FIXME */
+ if (-vpq_tol < vpq && vpq < +vpq_tol)
+#endif
+ { /* matrix U is not well conditioned, because its diagonal
+ * element u[t,t] = v[p,q] is too small in magnitude */
+ return 2;
+ }
+ else
+ { /* normal case */
+ return 0;
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* copy p-th row of matrix V, which is t-th row of matrix U, to */
+ /* working array */
+ /*--------------------------------------------------------------*/
+ /* copy p-th row of matrix V, including element v[p,q] = u[t,t],
+ * to the working array in dense format and remove these elements
+ * from matrix V; since no pivoting is used, only this row will
+ * change during elimination */
+ for (j = 1; j <= n; j++)
+ work[j] = 0.0;
+ work[q] = vpq;
+ for (p_end = (p_ptr = vr_ptr[p]) + vr_len[p];
+ p_ptr < p_end; p_ptr++)
+ { /* get column index of v[p,j] and store this element to the
+ * working array */
+ work[j = sv_ind[p_ptr]] = sv_val[p_ptr];
+ /* find and remove v[p,j] from j-th column */
+ for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ sv_ind[j_ptr] != p; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ sv_ind[j_ptr] = sv_ind[j_end-1];
+ sv_val[j_ptr] = sv_val[j_end-1];
+ vc_len[j]--;
+ }
+ /* now p-th row of matrix V is temporarily empty */
+ vr_len[p] = 0;
+ /*--------------------------------------------------------------*/
+ /* perform gaussian elimination */
+ /*--------------------------------------------------------------*/
+ /* transform p-th row of matrix V stored in working array, which
+ * is t-th row of matrix U, to eliminate subdiagonal elements
+ * u[t,s], ..., u[t,t-1]; corresponding gaussian multipliers will
+ * form non-trivial row of new row-like factor */
+ nnz = 0; /* number of non-zero gaussian multipliers */
+ for (k = s; k < t; k++)
+ { /* diagonal element u[k,k] = v[i,j] is used as pivot */
+ i = pp_inv[k], j = qq_ind[k];
+ /* take subdiagonal element u[t,k] = v[p,j] */
+ temp = work[j];
+#if 1 /* FIXME */
+ if (-eps_tol < temp && temp < +eps_tol)
+ continue;
+#endif
+ /* compute and save gaussian multiplier:
+ * f := u[t,k] / u[k,k] = v[p,j] / v[i,j] */
+ ind[++nnz] = i;
+ val[nnz] = f = work[j] / vr_piv[i];
+ /* gaussian transformation to eliminate u[t,k] = v[p,j]:
+ * (p-th row of V) := (p-th row of V) - f * (i-th row of V) */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ work[sv_ind[i_ptr]] -= f * sv_val[i_ptr];
+ }
+ /* now matrix U is again upper triangular */
+#if 1 /* FIXME */
+ if (-vpq_tol < work[q] && work[q] < +vpq_tol)
+#endif
+ { /* however, its new diagonal element u[t,t] = v[p,q] is too
+ * small in magnitude */
+ return 3;
+ }
+ /*--------------------------------------------------------------*/
+ /* create new row-like factor H[k] and add to eta file H */
+ /*--------------------------------------------------------------*/
+ /* (nnz = 0 means that all subdiagonal elements were too small
+ * in magnitude) */
+ if (nnz > 0)
+ { if (fhv->nfs == fhv->nfs_max)
+ { /* maximal number of row-like factors has been reached */
+ return 4;
+ }
+ k = ++(fhv->nfs);
+ hh_ind[k] = p;
+ /* store non-trivial row of H[k] in right (dynamic) part of
+ * SVA (diagonal unity element is not stored) */
+ if (sva->r_ptr - sva->m_ptr < nnz)
+ { sva_more_space(sva, nnz);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, fhv->hh_ref-1+k, nnz);
+ ptr = hh_ptr[k];
+ memcpy(&sv_ind[ptr], &ind[1], nnz * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], nnz * sizeof(double));
+ hh_len[k] = nnz;
+ }
+ /*--------------------------------------------------------------*/
+ /* copy transformed p-th row of matrix V, which is t-th row of */
+ /* matrix U, from working array back to matrix V */
+ /*--------------------------------------------------------------*/
+ /* copy elements of transformed p-th row of matrix V, which are
+ * non-diagonal elements u[t,t+1], ..., u[t,n] of matrix U, from
+ * working array to corresponding columns of matrix V (note that
+ * diagonal element u[t,t] = v[p,q] not copied); also transform
+ * p-th row of matrix V to sparse format */
+ len = 0;
+ for (k = t+1; k <= n; k++)
+ { /* j-th column of V = k-th column of U */
+ j = qq_ind[k];
+ /* take non-diagonal element v[p,j] = u[t,k] */
+ temp = work[j];
+#if 1 /* FIXME */
+ if (-eps_tol < temp && temp < +eps_tol)
+ continue;
+#endif
+ /* add v[p,j] to j-th column of matrix V */
+ if (vc_cap[j] == vc_len[j])
+ { /* reserve extra locations in j-th column to reduce further
+ * relocations of that column */
+#if 1 /* FIXME */
+ int need = vc_len[j] + 5;
+#endif
+ if (sva->r_ptr - sva->m_ptr < need)
+ { sva_more_space(sva, need);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vc_ref-1+j, need, 0);
+ }
+ sv_ind[ptr = vc_ptr[j] + (vc_len[j]++)] = p;
+ sv_val[ptr] = temp;
+ /* store element v[p,j] = u[t,k] to working sparse vector */
+ ind[++len] = j;
+ val[len] = temp;
+ }
+ /* copy elements from working sparse vector to p-th row of matrix
+ * V (this row is currently empty) */
+ if (vr_cap[p] < len)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vr_ref-1+p, len, 0);
+ }
+ ptr = vr_ptr[p];
+ memcpy(&sv_ind[ptr], &ind[1], len * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], len * sizeof(double));
+ vr_len[p] = len;
+ /* store new diagonal element u[t,t] = v[p,q] */
+ vr_piv[p] = work[q];
+ /*--------------------------------------------------------------*/
+ /* perform accuracy test (only if new H[k] was added) */
+ /*--------------------------------------------------------------*/
+ if (nnz > 0)
+ { /* copy p-th (non-trivial) row of row-like factor H[k] (except
+ * unity diagonal element) to working array in dense format */
+ for (j = 1; j <= n; j++)
+ work[j] = 0.0;
+ k = fhv->nfs;
+ for (end = (ptr = hh_ptr[k]) + hh_len[k]; ptr < end; ptr++)
+ work[sv_ind[ptr]] = sv_val[ptr];
+ /* compute inner product of p-th (non-trivial) row of matrix
+ * H[k] and q-th column of matrix V */
+ temp = vr_piv[p]; /* 1 * v[p,q] */
+ ptr = vc_ptr[q];
+ end = ptr + vc_len[q];
+ for (; ptr < end; ptr++)
+ temp += work[sv_ind[ptr]] * sv_val[ptr];
+ /* inner product should be equal to element v[p,q] *before*
+ * matrix V was transformed */
+ /* compute relative error */
+ temp = fabs(vpq - temp) / (1.0 + fabs(vpq));
+#if 1 /* FIXME */
+ if (temp > err_tol)
+#endif
+ { /* relative error is too large */
+ return 5;
+ }
+ }
+ /* factorization has been successfully updated */
+ return 0;
+}
+
+/***********************************************************************
+* fhv_h_solve - solve system H * x = b
+*
+* This routine solves the system H * x = b, where the matrix H is the
+* middle factor of the sparse updatable FHV-factorization.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix H. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void fhv_h_solve(FHV *fhv, double x[/*1+n*/])
+{ SVA *sva = fhv->luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int nfs = fhv->nfs;
+ int *hh_ind = fhv->hh_ind;
+ int hh_ref = fhv->hh_ref;
+ int *hh_ptr = &sva->ptr[hh_ref-1];
+ int *hh_len = &sva->len[hh_ref-1];
+ int i, k, end, ptr;
+ double x_i;
+ for (k = 1; k <= nfs; k++)
+ { x_i = x[i = hh_ind[k]];
+ for (end = (ptr = hh_ptr[k]) + hh_len[k]; ptr < end; ptr++)
+ x_i -= sv_val[ptr] * x[sv_ind[ptr]];
+ x[i] = x_i;
+ }
+ return;
+}
+
+/***********************************************************************
+* fhv_ht_solve - solve system H' * x = b
+*
+* This routine solves the system H' * x = b, where H' is a matrix
+* transposed to the matrix H, which is the middle factor of the sparse
+* updatable FHV-factorization.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix H. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void fhv_ht_solve(FHV *fhv, double x[/*1+n*/])
+{ SVA *sva = fhv->luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int nfs = fhv->nfs;
+ int *hh_ind = fhv->hh_ind;
+ int hh_ref = fhv->hh_ref;
+ int *hh_ptr = &sva->ptr[hh_ref-1];
+ int *hh_len = &sva->len[hh_ref-1];
+ int k, end, ptr;
+ double x_j;
+ for (k = nfs; k >= 1; k--)
+ { if ((x_j = x[hh_ind[k]]) == 0.0)
+ continue;
+ for (end = (ptr = hh_ptr[k]) + hh_len[k]; ptr < end; ptr++)
+ x[sv_ind[ptr]] -= sv_val[ptr] * x_j;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/fhv.h b/test/monniaux/glpk-4.65/src/bflib/fhv.h
new file mode 100644
index 00000000..df39ca5c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/fhv.h
@@ -0,0 +1,114 @@
+/* fhv.h (sparse updatable FHV-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef FHV_H
+#define FHV_H
+
+#include "luf.h"
+
+/***********************************************************************
+* The structure FHV describes sparse updatable FHV-factorization.
+*
+* The FHV-factorization has the following format:
+*
+* A = F * H * V, (1)
+*
+* F = P0 * L * P0', (2)
+*
+* H = H[1] * H[2] * ... * H[nfs], (3)
+*
+* V = P * U * Q, (4)
+*
+* where: A is a given (unsymmetric) square matrix; F, H, V are matrix
+* factors actually computed; L is a lower triangular matrix with unity
+* diagonal; U is an upper tringular matrix; H[k], k = 1, 2, ..., nfs,
+* is a row-like factor, which differs from unity matrix only in one
+* row called a non-trivial row; P0, P, Q are permutation matrices; and
+* P0' is a matrix transposed to P0.
+*
+* Matrices F, V, P, Q are stored in the underlying LUF object.
+*
+* Non-trivial rows of factors H[k] are stored as sparse vectors in the
+* right (static) part of the sparse vector area (SVA). Note that unity
+* diagonal elements of non-trivial rows are not stored.
+*
+* Matrix P0 is stored in the same way as matrix P.
+*
+* Matrices L and U are completely defined by matrices F, V, P, and Q,
+* and therefore not stored explicitly. */
+
+typedef struct FHV FHV;
+
+struct FHV
+{ /* FHV-factorization */
+ LUF *luf;
+ /* LU-factorization (contains matrices F, V, P, Q) */
+ /*--------------------------------------------------------------*/
+ /* matrix H in the form of eta file */
+ int nfs_max;
+ /* maximal number of row-like factors (this limits the number of
+ * updates of the factorization) */
+ int nfs;
+ /* current number of row-like factors, 0 <= nfs <= nfs_max */
+ int *hh_ind; /* int hh_ind[1+nfs_max]; */
+ /* hh_ind[0] is not used;
+ * hh_ind[k], 1 <= k <= nfs, is number of non-trivial row of
+ * factor H[k] */
+ int hh_ref;
+ /* reference number of sparse vector in SVA, which is non-trivial
+ * row of factor H[1] */
+#if 0 + 0
+ int *hh_ptr = &sva->ptr[hh_ref-1];
+ /* hh_ptr[0] is not used;
+ * hh_ptr[k], 1 <= k <= nfs, is pointer to non-trivial row of
+ * factor H[k] */
+ int *hh_len = &sva->len[hh_ref-1];
+ /* hh_len[0] is not used;
+ * hh_len[k], 1 <= k <= nfs, is number of non-zero elements in
+ * non-trivial row of factor H[k] */
+#endif
+ /*--------------------------------------------------------------*/
+ /* matrix P0 */
+ int *p0_ind; /* int p0_ind[1+n]; */
+ /* p0_ind[i] = j means that P0[i,j] = 1 */
+ int *p0_inv; /* int p0_inv[1+n]; */
+ /* p0_inv[j] = i means that P0[i,j] = 1 */
+};
+
+#define fhv_ft_update _glp_fhv_ft_update
+int fhv_ft_update(FHV *fhv, int q, int aq_len, const int aq_ind[],
+ const double aq_val[], int ind[/*1+n*/], double val[/*1+n*/],
+ double work[/*1+n*/]);
+/* update FHV-factorization (Forrest-Tomlin) */
+
+#define fhv_h_solve _glp_fhv_h_solve
+void fhv_h_solve(FHV *fhv, double x[/*1+n*/]);
+/* solve system H * x = b */
+
+#define fhv_ht_solve _glp_fhv_ht_solve
+void fhv_ht_solve(FHV *fhv, double x[/*1+n*/]);
+/* solve system H' * x = b */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/fhvint.c b/test/monniaux/glpk-4.65/src/bflib/fhvint.c
new file mode 100644
index 00000000..a21b71c6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/fhvint.c
@@ -0,0 +1,168 @@
+/* fhvint.c (interface to FHV-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "fhvint.h"
+
+FHVINT *fhvint_create(void)
+{ /* create interface to FHV-factorization */
+ FHVINT *fi;
+ fi = talloc(1, FHVINT);
+ memset(fi, 0, sizeof(FHVINT));
+ fi->lufi = lufint_create();
+ return fi;
+}
+
+int fhvint_factorize(FHVINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info)
+{ /* compute FHV-factorization of specified matrix A */
+ int nfs_max, old_n_max, n_max, k, ret;
+ xassert(n > 0);
+ fi->valid = 0;
+ /* get required value of nfs_max */
+ nfs_max = fi->nfs_max;
+ if (nfs_max == 0)
+ nfs_max = 100;
+ xassert(nfs_max > 0);
+ /* compute factorization of specified matrix A */
+ old_n_max = fi->lufi->n_max;
+ fi->lufi->sva_n_max = 4 * n + nfs_max;
+ fi->lufi->sgf_updat = 1;
+ ret = lufint_factorize(fi->lufi, n, col, info);
+ n_max = fi->lufi->n_max;
+ /* allocate/reallocate arrays, if necessary */
+ if (fi->fhv.nfs_max != nfs_max)
+ { if (fi->fhv.hh_ind != NULL)
+ tfree(fi->fhv.hh_ind);
+ fi->fhv.hh_ind = talloc(1+nfs_max, int);
+ }
+ if (old_n_max < n_max)
+ { if (fi->fhv.p0_ind != NULL)
+ tfree(fi->fhv.p0_ind);
+ if (fi->fhv.p0_inv != NULL)
+ tfree(fi->fhv.p0_inv);
+ fi->fhv.p0_ind = talloc(1+n_max, int);
+ fi->fhv.p0_inv = talloc(1+n_max, int);
+ }
+ /* initialize FHV-factorization */
+ fi->fhv.luf = fi->lufi->luf;
+ fi->fhv.nfs_max = nfs_max;
+ /* H := I */
+ fi->fhv.nfs = 0;
+ fi->fhv.hh_ref = sva_alloc_vecs(fi->lufi->sva, nfs_max);
+ /* P0 := P */
+ for (k = 1; k <= n; k++)
+ { fi->fhv.p0_ind[k] = fi->fhv.luf->pp_ind[k];
+ fi->fhv.p0_inv[k] = fi->fhv.luf->pp_inv[k];
+ }
+ /* set validation flag */
+ if (ret == 0)
+ fi->valid = 1;
+ return ret;
+}
+
+int fhvint_update(FHVINT *fi, int j, int len, const int ind[],
+ const double val[])
+{ /* update FHV-factorization after replacing j-th column of A */
+ SGF *sgf = fi->lufi->sgf;
+ int *ind1 = sgf->rs_next;
+ double *val1 = sgf->vr_max;
+ double *work = sgf->work;
+ int ret;
+ xassert(fi->valid);
+ ret = fhv_ft_update(&fi->fhv, j, len, ind, val, ind1, val1, work);
+ if (ret != 0)
+ fi->valid = 0;
+ return ret;
+}
+
+void fhvint_ftran(FHVINT *fi, double x[])
+{ /* solve system A * x = b */
+ FHV *fhv = &fi->fhv;
+ LUF *luf = fhv->luf;
+ int n = luf->n;
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ SGF *sgf = fi->lufi->sgf;
+ double *work = sgf->work;
+ xassert(fi->valid);
+ /* A = F * H * V */
+ /* x = inv(A) * b = inv(V) * inv(H) * inv(F) * b */
+ luf->pp_ind = fhv->p0_ind;
+ luf->pp_inv = fhv->p0_inv;
+ luf_f_solve(luf, x);
+ luf->pp_ind = pp_ind;
+ luf->pp_inv = pp_inv;
+ fhv_h_solve(fhv, x);
+ luf_v_solve(luf, x, work);
+ memcpy(&x[1], &work[1], n * sizeof(double));
+ return;
+}
+
+void fhvint_btran(FHVINT *fi, double x[])
+{ /* solve system A'* x = b */
+ FHV *fhv = &fi->fhv;
+ LUF *luf = fhv->luf;
+ int n = luf->n;
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ SGF *sgf = fi->lufi->sgf;
+ double *work = sgf->work;
+ xassert(fi->valid);
+ /* A' = (F * H * V)' = V'* H'* F' */
+ /* x = inv(A') * b = inv(F') * inv(H') * inv(V') * b */
+ luf_vt_solve(luf, x, work);
+ fhv_ht_solve(fhv, work);
+ luf->pp_ind = fhv->p0_ind;
+ luf->pp_inv = fhv->p0_inv;
+ luf_ft_solve(luf, work);
+ luf->pp_ind = pp_ind;
+ luf->pp_inv = pp_inv;
+ memcpy(&x[1], &work[1], n * sizeof(double));
+ return;
+}
+
+double fhvint_estimate(FHVINT *fi)
+{ /* estimate 1-norm of inv(A) */
+ double norm;
+ xassert(fi->valid);
+ xassert(fi->fhv.nfs == 0);
+ norm = luf_estimate_norm(fi->fhv.luf, fi->lufi->sgf->vr_max,
+ fi->lufi->sgf->work);
+ return norm;
+}
+
+void fhvint_delete(FHVINT *fi)
+{ /* delete interface to FHV-factorization */
+ lufint_delete(fi->lufi);
+ if (fi->fhv.hh_ind != NULL)
+ tfree(fi->fhv.hh_ind);
+ if (fi->fhv.p0_ind != NULL)
+ tfree(fi->fhv.p0_ind);
+ if (fi->fhv.p0_inv != NULL)
+ tfree(fi->fhv.p0_inv);
+ tfree(fi);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/fhvint.h b/test/monniaux/glpk-4.65/src/bflib/fhvint.h
new file mode 100644
index 00000000..000829c6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/fhvint.h
@@ -0,0 +1,78 @@
+/* fhvint.h (interface to FHV-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef FHVINT_H
+#define FHVINT_H
+
+#include "fhv.h"
+#include "lufint.h"
+
+typedef struct FHVINT FHVINT;
+
+struct FHVINT
+{ /* interface to FHV-factorization */
+ int valid;
+ /* factorization is valid only if this flag is set */
+ FHV fhv;
+ /* FHV-factorization */
+ LUFINT *lufi;
+ /* interface to underlying LU-factorization */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ int nfs_max;
+ /* required maximal number of row-like factors */
+};
+
+#define fhvint_create _glp_fhvint_create
+FHVINT *fhvint_create(void);
+/* create interface to FHV-factorization */
+
+#define fhvint_factorize _glp_fhvint_factorize
+int fhvint_factorize(FHVINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info);
+/* compute FHV-factorization of specified matrix A */
+
+#define fhvint_update _glp_fhvint_update
+int fhvint_update(FHVINT *fi, int j, int len, const int ind[],
+ const double val[]);
+/* update FHV-factorization after replacing j-th column of A */
+
+#define fhvint_ftran _glp_fhvint_ftran
+void fhvint_ftran(FHVINT *fi, double x[]);
+/* solve system A * x = b */
+
+#define fhvint_btran _glp_fhvint_btran
+void fhvint_btran(FHVINT *fi, double x[]);
+/* solve system A'* x = b */
+
+#define fhvint_estimate _glp_fhvint_estimate
+double fhvint_estimate(FHVINT *fi);
+/* estimate 1-norm of inv(A) */
+
+#define fhvint_delete _glp_fhvint_delete
+void fhvint_delete(FHVINT *fi);
+/* delete interface to FHV-factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/ifu.c b/test/monniaux/glpk-4.65/src/bflib/ifu.c
new file mode 100644
index 00000000..aa47fb09
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/ifu.c
@@ -0,0 +1,392 @@
+/* ifu.c (dense updatable IFU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ifu.h"
+
+/***********************************************************************
+* ifu_expand - expand IFU-factorization
+*
+* This routine expands the IFU-factorization of the matrix A according
+* to the following expansion of A:
+*
+* ( A c )
+* new A = ( )
+* ( r' d )
+*
+* where c[1,...,n] is a new column, r[1,...,n] is a new row, and d is
+* a new diagonal element.
+*
+* From the main equality F * A = U it follows that:
+*
+* ( F 0 ) ( A c ) ( FA Fc ) ( U Fc )
+* ( ) ( ) = ( ) = ( ),
+* ( 0 1 ) ( r' d ) ( r' d ) ( r' d )
+*
+* thus,
+*
+* ( F 0 ) ( U Fc )
+* new F = ( ), new U = ( ).
+* ( 0 1 ) ( r' d )
+*
+* Note that the resulting matrix U loses its upper triangular form due
+* to row spike r', which should be eliminated. */
+
+void ifu_expand(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d)
+{ /* non-optimized version */
+ int n_max = ifu->n_max;
+ int n = ifu->n;
+ double *f_ = ifu->f;
+ double *u_ = ifu->u;
+ int i, j;
+ double t;
+# define f(i,j) f_[(i)*n_max+(j)]
+# define u(i,j) u_[(i)*n_max+(j)]
+ xassert(0 <= n && n < n_max);
+ /* adjust indexing */
+ c++, r++;
+ /* set new zero column of matrix F */
+ for (i = 0; i < n; i++)
+ f(i,n) = 0.0;
+ /* set new zero row of matrix F */
+ for (j = 0; j < n; j++)
+ f(n,j) = 0.0;
+ /* set new unity diagonal element of matrix F */
+ f(n,n) = 1.0;
+ /* set new column of matrix U to vector (old F) * c */
+ for (i = 0; i < n; i++)
+ { /* u[i,n] := (i-th row of old F) * c */
+ t = 0.0;
+ for (j = 0; j < n; j++)
+ t += f(i,j) * c[j];
+ u(i,n) = t;
+ }
+ /* set new row of matrix U to vector r */
+ for (j = 0; j < n; j++)
+ u(n,j) = r[j];
+ /* set new diagonal element of matrix U to scalar d */
+ u(n,n) = d;
+ /* increase factorization order */
+ ifu->n++;
+# undef f
+# undef u
+ return;
+}
+
+/***********************************************************************
+* ifu_bg_update - update IFU-factorization (Bartels-Golub)
+*
+* This routine updates IFU-factorization of the matrix A according to
+* its expansion (see comments to the routine ifu_expand). The routine
+* is based on the method proposed by Bartels and Golub [1].
+*
+* RETURNS
+*
+* 0 The factorization has been successfully updated.
+*
+* 1 On some elimination step diagional element u[k,k] to be used as
+* pivot is too small in magnitude.
+*
+* 2 Diagonal element u[n,n] is too small in magnitude (at the end of
+* update).
+*
+* REFERENCES
+*
+* 1. R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming
+* Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */
+
+int ifu_bg_update(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d)
+{ /* non-optimized version */
+ int n_max = ifu->n_max;
+ int n = ifu->n;
+ double *f_ = ifu->f;
+ double *u_ = ifu->u;
+#if 1 /* FIXME */
+ double tol = 1e-5;
+#endif
+ int j, k;
+ double t;
+# define f(i,j) f_[(i)*n_max+(j)]
+# define u(i,j) u_[(i)*n_max+(j)]
+ /* expand factorization */
+ ifu_expand(ifu, c, r, d);
+ /* NOTE: n keeps its old value */
+ /* eliminate spike (non-zero subdiagonal elements) in last row of
+ * matrix U */
+ for (k = 0; k < n; k++)
+ { /* if |u[k,k]| < |u[n,k]|, interchange k-th and n-th rows to
+ * provide |u[k,k]| >= |u[n,k]| for numeric stability */
+ if (fabs(u(k,k)) < fabs(u(n,k)))
+ { /* interchange k-th and n-th rows of matrix U */
+ for (j = k; j <= n; j++)
+ t = u(k,j), u(k,j) = u(n,j), u(n,j) = t;
+ /* interchange k-th and n-th rows of matrix F to keep the
+ * main equality F * A = U */
+ for (j = 0; j <= n; j++)
+ t = f(k,j), f(k,j) = f(n,j), f(n,j) = t;
+ }
+ /* now |u[k,k]| >= |u[n,k]| */
+ /* check if diagonal element u[k,k] can be used as pivot */
+ if (fabs(u(k,k)) < tol)
+ { /* u[k,k] is too small in magnitude */
+ return 1;
+ }
+ /* if u[n,k] = 0, elimination is not needed */
+ if (u(n,k) == 0.0)
+ continue;
+ /* compute gaussian multiplier t = u[n,k] / u[k,k] */
+ t = u(n,k) / u(k,k);
+ /* apply gaussian transformation to eliminate u[n,k] */
+ /* (n-th row of U) := (n-th row of U) - t * (k-th row of U) */
+ for (j = k+1; j <= n; j++)
+ u(n,j) -= t * u(k,j);
+ /* apply the same transformation to matrix F to keep the main
+ * equality F * A = U */
+ for (j = 0; j <= n; j++)
+ f(n,j) -= t * f(k,j);
+ }
+ /* now matrix U is upper triangular */
+ if (fabs(u(n,n)) < tol)
+ { /* u[n,n] is too small in magnitude */
+ return 2;
+ }
+# undef f
+# undef u
+ return 0;
+}
+
+/***********************************************************************
+* The routine givens computes the parameters of Givens plane rotation
+* c = cos(teta) and s = sin(teta) such that:
+*
+* ( c -s ) ( a ) ( r )
+* ( ) ( ) = ( ) ,
+* ( s c ) ( b ) ( 0 )
+*
+* where a and b are given scalars.
+*
+* REFERENCES
+*
+* G.H.Golub, C.F.Van Loan, "Matrix Computations", 2nd ed. */
+
+static void givens(double a, double b, double *c, double *s)
+{ /* non-optimized version */
+ double t;
+ if (b == 0.0)
+ (*c) = 1.0, (*s) = 0.0;
+ else if (fabs(a) <= fabs(b))
+ t = - a / b, (*s) = 1.0 / sqrt(1.0 + t * t), (*c) = (*s) * t;
+ else
+ t = - b / a, (*c) = 1.0 / sqrt(1.0 + t * t), (*s) = (*c) * t;
+ return;
+}
+
+/***********************************************************************
+* ifu_gr_update - update IFU-factorization (Givens rotations)
+*
+* This routine updates IFU-factorization of the matrix A according to
+* its expansion (see comments to the routine ifu_expand). The routine
+* is based on Givens plane rotations [1].
+*
+* RETURNS
+*
+* 0 The factorization has been successfully updated.
+*
+* 1 On some elimination step both elements u[k,k] and u[n,k] are too
+* small in magnitude.
+*
+* 2 Diagonal element u[n,n] is too small in magnitude (at the end of
+* update).
+*
+* REFERENCES
+*
+* 1. G.H.Golub, C.F.Van Loan, "Matrix Computations", 2nd ed. */
+
+int ifu_gr_update(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d)
+{ /* non-optimized version */
+ int n_max = ifu->n_max;
+ int n = ifu->n;
+ double *f_ = ifu->f;
+ double *u_ = ifu->u;
+#if 1 /* FIXME */
+ double tol = 1e-5;
+#endif
+ int j, k;
+ double cs, sn;
+# define f(i,j) f_[(i)*n_max+(j)]
+# define u(i,j) u_[(i)*n_max+(j)]
+ /* expand factorization */
+ ifu_expand(ifu, c, r, d);
+ /* NOTE: n keeps its old value */
+ /* eliminate spike (non-zero subdiagonal elements) in last row of
+ * matrix U */
+ for (k = 0; k < n; k++)
+ { /* check if elements u[k,k] and u[n,k] are eligible */
+ if (fabs(u(k,k)) < tol && fabs(u(n,k)) < tol)
+ { /* both u[k,k] and u[n,k] are too small in magnitude */
+ return 1;
+ }
+ /* if u[n,k] = 0, elimination is not needed */
+ if (u(n,k) == 0.0)
+ continue;
+ /* compute parameters of Givens plane rotation */
+ givens(u(k,k), u(n,k), &cs, &sn);
+ /* apply Givens rotation to k-th and n-th rows of matrix U to
+ * eliminate u[n,k] */
+ for (j = k; j <= n; j++)
+ { double ukj = u(k,j), unj = u(n,j);
+ u(k,j) = cs * ukj - sn * unj;
+ u(n,j) = sn * ukj + cs * unj;
+ }
+ /* apply the same transformation to matrix F to keep the main
+ * equality F * A = U */
+ for (j = 0; j <= n; j++)
+ { double fkj = f(k,j), fnj = f(n,j);
+ f(k,j) = cs * fkj - sn * fnj;
+ f(n,j) = sn * fkj + cs * fnj;
+ }
+ }
+ /* now matrix U is upper triangular */
+ if (fabs(u(n,n)) < tol)
+ { /* u[n,n] is too small in magnitude */
+ return 2;
+ }
+# undef f
+# undef u
+ return 0;
+}
+
+/***********************************************************************
+* ifu_a_solve - solve system A * x = b
+*
+* This routine solves the system A * x = b, where the matrix A is
+* specified by its IFU-factorization.
+*
+* Using the main equality F * A = U we have:
+*
+* A * x = b => F * A * x = F * b => U * x = F * b =>
+*
+* x = inv(U) * F * b.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix A. On exit this array will contain elements of the solution
+* vector x in the same locations.
+*
+* The working array w should have at least 1+n elements (0-th element
+* is not used). */
+
+void ifu_a_solve(IFU *ifu, double x[/*1+n*/], double w[/*1+n*/])
+{ /* non-optimized version */
+ int n_max = ifu->n_max;
+ int n = ifu->n;
+ double *f_ = ifu->f;
+ double *u_ = ifu->u;
+ int i, j;
+ double t;
+# define f(i,j) f_[(i)*n_max+(j)]
+# define u(i,j) u_[(i)*n_max+(j)]
+ xassert(0 <= n && n <= n_max);
+ /* adjust indexing */
+ x++, w++;
+ /* y := F * b */
+ memcpy(w, x, n * sizeof(double));
+ for (i = 0; i < n; i++)
+ { /* y[i] := (i-th row of F) * b */
+ t = 0.0;
+ for (j = 0; j < n; j++)
+ t += f(i,j) * w[j];
+ x[i] = t;
+ }
+ /* x := inv(U) * y */
+ for (i = n-1; i >= 0; i--)
+ { t = x[i];
+ for (j = i+1; j < n; j++)
+ t -= u(i,j) * x[j];
+ x[i] = t / u(i,i);
+ }
+# undef f
+# undef u
+ return;
+}
+
+/***********************************************************************
+* ifu_at_solve - solve system A'* x = b
+*
+* This routine solves the system A'* x = b, where A' is a matrix
+* transposed to the matrix A, specified by its IFU-factorization.
+*
+* Using the main equality F * A = U, from which it follows that
+* A'* F' = U', we have:
+*
+* A'* x = b => A'* F'* inv(F') * x = b =>
+*
+* U'* inv(F') * x = b => inv(F') * x = inv(U') * b =>
+*
+* x = F' * inv(U') * b.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix A. On exit this array will contain elements of the solution
+* vector x in the same locations.
+*
+* The working array w should have at least 1+n elements (0-th element
+* is not used). */
+
+void ifu_at_solve(IFU *ifu, double x[/*1+n*/], double w[/*1+n*/])
+{ /* non-optimized version */
+ int n_max = ifu->n_max;
+ int n = ifu->n;
+ double *f_ = ifu->f;
+ double *u_ = ifu->u;
+ int i, j;
+ double t;
+# define f(i,j) f_[(i)*n_max+(j)]
+# define u(i,j) u_[(i)*n_max+(j)]
+ xassert(0 <= n && n <= n_max);
+ /* adjust indexing */
+ x++, w++;
+ /* y := inv(U') * b */
+ for (i = 0; i < n; i++)
+ { t = (x[i] /= u(i,i));
+ for (j = i+1; j < n; j++)
+ x[j] -= u(i,j) * t;
+ }
+ /* x := F'* y */
+ for (j = 0; j < n; j++)
+ { /* x[j] := (j-th column of F) * y */
+ t = 0.0;
+ for (i = 0; i < n; i++)
+ t += f(i,j) * x[i];
+ w[j] = t;
+ }
+ memcpy(x, w, n * sizeof(double));
+# undef f
+# undef u
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/ifu.h b/test/monniaux/glpk-4.65/src/bflib/ifu.h
new file mode 100644
index 00000000..1c67a801
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/ifu.h
@@ -0,0 +1,99 @@
+/* ifu.h (dense updatable IFU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef IFU_H
+#define IFU_H
+
+/***********************************************************************
+* The structure IFU describes dense updatable IFU-factorization.
+*
+* The IFU-factorization has the following format:
+*
+* A = inv(F) * U, (1)
+*
+* where A is a given (unsymmetric) nxn square matrix, F is a square
+* matrix, U is an upper triangular matrix. Obviously, the equality (1)
+* is equivalent to the following equality:
+*
+* F * A = U. (2)
+*
+* It is assumed that matrix A is small and dense, so matrices F and U
+* are stored by rows in dense format as follows:
+*
+* 1 n n_max 1 n n_max
+* 1 * * * * * * x x x x 1 * * * * * * x x x x
+* * * * * * * x x x x ? * * * * * x x x x
+* * * * * * * x x x x ? ? * * * * x x x x
+* * * * * * * x x x x ? ? ? * * * x x x x
+* * * * * * * x x x x ? ? ? ? * * x x x x
+* n * * * * * * x x x x n ? ? ? ? ? * x x x x
+* x x x x x x x x x x x x x x x x x x x x
+* x x x x x x x x x x x x x x x x x x x x
+* x x x x x x x x x x x x x x x x x x x x
+* n_max x x x x x x x x x x n_max x x x x x x x x x x
+*
+* matrix F matrix U
+*
+* where '*' are matrix elements, '?' are unused locations, 'x' are
+* reserved locations. */
+
+typedef struct IFU IFU;
+
+struct IFU
+{ /* IFU-factorization */
+ int n_max;
+ /* maximal order of matrices A, F, U; n_max >= 1 */
+ int n;
+ /* current order of matrices A, F, U; 0 <= n <= n_max */
+ double *f; /* double f[n_max*n_max]; */
+ /* matrix F stored by rows */
+ double *u; /* double u[n_max*n_max]; */
+ /* matrix U stored by rows */
+};
+
+#define ifu_expand _glp_ifu_expand
+void ifu_expand(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d);
+/* expand IFU-factorization */
+
+#define ifu_bg_update _glp_ifu_bg_update
+int ifu_bg_update(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d);
+/* update IFU-factorization (Bartels-Golub) */
+
+#define ifu_gr_update _glp_ifu_gr_update
+int ifu_gr_update(IFU *ifu, double c[/*1+n*/], double r[/*1+n*/],
+ double d);
+/* update IFU-factorization (Givens rotations) */
+
+#define ifu_a_solve _glp_ifu_a_solve
+void ifu_a_solve(IFU *ifu, double x[/*1+n*/], double w[/*1+n*/]);
+/* solve system A * x = b */
+
+#define ifu_at_solve _glp_ifu_at_solve
+void ifu_at_solve(IFU *ifu, double x[/*1+n*/], double w[/*1+n*/]);
+/* solve system A'* x = b */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/luf.c b/test/monniaux/glpk-4.65/src/bflib/luf.c
new file mode 100644
index 00000000..2797407d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/luf.c
@@ -0,0 +1,713 @@
+/* luf.c (sparse LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "luf.h"
+
+/***********************************************************************
+* luf_store_v_cols - store matrix V = A in column-wise format
+*
+* This routine stores matrix V = A in column-wise format, where A is
+* the original matrix to be factorized.
+*
+* On exit the routine returns the number of non-zeros in matrix V. */
+
+int luf_store_v_cols(LUF *luf, int (*col)(void *info, int j, int ind[],
+ double val[]), void *info, int ind[], double val[])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *vc_cap = &sva->cap[vc_ref-1];
+ int j, len, ptr, nnz;
+ nnz = 0;
+ for (j = 1; j <= n; j++)
+ { /* get j-th column */
+ len = col(info, j, ind, val);
+ xassert(0 <= len && len <= n);
+ /* enlarge j-th column capacity */
+ if (vc_cap[j] < len)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vc_ref-1+j, len, 0);
+ }
+ /* store j-th column */
+ ptr = vc_ptr[j];
+ memcpy(&sv_ind[ptr], &ind[1], len * sizeof(int));
+ memcpy(&sv_val[ptr], &val[1], len * sizeof(double));
+ vc_len[j] = len;
+ nnz += len;
+ }
+ return nnz;
+}
+
+/***********************************************************************
+* luf_check_all - check LU-factorization before k-th elimination step
+*
+* This routine checks that before performing k-th elimination step,
+* 1 <= k <= n+1, all components of the LU-factorization are correct.
+*
+* In case of k = n+1, i.e. after last elimination step, it is assumed
+* that rows of F and columns of V are *not* built yet.
+*
+* NOTE: For testing/debugging only. */
+
+void luf_check_all(LUF *luf, int k)
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fr_ref = luf->fr_ref;
+ int *fr_len = &sva->len[fr_ref-1];
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int i, ii, i_ptr, i_end, j, jj, j_ptr, j_end;
+ xassert(n > 0);
+ xassert(1 <= k && k <= n+1);
+ /* check permutation matrix P */
+ for (i = 1; i <= n; i++)
+ { ii = pp_ind[i];
+ xassert(1 <= ii && ii <= n);
+ xassert(pp_inv[ii] == i);
+ }
+ /* check permutation matrix Q */
+ for (j = 1; j <= n; j++)
+ { jj = qq_inv[j];
+ xassert(1 <= jj && jj <= n);
+ xassert(qq_ind[jj] == j);
+ }
+ /* check row-wise representation of matrix F */
+ for (i = 1; i <= n; i++)
+ xassert(fr_len[i] == 0);
+ /* check column-wise representation of matrix F */
+ for (j = 1; j <= n; j++)
+ { /* j-th column of F = jj-th column of L */
+ jj = pp_ind[j];
+ if (jj < k)
+ { j_ptr = fc_ptr[j];
+ j_end = j_ptr + fc_len[j];
+ for (; j_ptr < j_end; j_ptr++)
+ { i = sv_ind[j_ptr];
+ xassert(1 <= i && i <= n);
+ ii = pp_ind[i]; /* f[i,j] = l[ii,jj] */
+ xassert(ii > jj);
+ xassert(sv_val[j_ptr] != 0.0);
+ }
+ }
+ else /* jj >= k */
+ xassert(fc_len[j] == 0);
+ }
+ /* check row-wise representation of matrix V */
+ for (i = 1; i <= n; i++)
+ { /* i-th row of V = ii-th row of U */
+ ii = pp_ind[i];
+ i_ptr = vr_ptr[i];
+ i_end = i_ptr + vr_len[i];
+ for (; i_ptr < i_end; i_ptr++)
+ { j = sv_ind[i_ptr];
+ xassert(1 <= j && j <= n);
+ jj = qq_inv[j]; /* v[i,j] = u[ii,jj] */
+ if (ii < k)
+ xassert(jj > ii);
+ else /* ii >= k */
+ { xassert(jj >= k);
+ /* find v[i,j] in j-th column */
+ j_ptr = vc_ptr[j];
+ j_end = j_ptr + vc_len[j];
+ for (; sv_ind[j_ptr] != i; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ }
+ xassert(sv_val[i_ptr] != 0.0);
+ }
+ }
+ /* check column-wise representation of matrix V */
+ for (j = 1; j <= n; j++)
+ { /* j-th column of V = jj-th column of U */
+ jj = qq_inv[j];
+ if (jj < k)
+ xassert(vc_len[j] == 0);
+ else /* jj >= k */
+ { j_ptr = vc_ptr[j];
+ j_end = j_ptr + vc_len[j];
+ for (; j_ptr < j_end; j_ptr++)
+ { i = sv_ind[j_ptr];
+ ii = pp_ind[i]; /* v[i,j] = u[ii,jj] */
+ xassert(ii >= k);
+ /* find v[i,j] in i-th row */
+ i_ptr = vr_ptr[i];
+ i_end = i_ptr + vr_len[i];
+ for (; sv_ind[i_ptr] != j; i_ptr++)
+ /* nop */;
+ xassert(i_ptr < i_end);
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_build_v_rows - build matrix V in row-wise format
+*
+* This routine builds the row-wise representation of matrix V in the
+* left part of SVA using its column-wise representation.
+*
+* NOTE: On entry to the routine all rows of matrix V should have zero
+* capacity.
+*
+* The working array len should have at least 1+n elements (len[0] is
+* not used). */
+
+void luf_build_v_rows(LUF *luf, int len[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int i, j, end, nnz, ptr, ptr1;
+ /* calculate the number of non-zeros in each row of matrix V and
+ * the total number of non-zeros */
+ nnz = 0;
+ for (i = 1; i <= n; i++)
+ len[i] = 0;
+ for (j = 1; j <= n; j++)
+ { nnz += vc_len[j];
+ for (end = (ptr = vc_ptr[j]) + vc_len[j]; ptr < end; ptr++)
+ len[sv_ind[ptr]]++;
+ }
+ /* we need at least nnz free locations in SVA */
+ if (sva->r_ptr - sva->m_ptr < nnz)
+ { sva_more_space(sva, nnz);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ /* reserve locations for rows of matrix V */
+ for (i = 1; i <= n; i++)
+ { if (len[i] > 0)
+ sva_enlarge_cap(sva, vr_ref-1+i, len[i], 0);
+ vr_len[i] = len[i];
+ }
+ /* walk thru column of matrix V and build its rows */
+ for (j = 1; j <= n; j++)
+ { for (end = (ptr = vc_ptr[j]) + vc_len[j]; ptr < end; ptr++)
+ { i = sv_ind[ptr];
+ sv_ind[ptr1 = vr_ptr[i] + (--len[i])] = j;
+ sv_val[ptr1] = sv_val[ptr];
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_build_f_rows - build matrix F in row-wise format
+*
+* This routine builds the row-wise representation of matrix F in the
+* right part of SVA using its column-wise representation.
+*
+* NOTE: On entry to the routine all rows of matrix F should have zero
+* capacity.
+*
+* The working array len should have at least 1+n elements (len[0] is
+* not used). */
+
+void luf_build_f_rows(LUF *luf, int len[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fr_ref = luf->fr_ref;
+ int *fr_ptr = &sva->ptr[fr_ref-1];
+ int *fr_len = &sva->len[fr_ref-1];
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int i, j, end, nnz, ptr, ptr1;
+ /* calculate the number of non-zeros in each row of matrix F and
+ * the total number of non-zeros (except diagonal elements) */
+ nnz = 0;
+ for (i = 1; i <= n; i++)
+ len[i] = 0;
+ for (j = 1; j <= n; j++)
+ { nnz += fc_len[j];
+ for (end = (ptr = fc_ptr[j]) + fc_len[j]; ptr < end; ptr++)
+ len[sv_ind[ptr]]++;
+ }
+ /* we need at least nnz free locations in SVA */
+ if (sva->r_ptr - sva->m_ptr < nnz)
+ { sva_more_space(sva, nnz);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ /* reserve locations for rows of matrix F */
+ for (i = 1; i <= n; i++)
+ { if (len[i] > 0)
+ sva_reserve_cap(sva, fr_ref-1+i, len[i]);
+ fr_len[i] = len[i];
+ }
+ /* walk through columns of matrix F and build its rows */
+ for (j = 1; j <= n; j++)
+ { for (end = (ptr = fc_ptr[j]) + fc_len[j]; ptr < end; ptr++)
+ { i = sv_ind[ptr];
+ sv_ind[ptr1 = fr_ptr[i] + (--len[i])] = j;
+ sv_val[ptr1] = sv_val[ptr];
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_build_v_cols - build matrix V in column-wise format
+*
+* This routine builds the column-wise representation of matrix V in
+* the left (if the flag updat is set) or right (if the flag updat is
+* clear) part of SVA using its row-wise representation.
+*
+* NOTE: On entry to the routine all columns of matrix V should have
+* zero capacity.
+*
+* The working array len should have at least 1+n elements (len[0] is
+* not used). */
+
+void luf_build_v_cols(LUF *luf, int updat, int len[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int i, j, end, nnz, ptr, ptr1;
+ /* calculate the number of non-zeros in each column of matrix V
+ * and the total number of non-zeros (except pivot elements) */
+ nnz = 0;
+ for (j = 1; j <= n; j++)
+ len[j] = 0;
+ for (i = 1; i <= n; i++)
+ { nnz += vr_len[i];
+ for (end = (ptr = vr_ptr[i]) + vr_len[i]; ptr < end; ptr++)
+ len[sv_ind[ptr]]++;
+ }
+ /* we need at least nnz free locations in SVA */
+ if (sva->r_ptr - sva->m_ptr < nnz)
+ { sva_more_space(sva, nnz);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ /* reserve locations for columns of matrix V */
+ for (j = 1; j <= n; j++)
+ { if (len[j] > 0)
+ { if (updat)
+ sva_enlarge_cap(sva, vc_ref-1+j, len[j], 0);
+ else
+ sva_reserve_cap(sva, vc_ref-1+j, len[j]);
+ }
+ vc_len[j] = len[j];
+ }
+ /* walk through rows of matrix V and build its columns */
+ for (i = 1; i <= n; i++)
+ { for (end = (ptr = vr_ptr[i]) + vr_len[i]; ptr < end; ptr++)
+ { j = sv_ind[ptr];
+ sv_ind[ptr1 = vc_ptr[j] + (--len[j])] = i;
+ sv_val[ptr1] = sv_val[ptr];
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_check_f_rc - check rows and columns of matrix F
+*
+* This routine checks that the row- and column-wise representations
+* of matrix F are identical.
+*
+* NOTE: For testing/debugging only. */
+
+void luf_check_f_rc(LUF *luf)
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fr_ref = luf->fr_ref;
+ int *fr_ptr = &sva->ptr[fr_ref-1];
+ int *fr_len = &sva->len[fr_ref-1];
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int i, i_end, i_ptr, j, j_end, j_ptr;
+ /* walk thru rows of matrix F */
+ for (i = 1; i <= n; i++)
+ { for (i_end = (i_ptr = fr_ptr[i]) + fr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { j = sv_ind[i_ptr];
+ /* find element f[i,j] in j-th column of matrix F */
+ for (j_end = (j_ptr = fc_ptr[j]) + fc_len[j];
+ sv_ind[j_ptr] != i; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ xassert(sv_val[i_ptr] == sv_val[j_ptr]);
+ /* mark element f[i,j] */
+ sv_ind[j_ptr] = -i;
+ }
+ }
+ /* walk thru column of matix F and check that all elements has
+ been marked */
+ for (j = 1; j <= n; j++)
+ { for (j_end = (j_ptr = fc_ptr[j]) + fc_len[j];
+ j_ptr < j_end; j_ptr++)
+ { xassert((i = sv_ind[j_ptr]) < 0);
+ /* unmark element f[i,j] */
+ sv_ind[j_ptr] = -i;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_check_v_rc - check rows and columns of matrix V
+*
+* This routine checks that the row- and column-wise representations
+* of matrix V are identical.
+*
+* NOTE: For testing/debugging only. */
+
+void luf_check_v_rc(LUF *luf)
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int i, i_end, i_ptr, j, j_end, j_ptr;
+ /* walk thru rows of matrix V */
+ for (i = 1; i <= n; i++)
+ { for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { j = sv_ind[i_ptr];
+ /* find element v[i,j] in j-th column of matrix V */
+ for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ sv_ind[j_ptr] != i; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ xassert(sv_val[i_ptr] == sv_val[j_ptr]);
+ /* mark element v[i,j] */
+ sv_ind[j_ptr] = -i;
+ }
+ }
+ /* walk thru column of matix V and check that all elements has
+ been marked */
+ for (j = 1; j <= n; j++)
+ { for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ j_ptr < j_end; j_ptr++)
+ { xassert((i = sv_ind[j_ptr]) < 0);
+ /* unmark element v[i,j] */
+ sv_ind[j_ptr] = -i;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_f_solve - solve system F * x = b
+*
+* This routine solves the system F * x = b, where the matrix F is the
+* left factor of the sparse LU-factorization.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix F. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void luf_f_solve(LUF *luf, double x[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int j, k, ptr, end;
+ double x_j;
+ for (k = 1; k <= n; k++)
+ { /* k-th column of L = j-th column of F */
+ j = pp_inv[k];
+ /* x[j] is already computed */
+ /* walk thru j-th column of matrix F and substitute x[j] into
+ * other equations */
+ if ((x_j = x[j]) != 0.0)
+ { for (end = (ptr = fc_ptr[j]) + fc_len[j]; ptr < end; ptr++)
+ x[sv_ind[ptr]] -= sv_val[ptr] * x_j;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_ft_solve - solve system F' * x = b
+*
+* This routine solves the system F' * x = b, where F' is a matrix
+* transposed to the matrix F, which is the left factor of the sparse
+* LU-factorization.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix F. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void luf_ft_solve(LUF *luf, double x[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fr_ref = luf->fr_ref;
+ int *fr_ptr = &sva->ptr[fr_ref-1];
+ int *fr_len = &sva->len[fr_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int i, k, ptr, end;
+ double x_i;
+ for (k = n; k >= 1; k--)
+ { /* k-th column of L' = i-th row of F */
+ i = pp_inv[k];
+ /* x[i] is already computed */
+ /* walk thru i-th row of matrix F and substitute x[i] into
+ * other equations */
+ if ((x_i = x[i]) != 0.0)
+ { for (end = (ptr = fr_ptr[i]) + fr_len[i]; ptr < end; ptr++)
+ x[sv_ind[ptr]] -= sv_val[ptr] * x_i;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_v_solve - solve system V * x = b
+*
+* This routine solves the system V * x = b, where the matrix V is the
+* right factor of the sparse LU-factorization.
+*
+* On entry the array b should contain elements of the right-hand side
+* vector b in locations b[1], ..., b[n], where n is the order of the
+* matrix V. On exit the array x will contain elements of the solution
+* vector x in locations x[1], ..., x[n]. Note that the array b will be
+* clobbered on exit. */
+
+void luf_v_solve(LUF *luf, double b[/*1+n*/], double x[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int i, j, k, ptr, end;
+ double x_j;
+ for (k = n; k >= 1; k--)
+ { /* k-th row of U = i-th row of V */
+ /* k-th column of U = j-th column of V */
+ i = pp_inv[k];
+ j = qq_ind[k];
+ /* compute x[j] = b[i] / u[k,k], where u[k,k] = v[i,j];
+ * walk through j-th column of matrix V and substitute x[j]
+ * into other equations */
+ if ((x_j = x[j] = b[i] / vr_piv[i]) != 0.0)
+ { for (end = (ptr = vc_ptr[j]) + vc_len[j]; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * x_j;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_vt_solve - solve system V' * x = b
+*
+* This routine solves the system V' * x = b, where V' is a matrix
+* transposed to the matrix V, which is the right factor of the sparse
+* LU-factorization.
+*
+* On entry the array b should contain elements of the right-hand side
+* vector b in locations b[1], ..., b[n], where n is the order of the
+* matrix V. On exit the array x will contain elements of the solution
+* vector x in locations x[1], ..., x[n]. Note that the array b will be
+* clobbered on exit. */
+
+void luf_vt_solve(LUF *luf, double b[/*1+n*/], double x[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ double *vr_piv = luf->vr_piv;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int i, j, k, ptr, end;
+ double x_i;
+ for (k = 1; k <= n; k++)
+ { /* k-th row of U' = j-th column of V */
+ /* k-th column of U' = i-th row of V */
+ i = pp_inv[k];
+ j = qq_ind[k];
+ /* compute x[i] = b[j] / u'[k,k], where u'[k,k] = v[i,j];
+ * walk through i-th row of matrix V and substitute x[i] into
+ * other equations */
+ if ((x_i = x[i] = b[j] / vr_piv[i]) != 0.0)
+ { for (end = (ptr = vr_ptr[i]) + vr_len[i]; ptr < end; ptr++)
+ b[sv_ind[ptr]] -= sv_val[ptr] * x_i;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_vt_solve1 - solve system V' * y = e' to cause growth in y
+*
+* This routine is a special version of luf_vt_solve. It solves the
+* system V'* y = e' = e + delta e, where V' is a matrix transposed to
+* the matrix V, e is the specified right-hand side vector, and delta e
+* is a vector of +1 and -1 chosen to cause growth in the solution
+* vector y.
+*
+* On entry the array e should contain elements of the right-hand side
+* vector e in locations e[1], ..., e[n], where n is the order of the
+* matrix V. On exit the array y will contain elements of the solution
+* vector y in locations y[1], ..., y[n]. Note that the array e will be
+* clobbered on exit. */
+
+void luf_vt_solve1(LUF *luf, double e[/*1+n*/], double y[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ double *vr_piv = luf->vr_piv;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int i, j, k, ptr, end;
+ double e_j, y_i;
+ for (k = 1; k <= n; k++)
+ { /* k-th row of U' = j-th column of V */
+ /* k-th column of U' = i-th row of V */
+ i = pp_inv[k];
+ j = qq_ind[k];
+ /* determine e'[j] = e[j] + delta e[j] */
+ e_j = (e[j] >= 0.0 ? e[j] + 1.0 : e[j] - 1.0);
+ /* compute y[i] = e'[j] / u'[k,k], where u'[k,k] = v[i,j] */
+ y_i = y[i] = e_j / vr_piv[i];
+ /* walk through i-th row of matrix V and substitute y[i] into
+ * other equations */
+ for (end = (ptr = vr_ptr[i]) + vr_len[i]; ptr < end; ptr++)
+ e[sv_ind[ptr]] -= sv_val[ptr] * y_i;
+ }
+ return;
+}
+
+/***********************************************************************
+* luf_estimate_norm - estimate 1-norm of inv(A)
+*
+* This routine estimates 1-norm of inv(A) by one step of inverse
+* iteration for the small singular vector as described in [1]. This
+* involves solving two systems of equations:
+*
+* A'* y = e,
+*
+* A * z = y,
+*
+* where A' is a matrix transposed to A, and e is a vector of +1 and -1
+* chosen to cause growth in y. Then
+*
+* estimate 1-norm of inv(A) = (1-norm of z) / (1-norm of y)
+*
+* REFERENCES
+*
+* 1. G.E.Forsythe, M.A.Malcolm, C.B.Moler. Computer Methods for
+* Mathematical Computations. Prentice-Hall, Englewood Cliffs, N.J.,
+* pp. 30-62 (subroutines DECOMP and SOLVE). */
+
+double luf_estimate_norm(LUF *luf, double w1[/*1+n*/], double
+ w2[/*1+n*/])
+{ int n = luf->n;
+ double *e = w1;
+ double *y = w2;
+ double *z = w1;
+ int i;
+ double y_norm, z_norm;
+ /* y = inv(A') * e = inv(F') * inv(V') * e */
+ /* compute y' = inv(V') * e to cause growth in y' */
+ for (i = 1; i <= n; i++)
+ e[i] = 0.0;
+ luf_vt_solve1(luf, e, y);
+ /* compute y = inv(F') * y' */
+ luf_ft_solve(luf, y);
+ /* compute 1-norm of y = sum |y[i]| */
+ y_norm = 0.0;
+ for (i = 1; i <= n; i++)
+ y_norm += (y[i] >= 0.0 ? +y[i] : -y[i]);
+ /* z = inv(A) * y = inv(V) * inv(F) * y */
+ /* compute z' = inv(F) * y */
+ luf_f_solve(luf, y);
+ /* compute z = inv(V) * z' */
+ luf_v_solve(luf, y, z);
+ /* compute 1-norm of z = sum |z[i]| */
+ z_norm = 0.0;
+ for (i = 1; i <= n; i++)
+ z_norm += (z[i] >= 0.0 ? +z[i] : -z[i]);
+ /* estimate 1-norm of inv(A) = (1-norm of z) / (1-norm of y) */
+ return z_norm / y_norm;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/luf.h b/test/monniaux/glpk-4.65/src/bflib/luf.h
new file mode 100644
index 00000000..5634a753
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/luf.h
@@ -0,0 +1,227 @@
+/* luf.h (sparse LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef LUF_H
+#define LUF_H
+
+#include "sva.h"
+
+/***********************************************************************
+* The structure LUF describes sparse LU-factorization.
+*
+* The LU-factorization has the following format:
+*
+* A = F * V = P * L * U * Q, (1)
+*
+* F = P * L * P', (2)
+*
+* V = P * U * Q, (3)
+*
+* where A is a given (unsymmetric) square matrix, F and V are matrix
+* factors actually computed, L is a lower triangular matrix with unity
+* diagonal, U is an upper triangular matrix, P and Q are permutation
+* matrices, P' is a matrix transposed to P. All the matrices have the
+* same order n.
+*
+* Matrices F and V are stored in both row- and column-wise sparse
+* formats in the associated sparse vector area (SVA). Unity diagonal
+* elements of matrix F are not stored. Pivot elements of matrix V
+* (which correspond to diagonal elements of matrix U) are stored in
+* a separate ordinary array.
+*
+* Permutation matrices P and Q are stored in ordinary arrays in both
+* row- and column-like formats.
+*
+* Matrices L and U are completely defined by matrices F, V, P, and Q,
+* and therefore not stored explicitly. */
+
+typedef struct LUF LUF;
+
+struct LUF
+{ /* sparse LU-factorization */
+ int n;
+ /* order of matrices A, F, V, P, Q */
+ SVA *sva;
+ /* associated sparse vector area (SVA) used to store rows and
+ * columns of matrices F and V; note that different objects may
+ * share the same SVA */
+ /*--------------------------------------------------------------*/
+ /* matrix F in row-wise format */
+ /* during the factorization process this object is not used */
+ int fr_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * row of matrix F */
+#if 0 + 0
+ int *fr_ptr = &sva->ptr[fr_ref-1];
+ /* fr_ptr[0] is not used;
+ * fr_ptr[i], 1 <= i <= n, is pointer to i-th row in SVA */
+ int *fr_len = &sva->len[fr_ref-1];
+ /* fr_len[0] is not used;
+ * fr_len[i], 1 <= i <= n, is length of i-th row */
+#endif
+ /*--------------------------------------------------------------*/
+ /* matrix F in column-wise format */
+ /* during the factorization process this object is constructed
+ * by columns */
+ int fc_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * column of matrix F */
+#if 0 + 0
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ /* fc_ptr[0] is not used;
+ * fc_ptr[j], 1 <= j <= n, is pointer to j-th column in SVA */
+ int *fc_len = &sva->len[fc_ref-1];
+ /* fc_len[0] is not used;
+ * fc_len[j], 1 <= j <= n, is length of j-th column */
+#endif
+ /*--------------------------------------------------------------*/
+ /* matrix V in row-wise format */
+ int vr_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * row of matrix V */
+#if 0 + 0
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ /* vr_ptr[0] is not used;
+ * vr_ptr[i], 1 <= i <= n, is pointer to i-th row in SVA */
+ int *vr_len = &sva->len[vr_ref-1];
+ /* vr_len[0] is not used;
+ * vr_len[i], 1 <= i <= n, is length of i-th row */
+ int *vr_cap = &sva->cap[vr_ref-1];
+ /* vr_cap[0] is not used;
+ * vr_cap[i], 1 <= i <= n, is capacity of i-th row */
+#endif
+ double *vr_piv; /* double vr_piv[1+n]; */
+ /* vr_piv[0] is not used;
+ * vr_piv[i], 1 <= i <= n, is pivot element of i-th row */
+ /*--------------------------------------------------------------*/
+ /* matrix V in column-wise format */
+ /* during the factorization process this object contains only the
+ * patterns (row indices) of columns of the active submatrix */
+ int vc_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * column of matrix V */
+#if 0 + 0
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ /* vc_ptr[0] is not used;
+ * vc_ptr[j], 1 <= j <= n, is pointer to j-th column in SVA */
+ int *vc_len = &sva->len[vc_ref-1];
+ /* vc_len[0] is not used;
+ * vc_len[j], 1 <= j <= n, is length of j-th column */
+ int *vc_cap = &sva->cap[vc_ref-1];
+ /* vc_cap[0] is not used;
+ * vc_cap[j], 1 <= j <= n, is capacity of j-th column */
+#endif
+ /*--------------------------------------------------------------*/
+ /* matrix P */
+ int *pp_ind; /* int pp_ind[1+n]; */
+ /* pp_ind[i] = j means that P[i,j] = 1 */
+ int *pp_inv; /* int pp_inv[1+n]; */
+ /* pp_inv[j] = i means that P[i,j] = 1 */
+ /* if i-th row or column of matrix F is i'-th row or column of
+ * matrix L, or if i-th row of matrix V is i'-th row of matrix U,
+ * then pp_ind[i] = i' and pp_inv[i'] = i */
+ /*--------------------------------------------------------------*/
+ /* matrix Q */
+ int *qq_ind; /* int qq_ind[1+n]; */
+ /* qq_ind[i] = j means that Q[i,j] = 1 */
+ int *qq_inv; /* int qq_inv[1+n]; */
+ /* qq_inv[j] = i means that Q[i,j] = 1 */
+ /* if j-th column of matrix V is j'-th column of matrix U, then
+ * qq_ind[j'] = j and qq_inv[j] = j' */
+};
+
+#define luf_swap_u_rows(i1, i2) \
+ do \
+ { int j1, j2; \
+ j1 = pp_inv[i1], j2 = pp_inv[i2]; \
+ pp_ind[j1] = i2, pp_inv[i2] = j1; \
+ pp_ind[j2] = i1, pp_inv[i1] = j2; \
+ } while (0)
+/* swap rows i1 and i2 of matrix U = P'* V * Q' */
+
+#define luf_swap_u_cols(j1, j2) \
+ do \
+ { int i1, i2; \
+ i1 = qq_ind[j1], i2 = qq_ind[j2]; \
+ qq_ind[j1] = i2, qq_inv[i2] = j1; \
+ qq_ind[j2] = i1, qq_inv[i1] = j2; \
+ } while (0)
+/* swap columns j1 and j2 of matrix U = P'* V * Q' */
+
+#define luf_store_v_cols _glp_luf_store_v_cols
+int luf_store_v_cols(LUF *luf, int (*col)(void *info, int j, int ind[],
+ double val[]), void *info, int ind[], double val[]);
+/* store matrix V = A in column-wise format */
+
+#define luf_check_all _glp_luf_check_all
+void luf_check_all(LUF *luf, int k);
+/* check LU-factorization before k-th elimination step */
+
+#define luf_build_v_rows _glp_luf_build_v_rows
+void luf_build_v_rows(LUF *luf, int len[/*1+n*/]);
+/* build matrix V in row-wise format */
+
+#define luf_build_f_rows _glp_luf_build_f_rows
+void luf_build_f_rows(LUF *luf, int len[/*1+n*/]);
+/* build matrix F in row-wise format */
+
+#define luf_build_v_cols _glp_luf_build_v_cols
+void luf_build_v_cols(LUF *luf, int updat, int len[/*1+n*/]);
+/* build matrix V in column-wise format */
+
+#define luf_check_f_rc _glp_luf_check_f_rc
+void luf_check_f_rc(LUF *luf);
+/* check rows and columns of matrix F */
+
+#define luf_check_v_rc _glp_luf_check_v_rc
+void luf_check_v_rc(LUF *luf);
+/* check rows and columns of matrix V */
+
+#define luf_f_solve _glp_luf_f_solve
+void luf_f_solve(LUF *luf, double x[/*1+n*/]);
+/* solve system F * x = b */
+
+#define luf_ft_solve _glp_luf_ft_solve
+void luf_ft_solve(LUF *luf, double x[/*1+n*/]);
+/* solve system F' * x = b */
+
+#define luf_v_solve _glp_luf_v_solve
+void luf_v_solve(LUF *luf, double b[/*1+n*/], double x[/*1+n*/]);
+/* solve system V * x = b */
+
+#define luf_vt_solve _glp_luf_vt_solve
+void luf_vt_solve(LUF *luf, double b[/*1+n*/], double x[/*1+n*/]);
+/* solve system V' * x = b */
+
+#define luf_vt_solve1 _glp_luf_vt_solve1
+void luf_vt_solve1(LUF *luf, double e[/*1+n*/], double y[/*1+n*/]);
+/* solve system V' * y = e' to cause growth in y */
+
+#define luf_estimate_norm _glp_luf_estimate_norm
+double luf_estimate_norm(LUF *luf, double w1[/*1+n*/], double
+ w2[/*1+n*/]);
+/* estimate 1-norm of inv(A) */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/lufint.c b/test/monniaux/glpk-4.65/src/bflib/lufint.c
new file mode 100644
index 00000000..7cd00924
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/lufint.c
@@ -0,0 +1,182 @@
+/* lufint.c (interface to LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "lufint.h"
+
+LUFINT *lufint_create(void)
+{ /* create interface to LU-factorization */
+ LUFINT *fi;
+ fi = talloc(1, LUFINT);
+ fi->n_max = 0;
+ fi->valid = 0;
+ fi->sva = NULL;
+ fi->luf = NULL;
+ fi->sgf = NULL;
+ fi->sva_n_max = fi->sva_size = 0;
+ fi->delta_n0 = fi->delta_n = 0;
+ fi->sgf_updat = 0;
+ fi->sgf_piv_tol = 0.10;
+ fi->sgf_piv_lim = 4;
+ fi->sgf_suhl = 1;
+ fi->sgf_eps_tol = DBL_EPSILON;
+ return fi;
+}
+
+int lufint_factorize(LUFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info)
+{ /* compute LU-factorization of specified matrix A */
+ SVA *sva;
+ LUF *luf;
+ SGF *sgf;
+ int k;
+ xassert(n > 0);
+ fi->valid = 0;
+ /* create sparse vector area (SVA), if necessary */
+ sva = fi->sva;
+ if (sva == NULL)
+ { int sva_n_max = fi->sva_n_max;
+ int sva_size = fi->sva_size;
+ if (sva_n_max == 0)
+ sva_n_max = 4 * n;
+ if (sva_size == 0)
+ sva_size = 10 * n;
+ sva = fi->sva = sva_create_area(sva_n_max, sva_size);
+ }
+ /* allocate/reallocate underlying objects, if necessary */
+ if (fi->n_max < n)
+ { int n_max = fi->n_max;
+ if (n_max == 0)
+ n_max = fi->n_max = n + fi->delta_n0;
+ else
+ n_max = fi->n_max = n + fi->delta_n;
+ xassert(n_max >= n);
+ /* allocate/reallocate LU-factorization (LUF) */
+ luf = fi->luf;
+ if (luf == NULL)
+ { luf = fi->luf = talloc(1, LUF);
+ memset(luf, 0, sizeof(LUF));
+ luf->sva = sva;
+ }
+ else
+ { tfree(luf->vr_piv);
+ tfree(luf->pp_ind);
+ tfree(luf->pp_inv);
+ tfree(luf->qq_ind);
+ tfree(luf->qq_inv);
+ }
+ luf->vr_piv = talloc(1+n_max, double);
+ luf->pp_ind = talloc(1+n_max, int);
+ luf->pp_inv = talloc(1+n_max, int);
+ luf->qq_ind = talloc(1+n_max, int);
+ luf->qq_inv = talloc(1+n_max, int);
+ /* allocate/reallocate factorizer workspace (SGF) */
+ sgf = fi->sgf;
+ if (sgf == NULL)
+ { sgf = fi->sgf = talloc(1, SGF);
+ memset(sgf, 0, sizeof(SGF));
+ sgf->luf = luf;
+ }
+ else
+ { tfree(sgf->rs_head);
+ tfree(sgf->rs_prev);
+ tfree(sgf->rs_next);
+ tfree(sgf->cs_head);
+ tfree(sgf->cs_prev);
+ tfree(sgf->cs_next);
+ tfree(sgf->vr_max);
+ tfree(sgf->flag);
+ tfree(sgf->work);
+ }
+ sgf->rs_head = talloc(1+n_max, int);
+ sgf->rs_prev = talloc(1+n_max, int);
+ sgf->rs_next = talloc(1+n_max, int);
+ sgf->cs_head = talloc(1+n_max, int);
+ sgf->cs_prev = talloc(1+n_max, int);
+ sgf->cs_next = talloc(1+n_max, int);
+ sgf->vr_max = talloc(1+n_max, double);
+ sgf->flag = talloc(1+n_max, char);
+ sgf->work = talloc(1+n_max, double);
+ }
+ luf = fi->luf;
+ sgf = fi->sgf;
+#if 1 /* FIXME */
+ /* initialize SVA */
+ sva->n = 0;
+ sva->m_ptr = 1;
+ sva->r_ptr = sva->size + 1;
+ sva->head = sva->tail = 0;
+#endif
+ /* allocate sparse vectors in SVA */
+ luf->n = n;
+ luf->fr_ref = sva_alloc_vecs(sva, n);
+ luf->fc_ref = sva_alloc_vecs(sva, n);
+ luf->vr_ref = sva_alloc_vecs(sva, n);
+ luf->vc_ref = sva_alloc_vecs(sva, n);
+ /* store matrix V = A in column-wise format */
+ luf_store_v_cols(luf, col, info, sgf->rs_prev, sgf->work);
+ /* setup factorizer control parameters */
+ sgf->updat = fi->sgf_updat;
+ sgf->piv_tol = fi->sgf_piv_tol;
+ sgf->piv_lim = fi->sgf_piv_lim;
+ sgf->suhl = fi->sgf_suhl;
+ sgf->eps_tol = fi->sgf_eps_tol;
+ /* compute LU-factorization of specified matrix A */
+ k = sgf_factorize(sgf, 1);
+ if (k == 0)
+ fi->valid = 1;
+ return k;
+}
+
+void lufint_delete(LUFINT *fi)
+{ /* delete interface to LU-factorization */
+ SVA *sva = fi->sva;
+ LUF *luf = fi->luf;
+ SGF *sgf = fi->sgf;
+ if (sva != NULL)
+ sva_delete_area(sva);
+ if (luf != NULL)
+ { tfree(luf->vr_piv);
+ tfree(luf->pp_ind);
+ tfree(luf->pp_inv);
+ tfree(luf->qq_ind);
+ tfree(luf->qq_inv);
+ tfree(luf);
+ }
+ if (sgf != NULL)
+ { tfree(sgf->rs_head);
+ tfree(sgf->rs_prev);
+ tfree(sgf->rs_next);
+ tfree(sgf->cs_head);
+ tfree(sgf->cs_prev);
+ tfree(sgf->cs_next);
+ tfree(sgf->vr_max);
+ tfree(sgf->flag);
+ tfree(sgf->work);
+ tfree(sgf);
+ }
+ tfree(fi);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/lufint.h b/test/monniaux/glpk-4.65/src/bflib/lufint.h
new file mode 100644
index 00000000..b3ad5b64
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/lufint.h
@@ -0,0 +1,73 @@
+/* lufint.h (interface to LU-factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef LUFINT_H
+#define LUFINT_H
+
+#include "sgf.h"
+
+typedef struct LUFINT LUFINT;
+
+struct LUFINT
+{ /* interface to LU-factorization */
+ int n_max;
+ /* maximal value of n (increased automatically) */
+ int valid;
+ /* factorization is valid only if this flag is set */
+ SVA *sva;
+ /* sparse vector area (SVA) */
+ LUF *luf;
+ /* sparse LU-factorization */
+ SGF *sgf;
+ /* sparse Gaussian factorizer workspace */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ int sva_n_max, sva_size;
+ /* parameters passed to sva_create_area */
+ int delta_n0, delta_n;
+ /* if n_max = 0, set n_max = n + delta_n0
+ * if n_max < n, set n_max = n + delta_n */
+ int sgf_updat;
+ double sgf_piv_tol;
+ int sgf_piv_lim;
+ int sgf_suhl;
+ double sgf_eps_tol;
+ /* factorizer control parameters */
+};
+
+#define lufint_create _glp_lufint_create
+LUFINT *lufint_create(void);
+/* create interface to LU-factorization */
+
+#define lufint_factorize _glp_lufint_factorize
+int lufint_factorize(LUFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info);
+/* compute LU-factorization of specified matrix A */
+
+#define lufint_delete _glp_lufint_delete
+void lufint_delete(LUFINT *fi);
+/* delete interface to LU-factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/scf.c b/test/monniaux/glpk-4.65/src/bflib/scf.c
new file mode 100644
index 00000000..556b1911
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/scf.c
@@ -0,0 +1,523 @@
+/* scf.c (sparse updatable Schur-complement-based factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "scf.h"
+
+/***********************************************************************
+* scf_r0_solve - solve system R0 * x = b or R0'* x = b
+*
+* This routine solves the system R0 * x = b (if tr is zero) or the
+* system R0'* x = b (if tr is non-zero), where R0 is the left factor
+* of the initial matrix A0 = R0 * S0.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n0], where n0 is the order of the
+* matrix R0. On exit the array x will contain elements of the solution
+* vector in the same locations. */
+
+void scf_r0_solve(SCF *scf, int tr, double x[/*1+n0*/])
+{ switch (scf->type)
+ { case 1:
+ /* A0 = F0 * V0, so R0 = F0 */
+ if (!tr)
+ luf_f_solve(scf->a0.luf, x);
+ else
+ luf_ft_solve(scf->a0.luf, x);
+ break;
+ case 2:
+ /* A0 = I * A0, so R0 = I */
+ break;
+ default:
+ xassert(scf != scf);
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_s0_solve - solve system S0 * x = b or S0'* x = b
+*
+* This routine solves the system S0 * x = b (if tr is zero) or the
+* system S0'* x = b (if tr is non-zero), where S0 is the right factor
+* of the initial matrix A0 = R0 * S0.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n0], where n0 is the order of the
+* matrix S0. On exit the array x will contain elements of the solution
+* vector in the same locations.
+*
+* The routine uses locations [1], ..., [n0] of three working arrays
+* w1, w2, and w3. (In case of type = 1 arrays w2 and w3 are not used
+* and can be specified as NULL.) */
+
+void scf_s0_solve(SCF *scf, int tr, double x[/*1+n0*/],
+ double w1[/*1+n0*/], double w2[/*1+n0*/], double w3[/*1+n0*/])
+{ int n0 = scf->n0;
+ switch (scf->type)
+ { case 1:
+ /* A0 = F0 * V0, so S0 = V0 */
+ if (!tr)
+ luf_v_solve(scf->a0.luf, x, w1);
+ else
+ luf_vt_solve(scf->a0.luf, x, w1);
+ break;
+ case 2:
+ /* A0 = I * A0, so S0 = A0 */
+ if (!tr)
+ btf_a_solve(scf->a0.btf, x, w1, w2, w3);
+ else
+ btf_at_solve(scf->a0.btf, x, w1, w2, w3);
+ break;
+ default:
+ xassert(scf != scf);
+ }
+ memcpy(&x[1], &w1[1], n0 * sizeof(double));
+ return;
+}
+
+/***********************************************************************
+* scf_r_prod - compute product y := y + alpha * R * x
+*
+* This routine computes the product y := y + alpha * R * x, where
+* x is a n0-vector, alpha is a scalar, y is a nn-vector.
+*
+* Since matrix R is available by rows, the product components are
+* computed as inner products:
+*
+* y[i] = y[i] + alpha * (i-th row of R) * x
+*
+* for i = 1, 2, ..., nn. */
+
+void scf_r_prod(SCF *scf, double y[/*1+nn*/], double a, const double
+ x[/*1+n0*/])
+{ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int rr_ref = scf->rr_ref;
+ int *rr_ptr = &sva->ptr[rr_ref-1];
+ int *rr_len = &sva->len[rr_ref-1];
+ int i, ptr, end;
+ double t;
+ for (i = 1; i <= nn; i++)
+ { /* t := (i-th row of R) * x */
+ t = 0.0;
+ for (end = (ptr = rr_ptr[i]) + rr_len[i]; ptr < end; ptr++)
+ t += sv_val[ptr] * x[sv_ind[ptr]];
+ /* y[i] := y[i] + alpha * t */
+ y[i] += a * t;
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_rt_prod - compute product y := y + alpha * R'* x
+*
+* This routine computes the product y := y + alpha * R'* x, where
+* R' is a matrix transposed to R, x is a nn-vector, alpha is a scalar,
+* y is a n0-vector.
+*
+* Since matrix R is available by rows, the product is computed as a
+* linear combination:
+*
+* y := y + alpha * (R'[1] * x[1] + ... + R'[nn] * x[nn]),
+*
+* where R'[i] is i-th row of R. */
+
+void scf_rt_prod(SCF *scf, double y[/*1+n0*/], double a, const double
+ x[/*1+nn*/])
+{ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int rr_ref = scf->rr_ref;
+ int *rr_ptr = &sva->ptr[rr_ref-1];
+ int *rr_len = &sva->len[rr_ref-1];
+ int i, ptr, end;
+ double t;
+ for (i = 1; i <= nn; i++)
+ { if (x[i] == 0.0)
+ continue;
+ /* y := y + alpha * R'[i] * x[i] */
+ t = a * x[i];
+ for (end = (ptr = rr_ptr[i]) + rr_len[i]; ptr < end; ptr++)
+ y[sv_ind[ptr]] += sv_val[ptr] * t;
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_s_prod - compute product y := y + alpha * S * x
+*
+* This routine computes the product y := y + alpha * S * x, where
+* x is a nn-vector, alpha is a scalar, y is a n0 vector.
+*
+* Since matrix S is available by columns, the product is computed as
+* a linear combination:
+*
+* y := y + alpha * (S[1] * x[1] + ... + S[nn] * x[nn]),
+*
+* where S[j] is j-th column of S. */
+
+void scf_s_prod(SCF *scf, double y[/*1+n0*/], double a, const double
+ x[/*1+nn*/])
+{ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int ss_ref = scf->ss_ref;
+ int *ss_ptr = &sva->ptr[ss_ref-1];
+ int *ss_len = &sva->len[ss_ref-1];
+ int j, ptr, end;
+ double t;
+ for (j = 1; j <= nn; j++)
+ { if (x[j] == 0.0)
+ continue;
+ /* y := y + alpha * S[j] * x[j] */
+ t = a * x[j];
+ for (end = (ptr = ss_ptr[j]) + ss_len[j]; ptr < end; ptr++)
+ y[sv_ind[ptr]] += sv_val[ptr] * t;
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_st_prod - compute product y := y + alpha * S'* x
+*
+* This routine computes the product y := y + alpha * S'* x, where
+* S' is a matrix transposed to S, x is a n0-vector, alpha is a scalar,
+* y is a nn-vector.
+*
+* Since matrix S is available by columns, the product components are
+* computed as inner products:
+*
+* y[j] := y[j] + alpha * (j-th column of S) * x
+*
+* for j = 1, 2, ..., nn. */
+
+void scf_st_prod(SCF *scf, double y[/*1+nn*/], double a, const double
+ x[/*1+n0*/])
+{ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int ss_ref = scf->ss_ref;
+ int *ss_ptr = &sva->ptr[ss_ref-1];
+ int *ss_len = &sva->len[ss_ref-1];
+ int j, ptr, end;
+ double t;
+ for (j = 1; j <= nn; j++)
+ { /* t := (j-th column of S) * x */
+ t = 0.0;
+ for (end = (ptr = ss_ptr[j]) + ss_len[j]; ptr < end; ptr++)
+ t += sv_val[ptr] * x[sv_ind[ptr]];
+ /* y[j] := y[j] + alpha * t */
+ y[j] += a * t;
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_a_solve - solve system A * x = b
+*
+* This routine solves the system A * x = b, where A is the current
+* matrix.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix A. On exit the array x will contain elements of the solution
+* vector in the same locations.
+*
+* For details see the program documentation. */
+
+void scf_a_solve(SCF *scf, double x[/*1+n*/],
+ double w[/*1+n0+nn*/], double work1[/*1+max(n0,nn)*/],
+ double work2[/*1+n*/], double work3[/*1+n*/])
+{ int n = scf->n;
+ int n0 = scf->n0;
+ int nn = scf->nn;
+ int *pp_ind = scf->pp_ind;
+ int *qq_inv = scf->qq_inv;
+ int i, ii;
+ /* (u1, u2) := inv(P) * (b, 0) */
+ for (ii = 1; ii <= n0+nn; ii++)
+ { i = pp_ind[ii];
+#if 1 /* FIXME: currently P = I */
+ xassert(i == ii);
+#endif
+ w[ii] = (i <= n ? x[i] : 0.0);
+ }
+ /* v1 := inv(R0) * u1 */
+ scf_r0_solve(scf, 0, &w[0]);
+ /* v2 := u2 - R * v1 */
+ scf_r_prod(scf, &w[n0], -1.0, &w[0]);
+ /* w2 := inv(C) * v2 */
+ ifu_a_solve(&scf->ifu, &w[n0], work1);
+ /* w1 := inv(S0) * (v1 - S * w2) */
+ scf_s_prod(scf, &w[0], -1.0, &w[n0]);
+ scf_s0_solve(scf, 0, &w[0], work1, work2, work3);
+ /* (x, x~) := inv(Q) * (w1, w2); x~ is not needed */
+ for (i = 1; i <= n; i++)
+ x[i] = w[qq_inv[i]];
+ return;
+}
+
+/***********************************************************************
+* scf_at_solve - solve system A'* x = b
+*
+* This routine solves the system A'* x = b, where A' is a matrix
+* transposed to the current matrix A.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix A. On exit the array x will contain elements of the solution
+* vector in the same locations.
+*
+* For details see the program documentation. */
+
+void scf_at_solve(SCF *scf, double x[/*1+n*/],
+ double w[/*1+n0+nn*/], double work1[/*1+max(n0,nn)*/],
+ double work2[/*1+n*/], double work3[/*1+n*/])
+{ int n = scf->n;
+ int n0 = scf->n0;
+ int nn = scf->nn;
+ int *pp_inv = scf->pp_inv;
+ int *qq_ind = scf->qq_ind;
+ int i, ii;
+ /* (u1, u2) := Q * (b, 0) */
+ for (ii = 1; ii <= n0+nn; ii++)
+ { i = qq_ind[ii];
+ w[ii] = (i <= n ? x[i] : 0.0);
+ }
+ /* v1 := inv(S0') * u1 */
+ scf_s0_solve(scf, 1, &w[0], work1, work2, work3);
+ /* v2 := inv(C') * (u2 - S'* v1) */
+ scf_st_prod(scf, &w[n0], -1.0, &w[0]);
+ ifu_at_solve(&scf->ifu, &w[n0], work1);
+ /* w2 := v2 */
+ /* nop */
+ /* w1 := inv(R0') * (v1 - R'* w2) */
+ scf_rt_prod(scf, &w[0], -1.0, &w[n0]);
+ scf_r0_solve(scf, 1, &w[0]);
+ /* compute (x, x~) := P * (w1, w2); x~ is not needed */
+ for (i = 1; i <= n; i++)
+ {
+#if 1 /* FIXME: currently P = I */
+ xassert(pp_inv[i] == i);
+#endif
+ x[i] = w[pp_inv[i]];
+ }
+ return;
+}
+
+/***********************************************************************
+* scf_add_r_row - add new row to matrix R
+*
+* This routine adds new (nn+1)-th row to matrix R, whose elements are
+* specified in locations w[1,...,n0]. */
+
+void scf_add_r_row(SCF *scf, const double w[/*1+n0*/])
+{ int n0 = scf->n0;
+ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int rr_ref = scf->rr_ref;
+ int *rr_ptr = &sva->ptr[rr_ref-1];
+ int *rr_len = &sva->len[rr_ref-1];
+ int j, len, ptr;
+ xassert(0 <= nn && nn < scf->nn_max);
+ /* determine length of new row */
+ len = 0;
+ for (j = 1; j <= n0; j++)
+ { if (w[j] != 0.0)
+ len++;
+ }
+ /* reserve locations for new row in static part of SVA */
+ if (len > 0)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, rr_ref + nn, len);
+ }
+ /* store new row in sparse format */
+ ptr = rr_ptr[nn+1];
+ for (j = 1; j <= n0; j++)
+ { if (w[j] != 0.0)
+ { sv_ind[ptr] = j;
+ sv_val[ptr] = w[j];
+ ptr++;
+ }
+ }
+ xassert(ptr - rr_ptr[nn+1] == len);
+ rr_len[nn+1] = len;
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+#endif
+ return;
+}
+
+/***********************************************************************
+* scf_add_s_col - add new column to matrix S
+*
+* This routine adds new (nn+1)-th column to matrix S, whose elements
+* are specified in locations v[1,...,n0]. */
+
+void scf_add_s_col(SCF *scf, const double v[/*1+n0*/])
+{ int n0 = scf->n0;
+ int nn = scf->nn;
+ SVA *sva = scf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int ss_ref = scf->ss_ref;
+ int *ss_ptr = &sva->ptr[ss_ref-1];
+ int *ss_len = &sva->len[ss_ref-1];
+ int i, len, ptr;
+ xassert(0 <= nn && nn < scf->nn_max);
+ /* determine length of new column */
+ len = 0;
+ for (i = 1; i <= n0; i++)
+ { if (v[i] != 0.0)
+ len++;
+ }
+ /* reserve locations for new column in static part of SVA */
+ if (len > 0)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, ss_ref + nn, len);
+ }
+ /* store new column in sparse format */
+ ptr = ss_ptr[nn+1];
+ for (i = 1; i <= n0; i++)
+ { if (v[i] != 0.0)
+ { sv_ind[ptr] = i;
+ sv_val[ptr] = v[i];
+ ptr++;
+ }
+ }
+ xassert(ptr - ss_ptr[nn+1] == len);
+ ss_len[nn+1] = len;
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+#endif
+ return;
+}
+
+/***********************************************************************
+* scf_update_aug - update factorization of augmented matrix
+*
+* Given factorization of the current augmented matrix:
+*
+* ( A0 A1 ) ( R0 ) ( S0 S )
+* ( ) = ( ) ( ),
+* ( A2 A3 ) ( R I ) ( C )
+*
+* this routine computes factorization of the new augmented matrix:
+*
+* ( A0 | A1 b )
+* ( ---+------ ) ( A0 A1^ ) ( R0 ) ( S0 S^ )
+* ( A2 | A3 f ) = ( ) = ( ) ( ),
+* ( | ) ( A2^ A3^ ) ( R^ I ) ( C^ )
+* ( d' | g' h )
+*
+* where b and d are specified n0-vectors, f and g are specified
+* nn-vectors, and h is a specified scalar. (Note that corresponding
+* arrays are clobbered on exit.)
+*
+* The parameter upd specifies how to update factorization of the Schur
+* complement C:
+*
+* 1 Bartels-Golub updating.
+*
+* 2 Givens rotations updating.
+*
+* The working arrays w1, w2, and w3 are used in the same way as in the
+* routine scf_s0_solve.
+*
+* RETURNS
+*
+* 0 Factorization has been successfully updated.
+*
+* 1 Updating limit has been reached.
+*
+* 2 Updating IFU-factorization of matrix C failed.
+*
+* For details see the program documentation. */
+
+int scf_update_aug(SCF *scf, double b[/*1+n0*/], double d[/*1+n0*/],
+ double f[/*1+nn*/], double g[/*1+nn*/], double h, int upd,
+ double w1[/*1+n0*/], double w2[/*1+n0*/], double w3[/*1+n0*/])
+{ int n0 = scf->n0;
+ int k, ret;
+ double *v, *w, *x, *y, z;
+ if (scf->nn == scf->nn_max)
+ { /* updating limit has been reached */
+ return 1;
+ }
+ /* v := inv(R0) * b */
+ scf_r0_solve(scf, 0, (v = b));
+ /* w := inv(S0') * d */
+ scf_s0_solve(scf, 1, (w = d), w1, w2, w3);
+ /* x := f - R * v */
+ scf_r_prod(scf, (x = f), -1.0, v);
+ /* y := g - S'* w */
+ scf_st_prod(scf, (y = g), -1.0, w);
+ /* z := h - v'* w */
+ z = h;
+ for (k = 1; k <= n0; k++)
+ z -= v[k] * w[k];
+ /* new R := R with row w added */
+ scf_add_r_row(scf, w);
+ /* new S := S with column v added */
+ scf_add_s_col(scf, v);
+ /* update IFU-factorization of C */
+ switch (upd)
+ { case 1:
+ ret = ifu_bg_update(&scf->ifu, x, y, z);
+ break;
+ case 2:
+ ret = ifu_gr_update(&scf->ifu, x, y, z);
+ break;
+ default:
+ xassert(upd != upd);
+ }
+ if (ret != 0)
+ { /* updating IFU-factorization failed */
+ return 2;
+ }
+ /* increase number of additional rows and columns */
+ scf->nn++;
+ /* expand P and Q */
+ k = n0 + scf->nn;
+ scf->pp_ind[k] = scf->pp_inv[k] = k;
+ scf->qq_ind[k] = scf->qq_inv[k] = k;
+ /* factorization has been successfully updated */
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/scf.h b/test/monniaux/glpk-4.65/src/bflib/scf.h
new file mode 100644
index 00000000..69d8cfc2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/scf.h
@@ -0,0 +1,211 @@
+/* scf.h (sparse updatable Schur-complement-based factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SCF_H
+#define SCF_H
+
+#include "btf.h"
+#include "ifu.h"
+#include "luf.h"
+
+/***********************************************************************
+* The structure SCF describes sparse updatable factorization based on
+* Schur complement.
+*
+* The SCF-factorization has the following format:
+*
+* ( A A1~ ) ( A0 A1 ) ( R0 ) ( S0 S )
+* ( ) = P ( ) Q = P ( ) ( ) Q, (1)
+* ( A2~ A3~ ) ( A2 A3 ) ( R I ) ( C )
+*
+* where:
+*
+* A is current (unsymmetric) square matrix (not stored);
+*
+* A1~, A2~, A3~ are some additional matrices (not stored);
+*
+* A0 is initial (unsymmetric) square matrix (not stored);
+*
+* A1, A2, A3 are some additional matrices (not stored);
+*
+* R0 and S0 are matrices that define factorization of the initial
+* matrix A0 = R0 * S0 (stored in an invertable form);
+*
+* R is a matrix defined from R * S0 = A2, so R = A2 * inv(S0) (stored
+* in row-wise sparse format);
+*
+* S is a matrix defined from R0 * S = A1, so S = inv(R0) * A1 (stored
+* in column-wise sparse format);
+*
+* C is Schur complement (to matrix A0) defined from R * S + C = A3,
+* so C = A3 - R * S = A3 - A2 * inv(A0) * A1 (stored in an invertable
+* form).
+*
+* P, Q are permutation matrices (stored in both row- and column-like
+* formats). */
+
+typedef struct SCF SCF;
+
+struct SCF
+{ /* Schur-complement-based factorization */
+ int n;
+ /* order of current matrix A */
+ /*--------------------------------------------------------------*/
+ /* initial matrix A0 = R0 * S0 of order n0 in invertable form */
+ int n0;
+ /* order of matrix A0 */
+ int type;
+ /* type of factorization used:
+ * 1 - LU-factorization (R0 = F0, S0 = V0)
+ * 2 - BT-factorization (R0 = I, S0 = A0) */
+ union
+ { LUF *luf; /* type = 1 */
+ BTF *btf; /* type = 2 */
+ } a0;
+ /* factorization of matrix A0 */
+ /*--------------------------------------------------------------*/
+ /* augmented matrix (A0, A1; A2, A3) of order n0+nn */
+ int nn_max;
+ /* maximal number of additional rows and columns in the augmented
+ * matrix (this limits the number of updates) */
+ int nn;
+ /* current number of additional rows and columns in the augmented
+ * matrix, 0 <= nn <= nn_max */
+ SVA *sva;
+ /* associated sparse vector area (SVA) used to store rows of
+ * matrix R and columns of matrix S */
+ /*--------------------------------------------------------------*/
+ /* nn*n0-matrix R in row-wise format */
+ int rr_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * row of matrix R */
+#if 0 + 0
+ int *rr_ptr = &sva->ptr[rr_ref-1];
+ /* rr_ptr[0] is not used;
+ * rr_ptr[i], 1 <= i <= nn, is pointer to i-th row in SVA;
+ * rr_ptr[nn+1,...,nn_max] are reserved locations */
+ int *rr_len = &sva->len[rr_ref-1];
+ /* rr_len[0] is not used;
+ * rr_len[i], 1 <= i <= nn, is length of i-th row;
+ * rr_len[nn+1,...,nn_max] are reserved locations */
+#endif
+ /*--------------------------------------------------------------*/
+ /* n0*nn-matrix S in column-wise format */
+ int ss_ref;
+ /* reference number of sparse vector in SVA, which is the first
+ * column of matrix S */
+#if 0 + 0
+ int *ss_ptr = &sva->ptr[ss_ref-1];
+ /* ss_ptr[0] is not used;
+ * ss_ptr[j], 1 <= j <= nn, is pointer to j-th column in SVA;
+ * ss_ptr[nn+1,...,nn_max] are reserved locations */
+ int *ss_len = &sva->len[ss_ref-1];
+ /* ss_len[0] is not used;
+ * ss_len[j], 1 <= j <= nn, is length of j-th column;
+ * ss_len[nn+1,...,nn_max] are reserved locations */
+#endif
+ /*--------------------------------------------------------------*/
+ /* Schur complement C of order nn in invertable form */
+ IFU ifu;
+ /* IFU-factorization of matrix C */
+ /*--------------------------------------------------------------*/
+ /* permutation matrix P of order n0+nn */
+ int *pp_ind; /* int pp_ind[1+n0+nn_max]; */
+ /* pp_ind[i] = j means that P[i,j] = 1 */
+ int *pp_inv; /* int pp_inv[1+n0+nn_max]; */
+ /* pp_inv[j] = i means that P[i,j] = 1 */
+ /*--------------------------------------------------------------*/
+ /* permutation matrix Q of order n0+nn */
+ int *qq_ind; /* int qq_ind[1+n0+nn_max]; */
+ /* qq_ind[i] = j means that Q[i,j] = 1 */
+ int *qq_inv; /* int qq_inv[1+n0+nn_max]; */
+ /* qq_inv[j] = i means that Q[i,j] = 1 */
+};
+
+#define scf_swap_q_cols(j1, j2) \
+ do \
+ { int i1, i2; \
+ i1 = qq_inv[j1], i2 = qq_inv[j2]; \
+ qq_ind[i1] = j2, qq_inv[j2] = i1; \
+ qq_ind[i2] = j1, qq_inv[j1] = i2; \
+ } while (0)
+/* swap columns j1 and j2 of permutation matrix Q */
+
+#define scf_r0_solve _glp_scf_r0_solve
+void scf_r0_solve(SCF *scf, int tr, double x[/*1+n0*/]);
+/* solve system R0 * x = b or R0'* x = b */
+
+#define scf_s0_solve _glp_scf_s0_solve
+void scf_s0_solve(SCF *scf, int tr, double x[/*1+n0*/],
+ double w1[/*1+n0*/], double w2[/*1+n0*/], double w3[/*1+n0*/]);
+/* solve system S0 * x = b or S0'* x = b */
+
+#define scf_r_prod _glp_scf_r_prod
+void scf_r_prod(SCF *scf, double y[/*1+nn*/], double a, const double
+ x[/*1+n0*/]);
+/* compute product y := y + alpha * R * x */
+
+#define scf_rt_prod _glp_scf_rt_prod
+void scf_rt_prod(SCF *scf, double y[/*1+n0*/], double a, const double
+ x[/*1+nn*/]);
+/* compute product y := y + alpha * R'* x */
+
+#define scf_s_prod _glp_scf_s_prod
+void scf_s_prod(SCF *scf, double y[/*1+n0*/], double a, const double
+ x[/*1+nn*/]);
+/* compute product y := y + alpha * S * x */
+
+#define scf_st_prod _glp_scf_st_prod
+void scf_st_prod(SCF *scf, double y[/*1+nn*/], double a, const double
+ x[/*1+n0*/]);
+/* compute product y := y + alpha * S'* x */
+
+#define scf_a_solve _glp_scf_a_solve
+void scf_a_solve(SCF *scf, double x[/*1+n*/],
+ double w[/*1+n0+nn*/], double work1[/*1+max(n0,nn)*/],
+ double work2[/*1+n*/], double work3[/*1+n*/]);
+/* solve system A * x = b */
+
+#define scf_at_solve _glp_scf_at_solve
+void scf_at_solve(SCF *scf, double x[/*1+n*/],
+ double w[/*1+n0+nn*/], double work1[/*1+max(n0,nn)*/],
+ double work2[/*1+n*/], double work3[/*1+n*/]);
+/* solve system A'* x = b */
+
+#define scf_add_r_row _glp_scf_add_r_row
+void scf_add_r_row(SCF *scf, const double w[/*1+n0*/]);
+/* add new row to matrix R */
+
+#define scf_add_s_col _glp_scf_add_s_col
+void scf_add_s_col(SCF *scf, const double v[/*1+n0*/]);
+/* add new column to matrix S */
+
+#define scf_update_aug _glp_scf_update_aug
+int scf_update_aug(SCF *scf, double b[/*1+n0*/], double d[/*1+n0*/],
+ double f[/*1+nn*/], double g[/*1+nn*/], double h, int upd,
+ double w1[/*1+n0*/], double w2[/*1+n0*/], double w3[/*1+n0*/]);
+/* update factorization of augmented matrix */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/scfint.c b/test/monniaux/glpk-4.65/src/bflib/scfint.c
new file mode 100644
index 00000000..06aa8f7d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/scfint.c
@@ -0,0 +1,255 @@
+/* scfint.c (interface to Schur-complement-based factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "scfint.h"
+
+SCFINT *scfint_create(int type)
+{ /* create interface to SC-factorization */
+ SCFINT *fi;
+ fi = talloc(1, SCFINT);
+ memset(fi, 0, sizeof(SCFINT));
+ switch ((fi->scf.type = type))
+ { case 1:
+ fi->u.lufi = lufint_create();
+ break;
+ case 2:
+ fi->u.btfi = btfint_create();
+ break;
+ default:
+ xassert(type != type);
+ }
+ return fi;
+}
+
+int scfint_factorize(SCFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info)
+{ /* compute SC-factorization of specified matrix A */
+ int nn_max, old_n0_max, n0_max, k, ret;
+ xassert(n > 0);
+ fi->valid = 0;
+ /* get required value of nn_max */
+ nn_max = fi->nn_max;
+ if (nn_max == 0)
+ nn_max = 100;
+ xassert(nn_max > 0);
+ /* compute factorization of specified matrix A */
+ switch (fi->scf.type)
+ { case 1:
+ old_n0_max = fi->u.lufi->n_max;
+ fi->u.lufi->sva_n_max = 4 * n + 2 * nn_max;
+ ret = lufint_factorize(fi->u.lufi, n, col, info);
+ n0_max = fi->u.lufi->n_max;
+ fi->scf.sva = fi->u.lufi->sva;
+ fi->scf.a0.luf = fi->u.lufi->luf;
+ break;
+ case 2:
+ old_n0_max = fi->u.btfi->n_max;
+ fi->u.btfi->sva_n_max = 6 * n + 2 * nn_max;
+ ret = btfint_factorize(fi->u.btfi, n, col, info);
+ n0_max = fi->u.btfi->n_max;
+ fi->scf.sva = fi->u.btfi->sva;
+ fi->scf.a0.btf = fi->u.btfi->btf;
+ break;
+ default:
+ xassert(fi != fi);
+ }
+ /* allocate/reallocate arrays, if necessary */
+ if (old_n0_max < n0_max)
+ { if (fi->w1 != NULL)
+ tfree(fi->w1);
+ if (fi->w2 != NULL)
+ tfree(fi->w2);
+ if (fi->w3 != NULL)
+ tfree(fi->w3);
+ fi->w1 = talloc(1+n0_max, double);
+ fi->w2 = talloc(1+n0_max, double);
+ fi->w3 = talloc(1+n0_max, double);
+ }
+ if (fi->scf.nn_max != nn_max)
+ { if (fi->scf.ifu.f != NULL)
+ tfree(fi->scf.ifu.f);
+ if (fi->scf.ifu.u != NULL)
+ tfree(fi->scf.ifu.u);
+ fi->scf.ifu.f = talloc(nn_max * nn_max, double);
+ fi->scf.ifu.u = talloc(nn_max * nn_max, double);
+ }
+ if (old_n0_max < n0_max || fi->scf.nn_max != nn_max)
+ { if (fi->scf.pp_ind != NULL)
+ tfree(fi->scf.pp_ind);
+ if (fi->scf.pp_inv != NULL)
+ tfree(fi->scf.pp_inv);
+ if (fi->scf.qq_ind != NULL)
+ tfree(fi->scf.qq_ind);
+ if (fi->scf.qq_inv != NULL)
+ tfree(fi->scf.qq_inv);
+ if (fi->w4 != NULL)
+ tfree(fi->w4);
+ if (fi->w5 != NULL)
+ tfree(fi->w5);
+ fi->scf.pp_ind = talloc(1+n0_max+nn_max, int);
+ fi->scf.pp_inv = talloc(1+n0_max+nn_max, int);
+ fi->scf.qq_ind = talloc(1+n0_max+nn_max, int);
+ fi->scf.qq_inv = talloc(1+n0_max+nn_max, int);
+ fi->w4 = talloc(1+n0_max+nn_max, double);
+ fi->w5 = talloc(1+n0_max+nn_max, double);
+ }
+ /* initialize SC-factorization */
+ fi->scf.n = n;
+ fi->scf.n0 = n;
+ fi->scf.nn_max = nn_max;
+ fi->scf.nn = 0;
+ fi->scf.rr_ref = sva_alloc_vecs(fi->scf.sva, nn_max);
+ fi->scf.ss_ref = sva_alloc_vecs(fi->scf.sva, nn_max);
+ fi->scf.ifu.n_max = nn_max;
+ fi->scf.ifu.n = 0;
+ for (k = 1; k <= n; k++)
+ { fi->scf.pp_ind[k] = k;
+ fi->scf.pp_inv[k] = k;
+ fi->scf.qq_ind[k] = k;
+ fi->scf.qq_inv[k] = k;
+ }
+ /* set validation flag */
+ if (ret == 0)
+ fi->valid = 1;
+ return ret;
+}
+
+int scfint_update(SCFINT *fi, int upd, int j, int len, const int ind[],
+ const double val[])
+{ /* update SC-factorization after replacing j-th column of A */
+ int n = fi->scf.n;
+ int n0 = fi->scf.n0;
+ int nn = fi->scf.nn;
+ int *pp_ind = fi->scf.pp_ind;
+ int *qq_ind = fi->scf.qq_ind;
+ int *qq_inv = fi->scf.qq_inv;
+ double *bf = fi->w4;
+ double *dg = fi->w5;
+ int k, t, ret;
+ xassert(fi->valid);
+ xassert(0 <= n && n <= n0+nn);
+ /* (b, f) := inv(P) * (beta, 0) */
+ for (k = 1; k <= n0+nn; k++)
+ bf[k] = 0.0;
+ for (t = 1; t <= len; t++)
+ { k = ind[t];
+ xassert(1 <= k && k <= n);
+#if 1 /* FIXME: currently P = I */
+ xassert(pp_ind[k] == k);
+#endif
+ xassert(bf[k] == 0.0);
+ xassert(val[t] != 0.0);
+ bf[k] = val[t];
+ }
+ /* (d, g) := Q * (cj, 0) */
+ for (k = 1; k <= n0+nn; k++)
+ dg[k] = 0.0;
+ xassert(1 <= j && j <= n);
+ dg[fi->scf.qq_inv[j]] = 1;
+ /* update factorization of augmented matrix */
+ ret = scf_update_aug(&fi->scf, &bf[0], &dg[0], &bf[n0], &dg[n0],
+ 0.0, upd, fi->w1, fi->w2, fi->w3);
+ if (ret == 0)
+ { /* swap j-th and last columns of new matrix Q */
+ scf_swap_q_cols(j, n0+nn+1);
+ }
+ else
+ { /* updating failed */
+ fi->valid = 0;
+ }
+ return ret;
+}
+
+void scfint_ftran(SCFINT *fi, double x[])
+{ /* solve system A * x = b */
+ xassert(fi->valid);
+ scf_a_solve(&fi->scf, x, fi->w4, fi->w5, fi->w1, fi->w2);
+ return;
+}
+
+void scfint_btran(SCFINT *fi, double x[])
+{ /* solve system A'* x = b */
+ xassert(fi->valid);
+ scf_at_solve(&fi->scf, x, fi->w4, fi->w5, fi->w1, fi->w2);
+ return;
+}
+
+double scfint_estimate(SCFINT *fi)
+{ /* estimate 1-norm of inv(A) */
+ double norm;
+ xassert(fi->valid);
+ xassert(fi->scf.n == fi->scf.n0);
+ switch (fi->scf.type)
+ { case 1:
+ norm = luf_estimate_norm(fi->scf.a0.luf, fi->w1, fi->w2);
+ break;
+ case 2:
+ norm = btf_estimate_norm(fi->scf.a0.btf, fi->w1, fi->w2,
+ fi->w3, fi->w4);
+ break;
+ default:
+ xassert(fi != fi);
+ }
+ return norm;
+}
+
+void scfint_delete(SCFINT *fi)
+{ /* delete interface to SC-factorization */
+ switch (fi->scf.type)
+ { case 1:
+ lufint_delete(fi->u.lufi);
+ break;
+ case 2:
+ btfint_delete(fi->u.btfi);
+ break;
+ default:
+ xassert(fi != fi);
+ }
+ if (fi->scf.ifu.f != NULL)
+ tfree(fi->scf.ifu.f);
+ if (fi->scf.ifu.u != NULL)
+ tfree(fi->scf.ifu.u);
+ if (fi->scf.pp_ind != NULL)
+ tfree(fi->scf.pp_ind);
+ if (fi->scf.pp_inv != NULL)
+ tfree(fi->scf.pp_inv);
+ if (fi->scf.qq_ind != NULL)
+ tfree(fi->scf.qq_ind);
+ if (fi->scf.qq_inv != NULL)
+ tfree(fi->scf.qq_inv);
+ if (fi->w1 != NULL)
+ tfree(fi->w1);
+ if (fi->w2 != NULL)
+ tfree(fi->w2);
+ if (fi->w3 != NULL)
+ tfree(fi->w3);
+ if (fi->w4 != NULL)
+ tfree(fi->w4);
+ if (fi->w5 != NULL)
+ tfree(fi->w5);
+ tfree(fi);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/scfint.h b/test/monniaux/glpk-4.65/src/bflib/scfint.h
new file mode 100644
index 00000000..3e56355b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/scfint.h
@@ -0,0 +1,89 @@
+/* scfint.h (interface to Schur-complement-based factorization) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013-2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SCFINT_H
+#define SCFINT_H
+
+#include "scf.h"
+#include "lufint.h"
+#include "btfint.h"
+
+typedef struct SCFINT SCFINT;
+
+struct SCFINT
+{ /* interface to SC-factorization */
+ int valid;
+ /* factorization is valid only if this flag is set */
+ SCF scf;
+ /* Schur-complement based factorization */
+ union
+ { LUFINT *lufi; /* scf.type = 1 */
+ BTFINT *btfi; /* scf.type = 2 */
+ } u;
+ /* interface to factorize initial matrix A0 */
+ /*--------------------------------------------------------------*/
+ /* working arrays */
+ double *w1; /* double w1[1+n0_max]; */
+ double *w2; /* double w2[1+n0_max]; */
+ double *w3; /* double w3[1+n0_max]; */
+ double *w4; /* double w4[1+n0_max+nn_max]; */
+ double *w5; /* double w5[1+n0_max+nn_max]; */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ int nn_max;
+ /* required maximal number of updates */
+};
+
+#define scfint_create _glp_scfint_create
+SCFINT *scfint_create(int type);
+/* create interface to SC-factorization */
+
+#define scfint_factorize _glp_scfint_factorize
+int scfint_factorize(SCFINT *fi, int n, int (*col)(void *info, int j,
+ int ind[], double val[]), void *info);
+/* compute SC-factorization of specified matrix A */
+
+#define scfint_update _glp_scfint_update
+int scfint_update(SCFINT *fi, int upd, int j, int len, const int ind[],
+ const double val[]);
+/* update SC-factorization after replacing j-th column of A */
+
+#define scfint_ftran _glp_scfint_ftran
+void scfint_ftran(SCFINT *fi, double x[]);
+/* solve system A * x = b */
+
+#define scfint_btran _glp_scfint_btran
+void scfint_btran(SCFINT *fi, double x[]);
+/* solve system A'* x = b */
+
+#define scfint_estimate _glp_scfint_estimate
+double scfint_estimate(SCFINT *fi);
+/* estimate 1-norm of inv(A) */
+
+#define scfint_delete _glp_scfint_delete
+void scfint_delete(SCFINT *fi);
+/* delete interface to SC-factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/sgf.c b/test/monniaux/glpk-4.65/src/bflib/sgf.c
new file mode 100644
index 00000000..1c1f49a6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/sgf.c
@@ -0,0 +1,1443 @@
+/* sgf.c (sparse Gaussian factorizer) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "sgf.h"
+
+/***********************************************************************
+* sgf_reduce_nuc - initial reordering to minimize nucleus size
+*
+* On entry to this routine it is assumed that V = A and F = P = Q = I,
+* where A is the original matrix to be factorized. It is also assumed
+* that matrix V = A is stored in both row- and column-wise formats.
+*
+* This routine performs (implicit) non-symmetric permutations of rows
+* and columns of matrix U = P'* V * Q' to reduce it to the form:
+*
+* 1 k1 k2 n
+* 1 x x x x x x x x x x
+* . x x x x x x x x x
+* . . x x x x x x x x
+* k1 . . . * * * * x x x
+* . . . * * * * x x x
+* . . . * * * * x x x
+* k2 . . . * * * * x x x
+* . . . . . . . x x x
+* . . . . . . . . x x
+* n . . . . . . . . . x
+*
+* where non-zeros in rows and columns k1, k1+1, ..., k2 constitute so
+* called nucleus ('*'), whose size is minimized by the routine.
+*
+* The numbers k1 and k2 are returned by the routine on exit. Usually,
+* if the nucleus exists, 1 <= k1 < k2 <= n. However, if the resultant
+* matrix U is upper triangular (has no nucleus), k1 = n+1 and k2 = n.
+*
+* Note that the routines sgf_choose_pivot and sgf_eliminate perform
+* exactly the same transformations (by processing row and columns
+* singletons), so preliminary minimization of the nucleus may not be
+* used. However, processing row and column singletons by the routines
+* sgf_minimize_nuc and sgf_singl_phase is more efficient. */
+
+#if 1 /* 21/II-2016 */
+/* Normally this routine returns zero. If the matrix is structurally
+* singular, the routine returns non-zero. */
+#endif
+
+int sgf_reduce_nuc(LUF *luf, int *k1_, int *k2_, int cnt[/*1+n*/],
+ int list[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int i, ii, j, jj, k1, k2, ns, ptr, end;
+ /* initial nucleus is U = V = A */
+ k1 = 1, k2 = n;
+ /*--------------------------------------------------------------*/
+ /* process column singletons */
+ /*--------------------------------------------------------------*/
+ /* determine initial counts of columns of V and initialize list
+ * of active column singletons */
+ ns = 0; /* number of active column singletons */
+ for (j = 1; j <= n; j++)
+ { if ((cnt[j] = vc_len[j]) == 1)
+ list[++ns] = j;
+ }
+ /* process active column singletons */
+ while (ns > 0)
+ { /* column singleton is in j-th column of V */
+ j = list[ns--];
+#if 1 /* 21/II-2016 */
+ if (cnt[j] == 0)
+ { /* j-th column in the current nucleus is actually empty */
+ /* this happened because on a previous step in the nucleus
+ * there were two or more identical column singletons (that
+ * means structural singularity), so removing one of them
+ * from the nucleus made other columns empty */
+ return 1;
+ }
+#endif
+ /* find i-th row of V containing column singleton */
+ ptr = vc_ptr[j];
+ end = ptr + vc_len[j];
+ for (; pp_ind[i = sv_ind[ptr]] < k1; ptr++)
+ /* nop */;
+ xassert(ptr < end);
+ /* permute rows and columns of U to move column singleton to
+ * position u[k1,k1] */
+ ii = pp_ind[i];
+ luf_swap_u_rows(k1, ii);
+ jj = qq_inv[j];
+ luf_swap_u_cols(k1, jj);
+ /* nucleus size decreased */
+ k1++;
+ /* walk thru i-th row of V and decrease column counts; this
+ * may cause new column singletons to appear */
+ ptr = vr_ptr[i];
+ end = ptr + vr_len[i];
+ for (; ptr < end; ptr++)
+ { if (--(cnt[j = sv_ind[ptr]]) == 1)
+ list[++ns] = j;
+ }
+ }
+ /* nucleus begins at k1-th row/column of U */
+ if (k1 > n)
+ { /* U is upper triangular; no nucleus exist */
+ goto done;
+ }
+ /*--------------------------------------------------------------*/
+ /* process row singletons */
+ /*--------------------------------------------------------------*/
+ /* determine initial counts of rows of V and initialize list of
+ * active row singletons */
+ ns = 0; /* number of active row singletons */
+ for (i = 1; i <= n; i++)
+ { if (pp_ind[i] < k1)
+ { /* corresponding row of U is above its k1-th row; set its
+ * count to zero to prevent including it in active list */
+ cnt[i] = 0;
+ }
+ else if ((cnt[i] = vr_len[i]) == 1)
+ list[++ns] = i;
+ }
+ /* process active row singletons */
+ while (ns > 0)
+ { /* row singleton is in i-th row of V */
+ i = list[ns--];
+#if 1 /* 21/II-2016 */
+ if (cnt[i] == 0)
+ { /* i-th row in the current nucleus is actually empty */
+ /* (see comments above for similar case of empty column) */
+ return 2;
+ }
+#endif
+ /* find j-th column of V containing row singleton */
+ ptr = vr_ptr[i];
+ end = ptr + vr_len[i];
+ for (; qq_inv[j = sv_ind[ptr]] > k2; ptr++)
+ /* nop */;
+ xassert(ptr < end);
+ /* permute rows and columns of U to move row singleton to
+ * position u[k2,k2] */
+ ii = pp_ind[i];
+ luf_swap_u_rows(k2, ii);
+ jj = qq_inv[j];
+ luf_swap_u_cols(k2, jj);
+ /* nucleus size decreased */
+ k2--;
+ /* walk thru j-th column of V and decrease row counts; this
+ * may cause new row singletons to appear */
+ ptr = vc_ptr[j];
+ end = ptr + vc_len[j];
+ for (; ptr < end; ptr++)
+ { if (--(cnt[i = sv_ind[ptr]]) == 1)
+ list[++ns] = i;
+ }
+ }
+ /* nucleus ends at k2-th row/column of U */
+ xassert(k1 < k2);
+done: *k1_ = k1, *k2_ = k2;
+ return 0;
+}
+
+/***********************************************************************
+* sgf_singl_phase - compute LU-factorization (singleton phase)
+*
+* It is assumed that on entry to the routine L = P'* F * P = F = I
+* and matrix U = P'* V * Q' has the following structure (provided by
+* the routine sgf_reduce_nuc):
+*
+* 1 k1 k2 n
+* 1 a a a b b b b c c c
+* . a a b b b b c c c
+* . . a b b b b c c c
+* k1 . . . * * * * d d d
+* . . . * * * * d d d
+* . . . * * * * d d d
+* k2 . . . * * * * d d d
+* . . . . . . . e e e
+* . . . . . . . . e e
+* n . . . . . . . . . e
+*
+* First, the routine performs (implicit) symmetric permutations of
+* rows and columns of matrix U to place them in the following order:
+*
+* 1, 2, ..., k1-1; n, n-1, ..., k2+1; k1, k1+1, ..., k2
+*
+* This changes the structure of matrix U as follows:
+*
+* 1 k1 k2' n
+* 1 a a a c c c b b b b
+* . a a c c c b b b b
+* . . a c c c b b b b
+* k1 . . . e . . . . . .
+* . . . e e . . . . .
+* . . . e e e . . . .
+* k2'. . . d d d * * * *
+* . . . d d d * * * *
+* . . . d d d * * * *
+* n . . . d d d * * * *
+*
+* where k2' = n - k2 + k1.
+*
+* Then the routine performs elementary gaussian transformations to
+* eliminate subdiagonal elements in columns k1, ..., k2'-1 of U. The
+* effect is the same as if the routine sgf_eliminate would be called
+* for k = 1, ..., k2'-1 using diagonal elements u[k,k] as pivots.
+*
+* After elimination matrices L and U becomes the following:
+*
+* 1 k1 k2' n 1 k1 k2' n
+* 1 1 . . . . . . . . . 1 a a a c c c b b b b
+* . 1 . . . . . . . . . a a c c c b b b b
+* . . 1 . . . . . . . . . a c c c b b b b
+* k1 . . . 1 . . . . . . k1 . . . e . . . . . .
+* . . . e'1 . . . . . . . . . e . . . . .
+* . . . e'e'1 . . . . . . . . . e . . . .
+* k2'. . . d'd'd'1 . . . k2'. . . . . . * * * *
+* . . . d'd'd'. 1 . . . . . . . . * * * *
+* . . . d'd'd'. . 1 . . . . . . . * * * *
+* n . . . d'd'd'. . . 1 n . . . . . . * * * *
+*
+* matrix L matrix U
+*
+* where columns k1, ..., k2'-1 of L consist of subdiagonal elements
+* of initial matrix U divided by pivots u[k,k].
+*
+* On exit the routine returns k2', the elimination step number, from
+* which computing of the factorization should be continued. Note that
+* k2' = n+1 means that matrix U is already upper triangular. */
+
+int sgf_singl_phase(LUF *luf, int k1, int k2, int updat,
+ int ind[/*1+n*/], double val[/*1+n*/])
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int i, j, k, ptr, ptr1, end, len;
+ double piv;
+ /* (see routine sgf_reduce_nuc) */
+ xassert((1 <= k1 && k1 < k2 && k2 <= n)
+ || (k1 == n+1 && k2 == n));
+ /* perform symmetric permutations of rows/columns of U */
+ for (k = k1; k <= k2; k++)
+ pp_ind[pp_inv[k]] = qq_inv[qq_ind[k]] = k - k2 + n;
+ for (k = k2+1; k <= n; k++)
+ pp_ind[pp_inv[k]] = qq_inv[qq_ind[k]] = n - k + k1;
+ for (k = 1; k <= n; k++)
+ pp_inv[pp_ind[k]] = qq_ind[qq_inv[k]] = k;
+ /* determine k2' */
+ k2 = n - k2 + k1;
+ /* process rows and columns of V corresponding to rows and
+ * columns 1, ..., k1-1 of U */
+ for (k = 1; k < k1; k++)
+ { /* k-th row of U = i-th row of V */
+ i = pp_inv[k];
+ /* find pivot u[k,k] = v[i,j] in i-th row of V */
+ ptr = vr_ptr[i];
+ end = ptr + vr_len[i];
+ for (; qq_inv[sv_ind[ptr]] != k; ptr++)
+ /* nop */;
+ xassert(ptr < end);
+ /* store pivot */
+ vr_piv[i] = sv_val[ptr];
+ /* and remove it from i-th row of V */
+ sv_ind[ptr] = sv_ind[end-1];
+ sv_val[ptr] = sv_val[end-1];
+ vr_len[i]--;
+ /* clear column of V corresponding to k-th column of U */
+ vc_len[qq_ind[k]] = 0;
+ }
+ /* clear rows of V corresponding to rows k1, ..., k2'-1 of U */
+ for (k = k1; k < k2; k++)
+ vr_len[pp_inv[k]] = 0;
+ /* process rows and columns of V corresponding to rows and
+ * columns k2', ..., n of U */
+ for (k = k2; k <= n; k++)
+ { /* k-th row of U = i-th row of V */
+ i = pp_inv[k];
+ /* remove elements from i-th row of V that correspond to
+ * elements u[k,k1], ..., u[k,k2'-1] */
+ ptr = ptr1 = vr_ptr[i];
+ end = ptr + vr_len[i];
+ for (; ptr < end; ptr++)
+ { if (qq_inv[sv_ind[ptr]] >= k2)
+ { sv_ind[ptr1] = sv_ind[ptr];
+ sv_val[ptr1] = sv_val[ptr];
+ ptr1++;
+ }
+ }
+ vr_len[i] = ptr1 - vr_ptr[i];
+ /* k-th column of U = j-th column of V */
+ j = qq_ind[k];
+ /* remove elements from j-th column of V that correspond to
+ * elements u[1,k], ..., u[k1-1,k] */
+ ptr = ptr1 = vc_ptr[j];
+ end = ptr + vc_len[j];
+ for (; ptr < end; ptr++)
+ { if (pp_ind[sv_ind[ptr]] >= k2)
+ /* element value is not needed in this case */
+ sv_ind[ptr1++] = sv_ind[ptr];
+ }
+ vc_len[j] = ptr1 - vc_ptr[j];
+ }
+ /* process columns of V corresponding to columns k1, ..., k2'-1
+ * of U, build columns of F */
+ for (k = k1; k < k2; k++)
+ { /* k-th column of U = j-th column of V */
+ j = qq_ind[k];
+ /* remove elements from j-th column of V that correspond to
+ * pivot (diagonal) element u[k,k] and subdiagonal elements
+ * u[k+1,k], ..., u[n,k]; subdiagonal elements are stored for
+ * further addition to matrix F */
+ len = 0;
+ piv = 0.0;
+ ptr = vc_ptr[j];
+ end = ptr + vc_len[j];
+ for (; ptr < end; ptr++)
+ { i = sv_ind[ptr]; /* v[i,j] */
+ if (pp_ind[i] == k)
+ { /* store pivot v[i,j] = u[k,k] */
+ piv = vr_piv[i] = sv_val[ptr];
+ }
+ else if (pp_ind[i] > k)
+ { /* store subdiagonal element v[i,j] = u[i',k] */
+ len++;
+ ind[len] = i;
+ val[len] = sv_val[ptr];
+ }
+ }
+ /* clear j-th column of V = k-th column of U */
+ vc_len[j] = 0;
+ /* build k-th column of L = j-th column of F */
+ j = pp_inv[k];
+ xassert(piv != 0.0);
+ if (len > 0)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, fc_ref-1+j, len);
+ for (ptr = fc_ptr[j], ptr1 = 1; ptr1 <= len; ptr++, ptr1++)
+ { sv_ind[ptr] = ind[ptr1];
+ sv_val[ptr] = val[ptr1] / piv;
+ }
+ fc_len[j] = len;
+ }
+ }
+ /* if it is not planned to update matrix V, relocate all its
+ * non-active rows corresponding to rows 1, ..., k2'-1 of U to
+ * the right (static) part of SVA */
+ if (!updat)
+ { for (k = 1; k < k2; k++)
+ { i = pp_inv[k];
+ len = vr_len[i];
+ if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_make_static(sva, vr_ref-1+i);
+ }
+ }
+ /* elimination steps 1, ..., k2'-1 have been performed */
+ return k2;
+}
+
+/***********************************************************************
+* sgf_choose_pivot - choose pivot element v[p,q]
+*
+* This routine chooses pivot element v[p,q], k <= p, q <= n, in the
+* active submatrix of matrix V = P * U * Q, where k is the number of
+* current elimination step, 1 <= k <= n.
+*
+* It is assumed that on entry to the routine matrix U = P'* V * Q' has
+* the following partially triangularized form:
+*
+* 1 k n
+* 1 x x x x x x x x x x
+* . x x x x x x x x x
+* . . x x x x x x x x
+* . . . x x x x x x x
+* k . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* n . . . . * * * * * *
+*
+* where rows and columns k, k+1, ..., n belong to the active submatrix
+* (its elements are marked by '*').
+*
+* Since the matrix U is not stored, the routine works with the matrix
+* V = P * U * Q. It is assumed that the row-wise representation
+* corresponds to the matrix V, but the column-wise representation
+* corresponds to the active submatrix of the matrix V, i.e. elements,
+* which are not in the active submatrix, are not included in column
+* vectors. It is also assumed that each active row of the matrix V is
+* in the set R[len], where len is the number of non-zeros in the row,
+* and each active column of the matrix V is in the set C[len], where
+* len is the number of non-zeros in the column (in the latter case
+* only elements of the active submatrix are counted; such elements are
+* marked by '*' on the figure above).
+*
+* For the reason of numerical stability the routine applies so called
+* threshold pivoting proposed by J.Reid. It is assumed that an element
+* v[i,j] can be selected as a pivot candidate if it is not very small
+* (in magnitude) among other elements in the same row, i.e. if it
+* satisfies to the stability condition |v[i,j]| >= tol * max|v[i,*]|,
+* where 0 < tol < 1 is a given tolerance.
+*
+* In order to keep sparsity of the matrix V the routine uses Markowitz
+* strategy, trying to choose such element v[p,q], which satisfies to
+* the stability condition (see above) and has smallest Markowitz cost
+* (nr[p]-1) * (nc[q]-1), where nr[p] and nc[q] are, resp., numbers of
+* non-zeros in p-th row and q-th column of the active submatrix.
+*
+* In order to reduce the search, i.e. not to walk through all elements
+* of the active submatrix, the routine uses a technique proposed by
+* I.Duff. This technique is based on using the sets R[len] and C[len]
+* of active rows and columns.
+*
+* If the pivot element v[p,q] has been chosen, the routine stores its
+* indices to locations *p and *q and returns zero. Otherwise, non-zero
+* is returned. */
+
+int sgf_choose_pivot(SGF *sgf, int *p_, int *q_)
+{ LUF *luf = sgf->luf;
+ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *rs_head = sgf->rs_head;
+ int *rs_next = sgf->rs_next;
+ int *cs_head = sgf->cs_head;
+ int *cs_prev = sgf->cs_prev;
+ int *cs_next = sgf->cs_next;
+ double *vr_max = sgf->vr_max;
+ double piv_tol = sgf->piv_tol;
+ int piv_lim = sgf->piv_lim;
+ int suhl = sgf->suhl;
+ int i, i_ptr, i_end, j, j_ptr, j_end, len, min_i, min_j, min_len,
+ ncand, next_j, p, q;
+ double best, big, cost, temp;
+ /* no pivot candidate has been chosen so far */
+ p = q = 0, best = DBL_MAX, ncand = 0;
+ /* if the active submatrix contains a column having the only
+ * non-zero element (column singleton), choose it as the pivot */
+ j = cs_head[1];
+ if (j != 0)
+ { xassert(vc_len[j] == 1);
+ p = sv_ind[vc_ptr[j]], q = j;
+ goto done;
+ }
+ /* if the active submatrix contains a row having the only
+ * non-zero element (row singleton), choose it as the pivot */
+ i = rs_head[1];
+ if (i != 0)
+ { xassert(vr_len[i] == 1);
+ p = i, q = sv_ind[vr_ptr[i]];
+ goto done;
+ }
+ /* the active submatrix contains no singletons; walk thru its
+ * other non-empty rows and columns */
+ for (len = 2; len <= n; len++)
+ { /* consider active columns containing len non-zeros */
+ for (j = cs_head[len]; j != 0; j = next_j)
+ { /* save the number of next column of the same length */
+ next_j = cs_next[j];
+ /* find an element in j-th column, which is placed in the
+ * row with minimal number of non-zeros and satisfies to
+ * the stability condition (such element may not exist) */
+ min_i = min_j = 0, min_len = INT_MAX;
+ for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ j_ptr < j_end; j_ptr++)
+ { /* get row index of v[i,j] */
+ i = sv_ind[j_ptr];
+ /* if i-th row is not shorter, skip v[i,j] */
+ if (vr_len[i] >= min_len)
+ continue;
+ /* big := max|v[i,*]| */
+ if ((big = vr_max[i]) < 0.0)
+ { /* largest magnitude is unknown; compute it */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { if ((temp = sv_val[i_ptr]) < 0.0)
+ temp = -temp;
+ if (big < temp)
+ big = temp;
+ }
+ xassert(big > 0.0);
+ vr_max[i] = big;
+ }
+ /* find v[i,j] in i-th row */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ sv_ind[i_ptr] != j; i_ptr++)
+ /* nop */;
+ xassert(i_ptr < i_end);
+ /* if |v[i,j]| < piv_tol * max|v[i,*]|, skip v[i,j] */
+ if ((temp = sv_val[i_ptr]) < 0.0)
+ temp = -temp;
+ if (temp < piv_tol * big)
+ continue;
+ /* v[i,j] is a better candidate */
+ min_i = i, min_j = j, min_len = vr_len[i];
+ /* if Markowitz cost of v[i,j] is not greater than
+ * (len-1)**2, v[i,j] can be chosen as the pivot right
+ * now; this heuristic reduces the search and works well
+ * in many cases */
+ if (min_len <= len)
+ { p = min_i, q = min_j;
+ goto done;
+ }
+ }
+ /* j-th column has been scanned */
+ if (min_i != 0)
+ { /* element v[min_i,min_j] is a next pivot candidate */
+ ncand++;
+ /* compute its Markowitz cost */
+ cost = (double)(min_len - 1) * (double)(len - 1);
+ /* if this element is better, choose it as the pivot */
+ if (cost < best)
+ p = min_i, q = min_j, best = cost;
+ /* if piv_lim candidates were considered, terminate
+ * the search, because it is doubtful that a much better
+ * candidate will be found */
+ if (ncand == piv_lim)
+ goto done;
+ }
+ else if (suhl)
+ { /* j-th column has no eligible elements that satisfy to
+ * the stability criterion; Uwe Suhl suggests to exclude
+ * such column from further considerations until it
+ * becomes a column singleton; in hard cases this may
+ * significantly reduce the time needed to choose the
+ * pivot element */
+ sgf_deactivate_col(j);
+ cs_prev[j] = cs_next[j] = j;
+ }
+ }
+ /* consider active rows containing len non-zeros */
+ for (i = rs_head[len]; i != 0; i = rs_next[i])
+ { /* big := max|v[i,*]| */
+ if ((big = vr_max[i]) < 0.0)
+ { /* largest magnitude is unknown; compute it */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { if ((temp = sv_val[i_ptr]) < 0.0)
+ temp = -temp;
+ if (big < temp)
+ big = temp;
+ }
+ xassert(big > 0.0);
+ vr_max[i] = big;
+ }
+ /* find an element in i-th row, which is placed in the
+ * column with minimal number of non-zeros and satisfies to
+ * the stability condition (such element always exists) */
+ min_i = min_j = 0, min_len = INT_MAX;
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { /* get column index of v[i,j] */
+ j = sv_ind[i_ptr];
+ /* if j-th column is not shorter, skip v[i,j] */
+ if (vc_len[j] >= min_len)
+ continue;
+ /* if |v[i,j]| < piv_tol * max|v[i,*]|, skip v[i,j] */
+ if ((temp = sv_val[i_ptr]) < 0.0)
+ temp = -temp;
+ if (temp < piv_tol * big)
+ continue;
+ /* v[i,j] is a better candidate */
+ min_i = i, min_j = j, min_len = vc_len[j];
+ /* if Markowitz cost of v[i,j] is not greater than
+ * (len-1)**2, v[i,j] can be chosen as the pivot right
+ * now; this heuristic reduces the search and works well
+ * in many cases */
+ if (min_len <= len)
+ { p = min_i, q = min_j;
+ goto done;
+ }
+ }
+ /* i-th row has been scanned */
+ if (min_i != 0)
+ { /* element v[min_i,min_j] is a next pivot candidate */
+ ncand++;
+ /* compute its Markowitz cost */
+ cost = (double)(len - 1) * (double)(min_len - 1);
+ /* if this element is better, choose it as the pivot */
+ if (cost < best)
+ p = min_i, q = min_j, best = cost;
+ /* if piv_lim candidates were considered, terminate
+ * the search, because it is doubtful that a much better
+ * candidate will be found */
+ if (ncand == piv_lim)
+ goto done;
+ }
+ else
+ { /* this can never be */
+ xassert(min_i != min_i);
+ }
+ }
+ }
+done: /* report the pivot to the factorization routine */
+ *p_ = p, *q_ = q;
+ return (p == 0);
+}
+
+/***********************************************************************
+* sgf_eliminate - perform gaussian elimination
+*
+* This routine performs elementary gaussian transformations in order
+* to eliminate subdiagonal elements in k-th column of matrix
+* U = P'* V * Q' using pivot element u[k,k], where k is the number of
+* current elimination step, 1 <= k <= n.
+*
+* The parameters p and q specify, resp., row and column indices of the
+* pivot element v[p,q] = u[k,k].
+*
+* On entry the routine assumes that partially triangularized matrices
+* L = P'* F * P and U = P'* V * Q' have the following structure:
+*
+* 1 k n 1 k n
+* 1 1 . . . . . . . . . 1 x x x x x x x x x x
+* x 1 . . . . . . . . . x x x x x x x x x
+* x x 1 . . . . . . . . . x x x x x x x x
+* x x x 1 . . . . . . . . . x x x x x x x
+* k x x x x 1 . . . . . k . . . . * * * * * *
+* x x x x _ 1 . . . . . . . . # * * * * *
+* x x x x _ . 1 . . . . . . . # * * * * *
+* x x x x _ . . 1 . . . . . . # * * * * *
+* x x x x _ . . . 1 . . . . . # * * * * *
+* n x x x x _ . . . . 1 n . . . . # * * * * *
+*
+* matrix L matrix U
+*
+* where rows and columns k, k+1, ..., n of matrix U constitute the
+* active submatrix. Elements to be eliminated are marked by '#', and
+* other elements of the active submatrix are marked by '*'. May note
+* that each eliminated non-zero element u[i,k] of matrix U gives
+* corresponding non-zero element l[i,k] of matrix L (marked by '_').
+*
+* Actually all operations are performed on matrix V. It is assumed
+* that the row-wise representation corresponds to matrix V, but the
+* column-wise representation corresponds to the active submatrix of
+* matrix V (or, more precisely, to its pattern, because only row
+* indices for columns of the active submatrix are used on this stage).
+*
+* Let u[k,k] = v[p,q] be the pivot. In order to eliminate subdiagonal
+* elements u[i',k] = v[i,q], i'= k+1, k+2, ..., n, the routine applies
+* the following elementary gaussian transformations:
+*
+* (i-th row of V) := (i-th row of V) - f[i,p] * (p-th row of V),
+*
+* where f[i,p] = v[i,q] / v[p,q] is a gaussian multiplier stored to
+* p-th column of matrix F to keep the main equality A = F * V
+* (corresponding elements l[i',k] of matrix L are marked by '_' on the
+* figure above).
+*
+* NOTE: On entry to the routine the working arrays flag and work
+* should contain zeros. This status is retained by the routine
+* on exit. */
+
+int sgf_eliminate(SGF *sgf, int p, int q)
+{ LUF *luf = sgf->luf;
+ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int *vr_cap = &sva->cap[vr_ref-1];
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_ptr = &sva->ptr[vc_ref-1];
+ int *vc_len = &sva->len[vc_ref-1];
+ int *vc_cap = &sva->cap[vc_ref-1];
+ int *rs_head = sgf->rs_head;
+ int *rs_prev = sgf->rs_prev;
+ int *rs_next = sgf->rs_next;
+ int *cs_head = sgf->cs_head;
+ int *cs_prev = sgf->cs_prev;
+ int *cs_next = sgf->cs_next;
+ double *vr_max = sgf->vr_max;
+ char *flag = sgf->flag;
+ double *work = sgf->work;
+ double eps_tol = sgf->eps_tol;
+ int nnz_diff = 0;
+ int fill, i, i_ptr, i_end, j, j_ptr, j_end, ptr, len, loc, loc1;
+ double vpq, fip, vij;
+ xassert(1 <= p && p <= n);
+ xassert(1 <= q && q <= n);
+ /* remove p-th row from the active set; this row will never
+ * return there */
+ sgf_deactivate_row(p);
+ /* process p-th (pivot) row */
+ ptr = 0;
+ for (i_end = (i_ptr = vr_ptr[p]) + vr_len[p];
+ i_ptr < i_end; i_ptr++)
+ { /* get column index of v[p,j] */
+ j = sv_ind[i_ptr];
+ if (j == q)
+ { /* save pointer to pivot v[p,q] */
+ ptr = i_ptr;
+ }
+ else
+ { /* store v[p,j], j != q, to working array */
+ flag[j] = 1;
+ work[j] = sv_val[i_ptr];
+ }
+ /* remove j-th column from the active set; q-th column will
+ * never return there while other columns will return to the
+ * active set with new length */
+ if (cs_next[j] == j)
+ { /* j-th column was marked by the pivoting routine according
+ * to Uwe Suhl's suggestion and is already inactive */
+ xassert(cs_prev[j] == j);
+ }
+ else
+ sgf_deactivate_col(j);
+ nnz_diff -= vc_len[j];
+ /* find and remove v[p,j] from j-th column */
+ for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ sv_ind[j_ptr] != p; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ sv_ind[j_ptr] = sv_ind[j_end-1];
+ vc_len[j]--;
+ }
+ /* save pivot v[p,q] and remove it from p-th row */
+ xassert(ptr > 0);
+ vpq = vr_piv[p] = sv_val[ptr];
+ sv_ind[ptr] = sv_ind[i_end-1];
+ sv_val[ptr] = sv_val[i_end-1];
+ vr_len[p]--;
+ /* if it is not planned to update matrix V, relocate p-th row to
+ * the right (static) part of SVA */
+ if (!sgf->updat)
+ { len = vr_len[p];
+ if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_make_static(sva, vr_ref-1+p);
+ }
+ /* copy the pattern (row indices) of q-th column of the active
+ * submatrix (from which v[p,q] has been just removed) to p-th
+ * column of matrix F (without unity diagonal element) */
+ len = vc_len[q];
+ if (len > 0)
+ { if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_reserve_cap(sva, fc_ref-1+p, len);
+ memcpy(&sv_ind[fc_ptr[p]], &sv_ind[vc_ptr[q]],
+ len * sizeof(int));
+ fc_len[p] = len;
+ }
+ /* make q-th column of the active submatrix empty */
+ vc_len[q] = 0;
+ /* transform non-pivot rows of the active submatrix */
+ for (loc = fc_len[p]-1; loc >= 0; loc--)
+ { /* get row index of v[i,q] = row index of f[i,p] */
+ i = sv_ind[fc_ptr[p] + loc];
+ xassert(i != p); /* v[p,q] was removed */
+ /* remove i-th row from the active set; this row will return
+ * there with new length */
+ sgf_deactivate_row(i);
+ /* find v[i,q] in i-th row */
+ for (i_end = (i_ptr = vr_ptr[i]) + vr_len[i];
+ sv_ind[i_ptr] != q; i_ptr++)
+ /* nop */;
+ xassert(i_ptr < i_end);
+ /* compute gaussian multiplier f[i,p] = v[i,q] / v[p,q] */
+ fip = sv_val[fc_ptr[p] + loc] = sv_val[i_ptr] / vpq;
+ /* remove v[i,q] from i-th row */
+ sv_ind[i_ptr] = sv_ind[i_end-1];
+ sv_val[i_ptr] = sv_val[i_end-1];
+ vr_len[i]--;
+ /* perform elementary gaussian transformation:
+ * (i-th row) := (i-th row) - f[i,p] * (p-th row)
+ * note that p-th row of V, which is in the working array,
+ * doesn't contain pivot v[p,q], and i-th row of V doesn't
+ * contain v[i,q] to be eliminated */
+ /* walk thru i-th row and transform existing elements */
+ fill = vr_len[p];
+ for (i_end = (i_ptr = ptr = vr_ptr[i]) + vr_len[i];
+ i_ptr < i_end; i_ptr++)
+ { /* get column index and value of v[i,j] */
+ j = sv_ind[i_ptr];
+ vij = sv_val[i_ptr];
+ if (flag[j])
+ { /* v[p,j] != 0 */
+ flag[j] = 0, fill--;
+ /* v[i,j] := v[i,j] - f[i,p] * v[p,j] */
+ vij -= fip * work[j];
+ if (-eps_tol < vij && vij < +eps_tol)
+ { /* new v[i,j] is close to zero; remove it from the
+ * active submatrix, i.e. replace it by exact zero */
+ /* find and remove v[i,j] from j-th column */
+ for (j_end = (j_ptr = vc_ptr[j]) + vc_len[j];
+ sv_ind[j_ptr] != i; j_ptr++)
+ /* nop */;
+ xassert(j_ptr < j_end);
+ sv_ind[j_ptr] = sv_ind[j_end-1];
+ vc_len[j]--;
+ continue;
+ }
+ }
+ /* keep new v[i,j] in i-th row */
+ sv_ind[ptr] = j;
+ sv_val[ptr] = vij;
+ ptr++;
+ }
+ /* (new length of i-th row may decrease because of numerical
+ * cancellation) */
+ vr_len[i] = len = ptr - vr_ptr[i];
+ /* now flag[*] is the pattern of the set v[p,*] \ v[i,*], and
+ * fill is the number of non-zeros in this set */
+ if (fill == 0)
+ { /* no fill-in occurs */
+ /* walk thru p-th row and restore the column flags */
+ for (i_end = (i_ptr = vr_ptr[p]) + vr_len[p];
+ i_ptr < i_end; i_ptr++)
+ flag[sv_ind[i_ptr]] = 1; /* v[p,j] != 0 */
+ goto skip;
+ }
+ /* up to fill new non-zero elements may appear in i-th row due
+ * to fill-in; reserve locations for these elements (note that
+ * actual length of i-th row is currently stored in len) */
+ if (vr_cap[i] < len + fill)
+ { if (sva->r_ptr - sva->m_ptr < len + fill)
+ { sva_more_space(sva, len + fill);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vr_ref-1+i, len + fill, 0);
+ }
+ vr_len[i] += fill;
+ /* walk thru p-th row and add new elements to i-th row */
+ for (loc1 = vr_len[p]-1; loc1 >= 0; loc1--)
+ { /* get column index of v[p,j] */
+ j = sv_ind[vr_ptr[p] + loc1];
+ if (!flag[j])
+ { /* restore j-th column flag */
+ flag[j] = 1;
+ /* v[i,j] was computed earlier on transforming existing
+ * elements of i-th row */
+ continue;
+ }
+ /* v[i,j] := 0 - f[i,p] * v[p,j] */
+ vij = - fip * work[j];
+ if (-eps_tol < vij && vij < +eps_tol)
+ { /* new v[i,j] is close to zero; do not add it to the
+ * active submatrix, i.e. replace it by exact zero */
+ continue;
+ }
+ /* add new v[i,j] to i-th row */
+ sv_ind[ptr = vr_ptr[i] + (len++)] = j;
+ sv_val[ptr] = vij;
+ /* add new v[i,j] to j-th column */
+ if (vc_cap[j] == vc_len[j])
+ { /* we reserve extra locations in j-th column to reduce
+ * further relocations of that column */
+#if 1 /* FIXME */
+ /* use control parameter to specify the number of extra
+ * locations reserved */
+ int need = vc_len[j] + 10;
+#endif
+ if (sva->r_ptr - sva->m_ptr < need)
+ { sva_more_space(sva, need);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_enlarge_cap(sva, vc_ref-1+j, need, 1);
+ }
+ sv_ind[vc_ptr[j] + (vc_len[j]++)] = i;
+ }
+ /* set final length of i-th row just transformed */
+ xassert(len <= vr_len[i]);
+ vr_len[i] = len;
+skip: /* return i-th row to the active set with new length */
+ sgf_activate_row(i);
+ /* since i-th row has been changed, largest magnitude of its
+ * elements becomes unknown */
+ vr_max[i] = -1.0;
+ }
+ /* walk thru p-th (pivot) row */
+ for (i_end = (i_ptr = vr_ptr[p]) + vr_len[p];
+ i_ptr < i_end; i_ptr++)
+ { /* get column index of v[p,j] */
+ j = sv_ind[i_ptr];
+ xassert(j != q); /* v[p,q] was removed */
+ /* return j-th column to the active set with new length */
+ if (cs_next[j] == j && vc_len[j] != 1)
+ { /* j-th column was marked by the pivoting routine and it is
+ * still not a column singleton, so leave it incative */
+ xassert(cs_prev[j] == j);
+ }
+ else
+ sgf_activate_col(j);
+ nnz_diff += vc_len[j];
+ /* restore zero content of the working arrays */
+ flag[j] = 0;
+ work[j] = 0.0;
+ }
+ /* return the difference between the numbers of non-zeros in the
+ * active submatrix on entry and on exit, resp. */
+ return nnz_diff;
+}
+
+/***********************************************************************
+* sgf_dense_lu - compute dense LU-factorization with full pivoting
+*
+* This routine performs Gaussian elimination with full pivoting to
+* compute dense LU-factorization of the specified matrix A of order n
+* in the form:
+*
+* A = P * L * U * Q, (1)
+*
+* where L is lower triangular matrix with unit diagonal, U is upper
+* triangular matrix, P and Q are permutation matrices.
+*
+* On entry to the routine elements of matrix A = (a[i,j]) should be
+* placed in the array elements a[0], ..., a[n^2-1] in dense row-wise
+* format. On exit from the routine matrix A is replaced by factors L
+* and U as follows:
+*
+* u[1,1] u[1,2] ... u[1,n-1] u[1,n]
+* l[2,1] u[2,2] ... u[2,n-1] u[2,n]
+* . . . . . . . . . . . . . .
+* l[n-1,1] l[n-1,2] u[n-1,n-1] u[n-1,n]
+* l[n,1] l[n,2] ... l[n,n-1] u[n,n]
+*
+* The unit diagonal elements of L are not stored.
+*
+* Information on permutations of rows and columns of active submatrix
+* during factorization is accumulated by the routine as follows. Every
+* time the routine permutes rows i and i' or columns j and j', it also
+* permutes elements r[i-1] and r[i'-1] or c[j-1] and c[j'-1], resp.
+* Thus, on entry to the routine elements r[0], r[1], ..., r[n-1] and
+* c[0], c[1], ..., c[n-1] should be initialized by some integers that
+* identify rows and columns of the original matrix A.
+*
+* If the factorization has been successfully computed, the routine
+* returns zero. Otherwise, if on k-th elimination step, 1 <= k <= n,
+* all elements of the active submatrix are close to zero, the routine
+* returns k, in which case a partial factorization is stored in the
+* array a. */
+
+int sgf_dense_lu(int n, double a_[], int r[], int c[], double eps)
+{ /* non-optimized version */
+ int i, j, k, p, q, ref;
+ double akk, big, temp;
+# define a(i,j) a_[(i)*n+(j)]
+ /* initially U = A, L = P = Q = I */
+ /* main elimination loop */
+ for (k = 0; k < n; k++)
+ { /* choose pivot u[p,q], k <= p, q <= n */
+ p = q = -1, big = eps;
+ for (i = k; i < n; i++)
+ { for (j = k; j < n; j++)
+ { /* temp = |u[i,j]| */
+ if ((temp = a(i,j)) < 0.0)
+ temp = -temp;
+ if (big < temp)
+ p = i, q = j, big = temp;
+ }
+ }
+ if (p < 0)
+ { /* k-th elimination step failed */
+ return k+1;
+ }
+ /* permute rows k and p */
+ if (k != p)
+ { for (j = 0; j < n; j++)
+ temp = a(k,j), a(k,j) = a(p,j), a(p,j) = temp;
+ ref = r[k], r[k] = r[p], r[p] = ref;
+ }
+ /* permute columns k and q */
+ if (k != q)
+ { for (i = 0; i < n; i++)
+ temp = a(i,k), a(i,k) = a(i,q), a(i,q) = temp;
+ ref = c[k], c[k] = c[q], c[q] = ref;
+ }
+ /* now pivot is in position u[k,k] */
+ akk = a(k,k);
+ /* eliminate subdiagonal elements u[k+1,k], ..., u[n,k] */
+ for (i = k+1; i < n; i++)
+ { if (a(i,k) != 0.0)
+ { /* gaussian multiplier l[i,k] := u[i,k] / u[k,k] */
+ temp = (a(i,k) /= akk);
+ /* (i-th row) := (i-th row) - l[i,k] * (k-th row) */
+ for (j = k+1; j < n; j++)
+ a(i,j) -= temp * a(k,j);
+ }
+ }
+ }
+# undef a
+ return 0;
+}
+
+/***********************************************************************
+* sgf_dense_phase - compute LU-factorization (dense phase)
+*
+* This routine performs dense phase of computing LU-factorization.
+*
+* The aim is two-fold. First, the main factorization routine switches
+* to dense phase when the active submatrix is relatively dense, so
+* using dense format allows significantly reduces overheads needed to
+* maintain sparse data structures. And second, that is more important,
+* on dense phase full pivoting is used (rather than partial pivoting)
+* that allows improving numerical stability, since round-off errors
+* tend to increase on last steps of the elimination process.
+*
+* On entry the routine assumes that elimination steps 1, 2, ..., k-1
+* have been performed, so partially transformed matrices L = P'* F * P
+* and U = P'* V * Q' have the following structure:
+*
+* 1 k n 1 k n
+* 1 1 . . . . . . . . . 1 x x x x x x x x x x
+* x 1 . . . . . . . . . x x x x x x x x x
+* x x 1 . . . . . . . . . x x x x x x x x
+* x x x 1 . . . . . . . . . x x x x x x x
+* k x x x x 1 . . . . . k . . . . * * * * * *
+* x x x x . 1 . . . . . . . . * * * * * *
+* x x x x . . 1 . . . . . . . * * * * * *
+* x x x x . . . 1 . . . . . . * * * * * *
+* x x x x . . . . 1 . . . . . * * * * * *
+* n x x x x . . . . . 1 n . . . . * * * * * *
+*
+* matrix L matrix U
+*
+* where rows and columns k, k+1, ..., n of matrix U constitute the
+* active submatrix A~, whose elements are marked by '*'.
+*
+* The routine copies the active submatrix A~ to a working array in
+* dense format, compute dense factorization A~ = P~* L~* U~* Q~ using
+* full pivoting, and then copies non-zero elements of factors L~ and
+* U~ back to factors L and U (more precisely, to factors F and V).
+*
+* If the factorization has been successfully computed, the routine
+* returns zero. Otherwise, if on k-th elimination step, 1 <= k <= n,
+* all elements of the active submatrix are close to zero, the routine
+* returns k (information on linearly dependent rows/columns in this
+* case is provided by matrices P and Q). */
+
+int sgf_dense_phase(LUF *luf, int k, int updat)
+{ int n = luf->n;
+ SVA *sva = luf->sva;
+ int *sv_ind = sva->ind;
+ double *sv_val = sva->val;
+ int fc_ref = luf->fc_ref;
+ int *fc_ptr = &sva->ptr[fc_ref-1];
+ int *fc_len = &sva->len[fc_ref-1];
+ int *fc_cap = &sva->cap[fc_ref-1];
+ int vr_ref = luf->vr_ref;
+ int *vr_ptr = &sva->ptr[vr_ref-1];
+ int *vr_len = &sva->len[vr_ref-1];
+ int *vr_cap = &sva->cap[vr_ref-1];
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_inv = luf->pp_inv;
+ int *pp_ind = luf->pp_ind;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int a_end, a_ptr, end, i, ia, ii, j, ja, jj, ka, len, na, ne,
+ need, ptr;
+ double *a_;
+ xassert(1 <= k && k <= n);
+ /* active columns of V are not longer needed; make them empty */
+ for (jj = k; jj <= n; jj++)
+ { /* jj is number of active column of U = P'* V * Q' */
+ vc_len[qq_ind[jj]] = 0;
+ }
+ /* determine order of active submatrix A~ of matrix U */
+ na = n - k + 1;
+ xassert(1 <= na && na <= n);
+ /* determine number of elements in dense triangular factor (L~ or
+ * U~), except diagonal elements */
+ ne = na * (na - 1) / 2;
+ /* we allocate active submatrix A~ in free (middle) part of SVA;
+ * to avoid defragmentation that could destroy A~ we also should
+ * reserve ne locations to build rows of V from rows of U~ and ne
+ * locations to build columns of F from columns of L~ */
+ need = na * na + ne + ne;
+ if (sva->r_ptr - sva->m_ptr < need)
+ { sva_more_space(sva, need);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ /* free (middle) part of SVA is structured as follows:
+ * end of left (dynamic) part
+ * ne free locations for new rows of V
+ * na free locations for active submatrix A~
+ * unused locations, if any
+ * ne free locations for new columns of F
+ * beginning of right (static) part */
+ a_ptr = sva->m_ptr + ne;
+ a_end = a_ptr + na * na;
+ /* copy active submatrix A~ from matrix V to working array in
+ * dense row-wise format */
+ a_ = &sva->val[a_ptr];
+# define a(ia, ja) a_[((ia) - 1) * na + ((ja) - 1)]
+ for (ia = 1; ia <= na; ia++)
+ { /* clear ia-th row of A~ */
+ for (ja = 1; ja <= na; ja++)
+ a(ia, ja) = 0.0;
+ /* ia-th row of A~ = (k-1+ia)-th row of U = i-th row of V */
+ i = pp_inv[k-1+ia];
+ ptr = vr_ptr[i];
+ end = ptr + vr_len[i];
+ for (; ptr < end; ptr++)
+ a(ia, qq_inv[sv_ind[ptr]]-k+1) = sv_val[ptr];
+ /* i-th row of V is no longer needed; make it empty */
+ vr_len[i] = 0;
+ }
+ /* compute dense factorization A~ = P~* L~* U~* Q~ */
+#if 1 /* FIXME: epsilon tolerance */
+ ka = sgf_dense_lu(na, &a(1, 1), &pp_inv[k], &qq_ind[k], 1e-20);
+#endif
+ /* rows of U with numbers pp_inv[k, k+1, ..., n] were permuted
+ * due to row permutations of A~; update matrix P using P~ */
+ for (ii = k; ii <= n; ii++)
+ pp_ind[pp_inv[ii]] = ii;
+ /* columns of U with numbers qq_ind[k, k+1, ..., n] were permuted
+ * due to column permutations of A~; update matrix Q using Q~ */
+ for (jj = k; jj <= n; jj++)
+ qq_inv[qq_ind[jj]] = jj;
+ /* check if dense factorization is complete */
+ if (ka != 0)
+ { /* A~ is singular to working precision */
+ /* information on linearly dependent rows/columns is provided
+ * by matrices P and Q */
+ xassert(1 <= ka && ka <= na);
+ return k - 1 + ka;
+ }
+ /* build new rows of V from rows of U~ */
+ for (ia = 1; ia <= na; ia++)
+ { /* ia-th row of U~ = (k-1+ia)-th row of U = i-th row of V */
+ i = pp_inv[k-1+ia];
+ xassert(vr_len[i] == 0);
+ /* store diagonal element u~[ia,ia] */
+ vr_piv[i] = a(ia, ia);
+ /* determine number of non-zero non-diagonal elements in ia-th
+ * row of U~ */
+ len = 0;
+ for (ja = ia+1; ja <= na; ja++)
+ { if (a(ia, ja) != 0.0)
+ len++;
+ }
+ /* reserve len locations for i-th row of matrix V in left
+ * (dynamic) part of SVA */
+ if (vr_cap[i] < len)
+ { /* there should be enough room in free part of SVA */
+ xassert(sva->r_ptr - sva->m_ptr >= len);
+ sva_enlarge_cap(sva, vr_ref-1+i, len, 0);
+ /* left part of SVA should not overlap matrix A~ */
+ xassert(sva->m_ptr <= a_ptr);
+ }
+ /* copy non-zero non-diaginal elements of ia-th row of U~ to
+ * i-th row of V */
+ ptr = vr_ptr[i];
+ for (ja = ia+1; ja <= na; ja++)
+ { if (a(ia, ja) != 0.0)
+ { sv_ind[ptr] = qq_ind[k-1+ja];
+ sv_val[ptr] = a(ia, ja);
+ ptr++;
+ }
+ }
+ xassert(ptr - vr_ptr[i] == len);
+ vr_len[i] = len;
+ }
+ /* build new columns of F from columns of L~ */
+ for (ja = 1; ja <= na; ja++)
+ { /* ja-th column of L~ = (k-1+ja)-th column of L = j-th column
+ * of F */
+ j = pp_inv[k-1+ja];
+ xassert(fc_len[j] == 0);
+ xassert(fc_cap[j] == 0);
+ /* determine number of non-zero non-diagonal elements in ja-th
+ * column of L~ */
+ len = 0;
+ for (ia = ja+1; ia <= na; ia++)
+ { if (a(ia, ja) != 0.0)
+ len++;
+ }
+ /* reserve len locations for j-th column of matrix F in right
+ * (static) part of SVA */
+ /* there should be enough room in free part of SVA */
+ xassert(sva->r_ptr - sva->m_ptr >= len);
+ if (len > 0)
+ sva_reserve_cap(sva, fc_ref-1+j, len);
+ /* right part of SVA should not overlap matrix A~ */
+ xassert(a_end <= sva->r_ptr);
+ /* copy non-zero non-diagonal elements of ja-th column of L~
+ * to j-th column of F */
+ ptr = fc_ptr[j];
+ for (ia = ja+1; ia <= na; ia++)
+ { if (a(ia, ja) != 0.0)
+ { sv_ind[ptr] = pp_inv[k-1+ia];
+ sv_val[ptr] = a(ia, ja);
+ ptr++;
+ }
+ }
+ xassert(ptr - fc_ptr[j] == len);
+ fc_len[j] = len;
+ }
+ /* factors L~ and U~ are no longer needed */
+# undef a
+ /* if it is not planned to update matrix V, relocate all its new
+ * rows to the right (static) part of SVA */
+ if (!updat)
+ { for (ia = 1; ia <= na; ia++)
+ { i = pp_inv[k-1+ia];
+ len = vr_len[i];
+ if (sva->r_ptr - sva->m_ptr < len)
+ { sva_more_space(sva, len);
+ sv_ind = sva->ind;
+ sv_val = sva->val;
+ }
+ sva_make_static(sva, vr_ref-1+i);
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* sgf_factorize - compute LU-factorization (main routine)
+*
+* This routine computes sparse LU-factorization of specified matrix A
+* using Gaussian elimination.
+*
+* On entry to the routine matrix V = A should be stored in column-wise
+* format.
+*
+* If the factorization has been successfully computed, the routine
+* returns zero. Otherwise, if on k-th elimination step, 1 <= k <= n,
+* all elements of the active submatrix are close to zero, the routine
+* returns k (information on linearly dependent rows/columns in this
+* case is provided by matrices P and Q). */
+
+#if 1 /* 21/II-2016 */
+/* If the matrix A is structurally singular, the routine returns -1.
+* NOTE: This case can be detected only if the singl flag is set. */
+#endif
+
+int sgf_factorize(SGF *sgf, int singl)
+{ LUF *luf = sgf->luf;
+ int n = luf->n;
+ SVA *sva = luf->sva;
+ int vr_ref = luf->vr_ref;
+ int *vr_len = &sva->len[vr_ref-1];
+ double *vr_piv = luf->vr_piv;
+ int vc_ref = luf->vc_ref;
+ int *vc_len = &sva->len[vc_ref-1];
+ int *pp_ind = luf->pp_ind;
+ int *pp_inv = luf->pp_inv;
+ int *qq_ind = luf->qq_ind;
+ int *qq_inv = luf->qq_inv;
+ int *rs_head = sgf->rs_head;
+ int *rs_prev = sgf->rs_prev;
+ int *rs_next = sgf->rs_next;
+ int *cs_head = sgf->cs_head;
+ int *cs_prev = sgf->cs_prev;
+ int *cs_next = sgf->cs_next;
+ double *vr_max = sgf->vr_max;
+ char *flag = sgf->flag;
+ double *work = sgf->work;
+ int i, j, k, k1, k2, p, q, nnz;
+ /* build matrix V = A in row-wise format */
+ luf_build_v_rows(luf, rs_prev);
+ /* P := Q := I, so V = U = A, F = L = I */
+ for (k = 1; k <= n; k++)
+ { vr_piv[k] = 0.0;
+ pp_ind[k] = pp_inv[k] = qq_ind[k] = qq_inv[k] = k;
+ }
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+ luf_check_all(luf, 1);
+#endif
+ /* perform singleton phase, if required */
+ if (!singl)
+ { /* assume that nucleus is entire matrix U */
+ k2 = 1;
+ }
+ else
+ { /* minimize nucleus size */
+#if 0 /* 21/II-2016 */
+ sgf_reduce_nuc(luf, &k1, &k2, rs_prev, rs_next);
+#else
+ if (sgf_reduce_nuc(luf, &k1, &k2, rs_prev, rs_next))
+ return -1;
+#endif
+#ifdef GLP_DEBUG
+ xprintf("n = %d; k1 = %d; k2 = %d\n", n, k1, k2);
+#endif
+ /* perform singleton phase */
+ k2 = sgf_singl_phase(luf, k1, k2, sgf->updat, rs_prev, work);
+ }
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+ luf_check_all(luf, k2);
+#endif
+ /* initialize working arrays */
+ rs_head[0] = cs_head[0] = 0;
+ for (k = 1; k <= n; k++)
+ { rs_head[k] = cs_head[k] = 0;
+ vr_max[k] = -1.0;
+ flag[k] = 0;
+ work[k] = 0.0;
+ }
+ /* build lists of active rows and columns of matrix V; determine
+ * number of non-zeros in initial active submatrix */
+ nnz = 0;
+ for (k = k2; k <= n; k++)
+ { i = pp_inv[k];
+ sgf_activate_row(i);
+ nnz += vr_len[i];
+ j = qq_ind[k];
+ sgf_activate_col(j);
+ }
+ /* main factorization loop */
+ for (k = k2; k <= n; k++)
+ { int na;
+ double den;
+ /* calculate density of active submatrix */
+ na = n - k + 1; /* order of active submatrix */
+#if 0 /* 21/VIII-2014 */
+ den = (double)nnz / (double)(na * na);
+#else
+ den = (double)nnz / ((double)(na) * (double)(na));
+#endif
+ /* if active submatrix is relatively dense, switch to dense
+ * phase */
+#if 1 /* FIXME */
+ if (na >= 5 && den >= 0.71)
+ {
+#ifdef GLP_DEBUG
+ xprintf("na = %d; nnz = %d; den = %g\n", na, nnz, den);
+#endif
+ break;
+ }
+#endif
+ /* choose pivot v[p,q] */
+ if (sgf_choose_pivot(sgf, &p, &q) != 0)
+ return k; /* failure */
+ /* u[i,j] = v[p,q], k <= i, j <= n */
+ i = pp_ind[p];
+ xassert(k <= i && i <= n);
+ j = qq_inv[q];
+ xassert(k <= j && j <= n);
+ /* move u[i,j] to position u[k,k] by implicit permutations of
+ * rows and columns of matrix U */
+ luf_swap_u_rows(k, i);
+ luf_swap_u_cols(k, j);
+ /* perform gaussian elimination */
+ nnz += sgf_eliminate(sgf, p, q);
+ }
+#if 1 /* FIXME */
+ if (k <= n)
+ { /* continue computing factorization in dense mode */
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+ luf_check_all(luf, k);
+#endif
+ k = sgf_dense_phase(luf, k, sgf->updat);
+ if (k != 0)
+ return k; /* failure */
+ }
+#endif
+#ifdef GLP_DEBUG
+ sva_check_area(sva);
+ luf_check_all(luf, n+1);
+#endif
+ /* defragment SVA; currently all columns of V are empty, so they
+ * will have zero capacity as required by luf_build_v_cols */
+ sva_defrag_area(sva);
+ /* build matrix F in row-wise format */
+ luf_build_f_rows(luf, rs_head);
+ /* build matrix V in column-wise format */
+ luf_build_v_cols(luf, sgf->updat, rs_head);
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/sgf.h b/test/monniaux/glpk-4.65/src/bflib/sgf.h
new file mode 100644
index 00000000..4f744610
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/sgf.h
@@ -0,0 +1,203 @@
+/* sgf.h (sparse Gaussian factorizer) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SGF_H
+#define SGF_H
+
+#include "luf.h"
+
+typedef struct SGF SGF;
+
+struct SGF
+{ /* sparse Gaussian factorizer workspace */
+ LUF *luf;
+ /* LU-factorization being computed */
+ /*--------------------------------------------------------------*/
+ /* to efficiently choose pivot elements according to Markowitz
+ * strategy, the search technique proposed by Iain Duff is used;
+ * it is based on using two families of sets {R[0], ..., R[n]}
+ * and {C[0], ..., C[n]}, where R[k] and C[k], 0 <= k <= n, are,
+ * respectively, sets of rows and columns of the active submatrix
+ * of matrix V having k non-zeros (i.e. whose length is k); each
+ * set R[k] and C[k] is implemented as a doubly linked list */
+ int *rs_head; /* int rs_head[1+n]; */
+ /* rs_head[k], 0 <= k <= n, is the number of first row, which
+ * has k non-zeros in the active submatrix */
+ int *rs_prev; /* int rs_prev[1+n]; */
+ /* rs_prev[0] is not used;
+ * rs_prev[i], 1 <= i <= n, is the number of previous row, which
+ * has the same number of non-zeros as i-th row;
+ * rs_prev[i] < 0 means that i-th row is inactive */
+ int *rs_next; /* int rs_next[1+n]; */
+ /* rs_next[0] is not used;
+ * rs_next[i], 1 <= i <= n, is the number of next row, which has
+ * the same number of non-zeros as i-th row;
+ * rs_next[i] < 0 means that i-th row is inactive */
+ int *cs_head; /* int cs_head[1+n]; */
+ /* cs_head[k], 0 <= k <= n, is the number of first column, which
+ * has k non-zeros in the active submatrix */
+ int *cs_prev; /* int cs_prev[1+n]; */
+ /* cs_prev[0] is not used;
+ * cs_prev[j], 1 <= j <= n, is the number of previous column,
+ * which has the same number of non-zeros as j-th column;
+ * cs_prev[j] < 0 means that j-th column is inactive */
+ int *cs_next; /* int cs_next[1+n]; */
+ /* cs_next[0] is not used;
+ * cs_next[j], 1 <= j <= n, is the number of next column, which
+ * has the same number of non-zeros as j-th column;
+ * cs_next[j] < 0 means that j-th column is inactive */
+ /* NOTE: cs_prev[j] = cs_next[j] = j means that j-th column was
+ * temporarily removed from corresponding set C[k] by the
+ * pivoting routine according to Uwe Suhl's heuristic */
+ /*--------------------------------------------------------------*/
+ /* working arrays */
+ double *vr_max; /* int vr_max[1+n]; */
+ /* vr_max[0] is not used;
+ * vr_max[i], 1 <= i <= n, is used only if i-th row of matrix V
+ * is active (i.e. belongs to the active submatrix), and is the
+ * largest magnitude of elements in that row; if vr_max[i] < 0,
+ * the largest magnitude is unknown yet */
+ char *flag; /* char flag[1+n]; */
+ /* boolean working array */
+ double *work; /* double work[1+n]; */
+ /* floating-point working array */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ int updat;
+ /* if this flag is set, the matrix V is assumed to be updatable;
+ * in this case factorized (non-active) part of V is stored in
+ * the left part of SVA rather than in its right part */
+ double piv_tol;
+ /* threshold pivoting tolerance, 0 < piv_tol < 1; element v[i,j]
+ * of the active submatrix fits to be pivot if it satisfies to
+ * the stability criterion |v[i,j]| >= piv_tol * max |v[i,*]|,
+ * i.e. if it is not very small in the magnitude among other
+ * elements in the same row; decreasing this parameter gives
+ * better sparsity at the expense of numerical accuracy and vice
+ * versa */
+ int piv_lim;
+ /* maximal allowable number of pivot candidates to be considered;
+ * if piv_lim pivot candidates have been considered, the pivoting
+ * routine terminates the search with the best candidate found */
+ int suhl;
+ /* if this flag is set, the pivoting routine applies a heuristic
+ * proposed by Uwe Suhl: if a column of the active submatrix has
+ * no eligible pivot candidates (i.e. all its elements do not
+ * satisfy to the stability criterion), the routine excludes it
+ * from futher consideration until it becomes column singleton;
+ * in many cases this allows reducing the time needed to choose
+ * the pivot */
+ double eps_tol;
+ /* epsilon tolerance; each element of the active submatrix, whose
+ * magnitude is less than eps_tol, is replaced by exact zero */
+#if 0 /* FIXME */
+ double den_lim;
+ /* density limit; if the density of the active submatrix reaches
+ * this limit, the factorization routine switches from sparse to
+ * dense mode */
+#endif
+};
+
+#define sgf_activate_row(i) \
+ do \
+ { int len = vr_len[i]; \
+ rs_prev[i] = 0; \
+ rs_next[i] = rs_head[len]; \
+ if (rs_next[i] != 0) \
+ rs_prev[rs_next[i]] = i; \
+ rs_head[len] = i; \
+ } while (0)
+/* include i-th row of matrix V in active set R[len] */
+
+#define sgf_deactivate_row(i) \
+ do \
+ { if (rs_prev[i] == 0) \
+ rs_head[vr_len[i]] = rs_next[i]; \
+ else \
+ rs_next[rs_prev[i]] = rs_next[i]; \
+ if (rs_next[i] == 0) \
+ ; \
+ else \
+ rs_prev[rs_next[i]] = rs_prev[i]; \
+ rs_prev[i] = rs_next[i] = -1; \
+ } while (0)
+/* remove i-th row of matrix V from active set R[len] */
+
+#define sgf_activate_col(j) \
+ do \
+ { int len = vc_len[j]; \
+ cs_prev[j] = 0; \
+ cs_next[j] = cs_head[len]; \
+ if (cs_next[j] != 0) \
+ cs_prev[cs_next[j]] = j; \
+ cs_head[len] = j; \
+ } while (0)
+/* include j-th column of matrix V in active set C[len] */
+
+#define sgf_deactivate_col(j) \
+ do \
+ { if (cs_prev[j] == 0) \
+ cs_head[vc_len[j]] = cs_next[j]; \
+ else \
+ cs_next[cs_prev[j]] = cs_next[j]; \
+ if (cs_next[j] == 0) \
+ ; \
+ else \
+ cs_prev[cs_next[j]] = cs_prev[j]; \
+ cs_prev[j] = cs_next[j] = -1; \
+ } while (0)
+/* remove j-th column of matrix V from active set C[len] */
+
+#define sgf_reduce_nuc _glp_sgf_reduce_nuc
+int sgf_reduce_nuc(LUF *luf, int *k1, int *k2, int cnt[/*1+n*/],
+ int list[/*1+n*/]);
+/* initial reordering to minimize nucleus size */
+
+#define sgf_singl_phase _glp_sgf_singl_phase
+int sgf_singl_phase(LUF *luf, int k1, int k2, int updat,
+ int ind[/*1+n*/], double val[/*1+n*/]);
+/* compute LU-factorization (singleton phase) */
+
+#define sgf_choose_pivot _glp_sgf_choose_pivot
+int sgf_choose_pivot(SGF *sgf, int *p, int *q);
+/* choose pivot element v[p,q] */
+
+#define sgf_eliminate _glp_sgf_eliminate
+int sgf_eliminate(SGF *sgf, int p, int q);
+/* perform gaussian elimination */
+
+#define sgf_dense_lu _glp_sgf_dense_lu
+int sgf_dense_lu(int n, double a[], int r[], int c[], double eps);
+/* compute dense LU-factorization with full pivoting */
+
+#define sgf_dense_phase _glp_sgf_dense_phase
+int sgf_dense_phase(LUF *luf, int k, int updat);
+/* compute LU-factorization (dense phase) */
+
+#define sgf_factorize _glp_sgf_factorize
+int sgf_factorize(SGF *sgf, int singl);
+/* compute LU-factorization (main routine) */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/sva.c b/test/monniaux/glpk-4.65/src/bflib/sva.c
new file mode 100644
index 00000000..e6a675cc
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/sva.c
@@ -0,0 +1,572 @@
+/* sva.c (sparse vector area) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "sva.h"
+
+/***********************************************************************
+* sva_create_area - create sparse vector area (SVA)
+*
+* This routine creates the sparse vector area (SVA), which initially
+* is empty.
+*
+* The parameter n_max specifies the initial number of vectors that can
+* be allocated in the SVA, n_max > 0.
+*
+* The parameter size specifies the initial number of free locations in
+* the SVA, size > 0.
+*
+* On exit the routine returns a pointer to the SVA created. */
+
+SVA *sva_create_area(int n_max, int size)
+{ SVA *sva;
+ xassert(0 < n_max && n_max < INT_MAX);
+ xassert(0 < size && size < INT_MAX);
+ sva = talloc(1, SVA);
+ sva->n_max = n_max;
+ sva->n = 0;
+ sva->ptr = talloc(1+n_max, int);
+ sva->len = talloc(1+n_max, int);
+ sva->cap = talloc(1+n_max, int);
+ sva->size = size;
+ sva->m_ptr = 1;
+ sva->r_ptr = size+1;
+ sva->head = sva->tail = 0;
+ sva->prev = talloc(1+n_max, int);
+ sva->next = talloc(1+n_max, int);
+ sva->ind = talloc(1+size, int);
+ sva->val = talloc(1+size, double);
+ sva->talky = 0;
+ return sva;
+}
+
+/***********************************************************************
+* sva_alloc_vecs - allocate new vectors in SVA
+*
+* This routine allocates nnn new empty vectors, nnn > 0, in the sparse
+* vector area (SVA).
+*
+* The new vectors are assigned reference numbers k, k+1, ..., k+nnn-1,
+* where k is a reference number assigned to the very first new vector,
+* which is returned by the routine on exit. */
+
+int sva_alloc_vecs(SVA *sva, int nnn)
+{ int n = sva->n;
+ int n_max = sva->n_max;
+ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ int *prev = sva->prev;
+ int *next = sva->next;
+ int k, new_n;
+#if 1
+ if (sva->talky)
+ xprintf("sva_alloc_vecs: nnn = %d\n", nnn);
+#endif
+ xassert(nnn > 0);
+ /* determine new number of vectors in SVA */
+ new_n = n + nnn;
+ xassert(new_n > n);
+ if (n_max < new_n)
+ { /* enlarge the SVA arrays */
+ while (n_max < new_n)
+ { n_max += n_max;
+ xassert(n_max > 0);
+ }
+ sva->n_max = n_max;
+ sva->ptr = ptr = trealloc(ptr, 1+n_max, int);
+ sva->len = len = trealloc(len, 1+n_max, int);
+ sva->cap = cap = trealloc(cap, 1+n_max, int);
+ sva->prev = prev = trealloc(prev, 1+n_max, int);
+ sva->next = next = trealloc(next, 1+n_max, int);
+ }
+ /* initialize new vectors */
+ sva->n = new_n;
+ for (k = n+1; k <= new_n; k++)
+ { ptr[k] = len[k] = cap[k] = 0;
+ prev[k] = next[k] = -1;
+ }
+#if 1
+ if (sva->talky)
+ xprintf("now sva->n_max = %d, sva->n = %d\n",
+ sva->n_max, sva->n);
+#endif
+ /* return reference number of very first new vector */
+ return n+1;
+}
+
+/***********************************************************************
+* sva_resize_area - change size of SVA storage
+*
+* This routine increases or decrases the size of the SVA storage by
+* reallocating it.
+*
+* The parameter delta specifies the number of location by which the
+* current size of the SVA storage should be increased (if delta > 0)
+* or decreased (if delta < 0). Note that if delta is negative, it
+* should not be less than the current size of the middle part.
+*
+* As a result of this operation the size of the middle part of SVA is
+* increased/decreased by delta locations.
+*
+* NOTE: This operation changes ptr[k] for all vectors stored in the
+* right part of SVA. */
+
+void sva_resize_area(SVA *sva, int delta)
+{ int n = sva->n;
+ int *ptr = sva->ptr;
+ int size = sva->size;
+ int m_ptr = sva->m_ptr;
+ int r_ptr = sva->r_ptr;
+ int k, r_size;
+#if 1
+ if (sva->talky)
+ xprintf("sva_resize_area: delta = %d\n", delta);
+#endif
+ xassert(delta != 0);
+ /* determine size of the right part, in locations */
+ r_size = size - r_ptr + 1;
+ /* relocate the right part in case of negative delta */
+ if (delta < 0)
+ { xassert(delta >= m_ptr - r_ptr);
+ sva->r_ptr += delta;
+ memmove(&sva->ind[sva->r_ptr], &sva->ind[r_ptr],
+ r_size * sizeof(int));
+ memmove(&sva->val[sva->r_ptr], &sva->val[r_ptr],
+ r_size * sizeof(double));
+ }
+ /* reallocate the storage arrays */
+ xassert(delta < INT_MAX - sva->size);
+ sva->size += delta;
+ sva->ind = trealloc(sva->ind, 1+sva->size, int);
+ sva->val = trealloc(sva->val, 1+sva->size, double);
+ /* relocate the right part in case of positive delta */
+ if (delta > 0)
+ { sva->r_ptr += delta;
+ memmove(&sva->ind[sva->r_ptr], &sva->ind[r_ptr],
+ r_size * sizeof(int));
+ memmove(&sva->val[sva->r_ptr], &sva->val[r_ptr],
+ r_size * sizeof(double));
+ }
+ /* update pointers to vectors stored in the right part */
+ for (k = 1; k <= n; k++)
+ { if (ptr[k] >= r_ptr)
+ ptr[k] += delta;
+ }
+#if 1
+ if (sva->talky)
+ xprintf("now sva->size = %d\n", sva->size);
+#endif
+ return;
+}
+
+/***********************************************************************
+* sva_defrag_area - defragment left part of SVA
+*
+* This routine performs "garbage" collection to defragment the left
+* part of SVA.
+*
+* NOTE: This operation may change ptr[k] and cap[k] for all vectors
+* stored in the left part of SVA. */
+
+void sva_defrag_area(SVA *sva)
+{ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ int *prev = sva->prev;
+ int *next = sva->next;
+ int *ind = sva->ind;
+ double *val = sva->val;
+ int k, next_k, ptr_k, len_k, m_ptr, head, tail;
+#if 1
+ if (sva->talky)
+ { xprintf("sva_defrag_area:\n");
+ xprintf("before defragmenting = %d %d %d\n", sva->m_ptr - 1,
+ sva->r_ptr - sva->m_ptr, sva->size + 1 - sva->r_ptr);
+ }
+#endif
+ m_ptr = 1;
+ head = tail = 0;
+ /* walk through the linked list of vectors stored in the left
+ * part of SVA */
+ for (k = sva->head; k != 0; k = next_k)
+ { /* save number of next vector in the list */
+ next_k = next[k];
+ /* determine length of k-th vector */
+ len_k = len[k];
+ if (len_k == 0)
+ { /* k-th vector is empty; remove it from the left part */
+ ptr[k] = cap[k] = 0;
+ prev[k] = next[k] = -1;
+ }
+ else
+ { /* determine pointer to first location of k-th vector */
+ ptr_k = ptr[k];
+ xassert(m_ptr <= ptr_k);
+ /* relocate k-th vector to the beginning of the left part,
+ * if necessary */
+ if (m_ptr < ptr_k)
+ { memmove(&ind[m_ptr], &ind[ptr_k],
+ len_k * sizeof(int));
+ memmove(&val[m_ptr], &val[ptr_k],
+ len_k * sizeof(double));
+ ptr[k] = m_ptr;
+ }
+ /* remove unused locations from k-th vector */
+ cap[k] = len_k;
+ /* the left part of SVA has been enlarged */
+ m_ptr += len_k;
+ /* add k-th vector to the end of the new linked list */
+ prev[k] = tail;
+ next[k] = 0;
+ if (head == 0)
+ head = k;
+ else
+ next[tail] = k;
+ tail = k;
+ }
+ }
+ /* set new pointer to the middle part of SVA */
+ xassert(m_ptr <= sva->r_ptr);
+ sva->m_ptr = m_ptr;
+ /* set new head and tail of the linked list */
+ sva->head = head;
+ sva->tail = tail;
+#if 1
+ if (sva->talky)
+ xprintf("after defragmenting = %d %d %d\n", sva->m_ptr - 1,
+ sva->r_ptr - sva->m_ptr, sva->size + 1 - sva->r_ptr);
+#endif
+ return;
+}
+
+/***********************************************************************
+* sva_more_space - increase size of middle (free) part of SVA
+*
+* This routine increases the size of the middle (free) part of the
+* sparse vector area (SVA).
+*
+* The parameter m_size specifies the minimal size, in locations, of
+* the middle part to be provided. This new size should be greater than
+* the current size of the middle part.
+*
+* First, the routine defragments the left part of SVA. Then, if the
+* size of the left part has not sufficiently increased, the routine
+* increases the total size of the SVA storage by reallocating it. */
+
+void sva_more_space(SVA *sva, int m_size)
+{ int size, delta;
+#if 1
+ if (sva->talky)
+ xprintf("sva_more_space: m_size = %d\n", m_size);
+#endif
+ xassert(m_size > sva->r_ptr - sva->m_ptr);
+ /* defragment the left part */
+ sva_defrag_area(sva);
+ /* set, heuristically, the minimal size of the middle part to be
+ * not less than the size of the defragmented left part */
+ if (m_size < sva->m_ptr - 1)
+ m_size = sva->m_ptr - 1;
+ /* if there is still not enough room, increase the total size of
+ * the SVA storage */
+ if (sva->r_ptr - sva->m_ptr < m_size)
+ { size = sva->size; /* new sva size */
+ for (;;)
+ { delta = size - sva->size;
+ if (sva->r_ptr - sva->m_ptr + delta >= m_size)
+ break;
+ size += size;
+ xassert(size > 0);
+ }
+ sva_resize_area(sva, delta);
+ xassert(sva->r_ptr - sva->m_ptr >= m_size);
+ }
+ return;
+}
+
+/***********************************************************************
+* sva_enlarge_cap - enlarge capacity of specified vector
+*
+* This routine enlarges the current capacity of the specified vector
+* by relocating its content.
+*
+* The parameter k specifies the reference number of the vector whose
+* capacity should be enlarged, 1 <= k <= n. This vector should either
+* have zero capacity or be stored in the left (dynamic) part of SVA.
+*
+* The parameter new_cap specifies the new capacity of the vector,
+* in locations. This new capacity should be greater than the current
+* capacity of the vector.
+*
+* The parameter skip is a flag. If this flag is set, the routine does
+* *not* copy numerical values of elements of the vector on relocating
+* its content, i.e. only element indices are copied.
+*
+* NOTE: On entry to the routine the middle part of SVA should have at
+* least new_cap free locations. */
+
+void sva_enlarge_cap(SVA *sva, int k, int new_cap, int skip)
+{ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ int *prev = sva->prev;
+ int *next = sva->next;
+ int *ind = sva->ind;
+ double *val = sva->val;
+ xassert(1 <= k && k <= sva->n);
+ xassert(new_cap > cap[k]);
+ /* there should be at least new_cap free locations */
+ xassert(sva->r_ptr - sva->m_ptr >= new_cap);
+ /* relocate the vector */
+ if (cap[k] == 0)
+ { /* the vector is empty */
+ xassert(ptr[k] == 0);
+ xassert(len[k] == 0);
+ }
+ else
+ { /* the vector has non-zero capacity */
+ xassert(ptr[k] + len[k] <= sva->m_ptr);
+ /* copy the current vector content to the beginning of the
+ * middle part */
+ if (len[k] > 0)
+ { memcpy(&ind[sva->m_ptr], &ind[ptr[k]],
+ len[k] * sizeof(int));
+ if (!skip)
+ memcpy(&val[sva->m_ptr], &val[ptr[k]],
+ len[k] * sizeof(double));
+ }
+ /* remove the vector from the linked list */
+ if (prev[k] == 0)
+ sva->head = next[k];
+ else
+ { /* preceding vector exists; increase its capacity */
+ cap[prev[k]] += cap[k];
+ next[prev[k]] = next[k];
+ }
+ if (next[k] == 0)
+ sva->tail = prev[k];
+ else
+ prev[next[k]] = prev[k];
+ }
+ /* set new pointer and capacity of the vector */
+ ptr[k] = sva->m_ptr;
+ cap[k] = new_cap;
+ /* add the vector to the end of the linked list */
+ prev[k] = sva->tail;
+ next[k] = 0;
+ if (sva->head == 0)
+ sva->head = k;
+ else
+ next[sva->tail] = k;
+ sva->tail = k;
+ /* new_cap free locations have been consumed */
+ sva->m_ptr += new_cap;
+ xassert(sva->m_ptr <= sva->r_ptr);
+ return;
+}
+
+/***********************************************************************
+* sva_reserve_cap - reserve locations for specified vector
+*
+* This routine reserves locations for the specified vector in the
+* right (static) part of SVA.
+*
+* The parameter k specifies the reference number of the vector (this
+* vector should have zero capacity), 1 <= k <= n.
+*
+* The parameter new_cap specifies a non-zero capacity of the vector,
+* in locations.
+*
+* NOTE: On entry to the routine the middle part of SVA should have at
+* least new_cap free locations. */
+
+void sva_reserve_cap(SVA *sva, int k, int new_cap)
+{ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ xassert(1 <= k && k <= sva->n);
+ xassert(new_cap > 0);
+ xassert(ptr[k] == 0 && len[k] == 0 && cap[k] == 0);
+ /* there should be at least new_cap free locations */
+ xassert(sva->r_ptr - sva->m_ptr >= new_cap);
+ /* set the pointer and capacity of the vector */
+ ptr[k] = sva->r_ptr - new_cap;
+ cap[k] = new_cap;
+ /* new_cap free locations have been consumed */
+ sva->r_ptr -= new_cap;
+ return;
+}
+
+/***********************************************************************
+* sva_make_static - relocate specified vector to right part of SVA
+*
+* Assuming that the specified vector is stored in the left (dynamic)
+* part of SVA, this routine makes the vector static by relocating its
+* content to the right (static) part of SVA. However, if the specified
+* vector has zero capacity, the routine does nothing.
+*
+* The parameter k specifies the reference number of the vector to be
+* relocated, 1 <= k <= n.
+*
+* NOTE: On entry to the routine the middle part of SVA should have at
+* least len[k] free locations, where len[k] is the length of the
+* vector to be relocated. */
+
+void sva_make_static(SVA *sva, int k)
+{ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ int *prev = sva->prev;
+ int *next = sva->next;
+ int *ind = sva->ind;
+ double *val = sva->val;
+ int ptr_k, len_k;
+ xassert(1 <= k && k <= sva->n);
+ /* if the vector has zero capacity, do nothing */
+ if (cap[k] == 0)
+ { xassert(ptr[k] == 0);
+ xassert(len[k] == 0);
+ goto done;
+ }
+ /* there should be at least len[k] free locations */
+ len_k = len[k];
+ xassert(sva->r_ptr - sva->m_ptr >= len_k);
+ /* remove the vector from the linked list */
+ if (prev[k] == 0)
+ sva->head = next[k];
+ else
+ { /* preceding vector exists; increase its capacity */
+ cap[prev[k]] += cap[k];
+ next[prev[k]] = next[k];
+ }
+ if (next[k] == 0)
+ sva->tail = prev[k];
+ else
+ prev[next[k]] = prev[k];
+ /* if the vector has zero length, make it empty */
+ if (len_k == 0)
+ { ptr[k] = cap[k] = 0;
+ goto done;
+ }
+ /* copy the vector content to the beginning of the right part */
+ ptr_k = sva->r_ptr - len_k;
+ memcpy(&ind[ptr_k], &ind[ptr[k]], len_k * sizeof(int));
+ memcpy(&val[ptr_k], &val[ptr[k]], len_k * sizeof(double));
+ /* set new pointer and capacity of the vector */
+ ptr[k] = ptr_k;
+ cap[k] = len_k;
+ /* len[k] free locations have been consumed */
+ sva->r_ptr -= len_k;
+done: return;
+}
+
+/***********************************************************************
+* sva_check_area - check sparse vector area (SVA)
+*
+* This routine checks the SVA data structures for correctness.
+*
+* NOTE: For testing/debugging only. */
+
+void sva_check_area(SVA *sva)
+{ int n_max = sva->n_max;
+ int n = sva->n;
+ int *ptr = sva->ptr;
+ int *len = sva->len;
+ int *cap = sva->cap;
+ int size = sva->size;
+ int m_ptr = sva->m_ptr;
+ int r_ptr = sva->r_ptr;
+ int head = sva->head;
+ int tail = sva->tail;
+ int *prev = sva->prev;
+ int *next = sva->next;
+ int k;
+#if 0 /* 16/II-2004; SVA may be empty */
+ xassert(1 <= n && n <= n_max);
+#else
+ xassert(0 <= n && n <= n_max);
+#endif
+ xassert(1 <= m_ptr && m_ptr <= r_ptr && r_ptr <= size+1);
+ /* all vectors included the linked list should have non-zero
+ * capacity and be stored in the left part */
+ for (k = head; k != 0; k = next[k])
+ { xassert(1 <= k && k <= n);
+ xassert(cap[k] > 0);
+ xassert(0 <= len[k] && len[k] <= cap[k]);
+ if (prev[k] == 0)
+ xassert(k == head);
+ else
+ { xassert(1 <= prev[k] && prev[k] <= n);
+ xassert(next[prev[k]] == k);
+ }
+ if (next[k] == 0)
+ { xassert(k == tail);
+ xassert(ptr[k] + cap[k] <= m_ptr);
+ }
+ else
+ { xassert(1 <= next[k] && next[k] <= n);
+ xassert(prev[next[k]] == k);
+ xassert(ptr[k] + cap[k] <= ptr[next[k]]);
+ }
+ cap[k] = -cap[k];
+ }
+ /* all other vectors should either have zero capacity or be
+ * stored in the right part */
+ for (k = 1; k <= n; k++)
+ { if (cap[k] < 0)
+ { /* k-th vector is stored in the left part */
+ cap[k] = -cap[k];
+ }
+ else if (cap[k] == 0)
+ { /* k-th vector has zero capacity */
+ xassert(ptr[k] == 0);
+ xassert(len[k] == 0);
+ }
+ else /* cap[k] > 0 */
+ { /* k-th vector is stored in the right part */
+ xassert(0 <= len[k] && len[k] <= cap[k]);
+ xassert(r_ptr <= ptr[k] && ptr[k] + cap[k] <= size+1);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* sva_delete_area - delete sparse vector area (SVA)
+*
+* This routine deletes the sparse vector area (SVA) freeing all the
+* memory allocated to it. */
+
+void sva_delete_area(SVA *sva)
+{ tfree(sva->ptr);
+ tfree(sva->len);
+ tfree(sva->cap);
+ tfree(sva->prev);
+ tfree(sva->next);
+ tfree(sva->ind);
+ tfree(sva->val);
+ tfree(sva);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/bflib/sva.h b/test/monniaux/glpk-4.65/src/bflib/sva.h
new file mode 100644
index 00000000..0eab317b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/bflib/sva.h
@@ -0,0 +1,161 @@
+/* sva.h (sparse vector area) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SVA_H
+#define SVA_H
+
+/***********************************************************************
+* Sparse Vector Area (SVA) is a container for sparse vectors. This
+* program object is used mainly on computing factorization, where the
+* sparse vectors are rows and columns of sparse matrices.
+*
+* The SVA storage is a set of locations numbered 1, 2, ..., size,
+* where size is the size of SVA, which is the total number of
+* locations currently allocated. Each location is identified by its
+* pointer p, 1 <= p <= size, and is the pair (ind[p], val[p]), where
+* ind[p] and val[p] are, respectively, the index and value fields used
+* to store the index and numeric value of a particular vector element.
+*
+* Each sparse vector is identified by its reference number k,
+* 1 <= k <= n, where n is the total number of vectors currently stored
+* in SVA, and defined by the triplet (ptr[k], len[k], cap[k]), where:
+* ptr[k] is a pointer to the first location of the vector; len[k] is
+* the vector length, which is the number of its non-zero elements,
+* len[k] >= 0; and cap[k] is the capacity of the vector, which is the
+* total number of adjacent locations allocated to that vector,
+* cap[k] >= len[k]. Thus, non-zero elements of k-th vector are stored
+* in locations ptr[k], ptr[k]+1, ..., ptr[k]+len[k]-1, and locations
+* ptr[k]+len[k], ptr[k]+len[k]+1, ..., ptr[k]+cap[k]-1 are reserved.
+*
+* The SVA storage is divided into three parts as follows:
+*
+* Locations 1, 2, ..., m_ptr-1 constitute the left (dynamic) part of
+* SVA. This part is used to store vectors, whose capacity may change.
+* Note that all vectors stored in the left part are also included in
+* a doubly linked list, where they are ordered by increasing their
+* pointers ptr[k] (this list is needed for efficient implementation
+* of the garbage collector used to defragment the left part of SVA);
+*
+* Locations m_ptr, m_ptr+1, ..., r_ptr-1 are free and constitute the
+* middle (free) part of SVA.
+*
+* Locations r_ptr, r_ptr+1, ..., size constitute the right (static)
+* part of SVA. This part is used to store vectors, whose capacity is
+* not changed. */
+
+typedef struct SVA SVA;
+
+struct SVA
+{ /* sparse vector area */
+ int n_max;
+ /* maximal value of n (enlarged automatically) */
+ int n;
+ /* number of currently allocated vectors, 0 <= n <= n_max */
+ int *ptr; /* int ptr[1+n_max]; */
+ /* ptr[0] is not used;
+ * ptr[k], 1 <= i <= n, is pointer to first location of k-th
+ * vector in the arrays ind and val */
+ int *len; /* int len[1+n_max]; */
+ /* len[0] is not used;
+ * len[k], 1 <= k <= n, is length of k-th vector, len[k] >= 0 */
+ int *cap; /* int cap[1+n_max]; */
+ /* cap[0] is not used;
+ * cap[k], 1 <= k <= n, is capacity of k-th vector (the number
+ * of adjacent locations allocated to it), cap[k] >= len[k] */
+ /* NOTE: if cap[k] = 0, then ptr[k] = 0 and len[k] = 0 */
+ int size;
+ /* total number of locations in SVA */
+ int m_ptr, r_ptr;
+ /* partitioning pointers that define the left, middle, and right
+ * parts of SVA (see above); 1 <= m_ptr <= r_ptr <= size+1 */
+ int head;
+ /* number of first (leftmost) vector in the linked list */
+ int tail;
+ /* number of last (rightmost) vector in the linked list */
+ int *prev; /* int prev[1+n_max]; */
+ /* prev[0] is not used;
+ * prev[k] is number of vector which precedes k-th vector in the
+ * linked list;
+ * prev[k] < 0 means that k-th vector is not in the list */
+ int *next; /* int next[1+n_max]; */
+ /* next[0] is not used;
+ * next[k] is number of vector which succedes k-th vector in the
+ * linked list;
+ * next[k] < 0 means that k-th vector is not in the list */
+ /* NOTE: only vectors having non-zero capacity and stored in the
+ * left part of SVA are included in this linked list */
+ int *ind; /* int ind[1+size]; */
+ /* ind[0] is not used;
+ * ind[p], 1 <= p <= size, is index field of location p */
+ double *val; /* double val[1+size]; */
+ /* val[0] is not used;
+ * val[p], 1 <= p <= size, is value field of location p */
+#if 1
+ int talky;
+ /* option to enable talky mode */
+#endif
+};
+
+#define sva_create_area _glp_sva_create_area
+SVA *sva_create_area(int n_max, int size);
+/* create sparse vector area (SVA) */
+
+#define sva_alloc_vecs _glp_sva_alloc_vecs
+int sva_alloc_vecs(SVA *sva, int nnn);
+/* allocate new vectors in SVA */
+
+#define sva_resize_area _glp_sva_resize_area
+void sva_resize_area(SVA *sva, int delta);
+/* change size of SVA storage */
+
+#define sva_defrag_area _glp_sva_defrag_area
+void sva_defrag_area(SVA *sva);
+/* defragment left part of SVA */
+
+#define sva_more_space _glp_sva_more_space
+void sva_more_space(SVA *sva, int m_size);
+/* increase size of middle (free) part of SVA */
+
+#define sva_enlarge_cap _glp_sva_enlarge_cap
+void sva_enlarge_cap(SVA *sva, int k, int new_cap, int skip);
+/* enlarge capacity of specified vector */
+
+#define sva_reserve_cap _glp_sva_reserve_cap
+void sva_reserve_cap(SVA *sva, int k, int new_cap);
+/* reserve locations for specified vector */
+
+#define sva_make_static _glp_sva_make_static
+void sva_make_static(SVA *sva, int k);
+/* relocate specified vector to right part of SVA */
+
+#define sva_check_area _glp_sva_check_area
+void sva_check_area(SVA *sva);
+/* check sparse vector area (SVA) */
+
+#define sva_delete_area _glp_sva_delete_area
+void sva_delete_area(SVA *sva);
+/* delete sparse vector area (SVA) */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/colamd/COPYING b/test/monniaux/glpk-4.65/src/colamd/COPYING
new file mode 100644
index 00000000..84bba36d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/colamd/COPYING
@@ -0,0 +1,502 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/test/monniaux/glpk-4.65/src/colamd/README b/test/monniaux/glpk-4.65/src/colamd/README
new file mode 100644
index 00000000..a365059f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/colamd/README
@@ -0,0 +1,98 @@
+NOTE: Files in this subdirectory are NOT part of the GLPK package, but
+ are used with GLPK.
+
+ The original code was modified according to GLPK requirements by
+ Andrew Makhorin <mao@gnu.org>.
+************************************************************************
+COLAMD/SYMAMD Version 2.7, Copyright (C) 1998-2007, Timothy A. Davis,
+All Rights Reserved.
+
+Description:
+
+ colamd: an approximate minimum degree column ordering algorithm,
+ for LU factorization of symmetric or unsymmetric matrices,
+ QR factorization, least squares, interior point methods for
+ linear programming problems, and other related problems.
+
+ symamd: an approximate minimum degree ordering algorithm for
+ Cholesky factorization of symmetric matrices.
+
+Purpose:
+
+ Colamd computes a permutation Q such that the Cholesky factorization
+ of (AQ)'(AQ) has less fill-in and requires fewer floating point
+ operations than A'A. This also provides a good ordering for sparse
+ partial pivoting methods, P(AQ) = LU, where Q is computed prior to
+ numerical factorization, and P is computed during numerical
+ factorization via conventional partial pivoting with row
+ interchanges. Colamd is the column ordering method used in SuperLU,
+ part of the ScaLAPACK library. It is also available as built-in
+ function in MATLAB Version 6, available from MathWorks, Inc.
+ (http://www.mathworks.com). This routine can be used in place of
+ colmmd in MATLAB.
+
+ Symamd computes a permutation P of a symmetric matrix A such that
+ the Cholesky factorization of PAP' has less fill-in and requires
+ fewer floating point operations than A. Symamd constructs a matrix
+ M such that M'M has the same nonzero pattern of A, and then orders
+ the columns of M using colmmd. The column ordering of M is then
+ returned as the row and column ordering P of A.
+
+Authors:
+
+ The authors of the code itself are Stefan I. Larimore and Timothy A.
+ Davis (davis at cise.ufl.edu), University of Florida. The algorithm
+ was developed in collaboration with John Gilbert, Xerox PARC, and
+ Esmond Ng, Oak Ridge National Laboratory.
+
+Acknowledgements:
+
+ This work was supported by the National Science Foundation, under
+ grants DMS-9504974 and DMS-9803599.
+
+License:
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation; either version 2.1 of
+ the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA.
+
+ Permission is hereby granted to use or copy this program under the
+ terms of the GNU LGPL, provided that the Copyright, this License,
+ and the Availability of the original version is retained on all
+ copies. User documentation of any code that uses this code or any
+ modified version of this code must cite the Copyright, this License,
+ the Availability note, and "Used by permission." Permission to
+ modify the code and to distribute modified code is granted, provided
+ the Copyright, this License, and the Availability note are retained,
+ and a notice that the code was modified is included.
+
+ COLAMD is also available under alternate licenses, contact T. Davis
+ for details.
+
+Availability:
+
+ The colamd/symamd library is available at:
+
+ http://www.cise.ufl.edu/research/sparse/colamd/
+
+References:
+
+ T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, An approximate
+ column minimum degree ordering algorithm, ACM Transactions on
+ Mathematical Software, vol. 30, no. 3., pp. 353-376, 2004.
+
+ T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, Algorithm 836:
+ COLAMD, an approximate column minimum degree ordering algorithm, ACM
+ Transactions on Mathematical Software, vol. 30, no. 3., pp. 377-380,
+ 2004.
diff --git a/test/monniaux/glpk-4.65/src/colamd/colamd.c b/test/monniaux/glpk-4.65/src/colamd/colamd.c
new file mode 100644
index 00000000..86ddd6b7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/colamd/colamd.c
@@ -0,0 +1,3622 @@
+/* ========================================================================== */
+/* === colamd/symamd - a sparse matrix column ordering algorithm ============ */
+/* ========================================================================== */
+
+/* COLAMD / SYMAMD
+
+ colamd: an approximate minimum degree column ordering algorithm,
+ for LU factorization of symmetric or unsymmetric matrices,
+ QR factorization, least squares, interior point methods for
+ linear programming problems, and other related problems.
+
+ symamd: an approximate minimum degree ordering algorithm for Cholesky
+ factorization of symmetric matrices.
+
+ Purpose:
+
+ Colamd computes a permutation Q such that the Cholesky factorization of
+ (AQ)'(AQ) has less fill-in and requires fewer floating point operations
+ than A'A. This also provides a good ordering for sparse partial
+ pivoting methods, P(AQ) = LU, where Q is computed prior to numerical
+ factorization, and P is computed during numerical factorization via
+ conventional partial pivoting with row interchanges. Colamd is the
+ column ordering method used in SuperLU, part of the ScaLAPACK library.
+ It is also available as built-in function in MATLAB Version 6,
+ available from MathWorks, Inc. (http://www.mathworks.com). This
+ routine can be used in place of colmmd in MATLAB.
+
+ Symamd computes a permutation P of a symmetric matrix A such that the
+ Cholesky factorization of PAP' has less fill-in and requires fewer
+ floating point operations than A. Symamd constructs a matrix M such
+ that M'M has the same nonzero pattern of A, and then orders the columns
+ of M using colmmd. The column ordering of M is then returned as the
+ row and column ordering P of A.
+
+ Authors:
+
+ The authors of the code itself are Stefan I. Larimore and Timothy A.
+ Davis (davis at cise.ufl.edu), University of Florida. The algorithm was
+ developed in collaboration with John Gilbert, Xerox PARC, and Esmond
+ Ng, Oak Ridge National Laboratory.
+
+ Acknowledgements:
+
+ This work was supported by the National Science Foundation, under
+ grants DMS-9504974 and DMS-9803599.
+
+ Copyright and License:
+
+ Copyright (c) 1998-2007, Timothy A. Davis, All Rights Reserved.
+ COLAMD is also available under alternate licenses, contact T. Davis
+ for details.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+
+ Permission is hereby granted to use or copy this program under the
+ terms of the GNU LGPL, provided that the Copyright, this License,
+ and the Availability of the original version is retained on all copies.
+ User documentation of any code that uses this code or any modified
+ version of this code must cite the Copyright, this License, the
+ Availability note, and "Used by permission." Permission to modify
+ the code and to distribute modified code is granted, provided the
+ Copyright, this License, and the Availability note are retained,
+ and a notice that the code was modified is included.
+
+ Availability:
+
+ The colamd/symamd library is available at
+
+ http://www.cise.ufl.edu/research/sparse/colamd/
+
+ This is the http://www.cise.ufl.edu/research/sparse/colamd/colamd.c
+ file. It requires the colamd.h file. It is required by the colamdmex.c
+ and symamdmex.c files, for the MATLAB interface to colamd and symamd.
+ Appears as ACM Algorithm 836.
+
+ See the ChangeLog file for changes since Version 1.0.
+
+ References:
+
+ T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, An approximate column
+ minimum degree ordering algorithm, ACM Transactions on Mathematical
+ Software, vol. 30, no. 3., pp. 353-376, 2004.
+
+ T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, Algorithm 836: COLAMD,
+ an approximate column minimum degree ordering algorithm, ACM
+ Transactions on Mathematical Software, vol. 30, no. 3., pp. 377-380,
+ 2004.
+
+*/
+
+/* ========================================================================== */
+/* === Description of user-callable routines ================================ */
+/* ========================================================================== */
+
+/* COLAMD includes both int and UF_long versions of all its routines. The
+ * description below is for the int version. For UF_long, all int arguments
+ * become UF_long. UF_long is normally defined as long, except for WIN64.
+
+ ----------------------------------------------------------------------------
+ colamd_recommended:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ size_t colamd_recommended (int nnz, int n_row, int n_col) ;
+ size_t colamd_l_recommended (UF_long nnz, UF_long n_row,
+ UF_long n_col) ;
+
+ Purpose:
+
+ Returns recommended value of Alen for use by colamd. Returns 0
+ if any input argument is negative. The use of this routine
+ is optional. Not needed for symamd, which dynamically allocates
+ its own memory.
+
+ Note that in v2.4 and earlier, these routines returned int or long.
+ They now return a value of type size_t.
+
+ Arguments (all input arguments):
+
+ int nnz ; Number of nonzeros in the matrix A. This must
+ be the same value as p [n_col] in the call to
+ colamd - otherwise you will get a wrong value
+ of the recommended memory to use.
+
+ int n_row ; Number of rows in the matrix A.
+
+ int n_col ; Number of columns in the matrix A.
+
+ ----------------------------------------------------------------------------
+ colamd_set_defaults:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ colamd_set_defaults (double knobs [COLAMD_KNOBS]) ;
+ colamd_l_set_defaults (double knobs [COLAMD_KNOBS]) ;
+
+ Purpose:
+
+ Sets the default parameters. The use of this routine is optional.
+
+ Arguments:
+
+ double knobs [COLAMD_KNOBS] ; Output only.
+
+ NOTE: the meaning of the dense row/col knobs has changed in v2.4
+
+ knobs [0] and knobs [1] control dense row and col detection:
+
+ Colamd: rows with more than
+ max (16, knobs [COLAMD_DENSE_ROW] * sqrt (n_col))
+ entries are removed prior to ordering. Columns with more than
+ max (16, knobs [COLAMD_DENSE_COL] * sqrt (MIN (n_row,n_col)))
+ entries are removed prior to
+ ordering, and placed last in the output column ordering.
+
+ Symamd: uses only knobs [COLAMD_DENSE_ROW], which is knobs [0].
+ Rows and columns with more than
+ max (16, knobs [COLAMD_DENSE_ROW] * sqrt (n))
+ entries are removed prior to ordering, and placed last in the
+ output ordering.
+
+ COLAMD_DENSE_ROW and COLAMD_DENSE_COL are defined as 0 and 1,
+ respectively, in colamd.h. Default values of these two knobs
+ are both 10. Currently, only knobs [0] and knobs [1] are
+ used, but future versions may use more knobs. If so, they will
+ be properly set to their defaults by the future version of
+ colamd_set_defaults, so that the code that calls colamd will
+ not need to change, assuming that you either use
+ colamd_set_defaults, or pass a (double *) NULL pointer as the
+ knobs array to colamd or symamd.
+
+ knobs [2]: aggressive absorption
+
+ knobs [COLAMD_AGGRESSIVE] controls whether or not to do
+ aggressive absorption during the ordering. Default is TRUE.
+
+
+ ----------------------------------------------------------------------------
+ colamd:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ int colamd (int n_row, int n_col, int Alen, int *A, int *p,
+ double knobs [COLAMD_KNOBS], int stats [COLAMD_STATS]) ;
+ UF_long colamd_l (UF_long n_row, UF_long n_col, UF_long Alen,
+ UF_long *A, UF_long *p, double knobs [COLAMD_KNOBS],
+ UF_long stats [COLAMD_STATS]) ;
+
+ Purpose:
+
+ Computes a column ordering (Q) of A such that P(AQ)=LU or
+ (AQ)'AQ=LL' have less fill-in and require fewer floating point
+ operations than factorizing the unpermuted matrix A or A'A,
+ respectively.
+
+ Returns:
+
+ TRUE (1) if successful, FALSE (0) otherwise.
+
+ Arguments:
+
+ int n_row ; Input argument.
+
+ Number of rows in the matrix A.
+ Restriction: n_row >= 0.
+ Colamd returns FALSE if n_row is negative.
+
+ int n_col ; Input argument.
+
+ Number of columns in the matrix A.
+ Restriction: n_col >= 0.
+ Colamd returns FALSE if n_col is negative.
+
+ int Alen ; Input argument.
+
+ Restriction (see note):
+ Alen >= 2*nnz + 6*(n_col+1) + 4*(n_row+1) + n_col
+ Colamd returns FALSE if these conditions are not met.
+
+ Note: this restriction makes an modest assumption regarding
+ the size of the two typedef's structures in colamd.h.
+ We do, however, guarantee that
+
+ Alen >= colamd_recommended (nnz, n_row, n_col)
+
+ will be sufficient. Note: the macro version does not check
+ for integer overflow, and thus is not recommended. Use
+ the colamd_recommended routine instead.
+
+ int A [Alen] ; Input argument, undefined on output.
+
+ A is an integer array of size Alen. Alen must be at least as
+ large as the bare minimum value given above, but this is very
+ low, and can result in excessive run time. For best
+ performance, we recommend that Alen be greater than or equal to
+ colamd_recommended (nnz, n_row, n_col), which adds
+ nnz/5 to the bare minimum value given above.
+
+ On input, the row indices of the entries in column c of the
+ matrix are held in A [(p [c]) ... (p [c+1]-1)]. The row indices
+ in a given column c need not be in ascending order, and
+ duplicate row indices may be be present. However, colamd will
+ work a little faster if both of these conditions are met
+ (Colamd puts the matrix into this format, if it finds that the
+ the conditions are not met).
+
+ The matrix is 0-based. That is, rows are in the range 0 to
+ n_row-1, and columns are in the range 0 to n_col-1. Colamd
+ returns FALSE if any row index is out of range.
+
+ The contents of A are modified during ordering, and are
+ undefined on output.
+
+ int p [n_col+1] ; Both input and output argument.
+
+ p is an integer array of size n_col+1. On input, it holds the
+ "pointers" for the column form of the matrix A. Column c of
+ the matrix A is held in A [(p [c]) ... (p [c+1]-1)]. The first
+ entry, p [0], must be zero, and p [c] <= p [c+1] must hold
+ for all c in the range 0 to n_col-1. The value p [n_col] is
+ thus the total number of entries in the pattern of the matrix A.
+ Colamd returns FALSE if these conditions are not met.
+
+ On output, if colamd returns TRUE, the array p holds the column
+ permutation (Q, for P(AQ)=LU or (AQ)'(AQ)=LL'), where p [0] is
+ the first column index in the new ordering, and p [n_col-1] is
+ the last. That is, p [k] = j means that column j of A is the
+ kth pivot column, in AQ, where k is in the range 0 to n_col-1
+ (p [0] = j means that column j of A is the first column in AQ).
+
+ If colamd returns FALSE, then no permutation is returned, and
+ p is undefined on output.
+
+ double knobs [COLAMD_KNOBS] ; Input argument.
+
+ See colamd_set_defaults for a description.
+
+ int stats [COLAMD_STATS] ; Output argument.
+
+ Statistics on the ordering, and error status.
+ See colamd.h for related definitions.
+ Colamd returns FALSE if stats is not present.
+
+ stats [0]: number of dense or empty rows ignored.
+
+ stats [1]: number of dense or empty columns ignored (and
+ ordered last in the output permutation p)
+ Note that a row can become "empty" if it
+ contains only "dense" and/or "empty" columns,
+ and similarly a column can become "empty" if it
+ only contains "dense" and/or "empty" rows.
+
+ stats [2]: number of garbage collections performed.
+ This can be excessively high if Alen is close
+ to the minimum required value.
+
+ stats [3]: status code. < 0 is an error code.
+ > 1 is a warning or notice.
+
+ 0 OK. Each column of the input matrix contained
+ row indices in increasing order, with no
+ duplicates.
+
+ 1 OK, but columns of input matrix were jumbled
+ (unsorted columns or duplicate entries). Colamd
+ had to do some extra work to sort the matrix
+ first and remove duplicate entries, but it
+ still was able to return a valid permutation
+ (return value of colamd was TRUE).
+
+ stats [4]: highest numbered column that
+ is unsorted or has duplicate
+ entries.
+ stats [5]: last seen duplicate or
+ unsorted row index.
+ stats [6]: number of duplicate or
+ unsorted row indices.
+
+ -1 A is a null pointer
+
+ -2 p is a null pointer
+
+ -3 n_row is negative
+
+ stats [4]: n_row
+
+ -4 n_col is negative
+
+ stats [4]: n_col
+
+ -5 number of nonzeros in matrix is negative
+
+ stats [4]: number of nonzeros, p [n_col]
+
+ -6 p [0] is nonzero
+
+ stats [4]: p [0]
+
+ -7 A is too small
+
+ stats [4]: required size
+ stats [5]: actual size (Alen)
+
+ -8 a column has a negative number of entries
+
+ stats [4]: column with < 0 entries
+ stats [5]: number of entries in col
+
+ -9 a row index is out of bounds
+
+ stats [4]: column with bad row index
+ stats [5]: bad row index
+ stats [6]: n_row, # of rows of matrx
+
+ -10 (unused; see symamd.c)
+
+ -999 (unused; see symamd.c)
+
+ Future versions may return more statistics in the stats array.
+
+ Example:
+
+ See http://www.cise.ufl.edu/research/sparse/colamd/example.c
+ for a complete example.
+
+ To order the columns of a 5-by-4 matrix with 11 nonzero entries in
+ the following nonzero pattern
+
+ x 0 x 0
+ x 0 x x
+ 0 x x 0
+ 0 0 x x
+ x x 0 0
+
+ with default knobs and no output statistics, do the following:
+
+ #include "colamd.h"
+ #define ALEN 100
+ int A [ALEN] = {0, 1, 4, 2, 4, 0, 1, 2, 3, 1, 3} ;
+ int p [ ] = {0, 3, 5, 9, 11} ;
+ int stats [COLAMD_STATS] ;
+ colamd (5, 4, ALEN, A, p, (double *) NULL, stats) ;
+
+ The permutation is returned in the array p, and A is destroyed.
+
+ ----------------------------------------------------------------------------
+ symamd:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ int symamd (int n, int *A, int *p, int *perm,
+ double knobs [COLAMD_KNOBS], int stats [COLAMD_STATS],
+ void (*allocate) (size_t, size_t), void (*release) (void *)) ;
+ UF_long symamd_l (UF_long n, UF_long *A, UF_long *p, UF_long *perm,
+ double knobs [COLAMD_KNOBS], UF_long stats [COLAMD_STATS],
+ void (*allocate) (size_t, size_t), void (*release) (void *)) ;
+
+ Purpose:
+
+ The symamd routine computes an ordering P of a symmetric sparse
+ matrix A such that the Cholesky factorization PAP' = LL' remains
+ sparse. It is based on a column ordering of a matrix M constructed
+ so that the nonzero pattern of M'M is the same as A. The matrix A
+ is assumed to be symmetric; only the strictly lower triangular part
+ is accessed. You must pass your selected memory allocator (usually
+ calloc/free or mxCalloc/mxFree) to symamd, for it to allocate
+ memory for the temporary matrix M.
+
+ Returns:
+
+ TRUE (1) if successful, FALSE (0) otherwise.
+
+ Arguments:
+
+ int n ; Input argument.
+
+ Number of rows and columns in the symmetrix matrix A.
+ Restriction: n >= 0.
+ Symamd returns FALSE if n is negative.
+
+ int A [nnz] ; Input argument.
+
+ A is an integer array of size nnz, where nnz = p [n].
+
+ The row indices of the entries in column c of the matrix are
+ held in A [(p [c]) ... (p [c+1]-1)]. The row indices in a
+ given column c need not be in ascending order, and duplicate
+ row indices may be present. However, symamd will run faster
+ if the columns are in sorted order with no duplicate entries.
+
+ The matrix is 0-based. That is, rows are in the range 0 to
+ n-1, and columns are in the range 0 to n-1. Symamd
+ returns FALSE if any row index is out of range.
+
+ The contents of A are not modified.
+
+ int p [n+1] ; Input argument.
+
+ p is an integer array of size n+1. On input, it holds the
+ "pointers" for the column form of the matrix A. Column c of
+ the matrix A is held in A [(p [c]) ... (p [c+1]-1)]. The first
+ entry, p [0], must be zero, and p [c] <= p [c+1] must hold
+ for all c in the range 0 to n-1. The value p [n] is
+ thus the total number of entries in the pattern of the matrix A.
+ Symamd returns FALSE if these conditions are not met.
+
+ The contents of p are not modified.
+
+ int perm [n+1] ; Output argument.
+
+ On output, if symamd returns TRUE, the array perm holds the
+ permutation P, where perm [0] is the first index in the new
+ ordering, and perm [n-1] is the last. That is, perm [k] = j
+ means that row and column j of A is the kth column in PAP',
+ where k is in the range 0 to n-1 (perm [0] = j means
+ that row and column j of A are the first row and column in
+ PAP'). The array is used as a workspace during the ordering,
+ which is why it must be of length n+1, not just n.
+
+ double knobs [COLAMD_KNOBS] ; Input argument.
+
+ See colamd_set_defaults for a description.
+
+ int stats [COLAMD_STATS] ; Output argument.
+
+ Statistics on the ordering, and error status.
+ See colamd.h for related definitions.
+ Symamd returns FALSE if stats is not present.
+
+ stats [0]: number of dense or empty row and columns ignored
+ (and ordered last in the output permutation
+ perm). Note that a row/column can become
+ "empty" if it contains only "dense" and/or
+ "empty" columns/rows.
+
+ stats [1]: (same as stats [0])
+
+ stats [2]: number of garbage collections performed.
+
+ stats [3]: status code. < 0 is an error code.
+ > 1 is a warning or notice.
+
+ 0 OK. Each column of the input matrix contained
+ row indices in increasing order, with no
+ duplicates.
+
+ 1 OK, but columns of input matrix were jumbled
+ (unsorted columns or duplicate entries). Symamd
+ had to do some extra work to sort the matrix
+ first and remove duplicate entries, but it
+ still was able to return a valid permutation
+ (return value of symamd was TRUE).
+
+ stats [4]: highest numbered column that
+ is unsorted or has duplicate
+ entries.
+ stats [5]: last seen duplicate or
+ unsorted row index.
+ stats [6]: number of duplicate or
+ unsorted row indices.
+
+ -1 A is a null pointer
+
+ -2 p is a null pointer
+
+ -3 (unused, see colamd.c)
+
+ -4 n is negative
+
+ stats [4]: n
+
+ -5 number of nonzeros in matrix is negative
+
+ stats [4]: # of nonzeros (p [n]).
+
+ -6 p [0] is nonzero
+
+ stats [4]: p [0]
+
+ -7 (unused)
+
+ -8 a column has a negative number of entries
+
+ stats [4]: column with < 0 entries
+ stats [5]: number of entries in col
+
+ -9 a row index is out of bounds
+
+ stats [4]: column with bad row index
+ stats [5]: bad row index
+ stats [6]: n_row, # of rows of matrx
+
+ -10 out of memory (unable to allocate temporary
+ workspace for M or count arrays using the
+ "allocate" routine passed into symamd).
+
+ Future versions may return more statistics in the stats array.
+
+ void * (*allocate) (size_t, size_t)
+
+ A pointer to a function providing memory allocation. The
+ allocated memory must be returned initialized to zero. For a
+ C application, this argument should normally be a pointer to
+ calloc. For a MATLAB mexFunction, the routine mxCalloc is
+ passed instead.
+
+ void (*release) (size_t, size_t)
+
+ A pointer to a function that frees memory allocated by the
+ memory allocation routine above. For a C application, this
+ argument should normally be a pointer to free. For a MATLAB
+ mexFunction, the routine mxFree is passed instead.
+
+
+ ----------------------------------------------------------------------------
+ colamd_report:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ colamd_report (int stats [COLAMD_STATS]) ;
+ colamd_l_report (UF_long stats [COLAMD_STATS]) ;
+
+ Purpose:
+
+ Prints the error status and statistics recorded in the stats
+ array on the standard error output (for a standard C routine)
+ or on the MATLAB output (for a mexFunction).
+
+ Arguments:
+
+ int stats [COLAMD_STATS] ; Input only. Statistics from colamd.
+
+
+ ----------------------------------------------------------------------------
+ symamd_report:
+ ----------------------------------------------------------------------------
+
+ C syntax:
+
+ #include "colamd.h"
+ symamd_report (int stats [COLAMD_STATS]) ;
+ symamd_l_report (UF_long stats [COLAMD_STATS]) ;
+
+ Purpose:
+
+ Prints the error status and statistics recorded in the stats
+ array on the standard error output (for a standard C routine)
+ or on the MATLAB output (for a mexFunction).
+
+ Arguments:
+
+ int stats [COLAMD_STATS] ; Input only. Statistics from symamd.
+
+
+*/
+
+/* ========================================================================== */
+/* === Scaffolding code definitions ======================================== */
+/* ========================================================================== */
+
+/* Ensure that debugging is turned off: */
+#ifndef NDEBUG
+#define NDEBUG
+#endif
+
+/* turn on debugging by uncommenting the following line
+ #undef NDEBUG
+*/
+
+/*
+ Our "scaffolding code" philosophy: In our opinion, well-written library
+ code should keep its "debugging" code, and just normally have it turned off
+ by the compiler so as not to interfere with performance. This serves
+ several purposes:
+
+ (1) assertions act as comments to the reader, telling you what the code
+ expects at that point. All assertions will always be true (unless
+ there really is a bug, of course).
+
+ (2) leaving in the scaffolding code assists anyone who would like to modify
+ the code, or understand the algorithm (by reading the debugging output,
+ one can get a glimpse into what the code is doing).
+
+ (3) (gasp!) for actually finding bugs. This code has been heavily tested
+ and "should" be fully functional and bug-free ... but you never know...
+
+ The code will become outrageously slow when debugging is
+ enabled. To control the level of debugging output, set an environment
+ variable D to 0 (little), 1 (some), 2, 3, or 4 (lots). When debugging,
+ you should see the following message on the standard output:
+
+ colamd: debug version, D = 1 (THIS WILL BE SLOW!)
+
+ or a similar message for symamd. If you don't, then debugging has not
+ been enabled.
+
+*/
+
+/* ========================================================================== */
+/* === Include files ======================================================== */
+/* ========================================================================== */
+
+#include "colamd.h"
+
+#if 0 /* by mao */
+#include <limits.h>
+#include <math.h>
+
+#ifdef MATLAB_MEX_FILE
+#include "mex.h"
+#include "matrix.h"
+#endif /* MATLAB_MEX_FILE */
+
+#if !defined (NPRINT) || !defined (NDEBUG)
+#include <stdio.h>
+#endif
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+#endif
+
+/* ========================================================================== */
+/* === int or UF_long ======================================================= */
+/* ========================================================================== */
+
+#if 0 /* by mao */
+/* define UF_long */
+#include "UFconfig.h"
+#endif
+
+#ifdef DLONG
+
+#define Int UF_long
+#define ID UF_long_id
+#define Int_MAX UF_long_max
+
+#define COLAMD_recommended colamd_l_recommended
+#define COLAMD_set_defaults colamd_l_set_defaults
+#define COLAMD_MAIN colamd_l
+#define SYMAMD_MAIN symamd_l
+#define COLAMD_report colamd_l_report
+#define SYMAMD_report symamd_l_report
+
+#else
+
+#define Int int
+#define ID "%d"
+#define Int_MAX INT_MAX
+
+#define COLAMD_recommended colamd_recommended
+#define COLAMD_set_defaults colamd_set_defaults
+#define COLAMD_MAIN colamd
+#define SYMAMD_MAIN symamd
+#define COLAMD_report colamd_report
+#define SYMAMD_report symamd_report
+
+#endif
+
+/* ========================================================================== */
+/* === Row and Column structures ============================================ */
+/* ========================================================================== */
+
+/* User code that makes use of the colamd/symamd routines need not directly */
+/* reference these structures. They are used only for colamd_recommended. */
+
+typedef struct Colamd_Col_struct
+{
+ Int start ; /* index for A of first row in this column, or DEAD */
+ /* if column is dead */
+ Int length ; /* number of rows in this column */
+ union
+ {
+ Int thickness ; /* number of original columns represented by this */
+ /* col, if the column is alive */
+ Int parent ; /* parent in parent tree super-column structure, if */
+ /* the column is dead */
+ } shared1 ;
+ union
+ {
+ Int score ; /* the score used to maintain heap, if col is alive */
+ Int order ; /* pivot ordering of this column, if col is dead */
+ } shared2 ;
+ union
+ {
+ Int headhash ; /* head of a hash bucket, if col is at the head of */
+ /* a degree list */
+ Int hash ; /* hash value, if col is not in a degree list */
+ Int prev ; /* previous column in degree list, if col is in a */
+ /* degree list (but not at the head of a degree list) */
+ } shared3 ;
+ union
+ {
+ Int degree_next ; /* next column, if col is in a degree list */
+ Int hash_next ; /* next column, if col is in a hash list */
+ } shared4 ;
+
+} Colamd_Col ;
+
+typedef struct Colamd_Row_struct
+{
+ Int start ; /* index for A of first col in this row */
+ Int length ; /* number of principal columns in this row */
+ union
+ {
+ Int degree ; /* number of principal & non-principal columns in row */
+ Int p ; /* used as a row pointer in init_rows_cols () */
+ } shared1 ;
+ union
+ {
+ Int mark ; /* for computing set differences and marking dead rows*/
+ Int first_column ;/* first column in row (used in garbage collection) */
+ } shared2 ;
+
+} Colamd_Row ;
+
+/* ========================================================================== */
+/* === Definitions ========================================================== */
+/* ========================================================================== */
+
+/* Routines are either PUBLIC (user-callable) or PRIVATE (not user-callable) */
+#define PUBLIC
+#define PRIVATE static
+
+#define DENSE_DEGREE(alpha,n) \
+ ((Int) MAX (16.0, (alpha) * sqrt ((double) (n))))
+
+#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
+
+#define ONES_COMPLEMENT(r) (-(r)-1)
+
+/* -------------------------------------------------------------------------- */
+/* Change for version 2.1: define TRUE and FALSE only if not yet defined */
+/* -------------------------------------------------------------------------- */
+
+#ifndef TRUE
+#define TRUE (1)
+#endif
+
+#ifndef FALSE
+#define FALSE (0)
+#endif
+
+/* -------------------------------------------------------------------------- */
+
+#define EMPTY (-1)
+
+/* Row and column status */
+#define ALIVE (0)
+#define DEAD (-1)
+
+/* Column status */
+#define DEAD_PRINCIPAL (-1)
+#define DEAD_NON_PRINCIPAL (-2)
+
+/* Macros for row and column status update and checking. */
+#define ROW_IS_DEAD(r) ROW_IS_MARKED_DEAD (Row[r].shared2.mark)
+#define ROW_IS_MARKED_DEAD(row_mark) (row_mark < ALIVE)
+#define ROW_IS_ALIVE(r) (Row [r].shared2.mark >= ALIVE)
+#define COL_IS_DEAD(c) (Col [c].start < ALIVE)
+#define COL_IS_ALIVE(c) (Col [c].start >= ALIVE)
+#define COL_IS_DEAD_PRINCIPAL(c) (Col [c].start == DEAD_PRINCIPAL)
+#define KILL_ROW(r) { Row [r].shared2.mark = DEAD ; }
+#define KILL_PRINCIPAL_COL(c) { Col [c].start = DEAD_PRINCIPAL ; }
+#define KILL_NON_PRINCIPAL_COL(c) { Col [c].start = DEAD_NON_PRINCIPAL ; }
+
+/* ========================================================================== */
+/* === Colamd reporting mechanism =========================================== */
+/* ========================================================================== */
+
+#if defined (MATLAB_MEX_FILE) || defined (MATHWORKS)
+/* In MATLAB, matrices are 1-based to the user, but 0-based internally */
+#define INDEX(i) ((i)+1)
+#else
+/* In C, matrices are 0-based and indices are reported as such in *_report */
+#define INDEX(i) (i)
+#endif
+
+/* All output goes through the PRINTF macro. */
+#define PRINTF(params) { if (colamd_printf != NULL) (void) colamd_printf params ; }
+
+/* ========================================================================== */
+/* === Prototypes of PRIVATE routines ======================================= */
+/* ========================================================================== */
+
+PRIVATE Int init_rows_cols
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int p [],
+ Int stats [COLAMD_STATS]
+) ;
+
+PRIVATE void init_scoring
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int head [],
+ double knobs [COLAMD_KNOBS],
+ Int *p_n_row2,
+ Int *p_n_col2,
+ Int *p_max_deg
+) ;
+
+PRIVATE Int find_ordering
+(
+ Int n_row,
+ Int n_col,
+ Int Alen,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int head [],
+ Int n_col2,
+ Int max_deg,
+ Int pfree,
+ Int aggressive
+) ;
+
+PRIVATE void order_children
+(
+ Int n_col,
+ Colamd_Col Col [],
+ Int p []
+) ;
+
+PRIVATE void detect_super_cols
+(
+
+#ifndef NDEBUG
+ Int n_col,
+ Colamd_Row Row [],
+#endif /* NDEBUG */
+
+ Colamd_Col Col [],
+ Int A [],
+ Int head [],
+ Int row_start,
+ Int row_length
+) ;
+
+PRIVATE Int garbage_collection
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int *pfree
+) ;
+
+PRIVATE Int clear_mark
+(
+ Int tag_mark,
+ Int max_mark,
+ Int n_row,
+ Colamd_Row Row []
+) ;
+
+PRIVATE void print_report
+(
+ char *method,
+ Int stats [COLAMD_STATS]
+) ;
+
+/* ========================================================================== */
+/* === Debugging prototypes and definitions ================================= */
+/* ========================================================================== */
+
+#ifndef NDEBUG
+
+#if 0 /* by mao */
+#include <assert.h>
+#endif
+
+/* colamd_debug is the *ONLY* global variable, and is only */
+/* present when debugging */
+
+PRIVATE Int colamd_debug = 0 ; /* debug print level */
+
+#define DEBUG0(params) { PRINTF (params) ; }
+#define DEBUG1(params) { if (colamd_debug >= 1) PRINTF (params) ; }
+#define DEBUG2(params) { if (colamd_debug >= 2) PRINTF (params) ; }
+#define DEBUG3(params) { if (colamd_debug >= 3) PRINTF (params) ; }
+#define DEBUG4(params) { if (colamd_debug >= 4) PRINTF (params) ; }
+
+#if 0 /* by mao */
+#ifdef MATLAB_MEX_FILE
+#define ASSERT(expression) (mxAssert ((expression), ""))
+#else
+#define ASSERT(expression) (assert (expression))
+#endif /* MATLAB_MEX_FILE */
+#else
+#define ASSERT xassert
+#endif
+
+PRIVATE void colamd_get_debug /* gets the debug print level from getenv */
+(
+ char *method
+) ;
+
+PRIVATE void debug_deg_lists
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int head [],
+ Int min_score,
+ Int should,
+ Int max_deg
+) ;
+
+PRIVATE void debug_mark
+(
+ Int n_row,
+ Colamd_Row Row [],
+ Int tag_mark,
+ Int max_mark
+) ;
+
+PRIVATE void debug_matrix
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A []
+) ;
+
+PRIVATE void debug_structures
+(
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int n_col2
+) ;
+
+#else /* NDEBUG */
+
+/* === No debugging ========================================================= */
+
+#define DEBUG0(params) ;
+#define DEBUG1(params) ;
+#define DEBUG2(params) ;
+#define DEBUG3(params) ;
+#define DEBUG4(params) ;
+
+#define ASSERT(expression)
+
+#endif /* NDEBUG */
+
+/* ========================================================================== */
+/* === USER-CALLABLE ROUTINES: ============================================== */
+/* ========================================================================== */
+
+/* ========================================================================== */
+/* === colamd_recommended =================================================== */
+/* ========================================================================== */
+
+/*
+ The colamd_recommended routine returns the suggested size for Alen. This
+ value has been determined to provide good balance between the number of
+ garbage collections and the memory requirements for colamd. If any
+ argument is negative, or if integer overflow occurs, a 0 is returned as an
+ error condition. 2*nnz space is required for the row and column
+ indices of the matrix. COLAMD_C (n_col) + COLAMD_R (n_row) space is
+ required for the Col and Row arrays, respectively, which are internal to
+ colamd (roughly 6*n_col + 4*n_row). An additional n_col space is the
+ minimal amount of "elbow room", and nnz/5 more space is recommended for
+ run time efficiency.
+
+ Alen is approximately 2.2*nnz + 7*n_col + 4*n_row + 10.
+
+ This function is not needed when using symamd.
+*/
+
+/* add two values of type size_t, and check for integer overflow */
+static size_t t_add (size_t a, size_t b, int *ok)
+{
+ (*ok) = (*ok) && ((a + b) >= MAX (a,b)) ;
+ return ((*ok) ? (a + b) : 0) ;
+}
+
+/* compute a*k where k is a small integer, and check for integer overflow */
+static size_t t_mult (size_t a, size_t k, int *ok)
+{
+ size_t i, s = 0 ;
+ for (i = 0 ; i < k ; i++)
+ {
+ s = t_add (s, a, ok) ;
+ }
+ return (s) ;
+}
+
+/* size of the Col and Row structures */
+#define COLAMD_C(n_col,ok) \
+ ((t_mult (t_add (n_col, 1, ok), sizeof (Colamd_Col), ok) / sizeof (Int)))
+
+#define COLAMD_R(n_row,ok) \
+ ((t_mult (t_add (n_row, 1, ok), sizeof (Colamd_Row), ok) / sizeof (Int)))
+
+
+PUBLIC size_t COLAMD_recommended /* returns recommended value of Alen. */
+(
+ /* === Parameters ======================================================= */
+
+ Int nnz, /* number of nonzeros in A */
+ Int n_row, /* number of rows in A */
+ Int n_col /* number of columns in A */
+)
+{
+ size_t s, c, r ;
+ int ok = TRUE ;
+ if (nnz < 0 || n_row < 0 || n_col < 0)
+ {
+ return (0) ;
+ }
+ s = t_mult (nnz, 2, &ok) ; /* 2*nnz */
+ c = COLAMD_C (n_col, &ok) ; /* size of column structures */
+ r = COLAMD_R (n_row, &ok) ; /* size of row structures */
+ s = t_add (s, c, &ok) ;
+ s = t_add (s, r, &ok) ;
+ s = t_add (s, n_col, &ok) ; /* elbow room */
+ s = t_add (s, nnz/5, &ok) ; /* elbow room */
+ ok = ok && (s < Int_MAX) ;
+ return (ok ? s : 0) ;
+}
+
+
+/* ========================================================================== */
+/* === colamd_set_defaults ================================================== */
+/* ========================================================================== */
+
+/*
+ The colamd_set_defaults routine sets the default values of the user-
+ controllable parameters for colamd and symamd:
+
+ Colamd: rows with more than max (16, knobs [0] * sqrt (n_col))
+ entries are removed prior to ordering. Columns with more than
+ max (16, knobs [1] * sqrt (MIN (n_row,n_col))) entries are removed
+ prior to ordering, and placed last in the output column ordering.
+
+ Symamd: Rows and columns with more than max (16, knobs [0] * sqrt (n))
+ entries are removed prior to ordering, and placed last in the
+ output ordering.
+
+ knobs [0] dense row control
+
+ knobs [1] dense column control
+
+ knobs [2] if nonzero, do aggresive absorption
+
+ knobs [3..19] unused, but future versions might use this
+
+*/
+
+PUBLIC void COLAMD_set_defaults
+(
+ /* === Parameters ======================================================= */
+
+ double knobs [COLAMD_KNOBS] /* knob array */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int i ;
+
+ if (!knobs)
+ {
+ return ; /* no knobs to initialize */
+ }
+ for (i = 0 ; i < COLAMD_KNOBS ; i++)
+ {
+ knobs [i] = 0 ;
+ }
+ knobs [COLAMD_DENSE_ROW] = 10 ;
+ knobs [COLAMD_DENSE_COL] = 10 ;
+ knobs [COLAMD_AGGRESSIVE] = TRUE ; /* default: do aggressive absorption*/
+}
+
+
+/* ========================================================================== */
+/* === symamd =============================================================== */
+/* ========================================================================== */
+
+PUBLIC Int SYMAMD_MAIN /* return TRUE if OK, FALSE otherwise */
+(
+ /* === Parameters ======================================================= */
+
+ Int n, /* number of rows and columns of A */
+ Int A [], /* row indices of A */
+ Int p [], /* column pointers of A */
+ Int perm [], /* output permutation, size n+1 */
+ double knobs [COLAMD_KNOBS], /* parameters (uses defaults if NULL) */
+ Int stats [COLAMD_STATS], /* output statistics and error codes */
+ void * (*allocate) (size_t, size_t),
+ /* pointer to calloc (ANSI C) or */
+ /* mxCalloc (for MATLAB mexFunction) */
+ void (*release) (void *)
+ /* pointer to free (ANSI C) or */
+ /* mxFree (for MATLAB mexFunction) */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int *count ; /* length of each column of M, and col pointer*/
+ Int *mark ; /* mark array for finding duplicate entries */
+ Int *M ; /* row indices of matrix M */
+ size_t Mlen ; /* length of M */
+ Int n_row ; /* number of rows in M */
+ Int nnz ; /* number of entries in A */
+ Int i ; /* row index of A */
+ Int j ; /* column index of A */
+ Int k ; /* row index of M */
+ Int mnz ; /* number of nonzeros in M */
+ Int pp ; /* index into a column of A */
+ Int last_row ; /* last row seen in the current column */
+ Int length ; /* number of nonzeros in a column */
+
+ double cknobs [COLAMD_KNOBS] ; /* knobs for colamd */
+ double default_knobs [COLAMD_KNOBS] ; /* default knobs for colamd */
+
+#ifndef NDEBUG
+ colamd_get_debug ("symamd") ;
+#endif /* NDEBUG */
+
+ /* === Check the input arguments ======================================== */
+
+ if (!stats)
+ {
+ DEBUG0 (("symamd: stats not present\n")) ;
+ return (FALSE) ;
+ }
+ for (i = 0 ; i < COLAMD_STATS ; i++)
+ {
+ stats [i] = 0 ;
+ }
+ stats [COLAMD_STATUS] = COLAMD_OK ;
+ stats [COLAMD_INFO1] = -1 ;
+ stats [COLAMD_INFO2] = -1 ;
+
+ if (!A)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_A_not_present ;
+ DEBUG0 (("symamd: A not present\n")) ;
+ return (FALSE) ;
+ }
+
+ if (!p) /* p is not present */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_p_not_present ;
+ DEBUG0 (("symamd: p not present\n")) ;
+ return (FALSE) ;
+ }
+
+ if (n < 0) /* n must be >= 0 */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_ncol_negative ;
+ stats [COLAMD_INFO1] = n ;
+ DEBUG0 (("symamd: n negative %d\n", n)) ;
+ return (FALSE) ;
+ }
+
+ nnz = p [n] ;
+ if (nnz < 0) /* nnz must be >= 0 */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_nnz_negative ;
+ stats [COLAMD_INFO1] = nnz ;
+ DEBUG0 (("symamd: number of entries negative %d\n", nnz)) ;
+ return (FALSE) ;
+ }
+
+ if (p [0] != 0)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_p0_nonzero ;
+ stats [COLAMD_INFO1] = p [0] ;
+ DEBUG0 (("symamd: p[0] not zero %d\n", p [0])) ;
+ return (FALSE) ;
+ }
+
+ /* === If no knobs, set default knobs =================================== */
+
+ if (!knobs)
+ {
+ COLAMD_set_defaults (default_knobs) ;
+ knobs = default_knobs ;
+ }
+
+ /* === Allocate count and mark ========================================== */
+
+ count = (Int *) ((*allocate) (n+1, sizeof (Int))) ;
+ if (!count)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_out_of_memory ;
+ DEBUG0 (("symamd: allocate count (size %d) failed\n", n+1)) ;
+ return (FALSE) ;
+ }
+
+ mark = (Int *) ((*allocate) (n+1, sizeof (Int))) ;
+ if (!mark)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_out_of_memory ;
+ (*release) ((void *) count) ;
+ DEBUG0 (("symamd: allocate mark (size %d) failed\n", n+1)) ;
+ return (FALSE) ;
+ }
+
+ /* === Compute column counts of M, check if A is valid ================== */
+
+ stats [COLAMD_INFO3] = 0 ; /* number of duplicate or unsorted row indices*/
+
+ for (i = 0 ; i < n ; i++)
+ {
+ mark [i] = -1 ;
+ }
+
+ for (j = 0 ; j < n ; j++)
+ {
+ last_row = -1 ;
+
+ length = p [j+1] - p [j] ;
+ if (length < 0)
+ {
+ /* column pointers must be non-decreasing */
+ stats [COLAMD_STATUS] = COLAMD_ERROR_col_length_negative ;
+ stats [COLAMD_INFO1] = j ;
+ stats [COLAMD_INFO2] = length ;
+ (*release) ((void *) count) ;
+ (*release) ((void *) mark) ;
+ DEBUG0 (("symamd: col %d negative length %d\n", j, length)) ;
+ return (FALSE) ;
+ }
+
+ for (pp = p [j] ; pp < p [j+1] ; pp++)
+ {
+ i = A [pp] ;
+ if (i < 0 || i >= n)
+ {
+ /* row index i, in column j, is out of bounds */
+ stats [COLAMD_STATUS] = COLAMD_ERROR_row_index_out_of_bounds ;
+ stats [COLAMD_INFO1] = j ;
+ stats [COLAMD_INFO2] = i ;
+ stats [COLAMD_INFO3] = n ;
+ (*release) ((void *) count) ;
+ (*release) ((void *) mark) ;
+ DEBUG0 (("symamd: row %d col %d out of bounds\n", i, j)) ;
+ return (FALSE) ;
+ }
+
+ if (i <= last_row || mark [i] == j)
+ {
+ /* row index is unsorted or repeated (or both), thus col */
+ /* is jumbled. This is a notice, not an error condition. */
+ stats [COLAMD_STATUS] = COLAMD_OK_BUT_JUMBLED ;
+ stats [COLAMD_INFO1] = j ;
+ stats [COLAMD_INFO2] = i ;
+ (stats [COLAMD_INFO3]) ++ ;
+ DEBUG1 (("symamd: row %d col %d unsorted/duplicate\n", i, j)) ;
+ }
+
+ if (i > j && mark [i] != j)
+ {
+ /* row k of M will contain column indices i and j */
+ count [i]++ ;
+ count [j]++ ;
+ }
+
+ /* mark the row as having been seen in this column */
+ mark [i] = j ;
+
+ last_row = i ;
+ }
+ }
+
+ /* v2.4: removed free(mark) */
+
+ /* === Compute column pointers of M ===================================== */
+
+ /* use output permutation, perm, for column pointers of M */
+ perm [0] = 0 ;
+ for (j = 1 ; j <= n ; j++)
+ {
+ perm [j] = perm [j-1] + count [j-1] ;
+ }
+ for (j = 0 ; j < n ; j++)
+ {
+ count [j] = perm [j] ;
+ }
+
+ /* === Construct M ====================================================== */
+
+ mnz = perm [n] ;
+ n_row = mnz / 2 ;
+ Mlen = COLAMD_recommended (mnz, n_row, n) ;
+ M = (Int *) ((*allocate) (Mlen, sizeof (Int))) ;
+ DEBUG0 (("symamd: M is %d-by-%d with %d entries, Mlen = %g\n",
+ n_row, n, mnz, (double) Mlen)) ;
+
+ if (!M)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_out_of_memory ;
+ (*release) ((void *) count) ;
+ (*release) ((void *) mark) ;
+ DEBUG0 (("symamd: allocate M (size %g) failed\n", (double) Mlen)) ;
+ return (FALSE) ;
+ }
+
+ k = 0 ;
+
+ if (stats [COLAMD_STATUS] == COLAMD_OK)
+ {
+ /* Matrix is OK */
+ for (j = 0 ; j < n ; j++)
+ {
+ ASSERT (p [j+1] - p [j] >= 0) ;
+ for (pp = p [j] ; pp < p [j+1] ; pp++)
+ {
+ i = A [pp] ;
+ ASSERT (i >= 0 && i < n) ;
+ if (i > j)
+ {
+ /* row k of M contains column indices i and j */
+ M [count [i]++] = k ;
+ M [count [j]++] = k ;
+ k++ ;
+ }
+ }
+ }
+ }
+ else
+ {
+ /* Matrix is jumbled. Do not add duplicates to M. Unsorted cols OK. */
+ DEBUG0 (("symamd: Duplicates in A.\n")) ;
+ for (i = 0 ; i < n ; i++)
+ {
+ mark [i] = -1 ;
+ }
+ for (j = 0 ; j < n ; j++)
+ {
+ ASSERT (p [j+1] - p [j] >= 0) ;
+ for (pp = p [j] ; pp < p [j+1] ; pp++)
+ {
+ i = A [pp] ;
+ ASSERT (i >= 0 && i < n) ;
+ if (i > j && mark [i] != j)
+ {
+ /* row k of M contains column indices i and j */
+ M [count [i]++] = k ;
+ M [count [j]++] = k ;
+ k++ ;
+ mark [i] = j ;
+ }
+ }
+ }
+ /* v2.4: free(mark) moved below */
+ }
+
+ /* count and mark no longer needed */
+ (*release) ((void *) count) ;
+ (*release) ((void *) mark) ; /* v2.4: free (mark) moved here */
+ ASSERT (k == n_row) ;
+
+ /* === Adjust the knobs for M =========================================== */
+
+ for (i = 0 ; i < COLAMD_KNOBS ; i++)
+ {
+ cknobs [i] = knobs [i] ;
+ }
+
+ /* there are no dense rows in M */
+ cknobs [COLAMD_DENSE_ROW] = -1 ;
+ cknobs [COLAMD_DENSE_COL] = knobs [COLAMD_DENSE_ROW] ;
+
+ /* === Order the columns of M =========================================== */
+
+ /* v2.4: colamd cannot fail here, so the error check is removed */
+ (void) COLAMD_MAIN (n_row, n, (Int) Mlen, M, perm, cknobs, stats) ;
+
+ /* Note that the output permutation is now in perm */
+
+ /* === get the statistics for symamd from colamd ======================== */
+
+ /* a dense column in colamd means a dense row and col in symamd */
+ stats [COLAMD_DENSE_ROW] = stats [COLAMD_DENSE_COL] ;
+
+ /* === Free M =========================================================== */
+
+ (*release) ((void *) M) ;
+ DEBUG0 (("symamd: done.\n")) ;
+ return (TRUE) ;
+
+}
+
+/* ========================================================================== */
+/* === colamd =============================================================== */
+/* ========================================================================== */
+
+/*
+ The colamd routine computes a column ordering Q of a sparse matrix
+ A such that the LU factorization P(AQ) = LU remains sparse, where P is
+ selected via partial pivoting. The routine can also be viewed as
+ providing a permutation Q such that the Cholesky factorization
+ (AQ)'(AQ) = LL' remains sparse.
+*/
+
+PUBLIC Int COLAMD_MAIN /* returns TRUE if successful, FALSE otherwise*/
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row, /* number of rows in A */
+ Int n_col, /* number of columns in A */
+ Int Alen, /* length of A */
+ Int A [], /* row indices of A */
+ Int p [], /* pointers to columns in A */
+ double knobs [COLAMD_KNOBS],/* parameters (uses defaults if NULL) */
+ Int stats [COLAMD_STATS] /* output statistics and error codes */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int i ; /* loop index */
+ Int nnz ; /* nonzeros in A */
+ size_t Row_size ; /* size of Row [], in integers */
+ size_t Col_size ; /* size of Col [], in integers */
+ size_t need ; /* minimum required length of A */
+ Colamd_Row *Row ; /* pointer into A of Row [0..n_row] array */
+ Colamd_Col *Col ; /* pointer into A of Col [0..n_col] array */
+ Int n_col2 ; /* number of non-dense, non-empty columns */
+ Int n_row2 ; /* number of non-dense, non-empty rows */
+ Int ngarbage ; /* number of garbage collections performed */
+ Int max_deg ; /* maximum row degree */
+ double default_knobs [COLAMD_KNOBS] ; /* default knobs array */
+ Int aggressive ; /* do aggressive absorption */
+ int ok ;
+
+#ifndef NDEBUG
+ colamd_get_debug ("colamd") ;
+#endif /* NDEBUG */
+
+ /* === Check the input arguments ======================================== */
+
+ if (!stats)
+ {
+ DEBUG0 (("colamd: stats not present\n")) ;
+ return (FALSE) ;
+ }
+ for (i = 0 ; i < COLAMD_STATS ; i++)
+ {
+ stats [i] = 0 ;
+ }
+ stats [COLAMD_STATUS] = COLAMD_OK ;
+ stats [COLAMD_INFO1] = -1 ;
+ stats [COLAMD_INFO2] = -1 ;
+
+ if (!A) /* A is not present */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_A_not_present ;
+ DEBUG0 (("colamd: A not present\n")) ;
+ return (FALSE) ;
+ }
+
+ if (!p) /* p is not present */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_p_not_present ;
+ DEBUG0 (("colamd: p not present\n")) ;
+ return (FALSE) ;
+ }
+
+ if (n_row < 0) /* n_row must be >= 0 */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_nrow_negative ;
+ stats [COLAMD_INFO1] = n_row ;
+ DEBUG0 (("colamd: nrow negative %d\n", n_row)) ;
+ return (FALSE) ;
+ }
+
+ if (n_col < 0) /* n_col must be >= 0 */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_ncol_negative ;
+ stats [COLAMD_INFO1] = n_col ;
+ DEBUG0 (("colamd: ncol negative %d\n", n_col)) ;
+ return (FALSE) ;
+ }
+
+ nnz = p [n_col] ;
+ if (nnz < 0) /* nnz must be >= 0 */
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_nnz_negative ;
+ stats [COLAMD_INFO1] = nnz ;
+ DEBUG0 (("colamd: number of entries negative %d\n", nnz)) ;
+ return (FALSE) ;
+ }
+
+ if (p [0] != 0)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_p0_nonzero ;
+ stats [COLAMD_INFO1] = p [0] ;
+ DEBUG0 (("colamd: p[0] not zero %d\n", p [0])) ;
+ return (FALSE) ;
+ }
+
+ /* === If no knobs, set default knobs =================================== */
+
+ if (!knobs)
+ {
+ COLAMD_set_defaults (default_knobs) ;
+ knobs = default_knobs ;
+ }
+
+ aggressive = (knobs [COLAMD_AGGRESSIVE] != FALSE) ;
+
+ /* === Allocate the Row and Col arrays from array A ===================== */
+
+ ok = TRUE ;
+ Col_size = COLAMD_C (n_col, &ok) ; /* size of Col array of structs */
+ Row_size = COLAMD_R (n_row, &ok) ; /* size of Row array of structs */
+
+ /* need = 2*nnz + n_col + Col_size + Row_size ; */
+ need = t_mult (nnz, 2, &ok) ;
+ need = t_add (need, n_col, &ok) ;
+ need = t_add (need, Col_size, &ok) ;
+ need = t_add (need, Row_size, &ok) ;
+
+ if (!ok || need > (size_t) Alen || need > Int_MAX)
+ {
+ /* not enough space in array A to perform the ordering */
+ stats [COLAMD_STATUS] = COLAMD_ERROR_A_too_small ;
+ stats [COLAMD_INFO1] = need ;
+ stats [COLAMD_INFO2] = Alen ;
+ DEBUG0 (("colamd: Need Alen >= %d, given only Alen = %d\n", need,Alen));
+ return (FALSE) ;
+ }
+
+ Alen -= Col_size + Row_size ;
+ Col = (Colamd_Col *) &A [Alen] ;
+ Row = (Colamd_Row *) &A [Alen + Col_size] ;
+
+ /* === Construct the row and column data structures ===================== */
+
+ if (!init_rows_cols (n_row, n_col, Row, Col, A, p, stats))
+ {
+ /* input matrix is invalid */
+ DEBUG0 (("colamd: Matrix invalid\n")) ;
+ return (FALSE) ;
+ }
+
+ /* === Initialize scores, kill dense rows/columns ======================= */
+
+ init_scoring (n_row, n_col, Row, Col, A, p, knobs,
+ &n_row2, &n_col2, &max_deg) ;
+
+ /* === Order the supercolumns =========================================== */
+
+ ngarbage = find_ordering (n_row, n_col, Alen, Row, Col, A, p,
+ n_col2, max_deg, 2*nnz, aggressive) ;
+
+ /* === Order the non-principal columns ================================== */
+
+ order_children (n_col, Col, p) ;
+
+ /* === Return statistics in stats ======================================= */
+
+ stats [COLAMD_DENSE_ROW] = n_row - n_row2 ;
+ stats [COLAMD_DENSE_COL] = n_col - n_col2 ;
+ stats [COLAMD_DEFRAG_COUNT] = ngarbage ;
+ DEBUG0 (("colamd: done.\n")) ;
+ return (TRUE) ;
+}
+
+
+/* ========================================================================== */
+/* === colamd_report ======================================================== */
+/* ========================================================================== */
+
+PUBLIC void COLAMD_report
+(
+ Int stats [COLAMD_STATS]
+)
+{
+ print_report ("colamd", stats) ;
+}
+
+
+/* ========================================================================== */
+/* === symamd_report ======================================================== */
+/* ========================================================================== */
+
+PUBLIC void SYMAMD_report
+(
+ Int stats [COLAMD_STATS]
+)
+{
+ print_report ("symamd", stats) ;
+}
+
+
+
+/* ========================================================================== */
+/* === NON-USER-CALLABLE ROUTINES: ========================================== */
+/* ========================================================================== */
+
+/* There are no user-callable routines beyond this point in the file */
+
+
+/* ========================================================================== */
+/* === init_rows_cols ======================================================= */
+/* ========================================================================== */
+
+/*
+ Takes the column form of the matrix in A and creates the row form of the
+ matrix. Also, row and column attributes are stored in the Col and Row
+ structs. If the columns are un-sorted or contain duplicate row indices,
+ this routine will also sort and remove duplicate row indices from the
+ column form of the matrix. Returns FALSE if the matrix is invalid,
+ TRUE otherwise. Not user-callable.
+*/
+
+PRIVATE Int init_rows_cols /* returns TRUE if OK, or FALSE otherwise */
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row, /* number of rows of A */
+ Int n_col, /* number of columns of A */
+ Colamd_Row Row [], /* of size n_row+1 */
+ Colamd_Col Col [], /* of size n_col+1 */
+ Int A [], /* row indices of A, of size Alen */
+ Int p [], /* pointers to columns in A, of size n_col+1 */
+ Int stats [COLAMD_STATS] /* colamd statistics */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int col ; /* a column index */
+ Int row ; /* a row index */
+ Int *cp ; /* a column pointer */
+ Int *cp_end ; /* a pointer to the end of a column */
+ Int *rp ; /* a row pointer */
+ Int *rp_end ; /* a pointer to the end of a row */
+ Int last_row ; /* previous row */
+
+ /* === Initialize columns, and check column pointers ==================== */
+
+ for (col = 0 ; col < n_col ; col++)
+ {
+ Col [col].start = p [col] ;
+ Col [col].length = p [col+1] - p [col] ;
+
+ if (Col [col].length < 0)
+ {
+ /* column pointers must be non-decreasing */
+ stats [COLAMD_STATUS] = COLAMD_ERROR_col_length_negative ;
+ stats [COLAMD_INFO1] = col ;
+ stats [COLAMD_INFO2] = Col [col].length ;
+ DEBUG0 (("colamd: col %d length %d < 0\n", col, Col [col].length)) ;
+ return (FALSE) ;
+ }
+
+ Col [col].shared1.thickness = 1 ;
+ Col [col].shared2.score = 0 ;
+ Col [col].shared3.prev = EMPTY ;
+ Col [col].shared4.degree_next = EMPTY ;
+ }
+
+ /* p [0..n_col] no longer needed, used as "head" in subsequent routines */
+
+ /* === Scan columns, compute row degrees, and check row indices ========= */
+
+ stats [COLAMD_INFO3] = 0 ; /* number of duplicate or unsorted row indices*/
+
+ for (row = 0 ; row < n_row ; row++)
+ {
+ Row [row].length = 0 ;
+ Row [row].shared2.mark = -1 ;
+ }
+
+ for (col = 0 ; col < n_col ; col++)
+ {
+ last_row = -1 ;
+
+ cp = &A [p [col]] ;
+ cp_end = &A [p [col+1]] ;
+
+ while (cp < cp_end)
+ {
+ row = *cp++ ;
+
+ /* make sure row indices within range */
+ if (row < 0 || row >= n_row)
+ {
+ stats [COLAMD_STATUS] = COLAMD_ERROR_row_index_out_of_bounds ;
+ stats [COLAMD_INFO1] = col ;
+ stats [COLAMD_INFO2] = row ;
+ stats [COLAMD_INFO3] = n_row ;
+ DEBUG0 (("colamd: row %d col %d out of bounds\n", row, col)) ;
+ return (FALSE) ;
+ }
+
+ if (row <= last_row || Row [row].shared2.mark == col)
+ {
+ /* row index are unsorted or repeated (or both), thus col */
+ /* is jumbled. This is a notice, not an error condition. */
+ stats [COLAMD_STATUS] = COLAMD_OK_BUT_JUMBLED ;
+ stats [COLAMD_INFO1] = col ;
+ stats [COLAMD_INFO2] = row ;
+ (stats [COLAMD_INFO3]) ++ ;
+ DEBUG1 (("colamd: row %d col %d unsorted/duplicate\n",row,col));
+ }
+
+ if (Row [row].shared2.mark != col)
+ {
+ Row [row].length++ ;
+ }
+ else
+ {
+ /* this is a repeated entry in the column, */
+ /* it will be removed */
+ Col [col].length-- ;
+ }
+
+ /* mark the row as having been seen in this column */
+ Row [row].shared2.mark = col ;
+
+ last_row = row ;
+ }
+ }
+
+ /* === Compute row pointers ============================================= */
+
+ /* row form of the matrix starts directly after the column */
+ /* form of matrix in A */
+ Row [0].start = p [n_col] ;
+ Row [0].shared1.p = Row [0].start ;
+ Row [0].shared2.mark = -1 ;
+ for (row = 1 ; row < n_row ; row++)
+ {
+ Row [row].start = Row [row-1].start + Row [row-1].length ;
+ Row [row].shared1.p = Row [row].start ;
+ Row [row].shared2.mark = -1 ;
+ }
+
+ /* === Create row form ================================================== */
+
+ if (stats [COLAMD_STATUS] == COLAMD_OK_BUT_JUMBLED)
+ {
+ /* if cols jumbled, watch for repeated row indices */
+ for (col = 0 ; col < n_col ; col++)
+ {
+ cp = &A [p [col]] ;
+ cp_end = &A [p [col+1]] ;
+ while (cp < cp_end)
+ {
+ row = *cp++ ;
+ if (Row [row].shared2.mark != col)
+ {
+ A [(Row [row].shared1.p)++] = col ;
+ Row [row].shared2.mark = col ;
+ }
+ }
+ }
+ }
+ else
+ {
+ /* if cols not jumbled, we don't need the mark (this is faster) */
+ for (col = 0 ; col < n_col ; col++)
+ {
+ cp = &A [p [col]] ;
+ cp_end = &A [p [col+1]] ;
+ while (cp < cp_end)
+ {
+ A [(Row [*cp++].shared1.p)++] = col ;
+ }
+ }
+ }
+
+ /* === Clear the row marks and set row degrees ========================== */
+
+ for (row = 0 ; row < n_row ; row++)
+ {
+ Row [row].shared2.mark = 0 ;
+ Row [row].shared1.degree = Row [row].length ;
+ }
+
+ /* === See if we need to re-create columns ============================== */
+
+ if (stats [COLAMD_STATUS] == COLAMD_OK_BUT_JUMBLED)
+ {
+ DEBUG0 (("colamd: reconstructing column form, matrix jumbled\n")) ;
+
+#ifndef NDEBUG
+ /* make sure column lengths are correct */
+ for (col = 0 ; col < n_col ; col++)
+ {
+ p [col] = Col [col].length ;
+ }
+ for (row = 0 ; row < n_row ; row++)
+ {
+ rp = &A [Row [row].start] ;
+ rp_end = rp + Row [row].length ;
+ while (rp < rp_end)
+ {
+ p [*rp++]-- ;
+ }
+ }
+ for (col = 0 ; col < n_col ; col++)
+ {
+ ASSERT (p [col] == 0) ;
+ }
+ /* now p is all zero (different than when debugging is turned off) */
+#endif /* NDEBUG */
+
+ /* === Compute col pointers ========================================= */
+
+ /* col form of the matrix starts at A [0]. */
+ /* Note, we may have a gap between the col form and the row */
+ /* form if there were duplicate entries, if so, it will be */
+ /* removed upon the first garbage collection */
+ Col [0].start = 0 ;
+ p [0] = Col [0].start ;
+ for (col = 1 ; col < n_col ; col++)
+ {
+ /* note that the lengths here are for pruned columns, i.e. */
+ /* no duplicate row indices will exist for these columns */
+ Col [col].start = Col [col-1].start + Col [col-1].length ;
+ p [col] = Col [col].start ;
+ }
+
+ /* === Re-create col form =========================================== */
+
+ for (row = 0 ; row < n_row ; row++)
+ {
+ rp = &A [Row [row].start] ;
+ rp_end = rp + Row [row].length ;
+ while (rp < rp_end)
+ {
+ A [(p [*rp++])++] = row ;
+ }
+ }
+ }
+
+ /* === Done. Matrix is not (or no longer) jumbled ====================== */
+
+ return (TRUE) ;
+}
+
+
+/* ========================================================================== */
+/* === init_scoring ========================================================= */
+/* ========================================================================== */
+
+/*
+ Kills dense or empty columns and rows, calculates an initial score for
+ each column, and places all columns in the degree lists. Not user-callable.
+*/
+
+PRIVATE void init_scoring
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row, /* number of rows of A */
+ Int n_col, /* number of columns of A */
+ Colamd_Row Row [], /* of size n_row+1 */
+ Colamd_Col Col [], /* of size n_col+1 */
+ Int A [], /* column form and row form of A */
+ Int head [], /* of size n_col+1 */
+ double knobs [COLAMD_KNOBS],/* parameters */
+ Int *p_n_row2, /* number of non-dense, non-empty rows */
+ Int *p_n_col2, /* number of non-dense, non-empty columns */
+ Int *p_max_deg /* maximum row degree */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int c ; /* a column index */
+ Int r, row ; /* a row index */
+ Int *cp ; /* a column pointer */
+ Int deg ; /* degree of a row or column */
+ Int *cp_end ; /* a pointer to the end of a column */
+ Int *new_cp ; /* new column pointer */
+ Int col_length ; /* length of pruned column */
+ Int score ; /* current column score */
+ Int n_col2 ; /* number of non-dense, non-empty columns */
+ Int n_row2 ; /* number of non-dense, non-empty rows */
+ Int dense_row_count ; /* remove rows with more entries than this */
+ Int dense_col_count ; /* remove cols with more entries than this */
+ Int min_score ; /* smallest column score */
+ Int max_deg ; /* maximum row degree */
+ Int next_col ; /* Used to add to degree list.*/
+
+#ifndef NDEBUG
+ Int debug_count ; /* debug only. */
+#endif /* NDEBUG */
+
+ /* === Extract knobs ==================================================== */
+
+ /* Note: if knobs contains a NaN, this is undefined: */
+ if (knobs [COLAMD_DENSE_ROW] < 0)
+ {
+ /* only remove completely dense rows */
+ dense_row_count = n_col-1 ;
+ }
+ else
+ {
+ dense_row_count = DENSE_DEGREE (knobs [COLAMD_DENSE_ROW], n_col) ;
+ }
+ if (knobs [COLAMD_DENSE_COL] < 0)
+ {
+ /* only remove completely dense columns */
+ dense_col_count = n_row-1 ;
+ }
+ else
+ {
+ dense_col_count =
+ DENSE_DEGREE (knobs [COLAMD_DENSE_COL], MIN (n_row, n_col)) ;
+ }
+
+ DEBUG1 (("colamd: densecount: %d %d\n", dense_row_count, dense_col_count)) ;
+ max_deg = 0 ;
+ n_col2 = n_col ;
+ n_row2 = n_row ;
+
+ /* === Kill empty columns =============================================== */
+
+ /* Put the empty columns at the end in their natural order, so that LU */
+ /* factorization can proceed as far as possible. */
+ for (c = n_col-1 ; c >= 0 ; c--)
+ {
+ deg = Col [c].length ;
+ if (deg == 0)
+ {
+ /* this is a empty column, kill and order it last */
+ Col [c].shared2.order = --n_col2 ;
+ KILL_PRINCIPAL_COL (c) ;
+ }
+ }
+ DEBUG1 (("colamd: null columns killed: %d\n", n_col - n_col2)) ;
+
+ /* === Kill dense columns =============================================== */
+
+ /* Put the dense columns at the end, in their natural order */
+ for (c = n_col-1 ; c >= 0 ; c--)
+ {
+ /* skip any dead columns */
+ if (COL_IS_DEAD (c))
+ {
+ continue ;
+ }
+ deg = Col [c].length ;
+ if (deg > dense_col_count)
+ {
+ /* this is a dense column, kill and order it last */
+ Col [c].shared2.order = --n_col2 ;
+ /* decrement the row degrees */
+ cp = &A [Col [c].start] ;
+ cp_end = cp + Col [c].length ;
+ while (cp < cp_end)
+ {
+ Row [*cp++].shared1.degree-- ;
+ }
+ KILL_PRINCIPAL_COL (c) ;
+ }
+ }
+ DEBUG1 (("colamd: Dense and null columns killed: %d\n", n_col - n_col2)) ;
+
+ /* === Kill dense and empty rows ======================================== */
+
+ for (r = 0 ; r < n_row ; r++)
+ {
+ deg = Row [r].shared1.degree ;
+ ASSERT (deg >= 0 && deg <= n_col) ;
+ if (deg > dense_row_count || deg == 0)
+ {
+ /* kill a dense or empty row */
+ KILL_ROW (r) ;
+ --n_row2 ;
+ }
+ else
+ {
+ /* keep track of max degree of remaining rows */
+ max_deg = MAX (max_deg, deg) ;
+ }
+ }
+ DEBUG1 (("colamd: Dense and null rows killed: %d\n", n_row - n_row2)) ;
+
+ /* === Compute initial column scores ==================================== */
+
+ /* At this point the row degrees are accurate. They reflect the number */
+ /* of "live" (non-dense) columns in each row. No empty rows exist. */
+ /* Some "live" columns may contain only dead rows, however. These are */
+ /* pruned in the code below. */
+
+ /* now find the initial matlab score for each column */
+ for (c = n_col-1 ; c >= 0 ; c--)
+ {
+ /* skip dead column */
+ if (COL_IS_DEAD (c))
+ {
+ continue ;
+ }
+ score = 0 ;
+ cp = &A [Col [c].start] ;
+ new_cp = cp ;
+ cp_end = cp + Col [c].length ;
+ while (cp < cp_end)
+ {
+ /* get a row */
+ row = *cp++ ;
+ /* skip if dead */
+ if (ROW_IS_DEAD (row))
+ {
+ continue ;
+ }
+ /* compact the column */
+ *new_cp++ = row ;
+ /* add row's external degree */
+ score += Row [row].shared1.degree - 1 ;
+ /* guard against integer overflow */
+ score = MIN (score, n_col) ;
+ }
+ /* determine pruned column length */
+ col_length = (Int) (new_cp - &A [Col [c].start]) ;
+ if (col_length == 0)
+ {
+ /* a newly-made null column (all rows in this col are "dense" */
+ /* and have already been killed) */
+ DEBUG2 (("Newly null killed: %d\n", c)) ;
+ Col [c].shared2.order = --n_col2 ;
+ KILL_PRINCIPAL_COL (c) ;
+ }
+ else
+ {
+ /* set column length and set score */
+ ASSERT (score >= 0) ;
+ ASSERT (score <= n_col) ;
+ Col [c].length = col_length ;
+ Col [c].shared2.score = score ;
+ }
+ }
+ DEBUG1 (("colamd: Dense, null, and newly-null columns killed: %d\n",
+ n_col-n_col2)) ;
+
+ /* At this point, all empty rows and columns are dead. All live columns */
+ /* are "clean" (containing no dead rows) and simplicial (no supercolumns */
+ /* yet). Rows may contain dead columns, but all live rows contain at */
+ /* least one live column. */
+
+#ifndef NDEBUG
+ debug_structures (n_row, n_col, Row, Col, A, n_col2) ;
+#endif /* NDEBUG */
+
+ /* === Initialize degree lists ========================================== */
+
+#ifndef NDEBUG
+ debug_count = 0 ;
+#endif /* NDEBUG */
+
+ /* clear the hash buckets */
+ for (c = 0 ; c <= n_col ; c++)
+ {
+ head [c] = EMPTY ;
+ }
+ min_score = n_col ;
+ /* place in reverse order, so low column indices are at the front */
+ /* of the lists. This is to encourage natural tie-breaking */
+ for (c = n_col-1 ; c >= 0 ; c--)
+ {
+ /* only add principal columns to degree lists */
+ if (COL_IS_ALIVE (c))
+ {
+ DEBUG4 (("place %d score %d minscore %d ncol %d\n",
+ c, Col [c].shared2.score, min_score, n_col)) ;
+
+ /* === Add columns score to DList =============================== */
+
+ score = Col [c].shared2.score ;
+
+ ASSERT (min_score >= 0) ;
+ ASSERT (min_score <= n_col) ;
+ ASSERT (score >= 0) ;
+ ASSERT (score <= n_col) ;
+ ASSERT (head [score] >= EMPTY) ;
+
+ /* now add this column to dList at proper score location */
+ next_col = head [score] ;
+ Col [c].shared3.prev = EMPTY ;
+ Col [c].shared4.degree_next = next_col ;
+
+ /* if there already was a column with the same score, set its */
+ /* previous pointer to this new column */
+ if (next_col != EMPTY)
+ {
+ Col [next_col].shared3.prev = c ;
+ }
+ head [score] = c ;
+
+ /* see if this score is less than current min */
+ min_score = MIN (min_score, score) ;
+
+#ifndef NDEBUG
+ debug_count++ ;
+#endif /* NDEBUG */
+
+ }
+ }
+
+#ifndef NDEBUG
+ DEBUG1 (("colamd: Live cols %d out of %d, non-princ: %d\n",
+ debug_count, n_col, n_col-debug_count)) ;
+ ASSERT (debug_count == n_col2) ;
+ debug_deg_lists (n_row, n_col, Row, Col, head, min_score, n_col2, max_deg) ;
+#endif /* NDEBUG */
+
+ /* === Return number of remaining columns, and max row degree =========== */
+
+ *p_n_col2 = n_col2 ;
+ *p_n_row2 = n_row2 ;
+ *p_max_deg = max_deg ;
+}
+
+
+/* ========================================================================== */
+/* === find_ordering ======================================================== */
+/* ========================================================================== */
+
+/*
+ Order the principal columns of the supercolumn form of the matrix
+ (no supercolumns on input). Uses a minimum approximate column minimum
+ degree ordering method. Not user-callable.
+*/
+
+PRIVATE Int find_ordering /* return the number of garbage collections */
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row, /* number of rows of A */
+ Int n_col, /* number of columns of A */
+ Int Alen, /* size of A, 2*nnz + n_col or larger */
+ Colamd_Row Row [], /* of size n_row+1 */
+ Colamd_Col Col [], /* of size n_col+1 */
+ Int A [], /* column form and row form of A */
+ Int head [], /* of size n_col+1 */
+ Int n_col2, /* Remaining columns to order */
+ Int max_deg, /* Maximum row degree */
+ Int pfree, /* index of first free slot (2*nnz on entry) */
+ Int aggressive
+)
+{
+ /* === Local variables ================================================== */
+
+ Int k ; /* current pivot ordering step */
+ Int pivot_col ; /* current pivot column */
+ Int *cp ; /* a column pointer */
+ Int *rp ; /* a row pointer */
+ Int pivot_row ; /* current pivot row */
+ Int *new_cp ; /* modified column pointer */
+ Int *new_rp ; /* modified row pointer */
+ Int pivot_row_start ; /* pointer to start of pivot row */
+ Int pivot_row_degree ; /* number of columns in pivot row */
+ Int pivot_row_length ; /* number of supercolumns in pivot row */
+ Int pivot_col_score ; /* score of pivot column */
+ Int needed_memory ; /* free space needed for pivot row */
+ Int *cp_end ; /* pointer to the end of a column */
+ Int *rp_end ; /* pointer to the end of a row */
+ Int row ; /* a row index */
+ Int col ; /* a column index */
+ Int max_score ; /* maximum possible score */
+ Int cur_score ; /* score of current column */
+ unsigned Int hash ; /* hash value for supernode detection */
+ Int head_column ; /* head of hash bucket */
+ Int first_col ; /* first column in hash bucket */
+ Int tag_mark ; /* marker value for mark array */
+ Int row_mark ; /* Row [row].shared2.mark */
+ Int set_difference ; /* set difference size of row with pivot row */
+ Int min_score ; /* smallest column score */
+ Int col_thickness ; /* "thickness" (no. of columns in a supercol) */
+ Int max_mark ; /* maximum value of tag_mark */
+ Int pivot_col_thickness ; /* number of columns represented by pivot col */
+ Int prev_col ; /* Used by Dlist operations. */
+ Int next_col ; /* Used by Dlist operations. */
+ Int ngarbage ; /* number of garbage collections performed */
+
+#ifndef NDEBUG
+ Int debug_d ; /* debug loop counter */
+ Int debug_step = 0 ; /* debug loop counter */
+#endif /* NDEBUG */
+
+ /* === Initialization and clear mark ==================================== */
+
+ max_mark = INT_MAX - n_col ; /* INT_MAX defined in <limits.h> */
+ tag_mark = clear_mark (0, max_mark, n_row, Row) ;
+ min_score = 0 ;
+ ngarbage = 0 ;
+ DEBUG1 (("colamd: Ordering, n_col2=%d\n", n_col2)) ;
+
+ /* === Order the columns ================================================ */
+
+ for (k = 0 ; k < n_col2 ; /* 'k' is incremented below */)
+ {
+
+#ifndef NDEBUG
+ if (debug_step % 100 == 0)
+ {
+ DEBUG2 (("\n... Step k: %d out of n_col2: %d\n", k, n_col2)) ;
+ }
+ else
+ {
+ DEBUG3 (("\n----------Step k: %d out of n_col2: %d\n", k, n_col2)) ;
+ }
+ debug_step++ ;
+ debug_deg_lists (n_row, n_col, Row, Col, head,
+ min_score, n_col2-k, max_deg) ;
+ debug_matrix (n_row, n_col, Row, Col, A) ;
+#endif /* NDEBUG */
+
+ /* === Select pivot column, and order it ============================ */
+
+ /* make sure degree list isn't empty */
+ ASSERT (min_score >= 0) ;
+ ASSERT (min_score <= n_col) ;
+ ASSERT (head [min_score] >= EMPTY) ;
+
+#ifndef NDEBUG
+ for (debug_d = 0 ; debug_d < min_score ; debug_d++)
+ {
+ ASSERT (head [debug_d] == EMPTY) ;
+ }
+#endif /* NDEBUG */
+
+ /* get pivot column from head of minimum degree list */
+ while (head [min_score] == EMPTY && min_score < n_col)
+ {
+ min_score++ ;
+ }
+ pivot_col = head [min_score] ;
+ ASSERT (pivot_col >= 0 && pivot_col <= n_col) ;
+ next_col = Col [pivot_col].shared4.degree_next ;
+ head [min_score] = next_col ;
+ if (next_col != EMPTY)
+ {
+ Col [next_col].shared3.prev = EMPTY ;
+ }
+
+ ASSERT (COL_IS_ALIVE (pivot_col)) ;
+
+ /* remember score for defrag check */
+ pivot_col_score = Col [pivot_col].shared2.score ;
+
+ /* the pivot column is the kth column in the pivot order */
+ Col [pivot_col].shared2.order = k ;
+
+ /* increment order count by column thickness */
+ pivot_col_thickness = Col [pivot_col].shared1.thickness ;
+ k += pivot_col_thickness ;
+ ASSERT (pivot_col_thickness > 0) ;
+ DEBUG3 (("Pivot col: %d thick %d\n", pivot_col, pivot_col_thickness)) ;
+
+ /* === Garbage_collection, if necessary ============================= */
+
+ needed_memory = MIN (pivot_col_score, n_col - k) ;
+ if (pfree + needed_memory >= Alen)
+ {
+ pfree = garbage_collection (n_row, n_col, Row, Col, A, &A [pfree]) ;
+ ngarbage++ ;
+ /* after garbage collection we will have enough */
+ ASSERT (pfree + needed_memory < Alen) ;
+ /* garbage collection has wiped out the Row[].shared2.mark array */
+ tag_mark = clear_mark (0, max_mark, n_row, Row) ;
+
+#ifndef NDEBUG
+ debug_matrix (n_row, n_col, Row, Col, A) ;
+#endif /* NDEBUG */
+ }
+
+ /* === Compute pivot row pattern ==================================== */
+
+ /* get starting location for this new merged row */
+ pivot_row_start = pfree ;
+
+ /* initialize new row counts to zero */
+ pivot_row_degree = 0 ;
+
+ /* tag pivot column as having been visited so it isn't included */
+ /* in merged pivot row */
+ Col [pivot_col].shared1.thickness = -pivot_col_thickness ;
+
+ /* pivot row is the union of all rows in the pivot column pattern */
+ cp = &A [Col [pivot_col].start] ;
+ cp_end = cp + Col [pivot_col].length ;
+ while (cp < cp_end)
+ {
+ /* get a row */
+ row = *cp++ ;
+ DEBUG4 (("Pivot col pattern %d %d\n", ROW_IS_ALIVE (row), row)) ;
+ /* skip if row is dead */
+ if (ROW_IS_ALIVE (row))
+ {
+ rp = &A [Row [row].start] ;
+ rp_end = rp + Row [row].length ;
+ while (rp < rp_end)
+ {
+ /* get a column */
+ col = *rp++ ;
+ /* add the column, if alive and untagged */
+ col_thickness = Col [col].shared1.thickness ;
+ if (col_thickness > 0 && COL_IS_ALIVE (col))
+ {
+ /* tag column in pivot row */
+ Col [col].shared1.thickness = -col_thickness ;
+ ASSERT (pfree < Alen) ;
+ /* place column in pivot row */
+ A [pfree++] = col ;
+ pivot_row_degree += col_thickness ;
+ }
+ }
+ }
+ }
+
+ /* clear tag on pivot column */
+ Col [pivot_col].shared1.thickness = pivot_col_thickness ;
+ max_deg = MAX (max_deg, pivot_row_degree) ;
+
+#ifndef NDEBUG
+ DEBUG3 (("check2\n")) ;
+ debug_mark (n_row, Row, tag_mark, max_mark) ;
+#endif /* NDEBUG */
+
+ /* === Kill all rows used to construct pivot row ==================== */
+
+ /* also kill pivot row, temporarily */
+ cp = &A [Col [pivot_col].start] ;
+ cp_end = cp + Col [pivot_col].length ;
+ while (cp < cp_end)
+ {
+ /* may be killing an already dead row */
+ row = *cp++ ;
+ DEBUG3 (("Kill row in pivot col: %d\n", row)) ;
+ KILL_ROW (row) ;
+ }
+
+ /* === Select a row index to use as the new pivot row =============== */
+
+ pivot_row_length = pfree - pivot_row_start ;
+ if (pivot_row_length > 0)
+ {
+ /* pick the "pivot" row arbitrarily (first row in col) */
+ pivot_row = A [Col [pivot_col].start] ;
+ DEBUG3 (("Pivotal row is %d\n", pivot_row)) ;
+ }
+ else
+ {
+ /* there is no pivot row, since it is of zero length */
+ pivot_row = EMPTY ;
+ ASSERT (pivot_row_length == 0) ;
+ }
+ ASSERT (Col [pivot_col].length > 0 || pivot_row_length == 0) ;
+
+ /* === Approximate degree computation =============================== */
+
+ /* Here begins the computation of the approximate degree. The column */
+ /* score is the sum of the pivot row "length", plus the size of the */
+ /* set differences of each row in the column minus the pattern of the */
+ /* pivot row itself. The column ("thickness") itself is also */
+ /* excluded from the column score (we thus use an approximate */
+ /* external degree). */
+
+ /* The time taken by the following code (compute set differences, and */
+ /* add them up) is proportional to the size of the data structure */
+ /* being scanned - that is, the sum of the sizes of each column in */
+ /* the pivot row. Thus, the amortized time to compute a column score */
+ /* is proportional to the size of that column (where size, in this */
+ /* context, is the column "length", or the number of row indices */
+ /* in that column). The number of row indices in a column is */
+ /* monotonically non-decreasing, from the length of the original */
+ /* column on input to colamd. */
+
+ /* === Compute set differences ====================================== */
+
+ DEBUG3 (("** Computing set differences phase. **\n")) ;
+
+ /* pivot row is currently dead - it will be revived later. */
+
+ DEBUG3 (("Pivot row: ")) ;
+ /* for each column in pivot row */
+ rp = &A [pivot_row_start] ;
+ rp_end = rp + pivot_row_length ;
+ while (rp < rp_end)
+ {
+ col = *rp++ ;
+ ASSERT (COL_IS_ALIVE (col) && col != pivot_col) ;
+ DEBUG3 (("Col: %d\n", col)) ;
+
+ /* clear tags used to construct pivot row pattern */
+ col_thickness = -Col [col].shared1.thickness ;
+ ASSERT (col_thickness > 0) ;
+ Col [col].shared1.thickness = col_thickness ;
+
+ /* === Remove column from degree list =========================== */
+
+ cur_score = Col [col].shared2.score ;
+ prev_col = Col [col].shared3.prev ;
+ next_col = Col [col].shared4.degree_next ;
+ ASSERT (cur_score >= 0) ;
+ ASSERT (cur_score <= n_col) ;
+ ASSERT (cur_score >= EMPTY) ;
+ if (prev_col == EMPTY)
+ {
+ head [cur_score] = next_col ;
+ }
+ else
+ {
+ Col [prev_col].shared4.degree_next = next_col ;
+ }
+ if (next_col != EMPTY)
+ {
+ Col [next_col].shared3.prev = prev_col ;
+ }
+
+ /* === Scan the column ========================================== */
+
+ cp = &A [Col [col].start] ;
+ cp_end = cp + Col [col].length ;
+ while (cp < cp_end)
+ {
+ /* get a row */
+ row = *cp++ ;
+ row_mark = Row [row].shared2.mark ;
+ /* skip if dead */
+ if (ROW_IS_MARKED_DEAD (row_mark))
+ {
+ continue ;
+ }
+ ASSERT (row != pivot_row) ;
+ set_difference = row_mark - tag_mark ;
+ /* check if the row has been seen yet */
+ if (set_difference < 0)
+ {
+ ASSERT (Row [row].shared1.degree <= max_deg) ;
+ set_difference = Row [row].shared1.degree ;
+ }
+ /* subtract column thickness from this row's set difference */
+ set_difference -= col_thickness ;
+ ASSERT (set_difference >= 0) ;
+ /* absorb this row if the set difference becomes zero */
+ if (set_difference == 0 && aggressive)
+ {
+ DEBUG3 (("aggressive absorption. Row: %d\n", row)) ;
+ KILL_ROW (row) ;
+ }
+ else
+ {
+ /* save the new mark */
+ Row [row].shared2.mark = set_difference + tag_mark ;
+ }
+ }
+ }
+
+#ifndef NDEBUG
+ debug_deg_lists (n_row, n_col, Row, Col, head,
+ min_score, n_col2-k-pivot_row_degree, max_deg) ;
+#endif /* NDEBUG */
+
+ /* === Add up set differences for each column ======================= */
+
+ DEBUG3 (("** Adding set differences phase. **\n")) ;
+
+ /* for each column in pivot row */
+ rp = &A [pivot_row_start] ;
+ rp_end = rp + pivot_row_length ;
+ while (rp < rp_end)
+ {
+ /* get a column */
+ col = *rp++ ;
+ ASSERT (COL_IS_ALIVE (col) && col != pivot_col) ;
+ hash = 0 ;
+ cur_score = 0 ;
+ cp = &A [Col [col].start] ;
+ /* compact the column */
+ new_cp = cp ;
+ cp_end = cp + Col [col].length ;
+
+ DEBUG4 (("Adding set diffs for Col: %d.\n", col)) ;
+
+ while (cp < cp_end)
+ {
+ /* get a row */
+ row = *cp++ ;
+ ASSERT(row >= 0 && row < n_row) ;
+ row_mark = Row [row].shared2.mark ;
+ /* skip if dead */
+ if (ROW_IS_MARKED_DEAD (row_mark))
+ {
+ DEBUG4 ((" Row %d, dead\n", row)) ;
+ continue ;
+ }
+ DEBUG4 ((" Row %d, set diff %d\n", row, row_mark-tag_mark));
+ ASSERT (row_mark >= tag_mark) ;
+ /* compact the column */
+ *new_cp++ = row ;
+ /* compute hash function */
+ hash += row ;
+ /* add set difference */
+ cur_score += row_mark - tag_mark ;
+ /* integer overflow... */
+ cur_score = MIN (cur_score, n_col) ;
+ }
+
+ /* recompute the column's length */
+ Col [col].length = (Int) (new_cp - &A [Col [col].start]) ;
+
+ /* === Further mass elimination ================================= */
+
+ if (Col [col].length == 0)
+ {
+ DEBUG4 (("further mass elimination. Col: %d\n", col)) ;
+ /* nothing left but the pivot row in this column */
+ KILL_PRINCIPAL_COL (col) ;
+ pivot_row_degree -= Col [col].shared1.thickness ;
+ ASSERT (pivot_row_degree >= 0) ;
+ /* order it */
+ Col [col].shared2.order = k ;
+ /* increment order count by column thickness */
+ k += Col [col].shared1.thickness ;
+ }
+ else
+ {
+ /* === Prepare for supercolumn detection ==================== */
+
+ DEBUG4 (("Preparing supercol detection for Col: %d.\n", col)) ;
+
+ /* save score so far */
+ Col [col].shared2.score = cur_score ;
+
+ /* add column to hash table, for supercolumn detection */
+ hash %= n_col + 1 ;
+
+ DEBUG4 ((" Hash = %d, n_col = %d.\n", hash, n_col)) ;
+ ASSERT (((Int) hash) <= n_col) ;
+
+ head_column = head [hash] ;
+ if (head_column > EMPTY)
+ {
+ /* degree list "hash" is non-empty, use prev (shared3) of */
+ /* first column in degree list as head of hash bucket */
+ first_col = Col [head_column].shared3.headhash ;
+ Col [head_column].shared3.headhash = col ;
+ }
+ else
+ {
+ /* degree list "hash" is empty, use head as hash bucket */
+ first_col = - (head_column + 2) ;
+ head [hash] = - (col + 2) ;
+ }
+ Col [col].shared4.hash_next = first_col ;
+
+ /* save hash function in Col [col].shared3.hash */
+ Col [col].shared3.hash = (Int) hash ;
+ ASSERT (COL_IS_ALIVE (col)) ;
+ }
+ }
+
+ /* The approximate external column degree is now computed. */
+
+ /* === Supercolumn detection ======================================== */
+
+ DEBUG3 (("** Supercolumn detection phase. **\n")) ;
+
+ detect_super_cols (
+
+#ifndef NDEBUG
+ n_col, Row,
+#endif /* NDEBUG */
+
+ Col, A, head, pivot_row_start, pivot_row_length) ;
+
+ /* === Kill the pivotal column ====================================== */
+
+ KILL_PRINCIPAL_COL (pivot_col) ;
+
+ /* === Clear mark =================================================== */
+
+ tag_mark = clear_mark (tag_mark+max_deg+1, max_mark, n_row, Row) ;
+
+#ifndef NDEBUG
+ DEBUG3 (("check3\n")) ;
+ debug_mark (n_row, Row, tag_mark, max_mark) ;
+#endif /* NDEBUG */
+
+ /* === Finalize the new pivot row, and column scores ================ */
+
+ DEBUG3 (("** Finalize scores phase. **\n")) ;
+
+ /* for each column in pivot row */
+ rp = &A [pivot_row_start] ;
+ /* compact the pivot row */
+ new_rp = rp ;
+ rp_end = rp + pivot_row_length ;
+ while (rp < rp_end)
+ {
+ col = *rp++ ;
+ /* skip dead columns */
+ if (COL_IS_DEAD (col))
+ {
+ continue ;
+ }
+ *new_rp++ = col ;
+ /* add new pivot row to column */
+ A [Col [col].start + (Col [col].length++)] = pivot_row ;
+
+ /* retrieve score so far and add on pivot row's degree. */
+ /* (we wait until here for this in case the pivot */
+ /* row's degree was reduced due to mass elimination). */
+ cur_score = Col [col].shared2.score + pivot_row_degree ;
+
+ /* calculate the max possible score as the number of */
+ /* external columns minus the 'k' value minus the */
+ /* columns thickness */
+ max_score = n_col - k - Col [col].shared1.thickness ;
+
+ /* make the score the external degree of the union-of-rows */
+ cur_score -= Col [col].shared1.thickness ;
+
+ /* make sure score is less or equal than the max score */
+ cur_score = MIN (cur_score, max_score) ;
+ ASSERT (cur_score >= 0) ;
+
+ /* store updated score */
+ Col [col].shared2.score = cur_score ;
+
+ /* === Place column back in degree list ========================= */
+
+ ASSERT (min_score >= 0) ;
+ ASSERT (min_score <= n_col) ;
+ ASSERT (cur_score >= 0) ;
+ ASSERT (cur_score <= n_col) ;
+ ASSERT (head [cur_score] >= EMPTY) ;
+ next_col = head [cur_score] ;
+ Col [col].shared4.degree_next = next_col ;
+ Col [col].shared3.prev = EMPTY ;
+ if (next_col != EMPTY)
+ {
+ Col [next_col].shared3.prev = col ;
+ }
+ head [cur_score] = col ;
+
+ /* see if this score is less than current min */
+ min_score = MIN (min_score, cur_score) ;
+
+ }
+
+#ifndef NDEBUG
+ debug_deg_lists (n_row, n_col, Row, Col, head,
+ min_score, n_col2-k, max_deg) ;
+#endif /* NDEBUG */
+
+ /* === Resurrect the new pivot row ================================== */
+
+ if (pivot_row_degree > 0)
+ {
+ /* update pivot row length to reflect any cols that were killed */
+ /* during super-col detection and mass elimination */
+ Row [pivot_row].start = pivot_row_start ;
+ Row [pivot_row].length = (Int) (new_rp - &A[pivot_row_start]) ;
+ ASSERT (Row [pivot_row].length > 0) ;
+ Row [pivot_row].shared1.degree = pivot_row_degree ;
+ Row [pivot_row].shared2.mark = 0 ;
+ /* pivot row is no longer dead */
+
+ DEBUG1 (("Resurrect Pivot_row %d deg: %d\n",
+ pivot_row, pivot_row_degree)) ;
+ }
+ }
+
+ /* === All principal columns have now been ordered ====================== */
+
+ return (ngarbage) ;
+}
+
+
+/* ========================================================================== */
+/* === order_children ======================================================= */
+/* ========================================================================== */
+
+/*
+ The find_ordering routine has ordered all of the principal columns (the
+ representatives of the supercolumns). The non-principal columns have not
+ yet been ordered. This routine orders those columns by walking up the
+ parent tree (a column is a child of the column which absorbed it). The
+ final permutation vector is then placed in p [0 ... n_col-1], with p [0]
+ being the first column, and p [n_col-1] being the last. It doesn't look
+ like it at first glance, but be assured that this routine takes time linear
+ in the number of columns. Although not immediately obvious, the time
+ taken by this routine is O (n_col), that is, linear in the number of
+ columns. Not user-callable.
+*/
+
+PRIVATE void order_children
+(
+ /* === Parameters ======================================================= */
+
+ Int n_col, /* number of columns of A */
+ Colamd_Col Col [], /* of size n_col+1 */
+ Int p [] /* p [0 ... n_col-1] is the column permutation*/
+)
+{
+ /* === Local variables ================================================== */
+
+ Int i ; /* loop counter for all columns */
+ Int c ; /* column index */
+ Int parent ; /* index of column's parent */
+ Int order ; /* column's order */
+
+ /* === Order each non-principal column ================================== */
+
+ for (i = 0 ; i < n_col ; i++)
+ {
+ /* find an un-ordered non-principal column */
+ ASSERT (COL_IS_DEAD (i)) ;
+ if (!COL_IS_DEAD_PRINCIPAL (i) && Col [i].shared2.order == EMPTY)
+ {
+ parent = i ;
+ /* once found, find its principal parent */
+ do
+ {
+ parent = Col [parent].shared1.parent ;
+ } while (!COL_IS_DEAD_PRINCIPAL (parent)) ;
+
+ /* now, order all un-ordered non-principal columns along path */
+ /* to this parent. collapse tree at the same time */
+ c = i ;
+ /* get order of parent */
+ order = Col [parent].shared2.order ;
+
+ do
+ {
+ ASSERT (Col [c].shared2.order == EMPTY) ;
+
+ /* order this column */
+ Col [c].shared2.order = order++ ;
+ /* collaps tree */
+ Col [c].shared1.parent = parent ;
+
+ /* get immediate parent of this column */
+ c = Col [c].shared1.parent ;
+
+ /* continue until we hit an ordered column. There are */
+ /* guarranteed not to be anymore unordered columns */
+ /* above an ordered column */
+ } while (Col [c].shared2.order == EMPTY) ;
+
+ /* re-order the super_col parent to largest order for this group */
+ Col [parent].shared2.order = order ;
+ }
+ }
+
+ /* === Generate the permutation ========================================= */
+
+ for (c = 0 ; c < n_col ; c++)
+ {
+ p [Col [c].shared2.order] = c ;
+ }
+}
+
+
+/* ========================================================================== */
+/* === detect_super_cols ==================================================== */
+/* ========================================================================== */
+
+/*
+ Detects supercolumns by finding matches between columns in the hash buckets.
+ Check amongst columns in the set A [row_start ... row_start + row_length-1].
+ The columns under consideration are currently *not* in the degree lists,
+ and have already been placed in the hash buckets.
+
+ The hash bucket for columns whose hash function is equal to h is stored
+ as follows:
+
+ if head [h] is >= 0, then head [h] contains a degree list, so:
+
+ head [h] is the first column in degree bucket h.
+ Col [head [h]].headhash gives the first column in hash bucket h.
+
+ otherwise, the degree list is empty, and:
+
+ -(head [h] + 2) is the first column in hash bucket h.
+
+ For a column c in a hash bucket, Col [c].shared3.prev is NOT a "previous
+ column" pointer. Col [c].shared3.hash is used instead as the hash number
+ for that column. The value of Col [c].shared4.hash_next is the next column
+ in the same hash bucket.
+
+ Assuming no, or "few" hash collisions, the time taken by this routine is
+ linear in the sum of the sizes (lengths) of each column whose score has
+ just been computed in the approximate degree computation.
+ Not user-callable.
+*/
+
+PRIVATE void detect_super_cols
+(
+ /* === Parameters ======================================================= */
+
+#ifndef NDEBUG
+ /* these two parameters are only needed when debugging is enabled: */
+ Int n_col, /* number of columns of A */
+ Colamd_Row Row [], /* of size n_row+1 */
+#endif /* NDEBUG */
+
+ Colamd_Col Col [], /* of size n_col+1 */
+ Int A [], /* row indices of A */
+ Int head [], /* head of degree lists and hash buckets */
+ Int row_start, /* pointer to set of columns to check */
+ Int row_length /* number of columns to check */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int hash ; /* hash value for a column */
+ Int *rp ; /* pointer to a row */
+ Int c ; /* a column index */
+ Int super_c ; /* column index of the column to absorb into */
+ Int *cp1 ; /* column pointer for column super_c */
+ Int *cp2 ; /* column pointer for column c */
+ Int length ; /* length of column super_c */
+ Int prev_c ; /* column preceding c in hash bucket */
+ Int i ; /* loop counter */
+ Int *rp_end ; /* pointer to the end of the row */
+ Int col ; /* a column index in the row to check */
+ Int head_column ; /* first column in hash bucket or degree list */
+ Int first_col ; /* first column in hash bucket */
+
+ /* === Consider each column in the row ================================== */
+
+ rp = &A [row_start] ;
+ rp_end = rp + row_length ;
+ while (rp < rp_end)
+ {
+ col = *rp++ ;
+ if (COL_IS_DEAD (col))
+ {
+ continue ;
+ }
+
+ /* get hash number for this column */
+ hash = Col [col].shared3.hash ;
+ ASSERT (hash <= n_col) ;
+
+ /* === Get the first column in this hash bucket ===================== */
+
+ head_column = head [hash] ;
+ if (head_column > EMPTY)
+ {
+ first_col = Col [head_column].shared3.headhash ;
+ }
+ else
+ {
+ first_col = - (head_column + 2) ;
+ }
+
+ /* === Consider each column in the hash bucket ====================== */
+
+ for (super_c = first_col ; super_c != EMPTY ;
+ super_c = Col [super_c].shared4.hash_next)
+ {
+ ASSERT (COL_IS_ALIVE (super_c)) ;
+ ASSERT (Col [super_c].shared3.hash == hash) ;
+ length = Col [super_c].length ;
+
+ /* prev_c is the column preceding column c in the hash bucket */
+ prev_c = super_c ;
+
+ /* === Compare super_c with all columns after it ================ */
+
+ for (c = Col [super_c].shared4.hash_next ;
+ c != EMPTY ; c = Col [c].shared4.hash_next)
+ {
+ ASSERT (c != super_c) ;
+ ASSERT (COL_IS_ALIVE (c)) ;
+ ASSERT (Col [c].shared3.hash == hash) ;
+
+ /* not identical if lengths or scores are different */
+ if (Col [c].length != length ||
+ Col [c].shared2.score != Col [super_c].shared2.score)
+ {
+ prev_c = c ;
+ continue ;
+ }
+
+ /* compare the two columns */
+ cp1 = &A [Col [super_c].start] ;
+ cp2 = &A [Col [c].start] ;
+
+ for (i = 0 ; i < length ; i++)
+ {
+ /* the columns are "clean" (no dead rows) */
+ ASSERT (ROW_IS_ALIVE (*cp1)) ;
+ ASSERT (ROW_IS_ALIVE (*cp2)) ;
+ /* row indices will same order for both supercols, */
+ /* no gather scatter nessasary */
+ if (*cp1++ != *cp2++)
+ {
+ break ;
+ }
+ }
+
+ /* the two columns are different if the for-loop "broke" */
+ if (i != length)
+ {
+ prev_c = c ;
+ continue ;
+ }
+
+ /* === Got it! two columns are identical =================== */
+
+ ASSERT (Col [c].shared2.score == Col [super_c].shared2.score) ;
+
+ Col [super_c].shared1.thickness += Col [c].shared1.thickness ;
+ Col [c].shared1.parent = super_c ;
+ KILL_NON_PRINCIPAL_COL (c) ;
+ /* order c later, in order_children() */
+ Col [c].shared2.order = EMPTY ;
+ /* remove c from hash bucket */
+ Col [prev_c].shared4.hash_next = Col [c].shared4.hash_next ;
+ }
+ }
+
+ /* === Empty this hash bucket ======================================= */
+
+ if (head_column > EMPTY)
+ {
+ /* corresponding degree list "hash" is not empty */
+ Col [head_column].shared3.headhash = EMPTY ;
+ }
+ else
+ {
+ /* corresponding degree list "hash" is empty */
+ head [hash] = EMPTY ;
+ }
+ }
+}
+
+
+/* ========================================================================== */
+/* === garbage_collection =================================================== */
+/* ========================================================================== */
+
+/*
+ Defragments and compacts columns and rows in the workspace A. Used when
+ all avaliable memory has been used while performing row merging. Returns
+ the index of the first free position in A, after garbage collection. The
+ time taken by this routine is linear is the size of the array A, which is
+ itself linear in the number of nonzeros in the input matrix.
+ Not user-callable.
+*/
+
+PRIVATE Int garbage_collection /* returns the new value of pfree */
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row, /* number of rows */
+ Int n_col, /* number of columns */
+ Colamd_Row Row [], /* row info */
+ Colamd_Col Col [], /* column info */
+ Int A [], /* A [0 ... Alen-1] holds the matrix */
+ Int *pfree /* &A [0] ... pfree is in use */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int *psrc ; /* source pointer */
+ Int *pdest ; /* destination pointer */
+ Int j ; /* counter */
+ Int r ; /* a row index */
+ Int c ; /* a column index */
+ Int length ; /* length of a row or column */
+
+#ifndef NDEBUG
+ Int debug_rows ;
+ DEBUG2 (("Defrag..\n")) ;
+ for (psrc = &A[0] ; psrc < pfree ; psrc++) ASSERT (*psrc >= 0) ;
+ debug_rows = 0 ;
+#endif /* NDEBUG */
+
+ /* === Defragment the columns =========================================== */
+
+ pdest = &A[0] ;
+ for (c = 0 ; c < n_col ; c++)
+ {
+ if (COL_IS_ALIVE (c))
+ {
+ psrc = &A [Col [c].start] ;
+
+ /* move and compact the column */
+ ASSERT (pdest <= psrc) ;
+ Col [c].start = (Int) (pdest - &A [0]) ;
+ length = Col [c].length ;
+ for (j = 0 ; j < length ; j++)
+ {
+ r = *psrc++ ;
+ if (ROW_IS_ALIVE (r))
+ {
+ *pdest++ = r ;
+ }
+ }
+ Col [c].length = (Int) (pdest - &A [Col [c].start]) ;
+ }
+ }
+
+ /* === Prepare to defragment the rows =================================== */
+
+ for (r = 0 ; r < n_row ; r++)
+ {
+ if (ROW_IS_DEAD (r) || (Row [r].length == 0))
+ {
+ /* This row is already dead, or is of zero length. Cannot compact
+ * a row of zero length, so kill it. NOTE: in the current version,
+ * there are no zero-length live rows. Kill the row (for the first
+ * time, or again) just to be safe. */
+ KILL_ROW (r) ;
+ }
+ else
+ {
+ /* save first column index in Row [r].shared2.first_column */
+ psrc = &A [Row [r].start] ;
+ Row [r].shared2.first_column = *psrc ;
+ ASSERT (ROW_IS_ALIVE (r)) ;
+ /* flag the start of the row with the one's complement of row */
+ *psrc = ONES_COMPLEMENT (r) ;
+#ifndef NDEBUG
+ debug_rows++ ;
+#endif /* NDEBUG */
+ }
+ }
+
+ /* === Defragment the rows ============================================== */
+
+ psrc = pdest ;
+ while (psrc < pfree)
+ {
+ /* find a negative number ... the start of a row */
+ if (*psrc++ < 0)
+ {
+ psrc-- ;
+ /* get the row index */
+ r = ONES_COMPLEMENT (*psrc) ;
+ ASSERT (r >= 0 && r < n_row) ;
+ /* restore first column index */
+ *psrc = Row [r].shared2.first_column ;
+ ASSERT (ROW_IS_ALIVE (r)) ;
+ ASSERT (Row [r].length > 0) ;
+ /* move and compact the row */
+ ASSERT (pdest <= psrc) ;
+ Row [r].start = (Int) (pdest - &A [0]) ;
+ length = Row [r].length ;
+ for (j = 0 ; j < length ; j++)
+ {
+ c = *psrc++ ;
+ if (COL_IS_ALIVE (c))
+ {
+ *pdest++ = c ;
+ }
+ }
+ Row [r].length = (Int) (pdest - &A [Row [r].start]) ;
+ ASSERT (Row [r].length > 0) ;
+#ifndef NDEBUG
+ debug_rows-- ;
+#endif /* NDEBUG */
+ }
+ }
+ /* ensure we found all the rows */
+ ASSERT (debug_rows == 0) ;
+
+ /* === Return the new value of pfree ==================================== */
+
+ return ((Int) (pdest - &A [0])) ;
+}
+
+
+/* ========================================================================== */
+/* === clear_mark =========================================================== */
+/* ========================================================================== */
+
+/*
+ Clears the Row [].shared2.mark array, and returns the new tag_mark.
+ Return value is the new tag_mark. Not user-callable.
+*/
+
+PRIVATE Int clear_mark /* return the new value for tag_mark */
+(
+ /* === Parameters ======================================================= */
+
+ Int tag_mark, /* new value of tag_mark */
+ Int max_mark, /* max allowed value of tag_mark */
+
+ Int n_row, /* number of rows in A */
+ Colamd_Row Row [] /* Row [0 ... n_row-1].shared2.mark is set to zero */
+)
+{
+ /* === Local variables ================================================== */
+
+ Int r ;
+
+ if (tag_mark <= 0 || tag_mark >= max_mark)
+ {
+ for (r = 0 ; r < n_row ; r++)
+ {
+ if (ROW_IS_ALIVE (r))
+ {
+ Row [r].shared2.mark = 0 ;
+ }
+ }
+ tag_mark = 1 ;
+ }
+
+ return (tag_mark) ;
+}
+
+
+/* ========================================================================== */
+/* === print_report ========================================================= */
+/* ========================================================================== */
+
+PRIVATE void print_report
+(
+ char *method,
+ Int stats [COLAMD_STATS]
+)
+{
+
+ Int i1, i2, i3 ;
+
+ PRINTF (("\n%s version %d.%d, %s: ", method,
+ COLAMD_MAIN_VERSION, COLAMD_SUB_VERSION, COLAMD_DATE)) ;
+
+ if (!stats)
+ {
+ PRINTF (("No statistics available.\n")) ;
+ return ;
+ }
+
+ i1 = stats [COLAMD_INFO1] ;
+ i2 = stats [COLAMD_INFO2] ;
+ i3 = stats [COLAMD_INFO3] ;
+
+ if (stats [COLAMD_STATUS] >= 0)
+ {
+ PRINTF (("OK. ")) ;
+ }
+ else
+ {
+ PRINTF (("ERROR. ")) ;
+ }
+
+ switch (stats [COLAMD_STATUS])
+ {
+
+ case COLAMD_OK_BUT_JUMBLED:
+
+ PRINTF(("Matrix has unsorted or duplicate row indices.\n")) ;
+
+ PRINTF(("%s: number of duplicate or out-of-order row indices: %d\n",
+ method, i3)) ;
+
+ PRINTF(("%s: last seen duplicate or out-of-order row index: %d\n",
+ method, INDEX (i2))) ;
+
+ PRINTF(("%s: last seen in column: %d",
+ method, INDEX (i1))) ;
+
+ /* no break - fall through to next case instead */
+
+ case COLAMD_OK:
+
+ PRINTF(("\n")) ;
+
+ PRINTF(("%s: number of dense or empty rows ignored: %d\n",
+ method, stats [COLAMD_DENSE_ROW])) ;
+
+ PRINTF(("%s: number of dense or empty columns ignored: %d\n",
+ method, stats [COLAMD_DENSE_COL])) ;
+
+ PRINTF(("%s: number of garbage collections performed: %d\n",
+ method, stats [COLAMD_DEFRAG_COUNT])) ;
+ break ;
+
+ case COLAMD_ERROR_A_not_present:
+
+ PRINTF(("Array A (row indices of matrix) not present.\n")) ;
+ break ;
+
+ case COLAMD_ERROR_p_not_present:
+
+ PRINTF(("Array p (column pointers for matrix) not present.\n")) ;
+ break ;
+
+ case COLAMD_ERROR_nrow_negative:
+
+ PRINTF(("Invalid number of rows (%d).\n", i1)) ;
+ break ;
+
+ case COLAMD_ERROR_ncol_negative:
+
+ PRINTF(("Invalid number of columns (%d).\n", i1)) ;
+ break ;
+
+ case COLAMD_ERROR_nnz_negative:
+
+ PRINTF(("Invalid number of nonzero entries (%d).\n", i1)) ;
+ break ;
+
+ case COLAMD_ERROR_p0_nonzero:
+
+ PRINTF(("Invalid column pointer, p [0] = %d, must be zero.\n", i1));
+ break ;
+
+ case COLAMD_ERROR_A_too_small:
+
+ PRINTF(("Array A too small.\n")) ;
+ PRINTF((" Need Alen >= %d, but given only Alen = %d.\n",
+ i1, i2)) ;
+ break ;
+
+ case COLAMD_ERROR_col_length_negative:
+
+ PRINTF
+ (("Column %d has a negative number of nonzero entries (%d).\n",
+ INDEX (i1), i2)) ;
+ break ;
+
+ case COLAMD_ERROR_row_index_out_of_bounds:
+
+ PRINTF
+ (("Row index (row %d) out of bounds (%d to %d) in column %d.\n",
+ INDEX (i2), INDEX (0), INDEX (i3-1), INDEX (i1))) ;
+ break ;
+
+ case COLAMD_ERROR_out_of_memory:
+
+ PRINTF(("Out of memory.\n")) ;
+ break ;
+
+ /* v2.4: internal-error case deleted */
+ }
+}
+
+
+
+
+/* ========================================================================== */
+/* === colamd debugging routines ============================================ */
+/* ========================================================================== */
+
+/* When debugging is disabled, the remainder of this file is ignored. */
+
+#ifndef NDEBUG
+
+
+/* ========================================================================== */
+/* === debug_structures ===================================================== */
+/* ========================================================================== */
+
+/*
+ At this point, all empty rows and columns are dead. All live columns
+ are "clean" (containing no dead rows) and simplicial (no supercolumns
+ yet). Rows may contain dead columns, but all live rows contain at
+ least one live column.
+*/
+
+PRIVATE void debug_structures
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A [],
+ Int n_col2
+)
+{
+ /* === Local variables ================================================== */
+
+ Int i ;
+ Int c ;
+ Int *cp ;
+ Int *cp_end ;
+ Int len ;
+ Int score ;
+ Int r ;
+ Int *rp ;
+ Int *rp_end ;
+ Int deg ;
+
+ /* === Check A, Row, and Col ============================================ */
+
+ for (c = 0 ; c < n_col ; c++)
+ {
+ if (COL_IS_ALIVE (c))
+ {
+ len = Col [c].length ;
+ score = Col [c].shared2.score ;
+ DEBUG4 (("initial live col %5d %5d %5d\n", c, len, score)) ;
+ ASSERT (len > 0) ;
+ ASSERT (score >= 0) ;
+ ASSERT (Col [c].shared1.thickness == 1) ;
+ cp = &A [Col [c].start] ;
+ cp_end = cp + len ;
+ while (cp < cp_end)
+ {
+ r = *cp++ ;
+ ASSERT (ROW_IS_ALIVE (r)) ;
+ }
+ }
+ else
+ {
+ i = Col [c].shared2.order ;
+ ASSERT (i >= n_col2 && i < n_col) ;
+ }
+ }
+
+ for (r = 0 ; r < n_row ; r++)
+ {
+ if (ROW_IS_ALIVE (r))
+ {
+ i = 0 ;
+ len = Row [r].length ;
+ deg = Row [r].shared1.degree ;
+ ASSERT (len > 0) ;
+ ASSERT (deg > 0) ;
+ rp = &A [Row [r].start] ;
+ rp_end = rp + len ;
+ while (rp < rp_end)
+ {
+ c = *rp++ ;
+ if (COL_IS_ALIVE (c))
+ {
+ i++ ;
+ }
+ }
+ ASSERT (i > 0) ;
+ }
+ }
+}
+
+
+/* ========================================================================== */
+/* === debug_deg_lists ====================================================== */
+/* ========================================================================== */
+
+/*
+ Prints the contents of the degree lists. Counts the number of columns
+ in the degree list and compares it to the total it should have. Also
+ checks the row degrees.
+*/
+
+PRIVATE void debug_deg_lists
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int head [],
+ Int min_score,
+ Int should,
+ Int max_deg
+)
+{
+ /* === Local variables ================================================== */
+
+ Int deg ;
+ Int col ;
+ Int have ;
+ Int row ;
+
+ /* === Check the degree lists =========================================== */
+
+ if (n_col > 10000 && colamd_debug <= 0)
+ {
+ return ;
+ }
+ have = 0 ;
+ DEBUG4 (("Degree lists: %d\n", min_score)) ;
+ for (deg = 0 ; deg <= n_col ; deg++)
+ {
+ col = head [deg] ;
+ if (col == EMPTY)
+ {
+ continue ;
+ }
+ DEBUG4 (("%d:", deg)) ;
+ while (col != EMPTY)
+ {
+ DEBUG4 ((" %d", col)) ;
+ have += Col [col].shared1.thickness ;
+ ASSERT (COL_IS_ALIVE (col)) ;
+ col = Col [col].shared4.degree_next ;
+ }
+ DEBUG4 (("\n")) ;
+ }
+ DEBUG4 (("should %d have %d\n", should, have)) ;
+ ASSERT (should == have) ;
+
+ /* === Check the row degrees ============================================ */
+
+ if (n_row > 10000 && colamd_debug <= 0)
+ {
+ return ;
+ }
+ for (row = 0 ; row < n_row ; row++)
+ {
+ if (ROW_IS_ALIVE (row))
+ {
+ ASSERT (Row [row].shared1.degree <= max_deg) ;
+ }
+ }
+}
+
+
+/* ========================================================================== */
+/* === debug_mark =========================================================== */
+/* ========================================================================== */
+
+/*
+ Ensures that the tag_mark is less that the maximum and also ensures that
+ each entry in the mark array is less than the tag mark.
+*/
+
+PRIVATE void debug_mark
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row,
+ Colamd_Row Row [],
+ Int tag_mark,
+ Int max_mark
+)
+{
+ /* === Local variables ================================================== */
+
+ Int r ;
+
+ /* === Check the Row marks ============================================== */
+
+ ASSERT (tag_mark > 0 && tag_mark <= max_mark) ;
+ if (n_row > 10000 && colamd_debug <= 0)
+ {
+ return ;
+ }
+ for (r = 0 ; r < n_row ; r++)
+ {
+ ASSERT (Row [r].shared2.mark < tag_mark) ;
+ }
+}
+
+
+/* ========================================================================== */
+/* === debug_matrix ========================================================= */
+/* ========================================================================== */
+
+/*
+ Prints out the contents of the columns and the rows.
+*/
+
+PRIVATE void debug_matrix
+(
+ /* === Parameters ======================================================= */
+
+ Int n_row,
+ Int n_col,
+ Colamd_Row Row [],
+ Colamd_Col Col [],
+ Int A []
+)
+{
+ /* === Local variables ================================================== */
+
+ Int r ;
+ Int c ;
+ Int *rp ;
+ Int *rp_end ;
+ Int *cp ;
+ Int *cp_end ;
+
+ /* === Dump the rows and columns of the matrix ========================== */
+
+ if (colamd_debug < 3)
+ {
+ return ;
+ }
+ DEBUG3 (("DUMP MATRIX:\n")) ;
+ for (r = 0 ; r < n_row ; r++)
+ {
+ DEBUG3 (("Row %d alive? %d\n", r, ROW_IS_ALIVE (r))) ;
+ if (ROW_IS_DEAD (r))
+ {
+ continue ;
+ }
+ DEBUG3 (("start %d length %d degree %d\n",
+ Row [r].start, Row [r].length, Row [r].shared1.degree)) ;
+ rp = &A [Row [r].start] ;
+ rp_end = rp + Row [r].length ;
+ while (rp < rp_end)
+ {
+ c = *rp++ ;
+ DEBUG4 ((" %d col %d\n", COL_IS_ALIVE (c), c)) ;
+ }
+ }
+
+ for (c = 0 ; c < n_col ; c++)
+ {
+ DEBUG3 (("Col %d alive? %d\n", c, COL_IS_ALIVE (c))) ;
+ if (COL_IS_DEAD (c))
+ {
+ continue ;
+ }
+ DEBUG3 (("start %d length %d shared1 %d shared2 %d\n",
+ Col [c].start, Col [c].length,
+ Col [c].shared1.thickness, Col [c].shared2.score)) ;
+ cp = &A [Col [c].start] ;
+ cp_end = cp + Col [c].length ;
+ while (cp < cp_end)
+ {
+ r = *cp++ ;
+ DEBUG4 ((" %d row %d\n", ROW_IS_ALIVE (r), r)) ;
+ }
+ }
+}
+
+PRIVATE void colamd_get_debug
+(
+ char *method
+)
+{
+ FILE *f ;
+ colamd_debug = 0 ; /* no debug printing */
+ f = fopen ("debug", "r") ;
+ if (f == (FILE *) NULL)
+ {
+ colamd_debug = 0 ;
+ }
+ else
+ {
+ fscanf (f, "%d", &colamd_debug) ;
+ fclose (f) ;
+ }
+ DEBUG0 (("%s: debug version, D = %d (THIS WILL BE SLOW!)\n",
+ method, colamd_debug)) ;
+}
+
+#endif /* NDEBUG */
diff --git a/test/monniaux/glpk-4.65/src/colamd/colamd.h b/test/monniaux/glpk-4.65/src/colamd/colamd.h
new file mode 100644
index 00000000..511735e5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/colamd/colamd.h
@@ -0,0 +1,69 @@
+/* colamd.h */
+
+/* Written by Andrew Makhorin <mao@gnu.org>. */
+
+#ifndef COLAMD_H
+#define COLAMD_H
+
+#define _GLPSTD_STDIO
+#include "env.h"
+
+#define COLAMD_DATE "Nov 1, 2007"
+#define COLAMD_VERSION_CODE(main, sub) ((main) * 1000 + (sub))
+#define COLAMD_MAIN_VERSION 2
+#define COLAMD_SUB_VERSION 7
+#define COLAMD_SUBSUB_VERSION 1
+#define COLAMD_VERSION \
+ COLAMD_VERSION_CODE(COLAMD_MAIN_VERSION, COLAMD_SUB_VERSION)
+
+#define COLAMD_KNOBS 20
+#define COLAMD_STATS 20
+#define COLAMD_DENSE_ROW 0
+#define COLAMD_DENSE_COL 1
+#define COLAMD_AGGRESSIVE 2
+#define COLAMD_DEFRAG_COUNT 2
+#define COLAMD_STATUS 3
+#define COLAMD_INFO1 4
+#define COLAMD_INFO2 5
+#define COLAMD_INFO3 6
+
+#define COLAMD_OK (0)
+#define COLAMD_OK_BUT_JUMBLED (1)
+#define COLAMD_ERROR_A_not_present (-1)
+#define COLAMD_ERROR_p_not_present (-2)
+#define COLAMD_ERROR_nrow_negative (-3)
+#define COLAMD_ERROR_ncol_negative (-4)
+#define COLAMD_ERROR_nnz_negative (-5)
+#define COLAMD_ERROR_p0_nonzero (-6)
+#define COLAMD_ERROR_A_too_small (-7)
+#define COLAMD_ERROR_col_length_negative (-8)
+#define COLAMD_ERROR_row_index_out_of_bounds (-9)
+#define COLAMD_ERROR_out_of_memory (-10)
+#define COLAMD_ERROR_internal_error (-999)
+
+#define colamd_recommended _glp_colamd_recommended
+size_t colamd_recommended(int nnz, int n_row, int n_col);
+
+#define colamd_set_defaults _glp_colamd_set_defaults
+void colamd_set_defaults(double knobs [COLAMD_KNOBS]);
+
+#define colamd _glp_colamd
+int colamd(int n_row, int n_col, int Alen, int A[], int p[],
+ double knobs[COLAMD_KNOBS], int stats[COLAMD_STATS]);
+
+#define symamd _glp_symamd
+int symamd(int n, int A[], int p[], int perm[],
+ double knobs[COLAMD_KNOBS], int stats[COLAMD_STATS],
+ void *(*allocate)(size_t, size_t), void(*release)(void *));
+
+#define colamd_report _glp_colamd_report
+void colamd_report(int stats[COLAMD_STATS]);
+
+#define symamd_report _glp_symamd_report
+void symamd_report(int stats[COLAMD_STATS]);
+
+#define colamd_printf xprintf
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/bfd.c b/test/monniaux/glpk-4.65/src/draft/bfd.c
new file mode 100644
index 00000000..dece376c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/bfd.c
@@ -0,0 +1,544 @@
+/* bfd.c (LP basis factorization driver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2007, 2014 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "glpk.h"
+#include "env.h"
+#include "bfd.h"
+#include "fhvint.h"
+#include "scfint.h"
+#ifdef GLP_DEBUG
+#include "glpspm.h"
+#endif
+
+struct BFD
+{ /* LP basis factorization driver */
+ int valid;
+ /* factorization is valid only if this flag is set */
+ int type;
+ /* type of factorization used:
+ 0 - interface not established yet
+ 1 - FHV-factorization
+ 2 - Schur-complement-based factorization */
+ union
+ { void *none; /* type = 0 */
+ FHVINT *fhvi; /* type = 1 */
+ SCFINT *scfi; /* type = 2 */
+ } u;
+ /* interface to factorization of LP basis */
+ glp_bfcp parm;
+ /* factorization control parameters */
+#ifdef GLP_DEBUG
+ SPM *B;
+ /* current basis (for testing/debugging only) */
+#endif
+ int upd_cnt;
+ /* factorization update count */
+#if 1 /* 21/IV-2014 */
+ double b_norm;
+ /* 1-norm of matrix B */
+ double i_norm;
+ /* estimated 1-norm of matrix inv(B) */
+#endif
+};
+
+BFD *bfd_create_it(void)
+{ /* create LP basis factorization */
+ BFD *bfd;
+#ifdef GLP_DEBUG
+ xprintf("bfd_create_it: warning: debugging version used\n");
+#endif
+ bfd = talloc(1, BFD);
+ bfd->valid = 0;
+ bfd->type = 0;
+ bfd->u.none = NULL;
+ bfd_set_bfcp(bfd, NULL);
+#ifdef GLP_DEBUG
+ bfd->B = NULL;
+#endif
+ bfd->upd_cnt = 0;
+ return bfd;
+}
+
+#if 0 /* 08/III-2014 */
+void bfd_set_parm(BFD *bfd, const void *parm)
+{ /* change LP basis factorization control parameters */
+ memcpy(&bfd->parm, parm, sizeof(glp_bfcp));
+ return;
+}
+#endif
+
+void bfd_get_bfcp(BFD *bfd, void /* glp_bfcp */ *parm)
+{ /* retrieve LP basis factorization control parameters */
+ memcpy(parm, &bfd->parm, sizeof(glp_bfcp));
+ return;
+}
+
+void bfd_set_bfcp(BFD *bfd, const void /* glp_bfcp */ *parm)
+{ /* change LP basis factorization control parameters */
+ if (parm == NULL)
+ { /* reset to default */
+ memset(&bfd->parm, 0, sizeof(glp_bfcp));
+ bfd->parm.type = GLP_BF_LUF + GLP_BF_FT;
+ bfd->parm.piv_tol = 0.10;
+ bfd->parm.piv_lim = 4;
+ bfd->parm.suhl = 1;
+ bfd->parm.eps_tol = DBL_EPSILON;
+ bfd->parm.nfs_max = 100;
+ bfd->parm.nrs_max = 70;
+ }
+ else
+ memcpy(&bfd->parm, parm, sizeof(glp_bfcp));
+ return;
+}
+
+#if 1 /* 21/IV-2014 */
+struct bfd_info
+{ BFD *bfd;
+ int (*col)(void *info, int j, int ind[], double val[]);
+ void *info;
+};
+
+static int bfd_col(void *info_, int j, int ind[], double val[])
+{ struct bfd_info *info = info_;
+ int t, len;
+ double sum;
+ len = info->col(info->info, j, ind, val);
+ sum = 0.0;
+ for (t = 1; t <= len; t++)
+ { if (val[t] >= 0.0)
+ sum += val[t];
+ else
+ sum -= val[t];
+ }
+ if (info->bfd->b_norm < sum)
+ info->bfd->b_norm = sum;
+ return len;
+}
+#endif
+
+int bfd_factorize(BFD *bfd, int m, /*const int bh[],*/ int (*col1)
+ (void *info, int j, int ind[], double val[]), void *info1)
+{ /* compute LP basis factorization */
+#if 1 /* 21/IV-2014 */
+ struct bfd_info info;
+#endif
+ int type, ret;
+ /*xassert(bh == bh);*/
+ /* invalidate current factorization */
+ bfd->valid = 0;
+ /* determine required factorization type */
+ switch (bfd->parm.type)
+ { case GLP_BF_LUF + GLP_BF_FT:
+ type = 1;
+ break;
+ case GLP_BF_LUF + GLP_BF_BG:
+ case GLP_BF_LUF + GLP_BF_GR:
+ case GLP_BF_BTF + GLP_BF_BG:
+ case GLP_BF_BTF + GLP_BF_GR:
+ type = 2;
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+ /* delete factorization interface, if necessary */
+ switch (bfd->type)
+ { case 0:
+ break;
+ case 1:
+ if (type != 1)
+ { bfd->type = 0;
+ fhvint_delete(bfd->u.fhvi);
+ bfd->u.fhvi = NULL;
+ }
+ break;
+ case 2:
+ if (type != 2)
+ { bfd->type = 0;
+ scfint_delete(bfd->u.scfi);
+ bfd->u.scfi = NULL;
+ }
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+ /* establish factorization interface, if necessary */
+ if (bfd->type == 0)
+ { switch (type)
+ { case 1:
+ bfd->type = 1;
+ xassert(bfd->u.fhvi == NULL);
+ bfd->u.fhvi = fhvint_create();
+ break;
+ case 2:
+ bfd->type = 2;
+ xassert(bfd->u.scfi == NULL);
+ if (!(bfd->parm.type & GLP_BF_BTF))
+ bfd->u.scfi = scfint_create(1);
+ else
+ bfd->u.scfi = scfint_create(2);
+ break;
+ default:
+ xassert(type != type);
+ }
+ }
+ /* try to compute factorization */
+#if 1 /* 21/IV-2014 */
+ bfd->b_norm = bfd->i_norm = 0.0;
+ info.bfd = bfd;
+ info.col = col1;
+ info.info = info1;
+#endif
+ switch (bfd->type)
+ { case 1:
+ bfd->u.fhvi->lufi->sgf_piv_tol = bfd->parm.piv_tol;
+ bfd->u.fhvi->lufi->sgf_piv_lim = bfd->parm.piv_lim;
+ bfd->u.fhvi->lufi->sgf_suhl = bfd->parm.suhl;
+ bfd->u.fhvi->lufi->sgf_eps_tol = bfd->parm.eps_tol;
+ bfd->u.fhvi->nfs_max = bfd->parm.nfs_max;
+ ret = fhvint_factorize(bfd->u.fhvi, m, bfd_col, &info);
+#if 1 /* FIXME */
+ if (ret == 0)
+ bfd->i_norm = fhvint_estimate(bfd->u.fhvi);
+ else
+ ret = BFD_ESING;
+#endif
+ break;
+ case 2:
+ if (bfd->u.scfi->scf.type == 1)
+ { bfd->u.scfi->u.lufi->sgf_piv_tol = bfd->parm.piv_tol;
+ bfd->u.scfi->u.lufi->sgf_piv_lim = bfd->parm.piv_lim;
+ bfd->u.scfi->u.lufi->sgf_suhl = bfd->parm.suhl;
+ bfd->u.scfi->u.lufi->sgf_eps_tol = bfd->parm.eps_tol;
+ }
+ else if (bfd->u.scfi->scf.type == 2)
+ { bfd->u.scfi->u.btfi->sgf_piv_tol = bfd->parm.piv_tol;
+ bfd->u.scfi->u.btfi->sgf_piv_lim = bfd->parm.piv_lim;
+ bfd->u.scfi->u.btfi->sgf_suhl = bfd->parm.suhl;
+ bfd->u.scfi->u.btfi->sgf_eps_tol = bfd->parm.eps_tol;
+ }
+ else
+ xassert(bfd != bfd);
+ bfd->u.scfi->nn_max = bfd->parm.nrs_max;
+ ret = scfint_factorize(bfd->u.scfi, m, bfd_col, &info);
+#if 1 /* FIXME */
+ if (ret == 0)
+ bfd->i_norm = scfint_estimate(bfd->u.scfi);
+ else
+ ret = BFD_ESING;
+#endif
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+#ifdef GLP_DEBUG
+ /* save specified LP basis */
+ if (bfd->B != NULL)
+ spm_delete_mat(bfd->B);
+ bfd->B = spm_create_mat(m, m);
+ { int *ind = talloc(1+m, int);
+ double *val = talloc(1+m, double);
+ int j, k, len;
+ for (j = 1; j <= m; j++)
+ { len = col(info, j, ind, val);
+ for (k = 1; k <= len; k++)
+ spm_new_elem(bfd->B, ind[k], j, val[k]);
+ }
+ tfree(ind);
+ tfree(val);
+ }
+#endif
+ if (ret == 0)
+ { /* factorization has been successfully computed */
+ double cond;
+ bfd->valid = 1;
+#ifdef GLP_DEBUG
+ cond = bfd_condest(bfd);
+ if (cond > 1e9)
+ xprintf("bfd_factorize: warning: cond(B) = %g\n", cond);
+#endif
+ }
+#ifdef GLP_DEBUG
+ xprintf("bfd_factorize: m = %d; ret = %d\n", m, ret);
+#endif
+ bfd->upd_cnt = 0;
+ return ret;
+}
+
+#if 0 /* 21/IV-2014 */
+double bfd_estimate(BFD *bfd)
+{ /* estimate 1-norm of inv(B) */
+ double norm;
+ xassert(bfd->valid);
+ xassert(bfd->upd_cnt == 0);
+ switch (bfd->type)
+ { case 1:
+ norm = fhvint_estimate(bfd->u.fhvi);
+ break;
+ case 2:
+ norm = scfint_estimate(bfd->u.scfi);
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+ return norm;
+}
+#endif
+
+#if 1 /* 21/IV-2014 */
+double bfd_condest(BFD *bfd)
+{ /* estimate condition of B */
+ double cond;
+ xassert(bfd->valid);
+ /*xassert(bfd->upd_cnt == 0);*/
+ cond = bfd->b_norm * bfd->i_norm;
+ if (cond < 1.0)
+ cond = 1.0;
+ return cond;
+}
+#endif
+
+void bfd_ftran(BFD *bfd, double x[])
+{ /* perform forward transformation (solve system B * x = b) */
+#ifdef GLP_DEBUG
+ SPM *B = bfd->B;
+ int m = B->m;
+ double *b = talloc(1+m, double);
+ SPME *e;
+ int k;
+ double s, relerr, maxerr;
+ for (k = 1; k <= m; k++)
+ b[k] = x[k];
+#endif
+ xassert(bfd->valid);
+ switch (bfd->type)
+ { case 1:
+ fhvint_ftran(bfd->u.fhvi, x);
+ break;
+ case 2:
+ scfint_ftran(bfd->u.scfi, x);
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+#ifdef GLP_DEBUG
+ maxerr = 0.0;
+ for (k = 1; k <= m; k++)
+ { s = 0.0;
+ for (e = B->row[k]; e != NULL; e = e->r_next)
+ s += e->val * x[e->j];
+ relerr = (b[k] - s) / (1.0 + fabs(b[k]));
+ if (maxerr < relerr)
+ maxerr = relerr;
+ }
+ if (maxerr > 1e-8)
+ xprintf("bfd_ftran: maxerr = %g; relative error too large\n",
+ maxerr);
+ tfree(b);
+#endif
+ return;
+}
+
+#if 1 /* 30/III-2016 */
+void bfd_ftran_s(BFD *bfd, FVS *x)
+{ /* sparse version of bfd_ftran */
+ /* (sparse mode is not implemented yet) */
+ int n = x->n;
+ int *ind = x->ind;
+ double *vec = x->vec;
+ int j, nnz = 0;
+ bfd_ftran(bfd, vec);
+ for (j = n; j >= 1; j--)
+ { if (vec[j] != 0.0)
+ ind[++nnz] = j;
+ }
+ x->nnz = nnz;
+ return;
+}
+#endif
+
+void bfd_btran(BFD *bfd, double x[])
+{ /* perform backward transformation (solve system B'* x = b) */
+#ifdef GLP_DEBUG
+ SPM *B = bfd->B;
+ int m = B->m;
+ double *b = talloc(1+m, double);
+ SPME *e;
+ int k;
+ double s, relerr, maxerr;
+ for (k = 1; k <= m; k++)
+ b[k] = x[k];
+#endif
+ xassert(bfd->valid);
+ switch (bfd->type)
+ { case 1:
+ fhvint_btran(bfd->u.fhvi, x);
+ break;
+ case 2:
+ scfint_btran(bfd->u.scfi, x);
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+#ifdef GLP_DEBUG
+ maxerr = 0.0;
+ for (k = 1; k <= m; k++)
+ { s = 0.0;
+ for (e = B->col[k]; e != NULL; e = e->c_next)
+ s += e->val * x[e->i];
+ relerr = (b[k] - s) / (1.0 + fabs(b[k]));
+ if (maxerr < relerr)
+ maxerr = relerr;
+ }
+ if (maxerr > 1e-8)
+ xprintf("bfd_btran: maxerr = %g; relative error too large\n",
+ maxerr);
+ tfree(b);
+#endif
+ return;
+}
+
+#if 1 /* 30/III-2016 */
+void bfd_btran_s(BFD *bfd, FVS *x)
+{ /* sparse version of bfd_btran */
+ /* (sparse mode is not implemented yet) */
+ int n = x->n;
+ int *ind = x->ind;
+ double *vec = x->vec;
+ int j, nnz = 0;
+ bfd_btran(bfd, vec);
+ for (j = n; j >= 1; j--)
+ { if (vec[j] != 0.0)
+ ind[++nnz] = j;
+ }
+ x->nnz = nnz;
+ return;
+}
+#endif
+
+int bfd_update(BFD *bfd, int j, int len, const int ind[], const double
+ val[])
+{ /* update LP basis factorization */
+ int ret;
+ xassert(bfd->valid);
+ switch (bfd->type)
+ { case 1:
+ ret = fhvint_update(bfd->u.fhvi, j, len, ind, val);
+#if 1 /* FIXME */
+ switch (ret)
+ { case 0:
+ break;
+ case 1:
+ ret = BFD_ESING;
+ break;
+ case 2:
+ case 3:
+ ret = BFD_ECOND;
+ break;
+ case 4:
+ ret = BFD_ELIMIT;
+ break;
+ case 5:
+ ret = BFD_ECHECK;
+ break;
+ default:
+ xassert(ret != ret);
+ }
+#endif
+ break;
+ case 2:
+ switch (bfd->parm.type & 0x0F)
+ { case GLP_BF_BG:
+ ret = scfint_update(bfd->u.scfi, 1, j, len, ind, val);
+ break;
+ case GLP_BF_GR:
+ ret = scfint_update(bfd->u.scfi, 2, j, len, ind, val);
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+#if 1 /* FIXME */
+ switch (ret)
+ { case 0:
+ break;
+ case 1:
+ ret = BFD_ELIMIT;
+ break;
+ case 2:
+ ret = BFD_ECOND;
+ break;
+ default:
+ xassert(ret != ret);
+ }
+#endif
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+ if (ret != 0)
+ { /* updating factorization failed */
+ bfd->valid = 0;
+ }
+#ifdef GLP_DEBUG
+ /* save updated LP basis */
+ { SPME *e;
+ int k;
+ for (e = bfd->B->col[j]; e != NULL; e = e->c_next)
+ e->val = 0.0;
+ spm_drop_zeros(bfd->B, 0.0);
+ for (k = 1; k <= len; k++)
+ spm_new_elem(bfd->B, ind[k], j, val[k]);
+ }
+#endif
+ if (ret == 0)
+ bfd->upd_cnt++;
+ return ret;
+}
+
+int bfd_get_count(BFD *bfd)
+{ /* determine factorization update count */
+ return bfd->upd_cnt;
+}
+
+void bfd_delete_it(BFD *bfd)
+{ /* delete LP basis factorization */
+ switch (bfd->type)
+ { case 0:
+ break;
+ case 1:
+ fhvint_delete(bfd->u.fhvi);
+ break;
+ case 2:
+ scfint_delete(bfd->u.scfi);
+ break;
+ default:
+ xassert(bfd != bfd);
+ }
+#ifdef GLP_DEBUG
+ if (bfd->B != NULL)
+ spm_delete_mat(bfd->B);
+#endif
+ tfree(bfd);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/bfd.h b/test/monniaux/glpk-4.65/src/draft/bfd.h
new file mode 100644
index 00000000..0ef4c023
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/bfd.h
@@ -0,0 +1,107 @@
+/* bfd.h (LP basis factorization driver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef BFD_H
+#define BFD_H
+
+#if 1 /* 30/III-2016 */
+#include "fvs.h"
+#endif
+
+typedef struct BFD BFD;
+
+/* return codes: */
+#define BFD_ESING 1 /* singular matrix */
+#define BFD_ECOND 2 /* ill-conditioned matrix */
+#define BFD_ECHECK 3 /* insufficient accuracy */
+#define BFD_ELIMIT 4 /* update limit reached */
+#if 0 /* 05/III-2014 */
+#define BFD_EROOM 5 /* SVA overflow */
+#endif
+
+#define bfd_create_it _glp_bfd_create_it
+BFD *bfd_create_it(void);
+/* create LP basis factorization */
+
+#if 0 /* 08/III-2014 */
+#define bfd_set_parm _glp_bfd_set_parm
+void bfd_set_parm(BFD *bfd, const void *parm);
+/* change LP basis factorization control parameters */
+#endif
+
+#define bfd_get_bfcp _glp_bfd_get_bfcp
+void bfd_get_bfcp(BFD *bfd, void /* glp_bfcp */ *parm);
+/* retrieve LP basis factorization control parameters */
+
+#define bfd_set_bfcp _glp_bfd_set_bfcp
+void bfd_set_bfcp(BFD *bfd, const void /* glp_bfcp */ *parm);
+/* change LP basis factorization control parameters */
+
+#define bfd_factorize _glp_bfd_factorize
+int bfd_factorize(BFD *bfd, int m, /*const int bh[],*/ int (*col)
+ (void *info, int j, int ind[], double val[]), void *info);
+/* compute LP basis factorization */
+
+#if 1 /* 21/IV-2014 */
+#define bfd_condest _glp_bfd_condest
+double bfd_condest(BFD *bfd);
+/* estimate condition of B */
+#endif
+
+#define bfd_ftran _glp_bfd_ftran
+void bfd_ftran(BFD *bfd, double x[]);
+/* perform forward transformation (solve system B*x = b) */
+
+#if 1 /* 30/III-2016 */
+#define bfd_ftran_s _glp_bfd_ftran_s
+void bfd_ftran_s(BFD *bfd, FVS *x);
+/* sparse version of bfd_ftran */
+#endif
+
+#define bfd_btran _glp_bfd_btran
+void bfd_btran(BFD *bfd, double x[]);
+/* perform backward transformation (solve system B'*x = b) */
+
+#if 1 /* 30/III-2016 */
+#define bfd_btran_s _glp_bfd_btran_s
+void bfd_btran_s(BFD *bfd, FVS *x);
+/* sparse version of bfd_btran */
+#endif
+
+#define bfd_update _glp_bfd_update
+int bfd_update(BFD *bfd, int j, int len, const int ind[], const double
+ val[]);
+/* update LP basis factorization */
+
+#define bfd_get_count _glp_bfd_get_count
+int bfd_get_count(BFD *bfd);
+/* determine factorization update count */
+
+#define bfd_delete_it _glp_bfd_delete_it
+void bfd_delete_it(BFD *bfd);
+/* delete LP basis factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/bfx.c b/test/monniaux/glpk-4.65/src/draft/bfx.c
new file mode 100644
index 00000000..565480b6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/bfx.c
@@ -0,0 +1,89 @@
+/* bfx.c (LP basis factorization driver, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "bfx.h"
+#include "env.h"
+#include "lux.h"
+
+struct BFX
+{ int valid;
+ LUX *lux;
+};
+
+BFX *bfx_create_binv(void)
+{ /* create factorization of the basis matrix */
+ BFX *bfx;
+ bfx = xmalloc(sizeof(BFX));
+ bfx->valid = 0;
+ bfx->lux = NULL;
+ return bfx;
+}
+
+int bfx_factorize(BFX *binv, int m, int (*col)(void *info, int j,
+ int ind[], mpq_t val[]), void *info)
+{ /* compute factorization of the basis matrix */
+ int ret;
+ xassert(m > 0);
+ if (binv->lux != NULL && binv->lux->n != m)
+ { lux_delete(binv->lux);
+ binv->lux = NULL;
+ }
+ if (binv->lux == NULL)
+ binv->lux = lux_create(m);
+ ret = lux_decomp(binv->lux, col, info);
+ binv->valid = (ret == 0);
+ return ret;
+}
+
+void bfx_ftran(BFX *binv, mpq_t x[], int save)
+{ /* perform forward transformation (FTRAN) */
+ xassert(binv->valid);
+ lux_solve(binv->lux, 0, x);
+ xassert(save == save);
+ return;
+}
+
+void bfx_btran(BFX *binv, mpq_t x[])
+{ /* perform backward transformation (BTRAN) */
+ xassert(binv->valid);
+ lux_solve(binv->lux, 1, x);
+ return;
+}
+
+int bfx_update(BFX *binv, int j)
+{ /* update factorization of the basis matrix */
+ xassert(binv->valid);
+ xassert(1 <= j && j <= binv->lux->n);
+ return 1;
+}
+
+void bfx_delete_binv(BFX *binv)
+{ /* delete factorization of the basis matrix */
+ if (binv->lux != NULL)
+ lux_delete(binv->lux);
+ xfree(binv);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/bfx.h b/test/monniaux/glpk-4.65/src/draft/bfx.h
new file mode 100644
index 00000000..c67d5ea4
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/bfx.h
@@ -0,0 +1,67 @@
+/* bfx.h (LP basis factorization driver, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef BFX_H
+#define BFX_H
+
+#include "mygmp.h"
+
+typedef struct BFX BFX;
+
+#define bfx_create_binv _glp_bfx_create_binv
+BFX *bfx_create_binv(void);
+/* create factorization of the basis matrix */
+
+#define bfx_is_valid _glp_bfx_is_valid
+int bfx_is_valid(BFX *binv);
+/* check if factorization is valid */
+
+#define bfx_invalidate _glp_bfx_invalidate
+void bfx_invalidate(BFX *binv);
+/* invalidate factorization of the basis matrix */
+
+#define bfx_factorize _glp_bfx_factorize
+int bfx_factorize(BFX *binv, int m, int (*col)(void *info, int j,
+ int ind[], mpq_t val[]), void *info);
+/* compute factorization of the basis matrix */
+
+#define bfx_ftran _glp_bfx_ftran
+void bfx_ftran(BFX *binv, mpq_t x[], int save);
+/* perform forward transformation (FTRAN) */
+
+#define bfx_btran _glp_bfx_btran
+void bfx_btran(BFX *binv, mpq_t x[]);
+/* perform backward transformation (BTRAN) */
+
+#define bfx_update _glp_bfx_update
+int bfx_update(BFX *binv, int j);
+/* update factorization of the basis matrix */
+
+#define bfx_delete_binv _glp_bfx_delete_binv
+void bfx_delete_binv(BFX *binv);
+/* delete factorization of the basis matrix */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/draft.h b/test/monniaux/glpk-4.65/src/draft/draft.h
new file mode 100644
index 00000000..cefd2124
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/draft.h
@@ -0,0 +1,22 @@
+/* draft.h */
+
+/* (reserved for copyright notice) */
+
+#ifndef DRAFT_H
+#define DRAFT_H
+
+#if 1 /* 28/III-2016 */
+#define GLP_UNDOC 1
+#endif
+#include "glpk.h"
+
+#if 1 /* 28/XI-2009 */
+int _glp_analyze_row(glp_prob *P, int len, const int ind[],
+ const double val[], int type, double rhs, double eps, int *_piv,
+ double *_x, double *_dx, double *_y, double *_dy, double *_dz);
+/* simulate one iteration of dual simplex method */
+#endif
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi06.c b/test/monniaux/glpk-4.65/src/draft/glpapi06.c
new file mode 100644
index 00000000..a31e3968
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi06.c
@@ -0,0 +1,860 @@
+/* glpapi06.c (simplex method routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+#include "npp.h"
+#if 0 /* 07/XI-2015 */
+#include "glpspx.h"
+#else
+#include "simplex.h"
+#define spx_dual spy_dual
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_simplex - solve LP problem with the simplex method
+*
+* SYNOPSIS
+*
+* int glp_simplex(glp_prob *P, const glp_smcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_simplex is a driver to the LP solver based on the
+* simplex method. This routine retrieves problem data from the
+* specified problem object, calls the solver to solve the problem
+* instance, and stores results of computations back into the problem
+* object.
+*
+* The simplex solver has a set of control parameters. Values of the
+* control parameters can be passed in a structure glp_smcp, which the
+* parameter parm points to.
+*
+* The parameter parm can be specified as NULL, in which case the LP
+* solver uses default settings.
+*
+* RETURNS
+*
+* 0 The LP problem instance has been successfully solved. This code
+* does not necessarily mean that the solver has found optimal
+* solution. It only means that the solution process was successful.
+*
+* GLP_EBADB
+* Unable to start the search, because the initial basis specified
+* in the problem object is invalid--the number of basic (auxiliary
+* and structural) variables is not the same as the number of rows in
+* the problem object.
+*
+* GLP_ESING
+* Unable to start the search, because the basis matrix correspodning
+* to the initial basis is singular within the working precision.
+*
+* GLP_ECOND
+* Unable to start the search, because the basis matrix correspodning
+* to the initial basis is ill-conditioned, i.e. its condition number
+* is too large.
+*
+* GLP_EBOUND
+* Unable to start the search, because some double-bounded variables
+* have incorrect bounds.
+*
+* GLP_EFAIL
+* The search was prematurely terminated due to the solver failure.
+*
+* GLP_EOBJLL
+* The search was prematurely terminated, because the objective
+* function being maximized has reached its lower limit and continues
+* decreasing (dual simplex only).
+*
+* GLP_EOBJUL
+* The search was prematurely terminated, because the objective
+* function being minimized has reached its upper limit and continues
+* increasing (dual simplex only).
+*
+* GLP_EITLIM
+* The search was prematurely terminated, because the simplex
+* iteration limit has been exceeded.
+*
+* GLP_ETMLIM
+* The search was prematurely terminated, because the time limit has
+* been exceeded.
+*
+* GLP_ENOPFS
+* The LP problem instance has no primal feasible solution (only if
+* the LP presolver is used).
+*
+* GLP_ENODFS
+* The LP problem instance has no dual feasible solution (only if the
+* LP presolver is used). */
+
+static void trivial_lp(glp_prob *P, const glp_smcp *parm)
+{ /* solve trivial LP which has empty constraint matrix */
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j;
+ double p_infeas, d_infeas, zeta;
+ P->valid = 0;
+ P->pbs_stat = P->dbs_stat = GLP_FEAS;
+ P->obj_val = P->c0;
+ P->some = 0;
+ p_infeas = d_infeas = 0.0;
+ /* make all auxiliary variables basic */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ row->stat = GLP_BS;
+ row->prim = row->dual = 0.0;
+ /* check primal feasibility */
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ { /* row has lower bound */
+ if (row->lb > + parm->tol_bnd)
+ { P->pbs_stat = GLP_NOFEAS;
+ if (P->some == 0 && parm->meth != GLP_PRIMAL)
+ P->some = i;
+ }
+ if (p_infeas < + row->lb)
+ p_infeas = + row->lb;
+ }
+ if (row->type == GLP_UP || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ { /* row has upper bound */
+ if (row->ub < - parm->tol_bnd)
+ { P->pbs_stat = GLP_NOFEAS;
+ if (P->some == 0 && parm->meth != GLP_PRIMAL)
+ P->some = i;
+ }
+ if (p_infeas < - row->ub)
+ p_infeas = - row->ub;
+ }
+ }
+ /* determine scale factor for the objective row */
+ zeta = 1.0;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (zeta < fabs(col->coef)) zeta = fabs(col->coef);
+ }
+ zeta = (P->dir == GLP_MIN ? +1.0 : -1.0) / zeta;
+ /* make all structural variables non-basic */
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->type == GLP_FR)
+ col->stat = GLP_NF, col->prim = 0.0;
+ else if (col->type == GLP_LO)
+lo: col->stat = GLP_NL, col->prim = col->lb;
+ else if (col->type == GLP_UP)
+up: col->stat = GLP_NU, col->prim = col->ub;
+ else if (col->type == GLP_DB)
+ { if (zeta * col->coef > 0.0)
+ goto lo;
+ else if (zeta * col->coef < 0.0)
+ goto up;
+ else if (fabs(col->lb) <= fabs(col->ub))
+ goto lo;
+ else
+ goto up;
+ }
+ else if (col->type == GLP_FX)
+ col->stat = GLP_NS, col->prim = col->lb;
+ col->dual = col->coef;
+ P->obj_val += col->coef * col->prim;
+ /* check dual feasibility */
+ if (col->type == GLP_FR || col->type == GLP_LO)
+ { /* column has no upper bound */
+ if (zeta * col->dual < - parm->tol_dj)
+ { P->dbs_stat = GLP_NOFEAS;
+ if (P->some == 0 && parm->meth == GLP_PRIMAL)
+ P->some = P->m + j;
+ }
+ if (d_infeas < - zeta * col->dual)
+ d_infeas = - zeta * col->dual;
+ }
+ if (col->type == GLP_FR || col->type == GLP_UP)
+ { /* column has no lower bound */
+ if (zeta * col->dual > + parm->tol_dj)
+ { P->dbs_stat = GLP_NOFEAS;
+ if (P->some == 0 && parm->meth == GLP_PRIMAL)
+ P->some = P->m + j;
+ }
+ if (d_infeas < + zeta * col->dual)
+ d_infeas = + zeta * col->dual;
+ }
+ }
+ /* simulate the simplex solver output */
+ if (parm->msg_lev >= GLP_MSG_ON && parm->out_dly == 0)
+ { xprintf("~%6d: obj = %17.9e infeas = %10.3e\n", P->it_cnt,
+ P->obj_val, parm->meth == GLP_PRIMAL ? p_infeas : d_infeas);
+ }
+ if (parm->msg_lev >= GLP_MSG_ALL && parm->out_dly == 0)
+ { if (P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS)
+ xprintf("OPTIMAL SOLUTION FOUND\n");
+ else if (P->pbs_stat == GLP_NOFEAS)
+ xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
+ else if (parm->meth == GLP_PRIMAL)
+ xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
+ else
+ xprintf("PROBLEM HAS NO DUAL FEASIBLE SOLUTION\n");
+ }
+ return;
+}
+
+static int solve_lp(glp_prob *P, const glp_smcp *parm)
+{ /* solve LP directly without using the preprocessor */
+ int ret;
+ if (!glp_bf_exists(P))
+ { ret = glp_factorize(P);
+ if (ret == 0)
+ ;
+ else if (ret == GLP_EBADB)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_simplex: initial basis is invalid\n");
+ }
+ else if (ret == GLP_ESING)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_simplex: initial basis is singular\n");
+ }
+ else if (ret == GLP_ECOND)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf(
+ "glp_simplex: initial basis is ill-conditioned\n");
+ }
+ else
+ xassert(ret != ret);
+ if (ret != 0) goto done;
+ }
+ if (parm->meth == GLP_PRIMAL)
+ ret = spx_primal(P, parm);
+ else if (parm->meth == GLP_DUALP)
+ { ret = spx_dual(P, parm);
+ if (ret == GLP_EFAIL && P->valid)
+ ret = spx_primal(P, parm);
+ }
+ else if (parm->meth == GLP_DUAL)
+ ret = spx_dual(P, parm);
+ else
+ xassert(parm != parm);
+done: return ret;
+}
+
+static int preprocess_and_solve_lp(glp_prob *P, const glp_smcp *parm)
+{ /* solve LP using the preprocessor */
+ NPP *npp;
+ glp_prob *lp = NULL;
+ glp_bfcp bfcp;
+ int ret;
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Preprocessing...\n");
+ /* create preprocessor workspace */
+ npp = npp_create_wksp();
+ /* load original problem into the preprocessor workspace */
+ npp_load_prob(npp, P, GLP_OFF, GLP_SOL, GLP_OFF);
+ /* process LP prior to applying primal/dual simplex method */
+ ret = npp_simplex(npp, parm);
+ if (ret == 0)
+ ;
+ else if (ret == GLP_ENOPFS)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("PROBLEM HAS NO PRIMAL FEASIBLE SOLUTION\n");
+ }
+ else if (ret == GLP_ENODFS)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("PROBLEM HAS NO DUAL FEASIBLE SOLUTION\n");
+ }
+ else
+ xassert(ret != ret);
+ if (ret != 0) goto done;
+ /* build transformed LP */
+ lp = glp_create_prob();
+ npp_build_prob(npp, lp);
+ /* if the transformed LP is empty, it has empty solution, which
+ is optimal */
+ if (lp->m == 0 && lp->n == 0)
+ { lp->pbs_stat = lp->dbs_stat = GLP_FEAS;
+ lp->obj_val = lp->c0;
+ if (parm->msg_lev >= GLP_MSG_ON && parm->out_dly == 0)
+ { xprintf("~%6d: obj = %17.9e infeas = %10.3e\n", P->it_cnt,
+ lp->obj_val, 0.0);
+ }
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("OPTIMAL SOLUTION FOUND BY LP PREPROCESSOR\n");
+ goto post;
+ }
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ { xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ lp->m, lp->m == 1 ? "" : "s", lp->n, lp->n == 1 ? "" : "s",
+ lp->nnz, lp->nnz == 1 ? "" : "s");
+ }
+ /* inherit basis factorization control parameters */
+ glp_get_bfcp(P, &bfcp);
+ glp_set_bfcp(lp, &bfcp);
+ /* scale the transformed problem */
+ { ENV *env = get_env_ptr();
+ int term_out = env->term_out;
+ if (!term_out || parm->msg_lev < GLP_MSG_ALL)
+ env->term_out = GLP_OFF;
+ else
+ env->term_out = GLP_ON;
+ glp_scale_prob(lp, GLP_SF_AUTO);
+ env->term_out = term_out;
+ }
+ /* build advanced initial basis */
+ { ENV *env = get_env_ptr();
+ int term_out = env->term_out;
+ if (!term_out || parm->msg_lev < GLP_MSG_ALL)
+ env->term_out = GLP_OFF;
+ else
+ env->term_out = GLP_ON;
+ glp_adv_basis(lp, 0);
+ env->term_out = term_out;
+ }
+ /* solve the transformed LP */
+ lp->it_cnt = P->it_cnt;
+ ret = solve_lp(lp, parm);
+ P->it_cnt = lp->it_cnt;
+ /* only optimal solution can be postprocessed */
+ if (!(ret == 0 && lp->pbs_stat == GLP_FEAS && lp->dbs_stat ==
+ GLP_FEAS))
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_simplex: unable to recover undefined or non-op"
+ "timal solution\n");
+ if (ret == 0)
+ { if (lp->pbs_stat == GLP_NOFEAS)
+ ret = GLP_ENOPFS;
+ else if (lp->dbs_stat == GLP_NOFEAS)
+ ret = GLP_ENODFS;
+ else
+ xassert(lp != lp);
+ }
+ goto done;
+ }
+post: /* postprocess solution from the transformed LP */
+ npp_postprocess(npp, lp);
+ /* the transformed LP is no longer needed */
+ glp_delete_prob(lp), lp = NULL;
+ /* store solution to the original problem */
+ npp_unload_sol(npp, P);
+ /* the original LP has been successfully solved */
+ ret = 0;
+done: /* delete the transformed LP, if it exists */
+ if (lp != NULL) glp_delete_prob(lp);
+ /* delete preprocessor workspace */
+ npp_delete_wksp(npp);
+ return ret;
+}
+
+int glp_simplex(glp_prob *P, const glp_smcp *parm)
+{ /* solve LP problem with the simplex method */
+ glp_smcp _parm;
+ int i, j, ret;
+ /* check problem object */
+#if 0 /* 04/IV-2016 */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_simplex: P = %p; invalid problem object\n", P);
+#endif
+ if (P->tree != NULL && P->tree->reason != 0)
+ xerror("glp_simplex: operation not allowed\n");
+ /* check control parameters */
+ if (parm == NULL)
+ parm = &_parm, glp_init_smcp((glp_smcp *)parm);
+ if (!(parm->msg_lev == GLP_MSG_OFF ||
+ parm->msg_lev == GLP_MSG_ERR ||
+ parm->msg_lev == GLP_MSG_ON ||
+ parm->msg_lev == GLP_MSG_ALL ||
+ parm->msg_lev == GLP_MSG_DBG))
+ xerror("glp_simplex: msg_lev = %d; invalid parameter\n",
+ parm->msg_lev);
+ if (!(parm->meth == GLP_PRIMAL ||
+ parm->meth == GLP_DUALP ||
+ parm->meth == GLP_DUAL))
+ xerror("glp_simplex: meth = %d; invalid parameter\n",
+ parm->meth);
+ if (!(parm->pricing == GLP_PT_STD ||
+ parm->pricing == GLP_PT_PSE))
+ xerror("glp_simplex: pricing = %d; invalid parameter\n",
+ parm->pricing);
+ if (!(parm->r_test == GLP_RT_STD ||
+#if 1 /* 16/III-2016 */
+ parm->r_test == GLP_RT_FLIP ||
+#endif
+ parm->r_test == GLP_RT_HAR))
+ xerror("glp_simplex: r_test = %d; invalid parameter\n",
+ parm->r_test);
+ if (!(0.0 < parm->tol_bnd && parm->tol_bnd < 1.0))
+ xerror("glp_simplex: tol_bnd = %g; invalid parameter\n",
+ parm->tol_bnd);
+ if (!(0.0 < parm->tol_dj && parm->tol_dj < 1.0))
+ xerror("glp_simplex: tol_dj = %g; invalid parameter\n",
+ parm->tol_dj);
+ if (!(0.0 < parm->tol_piv && parm->tol_piv < 1.0))
+ xerror("glp_simplex: tol_piv = %g; invalid parameter\n",
+ parm->tol_piv);
+ if (parm->it_lim < 0)
+ xerror("glp_simplex: it_lim = %d; invalid parameter\n",
+ parm->it_lim);
+ if (parm->tm_lim < 0)
+ xerror("glp_simplex: tm_lim = %d; invalid parameter\n",
+ parm->tm_lim);
+#if 0 /* 15/VII-2017 */
+ if (parm->out_frq < 1)
+#else
+ if (parm->out_frq < 0)
+#endif
+ xerror("glp_simplex: out_frq = %d; invalid parameter\n",
+ parm->out_frq);
+ if (parm->out_dly < 0)
+ xerror("glp_simplex: out_dly = %d; invalid parameter\n",
+ parm->out_dly);
+ if (!(parm->presolve == GLP_ON || parm->presolve == GLP_OFF))
+ xerror("glp_simplex: presolve = %d; invalid parameter\n",
+ parm->presolve);
+#if 1 /* 11/VII-2017 */
+ if (!(parm->excl == GLP_ON || parm->excl == GLP_OFF))
+ xerror("glp_simplex: excl = %d; invalid parameter\n",
+ parm->excl);
+ if (!(parm->shift == GLP_ON || parm->shift == GLP_OFF))
+ xerror("glp_simplex: shift = %d; invalid parameter\n",
+ parm->shift);
+ if (!(parm->aorn == GLP_USE_AT || parm->aorn == GLP_USE_NT))
+ xerror("glp_simplex: aorn = %d; invalid parameter\n",
+ parm->aorn);
+#endif
+ /* basic solution is currently undefined */
+ P->pbs_stat = P->dbs_stat = GLP_UNDEF;
+ P->obj_val = 0.0;
+ P->some = 0;
+ /* check bounds of double-bounded variables */
+ for (i = 1; i <= P->m; i++)
+ { GLPROW *row = P->row[i];
+ if (row->type == GLP_DB && row->lb >= row->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_simplex: row %d: lb = %g, ub = %g; incorrec"
+ "t bounds\n", i, row->lb, row->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if (col->type == GLP_DB && col->lb >= col->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_simplex: column %d: lb = %g, ub = %g; incor"
+ "rect bounds\n", j, col->lb, col->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ /* solve LP problem */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ { xprintf("GLPK Simplex Optimizer, v%s\n", glp_version());
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
+ P->nnz, P->nnz == 1 ? "" : "s");
+ }
+ if (P->nnz == 0)
+ trivial_lp(P, parm), ret = 0;
+ else if (!parm->presolve)
+ ret = solve_lp(P, parm);
+ else
+ ret = preprocess_and_solve_lp(P, parm);
+done: /* return to the application program */
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_init_smcp - initialize simplex method control parameters
+*
+* SYNOPSIS
+*
+* void glp_init_smcp(glp_smcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_init_smcp initializes control parameters, which are
+* used by the simplex solver, with default values.
+*
+* Default values of the control parameters are stored in a glp_smcp
+* structure, which the parameter parm points to. */
+
+void glp_init_smcp(glp_smcp *parm)
+{ parm->msg_lev = GLP_MSG_ALL;
+ parm->meth = GLP_PRIMAL;
+ parm->pricing = GLP_PT_PSE;
+ parm->r_test = GLP_RT_HAR;
+ parm->tol_bnd = 1e-7;
+ parm->tol_dj = 1e-7;
+#if 0 /* 07/XI-2015 */
+ parm->tol_piv = 1e-10;
+#else
+ parm->tol_piv = 1e-9;
+#endif
+ parm->obj_ll = -DBL_MAX;
+ parm->obj_ul = +DBL_MAX;
+ parm->it_lim = INT_MAX;
+ parm->tm_lim = INT_MAX;
+#if 0 /* 15/VII-2017 */
+ parm->out_frq = 500;
+#else
+ parm->out_frq = 5000; /* 5 seconds */
+#endif
+ parm->out_dly = 0;
+ parm->presolve = GLP_OFF;
+#if 1 /* 11/VII-2017 */
+ parm->excl = GLP_ON;
+ parm->shift = GLP_ON;
+ parm->aorn = GLP_USE_NT;
+#endif
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_status - retrieve generic status of basic solution
+*
+* SYNOPSIS
+*
+* int glp_get_status(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_status reports the generic status of the basic
+* solution for the specified problem object as follows:
+*
+* GLP_OPT - solution is optimal;
+* GLP_FEAS - solution is feasible;
+* GLP_INFEAS - solution is infeasible;
+* GLP_NOFEAS - problem has no feasible solution;
+* GLP_UNBND - problem has unbounded solution;
+* GLP_UNDEF - solution is undefined. */
+
+int glp_get_status(glp_prob *lp)
+{ int status;
+ status = glp_get_prim_stat(lp);
+ switch (status)
+ { case GLP_FEAS:
+ switch (glp_get_dual_stat(lp))
+ { case GLP_FEAS:
+ status = GLP_OPT;
+ break;
+ case GLP_NOFEAS:
+ status = GLP_UNBND;
+ break;
+ case GLP_UNDEF:
+ case GLP_INFEAS:
+ status = status;
+ break;
+ default:
+ xassert(lp != lp);
+ }
+ break;
+ case GLP_UNDEF:
+ case GLP_INFEAS:
+ case GLP_NOFEAS:
+ status = status;
+ break;
+ default:
+ xassert(lp != lp);
+ }
+ return status;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_prim_stat - retrieve status of primal basic solution
+*
+* SYNOPSIS
+*
+* int glp_get_prim_stat(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_prim_stat reports the status of the primal basic
+* solution for the specified problem object as follows:
+*
+* GLP_UNDEF - primal solution is undefined;
+* GLP_FEAS - primal solution is feasible;
+* GLP_INFEAS - primal solution is infeasible;
+* GLP_NOFEAS - no primal feasible solution exists. */
+
+int glp_get_prim_stat(glp_prob *lp)
+{ int pbs_stat = lp->pbs_stat;
+ return pbs_stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_dual_stat - retrieve status of dual basic solution
+*
+* SYNOPSIS
+*
+* int glp_get_dual_stat(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_dual_stat reports the status of the dual basic
+* solution for the specified problem object as follows:
+*
+* GLP_UNDEF - dual solution is undefined;
+* GLP_FEAS - dual solution is feasible;
+* GLP_INFEAS - dual solution is infeasible;
+* GLP_NOFEAS - no dual feasible solution exists. */
+
+int glp_get_dual_stat(glp_prob *lp)
+{ int dbs_stat = lp->dbs_stat;
+ return dbs_stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_obj_val - retrieve objective value (basic solution)
+*
+* SYNOPSIS
+*
+* double glp_get_obj_val(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_obj_val returns value of the objective function
+* for basic solution. */
+
+double glp_get_obj_val(glp_prob *lp)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double z;
+ z = lp->obj_val;
+ /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
+ return z;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_stat - retrieve row status
+*
+* SYNOPSIS
+*
+* int glp_get_row_stat(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_stat returns current status assigned to the
+* auxiliary variable associated with i-th row as follows:
+*
+* GLP_BS - basic variable;
+* GLP_NL - non-basic variable on its lower bound;
+* GLP_NU - non-basic variable on its upper bound;
+* GLP_NF - non-basic free (unbounded) variable;
+* GLP_NS - non-basic fixed variable. */
+
+int glp_get_row_stat(glp_prob *lp, int i)
+{ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_stat: i = %d; row number out of range\n",
+ i);
+ return lp->row[i]->stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_prim - retrieve row primal value (basic solution)
+*
+* SYNOPSIS
+*
+* double glp_get_row_prim(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_prim returns primal value of the auxiliary
+* variable associated with i-th row. */
+
+double glp_get_row_prim(glp_prob *lp, int i)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double prim;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_prim: i = %d; row number out of range\n",
+ i);
+ prim = lp->row[i]->prim;
+ /*if (cps->round && fabs(prim) < 1e-9) prim = 0.0;*/
+ return prim;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_dual - retrieve row dual value (basic solution)
+*
+* SYNOPSIS
+*
+* double glp_get_row_dual(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_dual returns dual value (i.e. reduced cost)
+* of the auxiliary variable associated with i-th row. */
+
+double glp_get_row_dual(glp_prob *lp, int i)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double dual;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_dual: i = %d; row number out of range\n",
+ i);
+ dual = lp->row[i]->dual;
+ /*if (cps->round && fabs(dual) < 1e-9) dual = 0.0;*/
+ return dual;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_stat - retrieve column status
+*
+* SYNOPSIS
+*
+* int glp_get_col_stat(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_stat returns current status assigned to the
+* structural variable associated with j-th column as follows:
+*
+* GLP_BS - basic variable;
+* GLP_NL - non-basic variable on its lower bound;
+* GLP_NU - non-basic variable on its upper bound;
+* GLP_NF - non-basic free (unbounded) variable;
+* GLP_NS - non-basic fixed variable. */
+
+int glp_get_col_stat(glp_prob *lp, int j)
+{ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_stat: j = %d; column number out of range\n"
+ , j);
+ return lp->col[j]->stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_prim - retrieve column primal value (basic solution)
+*
+* SYNOPSIS
+*
+* double glp_get_col_prim(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_prim returns primal value of the structural
+* variable associated with j-th column. */
+
+double glp_get_col_prim(glp_prob *lp, int j)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double prim;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_prim: j = %d; column number out of range\n"
+ , j);
+ prim = lp->col[j]->prim;
+ /*if (cps->round && fabs(prim) < 1e-9) prim = 0.0;*/
+ return prim;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_dual - retrieve column dual value (basic solution)
+*
+* SYNOPSIS
+*
+* double glp_get_col_dual(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_dual returns dual value (i.e. reduced cost)
+* of the structural variable associated with j-th column. */
+
+double glp_get_col_dual(glp_prob *lp, int j)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double dual;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_dual: j = %d; column number out of range\n"
+ , j);
+ dual = lp->col[j]->dual;
+ /*if (cps->round && fabs(dual) < 1e-9) dual = 0.0;*/
+ return dual;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_unbnd_ray - determine variable causing unboundedness
+*
+* SYNOPSIS
+*
+* int glp_get_unbnd_ray(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_get_unbnd_ray returns the number k of a variable,
+* which causes primal or dual unboundedness. If 1 <= k <= m, it is
+* k-th auxiliary variable, and if m+1 <= k <= m+n, it is (k-m)-th
+* structural variable, where m is the number of rows, n is the number
+* of columns in the problem object. If such variable is not defined,
+* the routine returns 0.
+*
+* COMMENTS
+*
+* If it is not exactly known which version of the simplex solver
+* detected unboundedness, i.e. whether the unboundedness is primal or
+* dual, it is sufficient to check the status of the variable reported
+* with the routine glp_get_row_stat or glp_get_col_stat. If the
+* variable is non-basic, the unboundedness is primal, otherwise, if
+* the variable is basic, the unboundedness is dual (the latter case
+* means that the problem has no primal feasible dolution). */
+
+int glp_get_unbnd_ray(glp_prob *lp)
+{ int k;
+ k = lp->some;
+ xassert(k >= 0);
+ if (k > lp->m + lp->n) k = 0;
+ return k;
+}
+
+#if 1 /* 08/VIII-2013 */
+int glp_get_it_cnt(glp_prob *P)
+{ /* get simplex solver iteration count */
+ return P->it_cnt;
+}
+#endif
+
+#if 1 /* 08/VIII-2013 */
+void glp_set_it_cnt(glp_prob *P, int it_cnt)
+{ /* set simplex solver iteration count */
+ P->it_cnt = it_cnt;
+ return;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi07.c b/test/monniaux/glpk-4.65/src/draft/glpapi07.c
new file mode 100644
index 00000000..9ac294bd
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi07.c
@@ -0,0 +1,499 @@
+/* glpapi07.c (exact simplex solver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "draft.h"
+#include "glpssx.h"
+#include "misc.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_exact - solve LP problem in exact arithmetic
+*
+* SYNOPSIS
+*
+* int glp_exact(glp_prob *lp, const glp_smcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_exact is a tentative implementation of the primal
+* two-phase simplex method based on exact (rational) arithmetic. It is
+* similar to the routine glp_simplex, however, for all internal
+* computations it uses arithmetic of rational numbers, which is exact
+* in mathematical sense, i.e. free of round-off errors unlike floating
+* point arithmetic.
+*
+* Note that the routine glp_exact uses inly two control parameters
+* passed in the structure glp_smcp, namely, it_lim and tm_lim.
+*
+* RETURNS
+*
+* 0 The LP problem instance has been successfully solved. This code
+* does not necessarily mean that the solver has found optimal
+* solution. It only means that the solution process was successful.
+*
+* GLP_EBADB
+* Unable to start the search, because the initial basis specified
+* in the problem object is invalid--the number of basic (auxiliary
+* and structural) variables is not the same as the number of rows in
+* the problem object.
+*
+* GLP_ESING
+* Unable to start the search, because the basis matrix correspodning
+* to the initial basis is exactly singular.
+*
+* GLP_EBOUND
+* Unable to start the search, because some double-bounded variables
+* have incorrect bounds.
+*
+* GLP_EFAIL
+* The problem has no rows/columns.
+*
+* GLP_EITLIM
+* The search was prematurely terminated, because the simplex
+* iteration limit has been exceeded.
+*
+* GLP_ETMLIM
+* The search was prematurely terminated, because the time limit has
+* been exceeded. */
+
+static void set_d_eps(mpq_t x, double val)
+{ /* convert double val to rational x obtaining a more adequate
+ fraction than provided by mpq_set_d due to allowing a small
+ approximation error specified by a given relative tolerance;
+ for example, mpq_set_d would give the following
+ 1/3 ~= 0.333333333333333314829616256247391... ->
+ -> 6004799503160661/18014398509481984
+ while this routine gives exactly 1/3 */
+ int s, n, j;
+ double f, p, q, eps = 1e-9;
+ mpq_t temp;
+ xassert(-DBL_MAX <= val && val <= +DBL_MAX);
+#if 1 /* 30/VII-2008 */
+ if (val == floor(val))
+ { /* if val is integral, do not approximate */
+ mpq_set_d(x, val);
+ goto done;
+ }
+#endif
+ if (val > 0.0)
+ s = +1;
+ else if (val < 0.0)
+ s = -1;
+ else
+ { mpq_set_si(x, 0, 1);
+ goto done;
+ }
+ f = frexp(fabs(val), &n);
+ /* |val| = f * 2^n, where 0.5 <= f < 1.0 */
+ fp2rat(f, 0.1 * eps, &p, &q);
+ /* f ~= p / q, where p and q are integers */
+ mpq_init(temp);
+ mpq_set_d(x, p);
+ mpq_set_d(temp, q);
+ mpq_div(x, x, temp);
+ mpq_set_si(temp, 1, 1);
+ for (j = 1; j <= abs(n); j++)
+ mpq_add(temp, temp, temp);
+ if (n > 0)
+ mpq_mul(x, x, temp);
+ else if (n < 0)
+ mpq_div(x, x, temp);
+ mpq_clear(temp);
+ if (s < 0) mpq_neg(x, x);
+ /* check that the desired tolerance has been attained */
+ xassert(fabs(val - mpq_get_d(x)) <= eps * (1.0 + fabs(val)));
+done: return;
+}
+
+static void load_data(SSX *ssx, glp_prob *lp)
+{ /* load LP problem data into simplex solver workspace */
+ int m = ssx->m;
+ int n = ssx->n;
+ int nnz = ssx->A_ptr[n+1]-1;
+ int j, k, type, loc, len, *ind;
+ double lb, ub, coef, *val;
+ xassert(lp->m == m);
+ xassert(lp->n == n);
+ xassert(lp->nnz == nnz);
+ /* types and bounds of rows and columns */
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ { type = lp->row[k]->type;
+ lb = lp->row[k]->lb;
+ ub = lp->row[k]->ub;
+ }
+ else
+ { type = lp->col[k-m]->type;
+ lb = lp->col[k-m]->lb;
+ ub = lp->col[k-m]->ub;
+ }
+ switch (type)
+ { case GLP_FR: type = SSX_FR; break;
+ case GLP_LO: type = SSX_LO; break;
+ case GLP_UP: type = SSX_UP; break;
+ case GLP_DB: type = SSX_DB; break;
+ case GLP_FX: type = SSX_FX; break;
+ default: xassert(type != type);
+ }
+ ssx->type[k] = type;
+ set_d_eps(ssx->lb[k], lb);
+ set_d_eps(ssx->ub[k], ub);
+ }
+ /* optimization direction */
+ switch (lp->dir)
+ { case GLP_MIN: ssx->dir = SSX_MIN; break;
+ case GLP_MAX: ssx->dir = SSX_MAX; break;
+ default: xassert(lp != lp);
+ }
+ /* objective coefficients */
+ for (k = 0; k <= m+n; k++)
+ { if (k == 0)
+ coef = lp->c0;
+ else if (k <= m)
+ coef = 0.0;
+ else
+ coef = lp->col[k-m]->coef;
+ set_d_eps(ssx->coef[k], coef);
+ }
+ /* constraint coefficients */
+ ind = xcalloc(1+m, sizeof(int));
+ val = xcalloc(1+m, sizeof(double));
+ loc = 0;
+ for (j = 1; j <= n; j++)
+ { ssx->A_ptr[j] = loc+1;
+ len = glp_get_mat_col(lp, j, ind, val);
+ for (k = 1; k <= len; k++)
+ { loc++;
+ ssx->A_ind[loc] = ind[k];
+ set_d_eps(ssx->A_val[loc], val[k]);
+ }
+ }
+ xassert(loc == nnz);
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+static int load_basis(SSX *ssx, glp_prob *lp)
+{ /* load current LP basis into simplex solver workspace */
+ int m = ssx->m;
+ int n = ssx->n;
+ int *type = ssx->type;
+ int *stat = ssx->stat;
+ int *Q_row = ssx->Q_row;
+ int *Q_col = ssx->Q_col;
+ int i, j, k;
+ xassert(lp->m == m);
+ xassert(lp->n == n);
+ /* statuses of rows and columns */
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ stat[k] = lp->row[k]->stat;
+ else
+ stat[k] = lp->col[k-m]->stat;
+ switch (stat[k])
+ { case GLP_BS:
+ stat[k] = SSX_BS;
+ break;
+ case GLP_NL:
+ stat[k] = SSX_NL;
+ xassert(type[k] == SSX_LO || type[k] == SSX_DB);
+ break;
+ case GLP_NU:
+ stat[k] = SSX_NU;
+ xassert(type[k] == SSX_UP || type[k] == SSX_DB);
+ break;
+ case GLP_NF:
+ stat[k] = SSX_NF;
+ xassert(type[k] == SSX_FR);
+ break;
+ case GLP_NS:
+ stat[k] = SSX_NS;
+ xassert(type[k] == SSX_FX);
+ break;
+ default:
+ xassert(stat != stat);
+ }
+ }
+ /* build permutation matix Q */
+ i = j = 0;
+ for (k = 1; k <= m+n; k++)
+ { if (stat[k] == SSX_BS)
+ { i++;
+ if (i > m) return 1;
+ Q_row[k] = i, Q_col[i] = k;
+ }
+ else
+ { j++;
+ if (j > n) return 1;
+ Q_row[k] = m+j, Q_col[m+j] = k;
+ }
+ }
+ xassert(i == m && j == n);
+ return 0;
+}
+
+int glp_exact(glp_prob *lp, const glp_smcp *parm)
+{ glp_smcp _parm;
+ SSX *ssx;
+ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int i, j, k, type, pst, dst, ret, stat;
+ double lb, ub, prim, dual, sum;
+ if (parm == NULL)
+ parm = &_parm, glp_init_smcp((glp_smcp *)parm);
+ /* check control parameters */
+#if 1 /* 25/XI-2017 */
+ switch (parm->msg_lev)
+ { case GLP_MSG_OFF:
+ case GLP_MSG_ERR:
+ case GLP_MSG_ON:
+ case GLP_MSG_ALL:
+ case GLP_MSG_DBG:
+ break;
+ default:
+ xerror("glp_exact: msg_lev = %d; invalid parameter\n",
+ parm->msg_lev);
+ }
+#endif
+ if (parm->it_lim < 0)
+ xerror("glp_exact: it_lim = %d; invalid parameter\n",
+ parm->it_lim);
+ if (parm->tm_lim < 0)
+ xerror("glp_exact: tm_lim = %d; invalid parameter\n",
+ parm->tm_lim);
+ /* the problem must have at least one row and one column */
+ if (!(m > 0 && n > 0))
+#if 0 /* 25/XI-2017 */
+ { xprintf("glp_exact: problem has no rows/columns\n");
+#else
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_exact: problem has no rows/columns\n");
+#endif
+ return GLP_EFAIL;
+ }
+#if 1
+ /* basic solution is currently undefined */
+ lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
+ lp->obj_val = 0.0;
+ lp->some = 0;
+#endif
+ /* check that all double-bounded variables have correct bounds */
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ { type = lp->row[k]->type;
+ lb = lp->row[k]->lb;
+ ub = lp->row[k]->ub;
+ }
+ else
+ { type = lp->col[k-m]->type;
+ lb = lp->col[k-m]->lb;
+ ub = lp->col[k-m]->ub;
+ }
+ if (type == GLP_DB && lb >= ub)
+#if 0 /* 25/XI-2017 */
+ { xprintf("glp_exact: %s %d has invalid bounds\n",
+ k <= m ? "row" : "column", k <= m ? k : k-m);
+#else
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_exact: %s %d has invalid bounds\n",
+ k <= m ? "row" : "column", k <= m ? k : k-m);
+#endif
+ return GLP_EBOUND;
+ }
+ }
+ /* create the simplex solver workspace */
+#if 1 /* 25/XI-2017 */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ {
+#endif
+ xprintf("glp_exact: %d rows, %d columns, %d non-zeros\n",
+ m, n, nnz);
+#ifdef HAVE_GMP
+ xprintf("GNU MP bignum library is being used\n");
+#else
+ xprintf("GLPK bignum module is being used\n");
+ xprintf("(Consider installing GNU MP to attain a much better perf"
+ "ormance.)\n");
+#endif
+#if 1 /* 25/XI-2017 */
+ }
+#endif
+ ssx = ssx_create(m, n, nnz);
+ /* load LP problem data into the workspace */
+ load_data(ssx, lp);
+ /* load current LP basis into the workspace */
+ if (load_basis(ssx, lp))
+#if 0 /* 25/XI-2017 */
+ { xprintf("glp_exact: initial LP basis is invalid\n");
+#else
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_exact: initial LP basis is invalid\n");
+#endif
+ ret = GLP_EBADB;
+ goto done;
+ }
+#if 0
+ /* inherit some control parameters from the LP object */
+ ssx->it_lim = lpx_get_int_parm(lp, LPX_K_ITLIM);
+ ssx->it_cnt = lpx_get_int_parm(lp, LPX_K_ITCNT);
+ ssx->tm_lim = lpx_get_real_parm(lp, LPX_K_TMLIM);
+#else
+#if 1 /* 25/XI-2017 */
+ ssx->msg_lev = parm->msg_lev;
+#endif
+ ssx->it_lim = parm->it_lim;
+ ssx->it_cnt = lp->it_cnt;
+ ssx->tm_lim = (double)parm->tm_lim / 1000.0;
+#endif
+ ssx->out_frq = 5.0;
+ ssx->tm_beg = xtime();
+#if 0 /* 10/VI-2013 */
+ ssx->tm_lag = xlset(0);
+#else
+ ssx->tm_lag = 0.0;
+#endif
+ /* solve LP */
+ ret = ssx_driver(ssx);
+#if 0
+ /* copy back some statistics to the LP object */
+ lpx_set_int_parm(lp, LPX_K_ITLIM, ssx->it_lim);
+ lpx_set_int_parm(lp, LPX_K_ITCNT, ssx->it_cnt);
+ lpx_set_real_parm(lp, LPX_K_TMLIM, ssx->tm_lim);
+#else
+ lp->it_cnt = ssx->it_cnt;
+#endif
+ /* analyze the return code */
+ switch (ret)
+ { case 0:
+ /* optimal solution found */
+ ret = 0;
+ pst = dst = GLP_FEAS;
+ break;
+ case 1:
+ /* problem has no feasible solution */
+ ret = 0;
+ pst = GLP_NOFEAS, dst = GLP_INFEAS;
+ break;
+ case 2:
+ /* problem has unbounded solution */
+ ret = 0;
+ pst = GLP_FEAS, dst = GLP_NOFEAS;
+#if 1
+ xassert(1 <= ssx->q && ssx->q <= n);
+ lp->some = ssx->Q_col[m + ssx->q];
+ xassert(1 <= lp->some && lp->some <= m+n);
+#endif
+ break;
+ case 3:
+ /* iteration limit exceeded (phase I) */
+ ret = GLP_EITLIM;
+ pst = dst = GLP_INFEAS;
+ break;
+ case 4:
+ /* iteration limit exceeded (phase II) */
+ ret = GLP_EITLIM;
+ pst = GLP_FEAS, dst = GLP_INFEAS;
+ break;
+ case 5:
+ /* time limit exceeded (phase I) */
+ ret = GLP_ETMLIM;
+ pst = dst = GLP_INFEAS;
+ break;
+ case 6:
+ /* time limit exceeded (phase II) */
+ ret = GLP_ETMLIM;
+ pst = GLP_FEAS, dst = GLP_INFEAS;
+ break;
+ case 7:
+ /* initial basis matrix is singular */
+ ret = GLP_ESING;
+ goto done;
+ default:
+ xassert(ret != ret);
+ }
+ /* store final basic solution components into LP object */
+ lp->pbs_stat = pst;
+ lp->dbs_stat = dst;
+ sum = lp->c0;
+ for (k = 1; k <= m+n; k++)
+ { if (ssx->stat[k] == SSX_BS)
+ { i = ssx->Q_row[k]; /* x[k] = xB[i] */
+ xassert(1 <= i && i <= m);
+ stat = GLP_BS;
+ prim = mpq_get_d(ssx->bbar[i]);
+ dual = 0.0;
+ }
+ else
+ { j = ssx->Q_row[k] - m; /* x[k] = xN[j] */
+ xassert(1 <= j && j <= n);
+ switch (ssx->stat[k])
+ { case SSX_NF:
+ stat = GLP_NF;
+ prim = 0.0;
+ break;
+ case SSX_NL:
+ stat = GLP_NL;
+ prim = mpq_get_d(ssx->lb[k]);
+ break;
+ case SSX_NU:
+ stat = GLP_NU;
+ prim = mpq_get_d(ssx->ub[k]);
+ break;
+ case SSX_NS:
+ stat = GLP_NS;
+ prim = mpq_get_d(ssx->lb[k]);
+ break;
+ default:
+ xassert(ssx != ssx);
+ }
+ dual = mpq_get_d(ssx->cbar[j]);
+ }
+ if (k <= m)
+ { glp_set_row_stat(lp, k, stat);
+ lp->row[k]->prim = prim;
+ lp->row[k]->dual = dual;
+ }
+ else
+ { glp_set_col_stat(lp, k-m, stat);
+ lp->col[k-m]->prim = prim;
+ lp->col[k-m]->dual = dual;
+ sum += lp->col[k-m]->coef * prim;
+ }
+ }
+ lp->obj_val = sum;
+done: /* delete the simplex solver workspace */
+ ssx_delete(ssx);
+#if 1 /* 23/XI-2015 */
+ xassert(gmp_pool_count() == 0);
+ gmp_free_mem();
+#endif
+ /* return to the application program */
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi08.c b/test/monniaux/glpk-4.65/src/draft/glpapi08.c
new file mode 100644
index 00000000..652292cb
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi08.c
@@ -0,0 +1,388 @@
+/* glpapi08.c (interior-point method routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpipm.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_interior - solve LP problem with the interior-point method
+*
+* SYNOPSIS
+*
+* int glp_interior(glp_prob *P, const glp_iptcp *parm);
+*
+* The routine glp_interior is a driver to the LP solver based on the
+* interior-point method.
+*
+* The interior-point solver has a set of control parameters. Values of
+* the control parameters can be passed in a structure glp_iptcp, which
+* the parameter parm points to.
+*
+* Currently this routine implements an easy variant of the primal-dual
+* interior-point method based on Mehrotra's technique.
+*
+* This routine transforms the original LP problem to an equivalent LP
+* problem in the standard formulation (all constraints are equalities,
+* all variables are non-negative), calls the routine ipm_main to solve
+* the transformed problem, and then transforms an obtained solution to
+* the solution of the original problem.
+*
+* RETURNS
+*
+* 0 The LP problem instance has been successfully solved. This code
+* does not necessarily mean that the solver has found optimal
+* solution. It only means that the solution process was successful.
+*
+* GLP_EFAIL
+* The problem has no rows/columns.
+*
+* GLP_ENOCVG
+* Very slow convergence or divergence.
+*
+* GLP_EITLIM
+* Iteration limit exceeded.
+*
+* GLP_EINSTAB
+* Numerical instability on solving Newtonian system. */
+
+static void transform(NPP *npp)
+{ /* transform LP to the standard formulation */
+ NPPROW *row, *prev_row;
+ NPPCOL *col, *prev_col;
+ for (row = npp->r_tail; row != NULL; row = prev_row)
+ { prev_row = row->prev;
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ npp_free_row(npp, row);
+ else if (row->lb == -DBL_MAX)
+ npp_leq_row(npp, row);
+ else if (row->ub == +DBL_MAX)
+ npp_geq_row(npp, row);
+ else if (row->lb != row->ub)
+ { if (fabs(row->lb) < fabs(row->ub))
+ npp_geq_row(npp, row);
+ else
+ npp_leq_row(npp, row);
+ }
+ }
+ for (col = npp->c_tail; col != NULL; col = prev_col)
+ { prev_col = col->prev;
+ if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
+ npp_free_col(npp, col);
+ else if (col->lb == -DBL_MAX)
+ npp_ubnd_col(npp, col);
+ else if (col->ub == +DBL_MAX)
+ { if (col->lb != 0.0)
+ npp_lbnd_col(npp, col);
+ }
+ else if (col->lb != col->ub)
+ { if (fabs(col->lb) < fabs(col->ub))
+ { if (col->lb != 0.0)
+ npp_lbnd_col(npp, col);
+ }
+ else
+ npp_ubnd_col(npp, col);
+ npp_dbnd_col(npp, col);
+ }
+ else
+ npp_fixed_col(npp, col);
+ }
+ for (row = npp->r_head; row != NULL; row = row->next)
+ xassert(row->lb == row->ub);
+ for (col = npp->c_head; col != NULL; col = col->next)
+ xassert(col->lb == 0.0 && col->ub == +DBL_MAX);
+ return;
+}
+
+int glp_interior(glp_prob *P, const glp_iptcp *parm)
+{ glp_iptcp _parm;
+ GLPROW *row;
+ GLPCOL *col;
+ NPP *npp = NULL;
+ glp_prob *prob = NULL;
+ int i, j, ret;
+ /* check control parameters */
+ if (parm == NULL)
+ glp_init_iptcp(&_parm), parm = &_parm;
+ if (!(parm->msg_lev == GLP_MSG_OFF ||
+ parm->msg_lev == GLP_MSG_ERR ||
+ parm->msg_lev == GLP_MSG_ON ||
+ parm->msg_lev == GLP_MSG_ALL))
+ xerror("glp_interior: msg_lev = %d; invalid parameter\n",
+ parm->msg_lev);
+ if (!(parm->ord_alg == GLP_ORD_NONE ||
+ parm->ord_alg == GLP_ORD_QMD ||
+ parm->ord_alg == GLP_ORD_AMD ||
+ parm->ord_alg == GLP_ORD_SYMAMD))
+ xerror("glp_interior: ord_alg = %d; invalid parameter\n",
+ parm->ord_alg);
+ /* interior-point solution is currently undefined */
+ P->ipt_stat = GLP_UNDEF;
+ P->ipt_obj = 0.0;
+ /* check bounds of double-bounded variables */
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->type == GLP_DB && row->lb >= row->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_interior: row %d: lb = %g, ub = %g; incorre"
+ "ct bounds\n", i, row->lb, row->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->type == GLP_DB && col->lb >= col->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_interior: column %d: lb = %g, ub = %g; inco"
+ "rrect bounds\n", j, col->lb, col->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ /* transform LP to the standard formulation */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Original LP has %d row(s), %d column(s), and %d non-z"
+ "ero(s)\n", P->m, P->n, P->nnz);
+ npp = npp_create_wksp();
+ npp_load_prob(npp, P, GLP_OFF, GLP_IPT, GLP_ON);
+ transform(npp);
+ prob = glp_create_prob();
+ npp_build_prob(npp, prob);
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Working LP has %d row(s), %d column(s), and %d non-ze"
+ "ro(s)\n", prob->m, prob->n, prob->nnz);
+#if 1
+ /* currently empty problem cannot be solved */
+ if (!(prob->m > 0 && prob->n > 0))
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_interior: unable to solve empty problem\n");
+ ret = GLP_EFAIL;
+ goto done;
+ }
+#endif
+ /* scale the resultant LP */
+ { ENV *env = get_env_ptr();
+ int term_out = env->term_out;
+ env->term_out = GLP_OFF;
+ glp_scale_prob(prob, GLP_SF_EQ);
+ env->term_out = term_out;
+ }
+ /* warn about dense columns */
+ if (parm->msg_lev >= GLP_MSG_ON && prob->m >= 200)
+ { int len, cnt = 0;
+ for (j = 1; j <= prob->n; j++)
+ { len = glp_get_mat_col(prob, j, NULL, NULL);
+ if ((double)len >= 0.20 * (double)prob->m) cnt++;
+ }
+ if (cnt == 1)
+ xprintf("WARNING: PROBLEM HAS ONE DENSE COLUMN\n");
+ else if (cnt > 0)
+ xprintf("WARNING: PROBLEM HAS %d DENSE COLUMNS\n", cnt);
+ }
+ /* solve the transformed LP */
+ ret = ipm_solve(prob, parm);
+ /* postprocess solution from the transformed LP */
+ npp_postprocess(npp, prob);
+ /* and store solution to the original LP */
+ npp_unload_sol(npp, P);
+done: /* free working program objects */
+ if (npp != NULL) npp_delete_wksp(npp);
+ if (prob != NULL) glp_delete_prob(prob);
+ /* return to the application program */
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_init_iptcp - initialize interior-point solver control parameters
+*
+* SYNOPSIS
+*
+* void glp_init_iptcp(glp_iptcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_init_iptcp initializes control parameters, which are
+* used by the interior-point solver, with default values.
+*
+* Default values of the control parameters are stored in the glp_iptcp
+* structure, which the parameter parm points to. */
+
+void glp_init_iptcp(glp_iptcp *parm)
+{ parm->msg_lev = GLP_MSG_ALL;
+ parm->ord_alg = GLP_ORD_AMD;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_status - retrieve status of interior-point solution
+*
+* SYNOPSIS
+*
+* int glp_ipt_status(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_ipt_status reports the status of solution found by
+* the interior-point solver as follows:
+*
+* GLP_UNDEF - interior-point solution is undefined;
+* GLP_OPT - interior-point solution is optimal;
+* GLP_INFEAS - interior-point solution is infeasible;
+* GLP_NOFEAS - no feasible solution exists. */
+
+int glp_ipt_status(glp_prob *lp)
+{ int ipt_stat = lp->ipt_stat;
+ return ipt_stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_obj_val - retrieve objective value (interior point)
+*
+* SYNOPSIS
+*
+* double glp_ipt_obj_val(glp_prob *lp);
+*
+* RETURNS
+*
+* The routine glp_ipt_obj_val returns value of the objective function
+* for interior-point solution. */
+
+double glp_ipt_obj_val(glp_prob *lp)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double z;
+ z = lp->ipt_obj;
+ /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
+ return z;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_row_prim - retrieve row primal value (interior point)
+*
+* SYNOPSIS
+*
+* double glp_ipt_row_prim(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_ipt_row_prim returns primal value of the auxiliary
+* variable associated with i-th row. */
+
+double glp_ipt_row_prim(glp_prob *lp, int i)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double pval;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_ipt_row_prim: i = %d; row number out of range\n",
+ i);
+ pval = lp->row[i]->pval;
+ /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
+ return pval;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_row_dual - retrieve row dual value (interior point)
+*
+* SYNOPSIS
+*
+* double glp_ipt_row_dual(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_ipt_row_dual returns dual value (i.e. reduced cost)
+* of the auxiliary variable associated with i-th row. */
+
+double glp_ipt_row_dual(glp_prob *lp, int i)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double dval;
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_ipt_row_dual: i = %d; row number out of range\n",
+ i);
+ dval = lp->row[i]->dval;
+ /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
+ return dval;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_col_prim - retrieve column primal value (interior point)
+*
+* SYNOPSIS
+*
+* double glp_ipt_col_prim(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_ipt_col_prim returns primal value of the structural
+* variable associated with j-th column. */
+
+double glp_ipt_col_prim(glp_prob *lp, int j)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double pval;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_ipt_col_prim: j = %d; column number out of range\n"
+ , j);
+ pval = lp->col[j]->pval;
+ /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
+ return pval;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ipt_col_dual - retrieve column dual value (interior point)
+*
+* SYNOPSIS
+*
+* double glp_ipt_col_dual(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_ipt_col_dual returns dual value (i.e. reduced cost)
+* of the structural variable associated with j-th column. */
+
+double glp_ipt_col_dual(glp_prob *lp, int j)
+{ /*struct LPXCPS *cps = lp->cps;*/
+ double dval;
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_ipt_col_dual: j = %d; column number out of range\n"
+ , j);
+ dval = lp->col[j]->dval;
+ /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
+ return dval;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi09.c b/test/monniaux/glpk-4.65/src/draft/glpapi09.c
new file mode 100644
index 00000000..0d3ab57b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi09.c
@@ -0,0 +1,798 @@
+/* glpapi09.c (mixed integer programming routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "draft.h"
+#include "env.h"
+#include "ios.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_set_col_kind - set (change) column kind
+*
+* SYNOPSIS
+*
+* void glp_set_col_kind(glp_prob *mip, int j, int kind);
+*
+* DESCRIPTION
+*
+* The routine glp_set_col_kind sets (changes) the kind of j-th column
+* (structural variable) as specified by the parameter kind:
+*
+* GLP_CV - continuous variable;
+* GLP_IV - integer variable;
+* GLP_BV - binary variable. */
+
+void glp_set_col_kind(glp_prob *mip, int j, int kind)
+{ GLPCOL *col;
+ if (!(1 <= j && j <= mip->n))
+ xerror("glp_set_col_kind: j = %d; column number out of range\n"
+ , j);
+ col = mip->col[j];
+ switch (kind)
+ { case GLP_CV:
+ col->kind = GLP_CV;
+ break;
+ case GLP_IV:
+ col->kind = GLP_IV;
+ break;
+ case GLP_BV:
+ col->kind = GLP_IV;
+ if (!(col->type == GLP_DB && col->lb == 0.0 && col->ub ==
+ 1.0)) glp_set_col_bnds(mip, j, GLP_DB, 0.0, 1.0);
+ break;
+ default:
+ xerror("glp_set_col_kind: j = %d; kind = %d; invalid column"
+ " kind\n", j, kind);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_kind - retrieve column kind
+*
+* SYNOPSIS
+*
+* int glp_get_col_kind(glp_prob *mip, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_kind returns the kind of j-th column, i.e.
+* the kind of corresponding structural variable, as follows:
+*
+* GLP_CV - continuous variable;
+* GLP_IV - integer variable;
+* GLP_BV - binary variable */
+
+int glp_get_col_kind(glp_prob *mip, int j)
+{ GLPCOL *col;
+ int kind;
+ if (!(1 <= j && j <= mip->n))
+ xerror("glp_get_col_kind: j = %d; column number out of range\n"
+ , j);
+ col = mip->col[j];
+ kind = col->kind;
+ switch (kind)
+ { case GLP_CV:
+ break;
+ case GLP_IV:
+ if (col->type == GLP_DB && col->lb == 0.0 && col->ub == 1.0)
+ kind = GLP_BV;
+ break;
+ default:
+ xassert(kind != kind);
+ }
+ return kind;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_num_int - retrieve number of integer columns
+*
+* SYNOPSIS
+*
+* int glp_get_num_int(glp_prob *mip);
+*
+* RETURNS
+*
+* The routine glp_get_num_int returns the current number of columns,
+* which are marked as integer. */
+
+int glp_get_num_int(glp_prob *mip)
+{ GLPCOL *col;
+ int j, count = 0;
+ for (j = 1; j <= mip->n; j++)
+ { col = mip->col[j];
+ if (col->kind == GLP_IV) count++;
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_num_bin - retrieve number of binary columns
+*
+* SYNOPSIS
+*
+* int glp_get_num_bin(glp_prob *mip);
+*
+* RETURNS
+*
+* The routine glp_get_num_bin returns the current number of columns,
+* which are marked as binary. */
+
+int glp_get_num_bin(glp_prob *mip)
+{ GLPCOL *col;
+ int j, count = 0;
+ for (j = 1; j <= mip->n; j++)
+ { col = mip->col[j];
+ if (col->kind == GLP_IV && col->type == GLP_DB && col->lb ==
+ 0.0 && col->ub == 1.0) count++;
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_intopt - solve MIP problem with the branch-and-bound method
+*
+* SYNOPSIS
+*
+* int glp_intopt(glp_prob *P, const glp_iocp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_intopt is a driver to the MIP solver based on the
+* branch-and-bound method.
+*
+* On entry the problem object should contain optimal solution to LP
+* relaxation (which can be obtained with the routine glp_simplex).
+*
+* The MIP solver has a set of control parameters. Values of the control
+* parameters can be passed in a structure glp_iocp, which the parameter
+* parm points to.
+*
+* The parameter parm can be specified as NULL, in which case the MIP
+* solver uses default settings.
+*
+* RETURNS
+*
+* 0 The MIP problem instance has been successfully solved. This code
+* does not necessarily mean that the solver has found optimal
+* solution. It only means that the solution process was successful.
+*
+* GLP_EBOUND
+* Unable to start the search, because some double-bounded variables
+* have incorrect bounds or some integer variables have non-integer
+* (fractional) bounds.
+*
+* GLP_EROOT
+* Unable to start the search, because optimal basis for initial LP
+* relaxation is not provided.
+*
+* GLP_EFAIL
+* The search was prematurely terminated due to the solver failure.
+*
+* GLP_EMIPGAP
+* The search was prematurely terminated, because the relative mip
+* gap tolerance has been reached.
+*
+* GLP_ETMLIM
+* The search was prematurely terminated, because the time limit has
+* been exceeded.
+*
+* GLP_ENOPFS
+* The MIP problem instance has no primal feasible solution (only if
+* the MIP presolver is used).
+*
+* GLP_ENODFS
+* LP relaxation of the MIP problem instance has no dual feasible
+* solution (only if the MIP presolver is used).
+*
+* GLP_ESTOP
+* The search was prematurely terminated by application. */
+
+#if 0 /* 11/VII-2013 */
+static int solve_mip(glp_prob *P, const glp_iocp *parm)
+#else
+static int solve_mip(glp_prob *P, const glp_iocp *parm,
+ glp_prob *P0 /* problem passed to glp_intopt */,
+ NPP *npp /* preprocessor workspace or NULL */)
+#endif
+{ /* solve MIP directly without using the preprocessor */
+ glp_tree *T;
+ int ret;
+ /* optimal basis to LP relaxation must be provided */
+ if (glp_get_status(P) != GLP_OPT)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: optimal basis to initial LP relaxation"
+ " not provided\n");
+ ret = GLP_EROOT;
+ goto done;
+ }
+ /* it seems all is ok */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Integer optimization begins...\n");
+ /* create the branch-and-bound tree */
+ T = ios_create_tree(P, parm);
+#if 1 /* 11/VII-2013 */
+ T->P = P0;
+ T->npp = npp;
+#endif
+ /* solve the problem instance */
+ ret = ios_driver(T);
+ /* delete the branch-and-bound tree */
+ ios_delete_tree(T);
+ /* analyze exit code reported by the mip driver */
+ if (ret == 0)
+ { if (P->mip_stat == GLP_FEAS)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("INTEGER OPTIMAL SOLUTION FOUND\n");
+ P->mip_stat = GLP_OPT;
+ }
+ else
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("PROBLEM HAS NO INTEGER FEASIBLE SOLUTION\n");
+ P->mip_stat = GLP_NOFEAS;
+ }
+ }
+ else if (ret == GLP_EMIPGAP)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("RELATIVE MIP GAP TOLERANCE REACHED; SEARCH TERMINA"
+ "TED\n");
+ }
+ else if (ret == GLP_ETMLIM)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ }
+ else if (ret == GLP_EFAIL)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: cannot solve current LP relaxation\n");
+ }
+ else if (ret == GLP_ESTOP)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("SEARCH TERMINATED BY APPLICATION\n");
+ }
+ else
+ xassert(ret != ret);
+done: return ret;
+}
+
+static int preprocess_and_solve_mip(glp_prob *P, const glp_iocp *parm)
+{ /* solve MIP using the preprocessor */
+ ENV *env = get_env_ptr();
+ int term_out = env->term_out;
+ NPP *npp;
+ glp_prob *mip = NULL;
+ glp_bfcp bfcp;
+ glp_smcp smcp;
+ int ret;
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Preprocessing...\n");
+ /* create preprocessor workspace */
+ npp = npp_create_wksp();
+ /* load original problem into the preprocessor workspace */
+ npp_load_prob(npp, P, GLP_OFF, GLP_MIP, GLP_OFF);
+ /* process MIP prior to applying the branch-and-bound method */
+ if (!term_out || parm->msg_lev < GLP_MSG_ALL)
+ env->term_out = GLP_OFF;
+ else
+ env->term_out = GLP_ON;
+ ret = npp_integer(npp, parm);
+ env->term_out = term_out;
+ if (ret == 0)
+ ;
+ else if (ret == GLP_ENOPFS)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("PROBLEM HAS NO PRIMAL FEASIBLE SOLUTION\n");
+ }
+ else if (ret == GLP_ENODFS)
+ { if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("LP RELAXATION HAS NO DUAL FEASIBLE SOLUTION\n");
+ }
+ else
+ xassert(ret != ret);
+ if (ret != 0) goto done;
+ /* build transformed MIP */
+ mip = glp_create_prob();
+ npp_build_prob(npp, mip);
+ /* if the transformed MIP is empty, it has empty solution, which
+ is optimal */
+ if (mip->m == 0 && mip->n == 0)
+ { mip->mip_stat = GLP_OPT;
+ mip->mip_obj = mip->c0;
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ { xprintf("Objective value = %17.9e\n", mip->mip_obj);
+ xprintf("INTEGER OPTIMAL SOLUTION FOUND BY MIP PREPROCESSOR"
+ "\n");
+ }
+ goto post;
+ }
+ /* display some statistics */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ { int ni = glp_get_num_int(mip);
+ int nb = glp_get_num_bin(mip);
+ char s[50];
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ mip->m, mip->m == 1 ? "" : "s", mip->n, mip->n == 1 ? "" :
+ "s", mip->nnz, mip->nnz == 1 ? "" : "s");
+ if (nb == 0)
+ strcpy(s, "none of");
+ else if (ni == 1 && nb == 1)
+ strcpy(s, "");
+ else if (nb == 1)
+ strcpy(s, "one of");
+ else if (nb == ni)
+ strcpy(s, "all of");
+ else
+ sprintf(s, "%d of", nb);
+ xprintf("%d integer variable%s, %s which %s binary\n",
+ ni, ni == 1 ? "" : "s", s, nb == 1 ? "is" : "are");
+ }
+ /* inherit basis factorization control parameters */
+ glp_get_bfcp(P, &bfcp);
+ glp_set_bfcp(mip, &bfcp);
+ /* scale the transformed problem */
+ if (!term_out || parm->msg_lev < GLP_MSG_ALL)
+ env->term_out = GLP_OFF;
+ else
+ env->term_out = GLP_ON;
+ glp_scale_prob(mip,
+ GLP_SF_GM | GLP_SF_EQ | GLP_SF_2N | GLP_SF_SKIP);
+ env->term_out = term_out;
+ /* build advanced initial basis */
+ if (!term_out || parm->msg_lev < GLP_MSG_ALL)
+ env->term_out = GLP_OFF;
+ else
+ env->term_out = GLP_ON;
+ glp_adv_basis(mip, 0);
+ env->term_out = term_out;
+ /* solve initial LP relaxation */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Solving LP relaxation...\n");
+ glp_init_smcp(&smcp);
+ smcp.msg_lev = parm->msg_lev;
+ /* respect time limit */
+ smcp.tm_lim = parm->tm_lim;
+ mip->it_cnt = P->it_cnt;
+ ret = glp_simplex(mip, &smcp);
+ P->it_cnt = mip->it_cnt;
+ if (ret == GLP_ETMLIM)
+ goto done;
+ else if (ret != 0)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: cannot solve LP relaxation\n");
+ ret = GLP_EFAIL;
+ goto done;
+ }
+ /* check status of the basic solution */
+ ret = glp_get_status(mip);
+ if (ret == GLP_OPT)
+ ret = 0;
+ else if (ret == GLP_NOFEAS)
+ ret = GLP_ENOPFS;
+ else if (ret == GLP_UNBND)
+ ret = GLP_ENODFS;
+ else
+ xassert(ret != ret);
+ if (ret != 0) goto done;
+ /* solve the transformed MIP */
+ mip->it_cnt = P->it_cnt;
+#if 0 /* 11/VII-2013 */
+ ret = solve_mip(mip, parm);
+#else
+ if (parm->use_sol)
+ { mip->mip_stat = P->mip_stat;
+ mip->mip_obj = P->mip_obj;
+ }
+ ret = solve_mip(mip, parm, P, npp);
+#endif
+ P->it_cnt = mip->it_cnt;
+ /* only integer feasible solution can be postprocessed */
+ if (!(mip->mip_stat == GLP_OPT || mip->mip_stat == GLP_FEAS))
+ { P->mip_stat = mip->mip_stat;
+ goto done;
+ }
+ /* postprocess solution from the transformed MIP */
+post: npp_postprocess(npp, mip);
+ /* the transformed MIP is no longer needed */
+ glp_delete_prob(mip), mip = NULL;
+ /* store solution to the original problem */
+ npp_unload_sol(npp, P);
+done: /* delete the transformed MIP, if it exists */
+ if (mip != NULL) glp_delete_prob(mip);
+ /* delete preprocessor workspace */
+ npp_delete_wksp(npp);
+ return ret;
+}
+
+#ifndef HAVE_ALIEN_SOLVER /* 28/V-2010 */
+int _glp_intopt1(glp_prob *P, const glp_iocp *parm)
+{ xassert(P == P);
+ xassert(parm == parm);
+ xprintf("glp_intopt: no alien solver is available\n");
+ return GLP_EFAIL;
+}
+#endif
+
+int glp_intopt(glp_prob *P, const glp_iocp *parm)
+{ /* solve MIP problem with the branch-and-bound method */
+ glp_iocp _parm;
+ int i, j, ret;
+#if 0 /* 04/IV-2016 */
+ /* check problem object */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_intopt: P = %p; invalid problem object\n", P);
+#endif
+ if (P->tree != NULL)
+ xerror("glp_intopt: operation not allowed\n");
+ /* check control parameters */
+ if (parm == NULL)
+ parm = &_parm, glp_init_iocp((glp_iocp *)parm);
+ if (!(parm->msg_lev == GLP_MSG_OFF ||
+ parm->msg_lev == GLP_MSG_ERR ||
+ parm->msg_lev == GLP_MSG_ON ||
+ parm->msg_lev == GLP_MSG_ALL ||
+ parm->msg_lev == GLP_MSG_DBG))
+ xerror("glp_intopt: msg_lev = %d; invalid parameter\n",
+ parm->msg_lev);
+ if (!(parm->br_tech == GLP_BR_FFV ||
+ parm->br_tech == GLP_BR_LFV ||
+ parm->br_tech == GLP_BR_MFV ||
+ parm->br_tech == GLP_BR_DTH ||
+ parm->br_tech == GLP_BR_PCH))
+ xerror("glp_intopt: br_tech = %d; invalid parameter\n",
+ parm->br_tech);
+ if (!(parm->bt_tech == GLP_BT_DFS ||
+ parm->bt_tech == GLP_BT_BFS ||
+ parm->bt_tech == GLP_BT_BLB ||
+ parm->bt_tech == GLP_BT_BPH))
+ xerror("glp_intopt: bt_tech = %d; invalid parameter\n",
+ parm->bt_tech);
+ if (!(0.0 < parm->tol_int && parm->tol_int < 1.0))
+ xerror("glp_intopt: tol_int = %g; invalid parameter\n",
+ parm->tol_int);
+ if (!(0.0 < parm->tol_obj && parm->tol_obj < 1.0))
+ xerror("glp_intopt: tol_obj = %g; invalid parameter\n",
+ parm->tol_obj);
+ if (parm->tm_lim < 0)
+ xerror("glp_intopt: tm_lim = %d; invalid parameter\n",
+ parm->tm_lim);
+ if (parm->out_frq < 0)
+ xerror("glp_intopt: out_frq = %d; invalid parameter\n",
+ parm->out_frq);
+ if (parm->out_dly < 0)
+ xerror("glp_intopt: out_dly = %d; invalid parameter\n",
+ parm->out_dly);
+ if (!(0 <= parm->cb_size && parm->cb_size <= 256))
+ xerror("glp_intopt: cb_size = %d; invalid parameter\n",
+ parm->cb_size);
+ if (!(parm->pp_tech == GLP_PP_NONE ||
+ parm->pp_tech == GLP_PP_ROOT ||
+ parm->pp_tech == GLP_PP_ALL))
+ xerror("glp_intopt: pp_tech = %d; invalid parameter\n",
+ parm->pp_tech);
+ if (parm->mip_gap < 0.0)
+ xerror("glp_intopt: mip_gap = %g; invalid parameter\n",
+ parm->mip_gap);
+ if (!(parm->mir_cuts == GLP_ON || parm->mir_cuts == GLP_OFF))
+ xerror("glp_intopt: mir_cuts = %d; invalid parameter\n",
+ parm->mir_cuts);
+ if (!(parm->gmi_cuts == GLP_ON || parm->gmi_cuts == GLP_OFF))
+ xerror("glp_intopt: gmi_cuts = %d; invalid parameter\n",
+ parm->gmi_cuts);
+ if (!(parm->cov_cuts == GLP_ON || parm->cov_cuts == GLP_OFF))
+ xerror("glp_intopt: cov_cuts = %d; invalid parameter\n",
+ parm->cov_cuts);
+ if (!(parm->clq_cuts == GLP_ON || parm->clq_cuts == GLP_OFF))
+ xerror("glp_intopt: clq_cuts = %d; invalid parameter\n",
+ parm->clq_cuts);
+ if (!(parm->presolve == GLP_ON || parm->presolve == GLP_OFF))
+ xerror("glp_intopt: presolve = %d; invalid parameter\n",
+ parm->presolve);
+ if (!(parm->binarize == GLP_ON || parm->binarize == GLP_OFF))
+ xerror("glp_intopt: binarize = %d; invalid parameter\n",
+ parm->binarize);
+ if (!(parm->fp_heur == GLP_ON || parm->fp_heur == GLP_OFF))
+ xerror("glp_intopt: fp_heur = %d; invalid parameter\n",
+ parm->fp_heur);
+#if 1 /* 28/V-2010 */
+ if (!(parm->alien == GLP_ON || parm->alien == GLP_OFF))
+ xerror("glp_intopt: alien = %d; invalid parameter\n",
+ parm->alien);
+#endif
+#if 0 /* 11/VII-2013 */
+ /* integer solution is currently undefined */
+ P->mip_stat = GLP_UNDEF;
+ P->mip_obj = 0.0;
+#else
+ if (!parm->use_sol)
+ P->mip_stat = GLP_UNDEF;
+ if (P->mip_stat == GLP_NOFEAS)
+ P->mip_stat = GLP_UNDEF;
+ if (P->mip_stat == GLP_UNDEF)
+ P->mip_obj = 0.0;
+ else if (P->mip_stat == GLP_OPT)
+ P->mip_stat = GLP_FEAS;
+#endif
+ /* check bounds of double-bounded variables */
+ for (i = 1; i <= P->m; i++)
+ { GLPROW *row = P->row[i];
+ if (row->type == GLP_DB && row->lb >= row->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: row %d: lb = %g, ub = %g; incorrect"
+ " bounds\n", i, row->lb, row->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if (col->type == GLP_DB && col->lb >= col->ub)
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: column %d: lb = %g, ub = %g; incorr"
+ "ect bounds\n", j, col->lb, col->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ /* bounds of all integer variables must be integral */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if (col->kind != GLP_IV) continue;
+ if (col->type == GLP_LO || col->type == GLP_DB)
+ { if (col->lb != floor(col->lb))
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: integer column %d has non-intege"
+ "r lower bound %g\n", j, col->lb);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ if (col->type == GLP_UP || col->type == GLP_DB)
+ { if (col->ub != floor(col->ub))
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: integer column %d has non-intege"
+ "r upper bound %g\n", j, col->ub);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ if (col->type == GLP_FX)
+ { if (col->lb != floor(col->lb))
+ { if (parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("glp_intopt: integer column %d has non-intege"
+ "r fixed value %g\n", j, col->lb);
+ ret = GLP_EBOUND;
+ goto done;
+ }
+ }
+ }
+ /* solve MIP problem */
+ if (parm->msg_lev >= GLP_MSG_ALL)
+ { int ni = glp_get_num_int(P);
+ int nb = glp_get_num_bin(P);
+ char s[50];
+ xprintf("GLPK Integer Optimizer, v%s\n", glp_version());
+ xprintf("%d row%s, %d column%s, %d non-zero%s\n",
+ P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
+ P->nnz, P->nnz == 1 ? "" : "s");
+ if (nb == 0)
+ strcpy(s, "none of");
+ else if (ni == 1 && nb == 1)
+ strcpy(s, "");
+ else if (nb == 1)
+ strcpy(s, "one of");
+ else if (nb == ni)
+ strcpy(s, "all of");
+ else
+ sprintf(s, "%d of", nb);
+ xprintf("%d integer variable%s, %s which %s binary\n",
+ ni, ni == 1 ? "" : "s", s, nb == 1 ? "is" : "are");
+ }
+#if 1 /* 28/V-2010 */
+ if (parm->alien)
+ { /* use alien integer optimizer */
+ ret = _glp_intopt1(P, parm);
+ goto done;
+ }
+#endif
+ if (!parm->presolve)
+#if 0 /* 11/VII-2013 */
+ ret = solve_mip(P, parm);
+#else
+ ret = solve_mip(P, parm, P, NULL);
+#endif
+ else
+ ret = preprocess_and_solve_mip(P, parm);
+#if 1 /* 12/III-2013 */
+ if (ret == GLP_ENOPFS)
+ P->mip_stat = GLP_NOFEAS;
+#endif
+done: /* return to the application program */
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_init_iocp - initialize integer optimizer control parameters
+*
+* SYNOPSIS
+*
+* void glp_init_iocp(glp_iocp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_init_iocp initializes control parameters, which are
+* used by the integer optimizer, with default values.
+*
+* Default values of the control parameters are stored in a glp_iocp
+* structure, which the parameter parm points to. */
+
+void glp_init_iocp(glp_iocp *parm)
+{ parm->msg_lev = GLP_MSG_ALL;
+ parm->br_tech = GLP_BR_DTH;
+ parm->bt_tech = GLP_BT_BLB;
+ parm->tol_int = 1e-5;
+ parm->tol_obj = 1e-7;
+ parm->tm_lim = INT_MAX;
+ parm->out_frq = 5000;
+ parm->out_dly = 10000;
+ parm->cb_func = NULL;
+ parm->cb_info = NULL;
+ parm->cb_size = 0;
+ parm->pp_tech = GLP_PP_ALL;
+ parm->mip_gap = 0.0;
+ parm->mir_cuts = GLP_OFF;
+ parm->gmi_cuts = GLP_OFF;
+ parm->cov_cuts = GLP_OFF;
+ parm->clq_cuts = GLP_OFF;
+ parm->presolve = GLP_OFF;
+ parm->binarize = GLP_OFF;
+ parm->fp_heur = GLP_OFF;
+ parm->ps_heur = GLP_OFF;
+ parm->ps_tm_lim = 60000; /* 1 minute */
+ parm->sr_heur = GLP_ON;
+#if 1 /* 24/X-2015; not documented--should not be used */
+ parm->use_sol = GLP_OFF;
+ parm->save_sol = NULL;
+ parm->alien = GLP_OFF;
+#endif
+#if 0 /* 20/I-2018 */
+#if 1 /* 16/III-2016; not documented--should not be used */
+ parm->flip = GLP_OFF;
+#endif
+#else
+ parm->flip = GLP_ON;
+#endif
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mip_status - retrieve status of MIP solution
+*
+* SYNOPSIS
+*
+* int glp_mip_status(glp_prob *mip);
+*
+* RETURNS
+*
+* The routine lpx_mip_status reports the status of MIP solution found
+* by the branch-and-bound solver as follows:
+*
+* GLP_UNDEF - MIP solution is undefined;
+* GLP_OPT - MIP solution is integer optimal;
+* GLP_FEAS - MIP solution is integer feasible but its optimality
+* (or non-optimality) has not been proven, perhaps due to
+* premature termination of the search;
+* GLP_NOFEAS - problem has no integer feasible solution (proven by the
+* solver). */
+
+int glp_mip_status(glp_prob *mip)
+{ int mip_stat = mip->mip_stat;
+ return mip_stat;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mip_obj_val - retrieve objective value (MIP solution)
+*
+* SYNOPSIS
+*
+* double glp_mip_obj_val(glp_prob *mip);
+*
+* RETURNS
+*
+* The routine glp_mip_obj_val returns value of the objective function
+* for MIP solution. */
+
+double glp_mip_obj_val(glp_prob *mip)
+{ /*struct LPXCPS *cps = mip->cps;*/
+ double z;
+ z = mip->mip_obj;
+ /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
+ return z;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mip_row_val - retrieve row value (MIP solution)
+*
+* SYNOPSIS
+*
+* double glp_mip_row_val(glp_prob *mip, int i);
+*
+* RETURNS
+*
+* The routine glp_mip_row_val returns value of the auxiliary variable
+* associated with i-th row. */
+
+double glp_mip_row_val(glp_prob *mip, int i)
+{ /*struct LPXCPS *cps = mip->cps;*/
+ double mipx;
+ if (!(1 <= i && i <= mip->m))
+ xerror("glp_mip_row_val: i = %d; row number out of range\n", i)
+ ;
+ mipx = mip->row[i]->mipx;
+ /*if (cps->round && fabs(mipx) < 1e-9) mipx = 0.0;*/
+ return mipx;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mip_col_val - retrieve column value (MIP solution)
+*
+* SYNOPSIS
+*
+* double glp_mip_col_val(glp_prob *mip, int j);
+*
+* RETURNS
+*
+* The routine glp_mip_col_val returns value of the structural variable
+* associated with j-th column. */
+
+double glp_mip_col_val(glp_prob *mip, int j)
+{ /*struct LPXCPS *cps = mip->cps;*/
+ double mipx;
+ if (!(1 <= j && j <= mip->n))
+ xerror("glp_mip_col_val: j = %d; column number out of range\n",
+ j);
+ mipx = mip->col[j]->mipx;
+ /*if (cps->round && fabs(mipx) < 1e-9) mipx = 0.0;*/
+ return mipx;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi10.c b/test/monniaux/glpk-4.65/src/draft/glpapi10.c
new file mode 100644
index 00000000..5550aa39
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi10.c
@@ -0,0 +1,305 @@
+/* glpapi10.c (solution checking routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+void glp_check_kkt(glp_prob *P, int sol, int cond, double *_ae_max,
+ int *_ae_ind, double *_re_max, int *_re_ind)
+{ /* check feasibility and optimality conditions */
+ int m = P->m;
+ int n = P->n;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, j, ae_ind, re_ind;
+ double e, sp, sn, t, ae_max, re_max;
+ if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP))
+ xerror("glp_check_kkt: sol = %d; invalid solution indicator\n",
+ sol);
+ if (!(cond == GLP_KKT_PE || cond == GLP_KKT_PB ||
+ cond == GLP_KKT_DE || cond == GLP_KKT_DB ||
+ cond == GLP_KKT_CS))
+ xerror("glp_check_kkt: cond = %d; invalid condition indicator "
+ "\n", cond);
+ ae_max = re_max = 0.0;
+ ae_ind = re_ind = 0;
+ if (cond == GLP_KKT_PE)
+ { /* xR - A * xS = 0 */
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ sp = sn = 0.0;
+ /* t := xR[i] */
+ if (sol == GLP_SOL)
+ t = row->prim;
+ else if (sol == GLP_IPT)
+ t = row->pval;
+ else if (sol == GLP_MIP)
+ t = row->mipx;
+ else
+ xassert(sol != sol);
+ if (t >= 0.0) sp += t; else sn -= t;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ /* t := - a[i,j] * xS[j] */
+ if (sol == GLP_SOL)
+ t = - aij->val * col->prim;
+ else if (sol == GLP_IPT)
+ t = - aij->val * col->pval;
+ else if (sol == GLP_MIP)
+ t = - aij->val * col->mipx;
+ else
+ xassert(sol != sol);
+ if (t >= 0.0) sp += t; else sn -= t;
+ }
+ /* absolute error */
+ e = fabs(sp - sn);
+ if (ae_max < e)
+ ae_max = e, ae_ind = i;
+ /* relative error */
+ e /= (1.0 + sp + sn);
+ if (re_max < e)
+ re_max = e, re_ind = i;
+ }
+ }
+ else if (cond == GLP_KKT_PB)
+ { /* lR <= xR <= uR */
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ /* t := xR[i] */
+ if (sol == GLP_SOL)
+ t = row->prim;
+ else if (sol == GLP_IPT)
+ t = row->pval;
+ else if (sol == GLP_MIP)
+ t = row->mipx;
+ else
+ xassert(sol != sol);
+ /* check lower bound */
+ if (row->type == GLP_LO || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ { if (t < row->lb)
+ { /* absolute error */
+ e = row->lb - t;
+ if (ae_max < e)
+ ae_max = e, ae_ind = i;
+ /* relative error */
+ e /= (1.0 + fabs(row->lb));
+ if (re_max < e)
+ re_max = e, re_ind = i;
+ }
+ }
+ /* check upper bound */
+ if (row->type == GLP_UP || row->type == GLP_DB ||
+ row->type == GLP_FX)
+ { if (t > row->ub)
+ { /* absolute error */
+ e = t - row->ub;
+ if (ae_max < e)
+ ae_max = e, ae_ind = i;
+ /* relative error */
+ e /= (1.0 + fabs(row->ub));
+ if (re_max < e)
+ re_max = e, re_ind = i;
+ }
+ }
+ }
+ /* lS <= xS <= uS */
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ /* t := xS[j] */
+ if (sol == GLP_SOL)
+ t = col->prim;
+ else if (sol == GLP_IPT)
+ t = col->pval;
+ else if (sol == GLP_MIP)
+ t = col->mipx;
+ else
+ xassert(sol != sol);
+ /* check lower bound */
+ if (col->type == GLP_LO || col->type == GLP_DB ||
+ col->type == GLP_FX)
+ { if (t < col->lb)
+ { /* absolute error */
+ e = col->lb - t;
+ if (ae_max < e)
+ ae_max = e, ae_ind = m+j;
+ /* relative error */
+ e /= (1.0 + fabs(col->lb));
+ if (re_max < e)
+ re_max = e, re_ind = m+j;
+ }
+ }
+ /* check upper bound */
+ if (col->type == GLP_UP || col->type == GLP_DB ||
+ col->type == GLP_FX)
+ { if (t > col->ub)
+ { /* absolute error */
+ e = t - col->ub;
+ if (ae_max < e)
+ ae_max = e, ae_ind = m+j;
+ /* relative error */
+ e /= (1.0 + fabs(col->ub));
+ if (re_max < e)
+ re_max = e, re_ind = m+j;
+ }
+ }
+ }
+ }
+ else if (cond == GLP_KKT_DE)
+ { /* A' * (lambdaR - cR) + (lambdaS - cS) = 0 */
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ sp = sn = 0.0;
+ /* t := lambdaS[j] - cS[j] */
+ if (sol == GLP_SOL)
+ t = col->dual - col->coef;
+ else if (sol == GLP_IPT)
+ t = col->dval - col->coef;
+ else
+ xassert(sol != sol);
+ if (t >= 0.0) sp += t; else sn -= t;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ { row = aij->row;
+ /* t := a[i,j] * (lambdaR[i] - cR[i]) */
+ if (sol == GLP_SOL)
+ t = aij->val * row->dual;
+ else if (sol == GLP_IPT)
+ t = aij->val * row->dval;
+ else
+ xassert(sol != sol);
+ if (t >= 0.0) sp += t; else sn -= t;
+ }
+ /* absolute error */
+ e = fabs(sp - sn);
+ if (ae_max < e)
+ ae_max = e, ae_ind = m+j;
+ /* relative error */
+ e /= (1.0 + sp + sn);
+ if (re_max < e)
+ re_max = e, re_ind = m+j;
+ }
+ }
+ else if (cond == GLP_KKT_DB)
+ { /* check lambdaR */
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ /* t := lambdaR[i] */
+ if (sol == GLP_SOL)
+ t = row->dual;
+ else if (sol == GLP_IPT)
+ t = row->dval;
+ else
+ xassert(sol != sol);
+ /* correct sign */
+ if (P->dir == GLP_MIN)
+ t = + t;
+ else if (P->dir == GLP_MAX)
+ t = - t;
+ else
+ xassert(P != P);
+ /* check for positivity */
+#if 1 /* 08/III-2013 */
+ /* the former check was correct */
+ /* the bug reported by David Price is related to violation
+ of complementarity slackness, not to this condition */
+ if (row->type == GLP_FR || row->type == GLP_LO)
+#else
+ if (row->stat == GLP_NF || row->stat == GLP_NL)
+#endif
+ { if (t < 0.0)
+ { e = - t;
+ if (ae_max < e)
+ ae_max = re_max = e, ae_ind = re_ind = i;
+ }
+ }
+ /* check for negativity */
+#if 1 /* 08/III-2013 */
+ /* see comment above */
+ if (row->type == GLP_FR || row->type == GLP_UP)
+#else
+ if (row->stat == GLP_NF || row->stat == GLP_NU)
+#endif
+ { if (t > 0.0)
+ { e = + t;
+ if (ae_max < e)
+ ae_max = re_max = e, ae_ind = re_ind = i;
+ }
+ }
+ }
+ /* check lambdaS */
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ /* t := lambdaS[j] */
+ if (sol == GLP_SOL)
+ t = col->dual;
+ else if (sol == GLP_IPT)
+ t = col->dval;
+ else
+ xassert(sol != sol);
+ /* correct sign */
+ if (P->dir == GLP_MIN)
+ t = + t;
+ else if (P->dir == GLP_MAX)
+ t = - t;
+ else
+ xassert(P != P);
+ /* check for positivity */
+#if 1 /* 08/III-2013 */
+ /* see comment above */
+ if (col->type == GLP_FR || col->type == GLP_LO)
+#else
+ if (col->stat == GLP_NF || col->stat == GLP_NL)
+#endif
+ { if (t < 0.0)
+ { e = - t;
+ if (ae_max < e)
+ ae_max = re_max = e, ae_ind = re_ind = m+j;
+ }
+ }
+ /* check for negativity */
+#if 1 /* 08/III-2013 */
+ /* see comment above */
+ if (col->type == GLP_FR || col->type == GLP_UP)
+#else
+ if (col->stat == GLP_NF || col->stat == GLP_NU)
+#endif
+ { if (t > 0.0)
+ { e = + t;
+ if (ae_max < e)
+ ae_max = re_max = e, ae_ind = re_ind = m+j;
+ }
+ }
+ }
+ }
+ else
+ xassert(cond != cond);
+ if (_ae_max != NULL) *_ae_max = ae_max;
+ if (_ae_ind != NULL) *_ae_ind = ae_ind;
+ if (_re_max != NULL) *_re_max = re_max;
+ if (_re_ind != NULL) *_re_ind = re_ind;
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi12.c b/test/monniaux/glpk-4.65/src/draft/glpapi12.c
new file mode 100644
index 00000000..020c8981
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi12.c
@@ -0,0 +1,2185 @@
+/* glpapi12.c (basis factorization and simplex tableau routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "draft.h"
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_bf_exists - check if the basis factorization exists
+*
+* SYNOPSIS
+*
+* int glp_bf_exists(glp_prob *lp);
+*
+* RETURNS
+*
+* If the basis factorization for the current basis associated with
+* the specified problem object exists and therefore is available for
+* computations, the routine glp_bf_exists returns non-zero. Otherwise
+* the routine returns zero. */
+
+int glp_bf_exists(glp_prob *lp)
+{ int ret;
+ ret = (lp->m == 0 || lp->valid);
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_factorize - compute the basis factorization
+*
+* SYNOPSIS
+*
+* int glp_factorize(glp_prob *lp);
+*
+* DESCRIPTION
+*
+* The routine glp_factorize computes the basis factorization for the
+* current basis associated with the specified problem object.
+*
+* RETURNS
+*
+* 0 The basis factorization has been successfully computed.
+*
+* GLP_EBADB
+* The basis matrix is invalid, i.e. the number of basic (auxiliary
+* and structural) variables differs from the number of rows in the
+* problem object.
+*
+* GLP_ESING
+* The basis matrix is singular within the working precision.
+*
+* GLP_ECOND
+* The basis matrix is ill-conditioned. */
+
+static int b_col(void *info, int j, int ind[], double val[])
+{ glp_prob *lp = info;
+ int m = lp->m;
+ GLPAIJ *aij;
+ int k, len;
+ xassert(1 <= j && j <= m);
+ /* determine the ordinal number of basic auxiliary or structural
+ variable x[k] corresponding to basic variable xB[j] */
+ k = lp->head[j];
+ /* build j-th column of the basic matrix, which is k-th column of
+ the scaled augmented matrix (I | -R*A*S) */
+ if (k <= m)
+ { /* x[k] is auxiliary variable */
+ len = 1;
+ ind[1] = k;
+ val[1] = 1.0;
+ }
+ else
+ { /* x[k] is structural variable */
+ len = 0;
+ for (aij = lp->col[k-m]->ptr; aij != NULL; aij = aij->c_next)
+ { len++;
+ ind[len] = aij->row->i;
+ val[len] = - aij->row->rii * aij->val * aij->col->sjj;
+ }
+ }
+ return len;
+}
+
+int glp_factorize(glp_prob *lp)
+{ int m = lp->m;
+ int n = lp->n;
+ GLPROW **row = lp->row;
+ GLPCOL **col = lp->col;
+ int *head = lp->head;
+ int j, k, stat, ret;
+ /* invalidate the basis factorization */
+ lp->valid = 0;
+ /* build the basis header */
+ j = 0;
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ { stat = row[k]->stat;
+ row[k]->bind = 0;
+ }
+ else
+ { stat = col[k-m]->stat;
+ col[k-m]->bind = 0;
+ }
+ if (stat == GLP_BS)
+ { j++;
+ if (j > m)
+ { /* too many basic variables */
+ ret = GLP_EBADB;
+ goto fini;
+ }
+ head[j] = k;
+ if (k <= m)
+ row[k]->bind = j;
+ else
+ col[k-m]->bind = j;
+ }
+ }
+ if (j < m)
+ { /* too few basic variables */
+ ret = GLP_EBADB;
+ goto fini;
+ }
+ /* try to factorize the basis matrix */
+ if (m > 0)
+ { if (lp->bfd == NULL)
+ { lp->bfd = bfd_create_it();
+#if 0 /* 08/III-2014 */
+ copy_bfcp(lp);
+#endif
+ }
+ switch (bfd_factorize(lp->bfd, m, /*lp->head,*/ b_col, lp))
+ { case 0:
+ /* ok */
+ break;
+ case BFD_ESING:
+ /* singular matrix */
+ ret = GLP_ESING;
+ goto fini;
+ case BFD_ECOND:
+ /* ill-conditioned matrix */
+ ret = GLP_ECOND;
+ goto fini;
+ default:
+ xassert(lp != lp);
+ }
+ lp->valid = 1;
+ }
+ /* factorization successful */
+ ret = 0;
+fini: /* bring the return code to the calling program */
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_bf_updated - check if the basis factorization has been updated
+*
+* SYNOPSIS
+*
+* int glp_bf_updated(glp_prob *lp);
+*
+* RETURNS
+*
+* If the basis factorization has been just computed from scratch, the
+* routine glp_bf_updated returns zero. Otherwise, if the factorization
+* has been updated one or more times, the routine returns non-zero. */
+
+int glp_bf_updated(glp_prob *lp)
+{ int cnt;
+ if (!(lp->m == 0 || lp->valid))
+ xerror("glp_bf_update: basis factorization does not exist\n");
+#if 0 /* 15/XI-2009 */
+ cnt = (lp->m == 0 ? 0 : lp->bfd->upd_cnt);
+#else
+ cnt = (lp->m == 0 ? 0 : bfd_get_count(lp->bfd));
+#endif
+ return cnt;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_bfcp - retrieve basis factorization control parameters
+*
+* SYNOPSIS
+*
+* void glp_get_bfcp(glp_prob *lp, glp_bfcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_get_bfcp retrieves control parameters, which are
+* used on computing and updating the basis factorization associated
+* with the specified problem object.
+*
+* Current values of control parameters are stored by the routine in
+* a glp_bfcp structure, which the parameter parm points to. */
+
+#if 1 /* 08/III-2014 */
+void glp_get_bfcp(glp_prob *P, glp_bfcp *parm)
+{ if (P->bfd == NULL)
+ P->bfd = bfd_create_it();
+ bfd_get_bfcp(P->bfd, parm);
+ return;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_set_bfcp - change basis factorization control parameters
+*
+* SYNOPSIS
+*
+* void glp_set_bfcp(glp_prob *lp, const glp_bfcp *parm);
+*
+* DESCRIPTION
+*
+* The routine glp_set_bfcp changes control parameters, which are used
+* by internal GLPK routines in computing and updating the basis
+* factorization associated with the specified problem object.
+*
+* New values of the control parameters should be passed in a structure
+* glp_bfcp, which the parameter parm points to.
+*
+* The parameter parm can be specified as NULL, in which case all
+* control parameters are reset to their default values. */
+
+#if 1 /* 08/III-2014 */
+void glp_set_bfcp(glp_prob *P, const glp_bfcp *parm)
+{ if (P->bfd == NULL)
+ P->bfd = bfd_create_it();
+ if (parm != NULL)
+ { if (!(parm->type == GLP_BF_LUF + GLP_BF_FT ||
+ parm->type == GLP_BF_LUF + GLP_BF_BG ||
+ parm->type == GLP_BF_LUF + GLP_BF_GR ||
+ parm->type == GLP_BF_BTF + GLP_BF_BG ||
+ parm->type == GLP_BF_BTF + GLP_BF_GR))
+ xerror("glp_set_bfcp: type = 0x%02X; invalid parameter\n",
+ parm->type);
+ if (!(0.0 < parm->piv_tol && parm->piv_tol < 1.0))
+ xerror("glp_set_bfcp: piv_tol = %g; invalid parameter\n",
+ parm->piv_tol);
+ if (parm->piv_lim < 1)
+ xerror("glp_set_bfcp: piv_lim = %d; invalid parameter\n",
+ parm->piv_lim);
+ if (!(parm->suhl == GLP_ON || parm->suhl == GLP_OFF))
+ xerror("glp_set_bfcp: suhl = %d; invalid parameter\n",
+ parm->suhl);
+ if (!(0.0 <= parm->eps_tol && parm->eps_tol <= 1e-6))
+ xerror("glp_set_bfcp: eps_tol = %g; invalid parameter\n",
+ parm->eps_tol);
+ if (!(1 <= parm->nfs_max && parm->nfs_max <= 32767))
+ xerror("glp_set_bfcp: nfs_max = %d; invalid parameter\n",
+ parm->nfs_max);
+ if (!(1 <= parm->nrs_max && parm->nrs_max <= 32767))
+ xerror("glp_set_bfcp: nrs_max = %d; invalid parameter\n",
+ parm->nrs_max);
+ }
+ bfd_set_bfcp(P->bfd, parm);
+ return;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_get_bhead - retrieve the basis header information
+*
+* SYNOPSIS
+*
+* int glp_get_bhead(glp_prob *lp, int k);
+*
+* DESCRIPTION
+*
+* The routine glp_get_bhead returns the basis header information for
+* the current basis associated with the specified problem object.
+*
+* RETURNS
+*
+* If xB[k], 1 <= k <= m, is i-th auxiliary variable (1 <= i <= m), the
+* routine returns i. Otherwise, if xB[k] is j-th structural variable
+* (1 <= j <= n), the routine returns m+j. Here m is the number of rows
+* and n is the number of columns in the problem object. */
+
+int glp_get_bhead(glp_prob *lp, int k)
+{ if (!(lp->m == 0 || lp->valid))
+ xerror("glp_get_bhead: basis factorization does not exist\n");
+ if (!(1 <= k && k <= lp->m))
+ xerror("glp_get_bhead: k = %d; index out of range\n", k);
+ return lp->head[k];
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_row_bind - retrieve row index in the basis header
+*
+* SYNOPSIS
+*
+* int glp_get_row_bind(glp_prob *lp, int i);
+*
+* RETURNS
+*
+* The routine glp_get_row_bind returns the index k of basic variable
+* xB[k], 1 <= k <= m, which is i-th auxiliary variable, 1 <= i <= m,
+* in the current basis associated with the specified problem object,
+* where m is the number of rows. However, if i-th auxiliary variable
+* is non-basic, the routine returns zero. */
+
+int glp_get_row_bind(glp_prob *lp, int i)
+{ if (!(lp->m == 0 || lp->valid))
+ xerror("glp_get_row_bind: basis factorization does not exist\n"
+ );
+ if (!(1 <= i && i <= lp->m))
+ xerror("glp_get_row_bind: i = %d; row number out of range\n",
+ i);
+ return lp->row[i]->bind;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_get_col_bind - retrieve column index in the basis header
+*
+* SYNOPSIS
+*
+* int glp_get_col_bind(glp_prob *lp, int j);
+*
+* RETURNS
+*
+* The routine glp_get_col_bind returns the index k of basic variable
+* xB[k], 1 <= k <= m, which is j-th structural variable, 1 <= j <= n,
+* in the current basis associated with the specified problem object,
+* where m is the number of rows, n is the number of columns. However,
+* if j-th structural variable is non-basic, the routine returns zero.*/
+
+int glp_get_col_bind(glp_prob *lp, int j)
+{ if (!(lp->m == 0 || lp->valid))
+ xerror("glp_get_col_bind: basis factorization does not exist\n"
+ );
+ if (!(1 <= j && j <= lp->n))
+ xerror("glp_get_col_bind: j = %d; column number out of range\n"
+ , j);
+ return lp->col[j]->bind;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ftran - perform forward transformation (solve system B*x = b)
+*
+* SYNOPSIS
+*
+* void glp_ftran(glp_prob *lp, double x[]);
+*
+* DESCRIPTION
+*
+* The routine glp_ftran performs forward transformation, i.e. solves
+* the system B*x = b, where B is the basis matrix corresponding to the
+* current basis for the specified problem object, x is the vector of
+* unknowns to be computed, b is the vector of right-hand sides.
+*
+* On entry elements of the vector b should be stored in dense format
+* in locations x[1], ..., x[m], where m is the number of rows. On exit
+* the routine stores elements of the vector x in the same locations.
+*
+* SCALING/UNSCALING
+*
+* Let A~ = (I | -A) is the augmented constraint matrix of the original
+* (unscaled) problem. In the scaled LP problem instead the matrix A the
+* scaled matrix A" = R*A*S is actually used, so
+*
+* A~" = (I | A") = (I | R*A*S) = (R*I*inv(R) | R*A*S) =
+* (1)
+* = R*(I | A)*S~ = R*A~*S~,
+*
+* is the scaled augmented constraint matrix, where R and S are diagonal
+* scaling matrices used to scale rows and columns of the matrix A, and
+*
+* S~ = diag(inv(R) | S) (2)
+*
+* is an augmented diagonal scaling matrix.
+*
+* By definition:
+*
+* A~ = (B | N), (3)
+*
+* where B is the basic matrix, which consists of basic columns of the
+* augmented constraint matrix A~, and N is a matrix, which consists of
+* non-basic columns of A~. From (1) it follows that:
+*
+* A~" = (B" | N") = (R*B*SB | R*N*SN), (4)
+*
+* where SB and SN are parts of the augmented scaling matrix S~, which
+* correspond to basic and non-basic variables, respectively. Therefore
+*
+* B" = R*B*SB, (5)
+*
+* which is the scaled basis matrix. */
+
+void glp_ftran(glp_prob *lp, double x[])
+{ int m = lp->m;
+ GLPROW **row = lp->row;
+ GLPCOL **col = lp->col;
+ int i, k;
+ /* B*x = b ===> (R*B*SB)*(inv(SB)*x) = R*b ===>
+ B"*x" = b", where b" = R*b, x = SB*x" */
+ if (!(m == 0 || lp->valid))
+ xerror("glp_ftran: basis factorization does not exist\n");
+ /* b" := R*b */
+ for (i = 1; i <= m; i++)
+ x[i] *= row[i]->rii;
+ /* x" := inv(B")*b" */
+ if (m > 0) bfd_ftran(lp->bfd, x);
+ /* x := SB*x" */
+ for (i = 1; i <= m; i++)
+ { k = lp->head[i];
+ if (k <= m)
+ x[i] /= row[k]->rii;
+ else
+ x[i] *= col[k-m]->sjj;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_btran - perform backward transformation (solve system B'*x = b)
+*
+* SYNOPSIS
+*
+* void glp_btran(glp_prob *lp, double x[]);
+*
+* DESCRIPTION
+*
+* The routine glp_btran performs backward transformation, i.e. solves
+* the system B'*x = b, where B' is a matrix transposed to the basis
+* matrix corresponding to the current basis for the specified problem
+* problem object, x is the vector of unknowns to be computed, b is the
+* vector of right-hand sides.
+*
+* On entry elements of the vector b should be stored in dense format
+* in locations x[1], ..., x[m], where m is the number of rows. On exit
+* the routine stores elements of the vector x in the same locations.
+*
+* SCALING/UNSCALING
+*
+* See comments to the routine glp_ftran. */
+
+void glp_btran(glp_prob *lp, double x[])
+{ int m = lp->m;
+ GLPROW **row = lp->row;
+ GLPCOL **col = lp->col;
+ int i, k;
+ /* B'*x = b ===> (SB*B'*R)*(inv(R)*x) = SB*b ===>
+ (B")'*x" = b", where b" = SB*b, x = R*x" */
+ if (!(m == 0 || lp->valid))
+ xerror("glp_btran: basis factorization does not exist\n");
+ /* b" := SB*b */
+ for (i = 1; i <= m; i++)
+ { k = lp->head[i];
+ if (k <= m)
+ x[i] /= row[k]->rii;
+ else
+ x[i] *= col[k-m]->sjj;
+ }
+ /* x" := inv[(B")']*b" */
+ if (m > 0) bfd_btran(lp->bfd, x);
+ /* x := R*x" */
+ for (i = 1; i <= m; i++)
+ x[i] *= row[i]->rii;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_warm_up - "warm up" LP basis
+*
+* SYNOPSIS
+*
+* int glp_warm_up(glp_prob *P);
+*
+* DESCRIPTION
+*
+* The routine glp_warm_up "warms up" the LP basis for the specified
+* problem object using current statuses assigned to rows and columns
+* (that is, to auxiliary and structural variables).
+*
+* This operation includes computing factorization of the basis matrix
+* (if it does not exist), computing primal and dual components of basic
+* solution, and determining the solution status.
+*
+* RETURNS
+*
+* 0 The operation has been successfully performed.
+*
+* GLP_EBADB
+* The basis matrix is invalid, i.e. the number of basic (auxiliary
+* and structural) variables differs from the number of rows in the
+* problem object.
+*
+* GLP_ESING
+* The basis matrix is singular within the working precision.
+*
+* GLP_ECOND
+* The basis matrix is ill-conditioned. */
+
+int glp_warm_up(glp_prob *P)
+{ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, j, type, stat, ret;
+ double eps, temp, *work;
+ /* invalidate basic solution */
+ P->pbs_stat = P->dbs_stat = GLP_UNDEF;
+ P->obj_val = 0.0;
+ P->some = 0;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ row->prim = row->dual = 0.0;
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ col->prim = col->dual = 0.0;
+ }
+ /* compute the basis factorization, if necessary */
+ if (!glp_bf_exists(P))
+ { ret = glp_factorize(P);
+ if (ret != 0) goto done;
+ }
+ /* allocate working array */
+ work = xcalloc(1+P->m, sizeof(double));
+ /* determine and store values of non-basic variables, compute
+ vector (- N * xN) */
+ for (i = 1; i <= P->m; i++)
+ work[i] = 0.0;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->stat == GLP_BS)
+ continue;
+ else if (row->stat == GLP_NL)
+ row->prim = row->lb;
+ else if (row->stat == GLP_NU)
+ row->prim = row->ub;
+ else if (row->stat == GLP_NF)
+ row->prim = 0.0;
+ else if (row->stat == GLP_NS)
+ row->prim = row->lb;
+ else
+ xassert(row != row);
+ /* N[j] is i-th column of matrix (I|-A) */
+ work[i] -= row->prim;
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->stat == GLP_BS)
+ continue;
+ else if (col->stat == GLP_NL)
+ col->prim = col->lb;
+ else if (col->stat == GLP_NU)
+ col->prim = col->ub;
+ else if (col->stat == GLP_NF)
+ col->prim = 0.0;
+ else if (col->stat == GLP_NS)
+ col->prim = col->lb;
+ else
+ xassert(col != col);
+ /* N[j] is (m+j)-th column of matrix (I|-A) */
+ if (col->prim != 0.0)
+ { for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ work[aij->row->i] += aij->val * col->prim;
+ }
+ }
+ /* compute vector of basic variables xB = - inv(B) * N * xN */
+ glp_ftran(P, work);
+ /* store values of basic variables, check primal feasibility */
+ P->pbs_stat = GLP_FEAS;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->stat != GLP_BS)
+ continue;
+ row->prim = work[row->bind];
+ type = row->type;
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { eps = 1e-6 + 1e-9 * fabs(row->lb);
+ if (row->prim < row->lb - eps)
+ P->pbs_stat = GLP_INFEAS;
+ }
+ if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ { eps = 1e-6 + 1e-9 * fabs(row->ub);
+ if (row->prim > row->ub + eps)
+ P->pbs_stat = GLP_INFEAS;
+ }
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->stat != GLP_BS)
+ continue;
+ col->prim = work[col->bind];
+ type = col->type;
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { eps = 1e-6 + 1e-9 * fabs(col->lb);
+ if (col->prim < col->lb - eps)
+ P->pbs_stat = GLP_INFEAS;
+ }
+ if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ { eps = 1e-6 + 1e-9 * fabs(col->ub);
+ if (col->prim > col->ub + eps)
+ P->pbs_stat = GLP_INFEAS;
+ }
+ }
+ /* compute value of the objective function */
+ P->obj_val = P->c0;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ P->obj_val += col->coef * col->prim;
+ }
+ /* build vector cB of objective coefficients at basic variables */
+ for (i = 1; i <= P->m; i++)
+ work[i] = 0.0;
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->stat == GLP_BS)
+ work[col->bind] = col->coef;
+ }
+ /* compute vector of simplex multipliers pi = inv(B') * cB */
+ glp_btran(P, work);
+ /* compute and store reduced costs of non-basic variables d[j] =
+ c[j] - N'[j] * pi, check dual feasibility */
+ P->dbs_stat = GLP_FEAS;
+ for (i = 1; i <= P->m; i++)
+ { row = P->row[i];
+ if (row->stat == GLP_BS)
+ { row->dual = 0.0;
+ continue;
+ }
+ /* N[j] is i-th column of matrix (I|-A) */
+ row->dual = - work[i];
+#if 0 /* 07/III-2013 */
+ type = row->type;
+ temp = (P->dir == GLP_MIN ? + row->dual : - row->dual);
+ if ((type == GLP_FR || type == GLP_LO) && temp < -1e-5 ||
+ (type == GLP_FR || type == GLP_UP) && temp > +1e-5)
+ P->dbs_stat = GLP_INFEAS;
+#else
+ stat = row->stat;
+ temp = (P->dir == GLP_MIN ? + row->dual : - row->dual);
+ if ((stat == GLP_NF || stat == GLP_NL) && temp < -1e-5 ||
+ (stat == GLP_NF || stat == GLP_NU) && temp > +1e-5)
+ P->dbs_stat = GLP_INFEAS;
+#endif
+ }
+ for (j = 1; j <= P->n; j++)
+ { col = P->col[j];
+ if (col->stat == GLP_BS)
+ { col->dual = 0.0;
+ continue;
+ }
+ /* N[j] is (m+j)-th column of matrix (I|-A) */
+ col->dual = col->coef;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ col->dual += aij->val * work[aij->row->i];
+#if 0 /* 07/III-2013 */
+ type = col->type;
+ temp = (P->dir == GLP_MIN ? + col->dual : - col->dual);
+ if ((type == GLP_FR || type == GLP_LO) && temp < -1e-5 ||
+ (type == GLP_FR || type == GLP_UP) && temp > +1e-5)
+ P->dbs_stat = GLP_INFEAS;
+#else
+ stat = col->stat;
+ temp = (P->dir == GLP_MIN ? + col->dual : - col->dual);
+ if ((stat == GLP_NF || stat == GLP_NL) && temp < -1e-5 ||
+ (stat == GLP_NF || stat == GLP_NU) && temp > +1e-5)
+ P->dbs_stat = GLP_INFEAS;
+#endif
+ }
+ /* free working array */
+ xfree(work);
+ ret = 0;
+done: return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_eval_tab_row - compute row of the simplex tableau
+*
+* SYNOPSIS
+*
+* int glp_eval_tab_row(glp_prob *lp, int k, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_eval_tab_row computes a row of the current simplex
+* tableau for the basic variable, which is specified by the number k:
+* if 1 <= k <= m, x[k] is k-th auxiliary variable; if m+1 <= k <= m+n,
+* x[k] is (k-m)-th structural variable, where m is number of rows, and
+* n is number of columns. The current basis must be available.
+*
+* The routine stores column indices and numerical values of non-zero
+* elements of the computed row using sparse format to the locations
+* ind[1], ..., ind[len] and val[1], ..., val[len], respectively, where
+* 0 <= len <= n is number of non-zeros returned on exit.
+*
+* Element indices stored in the array ind have the same sense as the
+* index k, i.e. indices 1 to m denote auxiliary variables and indices
+* m+1 to m+n denote structural ones (all these variables are obviously
+* non-basic by definition).
+*
+* The computed row shows how the specified basic variable x[k] = xB[i]
+* depends on non-basic variables:
+*
+* xB[i] = alfa[i,1]*xN[1] + alfa[i,2]*xN[2] + ... + alfa[i,n]*xN[n],
+*
+* where alfa[i,j] are elements of the simplex table row, xN[j] are
+* non-basic (auxiliary and structural) variables.
+*
+* RETURNS
+*
+* The routine returns number of non-zero elements in the simplex table
+* row stored in the arrays ind and val.
+*
+* BACKGROUND
+*
+* The system of equality constraints of the LP problem is:
+*
+* xR = A * xS, (1)
+*
+* where xR is the vector of auxliary variables, xS is the vector of
+* structural variables, A is the matrix of constraint coefficients.
+*
+* The system (1) can be written in homogenous form as follows:
+*
+* A~ * x = 0, (2)
+*
+* where A~ = (I | -A) is the augmented constraint matrix (has m rows
+* and m+n columns), x = (xR | xS) is the vector of all (auxiliary and
+* structural) variables.
+*
+* By definition for the current basis we have:
+*
+* A~ = (B | N), (3)
+*
+* where B is the basis matrix. Thus, the system (2) can be written as:
+*
+* B * xB + N * xN = 0. (4)
+*
+* From (4) it follows that:
+*
+* xB = A^ * xN, (5)
+*
+* where the matrix
+*
+* A^ = - inv(B) * N (6)
+*
+* is called the simplex table.
+*
+* It is understood that i-th row of the simplex table is:
+*
+* e * A^ = - e * inv(B) * N, (7)
+*
+* where e is a unity vector with e[i] = 1.
+*
+* To compute i-th row of the simplex table the routine first computes
+* i-th row of the inverse:
+*
+* rho = inv(B') * e, (8)
+*
+* where B' is a matrix transposed to B, and then computes elements of
+* i-th row of the simplex table as scalar products:
+*
+* alfa[i,j] = - rho * N[j] for all j, (9)
+*
+* where N[j] is a column of the augmented constraint matrix A~, which
+* corresponds to some non-basic auxiliary or structural variable. */
+
+int glp_eval_tab_row(glp_prob *lp, int k, int ind[], double val[])
+{ int m = lp->m;
+ int n = lp->n;
+ int i, t, len, lll, *iii;
+ double alfa, *rho, *vvv;
+ if (!(m == 0 || lp->valid))
+ xerror("glp_eval_tab_row: basis factorization does not exist\n"
+ );
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_eval_tab_row: k = %d; variable number out of range"
+ , k);
+ /* determine xB[i] which corresponds to x[k] */
+ if (k <= m)
+ i = glp_get_row_bind(lp, k);
+ else
+ i = glp_get_col_bind(lp, k-m);
+ if (i == 0)
+ xerror("glp_eval_tab_row: k = %d; variable must be basic", k);
+ xassert(1 <= i && i <= m);
+ /* allocate working arrays */
+ rho = xcalloc(1+m, sizeof(double));
+ iii = xcalloc(1+m, sizeof(int));
+ vvv = xcalloc(1+m, sizeof(double));
+ /* compute i-th row of the inverse; see (8) */
+ for (t = 1; t <= m; t++) rho[t] = 0.0;
+ rho[i] = 1.0;
+ glp_btran(lp, rho);
+ /* compute i-th row of the simplex table */
+ len = 0;
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ { /* x[k] is auxiliary variable, so N[k] is a unity column */
+ if (glp_get_row_stat(lp, k) == GLP_BS) continue;
+ /* compute alfa[i,j]; see (9) */
+ alfa = - rho[k];
+ }
+ else
+ { /* x[k] is structural variable, so N[k] is a column of the
+ original constraint matrix A with negative sign */
+ if (glp_get_col_stat(lp, k-m) == GLP_BS) continue;
+ /* compute alfa[i,j]; see (9) */
+ lll = glp_get_mat_col(lp, k-m, iii, vvv);
+ alfa = 0.0;
+ for (t = 1; t <= lll; t++) alfa += rho[iii[t]] * vvv[t];
+ }
+ /* store alfa[i,j] */
+ if (alfa != 0.0) len++, ind[len] = k, val[len] = alfa;
+ }
+ xassert(len <= n);
+ /* free working arrays */
+ xfree(rho);
+ xfree(iii);
+ xfree(vvv);
+ /* return to the calling program */
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_eval_tab_col - compute column of the simplex tableau
+*
+* SYNOPSIS
+*
+* int glp_eval_tab_col(glp_prob *lp, int k, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_eval_tab_col computes a column of the current simplex
+* table for the non-basic variable, which is specified by the number k:
+* if 1 <= k <= m, x[k] is k-th auxiliary variable; if m+1 <= k <= m+n,
+* x[k] is (k-m)-th structural variable, where m is number of rows, and
+* n is number of columns. The current basis must be available.
+*
+* The routine stores row indices and numerical values of non-zero
+* elements of the computed column using sparse format to the locations
+* ind[1], ..., ind[len] and val[1], ..., val[len] respectively, where
+* 0 <= len <= m is number of non-zeros returned on exit.
+*
+* Element indices stored in the array ind have the same sense as the
+* index k, i.e. indices 1 to m denote auxiliary variables and indices
+* m+1 to m+n denote structural ones (all these variables are obviously
+* basic by the definition).
+*
+* The computed column shows how basic variables depend on the specified
+* non-basic variable x[k] = xN[j]:
+*
+* xB[1] = ... + alfa[1,j]*xN[j] + ...
+* xB[2] = ... + alfa[2,j]*xN[j] + ...
+* . . . . . .
+* xB[m] = ... + alfa[m,j]*xN[j] + ...
+*
+* where alfa[i,j] are elements of the simplex table column, xB[i] are
+* basic (auxiliary and structural) variables.
+*
+* RETURNS
+*
+* The routine returns number of non-zero elements in the simplex table
+* column stored in the arrays ind and val.
+*
+* BACKGROUND
+*
+* As it was explained in comments to the routine glp_eval_tab_row (see
+* above) the simplex table is the following matrix:
+*
+* A^ = - inv(B) * N. (1)
+*
+* Therefore j-th column of the simplex table is:
+*
+* A^ * e = - inv(B) * N * e = - inv(B) * N[j], (2)
+*
+* where e is a unity vector with e[j] = 1, B is the basis matrix, N[j]
+* is a column of the augmented constraint matrix A~, which corresponds
+* to the given non-basic auxiliary or structural variable. */
+
+int glp_eval_tab_col(glp_prob *lp, int k, int ind[], double val[])
+{ int m = lp->m;
+ int n = lp->n;
+ int t, len, stat;
+ double *col;
+ if (!(m == 0 || lp->valid))
+ xerror("glp_eval_tab_col: basis factorization does not exist\n"
+ );
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_eval_tab_col: k = %d; variable number out of range"
+ , k);
+ if (k <= m)
+ stat = glp_get_row_stat(lp, k);
+ else
+ stat = glp_get_col_stat(lp, k-m);
+ if (stat == GLP_BS)
+ xerror("glp_eval_tab_col: k = %d; variable must be non-basic",
+ k);
+ /* obtain column N[k] with negative sign */
+ col = xcalloc(1+m, sizeof(double));
+ for (t = 1; t <= m; t++) col[t] = 0.0;
+ if (k <= m)
+ { /* x[k] is auxiliary variable, so N[k] is a unity column */
+ col[k] = -1.0;
+ }
+ else
+ { /* x[k] is structural variable, so N[k] is a column of the
+ original constraint matrix A with negative sign */
+ len = glp_get_mat_col(lp, k-m, ind, val);
+ for (t = 1; t <= len; t++) col[ind[t]] = val[t];
+ }
+ /* compute column of the simplex table, which corresponds to the
+ specified non-basic variable x[k] */
+ glp_ftran(lp, col);
+ len = 0;
+ for (t = 1; t <= m; t++)
+ { if (col[t] != 0.0)
+ { len++;
+ ind[len] = glp_get_bhead(lp, t);
+ val[len] = col[t];
+ }
+ }
+ xfree(col);
+ /* return to the calling program */
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_transform_row - transform explicitly specified row
+*
+* SYNOPSIS
+*
+* int glp_transform_row(glp_prob *P, int len, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_transform_row performs the same operation as the
+* routine glp_eval_tab_row with exception that the row to be
+* transformed is specified explicitly as a sparse vector.
+*
+* The explicitly specified row may be thought as a linear form:
+*
+* x = a[1]*x[m+1] + a[2]*x[m+2] + ... + a[n]*x[m+n], (1)
+*
+* where x is an auxiliary variable for this row, a[j] are coefficients
+* of the linear form, x[m+j] are structural variables.
+*
+* On entry column indices and numerical values of non-zero elements of
+* the row should be stored in locations ind[1], ..., ind[len] and
+* val[1], ..., val[len], where len is the number of non-zero elements.
+*
+* This routine uses the system of equality constraints and the current
+* basis in order to express the auxiliary variable x in (1) through the
+* current non-basic variables (as if the transformed row were added to
+* the problem object and its auxiliary variable were basic), i.e. the
+* resultant row has the form:
+*
+* x = alfa[1]*xN[1] + alfa[2]*xN[2] + ... + alfa[n]*xN[n], (2)
+*
+* where xN[j] are non-basic (auxiliary or structural) variables, n is
+* the number of columns in the LP problem object.
+*
+* On exit the routine stores indices and numerical values of non-zero
+* elements of the resultant row (2) in locations ind[1], ..., ind[len']
+* and val[1], ..., val[len'], where 0 <= len' <= n is the number of
+* non-zero elements in the resultant row returned by the routine. Note
+* that indices (numbers) of non-basic variables stored in the array ind
+* correspond to original ordinal numbers of variables: indices 1 to m
+* mean auxiliary variables and indices m+1 to m+n mean structural ones.
+*
+* RETURNS
+*
+* The routine returns len', which is the number of non-zero elements in
+* the resultant row stored in the arrays ind and val.
+*
+* BACKGROUND
+*
+* The explicitly specified row (1) is transformed in the same way as it
+* were the objective function row.
+*
+* From (1) it follows that:
+*
+* x = aB * xB + aN * xN, (3)
+*
+* where xB is the vector of basic variables, xN is the vector of
+* non-basic variables.
+*
+* The simplex table, which corresponds to the current basis, is:
+*
+* xB = [-inv(B) * N] * xN. (4)
+*
+* Therefore substituting xB from (4) to (3) we have:
+*
+* x = aB * [-inv(B) * N] * xN + aN * xN =
+* (5)
+* = rho * (-N) * xN + aN * xN = alfa * xN,
+*
+* where:
+*
+* rho = inv(B') * aB, (6)
+*
+* and
+*
+* alfa = aN + rho * (-N) (7)
+*
+* is the resultant row computed by the routine. */
+
+int glp_transform_row(glp_prob *P, int len, int ind[], double val[])
+{ int i, j, k, m, n, t, lll, *iii;
+ double alfa, *a, *aB, *rho, *vvv;
+ if (!glp_bf_exists(P))
+ xerror("glp_transform_row: basis factorization does not exist "
+ "\n");
+ m = glp_get_num_rows(P);
+ n = glp_get_num_cols(P);
+ /* unpack the row to be transformed to the array a */
+ a = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++) a[j] = 0.0;
+ if (!(0 <= len && len <= n))
+ xerror("glp_transform_row: len = %d; invalid row length\n",
+ len);
+ for (t = 1; t <= len; t++)
+ { j = ind[t];
+ if (!(1 <= j && j <= n))
+ xerror("glp_transform_row: ind[%d] = %d; column index out o"
+ "f range\n", t, j);
+ if (val[t] == 0.0)
+ xerror("glp_transform_row: val[%d] = 0; zero coefficient no"
+ "t allowed\n", t);
+ if (a[j] != 0.0)
+ xerror("glp_transform_row: ind[%d] = %d; duplicate column i"
+ "ndices not allowed\n", t, j);
+ a[j] = val[t];
+ }
+ /* construct the vector aB */
+ aB = xcalloc(1+m, sizeof(double));
+ for (i = 1; i <= m; i++)
+ { k = glp_get_bhead(P, i);
+ /* xB[i] is k-th original variable */
+ xassert(1 <= k && k <= m+n);
+ aB[i] = (k <= m ? 0.0 : a[k-m]);
+ }
+ /* solve the system B'*rho = aB to compute the vector rho */
+ rho = aB, glp_btran(P, rho);
+ /* compute coefficients at non-basic auxiliary variables */
+ len = 0;
+ for (i = 1; i <= m; i++)
+ { if (glp_get_row_stat(P, i) != GLP_BS)
+ { alfa = - rho[i];
+ if (alfa != 0.0)
+ { len++;
+ ind[len] = i;
+ val[len] = alfa;
+ }
+ }
+ }
+ /* compute coefficients at non-basic structural variables */
+ iii = xcalloc(1+m, sizeof(int));
+ vvv = xcalloc(1+m, sizeof(double));
+ for (j = 1; j <= n; j++)
+ { if (glp_get_col_stat(P, j) != GLP_BS)
+ { alfa = a[j];
+ lll = glp_get_mat_col(P, j, iii, vvv);
+ for (t = 1; t <= lll; t++) alfa += vvv[t] * rho[iii[t]];
+ if (alfa != 0.0)
+ { len++;
+ ind[len] = m+j;
+ val[len] = alfa;
+ }
+ }
+ }
+ xassert(len <= n);
+ xfree(iii);
+ xfree(vvv);
+ xfree(aB);
+ xfree(a);
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_transform_col - transform explicitly specified column
+*
+* SYNOPSIS
+*
+* int glp_transform_col(glp_prob *P, int len, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* The routine glp_transform_col performs the same operation as the
+* routine glp_eval_tab_col with exception that the column to be
+* transformed is specified explicitly as a sparse vector.
+*
+* The explicitly specified column may be thought as if it were added
+* to the original system of equality constraints:
+*
+* x[1] = a[1,1]*x[m+1] + ... + a[1,n]*x[m+n] + a[1]*x
+* x[2] = a[2,1]*x[m+1] + ... + a[2,n]*x[m+n] + a[2]*x (1)
+* . . . . . . . . . . . . . . .
+* x[m] = a[m,1]*x[m+1] + ... + a[m,n]*x[m+n] + a[m]*x
+*
+* where x[i] are auxiliary variables, x[m+j] are structural variables,
+* x is a structural variable for the explicitly specified column, a[i]
+* are constraint coefficients for x.
+*
+* On entry row indices and numerical values of non-zero elements of
+* the column should be stored in locations ind[1], ..., ind[len] and
+* val[1], ..., val[len], where len is the number of non-zero elements.
+*
+* This routine uses the system of equality constraints and the current
+* basis in order to express the current basic variables through the
+* structural variable x in (1) (as if the transformed column were added
+* to the problem object and the variable x were non-basic), i.e. the
+* resultant column has the form:
+*
+* xB[1] = ... + alfa[1]*x
+* xB[2] = ... + alfa[2]*x (2)
+* . . . . . .
+* xB[m] = ... + alfa[m]*x
+*
+* where xB are basic (auxiliary and structural) variables, m is the
+* number of rows in the problem object.
+*
+* On exit the routine stores indices and numerical values of non-zero
+* elements of the resultant column (2) in locations ind[1], ...,
+* ind[len'] and val[1], ..., val[len'], where 0 <= len' <= m is the
+* number of non-zero element in the resultant column returned by the
+* routine. Note that indices (numbers) of basic variables stored in
+* the array ind correspond to original ordinal numbers of variables:
+* indices 1 to m mean auxiliary variables and indices m+1 to m+n mean
+* structural ones.
+*
+* RETURNS
+*
+* The routine returns len', which is the number of non-zero elements
+* in the resultant column stored in the arrays ind and val.
+*
+* BACKGROUND
+*
+* The explicitly specified column (1) is transformed in the same way
+* as any other column of the constraint matrix using the formula:
+*
+* alfa = inv(B) * a, (3)
+*
+* where alfa is the resultant column computed by the routine. */
+
+int glp_transform_col(glp_prob *P, int len, int ind[], double val[])
+{ int i, m, t;
+ double *a, *alfa;
+ if (!glp_bf_exists(P))
+ xerror("glp_transform_col: basis factorization does not exist "
+ "\n");
+ m = glp_get_num_rows(P);
+ /* unpack the column to be transformed to the array a */
+ a = xcalloc(1+m, sizeof(double));
+ for (i = 1; i <= m; i++) a[i] = 0.0;
+ if (!(0 <= len && len <= m))
+ xerror("glp_transform_col: len = %d; invalid column length\n",
+ len);
+ for (t = 1; t <= len; t++)
+ { i = ind[t];
+ if (!(1 <= i && i <= m))
+ xerror("glp_transform_col: ind[%d] = %d; row index out of r"
+ "ange\n", t, i);
+ if (val[t] == 0.0)
+ xerror("glp_transform_col: val[%d] = 0; zero coefficient no"
+ "t allowed\n", t);
+ if (a[i] != 0.0)
+ xerror("glp_transform_col: ind[%d] = %d; duplicate row indi"
+ "ces not allowed\n", t, i);
+ a[i] = val[t];
+ }
+ /* solve the system B*a = alfa to compute the vector alfa */
+ alfa = a, glp_ftran(P, alfa);
+ /* store resultant coefficients */
+ len = 0;
+ for (i = 1; i <= m; i++)
+ { if (alfa[i] != 0.0)
+ { len++;
+ ind[len] = glp_get_bhead(P, i);
+ val[len] = alfa[i];
+ }
+ }
+ xfree(a);
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_prim_rtest - perform primal ratio test
+*
+* SYNOPSIS
+*
+* int glp_prim_rtest(glp_prob *P, int len, const int ind[],
+* const double val[], int dir, double eps);
+*
+* DESCRIPTION
+*
+* The routine glp_prim_rtest performs the primal ratio test using an
+* explicitly specified column of the simplex table.
+*
+* The current basic solution associated with the LP problem object
+* must be primal feasible.
+*
+* The explicitly specified column of the simplex table shows how the
+* basic variables xB depend on some non-basic variable x (which is not
+* necessarily presented in the problem object):
+*
+* xB[1] = ... + alfa[1] * x + ...
+* xB[2] = ... + alfa[2] * x + ... (*)
+* . . . . . . . .
+* xB[m] = ... + alfa[m] * x + ...
+*
+* The column (*) is specifed on entry to the routine using the sparse
+* format. Ordinal numbers of basic variables xB[i] should be placed in
+* locations ind[1], ..., ind[len], where ordinal number 1 to m denote
+* auxiliary variables, and ordinal numbers m+1 to m+n denote structural
+* variables. The corresponding non-zero coefficients alfa[i] should be
+* placed in locations val[1], ..., val[len]. The arrays ind and val are
+* not changed on exit.
+*
+* The parameter dir specifies direction in which the variable x changes
+* on entering the basis: +1 means increasing, -1 means decreasing.
+*
+* The parameter eps is an absolute tolerance (small positive number)
+* used by the routine to skip small alfa[j] of the row (*).
+*
+* The routine determines which basic variable (among specified in
+* ind[1], ..., ind[len]) should leave the basis in order to keep primal
+* feasibility.
+*
+* RETURNS
+*
+* The routine glp_prim_rtest returns the index piv in the arrays ind
+* and val corresponding to the pivot element chosen, 1 <= piv <= len.
+* If the adjacent basic solution is primal unbounded and therefore the
+* choice cannot be made, the routine returns zero.
+*
+* COMMENTS
+*
+* If the non-basic variable x is presented in the LP problem object,
+* the column (*) can be computed with the routine glp_eval_tab_col;
+* otherwise it can be computed with the routine glp_transform_col. */
+
+int glp_prim_rtest(glp_prob *P, int len, const int ind[],
+ const double val[], int dir, double eps)
+{ int k, m, n, piv, t, type, stat;
+ double alfa, big, beta, lb, ub, temp, teta;
+ if (glp_get_prim_stat(P) != GLP_FEAS)
+ xerror("glp_prim_rtest: basic solution is not primal feasible "
+ "\n");
+ if (!(dir == +1 || dir == -1))
+ xerror("glp_prim_rtest: dir = %d; invalid parameter\n", dir);
+ if (!(0.0 < eps && eps < 1.0))
+ xerror("glp_prim_rtest: eps = %g; invalid parameter\n", eps);
+ m = glp_get_num_rows(P);
+ n = glp_get_num_cols(P);
+ /* initial settings */
+ piv = 0, teta = DBL_MAX, big = 0.0;
+ /* walk through the entries of the specified column */
+ for (t = 1; t <= len; t++)
+ { /* get the ordinal number of basic variable */
+ k = ind[t];
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_prim_rtest: ind[%d] = %d; variable number out o"
+ "f range\n", t, k);
+ /* determine type, bounds, status and primal value of basic
+ variable xB[i] = x[k] in the current basic solution */
+ if (k <= m)
+ { type = glp_get_row_type(P, k);
+ lb = glp_get_row_lb(P, k);
+ ub = glp_get_row_ub(P, k);
+ stat = glp_get_row_stat(P, k);
+ beta = glp_get_row_prim(P, k);
+ }
+ else
+ { type = glp_get_col_type(P, k-m);
+ lb = glp_get_col_lb(P, k-m);
+ ub = glp_get_col_ub(P, k-m);
+ stat = glp_get_col_stat(P, k-m);
+ beta = glp_get_col_prim(P, k-m);
+ }
+ if (stat != GLP_BS)
+ xerror("glp_prim_rtest: ind[%d] = %d; non-basic variable no"
+ "t allowed\n", t, k);
+ /* determine influence coefficient at basic variable xB[i]
+ in the explicitly specified column and turn to the case of
+ increasing the variable x in order to simplify the program
+ logic */
+ alfa = (dir > 0 ? + val[t] : - val[t]);
+ /* analyze main cases */
+ if (type == GLP_FR)
+ { /* xB[i] is free variable */
+ continue;
+ }
+ else if (type == GLP_LO)
+lo: { /* xB[i] has an lower bound */
+ if (alfa > - eps) continue;
+ temp = (lb - beta) / alfa;
+ }
+ else if (type == GLP_UP)
+up: { /* xB[i] has an upper bound */
+ if (alfa < + eps) continue;
+ temp = (ub - beta) / alfa;
+ }
+ else if (type == GLP_DB)
+ { /* xB[i] has both lower and upper bounds */
+ if (alfa < 0.0) goto lo; else goto up;
+ }
+ else if (type == GLP_FX)
+ { /* xB[i] is fixed variable */
+ if (- eps < alfa && alfa < + eps) continue;
+ temp = 0.0;
+ }
+ else
+ xassert(type != type);
+ /* if the value of the variable xB[i] violates its lower or
+ upper bound (slightly, because the current basis is assumed
+ to be primal feasible), temp is negative; we can think this
+ happens due to round-off errors and the value is exactly on
+ the bound; this allows replacing temp by zero */
+ if (temp < 0.0) temp = 0.0;
+ /* apply the minimal ratio test */
+ if (teta > temp || teta == temp && big < fabs(alfa))
+ piv = t, teta = temp, big = fabs(alfa);
+ }
+ /* return index of the pivot element chosen */
+ return piv;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_dual_rtest - perform dual ratio test
+*
+* SYNOPSIS
+*
+* int glp_dual_rtest(glp_prob *P, int len, const int ind[],
+* const double val[], int dir, double eps);
+*
+* DESCRIPTION
+*
+* The routine glp_dual_rtest performs the dual ratio test using an
+* explicitly specified row of the simplex table.
+*
+* The current basic solution associated with the LP problem object
+* must be dual feasible.
+*
+* The explicitly specified row of the simplex table is a linear form
+* that shows how some basic variable x (which is not necessarily
+* presented in the problem object) depends on non-basic variables xN:
+*
+* x = alfa[1] * xN[1] + alfa[2] * xN[2] + ... + alfa[n] * xN[n]. (*)
+*
+* The row (*) is specified on entry to the routine using the sparse
+* format. Ordinal numbers of non-basic variables xN[j] should be placed
+* in locations ind[1], ..., ind[len], where ordinal numbers 1 to m
+* denote auxiliary variables, and ordinal numbers m+1 to m+n denote
+* structural variables. The corresponding non-zero coefficients alfa[j]
+* should be placed in locations val[1], ..., val[len]. The arrays ind
+* and val are not changed on exit.
+*
+* The parameter dir specifies direction in which the variable x changes
+* on leaving the basis: +1 means that x goes to its lower bound, and -1
+* means that x goes to its upper bound.
+*
+* The parameter eps is an absolute tolerance (small positive number)
+* used by the routine to skip small alfa[j] of the row (*).
+*
+* The routine determines which non-basic variable (among specified in
+* ind[1], ..., ind[len]) should enter the basis in order to keep dual
+* feasibility.
+*
+* RETURNS
+*
+* The routine glp_dual_rtest returns the index piv in the arrays ind
+* and val corresponding to the pivot element chosen, 1 <= piv <= len.
+* If the adjacent basic solution is dual unbounded and therefore the
+* choice cannot be made, the routine returns zero.
+*
+* COMMENTS
+*
+* If the basic variable x is presented in the LP problem object, the
+* row (*) can be computed with the routine glp_eval_tab_row; otherwise
+* it can be computed with the routine glp_transform_row. */
+
+int glp_dual_rtest(glp_prob *P, int len, const int ind[],
+ const double val[], int dir, double eps)
+{ int k, m, n, piv, t, stat;
+ double alfa, big, cost, obj, temp, teta;
+ if (glp_get_dual_stat(P) != GLP_FEAS)
+ xerror("glp_dual_rtest: basic solution is not dual feasible\n")
+ ;
+ if (!(dir == +1 || dir == -1))
+ xerror("glp_dual_rtest: dir = %d; invalid parameter\n", dir);
+ if (!(0.0 < eps && eps < 1.0))
+ xerror("glp_dual_rtest: eps = %g; invalid parameter\n", eps);
+ m = glp_get_num_rows(P);
+ n = glp_get_num_cols(P);
+ /* take into account optimization direction */
+ obj = (glp_get_obj_dir(P) == GLP_MIN ? +1.0 : -1.0);
+ /* initial settings */
+ piv = 0, teta = DBL_MAX, big = 0.0;
+ /* walk through the entries of the specified row */
+ for (t = 1; t <= len; t++)
+ { /* get ordinal number of non-basic variable */
+ k = ind[t];
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_dual_rtest: ind[%d] = %d; variable number out o"
+ "f range\n", t, k);
+ /* determine status and reduced cost of non-basic variable
+ x[k] = xN[j] in the current basic solution */
+ if (k <= m)
+ { stat = glp_get_row_stat(P, k);
+ cost = glp_get_row_dual(P, k);
+ }
+ else
+ { stat = glp_get_col_stat(P, k-m);
+ cost = glp_get_col_dual(P, k-m);
+ }
+ if (stat == GLP_BS)
+ xerror("glp_dual_rtest: ind[%d] = %d; basic variable not al"
+ "lowed\n", t, k);
+ /* determine influence coefficient at non-basic variable xN[j]
+ in the explicitly specified row and turn to the case of
+ increasing the variable x in order to simplify the program
+ logic */
+ alfa = (dir > 0 ? + val[t] : - val[t]);
+ /* analyze main cases */
+ if (stat == GLP_NL)
+ { /* xN[j] is on its lower bound */
+ if (alfa < + eps) continue;
+ temp = (obj * cost) / alfa;
+ }
+ else if (stat == GLP_NU)
+ { /* xN[j] is on its upper bound */
+ if (alfa > - eps) continue;
+ temp = (obj * cost) / alfa;
+ }
+ else if (stat == GLP_NF)
+ { /* xN[j] is non-basic free variable */
+ if (- eps < alfa && alfa < + eps) continue;
+ temp = 0.0;
+ }
+ else if (stat == GLP_NS)
+ { /* xN[j] is non-basic fixed variable */
+ continue;
+ }
+ else
+ xassert(stat != stat);
+ /* if the reduced cost of the variable xN[j] violates its zero
+ bound (slightly, because the current basis is assumed to be
+ dual feasible), temp is negative; we can think this happens
+ due to round-off errors and the reduced cost is exact zero;
+ this allows replacing temp by zero */
+ if (temp < 0.0) temp = 0.0;
+ /* apply the minimal ratio test */
+ if (teta > temp || teta == temp && big < fabs(alfa))
+ piv = t, teta = temp, big = fabs(alfa);
+ }
+ /* return index of the pivot element chosen */
+ return piv;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_analyze_row - simulate one iteration of dual simplex method
+*
+* SYNOPSIS
+*
+* int glp_analyze_row(glp_prob *P, int len, const int ind[],
+* const double val[], int type, double rhs, double eps, int *piv,
+* double *x, double *dx, double *y, double *dy, double *dz);
+*
+* DESCRIPTION
+*
+* Let the current basis be optimal or dual feasible, and there be
+* specified a row (constraint), which is violated by the current basic
+* solution. The routine glp_analyze_row simulates one iteration of the
+* dual simplex method to determine some information on the adjacent
+* basis (see below), where the specified row becomes active constraint
+* (i.e. its auxiliary variable becomes non-basic).
+*
+* The current basic solution associated with the problem object passed
+* to the routine must be dual feasible, and its primal components must
+* be defined.
+*
+* The row to be analyzed must be previously transformed either with
+* the routine glp_eval_tab_row (if the row is in the problem object)
+* or with the routine glp_transform_row (if the row is external, i.e.
+* not in the problem object). This is needed to express the row only
+* through (auxiliary and structural) variables, which are non-basic in
+* the current basis:
+*
+* y = alfa[1] * xN[1] + alfa[2] * xN[2] + ... + alfa[n] * xN[n],
+*
+* where y is an auxiliary variable of the row, alfa[j] is an influence
+* coefficient, xN[j] is a non-basic variable.
+*
+* The row is passed to the routine in sparse format. Ordinal numbers
+* of non-basic variables are stored in locations ind[1], ..., ind[len],
+* where numbers 1 to m denote auxiliary variables while numbers m+1 to
+* m+n denote structural variables. Corresponding non-zero coefficients
+* alfa[j] are stored in locations val[1], ..., val[len]. The arrays
+* ind and val are ot changed on exit.
+*
+* The parameters type and rhs specify the row type and its right-hand
+* side as follows:
+*
+* type = GLP_LO: y = sum alfa[j] * xN[j] >= rhs
+*
+* type = GLP_UP: y = sum alfa[j] * xN[j] <= rhs
+*
+* The parameter eps is an absolute tolerance (small positive number)
+* used by the routine to skip small coefficients alfa[j] on performing
+* the dual ratio test.
+*
+* If the operation was successful, the routine stores the following
+* information to corresponding location (if some parameter is NULL,
+* its value is not stored):
+*
+* piv index in the array ind and val, 1 <= piv <= len, determining
+* the non-basic variable, which would enter the adjacent basis;
+*
+* x value of the non-basic variable in the current basis;
+*
+* dx difference between values of the non-basic variable in the
+* adjacent and current bases, dx = x.new - x.old;
+*
+* y value of the row (i.e. of its auxiliary variable) in the
+* current basis;
+*
+* dy difference between values of the row in the adjacent and
+* current bases, dy = y.new - y.old;
+*
+* dz difference between values of the objective function in the
+* adjacent and current bases, dz = z.new - z.old. Note that in
+* case of minimization dz >= 0, and in case of maximization
+* dz <= 0, i.e. in the adjacent basis the objective function
+* always gets worse (degrades). */
+
+int _glp_analyze_row(glp_prob *P, int len, const int ind[],
+ const double val[], int type, double rhs, double eps, int *_piv,
+ double *_x, double *_dx, double *_y, double *_dy, double *_dz)
+{ int t, k, dir, piv, ret = 0;
+ double x, dx, y, dy, dz;
+ if (P->pbs_stat == GLP_UNDEF)
+ xerror("glp_analyze_row: primal basic solution components are "
+ "undefined\n");
+ if (P->dbs_stat != GLP_FEAS)
+ xerror("glp_analyze_row: basic solution is not dual feasible\n"
+ );
+ /* compute the row value y = sum alfa[j] * xN[j] in the current
+ basis */
+ if (!(0 <= len && len <= P->n))
+ xerror("glp_analyze_row: len = %d; invalid row length\n", len);
+ y = 0.0;
+ for (t = 1; t <= len; t++)
+ { /* determine value of x[k] = xN[j] in the current basis */
+ k = ind[t];
+ if (!(1 <= k && k <= P->m+P->n))
+ xerror("glp_analyze_row: ind[%d] = %d; row/column index out"
+ " of range\n", t, k);
+ if (k <= P->m)
+ { /* x[k] is auxiliary variable */
+ if (P->row[k]->stat == GLP_BS)
+ xerror("glp_analyze_row: ind[%d] = %d; basic auxiliary v"
+ "ariable is not allowed\n", t, k);
+ x = P->row[k]->prim;
+ }
+ else
+ { /* x[k] is structural variable */
+ if (P->col[k-P->m]->stat == GLP_BS)
+ xerror("glp_analyze_row: ind[%d] = %d; basic structural "
+ "variable is not allowed\n", t, k);
+ x = P->col[k-P->m]->prim;
+ }
+ y += val[t] * x;
+ }
+ /* check if the row is primal infeasible in the current basis,
+ i.e. the constraint is violated at the current point */
+ if (type == GLP_LO)
+ { if (y >= rhs)
+ { /* the constraint is not violated */
+ ret = 1;
+ goto done;
+ }
+ /* in the adjacent basis y goes to its lower bound */
+ dir = +1;
+ }
+ else if (type == GLP_UP)
+ { if (y <= rhs)
+ { /* the constraint is not violated */
+ ret = 1;
+ goto done;
+ }
+ /* in the adjacent basis y goes to its upper bound */
+ dir = -1;
+ }
+ else
+ xerror("glp_analyze_row: type = %d; invalid parameter\n",
+ type);
+ /* compute dy = y.new - y.old */
+ dy = rhs - y;
+ /* perform dual ratio test to determine which non-basic variable
+ should enter the adjacent basis to keep it dual feasible */
+ piv = glp_dual_rtest(P, len, ind, val, dir, eps);
+ if (piv == 0)
+ { /* no dual feasible adjacent basis exists */
+ ret = 2;
+ goto done;
+ }
+ /* non-basic variable x[k] = xN[j] should enter the basis */
+ k = ind[piv];
+ xassert(1 <= k && k <= P->m+P->n);
+ /* determine its value in the current basis */
+ if (k <= P->m)
+ x = P->row[k]->prim;
+ else
+ x = P->col[k-P->m]->prim;
+ /* compute dx = x.new - x.old = dy / alfa[j] */
+ xassert(val[piv] != 0.0);
+ dx = dy / val[piv];
+ /* compute dz = z.new - z.old = d[j] * dx, where d[j] is reduced
+ cost of xN[j] in the current basis */
+ if (k <= P->m)
+ dz = P->row[k]->dual * dx;
+ else
+ dz = P->col[k-P->m]->dual * dx;
+ /* store the analysis results */
+ if (_piv != NULL) *_piv = piv;
+ if (_x != NULL) *_x = x;
+ if (_dx != NULL) *_dx = dx;
+ if (_y != NULL) *_y = y;
+ if (_dy != NULL) *_dy = dy;
+ if (_dz != NULL) *_dz = dz;
+done: return ret;
+}
+
+#if 0
+int main(void)
+{ /* example program for the routine glp_analyze_row */
+ glp_prob *P;
+ glp_smcp parm;
+ int i, k, len, piv, ret, ind[1+100];
+ double rhs, x, dx, y, dy, dz, val[1+100];
+ P = glp_create_prob();
+ /* read plan.mps (see glpk/examples) */
+ ret = glp_read_mps(P, GLP_MPS_DECK, NULL, "plan.mps");
+ glp_assert(ret == 0);
+ /* and solve it to optimality */
+ ret = glp_simplex(P, NULL);
+ glp_assert(ret == 0);
+ glp_assert(glp_get_status(P) == GLP_OPT);
+ /* the optimal objective value is 296.217 */
+ /* we would like to know what happens if we would add a new row
+ (constraint) to plan.mps:
+ .01 * bin1 + .01 * bin2 + .02 * bin4 + .02 * bin5 <= 12 */
+ /* first, we specify this new row */
+ glp_create_index(P);
+ len = 0;
+ ind[++len] = glp_find_col(P, "BIN1"), val[len] = .01;
+ ind[++len] = glp_find_col(P, "BIN2"), val[len] = .01;
+ ind[++len] = glp_find_col(P, "BIN4"), val[len] = .02;
+ ind[++len] = glp_find_col(P, "BIN5"), val[len] = .02;
+ rhs = 12;
+ /* then we can compute value of the row (i.e. of its auxiliary
+ variable) in the current basis to see if the constraint is
+ violated */
+ y = 0.0;
+ for (k = 1; k <= len; k++)
+ y += val[k] * glp_get_col_prim(P, ind[k]);
+ glp_printf("y = %g\n", y);
+ /* this prints y = 15.1372, so the constraint is violated, since
+ we require that y <= rhs = 12 */
+ /* now we transform the row to express it only through non-basic
+ (auxiliary and artificial) variables */
+ len = glp_transform_row(P, len, ind, val);
+ /* finally, we simulate one step of the dual simplex method to
+ obtain necessary information for the adjacent basis */
+ ret = _glp_analyze_row(P, len, ind, val, GLP_UP, rhs, 1e-9, &piv,
+ &x, &dx, &y, &dy, &dz);
+ glp_assert(ret == 0);
+ glp_printf("k = %d, x = %g; dx = %g; y = %g; dy = %g; dz = %g\n",
+ ind[piv], x, dx, y, dy, dz);
+ /* this prints dz = 5.64418 and means that in the adjacent basis
+ the objective function would be 296.217 + 5.64418 = 301.861 */
+ /* now we actually include the row into the problem object; note
+ that the arrays ind and val are clobbered, so we need to build
+ them once again */
+ len = 0;
+ ind[++len] = glp_find_col(P, "BIN1"), val[len] = .01;
+ ind[++len] = glp_find_col(P, "BIN2"), val[len] = .01;
+ ind[++len] = glp_find_col(P, "BIN4"), val[len] = .02;
+ ind[++len] = glp_find_col(P, "BIN5"), val[len] = .02;
+ rhs = 12;
+ i = glp_add_rows(P, 1);
+ glp_set_row_bnds(P, i, GLP_UP, 0, rhs);
+ glp_set_mat_row(P, i, len, ind, val);
+ /* and perform one dual simplex iteration */
+ glp_init_smcp(&parm);
+ parm.meth = GLP_DUAL;
+ parm.it_lim = 1;
+ glp_simplex(P, &parm);
+ /* the current objective value is 301.861 */
+ return 0;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_analyze_bound - analyze active bound of non-basic variable
+*
+* SYNOPSIS
+*
+* void glp_analyze_bound(glp_prob *P, int k, double *limit1, int *var1,
+* double *limit2, int *var2);
+*
+* DESCRIPTION
+*
+* The routine glp_analyze_bound analyzes the effect of varying the
+* active bound of specified non-basic variable.
+*
+* The non-basic variable is specified by the parameter k, where
+* 1 <= k <= m means auxiliary variable of corresponding row while
+* m+1 <= k <= m+n means structural variable (column).
+*
+* Note that the current basic solution must be optimal, and the basis
+* factorization must exist.
+*
+* Results of the analysis have the following meaning.
+*
+* value1 is the minimal value of the active bound, at which the basis
+* still remains primal feasible and thus optimal. -DBL_MAX means that
+* the active bound has no lower limit.
+*
+* var1 is the ordinal number of an auxiliary (1 to m) or structural
+* (m+1 to n) basic variable, which reaches its bound first and thereby
+* limits further decreasing the active bound being analyzed.
+* if value1 = -DBL_MAX, var1 is set to 0.
+*
+* value2 is the maximal value of the active bound, at which the basis
+* still remains primal feasible and thus optimal. +DBL_MAX means that
+* the active bound has no upper limit.
+*
+* var2 is the ordinal number of an auxiliary (1 to m) or structural
+* (m+1 to n) basic variable, which reaches its bound first and thereby
+* limits further increasing the active bound being analyzed.
+* if value2 = +DBL_MAX, var2 is set to 0. */
+
+void glp_analyze_bound(glp_prob *P, int k, double *value1, int *var1,
+ double *value2, int *var2)
+{ GLPROW *row;
+ GLPCOL *col;
+ int m, n, stat, kase, p, len, piv, *ind;
+ double x, new_x, ll, uu, xx, delta, *val;
+#if 0 /* 04/IV-2016 */
+ /* sanity checks */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_analyze_bound: P = %p; invalid problem object\n",
+ P);
+#endif
+ m = P->m, n = P->n;
+ if (!(P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS))
+ xerror("glp_analyze_bound: optimal basic solution required\n");
+ if (!(m == 0 || P->valid))
+ xerror("glp_analyze_bound: basis factorization required\n");
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_analyze_bound: k = %d; variable number out of rang"
+ "e\n", k);
+ /* retrieve information about the specified non-basic variable
+ x[k] whose active bound is to be analyzed */
+ if (k <= m)
+ { row = P->row[k];
+ stat = row->stat;
+ x = row->prim;
+ }
+ else
+ { col = P->col[k-m];
+ stat = col->stat;
+ x = col->prim;
+ }
+ if (stat == GLP_BS)
+ xerror("glp_analyze_bound: k = %d; basic variable not allowed "
+ "\n", k);
+ /* allocate working arrays */
+ ind = xcalloc(1+m, sizeof(int));
+ val = xcalloc(1+m, sizeof(double));
+ /* compute column of the simplex table corresponding to the
+ non-basic variable x[k] */
+ len = glp_eval_tab_col(P, k, ind, val);
+ xassert(0 <= len && len <= m);
+ /* perform analysis */
+ for (kase = -1; kase <= +1; kase += 2)
+ { /* kase < 0 means active bound of x[k] is decreasing;
+ kase > 0 means active bound of x[k] is increasing */
+ /* use the primal ratio test to determine some basic variable
+ x[p] which reaches its bound first */
+ piv = glp_prim_rtest(P, len, ind, val, kase, 1e-9);
+ if (piv == 0)
+ { /* nothing limits changing the active bound of x[k] */
+ p = 0;
+ new_x = (kase < 0 ? -DBL_MAX : +DBL_MAX);
+ goto store;
+ }
+ /* basic variable x[p] limits changing the active bound of
+ x[k]; determine its value in the current basis */
+ xassert(1 <= piv && piv <= len);
+ p = ind[piv];
+ if (p <= m)
+ { row = P->row[p];
+ ll = glp_get_row_lb(P, row->i);
+ uu = glp_get_row_ub(P, row->i);
+ stat = row->stat;
+ xx = row->prim;
+ }
+ else
+ { col = P->col[p-m];
+ ll = glp_get_col_lb(P, col->j);
+ uu = glp_get_col_ub(P, col->j);
+ stat = col->stat;
+ xx = col->prim;
+ }
+ xassert(stat == GLP_BS);
+ /* determine delta x[p] = bound of x[p] - value of x[p] */
+ if (kase < 0 && val[piv] > 0.0 ||
+ kase > 0 && val[piv] < 0.0)
+ { /* delta x[p] < 0, so x[p] goes toward its lower bound */
+ xassert(ll != -DBL_MAX);
+ delta = ll - xx;
+ }
+ else
+ { /* delta x[p] > 0, so x[p] goes toward its upper bound */
+ xassert(uu != +DBL_MAX);
+ delta = uu - xx;
+ }
+ /* delta x[p] = alfa[p,k] * delta x[k], so new x[k] = x[k] +
+ delta x[k] = x[k] + delta x[p] / alfa[p,k] is the value of
+ x[k] in the adjacent basis */
+ xassert(val[piv] != 0.0);
+ new_x = x + delta / val[piv];
+store: /* store analysis results */
+ if (kase < 0)
+ { if (value1 != NULL) *value1 = new_x;
+ if (var1 != NULL) *var1 = p;
+ }
+ else
+ { if (value2 != NULL) *value2 = new_x;
+ if (var2 != NULL) *var2 = p;
+ }
+ }
+ /* free working arrays */
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_analyze_coef - analyze objective coefficient at basic variable
+*
+* SYNOPSIS
+*
+* void glp_analyze_coef(glp_prob *P, int k, double *coef1, int *var1,
+* double *value1, double *coef2, int *var2, double *value2);
+*
+* DESCRIPTION
+*
+* The routine glp_analyze_coef analyzes the effect of varying the
+* objective coefficient at specified basic variable.
+*
+* The basic variable is specified by the parameter k, where
+* 1 <= k <= m means auxiliary variable of corresponding row while
+* m+1 <= k <= m+n means structural variable (column).
+*
+* Note that the current basic solution must be optimal, and the basis
+* factorization must exist.
+*
+* Results of the analysis have the following meaning.
+*
+* coef1 is the minimal value of the objective coefficient, at which
+* the basis still remains dual feasible and thus optimal. -DBL_MAX
+* means that the objective coefficient has no lower limit.
+*
+* var1 is the ordinal number of an auxiliary (1 to m) or structural
+* (m+1 to n) non-basic variable, whose reduced cost reaches its zero
+* bound first and thereby limits further decreasing the objective
+* coefficient being analyzed. If coef1 = -DBL_MAX, var1 is set to 0.
+*
+* value1 is value of the basic variable being analyzed in an adjacent
+* basis, which is defined as follows. Let the objective coefficient
+* reaches its minimal value (coef1) and continues decreasing. Then the
+* reduced cost of the limiting non-basic variable (var1) becomes dual
+* infeasible and the current basis becomes non-optimal that forces the
+* limiting non-basic variable to enter the basis replacing there some
+* basic variable that leaves the basis to keep primal feasibility.
+* Should note that on determining the adjacent basis current bounds
+* of the basic variable being analyzed are ignored as if it were free
+* (unbounded) variable, so it cannot leave the basis. It may happen
+* that no dual feasible adjacent basis exists, in which case value1 is
+* set to -DBL_MAX or +DBL_MAX.
+*
+* coef2 is the maximal value of the objective coefficient, at which
+* the basis still remains dual feasible and thus optimal. +DBL_MAX
+* means that the objective coefficient has no upper limit.
+*
+* var2 is the ordinal number of an auxiliary (1 to m) or structural
+* (m+1 to n) non-basic variable, whose reduced cost reaches its zero
+* bound first and thereby limits further increasing the objective
+* coefficient being analyzed. If coef2 = +DBL_MAX, var2 is set to 0.
+*
+* value2 is value of the basic variable being analyzed in an adjacent
+* basis, which is defined exactly in the same way as value1 above with
+* exception that now the objective coefficient is increasing. */
+
+void glp_analyze_coef(glp_prob *P, int k, double *coef1, int *var1,
+ double *value1, double *coef2, int *var2, double *value2)
+{ GLPROW *row; GLPCOL *col;
+ int m, n, type, stat, kase, p, q, dir, clen, cpiv, rlen, rpiv,
+ *cind, *rind;
+ double lb, ub, coef, x, lim_coef, new_x, d, delta, ll, uu, xx,
+ *rval, *cval;
+#if 0 /* 04/IV-2016 */
+ /* sanity checks */
+ if (P == NULL || P->magic != GLP_PROB_MAGIC)
+ xerror("glp_analyze_coef: P = %p; invalid problem object\n",
+ P);
+#endif
+ m = P->m, n = P->n;
+ if (!(P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS))
+ xerror("glp_analyze_coef: optimal basic solution required\n");
+ if (!(m == 0 || P->valid))
+ xerror("glp_analyze_coef: basis factorization required\n");
+ if (!(1 <= k && k <= m+n))
+ xerror("glp_analyze_coef: k = %d; variable number out of range"
+ "\n", k);
+ /* retrieve information about the specified basic variable x[k]
+ whose objective coefficient c[k] is to be analyzed */
+ if (k <= m)
+ { row = P->row[k];
+ type = row->type;
+ lb = row->lb;
+ ub = row->ub;
+ coef = 0.0;
+ stat = row->stat;
+ x = row->prim;
+ }
+ else
+ { col = P->col[k-m];
+ type = col->type;
+ lb = col->lb;
+ ub = col->ub;
+ coef = col->coef;
+ stat = col->stat;
+ x = col->prim;
+ }
+ if (stat != GLP_BS)
+ xerror("glp_analyze_coef: k = %d; non-basic variable not allow"
+ "ed\n", k);
+ /* allocate working arrays */
+ cind = xcalloc(1+m, sizeof(int));
+ cval = xcalloc(1+m, sizeof(double));
+ rind = xcalloc(1+n, sizeof(int));
+ rval = xcalloc(1+n, sizeof(double));
+ /* compute row of the simplex table corresponding to the basic
+ variable x[k] */
+ rlen = glp_eval_tab_row(P, k, rind, rval);
+ xassert(0 <= rlen && rlen <= n);
+ /* perform analysis */
+ for (kase = -1; kase <= +1; kase += 2)
+ { /* kase < 0 means objective coefficient c[k] is decreasing;
+ kase > 0 means objective coefficient c[k] is increasing */
+ /* note that decreasing c[k] is equivalent to increasing dual
+ variable lambda[k] and vice versa; we need to correctly set
+ the dir flag as required by the routine glp_dual_rtest */
+ if (P->dir == GLP_MIN)
+ dir = - kase;
+ else if (P->dir == GLP_MAX)
+ dir = + kase;
+ else
+ xassert(P != P);
+ /* use the dual ratio test to determine non-basic variable
+ x[q] whose reduced cost d[q] reaches zero bound first */
+ rpiv = glp_dual_rtest(P, rlen, rind, rval, dir, 1e-9);
+ if (rpiv == 0)
+ { /* nothing limits changing c[k] */
+ lim_coef = (kase < 0 ? -DBL_MAX : +DBL_MAX);
+ q = 0;
+ /* x[k] keeps its current value */
+ new_x = x;
+ goto store;
+ }
+ /* non-basic variable x[q] limits changing coefficient c[k];
+ determine its status and reduced cost d[k] in the current
+ basis */
+ xassert(1 <= rpiv && rpiv <= rlen);
+ q = rind[rpiv];
+ xassert(1 <= q && q <= m+n);
+ if (q <= m)
+ { row = P->row[q];
+ stat = row->stat;
+ d = row->dual;
+ }
+ else
+ { col = P->col[q-m];
+ stat = col->stat;
+ d = col->dual;
+ }
+ /* note that delta d[q] = new d[q] - d[q] = - d[q], because
+ new d[q] = 0; delta d[q] = alfa[k,q] * delta c[k], so
+ delta c[k] = delta d[q] / alfa[k,q] = - d[q] / alfa[k,q] */
+ xassert(rval[rpiv] != 0.0);
+ delta = - d / rval[rpiv];
+ /* compute new c[k] = c[k] + delta c[k], which is the limiting
+ value of the objective coefficient c[k] */
+ lim_coef = coef + delta;
+ /* let c[k] continue decreasing/increasing that makes d[q]
+ dual infeasible and forces x[q] to enter the basis;
+ to perform the primal ratio test we need to know in which
+ direction x[q] changes on entering the basis; we determine
+ that analyzing the sign of delta d[q] (see above), since
+ d[q] may be close to zero having wrong sign */
+ /* let, for simplicity, the problem is minimization */
+ if (kase < 0 && rval[rpiv] > 0.0 ||
+ kase > 0 && rval[rpiv] < 0.0)
+ { /* delta d[q] < 0, so d[q] being non-negative will become
+ negative, so x[q] will increase */
+ dir = +1;
+ }
+ else
+ { /* delta d[q] > 0, so d[q] being non-positive will become
+ positive, so x[q] will decrease */
+ dir = -1;
+ }
+ /* if the problem is maximization, correct the direction */
+ if (P->dir == GLP_MAX) dir = - dir;
+ /* check that we didn't make a silly mistake */
+ if (dir > 0)
+ xassert(stat == GLP_NL || stat == GLP_NF);
+ else
+ xassert(stat == GLP_NU || stat == GLP_NF);
+ /* compute column of the simplex table corresponding to the
+ non-basic variable x[q] */
+ clen = glp_eval_tab_col(P, q, cind, cval);
+ /* make x[k] temporarily free (unbounded) */
+ if (k <= m)
+ { row = P->row[k];
+ row->type = GLP_FR;
+ row->lb = row->ub = 0.0;
+ }
+ else
+ { col = P->col[k-m];
+ col->type = GLP_FR;
+ col->lb = col->ub = 0.0;
+ }
+ /* use the primal ratio test to determine some basic variable
+ which leaves the basis */
+ cpiv = glp_prim_rtest(P, clen, cind, cval, dir, 1e-9);
+ /* restore original bounds of the basic variable x[k] */
+ if (k <= m)
+ { row = P->row[k];
+ row->type = type;
+ row->lb = lb, row->ub = ub;
+ }
+ else
+ { col = P->col[k-m];
+ col->type = type;
+ col->lb = lb, col->ub = ub;
+ }
+ if (cpiv == 0)
+ { /* non-basic variable x[q] can change unlimitedly */
+ if (dir < 0 && rval[rpiv] > 0.0 ||
+ dir > 0 && rval[rpiv] < 0.0)
+ { /* delta x[k] = alfa[k,q] * delta x[q] < 0 */
+ new_x = -DBL_MAX;
+ }
+ else
+ { /* delta x[k] = alfa[k,q] * delta x[q] > 0 */
+ new_x = +DBL_MAX;
+ }
+ goto store;
+ }
+ /* some basic variable x[p] limits changing non-basic variable
+ x[q] in the adjacent basis */
+ xassert(1 <= cpiv && cpiv <= clen);
+ p = cind[cpiv];
+ xassert(1 <= p && p <= m+n);
+ xassert(p != k);
+ if (p <= m)
+ { row = P->row[p];
+ xassert(row->stat == GLP_BS);
+ ll = glp_get_row_lb(P, row->i);
+ uu = glp_get_row_ub(P, row->i);
+ xx = row->prim;
+ }
+ else
+ { col = P->col[p-m];
+ xassert(col->stat == GLP_BS);
+ ll = glp_get_col_lb(P, col->j);
+ uu = glp_get_col_ub(P, col->j);
+ xx = col->prim;
+ }
+ /* determine delta x[p] = new x[p] - x[p] */
+ if (dir < 0 && cval[cpiv] > 0.0 ||
+ dir > 0 && cval[cpiv] < 0.0)
+ { /* delta x[p] < 0, so x[p] goes toward its lower bound */
+ xassert(ll != -DBL_MAX);
+ delta = ll - xx;
+ }
+ else
+ { /* delta x[p] > 0, so x[p] goes toward its upper bound */
+ xassert(uu != +DBL_MAX);
+ delta = uu - xx;
+ }
+ /* compute new x[k] = x[k] + alfa[k,q] * delta x[q], where
+ delta x[q] = delta x[p] / alfa[p,q] */
+ xassert(cval[cpiv] != 0.0);
+ new_x = x + (rval[rpiv] / cval[cpiv]) * delta;
+store: /* store analysis results */
+ if (kase < 0)
+ { if (coef1 != NULL) *coef1 = lim_coef;
+ if (var1 != NULL) *var1 = q;
+ if (value1 != NULL) *value1 = new_x;
+ }
+ else
+ { if (coef2 != NULL) *coef2 = lim_coef;
+ if (var2 != NULL) *var2 = q;
+ if (value2 != NULL) *value2 = new_x;
+ }
+ }
+ /* free working arrays */
+ xfree(cind);
+ xfree(cval);
+ xfree(rind);
+ xfree(rval);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpapi13.c b/test/monniaux/glpk-4.65/src/draft/glpapi13.c
new file mode 100644
index 00000000..1181b397
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpapi13.c
@@ -0,0 +1,710 @@
+/* glpapi13.c (branch-and-bound interface routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_reason - determine reason for calling the callback routine
+*
+* SYNOPSIS
+*
+* glp_ios_reason(glp_tree *tree);
+*
+* RETURNS
+*
+* The routine glp_ios_reason returns a code, which indicates why the
+* user-defined callback routine is being called. */
+
+int glp_ios_reason(glp_tree *tree)
+{ return
+ tree->reason;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_get_prob - access the problem object
+*
+* SYNOPSIS
+*
+* glp_prob *glp_ios_get_prob(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_get_prob can be called from the user-defined
+* callback routine to access the problem object, which is used by the
+* MIP solver. It is the original problem object passed to the routine
+* glp_intopt if the MIP presolver is not used; otherwise it is an
+* internal problem object built by the presolver. If the current
+* subproblem exists, LP segment of the problem object corresponds to
+* its LP relaxation.
+*
+* RETURNS
+*
+* The routine glp_ios_get_prob returns a pointer to the problem object
+* used by the MIP solver. */
+
+glp_prob *glp_ios_get_prob(glp_tree *tree)
+{ return
+ tree->mip;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_tree_size - determine size of the branch-and-bound tree
+*
+* SYNOPSIS
+*
+* void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt,
+* int *t_cnt);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_tree_size stores the following three counts which
+* characterize the current size of the branch-and-bound tree:
+*
+* a_cnt is the current number of active nodes, i.e. the current size of
+* the active list;
+*
+* n_cnt is the current number of all (active and inactive) nodes;
+*
+* t_cnt is the total number of nodes including those which have been
+* already removed from the tree. This count is increased whenever
+* a new node appears in the tree and never decreased.
+*
+* If some of the parameters a_cnt, n_cnt, t_cnt is a null pointer, the
+* corresponding count is not stored. */
+
+void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt,
+ int *t_cnt)
+{ if (a_cnt != NULL) *a_cnt = tree->a_cnt;
+ if (n_cnt != NULL) *n_cnt = tree->n_cnt;
+ if (t_cnt != NULL) *t_cnt = tree->t_cnt;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_curr_node - determine current active subproblem
+*
+* SYNOPSIS
+*
+* int glp_ios_curr_node(glp_tree *tree);
+*
+* RETURNS
+*
+* The routine glp_ios_curr_node returns the reference number of the
+* current active subproblem. However, if the current subproblem does
+* not exist, the routine returns zero. */
+
+int glp_ios_curr_node(glp_tree *tree)
+{ IOSNPD *node;
+ /* obtain pointer to the current subproblem */
+ node = tree->curr;
+ /* return its reference number */
+ return node == NULL ? 0 : node->p;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_next_node - determine next active subproblem
+*
+* SYNOPSIS
+*
+* int glp_ios_next_node(glp_tree *tree, int p);
+*
+* RETURNS
+*
+* If the parameter p is zero, the routine glp_ios_next_node returns
+* the reference number of the first active subproblem. However, if the
+* tree is empty, zero is returned.
+*
+* If the parameter p is not zero, it must specify the reference number
+* of some active subproblem, in which case the routine returns the
+* reference number of the next active subproblem. However, if there is
+* no next active subproblem in the list, zero is returned.
+*
+* All subproblems in the active list are ordered chronologically, i.e.
+* subproblem A precedes subproblem B if A was created before B. */
+
+int glp_ios_next_node(glp_tree *tree, int p)
+{ IOSNPD *node;
+ if (p == 0)
+ { /* obtain pointer to the first active subproblem */
+ node = tree->head;
+ }
+ else
+ { /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_next_node: p = %d; invalid subproblem refer"
+ "ence number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* the specified subproblem must be active */
+ if (node->count != 0)
+ xerror("glp_ios_next_node: p = %d; subproblem not in the ac"
+ "tive list\n", p);
+ /* obtain pointer to the next active subproblem */
+ node = node->next;
+ }
+ /* return the reference number */
+ return node == NULL ? 0 : node->p;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_prev_node - determine previous active subproblem
+*
+* SYNOPSIS
+*
+* int glp_ios_prev_node(glp_tree *tree, int p);
+*
+* RETURNS
+*
+* If the parameter p is zero, the routine glp_ios_prev_node returns
+* the reference number of the last active subproblem. However, if the
+* tree is empty, zero is returned.
+*
+* If the parameter p is not zero, it must specify the reference number
+* of some active subproblem, in which case the routine returns the
+* reference number of the previous active subproblem. However, if there
+* is no previous active subproblem in the list, zero is returned.
+*
+* All subproblems in the active list are ordered chronologically, i.e.
+* subproblem A precedes subproblem B if A was created before B. */
+
+int glp_ios_prev_node(glp_tree *tree, int p)
+{ IOSNPD *node;
+ if (p == 0)
+ { /* obtain pointer to the last active subproblem */
+ node = tree->tail;
+ }
+ else
+ { /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_prev_node: p = %d; invalid subproblem refer"
+ "ence number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* the specified subproblem must be active */
+ if (node->count != 0)
+ xerror("glp_ios_prev_node: p = %d; subproblem not in the ac"
+ "tive list\n", p);
+ /* obtain pointer to the previous active subproblem */
+ node = node->prev;
+ }
+ /* return the reference number */
+ return node == NULL ? 0 : node->p;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_up_node - determine parent subproblem
+*
+* SYNOPSIS
+*
+* int glp_ios_up_node(glp_tree *tree, int p);
+*
+* RETURNS
+*
+* The parameter p must specify the reference number of some (active or
+* inactive) subproblem, in which case the routine iet_get_up_node
+* returns the reference number of its parent subproblem. However, if
+* the specified subproblem is the root of the tree and, therefore, has
+* no parent, the routine returns zero. */
+
+int glp_ios_up_node(glp_tree *tree, int p)
+{ IOSNPD *node;
+ /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_up_node: p = %d; invalid subproblem reference "
+ "number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* obtain pointer to the parent subproblem */
+ node = node->up;
+ /* return the reference number */
+ return node == NULL ? 0 : node->p;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_node_level - determine subproblem level
+*
+* SYNOPSIS
+*
+* int glp_ios_node_level(glp_tree *tree, int p);
+*
+* RETURNS
+*
+* The routine glp_ios_node_level returns the level of the subproblem,
+* whose reference number is p, in the branch-and-bound tree. (The root
+* subproblem has level 0, and the level of any other subproblem is the
+* level of its parent plus one.) */
+
+int glp_ios_node_level(glp_tree *tree, int p)
+{ IOSNPD *node;
+ /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen"
+ "ce number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* return the node level */
+ return node->level;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_node_bound - determine subproblem local bound
+*
+* SYNOPSIS
+*
+* double glp_ios_node_bound(glp_tree *tree, int p);
+*
+* RETURNS
+*
+* The routine glp_ios_node_bound returns the local bound for (active or
+* inactive) subproblem, whose reference number is p.
+*
+* COMMENTS
+*
+* The local bound for subproblem p is an lower (minimization) or upper
+* (maximization) bound for integer optimal solution to this subproblem
+* (not to the original problem). This bound is local in the sense that
+* only subproblems in the subtree rooted at node p cannot have better
+* integer feasible solutions.
+*
+* On creating a subproblem (due to the branching step) its local bound
+* is inherited from its parent and then may get only stronger (never
+* weaker). For the root subproblem its local bound is initially set to
+* -DBL_MAX (minimization) or +DBL_MAX (maximization) and then improved
+* as the root LP relaxation has been solved.
+*
+* Note that the local bound is not necessarily the optimal objective
+* value to corresponding LP relaxation; it may be stronger. */
+
+double glp_ios_node_bound(glp_tree *tree, int p)
+{ IOSNPD *node;
+ /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_node_bound: p = %d; invalid subproblem referen"
+ "ce number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* return the node local bound */
+ return node->bound;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_best_node - find active subproblem with best local bound
+*
+* SYNOPSIS
+*
+* int glp_ios_best_node(glp_tree *tree);
+*
+* RETURNS
+*
+* The routine glp_ios_best_node returns the reference number of the
+* active subproblem, whose local bound is best (i.e. smallest in case
+* of minimization or largest in case of maximization). However, if the
+* tree is empty, the routine returns zero.
+*
+* COMMENTS
+*
+* The best local bound is an lower (minimization) or upper
+* (maximization) bound for integer optimal solution to the original
+* MIP problem. */
+
+int glp_ios_best_node(glp_tree *tree)
+{ return
+ ios_best_node(tree);
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_mip_gap - compute relative MIP gap
+*
+* SYNOPSIS
+*
+* double glp_ios_mip_gap(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_mip_gap computes the relative MIP gap with the
+* following formula:
+*
+* gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON),
+*
+* where best_mip is the best integer feasible solution found so far,
+* best_bnd is the best (global) bound. If no integer feasible solution
+* has been found yet, gap is set to DBL_MAX.
+*
+* RETURNS
+*
+* The routine glp_ios_mip_gap returns the relative MIP gap. */
+
+double glp_ios_mip_gap(glp_tree *tree)
+{ return
+ ios_relative_gap(tree);
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_node_data - access subproblem application-specific data
+*
+* SYNOPSIS
+*
+* void *glp_ios_node_data(glp_tree *tree, int p);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_node_data allows the application accessing a
+* memory block allocated for the subproblem (which may be active or
+* inactive), whose reference number is p.
+*
+* The size of the block is defined by the control parameter cb_size
+* passed to the routine glp_intopt. The block is initialized by binary
+* zeros on creating corresponding subproblem, and its contents is kept
+* until the subproblem will be removed from the tree.
+*
+* The application may use these memory blocks to store specific data
+* for each subproblem.
+*
+* RETURNS
+*
+* The routine glp_ios_node_data returns a pointer to the memory block
+* for the specified subproblem. Note that if cb_size = 0, the routine
+* returns a null pointer. */
+
+void *glp_ios_node_data(glp_tree *tree, int p)
+{ IOSNPD *node;
+ /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen"
+ "ce number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* return pointer to the application-specific data */
+ return node->data;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_row_attr - retrieve additional row attributes
+*
+* SYNOPSIS
+*
+* void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_row_attr retrieves additional attributes of row
+* i and stores them in the structure glp_attr. */
+
+void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr)
+{ GLPROW *row;
+ if (!(1 <= i && i <= tree->mip->m))
+ xerror("glp_ios_row_attr: i = %d; row number out of range\n",
+ i);
+ row = tree->mip->row[i];
+ attr->level = row->level;
+ attr->origin = row->origin;
+ attr->klass = row->klass;
+ return;
+}
+
+/**********************************************************************/
+
+int glp_ios_pool_size(glp_tree *tree)
+{ /* determine current size of the cut pool */
+ if (tree->reason != GLP_ICUTGEN)
+ xerror("glp_ios_pool_size: operation not allowed\n");
+ xassert(tree->local != NULL);
+#ifdef NEW_LOCAL /* 02/II-2018 */
+ return tree->local->m;
+#else
+ return tree->local->size;
+#endif
+}
+
+/**********************************************************************/
+
+int glp_ios_add_row(glp_tree *tree,
+ const char *name, int klass, int flags, int len, const int ind[],
+ const double val[], int type, double rhs)
+{ /* add row (constraint) to the cut pool */
+ int num;
+ if (tree->reason != GLP_ICUTGEN)
+ xerror("glp_ios_add_row: operation not allowed\n");
+ xassert(tree->local != NULL);
+ num = ios_add_row(tree, tree->local, name, klass, flags, len,
+ ind, val, type, rhs);
+ return num;
+}
+
+/**********************************************************************/
+
+void glp_ios_del_row(glp_tree *tree, int i)
+{ /* remove row (constraint) from the cut pool */
+ if (tree->reason != GLP_ICUTGEN)
+ xerror("glp_ios_del_row: operation not allowed\n");
+ ios_del_row(tree, tree->local, i);
+ return;
+}
+
+/**********************************************************************/
+
+void glp_ios_clear_pool(glp_tree *tree)
+{ /* remove all rows (constraints) from the cut pool */
+ if (tree->reason != GLP_ICUTGEN)
+ xerror("glp_ios_clear_pool: operation not allowed\n");
+ ios_clear_pool(tree, tree->local);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_can_branch - check if can branch upon specified variable
+*
+* SYNOPSIS
+*
+* int glp_ios_can_branch(glp_tree *tree, int j);
+*
+* RETURNS
+*
+* If j-th variable (column) can be used to branch upon, the routine
+* glp_ios_can_branch returns non-zero, otherwise zero. */
+
+int glp_ios_can_branch(glp_tree *tree, int j)
+{ if (!(1 <= j && j <= tree->mip->n))
+ xerror("glp_ios_can_branch: j = %d; column number out of range"
+ "\n", j);
+ return tree->non_int[j];
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_branch_upon - choose variable to branch upon
+*
+* SYNOPSIS
+*
+* void glp_ios_branch_upon(glp_tree *tree, int j, int sel);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_branch_upon can be called from the user-defined
+* callback routine in response to the reason GLP_IBRANCH to choose a
+* branching variable, whose ordinal number is j. Should note that only
+* variables, for which the routine glp_ios_can_branch returns non-zero,
+* can be used to branch upon.
+*
+* The parameter sel is a flag that indicates which branch (subproblem)
+* should be selected next to continue the search:
+*
+* GLP_DN_BRNCH - select down-branch;
+* GLP_UP_BRNCH - select up-branch;
+* GLP_NO_BRNCH - use general selection technique. */
+
+void glp_ios_branch_upon(glp_tree *tree, int j, int sel)
+{ if (!(1 <= j && j <= tree->mip->n))
+ xerror("glp_ios_branch_upon: j = %d; column number out of rang"
+ "e\n", j);
+ if (!(sel == GLP_DN_BRNCH || sel == GLP_UP_BRNCH ||
+ sel == GLP_NO_BRNCH))
+ xerror("glp_ios_branch_upon: sel = %d: invalid branch selectio"
+ "n flag\n", sel);
+ if (!(tree->non_int[j]))
+ xerror("glp_ios_branch_upon: j = %d; variable cannot be used t"
+ "o branch upon\n", j);
+ if (tree->br_var != 0)
+ xerror("glp_ios_branch_upon: branching variable already chosen"
+ "\n");
+ tree->br_var = j;
+ tree->br_sel = sel;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_select_node - select subproblem to continue the search
+*
+* SYNOPSIS
+*
+* void glp_ios_select_node(glp_tree *tree, int p);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_select_node can be called from the user-defined
+* callback routine in response to the reason GLP_ISELECT to select an
+* active subproblem, whose reference number is p. The search will be
+* continued from the subproblem selected. */
+
+void glp_ios_select_node(glp_tree *tree, int p)
+{ IOSNPD *node;
+ /* obtain pointer to the specified subproblem */
+ if (!(1 <= p && p <= tree->nslots))
+err: xerror("glp_ios_select_node: p = %d; invalid subproblem refere"
+ "nce number\n", p);
+ node = tree->slot[p].node;
+ if (node == NULL) goto err;
+ /* the specified subproblem must be active */
+ if (node->count != 0)
+ xerror("glp_ios_select_node: p = %d; subproblem not in the act"
+ "ive list\n", p);
+ /* no subproblem must be selected yet */
+ if (tree->next_p != 0)
+ xerror("glp_ios_select_node: subproblem already selected\n");
+ /* select the specified subproblem to continue the search */
+ tree->next_p = p;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_heur_sol - provide solution found by heuristic
+*
+* SYNOPSIS
+*
+* int glp_ios_heur_sol(glp_tree *tree, const double x[]);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_heur_sol can be called from the user-defined
+* callback routine in response to the reason GLP_IHEUR to provide an
+* integer feasible solution found by a primal heuristic.
+*
+* Primal values of *all* variables (columns) found by the heuristic
+* should be placed in locations x[1], ..., x[n], where n is the number
+* of columns in the original problem object. Note that the routine
+* glp_ios_heur_sol *does not* check primal feasibility of the solution
+* provided.
+*
+* Using the solution passed in the array x the routine computes value
+* of the objective function. If the objective value is better than the
+* best known integer feasible solution, the routine computes values of
+* auxiliary variables (rows) and stores all solution components in the
+* problem object.
+*
+* RETURNS
+*
+* If the provided solution is accepted, the routine glp_ios_heur_sol
+* returns zero. Otherwise, if the provided solution is rejected, the
+* routine returns non-zero. */
+
+int glp_ios_heur_sol(glp_tree *tree, const double x[])
+{ glp_prob *mip = tree->mip;
+ int m = tree->orig_m;
+ int n = tree->n;
+ int i, j;
+ double obj;
+ xassert(mip->m >= m);
+ xassert(mip->n == n);
+ /* check values of integer variables and compute value of the
+ objective function */
+ obj = mip->c0;
+ for (j = 1; j <= n; j++)
+ { GLPCOL *col = mip->col[j];
+ if (col->kind == GLP_IV)
+ { /* provided value must be integral */
+ if (x[j] != floor(x[j])) return 1;
+ }
+ obj += col->coef * x[j];
+ }
+ /* check if the provided solution is better than the best known
+ integer feasible solution */
+ if (mip->mip_stat == GLP_FEAS)
+ { switch (mip->dir)
+ { case GLP_MIN:
+ if (obj >= tree->mip->mip_obj) return 1;
+ break;
+ case GLP_MAX:
+ if (obj <= tree->mip->mip_obj) return 1;
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ }
+ /* it is better; store it in the problem object */
+ if (tree->parm->msg_lev >= GLP_MSG_ON)
+ xprintf("Solution found by heuristic: %.12g\n", obj);
+ mip->mip_stat = GLP_FEAS;
+ mip->mip_obj = obj;
+ for (j = 1; j <= n; j++)
+ mip->col[j]->mipx = x[j];
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = mip->row[i];
+ GLPAIJ *aij;
+ row->mipx = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ row->mipx += aij->val * aij->col->mipx;
+ }
+#if 1 /* 11/VII-2013 */
+ ios_process_sol(tree);
+#endif
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ios_terminate - terminate the solution process.
+*
+* SYNOPSIS
+*
+* void glp_ios_terminate(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine glp_ios_terminate sets a flag indicating that the MIP
+* solver should prematurely terminate the search. */
+
+void glp_ios_terminate(glp_tree *tree)
+{ if (tree->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("The search is prematurely terminated due to applicati"
+ "on request\n");
+ tree->stop = 1;
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glphbm.c b/test/monniaux/glpk-4.65/src/draft/glphbm.c
new file mode 100644
index 00000000..8b33c172
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glphbm.c
@@ -0,0 +1,533 @@
+/* glphbm.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glphbm.h"
+#include "misc.h"
+
+/***********************************************************************
+* NAME
+*
+* hbm_read_mat - read sparse matrix in Harwell-Boeing format
+*
+* SYNOPSIS
+*
+* #include "glphbm.h"
+* HBM *hbm_read_mat(const char *fname);
+*
+* DESCRIPTION
+*
+* The routine hbm_read_mat reads a sparse matrix in the Harwell-Boeing
+* format from a text file whose name is the character string fname.
+*
+* Detailed description of the Harwell-Boeing format recognised by this
+* routine is given in the following report:
+*
+* I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the Harwell-Boeing
+* Sparse Matrix Collection (Release I), TR/PA/92/86, October 1992.
+*
+* RETURNS
+*
+* If no error occured, the routine hbm_read_mat returns a pointer to
+* a data structure containing the matrix. In case of error the routine
+* prints an appropriate error message and returns NULL. */
+
+struct dsa
+{ /* working area used by routine hbm_read_mat */
+ const char *fname;
+ /* name of input text file */
+ FILE *fp;
+ /* stream assigned to input text file */
+ int seqn;
+ /* card sequential number */
+ char card[80+1];
+ /* card image buffer */
+ int fmt_p;
+ /* scale factor */
+ int fmt_k;
+ /* iterator */
+ int fmt_f;
+ /* format code */
+ int fmt_w;
+ /* field width */
+ int fmt_d;
+ /* number of decimal places after point */
+};
+
+/***********************************************************************
+* read_card - read next data card
+*
+* This routine reads the next 80-column card from the input text file
+* and stores its image into the character string card. If the card was
+* read successfully, the routine returns zero, otherwise non-zero. */
+
+#if 1 /* 11/III-2012 */
+static int read_card(struct dsa *dsa)
+{ int c, len = 0;
+ char buf[255+1];
+ dsa->seqn++;
+ for (;;)
+ { c = fgetc(dsa->fp);
+ if (c == EOF)
+ { if (ferror(dsa->fp))
+ xprintf("%s:%d: read error\n",
+ dsa->fname, dsa->seqn);
+ else
+ xprintf("%s:%d: unexpected end-of-file\n",
+ dsa->fname, dsa->seqn);
+ return 1;
+ }
+ else if (c == '\r')
+ /* nop */;
+ else if (c == '\n')
+ break;
+ else if (iscntrl(c))
+ { xprintf("%s:%d: invalid control character\n",
+ dsa->fname, dsa->seqn, c);
+ return 1;
+ }
+ else
+ { if (len == sizeof(buf)-1)
+ goto err;
+ buf[len++] = (char)c;
+ }
+ }
+ /* remove trailing spaces */
+ while (len > 80 && buf[len-1] == ' ')
+ len--;
+ buf[len] = '\0';
+ /* line should not be longer than 80 chars */
+ if (len > 80)
+err: { xerror("%s:%d: card image too long\n",
+ dsa->fname, dsa->seqn);
+ return 1;
+ }
+ /* padd by spaces to 80-column card image */
+ strcpy(dsa->card, buf);
+ memset(&dsa->card[len], ' ', 80 - len);
+ dsa->card[80] = '\0';
+ return 0;
+}
+#endif
+
+/***********************************************************************
+* scan_int - scan integer value from the current card
+*
+* This routine scans an integer value from the current card, where fld
+* is the name of the field, pos is the position of the field, width is
+* the width of the field, val points to a location to which the scanned
+* value should be stored. If the value was scanned successfully, the
+* routine returns zero, otherwise non-zero. */
+
+static int scan_int(struct dsa *dsa, char *fld, int pos, int width,
+ int *val)
+{ char str[80+1];
+ xassert(1 <= width && width <= 80);
+ memcpy(str, dsa->card + pos, width), str[width] = '\0';
+ if (str2int(strspx(str), val))
+ { xprintf("%s:%d: field '%s' contains invalid value '%s'\n",
+ dsa->fname, dsa->seqn, fld, str);
+ return 1;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* parse_fmt - parse Fortran format specification
+*
+* This routine parses the Fortran format specification represented as
+* character string which fmt points to and stores format elements into
+* appropriate static locations. Should note that not all valid Fortran
+* format specifications may be recognised. If the format specification
+* was recognised, the routine returns zero, otherwise non-zero. */
+
+static int parse_fmt(struct dsa *dsa, char *fmt)
+{ int k, s, val;
+ char str[80+1];
+ /* first character should be left parenthesis */
+ if (fmt[0] != '(')
+fail: { xprintf("hbm_read_mat: format '%s' not recognised\n", fmt);
+ return 1;
+ }
+ k = 1;
+ /* optional scale factor */
+ dsa->fmt_p = 0;
+ if (isdigit((unsigned char)fmt[k]))
+ { s = 0;
+ while (isdigit((unsigned char)fmt[k]))
+ { if (s == 80) goto fail;
+ str[s++] = fmt[k++];
+ }
+ str[s] = '\0';
+ if (str2int(str, &val)) goto fail;
+ if (toupper((unsigned char)fmt[k]) != 'P') goto iter;
+ dsa->fmt_p = val, k++;
+ if (!(0 <= dsa->fmt_p && dsa->fmt_p <= 255)) goto fail;
+ /* optional comma may follow scale factor */
+ if (fmt[k] == ',') k++;
+ }
+ /* optional iterator */
+ dsa->fmt_k = 1;
+ if (isdigit((unsigned char)fmt[k]))
+ { s = 0;
+ while (isdigit((unsigned char)fmt[k]))
+ { if (s == 80) goto fail;
+ str[s++] = fmt[k++];
+ }
+ str[s] = '\0';
+ if (str2int(str, &val)) goto fail;
+iter: dsa->fmt_k = val;
+ if (!(1 <= dsa->fmt_k && dsa->fmt_k <= 255)) goto fail;
+ }
+ /* format code */
+ dsa->fmt_f = toupper((unsigned char)fmt[k++]);
+ if (!(dsa->fmt_f == 'D' || dsa->fmt_f == 'E' ||
+ dsa->fmt_f == 'F' || dsa->fmt_f == 'G' ||
+ dsa->fmt_f == 'I')) goto fail;
+ /* field width */
+ if (!isdigit((unsigned char)fmt[k])) goto fail;
+ s = 0;
+ while (isdigit((unsigned char)fmt[k]))
+ { if (s == 80) goto fail;
+ str[s++] = fmt[k++];
+ }
+ str[s] = '\0';
+ if (str2int(str, &dsa->fmt_w)) goto fail;
+ if (!(1 <= dsa->fmt_w && dsa->fmt_w <= 255)) goto fail;
+ /* optional number of decimal places after point */
+ dsa->fmt_d = 0;
+ if (fmt[k] == '.')
+ { k++;
+ if (!isdigit((unsigned char)fmt[k])) goto fail;
+ s = 0;
+ while (isdigit((unsigned char)fmt[k]))
+ { if (s == 80) goto fail;
+ str[s++] = fmt[k++];
+ }
+ str[s] = '\0';
+ if (str2int(str, &dsa->fmt_d)) goto fail;
+ if (!(0 <= dsa->fmt_d && dsa->fmt_d <= 255)) goto fail;
+ }
+ /* last character should be right parenthesis */
+ if (!(fmt[k] == ')' && fmt[k+1] == '\0')) goto fail;
+ return 0;
+}
+
+/***********************************************************************
+* read_int_array - read array of integer type
+*
+* This routine reads an integer array from the input text file, where
+* name is array name, fmt is Fortran format specification that controls
+* reading, n is number of array elements, val is array of integer type.
+* If the array was read successful, the routine returns zero, otherwise
+* non-zero. */
+
+static int read_int_array(struct dsa *dsa, char *name, char *fmt,
+ int n, int val[])
+{ int k, pos;
+ char str[80+1];
+ if (parse_fmt(dsa, fmt)) return 1;
+ if (!(dsa->fmt_f == 'I' && dsa->fmt_w <= 80 &&
+ dsa->fmt_k * dsa->fmt_w <= 80))
+ { xprintf(
+ "%s:%d: can't read array '%s' - invalid format '%s'\n",
+ dsa->fname, dsa->seqn, name, fmt);
+ return 1;
+ }
+ for (k = 1, pos = INT_MAX; k <= n; k++, pos++)
+ { if (pos >= dsa->fmt_k)
+ { if (read_card(dsa)) return 1;
+ pos = 0;
+ }
+ memcpy(str, dsa->card + dsa->fmt_w * pos, dsa->fmt_w);
+ str[dsa->fmt_w] = '\0';
+ strspx(str);
+ if (str2int(str, &val[k]))
+ { xprintf(
+ "%s:%d: can't read array '%s' - invalid value '%s'\n",
+ dsa->fname, dsa->seqn, name, str);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* read_real_array - read array of real type
+*
+* This routine reads a real array from the input text file, where name
+* is array name, fmt is Fortran format specification that controls
+* reading, n is number of array elements, val is array of real type.
+* If the array was read successful, the routine returns zero, otherwise
+* non-zero. */
+
+static int read_real_array(struct dsa *dsa, char *name, char *fmt,
+ int n, double val[])
+{ int k, pos;
+ char str[80+1], *ptr;
+ if (parse_fmt(dsa, fmt)) return 1;
+ if (!(dsa->fmt_f != 'I' && dsa->fmt_w <= 80 &&
+ dsa->fmt_k * dsa->fmt_w <= 80))
+ { xprintf(
+ "%s:%d: can't read array '%s' - invalid format '%s'\n",
+ dsa->fname, dsa->seqn, name, fmt);
+ return 1;
+ }
+ for (k = 1, pos = INT_MAX; k <= n; k++, pos++)
+ { if (pos >= dsa->fmt_k)
+ { if (read_card(dsa)) return 1;
+ pos = 0;
+ }
+ memcpy(str, dsa->card + dsa->fmt_w * pos, dsa->fmt_w);
+ str[dsa->fmt_w] = '\0';
+ strspx(str);
+ if (strchr(str, '.') == NULL && strcmp(str, "0"))
+ { xprintf("%s(%d): can't read array '%s' - value '%s' has no "
+ "decimal point\n", dsa->fname, dsa->seqn, name, str);
+ return 1;
+ }
+ /* sometimes lower case letters appear */
+ for (ptr = str; *ptr; ptr++)
+ *ptr = (char)toupper((unsigned char)*ptr);
+ ptr = strchr(str, 'D');
+ if (ptr != NULL) *ptr = 'E';
+ /* value may appear with decimal exponent but without letters
+ E or D (for example, -123.456-012), so missing letter should
+ be inserted */
+ ptr = strchr(str+1, '+');
+ if (ptr == NULL) ptr = strchr(str+1, '-');
+ if (ptr != NULL && *(ptr-1) != 'E')
+ { xassert(strlen(str) < 80);
+ memmove(ptr+1, ptr, strlen(ptr)+1);
+ *ptr = 'E';
+ }
+ if (str2num(str, &val[k]))
+ { xprintf(
+ "%s:%d: can't read array '%s' - invalid value '%s'\n",
+ dsa->fname, dsa->seqn, name, str);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+HBM *hbm_read_mat(const char *fname)
+{ struct dsa _dsa, *dsa = &_dsa;
+ HBM *hbm = NULL;
+ dsa->fname = fname;
+ xprintf("hbm_read_mat: reading matrix from '%s'...\n",
+ dsa->fname);
+ dsa->fp = fopen(dsa->fname, "r");
+ if (dsa->fp == NULL)
+ { xprintf("hbm_read_mat: unable to open '%s' - %s\n",
+#if 0 /* 29/I-2017 */
+ dsa->fname, strerror(errno));
+#else
+ dsa->fname, xstrerr(errno));
+#endif
+ goto fail;
+ }
+ dsa->seqn = 0;
+ hbm = xmalloc(sizeof(HBM));
+ memset(hbm, 0, sizeof(HBM));
+ /* read the first heading card */
+ if (read_card(dsa)) goto fail;
+ memcpy(hbm->title, dsa->card, 72), hbm->title[72] = '\0';
+ strtrim(hbm->title);
+ xprintf("%s\n", hbm->title);
+ memcpy(hbm->key, dsa->card+72, 8), hbm->key[8] = '\0';
+ strspx(hbm->key);
+ xprintf("key = %s\n", hbm->key);
+ /* read the second heading card */
+ if (read_card(dsa)) goto fail;
+ if (scan_int(dsa, "totcrd", 0, 14, &hbm->totcrd)) goto fail;
+ if (scan_int(dsa, "ptrcrd", 14, 14, &hbm->ptrcrd)) goto fail;
+ if (scan_int(dsa, "indcrd", 28, 14, &hbm->indcrd)) goto fail;
+ if (scan_int(dsa, "valcrd", 42, 14, &hbm->valcrd)) goto fail;
+ if (scan_int(dsa, "rhscrd", 56, 14, &hbm->rhscrd)) goto fail;
+ xprintf("totcrd = %d; ptrcrd = %d; indcrd = %d; valcrd = %d; rhsc"
+ "rd = %d\n", hbm->totcrd, hbm->ptrcrd, hbm->indcrd,
+ hbm->valcrd, hbm->rhscrd);
+ /* read the third heading card */
+ if (read_card(dsa)) goto fail;
+ memcpy(hbm->mxtype, dsa->card, 3), hbm->mxtype[3] = '\0';
+ if (strchr("RCP", hbm->mxtype[0]) == NULL ||
+ strchr("SUHZR", hbm->mxtype[1]) == NULL ||
+ strchr("AE", hbm->mxtype[2]) == NULL)
+ { xprintf("%s:%d: matrix type '%s' not recognised\n",
+ dsa->fname, dsa->seqn, hbm->mxtype);
+ goto fail;
+ }
+ if (scan_int(dsa, "nrow", 14, 14, &hbm->nrow)) goto fail;
+ if (scan_int(dsa, "ncol", 28, 14, &hbm->ncol)) goto fail;
+ if (scan_int(dsa, "nnzero", 42, 14, &hbm->nnzero)) goto fail;
+ if (scan_int(dsa, "neltvl", 56, 14, &hbm->neltvl)) goto fail;
+ xprintf("mxtype = %s; nrow = %d; ncol = %d; nnzero = %d; neltvl ="
+ " %d\n", hbm->mxtype, hbm->nrow, hbm->ncol, hbm->nnzero,
+ hbm->neltvl);
+ /* read the fourth heading card */
+ if (read_card(dsa)) goto fail;
+ memcpy(hbm->ptrfmt, dsa->card, 16), hbm->ptrfmt[16] = '\0';
+ strspx(hbm->ptrfmt);
+ memcpy(hbm->indfmt, dsa->card+16, 16), hbm->indfmt[16] = '\0';
+ strspx(hbm->indfmt);
+ memcpy(hbm->valfmt, dsa->card+32, 20), hbm->valfmt[20] = '\0';
+ strspx(hbm->valfmt);
+ memcpy(hbm->rhsfmt, dsa->card+52, 20), hbm->rhsfmt[20] = '\0';
+ strspx(hbm->rhsfmt);
+ xprintf("ptrfmt = %s; indfmt = %s; valfmt = %s; rhsfmt = %s\n",
+ hbm->ptrfmt, hbm->indfmt, hbm->valfmt, hbm->rhsfmt);
+ /* read the fifth heading card (optional) */
+ if (hbm->rhscrd <= 0)
+ { strcpy(hbm->rhstyp, "???");
+ hbm->nrhs = 0;
+ hbm->nrhsix = 0;
+ }
+ else
+ { if (read_card(dsa)) goto fail;
+ memcpy(hbm->rhstyp, dsa->card, 3), hbm->rhstyp[3] = '\0';
+ if (scan_int(dsa, "nrhs", 14, 14, &hbm->nrhs)) goto fail;
+ if (scan_int(dsa, "nrhsix", 28, 14, &hbm->nrhsix)) goto fail;
+ xprintf("rhstyp = '%s'; nrhs = %d; nrhsix = %d\n",
+ hbm->rhstyp, hbm->nrhs, hbm->nrhsix);
+ }
+ /* read matrix structure */
+ hbm->colptr = xcalloc(1+hbm->ncol+1, sizeof(int));
+ if (read_int_array(dsa, "colptr", hbm->ptrfmt, hbm->ncol+1,
+ hbm->colptr)) goto fail;
+ hbm->rowind = xcalloc(1+hbm->nnzero, sizeof(int));
+ if (read_int_array(dsa, "rowind", hbm->indfmt, hbm->nnzero,
+ hbm->rowind)) goto fail;
+ /* read matrix values */
+ if (hbm->valcrd <= 0) goto done;
+ if (hbm->mxtype[2] == 'A')
+ { /* assembled matrix */
+ hbm->values = xcalloc(1+hbm->nnzero, sizeof(double));
+ if (read_real_array(dsa, "values", hbm->valfmt, hbm->nnzero,
+ hbm->values)) goto fail;
+ }
+ else
+ { /* elemental (unassembled) matrix */
+ hbm->values = xcalloc(1+hbm->neltvl, sizeof(double));
+ if (read_real_array(dsa, "values", hbm->valfmt, hbm->neltvl,
+ hbm->values)) goto fail;
+ }
+ /* read right-hand sides */
+ if (hbm->nrhs <= 0) goto done;
+ if (hbm->rhstyp[0] == 'F')
+ { /* dense format */
+ hbm->nrhsvl = hbm->nrow * hbm->nrhs;
+ hbm->rhsval = xcalloc(1+hbm->nrhsvl, sizeof(double));
+ if (read_real_array(dsa, "rhsval", hbm->rhsfmt, hbm->nrhsvl,
+ hbm->rhsval)) goto fail;
+ }
+ else if (hbm->rhstyp[0] == 'M' && hbm->mxtype[2] == 'A')
+ { /* sparse format */
+ /* read pointers */
+ hbm->rhsptr = xcalloc(1+hbm->nrhs+1, sizeof(int));
+ if (read_int_array(dsa, "rhsptr", hbm->ptrfmt, hbm->nrhs+1,
+ hbm->rhsptr)) goto fail;
+ /* read sparsity pattern */
+ hbm->rhsind = xcalloc(1+hbm->nrhsix, sizeof(int));
+ if (read_int_array(dsa, "rhsind", hbm->indfmt, hbm->nrhsix,
+ hbm->rhsind)) goto fail;
+ /* read values */
+ hbm->rhsval = xcalloc(1+hbm->nrhsix, sizeof(double));
+ if (read_real_array(dsa, "rhsval", hbm->rhsfmt, hbm->nrhsix,
+ hbm->rhsval)) goto fail;
+ }
+ else if (hbm->rhstyp[0] == 'M' && hbm->mxtype[2] == 'E')
+ { /* elemental format */
+ hbm->rhsval = xcalloc(1+hbm->nrhsvl, sizeof(double));
+ if (read_real_array(dsa, "rhsval", hbm->rhsfmt, hbm->nrhsvl,
+ hbm->rhsval)) goto fail;
+ }
+ else
+ { xprintf("%s:%d: right-hand side type '%c' not recognised\n",
+ dsa->fname, dsa->seqn, hbm->rhstyp[0]);
+ goto fail;
+ }
+ /* read starting guesses */
+ if (hbm->rhstyp[1] == 'G')
+ { hbm->nguess = hbm->nrow * hbm->nrhs;
+ hbm->sguess = xcalloc(1+hbm->nguess, sizeof(double));
+ if (read_real_array(dsa, "sguess", hbm->rhsfmt, hbm->nguess,
+ hbm->sguess)) goto fail;
+ }
+ /* read solution vectors */
+ if (hbm->rhstyp[2] == 'X')
+ { hbm->nexact = hbm->nrow * hbm->nrhs;
+ hbm->xexact = xcalloc(1+hbm->nexact, sizeof(double));
+ if (read_real_array(dsa, "xexact", hbm->rhsfmt, hbm->nexact,
+ hbm->xexact)) goto fail;
+ }
+done: /* reading has been completed */
+ xprintf("hbm_read_mat: %d cards were read\n", dsa->seqn);
+ fclose(dsa->fp);
+ return hbm;
+fail: /* something wrong in Danish kingdom */
+ if (hbm != NULL)
+ { if (hbm->colptr != NULL) xfree(hbm->colptr);
+ if (hbm->rowind != NULL) xfree(hbm->rowind);
+ if (hbm->rhsptr != NULL) xfree(hbm->rhsptr);
+ if (hbm->rhsind != NULL) xfree(hbm->rhsind);
+ if (hbm->values != NULL) xfree(hbm->values);
+ if (hbm->rhsval != NULL) xfree(hbm->rhsval);
+ if (hbm->sguess != NULL) xfree(hbm->sguess);
+ if (hbm->xexact != NULL) xfree(hbm->xexact);
+ xfree(hbm);
+ }
+ if (dsa->fp != NULL) fclose(dsa->fp);
+ return NULL;
+}
+
+/***********************************************************************
+* NAME
+*
+* hbm_free_mat - free sparse matrix in Harwell-Boeing format
+*
+* SYNOPSIS
+*
+* #include "glphbm.h"
+* void hbm_free_mat(HBM *hbm);
+*
+* DESCRIPTION
+*
+* The hbm_free_mat routine frees all the memory allocated to the data
+* structure containing a sparse matrix in the Harwell-Boeing format. */
+
+void hbm_free_mat(HBM *hbm)
+{ if (hbm->colptr != NULL) xfree(hbm->colptr);
+ if (hbm->rowind != NULL) xfree(hbm->rowind);
+ if (hbm->rhsptr != NULL) xfree(hbm->rhsptr);
+ if (hbm->rhsind != NULL) xfree(hbm->rhsind);
+ if (hbm->values != NULL) xfree(hbm->values);
+ if (hbm->rhsval != NULL) xfree(hbm->rhsval);
+ if (hbm->sguess != NULL) xfree(hbm->sguess);
+ if (hbm->xexact != NULL) xfree(hbm->xexact);
+ xfree(hbm);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glphbm.h b/test/monniaux/glpk-4.65/src/draft/glphbm.h
new file mode 100644
index 00000000..688a78ec
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glphbm.h
@@ -0,0 +1,127 @@
+/* glphbm.h (Harwell-Boeing sparse matrix format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPHBM_H
+#define GLPHBM_H
+
+typedef struct HBM HBM;
+
+struct HBM
+{ /* sparse matrix in Harwell-Boeing format; for details see the
+ report: I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the
+ Harwell-Boeing Sparse Matrix Collection (Release I), 1992 */
+ char title[72+1];
+ /* matrix title (informative) */
+ char key[8+1];
+ /* matrix key (informative) */
+ char mxtype[3+1];
+ /* matrix type:
+ R.. real matrix
+ C.. complex matrix
+ P.. pattern only (no numerical values supplied)
+ .S. symmetric (lower triangle + main diagonal)
+ .U. unsymmetric
+ .H. hermitian (lower triangle + main diagonal)
+ .Z. skew symmetric (lower triangle only)
+ .R. rectangular
+ ..A assembled
+ ..E elemental (unassembled) */
+ char rhstyp[3+1];
+ /* optional types:
+ F.. right-hand sides in dense format
+ M.. right-hand sides in same format as matrix
+ .G. starting vector(s) (guess) is supplied
+ ..X exact solution vector(s) is supplied */
+ char ptrfmt[16+1];
+ /* format for pointers */
+ char indfmt[16+1];
+ /* format for row (or variable) indices */
+ char valfmt[20+1];
+ /* format for numerical values of coefficient matrix */
+ char rhsfmt[20+1];
+ /* format for numerical values of right-hand sides */
+ int totcrd;
+ /* total number of cards excluding header */
+ int ptrcrd;
+ /* number of cards for ponters */
+ int indcrd;
+ /* number of cards for row (or variable) indices */
+ int valcrd;
+ /* number of cards for numerical values */
+ int rhscrd;
+ /* number of lines for right-hand sides;
+ including starting guesses and solution vectors if present;
+ zero indicates no right-hand side data is present */
+ int nrow;
+ /* number of rows (or variables) */
+ int ncol;
+ /* number of columns (or elements) */
+ int nnzero;
+ /* number of row (or variable) indices;
+ equal to number of entries for assembled matrix */
+ int neltvl;
+ /* number of elemental matrix entries;
+ zero in case of assembled matrix */
+ int nrhs;
+ /* number of right-hand sides */
+ int nrhsix;
+ /* number of row indices;
+ ignored in case of unassembled matrix */
+ int nrhsvl;
+ /* total number of entries in all right-hand sides */
+ int nguess;
+ /* total number of entries in all starting guesses */
+ int nexact;
+ /* total number of entries in all solution vectors */
+ int *colptr; /* alias: eltptr */
+ /* column pointers (in case of assembled matrix);
+ elemental matrix pointers (in case of unassembled matrix) */
+ int *rowind; /* alias: varind */
+ /* row indices (in case of assembled matrix);
+ variable indices (in case of unassembled matrix) */
+ int *rhsptr;
+ /* right-hand side pointers */
+ int *rhsind;
+ /* right-hand side indices */
+ double *values;
+ /* matrix values */
+ double *rhsval;
+ /* right-hand side values */
+ double *sguess;
+ /* starting guess values */
+ double *xexact;
+ /* solution vector values */
+};
+
+#define hbm_read_mat _glp_hbm_read_mat
+HBM *hbm_read_mat(const char *fname);
+/* read sparse matrix in Harwell-Boeing format */
+
+#define hbm_free_mat _glp_hbm_free_mat
+void hbm_free_mat(HBM *hbm);
+/* free sparse matrix in Harwell-Boeing format */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios01.c b/test/monniaux/glpk-4.65/src/draft/glpios01.c
new file mode 100644
index 00000000..cb1a0dab
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios01.c
@@ -0,0 +1,1685 @@
+/* glpios01.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+#include "misc.h"
+
+static int lpx_eval_tab_row(glp_prob *lp, int k, int ind[],
+ double val[])
+{ /* compute row of the simplex tableau */
+ return glp_eval_tab_row(lp, k, ind, val);
+}
+
+static int lpx_dual_ratio_test(glp_prob *lp, int len, const int ind[],
+ const double val[], int how, double tol)
+{ /* perform dual ratio test */
+ int piv;
+ piv = glp_dual_rtest(lp, len, ind, val, how, tol);
+ xassert(0 <= piv && piv <= len);
+ return piv == 0 ? 0 : ind[piv];
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_create_tree - create branch-and-bound tree
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm);
+*
+* DESCRIPTION
+*
+* The routine ios_create_tree creates the branch-and-bound tree.
+*
+* Being created the tree consists of the only root subproblem whose
+* reference number is 1. Note that initially the root subproblem is in
+* frozen state and therefore needs to be revived.
+*
+* RETURNS
+*
+* The routine returns a pointer to the tree created. */
+
+static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent);
+
+glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm)
+{ int m = mip->m;
+ int n = mip->n;
+ glp_tree *tree;
+ int i, j;
+ xassert(mip->tree == NULL);
+ mip->tree = tree = xmalloc(sizeof(glp_tree));
+ tree->pool = dmp_create_pool();
+ tree->n = n;
+ /* save original problem components */
+ tree->orig_m = m;
+ tree->orig_type = xcalloc(1+m+n, sizeof(char));
+ tree->orig_lb = xcalloc(1+m+n, sizeof(double));
+ tree->orig_ub = xcalloc(1+m+n, sizeof(double));
+ tree->orig_stat = xcalloc(1+m+n, sizeof(char));
+ tree->orig_prim = xcalloc(1+m+n, sizeof(double));
+ tree->orig_dual = xcalloc(1+m+n, sizeof(double));
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = mip->row[i];
+ tree->orig_type[i] = (char)row->type;
+ tree->orig_lb[i] = row->lb;
+ tree->orig_ub[i] = row->ub;
+ tree->orig_stat[i] = (char)row->stat;
+ tree->orig_prim[i] = row->prim;
+ tree->orig_dual[i] = row->dual;
+ }
+ for (j = 1; j <= n; j++)
+ { GLPCOL *col = mip->col[j];
+ tree->orig_type[m+j] = (char)col->type;
+ tree->orig_lb[m+j] = col->lb;
+ tree->orig_ub[m+j] = col->ub;
+ tree->orig_stat[m+j] = (char)col->stat;
+ tree->orig_prim[m+j] = col->prim;
+ tree->orig_dual[m+j] = col->dual;
+ }
+ tree->orig_obj = mip->obj_val;
+ /* initialize the branch-and-bound tree */
+ tree->nslots = 0;
+ tree->avail = 0;
+ tree->slot = NULL;
+ tree->head = tree->tail = NULL;
+ tree->a_cnt = tree->n_cnt = tree->t_cnt = 0;
+ /* the root subproblem is not solved yet, so its final components
+ are unknown so far */
+ tree->root_m = 0;
+ tree->root_type = NULL;
+ tree->root_lb = tree->root_ub = NULL;
+ tree->root_stat = NULL;
+ /* the current subproblem does not exist yet */
+ tree->curr = NULL;
+ tree->mip = mip;
+ /*tree->solved = 0;*/
+ tree->non_int = xcalloc(1+n, sizeof(char));
+ memset(&tree->non_int[1], 0, n);
+ /* arrays to save parent subproblem components will be allocated
+ later */
+ tree->pred_m = tree->pred_max = 0;
+ tree->pred_type = NULL;
+ tree->pred_lb = tree->pred_ub = NULL;
+ tree->pred_stat = NULL;
+ /* cut generators */
+ tree->local = ios_create_pool(tree);
+ /*tree->first_attempt = 1;*/
+ /*tree->max_added_cuts = 0;*/
+ /*tree->min_eff = 0.0;*/
+ /*tree->miss = 0;*/
+ /*tree->just_selected = 0;*/
+#ifdef NEW_COVER /* 13/II-2018 */
+ tree->cov_gen = NULL;
+#endif
+ tree->mir_gen = NULL;
+ tree->clq_gen = NULL;
+ /*tree->round = 0;*/
+#if 0
+ /* create the conflict graph */
+ tree->n_ref = xcalloc(1+n, sizeof(int));
+ memset(&tree->n_ref[1], 0, n * sizeof(int));
+ tree->c_ref = xcalloc(1+n, sizeof(int));
+ memset(&tree->c_ref[1], 0, n * sizeof(int));
+ tree->g = scg_create_graph(0);
+ tree->j_ref = xcalloc(1+tree->g->n_max, sizeof(int));
+#endif
+ /* pseudocost branching */
+ tree->pcost = NULL;
+ tree->iwrk = xcalloc(1+n, sizeof(int));
+ tree->dwrk = xcalloc(1+n, sizeof(double));
+ /* initialize control parameters */
+ tree->parm = parm;
+ tree->tm_beg = xtime();
+#if 0 /* 10/VI-2013 */
+ tree->tm_lag = xlset(0);
+#else
+ tree->tm_lag = 0.0;
+#endif
+ tree->sol_cnt = 0;
+#if 1 /* 11/VII-2013 */
+ tree->P = NULL;
+ tree->npp = NULL;
+ tree->save_sol = parm->save_sol;
+ tree->save_cnt = 0;
+#endif
+ /* initialize advanced solver interface */
+ tree->reason = 0;
+ tree->reopt = 0;
+ tree->reinv = 0;
+ tree->br_var = 0;
+ tree->br_sel = 0;
+ tree->child = 0;
+ tree->next_p = 0;
+ /*tree->btrack = NULL;*/
+ tree->stop = 0;
+ /* create the root subproblem, which initially is identical to
+ the original MIP */
+ new_node(tree, NULL);
+ return tree;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_revive_node - revive specified subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_revive_node(glp_tree *tree, int p);
+*
+* DESCRIPTION
+*
+* The routine ios_revive_node revives the specified subproblem, whose
+* reference number is p, and thereby makes it the current subproblem.
+* Note that the specified subproblem must be active. Besides, if the
+* current subproblem already exists, it must be frozen before reviving
+* another subproblem. */
+
+void ios_revive_node(glp_tree *tree, int p)
+{ glp_prob *mip = tree->mip;
+ IOSNPD *node, *root;
+ /* obtain pointer to the specified subproblem */
+ xassert(1 <= p && p <= tree->nslots);
+ node = tree->slot[p].node;
+ xassert(node != NULL);
+ /* the specified subproblem must be active */
+ xassert(node->count == 0);
+ /* the current subproblem must not exist */
+ xassert(tree->curr == NULL);
+ /* the specified subproblem becomes current */
+ tree->curr = node;
+ /*tree->solved = 0;*/
+ /* obtain pointer to the root subproblem */
+ root = tree->slot[1].node;
+ xassert(root != NULL);
+ /* at this point problem object components correspond to the root
+ subproblem, so if the root subproblem should be revived, there
+ is nothing more to do */
+ if (node == root) goto done;
+ xassert(mip->m == tree->root_m);
+ /* build path from the root to the current node */
+ node->temp = NULL;
+ for (node = node; node != NULL; node = node->up)
+ { if (node->up == NULL)
+ xassert(node == root);
+ else
+ node->up->temp = node;
+ }
+ /* go down from the root to the current node and make necessary
+ changes to restore components of the current subproblem */
+ for (node = root; node != NULL; node = node->temp)
+ { int m = mip->m;
+ int n = mip->n;
+ /* if the current node is reached, the problem object at this
+ point corresponds to its parent, so save attributes of rows
+ and columns for the parent subproblem */
+ if (node->temp == NULL)
+ { int i, j;
+ tree->pred_m = m;
+ /* allocate/reallocate arrays, if necessary */
+ if (tree->pred_max < m + n)
+ { int new_size = m + n + 100;
+ if (tree->pred_type != NULL) xfree(tree->pred_type);
+ if (tree->pred_lb != NULL) xfree(tree->pred_lb);
+ if (tree->pred_ub != NULL) xfree(tree->pred_ub);
+ if (tree->pred_stat != NULL) xfree(tree->pred_stat);
+ tree->pred_max = new_size;
+ tree->pred_type = xcalloc(1+new_size, sizeof(char));
+ tree->pred_lb = xcalloc(1+new_size, sizeof(double));
+ tree->pred_ub = xcalloc(1+new_size, sizeof(double));
+ tree->pred_stat = xcalloc(1+new_size, sizeof(char));
+ }
+ /* save row attributes */
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = mip->row[i];
+ tree->pred_type[i] = (char)row->type;
+ tree->pred_lb[i] = row->lb;
+ tree->pred_ub[i] = row->ub;
+ tree->pred_stat[i] = (char)row->stat;
+ }
+ /* save column attributes */
+ for (j = 1; j <= n; j++)
+ { GLPCOL *col = mip->col[j];
+ tree->pred_type[mip->m+j] = (char)col->type;
+ tree->pred_lb[mip->m+j] = col->lb;
+ tree->pred_ub[mip->m+j] = col->ub;
+ tree->pred_stat[mip->m+j] = (char)col->stat;
+ }
+ }
+ /* change bounds of rows and columns */
+ { IOSBND *b;
+ for (b = node->b_ptr; b != NULL; b = b->next)
+ { if (b->k <= m)
+ glp_set_row_bnds(mip, b->k, b->type, b->lb, b->ub);
+ else
+ glp_set_col_bnds(mip, b->k-m, b->type, b->lb, b->ub);
+ }
+ }
+ /* change statuses of rows and columns */
+ { IOSTAT *s;
+ for (s = node->s_ptr; s != NULL; s = s->next)
+ { if (s->k <= m)
+ glp_set_row_stat(mip, s->k, s->stat);
+ else
+ glp_set_col_stat(mip, s->k-m, s->stat);
+ }
+ }
+ /* add new rows */
+ if (node->r_ptr != NULL)
+ { IOSROW *r;
+ IOSAIJ *a;
+ int i, len, *ind;
+ double *val;
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ for (r = node->r_ptr; r != NULL; r = r->next)
+ { i = glp_add_rows(mip, 1);
+ glp_set_row_name(mip, i, r->name);
+#if 1 /* 20/IX-2008 */
+ xassert(mip->row[i]->level == 0);
+ mip->row[i]->level = node->level;
+ mip->row[i]->origin = r->origin;
+ mip->row[i]->klass = r->klass;
+#endif
+ glp_set_row_bnds(mip, i, r->type, r->lb, r->ub);
+ len = 0;
+ for (a = r->ptr; a != NULL; a = a->next)
+ len++, ind[len] = a->j, val[len] = a->val;
+ glp_set_mat_row(mip, i, len, ind, val);
+ glp_set_rii(mip, i, r->rii);
+ glp_set_row_stat(mip, i, r->stat);
+ }
+ xfree(ind);
+ xfree(val);
+ }
+#if 0
+ /* add new edges to the conflict graph */
+ /* add new cliques to the conflict graph */
+ /* (not implemented yet) */
+ xassert(node->own_nn == 0);
+ xassert(node->own_nc == 0);
+ xassert(node->e_ptr == NULL);
+#endif
+ }
+ /* the specified subproblem has been revived */
+ node = tree->curr;
+ /* delete its bound change list */
+ while (node->b_ptr != NULL)
+ { IOSBND *b;
+ b = node->b_ptr;
+ node->b_ptr = b->next;
+ dmp_free_atom(tree->pool, b, sizeof(IOSBND));
+ }
+ /* delete its status change list */
+ while (node->s_ptr != NULL)
+ { IOSTAT *s;
+ s = node->s_ptr;
+ node->s_ptr = s->next;
+ dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
+ }
+#if 1 /* 20/XI-2009 */
+ /* delete its row addition list (additional rows may appear, for
+ example, due to branching on GUB constraints */
+ while (node->r_ptr != NULL)
+ { IOSROW *r;
+ r = node->r_ptr;
+ node->r_ptr = r->next;
+ xassert(r->name == NULL);
+ while (r->ptr != NULL)
+ { IOSAIJ *a;
+ a = r->ptr;
+ r->ptr = a->next;
+ dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
+ }
+ dmp_free_atom(tree->pool, r, sizeof(IOSROW));
+ }
+#endif
+done: return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_freeze_node - freeze current subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_freeze_node(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_freeze_node freezes the current subproblem. */
+
+void ios_freeze_node(glp_tree *tree)
+{ glp_prob *mip = tree->mip;
+ int m = mip->m;
+ int n = mip->n;
+ IOSNPD *node;
+ /* obtain pointer to the current subproblem */
+ node = tree->curr;
+ xassert(node != NULL);
+ if (node->up == NULL)
+ { /* freeze the root subproblem */
+ int k;
+ xassert(node->p == 1);
+ xassert(tree->root_m == 0);
+ xassert(tree->root_type == NULL);
+ xassert(tree->root_lb == NULL);
+ xassert(tree->root_ub == NULL);
+ xassert(tree->root_stat == NULL);
+ tree->root_m = m;
+ tree->root_type = xcalloc(1+m+n, sizeof(char));
+ tree->root_lb = xcalloc(1+m+n, sizeof(double));
+ tree->root_ub = xcalloc(1+m+n, sizeof(double));
+ tree->root_stat = xcalloc(1+m+n, sizeof(char));
+ for (k = 1; k <= m+n; k++)
+ { if (k <= m)
+ { GLPROW *row = mip->row[k];
+ tree->root_type[k] = (char)row->type;
+ tree->root_lb[k] = row->lb;
+ tree->root_ub[k] = row->ub;
+ tree->root_stat[k] = (char)row->stat;
+ }
+ else
+ { GLPCOL *col = mip->col[k-m];
+ tree->root_type[k] = (char)col->type;
+ tree->root_lb[k] = col->lb;
+ tree->root_ub[k] = col->ub;
+ tree->root_stat[k] = (char)col->stat;
+ }
+ }
+ }
+ else
+ { /* freeze non-root subproblem */
+ int root_m = tree->root_m;
+ int pred_m = tree->pred_m;
+ int i, j, k;
+ xassert(pred_m <= m);
+ /* build change lists for rows and columns which exist in the
+ parent subproblem */
+ xassert(node->b_ptr == NULL);
+ xassert(node->s_ptr == NULL);
+ for (k = 1; k <= pred_m + n; k++)
+ { int pred_type, pred_stat, type, stat;
+ double pred_lb, pred_ub, lb, ub;
+ /* determine attributes in the parent subproblem */
+ pred_type = tree->pred_type[k];
+ pred_lb = tree->pred_lb[k];
+ pred_ub = tree->pred_ub[k];
+ pred_stat = tree->pred_stat[k];
+ /* determine attributes in the current subproblem */
+ if (k <= pred_m)
+ { GLPROW *row = mip->row[k];
+ type = row->type;
+ lb = row->lb;
+ ub = row->ub;
+ stat = row->stat;
+ }
+ else
+ { GLPCOL *col = mip->col[k - pred_m];
+ type = col->type;
+ lb = col->lb;
+ ub = col->ub;
+ stat = col->stat;
+ }
+ /* save type and bounds of a row/column, if changed */
+ if (!(pred_type == type && pred_lb == lb && pred_ub == ub))
+ { IOSBND *b;
+ b = dmp_get_atom(tree->pool, sizeof(IOSBND));
+ b->k = k;
+ b->type = (unsigned char)type;
+ b->lb = lb;
+ b->ub = ub;
+ b->next = node->b_ptr;
+ node->b_ptr = b;
+ }
+ /* save status of a row/column, if changed */
+ if (pred_stat != stat)
+ { IOSTAT *s;
+ s = dmp_get_atom(tree->pool, sizeof(IOSTAT));
+ s->k = k;
+ s->stat = (unsigned char)stat;
+ s->next = node->s_ptr;
+ node->s_ptr = s;
+ }
+ }
+ /* save new rows added to the current subproblem */
+ xassert(node->r_ptr == NULL);
+ if (pred_m < m)
+ { int i, len, *ind;
+ double *val;
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ for (i = m; i > pred_m; i--)
+ { GLPROW *row = mip->row[i];
+ IOSROW *r;
+ const char *name;
+ r = dmp_get_atom(tree->pool, sizeof(IOSROW));
+ name = glp_get_row_name(mip, i);
+ if (name == NULL)
+ r->name = NULL;
+ else
+ { r->name = dmp_get_atom(tree->pool, strlen(name)+1);
+ strcpy(r->name, name);
+ }
+#if 1 /* 20/IX-2008 */
+ r->origin = row->origin;
+ r->klass = row->klass;
+#endif
+ r->type = (unsigned char)row->type;
+ r->lb = row->lb;
+ r->ub = row->ub;
+ r->ptr = NULL;
+ len = glp_get_mat_row(mip, i, ind, val);
+ for (k = 1; k <= len; k++)
+ { IOSAIJ *a;
+ a = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
+ a->j = ind[k];
+ a->val = val[k];
+ a->next = r->ptr;
+ r->ptr = a;
+ }
+ r->rii = row->rii;
+ r->stat = (unsigned char)row->stat;
+ r->next = node->r_ptr;
+ node->r_ptr = r;
+ }
+ xfree(ind);
+ xfree(val);
+ }
+ /* remove all rows missing in the root subproblem */
+ if (m != root_m)
+ { int nrs, *num;
+ nrs = m - root_m;
+ xassert(nrs > 0);
+ num = xcalloc(1+nrs, sizeof(int));
+ for (i = 1; i <= nrs; i++) num[i] = root_m + i;
+ glp_del_rows(mip, nrs, num);
+ xfree(num);
+ }
+ m = mip->m;
+ /* and restore attributes of all rows and columns for the root
+ subproblem */
+ xassert(m == root_m);
+ for (i = 1; i <= m; i++)
+ { glp_set_row_bnds(mip, i, tree->root_type[i],
+ tree->root_lb[i], tree->root_ub[i]);
+ glp_set_row_stat(mip, i, tree->root_stat[i]);
+ }
+ for (j = 1; j <= n; j++)
+ { glp_set_col_bnds(mip, j, tree->root_type[m+j],
+ tree->root_lb[m+j], tree->root_ub[m+j]);
+ glp_set_col_stat(mip, j, tree->root_stat[m+j]);
+ }
+#if 1
+ /* remove all edges and cliques missing in the conflict graph
+ for the root subproblem */
+ /* (not implemented yet) */
+#endif
+ }
+ /* the current subproblem has been frozen */
+ tree->curr = NULL;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_clone_node - clone specified subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[]);
+*
+* DESCRIPTION
+*
+* The routine ios_clone_node clones the specified subproblem, whose
+* reference number is p, creating its nnn exact copies. Note that the
+* specified subproblem must be active and must be in the frozen state
+* (i.e. it must not be the current subproblem).
+*
+* Each clone, an exact copy of the specified subproblem, becomes a new
+* active subproblem added to the end of the active list. After cloning
+* the specified subproblem becomes inactive.
+*
+* The reference numbers of clone subproblems are stored to locations
+* ref[1], ..., ref[nnn]. */
+
+static int get_slot(glp_tree *tree)
+{ int p;
+ /* if no free slots are available, increase the room */
+ if (tree->avail == 0)
+ { int nslots = tree->nslots;
+ IOSLOT *save = tree->slot;
+ if (nslots == 0)
+ tree->nslots = 20;
+ else
+ { tree->nslots = nslots + nslots;
+ xassert(tree->nslots > nslots);
+ }
+ tree->slot = xcalloc(1+tree->nslots, sizeof(IOSLOT));
+ if (save != NULL)
+ { memcpy(&tree->slot[1], &save[1], nslots * sizeof(IOSLOT));
+ xfree(save);
+ }
+ /* push more free slots into the stack */
+ for (p = tree->nslots; p > nslots; p--)
+ { tree->slot[p].node = NULL;
+ tree->slot[p].next = tree->avail;
+ tree->avail = p;
+ }
+ }
+ /* pull a free slot from the stack */
+ p = tree->avail;
+ tree->avail = tree->slot[p].next;
+ xassert(tree->slot[p].node == NULL);
+ tree->slot[p].next = 0;
+ return p;
+}
+
+static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent)
+{ IOSNPD *node;
+ int p;
+ /* pull a free slot for the new node */
+ p = get_slot(tree);
+ /* create descriptor of the new subproblem */
+ node = dmp_get_atom(tree->pool, sizeof(IOSNPD));
+ tree->slot[p].node = node;
+ node->p = p;
+ node->up = parent;
+ node->level = (parent == NULL ? 0 : parent->level + 1);
+ node->count = 0;
+ node->b_ptr = NULL;
+ node->s_ptr = NULL;
+ node->r_ptr = NULL;
+ node->solved = 0;
+#if 0
+ node->own_nn = node->own_nc = 0;
+ node->e_ptr = NULL;
+#endif
+#if 1 /* 04/X-2008 */
+ node->lp_obj = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
+ -DBL_MAX : +DBL_MAX) : parent->lp_obj);
+#endif
+ node->bound = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
+ -DBL_MAX : +DBL_MAX) : parent->bound);
+ node->br_var = 0;
+ node->br_val = 0.0;
+ node->ii_cnt = 0;
+ node->ii_sum = 0.0;
+#if 1 /* 30/XI-2009 */
+ node->changed = 0;
+#endif
+ if (tree->parm->cb_size == 0)
+ node->data = NULL;
+ else
+ { node->data = dmp_get_atom(tree->pool, tree->parm->cb_size);
+ memset(node->data, 0, tree->parm->cb_size);
+ }
+ node->temp = NULL;
+ node->prev = tree->tail;
+ node->next = NULL;
+ /* add the new subproblem to the end of the active list */
+ if (tree->head == NULL)
+ tree->head = node;
+ else
+ tree->tail->next = node;
+ tree->tail = node;
+ tree->a_cnt++;
+ tree->n_cnt++;
+ tree->t_cnt++;
+ /* increase the number of child subproblems */
+ if (parent == NULL)
+ xassert(p == 1);
+ else
+ parent->count++;
+ return node;
+}
+
+void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[])
+{ IOSNPD *node;
+ int k;
+ /* obtain pointer to the subproblem to be cloned */
+ xassert(1 <= p && p <= tree->nslots);
+ node = tree->slot[p].node;
+ xassert(node != NULL);
+ /* the specified subproblem must be active */
+ xassert(node->count == 0);
+ /* and must be in the frozen state */
+ xassert(tree->curr != node);
+ /* remove the specified subproblem from the active list, because
+ it becomes inactive */
+ if (node->prev == NULL)
+ tree->head = node->next;
+ else
+ node->prev->next = node->next;
+ if (node->next == NULL)
+ tree->tail = node->prev;
+ else
+ node->next->prev = node->prev;
+ node->prev = node->next = NULL;
+ tree->a_cnt--;
+ /* create clone subproblems */
+ xassert(nnn > 0);
+ for (k = 1; k <= nnn; k++)
+ ref[k] = new_node(tree, node)->p;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_delete_node - delete specified subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_delete_node(glp_tree *tree, int p);
+*
+* DESCRIPTION
+*
+* The routine ios_delete_node deletes the specified subproblem, whose
+* reference number is p. The subproblem must be active and must be in
+* the frozen state (i.e. it must not be the current subproblem).
+*
+* Note that deletion is performed recursively, i.e. if a subproblem to
+* be deleted is the only child of its parent, the parent subproblem is
+* also deleted, etc. */
+
+void ios_delete_node(glp_tree *tree, int p)
+{ IOSNPD *node, *temp;
+ /* obtain pointer to the subproblem to be deleted */
+ xassert(1 <= p && p <= tree->nslots);
+ node = tree->slot[p].node;
+ xassert(node != NULL);
+ /* the specified subproblem must be active */
+ xassert(node->count == 0);
+ /* and must be in the frozen state */
+ xassert(tree->curr != node);
+ /* remove the specified subproblem from the active list, because
+ it is gone from the tree */
+ if (node->prev == NULL)
+ tree->head = node->next;
+ else
+ node->prev->next = node->next;
+ if (node->next == NULL)
+ tree->tail = node->prev;
+ else
+ node->next->prev = node->prev;
+ node->prev = node->next = NULL;
+ tree->a_cnt--;
+loop: /* recursive deletion starts here */
+ /* delete the bound change list */
+ { IOSBND *b;
+ while (node->b_ptr != NULL)
+ { b = node->b_ptr;
+ node->b_ptr = b->next;
+ dmp_free_atom(tree->pool, b, sizeof(IOSBND));
+ }
+ }
+ /* delete the status change list */
+ { IOSTAT *s;
+ while (node->s_ptr != NULL)
+ { s = node->s_ptr;
+ node->s_ptr = s->next;
+ dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
+ }
+ }
+ /* delete the row addition list */
+ while (node->r_ptr != NULL)
+ { IOSROW *r;
+ r = node->r_ptr;
+ if (r->name != NULL)
+ dmp_free_atom(tree->pool, r->name, strlen(r->name)+1);
+ while (r->ptr != NULL)
+ { IOSAIJ *a;
+ a = r->ptr;
+ r->ptr = a->next;
+ dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
+ }
+ node->r_ptr = r->next;
+ dmp_free_atom(tree->pool, r, sizeof(IOSROW));
+ }
+#if 0
+ /* delete the edge addition list */
+ /* delete the clique addition list */
+ /* (not implemented yet) */
+ xassert(node->own_nn == 0);
+ xassert(node->own_nc == 0);
+ xassert(node->e_ptr == NULL);
+#endif
+ /* free application-specific data */
+ if (tree->parm->cb_size == 0)
+ xassert(node->data == NULL);
+ else
+ dmp_free_atom(tree->pool, node->data, tree->parm->cb_size);
+ /* free the corresponding node slot */
+ p = node->p;
+ xassert(tree->slot[p].node == node);
+ tree->slot[p].node = NULL;
+ tree->slot[p].next = tree->avail;
+ tree->avail = p;
+ /* save pointer to the parent subproblem */
+ temp = node->up;
+ /* delete the subproblem descriptor */
+ dmp_free_atom(tree->pool, node, sizeof(IOSNPD));
+ tree->n_cnt--;
+ /* take pointer to the parent subproblem */
+ node = temp;
+ if (node != NULL)
+ { /* the parent subproblem exists; decrease the number of its
+ child subproblems */
+ xassert(node->count > 0);
+ node->count--;
+ /* if now the parent subproblem has no childs, it also must be
+ deleted */
+ if (node->count == 0) goto loop;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_delete_tree - delete branch-and-bound tree
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_delete_tree(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_delete_tree deletes the branch-and-bound tree, which
+* the parameter tree points to, and frees all the memory allocated to
+* this program object.
+*
+* On exit components of the problem object are restored to correspond
+* to the original MIP passed to the routine ios_create_tree. */
+
+void ios_delete_tree(glp_tree *tree)
+{ glp_prob *mip = tree->mip;
+ int i, j;
+ int m = mip->m;
+ int n = mip->n;
+ xassert(mip->tree == tree);
+ /* remove all additional rows */
+ if (m != tree->orig_m)
+ { int nrs, *num;
+ nrs = m - tree->orig_m;
+ xassert(nrs > 0);
+ num = xcalloc(1+nrs, sizeof(int));
+ for (i = 1; i <= nrs; i++) num[i] = tree->orig_m + i;
+ glp_del_rows(mip, nrs, num);
+ xfree(num);
+ }
+ m = tree->orig_m;
+ /* restore original attributes of rows and columns */
+ xassert(m == tree->orig_m);
+ xassert(n == tree->n);
+ for (i = 1; i <= m; i++)
+ { glp_set_row_bnds(mip, i, tree->orig_type[i],
+ tree->orig_lb[i], tree->orig_ub[i]);
+ glp_set_row_stat(mip, i, tree->orig_stat[i]);
+ mip->row[i]->prim = tree->orig_prim[i];
+ mip->row[i]->dual = tree->orig_dual[i];
+ }
+ for (j = 1; j <= n; j++)
+ { glp_set_col_bnds(mip, j, tree->orig_type[m+j],
+ tree->orig_lb[m+j], tree->orig_ub[m+j]);
+ glp_set_col_stat(mip, j, tree->orig_stat[m+j]);
+ mip->col[j]->prim = tree->orig_prim[m+j];
+ mip->col[j]->dual = tree->orig_dual[m+j];
+ }
+ mip->pbs_stat = mip->dbs_stat = GLP_FEAS;
+ mip->obj_val = tree->orig_obj;
+ /* delete the branch-and-bound tree */
+ xassert(tree->local != NULL);
+ ios_delete_pool(tree, tree->local);
+ dmp_delete_pool(tree->pool);
+ xfree(tree->orig_type);
+ xfree(tree->orig_lb);
+ xfree(tree->orig_ub);
+ xfree(tree->orig_stat);
+ xfree(tree->orig_prim);
+ xfree(tree->orig_dual);
+ xfree(tree->slot);
+ if (tree->root_type != NULL) xfree(tree->root_type);
+ if (tree->root_lb != NULL) xfree(tree->root_lb);
+ if (tree->root_ub != NULL) xfree(tree->root_ub);
+ if (tree->root_stat != NULL) xfree(tree->root_stat);
+ xfree(tree->non_int);
+#if 0
+ xfree(tree->n_ref);
+ xfree(tree->c_ref);
+ xfree(tree->j_ref);
+#endif
+ if (tree->pcost != NULL) ios_pcost_free(tree);
+ xfree(tree->iwrk);
+ xfree(tree->dwrk);
+#if 0
+ scg_delete_graph(tree->g);
+#endif
+ if (tree->pred_type != NULL) xfree(tree->pred_type);
+ if (tree->pred_lb != NULL) xfree(tree->pred_lb);
+ if (tree->pred_ub != NULL) xfree(tree->pred_ub);
+ if (tree->pred_stat != NULL) xfree(tree->pred_stat);
+#if 0
+ xassert(tree->cut_gen == NULL);
+#endif
+ xassert(tree->mir_gen == NULL);
+ xassert(tree->clq_gen == NULL);
+ xfree(tree);
+ mip->tree = NULL;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_eval_degrad - estimate obj. degrad. for down- and up-branches
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up);
+*
+* DESCRIPTION
+*
+* Given optimal basis to LP relaxation of the current subproblem the
+* routine ios_eval_degrad performs the dual ratio test to compute the
+* objective values in the adjacent basis for down- and up-branches,
+* which are stored in locations *dn and *up, assuming that x[j] is a
+* variable chosen to branch upon. */
+
+void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up)
+{ glp_prob *mip = tree->mip;
+ int m = mip->m, n = mip->n;
+ int len, kase, k, t, stat;
+ double alfa, beta, gamma, delta, dz;
+ int *ind = tree->iwrk;
+ double *val = tree->dwrk;
+ /* current basis must be optimal */
+ xassert(glp_get_status(mip) == GLP_OPT);
+ /* basis factorization must exist */
+ xassert(glp_bf_exists(mip));
+ /* obtain (fractional) value of x[j] in optimal basic solution
+ to LP relaxation of the current subproblem */
+ xassert(1 <= j && j <= n);
+ beta = mip->col[j]->prim;
+ /* since the value of x[j] is fractional, it is basic; compute
+ corresponding row of the simplex table */
+ len = lpx_eval_tab_row(mip, m+j, ind, val);
+ /* kase < 0 means down-branch; kase > 0 means up-branch */
+ for (kase = -1; kase <= +1; kase += 2)
+ { /* for down-branch we introduce new upper bound floor(beta)
+ for x[j]; similarly, for up-branch we introduce new lower
+ bound ceil(beta) for x[j]; in the current basis this new
+ upper/lower bound is violated, so in the adjacent basis
+ x[j] will leave the basis and go to its new upper/lower
+ bound; we need to know which non-basic variable x[k] should
+ enter the basis to keep dual feasibility */
+#if 0 /* 23/XI-2009 */
+ k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-7);
+#else
+ k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-9);
+#endif
+ /* if no variable has been chosen, current basis being primal
+ infeasible due to the new upper/lower bound of x[j] is dual
+ unbounded, therefore, LP relaxation to corresponding branch
+ has no primal feasible solution */
+ if (k == 0)
+ { if (mip->dir == GLP_MIN)
+ { if (kase < 0)
+ *dn = +DBL_MAX;
+ else
+ *up = +DBL_MAX;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (kase < 0)
+ *dn = -DBL_MAX;
+ else
+ *up = -DBL_MAX;
+ }
+ else
+ xassert(mip != mip);
+ continue;
+ }
+ xassert(1 <= k && k <= m+n);
+ /* row of the simplex table corresponding to specified basic
+ variable x[j] is the following:
+ x[j] = ... + alfa * x[k] + ... ;
+ we need to know influence coefficient, alfa, at non-basic
+ variable x[k] chosen with the dual ratio test */
+ for (t = 1; t <= len; t++)
+ if (ind[t] == k) break;
+ xassert(1 <= t && t <= len);
+ alfa = val[t];
+ /* determine status and reduced cost of variable x[k] */
+ if (k <= m)
+ { stat = mip->row[k]->stat;
+ gamma = mip->row[k]->dual;
+ }
+ else
+ { stat = mip->col[k-m]->stat;
+ gamma = mip->col[k-m]->dual;
+ }
+ /* x[k] cannot be basic or fixed non-basic */
+ xassert(stat == GLP_NL || stat == GLP_NU || stat == GLP_NF);
+ /* if the current basis is dual degenerative, some reduced
+ costs, which are close to zero, may have wrong sign due to
+ round-off errors, so correct the sign of gamma */
+ if (mip->dir == GLP_MIN)
+ { if (stat == GLP_NL && gamma < 0.0 ||
+ stat == GLP_NU && gamma > 0.0 ||
+ stat == GLP_NF) gamma = 0.0;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (stat == GLP_NL && gamma > 0.0 ||
+ stat == GLP_NU && gamma < 0.0 ||
+ stat == GLP_NF) gamma = 0.0;
+ }
+ else
+ xassert(mip != mip);
+ /* determine the change of x[j] in the adjacent basis:
+ delta x[j] = new x[j] - old x[j] */
+ delta = (kase < 0 ? floor(beta) : ceil(beta)) - beta;
+ /* compute the change of x[k] in the adjacent basis:
+ delta x[k] = new x[k] - old x[k] = delta x[j] / alfa */
+ delta /= alfa;
+ /* compute the change of the objective in the adjacent basis:
+ delta z = new z - old z = gamma * delta x[k] */
+ dz = gamma * delta;
+ if (mip->dir == GLP_MIN)
+ xassert(dz >= 0.0);
+ else if (mip->dir == GLP_MAX)
+ xassert(dz <= 0.0);
+ else
+ xassert(mip != mip);
+ /* compute the new objective value in the adjacent basis:
+ new z = old z + delta z */
+ if (kase < 0)
+ *dn = mip->obj_val + dz;
+ else
+ *up = mip->obj_val + dz;
+ }
+ /*xprintf("obj = %g; dn = %g; up = %g\n",
+ mip->obj_val, *dn, *up);*/
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_round_bound - improve local bound by rounding
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* double ios_round_bound(glp_tree *tree, double bound);
+*
+* RETURNS
+*
+* For the given local bound for any integer feasible solution to the
+* current subproblem the routine ios_round_bound returns an improved
+* local bound for the same integer feasible solution.
+*
+* BACKGROUND
+*
+* Let the current subproblem has the following objective function:
+*
+* z = sum c[j] * x[j] + s >= b, (1)
+* j in J
+*
+* where J = {j: c[j] is non-zero and integer, x[j] is integer}, s is
+* the sum of terms corresponding to fixed variables, b is an initial
+* local bound (minimization).
+*
+* From (1) it follows that:
+*
+* d * sum (c[j] / d) * x[j] + s >= b, (2)
+* j in J
+*
+* or, equivalently,
+*
+* sum (c[j] / d) * x[j] >= (b - s) / d = h, (3)
+* j in J
+*
+* where d = gcd(c[j]). Since the left-hand side of (3) is integer,
+* h = (b - s) / d can be rounded up to the nearest integer:
+*
+* h' = ceil(h) = (b' - s) / d, (4)
+*
+* that gives an rounded, improved local bound:
+*
+* b' = d * h' + s. (5)
+*
+* In case of maximization '>=' in (1) should be replaced by '<=' that
+* leads to the following formula:
+*
+* h' = floor(h) = (b' - s) / d, (6)
+*
+* which should used in the same way as (4).
+*
+* NOTE: If b is a valid local bound for a child of the current
+* subproblem, b' is also valid for that child subproblem. */
+
+double ios_round_bound(glp_tree *tree, double bound)
+{ glp_prob *mip = tree->mip;
+ int n = mip->n;
+ int d, j, nn, *c = tree->iwrk;
+ double s, h;
+ /* determine c[j] and compute s */
+ nn = 0, s = mip->c0, d = 0;
+ for (j = 1; j <= n; j++)
+ { GLPCOL *col = mip->col[j];
+ if (col->coef == 0.0) continue;
+ if (col->type == GLP_FX)
+ { /* fixed variable */
+ s += col->coef * col->prim;
+ }
+ else
+ { /* non-fixed variable */
+ if (col->kind != GLP_IV) goto skip;
+ if (col->coef != floor(col->coef)) goto skip;
+ if (fabs(col->coef) <= (double)INT_MAX)
+ c[++nn] = (int)fabs(col->coef);
+ else
+ d = 1;
+ }
+ }
+ /* compute d = gcd(c[1],...c[nn]) */
+ if (d == 0)
+ { if (nn == 0) goto skip;
+ d = gcdn(nn, c);
+ }
+ xassert(d > 0);
+ /* compute new local bound */
+ if (mip->dir == GLP_MIN)
+ { if (bound != +DBL_MAX)
+ { h = (bound - s) / (double)d;
+ if (h >= floor(h) + 0.001)
+ { /* round up */
+ h = ceil(h);
+ /*xprintf("d = %d; old = %g; ", d, bound);*/
+ bound = (double)d * h + s;
+ /*xprintf("new = %g\n", bound);*/
+ }
+ }
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (bound != -DBL_MAX)
+ { h = (bound - s) / (double)d;
+ if (h <= ceil(h) - 0.001)
+ { /* round down */
+ h = floor(h);
+ bound = (double)d * h + s;
+ }
+ }
+ }
+ else
+ xassert(mip != mip);
+skip: return bound;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_is_hopeful - check if subproblem is hopeful
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_is_hopeful(glp_tree *tree, double bound);
+*
+* DESCRIPTION
+*
+* Given the local bound of a subproblem the routine ios_is_hopeful
+* checks if the subproblem can have an integer optimal solution which
+* is better than the best one currently known.
+*
+* RETURNS
+*
+* If the subproblem can have a better integer optimal solution, the
+* routine returns non-zero; otherwise, if the corresponding branch can
+* be pruned, the routine returns zero. */
+
+int ios_is_hopeful(glp_tree *tree, double bound)
+{ glp_prob *mip = tree->mip;
+ int ret = 1;
+ double eps;
+ if (mip->mip_stat == GLP_FEAS)
+ { eps = tree->parm->tol_obj * (1.0 + fabs(mip->mip_obj));
+ switch (mip->dir)
+ { case GLP_MIN:
+ if (bound >= mip->mip_obj - eps) ret = 0;
+ break;
+ case GLP_MAX:
+ if (bound <= mip->mip_obj + eps) ret = 0;
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ }
+ else
+ { switch (mip->dir)
+ { case GLP_MIN:
+ if (bound == +DBL_MAX) ret = 0;
+ break;
+ case GLP_MAX:
+ if (bound == -DBL_MAX) ret = 0;
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ }
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_best_node - find active node with best local bound
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_best_node(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_best_node finds an active node whose local bound is
+* best among other active nodes.
+*
+* It is understood that the integer optimal solution of the original
+* mip problem cannot be better than the best bound, so the best bound
+* is an lower (minimization) or upper (maximization) global bound for
+* the original problem.
+*
+* RETURNS
+*
+* The routine ios_best_node returns the subproblem reference number
+* for the best node. However, if the tree is empty, it returns zero. */
+
+int ios_best_node(glp_tree *tree)
+{ IOSNPD *node, *best = NULL;
+ switch (tree->mip->dir)
+ { case GLP_MIN:
+ /* minimization */
+ for (node = tree->head; node != NULL; node = node->next)
+ if (best == NULL || best->bound > node->bound)
+ best = node;
+ break;
+ case GLP_MAX:
+ /* maximization */
+ for (node = tree->head; node != NULL; node = node->next)
+ if (best == NULL || best->bound < node->bound)
+ best = node;
+ break;
+ default:
+ xassert(tree != tree);
+ }
+ return best == NULL ? 0 : best->p;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_relative_gap - compute relative mip gap
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* double ios_relative_gap(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_relative_gap computes the relative mip gap using the
+* formula:
+*
+* gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON),
+*
+* where best_mip is the best integer feasible solution found so far,
+* best_bnd is the best (global) bound. If no integer feasible solution
+* has been found yet, rel_gap is set to DBL_MAX.
+*
+* RETURNS
+*
+* The routine ios_relative_gap returns the relative mip gap. */
+
+double ios_relative_gap(glp_tree *tree)
+{ glp_prob *mip = tree->mip;
+ int p;
+ double best_mip, best_bnd, gap;
+ if (mip->mip_stat == GLP_FEAS)
+ { best_mip = mip->mip_obj;
+ p = ios_best_node(tree);
+ if (p == 0)
+ { /* the tree is empty */
+ gap = 0.0;
+ }
+ else
+ { best_bnd = tree->slot[p].node->bound;
+ gap = fabs(best_mip - best_bnd) / (fabs(best_mip) +
+ DBL_EPSILON);
+ }
+ }
+ else
+ { /* no integer feasible solution has been found yet */
+ gap = DBL_MAX;
+ }
+ return gap;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_solve_node - solve LP relaxation of current subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_solve_node(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_solve_node re-optimizes LP relaxation of the current
+* subproblem using the dual simplex method.
+*
+* RETURNS
+*
+* The routine returns the code which is reported by glp_simplex. */
+
+int ios_solve_node(glp_tree *tree)
+{ glp_prob *mip = tree->mip;
+ glp_smcp parm;
+ int ret;
+ /* the current subproblem must exist */
+ xassert(tree->curr != NULL);
+ /* set some control parameters */
+ glp_init_smcp(&parm);
+ switch (tree->parm->msg_lev)
+ { case GLP_MSG_OFF:
+ parm.msg_lev = GLP_MSG_OFF; break;
+ case GLP_MSG_ERR:
+ parm.msg_lev = GLP_MSG_ERR; break;
+ case GLP_MSG_ON:
+ case GLP_MSG_ALL:
+ parm.msg_lev = GLP_MSG_ON; break;
+ case GLP_MSG_DBG:
+ parm.msg_lev = GLP_MSG_ALL; break;
+ default:
+ xassert(tree != tree);
+ }
+ parm.meth = GLP_DUALP;
+#if 1 /* 16/III-2016 */
+ if (tree->parm->flip)
+ parm.r_test = GLP_RT_FLIP;
+#endif
+ /* respect time limit */
+ if (tree->parm->tm_lim < INT_MAX)
+ parm.tm_lim = tree->parm->tm_lim - (glp_time() - tree->tm_beg);
+ if (parm.tm_lim < 0)
+ parm.tm_lim = 0;
+ if (tree->parm->msg_lev < GLP_MSG_DBG)
+ parm.out_dly = tree->parm->out_dly;
+ else
+ parm.out_dly = 0;
+ /* if the incumbent objective value is already known, use it to
+ prematurely terminate the dual simplex search */
+ if (mip->mip_stat == GLP_FEAS)
+ { switch (tree->mip->dir)
+ { case GLP_MIN:
+ parm.obj_ul = mip->mip_obj;
+ break;
+ case GLP_MAX:
+ parm.obj_ll = mip->mip_obj;
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ }
+ /* try to solve/re-optimize the LP relaxation */
+ ret = glp_simplex(mip, &parm);
+#if 1 /* 21/II-2016 by Chris */
+ if (ret == GLP_EFAIL)
+ { /* retry with a new basis */
+ glp_adv_basis(mip, 0);
+ ret = glp_simplex(mip, &parm);
+ }
+#endif
+ tree->curr->solved++;
+#if 0
+ xprintf("ret = %d; status = %d; pbs = %d; dbs = %d; some = %d\n",
+ ret, glp_get_status(mip), mip->pbs_stat, mip->dbs_stat,
+ mip->some);
+ lpx_print_sol(mip, "sol");
+#endif
+ return ret;
+}
+
+/**********************************************************************/
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+IOSPOOL *ios_create_pool(glp_tree *tree)
+{ /* create cut pool */
+ IOSPOOL *pool;
+ pool = glp_create_prob();
+ glp_add_cols(pool, tree->mip->n);
+ return pool;
+}
+#else
+IOSPOOL *ios_create_pool(glp_tree *tree)
+{ /* create cut pool */
+ IOSPOOL *pool;
+#if 0
+ pool = dmp_get_atom(tree->pool, sizeof(IOSPOOL));
+#else
+ xassert(tree == tree);
+ pool = xmalloc(sizeof(IOSPOOL));
+#endif
+ pool->size = 0;
+ pool->head = pool->tail = NULL;
+ pool->ord = 0, pool->curr = NULL;
+ return pool;
+}
+#endif
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+int ios_add_row(glp_tree *tree, IOSPOOL *pool,
+ const char *name, int klass, int flags, int len, const int ind[],
+ const double val[], int type, double rhs)
+{ /* add row (constraint) to the cut pool */
+ int i;
+ i = glp_add_rows(pool, 1);
+ glp_set_row_name(pool, i, name);
+ pool->row[i]->klass = klass;
+ xassert(flags == 0);
+ glp_set_mat_row(pool, i, len, ind, val);
+ glp_set_row_bnds(pool, i, type, rhs, rhs);
+ return i;
+}
+#else
+int ios_add_row(glp_tree *tree, IOSPOOL *pool,
+ const char *name, int klass, int flags, int len, const int ind[],
+ const double val[], int type, double rhs)
+{ /* add row (constraint) to the cut pool */
+ IOSCUT *cut;
+ IOSAIJ *aij;
+ int k;
+ xassert(pool != NULL);
+ cut = dmp_get_atom(tree->pool, sizeof(IOSCUT));
+ if (name == NULL || name[0] == '\0')
+ cut->name = NULL;
+ else
+ { for (k = 0; name[k] != '\0'; k++)
+ { if (k == 256)
+ xerror("glp_ios_add_row: cut name too long\n");
+ if (iscntrl((unsigned char)name[k]))
+ xerror("glp_ios_add_row: cut name contains invalid chara"
+ "cter(s)\n");
+ }
+ cut->name = dmp_get_atom(tree->pool, strlen(name)+1);
+ strcpy(cut->name, name);
+ }
+ if (!(0 <= klass && klass <= 255))
+ xerror("glp_ios_add_row: klass = %d; invalid cut class\n",
+ klass);
+ cut->klass = (unsigned char)klass;
+ if (flags != 0)
+ xerror("glp_ios_add_row: flags = %d; invalid cut flags\n",
+ flags);
+ cut->ptr = NULL;
+ if (!(0 <= len && len <= tree->n))
+ xerror("glp_ios_add_row: len = %d; invalid cut length\n",
+ len);
+ for (k = 1; k <= len; k++)
+ { aij = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
+ if (!(1 <= ind[k] && ind[k] <= tree->n))
+ xerror("glp_ios_add_row: ind[%d] = %d; column index out of "
+ "range\n", k, ind[k]);
+ aij->j = ind[k];
+ aij->val = val[k];
+ aij->next = cut->ptr;
+ cut->ptr = aij;
+ }
+ if (!(type == GLP_LO || type == GLP_UP || type == GLP_FX))
+ xerror("glp_ios_add_row: type = %d; invalid cut type\n",
+ type);
+ cut->type = (unsigned char)type;
+ cut->rhs = rhs;
+ cut->prev = pool->tail;
+ cut->next = NULL;
+ if (cut->prev == NULL)
+ pool->head = cut;
+ else
+ cut->prev->next = cut;
+ pool->tail = cut;
+ pool->size++;
+ return pool->size;
+}
+#endif
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+IOSCUT *ios_find_row(IOSPOOL *pool, int i)
+{ /* find row (constraint) in the cut pool */
+ xassert(0);
+}
+#else
+IOSCUT *ios_find_row(IOSPOOL *pool, int i)
+{ /* find row (constraint) in the cut pool */
+ /* (smart linear search) */
+ xassert(pool != NULL);
+ xassert(1 <= i && i <= pool->size);
+ if (pool->ord == 0)
+ { xassert(pool->curr == NULL);
+ pool->ord = 1;
+ pool->curr = pool->head;
+ }
+ xassert(pool->curr != NULL);
+ if (i < pool->ord)
+ { if (i < pool->ord - i)
+ { pool->ord = 1;
+ pool->curr = pool->head;
+ while (pool->ord != i)
+ { pool->ord++;
+ xassert(pool->curr != NULL);
+ pool->curr = pool->curr->next;
+ }
+ }
+ else
+ { while (pool->ord != i)
+ { pool->ord--;
+ xassert(pool->curr != NULL);
+ pool->curr = pool->curr->prev;
+ }
+ }
+ }
+ else if (i > pool->ord)
+ { if (i - pool->ord < pool->size - i)
+ { while (pool->ord != i)
+ { pool->ord++;
+ xassert(pool->curr != NULL);
+ pool->curr = pool->curr->next;
+ }
+ }
+ else
+ { pool->ord = pool->size;
+ pool->curr = pool->tail;
+ while (pool->ord != i)
+ { pool->ord--;
+ xassert(pool->curr != NULL);
+ pool->curr = pool->curr->prev;
+ }
+ }
+ }
+ xassert(pool->ord == i);
+ xassert(pool->curr != NULL);
+ return pool->curr;
+}
+#endif
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+void ios_del_row(glp_tree *tree, IOSPOOL *pool, int i)
+{ /* remove row (constraint) from the cut pool */
+ xassert(0);
+}
+#else
+void ios_del_row(glp_tree *tree, IOSPOOL *pool, int i)
+{ /* remove row (constraint) from the cut pool */
+ IOSCUT *cut;
+ IOSAIJ *aij;
+ xassert(pool != NULL);
+ if (!(1 <= i && i <= pool->size))
+ xerror("glp_ios_del_row: i = %d; cut number out of range\n",
+ i);
+ cut = ios_find_row(pool, i);
+ xassert(pool->curr == cut);
+ if (cut->next != NULL)
+ pool->curr = cut->next;
+ else if (cut->prev != NULL)
+ pool->ord--, pool->curr = cut->prev;
+ else
+ pool->ord = 0, pool->curr = NULL;
+ if (cut->name != NULL)
+ dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
+ if (cut->prev == NULL)
+ { xassert(pool->head == cut);
+ pool->head = cut->next;
+ }
+ else
+ { xassert(cut->prev->next == cut);
+ cut->prev->next = cut->next;
+ }
+ if (cut->next == NULL)
+ { xassert(pool->tail == cut);
+ pool->tail = cut->prev;
+ }
+ else
+ { xassert(cut->next->prev == cut);
+ cut->next->prev = cut->prev;
+ }
+ while (cut->ptr != NULL)
+ { aij = cut->ptr;
+ cut->ptr = aij->next;
+ dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
+ }
+ dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
+ pool->size--;
+ return;
+}
+#endif
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+void ios_clear_pool(glp_tree *tree, IOSPOOL *pool)
+{ /* remove all rows (constraints) from the cut pool */
+ if (pool->m > 0)
+ { int i, *num;
+ num = talloc(1+pool->m, int);
+ for (i = 1; i <= pool->m; i++)
+ num[i] = i;
+ glp_del_rows(pool, pool->m, num);
+ tfree(num);
+ }
+ return;
+}
+#else
+void ios_clear_pool(glp_tree *tree, IOSPOOL *pool)
+{ /* remove all rows (constraints) from the cut pool */
+ xassert(pool != NULL);
+ while (pool->head != NULL)
+ { IOSCUT *cut = pool->head;
+ pool->head = cut->next;
+ if (cut->name != NULL)
+ dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
+ while (cut->ptr != NULL)
+ { IOSAIJ *aij = cut->ptr;
+ cut->ptr = aij->next;
+ dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
+ }
+ dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
+ }
+ pool->size = 0;
+ pool->head = pool->tail = NULL;
+ pool->ord = 0, pool->curr = NULL;
+ return;
+}
+#endif
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+void ios_delete_pool(glp_tree *tree, IOSPOOL *pool)
+{ /* delete cut pool */
+ xassert(pool != NULL);
+ glp_delete_prob(pool);
+ return;
+}
+#else
+void ios_delete_pool(glp_tree *tree, IOSPOOL *pool)
+{ /* delete cut pool */
+ xassert(pool != NULL);
+ ios_clear_pool(tree, pool);
+ xfree(pool);
+ return;
+}
+#endif
+
+#if 1 /* 11/VII-2013 */
+#include "npp.h"
+
+void ios_process_sol(glp_tree *T)
+{ /* process integer feasible solution just found */
+ if (T->npp != NULL)
+ { /* postprocess solution from transformed mip */
+ npp_postprocess(T->npp, T->mip);
+ /* store solution to problem passed to glp_intopt */
+ npp_unload_sol(T->npp, T->P);
+ }
+ xassert(T->P != NULL);
+ /* save solution to text file, if requested */
+ if (T->save_sol != NULL)
+ { char *fn, *mark;
+ fn = talloc(strlen(T->save_sol) + 50, char);
+ mark = strrchr(T->save_sol, '*');
+ if (mark == NULL)
+ strcpy(fn, T->save_sol);
+ else
+ { memcpy(fn, T->save_sol, mark - T->save_sol);
+ fn[mark - T->save_sol] = '\0';
+ sprintf(fn + strlen(fn), "%03d", ++(T->save_cnt));
+ strcat(fn, &mark[1]);
+ }
+ glp_write_mip(T->P, fn);
+ tfree(fn);
+ }
+ return;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios02.c b/test/monniaux/glpk-4.65/src/draft/glpios02.c
new file mode 100644
index 00000000..a73458aa
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios02.c
@@ -0,0 +1,826 @@
+/* glpios02.c (preprocess current subproblem) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* prepare_row_info - prepare row info to determine implied bounds
+*
+* Given a row (linear form)
+*
+* n
+* sum a[j] * x[j] (1)
+* j=1
+*
+* and bounds of columns (variables)
+*
+* l[j] <= x[j] <= u[j] (2)
+*
+* this routine computes f_min, j_min, f_max, j_max needed to determine
+* implied bounds.
+*
+* ALGORITHM
+*
+* Let J+ = {j : a[j] > 0} and J- = {j : a[j] < 0}.
+*
+* Parameters f_min and j_min are computed as follows:
+*
+* 1) if there is no x[k] such that k in J+ and l[k] = -inf or k in J-
+* and u[k] = +inf, then
+*
+* f_min := sum a[j] * l[j] + sum a[j] * u[j]
+* j in J+ j in J-
+* (3)
+* j_min := 0
+*
+* 2) if there is exactly one x[k] such that k in J+ and l[k] = -inf
+* or k in J- and u[k] = +inf, then
+*
+* f_min := sum a[j] * l[j] + sum a[j] * u[j]
+* j in J+\{k} j in J-\{k}
+* (4)
+* j_min := k
+*
+* 3) if there are two or more x[k] such that k in J+ and l[k] = -inf
+* or k in J- and u[k] = +inf, then
+*
+* f_min := -inf
+* (5)
+* j_min := 0
+*
+* Parameters f_max and j_max are computed in a similar way as follows:
+*
+* 1) if there is no x[k] such that k in J+ and u[k] = +inf or k in J-
+* and l[k] = -inf, then
+*
+* f_max := sum a[j] * u[j] + sum a[j] * l[j]
+* j in J+ j in J-
+* (6)
+* j_max := 0
+*
+* 2) if there is exactly one x[k] such that k in J+ and u[k] = +inf
+* or k in J- and l[k] = -inf, then
+*
+* f_max := sum a[j] * u[j] + sum a[j] * l[j]
+* j in J+\{k} j in J-\{k}
+* (7)
+* j_max := k
+*
+* 3) if there are two or more x[k] such that k in J+ and u[k] = +inf
+* or k in J- and l[k] = -inf, then
+*
+* f_max := +inf
+* (8)
+* j_max := 0 */
+
+struct f_info
+{ int j_min, j_max;
+ double f_min, f_max;
+};
+
+static void prepare_row_info(int n, const double a[], const double l[],
+ const double u[], struct f_info *f)
+{ int j, j_min, j_max;
+ double f_min, f_max;
+ xassert(n >= 0);
+ /* determine f_min and j_min */
+ f_min = 0.0, j_min = 0;
+ for (j = 1; j <= n; j++)
+ { if (a[j] > 0.0)
+ { if (l[j] == -DBL_MAX)
+ { if (j_min == 0)
+ j_min = j;
+ else
+ { f_min = -DBL_MAX, j_min = 0;
+ break;
+ }
+ }
+ else
+ f_min += a[j] * l[j];
+ }
+ else if (a[j] < 0.0)
+ { if (u[j] == +DBL_MAX)
+ { if (j_min == 0)
+ j_min = j;
+ else
+ { f_min = -DBL_MAX, j_min = 0;
+ break;
+ }
+ }
+ else
+ f_min += a[j] * u[j];
+ }
+ else
+ xassert(a != a);
+ }
+ f->f_min = f_min, f->j_min = j_min;
+ /* determine f_max and j_max */
+ f_max = 0.0, j_max = 0;
+ for (j = 1; j <= n; j++)
+ { if (a[j] > 0.0)
+ { if (u[j] == +DBL_MAX)
+ { if (j_max == 0)
+ j_max = j;
+ else
+ { f_max = +DBL_MAX, j_max = 0;
+ break;
+ }
+ }
+ else
+ f_max += a[j] * u[j];
+ }
+ else if (a[j] < 0.0)
+ { if (l[j] == -DBL_MAX)
+ { if (j_max == 0)
+ j_max = j;
+ else
+ { f_max = +DBL_MAX, j_max = 0;
+ break;
+ }
+ }
+ else
+ f_max += a[j] * l[j];
+ }
+ else
+ xassert(a != a);
+ }
+ f->f_max = f_max, f->j_max = j_max;
+ return;
+}
+
+/***********************************************************************
+* row_implied_bounds - determine row implied bounds
+*
+* Given a row (linear form)
+*
+* n
+* sum a[j] * x[j]
+* j=1
+*
+* and bounds of columns (variables)
+*
+* l[j] <= x[j] <= u[j]
+*
+* this routine determines implied bounds of the row.
+*
+* ALGORITHM
+*
+* Let J+ = {j : a[j] > 0} and J- = {j : a[j] < 0}.
+*
+* The implied lower bound of the row is computed as follows:
+*
+* L' := sum a[j] * l[j] + sum a[j] * u[j] (9)
+* j in J+ j in J-
+*
+* and as it follows from (3), (4), and (5):
+*
+* L' := if j_min = 0 then f_min else -inf (10)
+*
+* The implied upper bound of the row is computed as follows:
+*
+* U' := sum a[j] * u[j] + sum a[j] * l[j] (11)
+* j in J+ j in J-
+*
+* and as it follows from (6), (7), and (8):
+*
+* U' := if j_max = 0 then f_max else +inf (12)
+*
+* The implied bounds are stored in locations LL and UU. */
+
+static void row_implied_bounds(const struct f_info *f, double *LL,
+ double *UU)
+{ *LL = (f->j_min == 0 ? f->f_min : -DBL_MAX);
+ *UU = (f->j_max == 0 ? f->f_max : +DBL_MAX);
+ return;
+}
+
+/***********************************************************************
+* col_implied_bounds - determine column implied bounds
+*
+* Given a row (constraint)
+*
+* n
+* L <= sum a[j] * x[j] <= U (13)
+* j=1
+*
+* and bounds of columns (variables)
+*
+* l[j] <= x[j] <= u[j]
+*
+* this routine determines implied bounds of variable x[k].
+*
+* It is assumed that if L != -inf, the lower bound of the row can be
+* active, and if U != +inf, the upper bound of the row can be active.
+*
+* ALGORITHM
+*
+* From (13) it follows that
+*
+* L <= sum a[j] * x[j] + a[k] * x[k] <= U
+* j!=k
+* or
+*
+* L - sum a[j] * x[j] <= a[k] * x[k] <= U - sum a[j] * x[j]
+* j!=k j!=k
+*
+* Thus, if the row lower bound L can be active, implied lower bound of
+* term a[k] * x[k] can be determined as follows:
+*
+* ilb(a[k] * x[k]) = min(L - sum a[j] * x[j]) =
+* j!=k
+* (14)
+* = L - max sum a[j] * x[j]
+* j!=k
+*
+* where, as it follows from (6), (7), and (8)
+*
+* / f_max - a[k] * u[k], j_max = 0, a[k] > 0
+* |
+* | f_max - a[k] * l[k], j_max = 0, a[k] < 0
+* max sum a[j] * x[j] = {
+* j!=k | f_max, j_max = k
+* |
+* \ +inf, j_max != 0
+*
+* and if the upper bound U can be active, implied upper bound of term
+* a[k] * x[k] can be determined as follows:
+*
+* iub(a[k] * x[k]) = max(U - sum a[j] * x[j]) =
+* j!=k
+* (15)
+* = U - min sum a[j] * x[j]
+* j!=k
+*
+* where, as it follows from (3), (4), and (5)
+*
+* / f_min - a[k] * l[k], j_min = 0, a[k] > 0
+* |
+* | f_min - a[k] * u[k], j_min = 0, a[k] < 0
+* min sum a[j] * x[j] = {
+* j!=k | f_min, j_min = k
+* |
+* \ -inf, j_min != 0
+*
+* Since
+*
+* ilb(a[k] * x[k]) <= a[k] * x[k] <= iub(a[k] * x[k])
+*
+* implied lower and upper bounds of x[k] are determined as follows:
+*
+* l'[k] := if a[k] > 0 then ilb / a[k] else ulb / a[k] (16)
+*
+* u'[k] := if a[k] > 0 then ulb / a[k] else ilb / a[k] (17)
+*
+* The implied bounds are stored in locations ll and uu. */
+
+static void col_implied_bounds(const struct f_info *f, int n,
+ const double a[], double L, double U, const double l[],
+ const double u[], int k, double *ll, double *uu)
+{ double ilb, iub;
+ xassert(n >= 0);
+ xassert(1 <= k && k <= n);
+ /* determine implied lower bound of term a[k] * x[k] (14) */
+ if (L == -DBL_MAX || f->f_max == +DBL_MAX)
+ ilb = -DBL_MAX;
+ else if (f->j_max == 0)
+ { if (a[k] > 0.0)
+ { xassert(u[k] != +DBL_MAX);
+ ilb = L - (f->f_max - a[k] * u[k]);
+ }
+ else if (a[k] < 0.0)
+ { xassert(l[k] != -DBL_MAX);
+ ilb = L - (f->f_max - a[k] * l[k]);
+ }
+ else
+ xassert(a != a);
+ }
+ else if (f->j_max == k)
+ ilb = L - f->f_max;
+ else
+ ilb = -DBL_MAX;
+ /* determine implied upper bound of term a[k] * x[k] (15) */
+ if (U == +DBL_MAX || f->f_min == -DBL_MAX)
+ iub = +DBL_MAX;
+ else if (f->j_min == 0)
+ { if (a[k] > 0.0)
+ { xassert(l[k] != -DBL_MAX);
+ iub = U - (f->f_min - a[k] * l[k]);
+ }
+ else if (a[k] < 0.0)
+ { xassert(u[k] != +DBL_MAX);
+ iub = U - (f->f_min - a[k] * u[k]);
+ }
+ else
+ xassert(a != a);
+ }
+ else if (f->j_min == k)
+ iub = U - f->f_min;
+ else
+ iub = +DBL_MAX;
+ /* determine implied bounds of x[k] (16) and (17) */
+#if 1
+ /* do not use a[k] if it has small magnitude to prevent wrong
+ implied bounds; for example, 1e-15 * x1 >= x2 + x3, where
+ x1 >= -10, x2, x3 >= 0, would lead to wrong conclusion that
+ x1 >= 0 */
+ if (fabs(a[k]) < 1e-6)
+ *ll = -DBL_MAX, *uu = +DBL_MAX; else
+#endif
+ if (a[k] > 0.0)
+ { *ll = (ilb == -DBL_MAX ? -DBL_MAX : ilb / a[k]);
+ *uu = (iub == +DBL_MAX ? +DBL_MAX : iub / a[k]);
+ }
+ else if (a[k] < 0.0)
+ { *ll = (iub == +DBL_MAX ? -DBL_MAX : iub / a[k]);
+ *uu = (ilb == -DBL_MAX ? +DBL_MAX : ilb / a[k]);
+ }
+ else
+ xassert(a != a);
+ return;
+}
+
+/***********************************************************************
+* check_row_bounds - check and relax original row bounds
+*
+* Given a row (constraint)
+*
+* n
+* L <= sum a[j] * x[j] <= U
+* j=1
+*
+* and bounds of columns (variables)
+*
+* l[j] <= x[j] <= u[j]
+*
+* this routine checks the original row bounds L and U for feasibility
+* and redundancy. If the original lower bound L or/and upper bound U
+* cannot be active due to bounds of variables, the routine remove them
+* replacing by -inf or/and +inf, respectively.
+*
+* If no primal infeasibility is detected, the routine returns zero,
+* otherwise non-zero. */
+
+static int check_row_bounds(const struct f_info *f, double *L_,
+ double *U_)
+{ int ret = 0;
+ double L = *L_, U = *U_, LL, UU;
+ /* determine implied bounds of the row */
+ row_implied_bounds(f, &LL, &UU);
+ /* check if the original lower bound is infeasible */
+ if (L != -DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(L));
+ if (UU < L - eps)
+ { ret = 1;
+ goto done;
+ }
+ }
+ /* check if the original upper bound is infeasible */
+ if (U != +DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(U));
+ if (LL > U + eps)
+ { ret = 1;
+ goto done;
+ }
+ }
+ /* check if the original lower bound is redundant */
+ if (L != -DBL_MAX)
+ { double eps = 1e-12 * (1.0 + fabs(L));
+ if (LL > L - eps)
+ { /* it cannot be active, so remove it */
+ *L_ = -DBL_MAX;
+ }
+ }
+ /* check if the original upper bound is redundant */
+ if (U != +DBL_MAX)
+ { double eps = 1e-12 * (1.0 + fabs(U));
+ if (UU < U + eps)
+ { /* it cannot be active, so remove it */
+ *U_ = +DBL_MAX;
+ }
+ }
+done: return ret;
+}
+
+/***********************************************************************
+* check_col_bounds - check and tighten original column bounds
+*
+* Given a row (constraint)
+*
+* n
+* L <= sum a[j] * x[j] <= U
+* j=1
+*
+* and bounds of columns (variables)
+*
+* l[j] <= x[j] <= u[j]
+*
+* for column (variable) x[j] this routine checks the original column
+* bounds l[j] and u[j] for feasibility and redundancy. If the original
+* lower bound l[j] or/and upper bound u[j] cannot be active due to
+* bounds of the constraint and other variables, the routine tighten
+* them replacing by corresponding implied bounds, if possible.
+*
+* NOTE: It is assumed that if L != -inf, the row lower bound can be
+* active, and if U != +inf, the row upper bound can be active.
+*
+* The flag means that variable x[j] is required to be integer.
+*
+* New actual bounds for x[j] are stored in locations lj and uj.
+*
+* If no primal infeasibility is detected, the routine returns zero,
+* otherwise non-zero. */
+
+static int check_col_bounds(const struct f_info *f, int n,
+ const double a[], double L, double U, const double l[],
+ const double u[], int flag, int j, double *_lj, double *_uj)
+{ int ret = 0;
+ double lj, uj, ll, uu;
+ xassert(n >= 0);
+ xassert(1 <= j && j <= n);
+ lj = l[j], uj = u[j];
+ /* determine implied bounds of the column */
+ col_implied_bounds(f, n, a, L, U, l, u, j, &ll, &uu);
+ /* if x[j] is integral, round its implied bounds */
+ if (flag)
+ { if (ll != -DBL_MAX)
+ ll = (ll - floor(ll) < 1e-3 ? floor(ll) : ceil(ll));
+ if (uu != +DBL_MAX)
+ uu = (ceil(uu) - uu < 1e-3 ? ceil(uu) : floor(uu));
+ }
+ /* check if the original lower bound is infeasible */
+ if (lj != -DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(lj));
+ if (uu < lj - eps)
+ { ret = 1;
+ goto done;
+ }
+ }
+ /* check if the original upper bound is infeasible */
+ if (uj != +DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(uj));
+ if (ll > uj + eps)
+ { ret = 1;
+ goto done;
+ }
+ }
+ /* check if the original lower bound is redundant */
+ if (ll != -DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(ll));
+ if (lj < ll - eps)
+ { /* it cannot be active, so tighten it */
+ lj = ll;
+ }
+ }
+ /* check if the original upper bound is redundant */
+ if (uu != +DBL_MAX)
+ { double eps = 1e-3 * (1.0 + fabs(uu));
+ if (uj > uu + eps)
+ { /* it cannot be active, so tighten it */
+ uj = uu;
+ }
+ }
+ /* due to round-off errors it may happen that lj > uj (although
+ lj < uj + eps, since no primal infeasibility is detected), so
+ adjuct the new actual bounds to provide lj <= uj */
+ if (!(lj == -DBL_MAX || uj == +DBL_MAX))
+ { double t1 = fabs(lj), t2 = fabs(uj);
+ double eps = 1e-10 * (1.0 + (t1 <= t2 ? t1 : t2));
+ if (lj > uj - eps)
+ { if (lj == l[j])
+ uj = lj;
+ else if (uj == u[j])
+ lj = uj;
+ else if (t1 <= t2)
+ uj = lj;
+ else
+ lj = uj;
+ }
+ }
+ *_lj = lj, *_uj = uj;
+done: return ret;
+}
+
+/***********************************************************************
+* check_efficiency - check if change in column bounds is efficient
+*
+* Given the original bounds of a column l and u and its new actual
+* bounds l' and u' (possibly tighten by the routine check_col_bounds)
+* this routine checks if the change in the column bounds is efficient
+* enough. If so, the routine returns non-zero, otherwise zero.
+*
+* The flag means that the variable is required to be integer. */
+
+static int check_efficiency(int flag, double l, double u, double ll,
+ double uu)
+{ int eff = 0;
+ /* check efficiency for lower bound */
+ if (l < ll)
+ { if (flag || l == -DBL_MAX)
+ eff++;
+ else
+ { double r;
+ if (u == +DBL_MAX)
+ r = 1.0 + fabs(l);
+ else
+ r = 1.0 + (u - l);
+ if (ll - l >= 0.25 * r)
+ eff++;
+ }
+ }
+ /* check efficiency for upper bound */
+ if (u > uu)
+ { if (flag || u == +DBL_MAX)
+ eff++;
+ else
+ { double r;
+ if (l == -DBL_MAX)
+ r = 1.0 + fabs(u);
+ else
+ r = 1.0 + (u - l);
+ if (u - uu >= 0.25 * r)
+ eff++;
+ }
+ }
+ return eff;
+}
+
+/***********************************************************************
+* basic_preprocessing - perform basic preprocessing
+*
+* This routine performs basic preprocessing of the specified MIP that
+* includes relaxing some row bounds and tightening some column bounds.
+*
+* On entry the arrays L and U contains original row bounds, and the
+* arrays l and u contains original column bounds:
+*
+* L[0] is the lower bound of the objective row;
+* L[i], i = 1,...,m, is the lower bound of i-th row;
+* U[0] is the upper bound of the objective row;
+* U[i], i = 1,...,m, is the upper bound of i-th row;
+* l[0] is not used;
+* l[j], j = 1,...,n, is the lower bound of j-th column;
+* u[0] is not used;
+* u[j], j = 1,...,n, is the upper bound of j-th column.
+*
+* On exit the arrays L, U, l, and u contain new actual bounds of rows
+* and column in the same locations.
+*
+* The parameters nrs and num specify an initial list of rows to be
+* processed:
+*
+* nrs is the number of rows in the initial list, 0 <= nrs <= m+1;
+* num[0] is not used;
+* num[1,...,nrs] are row numbers (0 means the objective row).
+*
+* The parameter max_pass specifies the maximal number of times that
+* each row can be processed, max_pass > 0.
+*
+* If no primal infeasibility is detected, the routine returns zero,
+* otherwise non-zero. */
+
+static int basic_preprocessing(glp_prob *mip, double L[], double U[],
+ double l[], double u[], int nrs, const int num[], int max_pass)
+{ int m = mip->m;
+ int n = mip->n;
+ struct f_info f;
+ int i, j, k, len, size, ret = 0;
+ int *ind, *list, *mark, *pass;
+ double *val, *lb, *ub;
+ xassert(0 <= nrs && nrs <= m+1);
+ xassert(max_pass > 0);
+ /* allocate working arrays */
+ ind = xcalloc(1+n, sizeof(int));
+ list = xcalloc(1+m+1, sizeof(int));
+ mark = xcalloc(1+m+1, sizeof(int));
+ memset(&mark[0], 0, (m+1) * sizeof(int));
+ pass = xcalloc(1+m+1, sizeof(int));
+ memset(&pass[0], 0, (m+1) * sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ lb = xcalloc(1+n, sizeof(double));
+ ub = xcalloc(1+n, sizeof(double));
+ /* initialize the list of rows to be processed */
+ size = 0;
+ for (k = 1; k <= nrs; k++)
+ { i = num[k];
+ xassert(0 <= i && i <= m);
+ /* duplicate row numbers are not allowed */
+ xassert(!mark[i]);
+ list[++size] = i, mark[i] = 1;
+ }
+ xassert(size == nrs);
+ /* process rows in the list until it becomes empty */
+ while (size > 0)
+ { /* get a next row from the list */
+ i = list[size--], mark[i] = 0;
+ /* increase the row processing count */
+ pass[i]++;
+ /* if the row is free, skip it */
+ if (L[i] == -DBL_MAX && U[i] == +DBL_MAX) continue;
+ /* obtain coefficients of the row */
+ len = 0;
+ if (i == 0)
+ { for (j = 1; j <= n; j++)
+ { GLPCOL *col = mip->col[j];
+ if (col->coef != 0.0)
+ len++, ind[len] = j, val[len] = col->coef;
+ }
+ }
+ else
+ { GLPROW *row = mip->row[i];
+ GLPAIJ *aij;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ len++, ind[len] = aij->col->j, val[len] = aij->val;
+ }
+ /* determine lower and upper bounds of columns corresponding
+ to non-zero row coefficients */
+ for (k = 1; k <= len; k++)
+ j = ind[k], lb[k] = l[j], ub[k] = u[j];
+ /* prepare the row info to determine implied bounds */
+ prepare_row_info(len, val, lb, ub, &f);
+ /* check and relax bounds of the row */
+ if (check_row_bounds(&f, &L[i], &U[i]))
+ { /* the feasible region is empty */
+ ret = 1;
+ goto done;
+ }
+ /* if the row became free, drop it */
+ if (L[i] == -DBL_MAX && U[i] == +DBL_MAX) continue;
+ /* process columns having non-zero coefficients in the row */
+ for (k = 1; k <= len; k++)
+ { GLPCOL *col;
+ int flag, eff;
+ double ll, uu;
+ /* take a next column in the row */
+ j = ind[k], col = mip->col[j];
+ flag = col->kind != GLP_CV;
+ /* check and tighten bounds of the column */
+ if (check_col_bounds(&f, len, val, L[i], U[i], lb, ub,
+ flag, k, &ll, &uu))
+ { /* the feasible region is empty */
+ ret = 1;
+ goto done;
+ }
+ /* check if change in the column bounds is efficient */
+ eff = check_efficiency(flag, l[j], u[j], ll, uu);
+ /* set new actual bounds of the column */
+ l[j] = ll, u[j] = uu;
+ /* if the change is efficient, add all rows affected by the
+ corresponding column, to the list */
+ if (eff > 0)
+ { GLPAIJ *aij;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ { int ii = aij->row->i;
+ /* if the row was processed maximal number of times,
+ skip it */
+ if (pass[ii] >= max_pass) continue;
+ /* if the row is free, skip it */
+ if (L[ii] == -DBL_MAX && U[ii] == +DBL_MAX) continue;
+ /* put the row into the list */
+ if (mark[ii] == 0)
+ { xassert(size <= m);
+ list[++size] = ii, mark[ii] = 1;
+ }
+ }
+ }
+ }
+ }
+done: /* free working arrays */
+ xfree(ind);
+ xfree(list);
+ xfree(mark);
+ xfree(pass);
+ xfree(val);
+ xfree(lb);
+ xfree(ub);
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_preprocess_node - preprocess current subproblem
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_preprocess_node(glp_tree *tree, int max_pass);
+*
+* DESCRIPTION
+*
+* The routine ios_preprocess_node performs basic preprocessing of the
+* current subproblem.
+*
+* RETURNS
+*
+* If no primal infeasibility is detected, the routine returns zero,
+* otherwise non-zero. */
+
+int ios_preprocess_node(glp_tree *tree, int max_pass)
+{ glp_prob *mip = tree->mip;
+ int m = mip->m;
+ int n = mip->n;
+ int i, j, nrs, *num, ret = 0;
+ double *L, *U, *l, *u;
+ /* the current subproblem must exist */
+ xassert(tree->curr != NULL);
+ /* determine original row bounds */
+ L = xcalloc(1+m, sizeof(double));
+ U = xcalloc(1+m, sizeof(double));
+ switch (mip->mip_stat)
+ { case GLP_UNDEF:
+ L[0] = -DBL_MAX, U[0] = +DBL_MAX;
+ break;
+ case GLP_FEAS:
+ switch (mip->dir)
+ { case GLP_MIN:
+ L[0] = -DBL_MAX, U[0] = mip->mip_obj - mip->c0;
+ break;
+ case GLP_MAX:
+ L[0] = mip->mip_obj - mip->c0, U[0] = +DBL_MAX;
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ break;
+ default:
+ xassert(mip != mip);
+ }
+ for (i = 1; i <= m; i++)
+ { L[i] = glp_get_row_lb(mip, i);
+ U[i] = glp_get_row_ub(mip, i);
+ }
+ /* determine original column bounds */
+ l = xcalloc(1+n, sizeof(double));
+ u = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++)
+ { l[j] = glp_get_col_lb(mip, j);
+ u[j] = glp_get_col_ub(mip, j);
+ }
+ /* build the initial list of rows to be analyzed */
+ nrs = m + 1;
+ num = xcalloc(1+nrs, sizeof(int));
+ for (i = 1; i <= nrs; i++) num[i] = i - 1;
+ /* perform basic preprocessing */
+ if (basic_preprocessing(mip , L, U, l, u, nrs, num, max_pass))
+ { ret = 1;
+ goto done;
+ }
+ /* set new actual (relaxed) row bounds */
+ for (i = 1; i <= m; i++)
+ { /* consider only non-active rows to keep dual feasibility */
+ if (glp_get_row_stat(mip, i) == GLP_BS)
+ { if (L[i] == -DBL_MAX && U[i] == +DBL_MAX)
+ glp_set_row_bnds(mip, i, GLP_FR, 0.0, 0.0);
+ else if (U[i] == +DBL_MAX)
+ glp_set_row_bnds(mip, i, GLP_LO, L[i], 0.0);
+ else if (L[i] == -DBL_MAX)
+ glp_set_row_bnds(mip, i, GLP_UP, 0.0, U[i]);
+ }
+ }
+ /* set new actual (tightened) column bounds */
+ for (j = 1; j <= n; j++)
+ { int type;
+ if (l[j] == -DBL_MAX && u[j] == +DBL_MAX)
+ type = GLP_FR;
+ else if (u[j] == +DBL_MAX)
+ type = GLP_LO;
+ else if (l[j] == -DBL_MAX)
+ type = GLP_UP;
+ else if (l[j] != u[j])
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_col_bnds(mip, j, type, l[j], u[j]);
+ }
+done: /* free working arrays and return */
+ xfree(L);
+ xfree(U);
+ xfree(l);
+ xfree(u);
+ xfree(num);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios03.c b/test/monniaux/glpk-4.65/src/draft/glpios03.c
new file mode 100644
index 00000000..21d6a000
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios03.c
@@ -0,0 +1,1512 @@
+/* glpios03.c (branch-and-cut driver) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* show_progress - display current progress of the search
+*
+* This routine displays some information about current progress of the
+* search.
+*
+* The information includes:
+*
+* the current number of iterations performed by the simplex solver;
+*
+* the objective value for the best known integer feasible solution,
+* which is upper (minimization) or lower (maximization) global bound
+* for optimal solution of the original mip problem;
+*
+* the best local bound for active nodes, which is lower (minimization)
+* or upper (maximization) global bound for optimal solution of the
+* original mip problem;
+*
+* the relative mip gap, in percents;
+*
+* the number of open (active) subproblems;
+*
+* the number of completely explored subproblems, i.e. whose nodes have
+* been removed from the tree. */
+
+static void show_progress(glp_tree *T, int bingo)
+{ int p;
+ double temp;
+ char best_mip[50], best_bound[50], *rho, rel_gap[50];
+ /* format the best known integer feasible solution */
+ if (T->mip->mip_stat == GLP_FEAS)
+ sprintf(best_mip, "%17.9e", T->mip->mip_obj);
+ else
+ sprintf(best_mip, "%17s", "not found yet");
+ /* determine reference number of an active subproblem whose local
+ bound is best */
+ p = ios_best_node(T);
+ /* format the best bound */
+ if (p == 0)
+ sprintf(best_bound, "%17s", "tree is empty");
+ else
+ { temp = T->slot[p].node->bound;
+ if (temp == -DBL_MAX)
+ sprintf(best_bound, "%17s", "-inf");
+ else if (temp == +DBL_MAX)
+ sprintf(best_bound, "%17s", "+inf");
+ else
+ { if (fabs(temp) < 1e-9)
+ temp = 0;
+ sprintf(best_bound, "%17.9e", temp);
+ }
+ }
+ /* choose the relation sign between global bounds */
+ if (T->mip->dir == GLP_MIN)
+ rho = ">=";
+ else if (T->mip->dir == GLP_MAX)
+ rho = "<=";
+ else
+ xassert(T != T);
+ /* format the relative mip gap */
+ temp = ios_relative_gap(T);
+ if (temp == 0.0)
+ sprintf(rel_gap, " 0.0%%");
+ else if (temp < 0.001)
+ sprintf(rel_gap, "< 0.1%%");
+ else if (temp <= 9.999)
+ sprintf(rel_gap, "%5.1f%%", 100.0 * temp);
+ else
+ sprintf(rel_gap, "%6s", "");
+ /* display progress of the search */
+ xprintf("+%6d: %s %s %s %s %s (%d; %d)\n",
+ T->mip->it_cnt, bingo ? ">>>>>" : "mip =", best_mip, rho,
+ best_bound, rel_gap, T->a_cnt, T->t_cnt - T->n_cnt);
+ T->tm_lag = xtime();
+ return;
+}
+
+/***********************************************************************
+* is_branch_hopeful - check if specified branch is hopeful
+*
+* This routine checks if the specified subproblem can have an integer
+* optimal solution which is better than the best known one.
+*
+* The check is based on comparison of the local objective bound stored
+* in the subproblem descriptor and the incumbent objective value which
+* is the global objective bound.
+*
+* If there is a chance that the specified subproblem can have a better
+* integer optimal solution, the routine returns non-zero. Otherwise, if
+* the corresponding branch can pruned, zero is returned. */
+
+static int is_branch_hopeful(glp_tree *T, int p)
+{ xassert(1 <= p && p <= T->nslots);
+ xassert(T->slot[p].node != NULL);
+ return ios_is_hopeful(T, T->slot[p].node->bound);
+}
+
+/***********************************************************************
+* check_integrality - check integrality of basic solution
+*
+* This routine checks if the basic solution of LP relaxation of the
+* current subproblem satisfies to integrality conditions, i.e. that all
+* variables of integer kind have integral primal values. (The solution
+* is assumed to be optimal.)
+*
+* For each variable of integer kind the routine computes the following
+* quantity:
+*
+* ii(x[j]) = min(x[j] - floor(x[j]), ceil(x[j]) - x[j]), (1)
+*
+* which is a measure of the integer infeasibility (non-integrality) of
+* x[j] (for example, ii(2.1) = 0.1, ii(3.7) = 0.3, ii(5.0) = 0). It is
+* understood that 0 <= ii(x[j]) <= 0.5, and variable x[j] is integer
+* feasible if ii(x[j]) = 0. However, due to floating-point arithmetic
+* the routine checks less restrictive condition:
+*
+* ii(x[j]) <= tol_int, (2)
+*
+* where tol_int is a given tolerance (small positive number) and marks
+* each variable which does not satisfy to (2) as integer infeasible by
+* setting its fractionality flag.
+*
+* In order to characterize integer infeasibility of the basic solution
+* in the whole the routine computes two parameters: ii_cnt, which is
+* the number of variables with the fractionality flag set, and ii_sum,
+* which is the sum of integer infeasibilities (1). */
+
+static void check_integrality(glp_tree *T)
+{ glp_prob *mip = T->mip;
+ int j, type, ii_cnt = 0;
+ double lb, ub, x, temp1, temp2, ii_sum = 0.0;
+ /* walk through the set of columns (structural variables) */
+ for (j = 1; j <= mip->n; j++)
+ { GLPCOL *col = mip->col[j];
+ T->non_int[j] = 0;
+ /* if the column is not integer, skip it */
+ if (col->kind != GLP_IV) continue;
+ /* if the column is non-basic, it is integer feasible */
+ if (col->stat != GLP_BS) continue;
+ /* obtain the type and bounds of the column */
+ type = col->type, lb = col->lb, ub = col->ub;
+ /* obtain value of the column in optimal basic solution */
+ x = col->prim;
+ /* if the column's primal value is close to the lower bound,
+ the column is integer feasible within given tolerance */
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { temp1 = lb - T->parm->tol_int;
+ temp2 = lb + T->parm->tol_int;
+ if (temp1 <= x && x <= temp2) continue;
+#if 0
+ /* the lower bound must not be violated */
+ xassert(x >= lb);
+#else
+ if (x < lb) continue;
+#endif
+ }
+ /* if the column's primal value is close to the upper bound,
+ the column is integer feasible within given tolerance */
+ if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ { temp1 = ub - T->parm->tol_int;
+ temp2 = ub + T->parm->tol_int;
+ if (temp1 <= x && x <= temp2) continue;
+#if 0
+ /* the upper bound must not be violated */
+ xassert(x <= ub);
+#else
+ if (x > ub) continue;
+#endif
+ }
+ /* if the column's primal value is close to nearest integer,
+ the column is integer feasible within given tolerance */
+ temp1 = floor(x + 0.5) - T->parm->tol_int;
+ temp2 = floor(x + 0.5) + T->parm->tol_int;
+ if (temp1 <= x && x <= temp2) continue;
+ /* otherwise the column is integer infeasible */
+ T->non_int[j] = 1;
+ /* increase the number of fractional-valued columns */
+ ii_cnt++;
+ /* compute the sum of integer infeasibilities */
+ temp1 = x - floor(x);
+ temp2 = ceil(x) - x;
+ xassert(temp1 > 0.0 && temp2 > 0.0);
+ ii_sum += (temp1 <= temp2 ? temp1 : temp2);
+ }
+ /* store ii_cnt and ii_sum to the current problem descriptor */
+ xassert(T->curr != NULL);
+ T->curr->ii_cnt = ii_cnt;
+ T->curr->ii_sum = ii_sum;
+ /* and also display these parameters */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ { if (ii_cnt == 0)
+ xprintf("There are no fractional columns\n");
+ else if (ii_cnt == 1)
+ xprintf("There is one fractional column, integer infeasibil"
+ "ity is %.3e\n", ii_sum);
+ else
+ xprintf("There are %d fractional columns, integer infeasibi"
+ "lity is %.3e\n", ii_cnt, ii_sum);
+ }
+ return;
+}
+
+/***********************************************************************
+* record_solution - record better integer feasible solution
+*
+* This routine records optimal basic solution of LP relaxation of the
+* current subproblem, which being integer feasible is better than the
+* best known integer feasible solution. */
+
+static void record_solution(glp_tree *T)
+{ glp_prob *mip = T->mip;
+ int i, j;
+ mip->mip_stat = GLP_FEAS;
+ mip->mip_obj = mip->obj_val;
+ for (i = 1; i <= mip->m; i++)
+ { GLPROW *row = mip->row[i];
+ row->mipx = row->prim;
+ }
+ for (j = 1; j <= mip->n; j++)
+ { GLPCOL *col = mip->col[j];
+ if (col->kind == GLP_CV)
+ col->mipx = col->prim;
+ else if (col->kind == GLP_IV)
+ { /* value of the integer column must be integral */
+ col->mipx = floor(col->prim + 0.5);
+ }
+ else
+ xassert(col != col);
+ }
+ T->sol_cnt++;
+ return;
+}
+
+/***********************************************************************
+* fix_by_red_cost - fix non-basic integer columns by reduced costs
+*
+* This routine fixes some non-basic integer columns if their reduced
+* costs indicate that increasing (decreasing) the column at least by
+* one involves the objective value becoming worse than the incumbent
+* objective value. */
+
+static void fix_by_red_cost(glp_tree *T)
+{ glp_prob *mip = T->mip;
+ int j, stat, fixed = 0;
+ double obj, lb, ub, dj;
+ /* the global bound must exist */
+ xassert(T->mip->mip_stat == GLP_FEAS);
+ /* basic solution of LP relaxation must be optimal */
+ xassert(mip->pbs_stat == GLP_FEAS && mip->dbs_stat == GLP_FEAS);
+ /* determine the objective function value */
+ obj = mip->obj_val;
+ /* walk through the column list */
+ for (j = 1; j <= mip->n; j++)
+ { GLPCOL *col = mip->col[j];
+ /* if the column is not integer, skip it */
+ if (col->kind != GLP_IV) continue;
+ /* obtain bounds of j-th column */
+ lb = col->lb, ub = col->ub;
+ /* and determine its status and reduced cost */
+ stat = col->stat, dj = col->dual;
+ /* analyze the reduced cost */
+ switch (mip->dir)
+ { case GLP_MIN:
+ /* minimization */
+ if (stat == GLP_NL)
+ { /* j-th column is non-basic on its lower bound */
+ if (dj < 0.0) dj = 0.0;
+ if (obj + dj >= mip->mip_obj)
+ glp_set_col_bnds(mip, j, GLP_FX, lb, lb), fixed++;
+ }
+ else if (stat == GLP_NU)
+ { /* j-th column is non-basic on its upper bound */
+ if (dj > 0.0) dj = 0.0;
+ if (obj - dj >= mip->mip_obj)
+ glp_set_col_bnds(mip, j, GLP_FX, ub, ub), fixed++;
+ }
+ break;
+ case GLP_MAX:
+ /* maximization */
+ if (stat == GLP_NL)
+ { /* j-th column is non-basic on its lower bound */
+ if (dj > 0.0) dj = 0.0;
+ if (obj + dj <= mip->mip_obj)
+ glp_set_col_bnds(mip, j, GLP_FX, lb, lb), fixed++;
+ }
+ else if (stat == GLP_NU)
+ { /* j-th column is non-basic on its upper bound */
+ if (dj < 0.0) dj = 0.0;
+ if (obj - dj <= mip->mip_obj)
+ glp_set_col_bnds(mip, j, GLP_FX, ub, ub), fixed++;
+ }
+ break;
+ default:
+ xassert(T != T);
+ }
+ }
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ { if (fixed == 0)
+ /* nothing to say */;
+ else if (fixed == 1)
+ xprintf("One column has been fixed by reduced cost\n");
+ else
+ xprintf("%d columns have been fixed by reduced costs\n",
+ fixed);
+ }
+ /* fixing non-basic columns on their current bounds does not
+ change the basic solution */
+ xassert(mip->pbs_stat == GLP_FEAS && mip->dbs_stat == GLP_FEAS);
+ return;
+}
+
+/***********************************************************************
+* branch_on - perform branching on specified variable
+*
+* This routine performs branching on j-th column (structural variable)
+* of the current subproblem. The specified column must be of integer
+* kind and must have a fractional value in optimal basic solution of
+* LP relaxation of the current subproblem (i.e. only columns for which
+* the flag non_int[j] is set are valid candidates to branch on).
+*
+* Let x be j-th structural variable, and beta be its primal fractional
+* value in the current basic solution. Branching on j-th variable is
+* dividing the current subproblem into two new subproblems, which are
+* identical to the current subproblem with the following exception: in
+* the first subproblem that begins the down-branch x has a new upper
+* bound x <= floor(beta), and in the second subproblem that begins the
+* up-branch x has a new lower bound x >= ceil(beta).
+*
+* Depending on estimation of local bounds for down- and up-branches
+* this routine returns the following:
+*
+* 0 - both branches have been created;
+* 1 - one branch is hopeless and has been pruned, so now the current
+* subproblem is other branch;
+* 2 - both branches are hopeless and have been pruned; new subproblem
+* selection is needed to continue the search. */
+
+static int branch_on(glp_tree *T, int j, int next)
+{ glp_prob *mip = T->mip;
+ IOSNPD *node;
+ int m = mip->m;
+ int n = mip->n;
+ int type, dn_type, up_type, dn_bad, up_bad, p, ret, clone[1+2];
+ double lb, ub, beta, new_ub, new_lb, dn_lp, up_lp, dn_bnd, up_bnd;
+ /* determine bounds and value of x[j] in optimal solution to LP
+ relaxation of the current subproblem */
+ xassert(1 <= j && j <= n);
+ type = mip->col[j]->type;
+ lb = mip->col[j]->lb;
+ ub = mip->col[j]->ub;
+ beta = mip->col[j]->prim;
+ /* determine new bounds of x[j] for down- and up-branches */
+ new_ub = floor(beta);
+ new_lb = ceil(beta);
+ switch (type)
+ { case GLP_FR:
+ dn_type = GLP_UP;
+ up_type = GLP_LO;
+ break;
+ case GLP_LO:
+ xassert(lb <= new_ub);
+ dn_type = (lb == new_ub ? GLP_FX : GLP_DB);
+ xassert(lb + 1.0 <= new_lb);
+ up_type = GLP_LO;
+ break;
+ case GLP_UP:
+ xassert(new_ub <= ub - 1.0);
+ dn_type = GLP_UP;
+ xassert(new_lb <= ub);
+ up_type = (new_lb == ub ? GLP_FX : GLP_DB);
+ break;
+ case GLP_DB:
+ xassert(lb <= new_ub && new_ub <= ub - 1.0);
+ dn_type = (lb == new_ub ? GLP_FX : GLP_DB);
+ xassert(lb + 1.0 <= new_lb && new_lb <= ub);
+ up_type = (new_lb == ub ? GLP_FX : GLP_DB);
+ break;
+ default:
+ xassert(type != type);
+ }
+ /* compute local bounds to LP relaxation for both branches */
+ ios_eval_degrad(T, j, &dn_lp, &up_lp);
+ /* and improve them by rounding */
+ dn_bnd = ios_round_bound(T, dn_lp);
+ up_bnd = ios_round_bound(T, up_lp);
+ /* check local bounds for down- and up-branches */
+ dn_bad = !ios_is_hopeful(T, dn_bnd);
+ up_bad = !ios_is_hopeful(T, up_bnd);
+ if (dn_bad && up_bad)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Both down- and up-branches are hopeless\n");
+ ret = 2;
+ goto done;
+ }
+ else if (up_bad)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Up-branch is hopeless\n");
+ glp_set_col_bnds(mip, j, dn_type, lb, new_ub);
+ T->curr->lp_obj = dn_lp;
+ if (mip->dir == GLP_MIN)
+ { if (T->curr->bound < dn_bnd)
+ T->curr->bound = dn_bnd;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (T->curr->bound > dn_bnd)
+ T->curr->bound = dn_bnd;
+ }
+ else
+ xassert(mip != mip);
+ ret = 1;
+ goto done;
+ }
+ else if (dn_bad)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Down-branch is hopeless\n");
+ glp_set_col_bnds(mip, j, up_type, new_lb, ub);
+ T->curr->lp_obj = up_lp;
+ if (mip->dir == GLP_MIN)
+ { if (T->curr->bound < up_bnd)
+ T->curr->bound = up_bnd;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (T->curr->bound > up_bnd)
+ T->curr->bound = up_bnd;
+ }
+ else
+ xassert(mip != mip);
+ ret = 1;
+ goto done;
+ }
+ /* both down- and up-branches seem to be hopeful */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Branching on column %d, primal value is %.9e\n",
+ j, beta);
+ /* determine the reference number of the current subproblem */
+ xassert(T->curr != NULL);
+ p = T->curr->p;
+ T->curr->br_var = j;
+ T->curr->br_val = beta;
+ /* freeze the current subproblem */
+ ios_freeze_node(T);
+ /* create two clones of the current subproblem; the first clone
+ begins the down-branch, the second one begins the up-branch */
+ ios_clone_node(T, p, 2, clone);
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Node %d begins down branch, node %d begins up branch "
+ "\n", clone[1], clone[2]);
+ /* set new upper bound of j-th column in the down-branch */
+ node = T->slot[clone[1]].node;
+ xassert(node != NULL);
+ xassert(node->up != NULL);
+ xassert(node->b_ptr == NULL);
+ node->b_ptr = dmp_get_atom(T->pool, sizeof(IOSBND));
+ node->b_ptr->k = m + j;
+ node->b_ptr->type = (unsigned char)dn_type;
+ node->b_ptr->lb = lb;
+ node->b_ptr->ub = new_ub;
+ node->b_ptr->next = NULL;
+ node->lp_obj = dn_lp;
+ if (mip->dir == GLP_MIN)
+ { if (node->bound < dn_bnd)
+ node->bound = dn_bnd;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (node->bound > dn_bnd)
+ node->bound = dn_bnd;
+ }
+ else
+ xassert(mip != mip);
+ /* set new lower bound of j-th column in the up-branch */
+ node = T->slot[clone[2]].node;
+ xassert(node != NULL);
+ xassert(node->up != NULL);
+ xassert(node->b_ptr == NULL);
+ node->b_ptr = dmp_get_atom(T->pool, sizeof(IOSBND));
+ node->b_ptr->k = m + j;
+ node->b_ptr->type = (unsigned char)up_type;
+ node->b_ptr->lb = new_lb;
+ node->b_ptr->ub = ub;
+ node->b_ptr->next = NULL;
+ node->lp_obj = up_lp;
+ if (mip->dir == GLP_MIN)
+ { if (node->bound < up_bnd)
+ node->bound = up_bnd;
+ }
+ else if (mip->dir == GLP_MAX)
+ { if (node->bound > up_bnd)
+ node->bound = up_bnd;
+ }
+ else
+ xassert(mip != mip);
+ /* suggest the subproblem to be solved next */
+ xassert(T->child == 0);
+ if (next == GLP_NO_BRNCH)
+ T->child = 0;
+ else if (next == GLP_DN_BRNCH)
+ T->child = clone[1];
+ else if (next == GLP_UP_BRNCH)
+ T->child = clone[2];
+ else
+ xassert(next != next);
+ ret = 0;
+done: return ret;
+}
+
+/***********************************************************************
+* cleanup_the_tree - prune hopeless branches from the tree
+*
+* This routine walks through the active list and checks the local
+* bound for every active subproblem. If the local bound indicates that
+* the subproblem cannot have integer optimal solution better than the
+* incumbent objective value, the routine deletes such subproblem that,
+* in turn, involves pruning the corresponding branch of the tree. */
+
+static void cleanup_the_tree(glp_tree *T)
+{ IOSNPD *node, *next_node;
+ int count = 0;
+ /* the global bound must exist */
+ xassert(T->mip->mip_stat == GLP_FEAS);
+ /* walk through the list of active subproblems */
+ for (node = T->head; node != NULL; node = next_node)
+ { /* deleting some active problem node may involve deleting its
+ parents recursively; however, all its parents being created
+ *before* it are always *precede* it in the node list, so
+ the next problem node is never affected by such deletion */
+ next_node = node->next;
+ /* if the branch is hopeless, prune it */
+ if (!is_branch_hopeful(T, node->p))
+ ios_delete_node(T, node->p), count++;
+ }
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ { if (count == 1)
+ xprintf("One hopeless branch has been pruned\n");
+ else if (count > 1)
+ xprintf("%d hopeless branches have been pruned\n", count);
+ }
+ return;
+}
+
+/***********************************************************************
+* round_heur - simple rounding heuristic
+*
+* This routine attempts to guess an integer feasible solution by
+* simple rounding values of all integer variables in basic solution to
+* nearest integers. */
+
+static int round_heur(glp_tree *T)
+{ glp_prob *P = T->mip;
+ /*int m = P->m;*/
+ int n = P->n;
+ int i, j, ret;
+ double *x;
+ /* compute rounded values of variables */
+ x = talloc(1+n, double);
+ for (j = 1; j <= n; j++)
+ { GLPCOL *col = P->col[j];
+ if (col->kind == GLP_IV)
+ { /* integer variable */
+ x[j] = floor(col->prim + 0.5);
+ }
+ else if (col->type == GLP_FX)
+ { /* fixed variable */
+ x[j] = col->prim;
+ }
+ else
+ { /* non-integer non-fixed variable */
+ ret = 3;
+ goto done;
+ }
+ }
+ /* check that no constraints are violated */
+ for (i = 1; i <= T->orig_m; i++)
+ { int type = T->orig_type[i];
+ GLPAIJ *aij;
+ double sum;
+ if (type == GLP_FR)
+ continue;
+ /* compute value of linear form */
+ sum = 0.0;
+ for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ sum += aij->val * x[aij->col->j];
+ /* check lower bound */
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { if (sum < T->orig_lb[i] - 1e-9)
+ { /* lower bound is violated */
+ ret = 2;
+ goto done;
+ }
+ }
+ /* check upper bound */
+ if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ { if (sum > T->orig_ub[i] + 1e-9)
+ { /* upper bound is violated */
+ ret = 2;
+ goto done;
+ }
+ }
+ }
+ /* rounded solution is integer feasible */
+ if (glp_ios_heur_sol(T, x) == 0)
+ { /* solution is accepted */
+ ret = 0;
+ }
+ else
+ { /* solution is rejected */
+ ret = 1;
+ }
+done: tfree(x);
+ return ret;
+}
+
+/**********************************************************************/
+
+#if 1 /* 08/III-2016 */
+static void gmi_gen(glp_tree *T)
+{ /* generate Gomory's mixed integer cuts */
+ glp_prob *P, *pool;
+ P = T->mip;
+ pool = glp_create_prob();
+ glp_add_cols(pool, P->n);
+ glp_gmi_gen(P, pool, 50);
+ if (pool->m > 0)
+ { int i, len, *ind;
+ double *val;
+ ind = xcalloc(1+P->n, sizeof(int));
+ val = xcalloc(1+P->n, sizeof(double));
+ for (i = 1; i <= pool->m; i++)
+ { len = glp_get_mat_row(pool, i, ind, val);
+ glp_ios_add_row(T, NULL, GLP_RF_GMI, 0, len, ind, val,
+ GLP_LO, pool->row[i]->lb);
+ }
+ xfree(ind);
+ xfree(val);
+ }
+ glp_delete_prob(pool);
+ return;
+}
+#endif
+
+#ifdef NEW_COVER /* 13/II-2018 */
+static void cov_gen(glp_tree *T)
+{ /* generate cover cuts */
+ glp_prob *P, *pool;
+ if (T->cov_gen == NULL)
+ return;
+ P = T->mip;
+ pool = glp_create_prob();
+ glp_add_cols(pool, P->n);
+ glp_cov_gen1(P, T->cov_gen, pool);
+ if (pool->m > 0)
+ { int i, len, *ind;
+ double *val;
+ ind = xcalloc(1+P->n, sizeof(int));
+ val = xcalloc(1+P->n, sizeof(double));
+ for (i = 1; i <= pool->m; i++)
+ { len = glp_get_mat_row(pool, i, ind, val);
+ glp_ios_add_row(T, NULL, GLP_RF_COV, 0, len, ind, val,
+ GLP_UP, pool->row[i]->ub);
+ }
+ xfree(ind);
+ xfree(val);
+ }
+ glp_delete_prob(pool);
+ return;
+}
+#endif
+
+#if 1 /* 08/III-2016 */
+static void mir_gen(glp_tree *T)
+{ /* generate mixed integer rounding cuts */
+ glp_prob *P, *pool;
+ P = T->mip;
+ pool = glp_create_prob();
+ glp_add_cols(pool, P->n);
+ glp_mir_gen(P, T->mir_gen, pool);
+ if (pool->m > 0)
+ { int i, len, *ind;
+ double *val;
+ ind = xcalloc(1+P->n, sizeof(int));
+ val = xcalloc(1+P->n, sizeof(double));
+ for (i = 1; i <= pool->m; i++)
+ { len = glp_get_mat_row(pool, i, ind, val);
+ glp_ios_add_row(T, NULL, GLP_RF_MIR, 0, len, ind, val,
+ GLP_UP, pool->row[i]->ub);
+ }
+ xfree(ind);
+ xfree(val);
+ }
+ glp_delete_prob(pool);
+ return;
+}
+#endif
+
+#if 1 /* 08/III-2016 */
+static void clq_gen(glp_tree *T, glp_cfg *G)
+{ /* generate clique cut from conflict graph */
+ glp_prob *P = T->mip;
+ int n = P->n;
+ int len, *ind;
+ double *val;
+ ind = talloc(1+n, int);
+ val = talloc(1+n, double);
+ len = glp_clq_cut(T->mip, G, ind, val);
+ if (len > 0)
+ glp_ios_add_row(T, NULL, GLP_RF_CLQ, 0, len, ind, val, GLP_UP,
+ val[0]);
+ tfree(ind);
+ tfree(val);
+ return;
+}
+#endif
+
+static void generate_cuts(glp_tree *T)
+{ /* generate generic cuts with built-in generators */
+ if (!(T->parm->mir_cuts == GLP_ON ||
+ T->parm->gmi_cuts == GLP_ON ||
+ T->parm->cov_cuts == GLP_ON ||
+ T->parm->clq_cuts == GLP_ON)) goto done;
+#if 1 /* 20/IX-2008 */
+ { int i, max_cuts, added_cuts;
+ max_cuts = T->n;
+ if (max_cuts < 1000) max_cuts = 1000;
+ added_cuts = 0;
+ for (i = T->orig_m+1; i <= T->mip->m; i++)
+ { if (T->mip->row[i]->origin == GLP_RF_CUT)
+ added_cuts++;
+ }
+ /* xprintf("added_cuts = %d\n", added_cuts); */
+ if (added_cuts >= max_cuts) goto done;
+ }
+#endif
+ /* generate and add to POOL all cuts violated by x* */
+ if (T->parm->gmi_cuts == GLP_ON)
+ { if (T->curr->changed < 7)
+#if 0 /* 08/III-2016 */
+ ios_gmi_gen(T);
+#else
+ gmi_gen(T);
+#endif
+ }
+ if (T->parm->mir_cuts == GLP_ON)
+ { xassert(T->mir_gen != NULL);
+#if 0 /* 08/III-2016 */
+ ios_mir_gen(T, T->mir_gen);
+#else
+ mir_gen(T);
+#endif
+ }
+ if (T->parm->cov_cuts == GLP_ON)
+ { /* cover cuts works well along with mir cuts */
+#ifdef NEW_COVER /* 13/II-2018 */
+ cov_gen(T);
+#else
+ ios_cov_gen(T);
+#endif
+ }
+ if (T->parm->clq_cuts == GLP_ON)
+ { if (T->clq_gen != NULL)
+#if 0 /* 29/VI-2013 */
+ { if (T->curr->level == 0 && T->curr->changed < 50 ||
+ T->curr->level > 0 && T->curr->changed < 5)
+#else /* FIXME */
+ { if (T->curr->level == 0 && T->curr->changed < 500 ||
+ T->curr->level > 0 && T->curr->changed < 50)
+#endif
+#if 0 /* 08/III-2016 */
+ ios_clq_gen(T, T->clq_gen);
+#else
+ clq_gen(T, T->clq_gen);
+#endif
+ }
+ }
+done: return;
+}
+
+/**********************************************************************/
+
+static void remove_cuts(glp_tree *T)
+{ /* remove inactive cuts (some valueable globally valid cut might
+ be saved in the global cut pool) */
+ int i, cnt = 0, *num = NULL;
+ xassert(T->curr != NULL);
+ for (i = T->orig_m+1; i <= T->mip->m; i++)
+ { if (T->mip->row[i]->origin == GLP_RF_CUT &&
+ T->mip->row[i]->level == T->curr->level &&
+ T->mip->row[i]->stat == GLP_BS)
+ { if (num == NULL)
+ num = xcalloc(1+T->mip->m, sizeof(int));
+ num[++cnt] = i;
+ }
+ }
+ if (cnt > 0)
+ { glp_del_rows(T->mip, cnt, num);
+#if 0
+ xprintf("%d inactive cut(s) removed\n", cnt);
+#endif
+ xfree(num);
+ xassert(glp_factorize(T->mip) == 0);
+ }
+ return;
+}
+
+/**********************************************************************/
+
+static void display_cut_info(glp_tree *T)
+{ glp_prob *mip = T->mip;
+ int i, gmi = 0, mir = 0, cov = 0, clq = 0, app = 0;
+ for (i = mip->m; i > 0; i--)
+ { GLPROW *row;
+ row = mip->row[i];
+ /* if (row->level < T->curr->level) break; */
+ if (row->origin == GLP_RF_CUT)
+ { if (row->klass == GLP_RF_GMI)
+ gmi++;
+ else if (row->klass == GLP_RF_MIR)
+ mir++;
+ else if (row->klass == GLP_RF_COV)
+ cov++;
+ else if (row->klass == GLP_RF_CLQ)
+ clq++;
+ else
+ app++;
+ }
+ }
+ xassert(T->curr != NULL);
+ if (gmi + mir + cov + clq + app > 0)
+ { xprintf("Cuts on level %d:", T->curr->level);
+ if (gmi > 0) xprintf(" gmi = %d;", gmi);
+ if (mir > 0) xprintf(" mir = %d;", mir);
+ if (cov > 0) xprintf(" cov = %d;", cov);
+ if (clq > 0) xprintf(" clq = %d;", clq);
+ if (app > 0) xprintf(" app = %d;", app);
+ xprintf("\n");
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_driver - branch-and-cut driver
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_driver(glp_tree *T);
+*
+* DESCRIPTION
+*
+* The routine ios_driver is a branch-and-cut driver. It controls the
+* MIP solution process.
+*
+* RETURNS
+*
+* 0 The MIP problem instance has been successfully solved. This code
+* does not necessarily mean that the solver has found optimal
+* solution. It only means that the solution process was successful.
+*
+* GLP_EFAIL
+* The search was prematurely terminated due to the solver failure.
+*
+* GLP_EMIPGAP
+* The search was prematurely terminated, because the relative mip
+* gap tolerance has been reached.
+*
+* GLP_ETMLIM
+* The search was prematurely terminated, because the time limit has
+* been exceeded.
+*
+* GLP_ESTOP
+* The search was prematurely terminated by application. */
+
+int ios_driver(glp_tree *T)
+{ int p, curr_p, p_stat, d_stat, ret;
+#if 1 /* carry out to glp_tree */
+ int pred_p = 0;
+ /* if the current subproblem has been just created due to
+ branching, pred_p is the reference number of its parent
+ subproblem, otherwise pred_p is zero */
+#endif
+#if 1 /* 18/VII-2013 */
+ int bad_cut;
+ double old_obj;
+#endif
+#if 0 /* 10/VI-2013 */
+ glp_long ttt = T->tm_beg;
+#else
+ double ttt = T->tm_beg;
+#endif
+#if 1 /* 27/II-2016 by Chris */
+ int root_done = 0;
+#endif
+#if 0
+ ((glp_iocp *)T->parm)->msg_lev = GLP_MSG_DBG;
+#endif
+#if 1 /* 16/III-2016 */
+ if (((glp_iocp *)T->parm)->flip)
+#if 0 /* 20/I-2018 */
+ xprintf("WARNING: LONG-STEP DUAL SIMPLEX WILL BE USED\n");
+#else
+ xprintf("Long-step dual simplex will be used\n");
+#endif
+#endif
+ /* on entry to the B&B driver it is assumed that the active list
+ contains the only active (i.e. root) subproblem, which is the
+ original MIP problem to be solved */
+loop: /* main loop starts here */
+ /* at this point the current subproblem does not exist */
+ xassert(T->curr == NULL);
+ /* if the active list is empty, the search is finished */
+ if (T->head == NULL)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Active list is empty!\n");
+#if 0 /* 10/VI-2013 */
+ xassert(dmp_in_use(T->pool).lo == 0);
+#else
+ xassert(dmp_in_use(T->pool) == 0);
+#endif
+ ret = 0;
+ goto done;
+ }
+ /* select some active subproblem to continue the search */
+ xassert(T->next_p == 0);
+ /* let the application program select subproblem */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_ISELECT;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ }
+ if (T->next_p != 0)
+ { /* the application program has selected something */
+ ;
+ }
+ else if (T->a_cnt == 1)
+ { /* the only active subproblem exists, so select it */
+ xassert(T->head->next == NULL);
+ T->next_p = T->head->p;
+ }
+ else if (T->child != 0)
+ { /* select one of branching childs suggested by the branching
+ heuristic */
+ T->next_p = T->child;
+ }
+ else
+ { /* select active subproblem as specified by the backtracking
+ technique option */
+ T->next_p = ios_choose_node(T);
+ }
+ /* the active subproblem just selected becomes current */
+ ios_revive_node(T, T->next_p);
+ T->next_p = T->child = 0;
+ /* invalidate pred_p, if it is not the reference number of the
+ parent of the current subproblem */
+ if (T->curr->up != NULL && T->curr->up->p != pred_p) pred_p = 0;
+ /* determine the reference number of the current subproblem */
+ p = T->curr->p;
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ { xprintf("-----------------------------------------------------"
+ "-------------------\n");
+ xprintf("Processing node %d at level %d\n", p, T->curr->level);
+ }
+#if 0
+ if (p == 1)
+ glp_write_lp(T->mip, NULL, "root.lp");
+#endif
+#if 1 /* 24/X-2015 */
+ if (p == 1)
+ { if (T->parm->sr_heur == GLP_OFF)
+ { if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Simple rounding heuristic disabled\n");
+ }
+ }
+#endif
+ /* if it is the root subproblem, initialize cut generators */
+ if (p == 1)
+ { if (T->parm->gmi_cuts == GLP_ON)
+ { if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Gomory's cuts enabled\n");
+ }
+ if (T->parm->mir_cuts == GLP_ON)
+ { if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("MIR cuts enabled\n");
+ xassert(T->mir_gen == NULL);
+#if 0 /* 06/III-2016 */
+ T->mir_gen = ios_mir_init(T);
+#else
+ T->mir_gen = glp_mir_init(T->mip);
+#endif
+ }
+ if (T->parm->cov_cuts == GLP_ON)
+ { if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Cover cuts enabled\n");
+#ifdef NEW_COVER /* 13/II-2018 */
+ xassert(T->cov_gen == NULL);
+ T->cov_gen = glp_cov_init(T->mip);
+#endif
+ }
+ if (T->parm->clq_cuts == GLP_ON)
+ { xassert(T->clq_gen == NULL);
+ if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Clique cuts enabled\n");
+#if 0 /* 08/III-2016 */
+ T->clq_gen = ios_clq_init(T);
+#else
+ T->clq_gen = glp_cfg_init(T->mip);
+#endif
+ }
+ }
+#if 1 /* 18/VII-2013 */
+ bad_cut = 0;
+#endif
+more: /* minor loop starts here */
+ /* at this point the current subproblem needs either to be solved
+ for the first time or re-optimized due to reformulation */
+ /* display current progress of the search */
+ if (T->parm->msg_lev >= GLP_MSG_DBG ||
+ T->parm->msg_lev >= GLP_MSG_ON &&
+ (double)(T->parm->out_frq - 1) <=
+ 1000.0 * xdifftime(xtime(), T->tm_lag))
+ show_progress(T, 0);
+ if (T->parm->msg_lev >= GLP_MSG_ALL &&
+ xdifftime(xtime(), ttt) >= 60.0)
+#if 0 /* 16/II-2012 */
+ { glp_long total;
+ glp_mem_usage(NULL, NULL, &total, NULL);
+ xprintf("Time used: %.1f secs. Memory used: %.1f Mb.\n",
+ xdifftime(xtime(), T->tm_beg), xltod(total) / 1048576.0);
+ ttt = xtime();
+ }
+#else
+ { size_t total;
+ glp_mem_usage(NULL, NULL, &total, NULL);
+ xprintf("Time used: %.1f secs. Memory used: %.1f Mb.\n",
+ xdifftime(xtime(), T->tm_beg), (double)total / 1048576.0);
+ ttt = xtime();
+ }
+#endif
+ /* check the mip gap */
+ if (T->parm->mip_gap > 0.0 &&
+ ios_relative_gap(T) <= T->parm->mip_gap)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Relative gap tolerance reached; search terminated "
+ "\n");
+ ret = GLP_EMIPGAP;
+ goto done;
+ }
+ /* check if the time limit has been exhausted */
+ if (T->parm->tm_lim < INT_MAX &&
+ (double)(T->parm->tm_lim - 1) <=
+ 1000.0 * xdifftime(xtime(), T->tm_beg))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Time limit exhausted; search terminated\n");
+ ret = GLP_ETMLIM;
+ goto done;
+ }
+ /* let the application program preprocess the subproblem */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_IPREPRO;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ }
+ /* perform basic preprocessing */
+ if (T->parm->pp_tech == GLP_PP_NONE)
+ ;
+ else if (T->parm->pp_tech == GLP_PP_ROOT)
+#if 0 /* 27/II-2016 by Chris */
+ { if (T->curr->level == 0)
+#else
+ { if (!root_done)
+#endif
+ { if (ios_preprocess_node(T, 100))
+ goto fath;
+ }
+ }
+ else if (T->parm->pp_tech == GLP_PP_ALL)
+#if 0 /* 27/II-2016 by Chris */
+ { if (ios_preprocess_node(T, T->curr->level == 0 ? 100 : 10))
+#else
+ { if (ios_preprocess_node(T, !root_done ? 100 : 10))
+#endif
+ goto fath;
+ }
+ else
+ xassert(T != T);
+ /* preprocessing may improve the global bound */
+ if (!is_branch_hopeful(T, p))
+ { xprintf("*** not tested yet ***\n");
+ goto fath;
+ }
+ /* solve LP relaxation of the current subproblem */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Solving LP relaxation...\n");
+ ret = ios_solve_node(T);
+ if (ret == GLP_ETMLIM)
+ goto done;
+ else if (!(ret == 0 || ret == GLP_EOBJLL || ret == GLP_EOBJUL))
+ { if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("ios_driver: unable to solve current LP relaxation;"
+ " glp_simplex returned %d\n", ret);
+ ret = GLP_EFAIL;
+ goto done;
+ }
+ /* analyze status of the basic solution to LP relaxation found */
+ p_stat = T->mip->pbs_stat;
+ d_stat = T->mip->dbs_stat;
+ if (p_stat == GLP_FEAS && d_stat == GLP_FEAS)
+ { /* LP relaxation has optimal solution */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Found optimal solution to LP relaxation\n");
+ }
+ else if (d_stat == GLP_NOFEAS)
+ { /* LP relaxation has no dual feasible solution */
+ /* since the current subproblem cannot have a larger feasible
+ region than its parent, there is something wrong */
+ if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("ios_driver: current LP relaxation has no dual feas"
+ "ible solution\n");
+ ret = GLP_EFAIL;
+ goto done;
+ }
+ else if (p_stat == GLP_INFEAS && d_stat == GLP_FEAS)
+ { /* LP relaxation has no primal solution which is better than
+ the incumbent objective value */
+ xassert(T->mip->mip_stat == GLP_FEAS);
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("LP relaxation has no solution better than incumben"
+ "t objective value\n");
+ /* prune the branch */
+ goto fath;
+ }
+ else if (p_stat == GLP_NOFEAS)
+ { /* LP relaxation has no primal feasible solution */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("LP relaxation has no feasible solution\n");
+ /* prune the branch */
+ goto fath;
+ }
+ else
+ { /* other cases cannot appear */
+ xassert(T->mip != T->mip);
+ }
+ /* at this point basic solution to LP relaxation of the current
+ subproblem is optimal */
+ xassert(p_stat == GLP_FEAS && d_stat == GLP_FEAS);
+ xassert(T->curr != NULL);
+ T->curr->lp_obj = T->mip->obj_val;
+ /* thus, it defines a local bound to integer optimal solution of
+ the current subproblem */
+ { double bound = T->mip->obj_val;
+ /* some local bound to the current subproblem could be already
+ set before, so we should only improve it */
+ bound = ios_round_bound(T, bound);
+ if (T->mip->dir == GLP_MIN)
+ { if (T->curr->bound < bound)
+ T->curr->bound = bound;
+ }
+ else if (T->mip->dir == GLP_MAX)
+ { if (T->curr->bound > bound)
+ T->curr->bound = bound;
+ }
+ else
+ xassert(T->mip != T->mip);
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Local bound is %.9e\n", bound);
+ }
+ /* if the local bound indicates that integer optimal solution of
+ the current subproblem cannot be better than the global bound,
+ prune the branch */
+ if (!is_branch_hopeful(T, p))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Current branch is hopeless and can be pruned\n");
+ goto fath;
+ }
+ /* let the application program generate additional rows ("lazy"
+ constraints) */
+ xassert(T->reopt == 0);
+ xassert(T->reinv == 0);
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_IROWGEN;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ if (T->reopt)
+ { /* some rows were added; re-optimization is needed */
+ T->reopt = T->reinv = 0;
+ goto more;
+ }
+ if (T->reinv)
+ { /* no rows were added, however, some inactive rows were
+ removed */
+ T->reinv = 0;
+ xassert(glp_factorize(T->mip) == 0);
+ }
+ }
+ /* check if the basic solution is integer feasible */
+ check_integrality(T);
+ /* if the basic solution satisfies to all integrality conditions,
+ it is a new, better integer feasible solution */
+ if (T->curr->ii_cnt == 0)
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("New integer feasible solution found\n");
+ if (T->parm->msg_lev >= GLP_MSG_ALL)
+ display_cut_info(T);
+ record_solution(T);
+ if (T->parm->msg_lev >= GLP_MSG_ON)
+ show_progress(T, 1);
+#if 1 /* 11/VII-2013 */
+ ios_process_sol(T);
+#endif
+ /* make the application program happy */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_IBINGO;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ }
+ /* since the current subproblem has been fathomed, prune its
+ branch */
+ goto fath;
+ }
+ /* at this point basic solution to LP relaxation of the current
+ subproblem is optimal, but integer infeasible */
+ /* try to fix some non-basic structural variables of integer kind
+ on their current bounds due to reduced costs */
+ if (T->mip->mip_stat == GLP_FEAS)
+ fix_by_red_cost(T);
+ /* let the application program try to find some solution to the
+ original MIP with a primal heuristic */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_IHEUR;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ /* check if the current branch became hopeless */
+ if (!is_branch_hopeful(T, p))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Current branch became hopeless and can be prune"
+ "d\n");
+ goto fath;
+ }
+ }
+ /* try to find solution with the feasibility pump heuristic */
+#if 0 /* 27/II-2016 by Chris */
+ if (T->parm->fp_heur)
+#else
+ if (T->parm->fp_heur && !root_done)
+#endif
+ { xassert(T->reason == 0);
+ T->reason = GLP_IHEUR;
+ ios_feas_pump(T);
+ T->reason = 0;
+ /* check if the current branch became hopeless */
+ if (!is_branch_hopeful(T, p))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Current branch became hopeless and can be prune"
+ "d\n");
+ goto fath;
+ }
+ }
+#if 1 /* 25/V-2013 */
+ /* try to find solution with the proximity search heuristic */
+#if 0 /* 27/II-2016 by Chris */
+ if (T->parm->ps_heur)
+#else
+ if (T->parm->ps_heur && !root_done)
+#endif
+ { xassert(T->reason == 0);
+ T->reason = GLP_IHEUR;
+ ios_proxy_heur(T);
+ T->reason = 0;
+ /* check if the current branch became hopeless */
+ if (!is_branch_hopeful(T, p))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Current branch became hopeless and can be prune"
+ "d\n");
+ goto fath;
+ }
+ }
+#endif
+#if 1 /* 24/X-2015 */
+ /* try to find solution with a simple rounding heuristic */
+ if (T->parm->sr_heur)
+ { xassert(T->reason == 0);
+ T->reason = GLP_IHEUR;
+ round_heur(T);
+ T->reason = 0;
+ /* check if the current branch became hopeless */
+ if (!is_branch_hopeful(T, p))
+ { if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Current branch became hopeless and can be prune"
+ "d\n");
+ goto fath;
+ }
+ }
+#endif
+ /* it's time to generate cutting planes */
+ xassert(T->local != NULL);
+#ifdef NEW_LOCAL /* 02/II-2018 */
+ xassert(T->local->m == 0);
+#else
+ xassert(T->local->size == 0);
+#endif
+ /* let the application program generate some cuts; note that it
+ can add cuts either to the local cut pool or directly to the
+ current subproblem */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ T->reason = GLP_ICUTGEN;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ }
+#if 1 /* 18/VII-2013 */
+ if (T->curr->changed > 0)
+ { double degrad = fabs(T->curr->lp_obj - old_obj);
+ if (degrad < 1e-4 * (1.0 + fabs(old_obj)))
+ bad_cut++;
+ else
+ bad_cut = 0;
+ }
+ old_obj = T->curr->lp_obj;
+#if 0 /* 27/II-2016 by Chris */
+ if (bad_cut == 0 || (T->curr->level == 0 && bad_cut <= 3))
+#else
+ if (bad_cut == 0 || (!root_done && bad_cut <= 3))
+#endif
+#endif
+ /* try to generate generic cuts with built-in generators
+ (as suggested by Prof. Fischetti et al. the built-in cuts are
+ not generated at each branching node; an intense attempt of
+ generating new cuts is only made at the root node, and then
+ a moderate effort is spent after each backtracking step) */
+#if 0 /* 27/II-2016 by Chris */
+ if (T->curr->level == 0 || pred_p == 0)
+#else
+ if (!root_done || pred_p == 0)
+#endif
+ { xassert(T->reason == 0);
+ T->reason = GLP_ICUTGEN;
+ generate_cuts(T);
+ T->reason = 0;
+ }
+ /* if the local cut pool is not empty, select useful cuts and add
+ them to the current subproblem */
+#ifdef NEW_LOCAL /* 02/II-2018 */
+ if (T->local->m > 0)
+#else
+ if (T->local->size > 0)
+#endif
+ { xassert(T->reason == 0);
+ T->reason = GLP_ICUTGEN;
+ ios_process_cuts(T);
+ T->reason = 0;
+ }
+ /* clear the local cut pool */
+ ios_clear_pool(T, T->local);
+ /* perform re-optimization, if necessary */
+ if (T->reopt)
+ { T->reopt = 0;
+ T->curr->changed++;
+ goto more;
+ }
+ /* no cuts were generated; remove inactive cuts */
+ remove_cuts(T);
+#if 0 /* 27/II-2016 by Chris */
+ if (T->parm->msg_lev >= GLP_MSG_ALL && T->curr->level == 0)
+#else
+ if (T->parm->msg_lev >= GLP_MSG_ALL && !root_done)
+#endif
+ display_cut_info(T);
+#if 1 /* 27/II-2016 by Chris */
+ /* the first node will not be treated as root any more */
+ if (!root_done) root_done = 1;
+#endif
+ /* update history information used on pseudocost branching */
+ if (T->pcost != NULL) ios_pcost_update(T);
+ /* it's time to perform branching */
+ xassert(T->br_var == 0);
+ xassert(T->br_sel == 0);
+ /* let the application program choose variable to branch on */
+ if (T->parm->cb_func != NULL)
+ { xassert(T->reason == 0);
+ xassert(T->br_var == 0);
+ xassert(T->br_sel == 0);
+ T->reason = GLP_IBRANCH;
+ T->parm->cb_func(T, T->parm->cb_info);
+ T->reason = 0;
+ if (T->stop)
+ { ret = GLP_ESTOP;
+ goto done;
+ }
+ }
+ /* if nothing has been chosen, choose some variable as specified
+ by the branching technique option */
+ if (T->br_var == 0)
+ T->br_var = ios_choose_var(T, &T->br_sel);
+ /* perform actual branching */
+ curr_p = T->curr->p;
+ ret = branch_on(T, T->br_var, T->br_sel);
+ T->br_var = T->br_sel = 0;
+ if (ret == 0)
+ { /* both branches have been created */
+ pred_p = curr_p;
+ goto loop;
+ }
+ else if (ret == 1)
+ { /* one branch is hopeless and has been pruned, so now the
+ current subproblem is other branch */
+ /* the current subproblem should be considered as a new one,
+ since one bound of the branching variable was changed */
+ T->curr->solved = T->curr->changed = 0;
+#if 1 /* 18/VII-2013 */
+ /* bad_cut = 0; */
+#endif
+ goto more;
+ }
+ else if (ret == 2)
+ { /* both branches are hopeless and have been pruned; new
+ subproblem selection is needed to continue the search */
+ goto fath;
+ }
+ else
+ xassert(ret != ret);
+fath: /* the current subproblem has been fathomed */
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("Node %d fathomed\n", p);
+ /* freeze the current subproblem */
+ ios_freeze_node(T);
+ /* and prune the corresponding branch of the tree */
+ ios_delete_node(T, p);
+ /* if a new integer feasible solution has just been found, other
+ branches may become hopeless and therefore must be pruned */
+ if (T->mip->mip_stat == GLP_FEAS) cleanup_the_tree(T);
+ /* new subproblem selection is needed due to backtracking */
+ pred_p = 0;
+ goto loop;
+done: /* display progress of the search on exit from the solver */
+ if (T->parm->msg_lev >= GLP_MSG_ON)
+ show_progress(T, 0);
+ if (T->mir_gen != NULL)
+#if 0 /* 06/III-2016 */
+ ios_mir_term(T->mir_gen), T->mir_gen = NULL;
+#else
+ glp_mir_free(T->mir_gen), T->mir_gen = NULL;
+#endif
+#ifdef NEW_COVER /* 13/II-2018 */
+ if (T->cov_gen != NULL)
+ glp_cov_free(T->cov_gen), T->cov_gen = NULL;
+#endif
+ if (T->clq_gen != NULL)
+#if 0 /* 08/III-2016 */
+ ios_clq_term(T->clq_gen), T->clq_gen = NULL;
+#else
+ glp_cfg_free(T->clq_gen), T->clq_gen = NULL;
+#endif
+ /* return to the calling program */
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios07.c b/test/monniaux/glpk-4.65/src/draft/glpios07.c
new file mode 100644
index 00000000..f750e571
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios07.c
@@ -0,0 +1,551 @@
+/* glpios07.c (mixed cover cut generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/*----------------------------------------------------------------------
+-- COVER INEQUALITIES
+--
+-- Consider the set of feasible solutions to 0-1 knapsack problem:
+--
+-- sum a[j]*x[j] <= b, (1)
+-- j in J
+--
+-- x[j] is binary, (2)
+--
+-- where, wlog, we assume that a[j] > 0 (since 0-1 variables can be
+-- complemented) and a[j] <= b (since a[j] > b implies x[j] = 0).
+--
+-- A set C within J is called a cover if
+--
+-- sum a[j] > b. (3)
+-- j in C
+--
+-- For any cover C the inequality
+--
+-- sum x[j] <= |C| - 1 (4)
+-- j in C
+--
+-- is called a cover inequality and is valid for (1)-(2).
+--
+-- MIXED COVER INEQUALITIES
+--
+-- Consider the set of feasible solutions to mixed knapsack problem:
+--
+-- sum a[j]*x[j] + y <= b, (5)
+-- j in J
+--
+-- x[j] is binary, (6)
+--
+-- 0 <= y <= u is continuous, (7)
+--
+-- where again we assume that a[j] > 0.
+--
+-- Let C within J be some set. From (1)-(4) it follows that
+--
+-- sum a[j] > b - y (8)
+-- j in C
+--
+-- implies
+--
+-- sum x[j] <= |C| - 1. (9)
+-- j in C
+--
+-- Thus, we need to modify the inequality (9) in such a way that it be
+-- a constraint only if the condition (8) is satisfied.
+--
+-- Consider the following inequality:
+--
+-- sum x[j] <= |C| - t. (10)
+-- j in C
+--
+-- If 0 < t <= 1, then (10) is equivalent to (9), because all x[j] are
+-- binary variables. On the other hand, if t <= 0, (10) being satisfied
+-- for any values of x[j] is not a constraint.
+--
+-- Let
+--
+-- t' = sum a[j] + y - b. (11)
+-- j in C
+--
+-- It is understood that the condition t' > 0 is equivalent to (8).
+-- Besides, from (6)-(7) it follows that t' has an implied upper bound:
+--
+-- t'max = sum a[j] + u - b. (12)
+-- j in C
+--
+-- This allows to express the parameter t having desired properties:
+--
+-- t = t' / t'max. (13)
+--
+-- In fact, t <= 1 by definition, and t > 0 being equivalent to t' > 0
+-- is equivalent to (8).
+--
+-- Thus, the inequality (10), where t is given by formula (13) is valid
+-- for (5)-(7).
+--
+-- Note that if u = 0, then y = 0, so t = 1, and the conditions (8) and
+-- (10) is transformed to the conditions (3) and (4).
+--
+-- GENERATING MIXED COVER CUTS
+--
+-- To generate a mixed cover cut in the form (10) we need to find such
+-- set C which satisfies to the inequality (8) and for which, in turn,
+-- the inequality (10) is violated in the current point.
+--
+-- Substituting t from (13) to (10) gives:
+--
+-- 1
+-- sum x[j] <= |C| - ----- (sum a[j] + y - b), (14)
+-- j in C t'max j in C
+--
+-- and finally we have the cut inequality in the standard form:
+--
+-- sum x[j] + alfa * y <= beta, (15)
+-- j in C
+--
+-- where:
+--
+-- alfa = 1 / t'max, (16)
+--
+-- beta = |C| - alfa * (sum a[j] - b). (17)
+-- j in C */
+
+#if 1
+#define MAXTRY 1000
+#else
+#define MAXTRY 10000
+#endif
+
+static int cover2(int n, double a[], double b, double u, double x[],
+ double y, int cov[], double *_alfa, double *_beta)
+{ /* try to generate mixed cover cut using two-element cover */
+ int i, j, try = 0, ret = 0;
+ double eps, alfa, beta, temp, rmax = 0.001;
+ eps = 0.001 * (1.0 + fabs(b));
+ for (i = 0+1; i <= n; i++)
+ for (j = i+1; j <= n; j++)
+ { /* C = {i, j} */
+ try++;
+ if (try > MAXTRY) goto done;
+ /* check if condition (8) is satisfied */
+ if (a[i] + a[j] + y > b + eps)
+ { /* compute parameters for inequality (15) */
+ temp = a[i] + a[j] - b;
+ alfa = 1.0 / (temp + u);
+ beta = 2.0 - alfa * temp;
+ /* compute violation of inequality (15) */
+ temp = x[i] + x[j] + alfa * y - beta;
+ /* choose C providing maximum violation */
+ if (rmax < temp)
+ { rmax = temp;
+ cov[1] = i;
+ cov[2] = j;
+ *_alfa = alfa;
+ *_beta = beta;
+ ret = 1;
+ }
+ }
+ }
+done: return ret;
+}
+
+static int cover3(int n, double a[], double b, double u, double x[],
+ double y, int cov[], double *_alfa, double *_beta)
+{ /* try to generate mixed cover cut using three-element cover */
+ int i, j, k, try = 0, ret = 0;
+ double eps, alfa, beta, temp, rmax = 0.001;
+ eps = 0.001 * (1.0 + fabs(b));
+ for (i = 0+1; i <= n; i++)
+ for (j = i+1; j <= n; j++)
+ for (k = j+1; k <= n; k++)
+ { /* C = {i, j, k} */
+ try++;
+ if (try > MAXTRY) goto done;
+ /* check if condition (8) is satisfied */
+ if (a[i] + a[j] + a[k] + y > b + eps)
+ { /* compute parameters for inequality (15) */
+ temp = a[i] + a[j] + a[k] - b;
+ alfa = 1.0 / (temp + u);
+ beta = 3.0 - alfa * temp;
+ /* compute violation of inequality (15) */
+ temp = x[i] + x[j] + x[k] + alfa * y - beta;
+ /* choose C providing maximum violation */
+ if (rmax < temp)
+ { rmax = temp;
+ cov[1] = i;
+ cov[2] = j;
+ cov[3] = k;
+ *_alfa = alfa;
+ *_beta = beta;
+ ret = 1;
+ }
+ }
+ }
+done: return ret;
+}
+
+static int cover4(int n, double a[], double b, double u, double x[],
+ double y, int cov[], double *_alfa, double *_beta)
+{ /* try to generate mixed cover cut using four-element cover */
+ int i, j, k, l, try = 0, ret = 0;
+ double eps, alfa, beta, temp, rmax = 0.001;
+ eps = 0.001 * (1.0 + fabs(b));
+ for (i = 0+1; i <= n; i++)
+ for (j = i+1; j <= n; j++)
+ for (k = j+1; k <= n; k++)
+ for (l = k+1; l <= n; l++)
+ { /* C = {i, j, k, l} */
+ try++;
+ if (try > MAXTRY) goto done;
+ /* check if condition (8) is satisfied */
+ if (a[i] + a[j] + a[k] + a[l] + y > b + eps)
+ { /* compute parameters for inequality (15) */
+ temp = a[i] + a[j] + a[k] + a[l] - b;
+ alfa = 1.0 / (temp + u);
+ beta = 4.0 - alfa * temp;
+ /* compute violation of inequality (15) */
+ temp = x[i] + x[j] + x[k] + x[l] + alfa * y - beta;
+ /* choose C providing maximum violation */
+ if (rmax < temp)
+ { rmax = temp;
+ cov[1] = i;
+ cov[2] = j;
+ cov[3] = k;
+ cov[4] = l;
+ *_alfa = alfa;
+ *_beta = beta;
+ ret = 1;
+ }
+ }
+ }
+done: return ret;
+}
+
+static int cover(int n, double a[], double b, double u, double x[],
+ double y, int cov[], double *alfa, double *beta)
+{ /* try to generate mixed cover cut;
+ input (see (5)):
+ n is the number of binary variables;
+ a[1:n] are coefficients at binary variables;
+ b is the right-hand side;
+ u is upper bound of continuous variable;
+ x[1:n] are values of binary variables at current point;
+ y is value of continuous variable at current point;
+ output (see (15), (16), (17)):
+ cov[1:r] are indices of binary variables included in cover C,
+ where r is the set cardinality returned on exit;
+ alfa coefficient at continuous variable;
+ beta is the right-hand side; */
+ int j;
+ /* perform some sanity checks */
+ xassert(n >= 2);
+ for (j = 1; j <= n; j++) xassert(a[j] > 0.0);
+#if 1 /* ??? */
+ xassert(b > -1e-5);
+#else
+ xassert(b > 0.0);
+#endif
+ xassert(u >= 0.0);
+ for (j = 1; j <= n; j++) xassert(0.0 <= x[j] && x[j] <= 1.0);
+ xassert(0.0 <= y && y <= u);
+ /* try to generate mixed cover cut */
+ if (cover2(n, a, b, u, x, y, cov, alfa, beta)) return 2;
+ if (cover3(n, a, b, u, x, y, cov, alfa, beta)) return 3;
+ if (cover4(n, a, b, u, x, y, cov, alfa, beta)) return 4;
+ return 0;
+}
+
+/*----------------------------------------------------------------------
+-- lpx_cover_cut - generate mixed cover cut.
+--
+-- SYNOPSIS
+--
+-- int lpx_cover_cut(LPX *lp, int len, int ind[], double val[],
+-- double work[]);
+--
+-- DESCRIPTION
+--
+-- The routine lpx_cover_cut generates a mixed cover cut for a given
+-- row of the MIP problem.
+--
+-- The given row of the MIP problem should be explicitly specified in
+-- the form:
+--
+-- sum{j in J} a[j]*x[j] <= b. (1)
+--
+-- On entry indices (ordinal numbers) of structural variables, which
+-- have non-zero constraint coefficients, should be placed in locations
+-- ind[1], ..., ind[len], and corresponding constraint coefficients
+-- should be placed in locations val[1], ..., val[len]. The right-hand
+-- side b should be stored in location val[0].
+--
+-- The working array work should have at least nb locations, where nb
+-- is the number of binary variables in (1).
+--
+-- The routine generates a mixed cover cut in the same form as (1) and
+-- stores the cut coefficients and right-hand side in the same way as
+-- just described above.
+--
+-- RETURNS
+--
+-- If the cutting plane has been successfully generated, the routine
+-- returns 1 <= len' <= n, which is the number of non-zero coefficients
+-- in the inequality constraint. Otherwise, the routine returns zero. */
+
+static int lpx_cover_cut(glp_prob *lp, int len, int ind[],
+ double val[], double work[])
+{ int cov[1+4], j, k, nb, newlen, r;
+ double f_min, f_max, alfa, beta, u, *x = work, y;
+ /* substitute and remove fixed variables */
+ newlen = 0;
+ for (k = 1; k <= len; k++)
+ { j = ind[k];
+ if (glp_get_col_type(lp, j) == GLP_FX)
+ val[0] -= val[k] * glp_get_col_lb(lp, j);
+ else
+ { newlen++;
+ ind[newlen] = ind[k];
+ val[newlen] = val[k];
+ }
+ }
+ len = newlen;
+ /* move binary variables to the beginning of the list so that
+ elements 1, 2, ..., nb correspond to binary variables, and
+ elements nb+1, nb+2, ..., len correspond to rest variables */
+ nb = 0;
+ for (k = 1; k <= len; k++)
+ { j = ind[k];
+ if (glp_get_col_kind(lp, j) == GLP_BV)
+ { /* binary variable */
+ int ind_k;
+ double val_k;
+ nb++;
+ ind_k = ind[nb], val_k = val[nb];
+ ind[nb] = ind[k], val[nb] = val[k];
+ ind[k] = ind_k, val[k] = val_k;
+ }
+ }
+ /* now the specified row has the form:
+ sum a[j]*x[j] + sum a[j]*y[j] <= b,
+ where x[j] are binary variables, y[j] are rest variables */
+ /* at least two binary variables are needed */
+ if (nb < 2) return 0;
+ /* compute implied lower and upper bounds for sum a[j]*y[j] */
+ f_min = f_max = 0.0;
+ for (k = nb+1; k <= len; k++)
+ { j = ind[k];
+ /* both bounds must be finite */
+ if (glp_get_col_type(lp, j) != GLP_DB) return 0;
+ if (val[k] > 0.0)
+ { f_min += val[k] * glp_get_col_lb(lp, j);
+ f_max += val[k] * glp_get_col_ub(lp, j);
+ }
+ else
+ { f_min += val[k] * glp_get_col_ub(lp, j);
+ f_max += val[k] * glp_get_col_lb(lp, j);
+ }
+ }
+ /* sum a[j]*x[j] + sum a[j]*y[j] <= b ===>
+ sum a[j]*x[j] + (sum a[j]*y[j] - f_min) <= b - f_min ===>
+ sum a[j]*x[j] + y <= b - f_min,
+ where y = sum a[j]*y[j] - f_min;
+ note that 0 <= y <= u, u = f_max - f_min */
+ /* determine upper bound of y */
+ u = f_max - f_min;
+ /* determine value of y at the current point */
+ y = 0.0;
+ for (k = nb+1; k <= len; k++)
+ { j = ind[k];
+ y += val[k] * glp_get_col_prim(lp, j);
+ }
+ y -= f_min;
+ if (y < 0.0) y = 0.0;
+ if (y > u) y = u;
+ /* modify the right-hand side b */
+ val[0] -= f_min;
+ /* now the transformed row has the form:
+ sum a[j]*x[j] + y <= b, where 0 <= y <= u */
+ /* determine values of x[j] at the current point */
+ for (k = 1; k <= nb; k++)
+ { j = ind[k];
+ x[k] = glp_get_col_prim(lp, j);
+ if (x[k] < 0.0) x[k] = 0.0;
+ if (x[k] > 1.0) x[k] = 1.0;
+ }
+ /* if a[j] < 0, replace x[j] by its complement 1 - x'[j] */
+ for (k = 1; k <= nb; k++)
+ { if (val[k] < 0.0)
+ { ind[k] = - ind[k];
+ val[k] = - val[k];
+ val[0] += val[k];
+ x[k] = 1.0 - x[k];
+ }
+ }
+ /* try to generate a mixed cover cut for the transformed row */
+ r = cover(nb, val, val[0], u, x, y, cov, &alfa, &beta);
+ if (r == 0) return 0;
+ xassert(2 <= r && r <= 4);
+ /* now the cut is in the form:
+ sum{j in C} x[j] + alfa * y <= beta */
+ /* store the right-hand side beta */
+ ind[0] = 0, val[0] = beta;
+ /* restore the original ordinal numbers of x[j] */
+ for (j = 1; j <= r; j++) cov[j] = ind[cov[j]];
+ /* store cut coefficients at binary variables complementing back
+ the variables having negative row coefficients */
+ xassert(r <= nb);
+ for (k = 1; k <= r; k++)
+ { if (cov[k] > 0)
+ { ind[k] = +cov[k];
+ val[k] = +1.0;
+ }
+ else
+ { ind[k] = -cov[k];
+ val[k] = -1.0;
+ val[0] -= 1.0;
+ }
+ }
+ /* substitute y = sum a[j]*y[j] - f_min */
+ for (k = nb+1; k <= len; k++)
+ { r++;
+ ind[r] = ind[k];
+ val[r] = alfa * val[k];
+ }
+ val[0] += alfa * f_min;
+ xassert(r <= len);
+ len = r;
+ return len;
+}
+
+/*----------------------------------------------------------------------
+-- lpx_eval_row - compute explictily specified row.
+--
+-- SYNOPSIS
+--
+-- double lpx_eval_row(LPX *lp, int len, int ind[], double val[]);
+--
+-- DESCRIPTION
+--
+-- The routine lpx_eval_row computes the primal value of an explicitly
+-- specified row using current values of structural variables.
+--
+-- The explicitly specified row may be thought as a linear form:
+--
+-- y = a[1]*x[m+1] + a[2]*x[m+2] + ... + a[n]*x[m+n],
+--
+-- where y is an auxiliary variable for this row, a[j] are coefficients
+-- of the linear form, x[m+j] are structural variables.
+--
+-- On entry column indices and numerical values of non-zero elements of
+-- the row should be stored in locations ind[1], ..., ind[len] and
+-- val[1], ..., val[len], where len is the number of non-zero elements.
+-- The array ind and val are not changed on exit.
+--
+-- RETURNS
+--
+-- The routine returns a computed value of y, the auxiliary variable of
+-- the specified row. */
+
+static double lpx_eval_row(glp_prob *lp, int len, int ind[],
+ double val[])
+{ int n = glp_get_num_cols(lp);
+ int j, k;
+ double sum = 0.0;
+ if (len < 0)
+ xerror("lpx_eval_row: len = %d; invalid row length\n", len);
+ for (k = 1; k <= len; k++)
+ { j = ind[k];
+ if (!(1 <= j && j <= n))
+ xerror("lpx_eval_row: j = %d; column number out of range\n",
+ j);
+ sum += val[k] * glp_get_col_prim(lp, j);
+ }
+ return sum;
+}
+
+/***********************************************************************
+* NAME
+*
+* ios_cov_gen - generate mixed cover cuts
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_cov_gen(glp_tree *tree);
+*
+* DESCRIPTION
+*
+* The routine ios_cov_gen generates mixed cover cuts for the current
+* point and adds them to the cut pool. */
+
+void ios_cov_gen(glp_tree *tree)
+{ glp_prob *prob = tree->mip;
+ int m = glp_get_num_rows(prob);
+ int n = glp_get_num_cols(prob);
+ int i, k, type, kase, len, *ind;
+ double r, *val, *work;
+ xassert(glp_get_status(prob) == GLP_OPT);
+ /* allocate working arrays */
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ work = xcalloc(1+n, sizeof(double));
+ /* look through all rows */
+ for (i = 1; i <= m; i++)
+ for (kase = 1; kase <= 2; kase++)
+ { type = glp_get_row_type(prob, i);
+ if (kase == 1)
+ { /* consider rows of '<=' type */
+ if (!(type == GLP_UP || type == GLP_DB)) continue;
+ len = glp_get_mat_row(prob, i, ind, val);
+ val[0] = glp_get_row_ub(prob, i);
+ }
+ else
+ { /* consider rows of '>=' type */
+ if (!(type == GLP_LO || type == GLP_DB)) continue;
+ len = glp_get_mat_row(prob, i, ind, val);
+ for (k = 1; k <= len; k++) val[k] = - val[k];
+ val[0] = - glp_get_row_lb(prob, i);
+ }
+ /* generate mixed cover cut:
+ sum{j in J} a[j] * x[j] <= b */
+ len = lpx_cover_cut(prob, len, ind, val, work);
+ if (len == 0) continue;
+ /* at the current point the cut inequality is violated, i.e.
+ sum{j in J} a[j] * x[j] - b > 0 */
+ r = lpx_eval_row(prob, len, ind, val) - val[0];
+ if (r < 1e-3) continue;
+ /* add the cut to the cut pool */
+ glp_ios_add_row(tree, NULL, GLP_RF_COV, 0, len, ind, val,
+ GLP_UP, val[0]);
+ }
+ /* free working arrays */
+ xfree(ind);
+ xfree(val);
+ xfree(work);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios09.c b/test/monniaux/glpk-4.65/src/draft/glpios09.c
new file mode 100644
index 00000000..d80ed9a3
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios09.c
@@ -0,0 +1,664 @@
+/* glpios09.c (branching heuristics) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* NAME
+*
+* ios_choose_var - select variable to branch on
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_choose_var(glp_tree *T, int *next);
+*
+* The routine ios_choose_var chooses a variable from the candidate
+* list to branch on. Additionally the routine provides a flag stored
+* in the location next to suggests which of the child subproblems
+* should be solved next.
+*
+* RETURNS
+*
+* The routine ios_choose_var returns the ordinal number of the column
+* choosen. */
+
+static int branch_first(glp_tree *T, int *next);
+static int branch_last(glp_tree *T, int *next);
+static int branch_mostf(glp_tree *T, int *next);
+static int branch_drtom(glp_tree *T, int *next);
+
+int ios_choose_var(glp_tree *T, int *next)
+{ int j;
+ if (T->parm->br_tech == GLP_BR_FFV)
+ { /* branch on first fractional variable */
+ j = branch_first(T, next);
+ }
+ else if (T->parm->br_tech == GLP_BR_LFV)
+ { /* branch on last fractional variable */
+ j = branch_last(T, next);
+ }
+ else if (T->parm->br_tech == GLP_BR_MFV)
+ { /* branch on most fractional variable */
+ j = branch_mostf(T, next);
+ }
+ else if (T->parm->br_tech == GLP_BR_DTH)
+ { /* branch using the heuristic by Dreebeck and Tomlin */
+ j = branch_drtom(T, next);
+ }
+ else if (T->parm->br_tech == GLP_BR_PCH)
+ { /* hybrid pseudocost heuristic */
+ j = ios_pcost_branch(T, next);
+ }
+ else
+ xassert(T != T);
+ return j;
+}
+
+/***********************************************************************
+* branch_first - choose first branching variable
+*
+* This routine looks up the list of structural variables and chooses
+* the first one, which is of integer kind and has fractional value in
+* optimal solution to the current LP relaxation.
+*
+* This routine also selects the branch to be solved next where integer
+* infeasibility of the chosen variable is less than in other one. */
+
+static int branch_first(glp_tree *T, int *_next)
+{ int j, next;
+ double beta;
+ /* choose the column to branch on */
+ for (j = 1; j <= T->n; j++)
+ if (T->non_int[j]) break;
+ xassert(1 <= j && j <= T->n);
+ /* select the branch to be solved next */
+ beta = glp_get_col_prim(T->mip, j);
+ if (beta - floor(beta) < ceil(beta) - beta)
+ next = GLP_DN_BRNCH;
+ else
+ next = GLP_UP_BRNCH;
+ *_next = next;
+ return j;
+}
+
+/***********************************************************************
+* branch_last - choose last branching variable
+*
+* This routine looks up the list of structural variables and chooses
+* the last one, which is of integer kind and has fractional value in
+* optimal solution to the current LP relaxation.
+*
+* This routine also selects the branch to be solved next where integer
+* infeasibility of the chosen variable is less than in other one. */
+
+static int branch_last(glp_tree *T, int *_next)
+{ int j, next;
+ double beta;
+ /* choose the column to branch on */
+ for (j = T->n; j >= 1; j--)
+ if (T->non_int[j]) break;
+ xassert(1 <= j && j <= T->n);
+ /* select the branch to be solved next */
+ beta = glp_get_col_prim(T->mip, j);
+ if (beta - floor(beta) < ceil(beta) - beta)
+ next = GLP_DN_BRNCH;
+ else
+ next = GLP_UP_BRNCH;
+ *_next = next;
+ return j;
+}
+
+/***********************************************************************
+* branch_mostf - choose most fractional branching variable
+*
+* This routine looks up the list of structural variables and chooses
+* that one, which is of integer kind and has most fractional value in
+* optimal solution to the current LP relaxation.
+*
+* This routine also selects the branch to be solved next where integer
+* infeasibility of the chosen variable is less than in other one.
+*
+* (Alexander Martin notices that "...most infeasible is as good as
+* random...".) */
+
+static int branch_mostf(glp_tree *T, int *_next)
+{ int j, jj, next;
+ double beta, most, temp;
+ /* choose the column to branch on */
+ jj = 0, most = DBL_MAX;
+ for (j = 1; j <= T->n; j++)
+ { if (T->non_int[j])
+ { beta = glp_get_col_prim(T->mip, j);
+ temp = floor(beta) + 0.5;
+ if (most > fabs(beta - temp))
+ { jj = j, most = fabs(beta - temp);
+ if (beta < temp)
+ next = GLP_DN_BRNCH;
+ else
+ next = GLP_UP_BRNCH;
+ }
+ }
+ }
+ *_next = next;
+ return jj;
+}
+
+/***********************************************************************
+* branch_drtom - choose branching var using Driebeck-Tomlin heuristic
+*
+* This routine chooses a structural variable, which is required to be
+* integral and has fractional value in optimal solution of the current
+* LP relaxation, using a heuristic proposed by Driebeck and Tomlin.
+*
+* The routine also selects the branch to be solved next, again due to
+* Driebeck and Tomlin.
+*
+* This routine is based on the heuristic proposed in:
+*
+* Driebeck N.J. An algorithm for the solution of mixed-integer
+* programming problems, Management Science, 12: 576-87 (1966);
+*
+* and improved in:
+*
+* Tomlin J.A. Branch and bound methods for integer and non-convex
+* programming, in J.Abadie (ed.), Integer and Nonlinear Programming,
+* North-Holland, Amsterdam, pp. 437-50 (1970).
+*
+* Must note that this heuristic is time-expensive, because computing
+* one-step degradation (see the routine below) requires one BTRAN for
+* each fractional-valued structural variable. */
+
+static int branch_drtom(glp_tree *T, int *_next)
+{ glp_prob *mip = T->mip;
+ int m = mip->m;
+ int n = mip->n;
+ unsigned char *non_int = T->non_int;
+ int j, jj, k, t, next, kase, len, stat, *ind;
+ double x, dk, alfa, delta_j, delta_k, delta_z, dz_dn, dz_up,
+ dd_dn, dd_up, degrad, *val;
+ /* basic solution of LP relaxation must be optimal */
+ xassert(glp_get_status(mip) == GLP_OPT);
+ /* allocate working arrays */
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ /* nothing has been chosen so far */
+ jj = 0, degrad = -1.0;
+ /* walk through the list of columns (structural variables) */
+ for (j = 1; j <= n; j++)
+ { /* if j-th column is not marked as fractional, skip it */
+ if (!non_int[j]) continue;
+ /* obtain (fractional) value of j-th column in basic solution
+ of LP relaxation */
+ x = glp_get_col_prim(mip, j);
+ /* since the value of j-th column is fractional, the column is
+ basic; compute corresponding row of the simplex table */
+ len = glp_eval_tab_row(mip, m+j, ind, val);
+ /* the following fragment computes a change in the objective
+ function: delta Z = new Z - old Z, where old Z is the
+ objective value in the current optimal basis, and new Z is
+ the objective value in the adjacent basis, for two cases:
+ 1) if new upper bound ub' = floor(x[j]) is introduced for
+ j-th column (down branch);
+ 2) if new lower bound lb' = ceil(x[j]) is introduced for
+ j-th column (up branch);
+ since in both cases the solution remaining dual feasible
+ becomes primal infeasible, one implicit simplex iteration
+ is performed to determine the change delta Z;
+ it is obvious that new Z, which is never better than old Z,
+ is a lower (minimization) or upper (maximization) bound of
+ the objective function for down- and up-branches. */
+ for (kase = -1; kase <= +1; kase += 2)
+ { /* if kase < 0, the new upper bound of x[j] is introduced;
+ in this case x[j] should decrease in order to leave the
+ basis and go to its new upper bound */
+ /* if kase > 0, the new lower bound of x[j] is introduced;
+ in this case x[j] should increase in order to leave the
+ basis and go to its new lower bound */
+ /* apply the dual ratio test in order to determine which
+ auxiliary or structural variable should enter the basis
+ to keep dual feasibility */
+ k = glp_dual_rtest(mip, len, ind, val, kase, 1e-9);
+ if (k != 0) k = ind[k];
+ /* if no non-basic variable has been chosen, LP relaxation
+ of corresponding branch being primal infeasible and dual
+ unbounded has no primal feasible solution; in this case
+ the change delta Z is formally set to infinity */
+ if (k == 0)
+ { delta_z =
+ (T->mip->dir == GLP_MIN ? +DBL_MAX : -DBL_MAX);
+ goto skip;
+ }
+ /* row of the simplex table that corresponds to non-basic
+ variable x[k] choosen by the dual ratio test is:
+ x[j] = ... + alfa * x[k] + ...
+ where alfa is the influence coefficient (an element of
+ the simplex table row) */
+ /* determine the coefficient alfa */
+ for (t = 1; t <= len; t++) if (ind[t] == k) break;
+ xassert(1 <= t && t <= len);
+ alfa = val[t];
+ /* since in the adjacent basis the variable x[j] becomes
+ non-basic, knowing its value in the current basis we can
+ determine its change delta x[j] = new x[j] - old x[j] */
+ delta_j = (kase < 0 ? floor(x) : ceil(x)) - x;
+ /* and knowing the coefficient alfa we can determine the
+ corresponding change delta x[k] = new x[k] - old x[k],
+ where old x[k] is a value of x[k] in the current basis,
+ and new x[k] is a value of x[k] in the adjacent basis */
+ delta_k = delta_j / alfa;
+ /* Tomlin noticed that if the variable x[k] is of integer
+ kind, its change cannot be less (eventually) than one in
+ the magnitude */
+ if (k > m && glp_get_col_kind(mip, k-m) != GLP_CV)
+ { /* x[k] is structural integer variable */
+ if (fabs(delta_k - floor(delta_k + 0.5)) > 1e-3)
+ { if (delta_k > 0.0)
+ delta_k = ceil(delta_k); /* +3.14 -> +4 */
+ else
+ delta_k = floor(delta_k); /* -3.14 -> -4 */
+ }
+ }
+ /* now determine the status and reduced cost of x[k] in the
+ current basis */
+ if (k <= m)
+ { stat = glp_get_row_stat(mip, k);
+ dk = glp_get_row_dual(mip, k);
+ }
+ else
+ { stat = glp_get_col_stat(mip, k-m);
+ dk = glp_get_col_dual(mip, k-m);
+ }
+ /* if the current basis is dual degenerate, some reduced
+ costs which are close to zero may have wrong sign due to
+ round-off errors, so correct the sign of d[k] */
+ switch (T->mip->dir)
+ { case GLP_MIN:
+ if (stat == GLP_NL && dk < 0.0 ||
+ stat == GLP_NU && dk > 0.0 ||
+ stat == GLP_NF) dk = 0.0;
+ break;
+ case GLP_MAX:
+ if (stat == GLP_NL && dk > 0.0 ||
+ stat == GLP_NU && dk < 0.0 ||
+ stat == GLP_NF) dk = 0.0;
+ break;
+ default:
+ xassert(T != T);
+ }
+ /* now knowing the change of x[k] and its reduced cost d[k]
+ we can compute the corresponding change in the objective
+ function delta Z = new Z - old Z = d[k] * delta x[k];
+ note that due to Tomlin's modification new Z can be even
+ worse than in the adjacent basis */
+ delta_z = dk * delta_k;
+skip: /* new Z is never better than old Z, therefore the change
+ delta Z is always non-negative (in case of minimization)
+ or non-positive (in case of maximization) */
+ switch (T->mip->dir)
+ { case GLP_MIN: xassert(delta_z >= 0.0); break;
+ case GLP_MAX: xassert(delta_z <= 0.0); break;
+ default: xassert(T != T);
+ }
+ /* save the change in the objective fnction for down- and
+ up-branches, respectively */
+ if (kase < 0) dz_dn = delta_z; else dz_up = delta_z;
+ }
+ /* thus, in down-branch no integer feasible solution can be
+ better than Z + dz_dn, and in up-branch no integer feasible
+ solution can be better than Z + dz_up, where Z is value of
+ the objective function in the current basis */
+ /* following the heuristic by Driebeck and Tomlin we choose a
+ column (i.e. structural variable) which provides largest
+ degradation of the objective function in some of branches;
+ besides, we select the branch with smaller degradation to
+ be solved next and keep other branch with larger degradation
+ in the active list hoping to minimize the number of further
+ backtrackings */
+ if (degrad < fabs(dz_dn) || degrad < fabs(dz_up))
+ { jj = j;
+ if (fabs(dz_dn) < fabs(dz_up))
+ { /* select down branch to be solved next */
+ next = GLP_DN_BRNCH;
+ degrad = fabs(dz_up);
+ }
+ else
+ { /* select up branch to be solved next */
+ next = GLP_UP_BRNCH;
+ degrad = fabs(dz_dn);
+ }
+ /* save the objective changes for printing */
+ dd_dn = dz_dn, dd_up = dz_up;
+ /* if down- or up-branch has no feasible solution, we does
+ not need to consider other candidates (in principle, the
+ corresponding branch could be pruned right now) */
+ if (degrad == DBL_MAX) break;
+ }
+ }
+ /* free working arrays */
+ xfree(ind);
+ xfree(val);
+ /* something must be chosen */
+ xassert(1 <= jj && jj <= n);
+#if 1 /* 02/XI-2009 */
+ if (degrad < 1e-6 * (1.0 + 0.001 * fabs(mip->obj_val)))
+ { jj = branch_mostf(T, &next);
+ goto done;
+ }
+#endif
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ { xprintf("branch_drtom: column %d chosen to branch on\n", jj);
+ if (fabs(dd_dn) == DBL_MAX)
+ xprintf("branch_drtom: down-branch is infeasible\n");
+ else
+ xprintf("branch_drtom: down-branch bound is %.9e\n",
+ glp_get_obj_val(mip) + dd_dn);
+ if (fabs(dd_up) == DBL_MAX)
+ xprintf("branch_drtom: up-branch is infeasible\n");
+ else
+ xprintf("branch_drtom: up-branch bound is %.9e\n",
+ glp_get_obj_val(mip) + dd_up);
+ }
+done: *_next = next;
+ return jj;
+}
+
+/**********************************************************************/
+
+struct csa
+{ /* common storage area */
+ int *dn_cnt; /* int dn_cnt[1+n]; */
+ /* dn_cnt[j] is the number of subproblems, whose LP relaxations
+ have been solved and which are down-branches for variable x[j];
+ dn_cnt[j] = 0 means the down pseudocost is uninitialized */
+ double *dn_sum; /* double dn_sum[1+n]; */
+ /* dn_sum[j] is the sum of per unit degradations of the objective
+ over all dn_cnt[j] subproblems */
+ int *up_cnt; /* int up_cnt[1+n]; */
+ /* up_cnt[j] is the number of subproblems, whose LP relaxations
+ have been solved and which are up-branches for variable x[j];
+ up_cnt[j] = 0 means the up pseudocost is uninitialized */
+ double *up_sum; /* double up_sum[1+n]; */
+ /* up_sum[j] is the sum of per unit degradations of the objective
+ over all up_cnt[j] subproblems */
+};
+
+void *ios_pcost_init(glp_tree *tree)
+{ /* initialize working data used on pseudocost branching */
+ struct csa *csa;
+ int n = tree->n, j;
+ csa = xmalloc(sizeof(struct csa));
+ csa->dn_cnt = xcalloc(1+n, sizeof(int));
+ csa->dn_sum = xcalloc(1+n, sizeof(double));
+ csa->up_cnt = xcalloc(1+n, sizeof(int));
+ csa->up_sum = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++)
+ { csa->dn_cnt[j] = csa->up_cnt[j] = 0;
+ csa->dn_sum[j] = csa->up_sum[j] = 0.0;
+ }
+ return csa;
+}
+
+static double eval_degrad(glp_prob *P, int j, double bnd)
+{ /* compute degradation of the objective on fixing x[j] at given
+ value with a limited number of dual simplex iterations */
+ /* this routine fixes column x[j] at specified value bnd,
+ solves resulting LP, and returns a lower bound to degradation
+ of the objective, degrad >= 0 */
+ glp_prob *lp;
+ glp_smcp parm;
+ int ret;
+ double degrad;
+ /* the current basis must be optimal */
+ xassert(glp_get_status(P) == GLP_OPT);
+ /* create a copy of P */
+ lp = glp_create_prob();
+ glp_copy_prob(lp, P, 0);
+ /* fix column x[j] at specified value */
+ glp_set_col_bnds(lp, j, GLP_FX, bnd, bnd);
+ /* try to solve resulting LP */
+ glp_init_smcp(&parm);
+ parm.msg_lev = GLP_MSG_OFF;
+ parm.meth = GLP_DUAL;
+ parm.it_lim = 30;
+ parm.out_dly = 1000;
+ parm.meth = GLP_DUAL;
+ ret = glp_simplex(lp, &parm);
+ if (ret == 0 || ret == GLP_EITLIM)
+ { if (glp_get_prim_stat(lp) == GLP_NOFEAS)
+ { /* resulting LP has no primal feasible solution */
+ degrad = DBL_MAX;
+ }
+ else if (glp_get_dual_stat(lp) == GLP_FEAS)
+ { /* resulting basis is optimal or at least dual feasible,
+ so we have the correct lower bound to degradation */
+ if (P->dir == GLP_MIN)
+ degrad = lp->obj_val - P->obj_val;
+ else if (P->dir == GLP_MAX)
+ degrad = P->obj_val - lp->obj_val;
+ else
+ xassert(P != P);
+ /* degradation cannot be negative by definition */
+ /* note that the lower bound to degradation may be close
+ to zero even if its exact value is zero due to round-off
+ errors on computing the objective value */
+ if (degrad < 1e-6 * (1.0 + 0.001 * fabs(P->obj_val)))
+ degrad = 0.0;
+ }
+ else
+ { /* the final basis reported by the simplex solver is dual
+ infeasible, so we cannot determine a non-trivial lower
+ bound to degradation */
+ degrad = 0.0;
+ }
+ }
+ else
+ { /* the simplex solver failed */
+ degrad = 0.0;
+ }
+ /* delete the copy of P */
+ glp_delete_prob(lp);
+ return degrad;
+}
+
+void ios_pcost_update(glp_tree *tree)
+{ /* update history information for pseudocost branching */
+ /* this routine is called every time when LP relaxation of the
+ current subproblem has been solved to optimality with all lazy
+ and cutting plane constraints included */
+ int j;
+ double dx, dz, psi;
+ struct csa *csa = tree->pcost;
+ xassert(csa != NULL);
+ xassert(tree->curr != NULL);
+ /* if the current subproblem is the root, skip updating */
+ if (tree->curr->up == NULL) goto skip;
+ /* determine branching variable x[j], which was used in the
+ parent subproblem to create the current subproblem */
+ j = tree->curr->up->br_var;
+ xassert(1 <= j && j <= tree->n);
+ /* determine the change dx[j] = new x[j] - old x[j],
+ where new x[j] is a value of x[j] in optimal solution to LP
+ relaxation of the current subproblem, old x[j] is a value of
+ x[j] in optimal solution to LP relaxation of the parent
+ subproblem */
+ dx = tree->mip->col[j]->prim - tree->curr->up->br_val;
+ xassert(dx != 0.0);
+ /* determine corresponding change dz = new dz - old dz in the
+ objective function value */
+ dz = tree->mip->obj_val - tree->curr->up->lp_obj;
+ /* determine per unit degradation of the objective function */
+ psi = fabs(dz / dx);
+ /* update history information */
+ if (dx < 0.0)
+ { /* the current subproblem is down-branch */
+ csa->dn_cnt[j]++;
+ csa->dn_sum[j] += psi;
+ }
+ else /* dx > 0.0 */
+ { /* the current subproblem is up-branch */
+ csa->up_cnt[j]++;
+ csa->up_sum[j] += psi;
+ }
+skip: return;
+}
+
+void ios_pcost_free(glp_tree *tree)
+{ /* free working area used on pseudocost branching */
+ struct csa *csa = tree->pcost;
+ xassert(csa != NULL);
+ xfree(csa->dn_cnt);
+ xfree(csa->dn_sum);
+ xfree(csa->up_cnt);
+ xfree(csa->up_sum);
+ xfree(csa);
+ tree->pcost = NULL;
+ return;
+}
+
+static double eval_psi(glp_tree *T, int j, int brnch)
+{ /* compute estimation of pseudocost of variable x[j] for down-
+ or up-branch */
+ struct csa *csa = T->pcost;
+ double beta, degrad, psi;
+ xassert(csa != NULL);
+ xassert(1 <= j && j <= T->n);
+ if (brnch == GLP_DN_BRNCH)
+ { /* down-branch */
+ if (csa->dn_cnt[j] == 0)
+ { /* initialize down pseudocost */
+ beta = T->mip->col[j]->prim;
+ degrad = eval_degrad(T->mip, j, floor(beta));
+ if (degrad == DBL_MAX)
+ { psi = DBL_MAX;
+ goto done;
+ }
+ csa->dn_cnt[j] = 1;
+ csa->dn_sum[j] = degrad / (beta - floor(beta));
+ }
+ psi = csa->dn_sum[j] / (double)csa->dn_cnt[j];
+ }
+ else if (brnch == GLP_UP_BRNCH)
+ { /* up-branch */
+ if (csa->up_cnt[j] == 0)
+ { /* initialize up pseudocost */
+ beta = T->mip->col[j]->prim;
+ degrad = eval_degrad(T->mip, j, ceil(beta));
+ if (degrad == DBL_MAX)
+ { psi = DBL_MAX;
+ goto done;
+ }
+ csa->up_cnt[j] = 1;
+ csa->up_sum[j] = degrad / (ceil(beta) - beta);
+ }
+ psi = csa->up_sum[j] / (double)csa->up_cnt[j];
+ }
+ else
+ xassert(brnch != brnch);
+done: return psi;
+}
+
+static void progress(glp_tree *T)
+{ /* display progress of pseudocost initialization */
+ struct csa *csa = T->pcost;
+ int j, nv = 0, ni = 0;
+ for (j = 1; j <= T->n; j++)
+ { if (glp_ios_can_branch(T, j))
+ { nv++;
+ if (csa->dn_cnt[j] > 0 && csa->up_cnt[j] > 0) ni++;
+ }
+ }
+ xprintf("Pseudocosts initialized for %d of %d variables\n",
+ ni, nv);
+ return;
+}
+
+int ios_pcost_branch(glp_tree *T, int *_next)
+{ /* choose branching variable with pseudocost branching */
+#if 0 /* 10/VI-2013 */
+ glp_long t = xtime();
+#else
+ double t = xtime();
+#endif
+ int j, jjj, sel;
+ double beta, psi, d1, d2, d, dmax;
+ /* initialize the working arrays */
+ if (T->pcost == NULL)
+ T->pcost = ios_pcost_init(T);
+ /* nothing has been chosen so far */
+ jjj = 0, dmax = -1.0;
+ /* go through the list of branching candidates */
+ for (j = 1; j <= T->n; j++)
+ { if (!glp_ios_can_branch(T, j)) continue;
+ /* determine primal value of x[j] in optimal solution to LP
+ relaxation of the current subproblem */
+ beta = T->mip->col[j]->prim;
+ /* estimate pseudocost of x[j] for down-branch */
+ psi = eval_psi(T, j, GLP_DN_BRNCH);
+ if (psi == DBL_MAX)
+ { /* down-branch has no primal feasible solution */
+ jjj = j, sel = GLP_DN_BRNCH;
+ goto done;
+ }
+ /* estimate degradation of the objective for down-branch */
+ d1 = psi * (beta - floor(beta));
+ /* estimate pseudocost of x[j] for up-branch */
+ psi = eval_psi(T, j, GLP_UP_BRNCH);
+ if (psi == DBL_MAX)
+ { /* up-branch has no primal feasible solution */
+ jjj = j, sel = GLP_UP_BRNCH;
+ goto done;
+ }
+ /* estimate degradation of the objective for up-branch */
+ d2 = psi * (ceil(beta) - beta);
+ /* determine d = max(d1, d2) */
+ d = (d1 > d2 ? d1 : d2);
+ /* choose x[j] which provides maximal estimated degradation of
+ the objective either in down- or up-branch */
+ if (dmax < d)
+ { dmax = d;
+ jjj = j;
+ /* continue the search from a subproblem, where degradation
+ is less than in other one */
+ sel = (d1 <= d2 ? GLP_DN_BRNCH : GLP_UP_BRNCH);
+ }
+ /* display progress of pseudocost initialization */
+ if (T->parm->msg_lev >= GLP_ON)
+ { if (xdifftime(xtime(), t) >= 10.0)
+ { progress(T);
+ t = xtime();
+ }
+ }
+ }
+ if (dmax == 0.0)
+ { /* no degradation is indicated; choose a variable having most
+ fractional value */
+ jjj = branch_mostf(T, &sel);
+ }
+done: *_next = sel;
+ return jjj;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios11.c b/test/monniaux/glpk-4.65/src/draft/glpios11.c
new file mode 100644
index 00000000..09fccef6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios11.c
@@ -0,0 +1,435 @@
+/* glpios11.c (process cuts stored in the local cut pool) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017, 2018 Andrew Makhorin, Department for
+* Applied Informatics, Moscow Aviation Institute, Moscow, Russia. All
+* rights reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "draft.h"
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* NAME
+*
+* ios_process_cuts - process cuts stored in the local cut pool
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_process_cuts(glp_tree *T);
+*
+* DESCRIPTION
+*
+* The routine ios_process_cuts analyzes each cut currently stored in
+* the local cut pool, which must be non-empty, and either adds the cut
+* to the current subproblem or just discards it. All cuts are assumed
+* to be locally valid. On exit the local cut pool remains unchanged.
+*
+* REFERENCES
+*
+* 1. E.Balas, S.Ceria, G.Cornuejols, "Mixed 0-1 Programming by
+* Lift-and-Project in a Branch-and-Cut Framework", Management Sc.,
+* 42 (1996) 1229-1246.
+*
+* 2. G.Andreello, A.Caprara, and M.Fischetti, "Embedding Cuts in
+* a Branch&Cut Framework: a Computational Study with {0,1/2}-Cuts",
+* Preliminary Draft, October 28, 2003, pp.6-8. */
+
+struct info
+{ /* estimated cut efficiency */
+ IOSCUT *cut;
+ /* pointer to cut in the cut pool */
+ char flag;
+ /* if this flag is set, the cut is included into the current
+ subproblem */
+ double eff;
+ /* cut efficacy (normalized residual) */
+ double deg;
+ /* lower bound to objective degradation */
+};
+
+static int CDECL fcmp(const void *arg1, const void *arg2)
+{ const struct info *info1 = arg1, *info2 = arg2;
+ if (info1->deg == 0.0 && info2->deg == 0.0)
+ { if (info1->eff > info2->eff) return -1;
+ if (info1->eff < info2->eff) return +1;
+ }
+ else
+ { if (info1->deg > info2->deg) return -1;
+ if (info1->deg < info2->deg) return +1;
+ }
+ return 0;
+}
+
+static double parallel(IOSCUT *a, IOSCUT *b, double work[]);
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+void ios_process_cuts(glp_tree *T)
+{ IOSPOOL *pool;
+ IOSCUT *cut;
+ GLPAIJ *aij;
+ struct info *info;
+ int k, kk, max_cuts, len, ret, *ind;
+ double *val, *work, rhs;
+ /* the current subproblem must exist */
+ xassert(T->curr != NULL);
+ /* the pool must exist and be non-empty */
+ pool = T->local;
+ xassert(pool != NULL);
+ xassert(pool->m > 0);
+ /* allocate working arrays */
+ info = xcalloc(1+pool->m, sizeof(struct info));
+ ind = xcalloc(1+T->n, sizeof(int));
+ val = xcalloc(1+T->n, sizeof(double));
+ work = xcalloc(1+T->n, sizeof(double));
+ for (k = 1; k <= T->n; k++) work[k] = 0.0;
+ /* build the list of cuts stored in the cut pool */
+ for (k = 1; k <= pool->m; k++)
+ info[k].cut = pool->row[k], info[k].flag = 0;
+ /* estimate efficiency of all cuts in the cut pool */
+ for (k = 1; k <= pool->m; k++)
+ { double temp, dy, dz;
+ cut = info[k].cut;
+ /* build the vector of cut coefficients and compute its
+ Euclidean norm */
+ len = 0; temp = 0.0;
+ for (aij = cut->ptr; aij != NULL; aij = aij->r_next)
+ { xassert(1 <= aij->col->j && aij->col->j <= T->n);
+ len++, ind[len] = aij->col->j, val[len] = aij->val;
+ temp += aij->val * aij->val;
+ }
+ if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
+ /* transform the cut to express it only through non-basic
+ (auxiliary and structural) variables */
+ len = glp_transform_row(T->mip, len, ind, val);
+ /* determine change in the cut value and in the objective
+ value for the adjacent basis by simulating one step of the
+ dual simplex */
+ switch (cut->type)
+ { case GLP_LO: rhs = cut->lb; break;
+ case GLP_UP: rhs = cut->ub; break;
+ default: xassert(cut != cut);
+ }
+ ret = _glp_analyze_row(T->mip, len, ind, val, cut->type,
+ rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz);
+ /* determine normalized residual and lower bound to objective
+ degradation */
+ if (ret == 0)
+ { info[k].eff = fabs(dy) / sqrt(temp);
+ /* if some reduced costs violates (slightly) their zero
+ bounds (i.e. have wrong signs) due to round-off errors,
+ dz also may have wrong sign being close to zero */
+ if (T->mip->dir == GLP_MIN)
+ { if (dz < 0.0) dz = 0.0;
+ info[k].deg = + dz;
+ }
+ else /* GLP_MAX */
+ { if (dz > 0.0) dz = 0.0;
+ info[k].deg = - dz;
+ }
+ }
+ else if (ret == 1)
+ { /* the constraint is not violated at the current point */
+ info[k].eff = info[k].deg = 0.0;
+ }
+ else if (ret == 2)
+ { /* no dual feasible adjacent basis exists */
+ info[k].eff = 1.0;
+ info[k].deg = DBL_MAX;
+ }
+ else
+ xassert(ret != ret);
+ /* if the degradation is too small, just ignore it */
+ if (info[k].deg < 0.01) info[k].deg = 0.0;
+ }
+ /* sort the list of cuts by decreasing objective degradation and
+ then by decreasing efficacy */
+ qsort(&info[1], pool->m, sizeof(struct info), fcmp);
+ /* only first (most efficient) max_cuts in the list are qualified
+ as candidates to be added to the current subproblem */
+ max_cuts = (T->curr->level == 0 ? 90 : 10);
+ if (max_cuts > pool->m) max_cuts = pool->m;
+ /* add cuts to the current subproblem */
+#if 0
+ xprintf("*** adding cuts ***\n");
+#endif
+ for (k = 1; k <= max_cuts; k++)
+ { int i, len;
+ /* if this cut seems to be inefficient, skip it */
+ if (info[k].deg < 0.01 && info[k].eff < 0.01) continue;
+ /* if the angle between this cut and every other cut included
+ in the current subproblem is small, skip this cut */
+ for (kk = 1; kk < k; kk++)
+ { if (info[kk].flag)
+ { if (parallel(info[k].cut, info[kk].cut, work) > 0.90)
+ break;
+ }
+ }
+ if (kk < k) continue;
+ /* add this cut to the current subproblem */
+#if 0
+ xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg);
+#endif
+ cut = info[k].cut, info[k].flag = 1;
+ i = glp_add_rows(T->mip, 1);
+ if (cut->name != NULL)
+ glp_set_row_name(T->mip, i, cut->name);
+ xassert(T->mip->row[i]->origin == GLP_RF_CUT);
+ T->mip->row[i]->klass = cut->klass;
+ len = 0;
+ for (aij = cut->ptr; aij != NULL; aij = aij->r_next)
+ len++, ind[len] = aij->col->j, val[len] = aij->val;
+ glp_set_mat_row(T->mip, i, len, ind, val);
+ switch (cut->type)
+ { case GLP_LO: rhs = cut->lb; break;
+ case GLP_UP: rhs = cut->ub; break;
+ default: xassert(cut != cut);
+ }
+ glp_set_row_bnds(T->mip, i, cut->type, rhs, rhs);
+ }
+ /* free working arrays */
+ xfree(info);
+ xfree(ind);
+ xfree(val);
+ xfree(work);
+ return;
+}
+#else
+void ios_process_cuts(glp_tree *T)
+{ IOSPOOL *pool;
+ IOSCUT *cut;
+ IOSAIJ *aij;
+ struct info *info;
+ int k, kk, max_cuts, len, ret, *ind;
+ double *val, *work;
+ /* the current subproblem must exist */
+ xassert(T->curr != NULL);
+ /* the pool must exist and be non-empty */
+ pool = T->local;
+ xassert(pool != NULL);
+ xassert(pool->size > 0);
+ /* allocate working arrays */
+ info = xcalloc(1+pool->size, sizeof(struct info));
+ ind = xcalloc(1+T->n, sizeof(int));
+ val = xcalloc(1+T->n, sizeof(double));
+ work = xcalloc(1+T->n, sizeof(double));
+ for (k = 1; k <= T->n; k++) work[k] = 0.0;
+ /* build the list of cuts stored in the cut pool */
+ for (k = 0, cut = pool->head; cut != NULL; cut = cut->next)
+ k++, info[k].cut = cut, info[k].flag = 0;
+ xassert(k == pool->size);
+ /* estimate efficiency of all cuts in the cut pool */
+ for (k = 1; k <= pool->size; k++)
+ { double temp, dy, dz;
+ cut = info[k].cut;
+ /* build the vector of cut coefficients and compute its
+ Euclidean norm */
+ len = 0; temp = 0.0;
+ for (aij = cut->ptr; aij != NULL; aij = aij->next)
+ { xassert(1 <= aij->j && aij->j <= T->n);
+ len++, ind[len] = aij->j, val[len] = aij->val;
+ temp += aij->val * aij->val;
+ }
+ if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
+ /* transform the cut to express it only through non-basic
+ (auxiliary and structural) variables */
+ len = glp_transform_row(T->mip, len, ind, val);
+ /* determine change in the cut value and in the objective
+ value for the adjacent basis by simulating one step of the
+ dual simplex */
+ ret = _glp_analyze_row(T->mip, len, ind, val, cut->type,
+ cut->rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz);
+ /* determine normalized residual and lower bound to objective
+ degradation */
+ if (ret == 0)
+ { info[k].eff = fabs(dy) / sqrt(temp);
+ /* if some reduced costs violates (slightly) their zero
+ bounds (i.e. have wrong signs) due to round-off errors,
+ dz also may have wrong sign being close to zero */
+ if (T->mip->dir == GLP_MIN)
+ { if (dz < 0.0) dz = 0.0;
+ info[k].deg = + dz;
+ }
+ else /* GLP_MAX */
+ { if (dz > 0.0) dz = 0.0;
+ info[k].deg = - dz;
+ }
+ }
+ else if (ret == 1)
+ { /* the constraint is not violated at the current point */
+ info[k].eff = info[k].deg = 0.0;
+ }
+ else if (ret == 2)
+ { /* no dual feasible adjacent basis exists */
+ info[k].eff = 1.0;
+ info[k].deg = DBL_MAX;
+ }
+ else
+ xassert(ret != ret);
+ /* if the degradation is too small, just ignore it */
+ if (info[k].deg < 0.01) info[k].deg = 0.0;
+ }
+ /* sort the list of cuts by decreasing objective degradation and
+ then by decreasing efficacy */
+ qsort(&info[1], pool->size, sizeof(struct info), fcmp);
+ /* only first (most efficient) max_cuts in the list are qualified
+ as candidates to be added to the current subproblem */
+ max_cuts = (T->curr->level == 0 ? 90 : 10);
+ if (max_cuts > pool->size) max_cuts = pool->size;
+ /* add cuts to the current subproblem */
+#if 0
+ xprintf("*** adding cuts ***\n");
+#endif
+ for (k = 1; k <= max_cuts; k++)
+ { int i, len;
+ /* if this cut seems to be inefficient, skip it */
+ if (info[k].deg < 0.01 && info[k].eff < 0.01) continue;
+ /* if the angle between this cut and every other cut included
+ in the current subproblem is small, skip this cut */
+ for (kk = 1; kk < k; kk++)
+ { if (info[kk].flag)
+ { if (parallel(info[k].cut, info[kk].cut, work) > 0.90)
+ break;
+ }
+ }
+ if (kk < k) continue;
+ /* add this cut to the current subproblem */
+#if 0
+ xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg);
+#endif
+ cut = info[k].cut, info[k].flag = 1;
+ i = glp_add_rows(T->mip, 1);
+ if (cut->name != NULL)
+ glp_set_row_name(T->mip, i, cut->name);
+ xassert(T->mip->row[i]->origin == GLP_RF_CUT);
+ T->mip->row[i]->klass = cut->klass;
+ len = 0;
+ for (aij = cut->ptr; aij != NULL; aij = aij->next)
+ len++, ind[len] = aij->j, val[len] = aij->val;
+ glp_set_mat_row(T->mip, i, len, ind, val);
+ xassert(cut->type == GLP_LO || cut->type == GLP_UP);
+ glp_set_row_bnds(T->mip, i, cut->type, cut->rhs, cut->rhs);
+ }
+ /* free working arrays */
+ xfree(info);
+ xfree(ind);
+ xfree(val);
+ xfree(work);
+ return;
+}
+#endif
+
+#if 0
+/***********************************************************************
+* Given a cut a * x >= b (<= b) the routine efficacy computes the cut
+* efficacy as follows:
+*
+* eff = d * (a * x~ - b) / ||a||,
+*
+* where d is -1 (in case of '>= b') or +1 (in case of '<= b'), x~ is
+* the vector of values of structural variables in optimal solution to
+* LP relaxation of the current subproblem, ||a|| is the Euclidean norm
+* of the vector of cut coefficients.
+*
+* If the cut is violated at point x~, the efficacy eff is positive,
+* and its value is the Euclidean distance between x~ and the cut plane
+* a * x = b in the space of structural variables.
+*
+* Following geometrical intuition, it is quite natural to consider
+* this distance as a first-order measure of the expected efficacy of
+* the cut: the larger the distance the better the cut [1]. */
+
+static double efficacy(glp_tree *T, IOSCUT *cut)
+{ glp_prob *mip = T->mip;
+ IOSAIJ *aij;
+ double s = 0.0, t = 0.0, temp;
+ for (aij = cut->ptr; aij != NULL; aij = aij->next)
+ { xassert(1 <= aij->j && aij->j <= mip->n);
+ s += aij->val * mip->col[aij->j]->prim;
+ t += aij->val * aij->val;
+ }
+ temp = sqrt(t);
+ if (temp < DBL_EPSILON) temp = DBL_EPSILON;
+ if (cut->type == GLP_LO)
+ temp = (s >= cut->rhs ? 0.0 : (cut->rhs - s) / temp);
+ else if (cut->type == GLP_UP)
+ temp = (s <= cut->rhs ? 0.0 : (s - cut->rhs) / temp);
+ else
+ xassert(cut != cut);
+ return temp;
+}
+#endif
+
+/***********************************************************************
+* Given two cuts a1 * x >= b1 (<= b1) and a2 * x >= b2 (<= b2) the
+* routine parallel computes the cosine of angle between the cut planes
+* a1 * x = b1 and a2 * x = b2 (which is the acute angle between two
+* normals to these planes) in the space of structural variables as
+* follows:
+*
+* cos phi = (a1' * a2) / (||a1|| * ||a2||),
+*
+* where (a1' * a2) is a dot product of vectors of cut coefficients,
+* ||a1|| and ||a2|| are Euclidean norms of vectors a1 and a2.
+*
+* Note that requirement cos phi = 0 forces the cuts to be orthogonal,
+* i.e. with disjoint support, while requirement cos phi <= 0.999 means
+* only avoiding duplicate (parallel) cuts [1]. */
+
+#ifdef NEW_LOCAL /* 02/II-2018 */
+static double parallel(IOSCUT *a, IOSCUT *b, double work[])
+{ GLPAIJ *aij;
+ double s = 0.0, sa = 0.0, sb = 0.0, temp;
+ for (aij = a->ptr; aij != NULL; aij = aij->r_next)
+ { work[aij->col->j] = aij->val;
+ sa += aij->val * aij->val;
+ }
+ for (aij = b->ptr; aij != NULL; aij = aij->r_next)
+ { s += work[aij->col->j] * aij->val;
+ sb += aij->val * aij->val;
+ }
+ for (aij = a->ptr; aij != NULL; aij = aij->r_next)
+ work[aij->col->j] = 0.0;
+ temp = sqrt(sa) * sqrt(sb);
+ if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
+ return s / temp;
+}
+#else
+static double parallel(IOSCUT *a, IOSCUT *b, double work[])
+{ IOSAIJ *aij;
+ double s = 0.0, sa = 0.0, sb = 0.0, temp;
+ for (aij = a->ptr; aij != NULL; aij = aij->next)
+ { work[aij->j] = aij->val;
+ sa += aij->val * aij->val;
+ }
+ for (aij = b->ptr; aij != NULL; aij = aij->next)
+ { s += work[aij->j] * aij->val;
+ sb += aij->val * aij->val;
+ }
+ for (aij = a->ptr; aij != NULL; aij = aij->next)
+ work[aij->j] = 0.0;
+ temp = sqrt(sa) * sqrt(sb);
+ if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
+ return s / temp;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpios12.c b/test/monniaux/glpk-4.65/src/draft/glpios12.c
new file mode 100644
index 00000000..bec6fa2c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpios12.c
@@ -0,0 +1,177 @@
+/* glpios12.c (node selection heuristics) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+
+/***********************************************************************
+* NAME
+*
+* ios_choose_node - select subproblem to continue the search
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* int ios_choose_node(glp_tree *T);
+*
+* DESCRIPTION
+*
+* The routine ios_choose_node selects a subproblem from the active
+* list to continue the search. The choice depends on the backtracking
+* technique option.
+*
+* RETURNS
+*
+* The routine ios_choose_node return the reference number of the
+* subproblem selected. */
+
+static int most_feas(glp_tree *T);
+static int best_proj(glp_tree *T);
+static int best_node(glp_tree *T);
+
+int ios_choose_node(glp_tree *T)
+{ int p;
+ if (T->parm->bt_tech == GLP_BT_DFS)
+ { /* depth first search */
+ xassert(T->tail != NULL);
+ p = T->tail->p;
+ }
+ else if (T->parm->bt_tech == GLP_BT_BFS)
+ { /* breadth first search */
+ xassert(T->head != NULL);
+ p = T->head->p;
+ }
+ else if (T->parm->bt_tech == GLP_BT_BLB)
+ { /* select node with best local bound */
+ p = best_node(T);
+ }
+ else if (T->parm->bt_tech == GLP_BT_BPH)
+ { if (T->mip->mip_stat == GLP_UNDEF)
+ { /* "most integer feasible" subproblem */
+ p = most_feas(T);
+ }
+ else
+ { /* best projection heuristic */
+ p = best_proj(T);
+ }
+ }
+ else
+ xassert(T != T);
+ return p;
+}
+
+static int most_feas(glp_tree *T)
+{ /* select subproblem whose parent has minimal sum of integer
+ infeasibilities */
+ IOSNPD *node;
+ int p;
+ double best;
+ p = 0, best = DBL_MAX;
+ for (node = T->head; node != NULL; node = node->next)
+ { xassert(node->up != NULL);
+ if (best > node->up->ii_sum)
+ p = node->p, best = node->up->ii_sum;
+ }
+ return p;
+}
+
+static int best_proj(glp_tree *T)
+{ /* select subproblem using the best projection heuristic */
+ IOSNPD *root, *node;
+ int p;
+ double best, deg, obj;
+ /* the global bound must exist */
+ xassert(T->mip->mip_stat == GLP_FEAS);
+ /* obtain pointer to the root node, which must exist */
+ root = T->slot[1].node;
+ xassert(root != NULL);
+ /* deg estimates degradation of the objective function per unit
+ of the sum of integer infeasibilities */
+ xassert(root->ii_sum > 0.0);
+ deg = (T->mip->mip_obj - root->bound) / root->ii_sum;
+ /* nothing has been selected so far */
+ p = 0, best = DBL_MAX;
+ /* walk through the list of active subproblems */
+ for (node = T->head; node != NULL; node = node->next)
+ { xassert(node->up != NULL);
+ /* obj estimates optimal objective value if the sum of integer
+ infeasibilities were zero */
+ obj = node->up->bound + deg * node->up->ii_sum;
+ if (T->mip->dir == GLP_MAX) obj = - obj;
+ /* select the subproblem which has the best estimated optimal
+ objective value */
+ if (best > obj) p = node->p, best = obj;
+ }
+ return p;
+}
+
+static int best_node(glp_tree *T)
+{ /* select subproblem with best local bound */
+ IOSNPD *node, *best = NULL;
+ double bound, eps;
+ switch (T->mip->dir)
+ { case GLP_MIN:
+ bound = +DBL_MAX;
+ for (node = T->head; node != NULL; node = node->next)
+ if (bound > node->bound) bound = node->bound;
+ xassert(bound != +DBL_MAX);
+ eps = 1e-10 * (1.0 + fabs(bound));
+ for (node = T->head; node != NULL; node = node->next)
+ { if (node->bound <= bound + eps)
+ { xassert(node->up != NULL);
+ if (best == NULL ||
+#if 1
+ best->up->ii_sum > node->up->ii_sum) best = node;
+#else
+ best->lp_obj > node->lp_obj) best = node;
+#endif
+ }
+ }
+ break;
+ case GLP_MAX:
+ bound = -DBL_MAX;
+ for (node = T->head; node != NULL; node = node->next)
+ if (bound < node->bound) bound = node->bound;
+ xassert(bound != -DBL_MAX);
+ eps = 1e-10 * (1.0 + fabs(bound));
+ for (node = T->head; node != NULL; node = node->next)
+ { if (node->bound >= bound - eps)
+ { xassert(node->up != NULL);
+ if (best == NULL ||
+#if 1
+ best->up->ii_sum > node->up->ii_sum) best = node;
+#else
+ best->lp_obj < node->lp_obj) best = node;
+#endif
+ }
+ }
+ break;
+ default:
+ xassert(T != T);
+ }
+ xassert(best != NULL);
+ return best->p;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpipm.c b/test/monniaux/glpk-4.65/src/draft/glpipm.c
new file mode 100644
index 00000000..2b3a8176
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpipm.c
@@ -0,0 +1,1144 @@
+/* glpipm.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpipm.h"
+#include "glpmat.h"
+
+#define ITER_MAX 100
+/* maximal number of iterations */
+
+struct csa
+{ /* common storage area */
+ /*--------------------------------------------------------------*/
+ /* LP data */
+ int m;
+ /* number of rows (equality constraints) */
+ int n;
+ /* number of columns (structural variables) */
+ int *A_ptr; /* int A_ptr[1+m+1]; */
+ int *A_ind; /* int A_ind[A_ptr[m+1]]; */
+ double *A_val; /* double A_val[A_ptr[m+1]]; */
+ /* mxn-matrix A in storage-by-rows format */
+ double *b; /* double b[1+m]; */
+ /* m-vector b of right-hand sides */
+ double *c; /* double c[1+n]; */
+ /* n-vector c of objective coefficients; c[0] is constant term of
+ the objective function */
+ /*--------------------------------------------------------------*/
+ /* LP solution */
+ double *x; /* double x[1+n]; */
+ double *y; /* double y[1+m]; */
+ double *z; /* double z[1+n]; */
+ /* current point in primal-dual space; the best point on exit */
+ /*--------------------------------------------------------------*/
+ /* control parameters */
+ const glp_iptcp *parm;
+ /*--------------------------------------------------------------*/
+ /* working arrays and variables */
+ double *D; /* double D[1+n]; */
+ /* diagonal nxn-matrix D = X*inv(Z), where X = diag(x[j]) and
+ Z = diag(z[j]) */
+ int *P; /* int P[1+m+m]; */
+ /* permutation mxm-matrix P used to minimize fill-in in Cholesky
+ factorization */
+ int *S_ptr; /* int S_ptr[1+m+1]; */
+ int *S_ind; /* int S_ind[S_ptr[m+1]]; */
+ double *S_val; /* double S_val[S_ptr[m+1]]; */
+ double *S_diag; /* double S_diag[1+m]; */
+ /* symmetric mxm-matrix S = P*A*D*A'*P' whose upper triangular
+ part without diagonal elements is stored in S_ptr, S_ind, and
+ S_val in storage-by-rows format, diagonal elements are stored
+ in S_diag */
+ int *U_ptr; /* int U_ptr[1+m+1]; */
+ int *U_ind; /* int U_ind[U_ptr[m+1]]; */
+ double *U_val; /* double U_val[U_ptr[m+1]]; */
+ double *U_diag; /* double U_diag[1+m]; */
+ /* upper triangular mxm-matrix U defining Cholesky factorization
+ S = U'*U; its non-diagonal elements are stored in U_ptr, U_ind,
+ U_val in storage-by-rows format, diagonal elements are stored
+ in U_diag */
+ int iter;
+ /* iteration number (0, 1, 2, ...); iter = 0 corresponds to the
+ initial point */
+ double obj;
+ /* current value of the objective function */
+ double rpi;
+ /* relative primal infeasibility rpi = ||A*x-b||/(1+||b||) */
+ double rdi;
+ /* relative dual infeasibility rdi = ||A'*y+z-c||/(1+||c||) */
+ double gap;
+ /* primal-dual gap = |c'*x-b'*y|/(1+|c'*x|) which is a relative
+ difference between primal and dual objective functions */
+ double phi;
+ /* merit function phi = ||A*x-b||/max(1,||b||) +
+ + ||A'*y+z-c||/max(1,||c||) +
+ + |c'*x-b'*y|/max(1,||b||,||c||) */
+ double mu;
+ /* duality measure mu = x'*z/n (used as barrier parameter) */
+ double rmu;
+ /* rmu = max(||A*x-b||,||A'*y+z-c||)/mu */
+ double rmu0;
+ /* the initial value of rmu on iteration 0 */
+ double *phi_min; /* double phi_min[1+ITER_MAX]; */
+ /* phi_min[k] = min(phi[k]), where phi[k] is the value of phi on
+ k-th iteration, 0 <= k <= iter */
+ int best_iter;
+ /* iteration number, on which the value of phi reached its best
+ (minimal) value */
+ double *best_x; /* double best_x[1+n]; */
+ double *best_y; /* double best_y[1+m]; */
+ double *best_z; /* double best_z[1+n]; */
+ /* best point (in the sense of the merit function phi) which has
+ been reached on iteration iter_best */
+ double best_obj;
+ /* objective value at the best point */
+ double *dx_aff; /* double dx_aff[1+n]; */
+ double *dy_aff; /* double dy_aff[1+m]; */
+ double *dz_aff; /* double dz_aff[1+n]; */
+ /* affine scaling direction */
+ double alfa_aff_p, alfa_aff_d;
+ /* maximal primal and dual stepsizes in affine scaling direction,
+ on which x and z are still non-negative */
+ double mu_aff;
+ /* duality measure mu_aff = x_aff'*z_aff/n in the boundary point
+ x_aff' = x+alfa_aff_p*dx_aff, z_aff' = z+alfa_aff_d*dz_aff */
+ double sigma;
+ /* Mehrotra's heuristic parameter (0 <= sigma <= 1) */
+ double *dx_cc; /* double dx_cc[1+n]; */
+ double *dy_cc; /* double dy_cc[1+m]; */
+ double *dz_cc; /* double dz_cc[1+n]; */
+ /* centering corrector direction */
+ double *dx; /* double dx[1+n]; */
+ double *dy; /* double dy[1+m]; */
+ double *dz; /* double dz[1+n]; */
+ /* final combined direction dx = dx_aff+dx_cc, dy = dy_aff+dy_cc,
+ dz = dz_aff+dz_cc */
+ double alfa_max_p;
+ double alfa_max_d;
+ /* maximal primal and dual stepsizes in combined direction, on
+ which x and z are still non-negative */
+};
+
+/***********************************************************************
+* initialize - allocate and initialize common storage area
+*
+* This routine allocates and initializes the common storage area (CSA)
+* used by interior-point method routines. */
+
+static void initialize(struct csa *csa)
+{ int m = csa->m;
+ int n = csa->n;
+ int i;
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Matrix A has %d non-zeros\n", csa->A_ptr[m+1]-1);
+ csa->D = xcalloc(1+n, sizeof(double));
+ /* P := I */
+ csa->P = xcalloc(1+m+m, sizeof(int));
+ for (i = 1; i <= m; i++) csa->P[i] = csa->P[m+i] = i;
+ /* S := A*A', symbolically */
+ csa->S_ptr = xcalloc(1+m+1, sizeof(int));
+ csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
+ csa->S_ptr);
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Matrix S = A*A' has %d non-zeros (upper triangle)\n",
+ csa->S_ptr[m+1]-1 + m);
+ /* determine P using specified ordering algorithm */
+ if (csa->parm->ord_alg == GLP_ORD_NONE)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Original ordering is being used\n");
+ for (i = 1; i <= m; i++)
+ csa->P[i] = csa->P[m+i] = i;
+ }
+ else if (csa->parm->ord_alg == GLP_ORD_QMD)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Minimum degree ordering (QMD)...\n");
+ min_degree(m, csa->S_ptr, csa->S_ind, csa->P);
+ }
+ else if (csa->parm->ord_alg == GLP_ORD_AMD)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Approximate minimum degree ordering (AMD)...\n");
+ amd_order1(m, csa->S_ptr, csa->S_ind, csa->P);
+ }
+ else if (csa->parm->ord_alg == GLP_ORD_SYMAMD)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Approximate minimum degree ordering (SYMAMD)...\n")
+ ;
+ symamd_ord(m, csa->S_ptr, csa->S_ind, csa->P);
+ }
+ else
+ xassert(csa != csa);
+ /* S := P*A*A'*P', symbolically */
+ xfree(csa->S_ind);
+ csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
+ csa->S_ptr);
+ csa->S_val = xcalloc(csa->S_ptr[m+1], sizeof(double));
+ csa->S_diag = xcalloc(1+m, sizeof(double));
+ /* compute Cholesky factorization S = U'*U, symbolically */
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Computing Cholesky factorization S = L*L'...\n");
+ csa->U_ptr = xcalloc(1+m+1, sizeof(int));
+ csa->U_ind = chol_symbolic(m, csa->S_ptr, csa->S_ind, csa->U_ptr);
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Matrix L has %d non-zeros\n", csa->U_ptr[m+1]-1 + m);
+ csa->U_val = xcalloc(csa->U_ptr[m+1], sizeof(double));
+ csa->U_diag = xcalloc(1+m, sizeof(double));
+ csa->iter = 0;
+ csa->obj = 0.0;
+ csa->rpi = 0.0;
+ csa->rdi = 0.0;
+ csa->gap = 0.0;
+ csa->phi = 0.0;
+ csa->mu = 0.0;
+ csa->rmu = 0.0;
+ csa->rmu0 = 0.0;
+ csa->phi_min = xcalloc(1+ITER_MAX, sizeof(double));
+ csa->best_iter = 0;
+ csa->best_x = xcalloc(1+n, sizeof(double));
+ csa->best_y = xcalloc(1+m, sizeof(double));
+ csa->best_z = xcalloc(1+n, sizeof(double));
+ csa->best_obj = 0.0;
+ csa->dx_aff = xcalloc(1+n, sizeof(double));
+ csa->dy_aff = xcalloc(1+m, sizeof(double));
+ csa->dz_aff = xcalloc(1+n, sizeof(double));
+ csa->alfa_aff_p = 0.0;
+ csa->alfa_aff_d = 0.0;
+ csa->mu_aff = 0.0;
+ csa->sigma = 0.0;
+ csa->dx_cc = xcalloc(1+n, sizeof(double));
+ csa->dy_cc = xcalloc(1+m, sizeof(double));
+ csa->dz_cc = xcalloc(1+n, sizeof(double));
+ csa->dx = csa->dx_aff;
+ csa->dy = csa->dy_aff;
+ csa->dz = csa->dz_aff;
+ csa->alfa_max_p = 0.0;
+ csa->alfa_max_d = 0.0;
+ return;
+}
+
+/***********************************************************************
+* A_by_vec - compute y = A*x
+*
+* This routine computes matrix-vector product y = A*x, where A is the
+* constraint matrix. */
+
+static void A_by_vec(struct csa *csa, double x[], double y[])
+{ /* compute y = A*x */
+ int m = csa->m;
+ int *A_ptr = csa->A_ptr;
+ int *A_ind = csa->A_ind;
+ double *A_val = csa->A_val;
+ int i, t, beg, end;
+ double temp;
+ for (i = 1; i <= m; i++)
+ { temp = 0.0;
+ beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++) temp += A_val[t] * x[A_ind[t]];
+ y[i] = temp;
+ }
+ return;
+}
+
+/***********************************************************************
+* AT_by_vec - compute y = A'*x
+*
+* This routine computes matrix-vector product y = A'*x, where A' is a
+* matrix transposed to the constraint matrix A. */
+
+static void AT_by_vec(struct csa *csa, double x[], double y[])
+{ /* compute y = A'*x, where A' is transposed to A */
+ int m = csa->m;
+ int n = csa->n;
+ int *A_ptr = csa->A_ptr;
+ int *A_ind = csa->A_ind;
+ double *A_val = csa->A_val;
+ int i, j, t, beg, end;
+ double temp;
+ for (j = 1; j <= n; j++) y[j] = 0.0;
+ for (i = 1; i <= m; i++)
+ { temp = x[i];
+ if (temp == 0.0) continue;
+ beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++) y[A_ind[t]] += A_val[t] * temp;
+ }
+ return;
+}
+
+/***********************************************************************
+* decomp_NE - numeric factorization of matrix S = P*A*D*A'*P'
+*
+* This routine implements numeric phase of Cholesky factorization of
+* the matrix S = P*A*D*A'*P', which is a permuted matrix of the normal
+* equation system. Matrix D is assumed to be already computed. */
+
+static void decomp_NE(struct csa *csa)
+{ adat_numeric(csa->m, csa->n, csa->P, csa->A_ptr, csa->A_ind,
+ csa->A_val, csa->D, csa->S_ptr, csa->S_ind, csa->S_val,
+ csa->S_diag);
+ chol_numeric(csa->m, csa->S_ptr, csa->S_ind, csa->S_val,
+ csa->S_diag, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag);
+ return;
+}
+
+/***********************************************************************
+* solve_NE - solve normal equation system
+*
+* This routine solves the normal equation system:
+*
+* A*D*A'*y = h.
+*
+* It is assumed that the matrix A*D*A' has been previously factorized
+* by the routine decomp_NE.
+*
+* On entry the array y contains the vector of right-hand sides h. On
+* exit this array contains the computed vector of unknowns y.
+*
+* Once the vector y has been computed the routine checks for numeric
+* stability. If the residual vector:
+*
+* r = A*D*A'*y - h
+*
+* is relatively small, the routine returns zero, otherwise non-zero is
+* returned. */
+
+static int solve_NE(struct csa *csa, double y[])
+{ int m = csa->m;
+ int n = csa->n;
+ int *P = csa->P;
+ int i, j, ret = 0;
+ double *h, *r, *w;
+ /* save vector of right-hand sides h */
+ h = xcalloc(1+m, sizeof(double));
+ for (i = 1; i <= m; i++) h[i] = y[i];
+ /* solve normal equation system (A*D*A')*y = h */
+ /* since S = P*A*D*A'*P' = U'*U, then A*D*A' = P'*U'*U*P, so we
+ have inv(A*D*A') = P'*inv(U)*inv(U')*P */
+ /* w := P*h */
+ w = xcalloc(1+m, sizeof(double));
+ for (i = 1; i <= m; i++) w[i] = y[P[i]];
+ /* w := inv(U')*w */
+ ut_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
+ /* w := inv(U)*w */
+ u_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
+ /* y := P'*w */
+ for (i = 1; i <= m; i++) y[i] = w[P[m+i]];
+ xfree(w);
+ /* compute residual vector r = A*D*A'*y - h */
+ r = xcalloc(1+m, sizeof(double));
+ /* w := A'*y */
+ w = xcalloc(1+n, sizeof(double));
+ AT_by_vec(csa, y, w);
+ /* w := D*w */
+ for (j = 1; j <= n; j++) w[j] *= csa->D[j];
+ /* r := A*w */
+ A_by_vec(csa, w, r);
+ xfree(w);
+ /* r := r - h */
+ for (i = 1; i <= m; i++) r[i] -= h[i];
+ /* check for numeric stability */
+ for (i = 1; i <= m; i++)
+ { if (fabs(r[i]) / (1.0 + fabs(h[i])) > 1e-4)
+ { ret = 1;
+ break;
+ }
+ }
+ xfree(h);
+ xfree(r);
+ return ret;
+}
+
+/***********************************************************************
+* solve_NS - solve Newtonian system
+*
+* This routine solves the Newtonian system:
+*
+* A*dx = p
+*
+* A'*dy + dz = q
+*
+* Z*dx + X*dz = r
+*
+* where X = diag(x[j]), Z = diag(z[j]), by reducing it to the normal
+* equation system:
+*
+* (A*inv(Z)*X*A')*dy = A*inv(Z)*(X*q-r)+p
+*
+* (it is assumed that the matrix A*inv(Z)*X*A' has been factorized by
+* the routine decomp_NE).
+*
+* Once vector dy has been computed the routine computes vectors dx and
+* dz as follows:
+*
+* dx = inv(Z)*(X*(A'*dy-q)+r)
+*
+* dz = inv(X)*(r-Z*dx)
+*
+* The routine solve_NS returns the same code which was reported by the
+* routine solve_NE (see above). */
+
+static int solve_NS(struct csa *csa, double p[], double q[], double r[],
+ double dx[], double dy[], double dz[])
+{ int m = csa->m;
+ int n = csa->n;
+ double *x = csa->x;
+ double *z = csa->z;
+ int i, j, ret;
+ double *w = dx;
+ /* compute the vector of right-hand sides A*inv(Z)*(X*q-r)+p for
+ the normal equation system */
+ for (j = 1; j <= n; j++)
+ w[j] = (x[j] * q[j] - r[j]) / z[j];
+ A_by_vec(csa, w, dy);
+ for (i = 1; i <= m; i++) dy[i] += p[i];
+ /* solve the normal equation system to compute vector dy */
+ ret = solve_NE(csa, dy);
+ /* compute vectors dx and dz */
+ AT_by_vec(csa, dy, dx);
+ for (j = 1; j <= n; j++)
+ { dx[j] = (x[j] * (dx[j] - q[j]) + r[j]) / z[j];
+ dz[j] = (r[j] - z[j] * dx[j]) / x[j];
+ }
+ return ret;
+}
+
+/***********************************************************************
+* initial_point - choose initial point using Mehrotra's heuristic
+*
+* This routine chooses a starting point using a heuristic proposed in
+* the paper:
+*
+* S. Mehrotra. On the implementation of a primal-dual interior point
+* method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
+*
+* The starting point x in the primal space is chosen as a solution of
+* the following least squares problem:
+*
+* minimize ||x||
+*
+* subject to A*x = b
+*
+* which can be computed explicitly as follows:
+*
+* x = A'*inv(A*A')*b
+*
+* Similarly, the starting point (y, z) in the dual space is chosen as
+* a solution of the following least squares problem:
+*
+* minimize ||z||
+*
+* subject to A'*y + z = c
+*
+* which can be computed explicitly as follows:
+*
+* y = inv(A*A')*A*c
+*
+* z = c - A'*y
+*
+* However, some components of the vectors x and z may be non-positive
+* or close to zero, so the routine uses a Mehrotra's heuristic to find
+* a more appropriate starting point. */
+
+static void initial_point(struct csa *csa)
+{ int m = csa->m;
+ int n = csa->n;
+ double *b = csa->b;
+ double *c = csa->c;
+ double *x = csa->x;
+ double *y = csa->y;
+ double *z = csa->z;
+ double *D = csa->D;
+ int i, j;
+ double dp, dd, ex, ez, xz;
+ /* factorize A*A' */
+ for (j = 1; j <= n; j++) D[j] = 1.0;
+ decomp_NE(csa);
+ /* x~ = A'*inv(A*A')*b */
+ for (i = 1; i <= m; i++) y[i] = b[i];
+ solve_NE(csa, y);
+ AT_by_vec(csa, y, x);
+ /* y~ = inv(A*A')*A*c */
+ A_by_vec(csa, c, y);
+ solve_NE(csa, y);
+ /* z~ = c - A'*y~ */
+ AT_by_vec(csa, y,z);
+ for (j = 1; j <= n; j++) z[j] = c[j] - z[j];
+ /* use Mehrotra's heuristic in order to choose more appropriate
+ starting point with positive components of vectors x and z */
+ dp = dd = 0.0;
+ for (j = 1; j <= n; j++)
+ { if (dp < -1.5 * x[j]) dp = -1.5 * x[j];
+ if (dd < -1.5 * z[j]) dd = -1.5 * z[j];
+ }
+ /* note that b = 0 involves x = 0, and c = 0 involves y = 0 and
+ z = 0, so we need to be careful */
+ if (dp == 0.0) dp = 1.5;
+ if (dd == 0.0) dd = 1.5;
+ ex = ez = xz = 0.0;
+ for (j = 1; j <= n; j++)
+ { ex += (x[j] + dp);
+ ez += (z[j] + dd);
+ xz += (x[j] + dp) * (z[j] + dd);
+ }
+ dp += 0.5 * (xz / ez);
+ dd += 0.5 * (xz / ex);
+ for (j = 1; j <= n; j++)
+ { x[j] += dp;
+ z[j] += dd;
+ xassert(x[j] > 0.0 && z[j] > 0.0);
+ }
+ return;
+}
+
+/***********************************************************************
+* basic_info - perform basic computations at the current point
+*
+* This routine computes the following quantities at the current point:
+*
+* 1) value of the objective function:
+*
+* F = c'*x + c[0]
+*
+* 2) relative primal infeasibility:
+*
+* rpi = ||A*x-b|| / (1+||b||)
+*
+* 3) relative dual infeasibility:
+*
+* rdi = ||A'*y+z-c|| / (1+||c||)
+*
+* 4) primal-dual gap (relative difference between the primal and the
+* dual objective function values):
+*
+* gap = |c'*x-b'*y| / (1+|c'*x|)
+*
+* 5) merit function:
+*
+* phi = ||A*x-b|| / max(1,||b||) + ||A'*y+z-c|| / max(1,||c||) +
+*
+* + |c'*x-b'*y| / max(1,||b||,||c||)
+*
+* 6) duality measure:
+*
+* mu = x'*z / n
+*
+* 7) the ratio of infeasibility to mu:
+*
+* rmu = max(||A*x-b||,||A'*y+z-c||) / mu
+*
+* where ||*|| denotes euclidian norm, *' denotes transposition. */
+
+static void basic_info(struct csa *csa)
+{ int m = csa->m;
+ int n = csa->n;
+ double *b = csa->b;
+ double *c = csa->c;
+ double *x = csa->x;
+ double *y = csa->y;
+ double *z = csa->z;
+ int i, j;
+ double norm1, bnorm, norm2, cnorm, cx, by, *work, temp;
+ /* compute value of the objective function */
+ temp = c[0];
+ for (j = 1; j <= n; j++) temp += c[j] * x[j];
+ csa->obj = temp;
+ /* norm1 = ||A*x-b|| */
+ work = xcalloc(1+m, sizeof(double));
+ A_by_vec(csa, x, work);
+ norm1 = 0.0;
+ for (i = 1; i <= m; i++)
+ norm1 += (work[i] - b[i]) * (work[i] - b[i]);
+ norm1 = sqrt(norm1);
+ xfree(work);
+ /* bnorm = ||b|| */
+ bnorm = 0.0;
+ for (i = 1; i <= m; i++) bnorm += b[i] * b[i];
+ bnorm = sqrt(bnorm);
+ /* compute relative primal infeasibility */
+ csa->rpi = norm1 / (1.0 + bnorm);
+ /* norm2 = ||A'*y+z-c|| */
+ work = xcalloc(1+n, sizeof(double));
+ AT_by_vec(csa, y, work);
+ norm2 = 0.0;
+ for (j = 1; j <= n; j++)
+ norm2 += (work[j] + z[j] - c[j]) * (work[j] + z[j] - c[j]);
+ norm2 = sqrt(norm2);
+ xfree(work);
+ /* cnorm = ||c|| */
+ cnorm = 0.0;
+ for (j = 1; j <= n; j++) cnorm += c[j] * c[j];
+ cnorm = sqrt(cnorm);
+ /* compute relative dual infeasibility */
+ csa->rdi = norm2 / (1.0 + cnorm);
+ /* by = b'*y */
+ by = 0.0;
+ for (i = 1; i <= m; i++) by += b[i] * y[i];
+ /* cx = c'*x */
+ cx = 0.0;
+ for (j = 1; j <= n; j++) cx += c[j] * x[j];
+ /* compute primal-dual gap */
+ csa->gap = fabs(cx - by) / (1.0 + fabs(cx));
+ /* compute merit function */
+ csa->phi = 0.0;
+ csa->phi += norm1 / (bnorm > 1.0 ? bnorm : 1.0);
+ csa->phi += norm2 / (cnorm > 1.0 ? cnorm : 1.0);
+ temp = 1.0;
+ if (temp < bnorm) temp = bnorm;
+ if (temp < cnorm) temp = cnorm;
+ csa->phi += fabs(cx - by) / temp;
+ /* compute duality measure */
+ temp = 0.0;
+ for (j = 1; j <= n; j++) temp += x[j] * z[j];
+ csa->mu = temp / (double)n;
+ /* compute the ratio of infeasibility to mu */
+ csa->rmu = (norm1 > norm2 ? norm1 : norm2) / csa->mu;
+ return;
+}
+
+/***********************************************************************
+* make_step - compute next point using Mehrotra's technique
+*
+* This routine computes the next point using the predictor-corrector
+* technique proposed in the paper:
+*
+* S. Mehrotra. On the implementation of a primal-dual interior point
+* method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
+*
+* At first, the routine computes so called affine scaling (predictor)
+* direction (dx_aff,dy_aff,dz_aff) which is a solution of the system:
+*
+* A*dx_aff = b - A*x
+*
+* A'*dy_aff + dz_aff = c - A'*y - z
+*
+* Z*dx_aff + X*dz_aff = - X*Z*e
+*
+* where (x,y,z) is the current point, X = diag(x[j]), Z = diag(z[j]),
+* e = (1,...,1)'.
+*
+* Then, the routine computes the centering parameter sigma, using the
+* following Mehrotra's heuristic:
+*
+* alfa_aff_p = inf{0 <= alfa <= 1 | x+alfa*dx_aff >= 0}
+*
+* alfa_aff_d = inf{0 <= alfa <= 1 | z+alfa*dz_aff >= 0}
+*
+* mu_aff = (x+alfa_aff_p*dx_aff)'*(z+alfa_aff_d*dz_aff)/n
+*
+* sigma = (mu_aff/mu)^3
+*
+* where alfa_aff_p is the maximal stepsize along the affine scaling
+* direction in the primal space, alfa_aff_d is the maximal stepsize
+* along the same direction in the dual space.
+*
+* After determining sigma the routine computes so called centering
+* (corrector) direction (dx_cc,dy_cc,dz_cc) which is the solution of
+* the system:
+*
+* A*dx_cc = 0
+*
+* A'*dy_cc + dz_cc = 0
+*
+* Z*dx_cc + X*dz_cc = sigma*mu*e - X*Z*e
+*
+* Finally, the routine computes the combined direction
+*
+* (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc)
+*
+* and determines maximal primal and dual stepsizes along the combined
+* direction:
+*
+* alfa_max_p = inf{0 <= alfa <= 1 | x+alfa*dx >= 0}
+*
+* alfa_max_d = inf{0 <= alfa <= 1 | z+alfa*dz >= 0}
+*
+* In order to prevent the next point to be too close to the boundary
+* of the positive ortant, the routine decreases maximal stepsizes:
+*
+* alfa_p = gamma_p * alfa_max_p
+*
+* alfa_d = gamma_d * alfa_max_d
+*
+* where gamma_p and gamma_d are scaling factors, and computes the next
+* point:
+*
+* x_new = x + alfa_p * dx
+*
+* y_new = y + alfa_d * dy
+*
+* z_new = z + alfa_d * dz
+*
+* which becomes the current point on the next iteration. */
+
+static int make_step(struct csa *csa)
+{ int m = csa->m;
+ int n = csa->n;
+ double *b = csa->b;
+ double *c = csa->c;
+ double *x = csa->x;
+ double *y = csa->y;
+ double *z = csa->z;
+ double *dx_aff = csa->dx_aff;
+ double *dy_aff = csa->dy_aff;
+ double *dz_aff = csa->dz_aff;
+ double *dx_cc = csa->dx_cc;
+ double *dy_cc = csa->dy_cc;
+ double *dz_cc = csa->dz_cc;
+ double *dx = csa->dx;
+ double *dy = csa->dy;
+ double *dz = csa->dz;
+ int i, j, ret = 0;
+ double temp, gamma_p, gamma_d, *p, *q, *r;
+ /* allocate working arrays */
+ p = xcalloc(1+m, sizeof(double));
+ q = xcalloc(1+n, sizeof(double));
+ r = xcalloc(1+n, sizeof(double));
+ /* p = b - A*x */
+ A_by_vec(csa, x, p);
+ for (i = 1; i <= m; i++) p[i] = b[i] - p[i];
+ /* q = c - A'*y - z */
+ AT_by_vec(csa, y,q);
+ for (j = 1; j <= n; j++) q[j] = c[j] - q[j] - z[j];
+ /* r = - X * Z * e */
+ for (j = 1; j <= n; j++) r[j] = - x[j] * z[j];
+ /* solve the first Newtonian system */
+ if (solve_NS(csa, p, q, r, dx_aff, dy_aff, dz_aff))
+ { ret = 1;
+ goto done;
+ }
+ /* alfa_aff_p = inf{0 <= alfa <= 1 | x + alfa*dx_aff >= 0} */
+ /* alfa_aff_d = inf{0 <= alfa <= 1 | z + alfa*dz_aff >= 0} */
+ csa->alfa_aff_p = csa->alfa_aff_d = 1.0;
+ for (j = 1; j <= n; j++)
+ { if (dx_aff[j] < 0.0)
+ { temp = - x[j] / dx_aff[j];
+ if (csa->alfa_aff_p > temp) csa->alfa_aff_p = temp;
+ }
+ if (dz_aff[j] < 0.0)
+ { temp = - z[j] / dz_aff[j];
+ if (csa->alfa_aff_d > temp) csa->alfa_aff_d = temp;
+ }
+ }
+ /* mu_aff = (x+alfa_aff_p*dx_aff)' * (z+alfa_aff_d*dz_aff) / n */
+ temp = 0.0;
+ for (j = 1; j <= n; j++)
+ temp += (x[j] + csa->alfa_aff_p * dx_aff[j]) *
+ (z[j] + csa->alfa_aff_d * dz_aff[j]);
+ csa->mu_aff = temp / (double)n;
+ /* sigma = (mu_aff/mu)^3 */
+ temp = csa->mu_aff / csa->mu;
+ csa->sigma = temp * temp * temp;
+ /* p = 0 */
+ for (i = 1; i <= m; i++) p[i] = 0.0;
+ /* q = 0 */
+ for (j = 1; j <= n; j++) q[j] = 0.0;
+ /* r = sigma * mu * e - X * Z * e */
+ for (j = 1; j <= n; j++)
+ r[j] = csa->sigma * csa->mu - dx_aff[j] * dz_aff[j];
+ /* solve the second Newtonian system with the same coefficients
+ but with altered right-hand sides */
+ if (solve_NS(csa, p, q, r, dx_cc, dy_cc, dz_cc))
+ { ret = 1;
+ goto done;
+ }
+ /* (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc) */
+ for (j = 1; j <= n; j++) dx[j] = dx_aff[j] + dx_cc[j];
+ for (i = 1; i <= m; i++) dy[i] = dy_aff[i] + dy_cc[i];
+ for (j = 1; j <= n; j++) dz[j] = dz_aff[j] + dz_cc[j];
+ /* alfa_max_p = inf{0 <= alfa <= 1 | x + alfa*dx >= 0} */
+ /* alfa_max_d = inf{0 <= alfa <= 1 | z + alfa*dz >= 0} */
+ csa->alfa_max_p = csa->alfa_max_d = 1.0;
+ for (j = 1; j <= n; j++)
+ { if (dx[j] < 0.0)
+ { temp = - x[j] / dx[j];
+ if (csa->alfa_max_p > temp) csa->alfa_max_p = temp;
+ }
+ if (dz[j] < 0.0)
+ { temp = - z[j] / dz[j];
+ if (csa->alfa_max_d > temp) csa->alfa_max_d = temp;
+ }
+ }
+ /* determine scale factors (not implemented yet) */
+ gamma_p = 0.90;
+ gamma_d = 0.90;
+ /* compute the next point */
+ for (j = 1; j <= n; j++)
+ { x[j] += gamma_p * csa->alfa_max_p * dx[j];
+ xassert(x[j] > 0.0);
+ }
+ for (i = 1; i <= m; i++)
+ y[i] += gamma_d * csa->alfa_max_d * dy[i];
+ for (j = 1; j <= n; j++)
+ { z[j] += gamma_d * csa->alfa_max_d * dz[j];
+ xassert(z[j] > 0.0);
+ }
+done: /* free working arrays */
+ xfree(p);
+ xfree(q);
+ xfree(r);
+ return ret;
+}
+
+/***********************************************************************
+* terminate - deallocate common storage area
+*
+* This routine frees all memory allocated to the common storage area
+* used by interior-point method routines. */
+
+static void terminate(struct csa *csa)
+{ xfree(csa->D);
+ xfree(csa->P);
+ xfree(csa->S_ptr);
+ xfree(csa->S_ind);
+ xfree(csa->S_val);
+ xfree(csa->S_diag);
+ xfree(csa->U_ptr);
+ xfree(csa->U_ind);
+ xfree(csa->U_val);
+ xfree(csa->U_diag);
+ xfree(csa->phi_min);
+ xfree(csa->best_x);
+ xfree(csa->best_y);
+ xfree(csa->best_z);
+ xfree(csa->dx_aff);
+ xfree(csa->dy_aff);
+ xfree(csa->dz_aff);
+ xfree(csa->dx_cc);
+ xfree(csa->dy_cc);
+ xfree(csa->dz_cc);
+ return;
+}
+
+/***********************************************************************
+* ipm_main - main interior-point method routine
+*
+* This is a main routine of the primal-dual interior-point method.
+*
+* The routine ipm_main returns one of the following codes:
+*
+* 0 - optimal solution found;
+* 1 - problem has no feasible (primal or dual) solution;
+* 2 - no convergence;
+* 3 - iteration limit exceeded;
+* 4 - numeric instability on solving Newtonian system.
+*
+* In case of non-zero return code the routine returns the best point,
+* which has been reached during optimization. */
+
+static int ipm_main(struct csa *csa)
+{ int m = csa->m;
+ int n = csa->n;
+ int i, j, status;
+ double temp;
+ /* choose initial point using Mehrotra's heuristic */
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Guessing initial point...\n");
+ initial_point(csa);
+ /* main loop starts here */
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Optimization begins...\n");
+ for (;;)
+ { /* perform basic computations at the current point */
+ basic_info(csa);
+ /* save initial value of rmu */
+ if (csa->iter == 0) csa->rmu0 = csa->rmu;
+ /* accumulate values of min(phi[k]) and save the best point */
+ xassert(csa->iter <= ITER_MAX);
+ if (csa->iter == 0 || csa->phi_min[csa->iter-1] > csa->phi)
+ { csa->phi_min[csa->iter] = csa->phi;
+ csa->best_iter = csa->iter;
+ for (j = 1; j <= n; j++) csa->best_x[j] = csa->x[j];
+ for (i = 1; i <= m; i++) csa->best_y[i] = csa->y[i];
+ for (j = 1; j <= n; j++) csa->best_z[j] = csa->z[j];
+ csa->best_obj = csa->obj;
+ }
+ else
+ csa->phi_min[csa->iter] = csa->phi_min[csa->iter-1];
+ /* display information at the current point */
+ if (csa->parm->msg_lev >= GLP_MSG_ON)
+ xprintf("%3d: obj = %17.9e; rpi = %8.1e; rdi = %8.1e; gap ="
+ " %8.1e\n", csa->iter, csa->obj, csa->rpi, csa->rdi,
+ csa->gap);
+ /* check if the current point is optimal */
+ if (csa->rpi < 1e-8 && csa->rdi < 1e-8 && csa->gap < 1e-8)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("OPTIMAL SOLUTION FOUND\n");
+ status = 0;
+ break;
+ }
+ /* check if the problem has no feasible solution */
+ temp = 1e5 * csa->phi_min[csa->iter];
+ if (temp < 1e-8) temp = 1e-8;
+ if (csa->phi >= temp)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("PROBLEM HAS NO FEASIBLE PRIMAL/DUAL SOLUTION\n")
+ ;
+ status = 1;
+ break;
+ }
+ /* check for very slow convergence or divergence */
+ if (((csa->rpi >= 1e-8 || csa->rdi >= 1e-8) && csa->rmu /
+ csa->rmu0 >= 1e6) ||
+ (csa->iter >= 30 && csa->phi_min[csa->iter] >= 0.5 *
+ csa->phi_min[csa->iter - 30]))
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("NO CONVERGENCE; SEARCH TERMINATED\n");
+ status = 2;
+ break;
+ }
+ /* check for maximal number of iterations */
+ if (csa->iter == ITER_MAX)
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ status = 3;
+ break;
+ }
+ /* start the next iteration */
+ csa->iter++;
+ /* factorize normal equation system */
+ for (j = 1; j <= n; j++) csa->D[j] = csa->x[j] / csa->z[j];
+ decomp_NE(csa);
+ /* compute the next point using Mehrotra's predictor-corrector
+ technique */
+ if (make_step(csa))
+ { if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("NUMERIC INSTABILITY; SEARCH TERMINATED\n");
+ status = 4;
+ break;
+ }
+ }
+ /* restore the best point */
+ if (status != 0)
+ { for (j = 1; j <= n; j++) csa->x[j] = csa->best_x[j];
+ for (i = 1; i <= m; i++) csa->y[i] = csa->best_y[i];
+ for (j = 1; j <= n; j++) csa->z[j] = csa->best_z[j];
+ if (csa->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Best point %17.9e was reached on iteration %d\n",
+ csa->best_obj, csa->best_iter);
+ }
+ /* return to the calling program */
+ return status;
+}
+
+/***********************************************************************
+* NAME
+*
+* ipm_solve - core LP solver based on the interior-point method
+*
+* SYNOPSIS
+*
+* #include "glpipm.h"
+* int ipm_solve(glp_prob *P, const glp_iptcp *parm);
+*
+* DESCRIPTION
+*
+* The routine ipm_solve is a core LP solver based on the primal-dual
+* interior-point method.
+*
+* The routine assumes the following standard formulation of LP problem
+* to be solved:
+*
+* minimize
+*
+* F = c[0] + c[1]*x[1] + c[2]*x[2] + ... + c[n]*x[n]
+*
+* subject to linear constraints
+*
+* a[1,1]*x[1] + a[1,2]*x[2] + ... + a[1,n]*x[n] = b[1]
+*
+* a[2,1]*x[1] + a[2,2]*x[2] + ... + a[2,n]*x[n] = b[2]
+*
+* . . . . . .
+*
+* a[m,1]*x[1] + a[m,2]*x[2] + ... + a[m,n]*x[n] = b[m]
+*
+* and non-negative variables
+*
+* x[1] >= 0, x[2] >= 0, ..., x[n] >= 0
+*
+* where:
+* F is the objective function;
+* x[1], ..., x[n] are (structural) variables;
+* c[0] is a constant term of the objective function;
+* c[1], ..., c[n] are objective coefficients;
+* a[1,1], ..., a[m,n] are constraint coefficients;
+* b[1], ..., b[n] are right-hand sides.
+*
+* The solution is three vectors x, y, and z, which are stored by the
+* routine in the arrays x, y, and z, respectively. These vectors
+* correspond to the best primal-dual point found during optimization.
+* They are approximate solution of the following system (which is the
+* Karush-Kuhn-Tucker optimality conditions):
+*
+* A*x = b (primal feasibility condition)
+*
+* A'*y + z = c (dual feasibility condition)
+*
+* x'*z = 0 (primal-dual complementarity condition)
+*
+* x >= 0, z >= 0 (non-negativity condition)
+*
+* where:
+* x[1], ..., x[n] are primal (structural) variables;
+* y[1], ..., y[m] are dual variables (Lagrange multipliers) for
+* equality constraints;
+* z[1], ..., z[n] are dual variables (Lagrange multipliers) for
+* non-negativity constraints.
+*
+* RETURNS
+*
+* 0 LP has been successfully solved.
+*
+* GLP_ENOCVG
+* No convergence.
+*
+* GLP_EITLIM
+* Iteration limit exceeded.
+*
+* GLP_EINSTAB
+* Numeric instability on solving Newtonian system.
+*
+* In case of non-zero return code the routine returns the best point,
+* which has been reached during optimization. */
+
+int ipm_solve(glp_prob *P, const glp_iptcp *parm)
+{ struct csa _dsa, *csa = &_dsa;
+ int m = P->m;
+ int n = P->n;
+ int nnz = P->nnz;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, j, loc, ret, *A_ind, *A_ptr;
+ double dir, *A_val, *b, *c, *x, *y, *z;
+ xassert(m > 0);
+ xassert(n > 0);
+ /* allocate working arrays */
+ A_ptr = xcalloc(1+m+1, sizeof(int));
+ A_ind = xcalloc(1+nnz, sizeof(int));
+ A_val = xcalloc(1+nnz, sizeof(double));
+ b = xcalloc(1+m, sizeof(double));
+ c = xcalloc(1+n, sizeof(double));
+ x = xcalloc(1+n, sizeof(double));
+ y = xcalloc(1+m, sizeof(double));
+ z = xcalloc(1+n, sizeof(double));
+ /* prepare rows and constraint coefficients */
+ loc = 1;
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ xassert(row->type == GLP_FX);
+ b[i] = row->lb * row->rii;
+ A_ptr[i] = loc;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { A_ind[loc] = aij->col->j;
+ A_val[loc] = row->rii * aij->val * aij->col->sjj;
+ loc++;
+ }
+ }
+ A_ptr[m+1] = loc;
+ xassert(loc-1 == nnz);
+ /* prepare columns and objective coefficients */
+ if (P->dir == GLP_MIN)
+ dir = +1.0;
+ else if (P->dir == GLP_MAX)
+ dir = -1.0;
+ else
+ xassert(P != P);
+ c[0] = dir * P->c0;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ xassert(col->type == GLP_LO && col->lb == 0.0);
+ c[j] = dir * col->coef * col->sjj;
+ }
+ /* allocate and initialize the common storage area */
+ csa->m = m;
+ csa->n = n;
+ csa->A_ptr = A_ptr;
+ csa->A_ind = A_ind;
+ csa->A_val = A_val;
+ csa->b = b;
+ csa->c = c;
+ csa->x = x;
+ csa->y = y;
+ csa->z = z;
+ csa->parm = parm;
+ initialize(csa);
+ /* solve LP with the interior-point method */
+ ret = ipm_main(csa);
+ /* deallocate the common storage area */
+ terminate(csa);
+ /* determine solution status */
+ if (ret == 0)
+ { /* optimal solution found */
+ P->ipt_stat = GLP_OPT;
+ ret = 0;
+ }
+ else if (ret == 1)
+ { /* problem has no feasible (primal or dual) solution */
+ P->ipt_stat = GLP_NOFEAS;
+ ret = 0;
+ }
+ else if (ret == 2)
+ { /* no convergence */
+ P->ipt_stat = GLP_INFEAS;
+ ret = GLP_ENOCVG;
+ }
+ else if (ret == 3)
+ { /* iteration limit exceeded */
+ P->ipt_stat = GLP_INFEAS;
+ ret = GLP_EITLIM;
+ }
+ else if (ret == 4)
+ { /* numeric instability on solving Newtonian system */
+ P->ipt_stat = GLP_INFEAS;
+ ret = GLP_EINSTAB;
+ }
+ else
+ xassert(ret != ret);
+ /* store row solution components */
+ for (i = 1; i <= m; i++)
+ { row = P->row[i];
+ row->pval = row->lb;
+ row->dval = dir * y[i] * row->rii;
+ }
+ /* store column solution components */
+ P->ipt_obj = P->c0;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ col->pval = x[j] * col->sjj;
+ col->dval = dir * z[j] / col->sjj;
+ P->ipt_obj += col->coef * col->pval;
+ }
+ /* free working arrays */
+ xfree(A_ptr);
+ xfree(A_ind);
+ xfree(A_val);
+ xfree(b);
+ xfree(c);
+ xfree(x);
+ xfree(y);
+ xfree(z);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpipm.h b/test/monniaux/glpk-4.65/src/draft/glpipm.h
new file mode 100644
index 00000000..a5f94fec
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpipm.h
@@ -0,0 +1,36 @@
+/* glpipm.h (primal-dual interior-point method) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPIPM_H
+#define GLPIPM_H
+
+#include "prob.h"
+
+#define ipm_solve _glp_ipm_solve
+int ipm_solve(glp_prob *P, const glp_iptcp *parm);
+/* core LP solver based on the interior-point method */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpmat.c b/test/monniaux/glpk-4.65/src/draft/glpmat.c
new file mode 100644
index 00000000..97d1c651
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpmat.c
@@ -0,0 +1,924 @@
+/* glpmat.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpmat.h"
+#include "qmd.h"
+#include "amd.h"
+#include "colamd.h"
+
+/*----------------------------------------------------------------------
+-- check_fvs - check sparse vector in full-vector storage format.
+--
+-- SYNOPSIS
+--
+-- #include "glpmat.h"
+-- int check_fvs(int n, int nnz, int ind[], double vec[]);
+--
+-- DESCRIPTION
+--
+-- The routine check_fvs checks if a given vector of dimension n in
+-- full-vector storage format has correct representation.
+--
+-- RETURNS
+--
+-- The routine returns one of the following codes:
+--
+-- 0 - the vector is correct;
+-- 1 - the number of elements (n) is negative;
+-- 2 - the number of non-zero elements (nnz) is negative;
+-- 3 - some element index is out of range;
+-- 4 - some element index is duplicate;
+-- 5 - some non-zero element is out of pattern. */
+
+int check_fvs(int n, int nnz, int ind[], double vec[])
+{ int i, t, ret, *flag = NULL;
+ /* check the number of elements */
+ if (n < 0)
+ { ret = 1;
+ goto done;
+ }
+ /* check the number of non-zero elements */
+ if (nnz < 0)
+ { ret = 2;
+ goto done;
+ }
+ /* check vector indices */
+ flag = xcalloc(1+n, sizeof(int));
+ for (i = 1; i <= n; i++) flag[i] = 0;
+ for (t = 1; t <= nnz; t++)
+ { i = ind[t];
+ if (!(1 <= i && i <= n))
+ { ret = 3;
+ goto done;
+ }
+ if (flag[i])
+ { ret = 4;
+ goto done;
+ }
+ flag[i] = 1;
+ }
+ /* check vector elements */
+ for (i = 1; i <= n; i++)
+ { if (!flag[i] && vec[i] != 0.0)
+ { ret = 5;
+ goto done;
+ }
+ }
+ /* the vector is ok */
+ ret = 0;
+done: if (flag != NULL) xfree(flag);
+ return ret;
+}
+
+/*----------------------------------------------------------------------
+-- check_pattern - check pattern of sparse matrix.
+--
+-- SYNOPSIS
+--
+-- #include "glpmat.h"
+-- int check_pattern(int m, int n, int A_ptr[], int A_ind[]);
+--
+-- DESCRIPTION
+--
+-- The routine check_pattern checks the pattern of a given mxn matrix
+-- in storage-by-rows format.
+--
+-- RETURNS
+--
+-- The routine returns one of the following codes:
+--
+-- 0 - the pattern is correct;
+-- 1 - the number of rows (m) is negative;
+-- 2 - the number of columns (n) is negative;
+-- 3 - A_ptr[1] is not 1;
+-- 4 - some column index is out of range;
+-- 5 - some column indices are duplicate. */
+
+int check_pattern(int m, int n, int A_ptr[], int A_ind[])
+{ int i, j, ptr, ret, *flag = NULL;
+ /* check the number of rows */
+ if (m < 0)
+ { ret = 1;
+ goto done;
+ }
+ /* check the number of columns */
+ if (n < 0)
+ { ret = 2;
+ goto done;
+ }
+ /* check location A_ptr[1] */
+ if (A_ptr[1] != 1)
+ { ret = 3;
+ goto done;
+ }
+ /* check row patterns */
+ flag = xcalloc(1+n, sizeof(int));
+ for (j = 1; j <= n; j++) flag[j] = 0;
+ for (i = 1; i <= m; i++)
+ { /* check pattern of row i */
+ for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
+ { j = A_ind[ptr];
+ /* check column index */
+ if (!(1 <= j && j <= n))
+ { ret = 4;
+ goto done;
+ }
+ /* check for duplication */
+ if (flag[j])
+ { ret = 5;
+ goto done;
+ }
+ flag[j] = 1;
+ }
+ /* clear flags */
+ for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
+ { j = A_ind[ptr];
+ flag[j] = 0;
+ }
+ }
+ /* the pattern is ok */
+ ret = 0;
+done: if (flag != NULL) xfree(flag);
+ return ret;
+}
+
+/*----------------------------------------------------------------------
+-- transpose - transpose sparse matrix.
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- void transpose(int m, int n, int A_ptr[], int A_ind[],
+-- double A_val[], int AT_ptr[], int AT_ind[], double AT_val[]);
+--
+-- *Description*
+--
+-- For a given mxn sparse matrix A the routine transpose builds a nxm
+-- sparse matrix A' which is a matrix transposed to A.
+--
+-- The arrays A_ptr, A_ind, and A_val specify a given mxn matrix A to
+-- be transposed in storage-by-rows format. The parameter A_val can be
+-- NULL, in which case numeric values are not copied. The arrays A_ptr,
+-- A_ind, and A_val are not changed on exit.
+--
+-- On entry the arrays AT_ptr, AT_ind, and AT_val must be allocated,
+-- but their content is ignored. On exit the routine stores a resultant
+-- nxm matrix A' in these arrays in storage-by-rows format. Note that
+-- if the parameter A_val is NULL, the array AT_val is not used.
+--
+-- The routine transpose has a side effect that elements in rows of the
+-- resultant matrix A' follow in ascending their column indices. */
+
+void transpose(int m, int n, int A_ptr[], int A_ind[], double A_val[],
+ int AT_ptr[], int AT_ind[], double AT_val[])
+{ int i, j, t, beg, end, pos, len;
+ /* determine row lengths of resultant matrix */
+ for (j = 1; j <= n; j++) AT_ptr[j] = 0;
+ for (i = 1; i <= m; i++)
+ { beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++) AT_ptr[A_ind[t]]++;
+ }
+ /* set up row pointers of resultant matrix */
+ pos = 1;
+ for (j = 1; j <= n; j++)
+ len = AT_ptr[j], pos += len, AT_ptr[j] = pos;
+ AT_ptr[n+1] = pos;
+ /* build resultant matrix */
+ for (i = m; i >= 1; i--)
+ { beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++)
+ { pos = --AT_ptr[A_ind[t]];
+ AT_ind[pos] = i;
+ if (A_val != NULL) AT_val[pos] = A_val[t];
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- adat_symbolic - compute S = P*A*D*A'*P' (symbolic phase).
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- int *adat_symbolic(int m, int n, int P_per[], int A_ptr[],
+-- int A_ind[], int S_ptr[]);
+--
+-- *Description*
+--
+-- The routine adat_symbolic implements the symbolic phase to compute
+-- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
+-- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
+-- transposed to A, P' is an inverse of P.
+--
+-- The parameter m is the number of rows in A and the order of P.
+--
+-- The parameter n is the number of columns in A and the order of D.
+--
+-- The array P_per specifies permutation matrix P. It is not changed on
+-- exit.
+--
+-- The arrays A_ptr and A_ind specify the pattern of matrix A. They are
+-- not changed on exit.
+--
+-- On exit the routine stores the pattern of upper triangular part of
+-- matrix S without diagonal elements in the arrays S_ptr and S_ind in
+-- storage-by-rows format. The array S_ptr should be allocated on entry,
+-- however, its content is ignored. The array S_ind is allocated by the
+-- routine itself which returns a pointer to it.
+--
+-- *Returns*
+--
+-- The routine returns a pointer to the array S_ind. */
+
+int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], int A_ind[],
+ int S_ptr[])
+{ int i, j, t, ii, jj, tt, k, size, len;
+ int *S_ind, *AT_ptr, *AT_ind, *ind, *map, *temp;
+ /* build the pattern of A', which is a matrix transposed to A, to
+ efficiently access A in column-wise manner */
+ AT_ptr = xcalloc(1+n+1, sizeof(int));
+ AT_ind = xcalloc(A_ptr[m+1], sizeof(int));
+ transpose(m, n, A_ptr, A_ind, NULL, AT_ptr, AT_ind, NULL);
+ /* allocate the array S_ind */
+ size = A_ptr[m+1] - 1;
+ if (size < m) size = m;
+ S_ind = xcalloc(1+size, sizeof(int));
+ /* allocate and initialize working arrays */
+ ind = xcalloc(1+m, sizeof(int));
+ map = xcalloc(1+m, sizeof(int));
+ for (jj = 1; jj <= m; jj++) map[jj] = 0;
+ /* compute pattern of S; note that symbolically S = B*B', where
+ B = P*A, B' is matrix transposed to B */
+ S_ptr[1] = 1;
+ for (ii = 1; ii <= m; ii++)
+ { /* compute pattern of ii-th row of S */
+ len = 0;
+ i = P_per[ii]; /* i-th row of A = ii-th row of B */
+ for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
+ { k = A_ind[t];
+ /* walk through k-th column of A */
+ for (tt = AT_ptr[k]; tt < AT_ptr[k+1]; tt++)
+ { j = AT_ind[tt];
+ jj = P_per[m+j]; /* j-th row of A = jj-th row of B */
+ /* a[i,k] != 0 and a[j,k] != 0 ergo s[ii,jj] != 0 */
+ if (ii < jj && !map[jj]) ind[++len] = jj, map[jj] = 1;
+ }
+ }
+ /* now (ind) is pattern of ii-th row of S */
+ S_ptr[ii+1] = S_ptr[ii] + len;
+ /* at least (S_ptr[ii+1] - 1) locations should be available in
+ the array S_ind */
+ if (S_ptr[ii+1] - 1 > size)
+ { temp = S_ind;
+ size += size;
+ S_ind = xcalloc(1+size, sizeof(int));
+ memcpy(&S_ind[1], &temp[1], (S_ptr[ii] - 1) * sizeof(int));
+ xfree(temp);
+ }
+ xassert(S_ptr[ii+1] - 1 <= size);
+ /* (ii-th row of S) := (ind) */
+ memcpy(&S_ind[S_ptr[ii]], &ind[1], len * sizeof(int));
+ /* clear the row pattern map */
+ for (t = 1; t <= len; t++) map[ind[t]] = 0;
+ }
+ /* free working arrays */
+ xfree(AT_ptr);
+ xfree(AT_ind);
+ xfree(ind);
+ xfree(map);
+ /* reallocate the array S_ind to free unused locations */
+ temp = S_ind;
+ size = S_ptr[m+1] - 1;
+ S_ind = xcalloc(1+size, sizeof(int));
+ memcpy(&S_ind[1], &temp[1], size * sizeof(int));
+ xfree(temp);
+ return S_ind;
+}
+
+/*----------------------------------------------------------------------
+-- adat_numeric - compute S = P*A*D*A'*P' (numeric phase).
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- void adat_numeric(int m, int n, int P_per[],
+-- int A_ptr[], int A_ind[], double A_val[], double D_diag[],
+-- int S_ptr[], int S_ind[], double S_val[], double S_diag[]);
+--
+-- *Description*
+--
+-- The routine adat_numeric implements the numeric phase to compute
+-- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
+-- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
+-- transposed to A, P' is an inverse of P.
+--
+-- The parameter m is the number of rows in A and the order of P.
+--
+-- The parameter n is the number of columns in A and the order of D.
+--
+-- The matrix P is specified in the array P_per, which is not changed
+-- on exit.
+--
+-- The matrix A is specified in the arrays A_ptr, A_ind, and A_val in
+-- storage-by-rows format. These arrays are not changed on exit.
+--
+-- Diagonal elements of the matrix D are specified in the array D_diag,
+-- where D_diag[0] is not used, D_diag[i] = d[i,i] for i = 1, ..., n.
+-- The array D_diag is not changed on exit.
+--
+-- The pattern of the upper triangular part of the matrix S without
+-- diagonal elements (previously computed by the routine adat_symbolic)
+-- is specified in the arrays S_ptr and S_ind, which are not changed on
+-- exit. Numeric values of non-diagonal elements of S are stored in
+-- corresponding locations of the array S_val, and values of diagonal
+-- elements of S are stored in locations S_diag[1], ..., S_diag[n]. */
+
+void adat_numeric(int m, int n, int P_per[],
+ int A_ptr[], int A_ind[], double A_val[], double D_diag[],
+ int S_ptr[], int S_ind[], double S_val[], double S_diag[])
+{ int i, j, t, ii, jj, tt, beg, end, beg1, end1, k;
+ double sum, *work;
+ work = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++) work[j] = 0.0;
+ /* compute S = B*D*B', where B = P*A, B' is a matrix transposed
+ to B */
+ for (ii = 1; ii <= m; ii++)
+ { i = P_per[ii]; /* i-th row of A = ii-th row of B */
+ /* (work) := (i-th row of A) */
+ beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++)
+ work[A_ind[t]] = A_val[t];
+ /* compute ii-th row of S */
+ beg = S_ptr[ii], end = S_ptr[ii+1];
+ for (t = beg; t < end; t++)
+ { jj = S_ind[t];
+ j = P_per[jj]; /* j-th row of A = jj-th row of B */
+ /* s[ii,jj] := sum a[i,k] * d[k,k] * a[j,k] */
+ sum = 0.0;
+ beg1 = A_ptr[j], end1 = A_ptr[j+1];
+ for (tt = beg1; tt < end1; tt++)
+ { k = A_ind[tt];
+ sum += work[k] * D_diag[k] * A_val[tt];
+ }
+ S_val[t] = sum;
+ }
+ /* s[ii,ii] := sum a[i,k] * d[k,k] * a[i,k] */
+ sum = 0.0;
+ beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++)
+ { k = A_ind[t];
+ sum += A_val[t] * D_diag[k] * A_val[t];
+ work[k] = 0.0;
+ }
+ S_diag[ii] = sum;
+ }
+ xfree(work);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- min_degree - minimum degree ordering.
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]);
+--
+-- *Description*
+--
+-- The routine min_degree uses the minimum degree ordering algorithm
+-- to find a permutation matrix P for a given sparse symmetric positive
+-- matrix A which minimizes the number of non-zeros in upper triangular
+-- factor U for Cholesky factorization P*A*P' = U'*U.
+--
+-- The parameter n is the order of matrices A and P.
+--
+-- The pattern of the given matrix A is specified on entry in the arrays
+-- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
+-- part without diagonal elements (which all are assumed to be non-zero)
+-- should be specified as if A were upper triangular. The arrays A_ptr
+-- and A_ind are not changed on exit.
+--
+-- The permutation matrix P is stored by the routine in the array P_per
+-- on exit.
+--
+-- *Algorithm*
+--
+-- The routine min_degree is based on some subroutines from the package
+-- SPARSPAK (see comments in the module glpqmd). */
+
+void min_degree(int n, int A_ptr[], int A_ind[], int P_per[])
+{ int i, j, ne, t, pos, len;
+ int *xadj, *adjncy, *deg, *marker, *rchset, *nbrhd, *qsize,
+ *qlink, nofsub;
+ /* determine number of non-zeros in complete pattern */
+ ne = A_ptr[n+1] - 1;
+ ne += ne;
+ /* allocate working arrays */
+ xadj = xcalloc(1+n+1, sizeof(int));
+ adjncy = xcalloc(1+ne, sizeof(int));
+ deg = xcalloc(1+n, sizeof(int));
+ marker = xcalloc(1+n, sizeof(int));
+ rchset = xcalloc(1+n, sizeof(int));
+ nbrhd = xcalloc(1+n, sizeof(int));
+ qsize = xcalloc(1+n, sizeof(int));
+ qlink = xcalloc(1+n, sizeof(int));
+ /* determine row lengths in complete pattern */
+ for (i = 1; i <= n; i++) xadj[i] = 0;
+ for (i = 1; i <= n; i++)
+ { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
+ { j = A_ind[t];
+ xassert(i < j && j <= n);
+ xadj[i]++, xadj[j]++;
+ }
+ }
+ /* set up row pointers for complete pattern */
+ pos = 1;
+ for (i = 1; i <= n; i++)
+ len = xadj[i], pos += len, xadj[i] = pos;
+ xadj[n+1] = pos;
+ xassert(pos - 1 == ne);
+ /* construct complete pattern */
+ for (i = 1; i <= n; i++)
+ { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
+ { j = A_ind[t];
+ adjncy[--xadj[i]] = j, adjncy[--xadj[j]] = i;
+ }
+ }
+ /* call the main minimimum degree ordering routine */
+ genqmd(&n, xadj, adjncy, P_per, P_per + n, deg, marker, rchset,
+ nbrhd, qsize, qlink, &nofsub);
+ /* make sure that permutation matrix P is correct */
+ for (i = 1; i <= n; i++)
+ { j = P_per[i];
+ xassert(1 <= j && j <= n);
+ xassert(P_per[n+j] == i);
+ }
+ /* free working arrays */
+ xfree(xadj);
+ xfree(adjncy);
+ xfree(deg);
+ xfree(marker);
+ xfree(rchset);
+ xfree(nbrhd);
+ xfree(qsize);
+ xfree(qlink);
+ return;
+}
+
+/**********************************************************************/
+
+void amd_order1(int n, int A_ptr[], int A_ind[], int P_per[])
+{ /* approximate minimum degree ordering (AMD) */
+ int k, ret;
+ double Control[AMD_CONTROL], Info[AMD_INFO];
+ /* get the default parameters */
+ amd_defaults(Control);
+#if 0
+ /* and print them */
+ amd_control(Control);
+#endif
+ /* make all indices 0-based */
+ for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
+ for (k = 1; k <= n+1; k++) A_ptr[k]--;
+ /* call the ordering routine */
+ ret = amd_order(n, &A_ptr[1], &A_ind[1], &P_per[1], Control, Info)
+ ;
+#if 0
+ amd_info(Info);
+#endif
+ xassert(ret == AMD_OK || ret == AMD_OK_BUT_JUMBLED);
+ /* retsore 1-based indices */
+ for (k = 1; k <= n+1; k++) A_ptr[k]++;
+ for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
+ /* patch up permutation matrix */
+ memset(&P_per[n+1], 0, n * sizeof(int));
+ for (k = 1; k <= n; k++)
+ { P_per[k]++;
+ xassert(1 <= P_per[k] && P_per[k] <= n);
+ xassert(P_per[n+P_per[k]] == 0);
+ P_per[n+P_per[k]] = k;
+ }
+ return;
+}
+
+/**********************************************************************/
+
+static void *allocate(size_t n, size_t size)
+{ void *ptr;
+ ptr = xcalloc(n, size);
+ memset(ptr, 0, n * size);
+ return ptr;
+}
+
+static void release(void *ptr)
+{ xfree(ptr);
+ return;
+}
+
+void symamd_ord(int n, int A_ptr[], int A_ind[], int P_per[])
+{ /* approximate minimum degree ordering (SYMAMD) */
+ int k, ok;
+ int stats[COLAMD_STATS];
+ /* make all indices 0-based */
+ for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
+ for (k = 1; k <= n+1; k++) A_ptr[k]--;
+ /* call the ordering routine */
+ ok = symamd(n, &A_ind[1], &A_ptr[1], &P_per[1], NULL, stats,
+ allocate, release);
+#if 0
+ symamd_report(stats);
+#endif
+ xassert(ok);
+ /* restore 1-based indices */
+ for (k = 1; k <= n+1; k++) A_ptr[k]++;
+ for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
+ /* patch up permutation matrix */
+ memset(&P_per[n+1], 0, n * sizeof(int));
+ for (k = 1; k <= n; k++)
+ { P_per[k]++;
+ xassert(1 <= P_per[k] && P_per[k] <= n);
+ xassert(P_per[n+P_per[k]] == 0);
+ P_per[n+P_per[k]] = k;
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- chol_symbolic - compute Cholesky factorization (symbolic phase).
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]);
+--
+-- *Description*
+--
+-- The routine chol_symbolic implements the symbolic phase of Cholesky
+-- factorization A = U'*U, where A is a given sparse symmetric positive
+-- definite matrix, U is a resultant upper triangular factor, U' is a
+-- matrix transposed to U.
+--
+-- The parameter n is the order of matrices A and U.
+--
+-- The pattern of the given matrix A is specified on entry in the arrays
+-- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
+-- part without diagonal elements (which all are assumed to be non-zero)
+-- should be specified as if A were upper triangular. The arrays A_ptr
+-- and A_ind are not changed on exit.
+--
+-- The pattern of the matrix U without diagonal elements (which all are
+-- assumed to be non-zero) is stored on exit from the routine in the
+-- arrays U_ptr and U_ind in storage-by-rows format. The array U_ptr
+-- should be allocated on entry, however, its content is ignored. The
+-- array U_ind is allocated by the routine which returns a pointer to it
+-- on exit.
+--
+-- *Returns*
+--
+-- The routine returns a pointer to the array U_ind.
+--
+-- *Method*
+--
+-- The routine chol_symbolic computes the pattern of the matrix U in a
+-- row-wise manner. No pivoting is used.
+--
+-- It is known that to compute the pattern of row k of the matrix U we
+-- need to merge the pattern of row k of the matrix A and the patterns
+-- of each row i of U, where u[i,k] is non-zero (these rows are already
+-- computed and placed above row k).
+--
+-- However, to reduce the number of rows to be merged the routine uses
+-- an advanced algorithm proposed in:
+--
+-- D.J.Rose, R.E.Tarjan, and G.S.Lueker. Algorithmic aspects of vertex
+-- elimination on graphs. SIAM J. Comput. 5, 1976, 266-83.
+--
+-- The authors of the cited paper show that we have the same result if
+-- we merge row k of the matrix A and such rows of the matrix U (among
+-- rows 1, ..., k-1) whose leftmost non-diagonal non-zero element is
+-- placed in k-th column. This feature signficantly reduces the number
+-- of rows to be merged, especially on the final steps, where rows of
+-- the matrix U become quite dense.
+--
+-- To determine rows, which should be merged on k-th step, for a fixed
+-- time the routine uses linked lists of row numbers of the matrix U.
+-- Location head[k] contains the number of a first row, whose leftmost
+-- non-diagonal non-zero element is placed in column k, and location
+-- next[i] contains the number of a next row with the same property as
+-- row i. */
+
+int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[])
+{ int i, j, k, t, len, size, beg, end, min_j, *U_ind, *head, *next,
+ *ind, *map, *temp;
+ /* initially we assume that on computing the pattern of U fill-in
+ will double the number of non-zeros in A */
+ size = A_ptr[n+1] - 1;
+ if (size < n) size = n;
+ size += size;
+ U_ind = xcalloc(1+size, sizeof(int));
+ /* allocate and initialize working arrays */
+ head = xcalloc(1+n, sizeof(int));
+ for (i = 1; i <= n; i++) head[i] = 0;
+ next = xcalloc(1+n, sizeof(int));
+ ind = xcalloc(1+n, sizeof(int));
+ map = xcalloc(1+n, sizeof(int));
+ for (j = 1; j <= n; j++) map[j] = 0;
+ /* compute the pattern of matrix U */
+ U_ptr[1] = 1;
+ for (k = 1; k <= n; k++)
+ { /* compute the pattern of k-th row of U, which is the union of
+ k-th row of A and those rows of U (among 1, ..., k-1) whose
+ leftmost non-diagonal non-zero is placed in k-th column */
+ /* (ind) := (k-th row of A) */
+ len = A_ptr[k+1] - A_ptr[k];
+ memcpy(&ind[1], &A_ind[A_ptr[k]], len * sizeof(int));
+ for (t = 1; t <= len; t++)
+ { j = ind[t];
+ xassert(k < j && j <= n);
+ map[j] = 1;
+ }
+ /* walk through rows of U whose leftmost non-diagonal non-zero
+ is placed in k-th column */
+ for (i = head[k]; i != 0; i = next[i])
+ { /* (ind) := (ind) union (i-th row of U) */
+ beg = U_ptr[i], end = U_ptr[i+1];
+ for (t = beg; t < end; t++)
+ { j = U_ind[t];
+ if (j > k && !map[j]) ind[++len] = j, map[j] = 1;
+ }
+ }
+ /* now (ind) is the pattern of k-th row of U */
+ U_ptr[k+1] = U_ptr[k] + len;
+ /* at least (U_ptr[k+1] - 1) locations should be available in
+ the array U_ind */
+ if (U_ptr[k+1] - 1 > size)
+ { temp = U_ind;
+ size += size;
+ U_ind = xcalloc(1+size, sizeof(int));
+ memcpy(&U_ind[1], &temp[1], (U_ptr[k] - 1) * sizeof(int));
+ xfree(temp);
+ }
+ xassert(U_ptr[k+1] - 1 <= size);
+ /* (k-th row of U) := (ind) */
+ memcpy(&U_ind[U_ptr[k]], &ind[1], len * sizeof(int));
+ /* determine column index of leftmost non-diagonal non-zero in
+ k-th row of U and clear the row pattern map */
+ min_j = n + 1;
+ for (t = 1; t <= len; t++)
+ { j = ind[t], map[j] = 0;
+ if (min_j > j) min_j = j;
+ }
+ /* include k-th row into corresponding linked list */
+ if (min_j <= n) next[k] = head[min_j], head[min_j] = k;
+ }
+ /* free working arrays */
+ xfree(head);
+ xfree(next);
+ xfree(ind);
+ xfree(map);
+ /* reallocate the array U_ind to free unused locations */
+ temp = U_ind;
+ size = U_ptr[n+1] - 1;
+ U_ind = xcalloc(1+size, sizeof(int));
+ memcpy(&U_ind[1], &temp[1], size * sizeof(int));
+ xfree(temp);
+ return U_ind;
+}
+
+/*----------------------------------------------------------------------
+-- chol_numeric - compute Cholesky factorization (numeric phase).
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- int chol_numeric(int n,
+-- int A_ptr[], int A_ind[], double A_val[], double A_diag[],
+-- int U_ptr[], int U_ind[], double U_val[], double U_diag[]);
+--
+-- *Description*
+--
+-- The routine chol_symbolic implements the numeric phase of Cholesky
+-- factorization A = U'*U, where A is a given sparse symmetric positive
+-- definite matrix, U is a resultant upper triangular factor, U' is a
+-- matrix transposed to U.
+--
+-- The parameter n is the order of matrices A and U.
+--
+-- Upper triangular part of the matrix A without diagonal elements is
+-- specified in the arrays A_ptr, A_ind, and A_val in storage-by-rows
+-- format. Diagonal elements of A are specified in the array A_diag,
+-- where A_diag[0] is not used, A_diag[i] = a[i,i] for i = 1, ..., n.
+-- The arrays A_ptr, A_ind, A_val, and A_diag are not changed on exit.
+--
+-- The pattern of the matrix U without diagonal elements (previously
+-- computed with the routine chol_symbolic) is specified in the arrays
+-- U_ptr and U_ind, which are not changed on exit. Numeric values of
+-- non-diagonal elements of U are stored in corresponding locations of
+-- the array U_val, and values of diagonal elements of U are stored in
+-- locations U_diag[1], ..., U_diag[n].
+--
+-- *Returns*
+--
+-- The routine returns the number of non-positive diagonal elements of
+-- the matrix U which have been replaced by a huge positive number (see
+-- the method description below). Zero return code means the matrix A
+-- has been successfully factorized.
+--
+-- *Method*
+--
+-- The routine chol_numeric computes the matrix U in a row-wise manner
+-- using standard gaussian elimination technique. No pivoting is used.
+--
+-- Initially the routine sets U = A, and before k-th elimination step
+-- the matrix U is the following:
+--
+-- 1 k n
+-- 1 x x x x x x x x x x
+-- . x x x x x x x x x
+-- . . x x x x x x x x
+-- . . . x x x x x x x
+-- k . . . . * * * * * *
+-- . . . . * * * * * *
+-- . . . . * * * * * *
+-- . . . . * * * * * *
+-- . . . . * * * * * *
+-- n . . . . * * * * * *
+--
+-- where 'x' are elements of already computed rows, '*' are elements of
+-- the active submatrix. (Note that the lower triangular part of the
+-- active submatrix being symmetric is not stored and diagonal elements
+-- are stored separately in the array U_diag.)
+--
+-- The matrix A is assumed to be positive definite. However, if it is
+-- close to semi-definite, on some elimination step a pivot u[k,k] may
+-- happen to be non-positive due to round-off errors. In this case the
+-- routine uses a technique proposed in:
+--
+-- S.J.Wright. The Cholesky factorization in interior-point and barrier
+-- methods. Preprint MCS-P600-0596, Mathematics and Computer Science
+-- Division, Argonne National Laboratory, Argonne, Ill., May 1996.
+--
+-- The routine just replaces non-positive u[k,k] by a huge positive
+-- number. This involves non-diagonal elements in k-th row of U to be
+-- close to zero that, in turn, involves k-th component of a solution
+-- vector to be close to zero. Note, however, that this technique works
+-- only if the system A*x = b is consistent. */
+
+int chol_numeric(int n,
+ int A_ptr[], int A_ind[], double A_val[], double A_diag[],
+ int U_ptr[], int U_ind[], double U_val[], double U_diag[])
+{ int i, j, k, t, t1, beg, end, beg1, end1, count = 0;
+ double ukk, uki, *work;
+ work = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++) work[j] = 0.0;
+ /* U := (upper triangle of A) */
+ /* note that the upper traingle of A is a subset of U */
+ for (i = 1; i <= n; i++)
+ { beg = A_ptr[i], end = A_ptr[i+1];
+ for (t = beg; t < end; t++)
+ j = A_ind[t], work[j] = A_val[t];
+ beg = U_ptr[i], end = U_ptr[i+1];
+ for (t = beg; t < end; t++)
+ j = U_ind[t], U_val[t] = work[j], work[j] = 0.0;
+ U_diag[i] = A_diag[i];
+ }
+ /* main elimination loop */
+ for (k = 1; k <= n; k++)
+ { /* transform k-th row of U */
+ ukk = U_diag[k];
+ if (ukk > 0.0)
+ U_diag[k] = ukk = sqrt(ukk);
+ else
+ U_diag[k] = ukk = DBL_MAX, count++;
+ /* (work) := (transformed k-th row) */
+ beg = U_ptr[k], end = U_ptr[k+1];
+ for (t = beg; t < end; t++)
+ work[U_ind[t]] = (U_val[t] /= ukk);
+ /* transform other rows of U */
+ for (t = beg; t < end; t++)
+ { i = U_ind[t];
+ xassert(i > k);
+ /* (i-th row) := (i-th row) - u[k,i] * (k-th row) */
+ uki = work[i];
+ beg1 = U_ptr[i], end1 = U_ptr[i+1];
+ for (t1 = beg1; t1 < end1; t1++)
+ U_val[t1] -= uki * work[U_ind[t1]];
+ U_diag[i] -= uki * uki;
+ }
+ /* (work) := 0 */
+ for (t = beg; t < end; t++)
+ work[U_ind[t]] = 0.0;
+ }
+ xfree(work);
+ return count;
+}
+
+/*----------------------------------------------------------------------
+-- u_solve - solve upper triangular system U*x = b.
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+-- double U_diag[], double x[]);
+--
+-- *Description*
+--
+-- The routine u_solve solves an linear system U*x = b, where U is an
+-- upper triangular matrix.
+--
+-- The parameter n is the order of matrix U.
+--
+-- The matrix U without diagonal elements is specified in the arrays
+-- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
+-- of U are specified in the array U_diag, where U_diag[0] is not used,
+-- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
+-- changed on exit.
+--
+-- The right-hand side vector b is specified on entry in the array x,
+-- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
+-- the routine stores computed components of the vector of unknowns x
+-- in the array x in the same manner. */
+
+void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+ double U_diag[], double x[])
+{ int i, t, beg, end;
+ double temp;
+ for (i = n; i >= 1; i--)
+ { temp = x[i];
+ beg = U_ptr[i], end = U_ptr[i+1];
+ for (t = beg; t < end; t++)
+ temp -= U_val[t] * x[U_ind[t]];
+ xassert(U_diag[i] != 0.0);
+ x[i] = temp / U_diag[i];
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- ut_solve - solve lower triangular system U'*x = b.
+--
+-- *Synopsis*
+--
+-- #include "glpmat.h"
+-- void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+-- double U_diag[], double x[]);
+--
+-- *Description*
+--
+-- The routine ut_solve solves an linear system U'*x = b, where U is a
+-- matrix transposed to an upper triangular matrix.
+--
+-- The parameter n is the order of matrix U.
+--
+-- The matrix U without diagonal elements is specified in the arrays
+-- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
+-- of U are specified in the array U_diag, where U_diag[0] is not used,
+-- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
+-- changed on exit.
+--
+-- The right-hand side vector b is specified on entry in the array x,
+-- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
+-- the routine stores computed components of the vector of unknowns x
+-- in the array x in the same manner. */
+
+void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+ double U_diag[], double x[])
+{ int i, t, beg, end;
+ double temp;
+ for (i = 1; i <= n; i++)
+ { xassert(U_diag[i] != 0.0);
+ temp = (x[i] /= U_diag[i]);
+ if (temp == 0.0) continue;
+ beg = U_ptr[i], end = U_ptr[i+1];
+ for (t = beg; t < end; t++)
+ x[U_ind[t]] -= U_val[t] * temp;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpmat.h b/test/monniaux/glpk-4.65/src/draft/glpmat.h
new file mode 100644
index 00000000..5b058437
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpmat.h
@@ -0,0 +1,198 @@
+/* glpmat.h (linear algebra routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPMAT_H
+#define GLPMAT_H
+
+/***********************************************************************
+* FULL-VECTOR STORAGE
+*
+* For a sparse vector x having n elements, ne of which are non-zero,
+* the full-vector storage format uses two arrays x_ind and x_vec, which
+* are set up as follows:
+*
+* x_ind is an integer array of length [1+ne]. Location x_ind[0] is
+* not used, and locations x_ind[1], ..., x_ind[ne] contain indices of
+* non-zero elements in vector x.
+*
+* x_vec is a floating-point array of length [1+n]. Location x_vec[0]
+* is not used, and locations x_vec[1], ..., x_vec[n] contain numeric
+* values of ALL elements in vector x, including its zero elements.
+*
+* Let, for example, the following sparse vector x be given:
+*
+* (0, 1, 0, 0, 2, 3, 0, 4)
+*
+* Then the arrays are:
+*
+* x_ind = { X; 2, 5, 6, 8 }
+*
+* x_vec = { X; 0, 1, 0, 0, 2, 3, 0, 4 }
+*
+* COMPRESSED-VECTOR STORAGE
+*
+* For a sparse vector x having n elements, ne of which are non-zero,
+* the compressed-vector storage format uses two arrays x_ind and x_vec,
+* which are set up as follows:
+*
+* x_ind is an integer array of length [1+ne]. Location x_ind[0] is
+* not used, and locations x_ind[1], ..., x_ind[ne] contain indices of
+* non-zero elements in vector x.
+*
+* x_vec is a floating-point array of length [1+ne]. Location x_vec[0]
+* is not used, and locations x_vec[1], ..., x_vec[ne] contain numeric
+* values of corresponding non-zero elements in vector x.
+*
+* Let, for example, the following sparse vector x be given:
+*
+* (0, 1, 0, 0, 2, 3, 0, 4)
+*
+* Then the arrays are:
+*
+* x_ind = { X; 2, 5, 6, 8 }
+*
+* x_vec = { X; 1, 2, 3, 4 }
+*
+* STORAGE-BY-ROWS
+*
+* For a sparse matrix A, which has m rows, n columns, and ne non-zero
+* elements the storage-by-rows format uses three arrays A_ptr, A_ind,
+* and A_val, which are set up as follows:
+*
+* A_ptr is an integer array of length [1+m+1] also called "row pointer
+* array". It contains the relative starting positions of each row of A
+* in the arrays A_ind and A_val, i.e. element A_ptr[i], 1 <= i <= m,
+* indicates where row i begins in the arrays A_ind and A_val. If all
+* elements in row i are zero, then A_ptr[i] = A_ptr[i+1]. Location
+* A_ptr[0] is not used, location A_ptr[1] must contain 1, and location
+* A_ptr[m+1] must contain ne+1 that indicates the position after the
+* last element in the arrays A_ind and A_val.
+*
+* A_ind is an integer array of length [1+ne]. Location A_ind[0] is not
+* used, and locations A_ind[1], ..., A_ind[ne] contain column indices
+* of (non-zero) elements in matrix A.
+*
+* A_val is a floating-point array of length [1+ne]. Location A_val[0]
+* is not used, and locations A_val[1], ..., A_val[ne] contain numeric
+* values of non-zero elements in matrix A.
+*
+* Non-zero elements of matrix A are stored contiguously, and the rows
+* of matrix A are stored consecutively from 1 to m in the arrays A_ind
+* and A_val. The elements in each row of A may be stored in any order
+* in A_ind and A_val. Note that elements with duplicate column indices
+* are not allowed.
+*
+* Let, for example, the following sparse matrix A be given:
+*
+* | 11 . 13 . . . |
+* | 21 22 . 24 . . |
+* | . 32 33 . . . |
+* | . . 43 44 . 46 |
+* | . . . . . . |
+* | 61 62 . . . 66 |
+*
+* Then the arrays are:
+*
+* A_ptr = { X; 1, 3, 6, 8, 11, 11; 14 }
+*
+* A_ind = { X; 1, 3; 4, 2, 1; 2, 3; 4, 3, 6; 1, 2, 6 }
+*
+* A_val = { X; 11, 13; 24, 22, 21; 32, 33; 44, 43, 46; 61, 62, 66 }
+*
+* PERMUTATION MATRICES
+*
+* Let P be a permutation matrix of the order n. It is represented as
+* an integer array P_per of length [1+n+n] as follows: if p[i,j] = 1,
+* then P_per[i] = j and P_per[n+j] = i. Location P_per[0] is not used.
+*
+* Let A' = P*A. If i-th row of A corresponds to i'-th row of A', then
+* P_per[i'] = i and P_per[n+i] = i'.
+*
+* References:
+*
+* 1. Gustavson F.G. Some basic techniques for solving sparse systems of
+* linear equations. In Rose and Willoughby (1972), pp. 41-52.
+*
+* 2. Basic Linear Algebra Subprograms Technical (BLAST) Forum Standard.
+* University of Tennessee (2001). */
+
+#define check_fvs _glp_mat_check_fvs
+int check_fvs(int n, int nnz, int ind[], double vec[]);
+/* check sparse vector in full-vector storage format */
+
+#define check_pattern _glp_mat_check_pattern
+int check_pattern(int m, int n, int A_ptr[], int A_ind[]);
+/* check pattern of sparse matrix */
+
+#define transpose _glp_mat_transpose
+void transpose(int m, int n, int A_ptr[], int A_ind[], double A_val[],
+ int AT_ptr[], int AT_ind[], double AT_val[]);
+/* transpose sparse matrix */
+
+#define adat_symbolic _glp_mat_adat_symbolic
+int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], int A_ind[],
+ int S_ptr[]);
+/* compute S = P*A*D*A'*P' (symbolic phase) */
+
+#define adat_numeric _glp_mat_adat_numeric
+void adat_numeric(int m, int n, int P_per[],
+ int A_ptr[], int A_ind[], double A_val[], double D_diag[],
+ int S_ptr[], int S_ind[], double S_val[], double S_diag[]);
+/* compute S = P*A*D*A'*P' (numeric phase) */
+
+#define min_degree _glp_mat_min_degree
+void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]);
+/* minimum degree ordering */
+
+#define amd_order1 _glp_mat_amd_order1
+void amd_order1(int n, int A_ptr[], int A_ind[], int P_per[]);
+/* approximate minimum degree ordering (AMD) */
+
+#define symamd_ord _glp_mat_symamd_ord
+void symamd_ord(int n, int A_ptr[], int A_ind[], int P_per[]);
+/* approximate minimum degree ordering (SYMAMD) */
+
+#define chol_symbolic _glp_mat_chol_symbolic
+int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]);
+/* compute Cholesky factorization (symbolic phase) */
+
+#define chol_numeric _glp_mat_chol_numeric
+int chol_numeric(int n,
+ int A_ptr[], int A_ind[], double A_val[], double A_diag[],
+ int U_ptr[], int U_ind[], double U_val[], double U_diag[]);
+/* compute Cholesky factorization (numeric phase) */
+
+#define u_solve _glp_mat_u_solve
+void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+ double U_diag[], double x[]);
+/* solve upper triangular system U*x = b */
+
+#define ut_solve _glp_mat_ut_solve
+void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
+ double U_diag[], double x[]);
+/* solve lower triangular system U'*x = b */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glprgr.c b/test/monniaux/glpk-4.65/src/draft/glprgr.c
new file mode 100644
index 00000000..fbff6b8d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glprgr.c
@@ -0,0 +1,173 @@
+/* glprgr.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#define _GLPSTD_ERRNO
+#define _GLPSTD_STDIO
+#include "env.h"
+#include "glprgr.h"
+#define xfault xerror
+
+/***********************************************************************
+* NAME
+*
+* rgr_write_bmp16 - write 16-color raster image in BMP file format
+*
+* SYNOPSIS
+*
+* #include "glprgr.h"
+* int rgr_write_bmp16(const char *fname, int m, int n, const char
+* map[]);
+*
+* DESCRIPTION
+*
+* The routine rgr_write_bmp16 writes 16-color raster image in
+* uncompressed BMP file format (Windows bitmap) to a binary file whose
+* name is specified by the character string fname.
+*
+* The parameters m and n specify, respectively, the number of rows and
+* the numbers of columns (i.e. height and width) of the raster image.
+*
+* The character array map has m*n elements. Elements map[0, ..., n-1]
+* correspond to the first (top) scanline, elements map[n, ..., 2*n-1]
+* correspond to the second scanline, etc.
+*
+* Each element of the array map specifies a color of the corresponding
+* pixel as 8-bit binary number XXXXIRGB, where four high-order bits (X)
+* are ignored, I is high intensity bit, R is red color bit, G is green
+* color bit, and B is blue color bit. Thus, all 16 possible colors are
+* coded as following hexadecimal numbers:
+*
+* 0x00 = black 0x08 = dark gray
+* 0x01 = blue 0x09 = bright blue
+* 0x02 = green 0x0A = bright green
+* 0x03 = cyan 0x0B = bright cyan
+* 0x04 = red 0x0C = bright red
+* 0x05 = magenta 0x0D = bright magenta
+* 0x06 = brown 0x0E = yellow
+* 0x07 = light gray 0x0F = white
+*
+* RETURNS
+*
+* If no error occured, the routine returns zero; otherwise, it prints
+* an appropriate error message and returns non-zero. */
+
+static void put_byte(FILE *fp, int c)
+{ fputc(c, fp);
+ return;
+}
+
+static void put_word(FILE *fp, int w)
+{ /* big endian */
+ put_byte(fp, w);
+ put_byte(fp, w >> 8);
+ return;
+}
+
+static void put_dword(FILE *fp, int d)
+{ /* big endian */
+ put_word(fp, d);
+ put_word(fp, d >> 16);
+ return;
+}
+
+int rgr_write_bmp16(const char *fname, int m, int n, const char map[])
+{ FILE *fp;
+ int offset, bmsize, i, j, b, ret = 0;
+ if (!(1 <= m && m <= 32767))
+ xfault("rgr_write_bmp16: m = %d; invalid height\n", m);
+ if (!(1 <= n && n <= 32767))
+ xfault("rgr_write_bmp16: n = %d; invalid width\n", n);
+ fp = fopen(fname, "wb");
+ if (fp == NULL)
+ { xprintf("rgr_write_bmp16: unable to create '%s' - %s\n",
+#if 0 /* 29/I-2017 */
+ fname, strerror(errno));
+#else
+ fname, xstrerr(errno));
+#endif
+ ret = 1;
+ goto fini;
+ }
+ offset = 14 + 40 + 16 * 4;
+ bmsize = (4 * n + 31) / 32;
+ /* struct BMPFILEHEADER (14 bytes) */
+ /* UINT bfType */ put_byte(fp, 'B'), put_byte(fp, 'M');
+ /* DWORD bfSize */ put_dword(fp, offset + bmsize * 4);
+ /* UINT bfReserved1 */ put_word(fp, 0);
+ /* UNIT bfReserved2 */ put_word(fp, 0);
+ /* DWORD bfOffBits */ put_dword(fp, offset);
+ /* struct BMPINFOHEADER (40 bytes) */
+ /* DWORD biSize */ put_dword(fp, 40);
+ /* LONG biWidth */ put_dword(fp, n);
+ /* LONG biHeight */ put_dword(fp, m);
+ /* WORD biPlanes */ put_word(fp, 1);
+ /* WORD biBitCount */ put_word(fp, 4);
+ /* DWORD biCompression */ put_dword(fp, 0 /* BI_RGB */);
+ /* DWORD biSizeImage */ put_dword(fp, 0);
+ /* LONG biXPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
+ /* LONG biYPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
+ /* DWORD biClrUsed */ put_dword(fp, 0);
+ /* DWORD biClrImportant */ put_dword(fp, 0);
+ /* struct RGBQUAD (16 * 4 = 64 bytes) */
+ /* CGA-compatible colors: */
+ /* 0x00 = black */ put_dword(fp, 0x000000);
+ /* 0x01 = blue */ put_dword(fp, 0x000080);
+ /* 0x02 = green */ put_dword(fp, 0x008000);
+ /* 0x03 = cyan */ put_dword(fp, 0x008080);
+ /* 0x04 = red */ put_dword(fp, 0x800000);
+ /* 0x05 = magenta */ put_dword(fp, 0x800080);
+ /* 0x06 = brown */ put_dword(fp, 0x808000);
+ /* 0x07 = light gray */ put_dword(fp, 0xC0C0C0);
+ /* 0x08 = dark gray */ put_dword(fp, 0x808080);
+ /* 0x09 = bright blue */ put_dword(fp, 0x0000FF);
+ /* 0x0A = bright green */ put_dword(fp, 0x00FF00);
+ /* 0x0B = bright cyan */ put_dword(fp, 0x00FFFF);
+ /* 0x0C = bright red */ put_dword(fp, 0xFF0000);
+ /* 0x0D = bright magenta */ put_dword(fp, 0xFF00FF);
+ /* 0x0E = yellow */ put_dword(fp, 0xFFFF00);
+ /* 0x0F = white */ put_dword(fp, 0xFFFFFF);
+ /* pixel data bits */
+ b = 0;
+ for (i = m - 1; i >= 0; i--)
+ { for (j = 0; j < ((n + 7) / 8) * 8; j++)
+ { b <<= 4;
+ b |= (j < n ? map[i * n + j] & 15 : 0);
+ if (j & 1) put_byte(fp, b);
+ }
+ }
+ fflush(fp);
+ if (ferror(fp))
+ { xprintf("rgr_write_bmp16: write error on '%s' - %s\n",
+#if 0 /* 29/I-2017 */
+ fname, strerror(errno));
+#else
+ fname, xstrerr(errno));
+#endif
+ ret = 1;
+ }
+fini: if (fp != NULL) fclose(fp);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glprgr.h b/test/monniaux/glpk-4.65/src/draft/glprgr.h
new file mode 100644
index 00000000..71e089e9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glprgr.h
@@ -0,0 +1,34 @@
+/* glprgr.h (raster graphics) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPRGR_H
+#define GLPRGR_H
+
+#define rgr_write_bmp16 _glp_rgr_write_bmp16
+int rgr_write_bmp16(const char *fname, int m, int n, const char map[]);
+/* write 16-color raster image in BMP file format */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpscl.c b/test/monniaux/glpk-4.65/src/draft/glpscl.c
new file mode 100644
index 00000000..de769a8b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpscl.c
@@ -0,0 +1,478 @@
+/* glpscl.c (problem scaling routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+#include "prob.h"
+
+/***********************************************************************
+* min_row_aij - determine minimal |a[i,j]| in i-th row
+*
+* This routine returns minimal magnitude of (non-zero) constraint
+* coefficients in i-th row of the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If i-th row of the matrix is empty, the routine returns 1. */
+
+static double min_row_aij(glp_prob *lp, int i, int scaled)
+{ GLPAIJ *aij;
+ double min_aij, temp;
+ xassert(1 <= i && i <= lp->m);
+ min_aij = 1.0;
+ for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { temp = fabs(aij->val);
+ if (scaled) temp *= (aij->row->rii * aij->col->sjj);
+ if (aij->r_prev == NULL || min_aij > temp)
+ min_aij = temp;
+ }
+ return min_aij;
+}
+
+/***********************************************************************
+* max_row_aij - determine maximal |a[i,j]| in i-th row
+*
+* This routine returns maximal magnitude of (non-zero) constraint
+* coefficients in i-th row of the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If i-th row of the matrix is empty, the routine returns 1. */
+
+static double max_row_aij(glp_prob *lp, int i, int scaled)
+{ GLPAIJ *aij;
+ double max_aij, temp;
+ xassert(1 <= i && i <= lp->m);
+ max_aij = 1.0;
+ for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { temp = fabs(aij->val);
+ if (scaled) temp *= (aij->row->rii * aij->col->sjj);
+ if (aij->r_prev == NULL || max_aij < temp)
+ max_aij = temp;
+ }
+ return max_aij;
+}
+
+/***********************************************************************
+* min_col_aij - determine minimal |a[i,j]| in j-th column
+*
+* This routine returns minimal magnitude of (non-zero) constraint
+* coefficients in j-th column of the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If j-th column of the matrix is empty, the routine returns 1. */
+
+static double min_col_aij(glp_prob *lp, int j, int scaled)
+{ GLPAIJ *aij;
+ double min_aij, temp;
+ xassert(1 <= j && j <= lp->n);
+ min_aij = 1.0;
+ for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
+ { temp = fabs(aij->val);
+ if (scaled) temp *= (aij->row->rii * aij->col->sjj);
+ if (aij->c_prev == NULL || min_aij > temp)
+ min_aij = temp;
+ }
+ return min_aij;
+}
+
+/***********************************************************************
+* max_col_aij - determine maximal |a[i,j]| in j-th column
+*
+* This routine returns maximal magnitude of (non-zero) constraint
+* coefficients in j-th column of the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If j-th column of the matrix is empty, the routine returns 1. */
+
+static double max_col_aij(glp_prob *lp, int j, int scaled)
+{ GLPAIJ *aij;
+ double max_aij, temp;
+ xassert(1 <= j && j <= lp->n);
+ max_aij = 1.0;
+ for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
+ { temp = fabs(aij->val);
+ if (scaled) temp *= (aij->row->rii * aij->col->sjj);
+ if (aij->c_prev == NULL || max_aij < temp)
+ max_aij = temp;
+ }
+ return max_aij;
+}
+
+/***********************************************************************
+* min_mat_aij - determine minimal |a[i,j]| in constraint matrix
+*
+* This routine returns minimal magnitude of (non-zero) constraint
+* coefficients in the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If the matrix is empty, the routine returns 1. */
+
+static double min_mat_aij(glp_prob *lp, int scaled)
+{ int i;
+ double min_aij, temp;
+ min_aij = 1.0;
+ for (i = 1; i <= lp->m; i++)
+ { temp = min_row_aij(lp, i, scaled);
+ if (i == 1 || min_aij > temp)
+ min_aij = temp;
+ }
+ return min_aij;
+}
+
+/***********************************************************************
+* max_mat_aij - determine maximal |a[i,j]| in constraint matrix
+*
+* This routine returns maximal magnitude of (non-zero) constraint
+* coefficients in the constraint matrix.
+*
+* If the parameter scaled is zero, the original constraint matrix A is
+* assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
+*
+* If the matrix is empty, the routine returns 1. */
+
+static double max_mat_aij(glp_prob *lp, int scaled)
+{ int i;
+ double max_aij, temp;
+ max_aij = 1.0;
+ for (i = 1; i <= lp->m; i++)
+ { temp = max_row_aij(lp, i, scaled);
+ if (i == 1 || max_aij < temp)
+ max_aij = temp;
+ }
+ return max_aij;
+}
+
+/***********************************************************************
+* eq_scaling - perform equilibration scaling
+*
+* This routine performs equilibration scaling of rows and columns of
+* the constraint matrix.
+*
+* If the parameter flag is zero, the routine scales rows at first and
+* then columns. Otherwise, the routine scales columns and then rows.
+*
+* Rows are scaled as follows:
+*
+* n
+* a'[i,j] = a[i,j] / max |a[i,j]|, i = 1,...,m.
+* j=1
+*
+* This makes the infinity (maximum) norm of each row of the matrix
+* equal to 1.
+*
+* Columns are scaled as follows:
+*
+* m
+* a'[i,j] = a[i,j] / max |a[i,j]|, j = 1,...,n.
+* i=1
+*
+* This makes the infinity (maximum) norm of each column of the matrix
+* equal to 1. */
+
+static void eq_scaling(glp_prob *lp, int flag)
+{ int i, j, pass;
+ double temp;
+ xassert(flag == 0 || flag == 1);
+ for (pass = 0; pass <= 1; pass++)
+ { if (pass == flag)
+ { /* scale rows */
+ for (i = 1; i <= lp->m; i++)
+ { temp = max_row_aij(lp, i, 1);
+ glp_set_rii(lp, i, glp_get_rii(lp, i) / temp);
+ }
+ }
+ else
+ { /* scale columns */
+ for (j = 1; j <= lp->n; j++)
+ { temp = max_col_aij(lp, j, 1);
+ glp_set_sjj(lp, j, glp_get_sjj(lp, j) / temp);
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* gm_scaling - perform geometric mean scaling
+*
+* This routine performs geometric mean scaling of rows and columns of
+* the constraint matrix.
+*
+* If the parameter flag is zero, the routine scales rows at first and
+* then columns. Otherwise, the routine scales columns and then rows.
+*
+* Rows are scaled as follows:
+*
+* a'[i,j] = a[i,j] / sqrt(alfa[i] * beta[i]), i = 1,...,m,
+*
+* where:
+* n n
+* alfa[i] = min |a[i,j]|, beta[i] = max |a[i,j]|.
+* j=1 j=1
+*
+* This allows decreasing the ratio beta[i] / alfa[i] for each row of
+* the matrix.
+*
+* Columns are scaled as follows:
+*
+* a'[i,j] = a[i,j] / sqrt(alfa[j] * beta[j]), j = 1,...,n,
+*
+* where:
+* m m
+* alfa[j] = min |a[i,j]|, beta[j] = max |a[i,j]|.
+* i=1 i=1
+*
+* This allows decreasing the ratio beta[j] / alfa[j] for each column
+* of the matrix. */
+
+static void gm_scaling(glp_prob *lp, int flag)
+{ int i, j, pass;
+ double temp;
+ xassert(flag == 0 || flag == 1);
+ for (pass = 0; pass <= 1; pass++)
+ { if (pass == flag)
+ { /* scale rows */
+ for (i = 1; i <= lp->m; i++)
+ { temp = min_row_aij(lp, i, 1) * max_row_aij(lp, i, 1);
+ glp_set_rii(lp, i, glp_get_rii(lp, i) / sqrt(temp));
+ }
+ }
+ else
+ { /* scale columns */
+ for (j = 1; j <= lp->n; j++)
+ { temp = min_col_aij(lp, j, 1) * max_col_aij(lp, j, 1);
+ glp_set_sjj(lp, j, glp_get_sjj(lp, j) / sqrt(temp));
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* max_row_ratio - determine worst scaling "quality" for rows
+*
+* This routine returns the worst scaling "quality" for rows of the
+* currently scaled constraint matrix:
+*
+* m
+* ratio = max ratio[i],
+* i=1
+* where:
+* n n
+* ratio[i] = max |a[i,j]| / min |a[i,j]|, 1 <= i <= m,
+* j=1 j=1
+*
+* is the scaling "quality" of i-th row. */
+
+static double max_row_ratio(glp_prob *lp)
+{ int i;
+ double ratio, temp;
+ ratio = 1.0;
+ for (i = 1; i <= lp->m; i++)
+ { temp = max_row_aij(lp, i, 1) / min_row_aij(lp, i, 1);
+ if (i == 1 || ratio < temp) ratio = temp;
+ }
+ return ratio;
+}
+
+/***********************************************************************
+* max_col_ratio - determine worst scaling "quality" for columns
+*
+* This routine returns the worst scaling "quality" for columns of the
+* currently scaled constraint matrix:
+*
+* n
+* ratio = max ratio[j],
+* j=1
+* where:
+* m m
+* ratio[j] = max |a[i,j]| / min |a[i,j]|, 1 <= j <= n,
+* i=1 i=1
+*
+* is the scaling "quality" of j-th column. */
+
+static double max_col_ratio(glp_prob *lp)
+{ int j;
+ double ratio, temp;
+ ratio = 1.0;
+ for (j = 1; j <= lp->n; j++)
+ { temp = max_col_aij(lp, j, 1) / min_col_aij(lp, j, 1);
+ if (j == 1 || ratio < temp) ratio = temp;
+ }
+ return ratio;
+}
+
+/***********************************************************************
+* gm_iterate - perform iterative geometric mean scaling
+*
+* This routine performs iterative geometric mean scaling of rows and
+* columns of the constraint matrix.
+*
+* The parameter it_max specifies the maximal number of iterations.
+* Recommended value of it_max is 15.
+*
+* The parameter tau specifies a minimal improvement of the scaling
+* "quality" on each iteration, 0 < tau < 1. It means than the scaling
+* process continues while the following condition is satisfied:
+*
+* ratio[k] <= tau * ratio[k-1],
+*
+* where ratio = max |a[i,j]| / min |a[i,j]| is the scaling "quality"
+* to be minimized, k is the iteration number. Recommended value of tau
+* is 0.90. */
+
+static void gm_iterate(glp_prob *lp, int it_max, double tau)
+{ int k, flag;
+ double ratio = 0.0, r_old;
+ /* if the scaling "quality" for rows is better than for columns,
+ the rows are scaled first; otherwise, the columns are scaled
+ first */
+ flag = (max_row_ratio(lp) > max_col_ratio(lp));
+ for (k = 1; k <= it_max; k++)
+ { /* save the scaling "quality" from previous iteration */
+ r_old = ratio;
+ /* determine the current scaling "quality" */
+ ratio = max_mat_aij(lp, 1) / min_mat_aij(lp, 1);
+#if 0
+ xprintf("k = %d; ratio = %g\n", k, ratio);
+#endif
+ /* if improvement is not enough, terminate scaling */
+ if (k > 1 && ratio > tau * r_old) break;
+ /* otherwise, perform another iteration */
+ gm_scaling(lp, flag);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* scale_prob - scale problem data
+*
+* SYNOPSIS
+*
+* #include "glpscl.h"
+* void scale_prob(glp_prob *lp, int flags);
+*
+* DESCRIPTION
+*
+* The routine scale_prob performs automatic scaling of problem data
+* for the specified problem object. */
+
+static void scale_prob(glp_prob *lp, int flags)
+{ static const char *fmt =
+ "%s: min|aij| = %10.3e max|aij| = %10.3e ratio = %10.3e\n";
+ double min_aij, max_aij, ratio;
+ xprintf("Scaling...\n");
+ /* cancel the current scaling effect */
+ glp_unscale_prob(lp);
+ /* report original scaling "quality" */
+ min_aij = min_mat_aij(lp, 1);
+ max_aij = max_mat_aij(lp, 1);
+ ratio = max_aij / min_aij;
+ xprintf(fmt, " A", min_aij, max_aij, ratio);
+ /* check if the problem is well scaled */
+ if (min_aij >= 0.10 && max_aij <= 10.0)
+ { xprintf("Problem data seem to be well scaled\n");
+ /* skip scaling, if required */
+ if (flags & GLP_SF_SKIP) goto done;
+ }
+ /* perform iterative geometric mean scaling, if required */
+ if (flags & GLP_SF_GM)
+ { gm_iterate(lp, 15, 0.90);
+ min_aij = min_mat_aij(lp, 1);
+ max_aij = max_mat_aij(lp, 1);
+ ratio = max_aij / min_aij;
+ xprintf(fmt, "GM", min_aij, max_aij, ratio);
+ }
+ /* perform equilibration scaling, if required */
+ if (flags & GLP_SF_EQ)
+ { eq_scaling(lp, max_row_ratio(lp) > max_col_ratio(lp));
+ min_aij = min_mat_aij(lp, 1);
+ max_aij = max_mat_aij(lp, 1);
+ ratio = max_aij / min_aij;
+ xprintf(fmt, "EQ", min_aij, max_aij, ratio);
+ }
+ /* round scale factors to nearest power of two, if required */
+ if (flags & GLP_SF_2N)
+ { int i, j;
+ for (i = 1; i <= lp->m; i++)
+ glp_set_rii(lp, i, round2n(glp_get_rii(lp, i)));
+ for (j = 1; j <= lp->n; j++)
+ glp_set_sjj(lp, j, round2n(glp_get_sjj(lp, j)));
+ min_aij = min_mat_aij(lp, 1);
+ max_aij = max_mat_aij(lp, 1);
+ ratio = max_aij / min_aij;
+ xprintf(fmt, "2N", min_aij, max_aij, ratio);
+ }
+done: return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_scale_prob - scale problem data
+*
+* SYNOPSIS
+*
+* void glp_scale_prob(glp_prob *lp, int flags);
+*
+* DESCRIPTION
+*
+* The routine glp_scale_prob performs automatic scaling of problem
+* data for the specified problem object.
+*
+* The parameter flags specifies scaling options used by the routine.
+* Options can be combined with the bitwise OR operator and may be the
+* following:
+*
+* GLP_SF_GM perform geometric mean scaling;
+* GLP_SF_EQ perform equilibration scaling;
+* GLP_SF_2N round scale factors to nearest power of two;
+* GLP_SF_SKIP skip scaling, if the problem is well scaled.
+*
+* The parameter flags may be specified as GLP_SF_AUTO, in which case
+* the routine chooses scaling options automatically. */
+
+void glp_scale_prob(glp_prob *lp, int flags)
+{ if (flags & ~(GLP_SF_GM | GLP_SF_EQ | GLP_SF_2N | GLP_SF_SKIP |
+ GLP_SF_AUTO))
+ xerror("glp_scale_prob: flags = 0x%02X; invalid scaling option"
+ "s\n", flags);
+ if (flags & GLP_SF_AUTO)
+ flags = (GLP_SF_GM | GLP_SF_EQ | GLP_SF_SKIP);
+ scale_prob(lp, flags);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpspm.c b/test/monniaux/glpk-4.65/src/draft/glpspm.c
new file mode 100644
index 00000000..c6cfd25d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpspm.c
@@ -0,0 +1,847 @@
+/* glpspm.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "glphbm.h"
+#include "glprgr.h"
+#include "glpspm.h"
+#include "env.h"
+
+/***********************************************************************
+* NAME
+*
+* spm_create_mat - create general sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_create_mat(int m, int n);
+*
+* DESCRIPTION
+*
+* The routine spm_create_mat creates a general sparse matrix having
+* m rows and n columns. Being created the matrix is zero (empty), i.e.
+* has no elements.
+*
+* RETURNS
+*
+* The routine returns a pointer to the matrix created. */
+
+SPM *spm_create_mat(int m, int n)
+{ SPM *A;
+ xassert(0 <= m && m < INT_MAX);
+ xassert(0 <= n && n < INT_MAX);
+ A = xmalloc(sizeof(SPM));
+ A->m = m;
+ A->n = n;
+ if (m == 0 || n == 0)
+ { A->pool = NULL;
+ A->row = NULL;
+ A->col = NULL;
+ }
+ else
+ { int i, j;
+ A->pool = dmp_create_pool();
+ A->row = xcalloc(1+m, sizeof(SPME *));
+ for (i = 1; i <= m; i++) A->row[i] = NULL;
+ A->col = xcalloc(1+n, sizeof(SPME *));
+ for (j = 1; j <= n; j++) A->col[j] = NULL;
+ }
+ return A;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_new_elem - add new element to sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPME *spm_new_elem(SPM *A, int i, int j, double val);
+*
+* DESCRIPTION
+*
+* The routine spm_new_elem adds a new element to the specified sparse
+* matrix. Parameters i, j, and val specify the row number, the column
+* number, and a numerical value of the element, respectively.
+*
+* RETURNS
+*
+* The routine returns a pointer to the new element added. */
+
+SPME *spm_new_elem(SPM *A, int i, int j, double val)
+{ SPME *e;
+ xassert(1 <= i && i <= A->m);
+ xassert(1 <= j && j <= A->n);
+ e = dmp_get_atom(A->pool, sizeof(SPME));
+ e->i = i;
+ e->j = j;
+ e->val = val;
+ e->r_prev = NULL;
+ e->r_next = A->row[i];
+ if (e->r_next != NULL) e->r_next->r_prev = e;
+ e->c_prev = NULL;
+ e->c_next = A->col[j];
+ if (e->c_next != NULL) e->c_next->c_prev = e;
+ A->row[i] = A->col[j] = e;
+ return e;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_delete_mat - delete general sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* void spm_delete_mat(SPM *A);
+*
+* DESCRIPTION
+*
+* The routine deletes the specified general sparse matrix freeing all
+* the memory allocated to this object. */
+
+void spm_delete_mat(SPM *A)
+{ /* delete sparse matrix */
+ if (A->pool != NULL) dmp_delete_pool(A->pool);
+ if (A->row != NULL) xfree(A->row);
+ if (A->col != NULL) xfree(A->col);
+ xfree(A);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_test_mat_e - create test sparse matrix of E(n,c) class
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_test_mat_e(int n, int c);
+*
+* DESCRIPTION
+*
+* The routine spm_test_mat_e creates a test sparse matrix of E(n,c)
+* class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
+* Methods for Sparse Matrices. Springer-Verlag, 1983.
+*
+* Matrix of E(n,c) class is a symmetric positive definite matrix of
+* the order n. It has the number 4 on its main diagonal and the number
+* -1 on its four co-diagonals, two of which are neighbour to the main
+* diagonal and two others are shifted from the main diagonal on the
+* distance c.
+*
+* It is necessary that n >= 3 and 2 <= c <= n-1.
+*
+* RETURNS
+*
+* The routine returns a pointer to the matrix created. */
+
+SPM *spm_test_mat_e(int n, int c)
+{ SPM *A;
+ int i;
+ xassert(n >= 3 && 2 <= c && c <= n-1);
+ A = spm_create_mat(n, n);
+ for (i = 1; i <= n; i++)
+ spm_new_elem(A, i, i, 4.0);
+ for (i = 1; i <= n-1; i++)
+ { spm_new_elem(A, i, i+1, -1.0);
+ spm_new_elem(A, i+1, i, -1.0);
+ }
+ for (i = 1; i <= n-c; i++)
+ { spm_new_elem(A, i, i+c, -1.0);
+ spm_new_elem(A, i+c, i, -1.0);
+ }
+ return A;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_test_mat_d - create test sparse matrix of D(n,c) class
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_test_mat_d(int n, int c);
+*
+* DESCRIPTION
+*
+* The routine spm_test_mat_d creates a test sparse matrix of D(n,c)
+* class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
+* Methods for Sparse Matrices. Springer-Verlag, 1983.
+*
+* Matrix of D(n,c) class is a non-singular matrix of the order n. It
+* has unity main diagonal, three co-diagonals above the main diagonal
+* on the distance c, which are cyclically continued below the main
+* diagonal, and a triangle block of the size 10x10 in the upper right
+* corner.
+*
+* It is necessary that n >= 14 and 1 <= c <= n-13.
+*
+* RETURNS
+*
+* The routine returns a pointer to the matrix created. */
+
+SPM *spm_test_mat_d(int n, int c)
+{ SPM *A;
+ int i, j;
+ xassert(n >= 14 && 1 <= c && c <= n-13);
+ A = spm_create_mat(n, n);
+ for (i = 1; i <= n; i++)
+ spm_new_elem(A, i, i, 1.0);
+ for (i = 1; i <= n-c; i++)
+ spm_new_elem(A, i, i+c, (double)(i+1));
+ for (i = n-c+1; i <= n; i++)
+ spm_new_elem(A, i, i-n+c, (double)(i+1));
+ for (i = 1; i <= n-c-1; i++)
+ spm_new_elem(A, i, i+c+1, (double)(-i));
+ for (i = n-c; i <= n; i++)
+ spm_new_elem(A, i, i-n+c+1, (double)(-i));
+ for (i = 1; i <= n-c-2; i++)
+ spm_new_elem(A, i, i+c+2, 16.0);
+ for (i = n-c-1; i <= n; i++)
+ spm_new_elem(A, i, i-n+c+2, 16.0);
+ for (j = 1; j <= 10; j++)
+ for (i = 1; i <= 11-j; i++)
+ spm_new_elem(A, i, n-11+i+j, 100.0 * (double)j);
+ return A;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_show_mat - write sparse matrix pattern in BMP file format
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* int spm_show_mat(const SPM *A, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine spm_show_mat writes pattern of the specified sparse
+* matrix in uncompressed BMP file format (Windows bitmap) to a binary
+* file whose name is specified by the character string fname.
+*
+* Each pixel corresponds to one matrix element. The pixel colors have
+* the following meaning:
+*
+* Black structurally zero element
+* White positive element
+* Cyan negative element
+* Green zero element
+* Red duplicate element
+*
+* RETURNS
+*
+* If no error occured, the routine returns zero. Otherwise, it prints
+* an appropriate error message and returns non-zero. */
+
+int spm_show_mat(const SPM *A, const char *fname)
+{ int m = A->m;
+ int n = A->n;
+ int i, j, k, ret;
+ char *map;
+ xprintf("spm_show_mat: writing matrix pattern to '%s'...\n",
+ fname);
+ xassert(1 <= m && m <= 32767);
+ xassert(1 <= n && n <= 32767);
+ map = xmalloc(m * n);
+ memset(map, 0x08, m * n);
+ for (i = 1; i <= m; i++)
+ { SPME *e;
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ { j = e->j;
+ xassert(1 <= j && j <= n);
+ k = n * (i - 1) + (j - 1);
+ if (map[k] != 0x08)
+ map[k] = 0x0C;
+ else if (e->val > 0.0)
+ map[k] = 0x0F;
+ else if (e->val < 0.0)
+ map[k] = 0x0B;
+ else
+ map[k] = 0x0A;
+ }
+ }
+ ret = rgr_write_bmp16(fname, m, n, map);
+ xfree(map);
+ return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_read_hbm - read sparse matrix in Harwell-Boeing format
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_read_hbm(const char *fname);
+*
+* DESCRIPTION
+*
+* The routine spm_read_hbm reads a sparse matrix in the Harwell-Boeing
+* format from a text file whose name is the character string fname.
+*
+* Detailed description of the Harwell-Boeing format recognised by this
+* routine can be found in the following report:
+*
+* I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the Harwell-Boeing
+* Sparse Matrix Collection (Release I), TR/PA/92/86, October 1992.
+*
+* NOTE
+*
+* The routine spm_read_hbm reads the matrix "as is", due to which zero
+* and/or duplicate elements can appear in the matrix.
+*
+* RETURNS
+*
+* If no error occured, the routine returns a pointer to the matrix
+* created. Otherwise, the routine prints an appropriate error message
+* and returns NULL. */
+
+SPM *spm_read_hbm(const char *fname)
+{ SPM *A = NULL;
+ HBM *hbm;
+ int nrow, ncol, nnzero, i, j, beg, end, ptr, *colptr, *rowind;
+ double val, *values;
+ char *mxtype;
+ hbm = hbm_read_mat(fname);
+ if (hbm == NULL)
+ { xprintf("spm_read_hbm: unable to read matrix\n");
+ goto fini;
+ }
+ mxtype = hbm->mxtype;
+ nrow = hbm->nrow;
+ ncol = hbm->ncol;
+ nnzero = hbm->nnzero;
+ colptr = hbm->colptr;
+ rowind = hbm->rowind;
+ values = hbm->values;
+ if (!(strcmp(mxtype, "RSA") == 0 || strcmp(mxtype, "PSA") == 0 ||
+ strcmp(mxtype, "RUA") == 0 || strcmp(mxtype, "PUA") == 0 ||
+ strcmp(mxtype, "RRA") == 0 || strcmp(mxtype, "PRA") == 0))
+ { xprintf("spm_read_hbm: matrix type '%s' not supported\n",
+ mxtype);
+ goto fini;
+ }
+ A = spm_create_mat(nrow, ncol);
+ if (mxtype[1] == 'S' || mxtype[1] == 'U')
+ xassert(nrow == ncol);
+ for (j = 1; j <= ncol; j++)
+ { beg = colptr[j];
+ end = colptr[j+1];
+ xassert(1 <= beg && beg <= end && end <= nnzero + 1);
+ for (ptr = beg; ptr < end; ptr++)
+ { i = rowind[ptr];
+ xassert(1 <= i && i <= nrow);
+ if (mxtype[0] == 'R')
+ val = values[ptr];
+ else
+ val = 1.0;
+ spm_new_elem(A, i, j, val);
+ if (mxtype[1] == 'S' && i != j)
+ spm_new_elem(A, j, i, val);
+ }
+ }
+fini: if (hbm != NULL) hbm_free_mat(hbm);
+ return A;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_count_nnz - determine number of non-zeros in sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* int spm_count_nnz(const SPM *A);
+*
+* RETURNS
+*
+* The routine spm_count_nnz returns the number of structural non-zero
+* elements in the specified sparse matrix. */
+
+int spm_count_nnz(const SPM *A)
+{ SPME *e;
+ int i, nnz = 0;
+ for (i = 1; i <= A->m; i++)
+ for (e = A->row[i]; e != NULL; e = e->r_next) nnz++;
+ return nnz;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_drop_zeros - remove zero elements from sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* int spm_drop_zeros(SPM *A, double eps);
+*
+* DESCRIPTION
+*
+* The routine spm_drop_zeros removes all elements from the specified
+* sparse matrix, whose absolute value is less than eps.
+*
+* If the parameter eps is 0, only zero elements are removed from the
+* matrix.
+*
+* RETURNS
+*
+* The routine returns the number of elements removed. */
+
+int spm_drop_zeros(SPM *A, double eps)
+{ SPME *e, *next;
+ int i, count = 0;
+ for (i = 1; i <= A->m; i++)
+ { for (e = A->row[i]; e != NULL; e = next)
+ { next = e->r_next;
+ if (e->val == 0.0 || fabs(e->val) < eps)
+ { /* remove element from the row list */
+ if (e->r_prev == NULL)
+ A->row[e->i] = e->r_next;
+ else
+ e->r_prev->r_next = e->r_next;
+ if (e->r_next == NULL)
+ ;
+ else
+ e->r_next->r_prev = e->r_prev;
+ /* remove element from the column list */
+ if (e->c_prev == NULL)
+ A->col[e->j] = e->c_next;
+ else
+ e->c_prev->c_next = e->c_next;
+ if (e->c_next == NULL)
+ ;
+ else
+ e->c_next->c_prev = e->c_prev;
+ /* return element to the memory pool */
+ dmp_free_atom(A->pool, e, sizeof(SPME));
+ count++;
+ }
+ }
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* spm_read_mat - read sparse matrix from text file
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_read_mat(const char *fname);
+*
+* DESCRIPTION
+*
+* The routine reads a sparse matrix from a text file whose name is
+* specified by the parameter fname.
+*
+* For the file format see description of the routine spm_write_mat.
+*
+* RETURNS
+*
+* On success the routine returns a pointer to the matrix created,
+* otherwise NULL. */
+
+#if 1
+SPM *spm_read_mat(const char *fname)
+{ xassert(fname != fname);
+ return NULL;
+}
+#else
+SPM *spm_read_mat(const char *fname)
+{ SPM *A = NULL;
+ PDS *pds;
+ jmp_buf jump;
+ int i, j, k, m, n, nnz, fail = 0;
+ double val;
+ xprintf("spm_read_mat: reading matrix from '%s'...\n", fname);
+ pds = pds_open_file(fname);
+ if (pds == NULL)
+ { xprintf("spm_read_mat: unable to open '%s' - %s\n", fname,
+ strerror(errno));
+ fail = 1;
+ goto done;
+ }
+ if (setjmp(jump))
+ { fail = 1;
+ goto done;
+ }
+ pds_set_jump(pds, jump);
+ /* number of rows, number of columns, number of non-zeros */
+ m = pds_scan_int(pds);
+ if (m < 0)
+ pds_error(pds, "invalid number of rows\n");
+ n = pds_scan_int(pds);
+ if (n < 0)
+ pds_error(pds, "invalid number of columns\n");
+ nnz = pds_scan_int(pds);
+ if (nnz < 0)
+ pds_error(pds, "invalid number of non-zeros\n");
+ /* create matrix */
+ xprintf("spm_read_mat: %d rows, %d columns, %d non-zeros\n",
+ m, n, nnz);
+ A = spm_create_mat(m, n);
+ /* read matrix elements */
+ for (k = 1; k <= nnz; k++)
+ { /* row index, column index, element value */
+ i = pds_scan_int(pds);
+ if (!(1 <= i && i <= m))
+ pds_error(pds, "row index out of range\n");
+ j = pds_scan_int(pds);
+ if (!(1 <= j && j <= n))
+ pds_error(pds, "column index out of range\n");
+ val = pds_scan_num(pds);
+ /* add new element to the matrix */
+ spm_new_elem(A, i, j, val);
+ }
+ xprintf("spm_read_mat: %d lines were read\n", pds->count);
+done: if (pds != NULL) pds_close_file(pds);
+ if (fail && A != NULL) spm_delete_mat(A), A = NULL;
+ return A;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* spm_write_mat - write sparse matrix to text file
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* int spm_write_mat(const SPM *A, const char *fname);
+*
+* DESCRIPTION
+*
+* The routine spm_write_mat writes the specified sparse matrix to a
+* text file whose name is specified by the parameter fname. This file
+* can be read back with the routine spm_read_mat.
+*
+* RETURNS
+*
+* On success the routine returns zero, otherwise non-zero.
+*
+* FILE FORMAT
+*
+* The file created by the routine spm_write_mat is a plain text file,
+* which contains the following information:
+*
+* m n nnz
+* row[1] col[1] val[1]
+* row[2] col[2] val[2]
+* . . .
+* row[nnz] col[nnz] val[nnz]
+*
+* where:
+* m is the number of rows;
+* n is the number of columns;
+* nnz is the number of non-zeros;
+* row[k], k = 1,...,nnz, are row indices;
+* col[k], k = 1,...,nnz, are column indices;
+* val[k], k = 1,...,nnz, are element values. */
+
+#if 1
+int spm_write_mat(const SPM *A, const char *fname)
+{ xassert(A != A);
+ xassert(fname != fname);
+ return 0;
+}
+#else
+int spm_write_mat(const SPM *A, const char *fname)
+{ FILE *fp;
+ int i, nnz, ret = 0;
+ xprintf("spm_write_mat: writing matrix to '%s'...\n", fname);
+ fp = fopen(fname, "w");
+ if (fp == NULL)
+ { xprintf("spm_write_mat: unable to create '%s' - %s\n", fname,
+ strerror(errno));
+ ret = 1;
+ goto done;
+ }
+ /* number of rows, number of columns, number of non-zeros */
+ nnz = spm_count_nnz(A);
+ fprintf(fp, "%d %d %d\n", A->m, A->n, nnz);
+ /* walk through rows of the matrix */
+ for (i = 1; i <= A->m; i++)
+ { SPME *e;
+ /* walk through elements of i-th row */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ { /* row index, column index, element value */
+ fprintf(fp, "%d %d %.*g\n", e->i, e->j, DBL_DIG, e->val);
+ }
+ }
+ fflush(fp);
+ if (ferror(fp))
+ { xprintf("spm_write_mat: writing error on '%s' - %s\n", fname,
+ strerror(errno));
+ ret = 1;
+ goto done;
+ }
+ xprintf("spm_write_mat: %d lines were written\n", 1 + nnz);
+done: if (fp != NULL) fclose(fp);
+ return ret;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* spm_transpose - transpose sparse matrix
+*
+* SYNOPSIS
+*
+* #include "glpspm.h"
+* SPM *spm_transpose(const SPM *A);
+*
+* RETURNS
+*
+* The routine computes and returns sparse matrix B, which is a matrix
+* transposed to sparse matrix A. */
+
+SPM *spm_transpose(const SPM *A)
+{ SPM *B;
+ int i;
+ B = spm_create_mat(A->n, A->m);
+ for (i = 1; i <= A->m; i++)
+ { SPME *e;
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ spm_new_elem(B, e->j, i, e->val);
+ }
+ return B;
+}
+
+SPM *spm_add_sym(const SPM *A, const SPM *B)
+{ /* add two sparse matrices (symbolic phase) */
+ SPM *C;
+ int i, j, *flag;
+ xassert(A->m == B->m);
+ xassert(A->n == B->n);
+ /* create resultant matrix */
+ C = spm_create_mat(A->m, A->n);
+ /* allocate and clear the flag array */
+ flag = xcalloc(1+C->n, sizeof(int));
+ for (j = 1; j <= C->n; j++)
+ flag[j] = 0;
+ /* compute pattern of C = A + B */
+ for (i = 1; i <= C->m; i++)
+ { SPME *e;
+ /* at the beginning i-th row of C is empty */
+ /* (i-th row of C) := (i-th row of C) union (i-th row of A) */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ { /* (note that i-th row of A may have duplicate elements) */
+ j = e->j;
+ if (!flag[j])
+ { spm_new_elem(C, i, j, 0.0);
+ flag[j] = 1;
+ }
+ }
+ /* (i-th row of C) := (i-th row of C) union (i-th row of B) */
+ for (e = B->row[i]; e != NULL; e = e->r_next)
+ { /* (note that i-th row of B may have duplicate elements) */
+ j = e->j;
+ if (!flag[j])
+ { spm_new_elem(C, i, j, 0.0);
+ flag[j] = 1;
+ }
+ }
+ /* reset the flag array */
+ for (e = C->row[i]; e != NULL; e = e->r_next)
+ flag[e->j] = 0;
+ }
+ /* check and deallocate the flag array */
+ for (j = 1; j <= C->n; j++)
+ xassert(!flag[j]);
+ xfree(flag);
+ return C;
+}
+
+void spm_add_num(SPM *C, double alfa, const SPM *A, double beta,
+ const SPM *B)
+{ /* add two sparse matrices (numeric phase) */
+ int i, j;
+ double *work;
+ /* allocate and clear the working array */
+ work = xcalloc(1+C->n, sizeof(double));
+ for (j = 1; j <= C->n; j++)
+ work[j] = 0.0;
+ /* compute matrix C = alfa * A + beta * B */
+ for (i = 1; i <= C->n; i++)
+ { SPME *e;
+ /* work := alfa * (i-th row of A) + beta * (i-th row of B) */
+ /* (note that A and/or B may have duplicate elements) */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ work[e->j] += alfa * e->val;
+ for (e = B->row[i]; e != NULL; e = e->r_next)
+ work[e->j] += beta * e->val;
+ /* (i-th row of C) := work, work := 0 */
+ for (e = C->row[i]; e != NULL; e = e->r_next)
+ { j = e->j;
+ e->val = work[j];
+ work[j] = 0.0;
+ }
+ }
+ /* check and deallocate the working array */
+ for (j = 1; j <= C->n; j++)
+ xassert(work[j] == 0.0);
+ xfree(work);
+ return;
+}
+
+SPM *spm_add_mat(double alfa, const SPM *A, double beta, const SPM *B)
+{ /* add two sparse matrices (driver routine) */
+ SPM *C;
+ C = spm_add_sym(A, B);
+ spm_add_num(C, alfa, A, beta, B);
+ return C;
+}
+
+SPM *spm_mul_sym(const SPM *A, const SPM *B)
+{ /* multiply two sparse matrices (symbolic phase) */
+ int i, j, k, *flag;
+ SPM *C;
+ xassert(A->n == B->m);
+ /* create resultant matrix */
+ C = spm_create_mat(A->m, B->n);
+ /* allocate and clear the flag array */
+ flag = xcalloc(1+C->n, sizeof(int));
+ for (j = 1; j <= C->n; j++)
+ flag[j] = 0;
+ /* compute pattern of C = A * B */
+ for (i = 1; i <= C->m; i++)
+ { SPME *e, *ee;
+ /* compute pattern of i-th row of C */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ { k = e->j;
+ for (ee = B->row[k]; ee != NULL; ee = ee->r_next)
+ { j = ee->j;
+ /* if a[i,k] != 0 and b[k,j] != 0 then c[i,j] != 0 */
+ if (!flag[j])
+ { /* c[i,j] does not exist, so create it */
+ spm_new_elem(C, i, j, 0.0);
+ flag[j] = 1;
+ }
+ }
+ }
+ /* reset the flag array */
+ for (e = C->row[i]; e != NULL; e = e->r_next)
+ flag[e->j] = 0;
+ }
+ /* check and deallocate the flag array */
+ for (j = 1; j <= C->n; j++)
+ xassert(!flag[j]);
+ xfree(flag);
+ return C;
+}
+
+void spm_mul_num(SPM *C, const SPM *A, const SPM *B)
+{ /* multiply two sparse matrices (numeric phase) */
+ int i, j;
+ double *work;
+ /* allocate and clear the working array */
+ work = xcalloc(1+A->n, sizeof(double));
+ for (j = 1; j <= A->n; j++)
+ work[j] = 0.0;
+ /* compute matrix C = A * B */
+ for (i = 1; i <= C->m; i++)
+ { SPME *e, *ee;
+ double temp;
+ /* work := (i-th row of A) */
+ /* (note that A may have duplicate elements) */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ work[e->j] += e->val;
+ /* compute i-th row of C */
+ for (e = C->row[i]; e != NULL; e = e->r_next)
+ { j = e->j;
+ /* c[i,j] := work * (j-th column of B) */
+ temp = 0.0;
+ for (ee = B->col[j]; ee != NULL; ee = ee->c_next)
+ temp += work[ee->i] * ee->val;
+ e->val = temp;
+ }
+ /* reset the working array */
+ for (e = A->row[i]; e != NULL; e = e->r_next)
+ work[e->j] = 0.0;
+ }
+ /* check and deallocate the working array */
+ for (j = 1; j <= A->n; j++)
+ xassert(work[j] == 0.0);
+ xfree(work);
+ return;
+}
+
+SPM *spm_mul_mat(const SPM *A, const SPM *B)
+{ /* multiply two sparse matrices (driver routine) */
+ SPM *C;
+ C = spm_mul_sym(A, B);
+ spm_mul_num(C, A, B);
+ return C;
+}
+
+PER *spm_create_per(int n)
+{ /* create permutation matrix */
+ PER *P;
+ int k;
+ xassert(n >= 0);
+ P = xmalloc(sizeof(PER));
+ P->n = n;
+ P->row = xcalloc(1+n, sizeof(int));
+ P->col = xcalloc(1+n, sizeof(int));
+ /* initially it is identity matrix */
+ for (k = 1; k <= n; k++)
+ P->row[k] = P->col[k] = k;
+ return P;
+}
+
+void spm_check_per(PER *P)
+{ /* check permutation matrix for correctness */
+ int i, j;
+ xassert(P->n >= 0);
+ for (i = 1; i <= P->n; i++)
+ { j = P->row[i];
+ xassert(1 <= j && j <= P->n);
+ xassert(P->col[j] == i);
+ }
+ return;
+}
+
+void spm_delete_per(PER *P)
+{ /* delete permutation matrix */
+ xfree(P->row);
+ xfree(P->col);
+ xfree(P);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpspm.h b/test/monniaux/glpk-4.65/src/draft/glpspm.h
new file mode 100644
index 00000000..eda9f98f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpspm.h
@@ -0,0 +1,165 @@
+/* glpspm.h (general sparse matrix) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPSPM_H
+#define GLPSPM_H
+
+#include "dmp.h"
+
+typedef struct SPM SPM;
+typedef struct SPME SPME;
+
+struct SPM
+{ /* general sparse matrix */
+ int m;
+ /* number of rows, m >= 0 */
+ int n;
+ /* number of columns, n >= 0 */
+ DMP *pool;
+ /* memory pool to store matrix elements */
+ SPME **row; /* SPME *row[1+m]; */
+ /* row[i], 1 <= i <= m, is a pointer to i-th row list */
+ SPME **col; /* SPME *col[1+n]; */
+ /* col[j], 1 <= j <= n, is a pointer to j-th column list */
+};
+
+struct SPME
+{ /* sparse matrix element */
+ int i;
+ /* row number */
+ int j;
+ /* column number */
+ double val;
+ /* element value */
+ SPME *r_prev;
+ /* pointer to previous element in the same row */
+ SPME *r_next;
+ /* pointer to next element in the same row */
+ SPME *c_prev;
+ /* pointer to previous element in the same column */
+ SPME *c_next;
+ /* pointer to next element in the same column */
+};
+
+typedef struct PER PER;
+
+struct PER
+{ /* permutation matrix */
+ int n;
+ /* matrix order, n >= 0 */
+ int *row; /* int row[1+n]; */
+ /* row[i] = j means p[i,j] = 1 */
+ int *col; /* int col[1+n]; */
+ /* col[j] = i means p[i,j] = 1 */
+};
+
+#define spm_create_mat _glp_spm_create_mat
+SPM *spm_create_mat(int m, int n);
+/* create general sparse matrix */
+
+#define spm_new_elem _glp_spm_new_elem
+SPME *spm_new_elem(SPM *A, int i, int j, double val);
+/* add new element to sparse matrix */
+
+#define spm_delete_mat _glp_spm_delete_mat
+void spm_delete_mat(SPM *A);
+/* delete general sparse matrix */
+
+#define spm_test_mat_e _glp_spm_test_mat_e
+SPM *spm_test_mat_e(int n, int c);
+/* create test sparse matrix of E(n,c) class */
+
+#define spm_test_mat_d _glp_spm_test_mat_d
+SPM *spm_test_mat_d(int n, int c);
+/* create test sparse matrix of D(n,c) class */
+
+#define spm_show_mat _glp_spm_show_mat
+int spm_show_mat(const SPM *A, const char *fname);
+/* write sparse matrix pattern in BMP file format */
+
+#define spm_read_hbm _glp_spm_read_hbm
+SPM *spm_read_hbm(const char *fname);
+/* read sparse matrix in Harwell-Boeing format */
+
+#define spm_count_nnz _glp_spm_count_nnz
+int spm_count_nnz(const SPM *A);
+/* determine number of non-zeros in sparse matrix */
+
+#define spm_drop_zeros _glp_spm_drop_zeros
+int spm_drop_zeros(SPM *A, double eps);
+/* remove zero elements from sparse matrix */
+
+#define spm_read_mat _glp_spm_read_mat
+SPM *spm_read_mat(const char *fname);
+/* read sparse matrix from text file */
+
+#define spm_write_mat _glp_spm_write_mat
+int spm_write_mat(const SPM *A, const char *fname);
+/* write sparse matrix to text file */
+
+#define spm_transpose _glp_spm_transpose
+SPM *spm_transpose(const SPM *A);
+/* transpose sparse matrix */
+
+#define spm_add_sym _glp_spm_add_sym
+SPM *spm_add_sym(const SPM *A, const SPM *B);
+/* add two sparse matrices (symbolic phase) */
+
+#define spm_add_num _glp_spm_add_num
+void spm_add_num(SPM *C, double alfa, const SPM *A, double beta,
+ const SPM *B);
+/* add two sparse matrices (numeric phase) */
+
+#define spm_add_mat _glp_spm_add_mat
+SPM *spm_add_mat(double alfa, const SPM *A, double beta,
+ const SPM *B);
+/* add two sparse matrices (driver routine) */
+
+#define spm_mul_sym _glp_spm_mul_sym
+SPM *spm_mul_sym(const SPM *A, const SPM *B);
+/* multiply two sparse matrices (symbolic phase) */
+
+#define spm_mul_num _glp_spm_mul_num
+void spm_mul_num(SPM *C, const SPM *A, const SPM *B);
+/* multiply two sparse matrices (numeric phase) */
+
+#define spm_mul_mat _glp_spm_mul_mat
+SPM *spm_mul_mat(const SPM *A, const SPM *B);
+/* multiply two sparse matrices (driver routine) */
+
+#define spm_create_per _glp_spm_create_per
+PER *spm_create_per(int n);
+/* create permutation matrix */
+
+#define spm_check_per _glp_spm_check_per
+void spm_check_per(PER *P);
+/* check permutation matrix for correctness */
+
+#define spm_delete_per _glp_spm_delete_per
+void spm_delete_per(PER *P);
+/* delete permutation matrix */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpssx.h b/test/monniaux/glpk-4.65/src/draft/glpssx.h
new file mode 100644
index 00000000..3b52b3cc
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpssx.h
@@ -0,0 +1,437 @@
+/* glpssx.h (simplex method, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPSSX_H
+#define GLPSSX_H
+
+#include "bfx.h"
+#include "env.h"
+#if 1 /* 25/XI-2017 */
+#include "glpk.h"
+#endif
+
+typedef struct SSX SSX;
+
+struct SSX
+{ /* simplex solver workspace */
+/*----------------------------------------------------------------------
+// LP PROBLEM DATA
+//
+// It is assumed that LP problem has the following statement:
+//
+// minimize (or maximize)
+//
+// z = c[1]*x[1] + ... + c[m+n]*x[m+n] + c[0] (1)
+//
+// subject to equality constraints
+//
+// x[1] - a[1,1]*x[m+1] - ... - a[1,n]*x[m+n] = 0
+//
+// . . . . . . . (2)
+//
+// x[m] - a[m,1]*x[m+1] + ... - a[m,n]*x[m+n] = 0
+//
+// and bounds of variables
+//
+// l[1] <= x[1] <= u[1]
+//
+// . . . . . . . (3)
+//
+// l[m+n] <= x[m+n] <= u[m+n]
+//
+// where:
+// x[1], ..., x[m] - auxiliary variables;
+// x[m+1], ..., x[m+n] - structural variables;
+// z - objective function;
+// c[1], ..., c[m+n] - coefficients of the objective function;
+// c[0] - constant term of the objective function;
+// a[1,1], ..., a[m,n] - constraint coefficients;
+// l[1], ..., l[m+n] - lower bounds of variables;
+// u[1], ..., u[m+n] - upper bounds of variables.
+//
+// Bounds of variables can be finite as well as inifinite. Besides,
+// lower and upper bounds can be equal to each other. So the following
+// five types of variables are possible:
+//
+// Bounds of variable Type of variable
+// -------------------------------------------------
+// -inf < x[k] < +inf Free (unbounded) variable
+// l[k] <= x[k] < +inf Variable with lower bound
+// -inf < x[k] <= u[k] Variable with upper bound
+// l[k] <= x[k] <= u[k] Double-bounded variable
+// l[k] = x[k] = u[k] Fixed variable
+//
+// Using vector-matrix notations the LP problem (1)-(3) can be written
+// as follows:
+//
+// minimize (or maximize)
+//
+// z = c * x + c[0] (4)
+//
+// subject to equality constraints
+//
+// xR - A * xS = 0 (5)
+//
+// and bounds of variables
+//
+// l <= x <= u (6)
+//
+// where:
+// xR - vector of auxiliary variables;
+// xS - vector of structural variables;
+// x = (xR, xS) - vector of all variables;
+// z - objective function;
+// c - vector of objective coefficients;
+// c[0] - constant term of the objective function;
+// A - matrix of constraint coefficients (has m rows
+// and n columns);
+// l - vector of lower bounds of variables;
+// u - vector of upper bounds of variables.
+//
+// The simplex method makes no difference between auxiliary and
+// structural variables, so it is convenient to think the system of
+// equality constraints (5) written in a homogeneous form:
+//
+// (I | -A) * x = 0, (7)
+//
+// where (I | -A) is an augmented (m+n)xm constraint matrix, I is mxm
+// unity matrix whose columns correspond to auxiliary variables, and A
+// is the original mxn constraint matrix whose columns correspond to
+// structural variables. Note that only the matrix A is stored.
+----------------------------------------------------------------------*/
+ int m;
+ /* number of rows (auxiliary variables), m > 0 */
+ int n;
+ /* number of columns (structural variables), n > 0 */
+ int *type; /* int type[1+m+n]; */
+ /* type[0] is not used;
+ type[k], 1 <= k <= m+n, is the type of variable x[k]: */
+#define SSX_FR 0 /* free (unbounded) variable */
+#define SSX_LO 1 /* variable with lower bound */
+#define SSX_UP 2 /* variable with upper bound */
+#define SSX_DB 3 /* double-bounded variable */
+#define SSX_FX 4 /* fixed variable */
+ mpq_t *lb; /* mpq_t lb[1+m+n]; alias: l */
+ /* lb[0] is not used;
+ lb[k], 1 <= k <= m+n, is an lower bound of variable x[k];
+ if x[k] has no lower bound, lb[k] is zero */
+ mpq_t *ub; /* mpq_t ub[1+m+n]; alias: u */
+ /* ub[0] is not used;
+ ub[k], 1 <= k <= m+n, is an upper bound of variable x[k];
+ if x[k] has no upper bound, ub[k] is zero;
+ if x[k] is of fixed type, ub[k] is equal to lb[k] */
+ int dir;
+ /* optimization direction (sense of the objective function): */
+#define SSX_MIN 0 /* minimization */
+#define SSX_MAX 1 /* maximization */
+ mpq_t *coef; /* mpq_t coef[1+m+n]; alias: c */
+ /* coef[0] is a constant term of the objective function;
+ coef[k], 1 <= k <= m+n, is a coefficient of the objective
+ function at variable x[k];
+ note that auxiliary variables also may have non-zero objective
+ coefficients */
+ int *A_ptr; /* int A_ptr[1+n+1]; */
+ int *A_ind; /* int A_ind[A_ptr[n+1]]; */
+ mpq_t *A_val; /* mpq_t A_val[A_ptr[n+1]]; */
+ /* constraint matrix A (see (5)) in storage-by-columns format */
+/*----------------------------------------------------------------------
+// LP BASIS AND CURRENT BASIC SOLUTION
+//
+// The LP basis is defined by the following partition of the augmented
+// constraint matrix (7):
+//
+// (B | N) = (I | -A) * Q, (8)
+//
+// where B is a mxm non-singular basis matrix whose columns correspond
+// to basic variables xB, N is a mxn matrix whose columns correspond to
+// non-basic variables xN, and Q is a permutation (m+n)x(m+n) matrix.
+//
+// From (7) and (8) it follows that
+//
+// (I | -A) * x = (I | -A) * Q * Q' * x = (B | N) * (xB, xN),
+//
+// therefore
+//
+// (xB, xN) = Q' * x, (9)
+//
+// where x is the vector of all variables in the original order, xB is
+// a vector of basic variables, xN is a vector of non-basic variables,
+// Q' = inv(Q) is a matrix transposed to Q.
+//
+// Current values of non-basic variables xN[j], j = 1, ..., n, are not
+// stored; they are defined implicitly by their statuses as follows:
+//
+// 0, if xN[j] is free variable
+// lN[j], if xN[j] is on its lower bound (10)
+// uN[j], if xN[j] is on its upper bound
+// lN[j] = uN[j], if xN[j] is fixed variable
+//
+// where lN[j] and uN[j] are lower and upper bounds of xN[j].
+//
+// Current values of basic variables xB[i], i = 1, ..., m, are computed
+// as follows:
+//
+// beta = - inv(B) * N * xN, (11)
+//
+// where current values of xN are defined by (10).
+//
+// Current values of simplex multipliers pi[i], i = 1, ..., m (which
+// are values of Lagrange multipliers for equality constraints (7) also
+// called shadow prices) are computed as follows:
+//
+// pi = inv(B') * cB, (12)
+//
+// where B' is a matrix transposed to B, cB is a vector of objective
+// coefficients at basic variables xB.
+//
+// Current values of reduced costs d[j], j = 1, ..., n, (which are
+// values of Langrange multipliers for active inequality constraints
+// corresponding to non-basic variables) are computed as follows:
+//
+// d = cN - N' * pi, (13)
+//
+// where N' is a matrix transposed to N, cN is a vector of objective
+// coefficients at non-basic variables xN.
+----------------------------------------------------------------------*/
+ int *stat; /* int stat[1+m+n]; */
+ /* stat[0] is not used;
+ stat[k], 1 <= k <= m+n, is the status of variable x[k]: */
+#define SSX_BS 0 /* basic variable */
+#define SSX_NL 1 /* non-basic variable on lower bound */
+#define SSX_NU 2 /* non-basic variable on upper bound */
+#define SSX_NF 3 /* non-basic free variable */
+#define SSX_NS 4 /* non-basic fixed variable */
+ int *Q_row; /* int Q_row[1+m+n]; */
+ /* matrix Q in row-like format;
+ Q_row[0] is not used;
+ Q_row[i] = j means that q[i,j] = 1 */
+ int *Q_col; /* int Q_col[1+m+n]; */
+ /* matrix Q in column-like format;
+ Q_col[0] is not used;
+ Q_col[j] = i means that q[i,j] = 1 */
+ /* if k-th column of the matrix (I | A) is k'-th column of the
+ matrix (B | N), then Q_row[k] = k' and Q_col[k'] = k;
+ if x[k] is xB[i], then Q_row[k] = i and Q_col[i] = k;
+ if x[k] is xN[j], then Q_row[k] = m+j and Q_col[m+j] = k */
+ BFX *binv;
+ /* invertable form of the basis matrix B */
+ mpq_t *bbar; /* mpq_t bbar[1+m]; alias: beta */
+ /* bbar[0] is a value of the objective function;
+ bbar[i], 1 <= i <= m, is a value of basic variable xB[i] */
+ mpq_t *pi; /* mpq_t pi[1+m]; */
+ /* pi[0] is not used;
+ pi[i], 1 <= i <= m, is a simplex multiplier corresponding to
+ i-th row (equality constraint) */
+ mpq_t *cbar; /* mpq_t cbar[1+n]; alias: d */
+ /* cbar[0] is not used;
+ cbar[j], 1 <= j <= n, is a reduced cost of non-basic variable
+ xN[j] */
+/*----------------------------------------------------------------------
+// SIMPLEX TABLE
+//
+// Due to (8) and (9) the system of equality constraints (7) for the
+// current basis can be written as follows:
+//
+// xB = A~ * xN, (14)
+//
+// where
+//
+// A~ = - inv(B) * N (15)
+//
+// is a mxn matrix called the simplex table.
+//
+// The revised simplex method uses only two components of A~, namely,
+// pivot column corresponding to non-basic variable xN[q] chosen to
+// enter the basis, and pivot row corresponding to basic variable xB[p]
+// chosen to leave the basis.
+//
+// Pivot column alfa_q is q-th column of A~, so
+//
+// alfa_q = A~ * e[q] = - inv(B) * N * e[q] = - inv(B) * N[q], (16)
+//
+// where N[q] is q-th column of the matrix N.
+//
+// Pivot row alfa_p is p-th row of A~ or, equivalently, p-th column of
+// A~', a matrix transposed to A~, so
+//
+// alfa_p = A~' * e[p] = - N' * inv(B') * e[p] = - N' * rho_p, (17)
+//
+// where (*)' means transposition, and
+//
+// rho_p = inv(B') * e[p], (18)
+//
+// is p-th column of inv(B') or, that is the same, p-th row of inv(B).
+----------------------------------------------------------------------*/
+ int p;
+ /* number of basic variable xB[p], 1 <= p <= m, chosen to leave
+ the basis */
+ mpq_t *rho; /* mpq_t rho[1+m]; */
+ /* p-th row of the inverse inv(B); see (18) */
+ mpq_t *ap; /* mpq_t ap[1+n]; */
+ /* p-th row of the simplex table; see (17) */
+ int q;
+ /* number of non-basic variable xN[q], 1 <= q <= n, chosen to
+ enter the basis */
+ mpq_t *aq; /* mpq_t aq[1+m]; */
+ /* q-th column of the simplex table; see (16) */
+/*--------------------------------------------------------------------*/
+ int q_dir;
+ /* direction in which non-basic variable xN[q] should change on
+ moving to the adjacent vertex of the polyhedron:
+ +1 means that xN[q] increases
+ -1 means that xN[q] decreases */
+ int p_stat;
+ /* non-basic status which should be assigned to basic variable
+ xB[p] when it has left the basis and become xN[q] */
+ mpq_t delta;
+ /* actual change of xN[q] in the adjacent basis (it has the same
+ sign as q_dir) */
+/*--------------------------------------------------------------------*/
+#if 1 /* 25/XI-2017 */
+ int msg_lev;
+ /* verbosity level:
+ GLP_MSG_OFF no output
+ GLP_MSG_ERR report errors and warnings
+ GLP_MSG_ON normal output
+ GLP_MSG_ALL highest verbosity */
+#endif
+ int it_lim;
+ /* simplex iterations limit; if this value is positive, it is
+ decreased by one each time when one simplex iteration has been
+ performed, and reaching zero value signals the solver to stop
+ the search; negative value means no iterations limit */
+ int it_cnt;
+ /* simplex iterations count; this count is increased by one each
+ time when one simplex iteration has been performed */
+ double tm_lim;
+ /* searching time limit, in seconds; if this value is positive,
+ it is decreased each time when one simplex iteration has been
+ performed by the amount of time spent for the iteration, and
+ reaching zero value signals the solver to stop the search;
+ negative value means no time limit */
+ double out_frq;
+ /* output frequency, in seconds; this parameter specifies how
+ frequently the solver sends information about the progress of
+ the search to the standard output */
+#if 0 /* 10/VI-2013 */
+ glp_long tm_beg;
+#else
+ double tm_beg;
+#endif
+ /* starting time of the search, in seconds; the total time of the
+ search is the difference between xtime() and tm_beg */
+#if 0 /* 10/VI-2013 */
+ glp_long tm_lag;
+#else
+ double tm_lag;
+#endif
+ /* the most recent time, in seconds, at which the progress of the
+ the search was displayed */
+};
+
+#define ssx_create _glp_ssx_create
+#define ssx_factorize _glp_ssx_factorize
+#define ssx_get_xNj _glp_ssx_get_xNj
+#define ssx_eval_bbar _glp_ssx_eval_bbar
+#define ssx_eval_pi _glp_ssx_eval_pi
+#define ssx_eval_dj _glp_ssx_eval_dj
+#define ssx_eval_cbar _glp_ssx_eval_cbar
+#define ssx_eval_rho _glp_ssx_eval_rho
+#define ssx_eval_row _glp_ssx_eval_row
+#define ssx_eval_col _glp_ssx_eval_col
+#define ssx_chuzc _glp_ssx_chuzc
+#define ssx_chuzr _glp_ssx_chuzr
+#define ssx_update_bbar _glp_ssx_update_bbar
+#define ssx_update_pi _glp_ssx_update_pi
+#define ssx_update_cbar _glp_ssx_update_cbar
+#define ssx_change_basis _glp_ssx_change_basis
+#define ssx_delete _glp_ssx_delete
+
+#define ssx_phase_I _glp_ssx_phase_I
+#define ssx_phase_II _glp_ssx_phase_II
+#define ssx_driver _glp_ssx_driver
+
+SSX *ssx_create(int m, int n, int nnz);
+/* create simplex solver workspace */
+
+int ssx_factorize(SSX *ssx);
+/* factorize the current basis matrix */
+
+void ssx_get_xNj(SSX *ssx, int j, mpq_t x);
+/* determine value of non-basic variable */
+
+void ssx_eval_bbar(SSX *ssx);
+/* compute values of basic variables */
+
+void ssx_eval_pi(SSX *ssx);
+/* compute values of simplex multipliers */
+
+void ssx_eval_dj(SSX *ssx, int j, mpq_t dj);
+/* compute reduced cost of non-basic variable */
+
+void ssx_eval_cbar(SSX *ssx);
+/* compute reduced costs of all non-basic variables */
+
+void ssx_eval_rho(SSX *ssx);
+/* compute p-th row of the inverse */
+
+void ssx_eval_row(SSX *ssx);
+/* compute pivot row of the simplex table */
+
+void ssx_eval_col(SSX *ssx);
+/* compute pivot column of the simplex table */
+
+void ssx_chuzc(SSX *ssx);
+/* choose pivot column */
+
+void ssx_chuzr(SSX *ssx);
+/* choose pivot row */
+
+void ssx_update_bbar(SSX *ssx);
+/* update values of basic variables */
+
+void ssx_update_pi(SSX *ssx);
+/* update simplex multipliers */
+
+void ssx_update_cbar(SSX *ssx);
+/* update reduced costs of non-basic variables */
+
+void ssx_change_basis(SSX *ssx);
+/* change current basis to adjacent one */
+
+void ssx_delete(SSX *ssx);
+/* delete simplex solver workspace */
+
+int ssx_phase_I(SSX *ssx);
+/* find primal feasible solution */
+
+int ssx_phase_II(SSX *ssx);
+/* find optimal solution */
+
+int ssx_driver(SSX *ssx);
+/* base driver to exact simplex method */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpssx01.c b/test/monniaux/glpk-4.65/src/draft/glpssx01.c
new file mode 100644
index 00000000..9b70444e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpssx01.c
@@ -0,0 +1,839 @@
+/* glpssx01.c (simplex method, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpssx.h"
+#define xfault xerror
+
+/*----------------------------------------------------------------------
+// ssx_create - create simplex solver workspace.
+//
+// This routine creates the workspace used by simplex solver routines,
+// and returns a pointer to it.
+//
+// Parameters m, n, and nnz specify, respectively, the number of rows,
+// columns, and non-zero constraint coefficients.
+//
+// This routine only allocates the memory for the workspace components,
+// so the workspace needs to be saturated by data. */
+
+SSX *ssx_create(int m, int n, int nnz)
+{ SSX *ssx;
+ int i, j, k;
+ if (m < 1)
+ xfault("ssx_create: m = %d; invalid number of rows\n", m);
+ if (n < 1)
+ xfault("ssx_create: n = %d; invalid number of columns\n", n);
+ if (nnz < 0)
+ xfault("ssx_create: nnz = %d; invalid number of non-zero const"
+ "raint coefficients\n", nnz);
+ ssx = xmalloc(sizeof(SSX));
+ ssx->m = m;
+ ssx->n = n;
+ ssx->type = xcalloc(1+m+n, sizeof(int));
+ ssx->lb = xcalloc(1+m+n, sizeof(mpq_t));
+ for (k = 1; k <= m+n; k++) mpq_init(ssx->lb[k]);
+ ssx->ub = xcalloc(1+m+n, sizeof(mpq_t));
+ for (k = 1; k <= m+n; k++) mpq_init(ssx->ub[k]);
+ ssx->coef = xcalloc(1+m+n, sizeof(mpq_t));
+ for (k = 0; k <= m+n; k++) mpq_init(ssx->coef[k]);
+ ssx->A_ptr = xcalloc(1+n+1, sizeof(int));
+ ssx->A_ptr[n+1] = nnz+1;
+ ssx->A_ind = xcalloc(1+nnz, sizeof(int));
+ ssx->A_val = xcalloc(1+nnz, sizeof(mpq_t));
+ for (k = 1; k <= nnz; k++) mpq_init(ssx->A_val[k]);
+ ssx->stat = xcalloc(1+m+n, sizeof(int));
+ ssx->Q_row = xcalloc(1+m+n, sizeof(int));
+ ssx->Q_col = xcalloc(1+m+n, sizeof(int));
+ ssx->binv = bfx_create_binv();
+ ssx->bbar = xcalloc(1+m, sizeof(mpq_t));
+ for (i = 0; i <= m; i++) mpq_init(ssx->bbar[i]);
+ ssx->pi = xcalloc(1+m, sizeof(mpq_t));
+ for (i = 1; i <= m; i++) mpq_init(ssx->pi[i]);
+ ssx->cbar = xcalloc(1+n, sizeof(mpq_t));
+ for (j = 1; j <= n; j++) mpq_init(ssx->cbar[j]);
+ ssx->rho = xcalloc(1+m, sizeof(mpq_t));
+ for (i = 1; i <= m; i++) mpq_init(ssx->rho[i]);
+ ssx->ap = xcalloc(1+n, sizeof(mpq_t));
+ for (j = 1; j <= n; j++) mpq_init(ssx->ap[j]);
+ ssx->aq = xcalloc(1+m, sizeof(mpq_t));
+ for (i = 1; i <= m; i++) mpq_init(ssx->aq[i]);
+ mpq_init(ssx->delta);
+ return ssx;
+}
+
+/*----------------------------------------------------------------------
+// ssx_factorize - factorize the current basis matrix.
+//
+// This routine computes factorization of the current basis matrix B
+// and returns the singularity flag. If the matrix B is non-singular,
+// the flag is zero, otherwise non-zero. */
+
+static int basis_col(void *info, int j, int ind[], mpq_t val[])
+{ /* this auxiliary routine provides row indices and numeric values
+ of non-zero elements in j-th column of the matrix B */
+ SSX *ssx = info;
+ int m = ssx->m;
+ int n = ssx->n;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ int k, len, ptr;
+ xassert(1 <= j && j <= m);
+ k = Q_col[j]; /* x[k] = xB[j] */
+ xassert(1 <= k && k <= m+n);
+ /* j-th column of the matrix B is k-th column of the augmented
+ constraint matrix (I | -A) */
+ if (k <= m)
+ { /* it is a column of the unity matrix I */
+ len = 1, ind[1] = k, mpq_set_si(val[1], 1, 1);
+ }
+ else
+ { /* it is a column of the original constraint matrix -A */
+ len = 0;
+ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
+ { len++;
+ ind[len] = A_ind[ptr];
+ mpq_neg(val[len], A_val[ptr]);
+ }
+ }
+ return len;
+}
+
+int ssx_factorize(SSX *ssx)
+{ int ret;
+ ret = bfx_factorize(ssx->binv, ssx->m, basis_col, ssx);
+ return ret;
+}
+
+/*----------------------------------------------------------------------
+// ssx_get_xNj - determine value of non-basic variable.
+//
+// This routine determines the value of non-basic variable xN[j] in the
+// current basic solution defined as follows:
+//
+// 0, if xN[j] is free variable
+// lN[j], if xN[j] is on its lower bound
+// uN[j], if xN[j] is on its upper bound
+// lN[j] = uN[j], if xN[j] is fixed variable
+//
+// where lN[j] and uN[j] are lower and upper bounds of xN[j]. */
+
+void ssx_get_xNj(SSX *ssx, int j, mpq_t x)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *lb = ssx->lb;
+ mpq_t *ub = ssx->ub;
+ int *stat = ssx->stat;
+ int *Q_col = ssx->Q_col;
+ int k;
+ xassert(1 <= j && j <= n);
+ k = Q_col[m+j]; /* x[k] = xN[j] */
+ xassert(1 <= k && k <= m+n);
+ switch (stat[k])
+ { case SSX_NL:
+ /* xN[j] is on its lower bound */
+ mpq_set(x, lb[k]); break;
+ case SSX_NU:
+ /* xN[j] is on its upper bound */
+ mpq_set(x, ub[k]); break;
+ case SSX_NF:
+ /* xN[j] is free variable */
+ mpq_set_si(x, 0, 1); break;
+ case SSX_NS:
+ /* xN[j] is fixed variable */
+ mpq_set(x, lb[k]); break;
+ default:
+ xassert(stat != stat);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_bbar - compute values of basic variables.
+//
+// This routine computes values of basic variables xB in the current
+// basic solution as follows:
+//
+// beta = - inv(B) * N * xN,
+//
+// where B is the basis matrix, N is the matrix of non-basic columns,
+// xN is a vector of current values of non-basic variables. */
+
+void ssx_eval_bbar(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *coef = ssx->coef;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ mpq_t *bbar = ssx->bbar;
+ int i, j, k, ptr;
+ mpq_t x, temp;
+ mpq_init(x);
+ mpq_init(temp);
+ /* bbar := 0 */
+ for (i = 1; i <= m; i++)
+ mpq_set_si(bbar[i], 0, 1);
+ /* bbar := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n] */
+ for (j = 1; j <= n; j++)
+ { ssx_get_xNj(ssx, j, x);
+ if (mpq_sgn(x) == 0) continue;
+ k = Q_col[m+j]; /* x[k] = xN[j] */
+ if (k <= m)
+ { /* N[j] is a column of the unity matrix I */
+ mpq_sub(bbar[k], bbar[k], x);
+ }
+ else
+ { /* N[j] is a column of the original constraint matrix -A */
+ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
+ { mpq_mul(temp, A_val[ptr], x);
+ mpq_add(bbar[A_ind[ptr]], bbar[A_ind[ptr]], temp);
+ }
+ }
+ }
+ /* bbar := inv(B) * bbar */
+ bfx_ftran(ssx->binv, bbar, 0);
+#if 1
+ /* compute value of the objective function */
+ /* bbar[0] := c[0] */
+ mpq_set(bbar[0], coef[0]);
+ /* bbar[0] := bbar[0] + sum{i in B} cB[i] * xB[i] */
+ for (i = 1; i <= m; i++)
+ { k = Q_col[i]; /* x[k] = xB[i] */
+ if (mpq_sgn(coef[k]) == 0) continue;
+ mpq_mul(temp, coef[k], bbar[i]);
+ mpq_add(bbar[0], bbar[0], temp);
+ }
+ /* bbar[0] := bbar[0] + sum{j in N} cN[j] * xN[j] */
+ for (j = 1; j <= n; j++)
+ { k = Q_col[m+j]; /* x[k] = xN[j] */
+ if (mpq_sgn(coef[k]) == 0) continue;
+ ssx_get_xNj(ssx, j, x);
+ mpq_mul(temp, coef[k], x);
+ mpq_add(bbar[0], bbar[0], temp);
+ }
+#endif
+ mpq_clear(x);
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_pi - compute values of simplex multipliers.
+//
+// This routine computes values of simplex multipliers (shadow prices)
+// pi in the current basic solution as follows:
+//
+// pi = inv(B') * cB,
+//
+// where B' is a matrix transposed to the basis matrix B, cB is a vector
+// of objective coefficients at basic variables xB. */
+
+void ssx_eval_pi(SSX *ssx)
+{ int m = ssx->m;
+ mpq_t *coef = ssx->coef;
+ int *Q_col = ssx->Q_col;
+ mpq_t *pi = ssx->pi;
+ int i;
+ /* pi := cB */
+ for (i = 1; i <= m; i++) mpq_set(pi[i], coef[Q_col[i]]);
+ /* pi := inv(B') * cB */
+ bfx_btran(ssx->binv, pi);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_dj - compute reduced cost of non-basic variable.
+//
+// This routine computes reduced cost d[j] of non-basic variable xN[j]
+// in the current basic solution as follows:
+//
+// d[j] = cN[j] - N[j] * pi,
+//
+// where cN[j] is an objective coefficient at xN[j], N[j] is a column
+// of the augmented constraint matrix (I | -A) corresponding to xN[j],
+// pi is the vector of simplex multipliers (shadow prices). */
+
+void ssx_eval_dj(SSX *ssx, int j, mpq_t dj)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *coef = ssx->coef;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ mpq_t *pi = ssx->pi;
+ int k, ptr, end;
+ mpq_t temp;
+ mpq_init(temp);
+ xassert(1 <= j && j <= n);
+ k = Q_col[m+j]; /* x[k] = xN[j] */
+ xassert(1 <= k && k <= m+n);
+ /* j-th column of the matrix N is k-th column of the augmented
+ constraint matrix (I | -A) */
+ if (k <= m)
+ { /* it is a column of the unity matrix I */
+ mpq_sub(dj, coef[k], pi[k]);
+ }
+ else
+ { /* it is a column of the original constraint matrix -A */
+ mpq_set(dj, coef[k]);
+ for (ptr = A_ptr[k-m], end = A_ptr[k-m+1]; ptr < end; ptr++)
+ { mpq_mul(temp, A_val[ptr], pi[A_ind[ptr]]);
+ mpq_add(dj, dj, temp);
+ }
+ }
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_cbar - compute reduced costs of all non-basic variables.
+//
+// This routine computes the vector of reduced costs pi in the current
+// basic solution for all non-basic variables, including fixed ones. */
+
+void ssx_eval_cbar(SSX *ssx)
+{ int n = ssx->n;
+ mpq_t *cbar = ssx->cbar;
+ int j;
+ for (j = 1; j <= n; j++)
+ ssx_eval_dj(ssx, j, cbar[j]);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_rho - compute p-th row of the inverse.
+//
+// This routine computes p-th row of the matrix inv(B), where B is the
+// current basis matrix.
+//
+// p-th row of the inverse is computed using the following formula:
+//
+// rho = inv(B') * e[p],
+//
+// where B' is a matrix transposed to B, e[p] is a unity vector, which
+// contains one in p-th position. */
+
+void ssx_eval_rho(SSX *ssx)
+{ int m = ssx->m;
+ int p = ssx->p;
+ mpq_t *rho = ssx->rho;
+ int i;
+ xassert(1 <= p && p <= m);
+ /* rho := 0 */
+ for (i = 1; i <= m; i++) mpq_set_si(rho[i], 0, 1);
+ /* rho := e[p] */
+ mpq_set_si(rho[p], 1, 1);
+ /* rho := inv(B') * rho */
+ bfx_btran(ssx->binv, rho);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_row - compute pivot row of the simplex table.
+//
+// This routine computes p-th (pivot) row of the current simplex table
+// A~ = - inv(B) * N using the following formula:
+//
+// A~[p] = - N' * inv(B') * e[p] = - N' * rho[p],
+//
+// where N' is a matrix transposed to the matrix N, rho[p] is p-th row
+// of the inverse inv(B). */
+
+void ssx_eval_row(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ mpq_t *rho = ssx->rho;
+ mpq_t *ap = ssx->ap;
+ int j, k, ptr;
+ mpq_t temp;
+ mpq_init(temp);
+ for (j = 1; j <= n; j++)
+ { /* ap[j] := - N'[j] * rho (inner product) */
+ k = Q_col[m+j]; /* x[k] = xN[j] */
+ if (k <= m)
+ mpq_neg(ap[j], rho[k]);
+ else
+ { mpq_set_si(ap[j], 0, 1);
+ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
+ { mpq_mul(temp, A_val[ptr], rho[A_ind[ptr]]);
+ mpq_add(ap[j], ap[j], temp);
+ }
+ }
+ }
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_eval_col - compute pivot column of the simplex table.
+//
+// This routine computes q-th (pivot) column of the current simplex
+// table A~ = - inv(B) * N using the following formula:
+//
+// A~[q] = - inv(B) * N[q],
+//
+// where N[q] is q-th column of the matrix N corresponding to chosen
+// non-basic variable xN[q]. */
+
+void ssx_eval_col(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ int q = ssx->q;
+ mpq_t *aq = ssx->aq;
+ int i, k, ptr;
+ xassert(1 <= q && q <= n);
+ /* aq := 0 */
+ for (i = 1; i <= m; i++) mpq_set_si(aq[i], 0, 1);
+ /* aq := N[q] */
+ k = Q_col[m+q]; /* x[k] = xN[q] */
+ if (k <= m)
+ { /* N[q] is a column of the unity matrix I */
+ mpq_set_si(aq[k], 1, 1);
+ }
+ else
+ { /* N[q] is a column of the original constraint matrix -A */
+ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
+ mpq_neg(aq[A_ind[ptr]], A_val[ptr]);
+ }
+ /* aq := inv(B) * aq */
+ bfx_ftran(ssx->binv, aq, 1);
+ /* aq := - aq */
+ for (i = 1; i <= m; i++) mpq_neg(aq[i], aq[i]);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_chuzc - choose pivot column.
+//
+// This routine chooses non-basic variable xN[q] whose reduced cost
+// indicates possible improving of the objective function to enter it
+// in the basis.
+//
+// Currently the standard (textbook) pricing is used, i.e. that
+// non-basic variable is preferred which has greatest reduced cost (in
+// magnitude).
+//
+// If xN[q] has been chosen, the routine stores its number q and also
+// sets the flag q_dir that indicates direction in which xN[q] has to
+// change (+1 means increasing, -1 means decreasing).
+//
+// If the choice cannot be made, because the current basic solution is
+// dual feasible, the routine sets the number q to 0. */
+
+void ssx_chuzc(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int dir = (ssx->dir == SSX_MIN ? +1 : -1);
+ int *Q_col = ssx->Q_col;
+ int *stat = ssx->stat;
+ mpq_t *cbar = ssx->cbar;
+ int j, k, s, q, q_dir;
+ double best, temp;
+ /* nothing is chosen so far */
+ q = 0, q_dir = 0, best = 0.0;
+ /* look through the list of non-basic variables */
+ for (j = 1; j <= n; j++)
+ { k = Q_col[m+j]; /* x[k] = xN[j] */
+ s = dir * mpq_sgn(cbar[j]);
+ if ((stat[k] == SSX_NF || stat[k] == SSX_NL) && s < 0 ||
+ (stat[k] == SSX_NF || stat[k] == SSX_NU) && s > 0)
+ { /* reduced cost of xN[j] indicates possible improving of
+ the objective function */
+ temp = fabs(mpq_get_d(cbar[j]));
+ xassert(temp != 0.0);
+ if (q == 0 || best < temp)
+ q = j, q_dir = - s, best = temp;
+ }
+ }
+ ssx->q = q, ssx->q_dir = q_dir;
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_chuzr - choose pivot row.
+//
+// This routine looks through elements of q-th column of the simplex
+// table and chooses basic variable xB[p] which should leave the basis.
+//
+// The choice is based on the standard (textbook) ratio test.
+//
+// If xB[p] has been chosen, the routine stores its number p and also
+// sets its non-basic status p_stat which should be assigned to xB[p]
+// when it has left the basis and become xN[q].
+//
+// Special case p < 0 means that xN[q] is double-bounded variable and
+// it reaches its opposite bound before any basic variable does that,
+// so the current basis remains unchanged.
+//
+// If the choice cannot be made, because xN[q] can infinitely change in
+// the feasible direction, the routine sets the number p to 0. */
+
+void ssx_chuzr(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int *type = ssx->type;
+ mpq_t *lb = ssx->lb;
+ mpq_t *ub = ssx->ub;
+ int *Q_col = ssx->Q_col;
+ mpq_t *bbar = ssx->bbar;
+ int q = ssx->q;
+ mpq_t *aq = ssx->aq;
+ int q_dir = ssx->q_dir;
+ int i, k, s, t, p, p_stat;
+ mpq_t teta, temp;
+ mpq_init(teta);
+ mpq_init(temp);
+ xassert(1 <= q && q <= n);
+ xassert(q_dir == +1 || q_dir == -1);
+ /* nothing is chosen so far */
+ p = 0, p_stat = 0;
+ /* look through the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { s = q_dir * mpq_sgn(aq[i]);
+ if (s < 0)
+ { /* xB[i] decreases */
+ k = Q_col[i]; /* x[k] = xB[i] */
+ t = type[k];
+ if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
+ { /* xB[i] has finite lower bound */
+ mpq_sub(temp, bbar[i], lb[k]);
+ mpq_div(temp, temp, aq[i]);
+ mpq_abs(temp, temp);
+ if (p == 0 || mpq_cmp(teta, temp) > 0)
+ { p = i;
+ p_stat = (t == SSX_FX ? SSX_NS : SSX_NL);
+ mpq_set(teta, temp);
+ }
+ }
+ }
+ else if (s > 0)
+ { /* xB[i] increases */
+ k = Q_col[i]; /* x[k] = xB[i] */
+ t = type[k];
+ if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
+ { /* xB[i] has finite upper bound */
+ mpq_sub(temp, bbar[i], ub[k]);
+ mpq_div(temp, temp, aq[i]);
+ mpq_abs(temp, temp);
+ if (p == 0 || mpq_cmp(teta, temp) > 0)
+ { p = i;
+ p_stat = (t == SSX_FX ? SSX_NS : SSX_NU);
+ mpq_set(teta, temp);
+ }
+ }
+ }
+ /* if something has been chosen and the ratio test indicates
+ exact degeneracy, the search can be finished */
+ if (p != 0 && mpq_sgn(teta) == 0) break;
+ }
+ /* if xN[q] is double-bounded, check if it can reach its opposite
+ bound before any basic variable */
+ k = Q_col[m+q]; /* x[k] = xN[q] */
+ if (type[k] == SSX_DB)
+ { mpq_sub(temp, ub[k], lb[k]);
+ if (p == 0 || mpq_cmp(teta, temp) > 0)
+ { p = -1;
+ p_stat = -1;
+ mpq_set(teta, temp);
+ }
+ }
+ ssx->p = p;
+ ssx->p_stat = p_stat;
+ /* if xB[p] has been chosen, determine its actual change in the
+ adjacent basis (it has the same sign as q_dir) */
+ if (p != 0)
+ { xassert(mpq_sgn(teta) >= 0);
+ if (q_dir > 0)
+ mpq_set(ssx->delta, teta);
+ else
+ mpq_neg(ssx->delta, teta);
+ }
+ mpq_clear(teta);
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_update_bbar - update values of basic variables.
+//
+// This routine recomputes the current values of basic variables for
+// the adjacent basis.
+//
+// The simplex table for the current basis is the following:
+//
+// xB[i] = sum{j in 1..n} alfa[i,j] * xN[q], i = 1,...,m
+//
+// therefore
+//
+// delta xB[i] = alfa[i,q] * delta xN[q], i = 1,...,m
+//
+// where delta xN[q] = xN.new[q] - xN[q] is the change of xN[q] in the
+// adjacent basis, and delta xB[i] = xB.new[i] - xB[i] is the change of
+// xB[i]. This gives formulae for recomputing values of xB[i]:
+//
+// xB.new[p] = xN[q] + delta xN[q]
+//
+// (because xN[q] becomes xB[p] in the adjacent basis), and
+//
+// xB.new[i] = xB[i] + alfa[i,q] * delta xN[q], i != p
+//
+// for other basic variables. */
+
+void ssx_update_bbar(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *bbar = ssx->bbar;
+ mpq_t *cbar = ssx->cbar;
+ int p = ssx->p;
+ int q = ssx->q;
+ mpq_t *aq = ssx->aq;
+ int i;
+ mpq_t temp;
+ mpq_init(temp);
+ xassert(1 <= q && q <= n);
+ if (p < 0)
+ { /* xN[q] is double-bounded and goes to its opposite bound */
+ /* nop */;
+ }
+ else
+ { /* xN[q] becomes xB[p] in the adjacent basis */
+ /* xB.new[p] = xN[q] + delta xN[q] */
+ xassert(1 <= p && p <= m);
+ ssx_get_xNj(ssx, q, temp);
+ mpq_add(bbar[p], temp, ssx->delta);
+ }
+ /* update values of other basic variables depending on xN[q] */
+ for (i = 1; i <= m; i++)
+ { if (i == p) continue;
+ /* xB.new[i] = xB[i] + alfa[i,q] * delta xN[q] */
+ if (mpq_sgn(aq[i]) == 0) continue;
+ mpq_mul(temp, aq[i], ssx->delta);
+ mpq_add(bbar[i], bbar[i], temp);
+ }
+#if 1
+ /* update value of the objective function */
+ /* z.new = z + d[q] * delta xN[q] */
+ mpq_mul(temp, cbar[q], ssx->delta);
+ mpq_add(bbar[0], bbar[0], temp);
+#endif
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- ssx_update_pi - update simplex multipliers.
+--
+-- This routine recomputes the vector of simplex multipliers for the
+-- adjacent basis. */
+
+void ssx_update_pi(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *pi = ssx->pi;
+ mpq_t *cbar = ssx->cbar;
+ int p = ssx->p;
+ int q = ssx->q;
+ mpq_t *aq = ssx->aq;
+ mpq_t *rho = ssx->rho;
+ int i;
+ mpq_t new_dq, temp;
+ mpq_init(new_dq);
+ mpq_init(temp);
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n);
+ /* compute d[q] in the adjacent basis */
+ mpq_div(new_dq, cbar[q], aq[p]);
+ /* update the vector of simplex multipliers */
+ for (i = 1; i <= m; i++)
+ { if (mpq_sgn(rho[i]) == 0) continue;
+ mpq_mul(temp, new_dq, rho[i]);
+ mpq_sub(pi[i], pi[i], temp);
+ }
+ mpq_clear(new_dq);
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_update_cbar - update reduced costs of non-basic variables.
+//
+// This routine recomputes the vector of reduced costs of non-basic
+// variables for the adjacent basis. */
+
+void ssx_update_cbar(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ mpq_t *cbar = ssx->cbar;
+ int p = ssx->p;
+ int q = ssx->q;
+ mpq_t *ap = ssx->ap;
+ int j;
+ mpq_t temp;
+ mpq_init(temp);
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n);
+ /* compute d[q] in the adjacent basis */
+ /* d.new[q] = d[q] / alfa[p,q] */
+ mpq_div(cbar[q], cbar[q], ap[q]);
+ /* update reduced costs of other non-basic variables */
+ for (j = 1; j <= n; j++)
+ { if (j == q) continue;
+ /* d.new[j] = d[j] - (alfa[p,j] / alfa[p,q]) * d[q] */
+ if (mpq_sgn(ap[j]) == 0) continue;
+ mpq_mul(temp, ap[j], cbar[q]);
+ mpq_sub(cbar[j], cbar[j], temp);
+ }
+ mpq_clear(temp);
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_change_basis - change current basis to adjacent one.
+//
+// This routine changes the current basis to the adjacent one swapping
+// basic variable xB[p] and non-basic variable xN[q]. */
+
+void ssx_change_basis(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int *type = ssx->type;
+ int *stat = ssx->stat;
+ int *Q_row = ssx->Q_row;
+ int *Q_col = ssx->Q_col;
+ int p = ssx->p;
+ int q = ssx->q;
+ int p_stat = ssx->p_stat;
+ int k, kp, kq;
+ if (p < 0)
+ { /* special case: xN[q] goes to its opposite bound */
+ xassert(1 <= q && q <= n);
+ k = Q_col[m+q]; /* x[k] = xN[q] */
+ xassert(type[k] == SSX_DB);
+ switch (stat[k])
+ { case SSX_NL:
+ stat[k] = SSX_NU;
+ break;
+ case SSX_NU:
+ stat[k] = SSX_NL;
+ break;
+ default:
+ xassert(stat != stat);
+ }
+ }
+ else
+ { /* xB[p] leaves the basis, xN[q] enters the basis */
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n);
+ kp = Q_col[p]; /* x[kp] = xB[p] */
+ kq = Q_col[m+q]; /* x[kq] = xN[q] */
+ /* check non-basic status of xB[p] which becomes xN[q] */
+ switch (type[kp])
+ { case SSX_FR:
+ xassert(p_stat == SSX_NF);
+ break;
+ case SSX_LO:
+ xassert(p_stat == SSX_NL);
+ break;
+ case SSX_UP:
+ xassert(p_stat == SSX_NU);
+ break;
+ case SSX_DB:
+ xassert(p_stat == SSX_NL || p_stat == SSX_NU);
+ break;
+ case SSX_FX:
+ xassert(p_stat == SSX_NS);
+ break;
+ default:
+ xassert(type != type);
+ }
+ /* swap xB[p] and xN[q] */
+ stat[kp] = (char)p_stat, stat[kq] = SSX_BS;
+ Q_row[kp] = m+q, Q_row[kq] = p;
+ Q_col[p] = kq, Q_col[m+q] = kp;
+ /* update factorization of the basis matrix */
+ if (bfx_update(ssx->binv, p))
+ { if (ssx_factorize(ssx))
+ xassert(("Internal error: basis matrix is singular", 0));
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_delete - delete simplex solver workspace.
+//
+// This routine deletes the simplex solver workspace freeing all the
+// memory allocated to this object. */
+
+void ssx_delete(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int nnz = ssx->A_ptr[n+1]-1;
+ int i, j, k;
+ xfree(ssx->type);
+ for (k = 1; k <= m+n; k++) mpq_clear(ssx->lb[k]);
+ xfree(ssx->lb);
+ for (k = 1; k <= m+n; k++) mpq_clear(ssx->ub[k]);
+ xfree(ssx->ub);
+ for (k = 0; k <= m+n; k++) mpq_clear(ssx->coef[k]);
+ xfree(ssx->coef);
+ xfree(ssx->A_ptr);
+ xfree(ssx->A_ind);
+ for (k = 1; k <= nnz; k++) mpq_clear(ssx->A_val[k]);
+ xfree(ssx->A_val);
+ xfree(ssx->stat);
+ xfree(ssx->Q_row);
+ xfree(ssx->Q_col);
+ bfx_delete_binv(ssx->binv);
+ for (i = 0; i <= m; i++) mpq_clear(ssx->bbar[i]);
+ xfree(ssx->bbar);
+ for (i = 1; i <= m; i++) mpq_clear(ssx->pi[i]);
+ xfree(ssx->pi);
+ for (j = 1; j <= n; j++) mpq_clear(ssx->cbar[j]);
+ xfree(ssx->cbar);
+ for (i = 1; i <= m; i++) mpq_clear(ssx->rho[i]);
+ xfree(ssx->rho);
+ for (j = 1; j <= n; j++) mpq_clear(ssx->ap[j]);
+ xfree(ssx->ap);
+ for (i = 1; i <= m; i++) mpq_clear(ssx->aq[i]);
+ xfree(ssx->aq);
+ mpq_clear(ssx->delta);
+ xfree(ssx);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/glpssx02.c b/test/monniaux/glpk-4.65/src/draft/glpssx02.c
new file mode 100644
index 00000000..81db1350
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/glpssx02.c
@@ -0,0 +1,523 @@
+/* glpssx02.c (simplex method, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "glpssx.h"
+
+static void show_progress(SSX *ssx, int phase)
+{ /* this auxiliary routine displays information about progress of
+ the search */
+ int i, def = 0;
+ for (i = 1; i <= ssx->m; i++)
+ if (ssx->type[ssx->Q_col[i]] == SSX_FX) def++;
+ xprintf("%s%6d: %s = %22.15g (%d)\n", phase == 1 ? " " : "*",
+ ssx->it_cnt, phase == 1 ? "infsum" : "objval",
+ mpq_get_d(ssx->bbar[0]), def);
+#if 0
+ ssx->tm_lag = utime();
+#else
+ ssx->tm_lag = xtime();
+#endif
+ return;
+}
+
+/*----------------------------------------------------------------------
+// ssx_phase_I - find primal feasible solution.
+//
+// This routine implements phase I of the primal simplex method.
+//
+// On exit the routine returns one of the following codes:
+//
+// 0 - feasible solution found;
+// 1 - problem has no feasible solution;
+// 2 - iterations limit exceeded;
+// 3 - time limit exceeded.
+----------------------------------------------------------------------*/
+
+int ssx_phase_I(SSX *ssx)
+{ int m = ssx->m;
+ int n = ssx->n;
+ int *type = ssx->type;
+ mpq_t *lb = ssx->lb;
+ mpq_t *ub = ssx->ub;
+ mpq_t *coef = ssx->coef;
+ int *A_ptr = ssx->A_ptr;
+ int *A_ind = ssx->A_ind;
+ mpq_t *A_val = ssx->A_val;
+ int *Q_col = ssx->Q_col;
+ mpq_t *bbar = ssx->bbar;
+ mpq_t *pi = ssx->pi;
+ mpq_t *cbar = ssx->cbar;
+ int *orig_type, orig_dir;
+ mpq_t *orig_lb, *orig_ub, *orig_coef;
+ int i, k, ret;
+ /* save components of the original LP problem, which are changed
+ by the routine */
+ orig_type = xcalloc(1+m+n, sizeof(int));
+ orig_lb = xcalloc(1+m+n, sizeof(mpq_t));
+ orig_ub = xcalloc(1+m+n, sizeof(mpq_t));
+ orig_coef = xcalloc(1+m+n, sizeof(mpq_t));
+ for (k = 1; k <= m+n; k++)
+ { orig_type[k] = type[k];
+ mpq_init(orig_lb[k]);
+ mpq_set(orig_lb[k], lb[k]);
+ mpq_init(orig_ub[k]);
+ mpq_set(orig_ub[k], ub[k]);
+ }
+ orig_dir = ssx->dir;
+ for (k = 0; k <= m+n; k++)
+ { mpq_init(orig_coef[k]);
+ mpq_set(orig_coef[k], coef[k]);
+ }
+ /* build an artificial basic solution, which is primal feasible,
+ and also build an auxiliary objective function to minimize the
+ sum of infeasibilities for the original problem */
+ ssx->dir = SSX_MIN;
+ for (k = 0; k <= m+n; k++) mpq_set_si(coef[k], 0, 1);
+ mpq_set_si(bbar[0], 0, 1);
+ for (i = 1; i <= m; i++)
+ { int t;
+ k = Q_col[i]; /* x[k] = xB[i] */
+ t = type[k];
+ if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
+ { /* in the original problem x[k] has lower bound */
+ if (mpq_cmp(bbar[i], lb[k]) < 0)
+ { /* which is violated */
+ type[k] = SSX_UP;
+ mpq_set(ub[k], lb[k]);
+ mpq_set_si(lb[k], 0, 1);
+ mpq_set_si(coef[k], -1, 1);
+ mpq_add(bbar[0], bbar[0], ub[k]);
+ mpq_sub(bbar[0], bbar[0], bbar[i]);
+ }
+ }
+ if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
+ { /* in the original problem x[k] has upper bound */
+ if (mpq_cmp(bbar[i], ub[k]) > 0)
+ { /* which is violated */
+ type[k] = SSX_LO;
+ mpq_set(lb[k], ub[k]);
+ mpq_set_si(ub[k], 0, 1);
+ mpq_set_si(coef[k], +1, 1);
+ mpq_add(bbar[0], bbar[0], bbar[i]);
+ mpq_sub(bbar[0], bbar[0], lb[k]);
+ }
+ }
+ }
+ /* now the initial basic solution should be primal feasible due
+ to changes of bounds of some basic variables, which turned to
+ implicit artifical variables */
+ /* compute simplex multipliers and reduced costs */
+ ssx_eval_pi(ssx);
+ ssx_eval_cbar(ssx);
+ /* display initial progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+ show_progress(ssx, 1);
+ /* main loop starts here */
+ for (;;)
+ { /* display current progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+#if 0
+ if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
+#else
+ if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
+#endif
+ show_progress(ssx, 1);
+ /* we do not need to wait until all artificial variables have
+ left the basis */
+ if (mpq_sgn(bbar[0]) == 0)
+ { /* the sum of infeasibilities is zero, therefore the current
+ solution is primal feasible for the original problem */
+ ret = 0;
+ break;
+ }
+ /* check if the iterations limit has been exhausted */
+ if (ssx->it_lim == 0)
+ { ret = 2;
+ break;
+ }
+ /* check if the time limit has been exhausted */
+#if 0
+ if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
+#else
+ if (ssx->tm_lim >= 0.0 &&
+ ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
+#endif
+ { ret = 3;
+ break;
+ }
+ /* choose non-basic variable xN[q] */
+ ssx_chuzc(ssx);
+ /* if xN[q] cannot be chosen, the sum of infeasibilities is
+ minimal but non-zero; therefore the original problem has no
+ primal feasible solution */
+ if (ssx->q == 0)
+ { ret = 1;
+ break;
+ }
+ /* compute q-th column of the simplex table */
+ ssx_eval_col(ssx);
+ /* choose basic variable xB[p] */
+ ssx_chuzr(ssx);
+ /* the sum of infeasibilities cannot be negative, therefore
+ the auxiliary lp problem cannot have unbounded solution */
+ xassert(ssx->p != 0);
+ /* update values of basic variables */
+ ssx_update_bbar(ssx);
+ if (ssx->p > 0)
+ { /* compute p-th row of the inverse inv(B) */
+ ssx_eval_rho(ssx);
+ /* compute p-th row of the simplex table */
+ ssx_eval_row(ssx);
+ xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
+ /* update simplex multipliers */
+ ssx_update_pi(ssx);
+ /* update reduced costs of non-basic variables */
+ ssx_update_cbar(ssx);
+ }
+ /* xB[p] is leaving the basis; if it is implicit artificial
+ variable, the corresponding residual vanishes; therefore
+ bounds of this variable should be restored to the original
+ values */
+ if (ssx->p > 0)
+ { k = Q_col[ssx->p]; /* x[k] = xB[p] */
+ if (type[k] != orig_type[k])
+ { /* x[k] is implicit artificial variable */
+ type[k] = orig_type[k];
+ mpq_set(lb[k], orig_lb[k]);
+ mpq_set(ub[k], orig_ub[k]);
+ xassert(ssx->p_stat == SSX_NL || ssx->p_stat == SSX_NU);
+ ssx->p_stat = (ssx->p_stat == SSX_NL ? SSX_NU : SSX_NL);
+ if (type[k] == SSX_FX) ssx->p_stat = SSX_NS;
+ /* nullify the objective coefficient at x[k] */
+ mpq_set_si(coef[k], 0, 1);
+ /* since coef[k] has been changed, we need to compute
+ new reduced cost of x[k], which it will have in the
+ adjacent basis */
+ /* the formula d[j] = cN[j] - pi' * N[j] is used (note
+ that the vector pi is not changed, because it depends
+ on objective coefficients at basic variables, but in
+ the adjacent basis, for which the vector pi has been
+ just recomputed, x[k] is non-basic) */
+ if (k <= m)
+ { /* x[k] is auxiliary variable */
+ mpq_neg(cbar[ssx->q], pi[k]);
+ }
+ else
+ { /* x[k] is structural variable */
+ int ptr;
+ mpq_t temp;
+ mpq_init(temp);
+ mpq_set_si(cbar[ssx->q], 0, 1);
+ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
+ { mpq_mul(temp, pi[A_ind[ptr]], A_val[ptr]);
+ mpq_add(cbar[ssx->q], cbar[ssx->q], temp);
+ }
+ mpq_clear(temp);
+ }
+ }
+ }
+ /* jump to the adjacent vertex of the polyhedron */
+ ssx_change_basis(ssx);
+ /* one simplex iteration has been performed */
+ if (ssx->it_lim > 0) ssx->it_lim--;
+ ssx->it_cnt++;
+ }
+ /* display final progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+ show_progress(ssx, 1);
+ /* restore components of the original problem, which were changed
+ by the routine */
+ for (k = 1; k <= m+n; k++)
+ { type[k] = orig_type[k];
+ mpq_set(lb[k], orig_lb[k]);
+ mpq_clear(orig_lb[k]);
+ mpq_set(ub[k], orig_ub[k]);
+ mpq_clear(orig_ub[k]);
+ }
+ ssx->dir = orig_dir;
+ for (k = 0; k <= m+n; k++)
+ { mpq_set(coef[k], orig_coef[k]);
+ mpq_clear(orig_coef[k]);
+ }
+ xfree(orig_type);
+ xfree(orig_lb);
+ xfree(orig_ub);
+ xfree(orig_coef);
+ /* return to the calling program */
+ return ret;
+}
+
+/*----------------------------------------------------------------------
+// ssx_phase_II - find optimal solution.
+//
+// This routine implements phase II of the primal simplex method.
+//
+// On exit the routine returns one of the following codes:
+//
+// 0 - optimal solution found;
+// 1 - problem has unbounded solution;
+// 2 - iterations limit exceeded;
+// 3 - time limit exceeded.
+----------------------------------------------------------------------*/
+
+int ssx_phase_II(SSX *ssx)
+{ int ret;
+ /* display initial progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+ show_progress(ssx, 2);
+ /* main loop starts here */
+ for (;;)
+ { /* display current progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+#if 0
+ if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
+#else
+ if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
+#endif
+ show_progress(ssx, 2);
+ /* check if the iterations limit has been exhausted */
+ if (ssx->it_lim == 0)
+ { ret = 2;
+ break;
+ }
+ /* check if the time limit has been exhausted */
+#if 0
+ if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
+#else
+ if (ssx->tm_lim >= 0.0 &&
+ ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
+#endif
+ { ret = 3;
+ break;
+ }
+ /* choose non-basic variable xN[q] */
+ ssx_chuzc(ssx);
+ /* if xN[q] cannot be chosen, the current basic solution is
+ dual feasible and therefore optimal */
+ if (ssx->q == 0)
+ { ret = 0;
+ break;
+ }
+ /* compute q-th column of the simplex table */
+ ssx_eval_col(ssx);
+ /* choose basic variable xB[p] */
+ ssx_chuzr(ssx);
+ /* if xB[p] cannot be chosen, the problem has no dual feasible
+ solution (i.e. unbounded) */
+ if (ssx->p == 0)
+ { ret = 1;
+ break;
+ }
+ /* update values of basic variables */
+ ssx_update_bbar(ssx);
+ if (ssx->p > 0)
+ { /* compute p-th row of the inverse inv(B) */
+ ssx_eval_rho(ssx);
+ /* compute p-th row of the simplex table */
+ ssx_eval_row(ssx);
+ xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
+#if 0
+ /* update simplex multipliers */
+ ssx_update_pi(ssx);
+#endif
+ /* update reduced costs of non-basic variables */
+ ssx_update_cbar(ssx);
+ }
+ /* jump to the adjacent vertex of the polyhedron */
+ ssx_change_basis(ssx);
+ /* one simplex iteration has been performed */
+ if (ssx->it_lim > 0) ssx->it_lim--;
+ ssx->it_cnt++;
+ }
+ /* display final progress of the search */
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ON)
+#endif
+ show_progress(ssx, 2);
+ /* return to the calling program */
+ return ret;
+}
+
+/*----------------------------------------------------------------------
+// ssx_driver - base driver to exact simplex method.
+//
+// This routine is a base driver to a version of the primal simplex
+// method using exact (bignum) arithmetic.
+//
+// On exit the routine returns one of the following codes:
+//
+// 0 - optimal solution found;
+// 1 - problem has no feasible solution;
+// 2 - problem has unbounded solution;
+// 3 - iterations limit exceeded (phase I);
+// 4 - iterations limit exceeded (phase II);
+// 5 - time limit exceeded (phase I);
+// 6 - time limit exceeded (phase II);
+// 7 - initial basis matrix is exactly singular.
+----------------------------------------------------------------------*/
+
+int ssx_driver(SSX *ssx)
+{ int m = ssx->m;
+ int *type = ssx->type;
+ mpq_t *lb = ssx->lb;
+ mpq_t *ub = ssx->ub;
+ int *Q_col = ssx->Q_col;
+ mpq_t *bbar = ssx->bbar;
+ int i, k, ret;
+ ssx->tm_beg = xtime();
+ /* factorize the initial basis matrix */
+ if (ssx_factorize(ssx))
+#if 0 /* 25/XI-2017 */
+ { xprintf("Initial basis matrix is singular\n");
+#else
+ { if (ssx->msg_lev >= GLP_MSG_ERR)
+ xprintf("Initial basis matrix is singular\n");
+#endif
+ ret = 7;
+ goto done;
+ }
+ /* compute values of basic variables */
+ ssx_eval_bbar(ssx);
+ /* check if the initial basic solution is primal feasible */
+ for (i = 1; i <= m; i++)
+ { int t;
+ k = Q_col[i]; /* x[k] = xB[i] */
+ t = type[k];
+ if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
+ { /* x[k] has lower bound */
+ if (mpq_cmp(bbar[i], lb[k]) < 0)
+ { /* which is violated */
+ break;
+ }
+ }
+ if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
+ { /* x[k] has upper bound */
+ if (mpq_cmp(bbar[i], ub[k]) > 0)
+ { /* which is violated */
+ break;
+ }
+ }
+ }
+ if (i > m)
+ { /* no basic variable violates its bounds */
+ ret = 0;
+ goto skip;
+ }
+ /* phase I: find primal feasible solution */
+ ret = ssx_phase_I(ssx);
+ switch (ret)
+ { case 0:
+ ret = 0;
+ break;
+ case 1:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
+ ret = 1;
+ break;
+ case 2:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ ret = 3;
+ break;
+ case 3:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ ret = 5;
+ break;
+ default:
+ xassert(ret != ret);
+ }
+ /* compute values of basic variables (actually only the objective
+ value needs to be computed) */
+ ssx_eval_bbar(ssx);
+skip: /* compute simplex multipliers */
+ ssx_eval_pi(ssx);
+ /* compute reduced costs of non-basic variables */
+ ssx_eval_cbar(ssx);
+ /* if phase I failed, do not start phase II */
+ if (ret != 0) goto done;
+ /* phase II: find optimal solution */
+ ret = ssx_phase_II(ssx);
+ switch (ret)
+ { case 0:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("OPTIMAL SOLUTION FOUND\n");
+ ret = 0;
+ break;
+ case 1:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
+ ret = 2;
+ break;
+ case 2:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ ret = 4;
+ break;
+ case 3:
+#if 1 /* 25/XI-2017 */
+ if (ssx->msg_lev >= GLP_MSG_ALL)
+#endif
+ xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ ret = 6;
+ break;
+ default:
+ xassert(ret != ret);
+ }
+done: /* decrease the time limit by the spent amount of time */
+ if (ssx->tm_lim >= 0.0)
+#if 0
+ { ssx->tm_lim -= utime() - ssx->tm_beg;
+#else
+ { ssx->tm_lim -= xdifftime(xtime(), ssx->tm_beg);
+#endif
+ if (ssx->tm_lim < 0.0) ssx->tm_lim = 0.0;
+ }
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/ios.h b/test/monniaux/glpk-4.65/src/draft/ios.h
new file mode 100644
index 00000000..1cb07ee0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/ios.h
@@ -0,0 +1,547 @@
+/* ios.h (integer optimization suite) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef IOS_H
+#define IOS_H
+
+#include "prob.h"
+
+#if 1 /* 02/II-2018 */
+#define NEW_LOCAL 1
+#endif
+
+#if 1 /* 15/II-2018 */
+#define NEW_COVER 1
+#endif
+
+typedef struct IOSLOT IOSLOT;
+typedef struct IOSNPD IOSNPD;
+typedef struct IOSBND IOSBND;
+typedef struct IOSTAT IOSTAT;
+typedef struct IOSROW IOSROW;
+typedef struct IOSAIJ IOSAIJ;
+#ifdef NEW_LOCAL /* 02/II-2018 */
+typedef glp_prob IOSPOOL;
+typedef GLPROW IOSCUT;
+#else
+typedef struct IOSPOOL IOSPOOL;
+typedef struct IOSCUT IOSCUT;
+#endif
+
+struct glp_tree
+{ /* branch-and-bound tree */
+ int magic;
+ /* magic value used for debugging */
+ DMP *pool;
+ /* memory pool to store all IOS components */
+ int n;
+ /* number of columns (variables) */
+ /*--------------------------------------------------------------*/
+ /* problem components corresponding to the original MIP and its
+ LP relaxation (used to restore the original problem object on
+ exit from the solver) */
+ int orig_m;
+ /* number of rows */
+ unsigned char *orig_type; /* uchar orig_type[1+orig_m+n]; */
+ /* types of all variables */
+ double *orig_lb; /* double orig_lb[1+orig_m+n]; */
+ /* lower bounds of all variables */
+ double *orig_ub; /* double orig_ub[1+orig_m+n]; */
+ /* upper bounds of all variables */
+ unsigned char *orig_stat; /* uchar orig_stat[1+orig_m+n]; */
+ /* statuses of all variables */
+ double *orig_prim; /* double orig_prim[1+orig_m+n]; */
+ /* primal values of all variables */
+ double *orig_dual; /* double orig_dual[1+orig_m+n]; */
+ /* dual values of all variables */
+ double orig_obj;
+ /* optimal objective value for LP relaxation */
+ /*--------------------------------------------------------------*/
+ /* branch-and-bound tree */
+ int nslots;
+ /* length of the array of slots (enlarged automatically) */
+ int avail;
+ /* index of the first free slot; 0 means all slots are in use */
+ IOSLOT *slot; /* IOSLOT slot[1+nslots]; */
+ /* array of slots:
+ slot[0] is not used;
+ slot[p], 1 <= p <= nslots, either contains a pointer to some
+ node of the branch-and-bound tree, in which case p is used on
+ API level as the reference number of corresponding subproblem,
+ or is free; all free slots are linked into single linked list;
+ slot[1] always contains a pointer to the root node (it is free
+ only if the tree is empty) */
+ IOSNPD *head;
+ /* pointer to the head of the active list */
+ IOSNPD *tail;
+ /* pointer to the tail of the active list */
+ /* the active list is a doubly linked list of active subproblems
+ which correspond to leaves of the tree; all subproblems in the
+ active list are ordered chronologically (each a new subproblem
+ is always added to the tail of the list) */
+ int a_cnt;
+ /* current number of active nodes (including the current one) */
+ int n_cnt;
+ /* current number of all (active and inactive) nodes */
+ int t_cnt;
+ /* total number of nodes including those which have been already
+ removed from the tree; this count is increased by one whenever
+ a new node is created and never decreased */
+ /*--------------------------------------------------------------*/
+ /* problem components corresponding to the root subproblem */
+ int root_m;
+ /* number of rows */
+ unsigned char *root_type; /* uchar root_type[1+root_m+n]; */
+ /* types of all variables */
+ double *root_lb; /* double root_lb[1+root_m+n]; */
+ /* lower bounds of all variables */
+ double *root_ub; /* double root_ub[1+root_m+n]; */
+ /* upper bounds of all variables */
+ unsigned char *root_stat; /* uchar root_stat[1+root_m+n]; */
+ /* statuses of all variables */
+ /*--------------------------------------------------------------*/
+ /* current subproblem and its LP relaxation */
+ IOSNPD *curr;
+ /* pointer to the current subproblem (which can be only active);
+ NULL means the current subproblem does not exist */
+ glp_prob *mip;
+ /* original problem object passed to the solver; if the current
+ subproblem exists, its LP segment corresponds to LP relaxation
+ of the current subproblem; if the current subproblem does not
+ exist, its LP segment corresponds to LP relaxation of the root
+ subproblem (note that the root subproblem may differ from the
+ original MIP, because it may be preprocessed and/or may have
+ additional rows) */
+ unsigned char *non_int; /* uchar non_int[1+n]; */
+ /* these column flags are set each time when LP relaxation of the
+ current subproblem has been solved;
+ non_int[0] is not used;
+ non_int[j], 1 <= j <= n, is j-th column flag; if this flag is
+ set, corresponding variable is required to be integer, but its
+ value in basic solution is fractional */
+ /*--------------------------------------------------------------*/
+ /* problem components corresponding to the parent (predecessor)
+ subproblem for the current subproblem; used to inspect changes
+ on freezing the current subproblem */
+ int pred_m;
+ /* number of rows */
+ int pred_max;
+ /* length of the following four arrays (enlarged automatically),
+ pred_max >= pred_m + n */
+ unsigned char *pred_type; /* uchar pred_type[1+pred_m+n]; */
+ /* types of all variables */
+ double *pred_lb; /* double pred_lb[1+pred_m+n]; */
+ /* lower bounds of all variables */
+ double *pred_ub; /* double pred_ub[1+pred_m+n]; */
+ /* upper bounds of all variables */
+ unsigned char *pred_stat; /* uchar pred_stat[1+pred_m+n]; */
+ /* statuses of all variables */
+ /****************************************************************/
+ /* built-in cut generators segment */
+ IOSPOOL *local;
+ /* local cut pool */
+#if 1 /* 13/II-2018 */
+ glp_cov *cov_gen;
+ /* pointer to working area used by the cover cut generator */
+#endif
+ glp_mir *mir_gen;
+ /* pointer to working area used by the MIR cut generator */
+ glp_cfg *clq_gen;
+ /* pointer to conflict graph used by the clique cut generator */
+ /*--------------------------------------------------------------*/
+ void *pcost;
+ /* pointer to working area used on pseudocost branching */
+ int *iwrk; /* int iwrk[1+n]; */
+ /* working array */
+ double *dwrk; /* double dwrk[1+n]; */
+ /* working array */
+ /*--------------------------------------------------------------*/
+ /* control parameters and statistics */
+ const glp_iocp *parm;
+ /* copy of control parameters passed to the solver */
+ double tm_beg;
+ /* starting time of the search, in seconds; the total time of the
+ search is the difference between xtime() and tm_beg */
+ double tm_lag;
+ /* the most recent time, in seconds, at which the progress of the
+ the search was displayed */
+ int sol_cnt;
+ /* number of integer feasible solutions found */
+#if 1 /* 11/VII-2013 */
+ void *P; /* glp_prob *P; */
+ /* problem passed to glp_intopt */
+ void *npp; /* NPP *npp; */
+ /* preprocessor workspace or NULL */
+ const char *save_sol;
+ /* filename (template) to save every new solution */
+ int save_cnt;
+ /* count to generate filename */
+#endif
+ /*--------------------------------------------------------------*/
+ /* advanced solver interface */
+ int reason;
+ /* flag indicating the reason why the callback routine is being
+ called (see glpk.h) */
+ int stop;
+ /* flag indicating that the callback routine requires premature
+ termination of the search */
+ int next_p;
+ /* reference number of active subproblem selected to continue
+ the search; 0 means no subproblem has been selected */
+ int reopt;
+ /* flag indicating that the current LP relaxation needs to be
+ re-optimized */
+ int reinv;
+ /* flag indicating that some (non-active) rows were removed from
+ the current LP relaxation, so if there no new rows appear, the
+ basis must be re-factorized */
+ int br_var;
+ /* the number of variable chosen to branch on */
+ int br_sel;
+ /* flag indicating which branch (subproblem) is suggested to be
+ selected to continue the search:
+ GLP_DN_BRNCH - select down-branch
+ GLP_UP_BRNCH - select up-branch
+ GLP_NO_BRNCH - use general selection technique */
+ int child;
+ /* subproblem reference number corresponding to br_sel */
+};
+
+struct IOSLOT
+{ /* node subproblem slot */
+ IOSNPD *node;
+ /* pointer to subproblem descriptor; NULL means free slot */
+ int next;
+ /* index of another free slot (only if this slot is free) */
+};
+
+struct IOSNPD
+{ /* node subproblem descriptor */
+ int p;
+ /* subproblem reference number (it is the index to corresponding
+ slot, i.e. slot[p] points to this descriptor) */
+ IOSNPD *up;
+ /* pointer to the parent subproblem; NULL means this node is the
+ root of the tree, in which case p = 1 */
+ int level;
+ /* node level (the root node has level 0) */
+ int count;
+ /* if count = 0, this subproblem is active; if count > 0, this
+ subproblem is inactive, in which case count is the number of
+ its child subproblems */
+ /* the following three linked lists are destroyed on reviving and
+ built anew on freezing the subproblem: */
+ IOSBND *b_ptr;
+ /* linked list of rows and columns of the parent subproblem whose
+ types and bounds were changed */
+ IOSTAT *s_ptr;
+ /* linked list of rows and columns of the parent subproblem whose
+ statuses were changed */
+ IOSROW *r_ptr;
+ /* linked list of rows (cuts) added to the parent subproblem */
+ int solved;
+ /* how many times LP relaxation of this subproblem was solved;
+ for inactive subproblem this count is always non-zero;
+ for active subproblem, which is not current, this count may be
+ non-zero, if the subproblem was temporarily suspended */
+ double lp_obj;
+ /* optimal objective value to LP relaxation of this subproblem;
+ on creating a subproblem this value is inherited from its
+ parent; for the root subproblem, which has no parent, this
+ value is initially set to -DBL_MAX (minimization) or +DBL_MAX
+ (maximization); each time the subproblem is re-optimized, this
+ value is appropriately changed */
+ double bound;
+ /* local lower (minimization) or upper (maximization) bound for
+ integer optimal solution to *this* subproblem; this bound is
+ local in the sense that only subproblems in the subtree rooted
+ at this node cannot have better integer feasible solutions;
+ on creating a subproblem its local bound is inherited from its
+ parent and then can be made stronger (never weaker); for the
+ root subproblem its local bound is initially set to -DBL_MAX
+ (minimization) or +DBL_MAX (maximization) and then improved as
+ the root LP relaxation has been solved */
+ /* the following two quantities are defined only if LP relaxation
+ of this subproblem was solved at least once (solved > 0): */
+ int ii_cnt;
+ /* number of integer variables whose value in optimal solution to
+ LP relaxation of this subproblem is fractional */
+ double ii_sum;
+ /* sum of integer infeasibilities */
+#if 1 /* 30/XI-2009 */
+ int changed;
+ /* how many times this subproblem was re-formulated (by adding
+ cutting plane constraints) */
+#endif
+ int br_var;
+ /* ordinal number of branching variable, 1 <= br_var <= n, used
+ to split this subproblem; 0 means that either this subproblem
+ is active or branching was made on a constraint */
+ double br_val;
+ /* (fractional) value of branching variable in optimal solution
+ to final LP relaxation of this subproblem */
+ void *data; /* char data[tree->cb_size]; */
+ /* pointer to the application-specific data */
+ IOSNPD *temp;
+ /* working pointer used by some routines */
+ IOSNPD *prev;
+ /* pointer to previous subproblem in the active list */
+ IOSNPD *next;
+ /* pointer to next subproblem in the active list */
+};
+
+struct IOSBND
+{ /* bounds change entry */
+ int k;
+ /* ordinal number of corresponding row (1 <= k <= m) or column
+ (m+1 <= k <= m+n), where m and n are the number of rows and
+ columns, resp., in the parent subproblem */
+ unsigned char type;
+ /* new type */
+ double lb;
+ /* new lower bound */
+ double ub;
+ /* new upper bound */
+ IOSBND *next;
+ /* pointer to next entry for the same subproblem */
+};
+
+struct IOSTAT
+{ /* status change entry */
+ int k;
+ /* ordinal number of corresponding row (1 <= k <= m) or column
+ (m+1 <= k <= m+n), where m and n are the number of rows and
+ columns, resp., in the parent subproblem */
+ unsigned char stat;
+ /* new status */
+ IOSTAT *next;
+ /* pointer to next entry for the same subproblem */
+};
+
+struct IOSROW
+{ /* row (constraint) addition entry */
+ char *name;
+ /* row name or NULL */
+ unsigned char origin;
+ /* row origin flag (see glp_attr.origin) */
+ unsigned char klass;
+ /* row class descriptor (see glp_attr.klass) */
+ unsigned char type;
+ /* row type (GLP_LO, GLP_UP, etc.) */
+ double lb;
+ /* row lower bound */
+ double ub;
+ /* row upper bound */
+ IOSAIJ *ptr;
+ /* pointer to the row coefficient list */
+ double rii;
+ /* row scale factor */
+ unsigned char stat;
+ /* row status (GLP_BS, GLP_NL, etc.) */
+ IOSROW *next;
+ /* pointer to next entry for the same subproblem */
+};
+
+struct IOSAIJ
+{ /* constraint coefficient */
+ int j;
+ /* variable (column) number, 1 <= j <= n */
+ double val;
+ /* non-zero coefficient value */
+ IOSAIJ *next;
+ /* pointer to next coefficient for the same row */
+};
+
+#ifndef NEW_LOCAL /* 02/II-2018 */
+struct IOSPOOL
+{ /* cut pool */
+ int size;
+ /* pool size = number of cuts in the pool */
+ IOSCUT *head;
+ /* pointer to the first cut */
+ IOSCUT *tail;
+ /* pointer to the last cut */
+ int ord;
+ /* ordinal number of the current cut, 1 <= ord <= size */
+ IOSCUT *curr;
+ /* pointer to the current cut */
+};
+#endif
+
+#ifndef NEW_LOCAL /* 02/II-2018 */
+struct IOSCUT
+{ /* cut (cutting plane constraint) */
+ char *name;
+ /* cut name or NULL */
+ unsigned char klass;
+ /* cut class descriptor (see glp_attr.klass) */
+ IOSAIJ *ptr;
+ /* pointer to the cut coefficient list */
+ unsigned char type;
+ /* cut type:
+ GLP_LO: sum a[j] * x[j] >= b
+ GLP_UP: sum a[j] * x[j] <= b
+ GLP_FX: sum a[j] * x[j] = b */
+ double rhs;
+ /* cut right-hand side */
+ IOSCUT *prev;
+ /* pointer to previous cut */
+ IOSCUT *next;
+ /* pointer to next cut */
+};
+#endif
+
+#define ios_create_tree _glp_ios_create_tree
+glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm);
+/* create branch-and-bound tree */
+
+#define ios_revive_node _glp_ios_revive_node
+void ios_revive_node(glp_tree *tree, int p);
+/* revive specified subproblem */
+
+#define ios_freeze_node _glp_ios_freeze_node
+void ios_freeze_node(glp_tree *tree);
+/* freeze current subproblem */
+
+#define ios_clone_node _glp_ios_clone_node
+void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[]);
+/* clone specified subproblem */
+
+#define ios_delete_node _glp_ios_delete_node
+void ios_delete_node(glp_tree *tree, int p);
+/* delete specified subproblem */
+
+#define ios_delete_tree _glp_ios_delete_tree
+void ios_delete_tree(glp_tree *tree);
+/* delete branch-and-bound tree */
+
+#define ios_eval_degrad _glp_ios_eval_degrad
+void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up);
+/* estimate obj. degrad. for down- and up-branches */
+
+#define ios_round_bound _glp_ios_round_bound
+double ios_round_bound(glp_tree *tree, double bound);
+/* improve local bound by rounding */
+
+#define ios_is_hopeful _glp_ios_is_hopeful
+int ios_is_hopeful(glp_tree *tree, double bound);
+/* check if subproblem is hopeful */
+
+#define ios_best_node _glp_ios_best_node
+int ios_best_node(glp_tree *tree);
+/* find active node with best local bound */
+
+#define ios_relative_gap _glp_ios_relative_gap
+double ios_relative_gap(glp_tree *tree);
+/* compute relative mip gap */
+
+#define ios_solve_node _glp_ios_solve_node
+int ios_solve_node(glp_tree *tree);
+/* solve LP relaxation of current subproblem */
+
+#define ios_create_pool _glp_ios_create_pool
+IOSPOOL *ios_create_pool(glp_tree *tree);
+/* create cut pool */
+
+#define ios_add_row _glp_ios_add_row
+int ios_add_row(glp_tree *tree, IOSPOOL *pool,
+ const char *name, int klass, int flags, int len, const int ind[],
+ const double val[], int type, double rhs);
+/* add row (constraint) to the cut pool */
+
+#define ios_find_row _glp_ios_find_row
+IOSCUT *ios_find_row(IOSPOOL *pool, int i);
+/* find row (constraint) in the cut pool */
+
+#define ios_del_row _glp_ios_del_row
+void ios_del_row(glp_tree *tree, IOSPOOL *pool, int i);
+/* remove row (constraint) from the cut pool */
+
+#define ios_clear_pool _glp_ios_clear_pool
+void ios_clear_pool(glp_tree *tree, IOSPOOL *pool);
+/* remove all rows (constraints) from the cut pool */
+
+#define ios_delete_pool _glp_ios_delete_pool
+void ios_delete_pool(glp_tree *tree, IOSPOOL *pool);
+/* delete cut pool */
+
+#if 1 /* 11/VII-2013 */
+#define ios_process_sol _glp_ios_process_sol
+void ios_process_sol(glp_tree *T);
+/* process integer feasible solution just found */
+#endif
+
+#define ios_preprocess_node _glp_ios_preprocess_node
+int ios_preprocess_node(glp_tree *tree, int max_pass);
+/* preprocess current subproblem */
+
+#define ios_driver _glp_ios_driver
+int ios_driver(glp_tree *tree);
+/* branch-and-bound driver */
+
+#define ios_cov_gen _glp_ios_cov_gen
+void ios_cov_gen(glp_tree *tree);
+/* generate mixed cover cuts */
+
+#define ios_pcost_init _glp_ios_pcost_init
+void *ios_pcost_init(glp_tree *tree);
+/* initialize working data used on pseudocost branching */
+
+#define ios_pcost_branch _glp_ios_pcost_branch
+int ios_pcost_branch(glp_tree *T, int *next);
+/* choose branching variable with pseudocost branching */
+
+#define ios_pcost_update _glp_ios_pcost_update
+void ios_pcost_update(glp_tree *tree);
+/* update history information for pseudocost branching */
+
+#define ios_pcost_free _glp_ios_pcost_free
+void ios_pcost_free(glp_tree *tree);
+/* free working area used on pseudocost branching */
+
+#define ios_feas_pump _glp_ios_feas_pump
+void ios_feas_pump(glp_tree *T);
+/* feasibility pump heuristic */
+
+#if 1 /* 25/V-2013 */
+#define ios_proxy_heur _glp_ios_proxy_heur
+void ios_proxy_heur(glp_tree *T);
+/* proximity search heuristic */
+#endif
+
+#define ios_process_cuts _glp_ios_process_cuts
+void ios_process_cuts(glp_tree *T);
+/* process cuts stored in the local cut pool */
+
+#define ios_choose_node _glp_ios_choose_node
+int ios_choose_node(glp_tree *T);
+/* select subproblem to continue the search */
+
+#define ios_choose_var _glp_ios_choose_var
+int ios_choose_var(glp_tree *T, int *next);
+/* select variable to branch on */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/lux.c b/test/monniaux/glpk-4.65/src/draft/lux.c
new file mode 100644
index 00000000..38cb758c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/lux.c
@@ -0,0 +1,1030 @@
+/* lux.c (LU-factorization, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "lux.h"
+
+#define xfault xerror
+#define dmp_create_poolx(size) dmp_create_pool()
+
+/***********************************************************************
+* lux_create - create LU-factorization
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* LUX *lux_create(int n);
+*
+* DESCRIPTION
+*
+* The routine lux_create creates LU-factorization data structure for
+* a matrix of the order n. Initially the factorization corresponds to
+* the unity matrix (F = V = P = Q = I, so A = I).
+*
+* RETURNS
+*
+* The routine returns a pointer to the created LU-factorization data
+* structure, which represents the unity matrix of the order n. */
+
+LUX *lux_create(int n)
+{ LUX *lux;
+ int k;
+ if (n < 1)
+ xfault("lux_create: n = %d; invalid parameter\n", n);
+ lux = xmalloc(sizeof(LUX));
+ lux->n = n;
+ lux->pool = dmp_create_poolx(sizeof(LUXELM));
+ lux->F_row = xcalloc(1+n, sizeof(LUXELM *));
+ lux->F_col = xcalloc(1+n, sizeof(LUXELM *));
+ lux->V_piv = xcalloc(1+n, sizeof(mpq_t));
+ lux->V_row = xcalloc(1+n, sizeof(LUXELM *));
+ lux->V_col = xcalloc(1+n, sizeof(LUXELM *));
+ lux->P_row = xcalloc(1+n, sizeof(int));
+ lux->P_col = xcalloc(1+n, sizeof(int));
+ lux->Q_row = xcalloc(1+n, sizeof(int));
+ lux->Q_col = xcalloc(1+n, sizeof(int));
+ for (k = 1; k <= n; k++)
+ { lux->F_row[k] = lux->F_col[k] = NULL;
+ mpq_init(lux->V_piv[k]);
+ mpq_set_si(lux->V_piv[k], 1, 1);
+ lux->V_row[k] = lux->V_col[k] = NULL;
+ lux->P_row[k] = lux->P_col[k] = k;
+ lux->Q_row[k] = lux->Q_col[k] = k;
+ }
+ lux->rank = n;
+ return lux;
+}
+
+/***********************************************************************
+* initialize - initialize LU-factorization data structures
+*
+* This routine initializes data structures for subsequent computing
+* the LU-factorization of a given matrix A, which is specified by the
+* formal routine col. On exit V = A and F = P = Q = I, where I is the
+* unity matrix. */
+
+static void initialize(LUX *lux, int (*col)(void *info, int j,
+ int ind[], mpq_t val[]), void *info, LUXWKA *wka)
+{ int n = lux->n;
+ DMP *pool = lux->pool;
+ LUXELM **F_row = lux->F_row;
+ LUXELM **F_col = lux->F_col;
+ mpq_t *V_piv = lux->V_piv;
+ LUXELM **V_row = lux->V_row;
+ LUXELM **V_col = lux->V_col;
+ int *P_row = lux->P_row;
+ int *P_col = lux->P_col;
+ int *Q_row = lux->Q_row;
+ int *Q_col = lux->Q_col;
+ int *R_len = wka->R_len;
+ int *R_head = wka->R_head;
+ int *R_prev = wka->R_prev;
+ int *R_next = wka->R_next;
+ int *C_len = wka->C_len;
+ int *C_head = wka->C_head;
+ int *C_prev = wka->C_prev;
+ int *C_next = wka->C_next;
+ LUXELM *fij, *vij;
+ int i, j, k, len, *ind;
+ mpq_t *val;
+ /* F := I */
+ for (i = 1; i <= n; i++)
+ { while (F_row[i] != NULL)
+ { fij = F_row[i], F_row[i] = fij->r_next;
+ mpq_clear(fij->val);
+ dmp_free_atom(pool, fij, sizeof(LUXELM));
+ }
+ }
+ for (j = 1; j <= n; j++) F_col[j] = NULL;
+ /* V := 0 */
+ for (k = 1; k <= n; k++) mpq_set_si(V_piv[k], 0, 1);
+ for (i = 1; i <= n; i++)
+ { while (V_row[i] != NULL)
+ { vij = V_row[i], V_row[i] = vij->r_next;
+ mpq_clear(vij->val);
+ dmp_free_atom(pool, vij, sizeof(LUXELM));
+ }
+ }
+ for (j = 1; j <= n; j++) V_col[j] = NULL;
+ /* V := A */
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(mpq_t));
+ for (k = 1; k <= n; k++) mpq_init(val[k]);
+ for (j = 1; j <= n; j++)
+ { /* obtain j-th column of matrix A */
+ len = col(info, j, ind, val);
+ if (!(0 <= len && len <= n))
+ xfault("lux_decomp: j = %d: len = %d; invalid column length"
+ "\n", j, len);
+ /* copy elements of j-th column to matrix V */
+ for (k = 1; k <= len; k++)
+ { /* get row index of a[i,j] */
+ i = ind[k];
+ if (!(1 <= i && i <= n))
+ xfault("lux_decomp: j = %d: i = %d; row index out of ran"
+ "ge\n", j, i);
+ /* check for duplicate indices */
+ if (V_row[i] != NULL && V_row[i]->j == j)
+ xfault("lux_decomp: j = %d: i = %d; duplicate row indice"
+ "s not allowed\n", j, i);
+ /* check for zero value */
+ if (mpq_sgn(val[k]) == 0)
+ xfault("lux_decomp: j = %d: i = %d; zero elements not al"
+ "lowed\n", j, i);
+ /* add new element v[i,j] = a[i,j] to V */
+ vij = dmp_get_atom(pool, sizeof(LUXELM));
+ vij->i = i, vij->j = j;
+ mpq_init(vij->val);
+ mpq_set(vij->val, val[k]);
+ vij->r_prev = NULL;
+ vij->r_next = V_row[i];
+ vij->c_prev = NULL;
+ vij->c_next = V_col[j];
+ if (vij->r_next != NULL) vij->r_next->r_prev = vij;
+ if (vij->c_next != NULL) vij->c_next->c_prev = vij;
+ V_row[i] = V_col[j] = vij;
+ }
+ }
+ xfree(ind);
+ for (k = 1; k <= n; k++) mpq_clear(val[k]);
+ xfree(val);
+ /* P := Q := I */
+ for (k = 1; k <= n; k++)
+ P_row[k] = P_col[k] = Q_row[k] = Q_col[k] = k;
+ /* the rank of A and V is not determined yet */
+ lux->rank = -1;
+ /* initially the entire matrix V is active */
+ /* determine its row lengths */
+ for (i = 1; i <= n; i++)
+ { len = 0;
+ for (vij = V_row[i]; vij != NULL; vij = vij->r_next) len++;
+ R_len[i] = len;
+ }
+ /* build linked lists of active rows */
+ for (len = 0; len <= n; len++) R_head[len] = 0;
+ for (i = 1; i <= n; i++)
+ { len = R_len[i];
+ R_prev[i] = 0;
+ R_next[i] = R_head[len];
+ if (R_next[i] != 0) R_prev[R_next[i]] = i;
+ R_head[len] = i;
+ }
+ /* determine its column lengths */
+ for (j = 1; j <= n; j++)
+ { len = 0;
+ for (vij = V_col[j]; vij != NULL; vij = vij->c_next) len++;
+ C_len[j] = len;
+ }
+ /* build linked lists of active columns */
+ for (len = 0; len <= n; len++) C_head[len] = 0;
+ for (j = 1; j <= n; j++)
+ { len = C_len[j];
+ C_prev[j] = 0;
+ C_next[j] = C_head[len];
+ if (C_next[j] != 0) C_prev[C_next[j]] = j;
+ C_head[len] = j;
+ }
+ return;
+}
+
+/***********************************************************************
+* find_pivot - choose a pivot element
+*
+* This routine chooses a pivot element v[p,q] in the active submatrix
+* of matrix U = P*V*Q.
+*
+* It is assumed that on entry the matrix U has the following partially
+* triangularized form:
+*
+* 1 k n
+* 1 x x x x x x x x x x
+* . x x x x x x x x x
+* . . x x x x x x x x
+* . . . x x x x x x x
+* k . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* . . . . * * * * * *
+* n . . . . * * * * * *
+*
+* where rows and columns k, k+1, ..., n belong to the active submatrix
+* (elements of the active submatrix are marked by '*').
+*
+* Since the matrix U = P*V*Q is not stored, the routine works with the
+* matrix V. It is assumed that the row-wise representation corresponds
+* to the matrix V, but the column-wise representation corresponds to
+* the active submatrix of the matrix V, i.e. elements of the matrix V,
+* which does not belong to the active submatrix, are missing from the
+* column linked lists. It is also assumed that each active row of the
+* matrix V is in the set R[len], where len is number of non-zeros in
+* the row, and each active column of the matrix V is in the set C[len],
+* where len is number of non-zeros in the column (in the latter case
+* only elements of the active submatrix are counted; such elements are
+* marked by '*' on the figure above).
+*
+* Due to exact arithmetic any non-zero element of the active submatrix
+* can be chosen as a pivot. However, to keep sparsity of the matrix V
+* the routine uses Markowitz strategy, trying to choose such element
+* v[p,q], which has smallest Markowitz cost (nr[p]-1) * (nc[q]-1),
+* where nr[p] and nc[q] are the number of non-zero elements, resp., in
+* p-th row and in q-th column of the active submatrix.
+*
+* In order to reduce the search, i.e. not to walk through all elements
+* of the active submatrix, the routine exploits a technique proposed by
+* I.Duff. This technique is based on using the sets R[len] and C[len]
+* of active rows and columns.
+*
+* On exit the routine returns a pointer to a pivot v[p,q] chosen, or
+* NULL, if the active submatrix is empty. */
+
+static LUXELM *find_pivot(LUX *lux, LUXWKA *wka)
+{ int n = lux->n;
+ LUXELM **V_row = lux->V_row;
+ LUXELM **V_col = lux->V_col;
+ int *R_len = wka->R_len;
+ int *R_head = wka->R_head;
+ int *R_next = wka->R_next;
+ int *C_len = wka->C_len;
+ int *C_head = wka->C_head;
+ int *C_next = wka->C_next;
+ LUXELM *piv, *some, *vij;
+ int i, j, len, min_len, ncand, piv_lim = 5;
+ double best, cost;
+ /* nothing is chosen so far */
+ piv = NULL, best = DBL_MAX, ncand = 0;
+ /* if in the active submatrix there is a column that has the only
+ non-zero (column singleton), choose it as a pivot */
+ j = C_head[1];
+ if (j != 0)
+ { xassert(C_len[j] == 1);
+ piv = V_col[j];
+ xassert(piv != NULL && piv->c_next == NULL);
+ goto done;
+ }
+ /* if in the active submatrix there is a row that has the only
+ non-zero (row singleton), choose it as a pivot */
+ i = R_head[1];
+ if (i != 0)
+ { xassert(R_len[i] == 1);
+ piv = V_row[i];
+ xassert(piv != NULL && piv->r_next == NULL);
+ goto done;
+ }
+ /* there are no singletons in the active submatrix; walk through
+ other non-empty rows and columns */
+ for (len = 2; len <= n; len++)
+ { /* consider active columns having len non-zeros */
+ for (j = C_head[len]; j != 0; j = C_next[j])
+ { /* j-th column has len non-zeros */
+ /* find an element in the row of minimal length */
+ some = NULL, min_len = INT_MAX;
+ for (vij = V_col[j]; vij != NULL; vij = vij->c_next)
+ { if (min_len > R_len[vij->i])
+ some = vij, min_len = R_len[vij->i];
+ /* if Markowitz cost of this element is not greater than
+ (len-1)**2, it can be chosen right now; this heuristic
+ reduces the search and works well in many cases */
+ if (min_len <= len)
+ { piv = some;
+ goto done;
+ }
+ }
+ /* j-th column has been scanned */
+ /* the minimal element found is a next pivot candidate */
+ xassert(some != NULL);
+ ncand++;
+ /* compute its Markowitz cost */
+ cost = (double)(min_len - 1) * (double)(len - 1);
+ /* choose between the current candidate and this element */
+ if (cost < best) piv = some, best = cost;
+ /* if piv_lim candidates have been considered, there is a
+ doubt that a much better candidate exists; therefore it
+ is the time to terminate the search */
+ if (ncand == piv_lim) goto done;
+ }
+ /* now consider active rows having len non-zeros */
+ for (i = R_head[len]; i != 0; i = R_next[i])
+ { /* i-th row has len non-zeros */
+ /* find an element in the column of minimal length */
+ some = NULL, min_len = INT_MAX;
+ for (vij = V_row[i]; vij != NULL; vij = vij->r_next)
+ { if (min_len > C_len[vij->j])
+ some = vij, min_len = C_len[vij->j];
+ /* if Markowitz cost of this element is not greater than
+ (len-1)**2, it can be chosen right now; this heuristic
+ reduces the search and works well in many cases */
+ if (min_len <= len)
+ { piv = some;
+ goto done;
+ }
+ }
+ /* i-th row has been scanned */
+ /* the minimal element found is a next pivot candidate */
+ xassert(some != NULL);
+ ncand++;
+ /* compute its Markowitz cost */
+ cost = (double)(len - 1) * (double)(min_len - 1);
+ /* choose between the current candidate and this element */
+ if (cost < best) piv = some, best = cost;
+ /* if piv_lim candidates have been considered, there is a
+ doubt that a much better candidate exists; therefore it
+ is the time to terminate the search */
+ if (ncand == piv_lim) goto done;
+ }
+ }
+done: /* bring the pivot v[p,q] to the factorizing routine */
+ return piv;
+}
+
+/***********************************************************************
+* eliminate - perform gaussian elimination
+*
+* This routine performs elementary gaussian transformations in order
+* to eliminate subdiagonal elements in the k-th column of the matrix
+* U = P*V*Q using the pivot element u[k,k], where k is the number of
+* the current elimination step.
+*
+* The parameter piv specifies the pivot element v[p,q] = u[k,k].
+*
+* Each time when the routine applies the elementary transformation to
+* a non-pivot row of the matrix V, it stores the corresponding element
+* to the matrix F in order to keep the main equality A = F*V.
+*
+* The routine assumes that on entry the matrices L = P*F*inv(P) and
+* U = P*V*Q are the following:
+*
+* 1 k 1 k n
+* 1 1 . . . . . . . . . 1 x x x x x x x x x x
+* x 1 . . . . . . . . . x x x x x x x x x
+* x x 1 . . . . . . . . . x x x x x x x x
+* x x x 1 . . . . . . . . . x x x x x x x
+* k x x x x 1 . . . . . k . . . . * * * * * *
+* x x x x _ 1 . . . . . . . . # * * * * *
+* x x x x _ . 1 . . . . . . . # * * * * *
+* x x x x _ . . 1 . . . . . . # * * * * *
+* x x x x _ . . . 1 . . . . . # * * * * *
+* n x x x x _ . . . . 1 n . . . . # * * * * *
+*
+* matrix L matrix U
+*
+* where rows and columns of the matrix U with numbers k, k+1, ..., n
+* form the active submatrix (eliminated elements are marked by '#' and
+* other elements of the active submatrix are marked by '*'). Note that
+* each eliminated non-zero element u[i,k] of the matrix U gives the
+* corresponding element l[i,k] of the matrix L (marked by '_').
+*
+* Actually all operations are performed on the matrix V. Should note
+* that the row-wise representation corresponds to the matrix V, but the
+* column-wise representation corresponds to the active submatrix of the
+* matrix V, i.e. elements of the matrix V, which doesn't belong to the
+* active submatrix, are missing from the column linked lists.
+*
+* Let u[k,k] = v[p,q] be the pivot. In order to eliminate subdiagonal
+* elements u[i',k] = v[i,q], i' = k+1, k+2, ..., n, the routine applies
+* the following elementary gaussian transformations:
+*
+* (i-th row of V) := (i-th row of V) - f[i,p] * (p-th row of V),
+*
+* where f[i,p] = v[i,q] / v[p,q] is a gaussian multiplier.
+*
+* Additionally, in order to keep the main equality A = F*V, each time
+* when the routine applies the transformation to i-th row of the matrix
+* V, it also adds f[i,p] as a new element to the matrix F.
+*
+* IMPORTANT: On entry the working arrays flag and work should contain
+* zeros. This status is provided by the routine on exit. */
+
+static void eliminate(LUX *lux, LUXWKA *wka, LUXELM *piv, int flag[],
+ mpq_t work[])
+{ DMP *pool = lux->pool;
+ LUXELM **F_row = lux->F_row;
+ LUXELM **F_col = lux->F_col;
+ mpq_t *V_piv = lux->V_piv;
+ LUXELM **V_row = lux->V_row;
+ LUXELM **V_col = lux->V_col;
+ int *R_len = wka->R_len;
+ int *R_head = wka->R_head;
+ int *R_prev = wka->R_prev;
+ int *R_next = wka->R_next;
+ int *C_len = wka->C_len;
+ int *C_head = wka->C_head;
+ int *C_prev = wka->C_prev;
+ int *C_next = wka->C_next;
+ LUXELM *fip, *vij, *vpj, *viq, *next;
+ mpq_t temp;
+ int i, j, p, q;
+ mpq_init(temp);
+ /* determine row and column indices of the pivot v[p,q] */
+ xassert(piv != NULL);
+ p = piv->i, q = piv->j;
+ /* remove p-th (pivot) row from the active set; it will never
+ return there */
+ if (R_prev[p] == 0)
+ R_head[R_len[p]] = R_next[p];
+ else
+ R_next[R_prev[p]] = R_next[p];
+ if (R_next[p] == 0)
+ ;
+ else
+ R_prev[R_next[p]] = R_prev[p];
+ /* remove q-th (pivot) column from the active set; it will never
+ return there */
+ if (C_prev[q] == 0)
+ C_head[C_len[q]] = C_next[q];
+ else
+ C_next[C_prev[q]] = C_next[q];
+ if (C_next[q] == 0)
+ ;
+ else
+ C_prev[C_next[q]] = C_prev[q];
+ /* store the pivot value in a separate array */
+ mpq_set(V_piv[p], piv->val);
+ /* remove the pivot from p-th row */
+ if (piv->r_prev == NULL)
+ V_row[p] = piv->r_next;
+ else
+ piv->r_prev->r_next = piv->r_next;
+ if (piv->r_next == NULL)
+ ;
+ else
+ piv->r_next->r_prev = piv->r_prev;
+ R_len[p]--;
+ /* remove the pivot from q-th column */
+ if (piv->c_prev == NULL)
+ V_col[q] = piv->c_next;
+ else
+ piv->c_prev->c_next = piv->c_next;
+ if (piv->c_next == NULL)
+ ;
+ else
+ piv->c_next->c_prev = piv->c_prev;
+ C_len[q]--;
+ /* free the space occupied by the pivot */
+ mpq_clear(piv->val);
+ dmp_free_atom(pool, piv, sizeof(LUXELM));
+ /* walk through p-th (pivot) row, which already does not contain
+ the pivot v[p,q], and do the following... */
+ for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
+ { /* get column index of v[p,j] */
+ j = vpj->j;
+ /* store v[p,j] in the working array */
+ flag[j] = 1;
+ mpq_set(work[j], vpj->val);
+ /* remove j-th column from the active set; it will return there
+ later with a new length */
+ if (C_prev[j] == 0)
+ C_head[C_len[j]] = C_next[j];
+ else
+ C_next[C_prev[j]] = C_next[j];
+ if (C_next[j] == 0)
+ ;
+ else
+ C_prev[C_next[j]] = C_prev[j];
+ /* v[p,j] leaves the active submatrix, so remove it from j-th
+ column; however, v[p,j] is kept in p-th row */
+ if (vpj->c_prev == NULL)
+ V_col[j] = vpj->c_next;
+ else
+ vpj->c_prev->c_next = vpj->c_next;
+ if (vpj->c_next == NULL)
+ ;
+ else
+ vpj->c_next->c_prev = vpj->c_prev;
+ C_len[j]--;
+ }
+ /* now walk through q-th (pivot) column, which already does not
+ contain the pivot v[p,q], and perform gaussian elimination */
+ while (V_col[q] != NULL)
+ { /* element v[i,q] has to be eliminated */
+ viq = V_col[q];
+ /* get row index of v[i,q] */
+ i = viq->i;
+ /* remove i-th row from the active set; later it will return
+ there with a new length */
+ if (R_prev[i] == 0)
+ R_head[R_len[i]] = R_next[i];
+ else
+ R_next[R_prev[i]] = R_next[i];
+ if (R_next[i] == 0)
+ ;
+ else
+ R_prev[R_next[i]] = R_prev[i];
+ /* compute gaussian multiplier f[i,p] = v[i,q] / v[p,q] and
+ store it in the matrix F */
+ fip = dmp_get_atom(pool, sizeof(LUXELM));
+ fip->i = i, fip->j = p;
+ mpq_init(fip->val);
+ mpq_div(fip->val, viq->val, V_piv[p]);
+ fip->r_prev = NULL;
+ fip->r_next = F_row[i];
+ fip->c_prev = NULL;
+ fip->c_next = F_col[p];
+ if (fip->r_next != NULL) fip->r_next->r_prev = fip;
+ if (fip->c_next != NULL) fip->c_next->c_prev = fip;
+ F_row[i] = F_col[p] = fip;
+ /* v[i,q] has to be eliminated, so remove it from i-th row */
+ if (viq->r_prev == NULL)
+ V_row[i] = viq->r_next;
+ else
+ viq->r_prev->r_next = viq->r_next;
+ if (viq->r_next == NULL)
+ ;
+ else
+ viq->r_next->r_prev = viq->r_prev;
+ R_len[i]--;
+ /* and also from q-th column */
+ V_col[q] = viq->c_next;
+ C_len[q]--;
+ /* free the space occupied by v[i,q] */
+ mpq_clear(viq->val);
+ dmp_free_atom(pool, viq, sizeof(LUXELM));
+ /* perform gaussian transformation:
+ (i-th row) := (i-th row) - f[i,p] * (p-th row)
+ note that now p-th row, which is in the working array,
+ does not contain the pivot v[p,q], and i-th row does not
+ contain the element v[i,q] to be eliminated */
+ /* walk through i-th row and transform existing non-zero
+ elements */
+ for (vij = V_row[i]; vij != NULL; vij = next)
+ { next = vij->r_next;
+ /* get column index of v[i,j] */
+ j = vij->j;
+ /* v[i,j] := v[i,j] - f[i,p] * v[p,j] */
+ if (flag[j])
+ { /* v[p,j] != 0 */
+ flag[j] = 0;
+ mpq_mul(temp, fip->val, work[j]);
+ mpq_sub(vij->val, vij->val, temp);
+ if (mpq_sgn(vij->val) == 0)
+ { /* new v[i,j] is zero, so remove it from the active
+ submatrix */
+ /* remove v[i,j] from i-th row */
+ if (vij->r_prev == NULL)
+ V_row[i] = vij->r_next;
+ else
+ vij->r_prev->r_next = vij->r_next;
+ if (vij->r_next == NULL)
+ ;
+ else
+ vij->r_next->r_prev = vij->r_prev;
+ R_len[i]--;
+ /* remove v[i,j] from j-th column */
+ if (vij->c_prev == NULL)
+ V_col[j] = vij->c_next;
+ else
+ vij->c_prev->c_next = vij->c_next;
+ if (vij->c_next == NULL)
+ ;
+ else
+ vij->c_next->c_prev = vij->c_prev;
+ C_len[j]--;
+ /* free the space occupied by v[i,j] */
+ mpq_clear(vij->val);
+ dmp_free_atom(pool, vij, sizeof(LUXELM));
+ }
+ }
+ }
+ /* now flag is the pattern of the set v[p,*] \ v[i,*] */
+ /* walk through p-th (pivot) row and create new elements in
+ i-th row, which appear due to fill-in */
+ for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
+ { j = vpj->j;
+ if (flag[j])
+ { /* create new non-zero v[i,j] = 0 - f[i,p] * v[p,j] and
+ add it to i-th row and j-th column */
+ vij = dmp_get_atom(pool, sizeof(LUXELM));
+ vij->i = i, vij->j = j;
+ mpq_init(vij->val);
+ mpq_mul(vij->val, fip->val, work[j]);
+ mpq_neg(vij->val, vij->val);
+ vij->r_prev = NULL;
+ vij->r_next = V_row[i];
+ vij->c_prev = NULL;
+ vij->c_next = V_col[j];
+ if (vij->r_next != NULL) vij->r_next->r_prev = vij;
+ if (vij->c_next != NULL) vij->c_next->c_prev = vij;
+ V_row[i] = V_col[j] = vij;
+ R_len[i]++, C_len[j]++;
+ }
+ else
+ { /* there is no fill-in, because v[i,j] already exists in
+ i-th row; restore the flag, which was reset before */
+ flag[j] = 1;
+ }
+ }
+ /* now i-th row has been completely transformed and can return
+ to the active set with a new length */
+ R_prev[i] = 0;
+ R_next[i] = R_head[R_len[i]];
+ if (R_next[i] != 0) R_prev[R_next[i]] = i;
+ R_head[R_len[i]] = i;
+ }
+ /* at this point q-th (pivot) column must be empty */
+ xassert(C_len[q] == 0);
+ /* walk through p-th (pivot) row again and do the following... */
+ for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
+ { /* get column index of v[p,j] */
+ j = vpj->j;
+ /* erase v[p,j] from the working array */
+ flag[j] = 0;
+ mpq_set_si(work[j], 0, 1);
+ /* now j-th column has been completely transformed, so it can
+ return to the active list with a new length */
+ C_prev[j] = 0;
+ C_next[j] = C_head[C_len[j]];
+ if (C_next[j] != 0) C_prev[C_next[j]] = j;
+ C_head[C_len[j]] = j;
+ }
+ mpq_clear(temp);
+ /* return to the factorizing routine */
+ return;
+}
+
+/***********************************************************************
+* lux_decomp - compute LU-factorization
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* int lux_decomp(LUX *lux, int (*col)(void *info, int j, int ind[],
+* mpq_t val[]), void *info);
+*
+* DESCRIPTION
+*
+* The routine lux_decomp computes LU-factorization of a given square
+* matrix A.
+*
+* The parameter lux specifies LU-factorization data structure built by
+* means of the routine lux_create.
+*
+* The formal routine col specifies the original matrix A. In order to
+* obtain j-th column of the matrix A the routine lux_decomp calls the
+* routine col with the parameter j (1 <= j <= n, where n is the order
+* of A). In response the routine col should store row indices and
+* numerical values of non-zero elements of j-th column of A to the
+* locations ind[1], ..., ind[len] and val[1], ..., val[len], resp.,
+* where len is the number of non-zeros in j-th column, which should be
+* returned on exit. Neiter zero nor duplicate elements are allowed.
+*
+* The parameter info is a transit pointer passed to the formal routine
+* col; it can be used for various purposes.
+*
+* RETURNS
+*
+* The routine lux_decomp returns the singularity flag. Zero flag means
+* that the original matrix A is non-singular while non-zero flag means
+* that A is (exactly!) singular.
+*
+* Note that LU-factorization is valid in both cases, however, in case
+* of singularity some rows of the matrix V (including pivot elements)
+* will be empty.
+*
+* REPAIRING SINGULAR MATRIX
+*
+* If the routine lux_decomp returns non-zero flag, it provides all
+* necessary information that can be used for "repairing" the matrix A,
+* where "repairing" means replacing linearly dependent columns of the
+* matrix A by appropriate columns of the unity matrix. This feature is
+* needed when the routine lux_decomp is used for reinverting the basis
+* matrix within the simplex method procedure.
+*
+* On exit linearly dependent columns of the matrix U have the numbers
+* rank+1, rank+2, ..., n, where rank is the exact rank of the matrix A
+* stored by the routine to the member lux->rank. The correspondence
+* between columns of A and U is the same as between columns of V and U.
+* Thus, linearly dependent columns of the matrix A have the numbers
+* Q_col[rank+1], Q_col[rank+2], ..., Q_col[n], where Q_col is an array
+* representing the permutation matrix Q in column-like format. It is
+* understood that each j-th linearly dependent column of the matrix U
+* should be replaced by the unity vector, where all elements are zero
+* except the unity diagonal element u[j,j]. On the other hand j-th row
+* of the matrix U corresponds to the row of the matrix V (and therefore
+* of the matrix A) with the number P_row[j], where P_row is an array
+* representing the permutation matrix P in row-like format. Thus, each
+* j-th linearly dependent column of the matrix U should be replaced by
+* a column of the unity matrix with the number P_row[j].
+*
+* The code that repairs the matrix A may look like follows:
+*
+* for (j = rank+1; j <= n; j++)
+* { replace column Q_col[j] of the matrix A by column P_row[j] of
+* the unity matrix;
+* }
+*
+* where rank, P_row, and Q_col are members of the structure LUX. */
+
+int lux_decomp(LUX *lux, int (*col)(void *info, int j, int ind[],
+ mpq_t val[]), void *info)
+{ int n = lux->n;
+ LUXELM **V_row = lux->V_row;
+ LUXELM **V_col = lux->V_col;
+ int *P_row = lux->P_row;
+ int *P_col = lux->P_col;
+ int *Q_row = lux->Q_row;
+ int *Q_col = lux->Q_col;
+ LUXELM *piv, *vij;
+ LUXWKA *wka;
+ int i, j, k, p, q, t, *flag;
+ mpq_t *work;
+ /* allocate working area */
+ wka = xmalloc(sizeof(LUXWKA));
+ wka->R_len = xcalloc(1+n, sizeof(int));
+ wka->R_head = xcalloc(1+n, sizeof(int));
+ wka->R_prev = xcalloc(1+n, sizeof(int));
+ wka->R_next = xcalloc(1+n, sizeof(int));
+ wka->C_len = xcalloc(1+n, sizeof(int));
+ wka->C_head = xcalloc(1+n, sizeof(int));
+ wka->C_prev = xcalloc(1+n, sizeof(int));
+ wka->C_next = xcalloc(1+n, sizeof(int));
+ /* initialize LU-factorization data structures */
+ initialize(lux, col, info, wka);
+ /* allocate working arrays */
+ flag = xcalloc(1+n, sizeof(int));
+ work = xcalloc(1+n, sizeof(mpq_t));
+ for (k = 1; k <= n; k++)
+ { flag[k] = 0;
+ mpq_init(work[k]);
+ }
+ /* main elimination loop */
+ for (k = 1; k <= n; k++)
+ { /* choose a pivot element v[p,q] */
+ piv = find_pivot(lux, wka);
+ if (piv == NULL)
+ { /* no pivot can be chosen, because the active submatrix is
+ empty */
+ break;
+ }
+ /* determine row and column indices of the pivot element */
+ p = piv->i, q = piv->j;
+ /* let v[p,q] correspond to u[i',j']; permute k-th and i'-th
+ rows and k-th and j'-th columns of the matrix U = P*V*Q to
+ move the element u[i',j'] to the position u[k,k] */
+ i = P_col[p], j = Q_row[q];
+ xassert(k <= i && i <= n && k <= j && j <= n);
+ /* permute k-th and i-th rows of the matrix U */
+ t = P_row[k];
+ P_row[i] = t, P_col[t] = i;
+ P_row[k] = p, P_col[p] = k;
+ /* permute k-th and j-th columns of the matrix U */
+ t = Q_col[k];
+ Q_col[j] = t, Q_row[t] = j;
+ Q_col[k] = q, Q_row[q] = k;
+ /* eliminate subdiagonal elements of k-th column of the matrix
+ U = P*V*Q using the pivot element u[k,k] = v[p,q] */
+ eliminate(lux, wka, piv, flag, work);
+ }
+ /* determine the rank of A (and V) */
+ lux->rank = k - 1;
+ /* free working arrays */
+ xfree(flag);
+ for (k = 1; k <= n; k++) mpq_clear(work[k]);
+ xfree(work);
+ /* build column lists of the matrix V using its row lists */
+ for (j = 1; j <= n; j++)
+ xassert(V_col[j] == NULL);
+ for (i = 1; i <= n; i++)
+ { for (vij = V_row[i]; vij != NULL; vij = vij->r_next)
+ { j = vij->j;
+ vij->c_prev = NULL;
+ vij->c_next = V_col[j];
+ if (vij->c_next != NULL) vij->c_next->c_prev = vij;
+ V_col[j] = vij;
+ }
+ }
+ /* free working area */
+ xfree(wka->R_len);
+ xfree(wka->R_head);
+ xfree(wka->R_prev);
+ xfree(wka->R_next);
+ xfree(wka->C_len);
+ xfree(wka->C_head);
+ xfree(wka->C_prev);
+ xfree(wka->C_next);
+ xfree(wka);
+ /* return to the calling program */
+ return (lux->rank < n);
+}
+
+/***********************************************************************
+* lux_f_solve - solve system F*x = b or F'*x = b
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* void lux_f_solve(LUX *lux, int tr, mpq_t x[]);
+*
+* DESCRIPTION
+*
+* The routine lux_f_solve solves either the system F*x = b (if the
+* flag tr is zero) or the system F'*x = b (if the flag tr is non-zero),
+* where the matrix F is a component of LU-factorization specified by
+* the parameter lux, F' is a matrix transposed to F.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix F. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void lux_f_solve(LUX *lux, int tr, mpq_t x[])
+{ int n = lux->n;
+ LUXELM **F_row = lux->F_row;
+ LUXELM **F_col = lux->F_col;
+ int *P_row = lux->P_row;
+ LUXELM *fik, *fkj;
+ int i, j, k;
+ mpq_t temp;
+ mpq_init(temp);
+ if (!tr)
+ { /* solve the system F*x = b */
+ for (j = 1; j <= n; j++)
+ { k = P_row[j];
+ if (mpq_sgn(x[k]) != 0)
+ { for (fik = F_col[k]; fik != NULL; fik = fik->c_next)
+ { mpq_mul(temp, fik->val, x[k]);
+ mpq_sub(x[fik->i], x[fik->i], temp);
+ }
+ }
+ }
+ }
+ else
+ { /* solve the system F'*x = b */
+ for (i = n; i >= 1; i--)
+ { k = P_row[i];
+ if (mpq_sgn(x[k]) != 0)
+ { for (fkj = F_row[k]; fkj != NULL; fkj = fkj->r_next)
+ { mpq_mul(temp, fkj->val, x[k]);
+ mpq_sub(x[fkj->j], x[fkj->j], temp);
+ }
+ }
+ }
+ }
+ mpq_clear(temp);
+ return;
+}
+
+/***********************************************************************
+* lux_v_solve - solve system V*x = b or V'*x = b
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* void lux_v_solve(LUX *lux, int tr, double x[]);
+*
+* DESCRIPTION
+*
+* The routine lux_v_solve solves either the system V*x = b (if the
+* flag tr is zero) or the system V'*x = b (if the flag tr is non-zero),
+* where the matrix V is a component of LU-factorization specified by
+* the parameter lux, V' is a matrix transposed to V.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix V. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void lux_v_solve(LUX *lux, int tr, mpq_t x[])
+{ int n = lux->n;
+ mpq_t *V_piv = lux->V_piv;
+ LUXELM **V_row = lux->V_row;
+ LUXELM **V_col = lux->V_col;
+ int *P_row = lux->P_row;
+ int *Q_col = lux->Q_col;
+ LUXELM *vij;
+ int i, j, k;
+ mpq_t *b, temp;
+ b = xcalloc(1+n, sizeof(mpq_t));
+ for (k = 1; k <= n; k++)
+ mpq_init(b[k]), mpq_set(b[k], x[k]), mpq_set_si(x[k], 0, 1);
+ mpq_init(temp);
+ if (!tr)
+ { /* solve the system V*x = b */
+ for (k = n; k >= 1; k--)
+ { i = P_row[k], j = Q_col[k];
+ if (mpq_sgn(b[i]) != 0)
+ { mpq_set(x[j], b[i]);
+ mpq_div(x[j], x[j], V_piv[i]);
+ for (vij = V_col[j]; vij != NULL; vij = vij->c_next)
+ { mpq_mul(temp, vij->val, x[j]);
+ mpq_sub(b[vij->i], b[vij->i], temp);
+ }
+ }
+ }
+ }
+ else
+ { /* solve the system V'*x = b */
+ for (k = 1; k <= n; k++)
+ { i = P_row[k], j = Q_col[k];
+ if (mpq_sgn(b[j]) != 0)
+ { mpq_set(x[i], b[j]);
+ mpq_div(x[i], x[i], V_piv[i]);
+ for (vij = V_row[i]; vij != NULL; vij = vij->r_next)
+ { mpq_mul(temp, vij->val, x[i]);
+ mpq_sub(b[vij->j], b[vij->j], temp);
+ }
+ }
+ }
+ }
+ for (k = 1; k <= n; k++) mpq_clear(b[k]);
+ mpq_clear(temp);
+ xfree(b);
+ return;
+}
+
+/***********************************************************************
+* lux_solve - solve system A*x = b or A'*x = b
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* void lux_solve(LUX *lux, int tr, mpq_t x[]);
+*
+* DESCRIPTION
+*
+* The routine lux_solve solves either the system A*x = b (if the flag
+* tr is zero) or the system A'*x = b (if the flag tr is non-zero),
+* where the parameter lux specifies LU-factorization of the matrix A,
+* A' is a matrix transposed to A.
+*
+* On entry the array x should contain elements of the right-hand side
+* vector b in locations x[1], ..., x[n], where n is the order of the
+* matrix A. On exit this array will contain elements of the solution
+* vector x in the same locations. */
+
+void lux_solve(LUX *lux, int tr, mpq_t x[])
+{ if (lux->rank < lux->n)
+ xfault("lux_solve: LU-factorization has incomplete rank\n");
+ if (!tr)
+ { /* A = F*V, therefore inv(A) = inv(V)*inv(F) */
+ lux_f_solve(lux, 0, x);
+ lux_v_solve(lux, 0, x);
+ }
+ else
+ { /* A' = V'*F', therefore inv(A') = inv(F')*inv(V') */
+ lux_v_solve(lux, 1, x);
+ lux_f_solve(lux, 1, x);
+ }
+ return;
+}
+
+/***********************************************************************
+* lux_delete - delete LU-factorization
+*
+* SYNOPSIS
+*
+* #include "lux.h"
+* void lux_delete(LUX *lux);
+*
+* DESCRIPTION
+*
+* The routine lux_delete deletes LU-factorization data structure,
+* which the parameter lux points to, freeing all the memory allocated
+* to this object. */
+
+void lux_delete(LUX *lux)
+{ int n = lux->n;
+ LUXELM *fij, *vij;
+ int i;
+ for (i = 1; i <= n; i++)
+ { for (fij = lux->F_row[i]; fij != NULL; fij = fij->r_next)
+ mpq_clear(fij->val);
+ mpq_clear(lux->V_piv[i]);
+ for (vij = lux->V_row[i]; vij != NULL; vij = vij->r_next)
+ mpq_clear(vij->val);
+ }
+ dmp_delete_pool(lux->pool);
+ xfree(lux->F_row);
+ xfree(lux->F_col);
+ xfree(lux->V_piv);
+ xfree(lux->V_row);
+ xfree(lux->V_col);
+ xfree(lux->P_row);
+ xfree(lux->P_col);
+ xfree(lux->Q_row);
+ xfree(lux->Q_col);
+ xfree(lux);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/draft/lux.h b/test/monniaux/glpk-4.65/src/draft/lux.h
new file mode 100644
index 00000000..8767bb8e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/draft/lux.h
@@ -0,0 +1,220 @@
+/* lux.h (LU-factorization, rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef LUX_H
+#define LUX_H
+
+#include "dmp.h"
+#include "mygmp.h"
+
+/***********************************************************************
+* The structure LUX defines LU-factorization of a square matrix A,
+* which is the following quartet:
+*
+* [A] = (F, V, P, Q), (1)
+*
+* where F and V are such matrices that
+*
+* A = F * V, (2)
+*
+* and P and Q are such permutation matrices that the matrix
+*
+* L = P * F * inv(P) (3)
+*
+* is lower triangular with unity diagonal, and the matrix
+*
+* U = P * V * Q (4)
+*
+* is upper triangular. All the matrices have the order n.
+*
+* The matrices F and V are stored in row/column-wise sparse format as
+* row and column linked lists of non-zero elements. Unity elements on
+* the main diagonal of the matrix F are not stored. Pivot elements of
+* the matrix V (that correspond to diagonal elements of the matrix U)
+* are also missing from the row and column lists and stored separately
+* in an ordinary array.
+*
+* The permutation matrices P and Q are stored as ordinary arrays using
+* both row- and column-like formats.
+*
+* The matrices L and U being completely defined by the matrices F, V,
+* P, and Q are not stored explicitly.
+*
+* It is easy to show that the factorization (1)-(3) is some version of
+* LU-factorization. Indeed, from (3) and (4) it follows that:
+*
+* F = inv(P) * L * P,
+*
+* V = inv(P) * U * inv(Q),
+*
+* and substitution into (2) gives:
+*
+* A = F * V = inv(P) * L * U * inv(Q).
+*
+* For more details see the program documentation. */
+
+typedef struct LUX LUX;
+typedef struct LUXELM LUXELM;
+typedef struct LUXWKA LUXWKA;
+
+struct LUX
+{ /* LU-factorization of a square matrix */
+ int n;
+ /* the order of matrices A, F, V, P, Q */
+ DMP *pool;
+ /* memory pool for elements of matrices F and V */
+ LUXELM **F_row; /* LUXELM *F_row[1+n]; */
+ /* F_row[0] is not used;
+ F_row[i], 1 <= i <= n, is a pointer to the list of elements in
+ i-th row of matrix F (diagonal elements are not stored) */
+ LUXELM **F_col; /* LUXELM *F_col[1+n]; */
+ /* F_col[0] is not used;
+ F_col[j], 1 <= j <= n, is a pointer to the list of elements in
+ j-th column of matrix F (diagonal elements are not stored) */
+ mpq_t *V_piv; /* mpq_t V_piv[1+n]; */
+ /* V_piv[0] is not used;
+ V_piv[p], 1 <= p <= n, is a pivot element v[p,q] corresponding
+ to a diagonal element u[k,k] of matrix U = P*V*Q (used on k-th
+ elimination step, k = 1, 2, ..., n) */
+ LUXELM **V_row; /* LUXELM *V_row[1+n]; */
+ /* V_row[0] is not used;
+ V_row[i], 1 <= i <= n, is a pointer to the list of elements in
+ i-th row of matrix V (except pivot elements) */
+ LUXELM **V_col; /* LUXELM *V_col[1+n]; */
+ /* V_col[0] is not used;
+ V_col[j], 1 <= j <= n, is a pointer to the list of elements in
+ j-th column of matrix V (except pivot elements) */
+ int *P_row; /* int P_row[1+n]; */
+ /* P_row[0] is not used;
+ P_row[i] = j means that p[i,j] = 1, where p[i,j] is an element
+ of permutation matrix P */
+ int *P_col; /* int P_col[1+n]; */
+ /* P_col[0] is not used;
+ P_col[j] = i means that p[i,j] = 1, where p[i,j] is an element
+ of permutation matrix P */
+ /* if i-th row or column of matrix F is i'-th row or column of
+ matrix L = P*F*inv(P), or if i-th row of matrix V is i'-th row
+ of matrix U = P*V*Q, then P_row[i'] = i and P_col[i] = i' */
+ int *Q_row; /* int Q_row[1+n]; */
+ /* Q_row[0] is not used;
+ Q_row[i] = j means that q[i,j] = 1, where q[i,j] is an element
+ of permutation matrix Q */
+ int *Q_col; /* int Q_col[1+n]; */
+ /* Q_col[0] is not used;
+ Q_col[j] = i means that q[i,j] = 1, where q[i,j] is an element
+ of permutation matrix Q */
+ /* if j-th column of matrix V is j'-th column of matrix U = P*V*Q,
+ then Q_row[j] = j' and Q_col[j'] = j */
+ int rank;
+ /* the (exact) rank of matrices A and V */
+};
+
+struct LUXELM
+{ /* element of matrix F or V */
+ int i;
+ /* row index, 1 <= i <= m */
+ int j;
+ /* column index, 1 <= j <= n */
+ mpq_t val;
+ /* numeric (non-zero) element value */
+ LUXELM *r_prev;
+ /* pointer to previous element in the same row */
+ LUXELM *r_next;
+ /* pointer to next element in the same row */
+ LUXELM *c_prev;
+ /* pointer to previous element in the same column */
+ LUXELM *c_next;
+ /* pointer to next element in the same column */
+};
+
+struct LUXWKA
+{ /* working area (used only during factorization) */
+ /* in order to efficiently implement Markowitz strategy and Duff
+ search technique there are two families {R[0], R[1], ..., R[n]}
+ and {C[0], C[1], ..., C[n]}; member R[k] is a set of active
+ rows of matrix V having k non-zeros, and member C[k] is a set
+ of active columns of matrix V having k non-zeros (in the active
+ submatrix); each set R[k] and C[k] is implemented as a separate
+ doubly linked list */
+ int *R_len; /* int R_len[1+n]; */
+ /* R_len[0] is not used;
+ R_len[i], 1 <= i <= n, is the number of non-zero elements in
+ i-th row of matrix V (that is the length of i-th row) */
+ int *R_head; /* int R_head[1+n]; */
+ /* R_head[k], 0 <= k <= n, is the number of a first row, which is
+ active and whose length is k */
+ int *R_prev; /* int R_prev[1+n]; */
+ /* R_prev[0] is not used;
+ R_prev[i], 1 <= i <= n, is the number of a previous row, which
+ is active and has the same length as i-th row */
+ int *R_next; /* int R_next[1+n]; */
+ /* R_prev[0] is not used;
+ R_prev[i], 1 <= i <= n, is the number of a next row, which is
+ active and has the same length as i-th row */
+ int *C_len; /* int C_len[1+n]; */
+ /* C_len[0] is not used;
+ C_len[j], 1 <= j <= n, is the number of non-zero elements in
+ j-th column of the active submatrix of matrix V (that is the
+ length of j-th column in the active submatrix) */
+ int *C_head; /* int C_head[1+n]; */
+ /* C_head[k], 0 <= k <= n, is the number of a first column, which
+ is active and whose length is k */
+ int *C_prev; /* int C_prev[1+n]; */
+ /* C_prev[0] is not used;
+ C_prev[j], 1 <= j <= n, is the number of a previous column,
+ which is active and has the same length as j-th column */
+ int *C_next; /* int C_next[1+n]; */
+ /* C_next[0] is not used;
+ C_next[j], 1 <= j <= n, is the number of a next column, which
+ is active and has the same length as j-th column */
+};
+
+#define lux_create _glp_lux_create
+LUX *lux_create(int n);
+/* create LU-factorization */
+
+#define lux_decomp _glp_lux_decomp
+int lux_decomp(LUX *lux, int (*col)(void *info, int j, int ind[],
+ mpq_t val[]), void *info);
+/* compute LU-factorization */
+
+#define lux_f_solve _glp_lux_f_solve
+void lux_f_solve(LUX *lux, int tr, mpq_t x[]);
+/* solve system F*x = b or F'*x = b */
+
+#define lux_v_solve _glp_lux_v_solve
+void lux_v_solve(LUX *lux, int tr, mpq_t x[]);
+/* solve system V*x = b or V'*x = b */
+
+#define lux_solve _glp_lux_solve
+void lux_solve(LUX *lux, int tr, mpq_t x[]);
+/* solve system A*x = b or A'*x = b */
+
+#define lux_delete _glp_lux_delete
+void lux_delete(LUX *lux);
+/* delete LU-factorization */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/alloc.c b/test/monniaux/glpk-4.65/src/env/alloc.c
new file mode 100644
index 00000000..8e2d613d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/alloc.c
@@ -0,0 +1,252 @@
+/* alloc.c (dynamic memory allocation) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+
+#define ALIGN 16
+/* some processors need data to be properly aligned, so this macro
+ * defines the alignment boundary, in bytes, provided by glpk memory
+ * allocation routines; looks like 16-byte alignment boundary is
+ * sufficient for all 32- and 64-bit platforms (8-byte boundary is not
+ * sufficient for some 64-bit platforms because of jmp_buf) */
+
+#define MBD_SIZE (((sizeof(MBD) + (ALIGN - 1)) / ALIGN) * ALIGN)
+/* size of memory block descriptor, in bytes, rounded up to multiple
+ * of the alignment boundary */
+
+/***********************************************************************
+* dma - dynamic memory allocation (basic routine)
+*
+* This routine performs dynamic memory allocation. It is similar to
+* the standard realloc function, however, it provides every allocated
+* memory block with a descriptor, which is used for sanity checks on
+* reallocating/freeing previously allocated memory blocks as well as
+* for book-keeping the memory usage statistics. */
+
+static void *dma(const char *func, void *ptr, size_t size)
+{ ENV *env = get_env_ptr();
+ MBD *mbd;
+ if (ptr == NULL)
+ { /* new memory block will be allocated */
+ mbd = NULL;
+ }
+ else
+ { /* allocated memory block will be reallocated or freed */
+ /* get pointer to the block descriptor */
+ mbd = (MBD *)((char *)ptr - MBD_SIZE);
+ /* make sure that the block descriptor is valid */
+ if (mbd->self != mbd)
+ xerror("%s: ptr = %p; invalid pointer\n", func, ptr);
+ /* remove the block from the linked list */
+ mbd->self = NULL;
+ if (mbd->prev == NULL)
+ env->mem_ptr = mbd->next;
+ else
+ mbd->prev->next = mbd->next;
+ if (mbd->next == NULL)
+ ;
+ else
+ mbd->next->prev = mbd->prev;
+ /* decrease usage counts */
+ if (!(env->mem_count >= 1 && env->mem_total >= mbd->size))
+ xerror("%s: memory allocation error\n", func);
+ env->mem_count--;
+ env->mem_total -= mbd->size;
+ if (size == 0)
+ { /* free the memory block */
+ free(mbd);
+ return NULL;
+ }
+ }
+ /* allocate/reallocate memory block */
+ if (size > SIZE_T_MAX - MBD_SIZE)
+ xerror("%s: block too large\n", func);
+ size += MBD_SIZE;
+ if (size > env->mem_limit - env->mem_total)
+ xerror("%s: memory allocation limit exceeded\n", func);
+ if (env->mem_count == INT_MAX)
+ xerror("%s: too many memory blocks allocated\n", func);
+ mbd = (mbd == NULL ? malloc(size) : realloc(mbd, size));
+ if (mbd == NULL)
+ xerror("%s: no memory available\n", func);
+ /* setup the block descriptor */
+ mbd->size = size;
+ mbd->self = mbd;
+ mbd->prev = NULL;
+ mbd->next = env->mem_ptr;
+ /* add the block to the beginning of the linked list */
+ if (mbd->next != NULL)
+ mbd->next->prev = mbd;
+ env->mem_ptr = mbd;
+ /* increase usage counts */
+ env->mem_count++;
+ if (env->mem_cpeak < env->mem_count)
+ env->mem_cpeak = env->mem_count;
+ env->mem_total += size;
+ if (env->mem_tpeak < env->mem_total)
+ env->mem_tpeak = env->mem_total;
+ return (char *)mbd + MBD_SIZE;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_alloc - allocate memory block
+*
+* SYNOPSIS
+*
+* void *glp_alloc(int n, int size);
+*
+* DESCRIPTION
+*
+* The routine glp_alloc allocates a memory block of n * size bytes
+* long.
+*
+* Note that being allocated the memory block contains arbitrary data
+* (not binary zeros!).
+*
+* RETURNS
+*
+* The routine glp_alloc returns a pointer to the block allocated.
+* To free this block the routine glp_free (not free!) must be used. */
+
+void *glp_alloc(int n, int size)
+{ if (n < 1)
+ xerror("glp_alloc: n = %d; invalid parameter\n", n);
+ if (size < 1)
+ xerror("glp_alloc: size = %d; invalid parameter\n", size);
+ if ((size_t)n > SIZE_T_MAX / (size_t)size)
+ xerror("glp_alloc: n = %d, size = %d; block too large\n",
+ n, size);
+ return dma("glp_alloc", NULL, (size_t)n * (size_t)size);
+}
+
+/**********************************************************************/
+
+void *glp_realloc(void *ptr, int n, int size)
+{ /* reallocate memory block */
+ if (ptr == NULL)
+ xerror("glp_realloc: ptr = %p; invalid pointer\n", ptr);
+ if (n < 1)
+ xerror("glp_realloc: n = %d; invalid parameter\n", n);
+ if (size < 1)
+ xerror("glp_realloc: size = %d; invalid parameter\n", size);
+ if ((size_t)n > SIZE_T_MAX / (size_t)size)
+ xerror("glp_realloc: n = %d, size = %d; block too large\n",
+ n, size);
+ return dma("glp_realloc", ptr, (size_t)n * (size_t)size);
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_free - free (deallocate) memory block
+*
+* SYNOPSIS
+*
+* void glp_free(void *ptr);
+*
+* DESCRIPTION
+*
+* The routine glp_free frees (deallocates) a memory block pointed to
+* by ptr, which was previuosly allocated by the routine glp_alloc or
+* reallocated by the routine glp_realloc. */
+
+void glp_free(void *ptr)
+{ if (ptr == NULL)
+ xerror("glp_free: ptr = %p; invalid pointer\n", ptr);
+ dma("glp_free", ptr, 0);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mem_limit - set memory usage limit
+*
+* SYNOPSIS
+*
+* void glp_mem_limit(int limit);
+*
+* DESCRIPTION
+*
+* The routine glp_mem_limit limits the amount of memory available for
+* dynamic allocation (in GLPK routines) to limit megabytes. */
+
+void glp_mem_limit(int limit)
+{ ENV *env = get_env_ptr();
+ if (limit < 1)
+ xerror("glp_mem_limit: limit = %d; invalid parameter\n",
+ limit);
+ if ((size_t)limit <= (SIZE_T_MAX >> 20))
+ env->mem_limit = (size_t)limit << 20;
+ else
+ env->mem_limit = SIZE_T_MAX;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mem_usage - get memory usage information
+*
+* SYNOPSIS
+*
+* void glp_mem_usage(int *count, int *cpeak, size_t *total,
+* size_t *tpeak);
+*
+* DESCRIPTION
+*
+* The routine glp_mem_usage reports some information about utilization
+* of the memory by GLPK routines. Information is stored to locations
+* specified by corresponding parameters (see below). Any parameter can
+* be specified as NULL, in which case its value is not stored.
+*
+* *count is the number of the memory blocks currently allocated by the
+* routines glp_malloc and glp_calloc (one call to glp_malloc or
+* glp_calloc results in allocating one memory block).
+*
+* *cpeak is the peak value of *count reached since the initialization
+* of the GLPK library environment.
+*
+* *total is the total amount, in bytes, of the memory blocks currently
+* allocated by the routines glp_malloc and glp_calloc.
+*
+* *tpeak is the peak value of *total reached since the initialization
+* of the GLPK library envirionment. */
+
+void glp_mem_usage(int *count, int *cpeak, size_t *total,
+ size_t *tpeak)
+{ ENV *env = get_env_ptr();
+ if (count != NULL)
+ *count = env->mem_count;
+ if (cpeak != NULL)
+ *cpeak = env->mem_cpeak;
+ if (total != NULL)
+ *total = env->mem_total;
+ if (tpeak != NULL)
+ *tpeak = env->mem_tpeak;
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/dlsup.c b/test/monniaux/glpk-4.65/src/env/dlsup.c
new file mode 100644
index 00000000..54c56c6d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/dlsup.c
@@ -0,0 +1,167 @@
+/* dlsup.c (dynamic linking support) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "env.h"
+
+/* GNU version ********************************************************/
+
+#if defined(HAVE_LTDL)
+
+#include <ltdl.h>
+
+void *xdlopen(const char *module)
+{ /* open dynamically linked library */
+ void *h = NULL;
+ if (lt_dlinit() != 0)
+ { put_err_msg(lt_dlerror());
+ goto done;
+ }
+ h = lt_dlopen(module);
+ if (h == NULL)
+ { put_err_msg(lt_dlerror());
+ if (lt_dlexit() != 0)
+ xerror("xdlopen: %s\n", lt_dlerror());
+ }
+done: return h;
+}
+
+void *xdlsym(void *h, const char *symbol)
+{ /* obtain address of symbol from dynamically linked library */
+ void *ptr;
+ xassert(h != NULL);
+ ptr = lt_dlsym(h, symbol);
+ if (ptr == NULL)
+ xerror("xdlsym: %s: %s\n", symbol, lt_dlerror());
+ return ptr;
+}
+
+void xdlclose(void *h)
+{ /* close dynamically linked library */
+ xassert(h != NULL);
+ if (lt_dlclose(h) != 0)
+ xerror("xdlclose: %s\n", lt_dlerror());
+ if (lt_dlexit() != 0)
+ xerror("xdlclose: %s\n", lt_dlerror());
+ return;
+}
+
+/* POSIX version ******************************************************/
+
+#elif defined(HAVE_DLFCN)
+
+#include <dlfcn.h>
+
+void *xdlopen(const char *module)
+{ /* open dynamically linked library */
+ void *h;
+ h = dlopen(module, RTLD_NOW);
+ if (h == NULL)
+ put_err_msg(dlerror());
+ return h;
+}
+
+void *xdlsym(void *h, const char *symbol)
+{ /* obtain address of symbol from dynamically linked library */
+ void *ptr;
+ xassert(h != NULL);
+ ptr = dlsym(h, symbol);
+ if (ptr == NULL)
+ xerror("xdlsym: %s: %s\n", symbol, dlerror());
+ return ptr;
+}
+
+void xdlclose(void *h)
+{ /* close dynamically linked library */
+ xassert(h != NULL);
+ if (dlclose(h) != 0)
+ xerror("xdlclose: %s\n", dlerror());
+ return;
+}
+
+/* MS Windows version *************************************************/
+
+#elif defined(__WOE__)
+
+#include <windows.h>
+
+void *xdlopen(const char *module)
+{ /* open dynamically linked library */
+ void *h;
+ h = LoadLibrary(module);
+ if (h == NULL)
+ { char msg[20];
+ sprintf(msg, "Error %d", GetLastError());
+ put_err_msg(msg);
+ }
+ return h;
+}
+
+void *xdlsym(void *h, const char *symbol)
+{ /* obtain address of symbol from dynamically linked library */
+ void *ptr;
+ xassert(h != NULL);
+ ptr = GetProcAddress(h, symbol);
+ if (ptr == NULL)
+ xerror("xdlsym: %s: Error %d\n", symbol, GetLastError());
+ return ptr;
+}
+
+void xdlclose(void *h)
+{ /* close dynamically linked library */
+ xassert(h != NULL);
+ if (!FreeLibrary(h))
+ xerror("xdlclose: Error %d\n", GetLastError());
+ return;
+}
+
+/* NULL version *******************************************************/
+
+#else
+
+void *xdlopen(const char *module)
+{ /* open dynamically linked library */
+ xassert(module == module);
+ put_err_msg("Shared libraries not supported");
+ return NULL;
+}
+
+void *xdlsym(void *h, const char *symbol)
+{ /* obtain address of symbol from dynamically linked library */
+ xassert(h != h);
+ xassert(symbol != symbol);
+ return NULL;
+}
+
+void xdlclose(void *h)
+{ /* close dynamically linked library */
+ xassert(h != h);
+ return;
+}
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/env.c b/test/monniaux/glpk-4.65/src/env/env.c
new file mode 100644
index 00000000..5b901f35
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/env.c
@@ -0,0 +1,316 @@
+/* env.c (GLPK environment initialization/termination) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "glpk.h"
+#include "env.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_init_env - initialize GLPK environment
+*
+* SYNOPSIS
+*
+* int glp_init_env(void);
+*
+* DESCRIPTION
+*
+* The routine glp_init_env initializes the GLPK environment. Normally
+* the application program does not need to call this routine, because
+* it is called automatically on the first call to any API routine.
+*
+* RETURNS
+*
+* The routine glp_init_env returns one of the following codes:
+*
+* 0 - initialization successful;
+* 1 - environment has been already initialized;
+* 2 - initialization failed (insufficient memory);
+* 3 - initialization failed (unsupported programming model). */
+
+int glp_init_env(void)
+{ ENV *env;
+ int ok;
+ /* check if the programming model is supported */
+ ok = (CHAR_BIT == 8 && sizeof(char) == 1 &&
+ sizeof(short) == 2 && sizeof(int) == 4 &&
+ (sizeof(void *) == 4 || sizeof(void *) == 8));
+ if (!ok)
+ return 3;
+ /* check if the environment is already initialized */
+ if (tls_get_ptr() != NULL)
+ return 1;
+ /* allocate and initialize the environment block */
+ env = malloc(sizeof(ENV));
+ if (env == NULL)
+ return 2;
+ memset(env, 0, sizeof(ENV));
+#if 0 /* 14/I-2017 */
+ sprintf(env->version, "%d.%d",
+ GLP_MAJOR_VERSION, GLP_MINOR_VERSION);
+#endif
+ env->self = env;
+ env->term_buf = malloc(TBUF_SIZE);
+ if (env->term_buf == NULL)
+ { free(env);
+ return 2;
+ }
+ env->term_out = GLP_ON;
+ env->term_hook = NULL;
+ env->term_info = NULL;
+ env->tee_file = NULL;
+#if 1 /* 23/XI-2015 */
+ env->err_st = 0;
+#endif
+ env->err_file = NULL;
+ env->err_line = 0;
+ env->err_hook = NULL;
+ env->err_info = NULL;
+ env->err_buf = malloc(EBUF_SIZE);
+ if (env->err_buf == NULL)
+ { free(env->term_buf);
+ free(env);
+ return 2;
+ }
+ env->err_buf[0] = '\0';
+ env->mem_limit = SIZE_T_MAX;
+ env->mem_ptr = NULL;
+ env->mem_count = env->mem_cpeak = 0;
+ env->mem_total = env->mem_tpeak = 0;
+#if 1 /* 23/XI-2015 */
+ env->gmp_pool = NULL;
+ env->gmp_size = 0;
+ env->gmp_work = NULL;
+#endif
+ env->h_odbc = env->h_mysql = NULL;
+ /* save pointer to the environment block */
+ tls_set_ptr(env);
+ /* initialization successful */
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* get_env_ptr - retrieve pointer to environment block
+*
+* SYNOPSIS
+*
+* #include "env.h"
+* ENV *get_env_ptr(void);
+*
+* DESCRIPTION
+*
+* The routine get_env_ptr retrieves and returns a pointer to the GLPK
+* environment block.
+*
+* If the GLPK environment has not been initialized yet, the routine
+* performs initialization. If initialization fails, the routine prints
+* an error message to stderr and terminates the program.
+*
+* RETURNS
+*
+* The routine returns a pointer to the environment block. */
+
+ENV *get_env_ptr(void)
+{ ENV *env = tls_get_ptr();
+ /* check if the environment has been initialized */
+ if (env == NULL)
+ { /* not initialized yet; perform initialization */
+ if (glp_init_env() != 0)
+ { /* initialization failed; display an error message */
+ fprintf(stderr, "GLPK initialization failed\n");
+ fflush(stderr);
+ /* and abnormally terminate the program */
+ abort();
+ }
+ /* initialization successful; retrieve the pointer */
+ env = tls_get_ptr();
+ }
+ /* check if the environment block is valid */
+ if (env->self != env)
+ { fprintf(stderr, "Invalid GLPK environment\n");
+ fflush(stderr);
+ abort();
+ }
+ return env;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_version - determine library version
+*
+* SYNOPSIS
+*
+* const char *glp_version(void);
+*
+* RETURNS
+*
+* The routine glp_version returns a pointer to a null-terminated
+* character string, which specifies the version of the GLPK library in
+* the form "X.Y", where X is the major version number, and Y is the
+* minor version number, for example, "4.16". */
+
+#define str(s) # s
+#define xstr(s) str(s)
+
+const char *glp_version(void)
+#if 0 /* 14/I-2017 */
+{ ENV *env = get_env_ptr();
+ return env->version;
+}
+#else /* suggested by Heinrich */
+{ return
+ xstr(GLP_MAJOR_VERSION) "." xstr(GLP_MINOR_VERSION);
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_config - determine library configuration
+*
+* SYNOPSIS
+*
+* const char *glp_config(const char *option);
+*
+* DESCRIPTION
+*
+* The routine glp_config determines some options which were specified
+* on configuring the GLPK library.
+*
+* RETURNS
+*
+* The routine glp_config returns a pointer to a null-terminating
+* string depending on the option inquired.
+*
+* For option = "TLS" the routine returns the thread local storage
+* class specifier used (e.g. "_Thread_local") if the GLPK library was
+* configured to run in multi-threaded environment, or NULL otherwise.
+*
+* For option = "ODBC_DLNAME" the routine returns the name of ODBC
+* shared library if this option was enabled, or NULL otherwise.
+*
+* For option = "MYSQL_DLNAME" the routine returns the name of MySQL
+* shared library if this option was enabled, or NULL otherwise. */
+
+const char *glp_config(const char *option)
+{ const char *s;
+ if (strcmp(option, "TLS") == 0)
+#ifndef TLS
+ s = NULL;
+#else
+ s = xstr(TLS);
+#endif
+ else if (strcmp(option, "ODBC_DLNAME") == 0)
+#ifndef ODBC_DLNAME
+ s = NULL;
+#else
+ s = ODBC_DLNAME;
+#endif
+ else if (strcmp(option, "MYSQL_DLNAME") == 0)
+#ifndef MYSQL_DLNAME
+ s = NULL;
+#else
+ s = MYSQL_DLNAME;
+#endif
+ else
+ { /* invalid option is always disabled */
+ s = NULL;
+ }
+ return s;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_free_env - free GLPK environment
+*
+* SYNOPSIS
+*
+* int glp_free_env(void);
+*
+* DESCRIPTION
+*
+* The routine glp_free_env frees all resources used by GLPK routines
+* (memory blocks, etc.) which are currently still in use.
+*
+* Normally the application program does not need to call this routine,
+* because GLPK routines always free all unused resources. However, if
+* the application program even has deleted all problem objects, there
+* will be several memory blocks still allocated for the library needs.
+* For some reasons the application program may want GLPK to free this
+* memory, in which case it should call glp_free_env.
+*
+* Note that a call to glp_free_env invalidates all problem objects as
+* if no GLPK routine were called.
+*
+* RETURNS
+*
+* 0 - termination successful;
+* 1 - environment is inactive (was not initialized). */
+
+int glp_free_env(void)
+{ ENV *env = tls_get_ptr();
+ MBD *desc;
+ /* check if the environment is active */
+ if (env == NULL)
+ return 1;
+ /* check if the environment block is valid */
+ if (env->self != env)
+ { fprintf(stderr, "Invalid GLPK environment\n");
+ fflush(stderr);
+ abort();
+ }
+ /* close handles to shared libraries */
+ if (env->h_odbc != NULL)
+ xdlclose(env->h_odbc);
+ if (env->h_mysql != NULL)
+ xdlclose(env->h_mysql);
+ /* free memory blocks which are still allocated */
+ while (env->mem_ptr != NULL)
+ { desc = env->mem_ptr;
+ env->mem_ptr = desc->next;
+ free(desc);
+ }
+ /* close text file used for copying terminal output */
+ if (env->tee_file != NULL)
+ fclose(env->tee_file);
+ /* invalidate the environment block */
+ env->self = NULL;
+ /* free memory allocated to the environment block */
+ free(env->term_buf);
+ free(env->err_buf);
+ free(env);
+ /* reset a pointer to the environment block */
+ tls_set_ptr(NULL);
+ /* termination successful */
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/env.h b/test/monniaux/glpk-4.65/src/env/env.h
new file mode 100644
index 00000000..67214ef6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/env.h
@@ -0,0 +1,274 @@
+/* env.h (GLPK environment) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef ENV_H
+#define ENV_H
+
+#include "stdc.h"
+
+typedef struct ENV ENV;
+typedef struct MBD MBD;
+
+#define SIZE_T_MAX (~(size_t)0)
+/* largest value of size_t type */
+
+#define TBUF_SIZE 4096
+/* terminal output buffer size, in bytes */
+
+#define EBUF_SIZE 1024
+/* error message buffer size, in bytes */
+
+/* enable/disable flag: */
+#define GLP_ON 1
+#define GLP_OFF 0
+
+struct ENV
+{ /* GLPK environment block */
+#if 0 /* 14/I-2007 */
+ char version[7+1];
+ /* version string returned by the routine glp_version */
+#endif
+ ENV *self;
+ /* pointer to this block to check its validity */
+ /*--------------------------------------------------------------*/
+ /* terminal output */
+ char *term_buf; /* char term_buf[TBUF_SIZE]; */
+ /* terminal output buffer */
+ int term_out;
+ /* flag to enable/disable terminal output */
+ int (*term_hook)(void *info, const char *s);
+ /* user-defined routine to intercept terminal output */
+ void *term_info;
+ /* transit pointer (cookie) passed to the routine term_hook */
+ FILE *tee_file;
+ /* output stream used to copy terminal output */
+ /*--------------------------------------------------------------*/
+ /* error handling */
+#if 1 /* 07/XI-2015 */
+ int err_st;
+ /* error state flag; set on entry to glp_error */
+#endif
+ const char *err_file;
+ /* value of the __FILE__ macro passed to glp_error */
+ int err_line;
+ /* value of the __LINE__ macro passed to glp_error */
+ void (*err_hook)(void *info);
+ /* user-defined routine to intercept abnormal termination */
+ void *err_info;
+ /* transit pointer (cookie) passed to the routine err_hook */
+ char *err_buf; /* char err_buf[EBUF_SIZE]; */
+ /* buffer to store error messages (used by I/O routines) */
+ /*--------------------------------------------------------------*/
+ /* dynamic memory allocation */
+ size_t mem_limit;
+ /* maximal amount of memory, in bytes, available for dynamic
+ * allocation */
+ MBD *mem_ptr;
+ /* pointer to the linked list of allocated memory blocks */
+ int mem_count;
+ /* total number of currently allocated memory blocks */
+ int mem_cpeak;
+ /* peak value of mem_count */
+ size_t mem_total;
+ /* total amount of currently allocated memory, in bytes; it is
+ * the sum of the size field over all memory block descriptors */
+ size_t mem_tpeak;
+ /* peak value of mem_total */
+#if 1 /* 23/XI-2015 */
+ /*--------------------------------------------------------------*/
+ /* bignum module working area */
+ void *gmp_pool; /* DMP *gmp_pool; */
+ /* working memory pool */
+ int gmp_size;
+ /* size of working array */
+ unsigned short *gmp_work; /* ushort gmp_work[gmp_size]; */
+ /* working array */
+#endif
+ /*--------------------------------------------------------------*/
+ /* dynamic linking support (optional) */
+ void *h_odbc;
+ /* handle to ODBC shared library */
+ void *h_mysql;
+ /* handle to MySQL shared library */
+};
+
+struct MBD
+{ /* memory block descriptor */
+ size_t size;
+ /* size of block, in bytes, including descriptor */
+ MBD *self;
+ /* pointer to this descriptor to check its validity */
+ MBD *prev;
+ /* pointer to previous memory block descriptor */
+ MBD *next;
+ /* pointer to next memory block descriptor */
+};
+
+#define get_env_ptr _glp_get_env_ptr
+ENV *get_env_ptr(void);
+/* retrieve pointer to environment block */
+
+#define tls_set_ptr _glp_tls_set_ptr
+void tls_set_ptr(void *ptr);
+/* store global pointer in TLS */
+
+#define tls_get_ptr _glp_tls_get_ptr
+void *tls_get_ptr(void);
+/* retrieve global pointer from TLS */
+
+#define xputs glp_puts
+void glp_puts(const char *s);
+/* write string on terminal */
+
+#define xprintf glp_printf
+void glp_printf(const char *fmt, ...);
+/* write formatted output on terminal */
+
+#define xvprintf glp_vprintf
+void glp_vprintf(const char *fmt, va_list arg);
+/* write formatted output on terminal */
+
+int glp_term_out(int flag);
+/* enable/disable terminal output */
+
+void glp_term_hook(int (*func)(void *info, const char *s), void *info);
+/* install hook to intercept terminal output */
+
+int glp_open_tee(const char *fname);
+/* start copying terminal output to text file */
+
+int glp_close_tee(void);
+/* stop copying terminal output to text file */
+
+#ifndef GLP_ERRFUNC_DEFINED
+#define GLP_ERRFUNC_DEFINED
+typedef void (*glp_errfunc)(const char *fmt, ...);
+#endif
+
+#define xerror glp_error_(__FILE__, __LINE__)
+glp_errfunc glp_error_(const char *file, int line);
+/* display fatal error message and terminate execution */
+
+#define xassert(expr) \
+ ((void)((expr) || (glp_assert_(#expr, __FILE__, __LINE__), 1)))
+void glp_assert_(const char *expr, const char *file, int line);
+/* check for logical condition */
+
+void glp_error_hook(void (*func)(void *info), void *info);
+/* install hook to intercept abnormal termination */
+
+#define put_err_msg _glp_put_err_msg
+void put_err_msg(const char *msg);
+/* provide error message string */
+
+#define get_err_msg _glp_get_err_msg
+const char *get_err_msg(void);
+/* obtain error message string */
+
+#define xmalloc(size) glp_alloc(1, size)
+/* allocate memory block (obsolete) */
+
+#define xcalloc(n, size) glp_alloc(n, size)
+/* allocate memory block (obsolete) */
+
+#define xalloc(n, size) glp_alloc(n, size)
+#define talloc(n, type) ((type *)glp_alloc(n, sizeof(type)))
+void *glp_alloc(int n, int size);
+/* allocate memory block */
+
+#define xrealloc(ptr, n, size) glp_realloc(ptr, n, size)
+#define trealloc(ptr, n, type) ((type *)glp_realloc(ptr, n, \
+ sizeof(type)))
+void *glp_realloc(void *ptr, int n, int size);
+/* reallocate memory block */
+
+#define xfree(ptr) glp_free(ptr)
+#define tfree(ptr) glp_free(ptr)
+void glp_free(void *ptr);
+/* free memory block */
+
+void glp_mem_limit(int limit);
+/* set memory usage limit */
+
+void glp_mem_usage(int *count, int *cpeak, size_t *total,
+ size_t *tpeak);
+/* get memory usage information */
+
+typedef struct glp_file glp_file;
+/* sequential stream descriptor */
+
+#define glp_open _glp_open
+glp_file *glp_open(const char *name, const char *mode);
+/* open stream */
+
+#define glp_eof _glp_eof
+int glp_eof(glp_file *f);
+/* test end-of-file indicator */
+
+#define glp_ioerr _glp_ioerr
+int glp_ioerr(glp_file *f);
+/* test I/O error indicator */
+
+#define glp_read _glp_read
+int glp_read(glp_file *f, void *buf, int nnn);
+/* read data from stream */
+
+#define glp_getc _glp_getc
+int glp_getc(glp_file *f);
+/* read character from stream */
+
+#define glp_write _glp_write
+int glp_write(glp_file *f, const void *buf, int nnn);
+/* write data to stream */
+
+#define glp_format _glp_format
+int glp_format(glp_file *f, const char *fmt, ...);
+/* write formatted data to stream */
+
+#define glp_close _glp_close
+int glp_close(glp_file *f);
+/* close stream */
+
+#define xtime glp_time
+double glp_time(void);
+/* determine current universal time */
+
+#define xdifftime glp_difftime
+double glp_difftime(double t1, double t0);
+/* compute difference between two time values */
+
+#define xdlopen _glp_dlopen
+void *xdlopen(const char *module);
+/* open dynamically linked library */
+
+#define xdlsym _glp_dlsym
+void *xdlsym(void *h, const char *symbol);
+/* obtain address of symbol from dynamically linked library */
+
+#define xdlclose _glp_dlclose
+void xdlclose(void *h);
+/* close dynamically linked library */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/error.c b/test/monniaux/glpk-4.65/src/env/error.c
new file mode 100644
index 00000000..a898b768
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/error.c
@@ -0,0 +1,200 @@
+/* error.c (error handling) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_error - display fatal error message and terminate execution
+*
+* SYNOPSIS
+*
+* void glp_error(const char *fmt, ...);
+*
+* DESCRIPTION
+*
+* The routine glp_error (implemented as a macro) formats its
+* parameters using the format control string fmt, writes the formatted
+* message on the terminal, and abnormally terminates the program. */
+
+static void errfunc(const char *fmt, ...)
+{ ENV *env = get_env_ptr();
+ va_list arg;
+#if 1 /* 07/XI-2015 */
+ env->err_st = 1;
+#endif
+ env->term_out = GLP_ON;
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ xprintf("Error detected in file %s at line %d\n",
+ env->err_file, env->err_line);
+ if (env->err_hook != NULL)
+ env->err_hook(env->err_info);
+ abort();
+ exit(EXIT_FAILURE);
+ /* no return */
+}
+
+glp_errfunc glp_error_(const char *file, int line)
+{ ENV *env = get_env_ptr();
+ env->err_file = file;
+ env->err_line = line;
+ return errfunc;
+}
+
+#if 1 /* 07/XI-2015 */
+/***********************************************************************
+* NAME
+*
+* glp_at_error - check for error state
+*
+* SYNOPSIS
+*
+* int glp_at_error(void);
+*
+* DESCRIPTION
+*
+* The routine glp_at_error checks if the GLPK environment is at error
+* state, i.e. if the call to the routine is (indirectly) made from the
+* glp_error routine via an user-defined hook routine.
+*
+* RETURNS
+*
+* If the GLPK environment is at error state, the routine glp_at_error
+* returns non-zero, otherwise zero. */
+
+int glp_at_error(void)
+{ ENV *env = get_env_ptr();
+ return env->err_st;
+}
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_assert - check for logical condition
+*
+* SYNOPSIS
+*
+* void glp_assert(int expr);
+*
+* DESCRIPTION
+*
+* The routine glp_assert (implemented as a macro) checks for a logical
+* condition specified by the parameter expr. If the condition is false
+* (i.e. the value of expr is zero), the routine writes a message on
+* the terminal and abnormally terminates the program. */
+
+void glp_assert_(const char *expr, const char *file, int line)
+{ glp_error_(file, line)("Assertion failed: %s\n", expr);
+ /* no return */
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_error_hook - install hook to intercept abnormal termination
+*
+* SYNOPSIS
+*
+* void glp_error_hook(void (*func)(void *info), void *info);
+*
+* DESCRIPTION
+*
+* The routine glp_error_hook installs a user-defined hook routine to
+* intercept abnormal termination.
+*
+* The parameter func specifies the user-defined hook routine. It is
+* called from the routine glp_error before the latter calls the abort
+* function to abnormally terminate the application program because of
+* fatal error. The parameter info is a transit pointer, specified in
+* the corresponding call to the routine glp_error_hook; it may be used
+* to pass some information to the hook routine.
+*
+* To uninstall the hook routine the parameters func and info should be
+* both specified as NULL. */
+
+void glp_error_hook(void (*func)(void *info), void *info)
+{ ENV *env = get_env_ptr();
+ if (func == NULL)
+ { env->err_hook = NULL;
+ env->err_info = NULL;
+ }
+ else
+ { env->err_hook = func;
+ env->err_info = info;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* put_err_msg - provide error message string
+*
+* SYNOPSIS
+*
+* #include "env.h"
+* void put_err_msg(const char *msg);
+*
+* DESCRIPTION
+*
+* The routine put_err_msg stores an error message string pointed to by
+* msg to the environment block. */
+
+void put_err_msg(const char *msg)
+{ ENV *env = get_env_ptr();
+ int len;
+ len = strlen(msg);
+ if (len >= EBUF_SIZE)
+ len = EBUF_SIZE - 1;
+ memcpy(env->err_buf, msg, len);
+ if (len > 0 && env->err_buf[len-1] == '\n')
+ len--;
+ env->err_buf[len] = '\0';
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* get_err_msg - obtain error message string
+*
+* SYNOPSIS
+*
+* #include "env.h"
+* const char *get_err_msg(void);
+*
+* RETURNS
+*
+* The routine get_err_msg returns a pointer to an error message string
+* previously stored by the routine put_err_msg. */
+
+const char *get_err_msg(void)
+{ ENV *env = get_env_ptr();
+ return env->err_buf;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/stdc.c b/test/monniaux/glpk-4.65/src/env/stdc.c
new file mode 100644
index 00000000..59331e22
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/stdc.c
@@ -0,0 +1,98 @@
+/* stdc.c (replacements for standard non-thread-safe functions) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+/* portable ANSI C version ********************************************/
+
+#if !defined(TLS)
+
+#define ENABLE_NON_SAFE
+#include "stdc.h"
+
+struct tm *xgmtime(const time_t *timer)
+{ return
+ gmtime(timer);
+}
+
+char *xstrerr(int errnum)
+{ return
+ strerror(errnum);
+}
+
+char *xstrtok(char *s1, const char *s2)
+{ return
+ strtok(s1, s2);
+}
+
+/* MS Windows version *************************************************/
+
+#elif defined(__WOE__)
+
+#include "stdc.h"
+
+struct tm *xgmtime(const time_t *timer)
+{ static TLS struct tm result;
+ gmtime_s(&result, timer);
+ return &result;
+}
+
+char *xstrerr(int errnum)
+{ static TLS char s[1023+1];
+ strerror_s(s, sizeof(s), errnum);
+ return s;
+}
+
+char *xstrtok(char *s1, const char *s2)
+{ static TLS char *ptr;
+ return strtok_s(s1, s2, &ptr);
+}
+
+/* GNU/Linux version **************************************************/
+
+#else
+
+#include "stdc.h"
+
+struct tm *xgmtime(const time_t *timer)
+{ static TLS struct tm result;
+ gmtime_r(timer, &result);
+ return &result;
+}
+
+char *xstrerr(int errnum)
+{ static TLS char s[1023+1];
+ strerror_r(errnum, s, sizeof(s));
+ return s;
+}
+
+char *xstrtok(char *s1, const char *s2)
+{ static TLS char *ptr;
+ return strtok_r(s1, s2, &ptr);
+}
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/stdc.h b/test/monniaux/glpk-4.65/src/env/stdc.h
new file mode 100644
index 00000000..a376f2c9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/stdc.h
@@ -0,0 +1,73 @@
+/* stdc.h (standard ANSI C headers) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef STDC_H
+#define STDC_H
+
+#include <ctype.h>
+#include <errno.h>
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef ENABLE_NON_SAFE /* 29/I-2017 */
+/* disable using non-thread-safe functions directly */
+#undef gmtime
+#define gmtime ???
+#undef strerror
+#define strerror ???
+#undef strtok
+#define strtok ???
+#endif
+
+#if 1 /* 29/I-2017 */
+/* provide replacements for these functions on a per-thread basis */
+#define xgmtime _glp_xgmtime
+struct tm *xgmtime(const time_t *);
+#define xstrerr _glp_xstrerr
+char *xstrerr(int);
+#define xstrtok _glp_xstrtok
+char *xstrtok(char *, const char *);
+#endif
+
+#if 1 /* 06/II-2018 */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#ifndef __WOE__
+#define CDECL
+#else
+#define CDECL __cdecl
+#endif
+#endif
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/stdout.c b/test/monniaux/glpk-4.65/src/env/stdout.c
new file mode 100644
index 00000000..94eee02a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/stdout.c
@@ -0,0 +1,262 @@
+/* stdout.c (terminal output) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#undef NDEBUG
+#include <assert.h>
+#include "env.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_puts - write string on terminal
+*
+* SYNOPSIS
+*
+* void glp_puts(const char *s);
+*
+* The routine glp_puts writes the string s on the terminal. */
+
+void glp_puts(const char *s)
+{ ENV *env = get_env_ptr();
+ /* if terminal output is disabled, do nothing */
+ if (!env->term_out)
+ goto skip;
+ /* pass the string to the hook routine, if defined */
+ if (env->term_hook != NULL)
+ { if (env->term_hook(env->term_info, s) != 0)
+ goto skip;
+ }
+ /* write the string on the terminal */
+ fputs(s, stdout);
+ fflush(stdout);
+ /* write the string on the tee file, if required */
+ if (env->tee_file != NULL)
+ { fputs(s, env->tee_file);
+ fflush(env->tee_file);
+ }
+skip: return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_printf - write formatted output on terminal
+*
+* SYNOPSIS
+*
+* void glp_printf(const char *fmt, ...);
+*
+* DESCRIPTION
+*
+* The routine glp_printf uses the format control string fmt to format
+* its parameters and writes the formatted output on the terminal. */
+
+void glp_printf(const char *fmt, ...)
+{ ENV *env = get_env_ptr();
+ va_list arg;
+ /* if terminal output is disabled, do nothing */
+ if (!env->term_out)
+ goto skip;
+ /* format the output */
+ va_start(arg, fmt);
+ vsprintf(env->term_buf, fmt, arg);
+ /* (do not use xassert) */
+ assert(strlen(env->term_buf) < TBUF_SIZE);
+ va_end(arg);
+ /* write the formatted output on the terminal */
+ glp_puts(env->term_buf);
+skip: return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_vprintf - write formatted output on terminal
+*
+* SYNOPSIS
+*
+* void glp_vprintf(const char *fmt, va_list arg);
+*
+* DESCRIPTION
+*
+* The routine glp_vprintf uses the format control string fmt to format
+* its parameters specified by the list arg and writes the formatted
+* output on the terminal. */
+
+void glp_vprintf(const char *fmt, va_list arg)
+{ ENV *env = get_env_ptr();
+ /* if terminal output is disabled, do nothing */
+ if (!env->term_out)
+ goto skip;
+ /* format the output */
+ vsprintf(env->term_buf, fmt, arg);
+ /* (do not use xassert) */
+ assert(strlen(env->term_buf) < TBUF_SIZE);
+ /* write the formatted output on the terminal */
+ glp_puts(env->term_buf);
+skip: return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_term_out - enable/disable terminal output
+*
+* SYNOPSIS
+*
+* int glp_term_out(int flag);
+*
+* DESCRIPTION
+*
+* Depending on the parameter flag the routine glp_term_out enables or
+* disables terminal output performed by glpk routines:
+*
+* GLP_ON - enable terminal output;
+* GLP_OFF - disable terminal output.
+*
+* RETURNS
+*
+* The routine glp_term_out returns the previous value of the terminal
+* output flag. */
+
+int glp_term_out(int flag)
+{ ENV *env = get_env_ptr();
+ int old = env->term_out;
+ if (!(flag == GLP_ON || flag == GLP_OFF))
+ xerror("glp_term_out: flag = %d; invalid parameter\n", flag);
+ env->term_out = flag;
+ return old;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_term_hook - install hook to intercept terminal output
+*
+* SYNOPSIS
+*
+* void glp_term_hook(int (*func)(void *info, const char *s),
+* void *info);
+*
+* DESCRIPTION
+*
+* The routine glp_term_hook installs a user-defined hook routine to
+* intercept all terminal output performed by glpk routines.
+*
+* This feature can be used to redirect the terminal output to other
+* destination, for example to a file or a text window.
+*
+* The parameter func specifies the user-defined hook routine. It is
+* called from an internal printing routine, which passes to it two
+* parameters: info and s. The parameter info is a transit pointer,
+* specified in the corresponding call to the routine glp_term_hook;
+* it may be used to pass some information to the hook routine. The
+* parameter s is a pointer to the null terminated character string,
+* which is intended to be written to the terminal. If the hook routine
+* returns zero, the printing routine writes the string s to the
+* terminal in a usual way; otherwise, if the hook routine returns
+* non-zero, no terminal output is performed.
+*
+* To uninstall the hook routine the parameters func and info should be
+* specified as NULL. */
+
+void glp_term_hook(int (*func)(void *info, const char *s), void *info)
+{ ENV *env = get_env_ptr();
+ if (func == NULL)
+ { env->term_hook = NULL;
+ env->term_info = NULL;
+ }
+ else
+ { env->term_hook = func;
+ env->term_info = info;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_open_tee - start copying terminal output to text file
+*
+* SYNOPSIS
+*
+* int glp_open_tee(const char *name);
+*
+* DESCRIPTION
+*
+* The routine glp_open_tee starts copying all the terminal output to
+* an output text file, whose name is specified by the character string
+* name.
+*
+* RETURNS
+*
+* 0 - operation successful
+* 1 - copying terminal output is already active
+* 2 - unable to create output file */
+
+int glp_open_tee(const char *name)
+{ ENV *env = get_env_ptr();
+ if (env->tee_file != NULL)
+ { /* copying terminal output is already active */
+ return 1;
+ }
+ env->tee_file = fopen(name, "w");
+ if (env->tee_file == NULL)
+ { /* unable to create output file */
+ return 2;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_close_tee - stop copying terminal output to text file
+*
+* SYNOPSIS
+*
+* int glp_close_tee(void);
+*
+* DESCRIPTION
+*
+* The routine glp_close_tee stops copying the terminal output to the
+* output text file previously open by the routine glp_open_tee closing
+* that file.
+*
+* RETURNS
+*
+* 0 - operation successful
+* 1 - copying terminal output was not started */
+
+int glp_close_tee(void)
+{ ENV *env = get_env_ptr();
+ if (env->tee_file == NULL)
+ { /* copying terminal output was not started */
+ return 1;
+ }
+ fclose(env->tee_file);
+ env->tee_file = NULL;
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/stream.c b/test/monniaux/glpk-4.65/src/env/stream.c
new file mode 100644
index 00000000..906e5b04
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/stream.c
@@ -0,0 +1,517 @@
+/* stream.c (stream input/output) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "zlib.h"
+
+struct glp_file
+{ /* sequential stream descriptor */
+ char *base;
+ /* pointer to buffer */
+ int size;
+ /* size of buffer, in bytes */
+ char *ptr;
+ /* pointer to next byte in buffer */
+ int cnt;
+ /* count of bytes in buffer */
+ int flag;
+ /* stream flags: */
+#define IONULL 0x01 /* null file */
+#define IOSTD 0x02 /* standard stream */
+#define IOGZIP 0x04 /* gzipped file */
+#define IOWRT 0x08 /* output stream */
+#define IOEOF 0x10 /* end of file */
+#define IOERR 0x20 /* input/output error */
+ void *file;
+ /* pointer to underlying control object */
+};
+
+/***********************************************************************
+* NAME
+*
+* glp_open - open stream
+*
+* SYNOPSIS
+*
+* glp_file *glp_open(const char *name, const char *mode);
+*
+* DESCRIPTION
+*
+* The routine glp_open opens a file whose name is a string pointed to
+* by name and associates a stream with it.
+*
+* The following special filenames are recognized by the routine (this
+* feature is platform independent):
+*
+* "/dev/null" empty (null) file;
+* "/dev/stdin" standard input stream;
+* "/dev/stdout" standard output stream;
+* "/dev/stderr" standard error stream.
+*
+* If the specified filename is ended with ".gz", it is assumed that
+* the file is in gzipped format. In this case the file is compressed
+* or decompressed by the I/O routines "on the fly".
+*
+* The parameter mode points to a string, which indicates the open mode
+* and should be one of the following:
+*
+* "r" open text file for reading;
+* "w" truncate to zero length or create text file for writing;
+* "a" append, open or create text file for writing at end-of-file;
+* "rb" open binary file for reading;
+* "wb" truncate to zero length or create binary file for writing;
+* "ab" append, open or create binary file for writing at end-of-file.
+*
+* RETURNS
+*
+* The routine glp_open returns a pointer to the object controlling the
+* stream. If the operation fails, the routine returns NULL. */
+
+glp_file *glp_open(const char *name, const char *mode)
+{ glp_file *f;
+ int flag;
+ void *file;
+ if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0)
+ flag = 0;
+ else if (strcmp(mode, "w") == 0 || strcmp(mode, "wb") == 0)
+ flag = IOWRT;
+#if 1 /* 08/V-2014 */
+ else if (strcmp(mode, "a") == 0 || strcmp(mode, "ab") == 0)
+ flag = IOWRT;
+#endif
+ else
+ xerror("glp_open: invalid mode string\n");
+ if (strcmp(name, "/dev/null") == 0)
+ { flag |= IONULL;
+ file = NULL;
+ }
+ else if (strcmp(name, "/dev/stdin") == 0)
+ { flag |= IOSTD;
+ file = stdin;
+ }
+ else if (strcmp(name, "/dev/stdout") == 0)
+ { flag |= IOSTD;
+ file = stdout;
+ }
+ else if (strcmp(name, "/dev/stderr") == 0)
+ { flag |= IOSTD;
+ file = stderr;
+ }
+ else
+ { char *ext = strrchr(name, '.');
+ if (ext == NULL || strcmp(ext, ".gz") != 0)
+ { file = fopen(name, mode);
+ if (file == NULL)
+#if 0 /* 29/I-2017 */
+ { put_err_msg(strerror(errno));
+#else
+ { put_err_msg(xstrerr(errno));
+#endif
+ return NULL;
+ }
+ }
+ else
+ { flag |= IOGZIP;
+ if (strcmp(mode, "r") == 0)
+ mode = "rb";
+ else if (strcmp(mode, "w") == 0)
+ mode = "wb";
+#if 1 /* 08/V-2014; this mode seems not to work */
+ else if (strcmp(mode, "a") == 0)
+ mode = "ab";
+#endif
+ file = gzopen(name, mode);
+ if (file == NULL)
+#if 0 /* 29/I-2017 */
+ { put_err_msg(strerror(errno));
+#else
+ { put_err_msg(xstrerr(errno));
+#endif
+ return NULL;
+ }
+ }
+ }
+ f = talloc(1, glp_file);
+ f->base = talloc(BUFSIZ, char);
+ f->size = BUFSIZ;
+ f->ptr = f->base;
+ f->cnt = 0;
+ f->flag = flag;
+ f->file = file;
+ return f;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_eof - test end-of-file indicator
+*
+* SYNOPSIS
+*
+* int glp_eof(glp_file *f);
+*
+* DESCRIPTION
+*
+* The routine glp_eof tests the end-of-file indicator for the stream
+* pointed to by f.
+*
+* RETURNS
+*
+* The routine glp_eof returns non-zero if and only if the end-of-file
+* indicator is set for the specified stream. */
+
+int glp_eof(glp_file *f)
+{ return
+ f->flag & IOEOF;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_ioerr - test I/O error indicator
+*
+* SYNOPSIS
+*
+* int glp_ioerr(glp_file *f);
+*
+* DESCRIPTION
+*
+* The routine glp_ioerr tests the I/O error indicator for the stream
+* pointed to by f.
+*
+* RETURNS
+*
+* The routine glp_ioerr returns non-zero if and only if the I/O error
+* indicator is set for the specified stream. */
+
+int glp_ioerr(glp_file *f)
+{ return
+ f->flag & IOERR;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_read - read data from stream
+*
+* SYNOPSIS
+*
+* int glp_read(glp_file *f, void *buf, int nnn);
+*
+* DESCRIPTION
+*
+* The routine glp_read reads, into the buffer pointed to by buf, up to
+* nnn bytes, from the stream pointed to by f.
+*
+* RETURNS
+*
+* The routine glp_read returns the number of bytes successfully read
+* (which may be less than nnn). If an end-of-file is encountered, the
+* end-of-file indicator for the stream is set and glp_read returns
+* zero. If a read error occurs, the error indicator for the stream is
+* set and glp_read returns a negative value. */
+
+int glp_read(glp_file *f, void *buf, int nnn)
+{ int nrd, cnt;
+ if (f->flag & IOWRT)
+ xerror("glp_read: attempt to read from output stream\n");
+ if (nnn < 1)
+ xerror("glp_read: nnn = %d; invalid parameter\n", nnn);
+ for (nrd = 0; nrd < nnn; nrd += cnt)
+ { if (f->cnt == 0)
+ { /* buffer is empty; fill it */
+ if (f->flag & IONULL)
+ cnt = 0;
+ else if (!(f->flag & IOGZIP))
+ { cnt = fread(f->base, 1, f->size, (FILE *)(f->file));
+ if (ferror((FILE *)(f->file)))
+ { f->flag |= IOERR;
+#if 0 /* 29/I-2017 */
+ put_err_msg(strerror(errno));
+#else
+ put_err_msg(xstrerr(errno));
+#endif
+ return EOF;
+ }
+ }
+ else
+ { int errnum;
+ const char *msg;
+ cnt = gzread((gzFile)(f->file), f->base, f->size);
+ if (cnt < 0)
+ { f->flag |= IOERR;
+ msg = gzerror((gzFile)(f->file), &errnum);
+ if (errnum == Z_ERRNO)
+#if 0 /* 29/I-2017 */
+ put_err_msg(strerror(errno));
+#else
+ put_err_msg(xstrerr(errno));
+#endif
+ else
+ put_err_msg(msg);
+ return EOF;
+ }
+ }
+ if (cnt == 0)
+ { if (nrd == 0)
+ f->flag |= IOEOF;
+ break;
+ }
+ f->ptr = f->base;
+ f->cnt = cnt;
+ }
+ cnt = nnn - nrd;
+ if (cnt > f->cnt)
+ cnt = f->cnt;
+ memcpy((char *)buf + nrd, f->ptr, cnt);
+ f->ptr += cnt;
+ f->cnt -= cnt;
+ }
+ return nrd;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_getc - read character from stream
+*
+* SYNOPSIS
+*
+* int glp_getc(glp_file *f);
+*
+* DESCRIPTION
+*
+* The routine glp_getc obtains a next character as an unsigned char
+* converted to an int from the input stream pointed to by f.
+*
+* RETURNS
+*
+* The routine glp_getc returns the next character obtained. However,
+* if an end-of-file is encountered or a read error occurs, the routine
+* returns EOF. (An end-of-file and a read error can be distinguished
+* by use of the routines glp_eof and glp_ioerr.) */
+
+int glp_getc(glp_file *f)
+{ unsigned char buf[1];
+ if (f->flag & IOWRT)
+ xerror("glp_getc: attempt to read from output stream\n");
+ if (glp_read(f, buf, 1) != 1)
+ return EOF;
+ return buf[0];
+}
+
+/***********************************************************************
+* do_flush - flush output stream
+*
+* This routine causes buffered data for the specified output stream to
+* be written to the associated file.
+*
+* If the operation was successful, the routine returns zero, otherwise
+* non-zero. */
+
+static int do_flush(glp_file *f)
+{ xassert(f->flag & IOWRT);
+ if (f->cnt > 0)
+ { if (f->flag & IONULL)
+ ;
+ else if (!(f->flag & IOGZIP))
+ { if ((int)fwrite(f->base, 1, f->cnt, (FILE *)(f->file))
+ != f->cnt)
+ { f->flag |= IOERR;
+#if 0 /* 29/I-2017 */
+ put_err_msg(strerror(errno));
+#else
+ put_err_msg(xstrerr(errno));
+#endif
+ return EOF;
+ }
+ }
+ else
+ { int errnum;
+ const char *msg;
+ if (gzwrite((gzFile)(f->file), f->base, f->cnt) != f->cnt)
+ { f->flag |= IOERR;
+ msg = gzerror((gzFile)(f->file), &errnum);
+ if (errnum == Z_ERRNO)
+#if 0 /* 29/I-2017 */
+ put_err_msg(strerror(errno));
+#else
+ put_err_msg(xstrerr(errno));
+#endif
+ else
+ put_err_msg(msg);
+ return EOF;
+ }
+ }
+ }
+ f->ptr = f->base;
+ f->cnt = 0;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_write - write data to stream
+*
+* SYNOPSIS
+*
+* int glp_write(glp_file *f, const void *buf, int nnn);
+*
+* DESCRIPTION
+*
+* The routine glp_write writes, from the buffer pointed to by buf, up
+* to nnn bytes, to the stream pointed to by f.
+*
+* RETURNS
+*
+* The routine glp_write returns the number of bytes successfully
+* written (which is equal to nnn). If a write error occurs, the error
+* indicator for the stream is set and glp_write returns a negative
+* value. */
+
+int glp_write(glp_file *f, const void *buf, int nnn)
+{ int nwr, cnt;
+ if (!(f->flag & IOWRT))
+ xerror("glp_write: attempt to write to input stream\n");
+ if (nnn < 1)
+ xerror("glp_write: nnn = %d; invalid parameter\n", nnn);
+ for (nwr = 0; nwr < nnn; nwr += cnt)
+ { cnt = nnn - nwr;
+ if (cnt > f->size - f->cnt)
+ cnt = f->size - f->cnt;
+ memcpy(f->ptr, (const char *)buf + nwr, cnt);
+ f->ptr += cnt;
+ f->cnt += cnt;
+ if (f->cnt == f->size)
+ { /* buffer is full; flush it */
+ if (do_flush(f) != 0)
+ return EOF;
+ }
+ }
+ return nwr;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_format - write formatted data to stream
+*
+* SYNOPSIS
+*
+* int glp_format(glp_file *f, const char *fmt, ...);
+*
+* DESCRIPTION
+*
+* The routine glp_format writes formatted data to the stream pointed
+* to by f. The format control string pointed to by fmt specifies how
+* subsequent arguments are converted for output.
+*
+* RETURNS
+*
+* The routine glp_format returns the number of characters written, or
+* a negative value if an output error occurs. */
+
+int glp_format(glp_file *f, const char *fmt, ...)
+{ ENV *env = get_env_ptr();
+ va_list arg;
+ int nnn;
+ if (!(f->flag & IOWRT))
+ xerror("glp_format: attempt to write to input stream\n");
+ va_start(arg, fmt);
+ nnn = vsprintf(env->term_buf, fmt, arg);
+ xassert(0 <= nnn && nnn < TBUF_SIZE);
+ va_end(arg);
+ return nnn == 0 ? 0 : glp_write(f, env->term_buf, nnn);
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_close - close stream
+*
+* SYNOPSIS
+*
+* int glp_close(glp_file *f);
+*
+* DESCRIPTION
+*
+* The routine glp_close closes the stream pointed to by f.
+*
+* RETURNS
+*
+* If the operation was successful, the routine returns zero, otherwise
+* non-zero. */
+
+int glp_close(glp_file *f)
+{ int ret = 0;
+ if (f->flag & IOWRT)
+ { if (do_flush(f) != 0)
+ ret = EOF;
+ }
+ if (f->flag & (IONULL | IOSTD))
+ ;
+ else if (!(f->flag & IOGZIP))
+ { if (fclose((FILE *)(f->file)) != 0)
+ { if (ret == 0)
+#if 0 /* 29/I-2017 */
+ { put_err_msg(strerror(errno));
+#else
+ { put_err_msg(xstrerr(errno));
+#endif
+ ret = EOF;
+ }
+ }
+ }
+ else
+ { int errnum;
+ errnum = gzclose((gzFile)(f->file));
+ if (errnum == Z_OK)
+ ;
+ else if (errnum == Z_ERRNO)
+ { if (ret == 0)
+#if 0 /* 29/I-2017 */
+ { put_err_msg(strerror(errno));
+#else
+ { put_err_msg(xstrerr(errno));
+#endif
+ ret = EOF;
+ }
+ }
+#if 1 /* FIXME */
+ else
+ { if (ret == 0)
+ { ENV *env = get_env_ptr();
+ sprintf(env->term_buf, "gzclose returned %d", errnum);
+ put_err_msg(env->term_buf);
+ ret = EOF;
+ }
+ }
+#endif
+ }
+ tfree(f->base);
+ tfree(f);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/time.c b/test/monniaux/glpk-4.65/src/env/time.c
new file mode 100644
index 00000000..1ffb28e9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/time.c
@@ -0,0 +1,150 @@
+/* time.c (standard time) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <address@hidden>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "env.h"
+#include "jd.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_time - determine current universal time
+*
+* SYNOPSIS
+*
+* double glp_time(void);
+*
+* RETURNS
+*
+* The routine glp_time returns the current universal time (UTC), in
+* milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
+
+#define EPOCH 2440588 /* = jday(1, 1, 1970) */
+
+/* POSIX version ******************************************************/
+
+#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
+
+#if 0 /* 29/VI-2017 */
+#include <sys/time.h>
+#include <time.h>
+
+double glp_time(void)
+{ struct timeval tv;
+ struct tm *tm;
+ int j;
+ double t;
+ gettimeofday(&tv, NULL);
+#if 0 /* 29/I-2017 */
+ tm = gmtime(&tv.tv_sec);
+#else
+ tm = xgmtime(&tv.tv_sec);
+#endif
+ j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
+ xassert(j >= 0);
+ t = ((((double)(j - EPOCH) * 24.0 + (double)tm->tm_hour) * 60.0 +
+ (double)tm->tm_min) * 60.0 + (double)tm->tm_sec) * 1000.0 +
+ (double)(tv.tv_usec / 1000);
+ return t;
+}
+#else
+#include <sys/time.h>
+
+double glp_time(void)
+{ struct timeval tv;
+ double t;
+ gettimeofday(&tv, NULL);
+ t = (double)tv.tv_sec + (double)(tv.tv_usec) / 1e6;
+ xassert(0.0 <= t && t < 4294967296.0);
+ return 1000.0 * t;
+}
+#endif
+
+/* MS Windows version *************************************************/
+
+#elif defined(__WOE__)
+
+#include <windows.h>
+
+double glp_time(void)
+{ SYSTEMTIME st;
+ int j;
+ double t;
+ GetSystemTime(&st);
+ j = jday(st.wDay, st.wMonth, st.wYear);
+ xassert(j >= 0);
+ t = ((((double)(j - EPOCH) * 24.0 + (double)st.wHour) * 60.0 +
+ (double)st.wMinute) * 60.0 + (double)st.wSecond) * 1000.0 +
+ (double)st.wMilliseconds;
+ return t;
+}
+
+/* portable ANSI C version ********************************************/
+
+#else
+
+#include <time.h>
+
+double glp_time(void)
+{ time_t timer;
+ struct tm *tm;
+ int j;
+ double t;
+ timer = time(NULL);
+#if 0 /* 29/I-2017 */
+ tm = gmtime(&timer);
+#else
+ tm = xgmtime(&timer);
+#endif
+ j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
+ xassert(j >= 0);
+ t = ((((double)(j - EPOCH) * 24.0 + (double)tm->tm_hour) * 60.0 +
+ (double)tm->tm_min) * 60.0 + (double)tm->tm_sec) * 1000.0;
+ return t;
+}
+
+#endif
+
+/***********************************************************************
+* NAME
+*
+* glp_difftime - compute difference between two time values
+*
+* SYNOPSIS
+*
+* double glp_difftime(double t1, double t0);
+*
+* RETURNS
+*
+* The routine glp_difftime returns the difference between two time
+* values t1 and t0, expressed in seconds. */
+
+double glp_difftime(double t1, double t0)
+{ return
+ (t1 - t0) / 1000.0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/env/tls.c b/test/monniaux/glpk-4.65/src/env/tls.c
new file mode 100644
index 00000000..4062ee4c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/env/tls.c
@@ -0,0 +1,128 @@
+/* tls.c (thread local storage) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2001-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "env.h"
+
+#ifndef TLS
+static void *tls = NULL;
+#else
+static TLS void *tls = NULL;
+/* this option allows running multiple independent instances of GLPK in
+ * different threads of a multi-threaded application, in which case the
+ * variable tls should be placed in the Thread Local Storage (TLS);
+ * it is assumed that the macro TLS is previously defined to something
+ * like '__thread', '_Thread_local', etc. */
+#endif
+
+/***********************************************************************
+* NAME
+*
+* tls_set_ptr - store global pointer in TLS
+*
+* SYNOPSIS
+*
+* #include "env.h"
+* void tls_set_ptr(void *ptr);
+*
+* DESCRIPTION
+*
+* The routine tls_set_ptr stores a pointer specified by the parameter
+* ptr in the Thread Local Storage (TLS). */
+
+void tls_set_ptr(void *ptr)
+{ tls = ptr;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* tls_get_ptr - retrieve global pointer from TLS
+*
+* SYNOPSIS
+*
+* #include "env.h"
+* void *tls_get_ptr(void);
+*
+* RETURNS
+*
+* The routine tls_get_ptr returns a pointer previously stored by the
+* routine tls_set_ptr. If the latter has not been called yet, NULL is
+* returned. */
+
+void *tls_get_ptr(void)
+{ void *ptr;
+ ptr = tls;
+ return ptr;
+}
+
+/**********************************************************************/
+
+#ifdef __WOE__
+
+/*** Author: Heinrich Schuchardt <xypron.glpk@gmx.de> ***/
+
+#pragma comment(lib, "user32.lib")
+
+#include <windows.h>
+
+#define VISTA 0x06
+
+/* This is the main entry point of the DLL. */
+
+BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID
+ lpvReserved)
+{ DWORD version;
+ DWORD major_version;
+#ifdef TLS
+ switch (fdwReason)
+ { case DLL_PROCESS_ATTACH:
+ /* @TODO:
+ * GetVersion is deprecated but the version help functions are
+ * not available in Visual Studio 2010. So lets use it until
+ * we remove the outdated Build files. */
+ version = GetVersion();
+ major_version = version & 0xff;
+ if (major_version < VISTA)
+ { MessageBoxA(NULL,
+ "The GLPK library called by this application is configur"
+ "ed to use thread local storage which is not fully suppo"
+ "rted by your version of Microsoft Windows.\n\n"
+ "Microsoft Windows Vista or a later version of Windows i"
+ "s required to run this application.",
+ "GLPK", MB_OK | MB_ICONERROR);
+ return FALSE;
+ }
+ break;
+ }
+#endif /* TLS */
+ return TRUE;
+}
+
+#endif /* __WOE__ */
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/glpk.h b/test/monniaux/glpk-4.65/src/glpk.h
new file mode 100644
index 00000000..f4e250f9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/glpk.h
@@ -0,0 +1,1175 @@
+/* glpk.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef GLPK_H
+#define GLPK_H
+
+#include <stdarg.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* library version numbers: */
+#define GLP_MAJOR_VERSION 4
+#define GLP_MINOR_VERSION 65
+
+typedef struct glp_prob glp_prob;
+/* LP/MIP problem object */
+
+/* optimization direction flag: */
+#define GLP_MIN 1 /* minimization */
+#define GLP_MAX 2 /* maximization */
+
+/* kind of structural variable: */
+#define GLP_CV 1 /* continuous variable */
+#define GLP_IV 2 /* integer variable */
+#define GLP_BV 3 /* binary variable */
+
+/* type of auxiliary/structural variable: */
+#define GLP_FR 1 /* free (unbounded) variable */
+#define GLP_LO 2 /* variable with lower bound */
+#define GLP_UP 3 /* variable with upper bound */
+#define GLP_DB 4 /* double-bounded variable */
+#define GLP_FX 5 /* fixed variable */
+
+/* status of auxiliary/structural variable: */
+#define GLP_BS 1 /* basic variable */
+#define GLP_NL 2 /* non-basic variable on lower bound */
+#define GLP_NU 3 /* non-basic variable on upper bound */
+#define GLP_NF 4 /* non-basic free (unbounded) variable */
+#define GLP_NS 5 /* non-basic fixed variable */
+
+/* scaling options: */
+#define GLP_SF_GM 0x01 /* perform geometric mean scaling */
+#define GLP_SF_EQ 0x10 /* perform equilibration scaling */
+#define GLP_SF_2N 0x20 /* round scale factors to power of two */
+#define GLP_SF_SKIP 0x40 /* skip if problem is well scaled */
+#define GLP_SF_AUTO 0x80 /* choose scaling options automatically */
+
+/* solution indicator: */
+#define GLP_SOL 1 /* basic solution */
+#define GLP_IPT 2 /* interior-point solution */
+#define GLP_MIP 3 /* mixed integer solution */
+
+/* solution status: */
+#define GLP_UNDEF 1 /* solution is undefined */
+#define GLP_FEAS 2 /* solution is feasible */
+#define GLP_INFEAS 3 /* solution is infeasible */
+#define GLP_NOFEAS 4 /* no feasible solution exists */
+#define GLP_OPT 5 /* solution is optimal */
+#define GLP_UNBND 6 /* solution is unbounded */
+
+typedef struct
+{ /* basis factorization control parameters */
+ int msg_lev; /* (not used) */
+ int type; /* factorization type: */
+#if 1 /* 05/III-2014 */
+#define GLP_BF_LUF 0x00 /* plain LU-factorization */
+#define GLP_BF_BTF 0x10 /* block triangular LU-factorization */
+#endif
+#define GLP_BF_FT 0x01 /* Forrest-Tomlin (LUF only) */
+#define GLP_BF_BG 0x02 /* Schur compl. + Bartels-Golub */
+#define GLP_BF_GR 0x03 /* Schur compl. + Givens rotation */
+ int lu_size; /* (not used) */
+ double piv_tol; /* sgf_piv_tol */
+ int piv_lim; /* sgf_piv_lim */
+ int suhl; /* sgf_suhl */
+ double eps_tol; /* sgf_eps_tol */
+ double max_gro; /* (not used) */
+ int nfs_max; /* fhvint.nfs_max */
+ double upd_tol; /* (not used) */
+ int nrs_max; /* scfint.nn_max */
+ int rs_size; /* (not used) */
+ double foo_bar[38]; /* (reserved) */
+} glp_bfcp;
+
+typedef struct
+{ /* simplex solver control parameters */
+ int msg_lev; /* message level: */
+#define GLP_MSG_OFF 0 /* no output */
+#define GLP_MSG_ERR 1 /* warning and error messages only */
+#define GLP_MSG_ON 2 /* normal output */
+#define GLP_MSG_ALL 3 /* full output */
+#define GLP_MSG_DBG 4 /* debug output */
+ int meth; /* simplex method option: */
+#define GLP_PRIMAL 1 /* use primal simplex */
+#define GLP_DUALP 2 /* use dual; if it fails, use primal */
+#define GLP_DUAL 3 /* use dual simplex */
+ int pricing; /* pricing technique: */
+#define GLP_PT_STD 0x11 /* standard (Dantzig's rule) */
+#define GLP_PT_PSE 0x22 /* projected steepest edge */
+ int r_test; /* ratio test technique: */
+#define GLP_RT_STD 0x11 /* standard (textbook) */
+#define GLP_RT_HAR 0x22 /* Harris' two-pass ratio test */
+#if 1 /* 16/III-2016 */
+#define GLP_RT_FLIP 0x33 /* long-step (flip-flop) ratio test */
+#endif
+ double tol_bnd; /* primal feasibility tolerance */
+ double tol_dj; /* dual feasibility tolerance */
+ double tol_piv; /* pivot tolerance */
+ double obj_ll; /* lower objective limit */
+ double obj_ul; /* upper objective limit */
+ int it_lim; /* simplex iteration limit */
+ int tm_lim; /* time limit, ms */
+ int out_frq; /* display output frequency, ms */
+ int out_dly; /* display output delay, ms */
+ int presolve; /* enable/disable using LP presolver */
+#if 1 /* 11/VII-2017 (not documented yet) */
+ int excl; /* exclude fixed non-basic variables */
+ int shift; /* shift bounds of variables to zero */
+ int aorn; /* option to use A or N: */
+#define GLP_USE_AT 1 /* use A matrix in row-wise format */
+#define GLP_USE_NT 2 /* use N matrix in row-wise format */
+ double foo_bar[33]; /* (reserved) */
+#endif
+} glp_smcp;
+
+typedef struct
+{ /* interior-point solver control parameters */
+ int msg_lev; /* message level (see glp_smcp) */
+ int ord_alg; /* ordering algorithm: */
+#define GLP_ORD_NONE 0 /* natural (original) ordering */
+#define GLP_ORD_QMD 1 /* quotient minimum degree (QMD) */
+#define GLP_ORD_AMD 2 /* approx. minimum degree (AMD) */
+#define GLP_ORD_SYMAMD 3 /* approx. minimum degree (SYMAMD) */
+ double foo_bar[48]; /* (reserved) */
+} glp_iptcp;
+
+typedef struct glp_tree glp_tree;
+/* branch-and-bound tree */
+
+typedef struct
+{ /* integer optimizer control parameters */
+ int msg_lev; /* message level (see glp_smcp) */
+ int br_tech; /* branching technique: */
+#define GLP_BR_FFV 1 /* first fractional variable */
+#define GLP_BR_LFV 2 /* last fractional variable */
+#define GLP_BR_MFV 3 /* most fractional variable */
+#define GLP_BR_DTH 4 /* heuristic by Driebeck and Tomlin */
+#define GLP_BR_PCH 5 /* hybrid pseudocost heuristic */
+ int bt_tech; /* backtracking technique: */
+#define GLP_BT_DFS 1 /* depth first search */
+#define GLP_BT_BFS 2 /* breadth first search */
+#define GLP_BT_BLB 3 /* best local bound */
+#define GLP_BT_BPH 4 /* best projection heuristic */
+ double tol_int; /* mip.tol_int */
+ double tol_obj; /* mip.tol_obj */
+ int tm_lim; /* mip.tm_lim (milliseconds) */
+ int out_frq; /* mip.out_frq (milliseconds) */
+ int out_dly; /* mip.out_dly (milliseconds) */
+ void (*cb_func)(glp_tree *T, void *info);
+ /* mip.cb_func */
+ void *cb_info; /* mip.cb_info */
+ int cb_size; /* mip.cb_size */
+ int pp_tech; /* preprocessing technique: */
+#define GLP_PP_NONE 0 /* disable preprocessing */
+#define GLP_PP_ROOT 1 /* preprocessing only on root level */
+#define GLP_PP_ALL 2 /* preprocessing on all levels */
+ double mip_gap; /* relative MIP gap tolerance */
+ int mir_cuts; /* MIR cuts (GLP_ON/GLP_OFF) */
+ int gmi_cuts; /* Gomory's cuts (GLP_ON/GLP_OFF) */
+ int cov_cuts; /* cover cuts (GLP_ON/GLP_OFF) */
+ int clq_cuts; /* clique cuts (GLP_ON/GLP_OFF) */
+ int presolve; /* enable/disable using MIP presolver */
+ int binarize; /* try to binarize integer variables */
+ int fp_heur; /* feasibility pump heuristic */
+ int ps_heur; /* proximity search heuristic */
+ int ps_tm_lim; /* proxy time limit, milliseconds */
+ int sr_heur; /* simple rounding heuristic */
+#if 1 /* 24/X-2015; not documented--should not be used */
+ int use_sol; /* use existing solution */
+ const char *save_sol; /* filename to save every new solution */
+ int alien; /* use alien solver */
+#endif
+#if 1 /* 16/III-2016; not documented--should not be used */
+ int flip; /* use long-step dual simplex */
+#endif
+ double foo_bar[23]; /* (reserved) */
+} glp_iocp;
+
+typedef struct
+{ /* additional row attributes */
+ int level;
+ /* subproblem level at which the row was added */
+ int origin;
+ /* row origin flag: */
+#define GLP_RF_REG 0 /* regular constraint */
+#define GLP_RF_LAZY 1 /* "lazy" constraint */
+#define GLP_RF_CUT 2 /* cutting plane constraint */
+ int klass;
+ /* row class descriptor: */
+#define GLP_RF_GMI 1 /* Gomory's mixed integer cut */
+#define GLP_RF_MIR 2 /* mixed integer rounding cut */
+#define GLP_RF_COV 3 /* mixed cover cut */
+#define GLP_RF_CLQ 4 /* clique cut */
+ double foo_bar[7];
+ /* (reserved) */
+} glp_attr;
+
+/* enable/disable flag: */
+#define GLP_ON 1 /* enable something */
+#define GLP_OFF 0 /* disable something */
+
+/* reason codes: */
+#define GLP_IROWGEN 0x01 /* request for row generation */
+#define GLP_IBINGO 0x02 /* better integer solution found */
+#define GLP_IHEUR 0x03 /* request for heuristic solution */
+#define GLP_ICUTGEN 0x04 /* request for cut generation */
+#define GLP_IBRANCH 0x05 /* request for branching */
+#define GLP_ISELECT 0x06 /* request for subproblem selection */
+#define GLP_IPREPRO 0x07 /* request for preprocessing */
+
+/* branch selection indicator: */
+#define GLP_NO_BRNCH 0 /* select no branch */
+#define GLP_DN_BRNCH 1 /* select down-branch */
+#define GLP_UP_BRNCH 2 /* select up-branch */
+
+/* return codes: */
+#define GLP_EBADB 0x01 /* invalid basis */
+#define GLP_ESING 0x02 /* singular matrix */
+#define GLP_ECOND 0x03 /* ill-conditioned matrix */
+#define GLP_EBOUND 0x04 /* invalid bounds */
+#define GLP_EFAIL 0x05 /* solver failed */
+#define GLP_EOBJLL 0x06 /* objective lower limit reached */
+#define GLP_EOBJUL 0x07 /* objective upper limit reached */
+#define GLP_EITLIM 0x08 /* iteration limit exceeded */
+#define GLP_ETMLIM 0x09 /* time limit exceeded */
+#define GLP_ENOPFS 0x0A /* no primal feasible solution */
+#define GLP_ENODFS 0x0B /* no dual feasible solution */
+#define GLP_EROOT 0x0C /* root LP optimum not provided */
+#define GLP_ESTOP 0x0D /* search terminated by application */
+#define GLP_EMIPGAP 0x0E /* relative mip gap tolerance reached */
+#define GLP_ENOFEAS 0x0F /* no primal/dual feasible solution */
+#define GLP_ENOCVG 0x10 /* no convergence */
+#define GLP_EINSTAB 0x11 /* numerical instability */
+#define GLP_EDATA 0x12 /* invalid data */
+#define GLP_ERANGE 0x13 /* result out of range */
+
+/* condition indicator: */
+#define GLP_KKT_PE 1 /* primal equalities */
+#define GLP_KKT_PB 2 /* primal bounds */
+#define GLP_KKT_DE 3 /* dual equalities */
+#define GLP_KKT_DB 4 /* dual bounds */
+#define GLP_KKT_CS 5 /* complementary slackness */
+
+/* MPS file format: */
+#define GLP_MPS_DECK 1 /* fixed (ancient) */
+#define GLP_MPS_FILE 2 /* free (modern) */
+
+typedef struct
+{ /* MPS format control parameters */
+ int blank;
+ /* character code to replace blanks in symbolic names */
+ char *obj_name;
+ /* objective row name */
+ double tol_mps;
+ /* zero tolerance for MPS data */
+ double foo_bar[17];
+ /* (reserved for use in the future) */
+} glp_mpscp;
+
+typedef struct
+{ /* CPLEX LP format control parameters */
+ double foo_bar[20];
+ /* (reserved for use in the future) */
+} glp_cpxcp;
+
+#if 1 /* 10/XII-2017 */
+typedef struct glp_prep glp_prep;
+/* LP/MIP preprocessor workspace */
+#endif
+
+typedef struct glp_tran glp_tran;
+/* MathProg translator workspace */
+
+glp_prob *glp_create_prob(void);
+/* create problem object */
+
+void glp_set_prob_name(glp_prob *P, const char *name);
+/* assign (change) problem name */
+
+void glp_set_obj_name(glp_prob *P, const char *name);
+/* assign (change) objective function name */
+
+void glp_set_obj_dir(glp_prob *P, int dir);
+/* set (change) optimization direction flag */
+
+int glp_add_rows(glp_prob *P, int nrs);
+/* add new rows to problem object */
+
+int glp_add_cols(glp_prob *P, int ncs);
+/* add new columns to problem object */
+
+void glp_set_row_name(glp_prob *P, int i, const char *name);
+/* assign (change) row name */
+
+void glp_set_col_name(glp_prob *P, int j, const char *name);
+/* assign (change) column name */
+
+void glp_set_row_bnds(glp_prob *P, int i, int type, double lb,
+ double ub);
+/* set (change) row bounds */
+
+void glp_set_col_bnds(glp_prob *P, int j, int type, double lb,
+ double ub);
+/* set (change) column bounds */
+
+void glp_set_obj_coef(glp_prob *P, int j, double coef);
+/* set (change) obj. coefficient or constant term */
+
+void glp_set_mat_row(glp_prob *P, int i, int len, const int ind[],
+ const double val[]);
+/* set (replace) row of the constraint matrix */
+
+void glp_set_mat_col(glp_prob *P, int j, int len, const int ind[],
+ const double val[]);
+/* set (replace) column of the constraint matrix */
+
+void glp_load_matrix(glp_prob *P, int ne, const int ia[],
+ const int ja[], const double ar[]);
+/* load (replace) the whole constraint matrix */
+
+int glp_check_dup(int m, int n, int ne, const int ia[], const int ja[]);
+/* check for duplicate elements in sparse matrix */
+
+void glp_sort_matrix(glp_prob *P);
+/* sort elements of the constraint matrix */
+
+void glp_del_rows(glp_prob *P, int nrs, const int num[]);
+/* delete specified rows from problem object */
+
+void glp_del_cols(glp_prob *P, int ncs, const int num[]);
+/* delete specified columns from problem object */
+
+void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names);
+/* copy problem object content */
+
+void glp_erase_prob(glp_prob *P);
+/* erase problem object content */
+
+void glp_delete_prob(glp_prob *P);
+/* delete problem object */
+
+const char *glp_get_prob_name(glp_prob *P);
+/* retrieve problem name */
+
+const char *glp_get_obj_name(glp_prob *P);
+/* retrieve objective function name */
+
+int glp_get_obj_dir(glp_prob *P);
+/* retrieve optimization direction flag */
+
+int glp_get_num_rows(glp_prob *P);
+/* retrieve number of rows */
+
+int glp_get_num_cols(glp_prob *P);
+/* retrieve number of columns */
+
+const char *glp_get_row_name(glp_prob *P, int i);
+/* retrieve row name */
+
+const char *glp_get_col_name(glp_prob *P, int j);
+/* retrieve column name */
+
+int glp_get_row_type(glp_prob *P, int i);
+/* retrieve row type */
+
+double glp_get_row_lb(glp_prob *P, int i);
+/* retrieve row lower bound */
+
+double glp_get_row_ub(glp_prob *P, int i);
+/* retrieve row upper bound */
+
+int glp_get_col_type(glp_prob *P, int j);
+/* retrieve column type */
+
+double glp_get_col_lb(glp_prob *P, int j);
+/* retrieve column lower bound */
+
+double glp_get_col_ub(glp_prob *P, int j);
+/* retrieve column upper bound */
+
+double glp_get_obj_coef(glp_prob *P, int j);
+/* retrieve obj. coefficient or constant term */
+
+int glp_get_num_nz(glp_prob *P);
+/* retrieve number of constraint coefficients */
+
+int glp_get_mat_row(glp_prob *P, int i, int ind[], double val[]);
+/* retrieve row of the constraint matrix */
+
+int glp_get_mat_col(glp_prob *P, int j, int ind[], double val[]);
+/* retrieve column of the constraint matrix */
+
+void glp_create_index(glp_prob *P);
+/* create the name index */
+
+int glp_find_row(glp_prob *P, const char *name);
+/* find row by its name */
+
+int glp_find_col(glp_prob *P, const char *name);
+/* find column by its name */
+
+void glp_delete_index(glp_prob *P);
+/* delete the name index */
+
+void glp_set_rii(glp_prob *P, int i, double rii);
+/* set (change) row scale factor */
+
+void glp_set_sjj(glp_prob *P, int j, double sjj);
+/* set (change) column scale factor */
+
+double glp_get_rii(glp_prob *P, int i);
+/* retrieve row scale factor */
+
+double glp_get_sjj(glp_prob *P, int j);
+/* retrieve column scale factor */
+
+void glp_scale_prob(glp_prob *P, int flags);
+/* scale problem data */
+
+void glp_unscale_prob(glp_prob *P);
+/* unscale problem data */
+
+void glp_set_row_stat(glp_prob *P, int i, int stat);
+/* set (change) row status */
+
+void glp_set_col_stat(glp_prob *P, int j, int stat);
+/* set (change) column status */
+
+void glp_std_basis(glp_prob *P);
+/* construct standard initial LP basis */
+
+void glp_adv_basis(glp_prob *P, int flags);
+/* construct advanced initial LP basis */
+
+void glp_cpx_basis(glp_prob *P);
+/* construct Bixby's initial LP basis */
+
+int glp_simplex(glp_prob *P, const glp_smcp *parm);
+/* solve LP problem with the simplex method */
+
+int glp_exact(glp_prob *P, const glp_smcp *parm);
+/* solve LP problem in exact arithmetic */
+
+void glp_init_smcp(glp_smcp *parm);
+/* initialize simplex method control parameters */
+
+int glp_get_status(glp_prob *P);
+/* retrieve generic status of basic solution */
+
+int glp_get_prim_stat(glp_prob *P);
+/* retrieve status of primal basic solution */
+
+int glp_get_dual_stat(glp_prob *P);
+/* retrieve status of dual basic solution */
+
+double glp_get_obj_val(glp_prob *P);
+/* retrieve objective value (basic solution) */
+
+int glp_get_row_stat(glp_prob *P, int i);
+/* retrieve row status */
+
+double glp_get_row_prim(glp_prob *P, int i);
+/* retrieve row primal value (basic solution) */
+
+double glp_get_row_dual(glp_prob *P, int i);
+/* retrieve row dual value (basic solution) */
+
+int glp_get_col_stat(glp_prob *P, int j);
+/* retrieve column status */
+
+double glp_get_col_prim(glp_prob *P, int j);
+/* retrieve column primal value (basic solution) */
+
+double glp_get_col_dual(glp_prob *P, int j);
+/* retrieve column dual value (basic solution) */
+
+int glp_get_unbnd_ray(glp_prob *P);
+/* determine variable causing unboundedness */
+
+#if 1 /* 08/VIII-2013; not documented yet */
+int glp_get_it_cnt(glp_prob *P);
+/* get simplex solver iteration count */
+#endif
+
+#if 1 /* 08/VIII-2013; not documented yet */
+void glp_set_it_cnt(glp_prob *P, int it_cnt);
+/* set simplex solver iteration count */
+#endif
+
+int glp_interior(glp_prob *P, const glp_iptcp *parm);
+/* solve LP problem with the interior-point method */
+
+void glp_init_iptcp(glp_iptcp *parm);
+/* initialize interior-point solver control parameters */
+
+int glp_ipt_status(glp_prob *P);
+/* retrieve status of interior-point solution */
+
+double glp_ipt_obj_val(glp_prob *P);
+/* retrieve objective value (interior point) */
+
+double glp_ipt_row_prim(glp_prob *P, int i);
+/* retrieve row primal value (interior point) */
+
+double glp_ipt_row_dual(glp_prob *P, int i);
+/* retrieve row dual value (interior point) */
+
+double glp_ipt_col_prim(glp_prob *P, int j);
+/* retrieve column primal value (interior point) */
+
+double glp_ipt_col_dual(glp_prob *P, int j);
+/* retrieve column dual value (interior point) */
+
+void glp_set_col_kind(glp_prob *P, int j, int kind);
+/* set (change) column kind */
+
+int glp_get_col_kind(glp_prob *P, int j);
+/* retrieve column kind */
+
+int glp_get_num_int(glp_prob *P);
+/* retrieve number of integer columns */
+
+int glp_get_num_bin(glp_prob *P);
+/* retrieve number of binary columns */
+
+int glp_intopt(glp_prob *P, const glp_iocp *parm);
+/* solve MIP problem with the branch-and-bound method */
+
+void glp_init_iocp(glp_iocp *parm);
+/* initialize integer optimizer control parameters */
+
+int glp_mip_status(glp_prob *P);
+/* retrieve status of MIP solution */
+
+double glp_mip_obj_val(glp_prob *P);
+/* retrieve objective value (MIP solution) */
+
+double glp_mip_row_val(glp_prob *P, int i);
+/* retrieve row value (MIP solution) */
+
+double glp_mip_col_val(glp_prob *P, int j);
+/* retrieve column value (MIP solution) */
+
+void glp_check_kkt(glp_prob *P, int sol, int cond, double *ae_max,
+ int *ae_ind, double *re_max, int *re_ind);
+/* check feasibility/optimality conditions */
+
+int glp_print_sol(glp_prob *P, const char *fname);
+/* write basic solution in printable format */
+
+int glp_read_sol(glp_prob *P, const char *fname);
+/* read basic solution from text file */
+
+int glp_write_sol(glp_prob *P, const char *fname);
+/* write basic solution to text file */
+
+int glp_print_ranges(glp_prob *P, int len, const int list[],
+ int flags, const char *fname);
+/* print sensitivity analysis report */
+
+int glp_print_ipt(glp_prob *P, const char *fname);
+/* write interior-point solution in printable format */
+
+int glp_read_ipt(glp_prob *P, const char *fname);
+/* read interior-point solution from text file */
+
+int glp_write_ipt(glp_prob *P, const char *fname);
+/* write interior-point solution to text file */
+
+int glp_print_mip(glp_prob *P, const char *fname);
+/* write MIP solution in printable format */
+
+int glp_read_mip(glp_prob *P, const char *fname);
+/* read MIP solution from text file */
+
+int glp_write_mip(glp_prob *P, const char *fname);
+/* write MIP solution to text file */
+
+int glp_bf_exists(glp_prob *P);
+/* check if LP basis factorization exists */
+
+int glp_factorize(glp_prob *P);
+/* compute LP basis factorization */
+
+int glp_bf_updated(glp_prob *P);
+/* check if LP basis factorization has been updated */
+
+void glp_get_bfcp(glp_prob *P, glp_bfcp *parm);
+/* retrieve LP basis factorization control parameters */
+
+void glp_set_bfcp(glp_prob *P, const glp_bfcp *parm);
+/* change LP basis factorization control parameters */
+
+int glp_get_bhead(glp_prob *P, int k);
+/* retrieve LP basis header information */
+
+int glp_get_row_bind(glp_prob *P, int i);
+/* retrieve row index in the basis header */
+
+int glp_get_col_bind(glp_prob *P, int j);
+/* retrieve column index in the basis header */
+
+void glp_ftran(glp_prob *P, double x[]);
+/* perform forward transformation (solve system B*x = b) */
+
+void glp_btran(glp_prob *P, double x[]);
+/* perform backward transformation (solve system B'*x = b) */
+
+int glp_warm_up(glp_prob *P);
+/* "warm up" LP basis */
+
+int glp_eval_tab_row(glp_prob *P, int k, int ind[], double val[]);
+/* compute row of the simplex tableau */
+
+int glp_eval_tab_col(glp_prob *P, int k, int ind[], double val[]);
+/* compute column of the simplex tableau */
+
+int glp_transform_row(glp_prob *P, int len, int ind[], double val[]);
+/* transform explicitly specified row */
+
+int glp_transform_col(glp_prob *P, int len, int ind[], double val[]);
+/* transform explicitly specified column */
+
+int glp_prim_rtest(glp_prob *P, int len, const int ind[],
+ const double val[], int dir, double eps);
+/* perform primal ratio test */
+
+int glp_dual_rtest(glp_prob *P, int len, const int ind[],
+ const double val[], int dir, double eps);
+/* perform dual ratio test */
+
+void glp_analyze_bound(glp_prob *P, int k, double *value1, int *var1,
+ double *value2, int *var2);
+/* analyze active bound of non-basic variable */
+
+void glp_analyze_coef(glp_prob *P, int k, double *coef1, int *var1,
+ double *value1, double *coef2, int *var2, double *value2);
+/* analyze objective coefficient at basic variable */
+
+#if 1 /* 10/XII-2017 */
+glp_prep *glp_npp_alloc_wksp(void);
+/* allocate the preprocessor workspace */
+
+void glp_npp_load_prob(glp_prep *prep, glp_prob *P, int sol,
+ int names);
+/* load original problem instance */
+
+int glp_npp_preprocess1(glp_prep *prep, int hard);
+/* perform basic LP/MIP preprocessing */
+
+void glp_npp_build_prob(glp_prep *prep, glp_prob *Q);
+/* build resultant problem instance */
+
+void glp_npp_postprocess(glp_prep *prep, glp_prob *Q);
+/* postprocess solution to resultant problem */
+
+void glp_npp_obtain_sol(glp_prep *prep, glp_prob *P);
+/* obtain solution to original problem */
+
+void glp_npp_free_wksp(glp_prep *prep);
+/* free the preprocessor workspace */
+#endif
+
+int glp_ios_reason(glp_tree *T);
+/* determine reason for calling the callback routine */
+
+glp_prob *glp_ios_get_prob(glp_tree *T);
+/* access the problem object */
+
+void glp_ios_tree_size(glp_tree *T, int *a_cnt, int *n_cnt,
+ int *t_cnt);
+/* determine size of the branch-and-bound tree */
+
+int glp_ios_curr_node(glp_tree *T);
+/* determine current active subproblem */
+
+int glp_ios_next_node(glp_tree *T, int p);
+/* determine next active subproblem */
+
+int glp_ios_prev_node(glp_tree *T, int p);
+/* determine previous active subproblem */
+
+int glp_ios_up_node(glp_tree *T, int p);
+/* determine parent subproblem */
+
+int glp_ios_node_level(glp_tree *T, int p);
+/* determine subproblem level */
+
+double glp_ios_node_bound(glp_tree *T, int p);
+/* determine subproblem local bound */
+
+int glp_ios_best_node(glp_tree *T);
+/* find active subproblem with best local bound */
+
+double glp_ios_mip_gap(glp_tree *T);
+/* compute relative MIP gap */
+
+void *glp_ios_node_data(glp_tree *T, int p);
+/* access subproblem application-specific data */
+
+void glp_ios_row_attr(glp_tree *T, int i, glp_attr *attr);
+/* retrieve additional row attributes */
+
+int glp_ios_pool_size(glp_tree *T);
+/* determine current size of the cut pool */
+
+int glp_ios_add_row(glp_tree *T,
+ const char *name, int klass, int flags, int len, const int ind[],
+ const double val[], int type, double rhs);
+/* add row (constraint) to the cut pool */
+
+void glp_ios_del_row(glp_tree *T, int i);
+/* remove row (constraint) from the cut pool */
+
+void glp_ios_clear_pool(glp_tree *T);
+/* remove all rows (constraints) from the cut pool */
+
+int glp_ios_can_branch(glp_tree *T, int j);
+/* check if can branch upon specified variable */
+
+void glp_ios_branch_upon(glp_tree *T, int j, int sel);
+/* choose variable to branch upon */
+
+void glp_ios_select_node(glp_tree *T, int p);
+/* select subproblem to continue the search */
+
+int glp_ios_heur_sol(glp_tree *T, const double x[]);
+/* provide solution found by heuristic */
+
+void glp_ios_terminate(glp_tree *T);
+/* terminate the solution process */
+
+#ifdef GLP_UNDOC
+int glp_gmi_cut(glp_prob *P, int j, int ind[], double val[], double
+ phi[]);
+/* generate Gomory's mixed integer cut (core routine) */
+
+int glp_gmi_gen(glp_prob *P, glp_prob *pool, int max_cuts);
+/* generate Gomory's mixed integer cuts */
+
+typedef struct glp_cov glp_cov;
+/* cover cur generator workspace */
+
+glp_cov *glp_cov_init(glp_prob *P);
+/* create and initialize cover cut generator */
+
+void glp_cov_gen1(glp_prob *P, glp_cov *cov, glp_prob *pool);
+/* generate locally valid simple cover cuts */
+
+void glp_cov_free(glp_cov *cov);
+/* delete cover cut generator workspace */
+
+typedef struct glp_mir glp_mir;
+/* MIR cut generator workspace */
+
+glp_mir *glp_mir_init(glp_prob *P);
+/* create and initialize MIR cut generator */
+
+int glp_mir_gen(glp_prob *P, glp_mir *mir, glp_prob *pool);
+/* generate mixed integer rounding (MIR) cuts */
+
+void glp_mir_free(glp_mir *mir);
+/* delete MIR cut generator workspace */
+
+typedef struct glp_cfg glp_cfg;
+/* conflict graph descriptor */
+
+glp_cfg *glp_cfg_init(glp_prob *P);
+/* create and initialize conflict graph */
+
+void glp_cfg_free(glp_cfg *G);
+/* delete conflict graph descriptor */
+
+int glp_clq_cut(glp_prob *P, glp_cfg *G, int ind[], double val[]);
+/* generate clique cut from conflict graph */
+#endif /* GLP_UNDOC */
+
+void glp_init_mpscp(glp_mpscp *parm);
+/* initialize MPS format control parameters */
+
+int glp_read_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+ const char *fname);
+/* read problem data in MPS format */
+
+int glp_write_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
+ const char *fname);
+/* write problem data in MPS format */
+
+void glp_init_cpxcp(glp_cpxcp *parm);
+/* initialize CPLEX LP format control parameters */
+
+int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname);
+/* read problem data in CPLEX LP format */
+
+int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname);
+/* write problem data in CPLEX LP format */
+
+int glp_read_prob(glp_prob *P, int flags, const char *fname);
+/* read problem data in GLPK format */
+
+int glp_write_prob(glp_prob *P, int flags, const char *fname);
+/* write problem data in GLPK format */
+
+glp_tran *glp_mpl_alloc_wksp(void);
+/* allocate the MathProg translator workspace */
+
+void glp_mpl_init_rand(glp_tran *tran, int seed);
+/* initialize pseudo-random number generator */
+
+int glp_mpl_read_model(glp_tran *tran, const char *fname, int skip);
+/* read and translate model section */
+
+int glp_mpl_read_data(glp_tran *tran, const char *fname);
+/* read and translate data section */
+
+int glp_mpl_generate(glp_tran *tran, const char *fname);
+/* generate the model */
+
+void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob);
+/* build LP/MIP problem instance from the model */
+
+int glp_mpl_postsolve(glp_tran *tran, glp_prob *prob, int sol);
+/* postsolve the model */
+
+void glp_mpl_free_wksp(glp_tran *tran);
+/* free the MathProg translator workspace */
+
+int glp_read_cnfsat(glp_prob *P, const char *fname);
+/* read CNF-SAT problem data in DIMACS format */
+
+int glp_check_cnfsat(glp_prob *P);
+/* check for CNF-SAT problem instance */
+
+int glp_write_cnfsat(glp_prob *P, const char *fname);
+/* write CNF-SAT problem data in DIMACS format */
+
+int glp_minisat1(glp_prob *P);
+/* solve CNF-SAT problem with MiniSat solver */
+
+int glp_intfeas1(glp_prob *P, int use_bound, int obj_bound);
+/* solve integer feasibility problem */
+
+int glp_init_env(void);
+/* initialize GLPK environment */
+
+const char *glp_version(void);
+/* determine library version */
+
+const char *glp_config(const char *option);
+/* determine library configuration */
+
+int glp_free_env(void);
+/* free GLPK environment */
+
+void glp_puts(const char *s);
+/* write string on terminal */
+
+void glp_printf(const char *fmt, ...);
+/* write formatted output on terminal */
+
+void glp_vprintf(const char *fmt, va_list arg);
+/* write formatted output on terminal */
+
+int glp_term_out(int flag);
+/* enable/disable terminal output */
+
+void glp_term_hook(int (*func)(void *info, const char *s), void *info);
+/* install hook to intercept terminal output */
+
+int glp_open_tee(const char *name);
+/* start copying terminal output to text file */
+
+int glp_close_tee(void);
+/* stop copying terminal output to text file */
+
+#ifndef GLP_ERRFUNC_DEFINED
+#define GLP_ERRFUNC_DEFINED
+typedef void (*glp_errfunc)(const char *fmt, ...);
+#endif
+
+#define glp_error glp_error_(__FILE__, __LINE__)
+glp_errfunc glp_error_(const char *file, int line);
+/* display fatal error message and terminate execution */
+
+#if 1 /* 07/XI-2015 */
+int glp_at_error(void);
+/* check for error state */
+#endif
+
+#define glp_assert(expr) \
+ ((void)((expr) || (glp_assert_(#expr, __FILE__, __LINE__), 1)))
+void glp_assert_(const char *expr, const char *file, int line);
+/* check for logical condition */
+
+void glp_error_hook(void (*func)(void *info), void *info);
+/* install hook to intercept abnormal termination */
+
+#define glp_malloc(size) glp_alloc(1, size)
+/* allocate memory block (obsolete) */
+
+#define glp_calloc(n, size) glp_alloc(n, size)
+/* allocate memory block (obsolete) */
+
+void *glp_alloc(int n, int size);
+/* allocate memory block */
+
+void *glp_realloc(void *ptr, int n, int size);
+/* reallocate memory block */
+
+void glp_free(void *ptr);
+/* free (deallocate) memory block */
+
+void glp_mem_limit(int limit);
+/* set memory usage limit */
+
+void glp_mem_usage(int *count, int *cpeak, size_t *total,
+ size_t *tpeak);
+/* get memory usage information */
+
+double glp_time(void);
+/* determine current universal time */
+
+double glp_difftime(double t1, double t0);
+/* compute difference between two time values */
+
+typedef struct glp_graph glp_graph;
+typedef struct glp_vertex glp_vertex;
+typedef struct glp_arc glp_arc;
+
+struct glp_graph
+{ /* graph descriptor */
+ void *pool; /* DMP *pool; */
+ /* memory pool to store graph components */
+ char *name;
+ /* graph name (1 to 255 chars); NULL means no name is assigned
+ to the graph */
+ int nv_max;
+ /* length of the vertex list (enlarged automatically) */
+ int nv;
+ /* number of vertices in the graph, 0 <= nv <= nv_max */
+ int na;
+ /* number of arcs in the graph, na >= 0 */
+ glp_vertex **v; /* glp_vertex *v[1+nv_max]; */
+ /* v[i], 1 <= i <= nv, is a pointer to i-th vertex */
+ void *index; /* AVL *index; */
+ /* vertex index to find vertices by their names; NULL means the
+ index does not exist */
+ int v_size;
+ /* size of data associated with each vertex (0 to 256 bytes) */
+ int a_size;
+ /* size of data associated with each arc (0 to 256 bytes) */
+};
+
+struct glp_vertex
+{ /* vertex descriptor */
+ int i;
+ /* vertex ordinal number, 1 <= i <= nv */
+ char *name;
+ /* vertex name (1 to 255 chars); NULL means no name is assigned
+ to the vertex */
+ void *entry; /* AVLNODE *entry; */
+ /* pointer to corresponding entry in the vertex index; NULL means
+ that either the index does not exist or the vertex has no name
+ assigned */
+ void *data;
+ /* pointer to data associated with the vertex */
+ void *temp;
+ /* working pointer */
+ glp_arc *in;
+ /* pointer to the (unordered) list of incoming arcs */
+ glp_arc *out;
+ /* pointer to the (unordered) list of outgoing arcs */
+};
+
+struct glp_arc
+{ /* arc descriptor */
+ glp_vertex *tail;
+ /* pointer to the tail endpoint */
+ glp_vertex *head;
+ /* pointer to the head endpoint */
+ void *data;
+ /* pointer to data associated with the arc */
+ void *temp;
+ /* working pointer */
+ glp_arc *t_prev;
+ /* pointer to previous arc having the same tail endpoint */
+ glp_arc *t_next;
+ /* pointer to next arc having the same tail endpoint */
+ glp_arc *h_prev;
+ /* pointer to previous arc having the same head endpoint */
+ glp_arc *h_next;
+ /* pointer to next arc having the same head endpoint */
+};
+
+glp_graph *glp_create_graph(int v_size, int a_size);
+/* create graph */
+
+void glp_set_graph_name(glp_graph *G, const char *name);
+/* assign (change) graph name */
+
+int glp_add_vertices(glp_graph *G, int nadd);
+/* add new vertices to graph */
+
+void glp_set_vertex_name(glp_graph *G, int i, const char *name);
+/* assign (change) vertex name */
+
+glp_arc *glp_add_arc(glp_graph *G, int i, int j);
+/* add new arc to graph */
+
+void glp_del_vertices(glp_graph *G, int ndel, const int num[]);
+/* delete vertices from graph */
+
+void glp_del_arc(glp_graph *G, glp_arc *a);
+/* delete arc from graph */
+
+void glp_erase_graph(glp_graph *G, int v_size, int a_size);
+/* erase graph content */
+
+void glp_delete_graph(glp_graph *G);
+/* delete graph */
+
+void glp_create_v_index(glp_graph *G);
+/* create vertex name index */
+
+int glp_find_vertex(glp_graph *G, const char *name);
+/* find vertex by its name */
+
+void glp_delete_v_index(glp_graph *G);
+/* delete vertex name index */
+
+int glp_read_graph(glp_graph *G, const char *fname);
+/* read graph from plain text file */
+
+int glp_write_graph(glp_graph *G, const char *fname);
+/* write graph to plain text file */
+
+void glp_mincost_lp(glp_prob *P, glp_graph *G, int names, int v_rhs,
+ int a_low, int a_cap, int a_cost);
+/* convert minimum cost flow problem to LP */
+
+int glp_mincost_okalg(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, double *sol, int a_x, int v_pi);
+/* find minimum-cost flow with out-of-kilter algorithm */
+
+int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, int crash, double *sol, int a_x, int a_rc);
+/* find minimum-cost flow with Bertsekas-Tseng relaxation method */
+
+void glp_maxflow_lp(glp_prob *P, glp_graph *G, int names, int s,
+ int t, int a_cap);
+/* convert maximum flow problem to LP */
+
+int glp_maxflow_ffalg(glp_graph *G, int s, int t, int a_cap,
+ double *sol, int a_x, int v_cut);
+/* find maximal flow with Ford-Fulkerson algorithm */
+
+int glp_check_asnprob(glp_graph *G, int v_set);
+/* check correctness of assignment problem data */
+
+/* assignment problem formulation: */
+#define GLP_ASN_MIN 1 /* perfect matching (minimization) */
+#define GLP_ASN_MAX 2 /* perfect matching (maximization) */
+#define GLP_ASN_MMP 3 /* maximum matching */
+
+int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names,
+ int v_set, int a_cost);
+/* convert assignment problem to LP */
+
+int glp_asnprob_okalg(int form, glp_graph *G, int v_set, int a_cost,
+ double *sol, int a_x);
+/* solve assignment problem with out-of-kilter algorithm */
+
+int glp_asnprob_hall(glp_graph *G, int v_set, int a_x);
+/* find bipartite matching of maximum cardinality */
+
+double glp_cpp(glp_graph *G, int v_t, int v_es, int v_ls);
+/* solve critical path problem */
+
+int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, const char *fname);
+/* read min-cost flow problem data in DIMACS format */
+
+int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
+ int a_cost, const char *fname);
+/* write min-cost flow problem data in DIMACS format */
+
+int glp_read_maxflow(glp_graph *G, int *s, int *t, int a_cap,
+ const char *fname);
+/* read maximum flow problem data in DIMACS format */
+
+int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap,
+ const char *fname);
+/* write maximum flow problem data in DIMACS format */
+
+int glp_read_asnprob(glp_graph *G, int v_set, int a_cost, const char
+ *fname);
+/* read assignment problem data in DIMACS format */
+
+int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, const char
+ *fname);
+/* write assignment problem data in DIMACS format */
+
+int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname);
+/* read graph in DIMACS clique/coloring format */
+
+int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname);
+/* write graph in DIMACS clique/coloring format */
+
+int glp_netgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
+ const int parm[1+15]);
+/* Klingman's network problem generator */
+
+void glp_netgen_prob(int nprob, int parm[1+15]);
+/* Klingman's standard network problem instance */
+
+int glp_gridgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
+ const int parm[1+14]);
+/* grid-like network problem generator */
+
+int glp_rmfgen(glp_graph *G, int *s, int *t, int a_cap,
+ const int parm[1+5]);
+/* Goldfarb's maximum flow problem generator */
+
+int glp_weak_comp(glp_graph *G, int v_num);
+/* find all weakly connected components of graph */
+
+int glp_strong_comp(glp_graph *G, int v_num);
+/* find all strongly connected components of graph */
+
+int glp_top_sort(glp_graph *G, int v_num);
+/* topological sorting of acyclic digraph */
+
+int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set);
+/* find maximum weight clique with exact algorithm */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/cfg.c b/test/monniaux/glpk-4.65/src/intopt/cfg.c
new file mode 100644
index 00000000..ab73b2da
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/cfg.c
@@ -0,0 +1,409 @@
+/* cfg.c (conflict graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "cfg.h"
+#include "env.h"
+
+/***********************************************************************
+* cfg_create_graph - create conflict graph
+*
+* This routine creates the conflict graph, which initially is empty,
+* and returns a pointer to the graph descriptor.
+*
+* The parameter n specifies the number of *all* variables in MIP, for
+* which the conflict graph will be built.
+*
+* The parameter nv_max specifies maximal number of vertices in the
+* conflict graph. It should be the double number of binary variables
+* in corresponding MIP. */
+
+CFG *cfg_create_graph(int n, int nv_max)
+{ CFG *G;
+ xassert(n >= 0);
+ xassert(0 <= nv_max && nv_max <= n + n);
+ G = talloc(1, CFG);
+ G->n = n;
+ G->pos = talloc(1+n, int);
+ memset(&G->pos[1], 0, n * sizeof(int));
+ G->neg = talloc(1+n, int);
+ memset(&G->neg[1], 0, n * sizeof(int));
+ G->pool = dmp_create_pool();
+ G->nv_max = nv_max;
+ G->nv = 0;
+ G->ref = talloc(1+nv_max, int);
+ G->vptr = talloc(1+nv_max, CFGVLE *);
+ G->cptr = talloc(1+nv_max, CFGCLE *);
+ return G;
+}
+
+/***********************************************************************
+* cfg_add_clique - add clique to conflict graph
+*
+* This routine adds a clique to the conflict graph.
+*
+* The parameter size specifies the clique size, size >= 2. Note that
+* any edge can be considered as a clique of size 2.
+*
+* The array ind specifies vertices constituting the clique in elements
+* ind[k], 1 <= k <= size:
+*
+* ind[k] = +j means a vertex of the conflict graph that corresponds to
+* original binary variable x[j], 1 <= j <= n.
+*
+* ind[k] = -j means a vertex of the conflict graph that corresponds to
+* complement of original binary variable x[j], 1 <= j <= n.
+*
+* Note that if both vertices for x[j] and (1 - x[j]) have appeared in
+* the conflict graph, the routine automatically adds an edge incident
+* to these vertices. */
+
+static void add_edge(CFG *G, int v, int w)
+{ /* add clique of size 2 */
+ DMP *pool = G->pool;
+ int nv = G->nv;
+ CFGVLE **vptr = G->vptr;
+ CFGVLE *vle;
+ xassert(1 <= v && v <= nv);
+ xassert(1 <= w && w <= nv);
+ xassert(v != w);
+ vle = dmp_talloc(pool, CFGVLE);
+ vle->v = w;
+ vle->next = vptr[v];
+ vptr[v] = vle;
+ vle = dmp_talloc(pool, CFGVLE);
+ vle->v = v;
+ vle->next = vptr[w];
+ vptr[w] = vle;
+ return;
+}
+
+void cfg_add_clique(CFG *G, int size, const int ind[])
+{ int n = G->n;
+ int *pos = G->pos;
+ int *neg = G->neg;
+ DMP *pool = G->pool;
+ int nv_max = G->nv_max;
+ int *ref = G->ref;
+ CFGVLE **vptr = G->vptr;
+ CFGCLE **cptr = G->cptr;
+ int j, k, v;
+ xassert(2 <= size && size <= nv_max);
+ /* add new vertices to the conflict graph */
+ for (k = 1; k <= size; k++)
+ { j = ind[k];
+ if (j > 0)
+ { /* vertex corresponds to x[j] */
+ xassert(1 <= j && j <= n);
+ if (pos[j] == 0)
+ { /* no such vertex exists; add it */
+ v = pos[j] = ++(G->nv);
+ xassert(v <= nv_max);
+ ref[v] = j;
+ vptr[v] = NULL;
+ cptr[v] = NULL;
+ if (neg[j] != 0)
+ { /* now both vertices for x[j] and (1 - x[j]) exist */
+ add_edge(G, v, neg[j]);
+ }
+ }
+ }
+ else
+ { /* vertex corresponds to (1 - x[j]) */
+ j = -j;
+ xassert(1 <= j && j <= n);
+ if (neg[j] == 0)
+ { /* no such vertex exists; add it */
+ v = neg[j] = ++(G->nv);
+ xassert(v <= nv_max);
+ ref[v] = j;
+ vptr[v] = NULL;
+ cptr[v] = NULL;
+ if (pos[j] != 0)
+ { /* now both vertices for x[j] and (1 - x[j]) exist */
+ add_edge(G, v, pos[j]);
+ }
+ }
+ }
+ }
+ /* add specified clique to the conflict graph */
+ if (size == 2)
+ add_edge(G,
+ ind[1] > 0 ? pos[+ind[1]] : neg[-ind[1]],
+ ind[2] > 0 ? pos[+ind[2]] : neg[-ind[2]]);
+ else
+ { CFGVLE *vp, *vle;
+ CFGCLE *cle;
+ /* build list of clique vertices */
+ vp = NULL;
+ for (k = 1; k <= size; k++)
+ { vle = dmp_talloc(pool, CFGVLE);
+ vle->v = ind[k] > 0 ? pos[+ind[k]] : neg[-ind[k]];
+ vle->next = vp;
+ vp = vle;
+ }
+ /* attach the clique to all its vertices */
+ for (k = 1; k <= size; k++)
+ { cle = dmp_talloc(pool, CFGCLE);
+ cle->vptr = vp;
+ v = ind[k] > 0 ? pos[+ind[k]] : neg[-ind[k]];
+ cle->next = cptr[v];
+ cptr[v] = cle;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* cfg_get_adjacent - get vertices adjacent to specified vertex
+*
+* This routine stores numbers of all vertices adjacent to specified
+* vertex v of the conflict graph in locations ind[1], ..., ind[len],
+* and returns len, 1 <= len <= nv-1, where nv is the total number of
+* vertices in the conflict graph.
+*
+* Note that the conflict graph defined by this routine has neither
+* self-loops nor multiple edges. */
+
+int cfg_get_adjacent(CFG *G, int v, int ind[])
+{ int nv = G->nv;
+ int *ref = G->ref;
+ CFGVLE **vptr = G->vptr;
+ CFGCLE **cptr = G->cptr;
+ CFGVLE *vle;
+ CFGCLE *cle;
+ int k, w, len;
+ xassert(1 <= v && v <= nv);
+ len = 0;
+ /* walk thru the list of adjacent vertices */
+ for (vle = vptr[v]; vle != NULL; vle = vle->next)
+ { w = vle->v;
+ xassert(1 <= w && w <= nv);
+ xassert(w != v);
+ if (ref[w] > 0)
+ { ind[++len] = w;
+ ref[w] = -ref[w];
+ }
+ }
+ /* walk thru the list of incident cliques */
+ for (cle = cptr[v]; cle != NULL; cle = cle->next)
+ { /* walk thru the list of clique vertices */
+ for (vle = cle->vptr; vle != NULL; vle = vle->next)
+ { w = vle->v;
+ xassert(1 <= w && w <= nv);
+ if (w != v && ref[w] > 0)
+ { ind[++len] = w;
+ ref[w] = -ref[w];
+ }
+ }
+ }
+ xassert(1 <= len && len < nv);
+ /* unmark vertices included in the resultant adjacency list */
+ for (k = 1; k <= len; k++)
+ { w = ind[k];
+ ref[w] = -ref[w];
+ }
+ return len;
+}
+
+/***********************************************************************
+* cfg_expand_clique - expand specified clique to maximal clique
+*
+* Given some clique in the conflict graph this routine expands it to
+* a maximal clique by including in it new vertices.
+*
+* On entry vertex indices constituting the initial clique should be
+* stored in locations c_ind[1], ..., c_ind[c_len], where c_len is the
+* initial clique size. On exit the routine stores new vertex indices
+* to locations c_ind[c_len+1], ..., c_ind[c_len'], where c_len' is the
+* size of the maximal clique found, and returns c_len'.
+*
+* ALGORITHM
+*
+* Let G = (V, E) be a graph, C within V be a current clique to be
+* expanded, and D within V \ C be a subset of vertices adjacent to all
+* vertices from C. On every iteration the routine chooses some vertex
+* v in D, includes it into C, and removes from D the vertex v as well
+* as all vertices not adjacent to v. Initially C is empty and D = V.
+* Iterations repeat until D becomes an empty set. Obviously, the final
+* set C is a maximal clique in G.
+*
+* Now let C0 be an initial clique, and we want C0 to be a subset of
+* the final maximal clique C. To provide this condition the routine
+* starts constructing C by choosing only such vertices v in D, which
+* are in C0, until all vertices from C0 have been included in C. May
+* note that if on some iteration C0 \ C is non-empty (i.e. if not all
+* vertices from C0 have been included in C), C0 \ C is a subset of D,
+* because C0 is a clique. */
+
+static int intersection(int d_len, int d_ind[], int d_pos[], int len,
+ const int ind[])
+{ /* compute intersection D := D inter W, where W is some specified
+ * set of vertices */
+ int k, t, v, new_len;
+ /* walk thru vertices in W and mark vertices in D */
+ for (t = 1; t <= len; t++)
+ { /* v in W */
+ v = ind[t];
+ /* determine position of v in D */
+ k = d_pos[v];
+ if (k != 0)
+ { /* v in D */
+ xassert(d_ind[k] == v);
+ /* mark v to keep it in D */
+ d_ind[k] = -v;
+ }
+ }
+ /* remove all unmarked vertices from D */
+ new_len = 0;
+ for (k = 1; k <= d_len; k++)
+ { /* v in D */
+ v = d_ind[k];
+ if (v < 0)
+ { /* v is marked; keep it */
+ v = -v;
+ new_len++;
+ d_ind[new_len] = v;
+ d_pos[v] = new_len;
+ }
+ else
+ { /* v is not marked; remove it */
+ d_pos[v] = 0;
+ }
+ }
+ return new_len;
+}
+
+int cfg_expand_clique(CFG *G, int c_len, int c_ind[])
+{ int nv = G->nv;
+ int d_len, *d_ind, *d_pos, len, *ind;
+ int k, v;
+ xassert(0 <= c_len && c_len <= nv);
+ /* allocate working arrays */
+ d_ind = talloc(1+nv, int);
+ d_pos = talloc(1+nv, int);
+ ind = talloc(1+nv, int);
+ /* initialize C := 0, D := V */
+ d_len = nv;
+ for (k = 1; k <= nv; k++)
+ d_ind[k] = d_pos[k] = k;
+ /* expand C by vertices of specified initial clique C0 */
+ for (k = 1; k <= c_len; k++)
+ { /* v in C0 */
+ v = c_ind[k];
+ xassert(1 <= v && v <= nv);
+ /* since C0 is clique, v should be in D */
+ xassert(d_pos[v] != 0);
+ /* W := set of vertices adjacent to v */
+ len = cfg_get_adjacent(G, v, ind);
+ /* D := D inter W */
+ d_len = intersection(d_len, d_ind, d_pos, len, ind);
+ /* since v not in W, now v should be not in D */
+ xassert(d_pos[v] == 0);
+ }
+ /* expand C by some other vertices until D is empty */
+ while (d_len > 0)
+ { /* v in D */
+ v = d_ind[1];
+ xassert(1 <= v && v <= nv);
+ /* note that v is adjacent to all vertices in C (by design),
+ * so add v to C */
+ c_ind[++c_len] = v;
+ /* W := set of vertices adjacent to v */
+ len = cfg_get_adjacent(G, v, ind);
+ /* D := D inter W */
+ d_len = intersection(d_len, d_ind, d_pos, len, ind);
+ /* since v not in W, now v should be not in D */
+ xassert(d_pos[v] == 0);
+ }
+ /* free working arrays */
+ tfree(d_ind);
+ tfree(d_pos);
+ tfree(ind);
+ /* bring maximal clique to calling routine */
+ return c_len;
+}
+
+/***********************************************************************
+* cfg_check_clique - check clique in conflict graph
+*
+* This routine checks that vertices of the conflict graph specified
+* in locations c_ind[1], ..., c_ind[c_len] constitute a clique.
+*
+* NOTE: for testing/debugging only. */
+
+void cfg_check_clique(CFG *G, int c_len, const int c_ind[])
+{ int nv = G->nv;
+ int k, kk, v, w, len, *ind;
+ char *flag;
+ ind = talloc(1+nv, int);
+ flag = talloc(1+nv, char);
+ memset(&flag[1], 0, nv);
+ /* walk thru clique vertices */
+ xassert(c_len >= 0);
+ for (k = 1; k <= c_len; k++)
+ { /* get clique vertex v */
+ v = c_ind[k];
+ xassert(1 <= v && v <= nv);
+ /* get vertices adjacent to vertex v */
+ len = cfg_get_adjacent(G, v, ind);
+ for (kk = 1; kk <= len; kk++)
+ { w = ind[kk];
+ xassert(1 <= w && w <= nv);
+ xassert(w != v);
+ flag[w] = 1;
+ }
+ /* check that all clique vertices other than v are adjacent
+ to v */
+ for (kk = 1; kk <= c_len; kk++)
+ { w = c_ind[kk];
+ xassert(1 <= w && w <= nv);
+ if (w != v)
+ xassert(flag[w]);
+ }
+ /* reset vertex flags */
+ for (kk = 1; kk <= len; kk++)
+ flag[ind[kk]] = 0;
+ }
+ tfree(ind);
+ tfree(flag);
+ return;
+}
+
+/***********************************************************************
+* cfg_delete_graph - delete conflict graph
+*
+* This routine deletes the conflict graph by freeing all the memory
+* allocated to this program object. */
+
+void cfg_delete_graph(CFG *G)
+{ tfree(G->pos);
+ tfree(G->neg);
+ dmp_delete_pool(G->pool);
+ tfree(G->ref);
+ tfree(G->vptr);
+ tfree(G->cptr);
+ tfree(G);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/cfg.h b/test/monniaux/glpk-4.65/src/intopt/cfg.h
new file mode 100644
index 00000000..d478f6c0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/cfg.h
@@ -0,0 +1,138 @@
+/* cfg.h (conflict graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef CFG_H
+#define CFG_H
+
+#include "dmp.h"
+
+/***********************************************************************
+* The structure CFG describes the conflict graph.
+*
+* Conflict graph is an undirected graph G = (V, E), where V is a set
+* of vertices, E <= V x V is a set of edges. Each vertex v in V of the
+* conflict graph corresponds to a binary variable z[v], which is
+* either an original binary variable x[j] or its complement 1 - x[j].
+* Edge (v,w) in E means that z[v] and z[w] cannot take the value 1 at
+* the same time, i.e. it defines an inequality z[v] + z[w] <= 1, which
+* is assumed to be valid for original MIP.
+*
+* Since the conflict graph may be dense, it is stored as an union of
+* its cliques rather than explicitly. */
+
+#if 0 /* 08/III-2016 */
+typedef struct CFG CFG;
+#else
+typedef struct glp_cfg CFG;
+#endif
+typedef struct CFGVLE CFGVLE;
+typedef struct CFGCLE CFGCLE;
+
+#if 0 /* 08/III-2016 */
+struct CFG
+#else
+struct glp_cfg
+#endif
+{ /* conflict graph descriptor */
+ int n;
+ /* number of *all* variables (columns) in corresponding MIP */
+ int *pos; /* int pos[1+n]; */
+ /* pos[0] is not used;
+ * pos[j] = v, 1 <= j <= n, means that vertex v corresponds to
+ * original binary variable x[j], and pos[j] = 0 means that the
+ * conflict graph has no such vertex */
+ int *neg; /* int neg[1+n]; */
+ /* neg[0] is not used;
+ * neg[j] = v, 1 <= j <= n, means that vertex v corresponds to
+ * complement of original binary variable x[j], and neg[j] = 0
+ * means that the conflict graph has no such vertex */
+ DMP *pool;
+ /* memory pool to allocate elements of the conflict graph */
+ int nv_max;
+ /* maximal number of vertices in the conflict graph */
+ int nv;
+ /* current number of vertices in the conflict graph */
+ int *ref; /* int ref[1+nv_max]; */
+ /* ref[v] = j, 1 <= v <= nv, means that vertex v corresponds
+ * either to original binary variable x[j] or to its complement,
+ * i.e. either pos[j] = v or neg[j] = v */
+ CFGVLE **vptr; /* CFGVLE *vptr[1+nv_max]; */
+ /* vptr[v], 1 <= v <= nv, is an initial pointer to the list of
+ * vertices adjacent to vertex v */
+ CFGCLE **cptr; /* CFGCLE *cptr[1+nv_max]; */
+ /* cptr[v], 1 <= v <= nv, is an initial pointer to the list of
+ * cliques that contain vertex v */
+};
+
+struct CFGVLE
+{ /* vertex list element */
+ int v;
+ /* vertex number, 1 <= v <= nv */
+ CFGVLE *next;
+ /* pointer to next vertex list element */
+};
+
+struct CFGCLE
+{ /* clique list element */
+ CFGVLE *vptr;
+ /* initial pointer to the list of clique vertices */
+ CFGCLE *next;
+ /* pointer to next clique list element */
+};
+
+#define cfg_create_graph _glp_cfg_create_graph
+CFG *cfg_create_graph(int n, int nv_max);
+/* create conflict graph */
+
+#define cfg_add_clique _glp_cfg_add_clique
+void cfg_add_clique(CFG *G, int size, const int ind[]);
+/* add clique to conflict graph */
+
+#define cfg_get_adjacent _glp_cfg_get_adjacent
+int cfg_get_adjacent(CFG *G, int v, int ind[]);
+/* get vertices adjacent to specified vertex */
+
+#define cfg_expand_clique _glp_cfg_expand_clique
+int cfg_expand_clique(CFG *G, int c_len, int c_ind[]);
+/* expand specified clique to maximal clique */
+
+#define cfg_check_clique _glp_cfg_check_clique
+void cfg_check_clique(CFG *G, int c_len, const int c_ind[]);
+/* check clique in conflict graph */
+
+#define cfg_delete_graph _glp_cfg_delete_graph
+void cfg_delete_graph(CFG *G);
+/* delete conflict graph */
+
+#define cfg_build_graph _glp_cfg_build_graph
+CFG *cfg_build_graph(void /* glp_prob */ *P);
+/* build conflict graph */
+
+#define cfg_find_clique _glp_cfg_find_clique
+int cfg_find_clique(void /* glp_prob */ *P, CFG *G, int ind[],
+ double *sum);
+/* find maximum weight clique in conflict graph */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/cfg1.c b/test/monniaux/glpk-4.65/src/intopt/cfg1.c
new file mode 100644
index 00000000..80a2e834
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/cfg1.c
@@ -0,0 +1,703 @@
+/* cfg1.c (conflict graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "cfg.h"
+#include "env.h"
+#include "prob.h"
+#include "wclique.h"
+#include "wclique1.h"
+
+/***********************************************************************
+* cfg_build_graph - build conflict graph
+*
+* This routine builds the conflict graph. It analyzes the specified
+* problem object to discover original and implied packing inequalities
+* and adds corresponding cliques to the conflict graph.
+*
+* Packing inequality has the form:
+*
+* sum z[j] <= 1, (1)
+* j in J
+*
+* where z[j] = x[j] or z[j] = 1 - x[j], x[j] is an original binary
+* variable. Every packing inequality (1) is equivalent to a set of
+* edge inequalities:
+*
+* z[i] + z[j] <= 1 for all i, j in J, i != j, (2)
+*
+* and since every edge inequality (2) defines an edge in the conflict
+* graph, corresponding packing inequality (1) defines a clique.
+*
+* To discover packing inequalities the routine analyzes constraints
+* of the specified MIP. To simplify the analysis each constraint is
+* analyzed separately. The analysis is performed as follows.
+*
+* Let some original constraint be the following:
+*
+* L <= sum a[j] x[j] <= U. (3)
+*
+* To analyze it the routine analyzes two constraints of "not greater
+* than" type:
+*
+* sum (-a[j]) x[j] <= -L, (4)
+*
+* sum (+a[j]) x[j] <= +U, (5)
+*
+* which are relaxations of the original constraint (3). (If, however,
+* L = -oo, or U = +oo, corresponding constraint being redundant is not
+* analyzed.)
+*
+* Let a constraint of "not greater than" type be the following:
+*
+* sum a[j] x[j] + sum a[j] x[j] <= b, (6)
+* j in J j in J'
+*
+* where J is a subset of binary variables, J' is a subset of other
+* (continues and non-binary integer) variables. The constraint (6) is
+* is relaxed as follows, to eliminate non-binary variables:
+*
+* sum a[j] x[j] <= b - sum a[j] x[j] <= b', (7)
+* j in J j in J'
+*
+* b' = sup(b - sum a[j] x[j]) =
+* j in J'
+*
+* = b - inf(sum a[j] x[j]) =
+*
+* = b - sum inf(a[j] x[j]) = (8)
+*
+* = b - sum a[j] inf(x[j]) - sum a[j] sup(x[j]) =
+* a[j]>0 a[j]<0
+*
+* = b - sum a[j] l[j] - sum a[j] u[j],
+* a[j]>0 a[j]<0
+*
+* where l[j] and u[j] are, resp., lower and upper bounds of x[j].
+*
+* Then the routine transforms the relaxed constraint containing only
+* binary variables:
+*
+* sum a[j] x[j] <= b (9)
+*
+* to an equivalent 0-1 knapsack constraint as follows:
+*
+* sum a[j] x[j] + sum a[j] x[j] <= b ==>
+* a[j]>0 a[j]<0
+*
+* sum a[j] x[j] + sum a[j] (1 - x[j]) <= b ==>
+* a[j]>0 a[j]<0 (10)
+*
+* sum (+a[j]) x[j] + sum (-a[j]) x[j] <= b + sum (-a[j]) ==>
+* a[j]>0 a[j]<0 a[j]<0
+*
+* sum a'[j] z[j] <= b',
+*
+* where a'[j] = |a[j]| > 0, and
+*
+* ( x[j] if a[j] > 0
+* z[j] = <
+* ( 1 - x[j] if a[j] < 0
+*
+* is a binary variable, which is either original binary variable x[j]
+* or its complement.
+*
+* Finally, the routine analyzes the resultant 0-1 knapsack inequality:
+*
+* sum a[j] z[j] <= b, (11)
+* j in J
+*
+* where all a[j] are positive, to discover clique inequalities (1),
+* which are valid for (11) and therefore valid for (3). (It is assumed
+* that the original MIP has been preprocessed, so it is not checked,
+* for example, that b > 0 or that a[j] <= b.)
+*
+* In principle, to discover any edge inequalities valid for (11) it
+* is sufficient to check whether a[i] + a[j] > b for all i, j in J,
+* i < j. However, this way requires O(|J|^2) checks, so the routine
+* analyses (11) in the following way, which is much more efficient in
+* many practical cases.
+*
+* 1. Let a[p] and a[q] be two minimal coefficients:
+*
+* a[p] = min a[j], (12)
+*
+* a[q] = min a[j], j != p, (13)
+*
+* such that
+*
+* a[p] + a[q] > b. (14)
+*
+* This means that a[i] + a[j] > b for any i, j in J, i != j, so
+*
+* z[i] + z[j] <= 1 (15)
+*
+* are valid for (11) for any i, j in J, i != j. This case means that
+* J define a clique in the conflict graph.
+*
+* 2. Otherwise, let a[p] and [q] be two maximal coefficients:
+*
+* a[p] = max a[j], (16)
+*
+* a[q] = max a[j], j != p, (17)
+*
+* such that
+*
+* a[p] + a[q] <= b. (18)
+*
+* This means that a[i] + a[j] <= b for any i, j in J, i != j, so in
+* this case no valid edge inequalities for (11) exist.
+*
+* 3. Otherwise, let all a[j] be ordered by descending their values:
+*
+* a[1] >= a[2] >= ... >= a[p-1] >= a[p] >= a[p+1] >= ... (19)
+*
+* where p is such that
+*
+* a[p-1] + a[p] > b, (20)
+*
+* a[p] + a[p+1] <= b. (21)
+*
+* (May note that due to the former two cases in this case we always
+* have 2 <= p <= |J|-1.)
+*
+* Since a[p] and a[p-1] are two minimal coefficients in the set
+* J' = {1, ..., p}, J' define a clique in the conflict graph for the
+* same reason as in the first case. Similarly, since a[p] and a[p+1]
+* are two maximal coefficients in the set J" = {p, ..., |J|}, no edge
+* inequalities exist for all i, j in J" for the same reason as in the
+* second case. Thus, to discover other edge inequalities (15) valid
+* for (11), the routine checks if a[i] + a[j] > b for all i in J',
+* j in J", i != j. */
+
+#define is_binary(j) \
+ (P->col[j]->kind == GLP_IV && P->col[j]->type == GLP_DB && \
+ P->col[j]->lb == 0.0 && P->col[j]->ub == 1.0)
+/* check if x[j] is binary variable */
+
+struct term { int ind; double val; };
+/* term a[j] * z[j] used to sort a[j]'s */
+
+static int CDECL fcmp(const void *e1, const void *e2)
+{ /* auxiliary routine called from qsort */
+ const struct term *t1 = e1, *t2 = e2;
+ if (t1->val > t2->val)
+ return -1;
+ else if (t1->val < t2->val)
+ return +1;
+ else
+ return 0;
+}
+
+static void analyze_ineq(glp_prob *P, CFG *G, int len, int ind[],
+ double val[], double rhs, struct term t[])
+{ /* analyze inequality constraint (6) */
+ /* P is the original MIP
+ * G is the conflict graph to be built
+ * len is the number of terms in the constraint
+ * ind[1], ..., ind[len] are indices of variables x[j]
+ * val[1], ..., val[len] are constraint coefficients a[j]
+ * rhs is the right-hand side b
+ * t[1+len] is a working array */
+ int j, k, kk, p, q, type, new_len;
+ /* eliminate non-binary variables; see (7) and (8) */
+ new_len = 0;
+ for (k = 1; k <= len; k++)
+ { /* get index of variable x[j] */
+ j = ind[k];
+ if (is_binary(j))
+ { /* x[j] remains in relaxed constraint */
+ new_len++;
+ ind[new_len] = j;
+ val[new_len] = val[k];
+ }
+ else if (val[k] > 0.0)
+ { /* eliminate non-binary x[j] in case a[j] > 0 */
+ /* b := b - a[j] * l[j]; see (8) */
+ type = P->col[j]->type;
+ if (type == GLP_FR || type == GLP_UP)
+ { /* x[j] has no lower bound */
+ goto done;
+ }
+ rhs -= val[k] * P->col[j]->lb;
+ }
+ else /* val[j] < 0.0 */
+ { /* eliminate non-binary x[j] in case a[j] < 0 */
+ /* b := b - a[j] * u[j]; see (8) */
+ type = P->col[j]->type;
+ if (type == GLP_FR || type == GLP_LO)
+ { /* x[j] has no upper bound */
+ goto done;
+ }
+ rhs -= val[k] * P->col[j]->ub;
+ }
+ }
+ len = new_len;
+ /* now we have the constraint (9) */
+ if (len <= 1)
+ { /* at least two terms are needed */
+ goto done;
+ }
+ /* make all constraint coefficients positive; see (10) */
+ for (k = 1; k <= len; k++)
+ { if (val[k] < 0.0)
+ { /* a[j] < 0; substitute x[j] = 1 - x'[j], where x'[j] is
+ * a complement binary variable */
+ ind[k] = -ind[k];
+ val[k] = -val[k];
+ rhs += val[k];
+ }
+ }
+ /* now we have 0-1 knapsack inequality (11) */
+ /* increase the right-hand side a bit to avoid false checks due
+ * to rounding errors */
+ rhs += 0.001 * (1.0 + fabs(rhs));
+ /*** first case ***/
+ /* find two minimal coefficients a[p] and a[q] */
+ p = 0;
+ for (k = 1; k <= len; k++)
+ { if (p == 0 || val[p] > val[k])
+ p = k;
+ }
+ q = 0;
+ for (k = 1; k <= len; k++)
+ { if (k != p && (q == 0 || val[q] > val[k]))
+ q = k;
+ }
+ xassert(p != 0 && q != 0 && p != q);
+ /* check condition (14) */
+ if (val[p] + val[q] > rhs)
+ { /* all z[j] define a clique in the conflict graph */
+ cfg_add_clique(G, len, ind);
+ goto done;
+ }
+ /*** second case ***/
+ /* find two maximal coefficients a[p] and a[q] */
+ p = 0;
+ for (k = 1; k <= len; k++)
+ { if (p == 0 || val[p] < val[k])
+ p = k;
+ }
+ q = 0;
+ for (k = 1; k <= len; k++)
+ { if (k != p && (q == 0 || val[q] < val[k]))
+ q = k;
+ }
+ xassert(p != 0 && q != 0 && p != q);
+ /* check condition (18) */
+ if (val[p] + val[q] <= rhs)
+ { /* no valid edge inequalities exist */
+ goto done;
+ }
+ /*** third case ***/
+ xassert(len >= 3);
+ /* sort terms in descending order of coefficient values */
+ for (k = 1; k <= len; k++)
+ { t[k].ind = ind[k];
+ t[k].val = val[k];
+ }
+ qsort(&t[1], len, sizeof(struct term), fcmp);
+ for (k = 1; k <= len; k++)
+ { ind[k] = t[k].ind;
+ val[k] = t[k].val;
+ }
+ /* now a[1] >= a[2] >= ... >= a[len-1] >= a[len] */
+ /* note that a[1] + a[2] > b and a[len-1] + a[len] <= b due two
+ * the former two cases */
+ xassert(val[1] + val[2] > rhs);
+ xassert(val[len-1] + val[len] <= rhs);
+ /* find p according to conditions (20) and (21) */
+ for (p = 2; p < len; p++)
+ { if (val[p] + val[p+1] <= rhs)
+ break;
+ }
+ xassert(p < len);
+ /* z[1], ..., z[p] define a clique in the conflict graph */
+ cfg_add_clique(G, p, ind);
+ /* discover other edge inequalities */
+ for (k = 1; k <= p; k++)
+ { for (kk = p; kk <= len; kk++)
+ { if (k != kk && val[k] + val[kk] > rhs)
+ { int iii[1+2];
+ iii[1] = ind[k];
+ iii[2] = ind[kk];
+ cfg_add_clique(G, 2, iii);
+ }
+ }
+ }
+done: return;
+}
+
+CFG *cfg_build_graph(void *P_)
+{ glp_prob *P = P_;
+ int m = P->m;
+ int n = P->n;
+ CFG *G;
+ int i, k, type, len, *ind;
+ double *val;
+ struct term *t;
+ /* create the conflict graph (number of its vertices cannot be
+ * greater than double number of binary variables) */
+ G = cfg_create_graph(n, 2 * glp_get_num_bin(P));
+ /* allocate working arrays */
+ ind = talloc(1+n, int);
+ val = talloc(1+n, double);
+ t = talloc(1+n, struct term);
+ /* analyze constraints to discover edge inequalities */
+ for (i = 1; i <= m; i++)
+ { type = P->row[i]->type;
+ if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
+ { /* i-th row has lower bound */
+ /* analyze inequality sum (-a[j]) * x[j] <= -lb */
+ len = glp_get_mat_row(P, i, ind, val);
+ for (k = 1; k <= len; k++)
+ val[k] = -val[k];
+ analyze_ineq(P, G, len, ind, val, -P->row[i]->lb, t);
+ }
+ if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
+ { /* i-th row has upper bound */
+ /* analyze inequality sum (+a[j]) * x[j] <= +ub */
+ len = glp_get_mat_row(P, i, ind, val);
+ analyze_ineq(P, G, len, ind, val, +P->row[i]->ub, t);
+ }
+ }
+ /* free working arrays */
+ tfree(ind);
+ tfree(val);
+ tfree(t);
+ return G;
+}
+
+/***********************************************************************
+* cfg_find_clique - find maximum weight clique in conflict graph
+*
+* This routine finds a maximum weight clique in the conflict graph
+* G = (V, E), where the weight of vertex v in V is the value of
+* corresponding binary variable z (which is either an original binary
+* variable or its complement) in the optimal solution to LP relaxation
+* provided in the problem object. The goal is to find a clique in G,
+* whose weight is greater than 1, in which case corresponding packing
+* inequality is violated at the optimal point.
+*
+* On exit the routine stores vertex indices of the conflict graph
+* included in the clique found to locations ind[1], ..., ind[len], and
+* returns len, which is the clique size. The clique weight is stored
+* in location pointed to by the parameter sum. If no clique has been
+* found, the routine returns 0.
+*
+* Since the conflict graph may have a big number of vertices and be
+* quite dense, the routine uses an induced subgraph G' = (V', E'),
+* which is constructed as follows:
+*
+* 1. If the weight of some vertex v in V is zero (close to zero), it
+* is not included in V'. Obviously, including in a clique
+* zero-weight vertices does not change its weight, so if in G there
+* exist a clique of a non-zero weight, in G' exists a clique of the
+* same weight. This point is extremely important, because dropping
+* out zero-weight vertices can be done without retrieving lists of
+* adjacent vertices whose size may be very large.
+*
+* 2. Cumulative weight of vertex v in V is the sum of the weight of v
+* and weights of all vertices in V adjacent to v. Obviously, if
+* a clique includes a vertex v, the clique weight cannot be greater
+* than the cumulative weight of v. Since we are interested only in
+* cliques whose weight is greater than 1, vertices of V, whose
+* cumulative weight is not greater than 1, are not included in V'.
+*
+* May note that in many practical cases the size of the induced
+* subgraph G' is much less than the size of the original conflict
+* graph G due to many binary variables, whose optimal values are zero
+* or close to zero. For example, it may happen that |V| = 100,000 and
+* |E| = 1e9 while |V'| = 50 and |E'| = 1000. */
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* original MIP */
+ CFG *G;
+ /* original conflict graph G = (V, E), |V| = nv */
+ int *ind; /* int ind[1+nv]; */
+ /* working array */
+ /*--------------------------------------------------------------*/
+ /* induced subgraph G' = (V', E') of original conflict graph */
+ int nn;
+ /* number of vertices in V' */
+ int *vtoi; /* int vtoi[1+nv]; */
+ /* vtoi[v] = i, 1 <= v <= nv, means that vertex v in V is vertex
+ * i in V'; vtoi[v] = 0 means that vertex v is not included in
+ * the subgraph */
+ int *itov; /* int itov[1+nv]; */
+ /* itov[i] = v, 1 <= i <= nn, means that vertex i in V' is vertex
+ * v in V */
+ double *wgt; /* double wgt[1+nv]; */
+ /* wgt[i], 1 <= i <= nn, is a weight of vertex i in V', which is
+ * the value of corresponding binary variable in optimal solution
+ * to LP relaxation */
+};
+
+static void build_subgraph(struct csa *csa)
+{ /* build induced subgraph */
+ glp_prob *P = csa->P;
+ int n = P->n;
+ CFG *G = csa->G;
+ int *ind = csa->ind;
+ int *pos = G->pos;
+ int *neg = G->neg;
+ int nv = G->nv;
+ int *ref = G->ref;
+ int *vtoi = csa->vtoi;
+ int *itov = csa->itov;
+ double *wgt = csa->wgt;
+ int j, k, v, w, nn, len;
+ double z, sum;
+ /* initially induced subgraph is empty */
+ nn = 0;
+ /* walk thru vertices of original conflict graph */
+ for (v = 1; v <= nv; v++)
+ { /* determine value of binary variable z[j] that corresponds to
+ * vertex v */
+ j = ref[v];
+ xassert(1 <= j && j <= n);
+ if (pos[j] == v)
+ { /* z[j] = x[j], where x[j] is original variable */
+ z = P->col[j]->prim;
+ }
+ else if (neg[j] == v)
+ { /* z[j] = 1 - x[j], where x[j] is original variable */
+ z = 1.0 - P->col[j]->prim;
+ }
+ else
+ xassert(v != v);
+ /* if z[j] is close to zero, do not include v in the induced
+ * subgraph */
+ if (z < 0.001)
+ { vtoi[v] = 0;
+ continue;
+ }
+ /* calculate cumulative weight of vertex v */
+ sum = z;
+ /* walk thru all vertices adjacent to v */
+ len = cfg_get_adjacent(G, v, ind);
+ for (k = 1; k <= len; k++)
+ { /* there is an edge (v,w) in the conflict graph */
+ w = ind[k];
+ xassert(w != v);
+ /* add value of z[j] that corresponds to vertex w */
+ j = ref[w];
+ xassert(1 <= j && j <= n);
+ if (pos[j] == w)
+ sum += P->col[j]->prim;
+ else if (neg[j] == w)
+ sum += 1.0 - P->col[j]->prim;
+ else
+ xassert(w != w);
+ }
+ /* cumulative weight of vertex v is an upper bound of weight
+ * of any clique containing v; so if it not greater than 1, do
+ * not include v in the induced subgraph */
+ if (sum < 1.010)
+ { vtoi[v] = 0;
+ continue;
+ }
+ /* include vertex v in the induced subgraph */
+ nn++;
+ vtoi[v] = nn;
+ itov[nn] = v;
+ wgt[nn] = z;
+ }
+ /* induced subgraph has been built */
+ csa->nn = nn;
+ return;
+}
+
+static int sub_adjacent(struct csa *csa, int i, int adj[])
+{ /* retrieve vertices of induced subgraph adjacent to specified
+ * vertex */
+ CFG *G = csa->G;
+ int nv = G->nv;
+ int *ind = csa->ind;
+ int nn = csa->nn;
+ int *vtoi = csa->vtoi;
+ int *itov = csa->itov;
+ int j, k, v, w, len, len1;
+ /* determine original vertex v corresponding to vertex i */
+ xassert(1 <= i && i <= nn);
+ v = itov[i];
+ /* retrieve vertices adjacent to vertex v in original graph */
+ len1 = cfg_get_adjacent(G, v, ind);
+ /* keep only adjacent vertices which are in induced subgraph and
+ * change their numbers appropriately */
+ len = 0;
+ for (k = 1; k <= len1; k++)
+ { /* there exists edge (v, w) in original graph */
+ w = ind[k];
+ xassert(1 <= w && w <= nv && w != v);
+ j = vtoi[w];
+ if (j != 0)
+ { /* vertex w is vertex j in induced subgraph */
+ xassert(1 <= j && j <= nn && j != i);
+ adj[++len] = j;
+ }
+ }
+ return len;
+}
+
+static int find_clique(struct csa *csa, int c_ind[])
+{ /* find maximum weight clique in induced subgraph with exact
+ * Ostergard's algorithm */
+ int nn = csa->nn;
+ double *wgt = csa->wgt;
+ int i, j, k, p, q, t, ne, nb, len, *iwt, *ind;
+ unsigned char *a;
+ xassert(nn >= 2);
+ /* allocate working array */
+ ind = talloc(1+nn, int);
+ /* calculate the number of elements in lower triangle (without
+ * diagonal) of adjacency matrix of induced subgraph */
+ ne = (nn * (nn - 1)) / 2;
+ /* calculate the number of bytes needed to store lower triangle
+ * of adjacency matrix */
+ nb = (ne + (CHAR_BIT - 1)) / CHAR_BIT;
+ /* allocate lower triangle of adjacency matrix */
+ a = talloc(nb, unsigned char);
+ /* fill lower triangle of adjacency matrix */
+ memset(a, 0, nb);
+ for (p = 1; p <= nn; p++)
+ { /* retrieve vertices adjacent to vertex p */
+ len = sub_adjacent(csa, p, ind);
+ for (k = 1; k <= len; k++)
+ { /* there exists edge (p, q) in induced subgraph */
+ q = ind[k];
+ xassert(1 <= q && q <= nn && q != p);
+ /* determine row and column indices of this edge in lower
+ * triangle of adjacency matrix */
+ if (p > q)
+ i = p, j = q;
+ else /* p < q */
+ i = q, j = p;
+ /* set bit a[i,j] to 1, i > j */
+ t = ((i - 1) * (i - 2)) / 2 + (j - 1);
+ a[t / CHAR_BIT] |=
+ (unsigned char)(1 << ((CHAR_BIT - 1) - t % CHAR_BIT));
+ }
+ }
+ /* scale vertex weights by 1000 and convert them to integers as
+ * required by Ostergard's algorithm */
+ iwt = ind;
+ for (i = 1; i <= nn; i++)
+ { /* it is assumed that 0 <= wgt[i] <= 1 */
+ t = (int)(1000.0 * wgt[i] + 0.5);
+ if (t < 0)
+ t = 0;
+ else if (t > 1000)
+ t = 1000;
+ iwt[i] = t;
+ }
+ /* find maximum weight clique */
+ len = wclique(nn, iwt, a, c_ind);
+ /* free working arrays */
+ tfree(ind);
+ tfree(a);
+ /* return clique size to calling routine */
+ return len;
+}
+
+static int func(void *info, int i, int ind[])
+{ /* auxiliary routine used by routine find_clique1 */
+ struct csa *csa = info;
+ xassert(1 <= i && i <= csa->nn);
+ return sub_adjacent(csa, i, ind);
+}
+
+static int find_clique1(struct csa *csa, int c_ind[])
+{ /* find maximum weight clique in induced subgraph with greedy
+ * heuristic */
+ int nn = csa->nn;
+ double *wgt = csa->wgt;
+ int len;
+ xassert(nn >= 2);
+ len = wclique1(nn, wgt, func, csa, c_ind);
+ /* return clique size to calling routine */
+ return len;
+}
+
+int cfg_find_clique(void *P, CFG *G, int ind[], double *sum_)
+{ int nv = G->nv;
+ struct csa csa;
+ int i, k, len;
+ double sum;
+ /* initialize common storage area */
+ csa.P = P;
+ csa.G = G;
+ csa.ind = talloc(1+nv, int);
+ csa.nn = -1;
+ csa.vtoi = talloc(1+nv, int);
+ csa.itov = talloc(1+nv, int);
+ csa.wgt = talloc(1+nv, double);
+ /* build induced subgraph */
+ build_subgraph(&csa);
+#ifdef GLP_DEBUG
+ xprintf("nn = %d\n", csa.nn);
+#endif
+ /* if subgraph has less than two vertices, do nothing */
+ if (csa.nn < 2)
+ { len = 0;
+ sum = 0.0;
+ goto skip;
+ }
+ /* find maximum weight clique in induced subgraph */
+#if 1 /* FIXME */
+ if (csa.nn <= 50)
+#endif
+ { /* induced subgraph is small; use exact algorithm */
+ len = find_clique(&csa, ind);
+ }
+ else
+ { /* induced subgraph is large; use greedy heuristic */
+ len = find_clique1(&csa, ind);
+ }
+ /* do not report clique, if it has less than two vertices */
+ if (len < 2)
+ { len = 0;
+ sum = 0.0;
+ goto skip;
+ }
+ /* convert indices of clique vertices from induced subgraph to
+ * original conflict graph and compute clique weight */
+ sum = 0.0;
+ for (k = 1; k <= len; k++)
+ { i = ind[k];
+ xassert(1 <= i && i <= csa.nn);
+ sum += csa.wgt[i];
+ ind[k] = csa.itov[i];
+ }
+skip: /* free working arrays */
+ tfree(csa.ind);
+ tfree(csa.vtoi);
+ tfree(csa.itov);
+ tfree(csa.wgt);
+ /* return to calling routine */
+ *sum_ = sum;
+ return len;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/cfg2.c b/test/monniaux/glpk-4.65/src/intopt/cfg2.c
new file mode 100644
index 00000000..85c0705e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/cfg2.c
@@ -0,0 +1,91 @@
+/* cfg2.c (conflict graph) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "cfg.h"
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_cfg_init - create and initialize conflict graph
+*
+* SYNOPSIS
+*
+* glp_cfg *glp_cfg_init(glp_prob *P);
+*
+* DESCRIPTION
+*
+* This routine creates and initializes the conflict graph for the
+* specified problem object.
+*
+* RETURNS
+*
+* The routine returns a pointer to the conflict graph descriptor.
+* However, if the conflict graph is empty (no conflicts have been
+* found), the routine returns NULL. */
+
+glp_cfg *glp_cfg_init(glp_prob *P)
+{ glp_cfg *G;
+ int j, n1, n2;
+ xprintf("Constructing conflict graph...\n");
+ G = cfg_build_graph(P);
+ n1 = n2 = 0;
+ for (j = 1; j <= P->n; j++)
+ { if (G->pos[j])
+ n1 ++;
+ if (G->neg[j])
+ n2++;
+ }
+ if (n1 == 0 && n2 == 0)
+ { xprintf("No conflicts found\n");
+ cfg_delete_graph(G);
+ G = NULL;
+ }
+ else
+ xprintf("Conflict graph has %d + %d = %d vertices\n",
+ n1, n2, G->nv);
+ return G;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_cfg_free - delete conflict graph descriptor
+*
+* SYNOPSIS
+*
+* void glp_cfg_free(glp_cfg *G);
+*
+* DESCRIPTION
+*
+* This routine deletes the conflict graph descriptor and frees all the
+* memory allocated to it. */
+
+void glp_cfg_free(glp_cfg *G)
+{ xassert(G != NULL);
+ cfg_delete_graph(G);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/clqcut.c b/test/monniaux/glpk-4.65/src/intopt/clqcut.c
new file mode 100644
index 00000000..d3db5b39
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/clqcut.c
@@ -0,0 +1,134 @@
+/* clqcut.c (clique cut generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "cfg.h"
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_clq_cut - generate clique cut from conflict graph
+*
+* SYNOPSIS
+*
+* int glp_clq_cut(glp_prob *P, glp_cfg *G, int ind[], double val[]);
+*
+* DESCRIPTION
+*
+* This routine attempts to generate a clique cut.
+*
+* The cut generated by the routine is the following inequality:
+*
+* sum a[j] * x[j] <= b,
+*
+* which is expected to be violated at the current basic solution.
+*
+* If the cut has been successfully generated, the routine stores its
+* non-zero coefficients a[j] and corresponding column indices j in the
+* array locations val[1], ..., val[len] and ind[1], ..., ind[len],
+* where 1 <= len <= n is the number of non-zero coefficients. The
+* right-hand side value b is stored in val[0], and ind[0] is set to 0.
+*
+* RETURNS
+*
+* If the cut has been successfully generated, the routine returns
+* len, the number of non-zero coefficients in the cut, 1 <= len <= n.
+* Otherwise, the routine returns a non-positive value. */
+
+int glp_clq_cut(glp_prob *P, glp_cfg *G, int ind[], double val[])
+{ int n = P->n;
+ int *pos = G->pos;
+ int *neg = G->neg;
+ int nv = G->nv;
+ int *ref = G->ref;
+ int j, k, v, len;
+ double rhs, sum;
+ xassert(G->n == n);
+ /* find maximum weight clique in conflict graph */
+ len = cfg_find_clique(P, G, ind, &sum);
+#ifdef GLP_DEBUG
+ xprintf("len = %d; sum = %g\n", len, sum);
+ cfg_check_clique(G, len, ind);
+#endif
+ /* check if clique inequality is violated */
+ if (sum < 1.07)
+ return 0;
+ /* expand clique to maximal one */
+ len = cfg_expand_clique(G, len, ind);
+#ifdef GLP_DEBUG
+ xprintf("maximal clique size = %d\n", len);
+ cfg_check_clique(G, len, ind);
+#endif
+ /* construct clique cut (fixed binary variables are removed, so
+ this cut is only locally valid) */
+ rhs = 1.0;
+ for (j = 1; j <= n; j++)
+ val[j] = 0.0;
+ for (k = 1; k <= len; k++)
+ { /* v is clique vertex */
+ v = ind[k];
+ xassert(1 <= v && v <= nv);
+ /* j is number of corresponding binary variable */
+ j = ref[v];
+ xassert(1 <= j && j <= n);
+ if (pos[j] == v)
+ { /* v corresponds to x[j] */
+ if (P->col[j]->type == GLP_FX)
+ { /* x[j] is fixed */
+ rhs -= P->col[j]->prim;
+ }
+ else
+ { /* x[j] is not fixed */
+ val[j] += 1.0;
+ }
+ }
+ else if (neg[j] == v)
+ { /* v corresponds to (1 - x[j]) */
+ if (P->col[j]->type == GLP_FX)
+ { /* x[j] is fixed */
+ rhs -= (1.0 - P->col[j]->prim);
+ }
+ else
+ { /* x[j] is not fixed */
+ val[j] -= 1.0;
+ rhs -= 1.0;
+ }
+ }
+ else
+ xassert(v != v);
+ }
+ /* convert cut inequality to sparse format */
+ len = 0;
+ for (j = 1; j <= n; j++)
+ { if (val[j] != 0.0)
+ { len++;
+ ind[len] = j;
+ val[len] = val[j];
+ }
+ }
+ ind[0] = 0, val[0] = rhs;
+ return len;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/covgen.c b/test/monniaux/glpk-4.65/src/intopt/covgen.c
new file mode 100644
index 00000000..427c3aa8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/covgen.c
@@ -0,0 +1,885 @@
+/* covgen.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "fvs.h"
+#include "ks.h"
+#include "prob.h"
+
+struct glp_cov
+{ /* cover cut generator working area */
+ int n;
+ /* number of columns (variables) */
+ glp_prob *set;
+ /* set of globally valid 0-1 knapsack inequalities chosen from
+ * the root problem; each inequality is either original row or
+ * its relaxation (surrogate 0-1 knapsack) which is constructed
+ * by substitution of lower/upper single/variable bounds for
+ * continuous and general integer (non-binary) variables */
+};
+
+struct bnd
+{ /* simple or variable bound */
+ /* if z = 0, it is a simple bound x >= or <= b; if b = -DBL_MAX
+ * (b = +DBL_MAX), x has no lower (upper) bound; otherwise, if
+ * z != 0, it is a variable bound x >= or <= a * z + b */
+ int z;
+ /* number of binary variable or 0 */
+ double a, b;
+ /* bound parameters */
+};
+
+struct csa
+{ /* common storage area */
+ glp_prob *P;
+ /* original (root) MIP */
+ struct bnd *l; /* struct bnd l[1+P->n]; */
+ /* lower simple/variable bounds of variables */
+ struct bnd *u; /* struct bnd u[1+P->n]; */
+ /* upper simple/variable bounds of variables */
+ glp_prob *set;
+ /* see struct glp_cov above */
+};
+
+/***********************************************************************
+* init_bounds - initialize bounds of variables with simple bounds
+*
+* This routine initializes lower and upper bounds of all variables
+* with simple bounds specified in the original mip. */
+
+static void init_bounds(struct csa *csa)
+{ glp_prob *P = csa->P;
+ struct bnd *l = csa->l, *u = csa->u;
+ int j;
+ for (j = 1; j <= P->n; j++)
+ { l[j].z = u[j].z = 0;
+ l[j].a = u[j].a = 0;
+ l[j].b = glp_get_col_lb(P, j);
+ u[j].b = glp_get_col_ub(P, j);
+ }
+ return;
+}
+
+/***********************************************************************
+* check_vb - check variable bound
+*
+* This routine checks if the specified i-th row has the form
+*
+* a1 * x + a2 * z >= or <= rhs, (1)
+*
+* where x is a non-fixed continuous or general integer variable, and
+* z is a binary variable. If it is, the routine converts the row to
+* the following variable lower/upper bound (VLB/VUB) of x:
+*
+* x >= or <= a * z + b, (2)
+*
+* where a = - a2 / a1, b = rhs / a1. Note that the inequality type is
+* changed to opposite one when a1 < 0.
+*
+* If the row is identified as a variable bound, the routine returns
+* GLP_LO for VLB or GLP_UP for VUB and provides the reference numbers
+* of variables x and z and values of a and b. Otherwise, the routine
+* returns zero. */
+
+static int check_vb(struct csa *csa, int i, int *x, int *z, double *a,
+ double *b)
+{ glp_prob *P = csa->P;
+ GLPROW *row;
+ GLPAIJ *a1, *a2;
+ int type;
+ double rhs;
+ xassert(1 <= i && i <= P->m);
+ row = P->row[i];
+ /* check row type */
+ switch (row->type)
+ { case GLP_LO:
+ case GLP_UP:
+ break;
+ default:
+ return 0;
+ }
+ /* take first term of the row */
+ a1 = row->ptr;
+ if (a1 == NULL)
+ return 0;
+ /* take second term of the row */
+ a2 = a1->r_next;
+ if (a2 == NULL)
+ return 0;
+ /* there should be exactly two terms in the row */
+ if (a2->r_next != NULL)
+ return 0;
+ /* if first term is a binary variable, swap the terms */
+ if (glp_get_col_kind(P, a1->col->j) == GLP_BV)
+ { GLPAIJ *a;
+ a = a1, a1 = a2, a2 = a;
+ }
+ /* now first term should be a non-fixed continuous or general
+ * integer variable */
+ if (a1->col->type == GLP_FX)
+ return 0;
+ if (glp_get_col_kind(P, a1->col->j) == GLP_BV)
+ return 0;
+ /* and second term should be a binary variable */
+ if (glp_get_col_kind(P, a2->col->j) != GLP_BV)
+ return 0;
+ /* VLB/VUB row has been identified */
+ switch (row->type)
+ { case GLP_LO:
+ type = a1->val > 0 ? GLP_LO : GLP_UP;
+ rhs = row->lb;
+ break;
+ case GLP_UP:
+ type = a1->val > 0 ? GLP_UP : GLP_LO;
+ rhs = row->ub;
+ break;
+ default:
+ xassert(type != type);
+ }
+ *x = a1->col->j;
+ *z = a2->col->j;
+ *a = - a2->val / a1->val;
+ *b = rhs / a1->val;
+ return type;
+}
+
+/***********************************************************************
+* set_vb - set variable bound
+*
+* This routine sets lower or upper variable bound specified as
+*
+* x >= a * z + b (type = GLP_LO)
+*
+* x <= a * z + b (type = GLP_UP) */
+
+static void set_vb(struct csa *csa, int type, int x, int z, double a,
+ double b)
+{ glp_prob *P = csa->P;
+ struct bnd *l = csa->l, *u = csa->u;
+ xassert(glp_get_col_type(P, x) != GLP_FX);
+ xassert(glp_get_col_kind(P, x) != GLP_BV);
+ xassert(glp_get_col_kind(P, z) == GLP_BV);
+ xassert(a != 0);
+ switch (type)
+ { case GLP_LO:
+ /* FIXME: check existing simple lower bound? */
+ l[x].z = z, l[x].a = a, l[x].b = b;
+ break;
+ case GLP_UP:
+ /* FIXME: check existing simple upper bound? */
+ u[x].z = z, u[x].a = a, u[x].b = b;
+ break;
+ default:
+ xassert(type != type);
+ }
+ return;
+}
+
+/***********************************************************************
+* obtain_vbs - obtain and set variable bounds
+*
+* This routine walks thru all rows of the original mip, identifies
+* rows specifying variable lower/upper bounds, and sets these bounds
+* for corresponding (non-binary) variables. */
+
+static void obtain_vbs(struct csa *csa)
+{ glp_prob *P = csa->P;
+ int i, x, z, type, save;
+ double a, b;
+ for (i = 1; i <= P->m; i++)
+ { switch (P->row[i]->type)
+ { case GLP_FR:
+ break;
+ case GLP_LO:
+ case GLP_UP:
+ type = check_vb(csa, i, &x, &z, &a, &b);
+ if (type)
+ set_vb(csa, type, x, z, a, b);
+ break;
+ case GLP_DB:
+ case GLP_FX:
+ /* double-side inequality l <= ... <= u and equality
+ * ... = l = u are considered as two single inequalities
+ * ... >= l and ... <= u */
+ save = P->row[i]->type;
+ P->row[i]->type = GLP_LO;
+ type = check_vb(csa, i, &x, &z, &a, &b);
+ if (type)
+ set_vb(csa, type, x, z, a, b);
+ P->row[i]->type = GLP_UP;
+ type = check_vb(csa, i, &x, &z, &a, &b);
+ if (type)
+ set_vb(csa, type, x, z, a, b);
+ P->row[i]->type = save;
+ break;
+ default:
+ xassert(P != P);
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* add_term - add term to sparse vector
+*
+* This routine computes the following linear combination:
+*
+* v := v + a * e[j],
+*
+* where v is a sparse vector in full storage format, a is a non-zero
+* scalar, e[j] is j-th column of unity matrix. */
+
+static void add_term(FVS *v, int j, double a)
+{ xassert(1 <= j && j <= v->n);
+ xassert(a != 0);
+ if (v->vec[j] == 0)
+ { /* create j-th component */
+ v->nnz++;
+ xassert(v->nnz <= v->n);
+ v->ind[v->nnz] = j;
+ }
+ /* perform addition */
+ v->vec[j] += a;
+ if (fabs(v->vec[j]) < 1e-9 * (1 + fabs(a)))
+ { /* remove j-th component */
+ v->vec[j] = DBL_MIN;
+ }
+ return;
+}
+
+/***********************************************************************
+* build_ks - build "0-1 knapsack" inequality
+*
+* Given an inequality of "not greater" type:
+*
+* sum{j in 1..n} a[j]*x[j] <= b, (1)
+*
+* this routine attempts to transform it to equivalent or relaxed "0-1
+* knapsack" inequality that contains only binary variables.
+*
+* If x[j] is a binary variable, the term a[j]*x[j] is not changed.
+* Otherwise, if x[j] is a continuous or integer non-binary variable,
+* it is replaced by its lower (if a[j] > 0) or upper (if a[j] < 0)
+* single or variable bound. In the latter case, if x[j] is a non-fixed
+* variable, this results in a relaxation of original inequality known
+* as "surrogate knapsack". Thus, if the specified inequality is valid
+* for the original mip, the resulting inequality is also valid.
+*
+* Note that in both source and resulting inequalities coefficients
+* a[j] can have any sign.
+*
+* On entry to the routine the source inequality is specified by the
+* parameters n, ind (contains original numbers of x[j]), a, and b. The
+* parameter v is a working sparse vector whose components are assumed
+* to be zero.
+*
+* On exit the routine stores the resulting "0-1 knapsack" inequality
+* in the parameters ind, a, and b, and returns n which is the number
+* of terms in the resulting inequality. Zero content of the vector v
+* is restored before exit.
+*
+* If the resulting inequality cannot be constructed due to missing
+* lower/upper bounds of some variable, the routine returns a negative
+* value. */
+
+static int build_ks(struct csa *csa, int n, int ind[], double a[],
+ double *b, FVS *v)
+{ glp_prob *P = csa->P;
+ struct bnd *l = csa->l, *u = csa->u;
+ int j, k;
+ /* check that v = 0 */
+#ifdef GLP_DEBUG
+ fvs_check_vec(v);
+#endif
+ xassert(v->nnz == 0);
+ /* walk thru terms of original inequality */
+ for (j = 1; j <= n; j++)
+ { /* process term a[j]*x[j] */
+ k = ind[j]; /* original number of x[j] in mip */
+ if (glp_get_col_kind(P, k) == GLP_BV)
+ { /* x[j] is a binary variable */
+ /* include its term into resulting inequality */
+ add_term(v, k, a[j]);
+ }
+ else if (a[j] > 0)
+ { /* substitute x[j] by its lower bound */
+ if (l[k].b == -DBL_MAX)
+ { /* x[j] has no lower bound */
+ n = -1;
+ goto skip;
+ }
+ else if (l[k].z == 0)
+ { /* x[j] has simple lower bound */
+ *b -= a[j] * l[k].b;
+ }
+ else
+ { /* x[j] has variable lower bound (a * z + b) */
+ add_term(v, l[k].z, a[j] * l[k].a);
+ *b -= a[j] * l[k].b;
+ }
+ }
+ else /* a[j] < 0 */
+ { /* substitute x[j] by its upper bound */
+ if (u[k].b == +DBL_MAX)
+ { /* x[j] has no upper bound */
+ n = -1;
+ goto skip;
+ }
+ else if (u[k].z == 0)
+ { /* x[j] has simple upper bound */
+ *b -= a[j] * u[k].b;
+ }
+ else
+ { /* x[j] has variable upper bound (a * z + b) */
+ add_term(v, u[k].z, a[j] * u[k].a);
+ *b -= a[j] * u[k].b;
+ }
+ }
+ }
+ /* replace tiny coefficients by exact zeros (see add_term) */
+ fvs_adjust_vec(v, 2 * DBL_MIN);
+ /* copy terms of resulting inequality */
+ xassert(v->nnz <= n);
+ n = v->nnz;
+ for (j = 1; j <= n; j++)
+ { ind[j] = v->ind[j];
+ a[j] = v->vec[ind[j]];
+ }
+skip: /* restore zero content of v */
+ fvs_clear_vec(v);
+ return n;
+}
+
+/***********************************************************************
+* can_be_active - check if inequality can be active
+*
+* This routine checks if the specified "0-1 knapsack" inequality
+*
+* sum{j in 1..n} a[j]*x[j] <= b
+*
+* can be active. If so, the routine returns true, otherwise false. */
+
+static int can_be_active(int n, const double a[], double b)
+{ int j;
+ double s;
+ s = 0;
+ for (j = 1; j <= n; j++)
+ { if (a[j] > 0)
+ s += a[j];
+ }
+ return s > b + .001 * (1 + fabs(b));
+}
+
+/***********************************************************************
+* is_sos_ineq - check if inequality is packing (SOS) constraint
+*
+* This routine checks if the specified "0-1 knapsack" inequality
+*
+* sum{j in 1..n} a[j]*x[j] <= b (1)
+*
+* is equivalent to packing inequality (Padberg calls such inequalities
+* special ordered set or SOS constraints)
+*
+* sum{j in J'} x[j] - sum{j in J"} x[j] <= 1 - |J"|. (2)
+*
+* If so, the routine returns true, otherwise false.
+*
+* Note that if X is a set of feasible binary points satisfying to (2),
+* its convex hull conv(X) equals to the set of feasible points of LP
+* relaxation of (2), which is a n-dimensional simplex, so inequalities
+* (2) are useless for generating cover cuts (due to unimodularity).
+*
+* ALGORITHM
+*
+* First, we make all a[j] positive by complementing x[j] = 1 - x'[j]
+* in (1). This is performed implicitly (i.e. actually the array a is
+* not changed), but b is replaced by b - sum{j : a[j] < 0}.
+*
+* Then we find two smallest coefficients a[p] = min{j in 1..n} a[j]
+* and a[q] = min{j in 1..n : j != p} a[j]. It is obvious that if
+* a[p] + a[q] > b, then a[i] + a[j] > b for all i != j, from which it
+* follows that x[i] + x[j] <= 1 for all i != j. But the latter means
+* that the original inequality (with all a[j] > 0) is equivalent to
+* packing inequality
+*
+* sum{j in 1..n} x[j] <= 1. (3)
+*
+* Returning to original (uncomplemented) variables x'[j] = 1 - x[j]
+* we have that the original inequality is equivalent to (2), where
+* J' = {j : a[j] > 0} and J" = {j : a[j] < 0}. */
+
+static int is_sos_ineq(int n, const double a[], double b)
+{ int j, p, q;
+ xassert(n >= 2);
+ /* compute b := b - sum{j : a[j] < 0} */
+ for (j = 1; j <= n; j++)
+ { if (a[j] < 0)
+ b -= a[j];
+ }
+ /* find a[p] = min{j in 1..n} a[j] */
+ p = 1;
+ for (j = 2; j <= n; j++)
+ { if (fabs(a[p]) > fabs(a[j]))
+ p = j;
+ }
+ /* find a[q] = min{j in 1..n : j != p} a[j] */
+ q = 0;
+ for (j = 1; j <= n; j++)
+ { if (j != p)
+ { if (q == 0 || fabs(a[q]) > fabs(a[j]))
+ q = j;
+ }
+ }
+ xassert(q != 0);
+ /* check condition a[p] + a[q] > b */
+ return fabs(a[p]) + fabs(a[q]) > b + .001 * (1 + fabs(b));
+}
+
+/***********************************************************************
+* process_ineq - basic inequality processing
+*
+* This routine performs basic processing of an inequality of "not
+* greater" type
+*
+* sum{j in 1..n} a[j]*x[j] <= b
+*
+* specified by the parameters, n, ind, a, and b.
+*
+* If the inequality can be transformed to "0-1 knapsack" ineqiality
+* suitable for generating cover cuts, the routine adds it to the set
+* of "0-1 knapsack" inequalities.
+*
+* Note that the arrays ind and a are not saved on exit. */
+
+static void process_ineq(struct csa *csa, int n, int ind[], double a[],
+ double b, FVS *v)
+{ int i;
+ /* attempt to transform the specified inequality to equivalent or
+ * relaxed "0-1 knapsack" inequality */
+ n = build_ks(csa, n, ind, a, &b, v);
+ if (n <= 1)
+ { /* uninteresting inequality (in principle, such inequalities
+ * should be removed by the preprocessor) */
+ goto done;
+ }
+ if (!can_be_active(n, a, b))
+ { /* inequality is redundant (i.e. cannot be active) */
+ goto done;
+ }
+ if (is_sos_ineq(n, a, b))
+ { /* packing (SOS) inequality is useless for generating cover
+ * cuts; currently such inequalities are just ignored */
+ goto done;
+ }
+ /* add resulting "0-1 knapsack" inequality to the set */
+ i = glp_add_rows(csa->set, 1);
+ glp_set_mat_row(csa->set, i, n, ind, a);
+ glp_set_row_bnds(csa->set, i, GLP_UP, b, b);
+done: return;
+}
+
+/**********************************************************************/
+
+glp_cov *glp_cov_init(glp_prob *P)
+{ /* create and initialize cover cut generator */
+ glp_cov *cov;
+ struct csa csa;
+ int i, k, len, *ind;
+ double rhs, *val;
+ FVS fvs;
+ csa.P = P;
+ csa.l = talloc(1+P->n, struct bnd);
+ csa.u = talloc(1+P->n, struct bnd);
+ csa.set = glp_create_prob();
+ glp_add_cols(csa.set, P->n);
+ /* initialize bounds of variables with simple bounds */
+ init_bounds(&csa);
+ /* obtain and set variable bounds */
+ obtain_vbs(&csa);
+ /* allocate working arrays */
+ ind = talloc(1+P->n, int);
+ val = talloc(1+P->n, double);
+ fvs_alloc_vec(&fvs, P->n);
+ /* process all rows of the root mip */
+ for (i = 1; i <= P->m; i++)
+ { switch (P->row[i]->type)
+ { case GLP_FR:
+ break;
+ case GLP_LO:
+ /* obtain row of ">=" type */
+ len = glp_get_mat_row(P, i, ind, val);
+ rhs = P->row[i]->lb;
+ /* transforms it to row of "<=" type */
+ for (k = 1; k <= len; k++)
+ val[k] = - val[k];
+ rhs = - rhs;
+ /* process the row */
+ process_ineq(&csa, len, ind, val, rhs, &fvs);
+ break;
+ case GLP_UP:
+ /* obtain row of "<=" type */
+ len = glp_get_mat_row(P, i, ind, val);
+ rhs = P->row[i]->ub;
+ /* and process it */
+ process_ineq(&csa, len, ind, val, rhs, &fvs);
+ break;
+ case GLP_DB:
+ case GLP_FX:
+ /* double-sided inequalitiy and equality constraints are
+ * processed as two separate inequalities */
+ /* obtain row as if it were of ">=" type */
+ len = glp_get_mat_row(P, i, ind, val);
+ rhs = P->row[i]->lb;
+ /* transforms it to row of "<=" type */
+ for (k = 1; k <= len; k++)
+ val[k] = - val[k];
+ rhs = - rhs;
+ /* and process it */
+ process_ineq(&csa, len, ind, val, rhs, &fvs);
+ /* obtain the same row as if it were of "<=" type */
+ len = glp_get_mat_row(P, i, ind, val);
+ rhs = P->row[i]->ub;
+ /* and process it */
+ process_ineq(&csa, len, ind, val, rhs, &fvs);
+ break;
+ default:
+ xassert(P != P);
+ }
+ }
+ /* free working arrays */
+ tfree(ind);
+ tfree(val);
+ fvs_check_vec(&fvs);
+ fvs_free_vec(&fvs);
+ /* the set of "0-1 knapsack" inequalities has been built */
+ if (csa.set->m == 0)
+ { /* the set is empty */
+ xprintf("No 0-1 knapsack inequalities detected\n");
+ cov = NULL;
+ glp_delete_prob(csa.set);
+ }
+ else
+ { /* create the cover cut generator working area */
+ xprintf("Number of 0-1 knapsack inequalities = %d\n",
+ csa.set->m);
+ cov = talloc(1, glp_cov);
+ cov->n = P->n;
+ cov->set = csa.set;
+#if 0
+ glp_write_lp(cov->set, 0, "set.lp");
+#endif
+ }
+ tfree(csa.l);
+ tfree(csa.u);
+ return cov;
+}
+
+/***********************************************************************
+* solve_ks - solve 0-1 knapsack problem
+*
+* This routine finds (sub)optimal solution to 0-1 knapsack problem:
+*
+* maximize z = sum{j in 1..n} c[j]x[j] (1)
+*
+* s.t. sum{j in 1..n} a[j]x[j] <= b (2)
+*
+* x[j] in {0, 1} for all j in 1..n (3)
+*
+* It is assumed that the instance is non-normalized, i.e. parameters
+* a, b, and c may have any sign.
+*
+* On exit the routine stores the (sub)optimal point found in locations
+* x[1], ..., x[n] and returns the optimal objective value. However, if
+* the instance is infeasible, the routine returns INT_MIN. */
+
+static int solve_ks(int n, const int a[], int b, const int c[],
+ char x[])
+{ int z;
+ /* surprisingly, even for some small instances (n = 50-100)
+ * MT1 routine takes too much time, so it is used only for tiny
+ * instances */
+ if (n <= 16)
+#if 0
+ z = ks_enum(n, a, b, c, x);
+#else
+ z = ks_mt1(n, a, b, c, x);
+#endif
+ else
+ z = ks_greedy(n, a, b, c, x);
+ return z;
+}
+
+/***********************************************************************
+* simple_cover - find simple cover cut
+*
+* Given a 0-1 knapsack inequality (which may be globally as well as
+* locally valid)
+*
+* sum{j in 1..n} a[j]x[j] <= b, (1)
+*
+* where all x[j] are binary variables and all a[j] are positive, and
+* a fractional point x~{j in 1..n}, which is feasible to LP relaxation
+* of (1), this routine attempts to find a simple cover inequality
+*
+* sum{j in C} (1 - x[j]) >= 1, (2)
+*
+* which is valid for (1) and violated at x~.
+*
+* Actually, the routine finds a cover C, i.e. a subset of {1, ..., n}
+* such that
+*
+* sum{j in C} a[j] > b, (3)
+*
+* and which minimizes the left-hand side of (2) at x~
+*
+* zeta = sum{j in C} (1 - x~[j]). (4)
+*
+* On exit the routine stores the characteritic vector z{j in 1..n}
+* of the cover found (i.e. z[j] = 1 means j in C, and z[j] = 0 means
+* j not in C), and returns corresponding minimal value of zeta (4).
+* However, if no cover is found, the routine returns DBL_MAX.
+*
+* ALGORITHM
+*
+* The separation problem (3)-(4) is converted to 0-1 knapsack problem
+* as follows.
+*
+* First, note that the constraint (3) is equivalent to
+*
+* sum{j in 1..n} a[j]z[j] >= b + eps, (5)
+*
+* where eps > 0 is a sufficiently small number (in case of integral
+* a and b we may take eps = 1). Multiplying both sides of (5) by (-1)
+* gives
+*
+* sum{j in 1..n} (-a[j])z[j] <= - b - eps. (6)
+*
+* To make all coefficients in (6) positive, z[j] is complemented by
+* substitution z[j] = 1 - z'[j] that finally gives
+*
+* sum{j in 1..n} a[j]z'[j] <= sum{j in 1..n} a[j] - b - eps. (7)
+*
+* Minimization of zeta (4) is equivalent to maximization of
+*
+* -zeta = sum{j in 1..n} (x~[j] - 1)z[j]. (8)
+*
+* Substitution z[j] = 1 - z'[j] gives
+*
+* -zeta = sum{j in 1..n} (1 - x~[j])z'[j] - zeta0, (9)
+*
+* where zeta0 = sum{j in 1..n} (1 - x~[j]) is a constant term.
+*
+* Thus, the 0-1 knapsack problem to be solved is the following:
+*
+* maximize
+*
+* -zeta = sum{j in 1..n} (1 - x~[j])z'[j] - zeta0 (10)
+*
+* subject to
+*
+* sum{j in 1..n} a[j]z'[j] <= sum{j in 1..n} a[j] - b - eps (11)
+*
+* z'[j] in {0,1} for all j = 1,...,n (12)
+*
+* (The constant term zeta0 doesn't affect the solution, so it can be
+* dropped.) */
+
+static double simple_cover(int n, const double a[], double b, const
+ double x[], char z[])
+{ int j, *aa, bb, *cc;
+ double max_aj, min_aj, s, eps;
+ xassert(n >= 3);
+ /* allocate working arrays */
+ aa = talloc(1+n, int);
+ cc = talloc(1+n, int);
+ /* compute max{j in 1..n} a[j] and min{j in 1..n} a[j] */
+ max_aj = 0, min_aj = DBL_MAX;
+ for (j = 1; j <= n; j++)
+ { xassert(a[j] > 0);
+ if (max_aj < a[j])
+ max_aj = a[j];
+ if (min_aj > a[j])
+ min_aj = a[j];
+ }
+ /* scale and round constraint parameters to make them integral;
+ * note that we make the resulting inequality stronger than (11),
+ * so a[j]'s are rounded up while rhs is rounded down */
+ s = 0;
+ for (j = 1; j <= n; j++)
+ { s += a[j];
+ aa[j] = ceil(a[j] / max_aj * 1000);
+ }
+ bb = floor((s - b) / max_aj * 1000) - 1;
+ /* scale and round obj. coefficients to make them integral;
+ * again we make the objective function stronger than (10), so
+ * the coefficients are rounded down */
+ for (j = 1; j <= n; j++)
+ { xassert(0 <= x[j] && x[j] <= 1);
+ cc[j] = floor((1 - x[j]) * 1000);
+ }
+ /* solve separation problem */
+ if (solve_ks(n, aa, bb, cc, z) == INT_MIN)
+ { /* no cover exists */
+ s = DBL_MAX;
+ goto skip;
+ }
+ /* determine z[j] = 1 - z'[j] */
+ for (j = 1; j <= n; j++)
+ { xassert(z[j] == 0 || z[j] == 1);
+ z[j] ^= 1;
+ }
+ /* check condition (11) for original (non-scaled) parameters */
+ s = 0;
+ for (j = 1; j <= n; j++)
+ { if (z[j])
+ s += a[j];
+ }
+ eps = 0.01 * (min_aj >= 1 ? min_aj : 1);
+ if (!(s >= b + eps))
+ { /* no cover found within a precision req'd */
+ s = DBL_MAX;
+ goto skip;
+ }
+ /* compute corresponding zeta (4) for cover found */
+ s = 0;
+ for (j = 1; j <= n; j++)
+ { if (z[j])
+ s += 1 - x[j];
+ }
+skip: /* free working arrays */
+ tfree(aa);
+ tfree(cc);
+ return s;
+}
+
+/**********************************************************************/
+
+void glp_cov_gen1(glp_prob *P, glp_cov *cov, glp_prob *pool)
+{ /* generate locally valid simple cover cuts */
+ int i, k, len, new_len, *ind;
+ double *val, rhs, *x, zeta;
+ char *z;
+ xassert(P->n == cov->n && P->n == cov->set->n);
+ xassert(glp_get_status(P) == GLP_OPT);
+ /* allocate working arrays */
+ ind = talloc(1+P->n, int);
+ val = talloc(1+P->n, double);
+ x = talloc(1+P->n, double);
+ z = talloc(1+P->n, char);
+ /* walk thru 0-1 knapsack inequalities */
+ for (i = 1; i <= cov->set->m; i++)
+ { /* retrieve 0-1 knapsack inequality */
+ len = glp_get_mat_row(cov->set, i, ind, val);
+ rhs = glp_get_row_ub(cov->set, i);
+ xassert(rhs != +DBL_MAX);
+ /* FIXME: skip, if slack is too large? */
+ /* substitute and eliminate binary variables which have been
+ * fixed in the current subproblem (this makes the inequality
+ * only locally valid) */
+ new_len = 0;
+ for (k = 1; k <= len; k++)
+ { if (glp_get_col_type(P, ind[k]) == GLP_FX)
+ rhs -= val[k] * glp_get_col_prim(P, ind[k]);
+ else
+ { new_len++;
+ ind[new_len] = ind[k];
+ val[new_len] = val[k];
+ }
+ }
+ len = new_len;
+ /* we need at least 3 binary variables in the inequality */
+ if (len <= 2)
+ continue;
+ /* obtain values of binary variables from optimal solution to
+ * LP relaxation of current subproblem */
+ for (k = 1; k <= len; k++)
+ { xassert(glp_get_col_kind(P, ind[k]) == GLP_BV);
+ x[k] = glp_get_col_prim(P, ind[k]);
+ if (x[k] < 0.00001)
+ x[k] = 0;
+ else if (x[k] > 0.99999)
+ x[k] = 1;
+ /* if val[k] < 0, perform substitution x[k] = 1 - x'[k] to
+ * make all coefficients positive */
+ if (val[k] < 0)
+ { ind[k] = - ind[k]; /* x[k] is complemented */
+ val[k] = - val[k];
+ rhs += val[k];
+ x[k] = 1 - x[k];
+ }
+ }
+ /* find locally valid simple cover cut */
+ zeta = simple_cover(len, val, rhs, x, z);
+ if (zeta > 0.95)
+ { /* no violation or insufficient violation; see (2) */
+ continue;
+ }
+ /* construct cover inequality (2) for the cover found, which
+ * for original binary variables x[k] is equivalent to:
+ * sum{k in C'} x[k] + sum{k in C"} x'[k] <= |C| - 1
+ * or
+ * sum{k in C'} x[k] + sum{k in C"} (1 - x[k]) <= |C| - 1
+ * or
+ * sum{k in C'} x[k] - sum{k in C"} x[k] <= |C'| - 1
+ * since |C| - |C"| = |C'| */
+ new_len = 0;
+ rhs = -1;
+ for (k = 1; k <= len; k++)
+ { if (z[k])
+ { new_len++;
+ if (ind[k] > 0)
+ { ind[new_len] = +ind[k];
+ val[new_len] = +1;
+ rhs++;
+ }
+ else /* ind[k] < 0 */
+ { ind[new_len] = -ind[k];
+ val[new_len] = -1;
+ }
+ }
+ }
+ len = new_len;
+ /* add the cover inequality to the local cut pool */
+ k = glp_add_rows(pool, 1);
+ glp_set_mat_row(pool, k, len, ind, val);
+ glp_set_row_bnds(pool, k, GLP_UP, rhs, rhs);
+ }
+ /* free working arrays */
+ tfree(ind);
+ tfree(val);
+ tfree(x);
+ tfree(z);
+ return;
+}
+
+/**********************************************************************/
+
+void glp_cov_free(glp_cov *cov)
+{ /* delete cover cut generator workspace */
+ xassert(cov != NULL);
+ glp_delete_prob(cov->set);
+ tfree(cov);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/fpump.c b/test/monniaux/glpk-4.65/src/intopt/fpump.c
new file mode 100644
index 00000000..0bdd6d4e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/fpump.c
@@ -0,0 +1,360 @@
+/* fpump.c (feasibility pump heuristic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+#include "rng.h"
+
+/***********************************************************************
+* NAME
+*
+* ios_feas_pump - feasibility pump heuristic
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void ios_feas_pump(glp_tree *T);
+*
+* DESCRIPTION
+*
+* The routine ios_feas_pump is a simple implementation of the Feasi-
+* bility Pump heuristic.
+*
+* REFERENCES
+*
+* M.Fischetti, F.Glover, and A.Lodi. "The feasibility pump." Math.
+* Program., Ser. A 104, pp. 91-104 (2005). */
+
+struct VAR
+{ /* binary variable */
+ int j;
+ /* ordinal number */
+ int x;
+ /* value in the rounded solution (0 or 1) */
+ double d;
+ /* sorting key */
+};
+
+static int CDECL fcmp(const void *x, const void *y)
+{ /* comparison routine */
+ const struct VAR *vx = x, *vy = y;
+ if (vx->d > vy->d)
+ return -1;
+ else if (vx->d < vy->d)
+ return +1;
+ else
+ return 0;
+}
+
+void ios_feas_pump(glp_tree *T)
+{ glp_prob *P = T->mip;
+ int n = P->n;
+ glp_prob *lp = NULL;
+ struct VAR *var = NULL;
+ RNG *rand = NULL;
+ GLPCOL *col;
+ glp_smcp parm;
+ int j, k, new_x, nfail, npass, nv, ret, stalling;
+ double dist, tol;
+ xassert(glp_get_status(P) == GLP_OPT);
+ /* this heuristic is applied only once on the root level */
+ if (!(T->curr->level == 0 && T->curr->solved == 1)) goto done;
+ /* determine number of binary variables */
+ nv = 0;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ /* if x[j] is continuous, skip it */
+ if (col->kind == GLP_CV) continue;
+ /* if x[j] is fixed, skip it */
+ if (col->type == GLP_FX) continue;
+ /* x[j] is non-fixed integer */
+ xassert(col->kind == GLP_IV);
+ if (col->type == GLP_DB && col->lb == 0.0 && col->ub == 1.0)
+ { /* x[j] is binary */
+ nv++;
+ }
+ else
+ { /* x[j] is general integer */
+ if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("FPUMP heuristic cannot be applied due to genera"
+ "l integer variables\n");
+ goto done;
+ }
+ }
+ /* there must be at least one binary variable */
+ if (nv == 0) goto done;
+ if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Applying FPUMP heuristic...\n");
+ /* build the list of binary variables */
+ var = xcalloc(1+nv, sizeof(struct VAR));
+ k = 0;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ if (col->kind == GLP_IV && col->type == GLP_DB)
+ var[++k].j = j;
+ }
+ xassert(k == nv);
+ /* create working problem object */
+ lp = glp_create_prob();
+more: /* copy the original problem object to keep it intact */
+ glp_copy_prob(lp, P, GLP_OFF);
+ /* we are interested to find an integer feasible solution, which
+ is better than the best known one */
+ if (P->mip_stat == GLP_FEAS)
+ { int *ind;
+ double *val, bnd;
+ /* add a row and make it identical to the objective row */
+ glp_add_rows(lp, 1);
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++)
+ { ind[j] = j;
+ val[j] = P->col[j]->coef;
+ }
+ glp_set_mat_row(lp, lp->m, n, ind, val);
+ xfree(ind);
+ xfree(val);
+ /* introduce upper (minimization) or lower (maximization)
+ bound to the original objective function; note that this
+ additional constraint is not violated at the optimal point
+ to LP relaxation */
+#if 0 /* modified by xypron <xypron.glpk@gmx.de> */
+ if (P->dir == GLP_MIN)
+ { bnd = P->mip_obj - 0.10 * (1.0 + fabs(P->mip_obj));
+ if (bnd < P->obj_val) bnd = P->obj_val;
+ glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
+ }
+ else if (P->dir == GLP_MAX)
+ { bnd = P->mip_obj + 0.10 * (1.0 + fabs(P->mip_obj));
+ if (bnd > P->obj_val) bnd = P->obj_val;
+ glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
+ }
+ else
+ xassert(P != P);
+#else
+ bnd = 0.1 * P->obj_val + 0.9 * P->mip_obj;
+ /* xprintf("bnd = %f\n", bnd); */
+ if (P->dir == GLP_MIN)
+ glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
+ else if (P->dir == GLP_MAX)
+ glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
+ else
+ xassert(P != P);
+#endif
+ }
+ /* reset pass count */
+ npass = 0;
+ /* invalidate the rounded point */
+ for (k = 1; k <= nv; k++)
+ var[k].x = -1;
+pass: /* next pass starts here */
+ npass++;
+ if (T->parm->msg_lev >= GLP_MSG_ALL)
+ xprintf("Pass %d\n", npass);
+ /* initialize minimal distance between the basic point and the
+ rounded one obtained during this pass */
+ dist = DBL_MAX;
+ /* reset failure count (the number of succeeded iterations failed
+ to improve the distance) */
+ nfail = 0;
+ /* if it is not the first pass, perturb the last rounded point
+ rather than construct it from the basic solution */
+ if (npass > 1)
+ { double rho, temp;
+ if (rand == NULL)
+ rand = rng_create_rand();
+ for (k = 1; k <= nv; k++)
+ { j = var[k].j;
+ col = lp->col[j];
+ rho = rng_uniform(rand, -0.3, 0.7);
+ if (rho < 0.0) rho = 0.0;
+ temp = fabs((double)var[k].x - col->prim);
+ if (temp + rho > 0.5) var[k].x = 1 - var[k].x;
+ }
+ goto skip;
+ }
+loop: /* innermost loop begins here */
+ /* round basic solution (which is assumed primal feasible) */
+ stalling = 1;
+ for (k = 1; k <= nv; k++)
+ { col = lp->col[var[k].j];
+ if (col->prim < 0.5)
+ { /* rounded value is 0 */
+ new_x = 0;
+ }
+ else
+ { /* rounded value is 1 */
+ new_x = 1;
+ }
+ if (var[k].x != new_x)
+ { stalling = 0;
+ var[k].x = new_x;
+ }
+ }
+ /* if the rounded point has not changed (stalling), choose and
+ flip some its entries heuristically */
+ if (stalling)
+ { /* compute d[j] = |x[j] - round(x[j])| */
+ for (k = 1; k <= nv; k++)
+ { col = lp->col[var[k].j];
+ var[k].d = fabs(col->prim - (double)var[k].x);
+ }
+ /* sort the list of binary variables by descending d[j] */
+ qsort(&var[1], nv, sizeof(struct VAR), fcmp);
+ /* choose and flip some rounded components */
+ for (k = 1; k <= nv; k++)
+ { if (k >= 5 && var[k].d < 0.35 || k >= 10) break;
+ var[k].x = 1 - var[k].x;
+ }
+ }
+skip: /* check if the time limit has been exhausted */
+ if (T->parm->tm_lim < INT_MAX &&
+ (double)(T->parm->tm_lim - 1) <=
+ 1000.0 * xdifftime(xtime(), T->tm_beg)) goto done;
+ /* build the objective, which is the distance between the current
+ (basic) point and the rounded one */
+ lp->dir = GLP_MIN;
+ lp->c0 = 0.0;
+ for (j = 1; j <= n; j++)
+ lp->col[j]->coef = 0.0;
+ for (k = 1; k <= nv; k++)
+ { j = var[k].j;
+ if (var[k].x == 0)
+ lp->col[j]->coef = +1.0;
+ else
+ { lp->col[j]->coef = -1.0;
+ lp->c0 += 1.0;
+ }
+ }
+ /* minimize the distance with the simplex method */
+ glp_init_smcp(&parm);
+ if (T->parm->msg_lev <= GLP_MSG_ERR)
+ parm.msg_lev = T->parm->msg_lev;
+ else if (T->parm->msg_lev <= GLP_MSG_ALL)
+ { parm.msg_lev = GLP_MSG_ON;
+ parm.out_dly = 10000;
+ }
+ ret = glp_simplex(lp, &parm);
+ if (ret != 0)
+ { if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: glp_simplex returned %d\n", ret);
+ goto done;
+ }
+ ret = glp_get_status(lp);
+ if (ret != GLP_OPT)
+ { if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: glp_get_status returned %d\n", ret);
+ goto done;
+ }
+ if (T->parm->msg_lev >= GLP_MSG_DBG)
+ xprintf("delta = %g\n", lp->obj_val);
+ /* check if the basic solution is integer feasible; note that it
+ may be so even if the minimial distance is positive */
+ tol = 0.3 * T->parm->tol_int;
+ for (k = 1; k <= nv; k++)
+ { col = lp->col[var[k].j];
+ if (tol < col->prim && col->prim < 1.0 - tol) break;
+ }
+ if (k > nv)
+ { /* okay; the basic solution seems to be integer feasible */
+ double *x = xcalloc(1+n, sizeof(double));
+ for (j = 1; j <= n; j++)
+ { x[j] = lp->col[j]->prim;
+ if (P->col[j]->kind == GLP_IV) x[j] = floor(x[j] + 0.5);
+ }
+#if 1 /* modified by xypron <xypron.glpk@gmx.de> */
+ /* reset direction and right-hand side of objective */
+ lp->c0 = P->c0;
+ lp->dir = P->dir;
+ /* fix integer variables */
+ for (k = 1; k <= nv; k++)
+#if 0 /* 18/VI-2013; fixed by mao
+ * this bug causes numerical instability, because column statuses
+ * are not changed appropriately */
+ { lp->col[var[k].j]->lb = x[var[k].j];
+ lp->col[var[k].j]->ub = x[var[k].j];
+ lp->col[var[k].j]->type = GLP_FX;
+ }
+#else
+ glp_set_col_bnds(lp, var[k].j, GLP_FX, x[var[k].j], 0.);
+#endif
+ /* copy original objective function */
+ for (j = 1; j <= n; j++)
+ lp->col[j]->coef = P->col[j]->coef;
+ /* solve original LP and copy result */
+ ret = glp_simplex(lp, &parm);
+ if (ret != 0)
+ { if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: glp_simplex returned %d\n", ret);
+#if 1 /* 17/III-2016: fix memory leak */
+ xfree(x);
+#endif
+ goto done;
+ }
+ ret = glp_get_status(lp);
+ if (ret != GLP_OPT)
+ { if (T->parm->msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: glp_get_status returned %d\n", ret);
+#if 1 /* 17/III-2016: fix memory leak */
+ xfree(x);
+#endif
+ goto done;
+ }
+ for (j = 1; j <= n; j++)
+ if (P->col[j]->kind != GLP_IV) x[j] = lp->col[j]->prim;
+#endif
+ ret = glp_ios_heur_sol(T, x);
+ xfree(x);
+ if (ret == 0)
+ { /* the integer solution is accepted */
+ if (ios_is_hopeful(T, T->curr->bound))
+ { /* it is reasonable to apply the heuristic once again */
+ goto more;
+ }
+ else
+ { /* the best known integer feasible solution just found
+ is close to optimal solution to LP relaxation */
+ goto done;
+ }
+ }
+ }
+ /* the basic solution is fractional */
+ if (dist == DBL_MAX ||
+ lp->obj_val <= dist - 1e-6 * (1.0 + dist))
+ { /* the distance is reducing */
+ nfail = 0, dist = lp->obj_val;
+ }
+ else
+ { /* improving the distance failed */
+ nfail++;
+ }
+ if (nfail < 3) goto loop;
+ if (npass < 5) goto pass;
+done: /* delete working objects */
+ if (lp != NULL) glp_delete_prob(lp);
+ if (var != NULL) xfree(var);
+ if (rand != NULL) rng_delete_rand(rand);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/gmicut.c b/test/monniaux/glpk-4.65/src/intopt/gmicut.c
new file mode 100644
index 00000000..4ef0b746
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/gmicut.c
@@ -0,0 +1,284 @@
+/* gmicut.c (Gomory's mixed integer cut generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2002-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_gmi_cut - generate Gomory's mixed integer cut (core routine)
+*
+* SYNOPSIS
+*
+* int glp_gmi_cut(glp_prob *P, int j, int ind[], double val[], double
+* phi[]);
+*
+* DESCRIPTION
+*
+* This routine attempts to generate a Gomory's mixed integer cut for
+* specified integer column (structural variable), whose primal value
+* in current basic solution is integer infeasible (fractional).
+*
+* On entry to the routine the basic solution contained in the problem
+* object P should be optimal, and the basis factorization should be
+* valid. The parameter j should specify the ordinal number of column
+* (structural variable x[j]), for which the cut should be generated,
+* 1 <= j <= n, where n is the number of columns in the problem object.
+* This column should be integer, non-fixed, and basic, and its primal
+* value should be fractional.
+*
+* The cut generated by the routine is the following inequality:
+*
+* sum a[j] * x[j] >= b,
+*
+* which is expected to be violated at the current basic solution.
+*
+* If the cut has been successfully generated, the routine stores its
+* non-zero coefficients a[j] and corresponding column indices j in the
+* array locations val[1], ..., val[len] and ind[1], ..., ind[len],
+* where 1 <= len <= n is the number of non-zero coefficients. The
+* right-hand side value b is stored in val[0], and ind[0] is set to 0.
+*
+* The working array phi should have 1+m+n locations (location phi[0]
+* is not used), where m and n is the number of rows and columns in the
+* problem object, resp.
+*
+* RETURNS
+*
+* If the cut has been successfully generated, the routine returns
+* len, the number of non-zero coefficients in the cut, 1 <= len <= n.
+*
+* Otherwise, the routine returns one of the following codes:
+*
+* -1 current basis factorization is not valid;
+*
+* -2 current basic solution is not optimal;
+*
+* -3 column ordinal number j is out of range;
+*
+* -4 variable x[j] is not of integral kind;
+*
+* -5 variable x[j] is either fixed or non-basic;
+*
+* -6 primal value of variable x[j] in basic solution is too close
+* to nearest integer;
+*
+* -7 some coefficients in the simplex table row corresponding to
+* variable x[j] are too large in magnitude;
+*
+* -8 some free (unbounded) variables have non-zero coefficients in
+* the simplex table row corresponding to variable x[j].
+*
+* ALGORITHM
+*
+* See glpk/doc/notes/gomory (in Russian). */
+
+#define f(x) ((x) - floor(x))
+/* compute fractional part of x */
+
+int glp_gmi_cut(glp_prob *P, int j,
+ int ind[/*1+n*/], double val[/*1+n*/], double phi[/*1+m+n*/])
+{ int m = P->m;
+ int n = P->n;
+ GLPROW *row;
+ GLPCOL *col;
+ GLPAIJ *aij;
+ int i, k, len, kind, stat;
+ double lb, ub, alfa, beta, ksi, phi1, rhs;
+ /* sanity checks */
+ if (!(P->m == 0 || P->valid))
+ { /* current basis factorization is not valid */
+ return -1;
+ }
+ if (!(P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS))
+ { /* current basic solution is not optimal */
+ return -2;
+ }
+ if (!(1 <= j && j <= n))
+ { /* column ordinal number is out of range */
+ return -3;
+ }
+ col = P->col[j];
+ if (col->kind != GLP_IV)
+ { /* x[j] is not of integral kind */
+ return -4;
+ }
+ if (col->type == GLP_FX || col->stat != GLP_BS)
+ { /* x[j] is either fixed or non-basic */
+ return -5;
+ }
+ if (fabs(col->prim - floor(col->prim + 0.5)) < 0.001)
+ { /* primal value of x[j] is too close to nearest integer */
+ return -6;
+ }
+ /* compute row of the simplex tableau, which (row) corresponds
+ * to specified basic variable xB[i] = x[j]; see (23) */
+ len = glp_eval_tab_row(P, m+j, ind, val);
+ /* determine beta[i], which a value of xB[i] in optimal solution
+ * to current LP relaxation; note that this value is the same as
+ * if it would be computed with formula (27); it is assumed that
+ * beta[i] is fractional enough */
+ beta = P->col[j]->prim;
+ /* compute cut coefficients phi and right-hand side rho, which
+ * correspond to formula (30); dense format is used, because rows
+ * of the simplex tableau are usually dense */
+ for (k = 1; k <= m+n; k++)
+ phi[k] = 0.0;
+ rhs = f(beta); /* initial value of rho; see (28), (32) */
+ for (j = 1; j <= len; j++)
+ { /* determine original number of non-basic variable xN[j] */
+ k = ind[j];
+ xassert(1 <= k && k <= m+n);
+ /* determine the kind, bounds and current status of xN[j] in
+ * optimal solution to LP relaxation */
+ if (k <= m)
+ { /* auxiliary variable */
+ row = P->row[k];
+ kind = GLP_CV;
+ lb = row->lb;
+ ub = row->ub;
+ stat = row->stat;
+ }
+ else
+ { /* structural variable */
+ col = P->col[k-m];
+ kind = col->kind;
+ lb = col->lb;
+ ub = col->ub;
+ stat = col->stat;
+ }
+ /* xN[j] cannot be basic */
+ xassert(stat != GLP_BS);
+ /* determine row coefficient ksi[i,j] at xN[j]; see (23) */
+ ksi = val[j];
+ /* if ksi[i,j] is too large in magnitude, report failure */
+ if (fabs(ksi) > 1e+05)
+ return -7;
+ /* if ksi[i,j] is too small in magnitude, skip it */
+ if (fabs(ksi) < 1e-10)
+ goto skip;
+ /* compute row coefficient alfa[i,j] at y[j]; see (26) */
+ switch (stat)
+ { case GLP_NF:
+ /* xN[j] is free (unbounded) having non-zero ksi[i,j];
+ * report failure */
+ return -8;
+ case GLP_NL:
+ /* xN[j] has active lower bound */
+ alfa = - ksi;
+ break;
+ case GLP_NU:
+ /* xN[j] has active upper bound */
+ alfa = + ksi;
+ break;
+ case GLP_NS:
+ /* xN[j] is fixed; skip it */
+ goto skip;
+ default:
+ xassert(stat != stat);
+ }
+ /* compute cut coefficient phi'[j] at y[j]; see (21), (28) */
+ switch (kind)
+ { case GLP_IV:
+ /* y[j] is integer */
+ if (fabs(alfa - floor(alfa + 0.5)) < 1e-10)
+ { /* alfa[i,j] is close to nearest integer; skip it */
+ goto skip;
+ }
+ else if (f(alfa) <= f(beta))
+ phi1 = f(alfa);
+ else
+ phi1 = (f(beta) / (1.0 - f(beta))) * (1.0 - f(alfa));
+ break;
+ case GLP_CV:
+ /* y[j] is continuous */
+ if (alfa >= 0.0)
+ phi1 = + alfa;
+ else
+ phi1 = (f(beta) / (1.0 - f(beta))) * (- alfa);
+ break;
+ default:
+ xassert(kind != kind);
+ }
+ /* compute cut coefficient phi[j] at xN[j] and update right-
+ * hand side rho; see (31), (32) */
+ switch (stat)
+ { case GLP_NL:
+ /* xN[j] has active lower bound */
+ phi[k] = + phi1;
+ rhs += phi1 * lb;
+ break;
+ case GLP_NU:
+ /* xN[j] has active upper bound */
+ phi[k] = - phi1;
+ rhs -= phi1 * ub;
+ break;
+ default:
+ xassert(stat != stat);
+ }
+skip: ;
+ }
+ /* now the cut has the form sum_k phi[k] * x[k] >= rho, where cut
+ * coefficients are stored in the array phi in dense format;
+ * x[1,...,m] are auxiliary variables, x[m+1,...,m+n] are struc-
+ * tural variables; see (30) */
+ /* eliminate auxiliary variables in order to express the cut only
+ * through structural variables; see (33) */
+ for (i = 1; i <= m; i++)
+ { if (fabs(phi[i]) < 1e-10)
+ continue;
+ /* auxiliary variable x[i] has non-zero cut coefficient */
+ row = P->row[i];
+ /* x[i] cannot be fixed variable */
+ xassert(row->type != GLP_FX);
+ /* substitute x[i] = sum_j a[i,j] * x[m+j] */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ phi[m+aij->col->j] += phi[i] * aij->val;
+ }
+ /* convert the final cut to sparse format and substitute fixed
+ * (structural) variables */
+ len = 0;
+ for (j = 1; j <= n; j++)
+ { if (fabs(phi[m+j]) < 1e-10)
+ continue;
+ /* structural variable x[m+j] has non-zero cut coefficient */
+ col = P->col[j];
+ if (col->type == GLP_FX)
+ { /* eliminate x[m+j] */
+ rhs -= phi[m+j] * col->lb;
+ }
+ else
+ { len++;
+ ind[len] = j;
+ val[len] = phi[m+j];
+ }
+ }
+ if (fabs(rhs) < 1e-12)
+ rhs = 0.0;
+ ind[0] = 0, val[0] = rhs;
+ /* the cut has been successfully generated */
+ return len;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/gmigen.c b/test/monniaux/glpk-4.65/src/intopt/gmigen.c
new file mode 100644
index 00000000..627682cb
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/gmigen.c
@@ -0,0 +1,142 @@
+/* gmigen.c (Gomory's mixed integer cuts generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2002-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "prob.h"
+
+/***********************************************************************
+* NAME
+*
+* glp_gmi_gen - generate Gomory's mixed integer cuts
+*
+* SYNOPSIS
+*
+* int glp_gmi_gen(glp_prob *P, glp_prob *pool, int max_cuts);
+*
+* DESCRIPTION
+*
+* This routine attempts to generate Gomory's mixed integer cuts for
+* integer variables, whose primal values in current basic solution are
+* integer infeasible (fractional).
+*
+* On entry to the routine the basic solution contained in the problem
+* object P should be optimal, and the basis factorization should be
+* valid.
+*
+* The cutting plane inequalities generated by the routine are added to
+* the specified cut pool.
+*
+* The parameter max_cuts specifies the maximal number of cuts to be
+* generated. Note that the number of cuts cannot exceed the number of
+* basic variables, which is the number of rows in the problem object.
+*
+* RETURNS
+*
+* The routine returns the number of cuts that have been generated and
+* added to the cut pool. */
+
+#define f(x) ((x) - floor(x))
+/* compute fractional part of x */
+
+struct var { int j; double f; };
+
+static int CDECL fcmp(const void *p1, const void *p2)
+{ const struct var *v1 = p1, *v2 = p2;
+ if (v1->f > v2->f) return -1;
+ if (v1->f < v2->f) return +1;
+ return 0;
+}
+
+int glp_gmi_gen(glp_prob *P, glp_prob *pool, int max_cuts)
+{ int m = P->m;
+ int n = P->n;
+ GLPCOL *col;
+ struct var *var;
+ int i, j, k, t, len, nv, nnn, *ind;
+ double frac, *val, *phi;
+ /* sanity checks */
+ if (!(P->m == 0 || P->valid))
+ xerror("glp_gmi_gen: basis factorization does not exist\n");
+ if (!(P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS))
+ xerror("glp_gmi_gen: optimal basic solution required\n");
+ if (pool->n != n)
+ xerror("glp_gmi_gen: cut pool has wrong number of columns\n");
+ /* allocate working arrays */
+ var = xcalloc(1+n, sizeof(struct var));
+ ind = xcalloc(1+n, sizeof(int));
+ val = xcalloc(1+n, sizeof(double));
+ phi = xcalloc(1+m+n, sizeof(double));
+ /* build the list of integer structural variables, which are
+ * basic and have integer infeasible (fractional) primal values
+ * in optimal solution to specified LP */
+ nv = 0;
+ for (j = 1; j <= n; j++)
+ { col = P->col[j];
+ if (col->kind != GLP_IV)
+ continue;
+ if (col->type == GLP_FX)
+ continue;
+ if (col->stat != GLP_BS)
+ continue;
+ frac = f(col->prim);
+ if (!(0.05 <= frac && frac <= 0.95))
+ continue;
+ /* add variable to the list */
+ nv++, var[nv].j = j, var[nv].f = frac;
+ }
+ /* sort the list by descending fractionality */
+ qsort(&var[1], nv, sizeof(struct var), fcmp);
+ /* try to generate cuts by one for each variable in the list, but
+ * not more than max_cuts cuts */
+ nnn = 0;
+ for (t = 1; t <= nv; t++)
+ { len = glp_gmi_cut(P, var[t].j, ind, val, phi);
+ if (len < 1)
+ goto skip;
+ /* if the cut inequality seems to be badly scaled, reject it
+ * to avoid numerical difficulties */
+ for (k = 1; k <= len; k++)
+ { if (fabs(val[k]) < 1e-03)
+ goto skip;
+ if (fabs(val[k]) > 1e+03)
+ goto skip;
+ }
+ /* add the cut to the cut pool for further consideration */
+ i = glp_add_rows(pool, 1);
+ glp_set_row_bnds(pool, i, GLP_LO, val[0], 0);
+ glp_set_mat_row(pool, i, len, ind, val);
+ /* one cut has been generated */
+ nnn++;
+ if (nnn == max_cuts)
+ break;
+skip: ;
+ }
+ /* free working arrays */
+ xfree(var);
+ xfree(ind);
+ xfree(val);
+ xfree(phi);
+ return nnn;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/mirgen.c b/test/monniaux/glpk-4.65/src/intopt/mirgen.c
new file mode 100644
index 00000000..45a0a55d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/mirgen.c
@@ -0,0 +1,1529 @@
+/* mirgen.c (mixed integer rounding cuts generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2007-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#if 1 /* 29/II-2016 by Chris */
+/*----------------------------------------------------------------------
+Subject: Mir cut generation performance improvement
+From: Chris Matrakidis <cmatraki@gmail.com>
+To: Andrew Makhorin <mao@gnu.org>, help-glpk <help-glpk@gnu.org>
+
+Andrew,
+
+I noticed that mir cut generation takes considerable time on some large
+problems (like rocII-4-11 from miplib). The attached patch makes two
+improvements that considerably improve performance in such instances:
+1. A lot of time was spent on generating a temporary vector in function
+aggregate_row. It is a lot faster to reuse an existing vector.
+2. A search for an element in the same function was done in row order,
+where using the elements in the order they are in the column is more
+efficient. This changes the generated cuts in some cases, but seems
+neutral overall (0.3% less cuts in a test set of 64 miplib instances).
+
+Best Regards,
+
+Chris Matrakidis
+----------------------------------------------------------------------*/
+#endif
+
+#include "env.h"
+#include "prob.h"
+#include "spv.h"
+
+#define MIR_DEBUG 0
+
+#define MAXAGGR 5
+/* maximal number of rows that can be aggregated */
+
+struct glp_mir
+{ /* MIR cut generator working area */
+ /*--------------------------------------------------------------*/
+ /* global information valid for the root subproblem */
+ int m;
+ /* number of rows (in the root subproblem) */
+ int n;
+ /* number of columns */
+ char *skip; /* char skip[1+m]; */
+ /* skip[i], 1 <= i <= m, is a flag that means that row i should
+ not be used because (1) it is not suitable, or (2) because it
+ has been used in the aggregated constraint */
+ char *isint; /* char isint[1+m+n]; */
+ /* isint[k], 1 <= k <= m+n, is a flag that means that variable
+ x[k] is integer (otherwise, continuous) */
+ double *lb; /* double lb[1+m+n]; */
+ /* lb[k], 1 <= k <= m+n, is lower bound of x[k]; -DBL_MAX means
+ that x[k] has no lower bound */
+ int *vlb; /* int vlb[1+m+n]; */
+ /* vlb[k] = k', 1 <= k <= m+n, is the number of integer variable,
+ which defines variable lower bound x[k] >= lb[k] * x[k']; zero
+ means that x[k] has simple lower bound */
+ double *ub; /* double ub[1+m+n]; */
+ /* ub[k], 1 <= k <= m+n, is upper bound of x[k]; +DBL_MAX means
+ that x[k] has no upper bound */
+ int *vub; /* int vub[1+m+n]; */
+ /* vub[k] = k', 1 <= k <= m+n, is the number of integer variable,
+ which defines variable upper bound x[k] <= ub[k] * x[k']; zero
+ means that x[k] has simple upper bound */
+ /*--------------------------------------------------------------*/
+ /* current (fractional) point to be separated */
+ double *x; /* double x[1+m+n]; */
+ /* x[k] is current value of auxiliary (1 <= k <= m) or structural
+ (m+1 <= k <= m+n) variable */
+ /*--------------------------------------------------------------*/
+ /* aggregated constraint sum a[k] * x[k] = b, which is a linear
+ combination of original constraints transformed to equalities
+ by introducing auxiliary variables */
+ int agg_cnt;
+ /* number of rows (original constraints) used to build aggregated
+ constraint, 1 <= agg_cnt <= MAXAGGR */
+ int *agg_row; /* int agg_row[1+MAXAGGR]; */
+ /* agg_row[k], 1 <= k <= agg_cnt, is the row number used to build
+ aggregated constraint */
+ SPV *agg_vec; /* SPV agg_vec[1:m+n]; */
+ /* sparse vector of aggregated constraint coefficients, a[k] */
+ double agg_rhs;
+ /* right-hand side of the aggregated constraint, b */
+ /*--------------------------------------------------------------*/
+ /* bound substitution flags for modified constraint */
+ char *subst; /* char subst[1+m+n]; */
+ /* subst[k], 1 <= k <= m+n, is a bound substitution flag used for
+ variable x[k]:
+ '?' - x[k] is missing in modified constraint
+ 'L' - x[k] = (lower bound) + x'[k]
+ 'U' - x[k] = (upper bound) - x'[k] */
+ /*--------------------------------------------------------------*/
+ /* modified constraint sum a'[k] * x'[k] = b', where x'[k] >= 0,
+ derived from aggregated constraint by substituting bounds;
+ note that due to substitution of variable bounds there may be
+ additional terms in the modified constraint */
+ SPV *mod_vec; /* SPV mod_vec[1:m+n]; */
+ /* sparse vector of modified constraint coefficients, a'[k] */
+ double mod_rhs;
+ /* right-hand side of the modified constraint, b' */
+ /*--------------------------------------------------------------*/
+ /* cutting plane sum alpha[k] * x[k] <= beta */
+ SPV *cut_vec; /* SPV cut_vec[1:m+n]; */
+ /* sparse vector of cutting plane coefficients, alpha[k] */
+ double cut_rhs;
+ /* right-hand size of the cutting plane, beta */
+};
+
+/***********************************************************************
+* NAME
+*
+* glp_mir_init - create and initialize MIR cut generator
+*
+* SYNOPSIS
+*
+* glp_mir *glp_mir_init(glp_prob *P);
+*
+* DESCRIPTION
+*
+* This routine creates and initializes the MIR cut generator for the
+* specified problem object.
+*
+* RETURNS
+*
+* The routine returns a pointer to the MIR cut generator workspace. */
+
+static void set_row_attrib(glp_prob *mip, glp_mir *mir)
+{ /* set global row attributes */
+ int m = mir->m;
+ int k;
+ for (k = 1; k <= m; k++)
+ { GLPROW *row = mip->row[k];
+ mir->skip[k] = 0;
+ mir->isint[k] = 0;
+ switch (row->type)
+ { case GLP_FR:
+ mir->lb[k] = -DBL_MAX, mir->ub[k] = +DBL_MAX; break;
+ case GLP_LO:
+ mir->lb[k] = row->lb, mir->ub[k] = +DBL_MAX; break;
+ case GLP_UP:
+ mir->lb[k] = -DBL_MAX, mir->ub[k] = row->ub; break;
+ case GLP_DB:
+ mir->lb[k] = row->lb, mir->ub[k] = row->ub; break;
+ case GLP_FX:
+ mir->lb[k] = mir->ub[k] = row->lb; break;
+ default:
+ xassert(row != row);
+ }
+ mir->vlb[k] = mir->vub[k] = 0;
+ }
+ return;
+}
+
+static void set_col_attrib(glp_prob *mip, glp_mir *mir)
+{ /* set global column attributes */
+ int m = mir->m;
+ int n = mir->n;
+ int k;
+ for (k = m+1; k <= m+n; k++)
+ { GLPCOL *col = mip->col[k-m];
+ switch (col->kind)
+ { case GLP_CV:
+ mir->isint[k] = 0; break;
+ case GLP_IV:
+ mir->isint[k] = 1; break;
+ default:
+ xassert(col != col);
+ }
+ switch (col->type)
+ { case GLP_FR:
+ mir->lb[k] = -DBL_MAX, mir->ub[k] = +DBL_MAX; break;
+ case GLP_LO:
+ mir->lb[k] = col->lb, mir->ub[k] = +DBL_MAX; break;
+ case GLP_UP:
+ mir->lb[k] = -DBL_MAX, mir->ub[k] = col->ub; break;
+ case GLP_DB:
+ mir->lb[k] = col->lb, mir->ub[k] = col->ub; break;
+ case GLP_FX:
+ mir->lb[k] = mir->ub[k] = col->lb; break;
+ default:
+ xassert(col != col);
+ }
+ mir->vlb[k] = mir->vub[k] = 0;
+ }
+ return;
+}
+
+static void set_var_bounds(glp_prob *mip, glp_mir *mir)
+{ /* set variable bounds */
+ int m = mir->m;
+ GLPAIJ *aij;
+ int i, k1, k2;
+ double a1, a2;
+ for (i = 1; i <= m; i++)
+ { /* we need the row to be '>= 0' or '<= 0' */
+ if (!(mir->lb[i] == 0.0 && mir->ub[i] == +DBL_MAX ||
+ mir->lb[i] == -DBL_MAX && mir->ub[i] == 0.0)) continue;
+ /* take first term */
+ aij = mip->row[i]->ptr;
+ if (aij == NULL) continue;
+ k1 = m + aij->col->j, a1 = aij->val;
+ /* take second term */
+ aij = aij->r_next;
+ if (aij == NULL) continue;
+ k2 = m + aij->col->j, a2 = aij->val;
+ /* there must be only two terms */
+ if (aij->r_next != NULL) continue;
+ /* interchange terms, if needed */
+ if (!mir->isint[k1] && mir->isint[k2])
+ ;
+ else if (mir->isint[k1] && !mir->isint[k2])
+ { k2 = k1, a2 = a1;
+ k1 = m + aij->col->j, a1 = aij->val;
+ }
+ else
+ { /* both terms are either continuous or integer */
+ continue;
+ }
+ /* x[k2] should be double-bounded */
+ if (mir->lb[k2] == -DBL_MAX || mir->ub[k2] == +DBL_MAX ||
+ mir->lb[k2] == mir->ub[k2]) continue;
+ /* change signs, if necessary */
+ if (mir->ub[i] == 0.0) a1 = - a1, a2 = - a2;
+ /* now the row has the form a1 * x1 + a2 * x2 >= 0, where x1
+ is continuous, x2 is integer */
+ if (a1 > 0.0)
+ { /* x1 >= - (a2 / a1) * x2 */
+ if (mir->vlb[k1] == 0)
+ { /* set variable lower bound for x1 */
+ mir->lb[k1] = - a2 / a1;
+ mir->vlb[k1] = k2;
+ /* the row should not be used */
+ mir->skip[i] = 1;
+ }
+ }
+ else /* a1 < 0.0 */
+ { /* x1 <= - (a2 / a1) * x2 */
+ if (mir->vub[k1] == 0)
+ { /* set variable upper bound for x1 */
+ mir->ub[k1] = - a2 / a1;
+ mir->vub[k1] = k2;
+ /* the row should not be used */
+ mir->skip[i] = 1;
+ }
+ }
+ }
+ return;
+}
+
+static void mark_useless_rows(glp_prob *mip, glp_mir *mir)
+{ /* mark rows which should not be used */
+ int m = mir->m;
+ GLPAIJ *aij;
+ int i, k, nv;
+ for (i = 1; i <= m; i++)
+ { /* free rows should not be used */
+ if (mir->lb[i] == -DBL_MAX && mir->ub[i] == +DBL_MAX)
+ { mir->skip[i] = 1;
+ continue;
+ }
+ nv = 0;
+ for (aij = mip->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ { k = m + aij->col->j;
+ /* rows with free variables should not be used */
+ if (mir->lb[k] == -DBL_MAX && mir->ub[k] == +DBL_MAX)
+ { mir->skip[i] = 1;
+ break;
+ }
+ /* rows with integer variables having infinite (lower or
+ upper) bound should not be used */
+ if (mir->isint[k] && mir->lb[k] == -DBL_MAX ||
+ mir->isint[k] && mir->ub[k] == +DBL_MAX)
+ { mir->skip[i] = 1;
+ break;
+ }
+ /* count non-fixed variables */
+ if (!(mir->vlb[k] == 0 && mir->vub[k] == 0 &&
+ mir->lb[k] == mir->ub[k])) nv++;
+ }
+ /* rows with all variables fixed should not be used */
+ if (nv == 0)
+ { mir->skip[i] = 1;
+ continue;
+ }
+ }
+ return;
+}
+
+glp_mir *glp_mir_init(glp_prob *mip)
+{ /* create and initialize MIR cut generator */
+ int m = mip->m;
+ int n = mip->n;
+ glp_mir *mir;
+#if MIR_DEBUG
+ xprintf("ios_mir_init: warning: debug mode enabled\n");
+#endif
+ /* allocate working area */
+ mir = xmalloc(sizeof(glp_mir));
+ mir->m = m;
+ mir->n = n;
+ mir->skip = xcalloc(1+m, sizeof(char));
+ mir->isint = xcalloc(1+m+n, sizeof(char));
+ mir->lb = xcalloc(1+m+n, sizeof(double));
+ mir->vlb = xcalloc(1+m+n, sizeof(int));
+ mir->ub = xcalloc(1+m+n, sizeof(double));
+ mir->vub = xcalloc(1+m+n, sizeof(int));
+ mir->x = xcalloc(1+m+n, sizeof(double));
+ mir->agg_row = xcalloc(1+MAXAGGR, sizeof(int));
+ mir->agg_vec = spv_create_vec(m+n);
+ mir->subst = xcalloc(1+m+n, sizeof(char));
+ mir->mod_vec = spv_create_vec(m+n);
+ mir->cut_vec = spv_create_vec(m+n);
+ /* set global row attributes */
+ set_row_attrib(mip, mir);
+ /* set global column attributes */
+ set_col_attrib(mip, mir);
+ /* set variable bounds */
+ set_var_bounds(mip, mir);
+ /* mark rows which should not be used */
+ mark_useless_rows(mip, mir);
+ return mir;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mir_gen - generate mixed integer rounding (MIR) cuts
+*
+* SYNOPSIS
+*
+* int glp_mir_gen(glp_prob *P, glp_mir *mir, glp_prob *pool);
+*
+* DESCRIPTION
+*
+* This routine attempts to generate mixed integer rounding (MIR) cuts
+* for current basic solution to the specified problem object.
+*
+* The cutting plane inequalities generated by the routine are added to
+* the specified cut pool.
+*
+* RETURNS
+*
+* The routine returns the number of cuts that have been generated and
+* added to the cut pool. */
+
+static void get_current_point(glp_prob *mip, glp_mir *mir)
+{ /* obtain current point */
+ int m = mir->m;
+ int n = mir->n;
+ int k;
+ for (k = 1; k <= m; k++)
+ mir->x[k] = mip->row[k]->prim;
+ for (k = m+1; k <= m+n; k++)
+ mir->x[k] = mip->col[k-m]->prim;
+ return;
+}
+
+#if MIR_DEBUG
+static void check_current_point(glp_mir *mir)
+{ /* check current point */
+ int m = mir->m;
+ int n = mir->n;
+ int k, kk;
+ double lb, ub, eps;
+ for (k = 1; k <= m+n; k++)
+ { /* determine lower bound */
+ lb = mir->lb[k];
+ kk = mir->vlb[k];
+ if (kk != 0)
+ { xassert(lb != -DBL_MAX);
+ xassert(!mir->isint[k]);
+ xassert(mir->isint[kk]);
+ lb *= mir->x[kk];
+ }
+ /* check lower bound */
+ if (lb != -DBL_MAX)
+ { eps = 1e-6 * (1.0 + fabs(lb));
+ xassert(mir->x[k] >= lb - eps);
+ }
+ /* determine upper bound */
+ ub = mir->ub[k];
+ kk = mir->vub[k];
+ if (kk != 0)
+ { xassert(ub != +DBL_MAX);
+ xassert(!mir->isint[k]);
+ xassert(mir->isint[kk]);
+ ub *= mir->x[kk];
+ }
+ /* check upper bound */
+ if (ub != +DBL_MAX)
+ { eps = 1e-6 * (1.0 + fabs(ub));
+ xassert(mir->x[k] <= ub + eps);
+ }
+ }
+ return;
+}
+#endif
+
+static void initial_agg_row(glp_prob *mip, glp_mir *mir, int i)
+{ /* use original i-th row as initial aggregated constraint */
+ int m = mir->m;
+ GLPAIJ *aij;
+ xassert(1 <= i && i <= m);
+ xassert(!mir->skip[i]);
+ /* mark i-th row in order not to use it in the same aggregated
+ constraint */
+ mir->skip[i] = 2;
+ mir->agg_cnt = 1;
+ mir->agg_row[1] = i;
+ /* use x[i] - sum a[i,j] * x[m+j] = 0, where x[i] is auxiliary
+ variable of row i, x[m+j] are structural variables */
+ spv_clear_vec(mir->agg_vec);
+ spv_set_vj(mir->agg_vec, i, 1.0);
+ for (aij = mip->row[i]->ptr; aij != NULL; aij = aij->r_next)
+ spv_set_vj(mir->agg_vec, m + aij->col->j, - aij->val);
+ mir->agg_rhs = 0.0;
+#if MIR_DEBUG
+ spv_check_vec(mir->agg_vec);
+#endif
+ return;
+}
+
+#if MIR_DEBUG
+static void check_agg_row(glp_mir *mir)
+{ /* check aggregated constraint */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k;
+ double r, big;
+ /* compute the residual r = sum a[k] * x[k] - b and determine
+ big = max(1, |a[k]|, |b|) */
+ r = 0.0, big = 1.0;
+ for (j = 1; j <= mir->agg_vec->nnz; j++)
+ { k = mir->agg_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ r += mir->agg_vec->val[j] * mir->x[k];
+ if (big < fabs(mir->agg_vec->val[j]))
+ big = fabs(mir->agg_vec->val[j]);
+ }
+ r -= mir->agg_rhs;
+ if (big < fabs(mir->agg_rhs))
+ big = fabs(mir->agg_rhs);
+ /* the residual must be close to zero */
+ xassert(fabs(r) <= 1e-6 * big);
+ return;
+}
+#endif
+
+static void subst_fixed_vars(glp_mir *mir)
+{ /* substitute fixed variables into aggregated constraint */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k;
+ for (j = 1; j <= mir->agg_vec->nnz; j++)
+ { k = mir->agg_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->vlb[k] == 0 && mir->vub[k] == 0 &&
+ mir->lb[k] == mir->ub[k])
+ { /* x[k] is fixed */
+ mir->agg_rhs -= mir->agg_vec->val[j] * mir->lb[k];
+ mir->agg_vec->val[j] = 0.0;
+ }
+ }
+ /* remove terms corresponding to fixed variables */
+ spv_clean_vec(mir->agg_vec, DBL_EPSILON);
+#if MIR_DEBUG
+ spv_check_vec(mir->agg_vec);
+#endif
+ return;
+}
+
+static void bound_subst_heur(glp_mir *mir)
+{ /* bound substitution heuristic */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k, kk;
+ double d1, d2;
+ for (j = 1; j <= mir->agg_vec->nnz; j++)
+ { k = mir->agg_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->isint[k]) continue; /* skip integer variable */
+ /* compute distance from x[k] to its lower bound */
+ kk = mir->vlb[k];
+ if (kk == 0)
+ { if (mir->lb[k] == -DBL_MAX)
+ d1 = DBL_MAX;
+ else
+ d1 = mir->x[k] - mir->lb[k];
+ }
+ else
+ { xassert(1 <= kk && kk <= m+n);
+ xassert(mir->isint[kk]);
+ xassert(mir->lb[k] != -DBL_MAX);
+ d1 = mir->x[k] - mir->lb[k] * mir->x[kk];
+ }
+ /* compute distance from x[k] to its upper bound */
+ kk = mir->vub[k];
+ if (kk == 0)
+ { if (mir->vub[k] == +DBL_MAX)
+ d2 = DBL_MAX;
+ else
+ d2 = mir->ub[k] - mir->x[k];
+ }
+ else
+ { xassert(1 <= kk && kk <= m+n);
+ xassert(mir->isint[kk]);
+ xassert(mir->ub[k] != +DBL_MAX);
+ d2 = mir->ub[k] * mir->x[kk] - mir->x[k];
+ }
+ /* x[k] cannot be free */
+ xassert(d1 != DBL_MAX || d2 != DBL_MAX);
+ /* choose the bound which is closer to x[k] */
+ xassert(mir->subst[k] == '?');
+ if (d1 <= d2)
+ mir->subst[k] = 'L';
+ else
+ mir->subst[k] = 'U';
+ }
+ return;
+}
+
+static void build_mod_row(glp_mir *mir)
+{ /* substitute bounds and build modified constraint */
+ int m = mir->m;
+ int n = mir->n;
+ int j, jj, k, kk;
+ /* initially modified constraint is aggregated constraint */
+ spv_copy_vec(mir->mod_vec, mir->agg_vec);
+ mir->mod_rhs = mir->agg_rhs;
+#if MIR_DEBUG
+ spv_check_vec(mir->mod_vec);
+#endif
+ /* substitute bounds for continuous variables; note that due to
+ substitution of variable bounds additional terms may appear in
+ modified constraint */
+ for (j = mir->mod_vec->nnz; j >= 1; j--)
+ { k = mir->mod_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->isint[k]) continue; /* skip integer variable */
+ if (mir->subst[k] == 'L')
+ { /* x[k] = (lower bound) + x'[k] */
+ xassert(mir->lb[k] != -DBL_MAX);
+ kk = mir->vlb[k];
+ if (kk == 0)
+ { /* x[k] = lb[k] + x'[k] */
+ mir->mod_rhs -= mir->mod_vec->val[j] * mir->lb[k];
+ }
+ else
+ { /* x[k] = lb[k] * x[kk] + x'[k] */
+ xassert(mir->isint[kk]);
+ jj = mir->mod_vec->pos[kk];
+ if (jj == 0)
+ { spv_set_vj(mir->mod_vec, kk, 1.0);
+ jj = mir->mod_vec->pos[kk];
+ mir->mod_vec->val[jj] = 0.0;
+ }
+ mir->mod_vec->val[jj] +=
+ mir->mod_vec->val[j] * mir->lb[k];
+ }
+ }
+ else if (mir->subst[k] == 'U')
+ { /* x[k] = (upper bound) - x'[k] */
+ xassert(mir->ub[k] != +DBL_MAX);
+ kk = mir->vub[k];
+ if (kk == 0)
+ { /* x[k] = ub[k] - x'[k] */
+ mir->mod_rhs -= mir->mod_vec->val[j] * mir->ub[k];
+ }
+ else
+ { /* x[k] = ub[k] * x[kk] - x'[k] */
+ xassert(mir->isint[kk]);
+ jj = mir->mod_vec->pos[kk];
+ if (jj == 0)
+ { spv_set_vj(mir->mod_vec, kk, 1.0);
+ jj = mir->mod_vec->pos[kk];
+ mir->mod_vec->val[jj] = 0.0;
+ }
+ mir->mod_vec->val[jj] +=
+ mir->mod_vec->val[j] * mir->ub[k];
+ }
+ mir->mod_vec->val[j] = - mir->mod_vec->val[j];
+ }
+ else
+ xassert(k != k);
+ }
+#if MIR_DEBUG
+ spv_check_vec(mir->mod_vec);
+#endif
+ /* substitute bounds for integer variables */
+ for (j = 1; j <= mir->mod_vec->nnz; j++)
+ { k = mir->mod_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (!mir->isint[k]) continue; /* skip continuous variable */
+ xassert(mir->subst[k] == '?');
+ xassert(mir->vlb[k] == 0 && mir->vub[k] == 0);
+ xassert(mir->lb[k] != -DBL_MAX && mir->ub[k] != +DBL_MAX);
+ if (fabs(mir->lb[k]) <= fabs(mir->ub[k]))
+ { /* x[k] = lb[k] + x'[k] */
+ mir->subst[k] = 'L';
+ mir->mod_rhs -= mir->mod_vec->val[j] * mir->lb[k];
+ }
+ else
+ { /* x[k] = ub[k] - x'[k] */
+ mir->subst[k] = 'U';
+ mir->mod_rhs -= mir->mod_vec->val[j] * mir->ub[k];
+ mir->mod_vec->val[j] = - mir->mod_vec->val[j];
+ }
+ }
+#if MIR_DEBUG
+ spv_check_vec(mir->mod_vec);
+#endif
+ return;
+}
+
+#if MIR_DEBUG
+static void check_mod_row(glp_mir *mir)
+{ /* check modified constraint */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k, kk;
+ double r, big, x;
+ /* compute the residual r = sum a'[k] * x'[k] - b' and determine
+ big = max(1, |a[k]|, |b|) */
+ r = 0.0, big = 1.0;
+ for (j = 1; j <= mir->mod_vec->nnz; j++)
+ { k = mir->mod_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->subst[k] == 'L')
+ { /* x'[k] = x[k] - (lower bound) */
+ xassert(mir->lb[k] != -DBL_MAX);
+ kk = mir->vlb[k];
+ if (kk == 0)
+ x = mir->x[k] - mir->lb[k];
+ else
+ x = mir->x[k] - mir->lb[k] * mir->x[kk];
+ }
+ else if (mir->subst[k] == 'U')
+ { /* x'[k] = (upper bound) - x[k] */
+ xassert(mir->ub[k] != +DBL_MAX);
+ kk = mir->vub[k];
+ if (kk == 0)
+ x = mir->ub[k] - mir->x[k];
+ else
+ x = mir->ub[k] * mir->x[kk] - mir->x[k];
+ }
+ else
+ xassert(k != k);
+ r += mir->mod_vec->val[j] * x;
+ if (big < fabs(mir->mod_vec->val[j]))
+ big = fabs(mir->mod_vec->val[j]);
+ }
+ r -= mir->mod_rhs;
+ if (big < fabs(mir->mod_rhs))
+ big = fabs(mir->mod_rhs);
+ /* the residual must be close to zero */
+ xassert(fabs(r) <= 1e-6 * big);
+ return;
+}
+#endif
+
+/***********************************************************************
+* mir_ineq - construct MIR inequality
+*
+* Given the single constraint mixed integer set
+*
+* |N|
+* X = {(x,s) in Z x R : sum a[j] * x[j] <= b + s},
+* + + j in N
+*
+* this routine constructs the mixed integer rounding (MIR) inequality
+*
+* sum alpha[j] * x[j] <= beta + gamma * s,
+* j in N
+*
+* which is valid for X.
+*
+* If the MIR inequality has been successfully constructed, the routine
+* returns zero. Otherwise, if b is close to nearest integer, there may
+* be numeric difficulties due to big coefficients; so in this case the
+* routine returns non-zero. */
+
+static int mir_ineq(const int n, const double a[], const double b,
+ double alpha[], double *beta, double *gamma)
+{ int j;
+ double f, t;
+ if (fabs(b - floor(b + .5)) < 0.01)
+ return 1;
+ f = b - floor(b);
+ for (j = 1; j <= n; j++)
+ { t = (a[j] - floor(a[j])) - f;
+ if (t <= 0.0)
+ alpha[j] = floor(a[j]);
+ else
+ alpha[j] = floor(a[j]) + t / (1.0 - f);
+ }
+ *beta = floor(b);
+ *gamma = 1.0 / (1.0 - f);
+ return 0;
+}
+
+/***********************************************************************
+* cmir_ineq - construct c-MIR inequality
+*
+* Given the mixed knapsack set
+*
+* MK |N|
+* X = {(x,s) in Z x R : sum a[j] * x[j] <= b + s,
+* + + j in N
+*
+* x[j] <= u[j]},
+*
+* a subset C of variables to be complemented, and a divisor delta > 0,
+* this routine constructs the complemented MIR (c-MIR) inequality
+*
+* sum alpha[j] * x[j] <= beta + gamma * s,
+* j in N
+* MK
+* which is valid for X .
+*
+* If the c-MIR inequality has been successfully constructed, the
+* routine returns zero. Otherwise, if there is a risk of numerical
+* difficulties due to big coefficients (see comments to the routine
+* mir_ineq), the routine cmir_ineq returns non-zero. */
+
+static int cmir_ineq(const int n, const double a[], const double b,
+ const double u[], const char cset[], const double delta,
+ double alpha[], double *beta, double *gamma)
+{ int j;
+ double *aa, bb;
+ aa = alpha, bb = b;
+ for (j = 1; j <= n; j++)
+ { aa[j] = a[j] / delta;
+ if (cset[j])
+ aa[j] = - aa[j], bb -= a[j] * u[j];
+ }
+ bb /= delta;
+ if (mir_ineq(n, aa, bb, alpha, beta, gamma)) return 1;
+ for (j = 1; j <= n; j++)
+ { if (cset[j])
+ alpha[j] = - alpha[j], *beta += alpha[j] * u[j];
+ }
+ *gamma /= delta;
+ return 0;
+}
+
+/***********************************************************************
+* cmir_sep - c-MIR separation heuristic
+*
+* Given the mixed knapsack set
+*
+* MK |N|
+* X = {(x,s) in Z x R : sum a[j] * x[j] <= b + s,
+* + + j in N
+*
+* x[j] <= u[j]}
+*
+* * *
+* and a fractional point (x , s ), this routine tries to construct
+* c-MIR inequality
+*
+* sum alpha[j] * x[j] <= beta + gamma * s,
+* j in N
+* MK
+* which is valid for X and has (desirably maximal) violation at the
+* fractional point given. This is attained by choosing an appropriate
+* set C of variables to be complemented and a divisor delta > 0, which
+* together define corresponding c-MIR inequality.
+*
+* If a violated c-MIR inequality has been successfully constructed,
+* the routine returns its violation:
+*
+* * *
+* sum alpha[j] * x [j] - beta - gamma * s ,
+* j in N
+*
+* which is positive. In case of failure the routine returns zero. */
+
+struct vset { int j; double v; };
+
+static int CDECL cmir_cmp(const void *p1, const void *p2)
+{ const struct vset *v1 = p1, *v2 = p2;
+ if (v1->v < v2->v) return -1;
+ if (v1->v > v2->v) return +1;
+ return 0;
+}
+
+static double cmir_sep(const int n, const double a[], const double b,
+ const double u[], const double x[], const double s,
+ double alpha[], double *beta, double *gamma)
+{ int fail, j, k, nv, v;
+ double delta, eps, d_try[1+3], r, r_best;
+ char *cset;
+ struct vset *vset;
+ /* allocate working arrays */
+ cset = xcalloc(1+n, sizeof(char));
+ vset = xcalloc(1+n, sizeof(struct vset));
+ /* choose initial C */
+ for (j = 1; j <= n; j++)
+ cset[j] = (char)(x[j] >= 0.5 * u[j]);
+ /* choose initial delta */
+ r_best = delta = 0.0;
+ for (j = 1; j <= n; j++)
+ { xassert(a[j] != 0.0);
+ /* if x[j] is close to its bounds, skip it */
+ eps = 1e-9 * (1.0 + fabs(u[j]));
+ if (x[j] < eps || x[j] > u[j] - eps) continue;
+ /* try delta = |a[j]| to construct c-MIR inequality */
+ fail = cmir_ineq(n, a, b, u, cset, fabs(a[j]), alpha, beta,
+ gamma);
+ if (fail) continue;
+ /* compute violation */
+ r = - (*beta) - (*gamma) * s;
+ for (k = 1; k <= n; k++) r += alpha[k] * x[k];
+ if (r_best < r) r_best = r, delta = fabs(a[j]);
+ }
+ if (r_best < 0.001) r_best = 0.0;
+ if (r_best == 0.0) goto done;
+ xassert(delta > 0.0);
+ /* try to increase violation by dividing delta by 2, 4, and 8,
+ respectively */
+ d_try[1] = delta / 2.0;
+ d_try[2] = delta / 4.0;
+ d_try[3] = delta / 8.0;
+ for (j = 1; j <= 3; j++)
+ { /* construct c-MIR inequality */
+ fail = cmir_ineq(n, a, b, u, cset, d_try[j], alpha, beta,
+ gamma);
+ if (fail) continue;
+ /* compute violation */
+ r = - (*beta) - (*gamma) * s;
+ for (k = 1; k <= n; k++) r += alpha[k] * x[k];
+ if (r_best < r) r_best = r, delta = d_try[j];
+ }
+ /* build subset of variables lying strictly between their bounds
+ and order it by nondecreasing values of |x[j] - u[j]/2| */
+ nv = 0;
+ for (j = 1; j <= n; j++)
+ { /* if x[j] is close to its bounds, skip it */
+ eps = 1e-9 * (1.0 + fabs(u[j]));
+ if (x[j] < eps || x[j] > u[j] - eps) continue;
+ /* add x[j] to the subset */
+ nv++;
+ vset[nv].j = j;
+ vset[nv].v = fabs(x[j] - 0.5 * u[j]);
+ }
+ qsort(&vset[1], nv, sizeof(struct vset), cmir_cmp);
+ /* try to increase violation by successively complementing each
+ variable in the subset */
+ for (v = 1; v <= nv; v++)
+ { j = vset[v].j;
+ /* replace x[j] by its complement or vice versa */
+ cset[j] = (char)!cset[j];
+ /* construct c-MIR inequality */
+ fail = cmir_ineq(n, a, b, u, cset, delta, alpha, beta, gamma);
+ /* restore the variable */
+ cset[j] = (char)!cset[j];
+ /* do not replace the variable in case of failure */
+ if (fail) continue;
+ /* compute violation */
+ r = - (*beta) - (*gamma) * s;
+ for (k = 1; k <= n; k++) r += alpha[k] * x[k];
+ if (r_best < r) r_best = r, cset[j] = (char)!cset[j];
+ }
+ /* construct the best c-MIR inequality chosen */
+ fail = cmir_ineq(n, a, b, u, cset, delta, alpha, beta, gamma);
+ xassert(!fail);
+done: /* free working arrays */
+ xfree(cset);
+ xfree(vset);
+ /* return to the calling routine */
+ return r_best;
+}
+
+static double generate(glp_mir *mir)
+{ /* try to generate violated c-MIR cut for modified constraint */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k, kk, nint;
+ double s, *u, *x, *alpha, r_best = 0.0, b, beta, gamma;
+ spv_copy_vec(mir->cut_vec, mir->mod_vec);
+ mir->cut_rhs = mir->mod_rhs;
+ /* remove small terms, which can appear due to substitution of
+ variable bounds */
+ spv_clean_vec(mir->cut_vec, DBL_EPSILON);
+#if MIR_DEBUG
+ spv_check_vec(mir->cut_vec);
+#endif
+ /* remove positive continuous terms to obtain MK relaxation */
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (!mir->isint[k] && mir->cut_vec->val[j] > 0.0)
+ mir->cut_vec->val[j] = 0.0;
+ }
+ spv_clean_vec(mir->cut_vec, 0.0);
+#if MIR_DEBUG
+ spv_check_vec(mir->cut_vec);
+#endif
+ /* move integer terms to the beginning of the sparse vector and
+ determine the number of integer variables */
+ nint = 0;
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->isint[k])
+ { double temp;
+ nint++;
+ /* interchange elements [nint] and [j] */
+ kk = mir->cut_vec->ind[nint];
+ mir->cut_vec->pos[k] = nint;
+ mir->cut_vec->pos[kk] = j;
+ mir->cut_vec->ind[nint] = k;
+ mir->cut_vec->ind[j] = kk;
+ temp = mir->cut_vec->val[nint];
+ mir->cut_vec->val[nint] = mir->cut_vec->val[j];
+ mir->cut_vec->val[j] = temp;
+ }
+ }
+#if MIR_DEBUG
+ spv_check_vec(mir->cut_vec);
+#endif
+ /* if there is no integer variable, nothing to generate */
+ if (nint == 0) goto done;
+ /* allocate working arrays */
+ u = xcalloc(1+nint, sizeof(double));
+ x = xcalloc(1+nint, sizeof(double));
+ alpha = xcalloc(1+nint, sizeof(double));
+ /* determine u and x */
+ for (j = 1; j <= nint; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(m+1 <= k && k <= m+n);
+ xassert(mir->isint[k]);
+ u[j] = mir->ub[k] - mir->lb[k];
+ xassert(u[j] >= 1.0);
+ if (mir->subst[k] == 'L')
+ x[j] = mir->x[k] - mir->lb[k];
+ else if (mir->subst[k] == 'U')
+ x[j] = mir->ub[k] - mir->x[k];
+ else
+ xassert(k != k);
+#if 0 /* 06/III-2016; notorious bug reported many times */
+ xassert(x[j] >= -0.001);
+#else
+ if (x[j] < -0.001)
+ { xprintf("glp_mir_gen: warning: x[%d] = %g\n", j, x[j]);
+ r_best = 0.0;
+ goto skip;
+ }
+#endif
+ if (x[j] < 0.0) x[j] = 0.0;
+ }
+ /* compute s = - sum of continuous terms */
+ s = 0.0;
+ for (j = nint+1; j <= mir->cut_vec->nnz; j++)
+ { double x;
+ k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ /* must be continuous */
+ xassert(!mir->isint[k]);
+ if (mir->subst[k] == 'L')
+ { xassert(mir->lb[k] != -DBL_MAX);
+ kk = mir->vlb[k];
+ if (kk == 0)
+ x = mir->x[k] - mir->lb[k];
+ else
+ x = mir->x[k] - mir->lb[k] * mir->x[kk];
+ }
+ else if (mir->subst[k] == 'U')
+ { xassert(mir->ub[k] != +DBL_MAX);
+ kk = mir->vub[k];
+ if (kk == 0)
+ x = mir->ub[k] - mir->x[k];
+ else
+ x = mir->ub[k] * mir->x[kk] - mir->x[k];
+ }
+ else
+ xassert(k != k);
+#if 0 /* 06/III-2016; notorious bug reported many times */
+ xassert(x >= -0.001);
+#else
+ if (x < -0.001)
+ { xprintf("glp_mir_gen: warning: x = %g\n", x);
+ r_best = 0.0;
+ goto skip;
+ }
+#endif
+ if (x < 0.0) x = 0.0;
+ s -= mir->cut_vec->val[j] * x;
+ }
+ xassert(s >= 0.0);
+ /* apply heuristic to obtain most violated c-MIR inequality */
+ b = mir->cut_rhs;
+ r_best = cmir_sep(nint, mir->cut_vec->val, b, u, x, s, alpha,
+ &beta, &gamma);
+ if (r_best == 0.0) goto skip;
+ xassert(r_best > 0.0);
+ /* convert to raw cut */
+ /* sum alpha[j] * x[j] <= beta + gamma * s */
+ for (j = 1; j <= nint; j++)
+ mir->cut_vec->val[j] = alpha[j];
+ for (j = nint+1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ if (k <= m+n) mir->cut_vec->val[j] *= gamma;
+ }
+ mir->cut_rhs = beta;
+#if MIR_DEBUG
+ spv_check_vec(mir->cut_vec);
+#endif
+skip: /* free working arrays */
+ xfree(u);
+ xfree(x);
+ xfree(alpha);
+done: return r_best;
+}
+
+#if MIR_DEBUG
+static void check_raw_cut(glp_mir *mir, double r_best)
+{ /* check raw cut before back bound substitution */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k, kk;
+ double r, big, x;
+ /* compute the residual r = sum a[k] * x[k] - b and determine
+ big = max(1, |a[k]|, |b|) */
+ r = 0.0, big = 1.0;
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->subst[k] == 'L')
+ { xassert(mir->lb[k] != -DBL_MAX);
+ kk = mir->vlb[k];
+ if (kk == 0)
+ x = mir->x[k] - mir->lb[k];
+ else
+ x = mir->x[k] - mir->lb[k] * mir->x[kk];
+ }
+ else if (mir->subst[k] == 'U')
+ { xassert(mir->ub[k] != +DBL_MAX);
+ kk = mir->vub[k];
+ if (kk == 0)
+ x = mir->ub[k] - mir->x[k];
+ else
+ x = mir->ub[k] * mir->x[kk] - mir->x[k];
+ }
+ else
+ xassert(k != k);
+ r += mir->cut_vec->val[j] * x;
+ if (big < fabs(mir->cut_vec->val[j]))
+ big = fabs(mir->cut_vec->val[j]);
+ }
+ r -= mir->cut_rhs;
+ if (big < fabs(mir->cut_rhs))
+ big = fabs(mir->cut_rhs);
+ /* the residual must be close to r_best */
+ xassert(fabs(r - r_best) <= 1e-6 * big);
+ return;
+}
+#endif
+
+static void back_subst(glp_mir *mir)
+{ /* back substitution of original bounds */
+ int m = mir->m;
+ int n = mir->n;
+ int j, jj, k, kk;
+ /* at first, restore bounds of integer variables (because on
+ restoring variable bounds of continuous variables we need
+ original, not shifted, bounds of integer variables) */
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (!mir->isint[k]) continue; /* skip continuous */
+ if (mir->subst[k] == 'L')
+ { /* x'[k] = x[k] - lb[k] */
+ xassert(mir->lb[k] != -DBL_MAX);
+ xassert(mir->vlb[k] == 0);
+ mir->cut_rhs += mir->cut_vec->val[j] * mir->lb[k];
+ }
+ else if (mir->subst[k] == 'U')
+ { /* x'[k] = ub[k] - x[k] */
+ xassert(mir->ub[k] != +DBL_MAX);
+ xassert(mir->vub[k] == 0);
+ mir->cut_rhs -= mir->cut_vec->val[j] * mir->ub[k];
+ mir->cut_vec->val[j] = - mir->cut_vec->val[j];
+ }
+ else
+ xassert(k != k);
+ }
+ /* now restore bounds of continuous variables */
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (mir->isint[k]) continue; /* skip integer */
+ if (mir->subst[k] == 'L')
+ { /* x'[k] = x[k] - (lower bound) */
+ xassert(mir->lb[k] != -DBL_MAX);
+ kk = mir->vlb[k];
+ if (kk == 0)
+ { /* x'[k] = x[k] - lb[k] */
+ mir->cut_rhs += mir->cut_vec->val[j] * mir->lb[k];
+ }
+ else
+ { /* x'[k] = x[k] - lb[k] * x[kk] */
+ jj = mir->cut_vec->pos[kk];
+#if 0
+ xassert(jj != 0);
+#else
+ if (jj == 0)
+ { spv_set_vj(mir->cut_vec, kk, 1.0);
+ jj = mir->cut_vec->pos[kk];
+ xassert(jj != 0);
+ mir->cut_vec->val[jj] = 0.0;
+ }
+#endif
+ mir->cut_vec->val[jj] -= mir->cut_vec->val[j] *
+ mir->lb[k];
+ }
+ }
+ else if (mir->subst[k] == 'U')
+ { /* x'[k] = (upper bound) - x[k] */
+ xassert(mir->ub[k] != +DBL_MAX);
+ kk = mir->vub[k];
+ if (kk == 0)
+ { /* x'[k] = ub[k] - x[k] */
+ mir->cut_rhs -= mir->cut_vec->val[j] * mir->ub[k];
+ }
+ else
+ { /* x'[k] = ub[k] * x[kk] - x[k] */
+ jj = mir->cut_vec->pos[kk];
+ if (jj == 0)
+ { spv_set_vj(mir->cut_vec, kk, 1.0);
+ jj = mir->cut_vec->pos[kk];
+ xassert(jj != 0);
+ mir->cut_vec->val[jj] = 0.0;
+ }
+ mir->cut_vec->val[jj] += mir->cut_vec->val[j] *
+ mir->ub[k];
+ }
+ mir->cut_vec->val[j] = - mir->cut_vec->val[j];
+ }
+ else
+ xassert(k != k);
+ }
+#if MIR_DEBUG
+ spv_check_vec(mir->cut_vec);
+#endif
+ return;
+}
+
+#if MIR_DEBUG
+static void check_cut_row(glp_mir *mir, double r_best)
+{ /* check the cut after back bound substitution or elimination of
+ auxiliary variables */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k;
+ double r, big;
+ /* compute the residual r = sum a[k] * x[k] - b and determine
+ big = max(1, |a[k]|, |b|) */
+ r = 0.0, big = 1.0;
+ for (j = 1; j <= mir->cut_vec->nnz; j++)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ r += mir->cut_vec->val[j] * mir->x[k];
+ if (big < fabs(mir->cut_vec->val[j]))
+ big = fabs(mir->cut_vec->val[j]);
+ }
+ r -= mir->cut_rhs;
+ if (big < fabs(mir->cut_rhs))
+ big = fabs(mir->cut_rhs);
+ /* the residual must be close to r_best */
+ xassert(fabs(r - r_best) <= 1e-6 * big);
+ return;
+}
+#endif
+
+static void subst_aux_vars(glp_prob *mip, glp_mir *mir)
+{ /* final substitution to eliminate auxiliary variables */
+ int m = mir->m;
+ int n = mir->n;
+ GLPAIJ *aij;
+ int j, k, kk, jj;
+ for (j = mir->cut_vec->nnz; j >= 1; j--)
+ { k = mir->cut_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (k > m) continue; /* skip structurals */
+ for (aij = mip->row[k]->ptr; aij != NULL; aij = aij->r_next)
+ { kk = m + aij->col->j; /* structural */
+ jj = mir->cut_vec->pos[kk];
+ if (jj == 0)
+ { spv_set_vj(mir->cut_vec, kk, 1.0);
+ jj = mir->cut_vec->pos[kk];
+ mir->cut_vec->val[jj] = 0.0;
+ }
+ mir->cut_vec->val[jj] += mir->cut_vec->val[j] * aij->val;
+ }
+ mir->cut_vec->val[j] = 0.0;
+ }
+ spv_clean_vec(mir->cut_vec, 0.0);
+ return;
+}
+
+static void add_cut(glp_mir *mir, glp_prob *pool)
+{ /* add constructed cut inequality to the cut pool */
+ int m = mir->m;
+ int n = mir->n;
+ int j, k, len;
+ int *ind = xcalloc(1+n, sizeof(int));
+ double *val = xcalloc(1+n, sizeof(double));
+ len = 0;
+ for (j = mir->cut_vec->nnz; j >= 1; j--)
+ { k = mir->cut_vec->ind[j];
+ xassert(m+1 <= k && k <= m+n);
+ len++, ind[len] = k - m, val[len] = mir->cut_vec->val[j];
+ }
+#if 0
+#if 0
+ ios_add_cut_row(tree, pool, GLP_RF_MIR, len, ind, val, GLP_UP,
+ mir->cut_rhs);
+#else
+ glp_ios_add_row(tree, NULL, GLP_RF_MIR, 0, len, ind, val, GLP_UP,
+ mir->cut_rhs);
+#endif
+#else
+ { int i;
+ i = glp_add_rows(pool, 1);
+ glp_set_row_bnds(pool, i, GLP_UP, 0, mir->cut_rhs);
+ glp_set_mat_row(pool, i, len, ind, val);
+ }
+#endif
+ xfree(ind);
+ xfree(val);
+ return;
+}
+
+#if 0 /* 29/II-2016 by Chris */
+static int aggregate_row(glp_prob *mip, glp_mir *mir)
+#else
+static int aggregate_row(glp_prob *mip, glp_mir *mir, SPV *v)
+#endif
+{ /* try to aggregate another row */
+ int m = mir->m;
+ int n = mir->n;
+ GLPAIJ *aij;
+#if 0 /* 29/II-2016 by Chris */
+ SPV *v;
+#endif
+ int ii, j, jj, k, kk, kappa = 0, ret = 0;
+ double d1, d2, d, d_max = 0.0;
+ /* choose appropriate structural variable in the aggregated row
+ to be substituted */
+ for (j = 1; j <= mir->agg_vec->nnz; j++)
+ { k = mir->agg_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ if (k <= m) continue; /* skip auxiliary var */
+ if (mir->isint[k]) continue; /* skip integer var */
+ if (fabs(mir->agg_vec->val[j]) < 0.001) continue;
+ /* compute distance from x[k] to its lower bound */
+ kk = mir->vlb[k];
+ if (kk == 0)
+ { if (mir->lb[k] == -DBL_MAX)
+ d1 = DBL_MAX;
+ else
+ d1 = mir->x[k] - mir->lb[k];
+ }
+ else
+ { xassert(1 <= kk && kk <= m+n);
+ xassert(mir->isint[kk]);
+ xassert(mir->lb[k] != -DBL_MAX);
+ d1 = mir->x[k] - mir->lb[k] * mir->x[kk];
+ }
+ /* compute distance from x[k] to its upper bound */
+ kk = mir->vub[k];
+ if (kk == 0)
+ { if (mir->vub[k] == +DBL_MAX)
+ d2 = DBL_MAX;
+ else
+ d2 = mir->ub[k] - mir->x[k];
+ }
+ else
+ { xassert(1 <= kk && kk <= m+n);
+ xassert(mir->isint[kk]);
+ xassert(mir->ub[k] != +DBL_MAX);
+ d2 = mir->ub[k] * mir->x[kk] - mir->x[k];
+ }
+ /* x[k] cannot be free */
+ xassert(d1 != DBL_MAX || d2 != DBL_MAX);
+ /* d = min(d1, d2) */
+ d = (d1 <= d2 ? d1 : d2);
+ xassert(d != DBL_MAX);
+ /* should not be close to corresponding bound */
+ if (d < 0.001) continue;
+ if (d_max < d) d_max = d, kappa = k;
+ }
+ if (kappa == 0)
+ { /* nothing chosen */
+ ret = 1;
+ goto done;
+ }
+ /* x[kappa] has been chosen */
+ xassert(m+1 <= kappa && kappa <= m+n);
+ xassert(!mir->isint[kappa]);
+ /* find another row, which have not been used yet, to eliminate
+ x[kappa] from the aggregated row */
+#if 0 /* 29/II-2016 by Chris */
+ for (ii = 1; ii <= m; ii++)
+ { if (mir->skip[ii]) continue;
+ for (aij = mip->row[ii]->ptr; aij != NULL; aij = aij->r_next)
+ if (aij->col->j == kappa - m) break;
+ if (aij != NULL && fabs(aij->val) >= 0.001) break;
+#else
+ ii = 0;
+ for (aij = mip->col[kappa - m]->ptr; aij != NULL;
+ aij = aij->c_next)
+ { if (aij->row->i > m) continue;
+ if (mir->skip[aij->row->i]) continue;
+ if (fabs(aij->val) >= 0.001)
+ { ii = aij->row->i;
+ break;
+ }
+#endif
+ }
+#if 0 /* 29/II-2016 by Chris */
+ if (ii > m)
+#else
+ if (ii == 0)
+#endif
+ { /* nothing found */
+ ret = 2;
+ goto done;
+ }
+ /* row ii has been found; include it in the aggregated list */
+ mir->agg_cnt++;
+ xassert(mir->agg_cnt <= MAXAGGR);
+ mir->agg_row[mir->agg_cnt] = ii;
+ mir->skip[ii] = 2;
+ /* v := new row */
+#if 0 /* 29/II-2016 by Chris */
+ v = ios_create_vec(m+n);
+#else
+ spv_clear_vec(v);
+#endif
+ spv_set_vj(v, ii, 1.0);
+ for (aij = mip->row[ii]->ptr; aij != NULL; aij = aij->r_next)
+ spv_set_vj(v, m + aij->col->j, - aij->val);
+#if MIR_DEBUG
+ spv_check_vec(v);
+#endif
+ /* perform gaussian elimination to remove x[kappa] */
+ j = mir->agg_vec->pos[kappa];
+ xassert(j != 0);
+ jj = v->pos[kappa];
+ xassert(jj != 0);
+ spv_linear_comb(mir->agg_vec,
+ - mir->agg_vec->val[j] / v->val[jj], v);
+#if 0 /* 29/II-2016 by Chris */
+ ios_delete_vec(v);
+#endif
+ spv_set_vj(mir->agg_vec, kappa, 0.0);
+#if MIR_DEBUG
+ spv_check_vec(mir->agg_vec);
+#endif
+done: return ret;
+}
+
+int glp_mir_gen(glp_prob *mip, glp_mir *mir, glp_prob *pool)
+{ /* main routine to generate MIR cuts */
+ int m = mir->m;
+ int n = mir->n;
+ int i, nnn = 0;
+ double r_best;
+#if 1 /* 29/II-2016 by Chris */
+ SPV *work;
+#endif
+ xassert(mip->m >= m);
+ xassert(mip->n == n);
+ /* obtain current point */
+ get_current_point(mip, mir);
+#if MIR_DEBUG
+ /* check current point */
+ check_current_point(mir);
+#endif
+ /* reset bound substitution flags */
+ memset(&mir->subst[1], '?', m+n);
+#if 1 /* 29/II-2016 by Chris */
+ work = spv_create_vec(m+n);
+#endif
+ /* try to generate a set of violated MIR cuts */
+ for (i = 1; i <= m; i++)
+ { if (mir->skip[i]) continue;
+ /* use original i-th row as initial aggregated constraint */
+ initial_agg_row(mip, mir, i);
+loop: ;
+#if MIR_DEBUG
+ /* check aggregated row */
+ check_agg_row(mir);
+#endif
+ /* substitute fixed variables into aggregated constraint */
+ subst_fixed_vars(mir);
+#if MIR_DEBUG
+ /* check aggregated row */
+ check_agg_row(mir);
+#endif
+#if MIR_DEBUG
+ /* check bound substitution flags */
+ { int k;
+ for (k = 1; k <= m+n; k++)
+ xassert(mir->subst[k] == '?');
+ }
+#endif
+ /* apply bound substitution heuristic */
+ bound_subst_heur(mir);
+ /* substitute bounds and build modified constraint */
+ build_mod_row(mir);
+#if MIR_DEBUG
+ /* check modified row */
+ check_mod_row(mir);
+#endif
+ /* try to generate violated c-MIR cut for modified row */
+ r_best = generate(mir);
+ if (r_best > 0.0)
+ { /* success */
+#if MIR_DEBUG
+ /* check raw cut before back bound substitution */
+ check_raw_cut(mir, r_best);
+#endif
+ /* back substitution of original bounds */
+ back_subst(mir);
+#if MIR_DEBUG
+ /* check the cut after back bound substitution */
+ check_cut_row(mir, r_best);
+#endif
+ /* final substitution to eliminate auxiliary variables */
+ subst_aux_vars(mip, mir);
+#if MIR_DEBUG
+ /* check the cut after elimination of auxiliaries */
+ check_cut_row(mir, r_best);
+#endif
+ /* add constructed cut inequality to the cut pool */
+ add_cut(mir, pool), nnn++;
+ }
+ /* reset bound substitution flags */
+ { int j, k;
+ for (j = 1; j <= mir->mod_vec->nnz; j++)
+ { k = mir->mod_vec->ind[j];
+ xassert(1 <= k && k <= m+n);
+ xassert(mir->subst[k] != '?');
+ mir->subst[k] = '?';
+ }
+ }
+ if (r_best == 0.0)
+ { /* failure */
+ if (mir->agg_cnt < MAXAGGR)
+ { /* try to aggregate another row */
+#if 0 /* 29/II-2016 by Chris */
+ if (aggregate_row(mip, mir) == 0) goto loop;
+#else
+ if (aggregate_row(mip, mir, work) == 0) goto loop;
+#endif
+ }
+ }
+ /* unmark rows used in the aggregated constraint */
+ { int k, ii;
+ for (k = 1; k <= mir->agg_cnt; k++)
+ { ii = mir->agg_row[k];
+ xassert(1 <= ii && ii <= m);
+ xassert(mir->skip[ii] == 2);
+ mir->skip[ii] = 0;
+ }
+ }
+ }
+#if 1 /* 29/II-2016 by Chris */
+ spv_delete_vec(work);
+#endif
+ return nnn;
+}
+
+/***********************************************************************
+* NAME
+*
+* glp_mir_free - delete MIR cut generator workspace
+*
+* SYNOPSIS
+*
+* void glp_mir_free(glp_mir *mir);
+*
+* DESCRIPTION
+*
+* This routine deletes the MIR cut generator workspace and frees all
+* the memory allocated to it. */
+
+void glp_mir_free(glp_mir *mir)
+{ xfree(mir->skip);
+ xfree(mir->isint);
+ xfree(mir->lb);
+ xfree(mir->vlb);
+ xfree(mir->ub);
+ xfree(mir->vub);
+ xfree(mir->x);
+ xfree(mir->agg_row);
+ spv_delete_vec(mir->agg_vec);
+ xfree(mir->subst);
+ spv_delete_vec(mir->mod_vec);
+ spv_delete_vec(mir->cut_vec);
+ xfree(mir);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/spv.c b/test/monniaux/glpk-4.65/src/intopt/spv.c
new file mode 100644
index 00000000..502f3cd0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/spv.c
@@ -0,0 +1,303 @@
+/* spv.c (operations on sparse vectors) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2007-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spv.h"
+
+/***********************************************************************
+* NAME
+*
+* spv_create_vec - create sparse vector
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* SPV *spv_create_vec(int n);
+*
+* DESCRIPTION
+*
+* The routine spv_create_vec creates a sparse vector of dimension n,
+* which initially is a null vector.
+*
+* RETURNS
+*
+* The routine returns a pointer to the vector created. */
+
+SPV *spv_create_vec(int n)
+{ SPV *v;
+ xassert(n >= 0);
+ v = xmalloc(sizeof(SPV));
+ v->n = n;
+ v->nnz = 0;
+ v->pos = xcalloc(1+n, sizeof(int));
+ memset(&v->pos[1], 0, n * sizeof(int));
+ v->ind = xcalloc(1+n, sizeof(int));
+ v->val = xcalloc(1+n, sizeof(double));
+ return v;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_check_vec - check that sparse vector has correct representation
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_check_vec(SPV *v);
+*
+* DESCRIPTION
+*
+* The routine spv_check_vec checks that a sparse vector specified by
+* the parameter v has correct representation.
+*
+* NOTE
+*
+* Complexity of this operation is O(n). */
+
+void spv_check_vec(SPV *v)
+{ int j, k, nnz;
+ xassert(v->n >= 0);
+ nnz = 0;
+ for (j = v->n; j >= 1; j--)
+ { k = v->pos[j];
+ xassert(0 <= k && k <= v->nnz);
+ if (k != 0)
+ { xassert(v->ind[k] == j);
+ nnz++;
+ }
+ }
+ xassert(v->nnz == nnz);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_get_vj - retrieve component of sparse vector
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* double spv_get_vj(SPV *v, int j);
+*
+* RETURNS
+*
+* The routine spv_get_vj returns j-th component of a sparse vector
+* specified by the parameter v. */
+
+double spv_get_vj(SPV *v, int j)
+{ int k;
+ xassert(1 <= j && j <= v->n);
+ k = v->pos[j];
+ xassert(0 <= k && k <= v->nnz);
+ return (k == 0 ? 0.0 : v->val[k]);
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_set_vj - set/change component of sparse vector
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_set_vj(SPV *v, int j, double val);
+*
+* DESCRIPTION
+*
+* The routine spv_set_vj assigns val to j-th component of a sparse
+* vector specified by the parameter v. */
+
+void spv_set_vj(SPV *v, int j, double val)
+{ int k;
+ xassert(1 <= j && j <= v->n);
+ k = v->pos[j];
+ if (val == 0.0)
+ { if (k != 0)
+ { /* remove j-th component */
+ v->pos[j] = 0;
+ if (k < v->nnz)
+ { v->pos[v->ind[v->nnz]] = k;
+ v->ind[k] = v->ind[v->nnz];
+ v->val[k] = v->val[v->nnz];
+ }
+ v->nnz--;
+ }
+ }
+ else
+ { if (k == 0)
+ { /* create j-th component */
+ k = ++(v->nnz);
+ v->pos[j] = k;
+ v->ind[k] = j;
+ }
+ v->val[k] = val;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_clear_vec - set all components of sparse vector to zero
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_clear_vec(SPV *v);
+*
+* DESCRIPTION
+*
+* The routine spv_clear_vec sets all components of a sparse vector
+* specified by the parameter v to zero. */
+
+void spv_clear_vec(SPV *v)
+{ int k;
+ for (k = 1; k <= v->nnz; k++)
+ v->pos[v->ind[k]] = 0;
+ v->nnz = 0;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_clean_vec - remove zero or small components from sparse vector
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_clean_vec(SPV *v, double eps);
+*
+* DESCRIPTION
+*
+* The routine spv_clean_vec removes zero components and components
+* whose magnitude is less than eps from a sparse vector specified by
+* the parameter v. If eps is 0.0, only zero components are removed. */
+
+void spv_clean_vec(SPV *v, double eps)
+{ int k, nnz;
+ nnz = 0;
+ for (k = 1; k <= v->nnz; k++)
+ { if (fabs(v->val[k]) == 0.0 || fabs(v->val[k]) < eps)
+ { /* remove component */
+ v->pos[v->ind[k]] = 0;
+ }
+ else
+ { /* keep component */
+ nnz++;
+ v->pos[v->ind[k]] = nnz;
+ v->ind[nnz] = v->ind[k];
+ v->val[nnz] = v->val[k];
+ }
+ }
+ v->nnz = nnz;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_copy_vec - copy sparse vector (x := y)
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_copy_vec(SPV *x, SPV *y);
+*
+* DESCRIPTION
+*
+* The routine spv_copy_vec copies a sparse vector specified by the
+* parameter y to a sparse vector specified by the parameter x. */
+
+void spv_copy_vec(SPV *x, SPV *y)
+{ int j;
+ xassert(x != y);
+ xassert(x->n == y->n);
+ spv_clear_vec(x);
+ x->nnz = y->nnz;
+ memcpy(&x->ind[1], &y->ind[1], x->nnz * sizeof(int));
+ memcpy(&x->val[1], &y->val[1], x->nnz * sizeof(double));
+ for (j = 1; j <= x->nnz; j++)
+ x->pos[x->ind[j]] = j;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_linear_comb - compute linear combination (x := x + a * y)
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_linear_comb(SPV *x, double a, SPV *y);
+*
+* DESCRIPTION
+*
+* The routine spv_linear_comb computes the linear combination
+*
+* x := x + a * y,
+*
+* where x and y are sparse vectors, a is a scalar. */
+
+void spv_linear_comb(SPV *x, double a, SPV *y)
+{ int j, k;
+ double xj, yj;
+ xassert(x != y);
+ xassert(x->n == y->n);
+ for (k = 1; k <= y->nnz; k++)
+ { j = y->ind[k];
+ xj = spv_get_vj(x, j);
+ yj = y->val[k];
+ spv_set_vj(x, j, xj + a * yj);
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* spv_delete_vec - delete sparse vector
+*
+* SYNOPSIS
+*
+* #include "glpios.h"
+* void spv_delete_vec(SPV *v);
+*
+* DESCRIPTION
+*
+* The routine spv_delete_vec deletes a sparse vector specified by the
+* parameter v freeing all the memory allocated to this object. */
+
+void spv_delete_vec(SPV *v)
+{ /* delete sparse vector */
+ xfree(v->pos);
+ xfree(v->ind);
+ xfree(v->val);
+ xfree(v);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/intopt/spv.h b/test/monniaux/glpk-4.65/src/intopt/spv.h
new file mode 100644
index 00000000..d7d4699f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/intopt/spv.h
@@ -0,0 +1,83 @@
+/* spv.h (operations on sparse vectors) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2007-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPV_H
+#define SPV_H
+
+typedef struct SPV SPV;
+
+struct SPV
+{ /* sparse vector v = (v[j]) */
+ int n;
+ /* dimension, n >= 0 */
+ int nnz;
+ /* number of non-zero components, 0 <= nnz <= n */
+ int *pos; /* int pos[1+n]; */
+ /* pos[j] = k, 1 <= j <= n, is position of (non-zero) v[j] in the
+ * arrays ind and val, where 1 <= k <= nnz; pos[j] = 0 means that
+ * v[j] is structural zero */
+ int *ind; /* int ind[1+n]; */
+ /* ind[k] = j, 1 <= k <= nnz, is index of v[j] */
+ double *val; /* double val[1+n]; */
+ /* val[k], 1 <= k <= nnz, is a numeric value of v[j] */
+};
+
+#define spv_create_vec _glp_spv_create_vec
+SPV *spv_create_vec(int n);
+/* create sparse vector */
+
+#define spv_check_vec _glp_spv_check_vec
+void spv_check_vec(SPV *v);
+/* check that sparse vector has correct representation */
+
+#define spv_get_vj _glp_spv_get_vj
+double spv_get_vj(SPV *v, int j);
+/* retrieve component of sparse vector */
+
+#define spv_set_vj _glp_spv_set_vj
+void spv_set_vj(SPV *v, int j, double val);
+/* set/change component of sparse vector */
+
+#define spv_clear_vec _glp_spv_clear_vec
+void spv_clear_vec(SPV *v);
+/* set all components of sparse vector to zero */
+
+#define spv_clean_vec _glp_spv_clean_vec
+void spv_clean_vec(SPV *v, double eps);
+/* remove zero or small components from sparse vector */
+
+#define spv_copy_vec _glp_spv_copy_vec
+void spv_copy_vec(SPV *x, SPV *y);
+/* copy sparse vector (x := y) */
+
+#define spv_linear_comb _glp_spv_linear_comb
+void spv_linear_comb(SPV *x, double a, SPV *y);
+/* compute linear combination (x := x + a * y) */
+
+#define spv_delete_vec _glp_spv_delete_vec
+void spv_delete_vec(SPV *v);
+/* delete sparse vector */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/minisat/LICENSE b/test/monniaux/glpk-4.65/src/minisat/LICENSE
new file mode 100644
index 00000000..8a6b9f36
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/minisat/LICENSE
@@ -0,0 +1,20 @@
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/test/monniaux/glpk-4.65/src/minisat/README b/test/monniaux/glpk-4.65/src/minisat/README
new file mode 100644
index 00000000..c183c7d6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/minisat/README
@@ -0,0 +1,22 @@
+NOTE: Files in this subdirectory are NOT part of the GLPK package, but
+ are used with GLPK.
+
+ The original code was modified according to GLPK requirements by
+ Andrew Makhorin <mao@gnu.org>.
+************************************************************************
+MiniSat-C v1.14.1
+========================================
+
+* Fixed some serious bugs.
+* Tweaked to be Visual Studio friendly (by Alan Mishchenko).
+ This disabled reading of gzipped DIMACS files and signal handling,
+ but none of these features are essential (and easy to re-enable, if
+ wanted).
+
+MiniSat-C v1.14
+========================================
+
+Ok, we get it. You hate C++. You hate templates. We agree; C++ is a
+seriously messed up language. Although we are more pragmatic about the
+quirks and maldesigns in C++, we sympathize with you. So here is a
+pure C version of MiniSat, put together by Niklas Sorensson.
diff --git a/test/monniaux/glpk-4.65/src/minisat/minisat.c b/test/monniaux/glpk-4.65/src/minisat/minisat.c
new file mode 100644
index 00000000..2432d650
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/minisat/minisat.c
@@ -0,0 +1,1315 @@
+/* minisat.c */
+
+/* Modified by Andrew Makhorin <mao@gnu.org>, August 2011 */
+/* May 2017: Changes were made to provide 64-bit portability; thanks to
+ * Chris Matrakidis <cmatraki@gmail.com> for patch */
+
+/***********************************************************************
+* MiniSat -- Copyright (c) 2005, Niklas Sorensson
+* http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+*
+* Permission is hereby granted, free of charge, to any person
+* obtaining a copy of this software and associated documentation files
+* (the "Software"), to deal in the Software without restriction,
+* including without limitation the rights to use, copy, modify, merge,
+* publish, distribute, sublicense, and/or sell copies of the Software,
+* and to permit persons to whom the Software is furnished to do so,
+* subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+* SOFTWARE.
+***********************************************************************/
+/* Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko */
+
+#include "env.h"
+#include "minisat.h"
+
+#if 1 /* by mao */
+static void *ymalloc(int size)
+{ void *ptr;
+ xassert(size > 0);
+ ptr = malloc(size);
+ if (ptr == NULL)
+ xerror("MiniSat: no memory available\n");
+ return ptr;
+}
+
+static void *yrealloc(void *ptr, int size)
+{ xassert(size > 0);
+ if (ptr == NULL)
+ ptr = malloc(size);
+ else
+ ptr = realloc(ptr, size);
+ if (ptr == NULL)
+ xerror("MiniSat: no memory available\n");
+ return ptr;
+}
+
+static void yfree(void *ptr)
+{ xassert(ptr != NULL);
+ free(ptr);
+ return;
+}
+
+#define assert xassert
+#define printf xprintf
+#define fflush(f) /* nop */
+#define malloc ymalloc
+#define realloc yrealloc
+#define free yfree
+#define inline /* empty */
+#endif
+
+/*====================================================================*/
+/* Debug: */
+
+#if 0
+#define VERBOSEDEBUG 1
+#endif
+
+/* For derivation output (verbosity level 2) */
+#define L_IND "%-*d"
+#define L_ind solver_dlevel(s)*3+3,solver_dlevel(s)
+#define L_LIT "%sx%d"
+#define L_lit(p) lit_sign(p)?"~":"", (lit_var(p))
+
+#if 0 /* by mao */
+/* Just like 'assert()' but expression will be evaluated in the release
+ version as well. */
+static inline void check(int expr) { assert(expr); }
+#endif
+
+#if 0 /* by mao */
+static void printlits(lit* begin, lit* end)
+{
+ int i;
+ for (i = 0; i < end - begin; i++)
+ printf(L_LIT" ",L_lit(begin[i]));
+}
+#endif
+
+/*====================================================================*/
+/* Random numbers: */
+
+/* Returns a random float 0 <= x < 1. Seed must never be 0. */
+static inline double drand(double* seed) {
+ int q;
+ *seed *= 1389796;
+ q = (int)(*seed / 2147483647);
+ *seed -= (double)q * 2147483647;
+ return *seed / 2147483647; }
+
+/* Returns a random integer 0 <= x < size. Seed must never be 0. */
+static inline int irand(double* seed, int size) {
+ return (int)(drand(seed) * size); }
+
+/*====================================================================*/
+/* Predeclarations: */
+
+static void sort(void** array, int size,
+ int(*comp)(const void *, const void *));
+
+/*====================================================================*/
+/* Clause datatype + minor functions: */
+
+#if 0 /* by mao; see minisat.h */
+struct clause_t
+{
+ int size_learnt;
+ lit lits[0];
+};
+#endif
+
+#define clause_size(c) ((c)->size_learnt >> 1)
+
+#define clause_begin(c) ((c)->lits)
+
+#define clause_learnt(c) ((c)->size_learnt & 1)
+
+#define clause_activity(c) \
+ (*((float*)&(c)->lits[(c)->size_learnt>>1]))
+
+#define clause_setactivity(c, a) \
+ (void)(*((float*)&(c)->lits[(c)->size_learnt>>1]) = (a))
+
+/*====================================================================*/
+/* Encode literals in clause pointers: */
+
+#if 0 /* 8/I-2017 by cmatraki (64-bit portability) */
+#define clause_from_lit(l) \
+ (clause*)((unsigned long)(l) + (unsigned long)(l) + 1)
+
+#define clause_is_lit(c) \
+ ((unsigned long)(c) & 1)
+
+#define clause_read_lit(c) \
+ (lit)((unsigned long)(c) >> 1)
+#else
+#define clause_from_lit(l) \
+ (clause*)((size_t)(l) + (size_t)(l) + 1)
+
+#define clause_is_lit(c) \
+ ((size_t)(c) & 1)
+
+#define clause_read_lit(c) \
+ (lit)((size_t)(c) >> 1)
+#endif
+
+/*====================================================================*/
+/* Simple helpers: */
+
+#define solver_dlevel(s) \
+ (int)veci_size(&(s)->trail_lim)
+
+#define solver_read_wlist(s, l) \
+ (vecp *)(&(s)->wlists[l])
+
+static inline void vecp_remove(vecp* v, void* e)
+{
+ void** ws = vecp_begin(v);
+ int j = 0;
+
+ for (; ws[j] != e ; j++);
+ assert(j < vecp_size(v));
+ for (; j < vecp_size(v)-1; j++) ws[j] = ws[j+1];
+ vecp_resize(v,vecp_size(v)-1);
+}
+
+/*====================================================================*/
+/* Variable order functions: */
+
+static inline void order_update(solver* s, int v)
+{ /* updateorder */
+ int* orderpos = s->orderpos;
+ double* activity = s->activity;
+ int* heap = veci_begin(&s->order);
+ int i = orderpos[v];
+ int x = heap[i];
+ int parent = (i - 1) / 2;
+
+ assert(s->orderpos[v] != -1);
+
+ while (i != 0 && activity[x] > activity[heap[parent]]){
+ heap[i] = heap[parent];
+ orderpos[heap[i]] = i;
+ i = parent;
+ parent = (i - 1) / 2;
+ }
+ heap[i] = x;
+ orderpos[x] = i;
+}
+
+#define order_assigned(s, v) /* nop */
+
+static inline void order_unassigned(solver* s, int v)
+{ /* undoorder */
+ int* orderpos = s->orderpos;
+ if (orderpos[v] == -1){
+ orderpos[v] = veci_size(&s->order);
+ veci_push(&s->order,v);
+ order_update(s,v);
+ }
+}
+
+static int order_select(solver* s, float random_var_freq)
+{ /* selectvar */
+ int* heap;
+ double* activity;
+ int* orderpos;
+
+ lbool* values = s->assigns;
+
+ /* Random decision: */
+ if (drand(&s->random_seed) < random_var_freq){
+ int next = irand(&s->random_seed,s->size);
+ assert(next >= 0 && next < s->size);
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ /* Activity based decision: */
+
+ heap = veci_begin(&s->order);
+ activity = s->activity;
+ orderpos = s->orderpos;
+
+ while (veci_size(&s->order) > 0){
+ int next = heap[0];
+ int size = veci_size(&s->order)-1;
+ int x = heap[size];
+
+ veci_resize(&s->order,size);
+
+ orderpos[next] = -1;
+
+ if (size > 0){
+ double act = activity[x];
+
+ int i = 0;
+ int child = 1;
+
+ while (child < size){
+ if (child+1 < size
+ && activity[heap[child]] < activity[heap[child+1]])
+ child++;
+
+ assert(child < size);
+
+ if (act >= activity[heap[child]])
+ break;
+
+ heap[i] = heap[child];
+ orderpos[heap[i]] = i;
+ i = child;
+ child = 2 * child + 1;
+ }
+ heap[i] = x;
+ orderpos[heap[i]] = i;
+ }
+
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ return var_Undef;
+}
+
+/*====================================================================*/
+/* Activity functions: */
+
+static inline void act_var_rescale(solver* s) {
+ double* activity = s->activity;
+ int i;
+ for (i = 0; i < s->size; i++)
+ activity[i] *= 1e-100;
+ s->var_inc *= 1e-100;
+}
+
+static inline void act_var_bump(solver* s, int v) {
+ double* activity = s->activity;
+ if ((activity[v] += s->var_inc) > 1e100)
+ act_var_rescale(s);
+
+ /* printf("bump %d %f\n", v-1, activity[v]); */
+
+ if (s->orderpos[v] != -1)
+ order_update(s,v);
+
+}
+
+static inline void act_var_decay(solver* s)
+ { s->var_inc *= s->var_decay; }
+
+static inline void act_clause_rescale(solver* s) {
+ clause** cs = (clause**)vecp_begin(&s->learnts);
+ int i;
+ for (i = 0; i < vecp_size(&s->learnts); i++){
+ float a = clause_activity(cs[i]);
+ clause_setactivity(cs[i], a * (float)1e-20);
+ }
+ s->cla_inc *= (float)1e-20;
+}
+
+static inline void act_clause_bump(solver* s, clause *c) {
+ float a = clause_activity(c) + s->cla_inc;
+ clause_setactivity(c,a);
+ if (a > 1e20) act_clause_rescale(s);
+}
+
+static inline void act_clause_decay(solver* s)
+ { s->cla_inc *= s->cla_decay; }
+
+/*====================================================================*/
+/* Clause functions: */
+
+/* pre: size > 1 && no variable occurs twice
+ */
+static clause* clause_new(solver* s, lit* begin, lit* end, int learnt)
+{
+ int size;
+ clause* c;
+ int i;
+
+ assert(end - begin > 1);
+ assert(learnt >= 0 && learnt < 2);
+ size = end - begin;
+ c = (clause*)malloc(sizeof(clause)
+ + sizeof(lit) * size + learnt * sizeof(float));
+ c->size_learnt = (size << 1) | learnt;
+#if 1 /* by mao & cmatraki; non-portable check that is a fundamental \
+ * assumption of minisat code: bit 0 is used as a flag (zero \
+ * for pointer, one for shifted int) so allocated memory should \
+ * be at least 16-bit aligned */
+ assert(((size_t)c & 1) == 0);
+#endif
+
+ for (i = 0; i < size; i++)
+ c->lits[i] = begin[i];
+
+ if (learnt)
+ *((float*)&c->lits[size]) = 0.0;
+
+ assert(begin[0] >= 0);
+ assert(begin[0] < s->size*2);
+ assert(begin[1] >= 0);
+ assert(begin[1] < s->size*2);
+
+ assert(lit_neg(begin[0]) < s->size*2);
+ assert(lit_neg(begin[1]) < s->size*2);
+
+ /* vecp_push(solver_read_wlist(s,lit_neg(begin[0])),(void*)c); */
+ /* vecp_push(solver_read_wlist(s,lit_neg(begin[1])),(void*)c); */
+
+ vecp_push(solver_read_wlist(s,lit_neg(begin[0])),
+ (void*)(size > 2 ? c : clause_from_lit(begin[1])));
+ vecp_push(solver_read_wlist(s,lit_neg(begin[1])),
+ (void*)(size > 2 ? c : clause_from_lit(begin[0])));
+
+ return c;
+}
+
+static void clause_remove(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ assert(lit_neg(lits[0]) < s->size*2);
+ assert(lit_neg(lits[1]) < s->size*2);
+
+ /* vecp_remove(solver_read_wlist(s,lit_neg(lits[0])),(void*)c); */
+ /* vecp_remove(solver_read_wlist(s,lit_neg(lits[1])),(void*)c); */
+
+ assert(lits[0] < s->size*2);
+ vecp_remove(solver_read_wlist(s,lit_neg(lits[0])),
+ (void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[1])));
+ vecp_remove(solver_read_wlist(s,lit_neg(lits[1])),
+ (void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[0])));
+
+ if (clause_learnt(c)){
+ s->stats.learnts--;
+ s->stats.learnts_literals -= clause_size(c);
+ }else{
+ s->stats.clauses--;
+ s->stats.clauses_literals -= clause_size(c);
+ }
+
+ free(c);
+}
+
+static lbool clause_simplify(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ lbool* values = s->assigns;
+ int i;
+
+ assert(solver_dlevel(s) == 0);
+
+ for (i = 0; i < clause_size(c); i++){
+ lbool sig = !lit_sign(lits[i]); sig += sig - 1;
+ if (values[lit_var(lits[i])] == sig)
+ return l_True;
+ }
+ return l_False;
+}
+
+/*====================================================================*/
+/* Minor (solver) functions: */
+
+void solver_setnvars(solver* s,int n)
+{
+ int var;
+
+ if (s->cap < n){
+
+ while (s->cap < n) s->cap = s->cap*2+1;
+
+ s->wlists = (vecp*) realloc(s->wlists,
+ sizeof(vecp)*s->cap*2);
+ s->activity = (double*) realloc(s->activity,
+ sizeof(double)*s->cap);
+ s->assigns = (lbool*) realloc(s->assigns,
+ sizeof(lbool)*s->cap);
+ s->orderpos = (int*) realloc(s->orderpos,
+ sizeof(int)*s->cap);
+ s->reasons = (clause**)realloc(s->reasons,
+ sizeof(clause*)*s->cap);
+ s->levels = (int*) realloc(s->levels,
+ sizeof(int)*s->cap);
+ s->tags = (lbool*) realloc(s->tags,
+ sizeof(lbool)*s->cap);
+ s->trail = (lit*) realloc(s->trail,
+ sizeof(lit)*s->cap);
+ }
+
+ for (var = s->size; var < n; var++){
+ vecp_new(&s->wlists[2*var]);
+ vecp_new(&s->wlists[2*var+1]);
+ s->activity [var] = 0;
+ s->assigns [var] = l_Undef;
+ s->orderpos [var] = veci_size(&s->order);
+ s->reasons [var] = (clause*)0;
+ s->levels [var] = 0;
+ s->tags [var] = l_Undef;
+
+ /* does not hold because variables enqueued at top level will
+ not be reinserted in the heap
+ assert(veci_size(&s->order) == var);
+ */
+ veci_push(&s->order,var);
+ order_update(s, var);
+ }
+
+ s->size = n > s->size ? n : s->size;
+}
+
+static inline bool enqueue(solver* s, lit l, clause* from)
+{
+ lbool* values = s->assigns;
+ int v = lit_var(l);
+ lbool val = values[v];
+ lbool sig;
+#ifdef VERBOSEDEBUG
+ printf(L_IND"enqueue("L_LIT")\n", L_ind, L_lit(l));
+#endif
+
+ /* lbool */ sig = !lit_sign(l); sig += sig - 1;
+ if (val != l_Undef){
+ return val == sig;
+ }else{
+ /* New fact -- store it. */
+ int* levels;
+ clause** reasons;
+#ifdef VERBOSEDEBUG
+ printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ /* int* */ levels = s->levels;
+ /* clause** */ reasons = s->reasons;
+
+ values [v] = sig;
+ levels [v] = solver_dlevel(s);
+ reasons[v] = from;
+ s->trail[s->qtail++] = l;
+
+ order_assigned(s, v);
+ return true;
+ }
+}
+
+static inline void assume(solver* s, lit l){
+ assert(s->qtail == s->qhead);
+ assert(s->assigns[lit_var(l)] == l_Undef);
+#ifdef VERBOSEDEBUG
+ printf(L_IND"assume("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ veci_push(&s->trail_lim,s->qtail);
+ enqueue(s,l,(clause*)0);
+}
+
+static inline void solver_canceluntil(solver* s, int level) {
+ lit* trail;
+ lbool* values;
+ clause** reasons;
+ int bound;
+ int c;
+
+ if (solver_dlevel(s) <= level)
+ return;
+
+ trail = s->trail;
+ values = s->assigns;
+ reasons = s->reasons;
+ bound = (veci_begin(&s->trail_lim))[level];
+
+ for (c = s->qtail-1; c >= bound; c--) {
+ int x = lit_var(trail[c]);
+ values [x] = l_Undef;
+ reasons[x] = (clause*)0;
+ }
+
+ for (c = s->qhead-1; c >= bound; c--)
+ order_unassigned(s,lit_var(trail[c]));
+
+ s->qhead = s->qtail = bound;
+ veci_resize(&s->trail_lim,level);
+}
+
+static void solver_record(solver* s, veci* cls)
+{
+ lit* begin = veci_begin(cls);
+ lit* end = begin + veci_size(cls);
+ clause* c = (veci_size(cls) > 1) ? clause_new(s,begin,end,1)
+ : (clause*)0;
+ enqueue(s,*begin,c);
+
+ assert(veci_size(cls) > 0);
+
+ if (c != 0) {
+ vecp_push(&s->learnts,c);
+ act_clause_bump(s,c);
+ s->stats.learnts++;
+ s->stats.learnts_literals += veci_size(cls);
+ }
+}
+
+static double solver_progress(solver* s)
+{
+ lbool* values = s->assigns;
+ int* levels = s->levels;
+ int i;
+
+ double progress = 0;
+ double F = 1.0 / s->size;
+ for (i = 0; i < s->size; i++)
+ if (values[i] != l_Undef)
+ progress += pow(F, levels[i]);
+ return progress / s->size;
+}
+
+/*====================================================================*/
+/* Major methods: */
+
+static bool solver_lit_removable(solver* s, lit l, int minl)
+{
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int top = veci_size(&s->tagged);
+
+ assert(lit_var(l) >= 0 && lit_var(l) < s->size);
+ assert(reasons[lit_var(l)] != 0);
+ veci_resize(&s->stack,0);
+ veci_push(&s->stack,lit_var(l));
+
+ while (veci_size(&s->stack) > 0){
+ clause* c;
+ int v = veci_begin(&s->stack)[veci_size(&s->stack)-1];
+ assert(v >= 0 && v < s->size);
+ veci_resize(&s->stack,veci_size(&s->stack)-1);
+ assert(reasons[v] != 0);
+ c = reasons[v];
+
+ if (clause_is_lit(c)){
+ int v = lit_var(clause_read_lit(c));
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0
+ && ((1 << (levels[v] & 31)) & minl)){
+ veci_push(&s->stack,v);
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ int j;
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }else{
+ lit* lits = clause_begin(c);
+ int i, j;
+
+ for (i = 1; i < clause_size(c); i++){
+ int v = lit_var(lits[i]);
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0
+ && ((1 << (levels[v] & 31)) & minl)){
+
+ veci_push(&s->stack,lit_var(lits[i]));
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+static void solver_analyze(solver* s, clause* c, veci* learnt)
+{
+ lit* trail = s->trail;
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int cnt = 0;
+ lit p = lit_Undef;
+ int ind = s->qtail-1;
+ lit* lits;
+ int i, j, minl;
+ int* tagged;
+
+ veci_push(learnt,lit_Undef);
+
+ do{
+ assert(c != 0);
+
+ if (clause_is_lit(c)){
+ lit q = clause_read_lit(c);
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }else{
+
+ if (clause_learnt(c))
+ act_clause_bump(s,c);
+
+ lits = clause_begin(c);
+ /* printlits(lits,lits+clause_size(c)); printf("\n"); */
+ for (j = (p == lit_Undef ? 0 : 1); j < clause_size(c); j++){
+ lit q = lits[j];
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef
+ && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }
+ }
+
+ while (tags[lit_var(trail[ind--])] == l_Undef);
+
+ p = trail[ind+1];
+ c = reasons[lit_var(p)];
+ cnt--;
+
+ }while (cnt > 0);
+
+ *veci_begin(learnt) = lit_neg(p);
+
+ lits = veci_begin(learnt);
+ minl = 0;
+ for (i = 1; i < veci_size(learnt); i++){
+ int lev = levels[lit_var(lits[i])];
+ minl |= 1 << (lev & 31);
+ }
+
+ /* simplify (full) */
+ for (i = j = 1; i < veci_size(learnt); i++){
+ if (reasons[lit_var(lits[i])] == 0
+ || !solver_lit_removable(s,lits[i],minl))
+ lits[j++] = lits[i];
+ }
+
+ /* update size of learnt + statistics */
+ s->stats.max_literals += veci_size(learnt);
+ veci_resize(learnt,j);
+ s->stats.tot_literals += j;
+
+ /* clear tags */
+ tagged = veci_begin(&s->tagged);
+ for (i = 0; i < veci_size(&s->tagged); i++)
+ tags[tagged[i]] = l_Undef;
+ veci_resize(&s->tagged,0);
+
+#ifdef DEBUG
+ for (i = 0; i < s->size; i++)
+ assert(tags[i] == l_Undef);
+#endif
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"Learnt {", L_ind);
+ for (i = 0; i < veci_size(learnt); i++)
+ printf(" "L_LIT, L_lit(lits[i]));
+#endif
+ if (veci_size(learnt) > 1){
+ int max_i = 1;
+ int max = levels[lit_var(lits[1])];
+ lit tmp;
+
+ for (i = 2; i < veci_size(learnt); i++)
+ if (levels[lit_var(lits[i])] > max){
+ max = levels[lit_var(lits[i])];
+ max_i = i;
+ }
+
+ tmp = lits[1];
+ lits[1] = lits[max_i];
+ lits[max_i] = tmp;
+ }
+#ifdef VERBOSEDEBUG
+ {
+ int lev = veci_size(learnt) > 1 ? levels[lit_var(lits[1])] : 0;
+ printf(" } at level %d\n", lev);
+ }
+#endif
+}
+
+clause* solver_propagate(solver* s)
+{
+ lbool* values = s->assigns;
+ clause* confl = (clause*)0;
+ lit* lits;
+
+ /* printf("solver_propagate\n"); */
+ while (confl == 0 && s->qtail - s->qhead > 0){
+ lit p = s->trail[s->qhead++];
+ vecp* ws = solver_read_wlist(s,p);
+ clause **begin = (clause**)vecp_begin(ws);
+ clause **end = begin + vecp_size(ws);
+ clause **i, **j;
+
+ s->stats.propagations++;
+ s->simpdb_props--;
+
+ /* printf("checking lit %d: "L_LIT"\n", veci_size(ws),
+ L_lit(p)); */
+ for (i = j = begin; i < end; ){
+ if (clause_is_lit(*i)){
+ *j++ = *i;
+ if (!enqueue(s,clause_read_lit(*i),clause_from_lit(p))){
+ confl = s->binary;
+ (clause_begin(confl))[1] = lit_neg(p);
+ (clause_begin(confl))[0] = clause_read_lit(*i++);
+
+ /* Copy the remaining watches: */
+ while (i < end)
+ *j++ = *i++;
+ }
+ }else{
+ lit false_lit;
+ lbool sig;
+
+ lits = clause_begin(*i);
+
+ /* Make sure the false literal is data[1]: */
+ false_lit = lit_neg(p);
+ if (lits[0] == false_lit){
+ lits[0] = lits[1];
+ lits[1] = false_lit;
+ }
+ assert(lits[1] == false_lit);
+ /* printf("checking clause: ");
+ printlits(lits, lits+clause_size(*i));
+ printf("\n"); */
+
+ /* If 0th watch is true, then clause is already
+ satisfied. */
+ sig = !lit_sign(lits[0]); sig += sig - 1;
+ if (values[lit_var(lits[0])] == sig){
+ *j++ = *i;
+ }else{
+ /* Look for new watch: */
+ lit* stop = lits + clause_size(*i);
+ lit* k;
+ for (k = lits + 2; k < stop; k++){
+ lbool sig = lit_sign(*k); sig += sig - 1;
+ if (values[lit_var(*k)] != sig){
+ lits[1] = *k;
+ *k = false_lit;
+ vecp_push(solver_read_wlist(s,
+ lit_neg(lits[1])),*i);
+ goto next; }
+ }
+
+ *j++ = *i;
+ /* Clause is unit under assignment: */
+ if (!enqueue(s,lits[0], *i)){
+ confl = *i++;
+ /* Copy the remaining watches: */
+ while (i < end)
+ *j++ = *i++;
+ }
+ }
+ }
+ next:
+ i++;
+ }
+
+ s->stats.inspects += j - (clause**)vecp_begin(ws);
+ vecp_resize(ws,j - (clause**)vecp_begin(ws));
+ }
+
+ return confl;
+}
+
+static inline int clause_cmp (const void* x, const void* y) {
+ return clause_size((clause*)x) > 2
+ && (clause_size((clause*)y) == 2
+ || clause_activity((clause*)x)
+ < clause_activity((clause*)y)) ? -1 : 1; }
+
+void solver_reducedb(solver* s)
+{
+ int i, j;
+ double extra_lim = s->cla_inc / vecp_size(&s->learnts);
+ /* Remove any clause below this activity */
+ clause** learnts = (clause**)vecp_begin(&s->learnts);
+ clause** reasons = s->reasons;
+
+ sort(vecp_begin(&s->learnts), vecp_size(&s->learnts), clause_cmp);
+
+ for (i = j = 0; i < vecp_size(&s->learnts) / 2; i++){
+ if (clause_size(learnts[i]) > 2
+ && reasons[lit_var(*clause_begin(learnts[i]))]
+ != learnts[i])
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+ for (; i < vecp_size(&s->learnts); i++){
+ if (clause_size(learnts[i]) > 2
+ && reasons[lit_var(*clause_begin(learnts[i]))]
+ != learnts[i]
+ && clause_activity(learnts[i]) < extra_lim)
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+
+ /* printf("reducedb deleted %d\n", vecp_size(&s->learnts) - j); */
+
+ vecp_resize(&s->learnts,j);
+}
+
+static lbool solver_search(solver* s, int nof_conflicts,
+ int nof_learnts)
+{
+ int* levels = s->levels;
+ double var_decay = 0.95;
+ double clause_decay = 0.999;
+ double random_var_freq = 0.02;
+
+ int conflictC = 0;
+ veci learnt_clause;
+
+ assert(s->root_level == solver_dlevel(s));
+
+ s->stats.starts++;
+ s->var_decay = (float)(1 / var_decay );
+ s->cla_decay = (float)(1 / clause_decay);
+ veci_resize(&s->model,0);
+ veci_new(&learnt_clause);
+
+ for (;;){
+ clause* confl = solver_propagate(s);
+ if (confl != 0){
+ /* CONFLICT */
+ int blevel;
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"**CONFLICT**\n", L_ind);
+#endif
+ s->stats.conflicts++; conflictC++;
+ if (solver_dlevel(s) == s->root_level){
+ veci_delete(&learnt_clause);
+ return l_False;
+ }
+
+ veci_resize(&learnt_clause,0);
+ solver_analyze(s, confl, &learnt_clause);
+ blevel = veci_size(&learnt_clause) > 1
+ ? levels[lit_var(veci_begin(&learnt_clause)[1])]
+ : s->root_level;
+ blevel = s->root_level > blevel ? s->root_level : blevel;
+ solver_canceluntil(s,blevel);
+ solver_record(s,&learnt_clause);
+ act_var_decay(s);
+ act_clause_decay(s);
+
+ }else{
+ /* NO CONFLICT */
+ int next;
+
+ if (nof_conflicts >= 0 && conflictC >= nof_conflicts){
+ /* Reached bound on number of conflicts: */
+ s->progress_estimate = solver_progress(s);
+ solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+ return l_Undef; }
+
+ if (solver_dlevel(s) == 0)
+ /* Simplify the set of problem clauses: */
+ solver_simplify(s);
+
+ if (nof_learnts >= 0
+ && vecp_size(&s->learnts) - s->qtail >= nof_learnts)
+ /* Reduce the set of learnt clauses: */
+ solver_reducedb(s);
+
+ /* New variable decision: */
+ s->stats.decisions++;
+ next = order_select(s,(float)random_var_freq);
+
+ if (next == var_Undef){
+ /* Model found: */
+ lbool* values = s->assigns;
+ int i;
+ for (i = 0; i < s->size; i++)
+ veci_push(&s->model,(int)values[i]);
+ solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+
+ /*
+ veci apa; veci_new(&apa);
+ for (i = 0; i < s->size; i++)
+ veci_push(&apa,(int)(s->model.ptr[i] == l_True
+ ? toLit(i) : lit_neg(toLit(i))));
+ printf("model: ");
+ printlits((lit*)apa.ptr,
+ (lit*)apa.ptr + veci_size(&apa)); printf("\n");
+ veci_delete(&apa);
+ */
+
+ return l_True;
+ }
+
+ assume(s,lit_neg(toLit(next)));
+ }
+ }
+
+#if 0 /* by mao; unreachable code */
+ return l_Undef; /* cannot happen */
+#endif
+}
+
+/*====================================================================*/
+/* External solver functions: */
+
+solver* solver_new(void)
+{
+ solver* s = (solver*)malloc(sizeof(solver));
+
+ /* initialize vectors */
+ vecp_new(&s->clauses);
+ vecp_new(&s->learnts);
+ veci_new(&s->order);
+ veci_new(&s->trail_lim);
+ veci_new(&s->tagged);
+ veci_new(&s->stack);
+ veci_new(&s->model);
+
+ /* initialize arrays */
+ s->wlists = 0;
+ s->activity = 0;
+ s->assigns = 0;
+ s->orderpos = 0;
+ s->reasons = 0;
+ s->levels = 0;
+ s->tags = 0;
+ s->trail = 0;
+
+ /* initialize other vars */
+ s->size = 0;
+ s->cap = 0;
+ s->qhead = 0;
+ s->qtail = 0;
+ s->cla_inc = 1;
+ s->cla_decay = 1;
+ s->var_inc = 1;
+ s->var_decay = 1;
+ s->root_level = 0;
+ s->simpdb_assigns = 0;
+ s->simpdb_props = 0;
+ s->random_seed = 91648253;
+ s->progress_estimate = 0;
+ s->binary = (clause*)malloc(sizeof(clause)
+ + sizeof(lit)*2);
+ s->binary->size_learnt = (2 << 1);
+ s->verbosity = 0;
+
+ s->stats.starts = 0;
+ s->stats.decisions = 0;
+ s->stats.propagations = 0;
+ s->stats.inspects = 0;
+ s->stats.conflicts = 0;
+ s->stats.clauses = 0;
+ s->stats.clauses_literals = 0;
+ s->stats.learnts = 0;
+ s->stats.learnts_literals = 0;
+ s->stats.max_literals = 0;
+ s->stats.tot_literals = 0;
+
+ return s;
+}
+
+void solver_delete(solver* s)
+{
+ int i;
+ for (i = 0; i < vecp_size(&s->clauses); i++)
+ free(vecp_begin(&s->clauses)[i]);
+
+ for (i = 0; i < vecp_size(&s->learnts); i++)
+ free(vecp_begin(&s->learnts)[i]);
+
+ /* delete vectors */
+ vecp_delete(&s->clauses);
+ vecp_delete(&s->learnts);
+ veci_delete(&s->order);
+ veci_delete(&s->trail_lim);
+ veci_delete(&s->tagged);
+ veci_delete(&s->stack);
+ veci_delete(&s->model);
+ free(s->binary);
+
+ /* delete arrays */
+ if (s->wlists != 0){
+ int i;
+ for (i = 0; i < s->size*2; i++)
+ vecp_delete(&s->wlists[i]);
+
+ /* if one is different from null, all are */
+ free(s->wlists);
+ free(s->activity );
+ free(s->assigns );
+ free(s->orderpos );
+ free(s->reasons );
+ free(s->levels );
+ free(s->trail );
+ free(s->tags );
+ }
+
+ free(s);
+}
+
+bool solver_addclause(solver* s, lit* begin, lit* end)
+{
+ lit *i,*j;
+ int maxvar;
+ lbool* values;
+ lit last;
+
+ if (begin == end) return false;
+
+ /* printlits(begin,end); printf("\n"); */
+ /* insertion sort */
+ maxvar = lit_var(*begin);
+ for (i = begin + 1; i < end; i++){
+ lit l = *i;
+ maxvar = lit_var(l) > maxvar ? lit_var(l) : maxvar;
+ for (j = i; j > begin && *(j-1) > l; j--)
+ *j = *(j-1);
+ *j = l;
+ }
+ solver_setnvars(s,maxvar+1);
+
+ /* printlits(begin,end); printf("\n"); */
+ values = s->assigns;
+
+ /* delete duplicates */
+ last = lit_Undef;
+ for (i = j = begin; i < end; i++){
+ /* printf("lit: "L_LIT", value = %d\n", L_lit(*i),
+ (lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)])); */
+ lbool sig = !lit_sign(*i); sig += sig - 1;
+ if (*i == lit_neg(last) || sig == values[lit_var(*i)])
+ return true; /* tautology */
+ else if (*i != last && values[lit_var(*i)] == l_Undef)
+ last = *j++ = *i;
+ }
+
+ /* printf("final: "); printlits(begin,j); printf("\n"); */
+
+ if (j == begin) /* empty clause */
+ return false;
+ else if (j - begin == 1) /* unit clause */
+ return enqueue(s,*begin,(clause*)0);
+
+ /* create new clause */
+ vecp_push(&s->clauses,clause_new(s,begin,j,0));
+
+ s->stats.clauses++;
+ s->stats.clauses_literals += j - begin;
+
+ return true;
+}
+
+bool solver_simplify(solver* s)
+{
+ clause** reasons;
+ int type;
+
+ assert(solver_dlevel(s) == 0);
+
+ if (solver_propagate(s) != 0)
+ return false;
+
+ if (s->qhead == s->simpdb_assigns || s->simpdb_props > 0)
+ return true;
+
+ reasons = s->reasons;
+ for (type = 0; type < 2; type++){
+ vecp* cs = type ? &s->learnts : &s->clauses;
+ clause** cls = (clause**)vecp_begin(cs);
+
+ int i, j;
+ for (j = i = 0; i < vecp_size(cs); i++){
+ if (reasons[lit_var(*clause_begin(cls[i]))] != cls[i] &&
+ clause_simplify(s,cls[i]) == l_True)
+ clause_remove(s,cls[i]);
+ else
+ cls[j++] = cls[i];
+ }
+ vecp_resize(cs,j);
+ }
+
+ s->simpdb_assigns = s->qhead;
+ /* (shouldn't depend on 'stats' really, but it will do for now) */
+ s->simpdb_props = (int)(s->stats.clauses_literals
+ + s->stats.learnts_literals);
+
+ return true;
+}
+
+bool solver_solve(solver* s, lit* begin, lit* end)
+{
+ double nof_conflicts = 100;
+ double nof_learnts = solver_nclauses(s) / 3;
+ lbool status = l_Undef;
+ lbool* values = s->assigns;
+ lit* i;
+
+ /* printf("solve: "); printlits(begin, end); printf("\n"); */
+ for (i = begin; i < end; i++){
+ switch (lit_sign(*i) ? -values[lit_var(*i)]
+ : values[lit_var(*i)]){
+ case 1: /* l_True: */
+ break;
+ case 0: /* l_Undef */
+ assume(s, *i);
+ if (solver_propagate(s) == NULL)
+ break;
+ /* falltrough */
+ case -1: /* l_False */
+ solver_canceluntil(s, 0);
+ return false;
+ }
+ }
+
+ s->root_level = solver_dlevel(s);
+
+ if (s->verbosity >= 1){
+ printf("==================================[MINISAT]============"
+ "=======================\n");
+ printf("| Conflicts | ORIGINAL | LEARNT "
+ " | Progress |\n");
+ printf("| | Clauses Literals | Limit Clauses Litera"
+ "ls Lit/Cl | |\n");
+ printf("======================================================="
+ "=======================\n");
+ }
+
+ while (status == l_Undef){
+ double Ratio = (s->stats.learnts == 0)? 0.0 :
+ s->stats.learnts_literals / (double)s->stats.learnts;
+
+ if (s->verbosity >= 1){
+ printf("| %9.0f | %7.0f %8.0f | %7.0f %7.0f %8.0f %7.1f | %"
+ "6.3f %% |\n",
+ (double)s->stats.conflicts,
+ (double)s->stats.clauses,
+ (double)s->stats.clauses_literals,
+ (double)nof_learnts,
+ (double)s->stats.learnts,
+ (double)s->stats.learnts_literals,
+ Ratio,
+ s->progress_estimate*100);
+ fflush(stdout);
+ }
+ status = solver_search(s,(int)nof_conflicts, (int)nof_learnts);
+ nof_conflicts *= 1.5;
+ nof_learnts *= 1.1;
+ }
+ if (s->verbosity >= 1)
+ printf("======================================================="
+ "=======================\n");
+
+ solver_canceluntil(s,0);
+ return status != l_False;
+}
+
+int solver_nvars(solver* s)
+{
+ return s->size;
+}
+
+int solver_nclauses(solver* s)
+{
+ return vecp_size(&s->clauses);
+}
+
+int solver_nconflicts(solver* s)
+{
+ return (int)s->stats.conflicts;
+}
+
+/*====================================================================*/
+/* Sorting functions (sigh): */
+
+static inline void selectionsort(void** array, int size,
+ int(*comp)(const void *, const void *))
+{
+ int i, j, best_i;
+ void* tmp;
+
+ for (i = 0; i < size-1; i++){
+ best_i = i;
+ for (j = i+1; j < size; j++){
+ if (comp(array[j], array[best_i]) < 0)
+ best_i = j;
+ }
+ tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
+ }
+}
+
+static void sortrnd(void** array, int size,
+ int(*comp)(const void *, const void *),
+ double* seed)
+{
+ if (size <= 15)
+ selectionsort(array, size, comp);
+
+ else{
+ void* pivot = array[irand(seed, size)];
+ void* tmp;
+ int i = -1;
+ int j = size;
+
+ for(;;){
+ do i++; while(comp(array[i], pivot)<0);
+ do j--; while(comp(pivot, array[j])<0);
+
+ if (i >= j) break;
+
+ tmp = array[i]; array[i] = array[j]; array[j] = tmp;
+ }
+
+ sortrnd(array , i , comp, seed);
+ sortrnd(&array[i], size-i, comp, seed);
+ }
+}
+
+static void sort(void** array, int size,
+ int(*comp)(const void *, const void *))
+{
+ double seed = 91648253;
+ sortrnd(array,size,comp,&seed);
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/minisat/minisat.h b/test/monniaux/glpk-4.65/src/minisat/minisat.h
new file mode 100644
index 00000000..2733e8d6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/minisat/minisat.h
@@ -0,0 +1,230 @@
+/* minisat.h */
+
+/* Modified by Andrew Makhorin <mao@gnu.org>, August 2011 */
+
+/***********************************************************************
+* MiniSat -- Copyright (c) 2005, Niklas Sorensson
+* http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+*
+* Permission is hereby granted, free of charge, to any person
+* obtaining a copy of this software and associated documentation files
+* (the "Software"), to deal in the Software without restriction,
+* including without limitation the rights to use, copy, modify, merge,
+* publish, distribute, sublicense, and/or sell copies of the Software,
+* and to permit persons to whom the Software is furnished to do so,
+* subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+* SOFTWARE.
+***********************************************************************/
+/* Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko */
+
+#ifndef MINISAT_H
+#define MINISAT_H
+
+/*====================================================================*/
+/* Simple types: */
+
+typedef int bool;
+
+#define true 1
+#define false 0
+
+typedef int lit;
+#if 0 /* by mao */
+typedef char lbool;
+#else
+typedef int lbool;
+#endif
+
+#define var_Undef (int)(-1)
+#define lit_Undef (lit)(-2)
+
+#define l_Undef (lbool)0
+#define l_True (lbool)1
+#define l_False (lbool)(-1)
+
+#define toLit(v) (lit)((v) + (v))
+#define lit_neg(l) (lit)((l) ^ 1)
+#define lit_var(l) (int)((l) >> 1)
+#define lit_sign(l) (int)((l) & 1)
+
+/*====================================================================*/
+/* Vectors: */
+
+/* vector of 32-bit intergers (added for 64-bit portability) */
+typedef struct /* veci_t */ {
+ int size;
+ int cap;
+ int* ptr;
+} veci;
+
+#define veci_new(v) \
+{ (v)->size = 0; \
+ (v)->cap = 4; \
+ (v)->ptr = (int*)malloc(sizeof(int)*(v)->cap); \
+}
+
+#define veci_delete(v) free((v)->ptr)
+
+#define veci_begin(v) ((v)->ptr)
+
+#define veci_size(v) ((v)->size)
+
+#define veci_resize(v, k) (void)((v)->size = (k))
+/* only safe to shrink !! */
+
+#define veci_push(v, e) \
+{ if ((v)->size == (v)->cap) \
+ { int newsize = (v)->cap * 2+1; \
+ (v)->ptr = (int*)realloc((v)->ptr,sizeof(int)*newsize); \
+ (v)->cap = newsize; \
+ } \
+ (v)->ptr[(v)->size++] = (e); \
+}
+
+/* vector of 32- or 64-bit pointers */
+typedef struct /* vecp_t */ {
+ int size;
+ int cap;
+ void** ptr;
+} vecp;
+
+#define vecp_new(v) \
+{ (v)->size = 0; \
+ (v)->cap = 4; \
+ (v)->ptr = (void**)malloc(sizeof(void*)*(v)->cap); \
+}
+
+#define vecp_delete(v) free((v)->ptr)
+
+#define vecp_begin(v) ((v)->ptr)
+
+#define vecp_size(v) ((v)->size)
+
+#define vecp_resize(v, k) (void)((v)->size = (k))
+/* only safe to shrink !! */
+
+#define vecp_push(v, e) \
+{ if ((v)->size == (v)->cap) \
+ { int newsize = (v)->cap * 2+1; \
+ (v)->ptr = (void**)realloc((v)->ptr,sizeof(void*)*newsize); \
+ (v)->cap = newsize; \
+ } \
+ (v)->ptr[(v)->size++] = (e); \
+}
+
+/*====================================================================*/
+/* Solver representation: */
+
+typedef struct /* clause_t */
+{
+ int size_learnt;
+ lit lits[1];
+} clause;
+
+typedef struct /* stats_t */
+{
+ double starts, decisions, propagations, inspects, conflicts;
+ double clauses, clauses_literals, learnts, learnts_literals,
+ max_literals, tot_literals;
+} stats;
+
+typedef struct /* solver_t */
+{
+ int size; /* nof variables */
+ int cap; /* size of varmaps */
+ int qhead; /* Head index of queue. */
+ int qtail; /* Tail index of queue. */
+
+ /* clauses */
+ vecp clauses; /* List of problem constraints.
+ (contains: clause*) */
+ vecp learnts; /* List of learnt clauses.
+ (contains: clause*) */
+
+ /* activities */
+ double var_inc; /* Amount to bump next variable with. */
+ double var_decay; /* INVERSE decay factor for variable
+ activity: stores 1/decay. */
+ float cla_inc; /* Amount to bump next clause with. */
+ float cla_decay; /* INVERSE decay factor for clause
+ activity: stores 1/decay. */
+
+ vecp* wlists;
+ double* activity; /* A heuristic measurement of the activity
+ of a variable. */
+ lbool* assigns; /* Current values of variables. */
+ int* orderpos; /* Index in variable order. */
+ clause** reasons;
+ int* levels;
+ lit* trail;
+
+ clause* binary; /* A temporary binary clause */
+ lbool* tags;
+ veci tagged; /* (contains: var) */
+ veci stack; /* (contains: var) */
+
+ veci order; /* Variable order. (heap) (contains: var) */
+ veci trail_lim; /* Separator indices for different decision
+ levels in 'trail'. (contains: int) */
+ veci model; /* If problem is solved, this vector
+ contains the model (contains: lbool). */
+
+ int root_level; /* Level of first proper decision. */
+ int simpdb_assigns;/* Number of top-level assignments at last
+ 'simplifyDB()'. */
+ int simpdb_props; /* Number of propagations before next
+ 'simplifyDB()'. */
+ double random_seed;
+ double progress_estimate;
+ int verbosity; /* Verbosity level.
+ 0=silent,
+ 1=some progress report,
+ 2=everything */
+
+ stats stats;
+} solver;
+
+/*====================================================================*/
+/* Public interface: */
+
+#if 1 /* by mao; to keep namespace clean */
+#define solver_new _glp_minisat_new
+#define solver_delete _glp_minisat_delete
+#define solver_addclause _glp_minisat_addclause
+#define solver_simplify _glp_minisat_simplify
+#define solver_solve _glp_minisat_solve
+#define solver_nvars _glp_minisat_nvars
+#define solver_nclauses _glp_minisat_nclauses
+#define solver_nconflicts _glp_minisat_nconflicts
+#define solver_setnvars _glp_minisat_setnvars
+#define solver_propagate _glp_minisat_propagate
+#define solver_reducedb _glp_minisat_reducedb
+#endif
+
+solver* solver_new(void);
+void solver_delete(solver* s);
+
+bool solver_addclause(solver* s, lit* begin, lit* end);
+bool solver_simplify(solver* s);
+bool solver_solve(solver* s, lit* begin, lit* end);
+
+int solver_nvars(solver* s);
+int solver_nclauses(solver* s);
+int solver_nconflicts(solver* s);
+
+void solver_setnvars(solver* s,int n);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/avl.c b/test/monniaux/glpk-4.65/src/misc/avl.c
new file mode 100644
index 00000000..c97cf13a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/avl.c
@@ -0,0 +1,405 @@
+/* avl.c (binary search tree) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "avl.h"
+#include "dmp.h"
+#include "env.h"
+
+struct AVL
+{ /* AVL tree (Adelson-Velsky & Landis binary search tree) */
+ DMP *pool;
+ /* memory pool for allocating nodes */
+ AVLNODE *root;
+ /* pointer to the root node */
+ int (*fcmp)(void *info, const void *key1, const void *key2);
+ /* application-defined key comparison routine */
+ void *info;
+ /* transit pointer passed to the routine fcmp */
+ int size;
+ /* the tree size (the total number of nodes) */
+ int height;
+ /* the tree height */
+};
+
+struct AVLNODE
+{ /* node of AVL tree */
+ const void *key;
+ /* pointer to the node key (data structure for representing keys
+ is supplied by the application) */
+ int rank;
+ /* node rank = relative position of the node in its own subtree =
+ the number of nodes in the left subtree plus one */
+ int type;
+ /* reserved for the application specific information */
+ void *link;
+ /* reserved for the application specific information */
+ AVLNODE *up;
+ /* pointer to the parent node */
+ short int flag;
+ /* node flag:
+ 0 - this node is the left child of its parent (or this node is
+ the root of the tree and has no parent)
+ 1 - this node is the right child of its parent */
+ short int bal;
+ /* node balance = the difference between heights of the right and
+ left subtrees:
+ -1 - the left subtree is higher than the right one;
+ 0 - the left and right subtrees have the same height;
+ +1 - the left subtree is lower than the right one */
+ AVLNODE *left;
+ /* pointer to the root of the left subtree */
+ AVLNODE *right;
+ /* pointer to the root of the right subtree */
+};
+
+AVL *avl_create_tree(int (*fcmp)(void *info, const void *key1,
+ const void *key2), void *info)
+{ /* create AVL tree */
+ AVL *tree;
+ tree = xmalloc(sizeof(AVL));
+ tree->pool = dmp_create_pool();
+ tree->root = NULL;
+ tree->fcmp = fcmp;
+ tree->info = info;
+ tree->size = 0;
+ tree->height = 0;
+ return tree;
+}
+
+int avl_strcmp(void *info, const void *key1, const void *key2)
+{ /* compare character string keys */
+ xassert(info == info);
+ return strcmp(key1, key2);
+}
+
+static AVLNODE *rotate_subtree(AVL *tree, AVLNODE *node);
+
+AVLNODE *avl_insert_node(AVL *tree, const void *key)
+{ /* insert new node into AVL tree */
+ AVLNODE *p, *q, *r;
+ short int flag;
+ /* find an appropriate point for insertion */
+ p = NULL; q = tree->root;
+ while (q != NULL)
+ { p = q;
+ if (tree->fcmp(tree->info, key, p->key) <= 0)
+ { flag = 0;
+ q = p->left;
+ p->rank++;
+ }
+ else
+ { flag = 1;
+ q = p->right;
+ }
+ }
+ /* create new node and insert it into the tree */
+ r = dmp_get_atom(tree->pool, sizeof(AVLNODE));
+ r->key = key; r->type = 0; r->link = NULL;
+ r->rank = 1; r->up = p;
+ r->flag = (short int)(p == NULL ? 0 : flag);
+ r->bal = 0; r->left = NULL; r->right = NULL;
+ tree->size++;
+ if (p == NULL)
+ tree->root = r;
+ else
+ if (flag == 0) p->left = r; else p->right = r;
+ /* go upstairs to the root and correct all subtrees affected by
+ insertion */
+ while (p != NULL)
+ { if (flag == 0)
+ { /* the height of the left subtree of [p] is increased */
+ if (p->bal > 0)
+ { p->bal = 0;
+ break;
+ }
+ if (p->bal < 0)
+ { rotate_subtree(tree, p);
+ break;
+ }
+ p->bal = -1; flag = p->flag; p = p->up;
+ }
+ else
+ { /* the height of the right subtree of [p] is increased */
+ if (p->bal < 0)
+ { p->bal = 0;
+ break;
+ }
+ if (p->bal > 0)
+ { rotate_subtree(tree, p);
+ break;
+ }
+ p->bal = +1; flag = p->flag; p = p->up;
+ }
+ }
+ /* if the root has been reached, the height of the entire tree is
+ increased */
+ if (p == NULL) tree->height++;
+ return r;
+}
+
+void avl_set_node_type(AVLNODE *node, int type)
+{ /* assign the type field of specified node */
+ node->type = type;
+ return;
+}
+
+void avl_set_node_link(AVLNODE *node, void *link)
+{ /* assign the link field of specified node */
+ node->link = link;
+ return;
+}
+
+AVLNODE *avl_find_node(AVL *tree, const void *key)
+{ /* find node in AVL tree */
+ AVLNODE *p;
+ int c;
+ p = tree->root;
+ while (p != NULL)
+ { c = tree->fcmp(tree->info, key, p->key);
+ if (c == 0) break;
+ p = (c < 0 ? p->left : p->right);
+ }
+ return p;
+}
+
+int avl_get_node_type(AVLNODE *node)
+{ /* retrieve the type field of specified node */
+ return node->type;
+}
+
+void *avl_get_node_link(AVLNODE *node)
+{ /* retrieve the link field of specified node */
+ return node->link;
+}
+
+static AVLNODE *find_next_node(AVL *tree, AVLNODE *node)
+{ /* find next node in AVL tree */
+ AVLNODE *p, *q;
+ if (tree->root == NULL) return NULL;
+ p = node;
+ q = (p == NULL ? tree->root : p->right);
+ if (q == NULL)
+ { /* go upstairs from the left subtree */
+ for (;;)
+ { q = p->up;
+ if (q == NULL) break;
+ if (p->flag == 0) break;
+ p = q;
+ }
+ }
+ else
+ { /* go downstairs into the right subtree */
+ for (;;)
+ { p = q->left;
+ if (p == NULL) break;
+ q = p;
+ }
+ }
+ return q;
+}
+
+void avl_delete_node(AVL *tree, AVLNODE *node)
+{ /* delete specified node from AVL tree */
+ AVLNODE *f, *p, *q, *r, *s, *x, *y;
+ short int flag;
+ p = node;
+ /* if both subtrees of the specified node are non-empty, the node
+ should be interchanged with the next one, at least one subtree
+ of which is always empty */
+ if (p->left == NULL || p->right == NULL) goto skip;
+ f = p->up; q = p->left;
+ r = find_next_node(tree, p); s = r->right;
+ if (p->right == r)
+ { if (f == NULL)
+ tree->root = r;
+ else
+ if (p->flag == 0) f->left = r; else f->right = r;
+ r->rank = p->rank; r->up = f;
+ r->flag = p->flag; r->bal = p->bal;
+ r->left = q; r->right = p;
+ q->up = r;
+ p->rank = 1; p->up = r; p->flag = 1;
+ p->bal = (short int)(s == NULL ? 0 : +1);
+ p->left = NULL; p->right = s;
+ if (s != NULL) s->up = p;
+ }
+ else
+ { x = p->right; y = r->up;
+ if (f == NULL)
+ tree->root = r;
+ else
+ if (p->flag == 0) f->left = r; else f->right = r;
+ r->rank = p->rank; r->up = f;
+ r->flag = p->flag; r->bal = p->bal;
+ r->left = q; r->right = x;
+ q->up = r; x->up = r; y->left = p;
+ p->rank = 1; p->up = y; p->flag = 0;
+ p->bal = (short int)(s == NULL ? 0 : +1);
+ p->left = NULL; p->right = s;
+ if (s != NULL) s->up = p;
+ }
+skip: /* now the specified node [p] has at least one empty subtree;
+ go upstairs to the root and adjust the rank field of all nodes
+ affected by deletion */
+ q = p; f = q->up;
+ while (f != NULL)
+ { if (q->flag == 0) f->rank--;
+ q = f; f = q->up;
+ }
+ /* delete the specified node from the tree */
+ f = p->up; flag = p->flag;
+ q = p->left != NULL ? p->left : p->right;
+ if (f == NULL)
+ tree->root = q;
+ else
+ if (flag == 0) f->left = q; else f->right = q;
+ if (q != NULL) q->up = f, q->flag = flag;
+ tree->size--;
+ /* go upstairs to the root and correct all subtrees affected by
+ deletion */
+ while (f != NULL)
+ { if (flag == 0)
+ { /* the height of the left subtree of [f] is decreased */
+ if (f->bal == 0)
+ { f->bal = +1;
+ break;
+ }
+ if (f->bal < 0)
+ f->bal = 0;
+ else
+ { f = rotate_subtree(tree, f);
+ if (f->bal < 0) break;
+ }
+ flag = f->flag; f = f->up;
+ }
+ else
+ { /* the height of the right subtree of [f] is decreased */
+ if (f->bal == 0)
+ { f->bal = -1;
+ break;
+ }
+ if (f->bal > 0)
+ f->bal = 0;
+ else
+ { f = rotate_subtree(tree, f);
+ if (f->bal > 0) break;
+ }
+ flag = f->flag; f = f->up;
+ }
+ }
+ /* if the root has been reached, the height of the entire tree is
+ decreased */
+ if (f == NULL) tree->height--;
+ /* returns the deleted node to the memory pool */
+ dmp_free_atom(tree->pool, p, sizeof(AVLNODE));
+ return;
+}
+
+static AVLNODE *rotate_subtree(AVL *tree, AVLNODE *node)
+{ /* restore balance of AVL subtree */
+ AVLNODE *f, *p, *q, *r, *x, *y;
+ xassert(node != NULL);
+ p = node;
+ if (p->bal < 0)
+ { /* perform negative (left) rotation */
+ f = p->up; q = p->left; r = q->right;
+ if (q->bal <= 0)
+ { /* perform single negative rotation */
+ if (f == NULL)
+ tree->root = q;
+ else
+ if (p->flag == 0) f->left = q; else f->right = q;
+ p->rank -= q->rank;
+ q->up = f; q->flag = p->flag; q->bal++; q->right = p;
+ p->up = q; p->flag = 1;
+ p->bal = (short int)(-q->bal); p->left = r;
+ if (r != NULL) r->up = p, r->flag = 0;
+ node = q;
+ }
+ else
+ { /* perform double negative rotation */
+ x = r->left; y = r->right;
+ if (f == NULL)
+ tree->root = r;
+ else
+ if (p->flag == 0) f->left = r; else f->right = r;
+ p->rank -= (q->rank + r->rank);
+ r->rank += q->rank;
+ p->bal = (short int)(r->bal >= 0 ? 0 : +1);
+ q->bal = (short int)(r->bal <= 0 ? 0 : -1);
+ r->up = f; r->flag = p->flag; r->bal = 0;
+ r->left = q; r->right = p;
+ p->up = r; p->flag = 1; p->left = y;
+ q->up = r; q->flag = 0; q->right = x;
+ if (x != NULL) x->up = q, x->flag = 1;
+ if (y != NULL) y->up = p, y->flag = 0;
+ node = r;
+ }
+ }
+ else
+ { /* perform positive (right) rotation */
+ f = p->up; q = p->right; r = q->left;
+ if (q->bal >= 0)
+ { /* perform single positive rotation */
+ if (f == NULL)
+ tree->root = q;
+ else
+ if (p->flag == 0) f->left = q; else f->right = q;
+ q->rank += p->rank;
+ q->up = f; q->flag = p->flag; q->bal--; q->left = p;
+ p->up = q; p->flag = 0;
+ p->bal = (short int)(-q->bal); p->right = r;
+ if (r != NULL) r->up = p, r->flag = 1;
+ node = q;
+ }
+ else
+ { /* perform double positive rotation */
+ x = r->left; y = r->right;
+ if (f == NULL)
+ tree->root = r;
+ else
+ if (p->flag == 0) f->left = r; else f->right = r;
+ q->rank -= r->rank;
+ r->rank += p->rank;
+ p->bal = (short int)(r->bal <= 0 ? 0 : -1);
+ q->bal = (short int)(r->bal >= 0 ? 0 : +1);
+ r->up = f; r->flag = p->flag; r->bal = 0;
+ r->left = p; r->right = q;
+ p->up = r; p->flag = 0; p->right = x;
+ q->up = r; q->flag = 1; q->left = y;
+ if (x != NULL) x->up = p, x->flag = 1;
+ if (y != NULL) y->up = q, y->flag = 0;
+ node = r;
+ }
+ }
+ return node;
+}
+
+void avl_delete_tree(AVL *tree)
+{ /* delete AVL tree */
+ dmp_delete_pool(tree->pool);
+ xfree(tree);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/avl.h b/test/monniaux/glpk-4.65/src/misc/avl.h
new file mode 100644
index 00000000..b0aaef61
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/avl.h
@@ -0,0 +1,73 @@
+/* avl.h (binary search tree) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef AVL_H
+#define AVL_H
+
+typedef struct AVL AVL;
+typedef struct AVLNODE AVLNODE;
+
+#define avl_create_tree _glp_avl_create_tree
+AVL *avl_create_tree(int (*fcmp)(void *info, const void *key1,
+ const void *key2), void *info);
+/* create AVL tree */
+
+#define avl_strcmp _glp_avl_strcmp
+int avl_strcmp(void *info, const void *key1, const void *key2);
+/* compare character string keys */
+
+#define avl_insert_node _glp_avl_insert_node
+AVLNODE *avl_insert_node(AVL *tree, const void *key);
+/* insert new node into AVL tree */
+
+#define avl_set_node_type _glp_avl_set_node_type
+void avl_set_node_type(AVLNODE *node, int type);
+/* assign the type field of specified node */
+
+#define avl_set_node_link _glp_avl_set_node_link
+void avl_set_node_link(AVLNODE *node, void *link);
+/* assign the link field of specified node */
+
+#define avl_find_node _glp_avl_find_node
+AVLNODE *avl_find_node(AVL *tree, const void *key);
+/* find node in AVL tree */
+
+#define avl_get_node_type _glp_avl_get_node_type
+int avl_get_node_type(AVLNODE *node);
+/* retrieve the type field of specified node */
+
+#define avl_get_node_link _glp_avl_get_node_link
+void *avl_get_node_link(AVLNODE *node);
+/* retrieve the link field of specified node */
+
+#define avl_delete_node _glp_avl_delete_node
+void avl_delete_node(AVL *tree, AVLNODE *node);
+/* delete specified node from AVL tree */
+
+#define avl_delete_tree _glp_avl_delete_tree
+void avl_delete_tree(AVL *tree);
+/* delete AVL tree */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/bignum.c b/test/monniaux/glpk-4.65/src/misc/bignum.c
new file mode 100644
index 00000000..540dd9fd
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/bignum.c
@@ -0,0 +1,286 @@
+/* bignum.c (bignum arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2006-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "bignum.h"
+
+/***********************************************************************
+* Two routines below are intended to multiply and divide unsigned
+* integer numbers of arbitrary precision.
+*
+* The routines assume that an unsigned integer number is represented in
+* the positional numeral system with the base 2^16 = 65536, i.e. each
+* "digit" of the number is in the range [0, 65535] and represented as
+* a 16-bit value of the unsigned short type. In other words, a number x
+* has the following representation:
+*
+* n-1
+* x = sum d[j] * 65536^j,
+* j=0
+*
+* where n is the number of places (positions), and d[j] is j-th "digit"
+* of x, 0 <= d[j] <= 65535.
+***********************************************************************/
+
+/***********************************************************************
+* NAME
+*
+* bigmul - multiply unsigned integer numbers of arbitrary precision
+*
+* SYNOPSIS
+*
+* #include "bignum.h"
+* void bigmul(int n, int m, unsigned short x[], unsigned short y[]);
+*
+* DESCRIPTION
+*
+* The routine bigmul multiplies unsigned integer numbers of arbitrary
+* precision.
+*
+* n is the number of digits of multiplicand, n >= 1;
+*
+* m is the number of digits of multiplier, m >= 1;
+*
+* x is an array containing digits of the multiplicand in elements
+* x[m], x[m+1], ..., x[n+m-1]. Contents of x[0], x[1], ..., x[m-1] are
+* ignored on entry.
+*
+* y is an array containing digits of the multiplier in elements y[0],
+* y[1], ..., y[m-1].
+*
+* On exit digits of the product are stored in elements x[0], x[1], ...,
+* x[n+m-1]. The array y is not changed. */
+
+void bigmul(int n, int m, unsigned short x[], unsigned short y[])
+{ int i, j;
+ unsigned int t;
+ xassert(n >= 1);
+ xassert(m >= 1);
+ for (j = 0; j < m; j++) x[j] = 0;
+ for (i = 0; i < n; i++)
+ { if (x[i+m])
+ { t = 0;
+ for (j = 0; j < m; j++)
+ { t += (unsigned int)x[i+m] * (unsigned int)y[j] +
+ (unsigned int)x[i+j];
+ x[i+j] = (unsigned short)t;
+ t >>= 16;
+ }
+ x[i+m] = (unsigned short)t;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* bigdiv - divide unsigned integer numbers of arbitrary precision
+*
+* SYNOPSIS
+*
+* #include "bignum.h"
+* void bigdiv(int n, int m, unsigned short x[], unsigned short y[]);
+*
+* DESCRIPTION
+*
+* The routine bigdiv divides one unsigned integer number of arbitrary
+* precision by another with the algorithm described in [1].
+*
+* n is the difference between the number of digits of dividend and the
+* number of digits of divisor, n >= 0.
+*
+* m is the number of digits of divisor, m >= 1.
+*
+* x is an array containing digits of the dividend in elements x[0],
+* x[1], ..., x[n+m-1].
+*
+* y is an array containing digits of the divisor in elements y[0],
+* y[1], ..., y[m-1]. The highest digit y[m-1] must be non-zero.
+*
+* On exit n+1 digits of the quotient are stored in elements x[m],
+* x[m+1], ..., x[n+m], and m digits of the remainder are stored in
+* elements x[0], x[1], ..., x[m-1]. The array y is changed but then
+* restored.
+*
+* REFERENCES
+*
+* 1. D. Knuth. The Art of Computer Programming. Vol. 2: Seminumerical
+* Algorithms. Stanford University, 1969. */
+
+void bigdiv(int n, int m, unsigned short x[], unsigned short y[])
+{ int i, j;
+ unsigned int t;
+ unsigned short d, q, r;
+ xassert(n >= 0);
+ xassert(m >= 1);
+ xassert(y[m-1] != 0);
+ /* special case when divisor has the only digit */
+ if (m == 1)
+ { d = 0;
+ for (i = n; i >= 0; i--)
+ { t = ((unsigned int)d << 16) + (unsigned int)x[i];
+ x[i+1] = (unsigned short)(t / y[0]);
+ d = (unsigned short)(t % y[0]);
+ }
+ x[0] = d;
+ goto done;
+ }
+ /* multiply dividend and divisor by a normalizing coefficient in
+ * order to provide the condition y[m-1] >= base / 2 */
+ d = (unsigned short)(0x10000 / ((unsigned int)y[m-1] + 1));
+ if (d == 1)
+ x[n+m] = 0;
+ else
+ { t = 0;
+ for (i = 0; i < n+m; i++)
+ { t += (unsigned int)x[i] * (unsigned int)d;
+ x[i] = (unsigned short)t;
+ t >>= 16;
+ }
+ x[n+m] = (unsigned short)t;
+ t = 0;
+ for (j = 0; j < m; j++)
+ { t += (unsigned int)y[j] * (unsigned int)d;
+ y[j] = (unsigned short)t;
+ t >>= 16;
+ }
+ }
+ /* main loop */
+ for (i = n; i >= 0; i--)
+ { /* estimate and correct the current digit of quotient */
+ if (x[i+m] < y[m-1])
+ { t = ((unsigned int)x[i+m] << 16) + (unsigned int)x[i+m-1];
+ q = (unsigned short)(t / (unsigned int)y[m-1]);
+ r = (unsigned short)(t % (unsigned int)y[m-1]);
+ if (q == 0) goto putq; else goto test;
+ }
+ q = 0;
+ r = x[i+m-1];
+decr: q--; /* if q = 0 then q-- = 0xFFFF */
+ t = (unsigned int)r + (unsigned int)y[m-1];
+ r = (unsigned short)t;
+ if (t > 0xFFFF) goto msub;
+test: t = (unsigned int)y[m-2] * (unsigned int)q;
+ if ((unsigned short)(t >> 16) > r) goto decr;
+ if ((unsigned short)(t >> 16) < r) goto msub;
+ if ((unsigned short)t > x[i+m-2]) goto decr;
+msub: /* now subtract divisor multiplied by the current digit of
+ * quotient from the current dividend */
+ if (q == 0) goto putq;
+ t = 0;
+ for (j = 0; j < m; j++)
+ { t += (unsigned int)y[j] * (unsigned int)q;
+ if (x[i+j] < (unsigned short)t) t += 0x10000;
+ x[i+j] -= (unsigned short)t;
+ t >>= 16;
+ }
+ if (x[i+m] >= (unsigned short)t) goto putq;
+ /* perform correcting addition, because the current digit of
+ * quotient is greater by one than its correct value */
+ q--;
+ t = 0;
+ for (j = 0; j < m; j++)
+ { t += (unsigned int)x[i+j] + (unsigned int)y[j];
+ x[i+j] = (unsigned short)t;
+ t >>= 16;
+ }
+putq: /* store the current digit of quotient */
+ x[i+m] = q;
+ }
+ /* divide divisor and remainder by the normalizing coefficient in
+ * order to restore their original values */
+ if (d > 1)
+ { t = 0;
+ for (i = m-1; i >= 0; i--)
+ { t = (t << 16) + (unsigned int)x[i];
+ x[i] = (unsigned short)(t / (unsigned int)d);
+ t %= (unsigned int)d;
+ }
+ t = 0;
+ for (j = m-1; j >= 0; j--)
+ { t = (t << 16) + (unsigned int)y[j];
+ y[j] = (unsigned short)(t / (unsigned int)d);
+ t %= (unsigned int)d;
+ }
+ }
+done: return;
+}
+
+/**********************************************************************/
+
+#ifdef GLP_TEST
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "rng.h"
+
+#define N_MAX 7
+/* maximal number of digits in multiplicand */
+
+#define M_MAX 5
+/* maximal number of digits in multiplier */
+
+#define N_TEST 1000000
+/* number of tests */
+
+int main(void)
+{ RNG *rand;
+ int d, j, n, m, test;
+ unsigned short x[N_MAX], y[M_MAX], z[N_MAX+M_MAX];
+ rand = rng_create_rand();
+ for (test = 1; test <= N_TEST; test++)
+ { /* x[0,...,n-1] := multiplicand */
+ n = 1 + rng_unif_rand(rand, N_MAX-1);
+ assert(1 <= n && n <= N_MAX);
+ for (j = 0; j < n; j++)
+ { d = rng_unif_rand(rand, 65536);
+ assert(0 <= d && d <= 65535);
+ x[j] = (unsigned short)d;
+ }
+ /* y[0,...,m-1] := multiplier */
+ m = 1 + rng_unif_rand(rand, M_MAX-1);
+ assert(1 <= m && m <= M_MAX);
+ for (j = 0; j < m; j++)
+ { d = rng_unif_rand(rand, 65536);
+ assert(0 <= d && d <= 65535);
+ y[j] = (unsigned short)d;
+ }
+ if (y[m-1] == 0) y[m-1] = 1;
+ /* z[0,...,n+m-1] := x * y */
+ for (j = 0; j < n; j++) z[m+j] = x[j];
+ bigmul(n, m, z, y);
+ /* z[0,...,m-1] := z mod y, z[m,...,n+m-1] := z div y */
+ bigdiv(n, m, z, y);
+ /* z mod y must be 0 */
+ for (j = 0; j < m; j++) assert(z[j] == 0);
+ /* z div y must be x */
+ for (j = 0; j < n; j++) assert(z[m+j] == x[j]);
+ }
+ fprintf(stderr, "%d tests successfully passed\n", N_TEST);
+ rng_delete_rand(rand);
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/bignum.h b/test/monniaux/glpk-4.65/src/misc/bignum.h
new file mode 100644
index 00000000..8567519b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/bignum.h
@@ -0,0 +1,37 @@
+/* bignum.h (bignum arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2006-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef BIGNUM_H
+#define BIGNUM_H
+
+#define bigmul _glp_bigmul
+void bigmul(int n, int m, unsigned short x[], unsigned short y[]);
+/* multiply unsigned integer numbers of arbitrary precision */
+
+#define bigdiv _glp_bigdiv
+void bigdiv(int n, int m, unsigned short x[], unsigned short y[]);
+/* divide unsigned integer numbers of arbitrary precision */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/dimacs.c b/test/monniaux/glpk-4.65/src/misc/dimacs.c
new file mode 100644
index 00000000..6aa630a5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/dimacs.c
@@ -0,0 +1,147 @@
+/* dimacs.c (reading data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "dimacs.h"
+
+void dmx_error(DMX *csa, const char *fmt, ...)
+{ /* print error message and terminate processing */
+ va_list arg;
+ xprintf("%s:%d: error: ", csa->fname, csa->count);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ xprintf("\n");
+ longjmp(csa->jump, 1);
+ /* no return */
+}
+
+void dmx_warning(DMX *csa, const char *fmt, ...)
+{ /* print warning message and continue processing */
+ va_list arg;
+ xprintf("%s:%d: warning: ", csa->fname, csa->count);
+ va_start(arg, fmt);
+ xvprintf(fmt, arg);
+ va_end(arg);
+ xprintf("\n");
+ return;
+}
+
+void dmx_read_char(DMX *csa)
+{ /* read character from input text file */
+ int c;
+ if (csa->c == '\n') csa->count++;
+ c = glp_getc(csa->fp);
+ if (c < 0)
+ { if (glp_ioerr(csa->fp))
+ dmx_error(csa, "read error - %s", get_err_msg());
+ else if (csa->c == '\n')
+ dmx_error(csa, "unexpected end of file");
+ else
+ { dmx_warning(csa, "missing final end of line");
+ c = '\n';
+ }
+ }
+ else if (c == '\n')
+ ;
+ else if (isspace(c))
+ c = ' ';
+ else if (iscntrl(c))
+ dmx_error(csa, "invalid control character 0x%02X", c);
+ csa->c = c;
+ return;
+}
+
+void dmx_read_designator(DMX *csa)
+{ /* read one-character line designator */
+ xassert(csa->c == '\n');
+ dmx_read_char(csa);
+ for (;;)
+ { /* skip preceding white-space characters */
+ while (csa->c == ' ')
+ dmx_read_char(csa);
+ if (csa->c == '\n')
+ { /* ignore empty line */
+ if (!csa->empty)
+ { dmx_warning(csa, "empty line ignored");
+ csa->empty = 1;
+ }
+ dmx_read_char(csa);
+ }
+ else if (csa->c == 'c')
+ { /* skip comment line */
+ while (csa->c != '\n')
+ dmx_read_char(csa);
+ dmx_read_char(csa);
+ }
+ else
+ { /* hmm... looks like a line designator */
+ csa->field[0] = (char)csa->c, csa->field[1] = '\0';
+ /* check that it is followed by a white-space character */
+ dmx_read_char(csa);
+ if (!(csa->c == ' ' || csa->c == '\n'))
+ dmx_error(csa, "line designator missing or invalid");
+ break;
+ }
+ }
+ return;
+}
+
+void dmx_read_field(DMX *csa)
+{ /* read data field */
+ int len = 0;
+ /* skip preceding white-space characters */
+ while (csa->c == ' ')
+ dmx_read_char(csa);
+ /* scan data field */
+ if (csa->c == '\n')
+ dmx_error(csa, "unexpected end of line");
+ while (!(csa->c == ' ' || csa->c == '\n'))
+ { if (len == sizeof(csa->field)-1)
+ dmx_error(csa, "data field '%.15s...' too long",
+ csa->field);
+ csa->field[len++] = (char)csa->c;
+ dmx_read_char(csa);
+ }
+ csa->field[len] = '\0';
+ return;
+}
+
+void dmx_end_of_line(DMX *csa)
+{ /* skip white-space characters until end of line */
+ while (csa->c == ' ')
+ dmx_read_char(csa);
+ if (csa->c != '\n')
+ dmx_error(csa, "too many data fields specified");
+ return;
+}
+
+void dmx_check_int(DMX *csa, double num)
+{ /* print a warning if non-integer data are detected */
+ if (!csa->nonint && num != floor(num))
+ { dmx_warning(csa, "non-integer data detected");
+ csa->nonint = 1;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/dimacs.h b/test/monniaux/glpk-4.65/src/misc/dimacs.h
new file mode 100644
index 00000000..42fb9996
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/dimacs.h
@@ -0,0 +1,81 @@
+/* dimacs.h (reading data in DIMACS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef DIMACS_H
+#define DIMACS_H
+
+#include "env.h"
+
+typedef struct DMX DMX;
+
+struct DMX
+{ /* DIMACS data reader */
+ jmp_buf jump;
+ /* label for go to in case of error */
+ const char *fname;
+ /* name of input text file */
+ glp_file *fp;
+ /* stream assigned to input text file */
+ int count;
+ /* line count */
+ int c;
+ /* current character */
+ char field[255+1];
+ /* data field */
+ int empty;
+ /* warning 'empty line ignored' was printed */
+ int nonint;
+ /* warning 'non-integer data detected' was printed */
+};
+
+#define dmx_error _glp_dmx_error
+void dmx_error(DMX *csa, const char *fmt, ...);
+/* print error message and terminate processing */
+
+#define dmx_warning _glp_dmx_warning
+void dmx_warning(DMX *csa, const char *fmt, ...);
+/* print warning message and continue processing */
+
+#define dmx_read_char _glp_dmx_read_char
+void dmx_read_char(DMX *csa);
+/* read character from input text file */
+
+#define dmx_read_designator _glp_dmx_read_designator
+void dmx_read_designator(DMX *csa);
+/* read one-character line designator */
+
+#define dmx_read_field _glp_dmx_read_field
+void dmx_read_field(DMX *csa);
+/* read data field */
+
+#define dmx_end_of_line _glp_dmx_end_of_line
+void dmx_end_of_line(DMX *csa);
+/* skip white-space characters until end of line */
+
+#define dmx_check_int _glp_dmx_check_int
+void dmx_check_int(DMX *csa, double num);
+/* print a warning if non-integer data are detected */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/dmp.c b/test/monniaux/glpk-4.65/src/misc/dmp.c
new file mode 100644
index 00000000..a4882c86
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/dmp.c
@@ -0,0 +1,243 @@
+/* dmp.c (dynamic memory pool) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "dmp.h"
+
+struct DMP
+{ /* dynamic memory pool */
+ void *avail[32];
+ /* avail[k], 0 <= k <= 31, is a pointer to first available (free)
+ * atom of (k+1)*8 bytes long; at the beginning of each free atom
+ * there is a pointer to another free atom of the same size */
+ void *block;
+ /* pointer to most recently allocated memory block; at the
+ * beginning of each allocated memory block there is a pointer to
+ * previously allocated memory block */
+ int used;
+ /* number of bytes used in most recently allocated memory block */
+ size_t count;
+ /* number of atoms which are currently in use */
+};
+
+#define DMP_BLK_SIZE 8000
+/* size of memory blocks, in bytes, allocated for memory pools */
+
+struct prefix
+{ /* atom prefix (for debugging only) */
+ DMP *pool;
+ /* dynamic memory pool */
+ int size;
+ /* original atom size, in bytes */
+};
+
+#define prefix_size ((sizeof(struct prefix) + 7) & ~7)
+/* size of atom prefix rounded up to multiple of 8 bytes */
+
+int dmp_debug;
+/* debug mode flag */
+
+/***********************************************************************
+* NAME
+*
+* dmp_create_pool - create dynamic memory pool
+*
+* SYNOPSIS
+*
+* #include "dmp.h"
+* DMP *dmp_create_pool(void);
+*
+* DESCRIPTION
+*
+* The routine dmp_create_pool creates a dynamic memory pool.
+*
+* RETURNS
+*
+* The routine returns a pointer to the memory pool created. */
+
+DMP *dmp_create_pool(void)
+{ DMP *pool;
+ int k;
+ xassert(sizeof(void *) <= 8);
+ if (dmp_debug)
+ xprintf("dmp_create_pool: warning: debug mode is on\n");
+ pool = talloc(1, DMP);
+ for (k = 0; k <= 31; k++)
+ pool->avail[k] = NULL;
+ pool->block = NULL;
+ pool->used = DMP_BLK_SIZE;
+ pool->count = 0;
+ return pool;
+}
+
+/***********************************************************************
+* NAME
+*
+* dmp_get_atom - get free atom from dynamic memory pool
+*
+* SYNOPSIS
+*
+* #include "dmp.h"
+* void *dmp_get_atom(DMP *pool, int size);
+*
+* DESCRIPTION
+*
+* The routine dmp_get_atom obtains a free atom (memory space) from the
+* specified memory pool.
+*
+* The parameter size is the atom size, in bytes, 1 <= size <= 256.
+*
+* Note that the free atom contains arbitrary data, not binary zeros.
+*
+* RETURNS
+*
+* The routine returns a pointer to the free atom obtained. */
+
+void *dmp_get_atom(DMP *pool, int size)
+{ void *atom;
+ int k, need;
+ xassert(1 <= size && size <= 256);
+ /* round up atom size to multiple of 8 bytes */
+ need = (size + 7) & ~7;
+ /* determine number of corresponding list of free atoms */
+ k = (need >> 3) - 1;
+ /* obtain free atom */
+ if (pool->avail[k] == NULL)
+ { /* corresponding list of free atoms is empty */
+ /* if debug mode is on, add atom prefix size */
+ if (dmp_debug)
+ need += prefix_size;
+ if (pool->used + need > DMP_BLK_SIZE)
+ { /* allocate new memory block */
+ void *block = talloc(DMP_BLK_SIZE, char);
+ *(void **)block = pool->block;
+ pool->block = block;
+ pool->used = 8; /* sufficient to store pointer */
+ }
+ /* allocate new atom in current memory block */
+ atom = (char *)pool->block + pool->used;
+ pool->used += need;
+ }
+ else
+ { /* obtain atom from corresponding list of free atoms */
+ atom = pool->avail[k];
+ pool->avail[k] = *(void **)atom;
+ }
+ /* if debug mode is on, fill atom prefix */
+ if (dmp_debug)
+ { ((struct prefix *)atom)->pool = pool;
+ ((struct prefix *)atom)->size = size;
+ atom = (char *)atom + prefix_size;
+ }
+ /* increase number of allocated atoms */
+ pool->count++;
+ return atom;
+}
+
+/***********************************************************************
+* NAME
+*
+* dmp_free_atom - return atom to dynamic memory pool
+*
+* SYNOPSIS
+*
+* #include "dmp.h"
+* void dmp_free_atom(DMP *pool, void *atom, int size);
+*
+* DESCRIPTION
+*
+* The routine dmp_free_atom returns the specified atom (memory space)
+* to the specified memory pool, making the atom free.
+*
+* The parameter size is the atom size, in bytes, 1 <= size <= 256.
+*
+* Note that the atom can be returned only to the pool, from which it
+* was obtained, and its size must be exactly the same as on obtaining
+* it from the pool. */
+
+void dmp_free_atom(DMP *pool, void *atom, int size)
+{ int k;
+ xassert(1 <= size && size <= 256);
+ /* determine number of corresponding list of free atoms */
+ k = ((size + 7) >> 3) - 1;
+ /* if debug mode is on, check atom prefix */
+ if (dmp_debug)
+ { atom = (char *)atom - prefix_size;
+ xassert(((struct prefix *)atom)->pool == pool);
+ xassert(((struct prefix *)atom)->size == size);
+ }
+ /* return atom to corresponding list of free atoms */
+ *(void **)atom = pool->avail[k];
+ pool->avail[k] = atom;
+ /* decrease number of allocated atoms */
+ xassert(pool->count > 0);
+ pool->count--;
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* dmp_in_use - determine how many atoms are still in use
+*
+* SYNOPSIS
+*
+* #include "dmp.h"
+* size_t dmp_in_use(DMP *pool);
+*
+* RETURNS
+*
+* The routine returns the number of atoms of the specified memory pool
+* which are still in use. */
+
+size_t dmp_in_use(DMP *pool)
+{ return
+ pool->count;
+}
+
+/***********************************************************************
+* NAME
+*
+* dmp_delete_pool - delete dynamic memory pool
+*
+* SYNOPSIS
+*
+* #include "dmp.h"
+* void dmp_delete_pool(DMP *pool);
+*
+* DESCRIPTION
+*
+* The routine dmp_delete_pool deletes the specified dynamic memory
+* pool freeing all the memory allocated to this object. */
+
+void dmp_delete_pool(DMP *pool)
+{ while (pool->block != NULL)
+ { void *block = pool->block;
+ pool->block = *(void **)block;
+ tfree(block);
+ }
+ tfree(pool);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/dmp.h b/test/monniaux/glpk-4.65/src/misc/dmp.h
new file mode 100644
index 00000000..85fe7176
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/dmp.h
@@ -0,0 +1,63 @@
+/* dmp.h (dynamic memory pool) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef DMP_H
+#define DMP_H
+
+#include "stdc.h"
+
+typedef struct DMP DMP;
+
+#define dmp_debug _glp_dmp_debug
+extern int dmp_debug;
+/* debug mode flag */
+
+#define dmp_create_pool _glp_dmp_create_pool
+DMP *dmp_create_pool(void);
+/* create dynamic memory pool */
+
+#define dmp_talloc(pool, type) \
+ ((type *)dmp_get_atom(pool, sizeof(type)))
+
+#define dmp_get_atom _glp_dmp_get_atom
+void *dmp_get_atom(DMP *pool, int size);
+/* get free atom from dynamic memory pool */
+
+#define dmp_tfree(pool, atom) \
+ dmp_free_atom(pool, atom, sizeof(*(atom)))
+
+#define dmp_free_atom _glp_dmp_free_atom
+void dmp_free_atom(DMP *pool, void *atom, int size);
+/* return atom to dynamic memory pool */
+
+#define dmp_in_use _glp_dmp_in_use
+size_t dmp_in_use(DMP *pool);
+/* determine how many atoms are still in use */
+
+#define dmp_delete_pool _glp_dmp_delete_pool
+void dmp_delete_pool(DMP *pool);
+/* delete dynamic memory pool */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/ffalg.c b/test/monniaux/glpk-4.65/src/misc/ffalg.c
new file mode 100644
index 00000000..4ea2913d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/ffalg.c
@@ -0,0 +1,221 @@
+/* ffalg.c (Ford-Fulkerson algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ffalg.h"
+
+/***********************************************************************
+* NAME
+*
+* ffalg - Ford-Fulkerson algorithm
+*
+* SYNOPSIS
+*
+* #include "ffalg.h"
+* void ffalg(int nv, int na, const int tail[], const int head[],
+* int s, int t, const int cap[], int x[], char cut[]);
+*
+* DESCRIPTION
+*
+* The routine ffalg implements the Ford-Fulkerson algorithm to find a
+* maximal flow in the specified flow network.
+*
+* INPUT PARAMETERS
+*
+* nv is the number of nodes, nv >= 2.
+*
+* na is the number of arcs, na >= 0.
+*
+* tail[a], a = 1,...,na, is the index of tail node of arc a.
+*
+* head[a], a = 1,...,na, is the index of head node of arc a.
+*
+* s is the source node index, 1 <= s <= nv.
+*
+* t is the sink node index, 1 <= t <= nv, t != s.
+*
+* cap[a], a = 1,...,na, is the capacity of arc a, cap[a] >= 0.
+*
+* NOTE: Multiple arcs are allowed, but self-loops are not allowed.
+*
+* OUTPUT PARAMETERS
+*
+* x[a], a = 1,...,na, is optimal value of the flow through arc a.
+*
+* cut[i], i = 1,...,nv, is 1 if node i is labelled, and 0 otherwise.
+* The set of arcs, whose one endpoint is labelled and other is not,
+* defines the minimal cut corresponding to the maximal flow found.
+* If the parameter cut is NULL, the cut information are not stored.
+*
+* REFERENCES
+*
+* L.R.Ford, Jr., and D.R.Fulkerson, "Flows in Networks," The RAND
+* Corp., Report R-375-PR (August 1962), Chap. I "Static Maximal Flow,"
+* pp.30-33. */
+
+void ffalg(int nv, int na, const int tail[], const int head[],
+ int s, int t, const int cap[], int x[], char cut[])
+{ int a, delta, i, j, k, pos1, pos2, temp,
+ *ptr, *arc, *link, *list;
+ /* sanity checks */
+ xassert(nv >= 2);
+ xassert(na >= 0);
+ xassert(1 <= s && s <= nv);
+ xassert(1 <= t && t <= nv);
+ xassert(s != t);
+ for (a = 1; a <= na; a++)
+ { i = tail[a], j = head[a];
+ xassert(1 <= i && i <= nv);
+ xassert(1 <= j && j <= nv);
+ xassert(i != j);
+ xassert(cap[a] >= 0);
+ }
+ /* allocate working arrays */
+ ptr = xcalloc(1+nv+1, sizeof(int));
+ arc = xcalloc(1+na+na, sizeof(int));
+ link = xcalloc(1+nv, sizeof(int));
+ list = xcalloc(1+nv, sizeof(int));
+ /* ptr[i] := (degree of node i) */
+ for (i = 1; i <= nv; i++)
+ ptr[i] = 0;
+ for (a = 1; a <= na; a++)
+ { ptr[tail[a]]++;
+ ptr[head[a]]++;
+ }
+ /* initialize arc pointers */
+ ptr[1]++;
+ for (i = 1; i < nv; i++)
+ ptr[i+1] += ptr[i];
+ ptr[nv+1] = ptr[nv];
+ /* build arc lists */
+ for (a = 1; a <= na; a++)
+ { arc[--ptr[tail[a]]] = a;
+ arc[--ptr[head[a]]] = a;
+ }
+ xassert(ptr[1] == 1);
+ xassert(ptr[nv+1] == na+na+1);
+ /* now the indices of arcs incident to node i are stored in
+ * locations arc[ptr[i]], arc[ptr[i]+1], ..., arc[ptr[i+1]-1] */
+ /* initialize arc flows */
+ for (a = 1; a <= na; a++)
+ x[a] = 0;
+loop: /* main loop starts here */
+ /* build augmenting tree rooted at s */
+ /* link[i] = 0 means that node i is not labelled yet;
+ * link[i] = a means that arc a immediately precedes node i */
+ /* initially node s is labelled as the root */
+ for (i = 1; i <= nv; i++)
+ link[i] = 0;
+ link[s] = -1, list[1] = s, pos1 = pos2 = 1;
+ /* breadth first search */
+ while (pos1 <= pos2)
+ { /* dequeue node i */
+ i = list[pos1++];
+ /* consider all arcs incident to node i */
+ for (k = ptr[i]; k < ptr[i+1]; k++)
+ { a = arc[k];
+ if (tail[a] == i)
+ { /* a = i->j is a forward arc from s to t */
+ j = head[a];
+ /* if node j has been labelled, skip the arc */
+ if (link[j] != 0) continue;
+ /* if the arc does not allow increasing the flow through
+ * it, skip the arc */
+ if (x[a] == cap[a]) continue;
+ }
+ else if (head[a] == i)
+ { /* a = i<-j is a backward arc from s to t */
+ j = tail[a];
+ /* if node j has been labelled, skip the arc */
+ if (link[j] != 0) continue;
+ /* if the arc does not allow decreasing the flow through
+ * it, skip the arc */
+ if (x[a] == 0) continue;
+ }
+ else
+ xassert(a != a);
+ /* label node j and enqueue it */
+ link[j] = a, list[++pos2] = j;
+ /* check for breakthrough */
+ if (j == t) goto brkt;
+ }
+ }
+ /* NONBREAKTHROUGH */
+ /* no augmenting path exists; current flow is maximal */
+ /* store minimal cut information, if necessary */
+ if (cut != NULL)
+ { for (i = 1; i <= nv; i++)
+ cut[i] = (char)(link[i] != 0);
+ }
+ goto done;
+brkt: /* BREAKTHROUGH */
+ /* walk through arcs of the augmenting path (s, ..., t) found in
+ * the reverse order and determine maximal change of the flow */
+ delta = 0;
+ for (j = t; j != s; j = i)
+ { /* arc a immediately precedes node j in the path */
+ a = link[j];
+ if (head[a] == j)
+ { /* a = i->j is a forward arc of the cycle */
+ i = tail[a];
+ /* x[a] may be increased until its upper bound */
+ temp = cap[a] - x[a];
+ }
+ else if (tail[a] == j)
+ { /* a = i<-j is a backward arc of the cycle */
+ i = head[a];
+ /* x[a] may be decreased until its lower bound */
+ temp = x[a];
+ }
+ else
+ xassert(a != a);
+ if (delta == 0 || delta > temp) delta = temp;
+ }
+ xassert(delta > 0);
+ /* increase the flow along the path */
+ for (j = t; j != s; j = i)
+ { /* arc a immediately precedes node j in the path */
+ a = link[j];
+ if (head[a] == j)
+ { /* a = i->j is a forward arc of the cycle */
+ i = tail[a];
+ x[a] += delta;
+ }
+ else if (tail[a] == j)
+ { /* a = i<-j is a backward arc of the cycle */
+ i = head[a];
+ x[a] -= delta;
+ }
+ else
+ xassert(a != a);
+ }
+ goto loop;
+done: /* free working arrays */
+ xfree(ptr);
+ xfree(arc);
+ xfree(link);
+ xfree(list);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/ffalg.h b/test/monniaux/glpk-4.65/src/misc/ffalg.h
new file mode 100644
index 00000000..7016f8fa
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/ffalg.h
@@ -0,0 +1,34 @@
+/* ffalg.h (Ford-Fulkerson algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef FFALG_H
+#define FFALG_H
+
+#define ffalg _glp_ffalg
+void ffalg(int nv, int na, const int tail[], const int head[],
+ int s, int t, const int cap[], int x[], char cut[]);
+/* Ford-Fulkerson algorithm */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/fp2rat.c b/test/monniaux/glpk-4.65/src/misc/fp2rat.c
new file mode 100644
index 00000000..4699bbd1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/fp2rat.c
@@ -0,0 +1,164 @@
+/* fp2rat.c (convert floating-point number to rational number) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+
+/***********************************************************************
+* NAME
+*
+* fp2rat - convert floating-point number to rational number
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* int fp2rat(double x, double eps, double *p, double *q);
+*
+* DESCRIPTION
+*
+* Given a floating-point number 0 <= x < 1 the routine fp2rat finds
+* its "best" rational approximation p / q, where p >= 0 and q > 0 are
+* integer numbers, such that |x - p / q| <= eps.
+*
+* RETURNS
+*
+* The routine fp2rat returns the number of iterations used to achieve
+* the specified precision eps.
+*
+* EXAMPLES
+*
+* For x = sqrt(2) - 1 = 0.414213562373095 and eps = 1e-6 the routine
+* gives p = 408 and q = 985, where 408 / 985 = 0.414213197969543.
+*
+* BACKGROUND
+*
+* It is well known that every positive real number x can be expressed
+* as the following continued fraction:
+*
+* x = b[0] + a[1]
+* ------------------------
+* b[1] + a[2]
+* -----------------
+* b[2] + a[3]
+* ----------
+* b[3] + ...
+*
+* where:
+*
+* a[k] = 1, k = 0, 1, 2, ...
+*
+* b[k] = floor(x[k]), k = 0, 1, 2, ...
+*
+* x[0] = x,
+*
+* x[k] = 1 / frac(x[k-1]), k = 1, 2, 3, ...
+*
+* To find the "best" rational approximation of x the routine computes
+* partial fractions f[k] by dropping after k terms as follows:
+*
+* f[k] = A[k] / B[k],
+*
+* where:
+*
+* A[-1] = 1, A[0] = b[0], B[-1] = 0, B[0] = 1,
+*
+* A[k] = b[k] * A[k-1] + a[k] * A[k-2],
+*
+* B[k] = b[k] * B[k-1] + a[k] * B[k-2].
+*
+* Once the condition
+*
+* |x - f[k]| <= eps
+*
+* has been satisfied, the routine reports p = A[k] and q = B[k] as the
+* final answer.
+*
+* In the table below here is some statistics obtained for one million
+* random numbers uniformly distributed in the range [0, 1).
+*
+* eps max p mean p max q mean q max k mean k
+* -------------------------------------------------------------
+* 1e-1 8 1.6 9 3.2 3 1.4
+* 1e-2 98 6.2 99 12.4 5 2.4
+* 1e-3 997 20.7 998 41.5 8 3.4
+* 1e-4 9959 66.6 9960 133.5 10 4.4
+* 1e-5 97403 211.7 97404 424.2 13 5.3
+* 1e-6 479669 669.9 479670 1342.9 15 6.3
+* 1e-7 1579030 2127.3 3962146 4257.8 16 7.3
+* 1e-8 26188823 6749.4 26188824 13503.4 19 8.2
+*
+* REFERENCES
+*
+* W. B. Jones and W. J. Thron, "Continued Fractions: Analytic Theory
+* and Applications," Encyclopedia on Mathematics and Its Applications,
+* Addison-Wesley, 1980. */
+
+int fp2rat(double x, double eps, double *p, double *q)
+{ int k;
+ double xk, Akm1, Ak, Bkm1, Bk, ak, bk, fk, temp;
+ xassert(0.0 <= x && x < 1.0);
+ for (k = 0; ; k++)
+ { xassert(k <= 100);
+ if (k == 0)
+ { /* x[0] = x */
+ xk = x;
+ /* A[-1] = 1 */
+ Akm1 = 1.0;
+ /* A[0] = b[0] = floor(x[0]) = 0 */
+ Ak = 0.0;
+ /* B[-1] = 0 */
+ Bkm1 = 0.0;
+ /* B[0] = 1 */
+ Bk = 1.0;
+ }
+ else
+ { /* x[k] = 1 / frac(x[k-1]) */
+ temp = xk - floor(xk);
+ xassert(temp != 0.0);
+ xk = 1.0 / temp;
+ /* a[k] = 1 */
+ ak = 1.0;
+ /* b[k] = floor(x[k]) */
+ bk = floor(xk);
+ /* A[k] = b[k] * A[k-1] + a[k] * A[k-2] */
+ temp = bk * Ak + ak * Akm1;
+ Akm1 = Ak, Ak = temp;
+ /* B[k] = b[k] * B[k-1] + a[k] * B[k-2] */
+ temp = bk * Bk + ak * Bkm1;
+ Bkm1 = Bk, Bk = temp;
+ }
+ /* f[k] = A[k] / B[k] */
+ fk = Ak / Bk;
+#if 0
+ print("%.*g / %.*g = %.*g",
+ DBL_DIG, Ak, DBL_DIG, Bk, DBL_DIG, fk);
+#endif
+ if (fabs(x - fk) <= eps)
+ break;
+ }
+ *p = Ak;
+ *q = Bk;
+ return k;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/fvs.c b/test/monniaux/glpk-4.65/src/misc/fvs.c
new file mode 100644
index 00000000..916a1bf9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/fvs.c
@@ -0,0 +1,137 @@
+/* fvs.c (sparse vector in FVS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "fvs.h"
+
+void fvs_alloc_vec(FVS *x, int n)
+{ /* allocate sparse vector */
+ int j;
+ xassert(n >= 0);
+ x->n = n;
+ x->nnz = 0;
+ x->ind = talloc(1+n, int);
+ x->vec = talloc(1+n, double);
+ for (j = 1; j <= n; j++)
+ x->vec[j] = 0.0;
+ return;
+}
+
+void fvs_check_vec(const FVS *x)
+{ /* check sparse vector */
+ /* NOTE: for testing/debugging only */
+ int n = x->n;
+ int nnz = x->nnz;
+ int *ind = x->ind;
+ double *vec = x->vec;
+ char *map;
+ int j, k;
+ xassert(n >= 0);
+ xassert(0 <= nnz && nnz <= n);
+ map = talloc(1+n, char);
+ for (j = 1; j <= n; j++)
+ map[j] = (vec[j] != 0.0);
+ for (k = 1; k <= nnz; k++)
+ { j = ind[k];
+ xassert(1 <= j && j <= n);
+ xassert(map[j]);
+ map[j] = 0;
+ }
+ for (j = 1; j <= n; j++)
+ xassert(!map[j]);
+ tfree(map);
+ return;
+}
+
+void fvs_gather_vec(FVS *x, double eps)
+{ /* gather sparse vector */
+ int n = x->n;
+ int *ind = x->ind;
+ double *vec = x->vec;
+ int j, nnz = 0;
+ for (j = n; j >= 1; j--)
+ { if (-eps < vec[j] && vec[j] < +eps)
+ vec[j] = 0.0;
+ else
+ ind[++nnz] = j;
+ }
+ x->nnz = nnz;
+ return;
+}
+
+void fvs_clear_vec(FVS *x)
+{ /* clear sparse vector */
+ int *ind = x->ind;
+ double *vec = x->vec;
+ int k;
+ for (k = x->nnz; k >= 1; k--)
+ vec[ind[k]] = 0.0;
+ x->nnz = 0;
+ return;
+}
+
+void fvs_copy_vec(FVS *x, const FVS *y)
+{ /* copy sparse vector */
+ int *x_ind = x->ind;
+ double *x_vec = x->vec;
+ int *y_ind = y->ind;
+ double *y_vec = y->vec;
+ int j, k;
+ xassert(x != y);
+ xassert(x->n == y->n);
+ fvs_clear_vec(x);
+ for (k = x->nnz = y->nnz; k >= 1; k--)
+ { j = x_ind[k] = y_ind[k];
+ x_vec[j] = y_vec[j];
+ }
+ return;
+}
+
+void fvs_adjust_vec(FVS *x, double eps)
+{ /* replace tiny vector elements by exact zeros */
+ int nnz = x->nnz;
+ int *ind = x->ind;
+ double *vec = x->vec;
+ int j, k, cnt = 0;
+ for (k = 1; k <= nnz; k++)
+ { j = ind[k];
+ if (-eps < vec[j] && vec[j] < +eps)
+ vec[j] = 0.0;
+ else
+ ind[++cnt] = j;
+ }
+ x->nnz = cnt;
+ return;
+}
+
+void fvs_free_vec(FVS *x)
+{ /* deallocate sparse vector */
+ tfree(x->ind);
+ tfree(x->vec);
+ x->n = x->nnz = -1;
+ x->ind = NULL;
+ x->vec = NULL;
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/fvs.h b/test/monniaux/glpk-4.65/src/misc/fvs.h
new file mode 100644
index 00000000..abfed8cc
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/fvs.h
@@ -0,0 +1,76 @@
+/* fvs.h (sparse vector in FVS format) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef FVS_H
+#define FVS_H
+
+typedef struct FVS FVS;
+
+struct FVS
+{ /* sparse vector in FVS (Full Vector Storage) format */
+ int n;
+ /* vector dimension (total number of elements) */
+ int nnz;
+ /* number of non-zero elements, 0 <= nnz <= n */
+ int *ind; /* int ind[1+n]; */
+ /* ind[0] is not used;
+ * ind[k] = j, 1 <= k <= nnz, means that vec[j] != 0
+ * non-zero indices in the array ind are stored in arbitrary
+ * order; if vec[j] = 0, its index j SHOULD NOT be presented in
+ * the array ind */
+ double *vec; /* double vec[1+n]; */
+ /* vec[0] is not used;
+ * vec[j], 1 <= j <= n, is a numeric value of j-th element */
+};
+
+#define fvs_alloc_vec _glp_fvs_alloc_vec
+void fvs_alloc_vec(FVS *x, int n);
+/* allocate sparse vector */
+
+#define fvs_check_vec _glp_fvs_check_vec
+void fvs_check_vec(const FVS *x);
+/* check sparse vector */
+
+#define fvs_gather_vec _glp_fvs_gather_vec
+void fvs_gather_vec(FVS *x, double eps);
+/* gather sparse vector */
+
+#define fvs_clear_vec _glp_fvs_clear_vec
+void fvs_clear_vec(FVS *x);
+/* clear sparse vector */
+
+#define fvs_copy_vec _glp_fvs_copy_vec
+void fvs_copy_vec(FVS *x, const FVS *y);
+/* copy sparse vector */
+
+#define fvs_adjust_vec _glp_fvs_adjust_vec
+void fvs_adjust_vec(FVS *x, double eps);
+/* replace tiny vector elements by exact zeros */
+
+#define fvs_free_vec _glp_fvs_free_vec
+void fvs_free_vec(FVS *x);
+/* deallocate sparse vector */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/gcd.c b/test/monniaux/glpk-4.65/src/misc/gcd.c
new file mode 100644
index 00000000..95c48cc0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/gcd.c
@@ -0,0 +1,102 @@
+/* gcd.c (greatest common divisor) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+
+/***********************************************************************
+* NAME
+*
+* gcd - find greatest common divisor of two integers
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* int gcd(int x, int y);
+*
+* RETURNS
+*
+* The routine gcd returns gcd(x, y), the greatest common divisor of
+* the two positive integers given.
+*
+* ALGORITHM
+*
+* The routine gcd is based on Euclid's algorithm.
+*
+* REFERENCES
+*
+* Don Knuth, The Art of Computer Programming, Vol.2: Seminumerical
+* Algorithms, 3rd Edition, Addison-Wesley, 1997. Section 4.5.2: The
+* Greatest Common Divisor, pp. 333-56. */
+
+int gcd(int x, int y)
+{ int r;
+ xassert(x > 0 && y > 0);
+ while (y > 0)
+ r = x % y, x = y, y = r;
+ return x;
+}
+
+/***********************************************************************
+* NAME
+*
+* gcdn - find greatest common divisor of n integers
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* int gcdn(int n, int x[]);
+*
+* RETURNS
+*
+* The routine gcdn returns gcd(x[1], x[2], ..., x[n]), the greatest
+* common divisor of n positive integers given, n > 0.
+*
+* BACKGROUND
+*
+* The routine gcdn is based on the following identity:
+*
+* gcd(x, y, z) = gcd(gcd(x, y), z).
+*
+* REFERENCES
+*
+* Don Knuth, The Art of Computer Programming, Vol.2: Seminumerical
+* Algorithms, 3rd Edition, Addison-Wesley, 1997. Section 4.5.2: The
+* Greatest Common Divisor, pp. 333-56. */
+
+int gcdn(int n, int x[])
+{ int d, j;
+ xassert(n > 0);
+ for (j = 1; j <= n; j++)
+ { xassert(x[j] > 0);
+ if (j == 1)
+ d = x[1];
+ else
+ d = gcd(d, x[j]);
+ if (d == 1)
+ break;
+ }
+ return d;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/jd.c b/test/monniaux/glpk-4.65/src/misc/jd.c
new file mode 100644
index 00000000..c9d63171
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/jd.c
@@ -0,0 +1,152 @@
+/* jd.c (conversions between calendar date and Julian day number) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include <stddef.h>
+#include "jd.h"
+
+/***********************************************************************
+* NAME
+*
+* jday - convert calendar date to Julian day number
+*
+* SYNOPSIS
+*
+* #include "jd.h"
+* int jday(int d, int m, int y);
+*
+* DESCRIPTION
+*
+* The routine jday converts a calendar date, Gregorian calendar, to
+* corresponding Julian day number j.
+*
+* From the given day d, month m, and year y, the Julian day number j
+* is computed without using tables.
+*
+* The routine is valid for 1 <= y <= 4000.
+*
+* RETURNS
+*
+* The routine jday returns the Julian day number, or negative value if
+* the specified date is incorrect.
+*
+* REFERENCES
+*
+* R. G. Tantzen, Algorithm 199: conversions between calendar date and
+* Julian day number, Communications of the ACM, vol. 6, no. 8, p. 444,
+* Aug. 1963. */
+
+int jday(int d, int m, int y)
+{ int c, ya, j, dd;
+ if (!(1 <= d && d <= 31 &&
+ 1 <= m && m <= 12 &&
+ 1 <= y && y <= 4000))
+ return -1;
+ if (m >= 3)
+ m -= 3;
+ else
+ m += 9, y--;
+ c = y / 100;
+ ya = y - 100 * c;
+ j = (146097 * c) / 4 + (1461 * ya) / 4 + (153 * m + 2) / 5 + d +
+ 1721119;
+ jdate(j, &dd, NULL, NULL);
+ if (d != dd)
+ return -1;
+ return j;
+}
+
+/***********************************************************************
+* NAME
+*
+* jdate - convert Julian day number to calendar date
+*
+* SYNOPSIS
+*
+* #include "jd.h"
+* int jdate(int j, int *d, int *m, int *y);
+*
+* DESCRIPTION
+*
+* The routine jdate converts a Julian day number j to corresponding
+* calendar date, Gregorian calendar.
+*
+* The day d, month m, and year y are computed without using tables and
+* stored in corresponding locations.
+*
+* The routine is valid for 1721426 <= j <= 3182395.
+*
+* RETURNS
+*
+* If the conversion is successful, the routine returns zero, otherwise
+* non-zero.
+*
+* REFERENCES
+*
+* R. G. Tantzen, Algorithm 199: conversions between calendar date and
+* Julian day number, Communications of the ACM, vol. 6, no. 8, p. 444,
+* Aug. 1963. */
+
+int jdate(int j, int *d_, int *m_, int *y_)
+{ int d, m, y;
+ if (!(1721426 <= j && j <= 3182395))
+ return 1;
+ j -= 1721119;
+ y = (4 * j - 1) / 146097;
+ j = (4 * j - 1) % 146097;
+ d = j / 4;
+ j = (4 * d + 3) / 1461;
+ d = (4 * d + 3) % 1461;
+ d = (d + 4) / 4;
+ m = (5 * d - 3) / 153;
+ d = (5 * d - 3) % 153;
+ d = (d + 5) / 5;
+ y = 100 * y + j;
+ if (m <= 9)
+ m += 3;
+ else m -= 9,
+ y++;
+ if (d_ != NULL) *d_ = d;
+ if (m_ != NULL) *m_ = m;
+ if (y_ != NULL) *y_ = y;
+ return 0;
+}
+
+#ifdef GLP_TEST
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void)
+{ int jbeg, jend, j, d, m, y;
+ jbeg = jday(1, 1, 1);
+ jend = jday(31, 12, 4000);
+ for (j = jbeg; j <= jend; j++)
+ { assert(jdate(j, &d, &m, &y) == 0);
+ assert(jday(d, m, y) == j);
+ }
+ printf("Routines jday and jdate work correctly.\n");
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/jd.h b/test/monniaux/glpk-4.65/src/misc/jd.h
new file mode 100644
index 00000000..009d2daa
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/jd.h
@@ -0,0 +1,32 @@
+/* jd.h (conversions between calendar date and Julian day number) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#define jday _glp_jday
+int jday(int d, int m, int y);
+/* convert calendar date to Julian day number */
+
+#define jdate _glp_jdate
+int jdate(int j, int *d, int *m, int *y);
+/* convert Julian day number to calendar date */
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/keller.c b/test/monniaux/glpk-4.65/src/misc/keller.c
new file mode 100644
index 00000000..d64d3c1e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/keller.c
@@ -0,0 +1,235 @@
+/* keller.c (cover edges by cliques, Kellerman's heuristic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "glpk.h"
+#include "env.h"
+#include "keller.h"
+
+/***********************************************************************
+* NAME
+*
+* kellerman - cover edges by cliques with Kellerman's heuristic
+*
+* SYNOPSIS
+*
+* #include "keller.h"
+* int kellerman(int n, int (*func)(void *info, int i, int ind[]),
+* void *info, glp_graph *H);
+*
+* DESCRIPTION
+*
+* The routine kellerman implements Kellerman's heuristic algorithm
+* to find a minimal set of cliques which cover all edges of specified
+* graph G = (V, E).
+*
+* The parameter n specifies the number of vertices |V|, n >= 0.
+*
+* Formal routine func specifies the set of edges E in the following
+* way. Running the routine kellerman calls the routine func and passes
+* to it parameter i, which is the number of some vertex, 1 <= i <= n.
+* In response the routine func should store numbers of all vertices
+* adjacent to vertex i to locations ind[1], ind[2], ..., ind[len] and
+* return the value of len, which is the number of adjacent vertices,
+* 0 <= len <= n. Self-loops are allowed, but ignored. Multiple edges
+* are not allowed.
+*
+* The parameter info is a transit pointer (magic cookie) passed to the
+* formal routine func as its first parameter.
+*
+* The result provided by the routine kellerman is the bipartite graph
+* H = (V union C, F), which defines the covering found. (The program
+* object of type glp_graph specified by the parameter H should be
+* previously created with the routine glp_create_graph. On entry the
+* routine kellerman erases the content of this object with the routine
+* glp_erase_graph.) Vertices of first part V correspond to vertices of
+* the graph G and have the same ordinal numbers 1, 2, ..., n. Vertices
+* of second part C correspond to cliques and have ordinal numbers
+* n+1, n+2, ..., n+k, where k is the total number of cliques in the
+* edge covering found. Every edge f in F in the program object H is
+* represented as arc f = (i->j), where i in V and j in C, which means
+* that vertex i of the graph G is in clique C[j], 1 <= j <= k. (Thus,
+* if two vertices of the graph G are in the same clique, these vertices
+* are adjacent in G, and corresponding edge is covered by that clique.)
+*
+* RETURNS
+*
+* The routine Kellerman returns k, the total number of cliques in the
+* edge covering found.
+*
+* REFERENCE
+*
+* For more details see: glpk/doc/notes/keller.pdf (in Russian). */
+
+struct set
+{ /* set of vertices */
+ int size;
+ /* size (cardinality) of the set, 0 <= card <= n */
+ int *list; /* int list[1+n]; */
+ /* the set contains vertices list[1,...,size] */
+ int *pos; /* int pos[1+n]; */
+ /* pos[i] > 0 means that vertex i is in the set and
+ * list[pos[i]] = i; pos[i] = 0 means that vertex i is not in
+ * the set */
+};
+
+int kellerman(int n, int (*func)(void *info, int i, int ind[]),
+ void *info, void /* glp_graph */ *H_)
+{ glp_graph *H = H_;
+ struct set W_, *W = &W_, V_, *V = &V_;
+ glp_arc *a;
+ int i, j, k, m, t, len, card, best;
+ xassert(n >= 0);
+ /* H := (V, 0; 0), where V is the set of vertices of graph G */
+ glp_erase_graph(H, H->v_size, H->a_size);
+ glp_add_vertices(H, n);
+ /* W := 0 */
+ W->size = 0;
+ W->list = xcalloc(1+n, sizeof(int));
+ W->pos = xcalloc(1+n, sizeof(int));
+ memset(&W->pos[1], 0, sizeof(int) * n);
+ /* V := 0 */
+ V->size = 0;
+ V->list = xcalloc(1+n, sizeof(int));
+ V->pos = xcalloc(1+n, sizeof(int));
+ memset(&V->pos[1], 0, sizeof(int) * n);
+ /* main loop */
+ for (i = 1; i <= n; i++)
+ { /* W must be empty */
+ xassert(W->size == 0);
+ /* W := { j : i > j and (i,j) in E } */
+ len = func(info, i, W->list);
+ xassert(0 <= len && len <= n);
+ for (t = 1; t <= len; t++)
+ { j = W->list[t];
+ xassert(1 <= j && j <= n);
+ if (j >= i) continue;
+ xassert(W->pos[j] == 0);
+ W->list[++W->size] = j, W->pos[j] = W->size;
+ }
+ /* on i-th iteration we need to cover edges (i,j) for all
+ * j in W */
+ /* if W is empty, it is a special case */
+ if (W->size == 0)
+ { /* set k := k + 1 and create new clique C[k] = { i } */
+ k = glp_add_vertices(H, 1) - n;
+ glp_add_arc(H, i, n + k);
+ continue;
+ }
+ /* try to include vertex i into existing cliques */
+ /* V must be empty */
+ xassert(V->size == 0);
+ /* k is the number of cliques found so far */
+ k = H->nv - n;
+ for (m = 1; m <= k; m++)
+ { /* do while V != W; since here V is within W, we can use
+ * equivalent condition: do while |V| < |W| */
+ if (V->size == W->size) break;
+ /* check if C[m] is within W */
+ for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
+ { j = a->tail->i;
+ if (W->pos[j] == 0) break;
+ }
+ if (a != NULL) continue;
+ /* C[m] is within W, expand clique C[m] with vertex i */
+ /* C[m] := C[m] union {i} */
+ glp_add_arc(H, i, n + m);
+ /* V is a set of vertices whose incident edges are already
+ * covered by existing cliques */
+ /* V := V union C[m] */
+ for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
+ { j = a->tail->i;
+ if (V->pos[j] == 0)
+ V->list[++V->size] = j, V->pos[j] = V->size;
+ }
+ }
+ /* remove from set W the vertices whose incident edges are
+ * already covered by existing cliques */
+ /* W := W \ V, V := 0 */
+ for (t = 1; t <= V->size; t++)
+ { j = V->list[t], V->pos[j] = 0;
+ if (W->pos[j] != 0)
+ { /* remove vertex j from W */
+ if (W->pos[j] != W->size)
+ { int jj = W->list[W->size];
+ W->list[W->pos[j]] = jj;
+ W->pos[jj] = W->pos[j];
+ }
+ W->size--, W->pos[j] = 0;
+ }
+ }
+ V->size = 0;
+ /* now set W contains only vertices whose incident edges are
+ * still not covered by existing cliques; create new cliques
+ * to cover remaining edges until set W becomes empty */
+ while (W->size > 0)
+ { /* find clique C[m], 1 <= m <= k, which shares maximal
+ * number of vertices with W; to break ties choose clique
+ * having smallest number m */
+ m = 0, best = -1;
+ k = H->nv - n;
+ for (t = 1; t <= k; t++)
+ { /* compute cardinality of intersection of W and C[t] */
+ card = 0;
+ for (a = H->v[n + t]->in; a != NULL; a = a->h_next)
+ { j = a->tail->i;
+ if (W->pos[j] != 0) card++;
+ }
+ if (best < card)
+ m = t, best = card;
+ }
+ xassert(m > 0);
+ /* set k := k + 1 and create new clique:
+ * C[k] := (W intersect C[m]) union { i }, which covers all
+ * edges incident to vertices from (W intersect C[m]) */
+ k = glp_add_vertices(H, 1) - n;
+ for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
+ { j = a->tail->i;
+ if (W->pos[j] != 0)
+ { /* vertex j is in both W and C[m]; include it in new
+ * clique C[k] */
+ glp_add_arc(H, j, n + k);
+ /* remove vertex j from W, since edge (i,j) will be
+ * covered by new clique C[k] */
+ if (W->pos[j] != W->size)
+ { int jj = W->list[W->size];
+ W->list[W->pos[j]] = jj;
+ W->pos[jj] = W->pos[j];
+ }
+ W->size--, W->pos[j] = 0;
+ }
+ }
+ /* include vertex i to new clique C[k] to cover edges (i,j)
+ * incident to all vertices j just removed from W */
+ glp_add_arc(H, i, n + k);
+ }
+ }
+ /* free working arrays */
+ xfree(W->list);
+ xfree(W->pos);
+ xfree(V->list);
+ xfree(V->pos);
+ /* return the number of cliques in the edge covering found */
+ return H->nv - n;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/keller.h b/test/monniaux/glpk-4.65/src/misc/keller.h
new file mode 100644
index 00000000..d7a5b343
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/keller.h
@@ -0,0 +1,34 @@
+/* keller.h (cover edges by cliques, Kellerman's heuristic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef KELLER_H
+#define KELLER_H
+
+#define kellerman _glp_kellerman
+int kellerman(int n, int (*func)(void *info, int i, int ind[]),
+ void *info, void /* glp_graph */ *H);
+/* cover edges by cliques with Kellerman's heuristic */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/ks.c b/test/monniaux/glpk-4.65/src/misc/ks.c
new file mode 100644
index 00000000..0720cc90
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/ks.c
@@ -0,0 +1,466 @@
+/* ks.c (0-1 knapsack problem) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ks.h"
+#include "mt1.h"
+
+/***********************************************************************
+* 0-1 knapsack problem has the following formulation:
+*
+* maximize z = sum{j in 1..n} c[j]x[j] (1)
+*
+* s.t. sum{j in 1..n} a[j]x[j] <= b (2)
+*
+* x[j] in {0, 1} for all j in 1..n (3)
+*
+* In general case it is assumed that the instance is non-normalized,
+* i.e. parameters a, b, and c may have any sign.
+***********************************************************************/
+
+/***********************************************************************
+* ks_enum - solve 0-1 knapsack problem by complete enumeration
+*
+* This routine finds optimal solution to 0-1 knapsack problem (1)-(3)
+* by complete enumeration. It is intended mainly for testing purposes.
+*
+* The instance to be solved is specified by parameters n, a, b, and c.
+* Note that these parameters can have any sign, i.e. normalization is
+* not needed.
+*
+* On exit the routine stores the optimal point found in locations
+* x[1], ..., x[n] and returns the optimal objective value. However, if
+* the instance is infeasible, the routine returns INT_MIN.
+*
+* Since the complete enumeration is inefficient, this routine can be
+* used only for small instances (n <= 20-30). */
+
+#define N_MAX 40
+
+int ks_enum(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/])
+{ int j, s, z, z_best;
+ char x_best[1+N_MAX];
+ xassert(0 <= n && n <= N_MAX);
+ /* initialization */
+ memset(&x[1], 0, n * sizeof(char));
+ z_best = INT_MIN;
+loop: /* compute constraint and objective at current x */
+ s = z = 0;
+ for (j = 1; j <= n; j++)
+ { if (x[j])
+ s += a[j], z += c[j];
+ }
+ /* check constraint violation */
+ if (s > b)
+ goto next;
+ /* check objective function */
+ if (z_best < z)
+ { /* better solution has been found */
+ memcpy(&x_best[1], &x[1], n * sizeof(char));
+ z_best = z;
+ }
+next: /* generate next x */
+ for (j = 1; j <= n; j++)
+ { if (!x[j])
+ { x[j] = 1;
+ goto loop;
+ }
+ x[j] = 0;
+ }
+ /* report best (optimal) solution */
+ memcpy(&x[1], &x_best[1], n * sizeof(char));
+ return z_best;
+}
+
+/***********************************************************************
+* reduce - prepare reduced instance of 0-1 knapsack
+*
+* Given original instance of 0-1 knapsack (1)-(3) specified by the
+* parameters n, a, b, and c this routine transforms it to equivalent
+* reduced instance in the same format. The reduced instance is
+* normalized, i.e. the following additional conditions are met:
+*
+* n >= 2 (4)
+*
+* 1 <= a[j] <= b for all j in 1..n (5)
+*
+* sum{j in 1..n} a[j] >= b+1 (6)
+*
+* c[j] >= 1 for all j in 1..n (7)
+*
+* The routine creates the structure ks and stores there parameters n,
+* a, b, and c of the reduced instance as well as template of solution
+* to original instance.
+*
+* Normally the routine returns a pointer to the structure ks created.
+* However, if the original instance is infeasible, the routine returns
+* a null pointer. */
+
+struct ks
+{ int orig_n;
+ /* original problem dimension */
+ int n;
+ /* reduced problem dimension */
+ int *a; /* int a[1+orig_n]; */
+ /* a{j in 1..n} are constraint coefficients (2) */
+ int b;
+ /* b is constraint right-hand side (2) */
+ int *c; /* int c[1+orig_n]; */
+ /* c{j in 1..n} are objective coefficients (1) */
+ int c0;
+ /* c0 is objective constant term */
+ char *x; /* char x[1+orig_n]; */
+ /* x{j in 1..orig_n} is solution template to original instance:
+ * x[j] = 0 x[j] is fixed at 0
+ * x[j] = 1 x[j] is fixed at 1
+ * x[j] = 0x10 x[j] = x[j']
+ * x[j] = 0x11 x[j] = 1 - x[j']
+ * where x[j'] is corresponding solution to reduced instance */
+};
+
+static void free_ks(struct ks *ks);
+
+static struct ks *reduce(const int n, const int a[/*1+n*/], int b,
+ const int c[/*1+n*/])
+{ struct ks *ks;
+ int j, s;
+ xassert(n >= 0);
+ /* initially reduced instance is the same as original one */
+ ks = talloc(1, struct ks);
+ ks->orig_n = n;
+ ks->n = 0;
+ ks->a = talloc(1+n, int);
+ memcpy(&ks->a[1], &a[1], n * sizeof(int));
+ ks->b = b;
+ ks->c = talloc(1+n, int);
+ memcpy(&ks->c[1], &c[1], n * sizeof(int));
+ ks->c0 = 0;
+ ks->x = talloc(1+n, char);
+ /* make all a[j] non-negative */
+ for (j = 1; j <= n; j++)
+ { if (a[j] >= 0)
+ { /* keep original x[j] */
+ ks->x[j] = 0x10;
+ }
+ else /* a[j] < 0 */
+ { /* substitute x[j] = 1 - x'[j] */
+ ks->x[j] = 0x11;
+ /* ... + a[j]x[j] + ... <= b
+ * ... + a[j](1 - x'[j]) + ... <= b
+ * ... - a[j]x'[j] + ... <= b - a[j] */
+ ks->a[j] = - ks->a[j];
+ ks->b += ks->a[j];
+ /* z = ... + c[j]x[j] + ... + c0 =
+ * = ... + c[j](1 - x'[j]) + ... + c0 =
+ * = ... - c[j]x'[j] + ... + (c0 + c[j]) */
+ ks->c0 += ks->c[j];
+ ks->c[j] = - ks->c[j];
+ }
+ }
+ /* now a[j] >= 0 for all j in 1..n */
+ if (ks->b < 0)
+ { /* instance is infeasible */
+ free_ks(ks);
+ return NULL;
+ }
+ /* build reduced instance */
+ for (j = 1; j <= n; j++)
+ { if (ks->a[j] == 0)
+ { if (ks->c[j] <= 0)
+ { /* fix x[j] at 0 */
+ ks->x[j] ^= 0x10;
+ }
+ else
+ { /* fix x[j] at 1 */
+ ks->x[j] ^= 0x11;
+ ks->c0 += ks->c[j];
+ }
+ }
+ else if (ks->a[j] > ks->b || ks->c[j] <= 0)
+ { /* fix x[j] at 0 */
+ ks->x[j] ^= 0x10;
+ }
+ else
+ { /* include x[j] in reduced instance */
+ ks->n++;
+ ks->a[ks->n] = ks->a[j];
+ ks->c[ks->n] = ks->c[j];
+ }
+ }
+ /* now conditions (5) and (7) are met */
+ /* check condition (6) */
+ s = 0;
+ for (j = 1; j <= ks->n; j++)
+ { xassert(1 <= ks->a[j] && ks->a[j] <= ks->b);
+ xassert(ks->c[j] >= 1);
+ s += ks->a[j];
+ }
+ if (s <= ks->b)
+ { /* sum{j in 1..n} a[j] <= b */
+ /* fix all remaining x[j] at 1 to obtain trivial solution */
+ for (j = 1; j <= n; j++)
+ { if (ks->x[j] & 0x10)
+ ks->x[j] ^= 0x11;
+ }
+ for (j = 1; j <= ks->n; j++)
+ ks->c0 += ks->c[j];
+ /* reduced instance is empty */
+ ks->n = 0;
+ }
+ /* here n = 0 or n >= 2 due to condition (6) */
+ xassert(ks->n == 0 || ks->n >= 2);
+ return ks;
+}
+
+/***********************************************************************
+* restore - restore solution to original 0-1 knapsack instance
+*
+* Given optimal solution x{j in 1..ks->n} to the reduced 0-1 knapsack
+* instance (previously prepared by the routine reduce) this routine
+* constructs optimal solution to the original instance and stores it
+* in the array ks->x{j in 1..ks->orig_n}.
+*
+* On exit the routine returns optimal objective value for the original
+* instance.
+*
+* NOTE: This operation should be performed only once. */
+
+static int restore(struct ks *ks, char x[])
+{ int j, k, z;
+ z = ks->c0;
+ for (j = 1, k = 0; j <= ks->orig_n; j++)
+ { if (ks->x[j] & 0x10)
+ { k++;
+ xassert(k <= ks->n);
+ xassert(x[k] == 0 || x[k] == 1);
+ if (ks->x[j] & 1)
+ ks->x[j] = 1 - x[k];
+ else
+ ks->x[j] = x[k];
+ if (x[k])
+ z += ks->c[k];
+ }
+ }
+ xassert(k == ks->n);
+ return z;
+}
+
+/***********************************************************************
+* free_ks - deallocate structure ks
+*
+* This routine frees memory previously allocated to the structure ks
+* and all its components. */
+
+static void free_ks(struct ks *ks)
+{ xassert(ks != NULL);
+ tfree(ks->a);
+ tfree(ks->c);
+ tfree(ks->x);
+ tfree(ks);
+}
+
+/***********************************************************************
+* ks_mt1 - solve 0-1 knapsack problem with Martello & Toth algorithm
+*
+* This routine finds optimal solution to 0-1 knapsack problem (1)-(3)
+* with Martello & Toth algorithm MT1.
+*
+* The instance to be solved is specified by parameters n, a, b, and c.
+* Note that these parameters can have any sign, i.e. normalization is
+* not needed.
+*
+* On exit the routine stores the optimal point found in locations
+* x[1], ..., x[n] and returns the optimal objective value. However, if
+* the instance is infeasible, the routine returns INT_MIN.
+*
+* REFERENCES
+*
+* S.Martello, P.Toth. Knapsack Problems: Algorithms and Computer Imp-
+* lementations. John Wiley & Sons, 1990. */
+
+struct mt
+{ int j;
+ float r; /* r[j] = c[j] / a[j] */
+};
+
+static int CDECL fcmp(const void *p1, const void *p2)
+{ if (((struct mt *)p1)->r > ((struct mt *)p2)->r)
+ return -1;
+ else if (((struct mt *)p1)->r < ((struct mt *)p2)->r)
+ return +1;
+ else
+ return 0;
+}
+
+static int mt1a(int n, const int a[], int b, const int c[], char x[])
+{ /* interface routine to MT1 */
+ struct mt *mt;
+ int j, z, *p, *w, *x1, *xx, *min, *psign, *wsign, *zsign;
+ xassert(n >= 2);
+ /* allocate working arrays */
+ mt = talloc(1+n, struct mt);
+ p = talloc(1+n+1, int);
+ w = talloc(1+n+1, int);
+ x1 = talloc(1+n+1, int);
+ xx = talloc(1+n+1, int);
+ min = talloc(1+n+1, int);
+ psign = talloc(1+n+1, int);
+ wsign = talloc(1+n+1, int);
+ zsign = talloc(1+n+1, int);
+ /* reorder items to provide c[j] / a[j] >= a[j+1] / a[j+1] */
+ for (j = 1; j <= n; j++)
+ { mt[j].j = j;
+ mt[j].r = (float)c[j] / (float)a[j];
+ }
+ qsort(&mt[1], n, sizeof(struct mt), fcmp);
+ /* load instance parameters */
+ for (j = 1; j <= n; j++)
+ { p[j] = c[mt[j].j];
+ w[j] = a[mt[j].j];
+ }
+ /* find optimal solution */
+ z = mt1(n, p, w, b, x1, 1, xx, min, psign, wsign, zsign);
+ xassert(z >= 0);
+ /* store optimal point found */
+ for (j = 1; j <= n; j++)
+ { xassert(x1[j] == 0 || x1[j] == 1);
+ x[mt[j].j] = x1[j];
+ }
+ /* free working arrays */
+ tfree(mt);
+ tfree(p);
+ tfree(w);
+ tfree(x1);
+ tfree(xx);
+ tfree(min);
+ tfree(psign);
+ tfree(wsign);
+ tfree(zsign);
+ return z;
+}
+
+int ks_mt1(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/])
+{ struct ks *ks;
+ int j, s1, s2, z;
+ xassert(n >= 0);
+ /* prepare reduced instance */
+ ks = reduce(n, a, b, c);
+ if (ks == NULL)
+ { /* original instance is infeasible */
+ return INT_MIN;
+ }
+ /* find optimal solution to reduced instance */
+ if (ks->n > 0)
+ mt1a(ks->n, ks->a, ks->b, ks->c, x);
+ /* restore solution to original instance */
+ z = restore(ks, x);
+ memcpy(&x[1], &ks->x[1], n * sizeof(char));
+ free_ks(ks);
+ /* check solution found */
+ s1 = s2 = 0;
+ for (j = 1; j <= n; j++)
+ { xassert(x[j] == 0 || x[j] == 1);
+ if (x[j])
+ s1 += a[j], s2 += c[j];
+ }
+ xassert(s1 <= b);
+ xassert(s2 == z);
+ return z;
+}
+
+/***********************************************************************
+* ks_greedy - solve 0-1 knapsack problem with greedy heuristic
+*
+* This routine finds (sub)optimal solution to 0-1 knapsack problem
+* (1)-(3) with greedy heuristic.
+*
+* The instance to be solved is specified by parameters n, a, b, and c.
+* Note that these parameters can have any sign, i.e. normalization is
+* not needed.
+*
+* On exit the routine stores the optimal point found in locations
+* x[1], ..., x[n] and returns the optimal objective value. However, if
+* the instance is infeasible, the routine returns INT_MIN. */
+
+static int greedy(int n, const int a[], int b, const int c[], char x[])
+{ /* core routine for normalized 0-1 knapsack instance */
+ struct mt *mt;
+ int j, s, z;
+ xassert(n >= 2);
+ /* reorder items to provide c[j] / a[j] >= a[j+1] / a[j+1] */
+ mt = talloc(1+n, struct mt);
+ for (j = 1; j <= n; j++)
+ { mt[j].j = j;
+ mt[j].r = (float)c[j] / (float)a[j];
+ }
+ qsort(&mt[1], n, sizeof(struct mt), fcmp);
+ /* take items starting from most valuable ones until the knapsack
+ * is full */
+ s = z = 0;
+ for (j = 1; j <= n; j++)
+ { if (s + a[mt[j].j] > b)
+ break;
+ x[mt[j].j] = 1;
+ s += a[mt[j].j];
+ z += c[mt[j].j];
+ }
+ /* don't take remaining items */
+ for (j = j; j <= n; j++)
+ x[mt[j].j] = 0;
+ tfree(mt);
+ return z;
+}
+
+int ks_greedy(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/])
+{ struct ks *ks;
+ int j, s1, s2, z;
+ xassert(n >= 0);
+ /* prepare reduced instance */
+ ks = reduce(n, a, b, c);
+ if (ks == NULL)
+ { /* original instance is infeasible */
+ return INT_MIN;
+ }
+ /* find suboptimal solution to reduced instance */
+ if (ks->n > 0)
+ greedy(ks->n, ks->a, ks->b, ks->c, x);
+ /* restore solution to original instance */
+ z = restore(ks, x);
+ memcpy(&x[1], &ks->x[1], n * sizeof(char));
+ free_ks(ks);
+ /* check solution found */
+ s1 = s2 = 0;
+ for (j = 1; j <= n; j++)
+ { xassert(x[j] == 0 || x[j] == 1);
+ if (x[j])
+ s1 += a[j], s2 += c[j];
+ }
+ xassert(s1 <= b);
+ xassert(s2 == z);
+ return z;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/ks.h b/test/monniaux/glpk-4.65/src/misc/ks.h
new file mode 100644
index 00000000..d607dc44
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/ks.h
@@ -0,0 +1,44 @@
+/* ks.h (0-1 knapsack problem) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef KS_H
+#define KS_H
+
+#define ks_enum _glp_ks_enum
+int ks_enum(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/]);
+/* solve 0-1 knapsack problem by complete enumeration */
+
+#define ks_mt1 _glp_ks_mt1
+int ks_mt1(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/]);
+/* solve 0-1 knapsack problem with Martello & Toth algorithm */
+
+#define ks_greedy _glp_ks_greedy
+int ks_greedy(int n, const int a[/*1+n*/], int b, const int c[/*1+n*/],
+ char x[/*1+n*/]);
+/* solve 0-1 knapsack problem with greedy heuristic */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mc13d.c b/test/monniaux/glpk-4.65/src/misc/mc13d.c
new file mode 100644
index 00000000..d8bab398
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mc13d.c
@@ -0,0 +1,314 @@
+/* mc13d.c (permutations to block triangular form) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is the result of translation of the Fortran subroutines
+* MC13D and MC13E associated with the following paper:
+*
+* I.S.Duff, J.K.Reid, Algorithm 529: Permutations to block triangular
+* form, ACM Trans. on Math. Softw. 4 (1978), 189-192.
+*
+* Use of ACM Algorithms is subject to the ACM Software Copyright and
+* License Agreement. See <http://www.acm.org/publications/policies>.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mc13d.h"
+
+/***********************************************************************
+* NAME
+*
+* mc13d - permutations to block triangular form
+*
+* SYNOPSIS
+*
+* #include "mc13d.h"
+* int mc13d(int n, const int icn[], const int ip[], const int lenr[],
+* int ior[], int ib[], int lowl[], int numb[], int prev[]);
+*
+* DESCRIPTION
+*
+* Given the column numbers of the nonzeros in each row of the sparse
+* matrix, the routine mc13d finds a symmetric permutation that makes
+* the matrix block lower triangular.
+*
+* INPUT PARAMETERS
+*
+* n order of the matrix.
+*
+* icn array containing the column indices of the non-zeros. Those
+* belonging to a single row must be contiguous but the ordering
+* of column indices within each row is unimportant and wasted
+* space between rows is permitted.
+*
+* ip ip[i], i = 1,2,...,n, is the position in array icn of the
+* first column index of a non-zero in row i.
+*
+* lenr lenr[i], i = 1,2,...,n, is the number of non-zeros in row i.
+*
+* OUTPUT PARAMETERS
+*
+* ior ior[i], i = 1,2,...,n, gives the position on the original
+* ordering of the row or column which is in position i in the
+* permuted form.
+*
+* ib ib[i], i = 1,2,...,num, is the row number in the permuted
+* matrix of the beginning of block i, 1 <= num <= n.
+*
+* WORKING ARRAYS
+*
+* arp working array of length [1+n], where arp[0] is not used.
+* arp[i] is one less than the number of unsearched edges leaving
+* node i. At the end of the algorithm it is set to a permutation
+* which puts the matrix in block lower triangular form.
+*
+* ib working array of length [1+n], where ib[0] is not used.
+* ib[i] is the position in the ordering of the start of the ith
+* block. ib[n+1-i] holds the node number of the ith node on the
+* stack.
+*
+* lowl working array of length [1+n], where lowl[0] is not used.
+* lowl[i] is the smallest stack position of any node to which a
+* path from node i has been found. It is set to n+1 when node i
+* is removed from the stack.
+*
+* numb working array of length [1+n], where numb[0] is not used.
+* numb[i] is the position of node i in the stack if it is on it,
+* is the permuted order of node i for those nodes whose final
+* position has been found and is otherwise zero.
+*
+* prev working array of length [1+n], where prev[0] is not used.
+* prev[i] is the node at the end of the path when node i was
+* placed on the stack.
+*
+* RETURNS
+*
+* The routine mc13d returns num, the number of blocks found. */
+
+int mc13d(int n, const int icn[], const int ip[], const int lenr[],
+ int ior[], int ib[], int lowl[], int numb[], int prev[])
+{ int *arp = ior;
+ int dummy, i, i1, i2, icnt, ii, isn, ist, ist1, iv, iw, j, lcnt,
+ nnm1, num, stp;
+ /* icnt is the number of nodes whose positions in final ordering
+ * have been found. */
+ icnt = 0;
+ /* num is the number of blocks that have been found. */
+ num = 0;
+ nnm1 = n + n - 1;
+ /* Initialization of arrays. */
+ for (j = 1; j <= n; j++)
+ { numb[j] = 0;
+ arp[j] = lenr[j] - 1;
+ }
+ for (isn = 1; isn <= n; isn++)
+ { /* Look for a starting node. */
+ if (numb[isn] != 0) continue;
+ iv = isn;
+ /* ist is the number of nodes on the stack ... it is the stack
+ * pointer. */
+ ist = 1;
+ /* Put node iv at beginning of stack. */
+ lowl[iv] = numb[iv] = 1;
+ ib[n] = iv;
+ /* The body of this loop puts a new node on the stack or
+ * backtracks. */
+ for (dummy = 1; dummy <= nnm1; dummy++)
+ { i1 = arp[iv];
+ /* Have all edges leaving node iv been searched? */
+ if (i1 >= 0)
+ { i2 = ip[iv] + lenr[iv] - 1;
+ i1 = i2 - i1;
+ /* Look at edges leaving node iv until one enters a new
+ * node or all edges are exhausted. */
+ for (ii = i1; ii <= i2; ii++)
+ { iw = icn[ii];
+ /* Has node iw been on stack already? */
+ if (numb[iw] == 0) goto L70;
+ /* Update value of lowl[iv] if necessary. */
+ if (lowl[iw] < lowl[iv]) lowl[iv] = lowl[iw];
+ }
+ /* There are no more edges leaving node iv. */
+ arp[iv] = -1;
+ }
+ /* Is node iv the root of a block? */
+ if (lowl[iv] < numb[iv]) goto L60;
+ /* Order nodes in a block. */
+ num++;
+ ist1 = n + 1 - ist;
+ lcnt = icnt + 1;
+ /* Peel block off the top of the stack starting at the top
+ * and working down to the root of the block. */
+ for (stp = ist1; stp <= n; stp++)
+ { iw = ib[stp];
+ lowl[iw] = n + 1;
+ numb[iw] = ++icnt;
+ if (iw == iv) break;
+ }
+ ist = n - stp;
+ ib[num] = lcnt;
+ /* Are there any nodes left on the stack? */
+ if (ist != 0) goto L60;
+ /* Have all the nodes been ordered? */
+ if (icnt < n) break;
+ goto L100;
+L60: /* Backtrack to previous node on path. */
+ iw = iv;
+ iv = prev[iv];
+ /* Update value of lowl[iv] if necessary. */
+ if (lowl[iw] < lowl[iv]) lowl[iv] = lowl[iw];
+ continue;
+L70: /* Put new node on the stack. */
+ arp[iv] = i2 - ii - 1;
+ prev[iw] = iv;
+ iv = iw;
+ lowl[iv] = numb[iv] = ++ist;
+ ib[n+1-ist] = iv;
+ }
+ }
+L100: /* Put permutation in the required form. */
+ for (i = 1; i <= n; i++)
+ arp[numb[i]] = i;
+ return num;
+}
+
+/**********************************************************************/
+
+#ifdef GLP_TEST
+#include "env.h"
+
+void test(int n, int ipp);
+
+int main(void)
+{ /* test program for routine mc13d */
+ test( 1, 0);
+ test( 2, 1);
+ test( 2, 2);
+ test( 3, 3);
+ test( 4, 4);
+ test( 5, 10);
+ test(10, 10);
+ test(10, 20);
+ test(20, 20);
+ test(20, 50);
+ test(50, 50);
+ test(50, 200);
+ return 0;
+}
+
+void fa01bs(int max, int *nrand);
+
+void setup(int n, char a[1+50][1+50], int ip[], int icn[], int lenr[]);
+
+void test(int n, int ipp)
+{ int ip[1+50], icn[1+1000], ior[1+50], ib[1+51], iw[1+150],
+ lenr[1+50];
+ char a[1+50][1+50], hold[1+100];
+ int i, ii, iblock, ij, index, j, jblock, jj, k9, num;
+ xprintf("\n\n\nMatrix is of order %d and has %d off-diagonal non-"
+ "zeros\n", n, ipp);
+ for (j = 1; j <= n; j++)
+ { for (i = 1; i <= n; i++)
+ a[i][j] = 0;
+ a[j][j] = 1;
+ }
+ for (k9 = 1; k9 <= ipp; k9++)
+ { /* these statements should be replaced by calls to your
+ * favorite random number generator to place two pseudo-random
+ * numbers between 1 and n in the variables i and j */
+ for (;;)
+ { fa01bs(n, &i);
+ fa01bs(n, &j);
+ if (!a[i][j]) break;
+ }
+ a[i][j] = 1;
+ }
+ /* setup converts matrix a[i,j] to required sparsity-oriented
+ * storage format */
+ setup(n, a, ip, icn, lenr);
+ num = mc13d(n, icn, ip, lenr, ior, ib, &iw[0], &iw[n], &iw[n+n]);
+ /* output reordered matrix with blocking to improve clarity */
+ xprintf("\nThe reordered matrix which has %d block%s is of the fo"
+ "rm\n", num, num == 1 ? "" : "s");
+ ib[num+1] = n + 1;
+ index = 100;
+ iblock = 1;
+ for (i = 1; i <= n; i++)
+ { for (ij = 1; ij <= index; ij++)
+ hold[ij] = ' ';
+ if (i == ib[iblock])
+ { xprintf("\n");
+ iblock++;
+ }
+ jblock = 1;
+ index = 0;
+ for (j = 1; j <= n; j++)
+ { if (j == ib[jblock])
+ { hold[++index] = ' ';
+ jblock++;
+ }
+ ii = ior[i];
+ jj = ior[j];
+ hold[++index] = (char)(a[ii][jj] ? 'X' : '0');
+ }
+ xprintf("%.*s\n", index, &hold[1]);
+ }
+ xprintf("\nThe starting point for each block is given by\n");
+ for (i = 1; i <= num; i++)
+ { if ((i - 1) % 12 == 0) xprintf("\n");
+ xprintf(" %4d", ib[i]);
+ }
+ xprintf("\n");
+ return;
+}
+
+void setup(int n, char a[1+50][1+50], int ip[], int icn[], int lenr[])
+{ int i, j, ind;
+ for (i = 1; i <= n; i++)
+ lenr[i] = 0;
+ ind = 1;
+ for (i = 1; i <= n; i++)
+ { ip[i] = ind;
+ for (j = 1; j <= n; j++)
+ { if (a[i][j])
+ { lenr[i]++;
+ icn[ind++] = j;
+ }
+ }
+ }
+ return;
+}
+
+double g = 1431655765.0;
+
+double fa01as(int i)
+{ /* random number generator */
+ g = fmod(g * 9228907.0, 4294967296.0);
+ if (i >= 0)
+ return g / 4294967296.0;
+ else
+ return 2.0 * g / 4294967296.0 - 1.0;
+}
+
+void fa01bs(int max, int *nrand)
+{ *nrand = (int)(fa01as(1) * (double)max) + 1;
+ return;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mc13d.h b/test/monniaux/glpk-4.65/src/misc/mc13d.h
new file mode 100644
index 00000000..bdd57a19
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mc13d.h
@@ -0,0 +1,34 @@
+/* mc13d.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MC13D_H
+#define MC13D_H
+
+#define mc13d _glp_mc13d
+int mc13d(int n, const int icn[], const int ip[], const int lenr[],
+ int ior[], int ib[], int lowl[], int numb[], int prev[]);
+/* permutations to block triangular form */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mc21a.c b/test/monniaux/glpk-4.65/src/misc/mc21a.c
new file mode 100644
index 00000000..700d0f4e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mc21a.c
@@ -0,0 +1,301 @@
+/* mc21a.c (permutations for zero-free diagonal) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is the result of translation of the Fortran subroutines
+* MC21A and MC21B associated with the following paper:
+*
+* I.S.Duff, Algorithm 575: Permutations for zero-free diagonal, ACM
+* Trans. on Math. Softw. 7 (1981), 387-390.
+*
+* Use of ACM Algorithms is subject to the ACM Software Copyright and
+* License Agreement. See <http://www.acm.org/publications/policies>.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mc21a.h"
+
+/***********************************************************************
+* NAME
+*
+* mc21a - permutations for zero-free diagonal
+*
+* SYNOPSIS
+*
+* #include "mc21a.h"
+* int mc21a(int n, const int icn[], const int ip[], const int lenr[],
+* int iperm[], int pr[], int arp[], int cv[], int out[]);
+*
+* DESCRIPTION
+*
+* Given the pattern of nonzeros of a sparse matrix, the routine mc21a
+* attempts to find a permutation of its rows that makes the matrix have
+* no zeros on its diagonal.
+*
+* INPUT PARAMETERS
+*
+* n order of matrix.
+*
+* icn array containing the column indices of the non-zeros. Those
+* belonging to a single row must be contiguous but the ordering
+* of column indices within each row is unimportant and wasted
+* space between rows is permitted.
+*
+* ip ip[i], i = 1,2,...,n, is the position in array icn of the
+* first column index of a non-zero in row i.
+*
+* lenr lenr[i], i = 1,2,...,n, is the number of non-zeros in row i.
+*
+* OUTPUT PARAMETER
+*
+* iperm contains permutation to make diagonal have the smallest
+* number of zeros on it. Elements (iperm[i], i), i = 1,2,...,n,
+* are non-zero at the end of the algorithm unless the matrix is
+* structurally singular. In this case, (iperm[i], i) will be
+* zero for n - numnz entries.
+*
+* WORKING ARRAYS
+*
+* pr working array of length [1+n], where pr[0] is not used.
+* pr[i] is the previous row to i in the depth first search.
+*
+* arp working array of length [1+n], where arp[0] is not used.
+* arp[i] is one less than the number of non-zeros in row i which
+* have not been scanned when looking for a cheap assignment.
+*
+* cv working array of length [1+n], where cv[0] is not used.
+* cv[i] is the most recent row extension at which column i was
+* visited.
+*
+* out working array of length [1+n], where out[0] is not used.
+* out[i] is one less than the number of non-zeros in row i
+* which have not been scanned during one pass through the main
+* loop.
+*
+* RETURNS
+*
+* The routine mc21a returns numnz, the number of non-zeros on diagonal
+* of permuted matrix. */
+
+int mc21a(int n, const int icn[], const int ip[], const int lenr[],
+ int iperm[], int pr[], int arp[], int cv[], int out[])
+{ int i, ii, in1, in2, j, j1, jord, k, kk, numnz;
+ /* Initialization of arrays. */
+ for (i = 1; i <= n; i++)
+ { arp[i] = lenr[i] - 1;
+ cv[i] = iperm[i] = 0;
+ }
+ numnz = 0;
+ /* Main loop. */
+ /* Each pass round this loop either results in a new assignment
+ * or gives a row with no assignment. */
+ for (jord = 1; jord <= n; jord++)
+ { j = jord;
+ pr[j] = -1;
+ for (k = 1; k <= jord; k++)
+ { /* Look for a cheap assignment. */
+ in1 = arp[j];
+ if (in1 >= 0)
+ { in2 = ip[j] + lenr[j] - 1;
+ in1 = in2 - in1;
+ for (ii = in1; ii <= in2; ii++)
+ { i = icn[ii];
+ if (iperm[i] == 0) goto L110;
+ }
+ /* No cheap assignment in row. */
+ arp[j] = -1;
+ }
+ /* Begin looking for assignment chain starting with row j.*/
+ out[j] = lenr[j] - 1;
+ /* Inner loop. Extends chain by one or backtracks. */
+ for (kk = 1; kk <= jord; kk++)
+ { in1 = out[j];
+ if (in1 >= 0)
+ { in2 = ip[j] + lenr[j] - 1;
+ in1 = in2 - in1;
+ /* Forward scan. */
+ for (ii = in1; ii <= in2; ii++)
+ { i = icn[ii];
+ if (cv[i] != jord)
+ { /* Column i has not yet been accessed during
+ * this pass. */
+ j1 = j;
+ j = iperm[i];
+ cv[i] = jord;
+ pr[j] = j1;
+ out[j1] = in2 - ii - 1;
+ goto L100;
+ }
+ }
+ }
+ /* Backtracking step. */
+ j = pr[j];
+ if (j == -1) goto L130;
+ }
+L100: ;
+ }
+L110: /* New assignment is made. */
+ iperm[i] = j;
+ arp[j] = in2 - ii - 1;
+ numnz++;
+ for (k = 1; k <= jord; k++)
+ { j = pr[j];
+ if (j == -1) break;
+ ii = ip[j] + lenr[j] - out[j] - 2;
+ i = icn[ii];
+ iperm[i] = j;
+ }
+L130: ;
+ }
+ /* If matrix is structurally singular, we now complete the
+ * permutation iperm. */
+ if (numnz < n)
+ { for (i = 1; i <= n; i++)
+ arp[i] = 0;
+ k = 0;
+ for (i = 1; i <= n; i++)
+ { if (iperm[i] == 0)
+ out[++k] = i;
+ else
+ arp[iperm[i]] = i;
+ }
+ k = 0;
+ for (i = 1; i <= n; i++)
+ { if (arp[i] == 0)
+ iperm[out[++k]] = i;
+ }
+ }
+ return numnz;
+}
+
+/**********************************************************************/
+
+#ifdef GLP_TEST
+#include "env.h"
+
+int sing;
+
+void ranmat(int m, int n, int icn[], int iptr[], int nnnp1, int *knum,
+ int iw[]);
+
+void fa01bs(int max, int *nrand);
+
+int main(void)
+{ /* test program for the routine mc21a */
+ /* these runs on random matrices cause all possible statements in
+ * mc21a to be executed */
+ int i, iold, j, j1, j2, jj, knum, l, licn, n, nov4, num, numnz;
+ int ip[1+21], icn[1+1000], iperm[1+20], lenr[1+20], iw1[1+80];
+ licn = 1000;
+ /* run on random matrices of orders 1 through 20 */
+ for (n = 1; n <= 20; n++)
+ { nov4 = n / 4;
+ if (nov4 < 1) nov4 = 1;
+L10: fa01bs(nov4, &l);
+ knum = l * n;
+ /* knum is requested number of non-zeros in random matrix */
+ if (knum > licn) goto L10;
+ /* if sing is false, matrix is guaranteed structurally
+ * non-singular */
+ sing = ((n / 2) * 2 == n);
+ /* call to subroutine to generate random matrix */
+ ranmat(n, n, icn, ip, n+1, &knum, iw1);
+ /* knum is now actual number of non-zeros in random matrix */
+ if (knum > licn) goto L10;
+ xprintf("n = %2d; nz = %4d; sing = %d\n", n, knum, sing);
+ /* set up array of row lengths */
+ for (i = 1; i <= n; i++)
+ lenr[i] = ip[i+1] - ip[i];
+ /* call to mc21a */
+ numnz = mc21a(n, icn, ip, lenr, iperm, &iw1[0], &iw1[n],
+ &iw1[n+n], &iw1[n+n+n]);
+ /* testing to see if there are numnz non-zeros on the diagonal
+ * of the permuted matrix. */
+ num = 0;
+ for (i = 1; i <= n; i++)
+ { iold = iperm[i];
+ j1 = ip[iold];
+ j2 = j1 + lenr[iold] - 1;
+ if (j2 < j1) continue;
+ for (jj = j1; jj <= j2; jj++)
+ { j = icn[jj];
+ if (j == i)
+ { num++;
+ break;
+ }
+ }
+ }
+ if (num != numnz)
+ xprintf("Failure in mc21a, numnz = %d instead of %d\n",
+ numnz, num);
+ }
+ return 0;
+}
+
+void ranmat(int m, int n, int icn[], int iptr[], int nnnp1, int *knum,
+ int iw[])
+{ /* subroutine to generate random matrix */
+ int i, ii, inum, j, lrow, matnum;
+ inum = (*knum / n) * 2;
+ if (inum > n-1) inum = n-1;
+ matnum = 1;
+ /* each pass through this loop generates a row of the matrix */
+ for (j = 1; j <= m; j++)
+ { iptr[j] = matnum;
+ if (!(sing || j > n))
+ icn[matnum++] = j;
+ if (n == 1) continue;
+ for (i = 1; i <= n; i++) iw[i] = 0;
+ if (!sing) iw[j] = 1;
+ fa01bs(inum, &lrow);
+ lrow--;
+ if (lrow == 0) continue;
+ /* lrow off-diagonal non-zeros in row j of the matrix */
+ for (ii = 1; ii <= lrow; ii++)
+ { for (;;)
+ { fa01bs(n, &i);
+ if (iw[i] != 1) break;
+ }
+ iw[i] = 1;
+ icn[matnum++] = i;
+ }
+ }
+ for (i = m+1; i <= nnnp1; i++)
+ iptr[i] = matnum;
+ *knum = matnum - 1;
+ return;
+}
+
+double g = 1431655765.0;
+
+double fa01as(int i)
+{ /* random number generator */
+ g = fmod(g * 9228907.0, 4294967296.0);
+ if (i >= 0)
+ return g / 4294967296.0;
+ else
+ return 2.0 * g / 4294967296.0 - 1.0;
+}
+
+void fa01bs(int max, int *nrand)
+{ *nrand = (int)(fa01as(1) * (double)max) + 1;
+ return;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mc21a.h b/test/monniaux/glpk-4.65/src/misc/mc21a.h
new file mode 100644
index 00000000..755f28b2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mc21a.h
@@ -0,0 +1,34 @@
+/* mc21a.h (permutations for zero-free diagonal) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MC21A_H
+#define MC21A_H
+
+#define mc21a _glp_mc21a
+int mc21a(int n, const int icn[], const int ip[], const int lenr[],
+ int iperm[], int pr[], int arp[], int cv[], int out[]);
+/* permutations for zero-free diagonal */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/misc.h b/test/monniaux/glpk-4.65/src/misc/misc.h
new file mode 100644
index 00000000..1ba1dc50
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/misc.h
@@ -0,0 +1,61 @@
+/* misc.h (miscellaneous routines) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MISC_H
+#define MISC_H
+
+#define str2int _glp_str2int
+int str2int(const char *str, int *val);
+/* convert character string to value of int type */
+
+#define str2num _glp_str2num
+int str2num(const char *str, double *val);
+/* convert character string to value of double type */
+
+#define strspx _glp_strspx
+char *strspx(char *str);
+/* remove all spaces from character string */
+
+#define strtrim _glp_strtrim
+char *strtrim(char *str);
+/* remove trailing spaces from character string */
+
+#define gcd _glp_gcd
+int gcd(int x, int y);
+/* find greatest common divisor of two integers */
+
+#define gcdn _glp_gcdn
+int gcdn(int n, int x[]);
+/* find greatest common divisor of n integers */
+
+#define round2n _glp_round2n
+double round2n(double x);
+/* round floating-point number to nearest power of two */
+
+#define fp2rat _glp_fp2rat
+int fp2rat(double x, double eps, double *p, double *q);
+/* convert floating-point number to rational number */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mt1.c b/test/monniaux/glpk-4.65/src/misc/mt1.c
new file mode 100644
index 00000000..63a0f80e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mt1.c
@@ -0,0 +1,1110 @@
+/* mt1.c (0-1 knapsack problem; Martello & Toth algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* THIS CODE IS THE RESULT OF TRANSLATION OF THE FORTRAN SUBROUTINES
+* MT1 FROM THE BOOK:
+*
+* SILVANO MARTELLO, PAOLO TOTH. KNAPSACK PROBLEMS: ALGORITHMS AND
+* COMPUTER IMPLEMENTATIONS. JOHN WILEY & SONS, 1990.
+*
+* THE TRANSLATION HAS BEEN DONE WITH THE PERMISSION OF THE AUTHORS OF
+* THE ORIGINAL FORTRAN SUBROUTINES: SILVANO MARTELLO AND PAOLO TOTH.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#line 1 ""
+/* -- translated by f2c (version 20100827).
+ You must link the resulting object file with libf2c:
+ on Microsoft Windows system, link with libf2c.lib;
+ on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+ or, if you install libf2c.a in a standard place, with -lf2c -lm
+ -- in that order, at the end of the command line, as in
+ cc *.o -lf2c -lm
+ Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+ http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#if 0 /* by mao */
+#include "f2c.h"
+#else
+#include "env.h"
+#include "mt1.h"
+
+typedef int integer;
+typedef float real;
+#endif
+
+#line 1 ""
+/*< SUBROUTINE MT1(N,P,W,C,Z,X,JDIM,JCK,XX,MIN,PSIGN,WSIGN,ZSIGN) >*/
+#if 1 /* by mao */
+static int chmt1_(int *, int *, int *, int *, int *, int *);
+
+static
+#endif
+/* Subroutine */ int mt1_(integer *n, integer *p, integer *w, integer *c__,
+ integer *z__, integer *x, integer *jdim, integer *jck, integer *xx,
+ integer *min__, integer *psign, integer *wsign, integer *zsign)
+{
+ /* System generated locals */
+ integer i__1;
+
+ /* Local variables */
+ static real a, b;
+ static integer j, r__, t, j1, n1, ch, ii, jj, kk, in, ll, ip, nn, iu, ii1,
+ chs, lim, lim1, diff, lold, mink;
+ extern /* Subroutine */ int chmt1_(integer *, integer *, integer *,
+ integer *, integer *, integer *);
+ static integer profit;
+
+
+/* THIS SUBROUTINE SOLVES THE 0-1 SINGLE KNAPSACK PROBLEM */
+
+/* MAXIMIZE Z = P(1)*X(1) + ... + P(N)*X(N) */
+
+/* SUBJECT TO: W(1)*X(1) + ... + W(N)*X(N) .LE. C , */
+/* X(J) = 0 OR 1 FOR J=1,...,N. */
+
+/* THE PROGRAM IS INCLUDED IN THE VOLUME */
+/* S. MARTELLO, P. TOTH, "KNAPSACK PROBLEMS: ALGORITHMS */
+/* AND COMPUTER IMPLEMENTATIONS", JOHN WILEY, 1990 */
+/* AND IMPLEMENTS THE BRANCH-AND-BOUND ALGORITHM DESCRIBED IN */
+/* SECTION 2.5.2 . */
+/* THE PROGRAM DERIVES FROM AN EARLIER CODE PRESENTED IN */
+/* S. MARTELLO, P. TOTH, "ALGORITHM FOR THE SOLUTION OF THE 0-1 SINGLE */
+/* KNAPSACK PROBLEM", COMPUTING, 1978. */
+
+/* THE INPUT PROBLEM MUST SATISFY THE CONDITIONS */
+
+/* 1) 2 .LE. N .LE. JDIM - 1 ; */
+/* 2) P(J), W(J), C POSITIVE INTEGERS; */
+/* 3) MAX (W(J)) .LE. C ; */
+/* 4) W(1) + ... + W(N) .GT. C ; */
+/* 5) P(J)/W(J) .GE. P(J+1)/W(J+1) FOR J=1,...,N-1. */
+
+/* MT1 CALLS 1 PROCEDURE: CHMT1. */
+
+/* THE PROGRAM IS COMPLETELY SELF-CONTAINED AND COMMUNICATION TO IT IS */
+/* ACHIEVED SOLELY THROUGH THE PARAMETER LIST OF MT1. */
+/* NO MACHINE-DEPENDENT CONSTANT IS USED. */
+/* THE PROGRAM IS WRITTEN IN 1967 AMERICAN NATIONAL STANDARD FORTRAN */
+/* AND IS ACCEPTED BY THE PFORT VERIFIER (PFORT IS THE PORTABLE */
+/* SUBSET OF ANSI DEFINED BY THE ASSOCIATION FOR COMPUTING MACHINERY). */
+/* THE PROGRAM HAS BEEN TESTED ON A DIGITAL VAX 11/780 AND AN H.P. */
+/* 9000/840. */
+
+/* MT1 NEEDS 8 ARRAYS ( P , W , X , XX , MIN , PSIGN , WSIGN */
+/* AND ZSIGN ) OF LENGTH AT LEAST N + 1 . */
+
+/* MEANING OF THE INPUT PARAMETERS: */
+/* N = NUMBER OF ITEMS; */
+/* P(J) = PROFIT OF ITEM J (J=1,...,N); */
+/* W(J) = WEIGHT OF ITEM J (J=1,...,N); */
+/* C = CAPACITY OF THE KNAPSACK; */
+/* JDIM = DIMENSION OF THE 8 ARRAYS; */
+/* JCK = 1 IF CHECK ON THE INPUT DATA IS DESIRED, */
+/* = 0 OTHERWISE. */
+
+/* MEANING OF THE OUTPUT PARAMETERS: */
+/* Z = VALUE OF THE OPTIMAL SOLUTION IF Z .GT. 0 , */
+/* = ERROR IN THE INPUT DATA (WHEN JCK=1) IF Z .LT. 0 : CONDI- */
+/* TION - Z IS VIOLATED; */
+/* X(J) = 1 IF ITEM J IS IN THE OPTIMAL SOLUTION, */
+/* = 0 OTHERWISE. */
+
+/* ARRAYS XX, MIN, PSIGN, WSIGN AND ZSIGN ARE DUMMY. */
+
+/* ALL THE PARAMETERS ARE INTEGER. ON RETURN OF MT1 ALL THE INPUT */
+/* PARAMETERS ARE UNCHANGED. */
+
+/*< INTEGER P(JDIM),W(JDIM),X(JDIM),C,Z >*/
+/*< INTEGER XX(JDIM),MIN(JDIM),PSIGN(JDIM),WSIGN(JDIM),ZSIGN(JDIM) >*/
+/*< INTEGER CH,CHS,DIFF,PROFIT,R,T >*/
+/*< Z = 0 >*/
+#line 65 ""
+ /* Parameter adjustments */
+#line 65 ""
+ --zsign;
+#line 65 ""
+ --wsign;
+#line 65 ""
+ --psign;
+#line 65 ""
+ --min__;
+#line 65 ""
+ --xx;
+#line 65 ""
+ --x;
+#line 65 ""
+ --w;
+#line 65 ""
+ --p;
+#line 65 ""
+
+#line 65 ""
+ /* Function Body */
+#line 65 ""
+ *z__ = 0;
+/*< IF ( JCK .EQ. 1 ) CALL CHMT1(N,P,W,C,Z,JDIM) >*/
+#line 66 ""
+ if (*jck == 1) {
+#line 66 ""
+ chmt1_(n, &p[1], &w[1], c__, z__, jdim);
+#line 66 ""
+ }
+/*< IF ( Z .LT. 0 ) RETURN >*/
+#line 67 ""
+ if (*z__ < 0) {
+#line 67 ""
+ return 0;
+#line 67 ""
+ }
+/* INITIALIZE. */
+/*< CH = C >*/
+#line 69 ""
+ ch = *c__;
+/*< IP = 0 >*/
+#line 70 ""
+ ip = 0;
+/*< CHS = CH >*/
+#line 71 ""
+ chs = ch;
+/*< DO 10 LL=1,N >*/
+#line 72 ""
+ i__1 = *n;
+#line 72 ""
+ for (ll = 1; ll <= i__1; ++ll) {
+/*< IF ( W(LL) .GT. CHS ) GO TO 20 >*/
+#line 73 ""
+ if (w[ll] > chs) {
+#line 73 ""
+ goto L20;
+#line 73 ""
+ }
+/*< IP = IP + P(LL) >*/
+#line 74 ""
+ ip += p[ll];
+/*< CHS = CHS - W(LL) >*/
+#line 75 ""
+ chs -= w[ll];
+/*< 10 CONTINUE >*/
+#line 76 ""
+/* L10: */
+#line 76 ""
+ }
+/*< 20 LL = LL - 1 >*/
+#line 77 ""
+L20:
+#line 77 ""
+ --ll;
+/*< IF ( CHS .EQ. 0 ) GO TO 50 >*/
+#line 78 ""
+ if (chs == 0) {
+#line 78 ""
+ goto L50;
+#line 78 ""
+ }
+/*< P(N+1) = 0 >*/
+#line 79 ""
+ p[*n + 1] = 0;
+/*< W(N+1) = CH + 1 >*/
+#line 80 ""
+ w[*n + 1] = ch + 1;
+/*< LIM = IP + CHS*P(LL+2)/W(LL+2) >*/
+#line 81 ""
+ lim = ip + chs * p[ll + 2] / w[ll + 2];
+/*< A = W(LL+1) - CHS >*/
+#line 82 ""
+ a = (real) (w[ll + 1] - chs);
+/*< B = IP + P(LL+1) >*/
+#line 83 ""
+ b = (real) (ip + p[ll + 1]);
+/*< LIM1 = B - A*FLOAT(P(LL))/FLOAT(W(LL)) >*/
+#line 84 ""
+ lim1 = b - a * (real) p[ll] / (real) w[ll];
+/*< IF ( LIM1 .GT. LIM ) LIM = LIM1 >*/
+#line 85 ""
+ if (lim1 > lim) {
+#line 85 ""
+ lim = lim1;
+#line 85 ""
+ }
+/*< MINK = CH + 1 >*/
+#line 86 ""
+ mink = ch + 1;
+/*< MIN(N) = MINK >*/
+#line 87 ""
+ min__[*n] = mink;
+/*< DO 30 J=2,N >*/
+#line 88 ""
+ i__1 = *n;
+#line 88 ""
+ for (j = 2; j <= i__1; ++j) {
+/*< KK = N + 2 - J >*/
+#line 89 ""
+ kk = *n + 2 - j;
+/*< IF ( W(KK) .LT. MINK ) MINK = W(KK) >*/
+#line 90 ""
+ if (w[kk] < mink) {
+#line 90 ""
+ mink = w[kk];
+#line 90 ""
+ }
+/*< MIN(KK-1) = MINK >*/
+#line 91 ""
+ min__[kk - 1] = mink;
+/*< 30 CONTINUE >*/
+#line 92 ""
+/* L30: */
+#line 92 ""
+ }
+/*< DO 40 J=1,N >*/
+#line 93 ""
+ i__1 = *n;
+#line 93 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< XX(J) = 0 >*/
+#line 94 ""
+ xx[j] = 0;
+/*< 40 CONTINUE >*/
+#line 95 ""
+/* L40: */
+#line 95 ""
+ }
+/*< Z = 0 >*/
+#line 96 ""
+ *z__ = 0;
+/*< PROFIT = 0 >*/
+#line 97 ""
+ profit = 0;
+/*< LOLD = N >*/
+#line 98 ""
+ lold = *n;
+/*< II = 1 >*/
+#line 99 ""
+ ii = 1;
+/*< GO TO 170 >*/
+#line 100 ""
+ goto L170;
+/*< 50 Z = IP >*/
+#line 101 ""
+L50:
+#line 101 ""
+ *z__ = ip;
+/*< DO 60 J=1,LL >*/
+#line 102 ""
+ i__1 = ll;
+#line 102 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< X(J) = 1 >*/
+#line 103 ""
+ x[j] = 1;
+/*< 60 CONTINUE >*/
+#line 104 ""
+/* L60: */
+#line 104 ""
+ }
+/*< NN = LL + 1 >*/
+#line 105 ""
+ nn = ll + 1;
+/*< DO 70 J=NN,N >*/
+#line 106 ""
+ i__1 = *n;
+#line 106 ""
+ for (j = nn; j <= i__1; ++j) {
+/*< X(J) = 0 >*/
+#line 107 ""
+ x[j] = 0;
+/*< 70 CONTINUE >*/
+#line 108 ""
+/* L70: */
+#line 108 ""
+ }
+/*< RETURN >*/
+#line 109 ""
+ return 0;
+/* TRY TO INSERT THE II-TH ITEM INTO THE CURRENT SOLUTION. */
+/*< 80 IF ( W(II) .LE. CH ) GO TO 90 >*/
+#line 111 ""
+L80:
+#line 111 ""
+ if (w[ii] <= ch) {
+#line 111 ""
+ goto L90;
+#line 111 ""
+ }
+/*< II1 = II + 1 >*/
+#line 112 ""
+ ii1 = ii + 1;
+/*< IF ( Z .GE. CH*P(II1)/W(II1) + PROFIT ) GO TO 280 >*/
+#line 113 ""
+ if (*z__ >= ch * p[ii1] / w[ii1] + profit) {
+#line 113 ""
+ goto L280;
+#line 113 ""
+ }
+/*< II = II1 >*/
+#line 114 ""
+ ii = ii1;
+/*< GO TO 80 >*/
+#line 115 ""
+ goto L80;
+/* BUILD A NEW CURRENT SOLUTION. */
+/*< 90 IP = PSIGN(II) >*/
+#line 117 ""
+L90:
+#line 117 ""
+ ip = psign[ii];
+/*< CHS = CH - WSIGN(II) >*/
+#line 118 ""
+ chs = ch - wsign[ii];
+/*< IN = ZSIGN(II) >*/
+#line 119 ""
+ in = zsign[ii];
+/*< DO 100 LL=IN,N >*/
+#line 120 ""
+ i__1 = *n;
+#line 120 ""
+ for (ll = in; ll <= i__1; ++ll) {
+/*< IF ( W(LL) .GT. CHS ) GO TO 160 >*/
+#line 121 ""
+ if (w[ll] > chs) {
+#line 121 ""
+ goto L160;
+#line 121 ""
+ }
+/*< IP = IP + P(LL) >*/
+#line 122 ""
+ ip += p[ll];
+/*< CHS = CHS - W(LL) >*/
+#line 123 ""
+ chs -= w[ll];
+/*< 100 CONTINUE >*/
+#line 124 ""
+/* L100: */
+#line 124 ""
+ }
+/*< LL = N >*/
+#line 125 ""
+ ll = *n;
+/*< 110 IF ( Z .GE. IP + PROFIT ) GO TO 280 >*/
+#line 126 ""
+L110:
+#line 126 ""
+ if (*z__ >= ip + profit) {
+#line 126 ""
+ goto L280;
+#line 126 ""
+ }
+/*< Z = IP + PROFIT >*/
+#line 127 ""
+ *z__ = ip + profit;
+/*< NN = II - 1 >*/
+#line 128 ""
+ nn = ii - 1;
+/*< DO 120 J=1,NN >*/
+#line 129 ""
+ i__1 = nn;
+#line 129 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< X(J) = XX(J) >*/
+#line 130 ""
+ x[j] = xx[j];
+/*< 120 CONTINUE >*/
+#line 131 ""
+/* L120: */
+#line 131 ""
+ }
+/*< DO 130 J=II,LL >*/
+#line 132 ""
+ i__1 = ll;
+#line 132 ""
+ for (j = ii; j <= i__1; ++j) {
+/*< X(J) = 1 >*/
+#line 133 ""
+ x[j] = 1;
+/*< 130 CONTINUE >*/
+#line 134 ""
+/* L130: */
+#line 134 ""
+ }
+/*< IF ( LL .EQ. N ) GO TO 150 >*/
+#line 135 ""
+ if (ll == *n) {
+#line 135 ""
+ goto L150;
+#line 135 ""
+ }
+/*< NN = LL + 1 >*/
+#line 136 ""
+ nn = ll + 1;
+/*< DO 140 J=NN,N >*/
+#line 137 ""
+ i__1 = *n;
+#line 137 ""
+ for (j = nn; j <= i__1; ++j) {
+/*< X(J) = 0 >*/
+#line 138 ""
+ x[j] = 0;
+/*< 140 CONTINUE >*/
+#line 139 ""
+/* L140: */
+#line 139 ""
+ }
+/*< 150 IF ( Z .NE. LIM ) GO TO 280 >*/
+#line 140 ""
+L150:
+#line 140 ""
+ if (*z__ != lim) {
+#line 140 ""
+ goto L280;
+#line 140 ""
+ }
+/*< RETURN >*/
+#line 141 ""
+ return 0;
+/*< 160 IU = CHS*P(LL)/W(LL) >*/
+#line 142 ""
+L160:
+#line 142 ""
+ iu = chs * p[ll] / w[ll];
+/*< LL = LL - 1 >*/
+#line 143 ""
+ --ll;
+/*< IF ( IU .EQ. 0 ) GO TO 110 >*/
+#line 144 ""
+ if (iu == 0) {
+#line 144 ""
+ goto L110;
+#line 144 ""
+ }
+/*< IF ( Z .GE. PROFIT + IP + IU ) GO TO 280 >*/
+#line 145 ""
+ if (*z__ >= profit + ip + iu) {
+#line 145 ""
+ goto L280;
+#line 145 ""
+ }
+/* SAVE THE CURRENT SOLUTION. */
+/*< 170 WSIGN(II) = CH - CHS >*/
+#line 147 ""
+L170:
+#line 147 ""
+ wsign[ii] = ch - chs;
+/*< PSIGN(II) = IP >*/
+#line 148 ""
+ psign[ii] = ip;
+/*< ZSIGN(II) = LL + 1 >*/
+#line 149 ""
+ zsign[ii] = ll + 1;
+/*< XX(II) = 1 >*/
+#line 150 ""
+ xx[ii] = 1;
+/*< NN = LL - 1 >*/
+#line 151 ""
+ nn = ll - 1;
+/*< IF ( NN .LT. II) GO TO 190 >*/
+#line 152 ""
+ if (nn < ii) {
+#line 152 ""
+ goto L190;
+#line 152 ""
+ }
+/*< DO 180 J=II,NN >*/
+#line 153 ""
+ i__1 = nn;
+#line 153 ""
+ for (j = ii; j <= i__1; ++j) {
+/*< WSIGN(J+1) = WSIGN(J) - W(J) >*/
+#line 154 ""
+ wsign[j + 1] = wsign[j] - w[j];
+/*< PSIGN(J+1) = PSIGN(J) - P(J) >*/
+#line 155 ""
+ psign[j + 1] = psign[j] - p[j];
+/*< ZSIGN(J+1) = LL + 1 >*/
+#line 156 ""
+ zsign[j + 1] = ll + 1;
+/*< XX(J+1) = 1 >*/
+#line 157 ""
+ xx[j + 1] = 1;
+/*< 180 CONTINUE >*/
+#line 158 ""
+/* L180: */
+#line 158 ""
+ }
+/*< 190 J1 = LL + 1 >*/
+#line 159 ""
+L190:
+#line 159 ""
+ j1 = ll + 1;
+/*< DO 200 J=J1,LOLD >*/
+#line 160 ""
+ i__1 = lold;
+#line 160 ""
+ for (j = j1; j <= i__1; ++j) {
+/*< WSIGN(J) = 0 >*/
+#line 161 ""
+ wsign[j] = 0;
+/*< PSIGN(J) = 0 >*/
+#line 162 ""
+ psign[j] = 0;
+/*< ZSIGN(J) = J >*/
+#line 163 ""
+ zsign[j] = j;
+/*< 200 CONTINUE >*/
+#line 164 ""
+/* L200: */
+#line 164 ""
+ }
+/*< LOLD = LL >*/
+#line 165 ""
+ lold = ll;
+/*< CH = CHS >*/
+#line 166 ""
+ ch = chs;
+/*< PROFIT = PROFIT + IP >*/
+#line 167 ""
+ profit += ip;
+/*< IF ( LL - (N - 2) ) 240, 220, 210 >*/
+#line 168 ""
+ if ((i__1 = ll - (*n - 2)) < 0) {
+#line 168 ""
+ goto L240;
+#line 168 ""
+ } else if (i__1 == 0) {
+#line 168 ""
+ goto L220;
+#line 168 ""
+ } else {
+#line 168 ""
+ goto L210;
+#line 168 ""
+ }
+/*< 210 II = N >*/
+#line 169 ""
+L210:
+#line 169 ""
+ ii = *n;
+/*< GO TO 250 >*/
+#line 170 ""
+ goto L250;
+/*< 220 IF ( CH .LT. W(N) ) GO TO 230 >*/
+#line 171 ""
+L220:
+#line 171 ""
+ if (ch < w[*n]) {
+#line 171 ""
+ goto L230;
+#line 171 ""
+ }
+/*< CH = CH - W(N) >*/
+#line 172 ""
+ ch -= w[*n];
+/*< PROFIT = PROFIT + P(N) >*/
+#line 173 ""
+ profit += p[*n];
+/*< XX(N) = 1 >*/
+#line 174 ""
+ xx[*n] = 1;
+/*< 230 II = N - 1 >*/
+#line 175 ""
+L230:
+#line 175 ""
+ ii = *n - 1;
+/*< GO TO 250 >*/
+#line 176 ""
+ goto L250;
+/*< 240 II = LL + 2 >*/
+#line 177 ""
+L240:
+#line 177 ""
+ ii = ll + 2;
+/*< IF ( CH .GE. MIN(II-1) ) GO TO 80 >*/
+#line 178 ""
+ if (ch >= min__[ii - 1]) {
+#line 178 ""
+ goto L80;
+#line 178 ""
+ }
+/* SAVE THE CURRENT OPTIMAL SOLUTION. */
+/*< 250 IF ( Z .GE. PROFIT ) GO TO 270 >*/
+#line 180 ""
+L250:
+#line 180 ""
+ if (*z__ >= profit) {
+#line 180 ""
+ goto L270;
+#line 180 ""
+ }
+/*< Z = PROFIT >*/
+#line 181 ""
+ *z__ = profit;
+/*< DO 260 J=1,N >*/
+#line 182 ""
+ i__1 = *n;
+#line 182 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< X(J) = XX(J) >*/
+#line 183 ""
+ x[j] = xx[j];
+/*< 260 CONTINUE >*/
+#line 184 ""
+/* L260: */
+#line 184 ""
+ }
+/*< IF ( Z .EQ. LIM ) RETURN >*/
+#line 185 ""
+ if (*z__ == lim) {
+#line 185 ""
+ return 0;
+#line 185 ""
+ }
+/*< 270 IF ( XX(N) .EQ. 0 ) GO TO 280 >*/
+#line 186 ""
+L270:
+#line 186 ""
+ if (xx[*n] == 0) {
+#line 186 ""
+ goto L280;
+#line 186 ""
+ }
+/*< XX(N) = 0 >*/
+#line 187 ""
+ xx[*n] = 0;
+/*< CH = CH + W(N) >*/
+#line 188 ""
+ ch += w[*n];
+/*< PROFIT = PROFIT - P(N) >*/
+#line 189 ""
+ profit -= p[*n];
+/* BACKTRACK. */
+/*< 280 NN = II - 1 >*/
+#line 191 ""
+L280:
+#line 191 ""
+ nn = ii - 1;
+/*< IF ( NN .EQ. 0 ) RETURN >*/
+#line 192 ""
+ if (nn == 0) {
+#line 192 ""
+ return 0;
+#line 192 ""
+ }
+/*< DO 290 J=1,NN >*/
+#line 193 ""
+ i__1 = nn;
+#line 193 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< KK = II - J >*/
+#line 194 ""
+ kk = ii - j;
+/*< IF ( XX(KK) .EQ. 1 ) GO TO 300 >*/
+#line 195 ""
+ if (xx[kk] == 1) {
+#line 195 ""
+ goto L300;
+#line 195 ""
+ }
+/*< 290 CONTINUE >*/
+#line 196 ""
+/* L290: */
+#line 196 ""
+ }
+/*< RETURN >*/
+#line 197 ""
+ return 0;
+/*< 300 R = CH >*/
+#line 198 ""
+L300:
+#line 198 ""
+ r__ = ch;
+/*< CH = CH + W(KK) >*/
+#line 199 ""
+ ch += w[kk];
+/*< PROFIT = PROFIT - P(KK) >*/
+#line 200 ""
+ profit -= p[kk];
+/*< XX(KK) = 0 >*/
+#line 201 ""
+ xx[kk] = 0;
+/*< IF ( R .LT. MIN(KK) ) GO TO 310 >*/
+#line 202 ""
+ if (r__ < min__[kk]) {
+#line 202 ""
+ goto L310;
+#line 202 ""
+ }
+/*< II = KK + 1 >*/
+#line 203 ""
+ ii = kk + 1;
+/*< GO TO 80 >*/
+#line 204 ""
+ goto L80;
+/*< 310 NN = KK + 1 >*/
+#line 205 ""
+L310:
+#line 205 ""
+ nn = kk + 1;
+/*< II = KK >*/
+#line 206 ""
+ ii = kk;
+/* TRY TO SUBSTITUTE THE NN-TH ITEM FOR THE KK-TH. */
+/*< 320 IF ( Z .GE. PROFIT + CH*P(NN)/W(NN) ) GO TO 280 >*/
+#line 208 ""
+L320:
+#line 208 ""
+ if (*z__ >= profit + ch * p[nn] / w[nn]) {
+#line 208 ""
+ goto L280;
+#line 208 ""
+ }
+/*< DIFF = W(NN) - W(KK) >*/
+#line 209 ""
+ diff = w[nn] - w[kk];
+/*< IF ( DIFF ) 370, 330, 340 >*/
+#line 210 ""
+ if (diff < 0) {
+#line 210 ""
+ goto L370;
+#line 210 ""
+ } else if (diff == 0) {
+#line 210 ""
+ goto L330;
+#line 210 ""
+ } else {
+#line 210 ""
+ goto L340;
+#line 210 ""
+ }
+/*< 330 NN = NN + 1 >*/
+#line 211 ""
+L330:
+#line 211 ""
+ ++nn;
+/*< GO TO 320 >*/
+#line 212 ""
+ goto L320;
+/*< 340 IF ( DIFF .GT. R ) GO TO 330 >*/
+#line 213 ""
+L340:
+#line 213 ""
+ if (diff > r__) {
+#line 213 ""
+ goto L330;
+#line 213 ""
+ }
+/*< IF ( Z .GE. PROFIT + P(NN) ) GO TO 330 >*/
+#line 214 ""
+ if (*z__ >= profit + p[nn]) {
+#line 214 ""
+ goto L330;
+#line 214 ""
+ }
+/*< Z = PROFIT + P(NN) >*/
+#line 215 ""
+ *z__ = profit + p[nn];
+/*< DO 350 J=1,KK >*/
+#line 216 ""
+ i__1 = kk;
+#line 216 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< X(J) = XX(J) >*/
+#line 217 ""
+ x[j] = xx[j];
+/*< 350 CONTINUE >*/
+#line 218 ""
+/* L350: */
+#line 218 ""
+ }
+/*< JJ = KK + 1 >*/
+#line 219 ""
+ jj = kk + 1;
+/*< DO 360 J=JJ,N >*/
+#line 220 ""
+ i__1 = *n;
+#line 220 ""
+ for (j = jj; j <= i__1; ++j) {
+/*< X(J) = 0 >*/
+#line 221 ""
+ x[j] = 0;
+/*< 360 CONTINUE >*/
+#line 222 ""
+/* L360: */
+#line 222 ""
+ }
+/*< X(NN) = 1 >*/
+#line 223 ""
+ x[nn] = 1;
+/*< IF ( Z .EQ. LIM ) RETURN >*/
+#line 224 ""
+ if (*z__ == lim) {
+#line 224 ""
+ return 0;
+#line 224 ""
+ }
+/*< R = R - DIFF >*/
+#line 225 ""
+ r__ -= diff;
+/*< KK = NN >*/
+#line 226 ""
+ kk = nn;
+/*< NN = NN + 1 >*/
+#line 227 ""
+ ++nn;
+/*< GO TO 320 >*/
+#line 228 ""
+ goto L320;
+/*< 370 T = R - DIFF >*/
+#line 229 ""
+L370:
+#line 229 ""
+ t = r__ - diff;
+/*< IF ( T .LT. MIN(NN) ) GO TO 330 >*/
+#line 230 ""
+ if (t < min__[nn]) {
+#line 230 ""
+ goto L330;
+#line 230 ""
+ }
+/*< IF ( Z .GE. PROFIT + P(NN) + T*P(NN+1)/W(NN+1)) GO TO 280 >*/
+#line 231 ""
+ if (*z__ >= profit + p[nn] + t * p[nn + 1] / w[nn + 1]) {
+#line 231 ""
+ goto L280;
+#line 231 ""
+ }
+/*< CH = CH - W(NN) >*/
+#line 232 ""
+ ch -= w[nn];
+/*< PROFIT = PROFIT + P(NN) >*/
+#line 233 ""
+ profit += p[nn];
+/*< XX(NN) = 1 >*/
+#line 234 ""
+ xx[nn] = 1;
+/*< II = NN + 1 >*/
+#line 235 ""
+ ii = nn + 1;
+/*< WSIGN(NN) = W(NN) >*/
+#line 236 ""
+ wsign[nn] = w[nn];
+/*< PSIGN(NN) = P(NN) >*/
+#line 237 ""
+ psign[nn] = p[nn];
+/*< ZSIGN(NN) = II >*/
+#line 238 ""
+ zsign[nn] = ii;
+/*< N1 = NN + 1 >*/
+#line 239 ""
+ n1 = nn + 1;
+/*< DO 380 J=N1,LOLD >*/
+#line 240 ""
+ i__1 = lold;
+#line 240 ""
+ for (j = n1; j <= i__1; ++j) {
+/*< WSIGN(J) = 0 >*/
+#line 241 ""
+ wsign[j] = 0;
+/*< PSIGN(J) = 0 >*/
+#line 242 ""
+ psign[j] = 0;
+/*< ZSIGN(J) = J >*/
+#line 243 ""
+ zsign[j] = j;
+/*< 380 CONTINUE >*/
+#line 244 ""
+/* L380: */
+#line 244 ""
+ }
+/*< LOLD = NN >*/
+#line 245 ""
+ lold = nn;
+/*< GO TO 80 >*/
+#line 246 ""
+ goto L80;
+/*< END >*/
+} /* mt1_ */
+
+/*< SUBROUTINE CHMT1(N,P,W,C,Z,JDIM) >*/
+#if 1 /* by mao */
+static
+#endif
+/* Subroutine */ int chmt1_(integer *n, integer *p, integer *w, integer *c__,
+ integer *z__, integer *jdim)
+{
+ /* System generated locals */
+ integer i__1;
+
+ /* Local variables */
+ static integer j;
+ static real r__, rr;
+ static integer jsw;
+
+
+/* CHECK THE INPUT DATA. */
+
+/*< INTEGER P(JDIM),W(JDIM),C,Z >*/
+/*< IF ( N .GE. 2 .AND. N .LE. JDIM - 1 ) GO TO 10 >*/
+#line 253 ""
+ /* Parameter adjustments */
+#line 253 ""
+ --w;
+#line 253 ""
+ --p;
+#line 253 ""
+
+#line 253 ""
+ /* Function Body */
+#line 253 ""
+ if (*n >= 2 && *n <= *jdim - 1) {
+#line 253 ""
+ goto L10;
+#line 253 ""
+ }
+/*< Z = - 1 >*/
+#line 254 ""
+ *z__ = -1;
+/*< RETURN >*/
+#line 255 ""
+ return 0;
+/*< 10 IF ( C .GT. 0 ) GO TO 30 >*/
+#line 256 ""
+L10:
+#line 256 ""
+ if (*c__ > 0) {
+#line 256 ""
+ goto L30;
+#line 256 ""
+ }
+/*< 20 Z = - 2 >*/
+#line 257 ""
+L20:
+#line 257 ""
+ *z__ = -2;
+/*< RETURN >*/
+#line 258 ""
+ return 0;
+/*< 30 JSW = 0 >*/
+#line 259 ""
+L30:
+#line 259 ""
+ jsw = 0;
+/*< RR = FLOAT(P(1))/FLOAT(W(1)) >*/
+#line 260 ""
+ rr = (real) p[1] / (real) w[1];
+/*< DO 50 J=1,N >*/
+#line 261 ""
+ i__1 = *n;
+#line 261 ""
+ for (j = 1; j <= i__1; ++j) {
+/*< R = RR >*/
+#line 262 ""
+ r__ = rr;
+/*< IF ( P(J) .LE. 0 ) GO TO 20 >*/
+#line 263 ""
+ if (p[j] <= 0) {
+#line 263 ""
+ goto L20;
+#line 263 ""
+ }
+/*< IF ( W(J) .LE. 0 ) GO TO 20 >*/
+#line 264 ""
+ if (w[j] <= 0) {
+#line 264 ""
+ goto L20;
+#line 264 ""
+ }
+/*< JSW = JSW + W(J) >*/
+#line 265 ""
+ jsw += w[j];
+/*< IF ( W(J) .LE. C ) GO TO 40 >*/
+#line 266 ""
+ if (w[j] <= *c__) {
+#line 266 ""
+ goto L40;
+#line 266 ""
+ }
+/*< Z = - 3 >*/
+#line 267 ""
+ *z__ = -3;
+/*< RETURN >*/
+#line 268 ""
+ return 0;
+/*< 40 RR = FLOAT(P(J))/FLOAT(W(J)) >*/
+#line 269 ""
+L40:
+#line 269 ""
+ rr = (real) p[j] / (real) w[j];
+/*< IF ( RR .LE. R ) GO TO 50 >*/
+#line 270 ""
+ if (rr <= r__) {
+#line 270 ""
+ goto L50;
+#line 270 ""
+ }
+/*< Z = - 5 >*/
+#line 271 ""
+ *z__ = -5;
+/*< RETURN >*/
+#line 272 ""
+ return 0;
+/*< 50 CONTINUE >*/
+#line 273 ""
+L50:
+#line 273 ""
+ ;
+#line 273 ""
+ }
+/*< IF ( JSW .GT. C ) RETURN >*/
+#line 274 ""
+ if (jsw > *c__) {
+#line 274 ""
+ return 0;
+#line 274 ""
+ }
+/*< Z = - 4 >*/
+#line 275 ""
+ *z__ = -4;
+/*< RETURN >*/
+#line 276 ""
+ return 0;
+/*< END >*/
+} /* chmt1_ */
+
+#if 1 /* by mao */
+int mt1(int n, int p[], int w[], int c, int x[], int jck, int xx[],
+ int min[], int psign[], int wsign[], int zsign[])
+{ /* solve 0-1 knapsack problem */
+ int z, jdim = n+1, j, s1, s2;
+ mt1_(&n, &p[1], &w[1], &c, &z, &x[1], &jdim, &jck, &xx[1],
+ &min[1], &psign[1], &wsign[1], &zsign[1]);
+ /* check solution found */
+ s1 = s2 = 0;
+ for (j = 1; j <= n; j++)
+ { xassert(x[j] == 0 || x[j] == 1);
+ if (x[j])
+ s1 += p[j], s2 += w[j];
+ }
+ xassert(s1 == z);
+ xassert(s2 <= c);
+ return z;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mt1.f b/test/monniaux/glpk-4.65/src/misc/mt1.f
new file mode 100644
index 00000000..82cc4a1b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mt1.f
@@ -0,0 +1,277 @@
+ SUBROUTINE MT1(N,P,W,C,Z,X,JDIM,JCK,XX,MIN,PSIGN,WSIGN,ZSIGN)
+C
+C THIS SUBROUTINE SOLVES THE 0-1 SINGLE KNAPSACK PROBLEM
+C
+C MAXIMIZE Z = P(1)*X(1) + ... + P(N)*X(N)
+C
+C SUBJECT TO: W(1)*X(1) + ... + W(N)*X(N) .LE. C ,
+C X(J) = 0 OR 1 FOR J=1,...,N.
+C
+C THE PROGRAM IS INCLUDED IN THE VOLUME
+C S. MARTELLO, P. TOTH, "KNAPSACK PROBLEMS: ALGORITHMS
+C AND COMPUTER IMPLEMENTATIONS", JOHN WILEY, 1990
+C AND IMPLEMENTS THE BRANCH-AND-BOUND ALGORITHM DESCRIBED IN
+C SECTION 2.5.2 .
+C THE PROGRAM DERIVES FROM AN EARLIER CODE PRESENTED IN
+C S. MARTELLO, P. TOTH, "ALGORITHM FOR THE SOLUTION OF THE 0-1 SINGLE
+C KNAPSACK PROBLEM", COMPUTING, 1978.
+C
+C THE INPUT PROBLEM MUST SATISFY THE CONDITIONS
+C
+C 1) 2 .LE. N .LE. JDIM - 1 ;
+C 2) P(J), W(J), C POSITIVE INTEGERS;
+C 3) MAX (W(J)) .LE. C ;
+C 4) W(1) + ... + W(N) .GT. C ;
+C 5) P(J)/W(J) .GE. P(J+1)/W(J+1) FOR J=1,...,N-1.
+C
+C MT1 CALLS 1 PROCEDURE: CHMT1.
+C
+C THE PROGRAM IS COMPLETELY SELF-CONTAINED AND COMMUNICATION TO IT IS
+C ACHIEVED SOLELY THROUGH THE PARAMETER LIST OF MT1.
+C NO MACHINE-DEPENDENT CONSTANT IS USED.
+C THE PROGRAM IS WRITTEN IN 1967 AMERICAN NATIONAL STANDARD FORTRAN
+C AND IS ACCEPTED BY THE PFORT VERIFIER (PFORT IS THE PORTABLE
+C SUBSET OF ANSI DEFINED BY THE ASSOCIATION FOR COMPUTING MACHINERY).
+C THE PROGRAM HAS BEEN TESTED ON A DIGITAL VAX 11/780 AND AN H.P.
+C 9000/840.
+C
+C MT1 NEEDS 8 ARRAYS ( P , W , X , XX , MIN , PSIGN , WSIGN
+C AND ZSIGN ) OF LENGTH AT LEAST N + 1 .
+C
+C MEANING OF THE INPUT PARAMETERS:
+C N = NUMBER OF ITEMS;
+C P(J) = PROFIT OF ITEM J (J=1,...,N);
+C W(J) = WEIGHT OF ITEM J (J=1,...,N);
+C C = CAPACITY OF THE KNAPSACK;
+C JDIM = DIMENSION OF THE 8 ARRAYS;
+C JCK = 1 IF CHECK ON THE INPUT DATA IS DESIRED,
+C = 0 OTHERWISE.
+C
+C MEANING OF THE OUTPUT PARAMETERS:
+C Z = VALUE OF THE OPTIMAL SOLUTION IF Z .GT. 0 ,
+C = ERROR IN THE INPUT DATA (WHEN JCK=1) IF Z .LT. 0 : CONDI-
+C TION - Z IS VIOLATED;
+C X(J) = 1 IF ITEM J IS IN THE OPTIMAL SOLUTION,
+C = 0 OTHERWISE.
+C
+C ARRAYS XX, MIN, PSIGN, WSIGN AND ZSIGN ARE DUMMY.
+C
+C ALL THE PARAMETERS ARE INTEGER. ON RETURN OF MT1 ALL THE INPUT
+C PARAMETERS ARE UNCHANGED.
+C
+ INTEGER P(JDIM),W(JDIM),X(JDIM),C,Z
+ INTEGER XX(JDIM),MIN(JDIM),PSIGN(JDIM),WSIGN(JDIM),ZSIGN(JDIM)
+ INTEGER CH,CHS,DIFF,PROFIT,R,T
+ Z = 0
+ IF ( JCK .EQ. 1 ) CALL CHMT1(N,P,W,C,Z,JDIM)
+ IF ( Z .LT. 0 ) RETURN
+C INITIALIZE.
+ CH = C
+ IP = 0
+ CHS = CH
+ DO 10 LL=1,N
+ IF ( W(LL) .GT. CHS ) GO TO 20
+ IP = IP + P(LL)
+ CHS = CHS - W(LL)
+ 10 CONTINUE
+ 20 LL = LL - 1
+ IF ( CHS .EQ. 0 ) GO TO 50
+ P(N+1) = 0
+ W(N+1) = CH + 1
+ LIM = IP + CHS*P(LL+2)/W(LL+2)
+ A = W(LL+1) - CHS
+ B = IP + P(LL+1)
+ LIM1 = B - A*FLOAT(P(LL))/FLOAT(W(LL))
+ IF ( LIM1 .GT. LIM ) LIM = LIM1
+ MINK = CH + 1
+ MIN(N) = MINK
+ DO 30 J=2,N
+ KK = N + 2 - J
+ IF ( W(KK) .LT. MINK ) MINK = W(KK)
+ MIN(KK-1) = MINK
+ 30 CONTINUE
+ DO 40 J=1,N
+ XX(J) = 0
+ 40 CONTINUE
+ Z = 0
+ PROFIT = 0
+ LOLD = N
+ II = 1
+ GO TO 170
+ 50 Z = IP
+ DO 60 J=1,LL
+ X(J) = 1
+ 60 CONTINUE
+ NN = LL + 1
+ DO 70 J=NN,N
+ X(J) = 0
+ 70 CONTINUE
+ RETURN
+C TRY TO INSERT THE II-TH ITEM INTO THE CURRENT SOLUTION.
+ 80 IF ( W(II) .LE. CH ) GO TO 90
+ II1 = II + 1
+ IF ( Z .GE. CH*P(II1)/W(II1) + PROFIT ) GO TO 280
+ II = II1
+ GO TO 80
+C BUILD A NEW CURRENT SOLUTION.
+ 90 IP = PSIGN(II)
+ CHS = CH - WSIGN(II)
+ IN = ZSIGN(II)
+ DO 100 LL=IN,N
+ IF ( W(LL) .GT. CHS ) GO TO 160
+ IP = IP + P(LL)
+ CHS = CHS - W(LL)
+ 100 CONTINUE
+ LL = N
+ 110 IF ( Z .GE. IP + PROFIT ) GO TO 280
+ Z = IP + PROFIT
+ NN = II - 1
+ DO 120 J=1,NN
+ X(J) = XX(J)
+ 120 CONTINUE
+ DO 130 J=II,LL
+ X(J) = 1
+ 130 CONTINUE
+ IF ( LL .EQ. N ) GO TO 150
+ NN = LL + 1
+ DO 140 J=NN,N
+ X(J) = 0
+ 140 CONTINUE
+ 150 IF ( Z .NE. LIM ) GO TO 280
+ RETURN
+ 160 IU = CHS*P(LL)/W(LL)
+ LL = LL - 1
+ IF ( IU .EQ. 0 ) GO TO 110
+ IF ( Z .GE. PROFIT + IP + IU ) GO TO 280
+C SAVE THE CURRENT SOLUTION.
+ 170 WSIGN(II) = CH - CHS
+ PSIGN(II) = IP
+ ZSIGN(II) = LL + 1
+ XX(II) = 1
+ NN = LL - 1
+ IF ( NN .LT. II) GO TO 190
+ DO 180 J=II,NN
+ WSIGN(J+1) = WSIGN(J) - W(J)
+ PSIGN(J+1) = PSIGN(J) - P(J)
+ ZSIGN(J+1) = LL + 1
+ XX(J+1) = 1
+ 180 CONTINUE
+ 190 J1 = LL + 1
+ DO 200 J=J1,LOLD
+ WSIGN(J) = 0
+ PSIGN(J) = 0
+ ZSIGN(J) = J
+ 200 CONTINUE
+ LOLD = LL
+ CH = CHS
+ PROFIT = PROFIT + IP
+ IF ( LL - (N - 2) ) 240, 220, 210
+ 210 II = N
+ GO TO 250
+ 220 IF ( CH .LT. W(N) ) GO TO 230
+ CH = CH - W(N)
+ PROFIT = PROFIT + P(N)
+ XX(N) = 1
+ 230 II = N - 1
+ GO TO 250
+ 240 II = LL + 2
+ IF ( CH .GE. MIN(II-1) ) GO TO 80
+C SAVE THE CURRENT OPTIMAL SOLUTION.
+ 250 IF ( Z .GE. PROFIT ) GO TO 270
+ Z = PROFIT
+ DO 260 J=1,N
+ X(J) = XX(J)
+ 260 CONTINUE
+ IF ( Z .EQ. LIM ) RETURN
+ 270 IF ( XX(N) .EQ. 0 ) GO TO 280
+ XX(N) = 0
+ CH = CH + W(N)
+ PROFIT = PROFIT - P(N)
+C BACKTRACK.
+ 280 NN = II - 1
+ IF ( NN .EQ. 0 ) RETURN
+ DO 290 J=1,NN
+ KK = II - J
+ IF ( XX(KK) .EQ. 1 ) GO TO 300
+ 290 CONTINUE
+ RETURN
+ 300 R = CH
+ CH = CH + W(KK)
+ PROFIT = PROFIT - P(KK)
+ XX(KK) = 0
+ IF ( R .LT. MIN(KK) ) GO TO 310
+ II = KK + 1
+ GO TO 80
+ 310 NN = KK + 1
+ II = KK
+C TRY TO SUBSTITUTE THE NN-TH ITEM FOR THE KK-TH.
+ 320 IF ( Z .GE. PROFIT + CH*P(NN)/W(NN) ) GO TO 280
+ DIFF = W(NN) - W(KK)
+ IF ( DIFF ) 370, 330, 340
+ 330 NN = NN + 1
+ GO TO 320
+ 340 IF ( DIFF .GT. R ) GO TO 330
+ IF ( Z .GE. PROFIT + P(NN) ) GO TO 330
+ Z = PROFIT + P(NN)
+ DO 350 J=1,KK
+ X(J) = XX(J)
+ 350 CONTINUE
+ JJ = KK + 1
+ DO 360 J=JJ,N
+ X(J) = 0
+ 360 CONTINUE
+ X(NN) = 1
+ IF ( Z .EQ. LIM ) RETURN
+ R = R - DIFF
+ KK = NN
+ NN = NN + 1
+ GO TO 320
+ 370 T = R - DIFF
+ IF ( T .LT. MIN(NN) ) GO TO 330
+ IF ( Z .GE. PROFIT + P(NN) + T*P(NN+1)/W(NN+1)) GO TO 280
+ CH = CH - W(NN)
+ PROFIT = PROFIT + P(NN)
+ XX(NN) = 1
+ II = NN + 1
+ WSIGN(NN) = W(NN)
+ PSIGN(NN) = P(NN)
+ ZSIGN(NN) = II
+ N1 = NN + 1
+ DO 380 J=N1,LOLD
+ WSIGN(J) = 0
+ PSIGN(J) = 0
+ ZSIGN(J) = J
+ 380 CONTINUE
+ LOLD = NN
+ GO TO 80
+ END
+ SUBROUTINE CHMT1(N,P,W,C,Z,JDIM)
+C
+C CHECK THE INPUT DATA.
+C
+ INTEGER P(JDIM),W(JDIM),C,Z
+ IF ( N .GE. 2 .AND. N .LE. JDIM - 1 ) GO TO 10
+ Z = - 1
+ RETURN
+ 10 IF ( C .GT. 0 ) GO TO 30
+ 20 Z = - 2
+ RETURN
+ 30 JSW = 0
+ RR = FLOAT(P(1))/FLOAT(W(1))
+ DO 50 J=1,N
+ R = RR
+ IF ( P(J) .LE. 0 ) GO TO 20
+ IF ( W(J) .LE. 0 ) GO TO 20
+ JSW = JSW + W(J)
+ IF ( W(J) .LE. C ) GO TO 40
+ Z = - 3
+ RETURN
+ 40 RR = FLOAT(P(J))/FLOAT(W(J))
+ IF ( RR .LE. R ) GO TO 50
+ Z = - 5
+ RETURN
+ 50 CONTINUE
+ IF ( JSW .GT. C ) RETURN
+ Z = - 4
+ RETURN
+ END
diff --git a/test/monniaux/glpk-4.65/src/misc/mt1.h b/test/monniaux/glpk-4.65/src/misc/mt1.h
new file mode 100644
index 00000000..cceebba9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mt1.h
@@ -0,0 +1,34 @@
+/* mt1.h (0-1 knapsack problem; Martello & Toth algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2017-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MT1_H
+#define MT1_H
+
+#define mt1 _glp_mt1
+int mt1(int n, int p[], int w[], int c, int x[], int jck, int xx[],
+ int min[], int psign[], int wsign[], int zsign[]);
+/* solve 0-1 single knapsack problem */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mygmp.c b/test/monniaux/glpk-4.65/src/misc/mygmp.c
new file mode 100644
index 00000000..89d053ae
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mygmp.c
@@ -0,0 +1,1162 @@
+/* mygmp.c (integer and rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mygmp.h"
+
+#ifdef HAVE_GMP /* use GNU MP library */
+
+/* nothing is needed */
+
+#else /* use GLPK MP module */
+
+#include "bignum.h"
+#include "dmp.h"
+#include "env.h"
+
+#define gmp_pool env->gmp_pool
+#define gmp_size env->gmp_size
+#define gmp_work env->gmp_work
+
+void *gmp_get_atom(int size)
+{ ENV *env = get_env_ptr();
+ if (gmp_pool == NULL)
+ gmp_pool = dmp_create_pool();
+ return dmp_get_atom(gmp_pool, size);
+}
+
+void gmp_free_atom(void *ptr, int size)
+{ ENV *env = get_env_ptr();
+ xassert(gmp_pool != NULL);
+ dmp_free_atom(gmp_pool, ptr, size);
+ return;
+}
+
+int gmp_pool_count(void)
+{ ENV *env = get_env_ptr();
+ if (gmp_pool == NULL)
+ return 0;
+ else
+ return dmp_in_use(gmp_pool);
+}
+
+unsigned short *gmp_get_work(int size)
+{ ENV *env = get_env_ptr();
+ xassert(size > 0);
+ if (gmp_size < size)
+ { if (gmp_size == 0)
+ { xassert(gmp_work == NULL);
+ gmp_size = 100;
+ }
+ else
+ { xassert(gmp_work != NULL);
+ xfree(gmp_work);
+ }
+ while (gmp_size < size)
+ gmp_size += gmp_size;
+ gmp_work = xcalloc(gmp_size, sizeof(unsigned short));
+ }
+ return gmp_work;
+}
+
+void gmp_free_mem(void)
+{ ENV *env = get_env_ptr();
+ if (gmp_pool != NULL)
+ dmp_delete_pool(gmp_pool);
+ if (gmp_work != NULL)
+ xfree(gmp_work);
+ gmp_pool = NULL;
+ gmp_size = 0;
+ gmp_work = NULL;
+ return;
+}
+
+/*--------------------------------------------------------------------*/
+
+mpz_t _mpz_init(void)
+{ /* initialize x and set its value to 0 */
+ mpz_t x;
+ x = gmp_get_atom(sizeof(struct mpz));
+ x->val = 0;
+ x->ptr = NULL;
+ return x;
+}
+
+void mpz_clear(mpz_t x)
+{ /* free the space occupied by x */
+ mpz_set_si(x, 0);
+ xassert(x->ptr == NULL);
+ /* free the number descriptor */
+ gmp_free_atom(x, sizeof(struct mpz));
+ return;
+}
+
+void mpz_set(mpz_t z, mpz_t x)
+{ /* set the value of z from x */
+ struct mpz_seg *e, *ee, *es;
+ if (z != x)
+ { mpz_set_si(z, 0);
+ z->val = x->val;
+ xassert(z->ptr == NULL);
+ for (e = x->ptr, es = NULL; e != NULL; e = e->next)
+ { ee = gmp_get_atom(sizeof(struct mpz_seg));
+ memcpy(ee->d, e->d, 12);
+ ee->next = NULL;
+ if (z->ptr == NULL)
+ z->ptr = ee;
+ else
+ es->next = ee;
+ es = ee;
+ }
+ }
+ return;
+}
+
+void mpz_set_si(mpz_t x, int val)
+{ /* set the value of x to val */
+ struct mpz_seg *e;
+ /* free existing segments, if any */
+ while (x->ptr != NULL)
+ { e = x->ptr;
+ x->ptr = e->next;
+ gmp_free_atom(e, sizeof(struct mpz_seg));
+ }
+ /* assign new value */
+ if (val == 0x80000000)
+ { /* long format is needed */
+ x->val = -1;
+ x->ptr = e = gmp_get_atom(sizeof(struct mpz_seg));
+ memset(e->d, 0, 12);
+ e->d[1] = 0x8000;
+ e->next = NULL;
+ }
+ else
+ { /* short format is enough */
+ x->val = val;
+ }
+ return;
+}
+
+double mpz_get_d(mpz_t x)
+{ /* convert x to a double, truncating if necessary */
+ struct mpz_seg *e;
+ int j;
+ double val, deg;
+ if (x->ptr == NULL)
+ val = (double)x->val;
+ else
+ { xassert(x->val != 0);
+ val = 0.0;
+ deg = 1.0;
+ for (e = x->ptr; e != NULL; e = e->next)
+ { for (j = 0; j <= 5; j++)
+ { val += deg * (double)((int)e->d[j]);
+ deg *= 65536.0;
+ }
+ }
+ if (x->val < 0)
+ val = - val;
+ }
+ return val;
+}
+
+double mpz_get_d_2exp(int *exp, mpz_t x)
+{ /* convert x to a double, truncating if necessary (i.e. rounding
+ * towards zero), and returning the exponent separately;
+ * the return value is in the range 0.5 <= |d| < 1 and the
+ * exponent is stored to *exp; d*2^exp is the (truncated) x value;
+ * if x is zero, the return is 0.0 and 0 is stored to *exp;
+ * this is similar to the standard C frexp function */
+ struct mpz_seg *e;
+ int j, n, n1;
+ double val;
+ if (x->ptr == NULL)
+ val = (double)x->val, n = 0;
+ else
+ { xassert(x->val != 0);
+ val = 0.0, n = 0;
+ for (e = x->ptr; e != NULL; e = e->next)
+ { for (j = 0; j <= 5; j++)
+ { val += (double)((int)e->d[j]);
+ val /= 65536.0, n += 16;
+ }
+ }
+ if (x->val < 0)
+ val = - val;
+ }
+ val = frexp(val, &n1);
+ *exp = n + n1;
+ return val;
+}
+
+void mpz_swap(mpz_t x, mpz_t y)
+{ /* swap the values x and y efficiently */
+ int val;
+ void *ptr;
+ val = x->val, ptr = x->ptr;
+ x->val = y->val, x->ptr = y->ptr;
+ y->val = val, y->ptr = ptr;
+ return;
+}
+
+static void normalize(mpz_t x)
+{ /* normalize integer x that includes removing non-significant
+ * (leading) zeros and converting to short format, if possible */
+ struct mpz_seg *es, *e;
+ /* if the integer is in short format, it remains unchanged */
+ if (x->ptr == NULL)
+ { xassert(x->val != 0x80000000);
+ goto done;
+ }
+ xassert(x->val == +1 || x->val == -1);
+ /* find the last (most significant) non-zero segment */
+ es = NULL;
+ for (e = x->ptr; e != NULL; e = e->next)
+ { if (e->d[0] || e->d[1] || e->d[2] ||
+ e->d[3] || e->d[4] || e->d[5])
+ es = e;
+ }
+ /* if all segments contain zeros, the integer is zero */
+ if (es == NULL)
+ { mpz_set_si(x, 0);
+ goto done;
+ }
+ /* remove non-significant (leading) zero segments */
+ while (es->next != NULL)
+ { e = es->next;
+ es->next = e->next;
+ gmp_free_atom(e, sizeof(struct mpz_seg));
+ }
+ /* convert the integer to short format, if possible */
+ e = x->ptr;
+ if (e->next == NULL && e->d[1] <= 0x7FFF &&
+ !e->d[2] && !e->d[3] && !e->d[4] && !e->d[5])
+ { int val;
+ val = (int)e->d[0] + ((int)e->d[1] << 16);
+ if (x->val < 0)
+ val = - val;
+ mpz_set_si(x, val);
+ }
+done: return;
+}
+
+void mpz_add(mpz_t z, mpz_t x, mpz_t y)
+{ /* set z to x + y */
+ static struct mpz_seg zero = { { 0, 0, 0, 0, 0, 0 }, NULL };
+ struct mpz_seg dumx, dumy, *ex, *ey, *ez, *es, *ee;
+ int k, sx, sy, sz;
+ unsigned int t;
+ /* if [x] = 0 then [z] = [y] */
+ if (x->val == 0)
+ { xassert(x->ptr == NULL);
+ mpz_set(z, y);
+ goto done;
+ }
+ /* if [y] = 0 then [z] = [x] */
+ if (y->val == 0)
+ { xassert(y->ptr == NULL);
+ mpz_set(z, x);
+ goto done;
+ }
+ /* special case when both [x] and [y] are in short format */
+ if (x->ptr == NULL && y->ptr == NULL)
+ { int xval = x->val, yval = y->val, zval = x->val + y->val;
+ xassert(xval != 0x80000000 && yval != 0x80000000);
+ if (!(xval > 0 && yval > 0 && zval <= 0 ||
+ xval < 0 && yval < 0 && zval >= 0))
+ { mpz_set_si(z, zval);
+ goto done;
+ }
+ }
+ /* convert [x] to long format, if necessary */
+ if (x->ptr == NULL)
+ { xassert(x->val != 0x80000000);
+ if (x->val >= 0)
+ { sx = +1;
+ t = (unsigned int)(+ x->val);
+ }
+ else
+ { sx = -1;
+ t = (unsigned int)(- x->val);
+ }
+ ex = &dumx;
+ ex->d[0] = (unsigned short)t;
+ ex->d[1] = (unsigned short)(t >> 16);
+ ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0;
+ ex->next = NULL;
+ }
+ else
+ { sx = x->val;
+ xassert(sx == +1 || sx == -1);
+ ex = x->ptr;
+ }
+ /* convert [y] to long format, if necessary */
+ if (y->ptr == NULL)
+ { xassert(y->val != 0x80000000);
+ if (y->val >= 0)
+ { sy = +1;
+ t = (unsigned int)(+ y->val);
+ }
+ else
+ { sy = -1;
+ t = (unsigned int)(- y->val);
+ }
+ ey = &dumy;
+ ey->d[0] = (unsigned short)t;
+ ey->d[1] = (unsigned short)(t >> 16);
+ ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0;
+ ey->next = NULL;
+ }
+ else
+ { sy = y->val;
+ xassert(sy == +1 || sy == -1);
+ ey = y->ptr;
+ }
+ /* main fragment */
+ sz = sx;
+ ez = es = NULL;
+ if (sx > 0 && sy > 0 || sx < 0 && sy < 0)
+ { /* [x] and [y] have identical signs -- addition */
+ t = 0;
+ for (; ex || ey; ex = ex->next, ey = ey->next)
+ { if (ex == NULL)
+ ex = &zero;
+ if (ey == NULL)
+ ey = &zero;
+ ee = gmp_get_atom(sizeof(struct mpz_seg));
+ for (k = 0; k <= 5; k++)
+ { t += (unsigned int)ex->d[k];
+ t += (unsigned int)ey->d[k];
+ ee->d[k] = (unsigned short)t;
+ t >>= 16;
+ }
+ ee->next = NULL;
+ if (ez == NULL)
+ ez = ee;
+ else
+ es->next = ee;
+ es = ee;
+ }
+ if (t)
+ { /* overflow -- one extra digit is needed */
+ ee = gmp_get_atom(sizeof(struct mpz_seg));
+ ee->d[0] = 1;
+ ee->d[1] = ee->d[2] = ee->d[3] = ee->d[4] = ee->d[5] = 0;
+ ee->next = NULL;
+ xassert(es != NULL);
+ es->next = ee;
+ }
+ }
+ else
+ { /* [x] and [y] have different signs -- subtraction */
+ t = 1;
+ for (; ex || ey; ex = ex->next, ey = ey->next)
+ { if (ex == NULL)
+ ex = &zero;
+ if (ey == NULL)
+ ey = &zero;
+ ee = gmp_get_atom(sizeof(struct mpz_seg));
+ for (k = 0; k <= 5; k++)
+ { t += (unsigned int)ex->d[k];
+ t += (0xFFFF - (unsigned int)ey->d[k]);
+ ee->d[k] = (unsigned short)t;
+ t >>= 16;
+ }
+ ee->next = NULL;
+ if (ez == NULL)
+ ez = ee;
+ else
+ es->next = ee;
+ es = ee;
+ }
+ if (!t)
+ { /* |[x]| < |[y]| -- result in complement coding */
+ sz = - sz;
+ t = 1;
+ for (ee = ez; ee != NULL; ee = ee->next)
+ { for (k = 0; k <= 5; k++)
+ { t += (0xFFFF - (unsigned int)ee->d[k]);
+ ee->d[k] = (unsigned short)t;
+ t >>= 16;
+ }
+ }
+ }
+ }
+ /* contruct and normalize result */
+ mpz_set_si(z, 0);
+ z->val = sz;
+ z->ptr = ez;
+ normalize(z);
+done: return;
+}
+
+void mpz_sub(mpz_t z, mpz_t x, mpz_t y)
+{ /* set z to x - y */
+ if (x == y)
+ mpz_set_si(z, 0);
+ else
+ { y->val = - y->val;
+ mpz_add(z, x, y);
+ if (y != z)
+ y->val = - y->val;
+ }
+ return;
+}
+
+void mpz_mul(mpz_t z, mpz_t x, mpz_t y)
+{ /* set z to x * y */
+ struct mpz_seg dumx, dumy, *ex, *ey, *es, *e;
+ int sx, sy, k, nx, ny, n;
+ unsigned int t;
+ unsigned short *work, *wx, *wy;
+ /* if [x] = 0 then [z] = 0 */
+ if (x->val == 0)
+ { xassert(x->ptr == NULL);
+ mpz_set_si(z, 0);
+ goto done;
+ }
+ /* if [y] = 0 then [z] = 0 */
+ if (y->val == 0)
+ { xassert(y->ptr == NULL);
+ mpz_set_si(z, 0);
+ goto done;
+ }
+ /* special case when both [x] and [y] are in short format */
+ if (x->ptr == NULL && y->ptr == NULL)
+ { int xval = x->val, yval = y->val, sz = +1;
+ xassert(xval != 0x80000000 && yval != 0x80000000);
+ if (xval < 0)
+ xval = - xval, sz = - sz;
+ if (yval < 0)
+ yval = - yval, sz = - sz;
+ if (xval <= 0x7FFFFFFF / yval)
+ { mpz_set_si(z, sz * (xval * yval));
+ goto done;
+ }
+ }
+ /* convert [x] to long format, if necessary */
+ if (x->ptr == NULL)
+ { xassert(x->val != 0x80000000);
+ if (x->val >= 0)
+ { sx = +1;
+ t = (unsigned int)(+ x->val);
+ }
+ else
+ { sx = -1;
+ t = (unsigned int)(- x->val);
+ }
+ ex = &dumx;
+ ex->d[0] = (unsigned short)t;
+ ex->d[1] = (unsigned short)(t >> 16);
+ ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0;
+ ex->next = NULL;
+ }
+ else
+ { sx = x->val;
+ xassert(sx == +1 || sx == -1);
+ ex = x->ptr;
+ }
+ /* convert [y] to long format, if necessary */
+ if (y->ptr == NULL)
+ { xassert(y->val != 0x80000000);
+ if (y->val >= 0)
+ { sy = +1;
+ t = (unsigned int)(+ y->val);
+ }
+ else
+ { sy = -1;
+ t = (unsigned int)(- y->val);
+ }
+ ey = &dumy;
+ ey->d[0] = (unsigned short)t;
+ ey->d[1] = (unsigned short)(t >> 16);
+ ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0;
+ ey->next = NULL;
+ }
+ else
+ { sy = y->val;
+ xassert(sy == +1 || sy == -1);
+ ey = y->ptr;
+ }
+ /* determine the number of digits of [x] */
+ nx = n = 0;
+ for (e = ex; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++)
+ { n++;
+ if (e->d[k])
+ nx = n;
+ }
+ }
+ xassert(nx > 0);
+ /* determine the number of digits of [y] */
+ ny = n = 0;
+ for (e = ey; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++)
+ { n++;
+ if (e->d[k])
+ ny = n;
+ }
+ }
+ xassert(ny > 0);
+ /* we need working array containing at least nx+ny+ny places */
+ work = gmp_get_work(nx+ny+ny);
+ /* load digits of [x] */
+ wx = &work[0];
+ for (n = 0; n < nx; n++)
+ wx[ny+n] = 0;
+ for (n = 0, e = ex; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++, n++)
+ { if (e->d[k])
+ wx[ny+n] = e->d[k];
+ }
+ }
+ /* load digits of [y] */
+ wy = &work[nx+ny];
+ for (n = 0; n < ny; n++) wy[n] = 0;
+ for (n = 0, e = ey; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++, n++)
+ { if (e->d[k])
+ wy[n] = e->d[k];
+ }
+ }
+ /* compute [x] * [y] */
+ bigmul(nx, ny, wx, wy);
+ /* construct and normalize result */
+ mpz_set_si(z, 0);
+ z->val = sx * sy;
+ es = NULL;
+ k = 6;
+ for (n = 0; n < nx+ny; n++)
+ { if (k > 5)
+ { e = gmp_get_atom(sizeof(struct mpz_seg));
+ e->d[0] = e->d[1] = e->d[2] = 0;
+ e->d[3] = e->d[4] = e->d[5] = 0;
+ e->next = NULL;
+ if (z->ptr == NULL)
+ z->ptr = e;
+ else
+ es->next = e;
+ es = e;
+ k = 0;
+ }
+ es->d[k++] = wx[n];
+ }
+ normalize(z);
+done: return;
+}
+
+void mpz_neg(mpz_t z, mpz_t x)
+{ /* set z to 0 - x */
+ mpz_set(z, x);
+ z->val = - z->val;
+ return;
+}
+
+void mpz_abs(mpz_t z, mpz_t x)
+{ /* set z to the absolute value of x */
+ mpz_set(z, x);
+ if (z->val < 0)
+ z->val = - z->val;
+ return;
+}
+
+void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y)
+{ /* divide x by y, forming quotient q and/or remainder r
+ * if q = NULL then quotient is not stored; if r = NULL then
+ * remainder is not stored
+ * the sign of quotient is determined as in algebra while the
+ * sign of remainder is the same as the sign of dividend:
+ * +26 : +7 = +3, remainder is +5
+ * -26 : +7 = -3, remainder is -5
+ * +26 : -7 = -3, remainder is +5
+ * -26 : -7 = +3, remainder is -5 */
+ struct mpz_seg dumx, dumy, *ex, *ey, *es, *e;
+ int sx, sy, k, nx, ny, n;
+ unsigned int t;
+ unsigned short *work, *wx, *wy;
+ /* divide by zero is not allowed */
+ if (y->val == 0)
+ { xassert(y->ptr == NULL);
+ xerror("mpz_div: divide by zero not allowed\n");
+ }
+ /* if [x] = 0 then [q] = [r] = 0 */
+ if (x->val == 0)
+ { xassert(x->ptr == NULL);
+ if (q != NULL)
+ mpz_set_si(q, 0);
+ if (r != NULL)
+ mpz_set_si(r, 0);
+ goto done;
+ }
+ /* special case when both [x] and [y] are in short format */
+ if (x->ptr == NULL && y->ptr == NULL)
+ { int xval = x->val, yval = y->val;
+ xassert(xval != 0x80000000 && yval != 0x80000000);
+ /* FIXME: use div function */
+ if (q != NULL)
+ mpz_set_si(q, xval / yval);
+ if (r != NULL)
+ mpz_set_si(r, xval % yval);
+ goto done;
+ }
+ /* convert [x] to long format, if necessary */
+ if (x->ptr == NULL)
+ { xassert(x->val != 0x80000000);
+ if (x->val >= 0)
+ { sx = +1;
+ t = (unsigned int)(+ x->val);
+ }
+ else
+ { sx = -1;
+ t = (unsigned int)(- x->val);
+ }
+ ex = &dumx;
+ ex->d[0] = (unsigned short)t;
+ ex->d[1] = (unsigned short)(t >> 16);
+ ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0;
+ ex->next = NULL;
+ }
+ else
+ { sx = x->val;
+ xassert(sx == +1 || sx == -1);
+ ex = x->ptr;
+ }
+ /* convert [y] to long format, if necessary */
+ if (y->ptr == NULL)
+ { xassert(y->val != 0x80000000);
+ if (y->val >= 0)
+ { sy = +1;
+ t = (unsigned int)(+ y->val);
+ }
+ else
+ { sy = -1;
+ t = (unsigned int)(- y->val);
+ }
+ ey = &dumy;
+ ey->d[0] = (unsigned short)t;
+ ey->d[1] = (unsigned short)(t >> 16);
+ ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0;
+ ey->next = NULL;
+ }
+ else
+ { sy = y->val;
+ xassert(sy == +1 || sy == -1);
+ ey = y->ptr;
+ }
+ /* determine the number of digits of [x] */
+ nx = n = 0;
+ for (e = ex; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++)
+ { n++;
+ if (e->d[k])
+ nx = n;
+ }
+ }
+ xassert(nx > 0);
+ /* determine the number of digits of [y] */
+ ny = n = 0;
+ for (e = ey; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++)
+ { n++;
+ if (e->d[k])
+ ny = n;
+ }
+ }
+ xassert(ny > 0);
+ /* if nx < ny then [q] = 0 and [r] = [x] */
+ if (nx < ny)
+ { if (r != NULL)
+ mpz_set(r, x);
+ if (q != NULL)
+ mpz_set_si(q, 0);
+ goto done;
+ }
+ /* we need working array containing at least nx+ny+1 places */
+ work = gmp_get_work(nx+ny+1);
+ /* load digits of [x] */
+ wx = &work[0];
+ for (n = 0; n < nx; n++)
+ wx[n] = 0;
+ for (n = 0, e = ex; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++, n++)
+ if (e->d[k]) wx[n] = e->d[k];
+ }
+ /* load digits of [y] */
+ wy = &work[nx+1];
+ for (n = 0; n < ny; n++)
+ wy[n] = 0;
+ for (n = 0, e = ey; e != NULL; e = e->next)
+ { for (k = 0; k <= 5; k++, n++)
+ if (e->d[k]) wy[n] = e->d[k];
+ }
+ /* compute quotient and remainder */
+ xassert(wy[ny-1] != 0);
+ bigdiv(nx-ny, ny, wx, wy);
+ /* construct and normalize quotient */
+ if (q != NULL)
+ { mpz_set_si(q, 0);
+ q->val = sx * sy;
+ es = NULL;
+ k = 6;
+ for (n = ny; n <= nx; n++)
+ { if (k > 5)
+ { e = gmp_get_atom(sizeof(struct mpz_seg));
+ e->d[0] = e->d[1] = e->d[2] = 0;
+ e->d[3] = e->d[4] = e->d[5] = 0;
+ e->next = NULL;
+ if (q->ptr == NULL)
+ q->ptr = e;
+ else
+ es->next = e;
+ es = e;
+ k = 0;
+ }
+ es->d[k++] = wx[n];
+ }
+ normalize(q);
+ }
+ /* construct and normalize remainder */
+ if (r != NULL)
+ { mpz_set_si(r, 0);
+ r->val = sx;
+ es = NULL;
+ k = 6;
+ for (n = 0; n < ny; n++)
+ { if (k > 5)
+ { e = gmp_get_atom(sizeof(struct mpz_seg));
+ e->d[0] = e->d[1] = e->d[2] = 0;
+ e->d[3] = e->d[4] = e->d[5] = 0;
+ e->next = NULL;
+ if (r->ptr == NULL)
+ r->ptr = e;
+ else
+ es->next = e;
+ es = e;
+ k = 0;
+ }
+ es->d[k++] = wx[n];
+ }
+ normalize(r);
+ }
+done: return;
+}
+
+void mpz_gcd(mpz_t z, mpz_t x, mpz_t y)
+{ /* set z to the greatest common divisor of x and y */
+ /* in case of arbitrary integers GCD(x, y) = GCD(|x|, |y|), and,
+ * in particular, GCD(0, 0) = 0 */
+ mpz_t u, v, r;
+ mpz_init(u);
+ mpz_init(v);
+ mpz_init(r);
+ mpz_abs(u, x);
+ mpz_abs(v, y);
+ while (mpz_sgn(v))
+ { mpz_div(NULL, r, u, v);
+ mpz_set(u, v);
+ mpz_set(v, r);
+ }
+ mpz_set(z, u);
+ mpz_clear(u);
+ mpz_clear(v);
+ mpz_clear(r);
+ return;
+}
+
+int mpz_cmp(mpz_t x, mpz_t y)
+{ /* compare x and y; return a positive value if x > y, zero if
+ * x = y, or a nefative value if x < y */
+ static struct mpz_seg zero = { { 0, 0, 0, 0, 0, 0 }, NULL };
+ struct mpz_seg dumx, dumy, *ex, *ey;
+ int cc, sx, sy, k;
+ unsigned int t;
+ if (x == y)
+ { cc = 0;
+ goto done;
+ }
+ /* special case when both [x] and [y] are in short format */
+ if (x->ptr == NULL && y->ptr == NULL)
+ { int xval = x->val, yval = y->val;
+ xassert(xval != 0x80000000 && yval != 0x80000000);
+ cc = (xval > yval ? +1 : xval < yval ? -1 : 0);
+ goto done;
+ }
+ /* special case when [x] and [y] have different signs */
+ if (x->val > 0 && y->val <= 0 || x->val == 0 && y->val < 0)
+ { cc = +1;
+ goto done;
+ }
+ if (x->val < 0 && y->val >= 0 || x->val == 0 && y->val > 0)
+ { cc = -1;
+ goto done;
+ }
+ /* convert [x] to long format, if necessary */
+ if (x->ptr == NULL)
+ { xassert(x->val != 0x80000000);
+ if (x->val >= 0)
+ { sx = +1;
+ t = (unsigned int)(+ x->val);
+ }
+ else
+ { sx = -1;
+ t = (unsigned int)(- x->val);
+ }
+ ex = &dumx;
+ ex->d[0] = (unsigned short)t;
+ ex->d[1] = (unsigned short)(t >> 16);
+ ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0;
+ ex->next = NULL;
+ }
+ else
+ { sx = x->val;
+ xassert(sx == +1 || sx == -1);
+ ex = x->ptr;
+ }
+ /* convert [y] to long format, if necessary */
+ if (y->ptr == NULL)
+ { xassert(y->val != 0x80000000);
+ if (y->val >= 0)
+ { sy = +1;
+ t = (unsigned int)(+ y->val);
+ }
+ else
+ { sy = -1;
+ t = (unsigned int)(- y->val);
+ }
+ ey = &dumy;
+ ey->d[0] = (unsigned short)t;
+ ey->d[1] = (unsigned short)(t >> 16);
+ ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0;
+ ey->next = NULL;
+ }
+ else
+ { sy = y->val;
+ xassert(sy == +1 || sy == -1);
+ ey = y->ptr;
+ }
+ /* main fragment */
+ xassert(sx > 0 && sy > 0 || sx < 0 && sy < 0);
+ cc = 0;
+ for (; ex || ey; ex = ex->next, ey = ey->next)
+ { if (ex == NULL)
+ ex = &zero;
+ if (ey == NULL)
+ ey = &zero;
+ for (k = 0; k <= 5; k++)
+ { if (ex->d[k] > ey->d[k])
+ cc = +1;
+ if (ex->d[k] < ey->d[k])
+ cc = -1;
+ }
+ }
+ if (sx < 0) cc = - cc;
+done: return cc;
+}
+
+int mpz_sgn(mpz_t x)
+{ /* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */
+ int s;
+ s = (x->val > 0 ? +1 : x->val < 0 ? -1 : 0);
+ return s;
+}
+
+int mpz_out_str(void *_fp, int base, mpz_t x)
+{ /* output x on stream fp, as a string in given base; the base
+ * may vary from 2 to 36;
+ * return the number of bytes written, or if an error occurred,
+ * return 0 */
+ FILE *fp = _fp;
+ mpz_t b, y, r;
+ int n, j, nwr = 0;
+ unsigned char *d;
+ static char *set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ if (!(2 <= base && base <= 36))
+ xerror("mpz_out_str: base = %d; invalid base\n", base);
+ mpz_init(b);
+ mpz_set_si(b, base);
+ mpz_init(y);
+ mpz_init(r);
+ /* determine the number of digits */
+ mpz_abs(y, x);
+ for (n = 0; mpz_sgn(y) != 0; n++)
+ mpz_div(y, NULL, y, b);
+ if (n == 0) n = 1;
+ /* compute the digits */
+ d = xmalloc(n);
+ mpz_abs(y, x);
+ for (j = 0; j < n; j++)
+ { mpz_div(y, r, y, b);
+ xassert(0 <= r->val && r->val < base && r->ptr == NULL);
+ d[j] = (unsigned char)r->val;
+ }
+ /* output the integer to the stream */
+ if (fp == NULL)
+ fp = stdout;
+ if (mpz_sgn(x) < 0)
+ fputc('-', fp), nwr++;
+ for (j = n-1; j >= 0; j--)
+ fputc(set[d[j]], fp), nwr++;
+ if (ferror(fp))
+ nwr = 0;
+ mpz_clear(b);
+ mpz_clear(y);
+ mpz_clear(r);
+ xfree(d);
+ return nwr;
+}
+
+/*--------------------------------------------------------------------*/
+
+mpq_t _mpq_init(void)
+{ /* initialize x, and set its value to 0/1 */
+ mpq_t x;
+ x = gmp_get_atom(sizeof(struct mpq));
+ x->p.val = 0;
+ x->p.ptr = NULL;
+ x->q.val = 1;
+ x->q.ptr = NULL;
+ return x;
+}
+
+void mpq_clear(mpq_t x)
+{ /* free the space occupied by x */
+ mpz_set_si(&x->p, 0);
+ xassert(x->p.ptr == NULL);
+ mpz_set_si(&x->q, 0);
+ xassert(x->q.ptr == NULL);
+ /* free the number descriptor */
+ gmp_free_atom(x, sizeof(struct mpq));
+ return;
+}
+
+void mpq_canonicalize(mpq_t x)
+{ /* remove any factors that are common to the numerator and
+ * denominator of x, and make the denominator positive */
+ mpz_t f;
+ xassert(x->q.val != 0);
+ if (x->q.val < 0)
+ { mpz_neg(&x->p, &x->p);
+ mpz_neg(&x->q, &x->q);
+ }
+ mpz_init(f);
+ mpz_gcd(f, &x->p, &x->q);
+ if (!(f->val == 1 && f->ptr == NULL))
+ { mpz_div(&x->p, NULL, &x->p, f);
+ mpz_div(&x->q, NULL, &x->q, f);
+ }
+ mpz_clear(f);
+ return;
+}
+
+void mpq_set(mpq_t z, mpq_t x)
+{ /* set the value of z from x */
+ if (z != x)
+ { mpz_set(&z->p, &x->p);
+ mpz_set(&z->q, &x->q);
+ }
+ return;
+}
+
+void mpq_set_si(mpq_t x, int p, unsigned int q)
+{ /* set the value of x to p/q */
+ if (q == 0)
+ xerror("mpq_set_si: zero denominator not allowed\n");
+ mpz_set_si(&x->p, p);
+ xassert(q <= 0x7FFFFFFF);
+ mpz_set_si(&x->q, q);
+ return;
+}
+
+double mpq_get_d(mpq_t x)
+{ /* convert x to a double, truncating if necessary */
+ int np, nq;
+ double p, q;
+ p = mpz_get_d_2exp(&np, &x->p);
+ q = mpz_get_d_2exp(&nq, &x->q);
+ return ldexp(p / q, np - nq);
+}
+
+void mpq_set_d(mpq_t x, double val)
+{ /* set x to val; there is no rounding, the conversion is exact */
+ int s, n, d, j;
+ double f;
+ mpz_t temp;
+ xassert(-DBL_MAX <= val && val <= +DBL_MAX);
+ mpq_set_si(x, 0, 1);
+ if (val > 0.0)
+ s = +1;
+ else if (val < 0.0)
+ s = -1;
+ else
+ goto done;
+ f = frexp(fabs(val), &n);
+ /* |val| = f * 2^n, where 0.5 <= f < 1.0 */
+ mpz_init(temp);
+ while (f != 0.0)
+ { f *= 16.0, n -= 4;
+ d = (int)f;
+ xassert(0 <= d && d <= 15);
+ f -= (double)d;
+ /* x := 16 * x + d */
+ mpz_set_si(temp, 16);
+ mpz_mul(&x->p, &x->p, temp);
+ mpz_set_si(temp, d);
+ mpz_add(&x->p, &x->p, temp);
+ }
+ mpz_clear(temp);
+ /* x := x * 2^n */
+ if (n > 0)
+ { for (j = 1; j <= n; j++)
+ mpz_add(&x->p, &x->p, &x->p);
+ }
+ else if (n < 0)
+ { for (j = 1; j <= -n; j++)
+ mpz_add(&x->q, &x->q, &x->q);
+ mpq_canonicalize(x);
+ }
+ if (s < 0)
+ mpq_neg(x, x);
+done: return;
+}
+
+void mpq_add(mpq_t z, mpq_t x, mpq_t y)
+{ /* set z to x + y */
+ mpz_t p, q;
+ mpz_init(p);
+ mpz_init(q);
+ mpz_mul(p, &x->p, &y->q);
+ mpz_mul(q, &x->q, &y->p);
+ mpz_add(p, p, q);
+ mpz_mul(q, &x->q, &y->q);
+ mpz_set(&z->p, p);
+ mpz_set(&z->q, q);
+ mpz_clear(p);
+ mpz_clear(q);
+ mpq_canonicalize(z);
+ return;
+}
+
+void mpq_sub(mpq_t z, mpq_t x, mpq_t y)
+{ /* set z to x - y */
+ mpz_t p, q;
+ mpz_init(p);
+ mpz_init(q);
+ mpz_mul(p, &x->p, &y->q);
+ mpz_mul(q, &x->q, &y->p);
+ mpz_sub(p, p, q);
+ mpz_mul(q, &x->q, &y->q);
+ mpz_set(&z->p, p);
+ mpz_set(&z->q, q);
+ mpz_clear(p);
+ mpz_clear(q);
+ mpq_canonicalize(z);
+ return;
+}
+
+void mpq_mul(mpq_t z, mpq_t x, mpq_t y)
+{ /* set z to x * y */
+ mpz_mul(&z->p, &x->p, &y->p);
+ mpz_mul(&z->q, &x->q, &y->q);
+ mpq_canonicalize(z);
+ return;
+}
+
+void mpq_div(mpq_t z, mpq_t x, mpq_t y)
+{ /* set z to x / y */
+ mpz_t p, q;
+ if (mpq_sgn(y) == 0)
+ xerror("mpq_div: zero divisor not allowed\n");
+ mpz_init(p);
+ mpz_init(q);
+ mpz_mul(p, &x->p, &y->q);
+ mpz_mul(q, &x->q, &y->p);
+ mpz_set(&z->p, p);
+ mpz_set(&z->q, q);
+ mpz_clear(p);
+ mpz_clear(q);
+ mpq_canonicalize(z);
+ return;
+}
+
+void mpq_neg(mpq_t z, mpq_t x)
+{ /* set z to 0 - x */
+ mpq_set(z, x);
+ mpz_neg(&z->p, &z->p);
+ return;
+}
+
+void mpq_abs(mpq_t z, mpq_t x)
+{ /* set z to the absolute value of x */
+ mpq_set(z, x);
+ mpz_abs(&z->p, &z->p);
+ xassert(mpz_sgn(&x->q) > 0);
+ return;
+}
+
+int mpq_cmp(mpq_t x, mpq_t y)
+{ /* compare x and y; return a positive value if x > y, zero if
+ * x = y, or a negative value if x < y */
+ mpq_t temp;
+ int s;
+ mpq_init(temp);
+ mpq_sub(temp, x, y);
+ s = mpq_sgn(temp);
+ mpq_clear(temp);
+ return s;
+}
+
+int mpq_sgn(mpq_t x)
+{ /* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */
+ int s;
+ s = mpz_sgn(&x->p);
+ xassert(mpz_sgn(&x->q) > 0);
+ return s;
+}
+
+int mpq_out_str(void *_fp, int base, mpq_t x)
+{ /* output x on stream fp, as a string in given base; the base
+ * may vary from 2 to 36; output is in the form 'num/den' or if
+ * the denominator is 1 then just 'num';
+ * if the parameter fp is a null pointer, stdout is assumed;
+ * return the number of bytes written, or if an error occurred,
+ * return 0 */
+ FILE *fp = _fp;
+ int nwr;
+ if (!(2 <= base && base <= 36))
+ xerror("mpq_out_str: base = %d; invalid base\n", base);
+ if (fp == NULL)
+ fp = stdout;
+ nwr = mpz_out_str(fp, base, &x->p);
+ if (x->q.val == 1 && x->q.ptr == NULL)
+ ;
+ else
+ { fputc('/', fp), nwr++;
+ nwr += mpz_out_str(fp, base, &x->q);
+ }
+ if (ferror(fp))
+ nwr = 0;
+ return nwr;
+}
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/mygmp.h b/test/monniaux/glpk-4.65/src/misc/mygmp.h
new file mode 100644
index 00000000..31d2024d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/mygmp.h
@@ -0,0 +1,254 @@
+/* mygmp.h (integer and rational arithmetic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2008-2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MYGMP_H
+#define MYGMP_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_GMP /* use GNU MP library */
+
+#include <gmp.h>
+
+#define gmp_pool_count() 0
+
+#define gmp_free_mem() ((void)0)
+
+#else /* use GLPK MP module */
+
+/***********************************************************************
+* INTEGER NUMBERS
+* ---------------
+* Depending on its magnitude an integer number of arbitrary precision
+* is represented either in short format or in long format.
+*
+* Short format corresponds to the int type and allows representing
+* integer numbers in the range [-(2^31-1), +(2^31-1)]. Note that for
+* the most negative number of int type the short format is not used.
+*
+* In long format integer numbers are represented using the positional
+* system with the base (radix) 2^16 = 65536:
+*
+* x = (-1)^s sum{j in 0..n-1} d[j] * 65536^j,
+*
+* where x is the integer to be represented, s is its sign (+1 or -1),
+* d[j] are its digits (0 <= d[j] <= 65535).
+*
+* RATIONAL NUMBERS
+* ----------------
+* A rational number is represented as an irreducible fraction:
+*
+* p / q,
+*
+* where p (numerator) and q (denominator) are integer numbers (q > 0)
+* having no common divisors. */
+
+struct mpz
+{ /* integer number */
+ int val;
+ /* if ptr is a null pointer, the number is in short format, and
+ val is its value; otherwise, the number is in long format, and
+ val is its sign (+1 or -1) */
+ struct mpz_seg *ptr;
+ /* pointer to the linked list of the number segments ordered in
+ ascending of powers of the base */
+};
+
+struct mpz_seg
+{ /* integer number segment */
+ unsigned short d[6];
+ /* six digits of the number ordered in ascending of powers of the
+ base */
+ struct mpz_seg *next;
+ /* pointer to the next number segment */
+};
+
+struct mpq
+{ /* rational number (p / q) */
+ struct mpz p;
+ /* numerator */
+ struct mpz q;
+ /* denominator */
+};
+
+typedef struct mpz *mpz_t;
+typedef struct mpq *mpq_t;
+
+#define gmp_get_atom _glp_gmp_get_atom
+void *gmp_get_atom(int size);
+
+#define gmp_free_atom _glp_gmp_free_atom
+void gmp_free_atom(void *ptr, int size);
+
+#define gmp_pool_count _glp_gmp_pool_count
+int gmp_pool_count(void);
+
+#define gmp_get_work _glp_gmp_get_work
+unsigned short *gmp_get_work(int size);
+
+#define gmp_free_mem _glp_gmp_free_mem
+void gmp_free_mem(void);
+
+#define mpz_init(x) (void)((x) = _mpz_init())
+
+#define _mpz_init _glp_mpz_init
+mpz_t _mpz_init(void);
+/* initialize x and set its value to 0 */
+
+#define mpz_clear _glp_mpz_clear
+void mpz_clear(mpz_t x);
+/* free the space occupied by x */
+
+#define mpz_set _glp_mpz_set
+void mpz_set(mpz_t z, mpz_t x);
+/* set the value of z from x */
+
+#define mpz_set_si _glp_mpz_set_si
+void mpz_set_si(mpz_t x, int val);
+/* set the value of x to val */
+
+#define mpz_get_d _glp_mpz_get_d
+double mpz_get_d(mpz_t x);
+/* convert x to a double, truncating if necessary */
+
+#define mpz_get_d_2exp _glp_mpz_get_d_2exp
+double mpz_get_d_2exp(int *exp, mpz_t x);
+/* convert x to a double, returning the exponent separately */
+
+#define mpz_swap _glp_mpz_swap
+void mpz_swap(mpz_t x, mpz_t y);
+/* swap the values x and y efficiently */
+
+#define mpz_add _glp_mpz_add
+void mpz_add(mpz_t, mpz_t, mpz_t);
+/* set z to x + y */
+
+#define mpz_sub _glp_mpz_sub
+void mpz_sub(mpz_t, mpz_t, mpz_t);
+/* set z to x - y */
+
+#define mpz_mul _glp_mpz_mul
+void mpz_mul(mpz_t, mpz_t, mpz_t);
+/* set z to x * y */
+
+#define mpz_neg _glp_mpz_neg
+void mpz_neg(mpz_t z, mpz_t x);
+/* set z to 0 - x */
+
+#define mpz_abs _glp_mpz_abs
+void mpz_abs(mpz_t z, mpz_t x);
+/* set z to the absolute value of x */
+
+#define mpz_div _glp_mpz_div
+void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y);
+/* divide x by y, forming quotient q and/or remainder r */
+
+#define mpz_gcd _glp_mpz_gcd
+void mpz_gcd(mpz_t z, mpz_t x, mpz_t y);
+/* set z to the greatest common divisor of x and y */
+
+#define mpz_cmp _glp_mpz_cmp
+int mpz_cmp(mpz_t x, mpz_t y);
+/* compare x and y */
+
+#define mpz_sgn _glp_mpz_sgn
+int mpz_sgn(mpz_t x);
+/* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */
+
+#define mpz_out_str _glp_mpz_out_str
+int mpz_out_str(void *fp, int base, mpz_t x);
+/* output x on stream fp, as a string in given base */
+
+#define mpq_init(x) (void)((x) = _mpq_init())
+
+#define _mpq_init _glp_mpq_init
+mpq_t _mpq_init(void);
+/* initialize x, and set its value to 0/1 */
+
+#define mpq_clear _glp_mpq_clear
+void mpq_clear(mpq_t x);
+/* free the space occupied by x */
+
+#define mpq_canonicalize _glp_mpq_canonicalize
+void mpq_canonicalize(mpq_t x);
+/* canonicalize x */
+
+#define mpq_set _glp_mpq_set
+void mpq_set(mpq_t z, mpq_t x);
+/* set the value of z from x */
+
+#define mpq_set_si _glp_mpq_set_si
+void mpq_set_si(mpq_t x, int p, unsigned int q);
+/* set the value of x to p/q */
+
+#define mpq_get_d _glp_mpq_get_d
+double mpq_get_d(mpq_t x);
+/* convert x to a double, truncating if necessary */
+
+#define mpq_set_d _glp_mpq_set_d
+void mpq_set_d(mpq_t x, double val);
+/* set x to val; there is no rounding, the conversion is exact */
+
+#define mpq_add _glp_mpq_add
+void mpq_add(mpq_t z, mpq_t x, mpq_t y);
+/* set z to x + y */
+
+#define mpq_sub _glp_mpq_sub
+void mpq_sub(mpq_t z, mpq_t x, mpq_t y);
+/* set z to x - y */
+
+#define mpq_mul _glp_mpq_mul
+void mpq_mul(mpq_t z, mpq_t x, mpq_t y);
+/* set z to x * y */
+
+#define mpq_div _glp_mpq_div
+void mpq_div(mpq_t z, mpq_t x, mpq_t y);
+/* set z to x / y */
+
+#define mpq_neg _glp_mpq_neg
+void mpq_neg(mpq_t z, mpq_t x);
+/* set z to 0 - x */
+
+#define mpq_abs _glp_mpq_abs
+void mpq_abs(mpq_t z, mpq_t x);
+/* set z to the absolute value of x */
+
+#define mpq_cmp _glp_mpq_cmp
+int mpq_cmp(mpq_t x, mpq_t y);
+/* compare x and y */
+
+#define mpq_sgn _glp_mpq_sgn
+int mpq_sgn(mpq_t x);
+/* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */
+
+#define mpq_out_str _glp_mpq_out_str
+int mpq_out_str(void *fp, int base, mpq_t x);
+/* output x on stream fp, as a string in given base */
+
+#endif
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/okalg.c b/test/monniaux/glpk-4.65/src/misc/okalg.c
new file mode 100644
index 00000000..8eecd6df
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/okalg.c
@@ -0,0 +1,382 @@
+/* okalg.c (out-of-kilter algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "okalg.h"
+
+/***********************************************************************
+* NAME
+*
+* okalg - out-of-kilter algorithm
+*
+* SYNOPSIS
+*
+* #include "okalg.h"
+* int okalg(int nv, int na, const int tail[], const int head[],
+* const int low[], const int cap[], const int cost[], int x[],
+* int pi[]);
+*
+* DESCRIPTION
+*
+* The routine okalg implements the out-of-kilter algorithm to find a
+* minimal-cost circulation in the specified flow network.
+*
+* INPUT PARAMETERS
+*
+* nv is the number of nodes, nv >= 0.
+*
+* na is the number of arcs, na >= 0.
+*
+* tail[a], a = 1,...,na, is the index of tail node of arc a.
+*
+* head[a], a = 1,...,na, is the index of head node of arc a.
+*
+* low[a], a = 1,...,na, is an lower bound to the flow through arc a.
+*
+* cap[a], a = 1,...,na, is an upper bound to the flow through arc a,
+* which is the capacity of the arc.
+*
+* cost[a], a = 1,...,na, is a per-unit cost of the flow through arc a.
+*
+* NOTES
+*
+* 1. Multiple arcs are allowed, but self-loops are not allowed.
+*
+* 2. It is required that 0 <= low[a] <= cap[a] for all arcs.
+*
+* 3. Arc costs may have any sign.
+*
+* OUTPUT PARAMETERS
+*
+* x[a], a = 1,...,na, is optimal value of the flow through arc a.
+*
+* pi[i], i = 1,...,nv, is Lagrange multiplier for flow conservation
+* equality constraint corresponding to node i (the node potential).
+*
+* RETURNS
+*
+* 0 optimal circulation found;
+*
+* 1 there is no feasible circulation;
+*
+* 2 integer overflow occured;
+*
+* 3 optimality test failed (logic error).
+*
+* REFERENCES
+*
+* L.R.Ford, Jr., and D.R.Fulkerson, "Flows in Networks," The RAND
+* Corp., Report R-375-PR (August 1962), Chap. III "Minimal Cost Flow
+* Problems," pp.113-26. */
+
+static int overflow(int u, int v)
+{ /* check for integer overflow on computing u + v */
+ if (u > 0 && v > 0 && u + v < 0) return 1;
+ if (u < 0 && v < 0 && u + v > 0) return 1;
+ return 0;
+}
+
+int okalg(int nv, int na, const int tail[], const int head[],
+ const int low[], const int cap[], const int cost[], int x[],
+ int pi[])
+{ int a, aok, delta, i, j, k, lambda, pos1, pos2, s, t, temp, ret,
+ *ptr, *arc, *link, *list;
+ /* sanity checks */
+ xassert(nv >= 0);
+ xassert(na >= 0);
+ for (a = 1; a <= na; a++)
+ { i = tail[a], j = head[a];
+ xassert(1 <= i && i <= nv);
+ xassert(1 <= j && j <= nv);
+ xassert(i != j);
+ xassert(0 <= low[a] && low[a] <= cap[a]);
+ }
+ /* allocate working arrays */
+ ptr = xcalloc(1+nv+1, sizeof(int));
+ arc = xcalloc(1+na+na, sizeof(int));
+ link = xcalloc(1+nv, sizeof(int));
+ list = xcalloc(1+nv, sizeof(int));
+ /* ptr[i] := (degree of node i) */
+ for (i = 1; i <= nv; i++)
+ ptr[i] = 0;
+ for (a = 1; a <= na; a++)
+ { ptr[tail[a]]++;
+ ptr[head[a]]++;
+ }
+ /* initialize arc pointers */
+ ptr[1]++;
+ for (i = 1; i < nv; i++)
+ ptr[i+1] += ptr[i];
+ ptr[nv+1] = ptr[nv];
+ /* build arc lists */
+ for (a = 1; a <= na; a++)
+ { arc[--ptr[tail[a]]] = a;
+ arc[--ptr[head[a]]] = a;
+ }
+ xassert(ptr[1] == 1);
+ xassert(ptr[nv+1] == na+na+1);
+ /* now the indices of arcs incident to node i are stored in
+ * locations arc[ptr[i]], arc[ptr[i]+1], ..., arc[ptr[i+1]-1] */
+ /* initialize arc flows and node potentials */
+ for (a = 1; a <= na; a++)
+ x[a] = 0;
+ for (i = 1; i <= nv; i++)
+ pi[i] = 0;
+loop: /* main loop starts here */
+ /* find out-of-kilter arc */
+ aok = 0;
+ for (a = 1; a <= na; a++)
+ { i = tail[a], j = head[a];
+ if (overflow(cost[a], pi[i] - pi[j]))
+ { ret = 2;
+ goto done;
+ }
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if (x[a] < low[a] || (lambda < 0 && x[a] < cap[a]))
+ { /* arc a = i->j is out of kilter, and we need to increase
+ * the flow through this arc */
+ aok = a, s = j, t = i;
+ break;
+ }
+ if (x[a] > cap[a] || (lambda > 0 && x[a] > low[a]))
+ { /* arc a = i->j is out of kilter, and we need to decrease
+ * the flow through this arc */
+ aok = a, s = i, t = j;
+ break;
+ }
+ }
+ if (aok == 0)
+ { /* all arcs are in kilter */
+ /* check for feasibility */
+ for (a = 1; a <= na; a++)
+ { if (!(low[a] <= x[a] && x[a] <= cap[a]))
+ { ret = 3;
+ goto done;
+ }
+ }
+ for (i = 1; i <= nv; i++)
+ { temp = 0;
+ for (k = ptr[i]; k < ptr[i+1]; k++)
+ { a = arc[k];
+ if (tail[a] == i)
+ { /* a is outgoing arc */
+ temp += x[a];
+ }
+ else if (head[a] == i)
+ { /* a is incoming arc */
+ temp -= x[a];
+ }
+ else
+ xassert(a != a);
+ }
+ if (temp != 0)
+ { ret = 3;
+ goto done;
+ }
+ }
+ /* check for optimality */
+ for (a = 1; a <= na; a++)
+ { i = tail[a], j = head[a];
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if ((lambda > 0 && x[a] != low[a]) ||
+ (lambda < 0 && x[a] != cap[a]))
+ { ret = 3;
+ goto done;
+ }
+ }
+ /* current circulation is optimal */
+ ret = 0;
+ goto done;
+ }
+ /* now we need to find a cycle (t, a, s, ..., t), which allows
+ * increasing the flow along it, where a is the out-of-kilter arc
+ * just found */
+ /* link[i] = 0 means that node i is not labelled yet;
+ * link[i] = a means that arc a immediately precedes node i */
+ /* initially only node s is labelled */
+ for (i = 1; i <= nv; i++)
+ link[i] = 0;
+ link[s] = aok, list[1] = s, pos1 = pos2 = 1;
+ /* breadth first search */
+ while (pos1 <= pos2)
+ { /* dequeue node i */
+ i = list[pos1++];
+ /* consider all arcs incident to node i */
+ for (k = ptr[i]; k < ptr[i+1]; k++)
+ { a = arc[k];
+ if (tail[a] == i)
+ { /* a = i->j is a forward arc from s to t */
+ j = head[a];
+ /* if node j has been labelled, skip the arc */
+ if (link[j] != 0) continue;
+ /* if the arc does not allow increasing the flow through
+ * it, skip the arc */
+ if (x[a] >= cap[a]) continue;
+ if (overflow(cost[a], pi[i] - pi[j]))
+ { ret = 2;
+ goto done;
+ }
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if (lambda > 0 && x[a] >= low[a]) continue;
+ }
+ else if (head[a] == i)
+ { /* a = i<-j is a backward arc from s to t */
+ j = tail[a];
+ /* if node j has been labelled, skip the arc */
+ if (link[j] != 0) continue;
+ /* if the arc does not allow decreasing the flow through
+ * it, skip the arc */
+ if (x[a] <= low[a]) continue;
+ if (overflow(cost[a], pi[j] - pi[i]))
+ { ret = 2;
+ goto done;
+ }
+ lambda = cost[a] + (pi[j] - pi[i]);
+ if (lambda < 0 && x[a] <= cap[a]) continue;
+ }
+ else
+ xassert(a != a);
+ /* label node j and enqueue it */
+ link[j] = a, list[++pos2] = j;
+ /* check for breakthrough */
+ if (j == t) goto brkt;
+ }
+ }
+ /* NONBREAKTHROUGH */
+ /* consider all arcs, whose one endpoint is labelled and other is
+ * not, and determine maximal change of node potentials */
+ delta = 0;
+ for (a = 1; a <= na; a++)
+ { i = tail[a], j = head[a];
+ if (link[i] != 0 && link[j] == 0)
+ { /* a = i->j, where node i is labelled, node j is not */
+ if (overflow(cost[a], pi[i] - pi[j]))
+ { ret = 2;
+ goto done;
+ }
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if (x[a] <= cap[a] && lambda > 0)
+ if (delta == 0 || delta > + lambda) delta = + lambda;
+ }
+ else if (link[i] == 0 && link[j] != 0)
+ { /* a = j<-i, where node j is labelled, node i is not */
+ if (overflow(cost[a], pi[i] - pi[j]))
+ { ret = 2;
+ goto done;
+ }
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if (x[a] >= low[a] && lambda < 0)
+ if (delta == 0 || delta > - lambda) delta = - lambda;
+ }
+ }
+ if (delta == 0)
+ { /* there is no feasible circulation */
+ ret = 1;
+ goto done;
+ }
+ /* increase potentials of all unlabelled nodes */
+ for (i = 1; i <= nv; i++)
+ { if (link[i] == 0)
+ { if (overflow(pi[i], delta))
+ { ret = 2;
+ goto done;
+ }
+ pi[i] += delta;
+ }
+ }
+ goto loop;
+brkt: /* BREAKTHROUGH */
+ /* walk through arcs of the cycle (t, a, s, ..., t) found in the
+ * reverse order and determine maximal change of the flow */
+ delta = 0;
+ for (j = t;; j = i)
+ { /* arc a immediately precedes node j in the cycle */
+ a = link[j];
+ if (head[a] == j)
+ { /* a = i->j is a forward arc of the cycle */
+ i = tail[a];
+ lambda = cost[a] + (pi[i] - pi[j]);
+ if (lambda > 0 && x[a] < low[a])
+ { /* x[a] may be increased until its lower bound */
+ temp = low[a] - x[a];
+ }
+ else if (lambda <= 0 && x[a] < cap[a])
+ { /* x[a] may be increased until its upper bound */
+ temp = cap[a] - x[a];
+ }
+ else
+ xassert(a != a);
+ }
+ else if (tail[a] == j)
+ { /* a = i<-j is a backward arc of the cycle */
+ i = head[a];
+ lambda = cost[a] + (pi[j] - pi[i]);
+ if (lambda < 0 && x[a] > cap[a])
+ { /* x[a] may be decreased until its upper bound */
+ temp = x[a] - cap[a];
+ }
+ else if (lambda >= 0 && x[a] > low[a])
+ { /* x[a] may be decreased until its lower bound */
+ temp = x[a] - low[a];
+ }
+ else
+ xassert(a != a);
+ }
+ else
+ xassert(a != a);
+ if (delta == 0 || delta > temp) delta = temp;
+ /* check for end of the cycle */
+ if (i == t) break;
+ }
+ xassert(delta > 0);
+ /* increase the flow along the cycle */
+ for (j = t;; j = i)
+ { /* arc a immediately precedes node j in the cycle */
+ a = link[j];
+ if (head[a] == j)
+ { /* a = i->j is a forward arc of the cycle */
+ i = tail[a];
+ /* overflow cannot occur */
+ x[a] += delta;
+ }
+ else if (tail[a] == j)
+ { /* a = i<-j is a backward arc of the cycle */
+ i = head[a];
+ /* overflow cannot occur */
+ x[a] -= delta;
+ }
+ else
+ xassert(a != a);
+ /* check for end of the cycle */
+ if (i == t) break;
+ }
+ goto loop;
+done: /* free working arrays */
+ xfree(ptr);
+ xfree(arc);
+ xfree(link);
+ xfree(list);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/okalg.h b/test/monniaux/glpk-4.65/src/misc/okalg.h
new file mode 100644
index 00000000..2f2d9740
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/okalg.h
@@ -0,0 +1,35 @@
+/* okalg.h (out-of-kilter algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef OKALG_H
+#define OKALG_H
+
+#define okalg _glp_okalg
+int okalg(int nv, int na, const int tail[], const int head[],
+ const int low[], const int cap[], const int cost[], int x[],
+ int pi[]);
+/* out-of-kilter algorithm */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/qmd.c b/test/monniaux/glpk-4.65/src/misc/qmd.c
new file mode 100644
index 00000000..a3397dcf
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/qmd.c
@@ -0,0 +1,584 @@
+/* qmd.c (quotient minimum degree algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* THIS CODE IS THE RESULT OF TRANSLATION OF THE FORTRAN SUBROUTINES
+* GENQMD, QMDRCH, QMDQT, QMDUPD, AND QMDMRG FROM THE BOOK:
+*
+* ALAN GEORGE, JOSEPH W-H LIU. COMPUTER SOLUTION OF LARGE SPARSE
+* POSITIVE DEFINITE SYSTEMS. PRENTICE-HALL, 1981.
+*
+* THE TRANSLATION HAS BEEN DONE WITH THE PERMISSION OF THE AUTHORS
+* OF THE ORIGINAL FORTRAN SUBROUTINES: ALAN GEORGE AND JOSEPH LIU,
+* UNIVERSITY OF WATERLOO, WATERLOO, ONTARIO, CANADA.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "qmd.h"
+
+/***********************************************************************
+* NAME
+*
+* genqmd - GENeral Quotient Minimum Degree algorithm
+*
+* SYNOPSIS
+*
+* #include "qmd.h"
+* void genqmd(int *neqns, int xadj[], int adjncy[], int perm[],
+* int invp[], int deg[], int marker[], int rchset[], int nbrhd[],
+* int qsize[], int qlink[], int *nofsub);
+*
+* PURPOSE
+*
+* This routine implements the minimum degree algorithm. It makes use
+* of the implicit representation of the elimination graph by quotient
+* graphs, and the notion of indistinguishable nodes.
+*
+* CAUTION
+*
+* The adjancy vector adjncy will be destroyed.
+*
+* INPUT PARAMETERS
+*
+* neqns - number of equations;
+* (xadj, adjncy) -
+* the adjancy structure.
+*
+* OUTPUT PARAMETERS
+*
+* perm - the minimum degree ordering;
+* invp - the inverse of perm.
+*
+* WORKING PARAMETERS
+*
+* deg - the degree vector. deg[i] is negative means node i has been
+* numbered;
+* marker - a marker vector, where marker[i] is negative means node i
+* has been merged with another nodeand thus can be ignored;
+* rchset - vector used for the reachable set;
+* nbrhd - vector used for neighborhood set;
+* qsize - vector used to store the size of indistinguishable
+* supernodes;
+* qlink - vector used to store indistinguishable nodes, i, qlink[i],
+* qlink[qlink[i]], ... are the members of the supernode
+* represented by i.
+*
+* PROGRAM SUBROUTINES
+*
+* qmdrch, qmdqt, qmdupd.
+***********************************************************************/
+
+void genqmd(int *_neqns, int xadj[], int adjncy[], int perm[],
+ int invp[], int deg[], int marker[], int rchset[], int nbrhd[],
+ int qsize[], int qlink[], int *_nofsub)
+{ int inode, ip, irch, j, mindeg, ndeg, nhdsze, node, np, num,
+ nump1, nxnode, rchsze, search, thresh;
+# define neqns (*_neqns)
+# define nofsub (*_nofsub)
+ /* Initialize degree vector and other working variables. */
+ mindeg = neqns;
+ nofsub = 0;
+ for (node = 1; node <= neqns; node++)
+ { perm[node] = node;
+ invp[node] = node;
+ marker[node] = 0;
+ qsize[node] = 1;
+ qlink[node] = 0;
+ ndeg = xadj[node+1] - xadj[node];
+ deg[node] = ndeg;
+ if (ndeg < mindeg) mindeg = ndeg;
+ }
+ num = 0;
+ /* Perform threshold search to get a node of min degree.
+ * Variable search point to where search should start. */
+s200: search = 1;
+ thresh = mindeg;
+ mindeg = neqns;
+s300: nump1 = num + 1;
+ if (nump1 > search) search = nump1;
+ for (j = search; j <= neqns; j++)
+ { node = perm[j];
+ if (marker[node] >= 0)
+ { ndeg = deg[node];
+ if (ndeg <= thresh) goto s500;
+ if (ndeg < mindeg) mindeg = ndeg;
+ }
+ }
+ goto s200;
+ /* Node has minimum degree. Find its reachable sets by calling
+ * qmdrch. */
+s500: search = j;
+ nofsub += deg[node];
+ marker[node] = 1;
+ qmdrch(&node, xadj, adjncy, deg, marker, &rchsze, rchset, &nhdsze,
+ nbrhd);
+ /* Eliminate all nodes indistinguishable from node. They are given
+ * by node, qlink[node], ... . */
+ nxnode = node;
+s600: num++;
+ np = invp[nxnode];
+ ip = perm[num];
+ perm[np] = ip;
+ invp[ip] = np;
+ perm[num] = nxnode;
+ invp[nxnode] = num;
+ deg[nxnode] = -1;
+ nxnode = qlink[nxnode];
+ if (nxnode > 0) goto s600;
+ if (rchsze > 0)
+ { /* Update the degrees of the nodes in the reachable set and
+ * identify indistinguishable nodes. */
+ qmdupd(xadj, adjncy, &rchsze, rchset, deg, qsize, qlink,
+ marker, &rchset[rchsze+1], &nbrhd[nhdsze+1]);
+ /* Reset marker value of nodes in reach set. Update threshold
+ * value for cyclic search. Also call qmdqt to form new
+ * quotient graph. */
+ marker[node] = 0;
+ for (irch = 1; irch <= rchsze; irch++)
+ { inode = rchset[irch];
+ if (marker[inode] >= 0)
+ { marker[inode] = 0;
+ ndeg = deg[inode];
+ if (ndeg < mindeg) mindeg = ndeg;
+ if (ndeg <= thresh)
+ { mindeg = thresh;
+ thresh = ndeg;
+ search = invp[inode];
+ }
+ }
+ }
+ if (nhdsze > 0)
+ qmdqt(&node, xadj, adjncy, marker, &rchsze, rchset, nbrhd);
+ }
+ if (num < neqns) goto s300;
+ return;
+# undef neqns
+# undef nofsub
+}
+
+/***********************************************************************
+* NAME
+*
+* qmdrch - Quotient MD ReaCHable set
+*
+* SYNOPSIS
+*
+* #include "qmd.h"
+* void qmdrch(int *root, int xadj[], int adjncy[], int deg[],
+* int marker[], int *rchsze, int rchset[], int *nhdsze,
+* int nbrhd[]);
+*
+* PURPOSE
+*
+* This subroutine determines the reachable set of a node through a
+* given subset. The adjancy structure is assumed to be stored in a
+* quotient graph format.
+*
+* INPUT PARAMETERS
+*
+* root - the given node not in the subset;
+* (xadj, adjncy) -
+* the adjancy structure pair;
+* deg - the degree vector. deg[i] < 0 means the node belongs to the
+* given subset.
+*
+* OUTPUT PARAMETERS
+*
+* (rchsze, rchset) -
+* the reachable set;
+* (nhdsze, nbrhd) -
+* the neighborhood set.
+*
+* UPDATED PARAMETERS
+*
+* marker - the marker vector for reach and nbrhd sets. > 0 means the
+* node is in reach set. < 0 means the node has been merged
+* with others in the quotient or it is in nbrhd set.
+***********************************************************************/
+
+void qmdrch(int *_root, int xadj[], int adjncy[], int deg[],
+ int marker[], int *_rchsze, int rchset[], int *_nhdsze,
+ int nbrhd[])
+{ int i, istop, istrt, j, jstop, jstrt, nabor, node;
+# define root (*_root)
+# define rchsze (*_rchsze)
+# define nhdsze (*_nhdsze)
+ /* Loop through the neighbors of root in the quotient graph. */
+ nhdsze = 0;
+ rchsze = 0;
+ istrt = xadj[root];
+ istop = xadj[root+1] - 1;
+ if (istop < istrt) return;
+ for (i = istrt; i <= istop; i++)
+ { nabor = adjncy[i];
+ if (nabor == 0) return;
+ if (marker[nabor] == 0)
+ { if (deg[nabor] >= 0)
+ { /* Include nabor into the reachable set. */
+ rchsze++;
+ rchset[rchsze] = nabor;
+ marker[nabor] = 1;
+ goto s600;
+ }
+ /* nabor has been eliminated. Find nodes reachable from
+ * it. */
+ marker[nabor] = -1;
+ nhdsze++;
+ nbrhd[nhdsze] = nabor;
+s300: jstrt = xadj[nabor];
+ jstop = xadj[nabor+1] - 1;
+ for (j = jstrt; j <= jstop; j++)
+ { node = adjncy[j];
+ nabor = - node;
+ if (node < 0) goto s300;
+ if (node == 0) goto s600;
+ if (marker[node] == 0)
+ { rchsze++;
+ rchset[rchsze] = node;
+ marker[node] = 1;
+ }
+ }
+ }
+s600: ;
+ }
+ return;
+# undef root
+# undef rchsze
+# undef nhdsze
+}
+
+/***********************************************************************
+* NAME
+*
+* qmdqt - Quotient MD Quotient graph Transformation
+*
+* SYNOPSIS
+*
+* #include "qmd.h"
+* void qmdqt(int *root, int xadj[], int adjncy[], int marker[],
+* int *rchsze, int rchset[], int nbrhd[]);
+*
+* PURPOSE
+*
+* This subroutine performs the quotient graph transformation after a
+* node has been eliminated.
+*
+* INPUT PARAMETERS
+*
+* root - the node just eliminated. It becomes the representative of
+* the new supernode;
+* (xadj, adjncy) -
+* the adjancy structure;
+* (rchsze, rchset) -
+* the reachable set of root in the old quotient graph;
+* nbrhd - the neighborhood set which will be merged with root to form
+* the new supernode;
+* marker - the marker vector.
+*
+* UPDATED PARAMETERS
+*
+* adjncy - becomes the adjncy of the quotient graph.
+***********************************************************************/
+
+void qmdqt(int *_root, int xadj[], int adjncy[], int marker[],
+ int *_rchsze, int rchset[], int nbrhd[])
+{ int inhd, irch, j, jstop, jstrt, link, nabor, node;
+# define root (*_root)
+# define rchsze (*_rchsze)
+ irch = 0;
+ inhd = 0;
+ node = root;
+s100: jstrt = xadj[node];
+ jstop = xadj[node+1] - 2;
+ if (jstop >= jstrt)
+ { /* Place reach nodes into the adjacent list of node. */
+ for (j = jstrt; j <= jstop; j++)
+ { irch++;
+ adjncy[j] = rchset[irch];
+ if (irch >= rchsze) goto s400;
+ }
+ }
+ /* Link to other space provided by the nbrhd set. */
+ link = adjncy[jstop+1];
+ node = - link;
+ if (link >= 0)
+ { inhd++;
+ node = nbrhd[inhd];
+ adjncy[jstop+1] = - node;
+ }
+ goto s100;
+ /* All reachable nodes have been saved. End the adjacent list.
+ * Add root to the neighborhood list of each node in the reach
+ * set. */
+s400: adjncy[j+1] = 0;
+ for (irch = 1; irch <= rchsze; irch++)
+ { node = rchset[irch];
+ if (marker[node] >= 0)
+ { jstrt = xadj[node];
+ jstop = xadj[node+1] - 1;
+ for (j = jstrt; j <= jstop; j++)
+ { nabor = adjncy[j];
+ if (marker[nabor] < 0)
+ { adjncy[j] = root;
+ goto s600;
+ }
+ }
+ }
+s600: ;
+ }
+ return;
+# undef root
+# undef rchsze
+}
+
+/***********************************************************************
+* NAME
+*
+* qmdupd - Quotient MD UPDate
+*
+* SYNOPSIS
+*
+* #include "qmd.h"
+* void qmdupd(int xadj[], int adjncy[], int *nlist, int list[],
+* int deg[], int qsize[], int qlink[], int marker[], int rchset[],
+* int nbrhd[]);
+*
+* PURPOSE
+*
+* This routine performs degree update for a set of nodes in the minimum
+* degree algorithm.
+*
+* INPUT PARAMETERS
+*
+* (xadj, adjncy) -
+* the adjancy structure;
+* (nlist, list) -
+* the list of nodes whose degree has to be updated.
+*
+* UPDATED PARAMETERS
+*
+* deg - the degree vector;
+* qsize - size of indistinguishable supernodes;
+* qlink - linked list for indistinguishable nodes;
+* marker - used to mark those nodes in reach/nbrhd sets.
+*
+* WORKING PARAMETERS
+*
+* rchset - the reachable set;
+* nbrhd - the neighborhood set.
+*
+* PROGRAM SUBROUTINES
+*
+* qmdmrg.
+***********************************************************************/
+
+void qmdupd(int xadj[], int adjncy[], int *_nlist, int list[],
+ int deg[], int qsize[], int qlink[], int marker[], int rchset[],
+ int nbrhd[])
+{ int deg0, deg1, il, inhd, inode, irch, j, jstop, jstrt, mark,
+ nabor, nhdsze, node, rchsze;
+# define nlist (*_nlist)
+ /* Find all eliminated supernodes that are adjacent to some nodes
+ * in the given list. Put them into (nhdsze, nbrhd). deg0 contains
+ * the number of nodes in the list. */
+ if (nlist <= 0) return;
+ deg0 = 0;
+ nhdsze = 0;
+ for (il = 1; il <= nlist; il++)
+ { node = list[il];
+ deg0 += qsize[node];
+ jstrt = xadj[node];
+ jstop = xadj[node+1] - 1;
+ for (j = jstrt; j <= jstop; j++)
+ { nabor = adjncy[j];
+ if (marker[nabor] == 0 && deg[nabor] < 0)
+ { marker[nabor] = -1;
+ nhdsze++;
+ nbrhd[nhdsze] = nabor;
+ }
+ }
+ }
+ /* Merge indistinguishable nodes in the list by calling the
+ * subroutine qmdmrg. */
+ if (nhdsze > 0)
+ qmdmrg(xadj, adjncy, deg, qsize, qlink, marker, &deg0, &nhdsze,
+ nbrhd, rchset, &nbrhd[nhdsze+1]);
+ /* Find the new degrees of the nodes that have not been merged. */
+ for (il = 1; il <= nlist; il++)
+ { node = list[il];
+ mark = marker[node];
+ if (mark == 0 || mark == 1)
+ { marker[node] = 2;
+ qmdrch(&node, xadj, adjncy, deg, marker, &rchsze, rchset,
+ &nhdsze, nbrhd);
+ deg1 = deg0;
+ if (rchsze > 0)
+ { for (irch = 1; irch <= rchsze; irch++)
+ { inode = rchset[irch];
+ deg1 += qsize[inode];
+ marker[inode] = 0;
+ }
+ }
+ deg[node] = deg1 - 1;
+ if (nhdsze > 0)
+ { for (inhd = 1; inhd <= nhdsze; inhd++)
+ { inode = nbrhd[inhd];
+ marker[inode] = 0;
+ }
+ }
+ }
+ }
+ return;
+# undef nlist
+}
+
+/***********************************************************************
+* NAME
+*
+* qmdmrg - Quotient MD MeRGe
+*
+* SYNOPSIS
+*
+* #include "qmd.h"
+* void qmdmrg(int xadj[], int adjncy[], int deg[], int qsize[],
+* int qlink[], int marker[], int *deg0, int *nhdsze, int nbrhd[],
+* int rchset[], int ovrlp[]);
+*
+* PURPOSE
+*
+* This routine merges indistinguishable nodes in the minimum degree
+* ordering algorithm. It also computes the new degrees of these new
+* supernodes.
+*
+* INPUT PARAMETERS
+*
+* (xadj, adjncy) -
+* the adjancy structure;
+* deg0 - the number of nodes in the given set;
+* (nhdsze, nbrhd) -
+* the set of eliminated supernodes adjacent to some nodes in
+* the set.
+*
+* UPDATED PARAMETERS
+*
+* deg - the degree vector;
+* qsize - size of indistinguishable nodes;
+* qlink - linked list for indistinguishable nodes;
+* marker - the given set is given by those nodes with marker value set
+* to 1. Those nodes with degree updated will have marker value
+* set to 2.
+*
+* WORKING PARAMETERS
+*
+* rchset - the reachable set;
+* ovrlp - temp vector to store the intersection of two reachable sets.
+***********************************************************************/
+
+void qmdmrg(int xadj[], int adjncy[], int deg[], int qsize[],
+ int qlink[], int marker[], int *_deg0, int *_nhdsze, int nbrhd[],
+ int rchset[], int ovrlp[])
+{ int deg1, head, inhd, iov, irch, j, jstop, jstrt, link, lnode,
+ mark, mrgsze, nabor, node, novrlp, rchsze, root;
+# define deg0 (*_deg0)
+# define nhdsze (*_nhdsze)
+ /* Initialization. */
+ if (nhdsze <= 0) return;
+ for (inhd = 1; inhd <= nhdsze; inhd++)
+ { root = nbrhd[inhd];
+ marker[root] = 0;
+ }
+ /* Loop through each eliminated supernode in the set
+ * (nhdsze, nbrhd). */
+ for (inhd = 1; inhd <= nhdsze; inhd++)
+ { root = nbrhd[inhd];
+ marker[root] = -1;
+ rchsze = 0;
+ novrlp = 0;
+ deg1 = 0;
+s200: jstrt = xadj[root];
+ jstop = xadj[root+1] - 1;
+ /* Determine the reachable set and its intersection with the
+ * input reachable set. */
+ for (j = jstrt; j <= jstop; j++)
+ { nabor = adjncy[j];
+ root = - nabor;
+ if (nabor < 0) goto s200;
+ if (nabor == 0) break;
+ mark = marker[nabor];
+ if (mark == 0)
+ { rchsze++;
+ rchset[rchsze] = nabor;
+ deg1 += qsize[nabor];
+ marker[nabor] = 1;
+ }
+ else if (mark == 1)
+ { novrlp++;
+ ovrlp[novrlp] = nabor;
+ marker[nabor] = 2;
+ }
+ }
+ /* From the overlapped set, determine the nodes that can be
+ * merged together. */
+ head = 0;
+ mrgsze = 0;
+ for (iov = 1; iov <= novrlp; iov++)
+ { node = ovrlp[iov];
+ jstrt = xadj[node];
+ jstop = xadj[node+1] - 1;
+ for (j = jstrt; j <= jstop; j++)
+ { nabor = adjncy[j];
+ if (marker[nabor] == 0)
+ { marker[node] = 1;
+ goto s1100;
+ }
+ }
+ /* Node belongs to the new merged supernode. Update the
+ * vectors qlink and qsize. */
+ mrgsze += qsize[node];
+ marker[node] = -1;
+ lnode = node;
+s900: link = qlink[lnode];
+ if (link > 0)
+ { lnode = link;
+ goto s900;
+ }
+ qlink[lnode] = head;
+ head = node;
+s1100: ;
+ }
+ if (head > 0)
+ { qsize[head] = mrgsze;
+ deg[head] = deg0 + deg1 - 1;
+ marker[head] = 2;
+ }
+ /* Reset marker values. */
+ root = nbrhd[inhd];
+ marker[root] = 0;
+ if (rchsze > 0)
+ { for (irch = 1; irch <= rchsze; irch++)
+ { node = rchset[irch];
+ marker[node] = 0;
+ }
+ }
+ }
+ return;
+# undef deg0
+# undef nhdsze
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/qmd.h b/test/monniaux/glpk-4.65/src/misc/qmd.h
new file mode 100644
index 00000000..e55d50f5
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/qmd.h
@@ -0,0 +1,58 @@
+/* qmd.h (quotient minimum degree algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2001-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef QMD_H
+#define QMD_H
+
+#define genqmd _glp_genqmd
+void genqmd(int *neqns, int xadj[], int adjncy[], int perm[],
+ int invp[], int deg[], int marker[], int rchset[], int nbrhd[],
+ int qsize[], int qlink[], int *nofsub);
+/* GENeral Quotient Minimum Degree algorithm */
+
+#define qmdrch _glp_qmdrch
+void qmdrch(int *root, int xadj[], int adjncy[], int deg[],
+ int marker[], int *rchsze, int rchset[], int *nhdsze,
+ int nbrhd[]);
+/* Quotient MD ReaCHable set */
+
+#define qmdqt _glp_qmdqt
+void qmdqt(int *root, int xadj[], int adjncy[], int marker[],
+ int *rchsze, int rchset[], int nbrhd[]);
+/* Quotient MD Quotient graph Transformation */
+
+#define qmdupd _glp_qmdupd
+void qmdupd(int xadj[], int adjncy[], int *nlist, int list[],
+ int deg[], int qsize[], int qlink[], int marker[], int rchset[],
+ int nbrhd[]);
+/* Quotient MD UPDate */
+
+#define qmdmrg _glp_qmdmrg
+void qmdmrg(int xadj[], int adjncy[], int deg[], int qsize[],
+ int qlink[], int marker[], int *deg0, int *nhdsze, int nbrhd[],
+ int rchset[], int ovrlp[]);
+/* Quotient MD MeRGe */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/relax4.c b/test/monniaux/glpk-4.65/src/misc/relax4.c
new file mode 100644
index 00000000..f0a47d6d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/relax4.c
@@ -0,0 +1,2850 @@
+/* relax4.c (relaxation method of Bertsekas and Tseng) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* THIS CODE IS THE RESULT OF TRANSLATION OF THE FORTRAN CODE RELAX4.
+*
+* THE TRANSLATION HAS BEEN DONE WITH THE PERMISSION OF THE AUTHOR OF
+* THE ORIGINAL FORTRAN CODE PROF. DIMITRI P. BERTSEKAS, MASSACHUSETTS
+* INSTITUTE OF TECHNOLOGY, CAMBRIDGE, MASSACHUSETTS, USA.
+*
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "relax4.h"
+
+/***********************************************************************
+* WARNING
+*
+* A serious bug was *tentatively* fixed in this code (see #if/#endif
+* marked by 'mao').
+*
+* This bug is inherited from the original Fortran version of the
+* RELAX-IV code. Unfortunately, the code is very intricate, so this
+* bug is still under investigation. Thanks to Sylvain Fournier for bug
+* report.
+*
+* RELAX-IV bug details
+* --------------------
+* In the original RELAX-IV code there are four similar fragments in
+* subroutines ascnt1 and ascnt2 like this:
+*
+* C
+* C DECREASE THE PRICES OF THE SCANNED NODES BY DELPRC.
+* C ADJUST FLOW TO MAINTAIN COMPLEMENTARY SLACKNESS WITH
+* C THE PRICES.
+* C
+* NB = 0
+* DO 6 I=1,NSAVE
+* . . .
+* IF (RC(ARC).EQ.0) THEN
+* DELX=DELX+U(ARC)
+* NB = NB + 1
+* PRDCSR(NB) = ARC
+* END IF
+* . . .
+*
+* On some instances the variable NB becomes greater than N (the number
+* of nodes) that leads to indexing error, because the array PRDCSR is
+* declared as array of N elements (more precisely, as array of MAXNN
+* elements, however, NB becomes even much greater than MAXNN).
+***********************************************************************/
+
+#define false 0
+#define true 1
+
+/***********************************************************************
+* NAME
+*
+* RELAX-IV (version of October 1994)
+*
+* PURPOSE
+*
+* This routine implements the relaxation method of Bertsekas and Tseng
+* (see [1], [2]) for linear cost ordinary network flow problems.
+*
+* [1] Bertsekas, D. P., "A Unified Framework for Primal-Dual Methods"
+* Mathematical Programming, Vol. 32, 1985, pp. 125-145.
+* [2] Bertsekas, D. P., and Tseng, P., "Relaxation Methods for
+* Minimum Cost" Operations Research, Vol. 26, 1988, pp. 93-114.
+*
+* The relaxation method is also described in the books:
+*
+* [3] Bertsekas, D. P., "Linear Network Optimization: Algorithms and
+* Codes" MIT Press, 1991.
+* [4] Bertsekas, D. P. and Tsitsiklis, J. N., "Parallel and Distributed
+* Computation: Numerical Methods", Prentice-Hall, 1989.
+* [5] Bertsekas, D. P., "Network Optimization: Continuous and Discrete
+* Models", Athena Scientific, 1998.
+*
+* RELEASE NOTE
+*
+* This version of relaxation code has option for a special crash
+* procedure for the initial price-flow pair. This is recommended for
+* difficult problems where the default initialization results in long
+* running times. crash = 1 corresponds to an auction/shortest path
+* method
+*
+* These initializations are recommended in the absence of any prior
+* information on a favorable initial flow-price vector pair that
+* satisfies complementary slackness.
+*
+* The relaxation portion of the code differs from the code RELAXT-III
+* and other earlier relaxation codes in that it maintains the set of
+* nodes with nonzero deficit in a fifo queue. Like its predecessor
+* RELAXT-III, this code maintains a linked list of balanced (i.e., of
+* zero reduced cost) arcs so to reduce the work in labeling and
+* scanning. Unlike RELAXT-III, it does not use selectively shortest
+* path iterations for initialization.
+*
+* SOURCE
+*
+* The original Fortran code was written by Dimitri P. Bertsekas and
+* Paul Tseng, with a contribution by Jonathan Eckstein in the phase II
+* initialization. The original Fortran routine AUCTION was written by
+* Dimitri P. Bertsekas and is based on the method described in the
+* paper:
+*
+* [6] Bertsekas, D. P., "An Auction/Sequential Shortest Path Algorithm
+* for the Minimum Cost Flow Problem", LIDS Report P-2146, MIT,
+* Nov. 1992.
+*
+* For inquiries about the original Fortran code, please contact:
+*
+* Dimitri P. Bertsekas
+* Laboratory for information and decision systems
+* Massachusetts Institute of Technology
+* Cambridge, MA 02139
+* (617) 253-7267, dimitrib@mit.edu
+*
+* This code is the result of translation of the original Fortran code.
+* The translation was made by Andrew Makhorin <mao@gnu.org>.
+*
+* USER GUIDELINES
+*
+* This routine is in the public domain to be used only for research
+* purposes. It cannot be used as part of a commercial product, or to
+* satisfy in any part commercial delivery requirements to government
+* or industry, without prior agreement with the authors. Users are
+* requested to acknowledge the authorship of the code, and the
+* relaxation method.
+*
+* No modification should be made to this code other than the minimal
+* necessary to make it compatible with specific platforms.
+*
+* INPUT PARAMETERS (see notes 1, 2, 4)
+*
+* n = number of nodes
+* na = number of arcs
+* large = a very large integer to represent infinity
+* (see note 3)
+* repeat = true if initialization is to be skipped
+* (false otherwise)
+* crash = 0 if default initialization is used
+* 1 if auction initialization is used
+* startn[j] = starting node for arc j, j = 1,...,na
+* endn[j] = ending node for arc j, j = 1,...,na
+* fou[i] = first arc out of node i, i = 1,...,n
+* nxtou[j] = next arc out of the starting node of arc j, j = 1,...,na
+* fin[i] = first arc into node i, i = 1,...,n
+* nxtin[j] = next arc into the ending node of arc j, j = 1,...,na
+*
+* UPDATED PARAMETERS (see notes 1, 3, 4)
+*
+* rc[j] = reduced cost of arc j, j = 1,...,na
+* u[j] = capacity of arc j on input
+* and (capacity of arc j) - x[j] on output, j = 1,...,na
+* dfct[i] = demand at node i on input
+* and zero on output, i = 1,...,n
+*
+* OUTPUT PARAMETERS (see notes 1, 3, 4)
+*
+* x[j] = flow on arc j, j = 1,...,na
+* nmultinode = number of multinode relaxation iterations in RELAX4
+* iter = number of relaxation iterations in RELAX4
+* num_augm = number of flow augmentation steps in RELAX4
+* num_ascnt = number of multinode ascent steps in RELAX4
+* nsp = number of auction/shortest path iterations
+*
+* WORKING PARAMETERS (see notes 1, 4, 5)
+*
+* label[1+n], prdcsr[1+n], save[1+na], tfstou[1+n], tnxtou[1+na],
+* tfstin[1+n], tnxtin[1+na], nxtqueue[1+n], scan[1+n], mark[1+n],
+* extend_arc[1+n], sb_level[1+n], sb_arc[1+n]
+*
+* RETURNS
+*
+* 0 = normal return
+* 1,...,8 = problem is found to be infeasible
+*
+* NOTE 1
+*
+* To run in limited memory systems, declare the arrays startn, endn,
+* nxtin, nxtou, fin, fou, label, prdcsr, save, tfstou, tnxtou, tfstin,
+* tnxtin, ddpos, ddneg, nxtqueue as short instead.
+*
+* NOTE 2
+*
+* This routine makes no effort to initialize with a favorable x from
+* amongst those flow vectors that satisfy complementary slackness with
+* the initial reduced cost vector rc. If a favorable x is known, then
+* it can be passed, together with the corresponding arrays u and dfct,
+* to this routine directly. This, however, requires that the capacity
+* tightening portion and the flow initialization portion of this
+* routine (up to line labeled 90) be skipped.
+*
+* NOTE 3
+*
+* All problem data should be less than large in magnitude, and large
+* should be less than, say, 1/4 the largest int of the machine used.
+* This will guard primarily against overflow in uncapacitated problems
+* where the arc capacities are taken finite but very large. Note,
+* however, that as in all codes operating with integers, overflow may
+* occur if some of the problem data takes very large values.
+*
+* NOTE 4
+*
+* [This note being specific to Fortran was removed.-A.M.]
+*
+* NOTE 5
+*
+* ddpos and ddneg are arrays that give the directional derivatives for
+* all positive and negative single-node price changes. These are used
+* only in phase II of the initialization procedure, before the linked
+* list of balanced arcs comes to play. Therefore, to reduce storage,
+* they are equivalence to tfstou and tfstin, which are of the same size
+* (number of nodes) and are used only after the tree comes into use. */
+
+static void ascnt1(struct relax4_csa *csa, int dm, int *delx,
+ int *nlabel, int *feasbl, int *svitch, int nscan, int curnode,
+ int *prevnode);
+
+static void ascnt2(struct relax4_csa *csa, int dm, int *delx,
+ int *nlabel, int *feasbl, int *svitch, int nscan, int curnode,
+ int *prevnode);
+
+static int auction(struct relax4_csa *csa);
+
+int relax4(struct relax4_csa *csa)
+{ /* input parameters */
+ int n = csa->n;
+ int na = csa->na;
+ int large = csa->large;
+ int repeat = csa->repeat;
+ int crash = csa->crash;
+ int *startn = csa->startn;
+ int *endn = csa->endn;
+ int *fou = csa->fou;
+ int *nxtou = csa->nxtou;
+ int *fin = csa->fin;
+ int *nxtin = csa->nxtin;
+ /* updated parameters */
+ int *rc = csa->rc;
+ int *u = csa->u;
+ int *dfct = csa->dfct;
+ /* output parameters */
+ int *x = csa->x;
+# define nmultinode (csa->nmultinode)
+# define iter (csa->iter)
+# define num_augm (csa->num_augm)
+# define num_ascnt (csa->num_ascnt)
+# define nsp (csa->nsp)
+ /* working parameters */
+ int *label = csa->label;
+ int *prdcsr = csa->prdcsr;
+ int *save = csa->save;
+ int *tfstou = csa->tfstou;
+ int *tnxtou = csa->tnxtou;
+ int *tfstin = csa->tfstin;
+ int *tnxtin = csa->tnxtin;
+ int *nxtqueue = csa->nxtqueue;
+ char *scan = csa->scan;
+ char *mark = csa->mark;
+ int *ddpos = tfstou;
+ int *ddneg = tfstin;
+ /* local variables */
+ int arc, augnod, capin, capout, defcit, delprc, delx, dm, dp,
+ dx, feasbl, i, ib, indef, j, lastqueue, maxcap, narc, nb,
+ nlabel, node, node2, node_def, naugnod, nscan, num_passes,
+ numnz, numnz_new, numpasses, nxtarc, nxtbrk, nxtnode, passes,
+ pchange, posit, prevnode, prvarc, quit, rdcost, scapin,
+ scapou, svitch, t, t1, t2, tmparc, tp, trc, ts;
+ /*--------------------------------------------------------------*/
+ /* Initialization phase I */
+ /* In this phase, we reduce the arc capacities by as much as
+ * possible without changing the problem; then we set the initial
+ * flow array x, together with the corresponding arrays u and
+ * dfct. */
+ /* This phase and phase II (from here up to line labeled 90) can
+ * be skipped (by setting repeat to true) if the calling program
+ * places in common user-chosen values for the arc flows, the
+ * residual arc capacities, and the nodal deficits. When this is
+ * done, it is critical that the flow and the reduced cost for
+ * each arc satisfy complementary slackness and the dfct array
+ * properly correspond to the initial arc/flows. */
+ if (repeat)
+ goto L90;
+ for (node = 1; node <= n; node++)
+ { node_def = dfct[node];
+ ddpos[node] = node_def;
+ ddneg[node] = -node_def;
+ maxcap = 0;
+ scapou = 0;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { if (scapou <= large - u[arc])
+ scapou += u[arc];
+ else
+ goto L10;
+ }
+ if (scapou <= large - node_def)
+ capout = scapou + node_def;
+ else
+ goto L10;
+ if (capout < 0)
+ { /* problem is infeasible */
+ /* exogenous flow into node exceeds out capacity */
+ return 1;
+ }
+ scapin = 0;
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { if (u[arc] > capout)
+ u[arc] = capout;
+ if (maxcap < u[arc])
+ maxcap = u[arc];
+ if (scapin <= large - u[arc])
+ scapin += u[arc];
+ else
+ goto L10;
+ }
+ if (scapin <= large + node_def)
+ capin = scapin - node_def;
+ else
+ goto L10;
+ if (capin < 0)
+ { /* problem is infeasible */
+ /* exogenous flow out of node exceeds in capacity */
+ return 2;
+ }
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { if (u[arc] > capin)
+ u[arc] = capin;
+ }
+L10: ;
+ }
+ /*--------------------------------------------------------------*/
+ /* Initialization phase II */
+ /* In this phase, we initialize the prices and flows by either
+ * calling the routine auction or by performing only single node
+ * (coordinate) relaxation iterations. */
+ if (crash == 1)
+ { nsp = 0;
+ if (auction(csa) != 0)
+ { /* problem is found to be infeasible */
+ return 3;
+ }
+ goto L70;
+ }
+ /* Initialize the arc flows to satisfy complementary slackness
+ * with the prices. u[arc] is the residual capacity of arc, and
+ * x[arc] is the flow. These two always add up to the total
+ * capacity for arc. Also compute the directional derivatives for
+ * each coordinate and compute the actual deficits. */
+ for (arc = 1; arc <= na; arc++)
+ { x[arc] = 0;
+ if (rc[arc] <= 0)
+ { t = u[arc];
+ t1 = startn[arc];
+ t2 = endn[arc];
+ ddpos[t1] += t;
+ ddneg[t2] += t;
+ if (rc[arc] < 0)
+ { x[arc] = t;
+ u[arc] = 0;
+ dfct[t1] += t;
+ dfct[t2] -= t;
+ ddneg[t1] -= t;
+ ddpos[t2] -= t;
+ }
+ }
+ }
+ /* Make 2 or 3 passes through all nodes, performing only single
+ * node relaxation iterations. The number of passes depends on the
+ * density of the network. */
+ if (na > n * 10)
+ numpasses = 2;
+ else
+ numpasses = 3;
+ for (passes = 1; passes <= numpasses; passes++)
+ for (node = 1; node <= n; node++)
+ { if (dfct[node] == 0)
+ continue;
+ if (ddpos[node] <= 0)
+ { /* Compute delprc, the stepsize to the next breakpoint in
+ * the dual cost as the price of node is increased.
+ * [Since the reduced cost of all outgoing (resp., incoming)
+ * arcs will decrease (resp., increase) as the price of node
+ * is increased, the next breakpoint is the minimum of the
+ * positive reduced cost on outgoing arcs and of the
+ * negative reduced cost on incoming arcs.] */
+ delprc = large;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { trc = rc[arc];
+ if ((trc > 0) && (trc < delprc))
+ delprc = trc;
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { trc = rc[arc];
+ if ((trc < 0) && (trc > -delprc))
+ delprc = -trc;
+ }
+ /* If no breakpoint is left and dual ascent is still
+ * possible, the problem is infeasible. */
+ if (delprc >= large)
+ { if (ddpos[node] == 0)
+ continue;
+ return 4;
+ }
+ /* delprc is the stepsize to next breakpoint. Increase
+ * price of node by delprc and compute the stepsize to the
+ * next breakpoint in the dual cost. */
+L53: nxtbrk = large;
+ /* Look at all arcs out of node. */
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { trc = rc[arc];
+ if (trc == 0)
+ { t1 = endn[arc];
+ t = u[arc];
+ if (t > 0)
+ { dfct[node] += t;
+ dfct[t1] -= t;
+ x[arc] = t;
+ u[arc] = 0;
+ }
+ else
+ t = x[arc];
+ ddneg[node] -= t;
+ ddpos[t1] -= t;
+ }
+ /* Decrease the reduced costs on all outgoing arcs. */
+ trc -= delprc;
+ if ((trc > 0) && (trc < nxtbrk))
+ nxtbrk = trc;
+ else if (trc == 0)
+ { /* Arc goes from inactive to balanced. Update the rate
+ * of dual ascent at node and at its neighbor. */
+ ddpos[node] += u[arc];
+ ddneg[endn[arc]] += u[arc];
+ }
+ rc[arc] = trc;
+ }
+ /* Look at all arcs into node. */
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { trc = rc[arc];
+ if (trc == 0)
+ { t1 = startn[arc];
+ t = x[arc];
+ if (t > 0)
+ { dfct[node] += t;
+ dfct[t1] -= t;
+ u[arc] = t;
+ x[arc] = 0;
+ }
+ else
+ t = u[arc];
+ ddpos[t1] -= t;
+ ddneg[node] -= t;
+ }
+ /* Increase the reduced cost on all incoming arcs. */
+ trc += delprc;
+ if ((trc < 0) && (trc > -nxtbrk))
+ nxtbrk = -trc;
+ else if (trc == 0)
+ { /* Arc goes from active to balanced. Update the rate
+ * of dual ascent at node and at its neighbor. */
+ ddneg[startn[arc]] += x[arc];
+ ddpos[node] += x[arc];
+ }
+ rc[arc] = trc;
+ }
+ /* If price of node can be increased further without
+ * decreasing the dual cost (even the dual cost doesn't
+ * increase), return to increase the price further. */
+ if ((ddpos[node] <= 0) && (nxtbrk < large))
+ { delprc = nxtbrk;
+ goto L53;
+ }
+ }
+ else if (ddneg[node] <= 0)
+ { /* Compute delprc, the stepsize to the next breakpoint in
+ * the dual cost as the price of node is decreased.
+ * [Since the reduced cost of all outgoing (resp., incoming)
+ * arcs will increase (resp., decrease) as the price of node
+ * is decreased, the next breakpoint is the minimum of the
+ * negative reduced cost on outgoing arcs and of the
+ * positive reduced cost on incoming arcs.] */
+ delprc = large;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { trc = rc[arc];
+ if ((trc < 0) && (trc > -delprc))
+ delprc = -trc;
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { trc = rc[arc];
+ if ((trc > 0) && (trc < delprc))
+ delprc = trc;
+ }
+ /* If no breakpoint is left and dual ascent is still
+ * possible, the problem is infeasible. */
+ if (delprc == large)
+ { if (ddneg[node] == 0)
+ continue;
+ return 5;
+ }
+ /* delprc is the stepsize to next breakpoint. Decrease
+ * price of node by delprc and compute the stepsize to the
+ * next breakpoint in the dual cost. */
+L63: nxtbrk = large;
+ /* Look at all arcs out of node. */
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { trc = rc[arc];
+ if (trc == 0)
+ { t1 = endn[arc];
+ t = x[arc];
+ if (t > 0)
+ { dfct[node] -= t;
+ dfct[t1] += t;
+ u[arc] = t;
+ x[arc] = 0;
+ }
+ else
+ t = u[arc];
+ ddpos[node] -= t;
+ ddneg[t1] -= t;
+ }
+ /* Increase the reduced cost on all outgoing arcs. */
+ trc += delprc;
+ if ((trc < 0) && (trc > -nxtbrk))
+ nxtbrk = -trc;
+ else if (trc == 0)
+ { /* Arc goes from active to balanced. Update the rate
+ * of dual ascent at node and at its neighbor. */
+ ddneg[node] += x[arc];
+ ddpos[endn[arc]] += x[arc];
+ }
+ rc[arc] = trc;
+ }
+ /* Look at all arcs into node. */
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { trc = rc[arc];
+ if (trc == 0)
+ { t1 = startn[arc];
+ t = u[arc];
+ if (t > 0)
+ { dfct[node] -= t;
+ dfct[t1] += t;
+ x[arc] = t;
+ u[arc] = 0;
+ }
+ else
+ t = x[arc];
+ ddneg[t1] -= t;
+ ddpos[node] -= t;
+ }
+ /* Decrease the reduced cost on all incoming arcs. */
+ trc -= delprc;
+ if ((trc > 0) && (trc < nxtbrk))
+ nxtbrk = trc;
+ else if (trc == 0)
+ { /* Arc goes from inactive to balanced. Update the rate
+ * of dual ascent at node and at its neighbor. */
+ ddpos[startn[arc]] += u[arc];
+ ddneg[node] += u[arc];
+ }
+ rc[arc] = trc;
+ }
+ /* If price of node can be decreased further without
+ * decreasing the dual cost (even the dual cost doesn't
+ * increase), return to decrease the price further. */
+ if ((ddneg[node] <= 0) && (nxtbrk < large))
+ { delprc = nxtbrk;
+ goto L63;
+ }
+ }
+ }
+ /*--------------------------------------------------------------*/
+L70: /* Initialize tree data structure. */
+ for (i = 1; i <= n; i++)
+ tfstou[i] = tfstin[i] = 0;
+ for (i = 1; i <= na; i++)
+ { tnxtin[i] = tnxtou[i] = -1;
+ if (rc[i] == 0)
+ { tnxtou[i] = tfstou[startn[i]];
+ tfstou[startn[i]] = i;
+ tnxtin[i] = tfstin[endn[i]];
+ tfstin[endn[i]] = i;
+ }
+ }
+L90: /* Initialize other variables. */
+ feasbl = true;
+ iter = 0;
+ nmultinode = 0;
+ num_augm = 0;
+ num_ascnt = 0;
+ num_passes = 0;
+ numnz = n;
+ numnz_new = 0;
+ svitch = false;
+ for (i = 1; i <= n; i++)
+ mark[i] = scan[i] = false;
+ nlabel = 0;
+ /* RELAX4 uses an adaptive strategy to decide whether to continue
+ * the scanning process after a multinode price change.
+ * The threshold parameter tp and ts that control this strategy
+ * are set in the next two lines. */
+ tp = 10;
+ ts = n / 15;
+ /* Initialize the queue of nodes with nonzero deficit. */
+ for (node = 1; node <= n - 1; node++)
+ nxtqueue[node] = node + 1;
+ nxtqueue[n] = 1;
+ node = lastqueue = n;
+ /*--------------------------------------------------------------*/
+ /* Start the relaxation algorithm. */
+L100: /* Code for advancing the queue of nonzero deficit nodes. */
+ prevnode = node;
+ node = nxtqueue[node];
+ defcit = dfct[node];
+ if (node == lastqueue)
+ { numnz = numnz_new;
+ numnz_new = 0;
+ lastqueue = prevnode;
+ num_passes++;
+ }
+ /* Code for deleting a node from the queue. */
+ if (defcit == 0)
+ { nxtnode = nxtqueue[node];
+ if (node == nxtnode)
+ return 0;
+ else
+ { nxtqueue[prevnode] = nxtnode;
+ nxtqueue[node] = 0;
+ node = nxtnode;
+ goto L100;
+ }
+ }
+ else
+ posit = (defcit > 0);
+ iter++;
+ numnz_new++;
+ if (posit)
+ { /* Attempt a single node iteration from node with positive
+ * deficit. */
+ pchange = false;
+ indef = defcit;
+ delx = 0;
+ nb = 0;
+ /* Check outgoing (probably) balanced arcs from node. */
+ for (arc = tfstou[node]; arc > 0; arc = tnxtou[arc])
+ { if ((rc[arc] == 0) && (x[arc] > 0))
+ { delx += x[arc];
+ nb++;
+ save[nb] = arc;
+ }
+ }
+ /* Check incoming arcs. */
+ for (arc = tfstin[node]; arc > 0; arc = tnxtin[arc])
+ { if ((rc[arc] == 0) && (u[arc] > 0))
+ { delx += u[arc];
+ nb++;
+ save[nb] = -arc;
+ }
+ }
+ /* End of initial node scan. */
+L4018: /* If no price change is possible, exit. */
+ if (delx > defcit)
+ { quit = (defcit < indef);
+ goto L4016;
+ }
+ /* RELAX4 searches along the ascent direction for the best
+ * price by checking the slope of the dual cost at successive
+ * break points. First, we compute the distance to the next
+ * break point. */
+ delprc = large;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { rdcost = rc[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { rdcost = rc[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ /* Check if problem is infeasible. */
+ if ((delx < defcit) && (delprc == large))
+ { /* The dual cost can be decreased without bound. */
+ return 6;
+ }
+ /* Skip flow adjustment if there is no flow to modify. */
+ if (delx == 0)
+ goto L4014;
+ /* Adjust the flow on the balanced arcs incident to node to
+ * maintain complementary slackness after the price change. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc > 0)
+ { node2 = endn[arc];
+ t1 = x[arc];
+ dfct[node2] += t1;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ u[arc] += t1;
+ x[arc] = 0;
+ }
+ else
+ { narc = -arc;
+ node2 = startn[narc];
+ t1 = u[narc];
+ dfct[node2] += t1;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[narc] += t1;
+ u[narc] = 0;
+ }
+ }
+ defcit -= delx;
+L4014: if (delprc == large)
+ { quit = true;
+ goto L4019;
+ }
+ /* Node corresponds to a dual ascent direction. Decrease the
+ * price of node by delprc and compute the stepsize to the next
+ * breakpoint in the dual cost. */
+ nb = 0;
+ pchange = true;
+ dp = delprc;
+ delprc = large;
+ delx = 0;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { rdcost = rc[arc] + dp;
+ rc[arc] = rdcost;
+ if (rdcost == 0)
+ { nb++;
+ save[nb] = arc;
+ delx += x[arc];
+ }
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { rdcost = rc[arc] - dp;
+ rc[arc] = rdcost;
+ if (rdcost == 0)
+ { nb++;
+ save[nb] = -arc;
+ delx += u[arc];
+ }
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ /* Return to check if another price change is possible. */
+ goto L4018;
+L4016: /* Perform flow augmentation at node. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc > 0)
+ { /* arc is an outgoing arc from node. */
+ node2 = endn[arc];
+ t1 = dfct[node2];
+ if (t1 < 0)
+ { /* Decrease the total deficit by decreasing flow of
+ * arc. */
+ quit = true;
+ t2 = x[arc];
+ dx = defcit;
+ if (dx > -t1) dx = -t1;
+ if (dx > t2) dx = t2;
+ defcit -= dx;
+ dfct[node2] = t1 + dx;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[arc] = t2 - dx;
+ u[arc] += dx;
+ if (defcit == 0)
+ break;
+ }
+ }
+ else
+ { /* -arc is an incoming arc to node. */
+ narc = -arc;
+ node2 = startn[narc];
+ t1 = dfct[node2];
+ if (t1 < 0)
+ { /* Decrease the total deficit by increasing flow of
+ * -arc. */
+ quit = true;
+ t2 = u[narc];
+ dx = defcit;
+ if (dx > -t1) dx = -t1;
+ if (dx > t2) dx = t2;
+ defcit -= dx;
+ dfct[node2] = t1 + dx;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[narc] += dx;
+ u[narc] = t2 - dx;
+ if (defcit == 0)
+ break;
+ }
+ }
+ }
+L4019: dfct[node] = defcit;
+ /* Reconstruct the linked list of balance arcs incident to this
+ * node. For each adjacent node, we add any newly balanced arcs
+ * to the list, but do not bother removing formerly balanced
+ * ones (they will be removed the next time each adjacent node
+ * is scanned). */
+ if (pchange)
+ { arc = tfstou[node];
+ tfstou[node] = 0;
+ while (arc > 0)
+ { nxtarc = tnxtou[arc];
+ tnxtou[arc] = -1;
+ arc = nxtarc;
+ }
+ arc = tfstin[node];
+ tfstin[node] = 0;
+ while (arc > 0)
+ { nxtarc = tnxtin[arc];
+ tnxtin[arc] = -1;
+ arc = nxtarc;
+ }
+ /* Now add the currently balanced arcs to the list for this
+ * node (which is now empty), and the appropriate adjacent
+ * ones. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc < 0)
+ arc = -arc;
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[startn[arc]];
+ tfstou[startn[arc]] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[endn[arc]];
+ tfstin[endn[arc]] = arc;
+ }
+ }
+ }
+ /* End of single node iteration for positive deficit node. */
+ }
+ else
+ { /* Attempt a single node iteration from node with negative
+ * deficit. */
+ pchange = false;
+ defcit = -defcit;
+ indef = defcit;
+ delx = 0;
+ nb = 0;
+ for (arc = tfstin[node]; arc > 0; arc = tnxtin[arc])
+ { if ((rc[arc] == 0) && (x[arc] > 0))
+ { delx += x[arc];
+ nb++;
+ save[nb] = arc;
+ }
+ }
+ for (arc = tfstou[node]; arc > 0; arc = tnxtou[arc])
+ { if ((rc[arc] == 0) && (u[arc] > 0))
+ { delx += u[arc];
+ nb++;
+ save[nb] = -arc;
+ }
+ }
+L4028: if (delx >= defcit)
+ { quit = (defcit < indef);
+ goto L4026;
+ }
+ /* Compute distance to next breakpoint. */
+ delprc = large;
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { rdcost = rc[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { rdcost = rc[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ /* Check if problem is infeasible. */
+ if ((delx < defcit) && (delprc == large))
+ return 7;
+ if (delx == 0)
+ goto L4024;
+ /* Flow augmentation is possible. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc > 0)
+ { node2 = startn[arc];
+ t1 = x[arc];
+ dfct[node2] -= t1;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ u[arc] += t1;
+ x[arc] = 0;
+ }
+ else
+ { narc = -arc;
+ node2 = endn[narc];
+ t1 = u[narc];
+ dfct[node2] -= t1;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[narc] += t1;
+ u[narc] = 0;
+ }
+ }
+ defcit -= delx;
+L4024: if (delprc == large)
+ { quit = true;
+ goto L4029;
+ }
+ /* Price increase at node is possible. */
+ nb = 0;
+ pchange = true;
+ dp = delprc;
+ delprc = large;
+ delx = 0;
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { rdcost = rc[arc] + dp;
+ rc[arc] = rdcost;
+ if (rdcost == 0)
+ { nb++;
+ save[nb] = arc;
+ delx += x[arc];
+ }
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { rdcost = rc[arc] - dp;
+ rc[arc] = rdcost;
+ if (rdcost == 0)
+ { nb++;
+ save[nb] = -arc;
+ delx += u[arc];
+ }
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ goto L4028;
+L4026: /* Perform flow augmentation at node. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc > 0)
+ { /* arc is an incoming arc to node. */
+ node2 = startn[arc];
+ t1 = dfct[node2];
+ if (t1 > 0)
+ { quit = true;
+ t2 = x[arc];
+ dx = defcit;
+ if (dx > t1) dx = t1;
+ if (dx > t2) dx = t2;
+ defcit -= dx;
+ dfct[node2] = t1 - dx;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[arc] = t2 - dx;
+ u[arc] += dx;
+ if (defcit == 0)
+ break;
+ }
+ }
+ else
+ { /* -arc is an outgoing arc from node. */
+ narc = -arc;
+ node2 = endn[narc];
+ t1 = dfct[node2];
+ if (t1 > 0)
+ { quit = true;
+ t2 = u[narc];
+ dx = defcit;
+ if (dx > t1) dx = t1;
+ if (dx > t2) dx = t2;
+ defcit -= dx;
+ dfct[node2] = t1 - dx;
+ if (nxtqueue[node2] == 0)
+ { nxtqueue[prevnode] = node2;
+ nxtqueue[node2] = node;
+ prevnode = node2;
+ }
+ x[narc] += dx;
+ u[narc] = t2 - dx;
+ if (defcit == 0)
+ break;
+ }
+ }
+ }
+L4029: dfct[node] = -defcit;
+ /* Reconstruct the list of balanced arcs incident to node. */
+ if (pchange)
+ { arc = tfstou[node];
+ tfstou[node] = 0;
+ while (arc > 0)
+ { nxtarc = tnxtou[arc];
+ tnxtou[arc] = -1;
+ arc = nxtarc;
+ }
+ arc = tfstin[node];
+ tfstin[node] = 0;
+ while (arc > 0)
+ { nxtarc = tnxtin[arc];
+ tnxtin[arc] = -1;
+ arc = nxtarc;
+ }
+ /* Now add the currently balanced arcs to the list for this
+ * node (which is now empty), and the appropriate adjacent
+ * ones. */
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc <= 0)
+ arc = -arc;
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[startn[arc]];
+ tfstou[startn[arc]] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[endn[arc]];
+ tfstin[endn[arc]] = arc;
+ }
+ }
+ }
+ /* End of single node iteration for a negative deficit node. */
+ }
+ if (quit || (num_passes <= 3))
+ goto L100;
+ /* Do a multinode iteration from node. */
+ nmultinode++;
+ /* If number of nonzero deficit nodes is small, continue labeling
+ * until a flow augmentation is done. */
+ svitch = (numnz < tp);
+ /* Unmark nodes labeled earlier. */
+ for (j = 1; j <= nlabel; j++)
+ { node2 = label[j];
+ mark[node2] = scan[node2] = false;
+ }
+ /* Initialize labeling. */
+ nlabel = 1;
+ label[1] = node;
+ mark[node] = true;
+ prdcsr[node] = 0;
+ /* Scan starting node. */
+ scan[node] = true;
+ nscan = 1;
+ dm = dfct[node];
+ delx = 0;
+ for (j = 1; j <= nb; j++)
+ { arc = save[j];
+ if (arc > 0)
+ { if (posit)
+ node2 = endn[arc];
+ else
+ node2 = startn[arc];
+ if (!mark[node2])
+ { nlabel++;
+ label[nlabel] = node2;
+ prdcsr[node2] = arc;
+ mark[node2] = true;
+ delx += x[arc];
+ }
+ }
+ else
+ { narc = -arc;
+ if (posit)
+ node2 = startn[narc];
+ else
+ node2 = endn[narc];
+ if (!mark[node2])
+ { nlabel++;
+ label[nlabel] = node2;
+ prdcsr[node2] = arc;
+ mark[node2] = true;
+ delx += u[narc];
+ }
+ }
+ }
+L4120:/* Start scanning a labeled but unscanned node. */
+ nscan++;
+ /* Check to see if switch needs to be set to true so to continue
+ * scanning even after a price change. */
+ svitch = svitch || ((nscan > ts) && (numnz < ts));
+ /* Scanning will continue until either an overestimate of the
+ * residual capacity across the cut corresponding to the scanned
+ * set of nodes (called delx) exceeds the absolute value of the
+ * total deficit of the scanned nodes (called dm), or else an
+ * augmenting path is found. Arcs that are in the tree but are not
+ * balanced are removed as part of the scanning process. */
+ i = label[nscan];
+ scan[i] = true;
+ naugnod = 0;
+ if (posit)
+ { /* Scanning node i in case of positive deficit. */
+ prvarc = 0;
+ arc = tfstou[i];
+ while (arc > 0)
+ { /* arc is an outgoing arc from node. */
+ if (rc[arc] == 0)
+ { if (x[arc] > 0)
+ { node2 = endn[arc];
+ if (!mark[node2])
+ { /* node2 is not labeled, so add node2 to the
+ labeled set. */
+ prdcsr[node2] = arc;
+ if (dfct[node2] < 0)
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ delx += x[arc];
+ }
+ }
+ prvarc = arc;
+ arc = tnxtou[arc];
+ }
+ else
+ { tmparc = arc;
+ arc = tnxtou[arc];
+ tnxtou[tmparc] = -1;
+ if (prvarc == 0)
+ tfstou[i] = arc;
+ else
+ tnxtou[prvarc] = arc;
+ }
+ }
+ prvarc = 0;
+ arc = tfstin[i];
+ while (arc > 0)
+ { /* arc is an incoming arc into node. */
+ if (rc[arc] == 0)
+ { if (u[arc] > 0)
+ { node2 = startn[arc];
+ if (!mark[node2])
+ { /* node2 is not labeled, so add node2 to the
+ * labeled set. */
+ prdcsr[node2] = -arc;
+ if (dfct[node2] < 0)
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ delx += u[arc];
+ }
+ }
+ prvarc = arc;
+ arc = tnxtin[arc];
+ }
+ else
+ { tmparc = arc;
+ arc = tnxtin[arc];
+ tnxtin[tmparc] = -1;
+ if (prvarc == 0)
+ tfstin[i] = arc;
+ else
+ tnxtin[prvarc] = arc;
+ }
+ }
+ /* Correct the residual capacity of the scanned node cut. */
+ arc = prdcsr[i];
+ if (arc > 0)
+ delx -= x[arc];
+ else
+ delx -= u[-arc];
+ /* End of scanning of node i for positive deficit case. */
+ }
+ else
+ { /* Scanning node i for negative deficit case. */
+ prvarc = 0;
+ arc = tfstin[i];
+ while (arc > 0)
+ { if (rc[arc] == 0)
+ { if (x[arc] > 0)
+ { node2 = startn[arc];
+ if (!mark[node2])
+ { prdcsr[node2] = arc;
+ if (dfct[node2] > 0)
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ delx += x[arc];
+ }
+ }
+ prvarc = arc;
+ arc = tnxtin[arc];
+ }
+ else
+ { tmparc = arc;
+ arc = tnxtin[arc];
+ tnxtin[tmparc] = -1;
+ if (prvarc == 0)
+ tfstin[i] = arc;
+ else
+ tnxtin[prvarc] = arc;
+ }
+ }
+ prvarc = 0;
+ arc = tfstou[i];
+ while (arc > 0)
+ { if (rc[arc] == 0)
+ { if (u[arc] > 0)
+ { node2 = endn[arc];
+ if (!mark[node2])
+ { prdcsr[node2] = -arc;
+ if (dfct[node2] > 0)
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ delx += u[arc];
+ }
+ }
+ prvarc = arc;
+ arc = tnxtou[arc];
+ }
+ else
+ { tmparc = arc;
+ arc = tnxtou[arc];
+ tnxtou[tmparc] = -1;
+ if (prvarc == 0)
+ tfstou[i] = arc;
+ else
+ tnxtou[prvarc] = arc;
+ }
+ }
+ arc = prdcsr[i];
+ if (arc > 0)
+ delx -= x[arc];
+ else
+ delx -= u[-arc];
+ }
+ /* Add deficit of node scanned to dm. */
+ dm += dfct[i];
+ /* Check if the set of scanned nodes correspond to a dual ascent
+ * direction; if yes, perform a price adjustment step, otherwise
+ * continue labeling. */
+ if (nscan < nlabel)
+ { if (svitch)
+ goto L4210;
+ if ((delx >= dm) && (delx >= -dm))
+ goto L4210;
+ }
+ /* Try a price change.
+ * [Note that since delx - abs(dm) is an overestimate of ascent
+ * slope, we may occasionally try a direction that is not an
+ * ascent direction. In this case the ascnt routines return with
+ * quit = false, so we continue labeling nodes.] */
+ if (posit)
+ { ascnt1(csa, dm, &delx, &nlabel, &feasbl, &svitch, nscan, node,
+ &prevnode);
+ num_ascnt++;
+ }
+ else
+ { ascnt2(csa, dm, &delx, &nlabel, &feasbl, &svitch, nscan, node,
+ &prevnode);
+ num_ascnt++;
+ }
+ if (!feasbl)
+ return 8;
+ if (!svitch)
+ goto L100;
+ /* Store those newly labeled nodes to which flow augmentation is
+ * possible. */
+ naugnod = 0;
+ for (j = nscan + 1; j <= nlabel; j++)
+ { node2 = label[j];
+ if (posit && (dfct[node2] < 0))
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ else if ((!posit) && (dfct[node2] > 0))
+ { naugnod++;
+ save[naugnod] = node2;
+ }
+ }
+L4210:/* Check if flow augmentation is possible. If not, return to scan
+ * another node. */
+ if (naugnod == 0)
+ goto L4120;
+ for (j = 1; j <= naugnod; j++)
+ { num_augm++;
+ augnod = save[j];
+ if (posit)
+ { /* Do the augmentation from node with positive deficit. */
+ dx = -dfct[augnod];
+ ib = augnod;
+ while (ib != node)
+ { arc = prdcsr[ib];
+ if (arc > 0)
+ { if (dx > x[arc]) dx = x[arc];
+ ib = startn[arc];
+ }
+ else
+ { if (dx > u[-arc]) dx = u[-arc];
+ ib = endn[-arc];
+ }
+ }
+ if (dx > dfct[node]) dx = dfct[node];
+ if (dx > 0)
+ { /* Increase (decrease) the flow of all forward (backward)
+ * arcs in the flow augmenting path. Adjust node deficit
+ * accordingly. */
+ if (nxtqueue[augnod] == 0)
+ { nxtqueue[prevnode] = augnod;
+ nxtqueue[augnod] = node;
+ prevnode = augnod;
+ }
+ dfct[augnod] += dx;
+ dfct[node] -= dx;
+ ib = augnod;
+ while (ib != node)
+ { arc = prdcsr[ib];
+ if (arc > 0)
+ { x[arc] -= dx;
+ u[arc] += dx;
+ ib = startn[arc];
+ }
+ else
+ { narc = -arc;
+ x[narc] += dx;
+ u[narc] -= dx;
+ ib = endn[narc];
+ }
+ }
+ }
+ }
+ else
+ { /* Do the augmentation from node with negative deficit. */
+ dx = dfct[augnod];
+ ib = augnod;
+ while (ib != node)
+ { arc = prdcsr[ib];
+ if (arc > 0)
+ { if (dx > x[arc]) dx = x[arc];
+ ib = endn[arc];
+ }
+ else
+ { if (dx > u[-arc]) dx = u[-arc];
+ ib = startn[-arc];
+ }
+ }
+ if (dx > -dfct[node]) dx = -dfct[node];
+ if (dx > 0)
+ { /* Update the flow and deficits. */
+ if (nxtqueue[augnod] == 0)
+ { nxtqueue[prevnode] = augnod;
+ nxtqueue[augnod] = node;
+ prevnode = augnod;
+ }
+ dfct[augnod] -= dx;
+ dfct[node] += dx;
+ ib = augnod;
+ while (ib != node)
+ { arc = prdcsr[ib];
+ if (arc > 0)
+ { x[arc] -= dx;
+ u[arc] += dx;
+ ib = endn[arc];
+ }
+ else
+ { narc = -arc;
+ x[narc] += dx;
+ u[narc] -= dx;
+ ib = startn[narc];
+ }
+ }
+ }
+ }
+ if (dfct[node] == 0)
+ goto L100;
+ if (dfct[augnod] != 0)
+ svitch = false;
+ }
+ /* If node still has nonzero deficit and all newly labeled nodes
+ * have same sign for their deficit as node, we can continue
+ * labeling. In this case, continue labeling only when flow
+ * augmentation is done relatively infrequently. */
+ if (svitch && (iter > 8 * num_augm))
+ goto L4120;
+ /* Return to do another relaxation iteration. */
+ goto L100;
+# undef nmultinode
+# undef iter
+# undef num_augm
+# undef num_ascnt
+# undef nsp
+}
+
+/***********************************************************************
+* NAME
+*
+* relax4_inidat - construct linked lists for network topology
+*
+* PURPOSE
+*
+* This routine constructs two linked lists for the network topology:
+* one list (given by fou, nxtou) for the outgoing arcs of nodes and
+* one list (given by fin, nxtin) for the incoming arcs of nodes. These
+* two lists are required by RELAX4.
+*
+* INPUT PARAMETERS
+*
+* n = number of nodes
+* na = number of arcs
+* startn[j] = starting node for arc j, j = 1,...,na
+* endn[j] = ending node for arc j, j = 1,...,na
+*
+* OUTPUT PARAMETERS
+*
+* fou[i] = first arc out of node i, i = 1,...,n
+* nxtou[j] = next arc out of the starting node of arc j, j = 1,...,na
+* fin[i] = first arc into node i, i = 1,...,n
+* nxtin[j] = next arc into the ending node of arc j, j = 1,...,na
+*
+* WORKING PARAMETERS
+*
+* tempin[1+n], tempou[1+n] */
+
+void relax4_inidat(struct relax4_csa *csa)
+{ /* input parameters */
+ int n = csa->n;
+ int na = csa->na;
+ int *startn = csa->startn;
+ int *endn = csa->endn;
+ /* output parameters */
+ int *fou = csa->fou;
+ int *nxtou = csa->nxtou;
+ int *fin = csa->fin;
+ int *nxtin = csa->nxtin;
+ /* working parameters */
+ int *tempin = csa->label;
+ int *tempou = csa->prdcsr;
+ /* local variables */
+ int i, i1, i2;
+ for (i = 1; i <= n; i++)
+ { fin[i] = fou[i] = 0;
+ tempin[i] = tempou[i] = 0;
+ }
+ for (i = 1; i <= na; i++)
+ { nxtin[i] = nxtou[i] = 0;
+ i1 = startn[i];
+ i2 = endn[i];
+ if (fou[i1] != 0)
+ nxtou[tempou[i1]] = i;
+ else
+ fou[i1] = i;
+ tempou[i1] = i;
+ if (fin[i2] != 0)
+ nxtin[tempin[i2]] = i;
+ else
+ fin[i2] = i;
+ tempin[i2] = i;
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* ascnt1 - multi-node price adjustment for positive deficit case
+*
+* PURPOSE
+*
+* This subroutine performs the multi-node price adjustment step for
+* the case where the scanned nodes have positive deficit. It first
+* checks if decreasing the price of the scanned nodes increases the
+* dual cost. If yes, then it decreases the price of all scanned nodes.
+* There are two possibilities for price decrease: if switch = true,
+* then the set of scanned nodes corresponds to an elementary direction
+* of maximal rate of ascent, in which case the price of all scanned
+* nodes are decreased until the next breakpoint in the dual cost is
+* encountered. At this point, some arc becomes balanced and more
+* node(s) are added to the labeled set and the subroutine is exited.
+* If switch = false, then the price of all scanned nodes are decreased
+* until the rate of ascent becomes negative (this corresponds to the
+* price adjustment step in which both the line search and the
+* degenerate ascent iteration are implemented).
+*
+* INPUT PARAMETERS
+*
+* dm = total deficit of scanned nodes
+* switch = true if labeling is to continue after price change
+* nscan = number of scanned nodes
+* curnode = most recently scanned node
+* n = number of nodes
+* na = number of arcs
+* large = a very large integer to represent infinity (see note 3)
+* startn[i] = starting node for the i-th arc, i = 1,...,na
+* endn[i] = ending node for the i-th arc, i = 1,...,na
+* fou[i] = first arc leaving i-th node, i = 1,...,n
+* nxtou[i] = next arc leaving the starting node of j-th arc,
+* i = 1,...,na
+* fin[i] = first arc entering i-th node, i = 1,...,n
+* nxtin[i] = next arc entering the ending node of j-th arc,
+* i = 1,...,na
+*
+* UPDATED PARAMETERS
+*
+* delx = a lower estimate of the total flow on balanced arcs in
+* the scanned-nodes cut
+* nlabel = number of labeled nodes
+* feasbl = false if problem is found to be infeasible
+* prevnode = the node before curnode in queue
+* rc[j] = reduced cost of arc j, j = 1,...,na
+* u[j] = residual capacity of arc j, j = 1,...,na
+* x[j] = flow on arc j, j = 1,...,na
+* dfct[i] = deficit at node i, i = 1,...,n
+* label[k] = k-th node labeled, k = 1,...,nlabel
+* prdcsr[i] = predecessor of node i in tree of labeled nodes (0 if i
+* is unlabeled), i = 1,...,n
+* tfstou[i] = first balanced arc out of node i, i = 1,...,n
+* tnxtou[j] = next balanced arc out of the starting node of arc j,
+* j = 1,...,na
+* tfstin[i] = first balanced arc into node i, i = 1,...,n
+* tnxtin[j] = next balanced arc into the ending node of arc j,
+* j = 1,...,na
+* nxtqueue[i] = node following node i in the fifo queue (0 if node is
+* not in the queue), i = 1,...,n
+* scan[i] = true if node i is scanned, i = 1,...,n
+* mark[i] = true if node i is labeled, i = 1,...,n
+*
+* WORKING PARAMETERS
+*
+* save[1+na] */
+
+static void ascnt1(struct relax4_csa *csa, int dm, int *delx,
+ int *nlabel, int *feasbl, int *svitch, int nscan, int curnode,
+ int *prevnode)
+{ /* input parameters */
+ int n = csa->n;
+ /* int na = csa->na; */
+ int large = csa->large;
+ int *startn = csa->startn;
+ int *endn = csa->endn;
+ int *fou = csa->fou;
+ int *nxtou = csa->nxtou;
+ int *fin = csa->fin;
+ int *nxtin = csa->nxtin;
+ /* updated parameters */
+# define delx (*delx)
+# define nlabel (*nlabel)
+# define feasbl (*feasbl)
+# define svitch (*svitch)
+# define prevnode (*prevnode)
+ int *rc = csa->rc;
+ int *u = csa->u;
+ int *x = csa->x;
+ int *dfct = csa->dfct;
+ int *label = csa->label;
+ int *prdcsr = csa->prdcsr;
+ int *tfstou = csa->tfstou;
+ int *tnxtou = csa->tnxtou;
+ int *tfstin = csa->tfstin;
+ int *tnxtin = csa->tnxtin;
+ int *nxtqueue = csa->nxtqueue;
+ char *scan = csa->scan;
+ char *mark = csa->mark;
+ int *save = csa->save;
+ /* local variables */
+ int arc, delprc, dlx, i, j, nb, node, node2, nsave, rdcost, t1,
+ t2, t3;
+ /* Store the arcs between the set of scanned nodes and its
+ * complement in save and compute delprc, the stepsize to the next
+ * breakpoint in the dual cost in the direction of decreasing
+ * prices of the scanned nodes.
+ * [The arcs are stored into save by looking at the arcs incident
+ * to either the set of scanned nodes or its complement, depending
+ * on whether nscan > n/2 or not. This improves the efficiency of
+ * storing.] */
+ delprc = large;
+ dlx = 0;
+ nsave = 0;
+ if (nscan <= n / 2)
+ { for (i = 1; i <= nscan; i++)
+ { node = label[i];
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { /* arc points from scanned node to an unscanned node. */
+ node2 = endn[arc];
+ if (!scan[node2])
+ { nsave++;
+ save[nsave] = arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node2] != arc))
+ dlx += x[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { /* arc points from unscanned node to scanned node. */
+ node2 = startn[arc];
+ if (!scan[node2])
+ { nsave++;
+ save[nsave] = -arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node2] != -arc))
+ dlx += u[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ }
+ }
+ else
+ { for (node = 1; node <= n; node++)
+ { if (scan[node])
+ continue;
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { node2 = startn[arc];
+ if (scan[node2])
+ { nsave++;
+ save[nsave] = arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node] != arc))
+ dlx += x[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ }
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { node2 = endn[arc];
+ if (scan[node2])
+ { nsave++;
+ save[nsave] = -arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node] != -arc))
+ dlx += u[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ }
+ }
+ /* Check if the set of scanned nodes truly corresponds to a dual
+ * ascent direction. [Here delx + dlx is the exact sum of the flow
+ * on arcs from the scanned set to the unscanned set plus the
+ * (capacity - flow) on arcs from the unscanned set to the scanned
+ * set.] If this were not the case, set switch to true and exit
+ * subroutine. */
+ if ((!svitch) && (delx + dlx >= dm))
+ { svitch = true;
+ return;
+ }
+ delx += dlx;
+L4: /* Check that the problem is feasible. */
+ if (delprc == large)
+ { /* We can increase the dual cost without bound, so the primal
+ * problem is infeasible. */
+ feasbl = false;
+ return;
+ }
+ /* Decrease the prices of the scanned nodes, add more nodes to
+ * the labeled set and check if a newly labeled node has negative
+ * deficit. */
+ if (svitch)
+ { for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { rc[arc] += delprc;
+ if (rc[arc] == 0)
+ { node2 = endn[arc];
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[startn[arc]];
+ tfstou[startn[arc]] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[node2];
+ tfstin[node2] = arc;
+ }
+ if (!mark[node2])
+ { prdcsr[node2] = arc;
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ }
+ }
+ }
+ else
+ { arc = -arc;
+ rc[arc] -= delprc;
+ if (rc[arc] == 0)
+ { node2 = startn[arc];
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[node2];
+ tfstou[node2] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[endn[arc]];
+ tfstin[endn[arc]] = arc;
+ }
+ if (!mark[node2])
+ { prdcsr[node2] = -arc;
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ }
+ }
+ }
+ }
+ return;
+ }
+ else
+ { /* Decrease the prices of the scanned nodes by delprc. Adjust
+ * flow to maintain complementary slackness with the prices. */
+ nb = 0;
+ for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { t1 = rc[arc];
+ if (t1 == 0)
+ { t2 = x[arc];
+ t3 = startn[arc];
+ dfct[t3] -= t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ t3 = endn[arc];
+ dfct[t3] += t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ u[arc] += t2;
+ x[arc] = 0;
+ }
+ rc[arc] = t1 + delprc;
+#if 0 /* by mao; 26/IV-2013 */
+ if (rc[arc] == 0)
+#else
+ if (rc[arc] == 0 && nb < n)
+#endif
+ { delx += x[arc];
+ nb++;
+ prdcsr[nb] = arc;
+ }
+ }
+ else
+ { arc = -arc;
+ t1 = rc[arc];
+ if (t1 == 0)
+ { t2 = u[arc];
+ t3 = startn[arc];
+ dfct[t3] += t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ t3 = endn[arc];
+ dfct[t3] -= t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ x[arc] += t2;
+ u[arc] = 0;
+ }
+ rc[arc] = t1 - delprc;
+#if 0 /* by mao; 26/IV-2013 */
+ if (rc[arc] == 0)
+#else
+ if (rc[arc] == 0 && nb < n)
+#endif
+ { delx += u[arc];
+ nb++;
+ prdcsr[nb] = arc;
+ }
+ }
+ }
+ }
+ if (delx <= dm)
+ { /* The set of scanned nodes still corresponds to a dual
+ * (possibly degenerate) ascent direction. Compute the stepsize
+ * delprc to the next breakpoint in the dual cost. */
+ delprc = large;
+ for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { rdcost = rc[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ else
+ { arc = -arc;
+ rdcost = rc[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ if ((delprc != large) || (delx < dm))
+ goto L4;
+ }
+ /* Add new balanced arcs to the superset of balanced arcs. */
+ for (i = 1; i <= nb; i++)
+ { arc = prdcsr[i];
+ if (tnxtin[arc] == -1)
+ { j = endn[arc];
+ tnxtin[arc] = tfstin[j];
+ tfstin[j] = arc;
+ }
+ if (tnxtou[arc] == -1)
+ { j = startn[arc];
+ tnxtou[arc] = tfstou[j];
+ tfstou[j] = arc;
+ }
+ }
+ return;
+# undef delx
+# undef nlabel
+# undef feasbl
+# undef svitch
+# undef prevnode
+}
+
+/***********************************************************************
+* NAME
+*
+* ascnt2 - multi-node price adjustment for negative deficit case
+*
+* PURPOSE
+*
+* This routine is analogous to ascnt1 but for the case where the
+* scanned nodes have negative deficit. */
+
+static void ascnt2(struct relax4_csa *csa, int dm, int *delx,
+ int *nlabel, int *feasbl, int *svitch, int nscan, int curnode,
+ int *prevnode)
+{ /* input parameters */
+ int n = csa->n;
+ /* int na = csa->na; */
+ int large = csa->large;
+ int *startn = csa->startn;
+ int *endn = csa->endn;
+ int *fou = csa->fou;
+ int *nxtou = csa->nxtou;
+ int *fin = csa->fin;
+ int *nxtin = csa->nxtin;
+ /* updated parameters */
+# define delx (*delx)
+# define nlabel (*nlabel)
+# define feasbl (*feasbl)
+# define svitch (*svitch)
+# define prevnode (*prevnode)
+ int *rc = csa->rc;
+ int *u = csa->u;
+ int *x = csa->x;
+ int *dfct = csa->dfct;
+ int *label = csa->label;
+ int *prdcsr = csa->prdcsr;
+ int *tfstou = csa->tfstou;
+ int *tnxtou = csa->tnxtou;
+ int *tfstin = csa->tfstin;
+ int *tnxtin = csa->tnxtin;
+ int *nxtqueue = csa->nxtqueue;
+ char *scan = csa->scan;
+ char *mark = csa->mark;
+ int *save = csa->save;
+ /* local variables */
+ int arc, delprc, dlx, i, j, nb, node, node2, nsave, rdcost, t1,
+ t2, t3;
+ /* Store the arcs between the set of scanned nodes and its
+ * complement in save and compute delprc, the stepsize to the next
+ * breakpoint in the dual cost in the direction of increasing
+ * prices of the scanned nodes. */
+ delprc = large;
+ dlx = 0;
+ nsave = 0;
+ if (nscan <= n / 2)
+ { for (i = 1; i <= nscan; i++)
+ { node = label[i];
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { node2 = startn[arc];
+ if (!scan[node2])
+ { nsave++;
+ save[nsave] = arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node2] != arc))
+ dlx += x[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ }
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { node2 = endn[arc];
+ if (!scan[node2])
+ { nsave++;
+ save[nsave] = -arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node2] != -arc))
+ dlx += u[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ }
+ }
+ else
+ { for (node = 1; node <= n; node++)
+ { if (scan[node])
+ continue;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { node2 = endn[arc];
+ if (scan[node2])
+ { nsave++;
+ save[nsave] = arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node] != arc))
+ dlx += x[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ }
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { node2 = startn[arc];
+ if (scan[node2])
+ { nsave++;
+ save[nsave] = -arc;
+ rdcost = rc[arc];
+ if ((rdcost == 0) && (prdcsr[node] != -arc))
+ dlx += u[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ }
+ }
+ if ((!svitch) && (delx + dlx >= -dm))
+ { svitch = true;
+ return;
+ }
+ delx += dlx;
+ /* Check that the problem is feasible. */
+L4: if (delprc == large)
+ { feasbl = false;
+ return;
+ }
+ /* Increase the prices of the scanned nodes, add more nodes to
+ * the labeled set and check if a newly labeled node has positive
+ * deficit. */
+ if (svitch)
+ { for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { rc[arc] += delprc;
+ if (rc[arc] == 0)
+ { node2 = startn[arc];
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[node2];
+ tfstou[node2] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[endn[arc]];
+ tfstin[endn[arc]] = arc;
+ }
+ if (!mark[node2])
+ { prdcsr[node2] = arc;
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ }
+ }
+ }
+ else
+ { arc = -arc;
+ rc[arc] -= delprc;
+ if (rc[arc] == 0)
+ { node2 = endn[arc];
+ if (tnxtou[arc] < 0)
+ { tnxtou[arc] = tfstou[startn[arc]];
+ tfstou[startn[arc]] = arc;
+ }
+ if (tnxtin[arc] < 0)
+ { tnxtin[arc] = tfstin[node2];
+ tfstin[node2] = arc;
+ }
+ if (!mark[node2])
+ { prdcsr[node2] = -arc;
+ nlabel++;
+ label[nlabel] = node2;
+ mark[node2] = true;
+ }
+ }
+ }
+ }
+ return;
+ }
+ else
+ { nb = 0;
+ for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { t1 = rc[arc];
+ if (t1 == 0)
+ { t2 = x[arc];
+ t3 = startn[arc];
+ dfct[t3] -= t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ t3 = endn[arc];
+ dfct[t3] += t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ u[arc] += t2;
+ x[arc] = 0;
+ }
+ rc[arc] = t1 + delprc;
+#if 0 /* by mao; 26/IV-2013 */
+ if (rc[arc] == 0)
+#else
+ if (rc[arc] == 0 && nb < n)
+#endif
+ { delx += x[arc];
+ nb++;
+ prdcsr[nb] = arc;
+ }
+ }
+ else
+ { arc = -arc;
+ t1 = rc[arc];
+ if (t1 == 0)
+ { t2 = u[arc];
+ t3 = startn[arc];
+ dfct[t3] += t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ t3 = endn[arc];
+ dfct[t3] -= t2;
+ if (nxtqueue[t3] == 0)
+ { nxtqueue[prevnode] = t3;
+ nxtqueue[t3] = curnode;
+ prevnode = t3;
+ }
+ x[arc] += t2;
+ u[arc] = 0;
+ }
+ rc[arc] = t1 - delprc;
+#if 0 /* by mao; 26/IV-2013 */
+ if (rc[arc] == 0)
+#else
+ if (rc[arc] == 0 && nb < n)
+#endif
+ { delx += u[arc];
+ nb++;
+ prdcsr[nb] = arc;
+ }
+ }
+ }
+ }
+ if (delx <= -dm)
+ { delprc = large;
+ for (i = 1; i <= nsave; i++)
+ { arc = save[i];
+ if (arc > 0)
+ { rdcost = rc[arc];
+ if ((rdcost < 0) && (rdcost > -delprc))
+ delprc = -rdcost;
+ }
+ else
+ { arc = -arc;
+ rdcost = rc[arc];
+ if ((rdcost > 0) && (rdcost < delprc))
+ delprc = rdcost;
+ }
+ }
+ if ((delprc != large) || (delx < -dm))
+ goto L4;
+ }
+ /* Add new balanced arcs to the superset of balanced arcs. */
+ for (i = 1; i <= nb; i++)
+ { arc = prdcsr[i];
+ if (tnxtin[arc] == -1)
+ { j = endn[arc];
+ tnxtin[arc] = tfstin[j];
+ tfstin[j] = arc;
+ }
+ if (tnxtou[arc] == -1)
+ { j = startn[arc];
+ tnxtou[arc] = tfstou[j];
+ tfstou[j] = arc;
+ }
+ }
+ return;
+# undef delx
+# undef nlabel
+# undef feasbl
+# undef svitch
+# undef prevnode
+}
+
+/***********************************************************************
+* NAME
+*
+* auction - compute good initial flow and prices
+*
+* PURPOSE
+*
+* This subroutine uses a version of the auction algorithm for min
+* cost network flow to compute a good initial flow and prices for the
+* problem.
+*
+* INPUT PARAMETERS
+*
+* n = number of nodes
+* na = number of arcs
+* large = a very large integer to represent infinity (see note 3)
+* startn[i] = starting node for the i-th arc, i = 1,...,na
+* endn[i] = ending node for the i-th arc, i = 1,...,na
+* fou[i] = first arc leaving i-th node, i = 1,...,n
+* nxtou[i] = next arc leaving the starting node of j-th arc,
+* i = 1,...,na
+* fin[i] = first arc entering i-th node, i = 1,...,n
+* nxtin[i] = next arc entering the ending node of j-th arc,
+* i = 1,...,na
+*
+* UPDATED PARAMETERS
+*
+* rc[j] = reduced cost of arc j, j = 1,...,na
+* u[j] = residual capacity of arc j, j = 1,...,na
+* x[j] = flow on arc j, j = 1,...,na
+* dfct[i] = deficit at node i, i = 1,...,n
+*
+* OUTPUT PARAMETERS
+*
+* nsp = number of auction/shortest path iterations
+*
+* WORKING PARAMETERS
+*
+* p[1+n], prdcsr[1+n], save[1+na], fpushf[1+n], nxtpushf[1+na],
+* fpushb[1+n], nxtpushb[1+na], nxtqueue[1+n], extend_arc[1+n],
+* sb_level[1+n], sb_arc[1+n], path_id[1+n]
+*
+* RETURNS
+*
+* 0 = normal return
+* 1 = problem is found to be infeasible */
+
+static int auction(struct relax4_csa *csa)
+{ /* input parameters */
+ int n = csa->n;
+ int na = csa->na;
+ int large = csa->large;
+ int *startn = csa->startn;
+ int *endn = csa->endn;
+ int *fou = csa->fou;
+ int *nxtou = csa->nxtou;
+ int *fin = csa->fin;
+ int *nxtin = csa->nxtin;
+ /* updated parameters */
+# define crash (csa->crash)
+ int *rc = csa->rc;
+ int *u = csa->u;
+ int *x = csa->x;
+ int *dfct = csa->dfct;
+ /* output parameters */
+# define nsp (csa->nsp)
+ /* working parameters */
+ int *p = csa->label;
+ int *prdcsr = csa->prdcsr;
+ int *save = csa->save;
+ int *fpushf = csa->tfstou;
+ int *nxtpushf = csa->tnxtou;
+ int *fpushb = csa->tfstin;
+ int *nxtpushb = csa->tnxtin;
+ int *nxtqueue = csa->nxtqueue;
+ int *extend_arc = csa->extend_arc;
+ int *sb_level = csa->sb_level;
+ int *sb_arc = csa->sb_arc;
+ char *path_id = csa->mark;
+ /* local variables */
+ int arc, bstlevel, end, eps, extarc, factor, flow, i, incr,
+ last, lastqueue, maxcost, mincost, nas, naug, new_level, node,
+ nolist, num_passes, nxtnode, pass, pend, pr_term, prd,
+ prevarc, prevlevel, prevnode, pstart, pterm, rdcost, red_cost,
+ resid, root, secarc, seclevel, start, term, thresh_dfct;
+ /* start initialization using auction */
+ naug = 0;
+ pass = 0;
+ thresh_dfct = 0;
+ /* factor determines by how much epsilon is reduced at each
+ * minimization */
+ factor = 3;
+ /* num_passes determines how many auction scaling phases are
+ * performed */
+ num_passes = 1;
+ /* set arc flows to satisfy cs and calculate maxcost and
+ * mincost */
+ maxcost = -large;
+ mincost = large;
+ for (arc = 1; arc <= na; arc++)
+ { start = startn[arc];
+ end = endn[arc];
+ rdcost = rc[arc];
+ if (maxcost < rdcost)
+ maxcost = rdcost;
+ if (mincost > rdcost)
+ mincost = rdcost;
+ if (rdcost < 0)
+ { dfct[start] += u[arc];
+ dfct[end] -= u[arc];
+ x[arc] = u[arc];
+ u[arc] = 0;
+ }
+ else
+ x[arc] = 0;
+ }
+ /* set initial epsilon */
+ if ((maxcost - mincost) >= 8)
+ eps = (maxcost - mincost) / 8;
+ else
+ eps = 1;
+ /* set initial prices to zero */
+ for (node = 1; node <= n; node++)
+ p[node] = 0;
+ /* Initialization using auction/shortest paths. */
+L100: /* Start of the first scaling phase. */
+ pass++;
+ if ((pass == num_passes) || (eps == 1))
+ crash = 0;
+ nolist = 0;
+ /* construct list of positive surplus nodes and queue of negative
+ * surplus nodes */
+ for (node = 1; node <= n; node++)
+ { prdcsr[node] = 0;
+ path_id[node] = false;
+ extend_arc[node] = 0;
+ sb_level[node] = -large;
+ nxtqueue[node] = node + 1;
+ if (dfct[node] > 0)
+ { nolist++;
+ save[nolist] = node;
+ }
+ }
+ nxtqueue[n] = 1;
+ root = 1;
+ prevnode = lastqueue = n;
+ /* initialization with down iterations for negative surplus
+ * nodes */
+ for (i = 1; i <= nolist; i++)
+ { node = save[i];
+ nsp++;
+ /* build the list of arcs w/ room for pushing flow and find
+ * proper price for down iteration */
+ bstlevel = -large;
+ fpushf[node] = 0;
+ for (arc = fou[node]; arc > 0; arc = nxtou[arc])
+ { if (u[arc] > 0)
+ { if (fpushf[node] == 0)
+ { fpushf[node] = arc;
+ nxtpushf[arc] = 0;
+ last = arc;
+ }
+ else
+ { nxtpushf[last] = arc;
+ nxtpushf[arc] = 0;
+ last = arc;
+ }
+ }
+ if (x[arc] > 0)
+ { new_level = p[endn[arc]] + rc[arc];
+ if (new_level > bstlevel)
+ { bstlevel = new_level;
+ extarc = arc;
+ }
+ }
+ }
+ fpushb[node] = 0;
+ for (arc = fin[node]; arc > 0; arc = nxtin[arc])
+ { if (x[arc] > 0)
+ { if (fpushb[node] == 0)
+ { fpushb[node] = arc;
+ nxtpushb[arc] = 0;
+ last = arc;
+ }
+ else
+ { nxtpushb[last] = arc;
+ nxtpushb[arc] = 0;
+ last = arc;
+ }
+ }
+ if (u[arc] > 0)
+ { new_level = p[startn[arc]] - rc[arc];
+ if (new_level > bstlevel)
+ { bstlevel = new_level;
+ extarc = -arc;
+ }
+ }
+ }
+ extend_arc[node] = extarc;
+ p[node] = bstlevel - eps;
+ }
+L200: /* Start the augmentation cycles of the new scaling phase. */
+ if (dfct[root] >= thresh_dfct)
+ goto L3000;
+ term = root;
+ path_id[root] = true;
+L500: /* Main forward algorithm with root as origin. */
+ /* start of a new forward iteration */
+ pterm = p[term];
+ extarc = extend_arc[term];
+ if (extarc == 0)
+ { /* build the list of arcs w/ room for pushing flow */
+ fpushf[term] = 0;
+ for (arc = fou[term]; arc > 0; arc = nxtou[arc])
+ { if (u[arc] > 0)
+ { if (fpushf[term] == 0)
+ { fpushf[term] = arc;
+ nxtpushf[arc] = 0;
+ last = arc;
+ }
+ else
+ { nxtpushf[last] = arc;
+ nxtpushf[arc] = 0;
+ last = arc;
+ }
+ }
+ }
+ fpushb[term] = 0;
+ for (arc = fin[term]; arc > 0; arc = nxtin[arc])
+ { if (x[arc] > 0)
+ { if (fpushb[term] == 0)
+ { fpushb[term] = arc;
+ nxtpushb[arc] = 0;
+ last = arc;
+ }
+ else
+ { nxtpushb[last] = arc;
+ nxtpushb[arc] = 0;
+ last = arc;
+ }
+ }
+ }
+ goto L600;
+ }
+ /* speculative path extension attempt */
+ /* note: arc > 0 means that arc is oriented from the root to the
+ * destinations
+ * arc < 0 means that arc is oriented from the destinations to the
+ * root
+ * extarc = 0 or prdarc = 0, means the extension arc or the
+ * predecessor arc, respectively, has not been established */
+ if (extarc > 0)
+ { if (u[extarc] == 0)
+ { seclevel = sb_level[term];
+ goto L580;
+ }
+ end = endn[extarc];
+ bstlevel = p[end] + rc[extarc];
+ if (pterm >= bstlevel)
+ { if (path_id[end])
+ goto L1200;
+ term = end;
+ prdcsr[term] = extarc;
+ path_id[term] = true;
+ /* if negative surplus node is found, do an augmentation */
+ if (dfct[term] > 0)
+ goto L2000;
+ /* return for another iteration */
+ goto L500;
+ }
+ }
+ else
+ { extarc = -extarc;
+ if (x[extarc] == 0)
+ { seclevel = sb_level[term];
+ goto L580;
+ }
+ start = startn[extarc];
+ bstlevel = p[start] - rc[extarc];
+ if (pterm >= bstlevel)
+ { if (path_id[start])
+ goto L1200;
+ term = start;
+ prdcsr[term] = -extarc;
+ path_id[term] = true;
+ /* if negative surplus node is found, do an augmentation */
+ if (dfct[term] > 0)
+ goto L2000;
+ /* return for another iteration */
+ goto L500;
+ }
+ }
+L550: /* second best logic test applied to save a full node scan
+ * if old best level continues to be best go for another
+ * contraction */
+ seclevel = sb_level[term];
+ if (bstlevel <= seclevel)
+ goto L800;
+L580: /* if second best can be used do either a contraction or start
+ * over with a speculative extension */
+ if (seclevel > -large)
+ { extarc = sb_arc[term];
+ if (extarc > 0)
+ { if (u[extarc] == 0)
+ goto L600;
+ bstlevel = p[endn[extarc]] + rc[extarc];
+ }
+ else
+ { if (x[-extarc] == 0)
+ goto L600;
+ bstlevel = p[startn[-extarc]] - rc[-extarc];
+ }
+ if (bstlevel == seclevel)
+ { sb_level[term] = -large;
+ extend_arc[term] = extarc;
+ goto L800;
+ }
+ }
+L600: /* extension/contraction attempt was unsuccessful, so scan
+ * terminal node */
+ nsp++;
+ bstlevel = seclevel = large;
+ for (arc = fpushf[term]; arc > 0; arc = nxtpushf[arc])
+ { new_level = p[endn[arc]] + rc[arc];
+ if (new_level < seclevel)
+ { if (new_level < bstlevel)
+ { seclevel = bstlevel;
+ bstlevel = new_level;
+ secarc = extarc;
+ extarc = arc;
+ }
+ else
+ { seclevel = new_level;
+ secarc = arc;
+ }
+ }
+ }
+ for (arc = fpushb[term]; arc > 0; arc = nxtpushb[arc])
+ { new_level = p[startn[arc]] - rc[arc];
+ if (new_level < seclevel)
+ { if (new_level < bstlevel)
+ { seclevel = bstlevel;
+ bstlevel = new_level;
+ secarc = extarc;
+ extarc = -arc;
+ }
+ else
+ { seclevel = new_level;
+ secarc = -arc;
+ }
+ }
+ }
+ sb_level[term] = seclevel;
+ sb_arc[term] = secarc;
+ extend_arc[term] = extarc;
+L800: /* End of node scan. */
+ /* if the terminal node is the root, adjust its price and change
+ * root */
+ if (term == root)
+ { p[term] = bstlevel + eps;
+ if (p[term] >= large)
+ { /* no path to the destination */
+ /* problem is found to be infeasible */
+ return 1;
+ }
+ path_id[root] = false;
+ prevnode = root;
+ root = nxtqueue[root];
+ goto L200;
+ }
+ /* check whether extension or contraction */
+ prd = prdcsr[term];
+ if (prd > 0)
+ { pr_term = startn[prd];
+ prevlevel = p[pr_term] - rc[prd];
+ }
+ else
+ { pr_term = endn[-prd];
+ prevlevel = p[pr_term] + rc[-prd];
+ }
+ if (prevlevel > bstlevel)
+ { /* path extension */
+ if (prevlevel >= bstlevel + eps)
+ p[term] = bstlevel + eps;
+ else
+ p[term] = prevlevel;
+ if (extarc > 0)
+ { end = endn[extarc];
+ if (path_id[end])
+ goto L1200;
+ term = end;
+ }
+ else
+ { start = startn[-extarc];
+ if (path_id[start])
+ goto L1200;
+ term = start;
+ }
+ prdcsr[term] = extarc;
+ path_id[term] = true;
+ /* if negative surplus node is found, do an augmentation */
+ if (dfct[term] > 0)
+ goto L2000;
+ /* return for another iteration */
+ goto L500;
+ }
+ else
+ { /* path contraction */
+ p[term] = bstlevel + eps;
+ path_id[term] = false;
+ term = pr_term;
+ if (pr_term != root)
+ { if (bstlevel <= pterm + eps)
+ goto L2000;
+ }
+ pterm = p[term];
+ extarc = prd;
+ if (prd > 0)
+ bstlevel += eps + rc[prd];
+ else
+ bstlevel += eps - rc[-prd];
+ /* do a second best test and if that fails, do a full node
+ * scan */
+ goto L550;
+ }
+L1200:/* A cycle is about to form; do a retreat sequence. */
+ node = term;
+L1600:if (node != root)
+ { path_id[node] = false;
+ prd = prdcsr[node];
+ if (prd > 0)
+ { pr_term = startn[prd];
+ if (p[pr_term] == p[node] + rc[prd] + eps)
+ { node = pr_term;
+ goto L1600;
+ }
+ }
+ else
+ { pr_term = endn[-prd];
+ if (p[pr_term] == p[node] - rc[-prd] + eps)
+ { node = pr_term;
+ goto L1600;
+ }
+ }
+ /* do a full scan and price rise at pr_term */
+ nsp++;
+ bstlevel = seclevel = large;
+ for (arc = fpushf[pr_term]; arc > 0; arc = nxtpushf[arc])
+ { new_level = p[endn[arc]] + rc[arc];
+ if (new_level < seclevel)
+ { if (new_level < bstlevel)
+ { seclevel = bstlevel;
+ bstlevel = new_level;
+ secarc = extarc;
+ extarc = arc;
+ }
+ else
+ { seclevel = new_level;
+ secarc = arc;
+ }
+ }
+ }
+ for (arc = fpushb[pr_term]; arc > 0; arc = nxtpushb[arc])
+ { new_level = p[startn[arc]] - rc[arc];
+ if (new_level < seclevel)
+ { if (new_level < bstlevel)
+ { seclevel = bstlevel;
+ bstlevel = new_level;
+ secarc = extarc;
+ extarc = -arc;
+ }
+ else
+ { seclevel = new_level;
+ secarc = -arc;
+ }
+ }
+ }
+ sb_level[pr_term] = seclevel;
+ sb_arc[pr_term] = secarc;
+ extend_arc[pr_term] = extarc;
+ p[pr_term] = bstlevel + eps;
+ if (pr_term == root)
+ { prevnode = root;
+ path_id[root] = false;
+ root = nxtqueue[root];
+ goto L200;
+ }
+ path_id[pr_term] = false;
+ prd = prdcsr[pr_term];
+ if (prd > 0)
+ term = startn[prd];
+ else
+ term = endn[-prd];
+ if (term == root)
+ { prevnode = root;
+ path_id[root] = false;
+ root = nxtqueue[root];
+ goto L200;
+ }
+ else
+ goto L2000;
+ }
+L2000:/* End of auction/shortest path routine. */
+ /* do augmentation from root and correct the push lists */
+ incr = -dfct[root];
+ for (node = root;;)
+ { extarc = extend_arc[node];
+ path_id[node] = false;
+ if (extarc > 0)
+ { node = endn[extarc];
+ if (incr > u[extarc])
+ incr = u[extarc];
+ }
+ else
+ { node = startn[-extarc];
+ if (incr > x[-extarc])
+ incr = x[-extarc];
+ }
+ if (node == term)
+ break;
+ }
+ path_id[term] = false;
+ if (dfct[term] > 0)
+ { if (incr > dfct[term])
+ incr = dfct[term];
+ }
+ for (node = root;;)
+ { extarc = extend_arc[node];
+ if (extarc > 0)
+ { end = endn[extarc];
+ /* add arc to the reduced graph */
+ if (x[extarc] == 0)
+ { nxtpushb[extarc] = fpushb[end];
+ fpushb[end] = extarc;
+ new_level = p[node] - rc[extarc];
+ if (sb_level[end] > new_level)
+ { sb_level[end] = new_level;
+ sb_arc[end] = -extarc;
+ }
+ }
+ x[extarc] += incr;
+ u[extarc] -= incr;
+ /* remove arc from the reduced graph */
+ if (u[extarc] == 0)
+ { nas++;
+ arc = fpushf[node];
+ if (arc == extarc)
+ fpushf[node] = nxtpushf[arc];
+ else
+ { prevarc = arc;
+ arc = nxtpushf[arc];
+ while (arc > 0)
+ { if (arc == extarc)
+ { nxtpushf[prevarc] = nxtpushf[arc];
+ break;
+ }
+ prevarc = arc;
+ arc = nxtpushf[arc];
+ }
+ }
+ }
+ node = end;
+ }
+ else
+ { extarc = -extarc;
+ start = startn[extarc];
+ /* add arc to the reduced graph */
+ if (u[extarc] == 0)
+ { nxtpushf[extarc] = fpushf[start];
+ fpushf[start] = extarc;
+ new_level = p[node] + rc[extarc];
+ if (sb_level[start] > new_level)
+ { sb_level[start] = new_level;
+ sb_arc[start] = extarc;
+ }
+ }
+ u[extarc] += incr;
+ x[extarc] -= incr;
+ /* remove arc from the reduced graph */
+ if (x[extarc] == 0)
+ { nas++;
+ arc = fpushb[node];
+ if (arc == extarc)
+ fpushb[node] = nxtpushb[arc];
+ else
+ { prevarc = arc;
+ arc = nxtpushb[arc];
+ while (arc > 0)
+ { if (arc == extarc)
+ { nxtpushb[prevarc] = nxtpushb[arc];
+ break;
+ }
+ prevarc = arc;
+ arc = nxtpushb[arc];
+ }
+ }
+ }
+ node = start;
+ }
+ if (node == term)
+ break;
+ }
+ dfct[term] -= incr;
+ dfct[root] += incr;
+ /* insert term in the queue if it has a large enough surplus */
+ if (dfct[term] < thresh_dfct)
+ { if (nxtqueue[term] == 0)
+ { nxtnode = nxtqueue[root];
+ if ((p[term] >= p[nxtnode]) && (root != nxtnode))
+ { nxtqueue[root] = term;
+ nxtqueue[term] = nxtnode;
+ }
+ else
+ { nxtqueue[prevnode] = term;
+ nxtqueue[term] = root;
+ prevnode = term;
+ }
+ }
+ }
+ /* if root has a large enough surplus, keep it in the queue and
+ * return for another iteration */
+ if (dfct[root] < thresh_dfct)
+ { prevnode = root;
+ root = nxtqueue[root];
+ goto L200;
+ }
+L3000:/* end of augmentation cycle */
+ /* Check for termination of scaling phase. If scaling phase is not
+ * finished, advance the queue and return to take another node. */
+ nxtnode = nxtqueue[root];
+ if (root != nxtnode)
+ { nxtqueue[root] = 0;
+ nxtqueue[prevnode] = nxtnode;
+ root = nxtnode;
+ goto L200;
+ }
+ /* End of subproblem (scaling phase). */
+ /* Reduce epsilon. */
+ eps /= factor;
+ if (eps < 1) eps = 1;
+ thresh_dfct /= factor;
+ if (eps == 1) thresh_dfct = 0;
+ /* if another auction scaling phase remains, reset the flows &
+ * the push lists; else reset arc flows to satisfy cs and compute
+ * reduced costs */
+ if (crash == 1)
+ { for (arc = 1; arc <= na; arc++)
+ { start = startn[arc];
+ end = endn[arc];
+ pstart = p[start];
+ pend = p[end];
+ if (pstart > pend + eps + rc[arc])
+ { resid = u[arc];
+ if (resid > 0)
+ { dfct[start] += resid;
+ dfct[end] -= resid;
+ x[arc] += resid;
+ u[arc] = 0;
+ }
+ }
+ else if (pstart < pend - eps + rc[arc])
+ { flow = x[arc];
+ if (flow > 0)
+ { dfct[start] -= flow;
+ dfct[end] += flow;
+ x[arc] = 0;
+ u[arc] += flow;
+ }
+ }
+ }
+ /* return for another phase */
+ goto L100;
+ }
+ else
+ { crash = 1;
+ for (arc = 1; arc <= na; arc++)
+ { start = startn[arc];
+ end = endn[arc];
+ red_cost = rc[arc] + p[end] - p[start];
+ if (red_cost < 0)
+ { resid = u[arc];
+ if (resid > 0)
+ { dfct[start] += resid;
+ dfct[end] -= resid;
+ x[arc] += resid;
+ u[arc] = 0;
+ }
+ }
+ else if (red_cost > 0)
+ { flow = x[arc];
+ if (flow > 0)
+ { dfct[start] -= flow;
+ dfct[end] += flow;
+ x[arc] = 0;
+ u[arc] += flow;
+ }
+ }
+ rc[arc] = red_cost;
+ }
+ }
+ return 0;
+# undef crash
+# undef nsp
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/relax4.h b/test/monniaux/glpk-4.65/src/misc/relax4.h
new file mode 100644
index 00000000..f48b8508
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/relax4.h
@@ -0,0 +1,102 @@
+/* relax4.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef RELAX4_H
+#define RELAX4_H
+
+struct relax4_csa
+{ /* common storage area */
+ /* input parameters --------------------------------------------*/
+ int n;
+ /* number of nodes */
+ int na;
+ /* number of arcs */
+ int large;
+ /* very large int to represent infinity */
+ int repeat;
+ /* true if initialization is to be skipped (false otherwise) */
+ int crash;
+ /* 0 if default initialization is used
+ * 1 if auction initialization is used */
+ int *startn; /* int startn[1+na]; */
+ /* startn[j] = starting node for arc j, j = 1,...,na */
+ int *endn; /* int endn[1+na] */
+ /* endn[j] = ending node for arc j, j = 1,...,na */
+ int *fou; /* int fou[1+n]; */
+ /* fou[i] = first arc out of node i, i = 1,...,n */
+ int *nxtou; /* int nxtou[1+na]; */
+ /* nxtou[j] = next arc out of the starting node of arc j,
+ * j = 1,...,na */
+ int *fin; /* int fin[1+n]; */
+ /* fin[i] = first arc into node i, i = 1,...,n */
+ int *nxtin; /* int nxtin[1+na]; */
+ /* nxtin[j] = next arc into the ending node of arc j,
+ * j = 1,...,na */
+ /* updated parameters ------------------------------------------*/
+ int *rc; /* int rc[1+na]; */
+ /* rc[j] = reduced cost of arc j, j = 1,...,na */
+ int *u; /* int u[1+na]; */
+ /* u[j] = capacity of arc j on input
+ * and (capacity of arc j) - x(j) on output, j = 1,...,na */
+ int *dfct; /* int dfct[1+n]; */
+ /* dfct[i] = demand at node i on input
+ * and zero on output, i = 1,...,n */
+ /* output parameters -------------------------------------------*/
+ int *x; /* int x[1+na]; */
+ /* x[j] = flow on arc j, j = 1,...,na */
+ int nmultinode;
+ /* number of multinode relaxation iterations in RELAX4 */
+ int iter;
+ /* number of relaxation iterations in RELAX4 */
+ int num_augm;
+ /* number of flow augmentation steps in RELAX4 */
+ int num_ascnt;
+ /* number of multinode ascent steps in RELAX4 */
+ int nsp;
+ /* number of auction/shortest path iterations */
+ /* working parameters ------------------------------------------*/
+ int *label; /* int label, tempin, p[1+n]; */
+ int *prdcsr; /* int prdcsr, tempou, price[1+n]; */
+ int *save; /* int save[1+na]; */
+ int *tfstou; /* int tfstou, fpushf[1+n]; */
+ int *tnxtou; /* int tnxtou, nxtpushf[1+na]; */
+ int *tfstin; /* int tfstin, fpushb[1+n]; */
+ int *tnxtin; /* int tnxtin, nxtpushb[1+na]; */
+ int *nxtqueue; /* int nxtqueue[1+n]; */
+ char *scan; /* bool scan[1+n]; */
+ char *mark; /* bool mark, path_id[1+n]; */
+ /* working parameters used by routine auction only -------------*/
+ int *extend_arc; /* int extend_arc[1+n]; */
+ int *sb_level; /* int sb_level[1+n]; */
+ int *sb_arc; /* int sb_arc[1+n]; */
+};
+
+#define relax4 _glp_relax4
+int relax4(struct relax4_csa *csa);
+
+#define relax4_inidat _glp_relax4_inidat
+void relax4_inidat(struct relax4_csa *csa);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/rng.c b/test/monniaux/glpk-4.65/src/misc/rng.c
new file mode 100644
index 00000000..e0acb53a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/rng.c
@@ -0,0 +1,227 @@
+/* rng.c (pseudo-random number generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* This code is a modified version of the module GB_FLIP, a portable
+* pseudo-random number generator. The original version of GB_FLIP is
+* a part of The Stanford GraphBase developed by Donald E. Knuth (see
+* http://www-cs-staff.stanford.edu/~knuth/sgb.html).
+*
+* Note that all changes concern only external names, so this modified
+* version produces exactly the same results as the original version.
+*
+* Changes were made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "rng.h"
+
+#if 0
+int A[56] = { -1 };
+#else
+#define A (rand->A)
+#endif
+/* pseudo-random values */
+
+#if 0
+int *fptr = A;
+#else
+#define fptr (rand->fptr)
+#endif
+/* the next A value to be exported */
+
+#define mod_diff(x, y) (((x) - (y)) & 0x7FFFFFFF)
+/* difference modulo 2^31 */
+
+static int flip_cycle(RNG *rand)
+{ /* this is an auxiliary routine to do 55 more steps of the basic
+ * recurrence, at high speed, and to reset fptr */
+ int *ii, *jj;
+ for (ii = &A[1], jj = &A[32]; jj <= &A[55]; ii++, jj++)
+ *ii = mod_diff(*ii, *jj);
+ for (jj = &A[1]; ii <= &A[55]; ii++, jj++)
+ *ii = mod_diff(*ii, *jj);
+ fptr = &A[54];
+ return A[55];
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_create_rand - create pseudo-random number generator
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* RNG *rng_create_rand(void);
+*
+* DESCRIPTION
+*
+* The routine rng_create_rand creates and initializes a pseudo-random
+* number generator.
+*
+* RETURNS
+*
+* The routine returns a pointer to the generator created. */
+
+RNG *rng_create_rand(void)
+{ RNG *rand;
+ int i;
+ rand = talloc(1, RNG);
+ A[0] = -1;
+ for (i = 1; i <= 55; i++) A[i] = 0;
+ fptr = A;
+ rng_init_rand(rand, 1);
+ return rand;
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_init_rand - initialize pseudo-random number generator
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* void rng_init_rand(RNG *rand, int seed);
+*
+* DESCRIPTION
+*
+* The routine rng_init_rand initializes the pseudo-random number
+* generator. The parameter seed may be any integer number. Note that
+* on creating the generator this routine is called with the parameter
+* seed equal to 1. */
+
+void rng_init_rand(RNG *rand, int seed)
+{ int i;
+ int prev = seed, next = 1;
+ seed = prev = mod_diff(prev, 0);
+ A[55] = prev;
+ for (i = 21; i; i = (i + 21) % 55)
+ { A[i] = next;
+ next = mod_diff(prev, next);
+ if (seed & 1)
+ seed = 0x40000000 + (seed >> 1);
+ else
+ seed >>= 1;
+ next = mod_diff(next, seed);
+ prev = A[i];
+ }
+ flip_cycle(rand);
+ flip_cycle(rand);
+ flip_cycle(rand);
+ flip_cycle(rand);
+ flip_cycle(rand);
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_next_rand - obtain pseudo-random integer in the range [0, 2^31-1]
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* int rng_next_rand(RNG *rand);
+*
+* RETURNS
+*
+* The routine rng_next_rand returns a next pseudo-random integer which
+* is uniformly distributed between 0 and 2^31-1, inclusive. The period
+* length of the generated numbers is 2^85 - 2^30. The low order bits of
+* the generated numbers are just as random as the high-order bits. */
+
+int rng_next_rand(RNG *rand)
+{ return
+ *fptr >= 0 ? *fptr-- : flip_cycle(rand);
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_unif_rand - obtain pseudo-random integer in the range [0, m-1]
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* int rng_unif_rand(RNG *rand, int m);
+*
+* RETURNS
+*
+* The routine rng_unif_rand returns a next pseudo-random integer which
+* is uniformly distributed between 0 and m-1, inclusive, where m is any
+* positive integer less than 2^31. */
+
+#define two_to_the_31 ((unsigned int)0x80000000)
+
+int rng_unif_rand(RNG *rand, int m)
+{ unsigned int t = two_to_the_31 - (two_to_the_31 % m);
+ int r;
+ xassert(m > 0);
+ do { r = rng_next_rand(rand); } while (t <= (unsigned int)r);
+ return r % m;
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_delete_rand - delete pseudo-random number generator
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* void rng_delete_rand(RNG *rand);
+*
+* DESCRIPTION
+*
+* The routine rng_delete_rand frees all the memory allocated to the
+* specified pseudo-random number generator. */
+
+void rng_delete_rand(RNG *rand)
+{ tfree(rand);
+ return;
+}
+
+/**********************************************************************/
+
+#ifdef GLP_TEST
+/* To be sure that this modified version produces the same results as
+ * the original version, run this validation program. */
+
+int main(void)
+{ RNG *rand;
+ int j;
+ rand = rng_create_rand();
+ rng_init_rand(rand, -314159);
+ if (rng_next_rand(rand) != 119318998)
+ { fprintf(stderr, "Failure on the first try!\n");
+ return -1;
+ }
+ for (j = 1; j <= 133; j++) rng_next_rand(rand);
+ if (rng_unif_rand(rand, 0x55555555) != 748103812)
+ { fprintf(stderr, "Failure on the second try!\n");
+ return -2;
+ }
+ fprintf(stderr, "OK, the random-number generator routines seem to"
+ " work!\n");
+ rng_delete_rand(rand);
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/rng.h b/test/monniaux/glpk-4.65/src/misc/rng.h
new file mode 100644
index 00000000..49725e05
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/rng.h
@@ -0,0 +1,67 @@
+/* rng.h (pseudo-random number generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef RNG_H
+#define RNG_H
+
+typedef struct RNG RNG;
+
+struct RNG
+{ /* Knuth's portable pseudo-random number generator */
+ int A[56];
+ /* pseudo-random values */
+ int *fptr;
+ /* the next A value to be exported */
+};
+
+#define rng_create_rand _glp_rng_create_rand
+RNG *rng_create_rand(void);
+/* create pseudo-random number generator */
+
+#define rng_init_rand _glp_rng_init_rand
+void rng_init_rand(RNG *rand, int seed);
+/* initialize pseudo-random number generator */
+
+#define rng_next_rand _glp_rng_next_rand
+int rng_next_rand(RNG *rand);
+/* obtain pseudo-random integer in the range [0, 2^31-1] */
+
+#define rng_unif_rand _glp_rng_unif_rand
+int rng_unif_rand(RNG *rand, int m);
+/* obtain pseudo-random integer in the range [0, m-1] */
+
+#define rng_delete_rand _glp_rng_delete_rand
+void rng_delete_rand(RNG *rand);
+/* delete pseudo-random number generator */
+
+#define rng_unif_01 _glp_rng_unif_01
+double rng_unif_01(RNG *rand);
+/* obtain pseudo-random number in the range [0, 1] */
+
+#define rng_uniform _glp_rng_uniform
+double rng_uniform(RNG *rand, double a, double b);
+/* obtain pseudo-random number in the range [a, b] */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/rng1.c b/test/monniaux/glpk-4.65/src/misc/rng1.c
new file mode 100644
index 00000000..b89f676f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/rng1.c
@@ -0,0 +1,73 @@
+/* rng1.c (pseudo-random number generator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "rng.h"
+
+/***********************************************************************
+* NAME
+*
+* rng_unif_01 - obtain pseudo-random number in the range [0, 1]
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* double rng_unif_01(RNG *rand);
+*
+* RETURNS
+*
+* The routine rng_unif_01 returns a next pseudo-random number which is
+* uniformly distributed in the range [0, 1]. */
+
+double rng_unif_01(RNG *rand)
+{ double x;
+ x = (double)rng_next_rand(rand) / 2147483647.0;
+ xassert(0.0 <= x && x <= 1.0);
+ return x;
+}
+
+/***********************************************************************
+* NAME
+*
+* rng_uniform - obtain pseudo-random number in the range [a, b]
+*
+* SYNOPSIS
+*
+* #include "rng.h"
+* double rng_uniform(RNG *rand, double a, double b);
+*
+* RETURNS
+*
+* The routine rng_uniform returns a next pseudo-random number which is
+* uniformly distributed in the range [a, b]. */
+
+double rng_uniform(RNG *rand, double a, double b)
+{ double x;
+ xassert(a < b);
+ x = rng_unif_01(rand);
+ x = a * (1.0 - x) + b * x;
+ xassert(a <= x && x <= b);
+ return x;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/round2n.c b/test/monniaux/glpk-4.65/src/misc/round2n.c
new file mode 100644
index 00000000..8a94c616
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/round2n.c
@@ -0,0 +1,64 @@
+/* round2n.c (round floating-point number to nearest power of two) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "misc.h"
+
+/***********************************************************************
+* NAME
+*
+* round2n - round floating-point number to nearest power of two
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* double round2n(double x);
+*
+* RETURNS
+*
+* Given a positive floating-point value x the routine round2n returns
+* 2^n such that |x - 2^n| is minimal.
+*
+* EXAMPLES
+*
+* round2n(10.1) = 2^3 = 8
+* round2n(15.3) = 2^4 = 16
+* round2n(0.01) = 2^(-7) = 0.0078125
+*
+* BACKGROUND
+*
+* Let x = f * 2^e, where 0.5 <= f < 1 is a normalized fractional part,
+* e is an integer exponent. Then, obviously, 0.5 * 2^e <= x < 2^e, so
+* if x - 0.5 * 2^e <= 2^e - x, we choose 0.5 * 2^e = 2^(e-1), and 2^e
+* otherwise. The latter condition can be written as 2 * x <= 1.5 * 2^e
+* or 2 * f * 2^e <= 1.5 * 2^e or, finally, f <= 0.75. */
+
+double round2n(double x)
+{ int e;
+ double f;
+ xassert(x > 0.0);
+ f = frexp(x, &e);
+ return ldexp(1.0, f <= 0.75 ? e-1 : e);
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/str2int.c b/test/monniaux/glpk-4.65/src/misc/str2int.c
new file mode 100644
index 00000000..cbd6e953
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/str2int.c
@@ -0,0 +1,92 @@
+/* str2int.c (convert string to int) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "misc.h"
+#include "stdc.h"
+
+/***********************************************************************
+* NAME
+*
+* str2int - convert character string to value of int type
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* int str2int(const char *str, int *val);
+*
+* DESCRIPTION
+*
+* The routine str2int converts the character string str to a value of
+* integer type and stores the value into location, which the parameter
+* val points to (in the case of error content of this location is not
+* changed).
+*
+* RETURNS
+*
+* The routine returns one of the following error codes:
+*
+* 0 - no error;
+* 1 - value out of range;
+* 2 - character string is syntactically incorrect. */
+
+int str2int(const char *str, int *val_)
+{ int d, k, s, val = 0;
+ /* scan optional sign */
+ if (str[0] == '+')
+ s = +1, k = 1;
+ else if (str[0] == '-')
+ s = -1, k = 1;
+ else
+ s = +1, k = 0;
+ /* check for the first digit */
+ if (!isdigit((unsigned char)str[k]))
+ return 2;
+ /* scan digits */
+ while (isdigit((unsigned char)str[k]))
+ { d = str[k++] - '0';
+ if (s > 0)
+ { if (val > INT_MAX / 10)
+ return 1;
+ val *= 10;
+ if (val > INT_MAX - d)
+ return 1;
+ val += d;
+ }
+ else /* s < 0 */
+ { if (val < INT_MIN / 10)
+ return 1;
+ val *= 10;
+ if (val < INT_MIN + d)
+ return 1;
+ val -= d;
+ }
+ }
+ /* check for terminator */
+ if (str[k] != '\0')
+ return 2;
+ /* conversion has been done */
+ *val_ = val;
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/str2num.c b/test/monniaux/glpk-4.65/src/misc/str2num.c
new file mode 100644
index 00000000..26c2f68f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/str2num.c
@@ -0,0 +1,110 @@
+/* str2num.c (convert string to double) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "misc.h"
+#include "stdc.h"
+
+/***********************************************************************
+* NAME
+*
+* str2num - convert character string to value of double type
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* int str2num(const char *str, double *val);
+*
+* DESCRIPTION
+*
+* The routine str2num converts the character string str to a value of
+* double type and stores the value into location, which the parameter
+* val points to (in the case of error content of this location is not
+* changed).
+*
+* RETURNS
+*
+* The routine returns one of the following error codes:
+*
+* 0 - no error;
+* 1 - value out of range;
+* 2 - character string is syntactically incorrect. */
+
+int str2num(const char *str, double *val_)
+{ int k;
+ double val;
+ /* scan optional sign */
+ k = (str[0] == '+' || str[0] == '-' ? 1 : 0);
+ /* check for decimal point */
+ if (str[k] == '.')
+ { k++;
+ /* a digit should follow it */
+ if (!isdigit((unsigned char)str[k]))
+ return 2;
+ k++;
+ goto frac;
+ }
+ /* integer part should start with a digit */
+ if (!isdigit((unsigned char)str[k]))
+ return 2;
+ /* scan integer part */
+ while (isdigit((unsigned char)str[k]))
+ k++;
+ /* check for decimal point */
+ if (str[k] == '.') k++;
+frac: /* scan optional fraction part */
+ while (isdigit((unsigned char)str[k]))
+ k++;
+ /* check for decimal exponent */
+ if (str[k] == 'E' || str[k] == 'e')
+ { k++;
+ /* scan optional sign */
+ if (str[k] == '+' || str[k] == '-')
+ k++;
+ /* a digit should follow E, E+ or E- */
+ if (!isdigit((unsigned char)str[k]))
+ return 2;
+ }
+ /* scan optional exponent part */
+ while (isdigit((unsigned char)str[k]))
+ k++;
+ /* check for terminator */
+ if (str[k] != '\0')
+ return 2;
+ /* perform conversion */
+ { char *endptr;
+ val = strtod(str, &endptr);
+ if (*endptr != '\0')
+ return 2;
+ }
+ /* check for overflow */
+ if (!(-DBL_MAX <= val && val <= +DBL_MAX))
+ return 1;
+ /* check for underflow */
+ if (-DBL_MIN < val && val < +DBL_MIN)
+ val = 0.0;
+ /* conversion has been done */
+ *val_ = val;
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/strspx.c b/test/monniaux/glpk-4.65/src/misc/strspx.c
new file mode 100644
index 00000000..fe8a2a10
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/strspx.c
@@ -0,0 +1,60 @@
+/* strspx.c (remove all spaces from string) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "misc.h"
+
+/***********************************************************************
+* NAME
+*
+* strspx - remove all spaces from character string
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* char *strspx(char *str);
+*
+* DESCRIPTION
+*
+* The routine strspx removes all spaces from the character string str.
+*
+* RETURNS
+*
+* The routine returns a pointer to the character string.
+*
+* EXAMPLES
+*
+* strspx(" Errare humanum est ") => "Errarehumanumest"
+*
+* strspx(" ") => "" */
+
+char *strspx(char *str)
+{ char *s, *t;
+ for (s = t = str; *s; s++)
+ { if (*s != ' ')
+ *t++ = *s;
+ }
+ *t = '\0';
+ return str;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/strtrim.c b/test/monniaux/glpk-4.65/src/misc/strtrim.c
new file mode 100644
index 00000000..9992c4b0
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/strtrim.c
@@ -0,0 +1,62 @@
+/* strtrim.c (remove trailing spaces from string) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "misc.h"
+#include "stdc.h"
+
+/***********************************************************************
+* NAME
+*
+* strtrim - remove trailing spaces from character string
+*
+* SYNOPSIS
+*
+* #include "misc.h"
+* char *strtrim(char *str);
+*
+* DESCRIPTION
+*
+* The routine strtrim removes trailing spaces from the character
+* string str.
+*
+* RETURNS
+*
+* The routine returns a pointer to the character string.
+*
+* EXAMPLES
+*
+* strtrim("Errare humanum est ") => "Errare humanum est"
+*
+* strtrim(" ") => "" */
+
+char *strtrim(char *str)
+{ char *t;
+ for (t = strrchr(str, '\0') - 1; t >= str; t--)
+ { if (*t != ' ')
+ break;
+ *t = '\0';
+ }
+ return str;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/triang.c b/test/monniaux/glpk-4.65/src/misc/triang.c
new file mode 100644
index 00000000..99ba4d60
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/triang.c
@@ -0,0 +1,311 @@
+/* triang.c (find maximal triangular part of rectangular matrix) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "triang.h"
+
+/***********************************************************************
+* triang - find maximal triangular part of rectangular matrix
+*
+* Given a mxn sparse matrix A this routine finds permutation matrices
+* P and Q such that matrix A' = P * A * Q has the following structure:
+*
+* 1 s n
+* 1 * . . . . . x x x x x
+* * * . . . . x x x x x
+* * * * . . . x x x x x
+* * * * * . . x x x x x
+* * * * * * . x x x x x
+* s * * * * * * x x x x x
+* x x x x x x x x x x x
+* x x x x x x x x x x x
+* m x x x x x x x x x x x
+*
+* where '*' are elements of the triangular part, '.' are structural
+* zeros, 'x' are other elements.
+*
+* The formal routine mat specifies the original matrix A in both row-
+* and column-wise format. If the routine mat is called with k = +i,
+* 1 <= i <= m, it should store column indices and values of non-zero
+* elements of i-th row of A in locations ind[1], ..., ind[len] and
+* val[1], ..., val[len], resp., where len is the returned number of
+* non-zeros in the row, 0 <= len <= n. Similarly, if the routine mat
+* is called with k = -j, 1 <= j <= n, it should store row indices and
+* values of non-zero elements of j-th column of A and return len, the
+* number of non-zeros in the column, 0 <= len <= m. Should note that
+* duplicate indices are not allowed.
+*
+* The parameter info is a transit pointer passed to the routine mat.
+*
+* The parameter tol is a tolerance. The routine triang guarantees that
+* each diagonal element in the triangular part of matrix A' is not
+* less in magnitude than tol * max, where max is the maximal magnitude
+* of elements in corresponding column.
+*
+* On exit the routine triang stores information on the triangular part
+* found in the arrays rn and cn. Elements rn[1], ..., rn[s] specify
+* row numbers and elements cn[1], ..., cn[s] specify column numbers
+* of the original matrix A, which correspond to rows/columns 1, ..., s
+* of matrix A', where s is the size of the triangular part returned by
+* the routine, 0 <= s <= min(m, n). The order of rows and columns that
+* are not included in the triangular part remains unspecified.
+*
+* ALGORITHM
+*
+* The routine triang uses a simple greedy heuristic.
+*
+* At some step the matrix A' = P * A * Q has the following structure:
+*
+* 1 n
+* 1 * . . . . . . . x x x
+* * * . . . . . . x x x
+* * * * . . . . . x x x
+* * * * * . . . . x x x
+* x x x x # # # # x x x
+* x x x x # # # # x x x
+* x x x x # # # # x x x
+* x x x x # # # # x x x
+* m x x x x # # # # x x x
+*
+* where '#' are elements of active submatrix. Initially P = Q = I, so
+* the active submatrix is the original matrix A = A'.
+*
+* If some row has exactly one non-zero in the active submatrix (row
+* singleton), the routine includes this row and corresponding column
+* in the triangular part, and removes the column from the active
+* submatrix. Otherwise, the routine simply removes a column having
+* maximal number of non-zeros from the active submatrix in the hope
+* that new row singleton(s) will appear.
+*
+* COMPLEXITY
+*
+* The time complexity of the routine triang is O(nnz), where nnz is
+* number of non-zeros in the original matrix A. */
+
+int triang(int m, int n, int (*mat)(void *info, int k, int ind[],
+ double val[]), void *info, double tol, int rn[], int cn[])
+{ int head, i, j, jj, k, kk, ks, len, len2, next_j, ns, size;
+ int *cind, *rind, *cnt, *ptr, *list, *prev, *next;
+ double *cval, *rval, *big;
+ char *flag;
+ /* allocate working arrays */
+ cind = talloc(1+m, int);
+ cval = talloc(1+m, double);
+ rind = talloc(1+n, int);
+ rval = talloc(1+n, double);
+ cnt = ptr = talloc(1+m, int);
+ list = talloc(1+n, int);
+ prev = talloc(1+n, int);
+ next = talloc(1+n, int);
+ big = talloc(1+n, double);
+ flag = talloc(1+n, char);
+ /*--------------------------------------------------------------*/
+ /* build linked lists of columns having equal lengths */
+ /*--------------------------------------------------------------*/
+ /* ptr[len], 0 <= len <= m, is number of first column of length
+ * len;
+ * next[j], 1 <= j <= n, is number of next column having the same
+ * length as column j;
+ * big[j], 1 <= j <= n, is maximal magnitude of elements in j-th
+ * column */
+ for (len = 0; len <= m; len++)
+ ptr[len] = 0;
+ for (j = 1; j <= n; j++)
+ { /* get j-th column */
+ len = mat(info, -j, cind, cval);
+ xassert(0 <= len && len <= m);
+ /* add this column to beginning of list ptr[len] */
+ next[j] = ptr[len];
+ ptr[len] = j;
+ /* determine maximal magnitude of elements in this column */
+ big[j] = 0.0;
+ for (k = 1; k <= len; k++)
+ { if (big[j] < fabs(cval[k]))
+ big[j] = fabs(cval[k]);
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* build doubly linked list of columns ordered by decreasing */
+ /* column lengths */
+ /*--------------------------------------------------------------*/
+ /* head is number of first column in the list;
+ * prev[j], 1 <= j <= n, is number of column that precedes j-th
+ * column in the list;
+ * next[j], 1 <= j <= n, is number of column that follows j-th
+ * column in the list */
+ head = 0;
+ for (len = 0; len <= m; len++)
+ { /* walk thru list of columns of length len */
+ for (j = ptr[len]; j != 0; j = next_j)
+ { next_j = next[j];
+ /* add j-th column to beginning of the column list */
+ prev[j] = 0;
+ next[j] = head;
+ if (head != 0)
+ prev[head] = j;
+ head = j;
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* build initial singleton list */
+ /*--------------------------------------------------------------*/
+ /* there are used two list of columns:
+ * 1) doubly linked list of active columns, in which all columns
+ * are ordered by decreasing column lengths;
+ * 2) singleton list; an active column is included in this list
+ * if it has at least one row singleton in active submatrix */
+ /* flag[j], 1 <= j <= n, is a flag of j-th column:
+ * 0 j-th column is inactive;
+ * 1 j-th column is active;
+ * 2 j-th column is active and has row singleton(s) */
+ /* initially all columns are active */
+ for (j = 1; j <= n; j++)
+ flag[j] = 1;
+ /* initialize row counts and build initial singleton list */
+ /* cnt[i], 1 <= i <= m, is number of non-zeros, which i-th row
+ * has in active submatrix;
+ * ns is size of singleton list;
+ * list[1], ..., list[ns] are numbers of active columns included
+ * in the singleton list */
+ ns = 0;
+ for (i = 1; i <= m; i++)
+ { /* get i-th row */
+ len = cnt[i] = mat(info, +i, rind, rval);
+ xassert(0 <= len && len <= n);
+ if (len == 1)
+ { /* a[i,j] is row singleton */
+ j = rind[1];
+ xassert(1 <= j && j <= n);
+ if (flag[j] != 2)
+ { /* include j-th column in singleton list */
+ flag[j] = 2;
+ list[++ns] = j;
+ }
+ }
+ }
+ /*--------------------------------------------------------------*/
+ /* main loop */
+ /*--------------------------------------------------------------*/
+ size = 0; /* size of triangular part */
+ /* loop until active column list is non-empty, i.e. until the
+ * active submatrix has at least one column */
+ while (head != 0)
+ { if (ns == 0)
+ { /* singleton list is empty */
+ /* remove from the active submatrix a column of maximal
+ * length in the hope that some row singletons appear */
+ j = head;
+ len = mat(info, -j, cind, cval);
+ xassert(0 <= len && len <= m);
+ goto drop;
+ }
+ /* take column j from the singleton list */
+ j = list[ns--];
+ xassert(flag[j] == 2);
+ /* j-th column has at least one row singleton in the active
+ * submatrix; choose one having maximal magnitude */
+ len = mat(info, -j, cind, cval);
+ xassert(0 <= len && len <= m);
+ kk = 0;
+ for (k = 1; k <= len; k++)
+ { i = cind[k];
+ xassert(1 <= i && i <= m);
+ if (cnt[i] == 1)
+ { /* a[i,j] is row singleton */
+ if (kk == 0 || fabs(cval[kk]) < fabs(cval[k]))
+ kk = k;
+ }
+ }
+ xassert(kk > 0);
+ /* check magnitude of the row singleton chosen */
+ if (fabs(cval[kk]) < tol * big[j])
+ { /* all row singletons are too small in magnitude; drop j-th
+ * column */
+ goto drop;
+ }
+ /* row singleton a[i,j] is ok; add i-th row and j-th column to
+ * the triangular part */
+ size++;
+ rn[size] = cind[kk];
+ cn[size] = j;
+drop: /* remove j-th column from the active submatrix */
+ xassert(flag[j]);
+ flag[j] = 0;
+ if (prev[j] == 0)
+ head = next[j];
+ else
+ next[prev[j]] = next[j];
+ if (next[j] == 0)
+ ;
+ else
+ prev[next[j]] = prev[j];
+ /* decrease row counts */
+ for (k = 1; k <= len; k++)
+ { i = cind[k];
+ xassert(1 <= i && i <= m);
+ xassert(cnt[i] > 0);
+ cnt[i]--;
+ if (cnt[i] == 1)
+ { /* new singleton appeared in i-th row; determine number
+ * of corresponding column (it is the only active column
+ * in this row) */
+ len2 = mat(info, +i, rind, rval);
+ xassert(0 <= len2 && len2 <= n);
+ ks = 0;
+ for (kk = 1; kk <= len2; kk++)
+ { jj = rind[kk];
+ xassert(1 <= jj && jj <= n);
+ if (flag[jj])
+ { xassert(ks == 0);
+ ks = kk;
+ }
+ }
+ xassert(ks > 0);
+ /* a[i,jj] is new row singleton */
+ jj = rind[ks];
+ if (flag[jj] != 2)
+ { /* include jj-th column in the singleton list */
+ flag[jj] = 2;
+ list[++ns] = jj;
+ }
+ }
+ }
+ }
+ /* now all row counts should be zero */
+ for (i = 1; i <= m; i++)
+ xassert(cnt[i] == 0);
+ /* deallocate working arrays */
+ tfree(cind);
+ tfree(cval);
+ tfree(rind);
+ tfree(rval);
+ tfree(ptr);
+ tfree(list);
+ tfree(prev);
+ tfree(next);
+ tfree(big);
+ tfree(flag);
+ return size;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/triang.h b/test/monniaux/glpk-4.65/src/misc/triang.h
new file mode 100644
index 00000000..1e50d44d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/triang.h
@@ -0,0 +1,34 @@
+/* triang.h (find maximal triangular part of rectangular matrix) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef TRIANG_H
+#define TRIANG_H
+
+#define triang _glp_triang
+int triang(int m, int n, int (*mat)(void *info, int k, int ind[],
+ double val[]), void *info, double tol, int rn[], int cn[]);
+/* find maximal triangular part of rectangular matrix */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/wclique.c b/test/monniaux/glpk-4.65/src/misc/wclique.c
new file mode 100644
index 00000000..5daa69cf
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/wclique.c
@@ -0,0 +1,242 @@
+/* wclique.c (maximum weight clique, Ostergard's algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Two subroutines sub() and wclique() below are intended to find a
+* maximum weight clique in a given undirected graph. These subroutines
+* are slightly modified version of the program WCLIQUE developed by
+* Patric Ostergard <http://www.tcs.hut.fi/~pat/wclique.html> and based
+* on ideas from the article "P. R. J. Ostergard, A new algorithm for
+* the maximum-weight clique problem, submitted for publication", which
+* in turn is a generalization of the algorithm for unweighted graphs
+* presented in "P. R. J. Ostergard, A fast algorithm for the maximum
+* clique problem, submitted for publication".
+*
+* USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE.
+*
+* Changes were made by Andrew Makhorin <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "wclique.h"
+
+/***********************************************************************
+* NAME
+*
+* wclique - find maximum weight clique with Ostergard's algorithm
+*
+* SYNOPSIS
+*
+* #include "wclique.h"
+* int wclique(int n, const int w[], const unsigned char a[],
+* int ind[]);
+*
+* DESCRIPTION
+*
+* The routine wclique finds a maximum weight clique in an undirected
+* graph with Ostergard's algorithm.
+*
+* INPUT PARAMETERS
+*
+* n is the number of vertices, n > 0.
+*
+* w[i], i = 1,...,n, is a weight of vertex i.
+*
+* a[*] is the strict (without main diagonal) lower triangle of the
+* graph adjacency matrix in packed format.
+*
+* OUTPUT PARAMETER
+*
+* ind[k], k = 1,...,size, is the number of a vertex included in the
+* clique found, 1 <= ind[k] <= n, where size is the number of vertices
+* in the clique returned on exit.
+*
+* RETURNS
+*
+* The routine returns the clique size, i.e. the number of vertices in
+* the clique. */
+
+struct csa
+{ /* common storage area */
+ int n;
+ /* number of vertices */
+ const int *wt; /* int wt[0:n-1]; */
+ /* weights */
+ const unsigned char *a;
+ /* adjacency matrix (packed lower triangle without main diag.) */
+ int record;
+ /* weight of best clique */
+ int rec_level;
+ /* number of vertices in best clique */
+ int *rec; /* int rec[0:n-1]; */
+ /* best clique so far */
+ int *clique; /* int clique[0:n-1]; */
+ /* table for pruning */
+ int *set; /* int set[0:n-1]; */
+ /* current clique */
+};
+
+#define n (csa->n)
+#define wt (csa->wt)
+#define a (csa->a)
+#define record (csa->record)
+#define rec_level (csa->rec_level)
+#define rec (csa->rec)
+#define clique (csa->clique)
+#define set (csa->set)
+
+#if 0
+static int is_edge(struct csa *csa, int i, int j)
+{ /* if there is arc (i,j), the routine returns true; otherwise
+ * false; 0 <= i, j < n */
+ int k;
+ xassert(0 <= i && i < n);
+ xassert(0 <= j && j < n);
+ if (i == j) return 0;
+ if (i < j) k = i, i = j, j = k;
+ k = (i * (i - 1)) / 2 + j;
+ return a[k / CHAR_BIT] &
+ (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
+}
+#else
+#define is_edge(csa, i, j) ((i) == (j) ? 0 : \
+ (i) > (j) ? is_edge1(i, j) : is_edge1(j, i))
+#define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j))
+#define is_edge2(k) (a[(k) / CHAR_BIT] & \
+ (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT)))
+#endif
+
+static void sub(struct csa *csa, int ct, int table[], int level,
+ int weight, int l_weight)
+{ int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable;
+ newtable = xcalloc(n, sizeof(int));
+ if (ct <= 0)
+ { /* 0 or 1 elements left; include these */
+ if (ct == 0)
+ { set[level++] = table[0];
+ weight += l_weight;
+ }
+ if (weight > record)
+ { record = weight;
+ rec_level = level;
+ for (i = 0; i < level; i++) rec[i] = set[i];
+ }
+ goto done;
+ }
+ for (i = ct; i >= 0; i--)
+ { if ((level == 0) && (i < ct)) goto done;
+ k = table[i];
+ if ((level > 0) && (clique[k] <= (record - weight)))
+ goto done; /* prune */
+ set[level] = k;
+ curr_weight = weight + wt[k];
+ l_weight -= wt[k];
+ if (l_weight <= (record - curr_weight))
+ goto done; /* prune */
+ p1 = newtable;
+ p2 = table;
+ left_weight = 0;
+ while (p2 < table + i)
+ { j = *p2++;
+ if (is_edge(csa, j, k))
+ { *p1++ = j;
+ left_weight += wt[j];
+ }
+ }
+ if (left_weight <= (record - curr_weight)) continue;
+ sub(csa, p1 - newtable - 1, newtable, level + 1, curr_weight,
+ left_weight);
+ }
+done: xfree(newtable);
+ return;
+}
+
+int wclique(int n_, const int w[], const unsigned char a_[], int ind[])
+{ struct csa csa_, *csa = &csa_;
+ int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos;
+ double timer;
+ n = n_;
+ xassert(n > 0);
+ wt = &w[1];
+ a = a_;
+ record = 0;
+ rec_level = 0;
+ rec = &ind[1];
+ clique = xcalloc(n, sizeof(int));
+ set = xcalloc(n, sizeof(int));
+ used = xcalloc(n, sizeof(int));
+ nwt = xcalloc(n, sizeof(int));
+ pos = xcalloc(n, sizeof(int));
+ /* start timer */
+ timer = xtime();
+ /* order vertices */
+ for (i = 0; i < n; i++)
+ { nwt[i] = 0;
+ for (j = 0; j < n; j++)
+ if (is_edge(csa, i, j)) nwt[i] += wt[j];
+ }
+ for (i = 0; i < n; i++)
+ used[i] = 0;
+ for (i = n-1; i >= 0; i--)
+ { max_wt = -1;
+ max_nwt = -1;
+ for (j = 0; j < n; j++)
+ { if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt
+ && nwt[j] > max_nwt)))
+ { max_wt = wt[j];
+ max_nwt = nwt[j];
+ p = j;
+ }
+ }
+ pos[i] = p;
+ used[p] = 1;
+ for (j = 0; j < n; j++)
+ if ((!used[j]) && (j != p) && (is_edge(csa, p, j)))
+ nwt[j] -= wt[p];
+ }
+ /* main routine */
+ wth = 0;
+ for (i = 0; i < n; i++)
+ { wth += wt[pos[i]];
+ sub(csa, i, pos, 0, 0, wth);
+ clique[pos[i]] = record;
+ if (xdifftime(xtime(), timer) >= 5.0 - 0.001)
+ { /* print current record and reset timer */
+ xprintf("level = %d (%d); best = %d\n", i+1, n, record);
+ timer = xtime();
+ }
+ }
+ xfree(clique);
+ xfree(set);
+ xfree(used);
+ xfree(nwt);
+ xfree(pos);
+ /* return the solution found */
+ for (i = 1; i <= rec_level; i++) ind[i]++;
+ return rec_level;
+}
+
+#undef n
+#undef wt
+#undef a
+#undef record
+#undef rec_level
+#undef rec
+#undef clique
+#undef set
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/wclique.h b/test/monniaux/glpk-4.65/src/misc/wclique.h
new file mode 100644
index 00000000..d52dc805
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/wclique.h
@@ -0,0 +1,33 @@
+/* wclique.h (maximum weight clique, Ostergard's algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef WCLIQUE_H
+#define WCLIQUE_H
+
+#define wclique _glp_wclique
+int wclique(int n, const int w[], const unsigned char a[], int ind[]);
+/* find maximum weight clique with Ostergard's algorithm */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/wclique1.c b/test/monniaux/glpk-4.65/src/misc/wclique1.c
new file mode 100644
index 00000000..a3d89542
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/wclique1.c
@@ -0,0 +1,317 @@
+/* wclique1.c (maximum weight clique, greedy heuristic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "wclique1.h"
+
+/***********************************************************************
+* NAME
+*
+* wclique1 - find maximum weight clique with greedy heuristic
+*
+* SYNOPSIS
+*
+* #include "wclique1.h"
+* int wclique1(int n, const double w[],
+* int (*func)(void *info, int i, int ind[]), void *info, int c[]);
+*
+* DESCRIPTION
+*
+* The routine wclique1 implements a sequential greedy heuristic to
+* find maximum weight clique in a given (undirected) graph G = (V, E).
+*
+* The parameter n specifies the number of vertices |V| in the graph,
+* n >= 0.
+*
+* The array w specifies vertex weights in locations w[i], i = 1,...,n.
+* All weights must be non-negative.
+*
+* The formal routine func specifies the graph. For a given vertex i,
+* 1 <= i <= n, it stores indices of all vertices adjacent to vertex i
+* in locations ind[1], ..., ind[deg], where deg is the degree of
+* vertex i, 0 <= deg < n, returned on exit. Note that self-loops and
+* multiple edges are not allowed.
+*
+* The parameter info is a cookie passed to the routine func.
+*
+* On exit the routine wclique1 stores vertex indices included in
+* the clique found to locations c[1], ..., c[size], where size is the
+* clique size returned by the routine, 0 <= size <= n.
+*
+* RETURNS
+*
+* The routine wclique1 returns the size of the clique found. */
+
+struct vertex { int i; double cw; };
+
+static int CDECL fcmp(const void *xx, const void *yy)
+{ const struct vertex *x = xx, *y = yy;
+ if (x->cw > y->cw) return -1;
+ if (x->cw < y->cw) return +1;
+ return 0;
+}
+
+int wclique1(int n, const double w[],
+ int (*func)(void *info, int i, int ind[]), void *info, int c[])
+{ struct vertex *v_list;
+ int deg, c_size, d_size, i, j, k, kk, l, *ind, *c_list, *d_list,
+ size = 0;
+ double c_wght, d_wght, *sw, best = 0.0;
+ char *d_flag, *skip;
+ /* perform sanity checks */
+ xassert(n >= 0);
+ for (i = 1; i <= n; i++)
+ xassert(w[i] >= 0.0);
+ /* if the graph is empty, nothing to do */
+ if (n == 0) goto done;
+ /* allocate working arrays */
+ ind = xcalloc(1+n, sizeof(int));
+ v_list = xcalloc(1+n, sizeof(struct vertex));
+ c_list = xcalloc(1+n, sizeof(int));
+ d_list = xcalloc(1+n, sizeof(int));
+ d_flag = xcalloc(1+n, sizeof(char));
+ skip = xcalloc(1+n, sizeof(char));
+ sw = xcalloc(1+n, sizeof(double));
+ /* build the vertex list */
+ for (i = 1; i <= n; i++)
+ { v_list[i].i = i;
+ /* compute the cumulative weight of each vertex i, which is
+ * cw[i] = w[i] + sum{j : (i,j) in E} w[j] */
+ v_list[i].cw = w[i];
+ deg = func(info, i, ind);
+ xassert(0 <= deg && deg < n);
+ for (k = 1; k <= deg; k++)
+ { j = ind[k];
+ xassert(1 <= j && j <= n && j != i);
+ v_list[i].cw += w[j];
+ }
+ }
+ /* sort the vertex list to access vertices in descending order of
+ * cumulative weights */
+ qsort(&v_list[1], n, sizeof(struct vertex), fcmp);
+ /* initially all vertices are unmarked */
+ memset(&skip[1], 0, sizeof(char) * n);
+ /* clear flags of all vertices */
+ memset(&d_flag[1], 0, sizeof(char) * n);
+ /* look through all vertices of the graph */
+ for (l = 1; l <= n; l++)
+ { /* take vertex i */
+ i = v_list[l].i;
+ /* if this vertex was already included in one of previosuly
+ * constructed cliques, skip it */
+ if (skip[i]) continue;
+ /* use vertex i as the initial clique vertex */
+ c_size = 1; /* size of current clique */
+ c_list[1] = i; /* list of vertices in current clique */
+ c_wght = w[i]; /* weight of current clique */
+ /* determine the candidate set D = { j : (i,j) in E } */
+ d_size = func(info, i, d_list);
+ xassert(0 <= d_size && d_size < n);
+ d_wght = 0.0; /* weight of set D */
+ for (k = 1; k <= d_size; k++)
+ { j = d_list[k];
+ xassert(1 <= j && j <= n && j != i);
+ xassert(!d_flag[j]);
+ d_flag[j] = 1;
+ d_wght += w[j];
+ }
+ /* check an upper bound to the final clique weight */
+ if (c_wght + d_wght < best + 1e-5 * (1.0 + fabs(best)))
+ { /* skip constructing the current clique */
+ goto next;
+ }
+ /* compute the summary weight of each vertex i in D, which is
+ * sw[i] = w[i] + sum{j in D and (i,j) in E} w[j] */
+ for (k = 1; k <= d_size; k++)
+ { i = d_list[k];
+ sw[i] = w[i];
+ /* consider vertices adjacent to vertex i */
+ deg = func(info, i, ind);
+ xassert(0 <= deg && deg < n);
+ for (kk = 1; kk <= deg; kk++)
+ { j = ind[kk];
+ xassert(1 <= j && j <= n && j != i);
+ if (d_flag[j]) sw[i] += w[j];
+ }
+ }
+ /* grow the current clique by adding vertices from D */
+ while (d_size > 0)
+ { /* check an upper bound to the final clique weight */
+ if (c_wght + d_wght < best + 1e-5 * (1.0 + fabs(best)))
+ { /* skip constructing the current clique */
+ goto next;
+ }
+ /* choose vertex i in D having maximal summary weight */
+ i = d_list[1];
+ for (k = 2; k <= d_size; k++)
+ { j = d_list[k];
+ if (sw[i] < sw[j]) i = j;
+ }
+ /* include vertex i in the current clique */
+ c_size++;
+ c_list[c_size] = i;
+ c_wght += w[i];
+ /* remove all vertices not adjacent to vertex i, including
+ * vertex i itself, from the candidate set D */
+ deg = func(info, i, ind);
+ xassert(0 <= deg && deg < n);
+ for (k = 1; k <= deg; k++)
+ { j = ind[k];
+ xassert(1 <= j && j <= n && j != i);
+ /* vertex j is adjacent to vertex i */
+ if (d_flag[j])
+ { xassert(d_flag[j] == 1);
+ /* mark vertex j to keep it in D */
+ d_flag[j] = 2;
+ }
+ }
+ kk = d_size, d_size = 0;
+ for (k = 1; k <= kk; k++)
+ { j = d_list[k];
+ if (d_flag[j] == 1)
+ { /* remove vertex j from D */
+ d_flag[j] = 0;
+ d_wght -= w[j];
+ }
+ else if (d_flag[j] == 2)
+ { /* keep vertex j in D */
+ d_list[++d_size] = j;
+ d_flag[j] = 1;
+ }
+ else
+ xassert(d_flag != d_flag);
+ }
+ }
+ /* the current clique has been completely constructed */
+ if (best < c_wght)
+ { best = c_wght;
+ size = c_size;
+ xassert(1 <= size && size <= n);
+ memcpy(&c[1], &c_list[1], size * sizeof(int));
+ }
+next: /* mark the current clique vertices in order not to use them
+ * as initial vertices anymore */
+ for (k = 1; k <= c_size; k++)
+ skip[c_list[k]] = 1;
+ /* set D can be non-empty, so clean up vertex flags */
+ for (k = 1; k <= d_size; k++)
+ d_flag[d_list[k]] = 0;
+ }
+ /* free working arrays */
+ xfree(ind);
+ xfree(v_list);
+ xfree(c_list);
+ xfree(d_list);
+ xfree(d_flag);
+ xfree(skip);
+ xfree(sw);
+done: /* return to the calling program */
+ return size;
+}
+
+/**********************************************************************/
+
+#ifdef GLP_TEST
+#include "glpk.h"
+#include "rng.h"
+
+typedef struct { double w; } v_data;
+
+#define weight(v) (((v_data *)((v)->data))->w)
+
+glp_graph *G;
+
+char *flag;
+
+int func(void *info, int i, int ind[])
+{ glp_arc *e;
+ int j, k, deg = 0;
+ xassert(info == NULL);
+ xassert(1 <= i && i <= G->nv);
+ /* look through incoming arcs */
+ for (e = G->v[i]->in; e != NULL; e = e->h_next)
+ { j = e->tail->i; /* j->i */
+ if (j != i && !flag[j]) ind[++deg] = j, flag[j] = 1;
+ }
+ /* look through outgoing arcs */
+ for (e = G->v[i]->out; e != NULL; e = e->t_next)
+ { j = e->head->i; /* i->j */
+ if (j != i && !flag[j]) ind[++deg] = j, flag[j] = 1;
+ }
+ /* clear the flag array */
+ xassert(deg < G->nv);
+ for (k = 1; k <= deg; k++) flag[ind[k]] = 0;
+ return deg;
+}
+
+int main(int argc, char *argv[])
+{ RNG *rand;
+ int i, k, kk, size, *c, *ind, deg;
+ double *w, sum, t;
+ /* read graph in DIMACS format */
+ G = glp_create_graph(sizeof(v_data), 0);
+ xassert(argc == 2);
+ xassert(glp_read_ccdata(G, offsetof(v_data, w), argv[1]) == 0);
+ /* print the number of connected components */
+ xprintf("nc = %d\n", glp_weak_comp(G, -1));
+ /* assign random weights unformly distributed in [1,100] */
+ w = xcalloc(1+G->nv, sizeof(double));
+ rand = rng_create_rand();
+ for (i = 1; i <= G->nv; i++)
+#if 0
+ w[i] = weight(G->v[i]) = 1.0;
+#else
+ w[i] = weight(G->v[i]) = rng_unif_rand(rand, 100) + 1;
+#endif
+ /* write graph in DIMACS format */
+ xassert(glp_write_ccdata(G, offsetof(v_data, w), "graph") == 0);
+ /* find maximum weight clique */
+ c = xcalloc(1+G->nv, sizeof(int));
+ flag = xcalloc(1+G->nv, sizeof(char));
+ memset(&flag[1], 0, G->nv);
+ t = xtime();
+ size = wclique1(G->nv, w, func, NULL, c);
+ xprintf("Time used: %.1f s\n", xdifftime(xtime(), t));
+ /* check the clique found */
+ ind = xcalloc(1+G->nv, sizeof(int));
+ for (k = 1; k <= size; k++)
+ { i = c[k];
+ deg = func(NULL, i, ind);
+ for (kk = 1; kk <= size; kk++)
+ flag[c[kk]] = 1;
+ flag[i] = 0;
+ for (kk = 1; kk <= deg; kk++)
+ flag[ind[kk]] = 0;
+ for (kk = 1; kk <= size; kk++)
+ xassert(flag[c[kk]] == 0);
+ }
+ /* compute the clique weight */
+ sum = 0.0;
+ for (i = 1; i <= size; i++)
+ sum += w[c[i]];
+ xprintf("size = %d; sum = %g\n", size, sum);
+ return 0;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/misc/wclique1.h b/test/monniaux/glpk-4.65/src/misc/wclique1.h
new file mode 100644
index 00000000..588f3257
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/misc/wclique1.h
@@ -0,0 +1,34 @@
+/* wclique1.h (maximum weight clique, greedy heuristic) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2012-2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef WCLIQUE1_H
+#define WCLIQUE1_H
+
+#define wclique1 _glp_wclique1
+int wclique1(int n, const double w[],
+ int (*func)(void *info, int i, int ind[]), void *info, int c[]);
+/* find maximum weight clique with greedy heuristic */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl.h b/test/monniaux/glpk-4.65/src/mpl/mpl.h
new file mode 100644
index 00000000..ddd31543
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl.h
@@ -0,0 +1,2598 @@
+/* mpl.h (GNU MathProg translator) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MPL_H
+#define MPL_H
+
+#include "avl.h"
+#include "dmp.h"
+#include "env.h"
+#include "misc.h"
+#include "rng.h"
+
+#if 0 /* 22/I-2013 */
+typedef struct MPL MPL;
+#else
+typedef struct glp_tran MPL;
+#endif
+typedef char STRING;
+typedef struct SYMBOL SYMBOL;
+typedef struct TUPLE TUPLE;
+typedef struct ARRAY ELEMSET;
+typedef struct ELEMVAR ELEMVAR;
+typedef struct FORMULA FORMULA;
+typedef struct ELEMCON ELEMCON;
+typedef union VALUE VALUE;
+typedef struct ARRAY ARRAY;
+typedef struct MEMBER MEMBER;
+#if 1
+/* many C compilers have DOMAIN declared in <math.h> :( */
+#undef DOMAIN
+#define DOMAIN DOMAIN1
+#endif
+typedef struct DOMAIN DOMAIN;
+typedef struct DOMAIN_BLOCK DOMAIN_BLOCK;
+typedef struct DOMAIN_SLOT DOMAIN_SLOT;
+typedef struct SET SET;
+typedef struct WITHIN WITHIN;
+typedef struct GADGET GADGET;
+typedef struct PARAMETER PARAMETER;
+typedef struct CONDITION CONDITION;
+typedef struct VARIABLE VARIABLE;
+typedef struct CONSTRAINT CONSTRAINT;
+typedef struct TABLE TABLE;
+typedef struct TABARG TABARG;
+typedef struct TABFLD TABFLD;
+typedef struct TABIN TABIN;
+typedef struct TABOUT TABOUT;
+typedef struct TABDCA TABDCA;
+typedef union OPERANDS OPERANDS;
+typedef struct ARG_LIST ARG_LIST;
+typedef struct CODE CODE;
+typedef struct CHECK CHECK;
+typedef struct DISPLAY DISPLAY;
+typedef struct DISPLAY1 DISPLAY1;
+typedef struct PRINTF PRINTF;
+typedef struct PRINTF1 PRINTF1;
+typedef struct FOR FOR;
+typedef struct STATEMENT STATEMENT;
+typedef struct TUPLE SLICE;
+
+/**********************************************************************/
+/* * * TRANSLATOR DATABASE * * */
+/**********************************************************************/
+
+#define A_BINARY 101 /* something binary */
+#define A_CHECK 102 /* check statement */
+#define A_CONSTRAINT 103 /* model constraint */
+#define A_DISPLAY 104 /* display statement */
+#define A_ELEMCON 105 /* elemental constraint/objective */
+#define A_ELEMSET 106 /* elemental set */
+#define A_ELEMVAR 107 /* elemental variable */
+#define A_EXPRESSION 108 /* expression */
+#define A_FOR 109 /* for statement */
+#define A_FORMULA 110 /* formula */
+#define A_INDEX 111 /* dummy index */
+#define A_INPUT 112 /* input table */
+#define A_INTEGER 113 /* something integer */
+#define A_LOGICAL 114 /* something logical */
+#define A_MAXIMIZE 115 /* objective has to be maximized */
+#define A_MINIMIZE 116 /* objective has to be minimized */
+#define A_NONE 117 /* nothing */
+#define A_NUMERIC 118 /* something numeric */
+#define A_OUTPUT 119 /* output table */
+#define A_PARAMETER 120 /* model parameter */
+#define A_PRINTF 121 /* printf statement */
+#define A_SET 122 /* model set */
+#define A_SOLVE 123 /* solve statement */
+#define A_SYMBOLIC 124 /* something symbolic */
+#define A_TABLE 125 /* data table */
+#define A_TUPLE 126 /* n-tuple */
+#define A_VARIABLE 127 /* model variable */
+
+#define MAX_LENGTH 100
+/* maximal length of any symbolic value (this includes symbolic names,
+ numeric and string literals, and all symbolic values that may appear
+ during the evaluation phase) */
+
+#define CONTEXT_SIZE 60
+/* size of the context queue, in characters */
+
+#define OUTBUF_SIZE 1024
+/* size of the output buffer, in characters */
+
+#if 0 /* 22/I-2013 */
+struct MPL
+#else
+struct glp_tran
+#endif
+{ /* translator database */
+ /*--------------------------------------------------------------*/
+ /* scanning segment */
+ int line;
+ /* number of the current text line */
+ int c;
+ /* the current character or EOF */
+ int token;
+ /* the current token: */
+#define T_EOF 201 /* end of file */
+#define T_NAME 202 /* symbolic name (model section only) */
+#define T_SYMBOL 203 /* symbol (data section only) */
+#define T_NUMBER 204 /* numeric literal */
+#define T_STRING 205 /* string literal */
+#define T_AND 206 /* and && */
+#define T_BY 207 /* by */
+#define T_CROSS 208 /* cross */
+#define T_DIFF 209 /* diff */
+#define T_DIV 210 /* div */
+#define T_ELSE 211 /* else */
+#define T_IF 212 /* if */
+#define T_IN 213 /* in */
+#define T_INFINITY 214 /* Infinity */
+#define T_INTER 215 /* inter */
+#define T_LESS 216 /* less */
+#define T_MOD 217 /* mod */
+#define T_NOT 218 /* not ! */
+#define T_OR 219 /* or || */
+#define T_SPTP 220 /* s.t. */
+#define T_SYMDIFF 221 /* symdiff */
+#define T_THEN 222 /* then */
+#define T_UNION 223 /* union */
+#define T_WITHIN 224 /* within */
+#define T_PLUS 225 /* + */
+#define T_MINUS 226 /* - */
+#define T_ASTERISK 227 /* * */
+#define T_SLASH 228 /* / */
+#define T_POWER 229 /* ^ ** */
+#define T_LT 230 /* < */
+#define T_LE 231 /* <= */
+#define T_EQ 232 /* = == */
+#define T_GE 233 /* >= */
+#define T_GT 234 /* > */
+#define T_NE 235 /* <> != */
+#define T_CONCAT 236 /* & */
+#define T_BAR 237 /* | */
+#define T_POINT 238 /* . */
+#define T_COMMA 239 /* , */
+#define T_COLON 240 /* : */
+#define T_SEMICOLON 241 /* ; */
+#define T_ASSIGN 242 /* := */
+#define T_DOTS 243 /* .. */
+#define T_LEFT 244 /* ( */
+#define T_RIGHT 245 /* ) */
+#define T_LBRACKET 246 /* [ */
+#define T_RBRACKET 247 /* ] */
+#define T_LBRACE 248 /* { */
+#define T_RBRACE 249 /* } */
+#define T_APPEND 250 /* >> */
+#define T_TILDE 251 /* ~ */
+#define T_INPUT 252 /* <- */
+ int imlen;
+ /* length of the current token */
+ char *image; /* char image[MAX_LENGTH+1]; */
+ /* image of the current token */
+ double value;
+ /* value of the current token (for T_NUMBER only) */
+ int b_token;
+ /* the previous token */
+ int b_imlen;
+ /* length of the previous token */
+ char *b_image; /* char b_image[MAX_LENGTH+1]; */
+ /* image of the previous token */
+ double b_value;
+ /* value of the previous token (if token is T_NUMBER) */
+ int f_dots;
+ /* if this flag is set, the next token should be recognized as
+ T_DOTS, not as T_POINT */
+ int f_scan;
+ /* if this flag is set, the next token is already scanned */
+ int f_token;
+ /* the next token */
+ int f_imlen;
+ /* length of the next token */
+ char *f_image; /* char f_image[MAX_LENGTH+1]; */
+ /* image of the next token */
+ double f_value;
+ /* value of the next token (if token is T_NUMBER) */
+ char *context; /* char context[CONTEXT_SIZE]; */
+ /* context circular queue (not null-terminated!) */
+ int c_ptr;
+ /* pointer to the current position in the context queue */
+ int flag_d;
+ /* if this flag is set, the data section is being processed */
+ /*--------------------------------------------------------------*/
+ /* translating segment */
+ DMP *pool;
+ /* memory pool used to allocate all data instances created during
+ the translation phase */
+ AVL *tree;
+ /* symbolic name table:
+ node.type = A_INDEX => node.link -> DOMAIN_SLOT
+ node.type = A_SET => node.link -> SET
+ node.type = A_PARAMETER => node.link -> PARAMETER
+ node.type = A_VARIABLE => node.link -> VARIABLE
+ node.type = A_CONSTRANT => node.link -> CONSTRAINT */
+ STATEMENT *model;
+ /* linked list of model statements in the original order */
+ int flag_x;
+ /* if this flag is set, the current token being left parenthesis
+ begins a slice that allows recognizing any undeclared symbolic
+ names as dummy indices; this flag is automatically reset once
+ the next token has been scanned */
+ int as_within;
+ /* the warning "in understood as within" has been issued */
+ int as_in;
+ /* the warning "within understood as in" has been issued */
+ int as_binary;
+ /* the warning "logical understood as binary" has been issued */
+ int flag_s;
+ /* if this flag is set, the solve statement has been parsed */
+ /*--------------------------------------------------------------*/
+ /* common segment */
+ DMP *strings;
+ /* memory pool to allocate STRING data structures */
+ DMP *symbols;
+ /* memory pool to allocate SYMBOL data structures */
+ DMP *tuples;
+ /* memory pool to allocate TUPLE data structures */
+ DMP *arrays;
+ /* memory pool to allocate ARRAY data structures */
+ DMP *members;
+ /* memory pool to allocate MEMBER data structures */
+ DMP *elemvars;
+ /* memory pool to allocate ELEMVAR data structures */
+ DMP *formulae;
+ /* memory pool to allocate FORMULA data structures */
+ DMP *elemcons;
+ /* memory pool to allocate ELEMCON data structures */
+ ARRAY *a_list;
+ /* linked list of all arrays in the database */
+ char *sym_buf; /* char sym_buf[255+1]; */
+ /* working buffer used by the routine format_symbol */
+ char *tup_buf; /* char tup_buf[255+1]; */
+ /* working buffer used by the routine format_tuple */
+ /*--------------------------------------------------------------*/
+ /* generating/postsolving segment */
+ RNG *rand;
+ /* pseudo-random number generator */
+ int flag_p;
+ /* if this flag is set, the postsolving phase is in effect */
+ STATEMENT *stmt;
+ /* model statement being currently executed */
+ TABDCA *dca;
+ /* pointer to table driver communication area for table statement
+ currently executed */
+ int m;
+ /* number of rows in the problem, m >= 0 */
+ int n;
+ /* number of columns in the problem, n >= 0 */
+ ELEMCON **row; /* ELEMCON *row[1+m]; */
+ /* row[0] is not used;
+ row[i] is elemental constraint or objective, which corresponds
+ to i-th row of the problem, 1 <= i <= m */
+ ELEMVAR **col; /* ELEMVAR *col[1+n]; */
+ /* col[0] is not used;
+ col[j] is elemental variable, which corresponds to j-th column
+ of the problem, 1 <= j <= n */
+ /*--------------------------------------------------------------*/
+ /* input/output segment */
+ glp_file *in_fp;
+ /* stream assigned to the input text file */
+ char *in_file;
+ /* name of the input text file */
+ glp_file *out_fp;
+ /* stream assigned to the output text file used to write all data
+ produced by display and printf statements; NULL means the data
+ should be sent to stdout via the routine xprintf */
+ char *out_file;
+ /* name of the output text file */
+#if 0 /* 08/XI-2009 */
+ char *out_buf; /* char out_buf[OUTBUF_SIZE] */
+ /* buffer to accumulate output data */
+ int out_cnt;
+ /* count of data bytes stored in the output buffer */
+#endif
+ glp_file *prt_fp;
+ /* stream assigned to the print text file; may be NULL */
+ char *prt_file;
+ /* name of the output print file */
+ /*--------------------------------------------------------------*/
+ /* solver interface segment */
+ jmp_buf jump;
+ /* jump address for non-local go to in case of error */
+ int phase;
+ /* phase of processing:
+ 0 - database is being or has been initialized
+ 1 - model section is being or has been read
+ 2 - data section is being or has been read
+ 3 - model is being or has been generated/postsolved
+ 4 - model processing error has occurred */
+ char *mod_file;
+ /* name of the input text file, which contains model section */
+ char *mpl_buf; /* char mpl_buf[255+1]; */
+ /* working buffer used by some interface routines */
+};
+
+/**********************************************************************/
+/* * * PROCESSING MODEL SECTION * * */
+/**********************************************************************/
+
+#define alloc(type) ((type *)dmp_get_atomv(mpl->pool, sizeof(type)))
+/* allocate atom of given type */
+
+#define enter_context _glp_mpl_enter_context
+void enter_context(MPL *mpl);
+/* enter current token into context queue */
+
+#define print_context _glp_mpl_print_context
+void print_context(MPL *mpl);
+/* print current content of context queue */
+
+#define get_char _glp_mpl_get_char
+void get_char(MPL *mpl);
+/* scan next character from input text file */
+
+#define append_char _glp_mpl_append_char
+void append_char(MPL *mpl);
+/* append character to current token */
+
+#define get_token _glp_mpl_get_token
+void get_token(MPL *mpl);
+/* scan next token from input text file */
+
+#define unget_token _glp_mpl_unget_token
+void unget_token(MPL *mpl);
+/* return current token back to input stream */
+
+#define is_keyword _glp_mpl_is_keyword
+int is_keyword(MPL *mpl, char *keyword);
+/* check if current token is given non-reserved keyword */
+
+#define is_reserved _glp_mpl_is_reserved
+int is_reserved(MPL *mpl);
+/* check if current token is reserved keyword */
+
+#define make_code _glp_mpl_make_code
+CODE *make_code(MPL *mpl, int op, OPERANDS *arg, int type, int dim);
+/* generate pseudo-code (basic routine) */
+
+#define make_unary _glp_mpl_make_unary
+CODE *make_unary(MPL *mpl, int op, CODE *x, int type, int dim);
+/* generate pseudo-code for unary operation */
+
+#define make_binary _glp_mpl_make_binary
+CODE *make_binary(MPL *mpl, int op, CODE *x, CODE *y, int type,
+ int dim);
+/* generate pseudo-code for binary operation */
+
+#define make_ternary _glp_mpl_make_ternary
+CODE *make_ternary(MPL *mpl, int op, CODE *x, CODE *y, CODE *z,
+ int type, int dim);
+/* generate pseudo-code for ternary operation */
+
+#define numeric_literal _glp_mpl_numeric_literal
+CODE *numeric_literal(MPL *mpl);
+/* parse reference to numeric literal */
+
+#define string_literal _glp_mpl_string_literal
+CODE *string_literal(MPL *mpl);
+/* parse reference to string literal */
+
+#define create_arg_list _glp_mpl_create_arg_list
+ARG_LIST *create_arg_list(MPL *mpl);
+/* create empty operands list */
+
+#define expand_arg_list _glp_mpl_expand_arg_list
+ARG_LIST *expand_arg_list(MPL *mpl, ARG_LIST *list, CODE *x);
+/* append operand to operands list */
+
+#define arg_list_len _glp_mpl_arg_list_len
+int arg_list_len(MPL *mpl, ARG_LIST *list);
+/* determine length of operands list */
+
+#define subscript_list _glp_mpl_subscript_list
+ARG_LIST *subscript_list(MPL *mpl);
+/* parse subscript list */
+
+#define object_reference _glp_mpl_object_reference
+CODE *object_reference(MPL *mpl);
+/* parse reference to named object */
+
+#define numeric_argument _glp_mpl_numeric_argument
+CODE *numeric_argument(MPL *mpl, char *func);
+/* parse argument passed to built-in function */
+
+#define symbolic_argument _glp_mpl_symbolic_argument
+CODE *symbolic_argument(MPL *mpl, char *func);
+
+#define elemset_argument _glp_mpl_elemset_argument
+CODE *elemset_argument(MPL *mpl, char *func);
+
+#define function_reference _glp_mpl_function_reference
+CODE *function_reference(MPL *mpl);
+/* parse reference to built-in function */
+
+#define create_domain _glp_mpl_create_domain
+DOMAIN *create_domain(MPL *mpl);
+/* create empty domain */
+
+#define create_block _glp_mpl_create_block
+DOMAIN_BLOCK *create_block(MPL *mpl);
+/* create empty domain block */
+
+#define append_block _glp_mpl_append_block
+void append_block(MPL *mpl, DOMAIN *domain, DOMAIN_BLOCK *block);
+/* append domain block to specified domain */
+
+#define append_slot _glp_mpl_append_slot
+DOMAIN_SLOT *append_slot(MPL *mpl, DOMAIN_BLOCK *block, char *name,
+ CODE *code);
+/* create and append new slot to domain block */
+
+#define expression_list _glp_mpl_expression_list
+CODE *expression_list(MPL *mpl);
+/* parse expression list */
+
+#define literal_set _glp_mpl_literal_set
+CODE *literal_set(MPL *mpl, CODE *code);
+/* parse literal set */
+
+#define indexing_expression _glp_mpl_indexing_expression
+DOMAIN *indexing_expression(MPL *mpl);
+/* parse indexing expression */
+
+#define close_scope _glp_mpl_close_scope
+void close_scope(MPL *mpl, DOMAIN *domain);
+/* close scope of indexing expression */
+
+#define iterated_expression _glp_mpl_iterated_expression
+CODE *iterated_expression(MPL *mpl);
+/* parse iterated expression */
+
+#define domain_arity _glp_mpl_domain_arity
+int domain_arity(MPL *mpl, DOMAIN *domain);
+/* determine arity of domain */
+
+#define set_expression _glp_mpl_set_expression
+CODE *set_expression(MPL *mpl);
+/* parse set expression */
+
+#define branched_expression _glp_mpl_branched_expression
+CODE *branched_expression(MPL *mpl);
+/* parse conditional expression */
+
+#define primary_expression _glp_mpl_primary_expression
+CODE *primary_expression(MPL *mpl);
+/* parse primary expression */
+
+#define error_preceding _glp_mpl_error_preceding
+void error_preceding(MPL *mpl, char *opstr);
+/* raise error if preceding operand has wrong type */
+
+#define error_following _glp_mpl_error_following
+void error_following(MPL *mpl, char *opstr);
+/* raise error if following operand has wrong type */
+
+#define error_dimension _glp_mpl_error_dimension
+void error_dimension(MPL *mpl, char *opstr, int dim1, int dim2);
+/* raise error if operands have different dimension */
+
+#define expression_0 _glp_mpl_expression_0
+CODE *expression_0(MPL *mpl);
+/* parse expression of level 0 */
+
+#define expression_1 _glp_mpl_expression_1
+CODE *expression_1(MPL *mpl);
+/* parse expression of level 1 */
+
+#define expression_2 _glp_mpl_expression_2
+CODE *expression_2(MPL *mpl);
+/* parse expression of level 2 */
+
+#define expression_3 _glp_mpl_expression_3
+CODE *expression_3(MPL *mpl);
+/* parse expression of level 3 */
+
+#define expression_4 _glp_mpl_expression_4
+CODE *expression_4(MPL *mpl);
+/* parse expression of level 4 */
+
+#define expression_5 _glp_mpl_expression_5
+CODE *expression_5(MPL *mpl);
+/* parse expression of level 5 */
+
+#define expression_6 _glp_mpl_expression_6
+CODE *expression_6(MPL *mpl);
+/* parse expression of level 6 */
+
+#define expression_7 _glp_mpl_expression_7
+CODE *expression_7(MPL *mpl);
+/* parse expression of level 7 */
+
+#define expression_8 _glp_mpl_expression_8
+CODE *expression_8(MPL *mpl);
+/* parse expression of level 8 */
+
+#define expression_9 _glp_mpl_expression_9
+CODE *expression_9(MPL *mpl);
+/* parse expression of level 9 */
+
+#define expression_10 _glp_mpl_expression_10
+CODE *expression_10(MPL *mpl);
+/* parse expression of level 10 */
+
+#define expression_11 _glp_mpl_expression_11
+CODE *expression_11(MPL *mpl);
+/* parse expression of level 11 */
+
+#define expression_12 _glp_mpl_expression_12
+CODE *expression_12(MPL *mpl);
+/* parse expression of level 12 */
+
+#define expression_13 _glp_mpl_expression_13
+CODE *expression_13(MPL *mpl);
+/* parse expression of level 13 */
+
+#define set_statement _glp_mpl_set_statement
+SET *set_statement(MPL *mpl);
+/* parse set statement */
+
+#define parameter_statement _glp_mpl_parameter_statement
+PARAMETER *parameter_statement(MPL *mpl);
+/* parse parameter statement */
+
+#define variable_statement _glp_mpl_variable_statement
+VARIABLE *variable_statement(MPL *mpl);
+/* parse variable statement */
+
+#define constraint_statement _glp_mpl_constraint_statement
+CONSTRAINT *constraint_statement(MPL *mpl);
+/* parse constraint statement */
+
+#define objective_statement _glp_mpl_objective_statement
+CONSTRAINT *objective_statement(MPL *mpl);
+/* parse objective statement */
+
+#define table_statement _glp_mpl_table_statement
+TABLE *table_statement(MPL *mpl);
+/* parse table statement */
+
+#define solve_statement _glp_mpl_solve_statement
+void *solve_statement(MPL *mpl);
+/* parse solve statement */
+
+#define check_statement _glp_mpl_check_statement
+CHECK *check_statement(MPL *mpl);
+/* parse check statement */
+
+#define display_statement _glp_mpl_display_statement
+DISPLAY *display_statement(MPL *mpl);
+/* parse display statement */
+
+#define printf_statement _glp_mpl_printf_statement
+PRINTF *printf_statement(MPL *mpl);
+/* parse printf statement */
+
+#define for_statement _glp_mpl_for_statement
+FOR *for_statement(MPL *mpl);
+/* parse for statement */
+
+#define end_statement _glp_mpl_end_statement
+void end_statement(MPL *mpl);
+/* parse end statement */
+
+#define simple_statement _glp_mpl_simple_statement
+STATEMENT *simple_statement(MPL *mpl, int spec);
+/* parse simple statement */
+
+#define model_section _glp_mpl_model_section
+void model_section(MPL *mpl);
+/* parse model section */
+
+/**********************************************************************/
+/* * * PROCESSING DATA SECTION * * */
+/**********************************************************************/
+
+#if 2 + 2 == 5
+struct SLICE /* see TUPLE */
+{ /* component of slice; the slice itself is associated with its
+ first component; slices are similar to n-tuples with exception
+ that some slice components (which are indicated by asterisks)
+ don't refer to any symbols */
+ SYMBOL *sym;
+ /* symbol, which this component refers to; can be NULL */
+ SLICE *next;
+ /* the next component of slice */
+};
+#endif
+
+#define create_slice _glp_mpl_create_slice
+SLICE *create_slice(MPL *mpl);
+/* create slice */
+
+#define expand_slice _glp_mpl_expand_slice
+SLICE *expand_slice
+( MPL *mpl,
+ SLICE *slice, /* destroyed */
+ SYMBOL *sym /* destroyed */
+);
+/* append new component to slice */
+
+#define slice_dimen _glp_mpl_slice_dimen
+int slice_dimen
+( MPL *mpl,
+ SLICE *slice /* not changed */
+);
+/* determine dimension of slice */
+
+#define slice_arity _glp_mpl_slice_arity
+int slice_arity
+( MPL *mpl,
+ SLICE *slice /* not changed */
+);
+/* determine arity of slice */
+
+#define fake_slice _glp_mpl_fake_slice
+SLICE *fake_slice(MPL *mpl, int dim);
+/* create fake slice of all asterisks */
+
+#define delete_slice _glp_mpl_delete_slice
+void delete_slice
+( MPL *mpl,
+ SLICE *slice /* destroyed */
+);
+/* delete slice */
+
+#define is_number _glp_mpl_is_number
+int is_number(MPL *mpl);
+/* check if current token is number */
+
+#define is_symbol _glp_mpl_is_symbol
+int is_symbol(MPL *mpl);
+/* check if current token is symbol */
+
+#define is_literal _glp_mpl_is_literal
+int is_literal(MPL *mpl, char *literal);
+/* check if current token is given symbolic literal */
+
+#define read_number _glp_mpl_read_number
+double read_number(MPL *mpl);
+/* read number */
+
+#define read_symbol _glp_mpl_read_symbol
+SYMBOL *read_symbol(MPL *mpl);
+/* read symbol */
+
+#define read_slice _glp_mpl_read_slice
+SLICE *read_slice
+( MPL *mpl,
+ char *name, /* not changed */
+ int dim
+);
+/* read slice */
+
+#define select_set _glp_mpl_select_set
+SET *select_set
+( MPL *mpl,
+ char *name /* not changed */
+);
+/* select set to saturate it with elemental sets */
+
+#define simple_format _glp_mpl_simple_format
+void simple_format
+( MPL *mpl,
+ SET *set, /* not changed */
+ MEMBER *memb, /* modified */
+ SLICE *slice /* not changed */
+);
+/* read set data block in simple format */
+
+#define matrix_format _glp_mpl_matrix_format
+void matrix_format
+( MPL *mpl,
+ SET *set, /* not changed */
+ MEMBER *memb, /* modified */
+ SLICE *slice, /* not changed */
+ int tr
+);
+/* read set data block in matrix format */
+
+#define set_data _glp_mpl_set_data
+void set_data(MPL *mpl);
+/* read set data */
+
+#define select_parameter _glp_mpl_select_parameter
+PARAMETER *select_parameter
+( MPL *mpl,
+ char *name /* not changed */
+);
+/* select parameter to saturate it with data */
+
+#define set_default _glp_mpl_set_default
+void set_default
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SYMBOL *altval /* destroyed */
+);
+/* set default parameter value */
+
+#define read_value _glp_mpl_read_value
+MEMBER *read_value
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* destroyed */
+);
+/* read value and assign it to parameter member */
+
+#define plain_format _glp_mpl_plain_format
+void plain_format
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SLICE *slice /* not changed */
+);
+/* read parameter data block in plain format */
+
+#define tabular_format _glp_mpl_tabular_format
+void tabular_format
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SLICE *slice, /* not changed */
+ int tr
+);
+/* read parameter data block in tabular format */
+
+#define tabbing_format _glp_mpl_tabbing_format
+void tabbing_format
+( MPL *mpl,
+ SYMBOL *altval /* not changed */
+);
+/* read parameter data block in tabbing format */
+
+#define parameter_data _glp_mpl_parameter_data
+void parameter_data(MPL *mpl);
+/* read parameter data */
+
+#define data_section _glp_mpl_data_section
+void data_section(MPL *mpl);
+/* read data section */
+
+/**********************************************************************/
+/* * * FLOATING-POINT NUMBERS * * */
+/**********************************************************************/
+
+#define fp_add _glp_mpl_fp_add
+double fp_add(MPL *mpl, double x, double y);
+/* floating-point addition */
+
+#define fp_sub _glp_mpl_fp_sub
+double fp_sub(MPL *mpl, double x, double y);
+/* floating-point subtraction */
+
+#define fp_less _glp_mpl_fp_less
+double fp_less(MPL *mpl, double x, double y);
+/* floating-point non-negative subtraction */
+
+#define fp_mul _glp_mpl_fp_mul
+double fp_mul(MPL *mpl, double x, double y);
+/* floating-point multiplication */
+
+#define fp_div _glp_mpl_fp_div
+double fp_div(MPL *mpl, double x, double y);
+/* floating-point division */
+
+#define fp_idiv _glp_mpl_fp_idiv
+double fp_idiv(MPL *mpl, double x, double y);
+/* floating-point quotient of exact division */
+
+#define fp_mod _glp_mpl_fp_mod
+double fp_mod(MPL *mpl, double x, double y);
+/* floating-point remainder of exact division */
+
+#define fp_power _glp_mpl_fp_power
+double fp_power(MPL *mpl, double x, double y);
+/* floating-point exponentiation (raise to power) */
+
+#define fp_exp _glp_mpl_fp_exp
+double fp_exp(MPL *mpl, double x);
+/* floating-point base-e exponential */
+
+#define fp_log _glp_mpl_fp_log
+double fp_log(MPL *mpl, double x);
+/* floating-point natural logarithm */
+
+#define fp_log10 _glp_mpl_fp_log10
+double fp_log10(MPL *mpl, double x);
+/* floating-point common (decimal) logarithm */
+
+#define fp_sqrt _glp_mpl_fp_sqrt
+double fp_sqrt(MPL *mpl, double x);
+/* floating-point square root */
+
+#define fp_sin _glp_mpl_fp_sin
+double fp_sin(MPL *mpl, double x);
+/* floating-point trigonometric sine */
+
+#define fp_cos _glp_mpl_fp_cos
+double fp_cos(MPL *mpl, double x);
+/* floating-point trigonometric cosine */
+
+#define fp_tan _glp_mpl_fp_tan
+double fp_tan(MPL *mpl, double x);
+/* floating-point trigonometric tangent */
+
+#define fp_atan _glp_mpl_fp_atan
+double fp_atan(MPL *mpl, double x);
+/* floating-point trigonometric arctangent */
+
+#define fp_atan2 _glp_mpl_fp_atan2
+double fp_atan2(MPL *mpl, double y, double x);
+/* floating-point trigonometric arctangent */
+
+#define fp_round _glp_mpl_fp_round
+double fp_round(MPL *mpl, double x, double n);
+/* round floating-point value to n fractional digits */
+
+#define fp_trunc _glp_mpl_fp_trunc
+double fp_trunc(MPL *mpl, double x, double n);
+/* truncate floating-point value to n fractional digits */
+
+/**********************************************************************/
+/* * * PSEUDO-RANDOM NUMBER GENERATORS * * */
+/**********************************************************************/
+
+#define fp_irand224 _glp_mpl_fp_irand224
+double fp_irand224(MPL *mpl);
+/* pseudo-random integer in the range [0, 2^24) */
+
+#define fp_uniform01 _glp_mpl_fp_uniform01
+double fp_uniform01(MPL *mpl);
+/* pseudo-random number in the range [0, 1) */
+
+#define fp_uniform _glp_mpl_uniform
+double fp_uniform(MPL *mpl, double a, double b);
+/* pseudo-random number in the range [a, b) */
+
+#define fp_normal01 _glp_mpl_fp_normal01
+double fp_normal01(MPL *mpl);
+/* Gaussian random variate with mu = 0 and sigma = 1 */
+
+#define fp_normal _glp_mpl_fp_normal
+double fp_normal(MPL *mpl, double mu, double sigma);
+/* Gaussian random variate with specified mu and sigma */
+
+/**********************************************************************/
+/* * * DATE/TIME * * */
+/**********************************************************************/
+
+#define fn_gmtime _glp_mpl_fn_gmtime
+double fn_gmtime(MPL *mpl);
+/* obtain the current calendar time (UTC) */
+
+#define fn_str2time _glp_mpl_fn_str2time
+double fn_str2time(MPL *mpl, const char *str, const char *fmt);
+/* convert character string to the calendar time */
+
+#define fn_time2str _glp_mpl_fn_time2str
+void fn_time2str(MPL *mpl, char *str, double t, const char *fmt);
+/* convert the calendar time to character string */
+
+/**********************************************************************/
+/* * * CHARACTER STRINGS * * */
+/**********************************************************************/
+
+#define create_string _glp_mpl_create_string
+STRING *create_string
+( MPL *mpl,
+ char buf[MAX_LENGTH+1] /* not changed */
+);
+/* create character string */
+
+#define copy_string _glp_mpl_copy_string
+STRING *copy_string
+( MPL *mpl,
+ STRING *str /* not changed */
+);
+/* make copy of character string */
+
+#define compare_strings _glp_mpl_compare_strings
+int compare_strings
+( MPL *mpl,
+ STRING *str1, /* not changed */
+ STRING *str2 /* not changed */
+);
+/* compare one character string with another */
+
+#define fetch_string _glp_mpl_fetch_string
+char *fetch_string
+( MPL *mpl,
+ STRING *str, /* not changed */
+ char buf[MAX_LENGTH+1] /* modified */
+);
+/* extract content of character string */
+
+#define delete_string _glp_mpl_delete_string
+void delete_string
+( MPL *mpl,
+ STRING *str /* destroyed */
+);
+/* delete character string */
+
+/**********************************************************************/
+/* * * SYMBOLS * * */
+/**********************************************************************/
+
+struct SYMBOL
+{ /* symbol (numeric or abstract quantity) */
+ double num;
+ /* numeric value of symbol (used only if str == NULL) */
+ STRING *str;
+ /* abstract value of symbol (used only if str != NULL) */
+};
+
+#define create_symbol_num _glp_mpl_create_symbol_num
+SYMBOL *create_symbol_num(MPL *mpl, double num);
+/* create symbol of numeric type */
+
+#define create_symbol_str _glp_mpl_create_symbol_str
+SYMBOL *create_symbol_str
+( MPL *mpl,
+ STRING *str /* destroyed */
+);
+/* create symbol of abstract type */
+
+#define copy_symbol _glp_mpl_copy_symbol
+SYMBOL *copy_symbol
+( MPL *mpl,
+ SYMBOL *sym /* not changed */
+);
+/* make copy of symbol */
+
+#define compare_symbols _glp_mpl_compare_symbols
+int compare_symbols
+( MPL *mpl,
+ SYMBOL *sym1, /* not changed */
+ SYMBOL *sym2 /* not changed */
+);
+/* compare one symbol with another */
+
+#define delete_symbol _glp_mpl_delete_symbol
+void delete_symbol
+( MPL *mpl,
+ SYMBOL *sym /* destroyed */
+);
+/* delete symbol */
+
+#define format_symbol _glp_mpl_format_symbol
+char *format_symbol
+( MPL *mpl,
+ SYMBOL *sym /* not changed */
+);
+/* format symbol for displaying or printing */
+
+#define concat_symbols _glp_mpl_concat_symbols
+SYMBOL *concat_symbols
+( MPL *mpl,
+ SYMBOL *sym1, /* destroyed */
+ SYMBOL *sym2 /* destroyed */
+);
+/* concatenate one symbol with another */
+
+/**********************************************************************/
+/* * * N-TUPLES * * */
+/**********************************************************************/
+
+struct TUPLE
+{ /* component of n-tuple; the n-tuple itself is associated with
+ its first component; (note that 0-tuple has no components) */
+ SYMBOL *sym;
+ /* symbol, which the component refers to; cannot be NULL */
+ TUPLE *next;
+ /* the next component of n-tuple */
+};
+
+#define create_tuple _glp_mpl_create_tuple
+TUPLE *create_tuple(MPL *mpl);
+/* create n-tuple */
+
+#define expand_tuple _glp_mpl_expand_tuple
+TUPLE *expand_tuple
+( MPL *mpl,
+ TUPLE *tuple, /* destroyed */
+ SYMBOL *sym /* destroyed */
+);
+/* append symbol to n-tuple */
+
+#define tuple_dimen _glp_mpl_tuple_dimen
+int tuple_dimen
+( MPL *mpl,
+ TUPLE *tuple /* not changed */
+);
+/* determine dimension of n-tuple */
+
+#define copy_tuple _glp_mpl_copy_tuple
+TUPLE *copy_tuple
+( MPL *mpl,
+ TUPLE *tuple /* not changed */
+);
+/* make copy of n-tuple */
+
+#define compare_tuples _glp_mpl_compare_tuples
+int compare_tuples
+( MPL *mpl,
+ TUPLE *tuple1, /* not changed */
+ TUPLE *tuple2 /* not changed */
+);
+/* compare one n-tuple with another */
+
+#define build_subtuple _glp_mpl_build_subtuple
+TUPLE *build_subtuple
+( MPL *mpl,
+ TUPLE *tuple, /* not changed */
+ int dim
+);
+/* build subtuple of given n-tuple */
+
+#define delete_tuple _glp_mpl_delete_tuple
+void delete_tuple
+( MPL *mpl,
+ TUPLE *tuple /* destroyed */
+);
+/* delete n-tuple */
+
+#define format_tuple _glp_mpl_format_tuple
+char *format_tuple
+( MPL *mpl,
+ int c,
+ TUPLE *tuple /* not changed */
+);
+/* format n-tuple for displaying or printing */
+
+/**********************************************************************/
+/* * * ELEMENTAL SETS * * */
+/**********************************************************************/
+
+#if 2 + 2 == 5
+struct ELEMSET /* see ARRAY */
+{ /* elemental set of n-tuples; formally it is a "value" assigned
+ to members of model sets (like numbers and symbols, which are
+ values assigned to members of model parameters); note that a
+ simple model set is not an elemental set, it is 0-dimensional
+ array, the only member of which (if it exists) is assigned an
+ elemental set */
+#endif
+
+#define create_elemset _glp_mpl_create_elemset
+ELEMSET *create_elemset(MPL *mpl, int dim);
+/* create elemental set */
+
+#define find_tuple _glp_mpl_find_tuple
+MEMBER *find_tuple
+( MPL *mpl,
+ ELEMSET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* check if elemental set contains given n-tuple */
+
+#define add_tuple _glp_mpl_add_tuple
+MEMBER *add_tuple
+( MPL *mpl,
+ ELEMSET *set, /* modified */
+ TUPLE *tuple /* destroyed */
+);
+/* add new n-tuple to elemental set */
+
+#define check_then_add _glp_mpl_check_then_add
+MEMBER *check_then_add
+( MPL *mpl,
+ ELEMSET *set, /* modified */
+ TUPLE *tuple /* destroyed */
+);
+/* check and add new n-tuple to elemental set */
+
+#define copy_elemset _glp_mpl_copy_elemset
+ELEMSET *copy_elemset
+( MPL *mpl,
+ ELEMSET *set /* not changed */
+);
+/* make copy of elemental set */
+
+#define delete_elemset _glp_mpl_delete_elemset
+void delete_elemset
+( MPL *mpl,
+ ELEMSET *set /* destroyed */
+);
+/* delete elemental set */
+
+#define arelset_size _glp_mpl_arelset_size
+int arelset_size(MPL *mpl, double t0, double tf, double dt);
+/* compute size of "arithmetic" elemental set */
+
+#define arelset_member _glp_mpl_arelset_member
+double arelset_member(MPL *mpl, double t0, double tf, double dt, int j);
+/* compute member of "arithmetic" elemental set */
+
+#define create_arelset _glp_mpl_create_arelset
+ELEMSET *create_arelset(MPL *mpl, double t0, double tf, double dt);
+/* create "arithmetic" elemental set */
+
+#define set_union _glp_mpl_set_union
+ELEMSET *set_union
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+);
+/* union of two elemental sets */
+
+#define set_diff _glp_mpl_set_diff
+ELEMSET *set_diff
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+);
+/* difference between two elemental sets */
+
+#define set_symdiff _glp_mpl_set_symdiff
+ELEMSET *set_symdiff
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+);
+/* symmetric difference between two elemental sets */
+
+#define set_inter _glp_mpl_set_inter
+ELEMSET *set_inter
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+);
+/* intersection of two elemental sets */
+
+#define set_cross _glp_mpl_set_cross
+ELEMSET *set_cross
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+);
+/* cross (Cartesian) product of two elemental sets */
+
+/**********************************************************************/
+/* * * ELEMENTAL VARIABLES * * */
+/**********************************************************************/
+
+struct ELEMVAR
+{ /* elemental variable; formally it is a "value" assigned to
+ members of model variables (like numbers and symbols, which
+ are values assigned to members of model parameters) */
+ int j;
+ /* LP column number assigned to this elemental variable */
+ VARIABLE *var;
+ /* model variable, which contains this elemental variable */
+ MEMBER *memb;
+ /* array member, which is assigned this elemental variable */
+ double lbnd;
+ /* lower bound */
+ double ubnd;
+ /* upper bound */
+ double temp;
+ /* working quantity used in operations on linear forms; normally
+ it contains floating-point zero */
+#if 1 /* 15/V-2010 */
+ int stat;
+ double prim, dual;
+ /* solution components provided by the solver */
+#endif
+};
+
+/**********************************************************************/
+/* * * LINEAR FORMS * * */
+/**********************************************************************/
+
+struct FORMULA
+{ /* term of linear form c * x, where c is a coefficient, x is an
+ elemental variable; the linear form itself is the sum of terms
+ and is associated with its first term; (note that the linear
+ form may be empty that means the sum is equal to zero) */
+ double coef;
+ /* coefficient at elemental variable or constant term */
+ ELEMVAR *var;
+ /* reference to elemental variable; NULL means constant term */
+ FORMULA *next;
+ /* the next term of linear form */
+};
+
+#define constant_term _glp_mpl_constant_term
+FORMULA *constant_term(MPL *mpl, double coef);
+/* create constant term */
+
+#define single_variable _glp_mpl_single_variable
+FORMULA *single_variable
+( MPL *mpl,
+ ELEMVAR *var /* referenced */
+);
+/* create single variable */
+
+#define copy_formula _glp_mpl_copy_formula
+FORMULA *copy_formula
+( MPL *mpl,
+ FORMULA *form /* not changed */
+);
+/* make copy of linear form */
+
+#define delete_formula _glp_mpl_delete_formula
+void delete_formula
+( MPL *mpl,
+ FORMULA *form /* destroyed */
+);
+/* delete linear form */
+
+#define linear_comb _glp_mpl_linear_comb
+FORMULA *linear_comb
+( MPL *mpl,
+ double a, FORMULA *fx, /* destroyed */
+ double b, FORMULA *fy /* destroyed */
+);
+/* linear combination of two linear forms */
+
+#define remove_constant _glp_mpl_remove_constant
+FORMULA *remove_constant
+( MPL *mpl,
+ FORMULA *form, /* destroyed */
+ double *coef /* modified */
+);
+/* remove constant term from linear form */
+
+#define reduce_terms _glp_mpl_reduce_terms
+FORMULA *reduce_terms
+( MPL *mpl,
+ FORMULA *form /* destroyed */
+);
+/* reduce identical terms in linear form */
+
+/**********************************************************************/
+/* * * ELEMENTAL CONSTRAINTS * * */
+/**********************************************************************/
+
+struct ELEMCON
+{ /* elemental constraint; formally it is a "value" assigned to
+ members of model constraints (like numbers or symbols, which
+ are values assigned to members of model parameters) */
+ int i;
+ /* LP row number assigned to this elemental constraint */
+ CONSTRAINT *con;
+ /* model constraint, which contains this elemental constraint */
+ MEMBER *memb;
+ /* array member, which is assigned this elemental constraint */
+ FORMULA *form;
+ /* linear form */
+ double lbnd;
+ /* lower bound */
+ double ubnd;
+ /* upper bound */
+#if 1 /* 15/V-2010 */
+ int stat;
+ double prim, dual;
+ /* solution components provided by the solver */
+#endif
+};
+
+/**********************************************************************/
+/* * * GENERIC VALUES * * */
+/**********************************************************************/
+
+union VALUE
+{ /* generic value, which can be assigned to object member or be a
+ result of evaluation of expression */
+ /* indicator that specifies the particular type of generic value
+ is stored in the corresponding array or pseudo-code descriptor
+ and can be one of the following:
+ A_NONE - no value
+ A_NUMERIC - floating-point number
+ A_SYMBOLIC - symbol
+ A_LOGICAL - logical value
+ A_TUPLE - n-tuple
+ A_ELEMSET - elemental set
+ A_ELEMVAR - elemental variable
+ A_FORMULA - linear form
+ A_ELEMCON - elemental constraint */
+ void *none; /* null */
+ double num; /* value */
+ SYMBOL *sym; /* value */
+ int bit; /* value */
+ TUPLE *tuple; /* value */
+ ELEMSET *set; /* value */
+ ELEMVAR *var; /* reference */
+ FORMULA *form; /* value */
+ ELEMCON *con; /* reference */
+};
+
+#define delete_value _glp_mpl_delete_value
+void delete_value
+( MPL *mpl,
+ int type,
+ VALUE *value /* content destroyed */
+);
+/* delete generic value */
+
+/**********************************************************************/
+/* * * SYMBOLICALLY INDEXED ARRAYS * * */
+/**********************************************************************/
+
+struct ARRAY
+{ /* multi-dimensional array, a set of members indexed over simple
+ or compound sets of symbols; arrays are used to represent the
+ contents of model objects (i.e. sets, parameters, variables,
+ constraints, and objectives); arrays also are used as "values"
+ that are assigned to members of set objects, in which case the
+ array itself represents an elemental set */
+ int type;
+ /* type of generic values assigned to the array members:
+ A_NONE - none (members have no assigned values)
+ A_NUMERIC - floating-point numbers
+ A_SYMBOLIC - symbols
+ A_ELEMSET - elemental sets
+ A_ELEMVAR - elemental variables
+ A_ELEMCON - elemental constraints */
+ int dim;
+ /* dimension of the array that determines number of components in
+ n-tuples for all members of the array, dim >= 0; dim = 0 means
+ the array is 0-dimensional */
+ int size;
+ /* size of the array, i.e. number of its members */
+ MEMBER *head;
+ /* the first array member; NULL means the array is empty */
+ MEMBER *tail;
+ /* the last array member; NULL means the array is empty */
+ AVL *tree;
+ /* the search tree intended to find array members for logarithmic
+ time; NULL means the search tree doesn't exist */
+ ARRAY *prev;
+ /* the previous array in the translator database */
+ ARRAY *next;
+ /* the next array in the translator database */
+};
+
+struct MEMBER
+{ /* array member */
+ TUPLE *tuple;
+ /* n-tuple, which identifies the member; number of its components
+ is the same for all members within the array and determined by
+ the array dimension; duplicate members are not allowed */
+ MEMBER *next;
+ /* the next array member */
+ VALUE value;
+ /* generic value assigned to the member */
+};
+
+#define create_array _glp_mpl_create_array
+ARRAY *create_array(MPL *mpl, int type, int dim);
+/* create array */
+
+#define find_member _glp_mpl_find_member
+MEMBER *find_member
+( MPL *mpl,
+ ARRAY *array, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* find array member with given n-tuple */
+
+#define add_member _glp_mpl_add_member
+MEMBER *add_member
+( MPL *mpl,
+ ARRAY *array, /* modified */
+ TUPLE *tuple /* destroyed */
+);
+/* add new member to array */
+
+#define delete_array _glp_mpl_delete_array
+void delete_array
+( MPL *mpl,
+ ARRAY *array /* destroyed */
+);
+/* delete array */
+
+/**********************************************************************/
+/* * * DOMAINS AND DUMMY INDICES * * */
+/**********************************************************************/
+
+struct DOMAIN
+{ /* domain (a simple or compound set); syntactically domain looks
+ like '{ i in I, (j,k) in S, t in T : <predicate> }'; domains
+ are used to define sets, over which model objects are indexed,
+ and also as constituents of iterated operators */
+ DOMAIN_BLOCK *list;
+ /* linked list of domain blocks (in the example above such blocks
+ are 'i in I', '(j,k) in S', and 't in T'); this list cannot be
+ empty */
+ CODE *code;
+ /* pseudo-code for computing the logical predicate, which follows
+ the colon; NULL means no predicate is specified */
+};
+
+struct DOMAIN_BLOCK
+{ /* domain block; syntactically domain blocks look like 'i in I',
+ '(j,k) in S', and 't in T' in the example above (in the sequel
+ sets like I, S, and T are called basic sets) */
+ DOMAIN_SLOT *list;
+ /* linked list of domain slots (i.e. indexing positions); number
+ of slots in this list is the same as dimension of n-tuples in
+ the basic set; this list cannot be empty */
+ CODE *code;
+ /* pseudo-code for computing basic set; cannot be NULL */
+ TUPLE *backup;
+ /* if this n-tuple is not empty, current values of dummy indices
+ in the domain block are the same as components of this n-tuple
+ (note that this n-tuple may have larger dimension than number
+ of dummy indices in this block, in which case extra components
+ are ignored); this n-tuple is used to restore former values of
+ dummy indices, if they were changed due to recursive calls to
+ the domain block */
+ DOMAIN_BLOCK *next;
+ /* the next block in the same domain */
+};
+
+struct DOMAIN_SLOT
+{ /* domain slot; it specifies an individual indexing position and
+ defines the corresponding dummy index */
+ char *name;
+ /* symbolic name of the dummy index; null pointer means the dummy
+ index is not explicitly specified */
+ CODE *code;
+ /* pseudo-code for computing symbolic value, at which the dummy
+ index is bound; NULL means the dummy index is free within the
+ domain scope */
+ SYMBOL *value;
+ /* current value assigned to the dummy index; NULL means no value
+ is assigned at the moment */
+ CODE *list;
+ /* linked list of pseudo-codes with operation O_INDEX referring
+ to this slot; this linked list is used to invalidate resultant
+ values of the operation, which depend on this dummy index */
+ DOMAIN_SLOT *next;
+ /* the next slot in the same domain block */
+};
+
+#define assign_dummy_index _glp_mpl_assign_dummy_index
+void assign_dummy_index
+( MPL *mpl,
+ DOMAIN_SLOT *slot, /* modified */
+ SYMBOL *value /* not changed */
+);
+/* assign new value to dummy index */
+
+#define update_dummy_indices _glp_mpl_update_dummy_indices
+void update_dummy_indices
+( MPL *mpl,
+ DOMAIN_BLOCK *block /* not changed */
+);
+/* update current values of dummy indices */
+
+#define enter_domain_block _glp_mpl_enter_domain_block
+int enter_domain_block
+( MPL *mpl,
+ DOMAIN_BLOCK *block, /* not changed */
+ TUPLE *tuple, /* not changed */
+ void *info, void (*func)(MPL *mpl, void *info)
+);
+/* enter domain block */
+
+#define eval_within_domain _glp_mpl_eval_within_domain
+int eval_within_domain
+( MPL *mpl,
+ DOMAIN *domain, /* not changed */
+ TUPLE *tuple, /* not changed */
+ void *info, void (*func)(MPL *mpl, void *info)
+);
+/* perform evaluation within domain scope */
+
+#define loop_within_domain _glp_mpl_loop_within_domain
+void loop_within_domain
+( MPL *mpl,
+ DOMAIN *domain, /* not changed */
+ void *info, int (*func)(MPL *mpl, void *info)
+);
+/* perform iterations within domain scope */
+
+#define out_of_domain _glp_mpl_out_of_domain
+void out_of_domain
+( MPL *mpl,
+ char *name, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* raise domain exception */
+
+#define get_domain_tuple _glp_mpl_get_domain_tuple
+TUPLE *get_domain_tuple
+( MPL *mpl,
+ DOMAIN *domain /* not changed */
+);
+/* obtain current n-tuple from domain */
+
+#define clean_domain _glp_mpl_clean_domain
+void clean_domain(MPL *mpl, DOMAIN *domain);
+/* clean domain */
+
+/**********************************************************************/
+/* * * MODEL SETS * * */
+/**********************************************************************/
+
+struct SET
+{ /* model set */
+ char *name;
+ /* symbolic name; cannot be NULL */
+ char *alias;
+ /* alias; NULL means alias is not specified */
+ int dim; /* aka arity */
+ /* dimension (number of subscripts); dim = 0 means 0-dimensional
+ (unsubscripted) set, dim > 0 means set of sets */
+ DOMAIN *domain;
+ /* subscript domain; NULL for 0-dimensional set */
+ int dimen;
+ /* dimension of n-tuples, which members of this set consist of
+ (note that the model set itself is an array of elemental sets,
+ which are its members; so, don't confuse this dimension with
+ dimension of the model set); always non-zero */
+ WITHIN *within;
+ /* list of supersets, which restrict each member of the set to be
+ in every superset from this list; this list can be empty */
+ CODE *assign;
+ /* pseudo-code for computing assigned value; can be NULL */
+ CODE *option;
+ /* pseudo-code for computing default value; can be NULL */
+ GADGET *gadget;
+ /* plain set used to initialize the array of sets; can be NULL */
+ int data;
+ /* data status flag:
+ 0 - no data are provided in the data section
+ 1 - data are provided, but not checked yet
+ 2 - data are provided and have been checked */
+ ARRAY *array;
+ /* array of members, which are assigned elemental sets */
+};
+
+struct WITHIN
+{ /* restricting superset list entry */
+ CODE *code;
+ /* pseudo-code for computing the superset; cannot be NULL */
+ WITHIN *next;
+ /* the next entry for the same set or parameter */
+};
+
+struct GADGET
+{ /* plain set used to initialize the array of sets with data */
+ SET *set;
+ /* pointer to plain set; cannot be NULL */
+ int ind[20]; /* ind[dim+dimen]; */
+ /* permutation of integers 1, 2, ..., dim+dimen */
+};
+
+#define check_elem_set _glp_mpl_check_elem_set
+void check_elem_set
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple, /* not changed */
+ ELEMSET *refer /* not changed */
+);
+/* check elemental set assigned to set member */
+
+#define take_member_set _glp_mpl_take_member_set
+ELEMSET *take_member_set /* returns reference, not value */
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* obtain elemental set assigned to set member */
+
+#define eval_member_set _glp_mpl_eval_member_set
+ELEMSET *eval_member_set /* returns reference, not value */
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* evaluate elemental set assigned to set member */
+
+#define eval_whole_set _glp_mpl_eval_whole_set
+void eval_whole_set(MPL *mpl, SET *set);
+/* evaluate model set over entire domain */
+
+#define clean_set _glp_mpl_clean_set
+void clean_set(MPL *mpl, SET *set);
+/* clean model set */
+
+/**********************************************************************/
+/* * * MODEL PARAMETERS * * */
+/**********************************************************************/
+
+struct PARAMETER
+{ /* model parameter */
+ char *name;
+ /* symbolic name; cannot be NULL */
+ char *alias;
+ /* alias; NULL means alias is not specified */
+ int dim; /* aka arity */
+ /* dimension (number of subscripts); dim = 0 means 0-dimensional
+ (unsubscripted) parameter */
+ DOMAIN *domain;
+ /* subscript domain; NULL for 0-dimensional parameter */
+ int type;
+ /* parameter type:
+ A_NUMERIC - numeric
+ A_INTEGER - integer
+ A_BINARY - binary
+ A_SYMBOLIC - symbolic */
+ CONDITION *cond;
+ /* list of conditions, which restrict each parameter member to
+ satisfy to every condition from this list; this list is used
+ only for numeric parameters and can be empty */
+ WITHIN *in;
+ /* list of supersets, which restrict each parameter member to be
+ in every superset from this list; this list is used only for
+ symbolic parameters and can be empty */
+ CODE *assign;
+ /* pseudo-code for computing assigned value; can be NULL */
+ CODE *option;
+ /* pseudo-code for computing default value; can be NULL */
+ int data;
+ /* data status flag:
+ 0 - no data are provided in the data section
+ 1 - data are provided, but not checked yet
+ 2 - data are provided and have been checked */
+ SYMBOL *defval;
+ /* default value provided in the data section; can be NULL */
+ ARRAY *array;
+ /* array of members, which are assigned numbers or symbols */
+};
+
+struct CONDITION
+{ /* restricting condition list entry */
+ int rho;
+ /* flag that specifies the form of the condition:
+ O_LT - less than
+ O_LE - less than or equal to
+ O_EQ - equal to
+ O_GE - greater than or equal to
+ O_GT - greater than
+ O_NE - not equal to */
+ CODE *code;
+ /* pseudo-code for computing the reference value */
+ CONDITION *next;
+ /* the next entry for the same parameter */
+};
+
+#define check_value_num _glp_mpl_check_value_num
+void check_value_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple, /* not changed */
+ double value
+);
+/* check numeric value assigned to parameter member */
+
+#define take_member_num _glp_mpl_take_member_num
+double take_member_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* obtain numeric value assigned to parameter member */
+
+#define eval_member_num _glp_mpl_eval_member_num
+double eval_member_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* evaluate numeric value assigned to parameter member */
+
+#define check_value_sym _glp_mpl_check_value_sym
+void check_value_sym
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple, /* not changed */
+ SYMBOL *value /* not changed */
+);
+/* check symbolic value assigned to parameter member */
+
+#define take_member_sym _glp_mpl_take_member_sym
+SYMBOL *take_member_sym /* returns value, not reference */
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* obtain symbolic value assigned to parameter member */
+
+#define eval_member_sym _glp_mpl_eval_member_sym
+SYMBOL *eval_member_sym /* returns value, not reference */
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* evaluate symbolic value assigned to parameter member */
+
+#define eval_whole_par _glp_mpl_eval_whole_par
+void eval_whole_par(MPL *mpl, PARAMETER *par);
+/* evaluate model parameter over entire domain */
+
+#define clean_parameter _glp_mpl_clean_parameter
+void clean_parameter(MPL *mpl, PARAMETER *par);
+/* clean model parameter */
+
+/**********************************************************************/
+/* * * MODEL VARIABLES * * */
+/**********************************************************************/
+
+struct VARIABLE
+{ /* model variable */
+ char *name;
+ /* symbolic name; cannot be NULL */
+ char *alias;
+ /* alias; NULL means alias is not specified */
+ int dim; /* aka arity */
+ /* dimension (number of subscripts); dim = 0 means 0-dimensional
+ (unsubscripted) variable */
+ DOMAIN *domain;
+ /* subscript domain; NULL for 0-dimensional variable */
+ int type;
+ /* variable type:
+ A_NUMERIC - continuous
+ A_INTEGER - integer
+ A_BINARY - binary */
+ CODE *lbnd;
+ /* pseudo-code for computing lower bound; NULL means lower bound
+ is not specified */
+ CODE *ubnd;
+ /* pseudo-code for computing upper bound; NULL means upper bound
+ is not specified */
+ /* if both the pointers lbnd and ubnd refer to the same code, the
+ variable is fixed at the corresponding value */
+ ARRAY *array;
+ /* array of members, which are assigned elemental variables */
+};
+
+#define take_member_var _glp_mpl_take_member_var
+ELEMVAR *take_member_var /* returns reference */
+( MPL *mpl,
+ VARIABLE *var, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* obtain reference to elemental variable */
+
+#define eval_member_var _glp_mpl_eval_member_var
+ELEMVAR *eval_member_var /* returns reference */
+( MPL *mpl,
+ VARIABLE *var, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* evaluate reference to elemental variable */
+
+#define eval_whole_var _glp_mpl_eval_whole_var
+void eval_whole_var(MPL *mpl, VARIABLE *var);
+/* evaluate model variable over entire domain */
+
+#define clean_variable _glp_mpl_clean_variable
+void clean_variable(MPL *mpl, VARIABLE *var);
+/* clean model variable */
+
+/**********************************************************************/
+/* * * MODEL CONSTRAINTS AND OBJECTIVES * * */
+/**********************************************************************/
+
+struct CONSTRAINT
+{ /* model constraint or objective */
+ char *name;
+ /* symbolic name; cannot be NULL */
+ char *alias;
+ /* alias; NULL means alias is not specified */
+ int dim; /* aka arity */
+ /* dimension (number of subscripts); dim = 0 means 0-dimensional
+ (unsubscripted) constraint */
+ DOMAIN *domain;
+ /* subscript domain; NULL for 0-dimensional constraint */
+ int type;
+ /* constraint type:
+ A_CONSTRAINT - constraint
+ A_MINIMIZE - objective (minimization)
+ A_MAXIMIZE - objective (maximization) */
+ CODE *code;
+ /* pseudo-code for computing main linear form; cannot be NULL */
+ CODE *lbnd;
+ /* pseudo-code for computing lower bound; NULL means lower bound
+ is not specified */
+ CODE *ubnd;
+ /* pseudo-code for computing upper bound; NULL means upper bound
+ is not specified */
+ /* if both the pointers lbnd and ubnd refer to the same code, the
+ constraint has the form of equation */
+ ARRAY *array;
+ /* array of members, which are assigned elemental constraints */
+};
+
+#define take_member_con _glp_mpl_take_member_con
+ELEMCON *take_member_con /* returns reference */
+( MPL *mpl,
+ CONSTRAINT *con, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* obtain reference to elemental constraint */
+
+#define eval_member_con _glp_mpl_eval_member_con
+ELEMCON *eval_member_con /* returns reference */
+( MPL *mpl,
+ CONSTRAINT *con, /* not changed */
+ TUPLE *tuple /* not changed */
+);
+/* evaluate reference to elemental constraint */
+
+#define eval_whole_con _glp_mpl_eval_whole_con
+void eval_whole_con(MPL *mpl, CONSTRAINT *con);
+/* evaluate model constraint over entire domain */
+
+#define clean_constraint _glp_mpl_clean_constraint
+void clean_constraint(MPL *mpl, CONSTRAINT *con);
+/* clean model constraint */
+
+/**********************************************************************/
+/* * * DATA TABLES * * */
+/**********************************************************************/
+
+struct TABLE
+{ /* data table */
+ char *name;
+ /* symbolic name; cannot be NULL */
+ char *alias;
+ /* alias; NULL means alias is not specified */
+ int type;
+ /* table type:
+ A_INPUT - input table
+ A_OUTPUT - output table */
+ TABARG *arg;
+ /* argument list; cannot be empty */
+ union
+ { struct
+ { SET *set;
+ /* input set; NULL means the set is not specified */
+ TABFLD *fld;
+ /* field list; cannot be empty */
+ TABIN *list;
+ /* input list; can be empty */
+ } in;
+ struct
+ { DOMAIN *domain;
+ /* subscript domain; cannot be NULL */
+ TABOUT *list;
+ /* output list; cannot be empty */
+ } out;
+ } u;
+};
+
+struct TABARG
+{ /* table argument list entry */
+ CODE *code;
+ /* pseudo-code for computing the argument */
+ TABARG *next;
+ /* next entry for the same table */
+};
+
+struct TABFLD
+{ /* table field list entry */
+ char *name;
+ /* field name; cannot be NULL */
+ TABFLD *next;
+ /* next entry for the same table */
+};
+
+struct TABIN
+{ /* table input list entry */
+ PARAMETER *par;
+ /* parameter to be read; cannot be NULL */
+ char *name;
+ /* column name; cannot be NULL */
+ TABIN *next;
+ /* next entry for the same table */
+};
+
+struct TABOUT
+{ /* table output list entry */
+ CODE *code;
+ /* pseudo-code for computing the value to be written */
+ char *name;
+ /* column name; cannot be NULL */
+ TABOUT *next;
+ /* next entry for the same table */
+};
+
+struct TABDCA
+{ /* table driver communication area */
+ int id;
+ /* driver identifier (set by mpl_tab_drv_open) */
+ void *link;
+ /* driver link pointer (set by mpl_tab_drv_open) */
+ int na;
+ /* number of arguments */
+ char **arg; /* char *arg[1+ns]; */
+ /* arg[k], 1 <= k <= ns, is pointer to k-th argument */
+ int nf;
+ /* number of fields */
+ char **name; /* char *name[1+nc]; */
+ /* name[k], 1 <= k <= nc, is name of k-th field */
+ int *type; /* int type[1+nc]; */
+ /* type[k], 1 <= k <= nc, is type of k-th field:
+ '?' - value not assigned
+ 'N' - number
+ 'S' - character string */
+ double *num; /* double num[1+nc]; */
+ /* num[k], 1 <= k <= nc, is numeric value of k-th field */
+ char **str;
+ /* str[k], 1 <= k <= nc, is string value of k-th field */
+};
+
+#define mpl_tab_num_args _glp_mpl_tab_num_args
+int mpl_tab_num_args(TABDCA *dca);
+
+#define mpl_tab_get_arg _glp_mpl_tab_get_arg
+const char *mpl_tab_get_arg(TABDCA *dca, int k);
+
+#define mpl_tab_num_flds _glp_mpl_tab_num_flds
+int mpl_tab_num_flds(TABDCA *dca);
+
+#define mpl_tab_get_name _glp_mpl_tab_get_name
+const char *mpl_tab_get_name(TABDCA *dca, int k);
+
+#define mpl_tab_get_type _glp_mpl_tab_get_type
+int mpl_tab_get_type(TABDCA *dca, int k);
+
+#define mpl_tab_get_num _glp_mpl_tab_get_num
+double mpl_tab_get_num(TABDCA *dca, int k);
+
+#define mpl_tab_get_str _glp_mpl_tab_get_str
+const char *mpl_tab_get_str(TABDCA *dca, int k);
+
+#define mpl_tab_set_num _glp_mpl_tab_set_num
+void mpl_tab_set_num(TABDCA *dca, int k, double num);
+
+#define mpl_tab_set_str _glp_mpl_tab_set_str
+void mpl_tab_set_str(TABDCA *dca, int k, const char *str);
+
+#define mpl_tab_drv_open _glp_mpl_tab_drv_open
+void mpl_tab_drv_open(MPL *mpl, int mode);
+
+#define mpl_tab_drv_read _glp_mpl_tab_drv_read
+int mpl_tab_drv_read(MPL *mpl);
+
+#define mpl_tab_drv_write _glp_mpl_tab_drv_write
+void mpl_tab_drv_write(MPL *mpl);
+
+#define mpl_tab_drv_close _glp_mpl_tab_drv_close
+void mpl_tab_drv_close(MPL *mpl);
+
+/**********************************************************************/
+/* * * PSEUDO-CODE * * */
+/**********************************************************************/
+
+union OPERANDS
+{ /* operands that participate in pseudo-code operation (choice of
+ particular operands depends on the operation code) */
+ /*--------------------------------------------------------------*/
+ double num; /* O_NUMBER */
+ /* floaing-point number to be taken */
+ /*--------------------------------------------------------------*/
+ char *str; /* O_STRING */
+ /* character string to be taken */
+ /*--------------------------------------------------------------*/
+ struct /* O_INDEX */
+ { DOMAIN_SLOT *slot;
+ /* domain slot, which contains dummy index to be taken */
+ CODE *next;
+ /* the next pseudo-code with op = O_INDEX, which refers to the
+ same slot as this one; pointer to the beginning of this list
+ is stored in the corresponding domain slot */
+ } index;
+ /*--------------------------------------------------------------*/
+ struct /* O_MEMNUM, O_MEMSYM */
+ { PARAMETER *par;
+ /* model parameter, which contains member to be taken */
+ ARG_LIST *list;
+ /* list of subscripts; NULL for 0-dimensional parameter */
+ } par;
+ /*--------------------------------------------------------------*/
+ struct /* O_MEMSET */
+ { SET *set;
+ /* model set, which contains member to be taken */
+ ARG_LIST *list;
+ /* list of subscripts; NULL for 0-dimensional set */
+ } set;
+ /*--------------------------------------------------------------*/
+ struct /* O_MEMVAR */
+ { VARIABLE *var;
+ /* model variable, which contains member to be taken */
+ ARG_LIST *list;
+ /* list of subscripts; NULL for 0-dimensional variable */
+#if 1 /* 15/V-2010 */
+ int suff;
+ /* suffix specified: */
+#define DOT_NONE 0x00 /* none (means variable itself) */
+#define DOT_LB 0x01 /* .lb (lower bound) */
+#define DOT_UB 0x02 /* .ub (upper bound) */
+#define DOT_STATUS 0x03 /* .status (status) */
+#define DOT_VAL 0x04 /* .val (primal value) */
+#define DOT_DUAL 0x05 /* .dual (dual value) */
+#endif
+ } var;
+#if 1 /* 15/V-2010 */
+ /*--------------------------------------------------------------*/
+ struct /* O_MEMCON */
+ { CONSTRAINT *con;
+ /* model constraint, which contains member to be taken */
+ ARG_LIST *list;
+ /* list of subscripys; NULL for 0-dimensional constraint */
+ int suff;
+ /* suffix specified (see O_MEMVAR above) */
+ } con;
+#endif
+ /*--------------------------------------------------------------*/
+ ARG_LIST *list; /* O_TUPLE, O_MAKE, n-ary operations */
+ /* list of operands */
+ /*--------------------------------------------------------------*/
+ DOMAIN_BLOCK *slice; /* O_SLICE */
+ /* domain block, which specifies slice (i.e. n-tuple that contains
+ free dummy indices); this operation is never evaluated */
+ /*--------------------------------------------------------------*/
+ struct /* unary, binary, ternary operations */
+ { CODE *x;
+ /* pseudo-code for computing first operand */
+ CODE *y;
+ /* pseudo-code for computing second operand */
+ CODE *z;
+ /* pseudo-code for computing third operand */
+ } arg;
+ /*--------------------------------------------------------------*/
+ struct /* iterated operations */
+ { DOMAIN *domain;
+ /* domain, over which the operation is performed */
+ CODE *x;
+ /* pseudo-code for computing "integrand" */
+ } loop;
+ /*--------------------------------------------------------------*/
+};
+
+struct ARG_LIST
+{ /* operands list entry */
+ CODE *x;
+ /* pseudo-code for computing operand */
+ ARG_LIST *next;
+ /* the next operand of the same operation */
+};
+
+struct CODE
+{ /* pseudo-code (internal form of expressions) */
+ int op;
+ /* operation code: */
+#define O_NUMBER 301 /* take floating-point number */
+#define O_STRING 302 /* take character string */
+#define O_INDEX 303 /* take dummy index */
+#define O_MEMNUM 304 /* take member of numeric parameter */
+#define O_MEMSYM 305 /* take member of symbolic parameter */
+#define O_MEMSET 306 /* take member of set */
+#define O_MEMVAR 307 /* take member of variable */
+#define O_MEMCON 308 /* take member of constraint */
+#define O_TUPLE 309 /* make n-tuple */
+#define O_MAKE 310 /* make elemental set of n-tuples */
+#define O_SLICE 311 /* define domain block (dummy op) */
+ /* 0-ary operations --------------------*/
+#define O_IRAND224 312 /* pseudo-random in [0, 2^24-1] */
+#define O_UNIFORM01 313 /* pseudo-random in [0, 1) */
+#define O_NORMAL01 314 /* gaussian random, mu = 0, sigma = 1 */
+#define O_GMTIME 315 /* current calendar time (UTC) */
+ /* unary operations --------------------*/
+#define O_CVTNUM 316 /* conversion to numeric */
+#define O_CVTSYM 317 /* conversion to symbolic */
+#define O_CVTLOG 318 /* conversion to logical */
+#define O_CVTTUP 319 /* conversion to 1-tuple */
+#define O_CVTLFM 320 /* conversion to linear form */
+#define O_PLUS 321 /* unary plus */
+#define O_MINUS 322 /* unary minus */
+#define O_NOT 323 /* negation (logical "not") */
+#define O_ABS 324 /* absolute value */
+#define O_CEIL 325 /* round upward ("ceiling of x") */
+#define O_FLOOR 326 /* round downward ("floor of x") */
+#define O_EXP 327 /* base-e exponential */
+#define O_LOG 328 /* natural logarithm */
+#define O_LOG10 329 /* common (decimal) logarithm */
+#define O_SQRT 330 /* square root */
+#define O_SIN 331 /* trigonometric sine */
+#define O_COS 332 /* trigonometric cosine */
+#define O_TAN 333 /* trigonometric tangent */
+#define O_ATAN 334 /* trigonometric arctangent */
+#define O_ROUND 335 /* round to nearest integer */
+#define O_TRUNC 336 /* truncate to nearest integer */
+#define O_CARD 337 /* cardinality of set */
+#define O_LENGTH 338 /* length of symbolic value */
+ /* binary operations -------------------*/
+#define O_ADD 339 /* addition */
+#define O_SUB 340 /* subtraction */
+#define O_LESS 341 /* non-negative subtraction */
+#define O_MUL 342 /* multiplication */
+#define O_DIV 343 /* division */
+#define O_IDIV 344 /* quotient of exact division */
+#define O_MOD 345 /* remainder of exact division */
+#define O_POWER 346 /* exponentiation (raise to power) */
+#define O_ATAN2 347 /* trigonometric arctangent */
+#define O_ROUND2 348 /* round to n fractional digits */
+#define O_TRUNC2 349 /* truncate to n fractional digits */
+#define O_UNIFORM 350 /* pseudo-random in [a, b) */
+#define O_NORMAL 351 /* gaussian random, given mu and sigma */
+#define O_CONCAT 352 /* concatenation */
+#define O_LT 353 /* comparison on 'less than' */
+#define O_LE 354 /* comparison on 'not greater than' */
+#define O_EQ 355 /* comparison on 'equal to' */
+#define O_GE 356 /* comparison on 'not less than' */
+#define O_GT 357 /* comparison on 'greater than' */
+#define O_NE 358 /* comparison on 'not equal to' */
+#define O_AND 359 /* conjunction (logical "and") */
+#define O_OR 360 /* disjunction (logical "or") */
+#define O_UNION 361 /* union */
+#define O_DIFF 362 /* difference */
+#define O_SYMDIFF 363 /* symmetric difference */
+#define O_INTER 364 /* intersection */
+#define O_CROSS 365 /* cross (Cartesian) product */
+#define O_IN 366 /* test on 'x in Y' */
+#define O_NOTIN 367 /* test on 'x not in Y' */
+#define O_WITHIN 368 /* test on 'X within Y' */
+#define O_NOTWITHIN 369 /* test on 'X not within Y' */
+#define O_SUBSTR 370 /* substring */
+#define O_STR2TIME 371 /* convert string to time */
+#define O_TIME2STR 372 /* convert time to string */
+ /* ternary operations ------------------*/
+#define O_DOTS 373 /* build "arithmetic" set */
+#define O_FORK 374 /* if-then-else */
+#define O_SUBSTR3 375 /* substring */
+ /* n-ary operations --------------------*/
+#define O_MIN 376 /* minimal value (n-ary) */
+#define O_MAX 377 /* maximal value (n-ary) */
+ /* iterated operations -----------------*/
+#define O_SUM 378 /* summation */
+#define O_PROD 379 /* multiplication */
+#define O_MINIMUM 380 /* minimum */
+#define O_MAXIMUM 381 /* maximum */
+#define O_FORALL 382 /* conjunction (A-quantification) */
+#define O_EXISTS 383 /* disjunction (E-quantification) */
+#define O_SETOF 384 /* compute elemental set */
+#define O_BUILD 385 /* build elemental set */
+ OPERANDS arg;
+ /* operands that participate in the operation */
+ int type;
+ /* type of the resultant value:
+ A_NUMERIC - numeric
+ A_SYMBOLIC - symbolic
+ A_LOGICAL - logical
+ A_TUPLE - n-tuple
+ A_ELEMSET - elemental set
+ A_FORMULA - linear form */
+ int dim;
+ /* dimension of the resultant value; for A_TUPLE and A_ELEMSET it
+ is the dimension of the corresponding n-tuple(s) and cannot be
+ zero; for other resultant types it is always zero */
+ CODE *up;
+ /* parent pseudo-code, which refers to this pseudo-code as to its
+ operand; NULL means this pseudo-code has no parent and defines
+ an expression, which is not contained in another expression */
+ int vflag;
+ /* volatile flag; being set this flag means that this operation
+ has a side effect; for primary expressions this flag is set
+ directly by corresponding parsing routines (for example, if
+ primary expression is a reference to a function that generates
+ pseudo-random numbers); in other cases this flag is inherited
+ from operands */
+ int valid;
+ /* if this flag is set, the resultant value, which is a temporary
+ result of evaluating this operation on particular values of
+ operands, is valid; if this flag is clear, the resultant value
+ doesn't exist and therefore not valid; having been evaluated
+ the resultant value is stored here and not destroyed until the
+ dummy indices, which this value depends on, have been changed
+ (and if it doesn't depend on dummy indices at all, it is never
+ destroyed); thus, if the resultant value is valid, evaluating
+ routine can immediately take its copy not computing the result
+ from scratch; this mechanism is similar to moving invariants
+ out of loops and allows improving efficiency at the expense of
+ some extra memory needed to keep temporary results */
+ /* however, if the volatile flag (see above) is set, even if the
+ resultant value is valid, evaluating routine computes it as if
+ it were not valid, i.e. caching is not used in this case */
+ VALUE value;
+ /* resultant value in generic format */
+};
+
+#define eval_numeric _glp_mpl_eval_numeric
+double eval_numeric(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to determine numeric value */
+
+#define eval_symbolic _glp_mpl_eval_symbolic
+SYMBOL *eval_symbolic(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to determine symbolic value */
+
+#define eval_logical _glp_mpl_eval_logical
+int eval_logical(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to determine logical value */
+
+#define eval_tuple _glp_mpl_eval_tuple
+TUPLE *eval_tuple(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to construct n-tuple */
+
+#define eval_elemset _glp_mpl_eval_elemset
+ELEMSET *eval_elemset(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to construct elemental set */
+
+#define is_member _glp_mpl_is_member
+int is_member(MPL *mpl, CODE *code, TUPLE *tuple);
+/* check if n-tuple is in set specified by pseudo-code */
+
+#define eval_formula _glp_mpl_eval_formula
+FORMULA *eval_formula(MPL *mpl, CODE *code);
+/* evaluate pseudo-code to construct linear form */
+
+#define clean_code _glp_mpl_clean_code
+void clean_code(MPL *mpl, CODE *code);
+/* clean pseudo-code */
+
+/**********************************************************************/
+/* * * MODEL STATEMENTS * * */
+/**********************************************************************/
+
+struct CHECK
+{ /* check statement */
+ DOMAIN *domain;
+ /* subscript domain; NULL means domain is not used */
+ CODE *code;
+ /* code for computing the predicate to be checked */
+};
+
+struct DISPLAY
+{ /* display statement */
+ DOMAIN *domain;
+ /* subscript domain; NULL means domain is not used */
+ DISPLAY1 *list;
+ /* display list; cannot be empty */
+};
+
+struct DISPLAY1
+{ /* display list entry */
+ int type;
+ /* item type:
+ A_INDEX - dummy index
+ A_SET - model set
+ A_PARAMETER - model parameter
+ A_VARIABLE - model variable
+ A_CONSTRAINT - model constraint/objective
+ A_EXPRESSION - expression */
+ union
+ { DOMAIN_SLOT *slot;
+ SET *set;
+ PARAMETER *par;
+ VARIABLE *var;
+ CONSTRAINT *con;
+ CODE *code;
+ } u;
+ /* item to be displayed */
+#if 0 /* 15/V-2010 */
+ ARG_LIST *list;
+ /* optional subscript list (for constraint/objective only) */
+#endif
+ DISPLAY1 *next;
+ /* the next entry for the same statement */
+};
+
+struct PRINTF
+{ /* printf statement */
+ DOMAIN *domain;
+ /* subscript domain; NULL means domain is not used */
+ CODE *fmt;
+ /* pseudo-code for computing format string */
+ PRINTF1 *list;
+ /* printf list; can be empty */
+ CODE *fname;
+ /* pseudo-code for computing filename to redirect the output;
+ NULL means the output goes to stdout */
+ int app;
+ /* if this flag is set, the output is appended */
+};
+
+struct PRINTF1
+{ /* printf list entry */
+ CODE *code;
+ /* pseudo-code for computing value to be printed */
+ PRINTF1 *next;
+ /* the next entry for the same statement */
+};
+
+struct FOR
+{ /* for statement */
+ DOMAIN *domain;
+ /* subscript domain; cannot be NULL */
+ STATEMENT *list;
+ /* linked list of model statements within this for statement in
+ the original order */
+};
+
+struct STATEMENT
+{ /* model statement */
+ int line;
+ /* number of source text line, where statement begins */
+ int type;
+ /* statement type:
+ A_SET - set statement
+ A_PARAMETER - parameter statement
+ A_VARIABLE - variable statement
+ A_CONSTRAINT - constraint/objective statement
+ A_TABLE - table statement
+ A_SOLVE - solve statement
+ A_CHECK - check statement
+ A_DISPLAY - display statement
+ A_PRINTF - printf statement
+ A_FOR - for statement */
+ union
+ { SET *set;
+ PARAMETER *par;
+ VARIABLE *var;
+ CONSTRAINT *con;
+ TABLE *tab;
+ void *slv; /* currently not used (set to NULL) */
+ CHECK *chk;
+ DISPLAY *dpy;
+ PRINTF *prt;
+ FOR *fur;
+ } u;
+ /* specific part of statement */
+ STATEMENT *next;
+ /* the next statement; in this list statements follow in the same
+ order as they appear in the model section */
+};
+
+#define execute_table _glp_mpl_execute_table
+void execute_table(MPL *mpl, TABLE *tab);
+/* execute table statement */
+
+#define free_dca _glp_mpl_free_dca
+void free_dca(MPL *mpl);
+/* free table driver communucation area */
+
+#define clean_table _glp_mpl_clean_table
+void clean_table(MPL *mpl, TABLE *tab);
+/* clean table statement */
+
+#define execute_check _glp_mpl_execute_check
+void execute_check(MPL *mpl, CHECK *chk);
+/* execute check statement */
+
+#define clean_check _glp_mpl_clean_check
+void clean_check(MPL *mpl, CHECK *chk);
+/* clean check statement */
+
+#define execute_display _glp_mpl_execute_display
+void execute_display(MPL *mpl, DISPLAY *dpy);
+/* execute display statement */
+
+#define clean_display _glp_mpl_clean_display
+void clean_display(MPL *mpl, DISPLAY *dpy);
+/* clean display statement */
+
+#define execute_printf _glp_mpl_execute_printf
+void execute_printf(MPL *mpl, PRINTF *prt);
+/* execute printf statement */
+
+#define clean_printf _glp_mpl_clean_printf
+void clean_printf(MPL *mpl, PRINTF *prt);
+/* clean printf statement */
+
+#define execute_for _glp_mpl_execute_for
+void execute_for(MPL *mpl, FOR *fur);
+/* execute for statement */
+
+#define clean_for _glp_mpl_clean_for
+void clean_for(MPL *mpl, FOR *fur);
+/* clean for statement */
+
+#define execute_statement _glp_mpl_execute_statement
+void execute_statement(MPL *mpl, STATEMENT *stmt);
+/* execute specified model statement */
+
+#define clean_statement _glp_mpl_clean_statement
+void clean_statement(MPL *mpl, STATEMENT *stmt);
+/* clean specified model statement */
+
+/**********************************************************************/
+/* * * GENERATING AND POSTSOLVING MODEL * * */
+/**********************************************************************/
+
+#define alloc_content _glp_mpl_alloc_content
+void alloc_content(MPL *mpl);
+/* allocate content arrays for all model objects */
+
+#define generate_model _glp_mpl_generate_model
+void generate_model(MPL *mpl);
+/* generate model */
+
+#define build_problem _glp_mpl_build_problem
+void build_problem(MPL *mpl);
+/* build problem instance */
+
+#define postsolve_model _glp_mpl_postsolve_model
+void postsolve_model(MPL *mpl);
+/* postsolve model */
+
+#define clean_model _glp_mpl_clean_model
+void clean_model(MPL *mpl);
+/* clean model content */
+
+/**********************************************************************/
+/* * * INPUT/OUTPUT * * */
+/**********************************************************************/
+
+#define open_input _glp_mpl_open_input
+void open_input(MPL *mpl, char *file);
+/* open input text file */
+
+#define read_char _glp_mpl_read_char
+int read_char(MPL *mpl);
+/* read next character from input text file */
+
+#define close_input _glp_mpl_close_input
+void close_input(MPL *mpl);
+/* close input text file */
+
+#define open_output _glp_mpl_open_output
+void open_output(MPL *mpl, char *file);
+/* open output text file */
+
+#define write_char _glp_mpl_write_char
+void write_char(MPL *mpl, int c);
+/* write next character to output text file */
+
+#define write_text _glp_mpl_write_text
+void write_text(MPL *mpl, char *fmt, ...);
+/* format and write text to output text file */
+
+#define flush_output _glp_mpl_flush_output
+void flush_output(MPL *mpl);
+/* finalize writing data to output text file */
+
+/**********************************************************************/
+/* * * SOLVER INTERFACE * * */
+/**********************************************************************/
+
+#define MPL_FR 401 /* free (unbounded) */
+#define MPL_LO 402 /* lower bound */
+#define MPL_UP 403 /* upper bound */
+#define MPL_DB 404 /* both lower and upper bounds */
+#define MPL_FX 405 /* fixed */
+
+#define MPL_ST 411 /* constraint */
+#define MPL_MIN 412 /* objective (minimization) */
+#define MPL_MAX 413 /* objective (maximization) */
+
+#define MPL_NUM 421 /* continuous */
+#define MPL_INT 422 /* integer */
+#define MPL_BIN 423 /* binary */
+
+#define error _glp_mpl_error
+void error(MPL *mpl, char *fmt, ...);
+/* print error message and terminate model processing */
+
+#define warning _glp_mpl_warning
+void warning(MPL *mpl, char *fmt, ...);
+/* print warning message and continue model processing */
+
+#define mpl_initialize _glp_mpl_initialize
+MPL *mpl_initialize(void);
+/* create and initialize translator database */
+
+#define mpl_read_model _glp_mpl_read_model
+int mpl_read_model(MPL *mpl, char *file, int skip_data);
+/* read model section and optional data section */
+
+#define mpl_read_data _glp_mpl_read_data
+int mpl_read_data(MPL *mpl, char *file);
+/* read data section */
+
+#define mpl_generate _glp_mpl_generate
+int mpl_generate(MPL *mpl, char *file);
+/* generate model */
+
+#define mpl_get_prob_name _glp_mpl_get_prob_name
+char *mpl_get_prob_name(MPL *mpl);
+/* obtain problem (model) name */
+
+#define mpl_get_num_rows _glp_mpl_get_num_rows
+int mpl_get_num_rows(MPL *mpl);
+/* determine number of rows */
+
+#define mpl_get_num_cols _glp_mpl_get_num_cols
+int mpl_get_num_cols(MPL *mpl);
+/* determine number of columns */
+
+#define mpl_get_row_name _glp_mpl_get_row_name
+char *mpl_get_row_name(MPL *mpl, int i);
+/* obtain row name */
+
+#define mpl_get_row_kind _glp_mpl_get_row_kind
+int mpl_get_row_kind(MPL *mpl, int i);
+/* determine row kind */
+
+#define mpl_get_row_bnds _glp_mpl_get_row_bnds
+int mpl_get_row_bnds(MPL *mpl, int i, double *lb, double *ub);
+/* obtain row bounds */
+
+#define mpl_get_mat_row _glp_mpl_get_mat_row
+int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]);
+/* obtain row of the constraint matrix */
+
+#define mpl_get_row_c0 _glp_mpl_get_row_c0
+double mpl_get_row_c0(MPL *mpl, int i);
+/* obtain constant term of free row */
+
+#define mpl_get_col_name _glp_mpl_get_col_name
+char *mpl_get_col_name(MPL *mpl, int j);
+/* obtain column name */
+
+#define mpl_get_col_kind _glp_mpl_get_col_kind
+int mpl_get_col_kind(MPL *mpl, int j);
+/* determine column kind */
+
+#define mpl_get_col_bnds _glp_mpl_get_col_bnds
+int mpl_get_col_bnds(MPL *mpl, int j, double *lb, double *ub);
+/* obtain column bounds */
+
+#define mpl_has_solve_stmt _glp_mpl_has_solve_stmt
+int mpl_has_solve_stmt(MPL *mpl);
+/* check if model has solve statement */
+
+#if 1 /* 15/V-2010 */
+#define mpl_put_row_soln _glp_mpl_put_row_soln
+void mpl_put_row_soln(MPL *mpl, int i, int stat, double prim,
+ double dual);
+/* store row (constraint/objective) solution components */
+#endif
+
+#if 1 /* 15/V-2010 */
+#define mpl_put_col_soln _glp_mpl_put_col_soln
+void mpl_put_col_soln(MPL *mpl, int j, int stat, double prim,
+ double dual);
+/* store column (variable) solution components */
+#endif
+
+#if 0 /* 15/V-2010 */
+#define mpl_put_col_value _glp_mpl_put_col_value
+void mpl_put_col_value(MPL *mpl, int j, double val);
+/* store column value */
+#endif
+
+#define mpl_postsolve _glp_mpl_postsolve
+int mpl_postsolve(MPL *mpl);
+/* postsolve model */
+
+#define mpl_terminate _glp_mpl_terminate
+void mpl_terminate(MPL *mpl);
+/* free all resources used by translator */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl1.c b/test/monniaux/glpk-4.65/src/mpl/mpl1.c
new file mode 100644
index 00000000..7dc3cd79
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl1.c
@@ -0,0 +1,4718 @@
+/* mpl1.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+
+#define dmp_get_atomv dmp_get_atom
+
+/**********************************************************************/
+/* * * PROCESSING MODEL SECTION * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- enter_context - enter current token into context queue.
+--
+-- This routine enters the current token into the context queue. */
+
+void enter_context(MPL *mpl)
+{ char *image, *s;
+ if (mpl->token == T_EOF)
+ image = "_|_";
+ else if (mpl->token == T_STRING)
+ image = "'...'";
+ else
+ image = mpl->image;
+ xassert(0 <= mpl->c_ptr && mpl->c_ptr < CONTEXT_SIZE);
+ mpl->context[mpl->c_ptr++] = ' ';
+ if (mpl->c_ptr == CONTEXT_SIZE) mpl->c_ptr = 0;
+ for (s = image; *s != '\0'; s++)
+ { mpl->context[mpl->c_ptr++] = *s;
+ if (mpl->c_ptr == CONTEXT_SIZE) mpl->c_ptr = 0;
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- print_context - print current content of context queue.
+--
+-- This routine prints current content of the context queue. */
+
+void print_context(MPL *mpl)
+{ int c;
+ while (mpl->c_ptr > 0)
+ { mpl->c_ptr--;
+ c = mpl->context[0];
+ memmove(mpl->context, mpl->context+1, CONTEXT_SIZE-1);
+ mpl->context[CONTEXT_SIZE-1] = (char)c;
+ }
+ xprintf("Context: %s%.*s\n", mpl->context[0] == ' ' ? "" : "...",
+ CONTEXT_SIZE, mpl->context);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- get_char - scan next character from input text file.
+--
+-- This routine scans a next ASCII character from the input text file.
+-- In case of end-of-file, the character is assigned EOF. */
+
+void get_char(MPL *mpl)
+{ int c;
+ if (mpl->c == EOF) goto done;
+ if (mpl->c == '\n') mpl->line++;
+ c = read_char(mpl);
+ if (c == EOF)
+ { if (mpl->c == '\n')
+ mpl->line--;
+ else
+ warning(mpl, "final NL missing before end of file");
+ }
+ else if (c == '\n')
+ ;
+ else if (isspace(c))
+ c = ' ';
+ else if (iscntrl(c))
+ { enter_context(mpl);
+ error(mpl, "control character 0x%02X not allowed", c);
+ }
+ mpl->c = c;
+done: return;
+}
+
+/*----------------------------------------------------------------------
+-- append_char - append character to current token.
+--
+-- This routine appends the current character to the current token and
+-- then scans a next character. */
+
+void append_char(MPL *mpl)
+{ xassert(0 <= mpl->imlen && mpl->imlen <= MAX_LENGTH);
+ if (mpl->imlen == MAX_LENGTH)
+ { switch (mpl->token)
+ { case T_NAME:
+ enter_context(mpl);
+ error(mpl, "symbolic name %s... too long", mpl->image);
+ case T_SYMBOL:
+ enter_context(mpl);
+ error(mpl, "symbol %s... too long", mpl->image);
+ case T_NUMBER:
+ enter_context(mpl);
+ error(mpl, "numeric literal %s... too long", mpl->image);
+ case T_STRING:
+ enter_context(mpl);
+ error(mpl, "string literal too long");
+ default:
+ xassert(mpl != mpl);
+ }
+ }
+ mpl->image[mpl->imlen++] = (char)mpl->c;
+ mpl->image[mpl->imlen] = '\0';
+ get_char(mpl);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- get_token - scan next token from input text file.
+--
+-- This routine scans a next token from the input text file using the
+-- standard finite automation technique. */
+
+void get_token(MPL *mpl)
+{ /* save the current token */
+ mpl->b_token = mpl->token;
+ mpl->b_imlen = mpl->imlen;
+ strcpy(mpl->b_image, mpl->image);
+ mpl->b_value = mpl->value;
+ /* if the next token is already scanned, make it current */
+ if (mpl->f_scan)
+ { mpl->f_scan = 0;
+ mpl->token = mpl->f_token;
+ mpl->imlen = mpl->f_imlen;
+ strcpy(mpl->image, mpl->f_image);
+ mpl->value = mpl->f_value;
+ goto done;
+ }
+loop: /* nothing has been scanned so far */
+ mpl->token = 0;
+ mpl->imlen = 0;
+ mpl->image[0] = '\0';
+ mpl->value = 0.0;
+ /* skip any uninteresting characters */
+ while (mpl->c == ' ' || mpl->c == '\n') get_char(mpl);
+ /* recognize and construct the token */
+ if (mpl->c == EOF)
+ { /* end-of-file reached */
+ mpl->token = T_EOF;
+ }
+ else if (mpl->c == '#')
+ { /* comment; skip anything until end-of-line */
+ while (mpl->c != '\n' && mpl->c != EOF) get_char(mpl);
+ goto loop;
+ }
+ else if (!mpl->flag_d && (isalpha(mpl->c) || mpl->c == '_'))
+ { /* symbolic name or reserved keyword */
+ mpl->token = T_NAME;
+ while (isalnum(mpl->c) || mpl->c == '_') append_char(mpl);
+ if (strcmp(mpl->image, "and") == 0)
+ mpl->token = T_AND;
+ else if (strcmp(mpl->image, "by") == 0)
+ mpl->token = T_BY;
+ else if (strcmp(mpl->image, "cross") == 0)
+ mpl->token = T_CROSS;
+ else if (strcmp(mpl->image, "diff") == 0)
+ mpl->token = T_DIFF;
+ else if (strcmp(mpl->image, "div") == 0)
+ mpl->token = T_DIV;
+ else if (strcmp(mpl->image, "else") == 0)
+ mpl->token = T_ELSE;
+ else if (strcmp(mpl->image, "if") == 0)
+ mpl->token = T_IF;
+ else if (strcmp(mpl->image, "in") == 0)
+ mpl->token = T_IN;
+#if 1 /* 21/VII-2006 */
+ else if (strcmp(mpl->image, "Infinity") == 0)
+ mpl->token = T_INFINITY;
+#endif
+ else if (strcmp(mpl->image, "inter") == 0)
+ mpl->token = T_INTER;
+ else if (strcmp(mpl->image, "less") == 0)
+ mpl->token = T_LESS;
+ else if (strcmp(mpl->image, "mod") == 0)
+ mpl->token = T_MOD;
+ else if (strcmp(mpl->image, "not") == 0)
+ mpl->token = T_NOT;
+ else if (strcmp(mpl->image, "or") == 0)
+ mpl->token = T_OR;
+ else if (strcmp(mpl->image, "s") == 0 && mpl->c == '.')
+ { mpl->token = T_SPTP;
+ append_char(mpl);
+ if (mpl->c != 't')
+sptp: { enter_context(mpl);
+ error(mpl, "keyword s.t. incomplete");
+ }
+ append_char(mpl);
+ if (mpl->c != '.') goto sptp;
+ append_char(mpl);
+ }
+ else if (strcmp(mpl->image, "symdiff") == 0)
+ mpl->token = T_SYMDIFF;
+ else if (strcmp(mpl->image, "then") == 0)
+ mpl->token = T_THEN;
+ else if (strcmp(mpl->image, "union") == 0)
+ mpl->token = T_UNION;
+ else if (strcmp(mpl->image, "within") == 0)
+ mpl->token = T_WITHIN;
+ }
+ else if (!mpl->flag_d && isdigit(mpl->c))
+ { /* numeric literal */
+ mpl->token = T_NUMBER;
+ /* scan integer part */
+ while (isdigit(mpl->c)) append_char(mpl);
+ /* scan optional fractional part */
+ if (mpl->c == '.')
+ { append_char(mpl);
+ if (mpl->c == '.')
+ { /* hmm, it is not the fractional part, it is dots that
+ follow the integer part */
+ mpl->imlen--;
+ mpl->image[mpl->imlen] = '\0';
+ mpl->f_dots = 1;
+ goto conv;
+ }
+frac: while (isdigit(mpl->c)) append_char(mpl);
+ }
+ /* scan optional decimal exponent */
+ if (mpl->c == 'e' || mpl->c == 'E')
+ { append_char(mpl);
+ if (mpl->c == '+' || mpl->c == '-') append_char(mpl);
+ if (!isdigit(mpl->c))
+ { enter_context(mpl);
+ error(mpl, "numeric literal %s incomplete", mpl->image);
+ }
+ while (isdigit(mpl->c)) append_char(mpl);
+ }
+ /* there must be no letter following the numeric literal */
+ if (isalpha(mpl->c) || mpl->c == '_')
+ { enter_context(mpl);
+ error(mpl, "symbol %s%c... should be enclosed in quotes",
+ mpl->image, mpl->c);
+ }
+conv: /* convert numeric literal to floating-point */
+ if (str2num(mpl->image, &mpl->value))
+err: { enter_context(mpl);
+ error(mpl, "cannot convert numeric literal %s to floating-p"
+ "oint number", mpl->image);
+ }
+ }
+ else if (mpl->c == '\'' || mpl->c == '"')
+ { /* character string */
+ int quote = mpl->c;
+ mpl->token = T_STRING;
+ get_char(mpl);
+ for (;;)
+ { if (mpl->c == '\n' || mpl->c == EOF)
+ { enter_context(mpl);
+ error(mpl, "unexpected end of line; string literal incom"
+ "plete");
+ }
+ if (mpl->c == quote)
+ { get_char(mpl);
+ if (mpl->c != quote) break;
+ }
+ append_char(mpl);
+ }
+ }
+ else if (!mpl->flag_d && mpl->c == '+')
+ mpl->token = T_PLUS, append_char(mpl);
+ else if (!mpl->flag_d && mpl->c == '-')
+ mpl->token = T_MINUS, append_char(mpl);
+ else if (mpl->c == '*')
+ { mpl->token = T_ASTERISK, append_char(mpl);
+ if (mpl->c == '*')
+ mpl->token = T_POWER, append_char(mpl);
+ }
+ else if (mpl->c == '/')
+ { mpl->token = T_SLASH, append_char(mpl);
+ if (mpl->c == '*')
+ { /* comment sequence */
+ get_char(mpl);
+ for (;;)
+ { if (mpl->c == EOF)
+ { /* do not call enter_context at this point */
+ error(mpl, "unexpected end of file; comment sequence "
+ "incomplete");
+ }
+ else if (mpl->c == '*')
+ { get_char(mpl);
+ if (mpl->c == '/') break;
+ }
+ else
+ get_char(mpl);
+ }
+ get_char(mpl);
+ goto loop;
+ }
+ }
+ else if (mpl->c == '^')
+ mpl->token = T_POWER, append_char(mpl);
+ else if (mpl->c == '<')
+ { mpl->token = T_LT, append_char(mpl);
+ if (mpl->c == '=')
+ mpl->token = T_LE, append_char(mpl);
+ else if (mpl->c == '>')
+ mpl->token = T_NE, append_char(mpl);
+#if 1 /* 11/II-2008 */
+ else if (mpl->c == '-')
+ mpl->token = T_INPUT, append_char(mpl);
+#endif
+ }
+ else if (mpl->c == '=')
+ { mpl->token = T_EQ, append_char(mpl);
+ if (mpl->c == '=') append_char(mpl);
+ }
+ else if (mpl->c == '>')
+ { mpl->token = T_GT, append_char(mpl);
+ if (mpl->c == '=')
+ mpl->token = T_GE, append_char(mpl);
+#if 1 /* 14/VII-2006 */
+ else if (mpl->c == '>')
+ mpl->token = T_APPEND, append_char(mpl);
+#endif
+ }
+ else if (mpl->c == '!')
+ { mpl->token = T_NOT, append_char(mpl);
+ if (mpl->c == '=')
+ mpl->token = T_NE, append_char(mpl);
+ }
+ else if (mpl->c == '&')
+ { mpl->token = T_CONCAT, append_char(mpl);
+ if (mpl->c == '&')
+ mpl->token = T_AND, append_char(mpl);
+ }
+ else if (mpl->c == '|')
+ { mpl->token = T_BAR, append_char(mpl);
+ if (mpl->c == '|')
+ mpl->token = T_OR, append_char(mpl);
+ }
+ else if (!mpl->flag_d && mpl->c == '.')
+ { mpl->token = T_POINT, append_char(mpl);
+ if (mpl->f_dots)
+ { /* dots; the first dot was read on the previous call to the
+ scanner, so the current character is the second dot */
+ mpl->token = T_DOTS;
+ mpl->imlen = 2;
+ strcpy(mpl->image, "..");
+ mpl->f_dots = 0;
+ }
+ else if (mpl->c == '.')
+ mpl->token = T_DOTS, append_char(mpl);
+ else if (isdigit(mpl->c))
+ { /* numeric literal that begins with the decimal point */
+ mpl->token = T_NUMBER, append_char(mpl);
+ goto frac;
+ }
+ }
+ else if (mpl->c == ',')
+ mpl->token = T_COMMA, append_char(mpl);
+ else if (mpl->c == ':')
+ { mpl->token = T_COLON, append_char(mpl);
+ if (mpl->c == '=')
+ mpl->token = T_ASSIGN, append_char(mpl);
+ }
+ else if (mpl->c == ';')
+ mpl->token = T_SEMICOLON, append_char(mpl);
+ else if (mpl->c == '(')
+ mpl->token = T_LEFT, append_char(mpl);
+ else if (mpl->c == ')')
+ mpl->token = T_RIGHT, append_char(mpl);
+ else if (mpl->c == '[')
+ mpl->token = T_LBRACKET, append_char(mpl);
+ else if (mpl->c == ']')
+ mpl->token = T_RBRACKET, append_char(mpl);
+ else if (mpl->c == '{')
+ mpl->token = T_LBRACE, append_char(mpl);
+ else if (mpl->c == '}')
+ mpl->token = T_RBRACE, append_char(mpl);
+#if 1 /* 11/II-2008 */
+ else if (mpl->c == '~')
+ mpl->token = T_TILDE, append_char(mpl);
+#endif
+ else if (isalnum(mpl->c) || strchr("+-._", mpl->c) != NULL)
+ { /* symbol */
+ xassert(mpl->flag_d);
+ mpl->token = T_SYMBOL;
+ while (isalnum(mpl->c) || strchr("+-._", mpl->c) != NULL)
+ append_char(mpl);
+ switch (str2num(mpl->image, &mpl->value))
+ { case 0:
+ mpl->token = T_NUMBER;
+ break;
+ case 1:
+ goto err;
+ case 2:
+ break;
+ default:
+ xassert(mpl != mpl);
+ }
+ }
+ else
+ { enter_context(mpl);
+ error(mpl, "character %c not allowed", mpl->c);
+ }
+ /* enter the current token into the context queue */
+ enter_context(mpl);
+ /* reset the flag, which may be set by indexing_expression() and
+ is used by expression_list() */
+ mpl->flag_x = 0;
+done: return;
+}
+
+/*----------------------------------------------------------------------
+-- unget_token - return current token back to input stream.
+--
+-- This routine returns the current token back to the input stream, so
+-- the previously scanned token becomes the current one. */
+
+void unget_token(MPL *mpl)
+{ /* save the current token, which becomes the next one */
+ xassert(!mpl->f_scan);
+ mpl->f_scan = 1;
+ mpl->f_token = mpl->token;
+ mpl->f_imlen = mpl->imlen;
+ strcpy(mpl->f_image, mpl->image);
+ mpl->f_value = mpl->value;
+ /* restore the previous token, which becomes the current one */
+ mpl->token = mpl->b_token;
+ mpl->imlen = mpl->b_imlen;
+ strcpy(mpl->image, mpl->b_image);
+ mpl->value = mpl->b_value;
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- is_keyword - check if current token is given non-reserved keyword.
+--
+-- If the current token is given (non-reserved) keyword, this routine
+-- returns non-zero. Otherwise zero is returned. */
+
+int is_keyword(MPL *mpl, char *keyword)
+{ return
+ mpl->token == T_NAME && strcmp(mpl->image, keyword) == 0;
+}
+
+/*----------------------------------------------------------------------
+-- is_reserved - check if current token is reserved keyword.
+--
+-- If the current token is a reserved keyword, this routine returns
+-- non-zero. Otherwise zero is returned. */
+
+int is_reserved(MPL *mpl)
+{ return
+ mpl->token == T_AND && mpl->image[0] == 'a' ||
+ mpl->token == T_BY ||
+ mpl->token == T_CROSS ||
+ mpl->token == T_DIFF ||
+ mpl->token == T_DIV ||
+ mpl->token == T_ELSE ||
+ mpl->token == T_IF ||
+ mpl->token == T_IN ||
+ mpl->token == T_INTER ||
+ mpl->token == T_LESS ||
+ mpl->token == T_MOD ||
+ mpl->token == T_NOT && mpl->image[0] == 'n' ||
+ mpl->token == T_OR && mpl->image[0] == 'o' ||
+ mpl->token == T_SYMDIFF ||
+ mpl->token == T_THEN ||
+ mpl->token == T_UNION ||
+ mpl->token == T_WITHIN;
+}
+
+/*----------------------------------------------------------------------
+-- make_code - generate pseudo-code (basic routine).
+--
+-- This routine generates specified pseudo-code. It is assumed that all
+-- other translator routines use this basic routine. */
+
+CODE *make_code(MPL *mpl, int op, OPERANDS *arg, int type, int dim)
+{ CODE *code;
+ DOMAIN *domain;
+ DOMAIN_BLOCK *block;
+ ARG_LIST *e;
+ /* generate pseudo-code */
+ code = alloc(CODE);
+ code->op = op;
+ code->vflag = 0; /* is inherited from operand(s) */
+ /* copy operands and also make them referring to the pseudo-code
+ being generated, because the latter becomes the parent for all
+ its operands */
+ memset(&code->arg, '?', sizeof(OPERANDS));
+ switch (op)
+ { case O_NUMBER:
+ code->arg.num = arg->num;
+ break;
+ case O_STRING:
+ code->arg.str = arg->str;
+ break;
+ case O_INDEX:
+ code->arg.index.slot = arg->index.slot;
+ code->arg.index.next = arg->index.next;
+ break;
+ case O_MEMNUM:
+ case O_MEMSYM:
+ for (e = arg->par.list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.par.par = arg->par.par;
+ code->arg.par.list = arg->par.list;
+ break;
+ case O_MEMSET:
+ for (e = arg->set.list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.set.set = arg->set.set;
+ code->arg.set.list = arg->set.list;
+ break;
+ case O_MEMVAR:
+ for (e = arg->var.list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.var.var = arg->var.var;
+ code->arg.var.list = arg->var.list;
+#if 1 /* 15/V-2010 */
+ code->arg.var.suff = arg->var.suff;
+#endif
+ break;
+#if 1 /* 15/V-2010 */
+ case O_MEMCON:
+ for (e = arg->con.list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.con.con = arg->con.con;
+ code->arg.con.list = arg->con.list;
+ code->arg.con.suff = arg->con.suff;
+ break;
+#endif
+ case O_TUPLE:
+ case O_MAKE:
+ for (e = arg->list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.list = arg->list;
+ break;
+ case O_SLICE:
+ xassert(arg->slice != NULL);
+ code->arg.slice = arg->slice;
+ break;
+ case O_IRAND224:
+ case O_UNIFORM01:
+ case O_NORMAL01:
+ case O_GMTIME:
+ code->vflag = 1;
+ break;
+ case O_CVTNUM:
+ case O_CVTSYM:
+ case O_CVTLOG:
+ case O_CVTTUP:
+ case O_CVTLFM:
+ case O_PLUS:
+ case O_MINUS:
+ case O_NOT:
+ case O_ABS:
+ case O_CEIL:
+ case O_FLOOR:
+ case O_EXP:
+ case O_LOG:
+ case O_LOG10:
+ case O_SQRT:
+ case O_SIN:
+ case O_COS:
+ case O_TAN:
+ case O_ATAN:
+ case O_ROUND:
+ case O_TRUNC:
+ case O_CARD:
+ case O_LENGTH:
+ /* unary operation */
+ xassert(arg->arg.x != NULL);
+ xassert(arg->arg.x->up == NULL);
+ arg->arg.x->up = code;
+ code->vflag |= arg->arg.x->vflag;
+ code->arg.arg.x = arg->arg.x;
+ break;
+ case O_ADD:
+ case O_SUB:
+ case O_LESS:
+ case O_MUL:
+ case O_DIV:
+ case O_IDIV:
+ case O_MOD:
+ case O_POWER:
+ case O_ATAN2:
+ case O_ROUND2:
+ case O_TRUNC2:
+ case O_UNIFORM:
+ if (op == O_UNIFORM) code->vflag = 1;
+ case O_NORMAL:
+ if (op == O_NORMAL) code->vflag = 1;
+ case O_CONCAT:
+ case O_LT:
+ case O_LE:
+ case O_EQ:
+ case O_GE:
+ case O_GT:
+ case O_NE:
+ case O_AND:
+ case O_OR:
+ case O_UNION:
+ case O_DIFF:
+ case O_SYMDIFF:
+ case O_INTER:
+ case O_CROSS:
+ case O_IN:
+ case O_NOTIN:
+ case O_WITHIN:
+ case O_NOTWITHIN:
+ case O_SUBSTR:
+ case O_STR2TIME:
+ case O_TIME2STR:
+ /* binary operation */
+ xassert(arg->arg.x != NULL);
+ xassert(arg->arg.x->up == NULL);
+ arg->arg.x->up = code;
+ code->vflag |= arg->arg.x->vflag;
+ xassert(arg->arg.y != NULL);
+ xassert(arg->arg.y->up == NULL);
+ arg->arg.y->up = code;
+ code->vflag |= arg->arg.y->vflag;
+ code->arg.arg.x = arg->arg.x;
+ code->arg.arg.y = arg->arg.y;
+ break;
+ case O_DOTS:
+ case O_FORK:
+ case O_SUBSTR3:
+ /* ternary operation */
+ xassert(arg->arg.x != NULL);
+ xassert(arg->arg.x->up == NULL);
+ arg->arg.x->up = code;
+ code->vflag |= arg->arg.x->vflag;
+ xassert(arg->arg.y != NULL);
+ xassert(arg->arg.y->up == NULL);
+ arg->arg.y->up = code;
+ code->vflag |= arg->arg.y->vflag;
+ if (arg->arg.z != NULL)
+ { xassert(arg->arg.z->up == NULL);
+ arg->arg.z->up = code;
+ code->vflag |= arg->arg.z->vflag;
+ }
+ code->arg.arg.x = arg->arg.x;
+ code->arg.arg.y = arg->arg.y;
+ code->arg.arg.z = arg->arg.z;
+ break;
+ case O_MIN:
+ case O_MAX:
+ /* n-ary operation */
+ for (e = arg->list; e != NULL; e = e->next)
+ { xassert(e->x != NULL);
+ xassert(e->x->up == NULL);
+ e->x->up = code;
+ code->vflag |= e->x->vflag;
+ }
+ code->arg.list = arg->list;
+ break;
+ case O_SUM:
+ case O_PROD:
+ case O_MINIMUM:
+ case O_MAXIMUM:
+ case O_FORALL:
+ case O_EXISTS:
+ case O_SETOF:
+ case O_BUILD:
+ /* iterated operation */
+ domain = arg->loop.domain;
+ xassert(domain != NULL);
+ if (domain->code != NULL)
+ { xassert(domain->code->up == NULL);
+ domain->code->up = code;
+ code->vflag |= domain->code->vflag;
+ }
+ for (block = domain->list; block != NULL; block =
+ block->next)
+ { xassert(block->code != NULL);
+ xassert(block->code->up == NULL);
+ block->code->up = code;
+ code->vflag |= block->code->vflag;
+ }
+ if (arg->loop.x != NULL)
+ { xassert(arg->loop.x->up == NULL);
+ arg->loop.x->up = code;
+ code->vflag |= arg->loop.x->vflag;
+ }
+ code->arg.loop.domain = arg->loop.domain;
+ code->arg.loop.x = arg->loop.x;
+ break;
+ default:
+ xassert(op != op);
+ }
+ /* set other attributes of the pseudo-code */
+ code->type = type;
+ code->dim = dim;
+ code->up = NULL;
+ code->valid = 0;
+ memset(&code->value, '?', sizeof(VALUE));
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- make_unary - generate pseudo-code for unary operation.
+--
+-- This routine generates pseudo-code for unary operation. */
+
+CODE *make_unary(MPL *mpl, int op, CODE *x, int type, int dim)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(x != NULL);
+ arg.arg.x = x;
+ code = make_code(mpl, op, &arg, type, dim);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- make_binary - generate pseudo-code for binary operation.
+--
+-- This routine generates pseudo-code for binary operation. */
+
+CODE *make_binary(MPL *mpl, int op, CODE *x, CODE *y, int type,
+ int dim)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(x != NULL);
+ xassert(y != NULL);
+ arg.arg.x = x;
+ arg.arg.y = y;
+ code = make_code(mpl, op, &arg, type, dim);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- make_ternary - generate pseudo-code for ternary operation.
+--
+-- This routine generates pseudo-code for ternary operation. */
+
+CODE *make_ternary(MPL *mpl, int op, CODE *x, CODE *y, CODE *z,
+ int type, int dim)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(x != NULL);
+ xassert(y != NULL);
+ /* third operand can be NULL */
+ arg.arg.x = x;
+ arg.arg.y = y;
+ arg.arg.z = z;
+ code = make_code(mpl, op, &arg, type, dim);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- numeric_literal - parse reference to numeric literal.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <numeric literal> */
+
+CODE *numeric_literal(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(mpl->token == T_NUMBER);
+ arg.num = mpl->value;
+ code = make_code(mpl, O_NUMBER, &arg, A_NUMERIC, 0);
+ get_token(mpl /* <numeric literal> */);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- string_literal - parse reference to string literal.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <string literal> */
+
+CODE *string_literal(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(mpl->token == T_STRING);
+ arg.str = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(arg.str, mpl->image);
+ code = make_code(mpl, O_STRING, &arg, A_SYMBOLIC, 0);
+ get_token(mpl /* <string literal> */);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- create_arg_list - create empty operands list.
+--
+-- This routine creates operands list, which is initially empty. */
+
+ARG_LIST *create_arg_list(MPL *mpl)
+{ ARG_LIST *list;
+ xassert(mpl == mpl);
+ list = NULL;
+ return list;
+}
+
+/*----------------------------------------------------------------------
+-- expand_arg_list - append operand to operands list.
+--
+-- This routine appends new operand to specified operands list. */
+
+ARG_LIST *expand_arg_list(MPL *mpl, ARG_LIST *list, CODE *x)
+{ ARG_LIST *tail, *temp;
+ xassert(x != NULL);
+ /* create new operands list entry */
+ tail = alloc(ARG_LIST);
+ tail->x = x;
+ tail->next = NULL;
+ /* and append it to the operands list */
+ if (list == NULL)
+ list = tail;
+ else
+ { for (temp = list; temp->next != NULL; temp = temp->next);
+ temp->next = tail;
+ }
+ return list;
+}
+
+/*----------------------------------------------------------------------
+-- arg_list_len - determine length of operands list.
+--
+-- This routine returns the number of operands in operands list. */
+
+int arg_list_len(MPL *mpl, ARG_LIST *list)
+{ ARG_LIST *temp;
+ int len;
+ xassert(mpl == mpl);
+ len = 0;
+ for (temp = list; temp != NULL; temp = temp->next) len++;
+ return len;
+}
+
+/*----------------------------------------------------------------------
+-- subscript_list - parse subscript list.
+--
+-- This routine parses subscript list using the syntax:
+--
+-- <subscript list> ::= <subscript>
+-- <subscript list> ::= <subscript list> , <subscript>
+-- <subscript> ::= <expression 5> */
+
+ARG_LIST *subscript_list(MPL *mpl)
+{ ARG_LIST *list;
+ CODE *x;
+ list = create_arg_list(mpl);
+ for (;;)
+ { /* parse subscript expression */
+ x = expression_5(mpl);
+ /* convert it to symbolic type, if necessary */
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
+ /* check that now the expression is of symbolic type */
+ if (x->type != A_SYMBOLIC)
+ error(mpl, "subscript expression has invalid type");
+ xassert(x->dim == 0);
+ /* and append it to the subscript list */
+ list = expand_arg_list(mpl, list, x);
+ /* check a token that follows the subscript expression */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RBRACKET)
+ break;
+ else
+ error(mpl, "syntax error in subscript list");
+ }
+ return list;
+}
+
+#if 1 /* 15/V-2010 */
+/*----------------------------------------------------------------------
+-- object_reference - parse reference to named object.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <dummy index>
+-- <primary expression> ::= <set name>
+-- <primary expression> ::= <set name> [ <subscript list> ]
+-- <primary expression> ::= <parameter name>
+-- <primary expression> ::= <parameter name> [ <subscript list> ]
+-- <primary expression> ::= <variable name> <suffix>
+-- <primary expression> ::= <variable name> [ <subscript list> ]
+-- <suffix>
+-- <primary expression> ::= <constraint name> <suffix>
+-- <primary expression> ::= <constraint name> [ <subscript list> ]
+-- <suffix>
+-- <dummy index> ::= <symbolic name>
+-- <set name> ::= <symbolic name>
+-- <parameter name> ::= <symbolic name>
+-- <variable name> ::= <symbolic name>
+-- <constraint name> ::= <symbolic name>
+-- <suffix> ::= <empty> | .lb | .ub | .status | .val | .dual */
+
+CODE *object_reference(MPL *mpl)
+{ AVLNODE *node;
+ DOMAIN_SLOT *slot;
+ SET *set;
+ PARAMETER *par;
+ VARIABLE *var;
+ CONSTRAINT *con;
+ ARG_LIST *list;
+ OPERANDS arg;
+ CODE *code;
+ char *name;
+ int dim, suff;
+ /* find the object in the symbolic name table */
+ xassert(mpl->token == T_NAME);
+ node = avl_find_node(mpl->tree, mpl->image);
+ if (node == NULL)
+ error(mpl, "%s not defined", mpl->image);
+ /* check the object type and obtain its dimension */
+ switch (avl_get_node_type(node))
+ { case A_INDEX:
+ /* dummy index */
+ slot = (DOMAIN_SLOT *)avl_get_node_link(node);
+ name = slot->name;
+ dim = 0;
+ break;
+ case A_SET:
+ /* model set */
+ set = (SET *)avl_get_node_link(node);
+ name = set->name;
+ dim = set->dim;
+ /* if a set object is referenced in its own declaration and
+ the dimen attribute is not specified yet, use dimen 1 by
+ default */
+ if (set->dimen == 0) set->dimen = 1;
+ break;
+ case A_PARAMETER:
+ /* model parameter */
+ par = (PARAMETER *)avl_get_node_link(node);
+ name = par->name;
+ dim = par->dim;
+ break;
+ case A_VARIABLE:
+ /* model variable */
+ var = (VARIABLE *)avl_get_node_link(node);
+ name = var->name;
+ dim = var->dim;
+ break;
+ case A_CONSTRAINT:
+ /* model constraint or objective */
+ con = (CONSTRAINT *)avl_get_node_link(node);
+ name = con->name;
+ dim = con->dim;
+ break;
+ default:
+ xassert(node != node);
+ }
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional subscript list */
+ if (mpl->token == T_LBRACKET)
+ { /* subscript list is specified */
+ if (dim == 0)
+ error(mpl, "%s cannot be subscripted", name);
+ get_token(mpl /* [ */);
+ list = subscript_list(mpl);
+ if (dim != arg_list_len(mpl, list))
+ error(mpl, "%s must have %d subscript%s rather than %d",
+ name, dim, dim == 1 ? "" : "s", arg_list_len(mpl, list));
+ xassert(mpl->token == T_RBRACKET);
+ get_token(mpl /* ] */);
+ }
+ else
+ { /* subscript list is not specified */
+ if (dim != 0)
+ error(mpl, "%s must be subscripted", name);
+ list = create_arg_list(mpl);
+ }
+ /* parse optional suffix */
+ if (!mpl->flag_s && avl_get_node_type(node) == A_VARIABLE)
+ suff = DOT_NONE;
+ else
+ suff = DOT_VAL;
+ if (mpl->token == T_POINT)
+ { get_token(mpl /* . */);
+ if (mpl->token != T_NAME)
+ error(mpl, "invalid use of period");
+ if (!(avl_get_node_type(node) == A_VARIABLE ||
+ avl_get_node_type(node) == A_CONSTRAINT))
+ error(mpl, "%s cannot have a suffix", name);
+ if (strcmp(mpl->image, "lb") == 0)
+ suff = DOT_LB;
+ else if (strcmp(mpl->image, "ub") == 0)
+ suff = DOT_UB;
+ else if (strcmp(mpl->image, "status") == 0)
+ suff = DOT_STATUS;
+ else if (strcmp(mpl->image, "val") == 0)
+ suff = DOT_VAL;
+ else if (strcmp(mpl->image, "dual") == 0)
+ suff = DOT_DUAL;
+ else
+ error(mpl, "suffix .%s invalid", mpl->image);
+ get_token(mpl /* suffix */);
+ }
+ /* generate pseudo-code to take value of the object */
+ switch (avl_get_node_type(node))
+ { case A_INDEX:
+ arg.index.slot = slot;
+ arg.index.next = slot->list;
+ code = make_code(mpl, O_INDEX, &arg, A_SYMBOLIC, 0);
+ slot->list = code;
+ break;
+ case A_SET:
+ arg.set.set = set;
+ arg.set.list = list;
+ code = make_code(mpl, O_MEMSET, &arg, A_ELEMSET,
+ set->dimen);
+ break;
+ case A_PARAMETER:
+ arg.par.par = par;
+ arg.par.list = list;
+ if (par->type == A_SYMBOLIC)
+ code = make_code(mpl, O_MEMSYM, &arg, A_SYMBOLIC, 0);
+ else
+ code = make_code(mpl, O_MEMNUM, &arg, A_NUMERIC, 0);
+ break;
+ case A_VARIABLE:
+ if (!mpl->flag_s && (suff == DOT_STATUS || suff == DOT_VAL
+ || suff == DOT_DUAL))
+ error(mpl, "invalid reference to status, primal value, o"
+ "r dual value of variable %s above solve statement",
+ var->name);
+ arg.var.var = var;
+ arg.var.list = list;
+ arg.var.suff = suff;
+ code = make_code(mpl, O_MEMVAR, &arg, suff == DOT_NONE ?
+ A_FORMULA : A_NUMERIC, 0);
+ break;
+ case A_CONSTRAINT:
+ if (!mpl->flag_s && (suff == DOT_STATUS || suff == DOT_VAL
+ || suff == DOT_DUAL))
+ error(mpl, "invalid reference to status, primal value, o"
+ "r dual value of %s %s above solve statement",
+ con->type == A_CONSTRAINT ? "constraint" : "objective"
+ , con->name);
+ arg.con.con = con;
+ arg.con.list = list;
+ arg.con.suff = suff;
+ code = make_code(mpl, O_MEMCON, &arg, A_NUMERIC, 0);
+ break;
+ default:
+ xassert(node != node);
+ }
+ return code;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- numeric_argument - parse argument passed to built-in function.
+--
+-- This routine parses an argument passed to numeric built-in function
+-- using the syntax:
+--
+-- <arg> ::= <expression 5> */
+
+CODE *numeric_argument(MPL *mpl, char *func)
+{ CODE *x;
+ x = expression_5(mpl);
+ /* convert the argument to numeric type, if necessary */
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ /* check that now the argument is of numeric type */
+ if (x->type != A_NUMERIC)
+ error(mpl, "argument for %s has invalid type", func);
+ xassert(x->dim == 0);
+ return x;
+}
+
+#if 1 /* 15/VII-2006 */
+CODE *symbolic_argument(MPL *mpl, char *func)
+{ CODE *x;
+ x = expression_5(mpl);
+ /* convert the argument to symbolic type, if necessary */
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
+ /* check that now the argument is of symbolic type */
+ if (x->type != A_SYMBOLIC)
+ error(mpl, "argument for %s has invalid type", func);
+ xassert(x->dim == 0);
+ return x;
+}
+#endif
+
+#if 1 /* 15/VII-2006 */
+CODE *elemset_argument(MPL *mpl, char *func)
+{ CODE *x;
+ x = expression_9(mpl);
+ if (x->type != A_ELEMSET)
+ error(mpl, "argument for %s has invalid type", func);
+ xassert(x->dim > 0);
+ return x;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- function_reference - parse reference to built-in function.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= abs ( <arg> )
+-- <primary expression> ::= ceil ( <arg> )
+-- <primary expression> ::= floor ( <arg> )
+-- <primary expression> ::= exp ( <arg> )
+-- <primary expression> ::= log ( <arg> )
+-- <primary expression> ::= log10 ( <arg> )
+-- <primary expression> ::= max ( <arg list> )
+-- <primary expression> ::= min ( <arg list> )
+-- <primary expression> ::= sqrt ( <arg> )
+-- <primary expression> ::= sin ( <arg> )
+-- <primary expression> ::= cos ( <arg> )
+-- <primary expression> ::= tan ( <arg> )
+-- <primary expression> ::= atan ( <arg> )
+-- <primary expression> ::= atan2 ( <arg> , <arg> )
+-- <primary expression> ::= round ( <arg> )
+-- <primary expression> ::= round ( <arg> , <arg> )
+-- <primary expression> ::= trunc ( <arg> )
+-- <primary expression> ::= trunc ( <arg> , <arg> )
+-- <primary expression> ::= Irand224 ( )
+-- <primary expression> ::= Uniform01 ( )
+-- <primary expression> ::= Uniform ( <arg> , <arg> )
+-- <primary expression> ::= Normal01 ( )
+-- <primary expression> ::= Normal ( <arg> , <arg> )
+-- <primary expression> ::= card ( <arg> )
+-- <primary expression> ::= length ( <arg> )
+-- <primary expression> ::= substr ( <arg> , <arg> )
+-- <primary expression> ::= substr ( <arg> , <arg> , <arg> )
+-- <primary expression> ::= str2time ( <arg> , <arg> )
+-- <primary expression> ::= time2str ( <arg> , <arg> )
+-- <primary expression> ::= gmtime ( )
+-- <arg list> ::= <arg>
+-- <arg list> ::= <arg list> , <arg> */
+
+CODE *function_reference(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ int op;
+ char func[15+1];
+ /* determine operation code */
+ xassert(mpl->token == T_NAME);
+ if (strcmp(mpl->image, "abs") == 0)
+ op = O_ABS;
+ else if (strcmp(mpl->image, "ceil") == 0)
+ op = O_CEIL;
+ else if (strcmp(mpl->image, "floor") == 0)
+ op = O_FLOOR;
+ else if (strcmp(mpl->image, "exp") == 0)
+ op = O_EXP;
+ else if (strcmp(mpl->image, "log") == 0)
+ op = O_LOG;
+ else if (strcmp(mpl->image, "log10") == 0)
+ op = O_LOG10;
+ else if (strcmp(mpl->image, "sqrt") == 0)
+ op = O_SQRT;
+ else if (strcmp(mpl->image, "sin") == 0)
+ op = O_SIN;
+ else if (strcmp(mpl->image, "cos") == 0)
+ op = O_COS;
+ else if (strcmp(mpl->image, "tan") == 0)
+ op = O_TAN;
+ else if (strcmp(mpl->image, "atan") == 0)
+ op = O_ATAN;
+ else if (strcmp(mpl->image, "min") == 0)
+ op = O_MIN;
+ else if (strcmp(mpl->image, "max") == 0)
+ op = O_MAX;
+ else if (strcmp(mpl->image, "round") == 0)
+ op = O_ROUND;
+ else if (strcmp(mpl->image, "trunc") == 0)
+ op = O_TRUNC;
+ else if (strcmp(mpl->image, "Irand224") == 0)
+ op = O_IRAND224;
+ else if (strcmp(mpl->image, "Uniform01") == 0)
+ op = O_UNIFORM01;
+ else if (strcmp(mpl->image, "Uniform") == 0)
+ op = O_UNIFORM;
+ else if (strcmp(mpl->image, "Normal01") == 0)
+ op = O_NORMAL01;
+ else if (strcmp(mpl->image, "Normal") == 0)
+ op = O_NORMAL;
+ else if (strcmp(mpl->image, "card") == 0)
+ op = O_CARD;
+ else if (strcmp(mpl->image, "length") == 0)
+ op = O_LENGTH;
+ else if (strcmp(mpl->image, "substr") == 0)
+ op = O_SUBSTR;
+ else if (strcmp(mpl->image, "str2time") == 0)
+ op = O_STR2TIME;
+ else if (strcmp(mpl->image, "time2str") == 0)
+ op = O_TIME2STR;
+ else if (strcmp(mpl->image, "gmtime") == 0)
+ op = O_GMTIME;
+ else
+ error(mpl, "function %s unknown", mpl->image);
+ /* save symbolic name of the function */
+ strcpy(func, mpl->image);
+ xassert(strlen(func) < sizeof(func));
+ get_token(mpl /* <symbolic name> */);
+ /* check the left parenthesis that follows the function name */
+ xassert(mpl->token == T_LEFT);
+ get_token(mpl /* ( */);
+ /* parse argument list */
+ if (op == O_MIN || op == O_MAX)
+ { /* min and max allow arbitrary number of arguments */
+ arg.list = create_arg_list(mpl);
+ /* parse argument list */
+ for (;;)
+ { /* parse argument and append it to the operands list */
+ arg.list = expand_arg_list(mpl, arg.list,
+ numeric_argument(mpl, func));
+ /* check a token that follows the argument */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RIGHT)
+ break;
+ else
+ error(mpl, "syntax error in argument list for %s", func);
+ }
+ }
+ else if (op == O_IRAND224 || op == O_UNIFORM01 || op ==
+ O_NORMAL01 || op == O_GMTIME)
+ { /* Irand224, Uniform01, Normal01, gmtime need no arguments */
+ if (mpl->token != T_RIGHT)
+ error(mpl, "%s needs no arguments", func);
+ }
+ else if (op == O_UNIFORM || op == O_NORMAL)
+ { /* Uniform and Normal need two arguments */
+ /* parse the first argument */
+ arg.arg.x = numeric_argument(mpl, func);
+ /* check a token that follows the first argument */
+ if (mpl->token == T_COMMA)
+ ;
+ else if (mpl->token == T_RIGHT)
+ error(mpl, "%s needs two arguments", func);
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ get_token(mpl /* , */);
+ /* parse the second argument */
+ arg.arg.y = numeric_argument(mpl, func);
+ /* check a token that follows the second argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs two argument", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ else if (op == O_ATAN || op == O_ROUND || op == O_TRUNC)
+ { /* atan, round, and trunc need one or two arguments */
+ /* parse the first argument */
+ arg.arg.x = numeric_argument(mpl, func);
+ /* parse the second argument, if specified */
+ if (mpl->token == T_COMMA)
+ { switch (op)
+ { case O_ATAN: op = O_ATAN2; break;
+ case O_ROUND: op = O_ROUND2; break;
+ case O_TRUNC: op = O_TRUNC2; break;
+ default: xassert(op != op);
+ }
+ get_token(mpl /* , */);
+ arg.arg.y = numeric_argument(mpl, func);
+ }
+ /* check a token that follows the last argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs one or two arguments", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ else if (op == O_SUBSTR)
+ { /* substr needs two or three arguments */
+ /* parse the first argument */
+ arg.arg.x = symbolic_argument(mpl, func);
+ /* check a token that follows the first argument */
+ if (mpl->token == T_COMMA)
+ ;
+ else if (mpl->token == T_RIGHT)
+ error(mpl, "%s needs two or three arguments", func);
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ get_token(mpl /* , */);
+ /* parse the second argument */
+ arg.arg.y = numeric_argument(mpl, func);
+ /* parse the third argument, if specified */
+ if (mpl->token == T_COMMA)
+ { op = O_SUBSTR3;
+ get_token(mpl /* , */);
+ arg.arg.z = numeric_argument(mpl, func);
+ }
+ /* check a token that follows the last argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs two or three arguments", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ else if (op == O_STR2TIME)
+ { /* str2time needs two arguments, both symbolic */
+ /* parse the first argument */
+ arg.arg.x = symbolic_argument(mpl, func);
+ /* check a token that follows the first argument */
+ if (mpl->token == T_COMMA)
+ ;
+ else if (mpl->token == T_RIGHT)
+ error(mpl, "%s needs two arguments", func);
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ get_token(mpl /* , */);
+ /* parse the second argument */
+ arg.arg.y = symbolic_argument(mpl, func);
+ /* check a token that follows the second argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs two argument", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ else if (op == O_TIME2STR)
+ { /* time2str needs two arguments, numeric and symbolic */
+ /* parse the first argument */
+ arg.arg.x = numeric_argument(mpl, func);
+ /* check a token that follows the first argument */
+ if (mpl->token == T_COMMA)
+ ;
+ else if (mpl->token == T_RIGHT)
+ error(mpl, "%s needs two arguments", func);
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ get_token(mpl /* , */);
+ /* parse the second argument */
+ arg.arg.y = symbolic_argument(mpl, func);
+ /* check a token that follows the second argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs two argument", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ else
+ { /* other functions need one argument */
+ if (op == O_CARD)
+ arg.arg.x = elemset_argument(mpl, func);
+ else if (op == O_LENGTH)
+ arg.arg.x = symbolic_argument(mpl, func);
+ else
+ arg.arg.x = numeric_argument(mpl, func);
+ /* check a token that follows the argument */
+ if (mpl->token == T_COMMA)
+ error(mpl, "%s needs one argument", func);
+ else if (mpl->token == T_RIGHT)
+ ;
+ else
+ error(mpl, "syntax error in argument for %s", func);
+ }
+ /* make pseudo-code to call the built-in function */
+ if (op == O_SUBSTR || op == O_SUBSTR3 || op == O_TIME2STR)
+ code = make_code(mpl, op, &arg, A_SYMBOLIC, 0);
+ else
+ code = make_code(mpl, op, &arg, A_NUMERIC, 0);
+ /* the reference ends with the right parenthesis */
+ xassert(mpl->token == T_RIGHT);
+ get_token(mpl /* ) */);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- create_domain - create empty domain.
+--
+-- This routine creates empty domain, which is initially empty, i.e.
+-- has no domain blocks. */
+
+DOMAIN *create_domain(MPL *mpl)
+{ DOMAIN *domain;
+ domain = alloc(DOMAIN);
+ domain->list = NULL;
+ domain->code = NULL;
+ return domain;
+}
+
+/*----------------------------------------------------------------------
+-- create_block - create empty domain block.
+--
+-- This routine creates empty domain block, which is initially empty,
+-- i.e. has no domain slots. */
+
+DOMAIN_BLOCK *create_block(MPL *mpl)
+{ DOMAIN_BLOCK *block;
+ block = alloc(DOMAIN_BLOCK);
+ block->list = NULL;
+ block->code = NULL;
+ block->backup = NULL;
+ block->next = NULL;
+ return block;
+}
+
+/*----------------------------------------------------------------------
+-- append_block - append domain block to specified domain.
+--
+-- This routine adds given domain block to the end of the block list of
+-- specified domain. */
+
+void append_block(MPL *mpl, DOMAIN *domain, DOMAIN_BLOCK *block)
+{ DOMAIN_BLOCK *temp;
+ xassert(mpl == mpl);
+ xassert(domain != NULL);
+ xassert(block != NULL);
+ xassert(block->next == NULL);
+ if (domain->list == NULL)
+ domain->list = block;
+ else
+ { for (temp = domain->list; temp->next != NULL; temp =
+ temp->next);
+ temp->next = block;
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- append_slot - create and append new slot to domain block.
+--
+-- This routine creates new domain slot and adds it to the end of slot
+-- list of specified domain block.
+--
+-- The parameter name is symbolic name of the dummy index associated
+-- with the slot (the character string must be allocated). NULL means
+-- the dummy index is not explicitly specified.
+--
+-- The parameter code is pseudo-code for computing symbolic value, at
+-- which the dummy index is bounded. NULL means the dummy index is free
+-- in the domain scope. */
+
+DOMAIN_SLOT *append_slot(MPL *mpl, DOMAIN_BLOCK *block, char *name,
+ CODE *code)
+{ DOMAIN_SLOT *slot, *temp;
+ xassert(block != NULL);
+ slot = alloc(DOMAIN_SLOT);
+ slot->name = name;
+ slot->code = code;
+ slot->value = NULL;
+ slot->list = NULL;
+ slot->next = NULL;
+ if (block->list == NULL)
+ block->list = slot;
+ else
+ { for (temp = block->list; temp->next != NULL; temp =
+ temp->next);
+ temp->next = slot;
+ }
+ return slot;
+}
+
+/*----------------------------------------------------------------------
+-- expression_list - parse expression list.
+--
+-- This routine parses a list of one or more expressions enclosed into
+-- the parentheses using the syntax:
+--
+-- <primary expression> ::= ( <expression list> )
+-- <expression list> ::= <expression 13>
+-- <expression list> ::= <expression 13> , <expression list>
+--
+-- Note that this construction may have three different meanings:
+--
+-- 1. If <expression list> consists of only one expression, <primary
+-- expression> is a parenthesized expression, which may be of any
+-- valid type (not necessarily 1-tuple).
+--
+-- 2. If <expression list> consists of several expressions separated by
+-- commae, where no expression is undeclared symbolic name, <primary
+-- expression> is a n-tuple.
+--
+-- 3. If <expression list> consists of several expressions separated by
+-- commae, where at least one expression is undeclared symbolic name
+-- (that denotes a dummy index), <primary expression> is a slice and
+-- can be only used as constituent of indexing expression. */
+
+#define max_dim 20
+/* maximal number of components allowed within parentheses */
+
+CODE *expression_list(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ struct { char *name; CODE *code; } list[1+max_dim];
+ int flag_x, next_token, dim, j, slice = 0;
+ xassert(mpl->token == T_LEFT);
+ /* the flag, which allows recognizing undeclared symbolic names
+ as dummy indices, will be automatically reset by get_token(),
+ so save it before scanning the next token */
+ flag_x = mpl->flag_x;
+ get_token(mpl /* ( */);
+ /* parse <expression list> */
+ for (dim = 1; ; dim++)
+ { if (dim > max_dim)
+ error(mpl, "too many components within parentheses");
+ /* current component of <expression list> can be either dummy
+ index or expression */
+ if (mpl->token == T_NAME)
+ { /* symbolic name is recognized as dummy index only if:
+ the flag, which allows that, is set, and
+ the name is followed by comma or right parenthesis, and
+ the name is undeclared */
+ get_token(mpl /* <symbolic name> */);
+ next_token = mpl->token;
+ unget_token(mpl);
+ if (!(flag_x &&
+ (next_token == T_COMMA || next_token == T_RIGHT) &&
+ avl_find_node(mpl->tree, mpl->image) == NULL))
+ { /* this is not dummy index */
+ goto expr;
+ }
+ /* all dummy indices within the same slice must have unique
+ symbolic names */
+ for (j = 1; j < dim; j++)
+ { if (list[j].name != NULL && strcmp(list[j].name,
+ mpl->image) == 0)
+ error(mpl, "duplicate dummy index %s not allowed",
+ mpl->image);
+ }
+ /* current component of <expression list> is dummy index */
+ list[dim].name
+ = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(list[dim].name, mpl->image);
+ list[dim].code = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* <expression list> is a slice, because at least one dummy
+ index has appeared */
+ slice = 1;
+ /* note that the context ( <dummy index> ) is not allowed,
+ i.e. in this case <primary expression> is considered as
+ a parenthesized expression */
+ if (dim == 1 && mpl->token == T_RIGHT)
+ error(mpl, "%s not defined", list[dim].name);
+ }
+ else
+expr: { /* current component of <expression list> is expression */
+ code = expression_13(mpl);
+ /* if the current expression is followed by comma or it is
+ not the very first expression, entire <expression list>
+ is n-tuple or slice, in which case the current expression
+ should be converted to symbolic type, if necessary */
+ if (mpl->token == T_COMMA || dim > 1)
+ { if (code->type == A_NUMERIC)
+ code = make_unary(mpl, O_CVTSYM, code, A_SYMBOLIC, 0);
+ /* now the expression must be of symbolic type */
+ if (code->type != A_SYMBOLIC)
+ error(mpl, "component expression has invalid type");
+ xassert(code->dim == 0);
+ }
+ list[dim].name = NULL;
+ list[dim].code = code;
+ }
+ /* check a token that follows the current component */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RIGHT)
+ break;
+ else
+ error(mpl, "right parenthesis missing where expected");
+ }
+ /* generate pseudo-code for <primary expression> */
+ if (dim == 1 && !slice)
+ { /* <primary expression> is a parenthesized expression */
+ code = list[1].code;
+ }
+ else if (!slice)
+ { /* <primary expression> is a n-tuple */
+ arg.list = create_arg_list(mpl);
+ for (j = 1; j <= dim; j++)
+ arg.list = expand_arg_list(mpl, arg.list, list[j].code);
+ code = make_code(mpl, O_TUPLE, &arg, A_TUPLE, dim);
+ }
+ else
+ { /* <primary expression> is a slice */
+ arg.slice = create_block(mpl);
+ for (j = 1; j <= dim; j++)
+ append_slot(mpl, arg.slice, list[j].name, list[j].code);
+ /* note that actually pseudo-codes with op = O_SLICE are never
+ evaluated */
+ code = make_code(mpl, O_SLICE, &arg, A_TUPLE, dim);
+ }
+ get_token(mpl /* ) */);
+ /* if <primary expression> is a slice, there must be the keyword
+ 'in', which follows the right parenthesis */
+ if (slice && mpl->token != T_IN)
+ error(mpl, "keyword in missing where expected");
+ /* if the slice flag is set and there is the keyword 'in', which
+ follows <primary expression>, the latter must be a slice */
+ if (flag_x && mpl->token == T_IN && !slice)
+ { if (dim == 1)
+ error(mpl, "syntax error in indexing expression");
+ else
+ error(mpl, "0-ary slice not allowed");
+ }
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- literal set - parse literal set.
+--
+-- This routine parses literal set using the syntax:
+--
+-- <literal set> ::= { <member list> }
+-- <member list> ::= <member expression>
+-- <member list> ::= <member list> , <member expression>
+-- <member expression> ::= <expression 5>
+--
+-- It is assumed that the left curly brace and the very first member
+-- expression that follows it are already parsed. The right curly brace
+-- remains unscanned on exit. */
+
+CODE *literal_set(MPL *mpl, CODE *code)
+{ OPERANDS arg;
+ int j;
+ xassert(code != NULL);
+ arg.list = create_arg_list(mpl);
+ /* parse <member list> */
+ for (j = 1; ; j++)
+ { /* all member expressions must be n-tuples; so, if the current
+ expression is not n-tuple, convert it to 1-tuple */
+ if (code->type == A_NUMERIC)
+ code = make_unary(mpl, O_CVTSYM, code, A_SYMBOLIC, 0);
+ if (code->type == A_SYMBOLIC)
+ code = make_unary(mpl, O_CVTTUP, code, A_TUPLE, 1);
+ /* now the expression must be n-tuple */
+ if (code->type != A_TUPLE)
+ error(mpl, "member expression has invalid type");
+ /* all member expressions must have identical dimension */
+ if (arg.list != NULL && arg.list->x->dim != code->dim)
+ error(mpl, "member %d has %d component%s while member %d ha"
+ "s %d component%s",
+ j-1, arg.list->x->dim, arg.list->x->dim == 1 ? "" : "s",
+ j, code->dim, code->dim == 1 ? "" : "s");
+ /* append the current expression to the member list */
+ arg.list = expand_arg_list(mpl, arg.list, code);
+ /* check a token that follows the current expression */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RBRACE)
+ break;
+ else
+ error(mpl, "syntax error in literal set");
+ /* parse the next expression that follows the comma */
+ code = expression_5(mpl);
+ }
+ /* generate pseudo-code for <literal set> */
+ code = make_code(mpl, O_MAKE, &arg, A_ELEMSET, arg.list->x->dim);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- indexing_expression - parse indexing expression.
+--
+-- This routine parses indexing expression using the syntax:
+--
+-- <indexing expression> ::= <literal set>
+-- <indexing expression> ::= { <indexing list> }
+-- <indexing expression> ::= { <indexing list> : <logical expression> }
+-- <indexing list> ::= <indexing element>
+-- <indexing list> ::= <indexing list> , <indexing element>
+-- <indexing element> ::= <basic expression>
+-- <indexing element> ::= <dummy index> in <basic expression>
+-- <indexing element> ::= <slice> in <basic expression>
+-- <dummy index> ::= <symbolic name>
+-- <slice> ::= ( <expression list> )
+-- <basic expression> ::= <expression 9>
+-- <logical expression> ::= <expression 13>
+--
+-- This routine creates domain for <indexing expression>, where each
+-- domain block corresponds to <indexing element>, and each domain slot
+-- corresponds to individual indexing position. */
+
+DOMAIN *indexing_expression(MPL *mpl)
+{ DOMAIN *domain;
+ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ CODE *code;
+ xassert(mpl->token == T_LBRACE);
+ get_token(mpl /* { */);
+ if (mpl->token == T_RBRACE)
+ error(mpl, "empty indexing expression not allowed");
+ /* create domain to be constructed */
+ domain = create_domain(mpl);
+ /* parse either <member list> or <indexing list> that follows the
+ left brace */
+ for (;;)
+ { /* domain block for <indexing element> is not created yet */
+ block = NULL;
+ /* pseudo-code for <basic expression> is not generated yet */
+ code = NULL;
+ /* check a token, which <indexing element> begins with */
+ if (mpl->token == T_NAME)
+ { /* it is a symbolic name */
+ int next_token;
+ char *name;
+ /* symbolic name is recognized as dummy index only if it is
+ followed by the keyword 'in' and not declared */
+ get_token(mpl /* <symbolic name> */);
+ next_token = mpl->token;
+ unget_token(mpl);
+ if (!(next_token == T_IN &&
+ avl_find_node(mpl->tree, mpl->image) == NULL))
+ { /* this is not dummy index; the symbolic name begins an
+ expression, which is either <basic expression> or the
+ very first <member expression> in <literal set> */
+ goto expr;
+ }
+ /* create domain block with one slot, which is assigned the
+ dummy index */
+ block = create_block(mpl);
+ name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(name, mpl->image);
+ append_slot(mpl, block, name, NULL);
+ get_token(mpl /* <symbolic name> */);
+ /* the keyword 'in' is already checked above */
+ xassert(mpl->token == T_IN);
+ get_token(mpl /* in */);
+ /* <basic expression> that follows the keyword 'in' will be
+ parsed below */
+ }
+ else if (mpl->token == T_LEFT)
+ { /* it is the left parenthesis; parse expression that begins
+ with this parenthesis (the flag is set in order to allow
+ recognizing slices; see the routine expression_list) */
+ mpl->flag_x = 1;
+ code = expression_9(mpl);
+ if (code->op != O_SLICE)
+ { /* this is either <basic expression> or the very first
+ <member expression> in <literal set> */
+ goto expr;
+ }
+ /* this is a slice; besides the corresponding domain block
+ is already created by expression_list() */
+ block = code->arg.slice;
+ code = NULL; /* <basic expression> is not parsed yet */
+ /* the keyword 'in' following the slice is already checked
+ by expression_list() */
+ xassert(mpl->token == T_IN);
+ get_token(mpl /* in */);
+ /* <basic expression> that follows the keyword 'in' will be
+ parsed below */
+ }
+expr: /* parse expression that follows either the keyword 'in' (in
+ which case it can be <basic expression) or the left brace
+ (in which case it can be <basic expression> as well as the
+ very first <member expression> in <literal set>); note that
+ this expression can be already parsed above */
+ if (code == NULL) code = expression_9(mpl);
+ /* check the type of the expression just parsed */
+ if (code->type != A_ELEMSET)
+ { /* it is not <basic expression> and therefore it can only
+ be the very first <member expression> in <literal set>;
+ however, then there must be no dummy index neither slice
+ between the left brace and this expression */
+ if (block != NULL)
+ error(mpl, "domain expression has invalid type");
+ /* parse the rest part of <literal set> and make this set
+ be <basic expression>, i.e. the construction {a, b, c}
+ is parsed as it were written as {A}, where A = {a, b, c}
+ is a temporary elemental set */
+ code = literal_set(mpl, code);
+ }
+ /* now pseudo-code for <basic set> has been built */
+ xassert(code != NULL);
+ xassert(code->type == A_ELEMSET);
+ xassert(code->dim > 0);
+ /* if domain block for the current <indexing element> is still
+ not created, create it for fake slice of the same dimension
+ as <basic set> */
+ if (block == NULL)
+ { int j;
+ block = create_block(mpl);
+ for (j = 1; j <= code->dim; j++)
+ append_slot(mpl, block, NULL, NULL);
+ }
+ /* number of indexing positions in <indexing element> must be
+ the same as dimension of n-tuples in basic set */
+ { int dim = 0;
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ dim++;
+ if (dim != code->dim)
+ error(mpl,"%d %s specified for set of dimension %d",
+ dim, dim == 1 ? "index" : "indices", code->dim);
+ }
+ /* store pseudo-code for <basic set> in the domain block */
+ xassert(block->code == NULL);
+ block->code = code;
+ /* and append the domain block to the domain */
+ append_block(mpl, domain, block);
+ /* the current <indexing element> has been completely parsed;
+ include all its dummy indices into the symbolic name table
+ to make them available for referencing from expressions;
+ implicit declarations of dummy indices remain valid while
+ the corresponding domain scope is valid */
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ if (slot->name != NULL)
+ { AVLNODE *node;
+ xassert(avl_find_node(mpl->tree, slot->name) == NULL);
+ node = avl_insert_node(mpl->tree, slot->name);
+ avl_set_node_type(node, A_INDEX);
+ avl_set_node_link(node, (void *)slot);
+ }
+ /* check a token that follows <indexing element> */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_COLON || mpl->token == T_RBRACE)
+ break;
+ else
+ error(mpl, "syntax error in indexing expression");
+ }
+ /* parse <logical expression> that follows the colon */
+ if (mpl->token == T_COLON)
+ { get_token(mpl /* : */);
+ code = expression_13(mpl);
+ /* convert the expression to logical type, if necessary */
+ if (code->type == A_SYMBOLIC)
+ code = make_unary(mpl, O_CVTNUM, code, A_NUMERIC, 0);
+ if (code->type == A_NUMERIC)
+ code = make_unary(mpl, O_CVTLOG, code, A_LOGICAL, 0);
+ /* now the expression must be of logical type */
+ if (code->type != A_LOGICAL)
+ error(mpl, "expression following colon has invalid type");
+ xassert(code->dim == 0);
+ domain->code = code;
+ /* the right brace must follow the logical expression */
+ if (mpl->token != T_RBRACE)
+ error(mpl, "syntax error in indexing expression");
+ }
+ get_token(mpl /* } */);
+ return domain;
+}
+
+/*----------------------------------------------------------------------
+-- close_scope - close scope of indexing expression.
+--
+-- The routine closes the scope of indexing expression specified by its
+-- domain and thereby makes all dummy indices introduced in the indexing
+-- expression no longer available for referencing. */
+
+void close_scope(MPL *mpl, DOMAIN *domain)
+{ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ AVLNODE *node;
+ xassert(domain != NULL);
+ /* remove all dummy indices from the symbolic names table */
+ for (block = domain->list; block != NULL; block = block->next)
+ { for (slot = block->list; slot != NULL; slot = slot->next)
+ { if (slot->name != NULL)
+ { node = avl_find_node(mpl->tree, slot->name);
+ xassert(node != NULL);
+ xassert(avl_get_node_type(node) == A_INDEX);
+ avl_delete_node(mpl->tree, node);
+ }
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- iterated_expression - parse iterated expression.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <iterated expression>
+-- <iterated expression> ::= sum <indexing expression> <expression 3>
+-- <iterated expression> ::= prod <indexing expression> <expression 3>
+-- <iterated expression> ::= min <indexing expression> <expression 3>
+-- <iterated expression> ::= max <indexing expression> <expression 3>
+-- <iterated expression> ::= exists <indexing expression>
+-- <expression 12>
+-- <iterated expression> ::= forall <indexing expression>
+-- <expression 12>
+-- <iterated expression> ::= setof <indexing expression> <expression 5>
+--
+-- Note that parsing "integrand" depends on the iterated operator. */
+
+#if 1 /* 07/IX-2008 */
+static void link_up(CODE *code)
+{ /* if we have something like sum{(i+1,j,k-1) in E} x[i,j,k],
+ where i and k are dummy indices defined out of the iterated
+ expression, we should link up pseudo-code for computing i+1
+ and k-1 to pseudo-code for computing the iterated expression;
+ this is needed to invalidate current value of the iterated
+ expression once i or k have been changed */
+ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ for (block = code->arg.loop.domain->list; block != NULL;
+ block = block->next)
+ { for (slot = block->list; slot != NULL; slot = slot->next)
+ { if (slot->code != NULL)
+ { xassert(slot->code->up == NULL);
+ slot->code->up = code;
+ }
+ }
+ }
+ return;
+}
+#endif
+
+CODE *iterated_expression(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ int op;
+ char opstr[8];
+ /* determine operation code */
+ xassert(mpl->token == T_NAME);
+ if (strcmp(mpl->image, "sum") == 0)
+ op = O_SUM;
+ else if (strcmp(mpl->image, "prod") == 0)
+ op = O_PROD;
+ else if (strcmp(mpl->image, "min") == 0)
+ op = O_MINIMUM;
+ else if (strcmp(mpl->image, "max") == 0)
+ op = O_MAXIMUM;
+ else if (strcmp(mpl->image, "forall") == 0)
+ op = O_FORALL;
+ else if (strcmp(mpl->image, "exists") == 0)
+ op = O_EXISTS;
+ else if (strcmp(mpl->image, "setof") == 0)
+ op = O_SETOF;
+ else
+ error(mpl, "operator %s unknown", mpl->image);
+ strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ get_token(mpl /* <symbolic name> */);
+ /* check the left brace that follows the operator name */
+ xassert(mpl->token == T_LBRACE);
+ /* parse indexing expression that controls iterating */
+ arg.loop.domain = indexing_expression(mpl);
+ /* parse "integrand" expression and generate pseudo-code */
+ switch (op)
+ { case O_SUM:
+ case O_PROD:
+ case O_MINIMUM:
+ case O_MAXIMUM:
+ arg.loop.x = expression_3(mpl);
+ /* convert the integrand to numeric type, if necessary */
+ if (arg.loop.x->type == A_SYMBOLIC)
+ arg.loop.x = make_unary(mpl, O_CVTNUM, arg.loop.x,
+ A_NUMERIC, 0);
+ /* now the integrand must be of numeric type or linear form
+ (the latter is only allowed for the sum operator) */
+ if (!(arg.loop.x->type == A_NUMERIC ||
+ op == O_SUM && arg.loop.x->type == A_FORMULA))
+err: error(mpl, "integrand following %s{...} has invalid type"
+ , opstr);
+ xassert(arg.loop.x->dim == 0);
+ /* generate pseudo-code */
+ code = make_code(mpl, op, &arg, arg.loop.x->type, 0);
+ break;
+ case O_FORALL:
+ case O_EXISTS:
+ arg.loop.x = expression_12(mpl);
+ /* convert the integrand to logical type, if necessary */
+ if (arg.loop.x->type == A_SYMBOLIC)
+ arg.loop.x = make_unary(mpl, O_CVTNUM, arg.loop.x,
+ A_NUMERIC, 0);
+ if (arg.loop.x->type == A_NUMERIC)
+ arg.loop.x = make_unary(mpl, O_CVTLOG, arg.loop.x,
+ A_LOGICAL, 0);
+ /* now the integrand must be of logical type */
+ if (arg.loop.x->type != A_LOGICAL) goto err;
+ xassert(arg.loop.x->dim == 0);
+ /* generate pseudo-code */
+ code = make_code(mpl, op, &arg, A_LOGICAL, 0);
+ break;
+ case O_SETOF:
+ arg.loop.x = expression_5(mpl);
+ /* convert the integrand to 1-tuple, if necessary */
+ if (arg.loop.x->type == A_NUMERIC)
+ arg.loop.x = make_unary(mpl, O_CVTSYM, arg.loop.x,
+ A_SYMBOLIC, 0);
+ if (arg.loop.x->type == A_SYMBOLIC)
+ arg.loop.x = make_unary(mpl, O_CVTTUP, arg.loop.x,
+ A_TUPLE, 1);
+ /* now the integrand must be n-tuple */
+ if (arg.loop.x->type != A_TUPLE) goto err;
+ xassert(arg.loop.x->dim > 0);
+ /* generate pseudo-code */
+ code = make_code(mpl, op, &arg, A_ELEMSET, arg.loop.x->dim);
+ break;
+ default:
+ xassert(op != op);
+ }
+ /* close the scope of the indexing expression */
+ close_scope(mpl, arg.loop.domain);
+#if 1 /* 07/IX-2008 */
+ link_up(code);
+#endif
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- domain_arity - determine arity of domain.
+--
+-- This routine returns arity of specified domain, which is number of
+-- its free dummy indices. */
+
+int domain_arity(MPL *mpl, DOMAIN *domain)
+{ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ int arity;
+ xassert(mpl == mpl);
+ arity = 0;
+ for (block = domain->list; block != NULL; block = block->next)
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ if (slot->code == NULL) arity++;
+ return arity;
+}
+
+/*----------------------------------------------------------------------
+-- set_expression - parse set expression.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= { }
+-- <primary expression> ::= <indexing expression> */
+
+CODE *set_expression(MPL *mpl)
+{ CODE *code;
+ OPERANDS arg;
+ xassert(mpl->token == T_LBRACE);
+ get_token(mpl /* { */);
+ /* check a token that follows the left brace */
+ if (mpl->token == T_RBRACE)
+ { /* it is the right brace, so the resultant is an empty set of
+ dimension 1 */
+ arg.list = NULL;
+ /* generate pseudo-code to build the resultant set */
+ code = make_code(mpl, O_MAKE, &arg, A_ELEMSET, 1);
+ get_token(mpl /* } */);
+ }
+ else
+ { /* the next token begins an indexing expression */
+ unget_token(mpl);
+ arg.loop.domain = indexing_expression(mpl);
+ arg.loop.x = NULL; /* integrand is not used */
+ /* close the scope of the indexing expression */
+ close_scope(mpl, arg.loop.domain);
+ /* generate pseudo-code to build the resultant set */
+ code = make_code(mpl, O_BUILD, &arg, A_ELEMSET,
+ domain_arity(mpl, arg.loop.domain));
+#if 1 /* 07/IX-2008 */
+ link_up(code);
+#endif
+ }
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- branched_expression - parse conditional expression.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <branched expression>
+-- <branched expression> ::= if <logical expression> then <expression 9>
+-- <branched expression> ::= if <logical expression> then <expression 9>
+-- else <expression 9>
+-- <logical expression> ::= <expression 13> */
+
+CODE *branched_expression(MPL *mpl)
+{ CODE *code, *x, *y, *z;
+ xassert(mpl->token == T_IF);
+ get_token(mpl /* if */);
+ /* parse <logical expression> that follows 'if' */
+ x = expression_13(mpl);
+ /* convert the expression to logical type, if necessary */
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
+ /* now the expression must be of logical type */
+ if (x->type != A_LOGICAL)
+ error(mpl, "expression following if has invalid type");
+ xassert(x->dim == 0);
+ /* the keyword 'then' must follow the logical expression */
+ if (mpl->token != T_THEN)
+ error(mpl, "keyword then missing where expected");
+ get_token(mpl /* then */);
+ /* parse <expression> that follows 'then' and check its type */
+ y = expression_9(mpl);
+ if (!(y->type == A_NUMERIC || y->type == A_SYMBOLIC ||
+ y->type == A_ELEMSET || y->type == A_FORMULA))
+ error(mpl, "expression following then has invalid type");
+ /* if the expression that follows the keyword 'then' is elemental
+ set, the keyword 'else' cannot be omitted; otherwise else-part
+ is optional */
+ if (mpl->token != T_ELSE)
+ { if (y->type == A_ELEMSET)
+ error(mpl, "keyword else missing where expected");
+ z = NULL;
+ goto skip;
+ }
+ get_token(mpl /* else */);
+ /* parse <expression> that follow 'else' and check its type */
+ z = expression_9(mpl);
+ if (!(z->type == A_NUMERIC || z->type == A_SYMBOLIC ||
+ z->type == A_ELEMSET || z->type == A_FORMULA))
+ error(mpl, "expression following else has invalid type");
+ /* convert to identical types, if necessary */
+ if (y->type == A_FORMULA || z->type == A_FORMULA)
+ { if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
+ if (z->type == A_SYMBOLIC)
+ z = make_unary(mpl, O_CVTNUM, z, A_NUMERIC, 0);
+ if (z->type == A_NUMERIC)
+ z = make_unary(mpl, O_CVTLFM, z, A_FORMULA, 0);
+ }
+ if (y->type == A_SYMBOLIC || z->type == A_SYMBOLIC)
+ { if (y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
+ if (z->type == A_NUMERIC)
+ z = make_unary(mpl, O_CVTSYM, z, A_SYMBOLIC, 0);
+ }
+ /* now both expressions must have identical types */
+ if (y->type != z->type)
+ error(mpl, "expressions following then and else have incompati"
+ "ble types");
+ /* and identical dimensions */
+ if (y->dim != z->dim)
+ error(mpl, "expressions following then and else have different"
+ " dimensions %d and %d, respectively", y->dim, z->dim);
+skip: /* generate pseudo-code to perform branching */
+ code = make_ternary(mpl, O_FORK, x, y, z, y->type, y->dim);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- primary_expression - parse primary expression.
+--
+-- This routine parses primary expression using the syntax:
+--
+-- <primary expression> ::= <numeric literal>
+-- <primary expression> ::= Infinity
+-- <primary expression> ::= <string literal>
+-- <primary expression> ::= <dummy index>
+-- <primary expression> ::= <set name>
+-- <primary expression> ::= <set name> [ <subscript list> ]
+-- <primary expression> ::= <parameter name>
+-- <primary expression> ::= <parameter name> [ <subscript list> ]
+-- <primary expression> ::= <variable name>
+-- <primary expression> ::= <variable name> [ <subscript list> ]
+-- <primary expression> ::= <built-in function> ( <argument list> )
+-- <primary expression> ::= ( <expression list> )
+-- <primary expression> ::= <iterated expression>
+-- <primary expression> ::= { }
+-- <primary expression> ::= <indexing expression>
+-- <primary expression> ::= <branched expression>
+--
+-- For complete list of syntactic rules for <primary expression> see
+-- comments to the corresponding parsing routines. */
+
+CODE *primary_expression(MPL *mpl)
+{ CODE *code;
+ if (mpl->token == T_NUMBER)
+ { /* parse numeric literal */
+ code = numeric_literal(mpl);
+ }
+#if 1 /* 21/VII-2006 */
+ else if (mpl->token == T_INFINITY)
+ { /* parse "infinity" */
+ OPERANDS arg;
+ arg.num = DBL_MAX;
+ code = make_code(mpl, O_NUMBER, &arg, A_NUMERIC, 0);
+ get_token(mpl /* Infinity */);
+ }
+#endif
+ else if (mpl->token == T_STRING)
+ { /* parse string literal */
+ code = string_literal(mpl);
+ }
+ else if (mpl->token == T_NAME)
+ { int next_token;
+ get_token(mpl /* <symbolic name> */);
+ next_token = mpl->token;
+ unget_token(mpl);
+ /* check a token that follows <symbolic name> */
+ switch (next_token)
+ { case T_LBRACKET:
+ /* parse reference to subscripted object */
+ code = object_reference(mpl);
+ break;
+ case T_LEFT:
+ /* parse reference to built-in function */
+ code = function_reference(mpl);
+ break;
+ case T_LBRACE:
+ /* parse iterated expression */
+ code = iterated_expression(mpl);
+ break;
+ default:
+ /* parse reference to unsubscripted object */
+ code = object_reference(mpl);
+ break;
+ }
+ }
+ else if (mpl->token == T_LEFT)
+ { /* parse parenthesized expression */
+ code = expression_list(mpl);
+ }
+ else if (mpl->token == T_LBRACE)
+ { /* parse set expression */
+ code = set_expression(mpl);
+ }
+ else if (mpl->token == T_IF)
+ { /* parse conditional expression */
+ code = branched_expression(mpl);
+ }
+ else if (is_reserved(mpl))
+ { /* other reserved keywords cannot be used here */
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ }
+ else
+ error(mpl, "syntax error in expression");
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- error_preceding - raise error if preceding operand has wrong type.
+--
+-- This routine is called to raise error if operand that precedes some
+-- infix operator has invalid type. */
+
+void error_preceding(MPL *mpl, char *opstr)
+{ error(mpl, "operand preceding %s has invalid type", opstr);
+ /* no return */
+}
+
+/*----------------------------------------------------------------------
+-- error_following - raise error if following operand has wrong type.
+--
+-- This routine is called to raise error if operand that follows some
+-- infix operator has invalid type. */
+
+void error_following(MPL *mpl, char *opstr)
+{ error(mpl, "operand following %s has invalid type", opstr);
+ /* no return */
+}
+
+/*----------------------------------------------------------------------
+-- error_dimension - raise error if operands have different dimension.
+--
+-- This routine is called to raise error if two operands of some infix
+-- operator have different dimension. */
+
+void error_dimension(MPL *mpl, char *opstr, int dim1, int dim2)
+{ error(mpl, "operands preceding and following %s have different di"
+ "mensions %d and %d, respectively", opstr, dim1, dim2);
+ /* no return */
+}
+
+/*----------------------------------------------------------------------
+-- expression_0 - parse expression of level 0.
+--
+-- This routine parses expression of level 0 using the syntax:
+--
+-- <expression 0> ::= <primary expression> */
+
+CODE *expression_0(MPL *mpl)
+{ CODE *code;
+ code = primary_expression(mpl);
+ return code;
+}
+
+/*----------------------------------------------------------------------
+-- expression_1 - parse expression of level 1.
+--
+-- This routine parses expression of level 1 using the syntax:
+--
+-- <expression 1> ::= <expression 0>
+-- <expression 1> ::= <expression 0> <power> <expression 1>
+-- <expression 1> ::= <expression 0> <power> <expression 2>
+-- <power> ::= ^ | ** */
+
+CODE *expression_1(MPL *mpl)
+{ CODE *x, *y;
+ char opstr[8];
+ x = expression_0(mpl);
+ if (mpl->token == T_POWER)
+ { strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* ^ | ** */);
+ if (mpl->token == T_PLUS || mpl->token == T_MINUS)
+ y = expression_2(mpl);
+ else
+ y = expression_1(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, opstr);
+ x = make_binary(mpl, O_POWER, x, y, A_NUMERIC, 0);
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_2 - parse expression of level 2.
+--
+-- This routine parses expression of level 2 using the syntax:
+--
+-- <expression 2> ::= <expression 1>
+-- <expression 2> ::= + <expression 1>
+-- <expression 2> ::= - <expression 1> */
+
+CODE *expression_2(MPL *mpl)
+{ CODE *x;
+ if (mpl->token == T_PLUS)
+ { get_token(mpl /* + */);
+ x = expression_1(mpl);
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_following(mpl, "+");
+ x = make_unary(mpl, O_PLUS, x, x->type, 0);
+ }
+ else if (mpl->token == T_MINUS)
+ { get_token(mpl /* - */);
+ x = expression_1(mpl);
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_following(mpl, "-");
+ x = make_unary(mpl, O_MINUS, x, x->type, 0);
+ }
+ else
+ x = expression_1(mpl);
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_3 - parse expression of level 3.
+--
+-- This routine parses expression of level 3 using the syntax:
+--
+-- <expression 3> ::= <expression 2>
+-- <expression 3> ::= <expression 3> * <expression 2>
+-- <expression 3> ::= <expression 3> / <expression 2>
+-- <expression 3> ::= <expression 3> div <expression 2>
+-- <expression 3> ::= <expression 3> mod <expression 2> */
+
+CODE *expression_3(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_2(mpl);
+ for (;;)
+ { if (mpl->token == T_ASTERISK)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_preceding(mpl, "*");
+ get_token(mpl /* * */);
+ y = expression_2(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
+ error_following(mpl, "*");
+ if (x->type == A_FORMULA && y->type == A_FORMULA)
+ error(mpl, "multiplication of linear forms not allowed");
+ if (x->type == A_NUMERIC && y->type == A_NUMERIC)
+ x = make_binary(mpl, O_MUL, x, y, A_NUMERIC, 0);
+ else
+ x = make_binary(mpl, O_MUL, x, y, A_FORMULA, 0);
+ }
+ else if (mpl->token == T_SLASH)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_preceding(mpl, "/");
+ get_token(mpl /* / */);
+ y = expression_2(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, "/");
+ if (x->type == A_NUMERIC)
+ x = make_binary(mpl, O_DIV, x, y, A_NUMERIC, 0);
+ else
+ x = make_binary(mpl, O_DIV, x, y, A_FORMULA, 0);
+ }
+ else if (mpl->token == T_DIV)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, "div");
+ get_token(mpl /* div */);
+ y = expression_2(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, "div");
+ x = make_binary(mpl, O_IDIV, x, y, A_NUMERIC, 0);
+ }
+ else if (mpl->token == T_MOD)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, "mod");
+ get_token(mpl /* mod */);
+ y = expression_2(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, "mod");
+ x = make_binary(mpl, O_MOD, x, y, A_NUMERIC, 0);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_4 - parse expression of level 4.
+--
+-- This routine parses expression of level 4 using the syntax:
+--
+-- <expression 4> ::= <expression 3>
+-- <expression 4> ::= <expression 4> + <expression 3>
+-- <expression 4> ::= <expression 4> - <expression 3>
+-- <expression 4> ::= <expression 4> less <expression 3> */
+
+CODE *expression_4(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_3(mpl);
+ for (;;)
+ { if (mpl->token == T_PLUS)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_preceding(mpl, "+");
+ get_token(mpl /* + */);
+ y = expression_3(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
+ error_following(mpl, "+");
+ if (x->type == A_NUMERIC && y->type == A_FORMULA)
+ x = make_unary(mpl, O_CVTLFM, x, A_FORMULA, 0);
+ if (x->type == A_FORMULA && y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
+ x = make_binary(mpl, O_ADD, x, y, x->type, 0);
+ }
+ else if (mpl->token == T_MINUS)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
+ error_preceding(mpl, "-");
+ get_token(mpl /* - */);
+ y = expression_3(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
+ error_following(mpl, "-");
+ if (x->type == A_NUMERIC && y->type == A_FORMULA)
+ x = make_unary(mpl, O_CVTLFM, x, A_FORMULA, 0);
+ if (x->type == A_FORMULA && y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
+ x = make_binary(mpl, O_SUB, x, y, x->type, 0);
+ }
+ else if (mpl->token == T_LESS)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, "less");
+ get_token(mpl /* less */);
+ y = expression_3(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, "less");
+ x = make_binary(mpl, O_LESS, x, y, A_NUMERIC, 0);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_5 - parse expression of level 5.
+--
+-- This routine parses expression of level 5 using the syntax:
+--
+-- <expression 5> ::= <expression 4>
+-- <expression 5> ::= <expression 5> & <expression 4> */
+
+CODE *expression_5(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_4(mpl);
+ for (;;)
+ { if (mpl->token == T_CONCAT)
+ { if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
+ if (x->type != A_SYMBOLIC)
+ error_preceding(mpl, "&");
+ get_token(mpl /* & */);
+ y = expression_4(mpl);
+ if (y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
+ if (y->type != A_SYMBOLIC)
+ error_following(mpl, "&");
+ x = make_binary(mpl, O_CONCAT, x, y, A_SYMBOLIC, 0);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_6 - parse expression of level 6.
+--
+-- This routine parses expression of level 6 using the syntax:
+--
+-- <expression 6> ::= <expression 5>
+-- <expression 6> ::= <expression 5> .. <expression 5>
+-- <expression 6> ::= <expression 5> .. <expression 5> by
+-- <expression 5> */
+
+CODE *expression_6(MPL *mpl)
+{ CODE *x, *y, *z;
+ x = expression_5(mpl);
+ if (mpl->token == T_DOTS)
+ { if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, "..");
+ get_token(mpl /* .. */);
+ y = expression_5(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, "..");
+ if (mpl->token == T_BY)
+ { get_token(mpl /* by */);
+ z = expression_5(mpl);
+ if (z->type == A_SYMBOLIC)
+ z = make_unary(mpl, O_CVTNUM, z, A_NUMERIC, 0);
+ if (z->type != A_NUMERIC)
+ error_following(mpl, "by");
+ }
+ else
+ z = NULL;
+ x = make_ternary(mpl, O_DOTS, x, y, z, A_ELEMSET, 1);
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_7 - parse expression of level 7.
+--
+-- This routine parses expression of level 7 using the syntax:
+--
+-- <expression 7> ::= <expression 6>
+-- <expression 7> ::= <expression 7> cross <expression 6> */
+
+CODE *expression_7(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_6(mpl);
+ for (;;)
+ { if (mpl->token == T_CROSS)
+ { if (x->type != A_ELEMSET)
+ error_preceding(mpl, "cross");
+ get_token(mpl /* cross */);
+ y = expression_6(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, "cross");
+ x = make_binary(mpl, O_CROSS, x, y, A_ELEMSET,
+ x->dim + y->dim);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_8 - parse expression of level 8.
+--
+-- This routine parses expression of level 8 using the syntax:
+--
+-- <expression 8> ::= <expression 7>
+-- <expression 8> ::= <expression 8> inter <expression 7> */
+
+CODE *expression_8(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_7(mpl);
+ for (;;)
+ { if (mpl->token == T_INTER)
+ { if (x->type != A_ELEMSET)
+ error_preceding(mpl, "inter");
+ get_token(mpl /* inter */);
+ y = expression_7(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, "inter");
+ if (x->dim != y->dim)
+ error_dimension(mpl, "inter", x->dim, y->dim);
+ x = make_binary(mpl, O_INTER, x, y, A_ELEMSET, x->dim);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_9 - parse expression of level 9.
+--
+-- This routine parses expression of level 9 using the syntax:
+--
+-- <expression 9> ::= <expression 8>
+-- <expression 9> ::= <expression 9> union <expression 8>
+-- <expression 9> ::= <expression 9> diff <expression 8>
+-- <expression 9> ::= <expression 9> symdiff <expression 8> */
+
+CODE *expression_9(MPL *mpl)
+{ CODE *x, *y;
+ x = expression_8(mpl);
+ for (;;)
+ { if (mpl->token == T_UNION)
+ { if (x->type != A_ELEMSET)
+ error_preceding(mpl, "union");
+ get_token(mpl /* union */);
+ y = expression_8(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, "union");
+ if (x->dim != y->dim)
+ error_dimension(mpl, "union", x->dim, y->dim);
+ x = make_binary(mpl, O_UNION, x, y, A_ELEMSET, x->dim);
+ }
+ else if (mpl->token == T_DIFF)
+ { if (x->type != A_ELEMSET)
+ error_preceding(mpl, "diff");
+ get_token(mpl /* diff */);
+ y = expression_8(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, "diff");
+ if (x->dim != y->dim)
+ error_dimension(mpl, "diff", x->dim, y->dim);
+ x = make_binary(mpl, O_DIFF, x, y, A_ELEMSET, x->dim);
+ }
+ else if (mpl->token == T_SYMDIFF)
+ { if (x->type != A_ELEMSET)
+ error_preceding(mpl, "symdiff");
+ get_token(mpl /* symdiff */);
+ y = expression_8(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, "symdiff");
+ if (x->dim != y->dim)
+ error_dimension(mpl, "symdiff", x->dim, y->dim);
+ x = make_binary(mpl, O_SYMDIFF, x, y, A_ELEMSET, x->dim);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_10 - parse expression of level 10.
+--
+-- This routine parses expression of level 10 using the syntax:
+--
+-- <expression 10> ::= <expression 9>
+-- <expression 10> ::= <expression 9> <rho> <expression 9>
+-- <rho> ::= < | <= | = | == | >= | > | <> | != | in | not in | ! in |
+-- within | not within | ! within */
+
+CODE *expression_10(MPL *mpl)
+{ CODE *x, *y;
+ int op = -1;
+ char opstr[16];
+ x = expression_9(mpl);
+ strcpy(opstr, "");
+ switch (mpl->token)
+ { case T_LT:
+ op = O_LT; break;
+ case T_LE:
+ op = O_LE; break;
+ case T_EQ:
+ op = O_EQ; break;
+ case T_GE:
+ op = O_GE; break;
+ case T_GT:
+ op = O_GT; break;
+ case T_NE:
+ op = O_NE; break;
+ case T_IN:
+ op = O_IN; break;
+ case T_WITHIN:
+ op = O_WITHIN; break;
+ case T_NOT:
+ strcpy(opstr, mpl->image);
+ get_token(mpl /* not | ! */);
+ if (mpl->token == T_IN)
+ op = O_NOTIN;
+ else if (mpl->token == T_WITHIN)
+ op = O_NOTWITHIN;
+ else
+ error(mpl, "invalid use of %s", opstr);
+ strcat(opstr, " ");
+ break;
+ default:
+ goto done;
+ }
+ strcat(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ switch (op)
+ { case O_EQ:
+ case O_NE:
+#if 1 /* 02/VIII-2008 */
+ case O_LT:
+ case O_LE:
+ case O_GT:
+ case O_GE:
+#endif
+ if (!(x->type == A_NUMERIC || x->type == A_SYMBOLIC))
+ error_preceding(mpl, opstr);
+ get_token(mpl /* <rho> */);
+ y = expression_9(mpl);
+ if (!(y->type == A_NUMERIC || y->type == A_SYMBOLIC))
+ error_following(mpl, opstr);
+ if (x->type == A_NUMERIC && y->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
+ if (x->type == A_SYMBOLIC && y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
+ x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
+ break;
+#if 0 /* 02/VIII-2008 */
+ case O_LT:
+ case O_LE:
+ case O_GT:
+ case O_GE:
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type != A_NUMERIC)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* <rho> */);
+ y = expression_9(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type != A_NUMERIC)
+ error_following(mpl, opstr);
+ x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
+ break;
+#endif
+ case O_IN:
+ case O_NOTIN:
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTTUP, x, A_TUPLE, 1);
+ if (x->type != A_TUPLE)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* <rho> */);
+ y = expression_9(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, opstr);
+ if (x->dim != y->dim)
+ error_dimension(mpl, opstr, x->dim, y->dim);
+ x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
+ break;
+ case O_WITHIN:
+ case O_NOTWITHIN:
+ if (x->type != A_ELEMSET)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* <rho> */);
+ y = expression_9(mpl);
+ if (y->type != A_ELEMSET)
+ error_following(mpl, opstr);
+ if (x->dim != y->dim)
+ error_dimension(mpl, opstr, x->dim, y->dim);
+ x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
+ break;
+ default:
+ xassert(op != op);
+ }
+done: return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_11 - parse expression of level 11.
+--
+-- This routine parses expression of level 11 using the syntax:
+--
+-- <expression 11> ::= <expression 10>
+-- <expression 11> ::= not <expression 10>
+-- <expression 11> ::= ! <expression 10> */
+
+CODE *expression_11(MPL *mpl)
+{ CODE *x;
+ char opstr[8];
+ if (mpl->token == T_NOT)
+ { strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ get_token(mpl /* not | ! */);
+ x = expression_10(mpl);
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
+ if (x->type != A_LOGICAL)
+ error_following(mpl, opstr);
+ x = make_unary(mpl, O_NOT, x, A_LOGICAL, 0);
+ }
+ else
+ x = expression_10(mpl);
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_12 - parse expression of level 12.
+--
+-- This routine parses expression of level 12 using the syntax:
+--
+-- <expression 12> ::= <expression 11>
+-- <expression 12> ::= <expression 12> and <expression 11>
+-- <expression 12> ::= <expression 12> && <expression 11> */
+
+CODE *expression_12(MPL *mpl)
+{ CODE *x, *y;
+ char opstr[8];
+ x = expression_11(mpl);
+ for (;;)
+ { if (mpl->token == T_AND)
+ { strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
+ if (x->type != A_LOGICAL)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* and | && */);
+ y = expression_11(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTLOG, y, A_LOGICAL, 0);
+ if (y->type != A_LOGICAL)
+ error_following(mpl, opstr);
+ x = make_binary(mpl, O_AND, x, y, A_LOGICAL, 0);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- expression_13 - parse expression of level 13.
+--
+-- This routine parses expression of level 13 using the syntax:
+--
+-- <expression 13> ::= <expression 12>
+-- <expression 13> ::= <expression 13> or <expression 12>
+-- <expression 13> ::= <expression 13> || <expression 12> */
+
+CODE *expression_13(MPL *mpl)
+{ CODE *x, *y;
+ char opstr[8];
+ x = expression_12(mpl);
+ for (;;)
+ { if (mpl->token == T_OR)
+ { strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ if (x->type == A_SYMBOLIC)
+ x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
+ if (x->type == A_NUMERIC)
+ x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
+ if (x->type != A_LOGICAL)
+ error_preceding(mpl, opstr);
+ get_token(mpl /* or | || */);
+ y = expression_12(mpl);
+ if (y->type == A_SYMBOLIC)
+ y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
+ if (y->type == A_NUMERIC)
+ y = make_unary(mpl, O_CVTLOG, y, A_LOGICAL, 0);
+ if (y->type != A_LOGICAL)
+ error_following(mpl, opstr);
+ x = make_binary(mpl, O_OR, x, y, A_LOGICAL, 0);
+ }
+ else
+ break;
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- set_statement - parse set statement.
+--
+-- This routine parses set statement using the syntax:
+--
+-- <set statement> ::= set <symbolic name> <alias> <domain>
+-- <attributes> ;
+-- <alias> ::= <empty>
+-- <alias> ::= <string literal>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <attributes> ::= <empty>
+-- <attributes> ::= <attributes> , dimen <numeric literal>
+-- <attributes> ::= <attributes> , within <expression 9>
+-- <attributes> ::= <attributes> , := <expression 9>
+-- <attributes> ::= <attributes> , default <expression 9>
+--
+-- Commae in <attributes> are optional and may be omitted anywhere. */
+
+SET *set_statement(MPL *mpl)
+{ SET *set;
+ int dimen_used = 0;
+ xassert(is_keyword(mpl, "set"));
+ get_token(mpl /* set */);
+ /* symbolic name must follow the keyword 'set' */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create model set */
+ set = alloc(SET);
+ set->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(set->name, mpl->image);
+ set->alias = NULL;
+ set->dim = 0;
+ set->domain = NULL;
+ set->dimen = 0;
+ set->within = NULL;
+ set->assign = NULL;
+ set->option = NULL;
+ set->gadget = NULL;
+ set->data = 0;
+ set->array = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { set->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(set->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { set->domain = indexing_expression(mpl);
+ set->dim = domain_arity(mpl, set->domain);
+ }
+ /* include the set name in the symbolic names table */
+ { AVLNODE *node;
+ node = avl_insert_node(mpl->tree, set->name);
+ avl_set_node_type(node, A_SET);
+ avl_set_node_link(node, (void *)set);
+ }
+ /* parse the list of optional attributes */
+ for (;;)
+ { if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_SEMICOLON)
+ break;
+ if (is_keyword(mpl, "dimen"))
+ { /* dimension of set members */
+ int dimen;
+ get_token(mpl /* dimen */);
+ if (!(mpl->token == T_NUMBER &&
+ 1.0 <= mpl->value && mpl->value <= 20.0 &&
+ floor(mpl->value) == mpl->value))
+ error(mpl, "dimension must be integer between 1 and 20");
+ dimen = (int)(mpl->value + 0.5);
+ if (dimen_used)
+ error(mpl, "at most one dimension attribute allowed");
+ if (set->dimen > 0)
+ error(mpl, "dimension %d conflicts with dimension %d alr"
+ "eady determined", dimen, set->dimen);
+ set->dimen = dimen;
+ dimen_used = 1;
+ get_token(mpl /* <numeric literal> */);
+ }
+ else if (mpl->token == T_WITHIN || mpl->token == T_IN)
+ { /* restricting superset */
+ WITHIN *within, *temp;
+ if (mpl->token == T_IN && !mpl->as_within)
+ { warning(mpl, "keyword in understood as within");
+ mpl->as_within = 1;
+ }
+ get_token(mpl /* within */);
+ /* create new restricting superset list entry and append it
+ to the within-list */
+ within = alloc(WITHIN);
+ within->code = NULL;
+ within->next = NULL;
+ if (set->within == NULL)
+ set->within = within;
+ else
+ { for (temp = set->within; temp->next != NULL; temp =
+ temp->next);
+ temp->next = within;
+ }
+ /* parse an expression that follows 'within' */
+ within->code = expression_9(mpl);
+ if (within->code->type != A_ELEMSET)
+ error(mpl, "expression following within has invalid type"
+ );
+ xassert(within->code->dim > 0);
+ /* check/set dimension of set members */
+ if (set->dimen == 0) set->dimen = within->code->dim;
+ if (set->dimen != within->code->dim)
+ error(mpl, "set expression following within must have di"
+ "mension %d rather than %d",
+ set->dimen, within->code->dim);
+ }
+ else if (mpl->token == T_ASSIGN)
+ { /* assignment expression */
+ if (!(set->assign == NULL && set->option == NULL &&
+ set->gadget == NULL))
+err: error(mpl, "at most one := or default/data allowed");
+ get_token(mpl /* := */);
+ /* parse an expression that follows ':=' */
+ set->assign = expression_9(mpl);
+ if (set->assign->type != A_ELEMSET)
+ error(mpl, "expression following := has invalid type");
+ xassert(set->assign->dim > 0);
+ /* check/set dimension of set members */
+ if (set->dimen == 0) set->dimen = set->assign->dim;
+ if (set->dimen != set->assign->dim)
+ error(mpl, "set expression following := must have dimens"
+ "ion %d rather than %d",
+ set->dimen, set->assign->dim);
+ }
+ else if (is_keyword(mpl, "default"))
+ { /* expression for default value */
+ if (!(set->assign == NULL && set->option == NULL)) goto err;
+ get_token(mpl /* := */);
+ /* parse an expression that follows 'default' */
+ set->option = expression_9(mpl);
+ if (set->option->type != A_ELEMSET)
+ error(mpl, "expression following default has invalid typ"
+ "e");
+ xassert(set->option->dim > 0);
+ /* check/set dimension of set members */
+ if (set->dimen == 0) set->dimen = set->option->dim;
+ if (set->dimen != set->option->dim)
+ error(mpl, "set expression following default must have d"
+ "imension %d rather than %d",
+ set->dimen, set->option->dim);
+ }
+#if 1 /* 12/XII-2008 */
+ else if (is_keyword(mpl, "data"))
+ { /* gadget to initialize the set by data from plain set */
+ GADGET *gadget;
+ AVLNODE *node;
+ int i, k, fff[20];
+ if (!(set->assign == NULL && set->gadget == NULL)) goto err;
+ get_token(mpl /* data */);
+ set->gadget = gadget = alloc(GADGET);
+ /* set name must follow the keyword 'data' */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s",
+ mpl->image);
+ else
+ error(mpl, "set name missing where expected");
+ /* find the set in the symbolic name table */
+ node = avl_find_node(mpl->tree, mpl->image);
+ if (node == NULL)
+ error(mpl, "%s not defined", mpl->image);
+ if (avl_get_node_type(node) != A_SET)
+err1: error(mpl, "%s not a plain set", mpl->image);
+ gadget->set = avl_get_node_link(node);
+ if (gadget->set->dim != 0) goto err1;
+ if (gadget->set == set)
+ error(mpl, "set cannot be initialized by itself");
+ /* check and set dimensions */
+ if (set->dim >= gadget->set->dimen)
+err2: error(mpl, "dimension of %s too small", mpl->image);
+ if (set->dimen == 0)
+ set->dimen = gadget->set->dimen - set->dim;
+ if (set->dim + set->dimen > gadget->set->dimen)
+ goto err2;
+ else if (set->dim + set->dimen < gadget->set->dimen)
+ error(mpl, "dimension of %s too big", mpl->image);
+ get_token(mpl /* set name */);
+ /* left parenthesis must follow the set name */
+ if (mpl->token == T_LEFT)
+ get_token(mpl /* ( */);
+ else
+ error(mpl, "left parenthesis missing where expected");
+ /* parse permutation of component numbers */
+ for (k = 0; k < gadget->set->dimen; k++) fff[k] = 0;
+ k = 0;
+ for (;;)
+ { if (mpl->token != T_NUMBER)
+ error(mpl, "component number missing where expected");
+ if (str2int(mpl->image, &i) != 0)
+err3: error(mpl, "component number must be integer between "
+ "1 and %d", gadget->set->dimen);
+ if (!(1 <= i && i <= gadget->set->dimen)) goto err3;
+ if (fff[i-1] != 0)
+ error(mpl, "component %d multiply specified", i);
+ gadget->ind[k++] = i, fff[i-1] = 1;
+ xassert(k <= gadget->set->dimen);
+ get_token(mpl /* number */);
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RIGHT)
+ break;
+ else
+ error(mpl, "syntax error in data attribute");
+ }
+ if (k < gadget->set->dimen)
+ error(mpl, "there are must be %d components rather than "
+ "%d", gadget->set->dimen, k);
+ get_token(mpl /* ) */);
+ }
+#endif
+ else
+ error(mpl, "syntax error in set statement");
+ }
+ /* close the domain scope */
+ if (set->domain != NULL) close_scope(mpl, set->domain);
+ /* if dimension of set members is still unknown, set it to 1 */
+ if (set->dimen == 0) set->dimen = 1;
+ /* the set statement has been completely parsed */
+ xassert(mpl->token == T_SEMICOLON);
+ get_token(mpl /* ; */);
+ return set;
+}
+
+/*----------------------------------------------------------------------
+-- parameter_statement - parse parameter statement.
+--
+-- This routine parses parameter statement using the syntax:
+--
+-- <parameter statement> ::= param <symbolic name> <alias> <domain>
+-- <attributes> ;
+-- <alias> ::= <empty>
+-- <alias> ::= <string literal>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <attributes> ::= <empty>
+-- <attributes> ::= <attributes> , integer
+-- <attributes> ::= <attributes> , binary
+-- <attributes> ::= <attributes> , symbolic
+-- <attributes> ::= <attributes> , <rho> <expression 5>
+-- <attributes> ::= <attributes> , in <expression 9>
+-- <attributes> ::= <attributes> , := <expression 5>
+-- <attributes> ::= <attributes> , default <expression 5>
+-- <rho> ::= < | <= | = | == | >= | > | <> | !=
+--
+-- Commae in <attributes> are optional and may be omitted anywhere. */
+
+PARAMETER *parameter_statement(MPL *mpl)
+{ PARAMETER *par;
+ int integer_used = 0, binary_used = 0, symbolic_used = 0;
+ xassert(is_keyword(mpl, "param"));
+ get_token(mpl /* param */);
+ /* symbolic name must follow the keyword 'param' */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create model parameter */
+ par = alloc(PARAMETER);
+ par->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(par->name, mpl->image);
+ par->alias = NULL;
+ par->dim = 0;
+ par->domain = NULL;
+ par->type = A_NUMERIC;
+ par->cond = NULL;
+ par->in = NULL;
+ par->assign = NULL;
+ par->option = NULL;
+ par->data = 0;
+ par->defval = NULL;
+ par->array = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { par->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(par->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { par->domain = indexing_expression(mpl);
+ par->dim = domain_arity(mpl, par->domain);
+ }
+ /* include the parameter name in the symbolic names table */
+ { AVLNODE *node;
+ node = avl_insert_node(mpl->tree, par->name);
+ avl_set_node_type(node, A_PARAMETER);
+ avl_set_node_link(node, (void *)par);
+ }
+ /* parse the list of optional attributes */
+ for (;;)
+ { if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_SEMICOLON)
+ break;
+ if (is_keyword(mpl, "integer"))
+ { if (integer_used)
+ error(mpl, "at most one integer allowed");
+ if (par->type == A_SYMBOLIC)
+ error(mpl, "symbolic parameter cannot be integer");
+ if (par->type != A_BINARY) par->type = A_INTEGER;
+ integer_used = 1;
+ get_token(mpl /* integer */);
+ }
+ else if (is_keyword(mpl, "binary"))
+bin: { if (binary_used)
+ error(mpl, "at most one binary allowed");
+ if (par->type == A_SYMBOLIC)
+ error(mpl, "symbolic parameter cannot be binary");
+ par->type = A_BINARY;
+ binary_used = 1;
+ get_token(mpl /* binary */);
+ }
+ else if (is_keyword(mpl, "logical"))
+ { if (!mpl->as_binary)
+ { warning(mpl, "keyword logical understood as binary");
+ mpl->as_binary = 1;
+ }
+ goto bin;
+ }
+ else if (is_keyword(mpl, "symbolic"))
+ { if (symbolic_used)
+ error(mpl, "at most one symbolic allowed");
+ if (par->type != A_NUMERIC)
+ error(mpl, "integer or binary parameter cannot be symbol"
+ "ic");
+ /* the parameter may be referenced from expressions given
+ in the same parameter declaration, so its type must be
+ completed before parsing that expressions */
+ if (!(par->cond == NULL && par->in == NULL &&
+ par->assign == NULL && par->option == NULL))
+ error(mpl, "keyword symbolic must precede any other para"
+ "meter attributes");
+ par->type = A_SYMBOLIC;
+ symbolic_used = 1;
+ get_token(mpl /* symbolic */);
+ }
+ else if (mpl->token == T_LT || mpl->token == T_LE ||
+ mpl->token == T_EQ || mpl->token == T_GE ||
+ mpl->token == T_GT || mpl->token == T_NE)
+ { /* restricting condition */
+ CONDITION *cond, *temp;
+ char opstr[8];
+ /* create new restricting condition list entry and append
+ it to the conditions list */
+ cond = alloc(CONDITION);
+ switch (mpl->token)
+ { case T_LT:
+ cond->rho = O_LT, strcpy(opstr, mpl->image); break;
+ case T_LE:
+ cond->rho = O_LE, strcpy(opstr, mpl->image); break;
+ case T_EQ:
+ cond->rho = O_EQ, strcpy(opstr, mpl->image); break;
+ case T_GE:
+ cond->rho = O_GE, strcpy(opstr, mpl->image); break;
+ case T_GT:
+ cond->rho = O_GT, strcpy(opstr, mpl->image); break;
+ case T_NE:
+ cond->rho = O_NE, strcpy(opstr, mpl->image); break;
+ default:
+ xassert(mpl->token != mpl->token);
+ }
+ xassert(strlen(opstr) < sizeof(opstr));
+ cond->code = NULL;
+ cond->next = NULL;
+ if (par->cond == NULL)
+ par->cond = cond;
+ else
+ { for (temp = par->cond; temp->next != NULL; temp =
+ temp->next);
+ temp->next = cond;
+ }
+#if 0 /* 13/VIII-2008 */
+ if (par->type == A_SYMBOLIC &&
+ !(cond->rho == O_EQ || cond->rho == O_NE))
+ error(mpl, "inequality restriction not allowed");
+#endif
+ get_token(mpl /* rho */);
+ /* parse an expression that follows relational operator */
+ cond->code = expression_5(mpl);
+ if (!(cond->code->type == A_NUMERIC ||
+ cond->code->type == A_SYMBOLIC))
+ error(mpl, "expression following %s has invalid type",
+ opstr);
+ xassert(cond->code->dim == 0);
+ /* convert to the parameter type, if necessary */
+ if (par->type != A_SYMBOLIC && cond->code->type ==
+ A_SYMBOLIC)
+ cond->code = make_unary(mpl, O_CVTNUM, cond->code,
+ A_NUMERIC, 0);
+ if (par->type == A_SYMBOLIC && cond->code->type !=
+ A_SYMBOLIC)
+ cond->code = make_unary(mpl, O_CVTSYM, cond->code,
+ A_SYMBOLIC, 0);
+ }
+ else if (mpl->token == T_IN || mpl->token == T_WITHIN)
+ { /* restricting superset */
+ WITHIN *in, *temp;
+ if (mpl->token == T_WITHIN && !mpl->as_in)
+ { warning(mpl, "keyword within understood as in");
+ mpl->as_in = 1;
+ }
+ get_token(mpl /* in */);
+ /* create new restricting superset list entry and append it
+ to the in-list */
+ in = alloc(WITHIN);
+ in->code = NULL;
+ in->next = NULL;
+ if (par->in == NULL)
+ par->in = in;
+ else
+ { for (temp = par->in; temp->next != NULL; temp =
+ temp->next);
+ temp->next = in;
+ }
+ /* parse an expression that follows 'in' */
+ in->code = expression_9(mpl);
+ if (in->code->type != A_ELEMSET)
+ error(mpl, "expression following in has invalid type");
+ xassert(in->code->dim > 0);
+ if (in->code->dim != 1)
+ error(mpl, "set expression following in must have dimens"
+ "ion 1 rather than %d", in->code->dim);
+ }
+ else if (mpl->token == T_ASSIGN)
+ { /* assignment expression */
+ if (!(par->assign == NULL && par->option == NULL))
+err: error(mpl, "at most one := or default allowed");
+ get_token(mpl /* := */);
+ /* parse an expression that follows ':=' */
+ par->assign = expression_5(mpl);
+ /* the expression must be of numeric/symbolic type */
+ if (!(par->assign->type == A_NUMERIC ||
+ par->assign->type == A_SYMBOLIC))
+ error(mpl, "expression following := has invalid type");
+ xassert(par->assign->dim == 0);
+ /* convert to the parameter type, if necessary */
+ if (par->type != A_SYMBOLIC && par->assign->type ==
+ A_SYMBOLIC)
+ par->assign = make_unary(mpl, O_CVTNUM, par->assign,
+ A_NUMERIC, 0);
+ if (par->type == A_SYMBOLIC && par->assign->type !=
+ A_SYMBOLIC)
+ par->assign = make_unary(mpl, O_CVTSYM, par->assign,
+ A_SYMBOLIC, 0);
+ }
+ else if (is_keyword(mpl, "default"))
+ { /* expression for default value */
+ if (!(par->assign == NULL && par->option == NULL)) goto err;
+ get_token(mpl /* default */);
+ /* parse an expression that follows 'default' */
+ par->option = expression_5(mpl);
+ if (!(par->option->type == A_NUMERIC ||
+ par->option->type == A_SYMBOLIC))
+ error(mpl, "expression following default has invalid typ"
+ "e");
+ xassert(par->option->dim == 0);
+ /* convert to the parameter type, if necessary */
+ if (par->type != A_SYMBOLIC && par->option->type ==
+ A_SYMBOLIC)
+ par->option = make_unary(mpl, O_CVTNUM, par->option,
+ A_NUMERIC, 0);
+ if (par->type == A_SYMBOLIC && par->option->type !=
+ A_SYMBOLIC)
+ par->option = make_unary(mpl, O_CVTSYM, par->option,
+ A_SYMBOLIC, 0);
+ }
+ else
+ error(mpl, "syntax error in parameter statement");
+ }
+ /* close the domain scope */
+ if (par->domain != NULL) close_scope(mpl, par->domain);
+ /* the parameter statement has been completely parsed */
+ xassert(mpl->token == T_SEMICOLON);
+ get_token(mpl /* ; */);
+ return par;
+}
+
+/*----------------------------------------------------------------------
+-- variable_statement - parse variable statement.
+--
+-- This routine parses variable statement using the syntax:
+--
+-- <variable statement> ::= var <symbolic name> <alias> <domain>
+-- <attributes> ;
+-- <alias> ::= <empty>
+-- <alias> ::= <string literal>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <attributes> ::= <empty>
+-- <attributes> ::= <attributes> , integer
+-- <attributes> ::= <attributes> , binary
+-- <attributes> ::= <attributes> , <rho> <expression 5>
+-- <rho> ::= >= | <= | = | ==
+--
+-- Commae in <attributes> are optional and may be omitted anywhere. */
+
+VARIABLE *variable_statement(MPL *mpl)
+{ VARIABLE *var;
+ int integer_used = 0, binary_used = 0;
+ xassert(is_keyword(mpl, "var"));
+ if (mpl->flag_s)
+ error(mpl, "variable statement must precede solve statement");
+ get_token(mpl /* var */);
+ /* symbolic name must follow the keyword 'var' */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create model variable */
+ var = alloc(VARIABLE);
+ var->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(var->name, mpl->image);
+ var->alias = NULL;
+ var->dim = 0;
+ var->domain = NULL;
+ var->type = A_NUMERIC;
+ var->lbnd = NULL;
+ var->ubnd = NULL;
+ var->array = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { var->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(var->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { var->domain = indexing_expression(mpl);
+ var->dim = domain_arity(mpl, var->domain);
+ }
+ /* include the variable name in the symbolic names table */
+ { AVLNODE *node;
+ node = avl_insert_node(mpl->tree, var->name);
+ avl_set_node_type(node, A_VARIABLE);
+ avl_set_node_link(node, (void *)var);
+ }
+ /* parse the list of optional attributes */
+ for (;;)
+ { if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_SEMICOLON)
+ break;
+ if (is_keyword(mpl, "integer"))
+ { if (integer_used)
+ error(mpl, "at most one integer allowed");
+ if (var->type != A_BINARY) var->type = A_INTEGER;
+ integer_used = 1;
+ get_token(mpl /* integer */);
+ }
+ else if (is_keyword(mpl, "binary"))
+bin: { if (binary_used)
+ error(mpl, "at most one binary allowed");
+ var->type = A_BINARY;
+ binary_used = 1;
+ get_token(mpl /* binary */);
+ }
+ else if (is_keyword(mpl, "logical"))
+ { if (!mpl->as_binary)
+ { warning(mpl, "keyword logical understood as binary");
+ mpl->as_binary = 1;
+ }
+ goto bin;
+ }
+ else if (is_keyword(mpl, "symbolic"))
+ error(mpl, "variable cannot be symbolic");
+ else if (mpl->token == T_GE)
+ { /* lower bound */
+ if (var->lbnd != NULL)
+ { if (var->lbnd == var->ubnd)
+ error(mpl, "both fixed value and lower bound not allo"
+ "wed");
+ else
+ error(mpl, "at most one lower bound allowed");
+ }
+ get_token(mpl /* >= */);
+ /* parse an expression that specifies the lower bound */
+ var->lbnd = expression_5(mpl);
+ if (var->lbnd->type == A_SYMBOLIC)
+ var->lbnd = make_unary(mpl, O_CVTNUM, var->lbnd,
+ A_NUMERIC, 0);
+ if (var->lbnd->type != A_NUMERIC)
+ error(mpl, "expression following >= has invalid type");
+ xassert(var->lbnd->dim == 0);
+ }
+ else if (mpl->token == T_LE)
+ { /* upper bound */
+ if (var->ubnd != NULL)
+ { if (var->ubnd == var->lbnd)
+ error(mpl, "both fixed value and upper bound not allo"
+ "wed");
+ else
+ error(mpl, "at most one upper bound allowed");
+ }
+ get_token(mpl /* <= */);
+ /* parse an expression that specifies the upper bound */
+ var->ubnd = expression_5(mpl);
+ if (var->ubnd->type == A_SYMBOLIC)
+ var->ubnd = make_unary(mpl, O_CVTNUM, var->ubnd,
+ A_NUMERIC, 0);
+ if (var->ubnd->type != A_NUMERIC)
+ error(mpl, "expression following <= has invalid type");
+ xassert(var->ubnd->dim == 0);
+ }
+ else if (mpl->token == T_EQ)
+ { /* fixed value */
+ char opstr[8];
+ if (!(var->lbnd == NULL && var->ubnd == NULL))
+ { if (var->lbnd == var->ubnd)
+ error(mpl, "at most one fixed value allowed");
+ else if (var->lbnd != NULL)
+ error(mpl, "both lower bound and fixed value not allo"
+ "wed");
+ else
+ error(mpl, "both upper bound and fixed value not allo"
+ "wed");
+ }
+ strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ get_token(mpl /* = | == */);
+ /* parse an expression that specifies the fixed value */
+ var->lbnd = expression_5(mpl);
+ if (var->lbnd->type == A_SYMBOLIC)
+ var->lbnd = make_unary(mpl, O_CVTNUM, var->lbnd,
+ A_NUMERIC, 0);
+ if (var->lbnd->type != A_NUMERIC)
+ error(mpl, "expression following %s has invalid type",
+ opstr);
+ xassert(var->lbnd->dim == 0);
+ /* indicate that the variable is fixed, not bounded */
+ var->ubnd = var->lbnd;
+ }
+ else if (mpl->token == T_LT || mpl->token == T_GT ||
+ mpl->token == T_NE)
+ error(mpl, "strict bound not allowed");
+ else
+ error(mpl, "syntax error in variable statement");
+ }
+ /* close the domain scope */
+ if (var->domain != NULL) close_scope(mpl, var->domain);
+ /* the variable statement has been completely parsed */
+ xassert(mpl->token == T_SEMICOLON);
+ get_token(mpl /* ; */);
+ return var;
+}
+
+/*----------------------------------------------------------------------
+-- constraint_statement - parse constraint statement.
+--
+-- This routine parses constraint statement using the syntax:
+--
+-- <constraint statement> ::= <subject to> <symbolic name> <alias>
+-- <domain> : <constraint> ;
+-- <subject to> ::= <empty>
+-- <subject to> ::= subject to
+-- <subject to> ::= subj to
+-- <subject to> ::= s.t.
+-- <alias> ::= <empty>
+-- <alias> ::= <string literal>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <constraint> ::= <formula> , >= <formula>
+-- <constraint> ::= <formula> , <= <formula>
+-- <constraint> ::= <formula> , = <formula>
+-- <constraint> ::= <formula> , <= <formula> , <= <formula>
+-- <constraint> ::= <formula> , >= <formula> , >= <formula>
+-- <formula> ::= <expression 5>
+--
+-- Commae in <constraint> are optional and may be omitted anywhere. */
+
+CONSTRAINT *constraint_statement(MPL *mpl)
+{ CONSTRAINT *con;
+ CODE *first, *second, *third;
+ int rho;
+ char opstr[8];
+ if (mpl->flag_s)
+ error(mpl, "constraint statement must precede solve statement")
+ ;
+ if (is_keyword(mpl, "subject"))
+ { get_token(mpl /* subject */);
+ if (!is_keyword(mpl, "to"))
+ error(mpl, "keyword subject to incomplete");
+ get_token(mpl /* to */);
+ }
+ else if (is_keyword(mpl, "subj"))
+ { get_token(mpl /* subj */);
+ if (!is_keyword(mpl, "to"))
+ error(mpl, "keyword subj to incomplete");
+ get_token(mpl /* to */);
+ }
+ else if (mpl->token == T_SPTP)
+ get_token(mpl /* s.t. */);
+ /* the current token must be symbolic name of constraint */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create model constraint */
+ con = alloc(CONSTRAINT);
+ con->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(con->name, mpl->image);
+ con->alias = NULL;
+ con->dim = 0;
+ con->domain = NULL;
+ con->type = A_CONSTRAINT;
+ con->code = NULL;
+ con->lbnd = NULL;
+ con->ubnd = NULL;
+ con->array = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { con->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(con->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { con->domain = indexing_expression(mpl);
+ con->dim = domain_arity(mpl, con->domain);
+ }
+ /* include the constraint name in the symbolic names table */
+ { AVLNODE *node;
+ node = avl_insert_node(mpl->tree, con->name);
+ avl_set_node_type(node, A_CONSTRAINT);
+ avl_set_node_link(node, (void *)con);
+ }
+ /* the colon must precede the first expression */
+ if (mpl->token != T_COLON)
+ error(mpl, "colon missing where expected");
+ get_token(mpl /* : */);
+ /* parse the first expression */
+ first = expression_5(mpl);
+ if (first->type == A_SYMBOLIC)
+ first = make_unary(mpl, O_CVTNUM, first, A_NUMERIC, 0);
+ if (!(first->type == A_NUMERIC || first->type == A_FORMULA))
+ error(mpl, "expression following colon has invalid type");
+ xassert(first->dim == 0);
+ /* relational operator must follow the first expression */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ switch (mpl->token)
+ { case T_LE:
+ case T_GE:
+ case T_EQ:
+ break;
+ case T_LT:
+ case T_GT:
+ case T_NE:
+ error(mpl, "strict inequality not allowed");
+ case T_SEMICOLON:
+ error(mpl, "constraint must be equality or inequality");
+ default:
+ goto err;
+ }
+ rho = mpl->token;
+ strcpy(opstr, mpl->image);
+ xassert(strlen(opstr) < sizeof(opstr));
+ get_token(mpl /* rho */);
+ /* parse the second expression */
+ second = expression_5(mpl);
+ if (second->type == A_SYMBOLIC)
+ second = make_unary(mpl, O_CVTNUM, second, A_NUMERIC, 0);
+ if (!(second->type == A_NUMERIC || second->type == A_FORMULA))
+ error(mpl, "expression following %s has invalid type", opstr);
+ xassert(second->dim == 0);
+ /* check a token that follow the second expression */
+ if (mpl->token == T_COMMA)
+ { get_token(mpl /* , */);
+ if (mpl->token == T_SEMICOLON) goto err;
+ }
+ if (mpl->token == T_LT || mpl->token == T_LE ||
+ mpl->token == T_EQ || mpl->token == T_GE ||
+ mpl->token == T_GT || mpl->token == T_NE)
+ { /* it is another relational operator, therefore the constraint
+ is double inequality */
+ if (rho == T_EQ || mpl->token != rho)
+ error(mpl, "double inequality must be ... <= ... <= ... or "
+ "... >= ... >= ...");
+ /* the first expression cannot be linear form */
+ if (first->type == A_FORMULA)
+ error(mpl, "leftmost expression in double inequality cannot"
+ " be linear form");
+ get_token(mpl /* rho */);
+ /* parse the third expression */
+ third = expression_5(mpl);
+ if (third->type == A_SYMBOLIC)
+ third = make_unary(mpl, O_CVTNUM, second, A_NUMERIC, 0);
+ if (!(third->type == A_NUMERIC || third->type == A_FORMULA))
+ error(mpl, "rightmost expression in double inequality const"
+ "raint has invalid type");
+ xassert(third->dim == 0);
+ /* the third expression also cannot be linear form */
+ if (third->type == A_FORMULA)
+ error(mpl, "rightmost expression in double inequality canno"
+ "t be linear form");
+ }
+ else
+ { /* the constraint is equality or single inequality */
+ third = NULL;
+ }
+ /* close the domain scope */
+ if (con->domain != NULL) close_scope(mpl, con->domain);
+ /* convert all expressions to linear form, if necessary */
+ if (first->type != A_FORMULA)
+ first = make_unary(mpl, O_CVTLFM, first, A_FORMULA, 0);
+ if (second->type != A_FORMULA)
+ second = make_unary(mpl, O_CVTLFM, second, A_FORMULA, 0);
+ if (third != NULL)
+ third = make_unary(mpl, O_CVTLFM, third, A_FORMULA, 0);
+ /* arrange expressions in the constraint */
+ if (third == NULL)
+ { /* the constraint is equality or single inequality */
+ switch (rho)
+ { case T_LE:
+ /* first <= second */
+ con->code = first;
+ con->lbnd = NULL;
+ con->ubnd = second;
+ break;
+ case T_GE:
+ /* first >= second */
+ con->code = first;
+ con->lbnd = second;
+ con->ubnd = NULL;
+ break;
+ case T_EQ:
+ /* first = second */
+ con->code = first;
+ con->lbnd = second;
+ con->ubnd = second;
+ break;
+ default:
+ xassert(rho != rho);
+ }
+ }
+ else
+ { /* the constraint is double inequality */
+ switch (rho)
+ { case T_LE:
+ /* first <= second <= third */
+ con->code = second;
+ con->lbnd = first;
+ con->ubnd = third;
+ break;
+ case T_GE:
+ /* first >= second >= third */
+ con->code = second;
+ con->lbnd = third;
+ con->ubnd = first;
+ break;
+ default:
+ xassert(rho != rho);
+ }
+ }
+ /* the constraint statement has been completely parsed */
+ if (mpl->token != T_SEMICOLON)
+err: error(mpl, "syntax error in constraint statement");
+ get_token(mpl /* ; */);
+ return con;
+}
+
+/*----------------------------------------------------------------------
+-- objective_statement - parse objective statement.
+--
+-- This routine parses objective statement using the syntax:
+--
+-- <objective statement> ::= <verb> <symbolic name> <alias> <domain> :
+-- <formula> ;
+-- <verb> ::= minimize
+-- <verb> ::= maximize
+-- <alias> ::= <empty>
+-- <alias> ::= <string literal>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <formula> ::= <expression 5> */
+
+CONSTRAINT *objective_statement(MPL *mpl)
+{ CONSTRAINT *obj;
+ int type;
+ if (is_keyword(mpl, "minimize"))
+ type = A_MINIMIZE;
+ else if (is_keyword(mpl, "maximize"))
+ type = A_MAXIMIZE;
+ else
+ xassert(mpl != mpl);
+ if (mpl->flag_s)
+ error(mpl, "objective statement must precede solve statement");
+ get_token(mpl /* minimize | maximize */);
+ /* symbolic name must follow the verb 'minimize' or 'maximize' */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create model objective */
+ obj = alloc(CONSTRAINT);
+ obj->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(obj->name, mpl->image);
+ obj->alias = NULL;
+ obj->dim = 0;
+ obj->domain = NULL;
+ obj->type = type;
+ obj->code = NULL;
+ obj->lbnd = NULL;
+ obj->ubnd = NULL;
+ obj->array = NULL;
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { obj->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(obj->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { obj->domain = indexing_expression(mpl);
+ obj->dim = domain_arity(mpl, obj->domain);
+ }
+ /* include the constraint name in the symbolic names table */
+ { AVLNODE *node;
+ node = avl_insert_node(mpl->tree, obj->name);
+ avl_set_node_type(node, A_CONSTRAINT);
+ avl_set_node_link(node, (void *)obj);
+ }
+ /* the colon must precede the objective expression */
+ if (mpl->token != T_COLON)
+ error(mpl, "colon missing where expected");
+ get_token(mpl /* : */);
+ /* parse the objective expression */
+ obj->code = expression_5(mpl);
+ if (obj->code->type == A_SYMBOLIC)
+ obj->code = make_unary(mpl, O_CVTNUM, obj->code, A_NUMERIC, 0);
+ if (obj->code->type == A_NUMERIC)
+ obj->code = make_unary(mpl, O_CVTLFM, obj->code, A_FORMULA, 0);
+ if (obj->code->type != A_FORMULA)
+ error(mpl, "expression following colon has invalid type");
+ xassert(obj->code->dim == 0);
+ /* close the domain scope */
+ if (obj->domain != NULL) close_scope(mpl, obj->domain);
+ /* the objective statement has been completely parsed */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in objective statement");
+ get_token(mpl /* ; */);
+ return obj;
+}
+
+#if 1 /* 11/II-2008 */
+/***********************************************************************
+* table_statement - parse table statement
+*
+* This routine parses table statement using the syntax:
+*
+* <table statement> ::= <input table statement>
+* <table statement> ::= <output table statement>
+*
+* <input table statement> ::=
+* table <table name> <alias> IN <argument list> :
+* <input set> [ <field list> ] , <input list> ;
+* <alias> ::= <empty>
+* <alias> ::= <string literal>
+* <argument list> ::= <expression 5>
+* <argument list> ::= <argument list> <expression 5>
+* <argument list> ::= <argument list> , <expression 5>
+* <input set> ::= <empty>
+* <input set> ::= <set name> <-
+* <field list> ::= <field name>
+* <field list> ::= <field list> , <field name>
+* <input list> ::= <input item>
+* <input list> ::= <input list> , <input item>
+* <input item> ::= <parameter name>
+* <input item> ::= <parameter name> ~ <field name>
+*
+* <output table statement> ::=
+* table <table name> <alias> <domain> OUT <argument list> :
+* <output list> ;
+* <domain> ::= <indexing expression>
+* <output list> ::= <output item>
+* <output list> ::= <output list> , <output item>
+* <output item> ::= <expression 5>
+* <output item> ::= <expression 5> ~ <field name> */
+
+TABLE *table_statement(MPL *mpl)
+{ TABLE *tab;
+ TABARG *last_arg, *arg;
+ TABFLD *last_fld, *fld;
+ TABIN *last_in, *in;
+ TABOUT *last_out, *out;
+ AVLNODE *node;
+ int nflds;
+ char name[MAX_LENGTH+1];
+ xassert(is_keyword(mpl, "table"));
+ get_token(mpl /* solve */);
+ /* symbolic name must follow the keyword table */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "symbolic name missing where expected");
+ /* there must be no other object with the same name */
+ if (avl_find_node(mpl->tree, mpl->image) != NULL)
+ error(mpl, "%s multiply declared", mpl->image);
+ /* create data table */
+ tab = alloc(TABLE);
+ tab->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(tab->name, mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional alias */
+ if (mpl->token == T_STRING)
+ { tab->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(tab->alias, mpl->image);
+ get_token(mpl /* <string literal> */);
+ }
+ else
+ tab->alias = NULL;
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { /* this is output table */
+ tab->type = A_OUTPUT;
+ tab->u.out.domain = indexing_expression(mpl);
+ if (!is_keyword(mpl, "OUT"))
+ error(mpl, "keyword OUT missing where expected");
+ get_token(mpl /* OUT */);
+ }
+ else
+ { /* this is input table */
+ tab->type = A_INPUT;
+ if (!is_keyword(mpl, "IN"))
+ error(mpl, "keyword IN missing where expected");
+ get_token(mpl /* IN */);
+ }
+ /* parse argument list */
+ tab->arg = last_arg = NULL;
+ for (;;)
+ { /* create argument list entry */
+ arg = alloc(TABARG);
+ /* parse argument expression */
+ if (mpl->token == T_COMMA || mpl->token == T_COLON ||
+ mpl->token == T_SEMICOLON)
+ error(mpl, "argument expression missing where expected");
+ arg->code = expression_5(mpl);
+ /* convert the result to symbolic type, if necessary */
+ if (arg->code->type == A_NUMERIC)
+ arg->code =
+ make_unary(mpl, O_CVTSYM, arg->code, A_SYMBOLIC, 0);
+ /* check that now the result is of symbolic type */
+ if (arg->code->type != A_SYMBOLIC)
+ error(mpl, "argument expression has invalid type");
+ /* add the entry to the end of the list */
+ arg->next = NULL;
+ if (last_arg == NULL)
+ tab->arg = arg;
+ else
+ last_arg->next = arg;
+ last_arg = arg;
+ /* argument expression has been parsed */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_COLON || mpl->token == T_SEMICOLON)
+ break;
+ }
+ xassert(tab->arg != NULL);
+ /* argument list must end with colon */
+ if (mpl->token == T_COLON)
+ get_token(mpl /* : */);
+ else
+ error(mpl, "colon missing where expected");
+ /* parse specific part of the table statement */
+ switch (tab->type)
+ { case A_INPUT: goto input_table;
+ case A_OUTPUT: goto output_table;
+ default: xassert(tab != tab);
+ }
+input_table:
+ /* parse optional set name */
+ if (mpl->token == T_NAME)
+ { node = avl_find_node(mpl->tree, mpl->image);
+ if (node == NULL)
+ error(mpl, "%s not defined", mpl->image);
+ if (avl_get_node_type(node) != A_SET)
+ error(mpl, "%s not a set", mpl->image);
+ tab->u.in.set = (SET *)avl_get_node_link(node);
+ if (tab->u.in.set->assign != NULL)
+ error(mpl, "%s needs no data", mpl->image);
+ if (tab->u.in.set->dim != 0)
+ error(mpl, "%s must be a simple set", mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ if (mpl->token == T_INPUT)
+ get_token(mpl /* <- */);
+ else
+ error(mpl, "delimiter <- missing where expected");
+ }
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ tab->u.in.set = NULL;
+ /* parse field list */
+ tab->u.in.fld = last_fld = NULL;
+ nflds = 0;
+ if (mpl->token == T_LBRACKET)
+ get_token(mpl /* [ */);
+ else
+ error(mpl, "field list missing where expected");
+ for (;;)
+ { /* create field list entry */
+ fld = alloc(TABFLD);
+ /* parse field name */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl,
+ "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "field name missing where expected");
+ fld->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
+ strcpy(fld->name, mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ /* add the entry to the end of the list */
+ fld->next = NULL;
+ if (last_fld == NULL)
+ tab->u.in.fld = fld;
+ else
+ last_fld->next = fld;
+ last_fld = fld;
+ nflds++;
+ /* field name has been parsed */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RBRACKET)
+ break;
+ else
+ error(mpl, "syntax error in field list");
+ }
+ /* check that the set dimen is equal to the number of fields */
+ if (tab->u.in.set != NULL && tab->u.in.set->dimen != nflds)
+ error(mpl, "there must be %d field%s rather than %d",
+ tab->u.in.set->dimen, tab->u.in.set->dimen == 1 ? "" : "s",
+ nflds);
+ get_token(mpl /* ] */);
+ /* parse optional input list */
+ tab->u.in.list = last_in = NULL;
+ while (mpl->token == T_COMMA)
+ { get_token(mpl /* , */);
+ /* create input list entry */
+ in = alloc(TABIN);
+ /* parse parameter name */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl,
+ "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "parameter name missing where expected");
+ node = avl_find_node(mpl->tree, mpl->image);
+ if (node == NULL)
+ error(mpl, "%s not defined", mpl->image);
+ if (avl_get_node_type(node) != A_PARAMETER)
+ error(mpl, "%s not a parameter", mpl->image);
+ in->par = (PARAMETER *)avl_get_node_link(node);
+ if (in->par->dim != nflds)
+ error(mpl, "%s must have %d subscript%s rather than %d",
+ mpl->image, nflds, nflds == 1 ? "" : "s", in->par->dim);
+ if (in->par->assign != NULL)
+ error(mpl, "%s needs no data", mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ /* parse optional field name */
+ if (mpl->token == T_TILDE)
+ { get_token(mpl /* ~ */);
+ /* parse field name */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl,
+ "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "field name missing where expected");
+ xassert(strlen(mpl->image) < sizeof(name));
+ strcpy(name, mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ }
+ else
+ { /* field name is the same as the parameter name */
+ xassert(strlen(in->par->name) < sizeof(name));
+ strcpy(name, in->par->name);
+ }
+ /* assign field name */
+ in->name = dmp_get_atomv(mpl->pool, strlen(name)+1);
+ strcpy(in->name, name);
+ /* add the entry to the end of the list */
+ in->next = NULL;
+ if (last_in == NULL)
+ tab->u.in.list = in;
+ else
+ last_in->next = in;
+ last_in = in;
+ }
+ goto end_of_table;
+output_table:
+ /* parse output list */
+ tab->u.out.list = last_out = NULL;
+ for (;;)
+ { /* create output list entry */
+ out = alloc(TABOUT);
+ /* parse expression */
+ if (mpl->token == T_COMMA || mpl->token == T_SEMICOLON)
+ error(mpl, "expression missing where expected");
+ if (mpl->token == T_NAME)
+ { xassert(strlen(mpl->image) < sizeof(name));
+ strcpy(name, mpl->image);
+ }
+ else
+ name[0] = '\0';
+ out->code = expression_5(mpl);
+ /* parse optional field name */
+ if (mpl->token == T_TILDE)
+ { get_token(mpl /* ~ */);
+ /* parse field name */
+ if (mpl->token == T_NAME)
+ ;
+ else if (is_reserved(mpl))
+ error(mpl,
+ "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "field name missing where expected");
+ xassert(strlen(mpl->image) < sizeof(name));
+ strcpy(name, mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ }
+ /* assign field name */
+ if (name[0] == '\0')
+ error(mpl, "field name required");
+ out->name = dmp_get_atomv(mpl->pool, strlen(name)+1);
+ strcpy(out->name, name);
+ /* add the entry to the end of the list */
+ out->next = NULL;
+ if (last_out == NULL)
+ tab->u.out.list = out;
+ else
+ last_out->next = out;
+ last_out = out;
+ /* output item has been parsed */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_SEMICOLON)
+ break;
+ else
+ error(mpl, "syntax error in output list");
+ }
+ /* close the domain scope */
+ close_scope(mpl,tab->u.out.domain);
+end_of_table:
+ /* the table statement must end with semicolon */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in table statement");
+ get_token(mpl /* ; */);
+ return tab;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- solve_statement - parse solve statement.
+--
+-- This routine parses solve statement using the syntax:
+--
+-- <solve statement> ::= solve ;
+--
+-- The solve statement can be used at most once. */
+
+void *solve_statement(MPL *mpl)
+{ xassert(is_keyword(mpl, "solve"));
+ if (mpl->flag_s)
+ error(mpl, "at most one solve statement allowed");
+ mpl->flag_s = 1;
+ get_token(mpl /* solve */);
+ /* semicolon must follow solve statement */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in solve statement");
+ get_token(mpl /* ; */);
+ return NULL;
+}
+
+/*----------------------------------------------------------------------
+-- check_statement - parse check statement.
+--
+-- This routine parses check statement using the syntax:
+--
+-- <check statement> ::= check <domain> : <expression 13> ;
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+--
+-- If <domain> is omitted, colon following it may also be omitted. */
+
+CHECK *check_statement(MPL *mpl)
+{ CHECK *chk;
+ xassert(is_keyword(mpl, "check"));
+ /* create check descriptor */
+ chk = alloc(CHECK);
+ chk->domain = NULL;
+ chk->code = NULL;
+ get_token(mpl /* check */);
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { chk->domain = indexing_expression(mpl);
+#if 0
+ if (mpl->token != T_COLON)
+ error(mpl, "colon missing where expected");
+#endif
+ }
+ /* skip optional colon */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* parse logical expression */
+ chk->code = expression_13(mpl);
+ if (chk->code->type != A_LOGICAL)
+ error(mpl, "expression has invalid type");
+ xassert(chk->code->dim == 0);
+ /* close the domain scope */
+ if (chk->domain != NULL) close_scope(mpl, chk->domain);
+ /* the check statement has been completely parsed */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in check statement");
+ get_token(mpl /* ; */);
+ return chk;
+}
+
+#if 1 /* 15/V-2010 */
+/*----------------------------------------------------------------------
+-- display_statement - parse display statement.
+--
+-- This routine parses display statement using the syntax:
+--
+-- <display statement> ::= display <domain> : <display list> ;
+-- <display statement> ::= display <domain> <display list> ;
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <display list> ::= <display entry>
+-- <display list> ::= <display list> , <display entry>
+-- <display entry> ::= <dummy index>
+-- <display entry> ::= <set name>
+-- <display entry> ::= <set name> [ <subscript list> ]
+-- <display entry> ::= <parameter name>
+-- <display entry> ::= <parameter name> [ <subscript list> ]
+-- <display entry> ::= <variable name>
+-- <display entry> ::= <variable name> [ <subscript list> ]
+-- <display entry> ::= <constraint name>
+-- <display entry> ::= <constraint name> [ <subscript list> ]
+-- <display entry> ::= <expression 13> */
+
+DISPLAY *display_statement(MPL *mpl)
+{ DISPLAY *dpy;
+ DISPLAY1 *entry, *last_entry;
+ xassert(is_keyword(mpl, "display"));
+ /* create display descriptor */
+ dpy = alloc(DISPLAY);
+ dpy->domain = NULL;
+ dpy->list = last_entry = NULL;
+ get_token(mpl /* display */);
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ dpy->domain = indexing_expression(mpl);
+ /* skip optional colon */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* parse display list */
+ for (;;)
+ { /* create new display entry */
+ entry = alloc(DISPLAY1);
+ entry->type = 0;
+ entry->next = NULL;
+ /* and append it to the display list */
+ if (dpy->list == NULL)
+ dpy->list = entry;
+ else
+ last_entry->next = entry;
+ last_entry = entry;
+ /* parse display entry */
+ if (mpl->token == T_NAME)
+ { AVLNODE *node;
+ int next_token;
+ get_token(mpl /* <symbolic name> */);
+ next_token = mpl->token;
+ unget_token(mpl);
+ if (!(next_token == T_COMMA || next_token == T_SEMICOLON))
+ { /* symbolic name begins expression */
+ goto expr;
+ }
+ /* display entry is dummy index or model object */
+ node = avl_find_node(mpl->tree, mpl->image);
+ if (node == NULL)
+ error(mpl, "%s not defined", mpl->image);
+ entry->type = avl_get_node_type(node);
+ switch (avl_get_node_type(node))
+ { case A_INDEX:
+ entry->u.slot =
+ (DOMAIN_SLOT *)avl_get_node_link(node);
+ break;
+ case A_SET:
+ entry->u.set = (SET *)avl_get_node_link(node);
+ break;
+ case A_PARAMETER:
+ entry->u.par = (PARAMETER *)avl_get_node_link(node);
+ break;
+ case A_VARIABLE:
+ entry->u.var = (VARIABLE *)avl_get_node_link(node);
+ if (!mpl->flag_s)
+ error(mpl, "invalid reference to variable %s above"
+ " solve statement", entry->u.var->name);
+ break;
+ case A_CONSTRAINT:
+ entry->u.con = (CONSTRAINT *)avl_get_node_link(node);
+ if (!mpl->flag_s)
+ error(mpl, "invalid reference to %s %s above solve"
+ " statement",
+ entry->u.con->type == A_CONSTRAINT ?
+ "constraint" : "objective", entry->u.con->name);
+ break;
+ default:
+ xassert(node != node);
+ }
+ get_token(mpl /* <symbolic name> */);
+ }
+ else
+expr: { /* display entry is expression */
+ entry->type = A_EXPRESSION;
+ entry->u.code = expression_13(mpl);
+ }
+ /* check a token that follows the entry parsed */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else
+ break;
+ }
+ /* close the domain scope */
+ if (dpy->domain != NULL) close_scope(mpl, dpy->domain);
+ /* the display statement has been completely parsed */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in display statement");
+ get_token(mpl /* ; */);
+ return dpy;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- printf_statement - parse printf statement.
+--
+-- This routine parses print statement using the syntax:
+--
+-- <printf statement> ::= <printf clause> ;
+-- <printf statement> ::= <printf clause> > <file name> ;
+-- <printf statement> ::= <printf clause> >> <file name> ;
+-- <printf clause> ::= printf <domain> : <format> <printf list>
+-- <printf clause> ::= printf <domain> <format> <printf list>
+-- <domain> ::= <empty>
+-- <domain> ::= <indexing expression>
+-- <format> ::= <expression 5>
+-- <printf list> ::= <empty>
+-- <printf list> ::= <printf list> , <printf entry>
+-- <printf entry> ::= <expression 9>
+-- <file name> ::= <expression 5> */
+
+PRINTF *printf_statement(MPL *mpl)
+{ PRINTF *prt;
+ PRINTF1 *entry, *last_entry;
+ xassert(is_keyword(mpl, "printf"));
+ /* create printf descriptor */
+ prt = alloc(PRINTF);
+ prt->domain = NULL;
+ prt->fmt = NULL;
+ prt->list = last_entry = NULL;
+ get_token(mpl /* printf */);
+ /* parse optional indexing expression */
+ if (mpl->token == T_LBRACE)
+ { prt->domain = indexing_expression(mpl);
+#if 0
+ if (mpl->token != T_COLON)
+ error(mpl, "colon missing where expected");
+#endif
+ }
+ /* skip optional colon */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* parse expression for format string */
+ prt->fmt = expression_5(mpl);
+ /* convert it to symbolic type, if necessary */
+ if (prt->fmt->type == A_NUMERIC)
+ prt->fmt = make_unary(mpl, O_CVTSYM, prt->fmt, A_SYMBOLIC, 0);
+ /* check that now the expression is of symbolic type */
+ if (prt->fmt->type != A_SYMBOLIC)
+ error(mpl, "format expression has invalid type");
+ /* parse printf list */
+ while (mpl->token == T_COMMA)
+ { get_token(mpl /* , */);
+ /* create new printf entry */
+ entry = alloc(PRINTF1);
+ entry->code = NULL;
+ entry->next = NULL;
+ /* and append it to the printf list */
+ if (prt->list == NULL)
+ prt->list = entry;
+ else
+ last_entry->next = entry;
+ last_entry = entry;
+ /* parse printf entry */
+ entry->code = expression_9(mpl);
+ if (!(entry->code->type == A_NUMERIC ||
+ entry->code->type == A_SYMBOLIC ||
+ entry->code->type == A_LOGICAL))
+ error(mpl, "only numeric, symbolic, or logical expression a"
+ "llowed");
+ }
+ /* close the domain scope */
+ if (prt->domain != NULL) close_scope(mpl, prt->domain);
+#if 1 /* 14/VII-2006 */
+ /* parse optional redirection */
+ prt->fname = NULL, prt->app = 0;
+ if (mpl->token == T_GT || mpl->token == T_APPEND)
+ { prt->app = (mpl->token == T_APPEND);
+ get_token(mpl /* > or >> */);
+ /* parse expression for file name string */
+ prt->fname = expression_5(mpl);
+ /* convert it to symbolic type, if necessary */
+ if (prt->fname->type == A_NUMERIC)
+ prt->fname = make_unary(mpl, O_CVTSYM, prt->fname,
+ A_SYMBOLIC, 0);
+ /* check that now the expression is of symbolic type */
+ if (prt->fname->type != A_SYMBOLIC)
+ error(mpl, "file name expression has invalid type");
+ }
+#endif
+ /* the printf statement has been completely parsed */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "syntax error in printf statement");
+ get_token(mpl /* ; */);
+ return prt;
+}
+
+/*----------------------------------------------------------------------
+-- for_statement - parse for statement.
+--
+-- This routine parses for statement using the syntax:
+--
+-- <for statement> ::= for <domain> <statement>
+-- <for statement> ::= for <domain> { <statement list> }
+-- <domain> ::= <indexing expression>
+-- <statement list> ::= <empty>
+-- <statement list> ::= <statement list> <statement>
+-- <statement> ::= <check statement>
+-- <statement> ::= <display statement>
+-- <statement> ::= <printf statement>
+-- <statement> ::= <for statement> */
+
+FOR *for_statement(MPL *mpl)
+{ FOR *fur;
+ STATEMENT *stmt, *last_stmt;
+ xassert(is_keyword(mpl, "for"));
+ /* create for descriptor */
+ fur = alloc(FOR);
+ fur->domain = NULL;
+ fur->list = last_stmt = NULL;
+ get_token(mpl /* for */);
+ /* parse indexing expression */
+ if (mpl->token != T_LBRACE)
+ error(mpl, "indexing expression missing where expected");
+ fur->domain = indexing_expression(mpl);
+ /* skip optional colon */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* parse for statement body */
+ if (mpl->token != T_LBRACE)
+ { /* parse simple statement */
+ fur->list = simple_statement(mpl, 1);
+ }
+ else
+ { /* parse compound statement */
+ get_token(mpl /* { */);
+ while (mpl->token != T_RBRACE)
+ { /* parse statement */
+ stmt = simple_statement(mpl, 1);
+ /* and append it to the end of the statement list */
+ if (last_stmt == NULL)
+ fur->list = stmt;
+ else
+ last_stmt->next = stmt;
+ last_stmt = stmt;
+ }
+ get_token(mpl /* } */);
+ }
+ /* close the domain scope */
+ xassert(fur->domain != NULL);
+ close_scope(mpl, fur->domain);
+ /* the for statement has been completely parsed */
+ return fur;
+}
+
+/*----------------------------------------------------------------------
+-- end_statement - parse end statement.
+--
+-- This routine parses end statement using the syntax:
+--
+-- <end statement> ::= end ; <eof> */
+
+void end_statement(MPL *mpl)
+{ if (!mpl->flag_d && is_keyword(mpl, "end") ||
+ mpl->flag_d && is_literal(mpl, "end"))
+ { get_token(mpl /* end */);
+ if (mpl->token == T_SEMICOLON)
+ get_token(mpl /* ; */);
+ else
+ warning(mpl, "no semicolon following end statement; missing"
+ " semicolon inserted");
+ }
+ else
+ warning(mpl, "unexpected end of file; missing end statement in"
+ "serted");
+ if (mpl->token != T_EOF)
+ warning(mpl, "some text detected beyond end statement; text ig"
+ "nored");
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- simple_statement - parse simple statement.
+--
+-- This routine parses simple statement using the syntax:
+--
+-- <statement> ::= <set statement>
+-- <statement> ::= <parameter statement>
+-- <statement> ::= <variable statement>
+-- <statement> ::= <constraint statement>
+-- <statement> ::= <objective statement>
+-- <statement> ::= <solve statement>
+-- <statement> ::= <check statement>
+-- <statement> ::= <display statement>
+-- <statement> ::= <printf statement>
+-- <statement> ::= <for statement>
+--
+-- If the flag spec is set, some statements cannot be used. */
+
+STATEMENT *simple_statement(MPL *mpl, int spec)
+{ STATEMENT *stmt;
+ stmt = alloc(STATEMENT);
+ stmt->line = mpl->line;
+ stmt->next = NULL;
+ if (is_keyword(mpl, "set"))
+ { if (spec)
+ error(mpl, "set statement not allowed here");
+ stmt->type = A_SET;
+ stmt->u.set = set_statement(mpl);
+ }
+ else if (is_keyword(mpl, "param"))
+ { if (spec)
+ error(mpl, "parameter statement not allowed here");
+ stmt->type = A_PARAMETER;
+ stmt->u.par = parameter_statement(mpl);
+ }
+ else if (is_keyword(mpl, "var"))
+ { if (spec)
+ error(mpl, "variable statement not allowed here");
+ stmt->type = A_VARIABLE;
+ stmt->u.var = variable_statement(mpl);
+ }
+ else if (is_keyword(mpl, "subject") ||
+ is_keyword(mpl, "subj") ||
+ mpl->token == T_SPTP)
+ { if (spec)
+ error(mpl, "constraint statement not allowed here");
+ stmt->type = A_CONSTRAINT;
+ stmt->u.con = constraint_statement(mpl);
+ }
+ else if (is_keyword(mpl, "minimize") ||
+ is_keyword(mpl, "maximize"))
+ { if (spec)
+ error(mpl, "objective statement not allowed here");
+ stmt->type = A_CONSTRAINT;
+ stmt->u.con = objective_statement(mpl);
+ }
+#if 1 /* 11/II-2008 */
+ else if (is_keyword(mpl, "table"))
+ { if (spec)
+ error(mpl, "table statement not allowed here");
+ stmt->type = A_TABLE;
+ stmt->u.tab = table_statement(mpl);
+ }
+#endif
+ else if (is_keyword(mpl, "solve"))
+ { if (spec)
+ error(mpl, "solve statement not allowed here");
+ stmt->type = A_SOLVE;
+ stmt->u.slv = solve_statement(mpl);
+ }
+ else if (is_keyword(mpl, "check"))
+ { stmt->type = A_CHECK;
+ stmt->u.chk = check_statement(mpl);
+ }
+ else if (is_keyword(mpl, "display"))
+ { stmt->type = A_DISPLAY;
+ stmt->u.dpy = display_statement(mpl);
+ }
+ else if (is_keyword(mpl, "printf"))
+ { stmt->type = A_PRINTF;
+ stmt->u.prt = printf_statement(mpl);
+ }
+ else if (is_keyword(mpl, "for"))
+ { stmt->type = A_FOR;
+ stmt->u.fur = for_statement(mpl);
+ }
+ else if (mpl->token == T_NAME)
+ { if (spec)
+ error(mpl, "constraint statement not allowed here");
+ stmt->type = A_CONSTRAINT;
+ stmt->u.con = constraint_statement(mpl);
+ }
+ else if (is_reserved(mpl))
+ error(mpl, "invalid use of reserved keyword %s", mpl->image);
+ else
+ error(mpl, "syntax error in model section");
+ return stmt;
+}
+
+/*----------------------------------------------------------------------
+-- model_section - parse model section.
+--
+-- This routine parses model section using the syntax:
+--
+-- <model section> ::= <empty>
+-- <model section> ::= <model section> <statement>
+--
+-- Parsing model section is terminated by either the keyword 'data', or
+-- the keyword 'end', or the end of file. */
+
+void model_section(MPL *mpl)
+{ STATEMENT *stmt, *last_stmt;
+ xassert(mpl->model == NULL);
+ last_stmt = NULL;
+ while (!(mpl->token == T_EOF || is_keyword(mpl, "data") ||
+ is_keyword(mpl, "end")))
+ { /* parse statement */
+ stmt = simple_statement(mpl, 0);
+ /* and append it to the end of the statement list */
+ if (last_stmt == NULL)
+ mpl->model = stmt;
+ else
+ last_stmt->next = stmt;
+ last_stmt = stmt;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl2.c b/test/monniaux/glpk-4.65/src/mpl/mpl2.c
new file mode 100644
index 00000000..0f99528b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl2.c
@@ -0,0 +1,1202 @@
+/* mpl2.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+
+/**********************************************************************/
+/* * * PROCESSING DATA SECTION * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_slice - create slice.
+--
+-- This routine creates a slice, which initially has no components. */
+
+SLICE *create_slice(MPL *mpl)
+{ SLICE *slice;
+ xassert(mpl == mpl);
+ slice = NULL;
+ return slice;
+}
+
+/*----------------------------------------------------------------------
+-- expand_slice - append new component to slice.
+--
+-- This routine expands slice appending to it either a given symbol or
+-- null component, which becomes the last component of the slice. */
+
+SLICE *expand_slice
+( MPL *mpl,
+ SLICE *slice, /* destroyed */
+ SYMBOL *sym /* destroyed */
+)
+{ SLICE *tail, *temp;
+ /* create a new component */
+ tail = dmp_get_atom(mpl->tuples, sizeof(SLICE));
+ tail->sym = sym;
+ tail->next = NULL;
+ /* and append it to the component list */
+ if (slice == NULL)
+ slice = tail;
+ else
+ { for (temp = slice; temp->next != NULL; temp = temp->next);
+ temp->next = tail;
+ }
+ return slice;
+}
+
+/*----------------------------------------------------------------------
+-- slice_dimen - determine dimension of slice.
+--
+-- This routine returns dimension of slice, which is number of all its
+-- components including null ones. */
+
+int slice_dimen
+( MPL *mpl,
+ SLICE *slice /* not changed */
+)
+{ SLICE *temp;
+ int dim;
+ xassert(mpl == mpl);
+ dim = 0;
+ for (temp = slice; temp != NULL; temp = temp->next) dim++;
+ return dim;
+}
+
+/*----------------------------------------------------------------------
+-- slice_arity - determine arity of slice.
+--
+-- This routine returns arity of slice, i.e. number of null components
+-- (indicated by asterisks) in the slice. */
+
+int slice_arity
+( MPL *mpl,
+ SLICE *slice /* not changed */
+)
+{ SLICE *temp;
+ int arity;
+ xassert(mpl == mpl);
+ arity = 0;
+ for (temp = slice; temp != NULL; temp = temp->next)
+ if (temp->sym == NULL) arity++;
+ return arity;
+}
+
+/*----------------------------------------------------------------------
+-- fake_slice - create fake slice of all asterisks.
+--
+-- This routine creates a fake slice of given dimension, which contains
+-- asterisks in all components. Zero dimension is allowed. */
+
+SLICE *fake_slice(MPL *mpl, int dim)
+{ SLICE *slice;
+ slice = create_slice(mpl);
+ while (dim-- > 0) slice = expand_slice(mpl, slice, NULL);
+ return slice;
+}
+
+/*----------------------------------------------------------------------
+-- delete_slice - delete slice.
+--
+-- This routine deletes specified slice. */
+
+void delete_slice
+( MPL *mpl,
+ SLICE *slice /* destroyed */
+)
+{ SLICE *temp;
+ while (slice != NULL)
+ { temp = slice;
+ slice = temp->next;
+ if (temp->sym != NULL) delete_symbol(mpl, temp->sym);
+xassert(sizeof(SLICE) == sizeof(TUPLE));
+ dmp_free_atom(mpl->tuples, temp, sizeof(TUPLE));
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- is_number - check if current token is number.
+--
+-- If the current token is a number, this routine returns non-zero.
+-- Otherwise zero is returned. */
+
+int is_number(MPL *mpl)
+{ return
+ mpl->token == T_NUMBER;
+}
+
+/*----------------------------------------------------------------------
+-- is_symbol - check if current token is symbol.
+--
+-- If the current token is suitable to be a symbol, the routine returns
+-- non-zero. Otherwise zero is returned. */
+
+int is_symbol(MPL *mpl)
+{ return
+ mpl->token == T_NUMBER ||
+ mpl->token == T_SYMBOL ||
+ mpl->token == T_STRING;
+}
+
+/*----------------------------------------------------------------------
+-- is_literal - check if current token is given symbolic literal.
+--
+-- If the current token is given symbolic literal, this routine returns
+-- non-zero. Otherwise zero is returned.
+--
+-- This routine is used on processing the data section in the same way
+-- as the routine is_keyword on processing the model section. */
+
+int is_literal(MPL *mpl, char *literal)
+{ return
+ is_symbol(mpl) && strcmp(mpl->image, literal) == 0;
+}
+
+/*----------------------------------------------------------------------
+-- read_number - read number.
+--
+-- This routine reads the current token, which must be a number, and
+-- returns its numeric value. */
+
+double read_number(MPL *mpl)
+{ double num;
+ xassert(is_number(mpl));
+ num = mpl->value;
+ get_token(mpl /* <number> */);
+ return num;
+}
+
+/*----------------------------------------------------------------------
+-- read_symbol - read symbol.
+--
+-- This routine reads the current token, which must be a symbol, and
+-- returns its symbolic value. */
+
+SYMBOL *read_symbol(MPL *mpl)
+{ SYMBOL *sym;
+ xassert(is_symbol(mpl));
+ if (is_number(mpl))
+ sym = create_symbol_num(mpl, mpl->value);
+ else
+ sym = create_symbol_str(mpl, create_string(mpl, mpl->image));
+ get_token(mpl /* <symbol> */);
+ return sym;
+}
+
+/*----------------------------------------------------------------------
+-- read_slice - read slice.
+--
+-- This routine reads slice using the syntax:
+--
+-- <slice> ::= [ <symbol list> ]
+-- <slice> ::= ( <symbol list> )
+-- <symbol list> ::= <symbol or star>
+-- <symbol list> ::= <symbol list> , <symbol or star>
+-- <symbol or star> ::= <symbol>
+-- <symbol or star> ::= *
+--
+-- The bracketed form of slice is used for members of multi-dimensional
+-- objects while the parenthesized form is used for elemental sets. */
+
+SLICE *read_slice
+( MPL *mpl,
+ char *name, /* not changed */
+ int dim
+)
+{ SLICE *slice;
+ int close;
+ xassert(name != NULL);
+ switch (mpl->token)
+ { case T_LBRACKET:
+ close = T_RBRACKET;
+ break;
+ case T_LEFT:
+ xassert(dim > 0);
+ close = T_RIGHT;
+ break;
+ default:
+ xassert(mpl != mpl);
+ }
+ if (dim == 0)
+ error(mpl, "%s cannot be subscripted", name);
+ get_token(mpl /* ( | [ */);
+ /* read slice components */
+ slice = create_slice(mpl);
+ for (;;)
+ { /* the current token must be a symbol or asterisk */
+ if (is_symbol(mpl))
+ slice = expand_slice(mpl, slice, read_symbol(mpl));
+ else if (mpl->token == T_ASTERISK)
+ { slice = expand_slice(mpl, slice, NULL);
+ get_token(mpl /* * */);
+ }
+ else
+ error(mpl, "number, symbol, or asterisk missing where expec"
+ "ted");
+ /* check a token that follows the symbol */
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == close)
+ break;
+ else
+ error(mpl, "syntax error in slice");
+ }
+ /* number of slice components must be the same as the appropriate
+ dimension */
+ if (slice_dimen(mpl, slice) != dim)
+ { switch (close)
+ { case T_RBRACKET:
+ error(mpl, "%s must have %d subscript%s, not %d", name,
+ dim, dim == 1 ? "" : "s", slice_dimen(mpl, slice));
+ break;
+ case T_RIGHT:
+ error(mpl, "%s has dimension %d, not %d", name, dim,
+ slice_dimen(mpl, slice));
+ break;
+ default:
+ xassert(close != close);
+ }
+ }
+ get_token(mpl /* ) | ] */);
+ return slice;
+}
+
+/*----------------------------------------------------------------------
+-- select_set - select set to saturate it with elemental sets.
+--
+-- This routine selects set to saturate it with elemental sets provided
+-- in the data section. */
+
+SET *select_set
+( MPL *mpl,
+ char *name /* not changed */
+)
+{ SET *set;
+ AVLNODE *node;
+ xassert(name != NULL);
+ node = avl_find_node(mpl->tree, name);
+ if (node == NULL || avl_get_node_type(node) != A_SET)
+ error(mpl, "%s not a set", name);
+ set = (SET *)avl_get_node_link(node);
+ if (set->assign != NULL || set->gadget != NULL)
+ error(mpl, "%s needs no data", name);
+ set->data = 1;
+ return set;
+}
+
+/*----------------------------------------------------------------------
+-- simple_format - read set data block in simple format.
+--
+-- This routine reads set data block using the syntax:
+--
+-- <simple format> ::= <symbol> , <symbol> , ... , <symbol>
+--
+-- where <symbols> are used to construct a complete n-tuple, which is
+-- included in elemental set assigned to the set member. Commae between
+-- symbols are optional and may be omitted anywhere.
+--
+-- Number of components in the slice must be the same as dimension of
+-- n-tuples in elemental sets assigned to the set members. To construct
+-- complete n-tuple the routine replaces null positions in the slice by
+-- corresponding <symbols>.
+--
+-- If the slice contains at least one null position, the current token
+-- must be symbol. Otherwise, the routine reads no symbols to construct
+-- the n-tuple, so the current token is not checked. */
+
+void simple_format
+( MPL *mpl,
+ SET *set, /* not changed */
+ MEMBER *memb, /* modified */
+ SLICE *slice /* not changed */
+)
+{ TUPLE *tuple;
+ SLICE *temp;
+ SYMBOL *sym, *with = NULL;
+ xassert(set != NULL);
+ xassert(memb != NULL);
+ xassert(slice != NULL);
+ xassert(set->dimen == slice_dimen(mpl, slice));
+ xassert(memb->value.set->dim == set->dimen);
+ if (slice_arity(mpl, slice) > 0) xassert(is_symbol(mpl));
+ /* read symbols and construct complete n-tuple */
+ tuple = create_tuple(mpl);
+ for (temp = slice; temp != NULL; temp = temp->next)
+ { if (temp->sym == NULL)
+ { /* substitution is needed; read symbol */
+ if (!is_symbol(mpl))
+ { int lack = slice_arity(mpl, temp);
+ /* with cannot be null due to assertion above */
+ xassert(with != NULL);
+ if (lack == 1)
+ error(mpl, "one item missing in data group beginning "
+ "with %s", format_symbol(mpl, with));
+ else
+ error(mpl, "%d items missing in data group beginning "
+ "with %s", lack, format_symbol(mpl, with));
+ }
+ sym = read_symbol(mpl);
+ if (with == NULL) with = sym;
+ }
+ else
+ { /* copy symbol from the slice */
+ sym = copy_symbol(mpl, temp->sym);
+ }
+ /* append the symbol to the n-tuple */
+ tuple = expand_tuple(mpl, tuple, sym);
+ /* skip optional comma *between* <symbols> */
+ if (temp->next != NULL && mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ }
+ /* add constructed n-tuple to elemental set */
+ check_then_add(mpl, memb->value.set, tuple);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- matrix_format - read set data block in matrix format.
+--
+-- This routine reads set data block using the syntax:
+--
+-- <matrix format> ::= <column> <column> ... <column> :=
+-- <row> +/- +/- ... +/-
+-- <row> +/- +/- ... +/-
+-- . . . . . . . . . . .
+-- <row> +/- +/- ... +/-
+--
+-- where <rows> are symbols that denote rows of the matrix, <columns>
+-- are symbols that denote columns of the matrix, "+" and "-" indicate
+-- whether corresponding n-tuple needs to be included in the elemental
+-- set or not, respectively.
+--
+-- Number of the slice components must be the same as dimension of the
+-- elemental set. The slice must have two null positions. To construct
+-- complete n-tuple for particular element of the matrix the routine
+-- replaces first null position of the slice by the corresponding <row>
+-- (or <column>, if the flag tr is on) and second null position by the
+-- corresponding <column> (or by <row>, if the flag tr is on). */
+
+void matrix_format
+( MPL *mpl,
+ SET *set, /* not changed */
+ MEMBER *memb, /* modified */
+ SLICE *slice, /* not changed */
+ int tr
+)
+{ SLICE *list, *col, *temp;
+ TUPLE *tuple;
+ SYMBOL *row;
+ xassert(set != NULL);
+ xassert(memb != NULL);
+ xassert(slice != NULL);
+ xassert(set->dimen == slice_dimen(mpl, slice));
+ xassert(memb->value.set->dim == set->dimen);
+ xassert(slice_arity(mpl, slice) == 2);
+ /* read the matrix heading that contains column symbols (there
+ may be no columns at all) */
+ list = create_slice(mpl);
+ while (mpl->token != T_ASSIGN)
+ { /* read column symbol and append it to the column list */
+ if (!is_symbol(mpl))
+ error(mpl, "number, symbol, or := missing where expected");
+ list = expand_slice(mpl, list, read_symbol(mpl));
+ }
+ get_token(mpl /* := */);
+ /* read zero or more rows that contain matrix data */
+ while (is_symbol(mpl))
+ { /* read row symbol (if the matrix has no columns, row symbols
+ are just ignored) */
+ row = read_symbol(mpl);
+ /* read the matrix row accordingly to the column list */
+ for (col = list; col != NULL; col = col->next)
+ { int which = 0;
+ /* check indicator */
+ if (is_literal(mpl, "+"))
+ ;
+ else if (is_literal(mpl, "-"))
+ { get_token(mpl /* - */);
+ continue;
+ }
+ else
+ { int lack = slice_dimen(mpl, col);
+ if (lack == 1)
+ error(mpl, "one item missing in data group beginning "
+ "with %s", format_symbol(mpl, row));
+ else
+ error(mpl, "%d items missing in data group beginning "
+ "with %s", lack, format_symbol(mpl, row));
+ }
+ /* construct complete n-tuple */
+ tuple = create_tuple(mpl);
+ for (temp = slice; temp != NULL; temp = temp->next)
+ { if (temp->sym == NULL)
+ { /* substitution is needed */
+ switch (++which)
+ { case 1:
+ /* substitute in the first null position */
+ tuple = expand_tuple(mpl, tuple,
+ copy_symbol(mpl, tr ? col->sym : row));
+ break;
+ case 2:
+ /* substitute in the second null position */
+ tuple = expand_tuple(mpl, tuple,
+ copy_symbol(mpl, tr ? row : col->sym));
+ break;
+ default:
+ xassert(which != which);
+ }
+ }
+ else
+ { /* copy symbol from the slice */
+ tuple = expand_tuple(mpl, tuple, copy_symbol(mpl,
+ temp->sym));
+ }
+ }
+ xassert(which == 2);
+ /* add constructed n-tuple to elemental set */
+ check_then_add(mpl, memb->value.set, tuple);
+ get_token(mpl /* + */);
+ }
+ /* delete the row symbol */
+ delete_symbol(mpl, row);
+ }
+ /* delete the column list */
+ delete_slice(mpl, list);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- set_data - read set data.
+--
+-- This routine reads set data using the syntax:
+--
+-- <set data> ::= set <set name> <assignments> ;
+-- <set data> ::= set <set name> [ <symbol list> ] <assignments> ;
+-- <set name> ::= <symbolic name>
+-- <assignments> ::= <empty>
+-- <assignments> ::= <assignments> , :=
+-- <assignments> ::= <assignments> , ( <symbol list> )
+-- <assignments> ::= <assignments> , <simple format>
+-- <assignments> ::= <assignments> , : <matrix format>
+-- <assignments> ::= <assignments> , (tr) <matrix format>
+-- <assignments> ::= <assignments> , (tr) : <matrix format>
+--
+-- Commae in <assignments> are optional and may be omitted anywhere. */
+
+void set_data(MPL *mpl)
+{ SET *set;
+ TUPLE *tuple;
+ MEMBER *memb;
+ SLICE *slice;
+ int tr = 0;
+ xassert(is_literal(mpl, "set"));
+ get_token(mpl /* set */);
+ /* symbolic name of set must follows the keyword 'set' */
+ if (!is_symbol(mpl))
+ error(mpl, "set name missing where expected");
+ /* select the set to saturate it with data */
+ set = select_set(mpl, mpl->image);
+ get_token(mpl /* <symbolic name> */);
+ /* read optional subscript list, which identifies member of the
+ set to be read */
+ tuple = create_tuple(mpl);
+ if (mpl->token == T_LBRACKET)
+ { /* subscript list is specified */
+ if (set->dim == 0)
+ error(mpl, "%s cannot be subscripted", set->name);
+ get_token(mpl /* [ */);
+ /* read symbols and construct subscript list */
+ for (;;)
+ { if (!is_symbol(mpl))
+ error(mpl, "number or symbol missing where expected");
+ tuple = expand_tuple(mpl, tuple, read_symbol(mpl));
+ if (mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ else if (mpl->token == T_RBRACKET)
+ break;
+ else
+ error(mpl, "syntax error in subscript list");
+ }
+ if (set->dim != tuple_dimen(mpl, tuple))
+ error(mpl, "%s must have %d subscript%s rather than %d",
+ set->name, set->dim, set->dim == 1 ? "" : "s",
+ tuple_dimen(mpl, tuple));
+ get_token(mpl /* ] */);
+ }
+ else
+ { /* subscript list is not specified */
+ if (set->dim != 0)
+ error(mpl, "%s must be subscripted", set->name);
+ }
+ /* there must be no member with the same subscript list */
+ if (find_member(mpl, set->array, tuple) != NULL)
+ error(mpl, "%s%s already defined",
+ set->name, format_tuple(mpl, '[', tuple));
+ /* add new member to the set and assign it empty elemental set */
+ memb = add_member(mpl, set->array, tuple);
+ memb->value.set = create_elemset(mpl, set->dimen);
+ /* create an initial fake slice of all asterisks */
+ slice = fake_slice(mpl, set->dimen);
+ /* read zero or more data assignments */
+ for (;;)
+ { /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ /* process assignment element */
+ if (mpl->token == T_ASSIGN)
+ { /* assignment ligature is non-significant element */
+ get_token(mpl /* := */);
+ }
+ else if (mpl->token == T_LEFT)
+ { /* left parenthesis begins either new slice or "transpose"
+ indicator */
+ int is_tr;
+ get_token(mpl /* ( */);
+ is_tr = is_literal(mpl, "tr");
+ unget_token(mpl /* ( */);
+ if (is_tr) goto left;
+ /* delete the current slice and read new one */
+ delete_slice(mpl, slice);
+ slice = read_slice(mpl, set->name, set->dimen);
+ /* each new slice resets the "transpose" indicator */
+ tr = 0;
+ /* if the new slice is 0-ary, formally there is one 0-tuple
+ (in the simple format) that follows it */
+ if (slice_arity(mpl, slice) == 0)
+ simple_format(mpl, set, memb, slice);
+ }
+ else if (is_symbol(mpl))
+ { /* number or symbol begins data in the simple format */
+ simple_format(mpl, set, memb, slice);
+ }
+ else if (mpl->token == T_COLON)
+ { /* colon begins data in the matrix format */
+ if (slice_arity(mpl, slice) != 2)
+err1: error(mpl, "slice currently used must specify 2 asterisk"
+ "s, not %d", slice_arity(mpl, slice));
+ get_token(mpl /* : */);
+ /* read elemental set data in the matrix format */
+ matrix_format(mpl, set, memb, slice, tr);
+ }
+ else if (mpl->token == T_LEFT)
+left: { /* left parenthesis begins the "transpose" indicator, which
+ is followed by data in the matrix format */
+ get_token(mpl /* ( */);
+ if (!is_literal(mpl, "tr"))
+err2: error(mpl, "transpose indicator (tr) incomplete");
+ if (slice_arity(mpl, slice) != 2) goto err1;
+ get_token(mpl /* tr */);
+ if (mpl->token != T_RIGHT) goto err2;
+ get_token(mpl /* ) */);
+ /* in this case the colon is optional */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* set the "transpose" indicator */
+ tr = 1;
+ /* read elemental set data in the matrix format */
+ matrix_format(mpl, set, memb, slice, tr);
+ }
+ else if (mpl->token == T_SEMICOLON)
+ { /* semicolon terminates the data block */
+ get_token(mpl /* ; */);
+ break;
+ }
+ else
+ error(mpl, "syntax error in set data block");
+ }
+ /* delete the current slice */
+ delete_slice(mpl, slice);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- select_parameter - select parameter to saturate it with data.
+--
+-- This routine selects parameter to saturate it with data provided in
+-- the data section. */
+
+PARAMETER *select_parameter
+( MPL *mpl,
+ char *name /* not changed */
+)
+{ PARAMETER *par;
+ AVLNODE *node;
+ xassert(name != NULL);
+ node = avl_find_node(mpl->tree, name);
+ if (node == NULL || avl_get_node_type(node) != A_PARAMETER)
+ error(mpl, "%s not a parameter", name);
+ par = (PARAMETER *)avl_get_node_link(node);
+ if (par->assign != NULL)
+ error(mpl, "%s needs no data", name);
+ if (par->data)
+ error(mpl, "%s already provided with data", name);
+ par->data = 1;
+ return par;
+}
+
+/*----------------------------------------------------------------------
+-- set_default - set default parameter value.
+--
+-- This routine sets default value for specified parameter. */
+
+void set_default
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SYMBOL *altval /* destroyed */
+)
+{ xassert(par != NULL);
+ xassert(altval != NULL);
+ if (par->option != NULL)
+ error(mpl, "default value for %s already specified in model se"
+ "ction", par->name);
+ xassert(par->defval == NULL);
+ par->defval = altval;
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- read_value - read value and assign it to parameter member.
+--
+-- This routine reads numeric or symbolic value from the input stream
+-- and assigns to new parameter member specified by its n-tuple, which
+-- (the member) is created and added to the parameter array. */
+
+MEMBER *read_value
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* destroyed */
+)
+{ MEMBER *memb;
+ xassert(par != NULL);
+ xassert(is_symbol(mpl));
+ /* there must be no member with the same n-tuple */
+ if (find_member(mpl, par->array, tuple) != NULL)
+ error(mpl, "%s%s already defined",
+ par->name, format_tuple(mpl, '[', tuple));
+ /* create new parameter member with given n-tuple */
+ memb = add_member(mpl, par->array, tuple);
+ /* read value and assigns it to the new parameter member */
+ switch (par->type)
+ { case A_NUMERIC:
+ case A_INTEGER:
+ case A_BINARY:
+ if (!is_number(mpl))
+ error(mpl, "%s requires numeric data", par->name);
+ memb->value.num = read_number(mpl);
+ break;
+ case A_SYMBOLIC:
+ memb->value.sym = read_symbol(mpl);
+ break;
+ default:
+ xassert(par != par);
+ }
+ return memb;
+}
+
+/*----------------------------------------------------------------------
+-- plain_format - read parameter data block in plain format.
+--
+-- This routine reads parameter data block using the syntax:
+--
+-- <plain format> ::= <symbol> , <symbol> , ... , <symbol> , <value>
+--
+-- where <symbols> are used to determine a complete subscript list for
+-- parameter member, <value> is a numeric or symbolic value assigned to
+-- the parameter member. Commae between data items are optional and may
+-- be omitted anywhere.
+--
+-- Number of components in the slice must be the same as dimension of
+-- the parameter. To construct the complete subscript list the routine
+-- replaces null positions in the slice by corresponding <symbols>. */
+
+void plain_format
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SLICE *slice /* not changed */
+)
+{ TUPLE *tuple;
+ SLICE *temp;
+ SYMBOL *sym, *with = NULL;
+ xassert(par != NULL);
+ xassert(par->dim == slice_dimen(mpl, slice));
+ xassert(is_symbol(mpl));
+ /* read symbols and construct complete subscript list */
+ tuple = create_tuple(mpl);
+ for (temp = slice; temp != NULL; temp = temp->next)
+ { if (temp->sym == NULL)
+ { /* substitution is needed; read symbol */
+ if (!is_symbol(mpl))
+ { int lack = slice_arity(mpl, temp) + 1;
+ xassert(with != NULL);
+ xassert(lack > 1);
+ error(mpl, "%d items missing in data group beginning wit"
+ "h %s", lack, format_symbol(mpl, with));
+ }
+ sym = read_symbol(mpl);
+ if (with == NULL) with = sym;
+ }
+ else
+ { /* copy symbol from the slice */
+ sym = copy_symbol(mpl, temp->sym);
+ }
+ /* append the symbol to the subscript list */
+ tuple = expand_tuple(mpl, tuple, sym);
+ /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ }
+ /* read value and assign it to new parameter member */
+ if (!is_symbol(mpl))
+ { xassert(with != NULL);
+ error(mpl, "one item missing in data group beginning with %s",
+ format_symbol(mpl, with));
+ }
+ read_value(mpl, par, tuple);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- tabular_format - read parameter data block in tabular format.
+--
+-- This routine reads parameter data block using the syntax:
+--
+-- <tabular format> ::= <column> <column> ... <column> :=
+-- <row> <value> <value> ... <value>
+-- <row> <value> <value> ... <value>
+-- . . . . . . . . . . .
+-- <row> <value> <value> ... <value>
+--
+-- where <rows> are symbols that denote rows of the table, <columns>
+-- are symbols that denote columns of the table, <values> are numeric
+-- or symbolic values assigned to the corresponding parameter members.
+-- If <value> is specified as single point, no value is provided.
+--
+-- Number of components in the slice must be the same as dimension of
+-- the parameter. The slice must have two null positions. To construct
+-- complete subscript list for particular <value> the routine replaces
+-- the first null position of the slice by the corresponding <row> (or
+-- <column>, if the flag tr is on) and the second null position by the
+-- corresponding <column> (or by <row>, if the flag tr is on). */
+
+void tabular_format
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ SLICE *slice, /* not changed */
+ int tr
+)
+{ SLICE *list, *col, *temp;
+ TUPLE *tuple;
+ SYMBOL *row;
+ xassert(par != NULL);
+ xassert(par->dim == slice_dimen(mpl, slice));
+ xassert(slice_arity(mpl, slice) == 2);
+ /* read the table heading that contains column symbols (the table
+ may have no columns) */
+ list = create_slice(mpl);
+ while (mpl->token != T_ASSIGN)
+ { /* read column symbol and append it to the column list */
+ if (!is_symbol(mpl))
+ error(mpl, "number, symbol, or := missing where expected");
+ list = expand_slice(mpl, list, read_symbol(mpl));
+ }
+ get_token(mpl /* := */);
+ /* read zero or more rows that contain tabular data */
+ while (is_symbol(mpl))
+ { /* read row symbol (if the table has no columns, these symbols
+ are just ignored) */
+ row = read_symbol(mpl);
+ /* read values accordingly to the column list */
+ for (col = list; col != NULL; col = col->next)
+ { int which = 0;
+ /* if the token is single point, no value is provided */
+ if (is_literal(mpl, "."))
+ { get_token(mpl /* . */);
+ continue;
+ }
+ /* construct complete subscript list */
+ tuple = create_tuple(mpl);
+ for (temp = slice; temp != NULL; temp = temp->next)
+ { if (temp->sym == NULL)
+ { /* substitution is needed */
+ switch (++which)
+ { case 1:
+ /* substitute in the first null position */
+ tuple = expand_tuple(mpl, tuple,
+ copy_symbol(mpl, tr ? col->sym : row));
+ break;
+ case 2:
+ /* substitute in the second null position */
+ tuple = expand_tuple(mpl, tuple,
+ copy_symbol(mpl, tr ? row : col->sym));
+ break;
+ default:
+ xassert(which != which);
+ }
+ }
+ else
+ { /* copy symbol from the slice */
+ tuple = expand_tuple(mpl, tuple, copy_symbol(mpl,
+ temp->sym));
+ }
+ }
+ xassert(which == 2);
+ /* read value and assign it to new parameter member */
+ if (!is_symbol(mpl))
+ { int lack = slice_dimen(mpl, col);
+ if (lack == 1)
+ error(mpl, "one item missing in data group beginning "
+ "with %s", format_symbol(mpl, row));
+ else
+ error(mpl, "%d items missing in data group beginning "
+ "with %s", lack, format_symbol(mpl, row));
+ }
+ read_value(mpl, par, tuple);
+ }
+ /* delete the row symbol */
+ delete_symbol(mpl, row);
+ }
+ /* delete the column list */
+ delete_slice(mpl, list);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- tabbing_format - read parameter data block in tabbing format.
+--
+-- This routine reads parameter data block using the syntax:
+--
+-- <tabbing format> ::= <prefix> <name> , ... , <name> , := ,
+-- <symbol> , ... , <symbol> , <value> , ... , <value> ,
+-- <symbol> , ... , <symbol> , <value> , ... , <value> ,
+-- . . . . . . . . . . . . . . . . .
+-- <symbol> , ... , <symbol> , <value> , ... , <value>
+-- <prefix> ::= <empty>
+-- <prefix> ::= <set name> :
+--
+-- where <names> are names of parameters (all the parameters must be
+-- subscripted and have identical dimensions), <symbols> are symbols
+-- used to define subscripts of parameter members, <values> are numeric
+-- or symbolic values assigned to the corresponding parameter members.
+-- Optional <prefix> may specify a simple set, in which case n-tuples
+-- built of <symbols> for each row of the data table (i.e. subscripts
+-- of parameter members) are added to the specified set. Commae between
+-- data items are optional and may be omitted anywhere.
+--
+-- If the parameter altval is not NULL, it specifies a default value
+-- provided for all the parameters specified in the data block. */
+
+void tabbing_format
+( MPL *mpl,
+ SYMBOL *altval /* not changed */
+)
+{ SET *set = NULL;
+ PARAMETER *par;
+ SLICE *list, *col;
+ TUPLE *tuple;
+ int next_token, j, dim = 0;
+ char *last_name = NULL;
+ /* read the optional <prefix> */
+ if (is_symbol(mpl))
+ { get_token(mpl /* <symbol> */);
+ next_token = mpl->token;
+ unget_token(mpl /* <symbol> */);
+ if (next_token == T_COLON)
+ { /* select the set to saturate it with data */
+ set = select_set(mpl, mpl->image);
+ /* the set must be simple (i.e. not set of sets) */
+ if (set->dim != 0)
+ error(mpl, "%s must be a simple set", set->name);
+ /* and must not be defined yet */
+ if (set->array->head != NULL)
+ error(mpl, "%s already defined", set->name);
+ /* add new (the only) member to the set and assign it empty
+ elemental set */
+ add_member(mpl, set->array, NULL)->value.set =
+ create_elemset(mpl, set->dimen);
+ last_name = set->name, dim = set->dimen;
+ get_token(mpl /* <symbol> */);
+ xassert(mpl->token == T_COLON);
+ get_token(mpl /* : */);
+ }
+ }
+ /* read the table heading that contains parameter names */
+ list = create_slice(mpl);
+ while (mpl->token != T_ASSIGN)
+ { /* there must be symbolic name of parameter */
+ if (!is_symbol(mpl))
+ error(mpl, "parameter name or := missing where expected");
+ /* select the parameter to saturate it with data */
+ par = select_parameter(mpl, mpl->image);
+ /* the parameter must be subscripted */
+ if (par->dim == 0)
+ error(mpl, "%s not a subscripted parameter", mpl->image);
+ /* the set (if specified) and all the parameters in the data
+ block must have identical dimension */
+ if (dim != 0 && par->dim != dim)
+ { xassert(last_name != NULL);
+ error(mpl, "%s has dimension %d while %s has dimension %d",
+ last_name, dim, par->name, par->dim);
+ }
+ /* set default value for the parameter (if specified) */
+ if (altval != NULL)
+ set_default(mpl, par, copy_symbol(mpl, altval));
+ /* append the parameter to the column list */
+ list = expand_slice(mpl, list, (SYMBOL *)par);
+ last_name = par->name, dim = par->dim;
+ get_token(mpl /* <symbol> */);
+ /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ }
+ if (slice_dimen(mpl, list) == 0)
+ error(mpl, "at least one parameter name required");
+ get_token(mpl /* := */);
+ /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ /* read rows that contain tabbing data */
+ while (is_symbol(mpl))
+ { /* read subscript list */
+ tuple = create_tuple(mpl);
+ for (j = 1; j <= dim; j++)
+ { /* read j-th subscript */
+ if (!is_symbol(mpl))
+ { int lack = slice_dimen(mpl, list) + dim - j + 1;
+ xassert(tuple != NULL);
+ xassert(lack > 1);
+ error(mpl, "%d items missing in data group beginning wit"
+ "h %s", lack, format_symbol(mpl, tuple->sym));
+ }
+ /* read and append j-th subscript to the n-tuple */
+ tuple = expand_tuple(mpl, tuple, read_symbol(mpl));
+ /* skip optional comma *between* <symbols> */
+ if (j < dim && mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ }
+ /* if the set is specified, add to it new n-tuple, which is a
+ copy of the subscript list just read */
+ if (set != NULL)
+ check_then_add(mpl, set->array->head->value.set,
+ copy_tuple(mpl, tuple));
+ /* skip optional comma between <symbol> and <value> */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ /* read values accordingly to the column list */
+ for (col = list; col != NULL; col = col->next)
+ { /* if the token is single point, no value is provided */
+ if (is_literal(mpl, "."))
+ { get_token(mpl /* . */);
+ continue;
+ }
+ /* read value and assign it to new parameter member */
+ if (!is_symbol(mpl))
+ { int lack = slice_dimen(mpl, col);
+ xassert(tuple != NULL);
+ if (lack == 1)
+ error(mpl, "one item missing in data group beginning "
+ "with %s", format_symbol(mpl, tuple->sym));
+ else
+ error(mpl, "%d items missing in data group beginning "
+ "with %s", lack, format_symbol(mpl, tuple->sym));
+ }
+ read_value(mpl, (PARAMETER *)col->sym, copy_tuple(mpl,
+ tuple));
+ /* skip optional comma preceding the next value */
+ if (col->next != NULL && mpl->token == T_COMMA)
+ get_token(mpl /* , */);
+ }
+ /* delete the original subscript list */
+ delete_tuple(mpl, tuple);
+ /* skip optional comma (only if there is next data group) */
+ if (mpl->token == T_COMMA)
+ { get_token(mpl /* , */);
+ if (!is_symbol(mpl)) unget_token(mpl /* , */);
+ }
+ }
+ /* delete the column list (it contains parameters, not symbols,
+ so nullify it before) */
+ for (col = list; col != NULL; col = col->next) col->sym = NULL;
+ delete_slice(mpl, list);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- parameter_data - read parameter data.
+--
+-- This routine reads parameter data using the syntax:
+--
+-- <parameter data> ::= param <default value> : <tabbing format> ;
+-- <parameter data> ::= param <parameter name> <default value>
+-- <assignments> ;
+-- <parameter name> ::= <symbolic name>
+-- <default value> ::= <empty>
+-- <default value> ::= default <symbol>
+-- <assignments> ::= <empty>
+-- <assignments> ::= <assignments> , :=
+-- <assignments> ::= <assignments> , [ <symbol list> ]
+-- <assignments> ::= <assignments> , <plain format>
+-- <assignemnts> ::= <assignments> , : <tabular format>
+-- <assignments> ::= <assignments> , (tr) <tabular format>
+-- <assignments> ::= <assignments> , (tr) : <tabular format>
+--
+-- Commae in <assignments> are optional and may be omitted anywhere. */
+
+void parameter_data(MPL *mpl)
+{ PARAMETER *par;
+ SYMBOL *altval = NULL;
+ SLICE *slice;
+ int tr = 0;
+ xassert(is_literal(mpl, "param"));
+ get_token(mpl /* param */);
+ /* read optional default value */
+ if (is_literal(mpl, "default"))
+ { get_token(mpl /* default */);
+ if (!is_symbol(mpl))
+ error(mpl, "default value missing where expected");
+ altval = read_symbol(mpl);
+ /* if the default value follows the keyword 'param', the next
+ token must be only the colon */
+ if (mpl->token != T_COLON)
+ error(mpl, "colon missing where expected");
+ }
+ /* being used after the keyword 'param' or the optional default
+ value the colon begins data in the tabbing format */
+ if (mpl->token == T_COLON)
+ { get_token(mpl /* : */);
+ /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ /* read parameter data in the tabbing format */
+ tabbing_format(mpl, altval);
+ /* on reading data in the tabbing format the default value is
+ always copied, so delete the original symbol */
+ if (altval != NULL) delete_symbol(mpl, altval);
+ /* the next token must be only semicolon */
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "symbol, number, or semicolon missing where expe"
+ "cted");
+ get_token(mpl /* ; */);
+ goto done;
+ }
+ /* in other cases there must be symbolic name of parameter, which
+ follows the keyword 'param' */
+ if (!is_symbol(mpl))
+ error(mpl, "parameter name missing where expected");
+ /* select the parameter to saturate it with data */
+ par = select_parameter(mpl, mpl->image);
+ get_token(mpl /* <symbol> */);
+ /* read optional default value */
+ if (is_literal(mpl, "default"))
+ { get_token(mpl /* default */);
+ if (!is_symbol(mpl))
+ error(mpl, "default value missing where expected");
+ altval = read_symbol(mpl);
+ /* set default value for the parameter */
+ set_default(mpl, par, altval);
+ }
+ /* create initial fake slice of all asterisks */
+ slice = fake_slice(mpl, par->dim);
+ /* read zero or more data assignments */
+ for (;;)
+ { /* skip optional comma */
+ if (mpl->token == T_COMMA) get_token(mpl /* , */);
+ /* process current assignment */
+ if (mpl->token == T_ASSIGN)
+ { /* assignment ligature is non-significant element */
+ get_token(mpl /* := */);
+ }
+ else if (mpl->token == T_LBRACKET)
+ { /* left bracket begins new slice; delete the current slice
+ and read new one */
+ delete_slice(mpl, slice);
+ slice = read_slice(mpl, par->name, par->dim);
+ /* each new slice resets the "transpose" indicator */
+ tr = 0;
+ }
+ else if (is_symbol(mpl))
+ { /* number or symbol begins data in the plain format */
+ plain_format(mpl, par, slice);
+ }
+ else if (mpl->token == T_COLON)
+ { /* colon begins data in the tabular format */
+ if (par->dim == 0)
+err1: error(mpl, "%s not a subscripted parameter",
+ par->name);
+ if (slice_arity(mpl, slice) != 2)
+err2: error(mpl, "slice currently used must specify 2 asterisk"
+ "s, not %d", slice_arity(mpl, slice));
+ get_token(mpl /* : */);
+ /* read parameter data in the tabular format */
+ tabular_format(mpl, par, slice, tr);
+ }
+ else if (mpl->token == T_LEFT)
+ { /* left parenthesis begins the "transpose" indicator, which
+ is followed by data in the tabular format */
+ get_token(mpl /* ( */);
+ if (!is_literal(mpl, "tr"))
+err3: error(mpl, "transpose indicator (tr) incomplete");
+ if (par->dim == 0) goto err1;
+ if (slice_arity(mpl, slice) != 2) goto err2;
+ get_token(mpl /* tr */);
+ if (mpl->token != T_RIGHT) goto err3;
+ get_token(mpl /* ) */);
+ /* in this case the colon is optional */
+ if (mpl->token == T_COLON) get_token(mpl /* : */);
+ /* set the "transpose" indicator */
+ tr = 1;
+ /* read parameter data in the tabular format */
+ tabular_format(mpl, par, slice, tr);
+ }
+ else if (mpl->token == T_SEMICOLON)
+ { /* semicolon terminates the data block */
+ get_token(mpl /* ; */);
+ break;
+ }
+ else
+ error(mpl, "syntax error in parameter data block");
+ }
+ /* delete the current slice */
+ delete_slice(mpl, slice);
+done: return;
+}
+
+/*----------------------------------------------------------------------
+-- data_section - read data section.
+--
+-- This routine reads data section using the syntax:
+--
+-- <data section> ::= <empty>
+-- <data section> ::= <data section> <data block> ;
+-- <data block> ::= <set data>
+-- <data block> ::= <parameter data>
+--
+-- Reading data section is terminated by either the keyword 'end' or
+-- the end of file. */
+
+void data_section(MPL *mpl)
+{ while (!(mpl->token == T_EOF || is_literal(mpl, "end")))
+ { if (is_literal(mpl, "set"))
+ set_data(mpl);
+ else if (is_literal(mpl, "param"))
+ parameter_data(mpl);
+ else
+ error(mpl, "syntax error in data section");
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl3.c b/test/monniaux/glpk-4.65/src/mpl/mpl3.c
new file mode 100644
index 00000000..2489db27
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl3.c
@@ -0,0 +1,6100 @@
+/* mpl3.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+
+/**********************************************************************/
+/* * * FLOATING-POINT NUMBERS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- fp_add - floating-point addition.
+--
+-- This routine computes the sum x + y. */
+
+double fp_add(MPL *mpl, double x, double y)
+{ if (x > 0.0 && y > 0.0 && x > + 0.999 * DBL_MAX - y ||
+ x < 0.0 && y < 0.0 && x < - 0.999 * DBL_MAX - y)
+ error(mpl, "%.*g + %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ return x + y;
+}
+
+/*----------------------------------------------------------------------
+-- fp_sub - floating-point subtraction.
+--
+-- This routine computes the difference x - y. */
+
+double fp_sub(MPL *mpl, double x, double y)
+{ if (x > 0.0 && y < 0.0 && x > + 0.999 * DBL_MAX + y ||
+ x < 0.0 && y > 0.0 && x < - 0.999 * DBL_MAX + y)
+ error(mpl, "%.*g - %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ return x - y;
+}
+
+/*----------------------------------------------------------------------
+-- fp_less - floating-point non-negative subtraction.
+--
+-- This routine computes the non-negative difference max(0, x - y). */
+
+double fp_less(MPL *mpl, double x, double y)
+{ if (x < y) return 0.0;
+ if (x > 0.0 && y < 0.0 && x > + 0.999 * DBL_MAX + y)
+ error(mpl, "%.*g less %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ return x - y;
+}
+
+/*----------------------------------------------------------------------
+-- fp_mul - floating-point multiplication.
+--
+-- This routine computes the product x * y. */
+
+double fp_mul(MPL *mpl, double x, double y)
+{ if (fabs(y) > 1.0 && fabs(x) > (0.999 * DBL_MAX) / fabs(y))
+ error(mpl, "%.*g * %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ return x * y;
+}
+
+/*----------------------------------------------------------------------
+-- fp_div - floating-point division.
+--
+-- This routine computes the quotient x / y. */
+
+double fp_div(MPL *mpl, double x, double y)
+{ if (fabs(y) < DBL_MIN)
+ error(mpl, "%.*g / %.*g; floating-point zero divide",
+ DBL_DIG, x, DBL_DIG, y);
+ if (fabs(y) < 1.0 && fabs(x) > (0.999 * DBL_MAX) * fabs(y))
+ error(mpl, "%.*g / %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ return x / y;
+}
+
+/*----------------------------------------------------------------------
+-- fp_idiv - floating-point quotient of exact division.
+--
+-- This routine computes the quotient of exact division x div y. */
+
+double fp_idiv(MPL *mpl, double x, double y)
+{ if (fabs(y) < DBL_MIN)
+ error(mpl, "%.*g div %.*g; floating-point zero divide",
+ DBL_DIG, x, DBL_DIG, y);
+ if (fabs(y) < 1.0 && fabs(x) > (0.999 * DBL_MAX) * fabs(y))
+ error(mpl, "%.*g div %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ x /= y;
+ return x > 0.0 ? floor(x) : x < 0.0 ? ceil(x) : 0.0;
+}
+
+/*----------------------------------------------------------------------
+-- fp_mod - floating-point remainder of exact division.
+--
+-- This routine computes the remainder of exact division x mod y.
+--
+-- NOTE: By definition x mod y = x - y * floor(x / y). */
+
+double fp_mod(MPL *mpl, double x, double y)
+{ double r;
+ xassert(mpl == mpl);
+ if (x == 0.0)
+ r = 0.0;
+ else if (y == 0.0)
+ r = x;
+ else
+ { r = fmod(fabs(x), fabs(y));
+ if (r != 0.0)
+ { if (x < 0.0) r = - r;
+ if (x > 0.0 && y < 0.0 || x < 0.0 && y > 0.0) r += y;
+ }
+ }
+ return r;
+}
+
+/*----------------------------------------------------------------------
+-- fp_power - floating-point exponentiation (raise to power).
+--
+-- This routine computes the exponentiation x ** y. */
+
+double fp_power(MPL *mpl, double x, double y)
+{ double r;
+ if (x == 0.0 && y <= 0.0 || x < 0.0 && y != floor(y))
+ error(mpl, "%.*g ** %.*g; result undefined",
+ DBL_DIG, x, DBL_DIG, y);
+ if (x == 0.0) goto eval;
+ if (fabs(x) > 1.0 && y > +1.0 &&
+ +log(fabs(x)) > (0.999 * log(DBL_MAX)) / y ||
+ fabs(x) < 1.0 && y < -1.0 &&
+ +log(fabs(x)) < (0.999 * log(DBL_MAX)) / y)
+ error(mpl, "%.*g ** %.*g; floating-point overflow",
+ DBL_DIG, x, DBL_DIG, y);
+ if (fabs(x) > 1.0 && y < -1.0 &&
+ -log(fabs(x)) < (0.999 * log(DBL_MAX)) / y ||
+ fabs(x) < 1.0 && y > +1.0 &&
+ -log(fabs(x)) > (0.999 * log(DBL_MAX)) / y)
+ r = 0.0;
+ else
+eval: r = pow(x, y);
+ return r;
+}
+
+/*----------------------------------------------------------------------
+-- fp_exp - floating-point base-e exponential.
+--
+-- This routine computes the base-e exponential e ** x. */
+
+double fp_exp(MPL *mpl, double x)
+{ if (x > 0.999 * log(DBL_MAX))
+ error(mpl, "exp(%.*g); floating-point overflow", DBL_DIG, x);
+ return exp(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_log - floating-point natural logarithm.
+--
+-- This routine computes the natural logarithm log x. */
+
+double fp_log(MPL *mpl, double x)
+{ if (x <= 0.0)
+ error(mpl, "log(%.*g); non-positive argument", DBL_DIG, x);
+ return log(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_log10 - floating-point common (decimal) logarithm.
+--
+-- This routine computes the common (decimal) logarithm lg x. */
+
+double fp_log10(MPL *mpl, double x)
+{ if (x <= 0.0)
+ error(mpl, "log10(%.*g); non-positive argument", DBL_DIG, x);
+ return log10(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_sqrt - floating-point square root.
+--
+-- This routine computes the square root x ** 0.5. */
+
+double fp_sqrt(MPL *mpl, double x)
+{ if (x < 0.0)
+ error(mpl, "sqrt(%.*g); negative argument", DBL_DIG, x);
+ return sqrt(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_sin - floating-point trigonometric sine.
+--
+-- This routine computes the trigonometric sine sin(x). */
+
+double fp_sin(MPL *mpl, double x)
+{ if (!(-1e6 <= x && x <= +1e6))
+ error(mpl, "sin(%.*g); argument too large", DBL_DIG, x);
+ return sin(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_cos - floating-point trigonometric cosine.
+--
+-- This routine computes the trigonometric cosine cos(x). */
+
+double fp_cos(MPL *mpl, double x)
+{ if (!(-1e6 <= x && x <= +1e6))
+ error(mpl, "cos(%.*g); argument too large", DBL_DIG, x);
+ return cos(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_tan - floating-point trigonometric tangent.
+--
+-- This routine computes the trigonometric tangent tan(x). */
+
+double fp_tan(MPL *mpl, double x)
+{ if (!(-1e6 <= x && x <= +1e6))
+ error(mpl, "tan(%.*g); argument too large", DBL_DIG, x);
+ return tan(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_atan - floating-point trigonometric arctangent.
+--
+-- This routine computes the trigonometric arctangent atan(x). */
+
+double fp_atan(MPL *mpl, double x)
+{ xassert(mpl == mpl);
+ return atan(x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_atan2 - floating-point trigonometric arctangent.
+--
+-- This routine computes the trigonometric arctangent atan(y / x). */
+
+double fp_atan2(MPL *mpl, double y, double x)
+{ xassert(mpl == mpl);
+ return atan2(y, x);
+}
+
+/*----------------------------------------------------------------------
+-- fp_round - round floating-point value to n fractional digits.
+--
+-- This routine rounds given floating-point value x to n fractional
+-- digits with the formula:
+--
+-- round(x, n) = floor(x * 10^n + 0.5) / 10^n.
+--
+-- The parameter n is assumed to be integer. */
+
+double fp_round(MPL *mpl, double x, double n)
+{ double ten_to_n;
+ if (n != floor(n))
+ error(mpl, "round(%.*g, %.*g); non-integer second argument",
+ DBL_DIG, x, DBL_DIG, n);
+ if (n <= DBL_DIG + 2)
+ { ten_to_n = pow(10.0, n);
+ if (fabs(x) < (0.999 * DBL_MAX) / ten_to_n)
+ { x = floor(x * ten_to_n + 0.5);
+ if (x != 0.0) x /= ten_to_n;
+ }
+ }
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- fp_trunc - truncate floating-point value to n fractional digits.
+--
+-- This routine truncates given floating-point value x to n fractional
+-- digits with the formula:
+--
+-- ( floor(x * 10^n) / 10^n, if x >= 0
+-- trunc(x, n) = <
+-- ( ceil(x * 10^n) / 10^n, if x < 0
+--
+-- The parameter n is assumed to be integer. */
+
+double fp_trunc(MPL *mpl, double x, double n)
+{ double ten_to_n;
+ if (n != floor(n))
+ error(mpl, "trunc(%.*g, %.*g); non-integer second argument",
+ DBL_DIG, x, DBL_DIG, n);
+ if (n <= DBL_DIG + 2)
+ { ten_to_n = pow(10.0, n);
+ if (fabs(x) < (0.999 * DBL_MAX) / ten_to_n)
+ { x = (x >= 0.0 ? floor(x * ten_to_n) : ceil(x * ten_to_n));
+ if (x != 0.0) x /= ten_to_n;
+ }
+ }
+ return x;
+}
+
+/**********************************************************************/
+/* * * PSEUDO-RANDOM NUMBER GENERATORS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- fp_irand224 - pseudo-random integer in the range [0, 2^24).
+--
+-- This routine returns a next pseudo-random integer (converted to
+-- floating-point) which is uniformly distributed between 0 and 2^24-1,
+-- inclusive. */
+
+#define two_to_the_24 0x1000000
+
+double fp_irand224(MPL *mpl)
+{ return
+ (double)rng_unif_rand(mpl->rand, two_to_the_24);
+}
+
+/*----------------------------------------------------------------------
+-- fp_uniform01 - pseudo-random number in the range [0, 1).
+--
+-- This routine returns a next pseudo-random number which is uniformly
+-- distributed in the range [0, 1). */
+
+#define two_to_the_31 ((unsigned int)0x80000000)
+
+double fp_uniform01(MPL *mpl)
+{ return
+ (double)rng_next_rand(mpl->rand) / (double)two_to_the_31;
+}
+
+/*----------------------------------------------------------------------
+-- fp_uniform - pseudo-random number in the range [a, b).
+--
+-- This routine returns a next pseudo-random number which is uniformly
+-- distributed in the range [a, b). */
+
+double fp_uniform(MPL *mpl, double a, double b)
+{ double x;
+ if (a >= b)
+ error(mpl, "Uniform(%.*g, %.*g); invalid range",
+ DBL_DIG, a, DBL_DIG, b);
+ x = fp_uniform01(mpl);
+#if 0
+ x = a * (1.0 - x) + b * x;
+#else
+ x = fp_add(mpl, a * (1.0 - x), b * x);
+#endif
+ return x;
+}
+
+/*----------------------------------------------------------------------
+-- fp_normal01 - Gaussian random variate with mu = 0 and sigma = 1.
+--
+-- This routine returns a Gaussian random variate with zero mean and
+-- unit standard deviation. The polar (Box-Mueller) method is used.
+--
+-- This code is a modified version of the routine gsl_ran_gaussian from
+-- the GNU Scientific Library Version 1.0. */
+
+double fp_normal01(MPL *mpl)
+{ double x, y, r2;
+ do
+ { /* choose x, y in uniform square (-1,-1) to (+1,+1) */
+ x = -1.0 + 2.0 * fp_uniform01(mpl);
+ y = -1.0 + 2.0 * fp_uniform01(mpl);
+ /* see if it is in the unit circle */
+ r2 = x * x + y * y;
+ } while (r2 > 1.0 || r2 == 0.0);
+ /* Box-Muller transform */
+ return y * sqrt(-2.0 * log (r2) / r2);
+}
+
+/*----------------------------------------------------------------------
+-- fp_normal - Gaussian random variate with specified mu and sigma.
+--
+-- This routine returns a Gaussian random variate with mean mu and
+-- standard deviation sigma. */
+
+double fp_normal(MPL *mpl, double mu, double sigma)
+{ double x;
+#if 0
+ x = mu + sigma * fp_normal01(mpl);
+#else
+ x = fp_add(mpl, mu, fp_mul(mpl, sigma, fp_normal01(mpl)));
+#endif
+ return x;
+}
+
+/**********************************************************************/
+/* * * SEGMENTED CHARACTER STRINGS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_string - create character string.
+--
+-- This routine creates a segmented character string, which is exactly
+-- equivalent to specified character string. */
+
+STRING *create_string
+( MPL *mpl,
+ char buf[MAX_LENGTH+1] /* not changed */
+)
+#if 0
+{ STRING *head, *tail;
+ int i, j;
+ xassert(buf != NULL);
+ xassert(strlen(buf) <= MAX_LENGTH);
+ head = tail = dmp_get_atom(mpl->strings, sizeof(STRING));
+ for (i = j = 0; ; i++)
+ { if ((tail->seg[j++] = buf[i]) == '\0') break;
+ if (j == STRSEG_SIZE)
+tail = (tail->next = dmp_get_atom(mpl->strings, sizeof(STRING))), j = 0;
+ }
+ tail->next = NULL;
+ return head;
+}
+#else
+{ STRING *str;
+ xassert(strlen(buf) <= MAX_LENGTH);
+ str = dmp_get_atom(mpl->strings, strlen(buf)+1);
+ strcpy(str, buf);
+ return str;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- copy_string - make copy of character string.
+--
+-- This routine returns an exact copy of segmented character string. */
+
+STRING *copy_string
+( MPL *mpl,
+ STRING *str /* not changed */
+)
+#if 0
+{ STRING *head, *tail;
+ xassert(str != NULL);
+ head = tail = dmp_get_atom(mpl->strings, sizeof(STRING));
+ for (; str != NULL; str = str->next)
+ { memcpy(tail->seg, str->seg, STRSEG_SIZE);
+ if (str->next != NULL)
+tail = (tail->next = dmp_get_atom(mpl->strings, sizeof(STRING)));
+ }
+ tail->next = NULL;
+ return head;
+}
+#else
+{ xassert(mpl == mpl);
+ return create_string(mpl, str);
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- compare_strings - compare one character string with another.
+--
+-- This routine compares one segmented character strings with another
+-- and returns the result of comparison as follows:
+--
+-- = 0 - both strings are identical;
+-- < 0 - the first string precedes the second one;
+-- > 0 - the first string follows the second one. */
+
+int compare_strings
+( MPL *mpl,
+ STRING *str1, /* not changed */
+ STRING *str2 /* not changed */
+)
+#if 0
+{ int j, c1, c2;
+ xassert(mpl == mpl);
+ for (;; str1 = str1->next, str2 = str2->next)
+ { xassert(str1 != NULL);
+ xassert(str2 != NULL);
+ for (j = 0; j < STRSEG_SIZE; j++)
+ { c1 = (unsigned char)str1->seg[j];
+ c2 = (unsigned char)str2->seg[j];
+ if (c1 < c2) return -1;
+ if (c1 > c2) return +1;
+ if (c1 == '\0') goto done;
+ }
+ }
+done: return 0;
+}
+#else
+{ xassert(mpl == mpl);
+ return strcmp(str1, str2);
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- fetch_string - extract content of character string.
+--
+-- This routine returns a character string, which is exactly equivalent
+-- to specified segmented character string. */
+
+char *fetch_string
+( MPL *mpl,
+ STRING *str, /* not changed */
+ char buf[MAX_LENGTH+1] /* modified */
+)
+#if 0
+{ int i, j;
+ xassert(mpl == mpl);
+ xassert(buf != NULL);
+ for (i = 0; ; str = str->next)
+ { xassert(str != NULL);
+ for (j = 0; j < STRSEG_SIZE; j++)
+ if ((buf[i++] = str->seg[j]) == '\0') goto done;
+ }
+done: xassert(strlen(buf) <= MAX_LENGTH);
+ return buf;
+}
+#else
+{ xassert(mpl == mpl);
+ return strcpy(buf, str);
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- delete_string - delete character string.
+--
+-- This routine deletes specified segmented character string. */
+
+void delete_string
+( MPL *mpl,
+ STRING *str /* destroyed */
+)
+#if 0
+{ STRING *temp;
+ xassert(str != NULL);
+ while (str != NULL)
+ { temp = str;
+ str = str->next;
+ dmp_free_atom(mpl->strings, temp, sizeof(STRING));
+ }
+ return;
+}
+#else
+{ dmp_free_atom(mpl->strings, str, strlen(str)+1);
+ return;
+}
+#endif
+
+/**********************************************************************/
+/* * * SYMBOLS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_symbol_num - create symbol of numeric type.
+--
+-- This routine creates a symbol, which has a numeric value specified
+-- as floating-point number. */
+
+SYMBOL *create_symbol_num(MPL *mpl, double num)
+{ SYMBOL *sym;
+ sym = dmp_get_atom(mpl->symbols, sizeof(SYMBOL));
+ sym->num = num;
+ sym->str = NULL;
+ return sym;
+}
+
+/*----------------------------------------------------------------------
+-- create_symbol_str - create symbol of abstract type.
+--
+-- This routine creates a symbol, which has an abstract value specified
+-- as segmented character string. */
+
+SYMBOL *create_symbol_str
+( MPL *mpl,
+ STRING *str /* destroyed */
+)
+{ SYMBOL *sym;
+ xassert(str != NULL);
+ sym = dmp_get_atom(mpl->symbols, sizeof(SYMBOL));
+ sym->num = 0.0;
+ sym->str = str;
+ return sym;
+}
+
+/*----------------------------------------------------------------------
+-- copy_symbol - make copy of symbol.
+--
+-- This routine returns an exact copy of symbol. */
+
+SYMBOL *copy_symbol
+( MPL *mpl,
+ SYMBOL *sym /* not changed */
+)
+{ SYMBOL *copy;
+ xassert(sym != NULL);
+ copy = dmp_get_atom(mpl->symbols, sizeof(SYMBOL));
+ if (sym->str == NULL)
+ { copy->num = sym->num;
+ copy->str = NULL;
+ }
+ else
+ { copy->num = 0.0;
+ copy->str = copy_string(mpl, sym->str);
+ }
+ return copy;
+}
+
+/*----------------------------------------------------------------------
+-- compare_symbols - compare one symbol with another.
+--
+-- This routine compares one symbol with another and returns the result
+-- of comparison as follows:
+--
+-- = 0 - both symbols are identical;
+-- < 0 - the first symbol precedes the second one;
+-- > 0 - the first symbol follows the second one.
+--
+-- Note that the linear order, in which symbols follow each other, is
+-- implementation-dependent. It may be not an alphabetical order. */
+
+int compare_symbols
+( MPL *mpl,
+ SYMBOL *sym1, /* not changed */
+ SYMBOL *sym2 /* not changed */
+)
+{ xassert(sym1 != NULL);
+ xassert(sym2 != NULL);
+ /* let all numeric quantities precede all symbolic quantities */
+ if (sym1->str == NULL && sym2->str == NULL)
+ { if (sym1->num < sym2->num) return -1;
+ if (sym1->num > sym2->num) return +1;
+ return 0;
+ }
+ if (sym1->str == NULL) return -1;
+ if (sym2->str == NULL) return +1;
+ return compare_strings(mpl, sym1->str, sym2->str);
+}
+
+/*----------------------------------------------------------------------
+-- delete_symbol - delete symbol.
+--
+-- This routine deletes specified symbol. */
+
+void delete_symbol
+( MPL *mpl,
+ SYMBOL *sym /* destroyed */
+)
+{ xassert(sym != NULL);
+ if (sym->str != NULL) delete_string(mpl, sym->str);
+ dmp_free_atom(mpl->symbols, sym, sizeof(SYMBOL));
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- format_symbol - format symbol for displaying or printing.
+--
+-- This routine converts specified symbol to a charater string, which
+-- is suitable for displaying or printing.
+--
+-- The resultant string is never longer than 255 characters. If it gets
+-- longer, it is truncated from the right and appended by dots. */
+
+char *format_symbol
+( MPL *mpl,
+ SYMBOL *sym /* not changed */
+)
+{ char *buf = mpl->sym_buf;
+ xassert(sym != NULL);
+ if (sym->str == NULL)
+ sprintf(buf, "%.*g", DBL_DIG, sym->num);
+ else
+ { char str[MAX_LENGTH+1];
+ int quoted, j, len;
+ fetch_string(mpl, sym->str, str);
+ if (!(isalpha((unsigned char)str[0]) || str[0] == '_'))
+ quoted = 1;
+ else
+ { quoted = 0;
+ for (j = 1; str[j] != '\0'; j++)
+ { if (!(isalnum((unsigned char)str[j]) ||
+ strchr("+-._", (unsigned char)str[j]) != NULL))
+ { quoted = 1;
+ break;
+ }
+ }
+ }
+# define safe_append(c) \
+ (void)(len < 255 ? (buf[len++] = (char)(c)) : 0)
+ buf[0] = '\0', len = 0;
+ if (quoted) safe_append('\'');
+ for (j = 0; str[j] != '\0'; j++)
+ { if (quoted && str[j] == '\'') safe_append('\'');
+ safe_append(str[j]);
+ }
+ if (quoted) safe_append('\'');
+# undef safe_append
+ buf[len] = '\0';
+ if (len == 255) strcpy(buf+252, "...");
+ }
+ xassert(strlen(buf) <= 255);
+ return buf;
+}
+
+/*----------------------------------------------------------------------
+-- concat_symbols - concatenate one symbol with another.
+--
+-- This routine concatenates values of two given symbols and assigns
+-- the resultant character string to a new symbol, which is returned on
+-- exit. Both original symbols are destroyed. */
+
+SYMBOL *concat_symbols
+( MPL *mpl,
+ SYMBOL *sym1, /* destroyed */
+ SYMBOL *sym2 /* destroyed */
+)
+{ char str1[MAX_LENGTH+1], str2[MAX_LENGTH+1];
+ xassert(MAX_LENGTH >= DBL_DIG + DBL_DIG);
+ if (sym1->str == NULL)
+ sprintf(str1, "%.*g", DBL_DIG, sym1->num);
+ else
+ fetch_string(mpl, sym1->str, str1);
+ if (sym2->str == NULL)
+ sprintf(str2, "%.*g", DBL_DIG, sym2->num);
+ else
+ fetch_string(mpl, sym2->str, str2);
+ if (strlen(str1) + strlen(str2) > MAX_LENGTH)
+ { char buf[255+1];
+ strcpy(buf, format_symbol(mpl, sym1));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s & %s; resultant symbol exceeds %d characters",
+ buf, format_symbol(mpl, sym2), MAX_LENGTH);
+ }
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ return create_symbol_str(mpl, create_string(mpl, strcat(str1,
+ str2)));
+}
+
+/**********************************************************************/
+/* * * N-TUPLES * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_tuple - create n-tuple.
+--
+-- This routine creates a n-tuple, which initially has no components,
+-- i.e. which is 0-tuple. */
+
+TUPLE *create_tuple(MPL *mpl)
+{ TUPLE *tuple;
+ xassert(mpl == mpl);
+ tuple = NULL;
+ return tuple;
+}
+
+/*----------------------------------------------------------------------
+-- expand_tuple - append symbol to n-tuple.
+--
+-- This routine expands n-tuple appending to it a given symbol, which
+-- becomes its new last component. */
+
+TUPLE *expand_tuple
+( MPL *mpl,
+ TUPLE *tuple, /* destroyed */
+ SYMBOL *sym /* destroyed */
+)
+{ TUPLE *tail, *temp;
+ xassert(sym != NULL);
+ /* create a new component */
+ tail = dmp_get_atom(mpl->tuples, sizeof(TUPLE));
+ tail->sym = sym;
+ tail->next = NULL;
+ /* and append it to the component list */
+ if (tuple == NULL)
+ tuple = tail;
+ else
+ { for (temp = tuple; temp->next != NULL; temp = temp->next);
+ temp->next = tail;
+ }
+ return tuple;
+}
+
+/*----------------------------------------------------------------------
+-- tuple_dimen - determine dimension of n-tuple.
+--
+-- This routine returns dimension of n-tuple, i.e. number of components
+-- in the n-tuple. */
+
+int tuple_dimen
+( MPL *mpl,
+ TUPLE *tuple /* not changed */
+)
+{ TUPLE *temp;
+ int dim = 0;
+ xassert(mpl == mpl);
+ for (temp = tuple; temp != NULL; temp = temp->next) dim++;
+ return dim;
+}
+
+/*----------------------------------------------------------------------
+-- copy_tuple - make copy of n-tuple.
+--
+-- This routine returns an exact copy of n-tuple. */
+
+TUPLE *copy_tuple
+( MPL *mpl,
+ TUPLE *tuple /* not changed */
+)
+{ TUPLE *head, *tail;
+ if (tuple == NULL)
+ head = NULL;
+ else
+ { head = tail = dmp_get_atom(mpl->tuples, sizeof(TUPLE));
+ for (; tuple != NULL; tuple = tuple->next)
+ { xassert(tuple->sym != NULL);
+ tail->sym = copy_symbol(mpl, tuple->sym);
+ if (tuple->next != NULL)
+tail = (tail->next = dmp_get_atom(mpl->tuples, sizeof(TUPLE)));
+ }
+ tail->next = NULL;
+ }
+ return head;
+}
+
+/*----------------------------------------------------------------------
+-- compare_tuples - compare one n-tuple with another.
+--
+-- This routine compares two given n-tuples, which must have the same
+-- dimension (not checked for the sake of efficiency), and returns one
+-- of the following codes:
+--
+-- = 0 - both n-tuples are identical;
+-- < 0 - the first n-tuple precedes the second one;
+-- > 0 - the first n-tuple follows the second one.
+--
+-- Note that the linear order, in which n-tuples follow each other, is
+-- implementation-dependent. It may be not an alphabetical order. */
+
+int compare_tuples
+( MPL *mpl,
+ TUPLE *tuple1, /* not changed */
+ TUPLE *tuple2 /* not changed */
+)
+{ TUPLE *item1, *item2;
+ int ret;
+ xassert(mpl == mpl);
+ for (item1 = tuple1, item2 = tuple2; item1 != NULL;
+ item1 = item1->next, item2 = item2->next)
+ { xassert(item2 != NULL);
+ xassert(item1->sym != NULL);
+ xassert(item2->sym != NULL);
+ ret = compare_symbols(mpl, item1->sym, item2->sym);
+ if (ret != 0) return ret;
+ }
+ xassert(item2 == NULL);
+ return 0;
+}
+
+/*----------------------------------------------------------------------
+-- build_subtuple - build subtuple of given n-tuple.
+--
+-- This routine builds subtuple, which consists of first dim components
+-- of given n-tuple. */
+
+TUPLE *build_subtuple
+( MPL *mpl,
+ TUPLE *tuple, /* not changed */
+ int dim
+)
+{ TUPLE *head, *temp;
+ int j;
+ head = create_tuple(mpl);
+ for (j = 1, temp = tuple; j <= dim; j++, temp = temp->next)
+ { xassert(temp != NULL);
+ head = expand_tuple(mpl, head, copy_symbol(mpl, temp->sym));
+ }
+ return head;
+}
+
+/*----------------------------------------------------------------------
+-- delete_tuple - delete n-tuple.
+--
+-- This routine deletes specified n-tuple. */
+
+void delete_tuple
+( MPL *mpl,
+ TUPLE *tuple /* destroyed */
+)
+{ TUPLE *temp;
+ while (tuple != NULL)
+ { temp = tuple;
+ tuple = temp->next;
+ xassert(temp->sym != NULL);
+ delete_symbol(mpl, temp->sym);
+ dmp_free_atom(mpl->tuples, temp, sizeof(TUPLE));
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- format_tuple - format n-tuple for displaying or printing.
+--
+-- This routine converts specified n-tuple to a character string, which
+-- is suitable for displaying or printing.
+--
+-- The resultant string is never longer than 255 characters. If it gets
+-- longer, it is truncated from the right and appended by dots. */
+
+char *format_tuple
+( MPL *mpl,
+ int c,
+ TUPLE *tuple /* not changed */
+)
+{ TUPLE *temp;
+ int dim, j, len;
+ char *buf = mpl->tup_buf, str[255+1], *save;
+# define safe_append(c) \
+ (void)(len < 255 ? (buf[len++] = (char)(c)) : 0)
+ buf[0] = '\0', len = 0;
+ dim = tuple_dimen(mpl, tuple);
+ if (c == '[' && dim > 0) safe_append('[');
+ if (c == '(' && dim > 1) safe_append('(');
+ for (temp = tuple; temp != NULL; temp = temp->next)
+ { if (temp != tuple) safe_append(',');
+ xassert(temp->sym != NULL);
+ save = mpl->sym_buf;
+ mpl->sym_buf = str;
+ format_symbol(mpl, temp->sym);
+ mpl->sym_buf = save;
+ xassert(strlen(str) < sizeof(str));
+ for (j = 0; str[j] != '\0'; j++) safe_append(str[j]);
+ }
+ if (c == '[' && dim > 0) safe_append(']');
+ if (c == '(' && dim > 1) safe_append(')');
+# undef safe_append
+ buf[len] = '\0';
+ if (len == 255) strcpy(buf+252, "...");
+ xassert(strlen(buf) <= 255);
+ return buf;
+}
+
+/**********************************************************************/
+/* * * ELEMENTAL SETS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_elemset - create elemental set.
+--
+-- This routine creates an elemental set, whose members are n-tuples of
+-- specified dimension. Being created the set is initially empty. */
+
+ELEMSET *create_elemset(MPL *mpl, int dim)
+{ ELEMSET *set;
+ xassert(dim > 0);
+ set = create_array(mpl, A_NONE, dim);
+ return set;
+}
+
+/*----------------------------------------------------------------------
+-- find_tuple - check if elemental set contains given n-tuple.
+--
+-- This routine finds given n-tuple in specified elemental set in order
+-- to check if the set contains that n-tuple. If the n-tuple is found,
+-- the routine returns pointer to corresponding array member. Otherwise
+-- null pointer is returned. */
+
+MEMBER *find_tuple
+( MPL *mpl,
+ ELEMSET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ xassert(set != NULL);
+ xassert(set->type == A_NONE);
+ xassert(set->dim == tuple_dimen(mpl, tuple));
+ return find_member(mpl, set, tuple);
+}
+
+/*----------------------------------------------------------------------
+-- add_tuple - add new n-tuple to elemental set.
+--
+-- This routine adds given n-tuple to specified elemental set.
+--
+-- For the sake of efficiency this routine doesn't check whether the
+-- set already contains the same n-tuple or not. Therefore the calling
+-- program should use the routine find_tuple (if necessary) in order to
+-- make sure that the given n-tuple is not contained in the set, since
+-- duplicate n-tuples within the same set are not allowed. */
+
+MEMBER *add_tuple
+( MPL *mpl,
+ ELEMSET *set, /* modified */
+ TUPLE *tuple /* destroyed */
+)
+{ MEMBER *memb;
+ xassert(set != NULL);
+ xassert(set->type == A_NONE);
+ xassert(set->dim == tuple_dimen(mpl, tuple));
+ memb = add_member(mpl, set, tuple);
+ memb->value.none = NULL;
+ return memb;
+}
+
+/*----------------------------------------------------------------------
+-- check_then_add - check and add new n-tuple to elemental set.
+--
+-- This routine is equivalent to the routine add_tuple except that it
+-- does check for duplicate n-tuples. */
+
+MEMBER *check_then_add
+( MPL *mpl,
+ ELEMSET *set, /* modified */
+ TUPLE *tuple /* destroyed */
+)
+{ if (find_tuple(mpl, set, tuple) != NULL)
+ error(mpl, "duplicate tuple %s detected", format_tuple(mpl,
+ '(', tuple));
+ return add_tuple(mpl, set, tuple);
+}
+
+/*----------------------------------------------------------------------
+-- copy_elemset - make copy of elemental set.
+--
+-- This routine makes an exact copy of elemental set. */
+
+ELEMSET *copy_elemset
+( MPL *mpl,
+ ELEMSET *set /* not changed */
+)
+{ ELEMSET *copy;
+ MEMBER *memb;
+ xassert(set != NULL);
+ xassert(set->type == A_NONE);
+ xassert(set->dim > 0);
+ copy = create_elemset(mpl, set->dim);
+ for (memb = set->head; memb != NULL; memb = memb->next)
+ add_tuple(mpl, copy, copy_tuple(mpl, memb->tuple));
+ return copy;
+}
+
+/*----------------------------------------------------------------------
+-- delete_elemset - delete elemental set.
+--
+-- This routine deletes specified elemental set. */
+
+void delete_elemset
+( MPL *mpl,
+ ELEMSET *set /* destroyed */
+)
+{ xassert(set != NULL);
+ xassert(set->type == A_NONE);
+ delete_array(mpl, set);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- arelset_size - compute size of "arithmetic" elemental set.
+--
+-- This routine computes the size of "arithmetic" elemental set, which
+-- is specified in the form of arithmetic progression:
+--
+-- { t0 .. tf by dt }.
+--
+-- The size is computed using the formula:
+--
+-- n = max(0, floor((tf - t0) / dt) + 1). */
+
+int arelset_size(MPL *mpl, double t0, double tf, double dt)
+{ double temp;
+ if (dt == 0.0)
+ error(mpl, "%.*g .. %.*g by %.*g; zero stride not allowed",
+ DBL_DIG, t0, DBL_DIG, tf, DBL_DIG, dt);
+ if (tf > 0.0 && t0 < 0.0 && tf > + 0.999 * DBL_MAX + t0)
+ temp = +DBL_MAX;
+ else if (tf < 0.0 && t0 > 0.0 && tf < - 0.999 * DBL_MAX + t0)
+ temp = -DBL_MAX;
+ else
+ temp = tf - t0;
+ if (fabs(dt) < 1.0 && fabs(temp) > (0.999 * DBL_MAX) * fabs(dt))
+ { if (temp > 0.0 && dt > 0.0 || temp < 0.0 && dt < 0.0)
+ temp = +DBL_MAX;
+ else
+ temp = 0.0;
+ }
+ else
+ { temp = floor(temp / dt) + 1.0;
+ if (temp < 0.0) temp = 0.0;
+ }
+ xassert(temp >= 0.0);
+ if (temp > (double)(INT_MAX - 1))
+ error(mpl, "%.*g .. %.*g by %.*g; set too large",
+ DBL_DIG, t0, DBL_DIG, tf, DBL_DIG, dt);
+ return (int)(temp + 0.5);
+}
+
+/*----------------------------------------------------------------------
+-- arelset_member - compute member of "arithmetic" elemental set.
+--
+-- This routine returns a numeric value of symbol, which is equivalent
+-- to j-th member of given "arithmetic" elemental set specified in the
+-- form of arithmetic progression:
+--
+-- { t0 .. tf by dt }.
+--
+-- The symbol value is computed with the formula:
+--
+-- j-th member = t0 + (j - 1) * dt,
+--
+-- The number j must satisfy to the restriction 1 <= j <= n, where n is
+-- the set size computed by the routine arelset_size. */
+
+double arelset_member(MPL *mpl, double t0, double tf, double dt, int j)
+{ xassert(1 <= j && j <= arelset_size(mpl, t0, tf, dt));
+ return t0 + (double)(j - 1) * dt;
+}
+
+/*----------------------------------------------------------------------
+-- create_arelset - create "arithmetic" elemental set.
+--
+-- This routine creates "arithmetic" elemental set, which is specified
+-- in the form of arithmetic progression:
+--
+-- { t0 .. tf by dt }.
+--
+-- Components of this set are 1-tuples. */
+
+ELEMSET *create_arelset(MPL *mpl, double t0, double tf, double dt)
+{ ELEMSET *set;
+ int j, n;
+ set = create_elemset(mpl, 1);
+ n = arelset_size(mpl, t0, tf, dt);
+ for (j = 1; j <= n; j++)
+ { add_tuple
+ ( mpl,
+ set,
+ expand_tuple
+ ( mpl,
+ create_tuple(mpl),
+ create_symbol_num
+ ( mpl,
+ arelset_member(mpl, t0, tf, dt, j)
+ )
+ )
+ );
+ }
+ return set;
+}
+
+/*----------------------------------------------------------------------
+-- set_union - union of two elemental sets.
+--
+-- This routine computes the union:
+--
+-- X U Y = { j | (j in X) or (j in Y) },
+--
+-- where X and Y are given elemental sets (destroyed on exit). */
+
+ELEMSET *set_union
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+)
+{ MEMBER *memb;
+ xassert(X != NULL);
+ xassert(X->type == A_NONE);
+ xassert(X->dim > 0);
+ xassert(Y != NULL);
+ xassert(Y->type == A_NONE);
+ xassert(Y->dim > 0);
+ xassert(X->dim == Y->dim);
+ for (memb = Y->head; memb != NULL; memb = memb->next)
+ { if (find_tuple(mpl, X, memb->tuple) == NULL)
+ add_tuple(mpl, X, copy_tuple(mpl, memb->tuple));
+ }
+ delete_elemset(mpl, Y);
+ return X;
+}
+
+/*----------------------------------------------------------------------
+-- set_diff - difference between two elemental sets.
+--
+-- This routine computes the difference:
+--
+-- X \ Y = { j | (j in X) and (j not in Y) },
+--
+-- where X and Y are given elemental sets (destroyed on exit). */
+
+ELEMSET *set_diff
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+)
+{ ELEMSET *Z;
+ MEMBER *memb;
+ xassert(X != NULL);
+ xassert(X->type == A_NONE);
+ xassert(X->dim > 0);
+ xassert(Y != NULL);
+ xassert(Y->type == A_NONE);
+ xassert(Y->dim > 0);
+ xassert(X->dim == Y->dim);
+ Z = create_elemset(mpl, X->dim);
+ for (memb = X->head; memb != NULL; memb = memb->next)
+ { if (find_tuple(mpl, Y, memb->tuple) == NULL)
+ add_tuple(mpl, Z, copy_tuple(mpl, memb->tuple));
+ }
+ delete_elemset(mpl, X);
+ delete_elemset(mpl, Y);
+ return Z;
+}
+
+/*----------------------------------------------------------------------
+-- set_symdiff - symmetric difference between two elemental sets.
+--
+-- This routine computes the symmetric difference:
+--
+-- X (+) Y = (X \ Y) U (Y \ X),
+--
+-- where X and Y are given elemental sets (destroyed on exit). */
+
+ELEMSET *set_symdiff
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+)
+{ ELEMSET *Z;
+ MEMBER *memb;
+ xassert(X != NULL);
+ xassert(X->type == A_NONE);
+ xassert(X->dim > 0);
+ xassert(Y != NULL);
+ xassert(Y->type == A_NONE);
+ xassert(Y->dim > 0);
+ xassert(X->dim == Y->dim);
+ /* Z := X \ Y */
+ Z = create_elemset(mpl, X->dim);
+ for (memb = X->head; memb != NULL; memb = memb->next)
+ { if (find_tuple(mpl, Y, memb->tuple) == NULL)
+ add_tuple(mpl, Z, copy_tuple(mpl, memb->tuple));
+ }
+ /* Z := Z U (Y \ X) */
+ for (memb = Y->head; memb != NULL; memb = memb->next)
+ { if (find_tuple(mpl, X, memb->tuple) == NULL)
+ add_tuple(mpl, Z, copy_tuple(mpl, memb->tuple));
+ }
+ delete_elemset(mpl, X);
+ delete_elemset(mpl, Y);
+ return Z;
+}
+
+/*----------------------------------------------------------------------
+-- set_inter - intersection of two elemental sets.
+--
+-- This routine computes the intersection:
+--
+-- X ^ Y = { j | (j in X) and (j in Y) },
+--
+-- where X and Y are given elemental sets (destroyed on exit). */
+
+ELEMSET *set_inter
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+)
+{ ELEMSET *Z;
+ MEMBER *memb;
+ xassert(X != NULL);
+ xassert(X->type == A_NONE);
+ xassert(X->dim > 0);
+ xassert(Y != NULL);
+ xassert(Y->type == A_NONE);
+ xassert(Y->dim > 0);
+ xassert(X->dim == Y->dim);
+ Z = create_elemset(mpl, X->dim);
+ for (memb = X->head; memb != NULL; memb = memb->next)
+ { if (find_tuple(mpl, Y, memb->tuple) != NULL)
+ add_tuple(mpl, Z, copy_tuple(mpl, memb->tuple));
+ }
+ delete_elemset(mpl, X);
+ delete_elemset(mpl, Y);
+ return Z;
+}
+
+/*----------------------------------------------------------------------
+-- set_cross - cross (Cartesian) product of two elemental sets.
+--
+-- This routine computes the cross (Cartesian) product:
+--
+-- X x Y = { (i,j) | (i in X) and (j in Y) },
+--
+-- where X and Y are given elemental sets (destroyed on exit). */
+
+ELEMSET *set_cross
+( MPL *mpl,
+ ELEMSET *X, /* destroyed */
+ ELEMSET *Y /* destroyed */
+)
+{ ELEMSET *Z;
+ MEMBER *memx, *memy;
+ TUPLE *tuple, *temp;
+ xassert(X != NULL);
+ xassert(X->type == A_NONE);
+ xassert(X->dim > 0);
+ xassert(Y != NULL);
+ xassert(Y->type == A_NONE);
+ xassert(Y->dim > 0);
+ Z = create_elemset(mpl, X->dim + Y->dim);
+ for (memx = X->head; memx != NULL; memx = memx->next)
+ { for (memy = Y->head; memy != NULL; memy = memy->next)
+ { tuple = copy_tuple(mpl, memx->tuple);
+ for (temp = memy->tuple; temp != NULL; temp = temp->next)
+ tuple = expand_tuple(mpl, tuple, copy_symbol(mpl,
+ temp->sym));
+ add_tuple(mpl, Z, tuple);
+ }
+ }
+ delete_elemset(mpl, X);
+ delete_elemset(mpl, Y);
+ return Z;
+}
+
+/**********************************************************************/
+/* * * ELEMENTAL VARIABLES * * */
+/**********************************************************************/
+
+/* (there are no specific routines for elemental variables) */
+
+/**********************************************************************/
+/* * * LINEAR FORMS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- constant_term - create constant term.
+--
+-- This routine creates the linear form, which is a constant term. */
+
+FORMULA *constant_term(MPL *mpl, double coef)
+{ FORMULA *form;
+ if (coef == 0.0)
+ form = NULL;
+ else
+ { form = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ form->coef = coef;
+ form->var = NULL;
+ form->next = NULL;
+ }
+ return form;
+}
+
+/*----------------------------------------------------------------------
+-- single_variable - create single variable.
+--
+-- This routine creates the linear form, which is a single elemental
+-- variable. */
+
+FORMULA *single_variable
+( MPL *mpl,
+ ELEMVAR *var /* referenced */
+)
+{ FORMULA *form;
+ xassert(var != NULL);
+ form = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ form->coef = 1.0;
+ form->var = var;
+ form->next = NULL;
+ return form;
+}
+
+/*----------------------------------------------------------------------
+-- copy_formula - make copy of linear form.
+--
+-- This routine returns an exact copy of linear form. */
+
+FORMULA *copy_formula
+( MPL *mpl,
+ FORMULA *form /* not changed */
+)
+{ FORMULA *head, *tail;
+ if (form == NULL)
+ head = NULL;
+ else
+ { head = tail = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ for (; form != NULL; form = form->next)
+ { tail->coef = form->coef;
+ tail->var = form->var;
+ if (form->next != NULL)
+tail = (tail->next = dmp_get_atom(mpl->formulae, sizeof(FORMULA)));
+ }
+ tail->next = NULL;
+ }
+ return head;
+}
+
+/*----------------------------------------------------------------------
+-- delete_formula - delete linear form.
+--
+-- This routine deletes specified linear form. */
+
+void delete_formula
+( MPL *mpl,
+ FORMULA *form /* destroyed */
+)
+{ FORMULA *temp;
+ while (form != NULL)
+ { temp = form;
+ form = form->next;
+ dmp_free_atom(mpl->formulae, temp, sizeof(FORMULA));
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- linear_comb - linear combination of two linear forms.
+--
+-- This routine computes the linear combination:
+--
+-- a * fx + b * fy,
+--
+-- where a and b are numeric coefficients, fx and fy are linear forms
+-- (destroyed on exit). */
+
+FORMULA *linear_comb
+( MPL *mpl,
+ double a, FORMULA *fx, /* destroyed */
+ double b, FORMULA *fy /* destroyed */
+)
+{ FORMULA *form = NULL, *term, *temp;
+ double c0 = 0.0;
+ for (term = fx; term != NULL; term = term->next)
+ { if (term->var == NULL)
+ c0 = fp_add(mpl, c0, fp_mul(mpl, a, term->coef));
+ else
+ term->var->temp =
+ fp_add(mpl, term->var->temp, fp_mul(mpl, a, term->coef));
+ }
+ for (term = fy; term != NULL; term = term->next)
+ { if (term->var == NULL)
+ c0 = fp_add(mpl, c0, fp_mul(mpl, b, term->coef));
+ else
+ term->var->temp =
+ fp_add(mpl, term->var->temp, fp_mul(mpl, b, term->coef));
+ }
+ for (term = fx; term != NULL; term = term->next)
+ { if (term->var != NULL && term->var->temp != 0.0)
+ { temp = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ temp->coef = term->var->temp, temp->var = term->var;
+ temp->next = form, form = temp;
+ term->var->temp = 0.0;
+ }
+ }
+ for (term = fy; term != NULL; term = term->next)
+ { if (term->var != NULL && term->var->temp != 0.0)
+ { temp = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ temp->coef = term->var->temp, temp->var = term->var;
+ temp->next = form, form = temp;
+ term->var->temp = 0.0;
+ }
+ }
+ if (c0 != 0.0)
+ { temp = dmp_get_atom(mpl->formulae, sizeof(FORMULA));
+ temp->coef = c0, temp->var = NULL;
+ temp->next = form, form = temp;
+ }
+ delete_formula(mpl, fx);
+ delete_formula(mpl, fy);
+ return form;
+}
+
+/*----------------------------------------------------------------------
+-- remove_constant - remove constant term from linear form.
+--
+-- This routine removes constant term from linear form and stores its
+-- value to given location. */
+
+FORMULA *remove_constant
+( MPL *mpl,
+ FORMULA *form, /* destroyed */
+ double *coef /* modified */
+)
+{ FORMULA *head = NULL, *temp;
+ *coef = 0.0;
+ while (form != NULL)
+ { temp = form;
+ form = form->next;
+ if (temp->var == NULL)
+ { /* constant term */
+ *coef = fp_add(mpl, *coef, temp->coef);
+ dmp_free_atom(mpl->formulae, temp, sizeof(FORMULA));
+ }
+ else
+ { /* linear term */
+ temp->next = head;
+ head = temp;
+ }
+ }
+ return head;
+}
+
+/*----------------------------------------------------------------------
+-- reduce_terms - reduce identical terms in linear form.
+--
+-- This routine reduces identical terms in specified linear form. */
+
+FORMULA *reduce_terms
+( MPL *mpl,
+ FORMULA *form /* destroyed */
+)
+{ FORMULA *term, *next_term;
+ double c0 = 0.0;
+ for (term = form; term != NULL; term = term->next)
+ { if (term->var == NULL)
+ c0 = fp_add(mpl, c0, term->coef);
+ else
+ term->var->temp = fp_add(mpl, term->var->temp, term->coef);
+ }
+ next_term = form, form = NULL;
+ for (term = next_term; term != NULL; term = next_term)
+ { next_term = term->next;
+ if (term->var == NULL && c0 != 0.0)
+ { term->coef = c0, c0 = 0.0;
+ term->next = form, form = term;
+ }
+ else if (term->var != NULL && term->var->temp != 0.0)
+ { term->coef = term->var->temp, term->var->temp = 0.0;
+ term->next = form, form = term;
+ }
+ else
+ dmp_free_atom(mpl->formulae, term, sizeof(FORMULA));
+ }
+ return form;
+}
+
+/**********************************************************************/
+/* * * ELEMENTAL CONSTRAINTS * * */
+/**********************************************************************/
+
+/* (there are no specific routines for elemental constraints) */
+
+/**********************************************************************/
+/* * * GENERIC VALUES * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- delete_value - delete generic value.
+--
+-- This routine deletes specified generic value.
+--
+-- NOTE: The generic value to be deleted must be valid. */
+
+void delete_value
+( MPL *mpl,
+ int type,
+ VALUE *value /* content destroyed */
+)
+{ xassert(value != NULL);
+ switch (type)
+ { case A_NONE:
+ value->none = NULL;
+ break;
+ case A_NUMERIC:
+ value->num = 0.0;
+ break;
+ case A_SYMBOLIC:
+ delete_symbol(mpl, value->sym), value->sym = NULL;
+ break;
+ case A_LOGICAL:
+ value->bit = 0;
+ break;
+ case A_TUPLE:
+ delete_tuple(mpl, value->tuple), value->tuple = NULL;
+ break;
+ case A_ELEMSET:
+ delete_elemset(mpl, value->set), value->set = NULL;
+ break;
+ case A_ELEMVAR:
+ value->var = NULL;
+ break;
+ case A_FORMULA:
+ delete_formula(mpl, value->form), value->form = NULL;
+ break;
+ case A_ELEMCON:
+ value->con = NULL;
+ break;
+ default:
+ xassert(type != type);
+ }
+ return;
+}
+
+/**********************************************************************/
+/* * * SYMBOLICALLY INDEXED ARRAYS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- create_array - create array.
+--
+-- This routine creates an array of specified type and dimension. Being
+-- created the array is initially empty.
+--
+-- The type indicator determines generic values, which can be assigned
+-- to the array members:
+--
+-- A_NONE - none (members have no assigned values)
+-- A_NUMERIC - floating-point numbers
+-- A_SYMBOLIC - symbols
+-- A_ELEMSET - elemental sets
+-- A_ELEMVAR - elemental variables
+-- A_ELEMCON - elemental constraints
+--
+-- The dimension may be 0, in which case the array consists of the only
+-- member (such arrays represent 0-dimensional objects). */
+
+ARRAY *create_array(MPL *mpl, int type, int dim)
+{ ARRAY *array;
+ xassert(type == A_NONE || type == A_NUMERIC ||
+ type == A_SYMBOLIC || type == A_ELEMSET ||
+ type == A_ELEMVAR || type == A_ELEMCON);
+ xassert(dim >= 0);
+ array = dmp_get_atom(mpl->arrays, sizeof(ARRAY));
+ array->type = type;
+ array->dim = dim;
+ array->size = 0;
+ array->head = NULL;
+ array->tail = NULL;
+ array->tree = NULL;
+ array->prev = NULL;
+ array->next = mpl->a_list;
+ /* include the array in the global array list */
+ if (array->next != NULL) array->next->prev = array;
+ mpl->a_list = array;
+ return array;
+}
+
+/*----------------------------------------------------------------------
+-- find_member - find array member with given n-tuple.
+--
+-- This routine finds an array member, which has given n-tuple. If the
+-- array is short, the linear search is used. Otherwise the routine
+-- autimatically creates the search tree (i.e. the array index) to find
+-- members for logarithmic time. */
+
+static int compare_member_tuples(void *info, const void *key1,
+ const void *key2)
+{ /* this is an auxiliary routine used to compare keys, which are
+ n-tuples assigned to array members */
+ return compare_tuples((MPL *)info, (TUPLE *)key1, (TUPLE *)key2);
+}
+
+MEMBER *find_member
+( MPL *mpl,
+ ARRAY *array, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ xassert(array != NULL);
+ /* the n-tuple must have the same dimension as the array */
+ xassert(tuple_dimen(mpl, tuple) == array->dim);
+ /* if the array is large enough, create the search tree and index
+ all existing members of the array */
+ if (array->size > 30 && array->tree == NULL)
+ { array->tree = avl_create_tree(compare_member_tuples, mpl);
+ for (memb = array->head; memb != NULL; memb = memb->next)
+avl_set_node_link(avl_insert_node(array->tree, memb->tuple),
+ (void *)memb);
+ }
+ /* find a member, which has the given tuple */
+ if (array->tree == NULL)
+ { /* the search tree doesn't exist; use the linear search */
+ for (memb = array->head; memb != NULL; memb = memb->next)
+ if (compare_tuples(mpl, memb->tuple, tuple) == 0) break;
+ }
+ else
+ { /* the search tree exists; use the binary search */
+ AVLNODE *node;
+ node = avl_find_node(array->tree, tuple);
+memb = (MEMBER *)(node == NULL ? NULL : avl_get_node_link(node));
+ }
+ return memb;
+}
+
+/*----------------------------------------------------------------------
+-- add_member - add new member to array.
+--
+-- This routine creates a new member with given n-tuple and adds it to
+-- specified array.
+--
+-- For the sake of efficiency this routine doesn't check whether the
+-- array already contains a member with the given n-tuple or not. Thus,
+-- if necessary, the calling program should use the routine find_member
+-- in order to be sure that the array contains no member with the same
+-- n-tuple, because members with duplicate n-tuples are not allowed.
+--
+-- This routine assigns no generic value to the new member, because the
+-- calling program must do that. */
+
+MEMBER *add_member
+( MPL *mpl,
+ ARRAY *array, /* modified */
+ TUPLE *tuple /* destroyed */
+)
+{ MEMBER *memb;
+ xassert(array != NULL);
+ /* the n-tuple must have the same dimension as the array */
+ xassert(tuple_dimen(mpl, tuple) == array->dim);
+ /* create new member */
+ memb = dmp_get_atom(mpl->members, sizeof(MEMBER));
+ memb->tuple = tuple;
+ memb->next = NULL;
+ memset(&memb->value, '?', sizeof(VALUE));
+ /* and append it to the member list */
+ array->size++;
+ if (array->head == NULL)
+ array->head = memb;
+ else
+ array->tail->next = memb;
+ array->tail = memb;
+ /* if the search tree exists, index the new member */
+ if (array->tree != NULL)
+avl_set_node_link(avl_insert_node(array->tree, memb->tuple),
+ (void *)memb);
+ return memb;
+}
+
+/*----------------------------------------------------------------------
+-- delete_array - delete array.
+--
+-- This routine deletes specified array.
+--
+-- Generic values assigned to the array members are not deleted by this
+-- routine. The calling program itself must delete all assigned generic
+-- values before deleting the array. */
+
+void delete_array
+( MPL *mpl,
+ ARRAY *array /* destroyed */
+)
+{ MEMBER *memb;
+ xassert(array != NULL);
+ /* delete all existing array members */
+ while (array->head != NULL)
+ { memb = array->head;
+ array->head = memb->next;
+ delete_tuple(mpl, memb->tuple);
+ dmp_free_atom(mpl->members, memb, sizeof(MEMBER));
+ }
+ /* if the search tree exists, also delete it */
+ if (array->tree != NULL) avl_delete_tree(array->tree);
+ /* remove the array from the global array list */
+ if (array->prev == NULL)
+ mpl->a_list = array->next;
+ else
+ array->prev->next = array->next;
+ if (array->next == NULL)
+ ;
+ else
+ array->next->prev = array->prev;
+ /* delete the array descriptor */
+ dmp_free_atom(mpl->arrays, array, sizeof(ARRAY));
+ return;
+}
+
+/**********************************************************************/
+/* * * DOMAINS AND DUMMY INDICES * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- assign_dummy_index - assign new value to dummy index.
+--
+-- This routine assigns new value to specified dummy index and, that is
+-- important, invalidates all temporary resultant values, which depends
+-- on that dummy index. */
+
+void assign_dummy_index
+( MPL *mpl,
+ DOMAIN_SLOT *slot, /* modified */
+ SYMBOL *value /* not changed */
+)
+{ CODE *leaf, *code;
+ xassert(slot != NULL);
+ xassert(value != NULL);
+ /* delete the current value assigned to the dummy index */
+ if (slot->value != NULL)
+ { /* if the current value and the new one are identical, actual
+ assignment is not needed */
+ if (compare_symbols(mpl, slot->value, value) == 0) goto done;
+ /* delete a symbol, which is the current value */
+ delete_symbol(mpl, slot->value), slot->value = NULL;
+ }
+ /* now walk through all the pseudo-codes with op = O_INDEX, which
+ refer to the dummy index to be changed (these pseudo-codes are
+ leaves in the forest of *all* expressions in the database) */
+ for (leaf = slot->list; leaf != NULL; leaf = leaf->arg.index.
+ next)
+ { xassert(leaf->op == O_INDEX);
+ /* invalidate all resultant values, which depend on the dummy
+ index, walking from the current leaf toward the root of the
+ corresponding expression tree */
+ for (code = leaf; code != NULL; code = code->up)
+ { if (code->valid)
+ { /* invalidate and delete resultant value */
+ code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ }
+ }
+ /* assign new value to the dummy index */
+ slot->value = copy_symbol(mpl, value);
+done: return;
+}
+
+/*----------------------------------------------------------------------
+-- update_dummy_indices - update current values of dummy indices.
+--
+-- This routine assigns components of "backup" n-tuple to dummy indices
+-- of specified domain block. If no "backup" n-tuple is defined for the
+-- domain block, values of the dummy indices remain untouched. */
+
+void update_dummy_indices
+( MPL *mpl,
+ DOMAIN_BLOCK *block /* not changed */
+)
+{ DOMAIN_SLOT *slot;
+ TUPLE *temp;
+ if (block->backup != NULL)
+ { for (slot = block->list, temp = block->backup; slot != NULL;
+ slot = slot->next, temp = temp->next)
+ { xassert(temp != NULL);
+ xassert(temp->sym != NULL);
+ assign_dummy_index(mpl, slot, temp->sym);
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- enter_domain_block - enter domain block.
+--
+-- Let specified domain block have the form:
+--
+-- { ..., (j1, j2, ..., jn) in J, ... }
+--
+-- where j1, j2, ..., jn are dummy indices, J is a basic set.
+--
+-- This routine does the following:
+--
+-- 1. Checks if the given n-tuple is a member of the basic set J. Note
+-- that J being *out of the scope* of the domain block cannot depend
+-- on the dummy indices in the same and inner domain blocks, so it
+-- can be computed before the dummy indices are assigned new values.
+-- If this check fails, the routine returns with non-zero code.
+--
+-- 2. Saves current values of the dummy indices j1, j2, ..., jn.
+--
+-- 3. Assigns new values, which are components of the given n-tuple, to
+-- the dummy indices j1, j2, ..., jn. If dimension of the n-tuple is
+-- larger than n, its extra components n+1, n+2, ... are not used.
+--
+-- 4. Calls the formal routine func which either enters the next domain
+-- block or evaluates some code within the domain scope.
+--
+-- 5. Restores former values of the dummy indices j1, j2, ..., jn.
+--
+-- Since current values assigned to the dummy indices on entry to this
+-- routine are restored on exit, the formal routine func is allowed to
+-- call this routine recursively. */
+
+int enter_domain_block
+( MPL *mpl,
+ DOMAIN_BLOCK *block, /* not changed */
+ TUPLE *tuple, /* not changed */
+ void *info, void (*func)(MPL *mpl, void *info)
+)
+{ TUPLE *backup;
+ int ret = 0;
+ /* check if the given n-tuple is a member of the basic set */
+ xassert(block->code != NULL);
+ if (!is_member(mpl, block->code, tuple))
+ { ret = 1;
+ goto done;
+ }
+ /* save reference to "backup" n-tuple, which was used to assign
+ current values of the dummy indices (it is sufficient to save
+ reference, not value, because that n-tuple is defined in some
+ outer level of recursion and therefore cannot be changed on
+ this and deeper recursive calls) */
+ backup = block->backup;
+ /* set up new "backup" n-tuple, which defines new values of the
+ dummy indices */
+ block->backup = tuple;
+ /* assign new values to the dummy indices */
+ update_dummy_indices(mpl, block);
+ /* call the formal routine that does the rest part of the job */
+ func(mpl, info);
+ /* restore reference to the former "backup" n-tuple */
+ block->backup = backup;
+ /* restore former values of the dummy indices; note that if the
+ domain block just escaped has no other active instances which
+ may exist due to recursion (it is indicated by a null pointer
+ to the former n-tuple), former values of the dummy indices are
+ undefined; therefore in this case the routine keeps currently
+ assigned values of the dummy indices that involves keeping all
+ dependent temporary results and thereby, if this domain block
+ is not used recursively, allows improving efficiency */
+ update_dummy_indices(mpl, block);
+done: return ret;
+}
+
+/*----------------------------------------------------------------------
+-- eval_within_domain - perform evaluation within domain scope.
+--
+-- This routine assigns new values (symbols) to all dummy indices of
+-- specified domain and calls the formal routine func, which is used to
+-- evaluate some code in the domain scope. Each free dummy index in the
+-- domain is assigned a value specified in the corresponding component
+-- of given n-tuple. Non-free dummy indices are assigned values, which
+-- are computed by this routine.
+--
+-- Number of components in the given n-tuple must be the same as number
+-- of free indices in the domain.
+--
+-- If the given n-tuple is not a member of the domain set, the routine
+-- func is not called, and non-zero code is returned.
+--
+-- For the sake of convenience it is allowed to specify domain as NULL
+-- (then n-tuple also must be 0-tuple, i.e. empty), in which case this
+-- routine just calls the routine func and returns zero.
+--
+-- This routine allows recursive calls from the routine func providing
+-- correct values of dummy indices for each instance.
+--
+-- NOTE: The n-tuple passed to this routine must not be changed by any
+-- other routines called from the formal routine func until this
+-- routine has returned. */
+
+struct eval_domain_info
+{ /* working info used by the routine eval_within_domain */
+ DOMAIN *domain;
+ /* domain, which has to be entered */
+ DOMAIN_BLOCK *block;
+ /* domain block, which is currently processed */
+ TUPLE *tuple;
+ /* tail of original n-tuple, whose components have to be assigned
+ to free dummy indices in the current domain block */
+ void *info;
+ /* transit pointer passed to the formal routine func */
+ void (*func)(MPL *mpl, void *info);
+ /* routine, which has to be executed in the domain scope */
+ int failure;
+ /* this flag indicates that given n-tuple is not a member of the
+ domain set */
+};
+
+static void eval_domain_func(MPL *mpl, void *_my_info)
+{ /* this routine recursively enters into the domain scope and then
+ calls the routine func */
+ struct eval_domain_info *my_info = _my_info;
+ if (my_info->block != NULL)
+ { /* the current domain block to be entered exists */
+ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ TUPLE *tuple = NULL, *temp = NULL;
+ /* save pointer to the current domain block */
+ block = my_info->block;
+ /* and get ready to enter the next block (if it exists) */
+ my_info->block = block->next;
+ /* construct temporary n-tuple, whose components correspond to
+ dummy indices (slots) of the current domain; components of
+ the temporary n-tuple that correspond to free dummy indices
+ are assigned references (not values!) to symbols specified
+ in the corresponding components of the given n-tuple, while
+ other components that correspond to non-free dummy indices
+ are assigned symbolic values computed here */
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ { /* create component that corresponds to the current slot */
+ if (tuple == NULL)
+ tuple = temp = dmp_get_atom(mpl->tuples, sizeof(TUPLE));
+ else
+temp = (temp->next = dmp_get_atom(mpl->tuples, sizeof(TUPLE)));
+ if (slot->code == NULL)
+ { /* dummy index is free; take reference to symbol, which
+ is specified in the corresponding component of given
+ n-tuple */
+ xassert(my_info->tuple != NULL);
+ temp->sym = my_info->tuple->sym;
+ xassert(temp->sym != NULL);
+ my_info->tuple = my_info->tuple->next;
+ }
+ else
+ { /* dummy index is non-free; compute symbolic value to be
+ temporarily assigned to the dummy index */
+ temp->sym = eval_symbolic(mpl, slot->code);
+ }
+ }
+ temp->next = NULL;
+ /* enter the current domain block */
+ if (enter_domain_block(mpl, block, tuple, my_info,
+ eval_domain_func)) my_info->failure = 1;
+ /* delete temporary n-tuple as well as symbols that correspond
+ to non-free dummy indices (they were computed here) */
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ { xassert(tuple != NULL);
+ temp = tuple;
+ tuple = tuple->next;
+ if (slot->code != NULL)
+ { /* dummy index is non-free; delete symbolic value */
+ delete_symbol(mpl, temp->sym);
+ }
+ /* delete component that corresponds to the current slot */
+ dmp_free_atom(mpl->tuples, temp, sizeof(TUPLE));
+ }
+ }
+ else
+ { /* there are no more domain blocks, i.e. we have reached the
+ domain scope */
+ xassert(my_info->tuple == NULL);
+ /* check optional predicate specified for the domain */
+ if (my_info->domain->code != NULL && !eval_logical(mpl,
+ my_info->domain->code))
+ { /* the predicate is false */
+ my_info->failure = 2;
+ }
+ else
+ { /* the predicate is true; do the job */
+ my_info->func(mpl, my_info->info);
+ }
+ }
+ return;
+}
+
+int eval_within_domain
+( MPL *mpl,
+ DOMAIN *domain, /* not changed */
+ TUPLE *tuple, /* not changed */
+ void *info, void (*func)(MPL *mpl, void *info)
+)
+{ /* this routine performs evaluation within domain scope */
+ struct eval_domain_info _my_info, *my_info = &_my_info;
+ if (domain == NULL)
+ { xassert(tuple == NULL);
+ func(mpl, info);
+ my_info->failure = 0;
+ }
+ else
+ { xassert(tuple != NULL);
+ my_info->domain = domain;
+ my_info->block = domain->list;
+ my_info->tuple = tuple;
+ my_info->info = info;
+ my_info->func = func;
+ my_info->failure = 0;
+ /* enter the very first domain block */
+ eval_domain_func(mpl, my_info);
+ }
+ return my_info->failure;
+}
+
+/*----------------------------------------------------------------------
+-- loop_within_domain - perform iterations within domain scope.
+--
+-- This routine iteratively assigns new values (symbols) to the dummy
+-- indices of specified domain by enumerating all n-tuples, which are
+-- members of the domain set, and for every n-tuple it calls the formal
+-- routine func to evaluate some code within the domain scope.
+--
+-- If the routine func returns non-zero, enumeration within the domain
+-- is prematurely terminated.
+--
+-- For the sake of convenience it is allowed to specify domain as NULL,
+-- in which case this routine just calls the routine func only once and
+-- returns zero.
+--
+-- This routine allows recursive calls from the routine func providing
+-- correct values of dummy indices for each instance. */
+
+struct loop_domain_info
+{ /* working info used by the routine loop_within_domain */
+ DOMAIN *domain;
+ /* domain, which has to be entered */
+ DOMAIN_BLOCK *block;
+ /* domain block, which is currently processed */
+ int looping;
+ /* clearing this flag leads to terminating enumeration */
+ void *info;
+ /* transit pointer passed to the formal routine func */
+ int (*func)(MPL *mpl, void *info);
+ /* routine, which needs to be executed in the domain scope */
+};
+
+static void loop_domain_func(MPL *mpl, void *_my_info)
+{ /* this routine enumerates all n-tuples in the basic set of the
+ current domain block, enters recursively into the domain scope
+ for every n-tuple, and then calls the routine func */
+ struct loop_domain_info *my_info = _my_info;
+ if (my_info->block != NULL)
+ { /* the current domain block to be entered exists */
+ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ TUPLE *bound;
+ /* save pointer to the current domain block */
+ block = my_info->block;
+ /* and get ready to enter the next block (if it exists) */
+ my_info->block = block->next;
+ /* compute symbolic values, at which non-free dummy indices of
+ the current domain block are bound; since that values don't
+ depend on free dummy indices of the current block, they can
+ be computed once out of the enumeration loop */
+ bound = create_tuple(mpl);
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ { if (slot->code != NULL)
+ bound = expand_tuple(mpl, bound, eval_symbolic(mpl,
+ slot->code));
+ }
+ /* start enumeration */
+ xassert(block->code != NULL);
+ if (block->code->op == O_DOTS)
+ { /* the basic set is "arithmetic", in which case it doesn't
+ need to be computed explicitly */
+ TUPLE *tuple;
+ int n, j;
+ double t0, tf, dt;
+ /* compute "parameters" of the basic set */
+ t0 = eval_numeric(mpl, block->code->arg.arg.x);
+ tf = eval_numeric(mpl, block->code->arg.arg.y);
+ if (block->code->arg.arg.z == NULL)
+ dt = 1.0;
+ else
+ dt = eval_numeric(mpl, block->code->arg.arg.z);
+ /* determine cardinality of the basic set */
+ n = arelset_size(mpl, t0, tf, dt);
+ /* create dummy 1-tuple for members of the basic set */
+ tuple = expand_tuple(mpl, create_tuple(mpl),
+ create_symbol_num(mpl, 0.0));
+ /* in case of "arithmetic" set there is exactly one dummy
+ index, which cannot be non-free */
+ xassert(bound == NULL);
+ /* walk through 1-tuples of the basic set */
+ for (j = 1; j <= n && my_info->looping; j++)
+ { /* construct dummy 1-tuple for the current member */
+ tuple->sym->num = arelset_member(mpl, t0, tf, dt, j);
+ /* enter the current domain block */
+ enter_domain_block(mpl, block, tuple, my_info,
+ loop_domain_func);
+ }
+ /* delete dummy 1-tuple */
+ delete_tuple(mpl, tuple);
+ }
+ else
+ { /* the basic set is of general kind, in which case it needs
+ to be explicitly computed */
+ ELEMSET *set;
+ MEMBER *memb;
+ TUPLE *temp1, *temp2;
+ /* compute the basic set */
+ set = eval_elemset(mpl, block->code);
+ /* walk through all n-tuples of the basic set */
+ for (memb = set->head; memb != NULL && my_info->looping;
+ memb = memb->next)
+ { /* all components of the current n-tuple that correspond
+ to non-free dummy indices must be feasible; otherwise
+ the n-tuple is not in the basic set */
+ temp1 = memb->tuple;
+ temp2 = bound;
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ { xassert(temp1 != NULL);
+ if (slot->code != NULL)
+ { /* non-free dummy index */
+ xassert(temp2 != NULL);
+ if (compare_symbols(mpl, temp1->sym, temp2->sym)
+ != 0)
+ { /* the n-tuple is not in the basic set */
+ goto skip;
+ }
+ temp2 = temp2->next;
+ }
+ temp1 = temp1->next;
+ }
+ xassert(temp1 == NULL);
+ xassert(temp2 == NULL);
+ /* enter the current domain block */
+ enter_domain_block(mpl, block, memb->tuple, my_info,
+ loop_domain_func);
+skip: ;
+ }
+ /* delete the basic set */
+ delete_elemset(mpl, set);
+ }
+ /* delete symbolic values binding non-free dummy indices */
+ delete_tuple(mpl, bound);
+ /* restore pointer to the current domain block */
+ my_info->block = block;
+ }
+ else
+ { /* there are no more domain blocks, i.e. we have reached the
+ domain scope */
+ /* check optional predicate specified for the domain */
+ if (my_info->domain->code != NULL && !eval_logical(mpl,
+ my_info->domain->code))
+ { /* the predicate is false */
+ /* nop */;
+ }
+ else
+ { /* the predicate is true; do the job */
+ my_info->looping = !my_info->func(mpl, my_info->info);
+ }
+ }
+ return;
+}
+
+void loop_within_domain
+( MPL *mpl,
+ DOMAIN *domain, /* not changed */
+ void *info, int (*func)(MPL *mpl, void *info)
+)
+{ /* this routine performs iterations within domain scope */
+ struct loop_domain_info _my_info, *my_info = &_my_info;
+ if (domain == NULL)
+ func(mpl, info);
+ else
+ { my_info->domain = domain;
+ my_info->block = domain->list;
+ my_info->looping = 1;
+ my_info->info = info;
+ my_info->func = func;
+ /* enter the very first domain block */
+ loop_domain_func(mpl, my_info);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- out_of_domain - raise domain exception.
+--
+-- This routine is called when a reference is made to a member of some
+-- model object, but its n-tuple is out of the object domain. */
+
+void out_of_domain
+( MPL *mpl,
+ char *name, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ xassert(name != NULL);
+ xassert(tuple != NULL);
+ error(mpl, "%s%s out of domain", name, format_tuple(mpl, '[',
+ tuple));
+ /* no return */
+}
+
+/*----------------------------------------------------------------------
+-- get_domain_tuple - obtain current n-tuple from domain.
+--
+-- This routine constructs n-tuple, whose components are current values
+-- assigned to *free* dummy indices of specified domain.
+--
+-- For the sake of convenience it is allowed to specify domain as NULL,
+-- in which case this routine returns 0-tuple.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+TUPLE *get_domain_tuple
+( MPL *mpl,
+ DOMAIN *domain /* not changed */
+)
+{ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ TUPLE *tuple;
+ tuple = create_tuple(mpl);
+ if (domain != NULL)
+ { for (block = domain->list; block != NULL; block = block->next)
+ { for (slot = block->list; slot != NULL; slot = slot->next)
+ { if (slot->code == NULL)
+ { xassert(slot->value != NULL);
+ tuple = expand_tuple(mpl, tuple, copy_symbol(mpl,
+ slot->value));
+ }
+ }
+ }
+ }
+ return tuple;
+}
+
+/*----------------------------------------------------------------------
+-- clean_domain - clean domain.
+--
+-- This routine cleans specified domain that assumes deleting all stuff
+-- dynamically allocated during the generation phase. */
+
+void clean_domain(MPL *mpl, DOMAIN *domain)
+{ DOMAIN_BLOCK *block;
+ DOMAIN_SLOT *slot;
+ /* if no domain is specified, do nothing */
+ if (domain == NULL) goto done;
+ /* clean all domain blocks */
+ for (block = domain->list; block != NULL; block = block->next)
+ { /* clean all domain slots */
+ for (slot = block->list; slot != NULL; slot = slot->next)
+ { /* clean pseudo-code for computing bound value */
+ clean_code(mpl, slot->code);
+ /* delete symbolic value assigned to dummy index */
+ if (slot->value != NULL)
+ delete_symbol(mpl, slot->value), slot->value = NULL;
+ }
+ /* clean pseudo-code for computing basic set */
+ clean_code(mpl, block->code);
+ }
+ /* clean pseudo-code for computing domain predicate */
+ clean_code(mpl, domain->code);
+done: return;
+}
+
+/**********************************************************************/
+/* * * MODEL SETS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- check_elem_set - check elemental set assigned to set member.
+--
+-- This routine checks if given elemental set being assigned to member
+-- of specified model set satisfies to all restrictions.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+void check_elem_set
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple, /* not changed */
+ ELEMSET *refer /* not changed */
+)
+{ WITHIN *within;
+ MEMBER *memb;
+ int eqno;
+ /* elemental set must be within all specified supersets */
+ for (within = set->within, eqno = 1; within != NULL; within =
+ within->next, eqno++)
+ { xassert(within->code != NULL);
+ for (memb = refer->head; memb != NULL; memb = memb->next)
+ { if (!is_member(mpl, within->code, memb->tuple))
+ { char buf[255+1];
+ strcpy(buf, format_tuple(mpl, '(', memb->tuple));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s contains %s which not within specified "
+ "set; see (%d)", set->name, format_tuple(mpl, '[',
+ tuple), buf, eqno);
+ }
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- take_member_set - obtain elemental set assigned to set member.
+--
+-- This routine obtains a reference to elemental set assigned to given
+-- member of specified model set and returns it on exit.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+ELEMSET *take_member_set /* returns reference, not value */
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ ELEMSET *refer;
+ /* find member in the set array */
+ memb = find_member(mpl, set->array, tuple);
+ if (memb != NULL)
+ { /* member exists, so just take the reference */
+ refer = memb->value.set;
+ }
+ else if (set->assign != NULL)
+ { /* compute value using assignment expression */
+ refer = eval_elemset(mpl, set->assign);
+add: /* check that the elemental set satisfies to all restrictions,
+ assign it to new member, and add the member to the array */
+ check_elem_set(mpl, set, tuple, refer);
+ memb = add_member(mpl, set->array, copy_tuple(mpl, tuple));
+ memb->value.set = refer;
+ }
+ else if (set->option != NULL)
+ { /* compute default elemental set */
+ refer = eval_elemset(mpl, set->option);
+ goto add;
+ }
+ else
+ { /* no value (elemental set) is provided */
+ error(mpl, "no value for %s%s", set->name, format_tuple(mpl,
+ '[', tuple));
+ }
+ return refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_member_set - evaluate elemental set assigned to set member.
+--
+-- This routine evaluates a reference to elemental set assigned to given
+-- member of specified model set and returns it on exit. */
+
+struct eval_set_info
+{ /* working info used by the routine eval_member_set */
+ SET *set;
+ /* model set */
+ TUPLE *tuple;
+ /* n-tuple, which defines set member */
+ MEMBER *memb;
+ /* normally this pointer is NULL; the routine uses this pointer
+ to check data provided in the data section, in which case it
+ points to a member currently checked; this check is performed
+ automatically only once when a reference to any member occurs
+ for the first time */
+ ELEMSET *refer;
+ /* evaluated reference to elemental set */
+};
+
+static void eval_set_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine to work within domain scope */
+ struct eval_set_info *info = _info;
+ if (info->memb != NULL)
+ { /* checking call; check elemental set being assigned */
+ check_elem_set(mpl, info->set, info->memb->tuple,
+ info->memb->value.set);
+ }
+ else
+ { /* normal call; evaluate member, which has given n-tuple */
+ info->refer = take_member_set(mpl, info->set, info->tuple);
+ }
+ return;
+}
+
+#if 1 /* 12/XII-2008 */
+static void saturate_set(MPL *mpl, SET *set)
+{ GADGET *gadget = set->gadget;
+ ELEMSET *data;
+ MEMBER *elem, *memb;
+ TUPLE *tuple, *work[20];
+ int i;
+ xprintf("Generating %s...\n", set->name);
+ eval_whole_set(mpl, gadget->set);
+ /* gadget set must have exactly one member */
+ xassert(gadget->set->array != NULL);
+ xassert(gadget->set->array->head != NULL);
+ xassert(gadget->set->array->head == gadget->set->array->tail);
+ data = gadget->set->array->head->value.set;
+ xassert(data->type == A_NONE);
+ xassert(data->dim == gadget->set->dimen);
+ /* walk thru all elements of the plain set */
+ for (elem = data->head; elem != NULL; elem = elem->next)
+ { /* create a copy of n-tuple */
+ tuple = copy_tuple(mpl, elem->tuple);
+ /* rearrange component of the n-tuple */
+ for (i = 0; i < gadget->set->dimen; i++)
+ work[i] = NULL;
+ for (i = 0; tuple != NULL; tuple = tuple->next)
+ work[gadget->ind[i++]-1] = tuple;
+ xassert(i == gadget->set->dimen);
+ for (i = 0; i < gadget->set->dimen; i++)
+ { xassert(work[i] != NULL);
+ work[i]->next = work[i+1];
+ }
+ /* construct subscript list from first set->dim components */
+ if (set->dim == 0)
+ tuple = NULL;
+ else
+ tuple = work[0], work[set->dim-1]->next = NULL;
+ /* find corresponding member of the set to be initialized */
+ memb = find_member(mpl, set->array, tuple);
+ if (memb == NULL)
+ { /* not found; add new member to the set and assign it empty
+ elemental set */
+ memb = add_member(mpl, set->array, tuple);
+ memb->value.set = create_elemset(mpl, set->dimen);
+ }
+ else
+ { /* found; free subscript list */
+ delete_tuple(mpl, tuple);
+ }
+ /* construct new n-tuple from rest set->dimen components */
+ tuple = work[set->dim];
+ xassert(set->dim + set->dimen == gadget->set->dimen);
+ work[gadget->set->dimen-1]->next = NULL;
+ /* and add it to the elemental set assigned to the member
+ (no check for duplicates is needed) */
+ add_tuple(mpl, memb->value.set, tuple);
+ }
+ /* the set has been saturated with data */
+ set->data = 1;
+ return;
+}
+#endif
+
+ELEMSET *eval_member_set /* returns reference, not value */
+( MPL *mpl,
+ SET *set, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ /* this routine evaluates set member */
+ struct eval_set_info _info, *info = &_info;
+ xassert(set->dim == tuple_dimen(mpl, tuple));
+ info->set = set;
+ info->tuple = tuple;
+#if 1 /* 12/XII-2008 */
+ if (set->gadget != NULL && set->data == 0)
+ { /* initialize the set with data from a plain set */
+ saturate_set(mpl, set);
+ }
+#endif
+ if (set->data == 1)
+ { /* check data, which are provided in the data section, but not
+ checked yet */
+ /* save pointer to the last array member; note that during the
+ check new members may be added beyond the last member due to
+ references to the same parameter from default expression as
+ well as from expressions that define restricting supersets;
+ however, values assigned to the new members will be checked
+ by other routine, so we don't need to check them here */
+ MEMBER *tail = set->array->tail;
+ /* change the data status to prevent infinite recursive loop
+ due to references to the same set during the check */
+ set->data = 2;
+ /* check elemental sets assigned to array members in the data
+ section until the marked member has been reached */
+ for (info->memb = set->array->head; info->memb != NULL;
+ info->memb = info->memb->next)
+ { if (eval_within_domain(mpl, set->domain, info->memb->tuple,
+ info, eval_set_func))
+ out_of_domain(mpl, set->name, info->memb->tuple);
+ if (info->memb == tail) break;
+ }
+ /* the check has been finished */
+ }
+ /* evaluate member, which has given n-tuple */
+ info->memb = NULL;
+ if (eval_within_domain(mpl, info->set->domain, info->tuple, info,
+ eval_set_func))
+ out_of_domain(mpl, set->name, info->tuple);
+ /* bring evaluated reference to the calling program */
+ return info->refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_whole_set - evaluate model set over entire domain.
+--
+-- This routine evaluates all members of specified model set over entire
+-- domain. */
+
+static int whole_set_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ SET *set = (SET *)info;
+ TUPLE *tuple = get_domain_tuple(mpl, set->domain);
+ eval_member_set(mpl, set, tuple);
+ delete_tuple(mpl, tuple);
+ return 0;
+}
+
+void eval_whole_set(MPL *mpl, SET *set)
+{ loop_within_domain(mpl, set->domain, set, whole_set_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean set - clean model set.
+--
+-- This routine cleans specified model set that assumes deleting all
+-- stuff dynamically allocated during the generation phase. */
+
+void clean_set(MPL *mpl, SET *set)
+{ WITHIN *within;
+ MEMBER *memb;
+ /* clean subscript domain */
+ clean_domain(mpl, set->domain);
+ /* clean pseudo-code for computing supersets */
+ for (within = set->within; within != NULL; within = within->next)
+ clean_code(mpl, within->code);
+ /* clean pseudo-code for computing assigned value */
+ clean_code(mpl, set->assign);
+ /* clean pseudo-code for computing default value */
+ clean_code(mpl, set->option);
+ /* reset data status flag */
+ set->data = 0;
+ /* delete content array */
+ for (memb = set->array->head; memb != NULL; memb = memb->next)
+ delete_value(mpl, set->array->type, &memb->value);
+ delete_array(mpl, set->array), set->array = NULL;
+ return;
+}
+
+/**********************************************************************/
+/* * * MODEL PARAMETERS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- check_value_num - check numeric value assigned to parameter member.
+--
+-- This routine checks if numeric value being assigned to some member
+-- of specified numeric model parameter satisfies to all restrictions.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+void check_value_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple, /* not changed */
+ double value
+)
+{ CONDITION *cond;
+ WITHIN *in;
+ int eqno;
+ /* the value must satisfy to the parameter type */
+ switch (par->type)
+ { case A_NUMERIC:
+ break;
+ case A_INTEGER:
+ if (value != floor(value))
+ error(mpl, "%s%s = %.*g not integer", par->name,
+ format_tuple(mpl, '[', tuple), DBL_DIG, value);
+ break;
+ case A_BINARY:
+ if (!(value == 0.0 || value == 1.0))
+ error(mpl, "%s%s = %.*g not binary", par->name,
+ format_tuple(mpl, '[', tuple), DBL_DIG, value);
+ break;
+ default:
+ xassert(par != par);
+ }
+ /* the value must satisfy to all specified conditions */
+ for (cond = par->cond, eqno = 1; cond != NULL; cond = cond->next,
+ eqno++)
+ { double bound;
+ char *rho;
+ xassert(cond->code != NULL);
+ bound = eval_numeric(mpl, cond->code);
+ switch (cond->rho)
+ { case O_LT:
+ if (!(value < bound))
+ { rho = "<";
+err: error(mpl, "%s%s = %.*g not %s %.*g; see (%d)",
+ par->name, format_tuple(mpl, '[', tuple), DBL_DIG,
+ value, rho, DBL_DIG, bound, eqno);
+ }
+ break;
+ case O_LE:
+ if (!(value <= bound)) { rho = "<="; goto err; }
+ break;
+ case O_EQ:
+ if (!(value == bound)) { rho = "="; goto err; }
+ break;
+ case O_GE:
+ if (!(value >= bound)) { rho = ">="; goto err; }
+ break;
+ case O_GT:
+ if (!(value > bound)) { rho = ">"; goto err; }
+ break;
+ case O_NE:
+ if (!(value != bound)) { rho = "<>"; goto err; }
+ break;
+ default:
+ xassert(cond != cond);
+ }
+ }
+ /* the value must be in all specified supersets */
+ for (in = par->in, eqno = 1; in != NULL; in = in->next, eqno++)
+ { TUPLE *dummy;
+ xassert(in->code != NULL);
+ xassert(in->code->dim == 1);
+ dummy = expand_tuple(mpl, create_tuple(mpl),
+ create_symbol_num(mpl, value));
+ if (!is_member(mpl, in->code, dummy))
+ error(mpl, "%s%s = %.*g not in specified set; see (%d)",
+ par->name, format_tuple(mpl, '[', tuple), DBL_DIG,
+ value, eqno);
+ delete_tuple(mpl, dummy);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- take_member_num - obtain num. value assigned to parameter member.
+--
+-- This routine obtains a numeric value assigned to member of specified
+-- numeric model parameter and returns it on exit.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+double take_member_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ double value;
+ /* find member in the parameter array */
+ memb = find_member(mpl, par->array, tuple);
+ if (memb != NULL)
+ { /* member exists, so just take its value */
+ value = memb->value.num;
+ }
+ else if (par->assign != NULL)
+ { /* compute value using assignment expression */
+ value = eval_numeric(mpl, par->assign);
+add: /* check that the value satisfies to all restrictions, assign
+ it to new member, and add the member to the array */
+ check_value_num(mpl, par, tuple, value);
+ memb = add_member(mpl, par->array, copy_tuple(mpl, tuple));
+ memb->value.num = value;
+ }
+ else if (par->option != NULL)
+ { /* compute default value */
+ value = eval_numeric(mpl, par->option);
+ goto add;
+ }
+ else if (par->defval != NULL)
+ { /* take default value provided in the data section */
+ if (par->defval->str != NULL)
+ error(mpl, "cannot convert %s to floating-point number",
+ format_symbol(mpl, par->defval));
+ value = par->defval->num;
+ goto add;
+ }
+ else
+ { /* no value is provided */
+ error(mpl, "no value for %s%s", par->name, format_tuple(mpl,
+ '[', tuple));
+ }
+ return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_member_num - evaluate num. value assigned to parameter member.
+--
+-- This routine evaluates a numeric value assigned to given member of
+-- specified numeric model parameter and returns it on exit. */
+
+struct eval_num_info
+{ /* working info used by the routine eval_member_num */
+ PARAMETER *par;
+ /* model parameter */
+ TUPLE *tuple;
+ /* n-tuple, which defines parameter member */
+ MEMBER *memb;
+ /* normally this pointer is NULL; the routine uses this pointer
+ to check data provided in the data section, in which case it
+ points to a member currently checked; this check is performed
+ automatically only once when a reference to any member occurs
+ for the first time */
+ double value;
+ /* evaluated numeric value */
+};
+
+static void eval_num_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine to work within domain scope */
+ struct eval_num_info *info = _info;
+ if (info->memb != NULL)
+ { /* checking call; check numeric value being assigned */
+ check_value_num(mpl, info->par, info->memb->tuple,
+ info->memb->value.num);
+ }
+ else
+ { /* normal call; evaluate member, which has given n-tuple */
+ info->value = take_member_num(mpl, info->par, info->tuple);
+ }
+ return;
+}
+
+double eval_member_num
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ /* this routine evaluates numeric parameter member */
+ struct eval_num_info _info, *info = &_info;
+ xassert(par->type == A_NUMERIC || par->type == A_INTEGER ||
+ par->type == A_BINARY);
+ xassert(par->dim == tuple_dimen(mpl, tuple));
+ info->par = par;
+ info->tuple = tuple;
+ if (par->data == 1)
+ { /* check data, which are provided in the data section, but not
+ checked yet */
+ /* save pointer to the last array member; note that during the
+ check new members may be added beyond the last member due to
+ references to the same parameter from default expression as
+ well as from expressions that define restricting conditions;
+ however, values assigned to the new members will be checked
+ by other routine, so we don't need to check them here */
+ MEMBER *tail = par->array->tail;
+ /* change the data status to prevent infinite recursive loop
+ due to references to the same parameter during the check */
+ par->data = 2;
+ /* check values assigned to array members in the data section
+ until the marked member has been reached */
+ for (info->memb = par->array->head; info->memb != NULL;
+ info->memb = info->memb->next)
+ { if (eval_within_domain(mpl, par->domain, info->memb->tuple,
+ info, eval_num_func))
+ out_of_domain(mpl, par->name, info->memb->tuple);
+ if (info->memb == tail) break;
+ }
+ /* the check has been finished */
+ }
+ /* evaluate member, which has given n-tuple */
+ info->memb = NULL;
+ if (eval_within_domain(mpl, info->par->domain, info->tuple, info,
+ eval_num_func))
+ out_of_domain(mpl, par->name, info->tuple);
+ /* bring evaluated value to the calling program */
+ return info->value;
+}
+
+/*----------------------------------------------------------------------
+-- check_value_sym - check symbolic value assigned to parameter member.
+--
+-- This routine checks if symbolic value being assigned to some member
+-- of specified symbolic model parameter satisfies to all restrictions.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+void check_value_sym
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple, /* not changed */
+ SYMBOL *value /* not changed */
+)
+{ CONDITION *cond;
+ WITHIN *in;
+ int eqno;
+ /* the value must satisfy to all specified conditions */
+ for (cond = par->cond, eqno = 1; cond != NULL; cond = cond->next,
+ eqno++)
+ { SYMBOL *bound;
+ char buf[255+1];
+ xassert(cond->code != NULL);
+ bound = eval_symbolic(mpl, cond->code);
+ switch (cond->rho)
+ {
+#if 1 /* 13/VIII-2008 */
+ case O_LT:
+ if (!(compare_symbols(mpl, value, bound) < 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not < %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+ case O_LE:
+ if (!(compare_symbols(mpl, value, bound) <= 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not <= %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+#endif
+ case O_EQ:
+ if (!(compare_symbols(mpl, value, bound) == 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not = %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+#if 1 /* 13/VIII-2008 */
+ case O_GE:
+ if (!(compare_symbols(mpl, value, bound) >= 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not >= %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+ case O_GT:
+ if (!(compare_symbols(mpl, value, bound) > 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not > %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+#endif
+ case O_NE:
+ if (!(compare_symbols(mpl, value, bound) != 0))
+ { strcpy(buf, format_symbol(mpl, bound));
+ xassert(strlen(buf) < sizeof(buf));
+ error(mpl, "%s%s = %s not <> %s",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), buf, eqno);
+ }
+ break;
+ default:
+ xassert(cond != cond);
+ }
+ delete_symbol(mpl, bound);
+ }
+ /* the value must be in all specified supersets */
+ for (in = par->in, eqno = 1; in != NULL; in = in->next, eqno++)
+ { TUPLE *dummy;
+ xassert(in->code != NULL);
+ xassert(in->code->dim == 1);
+ dummy = expand_tuple(mpl, create_tuple(mpl), copy_symbol(mpl,
+ value));
+ if (!is_member(mpl, in->code, dummy))
+ error(mpl, "%s%s = %s not in specified set; see (%d)",
+ par->name, format_tuple(mpl, '[', tuple),
+ format_symbol(mpl, value), eqno);
+ delete_tuple(mpl, dummy);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- take_member_sym - obtain symb. value assigned to parameter member.
+--
+-- This routine obtains a symbolic value assigned to member of specified
+-- symbolic model parameter and returns it on exit.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+SYMBOL *take_member_sym /* returns value, not reference */
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ SYMBOL *value;
+ /* find member in the parameter array */
+ memb = find_member(mpl, par->array, tuple);
+ if (memb != NULL)
+ { /* member exists, so just take its value */
+ value = copy_symbol(mpl, memb->value.sym);
+ }
+ else if (par->assign != NULL)
+ { /* compute value using assignment expression */
+ value = eval_symbolic(mpl, par->assign);
+add: /* check that the value satisfies to all restrictions, assign
+ it to new member, and add the member to the array */
+ check_value_sym(mpl, par, tuple, value);
+ memb = add_member(mpl, par->array, copy_tuple(mpl, tuple));
+ memb->value.sym = copy_symbol(mpl, value);
+ }
+ else if (par->option != NULL)
+ { /* compute default value */
+ value = eval_symbolic(mpl, par->option);
+ goto add;
+ }
+ else if (par->defval != NULL)
+ { /* take default value provided in the data section */
+ value = copy_symbol(mpl, par->defval);
+ goto add;
+ }
+ else
+ { /* no value is provided */
+ error(mpl, "no value for %s%s", par->name, format_tuple(mpl,
+ '[', tuple));
+ }
+ return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_member_sym - evaluate symb. value assigned to parameter member.
+--
+-- This routine evaluates a symbolic value assigned to given member of
+-- specified symbolic model parameter and returns it on exit. */
+
+struct eval_sym_info
+{ /* working info used by the routine eval_member_sym */
+ PARAMETER *par;
+ /* model parameter */
+ TUPLE *tuple;
+ /* n-tuple, which defines parameter member */
+ MEMBER *memb;
+ /* normally this pointer is NULL; the routine uses this pointer
+ to check data provided in the data section, in which case it
+ points to a member currently checked; this check is performed
+ automatically only once when a reference to any member occurs
+ for the first time */
+ SYMBOL *value;
+ /* evaluated symbolic value */
+};
+
+static void eval_sym_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine to work within domain scope */
+ struct eval_sym_info *info = _info;
+ if (info->memb != NULL)
+ { /* checking call; check symbolic value being assigned */
+ check_value_sym(mpl, info->par, info->memb->tuple,
+ info->memb->value.sym);
+ }
+ else
+ { /* normal call; evaluate member, which has given n-tuple */
+ info->value = take_member_sym(mpl, info->par, info->tuple);
+ }
+ return;
+}
+
+SYMBOL *eval_member_sym /* returns value, not reference */
+( MPL *mpl,
+ PARAMETER *par, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ /* this routine evaluates symbolic parameter member */
+ struct eval_sym_info _info, *info = &_info;
+ xassert(par->type == A_SYMBOLIC);
+ xassert(par->dim == tuple_dimen(mpl, tuple));
+ info->par = par;
+ info->tuple = tuple;
+ if (par->data == 1)
+ { /* check data, which are provided in the data section, but not
+ checked yet */
+ /* save pointer to the last array member; note that during the
+ check new members may be added beyond the last member due to
+ references to the same parameter from default expression as
+ well as from expressions that define restricting conditions;
+ however, values assigned to the new members will be checked
+ by other routine, so we don't need to check them here */
+ MEMBER *tail = par->array->tail;
+ /* change the data status to prevent infinite recursive loop
+ due to references to the same parameter during the check */
+ par->data = 2;
+ /* check values assigned to array members in the data section
+ until the marked member has been reached */
+ for (info->memb = par->array->head; info->memb != NULL;
+ info->memb = info->memb->next)
+ { if (eval_within_domain(mpl, par->domain, info->memb->tuple,
+ info, eval_sym_func))
+ out_of_domain(mpl, par->name, info->memb->tuple);
+ if (info->memb == tail) break;
+ }
+ /* the check has been finished */
+ }
+ /* evaluate member, which has given n-tuple */
+ info->memb = NULL;
+ if (eval_within_domain(mpl, info->par->domain, info->tuple, info,
+ eval_sym_func))
+ out_of_domain(mpl, par->name, info->tuple);
+ /* bring evaluated value to the calling program */
+ return info->value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_whole_par - evaluate model parameter over entire domain.
+--
+-- This routine evaluates all members of specified model parameter over
+-- entire domain. */
+
+static int whole_par_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ PARAMETER *par = (PARAMETER *)info;
+ TUPLE *tuple = get_domain_tuple(mpl, par->domain);
+ switch (par->type)
+ { case A_NUMERIC:
+ case A_INTEGER:
+ case A_BINARY:
+ eval_member_num(mpl, par, tuple);
+ break;
+ case A_SYMBOLIC:
+ delete_symbol(mpl, eval_member_sym(mpl, par, tuple));
+ break;
+ default:
+ xassert(par != par);
+ }
+ delete_tuple(mpl, tuple);
+ return 0;
+}
+
+void eval_whole_par(MPL *mpl, PARAMETER *par)
+{ loop_within_domain(mpl, par->domain, par, whole_par_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_parameter - clean model parameter.
+--
+-- This routine cleans specified model parameter that assumes deleting
+-- all stuff dynamically allocated during the generation phase. */
+
+void clean_parameter(MPL *mpl, PARAMETER *par)
+{ CONDITION *cond;
+ WITHIN *in;
+ MEMBER *memb;
+ /* clean subscript domain */
+ clean_domain(mpl, par->domain);
+ /* clean pseudo-code for computing restricting conditions */
+ for (cond = par->cond; cond != NULL; cond = cond->next)
+ clean_code(mpl, cond->code);
+ /* clean pseudo-code for computing restricting supersets */
+ for (in = par->in; in != NULL; in = in->next)
+ clean_code(mpl, in->code);
+ /* clean pseudo-code for computing assigned value */
+ clean_code(mpl, par->assign);
+ /* clean pseudo-code for computing default value */
+ clean_code(mpl, par->option);
+ /* reset data status flag */
+ par->data = 0;
+ /* delete default symbolic value */
+ if (par->defval != NULL)
+ delete_symbol(mpl, par->defval), par->defval = NULL;
+ /* delete content array */
+ for (memb = par->array->head; memb != NULL; memb = memb->next)
+ delete_value(mpl, par->array->type, &memb->value);
+ delete_array(mpl, par->array), par->array = NULL;
+ return;
+}
+
+/**********************************************************************/
+/* * * MODEL VARIABLES * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- take_member_var - obtain reference to elemental variable.
+--
+-- This routine obtains a reference to elemental variable assigned to
+-- given member of specified model variable and returns it on exit. If
+-- necessary, new elemental variable is created.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+ELEMVAR *take_member_var /* returns reference */
+( MPL *mpl,
+ VARIABLE *var, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ ELEMVAR *refer;
+ /* find member in the variable array */
+ memb = find_member(mpl, var->array, tuple);
+ if (memb != NULL)
+ { /* member exists, so just take the reference */
+ refer = memb->value.var;
+ }
+ else
+ { /* member is referenced for the first time and therefore does
+ not exist; create new elemental variable, assign it to new
+ member, and add the member to the variable array */
+ memb = add_member(mpl, var->array, copy_tuple(mpl, tuple));
+ refer = (memb->value.var =
+ dmp_get_atom(mpl->elemvars, sizeof(ELEMVAR)));
+ refer->j = 0;
+ refer->var = var;
+ refer->memb = memb;
+ /* compute lower bound */
+ if (var->lbnd == NULL)
+ refer->lbnd = 0.0;
+ else
+ refer->lbnd = eval_numeric(mpl, var->lbnd);
+ /* compute upper bound */
+ if (var->ubnd == NULL)
+ refer->ubnd = 0.0;
+ else if (var->ubnd == var->lbnd)
+ refer->ubnd = refer->lbnd;
+ else
+ refer->ubnd = eval_numeric(mpl, var->ubnd);
+ /* nullify working quantity */
+ refer->temp = 0.0;
+#if 1 /* 15/V-2010 */
+ /* solution has not been obtained by the solver yet */
+ refer->stat = 0;
+ refer->prim = refer->dual = 0.0;
+#endif
+ }
+ return refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_member_var - evaluate reference to elemental variable.
+--
+-- This routine evaluates a reference to elemental variable assigned to
+-- member of specified model variable and returns it on exit. */
+
+struct eval_var_info
+{ /* working info used by the routine eval_member_var */
+ VARIABLE *var;
+ /* model variable */
+ TUPLE *tuple;
+ /* n-tuple, which defines variable member */
+ ELEMVAR *refer;
+ /* evaluated reference to elemental variable */
+};
+
+static void eval_var_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine to work within domain scope */
+ struct eval_var_info *info = _info;
+ info->refer = take_member_var(mpl, info->var, info->tuple);
+ return;
+}
+
+ELEMVAR *eval_member_var /* returns reference */
+( MPL *mpl,
+ VARIABLE *var, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ /* this routine evaluates variable member */
+ struct eval_var_info _info, *info = &_info;
+ xassert(var->dim == tuple_dimen(mpl, tuple));
+ info->var = var;
+ info->tuple = tuple;
+ /* evaluate member, which has given n-tuple */
+ if (eval_within_domain(mpl, info->var->domain, info->tuple, info,
+ eval_var_func))
+ out_of_domain(mpl, var->name, info->tuple);
+ /* bring evaluated reference to the calling program */
+ return info->refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_whole_var - evaluate model variable over entire domain.
+--
+-- This routine evaluates all members of specified model variable over
+-- entire domain. */
+
+static int whole_var_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ VARIABLE *var = (VARIABLE *)info;
+ TUPLE *tuple = get_domain_tuple(mpl, var->domain);
+ eval_member_var(mpl, var, tuple);
+ delete_tuple(mpl, tuple);
+ return 0;
+}
+
+void eval_whole_var(MPL *mpl, VARIABLE *var)
+{ loop_within_domain(mpl, var->domain, var, whole_var_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_variable - clean model variable.
+--
+-- This routine cleans specified model variable that assumes deleting
+-- all stuff dynamically allocated during the generation phase. */
+
+void clean_variable(MPL *mpl, VARIABLE *var)
+{ MEMBER *memb;
+ /* clean subscript domain */
+ clean_domain(mpl, var->domain);
+ /* clean code for computing lower bound */
+ clean_code(mpl, var->lbnd);
+ /* clean code for computing upper bound */
+ if (var->ubnd != var->lbnd) clean_code(mpl, var->ubnd);
+ /* delete content array */
+ for (memb = var->array->head; memb != NULL; memb = memb->next)
+ dmp_free_atom(mpl->elemvars, memb->value.var, sizeof(ELEMVAR));
+ delete_array(mpl, var->array), var->array = NULL;
+ return;
+}
+
+/**********************************************************************/
+/* * * MODEL CONSTRAINTS AND OBJECTIVES * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- take_member_con - obtain reference to elemental constraint.
+--
+-- This routine obtains a reference to elemental constraint assigned
+-- to given member of specified model constraint and returns it on exit.
+-- If necessary, new elemental constraint is created.
+--
+-- NOTE: This routine must not be called out of domain scope. */
+
+ELEMCON *take_member_con /* returns reference */
+( MPL *mpl,
+ CONSTRAINT *con, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ MEMBER *memb;
+ ELEMCON *refer;
+ /* find member in the constraint array */
+ memb = find_member(mpl, con->array, tuple);
+ if (memb != NULL)
+ { /* member exists, so just take the reference */
+ refer = memb->value.con;
+ }
+ else
+ { /* member is referenced for the first time and therefore does
+ not exist; create new elemental constraint, assign it to new
+ member, and add the member to the constraint array */
+ memb = add_member(mpl, con->array, copy_tuple(mpl, tuple));
+ refer = (memb->value.con =
+ dmp_get_atom(mpl->elemcons, sizeof(ELEMCON)));
+ refer->i = 0;
+ refer->con = con;
+ refer->memb = memb;
+ /* compute linear form */
+ xassert(con->code != NULL);
+ refer->form = eval_formula(mpl, con->code);
+ /* compute lower and upper bounds */
+ if (con->lbnd == NULL && con->ubnd == NULL)
+ { /* objective has no bounds */
+ double temp;
+ xassert(con->type == A_MINIMIZE || con->type == A_MAXIMIZE);
+ /* carry the constant term to the right-hand side */
+ refer->form = remove_constant(mpl, refer->form, &temp);
+ refer->lbnd = refer->ubnd = - temp;
+ }
+ else if (con->lbnd != NULL && con->ubnd == NULL)
+ { /* constraint a * x + b >= c * y + d is transformed to the
+ standard form a * x - c * y >= d - b */
+ double temp;
+ xassert(con->type == A_CONSTRAINT);
+ refer->form = linear_comb(mpl,
+ +1.0, refer->form,
+ -1.0, eval_formula(mpl, con->lbnd));
+ refer->form = remove_constant(mpl, refer->form, &temp);
+ refer->lbnd = - temp;
+ refer->ubnd = 0.0;
+ }
+ else if (con->lbnd == NULL && con->ubnd != NULL)
+ { /* constraint a * x + b <= c * y + d is transformed to the
+ standard form a * x - c * y <= d - b */
+ double temp;
+ xassert(con->type == A_CONSTRAINT);
+ refer->form = linear_comb(mpl,
+ +1.0, refer->form,
+ -1.0, eval_formula(mpl, con->ubnd));
+ refer->form = remove_constant(mpl, refer->form, &temp);
+ refer->lbnd = 0.0;
+ refer->ubnd = - temp;
+ }
+ else if (con->lbnd == con->ubnd)
+ { /* constraint a * x + b = c * y + d is transformed to the
+ standard form a * x - c * y = d - b */
+ double temp;
+ xassert(con->type == A_CONSTRAINT);
+ refer->form = linear_comb(mpl,
+ +1.0, refer->form,
+ -1.0, eval_formula(mpl, con->lbnd));
+ refer->form = remove_constant(mpl, refer->form, &temp);
+ refer->lbnd = refer->ubnd = - temp;
+ }
+ else
+ { /* ranged constraint c <= a * x + b <= d is transformed to
+ the standard form c - b <= a * x <= d - b */
+ double temp, temp1, temp2;
+ xassert(con->type == A_CONSTRAINT);
+ refer->form = remove_constant(mpl, refer->form, &temp);
+ xassert(remove_constant(mpl, eval_formula(mpl, con->lbnd),
+ &temp1) == NULL);
+ xassert(remove_constant(mpl, eval_formula(mpl, con->ubnd),
+ &temp2) == NULL);
+ refer->lbnd = fp_sub(mpl, temp1, temp);
+ refer->ubnd = fp_sub(mpl, temp2, temp);
+ }
+#if 1 /* 15/V-2010 */
+ /* solution has not been obtained by the solver yet */
+ refer->stat = 0;
+ refer->prim = refer->dual = 0.0;
+#endif
+ }
+ return refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_member_con - evaluate reference to elemental constraint.
+--
+-- This routine evaluates a reference to elemental constraint assigned
+-- to member of specified model constraint and returns it on exit. */
+
+struct eval_con_info
+{ /* working info used by the routine eval_member_con */
+ CONSTRAINT *con;
+ /* model constraint */
+ TUPLE *tuple;
+ /* n-tuple, which defines constraint member */
+ ELEMCON *refer;
+ /* evaluated reference to elemental constraint */
+};
+
+static void eval_con_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine to work within domain scope */
+ struct eval_con_info *info = _info;
+ info->refer = take_member_con(mpl, info->con, info->tuple);
+ return;
+}
+
+ELEMCON *eval_member_con /* returns reference */
+( MPL *mpl,
+ CONSTRAINT *con, /* not changed */
+ TUPLE *tuple /* not changed */
+)
+{ /* this routine evaluates constraint member */
+ struct eval_con_info _info, *info = &_info;
+ xassert(con->dim == tuple_dimen(mpl, tuple));
+ info->con = con;
+ info->tuple = tuple;
+ /* evaluate member, which has given n-tuple */
+ if (eval_within_domain(mpl, info->con->domain, info->tuple, info,
+ eval_con_func))
+ out_of_domain(mpl, con->name, info->tuple);
+ /* bring evaluated reference to the calling program */
+ return info->refer;
+}
+
+/*----------------------------------------------------------------------
+-- eval_whole_con - evaluate model constraint over entire domain.
+--
+-- This routine evaluates all members of specified model constraint over
+-- entire domain. */
+
+static int whole_con_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ CONSTRAINT *con = (CONSTRAINT *)info;
+ TUPLE *tuple = get_domain_tuple(mpl, con->domain);
+ eval_member_con(mpl, con, tuple);
+ delete_tuple(mpl, tuple);
+ return 0;
+}
+
+void eval_whole_con(MPL *mpl, CONSTRAINT *con)
+{ loop_within_domain(mpl, con->domain, con, whole_con_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_constraint - clean model constraint.
+--
+-- This routine cleans specified model constraint that assumes deleting
+-- all stuff dynamically allocated during the generation phase. */
+
+void clean_constraint(MPL *mpl, CONSTRAINT *con)
+{ MEMBER *memb;
+ /* clean subscript domain */
+ clean_domain(mpl, con->domain);
+ /* clean code for computing main linear form */
+ clean_code(mpl, con->code);
+ /* clean code for computing lower bound */
+ clean_code(mpl, con->lbnd);
+ /* clean code for computing upper bound */
+ if (con->ubnd != con->lbnd) clean_code(mpl, con->ubnd);
+ /* delete content array */
+ for (memb = con->array->head; memb != NULL; memb = memb->next)
+ { delete_formula(mpl, memb->value.con->form);
+ dmp_free_atom(mpl->elemcons, memb->value.con, sizeof(ELEMCON));
+ }
+ delete_array(mpl, con->array), con->array = NULL;
+ return;
+}
+
+/**********************************************************************/
+/* * * PSEUDO-CODE * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- eval_numeric - evaluate pseudo-code to determine numeric value.
+--
+-- This routine evaluates specified pseudo-code to determine resultant
+-- numeric value, which is returned on exit. */
+
+struct iter_num_info
+{ /* working info used by the routine iter_num_func */
+ CODE *code;
+ /* pseudo-code for iterated operation to be performed */
+ double value;
+ /* resultant value */
+};
+
+static int iter_num_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine used to perform iterated operation
+ on numeric "integrand" within domain scope */
+ struct iter_num_info *info = _info;
+ double temp;
+ temp = eval_numeric(mpl, info->code->arg.loop.x);
+ switch (info->code->op)
+ { case O_SUM:
+ /* summation over domain */
+ info->value = fp_add(mpl, info->value, temp);
+ break;
+ case O_PROD:
+ /* multiplication over domain */
+ info->value = fp_mul(mpl, info->value, temp);
+ break;
+ case O_MINIMUM:
+ /* minimum over domain */
+ if (info->value > temp) info->value = temp;
+ break;
+ case O_MAXIMUM:
+ /* maximum over domain */
+ if (info->value < temp) info->value = temp;
+ break;
+ default:
+ xassert(info != info);
+ }
+ return 0;
+}
+
+double eval_numeric(MPL *mpl, CODE *code)
+{ double value;
+ xassert(code != NULL);
+ xassert(code->type == A_NUMERIC);
+ xassert(code->dim == 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = code->value.num;
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_NUMBER:
+ /* take floating-point number */
+ value = code->arg.num;
+ break;
+ case O_MEMNUM:
+ /* take member of numeric parameter */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+ tuple = create_tuple(mpl);
+ for (e = code->arg.par.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+ value = eval_member_num(mpl, code->arg.par.par, tuple);
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_MEMVAR:
+ /* take computed value of elemental variable */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+#if 1 /* 15/V-2010 */
+ ELEMVAR *var;
+#endif
+ tuple = create_tuple(mpl);
+ for (e = code->arg.var.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+#if 0 /* 15/V-2010 */
+ value = eval_member_var(mpl, code->arg.var.var, tuple)
+ ->value;
+#else
+ var = eval_member_var(mpl, code->arg.var.var, tuple);
+ switch (code->arg.var.suff)
+ { case DOT_LB:
+ if (var->var->lbnd == NULL)
+ value = -DBL_MAX;
+ else
+ value = var->lbnd;
+ break;
+ case DOT_UB:
+ if (var->var->ubnd == NULL)
+ value = +DBL_MAX;
+ else
+ value = var->ubnd;
+ break;
+ case DOT_STATUS:
+ value = var->stat;
+ break;
+ case DOT_VAL:
+ value = var->prim;
+ break;
+ case DOT_DUAL:
+ value = var->dual;
+ break;
+ default:
+ xassert(code != code);
+ }
+#endif
+ delete_tuple(mpl, tuple);
+ }
+ break;
+#if 1 /* 15/V-2010 */
+ case O_MEMCON:
+ /* take computed value of elemental constraint */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+ ELEMCON *con;
+ tuple = create_tuple(mpl);
+ for (e = code->arg.con.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+ con = eval_member_con(mpl, code->arg.con.con, tuple);
+ switch (code->arg.con.suff)
+ { case DOT_LB:
+ if (con->con->lbnd == NULL)
+ value = -DBL_MAX;
+ else
+ value = con->lbnd;
+ break;
+ case DOT_UB:
+ if (con->con->ubnd == NULL)
+ value = +DBL_MAX;
+ else
+ value = con->ubnd;
+ break;
+ case DOT_STATUS:
+ value = con->stat;
+ break;
+ case DOT_VAL:
+ value = con->prim;
+ break;
+ case DOT_DUAL:
+ value = con->dual;
+ break;
+ default:
+ xassert(code != code);
+ }
+ delete_tuple(mpl, tuple);
+ }
+ break;
+#endif
+ case O_IRAND224:
+ /* pseudo-random in [0, 2^24-1] */
+ value = fp_irand224(mpl);
+ break;
+ case O_UNIFORM01:
+ /* pseudo-random in [0, 1) */
+ value = fp_uniform01(mpl);
+ break;
+ case O_NORMAL01:
+ /* gaussian random, mu = 0, sigma = 1 */
+ value = fp_normal01(mpl);
+ break;
+ case O_GMTIME:
+ /* current calendar time */
+ value = fn_gmtime(mpl);
+ break;
+ case O_CVTNUM:
+ /* conversion to numeric */
+ { SYMBOL *sym;
+ sym = eval_symbolic(mpl, code->arg.arg.x);
+#if 0 /* 23/XI-2008 */
+ if (sym->str != NULL)
+ error(mpl, "cannot convert %s to floating-point numbe"
+ "r", format_symbol(mpl, sym));
+ value = sym->num;
+#else
+ if (sym->str == NULL)
+ value = sym->num;
+ else
+ { if (str2num(sym->str, &value))
+ error(mpl, "cannot convert %s to floating-point nu"
+ "mber", format_symbol(mpl, sym));
+ }
+#endif
+ delete_symbol(mpl, sym);
+ }
+ break;
+ case O_PLUS:
+ /* unary plus */
+ value = + eval_numeric(mpl, code->arg.arg.x);
+ break;
+ case O_MINUS:
+ /* unary minus */
+ value = - eval_numeric(mpl, code->arg.arg.x);
+ break;
+ case O_ABS:
+ /* absolute value */
+ value = fabs(eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_CEIL:
+ /* round upward ("ceiling of x") */
+ value = ceil(eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_FLOOR:
+ /* round downward ("floor of x") */
+ value = floor(eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_EXP:
+ /* base-e exponential */
+ value = fp_exp(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_LOG:
+ /* natural logarithm */
+ value = fp_log(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_LOG10:
+ /* common (decimal) logarithm */
+ value = fp_log10(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_SQRT:
+ /* square root */
+ value = fp_sqrt(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_SIN:
+ /* trigonometric sine */
+ value = fp_sin(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_COS:
+ /* trigonometric cosine */
+ value = fp_cos(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_TAN:
+ /* trigonometric tangent */
+ value = fp_tan(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_ATAN:
+ /* trigonometric arctangent (one argument) */
+ value = fp_atan(mpl, eval_numeric(mpl, code->arg.arg.x));
+ break;
+ case O_ATAN2:
+ /* trigonometric arctangent (two arguments) */
+ value = fp_atan2(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_ROUND:
+ /* round to nearest integer */
+ value = fp_round(mpl,
+ eval_numeric(mpl, code->arg.arg.x), 0.0);
+ break;
+ case O_ROUND2:
+ /* round to n fractional digits */
+ value = fp_round(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_TRUNC:
+ /* truncate to nearest integer */
+ value = fp_trunc(mpl,
+ eval_numeric(mpl, code->arg.arg.x), 0.0);
+ break;
+ case O_TRUNC2:
+ /* truncate to n fractional digits */
+ value = fp_trunc(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_ADD:
+ /* addition */
+ value = fp_add(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_SUB:
+ /* subtraction */
+ value = fp_sub(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_LESS:
+ /* non-negative subtraction */
+ value = fp_less(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_MUL:
+ /* multiplication */
+ value = fp_mul(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_DIV:
+ /* division */
+ value = fp_div(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_IDIV:
+ /* quotient of exact division */
+ value = fp_idiv(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_MOD:
+ /* remainder of exact division */
+ value = fp_mod(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_POWER:
+ /* exponentiation (raise to power) */
+ value = fp_power(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_UNIFORM:
+ /* pseudo-random in [a, b) */
+ value = fp_uniform(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_NORMAL:
+ /* gaussian random, given mu and sigma */
+ value = fp_normal(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y));
+ break;
+ case O_CARD:
+ { ELEMSET *set;
+ set = eval_elemset(mpl, code->arg.arg.x);
+ value = set->size;
+ delete_array(mpl, set);
+ }
+ break;
+ case O_LENGTH:
+ { SYMBOL *sym;
+ char str[MAX_LENGTH+1];
+ sym = eval_symbolic(mpl, code->arg.arg.x);
+ if (sym->str == NULL)
+ sprintf(str, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, str);
+ delete_symbol(mpl, sym);
+ value = strlen(str);
+ }
+ break;
+ case O_STR2TIME:
+ { SYMBOL *sym;
+ char str[MAX_LENGTH+1], fmt[MAX_LENGTH+1];
+ sym = eval_symbolic(mpl, code->arg.arg.x);
+ if (sym->str == NULL)
+ sprintf(str, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, str);
+ delete_symbol(mpl, sym);
+ sym = eval_symbolic(mpl, code->arg.arg.y);
+ if (sym->str == NULL)
+ sprintf(fmt, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, fmt);
+ delete_symbol(mpl, sym);
+ value = fn_str2time(mpl, str, fmt);
+ }
+ break;
+ case O_FORK:
+ /* if-then-else */
+ if (eval_logical(mpl, code->arg.arg.x))
+ value = eval_numeric(mpl, code->arg.arg.y);
+ else if (code->arg.arg.z == NULL)
+ value = 0.0;
+ else
+ value = eval_numeric(mpl, code->arg.arg.z);
+ break;
+ case O_MIN:
+ /* minimal value (n-ary) */
+ { ARG_LIST *e;
+ double temp;
+ value = +DBL_MAX;
+ for (e = code->arg.list; e != NULL; e = e->next)
+ { temp = eval_numeric(mpl, e->x);
+ if (value > temp) value = temp;
+ }
+ }
+ break;
+ case O_MAX:
+ /* maximal value (n-ary) */
+ { ARG_LIST *e;
+ double temp;
+ value = -DBL_MAX;
+ for (e = code->arg.list; e != NULL; e = e->next)
+ { temp = eval_numeric(mpl, e->x);
+ if (value < temp) value = temp;
+ }
+ }
+ break;
+ case O_SUM:
+ /* summation over domain */
+ { struct iter_num_info _info, *info = &_info;
+ info->code = code;
+ info->value = 0.0;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_num_func);
+ value = info->value;
+ }
+ break;
+ case O_PROD:
+ /* multiplication over domain */
+ { struct iter_num_info _info, *info = &_info;
+ info->code = code;
+ info->value = 1.0;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_num_func);
+ value = info->value;
+ }
+ break;
+ case O_MINIMUM:
+ /* minimum over domain */
+ { struct iter_num_info _info, *info = &_info;
+ info->code = code;
+ info->value = +DBL_MAX;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_num_func);
+ if (info->value == +DBL_MAX)
+ error(mpl, "min{} over empty set; result undefined");
+ value = info->value;
+ }
+ break;
+ case O_MAXIMUM:
+ /* maximum over domain */
+ { struct iter_num_info _info, *info = &_info;
+ info->code = code;
+ info->value = -DBL_MAX;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_num_func);
+ if (info->value == -DBL_MAX)
+ error(mpl, "max{} over empty set; result undefined");
+ value = info->value;
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.num = value;
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_symbolic - evaluate pseudo-code to determine symbolic value.
+--
+-- This routine evaluates specified pseudo-code to determine resultant
+-- symbolic value, which is returned on exit. */
+
+SYMBOL *eval_symbolic(MPL *mpl, CODE *code)
+{ SYMBOL *value;
+ xassert(code != NULL);
+ xassert(code->type == A_SYMBOLIC);
+ xassert(code->dim == 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = copy_symbol(mpl, code->value.sym);
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_STRING:
+ /* take character string */
+ value = create_symbol_str(mpl, create_string(mpl,
+ code->arg.str));
+ break;
+ case O_INDEX:
+ /* take dummy index */
+ xassert(code->arg.index.slot->value != NULL);
+ value = copy_symbol(mpl, code->arg.index.slot->value);
+ break;
+ case O_MEMSYM:
+ /* take member of symbolic parameter */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+ tuple = create_tuple(mpl);
+ for (e = code->arg.par.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+ value = eval_member_sym(mpl, code->arg.par.par, tuple);
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_CVTSYM:
+ /* conversion to symbolic */
+ value = create_symbol_num(mpl, eval_numeric(mpl,
+ code->arg.arg.x));
+ break;
+ case O_CONCAT:
+ /* concatenation */
+ value = concat_symbols(mpl,
+ eval_symbolic(mpl, code->arg.arg.x),
+ eval_symbolic(mpl, code->arg.arg.y));
+ break;
+ case O_FORK:
+ /* if-then-else */
+ if (eval_logical(mpl, code->arg.arg.x))
+ value = eval_symbolic(mpl, code->arg.arg.y);
+ else if (code->arg.arg.z == NULL)
+ value = create_symbol_num(mpl, 0.0);
+ else
+ value = eval_symbolic(mpl, code->arg.arg.z);
+ break;
+ case O_SUBSTR:
+ case O_SUBSTR3:
+ { double pos, len;
+ char str[MAX_LENGTH+1];
+ value = eval_symbolic(mpl, code->arg.arg.x);
+ if (value->str == NULL)
+ sprintf(str, "%.*g", DBL_DIG, value->num);
+ else
+ fetch_string(mpl, value->str, str);
+ delete_symbol(mpl, value);
+ if (code->op == O_SUBSTR)
+ { pos = eval_numeric(mpl, code->arg.arg.y);
+ if (pos != floor(pos))
+ error(mpl, "substr('...', %.*g); non-integer secon"
+ "d argument", DBL_DIG, pos);
+ if (pos < 1 || pos > strlen(str) + 1)
+ error(mpl, "substr('...', %.*g); substring out of "
+ "range", DBL_DIG, pos);
+ }
+ else
+ { pos = eval_numeric(mpl, code->arg.arg.y);
+ len = eval_numeric(mpl, code->arg.arg.z);
+ if (pos != floor(pos) || len != floor(len))
+ error(mpl, "substr('...', %.*g, %.*g); non-integer"
+ " second and/or third argument", DBL_DIG, pos,
+ DBL_DIG, len);
+ if (pos < 1 || len < 0 || pos + len > strlen(str) + 1)
+ error(mpl, "substr('...', %.*g, %.*g); substring o"
+ "ut of range", DBL_DIG, pos, DBL_DIG, len);
+ str[(int)pos + (int)len - 1] = '\0';
+ }
+ value = create_symbol_str(mpl, create_string(mpl, str +
+ (int)pos - 1));
+ }
+ break;
+ case O_TIME2STR:
+ { double num;
+ SYMBOL *sym;
+ char str[MAX_LENGTH+1], fmt[MAX_LENGTH+1];
+ num = eval_numeric(mpl, code->arg.arg.x);
+ sym = eval_symbolic(mpl, code->arg.arg.y);
+ if (sym->str == NULL)
+ sprintf(fmt, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, fmt);
+ delete_symbol(mpl, sym);
+ fn_time2str(mpl, str, num, fmt);
+ value = create_symbol_str(mpl, create_string(mpl, str));
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.sym = copy_symbol(mpl, value);
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_logical - evaluate pseudo-code to determine logical value.
+--
+-- This routine evaluates specified pseudo-code to determine resultant
+-- logical value, which is returned on exit. */
+
+struct iter_log_info
+{ /* working info used by the routine iter_log_func */
+ CODE *code;
+ /* pseudo-code for iterated operation to be performed */
+ int value;
+ /* resultant value */
+};
+
+static int iter_log_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine used to perform iterated operation
+ on logical "integrand" within domain scope */
+ struct iter_log_info *info = _info;
+ int ret = 0;
+ switch (info->code->op)
+ { case O_FORALL:
+ /* conjunction over domain */
+ info->value &= eval_logical(mpl, info->code->arg.loop.x);
+ if (!info->value) ret = 1;
+ break;
+ case O_EXISTS:
+ /* disjunction over domain */
+ info->value |= eval_logical(mpl, info->code->arg.loop.x);
+ if (info->value) ret = 1;
+ break;
+ default:
+ xassert(info != info);
+ }
+ return ret;
+}
+
+int eval_logical(MPL *mpl, CODE *code)
+{ int value;
+ xassert(code->type == A_LOGICAL);
+ xassert(code->dim == 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = code->value.bit;
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_CVTLOG:
+ /* conversion to logical */
+ value = (eval_numeric(mpl, code->arg.arg.x) != 0.0);
+ break;
+ case O_NOT:
+ /* negation (logical "not") */
+ value = !eval_logical(mpl, code->arg.arg.x);
+ break;
+ case O_LT:
+ /* comparison on 'less than' */
+#if 0 /* 02/VIII-2008 */
+ value = (eval_numeric(mpl, code->arg.arg.x) <
+ eval_numeric(mpl, code->arg.arg.y));
+#else
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) <
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) < 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+#endif
+ break;
+ case O_LE:
+ /* comparison on 'not greater than' */
+#if 0 /* 02/VIII-2008 */
+ value = (eval_numeric(mpl, code->arg.arg.x) <=
+ eval_numeric(mpl, code->arg.arg.y));
+#else
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) <=
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) <= 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+#endif
+ break;
+ case O_EQ:
+ /* comparison on 'equal to' */
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) ==
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) == 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+ break;
+ case O_GE:
+ /* comparison on 'not less than' */
+#if 0 /* 02/VIII-2008 */
+ value = (eval_numeric(mpl, code->arg.arg.x) >=
+ eval_numeric(mpl, code->arg.arg.y));
+#else
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) >=
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) >= 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+#endif
+ break;
+ case O_GT:
+ /* comparison on 'greater than' */
+#if 0 /* 02/VIII-2008 */
+ value = (eval_numeric(mpl, code->arg.arg.x) >
+ eval_numeric(mpl, code->arg.arg.y));
+#else
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) >
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) > 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+#endif
+ break;
+ case O_NE:
+ /* comparison on 'not equal to' */
+ xassert(code->arg.arg.x != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ value = (eval_numeric(mpl, code->arg.arg.x) !=
+ eval_numeric(mpl, code->arg.arg.y));
+ else
+ { SYMBOL *sym1 = eval_symbolic(mpl, code->arg.arg.x);
+ SYMBOL *sym2 = eval_symbolic(mpl, code->arg.arg.y);
+ value = (compare_symbols(mpl, sym1, sym2) != 0);
+ delete_symbol(mpl, sym1);
+ delete_symbol(mpl, sym2);
+ }
+ break;
+ case O_AND:
+ /* conjunction (logical "and") */
+ value = eval_logical(mpl, code->arg.arg.x) &&
+ eval_logical(mpl, code->arg.arg.y);
+ break;
+ case O_OR:
+ /* disjunction (logical "or") */
+ value = eval_logical(mpl, code->arg.arg.x) ||
+ eval_logical(mpl, code->arg.arg.y);
+ break;
+ case O_IN:
+ /* test on 'x in Y' */
+ { TUPLE *tuple;
+ tuple = eval_tuple(mpl, code->arg.arg.x);
+ value = is_member(mpl, code->arg.arg.y, tuple);
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_NOTIN:
+ /* test on 'x not in Y' */
+ { TUPLE *tuple;
+ tuple = eval_tuple(mpl, code->arg.arg.x);
+ value = !is_member(mpl, code->arg.arg.y, tuple);
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_WITHIN:
+ /* test on 'X within Y' */
+ { ELEMSET *set;
+ MEMBER *memb;
+ set = eval_elemset(mpl, code->arg.arg.x);
+ value = 1;
+ for (memb = set->head; memb != NULL; memb = memb->next)
+ { if (!is_member(mpl, code->arg.arg.y, memb->tuple))
+ { value = 0;
+ break;
+ }
+ }
+ delete_elemset(mpl, set);
+ }
+ break;
+ case O_NOTWITHIN:
+ /* test on 'X not within Y' */
+ { ELEMSET *set;
+ MEMBER *memb;
+ set = eval_elemset(mpl, code->arg.arg.x);
+ value = 1;
+ for (memb = set->head; memb != NULL; memb = memb->next)
+ { if (is_member(mpl, code->arg.arg.y, memb->tuple))
+ { value = 0;
+ break;
+ }
+ }
+ delete_elemset(mpl, set);
+ }
+ break;
+ case O_FORALL:
+ /* conjunction (A-quantification) */
+ { struct iter_log_info _info, *info = &_info;
+ info->code = code;
+ info->value = 1;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_log_func);
+ value = info->value;
+ }
+ break;
+ case O_EXISTS:
+ /* disjunction (E-quantification) */
+ { struct iter_log_info _info, *info = &_info;
+ info->code = code;
+ info->value = 0;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_log_func);
+ value = info->value;
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.bit = value;
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_tuple - evaluate pseudo-code to construct n-tuple.
+--
+-- This routine evaluates specified pseudo-code to construct resultant
+-- n-tuple, which is returned on exit. */
+
+TUPLE *eval_tuple(MPL *mpl, CODE *code)
+{ TUPLE *value;
+ xassert(code != NULL);
+ xassert(code->type == A_TUPLE);
+ xassert(code->dim > 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = copy_tuple(mpl, code->value.tuple);
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_TUPLE:
+ /* make n-tuple */
+ { ARG_LIST *e;
+ value = create_tuple(mpl);
+ for (e = code->arg.list; e != NULL; e = e->next)
+ value = expand_tuple(mpl, value, eval_symbolic(mpl,
+ e->x));
+ }
+ break;
+ case O_CVTTUP:
+ /* convert to 1-tuple */
+ value = expand_tuple(mpl, create_tuple(mpl),
+ eval_symbolic(mpl, code->arg.arg.x));
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.tuple = copy_tuple(mpl, value);
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_elemset - evaluate pseudo-code to construct elemental set.
+--
+-- This routine evaluates specified pseudo-code to construct resultant
+-- elemental set, which is returned on exit. */
+
+struct iter_set_info
+{ /* working info used by the routine iter_set_func */
+ CODE *code;
+ /* pseudo-code for iterated operation to be performed */
+ ELEMSET *value;
+ /* resultant value */
+};
+
+static int iter_set_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine used to perform iterated operation
+ on n-tuple "integrand" within domain scope */
+ struct iter_set_info *info = _info;
+ TUPLE *tuple;
+ switch (info->code->op)
+ { case O_SETOF:
+ /* compute next n-tuple and add it to the set; in this case
+ duplicate n-tuples are silently ignored */
+ tuple = eval_tuple(mpl, info->code->arg.loop.x);
+ if (find_tuple(mpl, info->value, tuple) == NULL)
+ add_tuple(mpl, info->value, tuple);
+ else
+ delete_tuple(mpl, tuple);
+ break;
+ case O_BUILD:
+ /* construct next n-tuple using current values assigned to
+ *free* dummy indices as its components and add it to the
+ set; in this case duplicate n-tuples cannot appear */
+ add_tuple(mpl, info->value, get_domain_tuple(mpl,
+ info->code->arg.loop.domain));
+ break;
+ default:
+ xassert(info != info);
+ }
+ return 0;
+}
+
+ELEMSET *eval_elemset(MPL *mpl, CODE *code)
+{ ELEMSET *value;
+ xassert(code != NULL);
+ xassert(code->type == A_ELEMSET);
+ xassert(code->dim > 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = copy_elemset(mpl, code->value.set);
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_MEMSET:
+ /* take member of set */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+ tuple = create_tuple(mpl);
+ for (e = code->arg.set.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+ value = copy_elemset(mpl,
+ eval_member_set(mpl, code->arg.set.set, tuple));
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_MAKE:
+ /* make elemental set of n-tuples */
+ { ARG_LIST *e;
+ value = create_elemset(mpl, code->dim);
+ for (e = code->arg.list; e != NULL; e = e->next)
+ check_then_add(mpl, value, eval_tuple(mpl, e->x));
+ }
+ break;
+ case O_UNION:
+ /* union of two elemental sets */
+ value = set_union(mpl,
+ eval_elemset(mpl, code->arg.arg.x),
+ eval_elemset(mpl, code->arg.arg.y));
+ break;
+ case O_DIFF:
+ /* difference between two elemental sets */
+ value = set_diff(mpl,
+ eval_elemset(mpl, code->arg.arg.x),
+ eval_elemset(mpl, code->arg.arg.y));
+ break;
+ case O_SYMDIFF:
+ /* symmetric difference between two elemental sets */
+ value = set_symdiff(mpl,
+ eval_elemset(mpl, code->arg.arg.x),
+ eval_elemset(mpl, code->arg.arg.y));
+ break;
+ case O_INTER:
+ /* intersection of two elemental sets */
+ value = set_inter(mpl,
+ eval_elemset(mpl, code->arg.arg.x),
+ eval_elemset(mpl, code->arg.arg.y));
+ break;
+ case O_CROSS:
+ /* cross (Cartesian) product of two elemental sets */
+ value = set_cross(mpl,
+ eval_elemset(mpl, code->arg.arg.x),
+ eval_elemset(mpl, code->arg.arg.y));
+ break;
+ case O_DOTS:
+ /* build "arithmetic" elemental set */
+ value = create_arelset(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_numeric(mpl, code->arg.arg.y),
+ code->arg.arg.z == NULL ? 1.0 : eval_numeric(mpl,
+ code->arg.arg.z));
+ break;
+ case O_FORK:
+ /* if-then-else */
+ if (eval_logical(mpl, code->arg.arg.x))
+ value = eval_elemset(mpl, code->arg.arg.y);
+ else
+ value = eval_elemset(mpl, code->arg.arg.z);
+ break;
+ case O_SETOF:
+ /* compute elemental set */
+ { struct iter_set_info _info, *info = &_info;
+ info->code = code;
+ info->value = create_elemset(mpl, code->dim);
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_set_func);
+ value = info->value;
+ }
+ break;
+ case O_BUILD:
+ /* build elemental set identical to domain set */
+ { struct iter_set_info _info, *info = &_info;
+ info->code = code;
+ info->value = create_elemset(mpl, code->dim);
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_set_func);
+ value = info->value;
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.set = copy_elemset(mpl, value);
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- is_member - check if n-tuple is in set specified by pseudo-code.
+--
+-- This routine checks if given n-tuple is a member of elemental set
+-- specified in the form of pseudo-code (i.e. by expression).
+--
+-- The n-tuple may have more components that dimension of the elemental
+-- set, in which case the extra components are ignored. */
+
+static void null_func(MPL *mpl, void *info)
+{ /* this is dummy routine used to enter the domain scope */
+ xassert(mpl == mpl);
+ xassert(info == NULL);
+ return;
+}
+
+int is_member(MPL *mpl, CODE *code, TUPLE *tuple)
+{ int value;
+ xassert(code != NULL);
+ xassert(code->type == A_ELEMSET);
+ xassert(code->dim > 0);
+ xassert(tuple != NULL);
+ switch (code->op)
+ { case O_MEMSET:
+ /* check if given n-tuple is member of elemental set, which
+ is assigned to member of model set */
+ { ARG_LIST *e;
+ TUPLE *temp;
+ ELEMSET *set;
+ /* evaluate reference to elemental set */
+ temp = create_tuple(mpl);
+ for (e = code->arg.set.list; e != NULL; e = e->next)
+ temp = expand_tuple(mpl, temp, eval_symbolic(mpl,
+ e->x));
+ set = eval_member_set(mpl, code->arg.set.set, temp);
+ delete_tuple(mpl, temp);
+ /* check if the n-tuple is contained in the set array */
+ temp = build_subtuple(mpl, tuple, set->dim);
+ value = (find_tuple(mpl, set, temp) != NULL);
+ delete_tuple(mpl, temp);
+ }
+ break;
+ case O_MAKE:
+ /* check if given n-tuple is member of literal set */
+ { ARG_LIST *e;
+ TUPLE *temp, *that;
+ value = 0;
+ temp = build_subtuple(mpl, tuple, code->dim);
+ for (e = code->arg.list; e != NULL; e = e->next)
+ { that = eval_tuple(mpl, e->x);
+ value = (compare_tuples(mpl, temp, that) == 0);
+ delete_tuple(mpl, that);
+ if (value) break;
+ }
+ delete_tuple(mpl, temp);
+ }
+ break;
+ case O_UNION:
+ value = is_member(mpl, code->arg.arg.x, tuple) ||
+ is_member(mpl, code->arg.arg.y, tuple);
+ break;
+ case O_DIFF:
+ value = is_member(mpl, code->arg.arg.x, tuple) &&
+ !is_member(mpl, code->arg.arg.y, tuple);
+ break;
+ case O_SYMDIFF:
+ { int in1 = is_member(mpl, code->arg.arg.x, tuple);
+ int in2 = is_member(mpl, code->arg.arg.y, tuple);
+ value = (in1 && !in2) || (!in1 && in2);
+ }
+ break;
+ case O_INTER:
+ value = is_member(mpl, code->arg.arg.x, tuple) &&
+ is_member(mpl, code->arg.arg.y, tuple);
+ break;
+ case O_CROSS:
+ { int j;
+ value = is_member(mpl, code->arg.arg.x, tuple);
+ if (value)
+ { for (j = 1; j <= code->arg.arg.x->dim; j++)
+ { xassert(tuple != NULL);
+ tuple = tuple->next;
+ }
+ value = is_member(mpl, code->arg.arg.y, tuple);
+ }
+ }
+ break;
+ case O_DOTS:
+ /* check if given 1-tuple is member of "arithmetic" set */
+ { int j;
+ double x, t0, tf, dt;
+ xassert(code->dim == 1);
+ /* compute "parameters" of the "arithmetic" set */
+ t0 = eval_numeric(mpl, code->arg.arg.x);
+ tf = eval_numeric(mpl, code->arg.arg.y);
+ if (code->arg.arg.z == NULL)
+ dt = 1.0;
+ else
+ dt = eval_numeric(mpl, code->arg.arg.z);
+ /* make sure the parameters are correct */
+ arelset_size(mpl, t0, tf, dt);
+ /* if component of 1-tuple is symbolic, not numeric, the
+ 1-tuple cannot be member of "arithmetic" set */
+ xassert(tuple->sym != NULL);
+ if (tuple->sym->str != NULL)
+ { value = 0;
+ break;
+ }
+ /* determine numeric value of the component */
+ x = tuple->sym->num;
+ /* if the component value is out of the set range, the
+ 1-tuple is not in the set */
+ if (dt > 0.0 && !(t0 <= x && x <= tf) ||
+ dt < 0.0 && !(tf <= x && x <= t0))
+ { value = 0;
+ break;
+ }
+ /* estimate ordinal number of the 1-tuple in the set */
+ j = (int)(((x - t0) / dt) + 0.5) + 1;
+ /* perform the main check */
+ value = (arelset_member(mpl, t0, tf, dt, j) == x);
+ }
+ break;
+ case O_FORK:
+ /* check if given n-tuple is member of conditional set */
+ if (eval_logical(mpl, code->arg.arg.x))
+ value = is_member(mpl, code->arg.arg.y, tuple);
+ else
+ value = is_member(mpl, code->arg.arg.z, tuple);
+ break;
+ case O_SETOF:
+ /* check if given n-tuple is member of computed set */
+ /* it is not clear how to efficiently perform the check not
+ computing the entire elemental set :+( */
+ error(mpl, "implementation restriction; in/within setof{} n"
+ "ot allowed");
+ break;
+ case O_BUILD:
+ /* check if given n-tuple is member of domain set */
+ { TUPLE *temp;
+ temp = build_subtuple(mpl, tuple, code->dim);
+ /* try to enter the domain scope; if it is successful,
+ the n-tuple is in the domain set */
+ value = (eval_within_domain(mpl, code->arg.loop.domain,
+ temp, NULL, null_func) == 0);
+ delete_tuple(mpl, temp);
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ return value;
+}
+
+/*----------------------------------------------------------------------
+-- eval_formula - evaluate pseudo-code to construct linear form.
+--
+-- This routine evaluates specified pseudo-code to construct resultant
+-- linear form, which is returned on exit. */
+
+struct iter_form_info
+{ /* working info used by the routine iter_form_func */
+ CODE *code;
+ /* pseudo-code for iterated operation to be performed */
+ FORMULA *value;
+ /* resultant value */
+ FORMULA *tail;
+ /* pointer to the last term */
+};
+
+static int iter_form_func(MPL *mpl, void *_info)
+{ /* this is auxiliary routine used to perform iterated operation
+ on linear form "integrand" within domain scope */
+ struct iter_form_info *info = _info;
+ switch (info->code->op)
+ { case O_SUM:
+ /* summation over domain */
+#if 0
+ info->value =
+ linear_comb(mpl,
+ +1.0, info->value,
+ +1.0, eval_formula(mpl, info->code->arg.loop.x));
+#else
+ /* the routine linear_comb needs to look through all terms
+ of both linear forms to reduce identical terms, so using
+ it here is not a good idea (for example, evaluation of
+ sum{i in 1..n} x[i] required quadratic time); the better
+ idea is to gather all terms of the integrand in one list
+ and reduce identical terms only once after all terms of
+ the resultant linear form have been evaluated */
+ { FORMULA *form, *term;
+ form = eval_formula(mpl, info->code->arg.loop.x);
+ if (info->value == NULL)
+ { xassert(info->tail == NULL);
+ info->value = form;
+ }
+ else
+ { xassert(info->tail != NULL);
+ info->tail->next = form;
+ }
+ for (term = form; term != NULL; term = term->next)
+ info->tail = term;
+ }
+#endif
+ break;
+ default:
+ xassert(info != info);
+ }
+ return 0;
+}
+
+FORMULA *eval_formula(MPL *mpl, CODE *code)
+{ FORMULA *value;
+ xassert(code != NULL);
+ xassert(code->type == A_FORMULA);
+ xassert(code->dim == 0);
+ /* if the operation has a side effect, invalidate and delete the
+ resultant value */
+ if (code->vflag && code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* if resultant value is valid, no evaluation is needed */
+ if (code->valid)
+ { value = copy_formula(mpl, code->value.form);
+ goto done;
+ }
+ /* evaluate pseudo-code recursively */
+ switch (code->op)
+ { case O_MEMVAR:
+ /* take member of variable */
+ { TUPLE *tuple;
+ ARG_LIST *e;
+ tuple = create_tuple(mpl);
+ for (e = code->arg.var.list; e != NULL; e = e->next)
+ tuple = expand_tuple(mpl, tuple, eval_symbolic(mpl,
+ e->x));
+#if 1 /* 15/V-2010 */
+ xassert(code->arg.var.suff == DOT_NONE);
+#endif
+ value = single_variable(mpl,
+ eval_member_var(mpl, code->arg.var.var, tuple));
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case O_CVTLFM:
+ /* convert to linear form */
+ value = constant_term(mpl, eval_numeric(mpl,
+ code->arg.arg.x));
+ break;
+ case O_PLUS:
+ /* unary plus */
+ value = linear_comb(mpl,
+ 0.0, constant_term(mpl, 0.0),
+ +1.0, eval_formula(mpl, code->arg.arg.x));
+ break;
+ case O_MINUS:
+ /* unary minus */
+ value = linear_comb(mpl,
+ 0.0, constant_term(mpl, 0.0),
+ -1.0, eval_formula(mpl, code->arg.arg.x));
+ break;
+ case O_ADD:
+ /* addition */
+ value = linear_comb(mpl,
+ +1.0, eval_formula(mpl, code->arg.arg.x),
+ +1.0, eval_formula(mpl, code->arg.arg.y));
+ break;
+ case O_SUB:
+ /* subtraction */
+ value = linear_comb(mpl,
+ +1.0, eval_formula(mpl, code->arg.arg.x),
+ -1.0, eval_formula(mpl, code->arg.arg.y));
+ break;
+ case O_MUL:
+ /* multiplication */
+ xassert(code->arg.arg.x != NULL);
+ xassert(code->arg.arg.y != NULL);
+ if (code->arg.arg.x->type == A_NUMERIC)
+ { xassert(code->arg.arg.y->type == A_FORMULA);
+ value = linear_comb(mpl,
+ eval_numeric(mpl, code->arg.arg.x),
+ eval_formula(mpl, code->arg.arg.y),
+ 0.0, constant_term(mpl, 0.0));
+ }
+ else
+ { xassert(code->arg.arg.x->type == A_FORMULA);
+ xassert(code->arg.arg.y->type == A_NUMERIC);
+ value = linear_comb(mpl,
+ eval_numeric(mpl, code->arg.arg.y),
+ eval_formula(mpl, code->arg.arg.x),
+ 0.0, constant_term(mpl, 0.0));
+ }
+ break;
+ case O_DIV:
+ /* division */
+ value = linear_comb(mpl,
+ fp_div(mpl, 1.0, eval_numeric(mpl, code->arg.arg.y)),
+ eval_formula(mpl, code->arg.arg.x),
+ 0.0, constant_term(mpl, 0.0));
+ break;
+ case O_FORK:
+ /* if-then-else */
+ if (eval_logical(mpl, code->arg.arg.x))
+ value = eval_formula(mpl, code->arg.arg.y);
+ else if (code->arg.arg.z == NULL)
+ value = constant_term(mpl, 0.0);
+ else
+ value = eval_formula(mpl, code->arg.arg.z);
+ break;
+ case O_SUM:
+ /* summation over domain */
+ { struct iter_form_info _info, *info = &_info;
+ info->code = code;
+ info->value = constant_term(mpl, 0.0);
+ info->tail = NULL;
+ loop_within_domain(mpl, code->arg.loop.domain, info,
+ iter_form_func);
+ value = reduce_terms(mpl, info->value);
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ /* save resultant value */
+ xassert(!code->valid);
+ code->valid = 1;
+ code->value.form = copy_formula(mpl, value);
+done: return value;
+}
+
+/*----------------------------------------------------------------------
+-- clean_code - clean pseudo-code.
+--
+-- This routine recursively cleans specified pseudo-code that assumes
+-- deleting all temporary resultant values. */
+
+void clean_code(MPL *mpl, CODE *code)
+{ ARG_LIST *e;
+ /* if no pseudo-code is specified, do nothing */
+ if (code == NULL) goto done;
+ /* if resultant value is valid (exists), delete it */
+ if (code->valid)
+ { code->valid = 0;
+ delete_value(mpl, code->type, &code->value);
+ }
+ /* recursively clean pseudo-code for operands */
+ switch (code->op)
+ { case O_NUMBER:
+ case O_STRING:
+ case O_INDEX:
+ break;
+ case O_MEMNUM:
+ case O_MEMSYM:
+ for (e = code->arg.par.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+ case O_MEMSET:
+ for (e = code->arg.set.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+ case O_MEMVAR:
+ for (e = code->arg.var.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+#if 1 /* 15/V-2010 */
+ case O_MEMCON:
+ for (e = code->arg.con.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+#endif
+ case O_TUPLE:
+ case O_MAKE:
+ for (e = code->arg.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+ case O_SLICE:
+ xassert(code != code);
+ case O_IRAND224:
+ case O_UNIFORM01:
+ case O_NORMAL01:
+ case O_GMTIME:
+ break;
+ case O_CVTNUM:
+ case O_CVTSYM:
+ case O_CVTLOG:
+ case O_CVTTUP:
+ case O_CVTLFM:
+ case O_PLUS:
+ case O_MINUS:
+ case O_NOT:
+ case O_ABS:
+ case O_CEIL:
+ case O_FLOOR:
+ case O_EXP:
+ case O_LOG:
+ case O_LOG10:
+ case O_SQRT:
+ case O_SIN:
+ case O_COS:
+ case O_TAN:
+ case O_ATAN:
+ case O_ROUND:
+ case O_TRUNC:
+ case O_CARD:
+ case O_LENGTH:
+ /* unary operation */
+ clean_code(mpl, code->arg.arg.x);
+ break;
+ case O_ADD:
+ case O_SUB:
+ case O_LESS:
+ case O_MUL:
+ case O_DIV:
+ case O_IDIV:
+ case O_MOD:
+ case O_POWER:
+ case O_ATAN2:
+ case O_ROUND2:
+ case O_TRUNC2:
+ case O_UNIFORM:
+ case O_NORMAL:
+ case O_CONCAT:
+ case O_LT:
+ case O_LE:
+ case O_EQ:
+ case O_GE:
+ case O_GT:
+ case O_NE:
+ case O_AND:
+ case O_OR:
+ case O_UNION:
+ case O_DIFF:
+ case O_SYMDIFF:
+ case O_INTER:
+ case O_CROSS:
+ case O_IN:
+ case O_NOTIN:
+ case O_WITHIN:
+ case O_NOTWITHIN:
+ case O_SUBSTR:
+ case O_STR2TIME:
+ case O_TIME2STR:
+ /* binary operation */
+ clean_code(mpl, code->arg.arg.x);
+ clean_code(mpl, code->arg.arg.y);
+ break;
+ case O_DOTS:
+ case O_FORK:
+ case O_SUBSTR3:
+ /* ternary operation */
+ clean_code(mpl, code->arg.arg.x);
+ clean_code(mpl, code->arg.arg.y);
+ clean_code(mpl, code->arg.arg.z);
+ break;
+ case O_MIN:
+ case O_MAX:
+ /* n-ary operation */
+ for (e = code->arg.list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+ break;
+ case O_SUM:
+ case O_PROD:
+ case O_MINIMUM:
+ case O_MAXIMUM:
+ case O_FORALL:
+ case O_EXISTS:
+ case O_SETOF:
+ case O_BUILD:
+ /* iterated operation */
+ clean_domain(mpl, code->arg.loop.domain);
+ clean_code(mpl, code->arg.loop.x);
+ break;
+ default:
+ xassert(code->op != code->op);
+ }
+done: return;
+}
+
+#if 1 /* 11/II-2008 */
+/**********************************************************************/
+/* * * DATA TABLES * * */
+/**********************************************************************/
+
+int mpl_tab_num_args(TABDCA *dca)
+{ /* returns the number of arguments */
+ return dca->na;
+}
+
+const char *mpl_tab_get_arg(TABDCA *dca, int k)
+{ /* returns pointer to k-th argument */
+ xassert(1 <= k && k <= dca->na);
+ return dca->arg[k];
+}
+
+int mpl_tab_num_flds(TABDCA *dca)
+{ /* returns the number of fields */
+ return dca->nf;
+}
+
+const char *mpl_tab_get_name(TABDCA *dca, int k)
+{ /* returns pointer to name of k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ return dca->name[k];
+}
+
+int mpl_tab_get_type(TABDCA *dca, int k)
+{ /* returns type of k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ return dca->type[k];
+}
+
+double mpl_tab_get_num(TABDCA *dca, int k)
+{ /* returns numeric value of k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ xassert(dca->type[k] == 'N');
+ return dca->num[k];
+}
+
+const char *mpl_tab_get_str(TABDCA *dca, int k)
+{ /* returns pointer to string value of k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ xassert(dca->type[k] == 'S');
+ xassert(dca->str[k] != NULL);
+ return dca->str[k];
+}
+
+void mpl_tab_set_num(TABDCA *dca, int k, double num)
+{ /* assign numeric value to k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ xassert(dca->type[k] == '?');
+ dca->type[k] = 'N';
+ dca->num[k] = num;
+ return;
+}
+
+void mpl_tab_set_str(TABDCA *dca, int k, const char *str)
+{ /* assign string value to k-th field */
+ xassert(1 <= k && k <= dca->nf);
+ xassert(dca->type[k] == '?');
+ xassert(strlen(str) <= MAX_LENGTH);
+ xassert(dca->str[k] != NULL);
+ dca->type[k] = 'S';
+ strcpy(dca->str[k], str);
+ return;
+}
+
+static int write_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ TABLE *tab = info;
+ TABDCA *dca = mpl->dca;
+ TABOUT *out;
+ SYMBOL *sym;
+ int k;
+ char buf[MAX_LENGTH+1];
+ /* evaluate field values */
+ k = 0;
+ for (out = tab->u.out.list; out != NULL; out = out->next)
+ { k++;
+ switch (out->code->type)
+ { case A_NUMERIC:
+ dca->type[k] = 'N';
+ dca->num[k] = eval_numeric(mpl, out->code);
+ dca->str[k][0] = '\0';
+ break;
+ case A_SYMBOLIC:
+ sym = eval_symbolic(mpl, out->code);
+ if (sym->str == NULL)
+ { dca->type[k] = 'N';
+ dca->num[k] = sym->num;
+ dca->str[k][0] = '\0';
+ }
+ else
+ { dca->type[k] = 'S';
+ dca->num[k] = 0.0;
+ fetch_string(mpl, sym->str, buf);
+ strcpy(dca->str[k], buf);
+ }
+ delete_symbol(mpl, sym);
+ break;
+ default:
+ xassert(out != out);
+ }
+ }
+ /* write record to output table */
+ mpl_tab_drv_write(mpl);
+ return 0;
+}
+
+void execute_table(MPL *mpl, TABLE *tab)
+{ /* execute table statement */
+ TABARG *arg;
+ TABFLD *fld;
+ TABIN *in;
+ TABOUT *out;
+ TABDCA *dca;
+ SET *set;
+ int k;
+ char buf[MAX_LENGTH+1];
+ /* allocate table driver communication area */
+ xassert(mpl->dca == NULL);
+ mpl->dca = dca = xmalloc(sizeof(TABDCA));
+ dca->id = 0;
+ dca->link = NULL;
+ dca->na = 0;
+ dca->arg = NULL;
+ dca->nf = 0;
+ dca->name = NULL;
+ dca->type = NULL;
+ dca->num = NULL;
+ dca->str = NULL;
+ /* allocate arguments */
+ xassert(dca->na == 0);
+ for (arg = tab->arg; arg != NULL; arg = arg->next)
+ dca->na++;
+ dca->arg = xcalloc(1+dca->na, sizeof(char *));
+#if 1 /* 28/IX-2008 */
+ for (k = 1; k <= dca->na; k++) dca->arg[k] = NULL;
+#endif
+ /* evaluate argument values */
+ k = 0;
+ for (arg = tab->arg; arg != NULL; arg = arg->next)
+ { SYMBOL *sym;
+ k++;
+ xassert(arg->code->type == A_SYMBOLIC);
+ sym = eval_symbolic(mpl, arg->code);
+ if (sym->str == NULL)
+ sprintf(buf, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, buf);
+ delete_symbol(mpl, sym);
+ dca->arg[k] = xmalloc(strlen(buf)+1);
+ strcpy(dca->arg[k], buf);
+ }
+ /* perform table input/output */
+ switch (tab->type)
+ { case A_INPUT: goto read_table;
+ case A_OUTPUT: goto write_table;
+ default: xassert(tab != tab);
+ }
+read_table:
+ /* read data from input table */
+ /* add the only member to the control set and assign it empty
+ elemental set */
+ set = tab->u.in.set;
+ if (set != NULL)
+ { if (set->data)
+ error(mpl, "%s already provided with data", set->name);
+ xassert(set->array->head == NULL);
+ add_member(mpl, set->array, NULL)->value.set =
+ create_elemset(mpl, set->dimen);
+ set->data = 1;
+ }
+ /* check parameters specified in the input list */
+ for (in = tab->u.in.list; in != NULL; in = in->next)
+ { if (in->par->data)
+ error(mpl, "%s already provided with data", in->par->name);
+ in->par->data = 1;
+ }
+ /* allocate and initialize fields */
+ xassert(dca->nf == 0);
+ for (fld = tab->u.in.fld; fld != NULL; fld = fld->next)
+ dca->nf++;
+ for (in = tab->u.in.list; in != NULL; in = in->next)
+ dca->nf++;
+ dca->name = xcalloc(1+dca->nf, sizeof(char *));
+ dca->type = xcalloc(1+dca->nf, sizeof(int));
+ dca->num = xcalloc(1+dca->nf, sizeof(double));
+ dca->str = xcalloc(1+dca->nf, sizeof(char *));
+ k = 0;
+ for (fld = tab->u.in.fld; fld != NULL; fld = fld->next)
+ { k++;
+ dca->name[k] = fld->name;
+ dca->type[k] = '?';
+ dca->num[k] = 0.0;
+ dca->str[k] = xmalloc(MAX_LENGTH+1);
+ dca->str[k][0] = '\0';
+ }
+ for (in = tab->u.in.list; in != NULL; in = in->next)
+ { k++;
+ dca->name[k] = in->name;
+ dca->type[k] = '?';
+ dca->num[k] = 0.0;
+ dca->str[k] = xmalloc(MAX_LENGTH+1);
+ dca->str[k][0] = '\0';
+ }
+ /* open input table */
+ mpl_tab_drv_open(mpl, 'R');
+ /* read and process records */
+ for (;;)
+ { TUPLE *tup;
+ /* reset field types */
+ for (k = 1; k <= dca->nf; k++)
+ dca->type[k] = '?';
+ /* read next record */
+ if (mpl_tab_drv_read(mpl)) break;
+ /* all fields must be set by the driver */
+ for (k = 1; k <= dca->nf; k++)
+ { if (dca->type[k] == '?')
+ error(mpl, "field %s missing in input table",
+ dca->name[k]);
+ }
+ /* construct n-tuple */
+ tup = create_tuple(mpl);
+ k = 0;
+ for (fld = tab->u.in.fld; fld != NULL; fld = fld->next)
+ { k++;
+ xassert(k <= dca->nf);
+ switch (dca->type[k])
+ { case 'N':
+ tup = expand_tuple(mpl, tup, create_symbol_num(mpl,
+ dca->num[k]));
+ break;
+ case 'S':
+ xassert(strlen(dca->str[k]) <= MAX_LENGTH);
+ tup = expand_tuple(mpl, tup, create_symbol_str(mpl,
+ create_string(mpl, dca->str[k])));
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ }
+ /* add n-tuple just read to the control set */
+ if (tab->u.in.set != NULL)
+ check_then_add(mpl, tab->u.in.set->array->head->value.set,
+ copy_tuple(mpl, tup));
+ /* assign values to the parameters in the input list */
+ for (in = tab->u.in.list; in != NULL; in = in->next)
+ { MEMBER *memb;
+ k++;
+ xassert(k <= dca->nf);
+ /* there must be no member with the same n-tuple */
+ if (find_member(mpl, in->par->array, tup) != NULL)
+ error(mpl, "%s%s already defined", in->par->name,
+ format_tuple(mpl, '[', tup));
+ /* create new parameter member with given n-tuple */
+ memb = add_member(mpl, in->par->array, copy_tuple(mpl, tup))
+ ;
+ /* assign value to the parameter member */
+ switch (in->par->type)
+ { case A_NUMERIC:
+ case A_INTEGER:
+ case A_BINARY:
+ if (dca->type[k] != 'N')
+ error(mpl, "%s requires numeric data",
+ in->par->name);
+ memb->value.num = dca->num[k];
+ break;
+ case A_SYMBOLIC:
+ switch (dca->type[k])
+ { case 'N':
+ memb->value.sym = create_symbol_num(mpl,
+ dca->num[k]);
+ break;
+ case 'S':
+ xassert(strlen(dca->str[k]) <= MAX_LENGTH);
+ memb->value.sym = create_symbol_str(mpl,
+ create_string(mpl,dca->str[k]));
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ break;
+ default:
+ xassert(in != in);
+ }
+ }
+ /* n-tuple is no more needed */
+ delete_tuple(mpl, tup);
+ }
+ /* close input table */
+ mpl_tab_drv_close(mpl);
+ goto done;
+write_table:
+ /* write data to output table */
+ /* allocate and initialize fields */
+ xassert(dca->nf == 0);
+ for (out = tab->u.out.list; out != NULL; out = out->next)
+ dca->nf++;
+ dca->name = xcalloc(1+dca->nf, sizeof(char *));
+ dca->type = xcalloc(1+dca->nf, sizeof(int));
+ dca->num = xcalloc(1+dca->nf, sizeof(double));
+ dca->str = xcalloc(1+dca->nf, sizeof(char *));
+ k = 0;
+ for (out = tab->u.out.list; out != NULL; out = out->next)
+ { k++;
+ dca->name[k] = out->name;
+ dca->type[k] = '?';
+ dca->num[k] = 0.0;
+ dca->str[k] = xmalloc(MAX_LENGTH+1);
+ dca->str[k][0] = '\0';
+ }
+ /* open output table */
+ mpl_tab_drv_open(mpl, 'W');
+ /* evaluate fields and write records */
+ loop_within_domain(mpl, tab->u.out.domain, tab, write_func);
+ /* close output table */
+ mpl_tab_drv_close(mpl);
+done: /* free table driver communication area */
+ free_dca(mpl);
+ return;
+}
+
+void free_dca(MPL *mpl)
+{ /* free table driver communucation area */
+ TABDCA *dca = mpl->dca;
+ int k;
+ if (dca != NULL)
+ { if (dca->link != NULL)
+ mpl_tab_drv_close(mpl);
+ if (dca->arg != NULL)
+ { for (k = 1; k <= dca->na; k++)
+#if 1 /* 28/IX-2008 */
+ if (dca->arg[k] != NULL)
+#endif
+ xfree(dca->arg[k]);
+ xfree(dca->arg);
+ }
+ if (dca->name != NULL) xfree(dca->name);
+ if (dca->type != NULL) xfree(dca->type);
+ if (dca->num != NULL) xfree(dca->num);
+ if (dca->str != NULL)
+ { for (k = 1; k <= dca->nf; k++)
+ xfree(dca->str[k]);
+ xfree(dca->str);
+ }
+ xfree(dca), mpl->dca = NULL;
+ }
+ return;
+}
+
+void clean_table(MPL *mpl, TABLE *tab)
+{ /* clean table statement */
+ TABARG *arg;
+ TABOUT *out;
+ /* clean string list */
+ for (arg = tab->arg; arg != NULL; arg = arg->next)
+ clean_code(mpl, arg->code);
+ switch (tab->type)
+ { case A_INPUT:
+ break;
+ case A_OUTPUT:
+ /* clean subscript domain */
+ clean_domain(mpl, tab->u.out.domain);
+ /* clean output list */
+ for (out = tab->u.out.list; out != NULL; out = out->next)
+ clean_code(mpl, out->code);
+ break;
+ default:
+ xassert(tab != tab);
+ }
+ return;
+}
+#endif
+
+/**********************************************************************/
+/* * * MODEL STATEMENTS * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- execute_check - execute check statement.
+--
+-- This routine executes specified check statement. */
+
+static int check_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ CHECK *chk = (CHECK *)info;
+ if (!eval_logical(mpl, chk->code))
+ error(mpl, "check%s failed", format_tuple(mpl, '[',
+ get_domain_tuple(mpl, chk->domain)));
+ return 0;
+}
+
+void execute_check(MPL *mpl, CHECK *chk)
+{ loop_within_domain(mpl, chk->domain, chk, check_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_check - clean check statement.
+--
+-- This routine cleans specified check statement that assumes deleting
+-- all stuff dynamically allocated on generating/postsolving phase. */
+
+void clean_check(MPL *mpl, CHECK *chk)
+{ /* clean subscript domain */
+ clean_domain(mpl, chk->domain);
+ /* clean pseudo-code for computing predicate */
+ clean_code(mpl, chk->code);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- execute_display - execute display statement.
+--
+-- This routine executes specified display statement. */
+
+static void display_set(MPL *mpl, SET *set, MEMBER *memb)
+{ /* display member of model set */
+ ELEMSET *s = memb->value.set;
+ MEMBER *m;
+ write_text(mpl, "%s%s%s\n", set->name,
+ format_tuple(mpl, '[', memb->tuple),
+ s->head == NULL ? " is empty" : ":");
+ for (m = s->head; m != NULL; m = m->next)
+ write_text(mpl, " %s\n", format_tuple(mpl, '(', m->tuple));
+ return;
+}
+
+static void display_par(MPL *mpl, PARAMETER *par, MEMBER *memb)
+{ /* display member of model parameter */
+ switch (par->type)
+ { case A_NUMERIC:
+ case A_INTEGER:
+ case A_BINARY:
+ write_text(mpl, "%s%s = %.*g\n", par->name,
+ format_tuple(mpl, '[', memb->tuple),
+ DBL_DIG, memb->value.num);
+ break;
+ case A_SYMBOLIC:
+ write_text(mpl, "%s%s = %s\n", par->name,
+ format_tuple(mpl, '[', memb->tuple),
+ format_symbol(mpl, memb->value.sym));
+ break;
+ default:
+ xassert(par != par);
+ }
+ return;
+}
+
+#if 1 /* 15/V-2010 */
+static void display_var(MPL *mpl, VARIABLE *var, MEMBER *memb,
+ int suff)
+{ /* display member of model variable */
+ if (suff == DOT_NONE || suff == DOT_VAL)
+ write_text(mpl, "%s%s.val = %.*g\n", var->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.var->prim);
+ else if (suff == DOT_LB)
+ write_text(mpl, "%s%s.lb = %.*g\n", var->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.var->var->lbnd == NULL ? -DBL_MAX :
+ memb->value.var->lbnd);
+ else if (suff == DOT_UB)
+ write_text(mpl, "%s%s.ub = %.*g\n", var->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.var->var->ubnd == NULL ? +DBL_MAX :
+ memb->value.var->ubnd);
+ else if (suff == DOT_STATUS)
+ write_text(mpl, "%s%s.status = %d\n", var->name, format_tuple
+ (mpl, '[', memb->tuple), memb->value.var->stat);
+ else if (suff == DOT_DUAL)
+ write_text(mpl, "%s%s.dual = %.*g\n", var->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.var->dual);
+ else
+ xassert(suff != suff);
+ return;
+}
+#endif
+
+#if 1 /* 15/V-2010 */
+static void display_con(MPL *mpl, CONSTRAINT *con, MEMBER *memb,
+ int suff)
+{ /* display member of model constraint */
+ if (suff == DOT_NONE || suff == DOT_VAL)
+ write_text(mpl, "%s%s.val = %.*g\n", con->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.con->prim);
+ else if (suff == DOT_LB)
+ write_text(mpl, "%s%s.lb = %.*g\n", con->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.con->con->lbnd == NULL ? -DBL_MAX :
+ memb->value.con->lbnd);
+ else if (suff == DOT_UB)
+ write_text(mpl, "%s%s.ub = %.*g\n", con->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.con->con->ubnd == NULL ? +DBL_MAX :
+ memb->value.con->ubnd);
+ else if (suff == DOT_STATUS)
+ write_text(mpl, "%s%s.status = %d\n", con->name, format_tuple
+ (mpl, '[', memb->tuple), memb->value.con->stat);
+ else if (suff == DOT_DUAL)
+ write_text(mpl, "%s%s.dual = %.*g\n", con->name,
+ format_tuple(mpl, '[', memb->tuple), DBL_DIG,
+ memb->value.con->dual);
+ else
+ xassert(suff != suff);
+ return;
+}
+#endif
+
+static void display_memb(MPL *mpl, CODE *code)
+{ /* display member specified by pseudo-code */
+ MEMBER memb;
+ ARG_LIST *e;
+ xassert(code->op == O_MEMNUM || code->op == O_MEMSYM
+ || code->op == O_MEMSET || code->op == O_MEMVAR
+ || code->op == O_MEMCON);
+ memb.tuple = create_tuple(mpl);
+ for (e = code->arg.par.list; e != NULL; e = e->next)
+ memb.tuple = expand_tuple(mpl, memb.tuple, eval_symbolic(mpl,
+ e->x));
+ switch (code->op)
+ { case O_MEMNUM:
+ memb.value.num = eval_member_num(mpl, code->arg.par.par,
+ memb.tuple);
+ display_par(mpl, code->arg.par.par, &memb);
+ break;
+ case O_MEMSYM:
+ memb.value.sym = eval_member_sym(mpl, code->arg.par.par,
+ memb.tuple);
+ display_par(mpl, code->arg.par.par, &memb);
+ delete_symbol(mpl, memb.value.sym);
+ break;
+ case O_MEMSET:
+ memb.value.set = eval_member_set(mpl, code->arg.set.set,
+ memb.tuple);
+ display_set(mpl, code->arg.set.set, &memb);
+ break;
+ case O_MEMVAR:
+ memb.value.var = eval_member_var(mpl, code->arg.var.var,
+ memb.tuple);
+ display_var
+ (mpl, code->arg.var.var, &memb, code->arg.var.suff);
+ break;
+ case O_MEMCON:
+ memb.value.con = eval_member_con(mpl, code->arg.con.con,
+ memb.tuple);
+ display_con
+ (mpl, code->arg.con.con, &memb, code->arg.con.suff);
+ break;
+ default:
+ xassert(code != code);
+ }
+ delete_tuple(mpl, memb.tuple);
+ return;
+}
+
+static void display_code(MPL *mpl, CODE *code)
+{ /* display value of expression */
+ switch (code->type)
+ { case A_NUMERIC:
+ /* numeric value */
+ { double num;
+ num = eval_numeric(mpl, code);
+ write_text(mpl, "%.*g\n", DBL_DIG, num);
+ }
+ break;
+ case A_SYMBOLIC:
+ /* symbolic value */
+ { SYMBOL *sym;
+ sym = eval_symbolic(mpl, code);
+ write_text(mpl, "%s\n", format_symbol(mpl, sym));
+ delete_symbol(mpl, sym);
+ }
+ break;
+ case A_LOGICAL:
+ /* logical value */
+ { int bit;
+ bit = eval_logical(mpl, code);
+ write_text(mpl, "%s\n", bit ? "true" : "false");
+ }
+ break;
+ case A_TUPLE:
+ /* n-tuple */
+ { TUPLE *tuple;
+ tuple = eval_tuple(mpl, code);
+ write_text(mpl, "%s\n", format_tuple(mpl, '(', tuple));
+ delete_tuple(mpl, tuple);
+ }
+ break;
+ case A_ELEMSET:
+ /* elemental set */
+ { ELEMSET *set;
+ MEMBER *memb;
+ set = eval_elemset(mpl, code);
+ if (set->head == 0)
+ write_text(mpl, "set is empty\n");
+ for (memb = set->head; memb != NULL; memb = memb->next)
+ write_text(mpl, " %s\n", format_tuple(mpl, '(',
+ memb->tuple));
+ delete_elemset(mpl, set);
+ }
+ break;
+ case A_FORMULA:
+ /* linear form */
+ { FORMULA *form, *term;
+ form = eval_formula(mpl, code);
+ if (form == NULL)
+ write_text(mpl, "linear form is empty\n");
+ for (term = form; term != NULL; term = term->next)
+ { if (term->var == NULL)
+ write_text(mpl, " %.*g\n", term->coef);
+ else
+ write_text(mpl, " %.*g %s%s\n", DBL_DIG,
+ term->coef, term->var->var->name,
+ format_tuple(mpl, '[', term->var->memb->tuple));
+ }
+ delete_formula(mpl, form);
+ }
+ break;
+ default:
+ xassert(code != code);
+ }
+ return;
+}
+
+static int display_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ DISPLAY *dpy = (DISPLAY *)info;
+ DISPLAY1 *entry;
+ for (entry = dpy->list; entry != NULL; entry = entry->next)
+ { if (entry->type == A_INDEX)
+ { /* dummy index */
+ DOMAIN_SLOT *slot = entry->u.slot;
+ write_text(mpl, "%s = %s\n", slot->name,
+ format_symbol(mpl, slot->value));
+ }
+ else if (entry->type == A_SET)
+ { /* model set */
+ SET *set = entry->u.set;
+ MEMBER *memb;
+ if (set->assign != NULL)
+ { /* the set has assignment expression; evaluate all its
+ members over entire domain */
+ eval_whole_set(mpl, set);
+ }
+ else
+ { /* the set has no assignment expression; refer to its
+ any existing member ignoring resultant value to check
+ the data provided the data section */
+#if 1 /* 12/XII-2008 */
+ if (set->gadget != NULL && set->data == 0)
+ { /* initialize the set with data from a plain set */
+ saturate_set(mpl, set);
+ }
+#endif
+ if (set->array->head != NULL)
+ eval_member_set(mpl, set, set->array->head->tuple);
+ }
+ /* display all members of the set array */
+ if (set->array->head == NULL)
+ write_text(mpl, "%s has empty content\n", set->name);
+ for (memb = set->array->head; memb != NULL; memb =
+ memb->next) display_set(mpl, set, memb);
+ }
+ else if (entry->type == A_PARAMETER)
+ { /* model parameter */
+ PARAMETER *par = entry->u.par;
+ MEMBER *memb;
+ if (par->assign != NULL)
+ { /* the parameter has an assignment expression; evaluate
+ all its member over entire domain */
+ eval_whole_par(mpl, par);
+ }
+ else
+ { /* the parameter has no assignment expression; refer to
+ its any existing member ignoring resultant value to
+ check the data provided in the data section */
+ if (par->array->head != NULL)
+ { if (par->type != A_SYMBOLIC)
+ eval_member_num(mpl, par, par->array->head->tuple);
+ else
+ delete_symbol(mpl, eval_member_sym(mpl, par,
+ par->array->head->tuple));
+ }
+ }
+ /* display all members of the parameter array */
+ if (par->array->head == NULL)
+ write_text(mpl, "%s has empty content\n", par->name);
+ for (memb = par->array->head; memb != NULL; memb =
+ memb->next) display_par(mpl, par, memb);
+ }
+ else if (entry->type == A_VARIABLE)
+ { /* model variable */
+ VARIABLE *var = entry->u.var;
+ MEMBER *memb;
+ xassert(mpl->flag_p);
+ /* display all members of the variable array */
+ if (var->array->head == NULL)
+ write_text(mpl, "%s has empty content\n", var->name);
+ for (memb = var->array->head; memb != NULL; memb =
+ memb->next) display_var(mpl, var, memb, DOT_NONE);
+ }
+ else if (entry->type == A_CONSTRAINT)
+ { /* model constraint */
+ CONSTRAINT *con = entry->u.con;
+ MEMBER *memb;
+ xassert(mpl->flag_p);
+ /* display all members of the constraint array */
+ if (con->array->head == NULL)
+ write_text(mpl, "%s has empty content\n", con->name);
+ for (memb = con->array->head; memb != NULL; memb =
+ memb->next) display_con(mpl, con, memb, DOT_NONE);
+ }
+ else if (entry->type == A_EXPRESSION)
+ { /* expression */
+ CODE *code = entry->u.code;
+ if (code->op == O_MEMNUM || code->op == O_MEMSYM ||
+ code->op == O_MEMSET || code->op == O_MEMVAR ||
+ code->op == O_MEMCON)
+ display_memb(mpl, code);
+ else
+ display_code(mpl, code);
+ }
+ else
+ xassert(entry != entry);
+ }
+ return 0;
+}
+
+void execute_display(MPL *mpl, DISPLAY *dpy)
+{ loop_within_domain(mpl, dpy->domain, dpy, display_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_display - clean display statement.
+--
+-- This routine cleans specified display statement that assumes deleting
+-- all stuff dynamically allocated on generating/postsolving phase. */
+
+void clean_display(MPL *mpl, DISPLAY *dpy)
+{ DISPLAY1 *d;
+#if 0 /* 15/V-2010 */
+ ARG_LIST *e;
+#endif
+ /* clean subscript domain */
+ clean_domain(mpl, dpy->domain);
+ /* clean display list */
+ for (d = dpy->list; d != NULL; d = d->next)
+ { /* clean pseudo-code for computing expression */
+ if (d->type == A_EXPRESSION)
+ clean_code(mpl, d->u.code);
+#if 0 /* 15/V-2010 */
+ /* clean pseudo-code for computing subscripts */
+ for (e = d->list; e != NULL; e = e->next)
+ clean_code(mpl, e->x);
+#endif
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- execute_printf - execute printf statement.
+--
+-- This routine executes specified printf statement. */
+
+#if 1 /* 14/VII-2006 */
+static void print_char(MPL *mpl, int c)
+{ if (mpl->prt_fp == NULL)
+ write_char(mpl, c);
+ else
+#if 0 /* 04/VIII-2013 */
+ xfputc(c, mpl->prt_fp);
+#else
+ { unsigned char buf[1];
+ buf[0] = (unsigned char)c;
+ glp_write(mpl->prt_fp, buf, 1);
+ }
+#endif
+ return;
+}
+
+static void print_text(MPL *mpl, char *fmt, ...)
+{ va_list arg;
+ char buf[OUTBUF_SIZE], *c;
+ va_start(arg, fmt);
+ vsprintf(buf, fmt, arg);
+ xassert(strlen(buf) < sizeof(buf));
+ va_end(arg);
+ for (c = buf; *c != '\0'; c++) print_char(mpl, *c);
+ return;
+}
+#endif
+
+static int printf_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ PRINTF *prt = (PRINTF *)info;
+ PRINTF1 *entry;
+ SYMBOL *sym;
+ char fmt[MAX_LENGTH+1], *c, *from, save;
+ /* evaluate format control string */
+ sym = eval_symbolic(mpl, prt->fmt);
+ if (sym->str == NULL)
+ sprintf(fmt, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, fmt);
+ delete_symbol(mpl, sym);
+ /* scan format control string and perform formatting output */
+ entry = prt->list;
+ for (c = fmt; *c != '\0'; c++)
+ { if (*c == '%')
+ { /* scan format specifier */
+ from = c++;
+ if (*c == '%')
+ { print_char(mpl, '%');
+ continue;
+ }
+ if (entry == NULL) break;
+ /* scan optional flags */
+ while (*c == '-' || *c == '+' || *c == ' ' || *c == '#' ||
+ *c == '0') c++;
+ /* scan optional minimum field width */
+ while (isdigit((unsigned char)*c)) c++;
+ /* scan optional precision */
+ if (*c == '.')
+ { c++;
+ while (isdigit((unsigned char)*c)) c++;
+ }
+ /* scan conversion specifier and perform formatting */
+ save = *(c+1), *(c+1) = '\0';
+ if (*c == 'd' || *c == 'i' || *c == 'e' || *c == 'E' ||
+ *c == 'f' || *c == 'F' || *c == 'g' || *c == 'G')
+ { /* the specifier requires numeric value */
+ double value;
+ xassert(entry != NULL);
+ switch (entry->code->type)
+ { case A_NUMERIC:
+ value = eval_numeric(mpl, entry->code);
+ break;
+ case A_SYMBOLIC:
+ sym = eval_symbolic(mpl, entry->code);
+ if (sym->str != NULL)
+ error(mpl, "cannot convert %s to floating-point"
+ " number", format_symbol(mpl, sym));
+ value = sym->num;
+ delete_symbol(mpl, sym);
+ break;
+ case A_LOGICAL:
+ if (eval_logical(mpl, entry->code))
+ value = 1.0;
+ else
+ value = 0.0;
+ break;
+ default:
+ xassert(entry != entry);
+ }
+ if (*c == 'd' || *c == 'i')
+ { double int_max = (double)INT_MAX;
+ if (!(-int_max <= value && value <= +int_max))
+ error(mpl, "cannot convert %.*g to integer",
+ DBL_DIG, value);
+ print_text(mpl, from, (int)floor(value + 0.5));
+ }
+ else
+ print_text(mpl, from, value);
+ }
+ else if (*c == 's')
+ { /* the specifier requires symbolic value */
+ char value[MAX_LENGTH+1];
+ switch (entry->code->type)
+ { case A_NUMERIC:
+ sprintf(value, "%.*g", DBL_DIG, eval_numeric(mpl,
+ entry->code));
+ break;
+ case A_LOGICAL:
+ if (eval_logical(mpl, entry->code))
+ strcpy(value, "T");
+ else
+ strcpy(value, "F");
+ break;
+ case A_SYMBOLIC:
+ sym = eval_symbolic(mpl, entry->code);
+ if (sym->str == NULL)
+ sprintf(value, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, value);
+ delete_symbol(mpl, sym);
+ break;
+ default:
+ xassert(entry != entry);
+ }
+ print_text(mpl, from, value);
+ }
+ else
+ error(mpl, "format specifier missing or invalid");
+ *(c+1) = save;
+ entry = entry->next;
+ }
+ else if (*c == '\\')
+ { /* write some control character */
+ c++;
+ if (*c == 't')
+ print_char(mpl, '\t');
+ else if (*c == 'n')
+ print_char(mpl, '\n');
+#if 1 /* 28/X-2010 */
+ else if (*c == '\0')
+ { /* format string ends with backslash */
+ error(mpl, "invalid use of escape character \\ in format"
+ " control string");
+ }
+#endif
+ else
+ print_char(mpl, *c);
+ }
+ else
+ { /* write character without formatting */
+ print_char(mpl, *c);
+ }
+ }
+ return 0;
+}
+
+#if 0 /* 14/VII-2006 */
+void execute_printf(MPL *mpl, PRINTF *prt)
+{ loop_within_domain(mpl, prt->domain, prt, printf_func);
+ return;
+}
+#else
+void execute_printf(MPL *mpl, PRINTF *prt)
+{ if (prt->fname == NULL)
+ { /* switch to the standard output */
+ if (mpl->prt_fp != NULL)
+ { glp_close(mpl->prt_fp), mpl->prt_fp = NULL;
+ xfree(mpl->prt_file), mpl->prt_file = NULL;
+ }
+ }
+ else
+ { /* evaluate file name string */
+ SYMBOL *sym;
+ char fname[MAX_LENGTH+1];
+ sym = eval_symbolic(mpl, prt->fname);
+ if (sym->str == NULL)
+ sprintf(fname, "%.*g", DBL_DIG, sym->num);
+ else
+ fetch_string(mpl, sym->str, fname);
+ delete_symbol(mpl, sym);
+ /* close the current print file, if necessary */
+ if (mpl->prt_fp != NULL &&
+ (!prt->app || strcmp(mpl->prt_file, fname) != 0))
+ { glp_close(mpl->prt_fp), mpl->prt_fp = NULL;
+ xfree(mpl->prt_file), mpl->prt_file = NULL;
+ }
+ /* open the specified print file, if necessary */
+ if (mpl->prt_fp == NULL)
+ { mpl->prt_fp = glp_open(fname, prt->app ? "a" : "w");
+ if (mpl->prt_fp == NULL)
+ error(mpl, "unable to open '%s' for writing - %s",
+ fname, get_err_msg());
+ mpl->prt_file = xmalloc(strlen(fname)+1);
+ strcpy(mpl->prt_file, fname);
+ }
+ }
+ loop_within_domain(mpl, prt->domain, prt, printf_func);
+ if (mpl->prt_fp != NULL)
+ {
+#if 0 /* FIXME */
+ xfflush(mpl->prt_fp);
+#endif
+ if (glp_ioerr(mpl->prt_fp))
+ error(mpl, "writing error to '%s' - %s", mpl->prt_file,
+ get_err_msg());
+ }
+ return;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- clean_printf - clean printf statement.
+--
+-- This routine cleans specified printf statement that assumes deleting
+-- all stuff dynamically allocated on generating/postsolving phase. */
+
+void clean_printf(MPL *mpl, PRINTF *prt)
+{ PRINTF1 *p;
+ /* clean subscript domain */
+ clean_domain(mpl, prt->domain);
+ /* clean pseudo-code for computing format string */
+ clean_code(mpl, prt->fmt);
+ /* clean printf list */
+ for (p = prt->list; p != NULL; p = p->next)
+ { /* clean pseudo-code for computing value to be printed */
+ clean_code(mpl, p->code);
+ }
+#if 1 /* 14/VII-2006 */
+ /* clean pseudo-code for computing file name string */
+ clean_code(mpl, prt->fname);
+#endif
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- execute_for - execute for statement.
+--
+-- This routine executes specified for statement. */
+
+static int for_func(MPL *mpl, void *info)
+{ /* this is auxiliary routine to work within domain scope */
+ FOR *fur = (FOR *)info;
+ STATEMENT *stmt, *save;
+ save = mpl->stmt;
+ for (stmt = fur->list; stmt != NULL; stmt = stmt->next)
+ execute_statement(mpl, stmt);
+ mpl->stmt = save;
+ return 0;
+}
+
+void execute_for(MPL *mpl, FOR *fur)
+{ loop_within_domain(mpl, fur->domain, fur, for_func);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_for - clean for statement.
+--
+-- This routine cleans specified for statement that assumes deleting all
+-- stuff dynamically allocated on generating/postsolving phase. */
+
+void clean_for(MPL *mpl, FOR *fur)
+{ STATEMENT *stmt;
+ /* clean subscript domain */
+ clean_domain(mpl, fur->domain);
+ /* clean all sub-statements */
+ for (stmt = fur->list; stmt != NULL; stmt = stmt->next)
+ clean_statement(mpl, stmt);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- execute_statement - execute specified model statement.
+--
+-- This routine executes specified model statement. */
+
+void execute_statement(MPL *mpl, STATEMENT *stmt)
+{ mpl->stmt = stmt;
+ switch (stmt->type)
+ { case A_SET:
+ case A_PARAMETER:
+ case A_VARIABLE:
+ break;
+ case A_CONSTRAINT:
+ xprintf("Generating %s...\n", stmt->u.con->name);
+ eval_whole_con(mpl, stmt->u.con);
+ break;
+ case A_TABLE:
+ switch (stmt->u.tab->type)
+ { case A_INPUT:
+ xprintf("Reading %s...\n", stmt->u.tab->name);
+ break;
+ case A_OUTPUT:
+ xprintf("Writing %s...\n", stmt->u.tab->name);
+ break;
+ default:
+ xassert(stmt != stmt);
+ }
+ execute_table(mpl, stmt->u.tab);
+ break;
+ case A_SOLVE:
+ break;
+ case A_CHECK:
+ xprintf("Checking (line %d)...\n", stmt->line);
+ execute_check(mpl, stmt->u.chk);
+ break;
+ case A_DISPLAY:
+ write_text(mpl, "Display statement at line %d\n",
+ stmt->line);
+ execute_display(mpl, stmt->u.dpy);
+ break;
+ case A_PRINTF:
+ execute_printf(mpl, stmt->u.prt);
+ break;
+ case A_FOR:
+ execute_for(mpl, stmt->u.fur);
+ break;
+ default:
+ xassert(stmt != stmt);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_statement - clean specified model statement.
+--
+-- This routine cleans specified model statement that assumes deleting
+-- all stuff dynamically allocated on generating/postsolving phase. */
+
+void clean_statement(MPL *mpl, STATEMENT *stmt)
+{ switch(stmt->type)
+ { case A_SET:
+ clean_set(mpl, stmt->u.set); break;
+ case A_PARAMETER:
+ clean_parameter(mpl, stmt->u.par); break;
+ case A_VARIABLE:
+ clean_variable(mpl, stmt->u.var); break;
+ case A_CONSTRAINT:
+ clean_constraint(mpl, stmt->u.con); break;
+#if 1 /* 11/II-2008 */
+ case A_TABLE:
+ clean_table(mpl, stmt->u.tab); break;
+#endif
+ case A_SOLVE:
+ break;
+ case A_CHECK:
+ clean_check(mpl, stmt->u.chk); break;
+ case A_DISPLAY:
+ clean_display(mpl, stmt->u.dpy); break;
+ case A_PRINTF:
+ clean_printf(mpl, stmt->u.prt); break;
+ case A_FOR:
+ clean_for(mpl, stmt->u.fur); break;
+ default:
+ xassert(stmt != stmt);
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl4.c b/test/monniaux/glpk-4.65/src/mpl/mpl4.c
new file mode 100644
index 00000000..6e80499c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl4.c
@@ -0,0 +1,1426 @@
+/* mpl4.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+
+#define xfault xerror
+#define xfprintf glp_format
+#define dmp_create_poolx(size) dmp_create_pool()
+
+/**********************************************************************/
+/* * * GENERATING AND POSTSOLVING MODEL * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- alloc_content - allocate content arrays for all model objects.
+--
+-- This routine allocates content arrays for all existing model objects
+-- and thereby finalizes creating model.
+--
+-- This routine must be called immediately after reading model section,
+-- i.e. before reading data section or generating model. */
+
+void alloc_content(MPL *mpl)
+{ STATEMENT *stmt;
+ /* walk through all model statements */
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { switch (stmt->type)
+ { case A_SET:
+ /* model set */
+ xassert(stmt->u.set->array == NULL);
+ stmt->u.set->array = create_array(mpl, A_ELEMSET,
+ stmt->u.set->dim);
+ break;
+ case A_PARAMETER:
+ /* model parameter */
+ xassert(stmt->u.par->array == NULL);
+ switch (stmt->u.par->type)
+ { case A_NUMERIC:
+ case A_INTEGER:
+ case A_BINARY:
+ stmt->u.par->array = create_array(mpl, A_NUMERIC,
+ stmt->u.par->dim);
+ break;
+ case A_SYMBOLIC:
+ stmt->u.par->array = create_array(mpl, A_SYMBOLIC,
+ stmt->u.par->dim);
+ break;
+ default:
+ xassert(stmt != stmt);
+ }
+ break;
+ case A_VARIABLE:
+ /* model variable */
+ xassert(stmt->u.var->array == NULL);
+ stmt->u.var->array = create_array(mpl, A_ELEMVAR,
+ stmt->u.var->dim);
+ break;
+ case A_CONSTRAINT:
+ /* model constraint/objective */
+ xassert(stmt->u.con->array == NULL);
+ stmt->u.con->array = create_array(mpl, A_ELEMCON,
+ stmt->u.con->dim);
+ break;
+#if 1 /* 11/II-2008 */
+ case A_TABLE:
+#endif
+ case A_SOLVE:
+ case A_CHECK:
+ case A_DISPLAY:
+ case A_PRINTF:
+ case A_FOR:
+ /* functional statements have no content array */
+ break;
+ default:
+ xassert(stmt != stmt);
+ }
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- generate_model - generate model.
+--
+-- This routine executes the model statements which precede the solve
+-- statement. */
+
+void generate_model(MPL *mpl)
+{ STATEMENT *stmt;
+ xassert(!mpl->flag_p);
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { execute_statement(mpl, stmt);
+ if (mpl->stmt->type == A_SOLVE) break;
+ }
+ mpl->stmt = stmt;
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- build_problem - build problem instance.
+--
+-- This routine builds lists of rows and columns for problem instance,
+-- which corresponds to the generated model. */
+
+void build_problem(MPL *mpl)
+{ STATEMENT *stmt;
+ MEMBER *memb;
+ VARIABLE *v;
+ CONSTRAINT *c;
+ FORMULA *t;
+ int i, j;
+ xassert(mpl->m == 0);
+ xassert(mpl->n == 0);
+ xassert(mpl->row == NULL);
+ xassert(mpl->col == NULL);
+ /* check that all elemental variables has zero column numbers */
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { if (stmt->type == A_VARIABLE)
+ { v = stmt->u.var;
+ for (memb = v->array->head; memb != NULL; memb = memb->next)
+ xassert(memb->value.var->j == 0);
+ }
+ }
+ /* assign row numbers to elemental constraints and objectives */
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { if (stmt->type == A_CONSTRAINT)
+ { c = stmt->u.con;
+ for (memb = c->array->head; memb != NULL; memb = memb->next)
+ { xassert(memb->value.con->i == 0);
+ memb->value.con->i = ++mpl->m;
+ /* walk through linear form and mark elemental variables,
+ which are referenced at least once */
+ for (t = memb->value.con->form; t != NULL; t = t->next)
+ { xassert(t->var != NULL);
+ t->var->memb->value.var->j = -1;
+ }
+ }
+ }
+ }
+ /* assign column numbers to marked elemental variables */
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { if (stmt->type == A_VARIABLE)
+ { v = stmt->u.var;
+ for (memb = v->array->head; memb != NULL; memb = memb->next)
+ if (memb->value.var->j != 0) memb->value.var->j =
+ ++mpl->n;
+ }
+ }
+ /* build list of rows */
+ mpl->row = xcalloc(1+mpl->m, sizeof(ELEMCON *));
+ for (i = 1; i <= mpl->m; i++) mpl->row[i] = NULL;
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { if (stmt->type == A_CONSTRAINT)
+ { c = stmt->u.con;
+ for (memb = c->array->head; memb != NULL; memb = memb->next)
+ { i = memb->value.con->i;
+ xassert(1 <= i && i <= mpl->m);
+ xassert(mpl->row[i] == NULL);
+ mpl->row[i] = memb->value.con;
+ }
+ }
+ }
+ for (i = 1; i <= mpl->m; i++) xassert(mpl->row[i] != NULL);
+ /* build list of columns */
+ mpl->col = xcalloc(1+mpl->n, sizeof(ELEMVAR *));
+ for (j = 1; j <= mpl->n; j++) mpl->col[j] = NULL;
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ { if (stmt->type == A_VARIABLE)
+ { v = stmt->u.var;
+ for (memb = v->array->head; memb != NULL; memb = memb->next)
+ { j = memb->value.var->j;
+ if (j == 0) continue;
+ xassert(1 <= j && j <= mpl->n);
+ xassert(mpl->col[j] == NULL);
+ mpl->col[j] = memb->value.var;
+ }
+ }
+ }
+ for (j = 1; j <= mpl->n; j++) xassert(mpl->col[j] != NULL);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- postsolve_model - postsolve model.
+--
+-- This routine executes the model statements which follow the solve
+-- statement. */
+
+void postsolve_model(MPL *mpl)
+{ STATEMENT *stmt;
+ xassert(!mpl->flag_p);
+ mpl->flag_p = 1;
+ for (stmt = mpl->stmt; stmt != NULL; stmt = stmt->next)
+ execute_statement(mpl, stmt);
+ mpl->stmt = NULL;
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- clean_model - clean model content.
+--
+-- This routine cleans the model content that assumes deleting all stuff
+-- dynamically allocated on generating/postsolving phase.
+--
+-- Actually cleaning model content is not needed. This function is used
+-- mainly to be sure that there were no logical errors on using dynamic
+-- memory pools during the generation phase.
+--
+-- NOTE: This routine must not be called if any errors were detected on
+-- the generation phase. */
+
+void clean_model(MPL *mpl)
+{ STATEMENT *stmt;
+ for (stmt = mpl->model; stmt != NULL; stmt = stmt->next)
+ clean_statement(mpl, stmt);
+ /* check that all atoms have been returned to their pools */
+ if (dmp_in_use(mpl->strings) != 0)
+ error(mpl, "internal logic error: %d string segment(s) were lo"
+ "st", dmp_in_use(mpl->strings));
+ if (dmp_in_use(mpl->symbols) != 0)
+ error(mpl, "internal logic error: %d symbol(s) were lost",
+ dmp_in_use(mpl->symbols));
+ if (dmp_in_use(mpl->tuples) != 0)
+ error(mpl, "internal logic error: %d n-tuple component(s) were"
+ " lost", dmp_in_use(mpl->tuples));
+ if (dmp_in_use(mpl->arrays) != 0)
+ error(mpl, "internal logic error: %d array(s) were lost",
+ dmp_in_use(mpl->arrays));
+ if (dmp_in_use(mpl->members) != 0)
+ error(mpl, "internal logic error: %d array member(s) were lost"
+ , dmp_in_use(mpl->members));
+ if (dmp_in_use(mpl->elemvars) != 0)
+ error(mpl, "internal logic error: %d elemental variable(s) wer"
+ "e lost", dmp_in_use(mpl->elemvars));
+ if (dmp_in_use(mpl->formulae) != 0)
+ error(mpl, "internal logic error: %d linear term(s) were lost",
+ dmp_in_use(mpl->formulae));
+ if (dmp_in_use(mpl->elemcons) != 0)
+ error(mpl, "internal logic error: %d elemental constraint(s) w"
+ "ere lost", dmp_in_use(mpl->elemcons));
+ return;
+}
+
+/**********************************************************************/
+/* * * INPUT/OUTPUT * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- open_input - open input text file.
+--
+-- This routine opens the input text file for scanning. */
+
+void open_input(MPL *mpl, char *file)
+{ mpl->line = 0;
+ mpl->c = '\n';
+ mpl->token = 0;
+ mpl->imlen = 0;
+ mpl->image[0] = '\0';
+ mpl->value = 0.0;
+ mpl->b_token = T_EOF;
+ mpl->b_imlen = 0;
+ mpl->b_image[0] = '\0';
+ mpl->b_value = 0.0;
+ mpl->f_dots = 0;
+ mpl->f_scan = 0;
+ mpl->f_token = 0;
+ mpl->f_imlen = 0;
+ mpl->f_image[0] = '\0';
+ mpl->f_value = 0.0;
+ memset(mpl->context, ' ', CONTEXT_SIZE);
+ mpl->c_ptr = 0;
+ xassert(mpl->in_fp == NULL);
+ mpl->in_fp = glp_open(file, "r");
+ if (mpl->in_fp == NULL)
+ error(mpl, "unable to open %s - %s", file, get_err_msg());
+ mpl->in_file = file;
+ /* scan the very first character */
+ get_char(mpl);
+ /* scan the very first token */
+ get_token(mpl);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- read_char - read next character from input text file.
+--
+-- This routine returns a next ASCII character read from the input text
+-- file. If the end of file has been reached, EOF is returned. */
+
+int read_char(MPL *mpl)
+{ int c;
+ xassert(mpl->in_fp != NULL);
+ c = glp_getc(mpl->in_fp);
+ if (c < 0)
+ { if (glp_ioerr(mpl->in_fp))
+ error(mpl, "read error on %s - %s", mpl->in_file,
+ get_err_msg());
+ c = EOF;
+ }
+ return c;
+}
+
+/*----------------------------------------------------------------------
+-- close_input - close input text file.
+--
+-- This routine closes the input text file. */
+
+void close_input(MPL *mpl)
+{ xassert(mpl->in_fp != NULL);
+ glp_close(mpl->in_fp);
+ mpl->in_fp = NULL;
+ mpl->in_file = NULL;
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- open_output - open output text file.
+--
+-- This routine opens the output text file for writing data produced by
+-- display and printf statements. */
+
+void open_output(MPL *mpl, char *file)
+{ xassert(mpl->out_fp == NULL);
+ if (file == NULL)
+ { file = "<stdout>";
+ mpl->out_fp = (void *)stdout;
+ }
+ else
+ { mpl->out_fp = glp_open(file, "w");
+ if (mpl->out_fp == NULL)
+ error(mpl, "unable to create %s - %s", file, get_err_msg());
+ }
+ mpl->out_file = xmalloc(strlen(file)+1);
+ strcpy(mpl->out_file, file);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- write_char - write next character to output text file.
+--
+-- This routine writes an ASCII character to the output text file. */
+
+void write_char(MPL *mpl, int c)
+{ xassert(mpl->out_fp != NULL);
+ if (mpl->out_fp == (void *)stdout)
+ xprintf("%c", c);
+ else
+ xfprintf(mpl->out_fp, "%c", c);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- write_text - format and write text to output text file.
+--
+-- This routine formats a text using the format control string and then
+-- writes this text to the output text file. */
+
+void write_text(MPL *mpl, char *fmt, ...)
+{ va_list arg;
+ char buf[OUTBUF_SIZE], *c;
+ va_start(arg, fmt);
+ vsprintf(buf, fmt, arg);
+ xassert(strlen(buf) < sizeof(buf));
+ va_end(arg);
+ for (c = buf; *c != '\0'; c++) write_char(mpl, *c);
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- flush_output - finalize writing data to output text file.
+--
+-- This routine finalizes writing data to the output text file. */
+
+void flush_output(MPL *mpl)
+{ xassert(mpl->out_fp != NULL);
+ if (mpl->out_fp != (void *)stdout)
+ {
+#if 0 /* FIXME */
+ xfflush(mpl->out_fp);
+#endif
+ if (glp_ioerr(mpl->out_fp))
+ error(mpl, "write error on %s - %s", mpl->out_file,
+ get_err_msg());
+ }
+ return;
+}
+
+/**********************************************************************/
+/* * * SOLVER INTERFACE * * */
+/**********************************************************************/
+
+/*----------------------------------------------------------------------
+-- error - print error message and terminate model processing.
+--
+-- This routine formats and prints an error message and then terminates
+-- model processing. */
+
+void error(MPL *mpl, char *fmt, ...)
+{ va_list arg;
+ char msg[4095+1];
+ va_start(arg, fmt);
+ vsprintf(msg, fmt, arg);
+ xassert(strlen(msg) < sizeof(msg));
+ va_end(arg);
+ switch (mpl->phase)
+ { case 1:
+ case 2:
+ /* translation phase */
+ xprintf("%s:%d: %s\n",
+ mpl->in_file == NULL ? "(unknown)" : mpl->in_file,
+ mpl->line, msg);
+ print_context(mpl);
+ break;
+ case 3:
+ /* generation/postsolve phase */
+ xprintf("%s:%d: %s\n",
+ mpl->mod_file == NULL ? "(unknown)" : mpl->mod_file,
+ mpl->stmt == NULL ? 0 : mpl->stmt->line, msg);
+ break;
+ default:
+ xassert(mpl != mpl);
+ }
+ mpl->phase = 4;
+ longjmp(mpl->jump, 1);
+ /* no return */
+}
+
+/*----------------------------------------------------------------------
+-- warning - print warning message and continue model processing.
+--
+-- This routine formats and prints a warning message and returns to the
+-- calling program. */
+
+void warning(MPL *mpl, char *fmt, ...)
+{ va_list arg;
+ char msg[4095+1];
+ va_start(arg, fmt);
+ vsprintf(msg, fmt, arg);
+ xassert(strlen(msg) < sizeof(msg));
+ va_end(arg);
+ switch (mpl->phase)
+ { case 1:
+ case 2:
+ /* translation phase */
+ xprintf("%s:%d: warning: %s\n",
+ mpl->in_file == NULL ? "(unknown)" : mpl->in_file,
+ mpl->line, msg);
+ break;
+ case 3:
+ /* generation/postsolve phase */
+ xprintf("%s:%d: warning: %s\n",
+ mpl->mod_file == NULL ? "(unknown)" : mpl->mod_file,
+ mpl->stmt == NULL ? 0 : mpl->stmt->line, msg);
+ break;
+ default:
+ xassert(mpl != mpl);
+ }
+ return;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_initialize - create and initialize translator database.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- MPL *mpl_initialize(void);
+--
+-- *Description*
+--
+-- The routine mpl_initialize creates and initializes the database used
+-- by the GNU MathProg translator.
+--
+-- *Returns*
+--
+-- The routine returns a pointer to the database created. */
+
+MPL *mpl_initialize(void)
+{ MPL *mpl;
+ mpl = xmalloc(sizeof(MPL));
+ /* scanning segment */
+ mpl->line = 0;
+ mpl->c = 0;
+ mpl->token = 0;
+ mpl->imlen = 0;
+ mpl->image = xcalloc(MAX_LENGTH+1, sizeof(char));
+ mpl->image[0] = '\0';
+ mpl->value = 0.0;
+ mpl->b_token = 0;
+ mpl->b_imlen = 0;
+ mpl->b_image = xcalloc(MAX_LENGTH+1, sizeof(char));
+ mpl->b_image[0] = '\0';
+ mpl->b_value = 0.0;
+ mpl->f_dots = 0;
+ mpl->f_scan = 0;
+ mpl->f_token = 0;
+ mpl->f_imlen = 0;
+ mpl->f_image = xcalloc(MAX_LENGTH+1, sizeof(char));
+ mpl->f_image[0] = '\0';
+ mpl->f_value = 0.0;
+ mpl->context = xcalloc(CONTEXT_SIZE, sizeof(char));
+ memset(mpl->context, ' ', CONTEXT_SIZE);
+ mpl->c_ptr = 0;
+ mpl->flag_d = 0;
+ /* translating segment */
+ mpl->pool = dmp_create_poolx(0);
+ mpl->tree = avl_create_tree(avl_strcmp, NULL);
+ mpl->model = NULL;
+ mpl->flag_x = 0;
+ mpl->as_within = 0;
+ mpl->as_in = 0;
+ mpl->as_binary = 0;
+ mpl->flag_s = 0;
+ /* common segment */
+ mpl->strings = dmp_create_poolx(sizeof(STRING));
+ mpl->symbols = dmp_create_poolx(sizeof(SYMBOL));
+ mpl->tuples = dmp_create_poolx(sizeof(TUPLE));
+ mpl->arrays = dmp_create_poolx(sizeof(ARRAY));
+ mpl->members = dmp_create_poolx(sizeof(MEMBER));
+ mpl->elemvars = dmp_create_poolx(sizeof(ELEMVAR));
+ mpl->formulae = dmp_create_poolx(sizeof(FORMULA));
+ mpl->elemcons = dmp_create_poolx(sizeof(ELEMCON));
+ mpl->a_list = NULL;
+ mpl->sym_buf = xcalloc(255+1, sizeof(char));
+ mpl->sym_buf[0] = '\0';
+ mpl->tup_buf = xcalloc(255+1, sizeof(char));
+ mpl->tup_buf[0] = '\0';
+ /* generating/postsolving segment */
+ mpl->rand = rng_create_rand();
+ mpl->flag_p = 0;
+ mpl->stmt = NULL;
+#if 1 /* 11/II-2008 */
+ mpl->dca = NULL;
+#endif
+ mpl->m = 0;
+ mpl->n = 0;
+ mpl->row = NULL;
+ mpl->col = NULL;
+ /* input/output segment */
+ mpl->in_fp = NULL;
+ mpl->in_file = NULL;
+ mpl->out_fp = NULL;
+ mpl->out_file = NULL;
+ mpl->prt_fp = NULL;
+ mpl->prt_file = NULL;
+ /* solver interface segment */
+ if (setjmp(mpl->jump)) xassert(mpl != mpl);
+ mpl->phase = 0;
+ mpl->mod_file = NULL;
+ mpl->mpl_buf = xcalloc(255+1, sizeof(char));
+ mpl->mpl_buf[0] = '\0';
+ return mpl;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_read_model - read model section and optional data section.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_read_model(MPL *mpl, char *file, int skip_data);
+--
+-- *Description*
+--
+-- The routine mpl_read_model reads model section and optionally data
+-- section, which may follow the model section, from the text file,
+-- whose name is the character string file, performs translating model
+-- statements and data blocks, and stores all the information in the
+-- translator database.
+--
+-- The parameter skip_data is a flag. If the input file contains the
+-- data section and this flag is set, the data section is not read as
+-- if there were no data section and a warning message is issued. This
+-- allows reading the data section from another input file.
+--
+-- This routine should be called once after the routine mpl_initialize
+-- and before other API routines.
+--
+-- *Returns*
+--
+-- The routine mpl_read_model returns one the following codes:
+--
+-- 1 - translation successful. The input text file contains only model
+-- section. In this case the calling program may call the routine
+-- mpl_read_data to read data section from another file.
+-- 2 - translation successful. The input text file contains both model
+-- and data section.
+-- 4 - processing failed due to some errors. In this case the calling
+-- program should call the routine mpl_terminate to terminate model
+-- processing. */
+
+int mpl_read_model(MPL *mpl, char *file, int skip_data)
+{ if (mpl->phase != 0)
+ xfault("mpl_read_model: invalid call sequence\n");
+ if (file == NULL)
+ xfault("mpl_read_model: no input filename specified\n");
+ /* set up error handler */
+ if (setjmp(mpl->jump)) goto done;
+ /* translate model section */
+ mpl->phase = 1;
+ xprintf("Reading model section from %s...\n", file);
+ open_input(mpl, file);
+ model_section(mpl);
+ if (mpl->model == NULL)
+ error(mpl, "empty model section not allowed");
+ /* save name of the input text file containing model section for
+ error diagnostics during the generation phase */
+ mpl->mod_file = xcalloc(strlen(file)+1, sizeof(char));
+ strcpy(mpl->mod_file, mpl->in_file);
+ /* allocate content arrays for all model objects */
+ alloc_content(mpl);
+ /* optional data section may begin with the keyword 'data' */
+ if (is_keyword(mpl, "data"))
+ { if (skip_data)
+ { warning(mpl, "data section ignored");
+ goto skip;
+ }
+ mpl->flag_d = 1;
+ get_token(mpl /* data */);
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "semicolon missing where expected");
+ get_token(mpl /* ; */);
+ /* translate data section */
+ mpl->phase = 2;
+ xprintf("Reading data section from %s...\n", file);
+ data_section(mpl);
+ }
+ /* process end statement */
+ end_statement(mpl);
+skip: xprintf("%d line%s were read\n",
+ mpl->line, mpl->line == 1 ? "" : "s");
+ close_input(mpl);
+done: /* return to the calling program */
+ return mpl->phase;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_read_data - read data section.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_read_data(MPL *mpl, char *file);
+--
+-- *Description*
+--
+-- The routine mpl_read_data reads data section from the text file,
+-- whose name is the character string file, performs translating data
+-- blocks, and stores the data read in the translator database.
+--
+-- If this routine is used, it should be called once after the routine
+-- mpl_read_model and if the latter returned the code 1.
+--
+-- *Returns*
+--
+-- The routine mpl_read_data returns one of the following codes:
+--
+-- 2 - data section has been successfully processed.
+-- 4 - processing failed due to some errors. In this case the calling
+-- program should call the routine mpl_terminate to terminate model
+-- processing. */
+
+int mpl_read_data(MPL *mpl, char *file)
+#if 0 /* 02/X-2008 */
+{ if (mpl->phase != 1)
+#else
+{ if (!(mpl->phase == 1 || mpl->phase == 2))
+#endif
+ xfault("mpl_read_data: invalid call sequence\n");
+ if (file == NULL)
+ xfault("mpl_read_data: no input filename specified\n");
+ /* set up error handler */
+ if (setjmp(mpl->jump)) goto done;
+ /* process data section */
+ mpl->phase = 2;
+ xprintf("Reading data section from %s...\n", file);
+ mpl->flag_d = 1;
+ open_input(mpl, file);
+ /* in this case the keyword 'data' is optional */
+ if (is_literal(mpl, "data"))
+ { get_token(mpl /* data */);
+ if (mpl->token != T_SEMICOLON)
+ error(mpl, "semicolon missing where expected");
+ get_token(mpl /* ; */);
+ }
+ data_section(mpl);
+ /* process end statement */
+ end_statement(mpl);
+ xprintf("%d line%s were read\n",
+ mpl->line, mpl->line == 1 ? "" : "s");
+ close_input(mpl);
+done: /* return to the calling program */
+ return mpl->phase;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_generate - generate model.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_generate(MPL *mpl, char *file);
+--
+-- *Description*
+--
+-- The routine mpl_generate generates the model using its description
+-- stored in the translator database. This phase means generating all
+-- variables, constraints, and objectives, executing check and display
+-- statements, which precede the solve statement (if it is presented),
+-- and building the problem instance.
+--
+-- The character string file specifies the name of output text file, to
+-- which output produced by display statements should be written. It is
+-- allowed to specify NULL, in which case the output goes to stdout via
+-- the routine print.
+--
+-- This routine should be called once after the routine mpl_read_model
+-- or mpl_read_data and if one of the latters returned the code 2.
+--
+-- *Returns*
+--
+-- The routine mpl_generate returns one of the following codes:
+--
+-- 3 - model has been successfully generated. In this case the calling
+-- program may call other api routines to obtain components of the
+-- problem instance from the translator database.
+-- 4 - processing failed due to some errors. In this case the calling
+-- program should call the routine mpl_terminate to terminate model
+-- processing. */
+
+int mpl_generate(MPL *mpl, char *file)
+{ if (!(mpl->phase == 1 || mpl->phase == 2))
+ xfault("mpl_generate: invalid call sequence\n");
+ /* set up error handler */
+ if (setjmp(mpl->jump)) goto done;
+ /* generate model */
+ mpl->phase = 3;
+ open_output(mpl, file);
+ generate_model(mpl);
+ flush_output(mpl);
+ /* build problem instance */
+ build_problem(mpl);
+ /* generation phase has been finished */
+ xprintf("Model has been successfully generated\n");
+done: /* return to the calling program */
+ return mpl->phase;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_prob_name - obtain problem (model) name.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- char *mpl_get_prob_name(MPL *mpl);
+--
+-- *Returns*
+--
+-- The routine mpl_get_prob_name returns a pointer to internal buffer,
+-- which contains symbolic name of the problem (model).
+--
+-- *Note*
+--
+-- Currently MathProg has no feature to assign a symbolic name to the
+-- model. Therefore the routine mpl_get_prob_name tries to construct
+-- such name using the name of input text file containing model section,
+-- although this is not a good idea (due to portability problems). */
+
+char *mpl_get_prob_name(MPL *mpl)
+{ char *name = mpl->mpl_buf;
+ char *file = mpl->mod_file;
+ int k;
+ if (mpl->phase != 3)
+ xfault("mpl_get_prob_name: invalid call sequence\n");
+ for (;;)
+ { if (strchr(file, '/') != NULL)
+ file = strchr(file, '/') + 1;
+ else if (strchr(file, '\\') != NULL)
+ file = strchr(file, '\\') + 1;
+ else if (strchr(file, ':') != NULL)
+ file = strchr(file, ':') + 1;
+ else
+ break;
+ }
+ for (k = 0; ; k++)
+ { if (k == 255) break;
+ if (!(isalnum((unsigned char)*file) || *file == '_')) break;
+ name[k] = *file++;
+ }
+ if (k == 0)
+ strcpy(name, "Unknown");
+ else
+ name[k] = '\0';
+ xassert(strlen(name) <= 255);
+ return name;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_num_rows - determine number of rows.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_num_rows(MPL *mpl);
+--
+-- *Returns*
+--
+-- The routine mpl_get_num_rows returns total number of rows in the
+-- problem, where each row is an individual constraint or objective. */
+
+int mpl_get_num_rows(MPL *mpl)
+{ if (mpl->phase != 3)
+ xfault("mpl_get_num_rows: invalid call sequence\n");
+ return mpl->m;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_num_cols - determine number of columns.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_num_cols(MPL *mpl);
+--
+-- *Returns*
+--
+-- The routine mpl_get_num_cols returns total number of columns in the
+-- problem, where each column is an individual variable. */
+
+int mpl_get_num_cols(MPL *mpl)
+{ if (mpl->phase != 3)
+ xfault("mpl_get_num_cols: invalid call sequence\n");
+ return mpl->n;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_row_name - obtain row name.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- char *mpl_get_row_name(MPL *mpl, int i);
+--
+-- *Returns*
+--
+-- The routine mpl_get_row_name returns a pointer to internal buffer,
+-- which contains symbolic name of i-th row of the problem. */
+
+char *mpl_get_row_name(MPL *mpl, int i)
+{ char *name = mpl->mpl_buf, *t;
+ int len;
+ if (mpl->phase != 3)
+ xfault("mpl_get_row_name: invalid call sequence\n");
+ if (!(1 <= i && i <= mpl->m))
+ xfault("mpl_get_row_name: i = %d; row number out of range\n",
+ i);
+ strcpy(name, mpl->row[i]->con->name);
+ len = strlen(name);
+ xassert(len <= 255);
+ t = format_tuple(mpl, '[', mpl->row[i]->memb->tuple);
+ while (*t)
+ { if (len == 255) break;
+ name[len++] = *t++;
+ }
+ name[len] = '\0';
+ if (len == 255) strcpy(name+252, "...");
+ xassert(strlen(name) <= 255);
+ return name;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_row_kind - determine row kind.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_row_kind(MPL *mpl, int i);
+--
+-- *Returns*
+--
+-- The routine mpl_get_row_kind returns the kind of i-th row, which can
+-- be one of the following:
+--
+-- MPL_ST - non-free (constraint) row;
+-- MPL_MIN - free (objective) row to be minimized;
+-- MPL_MAX - free (objective) row to be maximized. */
+
+int mpl_get_row_kind(MPL *mpl, int i)
+{ int kind;
+ if (mpl->phase != 3)
+ xfault("mpl_get_row_kind: invalid call sequence\n");
+ if (!(1 <= i && i <= mpl->m))
+ xfault("mpl_get_row_kind: i = %d; row number out of range\n",
+ i);
+ switch (mpl->row[i]->con->type)
+ { case A_CONSTRAINT:
+ kind = MPL_ST; break;
+ case A_MINIMIZE:
+ kind = MPL_MIN; break;
+ case A_MAXIMIZE:
+ kind = MPL_MAX; break;
+ default:
+ xassert(mpl != mpl);
+ }
+ return kind;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_row_bnds - obtain row bounds.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_row_bnds(MPL *mpl, int i, double *lb, double *ub);
+--
+-- *Description*
+--
+-- The routine mpl_get_row_bnds stores lower and upper bounds of i-th
+-- row of the problem to the locations, which the parameters lb and ub
+-- point to, respectively. Besides the routine returns the type of the
+-- i-th row.
+--
+-- If some of the parameters lb and ub is NULL, the corresponding bound
+-- value is not stored.
+--
+-- Types and bounds have the following meaning:
+--
+-- Type Bounds Note
+-- -----------------------------------------------------------
+-- MPL_FR -inf < f(x) < +inf Free linear form
+-- MPL_LO lb <= f(x) < +inf Inequality f(x) >= lb
+-- MPL_UP -inf < f(x) <= ub Inequality f(x) <= ub
+-- MPL_DB lb <= f(x) <= ub Inequality lb <= f(x) <= ub
+-- MPL_FX f(x) = lb Equality f(x) = lb
+--
+-- where f(x) is the corresponding linear form of the i-th row.
+--
+-- If the row has no lower bound, *lb is set to zero; if the row has
+-- no upper bound, *ub is set to zero; and if the row is of fixed type,
+-- both *lb and *ub are set to the same value.
+--
+-- *Returns*
+--
+-- The routine returns the type of the i-th row as it is stated in the
+-- table above. */
+
+int mpl_get_row_bnds(MPL *mpl, int i, double *_lb, double *_ub)
+{ ELEMCON *con;
+ int type;
+ double lb, ub;
+ if (mpl->phase != 3)
+ xfault("mpl_get_row_bnds: invalid call sequence\n");
+ if (!(1 <= i && i <= mpl->m))
+ xfault("mpl_get_row_bnds: i = %d; row number out of range\n",
+ i);
+ con = mpl->row[i];
+#if 0 /* 21/VII-2006 */
+ if (con->con->lbnd == NULL && con->con->ubnd == NULL)
+ type = MPL_FR, lb = ub = 0.0;
+ else if (con->con->ubnd == NULL)
+ type = MPL_LO, lb = con->lbnd, ub = 0.0;
+ else if (con->con->lbnd == NULL)
+ type = MPL_UP, lb = 0.0, ub = con->ubnd;
+ else if (con->con->lbnd != con->con->ubnd)
+ type = MPL_DB, lb = con->lbnd, ub = con->ubnd;
+ else
+ type = MPL_FX, lb = ub = con->lbnd;
+#else
+ lb = (con->con->lbnd == NULL ? -DBL_MAX : con->lbnd);
+ ub = (con->con->ubnd == NULL ? +DBL_MAX : con->ubnd);
+ if (lb == -DBL_MAX && ub == +DBL_MAX)
+ type = MPL_FR, lb = ub = 0.0;
+ else if (ub == +DBL_MAX)
+ type = MPL_LO, ub = 0.0;
+ else if (lb == -DBL_MAX)
+ type = MPL_UP, lb = 0.0;
+ else if (con->con->lbnd != con->con->ubnd)
+ type = MPL_DB;
+ else
+ type = MPL_FX;
+#endif
+ if (_lb != NULL) *_lb = lb;
+ if (_ub != NULL) *_ub = ub;
+ return type;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_mat_row - obtain row of the constraint matrix.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]);
+--
+-- *Description*
+--
+-- The routine mpl_get_mat_row stores column indices and numeric values
+-- of constraint coefficients for the i-th row to locations ndx[1], ...,
+-- ndx[len] and val[1], ..., val[len], respectively, where 0 <= len <= n
+-- is number of (structural) non-zero constraint coefficients, and n is
+-- number of columns in the problem.
+--
+-- If the parameter ndx is NULL, column indices are not stored. If the
+-- parameter val is NULL, numeric values are not stored.
+--
+-- Note that free rows may have constant terms, which are not part of
+-- the constraint matrix and therefore not reported by this routine. The
+-- constant term of a particular row can be obtained, if necessary, via
+-- the routine mpl_get_row_c0.
+--
+-- *Returns*
+--
+-- The routine mpl_get_mat_row returns len, which is length of i-th row
+-- of the constraint matrix (i.e. number of non-zero coefficients). */
+
+int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[])
+{ FORMULA *term;
+ int len = 0;
+ if (mpl->phase != 3)
+ xfault("mpl_get_mat_row: invalid call sequence\n");
+ if (!(1 <= i && i <= mpl->m))
+ xfault("mpl_get_mat_row: i = %d; row number out of range\n",
+ i);
+ for (term = mpl->row[i]->form; term != NULL; term = term->next)
+ { xassert(term->var != NULL);
+ len++;
+ xassert(len <= mpl->n);
+ if (ndx != NULL) ndx[len] = term->var->j;
+ if (val != NULL) val[len] = term->coef;
+ }
+ return len;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_row_c0 - obtain constant term of free row.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- double mpl_get_row_c0(MPL *mpl, int i);
+--
+-- *Returns*
+--
+-- The routine mpl_get_row_c0 returns numeric value of constant term of
+-- i-th row.
+--
+-- Note that only free rows may have non-zero constant terms. Therefore
+-- if i-th row is not free, the routine returns zero. */
+
+double mpl_get_row_c0(MPL *mpl, int i)
+{ ELEMCON *con;
+ double c0;
+ if (mpl->phase != 3)
+ xfault("mpl_get_row_c0: invalid call sequence\n");
+ if (!(1 <= i && i <= mpl->m))
+ xfault("mpl_get_row_c0: i = %d; row number out of range\n",
+ i);
+ con = mpl->row[i];
+ if (con->con->lbnd == NULL && con->con->ubnd == NULL)
+ c0 = - con->lbnd;
+ else
+ c0 = 0.0;
+ return c0;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_col_name - obtain column name.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- char *mpl_get_col_name(MPL *mpl, int j);
+--
+-- *Returns*
+--
+-- The routine mpl_get_col_name returns a pointer to internal buffer,
+-- which contains symbolic name of j-th column of the problem. */
+
+char *mpl_get_col_name(MPL *mpl, int j)
+{ char *name = mpl->mpl_buf, *t;
+ int len;
+ if (mpl->phase != 3)
+ xfault("mpl_get_col_name: invalid call sequence\n");
+ if (!(1 <= j && j <= mpl->n))
+ xfault("mpl_get_col_name: j = %d; column number out of range\n"
+ , j);
+ strcpy(name, mpl->col[j]->var->name);
+ len = strlen(name);
+ xassert(len <= 255);
+ t = format_tuple(mpl, '[', mpl->col[j]->memb->tuple);
+ while (*t)
+ { if (len == 255) break;
+ name[len++] = *t++;
+ }
+ name[len] = '\0';
+ if (len == 255) strcpy(name+252, "...");
+ xassert(strlen(name) <= 255);
+ return name;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_col_kind - determine column kind.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_col_kind(MPL *mpl, int j);
+--
+-- *Returns*
+--
+-- The routine mpl_get_col_kind returns the kind of j-th column, which
+-- can be one of the following:
+--
+-- MPL_NUM - continuous variable;
+-- MPL_INT - integer variable;
+-- MPL_BIN - binary variable.
+--
+-- Note that column kinds are defined independently on type and bounds
+-- (reported by the routine mpl_get_col_bnds) of corresponding columns.
+-- This means, in particular, that bounds of an integer column may be
+-- fractional, or a binary column may have lower and upper bounds that
+-- are not 0 and 1 (or it may have no lower/upper bound at all). */
+
+int mpl_get_col_kind(MPL *mpl, int j)
+{ int kind;
+ if (mpl->phase != 3)
+ xfault("mpl_get_col_kind: invalid call sequence\n");
+ if (!(1 <= j && j <= mpl->n))
+ xfault("mpl_get_col_kind: j = %d; column number out of range\n"
+ , j);
+ switch (mpl->col[j]->var->type)
+ { case A_NUMERIC:
+ kind = MPL_NUM; break;
+ case A_INTEGER:
+ kind = MPL_INT; break;
+ case A_BINARY:
+ kind = MPL_BIN; break;
+ default:
+ xassert(mpl != mpl);
+ }
+ return kind;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_get_col_bnds - obtain column bounds.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_get_col_bnds(MPL *mpl, int j, double *lb, double *ub);
+--
+-- *Description*
+--
+-- The routine mpl_get_col_bnds stores lower and upper bound of j-th
+-- column of the problem to the locations, which the parameters lb and
+-- ub point to, respectively. Besides the routine returns the type of
+-- the j-th column.
+--
+-- If some of the parameters lb and ub is NULL, the corresponding bound
+-- value is not stored.
+--
+-- Types and bounds have the following meaning:
+--
+-- Type Bounds Note
+-- ------------------------------------------------------
+-- MPL_FR -inf < x < +inf Free (unbounded) variable
+-- MPL_LO lb <= x < +inf Variable with lower bound
+-- MPL_UP -inf < x <= ub Variable with upper bound
+-- MPL_DB lb <= x <= ub Double-bounded variable
+-- MPL_FX x = lb Fixed variable
+--
+-- where x is individual variable corresponding to the j-th column.
+--
+-- If the column has no lower bound, *lb is set to zero; if the column
+-- has no upper bound, *ub is set to zero; and if the column is of fixed
+-- type, both *lb and *ub are set to the same value.
+--
+-- *Returns*
+--
+-- The routine returns the type of the j-th column as it is stated in
+-- the table above. */
+
+int mpl_get_col_bnds(MPL *mpl, int j, double *_lb, double *_ub)
+{ ELEMVAR *var;
+ int type;
+ double lb, ub;
+ if (mpl->phase != 3)
+ xfault("mpl_get_col_bnds: invalid call sequence\n");
+ if (!(1 <= j && j <= mpl->n))
+ xfault("mpl_get_col_bnds: j = %d; column number out of range\n"
+ , j);
+ var = mpl->col[j];
+#if 0 /* 21/VII-2006 */
+ if (var->var->lbnd == NULL && var->var->ubnd == NULL)
+ type = MPL_FR, lb = ub = 0.0;
+ else if (var->var->ubnd == NULL)
+ type = MPL_LO, lb = var->lbnd, ub = 0.0;
+ else if (var->var->lbnd == NULL)
+ type = MPL_UP, lb = 0.0, ub = var->ubnd;
+ else if (var->var->lbnd != var->var->ubnd)
+ type = MPL_DB, lb = var->lbnd, ub = var->ubnd;
+ else
+ type = MPL_FX, lb = ub = var->lbnd;
+#else
+ lb = (var->var->lbnd == NULL ? -DBL_MAX : var->lbnd);
+ ub = (var->var->ubnd == NULL ? +DBL_MAX : var->ubnd);
+ if (lb == -DBL_MAX && ub == +DBL_MAX)
+ type = MPL_FR, lb = ub = 0.0;
+ else if (ub == +DBL_MAX)
+ type = MPL_LO, ub = 0.0;
+ else if (lb == -DBL_MAX)
+ type = MPL_UP, lb = 0.0;
+ else if (var->var->lbnd != var->var->ubnd)
+ type = MPL_DB;
+ else
+ type = MPL_FX;
+#endif
+ if (_lb != NULL) *_lb = lb;
+ if (_ub != NULL) *_ub = ub;
+ return type;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_has_solve_stmt - check if model has solve statement.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_has_solve_stmt(MPL *mpl);
+--
+-- *Returns*
+--
+-- If the model has the solve statement, the routine returns non-zero,
+-- otherwise zero is returned. */
+
+int mpl_has_solve_stmt(MPL *mpl)
+{ if (mpl->phase != 3)
+ xfault("mpl_has_solve_stmt: invalid call sequence\n");
+ return mpl->flag_s;
+}
+
+#if 1 /* 15/V-2010 */
+void mpl_put_row_soln(MPL *mpl, int i, int stat, double prim,
+ double dual)
+{ /* store row (constraint/objective) solution components */
+ xassert(mpl->phase == 3);
+ xassert(1 <= i && i <= mpl->m);
+ mpl->row[i]->stat = stat;
+ mpl->row[i]->prim = prim;
+ mpl->row[i]->dual = dual;
+ return;
+}
+#endif
+
+#if 1 /* 15/V-2010 */
+void mpl_put_col_soln(MPL *mpl, int j, int stat, double prim,
+ double dual)
+{ /* store column (variable) solution components */
+ xassert(mpl->phase == 3);
+ xassert(1 <= j && j <= mpl->n);
+ mpl->col[j]->stat = stat;
+ mpl->col[j]->prim = prim;
+ mpl->col[j]->dual = dual;
+ return;
+}
+#endif
+
+#if 0 /* 15/V-2010 */
+/*----------------------------------------------------------------------
+-- mpl_put_col_value - store column value.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- void mpl_put_col_value(MPL *mpl, int j, double val);
+--
+-- *Description*
+--
+-- The routine mpl_put_col_value stores numeric value of j-th column
+-- into the translator database. It is assumed that the column value is
+-- provided by the solver. */
+
+void mpl_put_col_value(MPL *mpl, int j, double val)
+{ if (mpl->phase != 3)
+ xfault("mpl_put_col_value: invalid call sequence\n");
+ if (!(1 <= j && j <= mpl->n))
+ xfault(
+ "mpl_put_col_value: j = %d; column number out of range\n", j);
+ mpl->col[j]->prim = val;
+ return;
+}
+#endif
+
+/*----------------------------------------------------------------------
+-- mpl_postsolve - postsolve model.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- int mpl_postsolve(MPL *mpl);
+--
+-- *Description*
+--
+-- The routine mpl_postsolve performs postsolving of the model using
+-- its description stored in the translator database. This phase means
+-- executing statements, which follow the solve statement.
+--
+-- If this routine is used, it should be called once after the routine
+-- mpl_generate and if the latter returned the code 3.
+--
+-- *Returns*
+--
+-- The routine mpl_postsolve returns one of the following codes:
+--
+-- 3 - model has been successfully postsolved.
+-- 4 - processing failed due to some errors. In this case the calling
+-- program should call the routine mpl_terminate to terminate model
+-- processing. */
+
+int mpl_postsolve(MPL *mpl)
+{ if (!(mpl->phase == 3 && !mpl->flag_p))
+ xfault("mpl_postsolve: invalid call sequence\n");
+ /* set up error handler */
+ if (setjmp(mpl->jump)) goto done;
+ /* perform postsolving */
+ postsolve_model(mpl);
+ flush_output(mpl);
+ /* postsolving phase has been finished */
+ xprintf("Model has been successfully processed\n");
+done: /* return to the calling program */
+ return mpl->phase;
+}
+
+/*----------------------------------------------------------------------
+-- mpl_terminate - free all resources used by translator.
+--
+-- *Synopsis*
+--
+-- #include "glpmpl.h"
+-- void mpl_terminate(MPL *mpl);
+--
+-- *Description*
+--
+-- The routine mpl_terminate frees all the resources used by the GNU
+-- MathProg translator. */
+
+void mpl_terminate(MPL *mpl)
+{ if (setjmp(mpl->jump)) xassert(mpl != mpl);
+ switch (mpl->phase)
+ { case 0:
+ case 1:
+ case 2:
+ case 3:
+ /* there were no errors; clean the model content */
+ clean_model(mpl);
+ xassert(mpl->a_list == NULL);
+#if 1 /* 11/II-2008 */
+ xassert(mpl->dca == NULL);
+#endif
+ break;
+ case 4:
+ /* model processing has been finished due to error; delete
+ search trees, which may be created for some arrays */
+ { ARRAY *a;
+ for (a = mpl->a_list; a != NULL; a = a->next)
+ if (a->tree != NULL) avl_delete_tree(a->tree);
+ }
+#if 1 /* 11/II-2008 */
+ free_dca(mpl);
+#endif
+ break;
+ default:
+ xassert(mpl != mpl);
+ }
+ /* delete the translator database */
+ xfree(mpl->image);
+ xfree(mpl->b_image);
+ xfree(mpl->f_image);
+ xfree(mpl->context);
+ dmp_delete_pool(mpl->pool);
+ avl_delete_tree(mpl->tree);
+ dmp_delete_pool(mpl->strings);
+ dmp_delete_pool(mpl->symbols);
+ dmp_delete_pool(mpl->tuples);
+ dmp_delete_pool(mpl->arrays);
+ dmp_delete_pool(mpl->members);
+ dmp_delete_pool(mpl->elemvars);
+ dmp_delete_pool(mpl->formulae);
+ dmp_delete_pool(mpl->elemcons);
+ xfree(mpl->sym_buf);
+ xfree(mpl->tup_buf);
+ rng_delete_rand(mpl->rand);
+ if (mpl->row != NULL) xfree(mpl->row);
+ if (mpl->col != NULL) xfree(mpl->col);
+ if (mpl->in_fp != NULL) glp_close(mpl->in_fp);
+ if (mpl->out_fp != NULL && mpl->out_fp != (void *)stdout)
+ glp_close(mpl->out_fp);
+ if (mpl->out_file != NULL) xfree(mpl->out_file);
+ if (mpl->prt_fp != NULL) glp_close(mpl->prt_fp);
+ if (mpl->prt_file != NULL) xfree(mpl->prt_file);
+ if (mpl->mod_file != NULL) xfree(mpl->mod_file);
+ xfree(mpl->mpl_buf);
+ xfree(mpl);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl5.c b/test/monniaux/glpk-4.65/src/mpl/mpl5.c
new file mode 100644
index 00000000..c5374c9c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl5.c
@@ -0,0 +1,566 @@
+/* mpl5.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Authors: Andrew Makhorin <mao@gnu.org>
+* Heinrich Schuchardt <xypron.glpk@gmx.de>
+*
+* Copyright (C) 2003-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#if 1 /* 11/VI-2013 */
+#include "jd.h"
+#endif
+#include "mpl.h"
+
+double fn_gmtime(MPL *mpl)
+{ /* obtain the current calendar time (UTC) */
+ time_t timer;
+ struct tm *tm;
+ int j;
+ time(&timer);
+ if (timer == (time_t)(-1))
+err: error(mpl, "gmtime(); unable to obtain current calendar time");
+#if 0 /* 29/I-2017 */
+ tm = gmtime(&timer);
+#else
+ tm = xgmtime(&timer);
+#endif
+ if (tm == NULL) goto err;
+ j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
+ if (j < 0) goto err;
+ return (((double)(j - jday(1, 1, 1970)) * 24.0 +
+ (double)tm->tm_hour) * 60.0 + (double)tm->tm_min) * 60.0 +
+ (double)tm->tm_sec;
+}
+
+static char *week[] = { "Monday", "Tuesday", "Wednesday", "Thursday",
+ "Friday", "Saturday", "Sunday" };
+
+static char *moon[] = { "January", "February", "March", "April", "May",
+ "June", "July", "August", "September", "October", "November",
+ "December" };
+
+static void error1(MPL *mpl, const char *str, const char *s,
+ const char *fmt, const char *f, const char *msg)
+{ xprintf("Input string passed to str2time:\n");
+ xprintf("%s\n", str);
+ xprintf("%*s\n", (s - str) + 1, "^");
+ xprintf("Format string passed to str2time:\n");
+ xprintf("%s\n", fmt);
+ xprintf("%*s\n", (f - fmt) + 1, "^");
+ error(mpl, "%s", msg);
+ /* no return */
+}
+
+double fn_str2time(MPL *mpl, const char *str, const char *fmt)
+{ /* convert character string to the calendar time */
+ int j, year, month, day, hh, mm, ss, zone;
+ const char *s, *f;
+ year = month = day = hh = mm = ss = -1, zone = INT_MAX;
+ s = str;
+ for (f = fmt; *f != '\0'; f++)
+ { if (*f == '%')
+ { f++;
+ if (*f == 'b' || *f == 'h')
+ { /* the abbreviated month name */
+ int k;
+ char *name;
+ if (month >= 0)
+ error1(mpl, str, s, fmt, f, "month multiply specified"
+ );
+ while (*s == ' ') s++;
+ for (month = 1; month <= 12; month++)
+ { name = moon[month-1];
+ for (k = 0; k <= 2; k++)
+ { if (toupper((unsigned char)s[k]) !=
+ toupper((unsigned char)name[k])) goto next;
+ }
+ s += 3;
+ for (k = 3; name[k] != '\0'; k++)
+ { if (toupper((unsigned char)*s) !=
+ toupper((unsigned char)name[k])) break;
+ s++;
+ }
+ break;
+next: ;
+ }
+ if (month > 12)
+ error1(mpl, str, s, fmt, f, "abbreviated month name m"
+ "issing or invalid");
+ }
+ else if (*f == 'd')
+ { /* the day of the month as a decimal number (01..31) */
+ if (day >= 0)
+ error1(mpl, str, s, fmt, f, "day multiply specified");
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "day missing or invalid");
+ day = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ day = 10 * day + ((*s++) - '0');
+ if (!(1 <= day && day <= 31))
+ error1(mpl, str, s, fmt, f, "day out of range");
+ }
+ else if (*f == 'H')
+ { /* the hour as a decimal number, using a 24-hour clock
+ (00..23) */
+ if (hh >= 0)
+ error1(mpl, str, s, fmt, f, "hour multiply specified")
+ ;
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "hour missing or invalid")
+ ;
+ hh = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ hh = 10 * hh + ((*s++) - '0');
+ if (!(0 <= hh && hh <= 23))
+ error1(mpl, str, s, fmt, f, "hour out of range");
+ }
+ else if (*f == 'm')
+ { /* the month as a decimal number (01..12) */
+ if (month >= 0)
+ error1(mpl, str, s, fmt, f, "month multiply specified"
+ );
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "month missing or invalid"
+ );
+ month = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ month = 10 * month + ((*s++) - '0');
+ if (!(1 <= month && month <= 12))
+ error1(mpl, str, s, fmt, f, "month out of range");
+ }
+ else if (*f == 'M')
+ { /* the minute as a decimal number (00..59) */
+ if (mm >= 0)
+ error1(mpl, str, s, fmt, f, "minute multiply specifie"
+ "d");
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "minute missing or invali"
+ "d");
+ mm = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ mm = 10 * mm + ((*s++) - '0');
+ if (!(0 <= mm && mm <= 59))
+ error1(mpl, str, s, fmt, f, "minute out of range");
+ }
+ else if (*f == 'S')
+ { /* the second as a decimal number (00..60) */
+ if (ss >= 0)
+ error1(mpl, str, s, fmt, f, "second multiply specifie"
+ "d");
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "second missing or invali"
+ "d");
+ ss = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ ss = 10 * ss + ((*s++) - '0');
+ if (!(0 <= ss && ss <= 60))
+ error1(mpl, str, s, fmt, f, "second out of range");
+ }
+ else if (*f == 'y')
+ { /* the year without a century as a decimal number
+ (00..99); the values 00 to 68 mean the years 2000 to
+ 2068 while the values 69 to 99 mean the years 1969 to
+ 1999 */
+ if (year >= 0)
+ error1(mpl, str, s, fmt, f, "year multiply specified")
+ ;
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "year missing or invalid")
+ ;
+ year = (*s++) - '0';
+ if ('0' <= *s && *s <= '9')
+ year = 10 * year + ((*s++) - '0');
+ year += (year >= 69 ? 1900 : 2000);
+ }
+ else if (*f == 'Y')
+ { /* the year as a decimal number, using the Gregorian
+ calendar */
+ if (year >= 0)
+ error1(mpl, str, s, fmt, f, "year multiply specified")
+ ;
+ while (*s == ' ') s++;
+ if (!('0' <= *s && *s <= '9'))
+ error1(mpl, str, s, fmt, f, "year missing or invalid")
+ ;
+ year = 0;
+ for (j = 1; j <= 4; j++)
+ { if (!('0' <= *s && *s <= '9')) break;
+ year = 10 * year + ((*s++) - '0');
+ }
+ if (!(1 <= year && year <= 4000))
+ error1(mpl, str, s, fmt, f, "year out of range");
+ }
+ else if (*f == 'z')
+ { /* time zone offset in the form zhhmm */
+ int z, hh, mm;
+ if (zone != INT_MAX)
+ error1(mpl, str, s, fmt, f, "time zone offset multipl"
+ "y specified");
+ while (*s == ' ') s++;
+ if (*s == 'Z')
+ { z = hh = mm = 0, s++;
+ goto skip;
+ }
+ if (*s == '+')
+ z = +1, s++;
+ else if (*s == '-')
+ z = -1, s++;
+ else
+ error1(mpl, str, s, fmt, f, "time zone offset sign mi"
+ "ssing");
+ hh = 0;
+ for (j = 1; j <= 2; j++)
+ { if (!('0' <= *s && *s <= '9'))
+err1: error1(mpl, str, s, fmt, f, "time zone offset valu"
+ "e incomplete or invalid");
+ hh = 10 * hh + ((*s++) - '0');
+ }
+ if (hh > 23)
+err2: error1(mpl, str, s, fmt, f, "time zone offset value o"
+ "ut of range");
+ if (*s == ':')
+ { s++;
+ if (!('0' <= *s && *s <= '9')) goto err1;
+ }
+ mm = 0;
+ if (!('0' <= *s && *s <= '9')) goto skip;
+ for (j = 1; j <= 2; j++)
+ { if (!('0' <= *s && *s <= '9')) goto err1;
+ mm = 10 * mm + ((*s++) - '0');
+ }
+ if (mm > 59) goto err2;
+skip: zone = z * (60 * hh + mm);
+ }
+ else if (*f == '%')
+ { /* literal % character */
+ goto test;
+ }
+ else
+ error1(mpl, str, s, fmt, f, "invalid conversion specifie"
+ "r");
+ }
+ else if (*f == ' ')
+ ;
+ else
+test: { /* check a matching character in the input string */
+ if (*s != *f)
+ error1(mpl, str, s, fmt, f, "character mismatch");
+ s++;
+ }
+ }
+ if (year < 0) year = 1970;
+ if (month < 0) month = 1;
+ if (day < 0) day = 1;
+ if (hh < 0) hh = 0;
+ if (mm < 0) mm = 0;
+ if (ss < 0) ss = 0;
+ if (zone == INT_MAX) zone = 0;
+ j = jday(day, month, year);
+ xassert(j >= 0);
+ return (((double)(j - jday(1, 1, 1970)) * 24.0 + (double)hh) *
+ 60.0 + (double)mm) * 60.0 + (double)ss - 60.0 * (double)zone;
+}
+
+static void error2(MPL *mpl, const char *fmt, const char *f,
+ const char *msg)
+{ xprintf("Format string passed to time2str:\n");
+ xprintf("%s\n", fmt);
+ xprintf("%*s\n", (f - fmt) + 1, "^");
+ error(mpl, "%s", msg);
+ /* no return */
+}
+
+static int weekday(int j)
+{ /* determine weekday number (1 = Mon, ..., 7 = Sun) */
+ return (j + jday(1, 1, 1970)) % 7 + 1;
+}
+
+static int firstday(int year)
+{ /* determine the first day of the first week for a specified year
+ according to ISO 8601 */
+ int j;
+ /* if 1 January is Monday, Tuesday, Wednesday or Thursday, it is
+ in week 01; if 1 January is Friday, Saturday or Sunday, it is
+ in week 52 or 53 of the previous year */
+ j = jday(1, 1, year) - jday(1, 1, 1970);
+ switch (weekday(j))
+ { case 1: /* 1 Jan is Mon */ j += 0; break;
+ case 2: /* 1 Jan is Tue */ j -= 1; break;
+ case 3: /* 1 Jan is Wed */ j -= 2; break;
+ case 4: /* 1 Jan is Thu */ j -= 3; break;
+ case 5: /* 1 Jan is Fri */ j += 3; break;
+ case 6: /* 1 Jan is Sat */ j += 2; break;
+ case 7: /* 1 Jan is Sun */ j += 1; break;
+ default: xassert(j != j);
+ }
+ /* the first day of the week must be Monday */
+ xassert(weekday(j) == 1);
+ return j;
+}
+
+void fn_time2str(MPL *mpl, char *str, double t, const char *fmt)
+{ /* convert the calendar time to character string */
+ int j, year, month, day, hh, mm, ss, len;
+ double temp;
+ const char *f;
+ char buf[MAX_LENGTH+1];
+ if (!(-62135596800.0 <= t && t <= 64092211199.0))
+ error(mpl, "time2str(%.*g,...); argument out of range",
+ DBL_DIG, t);
+ t = floor(t + 0.5);
+ temp = fabs(t) / 86400.0;
+ j = (int)floor(temp);
+ if (t < 0.0)
+ { if (temp == floor(temp))
+ j = - j;
+ else
+ j = - (j + 1);
+ }
+ xassert(jdate(j + jday(1, 1, 1970), &day, &month, &year) == 0);
+ ss = (int)(t - 86400.0 * (double)j);
+ xassert(0 <= ss && ss < 86400);
+ mm = ss / 60, ss %= 60;
+ hh = mm / 60, mm %= 60;
+ len = 0;
+ for (f = fmt; *f != '\0'; f++)
+ { if (*f == '%')
+ { f++;
+ if (*f == 'a')
+ { /* the abbreviated weekday name */
+ memcpy(buf, week[weekday(j)-1], 3), buf[3] = '\0';
+ }
+ else if (*f == 'A')
+ { /* the full weekday name */
+ strcpy(buf, week[weekday(j)-1]);
+ }
+ else if (*f == 'b' || *f == 'h')
+ { /* the abbreviated month name */
+ memcpy(buf, moon[month-1], 3), buf[3] = '\0';
+ }
+ else if (*f == 'B')
+ { /* the full month name */
+ strcpy(buf, moon[month-1]);
+ }
+ else if (*f == 'C')
+ { /* the century of the year */
+ sprintf(buf, "%02d", year / 100);
+ }
+ else if (*f == 'd')
+ { /* the day of the month as a decimal number (01..31) */
+ sprintf(buf, "%02d", day);
+ }
+ else if (*f == 'D')
+ { /* the date using the format %m/%d/%y */
+ sprintf(buf, "%02d/%02d/%02d", month, day, year % 100);
+ }
+ else if (*f == 'e')
+ { /* the day of the month like with %d, but padded with
+ blank (1..31) */
+ sprintf(buf, "%2d", day);
+ }
+ else if (*f == 'F')
+ { /* the date using the format %Y-%m-%d */
+ sprintf(buf, "%04d-%02d-%02d", year, month, day);
+ }
+ else if (*f == 'g')
+ { /* the year corresponding to the ISO week number, but
+ without the century (range 00 through 99); this has
+ the same format and value as %y, except that if the
+ ISO week number (see %V) belongs to the previous or
+ next year, that year is used instead */
+ int iso;
+ if (j < firstday(year))
+ iso = year - 1;
+ else if (j < firstday(year + 1))
+ iso = year;
+ else
+ iso = year + 1;
+ sprintf(buf, "%02d", iso % 100);
+ }
+ else if (*f == 'G')
+ { /* the year corresponding to the ISO week number; this
+ has the same format and value as %Y, excepth that if
+ the ISO week number (see %V) belongs to the previous
+ or next year, that year is used instead */
+ int iso;
+ if (j < firstday(year))
+ iso = year - 1;
+ else if (j < firstday(year + 1))
+ iso = year;
+ else
+ iso = year + 1;
+ sprintf(buf, "%04d", iso);
+ }
+ else if (*f == 'H')
+ { /* the hour as a decimal number, using a 24-hour clock
+ (00..23) */
+ sprintf(buf, "%02d", hh);
+ }
+ else if (*f == 'I')
+ { /* the hour as a decimal number, using a 12-hour clock
+ (01..12) */
+ sprintf(buf, "%02d",
+ hh == 0 ? 12 : hh <= 12 ? hh : hh - 12);
+ }
+ else if (*f == 'j')
+ { /* the day of the year as a decimal number (001..366) */
+ sprintf(buf, "%03d",
+ jday(day, month, year) - jday(1, 1, year) + 1);
+ }
+ else if (*f == 'k')
+ { /* the hour as a decimal number, using a 24-hour clock
+ like %H, but padded with blank (0..23) */
+ sprintf(buf, "%2d", hh);
+ }
+ else if (*f == 'l')
+ { /* the hour as a decimal number, using a 12-hour clock
+ like %I, but padded with blank (1..12) */
+ sprintf(buf, "%2d",
+ hh == 0 ? 12 : hh <= 12 ? hh : hh - 12);
+ }
+ else if (*f == 'm')
+ { /* the month as a decimal number (01..12) */
+ sprintf(buf, "%02d", month);
+ }
+ else if (*f == 'M')
+ { /* the minute as a decimal number (00..59) */
+ sprintf(buf, "%02d", mm);
+ }
+ else if (*f == 'p')
+ { /* either AM or PM, according to the given time value;
+ noon is treated as PM and midnight as AM */
+ strcpy(buf, hh <= 11 ? "AM" : "PM");
+ }
+ else if (*f == 'P')
+ { /* either am or pm, according to the given time value;
+ noon is treated as pm and midnight as am */
+ strcpy(buf, hh <= 11 ? "am" : "pm");
+ }
+ else if (*f == 'r')
+ { /* the calendar time using the format %I:%M:%S %p */
+ sprintf(buf, "%02d:%02d:%02d %s",
+ hh == 0 ? 12 : hh <= 12 ? hh : hh - 12,
+ mm, ss, hh <= 11 ? "AM" : "PM");
+ }
+ else if (*f == 'R')
+ { /* the hour and minute using the format %H:%M */
+ sprintf(buf, "%02d:%02d", hh, mm);
+ }
+ else if (*f == 'S')
+ { /* the second as a decimal number (00..59) */
+ sprintf(buf, "%02d", ss);
+ }
+ else if (*f == 'T')
+ { /* the time of day using the format %H:%M:%S */
+ sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
+ }
+ else if (*f == 'u')
+ { /* the day of the week as a decimal number (1..7),
+ Monday being 1 */
+ sprintf(buf, "%d", weekday(j));
+ }
+ else if (*f == 'U')
+ { /* the week number of the current year as a decimal
+ number (range 00 through 53), starting with the first
+ Sunday as the first day of the first week; days
+ preceding the first Sunday in the year are considered
+ to be in week 00 */
+#if 1 /* 09/I-2009 */
+#undef sun
+/* causes compilation error in SunOS */
+#endif
+ int sun;
+ /* sun = the first Sunday of the year */
+ sun = jday(1, 1, year) - jday(1, 1, 1970);
+ sun += (7 - weekday(sun));
+ sprintf(buf, "%02d", (j + 7 - sun) / 7);
+ }
+ else if (*f == 'V')
+ { /* the ISO week number as a decimal number (range 01
+ through 53); ISO weeks start with Monday and end with
+ Sunday; week 01 of a year is the first week which has
+ the majority of its days in that year; week 01 of
+ a year can contain days from the previous year; the
+ week before week 01 of a year is the last week (52 or
+ 53) of the previous year even if it contains days
+ from the new year */
+ int iso;
+ if (j < firstday(year))
+ iso = j - firstday(year - 1);
+ else if (j < firstday(year + 1))
+ iso = j - firstday(year);
+ else
+ iso = j - firstday(year + 1);
+ sprintf(buf, "%02d", iso / 7 + 1);
+ }
+ else if (*f == 'w')
+ { /* the day of the week as a decimal number (0..6),
+ Sunday being 0 */
+ sprintf(buf, "%d", weekday(j) % 7);
+ }
+ else if (*f == 'W')
+ { /* the week number of the current year as a decimal
+ number (range 00 through 53), starting with the first
+ Monday as the first day of the first week; days
+ preceding the first Monday in the year are considered
+ to be in week 00 */
+ int mon;
+ /* mon = the first Monday of the year */
+ mon = jday(1, 1, year) - jday(1, 1, 1970);
+ mon += (8 - weekday(mon)) % 7;
+ sprintf(buf, "%02d", (j + 7 - mon) / 7);
+ }
+ else if (*f == 'y')
+ { /* the year without a century as a decimal number
+ (00..99) */
+ sprintf(buf, "%02d", year % 100);
+ }
+ else if (*f == 'Y')
+ { /* the year as a decimal number, using the Gregorian
+ calendar */
+ sprintf(buf, "%04d", year);
+ }
+ else if (*f == '%')
+ { /* a literal % character */
+ buf[0] = '%', buf[1] = '\0';
+ }
+ else
+ error2(mpl, fmt, f, "invalid conversion specifier");
+ }
+ else
+ buf[0] = *f, buf[1] = '\0';
+ if (len + strlen(buf) > MAX_LENGTH)
+ error(mpl, "time2str; output string length exceeds %d chara"
+ "cters", MAX_LENGTH);
+ memcpy(str+len, buf, strlen(buf));
+ len += strlen(buf);
+ }
+ str[len] = '\0';
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mpl6.c b/test/monniaux/glpk-4.65/src/mpl/mpl6.c
new file mode 100644
index 00000000..ac2a0393
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mpl6.c
@@ -0,0 +1,1039 @@
+/* mpl6.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2003-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "mpl.h"
+#include "mplsql.h"
+
+/**********************************************************************/
+
+#define CSV_FIELD_MAX 50
+/* maximal number of fields in record */
+
+#define CSV_FDLEN_MAX 100
+/* maximal field length */
+
+struct csv
+{ /* comma-separated values file */
+ int mode;
+ /* 'R' = reading; 'W' = writing */
+ char *fname;
+ /* name of csv file */
+ FILE *fp;
+ /* stream assigned to csv file */
+ jmp_buf jump;
+ /* address for non-local go to in case of error */
+ int count;
+ /* record count */
+ /*--------------------------------------------------------------*/
+ /* used only for input csv file */
+ int c;
+ /* current character or EOF */
+ int what;
+ /* current marker: */
+#define CSV_EOF 0 /* end-of-file */
+#define CSV_EOR 1 /* end-of-record */
+#define CSV_NUM 2 /* floating-point number */
+#define CSV_STR 3 /* character string */
+ char field[CSV_FDLEN_MAX+1];
+ /* current field just read */
+ int nf;
+ /* number of fields in the csv file */
+ int ref[1+CSV_FIELD_MAX];
+ /* ref[k] = k', if k-th field of the csv file corresponds to
+ k'-th field in the table statement; if ref[k] = 0, k-th field
+ of the csv file is ignored */
+#if 1 /* 01/VI-2010 */
+ int nskip;
+ /* number of comment records preceding the header record */
+#endif
+};
+
+#undef read_char
+
+static void read_char(struct csv *csv)
+{ /* read character from csv data file */
+ int c;
+ xassert(csv->c != EOF);
+ if (csv->c == '\n') csv->count++;
+loop: c = fgetc(csv->fp);
+ if (ferror(csv->fp))
+ { xprintf("%s:%d: read error - %s\n", csv->fname, csv->count,
+#if 0 /* 29/I-2017 */
+ strerror(errno));
+#else
+ xstrerr(errno));
+#endif
+ longjmp(csv->jump, 0);
+ }
+ if (feof(csv->fp))
+ { if (csv->c == '\n')
+ { csv->count--;
+ c = EOF;
+ }
+ else
+ { xprintf("%s:%d: warning: missing final end-of-line\n",
+ csv->fname, csv->count);
+ c = '\n';
+ }
+ }
+ else if (c == '\r')
+ goto loop;
+ else if (c == '\n')
+ ;
+ else if (iscntrl(c))
+ { xprintf("%s:%d: invalid control character 0x%02X\n",
+ csv->fname, csv->count, c);
+ longjmp(csv->jump, 0);
+ }
+ csv->c = c;
+ return;
+}
+
+static void read_field(struct csv *csv)
+{ /* read field from csv data file */
+ /* check for end of file */
+ if (csv->c == EOF)
+ { csv->what = CSV_EOF;
+ strcpy(csv->field, "EOF");
+ goto done;
+ }
+ /* check for end of record */
+ if (csv->c == '\n')
+ { csv->what = CSV_EOR;
+ strcpy(csv->field, "EOR");
+ read_char(csv);
+ if (csv->c == ',')
+err1: { xprintf("%s:%d: empty field not allowed\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+ if (csv->c == '\n')
+ { xprintf("%s:%d: empty record not allowed\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+#if 1 /* 01/VI-2010 */
+ /* skip comment records; may appear only before the very first
+ record containing field names */
+ if (csv->c == '#' && csv->count == 1)
+ { while (csv->c == '#')
+ { while (csv->c != '\n')
+ read_char(csv);
+ read_char(csv);
+ csv->nskip++;
+ }
+ }
+#endif
+ goto done;
+ }
+ /* skip comma before next field */
+ if (csv->c == ',')
+ read_char(csv);
+ /* read field */
+ if (csv->c == '\'' || csv->c == '"')
+ { /* read a field enclosed in quotes */
+ int quote = csv->c, len = 0;
+ csv->what = CSV_STR;
+ /* skip opening quote */
+ read_char(csv);
+ /* read field characters within quotes */
+ for (;;)
+ { /* check for closing quote and read it */
+ if (csv->c == quote)
+ { read_char(csv);
+ if (csv->c == quote)
+ ;
+ else if (csv->c == ',' || csv->c == '\n')
+ break;
+ else
+ { xprintf("%s:%d: invalid field\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+ }
+ /* check the current field length */
+ if (len == CSV_FDLEN_MAX)
+err2: { xprintf("%s:%d: field too long\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+ /* add the current character to the field */
+ csv->field[len++] = (char)csv->c;
+ /* read the next character */
+ read_char(csv);
+ }
+ /* the field has been read */
+ if (len == 0) goto err1;
+ csv->field[len] = '\0';
+ }
+ else
+ { /* read a field not enclosed in quotes */
+ int len = 0;
+ double temp;
+ csv->what = CSV_NUM;
+ while (!(csv->c == ',' || csv->c == '\n'))
+ { /* quotes within the field are not allowed */
+ if (csv->c == '\'' || csv->c == '"')
+ { xprintf("%s:%d: invalid use of single or double quote wi"
+ "thin field\n", csv->fname, csv->count);
+ longjmp(csv->jump, 0);
+ }
+ /* check the current field length */
+ if (len == CSV_FDLEN_MAX) goto err2;
+ /* add the current character to the field */
+ csv->field[len++] = (char)csv->c;
+ /* read the next character */
+ read_char(csv);
+ }
+ /* the field has been read */
+ if (len == 0) goto err1;
+ csv->field[len] = '\0';
+ /* check the field type */
+ if (str2num(csv->field, &temp)) csv->what = CSV_STR;
+ }
+done: return;
+}
+
+static struct csv *csv_open_file(TABDCA *dca, int mode)
+{ /* open csv data file */
+ struct csv *csv;
+ /* create control structure */
+ csv = xmalloc(sizeof(struct csv));
+ csv->mode = mode;
+ csv->fname = NULL;
+ csv->fp = NULL;
+ if (setjmp(csv->jump)) goto fail;
+ csv->count = 0;
+ csv->c = '\n';
+ csv->what = 0;
+ csv->field[0] = '\0';
+ csv->nf = 0;
+ /* try to open the csv data file */
+ if (mpl_tab_num_args(dca) < 2)
+ { xprintf("csv_driver: file name not specified\n");
+ longjmp(csv->jump, 0);
+ }
+ csv->fname = xmalloc(strlen(mpl_tab_get_arg(dca, 2))+1);
+ strcpy(csv->fname, mpl_tab_get_arg(dca, 2));
+ if (mode == 'R')
+ { /* open the file for reading */
+ int k;
+ csv->fp = fopen(csv->fname, "r");
+ if (csv->fp == NULL)
+ { xprintf("csv_driver: unable to open %s - %s\n",
+#if 0 /* 29/I-2017 */
+ csv->fname, strerror(errno));
+#else
+ csv->fname, xstrerr(errno));
+#endif
+ longjmp(csv->jump, 0);
+ }
+#if 1 /* 01/VI-2010 */
+ csv->nskip = 0;
+#endif
+ /* skip fake new-line */
+ read_field(csv);
+ xassert(csv->what == CSV_EOR);
+ /* read field names */
+ xassert(csv->nf == 0);
+ for (;;)
+ { read_field(csv);
+ if (csv->what == CSV_EOR)
+ break;
+ if (csv->what != CSV_STR)
+ { xprintf("%s:%d: invalid field name\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+ if (csv->nf == CSV_FIELD_MAX)
+ { xprintf("%s:%d: too many fields\n", csv->fname,
+ csv->count);
+ longjmp(csv->jump, 0);
+ }
+ csv->nf++;
+ /* find corresponding field in the table statement */
+ for (k = mpl_tab_num_flds(dca); k >= 1; k--)
+ { if (strcmp(mpl_tab_get_name(dca, k), csv->field) == 0)
+ break;
+ }
+ csv->ref[csv->nf] = k;
+ }
+ /* find dummy RECNO field in the table statement */
+ for (k = mpl_tab_num_flds(dca); k >= 1; k--)
+ if (strcmp(mpl_tab_get_name(dca, k), "RECNO") == 0) break;
+ csv->ref[0] = k;
+ }
+ else if (mode == 'W')
+ { /* open the file for writing */
+ int k, nf;
+ csv->fp = fopen(csv->fname, "w");
+ if (csv->fp == NULL)
+ { xprintf("csv_driver: unable to create %s - %s\n",
+#if 0 /* 29/I-2017 */
+ csv->fname, strerror(errno));
+#else
+ csv->fname, xstrerr(errno));
+#endif
+ longjmp(csv->jump, 0);
+ }
+ /* write field names */
+ nf = mpl_tab_num_flds(dca);
+ for (k = 1; k <= nf; k++)
+ fprintf(csv->fp, "%s%c", mpl_tab_get_name(dca, k),
+ k < nf ? ',' : '\n');
+ csv->count++;
+ }
+ else
+ xassert(mode != mode);
+ /* the file has been open */
+ return csv;
+fail: /* the file cannot be open */
+ if (csv->fname != NULL) xfree(csv->fname);
+ if (csv->fp != NULL) fclose(csv->fp);
+ xfree(csv);
+ return NULL;
+}
+
+static int csv_read_record(TABDCA *dca, struct csv *csv)
+{ /* read next record from csv data file */
+ int k, ret = 0;
+ xassert(csv->mode == 'R');
+ if (setjmp(csv->jump))
+ { ret = 1;
+ goto done;
+ }
+ /* read dummy RECNO field */
+ if (csv->ref[0] > 0)
+#if 0 /* 01/VI-2010 */
+ mpl_tab_set_num(dca, csv->ref[0], csv->count-1);
+#else
+ mpl_tab_set_num(dca, csv->ref[0], csv->count-csv->nskip-1);
+#endif
+ /* read fields */
+ for (k = 1; k <= csv->nf; k++)
+ { read_field(csv);
+ if (csv->what == CSV_EOF)
+ { /* end-of-file reached */
+ xassert(k == 1);
+ ret = -1;
+ goto done;
+ }
+ else if (csv->what == CSV_EOR)
+ { /* end-of-record reached */
+ int lack = csv->nf - k + 1;
+ if (lack == 1)
+ xprintf("%s:%d: one field missing\n", csv->fname,
+ csv->count);
+ else
+ xprintf("%s:%d: %d fields missing\n", csv->fname,
+ csv->count, lack);
+ longjmp(csv->jump, 0);
+ }
+ else if (csv->what == CSV_NUM)
+ { /* floating-point number */
+ if (csv->ref[k] > 0)
+ { double num;
+ xassert(str2num(csv->field, &num) == 0);
+ mpl_tab_set_num(dca, csv->ref[k], num);
+ }
+ }
+ else if (csv->what == CSV_STR)
+ { /* character string */
+ if (csv->ref[k] > 0)
+ mpl_tab_set_str(dca, csv->ref[k], csv->field);
+ }
+ else
+ xassert(csv != csv);
+ }
+ /* now there must be NL */
+ read_field(csv);
+ xassert(csv->what != CSV_EOF);
+ if (csv->what != CSV_EOR)
+ { xprintf("%s:%d: too many fields\n", csv->fname, csv->count);
+ longjmp(csv->jump, 0);
+ }
+done: return ret;
+}
+
+static int csv_write_record(TABDCA *dca, struct csv *csv)
+{ /* write next record to csv data file */
+ int k, nf, ret = 0;
+ const char *c;
+ xassert(csv->mode == 'W');
+ nf = mpl_tab_num_flds(dca);
+ for (k = 1; k <= nf; k++)
+ { switch (mpl_tab_get_type(dca, k))
+ { case 'N':
+ fprintf(csv->fp, "%.*g", DBL_DIG,
+ mpl_tab_get_num(dca, k));
+ break;
+ case 'S':
+ fputc('"', csv->fp);
+ for (c = mpl_tab_get_str(dca, k); *c != '\0'; c++)
+ { if (*c == '"')
+ fputc('"', csv->fp), fputc('"', csv->fp);
+ else
+ fputc(*c, csv->fp);
+ }
+ fputc('"', csv->fp);
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ fputc(k < nf ? ',' : '\n', csv->fp);
+ }
+ csv->count++;
+ if (ferror(csv->fp))
+ { xprintf("%s:%d: write error - %s\n", csv->fname, csv->count,
+#if 0 /* 29/I-2017 */
+ strerror(errno));
+#else
+ xstrerr(errno));
+#endif
+ ret = 1;
+ }
+ return ret;
+}
+
+static int csv_close_file(TABDCA *dca, struct csv *csv)
+{ /* close csv data file */
+ int ret = 0;
+ xassert(dca == dca);
+ if (csv->mode == 'W')
+ { fflush(csv->fp);
+ if (ferror(csv->fp))
+ { xprintf("%s:%d: write error - %s\n", csv->fname,
+#if 0 /* 29/I-2017 */
+ csv->count, strerror(errno));
+#else
+ csv->count, xstrerr(errno));
+#endif
+ ret = 1;
+ }
+ }
+ xfree(csv->fname);
+ fclose(csv->fp);
+ xfree(csv);
+ return ret;
+}
+
+/**********************************************************************/
+
+#define DBF_FIELD_MAX 50
+/* maximal number of fields in record */
+
+#define DBF_FDLEN_MAX 100
+/* maximal field length */
+
+struct dbf
+{ /* xBASE data file */
+ int mode;
+ /* 'R' = reading; 'W' = writing */
+ char *fname;
+ /* name of xBASE file */
+ FILE *fp;
+ /* stream assigned to xBASE file */
+ jmp_buf jump;
+ /* address for non-local go to in case of error */
+ int offset;
+ /* offset of a byte to be read next */
+ int count;
+ /* record count */
+ int nf;
+ /* number of fields */
+ int ref[1+DBF_FIELD_MAX];
+ /* ref[k] = k', if k-th field of the csv file corresponds to
+ k'-th field in the table statement; if ref[k] = 0, k-th field
+ of the csv file is ignored */
+ int type[1+DBF_FIELD_MAX];
+ /* type[k] is type of k-th field */
+ int len[1+DBF_FIELD_MAX];
+ /* len[k] is length of k-th field */
+ int prec[1+DBF_FIELD_MAX];
+ /* prec[k] is precision of k-th field */
+};
+
+static int read_byte(struct dbf *dbf)
+{ /* read byte from xBASE data file */
+ int b;
+ b = fgetc(dbf->fp);
+ if (ferror(dbf->fp))
+ { xprintf("%s:0x%X: read error - %s\n", dbf->fname,
+#if 0 /* 29/I-2017 */
+ dbf->offset, strerror(errno));
+#else
+ dbf->offset, xstrerr(errno));
+#endif
+ longjmp(dbf->jump, 0);
+ }
+ if (feof(dbf->fp))
+ { xprintf("%s:0x%X: unexpected end of file\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ xassert(0x00 <= b && b <= 0xFF);
+ dbf->offset++;
+ return b;
+}
+
+static void read_header(TABDCA *dca, struct dbf *dbf)
+{ /* read xBASE data file header */
+ int b, j, k, recl;
+ char name[10+1];
+ /* (ignored) */
+ for (j = 1; j <= 10; j++)
+ read_byte(dbf);
+ /* length of each record, in bytes */
+ recl = read_byte(dbf);
+ recl += read_byte(dbf) << 8;
+ /* (ignored) */
+ for (j = 1; j <= 20; j++)
+ read_byte(dbf);
+ /* field descriptor array */
+ xassert(dbf->nf == 0);
+ for (;;)
+ { /* check for end of array */
+ b = read_byte(dbf);
+ if (b == 0x0D) break;
+ if (dbf->nf == DBF_FIELD_MAX)
+ { xprintf("%s:0x%X: too many fields\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ dbf->nf++;
+ /* field name */
+ name[0] = (char)b;
+ for (j = 1; j < 10; j++)
+ { b = read_byte(dbf);
+ name[j] = (char)b;
+ }
+ name[10] = '\0';
+ b = read_byte(dbf);
+ if (b != 0x00)
+ { xprintf("%s:0x%X: invalid field name\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ /* find corresponding field in the table statement */
+ for (k = mpl_tab_num_flds(dca); k >= 1; k--)
+ if (strcmp(mpl_tab_get_name(dca, k), name) == 0) break;
+ dbf->ref[dbf->nf] = k;
+ /* field type */
+ b = read_byte(dbf);
+ if (!(b == 'C' || b == 'N'))
+ { xprintf("%s:0x%X: invalid field type\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ dbf->type[dbf->nf] = b;
+ /* (ignored) */
+ for (j = 1; j <= 4; j++)
+ read_byte(dbf);
+ /* field length */
+ b = read_byte(dbf);
+ if (b == 0)
+ { xprintf("%s:0x%X: invalid field length\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ if (b > DBF_FDLEN_MAX)
+ { xprintf("%s:0x%X: field too long\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ dbf->len[dbf->nf] = b;
+ recl -= b;
+ /* (ignored) */
+ for (j = 1; j <= 15; j++)
+ read_byte(dbf);
+ }
+ if (recl != 1)
+ { xprintf("%s:0x%X: invalid file header\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ /* find dummy RECNO field in the table statement */
+ for (k = mpl_tab_num_flds(dca); k >= 1; k--)
+ if (strcmp(mpl_tab_get_name(dca, k), "RECNO") == 0) break;
+ dbf->ref[0] = k;
+ return;
+}
+
+static void parse_third_arg(TABDCA *dca, struct dbf *dbf)
+{ /* parse xBASE file format (third argument) */
+ int j, k, temp;
+ const char *arg;
+ dbf->nf = mpl_tab_num_flds(dca);
+ arg = mpl_tab_get_arg(dca, 3), j = 0;
+ for (k = 1; k <= dbf->nf; k++)
+ { /* parse specification of k-th field */
+ if (arg[j] == '\0')
+ { xprintf("xBASE driver: field %s: specification missing\n",
+ mpl_tab_get_name(dca, k));
+ longjmp(dbf->jump, 0);
+ }
+ /* parse field type */
+ if (arg[j] == 'C' || arg[j] == 'N')
+ dbf->type[k] = arg[j], j++;
+ else
+ { xprintf("xBASE driver: field %s: invalid field type\n",
+ mpl_tab_get_name(dca, k));
+ longjmp(dbf->jump, 0);
+ }
+ /* check for left parenthesis */
+ if (arg[j] == '(')
+ j++;
+ else
+err: { xprintf("xBASE driver: field %s: invalid field format\n",
+ mpl_tab_get_name(dca, k));
+ longjmp(dbf->jump, 0);
+ }
+ /* parse field length */
+ temp = 0;
+ while (isdigit(arg[j]))
+ { if (temp > DBF_FDLEN_MAX) break;
+ temp = 10 * temp + (arg[j] - '0'), j++;
+ }
+ if (!(1 <= temp && temp <= DBF_FDLEN_MAX))
+ { xprintf("xBASE driver: field %s: invalid field length\n",
+ mpl_tab_get_name(dca, k));
+ longjmp(dbf->jump, 0);
+ }
+ dbf->len[k] = temp;
+ /* parse optional field precision */
+ if (dbf->type[k] == 'N' && arg[j] == ',')
+ { j++;
+ temp = 0;
+ while (isdigit(arg[j]))
+ { if (temp > dbf->len[k]) break;
+ temp = 10 * temp + (arg[j] - '0'), j++;
+ }
+ if (temp > dbf->len[k])
+ { xprintf("xBASE driver: field %s: invalid field precision"
+ "\n", mpl_tab_get_name(dca, k));
+ longjmp(dbf->jump, 0);
+ }
+ dbf->prec[k] = temp;
+ }
+ else
+ dbf->prec[k] = 0;
+ /* check for right parenthesis */
+ if (arg[j] == ')')
+ j++;
+ else
+ goto err;
+ }
+ /* ignore other specifications */
+ return;
+}
+
+static void write_byte(struct dbf *dbf, int b)
+{ /* write byte to xBASE data file */
+ fputc(b, dbf->fp);
+ dbf->offset++;
+ return;
+}
+
+static void write_header(TABDCA *dca, struct dbf *dbf)
+{ /* write xBASE data file header */
+ int j, k, temp;
+ const char *name;
+ /* version number */
+ write_byte(dbf, 0x03 /* file without DBT */);
+ /* date of last update (YYMMDD) */
+ write_byte(dbf, 70 /* 1970 */);
+ write_byte(dbf, 1 /* January */);
+ write_byte(dbf, 1 /* 1st */);
+ /* number of records (unknown so far) */
+ for (j = 1; j <= 4; j++)
+ write_byte(dbf, 0xFF);
+ /* length of the header, in bytes */
+ temp = 32 + dbf->nf * 32 + 1;
+ write_byte(dbf, temp);
+ write_byte(dbf, temp >> 8);
+ /* length of each record, in bytes */
+ temp = 1;
+ for (k = 1; k <= dbf->nf; k++)
+ temp += dbf->len[k];
+ write_byte(dbf, temp);
+ write_byte(dbf, temp >> 8);
+ /* (reserved) */
+ for (j = 1; j <= 20; j++)
+ write_byte(dbf, 0x00);
+ /* field descriptor array */
+ for (k = 1; k <= dbf->nf; k++)
+ { /* field name (terminated by 0x00) */
+ name = mpl_tab_get_name(dca, k);
+ for (j = 0; j < 10 && name[j] != '\0'; j++)
+ write_byte(dbf, name[j]);
+ for (j = j; j < 11; j++)
+ write_byte(dbf, 0x00);
+ /* field type */
+ write_byte(dbf, dbf->type[k]);
+ /* (reserved) */
+ for (j = 1; j <= 4; j++)
+ write_byte(dbf, 0x00);
+ /* field length */
+ write_byte(dbf, dbf->len[k]);
+ /* field precision */
+ write_byte(dbf, dbf->prec[k]);
+ /* (reserved) */
+ for (j = 1; j <= 14; j++)
+ write_byte(dbf, 0x00);
+ }
+ /* end of header */
+ write_byte(dbf, 0x0D);
+ return;
+}
+
+static struct dbf *dbf_open_file(TABDCA *dca, int mode)
+{ /* open xBASE data file */
+ struct dbf *dbf;
+ /* create control structure */
+ dbf = xmalloc(sizeof(struct dbf));
+ dbf->mode = mode;
+ dbf->fname = NULL;
+ dbf->fp = NULL;
+ if (setjmp(dbf->jump)) goto fail;
+ dbf->offset = 0;
+ dbf->count = 0;
+ dbf->nf = 0;
+ /* try to open the xBASE data file */
+ if (mpl_tab_num_args(dca) < 2)
+ { xprintf("xBASE driver: file name not specified\n");
+ longjmp(dbf->jump, 0);
+ }
+ dbf->fname = xmalloc(strlen(mpl_tab_get_arg(dca, 2))+1);
+ strcpy(dbf->fname, mpl_tab_get_arg(dca, 2));
+ if (mode == 'R')
+ { /* open the file for reading */
+ dbf->fp = fopen(dbf->fname, "rb");
+ if (dbf->fp == NULL)
+ { xprintf("xBASE driver: unable to open %s - %s\n",
+#if 0 /* 29/I-2017 */
+ dbf->fname, strerror(errno));
+#else
+ dbf->fname, xstrerr(errno));
+#endif
+ longjmp(dbf->jump, 0);
+ }
+ read_header(dca, dbf);
+ }
+ else if (mode == 'W')
+ { /* open the file for writing */
+ if (mpl_tab_num_args(dca) < 3)
+ { xprintf("xBASE driver: file format not specified\n");
+ longjmp(dbf->jump, 0);
+ }
+ parse_third_arg(dca, dbf);
+ dbf->fp = fopen(dbf->fname, "wb");
+ if (dbf->fp == NULL)
+ { xprintf("xBASE driver: unable to create %s - %s\n",
+#if 0 /* 29/I-2017 */
+ dbf->fname, strerror(errno));
+#else
+ dbf->fname, xstrerr(errno));
+#endif
+ longjmp(dbf->jump, 0);
+ }
+ write_header(dca, dbf);
+ }
+ else
+ xassert(mode != mode);
+ /* the file has been open */
+ return dbf;
+fail: /* the file cannot be open */
+ if (dbf->fname != NULL) xfree(dbf->fname);
+ if (dbf->fp != NULL) fclose(dbf->fp);
+ xfree(dbf);
+ return NULL;
+}
+
+static int dbf_read_record(TABDCA *dca, struct dbf *dbf)
+{ /* read next record from xBASE data file */
+ int b, j, k, ret = 0;
+ char buf[DBF_FDLEN_MAX+1];
+ xassert(dbf->mode == 'R');
+ if (setjmp(dbf->jump))
+ { ret = 1;
+ goto done;
+ }
+ /* check record flag */
+ b = read_byte(dbf);
+ if (b == 0x1A)
+ { /* end of data */
+ ret = -1;
+ goto done;
+ }
+ if (b != 0x20)
+ { xprintf("%s:0x%X: invalid record flag\n", dbf->fname,
+ dbf->offset);
+ longjmp(dbf->jump, 0);
+ }
+ /* read dummy RECNO field */
+ if (dbf->ref[0] > 0)
+ mpl_tab_set_num(dca, dbf->ref[0], dbf->count+1);
+ /* read fields */
+ for (k = 1; k <= dbf->nf; k++)
+ { /* read k-th field */
+ for (j = 0; j < dbf->len[k]; j++)
+ buf[j] = (char)read_byte(dbf);
+ buf[dbf->len[k]] = '\0';
+ /* set field value */
+ if (dbf->type[k] == 'C')
+ { /* character field */
+ if (dbf->ref[k] > 0)
+ mpl_tab_set_str(dca, dbf->ref[k], strtrim(buf));
+ }
+ else if (dbf->type[k] == 'N')
+ { /* numeric field */
+ if (dbf->ref[k] > 0)
+ { double num;
+ strspx(buf);
+ xassert(str2num(buf, &num) == 0);
+ mpl_tab_set_num(dca, dbf->ref[k], num);
+ }
+ }
+ else
+ xassert(dbf != dbf);
+ }
+ /* increase record count */
+ dbf->count++;
+done: return ret;
+}
+
+static int dbf_write_record(TABDCA *dca, struct dbf *dbf)
+{ /* write next record to xBASE data file */
+ int j, k, ret = 0;
+ char buf[255+1];
+ xassert(dbf->mode == 'W');
+ if (setjmp(dbf->jump))
+ { ret = 1;
+ goto done;
+ }
+ /* record flag */
+ write_byte(dbf, 0x20);
+ xassert(dbf->nf == mpl_tab_num_flds(dca));
+ for (k = 1; k <= dbf->nf; k++)
+ { if (dbf->type[k] == 'C')
+ { /* character field */
+ const char *str;
+ if (mpl_tab_get_type(dca, k) == 'N')
+ { sprintf(buf, "%.*g", DBL_DIG, mpl_tab_get_num(dca, k));
+ str = buf;
+ }
+ else if (mpl_tab_get_type(dca, k) == 'S')
+ str = mpl_tab_get_str(dca, k);
+ else
+ xassert(dca != dca);
+ if ((int)strlen(str) > dbf->len[k])
+ { xprintf("xBASE driver: field %s: cannot convert %.15s..."
+ " to field format\n", mpl_tab_get_name(dca, k), str);
+ longjmp(dbf->jump, 0);
+ }
+ for (j = 0; j < dbf->len[k] && str[j] != '\0'; j++)
+ write_byte(dbf, str[j]);
+ for (j = j; j < dbf->len[k]; j++)
+ write_byte(dbf, ' ');
+ }
+ else if (dbf->type[k] == 'N')
+ { /* numeric field */
+ double num = mpl_tab_get_num(dca, k);
+ if (fabs(num) > 1e20)
+err: { xprintf("xBASE driver: field %s: cannot convert %g to fi"
+ "eld format\n", mpl_tab_get_name(dca, k), num);
+ longjmp(dbf->jump, 0);
+ }
+ sprintf(buf, "%*.*f", dbf->len[k], dbf->prec[k], num);
+ xassert(strlen(buf) < sizeof(buf));
+ if ((int)strlen(buf) != dbf->len[k]) goto err;
+ for (j = 0; j < dbf->len[k]; j++)
+ write_byte(dbf, buf[j]);
+ }
+ else
+ xassert(dbf != dbf);
+ }
+ /* increase record count */
+ dbf->count++;
+done: return ret;
+}
+
+static int dbf_close_file(TABDCA *dca, struct dbf *dbf)
+{ /* close xBASE data file */
+ int ret = 0;
+ xassert(dca == dca);
+ if (dbf->mode == 'W')
+ { if (setjmp(dbf->jump))
+ { ret = 1;
+ goto skip;
+ }
+ /* end-of-file flag */
+ write_byte(dbf, 0x1A);
+ /* number of records */
+ dbf->offset = 4;
+ if (fseek(dbf->fp, dbf->offset, SEEK_SET))
+ { xprintf("%s:0x%X: seek error - %s\n", dbf->fname,
+#if 0 /* 29/I-2017 */
+ dbf->offset, strerror(errno));
+#else
+ dbf->offset, xstrerr(errno));
+#endif
+ longjmp(dbf->jump, 0);
+ }
+ write_byte(dbf, dbf->count);
+ write_byte(dbf, dbf->count >> 8);
+ write_byte(dbf, dbf->count >> 16);
+ write_byte(dbf, dbf->count >> 24);
+ fflush(dbf->fp);
+ if (ferror(dbf->fp))
+ { xprintf("%s:0x%X: write error - %s\n", dbf->fname,
+#if 0 /* 29/I-2017 */
+ dbf->offset, strerror(errno));
+#else
+ dbf->offset, xstrerr(errno));
+#endif
+ longjmp(dbf->jump, 0);
+ }
+skip: ;
+ }
+ xfree(dbf->fname);
+ fclose(dbf->fp);
+ xfree(dbf);
+ return ret;
+}
+
+/**********************************************************************/
+
+#define TAB_CSV 1
+#define TAB_XBASE 2
+#define TAB_ODBC 3
+#define TAB_MYSQL 4
+
+void mpl_tab_drv_open(MPL *mpl, int mode)
+{ TABDCA *dca = mpl->dca;
+ xassert(dca->id == 0);
+ xassert(dca->link == NULL);
+ xassert(dca->na >= 1);
+ if (strcmp(dca->arg[1], "CSV") == 0)
+ { dca->id = TAB_CSV;
+ dca->link = csv_open_file(dca, mode);
+ }
+ else if (strcmp(dca->arg[1], "xBASE") == 0)
+ { dca->id = TAB_XBASE;
+ dca->link = dbf_open_file(dca, mode);
+ }
+ else if (strcmp(dca->arg[1], "ODBC") == 0 ||
+ strcmp(dca->arg[1], "iODBC") == 0)
+ { dca->id = TAB_ODBC;
+ dca->link = db_iodbc_open(dca, mode);
+ }
+ else if (strcmp(dca->arg[1], "MySQL") == 0)
+ { dca->id = TAB_MYSQL;
+ dca->link = db_mysql_open(dca, mode);
+ }
+ else
+ xprintf("Invalid table driver '%s'\n", dca->arg[1]);
+ if (dca->link == NULL)
+ error(mpl, "error on opening table %s",
+ mpl->stmt->u.tab->name);
+ return;
+}
+
+int mpl_tab_drv_read(MPL *mpl)
+{ TABDCA *dca = mpl->dca;
+ int ret;
+ switch (dca->id)
+ { case TAB_CSV:
+ ret = csv_read_record(dca, dca->link);
+ break;
+ case TAB_XBASE:
+ ret = dbf_read_record(dca, dca->link);
+ break;
+ case TAB_ODBC:
+ ret = db_iodbc_read(dca, dca->link);
+ break;
+ case TAB_MYSQL:
+ ret = db_mysql_read(dca, dca->link);
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ if (ret > 0)
+ error(mpl, "error on reading data from table %s",
+ mpl->stmt->u.tab->name);
+ return ret;
+}
+
+void mpl_tab_drv_write(MPL *mpl)
+{ TABDCA *dca = mpl->dca;
+ int ret;
+ switch (dca->id)
+ { case TAB_CSV:
+ ret = csv_write_record(dca, dca->link);
+ break;
+ case TAB_XBASE:
+ ret = dbf_write_record(dca, dca->link);
+ break;
+ case TAB_ODBC:
+ ret = db_iodbc_write(dca, dca->link);
+ break;
+ case TAB_MYSQL:
+ ret = db_mysql_write(dca, dca->link);
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ if (ret)
+ error(mpl, "error on writing data to table %s",
+ mpl->stmt->u.tab->name);
+ return;
+}
+
+void mpl_tab_drv_close(MPL *mpl)
+{ TABDCA *dca = mpl->dca;
+ int ret;
+ switch (dca->id)
+ { case TAB_CSV:
+ ret = csv_close_file(dca, dca->link);
+ break;
+ case TAB_XBASE:
+ ret = dbf_close_file(dca, dca->link);
+ break;
+ case TAB_ODBC:
+ ret = db_iodbc_close(dca, dca->link);
+ break;
+ case TAB_MYSQL:
+ ret = db_mysql_close(dca, dca->link);
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ dca->id = 0;
+ dca->link = NULL;
+ if (ret)
+ error(mpl, "error on closing table %s",
+ mpl->stmt->u.tab->name);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mplsql.c b/test/monniaux/glpk-4.65/src/mpl/mplsql.c
new file mode 100644
index 00000000..fcd2afa6
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mplsql.c
@@ -0,0 +1,1659 @@
+/* mplsql.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Author: Heinrich Schuchardt <xypron.glpk@gmx.de>.
+*
+* Copyright (C) 2003-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "mpl.h"
+#include "mplsql.h"
+
+#ifdef ODBC_DLNAME
+#define HAVE_ODBC
+#define libodbc ODBC_DLNAME
+#define h_odbc (get_env_ptr()->h_odbc)
+#endif
+
+#ifdef MYSQL_DLNAME
+#define HAVE_MYSQL
+#define libmysql MYSQL_DLNAME
+#define h_mysql (get_env_ptr()->h_mysql)
+#endif
+
+static void *db_iodbc_open_int(TABDCA *dca, int mode, const char
+ **sqllines);
+static void *db_mysql_open_int(TABDCA *dca, int mode, const char
+ **sqllines);
+
+/**********************************************************************/
+
+#if defined(HAVE_ODBC) || defined(HAVE_MYSQL)
+
+#define SQL_FIELD_MAX 100
+/* maximal field count */
+
+#define SQL_FDLEN_MAX 255
+/* maximal field length */
+
+/***********************************************************************
+* NAME
+*
+* args_concat - concatenate arguments
+*
+* SYNOPSIS
+*
+* static char **args_concat(TABDCA *dca);
+*
+* DESCRIPTION
+*
+* The arguments passed in dca are SQL statements. A SQL statement may
+* be split over multiple arguments. The last argument of a SQL
+* statement will be terminated with a semilocon. Each SQL statement is
+* merged into a single zero terminated string. Boundaries between
+* arguments are replaced by space.
+*
+* RETURNS
+*
+* Buffer with SQL statements */
+
+static char **args_concat(TABDCA *dca)
+{
+ const char *arg;
+ int i;
+ int j;
+ int j0;
+ int j1;
+ size_t len;
+ int lentot;
+ int narg;
+ int nline = 0;
+ char **sqllines = NULL;
+
+ narg = mpl_tab_num_args(dca);
+ /* The SQL statements start with argument 3. */
+ if (narg < 3)
+ return NULL;
+ /* Count the SQL statements */
+ for (j = 3; j <= narg; j++)
+ {
+ arg = mpl_tab_get_arg(dca, j);
+ len = strlen(arg);
+ if (arg[len-1] == ';' || j == narg)
+ nline ++;
+ }
+ /* Allocate string buffer. */
+ sqllines = (char **) xmalloc((nline+1) * sizeof(char **));
+ /* Join arguments */
+ sqllines[0] = NULL;
+ j0 = 3;
+ i = 0;
+ lentot = 0;
+ for (j = 3; j <= narg; j++)
+ {
+ arg = mpl_tab_get_arg(dca, j);
+ len = strlen(arg);
+ /* add length of part */
+ lentot += len;
+ /* add length of space separating parts or 0x00 at end of SQL
+ statement */
+ lentot++;
+ if (arg[len-1] == ';' || j == narg)
+ { /* Join arguments for a single SQL statement */
+ sqllines[i] = xmalloc(lentot);
+ sqllines[i+1] = NULL;
+ sqllines[i][0] = 0x00;
+ for (j1 = j0; j1 <= j; j1++)
+ { if(j1>j0)
+ strcat(sqllines[i], " ");
+ strcat(sqllines[i], mpl_tab_get_arg(dca, j1));
+ }
+ len = strlen(sqllines[i]);
+ if (sqllines[i][len-1] == ';')
+ sqllines[i][len-1] = 0x00;
+ j0 = j+1;
+ i++;
+ lentot = 0;
+ }
+ }
+ return sqllines;
+}
+
+/***********************************************************************
+* NAME
+*
+* free_buffer - free multiline string buffer
+*
+* SYNOPSIS
+*
+* static void free_buffer(char **buf);
+*
+* DESCRIPTION
+*
+* buf is a list of strings terminated by NULL.
+* The memory for the strings and for the list is released. */
+
+static void free_buffer(char **buf)
+{ int i;
+
+ for(i = 0; buf[i] != NULL; i++)
+ xfree(buf[i]);
+ xfree(buf);
+}
+
+static int db_escaped_string_length(const char* from)
+/* length of escaped string */
+{
+ int count;
+ const char *pointer;
+
+ for (pointer = from, count = 0; *pointer != (char) '\0'; pointer++,
+ count++)
+ {
+ switch (*pointer)
+ {
+ case '\'':
+ count++;
+ break;
+ }
+ }
+
+ return count;
+}
+
+static void db_escape_string (char *to, const char *from)
+/* escape string*/
+{
+ const char *source = from;
+ char *target = to;
+ size_t remaining;
+
+ remaining = strlen(from);
+
+ if (to == NULL)
+ to = (char *) (from + remaining);
+
+ while (remaining > 0)
+ {
+ switch (*source)
+ {
+ case '\'':
+ *target = '\'';
+ target++;
+ *target = '\'';
+ break;
+
+ default:
+ *target = *source;
+ }
+ source++;
+ target++;
+ remaining--;
+ }
+
+ /* Write the terminating NUL character. */
+ *target = '\0';
+}
+
+static char *db_generate_select_stmt(TABDCA *dca)
+/* generate select statement */
+{
+ char *arg;
+ char const *field;
+ char *query;
+ int j;
+ int narg;
+ int nf;
+ int total;
+
+ total = 50;
+ nf = mpl_tab_num_flds(dca);
+ narg = mpl_tab_num_args(dca);
+ for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
+ {
+ field = mpl_tab_get_name(dca, j);
+ total += strlen(field);
+ total += 2;
+ }
+ arg = (char *) mpl_tab_get_arg(dca, narg);
+ total += strlen(arg);
+ query = xmalloc( total * sizeof(char));
+ strcpy (query, "SELECT ");
+ for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
+ {
+ field = mpl_tab_get_name(dca, j);
+ strcat(query, field);
+ if ( j < nf )
+ strcat(query, ", ");
+ }
+ strcat(query, " FROM ");
+ strcat(query, arg);
+ return query;
+}
+
+static char *db_generate_insert_stmt(TABDCA *dca)
+/* generate insert statement */
+{
+ char *arg;
+ char const *field;
+ char *query;
+ int j;
+ int narg;
+ int nf;
+ int total;
+
+ total = 50;
+ nf = mpl_tab_num_flds(dca);
+ narg = mpl_tab_num_args(dca);
+ for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
+ {
+ field = mpl_tab_get_name(dca, j);
+ total += strlen(field);
+ total += 5;
+ }
+ arg = (char *) mpl_tab_get_arg(dca, narg);
+ total += strlen(arg);
+ query = xmalloc( (total+1) * sizeof(char));
+ strcpy (query, "INSERT INTO ");
+ strcat(query, arg);
+ strcat(query, " ( ");
+ for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
+ {
+ field = mpl_tab_get_name(dca, j);
+ strcat(query, field);
+ if ( j < nf )
+ strcat(query, ", ");
+ }
+ strcat(query, " ) VALUES ( ");
+ for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
+ {
+ strcat(query, "?");
+ if ( j < nf )
+ strcat(query, ", ");
+ }
+ strcat(query, " )");
+ return query;
+}
+
+#endif
+
+/**********************************************************************/
+
+#ifndef HAVE_ODBC
+
+void *db_iodbc_open(TABDCA *dca, int mode)
+{ xassert(dca == dca);
+ xassert(mode == mode);
+ xprintf("iODBC table driver not supported\n");
+ return NULL;
+}
+
+int db_iodbc_read(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+int db_iodbc_write(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+int db_iodbc_close(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+#else
+
+#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__WOE__)
+#include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqlext.h>
+
+struct db_odbc
+{
+ int mode; /*'R' = Read, 'W' = Write*/
+ SQLHDBC hdbc; /*connection handle*/
+ SQLHENV henv; /*environment handle*/
+ SQLHSTMT hstmt; /*statement handle*/
+ SQLSMALLINT nresultcols; /* columns in result*/
+ SQLULEN collen[SQL_FIELD_MAX+1];
+ SQLLEN outlen[SQL_FIELD_MAX+1];
+ SQLSMALLINT coltype[SQL_FIELD_MAX+1];
+ SQLCHAR data[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];
+#if 1 /* 12/I-2014 */
+ SQLDOUBLE datanum[SQL_FIELD_MAX+1];
+#endif
+ SQLCHAR colname[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];
+ int isnumeric[SQL_FIELD_MAX+1];
+ int nf;
+ /* number of fields in the csv file */
+ int ref[1+SQL_FIELD_MAX];
+ /* ref[k] = k', if k-th field of the csv file corresponds to
+ k'-th field in the table statement; if ref[k] = 0, k-th field
+ of the csv file is ignored */
+ SQLCHAR *query;
+ /* query generated by db_iodbc_open */
+};
+
+SQLRETURN SQL_API dl_SQLAllocHandle (
+ SQLSMALLINT HandleType,
+ SQLHANDLE InputHandle,
+ SQLHANDLE *OutputHandle)
+{
+ typedef SQLRETURN SQL_API ep_SQLAllocHandle(
+ SQLSMALLINT HandleType,
+ SQLHANDLE InputHandle,
+ SQLHANDLE *OutputHandle);
+
+ ep_SQLAllocHandle *fn;
+ fn = (ep_SQLAllocHandle *) xdlsym(h_odbc, "SQLAllocHandle");
+ xassert(fn != NULL);
+ return (*fn)(HandleType, InputHandle, OutputHandle);
+}
+
+SQLRETURN SQL_API dl_SQLBindCol (
+ SQLHSTMT StatementHandle,
+ SQLUSMALLINT ColumnNumber,
+ SQLSMALLINT TargetType,
+ SQLPOINTER TargetValue,
+ SQLLEN BufferLength,
+ SQLLEN *StrLen_or_Ind)
+{
+ typedef SQLRETURN SQL_API ep_SQLBindCol(
+ SQLHSTMT StatementHandle,
+ SQLUSMALLINT ColumnNumber,
+ SQLSMALLINT TargetType,
+ SQLPOINTER TargetValue,
+ SQLLEN BufferLength,
+ SQLLEN *StrLen_or_Ind);
+ ep_SQLBindCol *fn;
+ fn = (ep_SQLBindCol *) xdlsym(h_odbc, "SQLBindCol");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle, ColumnNumber, TargetType,
+ TargetValue, BufferLength, StrLen_or_Ind);
+}
+
+SQLRETURN SQL_API dl_SQLCloseCursor (
+ SQLHSTMT StatementHandle)
+{
+ typedef SQLRETURN SQL_API ep_SQLCloseCursor (
+ SQLHSTMT StatementHandle);
+
+ ep_SQLCloseCursor *fn;
+ fn = (ep_SQLCloseCursor *) xdlsym(h_odbc, "SQLCloseCursor");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle);
+}
+
+
+SQLRETURN SQL_API dl_SQLDisconnect (
+ SQLHDBC ConnectionHandle)
+{
+ typedef SQLRETURN SQL_API ep_SQLDisconnect(
+ SQLHDBC ConnectionHandle);
+
+ ep_SQLDisconnect *fn;
+ fn = (ep_SQLDisconnect *) xdlsym(h_odbc, "SQLDisconnect");
+ xassert(fn != NULL);
+ return (*fn)(ConnectionHandle);
+}
+
+SQLRETURN SQL_API dl_SQLDriverConnect (
+ SQLHDBC hdbc,
+ SQLHWND hwnd,
+ SQLCHAR *szConnStrIn,
+ SQLSMALLINT cbConnStrIn,
+ SQLCHAR *szConnStrOut,
+ SQLSMALLINT cbConnStrOutMax,
+ SQLSMALLINT *pcbConnStrOut,
+ SQLUSMALLINT fDriverCompletion)
+{
+ typedef SQLRETURN SQL_API ep_SQLDriverConnect(
+ SQLHDBC hdbc,
+ SQLHWND hwnd,
+ SQLCHAR * szConnStrIn,
+ SQLSMALLINT cbConnStrIn,
+ SQLCHAR * szConnStrOut,
+ SQLSMALLINT cbConnStrOutMax,
+ SQLSMALLINT * pcbConnStrOut,
+ SQLUSMALLINT fDriverCompletion);
+
+ ep_SQLDriverConnect *fn;
+ fn = (ep_SQLDriverConnect *) xdlsym(h_odbc, "SQLDriverConnect");
+ xassert(fn != NULL);
+ return (*fn)(hdbc, hwnd, szConnStrIn, cbConnStrIn, szConnStrOut,
+ cbConnStrOutMax, pcbConnStrOut, fDriverCompletion);
+}
+
+SQLRETURN SQL_API dl_SQLEndTran (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle,
+ SQLSMALLINT CompletionType)
+{
+ typedef SQLRETURN SQL_API ep_SQLEndTran (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle,
+ SQLSMALLINT CompletionType);
+
+ ep_SQLEndTran *fn;
+ fn = (ep_SQLEndTran *) xdlsym(h_odbc, "SQLEndTran");
+ xassert(fn != NULL);
+ return (*fn)(HandleType, Handle, CompletionType);
+}
+
+SQLRETURN SQL_API dl_SQLExecDirect (
+ SQLHSTMT StatementHandle,
+ SQLCHAR * StatementText,
+ SQLINTEGER TextLength)
+{
+ typedef SQLRETURN SQL_API ep_SQLExecDirect (
+ SQLHSTMT StatementHandle,
+ SQLCHAR * StatementText,
+ SQLINTEGER TextLength);
+
+ ep_SQLExecDirect *fn;
+ fn = (ep_SQLExecDirect *) xdlsym(h_odbc, "SQLExecDirect");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle, StatementText, TextLength);
+}
+
+SQLRETURN SQL_API dl_SQLFetch (
+ SQLHSTMT StatementHandle)
+{
+ typedef SQLRETURN SQL_API ep_SQLFetch (
+ SQLHSTMT StatementHandle);
+
+ ep_SQLFetch *fn;
+ fn = (ep_SQLFetch*) xdlsym(h_odbc, "SQLFetch");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle);
+}
+
+SQLRETURN SQL_API dl_SQLFreeHandle (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle)
+{
+ typedef SQLRETURN SQL_API ep_SQLFreeHandle (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle);
+
+ ep_SQLFreeHandle *fn;
+ fn = (ep_SQLFreeHandle *) xdlsym(h_odbc, "SQLFreeHandle");
+ xassert(fn != NULL);
+ return (*fn)(HandleType, Handle);
+}
+
+SQLRETURN SQL_API dl_SQLDescribeCol (
+ SQLHSTMT StatementHandle,
+ SQLUSMALLINT ColumnNumber,
+ SQLCHAR * ColumnName,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT * NameLength,
+ SQLSMALLINT * DataType,
+ SQLULEN * ColumnSize,
+ SQLSMALLINT * DecimalDigits,
+ SQLSMALLINT * Nullable)
+{
+ typedef SQLRETURN SQL_API ep_SQLDescribeCol (
+ SQLHSTMT StatementHandle,
+ SQLUSMALLINT ColumnNumber,
+ SQLCHAR *ColumnName,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT *NameLength,
+ SQLSMALLINT *DataType,
+ SQLULEN *ColumnSize,
+ SQLSMALLINT *DecimalDigits,
+ SQLSMALLINT *Nullable);
+
+ ep_SQLDescribeCol *fn;
+ fn = (ep_SQLDescribeCol *) xdlsym(h_odbc, "SQLDescribeCol");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle, ColumnNumber, ColumnName,
+ BufferLength, NameLength,
+ DataType, ColumnSize, DecimalDigits, Nullable);
+}
+
+SQLRETURN SQL_API dl_SQLGetDiagRec (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle,
+ SQLSMALLINT RecNumber,
+ SQLCHAR *Sqlstate,
+ SQLINTEGER *NativeError,
+ SQLCHAR *MessageText,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT *TextLength)
+{
+ typedef SQLRETURN SQL_API ep_SQLGetDiagRec (
+ SQLSMALLINT HandleType,
+ SQLHANDLE Handle,
+ SQLSMALLINT RecNumber,
+ SQLCHAR *Sqlstate,
+ SQLINTEGER *NativeError,
+ SQLCHAR *MessageText,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT *TextLength);
+
+ ep_SQLGetDiagRec *fn;
+ fn = (ep_SQLGetDiagRec *) xdlsym(h_odbc, "SQLGetDiagRec");
+ xassert(fn != NULL);
+ return (*fn)(HandleType, Handle, RecNumber, Sqlstate,
+ NativeError, MessageText, BufferLength, TextLength);
+}
+
+SQLRETURN SQL_API dl_SQLGetInfo (
+ SQLHDBC ConnectionHandle,
+ SQLUSMALLINT InfoType,
+ SQLPOINTER InfoValue,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT *StringLength)
+{
+ typedef SQLRETURN SQL_API ep_SQLGetInfo (
+ SQLHDBC ConnectionHandle,
+ SQLUSMALLINT InfoType,
+ SQLPOINTER InfoValue,
+ SQLSMALLINT BufferLength,
+ SQLSMALLINT *StringLength);
+
+ ep_SQLGetInfo *fn;
+ fn = (ep_SQLGetInfo *) xdlsym(h_odbc, "SQLGetInfo");
+ xassert(fn != NULL);
+ return (*fn)(ConnectionHandle, InfoType, InfoValue, BufferLength,
+ StringLength);
+}
+
+SQLRETURN SQL_API dl_SQLNumResultCols (
+ SQLHSTMT StatementHandle,
+ SQLSMALLINT *ColumnCount)
+{
+ typedef SQLRETURN SQL_API ep_SQLNumResultCols (
+ SQLHSTMT StatementHandle,
+ SQLSMALLINT *ColumnCount);
+
+ ep_SQLNumResultCols *fn;
+ fn = (ep_SQLNumResultCols *) xdlsym(h_odbc, "SQLNumResultCols");
+ xassert(fn != NULL);
+ return (*fn)(StatementHandle, ColumnCount);
+}
+
+SQLRETURN SQL_API dl_SQLSetConnectAttr (
+ SQLHDBC ConnectionHandle,
+ SQLINTEGER Attribute,
+ SQLPOINTER Value,
+ SQLINTEGER StringLength)
+{
+ typedef SQLRETURN SQL_API ep_SQLSetConnectAttr (
+ SQLHDBC ConnectionHandle,
+ SQLINTEGER Attribute,
+ SQLPOINTER Value,
+ SQLINTEGER StringLength);
+
+ ep_SQLSetConnectAttr *fn;
+ fn = (ep_SQLSetConnectAttr *) xdlsym(h_odbc, "SQLSetConnectAttr");
+ xassert(fn != NULL);
+ return (*fn)(ConnectionHandle, Attribute, Value, StringLength);
+}
+
+SQLRETURN SQL_API dl_SQLSetEnvAttr (
+ SQLHENV EnvironmentHandle,
+ SQLINTEGER Attribute,
+ SQLPOINTER Value,
+ SQLINTEGER StringLength)
+{
+ typedef SQLRETURN SQL_API ep_SQLSetEnvAttr (
+ SQLHENV EnvironmentHandle,
+ SQLINTEGER Attribute,
+ SQLPOINTER Value,
+ SQLINTEGER StringLength);
+
+ ep_SQLSetEnvAttr *fn;
+ fn = (ep_SQLSetEnvAttr *) xdlsym(h_odbc, "SQLSetEnvAttr");
+ xassert(fn != NULL);
+ return (*fn)(EnvironmentHandle, Attribute, Value, StringLength);
+}
+
+static void extract_error(
+ char *fn,
+ SQLHANDLE handle,
+ SQLSMALLINT type);
+
+static int is_numeric(
+ SQLSMALLINT coltype);
+
+/***********************************************************************
+* NAME
+*
+* db_iodbc_open - open connection to ODBC data base
+*
+* SYNOPSIS
+*
+* #include "mplsql.h"
+* void *db_iodbc_open(TABDCA *dca, int mode);
+*
+* DESCRIPTION
+*
+* The routine db_iodbc_open opens a connection to an ODBC data base.
+* It then executes the sql statements passed.
+*
+* In the case of table read the SELECT statement is executed.
+*
+* In the case of table write the INSERT statement is prepared.
+* RETURNS
+*
+* The routine returns a pointer to data storage area created. */
+void *db_iodbc_open(TABDCA *dca, int mode)
+{ void *ret;
+ char **sqllines;
+
+ sqllines = args_concat(dca);
+ if (sqllines == NULL)
+ { xprintf("Missing arguments in table statement.\n"
+ "Please, supply table driver, dsn, and query.\n");
+ return NULL;
+ }
+ ret = db_iodbc_open_int(dca, mode, (const char **) sqllines);
+ free_buffer(sqllines);
+ return ret;
+}
+
+static void *db_iodbc_open_int(TABDCA *dca, int mode, const char
+ **sqllines)
+{
+ struct db_odbc *sql;
+ SQLRETURN ret;
+ SQLCHAR FAR *dsn;
+ SQLCHAR info[256];
+ SQLSMALLINT colnamelen;
+ SQLSMALLINT nullable;
+ SQLSMALLINT scale;
+ const char *arg;
+ int narg;
+ int i, j;
+ int total;
+
+ if (libodbc == NULL)
+ {
+ xprintf("No loader for shared ODBC library available\n");
+ return NULL;
+ }
+
+ if (h_odbc == NULL)
+ {
+ h_odbc = xdlopen(libodbc);
+ if (h_odbc == NULL)
+ { xprintf("unable to open library %s\n", libodbc);
+ xprintf("%s\n", get_err_msg());
+ return NULL;
+ }
+ }
+
+ sql = (struct db_odbc *) xmalloc(sizeof(struct db_odbc));
+ if (sql == NULL)
+ return NULL;
+
+ sql->mode = mode;
+ sql->hdbc = NULL;
+ sql->henv = NULL;
+ sql->hstmt = NULL;
+ sql->query = NULL;
+ narg = mpl_tab_num_args(dca);
+
+ dsn = (SQLCHAR FAR *) mpl_tab_get_arg(dca, 2);
+ /* allocate an environment handle */
+ ret = dl_SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,
+ &(sql->henv));
+ /* set attribute to enable application to run as ODBC 3.0
+ application */
+ ret = dl_SQLSetEnvAttr(sql->henv, SQL_ATTR_ODBC_VERSION,
+ (void *) SQL_OV_ODBC3, 0);
+ /* allocate a connection handle */
+ ret = dl_SQLAllocHandle(SQL_HANDLE_DBC, sql->henv, &(sql->hdbc));
+ /* connect */
+ ret = dl_SQLDriverConnect(sql->hdbc, NULL, dsn, SQL_NTS, NULL, 0,
+ NULL, SQL_DRIVER_COMPLETE);
+ if (SQL_SUCCEEDED(ret))
+ { /* output information about data base connection */
+ xprintf("Connected to ");
+ dl_SQLGetInfo(sql->hdbc, SQL_DBMS_NAME, (SQLPOINTER)info,
+ sizeof(info), NULL);
+ xprintf("%s ", info);
+ dl_SQLGetInfo(sql->hdbc, SQL_DBMS_VER, (SQLPOINTER)info,
+ sizeof(info), NULL);
+ xprintf("%s - ", info);
+ dl_SQLGetInfo(sql->hdbc, SQL_DATABASE_NAME, (SQLPOINTER)info,
+ sizeof(info), NULL);
+ xprintf("%s\n", info);
+ }
+ else
+ { /* describe error */
+ xprintf("Failed to connect\n");
+ extract_error("SQLDriverConnect", sql->hdbc, SQL_HANDLE_DBC);
+ dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
+ xfree(sql);
+ return NULL;
+ }
+ /* set AUTOCOMMIT on*/
+ ret = dl_SQLSetConnectAttr(sql->hdbc, SQL_ATTR_AUTOCOMMIT,
+ (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);
+ /* allocate a statement handle */
+ ret = dl_SQLAllocHandle(SQL_HANDLE_STMT, sql->hdbc, &(sql->hstmt));
+
+ /* initialization queries */
+ for(j = 0; sqllines[j+1] != NULL; j++)
+ {
+ sql->query = (SQLCHAR *) sqllines[j];
+ xprintf("%s\n", sql->query);
+ ret = dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS);
+ switch (ret)
+ {
+ case SQL_SUCCESS:
+ case SQL_SUCCESS_WITH_INFO:
+ case SQL_NO_DATA_FOUND:
+ break;
+ default:
+ xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n",
+ sql->query);
+ extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
+ dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
+ dl_SQLDisconnect(sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
+ xfree(sql);
+ return NULL;
+ }
+ /* commit statement */
+ dl_SQLEndTran(SQL_HANDLE_ENV, sql->henv, SQL_COMMIT);
+ }
+
+ if ( sql->mode == 'R' )
+ { sql->nf = mpl_tab_num_flds(dca);
+ for(j = 0; sqllines[j] != NULL; j++)
+ arg = sqllines[j];
+ total = strlen(arg);
+ if (total > 7 && 0 == strncmp(arg, "SELECT ", 7))
+ {
+ total = strlen(arg);
+ sql->query = xmalloc( (total+1) * sizeof(char));
+ strcpy (sql->query, arg);
+ }
+ else
+ {
+ sql->query = db_generate_select_stmt(dca);
+ }
+ xprintf("%s\n", sql->query);
+ if (dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS) !=
+ SQL_SUCCESS)
+ {
+ xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n", sql->query);
+ extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
+ dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
+ dl_SQLDisconnect(sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
+ xfree(sql->query);
+ xfree(sql);
+ return NULL;
+ }
+ xfree(sql->query);
+ /* determine number of result columns */
+ ret = dl_SQLNumResultCols(sql->hstmt, &sql->nresultcols);
+ total = sql->nresultcols;
+ if (total > SQL_FIELD_MAX)
+ { xprintf("db_iodbc_open: Too many fields (> %d) in query.\n"
+ "\"%s\"\n", SQL_FIELD_MAX, sql->query);
+ dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
+ dl_SQLDisconnect(sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
+ xfree(sql->query);
+ return NULL;
+ }
+ for (i = 1; i <= total; i++)
+ { /* return a set of attributes for a column */
+ ret = dl_SQLDescribeCol(sql->hstmt, (SQLSMALLINT) i,
+ sql->colname[i], SQL_FDLEN_MAX,
+ &colnamelen, &(sql->coltype[i]), &(sql->collen[i]), &scale,
+ &nullable);
+ sql->isnumeric[i] = is_numeric(sql->coltype[i]);
+ /* bind columns to program vars, converting all types to CHAR*/
+ if (sql->isnumeric[i])
+#if 0 /* 12/I-2014 */
+ { dl_SQLBindCol(sql->hstmt, i, SQL_DOUBLE, sql->data[i],
+#else
+ { dl_SQLBindCol(sql->hstmt, i, SQL_DOUBLE, &sql->datanum[i],
+#endif
+ SQL_FDLEN_MAX, &(sql->outlen[i]));
+ } else
+ { dl_SQLBindCol(sql->hstmt, i, SQL_CHAR, sql->data[i],
+ SQL_FDLEN_MAX, &(sql->outlen[i]));
+ }
+ for (j = sql->nf; j >= 1; j--)
+ { if (strcmp(mpl_tab_get_name(dca, j), sql->colname[i]) == 0)
+ break;
+ }
+ sql->ref[i] = j;
+ }
+ }
+ else if ( sql->mode == 'W' )
+ { for(j = 0; sqllines[j] != NULL; j++)
+ arg = sqllines[j];
+ if ( NULL != strchr(arg, '?') )
+ {
+ total = strlen(arg);
+ sql->query = xmalloc( (total+1) * sizeof(char));
+ strcpy (sql->query, arg);
+ }
+ else
+ {
+ sql->query = db_generate_insert_stmt(dca);
+ }
+ xprintf("%s\n", sql->query);
+ }
+ return sql;
+}
+
+int db_iodbc_read(TABDCA *dca, void *link)
+{
+ struct db_odbc *sql;
+ SQLRETURN ret;
+ char buf[SQL_FDLEN_MAX+1];
+ int i;
+ int len;
+ double num;
+
+ sql = (struct db_odbc *) link;
+
+ xassert(sql != NULL);
+ xassert(sql->mode == 'R');
+
+ ret=dl_SQLFetch(sql->hstmt);
+ if (ret== SQL_ERROR)
+ return -1;
+ if (ret== SQL_NO_DATA_FOUND)
+ return -1; /*EOF*/
+ for (i=1; i <= sql->nresultcols; i++)
+ {
+ if (sql->ref[i] > 0)
+ {
+ len = sql->outlen[i];
+ if (len != SQL_NULL_DATA)
+ {
+ if (sql->isnumeric[i])
+ { mpl_tab_set_num(dca, sql->ref[i],
+#if 0 /* 12/I-2014 */
+ *((const double *) sql->data[i]));
+#else
+ (const double) sql->datanum[i]);
+#endif
+ }
+ else
+ { if (len > SQL_FDLEN_MAX)
+ len = SQL_FDLEN_MAX;
+ else if (len < 0)
+ len = 0;
+ strncpy(buf, (const char *) sql->data[i], len);
+ buf[len] = 0x00;
+ mpl_tab_set_str(dca, sql->ref[i], strtrim(buf));
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+int db_iodbc_write(TABDCA *dca, void *link)
+{
+ struct db_odbc *sql;
+ char *part;
+ char *query;
+ char *template;
+ char num[50];
+ int k;
+ int len;
+ int nf;
+
+ sql = (struct db_odbc *) link;
+ xassert(sql != NULL);
+ xassert(sql->mode == 'W');
+
+ len = strlen(sql->query);
+ template = (char *) xmalloc( (len + 1) * sizeof(char) );
+ strcpy(template, sql->query);
+
+ nf = mpl_tab_num_flds(dca);
+ for (k = 1; k <= nf; k++)
+ { switch (mpl_tab_get_type(dca, k))
+ { case 'N':
+ len += 20;
+ break;
+ case 'S':
+ len += db_escaped_string_length(mpl_tab_get_str(dca, k));
+ len += 2;
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ }
+ query = xmalloc( (len + 1 ) * sizeof(char) );
+ query[0] = 0x00;
+#if 0 /* 29/I-2017 */
+ for (k = 1, part = strtok (template, "?"); (part != NULL);
+ part = strtok (NULL, "?"), k++)
+#else
+ for (k = 1, part = xstrtok (template, "?"); (part != NULL);
+ part = xstrtok (NULL, "?"), k++)
+#endif
+ {
+ if (k > nf) break;
+ strcat( query, part );
+ switch (mpl_tab_get_type(dca, k))
+ { case 'N':
+#if 0 /* 02/XI-2010 by xypron */
+ sprintf(num, "%-18g",mpl_tab_get_num(dca, k));
+#else
+ sprintf(num, "%.*g", DBL_DIG, mpl_tab_get_num(dca, k));
+#endif
+ strcat( query, num );
+ break;
+ case 'S':
+ strcat( query, "'");
+ db_escape_string( query + strlen(query),
+ mpl_tab_get_str(dca, k) );
+ strcat( query, "'");
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ }
+ if (part != NULL)
+ strcat(query, part);
+ if (dl_SQLExecDirect(sql->hstmt, (SQLCHAR *) query, SQL_NTS)
+ != SQL_SUCCESS)
+ {
+ xprintf("db_iodbc_write: Query\n\"%s\"\nfailed.\n", query);
+ extract_error("SQLExecDirect", sql->hdbc, SQL_HANDLE_DBC);
+ xfree(query);
+ xfree(template);
+ return 1;
+ }
+
+ xfree(query);
+ xfree(template);
+ return 0;
+}
+
+int db_iodbc_close(TABDCA *dca, void *link)
+{
+ struct db_odbc *sql;
+
+ sql = (struct db_odbc *) link;
+ xassert(sql != NULL);
+ /* Commit */
+ if ( sql->mode == 'W' )
+ dl_SQLEndTran(SQL_HANDLE_ENV, sql->henv, SQL_COMMIT);
+ if ( sql->mode == 'R' )
+ dl_SQLCloseCursor(sql->hstmt);
+
+ dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
+ dl_SQLDisconnect(sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
+ dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
+ if ( sql->mode == 'W' )
+ xfree(sql->query);
+ xfree(sql);
+ dca->link = NULL;
+ return 0;
+}
+
+static void extract_error(
+ char *fn,
+ SQLHANDLE handle,
+ SQLSMALLINT type)
+{
+ SQLINTEGER i = 0;
+ SQLINTEGER native;
+ SQLCHAR state[ 7 ];
+ SQLCHAR text[256];
+ SQLSMALLINT len;
+ SQLRETURN ret;
+
+ xprintf("\nThe driver reported the following diagnostics whilst "
+ "running %s\n", fn);
+
+ do
+ {
+ ret = dl_SQLGetDiagRec(type, handle, ++i, state, &native, text,
+ sizeof(text), &len );
+ if (SQL_SUCCEEDED(ret))
+ xprintf("%s:%ld:%ld:%s\n", state, i, native, text);
+ }
+ while( ret == SQL_SUCCESS );
+}
+
+static int is_numeric(SQLSMALLINT coltype)
+{
+ int ret = 0;
+ switch (coltype)
+ {
+ case SQL_DECIMAL:
+ case SQL_NUMERIC:
+ case SQL_SMALLINT:
+ case SQL_INTEGER:
+ case SQL_REAL:
+ case SQL_FLOAT:
+ case SQL_DOUBLE:
+ case SQL_TINYINT:
+ case SQL_BIGINT:
+ ret = 1;
+ break;
+ }
+ return ret;
+}
+
+#endif
+
+/**********************************************************************/
+
+#ifndef HAVE_MYSQL
+
+void *db_mysql_open(TABDCA *dca, int mode)
+{ xassert(dca == dca);
+ xassert(mode == mode);
+ xprintf("MySQL table driver not supported\n");
+ return NULL;
+}
+
+int db_mysql_read(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+int db_mysql_write(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+int db_mysql_close(TABDCA *dca, void *link)
+{ xassert(dca != dca);
+ xassert(link != link);
+ return 0;
+}
+
+#else
+
+#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__WOE__)
+#include <windows.h>
+#endif
+
+#ifdef __CYGWIN__
+#define byte_defined 1
+#endif
+
+#if 0 /* 12/II-2014; to fix namespace bug */
+#include <my_global.h>
+#include <my_sys.h>
+#endif
+#include <mysql.h>
+
+struct db_mysql
+{
+ int mode; /*'R' = Read, 'W' = Write*/
+ MYSQL *con; /*connection*/
+ MYSQL_RES *res; /*result*/
+ int nf;
+ /* number of fields in the csv file */
+ int ref[1+SQL_FIELD_MAX];
+ /* ref[k] = k', if k-th field of the csv file corresponds to
+ k'-th field in the table statement; if ref[k] = 0, k-th field
+ of the csv file is ignored */
+ char *query;
+ /* query generated by db_mysql_open */
+};
+
+void STDCALL dl_mysql_close(MYSQL *sock)
+{
+ typedef void STDCALL ep_mysql_close(MYSQL *sock);
+
+ ep_mysql_close *fn;
+ fn = (ep_mysql_close *) xdlsym(h_mysql, "mysql_close");
+ xassert(fn != NULL);
+ return (*fn)(sock);
+}
+
+const char * STDCALL dl_mysql_error(MYSQL *mysql)
+{
+ typedef const char * STDCALL ep_mysql_error(MYSQL *mysql);
+
+ ep_mysql_error *fn;
+ fn = (ep_mysql_error *) xdlsym(h_mysql, "mysql_error");
+ xassert(fn != NULL);
+ return (*fn)(mysql);
+}
+
+MYSQL_FIELD * STDCALL dl_mysql_fetch_fields(MYSQL_RES *res)
+{
+ typedef MYSQL_FIELD * STDCALL
+ ep_mysql_fetch_fields(MYSQL_RES *res);
+
+ ep_mysql_fetch_fields *fn;
+ fn = (ep_mysql_fetch_fields *) xdlsym(h_mysql, "mysql_fetch_fields");
+ xassert(fn != NULL);
+ return (*fn)(res);
+}
+
+unsigned long * STDCALL dl_mysql_fetch_lengths(MYSQL_RES *result)
+{
+ typedef unsigned long * STDCALL
+ ep_mysql_fetch_lengths(MYSQL_RES *result);
+
+ ep_mysql_fetch_lengths *fn;
+ fn = (ep_mysql_fetch_lengths *) xdlsym(h_mysql,
+ "mysql_fetch_lengths");
+ xassert(fn != NULL);
+ return (*fn)(result);
+}
+
+MYSQL_ROW STDCALL dl_mysql_fetch_row(MYSQL_RES *result)
+{
+ typedef MYSQL_ROW STDCALL ep_mysql_fetch_row(MYSQL_RES *result);
+
+ ep_mysql_fetch_row *fn;
+ fn = (ep_mysql_fetch_row *) xdlsym(h_mysql, "mysql_fetch_row");
+ xassert(fn != NULL);
+ return (*fn)(result);
+}
+
+unsigned int STDCALL dl_mysql_field_count(MYSQL *mysql)
+{
+ typedef unsigned int STDCALL ep_mysql_field_count(MYSQL *mysql);
+
+ ep_mysql_field_count *fn;
+ fn = (ep_mysql_field_count *) xdlsym(h_mysql, "mysql_field_count");
+ xassert(fn != NULL);
+ return (*fn)(mysql);
+}
+
+MYSQL * STDCALL dl_mysql_init(MYSQL *mysql)
+{
+ typedef MYSQL * STDCALL ep_mysql_init(MYSQL *mysql);
+
+ ep_mysql_init *fn;
+ fn = (ep_mysql_init *) xdlsym(h_mysql, "mysql_init");
+ xassert(fn != NULL);
+ return (*fn)(mysql);
+}
+
+unsigned int STDCALL dl_mysql_num_fields(MYSQL_RES *res)
+{
+ typedef unsigned int STDCALL ep_mysql_num_fields(MYSQL_RES *res);
+
+ ep_mysql_num_fields *fn;
+ fn = (ep_mysql_num_fields *) xdlsym(h_mysql, "mysql_num_fields");
+ xassert(fn != NULL);
+ return (*fn)(res);
+}
+
+int STDCALL dl_mysql_query(MYSQL *mysql, const char *q)
+{
+ typedef int STDCALL ep_mysql_query(MYSQL *mysql, const char *q);
+
+ ep_mysql_query *fn;
+ fn = (ep_mysql_query *) xdlsym(h_mysql, "mysql_query");
+ xassert(fn != NULL);
+ return (*fn)(mysql, q);
+}
+
+MYSQL * STDCALL dl_mysql_real_connect(MYSQL *mysql, const char *host,
+ const char *user,
+ const char *passwd,
+ const char *db,
+ unsigned int port,
+ const char *unix_socket,
+ unsigned long clientflag)
+{
+ typedef MYSQL * STDCALL ep_mysql_real_connect(MYSQL *mysql,
+ const char *host,
+ const char *user,
+ const char *passwd,
+ const char *db,
+ unsigned int port,
+ const char *unix_socket,
+ unsigned long clientflag);
+
+ ep_mysql_real_connect *fn;
+ fn = (ep_mysql_real_connect *) xdlsym(h_mysql,
+ "mysql_real_connect");
+ xassert(fn != NULL);
+ return (*fn)(mysql, host, user, passwd, db, port, unix_socket,
+ clientflag);
+}
+
+MYSQL_RES * STDCALL dl_mysql_use_result(MYSQL *mysql)
+{
+ typedef MYSQL_RES * STDCALL ep_mysql_use_result(MYSQL *mysql);
+ ep_mysql_use_result *fn;
+ fn = (ep_mysql_use_result *) xdlsym(h_mysql, "mysql_use_result");
+ xassert(fn != NULL);
+ return (*fn)(mysql);
+}
+
+/***********************************************************************
+* NAME
+*
+* db_mysql_open - open connection to ODBC data base
+*
+* SYNOPSIS
+*
+* #include "mplsql.h"
+* void *db_mysql_open(TABDCA *dca, int mode);
+*
+* DESCRIPTION
+*
+* The routine db_mysql_open opens a connection to a MySQL data base.
+* It then executes the sql statements passed.
+*
+* In the case of table read the SELECT statement is executed.
+*
+* In the case of table write the INSERT statement is prepared.
+* RETURNS
+*
+* The routine returns a pointer to data storage area created. */
+
+void *db_mysql_open(TABDCA *dca, int mode)
+{ void *ret;
+ char **sqllines;
+
+ sqllines = args_concat(dca);
+ if (sqllines == NULL)
+ { xprintf("Missing arguments in table statement.\n"
+ "Please, supply table driver, dsn, and query.\n");
+ return NULL;
+ }
+ ret = db_mysql_open_int(dca, mode, (const char **) sqllines);
+ free_buffer(sqllines);
+ return ret;
+}
+
+static void *db_mysql_open_int(TABDCA *dca, int mode, const char
+ **sqllines)
+{
+ struct db_mysql *sql = NULL;
+ char *arg = NULL;
+ const char *field;
+ MYSQL_FIELD *fields;
+ char *keyword;
+ char *value;
+ char *query;
+ char *dsn;
+/* "Server=[server_name];Database=[database_name];UID=[username];*/
+/* PWD=[password];Port=[port]"*/
+ char *server = NULL; /* Server */
+ char *user = NULL; /* UID */
+ char *password = NULL; /* PWD */
+ char *database = NULL; /* Database */
+ unsigned int port = 0; /* Port */
+ int narg;
+ int i, j, total;
+
+ if (libmysql == NULL)
+ {
+ xprintf("No loader for shared MySQL library available\n");
+ return NULL;
+ }
+
+ if (h_mysql == NULL)
+ {
+ h_mysql = xdlopen(libmysql);
+ if (h_mysql == NULL)
+ { xprintf("unable to open library %s\n", libmysql);
+ xprintf("%s\n", get_err_msg());
+ return NULL;
+ }
+ }
+
+ sql = (struct db_mysql *) xmalloc(sizeof(struct db_mysql));
+ if (sql == NULL)
+ return NULL;
+ sql->mode = mode;
+ sql->res = NULL;
+ sql->query = NULL;
+ sql->nf = mpl_tab_num_flds(dca);
+
+ narg = mpl_tab_num_args(dca);
+ if (narg < 3 )
+ xprintf("MySQL driver: string list too short \n");
+
+ /* get connection string*/
+ dsn = (char *) mpl_tab_get_arg(dca, 2);
+ /* copy connection string*/
+ i = strlen(dsn);
+ i++;
+ arg = xmalloc(i * sizeof(char));
+ strcpy(arg, dsn);
+ /*tokenize connection string*/
+#if 0 /* 29/I-2017 */
+ for (i = 1, keyword = strtok (arg, "="); (keyword != NULL);
+ keyword = strtok (NULL, "="), i++)
+#else
+ for (i = 1, keyword = xstrtok (arg, "="); (keyword != NULL);
+ keyword = xstrtok (NULL, "="), i++)
+#endif
+ {
+#if 0 /* 29/I-2017 */
+ value = strtok (NULL, ";");
+#else
+ value = xstrtok (NULL, ";");
+#endif
+ if (value==NULL)
+ {
+ xprintf("db_mysql_open: Missing value for keyword %s\n",
+ keyword);
+ xfree(arg);
+ xfree(sql);
+ return NULL;
+ }
+ if (0 == strcmp(keyword, "Server"))
+ server = value;
+ else if (0 == strcmp(keyword, "Database"))
+ database = value;
+ else if (0 == strcmp(keyword, "UID"))
+ user = value;
+ else if (0 == strcmp(keyword, "PWD"))
+ password = value;
+ else if (0 == strcmp(keyword, "Port"))
+ port = (unsigned int) atol(value);
+ }
+ /* Connect to database */
+ sql->con = dl_mysql_init(NULL);
+ if (!dl_mysql_real_connect(sql->con, server, user, password, database,
+ port, NULL, 0))
+ {
+ xprintf("db_mysql_open: Connect failed\n");
+ xprintf("%s\n", dl_mysql_error(sql->con));
+ xfree(arg);
+ xfree(sql);
+ return NULL;
+ }
+ xfree(arg);
+
+ for(j = 0; sqllines[j+1] != NULL; j++)
+ { query = (char *) sqllines[j];
+ xprintf("%s\n", query);
+ if (dl_mysql_query(sql->con, query))
+ {
+ xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ dl_mysql_close(sql->con);
+ xfree(sql);
+ return NULL;
+ }
+ }
+
+ if ( sql->mode == 'R' )
+ { sql->nf = mpl_tab_num_flds(dca);
+ for(j = 0; sqllines[j] != NULL; j++)
+ arg = (char *) sqllines[j];
+ total = strlen(arg);
+ if (total > 7 && 0 == strncmp(arg, "SELECT ", 7))
+ {
+ total = strlen(arg);
+ query = xmalloc( (total+1) * sizeof(char));
+ strcpy (query, arg);
+ }
+ else
+ {
+ query = db_generate_select_stmt(dca);
+ }
+ xprintf("%s\n", query);
+ if (dl_mysql_query(sql->con, query))
+ {
+ xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ dl_mysql_close(sql->con);
+ xfree(query);
+ xfree(sql);
+ return NULL;
+ }
+ xfree(query);
+ sql->res = dl_mysql_use_result(sql->con);
+ if (sql->res)
+ {
+ /* create references between query results and table fields*/
+ total = dl_mysql_num_fields(sql->res);
+ if (total > SQL_FIELD_MAX)
+ { xprintf("db_mysql_open: Too many fields (> %d) in query.\n"
+ "\"%s\"\n", SQL_FIELD_MAX, query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ dl_mysql_close(sql->con);
+ xfree(query);
+ xfree(sql);
+ return NULL;
+ }
+ fields = dl_mysql_fetch_fields(sql->res);
+ for (i = 1; i <= total; i++)
+ {
+ for (j = sql->nf; j >= 1; j--)
+ {
+ if (strcmp(mpl_tab_get_name(dca, j), fields[i-1].name)
+ == 0)
+ break;
+ }
+ sql->ref[i] = j;
+ }
+ }
+ else
+ {
+ if(dl_mysql_field_count(sql->con) == 0)
+ {
+ xprintf("db_mysql_open: Query was not a SELECT\n\"%s\"\n",
+ query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ xfree(query);
+ xfree(sql);
+ return NULL;
+ }
+ else
+ {
+ xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ xfree(query);
+ xfree(sql);
+ return NULL;
+ }
+ }
+ }
+ else if ( sql->mode == 'W' )
+ { for(j = 0; sqllines[j] != NULL; j++)
+ arg = (char *) sqllines[j];
+ if ( NULL != strchr(arg, '?') )
+ {
+ total = strlen(arg);
+ query = xmalloc( (total+1) * sizeof(char));
+ strcpy (query, arg);
+ }
+ else
+ query = db_generate_insert_stmt(dca);
+ sql->query = query;
+ xprintf("%s\n", query);
+ }
+ return sql;
+}
+
+int db_mysql_read(TABDCA *dca, void *link)
+{ struct db_mysql *sql;
+ char buf[255+1];
+ char **row;
+ unsigned long *lengths;
+ MYSQL_FIELD *fields;
+ double num;
+ int len;
+ unsigned long num_fields;
+ int i;
+
+ sql = (struct db_mysql *) link;
+
+ xassert(sql != NULL);
+ xassert(sql->mode == 'R');
+ if (NULL == sql->res)
+ {
+ xprintf("db_mysql_read: no result set available");
+ return 1;
+ }
+ if (NULL==(row = (char **)dl_mysql_fetch_row(sql->res))) {
+ return -1; /*EOF*/
+ }
+ lengths = dl_mysql_fetch_lengths(sql->res);
+ fields = dl_mysql_fetch_fields(sql->res);
+ num_fields = dl_mysql_num_fields(sql->res);
+ for (i=1; i <= num_fields; i++)
+ {
+ if (row[i-1] != NULL)
+ { len = (size_t) lengths[i-1];
+ if (len > 255)
+ len = 255;
+ strncpy(buf, (const char *) row[i-1], len);
+ buf[len] = 0x00;
+ if (0 != (fields[i-1].flags & NUM_FLAG))
+ { strspx(buf); /* remove spaces*/
+ if (str2num(buf, &num) != 0)
+ { xprintf("'%s' cannot be converted to a number.\n", buf);
+ return 1;
+ }
+ if (sql->ref[i] > 0)
+ mpl_tab_set_num(dca, sql->ref[i], num);
+ }
+ else
+ { if (sql->ref[i] > 0)
+ mpl_tab_set_str(dca, sql->ref[i], strtrim(buf));
+ }
+ }
+ }
+ return 0;
+}
+
+int db_mysql_write(TABDCA *dca, void *link)
+{
+ struct db_mysql *sql;
+ char *part;
+ char *query;
+ char *template;
+ char num[50];
+ int k;
+ int len;
+ int nf;
+
+ sql = (struct db_mysql *) link;
+ xassert(sql != NULL);
+ xassert(sql->mode == 'W');
+
+ len = strlen(sql->query);
+ template = (char *) xmalloc( (len + 1) * sizeof(char) );
+ strcpy(template, sql->query);
+
+ nf = mpl_tab_num_flds(dca);
+ for (k = 1; k <= nf; k++)
+ { switch (mpl_tab_get_type(dca, k))
+ { case 'N':
+ len += 20;
+ break;
+ case 'S':
+ len += db_escaped_string_length(mpl_tab_get_str(dca, k));
+ len += 2;
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ }
+ query = xmalloc( (len + 1 ) * sizeof(char) );
+ query[0] = 0x00;
+#if 0 /* 29/I-2017 */
+ for (k = 1, part = strtok (template, "?"); (part != NULL);
+ part = strtok (NULL, "?"), k++)
+#else
+ for (k = 1, part = xstrtok (template, "?"); (part != NULL);
+ part = xstrtok (NULL, "?"), k++)
+#endif
+ {
+ if (k > nf) break;
+ strcat( query, part );
+ switch (mpl_tab_get_type(dca, k))
+ { case 'N':
+#if 0 /* 02/XI-2010 by xypron */
+ sprintf(num, "%-18g",mpl_tab_get_num(dca, k));
+#else
+ sprintf(num, "%.*g", DBL_DIG, mpl_tab_get_num(dca, k));
+#endif
+ strcat( query, num );
+ break;
+ case 'S':
+ strcat( query, "'");
+ db_escape_string( query + strlen(query),
+ mpl_tab_get_str(dca, k) );
+ strcat( query, "'");
+ break;
+ default:
+ xassert(dca != dca);
+ }
+ }
+ if (part != NULL)
+ strcat(query, part);
+ if (dl_mysql_query(sql->con, query))
+ {
+ xprintf("db_mysql_write: Query\n\"%s\"\nfailed.\n", query);
+ xprintf("%s\n",dl_mysql_error(sql->con));
+ xfree(query);
+ xfree(template);
+ return 1;
+ }
+
+ xfree(query);
+ xfree(template);
+ return 0;
+ }
+
+int db_mysql_close(TABDCA *dca, void *link)
+{
+ struct db_mysql *sql;
+
+ sql = (struct db_mysql *) link;
+ xassert(sql != NULL);
+ dl_mysql_close(sql->con);
+ if ( sql->mode == 'W' )
+ xfree(sql->query);
+ xfree(sql);
+ dca->link = NULL;
+ return 0;
+}
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/mpl/mplsql.h b/test/monniaux/glpk-4.65/src/mpl/mplsql.h
new file mode 100644
index 00000000..11d438bb
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/mpl/mplsql.h
@@ -0,0 +1,63 @@
+/* mplsql.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Author: Heinrich Schuchardt <heinrich.schuchardt@gmx.de>.
+*
+* Copyright (C) 2003-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef MPLSQL_H
+#define MPLSQL_H
+
+#define db_iodbc_open _glp_db_iodbc_open
+void *db_iodbc_open(TABDCA *dca, int mode);
+/* open iODBC database connection */
+
+#define db_iodbc_read _glp_db_iodbc_read
+int db_iodbc_read(TABDCA *dca, void *link);
+/* read data from iODBC */
+
+#define db_iodbc_write _glp_db_iodbc_write
+int db_iodbc_write(TABDCA *dca, void *link);
+/* write data to iODBC */
+
+#define db_iodbc_close _glp_db_iodbc_close
+int db_iodbc_close(TABDCA *dca, void *link);
+/* close iODBC database connection */
+
+#define db_mysql_open _glp_db_mysql_open
+void *db_mysql_open(TABDCA *dca, int mode);
+/* open MySQL database connection */
+
+#define db_mysql_read _glp_db_mysql_read
+int db_mysql_read(TABDCA *dca, void *link);
+/* read data from MySQL */
+
+#define db_mysql_write _glp_db_mysql_write
+int db_mysql_write(TABDCA *dca, void *link);
+/* write data to MySQL */
+
+#define db_mysql_close _glp_db_mysql_close
+int db_mysql_close(TABDCA *dca, void *link);
+/* close MySQL database connection */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp.h b/test/monniaux/glpk-4.65/src/npp/npp.h
new file mode 100644
index 00000000..428cb23c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp.h
@@ -0,0 +1,645 @@
+/* npp.h (LP/MIP preprocessor) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef NPP_H
+#define NPP_H
+
+#include "prob.h"
+
+#if 0 /* 20/XI-2017 */
+typedef struct NPP NPP;
+#else
+typedef struct glp_prep NPP;
+#endif
+typedef struct NPPROW NPPROW;
+typedef struct NPPCOL NPPCOL;
+typedef struct NPPAIJ NPPAIJ;
+typedef struct NPPTSE NPPTSE;
+typedef struct NPPLFE NPPLFE;
+
+#if 0 /* 20/XI-2017 */
+struct NPP
+#else
+struct glp_prep
+#endif
+{ /* LP/MIP preprocessor workspace */
+ /*--------------------------------------------------------------*/
+ /* original problem segment */
+ int orig_dir;
+ /* optimization direction flag:
+ GLP_MIN - minimization
+ GLP_MAX - maximization */
+ int orig_m;
+ /* number of rows */
+ int orig_n;
+ /* number of columns */
+ int orig_nnz;
+ /* number of non-zero constraint coefficients */
+ /*--------------------------------------------------------------*/
+ /* transformed problem segment (always minimization) */
+ DMP *pool;
+ /* memory pool to store problem components */
+ char *name;
+ /* problem name (1 to 255 chars); NULL means no name is assigned
+ to the problem */
+ char *obj;
+ /* objective function name (1 to 255 chars); NULL means no name
+ is assigned to the objective function */
+ double c0;
+ /* constant term of the objective function */
+ int nrows;
+ /* number of rows introduced into the problem; this count
+ increases by one every time a new row is added and never
+ decreases; thus, actual number of rows may be less than nrows
+ due to row deletions */
+ int ncols;
+ /* number of columns introduced into the problem; this count
+ increases by one every time a new column is added and never
+ decreases; thus, actual number of column may be less than
+ ncols due to column deletions */
+ NPPROW *r_head;
+ /* pointer to the beginning of the row list */
+ NPPROW *r_tail;
+ /* pointer to the end of the row list */
+ NPPCOL *c_head;
+ /* pointer to the beginning of the column list */
+ NPPCOL *c_tail;
+ /* pointer to the end of the column list */
+ /*--------------------------------------------------------------*/
+ /* transformation history */
+ DMP *stack;
+ /* memory pool to store transformation entries */
+ NPPTSE *top;
+ /* pointer to most recent transformation entry */
+#if 0 /* 16/XII-2009 */
+ int count[1+25];
+ /* transformation statistics */
+#endif
+ /*--------------------------------------------------------------*/
+ /* resultant (preprocessed) problem segment */
+ int m;
+ /* number of rows */
+ int n;
+ /* number of columns */
+ int nnz;
+ /* number of non-zero constraint coefficients */
+ int *row_ref; /* int row_ref[1+m]; */
+ /* row_ref[i], 1 <= i <= m, is the reference number assigned to
+ a row, which is i-th row of the resultant problem */
+ int *col_ref; /* int col_ref[1+n]; */
+ /* col_ref[j], 1 <= j <= n, is the reference number assigned to
+ a column, which is j-th column of the resultant problem */
+ /*--------------------------------------------------------------*/
+ /* recovered solution segment */
+ int sol;
+ /* solution indicator:
+ GLP_SOL - basic solution
+ GLP_IPT - interior-point solution
+ GLP_MIP - mixed integer solution */
+ int scaling;
+ /* scaling option:
+ GLP_OFF - scaling is disabled
+ GLP_ON - scaling is enabled */
+ int p_stat;
+ /* status of primal basic solution:
+ GLP_UNDEF - primal solution is undefined
+ GLP_FEAS - primal solution is feasible
+ GLP_INFEAS - primal solution is infeasible
+ GLP_NOFEAS - no primal feasible solution exists */
+ int d_stat;
+ /* status of dual basic solution:
+ GLP_UNDEF - dual solution is undefined
+ GLP_FEAS - dual solution is feasible
+ GLP_INFEAS - dual solution is infeasible
+ GLP_NOFEAS - no dual feasible solution exists */
+ int t_stat;
+ /* status of interior-point solution:
+ GLP_UNDEF - interior solution is undefined
+ GLP_OPT - interior solution is optimal */
+ int i_stat;
+ /* status of mixed integer solution:
+ GLP_UNDEF - integer solution is undefined
+ GLP_OPT - integer solution is optimal
+ GLP_FEAS - integer solution is feasible
+ GLP_NOFEAS - no integer solution exists */
+ char *r_stat; /* char r_stat[1+nrows]; */
+ /* r_stat[i], 1 <= i <= nrows, is status of i-th row:
+ GLP_BS - inactive constraint
+ GLP_NL - active constraint on lower bound
+ GLP_NU - active constraint on upper bound
+ GLP_NF - active free row
+ GLP_NS - active equality constraint */
+ char *c_stat; /* char c_stat[1+nrows]; */
+ /* c_stat[j], 1 <= j <= nrows, is status of j-th column:
+ GLP_BS - basic variable
+ GLP_NL - non-basic variable on lower bound
+ GLP_NU - non-basic variable on upper bound
+ GLP_NF - non-basic free variable
+ GLP_NS - non-basic fixed variable */
+ double *r_pi; /* double r_pi[1+nrows]; */
+ /* r_pi[i], 1 <= i <= nrows, is Lagrange multiplier (dual value)
+ for i-th row (constraint) */
+ double *c_value; /* double c_value[1+ncols]; */
+ /* c_value[j], 1 <= j <= ncols, is primal value of j-th column
+ (structural variable) */
+};
+
+struct NPPROW
+{ /* row (constraint) */
+ int i;
+ /* reference number assigned to the row, 1 <= i <= nrows */
+ char *name;
+ /* row name (1 to 255 chars); NULL means no name is assigned to
+ the row */
+ double lb;
+ /* lower bound; -DBL_MAX means the row has no lower bound */
+ double ub;
+ /* upper bound; +DBL_MAX means the row has no upper bound */
+ NPPAIJ *ptr;
+ /* pointer to the linked list of constraint coefficients */
+ int temp;
+ /* working field used by preprocessor routines */
+ NPPROW *prev;
+ /* pointer to previous row in the row list */
+ NPPROW *next;
+ /* pointer to next row in the row list */
+};
+
+struct NPPCOL
+{ /* column (variable) */
+ int j;
+ /* reference number assigned to the column, 1 <= j <= ncols */
+ char *name;
+ /* column name (1 to 255 chars); NULL means no name is assigned
+ to the column */
+ char is_int;
+ /* 0 means continuous variable; 1 means integer variable */
+ double lb;
+ /* lower bound; -DBL_MAX means the column has no lower bound */
+ double ub;
+ /* upper bound; +DBL_MAX means the column has no upper bound */
+ double coef;
+ /* objective coefficient */
+ NPPAIJ *ptr;
+ /* pointer to the linked list of constraint coefficients */
+ int temp;
+ /* working field used by preprocessor routines */
+#if 1 /* 28/XII-2009 */
+ union
+ { double ll;
+ /* implied column lower bound */
+ int pos;
+ /* vertex ordinal number corresponding to this binary column
+ in the conflict graph (0, if the vertex does not exist) */
+ } ll;
+ union
+ { double uu;
+ /* implied column upper bound */
+ int neg;
+ /* vertex ordinal number corresponding to complement of this
+ binary column in the conflict graph (0, if the vertex does
+ not exist) */
+ } uu;
+#endif
+ NPPCOL *prev;
+ /* pointer to previous column in the column list */
+ NPPCOL *next;
+ /* pointer to next column in the column list */
+};
+
+struct NPPAIJ
+{ /* constraint coefficient */
+ NPPROW *row;
+ /* pointer to corresponding row */
+ NPPCOL *col;
+ /* pointer to corresponding column */
+ double val;
+ /* (non-zero) coefficient value */
+ NPPAIJ *r_prev;
+ /* pointer to previous coefficient in the same row */
+ NPPAIJ *r_next;
+ /* pointer to next coefficient in the same row */
+ NPPAIJ *c_prev;
+ /* pointer to previous coefficient in the same column */
+ NPPAIJ *c_next;
+ /* pointer to next coefficient in the same column */
+};
+
+struct NPPTSE
+{ /* transformation stack entry */
+ int (*func)(NPP *npp, void *info);
+ /* pointer to routine performing back transformation */
+ void *info;
+ /* pointer to specific info (depends on the transformation) */
+ NPPTSE *link;
+ /* pointer to another entry created *before* this entry */
+};
+
+struct NPPLFE
+{ /* linear form element */
+ int ref;
+ /* row/column reference number */
+ double val;
+ /* (non-zero) coefficient value */
+ NPPLFE *next;
+ /* pointer to another element */
+};
+
+#define npp_create_wksp _glp_npp_create_wksp
+NPP *npp_create_wksp(void);
+/* create LP/MIP preprocessor workspace */
+
+#define npp_insert_row _glp_npp_insert_row
+void npp_insert_row(NPP *npp, NPPROW *row, int where);
+/* insert row to the row list */
+
+#define npp_remove_row _glp_npp_remove_row
+void npp_remove_row(NPP *npp, NPPROW *row);
+/* remove row from the row list */
+
+#define npp_activate_row _glp_npp_activate_row
+void npp_activate_row(NPP *npp, NPPROW *row);
+/* make row active */
+
+#define npp_deactivate_row _glp_npp_deactivate_row
+void npp_deactivate_row(NPP *npp, NPPROW *row);
+/* make row inactive */
+
+#define npp_insert_col _glp_npp_insert_col
+void npp_insert_col(NPP *npp, NPPCOL *col, int where);
+/* insert column to the column list */
+
+#define npp_remove_col _glp_npp_remove_col
+void npp_remove_col(NPP *npp, NPPCOL *col);
+/* remove column from the column list */
+
+#define npp_activate_col _glp_npp_activate_col
+void npp_activate_col(NPP *npp, NPPCOL *col);
+/* make column active */
+
+#define npp_deactivate_col _glp_npp_deactivate_col
+void npp_deactivate_col(NPP *npp, NPPCOL *col);
+/* make column inactive */
+
+#define npp_add_row _glp_npp_add_row
+NPPROW *npp_add_row(NPP *npp);
+/* add new row to the current problem */
+
+#define npp_add_col _glp_npp_add_col
+NPPCOL *npp_add_col(NPP *npp);
+/* add new column to the current problem */
+
+#define npp_add_aij _glp_npp_add_aij
+NPPAIJ *npp_add_aij(NPP *npp, NPPROW *row, NPPCOL *col, double val);
+/* add new element to the constraint matrix */
+
+#define npp_row_nnz _glp_npp_row_nnz
+int npp_row_nnz(NPP *npp, NPPROW *row);
+/* count number of non-zero coefficients in row */
+
+#define npp_col_nnz _glp_npp_col_nnz
+int npp_col_nnz(NPP *npp, NPPCOL *col);
+/* count number of non-zero coefficients in column */
+
+#define npp_push_tse _glp_npp_push_tse
+void *npp_push_tse(NPP *npp, int (*func)(NPP *npp, void *info),
+ int size);
+/* push new entry to the transformation stack */
+
+#define npp_erase_row _glp_npp_erase_row
+void npp_erase_row(NPP *npp, NPPROW *row);
+/* erase row content to make it empty */
+
+#define npp_del_row _glp_npp_del_row
+void npp_del_row(NPP *npp, NPPROW *row);
+/* remove row from the current problem */
+
+#define npp_del_col _glp_npp_del_col
+void npp_del_col(NPP *npp, NPPCOL *col);
+/* remove column from the current problem */
+
+#define npp_del_aij _glp_npp_del_aij
+void npp_del_aij(NPP *npp, NPPAIJ *aij);
+/* remove element from the constraint matrix */
+
+#define npp_load_prob _glp_npp_load_prob
+void npp_load_prob(NPP *npp, glp_prob *orig, int names, int sol,
+ int scaling);
+/* load original problem into the preprocessor workspace */
+
+#define npp_build_prob _glp_npp_build_prob
+void npp_build_prob(NPP *npp, glp_prob *prob);
+/* build resultant (preprocessed) problem */
+
+#define npp_postprocess _glp_npp_postprocess
+void npp_postprocess(NPP *npp, glp_prob *prob);
+/* postprocess solution from the resultant problem */
+
+#define npp_unload_sol _glp_npp_unload_sol
+void npp_unload_sol(NPP *npp, glp_prob *orig);
+/* store solution to the original problem */
+
+#define npp_delete_wksp _glp_npp_delete_wksp
+void npp_delete_wksp(NPP *npp);
+/* delete LP/MIP preprocessor workspace */
+
+#define npp_error()
+
+#define npp_free_row _glp_npp_free_row
+void npp_free_row(NPP *npp, NPPROW *p);
+/* process free (unbounded) row */
+
+#define npp_geq_row _glp_npp_geq_row
+void npp_geq_row(NPP *npp, NPPROW *p);
+/* process row of 'not less than' type */
+
+#define npp_leq_row _glp_npp_leq_row
+void npp_leq_row(NPP *npp, NPPROW *p);
+/* process row of 'not greater than' type */
+
+#define npp_free_col _glp_npp_free_col
+void npp_free_col(NPP *npp, NPPCOL *q);
+/* process free (unbounded) column */
+
+#define npp_lbnd_col _glp_npp_lbnd_col
+void npp_lbnd_col(NPP *npp, NPPCOL *q);
+/* process column with (non-zero) lower bound */
+
+#define npp_ubnd_col _glp_npp_ubnd_col
+void npp_ubnd_col(NPP *npp, NPPCOL *q);
+/* process column with upper bound */
+
+#define npp_dbnd_col _glp_npp_dbnd_col
+void npp_dbnd_col(NPP *npp, NPPCOL *q);
+/* process non-negative column with upper bound */
+
+#define npp_fixed_col _glp_npp_fixed_col
+void npp_fixed_col(NPP *npp, NPPCOL *q);
+/* process fixed column */
+
+#define npp_make_equality _glp_npp_make_equality
+int npp_make_equality(NPP *npp, NPPROW *p);
+/* process row with almost identical bounds */
+
+#define npp_make_fixed _glp_npp_make_fixed
+int npp_make_fixed(NPP *npp, NPPCOL *q);
+/* process column with almost identical bounds */
+
+#define npp_empty_row _glp_npp_empty_row
+int npp_empty_row(NPP *npp, NPPROW *p);
+/* process empty row */
+
+#define npp_empty_col _glp_npp_empty_col
+int npp_empty_col(NPP *npp, NPPCOL *q);
+/* process empty column */
+
+#define npp_implied_value _glp_npp_implied_value
+int npp_implied_value(NPP *npp, NPPCOL *q, double s);
+/* process implied column value */
+
+#define npp_eq_singlet _glp_npp_eq_singlet
+int npp_eq_singlet(NPP *npp, NPPROW *p);
+/* process row singleton (equality constraint) */
+
+#define npp_implied_lower _glp_npp_implied_lower
+int npp_implied_lower(NPP *npp, NPPCOL *q, double l);
+/* process implied column lower bound */
+
+#define npp_implied_upper _glp_npp_implied_upper
+int npp_implied_upper(NPP *npp, NPPCOL *q, double u);
+/* process implied upper bound of column */
+
+#define npp_ineq_singlet _glp_npp_ineq_singlet
+int npp_ineq_singlet(NPP *npp, NPPROW *p);
+/* process row singleton (inequality constraint) */
+
+#define npp_implied_slack _glp_npp_implied_slack
+void npp_implied_slack(NPP *npp, NPPCOL *q);
+/* process column singleton (implied slack variable) */
+
+#define npp_implied_free _glp_npp_implied_free
+int npp_implied_free(NPP *npp, NPPCOL *q);
+/* process column singleton (implied free variable) */
+
+#define npp_eq_doublet _glp_npp_eq_doublet
+NPPCOL *npp_eq_doublet(NPP *npp, NPPROW *p);
+/* process row doubleton (equality constraint) */
+
+#define npp_forcing_row _glp_npp_forcing_row
+int npp_forcing_row(NPP *npp, NPPROW *p, int at);
+/* process forcing row */
+
+#define npp_analyze_row _glp_npp_analyze_row
+int npp_analyze_row(NPP *npp, NPPROW *p);
+/* perform general row analysis */
+
+#define npp_inactive_bound _glp_npp_inactive_bound
+void npp_inactive_bound(NPP *npp, NPPROW *p, int which);
+/* remove row lower/upper inactive bound */
+
+#define npp_implied_bounds _glp_npp_implied_bounds
+void npp_implied_bounds(NPP *npp, NPPROW *p);
+/* determine implied column bounds */
+
+#define npp_binarize_prob _glp_npp_binarize_prob
+int npp_binarize_prob(NPP *npp);
+/* binarize MIP problem */
+
+#define npp_is_packing _glp_npp_is_packing
+int npp_is_packing(NPP *npp, NPPROW *row);
+/* test if constraint is packing inequality */
+
+#define npp_hidden_packing _glp_npp_hidden_packing
+int npp_hidden_packing(NPP *npp, NPPROW *row);
+/* identify hidden packing inequality */
+
+#define npp_implied_packing _glp_npp_implied_packing
+int npp_implied_packing(NPP *npp, NPPROW *row, int which,
+ NPPCOL *var[], char set[]);
+/* identify implied packing inequality */
+
+#define npp_is_covering _glp_npp_is_covering
+int npp_is_covering(NPP *npp, NPPROW *row);
+/* test if constraint is covering inequality */
+
+#define npp_hidden_covering _glp_npp_hidden_covering
+int npp_hidden_covering(NPP *npp, NPPROW *row);
+/* identify hidden covering inequality */
+
+#define npp_is_partitioning _glp_npp_is_partitioning
+int npp_is_partitioning(NPP *npp, NPPROW *row);
+/* test if constraint is partitioning equality */
+
+#define npp_reduce_ineq_coef _glp_npp_reduce_ineq_coef
+int npp_reduce_ineq_coef(NPP *npp, NPPROW *row);
+/* reduce inequality constraint coefficients */
+
+#define npp_clean_prob _glp_npp_clean_prob
+void npp_clean_prob(NPP *npp);
+/* perform initial LP/MIP processing */
+
+#define npp_process_row _glp_npp_process_row
+int npp_process_row(NPP *npp, NPPROW *row, int hard);
+/* perform basic row processing */
+
+#define npp_improve_bounds _glp_npp_improve_bounds
+int npp_improve_bounds(NPP *npp, NPPROW *row, int flag);
+/* improve current column bounds */
+
+#define npp_process_col _glp_npp_process_col
+int npp_process_col(NPP *npp, NPPCOL *col);
+/* perform basic column processing */
+
+#define npp_process_prob _glp_npp_process_prob
+int npp_process_prob(NPP *npp, int hard);
+/* perform basic LP/MIP processing */
+
+#define npp_simplex _glp_npp_simplex
+int npp_simplex(NPP *npp, const glp_smcp *parm);
+/* process LP prior to applying primal/dual simplex method */
+
+#define npp_integer _glp_npp_integer
+int npp_integer(NPP *npp, const glp_iocp *parm);
+/* process MIP prior to applying branch-and-bound method */
+
+/**********************************************************************/
+
+#define npp_sat_free_row _glp_npp_sat_free_row
+void npp_sat_free_row(NPP *npp, NPPROW *p);
+/* process free (unbounded) row */
+
+#define npp_sat_fixed_col _glp_npp_sat_fixed_col
+int npp_sat_fixed_col(NPP *npp, NPPCOL *q);
+/* process fixed column */
+
+#define npp_sat_is_bin_comb _glp_npp_sat_is_bin_comb
+int npp_sat_is_bin_comb(NPP *npp, NPPROW *row);
+/* test if row is binary combination */
+
+#define npp_sat_num_pos_coef _glp_npp_sat_num_pos_coef
+int npp_sat_num_pos_coef(NPP *npp, NPPROW *row);
+/* determine number of positive coefficients */
+
+#define npp_sat_num_neg_coef _glp_npp_sat_num_neg_coef
+int npp_sat_num_neg_coef(NPP *npp, NPPROW *row);
+/* determine number of negative coefficients */
+
+#define npp_sat_is_cover_ineq _glp_npp_sat_is_cover_ineq
+int npp_sat_is_cover_ineq(NPP *npp, NPPROW *row);
+/* test if row is covering inequality */
+
+#define npp_sat_is_pack_ineq _glp_npp_sat_is_pack_ineq
+int npp_sat_is_pack_ineq(NPP *npp, NPPROW *row);
+/* test if row is packing inequality */
+
+#define npp_sat_is_partn_eq _glp_npp_sat_is_partn_eq
+int npp_sat_is_partn_eq(NPP *npp, NPPROW *row);
+/* test if row is partitioning equality */
+
+#define npp_sat_reverse_row _glp_npp_sat_reverse_row
+int npp_sat_reverse_row(NPP *npp, NPPROW *row);
+/* multiply both sides of row by -1 */
+
+#define npp_sat_split_pack _glp_npp_sat_split_pack
+NPPROW *npp_sat_split_pack(NPP *npp, NPPROW *row, int nnn);
+/* split packing inequality */
+
+#define npp_sat_encode_pack _glp_npp_sat_encode_pack
+void npp_sat_encode_pack(NPP *npp, NPPROW *row);
+/* encode packing inequality */
+
+typedef struct NPPLIT NPPLIT;
+typedef struct NPPLSE NPPLSE;
+typedef struct NPPSED NPPSED;
+
+struct NPPLIT
+{ /* literal (binary variable or its negation) */
+ NPPCOL *col;
+ /* pointer to binary variable; NULL means constant false */
+ int neg;
+ /* negation flag:
+ 0 - literal is variable (or constant false)
+ 1 - literal is negation of variable (or constant true) */
+};
+
+struct NPPLSE
+{ /* literal set element */
+ NPPLIT lit;
+ /* literal */
+ NPPLSE *next;
+ /* pointer to another element */
+};
+
+struct NPPSED
+{ /* summation encoding descriptor */
+ /* this struct describes the equality
+ x + y + z = s + 2 * c,
+ which was encoded as CNF and included into the transformed
+ problem; here x and y are literals, z is either a literal or
+ constant zero, s and c are binary variables modeling, resp.,
+ the low and high (carry) sum bits */
+ NPPLIT x, y, z;
+ /* literals; if z.col = NULL, z is constant zero */
+ NPPCOL *s, *c;
+ /* binary variables modeling the sum bits */
+};
+
+#define npp_sat_encode_sum2 _glp_npp_sat_encode_sum2
+void npp_sat_encode_sum2(NPP *npp, NPPLSE *set, NPPSED *sed);
+/* encode 2-bit summation */
+
+#define npp_sat_encode_sum3 _glp_npp_sat_encode_sum3
+void npp_sat_encode_sum3(NPP *npp, NPPLSE *set, NPPSED *sed);
+/* encode 3-bit summation */
+
+#define npp_sat_encode_sum_ax _glp_npp_sat_encode_sum_ax
+int npp_sat_encode_sum_ax(NPP *npp, NPPROW *row, NPPLIT y[]);
+/* encode linear combination of 0-1 variables */
+
+#define npp_sat_normalize_clause _glp_npp_sat_normalize_clause
+int npp_sat_normalize_clause(NPP *npp, int size, NPPLIT lit[]);
+/* normalize clause */
+
+#define npp_sat_encode_clause _glp_npp_sat_encode_clause
+NPPROW *npp_sat_encode_clause(NPP *npp, int size, NPPLIT lit[]);
+/* translate clause to cover inequality */
+
+#define npp_sat_encode_geq _glp_npp_sat_encode_geq
+int npp_sat_encode_geq(NPP *npp, int n, NPPLIT y[], int rhs);
+/* encode "not less than" constraint */
+
+#define npp_sat_encode_leq _glp_npp_sat_encode_leq
+int npp_sat_encode_leq(NPP *npp, int n, NPPLIT y[], int rhs);
+/* encode "not greater than" constraint */
+
+#define npp_sat_encode_row _glp_npp_sat_encode_row
+int npp_sat_encode_row(NPP *npp, NPPROW *row);
+/* encode constraint (row) of general type */
+
+#define npp_sat_encode_prob _glp_npp_sat_encode_prob
+int npp_sat_encode_prob(NPP *npp);
+/* encode 0-1 feasibility problem */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp1.c b/test/monniaux/glpk-4.65/src/npp/npp1.c
new file mode 100644
index 00000000..51758bad
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp1.c
@@ -0,0 +1,937 @@
+/* npp1.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+NPP *npp_create_wksp(void)
+{ /* create LP/MIP preprocessor workspace */
+ NPP *npp;
+ npp = xmalloc(sizeof(NPP));
+ npp->orig_dir = 0;
+ npp->orig_m = npp->orig_n = npp->orig_nnz = 0;
+ npp->pool = dmp_create_pool();
+ npp->name = npp->obj = NULL;
+ npp->c0 = 0.0;
+ npp->nrows = npp->ncols = 0;
+ npp->r_head = npp->r_tail = NULL;
+ npp->c_head = npp->c_tail = NULL;
+ npp->stack = dmp_create_pool();
+ npp->top = NULL;
+#if 0 /* 16/XII-2009 */
+ memset(&npp->count, 0, sizeof(npp->count));
+#endif
+ npp->m = npp->n = npp->nnz = 0;
+ npp->row_ref = npp->col_ref = NULL;
+ npp->sol = npp->scaling = 0;
+ npp->p_stat = npp->d_stat = npp->t_stat = npp->i_stat = 0;
+ npp->r_stat = NULL;
+ /*npp->r_prim =*/ npp->r_pi = NULL;
+ npp->c_stat = NULL;
+ npp->c_value = /*npp->c_dual =*/ NULL;
+ return npp;
+}
+
+void npp_insert_row(NPP *npp, NPPROW *row, int where)
+{ /* insert row to the row list */
+ if (where == 0)
+ { /* insert row to the beginning of the row list */
+ row->prev = NULL;
+ row->next = npp->r_head;
+ if (row->next == NULL)
+ npp->r_tail = row;
+ else
+ row->next->prev = row;
+ npp->r_head = row;
+ }
+ else
+ { /* insert row to the end of the row list */
+ row->prev = npp->r_tail;
+ row->next = NULL;
+ if (row->prev == NULL)
+ npp->r_head = row;
+ else
+ row->prev->next = row;
+ npp->r_tail = row;
+ }
+ return;
+}
+
+void npp_remove_row(NPP *npp, NPPROW *row)
+{ /* remove row from the row list */
+ if (row->prev == NULL)
+ npp->r_head = row->next;
+ else
+ row->prev->next = row->next;
+ if (row->next == NULL)
+ npp->r_tail = row->prev;
+ else
+ row->next->prev = row->prev;
+ return;
+}
+
+void npp_activate_row(NPP *npp, NPPROW *row)
+{ /* make row active */
+ if (!row->temp)
+ { row->temp = 1;
+ /* move the row to the beginning of the row list */
+ npp_remove_row(npp, row);
+ npp_insert_row(npp, row, 0);
+ }
+ return;
+}
+
+void npp_deactivate_row(NPP *npp, NPPROW *row)
+{ /* make row inactive */
+ if (row->temp)
+ { row->temp = 0;
+ /* move the row to the end of the row list */
+ npp_remove_row(npp, row);
+ npp_insert_row(npp, row, 1);
+ }
+ return;
+}
+
+void npp_insert_col(NPP *npp, NPPCOL *col, int where)
+{ /* insert column to the column list */
+ if (where == 0)
+ { /* insert column to the beginning of the column list */
+ col->prev = NULL;
+ col->next = npp->c_head;
+ if (col->next == NULL)
+ npp->c_tail = col;
+ else
+ col->next->prev = col;
+ npp->c_head = col;
+ }
+ else
+ { /* insert column to the end of the column list */
+ col->prev = npp->c_tail;
+ col->next = NULL;
+ if (col->prev == NULL)
+ npp->c_head = col;
+ else
+ col->prev->next = col;
+ npp->c_tail = col;
+ }
+ return;
+}
+
+void npp_remove_col(NPP *npp, NPPCOL *col)
+{ /* remove column from the column list */
+ if (col->prev == NULL)
+ npp->c_head = col->next;
+ else
+ col->prev->next = col->next;
+ if (col->next == NULL)
+ npp->c_tail = col->prev;
+ else
+ col->next->prev = col->prev;
+ return;
+}
+
+void npp_activate_col(NPP *npp, NPPCOL *col)
+{ /* make column active */
+ if (!col->temp)
+ { col->temp = 1;
+ /* move the column to the beginning of the column list */
+ npp_remove_col(npp, col);
+ npp_insert_col(npp, col, 0);
+ }
+ return;
+}
+
+void npp_deactivate_col(NPP *npp, NPPCOL *col)
+{ /* make column inactive */
+ if (col->temp)
+ { col->temp = 0;
+ /* move the column to the end of the column list */
+ npp_remove_col(npp, col);
+ npp_insert_col(npp, col, 1);
+ }
+ return;
+}
+
+NPPROW *npp_add_row(NPP *npp)
+{ /* add new row to the current problem */
+ NPPROW *row;
+ row = dmp_get_atom(npp->pool, sizeof(NPPROW));
+ row->i = ++(npp->nrows);
+ row->name = NULL;
+ row->lb = -DBL_MAX, row->ub = +DBL_MAX;
+ row->ptr = NULL;
+ row->temp = 0;
+ npp_insert_row(npp, row, 1);
+ return row;
+}
+
+NPPCOL *npp_add_col(NPP *npp)
+{ /* add new column to the current problem */
+ NPPCOL *col;
+ col = dmp_get_atom(npp->pool, sizeof(NPPCOL));
+ col->j = ++(npp->ncols);
+ col->name = NULL;
+#if 0
+ col->kind = GLP_CV;
+#else
+ col->is_int = 0;
+#endif
+ col->lb = col->ub = col->coef = 0.0;
+ col->ptr = NULL;
+ col->temp = 0;
+ npp_insert_col(npp, col, 1);
+ return col;
+}
+
+NPPAIJ *npp_add_aij(NPP *npp, NPPROW *row, NPPCOL *col, double val)
+{ /* add new element to the constraint matrix */
+ NPPAIJ *aij;
+ aij = dmp_get_atom(npp->pool, sizeof(NPPAIJ));
+ aij->row = row;
+ aij->col = col;
+ aij->val = val;
+ aij->r_prev = NULL;
+ aij->r_next = row->ptr;
+ aij->c_prev = NULL;
+ aij->c_next = col->ptr;
+ if (aij->r_next != NULL)
+ aij->r_next->r_prev = aij;
+ if (aij->c_next != NULL)
+ aij->c_next->c_prev = aij;
+ row->ptr = col->ptr = aij;
+ return aij;
+}
+
+int npp_row_nnz(NPP *npp, NPPROW *row)
+{ /* count number of non-zero coefficients in row */
+ NPPAIJ *aij;
+ int nnz;
+ xassert(npp == npp);
+ nnz = 0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ nnz++;
+ return nnz;
+}
+
+int npp_col_nnz(NPP *npp, NPPCOL *col)
+{ /* count number of non-zero coefficients in column */
+ NPPAIJ *aij;
+ int nnz;
+ xassert(npp == npp);
+ nnz = 0;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ nnz++;
+ return nnz;
+}
+
+void *npp_push_tse(NPP *npp, int (*func)(NPP *npp, void *info),
+ int size)
+{ /* push new entry to the transformation stack */
+ NPPTSE *tse;
+ tse = dmp_get_atom(npp->stack, sizeof(NPPTSE));
+ tse->func = func;
+ tse->info = dmp_get_atom(npp->stack, size);
+ tse->link = npp->top;
+ npp->top = tse;
+ return tse->info;
+}
+
+#if 1 /* 23/XII-2009 */
+void npp_erase_row(NPP *npp, NPPROW *row)
+{ /* erase row content to make it empty */
+ NPPAIJ *aij;
+ while (row->ptr != NULL)
+ { aij = row->ptr;
+ row->ptr = aij->r_next;
+ if (aij->c_prev == NULL)
+ aij->col->ptr = aij->c_next;
+ else
+ aij->c_prev->c_next = aij->c_next;
+ if (aij->c_next == NULL)
+ ;
+ else
+ aij->c_next->c_prev = aij->c_prev;
+ dmp_free_atom(npp->pool, aij, sizeof(NPPAIJ));
+ }
+ return;
+}
+#endif
+
+void npp_del_row(NPP *npp, NPPROW *row)
+{ /* remove row from the current problem */
+#if 0 /* 23/XII-2009 */
+ NPPAIJ *aij;
+#endif
+ if (row->name != NULL)
+ dmp_free_atom(npp->pool, row->name, strlen(row->name)+1);
+#if 0 /* 23/XII-2009 */
+ while (row->ptr != NULL)
+ { aij = row->ptr;
+ row->ptr = aij->r_next;
+ if (aij->c_prev == NULL)
+ aij->col->ptr = aij->c_next;
+ else
+ aij->c_prev->c_next = aij->c_next;
+ if (aij->c_next == NULL)
+ ;
+ else
+ aij->c_next->c_prev = aij->c_prev;
+ dmp_free_atom(npp->pool, aij, sizeof(NPPAIJ));
+ }
+#else
+ npp_erase_row(npp, row);
+#endif
+ npp_remove_row(npp, row);
+ dmp_free_atom(npp->pool, row, sizeof(NPPROW));
+ return;
+}
+
+void npp_del_col(NPP *npp, NPPCOL *col)
+{ /* remove column from the current problem */
+ NPPAIJ *aij;
+ if (col->name != NULL)
+ dmp_free_atom(npp->pool, col->name, strlen(col->name)+1);
+ while (col->ptr != NULL)
+ { aij = col->ptr;
+ col->ptr = aij->c_next;
+ if (aij->r_prev == NULL)
+ aij->row->ptr = aij->r_next;
+ else
+ aij->r_prev->r_next = aij->r_next;
+ if (aij->r_next == NULL)
+ ;
+ else
+ aij->r_next->r_prev = aij->r_prev;
+ dmp_free_atom(npp->pool, aij, sizeof(NPPAIJ));
+ }
+ npp_remove_col(npp, col);
+ dmp_free_atom(npp->pool, col, sizeof(NPPCOL));
+ return;
+}
+
+void npp_del_aij(NPP *npp, NPPAIJ *aij)
+{ /* remove element from the constraint matrix */
+ if (aij->r_prev == NULL)
+ aij->row->ptr = aij->r_next;
+ else
+ aij->r_prev->r_next = aij->r_next;
+ if (aij->r_next == NULL)
+ ;
+ else
+ aij->r_next->r_prev = aij->r_prev;
+ if (aij->c_prev == NULL)
+ aij->col->ptr = aij->c_next;
+ else
+ aij->c_prev->c_next = aij->c_next;
+ if (aij->c_next == NULL)
+ ;
+ else
+ aij->c_next->c_prev = aij->c_prev;
+ dmp_free_atom(npp->pool, aij, sizeof(NPPAIJ));
+ return;
+}
+
+void npp_load_prob(NPP *npp, glp_prob *orig, int names, int sol,
+ int scaling)
+{ /* load original problem into the preprocessor workspace */
+ int m = orig->m;
+ int n = orig->n;
+ NPPROW **link;
+ int i, j;
+ double dir;
+ xassert(names == GLP_OFF || names == GLP_ON);
+ xassert(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP);
+ xassert(scaling == GLP_OFF || scaling == GLP_ON);
+ if (sol == GLP_MIP) xassert(!scaling);
+ npp->orig_dir = orig->dir;
+ if (npp->orig_dir == GLP_MIN)
+ dir = +1.0;
+ else if (npp->orig_dir == GLP_MAX)
+ dir = -1.0;
+ else
+ xassert(npp != npp);
+ npp->orig_m = m;
+ npp->orig_n = n;
+ npp->orig_nnz = orig->nnz;
+ if (names && orig->name != NULL)
+ { npp->name = dmp_get_atom(npp->pool, strlen(orig->name)+1);
+ strcpy(npp->name, orig->name);
+ }
+ if (names && orig->obj != NULL)
+ { npp->obj = dmp_get_atom(npp->pool, strlen(orig->obj)+1);
+ strcpy(npp->obj, orig->obj);
+ }
+ npp->c0 = dir * orig->c0;
+ /* load rows */
+ link = xcalloc(1+m, sizeof(NPPROW *));
+ for (i = 1; i <= m; i++)
+ { GLPROW *rrr = orig->row[i];
+ NPPROW *row;
+ link[i] = row = npp_add_row(npp);
+ xassert(row->i == i);
+ if (names && rrr->name != NULL)
+ { row->name = dmp_get_atom(npp->pool, strlen(rrr->name)+1);
+ strcpy(row->name, rrr->name);
+ }
+ if (!scaling)
+ { if (rrr->type == GLP_FR)
+ row->lb = -DBL_MAX, row->ub = +DBL_MAX;
+ else if (rrr->type == GLP_LO)
+ row->lb = rrr->lb, row->ub = +DBL_MAX;
+ else if (rrr->type == GLP_UP)
+ row->lb = -DBL_MAX, row->ub = rrr->ub;
+ else if (rrr->type == GLP_DB)
+ row->lb = rrr->lb, row->ub = rrr->ub;
+ else if (rrr->type == GLP_FX)
+ row->lb = row->ub = rrr->lb;
+ else
+ xassert(rrr != rrr);
+ }
+ else
+ { double rii = rrr->rii;
+ if (rrr->type == GLP_FR)
+ row->lb = -DBL_MAX, row->ub = +DBL_MAX;
+ else if (rrr->type == GLP_LO)
+ row->lb = rrr->lb * rii, row->ub = +DBL_MAX;
+ else if (rrr->type == GLP_UP)
+ row->lb = -DBL_MAX, row->ub = rrr->ub * rii;
+ else if (rrr->type == GLP_DB)
+ row->lb = rrr->lb * rii, row->ub = rrr->ub * rii;
+ else if (rrr->type == GLP_FX)
+ row->lb = row->ub = rrr->lb * rii;
+ else
+ xassert(rrr != rrr);
+ }
+ }
+ /* load columns and constraint coefficients */
+ for (j = 1; j <= n; j++)
+ { GLPCOL *ccc = orig->col[j];
+ GLPAIJ *aaa;
+ NPPCOL *col;
+ col = npp_add_col(npp);
+ xassert(col->j == j);
+ if (names && ccc->name != NULL)
+ { col->name = dmp_get_atom(npp->pool, strlen(ccc->name)+1);
+ strcpy(col->name, ccc->name);
+ }
+ if (sol == GLP_MIP)
+#if 0
+ col->kind = ccc->kind;
+#else
+ col->is_int = (char)(ccc->kind == GLP_IV);
+#endif
+ if (!scaling)
+ { if (ccc->type == GLP_FR)
+ col->lb = -DBL_MAX, col->ub = +DBL_MAX;
+ else if (ccc->type == GLP_LO)
+ col->lb = ccc->lb, col->ub = +DBL_MAX;
+ else if (ccc->type == GLP_UP)
+ col->lb = -DBL_MAX, col->ub = ccc->ub;
+ else if (ccc->type == GLP_DB)
+ col->lb = ccc->lb, col->ub = ccc->ub;
+ else if (ccc->type == GLP_FX)
+ col->lb = col->ub = ccc->lb;
+ else
+ xassert(ccc != ccc);
+ col->coef = dir * ccc->coef;
+ for (aaa = ccc->ptr; aaa != NULL; aaa = aaa->c_next)
+ npp_add_aij(npp, link[aaa->row->i], col, aaa->val);
+ }
+ else
+ { double sjj = ccc->sjj;
+ if (ccc->type == GLP_FR)
+ col->lb = -DBL_MAX, col->ub = +DBL_MAX;
+ else if (ccc->type == GLP_LO)
+ col->lb = ccc->lb / sjj, col->ub = +DBL_MAX;
+ else if (ccc->type == GLP_UP)
+ col->lb = -DBL_MAX, col->ub = ccc->ub / sjj;
+ else if (ccc->type == GLP_DB)
+ col->lb = ccc->lb / sjj, col->ub = ccc->ub / sjj;
+ else if (ccc->type == GLP_FX)
+ col->lb = col->ub = ccc->lb / sjj;
+ else
+ xassert(ccc != ccc);
+ col->coef = dir * ccc->coef * sjj;
+ for (aaa = ccc->ptr; aaa != NULL; aaa = aaa->c_next)
+ npp_add_aij(npp, link[aaa->row->i], col,
+ aaa->row->rii * aaa->val * sjj);
+ }
+ }
+ xfree(link);
+ /* keep solution indicator and scaling option */
+ npp->sol = sol;
+ npp->scaling = scaling;
+ return;
+}
+
+void npp_build_prob(NPP *npp, glp_prob *prob)
+{ /* build resultant (preprocessed) problem */
+ NPPROW *row;
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int i, j, type, len, *ind;
+ double dir, *val;
+ glp_erase_prob(prob);
+ glp_set_prob_name(prob, npp->name);
+ glp_set_obj_name(prob, npp->obj);
+ glp_set_obj_dir(prob, npp->orig_dir);
+ if (npp->orig_dir == GLP_MIN)
+ dir = +1.0;
+ else if (npp->orig_dir == GLP_MAX)
+ dir = -1.0;
+ else
+ xassert(npp != npp);
+ glp_set_obj_coef(prob, 0, dir * npp->c0);
+ /* build rows */
+ for (row = npp->r_head; row != NULL; row = row->next)
+ { row->temp = i = glp_add_rows(prob, 1);
+ glp_set_row_name(prob, i, row->name);
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ type = GLP_FR;
+ else if (row->ub == +DBL_MAX)
+ type = GLP_LO;
+ else if (row->lb == -DBL_MAX)
+ type = GLP_UP;
+ else if (row->lb != row->ub)
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_row_bnds(prob, i, type, row->lb, row->ub);
+ }
+ /* build columns and the constraint matrix */
+ ind = xcalloc(1+prob->m, sizeof(int));
+ val = xcalloc(1+prob->m, sizeof(double));
+ for (col = npp->c_head; col != NULL; col = col->next)
+ { j = glp_add_cols(prob, 1);
+ glp_set_col_name(prob, j, col->name);
+#if 0
+ glp_set_col_kind(prob, j, col->kind);
+#else
+ glp_set_col_kind(prob, j, col->is_int ? GLP_IV : GLP_CV);
+#endif
+ if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
+ type = GLP_FR;
+ else if (col->ub == +DBL_MAX)
+ type = GLP_LO;
+ else if (col->lb == -DBL_MAX)
+ type = GLP_UP;
+ else if (col->lb != col->ub)
+ type = GLP_DB;
+ else
+ type = GLP_FX;
+ glp_set_col_bnds(prob, j, type, col->lb, col->ub);
+ glp_set_obj_coef(prob, j, dir * col->coef);
+ len = 0;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ { len++;
+ ind[len] = aij->row->temp;
+ val[len] = aij->val;
+ }
+ glp_set_mat_col(prob, j, len, ind, val);
+ }
+ xfree(ind);
+ xfree(val);
+ /* resultant problem has been built */
+ npp->m = prob->m;
+ npp->n = prob->n;
+ npp->nnz = prob->nnz;
+ npp->row_ref = xcalloc(1+npp->m, sizeof(int));
+ npp->col_ref = xcalloc(1+npp->n, sizeof(int));
+ for (row = npp->r_head, i = 0; row != NULL; row = row->next)
+ npp->row_ref[++i] = row->i;
+ for (col = npp->c_head, j = 0; col != NULL; col = col->next)
+ npp->col_ref[++j] = col->j;
+ /* transformed problem segment is no longer needed */
+ dmp_delete_pool(npp->pool), npp->pool = NULL;
+ npp->name = npp->obj = NULL;
+ npp->c0 = 0.0;
+ npp->r_head = npp->r_tail = NULL;
+ npp->c_head = npp->c_tail = NULL;
+ return;
+}
+
+void npp_postprocess(NPP *npp, glp_prob *prob)
+{ /* postprocess solution from the resultant problem */
+ GLPROW *row;
+ GLPCOL *col;
+ NPPTSE *tse;
+ int i, j, k;
+ double dir;
+ xassert(npp->orig_dir == prob->dir);
+ if (npp->orig_dir == GLP_MIN)
+ dir = +1.0;
+ else if (npp->orig_dir == GLP_MAX)
+ dir = -1.0;
+ else
+ xassert(npp != npp);
+#if 0 /* 11/VII-2013; due to call from ios_main */
+ xassert(npp->m == prob->m);
+#else
+ if (npp->sol != GLP_MIP)
+ xassert(npp->m == prob->m);
+#endif
+ xassert(npp->n == prob->n);
+#if 0 /* 11/VII-2013; due to call from ios_main */
+ xassert(npp->nnz == prob->nnz);
+#else
+ if (npp->sol != GLP_MIP)
+ xassert(npp->nnz == prob->nnz);
+#endif
+ /* copy solution status */
+ if (npp->sol == GLP_SOL)
+ { npp->p_stat = prob->pbs_stat;
+ npp->d_stat = prob->dbs_stat;
+ }
+ else if (npp->sol == GLP_IPT)
+ npp->t_stat = prob->ipt_stat;
+ else if (npp->sol == GLP_MIP)
+ npp->i_stat = prob->mip_stat;
+ else
+ xassert(npp != npp);
+ /* allocate solution arrays */
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat == NULL)
+ npp->r_stat = xcalloc(1+npp->nrows, sizeof(char));
+ for (i = 1; i <= npp->nrows; i++)
+ npp->r_stat[i] = 0;
+ if (npp->c_stat == NULL)
+ npp->c_stat = xcalloc(1+npp->ncols, sizeof(char));
+ for (j = 1; j <= npp->ncols; j++)
+ npp->c_stat[j] = 0;
+ }
+#if 0
+ if (npp->r_prim == NULL)
+ npp->r_prim = xcalloc(1+npp->nrows, sizeof(double));
+ for (i = 1; i <= npp->nrows; i++)
+ npp->r_prim[i] = DBL_MAX;
+#endif
+ if (npp->c_value == NULL)
+ npp->c_value = xcalloc(1+npp->ncols, sizeof(double));
+ for (j = 1; j <= npp->ncols; j++)
+ npp->c_value[j] = DBL_MAX;
+ if (npp->sol != GLP_MIP)
+ { if (npp->r_pi == NULL)
+ npp->r_pi = xcalloc(1+npp->nrows, sizeof(double));
+ for (i = 1; i <= npp->nrows; i++)
+ npp->r_pi[i] = DBL_MAX;
+#if 0
+ if (npp->c_dual == NULL)
+ npp->c_dual = xcalloc(1+npp->ncols, sizeof(double));
+ for (j = 1; j <= npp->ncols; j++)
+ npp->c_dual[j] = DBL_MAX;
+#endif
+ }
+ /* copy solution components from the resultant problem */
+ if (npp->sol == GLP_SOL)
+ { for (i = 1; i <= npp->m; i++)
+ { row = prob->row[i];
+ k = npp->row_ref[i];
+ npp->r_stat[k] = (char)row->stat;
+ /*npp->r_prim[k] = row->prim;*/
+ npp->r_pi[k] = dir * row->dual;
+ }
+ for (j = 1; j <= npp->n; j++)
+ { col = prob->col[j];
+ k = npp->col_ref[j];
+ npp->c_stat[k] = (char)col->stat;
+ npp->c_value[k] = col->prim;
+ /*npp->c_dual[k] = dir * col->dual;*/
+ }
+ }
+ else if (npp->sol == GLP_IPT)
+ { for (i = 1; i <= npp->m; i++)
+ { row = prob->row[i];
+ k = npp->row_ref[i];
+ /*npp->r_prim[k] = row->pval;*/
+ npp->r_pi[k] = dir * row->dval;
+ }
+ for (j = 1; j <= npp->n; j++)
+ { col = prob->col[j];
+ k = npp->col_ref[j];
+ npp->c_value[k] = col->pval;
+ /*npp->c_dual[k] = dir * col->dval;*/
+ }
+ }
+ else if (npp->sol == GLP_MIP)
+ {
+#if 0
+ for (i = 1; i <= npp->m; i++)
+ { row = prob->row[i];
+ k = npp->row_ref[i];
+ /*npp->r_prim[k] = row->mipx;*/
+ }
+#endif
+ for (j = 1; j <= npp->n; j++)
+ { col = prob->col[j];
+ k = npp->col_ref[j];
+ npp->c_value[k] = col->mipx;
+ }
+ }
+ else
+ xassert(npp != npp);
+ /* perform postprocessing to construct solution to the original
+ problem */
+ for (tse = npp->top; tse != NULL; tse = tse->link)
+ { xassert(tse->func != NULL);
+ xassert(tse->func(npp, tse->info) == 0);
+ }
+ return;
+}
+
+void npp_unload_sol(NPP *npp, glp_prob *orig)
+{ /* store solution to the original problem */
+ GLPROW *row;
+ GLPCOL *col;
+ int i, j;
+ double dir;
+ xassert(npp->orig_dir == orig->dir);
+ if (npp->orig_dir == GLP_MIN)
+ dir = +1.0;
+ else if (npp->orig_dir == GLP_MAX)
+ dir = -1.0;
+ else
+ xassert(npp != npp);
+ xassert(npp->orig_m == orig->m);
+ xassert(npp->orig_n == orig->n);
+ xassert(npp->orig_nnz == orig->nnz);
+ if (npp->sol == GLP_SOL)
+ { /* store basic solution */
+ orig->valid = 0;
+ orig->pbs_stat = npp->p_stat;
+ orig->dbs_stat = npp->d_stat;
+ orig->obj_val = orig->c0;
+ orig->some = 0;
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ row->stat = npp->r_stat[i];
+ if (!npp->scaling)
+ { /*row->prim = npp->r_prim[i];*/
+ row->dual = dir * npp->r_pi[i];
+ }
+ else
+ { /*row->prim = npp->r_prim[i] / row->rii;*/
+ row->dual = dir * npp->r_pi[i] * row->rii;
+ }
+ if (row->stat == GLP_BS)
+ row->dual = 0.0;
+ else if (row->stat == GLP_NL)
+ { xassert(row->type == GLP_LO || row->type == GLP_DB);
+ row->prim = row->lb;
+ }
+ else if (row->stat == GLP_NU)
+ { xassert(row->type == GLP_UP || row->type == GLP_DB);
+ row->prim = row->ub;
+ }
+ else if (row->stat == GLP_NF)
+ { xassert(row->type == GLP_FR);
+ row->prim = 0.0;
+ }
+ else if (row->stat == GLP_NS)
+ { xassert(row->type == GLP_FX);
+ row->prim = row->lb;
+ }
+ else
+ xassert(row != row);
+ }
+ for (j = 1; j <= orig->n; j++)
+ { col = orig->col[j];
+ col->stat = npp->c_stat[j];
+ if (!npp->scaling)
+ { col->prim = npp->c_value[j];
+ /*col->dual = dir * npp->c_dual[j];*/
+ }
+ else
+ { col->prim = npp->c_value[j] * col->sjj;
+ /*col->dual = dir * npp->c_dual[j] / col->sjj;*/
+ }
+ if (col->stat == GLP_BS)
+ col->dual = 0.0;
+#if 1
+ else if (col->stat == GLP_NL)
+ { xassert(col->type == GLP_LO || col->type == GLP_DB);
+ col->prim = col->lb;
+ }
+ else if (col->stat == GLP_NU)
+ { xassert(col->type == GLP_UP || col->type == GLP_DB);
+ col->prim = col->ub;
+ }
+ else if (col->stat == GLP_NF)
+ { xassert(col->type == GLP_FR);
+ col->prim = 0.0;
+ }
+ else if (col->stat == GLP_NS)
+ { xassert(col->type == GLP_FX);
+ col->prim = col->lb;
+ }
+ else
+ xassert(col != col);
+#endif
+ orig->obj_val += col->coef * col->prim;
+ }
+#if 1
+ /* compute primal values of inactive rows */
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ if (row->stat == GLP_BS)
+ { GLPAIJ *aij;
+ double temp;
+ temp = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ temp += aij->val * aij->col->prim;
+ row->prim = temp;
+ }
+ }
+ /* compute reduced costs of active columns */
+ for (j = 1; j <= orig->n; j++)
+ { col = orig->col[j];
+ if (col->stat != GLP_BS)
+ { GLPAIJ *aij;
+ double temp;
+ temp = col->coef;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ temp -= aij->val * aij->row->dual;
+ col->dual = temp;
+ }
+ }
+#endif
+ }
+ else if (npp->sol == GLP_IPT)
+ { /* store interior-point solution */
+ orig->ipt_stat = npp->t_stat;
+ orig->ipt_obj = orig->c0;
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ if (!npp->scaling)
+ { /*row->pval = npp->r_prim[i];*/
+ row->dval = dir * npp->r_pi[i];
+ }
+ else
+ { /*row->pval = npp->r_prim[i] / row->rii;*/
+ row->dval = dir * npp->r_pi[i] * row->rii;
+ }
+ }
+ for (j = 1; j <= orig->n; j++)
+ { col = orig->col[j];
+ if (!npp->scaling)
+ { col->pval = npp->c_value[j];
+ /*col->dval = dir * npp->c_dual[j];*/
+ }
+ else
+ { col->pval = npp->c_value[j] * col->sjj;
+ /*col->dval = dir * npp->c_dual[j] / col->sjj;*/
+ }
+ orig->ipt_obj += col->coef * col->pval;
+ }
+#if 1
+ /* compute row primal values */
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ { GLPAIJ *aij;
+ double temp;
+ temp = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ temp += aij->val * aij->col->pval;
+ row->pval = temp;
+ }
+ }
+ /* compute column dual values */
+ for (j = 1; j <= orig->n; j++)
+ { col = orig->col[j];
+ { GLPAIJ *aij;
+ double temp;
+ temp = col->coef;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ temp -= aij->val * aij->row->dval;
+ col->dval = temp;
+ }
+ }
+#endif
+ }
+ else if (npp->sol == GLP_MIP)
+ { /* store MIP solution */
+ xassert(!npp->scaling);
+ orig->mip_stat = npp->i_stat;
+ orig->mip_obj = orig->c0;
+#if 0
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ /*row->mipx = npp->r_prim[i];*/
+ }
+#endif
+ for (j = 1; j <= orig->n; j++)
+ { col = orig->col[j];
+ col->mipx = npp->c_value[j];
+ if (col->kind == GLP_IV)
+ xassert(col->mipx == floor(col->mipx));
+ orig->mip_obj += col->coef * col->mipx;
+ }
+#if 1
+ /* compute row primal values */
+ for (i = 1; i <= orig->m; i++)
+ { row = orig->row[i];
+ { GLPAIJ *aij;
+ double temp;
+ temp = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ temp += aij->val * aij->col->mipx;
+ row->mipx = temp;
+ }
+ }
+#endif
+ }
+ else
+ xassert(npp != npp);
+ return;
+}
+
+void npp_delete_wksp(NPP *npp)
+{ /* delete LP/MIP preprocessor workspace */
+ if (npp->pool != NULL)
+ dmp_delete_pool(npp->pool);
+ if (npp->stack != NULL)
+ dmp_delete_pool(npp->stack);
+ if (npp->row_ref != NULL)
+ xfree(npp->row_ref);
+ if (npp->col_ref != NULL)
+ xfree(npp->col_ref);
+ if (npp->r_stat != NULL)
+ xfree(npp->r_stat);
+#if 0
+ if (npp->r_prim != NULL)
+ xfree(npp->r_prim);
+#endif
+ if (npp->r_pi != NULL)
+ xfree(npp->r_pi);
+ if (npp->c_stat != NULL)
+ xfree(npp->c_stat);
+ if (npp->c_value != NULL)
+ xfree(npp->c_value);
+#if 0
+ if (npp->c_dual != NULL)
+ xfree(npp->c_dual);
+#endif
+ xfree(npp);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp2.c b/test/monniaux/glpk-4.65/src/npp/npp2.c
new file mode 100644
index 00000000..4efcf1d1
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp2.c
@@ -0,0 +1,1433 @@
+/* npp2.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* npp_free_row - process free (unbounded) row
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_free_row(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_free_row processes row p, which is free (i.e. has
+* no finite bounds):
+*
+* -inf < sum a[p,j] x[j] < +inf. (1)
+* j
+*
+* PROBLEM TRANSFORMATION
+*
+* Constraint (1) cannot be active, so it is redundant and can be
+* removed from the original problem.
+*
+* Removing row p leads to removing a column of multiplier pi[p] for
+* this row in the dual system. Since row p has no bounds, pi[p] = 0,
+* so removing the column does not affect the dual solution.
+*
+* RECOVERING BASIC SOLUTION
+*
+* In solution to the original problem row p is inactive constraint,
+* so it is assigned status GLP_BS, and multiplier pi[p] is assigned
+* zero value.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* In solution to the original problem row p is inactive constraint,
+* so its multiplier pi[p] is assigned zero value.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct free_row
+{ /* free (unbounded) row */
+ int p;
+ /* row reference number */
+};
+
+static int rcv_free_row(NPP *npp, void *info);
+
+void npp_free_row(NPP *npp, NPPROW *p)
+{ /* process free (unbounded) row */
+ struct free_row *info;
+ /* the row must be free */
+ xassert(p->lb == -DBL_MAX && p->ub == +DBL_MAX);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_free_row, sizeof(struct free_row));
+ info->p = p->i;
+ /* remove the row from the problem */
+ npp_del_row(npp, p);
+ return;
+}
+
+static int rcv_free_row(NPP *npp, void *_info)
+{ /* recover free (unbounded) row */
+ struct free_row *info = _info;
+ if (npp->sol == GLP_SOL)
+ npp->r_stat[info->p] = GLP_BS;
+ if (npp->sol != GLP_MIP)
+ npp->r_pi[info->p] = 0.0;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_geq_row - process row of 'not less than' type
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_geq_row(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_geq_row processes row p, which is 'not less than'
+* inequality constraint:
+*
+* L[p] <= sum a[p,j] x[j] (<= U[p]), (1)
+* j
+*
+* where L[p] < U[p], and upper bound may not exist (U[p] = +oo).
+*
+* PROBLEM TRANSFORMATION
+*
+* Constraint (1) can be replaced by equality constraint:
+*
+* sum a[p,j] x[j] - s = L[p], (2)
+* j
+*
+* where
+*
+* 0 <= s (<= U[p] - L[p]) (3)
+*
+* is a non-negative surplus variable.
+*
+* Since in the primal system there appears column s having the only
+* non-zero coefficient in row p, in the dual system there appears a
+* new row:
+*
+* (-1) pi[p] + lambda = 0, (4)
+*
+* where (-1) is coefficient of column s in row p, pi[p] is multiplier
+* of row p, lambda is multiplier of column q, 0 is coefficient of
+* column s in the objective row.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of row p in solution to the original problem is determined
+* by its status and status of column q in solution to the transformed
+* problem as follows:
+*
+* +--------------------------------------+------------------+
+* | Transformed problem | Original problem |
+* +-----------------+--------------------+------------------+
+* | Status of row p | Status of column s | Status of row p |
+* +-----------------+--------------------+------------------+
+* | GLP_BS | GLP_BS | N/A |
+* | GLP_BS | GLP_NL | GLP_BS |
+* | GLP_BS | GLP_NU | GLP_BS |
+* | GLP_NS | GLP_BS | GLP_BS |
+* | GLP_NS | GLP_NL | GLP_NL |
+* | GLP_NS | GLP_NU | GLP_NU |
+* +-----------------+--------------------+------------------+
+*
+* Value of row multiplier pi[p] in solution to the original problem
+* is the same as in solution to the transformed problem.
+*
+* 1. In solution to the transformed problem row p and column q cannot
+* be basic at the same time; otherwise the basis matrix would have
+* two linear dependent columns: unity column of auxiliary variable
+* of row p and unity column of variable s.
+*
+* 2. Though in the transformed problem row p is equality constraint,
+* it may be basic due to primal degenerate solution.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of row multiplier pi[p] in solution to the original problem
+* is the same as in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct ineq_row
+{ /* inequality constraint row */
+ int p;
+ /* row reference number */
+ int s;
+ /* column reference number for slack/surplus variable */
+};
+
+static int rcv_geq_row(NPP *npp, void *info);
+
+void npp_geq_row(NPP *npp, NPPROW *p)
+{ /* process row of 'not less than' type */
+ struct ineq_row *info;
+ NPPCOL *s;
+ /* the row must have lower bound */
+ xassert(p->lb != -DBL_MAX);
+ xassert(p->lb < p->ub);
+ /* create column for surplus variable */
+ s = npp_add_col(npp);
+ s->lb = 0.0;
+ s->ub = (p->ub == +DBL_MAX ? +DBL_MAX : p->ub - p->lb);
+ /* and add it to the transformed problem */
+ npp_add_aij(npp, p, s, -1.0);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_geq_row, sizeof(struct ineq_row));
+ info->p = p->i;
+ info->s = s->j;
+ /* replace the row by equality constraint */
+ p->ub = p->lb;
+ return;
+}
+
+static int rcv_geq_row(NPP *npp, void *_info)
+{ /* recover row of 'not less than' type */
+ struct ineq_row *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] == GLP_BS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ { npp_error();
+ return 1;
+ }
+ else if (npp->c_stat[info->s] == GLP_NL ||
+ npp->c_stat[info->s] == GLP_NU)
+ npp->r_stat[info->p] = GLP_BS;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else if (npp->r_stat[info->p] == GLP_NS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ npp->r_stat[info->p] = GLP_BS;
+ else if (npp->c_stat[info->s] == GLP_NL)
+ npp->r_stat[info->p] = GLP_NL;
+ else if (npp->c_stat[info->s] == GLP_NU)
+ npp->r_stat[info->p] = GLP_NU;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_leq_row - process row of 'not greater than' type
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_leq_row(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_leq_row processes row p, which is 'not greater than'
+* inequality constraint:
+*
+* (L[p] <=) sum a[p,j] x[j] <= U[p], (1)
+* j
+*
+* where L[p] < U[p], and lower bound may not exist (L[p] = +oo).
+*
+* PROBLEM TRANSFORMATION
+*
+* Constraint (1) can be replaced by equality constraint:
+*
+* sum a[p,j] x[j] + s = L[p], (2)
+* j
+*
+* where
+*
+* 0 <= s (<= U[p] - L[p]) (3)
+*
+* is a non-negative slack variable.
+*
+* Since in the primal system there appears column s having the only
+* non-zero coefficient in row p, in the dual system there appears a
+* new row:
+*
+* (+1) pi[p] + lambda = 0, (4)
+*
+* where (+1) is coefficient of column s in row p, pi[p] is multiplier
+* of row p, lambda is multiplier of column q, 0 is coefficient of
+* column s in the objective row.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of row p in solution to the original problem is determined
+* by its status and status of column q in solution to the transformed
+* problem as follows:
+*
+* +--------------------------------------+------------------+
+* | Transformed problem | Original problem |
+* +-----------------+--------------------+------------------+
+* | Status of row p | Status of column s | Status of row p |
+* +-----------------+--------------------+------------------+
+* | GLP_BS | GLP_BS | N/A |
+* | GLP_BS | GLP_NL | GLP_BS |
+* | GLP_BS | GLP_NU | GLP_BS |
+* | GLP_NS | GLP_BS | GLP_BS |
+* | GLP_NS | GLP_NL | GLP_NU |
+* | GLP_NS | GLP_NU | GLP_NL |
+* +-----------------+--------------------+------------------+
+*
+* Value of row multiplier pi[p] in solution to the original problem
+* is the same as in solution to the transformed problem.
+*
+* 1. In solution to the transformed problem row p and column q cannot
+* be basic at the same time; otherwise the basis matrix would have
+* two linear dependent columns: unity column of auxiliary variable
+* of row p and unity column of variable s.
+*
+* 2. Though in the transformed problem row p is equality constraint,
+* it may be basic due to primal degeneracy.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of row multiplier pi[p] in solution to the original problem
+* is the same as in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+static int rcv_leq_row(NPP *npp, void *info);
+
+void npp_leq_row(NPP *npp, NPPROW *p)
+{ /* process row of 'not greater than' type */
+ struct ineq_row *info;
+ NPPCOL *s;
+ /* the row must have upper bound */
+ xassert(p->ub != +DBL_MAX);
+ xassert(p->lb < p->ub);
+ /* create column for slack variable */
+ s = npp_add_col(npp);
+ s->lb = 0.0;
+ s->ub = (p->lb == -DBL_MAX ? +DBL_MAX : p->ub - p->lb);
+ /* and add it to the transformed problem */
+ npp_add_aij(npp, p, s, +1.0);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_leq_row, sizeof(struct ineq_row));
+ info->p = p->i;
+ info->s = s->j;
+ /* replace the row by equality constraint */
+ p->lb = p->ub;
+ return;
+}
+
+static int rcv_leq_row(NPP *npp, void *_info)
+{ /* recover row of 'not greater than' type */
+ struct ineq_row *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] == GLP_BS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ { npp_error();
+ return 1;
+ }
+ else if (npp->c_stat[info->s] == GLP_NL ||
+ npp->c_stat[info->s] == GLP_NU)
+ npp->r_stat[info->p] = GLP_BS;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else if (npp->r_stat[info->p] == GLP_NS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ npp->r_stat[info->p] = GLP_BS;
+ else if (npp->c_stat[info->s] == GLP_NL)
+ npp->r_stat[info->p] = GLP_NU;
+ else if (npp->c_stat[info->s] == GLP_NU)
+ npp->r_stat[info->p] = GLP_NL;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_free_col - process free (unbounded) column
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_free_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_free_col processes column q, which is free (i.e. has
+* no finite bounds):
+*
+* -oo < x[q] < +oo. (1)
+*
+* PROBLEM TRANSFORMATION
+*
+* Free (unbounded) variable can be replaced by the difference of two
+* non-negative variables:
+*
+* x[q] = s' - s'', s', s'' >= 0. (2)
+*
+* Assuming that in the transformed problem x[q] becomes s',
+* transformation (2) causes new column s'' to appear, which differs
+* from column s' only in the sign of coefficients in constraint and
+* objective rows. Thus, if in the dual system the following row
+* corresponds to column s':
+*
+* sum a[i,q] pi[i] + lambda' = c[q], (3)
+* i
+*
+* the row which corresponds to column s'' is the following:
+*
+* sum (-a[i,q]) pi[i] + lambda'' = -c[q]. (4)
+* i
+*
+* Then from (3) and (4) it follows that:
+*
+* lambda' + lambda'' = 0 => lambda' = lmabda'' = 0, (5)
+*
+* where lambda' and lambda'' are multipliers for columns s' and s'',
+* resp.
+*
+* RECOVERING BASIC SOLUTION
+*
+* With respect to (5) status of column q in solution to the original
+* problem is determined by statuses of columns s' and s'' in solution
+* to the transformed problem as follows:
+*
+* +--------------------------------------+------------------+
+* | Transformed problem | Original problem |
+* +------------------+-------------------+------------------+
+* | Status of col s' | Status of col s'' | Status of col q |
+* +------------------+-------------------+------------------+
+* | GLP_BS | GLP_BS | N/A |
+* | GLP_BS | GLP_NL | GLP_BS |
+* | GLP_NL | GLP_BS | GLP_BS |
+* | GLP_NL | GLP_NL | GLP_NF |
+* +------------------+-------------------+------------------+
+*
+* Value of column q is computed with formula (2).
+*
+* 1. In solution to the transformed problem columns s' and s'' cannot
+* be basic at the same time, because they differ only in the sign,
+* hence, are linear dependent.
+*
+* 2. Though column q is free, it can be non-basic due to dual
+* degeneracy.
+*
+* 3. If column q is integral, columns s' and s'' are also integral.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q is computed with formula (2).
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is computed with formula (2). */
+
+struct free_col
+{ /* free (unbounded) column */
+ int q;
+ /* column reference number for variables x[q] and s' */
+ int s;
+ /* column reference number for variable s'' */
+};
+
+static int rcv_free_col(NPP *npp, void *info);
+
+void npp_free_col(NPP *npp, NPPCOL *q)
+{ /* process free (unbounded) column */
+ struct free_col *info;
+ NPPCOL *s;
+ NPPAIJ *aij;
+ /* the column must be free */
+ xassert(q->lb == -DBL_MAX && q->ub == +DBL_MAX);
+ /* variable x[q] becomes s' */
+ q->lb = 0.0, q->ub = +DBL_MAX;
+ /* create variable s'' */
+ s = npp_add_col(npp);
+ s->is_int = q->is_int;
+ s->lb = 0.0, s->ub = +DBL_MAX;
+ /* duplicate objective coefficient */
+ s->coef = -q->coef;
+ /* duplicate column of the constraint matrix */
+ for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ npp_add_aij(npp, aij->row, s, -aij->val);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_free_col, sizeof(struct free_col));
+ info->q = q->j;
+ info->s = s->j;
+ return;
+}
+
+static int rcv_free_col(NPP *npp, void *_info)
+{ /* recover free (unbounded) column */
+ struct free_col *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->c_stat[info->q] == GLP_BS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ { npp_error();
+ return 1;
+ }
+ else if (npp->c_stat[info->s] == GLP_NL)
+ npp->c_stat[info->q] = GLP_BS;
+ else
+ { npp_error();
+ return -1;
+ }
+ }
+ else if (npp->c_stat[info->q] == GLP_NL)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ npp->c_stat[info->q] = GLP_BS;
+ else if (npp->c_stat[info->s] == GLP_NL)
+ npp->c_stat[info->q] = GLP_NF;
+ else
+ { npp_error();
+ return -1;
+ }
+ }
+ else
+ { npp_error();
+ return -1;
+ }
+ }
+ /* compute value of x[q] with formula (2) */
+ npp->c_value[info->q] -= npp->c_value[info->s];
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_lbnd_col - process column with (non-zero) lower bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_lbnd_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_lbnd_col processes column q, which has (non-zero)
+* lower bound:
+*
+* l[q] <= x[q] (<= u[q]), (1)
+*
+* where l[q] < u[q], and upper bound may not exist (u[q] = +oo).
+*
+* PROBLEM TRANSFORMATION
+*
+* Column q can be replaced as follows:
+*
+* x[q] = l[q] + s, (2)
+*
+* where
+*
+* 0 <= s (<= u[q] - l[q]) (3)
+*
+* is a non-negative variable.
+*
+* Substituting x[q] from (2) into the objective row, we have:
+*
+* z = sum c[j] x[j] + c0 =
+* j
+*
+* = sum c[j] x[j] + c[q] x[q] + c0 =
+* j!=q
+*
+* = sum c[j] x[j] + c[q] (l[q] + s) + c0 =
+* j!=q
+*
+* = sum c[j] x[j] + c[q] s + c~0,
+*
+* where
+*
+* c~0 = c0 + c[q] l[q] (4)
+*
+* is the constant term of the objective in the transformed problem.
+* Similarly, substituting x[q] into constraint row i, we have:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i] ==>
+* j
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
+* j!=q
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] (l[q] + s) <= U[i] ==>
+* j!=q
+*
+* L~[i] <= sum a[i,j] x[j] + a[i,q] s <= U~[i],
+* j!=q
+*
+* where
+*
+* L~[i] = L[i] - a[i,q] l[q], U~[i] = U[i] - a[i,q] l[q] (5)
+*
+* are lower and upper bounds of row i in the transformed problem,
+* resp.
+*
+* Transformation (2) does not affect the dual system.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of column q in solution to the original problem is the same
+* as in solution to the transformed problem (GLP_BS, GLP_NL or GLP_NU).
+* Value of column q is computed with formula (2).
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q is computed with formula (2).
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is computed with formula (2). */
+
+struct bnd_col
+{ /* bounded column */
+ int q;
+ /* column reference number for variables x[q] and s */
+ double bnd;
+ /* lower/upper bound l[q] or u[q] */
+};
+
+static int rcv_lbnd_col(NPP *npp, void *info);
+
+void npp_lbnd_col(NPP *npp, NPPCOL *q)
+{ /* process column with (non-zero) lower bound */
+ struct bnd_col *info;
+ NPPROW *i;
+ NPPAIJ *aij;
+ /* the column must have non-zero lower bound */
+ xassert(q->lb != 0.0);
+ xassert(q->lb != -DBL_MAX);
+ xassert(q->lb < q->ub);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_lbnd_col, sizeof(struct bnd_col));
+ info->q = q->j;
+ info->bnd = q->lb;
+ /* substitute x[q] into objective row */
+ npp->c0 += q->coef * q->lb;
+ /* substitute x[q] into constraint rows */
+ for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row;
+ if (i->lb == i->ub)
+ i->ub = (i->lb -= aij->val * q->lb);
+ else
+ { if (i->lb != -DBL_MAX)
+ i->lb -= aij->val * q->lb;
+ if (i->ub != +DBL_MAX)
+ i->ub -= aij->val * q->lb;
+ }
+ }
+ /* column x[q] becomes column s */
+ if (q->ub != +DBL_MAX)
+ q->ub -= q->lb;
+ q->lb = 0.0;
+ return;
+}
+
+static int rcv_lbnd_col(NPP *npp, void *_info)
+{ /* recover column with (non-zero) lower bound */
+ struct bnd_col *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->c_stat[info->q] == GLP_BS ||
+ npp->c_stat[info->q] == GLP_NL ||
+ npp->c_stat[info->q] == GLP_NU)
+ npp->c_stat[info->q] = npp->c_stat[info->q];
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ /* compute value of x[q] with formula (2) */
+ npp->c_value[info->q] = info->bnd + npp->c_value[info->q];
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_ubnd_col - process column with upper bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_ubnd_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_ubnd_col processes column q, which has upper bound:
+*
+* (l[q] <=) x[q] <= u[q], (1)
+*
+* where l[q] < u[q], and lower bound may not exist (l[q] = -oo).
+*
+* PROBLEM TRANSFORMATION
+*
+* Column q can be replaced as follows:
+*
+* x[q] = u[q] - s, (2)
+*
+* where
+*
+* 0 <= s (<= u[q] - l[q]) (3)
+*
+* is a non-negative variable.
+*
+* Substituting x[q] from (2) into the objective row, we have:
+*
+* z = sum c[j] x[j] + c0 =
+* j
+*
+* = sum c[j] x[j] + c[q] x[q] + c0 =
+* j!=q
+*
+* = sum c[j] x[j] + c[q] (u[q] - s) + c0 =
+* j!=q
+*
+* = sum c[j] x[j] - c[q] s + c~0,
+*
+* where
+*
+* c~0 = c0 + c[q] u[q] (4)
+*
+* is the constant term of the objective in the transformed problem.
+* Similarly, substituting x[q] into constraint row i, we have:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i] ==>
+* j
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
+* j!=q
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] (u[q] - s) <= U[i] ==>
+* j!=q
+*
+* L~[i] <= sum a[i,j] x[j] - a[i,q] s <= U~[i],
+* j!=q
+*
+* where
+*
+* L~[i] = L[i] - a[i,q] u[q], U~[i] = U[i] - a[i,q] u[q] (5)
+*
+* are lower and upper bounds of row i in the transformed problem,
+* resp.
+*
+* Note that in the transformed problem coefficients c[q] and a[i,q]
+* change their sign. Thus, the row of the dual system corresponding to
+* column q:
+*
+* sum a[i,q] pi[i] + lambda[q] = c[q] (6)
+* i
+*
+* in the transformed problem becomes the following:
+*
+* sum (-a[i,q]) pi[i] + lambda[s] = -c[q]. (7)
+* i
+*
+* Therefore:
+*
+* lambda[q] = - lambda[s], (8)
+*
+* where lambda[q] is multiplier for column q, lambda[s] is multiplier
+* for column s.
+*
+* RECOVERING BASIC SOLUTION
+*
+* With respect to (8) status of column q in solution to the original
+* problem is determined by status of column s in solution to the
+* transformed problem as follows:
+*
+* +-----------------------+--------------------+
+* | Status of column s | Status of column q |
+* | (transformed problem) | (original problem) |
+* +-----------------------+--------------------+
+* | GLP_BS | GLP_BS |
+* | GLP_NL | GLP_NU |
+* | GLP_NU | GLP_NL |
+* +-----------------------+--------------------+
+*
+* Value of column q is computed with formula (2).
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q is computed with formula (2).
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is computed with formula (2). */
+
+static int rcv_ubnd_col(NPP *npp, void *info);
+
+void npp_ubnd_col(NPP *npp, NPPCOL *q)
+{ /* process column with upper bound */
+ struct bnd_col *info;
+ NPPROW *i;
+ NPPAIJ *aij;
+ /* the column must have upper bound */
+ xassert(q->ub != +DBL_MAX);
+ xassert(q->lb < q->ub);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_ubnd_col, sizeof(struct bnd_col));
+ info->q = q->j;
+ info->bnd = q->ub;
+ /* substitute x[q] into objective row */
+ npp->c0 += q->coef * q->ub;
+ q->coef = -q->coef;
+ /* substitute x[q] into constraint rows */
+ for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row;
+ if (i->lb == i->ub)
+ i->ub = (i->lb -= aij->val * q->ub);
+ else
+ { if (i->lb != -DBL_MAX)
+ i->lb -= aij->val * q->ub;
+ if (i->ub != +DBL_MAX)
+ i->ub -= aij->val * q->ub;
+ }
+ aij->val = -aij->val;
+ }
+ /* column x[q] becomes column s */
+ if (q->lb != -DBL_MAX)
+ q->ub -= q->lb;
+ else
+ q->ub = +DBL_MAX;
+ q->lb = 0.0;
+ return;
+}
+
+static int rcv_ubnd_col(NPP *npp, void *_info)
+{ /* recover column with upper bound */
+ struct bnd_col *info = _info;
+ if (npp->sol == GLP_BS)
+ { if (npp->c_stat[info->q] == GLP_BS)
+ npp->c_stat[info->q] = GLP_BS;
+ else if (npp->c_stat[info->q] == GLP_NL)
+ npp->c_stat[info->q] = GLP_NU;
+ else if (npp->c_stat[info->q] == GLP_NU)
+ npp->c_stat[info->q] = GLP_NL;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ /* compute value of x[q] with formula (2) */
+ npp->c_value[info->q] = info->bnd - npp->c_value[info->q];
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_dbnd_col - process non-negative column with upper bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_dbnd_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_dbnd_col processes column q, which is non-negative
+* and has upper bound:
+*
+* 0 <= x[q] <= u[q], (1)
+*
+* where u[q] > 0.
+*
+* PROBLEM TRANSFORMATION
+*
+* Upper bound of column q can be replaced by the following equality
+* constraint:
+*
+* x[q] + s = u[q], (2)
+*
+* where s >= 0 is a non-negative complement variable.
+*
+* Since in the primal system along with new row (2) there appears a
+* new column s having the only non-zero coefficient in this row, in
+* the dual system there appears a new row:
+*
+* (+1)pi + lambda[s] = 0, (3)
+*
+* where (+1) is coefficient at column s in row (2), pi is multiplier
+* for row (2), lambda[s] is multiplier for column s, 0 is coefficient
+* at column s in the objective row.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of column q in solution to the original problem is determined
+* by its status and status of column s in solution to the transformed
+* problem as follows:
+*
+* +-----------------------------------+------------------+
+* | Transformed problem | Original problem |
+* +-----------------+-----------------+------------------+
+* | Status of col q | Status of col s | Status of col q |
+* +-----------------+-----------------+------------------+
+* | GLP_BS | GLP_BS | GLP_BS |
+* | GLP_BS | GLP_NL | GLP_NU |
+* | GLP_NL | GLP_BS | GLP_NL |
+* | GLP_NL | GLP_NL | GLP_NL (*) |
+* +-----------------+-----------------+------------------+
+*
+* Value of column q in solution to the original problem is the same as
+* in solution to the transformed problem.
+*
+* 1. Formally, in solution to the transformed problem columns q and s
+* cannot be non-basic at the same time, since the constraint (2)
+* would be violated. However, if u[q] is close to zero, violation
+* may be less than a working precision even if both columns q and s
+* are non-basic. In this degenerate case row (2) can be only basic,
+* i.e. non-active constraint (otherwise corresponding row of the
+* basis matrix would be zero). This allows to pivot out auxiliary
+* variable and pivot in column s, in which case the row becomes
+* active while column s becomes basic.
+*
+* 2. If column q is integral, column s is also integral.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q in solution to the original problem is the same as
+* in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q in solution to the original problem is the same as
+* in solution to the transformed problem. */
+
+struct dbnd_col
+{ /* double-bounded column */
+ int q;
+ /* column reference number for variable x[q] */
+ int s;
+ /* column reference number for complement variable s */
+};
+
+static int rcv_dbnd_col(NPP *npp, void *info);
+
+void npp_dbnd_col(NPP *npp, NPPCOL *q)
+{ /* process non-negative column with upper bound */
+ struct dbnd_col *info;
+ NPPROW *p;
+ NPPCOL *s;
+ /* the column must be non-negative with upper bound */
+ xassert(q->lb == 0.0);
+ xassert(q->ub > 0.0);
+ xassert(q->ub != +DBL_MAX);
+ /* create variable s */
+ s = npp_add_col(npp);
+ s->is_int = q->is_int;
+ s->lb = 0.0, s->ub = +DBL_MAX;
+ /* create equality constraint (2) */
+ p = npp_add_row(npp);
+ p->lb = p->ub = q->ub;
+ npp_add_aij(npp, p, q, +1.0);
+ npp_add_aij(npp, p, s, +1.0);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_dbnd_col, sizeof(struct dbnd_col));
+ info->q = q->j;
+ info->s = s->j;
+ /* remove upper bound of x[q] */
+ q->ub = +DBL_MAX;
+ return;
+}
+
+static int rcv_dbnd_col(NPP *npp, void *_info)
+{ /* recover non-negative column with upper bound */
+ struct dbnd_col *info = _info;
+ if (npp->sol == GLP_BS)
+ { if (npp->c_stat[info->q] == GLP_BS)
+ { if (npp->c_stat[info->s] == GLP_BS)
+ npp->c_stat[info->q] = GLP_BS;
+ else if (npp->c_stat[info->s] == GLP_NL)
+ npp->c_stat[info->q] = GLP_NU;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else if (npp->c_stat[info->q] == GLP_NL)
+ { if (npp->c_stat[info->s] == GLP_BS ||
+ npp->c_stat[info->s] == GLP_NL)
+ npp->c_stat[info->q] = GLP_NL;
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_fixed_col - process fixed column
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_fixed_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_fixed_col processes column q, which is fixed:
+*
+* x[q] = s[q], (1)
+*
+* where s[q] is a fixed column value.
+*
+* PROBLEM TRANSFORMATION
+*
+* The value of a fixed column can be substituted into the objective
+* and constraint rows that allows removing the column from the problem.
+*
+* Substituting x[q] = s[q] into the objective row, we have:
+*
+* z = sum c[j] x[j] + c0 =
+* j
+*
+* = sum c[j] x[j] + c[q] x[q] + c0 =
+* j!=q
+*
+* = sum c[j] x[j] + c[q] s[q] + c0 =
+* j!=q
+*
+* = sum c[j] x[j] + c~0,
+* j!=q
+*
+* where
+*
+* c~0 = c0 + c[q] s[q] (2)
+*
+* is the constant term of the objective in the transformed problem.
+* Similarly, substituting x[q] = s[q] into constraint row i, we have:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i] ==>
+* j
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
+* j!=q
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] s[q] <= U[i] ==>
+* j!=q
+*
+* L~[i] <= sum a[i,j] x[j] + a[i,q] s <= U~[i],
+* j!=q
+*
+* where
+*
+* L~[i] = L[i] - a[i,q] s[q], U~[i] = U[i] - a[i,q] s[q] (3)
+*
+* are lower and upper bounds of row i in the transformed problem,
+* resp.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Column q is assigned status GLP_NS and its value is assigned s[q].
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q is assigned s[q].
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is assigned s[q]. */
+
+struct fixed_col
+{ /* fixed column */
+ int q;
+ /* column reference number for variable x[q] */
+ double s;
+ /* value, at which x[q] is fixed */
+};
+
+static int rcv_fixed_col(NPP *npp, void *info);
+
+void npp_fixed_col(NPP *npp, NPPCOL *q)
+{ /* process fixed column */
+ struct fixed_col *info;
+ NPPROW *i;
+ NPPAIJ *aij;
+ /* the column must be fixed */
+ xassert(q->lb == q->ub);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_fixed_col, sizeof(struct fixed_col));
+ info->q = q->j;
+ info->s = q->lb;
+ /* substitute x[q] = s[q] into objective row */
+ npp->c0 += q->coef * q->lb;
+ /* substitute x[q] = s[q] into constraint rows */
+ for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row;
+ if (i->lb == i->ub)
+ i->ub = (i->lb -= aij->val * q->lb);
+ else
+ { if (i->lb != -DBL_MAX)
+ i->lb -= aij->val * q->lb;
+ if (i->ub != +DBL_MAX)
+ i->ub -= aij->val * q->lb;
+ }
+ }
+ /* remove the column from the problem */
+ npp_del_col(npp, q);
+ return;
+}
+
+static int rcv_fixed_col(NPP *npp, void *_info)
+{ /* recover fixed column */
+ struct fixed_col *info = _info;
+ if (npp->sol == GLP_SOL)
+ npp->c_stat[info->q] = GLP_NS;
+ npp->c_value[info->q] = info->s;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_make_equality - process row with almost identical bounds
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_make_equality(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_make_equality processes row p:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p], (1)
+* j
+*
+* where -oo < L[p] < U[p] < +oo, i.e. which is double-sided inequality
+* constraint.
+*
+* RETURNS
+*
+* 0 - row bounds have not been changed;
+*
+* 1 - row has been replaced by equality constraint.
+*
+* PROBLEM TRANSFORMATION
+*
+* If bounds of row (1) are very close to each other:
+*
+* U[p] - L[p] <= eps, (2)
+*
+* where eps is an absolute tolerance for row value, the row can be
+* replaced by the following almost equivalent equiality constraint:
+*
+* sum a[p,j] x[j] = b, (3)
+* j
+*
+* where b = (L[p] + U[p]) / 2. If the right-hand side in (3) happens
+* to be very close to its nearest integer:
+*
+* |b - floor(b + 0.5)| <= eps, (4)
+*
+* it is reasonable to use this nearest integer as the right-hand side.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of row p in solution to the original problem is determined
+* by its status and the sign of its multiplier pi[p] in solution to
+* the transformed problem as follows:
+*
+* +-----------------------+---------+--------------------+
+* | Status of row p | Sign of | Status of row p |
+* | (transformed problem) | pi[p] | (original problem) |
+* +-----------------------+---------+--------------------+
+* | GLP_BS | + / - | GLP_BS |
+* | GLP_NS | + | GLP_NL |
+* | GLP_NS | - | GLP_NU |
+* +-----------------------+---------+--------------------+
+*
+* Value of row multiplier pi[p] in solution to the original problem is
+* the same as in solution to the transformed problem.
+*
+* RECOVERING INTERIOR POINT SOLUTION
+*
+* Value of row multiplier pi[p] in solution to the original problem is
+* the same as in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct make_equality
+{ /* row with almost identical bounds */
+ int p;
+ /* row reference number */
+};
+
+static int rcv_make_equality(NPP *npp, void *info);
+
+int npp_make_equality(NPP *npp, NPPROW *p)
+{ /* process row with almost identical bounds */
+ struct make_equality *info;
+ double b, eps, nint;
+ /* the row must be double-sided inequality */
+ xassert(p->lb != -DBL_MAX);
+ xassert(p->ub != +DBL_MAX);
+ xassert(p->lb < p->ub);
+ /* check row bounds */
+ eps = 1e-9 + 1e-12 * fabs(p->lb);
+ if (p->ub - p->lb > eps) return 0;
+ /* row bounds are very close to each other */
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_make_equality, sizeof(struct make_equality));
+ info->p = p->i;
+ /* compute right-hand side */
+ b = 0.5 * (p->ub + p->lb);
+ nint = floor(b + 0.5);
+ if (fabs(b - nint) <= eps) b = nint;
+ /* replace row p by almost equivalent equality constraint */
+ p->lb = p->ub = b;
+ return 1;
+}
+
+int rcv_make_equality(NPP *npp, void *_info)
+{ /* recover row with almost identical bounds */
+ struct make_equality *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] == GLP_BS)
+ npp->r_stat[info->p] = GLP_BS;
+ else if (npp->r_stat[info->p] == GLP_NS)
+ { if (npp->r_pi[info->p] >= 0.0)
+ npp->r_stat[info->p] = GLP_NL;
+ else
+ npp->r_stat[info->p] = GLP_NU;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_make_fixed - process column with almost identical bounds
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_make_fixed(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_make_fixed processes column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where -oo < l[q] < u[q] < +oo, i.e. which has both lower and upper
+* bounds.
+*
+* RETURNS
+*
+* 0 - column bounds have not been changed;
+*
+* 1 - column has been fixed.
+*
+* PROBLEM TRANSFORMATION
+*
+* If bounds of column (1) are very close to each other:
+*
+* u[q] - l[q] <= eps, (2)
+*
+* where eps is an absolute tolerance for column value, the column can
+* be fixed:
+*
+* x[q] = s[q], (3)
+*
+* where s[q] = (l[q] + u[q]) / 2. And if the fixed column value s[q]
+* happens to be very close to its nearest integer:
+*
+* |s[q] - floor(s[q] + 0.5)| <= eps, (4)
+*
+* it is reasonable to use this nearest integer as the fixed value.
+*
+* RECOVERING BASIC SOLUTION
+*
+* In the dual system of the original (as well as transformed) problem
+* column q corresponds to the following row:
+*
+* sum a[i,q] pi[i] + lambda[q] = c[q]. (5)
+* i
+*
+* Since multipliers pi[i] are known for all rows from solution to the
+* transformed problem, formula (5) allows computing value of multiplier
+* (reduced cost) for column q:
+*
+* lambda[q] = c[q] - sum a[i,q] pi[i]. (6)
+* i
+*
+* Status of column q in solution to the original problem is determined
+* by its status and the sign of its multiplier lambda[q] in solution to
+* the transformed problem as follows:
+*
+* +-----------------------+-----------+--------------------+
+* | Status of column q | Sign of | Status of column q |
+* | (transformed problem) | lambda[q] | (original problem) |
+* +-----------------------+-----------+--------------------+
+* | GLP_BS | + / - | GLP_BS |
+* | GLP_NS | + | GLP_NL |
+* | GLP_NS | - | GLP_NU |
+* +-----------------------+-----------+--------------------+
+*
+* Value of column q in solution to the original problem is the same as
+* in solution to the transformed problem.
+*
+* RECOVERING INTERIOR POINT SOLUTION
+*
+* Value of column q in solution to the original problem is the same as
+* in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct make_fixed
+{ /* column with almost identical bounds */
+ int q;
+ /* column reference number */
+ double c;
+ /* objective coefficient at x[q] */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[i,q] */
+};
+
+static int rcv_make_fixed(NPP *npp, void *info);
+
+int npp_make_fixed(NPP *npp, NPPCOL *q)
+{ /* process column with almost identical bounds */
+ struct make_fixed *info;
+ NPPAIJ *aij;
+ NPPLFE *lfe;
+ double s, eps, nint;
+ /* the column must be double-bounded */
+ xassert(q->lb != -DBL_MAX);
+ xassert(q->ub != +DBL_MAX);
+ xassert(q->lb < q->ub);
+ /* check column bounds */
+ eps = 1e-9 + 1e-12 * fabs(q->lb);
+ if (q->ub - q->lb > eps) return 0;
+ /* column bounds are very close to each other */
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_make_fixed, sizeof(struct make_fixed));
+ info->q = q->j;
+ info->c = q->coef;
+ info->ptr = NULL;
+ /* save column coefficients a[i,q] (needed for basic solution
+ only) */
+ if (npp->sol == GLP_SOL)
+ { for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = aij->row->i;
+ lfe->val = aij->val;
+ lfe->next = info->ptr;
+ info->ptr = lfe;
+ }
+ }
+ /* compute column fixed value */
+ s = 0.5 * (q->ub + q->lb);
+ nint = floor(s + 0.5);
+ if (fabs(s - nint) <= eps) s = nint;
+ /* make column q fixed */
+ q->lb = q->ub = s;
+ return 1;
+}
+
+static int rcv_make_fixed(NPP *npp, void *_info)
+{ /* recover column with almost identical bounds */
+ struct make_fixed *info = _info;
+ NPPLFE *lfe;
+ double lambda;
+ if (npp->sol == GLP_SOL)
+ { if (npp->c_stat[info->q] == GLP_BS)
+ npp->c_stat[info->q] = GLP_BS;
+ else if (npp->c_stat[info->q] == GLP_NS)
+ { /* compute multiplier for column q with formula (6) */
+ lambda = info->c;
+ for (lfe = info->ptr; lfe != NULL; lfe = lfe->next)
+ lambda -= lfe->val * npp->r_pi[lfe->ref];
+ /* assign status to non-basic column */
+ if (lambda >= 0.0)
+ npp->c_stat[info->q] = GLP_NL;
+ else
+ npp->c_stat[info->q] = GLP_NU;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp3.c b/test/monniaux/glpk-4.65/src/npp/npp3.c
new file mode 100644
index 00000000..883af127
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp3.c
@@ -0,0 +1,2861 @@
+/* npp3.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* npp_empty_row - process empty row
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_empty_row(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_empty_row processes row p, which is empty, i.e.
+* coefficients at all columns in this row are zero:
+*
+* L[p] <= sum 0 x[j] <= U[p], (1)
+*
+* where L[p] <= U[p].
+*
+* RETURNS
+*
+* 0 - success;
+*
+* 1 - problem has no primal feasible solution.
+*
+* PROBLEM TRANSFORMATION
+*
+* If the following conditions hold:
+*
+* L[p] <= +eps, U[p] >= -eps, (2)
+*
+* where eps is an absolute tolerance for row value, the row p is
+* redundant. In this case it can be replaced by equivalent redundant
+* row, which is free (unbounded), and then removed from the problem.
+* Otherwise, the row p is infeasible and, thus, the problem has no
+* primal feasible solution.
+*
+* RECOVERING BASIC SOLUTION
+*
+* See the routine npp_free_row.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* See the routine npp_free_row.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+int npp_empty_row(NPP *npp, NPPROW *p)
+{ /* process empty row */
+ double eps = 1e-3;
+ /* the row must be empty */
+ xassert(p->ptr == NULL);
+ /* check primal feasibility */
+ if (p->lb > +eps || p->ub < -eps)
+ return 1;
+ /* replace the row by equivalent free (unbounded) row */
+ p->lb = -DBL_MAX, p->ub = +DBL_MAX;
+ /* and process it */
+ npp_free_row(npp, p);
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_empty_col - process empty column
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_empty_col(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_empty_col processes column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] <= u[q], which is empty, i.e. has zero coefficients in
+* all constraint rows.
+*
+* RETURNS
+*
+* 0 - success;
+*
+* 1 - problem has no dual feasible solution.
+*
+* PROBLEM TRANSFORMATION
+*
+* The row of the dual system corresponding to the empty column is the
+* following:
+*
+* sum 0 pi[i] + lambda[q] = c[q], (2)
+* i
+*
+* from which it follows that:
+*
+* lambda[q] = c[q]. (3)
+*
+* If the following condition holds:
+*
+* c[q] < - eps, (4)
+*
+* where eps is an absolute tolerance for column multiplier, the lower
+* column bound l[q] must be active to provide dual feasibility (note
+* that being preprocessed the problem is always minimization). In this
+* case the column can be fixed on its lower bound and removed from the
+* problem (if the column is integral, its bounds are also assumed to
+* be integral). And if the column has no lower bound (l[q] = -oo), the
+* problem has no dual feasible solution.
+*
+* If the following condition holds:
+*
+* c[q] > + eps, (5)
+*
+* the upper column bound u[q] must be active to provide dual
+* feasibility. In this case the column can be fixed on its upper bound
+* and removed from the problem. And if the column has no upper bound
+* (u[q] = +oo), the problem has no dual feasible solution.
+*
+* Finally, if the following condition holds:
+*
+* - eps <= c[q] <= +eps, (6)
+*
+* dual feasibility does not depend on a particular value of column q.
+* In this case the column can be fixed either on its lower bound (if
+* l[q] > -oo) or on its upper bound (if u[q] < +oo) or at zero (if the
+* column is unbounded) and then removed from the problem.
+*
+* RECOVERING BASIC SOLUTION
+*
+* See the routine npp_fixed_col. Having been recovered the column
+* is assigned status GLP_NS. However, if actually it is not fixed
+* (l[q] < u[q]), its status should be changed to GLP_NL, GLP_NU, or
+* GLP_NF depending on which bound it was fixed on transformation stage.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* See the routine npp_fixed_col.
+*
+* RECOVERING MIP SOLUTION
+*
+* See the routine npp_fixed_col. */
+
+struct empty_col
+{ /* empty column */
+ int q;
+ /* column reference number */
+ char stat;
+ /* status in basic solution */
+};
+
+static int rcv_empty_col(NPP *npp, void *info);
+
+int npp_empty_col(NPP *npp, NPPCOL *q)
+{ /* process empty column */
+ struct empty_col *info;
+ double eps = 1e-3;
+ /* the column must be empty */
+ xassert(q->ptr == NULL);
+ /* check dual feasibility */
+ if (q->coef > +eps && q->lb == -DBL_MAX)
+ return 1;
+ if (q->coef < -eps && q->ub == +DBL_MAX)
+ return 1;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_empty_col, sizeof(struct empty_col));
+ info->q = q->j;
+ /* fix the column */
+ if (q->lb == -DBL_MAX && q->ub == +DBL_MAX)
+ { /* free column */
+ info->stat = GLP_NF;
+ q->lb = q->ub = 0.0;
+ }
+ else if (q->ub == +DBL_MAX)
+lo: { /* column with lower bound */
+ info->stat = GLP_NL;
+ q->ub = q->lb;
+ }
+ else if (q->lb == -DBL_MAX)
+up: { /* column with upper bound */
+ info->stat = GLP_NU;
+ q->lb = q->ub;
+ }
+ else if (q->lb != q->ub)
+ { /* double-bounded column */
+ if (q->coef >= +DBL_EPSILON) goto lo;
+ if (q->coef <= -DBL_EPSILON) goto up;
+ if (fabs(q->lb) <= fabs(q->ub)) goto lo; else goto up;
+ }
+ else
+ { /* fixed column */
+ info->stat = GLP_NS;
+ }
+ /* process fixed column */
+ npp_fixed_col(npp, q);
+ return 0;
+}
+
+static int rcv_empty_col(NPP *npp, void *_info)
+{ /* recover empty column */
+ struct empty_col *info = _info;
+ if (npp->sol == GLP_SOL)
+ npp->c_stat[info->q] = info->stat;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_value - process implied column value
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_implied_value(NPP *npp, NPPCOL *q, double s);
+*
+* DESCRIPTION
+*
+* For column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] < u[q], the routine npp_implied_value processes its
+* implied value s[q]. If this implied value satisfies to the current
+* column bounds and integrality condition, the routine fixes column q
+* at the given point. Note that the column is kept in the problem in
+* any case.
+*
+* RETURNS
+*
+* 0 - column has been fixed;
+*
+* 1 - implied value violates to current column bounds;
+*
+* 2 - implied value violates integrality condition.
+*
+* ALGORITHM
+*
+* Implied column value s[q] satisfies to the current column bounds if
+* the following condition holds:
+*
+* l[q] - eps <= s[q] <= u[q] + eps, (2)
+*
+* where eps is an absolute tolerance for column value. If the column
+* is integral, the following condition also must hold:
+*
+* |s[q] - floor(s[q]+0.5)| <= eps, (3)
+*
+* where floor(s[q]+0.5) is the nearest integer to s[q].
+*
+* If both condition (2) and (3) are satisfied, the column can be fixed
+* at the value s[q], or, if it is integral, at floor(s[q]+0.5).
+* Otherwise, if s[q] violates (2) or (3), the problem has no feasible
+* solution.
+*
+* Note: If s[q] is close to l[q] or u[q], it seems to be reasonable to
+* fix the column at its lower or upper bound, resp. rather than at the
+* implied value. */
+
+int npp_implied_value(NPP *npp, NPPCOL *q, double s)
+{ /* process implied column value */
+ double eps, nint;
+ xassert(npp == npp);
+ /* column must not be fixed */
+ xassert(q->lb < q->ub);
+ /* check integrality */
+ if (q->is_int)
+ { nint = floor(s + 0.5);
+ if (fabs(s - nint) <= 1e-5)
+ s = nint;
+ else
+ return 2;
+ }
+ /* check current column lower bound */
+ if (q->lb != -DBL_MAX)
+ { eps = (q->is_int ? 1e-5 : 1e-5 + 1e-8 * fabs(q->lb));
+ if (s < q->lb - eps) return 1;
+ /* if s[q] is close to l[q], fix column at its lower bound
+ rather than at the implied value */
+ if (s < q->lb + 1e-3 * eps)
+ { q->ub = q->lb;
+ return 0;
+ }
+ }
+ /* check current column upper bound */
+ if (q->ub != +DBL_MAX)
+ { eps = (q->is_int ? 1e-5 : 1e-5 + 1e-8 * fabs(q->ub));
+ if (s > q->ub + eps) return 1;
+ /* if s[q] is close to u[q], fix column at its upper bound
+ rather than at the implied value */
+ if (s > q->ub - 1e-3 * eps)
+ { q->lb = q->ub;
+ return 0;
+ }
+ }
+ /* fix column at the implied value */
+ q->lb = q->ub = s;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_eq_singlet - process row singleton (equality constraint)
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_eq_singlet(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_eq_singlet processes row p, which is equiality
+* constraint having the only non-zero coefficient:
+*
+* a[p,q] x[q] = b. (1)
+*
+* RETURNS
+*
+* 0 - success;
+*
+* 1 - problem has no primal feasible solution;
+*
+* 2 - problem has no integer feasible solution.
+*
+* PROBLEM TRANSFORMATION
+*
+* The equality constraint defines implied value of column q:
+*
+* x[q] = s[q] = b / a[p,q]. (2)
+*
+* If the implied value s[q] satisfies to the column bounds (see the
+* routine npp_implied_value), the column can be fixed at s[q] and
+* removed from the problem. In this case row p becomes redundant, so
+* it can be replaced by equivalent free row and also removed from the
+* problem.
+*
+* Note that the routine removes from the problem only row p. Column q
+* becomes fixed, however, it is kept in the problem.
+*
+* RECOVERING BASIC SOLUTION
+*
+* In solution to the original problem row p is assigned status GLP_NS
+* (active equality constraint), and column q is assigned status GLP_BS
+* (basic column).
+*
+* Multiplier for row p can be computed as follows. In the dual system
+* of the original problem column q corresponds to the following row:
+*
+* sum a[i,q] pi[i] + lambda[q] = c[q] ==>
+* i
+*
+* sum a[i,q] pi[i] + a[p,q] pi[p] + lambda[q] = c[q].
+* i!=p
+*
+* Therefore:
+*
+* 1
+* pi[p] = ------ (c[q] - lambda[q] - sum a[i,q] pi[i]), (3)
+* a[p,q] i!=q
+*
+* where lambda[q] = 0 (since column[q] is basic), and pi[i] for all
+* i != p are known in solution to the transformed problem.
+*
+* Value of column q in solution to the original problem is assigned
+* its implied value s[q].
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Multiplier for row p is computed with formula (3). Value of column
+* q is assigned its implied value s[q].
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is assigned its implied value s[q]. */
+
+struct eq_singlet
+{ /* row singleton (equality constraint) */
+ int p;
+ /* row reference number */
+ int q;
+ /* column reference number */
+ double apq;
+ /* constraint coefficient a[p,q] */
+ double c;
+ /* objective coefficient at x[q] */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[i,q], i != p */
+};
+
+static int rcv_eq_singlet(NPP *npp, void *info);
+
+int npp_eq_singlet(NPP *npp, NPPROW *p)
+{ /* process row singleton (equality constraint) */
+ struct eq_singlet *info;
+ NPPCOL *q;
+ NPPAIJ *aij;
+ NPPLFE *lfe;
+ int ret;
+ double s;
+ /* the row must be singleton equality constraint */
+ xassert(p->lb == p->ub);
+ xassert(p->ptr != NULL && p->ptr->r_next == NULL);
+ /* compute and process implied column value */
+ aij = p->ptr;
+ q = aij->col;
+ s = p->lb / aij->val;
+ ret = npp_implied_value(npp, q, s);
+ xassert(0 <= ret && ret <= 2);
+ if (ret != 0) return ret;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_eq_singlet, sizeof(struct eq_singlet));
+ info->p = p->i;
+ info->q = q->j;
+ info->apq = aij->val;
+ info->c = q->coef;
+ info->ptr = NULL;
+ /* save column coefficients a[i,q], i != p (not needed for MIP
+ solution) */
+ if (npp->sol != GLP_MIP)
+ { for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { if (aij->row == p) continue; /* skip a[p,q] */
+ lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = aij->row->i;
+ lfe->val = aij->val;
+ lfe->next = info->ptr;
+ info->ptr = lfe;
+ }
+ }
+ /* remove the row from the problem */
+ npp_del_row(npp, p);
+ return 0;
+}
+
+static int rcv_eq_singlet(NPP *npp, void *_info)
+{ /* recover row singleton (equality constraint) */
+ struct eq_singlet *info = _info;
+ NPPLFE *lfe;
+ double temp;
+ if (npp->sol == GLP_SOL)
+ { /* column q must be already recovered as GLP_NS */
+ if (npp->c_stat[info->q] != GLP_NS)
+ { npp_error();
+ return 1;
+ }
+ npp->r_stat[info->p] = GLP_NS;
+ npp->c_stat[info->q] = GLP_BS;
+ }
+ if (npp->sol != GLP_MIP)
+ { /* compute multiplier for row p with formula (3) */
+ temp = info->c;
+ for (lfe = info->ptr; lfe != NULL; lfe = lfe->next)
+ temp -= lfe->val * npp->r_pi[lfe->ref];
+ npp->r_pi[info->p] = temp / info->apq;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_lower - process implied column lower bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_implied_lower(NPP *npp, NPPCOL *q, double l);
+*
+* DESCRIPTION
+*
+* For column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] < u[q], the routine npp_implied_lower processes its
+* implied lower bound l'[q]. As the result the current column lower
+* bound may increase. Note that the column is kept in the problem in
+* any case.
+*
+* RETURNS
+*
+* 0 - current column lower bound has not changed;
+*
+* 1 - current column lower bound has changed, but not significantly;
+*
+* 2 - current column lower bound has significantly changed;
+*
+* 3 - column has been fixed on its upper bound;
+*
+* 4 - implied lower bound violates current column upper bound.
+*
+* ALGORITHM
+*
+* If column q is integral, before processing its implied lower bound
+* should be rounded up:
+*
+* ( floor(l'[q]+0.5), if |l'[q] - floor(l'[q]+0.5)| <= eps
+* l'[q] := < (2)
+* ( ceil(l'[q]), otherwise
+*
+* where floor(l'[q]+0.5) is the nearest integer to l'[q], ceil(l'[q])
+* is smallest integer not less than l'[q], and eps is an absolute
+* tolerance for column value.
+*
+* Processing implied column lower bound l'[q] includes the following
+* cases:
+*
+* 1) if l'[q] < l[q] + eps, implied lower bound is redundant;
+*
+* 2) if l[q] + eps <= l[q] <= u[q] + eps, current column lower bound
+* l[q] can be strengthened by replacing it with l'[q]. If in this
+* case new column lower bound becomes close to current column upper
+* bound u[q], the column can be fixed on its upper bound;
+*
+* 3) if l'[q] > u[q] + eps, implied lower bound violates current
+* column upper bound u[q], in which case the problem has no primal
+* feasible solution. */
+
+int npp_implied_lower(NPP *npp, NPPCOL *q, double l)
+{ /* process implied column lower bound */
+ int ret;
+ double eps, nint;
+ xassert(npp == npp);
+ /* column must not be fixed */
+ xassert(q->lb < q->ub);
+ /* implied lower bound must be finite */
+ xassert(l != -DBL_MAX);
+ /* if column is integral, round up l'[q] */
+ if (q->is_int)
+ { nint = floor(l + 0.5);
+ if (fabs(l - nint) <= 1e-5)
+ l = nint;
+ else
+ l = ceil(l);
+ }
+ /* check current column lower bound */
+ if (q->lb != -DBL_MAX)
+ { eps = (q->is_int ? 1e-3 : 1e-3 + 1e-6 * fabs(q->lb));
+ if (l < q->lb + eps)
+ { ret = 0; /* redundant */
+ goto done;
+ }
+ }
+ /* check current column upper bound */
+ if (q->ub != +DBL_MAX)
+ { eps = (q->is_int ? 1e-5 : 1e-5 + 1e-8 * fabs(q->ub));
+ if (l > q->ub + eps)
+ { ret = 4; /* infeasible */
+ goto done;
+ }
+ /* if l'[q] is close to u[q], fix column at its upper bound */
+ if (l > q->ub - 1e-3 * eps)
+ { q->lb = q->ub;
+ ret = 3; /* fixed */
+ goto done;
+ }
+ }
+ /* check if column lower bound changes significantly */
+ if (q->lb == -DBL_MAX)
+ ret = 2; /* significantly */
+ else if (q->is_int && l > q->lb + 0.5)
+ ret = 2; /* significantly */
+ else if (l > q->lb + 0.30 * (1.0 + fabs(q->lb)))
+ ret = 2; /* significantly */
+ else
+ ret = 1; /* not significantly */
+ /* set new column lower bound */
+ q->lb = l;
+done: return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_upper - process implied column upper bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_implied_upper(NPP *npp, NPPCOL *q, double u);
+*
+* DESCRIPTION
+*
+* For column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] < u[q], the routine npp_implied_upper processes its
+* implied upper bound u'[q]. As the result the current column upper
+* bound may decrease. Note that the column is kept in the problem in
+* any case.
+*
+* RETURNS
+*
+* 0 - current column upper bound has not changed;
+*
+* 1 - current column upper bound has changed, but not significantly;
+*
+* 2 - current column upper bound has significantly changed;
+*
+* 3 - column has been fixed on its lower bound;
+*
+* 4 - implied upper bound violates current column lower bound.
+*
+* ALGORITHM
+*
+* If column q is integral, before processing its implied upper bound
+* should be rounded down:
+*
+* ( floor(u'[q]+0.5), if |u'[q] - floor(l'[q]+0.5)| <= eps
+* u'[q] := < (2)
+* ( floor(l'[q]), otherwise
+*
+* where floor(u'[q]+0.5) is the nearest integer to u'[q],
+* floor(u'[q]) is largest integer not greater than u'[q], and eps is
+* an absolute tolerance for column value.
+*
+* Processing implied column upper bound u'[q] includes the following
+* cases:
+*
+* 1) if u'[q] > u[q] - eps, implied upper bound is redundant;
+*
+* 2) if l[q] - eps <= u[q] <= u[q] - eps, current column upper bound
+* u[q] can be strengthened by replacing it with u'[q]. If in this
+* case new column upper bound becomes close to current column lower
+* bound, the column can be fixed on its lower bound;
+*
+* 3) if u'[q] < l[q] - eps, implied upper bound violates current
+* column lower bound l[q], in which case the problem has no primal
+* feasible solution. */
+
+int npp_implied_upper(NPP *npp, NPPCOL *q, double u)
+{ int ret;
+ double eps, nint;
+ xassert(npp == npp);
+ /* column must not be fixed */
+ xassert(q->lb < q->ub);
+ /* implied upper bound must be finite */
+ xassert(u != +DBL_MAX);
+ /* if column is integral, round down u'[q] */
+ if (q->is_int)
+ { nint = floor(u + 0.5);
+ if (fabs(u - nint) <= 1e-5)
+ u = nint;
+ else
+ u = floor(u);
+ }
+ /* check current column upper bound */
+ if (q->ub != +DBL_MAX)
+ { eps = (q->is_int ? 1e-3 : 1e-3 + 1e-6 * fabs(q->ub));
+ if (u > q->ub - eps)
+ { ret = 0; /* redundant */
+ goto done;
+ }
+ }
+ /* check current column lower bound */
+ if (q->lb != -DBL_MAX)
+ { eps = (q->is_int ? 1e-5 : 1e-5 + 1e-8 * fabs(q->lb));
+ if (u < q->lb - eps)
+ { ret = 4; /* infeasible */
+ goto done;
+ }
+ /* if u'[q] is close to l[q], fix column at its lower bound */
+ if (u < q->lb + 1e-3 * eps)
+ { q->ub = q->lb;
+ ret = 3; /* fixed */
+ goto done;
+ }
+ }
+ /* check if column upper bound changes significantly */
+ if (q->ub == +DBL_MAX)
+ ret = 2; /* significantly */
+ else if (q->is_int && u < q->ub - 0.5)
+ ret = 2; /* significantly */
+ else if (u < q->ub - 0.30 * (1.0 + fabs(q->ub)))
+ ret = 2; /* significantly */
+ else
+ ret = 1; /* not significantly */
+ /* set new column upper bound */
+ q->ub = u;
+done: return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_ineq_singlet - process row singleton (inequality constraint)
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_ineq_singlet(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_ineq_singlet processes row p, which is inequality
+* constraint having the only non-zero coefficient:
+*
+* L[p] <= a[p,q] * x[q] <= U[p], (1)
+*
+* where L[p] < U[p], L[p] > -oo and/or U[p] < +oo.
+*
+* RETURNS
+*
+* 0 - current column bounds have not changed;
+*
+* 1 - current column bounds have changed, but not significantly;
+*
+* 2 - current column bounds have significantly changed;
+*
+* 3 - column has been fixed on its lower or upper bound;
+*
+* 4 - problem has no primal feasible solution.
+*
+* PROBLEM TRANSFORMATION
+*
+* Inequality constraint (1) defines implied bounds of column q:
+*
+* ( L[p] / a[p,q], if a[p,q] > 0
+* l'[q] = < (2)
+* ( U[p] / a[p,q], if a[p,q] < 0
+*
+* ( U[p] / a[p,q], if a[p,q] > 0
+* u'[q] = < (3)
+* ( L[p] / a[p,q], if a[p,q] < 0
+*
+* If these implied bounds do not violate current bounds of column q:
+*
+* l[q] <= x[q] <= u[q], (4)
+*
+* they can be used to strengthen the current column bounds:
+*
+* l[q] := max(l[q], l'[q]), (5)
+*
+* u[q] := min(u[q], u'[q]). (6)
+*
+* (See the routines npp_implied_lower and npp_implied_upper.)
+*
+* Once bounds of row p (1) have been carried over column q, the row
+* becomes redundant, so it can be replaced by equivalent free row and
+* removed from the problem.
+*
+* Note that the routine removes from the problem only row p. Column q,
+* even it has been fixed, is kept in the problem.
+*
+* RECOVERING BASIC SOLUTION
+*
+* Note that the row in the dual system corresponding to column q is
+* the following:
+*
+* sum a[i,q] pi[i] + lambda[q] = c[q] ==>
+* i
+* (7)
+* sum a[i,q] pi[i] + a[p,q] pi[p] + lambda[q] = c[q],
+* i!=p
+*
+* where pi[i] for all i != p are known in solution to the transformed
+* problem. Row p does not exist in the transformed problem, so it has
+* zero multiplier there. This allows computing multiplier for column q
+* in solution to the transformed problem:
+*
+* lambda~[q] = c[q] - sum a[i,q] pi[i]. (8)
+* i!=p
+*
+* Let in solution to the transformed problem column q be non-basic
+* with lower bound active (GLP_NL, lambda~[q] >= 0), and this lower
+* bound be implied one l'[q]. From the original problem's standpoint
+* this then means that actually the original column lower bound l[q]
+* is inactive, and active is that row bound L[p] or U[p] that defines
+* the implied bound l'[q] (2). In this case in solution to the
+* original problem column q is assigned status GLP_BS while row p is
+* assigned status GLP_NL (if a[p,q] > 0) or GLP_NU (if a[p,q] < 0).
+* Since now column q is basic, its multiplier lambda[q] is zero. This
+* allows using (7) and (8) to find multiplier for row p in solution to
+* the original problem:
+*
+* 1
+* pi[p] = ------ (c[q] - sum a[i,q] pi[i]) = lambda~[q] / a[p,q] (9)
+* a[p,q] i!=p
+*
+* Now let in solution to the transformed problem column q be non-basic
+* with upper bound active (GLP_NU, lambda~[q] <= 0), and this upper
+* bound be implied one u'[q]. As in the previous case this then means
+* that from the original problem's standpoint actually the original
+* column upper bound u[q] is inactive, and active is that row bound
+* L[p] or U[p] that defines the implied bound u'[q] (3). In this case
+* in solution to the original problem column q is assigned status
+* GLP_BS, row p is assigned status GLP_NU (if a[p,q] > 0) or GLP_NL
+* (if a[p,q] < 0), and its multiplier is computed with formula (9).
+*
+* Strengthening bounds of column q according to (5) and (6) may make
+* it fixed. Thus, if in solution to the transformed problem column q is
+* non-basic and fixed (GLP_NS), we can suppose that if lambda~[q] > 0,
+* column q has active lower bound (GLP_NL), and if lambda~[q] < 0,
+* column q has active upper bound (GLP_NU), reducing this case to two
+* previous ones. If, however, lambda~[q] is close to zero or
+* corresponding bound of row p does not exist (this may happen if
+* lambda~[q] has wrong sign due to round-off errors, in which case it
+* is expected to be close to zero, since solution is assumed to be dual
+* feasible), column q can be assigned status GLP_BS (basic), and row p
+* can be made active on its existing bound. In the latter case row
+* multiplier pi[p] computed with formula (9) will be also close to
+* zero, and dual feasibility will be kept.
+*
+* In all other cases, namely, if in solution to the transformed
+* problem column q is basic (GLP_BS), or non-basic with original lower
+* bound l[q] active (GLP_NL), or non-basic with original upper bound
+* u[q] active (GLP_NU), constraint (1) is inactive. So in solution to
+* the original problem status of column q remains unchanged, row p is
+* assigned status GLP_BS, and its multiplier pi[p] is assigned zero
+* value.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* First, value of multiplier for column q in solution to the original
+* problem is computed with formula (8). If lambda~[q] > 0 and column q
+* has implied lower bound, or if lambda~[q] < 0 and column q has
+* implied upper bound, this means that from the original problem's
+* standpoint actually row p has corresponding active bound, in which
+* case its multiplier pi[p] is computed with formula (9). In other
+* cases, when the sign of lambda~[q] corresponds to original bound of
+* column q, or when lambda~[q] =~ 0, value of row multiplier pi[p] is
+* assigned zero value.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct ineq_singlet
+{ /* row singleton (inequality constraint) */
+ int p;
+ /* row reference number */
+ int q;
+ /* column reference number */
+ double apq;
+ /* constraint coefficient a[p,q] */
+ double c;
+ /* objective coefficient at x[q] */
+ double lb;
+ /* row lower bound */
+ double ub;
+ /* row upper bound */
+ char lb_changed;
+ /* this flag is set if column lower bound was changed */
+ char ub_changed;
+ /* this flag is set if column upper bound was changed */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[i,q], i != p */
+};
+
+static int rcv_ineq_singlet(NPP *npp, void *info);
+
+int npp_ineq_singlet(NPP *npp, NPPROW *p)
+{ /* process row singleton (inequality constraint) */
+ struct ineq_singlet *info;
+ NPPCOL *q;
+ NPPAIJ *apq, *aij;
+ NPPLFE *lfe;
+ int lb_changed, ub_changed;
+ double ll, uu;
+ /* the row must be singleton inequality constraint */
+ xassert(p->lb != -DBL_MAX || p->ub != +DBL_MAX);
+ xassert(p->lb < p->ub);
+ xassert(p->ptr != NULL && p->ptr->r_next == NULL);
+ /* compute implied column bounds */
+ apq = p->ptr;
+ q = apq->col;
+ xassert(q->lb < q->ub);
+ if (apq->val > 0.0)
+ { ll = (p->lb == -DBL_MAX ? -DBL_MAX : p->lb / apq->val);
+ uu = (p->ub == +DBL_MAX ? +DBL_MAX : p->ub / apq->val);
+ }
+ else
+ { ll = (p->ub == +DBL_MAX ? -DBL_MAX : p->ub / apq->val);
+ uu = (p->lb == -DBL_MAX ? +DBL_MAX : p->lb / apq->val);
+ }
+ /* process implied column lower bound */
+ if (ll == -DBL_MAX)
+ lb_changed = 0;
+ else
+ { lb_changed = npp_implied_lower(npp, q, ll);
+ xassert(0 <= lb_changed && lb_changed <= 4);
+ if (lb_changed == 4) return 4; /* infeasible */
+ }
+ /* process implied column upper bound */
+ if (uu == +DBL_MAX)
+ ub_changed = 0;
+ else if (lb_changed == 3)
+ { /* column was fixed on its upper bound due to l'[q] = u[q] */
+ /* note that L[p] < U[p], so l'[q] = u[q] < u'[q] */
+ ub_changed = 0;
+ }
+ else
+ { ub_changed = npp_implied_upper(npp, q, uu);
+ xassert(0 <= ub_changed && ub_changed <= 4);
+ if (ub_changed == 4) return 4; /* infeasible */
+ }
+ /* if neither lower nor upper column bound was changed, the row
+ is originally redundant and can be replaced by free row */
+ if (!lb_changed && !ub_changed)
+ { p->lb = -DBL_MAX, p->ub = +DBL_MAX;
+ npp_free_row(npp, p);
+ return 0;
+ }
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_ineq_singlet, sizeof(struct ineq_singlet));
+ info->p = p->i;
+ info->q = q->j;
+ info->apq = apq->val;
+ info->c = q->coef;
+ info->lb = p->lb;
+ info->ub = p->ub;
+ info->lb_changed = (char)lb_changed;
+ info->ub_changed = (char)ub_changed;
+ info->ptr = NULL;
+ /* save column coefficients a[i,q], i != p (not needed for MIP
+ solution) */
+ if (npp->sol != GLP_MIP)
+ { for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { if (aij == apq) continue; /* skip a[p,q] */
+ lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = aij->row->i;
+ lfe->val = aij->val;
+ lfe->next = info->ptr;
+ info->ptr = lfe;
+ }
+ }
+ /* remove the row from the problem */
+ npp_del_row(npp, p);
+ return lb_changed >= ub_changed ? lb_changed : ub_changed;
+}
+
+static int rcv_ineq_singlet(NPP *npp, void *_info)
+{ /* recover row singleton (inequality constraint) */
+ struct ineq_singlet *info = _info;
+ NPPLFE *lfe;
+ double lambda;
+ if (npp->sol == GLP_MIP) goto done;
+ /* compute lambda~[q] in solution to the transformed problem
+ with formula (8) */
+ lambda = info->c;
+ for (lfe = info->ptr; lfe != NULL; lfe = lfe->next)
+ lambda -= lfe->val * npp->r_pi[lfe->ref];
+ if (npp->sol == GLP_SOL)
+ { /* recover basic solution */
+ if (npp->c_stat[info->q] == GLP_BS)
+ { /* column q is basic, so row p is inactive */
+ npp->r_stat[info->p] = GLP_BS;
+ npp->r_pi[info->p] = 0.0;
+ }
+ else if (npp->c_stat[info->q] == GLP_NL)
+nl: { /* column q is non-basic with lower bound active */
+ if (info->lb_changed)
+ { /* it is implied bound, so actually row p is active
+ while column q is basic */
+ npp->r_stat[info->p] =
+ (char)(info->apq > 0.0 ? GLP_NL : GLP_NU);
+ npp->c_stat[info->q] = GLP_BS;
+ npp->r_pi[info->p] = lambda / info->apq;
+ }
+ else
+ { /* it is original bound, so row p is inactive */
+ npp->r_stat[info->p] = GLP_BS;
+ npp->r_pi[info->p] = 0.0;
+ }
+ }
+ else if (npp->c_stat[info->q] == GLP_NU)
+nu: { /* column q is non-basic with upper bound active */
+ if (info->ub_changed)
+ { /* it is implied bound, so actually row p is active
+ while column q is basic */
+ npp->r_stat[info->p] =
+ (char)(info->apq > 0.0 ? GLP_NU : GLP_NL);
+ npp->c_stat[info->q] = GLP_BS;
+ npp->r_pi[info->p] = lambda / info->apq;
+ }
+ else
+ { /* it is original bound, so row p is inactive */
+ npp->r_stat[info->p] = GLP_BS;
+ npp->r_pi[info->p] = 0.0;
+ }
+ }
+ else if (npp->c_stat[info->q] == GLP_NS)
+ { /* column q is non-basic and fixed; note, however, that in
+ in the original problem it is non-fixed */
+ if (lambda > +1e-7)
+ { if (info->apq > 0.0 && info->lb != -DBL_MAX ||
+ info->apq < 0.0 && info->ub != +DBL_MAX ||
+ !info->lb_changed)
+ { /* either corresponding bound of row p exists or
+ column q remains non-basic with its original lower
+ bound active */
+ npp->c_stat[info->q] = GLP_NL;
+ goto nl;
+ }
+ }
+ if (lambda < -1e-7)
+ { if (info->apq > 0.0 && info->ub != +DBL_MAX ||
+ info->apq < 0.0 && info->lb != -DBL_MAX ||
+ !info->ub_changed)
+ { /* either corresponding bound of row p exists or
+ column q remains non-basic with its original upper
+ bound active */
+ npp->c_stat[info->q] = GLP_NU;
+ goto nu;
+ }
+ }
+ /* either lambda~[q] is close to zero, or corresponding
+ bound of row p does not exist, because lambda~[q] has
+ wrong sign due to round-off errors; in the latter case
+ lambda~[q] is also assumed to be close to zero; so, we
+ can make row p active on its existing bound and column q
+ basic; pi[p] will have wrong sign, but it also will be
+ close to zero (rarus casus of dual degeneracy) */
+ if (info->lb != -DBL_MAX && info->ub == +DBL_MAX)
+ { /* row lower bound exists, but upper bound doesn't */
+ npp->r_stat[info->p] = GLP_NL;
+ }
+ else if (info->lb == -DBL_MAX && info->ub != +DBL_MAX)
+ { /* row upper bound exists, but lower bound doesn't */
+ npp->r_stat[info->p] = GLP_NU;
+ }
+ else if (info->lb != -DBL_MAX && info->ub != +DBL_MAX)
+ { /* both row lower and upper bounds exist */
+ /* to choose proper active row bound we should not use
+ lambda~[q], because its value being close to zero is
+ unreliable; so we choose that bound which provides
+ primal feasibility for original constraint (1) */
+ if (info->apq * npp->c_value[info->q] <=
+ 0.5 * (info->lb + info->ub))
+ npp->r_stat[info->p] = GLP_NL;
+ else
+ npp->r_stat[info->p] = GLP_NU;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ npp->c_stat[info->q] = GLP_BS;
+ npp->r_pi[info->p] = lambda / info->apq;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ if (npp->sol == GLP_IPT)
+ { /* recover interior-point solution */
+ if (lambda > +DBL_EPSILON && info->lb_changed ||
+ lambda < -DBL_EPSILON && info->ub_changed)
+ { /* actually row p has corresponding active bound */
+ npp->r_pi[info->p] = lambda / info->apq;
+ }
+ else
+ { /* either bounds of column q are both inactive or its
+ original bound is active */
+ npp->r_pi[info->p] = 0.0;
+ }
+ }
+done: return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_slack - process column singleton (implied slack variable)
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_implied_slack(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_implied_slack processes column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] < u[q], having the only non-zero coefficient in row p,
+* which is equality constraint:
+*
+* sum a[p,j] x[j] + a[p,q] x[q] = b. (2)
+* j!=q
+*
+* PROBLEM TRANSFORMATION
+*
+* (If x[q] is integral, this transformation must not be used.)
+*
+* The term a[p,q] x[q] in constraint (2) can be considered as a slack
+* variable that allows to carry bounds of column q over row p and then
+* remove column q from the problem.
+*
+* Constraint (2) can be written as follows:
+*
+* sum a[p,j] x[j] = b - a[p,q] x[q]. (3)
+* j!=q
+*
+* According to (1) constraint (3) is equivalent to the following
+* inequality constraint:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p], (4)
+* j!=q
+*
+* where
+*
+* ( b - a[p,q] u[q], if a[p,q] > 0
+* L[p] = < (5)
+* ( b - a[p,q] l[q], if a[p,q] < 0
+*
+* ( b - a[p,q] l[q], if a[p,q] > 0
+* U[p] = < (6)
+* ( b - a[p,q] u[q], if a[p,q] < 0
+*
+* From (2) it follows that:
+*
+* 1
+* x[q] = ------ (b - sum a[p,j] x[j]). (7)
+* a[p,q] j!=q
+*
+* In order to eliminate x[q] from the objective row we substitute it
+* from (6) to that row:
+*
+* z = sum c[j] x[j] + c[q] x[q] + c[0] =
+* j!=q
+* 1
+* = sum c[j] x[j] + c[q] [------ (b - sum a[p,j] x[j])] + c0 =
+* j!=q a[p,q] j!=q
+*
+* = sum c~[j] x[j] + c~[0],
+* j!=q
+* a[p,j] b
+* c~[j] = c[j] - c[q] ------, c~0 = c0 - c[q] ------ (8)
+* a[p,q] a[p,q]
+*
+* are values of objective coefficients and constant term, resp., in
+* the transformed problem.
+*
+* Note that column q is column singleton, so in the dual system of the
+* original problem it corresponds to the following row singleton:
+*
+* a[p,q] pi[p] + lambda[q] = c[q]. (9)
+*
+* In the transformed problem row (9) would be the following:
+*
+* a[p,q] pi~[p] + lambda[q] = c~[q] = 0. (10)
+*
+* Subtracting (10) from (9) we have:
+*
+* a[p,q] (pi[p] - pi~[p]) = c[q]
+*
+* that gives the following formula to compute multiplier for row p in
+* solution to the original problem using its value in solution to the
+* transformed problem:
+*
+* pi[p] = pi~[p] + c[q] / a[p,q]. (11)
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of column q in solution to the original problem is defined
+* by status of row p in solution to the transformed problem and the
+* sign of coefficient a[p,q] in the original inequality constraint (2)
+* as follows:
+*
+* +-----------------------+---------+--------------------+
+* | Status of row p | Sign of | Status of column q |
+* | (transformed problem) | a[p,q] | (original problem) |
+* +-----------------------+---------+--------------------+
+* | GLP_BS | + / - | GLP_BS |
+* | GLP_NL | + | GLP_NU |
+* | GLP_NL | - | GLP_NL |
+* | GLP_NU | + | GLP_NL |
+* | GLP_NU | - | GLP_NU |
+* | GLP_NF | + / - | GLP_NF |
+* +-----------------------+---------+--------------------+
+*
+* Value of column q is computed with formula (7). Since originally row
+* p is equality constraint, its status is assigned GLP_NS, and value of
+* its multiplier pi[p] is computed with formula (11).
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of column q is computed with formula (7). Row multiplier value
+* pi[p] is computed with formula (11).
+*
+* RECOVERING MIP SOLUTION
+*
+* Value of column q is computed with formula (7). */
+
+struct implied_slack
+{ /* column singleton (implied slack variable) */
+ int p;
+ /* row reference number */
+ int q;
+ /* column reference number */
+ double apq;
+ /* constraint coefficient a[p,q] */
+ double b;
+ /* right-hand side of original equality constraint */
+ double c;
+ /* original objective coefficient at x[q] */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[p,j], j != q */
+};
+
+static int rcv_implied_slack(NPP *npp, void *info);
+
+void npp_implied_slack(NPP *npp, NPPCOL *q)
+{ /* process column singleton (implied slack variable) */
+ struct implied_slack *info;
+ NPPROW *p;
+ NPPAIJ *aij;
+ NPPLFE *lfe;
+ /* the column must be non-integral non-fixed singleton */
+ xassert(!q->is_int);
+ xassert(q->lb < q->ub);
+ xassert(q->ptr != NULL && q->ptr->c_next == NULL);
+ /* corresponding row must be equality constraint */
+ aij = q->ptr;
+ p = aij->row;
+ xassert(p->lb == p->ub);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_implied_slack, sizeof(struct implied_slack));
+ info->p = p->i;
+ info->q = q->j;
+ info->apq = aij->val;
+ info->b = p->lb;
+ info->c = q->coef;
+ info->ptr = NULL;
+ /* save row coefficients a[p,j], j != q, and substitute x[q]
+ into the objective row */
+ for (aij = p->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->col == q) continue; /* skip a[p,q] */
+ lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = aij->col->j;
+ lfe->val = aij->val;
+ lfe->next = info->ptr;
+ info->ptr = lfe;
+ aij->col->coef -= info->c * (aij->val / info->apq);
+ }
+ npp->c0 += info->c * (info->b / info->apq);
+ /* compute new row bounds */
+ if (info->apq > 0.0)
+ { p->lb = (q->ub == +DBL_MAX ?
+ -DBL_MAX : info->b - info->apq * q->ub);
+ p->ub = (q->lb == -DBL_MAX ?
+ +DBL_MAX : info->b - info->apq * q->lb);
+ }
+ else
+ { p->lb = (q->lb == -DBL_MAX ?
+ -DBL_MAX : info->b - info->apq * q->lb);
+ p->ub = (q->ub == +DBL_MAX ?
+ +DBL_MAX : info->b - info->apq * q->ub);
+ }
+ /* remove the column from the problem */
+ npp_del_col(npp, q);
+ return;
+}
+
+static int rcv_implied_slack(NPP *npp, void *_info)
+{ /* recover column singleton (implied slack variable) */
+ struct implied_slack *info = _info;
+ NPPLFE *lfe;
+ double temp;
+ if (npp->sol == GLP_SOL)
+ { /* assign statuses to row p and column q */
+ if (npp->r_stat[info->p] == GLP_BS ||
+ npp->r_stat[info->p] == GLP_NF)
+ npp->c_stat[info->q] = npp->r_stat[info->p];
+ else if (npp->r_stat[info->p] == GLP_NL)
+ npp->c_stat[info->q] =
+ (char)(info->apq > 0.0 ? GLP_NU : GLP_NL);
+ else if (npp->r_stat[info->p] == GLP_NU)
+ npp->c_stat[info->q] =
+ (char)(info->apq > 0.0 ? GLP_NL : GLP_NU);
+ else
+ { npp_error();
+ return 1;
+ }
+ npp->r_stat[info->p] = GLP_NS;
+ }
+ if (npp->sol != GLP_MIP)
+ { /* compute multiplier for row p */
+ npp->r_pi[info->p] += info->c / info->apq;
+ }
+ /* compute value of column q */
+ temp = info->b;
+ for (lfe = info->ptr; lfe != NULL; lfe = lfe->next)
+ temp -= lfe->val * npp->c_value[lfe->ref];
+ npp->c_value[info->q] = temp / info->apq;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_free - process column singleton (implied free variable)
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_implied_free(NPP *npp, NPPCOL *q);
+*
+* DESCRIPTION
+*
+* The routine npp_implied_free processes column q:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* having non-zero coefficient in the only row p, which is inequality
+* constraint:
+*
+* L[p] <= sum a[p,j] x[j] + a[p,q] x[q] <= U[p], (2)
+* j!=q
+*
+* where l[q] < u[q], L[p] < U[p], L[p] > -oo and/or U[p] < +oo.
+*
+* RETURNS
+*
+* 0 - success;
+*
+* 1 - column lower and/or upper bound(s) can be active;
+*
+* 2 - problem has no dual feasible solution.
+*
+* PROBLEM TRANSFORMATION
+*
+* Constraint (2) can be written as follows:
+*
+* L[p] - sum a[p,j] x[j] <= a[p,q] x[q] <= U[p] - sum a[p,j] x[j],
+* j!=q j!=q
+*
+* from which it follows that:
+*
+* alfa <= a[p,q] x[q] <= beta, (3)
+*
+* where
+*
+* alfa = inf(L[p] - sum a[p,j] x[j]) =
+* j!=q
+*
+* = L[p] - sup sum a[p,j] x[j] = (4)
+* j!=q
+*
+* = L[p] - sum a[p,j] u[j] - sum a[p,j] l[j],
+* j in Jp j in Jn
+*
+* beta = sup(L[p] - sum a[p,j] x[j]) =
+* j!=q
+*
+* = L[p] - inf sum a[p,j] x[j] = (5)
+* j!=q
+*
+* = L[p] - sum a[p,j] l[j] - sum a[p,j] u[j],
+* j in Jp j in Jn
+*
+* Jp = {j != q: a[p,j] > 0}, Jn = {j != q: a[p,j] < 0}. (6)
+*
+* Inequality (3) defines implied bounds of variable x[q]:
+*
+* l'[q] <= x[q] <= u'[q], (7)
+*
+* where
+*
+* ( alfa / a[p,q], if a[p,q] > 0
+* l'[q] = < (8a)
+* ( beta / a[p,q], if a[p,q] < 0
+*
+* ( beta / a[p,q], if a[p,q] > 0
+* u'[q] = < (8b)
+* ( alfa / a[p,q], if a[p,q] < 0
+*
+* Thus, if l'[q] > l[q] - eps and u'[q] < u[q] + eps, where eps is
+* an absolute tolerance for column value, column bounds (1) cannot be
+* active, in which case column q can be replaced by equivalent free
+* (unbounded) column.
+*
+* Note that column q is column singleton, so in the dual system of the
+* original problem it corresponds to the following row singleton:
+*
+* a[p,q] pi[p] + lambda[q] = c[q], (9)
+*
+* from which it follows that:
+*
+* pi[p] = (c[q] - lambda[q]) / a[p,q]. (10)
+*
+* Let x[q] be implied free (unbounded) variable. Then column q can be
+* only basic, so its multiplier lambda[q] is equal to zero, and from
+* (10) we have:
+*
+* pi[p] = c[q] / a[p,q]. (11)
+*
+* There are possible three cases:
+*
+* 1) pi[p] < -eps, where eps is an absolute tolerance for row
+* multiplier. In this case, to provide dual feasibility of the
+* original problem, row p must be active on its lower bound, and
+* if its lower bound does not exist (L[p] = -oo), the problem has
+* no dual feasible solution;
+*
+* 2) pi[p] > +eps. In this case row p must be active on its upper
+* bound, and if its upper bound does not exist (U[p] = +oo), the
+* problem has no dual feasible solution;
+*
+* 3) -eps <= pi[p] <= +eps. In this case any (either lower or upper)
+* bound of row p can be active, because this does not affect dual
+* feasibility.
+*
+* Thus, in all three cases original inequality constraint (2) can be
+* replaced by equality constraint, where the right-hand side is either
+* lower or upper bound of row p, and bounds of column q can be removed
+* that makes it free (unbounded). (May note that this transformation
+* can be followed by transformation "Column singleton (implied slack
+* variable)" performed by the routine npp_implied_slack.)
+*
+* RECOVERING BASIC SOLUTION
+*
+* Status of row p in solution to the original problem is determined
+* by its status in solution to the transformed problem and its bound,
+* which was choosen to be active:
+*
+* +-----------------------+--------+--------------------+
+* | Status of row p | Active | Status of row p |
+* | (transformed problem) | bound | (original problem) |
+* +-----------------------+--------+--------------------+
+* | GLP_BS | L[p] | GLP_BS |
+* | GLP_BS | U[p] | GLP_BS |
+* | GLP_NS | L[p] | GLP_NL |
+* | GLP_NS | U[p] | GLP_NU |
+* +-----------------------+--------+--------------------+
+*
+* Value of row multiplier pi[p] (as well as value of column q) in
+* solution to the original problem is the same as in solution to the
+* transformed problem.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of row multiplier pi[p] in solution to the original problem is
+* the same as in solution to the transformed problem.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct implied_free
+{ /* column singleton (implied free variable) */
+ int p;
+ /* row reference number */
+ char stat;
+ /* row status:
+ GLP_NL - active constraint on lower bound
+ GLP_NU - active constraint on upper bound */
+};
+
+static int rcv_implied_free(NPP *npp, void *info);
+
+int npp_implied_free(NPP *npp, NPPCOL *q)
+{ /* process column singleton (implied free variable) */
+ struct implied_free *info;
+ NPPROW *p;
+ NPPAIJ *apq, *aij;
+ double alfa, beta, l, u, pi, eps;
+ /* the column must be non-fixed singleton */
+ xassert(q->lb < q->ub);
+ xassert(q->ptr != NULL && q->ptr->c_next == NULL);
+ /* corresponding row must be inequality constraint */
+ apq = q->ptr;
+ p = apq->row;
+ xassert(p->lb != -DBL_MAX || p->ub != +DBL_MAX);
+ xassert(p->lb < p->ub);
+ /* compute alfa */
+ alfa = p->lb;
+ if (alfa != -DBL_MAX)
+ { for (aij = p->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij == apq) continue; /* skip a[p,q] */
+ if (aij->val > 0.0)
+ { if (aij->col->ub == +DBL_MAX)
+ { alfa = -DBL_MAX;
+ break;
+ }
+ alfa -= aij->val * aij->col->ub;
+ }
+ else /* < 0.0 */
+ { if (aij->col->lb == -DBL_MAX)
+ { alfa = -DBL_MAX;
+ break;
+ }
+ alfa -= aij->val * aij->col->lb;
+ }
+ }
+ }
+ /* compute beta */
+ beta = p->ub;
+ if (beta != +DBL_MAX)
+ { for (aij = p->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij == apq) continue; /* skip a[p,q] */
+ if (aij->val > 0.0)
+ { if (aij->col->lb == -DBL_MAX)
+ { beta = +DBL_MAX;
+ break;
+ }
+ beta -= aij->val * aij->col->lb;
+ }
+ else /* < 0.0 */
+ { if (aij->col->ub == +DBL_MAX)
+ { beta = +DBL_MAX;
+ break;
+ }
+ beta -= aij->val * aij->col->ub;
+ }
+ }
+ }
+ /* compute implied column lower bound l'[q] */
+ if (apq->val > 0.0)
+ l = (alfa == -DBL_MAX ? -DBL_MAX : alfa / apq->val);
+ else /* < 0.0 */
+ l = (beta == +DBL_MAX ? -DBL_MAX : beta / apq->val);
+ /* compute implied column upper bound u'[q] */
+ if (apq->val > 0.0)
+ u = (beta == +DBL_MAX ? +DBL_MAX : beta / apq->val);
+ else
+ u = (alfa == -DBL_MAX ? +DBL_MAX : alfa / apq->val);
+ /* check if column lower bound l[q] can be active */
+ if (q->lb != -DBL_MAX)
+ { eps = 1e-9 + 1e-12 * fabs(q->lb);
+ if (l < q->lb - eps) return 1; /* yes, it can */
+ }
+ /* check if column upper bound u[q] can be active */
+ if (q->ub != +DBL_MAX)
+ { eps = 1e-9 + 1e-12 * fabs(q->ub);
+ if (u > q->ub + eps) return 1; /* yes, it can */
+ }
+ /* okay; make column q free (unbounded) */
+ q->lb = -DBL_MAX, q->ub = +DBL_MAX;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_implied_free, sizeof(struct implied_free));
+ info->p = p->i;
+ info->stat = -1;
+ /* compute row multiplier pi[p] */
+ pi = q->coef / apq->val;
+ /* check dual feasibility for row p */
+ if (pi > +DBL_EPSILON)
+ { /* lower bound L[p] must be active */
+ if (p->lb != -DBL_MAX)
+nl: { info->stat = GLP_NL;
+ p->ub = p->lb;
+ }
+ else
+ { if (pi > +1e-5) return 2; /* dual infeasibility */
+ /* take a chance on U[p] */
+ xassert(p->ub != +DBL_MAX);
+ goto nu;
+ }
+ }
+ else if (pi < -DBL_EPSILON)
+ { /* upper bound U[p] must be active */
+ if (p->ub != +DBL_MAX)
+nu: { info->stat = GLP_NU;
+ p->lb = p->ub;
+ }
+ else
+ { if (pi < -1e-5) return 2; /* dual infeasibility */
+ /* take a chance on L[p] */
+ xassert(p->lb != -DBL_MAX);
+ goto nl;
+ }
+ }
+ else
+ { /* any bound (either L[p] or U[p]) can be made active */
+ if (p->ub == +DBL_MAX)
+ { xassert(p->lb != -DBL_MAX);
+ goto nl;
+ }
+ if (p->lb == -DBL_MAX)
+ { xassert(p->ub != +DBL_MAX);
+ goto nu;
+ }
+ if (fabs(p->lb) <= fabs(p->ub)) goto nl; else goto nu;
+ }
+ return 0;
+}
+
+static int rcv_implied_free(NPP *npp, void *_info)
+{ /* recover column singleton (implied free variable) */
+ struct implied_free *info = _info;
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] == GLP_BS)
+ npp->r_stat[info->p] = GLP_BS;
+ else if (npp->r_stat[info->p] == GLP_NS)
+ { xassert(info->stat == GLP_NL || info->stat == GLP_NU);
+ npp->r_stat[info->p] = info->stat;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_eq_doublet - process row doubleton (equality constraint)
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* NPPCOL *npp_eq_doublet(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_eq_doublet processes row p, which is equality
+* constraint having exactly two non-zero coefficients:
+*
+* a[p,q] x[q] + a[p,r] x[r] = b. (1)
+*
+* As the result of processing one of columns q or r is eliminated from
+* all other rows and, thus, becomes column singleton of type "implied
+* slack variable". Row p is not changed and along with column q and r
+* remains in the problem.
+*
+* RETURNS
+*
+* The routine npp_eq_doublet returns pointer to the descriptor of that
+* column q or r which has been eliminated. If, due to some reason, the
+* elimination was not performed, the routine returns NULL.
+*
+* PROBLEM TRANSFORMATION
+*
+* First, we decide which column q or r will be eliminated. Let it be
+* column q. Consider i-th constraint row, where column q has non-zero
+* coefficient a[i,q] != 0:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i]. (2)
+* j
+*
+* In order to eliminate column q from row (2) we subtract from it row
+* (1) multiplied by gamma[i] = a[i,q] / a[p,q], i.e. we replace in the
+* transformed problem row (2) by its linear combination with row (1).
+* This transformation changes only coefficients in columns q and r,
+* and bounds of row i as follows:
+*
+* a~[i,q] = a[i,q] - gamma[i] a[p,q] = 0, (3)
+*
+* a~[i,r] = a[i,r] - gamma[i] a[p,r], (4)
+*
+* L~[i] = L[i] - gamma[i] b, (5)
+*
+* U~[i] = U[i] - gamma[i] b. (6)
+*
+* RECOVERING BASIC SOLUTION
+*
+* The transformation of the primal system of the original problem:
+*
+* L <= A x <= U (7)
+*
+* is equivalent to multiplying from the left a transformation matrix F
+* by components of this primal system, which in the transformed problem
+* becomes the following:
+*
+* F L <= F A x <= F U ==> L~ <= A~x <= U~. (8)
+*
+* The matrix F has the following structure:
+*
+* ( 1 -gamma[1] )
+* ( )
+* ( 1 -gamma[2] )
+* ( )
+* ( ... ... )
+* ( )
+* F = ( 1 -gamma[p-1] ) (9)
+* ( )
+* ( 1 )
+* ( )
+* ( -gamma[p+1] 1 )
+* ( )
+* ( ... ... )
+*
+* where its column containing elements -gamma[i] corresponds to row p
+* of the primal system.
+*
+* From (8) it follows that the dual system of the original problem:
+*
+* A'pi + lambda = c, (10)
+*
+* in the transformed problem becomes the following:
+*
+* A'F'inv(F')pi + lambda = c ==> (A~)'pi~ + lambda = c, (11)
+*
+* where:
+*
+* pi~ = inv(F')pi (12)
+*
+* is the vector of row multipliers in the transformed problem. Thus:
+*
+* pi = F'pi~. (13)
+*
+* Therefore, as it follows from (13), value of multiplier for row p in
+* solution to the original problem can be computed as follows:
+*
+* pi[p] = pi~[p] - sum gamma[i] pi~[i], (14)
+* i
+*
+* where pi~[i] = pi[i] is multiplier for row i (i != p).
+*
+* Note that the statuses of all rows and columns are not changed.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Multiplier for row p in solution to the original problem is computed
+* with formula (14).
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct eq_doublet
+{ /* row doubleton (equality constraint) */
+ int p;
+ /* row reference number */
+ double apq;
+ /* constraint coefficient a[p,q] */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[i,q], i != p */
+};
+
+static int rcv_eq_doublet(NPP *npp, void *info);
+
+NPPCOL *npp_eq_doublet(NPP *npp, NPPROW *p)
+{ /* process row doubleton (equality constraint) */
+ struct eq_doublet *info;
+ NPPROW *i;
+ NPPCOL *q, *r;
+ NPPAIJ *apq, *apr, *aiq, *air, *next;
+ NPPLFE *lfe;
+ double gamma;
+ /* the row must be doubleton equality constraint */
+ xassert(p->lb == p->ub);
+ xassert(p->ptr != NULL && p->ptr->r_next != NULL &&
+ p->ptr->r_next->r_next == NULL);
+ /* choose column to be eliminated */
+ { NPPAIJ *a1, *a2;
+ a1 = p->ptr, a2 = a1->r_next;
+ if (fabs(a2->val) < 0.001 * fabs(a1->val))
+ { /* only first column can be eliminated, because second one
+ has too small constraint coefficient */
+ apq = a1, apr = a2;
+ }
+ else if (fabs(a1->val) < 0.001 * fabs(a2->val))
+ { /* only second column can be eliminated, because first one
+ has too small constraint coefficient */
+ apq = a2, apr = a1;
+ }
+ else
+ { /* both columns are appropriate; choose that one which is
+ shorter to minimize fill-in */
+ if (npp_col_nnz(npp, a1->col) <= npp_col_nnz(npp, a2->col))
+ { /* first column is shorter */
+ apq = a1, apr = a2;
+ }
+ else
+ { /* second column is shorter */
+ apq = a2, apr = a1;
+ }
+ }
+ }
+ /* now columns q and r have been chosen */
+ q = apq->col, r = apr->col;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_eq_doublet, sizeof(struct eq_doublet));
+ info->p = p->i;
+ info->apq = apq->val;
+ info->ptr = NULL;
+ /* transform each row i (i != p), where a[i,q] != 0, to eliminate
+ column q */
+ for (aiq = q->ptr; aiq != NULL; aiq = next)
+ { next = aiq->c_next;
+ if (aiq == apq) continue; /* skip row p */
+ i = aiq->row; /* row i to be transformed */
+ /* save constraint coefficient a[i,q] */
+ if (npp->sol != GLP_MIP)
+ { lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = i->i;
+ lfe->val = aiq->val;
+ lfe->next = info->ptr;
+ info->ptr = lfe;
+ }
+ /* find coefficient a[i,r] in row i */
+ for (air = i->ptr; air != NULL; air = air->r_next)
+ if (air->col == r) break;
+ /* if a[i,r] does not exist, create a[i,r] = 0 */
+ if (air == NULL)
+ air = npp_add_aij(npp, i, r, 0.0);
+ /* compute gamma[i] = a[i,q] / a[p,q] */
+ gamma = aiq->val / apq->val;
+ /* (row i) := (row i) - gamma[i] * (row p); see (3)-(6) */
+ /* new a[i,q] is exact zero due to elimnation; remove it from
+ row i */
+ npp_del_aij(npp, aiq);
+ /* compute new a[i,r] */
+ air->val -= gamma * apr->val;
+ /* if new a[i,r] is close to zero due to numeric cancelation,
+ remove it from row i */
+ if (fabs(air->val) <= 1e-10)
+ npp_del_aij(npp, air);
+ /* compute new lower and upper bounds of row i */
+ if (i->lb == i->ub)
+ i->lb = i->ub = (i->lb - gamma * p->lb);
+ else
+ { if (i->lb != -DBL_MAX)
+ i->lb -= gamma * p->lb;
+ if (i->ub != +DBL_MAX)
+ i->ub -= gamma * p->lb;
+ }
+ }
+ return q;
+}
+
+static int rcv_eq_doublet(NPP *npp, void *_info)
+{ /* recover row doubleton (equality constraint) */
+ struct eq_doublet *info = _info;
+ NPPLFE *lfe;
+ double gamma, temp;
+ /* we assume that processing row p is followed by processing
+ column q as singleton of type "implied slack variable", in
+ which case row p must always be active equality constraint */
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] != GLP_NS)
+ { npp_error();
+ return 1;
+ }
+ }
+ if (npp->sol != GLP_MIP)
+ { /* compute value of multiplier for row p; see (14) */
+ temp = npp->r_pi[info->p];
+ for (lfe = info->ptr; lfe != NULL; lfe = lfe->next)
+ { gamma = lfe->val / info->apq; /* a[i,q] / a[p,q] */
+ temp -= gamma * npp->r_pi[lfe->ref];
+ }
+ npp->r_pi[info->p] = temp;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_forcing_row - process forcing row
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_forcing_row(NPP *npp, NPPROW *p, int at);
+*
+* DESCRIPTION
+*
+* The routine npp_forcing row processes row p of general format:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p], (1)
+* j
+*
+* l[j] <= x[j] <= u[j], (2)
+*
+* where L[p] <= U[p] and l[j] < u[j] for all a[p,j] != 0. It is also
+* assumed that:
+*
+* 1) if at = 0 then |L[p] - U'[p]| <= eps, where U'[p] is implied
+* row upper bound (see below), eps is an absolute tolerance for row
+* value;
+*
+* 2) if at = 1 then |U[p] - L'[p]| <= eps, where L'[p] is implied
+* row lower bound (see below).
+*
+* RETURNS
+*
+* 0 - success;
+*
+* 1 - cannot fix columns due to too small constraint coefficients.
+*
+* PROBLEM TRANSFORMATION
+*
+* Implied lower and upper bounds of row (1) are determined by bounds
+* of corresponding columns (variables) as follows:
+*
+* L'[p] = inf sum a[p,j] x[j] =
+* j
+* (3)
+* = sum a[p,j] l[j] + sum a[p,j] u[j],
+* j in Jp j in Jn
+*
+* U'[p] = sup sum a[p,j] x[j] =
+* (4)
+* = sum a[p,j] u[j] + sum a[p,j] l[j],
+* j in Jp j in Jn
+*
+* Jp = {j: a[p,j] > 0}, Jn = {j: a[p,j] < 0}. (5)
+*
+* If L[p] =~ U'[p] (at = 0), solution can be primal feasible only when
+* all variables take their boundary values as defined by (4):
+*
+* ( u[j], if j in Jp
+* x[j] = < (6)
+* ( l[j], if j in Jn
+*
+* Similarly, if U[p] =~ L'[p] (at = 1), solution can be primal feasible
+* only when all variables take their boundary values as defined by (3):
+*
+* ( l[j], if j in Jp
+* x[j] = < (7)
+* ( u[j], if j in Jn
+*
+* Condition (6) or (7) allows fixing all columns (variables x[j])
+* in row (1) on their bounds and then removing them from the problem
+* (see the routine npp_fixed_col). Due to this row p becomes redundant,
+* so it can be replaced by equivalent free (unbounded) row and also
+* removed from the problem (see the routine npp_free_row).
+*
+* 1. To apply this transformation row (1) should not have coefficients
+* whose magnitude is too small, i.e. all a[p,j] should satisfy to
+* the following condition:
+*
+* |a[p,j]| >= eps * max(1, |a[p,k]|), (8)
+* k
+* where eps is a relative tolerance for constraint coefficients.
+* Otherwise, fixing columns may be numerically unreliable and may
+* lead to wrong solution.
+*
+* 2. The routine fixes columns and remove bounds of row p, however,
+* it does not remove the row and columns from the problem.
+*
+* RECOVERING BASIC SOLUTION
+*
+* In the transformed problem row p being inactive constraint is
+* assigned status GLP_BS (as the result of transformation of free
+* row), and all columns in this row are assigned status GLP_NS (as the
+* result of transformation of fixed columns).
+*
+* Note that in the dual system of the transformed (as well as original)
+* problem every column j in row p corresponds to the following row:
+*
+* sum a[i,j] pi[i] + a[p,j] pi[p] + lambda[j] = c[j], (9)
+* i!=p
+*
+* from which it follows that:
+*
+* lambda[j] = c[j] - sum a[i,j] pi[i] - a[p,j] pi[p]. (10)
+* i!=p
+*
+* In the transformed problem values of all multipliers pi[i] are known
+* (including pi[i], whose value is zero, since row p is inactive).
+* Thus, using formula (10) it is possible to compute values of
+* multipliers lambda[j] for all columns in row p.
+*
+* Note also that in the original problem all columns in row p are
+* bounded, not fixed. So status GLP_NS assigned to every such column
+* must be changed to GLP_NL or GLP_NU depending on which bound the
+* corresponding column has been fixed. This status change may lead to
+* dual feasibility violation for solution of the original problem,
+* because now column multipliers must satisfy to the following
+* condition:
+*
+* ( >= 0, if status of column j is GLP_NL,
+* lambda[j] < (11)
+* ( <= 0, if status of column j is GLP_NU.
+*
+* If this condition holds, solution to the original problem is the
+* same as to the transformed problem. Otherwise, we have to perform
+* one degenerate pivoting step of the primal simplex method to obtain
+* dual feasible (hence, optimal) solution to the original problem as
+* follows. If, on problem transformation, row p was made active on its
+* lower bound (case at = 0), we change its status to GLP_NL (or GLP_NS)
+* and start increasing its multiplier pi[p]. Otherwise, if row p was
+* made active on its upper bound (case at = 1), we change its status
+* to GLP_NU (or GLP_NS) and start decreasing pi[p]. From (10) it
+* follows that:
+*
+* delta lambda[j] = - a[p,j] * delta pi[p] = - a[p,j] pi[p]. (12)
+*
+* Simple analysis of formulae (3)-(5) shows that changing pi[p] in the
+* specified direction causes increasing lambda[j] for every column j
+* assigned status GLP_NL (delta lambda[j] > 0) and decreasing lambda[j]
+* for every column j assigned status GLP_NU (delta lambda[j] < 0). It
+* is understood that once the last lambda[q], which violates condition
+* (11), has reached zero, multipliers lambda[j] for all columns get
+* valid signs. Such column q can be determined as follows. Let d[j] be
+* initial value of lambda[j] (i.e. reduced cost of column j) in the
+* transformed problem computed with formula (10) when pi[p] = 0. Then
+* lambda[j] = d[j] + delta lambda[j], and from (12) it follows that
+* lambda[j] becomes zero if:
+*
+* delta lambda[j] = - a[p,j] pi[p] = - d[j] ==>
+* (13)
+* pi[p] = d[j] / a[p,j].
+*
+* Therefore, the last column q, for which lambda[q] becomes zero, can
+* be determined from the following condition:
+*
+* |d[q] / a[p,q]| = max |pi[p]| = max |d[j] / a[p,j]|, (14)
+* j in D j in D
+*
+* where D is a set of columns j whose, reduced costs d[j] have invalid
+* signs, i.e. violate condition (11). (Thus, if D is empty, solution
+* to the original problem is the same as solution to the transformed
+* problem, and no correction is needed as was noticed above.) In
+* solution to the original problem column q is assigned status GLP_BS,
+* since it replaces column of auxiliary variable of row p (becoming
+* active) in the basis, and multiplier for row p is assigned its new
+* value, which is pi[p] = d[q] / a[p,q]. Note that due to primal
+* degeneracy values of all columns having non-zero coefficients in row
+* p remain unchanged.
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* Value of multiplier pi[p] in solution to the original problem is
+* corrected in the same way as for basic solution. Values of all
+* columns having non-zero coefficients in row p remain unchanged.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct forcing_col
+{ /* column fixed on its bound by forcing row */
+ int j;
+ /* column reference number */
+ char stat;
+ /* original column status:
+ GLP_NL - fixed on lower bound
+ GLP_NU - fixed on upper bound */
+ double a;
+ /* constraint coefficient a[p,j] */
+ double c;
+ /* objective coefficient c[j] */
+ NPPLFE *ptr;
+ /* list of non-zero coefficients a[i,j], i != p */
+ struct forcing_col *next;
+ /* pointer to another column fixed by forcing row */
+};
+
+struct forcing_row
+{ /* forcing row */
+ int p;
+ /* row reference number */
+ char stat;
+ /* status assigned to the row if it becomes active:
+ GLP_NS - active equality constraint
+ GLP_NL - inequality constraint with lower bound active
+ GLP_NU - inequality constraint with upper bound active */
+ struct forcing_col *ptr;
+ /* list of all columns having non-zero constraint coefficient
+ a[p,j] in the forcing row */
+};
+
+static int rcv_forcing_row(NPP *npp, void *info);
+
+int npp_forcing_row(NPP *npp, NPPROW *p, int at)
+{ /* process forcing row */
+ struct forcing_row *info;
+ struct forcing_col *col = NULL;
+ NPPCOL *j;
+ NPPAIJ *apj, *aij;
+ NPPLFE *lfe;
+ double big;
+ xassert(at == 0 || at == 1);
+ /* determine maximal magnitude of the row coefficients */
+ big = 1.0;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ if (big < fabs(apj->val)) big = fabs(apj->val);
+ /* if there are too small coefficients in the row, transformation
+ should not be applied */
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ if (fabs(apj->val) < 1e-7 * big) return 1;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_forcing_row, sizeof(struct forcing_row));
+ info->p = p->i;
+ if (p->lb == p->ub)
+ { /* equality constraint */
+ info->stat = GLP_NS;
+ }
+ else if (at == 0)
+ { /* inequality constraint; case L[p] = U'[p] */
+ info->stat = GLP_NL;
+ xassert(p->lb != -DBL_MAX);
+ }
+ else /* at == 1 */
+ { /* inequality constraint; case U[p] = L'[p] */
+ info->stat = GLP_NU;
+ xassert(p->ub != +DBL_MAX);
+ }
+ info->ptr = NULL;
+ /* scan the forcing row, fix columns at corresponding bounds, and
+ save column information (the latter is not needed for MIP) */
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { /* column j has non-zero coefficient in the forcing row */
+ j = apj->col;
+ /* it must be non-fixed */
+ xassert(j->lb < j->ub);
+ /* allocate stack entry to save column information */
+ if (npp->sol != GLP_MIP)
+ { col = dmp_get_atom(npp->stack, sizeof(struct forcing_col));
+ col->j = j->j;
+ col->stat = -1; /* will be set below */
+ col->a = apj->val;
+ col->c = j->coef;
+ col->ptr = NULL;
+ col->next = info->ptr;
+ info->ptr = col;
+ }
+ /* fix column j */
+ if (at == 0 && apj->val < 0.0 || at != 0 && apj->val > 0.0)
+ { /* at its lower bound */
+ if (npp->sol != GLP_MIP)
+ col->stat = GLP_NL;
+ xassert(j->lb != -DBL_MAX);
+ j->ub = j->lb;
+ }
+ else
+ { /* at its upper bound */
+ if (npp->sol != GLP_MIP)
+ col->stat = GLP_NU;
+ xassert(j->ub != +DBL_MAX);
+ j->lb = j->ub;
+ }
+ /* save column coefficients a[i,j], i != p */
+ if (npp->sol != GLP_MIP)
+ { for (aij = j->ptr; aij != NULL; aij = aij->c_next)
+ { if (aij == apj) continue; /* skip a[p,j] */
+ lfe = dmp_get_atom(npp->stack, sizeof(NPPLFE));
+ lfe->ref = aij->row->i;
+ lfe->val = aij->val;
+ lfe->next = col->ptr;
+ col->ptr = lfe;
+ }
+ }
+ }
+ /* make the row free (unbounded) */
+ p->lb = -DBL_MAX, p->ub = +DBL_MAX;
+ return 0;
+}
+
+static int rcv_forcing_row(NPP *npp, void *_info)
+{ /* recover forcing row */
+ struct forcing_row *info = _info;
+ struct forcing_col *col, *piv;
+ NPPLFE *lfe;
+ double d, big, temp;
+ if (npp->sol == GLP_MIP) goto done;
+ /* initially solution to the original problem is the same as
+ to the transformed problem, where row p is inactive constraint
+ with pi[p] = 0, and all columns are non-basic */
+ if (npp->sol == GLP_SOL)
+ { if (npp->r_stat[info->p] != GLP_BS)
+ { npp_error();
+ return 1;
+ }
+ for (col = info->ptr; col != NULL; col = col->next)
+ { if (npp->c_stat[col->j] != GLP_NS)
+ { npp_error();
+ return 1;
+ }
+ npp->c_stat[col->j] = col->stat; /* original status */
+ }
+ }
+ /* compute reduced costs d[j] for all columns with formula (10)
+ and store them in col.c instead objective coefficients */
+ for (col = info->ptr; col != NULL; col = col->next)
+ { d = col->c;
+ for (lfe = col->ptr; lfe != NULL; lfe = lfe->next)
+ d -= lfe->val * npp->r_pi[lfe->ref];
+ col->c = d;
+ }
+ /* consider columns j, whose multipliers lambda[j] has wrong
+ sign in solution to the transformed problem (where lambda[j] =
+ d[j]), and choose column q, whose multipler lambda[q] reaches
+ zero last on changing row multiplier pi[p]; see (14) */
+ piv = NULL, big = 0.0;
+ for (col = info->ptr; col != NULL; col = col->next)
+ { d = col->c; /* d[j] */
+ temp = fabs(d / col->a);
+ if (col->stat == GLP_NL)
+ { /* column j has active lower bound */
+ if (d < 0.0 && big < temp)
+ piv = col, big = temp;
+ }
+ else if (col->stat == GLP_NU)
+ { /* column j has active upper bound */
+ if (d > 0.0 && big < temp)
+ piv = col, big = temp;
+ }
+ else
+ { npp_error();
+ return 1;
+ }
+ }
+ /* if column q does not exist, no correction is needed */
+ if (piv != NULL)
+ { /* correct solution; row p becomes active constraint while
+ column q becomes basic */
+ if (npp->sol == GLP_SOL)
+ { npp->r_stat[info->p] = info->stat;
+ npp->c_stat[piv->j] = GLP_BS;
+ }
+ /* assign new value to row multiplier pi[p] = d[p] / a[p,q] */
+ npp->r_pi[info->p] = piv->c / piv->a;
+ }
+done: return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_analyze_row - perform general row analysis
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_analyze_row(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_analyze_row performs analysis of row p of general
+* format:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p], (1)
+* j
+*
+* l[j] <= x[j] <= u[j], (2)
+*
+* where L[p] <= U[p] and l[j] <= u[j] for all a[p,j] != 0.
+*
+* RETURNS
+*
+* 0x?0 - row lower bound does not exist or is redundant;
+*
+* 0x?1 - row lower bound can be active;
+*
+* 0x?2 - row lower bound is a forcing bound;
+*
+* 0x0? - row upper bound does not exist or is redundant;
+*
+* 0x1? - row upper bound can be active;
+*
+* 0x2? - row upper bound is a forcing bound;
+*
+* 0x33 - row bounds are inconsistent with column bounds.
+*
+* ALGORITHM
+*
+* Analysis of row (1) is based on analysis of its implied lower and
+* upper bounds, which are determined by bounds of corresponding columns
+* (variables) as follows:
+*
+* L'[p] = inf sum a[p,j] x[j] =
+* j
+* (3)
+* = sum a[p,j] l[j] + sum a[p,j] u[j],
+* j in Jp j in Jn
+*
+* U'[p] = sup sum a[p,j] x[j] =
+* (4)
+* = sum a[p,j] u[j] + sum a[p,j] l[j],
+* j in Jp j in Jn
+*
+* Jp = {j: a[p,j] > 0}, Jn = {j: a[p,j] < 0}. (5)
+*
+* (Note that bounds of all columns in row p are assumed to be correct,
+* so L'[p] <= U'[p].)
+*
+* Analysis of row lower bound L[p] includes the following cases:
+*
+* 1) if L[p] > U'[p] + eps, where eps is an absolute tolerance for row
+* value, row lower bound L[p] and implied row upper bound U'[p] are
+* inconsistent, ergo, the problem has no primal feasible solution;
+*
+* 2) if U'[p] - eps <= L[p] <= U'[p] + eps, i.e. if L[p] =~ U'[p],
+* the row is a forcing row on its lower bound (see description of
+* the routine npp_forcing_row);
+*
+* 3) if L[p] > L'[p] + eps, row lower bound L[p] can be active (this
+* conclusion does not account other rows in the problem);
+*
+* 4) if L[p] <= L'[p] + eps, row lower bound L[p] cannot be active, so
+* it is redundant and can be removed (replaced by -oo).
+*
+* Analysis of row upper bound U[p] is performed in a similar way and
+* includes the following cases:
+*
+* 1) if U[p] < L'[p] - eps, row upper bound U[p] and implied row lower
+* bound L'[p] are inconsistent, ergo the problem has no primal
+* feasible solution;
+*
+* 2) if L'[p] - eps <= U[p] <= L'[p] + eps, i.e. if U[p] =~ L'[p],
+* the row is a forcing row on its upper bound (see description of
+* the routine npp_forcing_row);
+*
+* 3) if U[p] < U'[p] - eps, row upper bound U[p] can be active (this
+* conclusion does not account other rows in the problem);
+*
+* 4) if U[p] >= U'[p] - eps, row upper bound U[p] cannot be active, so
+* it is redundant and can be removed (replaced by +oo). */
+
+int npp_analyze_row(NPP *npp, NPPROW *p)
+{ /* perform general row analysis */
+ NPPAIJ *aij;
+ int ret = 0x00;
+ double l, u, eps;
+ xassert(npp == npp);
+ /* compute implied lower bound L'[p]; see (3) */
+ l = 0.0;
+ for (aij = p->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->val > 0.0)
+ { if (aij->col->lb == -DBL_MAX)
+ { l = -DBL_MAX;
+ break;
+ }
+ l += aij->val * aij->col->lb;
+ }
+ else /* aij->val < 0.0 */
+ { if (aij->col->ub == +DBL_MAX)
+ { l = -DBL_MAX;
+ break;
+ }
+ l += aij->val * aij->col->ub;
+ }
+ }
+ /* compute implied upper bound U'[p]; see (4) */
+ u = 0.0;
+ for (aij = p->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->val > 0.0)
+ { if (aij->col->ub == +DBL_MAX)
+ { u = +DBL_MAX;
+ break;
+ }
+ u += aij->val * aij->col->ub;
+ }
+ else /* aij->val < 0.0 */
+ { if (aij->col->lb == -DBL_MAX)
+ { u = +DBL_MAX;
+ break;
+ }
+ u += aij->val * aij->col->lb;
+ }
+ }
+ /* column bounds are assumed correct, so L'[p] <= U'[p] */
+ /* check if row lower bound is consistent */
+ if (p->lb != -DBL_MAX)
+ { eps = 1e-3 + 1e-6 * fabs(p->lb);
+ if (p->lb - eps > u)
+ { ret = 0x33;
+ goto done;
+ }
+ }
+ /* check if row upper bound is consistent */
+ if (p->ub != +DBL_MAX)
+ { eps = 1e-3 + 1e-6 * fabs(p->ub);
+ if (p->ub + eps < l)
+ { ret = 0x33;
+ goto done;
+ }
+ }
+ /* check if row lower bound can be active/forcing */
+ if (p->lb != -DBL_MAX)
+ { eps = 1e-9 + 1e-12 * fabs(p->lb);
+ if (p->lb - eps > l)
+ { if (p->lb + eps <= u)
+ ret |= 0x01;
+ else
+ ret |= 0x02;
+ }
+ }
+ /* check if row upper bound can be active/forcing */
+ if (p->ub != +DBL_MAX)
+ { eps = 1e-9 + 1e-12 * fabs(p->ub);
+ if (p->ub + eps < u)
+ { /* check if the upper bound is forcing */
+ if (p->ub - eps >= l)
+ ret |= 0x10;
+ else
+ ret |= 0x20;
+ }
+ }
+done: return ret;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_inactive_bound - remove row lower/upper inactive bound
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_inactive_bound(NPP *npp, NPPROW *p, int which);
+*
+* DESCRIPTION
+*
+* The routine npp_inactive_bound removes lower (if which = 0) or upper
+* (if which = 1) bound of row p:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p],
+*
+* which (bound) is assumed to be redundant.
+*
+* PROBLEM TRANSFORMATION
+*
+* If which = 0, current lower bound L[p] of row p is assigned -oo.
+* If which = 1, current upper bound U[p] of row p is assigned +oo.
+*
+* RECOVERING BASIC SOLUTION
+*
+* If in solution to the transformed problem row p is inactive
+* constraint (GLP_BS), its status is not changed in solution to the
+* original problem. Otherwise, status of row p in solution to the
+* original problem is defined by its type before transformation and
+* its status in solution to the transformed problem as follows:
+*
+* +---------------------+-------+---------------+---------------+
+* | Row | Flag | Row status in | Row status in |
+* | type | which | transfmd soln | original soln |
+* +---------------------+-------+---------------+---------------+
+* | sum >= L[p] | 0 | GLP_NF | GLP_NL |
+* | sum <= U[p] | 1 | GLP_NF | GLP_NU |
+* | L[p] <= sum <= U[p] | 0 | GLP_NU | GLP_NU |
+* | L[p] <= sum <= U[p] | 1 | GLP_NL | GLP_NL |
+* | sum = L[p] = U[p] | 0 | GLP_NU | GLP_NS |
+* | sum = L[p] = U[p] | 1 | GLP_NL | GLP_NS |
+* +---------------------+-------+---------------+---------------+
+*
+* RECOVERING INTERIOR-POINT SOLUTION
+*
+* None needed.
+*
+* RECOVERING MIP SOLUTION
+*
+* None needed. */
+
+struct inactive_bound
+{ /* row inactive bound */
+ int p;
+ /* row reference number */
+ char stat;
+ /* row status (if active constraint) */
+};
+
+static int rcv_inactive_bound(NPP *npp, void *info);
+
+void npp_inactive_bound(NPP *npp, NPPROW *p, int which)
+{ /* remove row lower/upper inactive bound */
+ struct inactive_bound *info;
+ if (npp->sol == GLP_SOL)
+ { /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_inactive_bound, sizeof(struct inactive_bound));
+ info->p = p->i;
+ if (p->ub == +DBL_MAX)
+ info->stat = GLP_NL;
+ else if (p->lb == -DBL_MAX)
+ info->stat = GLP_NU;
+ else if (p->lb != p->ub)
+ info->stat = (char)(which == 0 ? GLP_NU : GLP_NL);
+ else
+ info->stat = GLP_NS;
+ }
+ /* remove row inactive bound */
+ if (which == 0)
+ { xassert(p->lb != -DBL_MAX);
+ p->lb = -DBL_MAX;
+ }
+ else if (which == 1)
+ { xassert(p->ub != +DBL_MAX);
+ p->ub = +DBL_MAX;
+ }
+ else
+ xassert(which != which);
+ return;
+}
+
+static int rcv_inactive_bound(NPP *npp, void *_info)
+{ /* recover row status */
+ struct inactive_bound *info = _info;
+ if (npp->sol != GLP_SOL)
+ { npp_error();
+ return 1;
+ }
+ if (npp->r_stat[info->p] == GLP_BS)
+ npp->r_stat[info->p] = GLP_BS;
+ else
+ npp->r_stat[info->p] = info->stat;
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_bounds - determine implied column bounds
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_implied_bounds(NPP *npp, NPPROW *p);
+*
+* DESCRIPTION
+*
+* The routine npp_implied_bounds inspects general row (constraint) p:
+*
+* L[p] <= sum a[p,j] x[j] <= U[p], (1)
+*
+* l[j] <= x[j] <= u[j], (2)
+*
+* where L[p] <= U[p] and l[j] <= u[j] for all a[p,j] != 0, to compute
+* implied bounds of columns (variables x[j]) in this row.
+*
+* The routine stores implied column bounds l'[j] and u'[j] in column
+* descriptors (NPPCOL); it does not change current column bounds l[j]
+* and u[j]. (Implied column bounds can be then used to strengthen the
+* current column bounds; see the routines npp_implied_lower and
+* npp_implied_upper).
+*
+* ALGORITHM
+*
+* Current column bounds (2) define implied lower and upper bounds of
+* row (1) as follows:
+*
+* L'[p] = inf sum a[p,j] x[j] =
+* j
+* (3)
+* = sum a[p,j] l[j] + sum a[p,j] u[j],
+* j in Jp j in Jn
+*
+* U'[p] = sup sum a[p,j] x[j] =
+* (4)
+* = sum a[p,j] u[j] + sum a[p,j] l[j],
+* j in Jp j in Jn
+*
+* Jp = {j: a[p,j] > 0}, Jn = {j: a[p,j] < 0}. (5)
+*
+* (Note that bounds of all columns in row p are assumed to be correct,
+* so L'[p] <= U'[p].)
+*
+* If L[p] > L'[p] and/or U[p] < U'[p], the lower and/or upper bound of
+* row (1) can be active, in which case such row defines implied bounds
+* of its variables.
+*
+* Let x[k] be some variable having in row (1) coefficient a[p,k] != 0.
+* Consider a case when row lower bound can be active (L[p] > L'[p]):
+*
+* sum a[p,j] x[j] >= L[p] ==>
+* j
+*
+* sum a[p,j] x[j] + a[p,k] x[k] >= L[p] ==>
+* j!=k
+* (6)
+* a[p,k] x[k] >= L[p] - sum a[p,j] x[j] ==>
+* j!=k
+*
+* a[p,k] x[k] >= L[p,k],
+*
+* where
+*
+* L[p,k] = inf(L[p] - sum a[p,j] x[j]) =
+* j!=k
+*
+* = L[p] - sup sum a[p,j] x[j] = (7)
+* j!=k
+*
+* = L[p] - sum a[p,j] u[j] - sum a[p,j] l[j].
+* j in Jp\{k} j in Jn\{k}
+*
+* Thus:
+*
+* x[k] >= l'[k] = L[p,k] / a[p,k], if a[p,k] > 0, (8)
+*
+* x[k] <= u'[k] = L[p,k] / a[p,k], if a[p,k] < 0. (9)
+*
+* where l'[k] and u'[k] are implied lower and upper bounds of variable
+* x[k], resp.
+*
+* Now consider a similar case when row upper bound can be active
+* (U[p] < U'[p]):
+*
+* sum a[p,j] x[j] <= U[p] ==>
+* j
+*
+* sum a[p,j] x[j] + a[p,k] x[k] <= U[p] ==>
+* j!=k
+* (10)
+* a[p,k] x[k] <= U[p] - sum a[p,j] x[j] ==>
+* j!=k
+*
+* a[p,k] x[k] <= U[p,k],
+*
+* where:
+*
+* U[p,k] = sup(U[p] - sum a[p,j] x[j]) =
+* j!=k
+*
+* = U[p] - inf sum a[p,j] x[j] = (11)
+* j!=k
+*
+* = U[p] - sum a[p,j] l[j] - sum a[p,j] u[j].
+* j in Jp\{k} j in Jn\{k}
+*
+* Thus:
+*
+* x[k] <= u'[k] = U[p,k] / a[p,k], if a[p,k] > 0, (12)
+*
+* x[k] >= l'[k] = U[p,k] / a[p,k], if a[p,k] < 0. (13)
+*
+* Note that in formulae (8), (9), (12), and (13) coefficient a[p,k]
+* must not be too small in magnitude relatively to other non-zero
+* coefficients in row (1), i.e. the following condition must hold:
+*
+* |a[p,k]| >= eps * max(1, |a[p,j]|), (14)
+* j
+*
+* where eps is a relative tolerance for constraint coefficients.
+* Otherwise the implied column bounds can be numerical inreliable. For
+* example, using formula (8) for the following inequality constraint:
+*
+* 1e-12 x1 - x2 - x3 >= 0,
+*
+* where x1 >= -1, x2, x3, >= 0, may lead to numerically unreliable
+* conclusion that x1 >= 0.
+*
+* Using formulae (8), (9), (12), and (13) to compute implied bounds
+* for one variable requires |J| operations, where J = {j: a[p,j] != 0},
+* because this needs computing L[p,k] and U[p,k]. Thus, computing
+* implied bounds for all variables in row (1) would require |J|^2
+* operations, that is not a good technique. However, the total number
+* of operations can be reduced to |J| as follows.
+*
+* Let a[p,k] > 0. Then from (7) and (11) we have:
+*
+* L[p,k] = L[p] - (U'[p] - a[p,k] u[k]) =
+*
+* = L[p] - U'[p] + a[p,k] u[k],
+*
+* U[p,k] = U[p] - (L'[p] - a[p,k] l[k]) =
+*
+* = U[p] - L'[p] + a[p,k] l[k],
+*
+* where L'[p] and U'[p] are implied row lower and upper bounds defined
+* by formulae (3) and (4). Substituting these expressions into (8) and
+* (12) gives:
+*
+* l'[k] = L[p,k] / a[p,k] = u[k] + (L[p] - U'[p]) / a[p,k], (15)
+*
+* u'[k] = U[p,k] / a[p,k] = l[k] + (U[p] - L'[p]) / a[p,k]. (16)
+*
+* Similarly, if a[p,k] < 0, according to (7) and (11) we have:
+*
+* L[p,k] = L[p] - (U'[p] - a[p,k] l[k]) =
+*
+* = L[p] - U'[p] + a[p,k] l[k],
+*
+* U[p,k] = U[p] - (L'[p] - a[p,k] u[k]) =
+*
+* = U[p] - L'[p] + a[p,k] u[k],
+*
+* and substituting these expressions into (8) and (12) gives:
+*
+* l'[k] = U[p,k] / a[p,k] = u[k] + (U[p] - L'[p]) / a[p,k], (17)
+*
+* u'[k] = L[p,k] / a[p,k] = l[k] + (L[p] - U'[p]) / a[p,k]. (18)
+*
+* Note that formulae (15)-(18) can be used only if L'[p] and U'[p]
+* exist. However, if for some variable x[j] it happens that l[j] = -oo
+* and/or u[j] = +oo, values of L'[p] (if a[p,j] > 0) and/or U'[p] (if
+* a[p,j] < 0) are undefined. Consider, therefore, the most general
+* situation, when some column bounds (2) may not exist.
+*
+* Let:
+*
+* J' = {j : (a[p,j] > 0 and l[j] = -oo) or
+* (19)
+* (a[p,j] < 0 and u[j] = +oo)}.
+*
+* Then (assuming that row upper bound U[p] can be active) the following
+* three cases are possible:
+*
+* 1) |J'| = 0. In this case L'[p] exists, thus, for all variables x[j]
+* in row (1) we can use formulae (16) and (17);
+*
+* 2) J' = {k}. In this case L'[p] = -oo, however, U[p,k] (11) exists,
+* so for variable x[k] we can use formulae (12) and (13). Note that
+* for all other variables x[j] (j != k) l'[j] = -oo (if a[p,j] < 0)
+* or u'[j] = +oo (if a[p,j] > 0);
+*
+* 3) |J'| > 1. In this case for all variables x[j] in row [1] we have
+* l'[j] = -oo (if a[p,j] < 0) or u'[j] = +oo (if a[p,j] > 0).
+*
+* Similarly, let:
+*
+* J'' = {j : (a[p,j] > 0 and u[j] = +oo) or
+* (20)
+* (a[p,j] < 0 and l[j] = -oo)}.
+*
+* Then (assuming that row lower bound L[p] can be active) the following
+* three cases are possible:
+*
+* 1) |J''| = 0. In this case U'[p] exists, thus, for all variables x[j]
+* in row (1) we can use formulae (15) and (18);
+*
+* 2) J'' = {k}. In this case U'[p] = +oo, however, L[p,k] (7) exists,
+* so for variable x[k] we can use formulae (8) and (9). Note that
+* for all other variables x[j] (j != k) l'[j] = -oo (if a[p,j] > 0)
+* or u'[j] = +oo (if a[p,j] < 0);
+*
+* 3) |J''| > 1. In this case for all variables x[j] in row (1) we have
+* l'[j] = -oo (if a[p,j] > 0) or u'[j] = +oo (if a[p,j] < 0). */
+
+void npp_implied_bounds(NPP *npp, NPPROW *p)
+{ NPPAIJ *apj, *apk;
+ double big, eps, temp;
+ xassert(npp == npp);
+ /* initialize implied bounds for all variables and determine
+ maximal magnitude of row coefficients a[p,j] */
+ big = 1.0;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { apj->col->ll.ll = -DBL_MAX, apj->col->uu.uu = +DBL_MAX;
+ if (big < fabs(apj->val)) big = fabs(apj->val);
+ }
+ eps = 1e-6 * big;
+ /* process row lower bound (assuming that it can be active) */
+ if (p->lb != -DBL_MAX)
+ { apk = NULL;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj->val > 0.0 && apj->col->ub == +DBL_MAX ||
+ apj->val < 0.0 && apj->col->lb == -DBL_MAX)
+ { if (apk == NULL)
+ apk = apj;
+ else
+ goto skip1;
+ }
+ }
+ /* if a[p,k] = NULL then |J'| = 0 else J' = { k } */
+ temp = p->lb;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj == apk)
+ /* skip a[p,k] */;
+ else if (apj->val > 0.0)
+ temp -= apj->val * apj->col->ub;
+ else /* apj->val < 0.0 */
+ temp -= apj->val * apj->col->lb;
+ }
+ /* compute column implied bounds */
+ if (apk == NULL)
+ { /* temp = L[p] - U'[p] */
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj->val >= +eps)
+ { /* l'[j] := u[j] + (L[p] - U'[p]) / a[p,j] */
+ apj->col->ll.ll = apj->col->ub + temp / apj->val;
+ }
+ else if (apj->val <= -eps)
+ { /* u'[j] := l[j] + (L[p] - U'[p]) / a[p,j] */
+ apj->col->uu.uu = apj->col->lb + temp / apj->val;
+ }
+ }
+ }
+ else
+ { /* temp = L[p,k] */
+ if (apk->val >= +eps)
+ { /* l'[k] := L[p,k] / a[p,k] */
+ apk->col->ll.ll = temp / apk->val;
+ }
+ else if (apk->val <= -eps)
+ { /* u'[k] := L[p,k] / a[p,k] */
+ apk->col->uu.uu = temp / apk->val;
+ }
+ }
+skip1: ;
+ }
+ /* process row upper bound (assuming that it can be active) */
+ if (p->ub != +DBL_MAX)
+ { apk = NULL;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj->val > 0.0 && apj->col->lb == -DBL_MAX ||
+ apj->val < 0.0 && apj->col->ub == +DBL_MAX)
+ { if (apk == NULL)
+ apk = apj;
+ else
+ goto skip2;
+ }
+ }
+ /* if a[p,k] = NULL then |J''| = 0 else J'' = { k } */
+ temp = p->ub;
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj == apk)
+ /* skip a[p,k] */;
+ else if (apj->val > 0.0)
+ temp -= apj->val * apj->col->lb;
+ else /* apj->val < 0.0 */
+ temp -= apj->val * apj->col->ub;
+ }
+ /* compute column implied bounds */
+ if (apk == NULL)
+ { /* temp = U[p] - L'[p] */
+ for (apj = p->ptr; apj != NULL; apj = apj->r_next)
+ { if (apj->val >= +eps)
+ { /* u'[j] := l[j] + (U[p] - L'[p]) / a[p,j] */
+ apj->col->uu.uu = apj->col->lb + temp / apj->val;
+ }
+ else if (apj->val <= -eps)
+ { /* l'[j] := u[j] + (U[p] - L'[p]) / a[p,j] */
+ apj->col->ll.ll = apj->col->ub + temp / apj->val;
+ }
+ }
+ }
+ else
+ { /* temp = U[p,k] */
+ if (apk->val >= +eps)
+ { /* u'[k] := U[p,k] / a[p,k] */
+ apk->col->uu.uu = temp / apk->val;
+ }
+ else if (apk->val <= -eps)
+ { /* l'[k] := U[p,k] / a[p,k] */
+ apk->col->ll.ll = temp / apk->val;
+ }
+ }
+skip2: ;
+ }
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp4.c b/test/monniaux/glpk-4.65/src/npp/npp4.c
new file mode 100644
index 00000000..d7dd0e86
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp4.c
@@ -0,0 +1,1414 @@
+/* npp4.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* npp_binarize_prob - binarize MIP problem
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_binarize_prob(NPP *npp);
+*
+* DESCRIPTION
+*
+* The routine npp_binarize_prob replaces in the original MIP problem
+* every integer variable:
+*
+* l[q] <= x[q] <= u[q], (1)
+*
+* where l[q] < u[q], by an equivalent sum of binary variables.
+*
+* RETURNS
+*
+* The routine returns the number of integer variables for which the
+* transformation failed, because u[q] - l[q] > d_max.
+*
+* PROBLEM TRANSFORMATION
+*
+* If variable x[q] has non-zero lower bound, it is first processed
+* with the routine npp_lbnd_col. Thus, we can assume that:
+*
+* 0 <= x[q] <= u[q]. (2)
+*
+* If u[q] = 1, variable x[q] is already binary, so further processing
+* is not needed. Let, therefore, that 2 <= u[q] <= d_max, and n be a
+* smallest integer such that u[q] <= 2^n - 1 (n >= 2, since u[q] >= 2).
+* Then variable x[q] can be replaced by the following sum:
+*
+* n-1
+* x[q] = sum 2^k x[k], (3)
+* k=0
+*
+* where x[k] are binary columns (variables). If u[q] < 2^n - 1, the
+* following additional inequality constraint must be also included in
+* the transformed problem:
+*
+* n-1
+* sum 2^k x[k] <= u[q]. (4)
+* k=0
+*
+* Note: Assuming that in the transformed problem x[q] becomes binary
+* variable x[0], this transformation causes new n-1 binary variables
+* to appear.
+*
+* Substituting x[q] from (3) to the objective row gives:
+*
+* z = sum c[j] x[j] + c[0] =
+* j
+*
+* = sum c[j] x[j] + c[q] x[q] + c[0] =
+* j!=q
+* n-1
+* = sum c[j] x[j] + c[q] sum 2^k x[k] + c[0] =
+* j!=q k=0
+* n-1
+* = sum c[j] x[j] + sum c[k] x[k] + c[0],
+* j!=q k=0
+*
+* where:
+*
+* c[k] = 2^k c[q], k = 0, ..., n-1. (5)
+*
+* And substituting x[q] from (3) to i-th constraint row i gives:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i] ==>
+* j
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
+* j!=q
+* n-1
+* L[i] <= sum a[i,j] x[j] + a[i,q] sum 2^k x[k] <= U[i] ==>
+* j!=q k=0
+* n-1
+* L[i] <= sum a[i,j] x[j] + sum a[i,k] x[k] <= U[i],
+* j!=q k=0
+*
+* where:
+*
+* a[i,k] = 2^k a[i,q], k = 0, ..., n-1. (6)
+*
+* RECOVERING SOLUTION
+*
+* Value of variable x[q] is computed with formula (3). */
+
+struct binarize
+{ int q;
+ /* column reference number for x[q] = x[0] */
+ int j;
+ /* column reference number for x[1]; x[2] has reference number
+ j+1, x[3] - j+2, etc. */
+ int n;
+ /* total number of binary variables, n >= 2 */
+};
+
+static int rcv_binarize_prob(NPP *npp, void *info);
+
+int npp_binarize_prob(NPP *npp)
+{ /* binarize MIP problem */
+ struct binarize *info;
+ NPPROW *row;
+ NPPCOL *col, *bin;
+ NPPAIJ *aij;
+ int u, n, k, temp, nfails, nvars, nbins, nrows;
+ /* new variables will be added to the end of the column list, so
+ we go from the end to beginning of the column list */
+ nfails = nvars = nbins = nrows = 0;
+ for (col = npp->c_tail; col != NULL; col = col->prev)
+ { /* skip continuous variable */
+ if (!col->is_int) continue;
+ /* skip fixed variable */
+ if (col->lb == col->ub) continue;
+ /* skip binary variable */
+ if (col->lb == 0.0 && col->ub == 1.0) continue;
+ /* check if the transformation is applicable */
+ if (col->lb < -1e6 || col->ub > +1e6 ||
+ col->ub - col->lb > 4095.0)
+ { /* unfortunately, not */
+ nfails++;
+ continue;
+ }
+ /* process integer non-binary variable x[q] */
+ nvars++;
+ /* make x[q] non-negative, if its lower bound is non-zero */
+ if (col->lb != 0.0)
+ npp_lbnd_col(npp, col);
+ /* now 0 <= x[q] <= u[q] */
+ xassert(col->lb == 0.0);
+ u = (int)col->ub;
+ xassert(col->ub == (double)u);
+ /* if x[q] is binary, further processing is not needed */
+ if (u == 1) continue;
+ /* determine smallest n such that u <= 2^n - 1 (thus, n is the
+ number of binary variables needed) */
+ n = 2, temp = 4;
+ while (u >= temp)
+ n++, temp += temp;
+ nbins += n;
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_binarize_prob, sizeof(struct binarize));
+ info->q = col->j;
+ info->j = 0; /* will be set below */
+ info->n = n;
+ /* if u < 2^n - 1, we need one additional row for (4) */
+ if (u < temp - 1)
+ { row = npp_add_row(npp), nrows++;
+ row->lb = -DBL_MAX, row->ub = u;
+ }
+ else
+ row = NULL;
+ /* in the transformed problem variable x[q] becomes binary
+ variable x[0], so its objective and constraint coefficients
+ are not changed */
+ col->ub = 1.0;
+ /* include x[0] into constraint (4) */
+ if (row != NULL)
+ npp_add_aij(npp, row, col, 1.0);
+ /* add other binary variables x[1], ..., x[n-1] */
+ for (k = 1, temp = 2; k < n; k++, temp += temp)
+ { /* add new binary variable x[k] */
+ bin = npp_add_col(npp);
+ bin->is_int = 1;
+ bin->lb = 0.0, bin->ub = 1.0;
+ bin->coef = (double)temp * col->coef;
+ /* store column reference number for x[1] */
+ if (info->j == 0)
+ info->j = bin->j;
+ else
+ xassert(info->j + (k-1) == bin->j);
+ /* duplicate constraint coefficients for x[k]; this also
+ automatically includes x[k] into constraint (4) */
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ npp_add_aij(npp, aij->row, bin, (double)temp * aij->val);
+ }
+ }
+ if (nvars > 0)
+ xprintf("%d integer variable(s) were replaced by %d binary one"
+ "s\n", nvars, nbins);
+ if (nrows > 0)
+ xprintf("%d row(s) were added due to binarization\n", nrows);
+ if (nfails > 0)
+ xprintf("Binarization failed for %d integer variable(s)\n",
+ nfails);
+ return nfails;
+}
+
+static int rcv_binarize_prob(NPP *npp, void *_info)
+{ /* recovery binarized variable */
+ struct binarize *info = _info;
+ int k, temp;
+ double sum;
+ /* compute value of x[q]; see formula (3) */
+ sum = npp->c_value[info->q];
+ for (k = 1, temp = 2; k < info->n; k++, temp += temp)
+ sum += (double)temp * npp->c_value[info->j + (k-1)];
+ npp->c_value[info->q] = sum;
+ return 0;
+}
+
+/**********************************************************************/
+
+struct elem
+{ /* linear form element a[j] x[j] */
+ double aj;
+ /* non-zero coefficient value */
+ NPPCOL *xj;
+ /* pointer to variable (column) */
+ struct elem *next;
+ /* pointer to another term */
+};
+
+static struct elem *copy_form(NPP *npp, NPPROW *row, double s)
+{ /* copy linear form */
+ NPPAIJ *aij;
+ struct elem *ptr, *e;
+ ptr = NULL;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { e = dmp_get_atom(npp->pool, sizeof(struct elem));
+ e->aj = s * aij->val;
+ e->xj = aij->col;
+ e->next = ptr;
+ ptr = e;
+ }
+ return ptr;
+}
+
+static void drop_form(NPP *npp, struct elem *ptr)
+{ /* drop linear form */
+ struct elem *e;
+ while (ptr != NULL)
+ { e = ptr;
+ ptr = e->next;
+ dmp_free_atom(npp->pool, e, sizeof(struct elem));
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_is_packing - test if constraint is packing inequality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_is_packing(NPP *npp, NPPROW *row);
+*
+* RETURNS
+*
+* If the specified row (constraint) is packing inequality (see below),
+* the routine npp_is_packing returns non-zero. Otherwise, it returns
+* zero.
+*
+* PACKING INEQUALITIES
+*
+* In canonical format the packing inequality is the following:
+*
+* sum x[j] <= 1, (1)
+* j in J
+*
+* where all variables x[j] are binary. This inequality expresses the
+* condition that in any integer feasible solution at most one variable
+* from set J can take non-zero (unity) value while other variables
+* must be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because
+* if J is empty or |J| = 1, the inequality (1) is redundant.
+*
+* In general case the packing inequality may include original variables
+* x[j] as well as their complements x~[j]:
+*
+* sum x[j] + sum x~[j] <= 1, (2)
+* j in Jp j in Jn
+*
+* where Jp and Jn are not intersected. Therefore, using substitution
+* x~[j] = 1 - x[j] gives the packing inequality in generalized format:
+*
+* sum x[j] - sum x[j] <= 1 - |Jn|. (3)
+* j in Jp j in Jn */
+
+int npp_is_packing(NPP *npp, NPPROW *row)
+{ /* test if constraint is packing inequality */
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int b;
+ xassert(npp == npp);
+ if (!(row->lb == -DBL_MAX && row->ub != +DBL_MAX))
+ return 0;
+ b = 1;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ return 0;
+ if (aij->val == +1.0)
+ ;
+ else if (aij->val == -1.0)
+ b--;
+ else
+ return 0;
+ }
+ if (row->ub != (double)b) return 0;
+ return 1;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_hidden_packing - identify hidden packing inequality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_hidden_packing(NPP *npp, NPPROW *row);
+*
+* DESCRIPTION
+*
+* The routine npp_hidden_packing processes specified inequality
+* constraint, which includes only binary variables, and the number of
+* the variables is not less than two. If the original inequality is
+* equivalent to a packing inequality, the routine replaces it by this
+* equivalent inequality. If the original constraint is double-sided
+* inequality, it is replaced by a pair of single-sided inequalities,
+* if necessary.
+*
+* RETURNS
+*
+* If the original inequality constraint was replaced by equivalent
+* packing inequality, the routine npp_hidden_packing returns non-zero.
+* Otherwise, it returns zero.
+*
+* PROBLEM TRANSFORMATION
+*
+* Consider an inequality constraint:
+*
+* sum a[j] x[j] <= b, (1)
+* j in J
+*
+* where all variables x[j] are binary, and |J| >= 2. (In case of '>='
+* inequality it can be transformed to '<=' format by multiplying both
+* its sides by -1.)
+*
+* Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
+* x[j] = 1 - x~[j] for all j in Jn, we have:
+*
+* sum a[j] x[j] <= b ==>
+* j in J
+*
+* sum a[j] x[j] + sum a[j] x[j] <= b ==>
+* j in Jp j in Jn
+*
+* sum a[j] x[j] + sum a[j] (1 - x~[j]) <= b ==>
+* j in Jp j in Jn
+*
+* sum a[j] x[j] - sum a[j] x~[j] <= b - sum a[j].
+* j in Jp j in Jn j in Jn
+*
+* Thus, meaning the transformation above, we can assume that in
+* inequality (1) all coefficients a[j] are positive. Moreover, we can
+* assume that a[j] <= b. In fact, let a[j] > b; then the following
+* three cases are possible:
+*
+* 1) b < 0. In this case inequality (1) is infeasible, so the problem
+* has no feasible solution (see the routine npp_analyze_row);
+*
+* 2) b = 0. In this case inequality (1) is a forcing inequality on its
+* upper bound (see the routine npp_forcing row), from which it
+* follows that all variables x[j] should be fixed at zero;
+*
+* 3) b > 0. In this case inequality (1) defines an implied zero upper
+* bound for variable x[j] (see the routine npp_implied_bounds), from
+* which it follows that x[j] should be fixed at zero.
+*
+* It is assumed that all three cases listed above have been recognized
+* by the routine npp_process_prob, which performs basic MIP processing
+* prior to a call the routine npp_hidden_packing. So, if one of these
+* cases occurs, we should just skip processing such constraint.
+*
+* Thus, let 0 < a[j] <= b. Then it is obvious that constraint (1) is
+* equivalent to packing inquality only if:
+*
+* a[j] + a[k] > b + eps (2)
+*
+* for all j, k in J, j != k, where eps is an absolute tolerance for
+* row (linear form) value. Checking the condition (2) for all j and k,
+* j != k, requires time O(|J|^2). However, this time can be reduced to
+* O(|J|), if use minimal a[j] and a[k], in which case it is sufficient
+* to check the condition (2) only once.
+*
+* Once the original inequality (1) is replaced by equivalent packing
+* inequality, we need to perform back substitution x~[j] = 1 - x[j] for
+* all j in Jn (see above).
+*
+* RECOVERING SOLUTION
+*
+* None needed. */
+
+static int hidden_packing(NPP *npp, struct elem *ptr, double *_b)
+{ /* process inequality constraint: sum a[j] x[j] <= b;
+ 0 - specified row is NOT hidden packing inequality;
+ 1 - specified row is packing inequality;
+ 2 - specified row is hidden packing inequality. */
+ struct elem *e, *ej, *ek;
+ int neg;
+ double b = *_b, eps;
+ xassert(npp == npp);
+ /* a[j] must be non-zero, x[j] must be binary, for all j in J */
+ for (e = ptr; e != NULL; e = e->next)
+ { xassert(e->aj != 0.0);
+ xassert(e->xj->is_int);
+ xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
+ }
+ /* check if the specified inequality constraint already has the
+ form of packing inequality */
+ neg = 0; /* neg is |Jn| */
+ for (e = ptr; e != NULL; e = e->next)
+ { if (e->aj == +1.0)
+ ;
+ else if (e->aj == -1.0)
+ neg++;
+ else
+ break;
+ }
+ if (e == NULL)
+ { /* all coefficients a[j] are +1 or -1; check rhs b */
+ if (b == (double)(1 - neg))
+ { /* it is packing inequality; no processing is needed */
+ return 1;
+ }
+ }
+ /* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
+ positive; the result is a~[j] = |a[j]| and new rhs b */
+ for (e = ptr; e != NULL; e = e->next)
+ if (e->aj < 0) b -= e->aj;
+ /* now a[j] > 0 for all j in J (actually |a[j]| are used) */
+ /* if a[j] > b, skip processing--this case must not appear */
+ for (e = ptr; e != NULL; e = e->next)
+ if (fabs(e->aj) > b) return 0;
+ /* now 0 < a[j] <= b for all j in J */
+ /* find two minimal coefficients a[j] and a[k], j != k */
+ ej = NULL;
+ for (e = ptr; e != NULL; e = e->next)
+ if (ej == NULL || fabs(ej->aj) > fabs(e->aj)) ej = e;
+ xassert(ej != NULL);
+ ek = NULL;
+ for (e = ptr; e != NULL; e = e->next)
+ if (e != ej)
+ if (ek == NULL || fabs(ek->aj) > fabs(e->aj)) ek = e;
+ xassert(ek != NULL);
+ /* the specified constraint is equivalent to packing inequality
+ iff a[j] + a[k] > b + eps */
+ eps = 1e-3 + 1e-6 * fabs(b);
+ if (fabs(ej->aj) + fabs(ek->aj) <= b + eps) return 0;
+ /* perform back substitution x~[j] = 1 - x[j] and construct the
+ final equivalent packing inequality in generalized format */
+ b = 1.0;
+ for (e = ptr; e != NULL; e = e->next)
+ { if (e->aj > 0.0)
+ e->aj = +1.0;
+ else /* e->aj < 0.0 */
+ e->aj = -1.0, b -= 1.0;
+ }
+ *_b = b;
+ return 2;
+}
+
+int npp_hidden_packing(NPP *npp, NPPROW *row)
+{ /* identify hidden packing inequality */
+ NPPROW *copy;
+ NPPAIJ *aij;
+ struct elem *ptr, *e;
+ int kase, ret, count = 0;
+ double b;
+ /* the row must be inequality constraint */
+ xassert(row->lb < row->ub);
+ for (kase = 0; kase <= 1; kase++)
+ { if (kase == 0)
+ { /* process row upper bound */
+ if (row->ub == +DBL_MAX) continue;
+ ptr = copy_form(npp, row, +1.0);
+ b = + row->ub;
+ }
+ else
+ { /* process row lower bound */
+ if (row->lb == -DBL_MAX) continue;
+ ptr = copy_form(npp, row, -1.0);
+ b = - row->lb;
+ }
+ /* now the inequality has the form "sum a[j] x[j] <= b" */
+ ret = hidden_packing(npp, ptr, &b);
+ xassert(0 <= ret && ret <= 2);
+ if (kase == 1 && ret == 1 || ret == 2)
+ { /* the original inequality has been identified as hidden
+ packing inequality */
+ count++;
+#ifdef GLP_DEBUG
+ xprintf("Original constraint:\n");
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ xprintf(" %+g x%d", aij->val, aij->col->j);
+ if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
+ if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
+ xprintf("\n");
+ xprintf("Equivalent packing inequality:\n");
+ for (e = ptr; e != NULL; e = e->next)
+ xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
+ xprintf(", <= %g\n", b);
+#endif
+ if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
+ { /* the original row is single-sided inequality; no copy
+ is needed */
+ copy = NULL;
+ }
+ else
+ { /* the original row is double-sided inequality; we need
+ to create its copy for other bound before replacing it
+ with the equivalent inequality */
+ copy = npp_add_row(npp);
+ if (kase == 0)
+ { /* the copy is for lower bound */
+ copy->lb = row->lb, copy->ub = +DBL_MAX;
+ }
+ else
+ { /* the copy is for upper bound */
+ copy->lb = -DBL_MAX, copy->ub = row->ub;
+ }
+ /* copy original row coefficients */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_add_aij(npp, copy, aij->col, aij->val);
+ }
+ /* replace the original inequality by equivalent one */
+ npp_erase_row(npp, row);
+ row->lb = -DBL_MAX, row->ub = b;
+ for (e = ptr; e != NULL; e = e->next)
+ npp_add_aij(npp, row, e->xj, e->aj);
+ /* continue processing lower bound for the copy */
+ if (copy != NULL) row = copy;
+ }
+ drop_form(npp, ptr);
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_implied_packing - identify implied packing inequality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_implied_packing(NPP *npp, NPPROW *row, int which,
+* NPPCOL *var[], char set[]);
+*
+* DESCRIPTION
+*
+* The routine npp_implied_packing processes specified row (constraint)
+* of general format:
+*
+* L <= sum a[j] x[j] <= U. (1)
+* j
+*
+* If which = 0, only lower bound L, which must exist, is considered,
+* while upper bound U is ignored. Similarly, if which = 1, only upper
+* bound U, which must exist, is considered, while lower bound L is
+* ignored. Thus, if the specified row is a double-sided inequality or
+* equality constraint, this routine should be called twice for both
+* lower and upper bounds.
+*
+* The routine npp_implied_packing attempts to find a non-trivial (i.e.
+* having not less than two binary variables) packing inequality:
+*
+* sum x[j] - sum x[j] <= 1 - |Jn|, (2)
+* j in Jp j in Jn
+*
+* which is relaxation of the constraint (1) in the sense that any
+* solution satisfying to that constraint also satisfies to the packing
+* inequality (2). If such relaxation exists, the routine stores
+* pointers to descriptors of corresponding binary variables and their
+* flags, resp., to locations var[1], var[2], ..., var[len] and set[1],
+* set[2], ..., set[len], where set[j] = 0 means that j in Jp and
+* set[j] = 1 means that j in Jn.
+*
+* RETURNS
+*
+* The routine npp_implied_packing returns len, which is the total
+* number of binary variables in the packing inequality found, len >= 2.
+* However, if the relaxation does not exist, the routine returns zero.
+*
+* ALGORITHM
+*
+* If which = 0, the constraint coefficients (1) are multiplied by -1
+* and b is assigned -L; if which = 1, the constraint coefficients (1)
+* are not changed and b is assigned +U. In both cases the specified
+* constraint gets the following format:
+*
+* sum a[j] x[j] <= b. (3)
+* j
+*
+* (Note that (3) is a relaxation of (1), because one of bounds L or U
+* is ignored.)
+*
+* Let J be set of binary variables, Kp be set of non-binary (integer
+* or continuous) variables with a[j] > 0, and Kn be set of non-binary
+* variables with a[j] < 0. Then the inequality (3) can be written as
+* follows:
+*
+* sum a[j] x[j] <= b - sum a[j] x[j] - sum a[j] x[j]. (4)
+* j in J j in Kp j in Kn
+*
+* To get rid of non-binary variables we can replace the inequality (4)
+* by the following relaxed inequality:
+*
+* sum a[j] x[j] <= b~, (5)
+* j in J
+*
+* where:
+*
+* b~ = sup(b - sum a[j] x[j] - sum a[j] x[j]) =
+* j in Kp j in Kn
+*
+* = b - inf sum a[j] x[j] - inf sum a[j] x[j] = (6)
+* j in Kp j in Kn
+*
+* = b - sum a[j] l[j] - sum a[j] u[j].
+* j in Kp j in Kn
+*
+* Note that if lower bound l[j] (if j in Kp) or upper bound u[j]
+* (if j in Kn) of some non-binary variable x[j] does not exist, then
+* formally b = +oo, in which case further analysis is not performed.
+*
+* Let Bp = {j in J: a[j] > 0}, Bn = {j in J: a[j] < 0}. To make all
+* the inequality coefficients in (5) positive, we replace all x[j] in
+* Bn by their complementaries, substituting x[j] = 1 - x~[j] for all
+* j in Bn, that gives:
+*
+* sum a[j] x[j] - sum a[j] x~[j] <= b~ - sum a[j]. (7)
+* j in Bp j in Bn j in Bn
+*
+* This inequality is a relaxation of the original constraint (1), and
+* it is a binary knapsack inequality. Writing it in the standard format
+* we have:
+*
+* sum alfa[j] z[j] <= beta, (8)
+* j in J
+*
+* where:
+* ( + a[j], if j in Bp,
+* alfa[j] = < (9)
+* ( - a[j], if j in Bn,
+*
+* ( x[j], if j in Bp,
+* z[j] = < (10)
+* ( 1 - x[j], if j in Bn,
+*
+* beta = b~ - sum a[j]. (11)
+* j in Bn
+*
+* In the inequality (8) all coefficients are positive, therefore, the
+* packing relaxation to be found for this inequality is the following:
+*
+* sum z[j] <= 1. (12)
+* j in P
+*
+* It is obvious that set P within J, which we would like to find, must
+* satisfy to the following condition:
+*
+* alfa[j] + alfa[k] > beta + eps for all j, k in P, j != k, (13)
+*
+* where eps is an absolute tolerance for value of the linear form.
+* Thus, it is natural to take P = {j: alpha[j] > (beta + eps) / 2}.
+* Moreover, if in the equality (8) there exist coefficients alfa[k],
+* for which alfa[k] <= (beta + eps) / 2, but which, nevertheless,
+* satisfies to the condition (13) for all j in P, *one* corresponding
+* variable z[k] (having, for example, maximal coefficient alfa[k]) can
+* be included in set P, that allows increasing the number of binary
+* variables in (12) by one.
+*
+* Once the set P has been built, for the inequality (12) we need to
+* perform back substitution according to (10) in order to express it
+* through the original binary variables. As the result of such back
+* substitution the relaxed packing inequality get its final format (2),
+* where Jp = J intersect Bp, and Jn = J intersect Bn. */
+
+int npp_implied_packing(NPP *npp, NPPROW *row, int which,
+ NPPCOL *var[], char set[])
+{ struct elem *ptr, *e, *i, *k;
+ int len = 0;
+ double b, eps;
+ /* build inequality (3) */
+ if (which == 0)
+ { ptr = copy_form(npp, row, -1.0);
+ xassert(row->lb != -DBL_MAX);
+ b = - row->lb;
+ }
+ else if (which == 1)
+ { ptr = copy_form(npp, row, +1.0);
+ xassert(row->ub != +DBL_MAX);
+ b = + row->ub;
+ }
+ /* remove non-binary variables to build relaxed inequality (5);
+ compute its right-hand side b~ with formula (6) */
+ for (e = ptr; e != NULL; e = e->next)
+ { if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
+ { /* x[j] is non-binary variable */
+ if (e->aj > 0.0)
+ { if (e->xj->lb == -DBL_MAX) goto done;
+ b -= e->aj * e->xj->lb;
+ }
+ else /* e->aj < 0.0 */
+ { if (e->xj->ub == +DBL_MAX) goto done;
+ b -= e->aj * e->xj->ub;
+ }
+ /* a[j] = 0 means that variable x[j] is removed */
+ e->aj = 0.0;
+ }
+ }
+ /* substitute x[j] = 1 - x~[j] to build knapsack inequality (8);
+ compute its right-hand side beta with formula (11) */
+ for (e = ptr; e != NULL; e = e->next)
+ if (e->aj < 0.0) b -= e->aj;
+ /* if beta is close to zero, the knapsack inequality is either
+ infeasible or forcing inequality; this must never happen, so
+ we skip further analysis */
+ if (b < 1e-3) goto done;
+ /* build set P as well as sets Jp and Jn, and determine x[k] as
+ explained above in comments to the routine */
+ eps = 1e-3 + 1e-6 * b;
+ i = k = NULL;
+ for (e = ptr; e != NULL; e = e->next)
+ { /* note that alfa[j] = |a[j]| */
+ if (fabs(e->aj) > 0.5 * (b + eps))
+ { /* alfa[j] > (b + eps) / 2; include x[j] in set P, i.e. in
+ set Jp or Jn */
+ var[++len] = e->xj;
+ set[len] = (char)(e->aj > 0.0 ? 0 : 1);
+ /* alfa[i] = min alfa[j] over all j included in set P */
+ if (i == NULL || fabs(i->aj) > fabs(e->aj)) i = e;
+ }
+ else if (fabs(e->aj) >= 1e-3)
+ { /* alfa[k] = max alfa[j] over all j not included in set P;
+ we skip coefficient a[j] if it is close to zero to avoid
+ numerically unreliable results */
+ if (k == NULL || fabs(k->aj) < fabs(e->aj)) k = e;
+ }
+ }
+ /* if alfa[k] satisfies to condition (13) for all j in P, include
+ x[k] in P */
+ if (i != NULL && k != NULL && fabs(i->aj) + fabs(k->aj) > b + eps)
+ { var[++len] = k->xj;
+ set[len] = (char)(k->aj > 0.0 ? 0 : 1);
+ }
+ /* trivial packing inequality being redundant must never appear,
+ so we just ignore it */
+ if (len < 2) len = 0;
+done: drop_form(npp, ptr);
+ return len;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_is_covering - test if constraint is covering inequality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_is_covering(NPP *npp, NPPROW *row);
+*
+* RETURNS
+*
+* If the specified row (constraint) is covering inequality (see below),
+* the routine npp_is_covering returns non-zero. Otherwise, it returns
+* zero.
+*
+* COVERING INEQUALITIES
+*
+* In canonical format the covering inequality is the following:
+*
+* sum x[j] >= 1, (1)
+* j in J
+*
+* where all variables x[j] are binary. This inequality expresses the
+* condition that in any integer feasible solution variables in set J
+* cannot be all equal to zero at the same time, i.e. at least one
+* variable must take non-zero (unity) value. W.l.o.g. it is assumed
+* that |J| >= 2, because if J is empty, the inequality (1) is
+* infeasible, and if |J| = 1, the inequality (1) is a forcing row.
+*
+* In general case the covering inequality may include original
+* variables x[j] as well as their complements x~[j]:
+*
+* sum x[j] + sum x~[j] >= 1, (2)
+* j in Jp j in Jn
+*
+* where Jp and Jn are not intersected. Therefore, using substitution
+* x~[j] = 1 - x[j] gives the packing inequality in generalized format:
+*
+* sum x[j] - sum x[j] >= 1 - |Jn|. (3)
+* j in Jp j in Jn
+*
+* (May note that the inequality (3) cuts off infeasible solutions,
+* where x[j] = 0 for all j in Jp and x[j] = 1 for all j in Jn.)
+*
+* NOTE: If |J| = 2, the inequality (3) is equivalent to packing
+* inequality (see the routine npp_is_packing). */
+
+int npp_is_covering(NPP *npp, NPPROW *row)
+{ /* test if constraint is covering inequality */
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int b;
+ xassert(npp == npp);
+ if (!(row->lb != -DBL_MAX && row->ub == +DBL_MAX))
+ return 0;
+ b = 1;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ return 0;
+ if (aij->val == +1.0)
+ ;
+ else if (aij->val == -1.0)
+ b--;
+ else
+ return 0;
+ }
+ if (row->lb != (double)b) return 0;
+ return 1;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_hidden_covering - identify hidden covering inequality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_hidden_covering(NPP *npp, NPPROW *row);
+*
+* DESCRIPTION
+*
+* The routine npp_hidden_covering processes specified inequality
+* constraint, which includes only binary variables, and the number of
+* the variables is not less than three. If the original inequality is
+* equivalent to a covering inequality (see below), the routine
+* replaces it by the equivalent inequality. If the original constraint
+* is double-sided inequality, it is replaced by a pair of single-sided
+* inequalities, if necessary.
+*
+* RETURNS
+*
+* If the original inequality constraint was replaced by equivalent
+* covering inequality, the routine npp_hidden_covering returns
+* non-zero. Otherwise, it returns zero.
+*
+* PROBLEM TRANSFORMATION
+*
+* Consider an inequality constraint:
+*
+* sum a[j] x[j] >= b, (1)
+* j in J
+*
+* where all variables x[j] are binary, and |J| >= 3. (In case of '<='
+* inequality it can be transformed to '>=' format by multiplying both
+* its sides by -1.)
+*
+* Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
+* x[j] = 1 - x~[j] for all j in Jn, we have:
+*
+* sum a[j] x[j] >= b ==>
+* j in J
+*
+* sum a[j] x[j] + sum a[j] x[j] >= b ==>
+* j in Jp j in Jn
+*
+* sum a[j] x[j] + sum a[j] (1 - x~[j]) >= b ==>
+* j in Jp j in Jn
+*
+* sum m a[j] x[j] - sum a[j] x~[j] >= b - sum a[j].
+* j in Jp j in Jn j in Jn
+*
+* Thus, meaning the transformation above, we can assume that in
+* inequality (1) all coefficients a[j] are positive. Moreover, we can
+* assume that b > 0, because otherwise the inequality (1) would be
+* redundant (see the routine npp_analyze_row). It is then obvious that
+* constraint (1) is equivalent to covering inequality only if:
+*
+* a[j] >= b, (2)
+*
+* for all j in J.
+*
+* Once the original inequality (1) is replaced by equivalent covering
+* inequality, we need to perform back substitution x~[j] = 1 - x[j] for
+* all j in Jn (see above).
+*
+* RECOVERING SOLUTION
+*
+* None needed. */
+
+static int hidden_covering(NPP *npp, struct elem *ptr, double *_b)
+{ /* process inequality constraint: sum a[j] x[j] >= b;
+ 0 - specified row is NOT hidden covering inequality;
+ 1 - specified row is covering inequality;
+ 2 - specified row is hidden covering inequality. */
+ struct elem *e;
+ int neg;
+ double b = *_b, eps;
+ xassert(npp == npp);
+ /* a[j] must be non-zero, x[j] must be binary, for all j in J */
+ for (e = ptr; e != NULL; e = e->next)
+ { xassert(e->aj != 0.0);
+ xassert(e->xj->is_int);
+ xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
+ }
+ /* check if the specified inequality constraint already has the
+ form of covering inequality */
+ neg = 0; /* neg is |Jn| */
+ for (e = ptr; e != NULL; e = e->next)
+ { if (e->aj == +1.0)
+ ;
+ else if (e->aj == -1.0)
+ neg++;
+ else
+ break;
+ }
+ if (e == NULL)
+ { /* all coefficients a[j] are +1 or -1; check rhs b */
+ if (b == (double)(1 - neg))
+ { /* it is covering inequality; no processing is needed */
+ return 1;
+ }
+ }
+ /* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
+ positive; the result is a~[j] = |a[j]| and new rhs b */
+ for (e = ptr; e != NULL; e = e->next)
+ if (e->aj < 0) b -= e->aj;
+ /* now a[j] > 0 for all j in J (actually |a[j]| are used) */
+ /* if b <= 0, skip processing--this case must not appear */
+ if (b < 1e-3) return 0;
+ /* now a[j] > 0 for all j in J, and b > 0 */
+ /* the specified constraint is equivalent to covering inequality
+ iff a[j] >= b for all j in J */
+ eps = 1e-9 + 1e-12 * fabs(b);
+ for (e = ptr; e != NULL; e = e->next)
+ if (fabs(e->aj) < b - eps) return 0;
+ /* perform back substitution x~[j] = 1 - x[j] and construct the
+ final equivalent covering inequality in generalized format */
+ b = 1.0;
+ for (e = ptr; e != NULL; e = e->next)
+ { if (e->aj > 0.0)
+ e->aj = +1.0;
+ else /* e->aj < 0.0 */
+ e->aj = -1.0, b -= 1.0;
+ }
+ *_b = b;
+ return 2;
+}
+
+int npp_hidden_covering(NPP *npp, NPPROW *row)
+{ /* identify hidden covering inequality */
+ NPPROW *copy;
+ NPPAIJ *aij;
+ struct elem *ptr, *e;
+ int kase, ret, count = 0;
+ double b;
+ /* the row must be inequality constraint */
+ xassert(row->lb < row->ub);
+ for (kase = 0; kase <= 1; kase++)
+ { if (kase == 0)
+ { /* process row lower bound */
+ if (row->lb == -DBL_MAX) continue;
+ ptr = copy_form(npp, row, +1.0);
+ b = + row->lb;
+ }
+ else
+ { /* process row upper bound */
+ if (row->ub == +DBL_MAX) continue;
+ ptr = copy_form(npp, row, -1.0);
+ b = - row->ub;
+ }
+ /* now the inequality has the form "sum a[j] x[j] >= b" */
+ ret = hidden_covering(npp, ptr, &b);
+ xassert(0 <= ret && ret <= 2);
+ if (kase == 1 && ret == 1 || ret == 2)
+ { /* the original inequality has been identified as hidden
+ covering inequality */
+ count++;
+#ifdef GLP_DEBUG
+ xprintf("Original constraint:\n");
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ xprintf(" %+g x%d", aij->val, aij->col->j);
+ if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
+ if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
+ xprintf("\n");
+ xprintf("Equivalent covering inequality:\n");
+ for (e = ptr; e != NULL; e = e->next)
+ xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
+ xprintf(", >= %g\n", b);
+#endif
+ if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
+ { /* the original row is single-sided inequality; no copy
+ is needed */
+ copy = NULL;
+ }
+ else
+ { /* the original row is double-sided inequality; we need
+ to create its copy for other bound before replacing it
+ with the equivalent inequality */
+ copy = npp_add_row(npp);
+ if (kase == 0)
+ { /* the copy is for upper bound */
+ copy->lb = -DBL_MAX, copy->ub = row->ub;
+ }
+ else
+ { /* the copy is for lower bound */
+ copy->lb = row->lb, copy->ub = +DBL_MAX;
+ }
+ /* copy original row coefficients */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_add_aij(npp, copy, aij->col, aij->val);
+ }
+ /* replace the original inequality by equivalent one */
+ npp_erase_row(npp, row);
+ row->lb = b, row->ub = +DBL_MAX;
+ for (e = ptr; e != NULL; e = e->next)
+ npp_add_aij(npp, row, e->xj, e->aj);
+ /* continue processing upper bound for the copy */
+ if (copy != NULL) row = copy;
+ }
+ drop_form(npp, ptr);
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_is_partitioning - test if constraint is partitioning equality
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_is_partitioning(NPP *npp, NPPROW *row);
+*
+* RETURNS
+*
+* If the specified row (constraint) is partitioning equality (see
+* below), the routine npp_is_partitioning returns non-zero. Otherwise,
+* it returns zero.
+*
+* PARTITIONING EQUALITIES
+*
+* In canonical format the partitioning equality is the following:
+*
+* sum x[j] = 1, (1)
+* j in J
+*
+* where all variables x[j] are binary. This equality expresses the
+* condition that in any integer feasible solution exactly one variable
+* in set J must take non-zero (unity) value while other variables must
+* be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because if
+* J is empty, the inequality (1) is infeasible, and if |J| = 1, the
+* inequality (1) is a fixing row.
+*
+* In general case the partitioning equality may include original
+* variables x[j] as well as their complements x~[j]:
+*
+* sum x[j] + sum x~[j] = 1, (2)
+* j in Jp j in Jn
+*
+* where Jp and Jn are not intersected. Therefore, using substitution
+* x~[j] = 1 - x[j] leads to the partitioning equality in generalized
+* format:
+*
+* sum x[j] - sum x[j] = 1 - |Jn|. (3)
+* j in Jp j in Jn */
+
+int npp_is_partitioning(NPP *npp, NPPROW *row)
+{ /* test if constraint is partitioning equality */
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int b;
+ xassert(npp == npp);
+ if (row->lb != row->ub) return 0;
+ b = 1;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ return 0;
+ if (aij->val == +1.0)
+ ;
+ else if (aij->val == -1.0)
+ b--;
+ else
+ return 0;
+ }
+ if (row->lb != (double)b) return 0;
+ return 1;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_reduce_ineq_coef - reduce inequality constraint coefficients
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_reduce_ineq_coef(NPP *npp, NPPROW *row);
+*
+* DESCRIPTION
+*
+* The routine npp_reduce_ineq_coef processes specified inequality
+* constraint attempting to replace it by an equivalent constraint,
+* where magnitude of coefficients at binary variables is smaller than
+* in the original constraint. If the inequality is double-sided, it is
+* replaced by a pair of single-sided inequalities, if necessary.
+*
+* RETURNS
+*
+* The routine npp_reduce_ineq_coef returns the number of coefficients
+* reduced.
+*
+* BACKGROUND
+*
+* Consider an inequality constraint:
+*
+* sum a[j] x[j] >= b. (1)
+* j in J
+*
+* (In case of '<=' inequality it can be transformed to '>=' format by
+* multiplying both its sides by -1.) Let x[k] be a binary variable;
+* other variables can be integer as well as continuous. We can write
+* constraint (1) as follows:
+*
+* a[k] x[k] + t[k] >= b, (2)
+*
+* where:
+*
+* t[k] = sum a[j] x[j]. (3)
+* j in J\{k}
+*
+* Since x[k] is binary, constraint (2) is equivalent to disjunction of
+* the following two constraints:
+*
+* x[k] = 0, t[k] >= b (4)
+*
+* OR
+*
+* x[k] = 1, t[k] >= b - a[k]. (5)
+*
+* Let also that for the partial sum t[k] be known some its implied
+* lower bound inf t[k].
+*
+* Case a[k] > 0. Let inf t[k] < b, since otherwise both constraints
+* (4) and (5) and therefore constraint (2) are redundant.
+* If inf t[k] > b - a[k], only constraint (5) is redundant, in which
+* case it can be replaced with the following redundant and therefore
+* equivalent constraint:
+*
+* t[k] >= b - a'[k] = inf t[k], (6)
+*
+* where:
+*
+* a'[k] = b - inf t[k]. (7)
+*
+* Thus, the original constraint (2) is equivalent to the following
+* constraint with coefficient at variable x[k] changed:
+*
+* a'[k] x[k] + t[k] >= b. (8)
+*
+* From inf t[k] < b it follows that a'[k] > 0, i.e. the coefficient
+* at x[k] keeps its sign. And from inf t[k] > b - a[k] it follows that
+* a'[k] < a[k], i.e. the coefficient reduces in magnitude.
+*
+* Case a[k] < 0. Let inf t[k] < b - a[k], since otherwise both
+* constraints (4) and (5) and therefore constraint (2) are redundant.
+* If inf t[k] > b, only constraint (4) is redundant, in which case it
+* can be replaced with the following redundant and therefore equivalent
+* constraint:
+*
+* t[k] >= b' = inf t[k]. (9)
+*
+* Rewriting constraint (5) as follows:
+*
+* t[k] >= b - a[k] = b' - a'[k], (10)
+*
+* where:
+*
+* a'[k] = a[k] + b' - b = a[k] + inf t[k] - b, (11)
+*
+* we can see that disjunction of constraint (9) and (10) is equivalent
+* to disjunction of constraint (4) and (5), from which it follows that
+* the original constraint (2) is equivalent to the following constraint
+* with both coefficient at variable x[k] and right-hand side changed:
+*
+* a'[k] x[k] + t[k] >= b'. (12)
+*
+* From inf t[k] < b - a[k] it follows that a'[k] < 0, i.e. the
+* coefficient at x[k] keeps its sign. And from inf t[k] > b it follows
+* that a'[k] > a[k], i.e. the coefficient reduces in magnitude.
+*
+* PROBLEM TRANSFORMATION
+*
+* In the routine npp_reduce_ineq_coef the following implied lower
+* bound of the partial sum (3) is used:
+*
+* inf t[k] = sum a[j] l[j] + sum a[j] u[j], (13)
+* j in Jp\{k} k in Jn\{k}
+*
+* where Jp = {j : a[j] > 0}, Jn = {j : a[j] < 0}, l[j] and u[j] are
+* lower and upper bounds, resp., of variable x[j].
+*
+* In order to compute inf t[k] more efficiently, the following formula,
+* which is equivalent to (13), is actually used:
+*
+* ( h - a[k] l[k] = h, if a[k] > 0,
+* inf t[k] = < (14)
+* ( h - a[k] u[k] = h - a[k], if a[k] < 0,
+*
+* where:
+*
+* h = sum a[j] l[j] + sum a[j] u[j] (15)
+* j in Jp j in Jn
+*
+* is the implied lower bound of row (1).
+*
+* Reduction of positive coefficient (a[k] > 0) does not change value
+* of h, since l[k] = 0. In case of reduction of negative coefficient
+* (a[k] < 0) from (11) it follows that:
+*
+* delta a[k] = a'[k] - a[k] = inf t[k] - b (> 0), (16)
+*
+* so new value of h (accounting that u[k] = 1) can be computed as
+* follows:
+*
+* h := h + delta a[k] = h + (inf t[k] - b). (17)
+*
+* RECOVERING SOLUTION
+*
+* None needed. */
+
+static int reduce_ineq_coef(NPP *npp, struct elem *ptr, double *_b)
+{ /* process inequality constraint: sum a[j] x[j] >= b */
+ /* returns: the number of coefficients reduced */
+ struct elem *e;
+ int count = 0;
+ double h, inf_t, new_a, b = *_b;
+ xassert(npp == npp);
+ /* compute h; see (15) */
+ h = 0.0;
+ for (e = ptr; e != NULL; e = e->next)
+ { if (e->aj > 0.0)
+ { if (e->xj->lb == -DBL_MAX) goto done;
+ h += e->aj * e->xj->lb;
+ }
+ else /* e->aj < 0.0 */
+ { if (e->xj->ub == +DBL_MAX) goto done;
+ h += e->aj * e->xj->ub;
+ }
+ }
+ /* perform reduction of coefficients at binary variables */
+ for (e = ptr; e != NULL; e = e->next)
+ { /* skip non-binary variable */
+ if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
+ continue;
+ if (e->aj > 0.0)
+ { /* compute inf t[k]; see (14) */
+ inf_t = h;
+ if (b - e->aj < inf_t && inf_t < b)
+ { /* compute reduced coefficient a'[k]; see (7) */
+ new_a = b - inf_t;
+ if (new_a >= +1e-3 &&
+ e->aj - new_a >= 0.01 * (1.0 + e->aj))
+ { /* accept a'[k] */
+#ifdef GLP_DEBUG
+ xprintf("+");
+#endif
+ e->aj = new_a;
+ count++;
+ }
+ }
+ }
+ else /* e->aj < 0.0 */
+ { /* compute inf t[k]; see (14) */
+ inf_t = h - e->aj;
+ if (b < inf_t && inf_t < b - e->aj)
+ { /* compute reduced coefficient a'[k]; see (11) */
+ new_a = e->aj + (inf_t - b);
+ if (new_a <= -1e-3 &&
+ new_a - e->aj >= 0.01 * (1.0 - e->aj))
+ { /* accept a'[k] */
+#ifdef GLP_DEBUG
+ xprintf("-");
+#endif
+ e->aj = new_a;
+ /* update h; see (17) */
+ h += (inf_t - b);
+ /* compute b'; see (9) */
+ b = inf_t;
+ count++;
+ }
+ }
+ }
+ }
+ *_b = b;
+done: return count;
+}
+
+int npp_reduce_ineq_coef(NPP *npp, NPPROW *row)
+{ /* reduce inequality constraint coefficients */
+ NPPROW *copy;
+ NPPAIJ *aij;
+ struct elem *ptr, *e;
+ int kase, count[2];
+ double b;
+ /* the row must be inequality constraint */
+ xassert(row->lb < row->ub);
+ count[0] = count[1] = 0;
+ for (kase = 0; kase <= 1; kase++)
+ { if (kase == 0)
+ { /* process row lower bound */
+ if (row->lb == -DBL_MAX) continue;
+#ifdef GLP_DEBUG
+ xprintf("L");
+#endif
+ ptr = copy_form(npp, row, +1.0);
+ b = + row->lb;
+ }
+ else
+ { /* process row upper bound */
+ if (row->ub == +DBL_MAX) continue;
+#ifdef GLP_DEBUG
+ xprintf("U");
+#endif
+ ptr = copy_form(npp, row, -1.0);
+ b = - row->ub;
+ }
+ /* now the inequality has the form "sum a[j] x[j] >= b" */
+ count[kase] = reduce_ineq_coef(npp, ptr, &b);
+ if (count[kase] > 0)
+ { /* the original inequality has been replaced by equivalent
+ one with coefficients reduced */
+ if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
+ { /* the original row is single-sided inequality; no copy
+ is needed */
+ copy = NULL;
+ }
+ else
+ { /* the original row is double-sided inequality; we need
+ to create its copy for other bound before replacing it
+ with the equivalent inequality */
+#ifdef GLP_DEBUG
+ xprintf("*");
+#endif
+ copy = npp_add_row(npp);
+ if (kase == 0)
+ { /* the copy is for upper bound */
+ copy->lb = -DBL_MAX, copy->ub = row->ub;
+ }
+ else
+ { /* the copy is for lower bound */
+ copy->lb = row->lb, copy->ub = +DBL_MAX;
+ }
+ /* copy original row coefficients */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_add_aij(npp, copy, aij->col, aij->val);
+ }
+ /* replace the original inequality by equivalent one */
+ npp_erase_row(npp, row);
+ row->lb = b, row->ub = +DBL_MAX;
+ for (e = ptr; e != NULL; e = e->next)
+ npp_add_aij(npp, row, e->xj, e->aj);
+ /* continue processing upper bound for the copy */
+ if (copy != NULL) row = copy;
+ }
+ drop_form(npp, ptr);
+ }
+ return count[0] + count[1];
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp5.c b/test/monniaux/glpk-4.65/src/npp/npp5.c
new file mode 100644
index 00000000..2fad496d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp5.c
@@ -0,0 +1,809 @@
+/* npp5.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2009-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+/***********************************************************************
+* NAME
+*
+* npp_clean_prob - perform initial LP/MIP processing
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* void npp_clean_prob(NPP *npp);
+*
+* DESCRIPTION
+*
+* The routine npp_clean_prob performs initial LP/MIP processing that
+* currently includes:
+*
+* 1) removing free rows;
+*
+* 2) replacing double-sided constraint rows with almost identical
+* bounds, by equality constraint rows;
+*
+* 3) removing fixed columns;
+*
+* 4) replacing double-bounded columns with almost identical bounds by
+* fixed columns and removing those columns;
+*
+* 5) initial processing constraint coefficients (not implemented);
+*
+* 6) initial processing objective coefficients (not implemented). */
+
+void npp_clean_prob(NPP *npp)
+{ /* perform initial LP/MIP processing */
+ NPPROW *row, *next_row;
+ NPPCOL *col, *next_col;
+ int ret;
+ xassert(npp == npp);
+ /* process rows which originally are free */
+ for (row = npp->r_head; row != NULL; row = next_row)
+ { next_row = row->next;
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ { /* process free row */
+#ifdef GLP_DEBUG
+ xprintf("1");
+#endif
+ npp_free_row(npp, row);
+ /* row was deleted */
+ }
+ }
+ /* process rows which originally are double-sided inequalities */
+ for (row = npp->r_head; row != NULL; row = next_row)
+ { next_row = row->next;
+ if (row->lb != -DBL_MAX && row->ub != +DBL_MAX &&
+ row->lb < row->ub)
+ { ret = npp_make_equality(npp, row);
+ if (ret == 0)
+ ;
+ else if (ret == 1)
+ { /* row was replaced by equality constraint */
+#ifdef GLP_DEBUG
+ xprintf("2");
+#endif
+ }
+ else
+ xassert(ret != ret);
+ }
+ }
+ /* process columns which are originally fixed */
+ for (col = npp->c_head; col != NULL; col = next_col)
+ { next_col = col->next;
+ if (col->lb == col->ub)
+ { /* process fixed column */
+#ifdef GLP_DEBUG
+ xprintf("3");
+#endif
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ }
+ }
+ /* process columns which are originally double-bounded */
+ for (col = npp->c_head; col != NULL; col = next_col)
+ { next_col = col->next;
+ if (col->lb != -DBL_MAX && col->ub != +DBL_MAX &&
+ col->lb < col->ub)
+ { ret = npp_make_fixed(npp, col);
+ if (ret == 0)
+ ;
+ else if (ret == 1)
+ { /* column was replaced by fixed column; process it */
+#ifdef GLP_DEBUG
+ xprintf("4");
+#endif
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_process_row - perform basic row processing
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_process_row(NPP *npp, NPPROW *row, int hard);
+*
+* DESCRIPTION
+*
+* The routine npp_process_row performs basic row processing that
+* currently includes:
+*
+* 1) removing empty row;
+*
+* 2) removing equality constraint row singleton and corresponding
+* column;
+*
+* 3) removing inequality constraint row singleton and corresponding
+* column if it was fixed;
+*
+* 4) performing general row analysis;
+*
+* 5) removing redundant row bounds;
+*
+* 6) removing forcing row and corresponding columns;
+*
+* 7) removing row which becomes free due to redundant bounds;
+*
+* 8) computing implied bounds for all columns in the row and using
+* them to strengthen current column bounds (MIP only, optional,
+* performed if the flag hard is on).
+*
+* Additionally the routine may activate affected rows and/or columns
+* for further processing.
+*
+* RETURNS
+*
+* 0 success;
+*
+* GLP_ENOPFS primal/integer infeasibility detected;
+*
+* GLP_ENODFS dual infeasibility detected. */
+
+int npp_process_row(NPP *npp, NPPROW *row, int hard)
+{ /* perform basic row processing */
+ NPPCOL *col;
+ NPPAIJ *aij, *next_aij, *aaa;
+ int ret;
+ /* row must not be free */
+ xassert(!(row->lb == -DBL_MAX && row->ub == +DBL_MAX));
+ /* start processing row */
+ if (row->ptr == NULL)
+ { /* empty row */
+ ret = npp_empty_row(npp, row);
+ if (ret == 0)
+ { /* row was deleted */
+#ifdef GLP_DEBUG
+ xprintf("A");
+#endif
+ return 0;
+ }
+ else if (ret == 1)
+ { /* primal infeasibility */
+ return GLP_ENOPFS;
+ }
+ else
+ xassert(ret != ret);
+ }
+ if (row->ptr->r_next == NULL)
+ { /* row singleton */
+ col = row->ptr->col;
+ if (row->lb == row->ub)
+ { /* equality constraint */
+ ret = npp_eq_singlet(npp, row);
+ if (ret == 0)
+ { /* column was fixed, row was deleted */
+#ifdef GLP_DEBUG
+ xprintf("B");
+#endif
+ /* activate rows affected by column */
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ npp_activate_row(npp, aij->row);
+ /* process fixed column */
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ return 0;
+ }
+ else if (ret == 1 || ret == 2)
+ { /* primal/integer infeasibility */
+ return GLP_ENOPFS;
+ }
+ else
+ xassert(ret != ret);
+ }
+ else
+ { /* inequality constraint */
+ ret = npp_ineq_singlet(npp, row);
+ if (0 <= ret && ret <= 3)
+ { /* row was deleted */
+#ifdef GLP_DEBUG
+ xprintf("C");
+#endif
+ /* activate column, since its length was changed due to
+ row deletion */
+ npp_activate_col(npp, col);
+ if (ret >= 2)
+ { /* column bounds changed significantly or column was
+ fixed */
+ /* activate rows affected by column */
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ npp_activate_row(npp, aij->row);
+ }
+ if (ret == 3)
+ { /* column was fixed; process it */
+#ifdef GLP_DEBUG
+ xprintf("D");
+#endif
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ }
+ return 0;
+ }
+ else if (ret == 4)
+ { /* primal infeasibility */
+ return GLP_ENOPFS;
+ }
+ else
+ xassert(ret != ret);
+ }
+ }
+#if 0
+ /* sometimes this causes too large round-off errors; probably
+ pivot coefficient should be chosen more carefully */
+ if (row->ptr->r_next->r_next == NULL)
+ { /* row doubleton */
+ if (row->lb == row->ub)
+ { /* equality constraint */
+ if (!(row->ptr->col->is_int ||
+ row->ptr->r_next->col->is_int))
+ { /* both columns are continuous */
+ NPPCOL *q;
+ q = npp_eq_doublet(npp, row);
+ if (q != NULL)
+ { /* column q was eliminated */
+#ifdef GLP_DEBUG
+ xprintf("E");
+#endif
+ /* now column q is singleton of type "implied slack
+ variable"; we process it here to make sure that on
+ recovering basic solution the row is always active
+ equality constraint (as required by the routine
+ rcv_eq_doublet) */
+ xassert(npp_process_col(npp, q) == 0);
+ /* column q was deleted; note that row p also may be
+ deleted */
+ return 0;
+ }
+ }
+ }
+ }
+#endif
+ /* general row analysis */
+ ret = npp_analyze_row(npp, row);
+ xassert(0x00 <= ret && ret <= 0xFF);
+ if (ret == 0x33)
+ { /* row bounds are inconsistent with column bounds */
+ return GLP_ENOPFS;
+ }
+ if ((ret & 0x0F) == 0x00)
+ { /* row lower bound does not exist or redundant */
+ if (row->lb != -DBL_MAX)
+ { /* remove redundant row lower bound */
+#ifdef GLP_DEBUG
+ xprintf("F");
+#endif
+ npp_inactive_bound(npp, row, 0);
+ }
+ }
+ else if ((ret & 0x0F) == 0x01)
+ { /* row lower bound can be active */
+ /* see below */
+ }
+ else if ((ret & 0x0F) == 0x02)
+ { /* row lower bound is a forcing bound */
+#ifdef GLP_DEBUG
+ xprintf("G");
+#endif
+ /* process forcing row */
+ if (npp_forcing_row(npp, row, 0) == 0)
+fixup: { /* columns were fixed, row was made free */
+ for (aij = row->ptr; aij != NULL; aij = next_aij)
+ { /* process column fixed by forcing row */
+#ifdef GLP_DEBUG
+ xprintf("H");
+#endif
+ col = aij->col;
+ next_aij = aij->r_next;
+ /* activate rows affected by column */
+ for (aaa = col->ptr; aaa != NULL; aaa = aaa->c_next)
+ npp_activate_row(npp, aaa->row);
+ /* process fixed column */
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ }
+ /* process free row (which now is empty due to deletion of
+ all its columns) */
+ npp_free_row(npp, row);
+ /* row was deleted */
+ return 0;
+ }
+ }
+ else
+ xassert(ret != ret);
+ if ((ret & 0xF0) == 0x00)
+ { /* row upper bound does not exist or redundant */
+ if (row->ub != +DBL_MAX)
+ { /* remove redundant row upper bound */
+#ifdef GLP_DEBUG
+ xprintf("I");
+#endif
+ npp_inactive_bound(npp, row, 1);
+ }
+ }
+ else if ((ret & 0xF0) == 0x10)
+ { /* row upper bound can be active */
+ /* see below */
+ }
+ else if ((ret & 0xF0) == 0x20)
+ { /* row upper bound is a forcing bound */
+#ifdef GLP_DEBUG
+ xprintf("J");
+#endif
+ /* process forcing row */
+ if (npp_forcing_row(npp, row, 1) == 0) goto fixup;
+ }
+ else
+ xassert(ret != ret);
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ { /* row became free due to redundant bounds removal */
+#ifdef GLP_DEBUG
+ xprintf("K");
+#endif
+ /* activate its columns, since their length will change due
+ to row deletion */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_activate_col(npp, aij->col);
+ /* process free row */
+ npp_free_row(npp, row);
+ /* row was deleted */
+ return 0;
+ }
+#if 1 /* 23/XII-2009 */
+ /* row lower and/or upper bounds can be active */
+ if (npp->sol == GLP_MIP && hard)
+ { /* improve current column bounds (optional) */
+ if (npp_improve_bounds(npp, row, 1) < 0)
+ return GLP_ENOPFS;
+ }
+#endif
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_improve_bounds - improve current column bounds
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_improve_bounds(NPP *npp, NPPROW *row, int flag);
+*
+* DESCRIPTION
+*
+* The routine npp_improve_bounds analyzes specified row (inequality
+* or equality constraint) to determine implied column bounds and then
+* uses these bounds to improve (strengthen) current column bounds.
+*
+* If the flag is on and current column bounds changed significantly
+* or the column was fixed, the routine activate rows affected by the
+* column for further processing. (This feature is intended to be used
+* in the main loop of the routine npp_process_row.)
+*
+* NOTE: This operation can be used for MIP problem only.
+*
+* RETURNS
+*
+* The routine npp_improve_bounds returns the number of significantly
+* changed bounds plus the number of column having been fixed due to
+* bound improvements. However, if the routine detects primal/integer
+* infeasibility, it returns a negative value. */
+
+int npp_improve_bounds(NPP *npp, NPPROW *row, int flag)
+{ /* improve current column bounds */
+ NPPCOL *col;
+ NPPAIJ *aij, *next_aij, *aaa;
+ int kase, ret, count = 0;
+ double lb, ub;
+ xassert(npp->sol == GLP_MIP);
+ /* row must not be free */
+ xassert(!(row->lb == -DBL_MAX && row->ub == +DBL_MAX));
+ /* determine implied column bounds */
+ npp_implied_bounds(npp, row);
+ /* and use these bounds to strengthen current column bounds */
+ for (aij = row->ptr; aij != NULL; aij = next_aij)
+ { col = aij->col;
+ next_aij = aij->r_next;
+ for (kase = 0; kase <= 1; kase++)
+ { /* save current column bounds */
+ lb = col->lb, ub = col->ub;
+ if (kase == 0)
+ { /* process implied column lower bound */
+ if (col->ll.ll == -DBL_MAX) continue;
+ ret = npp_implied_lower(npp, col, col->ll.ll);
+ }
+ else
+ { /* process implied column upper bound */
+ if (col->uu.uu == +DBL_MAX) continue;
+ ret = npp_implied_upper(npp, col, col->uu.uu);
+ }
+ if (ret == 0 || ret == 1)
+ { /* current column bounds did not change or changed, but
+ not significantly; restore current column bounds */
+ col->lb = lb, col->ub = ub;
+ }
+ else if (ret == 2 || ret == 3)
+ { /* current column bounds changed significantly or column
+ was fixed */
+#ifdef GLP_DEBUG
+ xprintf("L");
+#endif
+ count++;
+ /* activate other rows affected by column, if required */
+ if (flag)
+ { for (aaa = col->ptr; aaa != NULL; aaa = aaa->c_next)
+ { if (aaa->row != row)
+ npp_activate_row(npp, aaa->row);
+ }
+ }
+ if (ret == 3)
+ { /* process fixed column */
+#ifdef GLP_DEBUG
+ xprintf("M");
+#endif
+ npp_fixed_col(npp, col);
+ /* column was deleted */
+ break; /* for kase */
+ }
+ }
+ else if (ret == 4)
+ { /* primal/integer infeasibility */
+ return -1;
+ }
+ else
+ xassert(ret != ret);
+ }
+ }
+ return count;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_process_col - perform basic column processing
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_process_col(NPP *npp, NPPCOL *col);
+*
+* DESCRIPTION
+*
+* The routine npp_process_col performs basic column processing that
+* currently includes:
+*
+* 1) fixing and removing empty column;
+*
+* 2) removing column singleton, which is implied slack variable, and
+* corresponding row if it becomes free;
+*
+* 3) removing bounds of column, which is implied free variable, and
+* replacing corresponding row by equality constraint.
+*
+* Additionally the routine may activate affected rows and/or columns
+* for further processing.
+*
+* RETURNS
+*
+* 0 success;
+*
+* GLP_ENOPFS primal/integer infeasibility detected;
+*
+* GLP_ENODFS dual infeasibility detected. */
+
+int npp_process_col(NPP *npp, NPPCOL *col)
+{ /* perform basic column processing */
+ NPPROW *row;
+ NPPAIJ *aij;
+ int ret;
+ /* column must not be fixed */
+ xassert(col->lb < col->ub);
+ /* start processing column */
+ if (col->ptr == NULL)
+ { /* empty column */
+ ret = npp_empty_col(npp, col);
+ if (ret == 0)
+ { /* column was fixed and deleted */
+#ifdef GLP_DEBUG
+ xprintf("N");
+#endif
+ return 0;
+ }
+ else if (ret == 1)
+ { /* dual infeasibility */
+ return GLP_ENODFS;
+ }
+ else
+ xassert(ret != ret);
+ }
+ if (col->ptr->c_next == NULL)
+ { /* column singleton */
+ row = col->ptr->row;
+ if (row->lb == row->ub)
+ { /* equality constraint */
+ if (!col->is_int)
+slack: { /* implied slack variable */
+#ifdef GLP_DEBUG
+ xprintf("O");
+#endif
+ npp_implied_slack(npp, col);
+ /* column was deleted */
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ { /* row became free due to implied slack variable */
+#ifdef GLP_DEBUG
+ xprintf("P");
+#endif
+ /* activate columns affected by row */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_activate_col(npp, aij->col);
+ /* process free row */
+ npp_free_row(npp, row);
+ /* row was deleted */
+ }
+ else
+ { /* row became inequality constraint; activate it
+ since its length changed due to column deletion */
+ npp_activate_row(npp, row);
+ }
+ return 0;
+ }
+ }
+ else
+ { /* inequality constraint */
+ if (!col->is_int)
+ { ret = npp_implied_free(npp, col);
+ if (ret == 0)
+ { /* implied free variable */
+#ifdef GLP_DEBUG
+ xprintf("Q");
+#endif
+ /* column bounds were removed, row was replaced by
+ equality constraint */
+ goto slack;
+ }
+ else if (ret == 1)
+ { /* column is not implied free variable, because its
+ lower and/or upper bounds can be active */
+ }
+ else if (ret == 2)
+ { /* dual infeasibility */
+ return GLP_ENODFS;
+ }
+ }
+ }
+ }
+ /* column still exists */
+ return 0;
+}
+
+/***********************************************************************
+* NAME
+*
+* npp_process_prob - perform basic LP/MIP processing
+*
+* SYNOPSIS
+*
+* #include "glpnpp.h"
+* int npp_process_prob(NPP *npp, int hard);
+*
+* DESCRIPTION
+*
+* The routine npp_process_prob performs basic LP/MIP processing that
+* currently includes:
+*
+* 1) initial LP/MIP processing (see the routine npp_clean_prob),
+*
+* 2) basic row processing (see the routine npp_process_row), and
+*
+* 3) basic column processing (see the routine npp_process_col).
+*
+* If the flag hard is on, the routine attempts to improve current
+* column bounds multiple times within the main processing loop, in
+* which case this feature may take a time. Otherwise, if the flag hard
+* is off, improving column bounds is performed only once at the end of
+* the main loop. (Note that this feature is used for MIP only.)
+*
+* The routine uses two sets: the set of active rows and the set of
+* active columns. Rows/columns are marked by a flag (the field temp in
+* NPPROW/NPPCOL). If the flag is non-zero, the row/column is active,
+* in which case it is placed in the beginning of the row/column list;
+* otherwise, if the flag is zero, the row/column is inactive, in which
+* case it is placed in the end of the row/column list. If a row/column
+* being currently processed may affect other rows/columns, the latters
+* are activated for further processing.
+*
+* RETURNS
+*
+* 0 success;
+*
+* GLP_ENOPFS primal/integer infeasibility detected;
+*
+* GLP_ENODFS dual infeasibility detected. */
+
+int npp_process_prob(NPP *npp, int hard)
+{ /* perform basic LP/MIP processing */
+ NPPROW *row;
+ NPPCOL *col;
+ int processing, ret;
+ /* perform initial LP/MIP processing */
+ npp_clean_prob(npp);
+ /* activate all remaining rows and columns */
+ for (row = npp->r_head; row != NULL; row = row->next)
+ row->temp = 1;
+ for (col = npp->c_head; col != NULL; col = col->next)
+ col->temp = 1;
+ /* main processing loop */
+ processing = 1;
+ while (processing)
+ { processing = 0;
+ /* process all active rows */
+ for (;;)
+ { row = npp->r_head;
+ if (row == NULL || !row->temp) break;
+ npp_deactivate_row(npp, row);
+ ret = npp_process_row(npp, row, hard);
+ if (ret != 0) goto done;
+ processing = 1;
+ }
+ /* process all active columns */
+ for (;;)
+ { col = npp->c_head;
+ if (col == NULL || !col->temp) break;
+ npp_deactivate_col(npp, col);
+ ret = npp_process_col(npp, col);
+ if (ret != 0) goto done;
+ processing = 1;
+ }
+ }
+#if 1 /* 23/XII-2009 */
+ if (npp->sol == GLP_MIP && !hard)
+ { /* improve current column bounds (optional) */
+ for (row = npp->r_head; row != NULL; row = row->next)
+ { if (npp_improve_bounds(npp, row, 0) < 0)
+ { ret = GLP_ENOPFS;
+ goto done;
+ }
+ }
+ }
+#endif
+ /* all seems ok */
+ ret = 0;
+done: xassert(ret == 0 || ret == GLP_ENOPFS || ret == GLP_ENODFS);
+#ifdef GLP_DEBUG
+ xprintf("\n");
+#endif
+ return ret;
+}
+
+/**********************************************************************/
+
+int npp_simplex(NPP *npp, const glp_smcp *parm)
+{ /* process LP prior to applying primal/dual simplex method */
+ int ret;
+ xassert(npp->sol == GLP_SOL);
+ xassert(parm == parm);
+ ret = npp_process_prob(npp, 0);
+ return ret;
+}
+
+/**********************************************************************/
+
+int npp_integer(NPP *npp, const glp_iocp *parm)
+{ /* process MIP prior to applying branch-and-bound method */
+ NPPROW *row, *prev_row;
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int count, ret;
+ xassert(npp->sol == GLP_MIP);
+ xassert(parm == parm);
+ /*==============================================================*/
+ /* perform basic MIP processing */
+ ret = npp_process_prob(npp, 1);
+ if (ret != 0) goto done;
+ /*==============================================================*/
+ /* binarize problem, if required */
+ if (parm->binarize)
+ npp_binarize_prob(npp);
+ /*==============================================================*/
+ /* identify hidden packing inequalities */
+ count = 0;
+ /* new rows will be added to the end of the row list, so we go
+ from the end to beginning of the row list */
+ for (row = npp->r_tail; row != NULL; row = prev_row)
+ { prev_row = row->prev;
+ /* skip free row */
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX) continue;
+ /* skip equality constraint */
+ if (row->lb == row->ub) continue;
+ /* skip row having less than two variables */
+ if (row->ptr == NULL || row->ptr->r_next == NULL) continue;
+ /* skip row having non-binary variables */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ break;
+ }
+ if (aij != NULL) continue;
+ count += npp_hidden_packing(npp, row);
+ }
+ if (count > 0)
+ xprintf("%d hidden packing inequaliti(es) were detected\n",
+ count);
+ /*==============================================================*/
+ /* identify hidden covering inequalities */
+ count = 0;
+ /* new rows will be added to the end of the row list, so we go
+ from the end to beginning of the row list */
+ for (row = npp->r_tail; row != NULL; row = prev_row)
+ { prev_row = row->prev;
+ /* skip free row */
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX) continue;
+ /* skip equality constraint */
+ if (row->lb == row->ub) continue;
+ /* skip row having less than three variables */
+ if (row->ptr == NULL || row->ptr->r_next == NULL ||
+ row->ptr->r_next->r_next == NULL) continue;
+ /* skip row having non-binary variables */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ break;
+ }
+ if (aij != NULL) continue;
+ count += npp_hidden_covering(npp, row);
+ }
+ if (count > 0)
+ xprintf("%d hidden covering inequaliti(es) were detected\n",
+ count);
+ /*==============================================================*/
+ /* reduce inequality constraint coefficients */
+ count = 0;
+ /* new rows will be added to the end of the row list, so we go
+ from the end to beginning of the row list */
+ for (row = npp->r_tail; row != NULL; row = prev_row)
+ { prev_row = row->prev;
+ /* skip equality constraint */
+ if (row->lb == row->ub) continue;
+ count += npp_reduce_ineq_coef(npp, row);
+ }
+ if (count > 0)
+ xprintf("%d constraint coefficient(s) were reduced\n", count);
+ /*==============================================================*/
+#ifdef GLP_DEBUG
+ routine(npp);
+#endif
+ /*==============================================================*/
+ /* all seems ok */
+ ret = 0;
+done: return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/npp/npp6.c b/test/monniaux/glpk-4.65/src/npp/npp6.c
new file mode 100644
index 00000000..b57f8615
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/npp/npp6.c
@@ -0,0 +1,1500 @@
+/* npp6.c (translate feasibility problem to CNF-SAT) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2011-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "npp.h"
+
+/***********************************************************************
+* npp_sat_free_row - process free (unbounded) row
+*
+* This routine processes row p, which is free (i.e. has no finite
+* bounds):
+*
+* -inf < sum a[p,j] x[j] < +inf. (1)
+*
+* The constraint (1) cannot be active and therefore it is redundant,
+* so the routine simply removes it from the original problem. */
+
+void npp_sat_free_row(NPP *npp, NPPROW *p)
+{ /* the row should be free */
+ xassert(p->lb == -DBL_MAX && p->ub == +DBL_MAX);
+ /* remove the row from the problem */
+ npp_del_row(npp, p);
+ return;
+}
+
+/***********************************************************************
+* npp_sat_fixed_col - process fixed column
+*
+* This routine processes column q, which is fixed:
+*
+* x[q] = s[q], (1)
+*
+* where s[q] is a fixed column value.
+*
+* The routine substitutes fixed value s[q] into constraint rows and
+* then removes column x[q] from the original problem.
+*
+* Substitution of x[q] = s[q] into row i gives:
+*
+* L[i] <= sum a[i,j] x[j] <= U[i] ==>
+* j
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
+* j!=q
+*
+* L[i] <= sum a[i,j] x[j] + a[i,q] s[q] <= U[i] ==>
+* j!=q
+*
+* L~[i] <= sum a[i,j] x[j] <= U~[i],
+* j!=q
+*
+* where
+*
+* L~[i] = L[i] - a[i,q] s[q], (2)
+*
+* U~[i] = U[i] - a[i,q] s[q] (3)
+*
+* are, respectively, lower and upper bound of row i in the transformed
+* problem.
+*
+* On recovering solution x[q] is assigned the value of s[q]. */
+
+struct sat_fixed_col
+{ /* fixed column */
+ int q;
+ /* column reference number for variable x[q] */
+ int s;
+ /* value, at which x[q] is fixed */
+};
+
+static int rcv_sat_fixed_col(NPP *, void *);
+
+int npp_sat_fixed_col(NPP *npp, NPPCOL *q)
+{ struct sat_fixed_col *info;
+ NPPROW *i;
+ NPPAIJ *aij;
+ int temp;
+ /* the column should be fixed */
+ xassert(q->lb == q->ub);
+ /* create transformation stack entry */
+ info = npp_push_tse(npp,
+ rcv_sat_fixed_col, sizeof(struct sat_fixed_col));
+ info->q = q->j;
+ info->s = (int)q->lb;
+ xassert((double)info->s == q->lb);
+ /* substitute x[q] = s[q] into constraint rows */
+ if (info->s == 0)
+ goto skip;
+ for (aij = q->ptr; aij != NULL; aij = aij->c_next)
+ { i = aij->row;
+ if (i->lb != -DBL_MAX)
+ { i->lb -= aij->val * (double)info->s;
+ temp = (int)i->lb;
+ if ((double)temp != i->lb)
+ return 1; /* integer arithmetic error */
+ }
+ if (i->ub != +DBL_MAX)
+ { i->ub -= aij->val * (double)info->s;
+ temp = (int)i->ub;
+ if ((double)temp != i->ub)
+ return 2; /* integer arithmetic error */
+ }
+ }
+skip: /* remove the column from the problem */
+ npp_del_col(npp, q);
+ return 0;
+}
+
+static int rcv_sat_fixed_col(NPP *npp, void *info_)
+{ struct sat_fixed_col *info = info_;
+ npp->c_value[info->q] = (double)info->s;
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_is_bin_comb - test if row is binary combination
+*
+* This routine tests if the specified row is a binary combination,
+* i.e. all its constraint coefficients are +1 and -1 and all variables
+* are binary. If the test was passed, the routine returns non-zero,
+* otherwise zero. */
+
+int npp_sat_is_bin_comb(NPP *npp, NPPROW *row)
+{ NPPCOL *col;
+ NPPAIJ *aij;
+ xassert(npp == npp);
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { if (!(aij->val == +1.0 || aij->val == -1.0))
+ return 0; /* non-unity coefficient */
+ col = aij->col;
+ if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
+ return 0; /* non-binary column */
+ }
+ return 1; /* test was passed */
+}
+
+/***********************************************************************
+* npp_sat_num_pos_coef - determine number of positive coefficients
+*
+* This routine returns the number of positive coefficients in the
+* specified row. */
+
+int npp_sat_num_pos_coef(NPP *npp, NPPROW *row)
+{ NPPAIJ *aij;
+ int num = 0;
+ xassert(npp == npp);
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->val > 0.0)
+ num++;
+ }
+ return num;
+}
+
+/***********************************************************************
+* npp_sat_num_neg_coef - determine number of negative coefficients
+*
+* This routine returns the number of negative coefficients in the
+* specified row. */
+
+int npp_sat_num_neg_coef(NPP *npp, NPPROW *row)
+{ NPPAIJ *aij;
+ int num = 0;
+ xassert(npp == npp);
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->val < 0.0)
+ num++;
+ }
+ return num;
+}
+
+/***********************************************************************
+* npp_sat_is_cover_ineq - test if row is covering inequality
+*
+* The canonical form of a covering inequality is the following:
+*
+* sum x[j] >= 1, (1)
+* j in J
+*
+* where all x[j] are binary variables.
+*
+* In general case a covering inequality may have one of the following
+* two forms:
+*
+* sum x[j] - sum x[j] >= 1 - |J-|, (2)
+* j in J+ j in J-
+*
+*
+* sum x[j] - sum x[j] <= |J+| - 1. (3)
+* j in J+ j in J-
+*
+* Obviously, the inequality (2) can be transformed to the form (1) by
+* substitution x[j] = 1 - x'[j] for all j in J-, where x'[j] is the
+* negation of variable x[j]. And the inequality (3) can be transformed
+* to (2) by multiplying both left- and right-hand sides by -1.
+*
+* This routine returns one of the following codes:
+*
+* 0, if the specified row is not a covering inequality;
+*
+* 1, if the specified row has the form (2);
+*
+* 2, if the specified row has the form (3). */
+
+int npp_sat_is_cover_ineq(NPP *npp, NPPROW *row)
+{ xassert(npp == npp);
+ if (row->lb != -DBL_MAX && row->ub == +DBL_MAX)
+ { /* row is inequality of '>=' type */
+ if (npp_sat_is_bin_comb(npp, row))
+ { /* row is a binary combination */
+ if (row->lb == 1.0 - npp_sat_num_neg_coef(npp, row))
+ { /* row has the form (2) */
+ return 1;
+ }
+ }
+ }
+ else if (row->lb == -DBL_MAX && row->ub != +DBL_MAX)
+ { /* row is inequality of '<=' type */
+ if (npp_sat_is_bin_comb(npp, row))
+ { /* row is a binary combination */
+ if (row->ub == npp_sat_num_pos_coef(npp, row) - 1.0)
+ { /* row has the form (3) */
+ return 2;
+ }
+ }
+ }
+ /* row is not a covering inequality */
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_is_pack_ineq - test if row is packing inequality
+*
+* The canonical form of a packing inequality is the following:
+*
+* sum x[j] <= 1, (1)
+* j in J
+*
+* where all x[j] are binary variables.
+*
+* In general case a packing inequality may have one of the following
+* two forms:
+*
+* sum x[j] - sum x[j] <= 1 - |J-|, (2)
+* j in J+ j in J-
+*
+*
+* sum x[j] - sum x[j] >= |J+| - 1. (3)
+* j in J+ j in J-
+*
+* Obviously, the inequality (2) can be transformed to the form (1) by
+* substitution x[j] = 1 - x'[j] for all j in J-, where x'[j] is the
+* negation of variable x[j]. And the inequality (3) can be transformed
+* to (2) by multiplying both left- and right-hand sides by -1.
+*
+* This routine returns one of the following codes:
+*
+* 0, if the specified row is not a packing inequality;
+*
+* 1, if the specified row has the form (2);
+*
+* 2, if the specified row has the form (3). */
+
+int npp_sat_is_pack_ineq(NPP *npp, NPPROW *row)
+{ xassert(npp == npp);
+ if (row->lb == -DBL_MAX && row->ub != +DBL_MAX)
+ { /* row is inequality of '<=' type */
+ if (npp_sat_is_bin_comb(npp, row))
+ { /* row is a binary combination */
+ if (row->ub == 1.0 - npp_sat_num_neg_coef(npp, row))
+ { /* row has the form (2) */
+ return 1;
+ }
+ }
+ }
+ else if (row->lb != -DBL_MAX && row->ub == +DBL_MAX)
+ { /* row is inequality of '>=' type */
+ if (npp_sat_is_bin_comb(npp, row))
+ { /* row is a binary combination */
+ if (row->lb == npp_sat_num_pos_coef(npp, row) - 1.0)
+ { /* row has the form (3) */
+ return 2;
+ }
+ }
+ }
+ /* row is not a packing inequality */
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_is_partn_eq - test if row is partitioning equality
+*
+* The canonical form of a partitioning equality is the following:
+*
+* sum x[j] = 1, (1)
+* j in J
+*
+* where all x[j] are binary variables.
+*
+* In general case a partitioning equality may have one of the following
+* two forms:
+*
+* sum x[j] - sum x[j] = 1 - |J-|, (2)
+* j in J+ j in J-
+*
+*
+* sum x[j] - sum x[j] = |J+| - 1. (3)
+* j in J+ j in J-
+*
+* Obviously, the equality (2) can be transformed to the form (1) by
+* substitution x[j] = 1 - x'[j] for all j in J-, where x'[j] is the
+* negation of variable x[j]. And the equality (3) can be transformed
+* to (2) by multiplying both left- and right-hand sides by -1.
+*
+* This routine returns one of the following codes:
+*
+* 0, if the specified row is not a partitioning equality;
+*
+* 1, if the specified row has the form (2);
+*
+* 2, if the specified row has the form (3). */
+
+int npp_sat_is_partn_eq(NPP *npp, NPPROW *row)
+{ xassert(npp == npp);
+ if (row->lb == row->ub)
+ { /* row is equality constraint */
+ if (npp_sat_is_bin_comb(npp, row))
+ { /* row is a binary combination */
+ if (row->lb == 1.0 - npp_sat_num_neg_coef(npp, row))
+ { /* row has the form (2) */
+ return 1;
+ }
+ if (row->ub == npp_sat_num_pos_coef(npp, row) - 1.0)
+ { /* row has the form (3) */
+ return 2;
+ }
+ }
+ }
+ /* row is not a partitioning equality */
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_reverse_row - multiply both sides of row by -1
+*
+* This routines multiplies by -1 both left- and right-hand sides of
+* the specified row:
+*
+* L <= sum x[j] <= U,
+*
+* that results in the following row:
+*
+* -U <= sum (-x[j]) <= -L.
+*
+* If no integer overflow occured, the routine returns zero, otherwise
+* non-zero. */
+
+int npp_sat_reverse_row(NPP *npp, NPPROW *row)
+{ NPPAIJ *aij;
+ int temp, ret = 0;
+ double old_lb, old_ub;
+ xassert(npp == npp);
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { aij->val = -aij->val;
+ temp = (int)aij->val;
+ if ((double)temp != aij->val)
+ ret = 1;
+ }
+ old_lb = row->lb, old_ub = row->ub;
+ if (old_ub == +DBL_MAX)
+ row->lb = -DBL_MAX;
+ else
+ { row->lb = -old_ub;
+ temp = (int)row->lb;
+ if ((double)temp != row->lb)
+ ret = 2;
+ }
+ if (old_lb == -DBL_MAX)
+ row->ub = +DBL_MAX;
+ else
+ { row->ub = -old_lb;
+ temp = (int)row->ub;
+ if ((double)temp != row->ub)
+ ret = 3;
+ }
+ return ret;
+}
+
+/***********************************************************************
+* npp_sat_split_pack - split packing inequality
+*
+* Let there be given a packing inequality in canonical form:
+*
+* sum t[j] <= 1, (1)
+* j in J
+*
+* where t[j] = x[j] or t[j] = 1 - x[j], x[j] is a binary variable.
+* And let J = J1 U J2 is a partition of the set of literals. Then the
+* inequality (1) is obviously equivalent to the following two packing
+* inequalities:
+*
+* sum t[j] <= y <--> sum t[j] + (1 - y) <= 1, (2)
+* j in J1 j in J1
+*
+* sum t[j] <= 1 - y <--> sum t[j] + y <= 1, (3)
+* j in J2 j in J2
+*
+* where y is a new binary variable added to the transformed problem.
+*
+* Assuming that the specified row is a packing inequality (1), this
+* routine constructs the set J1 by including there first nlit literals
+* (terms) from the specified row, and the set J2 = J \ J1. Then the
+* routine creates a new row, which corresponds to inequality (2), and
+* replaces the specified row with inequality (3). */
+
+NPPROW *npp_sat_split_pack(NPP *npp, NPPROW *row, int nlit)
+{ NPPROW *rrr;
+ NPPCOL *col;
+ NPPAIJ *aij;
+ int k;
+ /* original row should be packing inequality (1) */
+ xassert(npp_sat_is_pack_ineq(npp, row) == 1);
+ /* and nlit should be less than the number of literals (terms)
+ in the original row */
+ xassert(0 < nlit && nlit < npp_row_nnz(npp, row));
+ /* create new row corresponding to inequality (2) */
+ rrr = npp_add_row(npp);
+ rrr->lb = -DBL_MAX, rrr->ub = 1.0;
+ /* move first nlit literals (terms) from the original row to the
+ new row; the original row becomes inequality (3) */
+ for (k = 1; k <= nlit; k++)
+ { aij = row->ptr;
+ xassert(aij != NULL);
+ /* add literal to the new row */
+ npp_add_aij(npp, rrr, aij->col, aij->val);
+ /* correct rhs */
+ if (aij->val < 0.0)
+ rrr->ub -= 1.0, row->ub += 1.0;
+ /* remove literal from the original row */
+ npp_del_aij(npp, aij);
+ }
+ /* create new binary variable y */
+ col = npp_add_col(npp);
+ col->is_int = 1, col->lb = 0.0, col->ub = 1.0;
+ /* include literal (1 - y) in the new row */
+ npp_add_aij(npp, rrr, col, -1.0);
+ rrr->ub -= 1.0;
+ /* include literal y in the original row */
+ npp_add_aij(npp, row, col, +1.0);
+ return rrr;
+}
+
+/***********************************************************************
+* npp_sat_encode_pack - encode packing inequality
+*
+* Given a packing inequality in canonical form:
+*
+* sum t[j] <= 1, (1)
+* j in J
+*
+* where t[j] = x[j] or t[j] = 1 - x[j], x[j] is a binary variable,
+* this routine translates it to CNF by replacing it with the following
+* equivalent set of edge packing inequalities:
+*
+* t[j] + t[k] <= 1 for all j, k in J, j != k. (2)
+*
+* Then the routine transforms each edge packing inequality (2) to
+* corresponding covering inequality (that encodes two-literal clause)
+* by multiplying both its part by -1:
+*
+* - t[j] - t[k] >= -1 <--> (1 - t[j]) + (1 - t[k]) >= 1. (3)
+*
+* On exit the routine removes the original row from the problem. */
+
+void npp_sat_encode_pack(NPP *npp, NPPROW *row)
+{ NPPROW *rrr;
+ NPPAIJ *aij, *aik;
+ /* original row should be packing inequality (1) */
+ xassert(npp_sat_is_pack_ineq(npp, row) == 1);
+ /* create equivalent system of covering inequalities (3) */
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { /* due to symmetry only one of inequalities t[j] + t[k] <= 1
+ and t[k] <= t[j] <= 1 can be considered */
+ for (aik = aij->r_next; aik != NULL; aik = aik->r_next)
+ { /* create edge packing inequality (2) */
+ rrr = npp_add_row(npp);
+ rrr->lb = -DBL_MAX, rrr->ub = 1.0;
+ npp_add_aij(npp, rrr, aij->col, aij->val);
+ if (aij->val < 0.0)
+ rrr->ub -= 1.0;
+ npp_add_aij(npp, rrr, aik->col, aik->val);
+ if (aik->val < 0.0)
+ rrr->ub -= 1.0;
+ /* and transform it to covering inequality (3) */
+ npp_sat_reverse_row(npp, rrr);
+ xassert(npp_sat_is_cover_ineq(npp, rrr) == 1);
+ }
+ }
+ /* remove the original row from the problem */
+ npp_del_row(npp, row);
+ return;
+}
+
+/***********************************************************************
+* npp_sat_encode_sum2 - encode 2-bit summation
+*
+* Given a set containing two literals x and y this routine encodes
+* the equality
+*
+* x + y = s + 2 * c, (1)
+*
+* where
+*
+* s = (x + y) % 2 (2)
+*
+* is a binary variable modeling the low sum bit, and
+*
+* c = (x + y) / 2 (3)
+*
+* is a binary variable modeling the high (carry) sum bit. */
+
+void npp_sat_encode_sum2(NPP *npp, NPPLSE *set, NPPSED *sed)
+{ NPPROW *row;
+ int x, y, s, c;
+ /* the set should contain exactly two literals */
+ xassert(set != NULL);
+ xassert(set->next != NULL);
+ xassert(set->next->next == NULL);
+ sed->x = set->lit;
+ xassert(sed->x.neg == 0 || sed->x.neg == 1);
+ sed->y = set->next->lit;
+ xassert(sed->y.neg == 0 || sed->y.neg == 1);
+ sed->z.col = NULL, sed->z.neg = 0;
+ /* perform encoding s = (x + y) % 2 */
+ sed->s = npp_add_col(npp);
+ sed->s->is_int = 1, sed->s->lb = 0.0, sed->s->ub = 1.0;
+ for (x = 0; x <= 1; x++)
+ { for (y = 0; y <= 1; y++)
+ { for (s = 0; s <= 1; s++)
+ { if ((x + y) % 2 != s)
+ { /* generate CNF clause to disable infeasible
+ combination */
+ row = npp_add_row(npp);
+ row->lb = 1.0, row->ub = +DBL_MAX;
+ if (x == sed->x.neg)
+ npp_add_aij(npp, row, sed->x.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->x.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (y == sed->y.neg)
+ npp_add_aij(npp, row, sed->y.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->y.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (s == 0)
+ npp_add_aij(npp, row, sed->s, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->s, -1.0);
+ row->lb -= 1.0;
+ }
+ }
+ }
+ }
+ }
+ /* perform encoding c = (x + y) / 2 */
+ sed->c = npp_add_col(npp);
+ sed->c->is_int = 1, sed->c->lb = 0.0, sed->c->ub = 1.0;
+ for (x = 0; x <= 1; x++)
+ { for (y = 0; y <= 1; y++)
+ { for (c = 0; c <= 1; c++)
+ { if ((x + y) / 2 != c)
+ { /* generate CNF clause to disable infeasible
+ combination */
+ row = npp_add_row(npp);
+ row->lb = 1.0, row->ub = +DBL_MAX;
+ if (x == sed->x.neg)
+ npp_add_aij(npp, row, sed->x.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->x.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (y == sed->y.neg)
+ npp_add_aij(npp, row, sed->y.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->y.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (c == 0)
+ npp_add_aij(npp, row, sed->c, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->c, -1.0);
+ row->lb -= 1.0;
+ }
+ }
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* npp_sat_encode_sum3 - encode 3-bit summation
+*
+* Given a set containing at least three literals this routine chooses
+* some literals x, y, z from that set and encodes the equality
+*
+* x + y + z = s + 2 * c, (1)
+*
+* where
+*
+* s = (x + y + z) % 2 (2)
+*
+* is a binary variable modeling the low sum bit, and
+*
+* c = (x + y + z) / 2 (3)
+*
+* is a binary variable modeling the high (carry) sum bit. */
+
+void npp_sat_encode_sum3(NPP *npp, NPPLSE *set, NPPSED *sed)
+{ NPPROW *row;
+ int x, y, z, s, c;
+ /* the set should contain at least three literals */
+ xassert(set != NULL);
+ xassert(set->next != NULL);
+ xassert(set->next->next != NULL);
+ sed->x = set->lit;
+ xassert(sed->x.neg == 0 || sed->x.neg == 1);
+ sed->y = set->next->lit;
+ xassert(sed->y.neg == 0 || sed->y.neg == 1);
+ sed->z = set->next->next->lit;
+ xassert(sed->z.neg == 0 || sed->z.neg == 1);
+ /* perform encoding s = (x + y + z) % 2 */
+ sed->s = npp_add_col(npp);
+ sed->s->is_int = 1, sed->s->lb = 0.0, sed->s->ub = 1.0;
+ for (x = 0; x <= 1; x++)
+ { for (y = 0; y <= 1; y++)
+ { for (z = 0; z <= 1; z++)
+ { for (s = 0; s <= 1; s++)
+ { if ((x + y + z) % 2 != s)
+ { /* generate CNF clause to disable infeasible
+ combination */
+ row = npp_add_row(npp);
+ row->lb = 1.0, row->ub = +DBL_MAX;
+ if (x == sed->x.neg)
+ npp_add_aij(npp, row, sed->x.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->x.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (y == sed->y.neg)
+ npp_add_aij(npp, row, sed->y.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->y.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (z == sed->z.neg)
+ npp_add_aij(npp, row, sed->z.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->z.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (s == 0)
+ npp_add_aij(npp, row, sed->s, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->s, -1.0);
+ row->lb -= 1.0;
+ }
+ }
+ }
+ }
+ }
+ }
+ /* perform encoding c = (x + y + z) / 2 */
+ sed->c = npp_add_col(npp);
+ sed->c->is_int = 1, sed->c->lb = 0.0, sed->c->ub = 1.0;
+ for (x = 0; x <= 1; x++)
+ { for (y = 0; y <= 1; y++)
+ { for (z = 0; z <= 1; z++)
+ { for (c = 0; c <= 1; c++)
+ { if ((x + y + z) / 2 != c)
+ { /* generate CNF clause to disable infeasible
+ combination */
+ row = npp_add_row(npp);
+ row->lb = 1.0, row->ub = +DBL_MAX;
+ if (x == sed->x.neg)
+ npp_add_aij(npp, row, sed->x.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->x.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (y == sed->y.neg)
+ npp_add_aij(npp, row, sed->y.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->y.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (z == sed->z.neg)
+ npp_add_aij(npp, row, sed->z.col, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->z.col, -1.0);
+ row->lb -= 1.0;
+ }
+ if (c == 0)
+ npp_add_aij(npp, row, sed->c, +1.0);
+ else
+ { npp_add_aij(npp, row, sed->c, -1.0);
+ row->lb -= 1.0;
+ }
+ }
+ }
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* npp_sat_encode_sum_ax - encode linear combination of 0-1 variables
+*
+* PURPOSE
+*
+* Given a linear combination of binary variables:
+*
+* sum a[j] x[j], (1)
+* j
+*
+* which is the linear form of the specified row, this routine encodes
+* (i.e. translates to CNF) the following equality:
+*
+* n
+* sum |a[j]| t[j] = sum 2**(k-1) * y[k], (2)
+* j k=1
+*
+* where t[j] = x[j] (if a[j] > 0) or t[j] = 1 - x[j] (if a[j] < 0),
+* and y[k] is either t[j] or a new literal created by the routine or
+* a constant zero. Note that the sum in the right-hand side of (2) can
+* be thought as a n-bit representation of the sum in the left-hand
+* side, which is a non-negative integer number.
+*
+* ALGORITHM
+*
+* First, the number of bits, n, sufficient to represent any value in
+* the left-hand side of (2) is determined. Obviously, n is the number
+* of bits sufficient to represent the sum (sum |a[j]|).
+*
+* Let
+*
+* n
+* |a[j]| = sum 2**(k-1) b[j,k], (3)
+* k=1
+*
+* where b[j,k] is k-th bit in a n-bit representation of |a[j]|. Then
+*
+* m n
+* sum |a[j]| * t[j] = sum 2**(k-1) sum b[j,k] * t[j]. (4)
+* j k=1 j=1
+*
+* Introducing the set
+*
+* J[k] = { j : b[j,k] = 1 } (5)
+*
+* allows rewriting (4) as follows:
+*
+* n
+* sum |a[j]| * t[j] = sum 2**(k-1) sum t[j]. (6)
+* j k=1 j in J[k]
+*
+* Thus, our goal is to provide |J[k]| <= 1 for all k, in which case
+* we will have the representation (1).
+*
+* Let |J[k]| = 2, i.e. J[k] has exactly two literals u and v. In this
+* case we can apply the following transformation:
+*
+* u + v = s + 2 * c, (7)
+*
+* where s and c are, respectively, low (sum) and high (carry) bits of
+* the sum of two bits. This allows to replace two literals u and v in
+* J[k] by one literal s, and carry out literal c to J[k+1].
+*
+* If |J[k]| >= 3, i.e. J[k] has at least three literals u, v, and w,
+* we can apply the following transformation:
+*
+* u + v + w = s + 2 * c. (8)
+*
+* Again, literal s replaces literals u, v, and w in J[k], and literal
+* c goes into J[k+1].
+*
+* On exit the routine stores each literal from J[k] in element y[k],
+* 1 <= k <= n. If J[k] is empty, y[k] is set to constant false.
+*
+* RETURNS
+*
+* The routine returns n, the number of literals in the right-hand side
+* of (2), 0 <= n <= NBIT_MAX. If the sum (sum |a[j]|) is too large, so
+* more than NBIT_MAX (= 31) literals are needed to encode the original
+* linear combination, the routine returns a negative value. */
+
+#define NBIT_MAX 31
+/* maximal number of literals in the right hand-side of (2) */
+
+static NPPLSE *remove_lse(NPP *npp, NPPLSE *set, NPPCOL *col)
+{ /* remove specified literal from specified literal set */
+ NPPLSE *lse, *prev = NULL;
+ for (lse = set; lse != NULL; prev = lse, lse = lse->next)
+ if (lse->lit.col == col) break;
+ xassert(lse != NULL);
+ if (prev == NULL)
+ set = lse->next;
+ else
+ prev->next = lse->next;
+ dmp_free_atom(npp->pool, lse, sizeof(NPPLSE));
+ return set;
+}
+
+int npp_sat_encode_sum_ax(NPP *npp, NPPROW *row, NPPLIT y[])
+{ NPPAIJ *aij;
+ NPPLSE *set[1+NBIT_MAX], *lse;
+ NPPSED sed;
+ int k, n, temp;
+ double sum;
+ /* compute the sum (sum |a[j]|) */
+ sum = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ sum += fabs(aij->val);
+ /* determine n, the number of bits in the sum */
+ temp = (int)sum;
+ if ((double)temp != sum)
+ return -1; /* integer arithmetic error */
+ for (n = 0; temp > 0; n++, temp >>= 1);
+ xassert(0 <= n && n <= NBIT_MAX);
+ /* build initial sets J[k], 1 <= k <= n; see (5) */
+ /* set[k] is a pointer to the list of literals in J[k] */
+ for (k = 1; k <= n; k++)
+ set[k] = NULL;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { temp = (int)fabs(aij->val);
+ xassert((int)temp == fabs(aij->val));
+ for (k = 1; temp > 0; k++, temp >>= 1)
+ { if (temp & 1)
+ { xassert(k <= n);
+ lse = dmp_get_atom(npp->pool, sizeof(NPPLSE));
+ lse->lit.col = aij->col;
+ lse->lit.neg = (aij->val > 0.0 ? 0 : 1);
+ lse->next = set[k];
+ set[k] = lse;
+ }
+ }
+ }
+ /* main transformation loop */
+ for (k = 1; k <= n; k++)
+ { /* reduce J[k] and set y[k] */
+ for (;;)
+ { if (set[k] == NULL)
+ { /* J[k] is empty */
+ /* set y[k] to constant false */
+ y[k].col = NULL, y[k].neg = 0;
+ break;
+ }
+ if (set[k]->next == NULL)
+ { /* J[k] contains one literal */
+ /* set y[k] to that literal */
+ y[k] = set[k]->lit;
+ dmp_free_atom(npp->pool, set[k], sizeof(NPPLSE));
+ break;
+ }
+ if (set[k]->next->next == NULL)
+ { /* J[k] contains two literals */
+ /* apply transformation (7) */
+ npp_sat_encode_sum2(npp, set[k], &sed);
+ }
+ else
+ { /* J[k] contains at least three literals */
+ /* apply transformation (8) */
+ npp_sat_encode_sum3(npp, set[k], &sed);
+ /* remove third literal from set[k] */
+ set[k] = remove_lse(npp, set[k], sed.z.col);
+ }
+ /* remove second literal from set[k] */
+ set[k] = remove_lse(npp, set[k], sed.y.col);
+ /* remove first literal from set[k] */
+ set[k] = remove_lse(npp, set[k], sed.x.col);
+ /* include new literal s to set[k] */
+ lse = dmp_get_atom(npp->pool, sizeof(NPPLSE));
+ lse->lit.col = sed.s, lse->lit.neg = 0;
+ lse->next = set[k];
+ set[k] = lse;
+ /* include new literal c to set[k+1] */
+ xassert(k < n); /* FIXME: can "overflow" happen? */
+ lse = dmp_get_atom(npp->pool, sizeof(NPPLSE));
+ lse->lit.col = sed.c, lse->lit.neg = 0;
+ lse->next = set[k+1];
+ set[k+1] = lse;
+ }
+ }
+ return n;
+}
+
+/***********************************************************************
+* npp_sat_normalize_clause - normalize clause
+*
+* This routine normalizes the specified clause, which is a disjunction
+* of literals, by replacing multiple literals, which refer to the same
+* binary variable, with a single literal.
+*
+* On exit the routine returns the number of literals in the resulting
+* clause. However, if the specified clause includes both a literal and
+* its negation, the routine returns a negative value meaning that the
+* clause is equivalent to the value true. */
+
+int npp_sat_normalize_clause(NPP *npp, int size, NPPLIT lit[])
+{ int j, k, new_size;
+ xassert(npp == npp);
+ xassert(size >= 0);
+ new_size = 0;
+ for (k = 1; k <= size; k++)
+ { for (j = 1; j <= new_size; j++)
+ { if (lit[k].col == lit[j].col)
+ { /* lit[k] refers to the same variable as lit[j], which
+ is already included in the resulting clause */
+ if (lit[k].neg == lit[j].neg)
+ { /* ignore lit[k] due to the idempotent law */
+ goto skip;
+ }
+ else
+ { /* lit[k] is NOT lit[j]; the clause is equivalent to
+ the value true */
+ return -1;
+ }
+ }
+ }
+ /* include lit[k] in the resulting clause */
+ lit[++new_size] = lit[k];
+skip: ;
+ }
+ return new_size;
+}
+
+/***********************************************************************
+* npp_sat_encode_clause - translate clause to cover inequality
+*
+* Given a clause
+*
+* OR t[j], (1)
+* j in J
+*
+* where t[j] is a literal, i.e. t[j] = x[j] or t[j] = NOT x[j], this
+* routine translates it to the following equivalent cover inequality,
+* which is added to the transformed problem:
+*
+* sum t[j] >= 1, (2)
+* j in J
+*
+* where t[j] = x[j] or t[j] = 1 - x[j].
+*
+* If necessary, the clause should be normalized before a call to this
+* routine. */
+
+NPPROW *npp_sat_encode_clause(NPP *npp, int size, NPPLIT lit[])
+{ NPPROW *row;
+ int k;
+ xassert(size >= 1);
+ row = npp_add_row(npp);
+ row->lb = 1.0, row->ub = +DBL_MAX;
+ for (k = 1; k <= size; k++)
+ { xassert(lit[k].col != NULL);
+ if (lit[k].neg == 0)
+ npp_add_aij(npp, row, lit[k].col, +1.0);
+ else if (lit[k].neg == 1)
+ { npp_add_aij(npp, row, lit[k].col, -1.0);
+ row->lb -= 1.0;
+ }
+ else
+ xassert(lit != lit);
+ }
+ return row;
+}
+
+/***********************************************************************
+* npp_sat_encode_geq - encode "not less than" constraint
+*
+* PURPOSE
+*
+* This routine translates to CNF the following constraint:
+*
+* n
+* sum 2**(k-1) * y[k] >= b, (1)
+* k=1
+*
+* where y[k] is either a literal (i.e. y[k] = x[k] or y[k] = 1 - x[k])
+* or constant false (zero), b is a given lower bound.
+*
+* ALGORITHM
+*
+* If b < 0, the constraint is redundant, so assume that b >= 0. Let
+*
+* n
+* b = sum 2**(k-1) b[k], (2)
+* k=1
+*
+* where b[k] is k-th binary digit of b. (Note that if b >= 2**n and
+* therefore cannot be represented in the form (2), the constraint (1)
+* is infeasible.) In this case the condition (1) is equivalent to the
+* following condition:
+*
+* y[n] y[n-1] ... y[2] y[1] >= b[n] b[n-1] ... b[2] b[1], (3)
+*
+* where ">=" is understood lexicographically.
+*
+* Algorithmically the condition (3) can be tested as follows:
+*
+* for (k = n; k >= 1; k--)
+* { if (y[k] < b[k])
+* y is less than b;
+* if (y[k] > b[k])
+* y is greater than b;
+* }
+* y is equal to b;
+*
+* Thus, y is less than b iff there exists k, 1 <= k <= n, for which
+* the following condition is satisfied:
+*
+* y[n] = b[n] AND ... AND y[k+1] = b[k+1] AND y[k] < b[k]. (4)
+*
+* Negating the condition (4) we have that y is not less than b iff for
+* all k, 1 <= k <= n, the following condition is satisfied:
+*
+* y[n] != b[n] OR ... OR y[k+1] != b[k+1] OR y[k] >= b[k]. (5)
+*
+* Note that if b[k] = 0, the literal y[k] >= b[k] is always true, in
+* which case the entire clause (5) is true and can be omitted.
+*
+* RETURNS
+*
+* Normally the routine returns zero. However, if the constraint (1) is
+* infeasible, the routine returns non-zero. */
+
+int npp_sat_encode_geq(NPP *npp, int n, NPPLIT y[], int rhs)
+{ NPPLIT lit[1+NBIT_MAX];
+ int j, k, size, temp, b[1+NBIT_MAX];
+ xassert(0 <= n && n <= NBIT_MAX);
+ /* if the constraint (1) is redundant, do nothing */
+ if (rhs < 0)
+ return 0;
+ /* determine binary digits of b according to (2) */
+ for (k = 1, temp = rhs; k <= n; k++, temp >>= 1)
+ b[k] = temp & 1;
+ if (temp != 0)
+ { /* b >= 2**n; the constraint (1) is infeasible */
+ return 1;
+ }
+ /* main transformation loop */
+ for (k = 1; k <= n; k++)
+ { /* build the clause (5) for current k */
+ size = 0; /* clause size = number of literals */
+ /* add literal y[k] >= b[k] */
+ if (b[k] == 0)
+ { /* b[k] = 0 -> the literal is true */
+ goto skip;
+ }
+ else if (y[k].col == NULL)
+ { /* y[k] = 0, b[k] = 1 -> the literal is false */
+ xassert(y[k].neg == 0);
+ }
+ else
+ { /* add literal y[k] = 1 */
+ lit[++size] = y[k];
+ }
+ for (j = k+1; j <= n; j++)
+ { /* add literal y[j] != b[j] */
+ if (y[j].col == NULL)
+ { xassert(y[j].neg == 0);
+ if (b[j] == 0)
+ { /* y[j] = 0, b[j] = 0 -> the literal is false */
+ continue;
+ }
+ else
+ { /* y[j] = 0, b[j] = 1 -> the literal is true */
+ goto skip;
+ }
+ }
+ else
+ { lit[++size] = y[j];
+ if (b[j] != 0)
+ lit[size].neg = 1 - lit[size].neg;
+ }
+ }
+ /* normalize the clause */
+ size = npp_sat_normalize_clause(npp, size, lit);
+ if (size < 0)
+ { /* the clause is equivalent to the value true */
+ goto skip;
+ }
+ if (size == 0)
+ { /* the clause is equivalent to the value false; this means
+ that the constraint (1) is infeasible */
+ return 2;
+ }
+ /* translate the clause to corresponding cover inequality */
+ npp_sat_encode_clause(npp, size, lit);
+skip: ;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_encode_leq - encode "not greater than" constraint
+*
+* PURPOSE
+*
+* This routine translates to CNF the following constraint:
+*
+* n
+* sum 2**(k-1) * y[k] <= b, (1)
+* k=1
+*
+* where y[k] is either a literal (i.e. y[k] = x[k] or y[k] = 1 - x[k])
+* or constant false (zero), b is a given upper bound.
+*
+* ALGORITHM
+*
+* If b < 0, the constraint is infeasible, so assume that b >= 0. Let
+*
+* n
+* b = sum 2**(k-1) b[k], (2)
+* k=1
+*
+* where b[k] is k-th binary digit of b. (Note that if b >= 2**n and
+* therefore cannot be represented in the form (2), the constraint (1)
+* is redundant.) In this case the condition (1) is equivalent to the
+* following condition:
+*
+* y[n] y[n-1] ... y[2] y[1] <= b[n] b[n-1] ... b[2] b[1], (3)
+*
+* where "<=" is understood lexicographically.
+*
+* Algorithmically the condition (3) can be tested as follows:
+*
+* for (k = n; k >= 1; k--)
+* { if (y[k] < b[k])
+* y is less than b;
+* if (y[k] > b[k])
+* y is greater than b;
+* }
+* y is equal to b;
+*
+* Thus, y is greater than b iff there exists k, 1 <= k <= n, for which
+* the following condition is satisfied:
+*
+* y[n] = b[n] AND ... AND y[k+1] = b[k+1] AND y[k] > b[k]. (4)
+*
+* Negating the condition (4) we have that y is not greater than b iff
+* for all k, 1 <= k <= n, the following condition is satisfied:
+*
+* y[n] != b[n] OR ... OR y[k+1] != b[k+1] OR y[k] <= b[k]. (5)
+*
+* Note that if b[k] = 1, the literal y[k] <= b[k] is always true, in
+* which case the entire clause (5) is true and can be omitted.
+*
+* RETURNS
+*
+* Normally the routine returns zero. However, if the constraint (1) is
+* infeasible, the routine returns non-zero. */
+
+int npp_sat_encode_leq(NPP *npp, int n, NPPLIT y[], int rhs)
+{ NPPLIT lit[1+NBIT_MAX];
+ int j, k, size, temp, b[1+NBIT_MAX];
+ xassert(0 <= n && n <= NBIT_MAX);
+ /* check if the constraint (1) is infeasible */
+ if (rhs < 0)
+ return 1;
+ /* determine binary digits of b according to (2) */
+ for (k = 1, temp = rhs; k <= n; k++, temp >>= 1)
+ b[k] = temp & 1;
+ if (temp != 0)
+ { /* b >= 2**n; the constraint (1) is redundant */
+ return 0;
+ }
+ /* main transformation loop */
+ for (k = 1; k <= n; k++)
+ { /* build the clause (5) for current k */
+ size = 0; /* clause size = number of literals */
+ /* add literal y[k] <= b[k] */
+ if (b[k] == 1)
+ { /* b[k] = 1 -> the literal is true */
+ goto skip;
+ }
+ else if (y[k].col == NULL)
+ { /* y[k] = 0, b[k] = 0 -> the literal is true */
+ xassert(y[k].neg == 0);
+ goto skip;
+ }
+ else
+ { /* add literal y[k] = 0 */
+ lit[++size] = y[k];
+ lit[size].neg = 1 - lit[size].neg;
+ }
+ for (j = k+1; j <= n; j++)
+ { /* add literal y[j] != b[j] */
+ if (y[j].col == NULL)
+ { xassert(y[j].neg == 0);
+ if (b[j] == 0)
+ { /* y[j] = 0, b[j] = 0 -> the literal is false */
+ continue;
+ }
+ else
+ { /* y[j] = 0, b[j] = 1 -> the literal is true */
+ goto skip;
+ }
+ }
+ else
+ { lit[++size] = y[j];
+ if (b[j] != 0)
+ lit[size].neg = 1 - lit[size].neg;
+ }
+ }
+ /* normalize the clause */
+ size = npp_sat_normalize_clause(npp, size, lit);
+ if (size < 0)
+ { /* the clause is equivalent to the value true */
+ goto skip;
+ }
+ if (size == 0)
+ { /* the clause is equivalent to the value false; this means
+ that the constraint (1) is infeasible */
+ return 2;
+ }
+ /* translate the clause to corresponding cover inequality */
+ npp_sat_encode_clause(npp, size, lit);
+skip: ;
+ }
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_encode_row - encode constraint (row) of general type
+*
+* PURPOSE
+*
+* This routine translates to CNF the following constraint (row):
+*
+* L <= sum a[j] x[j] <= U, (1)
+* j
+*
+* where all x[j] are binary variables.
+*
+* ALGORITHM
+*
+* First, the routine performs substitution x[j] = t[j] for j in J+
+* and x[j] = 1 - t[j] for j in J-, where J+ = { j : a[j] > 0 } and
+* J- = { j : a[j] < 0 }. This gives:
+*
+* L <= sum a[j] t[j] + sum a[j] (1 - t[j]) <= U ==>
+* j in J+ j in J-
+*
+* L' <= sum |a[j]| t[j] <= U', (2)
+* j
+*
+* where
+*
+* L' = L - sum a[j], U' = U - sum a[j]. (3)
+* j in J- j in J-
+*
+* (Actually only new bounds L' and U' are computed.)
+*
+* Then the routine translates to CNF the following equality:
+*
+* n
+* sum |a[j]| t[j] = sum 2**(k-1) * y[k], (4)
+* j k=1
+*
+* where y[k] is either some t[j] or a new literal or a constant zero
+* (see the routine npp_sat_encode_sum_ax).
+*
+* Finally, the routine translates to CNF the following conditions:
+*
+* n
+* sum 2**(k-1) * y[k] >= L' (5)
+* k=1
+*
+* and
+*
+* n
+* sum 2**(k-1) * y[k] <= U' (6)
+* k=1
+*
+* (see the routines npp_sat_encode_geq and npp_sat_encode_leq).
+*
+* All resulting clauses are encoded as cover inequalities and included
+* into the transformed problem.
+*
+* Note that on exit the routine removes the specified constraint (row)
+* from the original problem.
+*
+* RETURNS
+*
+* The routine returns one of the following codes:
+*
+* 0 - translation was successful;
+* 1 - constraint (1) was found infeasible;
+* 2 - integer arithmetic error occured. */
+
+int npp_sat_encode_row(NPP *npp, NPPROW *row)
+{ NPPAIJ *aij;
+ NPPLIT y[1+NBIT_MAX];
+ int n, rhs;
+ double lb, ub;
+ /* the row should not be free */
+ xassert(!(row->lb == -DBL_MAX && row->ub == +DBL_MAX));
+ /* compute new bounds L' and U' (3) */
+ lb = row->lb;
+ ub = row->ub;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ { if (aij->val < 0.0)
+ { if (lb != -DBL_MAX)
+ lb -= aij->val;
+ if (ub != -DBL_MAX)
+ ub -= aij->val;
+ }
+ }
+ /* encode the equality (4) */
+ n = npp_sat_encode_sum_ax(npp, row, y);
+ if (n < 0)
+ return 2; /* integer arithmetic error */
+ /* encode the condition (5) */
+ if (lb != -DBL_MAX)
+ { rhs = (int)lb;
+ if ((double)rhs != lb)
+ return 2; /* integer arithmetic error */
+ if (npp_sat_encode_geq(npp, n, y, rhs) != 0)
+ return 1; /* original constraint is infeasible */
+ }
+ /* encode the condition (6) */
+ if (ub != +DBL_MAX)
+ { rhs = (int)ub;
+ if ((double)rhs != ub)
+ return 2; /* integer arithmetic error */
+ if (npp_sat_encode_leq(npp, n, y, rhs) != 0)
+ return 1; /* original constraint is infeasible */
+ }
+ /* remove the specified row from the problem */
+ npp_del_row(npp, row);
+ return 0;
+}
+
+/***********************************************************************
+* npp_sat_encode_prob - encode 0-1 feasibility problem
+*
+* This routine translates the specified 0-1 feasibility problem to an
+* equivalent SAT-CNF problem.
+*
+* N.B. Currently this is a very crude implementation.
+*
+* RETURNS
+*
+* 0 success;
+*
+* GLP_ENOPFS primal/integer infeasibility detected;
+*
+* GLP_ERANGE integer overflow occured. */
+
+int npp_sat_encode_prob(NPP *npp)
+{ NPPROW *row, *next_row, *prev_row;
+ NPPCOL *col, *next_col;
+ int cover = 0, pack = 0, partn = 0, ret;
+ /* process and remove free rows */
+ for (row = npp->r_head; row != NULL; row = next_row)
+ { next_row = row->next;
+ if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
+ npp_sat_free_row(npp, row);
+ }
+ /* process and remove fixed columns */
+ for (col = npp->c_head; col != NULL; col = next_col)
+ { next_col = col->next;
+ if (col->lb == col->ub)
+ xassert(npp_sat_fixed_col(npp, col) == 0);
+ }
+ /* only binary variables should remain */
+ for (col = npp->c_head; col != NULL; col = col->next)
+ xassert(col->is_int && col->lb == 0.0 && col->ub == 1.0);
+ /* new rows may be added to the end of the row list, so we walk
+ from the end to beginning of the list */
+ for (row = npp->r_tail; row != NULL; row = prev_row)
+ { prev_row = row->prev;
+ /* process special cases */
+ ret = npp_sat_is_cover_ineq(npp, row);
+ if (ret != 0)
+ { /* row is covering inequality */
+ cover++;
+ /* since it already encodes a clause, just transform it to
+ canonical form */
+ if (ret == 2)
+ { xassert(npp_sat_reverse_row(npp, row) == 0);
+ ret = npp_sat_is_cover_ineq(npp, row);
+ }
+ xassert(ret == 1);
+ continue;
+ }
+ ret = npp_sat_is_partn_eq(npp, row);
+ if (ret != 0)
+ { /* row is partitioning equality */
+ NPPROW *cov;
+ NPPAIJ *aij;
+ partn++;
+ /* transform it to canonical form */
+ if (ret == 2)
+ { xassert(npp_sat_reverse_row(npp, row) == 0);
+ ret = npp_sat_is_partn_eq(npp, row);
+ }
+ xassert(ret == 1);
+ /* and split it into covering and packing inequalities,
+ both in canonical forms */
+ cov = npp_add_row(npp);
+ cov->lb = row->lb, cov->ub = +DBL_MAX;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ npp_add_aij(npp, cov, aij->col, aij->val);
+ xassert(npp_sat_is_cover_ineq(npp, cov) == 1);
+ /* the cover inequality already encodes a clause and do
+ not need any further processing */
+ row->lb = -DBL_MAX;
+ xassert(npp_sat_is_pack_ineq(npp, row) == 1);
+ /* the packing inequality will be processed below */
+ pack--;
+ }
+ ret = npp_sat_is_pack_ineq(npp, row);
+ if (ret != 0)
+ { /* row is packing inequality */
+ NPPROW *rrr;
+ int nlit, desired_nlit = 4;
+ pack++;
+ /* transform it to canonical form */
+ if (ret == 2)
+ { xassert(npp_sat_reverse_row(npp, row) == 0);
+ ret = npp_sat_is_pack_ineq(npp, row);
+ }
+ xassert(ret == 1);
+ /* process the packing inequality */
+ for (;;)
+ { /* determine the number of literals in the remaining
+ inequality */
+ nlit = npp_row_nnz(npp, row);
+ if (nlit <= desired_nlit)
+ break;
+ /* split the current inequality into one having not more
+ than desired_nlit literals and remaining one */
+ rrr = npp_sat_split_pack(npp, row, desired_nlit-1);
+ /* translate the former inequality to CNF and remove it
+ from the original problem */
+ npp_sat_encode_pack(npp, rrr);
+ }
+ /* translate the remaining inequality to CNF and remove it
+ from the original problem */
+ npp_sat_encode_pack(npp, row);
+ continue;
+ }
+ /* translate row of general type to CNF and remove it from the
+ original problem */
+ ret = npp_sat_encode_row(npp, row);
+ if (ret == 0)
+ ;
+ else if (ret == 1)
+ ret = GLP_ENOPFS;
+ else if (ret == 2)
+ ret = GLP_ERANGE;
+ else
+ xassert(ret != ret);
+ if (ret != 0)
+ goto done;
+ }
+ ret = 0;
+ if (cover != 0)
+ xprintf("%d covering inequalities\n", cover);
+ if (pack != 0)
+ xprintf("%d packing inequalities\n", pack);
+ if (partn != 0)
+ xprintf("%d partitioning equalities\n", partn);
+done: return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/proxy/main.c.disabled b/test/monniaux/glpk-4.65/src/proxy/main.c.disabled
new file mode 100644
index 00000000..a7d1e2b8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/proxy/main.c.disabled
@@ -0,0 +1,87 @@
+/* Last update: 08-May-2013 */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "glpk.h"
+#include "proxy.h"
+
+/**********************************************************************/
+int main(int argc, char **argv)
+/**********************************************************************/
+{
+ glp_prob *lp;
+ int ncols, status;
+ double *initsol, zstar, *xstar;
+
+ /* check arguments */
+ if ( (argc == 1) || (argc > 3) ) {
+ printf("ERROR: Usage: ts <instance> <(possibly) xml initsols>\n"
+ );
+ exit(1);
+ }
+
+ /* creating the problem */
+ lp = glp_create_prob();
+ glp_set_prob_name(lp, "Proxy");
+
+ /* reading the problem */
+ glp_term_out(GLP_OFF);
+#if 0 /* by mao */
+ status = glp_read_lp(lp, NULL, argv[1]);
+#else
+ status = glp_read_mps(lp, GLP_MPS_FILE, NULL, argv[1]);
+#endif
+ glp_term_out(GLP_ON);
+ if ( status ) {
+ printf("Problem %s does not exist!!!, status %d\n",
+ argv[1], status);
+ exit(1);
+ }
+
+ ncols = glp_get_num_cols(lp);
+
+ initsol = (double *) calloc(ncols+1, sizeof(double));
+
+ if (argc == 3) {
+ FILE *fp=fopen(argv[2],"r");
+ char tmp[256]={0x0};
+ int counter = 1;
+ while(fp!=NULL && fgets(tmp, sizeof(tmp),fp)!=NULL)
+ {
+ char *valini = strstr(tmp, "value");
+ if (valini!=NULL){
+ int num;
+ double dnum;
+ valini +=7;
+ sscanf(valini, "%d%*s",&num);
+ dnum = (double)num;
+ initsol[counter] = dnum;
+ counter++;
+ }
+ }
+ fclose(fp);
+ }
+
+ xstar = (double *) calloc(ncols+1, sizeof(double));
+
+ if (argc == 3) {
+ status = proxy(lp, &zstar, xstar, initsol, 0.0, 0, 1);
+ }
+ else {
+ status = proxy(lp, &zstar, xstar, NULL, 0.0, 0, 1);
+ }
+
+ printf("Status = %d; ZSTAR = %f\n",status,zstar);
+ /*
+ int i;
+ for (i=1; i< ncols+1; i++) {
+ printf("XSTAR[%d] = %f\n",i, xstar[i]);
+ }
+ */
+
+ glp_delete_prob(lp);
+
+ return 0;
+}
diff --git a/test/monniaux/glpk-4.65/src/proxy/proxy.c b/test/monniaux/glpk-4.65/src/proxy/proxy.c
new file mode 100644
index 00000000..7d890003
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/proxy/proxy.c
@@ -0,0 +1,1073 @@
+/* proxy.c (proximity search heuristic algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Author: Giorgio Sartor <0gioker0@gmail.com>.
+*
+* Copyright (C) 2013, 2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+*
+************************************************************************
+*
+* THIS CODE IS AN IMPLEMENTATION OF THE ALGORITHM PROPOSED IN
+*
+* M. Fischetti, M. Monaci,
+* "Proximity Search for 0-1 Mixed-Integer Convex Programming"
+* Technical Report DEI, University of Padua, March 2013.
+*
+* AVAILABLE AT
+* http://www.dei.unipd.it/~fisch/papers/proximity_search.pdf
+*
+* THE CODE HAS BEEN WRITTEN BY GIORGIO SARTOR, " 0gioker0@gmail.com "
+*
+* BASIC IDEA:
+*
+* The initial feasible solution x_tilde is defined. This initial
+* solution can be found by an ad-hoc heuristic and proxy can be used to
+* refine it by exploiting an underlying MIP model whose solution from
+* scratch turned out to be problematic. Otherwise, x_tilde can be found
+* by running the GLPK mip solver until a first feasible solution is
+* found, setting a conservative time limit of 10 minutes (by default).
+* Time limit can be modified passing variable tlim [ms].
+*
+* Then the cutoff tolerance "delta" is defined. The default tolerance
+* is 1% of the last feasible solution obj value--rounded to integer if
+* all the variables and obj coefficients are integer.
+*
+* Next, the objective function c' x is replaced by the Hamming distance
+* between x (the actual obj coefficients) and x_tilde (the given
+* solution). Distance is only computed wrt the binary variables.
+*
+* The GLPK solver is then invoked to hopefully find a new incumbent
+* x_star with cost c' x_star <= c' x_tilde - delta. A crucial property
+* here is that the root-node solution of the LP relaxation is expected
+* to be not too different from x_tilde, as this latter solution would
+* be optimal without the cutoff constraint, that for a small delta can
+* typically be fulfilled with just local adjustments.
+*
+* If no new solution x_star is found within the time limit the
+* algorithm stops. Of course, if the MIP solver proved infeasibility
+* for the given delta, we have that c' x_tilde - delta is a valid lower
+* bound (in case of minimazation) on the optimal value of the original
+* MIP.
+*
+* The new solution x_star, if any, is possibly improved by solving a
+* simplified problem (refinement) where all binary variables have been
+* fixed to their value in x_star so as to find the best solution within
+* the neighborhood.
+*
+* Finally, the approach is reapplied on x_star (that replaces x_tilde)
+* so as to recenter the distance Hamming function and by modifying the
+* cutoff tolerance delta.
+*
+* In this way, there will be a series of hopefully not-too-difficult
+* sub-MIPs to solve, each leading to an improvement of the incumbent.
+* More aggressive policies on the definition of tolerance delta can
+* lead to a better performance, but would require an ad-hoc tuning.
+*
+************************************************************************
+*
+* int proxy(glp_prob *lp, double *zstar, double *xstar,
+* const double[] initsol, double rel_impr, int tlim,
+* int verbose)
+*
+* lp : GLPK problem pointer to a MIP with binary variables
+*
+* zstar : the value of objective function of the best solution found
+*
+* xstar : best solution with components xstar[1],...,xstar[ncols]
+*
+* initsol : pointer to a initial feasible solution, see
+* glp_ios_heur_sol
+* If initsol = NULL, the procedure finds the first solution
+* by itself.
+*
+* rel_impr : minimum relative obj improvement to be achieved at each
+* internal step; if <= 0.0 a default value of 0.01 (1%) is
+* used; for some problems (e.g., set covering with small
+* integer costs) a more-conservative choice of 0.001 (0.1%)
+* can lead to a better final solution; values larger than
+* 0.05 (5%) are typically too aggressive and do not work
+* well.
+*
+* tlim : time limit to find a new solution, in ms.
+* If tlim = 0, it is set to its default value, 600000 ms
+*
+* verbose : if 1 the output is activated. If 0 only errors are
+* displayed
+*
+* The procedure returns -1 if an error occurred, 0 otherwise (possibly,
+* time limit)
+*
+***********************************************************************/
+
+/**********************************************************************/
+/* 1. INCLUDE */
+/**********************************************************************/
+
+#include "glpk.h"
+#include "env.h"
+#include "proxy.h"
+
+/**********************************************************************/
+/* 2. PARAMETERS AND CONSTANTS */
+/**********************************************************************/
+
+#define TDAY 86400.0
+#define TRUE 1
+#define FALSE 0
+#define EPS 1e-6
+#define RINF 1e38
+#define MAXVAL 1e20
+#define MINVAL -1e20
+#if 0 /* by gioker */
+ #define PROXY_DEBUG
+#endif
+
+/**********************************************************************/
+/* 3. GLOBAL VARIABLES */
+/**********************************************************************/
+
+struct csa {
+
+int integer_obj; /* TRUE if each feasible solution has an
+ integral cost */
+int b_vars_exist; /* TRUE if there is at least one binary
+ variable in the problem */
+int i_vars_exist; /* TRUE if there is at least one general
+ integer variable in the problem */
+const double *startsol; /* Pointer to the initial solution */
+
+int *ckind; /* Store the kind of the structural variables
+ of the problem */
+double *clb; /* Store the lower bound on the structural
+ variables of the problem */
+double *cub; /* Store the upper bound on the structural
+ variables of the problem */
+double *true_obj; /* Store the obj coefficients of the problem */
+
+int dir; /* Minimization or maximization problem */
+int ncols; /* Number of structural variables of the
+ problem */
+
+time_t GLOtstart; /* starting time of the algorithm */
+
+glp_prob *lp_ref; /* glp problem for refining only*/
+
+};
+
+/**********************************************************************/
+/* 4. FUNCTIONS PROTOTYPES */
+/**********************************************************************/
+
+static void callback(glp_tree *tree, void *info);
+static void get_info(struct csa *csa, glp_prob *lp);
+static int is_integer(struct csa *csa);
+static void check_integrality(struct csa *csa);
+static int check_ref(struct csa *csa, glp_prob *lp, double *xref);
+static double second(void);
+static int add_cutoff(struct csa *csa, glp_prob *lp);
+static void get_sol(struct csa *csa, glp_prob *lp, double *xstar);
+static double elapsed_time(struct csa *csa);
+static void redefine_obj(glp_prob *lp, double *xtilde, int ncols,
+ int *ckind, double *clb, double *cub);
+static double update_cutoff(struct csa *csa, glp_prob *lp,
+ double zstar, int index, double rel_impr);
+static double compute_delta(struct csa *csa, double z,
+ double rel_impr);
+static double objval(int ncols, double *x, double *true_obj);
+static void array_copy(int begin, int end, double *source,
+ double *destination);
+static int do_refine(struct csa *csa, glp_prob *lp_ref, int ncols,
+ int *ckind, double *xref, int *tlim, int tref_lim,
+ int verbose);
+static void deallocate(struct csa *csa, int refine);
+
+/**********************************************************************/
+/* 5. FUNCTIONS */
+/**********************************************************************/
+
+int proxy(glp_prob *lp, double *zfinal, double *xfinal,
+ const double initsol[], double rel_impr, int tlim,
+ int verbose)
+
+{ struct csa csa_, *csa = &csa_;
+ glp_iocp parm;
+ glp_smcp parm_lp;
+ size_t tpeak;
+ int refine, tref_lim, err, cutoff_row, niter, status, i, tout;
+ double *xref, *xstar, zstar, tela, cutoff, zz;
+
+ memset(csa, 0, sizeof(struct csa));
+
+
+ /********** **********/
+ /********** RETRIEVING PROBLEM INFO **********/
+ /********** **********/
+
+ /* getting problem direction (min or max) */
+ csa->dir = glp_get_obj_dir(lp);
+
+ /* getting number of variables */
+ csa->ncols = glp_get_num_cols(lp);
+
+ /* getting kind, bounds and obj coefficient of each variable
+ information is stored in ckind, cub, clb, true_obj */
+ get_info(csa, lp);
+
+ /* checking if the objective function is always integral */
+ check_integrality(csa);
+
+ /* Proximity search cannot be used if there are no binary
+ variables */
+ if (csa->b_vars_exist == FALSE) {
+ if (verbose) {
+ xprintf("The problem has not binary variables. Proximity se"
+ "arch cannot be used.\n");
+ }
+ tfree(csa->ckind);
+ tfree(csa->clb);
+ tfree(csa->cub);
+ tfree(csa->true_obj);
+ return -1;
+ }
+
+ /* checking if the problem needs refinement, i.e., not all
+ variables are binary. If so, the routine creates a copy of the
+ lp problem named lp_ref and initializes the solution xref to
+ zero. */
+ xref = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(xref, 0, sizeof(double)*(csa->ncols+1));
+#endif
+ refine = check_ref(csa, lp, xref);
+#ifdef PROXY_DEBUG
+ xprintf("REFINE = %d\n",refine);
+#endif
+
+ /* Initializing the solution */
+ xstar = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(xstar, 0, sizeof(double)*(csa->ncols+1));
+#endif
+
+ /********** **********/
+ /********** FINDING FIRST SOLUTION **********/
+ /********** **********/
+
+ if (verbose) {
+ xprintf("Applying PROXY heuristic...\n");
+ }
+
+ /* get the initial time */
+ csa->GLOtstart = second();
+
+ /* setting the optimization parameters */
+ glp_init_iocp(&parm);
+ glp_init_smcp(&parm_lp);
+#if 0 /* by gioker */
+ /* Preprocessing should be disabled because the mip passed
+ to proxy is already preprocessed */
+ parm.presolve = GLP_ON;
+#endif
+#if 1 /* by mao */
+ /* best projection backtracking seems to be more efficient to find
+ any integer feasible solution */
+ parm.bt_tech = GLP_BT_BPH;
+#endif
+
+ /* Setting the default value of the minimum relative improvement
+ to 1% */
+ if ( rel_impr <= 0.0 ) {
+ rel_impr = 0.01;
+ }
+
+ /* Setting the default value of time limit to 10 minutes */
+ if (tlim <= 0) {
+ tlim = INT_MAX;
+ }
+ if (verbose) {
+ xprintf("Proxy's time limit set to %d seconds.\n",tlim/1000);
+ xprintf("Proxy's relative improvement "
+ "set to %2.2lf %c.\n",rel_impr*100,37);
+ }
+
+ parm_lp.tm_lim = tlim;
+
+ parm.mip_gap = 9999999.9; /* to stop the optimization at the first
+ feasible solution found */
+
+ /* finding the first solution */
+ if (verbose) {
+ xprintf("Searching for a feasible solution...\n");
+ }
+
+ /* verifying the existence of an input starting solution */
+ if (initsol != NULL) {
+ csa->startsol = initsol;
+ parm.cb_func = callback;
+ parm.cb_info = csa;
+ if (verbose) {
+ xprintf("Input solution found.\n");
+ }
+ }
+
+ tout = glp_term_out(GLP_OFF);
+ err = glp_simplex(lp,&parm_lp);
+ glp_term_out(tout);
+
+ status = glp_get_status(lp);
+
+ if (status != GLP_OPT) {
+ if (verbose) {
+ xprintf("Proxy heuristic terminated.\n");
+ }
+#ifdef PROXY_DEBUG
+ /* For debug only */
+ xprintf("GLP_SIMPLEX status = %d\n",status);
+ xprintf("GLP_SIMPLEX error code = %d\n",err);
+#endif
+ tfree(xref);
+ tfree(xstar);
+ deallocate(csa, refine);
+ return -1;
+ }
+
+ tela = elapsed_time(csa);
+ if (tlim-tela*1000 <= 0) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy could not "
+ "find optimal solution to LP relaxation.\n");
+ xprintf("Proxy heuristic aborted.\n");
+ }
+ tfree(xref);
+ tfree(xstar);
+ deallocate(csa, refine);
+ return -1;
+ }
+
+ parm.tm_lim = tlim - tela*1000;
+ tref_lim = (tlim - tela *1000) / 20;
+
+ tout = glp_term_out(GLP_OFF);
+ err = glp_intopt(lp, &parm);
+ glp_term_out(tout);
+
+ status = glp_mip_status(lp);
+
+ /***** If no solution was found *****/
+
+ if (status == GLP_NOFEAS || status == GLP_UNDEF) {
+ if (err == GLP_ETMLIM) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy could not "
+ "find an initial integer feasible solution.\n");
+ xprintf("Proxy heuristic aborted.\n");
+ }
+ }
+ else {
+ if (verbose) {
+ xprintf("Proxy could not "
+ "find an initial integer feasible solution.\n");
+ xprintf("Proxy heuristic aborted.\n");
+ }
+ }
+ tfree(xref);
+ tfree(xstar);
+ deallocate(csa, refine);
+ return -1;
+ }
+
+ /* getting the first solution and its value */
+ get_sol(csa, lp,xstar);
+ zstar = glp_mip_obj_val(lp);
+
+ if (verbose) {
+ xprintf(">>>>> first solution = %e;\n", zstar);
+ }
+
+ /* If a feasible solution was found but the time limit is
+ exceeded */
+ if (err == GLP_ETMLIM) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy heuristic terminated.\n");
+ }
+ goto done;
+ }
+
+ tela = elapsed_time(csa);
+ tpeak = 0;
+ glp_mem_usage(NULL, NULL, NULL, &tpeak);
+ if (verbose) {
+ xprintf("Time used: %3.1lf secs. Memory used: %2.1lf Mb\n",
+ tela,(double)tpeak/1048576);
+ xprintf("Starting proximity search...\n");
+ }
+
+ /********** **********/
+ /********** PREPARING THE PROBLEM FOR PROXY **********/
+ /********** **********/
+
+ /* adding a dummy cutoff constraint */
+ cutoff_row = add_cutoff(csa, lp);
+
+ /* proximity search needs minimization direction
+ even if the problem is a maximization one */
+ if (csa->dir == GLP_MAX) {
+ glp_set_obj_dir(lp, GLP_MIN);
+ }
+
+ /********** **********/
+ /********** STARTING PROXIMITY SEARCH **********/
+ /********** **********/
+
+
+ niter = 0;
+
+ while (TRUE) {
+ niter++;
+
+ /********** CHANGING THE OBJ FUNCTION **********/
+
+ redefine_obj(lp,xstar, csa->ncols, csa->ckind, csa->clb,
+ csa->cub);
+
+ /********** UPDATING THE CUTOFF CONSTRAINT **********/
+
+ cutoff = update_cutoff(csa, lp,zstar, cutoff_row, rel_impr);
+
+#ifdef PROXY_DEBUG
+ xprintf("TRUE_OBJ[0] = %f\n",csa->true_obj[0]);
+ xprintf("ZSTAR = %f\n",zstar);
+ xprintf("CUTOFF = %f\n",cutoff);
+#endif
+
+ /********** SEARCHING FOR A BETTER SOLUTION **********/
+
+ tela = elapsed_time(csa);
+ if (tlim-tela*1000 <= 0) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy heuristic "
+ "terminated.\n");
+ }
+ goto done;
+ }
+#ifdef PROXY_DEBUG
+ xprintf("TELA = %3.1lf\n",tela*1000);
+ xprintf("TLIM = %3.1lf\n",tlim - tela*1000);
+#endif
+ parm_lp.tm_lim = tlim -tela*1000;
+
+ tout = glp_term_out(GLP_OFF);
+ err = glp_simplex(lp,&parm_lp);
+ glp_term_out(tout);
+
+ status = glp_get_status(lp);
+
+ if (status != GLP_OPT) {
+ if (status == GLP_NOFEAS) {
+ if (verbose) {
+ xprintf("Bound exceeded = %f. ",cutoff);
+ }
+ }
+ if (verbose) {
+ xprintf("Proxy heuristic terminated.\n");
+ }
+#ifdef PROXY_DEBUG
+ xprintf("GLP_SIMPLEX status = %d\n",status);
+ xprintf("GLP_SIMPLEX error code = %d\n",err);
+#endif
+ goto done;
+ }
+
+ tela = elapsed_time(csa);
+ if (tlim-tela*1000 <= 0) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy heuristic "
+ "terminated.\n");
+ }
+ goto done;
+ }
+ parm.tm_lim = tlim - tela*1000;
+ parm.cb_func = NULL;
+#if 0 /* by gioker */
+ /* Preprocessing should be disabled because the mip passed
+ to proxy is already preprocessed */
+ parm.presolve = GLP_ON;
+#endif
+ tout = glp_term_out(GLP_OFF);
+ err = glp_intopt(lp, &parm);
+ glp_term_out(tout);
+
+ /********** MANAGEMENT OF THE SOLUTION **********/
+
+ status = glp_mip_status(lp);
+
+ /***** No feasible solutions *****/
+
+ if (status == GLP_NOFEAS) {
+ if (verbose) {
+ xprintf("Bound exceeded = %f. Proxy heuristic "
+ "terminated.\n",cutoff);
+ }
+ goto done;
+ }
+
+ /***** Undefined solution *****/
+
+ if (status == GLP_UNDEF) {
+ if (err == GLP_ETMLIM) {
+ if (verbose) {
+ xprintf("Time limit exceeded. Proxy heuristic "
+ "terminated.\n");
+ }
+ }
+ else {
+ if (verbose) {
+ xprintf("Proxy terminated unexpectedly.\n");
+#ifdef PROXY_DEBUG
+ xprintf("GLP_INTOPT error code = %d\n",err);
+#endif
+ }
+ }
+ goto done;
+ }
+
+ /***** Feasible solution *****/
+
+ if ((status == GLP_FEAS) || (status == GLP_OPT)) {
+
+ /* getting the solution and computing its value */
+ get_sol(csa, lp,xstar);
+ zz = objval(csa->ncols, xstar, csa->true_obj);
+
+ /* Comparing the incumbent solution with the current best
+ one */
+#ifdef PROXY_DEBUG
+ xprintf("ZZ = %f\n",zz);
+ xprintf("ZSTAR = %f\n",zstar);
+ xprintf("REFINE = %d\n",refine);
+#endif
+ if (((zz<zstar) && (csa->dir == GLP_MIN)) ||
+ ((zz>zstar) && (csa->dir == GLP_MAX))) {
+
+ /* refining (possibly) the solution */
+ if (refine) {
+
+ /* copying the incumbent solution in the refinement
+ one */
+ array_copy(1, csa->ncols +1, xstar, xref);
+ err = do_refine(csa, csa->lp_ref, csa->ncols,
+ csa->ckind, xref, &tlim, tref_lim, verbose);
+ if (!err) {
+ double zref = objval(csa->ncols, xref,
+ csa->true_obj);
+ if (((zref<zz) && (csa->dir == GLP_MIN)) ||
+ ((zref>zz) && (csa->dir == GLP_MAX))) {
+ zz = zref;
+ /* copying the refinement solution in the
+ incumbent one */
+ array_copy(1, csa->ncols +1, xref, xstar);
+ }
+ }
+ }
+ zstar = zz;
+ tela = elapsed_time(csa);
+ if (verbose) {
+ xprintf(">>>>> it: %3d: mip = %e; elapsed time "
+ "%3.1lf sec.s\n", niter,zstar,tela);
+ }
+ }
+ }
+ }
+
+done:
+ tela = elapsed_time(csa);
+ glp_mem_usage(NULL, NULL, NULL, &tpeak);
+ if (verbose) {
+ xprintf("Time used: %3.1lf. Memory used: %2.1lf Mb\n",
+ tela,(double)tpeak/1048576);
+ }
+
+
+ /* Exporting solution and obj val */
+ *zfinal = zstar;
+
+ for (i=1; i < (csa->ncols + 1); i++) {
+ xfinal[i]=xstar[i];
+ }
+
+ /* Freeing allocated memory */
+ tfree(xref);
+ tfree(xstar);
+ deallocate(csa, refine);
+
+ return 0;
+}
+
+/**********************************************************************/
+static void callback(glp_tree *tree, void *info){
+/**********************************************************************/
+ struct csa *csa = info;
+ switch(glp_ios_reason(tree)) {
+ case GLP_IHEUR:
+ glp_ios_heur_sol(tree, csa->startsol);
+ break;
+ default: break;
+ }
+}
+
+/**********************************************************************/
+static void get_info(struct csa *csa, glp_prob *lp)
+/**********************************************************************/
+{
+ int i;
+
+ /* Storing helpful info of the problem */
+
+ csa->ckind = talloc(csa->ncols+1, int);
+#if 0 /* by mao */
+ memset(csa->ckind, 0, sizeof(int)*(csa->ncols+1));
+#endif
+ csa->clb = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(csa->clb, 0, sizeof(double)*(csa->ncols+1));
+#endif
+ csa->cub = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(csa->cub, 0, sizeof(double)*(csa->ncols+1));
+#endif
+ csa->true_obj = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(csa->true_obj, 0, sizeof(double)*(csa->ncols+1));
+#endif
+ for( i = 1 ; i < (csa->ncols + 1); i++ ) {
+ csa->ckind[i] = glp_get_col_kind(lp, i);
+ csa->clb[i] = glp_get_col_lb(lp, i);
+ csa->cub[i] = glp_get_col_ub(lp, i);
+ csa->true_obj[i] = glp_get_obj_coef(lp, i);
+ }
+ csa->true_obj[0] = glp_get_obj_coef(lp, 0);
+}
+
+/**********************************************************************/
+static int is_integer(struct csa *csa)
+/**********************************************************************/
+{
+ int i;
+ csa->integer_obj = TRUE;
+ for ( i = 1; i < (csa->ncols + 1); i++ ) {
+ if (fabs(csa->true_obj[i]) > INT_MAX ) {
+ csa->integer_obj = FALSE;
+ }
+ if (fabs(csa->true_obj[i]) <= INT_MAX) {
+ double tmp, rem;
+ if (fabs(csa->true_obj[i]) - floor(fabs(csa->true_obj[i]))
+ < 0.5) {
+ tmp = floor(fabs(csa->true_obj[i]));
+ }
+ else {
+ tmp = ceil(fabs(csa->true_obj[i]));
+ }
+ rem = fabs(csa->true_obj[i]) - tmp;
+ rem = fabs(rem);
+ if (rem > EPS) {
+ csa->integer_obj = FALSE;
+ }
+
+ }
+ }
+ return csa->integer_obj;
+}
+
+/**********************************************************************/
+static void check_integrality(struct csa *csa)
+/**********************************************************************/
+{
+ /*
+ Checking if the problem has binary, integer or continuos variables.
+ integer_obj is TRUE if the problem has no continuous variables
+ and all the obj coefficients are integer (and < INT_MAX).
+ */
+
+ int i;
+ csa->integer_obj = is_integer(csa);
+ csa->b_vars_exist = FALSE;
+ csa->i_vars_exist = FALSE;
+ for ( i = 1; i < (csa->ncols + 1); i++ ) {
+ if ( csa->ckind[i] == GLP_IV ){
+ csa->i_vars_exist = TRUE;
+ continue;
+ }
+ if ( csa->ckind[i] == GLP_BV ){
+ csa->b_vars_exist =TRUE;
+ continue;
+ }
+ csa->integer_obj = FALSE;
+ }
+}
+
+/**********************************************************************/
+static int check_ref(struct csa *csa, glp_prob *lp, double *xref)
+/**********************************************************************/
+{
+ /*
+ checking if the problem has continuos or integer variables. If so,
+ refinement is prepared.
+ */
+ int refine = FALSE;
+ int i;
+ for ( i = 1; i < (csa->ncols + 1); i++ ) {
+ if ( csa->ckind[i] != GLP_BV) {
+ refine = TRUE;
+ break;
+ }
+ }
+
+ /* possibly creating a mip clone for refinement only */
+ if ( refine ) {
+ csa->lp_ref = glp_create_prob();
+ glp_copy_prob(csa->lp_ref, lp, GLP_ON);
+ }
+
+ return refine;
+}
+
+/**********************************************************************/
+static double second(void)
+/**********************************************************************/
+{
+#if 0 /* by mao */
+ return ((double)clock()/(double)CLOCKS_PER_SEC);
+#else
+ return xtime() / 1000.0;
+#endif
+}
+
+/**********************************************************************/
+static int add_cutoff(struct csa *csa, glp_prob *lp)
+/**********************************************************************/
+{
+ /*
+ Adding a cutoff constraint to set an upper bound (in case of
+ minimaztion) on the obj value of the next solution, i.e., the next
+ value of the true obj function that we would like to find
+ */
+
+ /* store non-zero coefficients in the objective function */
+ int *obj_index = talloc(csa->ncols+1, int);
+#if 0 /* by mao */
+ memset(obj_index, 0, sizeof(int)*(csa->ncols+1));
+#endif
+ double *obj_value = talloc(csa->ncols+1, double);
+#if 0 /* by mao */
+ memset(obj_value, 0, sizeof(double)*(csa->ncols+1));
+#endif
+ int obj_nzcnt = 0;
+ int i, irow;
+ const char *rowname;
+ for ( i = 1; i < (csa->ncols + 1); i++ ) {
+ if ( fabs(csa->true_obj[i]) > EPS ) {
+ obj_nzcnt++;
+ obj_index[obj_nzcnt] = i;
+ obj_value[obj_nzcnt] = csa->true_obj[i];
+ }
+ }
+
+ irow = glp_add_rows(lp, 1);
+ rowname = "Cutoff";
+ glp_set_row_name(lp, irow, rowname);
+ if (csa->dir == GLP_MIN) {
+ /* minimization problem */
+ glp_set_row_bnds(lp, irow, GLP_UP, MAXVAL, MAXVAL);
+ }
+ else {
+ /* maximization problem */
+ glp_set_row_bnds(lp, irow, GLP_LO, MINVAL, MINVAL);
+ }
+
+ glp_set_mat_row(lp, irow, obj_nzcnt, obj_index, obj_value);
+
+ tfree(obj_index);
+ tfree(obj_value);
+
+ return irow;
+}
+
+/**********************************************************************/
+static void get_sol(struct csa *csa, glp_prob *lp, double *xstar)
+/**********************************************************************/
+{
+ /* Retrieving and storing the coefficients of the solution */
+
+ int i;
+ for (i = 1; i < (csa->ncols +1); i++) {
+ xstar[i] = glp_mip_col_val(lp, i);
+ }
+}
+
+/**********************************************************************/
+static double elapsed_time(struct csa *csa)
+/**********************************************************************/
+{
+ double tela = second() - csa->GLOtstart;
+ if ( tela < 0 ) tela += TDAY;
+ return(tela);
+}
+
+/**********************************************************************/
+static void redefine_obj(glp_prob *lp, double *xtilde, int ncols,
+ int *ckind, double *clb, double *cub)
+/**********************************************************************/
+
+/*
+ Redefine the lp objective function obj as the distance-to-integrality
+ (Hamming distance) from xtilde (the incumbent feasible solution), wrt
+ to binary vars only
+ */
+
+{
+ int j;
+ double *delta = talloc(ncols+1, double);
+#if 0 /* by mao */
+ memset(delta, 0, sizeof(double)*(ncols+1));
+#endif
+
+ for ( j = 1; j < (ncols +1); j++ ) {
+ delta[j] = 0.0;
+ /* skip continuous variables */
+ if ( ckind[j] == GLP_CV ) continue;
+
+ /* skip integer variables that have been fixed */
+ if ( cub[j]-clb[j] < 0.5 ) continue;
+
+ /* binary variable */
+ if ( ckind[j] == GLP_BV ) {
+ if ( xtilde[j] > 0.5 ) {
+ delta[j] = -1.0;
+ }
+ else {
+ delta[j] = 1.0;
+ }
+ }
+ }
+
+ /* changing the obj coeff. for all variables, including continuous
+ ones */
+ for ( j = 1; j < (ncols +1); j++ ) {
+ glp_set_obj_coef(lp, j, delta[j]);
+ }
+ glp_set_obj_coef(lp, 0, 0.0);
+
+ tfree(delta);
+}
+
+/**********************************************************************/
+static double update_cutoff(struct csa *csa, glp_prob *lp,
+ double zstar, int cutoff_row,
+ double rel_impr)
+/**********************************************************************/
+{
+ /*
+ Updating the cutoff constraint with the value we would like to
+ find during the next optimization
+ */
+ double cutoff;
+ zstar -= csa->true_obj[0];
+ if (csa->dir == GLP_MIN) {
+ cutoff = zstar - compute_delta(csa, zstar, rel_impr);
+ glp_set_row_bnds(lp, cutoff_row, GLP_UP, cutoff, cutoff);
+ }
+ else {
+ cutoff = zstar + compute_delta(csa, zstar, rel_impr);
+ glp_set_row_bnds(lp, cutoff_row, GLP_LO, cutoff, cutoff);
+ }
+
+ return cutoff;
+}
+
+/**********************************************************************/
+static double compute_delta(struct csa *csa, double z, double rel_impr)
+/**********************************************************************/
+{
+ /* Computing the offset for the next best solution */
+
+ double delta = rel_impr * fabs(z);
+ if ( csa->integer_obj ) delta = ceil(delta);
+
+ return(delta);
+}
+
+/**********************************************************************/
+static double objval(int ncols, double *x, double *true_obj)
+/**********************************************************************/
+{
+ /* Computing the true cost of x (using the original obj coeff.s) */
+
+ int j;
+ double z = 0.0;
+ for ( j = 1; j < (ncols +1); j++ ) {
+ z += x[j] * true_obj[j];
+ }
+ return z + true_obj[0];
+}
+
+/**********************************************************************/
+static void array_copy(int begin, int end, double *source,
+ double *destination)
+/**********************************************************************/
+{
+ int i;
+ for (i = begin; i < end; i++) {
+ destination[i] = source[i];
+ }
+}
+/**********************************************************************/
+static int do_refine(struct csa *csa, glp_prob *lp_ref, int ncols,
+ int *ckind, double *xref, int *tlim, int tref_lim,
+ int verbose)
+/**********************************************************************/
+{
+ /*
+ Refinement is applied when the variables of the problem are not
+ all binary. Binary variables are fixed to their value and
+ remaining ones are optimized. If there are only continuos
+ variables (in addition to those binary) the problem becomes just
+ an LP. Otherwise, it remains a MIP but of smaller size.
+ */
+
+ int j, tout;
+ double refineStart = second();
+ double val, tela, tlimit;
+
+ if ( glp_get_num_cols(lp_ref) != ncols ) {
+ if (verbose) {
+ xprintf("Error in Proxy refinement: ");
+ xprintf("wrong number of columns (%d vs %d).\n",
+ ncols, glp_get_num_cols(lp_ref));
+ }
+ return 1;
+ }
+
+ val = -1.0;
+
+ /* fixing all binary variables to their current value in xref */
+ for ( j = 1; j < (ncols + 1); j++ ) {
+ if ( ckind[j] == GLP_BV ) {
+ val = 0.0;
+ if ( xref[j] > 0.5 ) val = 1.0;
+ glp_set_col_bnds(lp_ref, j, GLP_FX, val, val);
+ }
+ }
+
+ /* re-optimizing (refining) if some bound has been changed */
+ if ( val > -1.0 ) {
+ glp_iocp parm_ref;
+ glp_smcp parm_ref_lp;
+ int err, status;
+
+ glp_init_iocp(&parm_ref);
+ parm_ref.presolve = GLP_ON;
+ glp_init_smcp(&parm_ref_lp);
+ /*
+ If there are no general integer variable the problem becomes
+ an LP (after fixing the binary variables) and can be solved
+ quickly. Otherwise the problem is still a MIP problem and a
+ timelimit has to be set.
+ */
+ parm_ref.tm_lim = tref_lim;
+ if (parm_ref.tm_lim > *tlim) {
+ parm_ref.tm_lim = *tlim;
+ }
+ parm_ref_lp.tm_lim = parm_ref.tm_lim;
+#ifdef PROXY_DEBUG
+ xprintf("***** REFINING *****\n");
+#endif
+ tout = glp_term_out(GLP_OFF);
+ if (csa->i_vars_exist == TRUE) {
+ err = glp_intopt(lp_ref, &parm_ref);
+ }
+ else {
+ err = glp_simplex(lp_ref, &parm_ref_lp);
+ }
+ glp_term_out(tout);
+
+ if (csa->i_vars_exist == TRUE) {
+ status = glp_mip_status(lp_ref);
+ }
+ else {
+ status = glp_get_status(lp_ref);
+ }
+
+#if 1 /* 29/II-2016 by mao as reported by Chris */
+ switch (status)
+ { case GLP_OPT:
+ case GLP_FEAS:
+ break;
+ default:
+ status = GLP_UNDEF;
+ break;
+ }
+#endif
+
+#ifdef PROXY_DEBUG
+ xprintf("STATUS REFINING = %d\n",status);
+#endif
+ if (status == GLP_UNDEF) {
+ if (err == GLP_ETMLIM) {
+#ifdef PROXY_DEBUG
+ xprintf("Time limit exceeded on Proxy refining.\n");
+#endif
+ return 1;
+ }
+ }
+ for( j = 1 ; j < (ncols + 1); j++ ){
+ if (ckind[j] != GLP_BV) {
+ if (csa->i_vars_exist == TRUE) {
+ xref[j] = glp_mip_col_val(lp_ref, j);
+ }
+ else{
+ xref[j] = glp_get_col_prim(lp_ref, j);
+ }
+ }
+ }
+ }
+ tela = second() - refineStart;
+#ifdef PROXY_DEBUG
+ xprintf("REFINE TELA = %3.1lf\n",tela*1000);
+#endif
+ return 0;
+}
+/**********************************************************************/
+static void deallocate(struct csa *csa, int refine)
+/**********************************************************************/
+{
+ /* Deallocating routine */
+
+ if (refine) {
+ glp_delete_prob(csa->lp_ref);
+ }
+
+ tfree(csa->ckind);
+ tfree(csa->clb);
+ tfree(csa->cub);
+ tfree(csa->true_obj);
+
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/proxy/proxy.h b/test/monniaux/glpk-4.65/src/proxy/proxy.h
new file mode 100644
index 00000000..a91e36f2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/proxy/proxy.h
@@ -0,0 +1,36 @@
+/* proxy.h (proximity search heuristic algorithm) */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Author: Giorgio Sartor <0gioker0@gmail.com>.
+*
+* Copyright (C) 2013 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef PROXY_H
+#define PROXY_H
+
+#define proxy _glp_proxy
+int proxy(glp_prob *lp, double *zstar, double *xstar,
+ const double initsol[], double rel_impr, int tlim,
+ int verbose);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/proxy/proxy1.c b/test/monniaux/glpk-4.65/src/proxy/proxy1.c
new file mode 100644
index 00000000..5f9850d4
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/proxy/proxy1.c
@@ -0,0 +1,88 @@
+/* proxy1.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2013, 2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "ios.h"
+#include "proxy.h"
+
+void ios_proxy_heur(glp_tree *T)
+{ glp_prob *prob;
+ int j, status;
+ double *xstar, zstar;
+ /* this heuristic is applied only once on the root level */
+ if (!(T->curr->level == 0 && T->curr->solved == 1))
+ goto done;
+ prob = glp_create_prob();
+ glp_copy_prob(prob, T->mip, 0);
+ xstar = xcalloc(1+prob->n, sizeof(double));
+ for (j = 1; j <= prob->n; j++)
+ xstar[j] = 0.0;
+ if (T->mip->mip_stat != GLP_FEAS)
+ status = proxy(prob, &zstar, xstar, NULL, 0.0,
+ T->parm->ps_tm_lim, 1);
+ else
+ { double *xinit = xcalloc(1+prob->n, sizeof(double));
+ for (j = 1; j <= prob->n; j++)
+ xinit[j] = T->mip->col[j]->mipx;
+ status = proxy(prob, &zstar, xstar, xinit, 0.0,
+ T->parm->ps_tm_lim, 1);
+ xfree(xinit);
+ }
+ if (status == 0)
+#if 0 /* 17/III-2016 */
+ glp_ios_heur_sol(T, xstar);
+#else
+ { /* sometimes the proxy heuristic reports a wrong solution, so
+ * make sure that the solution is really integer feasible */
+ int i, feas1, feas2, ae_ind, re_ind;
+ double ae_max, re_max;
+ glp_copy_prob(prob, T->mip, 0);
+ for (j = 1; j <= prob->n; j++)
+ prob->col[j]->mipx = xstar[j];
+ for (i = 1; i <= prob->m; i++)
+ { GLPROW *row;
+ GLPAIJ *aij;
+ row = prob->row[i];
+ row->mipx = 0.0;
+ for (aij = row->ptr; aij != NULL; aij = aij->r_next)
+ row->mipx += aij->val * aij->col->mipx;
+ }
+ glp_check_kkt(prob, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind,
+ &re_max, &re_ind);
+ feas1 = (re_max <= 1e-6);
+ glp_check_kkt(prob, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind,
+ &re_max, &re_ind);
+ feas2 = (re_max <= 1e-6);
+ if (feas1 && feas2)
+ glp_ios_heur_sol(T, xstar);
+ else
+ xprintf("WARNING: PROXY HEURISTIC REPORTED WRONG SOLUTION; "
+ "SOLUTION REJECTED\n");
+ }
+#endif
+ xfree(xstar);
+ glp_delete_prob(prob);
+done: return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/simplex.h b/test/monniaux/glpk-4.65/src/simplex/simplex.h
new file mode 100644
index 00000000..9a5acdb2
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/simplex.h
@@ -0,0 +1,39 @@
+/* simplex.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SIMPLEX_H
+#define SIMPLEX_H
+
+#include "prob.h"
+
+#define spx_primal _glp_spx_primal
+int spx_primal(glp_prob *P, const glp_smcp *parm);
+/* driver to the primal simplex method */
+
+#define spy_dual _glp_spy_dual
+int spy_dual(glp_prob *P, const glp_smcp *parm);
+/* driver to the dual simplex method */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxat.c b/test/monniaux/glpk-4.65/src/simplex/spxat.c
new file mode 100644
index 00000000..3570a18c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxat.c
@@ -0,0 +1,265 @@
+/* spxat.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxat.h"
+
+/***********************************************************************
+* spx_alloc_at - allocate constraint matrix in sparse row-wise format
+*
+* This routine allocates the memory for arrays needed to represent the
+* constraint matrix in sparse row-wise format. */
+
+void spx_alloc_at(SPXLP *lp, SPXAT *at)
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ at->ptr = talloc(1+m+1, int);
+ at->ind = talloc(1+nnz, int);
+ at->val = talloc(1+nnz, double);
+ at->work = talloc(1+n, double);
+ return;
+}
+
+/***********************************************************************
+* spx_build_at - build constraint matrix in sparse row-wise format
+*
+* This routine builds sparse row-wise representation of the constraint
+* matrix A using its sparse column-wise representation stored in the
+* lp object, and stores the result in the at object. */
+
+void spx_build_at(SPXLP *lp, SPXAT *at)
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int *AT_ptr = at->ptr;
+ int *AT_ind = at->ind;
+ double *AT_val = at->val;
+ int i, k, ptr, end, pos;
+ /* calculate AT_ptr[i] = number of non-zeros in i-th row */
+ memset(&AT_ptr[1], 0, m * sizeof(int));
+ for (k = 1; k <= n; k++)
+ { ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ AT_ptr[A_ind[ptr]]++;
+ }
+ /* set AT_ptr[i] to position after last element in i-th row */
+ AT_ptr[1]++;
+ for (i = 2; i <= m; i++)
+ AT_ptr[i] += AT_ptr[i-1];
+ xassert(AT_ptr[m] == nnz+1);
+ AT_ptr[m+1] = nnz+1;
+ /* build row-wise representation and re-arrange AT_ptr[i] */
+ for (k = n; k >= 1; k--)
+ { /* copy elements from k-th column to corresponding rows */
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ { pos = --AT_ptr[A_ind[ptr]];
+ AT_ind[pos] = k;
+ AT_val[pos] = A_val[ptr];
+ }
+ }
+ xassert(AT_ptr[1] == 1);
+ return;
+}
+
+/***********************************************************************
+* spx_at_prod - compute product y := y + s * A'* x
+*
+* This routine computes the product:
+*
+* y := y + s * A'* x,
+*
+* where A' is a matrix transposed to the mxn-matrix A of constraint
+* coefficients, x is a m-vector, s is a scalar, y is a n-vector.
+*
+* The routine uses the row-wise representation of the matrix A and
+* computes the product as a linear combination:
+*
+* y := y + s * (A'[1] * x[1] + ... + A'[m] * x[m]),
+*
+* where A'[i] is i-th row of A, 1 <= i <= m. */
+
+void spx_at_prod(SPXLP *lp, SPXAT *at, double y[/*1+n*/], double s,
+ const double x[/*1+m*/])
+{ int m = lp->m;
+ int *AT_ptr = at->ptr;
+ int *AT_ind = at->ind;
+ double *AT_val = at->val;
+ int i, ptr, end;
+ double t;
+ for (i = 1; i <= m; i++)
+ { if (x[i] != 0.0)
+ { /* y := y + s * (i-th row of A) * x[i] */
+ t = s * x[i];
+ ptr = AT_ptr[i];
+ end = AT_ptr[i+1];
+ for (; ptr < end; ptr++)
+ y[AT_ind[ptr]] += AT_val[ptr] * t;
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_nt_prod1 - compute product y := y + s * N'* x
+*
+* This routine computes the product:
+*
+* y := y + s * N'* x,
+*
+* where N' is a matrix transposed to the mx(n-m)-matrix N composed
+* from non-basic columns of the constraint matrix A, x is a m-vector,
+* s is a scalar, y is (n-m)-vector.
+*
+* If the flag ign is non-zero, the routine ignores the input content
+* of the array y assuming that y = 0. */
+
+void spx_nt_prod1(SPXLP *lp, SPXAT *at, double y[/*1+n-m*/], int ign,
+ double s, const double x[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ double *work = at->work;
+ int j, k;
+ for (k = 1; k <= n; k++)
+ work[k] = 0.0;
+ if (!ign)
+ { for (j = 1; j <= n-m; j++)
+ work[head[m+j]] = y[j];
+ }
+ spx_at_prod(lp, at, work, s, x);
+ for (j = 1; j <= n-m; j++)
+ y[j] = work[head[m+j]];
+ return;
+}
+
+/***********************************************************************
+* spx_eval_trow1 - compute i-th row of simplex table
+*
+* This routine computes i-th row of the current simplex table
+* T = (T[i,j]) = - inv(B) * N, 1 <= i <= m, using representation of
+* the constraint matrix A in row-wise format.
+*
+* The vector rho = (rho[j]), which is i-th row of the basis inverse
+* inv(B), should be previously computed with the routine spx_eval_rho.
+* It is assumed that elements of this vector are stored in the array
+* locations rho[1], ..., rho[m].
+*
+* There exist two ways to compute the simplex table row.
+*
+* 1. T[i,j], j = 1,...,n-m, is computed as inner product:
+*
+* m
+* T[i,j] = - sum a[i,k] * rho[i],
+* i=1
+*
+* where N[j] = A[k] is a column of the constraint matrix corresponding
+* to non-basic variable xN[j]. The estimated number of operations in
+* this case is:
+*
+* n1 = (n - m) * (nnz(A) / n),
+*
+* (n - m) is the number of columns of N, nnz(A) / n is the average
+* number of non-zeros in one column of A and, therefore, of N.
+*
+* 2. The simplex table row is computed as part of a linear combination
+* of rows of A with coefficients rho[i] != 0. The estimated number
+* of operations in this case is:
+*
+* n2 = nnz(rho) * (nnz(A) / m),
+*
+* where nnz(rho) is the number of non-zeros in the vector rho,
+* nnz(A) / m is the average number of non-zeros in one row of A.
+*
+* If n1 < n2, the routine computes the simples table row using the
+* first way (like the routine spx_eval_trow). Otherwise, the routine
+* uses the second way calling the routine spx_nt_prod1.
+*
+* On exit components of the simplex table row are stored in the array
+* locations trow[1], ... trow[n-m]. */
+
+void spx_eval_trow1(SPXLP *lp, SPXAT *at, const double rho[/*1+m*/],
+ double trow[/*1+n-m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int i, j, nnz_rho;
+ double cnt1, cnt2;
+ /* determine nnz(rho) */
+ nnz_rho = 0;
+ for (i = 1; i <= m; i++)
+ { if (rho[i] != 0.0)
+ nnz_rho++;
+ }
+ /* estimate the number of operations for both ways */
+ cnt1 = (double)(n - m) * ((double)nnz / (double)n);
+ cnt2 = (double)nnz_rho * ((double)nnz / (double)m);
+ /* compute i-th row of simplex table */
+ if (cnt1 < cnt2)
+ { /* as inner products */
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int *head = lp->head;
+ int k, ptr, end;
+ double tij;
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* compute t[i,j] = - N'[j] * pi */
+ tij = 0.0;
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ tij -= A_val[ptr] * rho[A_ind[ptr]];
+ trow[j] = tij;
+ }
+ }
+ else
+ { /* as linear combination */
+ spx_nt_prod1(lp, at, trow, 1, -1.0, rho);
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_free_at - deallocate constraint matrix in sparse row-wise format
+*
+* This routine deallocates the memory used for arrays of the program
+* object at. */
+
+void spx_free_at(SPXLP *lp, SPXAT *at)
+{ xassert(lp == lp);
+ tfree(at->ptr);
+ tfree(at->ind);
+ tfree(at->val);
+ tfree(at->work);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxat.h b/test/monniaux/glpk-4.65/src/simplex/spxat.h
new file mode 100644
index 00000000..98d5b003
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxat.h
@@ -0,0 +1,80 @@
+/* spxat.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXAT_H
+#define SPXAT_H
+
+#include "spxlp.h"
+
+typedef struct SPXAT SPXAT;
+
+struct SPXAT
+{ /* mxn-matrix A of constraint coefficients in sparse row-wise
+ * format */
+ int *ptr; /* int ptr[1+m+1]; */
+ /* ptr[0] is not used;
+ * ptr[i], 1 <= i <= m, is starting position of i-th row in
+ * arrays ind and val; note that ptr[1] is always 1;
+ * ptr[m+1] indicates the position after the last element in
+ * arrays ind and val, i.e. ptr[m+1] = nnz+1, where nnz is the
+ * number of non-zero elements in matrix A;
+ * the length of i-th row (the number of non-zero elements in
+ * that row) can be calculated as ptr[i+1] - ptr[i] */
+ int *ind; /* int ind[1+nnz]; */
+ /* column indices */
+ double *val; /* double val[1+nnz]; */
+ /* non-zero element values */
+ double *work; /* double work[1+n]; */
+ /* working array */
+};
+
+#define spx_alloc_at _glp_spx_alloc_at
+void spx_alloc_at(SPXLP *lp, SPXAT *at);
+/* allocate constraint matrix in sparse row-wise format */
+
+#define spx_build_at _glp_spx_build_at
+void spx_build_at(SPXLP *lp, SPXAT *at);
+/* build constraint matrix in sparse row-wise format */
+
+#define spx_at_prod _glp_spx_at_prod
+void spx_at_prod(SPXLP *lp, SPXAT *at, double y[/*1+n*/], double s,
+ const double x[/*1+m*/]);
+/* compute product y := y + s * A'* x */
+
+#define spx_nt_prod1 _glp_spx_nt_prod1
+void spx_nt_prod1(SPXLP *lp, SPXAT *at, double y[/*1+n-m*/], int ign,
+ double s, const double x[/*1+m*/]);
+/* compute product y := y + s * N'* x */
+
+#define spx_eval_trow1 _glp_spx_eval_trow1
+void spx_eval_trow1(SPXLP *lp, SPXAT *at, const double rho[/*1+m*/],
+ double trow[/*1+n-m*/]);
+/* compute i-th row of simplex table */
+
+#define spx_free_at _glp_spx_free_at
+void spx_free_at(SPXLP *lp, SPXAT *at);
+/* deallocate constraint matrix in sparse row-wise format */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxchuzc.c b/test/monniaux/glpk-4.65/src/simplex/spxchuzc.c
new file mode 100644
index 00000000..c60ccabc
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxchuzc.c
@@ -0,0 +1,381 @@
+/* spxchuzc.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxchuzc.h"
+
+/***********************************************************************
+* spx_chuzc_sel - select eligible non-basic variables
+*
+* This routine selects eligible non-basic variables xN[j], whose
+* reduced costs d[j] have "wrong" sign, i.e. changing such xN[j] in
+* feasible direction improves (decreases) the objective function.
+*
+* Reduced costs of non-basic variables should be placed in the array
+* locations d[1], ..., d[n-m].
+*
+* Non-basic variable xN[j] is considered eligible if:
+*
+* d[j] <= -eps[j] and xN[j] can increase
+*
+* d[j] >= +eps[j] and xN[j] can decrease
+*
+* for
+*
+* eps[j] = tol + tol1 * |cN[j]|,
+*
+* where cN[j] is the objective coefficient at xN[j], tol and tol1 are
+* specified tolerances.
+*
+* On exit the routine stores indices j of eligible non-basic variables
+* xN[j] to the array locations list[1], ..., list[num] and returns the
+* number of such variables 0 <= num <= n-m. (If the parameter list is
+* specified as NULL, no indices are stored.) */
+
+int spx_chuzc_sel(SPXLP *lp, const double d[/*1+n-m*/], double tol,
+ double tol1, int list[/*1+n-m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k, num;
+ double ck, eps;
+ num = 0;
+ /* walk thru list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == u[k])
+ { /* xN[j] is fixed variable; skip it */
+ continue;
+ }
+ /* determine absolute tolerance eps[j] */
+ ck = c[k];
+ eps = tol + tol1 * (ck >= 0.0 ? +ck : -ck);
+ /* check if xN[j] is eligible */
+ if (d[j] <= -eps)
+ { /* xN[j] should be able to increase */
+ if (flag[j])
+ { /* but its upper bound is active */
+ continue;
+ }
+ }
+ else if (d[j] >= +eps)
+ { /* xN[j] should be able to decrease */
+ if (!flag[j] && l[k] != -DBL_MAX)
+ { /* but its lower bound is active */
+ continue;
+ }
+ }
+ else /* -eps < d[j] < +eps */
+ { /* xN[j] does not affect the objective function within the
+ * specified tolerance */
+ continue;
+ }
+ /* xN[j] is eligible non-basic variable */
+ num++;
+ if (list != NULL)
+ list[num] = j;
+ }
+ return num;
+}
+
+/***********************************************************************
+* spx_chuzc_std - choose non-basic variable (Dantzig's rule)
+*
+* This routine chooses most eligible non-basic variable xN[q]
+* according to Dantzig's ("standard") rule:
+*
+* d[q] = max |d[j]|,
+* j in J
+*
+* where J <= {1, ..., n-m} is the set of indices of eligible non-basic
+* variables, d[j] is the reduced cost of non-basic variable xN[j] in
+* the current basis.
+*
+* Reduced costs of non-basic variables should be placed in the array
+* locations d[1], ..., d[n-m].
+*
+* Indices of eligible non-basic variables j in J should be placed in
+* the array locations list[1], ..., list[num], where num = |J| > 0 is
+* the total number of such variables.
+*
+* On exit the routine returns q, the index of the non-basic variable
+* xN[q] chosen. */
+
+int spx_chuzc_std(SPXLP *lp, const double d[/*1+n-m*/], int num,
+ const int list[])
+{ int m = lp->m;
+ int n = lp->n;
+ int j, q, t;
+ double abs_dj, abs_dq;
+ xassert(0 < num && num <= n-m);
+ q = 0, abs_dq = -1.0;
+ for (t = 1; t <= num; t++)
+ { j = list[t];
+ abs_dj = (d[j] >= 0.0 ? +d[j] : -d[j]);
+ if (abs_dq < abs_dj)
+ q = j, abs_dq = abs_dj;
+ }
+ xassert(q != 0);
+ return q;
+}
+
+/***********************************************************************
+* spx_alloc_se - allocate pricing data block
+*
+* This routine allocates the memory for arrays used in the pricing
+* data block. */
+
+void spx_alloc_se(SPXLP *lp, SPXSE *se)
+{ int m = lp->m;
+ int n = lp->n;
+ se->valid = 0;
+ se->refsp = talloc(1+n, char);
+ se->gamma = talloc(1+n-m, double);
+ se->work = talloc(1+m, double);
+ return;
+}
+
+/***********************************************************************
+* spx_reset_refsp - reset reference space
+*
+* This routine resets (re-initializes) the reference space composing
+* it from variables which are non-basic in the current basis, and sets
+* all weights gamma[j] to 1. */
+
+void spx_reset_refsp(SPXLP *lp, SPXSE *se)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *gamma = se->gamma;
+ int j, k;
+ se->valid = 1;
+ memset(&refsp[1], 0, n * sizeof(char));
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ refsp[k] = 1;
+ gamma[j] = 1.0;
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_eval_gamma_j - compute projected steepest edge weight directly
+*
+* This routine computes projected steepest edge weight gamma[j],
+* 1 <= j <= n-m, for the current basis directly with the formula:
+*
+* m
+* gamma[j] = delta[j] + sum eta[i] * T[i,j]**2,
+* i=1
+*
+* where T[i,j] is element of the current simplex table, and
+*
+* ( 1, if xB[i] is in the reference space
+* eta[i] = {
+* ( 0, otherwise
+*
+* ( 1, if xN[j] is in the reference space
+* delta[j] = {
+* ( 0, otherwise
+*
+* NOTE: For testing/debugging only. */
+
+double spx_eval_gamma_j(SPXLP *lp, SPXSE *se, int j)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *tcol = se->work;
+ int i, k;
+ double gamma_j;
+ xassert(se->valid);
+ xassert(1 <= j && j <= n-m);
+ k = head[m+j]; /* x[k] = xN[j] */
+ gamma_j = (refsp[k] ? 1.0 : 0.0);
+ spx_eval_tcol(lp, j, tcol);
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ if (refsp[k])
+ gamma_j += tcol[i] * tcol[i];
+ }
+ return gamma_j;
+}
+
+/***********************************************************************
+* spx_chuzc_pse - choose non-basic variable (projected steepest edge)
+*
+* This routine chooses most eligible non-basic variable xN[q]
+* according to the projected steepest edge method:
+*
+* d[q]**2 d[j]**2
+* -------- = max -------- ,
+* gamma[q] j in J gamma[j]
+*
+* where J <= {1, ..., n-m} is the set of indices of eligible non-basic
+* variable, d[j] is the reduced cost of non-basic variable xN[j] in
+* the current basis, gamma[j] is the projected steepest edge weight.
+*
+* Reduced costs of non-basic variables should be placed in the array
+* locations d[1], ..., d[n-m].
+*
+* Indices of eligible non-basic variables j in J should be placed in
+* the array locations list[1], ..., list[num], where num = |J| > 0 is
+* the total number of such variables.
+*
+* On exit the routine returns q, the index of the non-basic variable
+* xN[q] chosen. */
+
+int spx_chuzc_pse(SPXLP *lp, SPXSE *se, const double d[/*1+n-m*/],
+ int num, const int list[])
+{ int m = lp->m;
+ int n = lp->n;
+ double *gamma = se->gamma;
+ int j, q, t;
+ double best, temp;
+ xassert(se->valid);
+ xassert(0 < num && num <= n-m);
+ q = 0, best = -1.0;
+ for (t = 1; t <= num; t++)
+ { j = list[t];
+ /* FIXME */
+ if (gamma[j] < DBL_EPSILON)
+ temp = 0.0;
+ else
+ temp = (d[j] * d[j]) / gamma[j];
+ if (best < temp)
+ q = j, best = temp;
+ }
+ xassert(q != 0);
+ return q;
+}
+
+/***********************************************************************
+* spx_update_gamma - update projected steepest edge weights exactly
+*
+* This routine updates the vector gamma = (gamma[j]) of projected
+* steepest edge weights exactly, for the adjacent basis.
+*
+* On entry to the routine the content of the se object should be valid
+* and should correspond to the current basis.
+*
+* The parameter 1 <= p <= m specifies basic variable xB[p] which
+* becomes non-basic variable xN[q] in the adjacent basis.
+*
+* The parameter 1 <= q <= n-m specified non-basic variable xN[q] which
+* becomes basic variable xB[p] in the adjacent basis.
+*
+* It is assumed that the array trow contains elements of p-th (pivot)
+* row T'[p] of the simplex table in locations trow[1], ..., trow[n-m].
+* It is also assumed that the array tcol contains elements of q-th
+* (pivot) column T[q] of the simple table in locations tcol[1], ...,
+* tcol[m]. (These row and column should be computed for the current
+* basis.)
+*
+* For details about the formulae used see the program documentation.
+*
+* The routine also computes the relative error:
+*
+* e = |gamma[q] - gamma'[q]| / (1 + |gamma[q]|),
+*
+* where gamma'[q] is the weight for xN[q] on entry to the routine,
+* and returns e on exit. (If e happens to be large enough, the calling
+* program may reset the reference space, since other weights also may
+* be inaccurate.) */
+
+double spx_update_gamma(SPXLP *lp, SPXSE *se, int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *gamma = se->gamma;
+ double *u = se->work;
+ int i, j, k, ptr, end;
+ double gamma_q, delta_q, e, r, s, t1, t2;
+ xassert(se->valid);
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* compute gamma[q] in current basis more accurately; also
+ * compute auxiliary vector u */
+ k = head[m+q]; /* x[k] = xN[q] */
+ gamma_q = delta_q = (refsp[k] ? 1.0 : 0.0);
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ if (refsp[k])
+ { gamma_q += tcol[i] * tcol[i];
+ u[i] = tcol[i];
+ }
+ else
+ u[i] = 0.0;
+ }
+ bfd_btran(lp->bfd, u);
+ /* compute relative error in gamma[q] */
+ e = fabs(gamma_q - gamma[q]) / (1.0 + gamma_q);
+ /* compute new gamma[q] */
+ gamma[q] = gamma_q / (tcol[p] * tcol[p]);
+ /* compute new gamma[j] for all j != q */
+ for (j = 1; j <= n-m; j++)
+ { if (j == q)
+ continue;
+ if (-1e-9 < trow[j] && trow[j] < +1e-9)
+ { /* T[p,j] is close to zero; gamma[j] is not changed */
+ continue;
+ }
+ /* compute r[j] = T[p,j] / T[p,q] */
+ r = trow[j] / tcol[p];
+ /* compute inner product s[j] = N'[j] * u, where N[j] = A[k]
+ * is constraint matrix column corresponding to xN[j] */
+ s = 0.0;
+ k = head[m+j]; /* x[k] = xN[j] */
+ ptr = lp->A_ptr[k];
+ end = lp->A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ s += lp->A_val[ptr] * u[lp->A_ind[ptr]];
+ /* compute new gamma[j] */
+ t1 = gamma[j] + r * (r * gamma_q + s + s);
+ t2 = (refsp[k] ? 1.0 : 0.0) + delta_q * r * r;
+ gamma[j] = (t1 >= t2 ? t1 : t2);
+ }
+ return e;
+}
+
+/***********************************************************************
+* spx_free_se - deallocate pricing data block
+*
+* This routine deallocates the memory used for arrays in the pricing
+* data block. */
+
+void spx_free_se(SPXLP *lp, SPXSE *se)
+{ xassert(lp == lp);
+ tfree(se->refsp);
+ tfree(se->gamma);
+ tfree(se->work);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxchuzc.h b/test/monniaux/glpk-4.65/src/simplex/spxchuzc.h
new file mode 100644
index 00000000..c09cca9a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxchuzc.h
@@ -0,0 +1,85 @@
+/* spxchuzc.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXCHUZC_H
+#define SPXCHUZC_H
+
+#include "spxlp.h"
+
+#define spx_chuzc_sel _glp_spx_chuzc_sel
+int spx_chuzc_sel(SPXLP *lp, const double d[/*1+n-m*/], double tol,
+ double tol1, int list[/*1+n-m*/]);
+/* select eligible non-basic variables */
+
+#define spx_chuzc_std _glp_spx_chuzc_std
+int spx_chuzc_std(SPXLP *lp, const double d[/*1+n-m*/], int num,
+ const int list[]);
+/* choose non-basic variable (Dantzig's rule) */
+
+typedef struct SPXSE SPXSE;
+
+struct SPXSE
+{ /* projected steepest edge and Devex pricing data block */
+ int valid;
+ /* content validity flag */
+ char *refsp; /* char refsp[1+n]; */
+ /* refsp[0] is not used;
+ * refsp[k], 1 <= k <= n, is the flag meaning that variable x[k]
+ * is in the reference space */
+ double *gamma; /* double gamma[1+n-m]; */
+ /* gamma[0] is not used;
+ * gamma[j], 1 <= j <= n-m, is the weight for reduced cost d[j]
+ * of non-basic variable xN[j] in the current basis */
+ double *work; /* double work[1+m]; */
+ /* working array */
+};
+
+#define spx_alloc_se _glp_spx_alloc_se
+void spx_alloc_se(SPXLP *lp, SPXSE *se);
+/* allocate pricing data block */
+
+#define spx_reset_refsp _glp_spx_reset_refsp
+void spx_reset_refsp(SPXLP *lp, SPXSE *se);
+/* reset reference space */
+
+#define spx_eval_gamma_j _glp_spx_eval_gamma_j
+double spx_eval_gamma_j(SPXLP *lp, SPXSE *se, int j);
+/* compute projeted steepest edge weight directly */
+
+#define spx_chuzc_pse _glp_spx_chuzc_pse
+int spx_chuzc_pse(SPXLP *lp, SPXSE *se, const double d[/*1+n-m*/],
+ int num, const int list[]);
+/* choose non-basic variable (projected steepest edge) */
+
+#define spx_update_gamma _glp_spx_update_gamma
+double spx_update_gamma(SPXLP *lp, SPXSE *se, int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/]);
+/* update projected steepest edge weights exactly */
+
+#define spx_free_se _glp_spx_free_se
+void spx_free_se(SPXLP *lp, SPXSE *se);
+/* deallocate pricing data block */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxchuzr.c b/test/monniaux/glpk-4.65/src/simplex/spxchuzr.c
new file mode 100644
index 00000000..8bef77ba
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxchuzr.c
@@ -0,0 +1,594 @@
+/* spxchuzr.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxchuzr.h"
+
+/***********************************************************************
+* spx_chuzr_std - choose basic variable (textbook ratio test)
+*
+* This routine implements an improved textbook ratio test to choose
+* basic variable xB[p].
+*
+* The parameter phase specifies the search phase:
+*
+* 1 - searching for feasible basic solution. In this case the routine
+* uses artificial bounds of basic variables that correspond to
+* breakpoints of the penalty function:
+*
+* ( lB[i], if cB[i] = 0
+* (
+* lB'[i] = { uB[i], if cB[i] > 0
+* (
+* ( -inf, if cB[i] < 0
+*
+* ( uB[i], if cB[i] = 0
+* (
+* uB'[i] = { +inf, if cB[i] > 0
+* (
+* ( lB[i], if cB[i] < 0
+*
+* where lB[i] and uB[i] are original bounds of variable xB[i],
+* cB[i] is the penalty (objective) coefficient of that variable.
+*
+* 2 - searching for optimal basic solution. In this case the routine
+* uses original bounds of basic variables.
+*
+* Current values of basic variables should be placed in the array
+* locations beta[1], ..., beta[m].
+*
+* The parameter 1 <= q <= n-m specifies the index of non-basic
+* variable xN[q] chosen.
+*
+* The parameter s specifies the direction in which xN[q] changes:
+* s = +1.0 means xN[q] increases, and s = -1.0 means xN[q] decreases.
+* (Thus, the corresponding ray parameter is theta = s (xN[q] - f[q]),
+* where f[q] is the active bound of xN[q] in the current basis.)
+*
+* Elements of q-th simplex table column T[q] = (t[i,q]) corresponding
+* to non-basic variable xN[q] should be placed in the array locations
+* tcol[1], ..., tcol[m].
+*
+* The parameter tol_piv specifies a tolerance for elements of the
+* simplex table column T[q]. If |t[i,q]| < tol_piv, basic variable
+* xB[i] is skipped, i.e. it is assumed that it does not depend on the
+* ray parameter theta.
+*
+* The parameters tol and tol1 specify tolerances used to increase the
+* choice freedom by simulating an artificial degeneracy as follows.
+* If beta[i] <= lB[i] + delta[i], where delta[i] = tol + tol1 |lB[i]|,
+* it is assumed that beta[i] is exactly the same as lB[i]. Similarly,
+* if beta[i] >= uB[i] - delta[i], where delta[i] = tol + tol1 |uB[i]|,
+* it is assumed that beta[i] is exactly the same as uB[i].
+*
+* The routine determines the index 1 <= p <= m of basic variable xB[p]
+* that reaches its (lower or upper) bound first on increasing the ray
+* parameter theta, stores the bound flag (0 - lower bound or fixed
+* value, 1 - upper bound) to the location pointed to by the pointer
+* p_flag, and returns the index p. If non-basic variable xN[q] is
+* double-bounded and reaches its opposite bound first, the routine
+* returns (-1). And if the ray parameter may increase unlimitedly, the
+* routine returns zero.
+*
+* Should note that the bound flag stored to the location pointed to by
+* p_flag corresponds to the original (not artficial) bound of variable
+* xB[p] and defines the active bound flag lp->flag[q] to be set in the
+* adjacent basis for that basic variable. */
+
+int spx_chuzr_std(SPXLP *lp, int phase, const double beta[/*1+m*/],
+ int q, double s, const double tcol[/*1+m*/], int *p_flag,
+ double tol_piv, double tol, double tol1)
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, i_flag, k, p;
+ double alfa, biga, delta, lk, uk, teta, teta_min;
+ xassert(phase == 1 || phase == 2);
+ xassert(1 <= q && q <= n-m);
+ xassert(s == +1.0 || s == -1.0);
+ /* determine initial teta_min */
+ k = head[m+q]; /* x[k] = xN[q] */
+ if (l[k] == -DBL_MAX || u[k] == +DBL_MAX)
+ { /* xN[q] has no opposite bound */
+ p = 0, *p_flag = 0, teta_min = DBL_MAX, biga = 0.0;
+ }
+ else
+ { /* xN[q] have both lower and upper bounds */
+ p = -1, *p_flag = 0, teta_min = fabs(l[k] - u[k]), biga = 1.0;
+ }
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ /* determine alfa such that delta xB[i] = alfa * teta */
+ alfa = s * tcol[i];
+ if (alfa <= -tol_piv)
+ { /* xB[i] decreases */
+ /* determine actual lower bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* xB[i] has no actual lower bound */
+ continue;
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* actual lower bound of xB[i] is its upper bound */
+ lk = u[k];
+ xassert(lk != +DBL_MAX);
+ i_flag = 1;
+ }
+ else
+ { /* actual lower bound of xB[i] is its original bound */
+ lk = l[k];
+ if (lk == -DBL_MAX)
+ continue;
+ i_flag = 0;
+ }
+ /* determine teta on which xB[i] reaches its lower bound */
+ delta = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] <= lk + delta)
+ teta = 0.0;
+ else
+ teta = (lk - beta[i]) / alfa;
+ }
+ else if (alfa >= +tol_piv)
+ { /* xB[i] increases */
+ /* determine actual upper bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* actual upper bound of xB[i] is its lower bound */
+ uk = l[k];
+ xassert(uk != -DBL_MAX);
+ i_flag = 0;
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* xB[i] has no actual upper bound */
+ continue;
+ }
+ else
+ { /* actual upper bound of xB[i] is its original bound */
+ uk = u[k];
+ if (uk == +DBL_MAX)
+ continue;
+ i_flag = 1;
+ }
+ /* determine teta on which xB[i] reaches its upper bound */
+ delta = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] >= uk - delta)
+ teta = 0.0;
+ else
+ teta = (uk - beta[i]) / alfa;
+ }
+ else
+ { /* xB[i] does not depend on teta */
+ continue;
+ }
+ /* choose basic variable xB[p] for which teta is minimal */
+ xassert(teta >= 0.0);
+ alfa = (alfa >= 0.0 ? +alfa : -alfa);
+ if (teta_min > teta || (teta_min == teta && biga < alfa))
+ p = i, *p_flag = i_flag, teta_min = teta, biga = alfa;
+ }
+ /* if xB[p] is fixed variable, adjust its bound flag */
+ if (p > 0)
+ { k = head[p];
+ if (l[k] == u[k])
+ *p_flag = 0;
+ }
+ return p;
+}
+
+/***********************************************************************
+* spx_chuzr_harris - choose basic variable (Harris' ratio test)
+*
+* This routine implements Harris' ratio test to choose basic variable
+* xB[p].
+*
+* All the parameters, except tol and tol1, as well as the returned
+* value have the same meaning as for the routine spx_chuzr_std (see
+* above).
+*
+* The parameters tol and tol1 specify tolerances on bound violations
+* for basic variables. For the lower bound of basic variable xB[i] the
+* tolerance is delta[i] = tol + tol1 |lB[i]|, and for the upper bound
+* the tolerance is delta[i] = tol + tol1 |uB[i]|. */
+
+int spx_chuzr_harris(SPXLP *lp, int phase, const double beta[/*1+m*/],
+ int q, double s, const double tcol[/*1+m*/], int *p_flag,
+ double tol_piv, double tol, double tol1)
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, i_flag, k, p;
+ double alfa, biga, delta, lk, uk, teta, teta_min;
+ xassert(phase == 1 || phase == 2);
+ xassert(1 <= q && q <= n-m);
+ xassert(s == +1.0 || s == -1.0);
+ /*--------------------------------------------------------------*/
+ /* first pass: determine teta_min for relaxed bounds */
+ /*--------------------------------------------------------------*/
+ teta_min = DBL_MAX;
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ /* determine alfa such that delta xB[i] = alfa * teta */
+ alfa = s * tcol[i];
+ if (alfa <= -tol_piv)
+ { /* xB[i] decreases */
+ /* determine actual lower bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* xB[i] has no actual lower bound */
+ continue;
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* actual lower bound of xB[i] is its upper bound */
+ lk = u[k];
+ xassert(lk != +DBL_MAX);
+ }
+ else
+ { /* actual lower bound of xB[i] is its original bound */
+ lk = l[k];
+ if (lk == -DBL_MAX)
+ continue;
+ }
+ /* determine teta on which xB[i] reaches its relaxed lower
+ * bound */
+ delta = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] < lk)
+ teta = - delta / alfa;
+ else
+ teta = ((lk - delta) - beta[i]) / alfa;
+ }
+ else if (alfa >= +tol_piv)
+ { /* xB[i] increases */
+ /* determine actual upper bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* actual upper bound of xB[i] is its lower bound */
+ uk = l[k];
+ xassert(uk != -DBL_MAX);
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* xB[i] has no actual upper bound */
+ continue;
+ }
+ else
+ { /* actual upper bound of xB[i] is its original bound */
+ uk = u[k];
+ if (uk == +DBL_MAX)
+ continue;
+ }
+ /* determine teta on which xB[i] reaches its relaxed upper
+ * bound */
+ delta = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] > uk)
+ teta = + delta / alfa;
+ else
+ teta = ((uk + delta) - beta[i]) / alfa;
+ }
+ else
+ { /* xB[i] does not depend on teta */
+ continue;
+ }
+ xassert(teta >= 0.0);
+ if (teta_min > teta)
+ teta_min = teta;
+ }
+ /*--------------------------------------------------------------*/
+ /* second pass: choose basic variable xB[p] */
+ /*--------------------------------------------------------------*/
+ k = head[m+q]; /* x[k] = xN[q] */
+ if (l[k] != -DBL_MAX && u[k] != +DBL_MAX)
+ { /* xN[q] has both lower and upper bounds */
+ if (fabs(l[k] - u[k]) <= teta_min)
+ { /* and reaches its opposite bound */
+ p = -1, *p_flag = 0;
+ goto done;
+ }
+ }
+ if (teta_min == DBL_MAX)
+ { /* teta may increase unlimitedly */
+ p = 0, *p_flag = 0;
+ goto done;
+ }
+ /* nothing is chosen so far */
+ p = 0, *p_flag = 0, biga = 0.0;
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ /* determine alfa such that delta xB[i] = alfa * teta */
+ alfa = s * tcol[i];
+ if (alfa <= -tol_piv)
+ { /* xB[i] decreases */
+ /* determine actual lower bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* xB[i] has no actual lower bound */
+ continue;
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* actual lower bound of xB[i] is its upper bound */
+ lk = u[k];
+ xassert(lk != +DBL_MAX);
+ i_flag = 1;
+ }
+ else
+ { /* actual lower bound of xB[i] is its original bound */
+ lk = l[k];
+ if (lk == -DBL_MAX)
+ continue;
+ i_flag = 0;
+ }
+ /* determine teta on which xB[i] reaches its lower bound */
+ teta = (lk - beta[i]) / alfa;
+ }
+ else if (alfa >= +tol_piv)
+ { /* xB[i] increases */
+ /* determine actual upper bound of xB[i] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* actual upper bound of xB[i] is its lower bound */
+ uk = l[k];
+ xassert(uk != -DBL_MAX);
+ i_flag = 0;
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* xB[i] has no actual upper bound */
+ continue;
+ }
+ else
+ { /* actual upper bound of xB[i] is its original bound */
+ uk = u[k];
+ if (uk == +DBL_MAX)
+ continue;
+ i_flag = 1;
+ }
+ /* determine teta on which xB[i] reaches its upper bound */
+ teta = (uk - beta[i]) / alfa;
+ }
+ else
+ { /* xB[i] does not depend on teta */
+ continue;
+ }
+ /* choose basic variable for which teta is not greater than
+ * teta_min determined for relaxed bounds and which has best
+ * (largest in magnitude) pivot */
+ alfa = (alfa >= 0.0 ? +alfa : -alfa);
+ if (teta <= teta_min && biga < alfa)
+ p = i, *p_flag = i_flag, biga = alfa;
+ }
+ /* something must be chosen */
+ xassert(1 <= p && p <= m);
+ /* if xB[p] is fixed variable, adjust its bound flag */
+ k = head[p];
+ if (l[k] == u[k])
+ *p_flag = 0;
+done: return p;
+}
+
+#if 1 /* 22/VI-2017 */
+/***********************************************************************
+* spx_ls_eval_bp - determine penalty function break points
+*
+* This routine determines break points of the penalty function (which
+* is the sum of primal infeasibilities).
+*
+* The parameters lp, beta, q, dq, tcol, and tol_piv have the same
+* meaning as for the routine spx_chuzr_std (see above).
+*
+* The routine stores the break-points determined to the array elements
+* bp[1], ..., bp[nbp] in *arbitrary* order, where 0 <= nbp <= 2*m+1 is
+* the number of break-points returned by the routine on exit. */
+
+int spx_ls_eval_bp(SPXLP *lp, const double beta[/*1+m*/],
+ int q, double dq, const double tcol[/*1+m*/], double tol_piv,
+ SPXBP bp[/*1+2*m+1*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, k, nbp;
+ double s, alfa;
+ xassert(1 <= q && q <= n-m);
+ xassert(dq != 0.0);
+ s = (dq < 0.0 ? +1.0 : -1.0);
+ nbp = 0;
+ /* if chosen non-basic variable xN[q] is double-bounded, include
+ * it in the list, because it can cross its opposite bound */
+ k = head[m+q]; /* x[k] = xN[q] */
+ if (l[k] != -DBL_MAX && u[k] != +DBL_MAX)
+ { nbp++;
+ bp[nbp].i = 0;
+ xassert(l[k] < u[k]); /* xN[q] cannot be fixed */
+ bp[nbp].teta = u[k] - l[k];
+ bp[nbp].dc = s;
+ }
+ /* build the list of all basic variables xB[i] that can cross
+ * their bound(s) for the ray parameter 0 <= teta < teta_max */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ xassert(l[k] <= u[k]);
+ /* determine alfa such that (delta xB[i]) = alfa * teta */
+ alfa = s * tcol[i];
+ if (alfa >= +tol_piv)
+ { /* xB[i] increases on increasing teta */
+ if (l[k] == u[k])
+ { /* xB[i] is fixed at lB[i] = uB[i] */
+ if (c[k] <= 0.0)
+ { /* increasing xB[i] can cross its fixed value lB[i],
+ * because currently xB[i] <= lB[i] */
+ nbp++;
+ bp[nbp].i = +i;
+ bp[nbp].teta = (l[k] - beta[i]) / alfa;
+ /* if xB[i] > lB[i] then cB[i] = +1 */
+ bp[nbp].dc = +1.0 - c[k];
+ }
+ }
+ else
+ { if (l[k] != -DBL_MAX && c[k] < 0.0)
+ { /* increasing xB[i] can cross its lower bound lB[i],
+ * because currently xB[i] < lB[i] */
+ nbp++;
+ bp[nbp].i = +i;
+ bp[nbp].teta = (l[k] - beta[i]) / alfa;
+ bp[nbp].dc = +1.0;
+ }
+ if (u[k] != +DBL_MAX && c[k] <= 0.0)
+ { /* increasing xB[i] can cross its upper bound uB[i],
+ * because currently xB[i] does not violate it */
+ nbp++;
+ bp[nbp].i = -i;
+ bp[nbp].teta = (u[k] - beta[i]) / alfa;
+ bp[nbp].dc = +1.0;
+ }
+ }
+ }
+ else if (alfa <= -tol_piv)
+ { /* xB[i] decreases on increasing teta */
+ if (l[k] == u[k])
+ { /* xB[i] is fixed at lB[i] = uB[i] */
+ if (c[k] >= 0.0)
+ { /* decreasing xB[i] can cross its fixed value lB[i],
+ * because currently xB[i] >= lB[i] */
+ nbp++;
+ bp[nbp].i = +i;
+ bp[nbp].teta = (l[k] - beta[i]) / alfa;
+ /* if xB[i] < lB[i] then cB[i] = -1 */
+ bp[nbp].dc = -1.0 - c[k];
+ }
+ }
+ else
+ { if (l[k] != -DBL_MAX && c[k] >= 0.0)
+ { /* decreasing xB[i] can cross its lower bound lB[i],
+ * because currently xB[i] does not violate it */
+ nbp++;
+ bp[nbp].i = +i;
+ bp[nbp].teta = (l[k] - beta[i]) / alfa;
+ bp[nbp].dc = -1.0;
+ }
+ if (u[k] != +DBL_MAX && c[k] > 0.0)
+ { /* decreasing xB[i] can cross its upper bound uB[i],
+ * because currently xB[i] > uB[i] */
+ nbp++;
+ bp[nbp].i = -i;
+ bp[nbp].teta = (u[k] - beta[i]) / alfa;
+ bp[nbp].dc = -1.0;
+ }
+ }
+ }
+ else
+ { /* xB[i] does not depend on teta within a tolerance */
+ continue;
+ }
+ /* teta < 0 may happen only due to round-off errors when the
+ * current value of xB[i] is *close* to its (lower or upper)
+ * bound; in this case we replace teta by exact zero */
+ if (bp[nbp].teta < 0.0)
+ bp[nbp].teta = 0.0;
+ }
+ xassert(nbp <= 2*m+1);
+ return nbp;
+}
+#endif
+
+#if 1 /* 22/VI-2017 */
+/***********************************************************************
+* spx_ls_select_bp - select and process penalty function break points
+*
+* This routine selects a next portion of the penalty function break
+* points and processes them.
+*
+* On entry to the routine it is assumed that break points bp[1], ...,
+* bp[num] are already processed, and slope is the penalty function
+* slope to the right of the last processed break point bp[num].
+* (Initially, when num = 0, slope should be specified as -fabs(d[q]),
+* where d[q] is the reduced cost of chosen non-basic variable xN[q].)
+*
+* The routine selects break points among bp[num+1], ..., bp[nbp], for
+* which teta <= teta_lim, and moves these break points to the array
+* elements bp[num+1], ..., bp[num1], where num <= num1 <= 2*m+1 is the
+* new number of processed break points returned by the routine on
+* exit. Then the routine sorts the break points by ascending teta and
+* computes the change of the penalty function relative to its value at
+* teta = 0.
+*
+* On exit the routine also replaces the parameter slope with a new
+* value that corresponds to the new last break-point bp[num1]. */
+
+static int CDECL fcmp(const void *v1, const void *v2)
+{ const SPXBP *p1 = v1, *p2 = v2;
+ if (p1->teta < p2->teta)
+ return -1;
+ else if (p1->teta > p2->teta)
+ return +1;
+ else
+ return 0;
+}
+
+int spx_ls_select_bp(SPXLP *lp, const double tcol[/*1+m*/],
+ int nbp, SPXBP bp[/*1+m+m+1*/], int num, double *slope, double
+ teta_lim)
+{ int m = lp->m;
+ int i, t, num1;
+ double teta, dz;
+ xassert(0 <= num && num <= nbp && nbp <= m+m+1);
+ /* select a new portion of break points */
+ num1 = num;
+ for (t = num+1; t <= nbp; t++)
+ { if (bp[t].teta <= teta_lim)
+ { /* move break point to the beginning of the new portion */
+ num1++;
+ i = bp[num1].i, teta = bp[num1].teta, dz = bp[num1].dc;
+ bp[num1].i = bp[t].i, bp[num1].teta = bp[t].teta,
+ bp[num1].dc = bp[t].dc;
+ bp[t].i = i, bp[t].teta = teta, bp[t].dc = dz;
+ }
+ }
+ /* sort new break points bp[num+1], ..., bp[num1] by ascending
+ * the ray parameter teta */
+ if (num1 - num > 1)
+ qsort(&bp[num+1], num1 - num, sizeof(SPXBP), fcmp);
+ /* calculate the penalty function change at the new break points
+ * selected */
+ for (t = num+1; t <= num1; t++)
+ { /* calculate the penalty function change relative to its value
+ * at break point bp[t-1] */
+ dz = (*slope) * (bp[t].teta - (t == 1 ? 0.0 : bp[t-1].teta));
+ /* calculate the penalty function change relative to its value
+ * at teta = 0 */
+ bp[t].dz = (t == 1 ? 0.0 : bp[t-1].dz) + dz;
+ /* calculate a new slope of the penalty function to the right
+ * of the current break point bp[t] */
+ i = (bp[t].i >= 0 ? bp[t].i : -bp[t].i);
+ xassert(0 <= i && i <= m);
+ if (i == 0)
+ *slope += fabs(1.0 * bp[t].dc);
+ else
+ *slope += fabs(tcol[i] * bp[t].dc);
+ }
+ return num1;
+}
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxchuzr.h b/test/monniaux/glpk-4.65/src/simplex/spxchuzr.h
new file mode 100644
index 00000000..3ec90050
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxchuzr.h
@@ -0,0 +1,77 @@
+/* spxchuzr.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXCHUZR_H
+#define SPXCHUZR_H
+
+#include "spxlp.h"
+
+#define spx_chuzr_std _glp_spx_chuzr_std
+int spx_chuzr_std(SPXLP *lp, int phase, const double beta[/*1+m*/],
+ int q, double s, const double tcol[/*1+m*/], int *p_flag,
+ double tol_piv, double tol, double tol1);
+/* choose basic variable (textbook ratio test) */
+
+#define spx_chuzr_harris _glp_spx_chuzr_harris
+int spx_chuzr_harris(SPXLP *lp, int phase, const double beta[/*1+m*/],
+ int q, double s, const double tcol[/*1+m*/], int *p_flag,
+ double tol_piv, double tol, double tol1);
+/* choose basic variable (Harris' ratio test) */
+
+#if 1 /* 22/VI-2017 */
+typedef struct SPXBP SPXBP;
+
+struct SPXBP
+{ /* penalty function (sum of infeasibilities) break point */
+ int i;
+ /* basic variable xB[i], 1 <= i <= m, that intersects its bound
+ * at this break point
+ * i > 0 if xB[i] intersects its lower bound (or fixed value)
+ * i < 0 if xB[i] intersects its upper bound
+ * i = 0 if xN[q] intersects its opposite bound */
+ double teta;
+ /* ray parameter value, teta >= 0, at this break point */
+ double dc;
+ /* increment of the penalty function coefficient cB[i] at this
+ * break point */
+ double dz;
+ /* increment, z[t] - z[0], of the penalty function at this break
+ * point */
+};
+
+#define spx_ls_eval_bp _glp_spx_ls_eval_bp
+int spx_ls_eval_bp(SPXLP *lp, const double beta[/*1+m*/],
+ int q, double dq, const double tcol[/*1+m*/], double tol_piv,
+ SPXBP bp[/*1+2*m+1*/]);
+/* determine penalty function break points */
+
+#define spx_ls_select_bp _glp_spx_ls_select_bp
+int spx_ls_select_bp(SPXLP *lp, const double tcol[/*1+m*/],
+ int nbp, SPXBP bp[/*1+m+m+1*/], int num, double *slope, double
+ teta_lim);
+/* select and process penalty function break points */
+#endif
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxlp.c b/test/monniaux/glpk-4.65/src/simplex/spxlp.c
new file mode 100644
index 00000000..90ce2636
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxlp.c
@@ -0,0 +1,819 @@
+/* spxlp.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxlp.h"
+
+/***********************************************************************
+* spx_factorize - compute factorization of current basis matrix
+*
+* This routine computes factorization of the current basis matrix B.
+*
+* If the factorization has been successfully computed, the routine
+* validates it and returns zero. Otherwise, the routine invalidates
+* the factorization and returns the code provided by the factorization
+* driver (bfd_factorize). */
+
+static int jth_col(void *info, int j, int ind[], double val[])
+{ /* provide column B[j] */
+ SPXLP *lp = info;
+ int m = lp->m;
+ int *A_ptr = lp->A_ptr;
+ int *head = lp->head;
+ int k, ptr, len;
+ xassert(1 <= j && j <= m);
+ k = head[j]; /* x[k] = xB[j] */
+ ptr = A_ptr[k];
+ len = A_ptr[k+1] - ptr;
+ memcpy(&ind[1], &lp->A_ind[ptr], len * sizeof(int));
+ memcpy(&val[1], &lp->A_val[ptr], len * sizeof(double));
+ return len;
+}
+
+int spx_factorize(SPXLP *lp)
+{ int ret;
+ ret = bfd_factorize(lp->bfd, lp->m, jth_col, lp);
+ lp->valid = (ret == 0);
+ return ret;
+}
+
+/***********************************************************************
+* spx_eval_beta - compute current values of basic variables
+*
+* This routine computes vector beta = (beta[i]) of current values of
+* basic variables xB = (xB[i]). (Factorization of the current basis
+* matrix should be valid.)
+*
+* First the routine computes a modified vector of right-hand sides:
+*
+* n-m
+* y = b - N * f = b - sum N[j] * f[j],
+* j=1
+*
+* where b = (b[i]) is the original vector of right-hand sides, N is
+* a matrix composed from columns of the original constraint matrix A,
+* which (columns) correspond to non-basic variables, f = (f[j]) is the
+* vector of active bounds of non-basic variables xN = (xN[j]),
+* N[j] = A[k] is a column of matrix A corresponding to non-basic
+* variable xN[j] = x[k], f[j] is current active bound lN[j] = l[k] or
+* uN[j] = u[k] of non-basic variable xN[j] = x[k]. The matrix-vector
+* product N * f is computed as a linear combination of columns of N,
+* so if f[j] = 0, column N[j] can be skipped.
+*
+* Then the routine performs FTRAN to compute the vector beta:
+*
+* beta = inv(B) * y.
+*
+* On exit the routine stores components of the vector beta to array
+* locations beta[1], ..., beta[m]. */
+
+void spx_eval_beta(SPXLP *lp, double beta[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ double *b = lp->b;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k, ptr, end;
+ double fj, *y;
+ /* compute y = b - N * xN */
+ /* y := b */
+ y = beta;
+ memcpy(&y[1], &b[1], m * sizeof(double));
+ /* y := y - N * f */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* f[j] := active bound of xN[j] */
+ fj = flag[j] ? u[k] : l[k];
+ if (fj == 0.0 || fj == -DBL_MAX)
+ { /* either xN[j] has zero active bound or it is unbounded;
+ * in the latter case its value is assumed to be zero */
+ continue;
+ }
+ /* y := y - N[j] * f[j] */
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ y[A_ind[ptr]] -= A_val[ptr] * fj;
+ }
+ /* compute beta = inv(B) * y */
+ xassert(lp->valid);
+ bfd_ftran(lp->bfd, beta);
+ return;
+}
+
+/***********************************************************************
+* spx_eval_obj - compute current value of objective function
+*
+* This routine computes the value of the objective function in the
+* current basic solution:
+*
+* z = cB'* beta + cN'* f + c[0] =
+*
+* m n-m
+* = sum cB[i] * beta[i] + sum cN[j] * f[j] + c[0],
+* i=1 j=1
+*
+* where cB = (cB[i]) is the vector of objective coefficients at basic
+* variables, beta = (beta[i]) is the vector of current values of basic
+* variables, cN = (cN[j]) is the vector of objective coefficients at
+* non-basic variables, f = (f[j]) is the vector of current active
+* bounds of non-basic variables, c[0] is the constant term of the
+* objective function.
+*
+* It as assumed that components of the vector beta are stored in the
+* array locations beta[1], ..., beta[m]. */
+
+double spx_eval_obj(SPXLP *lp, const double beta[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int i, j, k;
+ double fj, z;
+ /* compute z = cB'* beta + cN'* f + c0 */
+ /* z := c0 */
+ z = c[0];
+ /* z := z + cB'* beta */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ z += c[k] * beta[i];
+ }
+ /* z := z + cN'* f */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* f[j] := active bound of xN[j] */
+ fj = flag[j] ? u[k] : l[k];
+ if (fj == 0.0 || fj == -DBL_MAX)
+ { /* either xN[j] has zero active bound or it is unbounded;
+ * in the latter case its value is assumed to be zero */
+ continue;
+ }
+ z += c[k] * fj;
+ }
+ return z;
+}
+
+/***********************************************************************
+* spx_eval_pi - compute simplex multipliers in current basis
+*
+* This routine computes vector pi = (pi[i]) of simplex multipliers in
+* the current basis. (Factorization of the current basis matrix should
+* be valid.)
+*
+* The vector pi is computed by performing BTRAN:
+*
+* pi = inv(B') * cB,
+*
+* where cB = (cB[i]) is the vector of objective coefficients at basic
+* variables xB = (xB[i]).
+*
+* On exit components of vector pi are stored in the array locations
+* pi[1], ..., pi[m]. */
+
+void spx_eval_pi(SPXLP *lp, double pi[/*1+m*/])
+{ int m = lp->m;
+ double *c = lp->c;
+ int *head = lp->head;
+ int i;
+ double *cB;
+ /* construct cB */
+ cB = pi;
+ for (i = 1; i <= m; i++)
+ cB[i] = c[head[i]];
+ /* compute pi = inv(B) * cB */
+ bfd_btran(lp->bfd, pi);
+ return;
+}
+
+/***********************************************************************
+* spx_eval_dj - compute reduced cost of j-th non-basic variable
+*
+* This routine computes reduced cost d[j] of non-basic variable
+* xN[j] = x[k], 1 <= j <= n-m, in the current basic solution:
+*
+* d[j] = c[k] - A'[k] * pi,
+*
+* where c[k] is the objective coefficient at x[k], A[k] is k-th column
+* of the constraint matrix, pi is the vector of simplex multipliers in
+* the current basis.
+*
+* It as assumed that components of the vector pi are stored in the
+* array locations pi[1], ..., pi[m]. */
+
+double spx_eval_dj(SPXLP *lp, const double pi[/*1+m*/], int j)
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int k, ptr, end;
+ double dj;
+ xassert(1 <= j && j <= n-m);
+ k = lp->head[m+j]; /* x[k] = xN[j] */
+ /* dj := c[k] */
+ dj = lp->c[k];
+ /* dj := dj - A'[k] * pi */
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ dj -= A_val[ptr] * pi[A_ind[ptr]];
+ return dj;
+}
+
+/***********************************************************************
+* spx_eval_tcol - compute j-th column of simplex table
+*
+* This routine computes j-th column of the current simplex table
+* T = (T[i,j]) = - inv(B) * N, 1 <= j <= n-m. (Factorization of the
+* current basis matrix should be valid.)
+*
+* The simplex table column is computed by performing FTRAN:
+*
+* tcol = - inv(B) * N[j],
+*
+* where B is the current basis matrix, N[j] = A[k] is a column of the
+* constraint matrix corresponding to non-basic variable xN[j] = x[k].
+*
+* On exit components of the simplex table column are stored in the
+* array locations tcol[1], ... tcol[m]. */
+
+void spx_eval_tcol(SPXLP *lp, int j, double tcol[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int *head = lp->head;
+ int i, k, ptr, end;
+ xassert(1 <= j && j <= n-m);
+ k = head[m+j]; /* x[k] = xN[j] */
+ /* compute tcol = - inv(B) * N[j] */
+ for (i = 1; i <= m; i++)
+ tcol[i] = 0.0;
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ tcol[A_ind[ptr]] = -A_val[ptr];
+ bfd_ftran(lp->bfd, tcol);
+ return;
+}
+
+/***********************************************************************
+* spx_eval_rho - compute i-th row of basis matrix inverse
+*
+* This routine computes i-th row of the matrix inv(B), where B is
+* the current basis matrix, 1 <= i <= m. (Factorization of the current
+* basis matrix should be valid.)
+*
+* The inverse row is computed by performing BTRAN:
+*
+* rho = inv(B') * e[i],
+*
+* where e[i] is i-th column of unity matrix.
+*
+* On exit components of the row are stored in the array locations
+* row[1], ..., row[m]. */
+
+void spx_eval_rho(SPXLP *lp, int i, double rho[/*1+m*/])
+{ int m = lp->m;
+ int j;
+ xassert(1 <= i && i <= m);
+ /* compute rho = inv(B') * e[i] */
+ for (j = 1; j <= m; j++)
+ rho[j] = 0.0;
+ rho[i] = 1.0;
+ bfd_btran(lp->bfd, rho);
+ return;
+}
+
+#if 1 /* 31/III-2016 */
+void spx_eval_rho_s(SPXLP *lp, int i, FVS *rho)
+{ /* sparse version of spx_eval_rho */
+ int m = lp->m;
+ xassert(1 <= i && i <= m);
+ /* compute rho = inv(B') * e[i] */
+ xassert(rho->n == m);
+ fvs_clear_vec(rho);
+ rho->nnz = 1;
+ rho->ind[1] = i;
+ rho->vec[i] = 1.0;
+ bfd_btran_s(lp->bfd, rho);
+ return;
+}
+#endif
+
+/***********************************************************************
+* spx_eval_tij - compute element T[i,j] of simplex table
+*
+* This routine computes element T[i,j] of the current simplex table
+* T = - inv(B) * N, 1 <= i <= m, 1 <= j <= n-m, with the following
+* formula:
+*
+* T[i,j] = - N'[j] * rho, (1)
+*
+* where N[j] = A[k] is a column of the constraint matrix corresponding
+* to non-basic variable xN[j] = x[k], rho is i-th row of the inverse
+* matrix inv(B).
+*
+* It as assumed that components of the inverse row rho = (rho[j]) are
+* stored in the array locations rho[1], ..., rho[m]. */
+
+double spx_eval_tij(SPXLP *lp, const double rho[/*1+m*/], int j)
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int k, ptr, end;
+ double tij;
+ xassert(1 <= j && j <= n-m);
+ k = lp->head[m+j]; /* x[k] = xN[j] */
+ /* compute t[i,j] = - N'[j] * pi */
+ tij = 0.0;
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ tij -= A_val[ptr] * rho[A_ind[ptr]];
+ return tij;
+}
+
+/***********************************************************************
+* spx_eval_trow - compute i-th row of simplex table
+*
+* This routine computes i-th row of the current simplex table
+* T = (T[i,j]) = - inv(B) * N, 1 <= i <= m.
+*
+* Elements of the row T[i] = (T[i,j]), j = 1, ..., n-m, are computed
+* directly with the routine spx_eval_tij.
+*
+* The vector rho = (rho[j]), which is i-th row of the basis inverse
+* inv(B), should be previously computed with the routine spx_eval_rho.
+* It is assumed that elements of this vector are stored in the array
+* locations rho[1], ..., rho[m].
+*
+* On exit components of the simplex table row are stored in the array
+* locations trow[1], ... trow[n-m].
+*
+* NOTE: For testing/debugging only. */
+
+void spx_eval_trow(SPXLP *lp, const double rho[/*1+m*/], double
+ trow[/*1+n-m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int j;
+ for (j = 1; j <= n-m; j++)
+ trow[j] = spx_eval_tij(lp, rho, j);
+ return;
+}
+
+/***********************************************************************
+* spx_update_beta - update values of basic variables
+*
+* This routine updates the vector beta = (beta[i]) of values of basic
+* variables xB = (xB[i]) for the adjacent basis.
+*
+* On entry to the routine components of the vector beta in the current
+* basis should be placed in array locations beta[1], ..., beta[m].
+*
+* The parameter 1 <= p <= m specifies basic variable xB[p] which
+* becomes non-basic variable xN[q] in the adjacent basis. The special
+* case p < 0 means that non-basic variable xN[q] goes from its current
+* active bound to opposite one in the adjacent basis.
+*
+* If the flag p_flag is set, the active bound of xB[p] in the adjacent
+* basis is set to its upper bound. (In this case xB[p] should have its
+* upper bound and should not be fixed.)
+*
+* The parameter 1 <= q <= n-m specifies non-basic variable xN[q] which
+* becomes basic variable xB[p] in the adjacent basis (if 1 <= p <= m),
+* or goes to its opposite bound (if p < 0). (In the latter case xN[q]
+* should have both lower and upper bounds and should not be fixed.)
+*
+* It is assumed that the array tcol contains elements of q-th (pivot)
+* column T[q] of the simple table in locations tcol[1], ..., tcol[m].
+* (This column should be computed for the current basis.)
+*
+* First, the routine determines the increment of basic variable xB[p]
+* in the adjacent basis (but only if 1 <= p <= m):
+*
+* ( - beta[p], if -inf < xB[p] < +inf
+* (
+* delta xB[p] = { lB[p] - beta[p], if p_flag = 0
+* (
+* ( uB[p] - beta[p], if p_flag = 1
+*
+* where beta[p] is the value of xB[p] in the current basis, lB[p] and
+* uB[p] are its lower and upper bounds. Then, the routine determines
+* the increment of non-basic variable xN[q] in the adjacent basis:
+*
+* ( delta xB[p] / T[p,q], if 1 <= p <= m
+* (
+* delta xN[q] = { uN[q] - lN[q], if p < 0 and f[q] = lN[q]
+* (
+* ( lN[q] - uN[q], if p < 0 and f[q] = uN[q]
+*
+* where T[p,q] is the pivot element of the simplex table, f[q] is the
+* active bound of xN[q] in the current basis.
+*
+* If 1 <= p <= m, in the adjacent basis xN[q] becomes xB[p], so:
+*
+* new beta[p] = f[q] + delta xN[q].
+*
+* Values of other basic variables xB[i] for 1 <= i <= m, i != p, are
+* updated as follows:
+*
+* new beta[i] = beta[i] + T[i,q] * delta xN[q].
+*
+* On exit the routine stores updated components of the vector beta to
+* the same locations, where the input vector beta was stored. */
+
+void spx_update_beta(SPXLP *lp, double beta[/*1+m*/], int p,
+ int p_flag, int q, const double tcol[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int i, k;
+ double delta_p, delta_q;
+ if (p < 0)
+ { /* special case: xN[q] goes to its opposite bound */
+ xassert(1 <= q && q <= n-m);
+ /* xN[q] should be double-bounded variable */
+ k = head[m+q]; /* x[k] = xN[q] */
+ xassert(l[k] != -DBL_MAX && u[k] != +DBL_MAX && l[k] != u[k]);
+ /* determine delta xN[q] */
+ if (flag[q])
+ { /* xN[q] goes from its upper bound to its lower bound */
+ delta_q = l[k] - u[k];
+ }
+ else
+ { /* xN[q] goes from its lower bound to its upper bound */
+ delta_q = u[k] - l[k];
+ }
+ }
+ else
+ { /* xB[p] leaves the basis, xN[q] enters the basis */
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* determine delta xB[p] */
+ k = head[p]; /* x[k] = xB[p] */
+ if (p_flag)
+ { /* xB[p] goes to its upper bound */
+ xassert(l[k] != u[k] && u[k] != +DBL_MAX);
+ delta_p = u[k] - beta[p];
+ }
+ else if (l[k] == -DBL_MAX)
+ { /* unbounded xB[p] becomes non-basic (unusual case) */
+ xassert(u[k] == +DBL_MAX);
+ delta_p = 0.0 - beta[p];
+ }
+ else
+ { /* xB[p] goes to its lower bound or becomes fixed */
+ delta_p = l[k] - beta[p];
+ }
+ /* determine delta xN[q] */
+ delta_q = delta_p / tcol[p];
+ /* compute new beta[p], which is the value of xN[q] in the
+ * adjacent basis */
+ k = head[m+q]; /* x[k] = xN[q] */
+ if (flag[q])
+ { /* xN[q] has its upper bound active */
+ xassert(l[k] != u[k] && u[k] != +DBL_MAX);
+ beta[p] = u[k] + delta_q;
+ }
+ else if (l[k] == -DBL_MAX)
+ { /* xN[q] is non-basic unbounded variable */
+ xassert(u[k] == +DBL_MAX);
+ beta[p] = 0.0 + delta_q;
+ }
+ else
+ { /* xN[q] has its lower bound active or is fixed (latter
+ * case is unusual) */
+ beta[p] = l[k] + delta_q;
+ }
+ }
+ /* compute new beta[i] for all i != p */
+ for (i = 1; i <= m; i++)
+ { if (i != p)
+ beta[i] += tcol[i] * delta_q;
+ }
+ return;
+}
+
+#if 1 /* 30/III-2016 */
+void spx_update_beta_s(SPXLP *lp, double beta[/*1+m*/], int p,
+ int p_flag, int q, const FVS *tcol)
+{ /* sparse version of spx_update_beta */
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int nnz = tcol->nnz;
+ int *ind = tcol->ind;
+ double *vec = tcol->vec;
+ int i, k;
+ double delta_p, delta_q;
+ xassert(tcol->n == m);
+ if (p < 0)
+ { /* special case: xN[q] goes to its opposite bound */
+#if 0 /* 11/VI-2017 */
+ /* FIXME: not tested yet */
+ xassert(0);
+#endif
+ xassert(1 <= q && q <= n-m);
+ /* xN[q] should be double-bounded variable */
+ k = head[m+q]; /* x[k] = xN[q] */
+ xassert(l[k] != -DBL_MAX && u[k] != +DBL_MAX && l[k] != u[k]);
+ /* determine delta xN[q] */
+ if (flag[q])
+ { /* xN[q] goes from its upper bound to its lower bound */
+ delta_q = l[k] - u[k];
+ }
+ else
+ { /* xN[q] goes from its lower bound to its upper bound */
+ delta_q = u[k] - l[k];
+ }
+ }
+ else
+ { /* xB[p] leaves the basis, xN[q] enters the basis */
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* determine delta xB[p] */
+ k = head[p]; /* x[k] = xB[p] */
+ if (p_flag)
+ { /* xB[p] goes to its upper bound */
+ xassert(l[k] != u[k] && u[k] != +DBL_MAX);
+ delta_p = u[k] - beta[p];
+ }
+ else if (l[k] == -DBL_MAX)
+ { /* unbounded xB[p] becomes non-basic (unusual case) */
+ xassert(u[k] == +DBL_MAX);
+ delta_p = 0.0 - beta[p];
+ }
+ else
+ { /* xB[p] goes to its lower bound or becomes fixed */
+ delta_p = l[k] - beta[p];
+ }
+ /* determine delta xN[q] */
+ delta_q = delta_p / vec[p];
+ /* compute new beta[p], which is the value of xN[q] in the
+ * adjacent basis */
+ k = head[m+q]; /* x[k] = xN[q] */
+ if (flag[q])
+ { /* xN[q] has its upper bound active */
+ xassert(l[k] != u[k] && u[k] != +DBL_MAX);
+ beta[p] = u[k] + delta_q;
+ }
+ else if (l[k] == -DBL_MAX)
+ { /* xN[q] is non-basic unbounded variable */
+ xassert(u[k] == +DBL_MAX);
+ beta[p] = 0.0 + delta_q;
+ }
+ else
+ { /* xN[q] has its lower bound active or is fixed (latter
+ * case is unusual) */
+ beta[p] = l[k] + delta_q;
+ }
+ }
+ /* compute new beta[i] for all i != p */
+ for (k = 1; k <= nnz; k++)
+ { i = ind[k];
+ if (i != p)
+ beta[i] += vec[i] * delta_q;
+ }
+ return;
+}
+#endif
+
+/***********************************************************************
+* spx_update_d - update reduced costs of non-basic variables
+*
+* This routine updates the vector d = (d[j]) of reduced costs of
+* non-basic variables xN = (xN[j]) for the adjacent basis.
+*
+* On entry to the routine components of the vector d in the current
+* basis should be placed in locations d[1], ..., d[n-m].
+*
+* The parameter 1 <= p <= m specifies basic variable xB[p] which
+* becomes non-basic variable xN[q] in the adjacent basis.
+*
+* The parameter 1 <= q <= n-m specified non-basic variable xN[q] which
+* becomes basic variable xB[p] in the adjacent basis.
+*
+* It is assumed that the array trow contains elements of p-th (pivot)
+* row T'[p] of the simplex table in locations trow[1], ..., trow[n-m].
+* It is also assumed that the array tcol contains elements of q-th
+* (pivot) column T[q] of the simple table in locations tcol[1], ...,
+* tcol[m]. (These row and column should be computed for the current
+* basis.)
+*
+* First, the routine computes more accurate reduced cost d[q] in the
+* current basis using q-th column of the simplex table:
+*
+* n-m
+* d[q] = cN[q] + sum t[i,q] * cB[i],
+* i=1
+*
+* where cN[q] and cB[i] are objective coefficients at variables xN[q]
+* and xB[i], resp. The routine also computes the relative error:
+*
+* e = |d[q] - d'[q]| / (1 + |d[q]|),
+*
+* where d'[q] is the reduced cost of xN[q] on entry to the routine,
+* and returns e on exit. (If e happens to be large enough, the calling
+* program may compute the reduced costs directly, since other reduced
+* costs also may be inaccurate.)
+*
+* In the adjacent basis xB[p] becomes xN[q], so:
+*
+* new d[q] = d[q] / T[p,q],
+*
+* where T[p,q] is the pivot element of the simplex table (it is taken
+* from column T[q] as more accurate). Reduced costs of other non-basic
+* variables xN[j] for 1 <= j <= n-m, j != q, are updated as follows:
+*
+* new d[j] = d[j] + T[p,j] * new d[q].
+*
+* On exit the routine stores updated components of the vector d to the
+* same locations, where the input vector d was stored. */
+
+double spx_update_d(SPXLP *lp, double d[/*1+n-m*/], int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ int *head = lp->head;
+ int i, j, k;
+ double dq, e;
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n);
+ /* compute d[q] in current basis more accurately */
+ k = head[m+q]; /* x[k] = xN[q] */
+ dq = c[k];
+ for (i = 1; i <= m; i++)
+ dq += tcol[i] * c[head[i]];
+ /* compute relative error in d[q] */
+ e = fabs(dq - d[q]) / (1.0 + fabs(dq));
+ /* compute new d[q], which is the reduced cost of xB[p] in the
+ * adjacent basis */
+ d[q] = (dq /= tcol[p]);
+ /* compute new d[j] for all j != q */
+ for (j = 1; j <= n-m; j++)
+ { if (j != q)
+ d[j] -= trow[j] * dq;
+ }
+ return e;
+}
+
+#if 1 /* 30/III-2016 */
+double spx_update_d_s(SPXLP *lp, double d[/*1+n-m*/], int p, int q,
+ const FVS *trow, const FVS *tcol)
+{ /* sparse version of spx_update_d */
+ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ int *head = lp->head;
+ int trow_nnz = trow->nnz;
+ int *trow_ind = trow->ind;
+ double *trow_vec = trow->vec;
+ int tcol_nnz = tcol->nnz;
+ int *tcol_ind = tcol->ind;
+ double *tcol_vec = tcol->vec;
+ int i, j, k;
+ double dq, e;
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n);
+ xassert(trow->n == n-m);
+ xassert(tcol->n == m);
+ /* compute d[q] in current basis more accurately */
+ k = head[m+q]; /* x[k] = xN[q] */
+ dq = c[k];
+ for (k = 1; k <= tcol_nnz; k++)
+ { i = tcol_ind[k];
+ dq += tcol_vec[i] * c[head[i]];
+ }
+ /* compute relative error in d[q] */
+ e = fabs(dq - d[q]) / (1.0 + fabs(dq));
+ /* compute new d[q], which is the reduced cost of xB[p] in the
+ * adjacent basis */
+ d[q] = (dq /= tcol_vec[p]);
+ /* compute new d[j] for all j != q */
+ for (k = 1; k <= trow_nnz; k++)
+ { j = trow_ind[k];
+ if (j != q)
+ d[j] -= trow_vec[j] * dq;
+ }
+ return e;
+}
+#endif
+
+/***********************************************************************
+* spx_change_basis - change current basis to adjacent one
+*
+* This routine changes the current basis to the adjacent one making
+* necessary changes in lp->head and lp->flag members.
+*
+* The parameters p, p_flag, and q have the same meaning as for the
+* routine spx_update_beta. */
+
+void spx_change_basis(SPXLP *lp, int p, int p_flag, int q)
+{ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int k;
+ if (p < 0)
+ { /* special case: xN[q] goes to its opposite bound */
+ xassert(1 <= q && q <= n-m);
+ /* xN[q] should be double-bounded variable */
+ k = head[m+q]; /* x[k] = xN[q] */
+ xassert(l[k] != -DBL_MAX && u[k] != +DBL_MAX && l[k] != u[k]);
+ /* change active bound flag */
+ flag[q] = 1 - flag[q];
+ }
+ else
+ { /* xB[p] leaves the basis, xN[q] enters the basis */
+ xassert(1 <= p && p <= m);
+ xassert(p_flag == 0 || p_flag == 1);
+ xassert(1 <= q && q <= n-m);
+ k = head[p]; /* xB[p] = x[k] */
+ if (p_flag)
+ { /* xB[p] goes to its upper bound */
+ xassert(l[k] != u[k] && u[k] != +DBL_MAX);
+ }
+ /* swap xB[p] and xN[q] in the basis */
+ head[p] = head[m+q], head[m+q] = k;
+ /* and set active bound flag for new xN[q] */
+ lp->flag[q] = p_flag;
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_update_invb - update factorization of basis matrix
+*
+* This routine updates factorization of the basis matrix B when i-th
+* column of B is replaced by k-th column of the constraint matrix A.
+*
+* The parameter 1 <= i <= m specifies the number of column of matrix B
+* to be replaced by a new column.
+*
+* The parameter 1 <= k <= n specifies the number of column of matrix A
+* to be used for replacement.
+*
+* If the factorization has been successfully updated, the routine
+* validates it and returns zero. Otherwise, the routine invalidates
+* the factorization and returns the code provided by the factorization
+* driver (bfd_update). */
+
+int spx_update_invb(SPXLP *lp, int i, int k)
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int ptr, len, ret;
+ xassert(1 <= i && i <= m);
+ xassert(1 <= k && k <= n);
+ ptr = A_ptr[k];
+ len = A_ptr[k+1] - ptr;
+ ret = bfd_update(lp->bfd, i, len, &A_ind[ptr-1], &A_val[ptr-1]);
+ lp->valid = (ret == 0);
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxlp.h b/test/monniaux/glpk-4.65/src/simplex/spxlp.h
new file mode 100644
index 00000000..29a135fe
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxlp.h
@@ -0,0 +1,234 @@
+/* spxlp.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXLP_H
+#define SPXLP_H
+
+#include "bfd.h"
+
+/***********************************************************************
+* The structure SPXLP describes LP problem and its current basis.
+*
+* It is assumed that LP problem has the following formulation (this is
+* so called "working format"):
+*
+* z = c'* x + c0 -> min (1)
+*
+* A * x = b (2)
+*
+* l <= x <= u (3)
+*
+* where:
+*
+* x = (x[k]) is a n-vector of variables;
+*
+* z is an objective function;
+*
+* c = (c[k]) is a n-vector of objective coefficients;
+*
+* c0 is a constant term of the objective function;
+*
+* A = (a[i,k]) is a mxn-matrix of constraint coefficients;
+*
+* b = (b[i]) is a m-vector of right-hand sides;
+*
+* l = (l[k]) is a n-vector of lower bounds of variables;
+*
+* u = (u[k]) is a n-vector of upper bounds of variables.
+*
+* If variable x[k] has no lower (upper) bound, it is formally assumed
+* that l[k] = -inf (u[k] = +inf). Variable having no bounds is called
+* free (unbounded) variable. If l[k] = u[k], variable x[k] is assumed
+* to be fixed.
+*
+* It is also assumed that matrix A has full row rank: rank(A) = m,
+* i.e. all its rows are linearly independent, so m <= n.
+*
+* The (current) basis is defined by an appropriate permutation matrix
+* P of order n such that:
+*
+* ( xB )
+* P * x = ( ), (4)
+* ( xN )
+*
+* where xB = (xB[i]) is a m-vector of basic variables, xN = (xN[j]) is
+* a (n-m)-vector of non-basic variables. If a non-basic variable xN[j]
+* has both lower and upper bounds, there is used an additional flag to
+* indicate which bound is active.
+*
+* From (2) and (4) it follows that:
+*
+* A * P'* P * x = b <=> B * xB + N * xN = b, (5)
+*
+* where P' is a matrix transposed to P, and
+*
+* A * P' = (B | N). (6)
+*
+* Here B is the basis matrix, which is a square non-singular matrix
+* of order m composed from columns of matrix A that correspond to
+* basic variables xB, and N is a mx(n-m) matrix composed from columns
+* of matrix A that correspond to non-basic variables xN. */
+
+typedef struct SPXLP SPXLP;
+
+struct SPXLP
+{ /* LP problem data and its (current) basis */
+ int m;
+ /* number of equality constraints, m > 0 */
+ int n;
+ /* number of variables, n >= m */
+ int nnz;
+ /* number of non-zeros in constraint matrix A */
+ /*--------------------------------------------------------------*/
+ /* mxn-matrix A of constraint coefficients in sparse column-wise
+ * format */
+ int *A_ptr; /* int A_ptr[1+n+1]; */
+ /* A_ptr[0] is not used;
+ * A_ptr[k], 1 <= k <= n, is starting position of k-th column in
+ * arrays A_ind and A_val; note that A_ptr[1] is always 1;
+ * A_ptr[n+1] indicates the position after the last element in
+ * arrays A_ind and A_val, i.e. A_ptr[n+1] = nnz+1, where nnz is
+ * the number of non-zero elements in matrix A;
+ * the length of k-th column (the number of non-zero elements in
+ * that column) can be calculated as A_ptr[k+1] - A_ptr[k] */
+ int *A_ind; /* int A_ind[1+nnz]; */
+ /* row indices */
+ double *A_val; /* double A_val[1+nnz]; */
+ /* non-zero element values (constraint coefficients) */
+ /*--------------------------------------------------------------*/
+ /* principal vectors of LP formulation */
+ double *b; /* double b[1+m]; */
+ /* b[0] is not used;
+ * b[i], 1 <= i <= m, is the right-hand side of i-th equality
+ * constraint */
+ double *c; /* double c[1+n]; */
+ /* c[0] is the constant term of the objective function;
+ * c[k], 1 <= k <= n, is the objective function coefficient at
+ * variable x[k] */
+ double *l; /* double l[1+n]; */
+ /* l[0] is not used;
+ * l[k], 1 <= k <= n, is the lower bound of variable x[k];
+ * if x[k] has no lower bound, l[k] = -DBL_MAX */
+ double *u; /* double u[1+n]; */
+ /* u[0] is not used;
+ * u[k], 1 <= k <= n, is the upper bound of variable u[k];
+ * if x[k] has no upper bound, u[k] = +DBL_MAX;
+ * note that l[k] = u[k] means that x[k] is fixed variable */
+ /*--------------------------------------------------------------*/
+ /* LP basis */
+ int *head; /* int head[1+n]; */
+ /* basis header, which is permutation matrix P (4):
+ * head[0] is not used;
+ * head[i] = k means that xB[i] = x[k], 1 <= i <= m;
+ * head[m+j] = k, means that xN[j] = x[k], 1 <= j <= n-m */
+ char *flag; /* char flag[1+n-m]; */
+ /* flags of non-basic variables:
+ * flag[0] is not used;
+ * flag[j], 1 <= j <= n-m, indicates that non-basic variable
+ * xN[j] is non-fixed and has its upper bound active */
+ /*--------------------------------------------------------------*/
+ /* basis matrix B of order m stored in factorized form */
+ int valid;
+ /* factorization validity flag */
+ BFD *bfd;
+ /* driver to factorization of the basis matrix */
+};
+
+#define spx_factorize _glp_spx_factorize
+int spx_factorize(SPXLP *lp);
+/* compute factorization of current basis matrix */
+
+#define spx_eval_beta _glp_spx_eval_beta
+void spx_eval_beta(SPXLP *lp, double beta[/*1+m*/]);
+/* compute values of basic variables */
+
+#define spx_eval_obj _glp_spx_eval_obj
+double spx_eval_obj(SPXLP *lp, const double beta[/*1+m*/]);
+/* compute value of objective function */
+
+#define spx_eval_pi _glp_spx_eval_pi
+void spx_eval_pi(SPXLP *lp, double pi[/*1+m*/]);
+/* compute simplex multipliers */
+
+#define spx_eval_dj _glp_spx_eval_dj
+double spx_eval_dj(SPXLP *lp, const double pi[/*1+m*/], int j);
+/* compute reduced cost of j-th non-basic variable */
+
+#define spx_eval_tcol _glp_spx_eval_tcol
+void spx_eval_tcol(SPXLP *lp, int j, double tcol[/*1+m*/]);
+/* compute j-th column of simplex table */
+
+#define spx_eval_rho _glp_spx_eval_rho
+void spx_eval_rho(SPXLP *lp, int i, double rho[/*1+m*/]);
+/* compute i-th row of basis matrix inverse */
+
+#if 1 /* 31/III-2016 */
+#define spx_eval_rho_s _glp_spx_eval_rho_s
+void spx_eval_rho_s(SPXLP *lp, int i, FVS *rho);
+/* sparse version of spx_eval_rho */
+#endif
+
+#define spx_eval_tij _glp_spx_eval_tij
+double spx_eval_tij(SPXLP *lp, const double rho[/*1+m*/], int j);
+/* compute element T[i,j] of simplex table */
+
+#define spx_eval_trow _glp_spx_eval_trow
+void spx_eval_trow(SPXLP *lp, const double rho[/*1+m*/], double
+ trow[/*1+n-m*/]);
+/* compute i-th row of simplex table */
+
+#define spx_update_beta _glp_spx_update_beta
+void spx_update_beta(SPXLP *lp, double beta[/*1+m*/], int p,
+ int p_flag, int q, const double tcol[/*1+m*/]);
+/* update values of basic variables */
+
+#if 1 /* 30/III-2016 */
+#define spx_update_beta_s _glp_spx_update_beta_s
+void spx_update_beta_s(SPXLP *lp, double beta[/*1+m*/], int p,
+ int p_flag, int q, const FVS *tcol);
+/* sparse version of spx_update_beta */
+#endif
+
+#define spx_update_d _glp_spx_update_d
+double spx_update_d(SPXLP *lp, double d[/*1+n-m*/], int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/]);
+/* update reduced costs of non-basic variables */
+
+#if 1 /* 30/III-2016 */
+#define spx_update_d_s _glp_spx_update_d_s
+double spx_update_d_s(SPXLP *lp, double d[/*1+n-m*/], int p, int q,
+ const FVS *trow, const FVS *tcol);
+/* sparse version of spx_update_d */
+#endif
+
+#define spx_change_basis _glp_spx_change_basis
+void spx_change_basis(SPXLP *lp, int p, int p_flag, int q);
+/* change current basis to adjacent one */
+
+#define spx_update_invb _glp_spx_update_invb
+int spx_update_invb(SPXLP *lp, int i, int k);
+/* update factorization of basis matrix */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxnt.c b/test/monniaux/glpk-4.65/src/simplex/spxnt.c
new file mode 100644
index 00000000..7eaac852
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxnt.c
@@ -0,0 +1,303 @@
+/* spxnt.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxnt.h"
+
+/***********************************************************************
+* spx_alloc_nt - allocate matrix N in sparse row-wise format
+*
+* This routine allocates the memory for arrays needed to represent the
+* matrix N composed of non-basic columns of the constraint matrix A. */
+
+void spx_alloc_nt(SPXLP *lp, SPXNT *nt)
+{ int m = lp->m;
+ int nnz = lp->nnz;
+ nt->ptr = talloc(1+m, int);
+ nt->len = talloc(1+m, int);
+ nt->ind = talloc(1+nnz, int);
+ nt->val = talloc(1+nnz, double);
+ return;
+}
+
+/***********************************************************************
+* spx_init_nt - initialize row pointers for matrix N
+*
+* This routine initializes (sets up) row pointers for the matrix N
+* using column-wise representation of the constraint matrix A.
+*
+* This routine needs to be called only once. */
+
+void spx_init_nt(SPXLP *lp, SPXNT *nt)
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ int *NT_ptr = nt->ptr;
+ int *NT_len = nt->len;
+ int i, k, ptr, end;
+ /* calculate NT_len[i] = maximal number of non-zeros in i-th row
+ * of N = number of non-zeros in i-th row of A */
+ memset(&NT_len[1], 0, m * sizeof(int));
+ for (k = 1; k <= n; k++)
+ { ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ NT_len[A_ind[ptr]]++;
+ }
+ /* initialize row pointers NT_ptr[i], i = 1,...,n-m */
+ NT_ptr[1] = 1;
+ for (i = 2; i <= m; i++)
+ NT_ptr[i] = NT_ptr[i-1] + NT_len[i-1];
+ xassert(NT_ptr[m] + NT_len[m] == nnz+1);
+ return;
+}
+
+/***********************************************************************
+* spx_nt_add_col - add column N[j] = A[k] to matrix N
+*
+* This routine adds elements of column N[j] = A[k], 1 <= j <= n-m,
+* 1 <= k <= n, to the row-wise represntation of the matrix N. It is
+* assumed (with no check) that elements of the specified column are
+* missing in the row-wise represntation of N. */
+
+void spx_nt_add_col(SPXLP *lp, SPXNT *nt, int j, int k)
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ int *NT_ptr = nt->ptr;
+ int *NT_len = nt->len;
+ int *NT_ind = nt->ind;
+ double *NT_val = nt->val;
+ int i, ptr, end, pos;
+ xassert(1 <= j && j <= n-m);
+ xassert(1 <= k && k <= n);
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ { i = A_ind[ptr];
+ /* add element N[i,j] = A[i,k] to i-th row of matrix N */
+ pos = NT_ptr[i] + (NT_len[i]++);
+ if (i < m)
+ xassert(pos < NT_ptr[i+1]);
+ else
+ xassert(pos <= nnz);
+ NT_ind[pos] = j;
+ NT_val[pos] = A_val[ptr];
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_build_nt - build matrix N for current basis
+*
+* This routine builds the row-wise represntation of the matrix N
+* for the current basis by adding columns of the constraint matrix A
+* corresponding to non-basic variables. */
+
+void spx_build_nt(SPXLP *lp, SPXNT *nt)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ int *NT_len = nt->len;
+ int j, k;
+ /* N := 0 */
+ memset(&NT_len[1], 0, m * sizeof(int));
+ /* add non-basic columns N[j] = A[k] */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ spx_nt_add_col(lp, nt, j, k);
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_nt_del_col - remove column N[j] = A[k] from matrix N
+*
+* This routine removes elements of column N[j] = A[k], 1 <= j <= n-m,
+* 1 <= k <= n, from the row-wise representation of the matrix N. It is
+* assumed (with no check) that elements of the specified column are
+* present in the row-wise representation of N. */
+
+void spx_nt_del_col(SPXLP *lp, SPXNT *nt, int j, int k)
+{ int m = lp->m;
+ int n = lp->n;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ int *NT_ptr = nt->ptr;
+ int *NT_len = nt->len;
+ int *NT_ind = nt->ind;
+ double *NT_val = nt->val;
+ int i, ptr, end, ptr1, end1;
+ xassert(1 <= j && j <= n-m);
+ xassert(1 <= k && k <= n);
+ ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ { i = A_ind[ptr];
+ /* find element N[i,j] = A[i,k] in i-th row of matrix N */
+ ptr1 = NT_ptr[i];
+ end1 = ptr1 + NT_len[i];
+ for (; NT_ind[ptr1] != j; ptr1++)
+ /* nop */;
+ xassert(ptr1 < end1);
+ /* and remove it from i-th row element list */
+ NT_len[i]--;
+ NT_ind[ptr1] = NT_ind[end1-1];
+ NT_val[ptr1] = NT_val[end1-1];
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_update_nt - update matrix N for adjacent basis
+*
+* This routine updates the row-wise represntation of matrix N for
+* the adjacent basis, where column N[q], 1 <= q <= n-m, is replaced by
+* column B[p], 1 <= p <= m, of the current basis matrix B. */
+
+void spx_update_nt(SPXLP *lp, SPXNT *nt, int p, int q)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* remove old column N[q] corresponding to variable xN[q] */
+ spx_nt_del_col(lp, nt, q, head[m+q]);
+ /* add new column N[q] corresponding to variable xB[p] */
+ spx_nt_add_col(lp, nt, q, head[p]);
+ return;
+}
+
+/***********************************************************************
+* spx_nt_prod - compute product y := y + s * N'* x
+*
+* This routine computes the product:
+*
+* y := y + s * N'* x,
+*
+* where N' is a matrix transposed to the mx(n-m)-matrix N composed
+* from non-basic columns of the constraint matrix A, x is a m-vector,
+* s is a scalar, y is (n-m)-vector.
+*
+* If the flag ign is non-zero, the routine ignores the input content
+* of the array y assuming that y = 0.
+*
+* The routine uses the row-wise representation of the matrix N and
+* computes the product as a linear combination:
+*
+* y := y + s * (N'[1] * x[1] + ... + N'[m] * x[m]),
+*
+* where N'[i] is i-th row of N, 1 <= i <= m. */
+
+void spx_nt_prod(SPXLP *lp, SPXNT *nt, double y[/*1+n-m*/], int ign,
+ double s, const double x[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *NT_ptr = nt->ptr;
+ int *NT_len = nt->len;
+ int *NT_ind = nt->ind;
+ double *NT_val = nt->val;
+ int i, j, ptr, end;
+ double t;
+ if (ign)
+ { /* y := 0 */
+ for (j = 1; j <= n-m; j++)
+ y[j] = 0.0;
+ }
+ for (i = 1; i <= m; i++)
+ { if (x[i] != 0.0)
+ { /* y := y + s * (i-th row of N) * x[i] */
+ t = s * x[i];
+ ptr = NT_ptr[i];
+ end = ptr + NT_len[i];
+ for (; ptr < end; ptr++)
+ y[NT_ind[ptr]] += NT_val[ptr] * t;
+ }
+ }
+ return;
+}
+
+#if 1 /* 31/III-2016 */
+void spx_nt_prod_s(SPXLP *lp, SPXNT *nt, FVS *y, int ign, double s,
+ const FVS *x, double eps)
+{ /* sparse version of spx_nt_prod */
+ int *NT_ptr = nt->ptr;
+ int *NT_len = nt->len;
+ int *NT_ind = nt->ind;
+ double *NT_val = nt->val;
+ int *x_ind = x->ind;
+ double *x_vec = x->vec;
+ int *y_ind = y->ind;
+ double *y_vec = y->vec;
+ int i, j, k, nnz, ptr, end;
+ double t;
+ xassert(x->n == lp->m);
+ xassert(y->n == lp->n-lp->m);
+ if (ign)
+ { /* y := 0 */
+ fvs_clear_vec(y);
+ }
+ nnz = y->nnz;
+ for (k = x->nnz; k >= 1; k--)
+ { i = x_ind[k];
+ /* y := y + s * (i-th row of N) * x[i] */
+ t = s * x_vec[i];
+ ptr = NT_ptr[i];
+ end = ptr + NT_len[i];
+ for (; ptr < end; ptr++)
+ { j = NT_ind[ptr];
+ if (y_vec[j] == 0.0)
+ y_ind[++nnz] = j;
+ y_vec[j] += NT_val[ptr] * t;
+ /* don't forget about numeric cancellation */
+ if (y_vec[j] == 0.0)
+ y_vec[j] = DBL_MIN;
+ }
+ }
+ y->nnz = nnz;
+ fvs_adjust_vec(y, eps);
+ return;
+}
+#endif
+
+/***********************************************************************
+* spx_free_nt - deallocate matrix N in sparse row-wise format
+*
+* This routine deallocates the memory used for arrays of the program
+* object nt. */
+
+void spx_free_nt(SPXLP *lp, SPXNT *nt)
+{ xassert(lp == lp);
+ tfree(nt->ptr);
+ tfree(nt->len);
+ tfree(nt->ind);
+ tfree(nt->val);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxnt.h b/test/monniaux/glpk-4.65/src/simplex/spxnt.h
new file mode 100644
index 00000000..857917b8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxnt.h
@@ -0,0 +1,96 @@
+/* spxnt.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXNT_H
+#define SPXNT_H
+
+#include "spxlp.h"
+
+typedef struct SPXNT SPXNT;
+
+struct SPXNT
+{ /* mx(n-m)-matrix N composed of non-basic columns of constraint
+ * matrix A, in sparse row-wise format */
+ int *ptr; /* int ptr[1+m]; */
+ /* ptr[0] is not used;
+ * ptr[i], 1 <= i <= m, is starting position of i-th row in
+ * arrays ind and val; note that ptr[1] is always 1;
+ * these starting positions are set up *once* as if they would
+ * correspond to rows of matrix A stored without gaps, i.e.
+ * ptr[i+1] - ptr[i] is the number of non-zeros in i-th (i < m)
+ * row of matrix A, and (nnz+1) - ptr[m] is the number of
+ * non-zero in m-th (last) row of matrix A, where nnz is the
+ * total number of non-zeros in matrix A */
+ int *len; /* int len[1+m]; */
+ /* len[0] is not used;
+ * len[i], 1 <= i <= m, is the number of non-zeros in i-th row
+ * of current matrix N */
+ int *ind; /* int ind[1+nnz]; */
+ /* column indices */
+ double *val; /* double val[1+nnz]; */
+ /* non-zero element values */
+};
+
+#define spx_alloc_nt _glp_spx_alloc_nt
+void spx_alloc_nt(SPXLP *lp, SPXNT *nt);
+/* allocate matrix N in sparse row-wise format */
+
+#define spx_init_nt _glp_spx_init_nt
+void spx_init_nt(SPXLP *lp, SPXNT *nt);
+/* initialize row pointers for matrix N */
+
+#define spx_nt_add_col _glp_spx_nt_add_col
+void spx_nt_add_col(SPXLP *lp, SPXNT *nt, int j, int k);
+/* add column N[j] = A[k] */
+
+#define spx_build_nt _glp_spx_build_nt
+void spx_build_nt(SPXLP *lp, SPXNT *nt);
+/* build matrix N for current basis */
+
+#define spx_nt_del_col _glp_spx_nt_del_col
+void spx_nt_del_col(SPXLP *lp, SPXNT *nt, int j, int k);
+/* remove column N[j] = A[k] from matrix N */
+
+#define spx_update_nt _glp_spx_update_nt
+void spx_update_nt(SPXLP *lp, SPXNT *nt, int p, int q);
+/* update matrix N for adjacent basis */
+
+#define spx_nt_prod _glp_spx_nt_prod
+void spx_nt_prod(SPXLP *lp, SPXNT *nt, double y[/*1+n-m*/], int ign,
+ double s, const double x[/*1+m*/]);
+/* compute product y := y + s * N'* x */
+
+#if 1 /* 31/III-2016 */
+#define spx_nt_prod_s _glp_spx_nt_prod_s
+void spx_nt_prod_s(SPXLP *lp, SPXNT *nt, FVS *y, int ign, double s,
+ const FVS *x, double eps);
+/* sparse version of spx_nt_prod */
+#endif
+
+#define spx_free_nt _glp_spx_free_nt
+void spx_free_nt(SPXLP *lp, SPXNT *nt);
+/* deallocate matrix N in sparse row-wise format */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxprim.c b/test/monniaux/glpk-4.65/src/simplex/spxprim.c
new file mode 100644
index 00000000..e1cdfb5a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxprim.c
@@ -0,0 +1,1860 @@
+/* spxprim.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#if 1 /* 18/VII-2017 */
+#define SCALE_Z 1
+#endif
+
+#include "env.h"
+#include "simplex.h"
+#include "spxat.h"
+#include "spxnt.h"
+#include "spxchuzc.h"
+#include "spxchuzr.h"
+#include "spxprob.h"
+
+#define CHECK_ACCURACY 0
+/* (for debugging) */
+
+struct csa
+{ /* common storage area */
+ SPXLP *lp;
+ /* LP problem data and its (current) basis; this LP has m rows
+ * and n columns */
+ int dir;
+ /* original optimization direction:
+ * +1 - minimization
+ * -1 - maximization */
+#if SCALE_Z
+ double fz;
+ /* factor used to scale original objective */
+#endif
+ double *orig_c; /* double orig_c[1+n]; */
+ /* copy of original objective coefficients */
+ double *orig_l; /* double orig_l[1+n]; */
+ /* copy of original lower bounds */
+ double *orig_u; /* double orig_u[1+n]; */
+ /* copy of original upper bounds */
+ SPXAT *at;
+ /* mxn-matrix A of constraint coefficients, in sparse row-wise
+ * format (NULL if not used) */
+ SPXNT *nt;
+ /* mx(n-m)-matrix N composed of non-basic columns of constraint
+ * matrix A, in sparse row-wise format (NULL if not used) */
+ int phase;
+ /* search phase:
+ * 0 - not determined yet
+ * 1 - searching for primal feasible solution
+ * 2 - searching for optimal solution */
+ double *beta; /* double beta[1+m]; */
+ /* beta[i] is a primal value of basic variable xB[i] */
+ int beta_st;
+ /* status of the vector beta:
+ * 0 - undefined
+ * 1 - just computed
+ * 2 - updated */
+ double *d; /* double d[1+n-m]; */
+ /* d[j] is a reduced cost of non-basic variable xN[j] */
+ int d_st;
+ /* status of the vector d:
+ * 0 - undefined
+ * 1 - just computed
+ * 2 - updated */
+ SPXSE *se;
+ /* projected steepest edge and Devex pricing data block (NULL if
+ * not used) */
+ int num;
+ /* number of eligible non-basic variables */
+ int *list; /* int list[1+n-m]; */
+ /* list[1], ..., list[num] are indices j of eligible non-basic
+ * variables xN[j] */
+ int q;
+ /* xN[q] is a non-basic variable chosen to enter the basis */
+#if 0 /* 11/VI-2017 */
+ double *tcol; /* double tcol[1+m]; */
+#else
+ FVS tcol; /* FVS tcol[1:m]; */
+#endif
+ /* q-th (pivot) column of the simplex table */
+#if 1 /* 23/VI-2017 */
+ SPXBP *bp; /* SPXBP bp[1+2*m+1]; */
+ /* penalty function break points */
+#endif
+ int p;
+ /* xB[p] is a basic variable chosen to leave the basis;
+ * p = 0 means that no basic variable reaches its bound;
+ * p < 0 means that non-basic variable xN[q] reaches its opposite
+ * bound before any basic variable */
+ int p_flag;
+ /* if this flag is set, the active bound of xB[p] in the adjacent
+ * basis should be set to the upper bound */
+#if 0 /* 11/VI-2017 */
+ double *trow; /* double trow[1+n-m]; */
+#else
+ FVS trow; /* FVS trow[1:n-m]; */
+#endif
+ /* p-th (pivot) row of the simplex table */
+#if 0 /* 09/VII-2017 */
+ double *work; /* double work[1+m]; */
+ /* working array */
+#else
+ FVS work; /* FVS work[1:m]; */
+ /* working vector */
+#endif
+ int p_stat, d_stat;
+ /* primal and dual solution statuses */
+ /*--------------------------------------------------------------*/
+ /* control parameters (see struct glp_smcp) */
+ int msg_lev;
+ /* message level */
+#if 0 /* 23/VI-2017 */
+ int harris;
+ /* ratio test technique:
+ * 0 - textbook ratio test
+ * 1 - Harris' two pass ratio test */
+#else
+ int r_test;
+ /* ratio test technique:
+ * GLP_RT_STD - textbook ratio test
+ * GLP_RT_HAR - Harris' two pass ratio test
+ * GLP_RT_FLIP - long-step ratio test (only for phase I) */
+#endif
+ double tol_bnd, tol_bnd1;
+ /* primal feasibility tolerances */
+ double tol_dj, tol_dj1;
+ /* dual feasibility tolerances */
+ double tol_piv;
+ /* pivot tolerance */
+ int it_lim;
+ /* iteration limit */
+ int tm_lim;
+ /* time limit, milliseconds */
+ int out_frq;
+#if 0 /* 15/VII-2017 */
+ /* display output frequency, iterations */
+#else
+ /* display output frequency, milliseconds */
+#endif
+ int out_dly;
+ /* display output delay, milliseconds */
+ /*--------------------------------------------------------------*/
+ /* working parameters */
+ double tm_beg;
+ /* time value at the beginning of the search */
+ int it_beg;
+ /* simplex iteration count at the beginning of the search */
+ int it_cnt;
+ /* simplex iteration count; it increases by one every time the
+ * basis changes (including the case when a non-basic variable
+ * jumps to its opposite bound) */
+ int it_dpy;
+ /* simplex iteration count at most recent display output */
+#if 1 /* 15/VII-2017 */
+ double tm_dpy;
+ /* time value at most recent display output */
+#endif
+ int inv_cnt;
+ /* basis factorization count since most recent display output */
+#if 1 /* 01/VII-2017 */
+ int degen;
+ /* count of successive degenerate iterations; this count is used
+ * to detect stalling */
+#endif
+#if 1 /* 23/VI-2017 */
+ int ns_cnt, ls_cnt;
+ /* normal and long-step iteration counts */
+#endif
+};
+
+/***********************************************************************
+* set_penalty - set penalty function coefficients
+*
+* This routine sets up objective coefficients of the penalty function,
+* which is the sum of primal infeasibilities, as follows:
+*
+* if beta[i] < l[k] - eps1, set c[k] = -1,
+*
+* if beta[i] > u[k] + eps2, set c[k] = +1,
+*
+* otherwise, set c[k] = 0,
+*
+* where beta[i] is current value of basic variable xB[i] = x[k], l[k]
+* and u[k] are original bounds of x[k], and
+*
+* eps1 = tol + tol1 * |l[k]|,
+*
+* eps2 = tol + tol1 * |u[k]|.
+*
+* The routine returns the number of non-zero objective coefficients,
+* which is the number of basic variables violating their bounds. Thus,
+* if the value returned is zero, the current basis is primal feasible
+* within the specified tolerances. */
+
+static int set_penalty(struct csa *csa, double tol, double tol1)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *beta = csa->beta;
+ int i, k, count = 0;
+ double t, eps;
+ /* reset objective coefficients */
+ for (k = 0; k <= n; k++)
+ c[k] = 0.0;
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ /* check lower bound */
+ if ((t = l[k]) != -DBL_MAX)
+ { eps = tol + tol1 * (t >= 0.0 ? +t : -t);
+ if (beta[i] < t - eps)
+ { /* lower bound is violated */
+ c[k] = -1.0, count++;
+ }
+ }
+ /* check upper bound */
+ if ((t = u[k]) != +DBL_MAX)
+ { eps = tol + tol1 * (t >= 0.0 ? +t : -t);
+ if (beta[i] > t + eps)
+ { /* upper bound is violated */
+ c[k] = +1.0, count++;
+ }
+ }
+ }
+ return count;
+}
+
+/***********************************************************************
+* check_feas - check primal feasibility of basic solution
+*
+* This routine checks if the specified values of all basic variables
+* beta = (beta[i]) are within their bounds.
+*
+* Let l[k] and u[k] be original bounds of basic variable xB[i] = x[k].
+* The actual bounds of x[k] are determined as follows:
+*
+* 1) if phase = 1 and c[k] < 0, x[k] violates its lower bound, so its
+* actual bounds are artificial: -inf < x[k] <= l[k];
+*
+* 2) if phase = 1 and c[k] > 0, x[k] violates its upper bound, so its
+* actual bounds are artificial: u[k] <= x[k] < +inf;
+*
+* 3) in all other cases (if phase = 1 and c[k] = 0, or if phase = 2)
+* actual bounds are original: l[k] <= x[k] <= u[k].
+*
+* The parameters tol and tol1 are bound violation tolerances. The
+* actual bounds l'[k] and u'[k] are considered as non-violated within
+* the specified tolerance if
+*
+* l'[k] - eps1 <= beta[i] <= u'[k] + eps2,
+*
+* where eps1 = tol + tol1 * |l'[k]|, eps2 = tol + tol1 * |u'[k]|.
+*
+* The routine returns one of the following codes:
+*
+* 0 - solution is feasible (no actual bounds are violated);
+*
+* 1 - solution is infeasible, however, only artificial bounds are
+* violated (this is possible only if phase = 1);
+*
+* 2 - solution is infeasible and at least one original bound is
+* violated. */
+
+static int check_feas(struct csa *csa, int phase, double tol, double
+ tol1)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *beta = csa->beta;
+ int i, k, orig, ret = 0;
+ double lk, uk, eps;
+ xassert(phase == 1 || phase == 2);
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ /* determine actual bounds of x[k] */
+ if (phase == 1 && c[k] < 0.0)
+ { /* -inf < x[k] <= l[k] */
+ lk = -DBL_MAX, uk = l[k];
+ orig = 0; /* artificial bounds */
+ }
+ else if (phase == 1 && c[k] > 0.0)
+ { /* u[k] <= x[k] < +inf */
+ lk = u[k], uk = +DBL_MAX;
+ orig = 0; /* artificial bounds */
+ }
+ else
+ { /* l[k] <= x[k] <= u[k] */
+ lk = l[k], uk = u[k];
+ orig = 1; /* original bounds */
+ }
+ /* check actual lower bound */
+ if (lk != -DBL_MAX)
+ { eps = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] < lk - eps)
+ { /* actual lower bound is violated */
+ if (orig)
+ { ret = 2;
+ break;
+ }
+ ret = 1;
+ }
+ }
+ /* check actual upper bound */
+ if (uk != +DBL_MAX)
+ { eps = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] > uk + eps)
+ { /* actual upper bound is violated */
+ if (orig)
+ { ret = 2;
+ break;
+ }
+ ret = 1;
+ }
+ }
+ }
+ return ret;
+}
+
+/***********************************************************************
+* adjust_penalty - adjust penalty function coefficients
+*
+* On searching for primal feasible solution it may happen that some
+* basic variable xB[i] = x[k] has non-zero objective coefficient c[k]
+* indicating that xB[i] violates its lower (if c[k] < 0) or upper (if
+* c[k] > 0) original bound, but due to primal degenarcy the violation
+* is close to zero.
+*
+* This routine identifies such basic variables and sets objective
+* coefficients at these variables to zero that allows avoiding zero-
+* step simplex iterations.
+*
+* The parameters tol and tol1 are bound violation tolerances. The
+* original bounds l[k] and u[k] are considered as non-violated within
+* the specified tolerance if
+*
+* l[k] - eps1 <= beta[i] <= u[k] + eps2,
+*
+* where beta[i] is value of basic variable xB[i] = x[k] in the current
+* basis, eps1 = tol + tol1 * |l[k]|, eps2 = tol + tol1 * |u[k]|.
+*
+* The routine returns the number of objective coefficients which were
+* set to zero. */
+
+#if 0
+static int adjust_penalty(struct csa *csa, double tol, double tol1)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *beta = csa->beta;
+ int i, k, count = 0;
+ double t, eps;
+ xassert(csa->phase == 1);
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ if (c[k] < 0.0)
+ { /* x[k] violates its original lower bound l[k] */
+ xassert((t = l[k]) != -DBL_MAX);
+ eps = tol + tol1 * (t >= 0.0 ? +t : -t);
+ if (beta[i] >= t - eps)
+ { /* however, violation is close to zero */
+ c[k] = 0.0, count++;
+ }
+ }
+ else if (c[k] > 0.0)
+ { /* x[k] violates its original upper bound u[k] */
+ xassert((t = u[k]) != +DBL_MAX);
+ eps = tol + tol1 * (t >= 0.0 ? +t : -t);
+ if (beta[i] <= t + eps)
+ { /* however, violation is close to zero */
+ c[k] = 0.0, count++;
+ }
+ }
+ }
+ return count;
+}
+#else
+static int adjust_penalty(struct csa *csa, int num, const int
+ ind[/*1+num*/], double tol, double tol1)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *beta = csa->beta;
+ int i, k, t, cnt = 0;
+ double lk, uk, eps;
+ xassert(csa->phase == 1);
+ /* walk thru the specified list of basic variables */
+ for (t = 1; t <= num; t++)
+ { i = ind[t];
+ xassert(1 <= i && i <= m);
+ k = head[i]; /* x[k] = xB[i] */
+ if (c[k] < 0.0)
+ { /* x[k] violates its original lower bound */
+ lk = l[k];
+ xassert(lk != -DBL_MAX);
+ eps = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] >= lk - eps)
+ { /* however, violation is close to zero */
+ c[k] = 0.0, cnt++;
+ }
+ }
+ else if (c[k] > 0.0)
+ { /* x[k] violates its original upper bound */
+ uk = u[k];
+ xassert(uk != +DBL_MAX);
+ eps = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] <= uk + eps)
+ { /* however, violation is close to zero */
+ c[k] = 0.0, cnt++;
+ }
+ }
+ }
+ return cnt;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_vec - compute maximal relative error between two vectors
+*
+* This routine computes and returns maximal relative error between
+* n-vectors x and y:
+*
+* err_max = max |x[i] - y[i]| / (1 + |x[i]|).
+*
+* NOTE: This routine is intended only for debugginig purposes. */
+
+static double err_in_vec(int n, const double x[], const double y[])
+{ int i;
+ double err, err_max;
+ err_max = 0.0;
+ for (i = 1; i <= n; i++)
+ { err = fabs(x[i] - y[i]) / (1.0 + fabs(x[i]));
+ if (err_max < err)
+ err_max = err;
+ }
+ return err_max;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_beta - compute maximal relative error in vector beta
+*
+* This routine computes and returns maximal relative error in vector
+* of values of basic variables beta = (beta[i]).
+*
+* NOTE: This routine is intended only for debugginig purposes. */
+
+static double err_in_beta(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double err, *beta;
+ beta = talloc(1+m, double);
+ spx_eval_beta(lp, beta);
+ err = err_in_vec(m, beta, csa->beta);
+ tfree(beta);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_d - compute maximal relative error in vector d
+*
+* This routine computes and returns maximal relative error in vector
+* of reduced costs of non-basic variables d = (d[j]).
+*
+* NOTE: This routine is intended only for debugginig purposes. */
+
+static double err_in_d(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ int j;
+ double err, *pi, *d;
+ pi = talloc(1+m, double);
+ d = talloc(1+n-m, double);
+ spx_eval_pi(lp, pi);
+ for (j = 1; j <= n-m; j++)
+ d[j] = spx_eval_dj(lp, pi, j);
+ err = err_in_vec(n-m, d, csa->d);
+ tfree(pi);
+ tfree(d);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_gamma - compute maximal relative error in vector gamma
+*
+* This routine computes and returns maximal relative error in vector
+* of projected steepest edge weights gamma = (gamma[j]).
+*
+* NOTE: This routine is intended only for debugginig purposes. */
+
+static double err_in_gamma(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ SPXSE *se = csa->se;
+ int j;
+ double err, *gamma;
+ xassert(se != NULL);
+ gamma = talloc(1+n-m, double);
+ for (j = 1; j <= n-m; j++)
+ gamma[j] = spx_eval_gamma_j(lp, se, j);
+ err = err_in_vec(n-m, gamma, se->gamma);
+ tfree(gamma);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* check_accuracy - check accuracy of basic solution components
+*
+* This routine checks accuracy of current basic solution components.
+*
+* NOTE: This routine is intended only for debugginig purposes. */
+
+static void check_accuracy(struct csa *csa)
+{ double e_beta, e_d, e_gamma;
+ e_beta = err_in_beta(csa);
+ e_d = err_in_d(csa);
+ if (csa->se == NULL)
+ e_gamma = 0.;
+ else
+ e_gamma = err_in_gamma(csa);
+ xprintf("e_beta = %10.3e; e_d = %10.3e; e_gamma = %10.3e\n",
+ e_beta, e_d, e_gamma);
+ xassert(e_beta <= 1e-5 && e_d <= 1e-5 && e_gamma <= 1e-3);
+ return;
+}
+#endif
+
+/***********************************************************************
+* choose_pivot - choose xN[q] and xB[p]
+*
+* Given the list of eligible non-basic variables this routine first
+* chooses non-basic variable xN[q]. This choice is always possible,
+* because the list is assumed to be non-empty. Then the routine
+* computes q-th column T[*,q] of the simplex table T[i,j] and chooses
+* basic variable xB[p]. If the pivot T[p,q] is small in magnitude,
+* the routine attempts to choose another xN[q] and xB[p] in order to
+* avoid badly conditioned adjacent bases. */
+
+#if 1 /* 17/III-2016 */
+#define MIN_RATIO 0.0001
+
+static int choose_pivot(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *beta = csa->beta;
+ double *d = csa->d;
+ SPXSE *se = csa->se;
+ int *list = csa->list;
+#if 0 /* 09/VII-2017 */
+ double *tcol = csa->work;
+#else
+ double *tcol = csa->work.vec;
+#endif
+ double tol_piv = csa->tol_piv;
+ int try, nnn, /*i,*/ p, p_flag, q, t;
+ double big, /*temp,*/ best_ratio;
+#if 1 /* 23/VI-2017 */
+ double *c = lp->c;
+ int *head = lp->head;
+ SPXBP *bp = csa->bp;
+ int nbp, t_best, ret, k;
+ double dz_best;
+#endif
+ xassert(csa->beta_st);
+ xassert(csa->d_st);
+more: /* initial number of eligible non-basic variables */
+ nnn = csa->num;
+ /* nothing has been chosen so far */
+ csa->q = 0;
+ best_ratio = 0.0;
+#if 0 /* 23/VI-2017 */
+ try = 0;
+#else
+ try = ret = 0;
+#endif
+try: /* choose non-basic variable xN[q] */
+ xassert(nnn > 0);
+ try++;
+ if (se == NULL)
+ { /* Dantzig's rule */
+ q = spx_chuzc_std(lp, d, nnn, list);
+ }
+ else
+ { /* projected steepest edge */
+ q = spx_chuzc_pse(lp, se, d, nnn, list);
+ }
+ xassert(1 <= q && q <= n-m);
+ /* compute q-th column of the simplex table */
+ spx_eval_tcol(lp, q, tcol);
+#if 0
+ /* big := max(1, |tcol[1]|, ..., |tcol[m]|) */
+ big = 1.0;
+ for (i = 1; i <= m; i++)
+ { temp = tcol[i];
+ if (temp < 0.0)
+ temp = - temp;
+ if (big < temp)
+ big = temp;
+ }
+#else
+ /* this still puzzles me */
+ big = 1.0;
+#endif
+ /* choose basic variable xB[p] */
+#if 1 /* 23/VI-2017 */
+ if (csa->phase == 1 && csa->r_test == GLP_RT_FLIP && try <= 2)
+ { /* long-step ratio test */
+ int t, num, num1;
+ double slope, teta_lim;
+ /* determine penalty function break points */
+ nbp = spx_ls_eval_bp(lp, beta, q, d[q], tcol, tol_piv, bp);
+ if (nbp < 2)
+ goto skip;
+ /* set initial slope */
+ slope = - fabs(d[q]);
+ /* estimate initial teta_lim */
+ teta_lim = DBL_MAX;
+ for (t = 1; t <= nbp; t++)
+ { if (teta_lim > bp[t].teta)
+ teta_lim = bp[t].teta;
+ }
+ xassert(teta_lim >= 0.0);
+ if (teta_lim < 1e-3)
+ teta_lim = 1e-3;
+ /* nothing has been chosen so far */
+ t_best = 0, dz_best = 0.0, num = 0;
+ /* choose appropriate break point */
+ while (num < nbp)
+ { /* select and process a new portion of break points */
+ num1 = spx_ls_select_bp(lp, tcol, nbp, bp, num, &slope,
+ teta_lim);
+ for (t = num+1; t <= num1; t++)
+ { int i = (bp[t].i >= 0 ? bp[t].i : -bp[t].i);
+ xassert(0 <= i && i <= m);
+ if (i == 0 || fabs(tcol[i]) / big >= MIN_RATIO)
+ { if (dz_best > bp[t].dz)
+ t_best = t, dz_best = bp[t].dz;
+ }
+#if 0
+ if (i == 0)
+ { /* do not consider further break points beyond this
+ * point, where xN[q] reaches its opposite bound;
+ * in principle (see spx_ls_eval_bp), this break
+ * point should be the last one, however, due to
+ * round-off errors there may be other break points
+ * with the same teta beyond this one */
+ slope = +1.0;
+ }
+#endif
+ }
+ if (slope > 0.0)
+ { /* penalty function starts increasing */
+ break;
+ }
+ /* penalty function continues decreasing */
+ num = num1;
+ teta_lim += teta_lim;
+ }
+ if (dz_best == 0.0)
+ goto skip;
+ /* the choice has been made */
+ xassert(1 <= t_best && t_best <= num1);
+ if (t_best == 1)
+ { /* the very first break point was chosen; it is reasonable
+ * to use the short-step ratio test */
+ goto skip;
+ }
+ csa->q = q;
+ memcpy(&csa->tcol.vec[1], &tcol[1], m * sizeof(double));
+ fvs_gather_vec(&csa->tcol, DBL_EPSILON);
+ if (bp[t_best].i == 0)
+ { /* xN[q] goes to its opposite bound */
+ csa->p = -1;
+ csa->p_flag = 0;
+ best_ratio = 1.0;
+ }
+ else if (bp[t_best].i > 0)
+ { /* xB[p] leaves the basis and goes to its lower bound */
+ csa->p = + bp[t_best].i;
+ xassert(1 <= csa->p && csa->p <= m);
+ csa->p_flag = 0;
+ best_ratio = fabs(tcol[csa->p]) / big;
+ }
+ else
+ { /* xB[p] leaves the basis and goes to its upper bound */
+ csa->p = - bp[t_best].i;
+ xassert(1 <= csa->p && csa->p <= m);
+ csa->p_flag = 1;
+ best_ratio = fabs(tcol[csa->p]) / big;
+ }
+#if 0
+ xprintf("num1 = %d; t_best = %d; dz = %g\n", num1, t_best,
+ bp[t_best].dz);
+#endif
+ ret = 1;
+ goto done;
+skip: ;
+ }
+#endif
+#if 0 /* 23/VI-2017 */
+ if (!csa->harris)
+#else
+ if (csa->r_test == GLP_RT_STD)
+#endif
+ { /* textbook ratio test */
+ p = spx_chuzr_std(lp, csa->phase, beta, q,
+ d[q] < 0.0 ? +1. : -1., tcol, &p_flag, tol_piv,
+ .30 * csa->tol_bnd, .30 * csa->tol_bnd1);
+ }
+ else
+ { /* Harris' two-pass ratio test */
+ p = spx_chuzr_harris(lp, csa->phase, beta, q,
+ d[q] < 0.0 ? +1. : -1., tcol, &p_flag , tol_piv,
+ .50 * csa->tol_bnd, .50 * csa->tol_bnd1);
+ }
+ if (p <= 0)
+ { /* primal unboundedness or special case */
+ csa->q = q;
+#if 0 /* 11/VI-2017 */
+ memcpy(&csa->tcol[1], &tcol[1], m * sizeof(double));
+#else
+ memcpy(&csa->tcol.vec[1], &tcol[1], m * sizeof(double));
+ fvs_gather_vec(&csa->tcol, DBL_EPSILON);
+#endif
+ csa->p = p;
+ csa->p_flag = p_flag;
+ best_ratio = 1.0;
+ goto done;
+ }
+ /* either keep previous choice or accept new choice depending on
+ * which one is better */
+ if (best_ratio < fabs(tcol[p]) / big)
+ { csa->q = q;
+#if 0 /* 11/VI-2017 */
+ memcpy(&csa->tcol[1], &tcol[1], m * sizeof(double));
+#else
+ memcpy(&csa->tcol.vec[1], &tcol[1], m * sizeof(double));
+ fvs_gather_vec(&csa->tcol, DBL_EPSILON);
+#endif
+ csa->p = p;
+ csa->p_flag = p_flag;
+ best_ratio = fabs(tcol[p]) / big;
+ }
+ /* check if the current choice is acceptable */
+ if (best_ratio >= MIN_RATIO || nnn == 1 || try == 5)
+ goto done;
+ /* try to choose other xN[q] and xB[p] */
+ /* find xN[q] in the list */
+ for (t = 1; t <= nnn; t++)
+ if (list[t] == q) break;
+ xassert(t <= nnn);
+ /* move xN[q] to the end of the list */
+ list[t] = list[nnn], list[nnn] = q;
+ /* and exclude it from consideration */
+ nnn--;
+ /* repeat the choice */
+ goto try;
+done: /* the choice has been made */
+#if 1 /* FIXME: currently just to avoid badly conditioned basis */
+ if (best_ratio < .001 * MIN_RATIO)
+ { /* looks like this helps */
+ if (bfd_get_count(lp->bfd) > 0)
+ return -1;
+ /* didn't help; last chance to improve the choice */
+ if (tol_piv == csa->tol_piv)
+ { tol_piv *= 1000.;
+ goto more;
+ }
+ }
+#endif
+#if 0 /* 23/VI-2017 */
+ return 0;
+#else /* FIXME */
+ if (ret)
+ { /* invalidate dual basic solution components */
+ csa->d_st = 0;
+ /* change penalty function coefficients at basic variables for
+ * all break points preceding the chosen one */
+ for (t = 1; t < t_best; t++)
+ { int i = (bp[t].i >= 0 ? bp[t].i : -bp[t].i);
+ xassert(0 <= i && i <= m);
+ if (i == 0)
+ { /* xN[q] crosses its opposite bound */
+ xassert(1 <= csa->q && csa->q <= n-m);
+ k = head[m+csa->q];
+ }
+ else
+ { /* xB[i] crosses its (lower or upper) bound */
+ k = head[i]; /* x[k] = xB[i] */
+ }
+ c[k] += bp[t].dc;
+ xassert(c[k] == 0.0 || c[k] == +1.0 || c[k] == -1.0);
+ }
+ }
+ return ret;
+#endif
+}
+#endif
+
+/***********************************************************************
+* play_bounds - play bounds of primal variables
+*
+* This routine is called after the primal values of basic variables
+* beta[i] were updated and the basis was changed to the adjacent one.
+*
+* It is assumed that before updating all the primal values beta[i]
+* were strongly feasible, so in the adjacent basis beta[i] remain
+* feasible within a tolerance, i.e. if some beta[i] violates its lower
+* or upper bound, the violation is insignificant.
+*
+* If some beta[i] violates its lower or upper bound, this routine
+* changes (perturbs) the bound to remove such violation, i.e. to make
+* all beta[i] strongly feasible. Otherwise, if beta[i] has a feasible
+* value, this routine attempts to reduce (or remove) perturbation of
+* corresponding lower/upper bound keeping strong feasibility. */
+
+/* FIXME: what to do if l[k] = u[k]? */
+
+/* FIXME: reduce/remove perturbation if x[k] becomes non-basic? */
+
+static void play_bounds(struct csa *csa, int all)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *orig_l = csa->orig_l;
+ double *orig_u = csa->orig_u;
+ double *beta = csa->beta;
+#if 0 /* 11/VI-2017 */
+ const double *tcol = csa->tcol; /* was used to update beta */
+#else
+ const double *tcol = csa->tcol.vec;
+#endif
+ int i, k;
+ xassert(csa->phase == 1 || csa->phase == 2);
+ /* primal values beta = (beta[i]) should be valid */
+ xassert(csa->beta_st);
+ /* walk thru the list of basic variables xB = (xB[i]) */
+ for (i = 1; i <= m; i++)
+ { if (all || tcol[i] != 0.0)
+ { /* beta[i] has changed in the adjacent basis */
+ k = head[i]; /* x[k] = xB[i] */
+ if (csa->phase == 1 && c[k] < 0.0)
+ { /* -inf < xB[i] <= lB[i] (artificial bounds) */
+ if (beta[i] < l[k] - 1e-9)
+ continue;
+ /* restore actual bounds */
+ c[k] = 0.0;
+ csa->d_st = 0; /* since c[k] = cB[i] has changed */
+ }
+ if (csa->phase == 1 && c[k] > 0.0)
+ { /* uB[i] <= xB[i] < +inf (artificial bounds) */
+ if (beta[i] > u[k] + 1e-9)
+ continue;
+ /* restore actual bounds */
+ c[k] = 0.0;
+ csa->d_st = 0; /* since c[k] = cB[i] has changed */
+ }
+ /* lB[i] <= xB[i] <= uB[i] */
+ if (csa->phase == 1)
+ xassert(c[k] == 0.0);
+ if (l[k] != -DBL_MAX)
+ { /* xB[i] has lower bound */
+ if (beta[i] < l[k])
+ { /* strong feasibility means xB[i] >= lB[i] */
+#if 0 /* 11/VI-2017 */
+ l[k] = beta[i];
+#else
+ l[k] = beta[i] - 1e-9;
+#endif
+ }
+ else if (l[k] < orig_l[k])
+ { /* remove/reduce perturbation of lB[i] */
+ if (beta[i] >= orig_l[k])
+ l[k] = orig_l[k];
+ else
+ l[k] = beta[i];
+ }
+ }
+ if (u[k] != +DBL_MAX)
+ { /* xB[i] has upper bound */
+ if (beta[i] > u[k])
+ { /* strong feasibility means xB[i] <= uB[i] */
+#if 0 /* 11/VI-2017 */
+ u[k] = beta[i];
+#else
+ u[k] = beta[i] + 1e-9;
+#endif
+ }
+ else if (u[k] > orig_u[k])
+ { /* remove/reduce perturbation of uB[i] */
+ if (beta[i] <= orig_u[k])
+ u[k] = orig_u[k];
+ else
+ u[k] = beta[i];
+ }
+ }
+ }
+ }
+ return;
+}
+
+static void remove_perturb(struct csa *csa)
+{ /* remove perturbation */
+ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *orig_l = csa->orig_l;
+ double *orig_u = csa->orig_u;
+ int j, k;
+ /* restore original bounds of variables */
+ memcpy(l, orig_l, (1+n) * sizeof(double));
+ memcpy(u, orig_u, (1+n) * sizeof(double));
+ /* adjust flags of fixed non-basic variables, because in the
+ * perturbed problem such variables might be changed to double-
+ * bounded type */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == u[k])
+ flag[j] = 0;
+ }
+ /* removing perturbation changes primal solution components */
+ csa->phase = csa->beta_st = 0;
+#if 1
+ if (csa->msg_lev >= GLP_MSG_ALL)
+ xprintf("Removing LP perturbation [%d]...\n",
+ csa->it_cnt);
+#endif
+ return;
+}
+
+/***********************************************************************
+* sum_infeas - compute sum of primal infeasibilities
+*
+* This routine compute the sum of primal infeasibilities, which is the
+* current penalty function value. */
+
+static double sum_infeas(SPXLP *lp, const double beta[/*1+m*/])
+{ int m = lp->m;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, k;
+ double sum = 0.0;
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ if (l[k] != -DBL_MAX && beta[i] < l[k])
+ sum += l[k] - beta[i];
+ if (u[k] != +DBL_MAX && beta[i] > u[k])
+ sum += beta[i] - u[k];
+ }
+ return sum;
+}
+
+/***********************************************************************
+* display - display search progress
+*
+* This routine displays some information about the search progress
+* that includes:
+*
+* search phase;
+*
+* number of simplex iterations performed by the solver;
+*
+* original objective value;
+*
+* sum of (scaled) primal infeasibilities;
+*
+* number of infeasibilities (phase I) or non-optimalities (phase II);
+*
+* number of basic factorizations since last display output. */
+
+static void display(struct csa *csa, int spec)
+{ int nnn, k;
+ double obj, sum, *save, *save1;
+#if 1 /* 15/VII-2017 */
+ double tm_cur;
+#endif
+ /* check if the display output should be skipped */
+ if (csa->msg_lev < GLP_MSG_ON) goto skip;
+#if 1 /* 15/VII-2017 */
+ tm_cur = xtime();
+#endif
+ if (csa->out_dly > 0 &&
+#if 0 /* 15/VII-2017 */
+ 1000.0 * xdifftime(xtime(), csa->tm_beg) < csa->out_dly)
+#else
+ 1000.0 * xdifftime(tm_cur, csa->tm_beg) < csa->out_dly)
+#endif
+ goto skip;
+ if (csa->it_cnt == csa->it_dpy) goto skip;
+#if 0 /* 15/VII-2017 */
+ if (!spec && csa->it_cnt % csa->out_frq != 0) goto skip;
+#else
+ if (!spec &&
+ 1000.0 * xdifftime(tm_cur, csa->tm_dpy) < csa->out_frq)
+ goto skip;
+#endif
+ /* compute original objective value */
+ save = csa->lp->c;
+ csa->lp->c = csa->orig_c;
+ obj = csa->dir * spx_eval_obj(csa->lp, csa->beta);
+ csa->lp->c = save;
+#if SCALE_Z
+ obj *= csa->fz;
+#endif
+ /* compute sum of (scaled) primal infeasibilities */
+#if 1 /* 01/VII-2017 */
+ save = csa->lp->l;
+ save1 = csa->lp->u;
+ csa->lp->l = csa->orig_l;
+ csa->lp->u = csa->orig_u;
+#endif
+ sum = sum_infeas(csa->lp, csa->beta);
+#if 1 /* 01/VII-2017 */
+ csa->lp->l = save;
+ csa->lp->u = save1;
+#endif
+ /* compute number of infeasibilities/non-optimalities */
+ switch (csa->phase)
+ { case 1:
+ nnn = 0;
+ for (k = 1; k <= csa->lp->n; k++)
+ if (csa->lp->c[k] != 0.0) nnn++;
+ break;
+ case 2:
+ xassert(csa->d_st);
+ nnn = spx_chuzc_sel(csa->lp, csa->d, csa->tol_dj,
+ csa->tol_dj1, NULL);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ /* display search progress */
+ xprintf("%c%6d: obj = %17.9e inf = %11.3e (%d)",
+ csa->phase == 2 ? '*' : ' ', csa->it_cnt, obj, sum, nnn);
+ if (csa->inv_cnt)
+ { /* number of basis factorizations performed */
+ xprintf(" %d", csa->inv_cnt);
+ csa->inv_cnt = 0;
+ }
+#if 1 /* 23/VI-2017 */
+ if (csa->phase == 1 && csa->r_test == GLP_RT_FLIP)
+ { /*xprintf(" %d,%d", csa->ns_cnt, csa->ls_cnt);*/
+ if (csa->ns_cnt + csa->ls_cnt)
+ xprintf(" %d%%",
+ (100 * csa->ls_cnt) / (csa->ns_cnt + csa->ls_cnt));
+ csa->ns_cnt = csa->ls_cnt = 0;
+ }
+#endif
+ xprintf("\n");
+ csa->it_dpy = csa->it_cnt;
+#if 1 /* 15/VII-2017 */
+ csa->tm_dpy = tm_cur;
+#endif
+skip: return;
+}
+
+/***********************************************************************
+* spx_primal - driver to the primal simplex method
+*
+* This routine is a driver to the two-phase primal simplex method.
+*
+* On exit this routine returns one of the following codes:
+*
+* 0 LP instance has been successfully solved.
+*
+* GLP_EITLIM
+* Iteration limit has been exhausted.
+*
+* GLP_ETMLIM
+* Time limit has been exhausted.
+*
+* GLP_EFAIL
+* The solver failed to solve LP instance. */
+
+static int primal_simplex(struct csa *csa)
+{ /* primal simplex method main logic routine */
+ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ int *head = lp->head;
+ SPXAT *at = csa->at;
+ SPXNT *nt = csa->nt;
+ double *beta = csa->beta;
+ double *d = csa->d;
+ SPXSE *se = csa->se;
+ int *list = csa->list;
+#if 0 /* 11/VI-2017 */
+ double *tcol = csa->tcol;
+ double *trow = csa->trow;
+#endif
+#if 0 /* 09/VII-2017 */
+ double *pi = csa->work;
+ double *rho = csa->work;
+#else
+ double *pi = csa->work.vec;
+ double *rho = csa->work.vec;
+#endif
+ int msg_lev = csa->msg_lev;
+ double tol_bnd = csa->tol_bnd;
+ double tol_bnd1 = csa->tol_bnd1;
+ double tol_dj = csa->tol_dj;
+ double tol_dj1 = csa->tol_dj1;
+ int perturb = -1;
+ /* -1 = perturbation is not used, but enabled
+ * 0 = perturbation is not used and disabled
+ * +1 = perturbation is being used */
+ int j, refct, ret;
+loop: /* main loop starts here */
+ /* compute factorization of the basis matrix */
+ if (!lp->valid)
+ { double cond;
+ ret = spx_factorize(lp);
+ csa->inv_cnt++;
+ if (ret != 0)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: unable to factorize the basis matrix (%d"
+ ")\n", ret);
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+ /* check condition of the basis matrix */
+ cond = bfd_condest(lp->bfd);
+ if (cond > 1.0 / DBL_EPSILON)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: basis matrix is singular to working prec"
+ "ision (cond = %.3g)\n", cond);
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+ if (cond > 0.001 / DBL_EPSILON)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: basis matrix is ill-conditioned (cond "
+ "= %.3g)\n", cond);
+ }
+ /* invalidate basic solution components */
+ csa->beta_st = csa->d_st = 0;
+ }
+ /* compute values of basic variables beta = (beta[i]) */
+ if (!csa->beta_st)
+ { spx_eval_beta(lp, beta);
+ csa->beta_st = 1; /* just computed */
+ /* determine the search phase, if not determined yet */
+ if (!csa->phase)
+ { if (set_penalty(csa, 0.97 * tol_bnd, 0.97 * tol_bnd1))
+ { /* current basic solution is primal infeasible */
+ /* start to minimize the sum of infeasibilities */
+ csa->phase = 1;
+ }
+ else
+ { /* current basic solution is primal feasible */
+ /* start to minimize the original objective function */
+ csa->phase = 2;
+ memcpy(c, csa->orig_c, (1+n) * sizeof(double));
+ }
+ /* working objective coefficients have been changed, so
+ * invalidate reduced costs */
+ csa->d_st = 0;
+ }
+ /* make sure that the current basic solution remains primal
+ * feasible (or pseudo-feasible on phase I) */
+ if (perturb <= 0)
+ { if (check_feas(csa, csa->phase, tol_bnd, tol_bnd1))
+ { /* excessive bound violations due to round-off errors */
+#if 1 /* 01/VII-2017 */
+ if (perturb < 0)
+ { if (msg_lev >= GLP_MSG_ALL)
+ xprintf("Perturbing LP to avoid instability [%d].."
+ ".\n", csa->it_cnt);
+ perturb = 1;
+ goto loop;
+ }
+#endif
+ if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: numerical instability (primal simpl"
+ "ex, phase %s)\n", csa->phase == 1 ? "I" : "II");
+ /* restart the search */
+ lp->valid = 0;
+ csa->phase = 0;
+ goto loop;
+ }
+ if (csa->phase == 1)
+ { int i, cnt;
+ for (i = 1; i <= m; i++)
+ csa->tcol.ind[i] = i;
+ cnt = adjust_penalty(csa, m, csa->tcol.ind,
+ 0.99 * tol_bnd, 0.99 * tol_bnd1);
+ if (cnt)
+ { /*xprintf("*** cnt = %d\n", cnt);*/
+ csa->d_st = 0;
+ }
+ }
+ }
+ else
+ { /* FIXME */
+ play_bounds(csa, 1);
+ }
+ }
+ /* at this point the search phase is determined */
+ xassert(csa->phase == 1 || csa->phase == 2);
+ /* compute reduced costs of non-basic variables d = (d[j]) */
+ if (!csa->d_st)
+ { spx_eval_pi(lp, pi);
+ for (j = 1; j <= n-m; j++)
+ d[j] = spx_eval_dj(lp, pi, j);
+ csa->d_st = 1; /* just computed */
+ }
+ /* reset the reference space, if necessary */
+ if (se != NULL && !se->valid)
+ spx_reset_refsp(lp, se), refct = 1000;
+ /* at this point the basis factorization and all basic solution
+ * components are valid */
+ xassert(lp->valid && csa->beta_st && csa->d_st);
+#if CHECK_ACCURACY
+ /* check accuracy of current basic solution components (only for
+ * debugging) */
+ check_accuracy(csa);
+#endif
+ /* check if the iteration limit has been exhausted */
+ if (csa->it_cnt - csa->it_beg >= csa->it_lim)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ csa->p_stat = (csa->phase == 2 ? GLP_FEAS : GLP_INFEAS);
+ csa->d_stat = GLP_UNDEF; /* will be set below */
+ ret = GLP_EITLIM;
+ goto fini;
+ }
+ /* check if the time limit has been exhausted */
+ if (1000.0 * xdifftime(xtime(), csa->tm_beg) >= csa->tm_lim)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ csa->p_stat = (csa->phase == 2 ? GLP_FEAS : GLP_INFEAS);
+ csa->d_stat = GLP_UNDEF; /* will be set below */
+ ret = GLP_ETMLIM;
+ goto fini;
+ }
+ /* display the search progress */
+ display(csa, 0);
+ /* select eligible non-basic variables */
+ switch (csa->phase)
+ { case 1:
+ csa->num = spx_chuzc_sel(lp, d, 1e-8, 0.0, list);
+ break;
+ case 2:
+ csa->num = spx_chuzc_sel(lp, d, tol_dj, tol_dj1, list);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ /* check for optimality */
+ if (csa->num == 0)
+ { if (perturb > 0 && csa->phase == 2)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ /* current basis is optimal */
+ display(csa, 1);
+ switch (csa->phase)
+ { case 1:
+ /* check for primal feasibility */
+ if (!check_feas(csa, 2, tol_bnd, tol_bnd1))
+ { /* feasible solution found; switch to phase II */
+ memcpy(c, csa->orig_c, (1+n) * sizeof(double));
+ csa->phase = 2;
+ csa->d_st = 0;
+ goto loop;
+ }
+ /* no feasible solution exists */
+#if 1 /* 09/VII-2017 */
+ /* FIXME: remove perturbation */
+#endif
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("LP HAS NO PRIMAL FEASIBLE SOLUTION\n");
+ csa->p_stat = GLP_NOFEAS;
+ csa->d_stat = GLP_UNDEF; /* will be set below */
+ ret = 0;
+ goto fini;
+ case 2:
+ /* optimal solution found */
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("OPTIMAL LP SOLUTION FOUND\n");
+ csa->p_stat = csa->d_stat = GLP_FEAS;
+ ret = 0;
+ goto fini;
+ default:
+ xassert(csa != csa);
+ }
+ }
+ /* choose xN[q] and xB[p] */
+#if 0 /* 23/VI-2017 */
+#if 0 /* 17/III-2016 */
+ choose_pivot(csa);
+#else
+ if (choose_pivot(csa) < 0)
+ { lp->valid = 0;
+ goto loop;
+ }
+#endif
+#else
+ ret = choose_pivot(csa);
+ if (ret < 0)
+ { lp->valid = 0;
+ goto loop;
+ }
+ if (ret == 0)
+ csa->ns_cnt++;
+ else
+ csa->ls_cnt++;
+#endif
+ /* check for unboundedness */
+ if (csa->p == 0)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ switch (csa->phase)
+ { case 1:
+ /* this should never happen */
+ if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: primal simplex failed\n");
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ case 2:
+ /* primal unboundedness detected */
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("LP HAS UNBOUNDED PRIMAL SOLUTION\n");
+ csa->p_stat = GLP_FEAS;
+ csa->d_stat = GLP_NOFEAS;
+ ret = 0;
+ goto fini;
+ default:
+ xassert(csa != csa);
+ }
+ }
+#if 1 /* 01/VII-2017 */
+ /* check for stalling */
+ if (csa->p > 0)
+ { int k;
+ xassert(1 <= csa->p && csa->p <= m);
+ k = head[csa->p]; /* x[k] = xB[p] */
+ if (lp->l[k] != lp->u[k])
+ { if (csa->p_flag)
+ { /* xB[p] goes to its upper bound */
+ xassert(lp->u[k] != +DBL_MAX);
+ if (fabs(beta[csa->p] - lp->u[k]) >= 1e-6)
+ { csa->degen = 0;
+ goto skip1;
+ }
+ }
+ else if (lp->l[k] == -DBL_MAX)
+ { /* unusual case */
+ goto skip1;
+ }
+ else
+ { /* xB[p] goes to its lower bound */
+ xassert(lp->l[k] != -DBL_MAX);
+ if (fabs(beta[csa->p] - lp->l[k]) >= 1e-6)
+ { csa->degen = 0;
+ goto skip1;
+ }
+ }
+ /* degenerate iteration has been detected */
+ csa->degen++;
+ if (perturb < 0 && csa->degen >= 200)
+ { if (msg_lev >= GLP_MSG_ALL)
+ xprintf("Perturbing LP to avoid stalling [%d]...\n",
+ csa->it_cnt);
+ perturb = 1;
+ }
+skip1: ;
+ }
+ }
+#endif
+ /* update values of basic variables for adjacent basis */
+#if 0 /* 11/VI-2017 */
+ spx_update_beta(lp, beta, csa->p, csa->p_flag, csa->q, tcol);
+#else
+ spx_update_beta_s(lp, beta, csa->p, csa->p_flag, csa->q,
+ &csa->tcol);
+#endif
+ csa->beta_st = 2;
+ /* p < 0 means that xN[q] jumps to its opposite bound */
+ if (csa->p < 0)
+ goto skip;
+ /* xN[q] enters and xB[p] leaves the basis */
+ /* compute p-th row of inv(B) */
+ spx_eval_rho(lp, csa->p, rho);
+ /* compute p-th (pivot) row of the simplex table */
+#if 0 /* 11/VI-2017 */
+ if (at != NULL)
+ spx_eval_trow1(lp, at, rho, trow);
+ else
+ spx_nt_prod(lp, nt, trow, 1, -1.0, rho);
+#else
+ if (at != NULL)
+ spx_eval_trow1(lp, at, rho, csa->trow.vec);
+ else
+ spx_nt_prod(lp, nt, csa->trow.vec, 1, -1.0, rho);
+ fvs_gather_vec(&csa->trow, DBL_EPSILON);
+#endif
+ /* FIXME: tcol[p] and trow[q] should be close to each other */
+#if 0 /* 26/V-2017 by cmatraki */
+ xassert(trow[csa->q] != 0.0);
+#else
+ if (csa->trow.vec[csa->q] == 0.0)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: trow[q] = 0.0\n");
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+#endif
+ /* update reduced costs of non-basic variables for adjacent
+ * basis */
+#if 1 /* 23/VI-2017 */
+ /* dual solution may be invalidated due to long step */
+ if (csa->d_st)
+#endif
+#if 0 /* 11/VI-2017 */
+ if (spx_update_d(lp, d, csa->p, csa->q, trow, tcol) <= 1e-9)
+#else
+ if (spx_update_d_s(lp, d, csa->p, csa->q, &csa->trow, &csa->tcol)
+ <= 1e-9)
+#endif
+ { /* successful updating */
+ csa->d_st = 2;
+ if (csa->phase == 1)
+ { /* adjust reduced cost of xN[q] in adjacent basis, since
+ * its penalty coefficient changes (see below) */
+ d[csa->q] -= c[head[csa->p]];
+ }
+ }
+ else
+ { /* new reduced costs are inaccurate */
+ csa->d_st = 0;
+ }
+ if (csa->phase == 1)
+ { /* xB[p] leaves the basis replacing xN[q], so set its penalty
+ * coefficient to zero */
+ c[head[csa->p]] = 0.0;
+ }
+ /* update steepest edge weights for adjacent basis, if used */
+ if (se != NULL)
+ { if (refct > 0)
+#if 0 /* 11/VI-2017 */
+ { if (spx_update_gamma(lp, se, csa->p, csa->q, trow, tcol)
+ <= 1e-3)
+#else /* FIXME: spx_update_gamma_s */
+ { if (spx_update_gamma(lp, se, csa->p, csa->q, csa->trow.vec,
+ csa->tcol.vec) <= 1e-3)
+#endif
+ { /* successful updating */
+ refct--;
+ }
+ else
+ { /* new weights are inaccurate; reset reference space */
+ se->valid = 0;
+ }
+ }
+ else
+ { /* too many updates; reset reference space */
+ se->valid = 0;
+ }
+ }
+ /* update matrix N for adjacent basis, if used */
+ if (nt != NULL)
+ spx_update_nt(lp, nt, csa->p, csa->q);
+skip: /* change current basis header to adjacent one */
+ spx_change_basis(lp, csa->p, csa->p_flag, csa->q);
+ /* and update factorization of the basis matrix */
+ if (csa->p > 0)
+ spx_update_invb(lp, csa->p, head[csa->p]);
+#if 1
+ if (perturb <= 0)
+ { if (csa->phase == 1)
+ { int cnt;
+ /* adjust penalty function coefficients */
+ cnt = adjust_penalty(csa, csa->tcol.nnz, csa->tcol.ind,
+ 0.99 * tol_bnd, 0.99 * tol_bnd1);
+ if (cnt)
+ { /* some coefficients were changed, so invalidate reduced
+ * costs of non-basic variables */
+ /*xprintf("... cnt = %d\n", cnt);*/
+ csa->d_st = 0;
+ }
+ }
+ }
+ else
+ { /* FIXME */
+ play_bounds(csa, 0);
+ }
+#endif
+ /* simplex iteration complete */
+ csa->it_cnt++;
+ goto loop;
+fini: /* restore original objective function */
+ memcpy(c, csa->orig_c, (1+n) * sizeof(double));
+ /* compute reduced costs of non-basic variables and determine
+ * solution dual status, if necessary */
+ if (csa->p_stat != GLP_UNDEF && csa->d_stat == GLP_UNDEF)
+ { xassert(ret != GLP_EFAIL);
+ spx_eval_pi(lp, pi);
+ for (j = 1; j <= n-m; j++)
+ d[j] = spx_eval_dj(lp, pi, j);
+ csa->num = spx_chuzc_sel(lp, d, tol_dj, tol_dj1, NULL);
+ csa->d_stat = (csa->num == 0 ? GLP_FEAS : GLP_INFEAS);
+ }
+ return ret;
+}
+
+int spx_primal(glp_prob *P, const glp_smcp *parm)
+{ /* driver to the primal simplex method */
+ struct csa csa_, *csa = &csa_;
+ SPXLP lp;
+ SPXAT at;
+ SPXNT nt;
+ SPXSE se;
+ int ret, *map, *daeh;
+#if SCALE_Z
+ int i, j, k;
+#endif
+ /* build working LP and its initial basis */
+ memset(csa, 0, sizeof(struct csa));
+ csa->lp = &lp;
+ spx_init_lp(csa->lp, P, parm->excl);
+ spx_alloc_lp(csa->lp);
+ map = talloc(1+P->m+P->n, int);
+ spx_build_lp(csa->lp, P, parm->excl, parm->shift, map);
+ spx_build_basis(csa->lp, P, map);
+ switch (P->dir)
+ { case GLP_MIN:
+ csa->dir = +1;
+ break;
+ case GLP_MAX:
+ csa->dir = -1;
+ break;
+ default:
+ xassert(P != P);
+ }
+#if SCALE_Z
+ csa->fz = 0.0;
+ for (k = 1; k <= csa->lp->n; k++)
+ { double t = fabs(csa->lp->c[k]);
+ if (csa->fz < t)
+ csa->fz = t;
+ }
+ if (csa->fz <= 1000.0)
+ csa->fz = 1.0;
+ else
+ csa->fz /= 1000.0;
+ /*xprintf("csa->fz = %g\n", csa->fz);*/
+ for (k = 0; k <= csa->lp->n; k++)
+ csa->lp->c[k] /= csa->fz;
+#endif
+ csa->orig_c = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_c, csa->lp->c, (1+csa->lp->n) * sizeof(double));
+#if 1 /*PERTURB*/
+ csa->orig_l = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_l, csa->lp->l, (1+csa->lp->n) * sizeof(double));
+ csa->orig_u = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_u, csa->lp->u, (1+csa->lp->n) * sizeof(double));
+#else
+ csa->orig_l = csa->orig_u = NULL;
+#endif
+ switch (parm->aorn)
+ { case GLP_USE_AT:
+ /* build matrix A in row-wise format */
+ csa->at = &at;
+ csa->nt = NULL;
+ spx_alloc_at(csa->lp, csa->at);
+ spx_build_at(csa->lp, csa->at);
+ break;
+ case GLP_USE_NT:
+ /* build matrix N in row-wise format for initial basis */
+ csa->at = NULL;
+ csa->nt = &nt;
+ spx_alloc_nt(csa->lp, csa->nt);
+ spx_init_nt(csa->lp, csa->nt);
+ spx_build_nt(csa->lp, csa->nt);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+ /* allocate and initialize working components */
+ csa->phase = 0;
+ csa->beta = talloc(1+csa->lp->m, double);
+ csa->beta_st = 0;
+ csa->d = talloc(1+csa->lp->n-csa->lp->m, double);
+ csa->d_st = 0;
+ switch (parm->pricing)
+ { case GLP_PT_STD:
+ csa->se = NULL;
+ break;
+ case GLP_PT_PSE:
+ csa->se = &se;
+ spx_alloc_se(csa->lp, csa->se);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+ csa->list = talloc(1+csa->lp->n-csa->lp->m, int);
+#if 0 /* 11/VI-2017 */
+ csa->tcol = talloc(1+csa->lp->m, double);
+ csa->trow = talloc(1+csa->lp->n-csa->lp->m, double);
+#else
+ fvs_alloc_vec(&csa->tcol, csa->lp->m);
+ fvs_alloc_vec(&csa->trow, csa->lp->n-csa->lp->m);
+#endif
+#if 1 /* 23/VI-2017 */
+ csa->bp = NULL;
+#endif
+#if 0 /* 09/VII-2017 */
+ csa->work = talloc(1+csa->lp->m, double);
+#else
+ fvs_alloc_vec(&csa->work, csa->lp->m);
+#endif
+ /* initialize control parameters */
+ csa->msg_lev = parm->msg_lev;
+#if 0 /* 23/VI-2017 */
+ switch (parm->r_test)
+ { case GLP_RT_STD:
+ csa->harris = 0;
+ break;
+ case GLP_RT_HAR:
+#if 1 /* 16/III-2016 */
+ case GLP_RT_FLIP:
+ /* FIXME */
+ /* currently for primal simplex GLP_RT_FLIP is equivalent
+ * to GLP_RT_HAR */
+#endif
+ csa->harris = 1;
+ break;
+ default:
+ xassert(parm != parm);
+ }
+#else
+ switch (parm->r_test)
+ { case GLP_RT_STD:
+ case GLP_RT_HAR:
+ break;
+ case GLP_RT_FLIP:
+ csa->bp = talloc(1+2*csa->lp->m+1, SPXBP);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+ csa->r_test = parm->r_test;
+#endif
+ csa->tol_bnd = parm->tol_bnd;
+ csa->tol_bnd1 = .001 * parm->tol_bnd;
+ csa->tol_dj = parm->tol_dj;
+ csa->tol_dj1 = .001 * parm->tol_dj;
+ csa->tol_piv = parm->tol_piv;
+ csa->it_lim = parm->it_lim;
+ csa->tm_lim = parm->tm_lim;
+ csa->out_frq = parm->out_frq;
+ csa->out_dly = parm->out_dly;
+ /* initialize working parameters */
+ csa->tm_beg = xtime();
+ csa->it_beg = csa->it_cnt = P->it_cnt;
+ csa->it_dpy = -1;
+#if 1 /* 15/VII-2017 */
+ csa->tm_dpy = 0.0;
+#endif
+ csa->inv_cnt = 0;
+#if 1 /* 01/VII-2017 */
+ csa->degen = 0;
+#endif
+#if 1 /* 23/VI-2017 */
+ csa->ns_cnt = csa->ls_cnt = 0;
+#endif
+ /* try to solve working LP */
+ ret = primal_simplex(csa);
+ /* return basis factorization back to problem object */
+ P->valid = csa->lp->valid;
+ P->bfd = csa->lp->bfd;
+ /* set solution status */
+ P->pbs_stat = csa->p_stat;
+ P->dbs_stat = csa->d_stat;
+ /* if the solver failed, do not store basis header and basic
+ * solution components to problem object */
+ if (ret == GLP_EFAIL)
+ goto skip;
+ /* convert working LP basis to original LP basis and store it to
+ * problem object */
+ daeh = talloc(1+csa->lp->n, int);
+ spx_store_basis(csa->lp, P, map, daeh);
+ /* compute simplex multipliers for final basic solution found by
+ * the solver */
+#if 0 /* 09/VII-2017 */
+ spx_eval_pi(csa->lp, csa->work);
+#else
+ spx_eval_pi(csa->lp, csa->work.vec);
+#endif
+ /* convert working LP solution to original LP solution and store
+ * it into the problem object */
+#if SCALE_Z
+ for (i = 1; i <= csa->lp->m; i++)
+ csa->work.vec[i] *= csa->fz;
+ for (j = 1; j <= csa->lp->n-csa->lp->m; j++)
+ csa->d[j] *= csa->fz;
+#endif
+#if 0 /* 09/VII-2017 */
+ spx_store_sol(csa->lp, P, SHIFT, map, daeh, csa->beta, csa->work,
+ csa->d);
+#else
+ spx_store_sol(csa->lp, P, parm->shift, map, daeh, csa->beta,
+ csa->work.vec, csa->d);
+#endif
+ tfree(daeh);
+ /* save simplex iteration count */
+ P->it_cnt = csa->it_cnt;
+ /* report auxiliary/structural variable causing unboundedness */
+ P->some = 0;
+ if (csa->p_stat == GLP_FEAS && csa->d_stat == GLP_NOFEAS)
+ { int k, kk;
+ /* xN[q] = x[k] causes unboundedness */
+ xassert(1 <= csa->q && csa->q <= csa->lp->n - csa->lp->m);
+ k = csa->lp->head[csa->lp->m + csa->q];
+ xassert(1 <= k && k <= csa->lp->n);
+ /* convert to number of original variable */
+ for (kk = 1; kk <= P->m + P->n; kk++)
+ { if (abs(map[kk]) == k)
+ { P->some = kk;
+ break;
+ }
+ }
+ xassert(P->some != 0);
+ }
+skip: /* deallocate working objects and arrays */
+ spx_free_lp(csa->lp);
+ tfree(map);
+ tfree(csa->orig_c);
+#if 1 /*PERTURB*/
+ tfree(csa->orig_l);
+ tfree(csa->orig_u);
+#endif
+ if (csa->at != NULL)
+ spx_free_at(csa->lp, csa->at);
+ if (csa->nt != NULL)
+ spx_free_nt(csa->lp, csa->nt);
+ tfree(csa->beta);
+ tfree(csa->d);
+ if (csa->se != NULL)
+ spx_free_se(csa->lp, csa->se);
+ tfree(csa->list);
+#if 0 /* 11/VI-2017 */
+ tfree(csa->tcol);
+ tfree(csa->trow);
+#else
+ fvs_free_vec(&csa->tcol);
+ fvs_free_vec(&csa->trow);
+#endif
+#if 1 /* 23/VI-2017 */
+ if (csa->bp != NULL)
+ tfree(csa->bp);
+#endif
+#if 0 /* 09/VII-2017 */
+ tfree(csa->work);
+#else
+ fvs_free_vec(&csa->work);
+#endif
+ /* return to calling program */
+ return ret;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxprob.c b/test/monniaux/glpk-4.65/src/simplex/spxprob.c
new file mode 100644
index 00000000..4bebe2e7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxprob.c
@@ -0,0 +1,679 @@
+/* spxprob.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spxprob.h"
+
+/***********************************************************************
+* spx_init_lp - initialize working LP object
+*
+* This routine determines the number of equality constraints m, the
+* number of variables n, and the number of non-zero elements nnz in
+* the constraint matrix for the working LP, which corresponds to the
+* original LP, and stores these dimensions to the working LP object.
+* (The working LP object should be allocated by the calling routine.)
+*
+* If the flag excl is set, the routine assumes that non-basic fixed
+* variables will be excluded from the working LP. */
+
+void spx_init_lp(SPXLP *lp, glp_prob *P, int excl)
+{ int i, j, m, n, nnz;
+ m = P->m;
+ xassert(m > 0);
+ n = 0;
+ nnz = P->nnz;
+ xassert(P->valid);
+ /* scan rows of original LP */
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = P->row[i];
+ if (excl && row->stat == GLP_NS)
+ { /* skip non-basic fixed auxiliary variable */
+ /* nop */
+ }
+ else
+ { /* include auxiliary variable in working LP */
+ n++;
+ nnz++; /* unity column */
+ }
+ }
+ /* scan columns of original LP */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if (excl && col->stat == GLP_NS)
+ { /* skip non-basic fixed structural variable */
+ GLPAIJ *aij;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ nnz--;
+ }
+ else
+ { /* include structural variable in working LP */
+ n++;
+ }
+ }
+ /* initialize working LP data block */
+ memset(lp, 0, sizeof(SPXLP));
+ lp->m = m;
+ xassert(n > 0);
+ lp->n = n;
+ lp->nnz = nnz;
+ return;
+}
+
+/***********************************************************************
+* spx_alloc_lp - allocate working LP arrays
+*
+* This routine allocates the memory for all arrays in the working LP
+* object. */
+
+void spx_alloc_lp(SPXLP *lp)
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ lp->A_ptr = talloc(1+n+1, int);
+ lp->A_ind = talloc(1+nnz, int);
+ lp->A_val = talloc(1+nnz, double);
+ lp->b = talloc(1+m, double);
+ lp->c = talloc(1+n, double);
+ lp->l = talloc(1+n, double);
+ lp->u = talloc(1+n, double);
+ lp->head = talloc(1+n, int);
+ lp->flag = talloc(1+n-m, char);
+ return;
+}
+
+/***********************************************************************
+* spx_build_lp - convert original LP to working LP
+*
+* This routine converts components (except the current basis) of the
+* original LP to components of the working LP and perform scaling of
+* these components. Also, if the original LP is maximization, the
+* routine changes the signs of the objective coefficients and constant
+* term to opposite ones.
+*
+* If the flag excl is set, original non-basic fixed variables are
+* *not* included in the working LP. Otherwise, all (auxiliary and
+* structural) original variables are included in the working LP. Note
+* that this flag should have the same value as it has in a call to the
+* routine spx_init_lp.
+*
+* If the flag shift is set, the routine shift bounds of variables
+* included in the working LP to make at least one bound to be zero.
+* If a variable has both lower and upper bounds, the bound having
+* smaller magnitude is shifted to zero.
+*
+* On exit the routine stores information about correspondence between
+* numbers of variables in the original and working LPs to the array
+* map, which should have 1+P->m+P->n locations (location [0] is not
+* used), where P->m is the numbers of rows and P->n is the number of
+* columns in the original LP:
+*
+* map[i] = +k, 1 <= i <= P->m, means that i-th auxiliary variable of
+* the original LP corresponds to variable x[k] of the working LP;
+*
+* map[i] = -k, 1 <= i <= P->m, means that i-th auxiliary variable of
+* the original LP corresponds to variable x[k] of the working LP, and
+* the upper bound of that variable was shifted to zero;
+*
+* map[i] = 0, 1 <= i <= P->m, means that i-th auxiliary variable of
+* the original LP was excluded from the working LP;
+*
+* map[P->m+j], 1 <= j <= P->n, has the same sense as above, however,
+* for j-th structural variable of the original LP. */
+
+void spx_build_lp(SPXLP *lp, glp_prob *P, int excl, int shift,
+ int map[/*1+P->m+P->n*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int nnz = lp->nnz;
+ int *A_ptr = lp->A_ptr;
+ int *A_ind = lp->A_ind;
+ double *A_val = lp->A_val;
+ double *b = lp->b;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int i, j, k, kk, ptr, end;
+ double dir, delta;
+ /* working LP is always minimization */
+ switch (P->dir)
+ { case GLP_MIN:
+ dir = +1.0;
+ break;
+ case GLP_MAX:
+ dir = -1.0;
+ break;
+ default:
+ xassert(P != P);
+ }
+ /* initialize constant term of the objective */
+ c[0] = dir * P->c0;
+ k = 0; /* number of variable in working LP */
+ ptr = 1; /* current available position in A_ind/A_val */
+ /* process rows of original LP */
+ xassert(P->m == m);
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = P->row[i];
+ if (excl && row->stat == GLP_NS)
+ { /* i-th auxiliary variable is non-basic and fixed */
+ /* substitute its scaled value in working LP */
+ xassert(row->type == GLP_FX);
+ map[i] = 0;
+ b[i] = - row->lb * row->rii;
+ }
+ else
+ { /* include i-th auxiliary variable in working LP */
+ map[i] = ++k;
+ /* setup k-th column of working constraint matrix which is
+ * i-th column of unity matrix */
+ A_ptr[k] = ptr;
+ A_ind[ptr] = i;
+ A_val[ptr] = 1.0;
+ ptr++;
+ /* initialize right-hand side of i-th equality constraint
+ * and setup zero objective coefficient at variable x[k] */
+ b[i] = c[k] = 0.0;
+ /* setup scaled bounds of variable x[k] */
+ switch (row->type)
+ { case GLP_FR:
+ l[k] = -DBL_MAX, u[k] = +DBL_MAX;
+ break;
+ case GLP_LO:
+ l[k] = row->lb * row->rii, u[k] = +DBL_MAX;
+ break;
+ case GLP_UP:
+ l[k] = -DBL_MAX, u[k] = row->ub * row->rii;
+ break;
+ case GLP_DB:
+ l[k] = row->lb * row->rii, u[k] = row->ub * row->rii;
+ xassert(l[k] != u[k]);
+ break;
+ case GLP_FX:
+ l[k] = u[k] = row->lb * row->rii;
+ break;
+ default:
+ xassert(row != row);
+ }
+ }
+ }
+ /* process columns of original LP */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ GLPAIJ *aij;
+ if (excl && col->stat == GLP_NS)
+ { /* j-th structural variable is non-basic and fixed */
+ /* substitute its scaled value in working LP */
+ xassert(col->type == GLP_FX);
+ map[m+j] = 0;
+ if (col->lb != 0.0)
+ { /* (note that sjj scale factor is cancelled) */
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ b[aij->row->i] +=
+ (aij->row->rii * aij->val) * col->lb;
+ c[0] += (dir * col->coef) * col->lb;
+ }
+ }
+ else
+ { /* include j-th structural variable in working LP */
+ map[m+j] = ++k;
+ /* setup k-th column of working constraint matrix which is
+ * scaled j-th column of original constraint matrix (-A) */
+ A_ptr[k] = ptr;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ { A_ind[ptr] = aij->row->i;
+ A_val[ptr] = - aij->row->rii * aij->val * col->sjj;
+ ptr++;
+ }
+ /* setup scaled objective coefficient at variable x[k] */
+ c[k] = dir * col->coef * col->sjj;
+ /* setup scaled bounds of variable x[k] */
+ switch (col->type)
+ { case GLP_FR:
+ l[k] = -DBL_MAX, u[k] = +DBL_MAX;
+ break;
+ case GLP_LO:
+ l[k] = col->lb / col->sjj, u[k] = +DBL_MAX;
+ break;
+ case GLP_UP:
+ l[k] = -DBL_MAX, u[k] = col->ub / col->sjj;
+ break;
+ case GLP_DB:
+ l[k] = col->lb / col->sjj, u[k] = col->ub / col->sjj;
+ xassert(l[k] != u[k]);
+ break;
+ case GLP_FX:
+ l[k] = u[k] = col->lb / col->sjj;
+ break;
+ default:
+ xassert(col != col);
+ }
+ }
+ }
+ xassert(k == n);
+ xassert(ptr == nnz+1);
+ A_ptr[n+1] = ptr;
+ /* shift bounds of all variables of working LP (optionally) */
+ if (shift)
+ { for (kk = 1; kk <= m+P->n; kk++)
+ { k = map[kk];
+ if (k == 0)
+ { /* corresponding original variable was excluded */
+ continue;
+ }
+ /* shift bounds of variable x[k] */
+ if (l[k] == -DBL_MAX && u[k] == +DBL_MAX)
+ { /* x[k] is unbounded variable */
+ delta = 0.0;
+ }
+ else if (l[k] != -DBL_MAX && u[k] == +DBL_MAX)
+ { /* shift lower bound to zero */
+ delta = l[k];
+ l[k] = 0.0;
+ }
+ else if (l[k] == -DBL_MAX && u[k] != +DBL_MAX)
+ { /* shift upper bound to zero */
+ map[kk] = -k;
+ delta = u[k];
+ u[k] = 0.0;
+ }
+ else if (l[k] != u[k])
+ { /* x[k] is double bounded variable */
+ if (fabs(l[k]) <= fabs(u[k]))
+ { /* shift lower bound to zero */
+ delta = l[k];
+ l[k] = 0.0, u[k] -= delta;
+ }
+ else
+ { /* shift upper bound to zero */
+ map[kk] = -k;
+ delta = u[k];
+ l[k] -= delta, u[k] = 0.0;
+ }
+ xassert(l[k] != u[k]);
+ }
+ else
+ { /* shift fixed value to zero */
+ delta = l[k];
+ l[k] = u[k] = 0.0;
+ }
+ /* substitute x[k] = x'[k] + delta into all constraints
+ * and the objective function of working LP */
+ if (delta != 0.0)
+ { ptr = A_ptr[k];
+ end = A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ b[A_ind[ptr]] -= A_val[ptr] * delta;
+ c[0] += c[k] * delta;
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_build_basis - convert original LP basis to working LP basis
+*
+* This routine converts the current basis of the original LP to
+* corresponding initial basis of the working LP, and moves the basis
+* factorization driver from the original LP object to the working LP
+* object.
+*
+* The array map should contain information provided by the routine
+* spx_build_lp. */
+
+void spx_build_basis(SPXLP *lp, glp_prob *P, const int map[])
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int i, j, k, ii, jj;
+ /* original basis factorization should be valid that guarantees
+ * the basis is correct */
+ xassert(P->m == m);
+ xassert(P->valid);
+ /* initialize basis header for working LP */
+ memset(&head[1], 0, m * sizeof(int));
+ jj = 0;
+ /* scan rows of original LP */
+ xassert(P->m == m);
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = P->row[i];
+ /* determine ordinal number of x[k] in working LP */
+ if ((k = map[i]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* corresponding original variable was excluded */
+ continue;
+ }
+ xassert(1 <= k && k <= n);
+ if (row->stat == GLP_BS)
+ { /* x[k] is basic variable xB[ii] */
+ ii = row->bind;
+ xassert(1 <= ii && ii <= m);
+ xassert(head[ii] == 0);
+ head[ii] = k;
+ }
+ else
+ { /* x[k] is non-basic variable xN[jj] */
+ jj++;
+ head[m+jj] = k;
+ flag[jj] = (row->stat == GLP_NU);
+ }
+ }
+ /* scan columns of original LP */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ /* determine ordinal number of x[k] in working LP */
+ if ((k = map[m+j]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* corresponding original variable was excluded */
+ continue;
+ }
+ xassert(1 <= k && k <= n);
+ if (col->stat == GLP_BS)
+ { /* x[k] is basic variable xB[ii] */
+ ii = col->bind;
+ xassert(1 <= ii && ii <= m);
+ xassert(head[ii] == 0);
+ head[ii] = k;
+ }
+ else
+ { /* x[k] is non-basic variable xN[jj] */
+ jj++;
+ head[m+jj] = k;
+ flag[jj] = (col->stat == GLP_NU);
+ }
+ }
+ xassert(m+jj == n);
+ /* acquire basis factorization */
+ lp->valid = 1;
+ lp->bfd = P->bfd;
+ P->valid = 0;
+ P->bfd = NULL;
+ return;
+}
+
+/***********************************************************************
+* spx_store_basis - convert working LP basis to original LP basis
+*
+* This routine converts the current working LP basis to corresponding
+* original LP basis. This operations includes determining and setting
+* statuses of all rows (auxiliary variables) and columns (structural
+* variables), and building the basis header.
+*
+* The array map should contain information provided by the routine
+* spx_build_lp.
+*
+* On exit the routine fills the array daeh. This array should have
+* 1+lp->n locations (location [0] is not used) and contain the inverse
+* of the working basis header lp->head, i.e. head[k'] = k means that
+* daeh[k] = k'. */
+
+void spx_store_basis(SPXLP *lp, glp_prob *P, const int map[],
+ int daeh[/*1+n*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int i, j, k, kk;
+ /* determine inverse of working basis header */
+ for (kk = 1; kk <= n; kk++)
+ daeh[head[kk]] = kk;
+ /* set row statuses */
+ xassert(P->m == m);
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = P->row[i];
+ if ((k = map[i]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* non-basic fixed auxiliary variable was excluded */
+ xassert(row->type == GLP_FX);
+ row->stat = GLP_NS;
+ row->bind = 0;
+ }
+ else
+ { /* auxiliary variable corresponds to variable x[k] */
+ kk = daeh[k];
+ if (kk <= m)
+ { /* x[k] = xB[kk] */
+ P->head[kk] = i;
+ row->stat = GLP_BS;
+ row->bind = kk;
+ }
+ else
+ { /* x[k] = xN[kk-m] */
+ switch (row->type)
+ { case GLP_FR:
+ row->stat = GLP_NF;
+ break;
+ case GLP_LO:
+ row->stat = GLP_NL;
+ break;
+ case GLP_UP:
+ row->stat = GLP_NU;
+ break;
+ case GLP_DB:
+ row->stat = (flag[kk-m] ? GLP_NU : GLP_NL);
+ break;
+ case GLP_FX:
+ row->stat = GLP_NS;
+ break;
+ default:
+ xassert(row != row);
+ }
+ row->bind = 0;
+ }
+ }
+ }
+ /* set column statuses */
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if ((k = map[m+j]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* non-basic fixed structural variable was excluded */
+ xassert(col->type == GLP_FX);
+ col->stat = GLP_NS;
+ col->bind = 0;
+ }
+ else
+ { /* structural variable corresponds to variable x[k] */
+ kk = daeh[k];
+ if (kk <= m)
+ { /* x[k] = xB[kk] */
+ P->head[kk] = m+j;
+ col->stat = GLP_BS;
+ col->bind = kk;
+ }
+ else
+ { /* x[k] = xN[kk-m] */
+ switch (col->type)
+ { case GLP_FR:
+ col->stat = GLP_NF;
+ break;
+ case GLP_LO:
+ col->stat = GLP_NL;
+ break;
+ case GLP_UP:
+ col->stat = GLP_NU;
+ break;
+ case GLP_DB:
+ col->stat = (flag[kk-m] ? GLP_NU : GLP_NL);
+ break;
+ case GLP_FX:
+ col->stat = GLP_NS;
+ break;
+ default:
+ xassert(col != col);
+ }
+ col->bind = 0;
+ }
+ }
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_store_sol - convert working LP solution to original LP solution
+*
+* This routine converts the current basic solution of the working LP
+* (values of basic variables, simplex multipliers, reduced costs of
+* non-basic variables) to corresponding basic solution of the original
+* LP (values and reduced costs of auxiliary and structural variables).
+* This conversion includes unscaling all basic solution components,
+* computing reduced costs of excluded non-basic variables, recovering
+* unshifted values of basic variables, changing the signs of reduced
+* costs (if the original LP is maximization), and computing the value
+* of the objective function.
+*
+* The flag shift should have the same value as it has in a call to the
+* routine spx_build_lp.
+*
+* The array map should contain information provided by the routine
+* spx_build_lp.
+*
+* The array daeh should contain information provided by the routine
+* spx_store_basis.
+*
+* The arrays beta, pi, and d should contain basic solution components
+* for the working LP:
+*
+* array locations beta[1], ..., beta[m] should contain values of basic
+* variables beta = (beta[i]);
+*
+* array locations pi[1], ..., pi[m] should contain simplex multipliers
+* pi = (pi[i]);
+*
+* array locations d[1], ..., d[n-m] should contain reduced costs of
+* non-basic variables d = (d[j]). */
+
+void spx_store_sol(SPXLP *lp, glp_prob *P, int shift,
+ const int map[], const int daeh[], const double beta[],
+ const double pi[], const double d[])
+{ int m = lp->m;
+ char *flag = lp->flag;
+ int i, j, k, kk;
+ double dir;
+ /* working LP is always minimization */
+ switch (P->dir)
+ { case GLP_MIN:
+ dir = +1.0;
+ break;
+ case GLP_MAX:
+ dir = -1.0;
+ break;
+ default:
+ xassert(P != P);
+ }
+ /* compute row solution components */
+ xassert(P->m == m);
+ for (i = 1; i <= m; i++)
+ { GLPROW *row = P->row[i];
+ if ((k = map[i]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* non-basic fixed auxiliary variable was excluded */
+ xassert(row->type == GLP_FX);
+ row->prim = row->lb;
+ /* compute reduced cost d[k] = c[k] - A'[k] * pi as if x[k]
+ * would be non-basic in working LP */
+ row->dual = - dir * pi[i] * row->rii;
+ }
+ else
+ { /* auxiliary variable corresponds to variable x[k] */
+ kk = daeh[k];
+ if (kk <= m)
+ { /* x[k] = xB[kk] */
+ row->prim = beta[kk] / row->rii;
+ if (shift)
+ row->prim += (map[i] < 0 ? row->ub : row->lb);
+ row->dual = 0.0;
+ }
+ else
+ { /* x[k] = xN[kk-m] */
+ row->prim = (flag[kk-m] ? row->ub : row->lb);
+ row->dual = (dir * d[kk-m]) * row->rii;
+ }
+ }
+ }
+ /* compute column solution components and objective value */
+ P->obj_val = P->c0;
+ for (j = 1; j <= P->n; j++)
+ { GLPCOL *col = P->col[j];
+ if ((k = map[m+j]) < 0)
+ k = -k;
+ if (k == 0)
+ { /* non-basic fixed structural variable was excluded */
+ GLPAIJ *aij;
+ double dk;
+ xassert(col->type == GLP_FX);
+ col->prim = col->lb;
+ /* compute reduced cost d[k] = c[k] - A'[k] * pi as if x[k]
+ * would be non-basic in working LP */
+ /* (note that sjj scale factor is cancelled) */
+ dk = dir * col->coef;
+ for (aij = col->ptr; aij != NULL; aij = aij->c_next)
+ dk += (aij->row->rii * aij->val) * pi[aij->row->i];
+ col->dual = dir * dk;
+ }
+ else
+ { /* structural variable corresponds to variable x[k] */
+ kk = daeh[k];
+ if (kk <= m)
+ { /* x[k] = xB[kk] */
+ col->prim = beta[kk] * col->sjj;
+ if (shift)
+ col->prim += (map[m+j] < 0 ? col->ub : col->lb);
+ col->dual = 0.0;
+ }
+ else
+ { /* x[k] = xN[kk-m] */
+ col->prim = (flag[kk-m] ? col->ub : col->lb);
+ col->dual = (dir * d[kk-m]) / col->sjj;
+ }
+ }
+ P->obj_val += col->coef * col->prim;
+ }
+ return;
+}
+
+/***********************************************************************
+* spx_free_lp - deallocate working LP arrays
+*
+* This routine deallocates the memory used for arrays of the working
+* LP object. */
+
+void spx_free_lp(SPXLP *lp)
+{ tfree(lp->A_ptr);
+ tfree(lp->A_ind);
+ tfree(lp->A_val);
+ tfree(lp->b);
+ tfree(lp->c);
+ tfree(lp->l);
+ tfree(lp->u);
+ tfree(lp->head);
+ tfree(lp->flag);
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spxprob.h b/test/monniaux/glpk-4.65/src/simplex/spxprob.h
new file mode 100644
index 00000000..b7d87fa7
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spxprob.h
@@ -0,0 +1,64 @@
+/* spxprob.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPXPROB_H
+#define SPXPROB_H
+
+#include "prob.h"
+#include "spxlp.h"
+
+#define spx_init_lp _glp_spx_init_lp
+void spx_init_lp(SPXLP *lp, glp_prob *P, int excl);
+/* initialize working LP object */
+
+#define spx_alloc_lp _glp_spx_alloc_lp
+void spx_alloc_lp(SPXLP *lp);
+/* allocate working LP arrays */
+
+#define spx_build_lp _glp_spx_build_lp
+void spx_build_lp(SPXLP *lp, glp_prob *P, int excl, int shift,
+ int map[/*1+P->m+P->n*/]);
+/* convert original LP to working LP */
+
+#define spx_build_basis _glp_spx_build_basis
+void spx_build_basis(SPXLP *lp, glp_prob *P, const int map[]);
+/* convert original LP basis to working LP basis */
+
+#define spx_store_basis _glp_spx_store_basis
+void spx_store_basis(SPXLP *lp, glp_prob *P, const int map[],
+ int daeh[/*1+n*/]);
+/* convert working LP basis to original LP basis */
+
+#define spx_store_sol _glp_spx_store_sol
+void spx_store_sol(SPXLP *lp, glp_prob *P, int shift,
+ const int map[], const int daeh[], const double beta[],
+ const double pi[], const double d[]);
+/* convert working LP solution to original LP solution */
+
+#define spx_free_lp _glp_spx_free_lp
+void spx_free_lp(SPXLP *lp);
+/* deallocate working LP arrays */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spychuzc.c b/test/monniaux/glpk-4.65/src/simplex/spychuzc.c
new file mode 100644
index 00000000..b9221298
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spychuzc.c
@@ -0,0 +1,567 @@
+/* spychuzc.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2018 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spychuzc.h"
+
+/***********************************************************************
+* spy_chuzc_std - choose non-basic variable (dual textbook ratio test)
+*
+* This routine implements an improved dual textbook ratio test to
+* choose non-basic variable xN[q].
+*
+* Current reduced costs of non-basic variables should be placed in the
+* array locations d[1], ..., d[n-m]. Note that d[j] is a value of dual
+* basic variable lambdaN[j] in the current basis.
+*
+#if 0 (* 14/III-2016 *)
+* The parameter s specifies the sign of bound violation for basic
+* variable xB[p] chosen: s = +1.0 means that xB[p] violates its lower
+* bound, so dual non-basic variable lambdaB[p] = lambda^+B[p]
+* increases, and s = -1.0 means that xB[p] violates its upper bound,
+* so dual non-basic variable lambdaB[p] = lambda^-B[p] decreases.
+* (Thus, the dual ray parameter theta = s * lambdaB[p] >= 0.)
+#else
+* The parameter r specifies the bound violation for basic variable
+* xB[p] chosen:
+*
+* r = lB[p] - beta[p] > 0 means that xB[p] violates its lower bound,
+* so dual non-basic variable lambdaB[p] = lambda^+B[p] increases; and
+*
+* r = uB[p] - beta[p] < 0 means that xB[p] violates its upper bound,
+* so dual non-basic variable lambdaB[p] = lambda^-B[p] decreases.
+*
+* (Note that r is the dual reduced cost of lambdaB[p].)
+#endif
+*
+* Elements of p-th simplex table row t[p] = (t[p,j]) corresponding
+* to basic variable xB[p] should be placed in the array locations
+* trow[1], ..., trow[n-m].
+*
+* The parameter tol_piv specifies a tolerance for elements of the
+* simplex table row t[p]. If |t[p,j]| < tol_piv, dual basic variable
+* lambdaN[j] is skipped, i.e. it is assumed that it does not depend on
+* the dual ray parameter theta.
+*
+* The parameters tol and tol1 specify tolerances used to increase the
+* choice freedom by simulating an artificial degeneracy as follows.
+* If lambdaN[j] = lambda^+N[j] >= 0 and d[j] <= +delta[j], or if
+* lambdaN[j] = lambda^-N[j] <= 0 and d[j] >= -delta[j], where
+* delta[j] = tol + tol1 * |cN[j]|, cN[j] is objective coefficient at
+* xN[j], then it is assumed that reduced cost d[j] is equal to zero.
+*
+* The routine determines the index 1 <= q <= n-m of non-basic variable
+* xN[q], for which corresponding dual basic variable lambda^+N[j] or
+* lambda^-N[j] reaches its zero bound first on increasing the dual ray
+* parameter theta, and returns p on exit. And if theta may increase
+* unlimitedly, the routine returns zero. */
+
+int spy_chuzc_std(SPXLP *lp, const double d[/*1+n-m*/],
+#if 0 /* 14/III-2016 */
+ double s, const double trow[/*1+n-m*/], double tol_piv,
+#else
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+#endif
+ double tol, double tol1)
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k, q;
+ double alfa, biga, delta, teta, teta_min;
+#if 0 /* 14/III-2016 */
+ xassert(s == +1.0 || s == -1.0);
+#else
+ double s;
+ xassert(r != 0.0);
+ s = (r > 0.0 ? +1.0 : -1.0);
+#endif
+ /* nothing is chosen so far */
+ q = 0, teta_min = DBL_MAX, biga = 0.0;
+ /* walk thru the list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* if xN[j] is fixed variable, skip it */
+ if (l[k] == u[k])
+ continue;
+ alfa = s * trow[j];
+ if (alfa >= +tol_piv && !flag[j])
+ { /* xN[j] is either free or has its lower bound active, so
+ * lambdaN[j] = d[j] >= 0 decreases down to zero */
+ delta = tol + tol1 * (c[k] >= 0.0 ? +c[k] : -c[k]);
+ /* determine theta on which lambdaN[j] reaches zero */
+ teta = (d[j] < +delta ? 0.0 : d[j] / alfa);
+ }
+ else if (alfa <= -tol_piv && (l[k] == -DBL_MAX || flag[j]))
+ { /* xN[j] is either free or has its upper bound active, so
+ * lambdaN[j] = d[j] <= 0 increases up to zero */
+ delta = tol + tol1 * (c[k] >= 0.0 ? +c[k] : -c[k]);
+ /* determine theta on which lambdaN[j] reaches zero */
+ teta = (d[j] > -delta ? 0.0 : d[j] / alfa);
+ }
+ else
+ { /* lambdaN[j] cannot reach zero on increasing theta */
+ continue;
+ }
+ /* choose non-basic variable xN[q] by corresponding dual basic
+ * variable lambdaN[q] for which theta is minimal */
+ xassert(teta >= 0.0);
+ alfa = (alfa >= 0.0 ? +alfa : -alfa);
+ if (teta_min > teta || (teta_min == teta && biga < alfa))
+ q = j, teta_min = teta, biga = alfa;
+ }
+ return q;
+}
+
+/***********************************************************************
+* spy_chuzc_harris - choose non-basic var. (dual Harris' ratio test)
+*
+* This routine implements dual Harris' ratio test to choose non-basic
+* variable xN[q].
+*
+* All the parameters, except tol and tol1, as well as the returned
+* value have the same meaning as for the routine spx_chuzr_std (see
+* above).
+*
+* The parameters tol and tol1 specify tolerances on zero bound
+* violations for reduced costs of non-basic variables. For reduced
+* cost d[j] the tolerance is delta[j] = tol + tol1 |cN[j]|, where
+* cN[j] is objective coefficient at non-basic variable xN[j]. */
+
+int spy_chuzc_harris(SPXLP *lp, const double d[/*1+n-m*/],
+#if 0 /* 14/III-2016 */
+ double s, const double trow[/*1+n-m*/], double tol_piv,
+#else
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+#endif
+ double tol, double tol1)
+{ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k, q;
+ double alfa, biga, delta, teta, teta_min;
+#if 0 /* 14/III-2016 */
+ xassert(s == +1.0 || s == -1.0);
+#else
+ double s;
+ xassert(r != 0.0);
+ s = (r > 0.0 ? +1.0 : -1.0);
+#endif
+ /*--------------------------------------------------------------*/
+ /* first pass: determine teta_min for relaxed bounds */
+ /*--------------------------------------------------------------*/
+ teta_min = DBL_MAX;
+ /* walk thru the list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* if xN[j] is fixed variable, skip it */
+ if (l[k] == u[k])
+ continue;
+ alfa = s * trow[j];
+ if (alfa >= +tol_piv && !flag[j])
+ { /* xN[j] is either free or has its lower bound active, so
+ * lambdaN[j] = d[j] >= 0 decreases down to zero */
+ delta = tol + tol1 * (c[k] >= 0.0 ? +c[k] : -c[k]);
+ /* determine theta on which lambdaN[j] reaches -delta */
+ teta = ((d[j] < 0.0 ? 0.0 : d[j]) + delta) / alfa;
+ }
+ else if (alfa <= -tol_piv && (l[k] == -DBL_MAX || flag[j]))
+ { /* xN[j] is either free or has its upper bound active, so
+ * lambdaN[j] = d[j] <= 0 increases up to zero */
+ delta = tol + tol1 * (c[k] >= 0.0 ? +c[k] : -c[k]);
+ /* determine theta on which lambdaN[j] reaches +delta */
+ teta = ((d[j] > 0.0 ? 0.0 : d[j]) - delta) / alfa;
+ }
+ else
+ { /* lambdaN[j] cannot reach zero on increasing theta */
+ continue;
+ }
+ xassert(teta >= 0.0);
+ if (teta_min > teta)
+ teta_min = teta;
+ }
+ /*--------------------------------------------------------------*/
+ /* second pass: choose non-basic variable xN[q] */
+ /*--------------------------------------------------------------*/
+ if (teta_min == DBL_MAX)
+ { /* theta may increase unlimitedly */
+ q = 0;
+ goto done;
+ }
+ /* nothing is chosen so far */
+ q = 0, biga = 0.0;
+ /* walk thru the list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* if xN[j] is fixed variable, skip it */
+ if (l[k] == u[k])
+ continue;
+ alfa = s * trow[j];
+ if (alfa >= +tol_piv && !flag[j])
+ { /* xN[j] is either free or has its lower bound active, so
+ * lambdaN[j] = d[j] >= 0 decreases down to zero */
+ /* determine theta on which lambdaN[j] reaches zero */
+ teta = d[j] / alfa;
+ }
+ else if (alfa <= -tol_piv && (l[k] == -DBL_MAX || flag[j]))
+ { /* xN[j] is either free or has its upper bound active, so
+ * lambdaN[j] = d[j] <= 0 increases up to zero */
+ /* determine theta on which lambdaN[j] reaches zero */
+ teta = d[j] / alfa;
+ }
+ else
+ { /* lambdaN[j] cannot reach zero on increasing theta */
+ continue;
+ }
+ /* choose non-basic variable for which theta is not greater
+ * than theta_min determined for relaxed bounds and which has
+ * best (largest in magnitude) pivot */
+ alfa = (alfa >= 0.0 ? +alfa : -alfa);
+ if (teta <= teta_min && biga < alfa)
+ q = j, biga = alfa;
+ }
+ /* something must be chosen */
+ xassert(1 <= q && q <= n-m);
+done: return q;
+}
+
+#if 0 /* 23/III-2016 */
+/***********************************************************************
+* spy_eval_bp - determine dual objective function break-points
+*
+* This routine determines the dual objective function break-points.
+*
+* The parameters lp, d, r, trow, and tol_piv have the same meaning as
+* for the routine spx_chuzc_std (see above).
+*
+* On exit the routine stores the break-points determined to the array
+* elements bp[1], ..., bp[num], where 0 <= num <= n-m is the number of
+* break-points returned by the routine.
+*
+* The break-points stored in the array bp are ordered by ascending
+* the ray parameter teta >= 0. The break-points numbered 1, ..., num-1
+* always correspond to non-basic non-fixed variables xN[j] of primal
+* LP having both lower and upper bounds while the last break-point
+* numbered num may correspond to a non-basic variable having only one
+* lower or upper bound, if such variable prevents further increasing
+* of the ray parameter teta. Besides, the routine includes in the
+* array bp only the break-points that correspond to positive increment
+* of the dual objective. */
+
+static int CDECL fcmp(const void *v1, const void *v2)
+{ const SPYBP *p1 = v1, *p2 = v2;
+ if (p1->teta < p2->teta)
+ return -1;
+ else if (p1->teta > p2->teta)
+ return +1;
+ else
+ return 0;
+}
+
+int spy_eval_bp(SPXLP *lp, const double d[/*1+n-m*/],
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+ SPYBP bp[/*1+n-m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, j_max, k, t, nnn, num;
+ double s, alfa, teta, teta_max, dz, v;
+ xassert(r != 0.0);
+ s = (r > 0.0 ? +1.0 : -1.0);
+ /* build the list of all dual basic variables lambdaN[j] that
+ * can reach zero on increasing the ray parameter teta >= 0 */
+ num = 0;
+ /* walk thru the list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* if xN[j] is fixed variable, skip it */
+ if (l[k] == u[k])
+ continue;
+ alfa = s * trow[j];
+ if (alfa >= +tol_piv && !flag[j])
+ { /* xN[j] is either free or has its lower bound active, so
+ * lambdaN[j] = d[j] >= 0 decreases down to zero */
+ /* determine teta[j] on which lambdaN[j] reaches zero */
+ teta = (d[j] < 0.0 ? 0.0 : d[j] / alfa);
+ }
+ else if (alfa <= -tol_piv && (l[k] == -DBL_MAX || flag[j]))
+ { /* xN[j] is either free or has its upper bound active, so
+ * lambdaN[j] = d[j] <= 0 increases up to zero */
+ /* determine teta[j] on which lambdaN[j] reaches zero */
+ teta = (d[j] > 0.0 ? 0.0 : d[j] / alfa);
+ }
+ else
+ { /* lambdaN[j] cannot reach zero on increasing teta */
+ continue;
+ }
+ /* add lambdaN[j] to the list */
+ num++;
+ bp[num].j = j;
+ bp[num].teta = teta;
+ }
+ if (num == 0)
+ { /* dual unboundedness */
+ goto done;
+ }
+ /* determine "blocking" dual basic variable lambdaN[j_max] that
+ * prevents increasing teta more than teta_max */
+ j_max = 0, teta_max = DBL_MAX;
+ for (t = 1; t <= num; t++)
+ { j = bp[t].j;
+ k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == -DBL_MAX || u[k] == +DBL_MAX)
+ { /* lambdaN[j] cannot intersect zero */
+ if (j_max == 0
+ || teta_max > bp[t].teta
+ || (teta_max == bp[t].teta
+ && fabs(trow[j_max]) < fabs(trow[j])))
+ j_max = j, teta_max = bp[t].teta;
+ }
+ }
+ /* keep in the list only dual basic variables lambdaN[j] that
+ * correspond to primal double-bounded variables xN[j] and whose
+ * teta[j] is not greater than teta_max */
+ nnn = 0;
+ for (t = 1; t <= num; t++)
+ { j = bp[t].j;
+ k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] != -DBL_MAX && u[k] != +DBL_MAX
+ && bp[t].teta <= teta_max)
+ { nnn++;
+ bp[nnn].j = j;
+ bp[nnn].teta = bp[t].teta;
+ }
+ }
+ num = nnn;
+ /* sort break-points by ascending teta[j] */
+ qsort(&bp[1], num, sizeof(SPYBP), fcmp);
+ /* add lambdaN[j_max] to the end of the list */
+ if (j_max != 0)
+ { xassert(num < n-m);
+ num++;
+ bp[num].j = j_max;
+ bp[num].teta = teta_max;
+ }
+ /* compute increments of the dual objective at all break-points
+ * (relative to its value at teta = 0) */
+ dz = 0.0; /* dual objective increment */
+ v = fabs(r); /* dual objective slope d zeta / d teta */
+ for (t = 1; t <= num; t++)
+ { /* compute increment at current break-point */
+ dz += v * (bp[t].teta - (t == 1 ? 0.0 : bp[t-1].teta));
+ if (dz < 0.001)
+ { /* break-point with non-positive increment reached */
+ num = t - 1;
+ break;
+ }
+ bp[t].dz = dz;
+ /* compute next slope on the right to current break-point */
+ if (t < num)
+ { j = bp[t].j;
+ k = head[m+j]; /* x[k] = xN[j] */
+ xassert(-DBL_MAX < l[k] && l[k] < u[k] && u[k] < +DBL_MAX);
+ v -= fabs(trow[j]) * (u[k] - l[k]);
+ }
+ }
+done: return num;
+}
+#endif
+
+/***********************************************************************
+* spy_ls_eval_bp - determine dual objective function break-points
+*
+* This routine determines the dual objective function break-points.
+*
+* The parameters lp, d, r, trow, and tol_piv have the same meaning as
+* for the routine spx_chuzc_std (see above).
+*
+* The routine stores the break-points determined to the array elements
+* bp[1], ..., bp[nbp] in *arbitrary* order, where 0 <= nbp <= n-m is
+* the number of break-points returned by the routine on exit. */
+
+int spy_ls_eval_bp(SPXLP *lp, const double d[/*1+n-m*/],
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+ SPYBP bp[/*1+n-m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k, t, nnn, nbp;
+ double s, alfa, teta, teta_max;
+ xassert(r != 0.0);
+ s = (r > 0.0 ? +1.0 : -1.0);
+ /* build the list of all dual basic variables lambdaN[j] that
+ * can reach zero on increasing the ray parameter teta >= 0 */
+ nnn = 0, teta_max = DBL_MAX;
+ /* walk thru the list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ /* if xN[j] is fixed variable, skip it */
+ if (l[k] == u[k])
+ continue;
+ alfa = s * trow[j];
+ if (alfa >= +tol_piv && !flag[j])
+ { /* xN[j] is either free or has its lower bound active, so
+ * lambdaN[j] = d[j] >= 0 decreases down to zero */
+ /* determine teta[j] on which lambdaN[j] reaches zero */
+ teta = (d[j] < 0.0 ? 0.0 : d[j] / alfa);
+ /* if xN[j] has no upper bound, lambdaN[j] cannot become
+ * negative and thereby blocks further increasing teta */
+ if (u[k] == +DBL_MAX && teta_max > teta)
+ teta_max = teta;
+ }
+ else if (alfa <= -tol_piv && (l[k] == -DBL_MAX || flag[j]))
+ { /* xN[j] is either free or has its upper bound active, so
+ * lambdaN[j] = d[j] <= 0 increases up to zero */
+ /* determine teta[j] on which lambdaN[j] reaches zero */
+ teta = (d[j] > 0.0 ? 0.0 : d[j] / alfa);
+ /* if xN[j] has no lower bound, lambdaN[j] cannot become
+ * positive and thereby blocks further increasing teta */
+ if (l[k] == -DBL_MAX && teta_max > teta)
+ teta_max = teta;
+ }
+ else
+ { /* lambdaN[j] cannot reach zero on increasing teta */
+ continue;
+ }
+ /* add lambdaN[j] to the list */
+ nnn++;
+ bp[nnn].j = j;
+ bp[nnn].teta = teta;
+ }
+ /* remove from the list all dual basic variables lambdaN[j], for
+ * which teta[j] > teta_max */
+ nbp = 0;
+ for (t = 1; t <= nnn; t++)
+ { if (bp[t].teta <= teta_max + 1e-6)
+ { nbp++;
+ bp[nbp].j = bp[t].j;
+ bp[nbp].teta = bp[t].teta;
+ }
+ }
+ return nbp;
+}
+
+/***********************************************************************
+* spy_ls_select_bp - select and process dual objective break-points
+*
+* This routine selects a next portion of the dual objective function
+* break-points and processes them.
+*
+* On entry to the routine it is assumed that break-points bp[1], ...,
+* bp[num] are already processed, and slope is the dual objective slope
+* to the right of the last processed break-point bp[num]. (Initially,
+* when num = 0, slope should be specified as fabs(r), where r has the
+* same meaning as above.)
+*
+* The routine selects break-points among bp[num+1], ..., bp[nbp], for
+* which teta <= teta_lim, and moves these break-points to the array
+* elements bp[num+1], ..., bp[num1], where num <= num1 <= n-m is the
+* new number of processed break-points returned by the routine on
+* exit. Then the routine sorts these break-points by ascending teta
+* and computes the change of the dual objective function relative to
+* its value at teta = 0.
+*
+* On exit the routine also replaces the parameter slope with a new
+* value that corresponds to the new last break-point bp[num1]. */
+
+static int CDECL fcmp(const void *v1, const void *v2)
+{ const SPYBP *p1 = v1, *p2 = v2;
+ if (p1->teta < p2->teta)
+ return -1;
+ else if (p1->teta > p2->teta)
+ return +1;
+ else
+ return 0;
+}
+
+int spy_ls_select_bp(SPXLP *lp, const double trow[/*1+n-m*/],
+ int nbp, SPYBP bp[/*1+n-m*/], int num, double *slope, double
+ teta_lim)
+{ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int j, k, t, num1;
+ double teta, dz;
+ xassert(0 <= num && num <= nbp && nbp <= n-m);
+ /* select a new portion of break-points */
+ num1 = num;
+ for (t = num+1; t <= nbp; t++)
+ { if (bp[t].teta <= teta_lim)
+ { /* move break-point to the beginning of the new portion */
+ num1++;
+ j = bp[num1].j, teta = bp[num1].teta;
+ bp[num1].j = bp[t].j, bp[num1].teta = bp[t].teta;
+ bp[t].j = j, bp[t].teta = teta;
+ }
+ }
+ /* sort new break-points bp[num+1], ..., bp[num1] by ascending
+ * the ray parameter teta */
+ if (num1 - num > 1)
+ qsort(&bp[num+1], num1 - num, sizeof(SPYBP), fcmp);
+ /* calculate the dual objective change at the new break-points */
+ for (t = num+1; t <= num1; t++)
+ { /* calculate the dual objective change relative to its value
+ * at break-point bp[t-1] */
+ if (*slope == -DBL_MAX)
+ dz = -DBL_MAX;
+ else
+ dz = (*slope) *
+ (bp[t].teta - (t == 1 ? 0.0 : bp[t-1].teta));
+ /* calculate the dual objective change relative to its value
+ * at teta = 0 */
+ if (dz == -DBL_MAX)
+ bp[t].dz = -DBL_MAX;
+ else
+ bp[t].dz = (t == 1 ? 0.0 : bp[t-1].dz) + dz;
+ /* calculate a new slope of the dual objective to the right of
+ * the current break-point bp[t] */
+ if (*slope != -DBL_MAX)
+ { j = bp[t].j;
+ k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == -DBL_MAX || u[k] == +DBL_MAX)
+ *slope = -DBL_MAX; /* blocking break-point reached */
+ else
+ { xassert(l[k] < u[k]);
+ *slope -= fabs(trow[j]) * (u[k] - l[k]);
+ }
+ }
+ }
+ return num1;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spychuzc.h b/test/monniaux/glpk-4.65/src/simplex/spychuzc.h
new file mode 100644
index 00000000..8aa45a07
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spychuzc.h
@@ -0,0 +1,85 @@
+/* spychuzc.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2016 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPYCHUZC_H
+#define SPYCHUZC_H
+
+#include "spxlp.h"
+
+#define spy_chuzc_std _glp_spy_chuzc_std
+int spy_chuzc_std(SPXLP *lp, const double d[/*1+n-m*/],
+#if 0 /* 14/III-2016 */
+ double s, const double trow[/*1+n-m*/], double tol_piv,
+#else
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+#endif
+ double tol, double tol1);
+/* choose non-basic variable (dual textbook ratio test) */
+
+#define spy_chuzc_harris _glp_spy_chuzc_harris
+int spy_chuzc_harris(SPXLP *lp, const double d[/*1+n-m*/],
+#if 0 /* 14/III-2016 */
+ double s, const double trow[/*1+n-m*/], double tol_piv,
+#else
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+#endif
+ double tol, double tol1);
+/* choose non-basic variable (dual Harris' ratio test) */
+
+typedef struct SPYBP SPYBP;
+
+struct SPYBP
+{ /* dual objective function break point */
+ int j;
+ /* dual basic variable lambdaN[j], 1 <= j <= n-m, that intersects
+ * zero at this break point */
+ double teta;
+ /* ray parameter value, teta[j] >= 0, at this break point */
+ double dz;
+ /* increment, zeta[j] - zeta[0], of the dual objective function
+ * at this break point */
+};
+
+#if 0 /* 23/III-2016 */
+#define spy_eval_bp _glp_spy_eval_bp
+int spy_eval_bp(SPXLP *lp, const double d[/*1+n-m*/],
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+ SPYBP bp[/*1+n-m*/]);
+/* determine dual objective function break-points */
+#endif
+
+#define spy_ls_eval_bp _glp_spy_ls_eval_bp
+int spy_ls_eval_bp(SPXLP *lp, const double d[/*1+n-m*/],
+ double r, const double trow[/*1+n-m*/], double tol_piv,
+ SPYBP bp[/*1+n-m*/]);
+/* determine dual objective function break-points */
+
+#define spy_ls_select_bp _glp_spy_ls_select_bp
+int spy_ls_select_bp(SPXLP *lp, const double trow[/*1+n-m*/],
+ int nbp, SPYBP bp[/*1+n-m*/], int num, double *slope, double
+ teta_lim);
+/* select and process dual objective break-points */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spychuzr.c b/test/monniaux/glpk-4.65/src/simplex/spychuzr.c
new file mode 100644
index 00000000..63079c17
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spychuzr.c
@@ -0,0 +1,483 @@
+/* spychuzr.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#include "env.h"
+#include "spychuzr.h"
+
+/***********************************************************************
+* spy_chuzr_sel - select eligible basic variables
+*
+* This routine selects eligible basic variables xB[i], whose value
+* beta[i] violates corresponding lower lB[i] or upper uB[i] bound.
+* Positive bound violation rp[i] = lb[i] - beta[i] > 0 is the reduced
+* cost of non-basic dual variable lambda^+B[i] >= 0, so increasing it
+* increases the dual objective. Similarly, negative bound violation
+* rn[i] = ub[i] - beta[i] < 0 is the reduced cost of non-basic dual
+* variable lambda^-B[i] <= 0, so decreasing it also increases the dual
+* objective.
+*
+* Current values of basic variables should be placed in the array
+* locations beta[1], ..., beta[m].
+*
+* Basic variable xB[i] is considered eligible, if:
+*
+* beta[i] <= lB[i] - eps1[i], or
+*
+* beta[i] >= uB[i] + eps2[i],
+*
+* for
+*
+* eps1[i] = tol + tol1 * |lB[i]|,
+*
+* eps2[i] = tol + tol2 * |uB[i]|,
+*
+* where lB[i] and uB[i] are, resp., lower and upper bounds of xB[i],
+* tol and tol1 are specified tolerances.
+*
+* On exit the routine stores indices i of eligible basic variables
+* xB[i] to the array locations list[1], ..., list[num] and returns the
+* number of such variables 0 <= num <= m. (If the parameter list is
+* specified as NULL, no indices are stored.) */
+
+int spy_chuzr_sel(SPXLP *lp, const double beta[/*1+m*/], double tol,
+ double tol1, int list[/*1+m*/])
+{ int m = lp->m;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, k, num;
+ double lk, uk, eps;
+ num = 0;
+ /* walk thru list of basic variables */
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ lk = l[k], uk = u[k];
+ /* check if xB[i] is eligible */
+ if (beta[i] < lk)
+ { /* determine absolute tolerance eps1[i] */
+ eps = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] < lk - eps)
+ { /* lower bound is violated */
+ num++;
+ if (list != NULL)
+ list[num] = i;
+ }
+ }
+ else if (beta[i] > uk)
+ { /* determine absolute tolerance eps2[i] */
+ eps = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] > uk + eps)
+ { /* upper bound is violated */
+ num++;
+ if (list != NULL)
+ list[num] = i;
+ }
+ }
+ }
+ return num;
+}
+
+/***********************************************************************
+* spy_chuzr_std - choose basic variable (dual Dantzig's rule)
+*
+* This routine chooses most eligible basic variable xB[p] according
+* to dual Dantzig's ("standard") rule:
+*
+* r[p] = max |r[i]|,
+* i in I
+*
+* ( lB[i] - beta[i], if beta[i] < lB[i]
+* (
+* r[i] = { 0, if lB[i] <= beta[i] <= uB[i]
+* (
+* ( uB[i] - beta[i], if beta[i] > uB[i]
+*
+* where I <= {1, ..., m} is the set of indices of eligible basic
+* variables, beta[i] is current value of xB[i], lB[i] and uB[i] are,
+* resp., lower and upper bounds of xB[i], r[i] is bound violation.
+*
+* Current values of basic variables should be placed in the array
+* locations beta[1], ..., beta[m].
+*
+* Indices of eligible basic variables i in I should be placed in the
+* array locations list[1], ..., list[num], where num = |J| > 0 is the
+* total number of such variables.
+*
+* On exit the routine returns p, the index of the basic variable xB[p]
+* chosen. */
+
+int spy_chuzr_std(SPXLP *lp, const double beta[/*1+m*/], int num,
+ const int list[])
+{ int m = lp->m;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int i, k, p, t;
+ double abs_ri, abs_rp;
+ xassert(0 < num && num <= m);
+ p = 0, abs_rp = -1.0;
+ for (t = 1; t <= num; t++)
+ { i = list[t];
+ k = head[i]; /* x[k] = xB[i] */
+ if (beta[i] < l[k])
+ abs_ri = l[k] - beta[i];
+ else if (beta[i] > u[k])
+ abs_ri = beta[i] - u[k];
+ else
+ xassert(t != t);
+ if (abs_rp < abs_ri)
+ p = i, abs_rp = abs_ri;
+ }
+ xassert(p != 0);
+ return p;
+}
+
+/***********************************************************************
+* spy_alloc_se - allocate dual pricing data block
+*
+* This routine allocates the memory for arrays used in the dual
+* pricing data block. */
+
+void spy_alloc_se(SPXLP *lp, SPYSE *se)
+{ int m = lp->m;
+ int n = lp->n;
+#if 1 /* 30/III-2016 */
+ int i;
+#endif
+ se->valid = 0;
+ se->refsp = talloc(1+n, char);
+ se->gamma = talloc(1+m, double);
+ se->work = talloc(1+m, double);
+#if 1 /* 30/III-2016 */
+ se->u.n = m;
+ se->u.nnz = 0;
+ se->u.ind = talloc(1+m, int);
+ se->u.vec = talloc(1+m, double);
+ for (i = 1; i <= m; i++)
+ se->u.vec[i] = 0.0;
+#endif
+ return;
+}
+
+/***********************************************************************
+* spy_reset_refsp - reset dual reference space
+*
+* This routine resets (re-initializes) the dual reference space
+* composing it from dual variables which are non-basic (corresponding
+* to basic primal variables) in the current basis, and sets all
+* weights gamma[i] to 1. */
+
+void spy_reset_refsp(SPXLP *lp, SPYSE *se)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *gamma = se->gamma;
+ int i, k;
+ se->valid = 1;
+ memset(&refsp[1], 0, n * sizeof(char));
+ for (i = 1; i <= m; i++)
+ { k = head[i]; /* x[k] = xB[i] */
+ refsp[k] = 1;
+ gamma[i] = 1.0;
+ }
+ return;
+}
+
+/***********************************************************************
+* spy_eval_gamma_i - compute dual proj. steepest edge weight directly
+*
+* This routine computes dual projected steepest edge weight gamma[i],
+* 1 <= i <= m, for the current basis directly with the formula:
+*
+* n-m
+* gamma[i] = delta[i] + sum eta[j] * T[i,j]**2,
+* j=1
+*
+* where T[i,j] is element of the current simplex table, and
+*
+* ( 1, if lambdaN[j] is in the reference space
+* eta[j] = {
+* ( 0, otherwise
+*
+* ( 1, if lambdaB[i] is in the reference space
+* delta[i] = {
+* ( 0, otherwise
+*
+* Dual basic variable lambdaN[j] corresponds to primal non-basic
+* variable xN[j], and dual non-basic variable lambdaB[j] corresponds
+* to primal basic variable xB[i].
+*
+* NOTE: For testing/debugging only. */
+
+double spy_eval_gamma_i(SPXLP *lp, SPYSE *se, int i)
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *rho = se->work;
+ int j, k;
+ double gamma_i, t_ij;
+ xassert(se->valid);
+ xassert(1 <= i && i <= m);
+ k = head[i]; /* x[k] = xB[i] */
+ gamma_i = (refsp[k] ? 1.0 : 0.0);
+ spx_eval_rho(lp, i, rho);
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (refsp[k])
+ { t_ij = spx_eval_tij(lp, rho, j);
+ gamma_i += t_ij * t_ij;
+ }
+ }
+ return gamma_i;
+}
+
+/***********************************************************************
+* spy_chuzr_pse - choose basic variable (dual projected steepest edge)
+*
+* This routine chooses most eligible basic variable xB[p] according
+* to the dual projected steepest edge method:
+*
+* r[p]**2 r[i]**2
+* -------- = max -------- ,
+* gamma[p] i in I gamma[i]
+*
+* ( lB[i] - beta[i], if beta[i] < lB[i]
+* (
+* r[i] = { 0, if lB[i] <= beta[i] <= uB[i]
+* (
+* ( uB[i] - beta[i], if beta[i] > uB[i]
+*
+* where I <= {1, ..., m} is the set of indices of eligible basic
+* variables, beta[i] is current value of xB[i], lB[i] and uB[i] are,
+* resp., lower and upper bounds of xB[i], r[i] is bound violation.
+*
+* Current values of basic variables should be placed in the array
+* locations beta[1], ..., beta[m].
+*
+* Indices of eligible basic variables i in I should be placed in the
+* array locations list[1], ..., list[num], where num = |J| > 0 is the
+* total number of such variables.
+*
+* On exit the routine returns p, the index of the basic variable xB[p]
+* chosen. */
+
+int spy_chuzr_pse(SPXLP *lp, SPYSE *se, const double beta[/*1+m*/],
+ int num, const int list[])
+{ int m = lp->m;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ double *gamma = se->gamma;
+ int i, k, p, t;
+ double best, ri, temp;
+ xassert(0 < num && num <= m);
+ p = 0, best = -1.0;
+ for (t = 1; t <= num; t++)
+ { i = list[t];
+ k = head[i]; /* x[k] = xB[i] */
+ if (beta[i] < l[k])
+ ri = l[k] - beta[i];
+ else if (beta[i] > u[k])
+ ri = u[k] - beta[i];
+ else
+ xassert(t != t);
+ /* FIXME */
+ if (gamma[i] < DBL_EPSILON)
+ temp = 0.0;
+ else
+ temp = (ri * ri) / gamma[i];
+ if (best < temp)
+ p = i, best = temp;
+ }
+ xassert(p != 0);
+ return p;
+}
+
+/***********************************************************************
+* spy_update_gamma - update dual proj. steepest edge weights exactly
+*
+* This routine updates the vector gamma = (gamma[i]) of dual projected
+* steepest edge weights exactly, for the adjacent basis.
+*
+* On entry to the routine the content of the se object should be valid
+* and should correspond to the current basis.
+*
+* The parameter 1 <= p <= m specifies basic variable xB[p] which
+* becomes non-basic variable xN[q] in the adjacent basis.
+*
+* The parameter 1 <= q <= n-m specified non-basic variable xN[q] which
+* becomes basic variable xB[p] in the adjacent basis.
+*
+* It is assumed that the array trow contains elements of p-th (pivot)
+* row T'[p] of the simplex table in locations trow[1], ..., trow[n-m].
+* It is also assumed that the array tcol contains elements of q-th
+* (pivot) column T[q] of the simple table in locations tcol[1], ...,
+* tcol[m]. (These row and column should be computed for the current
+* basis.)
+*
+* For details about the formulae used see the program documentation.
+*
+* The routine also computes the relative error:
+*
+* e = |gamma[p] - gamma'[p]| / (1 + |gamma[p]|),
+*
+* where gamma'[p] is the weight for lambdaB[p] (which is dual
+* non-basic variable corresponding to xB[p]) on entry to the routine,
+* and returns e on exit. (If e happens to be large enough, the calling
+* program may reset the reference space, since other weights also may
+* be inaccurate.) */
+
+double spy_update_gamma(SPXLP *lp, SPYSE *se, int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/])
+{ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *gamma = se->gamma;
+ double *u = se->work;
+ int i, j, k, ptr, end;
+ double gamma_p, delta_p, e, r, t1, t2;
+ xassert(se->valid);
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* compute gamma[p] in current basis more accurately; also
+ * compute auxiliary vector u */
+ k = head[p]; /* x[k] = xB[p] */
+ gamma_p = delta_p = (refsp[k] ? 1.0 : 0.0);
+ for (i = 1; i <= m; i++)
+ u[i] = 0.0;
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (refsp[k] && trow[j] != 0.0)
+ { gamma_p += trow[j] * trow[j];
+ /* u := u + T[p,j] * N[j], where N[j] = A[k] is constraint
+ * matrix column corresponding to xN[j] */
+ ptr = lp->A_ptr[k];
+ end = lp->A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ u[lp->A_ind[ptr]] += trow[j] * lp->A_val[ptr];
+ }
+ }
+ bfd_ftran(lp->bfd, u);
+ /* compute relative error in gamma[p] */
+ e = fabs(gamma_p - gamma[p]) / (1.0 + gamma_p);
+ /* compute new gamma[p] */
+ gamma[p] = gamma_p / (tcol[p] * tcol[p]);
+ /* compute new gamma[i] for all i != p */
+ for (i = 1; i <= m; i++)
+ { if (i == p)
+ continue;
+ /* compute r[i] = T[i,q] / T[p,q] */
+ r = tcol[i] / tcol[p];
+ /* compute new gamma[i] */
+ t1 = gamma[i] + r * (r * gamma_p + u[i] + u[i]);
+ k = head[i]; /* x[k] = xB[i] */
+ t2 = (refsp[k] ? 1.0 : 0.0) + delta_p * r * r;
+ gamma[i] = (t1 >= t2 ? t1 : t2);
+ }
+ return e;
+}
+
+#if 1 /* 30/III-2016 */
+double spy_update_gamma_s(SPXLP *lp, SPYSE *se, int p, int q,
+ const FVS *trow, const FVS *tcol)
+{ /* sparse version of spy_update_gamma */
+ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *refsp = se->refsp;
+ double *gamma = se->gamma;
+ double *u = se->work;
+ int trow_nnz = trow->nnz;
+ int *trow_ind = trow->ind;
+ double *trow_vec = trow->vec;
+ int tcol_nnz = tcol->nnz;
+ int *tcol_ind = tcol->ind;
+ double *tcol_vec = tcol->vec;
+ int i, j, k, t, ptr, end;
+ double gamma_p, delta_p, e, r, t1, t2;
+ xassert(se->valid);
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ /* compute gamma[p] in current basis more accurately; also
+ * compute auxiliary vector u */
+ k = head[p]; /* x[k] = xB[p] */
+ gamma_p = delta_p = (refsp[k] ? 1.0 : 0.0);
+ for (i = 1; i <= m; i++)
+ u[i] = 0.0;
+ for (t = 1; t <= trow_nnz; t++)
+ { j = trow_ind[t];
+ k = head[m+j]; /* x[k] = xN[j] */
+ if (refsp[k])
+ { gamma_p += trow_vec[j] * trow_vec[j];
+ /* u := u + T[p,j] * N[j], where N[j] = A[k] is constraint
+ * matrix column corresponding to xN[j] */
+ ptr = lp->A_ptr[k];
+ end = lp->A_ptr[k+1];
+ for (; ptr < end; ptr++)
+ u[lp->A_ind[ptr]] += trow_vec[j] * lp->A_val[ptr];
+ }
+ }
+ bfd_ftran(lp->bfd, u);
+ /* compute relative error in gamma[p] */
+ e = fabs(gamma_p - gamma[p]) / (1.0 + gamma_p);
+ /* compute new gamma[p] */
+ gamma[p] = gamma_p / (tcol_vec[p] * tcol_vec[p]);
+ /* compute new gamma[i] for all i != p */
+ for (t = 1; t <= tcol_nnz; t++)
+ { i = tcol_ind[t];
+ if (i == p)
+ continue;
+ /* compute r[i] = T[i,q] / T[p,q] */
+ r = tcol_vec[i] / tcol_vec[p];
+ /* compute new gamma[i] */
+ t1 = gamma[i] + r * (r * gamma_p + u[i] + u[i]);
+ k = head[i]; /* x[k] = xB[i] */
+ t2 = (refsp[k] ? 1.0 : 0.0) + delta_p * r * r;
+ gamma[i] = (t1 >= t2 ? t1 : t2);
+ }
+ return e;
+}
+#endif
+
+/***********************************************************************
+* spy_free_se - deallocate dual pricing data block
+*
+* This routine deallocates the memory used for arrays in the dual
+* pricing data block. */
+
+void spy_free_se(SPXLP *lp, SPYSE *se)
+{ xassert(lp == lp);
+ tfree(se->refsp);
+ tfree(se->gamma);
+ tfree(se->work);
+#if 1 /* 30/III-2016 */
+ tfree(se->u.ind);
+ tfree(se->u.vec);
+#endif
+ return;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spychuzr.h b/test/monniaux/glpk-4.65/src/simplex/spychuzr.h
new file mode 100644
index 00000000..31f01b78
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spychuzr.h
@@ -0,0 +1,97 @@
+/* spychuzr.h */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#ifndef SPYCHUZR_H
+#define SPYCHUZR_H
+
+#include "spxlp.h"
+
+#define spy_chuzr_sel _glp_spy_chuzr_sel
+int spy_chuzr_sel(SPXLP *lp, const double beta[/*1+m*/], double tol,
+ double tol1, int list[/*1+m*/]);
+/* select eligible basic variables */
+
+#define spy_chuzr_std _glp_spy_chuzr_std
+int spy_chuzr_std(SPXLP *lp, const double beta[/*1+m*/], int num,
+ const int list[]);
+/* choose basic variable (dual Dantzig's rule) */
+
+typedef struct SPYSE SPYSE;
+
+struct SPYSE
+{ /* dual projected steepest edge and Devex pricing data block */
+ int valid;
+ /* content validity flag */
+ char *refsp; /* char refsp[1+n]; */
+ /* refsp[0] is not used;
+ * refsp[k], 1 <= k <= n, is the flag meaning that dual variable
+ * lambda[k] is in the dual reference space */
+ double *gamma; /* double gamma[1+m]; */
+ /* gamma[0] is not used;
+ * gamma[i], 1 <= i <= m, is the weight for reduced cost r[i]
+ * of dual non-basic variable lambdaB[j] in the current basis
+ * (r[i] is bound violation for basic variable xB[i]) */
+ double *work; /* double work[1+m]; */
+ /* working array */
+#if 1 /* 30/III-2016 */
+ FVS u; /* FVS u[1:m]; */
+ /* working vector */
+#endif
+};
+
+#define spy_alloc_se _glp_spy_alloc_se
+void spy_alloc_se(SPXLP *lp, SPYSE *se);
+/* allocate dual pricing data block */
+
+#define spy_reset_refsp _glp_spy_reset_refsp
+void spy_reset_refsp(SPXLP *lp, SPYSE *se);
+/* reset dual reference space */
+
+#define spy_eval_gamma_i _glp_spy_eval_gamma_i
+double spy_eval_gamma_i(SPXLP *lp, SPYSE *se, int i);
+/* compute dual projected steepest edge weight directly */
+
+#define spy_chuzr_pse _glp_spy_chuzr_pse
+int spy_chuzr_pse(SPXLP *lp, SPYSE *se, const double beta[/*1+m*/],
+ int num, const int list[]);
+/* choose basic variable (dual projected steepest edge) */
+
+#define spy_update_gamma _glp_spy_update_gamma
+double spy_update_gamma(SPXLP *lp, SPYSE *se, int p, int q,
+ const double trow[/*1+n-m*/], const double tcol[/*1+m*/]);
+/* update dual projected steepest edge weights exactly */
+
+#if 1 /* 30/III-2016 */
+#define spy_update_gamma_s _glp_spy_update_gamma_s
+double spy_update_gamma_s(SPXLP *lp, SPYSE *se, int p, int q,
+ const FVS *trow, const FVS *tcol);
+/* sparse version of spy_update_gamma */
+#endif
+
+#define spy_free_se _glp_spy_free_se
+void spy_free_se(SPXLP *lp, SPYSE *se);
+/* deallocate dual pricing data block */
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/simplex/spydual.c b/test/monniaux/glpk-4.65/src/simplex/spydual.c
new file mode 100644
index 00000000..89d98db9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/simplex/spydual.c
@@ -0,0 +1,2101 @@
+/* spydual.c */
+
+/***********************************************************************
+* This code is part of GLPK (GNU Linear Programming Kit).
+*
+* Copyright (C) 2015-2017 Andrew Makhorin, Department for Applied
+* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
+* reserved. E-mail: <mao@gnu.org>.
+*
+* GLPK is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* GLPK is distributed in the hope that it will be useful, but WITHOUT
+* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+* License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
+***********************************************************************/
+
+#if 1 /* 18/VII-2017 */
+#define SCALE_Z 1
+#endif
+
+#include "env.h"
+#include "simplex.h"
+#include "spxat.h"
+#include "spxnt.h"
+#include "spxprob.h"
+#include "spychuzc.h"
+#include "spychuzr.h"
+#if 0 /* 11/VI-2017 */
+#if 1 /* 29/III-2016 */
+#include "fvs.h"
+#endif
+#endif
+
+#define CHECK_ACCURACY 0
+/* (for debugging) */
+
+struct csa
+{ /* common storage area */
+ SPXLP *lp;
+ /* LP problem data and its (current) basis; this LP has m rows
+ * and n columns */
+ int dir;
+ /* original optimization direction:
+ * +1 - minimization
+ * -1 - maximization */
+#if SCALE_Z
+ double fz;
+ /* factor used to scale original objective */
+#endif
+ double *orig_b; /* double orig_b[1+m]; */
+ /* copy of original right-hand sides */
+ double *orig_c; /* double orig_c[1+n]; */
+ /* copy of original objective coefficients */
+ double *orig_l; /* double orig_l[1+n]; */
+ /* copy of original lower bounds */
+ double *orig_u; /* double orig_u[1+n]; */
+ /* copy of original upper bounds */
+ SPXAT *at;
+ /* mxn-matrix A of constraint coefficients, in sparse row-wise
+ * format (NULL if not used) */
+ SPXNT *nt;
+ /* mx(n-m)-matrix N composed of non-basic columns of constraint
+ * matrix A, in sparse row-wise format (NULL if not used) */
+ int phase;
+ /* search phase:
+ * 0 - not determined yet
+ * 1 - searching for dual feasible solution
+ * 2 - searching for optimal solution */
+ double *beta; /* double beta[1+m]; */
+ /* beta[i] is primal value of basic variable xB[i] */
+ int beta_st;
+ /* status of the vector beta:
+ * 0 - undefined
+ * 1 - just computed
+ * 2 - updated */
+ double *d; /* double d[1+n-m]; */
+ /* d[j] is reduced cost of non-basic variable xN[j] */
+ int d_st;
+ /* status of the vector d:
+ * 0 - undefined
+ * 1 - just computed
+ * 2 - updated */
+ SPYSE *se;
+ /* dual projected steepest edge and Devex pricing data block
+ * (NULL if not used) */
+#if 0 /* 30/III-2016 */
+ int num;
+ /* number of eligible basic variables */
+ int *list; /* int list[1+m]; */
+ /* list[1], ..., list[num] are indices i of eligible basic
+ * variables xB[i] */
+#else
+ FVS r; /* FVS r[1:m]; */
+ /* vector of primal infeasibilities */
+ /* r->nnz = num; r->ind = list */
+ /* vector r has the same status as vector beta (see above) */
+#endif
+ int p;
+ /* xB[p] is a basic variable chosen to leave the basis */
+#if 0 /* 29/III-2016 */
+ double *trow; /* double trow[1+n-m]; */
+#else
+ FVS trow; /* FVS trow[1:n-m]; */
+#endif
+ /* p-th (pivot) row of the simplex table */
+#if 1 /* 16/III-2016 */
+ SPYBP *bp; /* SPYBP bp[1+n-m]; */
+ /* dual objective break-points */
+#endif
+ int q;
+ /* xN[q] is a non-basic variable chosen to enter the basis */
+#if 0 /* 29/III-2016 */
+ double *tcol; /* double tcol[1+m]; */
+#else
+ FVS tcol; /* FVS tcol[1:m]; */
+#endif
+ /* q-th (pivot) column of the simplex table */
+ double *work; /* double work[1+m]; */
+ /* working array */
+ double *work1; /* double work1[1+n-m]; */
+ /* another working array */
+#if 0 /* 11/VI-2017 */
+#if 1 /* 31/III-2016 */
+ FVS wrow; /* FVS wrow[1:n-m]; */
+ FVS wcol; /* FVS wcol[1:m]; */
+ /* working sparse vectors */
+#endif
+#endif
+ int p_stat, d_stat;
+ /* primal and dual solution statuses */
+ /*--------------------------------------------------------------*/
+ /* control parameters (see struct glp_smcp) */
+ int msg_lev;
+ /* message level */
+ int dualp;
+ /* if this flag is set, report failure in case of instability */
+#if 0 /* 16/III-2016 */
+ int harris;
+ /* dual ratio test technique:
+ * 0 - textbook ratio test
+ * 1 - Harris' two pass ratio test */
+#else
+ int r_test;
+ /* dual ratio test technique:
+ * GLP_RT_STD - textbook ratio test
+ * GLP_RT_HAR - Harris' two pass ratio test
+ * GLP_RT_FLIP - long-step (flip-flop) ratio test */
+#endif
+ double tol_bnd, tol_bnd1;
+ /* primal feasibility tolerances */
+ double tol_dj, tol_dj1;
+ /* dual feasibility tolerances */
+ double tol_piv;
+ /* pivot tolerance */
+ double obj_lim;
+ /* objective limit */
+ int it_lim;
+ /* iteration limit */
+ int tm_lim;
+ /* time limit, milliseconds */
+ int out_frq;
+#if 0 /* 15/VII-2017 */
+ /* display output frequency, iterations */
+#else
+ /* display output frequency, milliseconds */
+#endif
+ int out_dly;
+ /* display output delay, milliseconds */
+ /*--------------------------------------------------------------*/
+ /* working parameters */
+ double tm_beg;
+ /* time value at the beginning of the search */
+ int it_beg;
+ /* simplex iteration count at the beginning of the search */
+ int it_cnt;
+ /* simplex iteration count; it increases by one every time the
+ * basis changes */
+ int it_dpy;
+ /* simplex iteration count at most recent display output */
+#if 1 /* 15/VII-2017 */
+ double tm_dpy;
+ /* time value at most recent display output */
+#endif
+ int inv_cnt;
+ /* basis factorization count since most recent display output */
+#if 1 /* 11/VII-2017 */
+ int degen;
+ /* count of successive degenerate iterations; this count is used
+ * to detect stalling */
+#endif
+#if 1 /* 23/III-2016 */
+ int ns_cnt, ls_cnt;
+ /* normal and long-step iteration count */
+#endif
+};
+
+/***********************************************************************
+* check_flags - check correctness of active bound flags
+*
+* This routine checks that flags specifying active bounds of all
+* non-basic variables are correct.
+*
+* NOTE: It is important to note that if bounds of variables have been
+* changed, active bound flags should be corrected accordingly. */
+
+static void check_flags(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ int j, k;
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == -DBL_MAX && u[k] == +DBL_MAX)
+ xassert(!flag[j]);
+ else if (l[k] != -DBL_MAX && u[k] == +DBL_MAX)
+ xassert(!flag[j]);
+ else if (l[k] == -DBL_MAX && u[k] != +DBL_MAX)
+ xassert(flag[j]);
+ else if (l[k] == u[k])
+ xassert(!flag[j]);
+ }
+ return;
+}
+
+/***********************************************************************
+* set_art_bounds - set artificial right-hand sides and bounds
+*
+* This routine sets artificial right-hand sides and artificial bounds
+* for all variables to minimize the sum of dual infeasibilities on
+* phase I. Given current reduced costs d = (d[j]) this routine also
+* sets active artificial bounds of non-basic variables to provide dual
+* feasibility (this is always possible because all variables have both
+* lower and upper artificial bounds). */
+
+static void set_art_bounds(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *b = lp->b;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *d = csa->d;
+ int i, j, k;
+#if 1 /* 31/III-2016: FIXME */
+ /* set artificial right-hand sides */
+ for (i = 1; i <= m; i++)
+ b[i] = 0.0;
+ /* set artificial bounds depending on types of variables */
+ for (k = 1; k <= n; k++)
+ { if (csa->orig_l[k] == -DBL_MAX && csa->orig_u[k] == +DBL_MAX)
+ { /* force free variables to enter the basis */
+ l[k] = -1e3, u[k] = +1e3;
+ }
+ else if (csa->orig_l[k] != -DBL_MAX && csa->orig_u[k] == +DBL_MAX)
+ l[k] = 0.0, u[k] = +1.0;
+ else if (csa->orig_l[k] == -DBL_MAX && csa->orig_u[k] != +DBL_MAX)
+ l[k] = -1.0, u[k] = 0.0;
+ else
+ l[k] = u[k] = 0.0;
+ }
+#endif
+ /* set active artificial bounds for non-basic variables */
+ xassert(csa->d_st == 1);
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ flag[j] = (l[k] != u[k] && d[j] < 0.0);
+ }
+ /* invalidate values of basic variables, since active bounds of
+ * non-basic variables have been changed */
+ csa->beta_st = 0;
+ return;
+}
+
+/***********************************************************************
+* set_orig_bounds - restore original right-hand sides and bounds
+*
+* This routine restores original right-hand sides and original bounds
+* for all variables. This routine also sets active original bounds for
+* non-basic variables; for double-bounded non-basic variables current
+* reduced costs d = (d[j]) are used to decide which bound (lower or
+* upper) should be made active. */
+
+static void set_orig_bounds(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *b = lp->b;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *d = csa->d;
+ int j, k;
+ /* restore original right-hand sides */
+ memcpy(b, csa->orig_b, (1+m) * sizeof(double));
+ /* restore original bounds of all variables */
+ memcpy(l, csa->orig_l, (1+n) * sizeof(double));
+ memcpy(u, csa->orig_u, (1+n) * sizeof(double));
+ /* set active original bounds for non-basic variables */
+ xassert(csa->d_st == 1);
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == -DBL_MAX && u[k] == +DBL_MAX)
+ flag[j] = 0;
+ else if (l[k] != -DBL_MAX && u[k] == +DBL_MAX)
+ flag[j] = 0;
+ else if (l[k] == -DBL_MAX && u[k] != +DBL_MAX)
+ flag[j] = 1;
+ else if (l[k] != u[k])
+ flag[j] = (d[j] < 0.0);
+ else
+ flag[j] = 0;
+ }
+ /* invalidate values of basic variables, since active bounds of
+ * non-basic variables have been changed */
+ csa->beta_st = 0;
+ return;
+}
+
+/***********************************************************************
+* check_feas - check dual feasibility of basic solution
+*
+* This routine checks that reduced costs of all non-basic variables
+* d = (d[j]) have correct signs.
+*
+* Reduced cost d[j] is considered as having correct sign within the
+* specified tolerance depending on status of non-basic variable xN[j]
+* if one of the following conditions is met:
+*
+* xN[j] is free -eps <= d[j] <= +eps
+*
+* xN[j] has its lower bound active d[j] >= -eps
+*
+* xN[j] has its upper bound active d[j] <= +eps
+*
+* xN[j] is fixed d[j] has any value
+*
+* where eps = tol + tol1 * |cN[j]|, cN[j] is the objective coefficient
+* at xN[j]. (See also the routine spx_chuzc_sel.)
+*
+* The flag recov allows the routine to recover dual feasibility by
+* changing active bounds of non-basic variables. (For example, if
+* xN[j] has its lower bound active and d[j] < -eps, the feasibility
+* can be recovered by making xN[j] active on its upper bound.)
+*
+* If the basic solution is dual feasible, the routine returns zero.
+* If the basic solution is dual infeasible, but its dual feasibility
+* can be recovered (or has been recovered, if the flag recov is set),
+* the routine returns a negative value. Otherwise, the routine returns
+* the number j of some non-basic variable xN[j], whose reduced cost
+* d[j] is dual infeasible and cannot be recovered. */
+
+static int check_feas(struct csa *csa, double tol, double tol1,
+ int recov)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *d = csa->d;
+ int j, k, ret = 0;
+ double eps;
+ /* reduced costs should be just computed */
+ xassert(csa->d_st == 1);
+ /* walk thru list of non-basic variables */
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == u[k])
+ { /* xN[j] is fixed variable; skip it */
+ continue;
+ }
+ /* determine absolute tolerance eps[j] */
+ eps = tol + tol1 * (c[k] >= 0.0 ? +c[k] : -c[k]);
+ /* check dual feasibility of xN[j] */
+ if (d[j] > +eps)
+ { /* xN[j] should have its lower bound active */
+ if (l[k] == -DBL_MAX || flag[j])
+ { /* but it either has no lower bound or its lower bound
+ * is inactive */
+ if (l[k] == -DBL_MAX)
+ { /* cannot recover, since xN[j] has no lower bound */
+ ret = j;
+ break;
+ }
+ /* recovering is possible */
+ if (recov)
+ flag[j] = 0;
+ ret = -1;
+ }
+ }
+ else if (d[j] < -eps)
+ { /* xN[j] should have its upper bound active */
+ if (!flag[j])
+ { /* but it either has no upper bound or its upper bound
+ * is inactive */
+ if (u[k] == +DBL_MAX)
+ { /* cannot recover, since xN[j] has no upper bound */
+ ret = j;
+ break;
+ }
+ /* recovering is possible */
+ if (recov)
+ flag[j] = 1;
+ ret = -1;
+ }
+ }
+ }
+ if (recov && ret)
+ { /* invalidate values of basic variables, since active bounds
+ * of non-basic variables have been changed */
+ csa->beta_st = 0;
+ }
+ return ret;
+}
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_vec - compute maximal relative error between two vectors
+*
+* This routine computes and returns maximal relative error between
+* n-vectors x and y:
+*
+* err_max = max |x[i] - y[i]| / (1 + |x[i]|).
+*
+* NOTE: This routine is intended only for debugging purposes. */
+
+static double err_in_vec(int n, const double x[], const double y[])
+{ int i;
+ double err, err_max;
+ err_max = 0.0;
+ for (i = 1; i <= n; i++)
+ { err = fabs(x[i] - y[i]) / (1.0 + fabs(x[i]));
+ if (err_max < err)
+ err_max = err;
+ }
+ return err_max;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_beta - compute maximal relative error in vector beta
+*
+* This routine computes and returns maximal relative error in vector
+* of values of basic variables beta = (beta[i]).
+*
+* NOTE: This routine is intended only for debugging purposes. */
+
+static double err_in_beta(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ double err, *beta;
+ beta = talloc(1+m, double);
+ spx_eval_beta(lp, beta);
+ err = err_in_vec(m, beta, csa->beta);
+ tfree(beta);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+static double err_in_r(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int i, k;
+ double err, *r;
+ r = talloc(1+m, double);
+ for (i = 1; i <= m; i++)
+ { k = lp->head[i];
+ if (csa->beta[i] < lp->l[k])
+ r[i] = lp->l[k] - csa->beta[i];
+ else if (csa->beta[i] > lp->u[k])
+ r[i] = lp->u[k] - csa->beta[i];
+ else
+ r[i] = 0.0;
+
+if (fabs(r[i] - csa->r.vec[i]) > 1e-6)
+printf("i = %d; r = %g; csa->r = %g\n", i, r[i], csa->r.vec[i]);
+
+
+ }
+ err = err_in_vec(m, r, csa->r.vec);
+ tfree(r);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_d - compute maximal relative error in vector d
+*
+* This routine computes and returns maximal relative error in vector
+* of reduced costs of non-basic variables d = (d[j]).
+*
+* NOTE: This routine is intended only for debugging purposes. */
+
+static double err_in_d(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ int j;
+ double err, *pi, *d;
+ pi = talloc(1+m, double);
+ d = talloc(1+n-m, double);
+ spx_eval_pi(lp, pi);
+ for (j = 1; j <= n-m; j++)
+ d[j] = spx_eval_dj(lp, pi, j);
+ err = err_in_vec(n-m, d, csa->d);
+ tfree(pi);
+ tfree(d);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* err_in_gamma - compute maximal relative error in vector gamma
+*
+* This routine computes and returns maximal relative error in vector
+* of projected steepest edge weights gamma = (gamma[j]).
+*
+* NOTE: This routine is intended only for debugging purposes. */
+
+static double err_in_gamma(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ SPYSE *se = csa->se;
+ int i;
+ double err, *gamma;
+ xassert(se != NULL);
+gamma = talloc(1+m, double);
+ for (i = 1; i <= m; i++)
+ gamma[i] = spy_eval_gamma_i(lp, se, i);
+ err = err_in_vec(m, gamma, se->gamma);
+ tfree(gamma);
+ return err;
+}
+#endif
+
+#if CHECK_ACCURACY
+/***********************************************************************
+* check_accuracy - check accuracy of basic solution components
+*
+* This routine checks accuracy of current basic solution components.
+*
+* NOTE: This routine is intended only for debugging purposes. */
+
+static void check_accuracy(struct csa *csa)
+{ double e_beta, e_r, e_d, e_gamma;
+ e_beta = err_in_beta(csa);
+ e_r = err_in_r(csa);
+ e_d = err_in_d(csa);
+ if (csa->se == NULL)
+ e_gamma = 0.;
+ else
+ e_gamma = err_in_gamma(csa);
+ xprintf("e_beta = %10.3e; e_r = %10.3e; e_d = %10.3e; e_gamma = %"
+ "10.3e\n", e_beta, e_r, e_d, e_gamma);
+ xassert(e_beta <= 1e-5 && e_d <= 1e-5 && e_gamma <= 1e-3);
+ return;
+}
+#endif
+
+#if 1 /* 30/III-2016 */
+static
+void spy_eval_r(SPXLP *lp, const double beta[/*1+m*/], double tol,
+ double tol1, FVS *r)
+{ /* this routine computes the vector of primal infeasibilities:
+ *
+ * ( lB[i] - beta[i] > 0, if beta[i] < lb[i]
+ * r[i] = { 0, if lb[i] <= beta[i] <= ub[i]
+ * ( ub[i] - beta[i] < 0, if beta[i] > ub[i]
+ *
+ * (this routine replaces spy_chuzr_sel) */
+ int m = lp->m;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int *ind = r->ind;
+ double *vec = r->vec;
+ int i, k, nnz = 0;
+ double lk, uk, eps;
+ xassert(r->n == m);
+ /* walk thru the list of basic variables */
+ for (i = 1; i <= m; i++)
+ { vec[i] = 0.0;
+ k = head[i]; /* x[k] = xB[i] */
+ lk = l[k], uk = u[k];
+ /* check primal feasibility */
+ if (beta[i] < lk)
+ { /* determine absolute tolerance eps1[i] */
+ eps = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] < lk - eps)
+ { /* lower bound is violated */
+ ind[++nnz] = i;
+ vec[i] = lk - beta[i];
+ }
+ }
+ else if (beta[i] > uk)
+ { /* determine absolute tolerance eps2[i] */
+ eps = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] > uk + eps)
+ { /* upper bound is violated */
+ ind[++nnz] = i;
+ vec[i] = uk - beta[i];
+ }
+ }
+ }
+ r->nnz = nnz;
+ return;
+}
+#endif
+
+/***********************************************************************
+* choose_pivot - choose xB[p] and xN[q]
+*
+* Given the list of eligible basic variables this routine first
+* chooses basic variable xB[p]. This choice is always possible,
+* because the list is assumed to be non-empty. Then the routine
+* computes p-th row T[p,*] of the simplex table T[i,j] and chooses
+* non-basic variable xN[q]. If the pivot T[p,q] is small in magnitude,
+* the routine attempts to choose another xB[p] and xN[q] in order to
+* avoid badly conditioned adjacent bases.
+*
+* If the normal choice was made, the routine returns zero. Otherwise,
+* if the long-step choice was made, the routine returns non-zero. */
+
+#ifdef TIMING /* 31/III-2016 */
+
+#include "choose_pivot.c"
+
+#else
+
+#define MIN_RATIO 0.0001
+
+static int choose_pivot(struct csa *csa)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ SPXAT *at = csa->at;
+ SPXNT *nt = csa->nt;
+ double *beta = csa->beta;
+ double *d = csa->d;
+ SPYSE *se = csa->se;
+#if 0 /* 30/III-2016 */
+ int *list = csa->list;
+#else
+ int *list = csa->r.ind;
+#endif
+ double *rho = csa->work;
+ double *trow = csa->work1;
+ SPYBP *bp = csa->bp;
+ double tol_piv = csa->tol_piv;
+ int try, nnn, j, k, p, q, t, t_best, nbp, ret;
+ double big, temp, r, best_ratio, dz_best;
+ xassert(csa->beta_st);
+ xassert(csa->d_st);
+more: /* initial number of eligible basic variables */
+#if 0 /* 30/III-2016 */
+ nnn = csa->num;
+#else
+ nnn = csa->r.nnz;
+#endif
+ /* nothing has been chosen so far */
+ csa->p = 0;
+ best_ratio = 0.0;
+ try = ret = 0;
+try: /* choose basic variable xB[p] */
+ xassert(nnn > 0);
+ try++;
+ if (se == NULL)
+ { /* dual Dantzig's rule */
+ p = spy_chuzr_std(lp, beta, nnn, list);
+ }
+ else
+ { /* dual projected steepest edge */
+ p = spy_chuzr_pse(lp, se, beta, nnn, list);
+ }
+ xassert(1 <= p && p <= m);
+ /* compute p-th row of inv(B) */
+ spx_eval_rho(lp, p, rho);
+ /* compute p-th row of the simplex table */
+ if (at != NULL)
+ spx_eval_trow1(lp, at, rho, trow);
+ else
+ spx_nt_prod(lp, nt, trow, 1, -1.0, rho);
+#if 1 /* 23/III-2016 */
+ /* big := max(1, |trow[1]|, ..., |trow[n-m]|) */
+ big = 1.0;
+ for (j = 1; j <= n-m; j++)
+ { temp = trow[j];
+ if (temp < 0.0)
+ temp = - temp;
+ if (big < temp)
+ big = temp;
+ }
+#else
+ /* this still puzzles me */
+ big = 1.0;
+#endif
+ /* choose non-basic variable xN[q] */
+ k = head[p]; /* x[k] = xB[p] */
+ xassert(beta[p] < l[k] || beta[p] > u[k]);
+ r = beta[p] < l[k] ? l[k] - beta[p] : u[k] - beta[p];
+ if (csa->r_test == GLP_RT_FLIP && try <= 2)
+ { /* long-step ratio test */
+#if 0 /* 23/III-2016 */
+ /* determine dual objective break-points */
+ nbp = spy_eval_bp(lp, d, r, trow, tol_piv, bp);
+ if (nbp <= 1)
+ goto skip;
+ /* choose appropriate break-point */
+ t_best = 0, dz_best = -DBL_MAX;
+ for (t = 1; t <= nbp; t++)
+ { if (fabs(trow[bp[t].j]) / big >= MIN_RATIO)
+ { if (dz_best < bp[t].dz)
+ t_best = t, dz_best = bp[t].dz;
+ }
+ }
+ if (t_best == 0)
+ goto skip;
+#else
+ int t, num, num1;
+ double slope, teta_lim;
+ /* determine dual objective break-points */
+ nbp = spy_ls_eval_bp(lp, d, r, trow, tol_piv, bp);
+ if (nbp < 2)
+ goto skip;
+ /* set initial slope */
+ slope = fabs(r);
+ /* estimate initial teta_lim */
+ teta_lim = DBL_MAX;
+ for (t = 1; t <= nbp; t++)
+ { if (teta_lim > bp[t].teta)
+ teta_lim = bp[t].teta;
+ }
+ xassert(teta_lim >= 0.0);
+ if (teta_lim < 1e-6)
+ teta_lim = 1e-6;
+ /* nothing has been chosen so far */
+ t_best = 0, dz_best = 0.0, num = 0;
+ /* choose appropriate break-point */
+ while (num < nbp)
+ { /* select and process a new portion of break-points */
+ num1 = spy_ls_select_bp(lp, trow, nbp, bp, num, &slope,
+ teta_lim);
+ for (t = num+1; t <= num1; t++)
+ { if (fabs(trow[bp[t].j]) / big >= MIN_RATIO)
+ { if (dz_best < bp[t].dz)
+ t_best = t, dz_best = bp[t].dz;
+ }
+ }
+ if (slope < 0.0)
+ { /* the dual objective starts decreasing */
+ break;
+ }
+ /* the dual objective continues increasing */
+ num = num1;
+ teta_lim += teta_lim;
+ }
+ if (dz_best == 0.0)
+ goto skip;
+ xassert(1 <= t_best && t_best <= num1);
+#endif
+ /* the choice has been made */
+ csa->p = p;
+#if 0 /* 29/III-2016 */
+ memcpy(&csa->trow[1], &trow[1], (n-m) * sizeof(double));
+#else
+ memcpy(&csa->trow.vec[1], &trow[1], (n-m) * sizeof(double));
+ fvs_gather_vec(&csa->trow, DBL_EPSILON);
+#endif
+ csa->q = bp[t_best].j;
+ best_ratio = fabs(trow[bp[t_best].j]) / big;
+#if 0
+ xprintf("num = %d; t_best = %d; dz = %g\n", num, t_best,
+ bp[t_best].dz);
+#endif
+ ret = 1;
+ goto done;
+skip: ;
+ }
+ if (csa->r_test == GLP_RT_STD)
+ { /* textbook dual ratio test */
+ q = spy_chuzc_std(lp, d, r, trow, tol_piv,
+ .30 * csa->tol_dj, .30 * csa->tol_dj1);
+ }
+ else
+ { /* Harris' two-pass dual ratio test */
+ q = spy_chuzc_harris(lp, d, r, trow, tol_piv,
+ .35 * csa->tol_dj, .35 * csa->tol_dj1);
+ }
+ if (q == 0)
+ { /* dual unboundedness */
+ csa->p = p;
+#if 0 /* 29/III-2016 */
+ memcpy(&csa->trow[1], &trow[1], (n-m) * sizeof(double));
+#else
+ memcpy(&csa->trow.vec[1], &trow[1], (n-m) * sizeof(double));
+ fvs_gather_vec(&csa->trow, DBL_EPSILON);
+#endif
+ csa->q = q;
+ best_ratio = 1.0;
+ goto done;
+ }
+ /* either keep previous choice or accept new choice depending on
+ * which one is better */
+ if (best_ratio < fabs(trow[q]) / big)
+ { csa->p = p;
+#if 0 /* 29/III-2016 */
+ memcpy(&csa->trow[1], &trow[1], (n-m) * sizeof(double));
+#else
+ memcpy(&csa->trow.vec[1], &trow[1], (n-m) * sizeof(double));
+ fvs_gather_vec(&csa->trow, DBL_EPSILON);
+#endif
+ csa->q = q;
+ best_ratio = fabs(trow[q]) / big;
+ }
+ /* check if the current choice is acceptable */
+ if (best_ratio >= MIN_RATIO || nnn == 1 || try == 5)
+ goto done;
+ /* try to choose other xB[p] and xN[q] */
+ /* find xB[p] in the list */
+ for (t = 1; t <= nnn; t++)
+ if (list[t] == p) break;
+ xassert(t <= nnn);
+ /* move xB[p] to the end of the list */
+ list[t] = list[nnn], list[nnn] = p;
+ /* and exclude it from consideration */
+ nnn--;
+ /* repeat the choice */
+ goto try;
+done: /* the choice has been made */
+#if 1 /* FIXME: currently just to avoid badly conditioned basis */
+ if (best_ratio < .001 * MIN_RATIO)
+ { /* looks like this helps */
+ if (bfd_get_count(lp->bfd) > 0)
+ return -1;
+ /* didn't help; last chance to improve the choice */
+ if (tol_piv == csa->tol_piv)
+ { tol_piv *= 1000.;
+ goto more;
+ }
+ }
+#endif
+#if 1 /* FIXME */
+ if (ret)
+ { /* invalidate basic solution components */
+#if 0 /* 28/III-2016 */
+ csa->beta_st = csa->d_st = 0;
+#else
+ /* dual solution remains valid */
+ csa->beta_st = 0;
+#endif
+ /* set double-bounded non-basic variables to opposite bounds
+ * for all break-points preceding the chosen one */
+ for (t = 1; t < t_best; t++)
+ { k = head[m + bp[t].j];
+ xassert(-DBL_MAX < l[k] && l[k] < u[k] && u[k] < +DBL_MAX);
+ lp->flag[bp[t].j] = !(lp->flag[bp[t].j]);
+ }
+ }
+#endif
+ return ret;
+}
+
+#endif
+
+/***********************************************************************
+* play_coef - play objective coefficients
+*
+* This routine is called after the reduced costs d[j] was updated and
+* the basis was changed to the adjacent one.
+*
+* It is assumed that before updating all the reduced costs d[j] were
+* strongly feasible, so in the adjacent basis d[j] remain feasible
+* within a tolerance, i.e. if some d[j] violates its zero bound, the
+* violation is insignificant.
+*
+* If some d[j] violates its zero bound, the routine changes (perturbs)
+* objective coefficient cN[j] to provide d[j] = 0, i.e. to make all
+* d[j] strongly feasible. Otherwise, if d[j] has a feasible value, the
+* routine attempts to reduce (or remove) perturbation in cN[j] by
+* shifting d[j] to its zero bound keeping strong feasibility. */
+
+static void play_coef(struct csa *csa, int all)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *c = lp->c;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *orig_c = csa->orig_c;
+ double *d = csa->d;
+ const double *trow = csa->trow.vec;
+ /* this vector was used to update d = (d[j]) */
+ int j, k;
+ static const double eps = 1e-9;
+ /* reduced costs d = (d[j]) should be valid */
+ xassert(csa->d_st);
+ /* walk thru the list of non-basic variables xN = (xN[j]) */
+ for (j = 1; j <= n-m; j++)
+ { if (all || trow[j] != 0.0)
+ { /* d[j] has changed in the adjacent basis */
+ k = head[m+j]; /* x[k] = xN[j] */
+ if (l[k] == u[k])
+ { /* xN[j] is fixed variable */
+ /* d[j] may have any sign */
+ }
+ else if (l[k] == -DBL_MAX && u[k] == +DBL_MAX)
+ { /* xN[j] is free (unbounded) variable */
+ /* strong feasibility means d[j] = 0 */
+ c[k] -= d[j], d[j] = 0.0;
+ /* in this case dual degeneracy is not critical, since
+ * if xN[j] enters the basis, it never leaves it */
+ }
+ else if (!flag[j])
+ { /* xN[j] has its lower bound active */
+ xassert(l[k] != -DBL_MAX);
+ /* first, we remove current perturbation to provide
+ * c[k] = orig_c[k] */
+ d[j] -= c[k] - orig_c[k], c[k] = orig_c[k];
+ /* strong feasibility means d[j] >= 0, but we provide
+ * d[j] >= +eps to prevent dual degeneracy */
+ if (d[j] < +eps)
+ c[k] -= d[j] - eps, d[j] = +eps;
+ }
+ else
+ { /* xN[j] has its upper bound active */
+ xassert(u[k] != +DBL_MAX);
+ /* similarly, we remove current perturbation to provide
+ * c[k] = orig_c[k] */
+ d[j] -= c[k] - orig_c[k], c[k] = orig_c[k];
+ /* strong feasibility means d[j] <= 0, but we provide
+ * d[j] <= -eps to prevent dual degeneracy */
+ if (d[j] > -eps)
+ c[k] -= d[j] + eps, d[j] = -eps;
+ }
+ }
+ }
+ return;
+}
+
+#if 1 /* 11/VII-2017 */
+static void remove_perturb(struct csa *csa)
+{ /* remove perturbation */
+ SPXLP *lp = csa->lp;
+ int n = lp->n;
+ double *c = lp->c;
+ double *orig_c = csa->orig_c;
+ memcpy(c, orig_c, (1+n) * sizeof(double));
+ /* removing perturbation changes dual solution components */
+ csa->phase = csa->d_st = 0;
+#if 1
+ if (csa->msg_lev >= GLP_MSG_ALL)
+ xprintf("Removing LP perturbation [%d]...\n",
+ csa->it_cnt);
+#endif
+ return;
+}
+#endif
+
+/***********************************************************************
+* display - display search progress
+*
+* This routine displays some information about the search progress
+* that includes:
+*
+* search phase;
+*
+* number of simplex iterations performed by the solver;
+*
+* original objective value (only on phase II);
+*
+* sum of (scaled) dual infeasibilities for original bounds;
+*
+* number of dual infeasibilities (phase I) or primal infeasibilities
+* (phase II);
+*
+* number of basic factorizations since last display output. */
+
+static void display(struct csa *csa, int spec)
+{ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ int *head = lp->head;
+ char *flag = lp->flag;
+ double *l = csa->orig_l; /* original lower bounds */
+ double *u = csa->orig_u; /* original upper bounds */
+ double *beta = csa->beta;
+ double *d = csa->d;
+ int j, k, nnn;
+ double sum;
+#if 1 /* 15/VII-2017 */
+ double tm_cur;
+#endif
+ /* check if the display output should be skipped */
+ if (csa->msg_lev < GLP_MSG_ON) goto skip;
+#if 1 /* 15/VII-2017 */
+ tm_cur = xtime();
+#endif
+ if (csa->out_dly > 0 &&
+#if 0 /* 15/VII-2017 */
+ 1000.0 * xdifftime(xtime(), csa->tm_beg) < csa->out_dly)
+#else
+ 1000.0 * xdifftime(tm_cur, csa->tm_beg) < csa->out_dly)
+#endif
+ goto skip;
+ if (csa->it_cnt == csa->it_dpy) goto skip;
+#if 0 /* 15/VII-2017 */
+ if (!spec && csa->it_cnt % csa->out_frq != 0) goto skip;
+#else
+ if (!spec &&
+ 1000.0 * xdifftime(tm_cur, csa->tm_dpy) < csa->out_frq)
+ goto skip;
+#endif
+ /* display search progress depending on search phase */
+ switch (csa->phase)
+ { case 1:
+ /* compute sum and number of (scaled) dual infeasibilities
+ * for original bounds */
+ sum = 0.0, nnn = 0;
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (d[j] > 0.0)
+ { /* xN[j] should have lower bound */
+ if (l[k] == -DBL_MAX)
+ { sum += d[j];
+ if (d[j] > +1e-7)
+ nnn++;
+ }
+ }
+ else if (d[j] < 0.0)
+ { /* xN[j] should have upper bound */
+ if (u[k] == +DBL_MAX)
+ { sum -= d[j];
+ if (d[j] < -1e-7)
+ nnn++;
+ }
+ }
+ }
+ /* on phase I variables have artificial bounds which are
+ * meaningless for original LP, so corresponding objective
+ * function value is also meaningless */
+#if 0 /* 27/III-2016 */
+ xprintf(" %6d: %23s inf = %11.3e (%d)",
+ csa->it_cnt, "", sum, nnn);
+#else
+ xprintf(" %6d: sum = %17.9e inf = %11.3e (%d)",
+ csa->it_cnt, lp->c[0] - spx_eval_obj(lp, beta),
+ sum, nnn);
+#endif
+ break;
+ case 2:
+ /* compute sum of (scaled) dual infeasibilities */
+ sum = 0.0, nnn = 0;
+ for (j = 1; j <= n-m; j++)
+ { k = head[m+j]; /* x[k] = xN[j] */
+ if (d[j] > 0.0)
+ { /* xN[j] should have its lower bound active */
+ if (l[k] == -DBL_MAX || flag[j])
+ sum += d[j];
+ }
+ else if (d[j] < 0.0)
+ { /* xN[j] should have its upper bound active */
+ if (l[k] != u[k] && !flag[j])
+ sum -= d[j];
+ }
+ }
+ /* compute number of primal infeasibilities */
+ nnn = spy_chuzr_sel(lp, beta, csa->tol_bnd, csa->tol_bnd1,
+ NULL);
+ xprintf("#%6d: obj = %17.9e inf = %11.3e (%d)",
+#if SCALE_Z
+ csa->it_cnt,
+ (double)csa->dir * csa->fz * spx_eval_obj(lp, beta),
+#else
+ csa->it_cnt, (double)csa->dir * spx_eval_obj(lp, beta),
+#endif
+ sum, nnn);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ if (csa->inv_cnt)
+ { /* number of basis factorizations performed */
+ xprintf(" %d", csa->inv_cnt);
+ csa->inv_cnt = 0;
+ }
+#if 1 /* 23/III-2016 */
+ if (csa->r_test == GLP_RT_FLIP)
+ { /*xprintf(" %d,%d", csa->ns_cnt, csa->ls_cnt);*/
+ if (csa->ns_cnt + csa->ls_cnt)
+ xprintf(" %d%%",
+ (100 * csa->ls_cnt) / (csa->ns_cnt + csa->ls_cnt));
+ csa->ns_cnt = csa->ls_cnt = 0;
+ }
+#endif
+ xprintf("\n");
+ csa->it_dpy = csa->it_cnt;
+#if 1 /* 15/VII-2017 */
+ csa->tm_dpy = tm_cur;
+#endif
+skip: return;
+}
+
+#if 1 /* 31/III-2016 */
+static
+void spy_update_r(SPXLP *lp, int p, int q, const double beta[/*1+m*/],
+ const FVS *tcol, double tol, double tol1, FVS *r)
+{ /* update vector r of primal infeasibilities */
+ /* it is assumed that xB[p] leaves the basis, xN[q] enters the
+ * basis, and beta corresponds to the adjacent basis (i.e. this
+ * routine should be called after spx_update_beta) */
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ int *tcol_ind = tcol->ind;
+ int *ind = r->ind;
+ double *vec = r->vec;
+ int i, k, t, nnz;
+ double lk, uk, ri, eps;
+ xassert(1 <= p && p <= m);
+ xassert(1 <= q && q <= n-m);
+ nnz = r->nnz;
+ for (t = tcol->nnz; t >= 1; t--)
+ { i = tcol_ind[t];
+ /* xB[i] changes in the adjacent basis to beta[i], so only
+ * r[i] should be updated */
+ if (i == p)
+ k = head[m+q]; /* x[k] = new xB[p] = old xN[q] */
+ else
+ k = head[i]; /* x[k] = new xB[i] = old xB[i] */
+ lk = l[k], uk = u[k];
+ /* determine new value of r[i]; see spy_eval_r */
+ ri = 0.0;
+ if (beta[i] < lk)
+ { /* determine absolute tolerance eps1[i] */
+ eps = tol + tol1 * (lk >= 0.0 ? +lk : -lk);
+ if (beta[i] < lk - eps)
+ { /* lower bound is violated */
+ ri = lk - beta[i];
+ }
+ }
+ else if (beta[i] > uk)
+ { /* determine absolute tolerance eps2[i] */
+ eps = tol + tol1 * (uk >= 0.0 ? +uk : -uk);
+ if (beta[i] > uk + eps)
+ { /* upper bound is violated */
+ ri = uk - beta[i];
+ }
+ }
+ if (ri == 0.0)
+ { if (vec[i] != 0.0)
+ vec[i] = DBL_MIN; /* will be removed */
+ }
+ else
+ { if (vec[i] == 0.0)
+ ind[++nnz] = i;
+ vec[i] = ri;
+ }
+
+ }
+ r->nnz = nnz;
+ /* remove zero elements */
+ fvs_adjust_vec(r, DBL_MIN + DBL_MIN);
+ return;
+}
+#endif
+
+/***********************************************************************
+* spy_dual - driver to the dual simplex method
+*
+* This routine is a driver to the two-phase dual simplex method.
+*
+* On exit this routine returns one of the following codes:
+*
+* 0 LP instance has been successfully solved.
+*
+* GLP_EOBJLL
+* Objective lower limit has been reached (maximization).
+*
+* GLP_EOBJUL
+* Objective upper limit has been reached (minimization).
+*
+* GLP_EITLIM
+* Iteration limit has been exhausted.
+*
+* GLP_ETMLIM
+* Time limit has been exhausted.
+*
+* GLP_EFAIL
+* The solver failed to solve LP instance. */
+
+static int dual_simplex(struct csa *csa)
+{ /* dual simplex method main logic routine */
+ SPXLP *lp = csa->lp;
+ int m = lp->m;
+ int n = lp->n;
+ double *l = lp->l;
+ double *u = lp->u;
+ int *head = lp->head;
+ SPXNT *nt = csa->nt;
+ double *beta = csa->beta;
+ double *d = csa->d;
+ SPYSE *se = csa->se;
+#if 0 /* 30/III-2016 */
+ int *list = csa->list;
+#endif
+#if 0 /* 31/III-2016 */
+ double *trow = csa->trow;
+ double *tcol = csa->tcol;
+#endif
+ double *pi = csa->work;
+ int msg_lev = csa->msg_lev;
+ double tol_bnd = csa->tol_bnd;
+ double tol_bnd1 = csa->tol_bnd1;
+ double tol_dj = csa->tol_dj;
+ double tol_dj1 = csa->tol_dj1;
+ int j, k, p_flag, refct, ret;
+ int perturb = -1;
+ /* -1 = perturbation is not used, but enabled
+ * 0 = perturbation is not used and disabled
+ * +1 = perturbation is being used */
+#if 1 /* 27/III-2016 */
+ int instab = 0; /* instability count */
+#endif
+#ifdef TIMING
+ double t_total = timer(); /* total time */
+ double t_fact = 0.0; /* computing factorization */
+ double t_rtest = 0.0; /* performing ratio test */
+ double t_pivcol = 0.0; /* computing pivot column */
+ double t_upd1 = 0.0; /* updating primal values */
+ double t_upd2 = 0.0; /* updating dual values */
+ double t_upd3 = 0.0; /* updating se weights */
+ double t_upd4 = 0.0; /* updating matrix N */
+ double t_upd5 = 0.0; /* updating factorization */
+ double t_start;
+#endif
+ check_flags(csa);
+loop: /* main loop starts here */
+ /* compute factorization of the basis matrix */
+ if (!lp->valid)
+ { double cond;
+#ifdef TIMING
+ t_start = timer();
+#endif
+ ret = spx_factorize(lp);
+#ifdef TIMING
+ t_fact += timer() - t_start;
+#endif
+ csa->inv_cnt++;
+ if (ret != 0)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: unable to factorize the basis matrix (%d"
+ ")\n", ret);
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+ /* check condition of the basis matrix */
+ cond = bfd_condest(lp->bfd);
+ if (cond > 1.0 / DBL_EPSILON)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: basis matrix is singular to working prec"
+ "ision (cond = %.3g)\n", cond);
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+ if (cond > 0.001 / DBL_EPSILON)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: basis matrix is ill-conditioned (cond "
+ "= %.3g)\n", cond);
+ }
+ /* invalidate basic solution components */
+ csa->beta_st = csa->d_st = 0;
+ }
+ /* compute reduced costs of non-basic variables d = (d[j]) */
+ if (!csa->d_st)
+ { spx_eval_pi(lp, pi);
+ for (j = 1; j <= n-m; j++)
+ d[j] = spx_eval_dj(lp, pi, j);
+ csa->d_st = 1; /* just computed */
+ /* determine the search phase, if not determined yet (this is
+ * performed only once at the beginning of the search for the
+ * original bounds) */
+ if (!csa->phase)
+ { j = check_feas(csa, 0.97 * tol_dj, 0.97 * tol_dj1, 1);
+ if (j > 0)
+ { /* initial basic solution is dual infeasible and cannot
+ * be recovered */
+ /* start to search for dual feasible solution */
+ set_art_bounds(csa);
+ csa->phase = 1;
+ }
+ else
+ { /* initial basic solution is either dual feasible or its
+ * dual feasibility has been recovered */
+ /* start to search for optimal solution */
+ csa->phase = 2;
+ }
+ }
+ /* make sure that current basic solution is dual feasible */
+#if 1 /* 11/VII-2017 */
+ if (perturb <= 0)
+ { if (check_feas(csa, tol_dj, tol_dj1, 0))
+ { /* dual feasibility is broken due to excessive round-off
+ * errors */
+ if (perturb < 0)
+ { if (msg_lev >= GLP_MSG_ALL)
+ xprintf("Perturbing LP to avoid instability [%d].."
+ ".\n", csa->it_cnt);
+ perturb = 1;
+ goto loop;
+ }
+ if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: numerical instability (dual simplex"
+ ", phase %s)\n", csa->phase == 1 ? "I" : "II");
+ instab++;
+ if (csa->dualp && instab >= 10)
+ { /* do not continue the search; report failure */
+ if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Warning: dual simplex failed due to exces"
+ "sive numerical instability\n");
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = -1; /* special case of GLP_EFAIL */
+ goto fini;
+ }
+ /* try to recover dual feasibility */
+ j = check_feas(csa, 0.97 * tol_dj, 0.97 * tol_dj1, 1);
+ if (j > 0)
+ { /* dual feasibility cannot be recovered (this may
+ * happen only on phase II) */
+ xassert(csa->phase == 2);
+ /* restart to search for dual feasible solution */
+ set_art_bounds(csa);
+ csa->phase = 1;
+ }
+ }
+ }
+ else
+ { /* FIXME */
+ play_coef(csa, 1);
+ }
+ }
+#endif
+ /* at this point the search phase is determined */
+ xassert(csa->phase == 1 || csa->phase == 2);
+ /* compute values of basic variables beta = (beta[i]) */
+ if (!csa->beta_st)
+ { spx_eval_beta(lp, beta);
+#if 1 /* 31/III-2016 */
+ /* also compute vector r of primal infeasibilities */
+ switch (csa->phase)
+ { case 1:
+ spy_eval_r(lp, beta, 1e-8, 0.0, &csa->r);
+ break;
+ case 2:
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+#endif
+ csa->beta_st = 1; /* just computed */
+ }
+ /* reset the dual reference space, if necessary */
+ if (se != NULL && !se->valid)
+ spy_reset_refsp(lp, se), refct = 1000;
+ /* at this point the basis factorization and all basic solution
+ * components are valid */
+ xassert(lp->valid && csa->beta_st && csa->d_st);
+#ifdef GLP_DEBUG
+ check_flags(csa);
+#endif
+#if CHECK_ACCURACY
+ /* check accuracy of current basic solution components (only for
+ * debugging) */
+ check_accuracy(csa);
+#endif
+ /* check if the objective limit has been reached */
+ if (csa->phase == 2 && csa->obj_lim != DBL_MAX
+ && spx_eval_obj(lp, beta) >= csa->obj_lim)
+ {
+#if 1 /* 26/V-2017 by mao */
+ if (perturb > 0)
+ { /* remove perturbation */
+ /* [Should note that perturbing of objective coefficients
+ * implemented in play_coef is equivalent to *relaxing* of
+ * (zero) bounds of dual variables, so the perturbed
+ * objective is always better (*greater*) that the original
+ * one at the same basic point.] */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+#endif
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("OBJECTIVE %s LIMIT REACHED; SEARCH TERMINATED\n",
+ csa->dir > 0 ? "UPPER" : "LOWER");
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, tol_bnd, tol_bnd1, list);
+ csa->p_stat = (csa->num == 0 ? GLP_FEAS : GLP_INFEAS);
+#else
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+ csa->p_stat = (csa->r.nnz == 0 ? GLP_FEAS : GLP_INFEAS);
+#endif
+ csa->d_stat = GLP_FEAS;
+ ret = (csa->dir > 0 ? GLP_EOBJUL : GLP_EOBJLL);
+ goto fini;
+ }
+ /* check if the iteration limit has been exhausted */
+ if (csa->it_cnt - csa->it_beg >= csa->it_lim)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ if (csa->phase == 1)
+ { set_orig_bounds(csa);
+ check_flags(csa);
+ spx_eval_beta(lp, beta);
+ }
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, tol_bnd, tol_bnd1, list);
+ csa->p_stat = (csa->num == 0 ? GLP_FEAS : GLP_INFEAS);
+#else
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+ csa->p_stat = (csa->r.nnz == 0 ? GLP_FEAS : GLP_INFEAS);
+#endif
+ csa->d_stat = (csa->phase == 1 ? GLP_INFEAS : GLP_FEAS);
+ ret = GLP_EITLIM;
+ goto fini;
+ }
+ /* check if the time limit has been exhausted */
+ if (1000.0 * xdifftime(xtime(), csa->tm_beg) >= csa->tm_lim)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
+ if (csa->phase == 1)
+ { set_orig_bounds(csa);
+ check_flags(csa);
+ spx_eval_beta(lp, beta);
+ }
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, tol_bnd, tol_bnd1, list);
+ csa->p_stat = (csa->num == 0 ? GLP_FEAS : GLP_INFEAS);
+#else
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+ csa->p_stat = (csa->r.nnz == 0 ? GLP_FEAS : GLP_INFEAS);
+#endif
+ csa->d_stat = (csa->phase == 1 ? GLP_INFEAS : GLP_FEAS);
+ ret = GLP_ETMLIM;
+ goto fini;
+ }
+ /* display the search progress */
+ display(csa, 0);
+ /* select eligible basic variables */
+#if 0 /* 31/III-2016; not needed because r is valid */
+ switch (csa->phase)
+ { case 1:
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, 1e-8, 0.0, list);
+#else
+ spy_eval_r(lp, beta, 1e-8, 0.0, &csa->r);
+#endif
+ break;
+ case 2:
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, tol_bnd, tol_bnd1, list);
+#else
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+#endif
+ break;
+ default:
+ xassert(csa != csa);
+ }
+#endif
+ /* check for optimality */
+#if 0 /* 30/III-2016 */
+ if (csa->num == 0)
+#else
+ if (csa->r.nnz == 0)
+#endif
+ { if (perturb > 0 && csa->phase == 2)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ /* current basis is optimal */
+ display(csa, 1);
+ switch (csa->phase)
+ { case 1:
+ /* check for dual feasibility */
+ set_orig_bounds(csa);
+ check_flags(csa);
+ if (check_feas(csa, tol_dj, tol_dj1, 0) == 0)
+ { /* dual feasible solution found; switch to phase II */
+ csa->phase = 2;
+ xassert(!csa->beta_st);
+ goto loop;
+ }
+#if 1 /* 26/V-2017 by cmatraki */
+ if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ goto loop;
+ }
+#endif
+ /* no dual feasible solution exists */
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("LP HAS NO DUAL FEASIBLE SOLUTION\n");
+ spx_eval_beta(lp, beta);
+#if 0 /* 30/III-2016 */
+ csa->num = spy_chuzr_sel(lp, beta, tol_bnd, tol_bnd1,
+ list);
+ csa->p_stat = (csa->num == 0 ? GLP_FEAS : GLP_INFEAS);
+#else
+ spy_eval_r(lp, beta, tol_bnd, tol_bnd1, &csa->r);
+ csa->p_stat = (csa->r.nnz == 0 ? GLP_FEAS : GLP_INFEAS);
+#endif
+ csa->d_stat = GLP_NOFEAS;
+ ret = 0;
+ goto fini;
+ case 2:
+ /* optimal solution found */
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("OPTIMAL LP SOLUTION FOUND\n");
+ csa->p_stat = csa->d_stat = GLP_FEAS;
+ ret = 0;
+ goto fini;
+ default:
+ xassert(csa != csa);
+ }
+ }
+ /* choose xB[p] and xN[q] */
+#if 0 /* 23/III-2016 */
+ choose_pivot(csa);
+#else
+#ifdef TIMING
+ t_start = timer();
+#endif
+#if 1 /* 31/III-2016 */
+ ret = choose_pivot(csa);
+#endif
+#ifdef TIMING
+ t_rtest += timer() - t_start;
+#endif
+ if (ret < 0)
+ { lp->valid = 0;
+ goto loop;
+ }
+ if (ret == 0)
+ csa->ns_cnt++;
+ else
+ csa->ls_cnt++;
+#endif
+ /* check for dual unboundedness */
+ if (csa->q == 0)
+ { if (perturb > 0)
+ { /* remove perturbation */
+ remove_perturb(csa);
+ perturb = 0;
+ }
+ if (csa->beta_st != 1)
+ csa->beta_st = 0;
+ if (csa->d_st != 1)
+ csa->d_st = 0;
+ if (!(csa->beta_st && csa->d_st))
+ goto loop;
+ display(csa, 1);
+ switch (csa->phase)
+ { case 1:
+ /* this should never happen */
+ if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: dual simplex failed\n");
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ case 2:
+ /* dual unboundedness detected */
+ if (msg_lev >= GLP_MSG_ALL)
+ xprintf("LP HAS NO PRIMAL FEASIBLE SOLUTION\n");
+ csa->p_stat = GLP_NOFEAS;
+ csa->d_stat = GLP_FEAS;
+ ret = 0;
+ goto fini;
+ default:
+ xassert(csa != csa);
+ }
+ }
+ /* compute q-th column of the simplex table */
+#ifdef TIMING
+ t_start = timer();
+#endif
+#if 0 /* 31/III-2016 */
+ spx_eval_tcol(lp, csa->q, tcol);
+#else
+ spx_eval_tcol(lp, csa->q, csa->tcol.vec);
+ fvs_gather_vec(&csa->tcol, DBL_EPSILON);
+#endif
+#ifdef TIMING
+ t_pivcol += timer() - t_start;
+#endif
+ /* FIXME: tcol[p] and trow[q] should be close to each other */
+#if 0 /* 26/V-2017 by cmatraki */
+ xassert(csa->tcol.vec[csa->p] != 0.0);
+#else
+ if (csa->tcol.vec[csa->p] == 0.0)
+ { if (msg_lev >= GLP_MSG_ERR)
+ xprintf("Error: tcol[p] = 0.0\n");
+ csa->p_stat = csa->d_stat = GLP_UNDEF;
+ ret = GLP_EFAIL;
+ goto fini;
+ }
+#endif
+ /* update values of basic variables for adjacent basis */
+ k = head[csa->p]; /* x[k] = xB[p] */
+ p_flag = (l[k] != u[k] && beta[csa->p] > u[k]);
+#if 0 /* 16/III-2016 */
+ spx_update_beta(lp, beta, csa->p, p_flag, csa->q, tcol);
+ csa->beta_st = 2;
+#else
+ /* primal solution may be invalidated due to long step */
+#ifdef TIMING
+ t_start = timer();
+#endif
+ if (csa->beta_st)
+#if 0 /* 30/III-2016 */
+ { spx_update_beta(lp, beta, csa->p, p_flag, csa->q, tcol);
+#else
+ { spx_update_beta_s(lp, beta, csa->p, p_flag, csa->q,
+ &csa->tcol);
+ /* also update vector r of primal infeasibilities */
+ /*fvs_check_vec(&csa->r);*/
+ switch (csa->phase)
+ { case 1:
+ spy_update_r(lp, csa->p, csa->q, beta, &csa->tcol,
+ 1e-8, 0.0, &csa->r);
+ break;
+ case 2:
+ spy_update_r(lp, csa->p, csa->q, beta, &csa->tcol,
+ tol_bnd, tol_bnd1, &csa->r);
+ break;
+ default:
+ xassert(csa != csa);
+ }
+ /*fvs_check_vec(&csa->r);*/
+#endif
+ csa->beta_st = 2;
+ }
+#ifdef TIMING
+ t_upd1 += timer() - t_start;
+#endif
+#endif
+#if 1 /* 11/VII-2017 */
+ /* check for stalling */
+ { int k;
+ xassert(1 <= csa->p && csa->p <= m);
+ xassert(1 <= csa->q && csa->q <= n-m);
+ /* FIXME: recompute d[q]; see spx_update_d */
+ k = head[m+csa->q]; /* x[k] = xN[q] */
+ if (!(lp->l[k] == -DBL_MAX && lp->u[k] == +DBL_MAX))
+ { if (fabs(d[csa->q]) >= 1e-6)
+ { csa->degen = 0;
+ goto skip1;
+ }
+ /* degenerate iteration has been detected */
+ csa->degen++;
+ if (perturb < 0 && csa->degen >= 200)
+ { if (msg_lev >= GLP_MSG_ALL)
+ xprintf("Perturbing LP to avoid stalling [%d]...\n",
+ csa->it_cnt);
+ perturb = 1;
+ }
+skip1: ;
+ }
+ }
+#endif
+ /* update reduced costs of non-basic variables for adjacent
+ * basis */
+#if 1 /* 28/III-2016 */
+ xassert(csa->d_st);
+#endif
+#ifdef TIMING
+ t_start = timer();
+#endif
+#if 0 /* 30/III-2016 */
+ if (spx_update_d(lp, d, csa->p, csa->q, trow, tcol) <= 1e-9)
+#else
+ if (spx_update_d_s(lp, d, csa->p, csa->q, &csa->trow, &csa->tcol)
+ <= 1e-9)
+#endif
+ { /* successful updating */
+ csa->d_st = 2;
+ }
+ else
+ { /* new reduced costs are inaccurate */
+ csa->d_st = 0;
+ }
+#ifdef TIMING
+ t_upd2 += timer() - t_start;
+#endif
+ /* update steepest edge weights for adjacent basis, if used */
+#ifdef TIMING
+ t_start = timer();
+#endif
+ if (se != NULL)
+ { if (refct > 0)
+#if 0 /* 30/III-2016 */
+ { if (spy_update_gamma(lp, se, csa->p, csa->q, trow, tcol)
+ <= 1e-3)
+#else
+ { if (spy_update_gamma_s(lp, se, csa->p, csa->q, &csa->trow,
+ &csa->tcol) <= 1e-3)
+#endif
+ { /* successful updating */
+ refct--;
+ }
+ else
+ { /* new weights are inaccurate; reset reference space */
+ se->valid = 0;
+ }
+ }
+ else
+ { /* too many updates; reset reference space */
+ se->valid = 0;
+ }
+ }
+#ifdef TIMING
+ t_upd3 += timer() - t_start;
+#endif
+#ifdef TIMING
+ t_start = timer();
+#endif
+ /* update matrix N for adjacent basis, if used */
+ if (nt != NULL)
+ spx_update_nt(lp, nt, csa->p, csa->q);
+#ifdef TIMING
+ t_upd4 += timer() - t_start;
+#endif
+ /* change current basis header to adjacent one */
+ spx_change_basis(lp, csa->p, p_flag, csa->q);
+ /* and update factorization of the basis matrix */
+#ifdef TIMING
+ t_start = timer();
+#endif
+#if 0 /* 16/III-2016 */
+ if (csa->p > 0)
+#endif
+ spx_update_invb(lp, csa->p, head[csa->p]);
+#ifdef TIMING
+ t_upd5 += timer() - t_start;
+#endif
+ if (perturb > 0 && csa->d_st)
+ play_coef(csa, 0);
+ /* dual simplex iteration complete */
+ csa->it_cnt++;
+ goto loop;
+fini:
+#ifdef TIMING
+ t_total = timer() - t_total;
+ xprintf("Total time = %10.3f\n", t_total);
+ xprintf("Factorization = %10.3f\n", t_fact);
+ xprintf("Ratio test = %10.3f\n", t_rtest);
+ xprintf("Pivot column = %10.3f\n", t_pivcol);
+ xprintf("Updating beta = %10.3f\n", t_upd1);
+ xprintf("Updating d = %10.3f\n", t_upd2);
+ xprintf("Updating gamma = %10.3f\n", t_upd3);
+ xprintf("Updating N = %10.3f\n", t_upd4);
+ xprintf("Updating inv(B) = %10.3f\n", t_upd5);
+#endif
+ return ret;
+}
+
+int spy_dual(glp_prob *P, const glp_smcp *parm)
+{ /* driver to the dual simplex method */
+ struct csa csa_, *csa = &csa_;
+ SPXLP lp;
+ SPXAT at;
+ SPXNT nt;
+ SPYSE se;
+ int ret, *map, *daeh;
+#if SCALE_Z
+ int i, j, k;
+#endif
+ /* build working LP and its initial basis */
+ memset(csa, 0, sizeof(struct csa));
+ csa->lp = &lp;
+ spx_init_lp(csa->lp, P, parm->excl);
+ spx_alloc_lp(csa->lp);
+ map = talloc(1+P->m+P->n, int);
+ spx_build_lp(csa->lp, P, parm->excl, parm->shift, map);
+ spx_build_basis(csa->lp, P, map);
+ switch (P->dir)
+ { case GLP_MIN:
+ csa->dir = +1;
+ break;
+ case GLP_MAX:
+ csa->dir = -1;
+ break;
+ default:
+ xassert(P != P);
+ }
+#if SCALE_Z
+ csa->fz = 0.0;
+ for (k = 1; k <= csa->lp->n; k++)
+ { double t = fabs(csa->lp->c[k]);
+ if (csa->fz < t)
+ csa->fz = t;
+ }
+ if (csa->fz <= 1000.0)
+ csa->fz = 1.0;
+ else
+ csa->fz /= 1000.0;
+ /*xprintf("csa->fz = %g\n", csa->fz);*/
+ for (k = 0; k <= csa->lp->n; k++)
+ csa->lp->c[k] /= csa->fz;
+#endif
+ csa->orig_b = talloc(1+csa->lp->m, double);
+ memcpy(csa->orig_b, csa->lp->b, (1+csa->lp->m) * sizeof(double));
+ csa->orig_c = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_c, csa->lp->c, (1+csa->lp->n) * sizeof(double));
+ csa->orig_l = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_l, csa->lp->l, (1+csa->lp->n) * sizeof(double));
+ csa->orig_u = talloc(1+csa->lp->n, double);
+ memcpy(csa->orig_u, csa->lp->u, (1+csa->lp->n) * sizeof(double));
+ switch (parm->aorn)
+ { case GLP_USE_AT:
+ /* build matrix A in row-wise format */
+ csa->at = &at;
+ csa->nt = NULL;
+ spx_alloc_at(csa->lp, csa->at);
+ spx_build_at(csa->lp, csa->at);
+ break;
+ case GLP_USE_NT:
+ /* build matrix N in row-wise format for initial basis */
+ csa->at = NULL;
+ csa->nt = &nt;
+ spx_alloc_nt(csa->lp, csa->nt);
+ spx_init_nt(csa->lp, csa->nt);
+ spx_build_nt(csa->lp, csa->nt);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+ /* allocate and initialize working components */
+ csa->phase = 0;
+ csa->beta = talloc(1+csa->lp->m, double);
+ csa->beta_st = 0;
+ csa->d = talloc(1+csa->lp->n-csa->lp->m, double);
+ csa->d_st = 0;
+ switch (parm->pricing)
+ { case GLP_PT_STD:
+ csa->se = NULL;
+ break;
+ case GLP_PT_PSE:
+ csa->se = &se;
+ spy_alloc_se(csa->lp, csa->se);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+#if 0 /* 30/III-2016 */
+ csa->list = talloc(1+csa->lp->m, int);
+ csa->trow = talloc(1+csa->lp->n-csa->lp->m, double);
+ csa->tcol = talloc(1+csa->lp->m, double);
+#else
+ fvs_alloc_vec(&csa->r, csa->lp->m);
+ fvs_alloc_vec(&csa->trow, csa->lp->n-csa->lp->m);
+ fvs_alloc_vec(&csa->tcol, csa->lp->m);
+#endif
+#if 1 /* 16/III-2016 */
+ csa->bp = NULL;
+#endif
+ csa->work = talloc(1+csa->lp->m, double);
+ csa->work1 = talloc(1+csa->lp->n-csa->lp->m, double);
+#if 0 /* 11/VI-2017 */
+#if 1 /* 31/III-2016 */
+ fvs_alloc_vec(&csa->wrow, csa->lp->n-csa->lp->m);
+ fvs_alloc_vec(&csa->wcol, csa->lp->m);
+#endif
+#endif
+ /* initialize control parameters */
+ csa->msg_lev = parm->msg_lev;
+ csa->dualp = (parm->meth == GLP_DUALP);
+#if 0 /* 16/III-2016 */
+ switch (parm->r_test)
+ { case GLP_RT_STD:
+ csa->harris = 0;
+ break;
+ case GLP_RT_HAR:
+ csa->harris = 1;
+ break;
+ default:
+ xassert(parm != parm);
+ }
+#else
+ switch (parm->r_test)
+ { case GLP_RT_STD:
+ case GLP_RT_HAR:
+ break;
+ case GLP_RT_FLIP:
+ csa->bp = talloc(1+csa->lp->n-csa->lp->m, SPYBP);
+ break;
+ default:
+ xassert(parm != parm);
+ }
+ csa->r_test = parm->r_test;
+#endif
+ csa->tol_bnd = parm->tol_bnd;
+ csa->tol_bnd1 = .001 * parm->tol_bnd;
+ csa->tol_dj = parm->tol_dj;
+ csa->tol_dj1 = .001 * parm->tol_dj;
+#if 0
+ csa->tol_dj1 = 1e-9 * parm->tol_dj;
+#endif
+ csa->tol_piv = parm->tol_piv;
+ switch (P->dir)
+ { case GLP_MIN:
+ csa->obj_lim = + parm->obj_ul;
+ break;
+ case GLP_MAX:
+ csa->obj_lim = - parm->obj_ll;
+ break;
+ default:
+ xassert(parm != parm);
+ }
+#if SCALE_Z
+ if (csa->obj_lim != DBL_MAX)
+ csa->obj_lim /= csa->fz;
+#endif
+ csa->it_lim = parm->it_lim;
+ csa->tm_lim = parm->tm_lim;
+ csa->out_frq = parm->out_frq;
+ csa->out_dly = parm->out_dly;
+ /* initialize working parameters */
+ csa->tm_beg = xtime();
+ csa->it_beg = csa->it_cnt = P->it_cnt;
+ csa->it_dpy = -1;
+#if 1 /* 15/VII-2017 */
+ csa->tm_dpy = 0.0;
+#endif
+ csa->inv_cnt = 0;
+#if 1 /* 11/VII-2017 */
+ csa->degen = 0;
+#endif
+#if 1 /* 23/III-2016 */
+ csa->ns_cnt = csa->ls_cnt = 0;
+#endif
+ /* try to solve working LP */
+ ret = dual_simplex(csa);
+ /* return basis factorization back to problem object */
+ P->valid = csa->lp->valid;
+ P->bfd = csa->lp->bfd;
+ /* set solution status */
+ P->pbs_stat = csa->p_stat;
+ P->dbs_stat = csa->d_stat;
+ /* if the solver failed, do not store basis header and basic
+ * solution components to problem object */
+ if (ret == GLP_EFAIL)
+ goto skip;
+ /* convert working LP basis to original LP basis and store it to
+ * problem object */
+ daeh = talloc(1+csa->lp->n, int);
+ spx_store_basis(csa->lp, P, map, daeh);
+ /* compute simplex multipliers for final basic solution found by
+ * the solver */
+ spx_eval_pi(csa->lp, csa->work);
+ /* convert working LP solution to original LP solution and store
+ * it to problem object */
+#if SCALE_Z
+ for (i = 1; i <= csa->lp->m; i++)
+ csa->work[i] *= csa->fz;
+ for (j = 1; j <= csa->lp->n-csa->lp->m; j++)
+ csa->d[j] *= csa->fz;
+#endif
+ spx_store_sol(csa->lp, P, parm->shift, map, daeh, csa->beta,
+ csa->work, csa->d);
+ tfree(daeh);
+ /* save simplex iteration count */
+ P->it_cnt = csa->it_cnt;
+ /* report auxiliary/structural variable causing unboundedness */
+ P->some = 0;
+ if (csa->p_stat == GLP_NOFEAS && csa->d_stat == GLP_FEAS)
+ { int k, kk;
+ /* xB[p] = x[k] causes dual unboundedness */
+ xassert(1 <= csa->p && csa->p <= csa->lp->m);
+ k = csa->lp->head[csa->p];
+ xassert(1 <= k && k <= csa->lp->n);
+ /* convert to number of original variable */
+ for (kk = 1; kk <= P->m + P->n; kk++)
+ { if (abs(map[kk]) == k)
+ { P->some = kk;
+ break;
+ }
+ }
+ xassert(P->some != 0);
+ }
+skip: /* deallocate working objects and arrays */
+ spx_free_lp(csa->lp);
+ tfree(map);
+ tfree(csa->orig_b);
+ tfree(csa->orig_c);
+ tfree(csa->orig_l);
+ tfree(csa->orig_u);
+ if (csa->at != NULL)
+ spx_free_at(csa->lp, csa->at);
+ if (csa->nt != NULL)
+ spx_free_nt(csa->lp, csa->nt);
+ tfree(csa->beta);
+ tfree(csa->d);
+ if (csa->se != NULL)
+ spy_free_se(csa->lp, csa->se);
+#if 0 /* 30/III-2016 */
+ tfree(csa->list);
+ tfree(csa->trow);
+#else
+ fvs_free_vec(&csa->r);
+ fvs_free_vec(&csa->trow);
+#endif
+#if 1 /* 16/III-2016 */
+ if (csa->bp != NULL)
+ tfree(csa->bp);
+#endif
+#if 0 /* 29/III-2016 */
+ tfree(csa->tcol);
+#else
+ fvs_free_vec(&csa->tcol);
+#endif
+ tfree(csa->work);
+ tfree(csa->work1);
+#if 0 /* 11/VI-2017 */
+#if 1 /* 31/III-2016 */
+ fvs_free_vec(&csa->wrow);
+ fvs_free_vec(&csa->wcol);
+#endif
+#endif
+ /* return to calling program */
+ return ret >= 0 ? ret : GLP_EFAIL;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/zlib/README b/test/monniaux/glpk-4.65/src/zlib/README
new file mode 100644
index 00000000..2796312f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/README
@@ -0,0 +1,45 @@
+NOTE: Files in this subdirectory are NOT part of the GLPK package, but
+ are used with GLPK.
+
+ The original code was modified according to GLPK requirements by
+ Andrew Makhorin <mao@gnu.org>.
+
+ The following files were rewritten:
+ gzguts.h, zconf.h, zutil.h.
+
+ The following files were added:
+ zio.h, zio.c.
+
+ Other files were not changed.
+************************************************************************
+zlib general purpose compression library
+version 1.2.5, April 19th, 2010
+
+Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would
+ be appreciated but is not required.
+
+2. Altered source versions must be plainly marked as such, and must not
+ be misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any source
+ distribution.
+
+Jean-loup Gailly Mark Adler
+jloup@gzip.org madler@alumni.caltech.edu
+
+The data format used by the zlib library is described by RFCs (Request
+for Comments) 1950 to 1952 in the files
+http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
+format) and rfc1952.txt (gzip format).
diff --git a/test/monniaux/glpk-4.65/src/zlib/adler32.c b/test/monniaux/glpk-4.65/src/zlib/adler32.c
new file mode 100644
index 00000000..65ad6a5a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/adler32.c
@@ -0,0 +1,169 @@
+/* adler32.c -- compute the Adler-32 checksum of a data stream
+ * Copyright (C) 1995-2007 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+
+#define local static
+
+local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
+
+#define BASE 65521UL /* largest prime smaller than 65536 */
+#define NMAX 5552
+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
+
+#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
+#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf) DO8(buf,0); DO8(buf,8);
+
+/* use NO_DIVIDE if your processor does not do division in hardware */
+#ifdef NO_DIVIDE
+# define MOD(a) \
+ do { \
+ if (a >= (BASE << 16)) a -= (BASE << 16); \
+ if (a >= (BASE << 15)) a -= (BASE << 15); \
+ if (a >= (BASE << 14)) a -= (BASE << 14); \
+ if (a >= (BASE << 13)) a -= (BASE << 13); \
+ if (a >= (BASE << 12)) a -= (BASE << 12); \
+ if (a >= (BASE << 11)) a -= (BASE << 11); \
+ if (a >= (BASE << 10)) a -= (BASE << 10); \
+ if (a >= (BASE << 9)) a -= (BASE << 9); \
+ if (a >= (BASE << 8)) a -= (BASE << 8); \
+ if (a >= (BASE << 7)) a -= (BASE << 7); \
+ if (a >= (BASE << 6)) a -= (BASE << 6); \
+ if (a >= (BASE << 5)) a -= (BASE << 5); \
+ if (a >= (BASE << 4)) a -= (BASE << 4); \
+ if (a >= (BASE << 3)) a -= (BASE << 3); \
+ if (a >= (BASE << 2)) a -= (BASE << 2); \
+ if (a >= (BASE << 1)) a -= (BASE << 1); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+# define MOD4(a) \
+ do { \
+ if (a >= (BASE << 4)) a -= (BASE << 4); \
+ if (a >= (BASE << 3)) a -= (BASE << 3); \
+ if (a >= (BASE << 2)) a -= (BASE << 2); \
+ if (a >= (BASE << 1)) a -= (BASE << 1); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+#else
+# define MOD(a) a %= BASE
+# define MOD4(a) a %= BASE
+#endif
+
+/* ========================================================================= */
+uLong ZEXPORT adler32(adler, buf, len)
+ uLong adler;
+ const Bytef *buf;
+ uInt len;
+{
+ unsigned long sum2;
+ unsigned n;
+
+ /* split Adler-32 into component sums */
+ sum2 = (adler >> 16) & 0xffff;
+ adler &= 0xffff;
+
+ /* in case user likes doing a byte at a time, keep it fast */
+ if (len == 1) {
+ adler += buf[0];
+ if (adler >= BASE)
+ adler -= BASE;
+ sum2 += adler;
+ if (sum2 >= BASE)
+ sum2 -= BASE;
+ return adler | (sum2 << 16);
+ }
+
+ /* initial Adler-32 value (deferred check for len == 1 speed) */
+ if (buf == Z_NULL)
+ return 1L;
+
+ /* in case short lengths are provided, keep it somewhat fast */
+ if (len < 16) {
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ if (adler >= BASE)
+ adler -= BASE;
+ MOD4(sum2); /* only added so many BASE's */
+ return adler | (sum2 << 16);
+ }
+
+ /* do length NMAX blocks -- requires just one modulo operation */
+ while (len >= NMAX) {
+ len -= NMAX;
+ n = NMAX / 16; /* NMAX is divisible by 16 */
+ do {
+ DO16(buf); /* 16 sums unrolled */
+ buf += 16;
+ } while (--n);
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* do remaining bytes (less than NMAX, still just one modulo) */
+ if (len) { /* avoid modulos if none remaining */
+ while (len >= 16) {
+ len -= 16;
+ DO16(buf);
+ buf += 16;
+ }
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* return recombined sums */
+ return adler | (sum2 << 16);
+}
+
+/* ========================================================================= */
+local uLong adler32_combine_(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off64_t len2;
+{
+ unsigned long sum1;
+ unsigned long sum2;
+ unsigned rem;
+
+ /* the derivation of this formula is left as an exercise for the reader */
+ rem = (unsigned)(len2 % BASE);
+ sum1 = adler1 & 0xffff;
+ sum2 = rem * sum1;
+ MOD(sum2);
+ sum1 += (adler2 & 0xffff) + BASE - 1;
+ sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
+ if (sum2 >= BASE) sum2 -= BASE;
+ return sum1 | (sum2 << 16);
+}
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off_t len2;
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
+
+uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off64_t len2;
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/compress.c b/test/monniaux/glpk-4.65/src/zlib/compress.c
new file mode 100644
index 00000000..ea4dfbe9
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/compress.c
@@ -0,0 +1,80 @@
+/* compress.c -- compress a memory buffer
+ * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least 0.1% larger than sourceLen plus
+ 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+ int level;
+{
+ z_stream stream;
+ int err;
+
+ stream.next_in = (Bytef*)source;
+ stream.avail_in = (uInt)sourceLen;
+#ifdef MAXSEG_64K
+ /* Check for source > 64K on 16-bit machine: */
+ if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+#endif
+ stream.next_out = dest;
+ stream.avail_out = (uInt)*destLen;
+ if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+ stream.opaque = (voidpf)0;
+
+ err = deflateInit(&stream, level);
+ if (err != Z_OK) return err;
+
+ err = deflate(&stream, Z_FINISH);
+ if (err != Z_STREAM_END) {
+ deflateEnd(&stream);
+ return err == Z_OK ? Z_BUF_ERROR : err;
+ }
+ *destLen = stream.total_out;
+
+ err = deflateEnd(&stream);
+ return err;
+}
+
+/* ===========================================================================
+ */
+int ZEXPORT compress (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+{
+ return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
+}
+
+/* ===========================================================================
+ If the default memLevel or windowBits for deflateInit() is changed, then
+ this function needs to be updated.
+ */
+uLong ZEXPORT compressBound (sourceLen)
+ uLong sourceLen;
+{
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/crc32.c b/test/monniaux/glpk-4.65/src/zlib/crc32.c
new file mode 100644
index 00000000..91be372d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/crc32.c
@@ -0,0 +1,442 @@
+/* crc32.c -- compute the CRC-32 of a data stream
+ * Copyright (C) 1995-2006, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ *
+ * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
+ * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
+ * tables for updating the shift register in one step with three exclusive-ors
+ * instead of four steps with four exclusive-ors. This results in about a
+ * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
+ */
+
+/* @(#) $Id$ */
+
+/*
+ Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
+ protection on the static variables used to control the first-use generation
+ of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
+ first call get_crc_table() to initialize the tables before allowing more than
+ one thread to use crc32().
+ */
+
+#ifdef MAKECRCH
+# include <stdio.h>
+# ifndef DYNAMIC_CRC_TABLE
+# define DYNAMIC_CRC_TABLE
+# endif /* !DYNAMIC_CRC_TABLE */
+#endif /* MAKECRCH */
+
+#include "zutil.h" /* for STDC and FAR definitions */
+
+#define local static
+
+/* Find a four-byte integer type for crc32_little() and crc32_big(). */
+#ifndef NOBYFOUR
+# ifdef STDC /* need ANSI C limits.h to determine sizes */
+# include <limits.h>
+# define BYFOUR
+# if (UINT_MAX == 0xffffffffUL)
+ typedef unsigned int u4;
+# else
+# if (ULONG_MAX == 0xffffffffUL)
+ typedef unsigned long u4;
+# else
+# if (USHRT_MAX == 0xffffffffUL)
+ typedef unsigned short u4;
+# else
+# undef BYFOUR /* can't find a four-byte integer type! */
+# endif
+# endif
+# endif
+# endif /* STDC */
+#endif /* !NOBYFOUR */
+
+/* Definitions for doing the crc four data bytes at a time. */
+#ifdef BYFOUR
+# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
+ (((w)&0xff00)<<8)+(((w)&0xff)<<24))
+ local unsigned long crc32_little OF((unsigned long,
+ const unsigned char FAR *, unsigned));
+ local unsigned long crc32_big OF((unsigned long,
+ const unsigned char FAR *, unsigned));
+# define TBLS 8
+#else
+# define TBLS 1
+#endif /* BYFOUR */
+
+/* Local functions for crc concatenation */
+local unsigned long gf2_matrix_times OF((unsigned long *mat,
+ unsigned long vec));
+local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
+local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2);
+
+
+#ifdef DYNAMIC_CRC_TABLE
+
+local volatile int crc_table_empty = 1;
+local unsigned long FAR crc_table[TBLS][256];
+local void make_crc_table OF((void));
+#ifdef MAKECRCH
+ local void write_table OF((FILE *, const unsigned long FAR *));
+#endif /* MAKECRCH */
+/*
+ Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
+ x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
+
+ Polynomials over GF(2) are represented in binary, one bit per coefficient,
+ with the lowest powers in the most significant bit. Then adding polynomials
+ is just exclusive-or, and multiplying a polynomial by x is a right shift by
+ one. If we call the above polynomial p, and represent a byte as the
+ polynomial q, also with the lowest power in the most significant bit (so the
+ byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
+ where a mod b means the remainder after dividing a by b.
+
+ This calculation is done using the shift-register method of multiplying and
+ taking the remainder. The register is initialized to zero, and for each
+ incoming bit, x^32 is added mod p to the register if the bit is a one (where
+ x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
+ x (which is shifting right by one and adding x^32 mod p if the bit shifted
+ out is a one). We start with the highest power (least significant bit) of
+ q and repeat for all eight bits of q.
+
+ The first table is simply the CRC of all possible eight bit values. This is
+ all the information needed to generate CRCs on data a byte at a time for all
+ combinations of CRC register values and incoming bytes. The remaining tables
+ allow for word-at-a-time CRC calculation for both big-endian and little-
+ endian machines, where a word is four bytes.
+*/
+local void make_crc_table()
+{
+ unsigned long c;
+ int n, k;
+ unsigned long poly; /* polynomial exclusive-or pattern */
+ /* terms of polynomial defining this crc (except x^32): */
+ static volatile int first = 1; /* flag to limit concurrent making */
+ static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+ /* See if another task is already doing this (not thread-safe, but better
+ than nothing -- significantly reduces duration of vulnerability in
+ case the advice about DYNAMIC_CRC_TABLE is ignored) */
+ if (first) {
+ first = 0;
+
+ /* make exclusive-or pattern from polynomial (0xedb88320UL) */
+ poly = 0UL;
+ for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
+ poly |= 1UL << (31 - p[n]);
+
+ /* generate a crc for every 8-bit value */
+ for (n = 0; n < 256; n++) {
+ c = (unsigned long)n;
+ for (k = 0; k < 8; k++)
+ c = c & 1 ? poly ^ (c >> 1) : c >> 1;
+ crc_table[0][n] = c;
+ }
+
+#ifdef BYFOUR
+ /* generate crc for each value followed by one, two, and three zeros,
+ and then the byte reversal of those as well as the first table */
+ for (n = 0; n < 256; n++) {
+ c = crc_table[0][n];
+ crc_table[4][n] = REV(c);
+ for (k = 1; k < 4; k++) {
+ c = crc_table[0][c & 0xff] ^ (c >> 8);
+ crc_table[k][n] = c;
+ crc_table[k + 4][n] = REV(c);
+ }
+ }
+#endif /* BYFOUR */
+
+ crc_table_empty = 0;
+ }
+ else { /* not first */
+ /* wait for the other guy to finish (not efficient, but rare) */
+ while (crc_table_empty)
+ ;
+ }
+
+#ifdef MAKECRCH
+ /* write out CRC tables to crc32.h */
+ {
+ FILE *out;
+
+ out = fopen("crc32.h", "w");
+ if (out == NULL) return;
+ fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
+ fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
+ fprintf(out, "local const unsigned long FAR ");
+ fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
+ write_table(out, crc_table[0]);
+# ifdef BYFOUR
+ fprintf(out, "#ifdef BYFOUR\n");
+ for (k = 1; k < 8; k++) {
+ fprintf(out, " },\n {\n");
+ write_table(out, crc_table[k]);
+ }
+ fprintf(out, "#endif\n");
+# endif /* BYFOUR */
+ fprintf(out, " }\n};\n");
+ fclose(out);
+ }
+#endif /* MAKECRCH */
+}
+
+#ifdef MAKECRCH
+local void write_table(out, table)
+ FILE *out;
+ const unsigned long FAR *table;
+{
+ int n;
+
+ for (n = 0; n < 256; n++)
+ fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
+ n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
+}
+#endif /* MAKECRCH */
+
+#else /* !DYNAMIC_CRC_TABLE */
+/* ========================================================================
+ * Tables of CRC-32s of all single-byte values, made by make_crc_table().
+ */
+#include "crc32.h"
+#endif /* DYNAMIC_CRC_TABLE */
+
+/* =========================================================================
+ * This function can be used by asm versions of crc32()
+ */
+const unsigned long FAR * ZEXPORT get_crc_table()
+{
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+ return (const unsigned long FAR *)crc_table;
+}
+
+/* ========================================================================= */
+#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
+#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
+
+/* ========================================================================= */
+unsigned long ZEXPORT crc32(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ uInt len;
+{
+ if (buf == Z_NULL) return 0UL;
+
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+
+#ifdef BYFOUR
+ if (sizeof(void *) == sizeof(ptrdiff_t)) {
+ u4 endian;
+
+ endian = 1;
+ if (*((unsigned char *)(&endian)))
+ return crc32_little(crc, buf, len);
+ else
+ return crc32_big(crc, buf, len);
+ }
+#endif /* BYFOUR */
+ crc = crc ^ 0xffffffffUL;
+ while (len >= 8) {
+ DO8;
+ len -= 8;
+ }
+ if (len) do {
+ DO1;
+ } while (--len);
+ return crc ^ 0xffffffffUL;
+}
+
+#ifdef BYFOUR
+
+/* ========================================================================= */
+#define DOLIT4 c ^= *buf4++; \
+ c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
+ crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
+#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
+
+/* ========================================================================= */
+local unsigned long crc32_little(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ unsigned len;
+{
+ register u4 c;
+ register const u4 FAR *buf4;
+
+ c = (u4)crc;
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ len--;
+ }
+
+ buf4 = (const u4 FAR *)(const void FAR *)buf;
+ while (len >= 32) {
+ DOLIT32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOLIT4;
+ len -= 4;
+ }
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)c;
+}
+
+/* ========================================================================= */
+#define DOBIG4 c ^= *++buf4; \
+ c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
+ crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
+#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
+
+/* ========================================================================= */
+local unsigned long crc32_big(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ unsigned len;
+{
+ register u4 c;
+ register const u4 FAR *buf4;
+
+ c = REV((u4)crc);
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ len--;
+ }
+
+ buf4 = (const u4 FAR *)(const void FAR *)buf;
+ buf4--;
+ while (len >= 32) {
+ DOBIG32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOBIG4;
+ len -= 4;
+ }
+ buf4++;
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)(REV(c));
+}
+
+#endif /* BYFOUR */
+
+#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
+
+/* ========================================================================= */
+local unsigned long gf2_matrix_times(mat, vec)
+ unsigned long *mat;
+ unsigned long vec;
+{
+ unsigned long sum;
+
+ sum = 0;
+ while (vec) {
+ if (vec & 1)
+ sum ^= *mat;
+ vec >>= 1;
+ mat++;
+ }
+ return sum;
+}
+
+/* ========================================================================= */
+local void gf2_matrix_square(square, mat)
+ unsigned long *square;
+ unsigned long *mat;
+{
+ int n;
+
+ for (n = 0; n < GF2_DIM; n++)
+ square[n] = gf2_matrix_times(mat, mat[n]);
+}
+
+/* ========================================================================= */
+local uLong crc32_combine_(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off64_t len2;
+{
+ int n;
+ unsigned long row;
+ unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
+ unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
+
+ /* degenerate case (also disallow negative lengths) */
+ if (len2 <= 0)
+ return crc1;
+
+ /* put operator for one zero bit in odd */
+ odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
+ row = 1;
+ for (n = 1; n < GF2_DIM; n++) {
+ odd[n] = row;
+ row <<= 1;
+ }
+
+ /* put operator for two zero bits in even */
+ gf2_matrix_square(even, odd);
+
+ /* put operator for four zero bits in odd */
+ gf2_matrix_square(odd, even);
+
+ /* apply len2 zeros to crc1 (first square will put the operator for one
+ zero byte, eight zero bits, in even) */
+ do {
+ /* apply zeros operator for this bit of len2 */
+ gf2_matrix_square(even, odd);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(even, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ if (len2 == 0)
+ break;
+
+ /* another iteration of the loop with odd and even swapped */
+ gf2_matrix_square(odd, even);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(odd, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ } while (len2 != 0);
+
+ /* return combined crc */
+ crc1 ^= crc2;
+ return crc1;
+}
+
+/* ========================================================================= */
+uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off_t len2;
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
+
+uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off64_t len2;
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/crc32.h b/test/monniaux/glpk-4.65/src/zlib/crc32.h
new file mode 100644
index 00000000..8053b611
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/crc32.h
@@ -0,0 +1,441 @@
+/* crc32.h -- tables for rapid CRC calculation
+ * Generated automatically by crc32.c
+ */
+
+local const unsigned long FAR crc_table[TBLS][256] =
+{
+ {
+ 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
+ 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
+ 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
+ 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
+ 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
+ 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
+ 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
+ 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
+ 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
+ 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
+ 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
+ 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
+ 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
+ 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
+ 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
+ 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
+ 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
+ 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
+ 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
+ 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
+ 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
+ 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
+ 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
+ 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
+ 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
+ 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
+ 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
+ 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
+ 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
+ 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
+ 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
+ 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
+ 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
+ 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
+ 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
+ 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
+ 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
+ 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
+ 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
+ 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
+ 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
+ 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
+ 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
+ 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
+ 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
+ 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
+ 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
+ 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
+ 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
+ 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
+ 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
+ 0x2d02ef8dUL
+#ifdef BYFOUR
+ },
+ {
+ 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
+ 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
+ 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
+ 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
+ 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
+ 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
+ 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
+ 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
+ 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
+ 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
+ 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
+ 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
+ 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
+ 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
+ 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
+ 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
+ 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
+ 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
+ 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
+ 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
+ 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
+ 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
+ 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
+ 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
+ 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
+ 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
+ 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
+ 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
+ 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
+ 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
+ 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
+ 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
+ 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
+ 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
+ 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
+ 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
+ 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
+ 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
+ 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
+ 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
+ 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
+ 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
+ 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
+ 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
+ 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
+ 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
+ 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
+ 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
+ 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
+ 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
+ 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
+ 0x9324fd72UL
+ },
+ {
+ 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
+ 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
+ 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
+ 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
+ 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
+ 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
+ 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
+ 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
+ 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
+ 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
+ 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
+ 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
+ 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
+ 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
+ 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
+ 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
+ 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
+ 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
+ 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
+ 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
+ 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
+ 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
+ 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
+ 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
+ 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
+ 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
+ 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
+ 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
+ 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
+ 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
+ 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
+ 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
+ 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
+ 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
+ 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
+ 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
+ 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
+ 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
+ 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
+ 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
+ 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
+ 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
+ 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
+ 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
+ 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
+ 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
+ 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
+ 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
+ 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
+ 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
+ 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
+ 0xbe9834edUL
+ },
+ {
+ 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
+ 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
+ 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
+ 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
+ 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
+ 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
+ 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
+ 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
+ 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
+ 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
+ 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
+ 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
+ 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
+ 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
+ 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
+ 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
+ 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
+ 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
+ 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
+ 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
+ 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
+ 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
+ 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
+ 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
+ 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
+ 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
+ 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
+ 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
+ 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
+ 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
+ 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
+ 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
+ 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
+ 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
+ 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
+ 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
+ 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
+ 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
+ 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
+ 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
+ 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
+ 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
+ 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
+ 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
+ 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
+ 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
+ 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
+ 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
+ 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
+ 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
+ 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
+ 0xde0506f1UL
+ },
+ {
+ 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
+ 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
+ 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
+ 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
+ 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
+ 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
+ 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
+ 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
+ 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
+ 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
+ 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
+ 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
+ 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
+ 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
+ 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
+ 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
+ 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
+ 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
+ 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
+ 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
+ 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
+ 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
+ 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
+ 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
+ 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
+ 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
+ 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
+ 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
+ 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
+ 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
+ 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
+ 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
+ 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
+ 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
+ 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
+ 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
+ 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
+ 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
+ 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
+ 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
+ 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
+ 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
+ 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
+ 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
+ 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
+ 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
+ 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
+ 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
+ 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
+ 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
+ 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
+ 0x8def022dUL
+ },
+ {
+ 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
+ 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
+ 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
+ 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
+ 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
+ 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
+ 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
+ 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
+ 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
+ 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
+ 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
+ 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
+ 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
+ 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
+ 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
+ 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
+ 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
+ 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
+ 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
+ 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
+ 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
+ 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
+ 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
+ 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
+ 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
+ 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
+ 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
+ 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
+ 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
+ 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
+ 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
+ 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
+ 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
+ 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
+ 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
+ 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
+ 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
+ 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
+ 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
+ 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
+ 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
+ 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
+ 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
+ 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
+ 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
+ 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
+ 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
+ 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
+ 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
+ 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
+ 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
+ 0x72fd2493UL
+ },
+ {
+ 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
+ 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
+ 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
+ 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
+ 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
+ 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
+ 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
+ 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
+ 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
+ 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
+ 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
+ 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
+ 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
+ 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
+ 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
+ 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
+ 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
+ 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
+ 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
+ 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
+ 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
+ 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
+ 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
+ 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
+ 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
+ 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
+ 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
+ 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
+ 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
+ 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
+ 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
+ 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
+ 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
+ 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
+ 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
+ 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
+ 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
+ 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
+ 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
+ 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
+ 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
+ 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
+ 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
+ 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
+ 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
+ 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
+ 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
+ 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
+ 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
+ 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
+ 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
+ 0xed3498beUL
+ },
+ {
+ 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
+ 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
+ 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
+ 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
+ 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
+ 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
+ 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
+ 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
+ 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
+ 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
+ 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
+ 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
+ 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
+ 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
+ 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
+ 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
+ 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
+ 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
+ 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
+ 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
+ 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
+ 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
+ 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
+ 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
+ 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
+ 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
+ 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
+ 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
+ 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
+ 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
+ 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
+ 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
+ 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
+ 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
+ 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
+ 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
+ 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
+ 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
+ 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
+ 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
+ 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
+ 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
+ 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
+ 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
+ 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
+ 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
+ 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
+ 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
+ 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
+ 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
+ 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
+ 0xf10605deUL
+#endif
+ }
+};
diff --git a/test/monniaux/glpk-4.65/src/zlib/deflate.c b/test/monniaux/glpk-4.65/src/zlib/deflate.c
new file mode 100644
index 00000000..5c4022f3
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/deflate.c
@@ -0,0 +1,1834 @@
+/* deflate.c -- compress data using the deflation algorithm
+ * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process depends on being able to identify portions
+ * of the input text which are identical to earlier input (within a
+ * sliding window trailing behind the input currently being processed).
+ *
+ * The most straightforward technique turns out to be the fastest for
+ * most input files: try all possible matches and select the longest.
+ * The key feature of this algorithm is that insertions into the string
+ * dictionary are very simple and thus fast, and deletions are avoided
+ * completely. Insertions are performed at each input character, whereas
+ * string matches are performed only when the previous match ends. So it
+ * is preferable to spend more time in matches to allow very fast string
+ * insertions and avoid deletions. The matching algorithm for small
+ * strings is inspired from that of Rabin & Karp. A brute force approach
+ * is used to find longer strings when a small match has been found.
+ * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
+ * (by Leonid Broukhis).
+ * A previous version of this file used a more sophisticated algorithm
+ * (by Fiala and Greene) which is guaranteed to run in linear amortized
+ * time, but has a larger average cost, uses more memory and is patented.
+ * However the F&G algorithm may be faster for some highly redundant
+ * files if the parameter max_chain_length (described below) is too large.
+ *
+ * ACKNOWLEDGEMENTS
+ *
+ * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
+ * I found it in 'freeze' written by Leonid Broukhis.
+ * Thanks to many people for bug reports and testing.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
+ * Available in http://www.ietf.org/rfc/rfc1951.txt
+ *
+ * A description of the Rabin and Karp algorithm is given in the book
+ * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
+ *
+ * Fiala,E.R., and Greene,D.H.
+ * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
+ *
+ */
+
+/* @(#) $Id$ */
+
+#include "deflate.h"
+
+const char deflate_copyright[] =
+ " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/* ===========================================================================
+ * Function prototypes.
+ */
+typedef enum {
+ need_more, /* block not completed, need more input or more output */
+ block_done, /* block flush performed */
+ finish_started, /* finish started, need only more output at next deflate */
+ finish_done /* finish done, accept no more input or output */
+} block_state;
+
+typedef block_state (*compress_func) OF((deflate_state *s, int flush));
+/* Compression function. Returns the block state after the call. */
+
+local void fill_window OF((deflate_state *s));
+local block_state deflate_stored OF((deflate_state *s, int flush));
+local block_state deflate_fast OF((deflate_state *s, int flush));
+#ifndef FASTEST
+local block_state deflate_slow OF((deflate_state *s, int flush));
+#endif
+local block_state deflate_rle OF((deflate_state *s, int flush));
+local block_state deflate_huff OF((deflate_state *s, int flush));
+local void lm_init OF((deflate_state *s));
+local void putShortMSB OF((deflate_state *s, uInt b));
+local void flush_pending OF((z_streamp strm));
+local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
+#ifdef ASMV
+ void match_init OF((void)); /* asm code initialization */
+ uInt longest_match OF((deflate_state *s, IPos cur_match));
+#else
+local uInt longest_match OF((deflate_state *s, IPos cur_match));
+#endif
+
+#ifdef DEBUG
+local void check_match OF((deflate_state *s, IPos start, IPos match,
+ int length));
+#endif
+
+/* ===========================================================================
+ * Local data
+ */
+
+#define NIL 0
+/* Tail of hash chains */
+
+#ifndef TOO_FAR
+# define TOO_FAR 4096
+#endif
+/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
+
+/* Values for max_lazy_match, good_match and max_chain_length, depending on
+ * the desired pack level (0..9). The values given below have been tuned to
+ * exclude worst case performance for pathological files. Better values may be
+ * found for specific files.
+ */
+typedef struct config_s {
+ ush good_length; /* reduce lazy search above this match length */
+ ush max_lazy; /* do not perform lazy search above this match length */
+ ush nice_length; /* quit search above this match length */
+ ush max_chain;
+ compress_func func;
+} config;
+
+#ifdef FASTEST
+local const config configuration_table[2] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
+#else
+local const config configuration_table[10] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
+/* 2 */ {4, 5, 16, 8, deflate_fast},
+/* 3 */ {4, 6, 32, 32, deflate_fast},
+
+/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
+/* 5 */ {8, 16, 32, 32, deflate_slow},
+/* 6 */ {8, 16, 128, 128, deflate_slow},
+/* 7 */ {8, 32, 128, 256, deflate_slow},
+/* 8 */ {32, 128, 258, 1024, deflate_slow},
+/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
+#endif
+
+/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
+ * meaning.
+ */
+
+#define EQUAL 0
+/* result of memcmp for equal strings */
+
+#ifndef NO_DUMMY_DECL
+struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
+#endif
+
+/* ===========================================================================
+ * Update a hash value with the given input byte
+ * IN assertion: all calls to to UPDATE_HASH are made with consecutive
+ * input characters, so that a running hash key can be computed from the
+ * previous key instead of complete recalculation each time.
+ */
+#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
+
+
+/* ===========================================================================
+ * Insert string str in the dictionary and set match_head to the previous head
+ * of the hash chain (the most recent string with same hash key). Return
+ * the previous length of the hash chain.
+ * If this file is compiled with -DFASTEST, the compression level is forced
+ * to 1, and no hash chains are maintained.
+ * IN assertion: all calls to to INSERT_STRING are made with consecutive
+ * input characters and the first MIN_MATCH bytes of str are valid
+ * (except for the last MIN_MATCH-1 bytes of the input file).
+ */
+#ifdef FASTEST
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#else
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#endif
+
+/* ===========================================================================
+ * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
+ * prev[] will be initialized on the fly.
+ */
+#define CLEAR_HASH(s) \
+ s->head[s->hash_size-1] = NIL; \
+ zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
+
+/* ========================================================================= */
+int ZEXPORT deflateInit_(strm, level, version, stream_size)
+ z_streamp strm;
+ int level;
+ const char *version;
+ int stream_size;
+{
+ return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
+ Z_DEFAULT_STRATEGY, version, stream_size);
+ /* To do: ignore strm->next_in if we use it as window */
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
+ version, stream_size)
+ z_streamp strm;
+ int level;
+ int method;
+ int windowBits;
+ int memLevel;
+ int strategy;
+ const char *version;
+ int stream_size;
+{
+ deflate_state *s;
+ int wrap = 1;
+ static const char my_version[] = ZLIB_VERSION;
+
+ ushf *overlay;
+ /* We overlay pending_buf and d_buf+l_buf. This works since the average
+ * output size for (length,distance) codes is <= 24 bits.
+ */
+
+ if (version == Z_NULL || version[0] != my_version[0] ||
+ stream_size != sizeof(z_stream)) {
+ return Z_VERSION_ERROR;
+ }
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+
+ strm->msg = Z_NULL;
+ if (strm->zalloc == (alloc_func)0) {
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+ }
+ if (strm->zfree == (free_func)0) strm->zfree = zcfree;
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+
+ if (windowBits < 0) { /* suppress zlib wrapper */
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+#ifdef GZIP
+ else if (windowBits > 15) {
+ wrap = 2; /* write gzip wrapper instead */
+ windowBits -= 16;
+ }
+#endif
+ if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
+ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
+ strategy < 0 || strategy > Z_FIXED) {
+ return Z_STREAM_ERROR;
+ }
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
+ s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
+ if (s == Z_NULL) return Z_MEM_ERROR;
+ strm->state = (struct internal_state FAR *)s;
+ s->strm = strm;
+
+ s->wrap = wrap;
+ s->gzhead = Z_NULL;
+ s->w_bits = windowBits;
+ s->w_size = 1 << s->w_bits;
+ s->w_mask = s->w_size - 1;
+
+ s->hash_bits = memLevel + 7;
+ s->hash_size = 1 << s->hash_bits;
+ s->hash_mask = s->hash_size - 1;
+ s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
+
+ s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
+ s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
+ s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
+
+ s->high_water = 0; /* nothing written to s->window yet */
+
+ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
+
+ overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
+ s->pending_buf = (uchf *) overlay;
+ s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
+
+ if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
+ s->pending_buf == Z_NULL) {
+ s->status = FINISH_STATE;
+ strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
+ deflateEnd (strm);
+ return Z_MEM_ERROR;
+ }
+ s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
+ s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
+
+ s->level = level;
+ s->strategy = strategy;
+ s->method = (Byte)method;
+
+ return deflateReset(strm);
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
+ z_streamp strm;
+ const Bytef *dictionary;
+ uInt dictLength;
+{
+ deflate_state *s;
+ uInt length = dictLength;
+ uInt n;
+ IPos hash_head = 0;
+
+ if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
+ strm->state->wrap == 2 ||
+ (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
+ return Z_STREAM_ERROR;
+
+ s = strm->state;
+ if (s->wrap)
+ strm->adler = adler32(strm->adler, dictionary, dictLength);
+
+ if (length < MIN_MATCH) return Z_OK;
+ if (length > s->w_size) {
+ length = s->w_size;
+ dictionary += dictLength - length; /* use the tail of the dictionary */
+ }
+ zmemcpy(s->window, dictionary, length);
+ s->strstart = length;
+ s->block_start = (long)length;
+
+ /* Insert all strings in the hash table (except for the last two bytes).
+ * s->lookahead stays null, so s->ins_h will be recomputed at the next
+ * call of fill_window.
+ */
+ s->ins_h = s->window[0];
+ UPDATE_HASH(s, s->ins_h, s->window[1]);
+ for (n = 0; n <= length - MIN_MATCH; n++) {
+ INSERT_STRING(s, n, hash_head);
+ }
+ if (hash_head) hash_head = 0; /* to make compiler happy */
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateReset (strm)
+ z_streamp strm;
+{
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL ||
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
+ return Z_STREAM_ERROR;
+ }
+
+ strm->total_in = strm->total_out = 0;
+ strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
+ strm->data_type = Z_UNKNOWN;
+
+ s = (deflate_state *)strm->state;
+ s->pending = 0;
+ s->pending_out = s->pending_buf;
+
+ if (s->wrap < 0) {
+ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
+ }
+ s->status = s->wrap ? INIT_STATE : BUSY_STATE;
+ strm->adler =
+#ifdef GZIP
+ s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
+#endif
+ adler32(0L, Z_NULL, 0);
+ s->last_flush = Z_NO_FLUSH;
+
+ _tr_init(s);
+ lm_init(s);
+
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetHeader (strm, head)
+ z_streamp strm;
+ gz_headerp head;
+{
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ if (strm->state->wrap != 2) return Z_STREAM_ERROR;
+ strm->state->gzhead = head;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflatePrime (strm, bits, value)
+ z_streamp strm;
+ int bits;
+ int value;
+{
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ strm->state->bi_valid = bits;
+ strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateParams(strm, level, strategy)
+ z_streamp strm;
+ int level;
+ int strategy;
+{
+ deflate_state *s;
+ compress_func func;
+ int err = Z_OK;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ s = strm->state;
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+ if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
+ return Z_STREAM_ERROR;
+ }
+ func = configuration_table[s->level].func;
+
+ if ((strategy != s->strategy || func != configuration_table[level].func) &&
+ strm->total_in != 0) {
+ /* Flush the last buffer: */
+ err = deflate(strm, Z_BLOCK);
+ }
+ if (s->level != level) {
+ s->level = level;
+ s->max_lazy_match = configuration_table[level].max_lazy;
+ s->good_match = configuration_table[level].good_length;
+ s->nice_match = configuration_table[level].nice_length;
+ s->max_chain_length = configuration_table[level].max_chain;
+ }
+ s->strategy = strategy;
+ return err;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
+ z_streamp strm;
+ int good_length;
+ int max_lazy;
+ int nice_length;
+ int max_chain;
+{
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ s = strm->state;
+ s->good_match = good_length;
+ s->max_lazy_match = max_lazy;
+ s->nice_match = nice_length;
+ s->max_chain_length = max_chain;
+ return Z_OK;
+}
+
+/* =========================================================================
+ * For the default windowBits of 15 and memLevel of 8, this function returns
+ * a close to exact, as well as small, upper bound on the compressed size.
+ * They are coded as constants here for a reason--if the #define's are
+ * changed, then this function needs to be changed as well. The return
+ * value for 15 and 8 only works for those exact settings.
+ *
+ * For any setting other than those defaults for windowBits and memLevel,
+ * the value returned is a conservative worst case for the maximum expansion
+ * resulting from using fixed blocks instead of stored blocks, which deflate
+ * can emit on compressed data for some combinations of the parameters.
+ *
+ * This function could be more sophisticated to provide closer upper bounds for
+ * every combination of windowBits and memLevel. But even the conservative
+ * upper bound of about 14% expansion does not seem onerous for output buffer
+ * allocation.
+ */
+uLong ZEXPORT deflateBound(strm, sourceLen)
+ z_streamp strm;
+ uLong sourceLen;
+{
+ deflate_state *s;
+ uLong complen, wraplen;
+ Bytef *str;
+
+ /* conservative upper bound for compressed data */
+ complen = sourceLen +
+ ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
+
+ /* if can't get parameters, return conservative bound plus zlib wrapper */
+ if (strm == Z_NULL || strm->state == Z_NULL)
+ return complen + 6;
+
+ /* compute wrapper length */
+ s = strm->state;
+ switch (s->wrap) {
+ case 0: /* raw deflate */
+ wraplen = 0;
+ break;
+ case 1: /* zlib wrapper */
+ wraplen = 6 + (s->strstart ? 4 : 0);
+ break;
+ case 2: /* gzip wrapper */
+ wraplen = 18;
+ if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
+ if (s->gzhead->extra != Z_NULL)
+ wraplen += 2 + s->gzhead->extra_len;
+ str = s->gzhead->name;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ str = s->gzhead->comment;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ if (s->gzhead->hcrc)
+ wraplen += 2;
+ }
+ break;
+ default: /* for compiler happiness */
+ wraplen = 6;
+ }
+
+ /* if not default parameters, return conservative bound */
+ if (s->w_bits != 15 || s->hash_bits != 8 + 7)
+ return complen + wraplen;
+
+ /* default settings: return tight bound for that case */
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13 - 6 + wraplen;
+}
+
+/* =========================================================================
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
+ * IN assertion: the stream state is correct and there is enough room in
+ * pending_buf.
+ */
+local void putShortMSB (s, b)
+ deflate_state *s;
+ uInt b;
+{
+ put_byte(s, (Byte)(b >> 8));
+ put_byte(s, (Byte)(b & 0xff));
+}
+
+/* =========================================================================
+ * Flush as much pending output as possible. All deflate() output goes
+ * through this function so some applications may wish to modify it
+ * to avoid allocating a large strm->next_out buffer and copying into it.
+ * (See also read_buf()).
+ */
+local void flush_pending(strm)
+ z_streamp strm;
+{
+ unsigned len = strm->state->pending;
+
+ if (len > strm->avail_out) len = strm->avail_out;
+ if (len == 0) return;
+
+ zmemcpy(strm->next_out, strm->state->pending_out, len);
+ strm->next_out += len;
+ strm->state->pending_out += len;
+ strm->total_out += len;
+ strm->avail_out -= len;
+ strm->state->pending -= len;
+ if (strm->state->pending == 0) {
+ strm->state->pending_out = strm->state->pending_buf;
+ }
+}
+
+/* ========================================================================= */
+int ZEXPORT deflate (strm, flush)
+ z_streamp strm;
+ int flush;
+{
+ int old_flush; /* value of flush param for previous deflate call */
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL ||
+ flush > Z_BLOCK || flush < 0) {
+ return Z_STREAM_ERROR;
+ }
+ s = strm->state;
+
+ if (strm->next_out == Z_NULL ||
+ (strm->next_in == Z_NULL && strm->avail_in != 0) ||
+ (s->status == FINISH_STATE && flush != Z_FINISH)) {
+ ERR_RETURN(strm, Z_STREAM_ERROR);
+ }
+ if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
+
+ s->strm = strm; /* just in case */
+ old_flush = s->last_flush;
+ s->last_flush = flush;
+
+ /* Write the header */
+ if (s->status == INIT_STATE) {
+#ifdef GZIP
+ if (s->wrap == 2) {
+ strm->adler = crc32(0L, Z_NULL, 0);
+ put_byte(s, 31);
+ put_byte(s, 139);
+ put_byte(s, 8);
+ if (s->gzhead == Z_NULL) {
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, OS_CODE);
+ s->status = BUSY_STATE;
+ }
+ else {
+ put_byte(s, (s->gzhead->text ? 1 : 0) +
+ (s->gzhead->hcrc ? 2 : 0) +
+ (s->gzhead->extra == Z_NULL ? 0 : 4) +
+ (s->gzhead->name == Z_NULL ? 0 : 8) +
+ (s->gzhead->comment == Z_NULL ? 0 : 16)
+ );
+ put_byte(s, (Byte)(s->gzhead->time & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, s->gzhead->os & 0xff);
+ if (s->gzhead->extra != Z_NULL) {
+ put_byte(s, s->gzhead->extra_len & 0xff);
+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
+ }
+ if (s->gzhead->hcrc)
+ strm->adler = crc32(strm->adler, s->pending_buf,
+ s->pending);
+ s->gzindex = 0;
+ s->status = EXTRA_STATE;
+ }
+ }
+ else
+#endif
+ {
+ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
+ uInt level_flags;
+
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
+ level_flags = 0;
+ else if (s->level < 6)
+ level_flags = 1;
+ else if (s->level == 6)
+ level_flags = 2;
+ else
+ level_flags = 3;
+ header |= (level_flags << 6);
+ if (s->strstart != 0) header |= PRESET_DICT;
+ header += 31 - (header % 31);
+
+ s->status = BUSY_STATE;
+ putShortMSB(s, header);
+
+ /* Save the adler32 of the preset dictionary: */
+ if (s->strstart != 0) {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ strm->adler = adler32(0L, Z_NULL, 0);
+ }
+ }
+#ifdef GZIP
+ if (s->status == EXTRA_STATE) {
+ if (s->gzhead->extra != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+
+ while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size)
+ break;
+ }
+ put_byte(s, s->gzhead->extra[s->gzindex]);
+ s->gzindex++;
+ }
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (s->gzindex == s->gzhead->extra_len) {
+ s->gzindex = 0;
+ s->status = NAME_STATE;
+ }
+ }
+ else
+ s->status = NAME_STATE;
+ }
+ if (s->status == NAME_STATE) {
+ if (s->gzhead->name != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+ int val;
+
+ do {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size) {
+ val = 1;
+ break;
+ }
+ }
+ val = s->gzhead->name[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (val == 0) {
+ s->gzindex = 0;
+ s->status = COMMENT_STATE;
+ }
+ }
+ else
+ s->status = COMMENT_STATE;
+ }
+ if (s->status == COMMENT_STATE) {
+ if (s->gzhead->comment != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+ int val;
+
+ do {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size) {
+ val = 1;
+ break;
+ }
+ }
+ val = s->gzhead->comment[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (val == 0)
+ s->status = HCRC_STATE;
+ }
+ else
+ s->status = HCRC_STATE;
+ }
+ if (s->status == HCRC_STATE) {
+ if (s->gzhead->hcrc) {
+ if (s->pending + 2 > s->pending_buf_size)
+ flush_pending(strm);
+ if (s->pending + 2 <= s->pending_buf_size) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ strm->adler = crc32(0L, Z_NULL, 0);
+ s->status = BUSY_STATE;
+ }
+ }
+ else
+ s->status = BUSY_STATE;
+ }
+#endif
+
+ /* Flush as much pending output as possible */
+ if (s->pending != 0) {
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ /* Since avail_out is 0, deflate will be called again with
+ * more output space, but possibly with both pending and
+ * avail_in equal to zero. There won't be anything to do,
+ * but this is not an error situation so make sure we
+ * return OK instead of BUF_ERROR at next call of deflate:
+ */
+ s->last_flush = -1;
+ return Z_OK;
+ }
+
+ /* Make sure there is something to do and avoid duplicate consecutive
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
+ */
+ } else if (strm->avail_in == 0 && flush <= old_flush &&
+ flush != Z_FINISH) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* User must not provide more input after the first FINISH: */
+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* Start a new block or continue the current one.
+ */
+ if (strm->avail_in != 0 || s->lookahead != 0 ||
+ (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
+ block_state bstate;
+
+ bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
+ (s->strategy == Z_RLE ? deflate_rle(s, flush) :
+ (*(configuration_table[s->level].func))(s, flush));
+
+ if (bstate == finish_started || bstate == finish_done) {
+ s->status = FINISH_STATE;
+ }
+ if (bstate == need_more || bstate == finish_started) {
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
+ }
+ return Z_OK;
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
+ * of deflate should use the same flush parameter to make sure
+ * that the flush is complete. So we don't have to output an
+ * empty block here, this will be done at next call. This also
+ * ensures that for a very small output buffer, we emit at most
+ * one empty block.
+ */
+ }
+ if (bstate == block_done) {
+ if (flush == Z_PARTIAL_FLUSH) {
+ _tr_align(s);
+ } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
+ _tr_stored_block(s, (char*)0, 0L, 0);
+ /* For a full flush, this empty block will be recognized
+ * as a special marker by inflate_sync().
+ */
+ if (flush == Z_FULL_FLUSH) {
+ CLEAR_HASH(s); /* forget history */
+ if (s->lookahead == 0) {
+ s->strstart = 0;
+ s->block_start = 0L;
+ }
+ }
+ }
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
+ return Z_OK;
+ }
+ }
+ }
+ Assert(strm->avail_out > 0, "bug2");
+
+ if (flush != Z_FINISH) return Z_OK;
+ if (s->wrap <= 0) return Z_STREAM_END;
+
+ /* Write the trailer */
+#ifdef GZIP
+ if (s->wrap == 2) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
+ put_byte(s, (Byte)(strm->total_in & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
+ }
+ else
+#endif
+ {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ flush_pending(strm);
+ /* If avail_out is zero, the application will call deflate again
+ * to flush the rest.
+ */
+ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
+ return s->pending != 0 ? Z_OK : Z_STREAM_END;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateEnd (strm)
+ z_streamp strm;
+{
+ int status;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+
+ status = strm->state->status;
+ if (status != INIT_STATE &&
+ status != EXTRA_STATE &&
+ status != NAME_STATE &&
+ status != COMMENT_STATE &&
+ status != HCRC_STATE &&
+ status != BUSY_STATE &&
+ status != FINISH_STATE) {
+ return Z_STREAM_ERROR;
+ }
+
+ /* Deallocate in reverse order of allocations: */
+ TRY_FREE(strm, strm->state->pending_buf);
+ TRY_FREE(strm, strm->state->head);
+ TRY_FREE(strm, strm->state->prev);
+ TRY_FREE(strm, strm->state->window);
+
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+
+ return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
+}
+
+/* =========================================================================
+ * Copy the source state to the destination state.
+ * To simplify the source, this is not supported for 16-bit MSDOS (which
+ * doesn't have enough memory anyway to duplicate compression states).
+ */
+int ZEXPORT deflateCopy (dest, source)
+ z_streamp dest;
+ z_streamp source;
+{
+#ifdef MAXSEG_64K
+ return Z_STREAM_ERROR;
+#else
+ deflate_state *ds;
+ deflate_state *ss;
+ ushf *overlay;
+
+
+ if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
+ return Z_STREAM_ERROR;
+ }
+
+ ss = source->state;
+
+ zmemcpy(dest, source, sizeof(z_stream));
+
+ ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
+ if (ds == Z_NULL) return Z_MEM_ERROR;
+ dest->state = (struct internal_state FAR *) ds;
+ zmemcpy(ds, ss, sizeof(deflate_state));
+ ds->strm = dest;
+
+ ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
+ ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
+ ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
+ overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
+ ds->pending_buf = (uchf *) overlay;
+
+ if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
+ ds->pending_buf == Z_NULL) {
+ deflateEnd (dest);
+ return Z_MEM_ERROR;
+ }
+ /* following zmemcpy do not work for 16-bit MSDOS */
+ zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
+ zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
+ zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
+ zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
+
+ ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
+ ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
+ ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
+
+ ds->l_desc.dyn_tree = ds->dyn_ltree;
+ ds->d_desc.dyn_tree = ds->dyn_dtree;
+ ds->bl_desc.dyn_tree = ds->bl_tree;
+
+ return Z_OK;
+#endif /* MAXSEG_64K */
+}
+
+/* ===========================================================================
+ * Read a new buffer from the current input stream, update the adler32
+ * and total number of bytes read. All deflate() input goes through
+ * this function so some applications may wish to modify it to avoid
+ * allocating a large strm->next_in buffer and copying from it.
+ * (See also flush_pending()).
+ */
+local int read_buf(strm, buf, size)
+ z_streamp strm;
+ Bytef *buf;
+ unsigned size;
+{
+ unsigned len = strm->avail_in;
+
+ if (len > size) len = size;
+ if (len == 0) return 0;
+
+ strm->avail_in -= len;
+
+ if (strm->state->wrap == 1) {
+ strm->adler = adler32(strm->adler, strm->next_in, len);
+ }
+#ifdef GZIP
+ else if (strm->state->wrap == 2) {
+ strm->adler = crc32(strm->adler, strm->next_in, len);
+ }
+#endif
+ zmemcpy(buf, strm->next_in, len);
+ strm->next_in += len;
+ strm->total_in += len;
+
+ return (int)len;
+}
+
+/* ===========================================================================
+ * Initialize the "longest match" routines for a new zlib stream
+ */
+local void lm_init (s)
+ deflate_state *s;
+{
+ s->window_size = (ulg)2L*s->w_size;
+
+ CLEAR_HASH(s);
+
+ /* Set the default configuration parameters:
+ */
+ s->max_lazy_match = configuration_table[s->level].max_lazy;
+ s->good_match = configuration_table[s->level].good_length;
+ s->nice_match = configuration_table[s->level].nice_length;
+ s->max_chain_length = configuration_table[s->level].max_chain;
+
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->lookahead = 0;
+ s->match_length = s->prev_length = MIN_MATCH-1;
+ s->match_available = 0;
+ s->ins_h = 0;
+#ifndef FASTEST
+#ifdef ASMV
+ match_init(); /* initialize the asm code */
+#endif
+#endif
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Set match_start to the longest match starting at the given string and
+ * return its length. Matches shorter or equal to prev_length are discarded,
+ * in which case the result is equal to prev_length and match_start is
+ * garbage.
+ * IN assertions: cur_match is the head of the hash chain for the current
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
+ * OUT assertion: the match length is not greater than s->lookahead.
+ */
+#ifndef ASMV
+/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
+ * match.S. The code will be functionally equivalent.
+ */
+local uInt longest_match(s, cur_match)
+ deflate_state *s;
+ IPos cur_match; /* current match */
+{
+ unsigned chain_length = s->max_chain_length;/* max hash chain length */
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ int best_len = s->prev_length; /* best match length so far */
+ int nice_match = s->nice_match; /* stop if match long enough */
+ IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
+ s->strstart - (IPos)MAX_DIST(s) : NIL;
+ /* Stop when cur_match becomes <= limit. To simplify the code,
+ * we prevent matches with the string of window index 0.
+ */
+ Posf *prev = s->prev;
+ uInt wmask = s->w_mask;
+
+#ifdef UNALIGNED_OK
+ /* Compare two bytes at a time. Note: this is not always beneficial.
+ * Try with and without -DUNALIGNED_OK to check.
+ */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
+ register ush scan_start = *(ushf*)scan;
+ register ush scan_end = *(ushf*)(scan+best_len-1);
+#else
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+ register Byte scan_end1 = scan[best_len-1];
+ register Byte scan_end = scan[best_len];
+#endif
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ /* Do not waste too much time if we already have a good match: */
+ if (s->prev_length >= s->good_match) {
+ chain_length >>= 2;
+ }
+ /* Do not look for matches beyond the end of the input. This is necessary
+ * to make deflate deterministic.
+ */
+ if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ do {
+ Assert(cur_match < s->strstart, "no future");
+ match = s->window + cur_match;
+
+ /* Skip to next match if the match length cannot increase
+ * or if the match length is less than 2. Note that the checks below
+ * for insufficient lookahead only occur occasionally for performance
+ * reasons. Therefore uninitialized memory will be accessed, and
+ * conditional jumps will be made that depend on those values.
+ * However the length of the match is limited to the lookahead, so
+ * the output of deflate is not affected by the uninitialized values.
+ */
+#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
+ /* This code assumes sizeof(unsigned short) == 2. Do not use
+ * UNALIGNED_OK if your compiler uses a different size.
+ */
+ if (*(ushf*)(match+best_len-1) != scan_end ||
+ *(ushf*)match != scan_start) continue;
+
+ /* It is not necessary to compare scan[2] and match[2] since they are
+ * always equal when the other bytes match, given that the hash keys
+ * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
+ * strstart+3, +5, ... up to strstart+257. We check for insufficient
+ * lookahead only every 4th comparison; the 128th check will be made
+ * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
+ * necessary to put more guard bytes at the end of the window, or
+ * to check more often for insufficient lookahead.
+ */
+ Assert(scan[2] == match[2], "scan[2]?");
+ scan++, match++;
+ do {
+ } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ scan < strend);
+ /* The funny "do {}" generates better code on most compilers */
+
+ /* Here, scan <= window+strstart+257 */
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+ if (*scan == *match) scan++;
+
+ len = (MAX_MATCH - 1) - (int)(strend-scan);
+ scan = strend - (MAX_MATCH-1);
+
+#else /* UNALIGNED_OK */
+
+ if (match[best_len] != scan_end ||
+ match[best_len-1] != scan_end1 ||
+ *match != *scan ||
+ *++match != scan[1]) continue;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match++;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+ scan = strend - MAX_MATCH;
+
+#endif /* UNALIGNED_OK */
+
+ if (len > best_len) {
+ s->match_start = cur_match;
+ best_len = len;
+ if (len >= nice_match) break;
+#ifdef UNALIGNED_OK
+ scan_end = *(ushf*)(scan+best_len-1);
+#else
+ scan_end1 = scan[best_len-1];
+ scan_end = scan[best_len];
+#endif
+ }
+ } while ((cur_match = prev[cur_match & wmask]) > limit
+ && --chain_length != 0);
+
+ if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
+ return s->lookahead;
+}
+#endif /* ASMV */
+
+#else /* FASTEST */
+
+/* ---------------------------------------------------------------------------
+ * Optimized version for FASTEST only
+ */
+local uInt longest_match(s, cur_match)
+ deflate_state *s;
+ IPos cur_match; /* current match */
+{
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ Assert(cur_match < s->strstart, "no future");
+
+ match = s->window + cur_match;
+
+ /* Return failure if the match length is less than 2:
+ */
+ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match += 2;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+
+ if (len < MIN_MATCH) return MIN_MATCH - 1;
+
+ s->match_start = cur_match;
+ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
+}
+
+#endif /* FASTEST */
+
+#ifdef DEBUG
+/* ===========================================================================
+ * Check that the match at match_start is indeed a match.
+ */
+local void check_match(s, start, match, length)
+ deflate_state *s;
+ IPos start, match;
+ int length;
+{
+ /* check that the match is indeed a match */
+ if (zmemcmp(s->window + match,
+ s->window + start, length) != EQUAL) {
+ fprintf(stderr, " start %u, match %u, length %d\n",
+ start, match, length);
+ do {
+ fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
+ } while (--length != 0);
+ z_error("invalid match");
+ }
+ if (z_verbose > 1) {
+ fprintf(stderr,"\\[%d,%d]", start-match, length);
+ do { putc(s->window[start++], stderr); } while (--length != 0);
+ }
+}
+#else
+# define check_match(s, start, match, length)
+#endif /* DEBUG */
+
+/* ===========================================================================
+ * Fill the window when the lookahead becomes insufficient.
+ * Updates strstart and lookahead.
+ *
+ * IN assertion: lookahead < MIN_LOOKAHEAD
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
+ * At least one byte has been read, or avail_in == 0; reads are
+ * performed for at least two bytes (required for the zip translate_eol
+ * option -- not supported here).
+ */
+local void fill_window(s)
+ deflate_state *s;
+{
+ register unsigned n, m;
+ register Posf *p;
+ unsigned more; /* Amount of free space at the end of the window. */
+ uInt wsize = s->w_size;
+
+ do {
+ more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
+
+ /* Deal with !@#$% 64K limit: */
+ if (sizeof(int) <= 2) {
+ if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
+ more = wsize;
+
+ } else if (more == (unsigned)(-1)) {
+ /* Very unlikely, but possible on 16 bit machine if
+ * strstart == 0 && lookahead == 1 (input done a byte at time)
+ */
+ more--;
+ }
+ }
+
+ /* If the window is almost full and there is insufficient lookahead,
+ * move the upper half to the lower one to make room in the upper half.
+ */
+ if (s->strstart >= wsize+MAX_DIST(s)) {
+
+ zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
+ s->match_start -= wsize;
+ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
+ s->block_start -= (long) wsize;
+
+ /* Slide the hash table (could be avoided with 32 bit values
+ at the expense of memory usage). We slide even when level == 0
+ to keep the hash table consistent if we switch back to level > 0
+ later. (Using level 0 permanently is not an optimal usage of
+ zlib, so we don't care about this pathological case.)
+ */
+ n = s->hash_size;
+ p = &s->head[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m-wsize : NIL);
+ } while (--n);
+
+ n = wsize;
+#ifndef FASTEST
+ p = &s->prev[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m-wsize : NIL);
+ /* If n is not on any hash chain, prev[n] is garbage but
+ * its value will never be used.
+ */
+ } while (--n);
+#endif
+ more += wsize;
+ }
+ if (s->strm->avail_in == 0) return;
+
+ /* If there was no sliding:
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
+ * more == window_size - lookahead - strstart
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
+ * => more >= window_size - 2*WSIZE + 2
+ * In the BIG_MEM or MMAP case (not yet supported),
+ * window_size == input_size + MIN_LOOKAHEAD &&
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
+ */
+ Assert(more >= 2, "more < 2");
+
+ n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
+ s->lookahead += n;
+
+ /* Initialize the hash value now that we have some input: */
+ if (s->lookahead >= MIN_MATCH) {
+ s->ins_h = s->window[s->strstart];
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ }
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
+ * but this is not important since only literal bytes will be emitted.
+ */
+
+ } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+
+ /* If the WIN_INIT bytes after the end of the current data have never been
+ * written, then zero those bytes in order to avoid memory check reports of
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
+ * the longest match routines. Update the high water mark for the next
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
+ */
+ if (s->high_water < s->window_size) {
+ ulg curr = s->strstart + (ulg)(s->lookahead);
+ ulg init;
+
+ if (s->high_water < curr) {
+ /* Previous high water mark below current data -- zero WIN_INIT
+ * bytes or up to end of window, whichever is less.
+ */
+ init = s->window_size - curr;
+ if (init > WIN_INIT)
+ init = WIN_INIT;
+ zmemzero(s->window + curr, (unsigned)init);
+ s->high_water = curr + init;
+ }
+ else if (s->high_water < (ulg)curr + WIN_INIT) {
+ /* High water mark at or above current data, but below current data
+ * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
+ * to end of window, whichever is less.
+ */
+ init = (ulg)curr + WIN_INIT - s->high_water;
+ if (init > s->window_size - s->high_water)
+ init = s->window_size - s->high_water;
+ zmemzero(s->window + s->high_water, (unsigned)init);
+ s->high_water += init;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Flush the current block, with given end-of-file flag.
+ * IN assertion: strstart is set to the end of the current match.
+ */
+#define FLUSH_BLOCK_ONLY(s, last) { \
+ _tr_flush_block(s, (s->block_start >= 0L ? \
+ (charf *)&s->window[(unsigned)s->block_start] : \
+ (charf *)Z_NULL), \
+ (ulg)((long)s->strstart - s->block_start), \
+ (last)); \
+ s->block_start = s->strstart; \
+ flush_pending(s->strm); \
+ Tracev((stderr,"[FLUSH]")); \
+}
+
+/* Same but force premature exit if necessary. */
+#define FLUSH_BLOCK(s, last) { \
+ FLUSH_BLOCK_ONLY(s, last); \
+ if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
+}
+
+/* ===========================================================================
+ * Copy without compression as much as possible from the input stream, return
+ * the current block state.
+ * This function does not insert new strings in the dictionary since
+ * uncompressible data is probably not useful. This function is used
+ * only for the level=0 compression option.
+ * NOTE: this function should be optimized to avoid extra copying from
+ * window to pending_buf.
+ */
+local block_state deflate_stored(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
+ * to pending_buf_size, and each stored block has a 5 byte header:
+ */
+ ulg max_block_size = 0xffff;
+ ulg max_start;
+
+ if (max_block_size > s->pending_buf_size - 5) {
+ max_block_size = s->pending_buf_size - 5;
+ }
+
+ /* Copy as much as possible from input to output: */
+ for (;;) {
+ /* Fill the window as much as possible: */
+ if (s->lookahead <= 1) {
+
+ Assert(s->strstart < s->w_size+MAX_DIST(s) ||
+ s->block_start >= (long)s->w_size, "slide too late");
+
+ fill_window(s);
+ if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
+
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+ Assert(s->block_start >= 0L, "block gone");
+
+ s->strstart += s->lookahead;
+ s->lookahead = 0;
+
+ /* Emit a stored block if pending_buf will be full: */
+ max_start = s->block_start + max_block_size;
+ if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
+ /* strstart == 0 is possible when wraparound on 16-bit machine */
+ s->lookahead = (uInt)(s->strstart - max_start);
+ s->strstart = (uInt)max_start;
+ FLUSH_BLOCK(s, 0);
+ }
+ /* Flush if we may have to slide, otherwise block_start may become
+ * negative and the data will be gone:
+ */
+ if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
+ FLUSH_BLOCK(s, 0);
+ }
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
+
+/* ===========================================================================
+ * Compress as much as possible from the input stream, return the current
+ * block state.
+ * This function does not perform lazy evaluation of matches and inserts
+ * new strings in the dictionary only for unmatched strings or for short
+ * matches. It is used only for the fast compression options.
+ */
+local block_state deflate_fast(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ IPos hash_head; /* head of the hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ * At this point we have always match_length < MIN_MATCH
+ */
+ if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+ }
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->match_start, s->match_length);
+
+ _tr_tally_dist(s, s->strstart - s->match_start,
+ s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+
+ /* Insert new strings in the hash table only if the match length
+ * is not too large. This saves time but degrades compression.
+ */
+#ifndef FASTEST
+ if (s->match_length <= s->max_insert_length &&
+ s->lookahead >= MIN_MATCH) {
+ s->match_length--; /* string at strstart already in table */
+ do {
+ s->strstart++;
+ INSERT_STRING(s, s->strstart, hash_head);
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+ * always MIN_MATCH bytes ahead.
+ */
+ } while (--s->match_length != 0);
+ s->strstart++;
+ } else
+#endif
+ {
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ s->ins_h = s->window[s->strstart];
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
+ * matter since it will be recomputed at next deflate call.
+ */
+ }
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Same as above, but achieves better compression. We use a lazy
+ * evaluation for matches: a match is finally adopted only if there is
+ * no better match at the next window position.
+ */
+local block_state deflate_slow(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ IPos hash_head; /* head of hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ /* Process the input block. */
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ */
+ s->prev_length = s->match_length, s->prev_match = s->match_start;
+ s->match_length = MIN_MATCH-1;
+
+ if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
+ s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+
+ if (s->match_length <= 5 && (s->strategy == Z_FILTERED
+#if TOO_FAR <= 32767
+ || (s->match_length == MIN_MATCH &&
+ s->strstart - s->match_start > TOO_FAR)
+#endif
+ )) {
+
+ /* If prev_match is also MIN_MATCH, match_start is garbage
+ * but we will ignore the current match anyway.
+ */
+ s->match_length = MIN_MATCH-1;
+ }
+ }
+ /* If there was a match at the previous step and the current
+ * match is not better, output the previous match:
+ */
+ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
+ uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
+ /* Do not insert strings in hash table beyond this. */
+
+ check_match(s, s->strstart-1, s->prev_match, s->prev_length);
+
+ _tr_tally_dist(s, s->strstart -1 - s->prev_match,
+ s->prev_length - MIN_MATCH, bflush);
+
+ /* Insert in hash table all strings up to the end of the match.
+ * strstart-1 and strstart are already inserted. If there is not
+ * enough lookahead, the last two strings are not inserted in
+ * the hash table.
+ */
+ s->lookahead -= s->prev_length-1;
+ s->prev_length -= 2;
+ do {
+ if (++s->strstart <= max_insert) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+ } while (--s->prev_length != 0);
+ s->match_available = 0;
+ s->match_length = MIN_MATCH-1;
+ s->strstart++;
+
+ if (bflush) FLUSH_BLOCK(s, 0);
+
+ } else if (s->match_available) {
+ /* If there was no match at the previous position, output a
+ * single literal. If there was a match but the current match
+ * is longer, truncate the previous match to a single literal.
+ */
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ if (bflush) {
+ FLUSH_BLOCK_ONLY(s, 0);
+ }
+ s->strstart++;
+ s->lookahead--;
+ if (s->strm->avail_out == 0) return need_more;
+ } else {
+ /* There is no previous match to compare with, wait for
+ * the next step to decide.
+ */
+ s->match_available = 1;
+ s->strstart++;
+ s->lookahead--;
+ }
+ }
+ Assert (flush != Z_NO_FLUSH, "no flush?");
+ if (s->match_available) {
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ s->match_available = 0;
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
+#endif /* FASTEST */
+
+/* ===========================================================================
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
+ * deflate switches away from Z_RLE.)
+ */
+local block_state deflate_rle(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ int bflush; /* set if current block must be flushed */
+ uInt prev; /* byte at distance one to match */
+ Bytef *scan, *strend; /* scan goes up to strend for length of run */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the longest encodable run.
+ */
+ if (s->lookahead < MAX_MATCH) {
+ fill_window(s);
+ if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* See how many times the previous byte repeats */
+ s->match_length = 0;
+ if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
+ scan = s->window + s->strstart - 1;
+ prev = *scan;
+ if (prev == *++scan && prev == *++scan && prev == *++scan) {
+ strend = s->window + s->strstart + MAX_MATCH;
+ do {
+ } while (prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ scan < strend);
+ s->match_length = MAX_MATCH - (int)(strend - scan);
+ if (s->match_length > s->lookahead)
+ s->match_length = s->lookahead;
+ }
+ }
+
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->strstart - 1, s->match_length);
+
+ _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
+
+/* ===========================================================================
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
+ */
+local block_state deflate_huff(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we have a literal to write. */
+ if (s->lookahead == 0) {
+ fill_window(s);
+ if (s->lookahead == 0) {
+ if (flush == Z_NO_FLUSH)
+ return need_more;
+ break; /* flush the current block */
+ }
+ }
+
+ /* Output a literal byte */
+ s->match_length = 0;
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/deflate.h b/test/monniaux/glpk-4.65/src/zlib/deflate.h
new file mode 100644
index 00000000..cbf0d1ea
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/deflate.h
@@ -0,0 +1,342 @@
+/* deflate.h -- internal compression state
+ * Copyright (C) 1995-2010 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* @(#) $Id$ */
+
+#ifndef DEFLATE_H
+#define DEFLATE_H
+
+#include "zutil.h"
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer creation by deflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip encoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GZIP
+#endif
+
+/* ===========================================================================
+ * Internal compression state.
+ */
+
+#define LENGTH_CODES 29
+/* number of length codes, not counting the special END_BLOCK code */
+
+#define LITERALS 256
+/* number of literal bytes 0..255 */
+
+#define L_CODES (LITERALS+1+LENGTH_CODES)
+/* number of Literal or Length codes, including the END_BLOCK code */
+
+#define D_CODES 30
+/* number of distance codes */
+
+#define BL_CODES 19
+/* number of codes used to transfer the bit lengths */
+
+#define HEAP_SIZE (2*L_CODES+1)
+/* maximum heap size */
+
+#define MAX_BITS 15
+/* All codes must not exceed MAX_BITS bits */
+
+#define INIT_STATE 42
+#define EXTRA_STATE 69
+#define NAME_STATE 73
+#define COMMENT_STATE 91
+#define HCRC_STATE 103
+#define BUSY_STATE 113
+#define FINISH_STATE 666
+/* Stream status */
+
+
+/* Data structure describing a single value and its code string. */
+typedef struct ct_data_s {
+ union {
+ ush freq; /* frequency count */
+ ush code; /* bit string */
+ } fc;
+ union {
+ ush dad; /* father node in Huffman tree */
+ ush len; /* length of bit string */
+ } dl;
+} FAR ct_data;
+
+#define Freq fc.freq
+#define Code fc.code
+#define Dad dl.dad
+#define Len dl.len
+
+typedef struct static_tree_desc_s static_tree_desc;
+
+typedef struct tree_desc_s {
+ ct_data *dyn_tree; /* the dynamic tree */
+ int max_code; /* largest code with non zero frequency */
+ static_tree_desc *stat_desc; /* the corresponding static tree */
+} FAR tree_desc;
+
+typedef ush Pos;
+typedef Pos FAR Posf;
+typedef unsigned IPos;
+
+/* A Pos is an index in the character window. We use short instead of int to
+ * save space in the various tables. IPos is used only for parameter passing.
+ */
+
+typedef struct internal_state {
+ z_streamp strm; /* pointer back to this zlib stream */
+ int status; /* as the name implies */
+ Bytef *pending_buf; /* output still pending */
+ ulg pending_buf_size; /* size of pending_buf */
+ Bytef *pending_out; /* next pending byte to output to the stream */
+ uInt pending; /* nb of bytes in the pending buffer */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
+ gz_headerp gzhead; /* gzip header information to write */
+ uInt gzindex; /* where in extra, name, or comment */
+ Byte method; /* STORED (for zip only) or DEFLATED */
+ int last_flush; /* value of flush param for previous deflate call */
+
+ /* used by deflate.c: */
+
+ uInt w_size; /* LZ77 window size (32K by default) */
+ uInt w_bits; /* log2(w_size) (8..16) */
+ uInt w_mask; /* w_size - 1 */
+
+ Bytef *window;
+ /* Sliding window. Input bytes are read into the second half of the window,
+ * and move to the first half later to keep a dictionary of at least wSize
+ * bytes. With this organization, matches are limited to a distance of
+ * wSize-MAX_MATCH bytes, but this ensures that IO is always
+ * performed with a length multiple of the block size. Also, it limits
+ * the window size to 64K, which is quite useful on MSDOS.
+ * To do: use the user input buffer as sliding window.
+ */
+
+ ulg window_size;
+ /* Actual size of window: 2*wSize, except when the user input buffer
+ * is directly used as sliding window.
+ */
+
+ Posf *prev;
+ /* Link to older string with same hash index. To limit the size of this
+ * array to 64K, this link is maintained only for the last 32K strings.
+ * An index in this array is thus a window index modulo 32K.
+ */
+
+ Posf *head; /* Heads of the hash chains or NIL. */
+
+ uInt ins_h; /* hash index of string to be inserted */
+ uInt hash_size; /* number of elements in hash table */
+ uInt hash_bits; /* log2(hash_size) */
+ uInt hash_mask; /* hash_size-1 */
+
+ uInt hash_shift;
+ /* Number of bits by which ins_h must be shifted at each input
+ * step. It must be such that after MIN_MATCH steps, the oldest
+ * byte no longer takes part in the hash key, that is:
+ * hash_shift * MIN_MATCH >= hash_bits
+ */
+
+ long block_start;
+ /* Window position at the beginning of the current output block. Gets
+ * negative when the window is moved backwards.
+ */
+
+ uInt match_length; /* length of best match */
+ IPos prev_match; /* previous match */
+ int match_available; /* set if previous match exists */
+ uInt strstart; /* start of string to insert */
+ uInt match_start; /* start of matching string */
+ uInt lookahead; /* number of valid bytes ahead in window */
+
+ uInt prev_length;
+ /* Length of the best match at previous step. Matches not greater than this
+ * are discarded. This is used in the lazy match evaluation.
+ */
+
+ uInt max_chain_length;
+ /* To speed up deflation, hash chains are never searched beyond this
+ * length. A higher limit improves compression ratio but degrades the
+ * speed.
+ */
+
+ uInt max_lazy_match;
+ /* Attempt to find a better match only when the current match is strictly
+ * smaller than this value. This mechanism is used only for compression
+ * levels >= 4.
+ */
+# define max_insert_length max_lazy_match
+ /* Insert new strings in the hash table only if the match length is not
+ * greater than this length. This saves time but degrades compression.
+ * max_insert_length is used only for compression levels <= 3.
+ */
+
+ int level; /* compression level (1..9) */
+ int strategy; /* favor or force Huffman coding*/
+
+ uInt good_match;
+ /* Use a faster search when the previous match is longer than this */
+
+ int nice_match; /* Stop searching when current match exceeds this */
+
+ /* used by trees.c: */
+ /* Didn't use ct_data typedef below to supress compiler warning */
+ struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
+ struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
+ struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
+
+ struct tree_desc_s l_desc; /* desc. for literal tree */
+ struct tree_desc_s d_desc; /* desc. for distance tree */
+ struct tree_desc_s bl_desc; /* desc. for bit length tree */
+
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
+ int heap_len; /* number of elements in the heap */
+ int heap_max; /* element of largest frequency */
+ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
+ * The same heap array is used to build all trees.
+ */
+
+ uch depth[2*L_CODES+1];
+ /* Depth of each subtree used as tie breaker for trees of equal frequency
+ */
+
+ uchf *l_buf; /* buffer for literals or lengths */
+
+ uInt lit_bufsize;
+ /* Size of match buffer for literals/lengths. There are 4 reasons for
+ * limiting lit_bufsize to 64K:
+ * - frequencies can be kept in 16 bit counters
+ * - if compression is not successful for the first block, all input
+ * data is still in the window so we can still emit a stored block even
+ * when input comes from standard input. (This can also be done for
+ * all blocks if lit_bufsize is not greater than 32K.)
+ * - if compression is not successful for a file smaller than 64K, we can
+ * even emit a stored file instead of a stored block (saving 5 bytes).
+ * This is applicable only for zip (not gzip or zlib).
+ * - creating new Huffman trees less frequently may not provide fast
+ * adaptation to changes in the input data statistics. (Take for
+ * example a binary file with poorly compressible code followed by
+ * a highly compressible string table.) Smaller buffer sizes give
+ * fast adaptation but have of course the overhead of transmitting
+ * trees more frequently.
+ * - I can't count above 4
+ */
+
+ uInt last_lit; /* running index in l_buf */
+
+ ushf *d_buf;
+ /* Buffer for distances. To simplify the code, d_buf and l_buf have
+ * the same number of elements. To use different lengths, an extra flag
+ * array would be necessary.
+ */
+
+ ulg opt_len; /* bit length of current block with optimal trees */
+ ulg static_len; /* bit length of current block with static trees */
+ uInt matches; /* number of string matches in current block */
+ int last_eob_len; /* bit length of EOB code for last block */
+
+#ifdef DEBUG
+ ulg compressed_len; /* total bit length of compressed file mod 2^32 */
+ ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
+#endif
+
+ ush bi_buf;
+ /* Output buffer. bits are inserted starting at the bottom (least
+ * significant bits).
+ */
+ int bi_valid;
+ /* Number of valid bits in bi_buf. All bits above the last valid bit
+ * are always zero.
+ */
+
+ ulg high_water;
+ /* High water mark offset in window for initialized bytes -- bytes above
+ * this are set to zero in order to avoid memory check warnings when
+ * longest match routines access bytes past the input. This is then
+ * updated to the new high water mark.
+ */
+
+} FAR deflate_state;
+
+/* Output a byte on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
+
+
+#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
+/* Minimum amount of lookahead, except at the end of the input file.
+ * See deflate.c for comments about the MIN_MATCH+1.
+ */
+
+#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
+/* In order to simplify the code, particularly on 16 bit machines, match
+ * distances are limited to MAX_DIST instead of WSIZE.
+ */
+
+#define WIN_INIT MAX_MATCH
+/* Number of bytes after end of data in window to initialize in order to avoid
+ memory checker errors from longest match routines */
+
+ /* in trees.c */
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+
+#define d_code(dist) \
+ ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
+/* Mapping from a distance to a distance code. dist is the distance - 1 and
+ * must not have side effects. _dist_code[256] and _dist_code[257] are never
+ * used.
+ */
+
+#ifndef DEBUG
+/* Inline versions of _tr_tally for speed: */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+ extern uch ZLIB_INTERNAL _length_code[];
+ extern uch ZLIB_INTERNAL _dist_code[];
+#else
+ extern const uch ZLIB_INTERNAL _length_code[];
+ extern const uch ZLIB_INTERNAL _dist_code[];
+#endif
+
+# define _tr_tally_lit(s, c, flush) \
+ { uch cc = (c); \
+ s->d_buf[s->last_lit] = 0; \
+ s->l_buf[s->last_lit++] = cc; \
+ s->dyn_ltree[cc].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+# define _tr_tally_dist(s, distance, length, flush) \
+ { uch len = (length); \
+ ush dist = (distance); \
+ s->d_buf[s->last_lit] = dist; \
+ s->l_buf[s->last_lit++] = len; \
+ dist--; \
+ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
+ s->dyn_dtree[d_code(dist)].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+#else
+# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
+# define _tr_tally_dist(s, distance, length, flush) \
+ flush = _tr_tally(s, distance, length)
+#endif
+
+#endif /* DEFLATE_H */
diff --git a/test/monniaux/glpk-4.65/src/zlib/gzclose.c b/test/monniaux/glpk-4.65/src/zlib/gzclose.c
new file mode 100644
index 00000000..caeb99a3
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/gzclose.c
@@ -0,0 +1,25 @@
+/* gzclose.c -- zlib gzclose() function
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* gzclose() is in a separate file so that it is linked in only if it is used.
+ That way the other gzclose functions can be used instead to avoid linking in
+ unneeded compression or decompression routines. */
+int ZEXPORT gzclose(file)
+ gzFile file;
+{
+#ifndef NO_GZCOMPRESS
+ gz_statep state;
+
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
+#else
+ return gzclose_r(file);
+#endif
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/gzguts.h b/test/monniaux/glpk-4.65/src/zlib/gzguts.h
new file mode 100644
index 00000000..9d01ac7b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/gzguts.h
@@ -0,0 +1,74 @@
+/* gzguts.h (zlib internal header definitions for gz* operations) */
+
+/* Modified by Andrew Makhorin <mao@gnu.org>, April 2011 */
+
+/* Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in
+ * zlib.h */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h. */
+
+#ifndef GZGUTS_H
+#define GZGUTS_H
+
+#define ZLIB_INTERNAL
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "zio.h"
+#include "zlib.h"
+
+#define local static
+
+#define zstrerror() strerror(errno)
+
+#define GZBUFSIZE 8192
+
+#define GZ_NONE 0
+#define GZ_READ 7247
+#define GZ_WRITE 31153
+#define GZ_APPEND 1
+
+#define LOOK 0
+#define COPY 1
+#define GZIP 2
+
+typedef struct
+{ int mode;
+ int fd;
+ char *path;
+ z_off64_t pos;
+ unsigned size;
+ unsigned want;
+ unsigned char *in;
+ unsigned char *out;
+ unsigned char *next;
+ unsigned have;
+ int eof;
+ z_off64_t start;
+ z_off64_t raw;
+ int how;
+ int direct;
+ int level;
+ int strategy;
+ z_off64_t skip;
+ int seek;
+ int err;
+ char *msg;
+ z_stream strm;
+} gz_state;
+
+typedef gz_state *gz_statep;
+
+void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
+
+#define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/zlib/gzlib.c b/test/monniaux/glpk-4.65/src/zlib/gzlib.c
new file mode 100644
index 00000000..603e60ed
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/gzlib.c
@@ -0,0 +1,537 @@
+/* gzlib.c -- zlib functions common to reading and writing gzip files
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+# define LSEEK lseek64
+#else
+# define LSEEK lseek
+#endif
+
+/* Local functions */
+local void gz_reset OF((gz_statep));
+local gzFile gz_open OF((const char *, int, const char *));
+
+#if defined UNDER_CE
+
+/* Map the Windows error number in ERROR to a locale-dependent error message
+ string and return a pointer to it. Typically, the values for ERROR come
+ from GetLastError.
+
+ The string pointed to shall not be modified by the application, but may be
+ overwritten by a subsequent call to gz_strwinerror
+
+ The gz_strwinerror function does not change the current setting of
+ GetLastError. */
+char ZLIB_INTERNAL *gz_strwinerror (error)
+ DWORD error;
+{
+ static char buf[1024];
+
+ wchar_t *msgbuf;
+ DWORD lasterr = GetLastError();
+ DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL,
+ error,
+ 0, /* Default language */
+ (LPVOID)&msgbuf,
+ 0,
+ NULL);
+ if (chars != 0) {
+ /* If there is an \r\n appended, zap it. */
+ if (chars >= 2
+ && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+ chars -= 2;
+ msgbuf[chars] = 0;
+ }
+
+ if (chars > sizeof (buf) - 1) {
+ chars = sizeof (buf) - 1;
+ msgbuf[chars] = 0;
+ }
+
+ wcstombs(buf, msgbuf, chars + 1);
+ LocalFree(msgbuf);
+ }
+ else {
+ sprintf(buf, "unknown win32 error (%ld)", error);
+ }
+
+ SetLastError(lasterr);
+ return buf;
+}
+
+#endif /* UNDER_CE */
+
+/* Reset gzip file state */
+local void gz_reset(state)
+ gz_statep state;
+{
+ if (state->mode == GZ_READ) { /* for reading ... */
+ state->have = 0; /* no output data available */
+ state->eof = 0; /* not at end of file */
+ state->how = LOOK; /* look for gzip header */
+ state->direct = 1; /* default for empty file */
+ }
+ state->seek = 0; /* no seek request pending */
+ gz_error(state, Z_OK, NULL); /* clear error */
+ state->pos = 0; /* no uncompressed data yet */
+ state->strm.avail_in = 0; /* no input data yet */
+}
+
+/* Open a gzip file either by name or file descriptor. */
+local gzFile gz_open(path, fd, mode)
+ const char *path;
+ int fd;
+ const char *mode;
+{
+ gz_statep state;
+
+ /* allocate gzFile structure to return */
+ state = malloc(sizeof(gz_state));
+ if (state == NULL)
+ return NULL;
+ state->size = 0; /* no buffers allocated yet */
+ state->want = GZBUFSIZE; /* requested buffer size */
+ state->msg = NULL; /* no error message yet */
+
+ /* interpret mode */
+ state->mode = GZ_NONE;
+ state->level = Z_DEFAULT_COMPRESSION;
+ state->strategy = Z_DEFAULT_STRATEGY;
+ while (*mode) {
+ if (*mode >= '0' && *mode <= '9')
+ state->level = *mode - '0';
+ else
+ switch (*mode) {
+ case 'r':
+ state->mode = GZ_READ;
+ break;
+#ifndef NO_GZCOMPRESS
+ case 'w':
+ state->mode = GZ_WRITE;
+ break;
+ case 'a':
+ state->mode = GZ_APPEND;
+ break;
+#endif
+ case '+': /* can't read and write at the same time */
+ free(state);
+ return NULL;
+ case 'b': /* ignore -- will request binary anyway */
+ break;
+ case 'f':
+ state->strategy = Z_FILTERED;
+ break;
+ case 'h':
+ state->strategy = Z_HUFFMAN_ONLY;
+ break;
+ case 'R':
+ state->strategy = Z_RLE;
+ break;
+ case 'F':
+ state->strategy = Z_FIXED;
+ default: /* could consider as an error, but just ignore */
+ ;
+ }
+ mode++;
+ }
+
+ /* must provide an "r", "w", or "a" */
+ if (state->mode == GZ_NONE) {
+ free(state);
+ return NULL;
+ }
+
+ /* save the path name for error messages */
+ state->path = malloc(strlen(path) + 1);
+ if (state->path == NULL) {
+ free(state);
+ return NULL;
+ }
+ strcpy(state->path, path);
+
+ /* open the file with the appropriate mode (or just use fd) */
+ state->fd = fd != -1 ? fd :
+ open(path,
+#ifdef O_LARGEFILE
+ O_LARGEFILE |
+#endif
+#ifdef O_BINARY
+ O_BINARY |
+#endif
+ (state->mode == GZ_READ ?
+ O_RDONLY :
+ (O_WRONLY | O_CREAT | (
+ state->mode == GZ_WRITE ?
+ O_TRUNC :
+ O_APPEND))),
+ 0666);
+ if (state->fd == -1) {
+ free(state->path);
+ free(state);
+ return NULL;
+ }
+ if (state->mode == GZ_APPEND)
+ state->mode = GZ_WRITE; /* simplify later checks */
+
+ /* save the current position for rewinding (only if reading) */
+ if (state->mode == GZ_READ) {
+ state->start = LSEEK(state->fd, 0, SEEK_CUR);
+ if (state->start == -1) state->start = 0;
+ }
+
+ /* initialize stream */
+ gz_reset(state);
+
+ /* return stream */
+ return (gzFile)state;
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen(path, mode)
+ const char *path;
+ const char *mode;
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen64(path, mode)
+ const char *path;
+ const char *mode;
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzdopen(fd, mode)
+ int fd;
+ const char *mode;
+{
+ char *path; /* identifier for error messages */
+ gzFile gz;
+
+ if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
+ return NULL;
+ sprintf(path, "<fd:%d>", fd); /* for debugging */
+ gz = gz_open(path, fd, mode);
+ free(path);
+ return gz;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzbuffer(file, size)
+ gzFile file;
+ unsigned size;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* make sure we haven't already allocated memory */
+ if (state->size != 0)
+ return -1;
+
+ /* check and set requested size */
+ if (size == 0)
+ return -1;
+ state->want = size;
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzrewind(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ || state->err != Z_OK)
+ return -1;
+
+ /* back up and start over */
+ if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
+ return -1;
+ gz_reset(state);
+ return 0;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzseek64(file, offset, whence)
+ gzFile file;
+ z_off64_t offset;
+ int whence;
+{
+ unsigned n;
+ z_off64_t ret;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* check that there's no error */
+ if (state->err != Z_OK)
+ return -1;
+
+ /* can only seek from start or relative to current position */
+ if (whence != SEEK_SET && whence != SEEK_CUR)
+ return -1;
+
+ /* normalize offset to a SEEK_CUR specification */
+ if (whence == SEEK_SET)
+ offset -= state->pos;
+ else if (state->seek)
+ offset += state->skip;
+ state->seek = 0;
+
+ /* if within raw area while reading, just go there */
+ if (state->mode == GZ_READ && state->how == COPY &&
+ state->pos + offset >= state->raw) {
+ ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
+ if (ret == -1)
+ return -1;
+ state->have = 0;
+ state->eof = 0;
+ state->seek = 0;
+ gz_error(state, Z_OK, NULL);
+ state->strm.avail_in = 0;
+ state->pos += offset;
+ return state->pos;
+ }
+
+ /* calculate skip amount, rewinding if needed for back seek when reading */
+ if (offset < 0) {
+ if (state->mode != GZ_READ) /* writing -- can't go backwards */
+ return -1;
+ offset += state->pos;
+ if (offset < 0) /* before start of file! */
+ return -1;
+ if (gzrewind(file) == -1) /* rewind, then skip to offset */
+ return -1;
+ }
+
+ /* if reading, skip what's in output buffer (one less gzgetc() check) */
+ if (state->mode == GZ_READ) {
+ n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
+ (unsigned)offset : state->have;
+ state->have -= n;
+ state->next += n;
+ state->pos += n;
+ offset -= n;
+ }
+
+ /* request skip (if not zero) */
+ if (offset) {
+ state->seek = 1;
+ state->skip = offset;
+ }
+ return state->pos + offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzseek(file, offset, whence)
+ gzFile file;
+ z_off_t offset;
+ int whence;
+{
+ z_off64_t ret;
+
+ ret = gzseek64(file, (z_off64_t)offset, whence);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gztell64(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* return position */
+ return state->pos + (state->seek ? state->skip : 0);
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gztell(file)
+ gzFile file;
+{
+ z_off64_t ret;
+
+ ret = gztell64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzoffset64(file)
+ gzFile file;
+{
+ z_off64_t offset;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* compute and return effective offset in file */
+ offset = LSEEK(state->fd, 0, SEEK_CUR);
+ if (offset == -1)
+ return -1;
+ if (state->mode == GZ_READ) /* reading */
+ offset -= state->strm.avail_in; /* don't count buffered input */
+ return offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzoffset(file)
+ gzFile file;
+{
+ z_off64_t ret;
+
+ ret = gzoffset64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzeof(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return 0;
+
+ /* return end-of-file state */
+ return state->mode == GZ_READ ?
+ (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
+}
+
+/* -- see zlib.h -- */
+const char * ZEXPORT gzerror(file, errnum)
+ gzFile file;
+ int *errnum;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return NULL;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return NULL;
+
+ /* return error information */
+ if (errnum != NULL)
+ *errnum = state->err;
+ return state->msg == NULL ? "" : state->msg;
+}
+
+/* -- see zlib.h -- */
+void ZEXPORT gzclearerr(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return;
+
+ /* clear error and end-of-file */
+ if (state->mode == GZ_READ)
+ state->eof = 0;
+ gz_error(state, Z_OK, NULL);
+}
+
+/* Create an error message in allocated memory and set state->err and
+ state->msg accordingly. Free any previous error message already there. Do
+ not try to free or allocate space if the error is Z_MEM_ERROR (out of
+ memory). Simply save the error message as a static string. If there is an
+ allocation failure constructing the error message, then convert the error to
+ out of memory. */
+void ZLIB_INTERNAL gz_error(state, err, msg)
+ gz_statep state;
+ int err;
+ const char *msg;
+{
+ /* free previously allocated message and clear */
+ if (state->msg != NULL) {
+ if (state->err != Z_MEM_ERROR)
+ free(state->msg);
+ state->msg = NULL;
+ }
+
+ /* set error code, and if no message, then done */
+ state->err = err;
+ if (msg == NULL)
+ return;
+
+ /* for an out of memory error, save as static string */
+ if (err == Z_MEM_ERROR) {
+ state->msg = (char *)msg;
+ return;
+ }
+
+ /* construct error message with path */
+ if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
+ state->err = Z_MEM_ERROR;
+ state->msg = (char *)"out of memory";
+ return;
+ }
+ strcpy(state->msg, state->path);
+ strcat(state->msg, ": ");
+ strcat(state->msg, msg);
+ return;
+}
+
+#ifndef INT_MAX
+/* portably return maximum value for an int (when limits.h presumed not
+ available) -- we need to do this to cover cases where 2's complement not
+ used, since C standard permits 1's complement and sign-bit representations,
+ otherwise we could just use ((unsigned)-1) >> 1 */
+unsigned ZLIB_INTERNAL gz_intmax()
+{
+ unsigned p, q;
+
+ p = 1;
+ do {
+ q = p;
+ p <<= 1;
+ p++;
+ } while (p > q);
+ return q >> 1;
+}
+#endif
diff --git a/test/monniaux/glpk-4.65/src/zlib/gzread.c b/test/monniaux/glpk-4.65/src/zlib/gzread.c
new file mode 100644
index 00000000..548201ab
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/gzread.c
@@ -0,0 +1,653 @@
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
+local int gz_avail OF((gz_statep));
+local int gz_next4 OF((gz_statep, unsigned long *));
+local int gz_head OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_make OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+
+/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
+ state->fd, and update state->eof, state->err, and state->msg as appropriate.
+ This function needs to loop on read(), since read() is not guaranteed to
+ read the number of bytes requested, depending on the type of descriptor. */
+local int gz_load(state, buf, len, have)
+ gz_statep state;
+ unsigned char *buf;
+ unsigned len;
+ unsigned *have;
+{
+ int ret;
+
+ *have = 0;
+ do {
+ ret = read(state->fd, buf + *have, len - *have);
+ if (ret <= 0)
+ break;
+ *have += ret;
+ } while (*have < len);
+ if (ret < 0) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ if (ret == 0)
+ state->eof = 1;
+ return 0;
+}
+
+/* Load up input buffer and set eof flag if last data loaded -- return -1 on
+ error, 0 otherwise. Note that the eof flag is set when the end of the input
+ file is reached, even though there may be unused data in the buffer. Once
+ that data has been used, no more attempts will be made to read the file.
+ gz_avail() assumes that strm->avail_in == 0. */
+local int gz_avail(state)
+ gz_statep state;
+{
+ z_streamp strm = &(state->strm);
+
+ if (state->err != Z_OK)
+ return -1;
+ if (state->eof == 0) {
+ if (gz_load(state, state->in, state->size,
+ (unsigned *)&(strm->avail_in)) == -1)
+ return -1;
+ strm->next_in = state->in;
+ }
+ return 0;
+}
+
+/* Get next byte from input, or -1 if end or error. */
+#define NEXT() ((strm->avail_in == 0 && gz_avail(state) == -1) ? -1 : \
+ (strm->avail_in == 0 ? -1 : \
+ (strm->avail_in--, *(strm->next_in)++)))
+
+/* Get a four-byte little-endian integer and return 0 on success and the value
+ in *ret. Otherwise -1 is returned and *ret is not modified. */
+local int gz_next4(state, ret)
+ gz_statep state;
+ unsigned long *ret;
+{
+ int ch;
+ unsigned long val;
+ z_streamp strm = &(state->strm);
+
+ val = NEXT();
+ val += (unsigned)NEXT() << 8;
+ val += (unsigned long)NEXT() << 16;
+ ch = NEXT();
+ if (ch == -1)
+ return -1;
+ val += (unsigned long)ch << 24;
+ *ret = val;
+ return 0;
+}
+
+/* Look for gzip header, set up for inflate or copy. state->have must be zero.
+ If this is the first time in, allocate required memory. state->how will be
+ left unchanged if there is no more input data available, will be set to COPY
+ if there is no gzip header and direct copying will be performed, or it will
+ be set to GZIP for decompression, and the gzip header will be skipped so
+ that the next available input data is the raw deflate stream. If direct
+ copying, then leftover input data from the input buffer will be copied to
+ the output buffer. In that case, all further file reads will be directly to
+ either the output buffer or a user buffer. If decompressing, the inflate
+ state and the check value will be initialized. gz_head() will return 0 on
+ success or -1 on failure. Failures may include read errors or gzip header
+ errors. */
+local int gz_head(state)
+ gz_statep state;
+{
+ z_streamp strm = &(state->strm);
+ int flags;
+ unsigned len;
+
+ /* allocate read buffers and inflate memory */
+ if (state->size == 0) {
+ /* allocate buffers */
+ state->in = malloc(state->want);
+ state->out = malloc(state->want << 1);
+ if (state->in == NULL || state->out == NULL) {
+ if (state->out != NULL)
+ free(state->out);
+ if (state->in != NULL)
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ state->size = state->want;
+
+ /* allocate inflate memory */
+ state->strm.zalloc = Z_NULL;
+ state->strm.zfree = Z_NULL;
+ state->strm.opaque = Z_NULL;
+ state->strm.avail_in = 0;
+ state->strm.next_in = Z_NULL;
+ if (inflateInit2(&(state->strm), -15) != Z_OK) { /* raw inflate */
+ free(state->out);
+ free(state->in);
+ state->size = 0;
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ }
+
+ /* get some data in the input buffer */
+ if (strm->avail_in == 0) {
+ if (gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0)
+ return 0;
+ }
+
+ /* look for the gzip magic header bytes 31 and 139 */
+ if (strm->next_in[0] == 31) {
+ strm->avail_in--;
+ strm->next_in++;
+ if (strm->avail_in == 0 && gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in && strm->next_in[0] == 139) {
+ /* we have a gzip header, woo hoo! */
+ strm->avail_in--;
+ strm->next_in++;
+
+ /* skip rest of header */
+ if (NEXT() != 8) { /* compression method */
+ gz_error(state, Z_DATA_ERROR, "unknown compression method");
+ return -1;
+ }
+ flags = NEXT();
+ if (flags & 0xe0) { /* reserved flag bits */
+ gz_error(state, Z_DATA_ERROR, "unknown header flags set");
+ return -1;
+ }
+ NEXT(); /* modification time */
+ NEXT();
+ NEXT();
+ NEXT();
+ NEXT(); /* extra flags */
+ NEXT(); /* operating system */
+ if (flags & 4) { /* extra field */
+ len = (unsigned)NEXT();
+ len += (unsigned)NEXT() << 8;
+ while (len--)
+ if (NEXT() < 0)
+ break;
+ }
+ if (flags & 8) /* file name */
+ while (NEXT() > 0)
+ ;
+ if (flags & 16) /* comment */
+ while (NEXT() > 0)
+ ;
+ if (flags & 2) { /* header crc */
+ NEXT();
+ NEXT();
+ }
+ /* an unexpected end of file is not checked for here -- it will be
+ noticed on the first request for uncompressed data */
+
+ /* set up for decompression */
+ inflateReset(strm);
+ strm->adler = crc32(0L, Z_NULL, 0);
+ state->how = GZIP;
+ state->direct = 0;
+ return 0;
+ }
+ else {
+ /* not a gzip file -- save first byte (31) and fall to raw i/o */
+ state->out[0] = 31;
+ state->have = 1;
+ }
+ }
+
+ /* doing raw i/o, save start of raw data for seeking, copy any leftover
+ input to output -- this assumes that the output buffer is larger than
+ the input buffer, which also assures space for gzungetc() */
+ state->raw = state->pos;
+ state->next = state->out;
+ if (strm->avail_in) {
+ memcpy(state->next + state->have, strm->next_in, strm->avail_in);
+ state->have += strm->avail_in;
+ strm->avail_in = 0;
+ }
+ state->how = COPY;
+ state->direct = 1;
+ return 0;
+}
+
+/* Decompress from input to the provided next_out and avail_out in the state.
+ If the end of the compressed data is reached, then verify the gzip trailer
+ check value and length (modulo 2^32). state->have and state->next are set
+ to point to the just decompressed data, and the crc is updated. If the
+ trailer is verified, state->how is reset to LOOK to look for the next gzip
+ stream or raw data, once state->have is depleted. Returns 0 on success, -1
+ on failure. Failures may include invalid compressed data or a failed gzip
+ trailer verification. */
+local int gz_decomp(state)
+ gz_statep state;
+{
+ int ret;
+ unsigned had;
+ unsigned long crc, len;
+ z_streamp strm = &(state->strm);
+
+ /* fill output buffer up to end of deflate stream */
+ had = strm->avail_out;
+ do {
+ /* get more input for inflate() */
+ if (strm->avail_in == 0 && gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0) {
+ gz_error(state, Z_DATA_ERROR, "unexpected end of file");
+ return -1;
+ }
+
+ /* decompress and handle errors */
+ ret = inflate(strm, Z_NO_FLUSH);
+ if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: inflate stream corrupt");
+ return -1;
+ }
+ if (ret == Z_MEM_ERROR) {
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
+ gz_error(state, Z_DATA_ERROR,
+ strm->msg == NULL ? "compressed data error" : strm->msg);
+ return -1;
+ }
+ } while (strm->avail_out && ret != Z_STREAM_END);
+
+ /* update available output and crc check value */
+ state->have = had - strm->avail_out;
+ state->next = strm->next_out - state->have;
+ strm->adler = crc32(strm->adler, state->next, state->have);
+
+ /* check gzip trailer if at end of deflate stream */
+ if (ret == Z_STREAM_END) {
+ if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
+ gz_error(state, Z_DATA_ERROR, "unexpected end of file");
+ return -1;
+ }
+ if (crc != strm->adler) {
+ gz_error(state, Z_DATA_ERROR, "incorrect data check");
+ return -1;
+ }
+ if (len != (strm->total_out & 0xffffffffL)) {
+ gz_error(state, Z_DATA_ERROR, "incorrect length check");
+ return -1;
+ }
+ state->how = LOOK; /* ready for next stream, once have is 0 (leave
+ state->direct unchanged to remember how) */
+ }
+
+ /* good decompression */
+ return 0;
+}
+
+/* Make data and put in the output buffer. Assumes that state->have == 0.
+ Data is either copied from the input file or decompressed from the input
+ file depending on state->how. If state->how is LOOK, then a gzip header is
+ looked for (and skipped if found) to determine wither to copy or decompress.
+ Returns -1 on error, otherwise 0. gz_make() will leave state->have as COPY
+ or GZIP unless the end of the input file has been reached and all data has
+ been processed. */
+local int gz_make(state)
+ gz_statep state;
+{
+ z_streamp strm = &(state->strm);
+
+ if (state->how == LOOK) { /* look for gzip header */
+ if (gz_head(state) == -1)
+ return -1;
+ if (state->have) /* got some data from gz_head() */
+ return 0;
+ }
+ if (state->how == COPY) { /* straight copy */
+ if (gz_load(state, state->out, state->size << 1, &(state->have)) == -1)
+ return -1;
+ state->next = state->out;
+ }
+ else if (state->how == GZIP) { /* decompress */
+ strm->avail_out = state->size << 1;
+ strm->next_out = state->out;
+ if (gz_decomp(state) == -1)
+ return -1;
+ }
+ return 0;
+}
+
+/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
+local int gz_skip(state, len)
+ gz_statep state;
+ z_off64_t len;
+{
+ unsigned n;
+
+ /* skip over len bytes or reach end-of-file, whichever comes first */
+ while (len)
+ /* skip over whatever is in output buffer */
+ if (state->have) {
+ n = GT_OFF(state->have) || (z_off64_t)state->have > len ?
+ (unsigned)len : state->have;
+ state->have -= n;
+ state->next += n;
+ state->pos += n;
+ len -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && state->strm.avail_in == 0)
+ break;
+
+ /* need more data to skip -- load up output buffer */
+ else {
+ /* get more output, looking for header if required */
+ if (gz_make(state) == -1)
+ return -1;
+ }
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzread(file, buf, len)
+ gzFile file;
+ voidp buf;
+ unsigned len;
+{
+ unsigned got, n;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ || state->err != Z_OK)
+ return -1;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids the flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
+ return -1;
+ }
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* get len bytes to buf, or less than len if at the end */
+ got = 0;
+ do {
+ /* first just try copying data from the output buffer */
+ if (state->have) {
+ n = state->have > len ? len : state->have;
+ memcpy(buf, state->next, n);
+ state->next += n;
+ state->have -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && strm->avail_in == 0)
+ break;
+
+ /* need output data -- for small len or new stream load up our output
+ buffer */
+ else if (state->how == LOOK || len < (state->size << 1)) {
+ /* get more output, looking for header if required */
+ if (gz_make(state) == -1)
+ return -1;
+ continue; /* no progress yet -- go back to memcpy() above */
+ /* the copy above assures that we will leave with space in the
+ output buffer, allowing at least one gzungetc() to succeed */
+ }
+
+ /* large len -- read directly into user buffer */
+ else if (state->how == COPY) { /* read directly */
+ if (gz_load(state, buf, len, &n) == -1)
+ return -1;
+ }
+
+ /* large len -- decompress directly into user buffer */
+ else { /* state->how == GZIP */
+ strm->avail_out = len;
+ strm->next_out = buf;
+ if (gz_decomp(state) == -1)
+ return -1;
+ n = state->have;
+ state->have = 0;
+ }
+
+ /* update progress */
+ len -= n;
+ buf = (char *)buf + n;
+ got += n;
+ state->pos += n;
+ } while (len);
+
+ /* return number of bytes read into user buffer (will fit in int) */
+ return (int)got;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzgetc(file)
+ gzFile file;
+{
+ int ret;
+ unsigned char buf[1];
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ || state->err != Z_OK)
+ return -1;
+
+ /* try output buffer (no need to check for skip request) */
+ if (state->have) {
+ state->have--;
+ state->pos++;
+ return *(state->next)++;
+ }
+
+ /* nothing there -- try gzread() */
+ ret = gzread(file, buf, 1);
+ return ret < 1 ? -1 : buf[0];
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzungetc(c, file)
+ int c;
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ || state->err != Z_OK)
+ return -1;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* can't push EOF */
+ if (c < 0)
+ return -1;
+
+ /* if output buffer empty, put byte at end (allows more pushing) */
+ if (state->have == 0) {
+ state->have = 1;
+ state->next = state->out + (state->size << 1) - 1;
+ state->next[0] = c;
+ state->pos--;
+ return c;
+ }
+
+ /* if no room, give up (must have already done a gzungetc()) */
+ if (state->have == (state->size << 1)) {
+ gz_error(state, Z_BUF_ERROR, "out of room to push characters");
+ return -1;
+ }
+
+ /* slide output data if needed and insert byte before existing data */
+ if (state->next == state->out) {
+ unsigned char *src = state->out + state->have;
+ unsigned char *dest = state->out + (state->size << 1);
+ while (src > state->out)
+ *--dest = *--src;
+ state->next = dest;
+ }
+ state->have++;
+ state->next--;
+ state->next[0] = c;
+ state->pos--;
+ return c;
+}
+
+/* -- see zlib.h -- */
+char * ZEXPORT gzgets(file, buf, len)
+ gzFile file;
+ char *buf;
+ int len;
+{
+ unsigned left, n;
+ char *str;
+ unsigned char *eol;
+ gz_statep state;
+
+ /* check parameters and get internal structure */
+ if (file == NULL || buf == NULL || len < 1)
+ return NULL;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ || state->err != Z_OK)
+ return NULL;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return NULL;
+ }
+
+ /* copy output bytes up to new line or len - 1, whichever comes first --
+ append a terminating zero to the string (we don't check for a zero in
+ the contents, let the user worry about that) */
+ str = buf;
+ left = (unsigned)len - 1;
+ if (left) do {
+ /* assure that something is in the output buffer */
+ if (state->have == 0) {
+ if (gz_make(state) == -1)
+ return NULL; /* error */
+ if (state->have == 0) { /* end of file */
+ if (buf == str) /* got bupkus */
+ return NULL;
+ break; /* got something -- return it */
+ }
+ }
+
+ /* look for end-of-line in current output buffer */
+ n = state->have > left ? left : state->have;
+ eol = memchr(state->next, '\n', n);
+ if (eol != NULL)
+ n = (unsigned)(eol - state->next) + 1;
+
+ /* copy through end-of-line, or remainder if not found */
+ memcpy(buf, state->next, n);
+ state->have -= n;
+ state->next += n;
+ state->pos += n;
+ left -= n;
+ buf += n;
+ } while (left && eol == NULL);
+
+ /* found end-of-line or out of space -- terminate string and return it */
+ buf[0] = 0;
+ return str;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzdirect(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* check that we're reading */
+ if (state->mode != GZ_READ)
+ return 0;
+
+ /* if the state is not known, but we can find out, then do so (this is
+ mainly for right after a gzopen() or gzdopen()) */
+ if (state->how == LOOK && state->have == 0)
+ (void)gz_head(state);
+
+ /* return 1 if reading direct, 0 if decompressing a gzip stream */
+ return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(file)
+ gzFile file;
+{
+ int ret;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're reading */
+ if (state->mode != GZ_READ)
+ return Z_STREAM_ERROR;
+
+ /* free memory and close file */
+ if (state->size) {
+ inflateEnd(&(state->strm));
+ free(state->out);
+ free(state->in);
+ }
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ ret = close(state->fd);
+ free(state);
+ return ret ? Z_ERRNO : Z_OK;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/gzwrite.c b/test/monniaux/glpk-4.65/src/zlib/gzwrite.c
new file mode 100644
index 00000000..13c5558e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/gzwrite.c
@@ -0,0 +1,531 @@
+/* gzwrite.c -- zlib functions for writing gzip files
+ * Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_init OF((gz_statep));
+local int gz_comp OF((gz_statep, int));
+local int gz_zero OF((gz_statep, z_off64_t));
+
+/* Initialize state for writing a gzip file. Mark initialization by setting
+ state->size to non-zero. Return -1 on failure or 0 on success. */
+local int gz_init(state)
+ gz_statep state;
+{
+ int ret;
+ z_streamp strm = &(state->strm);
+
+ /* allocate input and output buffers */
+ state->in = malloc(state->want);
+ state->out = malloc(state->want);
+ if (state->in == NULL || state->out == NULL) {
+ if (state->out != NULL)
+ free(state->out);
+ if (state->in != NULL)
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* allocate deflate memory, set up for gzip compression */
+ strm->zalloc = Z_NULL;
+ strm->zfree = Z_NULL;
+ strm->opaque = Z_NULL;
+ ret = deflateInit2(strm, state->level, Z_DEFLATED,
+ 15 + 16, 8, state->strategy);
+ if (ret != Z_OK) {
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* mark state as initialized */
+ state->size = state->want;
+
+ /* initialize write buffer */
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ state->next = strm->next_out;
+ return 0;
+}
+
+/* Compress whatever is at avail_in and next_in and write to the output file.
+ Return -1 if there is an error writing to the output file, otherwise 0.
+ flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
+ then the deflate() state is reset to start a new gzip stream. */
+local int gz_comp(state, flush)
+ gz_statep state;
+ int flush;
+{
+ int ret, got;
+ unsigned have;
+ z_streamp strm = &(state->strm);
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return -1;
+
+ /* run deflate() on provided input until it produces no more output */
+ ret = Z_OK;
+ do {
+ /* write out current buffer contents if full, or if flushing, but if
+ doing Z_FINISH then don't write until we get to Z_STREAM_END */
+ if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
+ (flush != Z_FINISH || ret == Z_STREAM_END))) {
+ have = (unsigned)(strm->next_out - state->next);
+ if (have && ((got = write(state->fd, state->next, have)) < 0 ||
+ (unsigned)got != have)) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ if (strm->avail_out == 0) {
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ }
+ state->next = strm->next_out;
+ }
+
+ /* compress */
+ have = strm->avail_out;
+ ret = deflate(strm, flush);
+ if (ret == Z_STREAM_ERROR) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: deflate stream corrupt");
+ return -1;
+ }
+ have -= strm->avail_out;
+ } while (have);
+
+ /* if that completed a deflate stream, allow another to start */
+ if (flush == Z_FINISH)
+ deflateReset(strm);
+
+ /* all done, no errors */
+ return 0;
+}
+
+/* Compress len zeros to output. Return -1 on error, 0 on success. */
+local int gz_zero(state, len)
+ gz_statep state;
+ z_off64_t len;
+{
+ int first;
+ unsigned n;
+ z_streamp strm = &(state->strm);
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+
+ /* compress len zeros (len guaranteed > 0) */
+ first = 1;
+ while (len) {
+ n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
+ (unsigned)len : state->size;
+ if (first) {
+ memset(state->in, 0, n);
+ first = 0;
+ }
+ strm->avail_in = n;
+ strm->next_in = state->in;
+ state->pos += n;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+ len -= n;
+ }
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzwrite(file, buf, len)
+ gzFile file;
+ voidpc buf;
+ unsigned len;
+{
+ unsigned put = len;
+ unsigned n;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids the flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
+ return 0;
+ }
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* for small len, copy to input buffer, otherwise compress directly */
+ if (len < state->size) {
+ /* copy to input buffer, compress when full */
+ do {
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ n = state->size - strm->avail_in;
+ if (n > len)
+ n = len;
+ memcpy(strm->next_in + strm->avail_in, buf, n);
+ strm->avail_in += n;
+ state->pos += n;
+ buf = (char *)buf + n;
+ len -= n;
+ if (len && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ } while (len);
+ }
+ else {
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* directly compress user buffer to file */
+ strm->avail_in = len;
+ strm->next_in = (voidp)buf;
+ state->pos += len;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ }
+
+ /* input was all buffered or compressed (put will fit in int) */
+ return (int)put;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputc(file, c)
+ gzFile file;
+ int c;
+{
+ unsigned char buf[1];
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return -1;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* try writing to input buffer for speed (state->size == 0 if buffer not
+ initialized) */
+ if (strm->avail_in < state->size) {
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ strm->next_in[strm->avail_in++] = c;
+ state->pos++;
+ return c;
+ }
+
+ /* no room in buffer or not initialized, use gz_write() */
+ buf[0] = c;
+ if (gzwrite(file, buf, 1) != 1)
+ return -1;
+ return c;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputs(file, str)
+ gzFile file;
+ const char *str;
+{
+ int ret;
+ unsigned len;
+
+ /* write string */
+ len = (unsigned)strlen(str);
+ ret = gzwrite(file, str, len);
+ return ret == 0 && len != 0 ? -1 : ret;
+}
+
+#ifdef STDC
+#include <stdarg.h>
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
+{
+ int size, len;
+ gz_statep state;
+ z_streamp strm;
+ va_list va;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* do the printf() into the input buffer, put length in len */
+ size = (int)(state->size);
+ state->in[size - 1] = 0;
+ va_start(va, format);
+#ifdef NO_vsnprintf
+# ifdef HAS_vsprintf_void
+ (void)vsprintf(state->in, format, va);
+ va_end(va);
+ for (len = 0; len < size; len++)
+ if (state->in[len] == 0) break;
+# else
+ len = vsprintf((char *)state->in, format, va);
+ va_end(va);
+# endif
+#else
+# ifdef HAS_vsnprintf_void
+ (void)vsnprintf(state->in, size, format, va);
+ va_end(va);
+ len = strlen(state->in);
+# else
+ len = vsnprintf((char *)(state->in), size, format, va);
+ va_end(va);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, defer compression until needed */
+ strm->avail_in = (unsigned)len;
+ strm->next_in = state->in;
+ state->pos += len;
+ return len;
+}
+
+#else /* !STDC */
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+ a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
+ gzFile file;
+ const char *format;
+ int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+ a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
+{
+ int size, len;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* do the printf() into the input buffer, put length in len */
+ size = (int)(state->size);
+ state->in[size - 1] = 0;
+#ifdef NO_snprintf
+# ifdef HAS_sprintf_void
+ sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+ for (len = 0; len < size; len++)
+ if (state->in[len] == 0) break;
+# else
+ len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+# endif
+#else
+# ifdef HAS_snprintf_void
+ snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+ len = strlen(state->in);
+# else
+ len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, defer compression until needed */
+ strm->avail_in = (unsigned)len;
+ strm->next_in = state->in;
+ state->pos += len;
+ return len;
+}
+
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzflush(file, flush)
+ gzFile file;
+ int flush;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* check flush parameter */
+ if (flush < 0 || flush > Z_FINISH)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* compress remaining data with requested flush */
+ gz_comp(state, flush);
+ return state->err;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzsetparams(file, level, strategy)
+ gzFile file;
+ int level;
+ int strategy;
+{
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* if no change is requested, then do nothing */
+ if (level == state->level && strategy == state->strategy)
+ return Z_OK;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* change compression parameters for subsequent input */
+ if (state->size) {
+ /* flush previous input with previous parameters before changing */
+ if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
+ return state->err;
+ deflateParams(strm, level, strategy);
+ }
+ state->level = level;
+ state->strategy = strategy;
+ return Z_OK;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_w(file)
+ gzFile file;
+{
+ int ret = 0;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're writing */
+ if (state->mode != GZ_WRITE)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ ret += gz_zero(state, state->skip);
+ }
+
+ /* flush, free memory, and close file */
+ ret += gz_comp(state, Z_FINISH);
+ (void)deflateEnd(&(state->strm));
+ free(state->out);
+ free(state->in);
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ ret += close(state->fd);
+ free(state);
+ return ret ? Z_ERRNO : Z_OK;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/inffast.c b/test/monniaux/glpk-4.65/src/zlib/inffast.c
new file mode 100644
index 00000000..2f1d60b4
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inffast.c
@@ -0,0 +1,340 @@
+/* inffast.c -- fast decoding
+ * Copyright (C) 1995-2008, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifndef ASMINF
+
+/* Allow machine dependent optimization for post-increment or pre-increment.
+ Based on testing to date,
+ Pre-increment preferred for:
+ - PowerPC G3 (Adler)
+ - MIPS R5000 (Randers-Pehrson)
+ Post-increment preferred for:
+ - none
+ No measurable difference:
+ - Pentium III (Anderson)
+ - M68060 (Nikl)
+ */
+#ifdef POSTINC
+# define OFF 0
+# define PUP(a) *(a)++
+#else
+# define OFF 1
+# define PUP(a) *++(a)
+#endif
+
+/*
+ Decode literal, length, and distance codes and write out the resulting
+ literal and match bytes until either not enough input or output is
+ available, an end-of-block is encountered, or a data error is encountered.
+ When large enough input and output buffers are supplied to inflate(), for
+ example, a 16K input buffer and a 64K output buffer, more than 95% of the
+ inflate execution time is spent in this routine.
+
+ Entry assumptions:
+
+ state->mode == LEN
+ strm->avail_in >= 6
+ strm->avail_out >= 258
+ start >= strm->avail_out
+ state->bits < 8
+
+ On return, state->mode is one of:
+
+ LEN -- ran out of enough output space or enough available input
+ TYPE -- reached end of block code, inflate() to interpret next block
+ BAD -- error in block data
+
+ Notes:
+
+ - The maximum input bits used by a length/distance pair is 15 bits for the
+ length code, 5 bits for the length extra, 15 bits for the distance code,
+ and 13 bits for the distance extra. This totals 48 bits, or six bytes.
+ Therefore if strm->avail_in >= 6, then there is enough input to avoid
+ checking for available input while decoding.
+
+ - The maximum bytes that a single length/distance pair can output is 258
+ bytes, which is the maximum length that can be coded. inflate_fast()
+ requires strm->avail_out >= 258 for each loop to avoid checking for
+ output space.
+ */
+void ZLIB_INTERNAL inflate_fast(strm, start)
+z_streamp strm;
+unsigned start; /* inflate()'s starting value for strm->avail_out */
+{
+ struct inflate_state FAR *state;
+ unsigned char FAR *in; /* local strm->next_in */
+ unsigned char FAR *last; /* while in < last, enough input available */
+ unsigned char FAR *out; /* local strm->next_out */
+ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
+ unsigned char FAR *end; /* while out < end, enough space available */
+#ifdef INFLATE_STRICT
+ unsigned dmax; /* maximum distance from zlib header */
+#endif
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
+ unsigned long hold; /* local strm->hold */
+ unsigned bits; /* local strm->bits */
+ code const FAR *lcode; /* local strm->lencode */
+ code const FAR *dcode; /* local strm->distcode */
+ unsigned lmask; /* mask for first level of length codes */
+ unsigned dmask; /* mask for first level of distance codes */
+ code here; /* retrieved table entry */
+ unsigned op; /* code bits, operation, extra bits, or */
+ /* window position, window bytes to copy */
+ unsigned len; /* match length, unused bytes */
+ unsigned dist; /* match distance */
+ unsigned char FAR *from; /* where to copy match from */
+
+ /* copy state to local variables */
+ state = (struct inflate_state FAR *)strm->state;
+ in = strm->next_in - OFF;
+ last = in + (strm->avail_in - 5);
+ out = strm->next_out - OFF;
+ beg = out - (start - strm->avail_out);
+ end = out + (strm->avail_out - 257);
+#ifdef INFLATE_STRICT
+ dmax = state->dmax;
+#endif
+ wsize = state->wsize;
+ whave = state->whave;
+ wnext = state->wnext;
+ window = state->window;
+ hold = state->hold;
+ bits = state->bits;
+ lcode = state->lencode;
+ dcode = state->distcode;
+ lmask = (1U << state->lenbits) - 1;
+ dmask = (1U << state->distbits) - 1;
+
+ /* decode literals and length/distances until end-of-block or not enough
+ input data or output space */
+ do {
+ if (bits < 15) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ here = lcode[hold & lmask];
+ dolen:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op == 0) { /* literal */
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ PUP(out) = (unsigned char)(here.val);
+ }
+ else if (op & 16) { /* length base */
+ len = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (op) {
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ len += (unsigned)hold & ((1U << op) - 1);
+ hold >>= op;
+ bits -= op;
+ }
+ Tracevv((stderr, "inflate: length %u\n", len));
+ if (bits < 15) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ here = dcode[hold & dmask];
+ dodist:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op & 16) { /* distance base */
+ dist = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ }
+ dist += (unsigned)hold & ((1U << op) - 1);
+#ifdef INFLATE_STRICT
+ if (dist > dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ hold >>= op;
+ bits -= op;
+ Tracevv((stderr, "inflate: distance %u\n", dist));
+ op = (unsigned)(out - beg); /* max distance in output */
+ if (dist > op) { /* see if copy from window */
+ op = dist - op; /* distance back in window */
+ if (op > whave) {
+ if (state->sane) {
+ strm->msg =
+ (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ if (len <= op - whave) {
+ do {
+ PUP(out) = 0;
+ } while (--len);
+ continue;
+ }
+ len -= op - whave;
+ do {
+ PUP(out) = 0;
+ } while (--op > whave);
+ if (op == 0) {
+ from = out - dist;
+ do {
+ PUP(out) = PUP(from);
+ } while (--len);
+ continue;
+ }
+#endif
+ }
+ from = window - OFF;
+ if (wnext == 0) { /* very common case */
+ from += wsize - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ else if (wnext < op) { /* wrap around window */
+ from += wsize + wnext - op;
+ op -= wnext;
+ if (op < len) { /* some from end of window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = window - OFF;
+ if (wnext < len) { /* some from start of window */
+ op = wnext;
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ }
+ else { /* contiguous in window */
+ from += wnext - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ while (len > 2) {
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ len -= 3;
+ }
+ if (len) {
+ PUP(out) = PUP(from);
+ if (len > 1)
+ PUP(out) = PUP(from);
+ }
+ }
+ else {
+ from = out - dist; /* copy direct from output */
+ do { /* minimum length is three */
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ len -= 3;
+ } while (len > 2);
+ if (len) {
+ PUP(out) = PUP(from);
+ if (len > 1)
+ PUP(out) = PUP(from);
+ }
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level distance code */
+ here = dcode[here.val + (hold & ((1U << op) - 1))];
+ goto dodist;
+ }
+ else {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level length code */
+ here = lcode[here.val + (hold & ((1U << op) - 1))];
+ goto dolen;
+ }
+ else if (op & 32) { /* end-of-block */
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->mode = TYPE;
+ break;
+ }
+ else {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ } while (in < last && out < end);
+
+ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
+ len = bits >> 3;
+ in -= len;
+ bits -= len << 3;
+ hold &= (1U << bits) - 1;
+
+ /* update state and return */
+ strm->next_in = in + OFF;
+ strm->next_out = out + OFF;
+ strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
+ strm->avail_out = (unsigned)(out < end ?
+ 257 + (end - out) : 257 - (out - end));
+ state->hold = hold;
+ state->bits = bits;
+ return;
+}
+
+/*
+ inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
+ - Using bit fields for code structure
+ - Different op definition to avoid & for extra bits (do & for table bits)
+ - Three separate decoding do-loops for direct, window, and wnext == 0
+ - Special case for distance > 1 copies to do overlapped load and store copy
+ - Explicit branch predictions (based on measured branch probabilities)
+ - Deferring match copy and interspersed it with decoding subsequent codes
+ - Swapping literal/length else
+ - Swapping window/direct else
+ - Larger unrolled copy loops (three is about right)
+ - Moving len -= 3 statement into middle of loop
+ */
+
+#endif /* !ASMINF */
diff --git a/test/monniaux/glpk-4.65/src/zlib/inffast.h b/test/monniaux/glpk-4.65/src/zlib/inffast.h
new file mode 100644
index 00000000..e5c1aa4c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inffast.h
@@ -0,0 +1,11 @@
+/* inffast.h -- header to use inffast.c
+ * Copyright (C) 1995-2003, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
diff --git a/test/monniaux/glpk-4.65/src/zlib/inffixed.h b/test/monniaux/glpk-4.65/src/zlib/inffixed.h
new file mode 100644
index 00000000..75ed4b59
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inffixed.h
@@ -0,0 +1,94 @@
+ /* inffixed.h -- table for decoding fixed codes
+ * Generated automatically by makefixed().
+ */
+
+ /* WARNING: this file should *not* be used by applications. It
+ is part of the implementation of the compression library and
+ is subject to change. Applications should only use zlib.h.
+ */
+
+ static const code lenfix[512] = {
+ {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
+ {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
+ {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
+ {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
+ {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
+ {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
+ {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
+ {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
+ {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
+ {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
+ {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
+ {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
+ {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
+ {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
+ {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
+ {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
+ {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
+ {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
+ {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
+ {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
+ {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
+ {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
+ {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
+ {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
+ {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
+ {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
+ {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
+ {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
+ {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
+ {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
+ {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
+ {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
+ {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
+ {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
+ {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
+ {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
+ {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
+ {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
+ {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
+ {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
+ {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
+ {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
+ {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
+ {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
+ {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
+ {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
+ {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
+ {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
+ {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
+ {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
+ {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
+ {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
+ {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
+ {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
+ {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
+ {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
+ {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
+ {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
+ {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
+ {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
+ {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
+ {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
+ {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
+ {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
+ {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
+ {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
+ {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
+ {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
+ {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
+ {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
+ {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
+ {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
+ {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
+ {0,9,255}
+ };
+
+ static const code distfix[32] = {
+ {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
+ {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
+ {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
+ {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
+ {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
+ {22,5,193},{64,5,0}
+ };
diff --git a/test/monniaux/glpk-4.65/src/zlib/inflate.c b/test/monniaux/glpk-4.65/src/zlib/inflate.c
new file mode 100644
index 00000000..a8431abe
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inflate.c
@@ -0,0 +1,1480 @@
+/* inflate.c -- zlib decompression
+ * Copyright (C) 1995-2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * Change history:
+ *
+ * 1.2.beta0 24 Nov 2002
+ * - First version -- complete rewrite of inflate to simplify code, avoid
+ * creation of window when not needed, minimize use of window when it is
+ * needed, make inffast.c even faster, implement gzip decoding, and to
+ * improve code readability and style over the previous zlib inflate code
+ *
+ * 1.2.beta1 25 Nov 2002
+ * - Use pointers for available input and output checking in inffast.c
+ * - Remove input and output counters in inffast.c
+ * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
+ * - Remove unnecessary second byte pull from length extra in inffast.c
+ * - Unroll direct copy to three copies per loop in inffast.c
+ *
+ * 1.2.beta2 4 Dec 2002
+ * - Change external routine names to reduce potential conflicts
+ * - Correct filename to inffixed.h for fixed tables in inflate.c
+ * - Make hbuf[] unsigned char to match parameter type in inflate.c
+ * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
+ * to avoid negation problem on Alphas (64 bit) in inflate.c
+ *
+ * 1.2.beta3 22 Dec 2002
+ * - Add comments on state->bits assertion in inffast.c
+ * - Add comments on op field in inftrees.h
+ * - Fix bug in reuse of allocated window after inflateReset()
+ * - Remove bit fields--back to byte structure for speed
+ * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
+ * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
+ * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
+ * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
+ * - Use local copies of stream next and avail values, as well as local bit
+ * buffer and bit count in inflate()--for speed when inflate_fast() not used
+ *
+ * 1.2.beta4 1 Jan 2003
+ * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
+ * - Move a comment on output buffer sizes from inffast.c to inflate.c
+ * - Add comments in inffast.c to introduce the inflate_fast() routine
+ * - Rearrange window copies in inflate_fast() for speed and simplification
+ * - Unroll last copy for window match in inflate_fast()
+ * - Use local copies of window variables in inflate_fast() for speed
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
+ * - Make op and len in inflate_fast() unsigned for consistency
+ * - Add FAR to lcode and dcode declarations in inflate_fast()
+ * - Simplified bad distance check in inflate_fast()
+ * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
+ * source file infback.c to provide a call-back interface to inflate for
+ * programs like gzip and unzip -- uses window as output buffer to avoid
+ * window copying
+ *
+ * 1.2.beta5 1 Jan 2003
+ * - Improved inflateBack() interface to allow the caller to provide initial
+ * input in strm.
+ * - Fixed stored blocks bug in inflateBack()
+ *
+ * 1.2.beta6 4 Jan 2003
+ * - Added comments in inffast.c on effectiveness of POSTINC
+ * - Typecasting all around to reduce compiler warnings
+ * - Changed loops from while (1) or do {} while (1) to for (;;), again to
+ * make compilers happy
+ * - Changed type of window in inflateBackInit() to unsigned char *
+ *
+ * 1.2.beta7 27 Jan 2003
+ * - Changed many types to unsigned or unsigned short to avoid warnings
+ * - Added inflateCopy() function
+ *
+ * 1.2.0 9 Mar 2003
+ * - Changed inflateBack() interface to provide separate opaque descriptors
+ * for the in() and out() functions
+ * - Changed inflateBack() argument and in_func typedef to swap the length
+ * and buffer address return values for the input function
+ * - Check next_in and next_out for Z_NULL on entry to inflate()
+ *
+ * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifdef MAKEFIXED
+# ifndef BUILDFIXED
+# define BUILDFIXED
+# endif
+#endif
+
+/* function prototypes */
+local void fixedtables OF((struct inflate_state FAR *state));
+local int updatewindow OF((z_streamp strm, unsigned out));
+#ifdef BUILDFIXED
+ void makefixed OF((void));
+#endif
+local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
+ unsigned len));
+
+int ZEXPORT inflateReset(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ strm->total_in = strm->total_out = state->total = 0;
+ strm->msg = Z_NULL;
+ strm->adler = 1; /* to support ill-conceived Java test suite */
+ state->mode = HEAD;
+ state->last = 0;
+ state->havedict = 0;
+ state->dmax = 32768U;
+ state->head = Z_NULL;
+ state->wsize = 0;
+ state->whave = 0;
+ state->wnext = 0;
+ state->hold = 0;
+ state->bits = 0;
+ state->lencode = state->distcode = state->next = state->codes;
+ state->sane = 1;
+ state->back = -1;
+ Tracev((stderr, "inflate: reset\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateReset2(strm, windowBits)
+z_streamp strm;
+int windowBits;
+{
+ int wrap;
+ struct inflate_state FAR *state;
+
+ /* get the state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* extract wrap request from windowBits parameter */
+ if (windowBits < 0) {
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+ else {
+ wrap = (windowBits >> 4) + 1;
+#ifdef GUNZIP
+ if (windowBits < 48)
+ windowBits &= 15;
+#endif
+ }
+
+ /* set number of window bits, free window if different */
+ if (windowBits && (windowBits < 8 || windowBits > 15))
+ return Z_STREAM_ERROR;
+ if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
+ ZFREE(strm, state->window);
+ state->window = Z_NULL;
+ }
+
+ /* update state and reset the rest of it */
+ state->wrap = wrap;
+ state->wbits = (unsigned)windowBits;
+ return inflateReset(strm);
+}
+
+int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
+z_streamp strm;
+int windowBits;
+const char *version;
+int stream_size;
+{
+ int ret;
+ struct inflate_state FAR *state;
+
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+ stream_size != (int)(sizeof(z_stream)))
+ return Z_VERSION_ERROR;
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+ strm->msg = Z_NULL; /* in case we return an error */
+ if (strm->zalloc == (alloc_func)0) {
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+ }
+ if (strm->zfree == (free_func)0) strm->zfree = zcfree;
+ state = (struct inflate_state FAR *)
+ ZALLOC(strm, 1, sizeof(struct inflate_state));
+ if (state == Z_NULL) return Z_MEM_ERROR;
+ Tracev((stderr, "inflate: allocated\n"));
+ strm->state = (struct internal_state FAR *)state;
+ state->window = Z_NULL;
+ ret = inflateReset2(strm, windowBits);
+ if (ret != Z_OK) {
+ ZFREE(strm, state);
+ strm->state = Z_NULL;
+ }
+ return ret;
+}
+
+int ZEXPORT inflateInit_(strm, version, stream_size)
+z_streamp strm;
+const char *version;
+int stream_size;
+{
+ return inflateInit2_(strm, DEF_WBITS, version, stream_size);
+}
+
+int ZEXPORT inflatePrime(strm, bits, value)
+z_streamp strm;
+int bits;
+int value;
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (bits < 0) {
+ state->hold = 0;
+ state->bits = 0;
+ return Z_OK;
+ }
+ if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
+ value &= (1L << bits) - 1;
+ state->hold += value << state->bits;
+ state->bits += bits;
+ return Z_OK;
+}
+
+/*
+ Return state with length and distance decoding tables and index sizes set to
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
+ If BUILDFIXED is defined, then instead this routine builds the tables the
+ first time it's called, and returns those tables the first time and
+ thereafter. This reduces the size of the code by about 2K bytes, in
+ exchange for a little execution time. However, BUILDFIXED should not be
+ used for threaded applications, since the rewriting of the tables and virgin
+ may not be thread-safe.
+ */
+local void fixedtables(state)
+struct inflate_state FAR *state;
+{
+#ifdef BUILDFIXED
+ static int virgin = 1;
+ static code *lenfix, *distfix;
+ static code fixed[544];
+
+ /* build fixed huffman tables if first call (may not be thread safe) */
+ if (virgin) {
+ unsigned sym, bits;
+ static code *next;
+
+ /* literal/length table */
+ sym = 0;
+ while (sym < 144) state->lens[sym++] = 8;
+ while (sym < 256) state->lens[sym++] = 9;
+ while (sym < 280) state->lens[sym++] = 7;
+ while (sym < 288) state->lens[sym++] = 8;
+ next = fixed;
+ lenfix = next;
+ bits = 9;
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
+
+ /* distance table */
+ sym = 0;
+ while (sym < 32) state->lens[sym++] = 5;
+ distfix = next;
+ bits = 5;
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
+
+ /* do this just once */
+ virgin = 0;
+ }
+#else /* !BUILDFIXED */
+# include "inffixed.h"
+#endif /* BUILDFIXED */
+ state->lencode = lenfix;
+ state->lenbits = 9;
+ state->distcode = distfix;
+ state->distbits = 5;
+}
+
+#ifdef MAKEFIXED
+#include <stdio.h>
+
+/*
+ Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
+ defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
+ those tables to stdout, which would be piped to inffixed.h. A small program
+ can simply call makefixed to do this:
+
+ void makefixed(void);
+
+ int main(void)
+ {
+ makefixed();
+ return 0;
+ }
+
+ Then that can be linked with zlib built with MAKEFIXED defined and run:
+
+ a.out > inffixed.h
+ */
+void makefixed()
+{
+ unsigned low, size;
+ struct inflate_state state;
+
+ fixedtables(&state);
+ puts(" /* inffixed.h -- table for decoding fixed codes");
+ puts(" * Generated automatically by makefixed().");
+ puts(" */");
+ puts("");
+ puts(" /* WARNING: this file should *not* be used by applications.");
+ puts(" It is part of the implementation of this library and is");
+ puts(" subject to change. Applications should only use zlib.h.");
+ puts(" */");
+ puts("");
+ size = 1U << 9;
+ printf(" static const code lenfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 7) == 0) printf("\n ");
+ printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
+ state.lencode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+ size = 1U << 5;
+ printf("\n static const code distfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 6) == 0) printf("\n ");
+ printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
+ state.distcode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+}
+#endif /* MAKEFIXED */
+
+/*
+ Update the window with the last wsize (normally 32K) bytes written before
+ returning. If window does not exist yet, create it. This is only called
+ when a window is already in use, or when output has been written during this
+ inflate call, but the end of the deflate stream has not been reached yet.
+ It is also called to create a window for dictionary data when a dictionary
+ is loaded.
+
+ Providing output buffers larger than 32K to inflate() should provide a speed
+ advantage, since only the last 32K of output is copied to the sliding window
+ upon return from inflate(), and since all distances after the first 32K of
+ output will fall in the output data, making match copies simpler and faster.
+ The advantage may be dependent on the size of the processor's data caches.
+ */
+local int updatewindow(strm, out)
+z_streamp strm;
+unsigned out;
+{
+ struct inflate_state FAR *state;
+ unsigned copy, dist;
+
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* if it hasn't been done already, allocate space for the window */
+ if (state->window == Z_NULL) {
+ state->window = (unsigned char FAR *)
+ ZALLOC(strm, 1U << state->wbits,
+ sizeof(unsigned char));
+ if (state->window == Z_NULL) return 1;
+ }
+
+ /* if window not in use yet, initialize */
+ if (state->wsize == 0) {
+ state->wsize = 1U << state->wbits;
+ state->wnext = 0;
+ state->whave = 0;
+ }
+
+ /* copy state->wsize or less output bytes into the circular window */
+ copy = out - strm->avail_out;
+ if (copy >= state->wsize) {
+ zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
+ state->wnext = 0;
+ state->whave = state->wsize;
+ }
+ else {
+ dist = state->wsize - state->wnext;
+ if (dist > copy) dist = copy;
+ zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
+ copy -= dist;
+ if (copy) {
+ zmemcpy(state->window, strm->next_out - copy, copy);
+ state->wnext = copy;
+ state->whave = state->wsize;
+ }
+ else {
+ state->wnext += dist;
+ if (state->wnext == state->wsize) state->wnext = 0;
+ if (state->whave < state->wsize) state->whave += dist;
+ }
+ }
+ return 0;
+}
+
+/* Macros for inflate(): */
+
+/* check function to use adler32() for zlib or crc32() for gzip */
+#ifdef GUNZIP
+# define UPDATE(check, buf, len) \
+ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
+#else
+# define UPDATE(check, buf, len) adler32(check, buf, len)
+#endif
+
+/* check macros for header crc */
+#ifdef GUNZIP
+# define CRC2(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ check = crc32(check, hbuf, 2); \
+ } while (0)
+
+# define CRC4(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ hbuf[2] = (unsigned char)((word) >> 16); \
+ hbuf[3] = (unsigned char)((word) >> 24); \
+ check = crc32(check, hbuf, 4); \
+ } while (0)
+#endif
+
+/* Load registers with state in inflate() for speed */
+#define LOAD() \
+ do { \
+ put = strm->next_out; \
+ left = strm->avail_out; \
+ next = strm->next_in; \
+ have = strm->avail_in; \
+ hold = state->hold; \
+ bits = state->bits; \
+ } while (0)
+
+/* Restore state from registers in inflate() */
+#define RESTORE() \
+ do { \
+ strm->next_out = put; \
+ strm->avail_out = left; \
+ strm->next_in = next; \
+ strm->avail_in = have; \
+ state->hold = hold; \
+ state->bits = bits; \
+ } while (0)
+
+/* Clear the input bit accumulator */
+#define INITBITS() \
+ do { \
+ hold = 0; \
+ bits = 0; \
+ } while (0)
+
+/* Get a byte of input into the bit accumulator, or return from inflate()
+ if there is no input available. */
+#define PULLBYTE() \
+ do { \
+ if (have == 0) goto inf_leave; \
+ have--; \
+ hold += (unsigned long)(*next++) << bits; \
+ bits += 8; \
+ } while (0)
+
+/* Assure that there are at least n bits in the bit accumulator. If there is
+ not enough available input to do that, then return from inflate(). */
+#define NEEDBITS(n) \
+ do { \
+ while (bits < (unsigned)(n)) \
+ PULLBYTE(); \
+ } while (0)
+
+/* Return the low n bits of the bit accumulator (n < 16) */
+#define BITS(n) \
+ ((unsigned)hold & ((1U << (n)) - 1))
+
+/* Remove n bits from the bit accumulator */
+#define DROPBITS(n) \
+ do { \
+ hold >>= (n); \
+ bits -= (unsigned)(n); \
+ } while (0)
+
+/* Remove zero to seven bits as needed to go to a byte boundary */
+#define BYTEBITS() \
+ do { \
+ hold >>= bits & 7; \
+ bits -= bits & 7; \
+ } while (0)
+
+/* Reverse the bytes in a 32-bit value */
+#define REVERSE(q) \
+ ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+ (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+
+/*
+ inflate() uses a state machine to process as much input data and generate as
+ much output data as possible before returning. The state machine is
+ structured roughly as follows:
+
+ for (;;) switch (state) {
+ ...
+ case STATEn:
+ if (not enough input data or output space to make progress)
+ return;
+ ... make progress ...
+ state = STATEm;
+ break;
+ ...
+ }
+
+ so when inflate() is called again, the same case is attempted again, and
+ if the appropriate resources are provided, the machine proceeds to the
+ next state. The NEEDBITS() macro is usually the way the state evaluates
+ whether it can proceed or should return. NEEDBITS() does the return if
+ the requested bits are not available. The typical use of the BITS macros
+ is:
+
+ NEEDBITS(n);
+ ... do something with BITS(n) ...
+ DROPBITS(n);
+
+ where NEEDBITS(n) either returns from inflate() if there isn't enough
+ input left to load n bits into the accumulator, or it continues. BITS(n)
+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
+ the low n bits off the accumulator. INITBITS() clears the accumulator
+ and sets the number of available bits to zero. BYTEBITS() discards just
+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
+
+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
+ if there is no input available. The decoding of variable length codes uses
+ PULLBYTE() directly in order to pull just enough bytes to decode the next
+ code, and no more.
+
+ Some states loop until they get enough input, making sure that enough
+ state information is maintained to continue the loop where it left off
+ if NEEDBITS() returns in the loop. For example, want, need, and keep
+ would all have to actually be part of the saved state in case NEEDBITS()
+ returns:
+
+ case STATEw:
+ while (want < need) {
+ NEEDBITS(n);
+ keep[want++] = BITS(n);
+ DROPBITS(n);
+ }
+ state = STATEx;
+ case STATEx:
+
+ As shown above, if the next state is also the next case, then the break
+ is omitted.
+
+ A state may also return if there is not enough output space available to
+ complete that state. Those states are copying stored data, writing a
+ literal byte, and copying a matching string.
+
+ When returning, a "goto inf_leave" is used to update the total counters,
+ update the check value, and determine whether any progress has been made
+ during that inflate() call in order to return the proper return code.
+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
+ When there is a window, goto inf_leave will update the window with the last
+ output written. If a goto inf_leave occurs in the middle of decompression
+ and there is no window currently, goto inf_leave will create one and copy
+ output to the window for the next call of inflate().
+
+ In this implementation, the flush parameter of inflate() only affects the
+ return code (per zlib.h). inflate() always writes as much as possible to
+ strm->next_out, given the space available and the provided input--the effect
+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
+ the allocation of and copying into a sliding window until necessary, which
+ provides the effect documented in zlib.h for Z_FINISH when the entire input
+ stream available. So the only thing the flush parameter actually does is:
+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
+ will return Z_BUF_ERROR if it has not reached the end of the stream.
+ */
+
+int ZEXPORT inflate(strm, flush)
+z_streamp strm;
+int flush;
+{
+ struct inflate_state FAR *state;
+ unsigned char FAR *next; /* next input */
+ unsigned char FAR *put; /* next output */
+ unsigned have, left; /* available input and output */
+ unsigned long hold; /* bit buffer */
+ unsigned bits; /* bits in bit buffer */
+ unsigned in, out; /* save starting available input and output */
+ unsigned copy; /* number of stored or match bytes to copy */
+ unsigned char FAR *from; /* where to copy match bytes from */
+ code here; /* current decoding table entry */
+ code last; /* parent table entry */
+ unsigned len; /* length to copy for repeats, bits to drop */
+ int ret; /* return code */
+#ifdef GUNZIP
+ unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
+#endif
+ static const unsigned short order[19] = /* permutation of code lengths */
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
+ (strm->next_in == Z_NULL && strm->avail_in != 0))
+ return Z_STREAM_ERROR;
+
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
+ LOAD();
+ in = have;
+ out = left;
+ ret = Z_OK;
+ for (;;)
+ switch (state->mode) {
+ case HEAD:
+ if (state->wrap == 0) {
+ state->mode = TYPEDO;
+ break;
+ }
+ NEEDBITS(16);
+#ifdef GUNZIP
+ if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
+ state->check = crc32(0L, Z_NULL, 0);
+ CRC2(state->check, hold);
+ INITBITS();
+ state->mode = FLAGS;
+ break;
+ }
+ state->flags = 0; /* expect zlib header */
+ if (state->head != Z_NULL)
+ state->head->done = -1;
+ if (!(state->wrap & 1) || /* check if zlib header allowed */
+#else
+ if (
+#endif
+ ((BITS(8) << 8) + (hold >> 8)) % 31) {
+ strm->msg = (char *)"incorrect header check";
+ state->mode = BAD;
+ break;
+ }
+ if (BITS(4) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ DROPBITS(4);
+ len = BITS(4) + 8;
+ if (state->wbits == 0)
+ state->wbits = len;
+ else if (len > state->wbits) {
+ strm->msg = (char *)"invalid window size";
+ state->mode = BAD;
+ break;
+ }
+ state->dmax = 1U << len;
+ Tracev((stderr, "inflate: zlib header ok\n"));
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = hold & 0x200 ? DICTID : TYPE;
+ INITBITS();
+ break;
+#ifdef GUNZIP
+ case FLAGS:
+ NEEDBITS(16);
+ state->flags = (int)(hold);
+ if ((state->flags & 0xff) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ if (state->flags & 0xe000) {
+ strm->msg = (char *)"unknown header flags set";
+ state->mode = BAD;
+ break;
+ }
+ if (state->head != Z_NULL)
+ state->head->text = (int)((hold >> 8) & 1);
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ state->mode = TIME;
+ case TIME:
+ NEEDBITS(32);
+ if (state->head != Z_NULL)
+ state->head->time = hold;
+ if (state->flags & 0x0200) CRC4(state->check, hold);
+ INITBITS();
+ state->mode = OS;
+ case OS:
+ NEEDBITS(16);
+ if (state->head != Z_NULL) {
+ state->head->xflags = (int)(hold & 0xff);
+ state->head->os = (int)(hold >> 8);
+ }
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ state->mode = EXLEN;
+ case EXLEN:
+ if (state->flags & 0x0400) {
+ NEEDBITS(16);
+ state->length = (unsigned)(hold);
+ if (state->head != Z_NULL)
+ state->head->extra_len = (unsigned)hold;
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ }
+ else if (state->head != Z_NULL)
+ state->head->extra = Z_NULL;
+ state->mode = EXTRA;
+ case EXTRA:
+ if (state->flags & 0x0400) {
+ copy = state->length;
+ if (copy > have) copy = have;
+ if (copy) {
+ if (state->head != Z_NULL &&
+ state->head->extra != Z_NULL) {
+ len = state->head->extra_len - state->length;
+ zmemcpy(state->head->extra + len, next,
+ len + copy > state->head->extra_max ?
+ state->head->extra_max - len : copy);
+ }
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ state->length -= copy;
+ }
+ if (state->length) goto inf_leave;
+ }
+ state->length = 0;
+ state->mode = NAME;
+ case NAME:
+ if (state->flags & 0x0800) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->name != Z_NULL &&
+ state->length < state->head->name_max)
+ state->head->name[state->length++] = len;
+ } while (len && copy < have);
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->name = Z_NULL;
+ state->length = 0;
+ state->mode = COMMENT;
+ case COMMENT:
+ if (state->flags & 0x1000) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->comment != Z_NULL &&
+ state->length < state->head->comm_max)
+ state->head->comment[state->length++] = len;
+ } while (len && copy < have);
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->comment = Z_NULL;
+ state->mode = HCRC;
+ case HCRC:
+ if (state->flags & 0x0200) {
+ NEEDBITS(16);
+ if (hold != (state->check & 0xffff)) {
+ strm->msg = (char *)"header crc mismatch";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ }
+ if (state->head != Z_NULL) {
+ state->head->hcrc = (int)((state->flags >> 9) & 1);
+ state->head->done = 1;
+ }
+ strm->adler = state->check = crc32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ break;
+#endif
+ case DICTID:
+ NEEDBITS(32);
+ strm->adler = state->check = REVERSE(hold);
+ INITBITS();
+ state->mode = DICT;
+ case DICT:
+ if (state->havedict == 0) {
+ RESTORE();
+ return Z_NEED_DICT;
+ }
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ case TYPE:
+ if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
+ case TYPEDO:
+ if (state->last) {
+ BYTEBITS();
+ state->mode = CHECK;
+ break;
+ }
+ NEEDBITS(3);
+ state->last = BITS(1);
+ DROPBITS(1);
+ switch (BITS(2)) {
+ case 0: /* stored block */
+ Tracev((stderr, "inflate: stored block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = STORED;
+ break;
+ case 1: /* fixed block */
+ fixedtables(state);
+ Tracev((stderr, "inflate: fixed codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = LEN_; /* decode codes */
+ if (flush == Z_TREES) {
+ DROPBITS(2);
+ goto inf_leave;
+ }
+ break;
+ case 2: /* dynamic block */
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = TABLE;
+ break;
+ case 3:
+ strm->msg = (char *)"invalid block type";
+ state->mode = BAD;
+ }
+ DROPBITS(2);
+ break;
+ case STORED:
+ BYTEBITS(); /* go to byte boundary */
+ NEEDBITS(32);
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
+ strm->msg = (char *)"invalid stored block lengths";
+ state->mode = BAD;
+ break;
+ }
+ state->length = (unsigned)hold & 0xffff;
+ Tracev((stderr, "inflate: stored length %u\n",
+ state->length));
+ INITBITS();
+ state->mode = COPY_;
+ if (flush == Z_TREES) goto inf_leave;
+ case COPY_:
+ state->mode = COPY;
+ case COPY:
+ copy = state->length;
+ if (copy) {
+ if (copy > have) copy = have;
+ if (copy > left) copy = left;
+ if (copy == 0) goto inf_leave;
+ zmemcpy(put, next, copy);
+ have -= copy;
+ next += copy;
+ left -= copy;
+ put += copy;
+ state->length -= copy;
+ break;
+ }
+ Tracev((stderr, "inflate: stored end\n"));
+ state->mode = TYPE;
+ break;
+ case TABLE:
+ NEEDBITS(14);
+ state->nlen = BITS(5) + 257;
+ DROPBITS(5);
+ state->ndist = BITS(5) + 1;
+ DROPBITS(5);
+ state->ncode = BITS(4) + 4;
+ DROPBITS(4);
+#ifndef PKZIP_BUG_WORKAROUND
+ if (state->nlen > 286 || state->ndist > 30) {
+ strm->msg = (char *)"too many length or distance symbols";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracev((stderr, "inflate: table sizes ok\n"));
+ state->have = 0;
+ state->mode = LENLENS;
+ case LENLENS:
+ while (state->have < state->ncode) {
+ NEEDBITS(3);
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
+ DROPBITS(3);
+ }
+ while (state->have < 19)
+ state->lens[order[state->have++]] = 0;
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 7;
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid code lengths set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: code lengths ok\n"));
+ state->have = 0;
+ state->mode = CODELENS;
+ case CODELENS:
+ while (state->have < state->nlen + state->ndist) {
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.val < 16) {
+ NEEDBITS(here.bits);
+ DROPBITS(here.bits);
+ state->lens[state->have++] = here.val;
+ }
+ else {
+ if (here.val == 16) {
+ NEEDBITS(here.bits + 2);
+ DROPBITS(here.bits);
+ if (state->have == 0) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ len = state->lens[state->have - 1];
+ copy = 3 + BITS(2);
+ DROPBITS(2);
+ }
+ else if (here.val == 17) {
+ NEEDBITS(here.bits + 3);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 3 + BITS(3);
+ DROPBITS(3);
+ }
+ else {
+ NEEDBITS(here.bits + 7);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 11 + BITS(7);
+ DROPBITS(7);
+ }
+ if (state->have + copy > state->nlen + state->ndist) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ while (copy--)
+ state->lens[state->have++] = (unsigned short)len;
+ }
+ }
+
+ /* handle error breaks in while */
+ if (state->mode == BAD) break;
+
+ /* check for end-of-block code (better have one) */
+ if (state->lens[256] == 0) {
+ strm->msg = (char *)"invalid code -- missing end-of-block";
+ state->mode = BAD;
+ break;
+ }
+
+ /* build code tables -- note: do not change the lenbits or distbits
+ values here (9 and 6) without reading the comments in inftrees.h
+ concerning the ENOUGH constants, which depend on those values */
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 9;
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid literal/lengths set";
+ state->mode = BAD;
+ break;
+ }
+ state->distcode = (code const FAR *)(state->next);
+ state->distbits = 6;
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
+ &(state->next), &(state->distbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid distances set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: codes ok\n"));
+ state->mode = LEN_;
+ if (flush == Z_TREES) goto inf_leave;
+ case LEN_:
+ state->mode = LEN;
+ case LEN:
+ if (have >= 6 && left >= 258) {
+ RESTORE();
+ inflate_fast(strm, out);
+ LOAD();
+ if (state->mode == TYPE)
+ state->back = -1;
+ break;
+ }
+ state->back = 0;
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.op && (here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->lencode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ state->length = (unsigned)here.val;
+ if ((int)(here.op) == 0) {
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ state->mode = LIT;
+ break;
+ }
+ if (here.op & 32) {
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->back = -1;
+ state->mode = TYPE;
+ break;
+ }
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = LENEXT;
+ case LENEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->length += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+ Tracevv((stderr, "inflate: length %u\n", state->length));
+ state->was = state->length;
+ state->mode = DIST;
+ case DIST:
+ for (;;) {
+ here = state->distcode[BITS(state->distbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if ((here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->distcode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ state->offset = (unsigned)here.val;
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = DISTEXT;
+ case DISTEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->offset += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+#ifdef INFLATE_STRICT
+ if (state->offset > state->dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
+ state->mode = MATCH;
+ case MATCH:
+ if (left == 0) goto inf_leave;
+ copy = out - left;
+ if (state->offset > copy) { /* copy from window */
+ copy = state->offset - copy;
+ if (copy > state->whave) {
+ if (state->sane) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ Trace((stderr, "inflate.c too far\n"));
+ copy -= state->whave;
+ if (copy > state->length) copy = state->length;
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = 0;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+#endif
+ }
+ if (copy > state->wnext) {
+ copy -= state->wnext;
+ from = state->window + (state->wsize - copy);
+ }
+ else
+ from = state->window + (state->wnext - copy);
+ if (copy > state->length) copy = state->length;
+ }
+ else { /* copy from output */
+ from = put - state->offset;
+ copy = state->length;
+ }
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = *from++;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+ case LIT:
+ if (left == 0) goto inf_leave;
+ *put++ = (unsigned char)(state->length);
+ left--;
+ state->mode = LEN;
+ break;
+ case CHECK:
+ if (state->wrap) {
+ NEEDBITS(32);
+ out -= left;
+ strm->total_out += out;
+ state->total += out;
+ if (out)
+ strm->adler = state->check =
+ UPDATE(state->check, put - out, out);
+ out = left;
+ if ((
+#ifdef GUNZIP
+ state->flags ? hold :
+#endif
+ REVERSE(hold)) != state->check) {
+ strm->msg = (char *)"incorrect data check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: check matches trailer\n"));
+ }
+#ifdef GUNZIP
+ state->mode = LENGTH;
+ case LENGTH:
+ if (state->wrap && state->flags) {
+ NEEDBITS(32);
+ if (hold != (state->total & 0xffffffffUL)) {
+ strm->msg = (char *)"incorrect length check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: length matches trailer\n"));
+ }
+#endif
+ state->mode = DONE;
+ case DONE:
+ ret = Z_STREAM_END;
+ goto inf_leave;
+ case BAD:
+ ret = Z_DATA_ERROR;
+ goto inf_leave;
+ case MEM:
+ return Z_MEM_ERROR;
+ case SYNC:
+ default:
+ return Z_STREAM_ERROR;
+ }
+
+ /*
+ Return from inflate(), updating the total counts and the check value.
+ If there was no progress during the inflate() call, return a buffer
+ error. Call updatewindow() to create and/or update the window state.
+ Note: a memory error from inflate() is non-recoverable.
+ */
+ inf_leave:
+ RESTORE();
+ if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
+ if (updatewindow(strm, out)) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ in -= strm->avail_in;
+ out -= strm->avail_out;
+ strm->total_in += in;
+ strm->total_out += out;
+ state->total += out;
+ if (state->wrap && out)
+ strm->adler = state->check =
+ UPDATE(state->check, strm->next_out - out, out);
+ strm->data_type = state->bits + (state->last ? 64 : 0) +
+ (state->mode == TYPE ? 128 : 0) +
+ (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
+ if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
+ ret = Z_BUF_ERROR;
+ return ret;
+}
+
+int ZEXPORT inflateEnd(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->window != Z_NULL) ZFREE(strm, state->window);
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+ Tracev((stderr, "inflate: end\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
+z_streamp strm;
+const Bytef *dictionary;
+uInt dictLength;
+{
+ struct inflate_state FAR *state;
+ unsigned long id;
+
+ /* check state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->wrap != 0 && state->mode != DICT)
+ return Z_STREAM_ERROR;
+
+ /* check for correct dictionary id */
+ if (state->mode == DICT) {
+ id = adler32(0L, Z_NULL, 0);
+ id = adler32(id, dictionary, dictLength);
+ if (id != state->check)
+ return Z_DATA_ERROR;
+ }
+
+ /* copy dictionary to window */
+ if (updatewindow(strm, strm->avail_out)) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ if (dictLength > state->wsize) {
+ zmemcpy(state->window, dictionary + dictLength - state->wsize,
+ state->wsize);
+ state->whave = state->wsize;
+ }
+ else {
+ zmemcpy(state->window + state->wsize - dictLength, dictionary,
+ dictLength);
+ state->whave = dictLength;
+ }
+ state->havedict = 1;
+ Tracev((stderr, "inflate: dictionary set\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateGetHeader(strm, head)
+z_streamp strm;
+gz_headerp head;
+{
+ struct inflate_state FAR *state;
+
+ /* check state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
+
+ /* save header structure */
+ state->head = head;
+ head->done = 0;
+ return Z_OK;
+}
+
+/*
+ Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
+ or when out of input. When called, *have is the number of pattern bytes
+ found in order so far, in 0..3. On return *have is updated to the new
+ state. If on return *have equals four, then the pattern was found and the
+ return value is how many bytes were read including the last byte of the
+ pattern. If *have is less than four, then the pattern has not been found
+ yet and the return value is len. In the latter case, syncsearch() can be
+ called again with more data and the *have state. *have is initialized to
+ zero for the first call.
+ */
+local unsigned syncsearch(have, buf, len)
+unsigned FAR *have;
+unsigned char FAR *buf;
+unsigned len;
+{
+ unsigned got;
+ unsigned next;
+
+ got = *have;
+ next = 0;
+ while (next < len && got < 4) {
+ if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
+ got++;
+ else if (buf[next])
+ got = 0;
+ else
+ got = 4 - got;
+ next++;
+ }
+ *have = got;
+ return next;
+}
+
+int ZEXPORT inflateSync(strm)
+z_streamp strm;
+{
+ unsigned len; /* number of bytes to look at or looked at */
+ unsigned long in, out; /* temporary to save total_in and total_out */
+ unsigned char buf[4]; /* to restore bit buffer to byte string */
+ struct inflate_state FAR *state;
+
+ /* check parameters */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
+
+ /* if first time, start search in bit buffer */
+ if (state->mode != SYNC) {
+ state->mode = SYNC;
+ state->hold <<= state->bits & 7;
+ state->bits -= state->bits & 7;
+ len = 0;
+ while (state->bits >= 8) {
+ buf[len++] = (unsigned char)(state->hold);
+ state->hold >>= 8;
+ state->bits -= 8;
+ }
+ state->have = 0;
+ syncsearch(&(state->have), buf, len);
+ }
+
+ /* search available input */
+ len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
+ strm->avail_in -= len;
+ strm->next_in += len;
+ strm->total_in += len;
+
+ /* return no joy or set up to restart inflate() on a new block */
+ if (state->have != 4) return Z_DATA_ERROR;
+ in = strm->total_in; out = strm->total_out;
+ inflateReset(strm);
+ strm->total_in = in; strm->total_out = out;
+ state->mode = TYPE;
+ return Z_OK;
+}
+
+/*
+ Returns true if inflate is currently at the end of a block generated by
+ Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
+ implementation to provide an additional safety check. PPP uses
+ Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
+ block. When decompressing, PPP checks that at the end of input packet,
+ inflate is waiting for these length bytes.
+ */
+int ZEXPORT inflateSyncPoint(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ return state->mode == STORED && state->bits == 0;
+}
+
+int ZEXPORT inflateCopy(dest, source)
+z_streamp dest;
+z_streamp source;
+{
+ struct inflate_state FAR *state;
+ struct inflate_state FAR *copy;
+ unsigned char FAR *window;
+ unsigned wsize;
+
+ /* check input */
+ if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
+ source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)source->state;
+
+ /* allocate space */
+ copy = (struct inflate_state FAR *)
+ ZALLOC(source, 1, sizeof(struct inflate_state));
+ if (copy == Z_NULL) return Z_MEM_ERROR;
+ window = Z_NULL;
+ if (state->window != Z_NULL) {
+ window = (unsigned char FAR *)
+ ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
+ if (window == Z_NULL) {
+ ZFREE(source, copy);
+ return Z_MEM_ERROR;
+ }
+ }
+
+ /* copy state */
+ zmemcpy(dest, source, sizeof(z_stream));
+ zmemcpy(copy, state, sizeof(struct inflate_state));
+ if (state->lencode >= state->codes &&
+ state->lencode <= state->codes + ENOUGH - 1) {
+ copy->lencode = copy->codes + (state->lencode - state->codes);
+ copy->distcode = copy->codes + (state->distcode - state->codes);
+ }
+ copy->next = copy->codes + (state->next - state->codes);
+ if (window != Z_NULL) {
+ wsize = 1U << state->wbits;
+ zmemcpy(window, state->window, wsize);
+ }
+ copy->window = window;
+ dest->state = (struct internal_state FAR *)copy;
+ return Z_OK;
+}
+
+int ZEXPORT inflateUndermine(strm, subvert)
+z_streamp strm;
+int subvert;
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ state->sane = !subvert;
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ return Z_OK;
+#else
+ state->sane = 1;
+ return Z_DATA_ERROR;
+#endif
+}
+
+long ZEXPORT inflateMark(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
+ state = (struct inflate_state FAR *)strm->state;
+ return ((long)(state->back) << 16) +
+ (state->mode == COPY ? state->length :
+ (state->mode == MATCH ? state->was - state->length : 0));
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/inflate.h b/test/monniaux/glpk-4.65/src/zlib/inflate.h
new file mode 100644
index 00000000..95f4986d
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inflate.h
@@ -0,0 +1,122 @@
+/* inflate.h -- internal inflate state definition
+ * Copyright (C) 1995-2009 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip decoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GUNZIP
+#endif
+
+/* Possible inflate modes between inflate() calls */
+typedef enum {
+ HEAD, /* i: waiting for magic header */
+ FLAGS, /* i: waiting for method and flags (gzip) */
+ TIME, /* i: waiting for modification time (gzip) */
+ OS, /* i: waiting for extra flags and operating system (gzip) */
+ EXLEN, /* i: waiting for extra length (gzip) */
+ EXTRA, /* i: waiting for extra bytes (gzip) */
+ NAME, /* i: waiting for end of file name (gzip) */
+ COMMENT, /* i: waiting for end of comment (gzip) */
+ HCRC, /* i: waiting for header crc (gzip) */
+ DICTID, /* i: waiting for dictionary check value */
+ DICT, /* waiting for inflateSetDictionary() call */
+ TYPE, /* i: waiting for type bits, including last-flag bit */
+ TYPEDO, /* i: same, but skip check to exit inflate on new block */
+ STORED, /* i: waiting for stored size (length and complement) */
+ COPY_, /* i/o: same as COPY below, but only first time in */
+ COPY, /* i/o: waiting for input or output to copy stored block */
+ TABLE, /* i: waiting for dynamic block table lengths */
+ LENLENS, /* i: waiting for code length code lengths */
+ CODELENS, /* i: waiting for length/lit and distance code lengths */
+ LEN_, /* i: same as LEN below, but only first time in */
+ LEN, /* i: waiting for length/lit/eob code */
+ LENEXT, /* i: waiting for length extra bits */
+ DIST, /* i: waiting for distance code */
+ DISTEXT, /* i: waiting for distance extra bits */
+ MATCH, /* o: waiting for output space to copy string */
+ LIT, /* o: waiting for output space to write literal */
+ CHECK, /* i: waiting for 32-bit check value */
+ LENGTH, /* i: waiting for 32-bit length (gzip) */
+ DONE, /* finished check, done -- remain here until reset */
+ BAD, /* got a data error -- remain here until reset */
+ MEM, /* got an inflate() memory error -- remain here until reset */
+ SYNC /* looking for synchronization bytes to restart inflate() */
+} inflate_mode;
+
+/*
+ State transitions between above modes -
+
+ (most modes can go to BAD or MEM on error -- not shown for clarity)
+
+ Process header:
+ HEAD -> (gzip) or (zlib) or (raw)
+ (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
+ HCRC -> TYPE
+ (zlib) -> DICTID or TYPE
+ DICTID -> DICT -> TYPE
+ (raw) -> TYPEDO
+ Read deflate blocks:
+ TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
+ STORED -> COPY_ -> COPY -> TYPE
+ TABLE -> LENLENS -> CODELENS -> LEN_
+ LEN_ -> LEN
+ Read deflate codes in fixed or dynamic block:
+ LEN -> LENEXT or LIT or TYPE
+ LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
+ LIT -> LEN
+ Process trailer:
+ CHECK -> LENGTH -> DONE
+ */
+
+/* state maintained between inflate() calls. Approximately 10K bytes. */
+struct inflate_state {
+ inflate_mode mode; /* current inflate mode */
+ int last; /* true if processing last block */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
+ int havedict; /* true if dictionary provided */
+ int flags; /* gzip header method and flags (0 if zlib) */
+ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
+ unsigned long check; /* protected copy of check value */
+ unsigned long total; /* protected copy of output count */
+ gz_headerp head; /* where to save gzip header information */
+ /* sliding window */
+ unsigned wbits; /* log base 2 of requested window size */
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if needed */
+ /* bit accumulator */
+ unsigned long hold; /* input bit accumulator */
+ unsigned bits; /* number of bits in "in" */
+ /* for string and stored block copying */
+ unsigned length; /* literal or length of data to copy */
+ unsigned offset; /* distance back to copy string from */
+ /* for table and code decoding */
+ unsigned extra; /* extra bits needed */
+ /* fixed and dynamic code tables */
+ code const FAR *lencode; /* starting table for length/literal codes */
+ code const FAR *distcode; /* starting table for distance codes */
+ unsigned lenbits; /* index bits for lencode */
+ unsigned distbits; /* index bits for distcode */
+ /* dynamic table building */
+ unsigned ncode; /* number of code length code lengths */
+ unsigned nlen; /* number of length code lengths */
+ unsigned ndist; /* number of distance code lengths */
+ unsigned have; /* number of code lengths in lens[] */
+ code FAR *next; /* next available space in codes[] */
+ unsigned short lens[320]; /* temporary storage for code lengths */
+ unsigned short work[288]; /* work area for code table building */
+ code codes[ENOUGH]; /* space for code tables */
+ int sane; /* if false, allow invalid distance too far */
+ int back; /* bits back of last unprocessed length/lit */
+ unsigned was; /* initial length of match */
+};
diff --git a/test/monniaux/glpk-4.65/src/zlib/inftrees.c b/test/monniaux/glpk-4.65/src/zlib/inftrees.c
new file mode 100644
index 00000000..11e9c52a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inftrees.c
@@ -0,0 +1,330 @@
+/* inftrees.c -- generate Huffman trees for efficient decoding
+ * Copyright (C) 1995-2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+
+#define MAXBITS 15
+
+const char inflate_copyright[] =
+ " inflate 1.2.5 Copyright 1995-2010 Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/*
+ Build a set of tables to decode the provided canonical Huffman code.
+ The code lengths are lens[0..codes-1]. The result starts at *table,
+ whose indices are 0..2^bits-1. work is a writable array of at least
+ lens shorts, which is used as a work area. type is the type of code
+ to be generated, CODES, LENS, or DISTS. On return, zero is success,
+ -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
+ on return points to the next available entry's address. bits is the
+ requested root table index bits, and on return it is the actual root
+ table index bits. It will differ if the request is greater than the
+ longest code or if it is less than the shortest code.
+ */
+int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
+codetype type;
+unsigned short FAR *lens;
+unsigned codes;
+code FAR * FAR *table;
+unsigned FAR *bits;
+unsigned short FAR *work;
+{
+ unsigned len; /* a code's length in bits */
+ unsigned sym; /* index of code symbols */
+ unsigned min, max; /* minimum and maximum code lengths */
+ unsigned root; /* number of index bits for root table */
+ unsigned curr; /* number of index bits for current table */
+ unsigned drop; /* code bits to drop for sub-table */
+ int left; /* number of prefix codes available */
+ unsigned used; /* code entries in table used */
+ unsigned huff; /* Huffman code */
+ unsigned incr; /* for incrementing code, index */
+ unsigned fill; /* index for replicating entries */
+ unsigned low; /* low bits for current root entry */
+ unsigned mask; /* mask for low root bits */
+ code here; /* table entry for duplication */
+ code FAR *next; /* next available space in table */
+ const unsigned short FAR *base; /* base value table to use */
+ const unsigned short FAR *extra; /* extra bits table to use */
+ int end; /* use base and extra for symbol > end */
+ unsigned short count[MAXBITS+1]; /* number of codes of each length */
+ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
+ static const unsigned short lbase[31] = { /* Length codes 257..285 base */
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
+ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
+ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
+ 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
+ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577, 0, 0};
+ static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
+ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
+ 28, 28, 29, 29, 64, 64};
+
+ /*
+ Process a set of code lengths to create a canonical Huffman code. The
+ code lengths are lens[0..codes-1]. Each length corresponds to the
+ symbols 0..codes-1. The Huffman code is generated by first sorting the
+ symbols by length from short to long, and retaining the symbol order
+ for codes with equal lengths. Then the code starts with all zero bits
+ for the first code of the shortest length, and the codes are integer
+ increments for the same length, and zeros are appended as the length
+ increases. For the deflate format, these bits are stored backwards
+ from their more natural integer increment ordering, and so when the
+ decoding tables are built in the large loop below, the integer codes
+ are incremented backwards.
+
+ This routine assumes, but does not check, that all of the entries in
+ lens[] are in the range 0..MAXBITS. The caller must assure this.
+ 1..MAXBITS is interpreted as that code length. zero means that that
+ symbol does not occur in this code.
+
+ The codes are sorted by computing a count of codes for each length,
+ creating from that a table of starting indices for each length in the
+ sorted table, and then entering the symbols in order in the sorted
+ table. The sorted table is work[], with that space being provided by
+ the caller.
+
+ The length counts are used for other purposes as well, i.e. finding
+ the minimum and maximum length codes, determining if there are any
+ codes at all, checking for a valid set of lengths, and looking ahead
+ at length counts to determine sub-table sizes when building the
+ decoding tables.
+ */
+
+ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
+ for (len = 0; len <= MAXBITS; len++)
+ count[len] = 0;
+ for (sym = 0; sym < codes; sym++)
+ count[lens[sym]]++;
+
+ /* bound code lengths, force root to be within code lengths */
+ root = *bits;
+ for (max = MAXBITS; max >= 1; max--)
+ if (count[max] != 0) break;
+ if (root > max) root = max;
+ if (max == 0) { /* no symbols to code at all */
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)1;
+ here.val = (unsigned short)0;
+ *(*table)++ = here; /* make a table to force an error */
+ *(*table)++ = here;
+ *bits = 1;
+ return 0; /* no symbols, but wait for decoding to report error */
+ }
+ for (min = 1; min < max; min++)
+ if (count[min] != 0) break;
+ if (root < min) root = min;
+
+ /* check for an over-subscribed or incomplete set of lengths */
+ left = 1;
+ for (len = 1; len <= MAXBITS; len++) {
+ left <<= 1;
+ left -= count[len];
+ if (left < 0) return -1; /* over-subscribed */
+ }
+ if (left > 0 && (type == CODES || max != 1))
+ return -1; /* incomplete set */
+
+ /* generate offsets into symbol table for each length for sorting */
+ offs[1] = 0;
+ for (len = 1; len < MAXBITS; len++)
+ offs[len + 1] = offs[len] + count[len];
+
+ /* sort symbols by length, by symbol order within each length */
+ for (sym = 0; sym < codes; sym++)
+ if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
+
+ /*
+ Create and fill in decoding tables. In this loop, the table being
+ filled is at next and has curr index bits. The code being used is huff
+ with length len. That code is converted to an index by dropping drop
+ bits off of the bottom. For codes where len is less than drop + curr,
+ those top drop + curr - len bits are incremented through all values to
+ fill the table with replicated entries.
+
+ root is the number of index bits for the root table. When len exceeds
+ root, sub-tables are created pointed to by the root entry with an index
+ of the low root bits of huff. This is saved in low to check for when a
+ new sub-table should be started. drop is zero when the root table is
+ being filled, and drop is root when sub-tables are being filled.
+
+ When a new sub-table is needed, it is necessary to look ahead in the
+ code lengths to determine what size sub-table is needed. The length
+ counts are used for this, and so count[] is decremented as codes are
+ entered in the tables.
+
+ used keeps track of how many table entries have been allocated from the
+ provided *table space. It is checked for LENS and DIST tables against
+ the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
+ the initial root table size constants. See the comments in inftrees.h
+ for more information.
+
+ sym increments through all symbols, and the loop terminates when
+ all codes of length max, i.e. all codes, have been processed. This
+ routine permits incomplete codes, so another loop after this one fills
+ in the rest of the decoding tables with invalid code markers.
+ */
+
+ /* set up for code type */
+ switch (type) {
+ case CODES:
+ base = extra = work; /* dummy value--not used */
+ end = 19;
+ break;
+ case LENS:
+ base = lbase;
+ base -= 257;
+ extra = lext;
+ extra -= 257;
+ end = 256;
+ break;
+ default: /* DISTS */
+ base = dbase;
+ extra = dext;
+ end = -1;
+ }
+
+ /* initialize state for loop */
+ huff = 0; /* starting code */
+ sym = 0; /* starting code symbol */
+ len = min; /* starting code length */
+ next = *table; /* current table to fill in */
+ curr = root; /* current table index bits */
+ drop = 0; /* current bits to drop from code for index */
+ low = (unsigned)(-1); /* trigger new sub-table when len > root */
+ used = 1U << root; /* use root table entries */
+ mask = used - 1; /* mask for comparing low */
+
+ /* check available table space */
+ if ((type == LENS && used >= ENOUGH_LENS) ||
+ (type == DISTS && used >= ENOUGH_DISTS))
+ return 1;
+
+ /* process all codes and make table entries */
+ for (;;) {
+ /* create table entry */
+ here.bits = (unsigned char)(len - drop);
+ if ((int)(work[sym]) < end) {
+ here.op = (unsigned char)0;
+ here.val = work[sym];
+ }
+ else if ((int)(work[sym]) > end) {
+ here.op = (unsigned char)(extra[work[sym]]);
+ here.val = base[work[sym]];
+ }
+ else {
+ here.op = (unsigned char)(32 + 64); /* end of block */
+ here.val = 0;
+ }
+
+ /* replicate for those indices with low len bits equal to huff */
+ incr = 1U << (len - drop);
+ fill = 1U << curr;
+ min = fill; /* save offset to next table */
+ do {
+ fill -= incr;
+ next[(huff >> drop) + fill] = here;
+ } while (fill != 0);
+
+ /* backwards increment the len-bit code huff */
+ incr = 1U << (len - 1);
+ while (huff & incr)
+ incr >>= 1;
+ if (incr != 0) {
+ huff &= incr - 1;
+ huff += incr;
+ }
+ else
+ huff = 0;
+
+ /* go to next symbol, update count, len */
+ sym++;
+ if (--(count[len]) == 0) {
+ if (len == max) break;
+ len = lens[work[sym]];
+ }
+
+ /* create new sub-table if needed */
+ if (len > root && (huff & mask) != low) {
+ /* if first time, transition to sub-tables */
+ if (drop == 0)
+ drop = root;
+
+ /* increment past last table */
+ next += min; /* here min is 1 << curr */
+
+ /* determine length of next table */
+ curr = len - drop;
+ left = (int)(1 << curr);
+ while (curr + drop < max) {
+ left -= count[curr + drop];
+ if (left <= 0) break;
+ curr++;
+ left <<= 1;
+ }
+
+ /* check for enough space */
+ used += 1U << curr;
+ if ((type == LENS && used >= ENOUGH_LENS) ||
+ (type == DISTS && used >= ENOUGH_DISTS))
+ return 1;
+
+ /* point entry in root table to sub-table */
+ low = huff & mask;
+ (*table)[low].op = (unsigned char)curr;
+ (*table)[low].bits = (unsigned char)root;
+ (*table)[low].val = (unsigned short)(next - *table);
+ }
+ }
+
+ /*
+ Fill in rest of table for incomplete codes. This loop is similar to the
+ loop above in incrementing huff for table indices. It is assumed that
+ len is equal to curr + drop, so there is no loop needed to increment
+ through high index bits. When the current sub-table is filled, the loop
+ drops back to the root table to fill in any remaining entries there.
+ */
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)(len - drop);
+ here.val = (unsigned short)0;
+ while (huff != 0) {
+ /* when done with sub-table, drop back to root table */
+ if (drop != 0 && (huff & mask) != low) {
+ drop = 0;
+ len = root;
+ next = *table;
+ here.bits = (unsigned char)len;
+ }
+
+ /* put invalid code marker in table */
+ next[huff >> drop] = here;
+
+ /* backwards increment the len-bit code huff */
+ incr = 1U << (len - 1);
+ while (huff & incr)
+ incr >>= 1;
+ if (incr != 0) {
+ huff &= incr - 1;
+ huff += incr;
+ }
+ else
+ huff = 0;
+ }
+
+ /* set return parameters */
+ *table += used;
+ *bits = root;
+ return 0;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/inftrees.h b/test/monniaux/glpk-4.65/src/zlib/inftrees.h
new file mode 100644
index 00000000..baa53a0b
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/inftrees.h
@@ -0,0 +1,62 @@
+/* inftrees.h -- header to use inftrees.c
+ * Copyright (C) 1995-2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* Structure for decoding tables. Each entry provides either the
+ information needed to do the operation requested by the code that
+ indexed that table entry, or it provides a pointer to another
+ table that indexes more bits of the code. op indicates whether
+ the entry is a pointer to another table, a literal, a length or
+ distance, an end-of-block, or an invalid code. For a table
+ pointer, the low four bits of op is the number of index bits of
+ that table. For a length or distance, the low four bits of op
+ is the number of extra bits to get after the code. bits is
+ the number of bits in this code or part of the code to drop off
+ of the bit buffer. val is the actual byte to output in the case
+ of a literal, the base length or distance, or the offset from
+ the current table to the next table. Each entry is four bytes. */
+typedef struct {
+ unsigned char op; /* operation, extra bits, table bits */
+ unsigned char bits; /* bits in this part of the code */
+ unsigned short val; /* offset in table or code value */
+} code;
+
+/* op values as set by inflate_table():
+ 00000000 - literal
+ 0000tttt - table link, tttt != 0 is the number of table index bits
+ 0001eeee - length or distance, eeee is the number of extra bits
+ 01100000 - end of block
+ 01000000 - invalid code
+ */
+
+/* Maximum size of the dynamic table. The maximum number of code structures is
+ 1444, which is the sum of 852 for literal/length codes and 592 for distance
+ codes. These values were found by exhaustive searches using the program
+ examples/enough.c found in the zlib distribtution. The arguments to that
+ program are the number of symbols, the initial root table size, and the
+ maximum bit length of a code. "enough 286 9 15" for literal/length codes
+ returns returns 852, and "enough 30 6 15" for distance codes returns 592.
+ The initial root table size (9 or 6) is found in the fifth argument of the
+ inflate_table() calls in inflate.c and infback.c. If the root table size is
+ changed, then these maximum sizes would be need to be recalculated and
+ updated. */
+#define ENOUGH_LENS 852
+#define ENOUGH_DISTS 592
+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
+
+/* Type of code to build for inflate_table() */
+typedef enum {
+ CODES,
+ LENS,
+ DISTS
+} codetype;
+
+int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
+ unsigned codes, code FAR * FAR *table,
+ unsigned FAR *bits, unsigned short FAR *work));
diff --git a/test/monniaux/glpk-4.65/src/zlib/trees.c b/test/monniaux/glpk-4.65/src/zlib/trees.c
new file mode 100644
index 00000000..56e9bb1c
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/trees.c
@@ -0,0 +1,1244 @@
+/* trees.c -- output deflated data using Huffman coding
+ * Copyright (C) 1995-2010 Jean-loup Gailly
+ * detect_data_type() function provided freely by Cosmin Truta, 2006
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process uses several Huffman trees. The more
+ * common source values are represented by shorter bit sequences.
+ *
+ * Each code tree is stored in a compressed form which is itself
+ * a Huffman encoding of the lengths of all the code strings (in
+ * ascending order by source values). The actual code strings are
+ * reconstructed from the lengths in the inflate process, as described
+ * in the deflate specification.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
+ * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
+ *
+ * Storer, James A.
+ * Data Compression: Methods and Theory, pp. 49-50.
+ * Computer Science Press, 1988. ISBN 0-7167-8156-5.
+ *
+ * Sedgewick, R.
+ * Algorithms, p290.
+ * Addison-Wesley, 1983. ISBN 0-201-06672-6.
+ */
+
+/* @(#) $Id$ */
+
+/* #define GEN_TREES_H */
+
+#include "deflate.h"
+
+#ifdef DEBUG
+# include <ctype.h>
+#endif
+
+/* ===========================================================================
+ * Constants
+ */
+
+#define MAX_BL_BITS 7
+/* Bit length codes must not exceed MAX_BL_BITS bits */
+
+#define END_BLOCK 256
+/* end of block literal code */
+
+#define REP_3_6 16
+/* repeat previous bit length 3-6 times (2 bits of repeat count) */
+
+#define REPZ_3_10 17
+/* repeat a zero length 3-10 times (3 bits of repeat count) */
+
+#define REPZ_11_138 18
+/* repeat a zero length 11-138 times (7 bits of repeat count) */
+
+local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
+ = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
+
+local const int extra_dbits[D_CODES] /* extra bits for each distance code */
+ = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
+
+local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
+ = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
+
+local const uch bl_order[BL_CODES]
+ = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
+/* The lengths of the bit length codes are sent in order of decreasing
+ * probability, to avoid transmitting the lengths for unused bit length codes.
+ */
+
+#define Buf_size (8 * 2*sizeof(char))
+/* Number of bits used within bi_buf. (bi_buf might be implemented on
+ * more than 16 bits on some systems.)
+ */
+
+/* ===========================================================================
+ * Local data. These are initialized only once.
+ */
+
+#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+/* non ANSI compilers may not accept trees.h */
+
+local ct_data static_ltree[L_CODES+2];
+/* The static literal tree. Since the bit lengths are imposed, there is no
+ * need for the L_CODES extra codes used during heap construction. However
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
+ * below).
+ */
+
+local ct_data static_dtree[D_CODES];
+/* The static distance tree. (Actually a trivial tree since all codes use
+ * 5 bits.)
+ */
+
+uch _dist_code[DIST_CODE_LEN];
+/* Distance codes. The first 256 values correspond to the distances
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
+ * the 15 bit distances.
+ */
+
+uch _length_code[MAX_MATCH-MIN_MATCH+1];
+/* length code for each normalized match length (0 == MIN_MATCH) */
+
+local int base_length[LENGTH_CODES];
+/* First normalized length for each code (0 = MIN_MATCH) */
+
+local int base_dist[D_CODES];
+/* First normalized distance for each code (0 = distance of 1) */
+
+#else
+# include "trees.h"
+#endif /* GEN_TREES_H */
+
+struct static_tree_desc_s {
+ const ct_data *static_tree; /* static tree or NULL */
+ const intf *extra_bits; /* extra bits for each code or NULL */
+ int extra_base; /* base index for extra_bits */
+ int elems; /* max number of elements in the tree */
+ int max_length; /* max bit length for the codes */
+};
+
+local static_tree_desc static_l_desc =
+{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
+
+local static_tree_desc static_d_desc =
+{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
+
+local static_tree_desc static_bl_desc =
+{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
+
+/* ===========================================================================
+ * Local (static) routines in this file.
+ */
+
+local void tr_static_init OF((void));
+local void init_block OF((deflate_state *s));
+local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
+local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
+local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
+local void build_tree OF((deflate_state *s, tree_desc *desc));
+local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local int build_bl_tree OF((deflate_state *s));
+local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
+ int blcodes));
+local void compress_block OF((deflate_state *s, ct_data *ltree,
+ ct_data *dtree));
+local int detect_data_type OF((deflate_state *s));
+local unsigned bi_reverse OF((unsigned value, int length));
+local void bi_windup OF((deflate_state *s));
+local void bi_flush OF((deflate_state *s));
+local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
+ int header));
+
+#ifdef GEN_TREES_H
+local void gen_trees_header OF((void));
+#endif
+
+#ifndef DEBUG
+# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
+ /* Send a code of the given tree. c and tree must not have side effects */
+
+#else /* DEBUG */
+# define send_code(s, c, tree) \
+ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
+ send_bits(s, tree[c].Code, tree[c].Len); }
+#endif
+
+/* ===========================================================================
+ * Output a short LSB first on the stream.
+ * IN assertion: there is enough room in pendingBuf.
+ */
+#define put_short(s, w) { \
+ put_byte(s, (uch)((w) & 0xff)); \
+ put_byte(s, (uch)((ush)(w) >> 8)); \
+}
+
+/* ===========================================================================
+ * Send a value on a given number of bits.
+ * IN assertion: length <= 16 and value fits in length bits.
+ */
+#ifdef DEBUG
+local void send_bits OF((deflate_state *s, int value, int length));
+
+local void send_bits(s, value, length)
+ deflate_state *s;
+ int value; /* value to send */
+ int length; /* number of bits */
+{
+ Tracevv((stderr," l %2d v %4x ", length, value));
+ Assert(length > 0 && length <= 15, "invalid length");
+ s->bits_sent += (ulg)length;
+
+ /* If not enough room in bi_buf, use (valid) bits from bi_buf and
+ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
+ * unused bits in value.
+ */
+ if (s->bi_valid > (int)Buf_size - length) {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ put_short(s, s->bi_buf);
+ s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
+ s->bi_valid += length - Buf_size;
+ } else {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ s->bi_valid += length;
+ }
+}
+#else /* !DEBUG */
+
+#define send_bits(s, value, length) \
+{ int len = length;\
+ if (s->bi_valid > (int)Buf_size - len) {\
+ int val = value;\
+ s->bi_buf |= (ush)val << s->bi_valid;\
+ put_short(s, s->bi_buf);\
+ s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
+ s->bi_valid += len - Buf_size;\
+ } else {\
+ s->bi_buf |= (ush)(value) << s->bi_valid;\
+ s->bi_valid += len;\
+ }\
+}
+#endif /* DEBUG */
+
+
+/* the arguments must not have side effects */
+
+/* ===========================================================================
+ * Initialize the various 'constant' tables.
+ */
+local void tr_static_init()
+{
+#if defined(GEN_TREES_H) || !defined(STDC)
+ static int static_init_done = 0;
+ int n; /* iterates over tree elements */
+ int bits; /* bit counter */
+ int length; /* length value */
+ int code; /* code value */
+ int dist; /* distance index */
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ if (static_init_done) return;
+
+ /* For some embedded targets, global variables are not initialized: */
+#ifdef NO_INIT_GLOBAL_POINTERS
+ static_l_desc.static_tree = static_ltree;
+ static_l_desc.extra_bits = extra_lbits;
+ static_d_desc.static_tree = static_dtree;
+ static_d_desc.extra_bits = extra_dbits;
+ static_bl_desc.extra_bits = extra_blbits;
+#endif
+
+ /* Initialize the mapping length (0..255) -> length code (0..28) */
+ length = 0;
+ for (code = 0; code < LENGTH_CODES-1; code++) {
+ base_length[code] = length;
+ for (n = 0; n < (1<<extra_lbits[code]); n++) {
+ _length_code[length++] = (uch)code;
+ }
+ }
+ Assert (length == 256, "tr_static_init: length != 256");
+ /* Note that the length 255 (match length 258) can be represented
+ * in two different ways: code 284 + 5 bits or code 285, so we
+ * overwrite length_code[255] to use the best encoding:
+ */
+ _length_code[length-1] = (uch)code;
+
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
+ dist = 0;
+ for (code = 0 ; code < 16; code++) {
+ base_dist[code] = dist;
+ for (n = 0; n < (1<<extra_dbits[code]); n++) {
+ _dist_code[dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: dist != 256");
+ dist >>= 7; /* from now on, all distances are divided by 128 */
+ for ( ; code < D_CODES; code++) {
+ base_dist[code] = dist << 7;
+ for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
+ _dist_code[256 + dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: 256+dist != 512");
+
+ /* Construct the codes of the static literal tree */
+ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
+ n = 0;
+ while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
+ while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
+ while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
+ while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
+ /* Codes 286 and 287 do not exist, but we must include them in the
+ * tree construction to get a canonical Huffman tree (longest code
+ * all ones)
+ */
+ gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
+
+ /* The static distance tree is trivial: */
+ for (n = 0; n < D_CODES; n++) {
+ static_dtree[n].Len = 5;
+ static_dtree[n].Code = bi_reverse((unsigned)n, 5);
+ }
+ static_init_done = 1;
+
+# ifdef GEN_TREES_H
+ gen_trees_header();
+# endif
+#endif /* defined(GEN_TREES_H) || !defined(STDC) */
+}
+
+/* ===========================================================================
+ * Genererate the file trees.h describing the static trees.
+ */
+#ifdef GEN_TREES_H
+# ifndef DEBUG
+# include <stdio.h>
+# endif
+
+# define SEPARATOR(i, last, width) \
+ ((i) == (last)? "\n};\n\n" : \
+ ((i) % (width) == (width)-1 ? ",\n" : ", "))
+
+void gen_trees_header()
+{
+ FILE *header = fopen("trees.h", "w");
+ int i;
+
+ Assert (header != NULL, "Can't open trees.h");
+ fprintf(header,
+ "/* header created automatically with -DGEN_TREES_H */\n\n");
+
+ fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
+ for (i = 0; i < L_CODES+2; i++) {
+ fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
+ static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
+ }
+
+ fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
+ static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
+ }
+
+ fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
+ for (i = 0; i < DIST_CODE_LEN; i++) {
+ fprintf(header, "%2u%s", _dist_code[i],
+ SEPARATOR(i, DIST_CODE_LEN-1, 20));
+ }
+
+ fprintf(header,
+ "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
+ for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
+ fprintf(header, "%2u%s", _length_code[i],
+ SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
+ }
+
+ fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
+ for (i = 0; i < LENGTH_CODES; i++) {
+ fprintf(header, "%1u%s", base_length[i],
+ SEPARATOR(i, LENGTH_CODES-1, 20));
+ }
+
+ fprintf(header, "local const int base_dist[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "%5u%s", base_dist[i],
+ SEPARATOR(i, D_CODES-1, 10));
+ }
+
+ fclose(header);
+}
+#endif /* GEN_TREES_H */
+
+/* ===========================================================================
+ * Initialize the tree data structures for a new zlib stream.
+ */
+void ZLIB_INTERNAL _tr_init(s)
+ deflate_state *s;
+{
+ tr_static_init();
+
+ s->l_desc.dyn_tree = s->dyn_ltree;
+ s->l_desc.stat_desc = &static_l_desc;
+
+ s->d_desc.dyn_tree = s->dyn_dtree;
+ s->d_desc.stat_desc = &static_d_desc;
+
+ s->bl_desc.dyn_tree = s->bl_tree;
+ s->bl_desc.stat_desc = &static_bl_desc;
+
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+ s->last_eob_len = 8; /* enough lookahead for inflate */
+#ifdef DEBUG
+ s->compressed_len = 0L;
+ s->bits_sent = 0L;
+#endif
+
+ /* Initialize the first block of the first file: */
+ init_block(s);
+}
+
+/* ===========================================================================
+ * Initialize a new block.
+ */
+local void init_block(s)
+ deflate_state *s;
+{
+ int n; /* iterates over tree elements */
+
+ /* Initialize the trees. */
+ for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
+ for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
+ for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
+
+ s->dyn_ltree[END_BLOCK].Freq = 1;
+ s->opt_len = s->static_len = 0L;
+ s->last_lit = s->matches = 0;
+}
+
+#define SMALLEST 1
+/* Index within the heap array of least frequent node in the Huffman tree */
+
+
+/* ===========================================================================
+ * Remove the smallest element from the heap and recreate the heap with
+ * one less element. Updates heap and heap_len.
+ */
+#define pqremove(s, tree, top) \
+{\
+ top = s->heap[SMALLEST]; \
+ s->heap[SMALLEST] = s->heap[s->heap_len--]; \
+ pqdownheap(s, tree, SMALLEST); \
+}
+
+/* ===========================================================================
+ * Compares to subtrees, using the tree depth as tie breaker when
+ * the subtrees have equal frequency. This minimizes the worst case length.
+ */
+#define smaller(tree, n, m, depth) \
+ (tree[n].Freq < tree[m].Freq || \
+ (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
+
+/* ===========================================================================
+ * Restore the heap property by moving down the tree starting at node k,
+ * exchanging a node with the smallest of its two sons if necessary, stopping
+ * when the heap property is re-established (each father smaller than its
+ * two sons).
+ */
+local void pqdownheap(s, tree, k)
+ deflate_state *s;
+ ct_data *tree; /* the tree to restore */
+ int k; /* node to move down */
+{
+ int v = s->heap[k];
+ int j = k << 1; /* left son of k */
+ while (j <= s->heap_len) {
+ /* Set j to the smallest of the two sons: */
+ if (j < s->heap_len &&
+ smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
+ j++;
+ }
+ /* Exit if v is smaller than both sons */
+ if (smaller(tree, v, s->heap[j], s->depth)) break;
+
+ /* Exchange v with the smallest son */
+ s->heap[k] = s->heap[j]; k = j;
+
+ /* And continue down the tree, setting j to the left son of k */
+ j <<= 1;
+ }
+ s->heap[k] = v;
+}
+
+/* ===========================================================================
+ * Compute the optimal bit lengths for a tree and update the total bit length
+ * for the current block.
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
+ * above are the tree nodes sorted by increasing frequency.
+ * OUT assertions: the field len is set to the optimal bit length, the
+ * array bl_count contains the frequencies for each bit length.
+ * The length opt_len is updated; static_len is also updated if stree is
+ * not null.
+ */
+local void gen_bitlen(s, desc)
+ deflate_state *s;
+ tree_desc *desc; /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ int max_code = desc->max_code;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ const intf *extra = desc->stat_desc->extra_bits;
+ int base = desc->stat_desc->extra_base;
+ int max_length = desc->stat_desc->max_length;
+ int h; /* heap index */
+ int n, m; /* iterate over the tree elements */
+ int bits; /* bit length */
+ int xbits; /* extra bits */
+ ush f; /* frequency */
+ int overflow = 0; /* number of elements with bit length too large */
+
+ for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
+
+ /* In a first pass, compute the optimal bit lengths (which may
+ * overflow in the case of the bit length tree).
+ */
+ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
+
+ for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
+ n = s->heap[h];
+ bits = tree[tree[n].Dad].Len + 1;
+ if (bits > max_length) bits = max_length, overflow++;
+ tree[n].Len = (ush)bits;
+ /* We overwrite tree[n].Dad which is no longer needed */
+
+ if (n > max_code) continue; /* not a leaf node */
+
+ s->bl_count[bits]++;
+ xbits = 0;
+ if (n >= base) xbits = extra[n-base];
+ f = tree[n].Freq;
+ s->opt_len += (ulg)f * (bits + xbits);
+ if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
+ }
+ if (overflow == 0) return;
+
+ Trace((stderr,"\nbit length overflow\n"));
+ /* This happens for example on obj2 and pic of the Calgary corpus */
+
+ /* Find the first bit length which could increase: */
+ do {
+ bits = max_length-1;
+ while (s->bl_count[bits] == 0) bits--;
+ s->bl_count[bits]--; /* move one leaf down the tree */
+ s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
+ s->bl_count[max_length]--;
+ /* The brother of the overflow item also moves one step up,
+ * but this does not affect bl_count[max_length]
+ */
+ overflow -= 2;
+ } while (overflow > 0);
+
+ /* Now recompute all bit lengths, scanning in increasing frequency.
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
+ * lengths instead of fixing only the wrong ones. This idea is taken
+ * from 'ar' written by Haruhiko Okumura.)
+ */
+ for (bits = max_length; bits != 0; bits--) {
+ n = s->bl_count[bits];
+ while (n != 0) {
+ m = s->heap[--h];
+ if (m > max_code) continue;
+ if ((unsigned) tree[m].Len != (unsigned) bits) {
+ Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
+ s->opt_len += ((long)bits - (long)tree[m].Len)
+ *(long)tree[m].Freq;
+ tree[m].Len = (ush)bits;
+ }
+ n--;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Generate the codes for a given tree and bit counts (which need not be
+ * optimal).
+ * IN assertion: the array bl_count contains the bit length statistics for
+ * the given tree and the field len is set for all tree elements.
+ * OUT assertion: the field code is set for all tree elements of non
+ * zero code length.
+ */
+local void gen_codes (tree, max_code, bl_count)
+ ct_data *tree; /* the tree to decorate */
+ int max_code; /* largest code with non zero frequency */
+ ushf *bl_count; /* number of codes at each bit length */
+{
+ ush next_code[MAX_BITS+1]; /* next code value for each bit length */
+ ush code = 0; /* running code value */
+ int bits; /* bit index */
+ int n; /* code index */
+
+ /* The distribution counts are first used to generate the code values
+ * without bit reversal.
+ */
+ for (bits = 1; bits <= MAX_BITS; bits++) {
+ next_code[bits] = code = (code + bl_count[bits-1]) << 1;
+ }
+ /* Check that the bit counts in bl_count are consistent. The last code
+ * must be all ones.
+ */
+ Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
+ "inconsistent bit counts");
+ Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
+
+ for (n = 0; n <= max_code; n++) {
+ int len = tree[n].Len;
+ if (len == 0) continue;
+ /* Now reverse the bits */
+ tree[n].Code = bi_reverse(next_code[len]++, len);
+
+ Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
+ n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
+ }
+}
+
+/* ===========================================================================
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
+ * Update the total bit length for the current block.
+ * IN assertion: the field freq is set for all tree elements.
+ * OUT assertions: the fields len and code are set to the optimal bit length
+ * and corresponding code. The length opt_len is updated; static_len is
+ * also updated if stree is not null. The field max_code is set.
+ */
+local void build_tree(s, desc)
+ deflate_state *s;
+ tree_desc *desc; /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ int elems = desc->stat_desc->elems;
+ int n, m; /* iterate over heap elements */
+ int max_code = -1; /* largest code with non zero frequency */
+ int node; /* new node being created */
+
+ /* Construct the initial heap, with least frequent element in
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
+ * heap[0] is not used.
+ */
+ s->heap_len = 0, s->heap_max = HEAP_SIZE;
+
+ for (n = 0; n < elems; n++) {
+ if (tree[n].Freq != 0) {
+ s->heap[++(s->heap_len)] = max_code = n;
+ s->depth[n] = 0;
+ } else {
+ tree[n].Len = 0;
+ }
+ }
+
+ /* The pkzip format requires that at least one distance code exists,
+ * and that at least one bit should be sent even if there is only one
+ * possible code. So to avoid special checks later on we force at least
+ * two codes of non zero frequency.
+ */
+ while (s->heap_len < 2) {
+ node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
+ tree[node].Freq = 1;
+ s->depth[node] = 0;
+ s->opt_len--; if (stree) s->static_len -= stree[node].Len;
+ /* node is 0 or 1 so it does not have extra bits */
+ }
+ desc->max_code = max_code;
+
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
+ * establish sub-heaps of increasing lengths:
+ */
+ for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
+
+ /* Construct the Huffman tree by repeatedly combining the least two
+ * frequent nodes.
+ */
+ node = elems; /* next internal node of the tree */
+ do {
+ pqremove(s, tree, n); /* n = node of least frequency */
+ m = s->heap[SMALLEST]; /* m = node of next least frequency */
+
+ s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
+ s->heap[--(s->heap_max)] = m;
+
+ /* Create a new node father of n and m */
+ tree[node].Freq = tree[n].Freq + tree[m].Freq;
+ s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
+ s->depth[n] : s->depth[m]) + 1);
+ tree[n].Dad = tree[m].Dad = (ush)node;
+#ifdef DUMP_BL_TREE
+ if (tree == s->bl_tree) {
+ fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
+ node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
+ }
+#endif
+ /* and insert the new node in the heap */
+ s->heap[SMALLEST] = node++;
+ pqdownheap(s, tree, SMALLEST);
+
+ } while (s->heap_len >= 2);
+
+ s->heap[--(s->heap_max)] = s->heap[SMALLEST];
+
+ /* At this point, the fields freq and dad are set. We can now
+ * generate the bit lengths.
+ */
+ gen_bitlen(s, (tree_desc *)desc);
+
+ /* The field len is now set, we can generate the bit codes */
+ gen_codes ((ct_data *)tree, max_code, s->bl_count);
+}
+
+/* ===========================================================================
+ * Scan a literal or distance tree to determine the frequencies of the codes
+ * in the bit length tree.
+ */
+local void scan_tree (s, tree, max_code)
+ deflate_state *s;
+ ct_data *tree; /* the tree to be scanned */
+ int max_code; /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ if (nextlen == 0) max_count = 138, min_count = 3;
+ tree[max_code+1].Len = (ush)0xffff; /* guard */
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ s->bl_tree[curlen].Freq += count;
+ } else if (curlen != 0) {
+ if (curlen != prevlen) s->bl_tree[curlen].Freq++;
+ s->bl_tree[REP_3_6].Freq++;
+ } else if (count <= 10) {
+ s->bl_tree[REPZ_3_10].Freq++;
+ } else {
+ s->bl_tree[REPZ_11_138].Freq++;
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Send a literal or distance tree in compressed form, using the codes in
+ * bl_tree.
+ */
+local void send_tree (s, tree, max_code)
+ deflate_state *s;
+ ct_data *tree; /* the tree to be scanned */
+ int max_code; /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ /* tree[max_code+1].Len = -1; */ /* guard already set */
+ if (nextlen == 0) max_count = 138, min_count = 3;
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
+
+ } else if (curlen != 0) {
+ if (curlen != prevlen) {
+ send_code(s, curlen, s->bl_tree); count--;
+ }
+ Assert(count >= 3 && count <= 6, " 3_6?");
+ send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
+
+ } else if (count <= 10) {
+ send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
+
+ } else {
+ send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Construct the Huffman tree for the bit lengths and return the index in
+ * bl_order of the last bit length code to send.
+ */
+local int build_bl_tree(s)
+ deflate_state *s;
+{
+ int max_blindex; /* index of last bit length code of non zero freq */
+
+ /* Determine the bit length frequencies for literal and distance trees */
+ scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
+ scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
+
+ /* Build the bit length tree: */
+ build_tree(s, (tree_desc *)(&(s->bl_desc)));
+ /* opt_len now includes the length of the tree representations, except
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
+ */
+
+ /* Determine the number of bit length codes to send. The pkzip format
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
+ * 3 but the actual value used is 4.)
+ */
+ for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
+ if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
+ }
+ /* Update opt_len to include the bit length tree and counts */
+ s->opt_len += 3*(max_blindex+1) + 5+5+4;
+ Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
+ s->opt_len, s->static_len));
+
+ return max_blindex;
+}
+
+/* ===========================================================================
+ * Send the header for a block using dynamic Huffman trees: the counts, the
+ * lengths of the bit length codes, the literal tree and the distance tree.
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
+ */
+local void send_all_trees(s, lcodes, dcodes, blcodes)
+ deflate_state *s;
+ int lcodes, dcodes, blcodes; /* number of codes for each tree */
+{
+ int rank; /* index in bl_order */
+
+ Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
+ Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
+ "too many codes");
+ Tracev((stderr, "\nbl counts: "));
+ send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
+ send_bits(s, dcodes-1, 5);
+ send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
+ for (rank = 0; rank < blcodes; rank++) {
+ Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
+ send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
+ }
+ Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
+ Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
+ Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
+}
+
+/* ===========================================================================
+ * Send a stored block
+ */
+void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
+ deflate_state *s;
+ charf *buf; /* input block */
+ ulg stored_len; /* length of input block */
+ int last; /* one if this is the last block for a file */
+{
+ send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
+#ifdef DEBUG
+ s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
+ s->compressed_len += (stored_len + 4) << 3;
+#endif
+ copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
+}
+
+/* ===========================================================================
+ * Send one empty static block to give enough lookahead for inflate.
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
+ * The current inflate code requires 9 bits of lookahead. If the
+ * last two codes for the previous block (real code plus EOB) were coded
+ * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
+ * the last real code. In this case we send two empty static blocks instead
+ * of one. (There are no problems if the previous block is stored or fixed.)
+ * To simplify the code, we assume the worst case of last real code encoded
+ * on one bit only.
+ */
+void ZLIB_INTERNAL _tr_align(s)
+ deflate_state *s;
+{
+ send_bits(s, STATIC_TREES<<1, 3);
+ send_code(s, END_BLOCK, static_ltree);
+#ifdef DEBUG
+ s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
+#endif
+ bi_flush(s);
+ /* Of the 10 bits for the empty block, we have already sent
+ * (10 - bi_valid) bits. The lookahead for the last real code (before
+ * the EOB of the previous block) was thus at least one plus the length
+ * of the EOB plus what we have just sent of the empty static block.
+ */
+ if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
+ send_bits(s, STATIC_TREES<<1, 3);
+ send_code(s, END_BLOCK, static_ltree);
+#ifdef DEBUG
+ s->compressed_len += 10L;
+#endif
+ bi_flush(s);
+ }
+ s->last_eob_len = 7;
+}
+
+/* ===========================================================================
+ * Determine the best encoding for the current block: dynamic trees, static
+ * trees or store, and output the encoded block to the zip file.
+ */
+void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
+ deflate_state *s;
+ charf *buf; /* input block, or NULL if too old */
+ ulg stored_len; /* length of input block */
+ int last; /* one if this is the last block for a file */
+{
+ ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
+ int max_blindex = 0; /* index of last bit length code of non zero freq */
+
+ /* Build the Huffman trees unless a stored block is forced */
+ if (s->level > 0) {
+
+ /* Check if the file is binary or text */
+ if (s->strm->data_type == Z_UNKNOWN)
+ s->strm->data_type = detect_data_type(s);
+
+ /* Construct the literal and distance trees */
+ build_tree(s, (tree_desc *)(&(s->l_desc)));
+ Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+
+ build_tree(s, (tree_desc *)(&(s->d_desc)));
+ Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+ /* At this point, opt_len and static_len are the total bit lengths of
+ * the compressed block data, excluding the tree representations.
+ */
+
+ /* Build the bit length tree for the above two trees, and get the index
+ * in bl_order of the last bit length code to send.
+ */
+ max_blindex = build_bl_tree(s);
+
+ /* Determine the best encoding. Compute the block lengths in bytes. */
+ opt_lenb = (s->opt_len+3+7)>>3;
+ static_lenb = (s->static_len+3+7)>>3;
+
+ Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
+ opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
+ s->last_lit));
+
+ if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
+
+ } else {
+ Assert(buf != (char*)0, "lost buf");
+ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
+ }
+
+#ifdef FORCE_STORED
+ if (buf != (char*)0) { /* force stored block */
+#else
+ if (stored_len+4 <= opt_lenb && buf != (char*)0) {
+ /* 4: two words for the lengths */
+#endif
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
+ * Otherwise we can't have processed more than WSIZE input bytes since
+ * the last block flush, because compression would have been
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
+ * transform a block into a stored block.
+ */
+ _tr_stored_block(s, buf, stored_len, last);
+
+#ifdef FORCE_STATIC
+ } else if (static_lenb >= 0) { /* force static trees */
+#else
+ } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
+#endif
+ send_bits(s, (STATIC_TREES<<1)+last, 3);
+ compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
+#ifdef DEBUG
+ s->compressed_len += 3 + s->static_len;
+#endif
+ } else {
+ send_bits(s, (DYN_TREES<<1)+last, 3);
+ send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
+ max_blindex+1);
+ compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
+#ifdef DEBUG
+ s->compressed_len += 3 + s->opt_len;
+#endif
+ }
+ Assert (s->compressed_len == s->bits_sent, "bad compressed size");
+ /* The above check is made mod 2^32, for files larger than 512 MB
+ * and uLong implemented on 32 bits.
+ */
+ init_block(s);
+
+ if (last) {
+ bi_windup(s);
+#ifdef DEBUG
+ s->compressed_len += 7; /* align on byte boundary */
+#endif
+ }
+ Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
+ s->compressed_len-7*last));
+}
+
+/* ===========================================================================
+ * Save the match info and tally the frequency counts. Return true if
+ * the current block must be flushed.
+ */
+int ZLIB_INTERNAL _tr_tally (s, dist, lc)
+ deflate_state *s;
+ unsigned dist; /* distance of matched string */
+ unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
+{
+ s->d_buf[s->last_lit] = (ush)dist;
+ s->l_buf[s->last_lit++] = (uch)lc;
+ if (dist == 0) {
+ /* lc is the unmatched char */
+ s->dyn_ltree[lc].Freq++;
+ } else {
+ s->matches++;
+ /* Here, lc is the match length - MIN_MATCH */
+ dist--; /* dist = match distance - 1 */
+ Assert((ush)dist < (ush)MAX_DIST(s) &&
+ (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
+ (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
+
+ s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
+ s->dyn_dtree[d_code(dist)].Freq++;
+ }
+
+#ifdef TRUNCATE_BLOCK
+ /* Try to guess if it is profitable to stop the current block here */
+ if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
+ /* Compute an upper bound for the compressed length */
+ ulg out_length = (ulg)s->last_lit*8L;
+ ulg in_length = (ulg)((long)s->strstart - s->block_start);
+ int dcode;
+ for (dcode = 0; dcode < D_CODES; dcode++) {
+ out_length += (ulg)s->dyn_dtree[dcode].Freq *
+ (5L+extra_dbits[dcode]);
+ }
+ out_length >>= 3;
+ Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
+ s->last_lit, in_length, out_length,
+ 100L - out_length*100L/in_length));
+ if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
+ }
+#endif
+ return (s->last_lit == s->lit_bufsize-1);
+ /* We avoid equality with lit_bufsize because of wraparound at 64K
+ * on 16 bit machines and because stored blocks are restricted to
+ * 64K-1 bytes.
+ */
+}
+
+/* ===========================================================================
+ * Send the block data compressed using the given Huffman trees
+ */
+local void compress_block(s, ltree, dtree)
+ deflate_state *s;
+ ct_data *ltree; /* literal tree */
+ ct_data *dtree; /* distance tree */
+{
+ unsigned dist; /* distance of matched string */
+ int lc; /* match length or unmatched char (if dist == 0) */
+ unsigned lx = 0; /* running index in l_buf */
+ unsigned code; /* the code to send */
+ int extra; /* number of extra bits to send */
+
+ if (s->last_lit != 0) do {
+ dist = s->d_buf[lx];
+ lc = s->l_buf[lx++];
+ if (dist == 0) {
+ send_code(s, lc, ltree); /* send a literal byte */
+ Tracecv(isgraph(lc), (stderr," '%c' ", lc));
+ } else {
+ /* Here, lc is the match length - MIN_MATCH */
+ code = _length_code[lc];
+ send_code(s, code+LITERALS+1, ltree); /* send the length code */
+ extra = extra_lbits[code];
+ if (extra != 0) {
+ lc -= base_length[code];
+ send_bits(s, lc, extra); /* send the extra length bits */
+ }
+ dist--; /* dist is now the match distance - 1 */
+ code = d_code(dist);
+ Assert (code < D_CODES, "bad d_code");
+
+ send_code(s, code, dtree); /* send the distance code */
+ extra = extra_dbits[code];
+ if (extra != 0) {
+ dist -= base_dist[code];
+ send_bits(s, dist, extra); /* send the extra distance bits */
+ }
+ } /* literal or match pair ? */
+
+ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
+ Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
+ "pendingBuf overflow");
+
+ } while (lx < s->last_lit);
+
+ send_code(s, END_BLOCK, ltree);
+ s->last_eob_len = ltree[END_BLOCK].Len;
+}
+
+/* ===========================================================================
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
+ * - TEXT if the two conditions below are satisfied:
+ * a) There are no non-portable control characters belonging to the
+ * "black list" (0..6, 14..25, 28..31).
+ * b) There is at least one printable character belonging to the
+ * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
+ * - BINARY otherwise.
+ * - The following partially-portable control characters form a
+ * "gray list" that is ignored in this detection algorithm:
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
+ * IN assertion: the fields Freq of dyn_ltree are set.
+ */
+local int detect_data_type(s)
+ deflate_state *s;
+{
+ /* black_mask is the bit mask of black-listed bytes
+ * set bits 0..6, 14..25, and 28..31
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
+ */
+ unsigned long black_mask = 0xf3ffc07fUL;
+ int n;
+
+ /* Check for non-textual ("black-listed") bytes. */
+ for (n = 0; n <= 31; n++, black_mask >>= 1)
+ if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
+ return Z_BINARY;
+
+ /* Check for textual ("white-listed") bytes. */
+ if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
+ || s->dyn_ltree[13].Freq != 0)
+ return Z_TEXT;
+ for (n = 32; n < LITERALS; n++)
+ if (s->dyn_ltree[n].Freq != 0)
+ return Z_TEXT;
+
+ /* There are no "black-listed" or "white-listed" bytes:
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
+ */
+ return Z_BINARY;
+}
+
+/* ===========================================================================
+ * Reverse the first len bits of a code, using straightforward code (a faster
+ * method would use a table)
+ * IN assertion: 1 <= len <= 15
+ */
+local unsigned bi_reverse(code, len)
+ unsigned code; /* the value to invert */
+ int len; /* its bit length */
+{
+ register unsigned res = 0;
+ do {
+ res |= code & 1;
+ code >>= 1, res <<= 1;
+ } while (--len > 0);
+ return res >> 1;
+}
+
+/* ===========================================================================
+ * Flush the bit buffer, keeping at most 7 bits in it.
+ */
+local void bi_flush(s)
+ deflate_state *s;
+{
+ if (s->bi_valid == 16) {
+ put_short(s, s->bi_buf);
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+ } else if (s->bi_valid >= 8) {
+ put_byte(s, (Byte)s->bi_buf);
+ s->bi_buf >>= 8;
+ s->bi_valid -= 8;
+ }
+}
+
+/* ===========================================================================
+ * Flush the bit buffer and align the output on a byte boundary
+ */
+local void bi_windup(s)
+ deflate_state *s;
+{
+ if (s->bi_valid > 8) {
+ put_short(s, s->bi_buf);
+ } else if (s->bi_valid > 0) {
+ put_byte(s, (Byte)s->bi_buf);
+ }
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+#ifdef DEBUG
+ s->bits_sent = (s->bits_sent+7) & ~7;
+#endif
+}
+
+/* ===========================================================================
+ * Copy a stored block, storing first the length and its
+ * one's complement if requested.
+ */
+local void copy_block(s, buf, len, header)
+ deflate_state *s;
+ charf *buf; /* the input data */
+ unsigned len; /* its length */
+ int header; /* true if block header must be written */
+{
+ bi_windup(s); /* align on byte boundary */
+ s->last_eob_len = 8; /* enough lookahead for inflate */
+
+ if (header) {
+ put_short(s, (ush)len);
+ put_short(s, (ush)~len);
+#ifdef DEBUG
+ s->bits_sent += 2*16;
+#endif
+ }
+#ifdef DEBUG
+ s->bits_sent += (ulg)len<<3;
+#endif
+ while (len--) {
+ put_byte(s, *buf++);
+ }
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/trees.h b/test/monniaux/glpk-4.65/src/zlib/trees.h
new file mode 100644
index 00000000..d35639d8
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/trees.h
@@ -0,0 +1,128 @@
+/* header created automatically with -DGEN_TREES_H */
+
+local const ct_data static_ltree[L_CODES+2] = {
+{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
+{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
+{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
+{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
+{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
+{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
+{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
+{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
+{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
+{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
+{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
+{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
+{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
+{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
+{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
+{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
+{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
+{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
+{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
+{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
+{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
+{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
+{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
+{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
+{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
+{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
+{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
+{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
+{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
+{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
+{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
+{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
+{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
+{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
+{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
+{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
+{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
+{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
+{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
+{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
+{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
+{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
+{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
+{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
+{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
+{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
+{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
+{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
+{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
+{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
+{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
+{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
+{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
+{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
+{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
+{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
+{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
+{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
+};
+
+local const ct_data static_dtree[D_CODES] = {
+{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
+{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
+{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
+{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
+{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
+{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
+};
+
+const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
+ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
+ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
+10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
+12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
+18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
+};
+
+const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
+13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
+17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
+19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
+22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
+};
+
+local const int base_length[LENGTH_CODES] = {
+0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
+64, 80, 96, 112, 128, 160, 192, 224, 0
+};
+
+local const int base_dist[D_CODES] = {
+ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
+ 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
+ 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
+};
+
diff --git a/test/monniaux/glpk-4.65/src/zlib/uncompr.c b/test/monniaux/glpk-4.65/src/zlib/uncompr.c
new file mode 100644
index 00000000..ad98be3a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/uncompr.c
@@ -0,0 +1,59 @@
+/* uncompr.c -- decompress a memory buffer
+ * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Decompresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total
+ size of the destination buffer, which must be large enough to hold the
+ entire uncompressed data. (The size of the uncompressed data must have
+ been saved previously by the compressor and transmitted to the decompressor
+ by some mechanism outside the scope of this compression library.)
+ Upon exit, destLen is the actual size of the compressed buffer.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer, or Z_DATA_ERROR if the input data was corrupted.
+*/
+int ZEXPORT uncompress (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+{
+ z_stream stream;
+ int err;
+
+ stream.next_in = (Bytef*)source;
+ stream.avail_in = (uInt)sourceLen;
+ /* Check for source > 64K on 16-bit machine: */
+ if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+
+ stream.next_out = dest;
+ stream.avail_out = (uInt)*destLen;
+ if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+
+ err = inflateInit(&stream);
+ if (err != Z_OK) return err;
+
+ err = inflate(&stream, Z_FINISH);
+ if (err != Z_STREAM_END) {
+ inflateEnd(&stream);
+ if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
+ return Z_DATA_ERROR;
+ return err;
+ }
+ *destLen = stream.total_out;
+
+ err = inflateEnd(&stream);
+ return err;
+}
diff --git a/test/monniaux/glpk-4.65/src/zlib/zconf.h b/test/monniaux/glpk-4.65/src/zlib/zconf.h
new file mode 100644
index 00000000..af6a4f0f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zconf.h
@@ -0,0 +1,168 @@
+/* zconf.h (configuration of the zlib compression library) */
+
+/* Modified by Andrew Makhorin <mao@gnu.org>, April 2011 */
+
+/* Copyright (C) 1995-2010 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in
+ * zlib.h */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h. */
+
+#ifndef ZCONF_H
+#define ZCONF_H
+
+/* (file adler32.c) */
+#define adler32 _glp_zlib_adler32
+#define adler32_combine _glp_zlib_adler32_combine
+#define adler32_combine64 _glp_zlib_adler32_combine64
+
+/* (file compress.c) */
+#define compress2 _glp_zlib_compress2
+#define compress _glp_zlib_compress
+#define compressBound _glp_zlib_compressBound
+
+/* (file crc32.c) */
+#define get_crc_table _glp_zlib_get_crc_table
+#define crc32 _glp_zlib_crc32
+#define crc32_combine _glp_zlib_crc32_combine
+#define crc32_combine64 _glp_zlib_crc32_combine64
+
+/* (file deflate.c) */
+#define deflateInit_ _glp_zlib_deflateInit_
+#define deflateInit2_ _glp_zlib_deflateInit2_
+#define deflateSetDictionary _glp_zlib_deflateSetDictionary
+#define deflateReset _glp_zlib_deflateReset
+#define deflateSetHeader _glp_zlib_deflateSetHeader
+#define deflatePrime _glp_zlib_deflatePrime
+#define deflateParams _glp_zlib_deflateParams
+#define deflateTune _glp_zlib_deflateTune
+#define deflateBound _glp_zlib_deflateBound
+#define deflate _glp_zlib_deflate
+#define deflateEnd _glp_zlib_deflateEnd
+#define deflateCopy _glp_zlib_deflateCopy
+#define deflate_copyright _glp_zlib_deflate_copyright
+
+/* (file gzclose.c) */
+#define gzclose _glp_zlib_gzclose
+
+/* (file gzlib.c) */
+#define gzopen _glp_zlib_gzopen
+#define gzopen64 _glp_zlib_gzopen64
+#define gzdopen _glp_zlib_gzdopen
+#define gzbuffer _glp_zlib_gzbuffer
+#define gzrewind _glp_zlib_gzrewind
+#define gzseek64 _glp_zlib_gzseek64
+#define gzseek _glp_zlib_gzseek
+#define gztell64 _glp_zlib_gztell64
+#define gztell _glp_zlib_gztell
+#define gzoffset64 _glp_zlib_gzoffset64
+#define gzoffset _glp_zlib_gzoffset
+#define gzeof _glp_zlib_gzeof
+#define gzerror _glp_zlib_gzerror
+#define gzclearerr _glp_zlib_gzclearerr
+#define gz_error _glp_zlib_gz_error
+
+/* (file gzread.c) */
+#define gzread _glp_zlib_gzread
+#define gzgetc _glp_zlib_gzgetc
+#define gzungetc _glp_zlib_gzungetc
+#define gzgets _glp_zlib_gzgets
+#define gzdirect _glp_zlib_gzdirect
+#define gzclose_r _glp_zlib_gzclose_r
+
+/* (file gzwrite.c) */
+#define gzwrite _glp_zlib_gzwrite
+#define gzputc _glp_zlib_gzputc
+#define gzputs _glp_zlib_gzputs
+#define gzprintf _glp_zlib_gzprintf
+#define gzflush _glp_zlib_gzflush
+#define gzsetparams _glp_zlib_gzsetparams
+#define gzclose_w _glp_zlib_gzclose_w
+
+/* (file infback.c) */
+#define inflateBackInit_ _glp_zlib_inflateBackInit_
+#define inflateBack _glp_zlib_inflateBack
+#define inflateBackEnd _glp_zlib_inflateBackEnd
+
+/* (file inffast.c) */
+#define inflate_fast _glp_zlib_inflate_fast
+
+/* (file inflate.c) */
+#define inflateReset _glp_zlib_inflateReset
+#define inflateReset2 _glp_zlib_inflateReset2
+#define inflateInit2_ _glp_zlib_inflateInit2_
+#define inflateInit_ _glp_zlib_inflateInit_
+#define inflatePrime _glp_zlib_inflatePrime
+#define inflate _glp_zlib_inflate
+#define inflateEnd _glp_zlib_inflateEnd
+#define inflateSetDictionary _glp_zlib_inflateSetDictionary
+#define inflateGetHeader _glp_zlib_inflateGetHeader
+#define inflateSync _glp_zlib_inflateSync
+#define inflateSyncPoint _glp_zlib_inflateSyncPoint
+#define inflateCopy _glp_zlib_inflateCopy
+#define inflateUndermine _glp_zlib_inflateUndermine
+#define inflateMark _glp_zlib_inflateMark
+
+/* (file inftrees.c) */
+#define inflate_table _glp_zlib_inflate_table
+#define inflate_copyright _glp_zlib_inflate_copyright
+
+/* (file trees.c) */
+#define _tr_init _glp_zlib_tr_init
+#define _tr_stored_block _glp_zlib_tr_stored_block
+#define _tr_align _glp_zlib_tr_align
+#define _tr_flush_block _glp_zlib_tr_flush_block
+#define _tr_tally _glp_zlib_tr_tally
+#define _dist_code _glp_zlib_dist_code
+#define _length_code _glp_zlib_length_code
+
+/* (file uncompr.c) */
+#define uncompress _glp_zlib_uncompress
+
+/* (file zutil.c) */
+#define zlibVersion _glp_zlib_zlibVersion
+#define zlibCompileFlags _glp_zlib_zlibCompileFlags
+#define zError _glp_zlib_zError
+#define zcalloc _glp_zlib_zcalloc
+#define zcfree _glp_zlib_zcfree
+#define z_errmsg _glp_zlib_z_errmsg
+
+#define STDC 1
+
+#define MAX_MEM_LEVEL 9
+
+#define MAX_WBITS 15
+
+#define OF(args) args
+
+#define ZEXTERN extern
+#define ZEXPORT
+#define ZEXPORTVA
+
+#define FAR
+
+typedef unsigned char Byte;
+typedef unsigned int uInt;
+typedef unsigned long uLong;
+
+typedef Byte Bytef;
+typedef char charf;
+typedef int intf;
+typedef uInt uIntf;
+typedef uLong uLongf;
+
+typedef void const *voidpc;
+typedef void *voidpf;
+typedef void *voidp;
+
+#define z_off_t long
+
+#define z_off64_t z_off_t
+
+#define NO_vsnprintf 1
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/zlib/zio.c b/test/monniaux/glpk-4.65/src/zlib/zio.c
new file mode 100644
index 00000000..a55b258a
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zio.c
@@ -0,0 +1,92 @@
+/* zio.c (simulation of non-standard low-level i/o functions) */
+
+/* Written by Andrew Makhorin <mao@gnu.org>, April 2011
+ * For conditions of distribution and use, see copyright notice in
+ * zlib.h */
+
+/* (reserved for copyright notice) */
+
+#include <assert.h>
+#include <stdio.h>
+#include "zio.h"
+
+static FILE *file[FOPEN_MAX];
+static int initialized = 0;
+
+static void initialize(void)
+{ int fd;
+ assert(!initialized);
+ file[0] = stdin;
+ file[1] = stdout;
+ file[2] = stderr;
+ for (fd = 3; fd < FOPEN_MAX; fd++)
+ file[fd] = NULL;
+ initialized = 1;
+ return;
+}
+
+int open(const char *path, int oflag, ...)
+{ FILE *fp;
+ int fd;
+ if (!initialized) initialize();
+ /* see file gzlib.c, function gz_open */
+ if (oflag == O_RDONLY)
+ fp = fopen(path, "rb");
+ else if (oflag == (O_WRONLY | O_CREAT | O_TRUNC))
+ fp = fopen(path, "wb");
+ else if (oflag == (O_WRONLY | O_CREAT | O_APPEND))
+ fp = fopen(path, "ab");
+ else
+ assert(oflag != oflag);
+ if (fp == NULL)
+ return -1;
+ for (fd = 0; fd < FOPEN_MAX; fd++)
+ if (file[fd] == NULL) break;
+ assert(fd < FOPEN_MAX);
+ file[fd] = fp;
+ return fd;
+}
+
+long read(int fd, void *buf, unsigned long nbyte)
+{ unsigned long count;
+ if (!initialized) initialize();
+ assert(0 <= fd && fd < FOPEN_MAX);
+ assert(file[fd] != NULL);
+ count = fread(buf, 1, nbyte, file[fd]);
+ if (ferror(file[fd]))
+ return -1;
+ return count;
+}
+
+long write(int fd, const void *buf, unsigned long nbyte)
+{ unsigned long count;
+ if (!initialized) initialize();
+ assert(0 <= fd && fd < FOPEN_MAX);
+ assert(file[fd] != NULL);
+ count = fwrite(buf, 1, nbyte, file[fd]);
+ if (count != nbyte)
+ return -1;
+ if (fflush(file[fd]) != 0)
+ return -1;
+ return count;
+}
+
+long lseek(int fd, long offset, int whence)
+{ if (!initialized) initialize();
+ assert(0 <= fd && fd < FOPEN_MAX);
+ assert(file[fd] != NULL);
+ if (fseek(file[fd], offset, whence) != 0)
+ return -1;
+ return ftell(file[fd]);
+}
+
+int close(int fd)
+{ if (!initialized) initialize();
+ assert(0 <= fd && fd < FOPEN_MAX);
+ assert(file[fd] != NULL);
+ fclose(file[fd]);
+ file[fd] = NULL;
+ return 0;
+}
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/zlib/zio.h b/test/monniaux/glpk-4.65/src/zlib/zio.h
new file mode 100644
index 00000000..1626c4ae
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zio.h
@@ -0,0 +1,37 @@
+/* zio.h (simulation of non-standard low-level i/o functions) */
+
+/* Written by Andrew Makhorin <mao@gnu.org>, April 2011
+ * For conditions of distribution and use, see copyright notice in
+ * zlib.h */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h. */
+
+#ifndef ZIO_H
+#define ZIO_H
+
+#define O_RDONLY 0x00
+#define O_WRONLY 0x01
+#define O_CREAT 0x10
+#define O_TRUNC 0x20
+#define O_APPEND 0x30
+
+#define open _glp_zlib_open
+int open(const char *path, int oflag, ...);
+
+#define read _glp_zlib_read
+long read(int fd, void *buf, unsigned long nbyte);
+
+#define write _glp_zlib_write
+long write(int fd, const void *buf, unsigned long nbyte);
+
+#define lseek _glp_zlib_lseek
+long lseek(int fd, long offset, int whence);
+
+#define close _glp_zlib_close
+int close(int fd);
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/glpk-4.65/src/zlib/zlib.h b/test/monniaux/glpk-4.65/src/zlib/zlib.h
new file mode 100644
index 00000000..bfbba83e
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zlib.h
@@ -0,0 +1,1613 @@
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+ version 1.2.5, April 19th, 2010
+
+ Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Jean-loup Gailly Mark Adler
+ jloup@gzip.org madler@alumni.caltech.edu
+
+
+ The data format used by the zlib library is described by RFCs (Request for
+ Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
+ (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
+*/
+
+#ifndef ZLIB_H
+#define ZLIB_H
+
+#include "zconf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ZLIB_VERSION "1.2.5"
+#define ZLIB_VERNUM 0x1250
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 5
+#define ZLIB_VER_SUBREVISION 0
+
+/*
+ The 'zlib' compression library provides in-memory compression and
+ decompression functions, including integrity checks of the uncompressed data.
+ This version of the library supports only one compression method (deflation)
+ but other algorithms will be added later and will have the same stream
+ interface.
+
+ Compression can be done in a single step if the buffers are large enough,
+ or can be done by repeated calls of the compression function. In the latter
+ case, the application must provide more input and/or consume the output
+ (providing more output space) before each call.
+
+ The compressed data format used by default by the in-memory functions is
+ the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
+ around a deflate stream, which is itself documented in RFC 1951.
+
+ The library also supports reading and writing files in gzip (.gz) format
+ with an interface similar to that of stdio using the functions that start
+ with "gz". The gzip format is different from the zlib format. gzip is a
+ gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
+
+ This library can optionally read and write gzip streams in memory as well.
+
+ The zlib format was designed to be compact and fast for use in memory
+ and on communications channels. The gzip format was designed for single-
+ file compression on file systems, has a larger header than zlib to maintain
+ directory information, and uses a different, slower check method than zlib.
+
+ The library does not install any signal handler. The decoder checks
+ the consistency of the compressed data, so the library should never crash
+ even in case of corrupted input.
+*/
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void (*free_func) OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+ Bytef *next_in; /* next input byte */
+ uInt avail_in; /* number of bytes available at next_in */
+ uLong total_in; /* total nb of input bytes read so far */
+
+ Bytef *next_out; /* next output byte should be put there */
+ uInt avail_out; /* remaining free space at next_out */
+ uLong total_out; /* total nb of bytes output so far */
+
+ char *msg; /* last error message, NULL if no error */
+ struct internal_state FAR *state; /* not visible by applications */
+
+ alloc_func zalloc; /* used to allocate the internal state */
+ free_func zfree; /* used to free the internal state */
+ voidpf opaque; /* private data object passed to zalloc and zfree */
+
+ int data_type; /* best guess about the data type: binary or text */
+ uLong adler; /* adler32 value of the uncompressed data */
+ uLong reserved; /* reserved for future use */
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+/*
+ gzip header information passed to and from zlib routines. See RFC 1952
+ for more details on the meanings of these fields.
+*/
+typedef struct gz_header_s {
+ int text; /* true if compressed data believed to be text */
+ uLong time; /* modification time */
+ int xflags; /* extra flags (not used when writing a gzip file) */
+ int os; /* operating system */
+ Bytef *extra; /* pointer to extra field or Z_NULL if none */
+ uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
+ uInt extra_max; /* space at extra (only when reading header) */
+ Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
+ uInt name_max; /* space at name (only when reading header) */
+ Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
+ uInt comm_max; /* space at comment (only when reading header) */
+ int hcrc; /* true if there was or will be a header crc */
+ int done; /* true when done reading gzip header (not used
+ when writing a gzip file) */
+} gz_header;
+
+typedef gz_header FAR *gz_headerp;
+
+/*
+ The application must update next_in and avail_in when avail_in has dropped
+ to zero. It must update next_out and avail_out when avail_out has dropped
+ to zero. The application must initialize zalloc, zfree and opaque before
+ calling the init function. All other fields are set by the compression
+ library and must not be updated by the application.
+
+ The opaque value provided by the application will be passed as the first
+ parameter for calls of zalloc and zfree. This can be useful for custom
+ memory management. The compression library attaches no meaning to the
+ opaque value.
+
+ zalloc must return Z_NULL if there is not enough memory for the object.
+ If zlib is used in a multi-threaded application, zalloc and zfree must be
+ thread safe.
+
+ On 16-bit systems, the functions zalloc and zfree must be able to allocate
+ exactly 65536 bytes, but will not be required to allocate more than this if
+ the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers
+ returned by zalloc for objects of exactly 65536 bytes *must* have their
+ offset normalized to zero. The default allocation function provided by this
+ library ensures this (see zutil.c). To reduce memory requirements and avoid
+ any allocation of 64K objects, at the expense of compression ratio, compile
+ the library with -DMAX_WBITS=14 (see zconf.h).
+
+ The fields total_in and total_out can be used for statistics or progress
+ reports. After compression, total_in holds the total size of the
+ uncompressed data and may be saved for use in the decompressor (particularly
+ if the decompressor wants to decompress everything in a single step).
+*/
+
+ /* constants */
+
+#define Z_NO_FLUSH 0
+#define Z_PARTIAL_FLUSH 1
+#define Z_SYNC_FLUSH 2
+#define Z_FULL_FLUSH 3
+#define Z_FINISH 4
+#define Z_BLOCK 5
+#define Z_TREES 6
+/* Allowed flush values; see deflate() and inflate() below for details */
+
+#define Z_OK 0
+#define Z_STREAM_END 1
+#define Z_NEED_DICT 2
+#define Z_ERRNO (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR (-3)
+#define Z_MEM_ERROR (-4)
+#define Z_BUF_ERROR (-5)
+#define Z_VERSION_ERROR (-6)
+/* Return codes for the compression/decompression functions. Negative values
+ * are errors, positive values are used for special but normal events.
+ */
+
+#define Z_NO_COMPRESSION 0
+#define Z_BEST_SPEED 1
+#define Z_BEST_COMPRESSION 9
+#define Z_DEFAULT_COMPRESSION (-1)
+/* compression levels */
+
+#define Z_FILTERED 1
+#define Z_HUFFMAN_ONLY 2
+#define Z_RLE 3
+#define Z_FIXED 4
+#define Z_DEFAULT_STRATEGY 0
+/* compression strategy; see deflateInit2() below for details */
+
+#define Z_BINARY 0
+#define Z_TEXT 1
+#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
+#define Z_UNKNOWN 2
+/* Possible values of the data_type field (though see inflate()) */
+
+#define Z_DEFLATED 8
+/* The deflate compression method (the only one supported in this version) */
+
+#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
+
+#define zlib_version zlibVersion()
+/* for compatibility with versions < 1.0.2 */
+
+
+ /* basic functions */
+
+ZEXTERN const char * ZEXPORT zlibVersion OF((void));
+/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
+ If the first character differs, the library code actually used is not
+ compatible with the zlib.h header file used by the application. This check
+ is automatically made by deflateInit and inflateInit.
+ */
+
+/*
+ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
+
+ Initializes the internal stream state for compression. The fields
+ zalloc, zfree and opaque must be initialized before by the caller. If
+ zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
+ allocation functions.
+
+ The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
+ 1 gives best speed, 9 gives best compression, 0 gives no compression at all
+ (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION
+ requests a default compromise between speed and compression (currently
+ equivalent to level 6).
+
+ deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if level is not a valid compression level, or
+ Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
+ with the version assumed by the caller (ZLIB_VERSION). msg is set to null
+ if there is no error message. deflateInit does not perform any compression:
+ this will be done by deflate().
+*/
+
+
+ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
+/*
+ deflate compresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. deflate performs one or both of the
+ following actions:
+
+ - Compress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), next_in and avail_in are updated and
+ processing will resume at this point for the next call of deflate().
+
+ - Provide more output starting at next_out and update next_out and avail_out
+ accordingly. This action is forced if the parameter flush is non zero.
+ Forcing flush frequently degrades the compression ratio, so this parameter
+ should be set only when necessary (in interactive applications). Some
+ output may be provided even if flush is not set.
+
+ Before the call of deflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating avail_in or avail_out accordingly; avail_out should
+ never be zero before the call. The application can consume the compressed
+ output when it wants, for example when the output buffer is full (avail_out
+ == 0), or after each call of deflate(). If deflate returns Z_OK and with
+ zero avail_out, it must be called again after making room in the output
+ buffer because there might be more output pending.
+
+ Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
+ decide how much data to accumulate before producing output, in order to
+ maximize compression.
+
+ If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
+ flushed to the output buffer and the output is aligned on a byte boundary, so
+ that the decompressor can get all input data available so far. (In
+ particular avail_in is zero after the call if enough output space has been
+ provided before the call.) Flushing may degrade compression for some
+ compression algorithms and so it should be used only when necessary. This
+ completes the current deflate block and follows it with an empty stored block
+ that is three bits plus filler bits to the next byte, followed by four bytes
+ (00 00 ff ff).
+
+ If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
+ output buffer, but the output is not aligned to a byte boundary. All of the
+ input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
+ This completes the current deflate block and follows it with an empty fixed
+ codes block that is 10 bits long. This assures that enough bytes are output
+ in order for the decompressor to finish the block before the empty fixed code
+ block.
+
+ If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
+ for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
+ seven bits of the current block are held to be written as the next byte after
+ the next deflate block is completed. In this case, the decompressor may not
+ be provided enough bits at this point in order to complete decompression of
+ the data provided so far to the compressor. It may need to wait for the next
+ block to be emitted. This is for advanced applications that need to control
+ the emission of deflate blocks.
+
+ If flush is set to Z_FULL_FLUSH, all output is flushed as with
+ Z_SYNC_FLUSH, and the compression state is reset so that decompression can
+ restart from this point if previous compressed data has been damaged or if
+ random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+ compression.
+
+ If deflate returns with avail_out == 0, this function must be called again
+ with the same value of the flush parameter and more output space (updated
+ avail_out), until the flush is complete (deflate returns with non-zero
+ avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+ avail_out is greater than six to avoid repeated flush markers due to
+ avail_out == 0 on return.
+
+ If the parameter flush is set to Z_FINISH, pending input is processed,
+ pending output is flushed and deflate returns with Z_STREAM_END if there was
+ enough output space; if deflate returns with Z_OK, this function must be
+ called again with Z_FINISH and more output space (updated avail_out) but no
+ more input data, until it returns with Z_STREAM_END or an error. After
+ deflate has returned Z_STREAM_END, the only possible operations on the stream
+ are deflateReset or deflateEnd.
+
+ Z_FINISH can be used immediately after deflateInit if all the compression
+ is to be done in a single step. In this case, avail_out must be at least the
+ value returned by deflateBound (see below). If deflate does not return
+ Z_STREAM_END, then it must be called again as described above.
+
+ deflate() sets strm->adler to the adler32 checksum of all input read
+ so far (that is, total_in bytes).
+
+ deflate() may update strm->data_type if it can make a good guess about
+ the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
+ binary. This field is only for information purposes and does not affect the
+ compression algorithm in any manner.
+
+ deflate() returns Z_OK if some progress has been made (more input
+ processed or more output produced), Z_STREAM_END if all input has been
+ consumed and all output has been produced (only when flush is set to
+ Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
+ if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
+ (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
+ fatal, and deflate() can be called again with more input and more output
+ space to continue compressing.
+*/
+
+
+ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
+ stream state was inconsistent, Z_DATA_ERROR if the stream was freed
+ prematurely (some input or output was discarded). In the error case, msg
+ may be set but then points to a static string (which must not be
+ deallocated).
+*/
+
+
+/*
+ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
+
+ Initializes the internal stream state for decompression. The fields
+ next_in, avail_in, zalloc, zfree and opaque must be initialized before by
+ the caller. If next_in is not Z_NULL and avail_in is large enough (the
+ exact value depends on the compression method), inflateInit determines the
+ compression method from the zlib header and allocates all data structures
+ accordingly; otherwise the allocation will be deferred to the first call of
+ inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
+ use default allocation functions.
+
+ inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit does not perform any decompression
+ apart from possibly reading the zlib header if present: actual decompression
+ will be done by inflate(). (So next_in and avail_in may be modified, but
+ next_out and avail_out are unused and unchanged.) The current implementation
+ of inflateInit() does not process any header information -- that is deferred
+ until inflate() is called.
+*/
+
+
+ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
+/*
+ inflate decompresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. inflate performs one or both of the
+ following actions:
+
+ - Decompress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), next_in is updated and processing will
+ resume at this point for the next call of inflate().
+
+ - Provide more output starting at next_out and update next_out and avail_out
+ accordingly. inflate() provides as much output as possible, until there is
+ no more input data or no more space in the output buffer (see below about
+ the flush parameter).
+
+ Before the call of inflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating the next_* and avail_* values accordingly. The
+ application can consume the uncompressed output when it wants, for example
+ when the output buffer is full (avail_out == 0), or after each call of
+ inflate(). If inflate returns Z_OK and with zero avail_out, it must be
+ called again after making room in the output buffer because there might be
+ more output pending.
+
+ The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
+ Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much
+ output as possible to the output buffer. Z_BLOCK requests that inflate()
+ stop if and when it gets to the next deflate block boundary. When decoding
+ the zlib or gzip format, this will cause inflate() to return immediately
+ after the header and before the first block. When doing a raw inflate,
+ inflate() will go ahead and process the first block, and will return when it
+ gets to the end of that block, or when it runs out of data.
+
+ The Z_BLOCK option assists in appending to or combining deflate streams.
+ Also to assist in this, on return inflate() will set strm->data_type to the
+ number of unused bits in the last byte taken from strm->next_in, plus 64 if
+ inflate() is currently decoding the last block in the deflate stream, plus
+ 128 if inflate() returned immediately after decoding an end-of-block code or
+ decoding the complete header up to just before the first byte of the deflate
+ stream. The end-of-block will not be indicated until all of the uncompressed
+ data from that block has been written to strm->next_out. The number of
+ unused bits may in general be greater than seven, except when bit 7 of
+ data_type is set, in which case the number of unused bits will be less than
+ eight. data_type is set as noted here every time inflate() returns for all
+ flush options, and so can be used to determine the amount of currently
+ consumed input in bits.
+
+ The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
+ end of each deflate block header is reached, before any actual data in that
+ block is decoded. This allows the caller to determine the length of the
+ deflate block header for later use in random access within a deflate block.
+ 256 is added to the value of strm->data_type when inflate() returns
+ immediately after reaching the end of the deflate block header.
+
+ inflate() should normally be called until it returns Z_STREAM_END or an
+ error. However if all decompression is to be performed in a single step (a
+ single call of inflate), the parameter flush should be set to Z_FINISH. In
+ this case all pending input is processed and all pending output is flushed;
+ avail_out must be large enough to hold all the uncompressed data. (The size
+ of the uncompressed data may have been saved by the compressor for this
+ purpose.) The next operation on this stream must be inflateEnd to deallocate
+ the decompression state. The use of Z_FINISH is never required, but can be
+ used to inform inflate that a faster approach may be used for the single
+ inflate() call.
+
+ In this implementation, inflate() always flushes as much output as
+ possible to the output buffer, and always uses the faster approach on the
+ first call. So the only effect of the flush parameter in this implementation
+ is on the return value of inflate(), as noted below, or when it returns early
+ because Z_BLOCK or Z_TREES is used.
+
+ If a preset dictionary is needed after this call (see inflateSetDictionary
+ below), inflate sets strm->adler to the adler32 checksum of the dictionary
+ chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
+ strm->adler to the adler32 checksum of all output produced so far (that is,
+ total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
+ below. At the end of the stream, inflate() checks that its computed adler32
+ checksum is equal to that saved by the compressor and returns Z_STREAM_END
+ only if the checksum is correct.
+
+ inflate() can decompress and check either zlib-wrapped or gzip-wrapped
+ deflate data. The header type is detected automatically, if requested when
+ initializing with inflateInit2(). Any information contained in the gzip
+ header is not retained, so applications that need that information should
+ instead use raw inflate, see inflateInit2() below, or inflateBack() and
+ perform their own processing of the gzip header and trailer.
+
+ inflate() returns Z_OK if some progress has been made (more input processed
+ or more output produced), Z_STREAM_END if the end of the compressed data has
+ been reached and all uncompressed output has been produced, Z_NEED_DICT if a
+ preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
+ corrupted (input stream not conforming to the zlib format or incorrect check
+ value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
+ next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
+ Z_BUF_ERROR if no progress is possible or if there was not enough room in the
+ output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+ inflate() can be called again with more input and more output space to
+ continue decompressing. If Z_DATA_ERROR is returned, the application may
+ then call inflateSync() to look for a good compression block if a partial
+ recovery of the data is desired.
+*/
+
+
+ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
+ was inconsistent. In the error case, msg may be set but then points to a
+ static string (which must not be deallocated).
+*/
+
+
+ /* Advanced functions */
+
+/*
+ The following functions are needed only in some special applications.
+*/
+
+/*
+ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
+ int level,
+ int method,
+ int windowBits,
+ int memLevel,
+ int strategy));
+
+ This is another version of deflateInit with more compression options. The
+ fields next_in, zalloc, zfree and opaque must be initialized before by the
+ caller.
+
+ The method parameter is the compression method. It must be Z_DEFLATED in
+ this version of the library.
+
+ The windowBits parameter is the base two logarithm of the window size
+ (the size of the history buffer). It should be in the range 8..15 for this
+ version of the library. Larger values of this parameter result in better
+ compression at the expense of memory usage. The default value is 15 if
+ deflateInit is used instead.
+
+ windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
+ determines the window size. deflate() will then generate raw deflate data
+ with no zlib header or trailer, and will not compute an adler32 check value.
+
+ windowBits can also be greater than 15 for optional gzip encoding. Add
+ 16 to windowBits to write a simple gzip header and trailer around the
+ compressed data instead of a zlib wrapper. The gzip header will have no
+ file name, no extra data, no comment, no modification time (set to zero), no
+ header crc, and the operating system will be set to 255 (unknown). If a
+ gzip stream is being written, strm->adler is a crc32 instead of an adler32.
+
+ The memLevel parameter specifies how much memory should be allocated
+ for the internal compression state. memLevel=1 uses minimum memory but is
+ slow and reduces compression ratio; memLevel=9 uses maximum memory for
+ optimal speed. The default value is 8. See zconf.h for total memory usage
+ as a function of windowBits and memLevel.
+
+ The strategy parameter is used to tune the compression algorithm. Use the
+ value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
+ filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
+ string match), or Z_RLE to limit match distances to one (run-length
+ encoding). Filtered data consists mostly of small values with a somewhat
+ random distribution. In this case, the compression algorithm is tuned to
+ compress them better. The effect of Z_FILTERED is to force more Huffman
+ coding and less string matching; it is somewhat intermediate between
+ Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as
+ fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The
+ strategy parameter only affects the compression ratio but not the
+ correctness of the compressed output even if it is not set appropriately.
+ Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
+ decoder for special applications.
+
+ deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
+ method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
+ incompatible with the version assumed by the caller (ZLIB_VERSION). msg is
+ set to null if there is no error message. deflateInit2 does not perform any
+ compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the compression dictionary from the given byte sequence
+ without producing any compressed output. This function must be called
+ immediately after deflateInit, deflateInit2 or deflateReset, before any call
+ of deflate. The compressor and decompressor must use exactly the same
+ dictionary (see inflateSetDictionary).
+
+ The dictionary should consist of strings (byte sequences) that are likely
+ to be encountered later in the data to be compressed, with the most commonly
+ used strings preferably put towards the end of the dictionary. Using a
+ dictionary is most useful when the data to be compressed is short and can be
+ predicted with good accuracy; the data can then be compressed better than
+ with the default empty dictionary.
+
+ Depending on the size of the compression data structures selected by
+ deflateInit or deflateInit2, a part of the dictionary may in effect be
+ discarded, for example if the dictionary is larger than the window size
+ provided in deflateInit or deflateInit2. Thus the strings most likely to be
+ useful should be put at the end of the dictionary, not at the front. In
+ addition, the current implementation of deflate will use at most the window
+ size minus 262 bytes of the provided dictionary.
+
+ Upon return of this function, strm->adler is set to the adler32 value
+ of the dictionary; the decompressor may later use this value to determine
+ which dictionary has been used by the compressor. (The adler32 value
+ applies to the whole dictionary even if only a subset of the dictionary is
+ actually used by the compressor.) If a raw deflate was requested, then the
+ adler32 value is not computed and strm->adler is not set.
+
+ deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent (for example if deflate has already been called for this stream
+ or if the compression method is bsort). deflateSetDictionary does not
+ perform any compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when several compression strategies will be
+ tried, for example when there are several ways of pre-processing the input
+ data with a filter. The streams that will be discarded should then be freed
+ by calling deflateEnd. Note that deflateCopy duplicates the internal
+ compression state which can be quite large, so this strategy is slow and can
+ consume lots of memory.
+
+ deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to deflateEnd followed by deflateInit,
+ but does not free and reallocate all the internal compression state. The
+ stream will keep the same compression level and any other attributes that
+ may have been set by deflateInit2.
+
+ deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
+ int level,
+ int strategy));
+/*
+ Dynamically update the compression level and compression strategy. The
+ interpretation of level and strategy is as in deflateInit2. This can be
+ used to switch between compression and straight copy of the input data, or
+ to switch to a different kind of input data requiring a different strategy.
+ If the compression level is changed, the input available so far is
+ compressed with the old level (and may be flushed); the new level will take
+ effect only at the next call of deflate().
+
+ Before the call of deflateParams, the stream state must be set as for
+ a call of deflate(), since the currently available input may have to be
+ compressed and flushed. In particular, strm->avail_out must be non-zero.
+
+ deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
+ stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if
+ strm->avail_out was zero.
+*/
+
+ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
+ int good_length,
+ int max_lazy,
+ int nice_length,
+ int max_chain));
+/*
+ Fine tune deflate's internal compression parameters. This should only be
+ used by someone who understands the algorithm used by zlib's deflate for
+ searching for the best matching string, and even then only by the most
+ fanatic optimizer trying to squeeze out the last compressed bit for their
+ specific input data. Read the deflate.c source code for the meaning of the
+ max_lazy, good_length, nice_length, and max_chain parameters.
+
+ deflateTune() can be called after deflateInit() or deflateInit2(), and
+ returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
+ */
+
+ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
+ uLong sourceLen));
+/*
+ deflateBound() returns an upper bound on the compressed size after
+ deflation of sourceLen bytes. It must be called after deflateInit() or
+ deflateInit2(), and after deflateSetHeader(), if used. This would be used
+ to allocate an output buffer for deflation in a single pass, and so would be
+ called before deflate().
+*/
+
+ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ deflatePrime() inserts bits in the deflate output stream. The intent
+ is that this function is used to start off the deflate output with the bits
+ leftover from a previous deflate stream when appending to it. As such, this
+ function can only be used for raw deflate, and must be used before the first
+ deflate() call after a deflateInit2() or deflateReset(). bits must be less
+ than or equal to 16, and that many of the least significant bits of value
+ will be inserted in the output.
+
+ deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ deflateSetHeader() provides gzip header information for when a gzip
+ stream is requested by deflateInit2(). deflateSetHeader() may be called
+ after deflateInit2() or deflateReset() and before the first call of
+ deflate(). The text, time, os, extra field, name, and comment information
+ in the provided gz_header structure are written to the gzip header (xflag is
+ ignored -- the extra flags are set according to the compression level). The
+ caller must assure that, if not Z_NULL, name and comment are terminated with
+ a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
+ available there. If hcrc is true, a gzip header crc is included. Note that
+ the current versions of the command-line version of gzip (up through version
+ 1.3.x) do not support header crc's, and will report that it is a "multi-part
+ gzip file" and give up.
+
+ If deflateSetHeader is not used, the default gzip header has text false,
+ the time set to zero, and os set to 255, with no extra, name, or comment
+ fields. The gzip header is returned to the default state by deflateReset().
+
+ deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
+ int windowBits));
+
+ This is another version of inflateInit with an extra parameter. The
+ fields next_in, avail_in, zalloc, zfree and opaque must be initialized
+ before by the caller.
+
+ The windowBits parameter is the base two logarithm of the maximum window
+ size (the size of the history buffer). It should be in the range 8..15 for
+ this version of the library. The default value is 15 if inflateInit is used
+ instead. windowBits must be greater than or equal to the windowBits value
+ provided to deflateInit2() while compressing, or it must be equal to 15 if
+ deflateInit2() was not used. If a compressed stream with a larger window
+ size is given as input, inflate() will return with the error code
+ Z_DATA_ERROR instead of trying to allocate a larger window.
+
+ windowBits can also be zero to request that inflate use the window size in
+ the zlib header of the compressed stream.
+
+ windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
+ determines the window size. inflate() will then process raw deflate data,
+ not looking for a zlib or gzip header, not generating a check value, and not
+ looking for any check values for comparison at the end of the stream. This
+ is for use with other formats that use the deflate compressed data format
+ such as zip. Those formats provide their own check values. If a custom
+ format is developed using the raw deflate format for compressed data, it is
+ recommended that a check value such as an adler32 or a crc32 be applied to
+ the uncompressed data as is done in the zlib, gzip, and zip formats. For
+ most applications, the zlib format should be used as is. Note that comments
+ above on the use in deflateInit2() applies to the magnitude of windowBits.
+
+ windowBits can also be greater than 15 for optional gzip decoding. Add
+ 32 to windowBits to enable zlib and gzip decoding with automatic header
+ detection, or add 16 to decode only the gzip format (the zlib format will
+ return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
+ crc32 instead of an adler32.
+
+ inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit2 does not perform any decompression
+ apart from possibly reading the zlib header if present: actual decompression
+ will be done by inflate(). (So next_in and avail_in may be modified, but
+ next_out and avail_out are unused and unchanged.) The current implementation
+ of inflateInit2() does not process any header information -- that is
+ deferred until inflate() is called.
+*/
+
+ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the decompression dictionary from the given uncompressed byte
+ sequence. This function must be called immediately after a call of inflate,
+ if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+ can be determined from the adler32 value returned by that call of inflate.
+ The compressor and decompressor must use exactly the same dictionary (see
+ deflateSetDictionary). For raw inflate, this function can be called
+ immediately after inflateInit2() or inflateReset() and before any call of
+ inflate() to set the dictionary. The application must insure that the
+ dictionary that was used for compression is provided.
+
+ inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
+ expected one (incorrect adler32 value). inflateSetDictionary does not
+ perform any decompression: this will be done by subsequent calls of
+ inflate().
+*/
+
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+/*
+ Skips invalid compressed data until a full flush point (see above the
+ description of deflate with Z_FULL_FLUSH) can be found, or until all
+ available input is skipped. No output is provided.
+
+ inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
+ if no more input was provided, Z_DATA_ERROR if no flush point has been
+ found, or Z_STREAM_ERROR if the stream structure was inconsistent. In the
+ success case, the application may save the current current value of total_in
+ which indicates where valid compressed data was found. In the error case,
+ the application may repeatedly call inflateSync, providing more input each
+ time, until success or end of the input data.
+*/
+
+ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when randomly accessing a large stream. The
+ first pass through the stream can periodically record the inflate state,
+ allowing restarting inflate at those points when randomly accessing the
+ stream.
+
+ inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to inflateEnd followed by inflateInit,
+ but does not free and reallocate all the internal decompression state. The
+ stream will keep attributes that may have been set by inflateInit2.
+
+ inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+ int windowBits));
+/*
+ This function is the same as inflateReset, but it also permits changing
+ the wrap and window size requests. The windowBits parameter is interpreted
+ the same as it is for inflateInit2.
+
+ inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL), or if
+ the windowBits parameter is invalid.
+*/
+
+ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ This function inserts bits in the inflate input stream. The intent is
+ that this function is used to start inflating at a bit position in the
+ middle of a byte. The provided bits will be used before any bytes are used
+ from next_in. This function should only be used with raw inflate, and
+ should be used before the first inflate() call after inflateInit2() or
+ inflateReset(). bits must be less than or equal to 16, and that many of the
+ least significant bits of value will be inserted in the input.
+
+ If bits is negative, then the input stream bit buffer is emptied. Then
+ inflatePrime() can be called again to put bits in the buffer. This is used
+ to clear out bits leftover after feeding inflate a block description prior
+ to feeding inflate codes.
+
+ inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+/*
+ This function returns two values, one in the lower 16 bits of the return
+ value, and the other in the remaining upper bits, obtained by shifting the
+ return value down 16 bits. If the upper value is -1 and the lower value is
+ zero, then inflate() is currently decoding information outside of a block.
+ If the upper value is -1 and the lower value is non-zero, then inflate is in
+ the middle of a stored block, with the lower value equaling the number of
+ bytes from the input remaining to copy. If the upper value is not -1, then
+ it is the number of bits back from the current bit position in the input of
+ the code (literal or length/distance pair) currently being processed. In
+ that case the lower value is the number of bytes already emitted for that
+ code.
+
+ A code is being processed if inflate is waiting for more input to complete
+ decoding of the code, or if it has completed decoding but is waiting for
+ more output space to write the literal or match data.
+
+ inflateMark() is used to mark locations in the input data for random
+ access, which may be at bit positions, and to note those cases where the
+ output of a code may span boundaries of random access blocks. The current
+ location in the input stream can be determined from avail_in and data_type
+ as noted in the description for the Z_BLOCK flush parameter for inflate.
+
+ inflateMark returns the value noted above or -1 << 16 if the provided
+ source stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ inflateGetHeader() requests that gzip header information be stored in the
+ provided gz_header structure. inflateGetHeader() may be called after
+ inflateInit2() or inflateReset(), and before the first call of inflate().
+ As inflate() processes the gzip stream, head->done is zero until the header
+ is completed, at which time head->done is set to one. If a zlib stream is
+ being decoded, then head->done is set to -1 to indicate that there will be
+ no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be
+ used to force inflate() to return immediately after header processing is
+ complete and before any actual data is decompressed.
+
+ The text, time, xflags, and os fields are filled in with the gzip header
+ contents. hcrc is set to true if there is a header CRC. (The header CRC
+ was valid if done is set to one.) If extra is not Z_NULL, then extra_max
+ contains the maximum number of bytes to write to extra. Once done is true,
+ extra_len contains the actual extra field length, and extra contains the
+ extra field, or that field truncated if extra_max is less than extra_len.
+ If name is not Z_NULL, then up to name_max characters are written there,
+ terminated with a zero unless the length is greater than name_max. If
+ comment is not Z_NULL, then up to comm_max characters are written there,
+ terminated with a zero unless the length is greater than comm_max. When any
+ of extra, name, or comment are not Z_NULL and the respective field is not
+ present in the header, then that field is set to Z_NULL to signal its
+ absence. This allows the use of deflateSetHeader() with the returned
+ structure to duplicate the header. However if those fields are set to
+ allocated memory, then the application will need to save those pointers
+ elsewhere so that they can be eventually freed.
+
+ If inflateGetHeader is not used, then the header information is simply
+ discarded. The header is always checked for validity, including the header
+ CRC if present. inflateReset() will reset the process to discard the header
+ information. The application would need to call inflateGetHeader() again to
+ retrieve the header from the next gzip stream.
+
+ inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window));
+
+ Initialize the internal stream state for decompression using inflateBack()
+ calls. The fields zalloc, zfree and opaque in strm must be initialized
+ before the call. If zalloc and zfree are Z_NULL, then the default library-
+ derived memory allocation routines are used. windowBits is the base two
+ logarithm of the window size, in the range 8..15. window is a caller
+ supplied buffer of that size. Except for special applications where it is
+ assured that deflate was used with small window sizes, windowBits must be 15
+ and a 32K byte window must be supplied to be able to decompress general
+ deflate streams.
+
+ See inflateBack() for the usage of these routines.
+
+ inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
+ the paramaters are invalid, Z_MEM_ERROR if the internal state could not be
+ allocated, or Z_VERSION_ERROR if the version of the library does not match
+ the version of the header file.
+*/
+
+typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
+typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
+
+ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
+ in_func in, void FAR *in_desc,
+ out_func out, void FAR *out_desc));
+/*
+ inflateBack() does a raw inflate with a single call using a call-back
+ interface for input and output. This is more efficient than inflate() for
+ file i/o applications in that it avoids copying between the output and the
+ sliding window by simply making the window itself the output buffer. This
+ function trusts the application to not change the output buffer passed by
+ the output function, at least until inflateBack() returns.
+
+ inflateBackInit() must be called first to allocate the internal state
+ and to initialize the state with the user-provided window buffer.
+ inflateBack() may then be used multiple times to inflate a complete, raw
+ deflate stream with each call. inflateBackEnd() is then called to free the
+ allocated state.
+
+ A raw deflate stream is one with no zlib or gzip header or trailer.
+ This routine would normally be used in a utility that reads zip or gzip
+ files and writes out uncompressed files. The utility would decode the
+ header and process the trailer on its own, hence this routine expects only
+ the raw deflate stream to decompress. This is different from the normal
+ behavior of inflate(), which expects either a zlib or gzip header and
+ trailer around the deflate stream.
+
+ inflateBack() uses two subroutines supplied by the caller that are then
+ called by inflateBack() for input and output. inflateBack() calls those
+ routines until it reads a complete deflate stream and writes out all of the
+ uncompressed data, or until it encounters an error. The function's
+ parameters and return types are defined above in the in_func and out_func
+ typedefs. inflateBack() will call in(in_desc, &buf) which should return the
+ number of bytes of provided input, and a pointer to that input in buf. If
+ there is no input available, in() must return zero--buf is ignored in that
+ case--and inflateBack() will return a buffer error. inflateBack() will call
+ out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
+ should return zero on success, or non-zero on failure. If out() returns
+ non-zero, inflateBack() will return with an error. Neither in() nor out()
+ are permitted to change the contents of the window provided to
+ inflateBackInit(), which is also the buffer that out() uses to write from.
+ The length written by out() will be at most the window size. Any non-zero
+ amount of input may be provided by in().
+
+ For convenience, inflateBack() can be provided input on the first call by
+ setting strm->next_in and strm->avail_in. If that input is exhausted, then
+ in() will be called. Therefore strm->next_in must be initialized before
+ calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
+ immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
+ must also be initialized, and then if strm->avail_in is not zero, input will
+ initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+
+ The in_desc and out_desc parameters of inflateBack() is passed as the
+ first parameter of in() and out() respectively when they are called. These
+ descriptors can be optionally used to pass any information that the caller-
+ supplied in() and out() functions need to do their job.
+
+ On return, inflateBack() will set strm->next_in and strm->avail_in to
+ pass back any unused input that was provided by the last in() call. The
+ return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
+ if in() or out() returned an error, Z_DATA_ERROR if there was a format error
+ in the deflate stream (in which case strm->msg is set to indicate the nature
+ of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
+ In the case of Z_BUF_ERROR, an input or output error can be distinguished
+ using strm->next_in which will be Z_NULL only if in() returned an error. If
+ strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
+ non-zero. (in() will always be called before out(), so strm->next_in is
+ assured to be defined if out() returns non-zero.) Note that inflateBack()
+ cannot return Z_OK.
+*/
+
+ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
+/*
+ All memory allocated by inflateBackInit() is freed.
+
+ inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
+ state was inconsistent.
+*/
+
+ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
+/* Return flags indicating compile-time options.
+
+ Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
+ 1.0: size of uInt
+ 3.2: size of uLong
+ 5.4: size of voidpf (pointer)
+ 7.6: size of z_off_t
+
+ Compiler, assembler, and debug options:
+ 8: DEBUG
+ 9: ASMV or ASMINF -- use ASM code
+ 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
+ 11: 0 (reserved)
+
+ One-time table building (smaller code, but not thread-safe if true):
+ 12: BUILDFIXED -- build static block decoding tables when needed
+ 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
+ 14,15: 0 (reserved)
+
+ Library content (indicates missing functionality):
+ 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
+ deflate code when not needed)
+ 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
+ and decode gzip streams (to avoid linking crc code)
+ 18-19: 0 (reserved)
+
+ Operation variations (changes in library functionality):
+ 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
+ 21: FASTEST -- deflate algorithm with only one, lowest compression level
+ 22,23: 0 (reserved)
+
+ The sprintf variant used by gzprintf (zero is best):
+ 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
+ 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
+ 26: 0 = returns value, 1 = void -- 1 means inferred string length returned
+
+ Remainder:
+ 27-31: 0 (reserved)
+ */
+
+
+ /* utility functions */
+
+/*
+ The following utility functions are implemented on top of the basic
+ stream-oriented functions. To simplify the interface, some default options
+ are assumed (compression level and memory usage, standard memory allocation
+ functions). The source code of these utility functions can be modified if
+ you need special options.
+*/
+
+ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Compresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed buffer.
+
+ compress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer.
+*/
+
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen,
+ int level));
+/*
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+
+ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
+/*
+ compressBound() returns an upper bound on the compressed size after
+ compress() or compress2() on sourceLen bytes. It would be used before a
+ compress() or compress2() call to allocate the destination buffer.
+*/
+
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Decompresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be large enough to hold the entire
+ uncompressed data. (The size of the uncompressed data must have been saved
+ previously by the compressor and transmitted to the decompressor by some
+ mechanism outside the scope of this compression library.) Upon exit, destLen
+ is the actual size of the uncompressed buffer.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
+*/
+
+
+ /* gzip file access functions */
+
+/*
+ This library supports reading and writing files in gzip (.gz) format with
+ an interface similar to that of stdio, using the functions that start with
+ "gz". The gzip format is different from the zlib format. gzip is a gzip
+ wrapper, documented in RFC 1952, wrapped around a deflate stream.
+*/
+
+typedef voidp gzFile; /* opaque gzip file descriptor */
+
+/*
+ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
+
+ Opens a gzip (.gz) file for reading or writing. The mode parameter is as
+ in fopen ("rb" or "wb") but can also include a compression level ("wb9") or
+ a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only
+ compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F'
+ for fixed code compression as in "wb9F". (See the description of
+ deflateInit2 for more information about the strategy parameter.) Also "a"
+ can be used instead of "w" to request that the gzip stream that will be
+ written be appended to the file. "+" will result in an error, since reading
+ and writing to the same gzip file is not supported.
+
+ gzopen can be used to read a file which is not in gzip format; in this
+ case gzread will directly read from the file without decompression.
+
+ gzopen returns NULL if the file could not be opened, if there was
+ insufficient memory to allocate the gzFile state, or if an invalid mode was
+ specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
+ errno can be checked to determine if the reason gzopen failed was that the
+ file could not be opened.
+*/
+
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
+/*
+ gzdopen associates a gzFile with the file descriptor fd. File descriptors
+ are obtained from calls like open, dup, creat, pipe or fileno (if the file
+ has been previously opened with fopen). The mode parameter is as in gzopen.
+
+ The next call of gzclose on the returned gzFile will also close the file
+ descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
+ fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
+ mode);. The duplicated descriptor should be saved to avoid a leak, since
+ gzdopen does not close fd if it fails.
+
+ gzdopen returns NULL if there was insufficient memory to allocate the
+ gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
+ provided, or '+' was provided), or if fd is -1. The file descriptor is not
+ used until the next gz* read, write, seek, or close operation, so gzdopen
+ will not detect if fd is invalid (unless fd is -1).
+*/
+
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
+/*
+ Set the internal buffer size used by this library's functions. The
+ default buffer size is 8192 bytes. This function must be called after
+ gzopen() or gzdopen(), and before any other calls that read or write the
+ file. The buffer memory allocation is always deferred to the first read or
+ write. Two buffers are allocated, either both of the specified size when
+ writing, or one of the specified size and the other twice that size when
+ reading. A larger buffer size of, for example, 64K or 128K bytes will
+ noticeably increase the speed of decompression (reading).
+
+ The new buffer size also affects the maximum length for gzprintf().
+
+ gzbuffer() returns 0 on success, or -1 on failure, such as being called
+ too late.
+*/
+
+ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
+/*
+ Dynamically update the compression level or strategy. See the description
+ of deflateInit2 for the meaning of these parameters.
+
+ gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
+ opened for writing.
+*/
+
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
+/*
+ Reads the given number of uncompressed bytes from the compressed file. If
+ the input file was not in gzip format, gzread copies the given number of
+ bytes into the buffer.
+
+ After reaching the end of a gzip stream in the input, gzread will continue
+ to read, looking for another gzip stream, or failing that, reading the rest
+ of the input file directly without decompression. The entire input file
+ will be read if gzread is called until it returns less than the requested
+ len.
+
+ gzread returns the number of uncompressed bytes actually read, less than
+ len for end of file, or -1 for error.
+*/
+
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+ voidpc buf, unsigned len));
+/*
+ Writes the given number of uncompressed bytes into the compressed file.
+ gzwrite returns the number of uncompressed bytes written or 0 in case of
+ error.
+*/
+
+ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
+/*
+ Converts, formats, and writes the arguments to the compressed file under
+ control of the format string, as in fprintf. gzprintf returns the number of
+ uncompressed bytes actually written, or 0 in case of error. The number of
+ uncompressed bytes written is limited to 8191, or one less than the buffer
+ size given to gzbuffer(). The caller should assure that this limit is not
+ exceeded. If it is exceeded, then gzprintf() will return an error (0) with
+ nothing written. In this case, there may also be a buffer overflow with
+ unpredictable consequences, which is possible only if zlib was compiled with
+ the insecure functions sprintf() or vsprintf() because the secure snprintf()
+ or vsnprintf() functions were not available. This can be determined using
+ zlibCompileFlags().
+*/
+
+ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
+/*
+ Writes the given null-terminated string to the compressed file, excluding
+ the terminating null character.
+
+ gzputs returns the number of characters written, or -1 in case of error.
+*/
+
+ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
+/*
+ Reads bytes from the compressed file until len-1 characters are read, or a
+ newline character is read and transferred to buf, or an end-of-file
+ condition is encountered. If any characters are read or if len == 1, the
+ string is terminated with a null character. If no characters are read due
+ to an end-of-file or len < 1, then the buffer is left untouched.
+
+ gzgets returns buf which is a null-terminated string, or it returns NULL
+ for end-of-file or in case of error. If there was an error, the contents at
+ buf are indeterminate.
+*/
+
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
+/*
+ Writes c, converted to an unsigned char, into the compressed file. gzputc
+ returns the value that was written, or -1 in case of error.
+*/
+
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
+/*
+ Reads one byte from the compressed file. gzgetc returns this byte or -1
+ in case of end of file or error.
+*/
+
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
+/*
+ Push one character back onto the stream to be read as the first character
+ on the next read. At least one character of push-back is allowed.
+ gzungetc() returns the character pushed, or -1 on failure. gzungetc() will
+ fail if c is -1, and may fail if a character has been pushed but not read
+ yet. If gzungetc is used immediately after gzopen or gzdopen, at least the
+ output buffer size of pushed characters is allowed. (See gzbuffer above.)
+ The pushed character will be discarded if the stream is repositioned with
+ gzseek() or gzrewind().
+*/
+
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
+/*
+ Flushes all pending output into the compressed file. The parameter flush
+ is as in the deflate() function. The return value is the zlib error number
+ (see function gzerror below). gzflush is only permitted when writing.
+
+ If the flush parameter is Z_FINISH, the remaining data is written and the
+ gzip stream is completed in the output. If gzwrite() is called again, a new
+ gzip stream will be started in the output. gzread() is able to read such
+ concatented gzip streams.
+
+ gzflush should be called only when strictly necessary because it will
+ degrade compression if called too often.
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
+ z_off_t offset, int whence));
+
+ Sets the starting position for the next gzread or gzwrite on the given
+ compressed file. The offset represents a number of bytes in the
+ uncompressed data stream. The whence parameter is defined as in lseek(2);
+ the value SEEK_END is not supported.
+
+ If the file is opened for reading, this function is emulated but can be
+ extremely slow. If the file is opened for writing, only forward seeks are
+ supported; gzseek then compresses a sequence of zeroes up to the new
+ starting position.
+
+ gzseek returns the resulting offset location as measured in bytes from
+ the beginning of the uncompressed stream, or -1 in case of error, in
+ particular if the file is opened for writing and the new starting position
+ would be before the current position.
+*/
+
+ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
+/*
+ Rewinds the given file. This function is supported only for reading.
+
+ gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
+
+ Returns the starting position for the next gzread or gzwrite on the given
+ compressed file. This position represents a number of bytes in the
+ uncompressed data stream, and is zero when starting, even if appending or
+ reading a gzip stream from the middle of a file using gzdopen().
+
+ gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
+
+ Returns the current offset in the file being read or written. This offset
+ includes the count of bytes that precede the gzip stream, for example when
+ appending or when using gzdopen() for reading. When reading, the offset
+ does not include as yet unused buffered input. This information can be used
+ for a progress indicator. On error, gzoffset() returns -1.
+*/
+
+ZEXTERN int ZEXPORT gzeof OF((gzFile file));
+/*
+ Returns true (1) if the end-of-file indicator has been set while reading,
+ false (0) otherwise. Note that the end-of-file indicator is set only if the
+ read tried to go past the end of the input, but came up short. Therefore,
+ just like feof(), gzeof() may return false even if there is no more data to
+ read, in the event that the last read request was for the exact number of
+ bytes remaining in the input file. This will happen if the input file size
+ is an exact multiple of the buffer size.
+
+ If gzeof() returns true, then the read functions will return no more data,
+ unless the end-of-file indicator is reset by gzclearerr() and the input file
+ has grown since the previous end of file was detected.
+*/
+
+ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
+/*
+ Returns true (1) if file is being copied directly while reading, or false
+ (0) if file is a gzip stream being decompressed. This state can change from
+ false to true while reading the input file if the end of a gzip stream is
+ reached, but is followed by data that is not another gzip stream.
+
+ If the input file is empty, gzdirect() will return true, since the input
+ does not contain a gzip stream.
+
+ If gzdirect() is used immediately after gzopen() or gzdopen() it will
+ cause buffers to be allocated to allow reading the file to determine if it
+ is a gzip file. Therefore if gzbuffer() is used, it should be called before
+ gzdirect().
+*/
+
+ZEXTERN int ZEXPORT gzclose OF((gzFile file));
+/*
+ Flushes all pending output if necessary, closes the compressed file and
+ deallocates the (de)compression state. Note that once file is closed, you
+ cannot call gzerror with file, since its structures have been deallocated.
+ gzclose must not be called more than once on the same file, just as free
+ must not be called more than once on the same allocation.
+
+ gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
+ file operation error, or Z_OK on success.
+*/
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+/*
+ Same as gzclose(), but gzclose_r() is only for use when reading, and
+ gzclose_w() is only for use when writing or appending. The advantage to
+ using these instead of gzclose() is that they avoid linking in zlib
+ compression or decompression code that is not used when only reading or only
+ writing respectively. If gzclose() is used, then both compression and
+ decompression code will be included the application when linking to a static
+ zlib library.
+*/
+
+ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
+/*
+ Returns the error message for the last error which occurred on the given
+ compressed file. errnum is set to zlib error number. If an error occurred
+ in the file system and not in the compression library, errnum is set to
+ Z_ERRNO and the application may consult errno to get the exact error code.
+
+ The application must not modify the returned string. Future calls to
+ this function may invalidate the previously returned string. If file is
+ closed, then the string previously returned by gzerror will no longer be
+ available.
+
+ gzerror() should be used to distinguish errors from end-of-file for those
+ functions above that do not distinguish those cases in their return values.
+*/
+
+ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
+/*
+ Clears the error and end-of-file flags for file. This is analogous to the
+ clearerr() function in stdio. This is useful for continuing to read a gzip
+ file that is being written concurrently.
+*/
+
+
+ /* checksum functions */
+
+/*
+ These functions are not related to compression but are exported
+ anyway because they might be useful in applications using the compression
+ library.
+*/
+
+ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+/*
+ Update a running Adler-32 checksum with the bytes buf[0..len-1] and
+ return the updated checksum. If buf is Z_NULL, this function returns the
+ required initial value for the checksum.
+
+ An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+ much faster.
+
+ Usage example:
+
+ uLong adler = adler32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ adler = adler32(adler, buffer, length);
+ }
+ if (adler != original_adler) error();
+*/
+
+/*
+ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
+ z_off_t len2));
+
+ Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
+ and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
+ each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
+ seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
+*/
+
+ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
+/*
+ Update a running CRC-32 with the bytes buf[0..len-1] and return the
+ updated CRC-32. If buf is Z_NULL, this function returns the required
+ initial value for the for the crc. Pre- and post-conditioning (one's
+ complement) is performed within this function so it shouldn't be done by the
+ application.
+
+ Usage example:
+
+ uLong crc = crc32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ crc = crc32(crc, buffer, length);
+ }
+ if (crc != original_crc) error();
+*/
+
+/*
+ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
+
+ Combine two CRC-32 check values into one. For two sequences of bytes,
+ seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
+ calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
+ check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
+ len2.
+*/
+
+
+ /* various hacks, don't look :) */
+
+/* deflateInit and inflateInit are macros to allow checking the zlib version
+ * and the compiler's view of z_stream:
+ */
+ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
+ int windowBits, int memLevel,
+ int strategy, const char *version,
+ int stream_size));
+ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window,
+ const char *version,
+ int stream_size));
+#define deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
+#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+#define inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, sizeof(z_stream))
+
+/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
+ * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
+ * both are true, the application gets the *64 functions, and the regular
+ * functions are changed to 64 bits) -- in case these are set on systems
+ * without large file support, _LFS64_LARGEFILE must also be true
+ */
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0
+# define gzopen gzopen64
+# define gzseek gzseek64
+# define gztell gztell64
+# define gzoffset gzoffset64
+# define adler32_combine adler32_combine64
+# define crc32_combine crc32_combine64
+# ifdef _LARGEFILE64_SOURCE
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+# endif
+#else
+ ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
+
+/* hack for buggy compilers */
+#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
+ struct internal_state {int dummy;};
+#endif
+
+/* undocumented functions */
+ZEXTERN const char * ZEXPORT zError OF((int));
+ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp));
+ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
+ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int));
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ZLIB_H */
diff --git a/test/monniaux/glpk-4.65/src/zlib/zutil.c b/test/monniaux/glpk-4.65/src/zlib/zutil.c
new file mode 100644
index 00000000..898ed345
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zutil.c
@@ -0,0 +1,318 @@
+/* zutil.c -- target dependent utility functions for the compression library
+ * Copyright (C) 1995-2005, 2010 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+
+#ifndef NO_DUMMY_DECL
+struct internal_state {int dummy;}; /* for buggy compilers */
+#endif
+
+const char * const z_errmsg[10] = {
+"need dictionary", /* Z_NEED_DICT 2 */
+"stream end", /* Z_STREAM_END 1 */
+"", /* Z_OK 0 */
+"file error", /* Z_ERRNO (-1) */
+"stream error", /* Z_STREAM_ERROR (-2) */
+"data error", /* Z_DATA_ERROR (-3) */
+"insufficient memory", /* Z_MEM_ERROR (-4) */
+"buffer error", /* Z_BUF_ERROR (-5) */
+"incompatible version",/* Z_VERSION_ERROR (-6) */
+""};
+
+
+const char * ZEXPORT zlibVersion()
+{
+ return ZLIB_VERSION;
+}
+
+uLong ZEXPORT zlibCompileFlags()
+{
+ uLong flags;
+
+ flags = 0;
+ switch ((int)(sizeof(uInt))) {
+ case 2: break;
+ case 4: flags += 1; break;
+ case 8: flags += 2; break;
+ default: flags += 3;
+ }
+ switch ((int)(sizeof(uLong))) {
+ case 2: break;
+ case 4: flags += 1 << 2; break;
+ case 8: flags += 2 << 2; break;
+ default: flags += 3 << 2;
+ }
+ switch ((int)(sizeof(voidpf))) {
+ case 2: break;
+ case 4: flags += 1 << 4; break;
+ case 8: flags += 2 << 4; break;
+ default: flags += 3 << 4;
+ }
+ switch ((int)(sizeof(z_off_t))) {
+ case 2: break;
+ case 4: flags += 1 << 6; break;
+ case 8: flags += 2 << 6; break;
+ default: flags += 3 << 6;
+ }
+#ifdef DEBUG
+ flags += 1 << 8;
+#endif
+#if defined(ASMV) || defined(ASMINF)
+ flags += 1 << 9;
+#endif
+#ifdef ZLIB_WINAPI
+ flags += 1 << 10;
+#endif
+#ifdef BUILDFIXED
+ flags += 1 << 12;
+#endif
+#ifdef DYNAMIC_CRC_TABLE
+ flags += 1 << 13;
+#endif
+#ifdef NO_GZCOMPRESS
+ flags += 1L << 16;
+#endif
+#ifdef NO_GZIP
+ flags += 1L << 17;
+#endif
+#ifdef PKZIP_BUG_WORKAROUND
+ flags += 1L << 20;
+#endif
+#ifdef FASTEST
+ flags += 1L << 21;
+#endif
+#ifdef STDC
+# ifdef NO_vsnprintf
+ flags += 1L << 25;
+# ifdef HAS_vsprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_vsnprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#else
+ flags += 1L << 24;
+# ifdef NO_snprintf
+ flags += 1L << 25;
+# ifdef HAS_sprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_snprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#endif
+ return flags;
+}
+
+#ifdef DEBUG
+
+# ifndef verbose
+# define verbose 0
+# endif
+int ZLIB_INTERNAL z_verbose = verbose;
+
+void ZLIB_INTERNAL z_error (m)
+ char *m;
+{
+ fprintf(stderr, "%s\n", m);
+ exit(1);
+}
+#endif
+
+/* exported to allow conversion of error code to string for compress() and
+ * uncompress()
+ */
+const char * ZEXPORT zError(err)
+ int err;
+{
+ return ERR_MSG(err);
+}
+
+#if defined(_WIN32_WCE)
+ /* The Microsoft C Run-Time Library for Windows CE doesn't have
+ * errno. We define it as a global variable to simplify porting.
+ * Its value is always 0 and should not be used.
+ */
+ int errno = 0;
+#endif
+
+#ifndef HAVE_MEMCPY
+
+void ZLIB_INTERNAL zmemcpy(dest, source, len)
+ Bytef* dest;
+ const Bytef* source;
+ uInt len;
+{
+ if (len == 0) return;
+ do {
+ *dest++ = *source++; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+
+int ZLIB_INTERNAL zmemcmp(s1, s2, len)
+ const Bytef* s1;
+ const Bytef* s2;
+ uInt len;
+{
+ uInt j;
+
+ for (j = 0; j < len; j++) {
+ if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
+ }
+ return 0;
+}
+
+void ZLIB_INTERNAL zmemzero(dest, len)
+ Bytef* dest;
+ uInt len;
+{
+ if (len == 0) return;
+ do {
+ *dest++ = 0; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+#endif
+
+
+#ifdef SYS16BIT
+
+#ifdef __TURBOC__
+/* Turbo C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
+ * and farmalloc(64K) returns a pointer with an offset of 8, so we
+ * must fix the pointer. Warning: the pointer must be put back to its
+ * original form in order to free it, use zcfree().
+ */
+
+#define MAX_PTR 10
+/* 10*64K = 640K */
+
+local int next_ptr = 0;
+
+typedef struct ptr_table_s {
+ voidpf org_ptr;
+ voidpf new_ptr;
+} ptr_table;
+
+local ptr_table table[MAX_PTR];
+/* This table is used to remember the original form of pointers
+ * to large buffers (64K). Such pointers are normalized with a zero offset.
+ * Since MSDOS is not a preemptive multitasking OS, this table is not
+ * protected from concurrent access. This hack doesn't work anyway on
+ * a protected system like OS/2. Use Microsoft C instead.
+ */
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
+{
+ voidpf buf = opaque; /* just to make some compilers happy */
+ ulg bsize = (ulg)items*size;
+
+ /* If we allocate less than 65520 bytes, we assume that farmalloc
+ * will return a usable pointer which doesn't have to be normalized.
+ */
+ if (bsize < 65520L) {
+ buf = farmalloc(bsize);
+ if (*(ush*)&buf != 0) return buf;
+ } else {
+ buf = farmalloc(bsize + 16L);
+ }
+ if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
+ table[next_ptr].org_ptr = buf;
+
+ /* Normalize the pointer to seg:0 */
+ *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
+ *(ush*)&buf = 0;
+ table[next_ptr++].new_ptr = buf;
+ return buf;
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ int n;
+ if (*(ush*)&ptr != 0) { /* object < 64K */
+ farfree(ptr);
+ return;
+ }
+ /* Find the original pointer */
+ for (n = 0; n < next_ptr; n++) {
+ if (ptr != table[n].new_ptr) continue;
+
+ farfree(table[n].org_ptr);
+ while (++n < next_ptr) {
+ table[n-1] = table[n];
+ }
+ next_ptr--;
+ return;
+ }
+ ptr = opaque; /* just to make some compilers happy */
+ Assert(0, "zcfree: ptr not found");
+}
+
+#endif /* __TURBOC__ */
+
+
+#ifdef M_I86
+/* Microsoft C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
+# define _halloc halloc
+# define _hfree hfree
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
+{
+ if (opaque) opaque = 0; /* to make compiler happy */
+ return _halloc((long)items, size);
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ if (opaque) opaque = 0; /* to make compiler happy */
+ _hfree(ptr);
+}
+
+#endif /* M_I86 */
+
+#endif /* SYS16BIT */
+
+
+#ifndef MY_ZCALLOC /* Any system without a special alloc function */
+
+#ifndef STDC
+extern voidp malloc OF((uInt size));
+extern voidp calloc OF((uInt items, uInt size));
+extern void free OF((voidpf ptr));
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
+ voidpf opaque;
+ unsigned items;
+ unsigned size;
+{
+ if (opaque) items += size - size; /* make compiler happy */
+ return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
+ (voidpf)calloc(items, size);
+}
+
+void ZLIB_INTERNAL zcfree (opaque, ptr)
+ voidpf opaque;
+ voidpf ptr;
+{
+ free(ptr);
+ if (opaque) return; /* make compiler happy */
+}
+
+#endif /* MY_ZCALLOC */
diff --git a/test/monniaux/glpk-4.65/src/zlib/zutil.h b/test/monniaux/glpk-4.65/src/zlib/zutil.h
new file mode 100644
index 00000000..737bd38f
--- /dev/null
+++ b/test/monniaux/glpk-4.65/src/zlib/zutil.h
@@ -0,0 +1,93 @@
+/* zutil.h (internal interface of the zlib compression library) */
+
+/* Modified by Andrew Makhorin <mao@gnu.org>, April 2011 */
+
+/* Copyright (C) 1995-2010 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in
+ * zlib.h */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h. */
+
+#ifndef ZUTIL_H
+#define ZUTIL_H
+
+#define ZLIB_INTERNAL
+
+#include "zlib.h"
+
+#include <stddef.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define local static
+
+typedef unsigned char uch;
+typedef uch uchf;
+typedef unsigned short ush;
+typedef ush ushf;
+typedef unsigned long ulg;
+
+extern const char * const z_errmsg[10];
+
+#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
+
+#define ERR_RETURN(strm, err) \
+ return (strm->msg = (char *)ERR_MSG(err), (err))
+
+#define DEF_WBITS MAX_WBITS
+
+#if MAX_MEM_LEVEL >= 8
+#define DEF_MEM_LEVEL 8
+#else
+#define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+
+#define STORED_BLOCK 0
+#define STATIC_TREES 1
+#define DYN_TREES 2
+
+#define MIN_MATCH 3
+#define MAX_MATCH 258
+
+#define PRESET_DICT 0x20
+
+#define OS_CODE 0x03 /* assume Unix */
+
+#define HAVE_MEMCPY 1
+#define zmemcpy memcpy
+#define zmemzero(dest, len) memset(dest, 0, len)
+
+#ifdef DEBUG
+#include <stdio.h>
+extern int ZLIB_INTERNAL z_verbose;
+extern void ZLIB_INTERNAL z_error OF((char *m));
+#define Assert(cond, msg) { if(!(cond)) z_error(msg); }
+#define Trace(x) { if (z_verbose >= 0) fprintf x; }
+#define Tracev(x) { if (z_verbose > 0) fprintf x; }
+#define Tracevv(x) {if (z_verbose > 1) fprintf x; }
+#define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x; }
+#define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x; }
+#else
+#define Assert(cond, msg)
+#define Trace(x)
+#define Tracev(x)
+#define Tracevv(x)
+#define Tracec(c, x)
+#define Tracecv(c, x)
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
+ unsigned size));
+void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
+
+#define ZALLOC(strm, items, size) \
+ (*((strm)->zalloc))((strm)->opaque, (items), (size))
+#define ZFREE(strm, addr) \
+ (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
+#define TRY_FREE(s, p) { if (p) ZFREE(s, p); }
+
+#endif
+
+/* eof */
diff --git a/test/monniaux/heapsort/Makefile b/test/monniaux/heapsort/Makefile
new file mode 100644
index 00000000..69f0c3ca
--- /dev/null
+++ b/test/monniaux/heapsort/Makefile
@@ -0,0 +1,3 @@
+TARGET=heapsort
+
+include ../rules.mk
diff --git a/test/monniaux/heapsort/heapsort.c b/test/monniaux/heapsort/heapsort.c
new file mode 100644
index 00000000..550eff4d
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort.c
@@ -0,0 +1,58 @@
+#include "heapsort.h"
+
+/* Rosetta Code */
+static inline int max (data *a, int n, int i, int j, int k) {
+ int m = i;
+ if (j < n && a[j] > a[m]) {
+ m = j;
+ }
+ if (k < n && a[k] > a[m]) {
+ m = k;
+ }
+ return m;
+}
+
+static void downheap (data *a, int n, int i) {
+ while (1) {
+ int j = max(a, n, i, 2 * i + 1, 2 * i + 2);
+ if (j == i) {
+ break;
+ }
+ data t = a[i];
+ a[i] = a[j];
+ a[j] = t;
+ i = j;
+ }
+}
+
+void heapsort (data *a, int n) {
+ int i;
+ for (i = (n - 2) / 2; i >= 0; i--) {
+ downheap(a, n, i);
+ }
+ for (i = 0; i < n; i++) {
+ data t = a[n - i - 1];
+ a[n - i - 1] = a[0];
+ a[0] = t;
+ downheap(a, n - i - 1, 0);
+ }
+}
+
+data data_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next;
+}
+
+void data_vec_random(data *a, unsigned len) {
+ for(unsigned i=0; i<len; i++) {
+ a[i] = data_random();
+ }
+}
+
+bool data_vec_is_sorted(const data *a, unsigned len) {
+ for(unsigned i=0; i<len-1; i++) {
+ if (a[i] > a[i+1]) return false;
+ }
+ return true;
+}
diff --git a/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified5 b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified5
new file mode 100644
index 00000000..f56df84c
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified5
@@ -0,0 +1,333 @@
+# File generated by CompCert 3.4
+# Command line: -Wall -O3 -S heapsort.c -o heapsort.ccomp.kvx.s
+ .text
+ .balign 2
+downheap:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+.L100:
+ sllw $r3 = $r2, 1
+ addd $r4 = $r2, 0
+;;
+ addw $r5 = $r3, 1
+ addw $r3 = $r3, 2
+;;
+ compw.ge $r32 = $r5, $r1
+;;
+ cb.wnez $r32?.L101
+;;
+ sxwd $r11 = $r5
+ sxwd $r43 = $r4
+;;
+ ld.xs $r7 = $r11[$r0]
+;;
+ ld.xs $r35 = $r43[$r0]
+;;
+ compd.leu $r32 = $r7, $r35
+;;
+ cmoved.weqz $r32? $r4 = $r5
+.L101:
+ compw.ge $r32 = $r3, $r1
+;;
+ cb.wnez $r32?.L102
+;;
+ sxwd $r10 = $r3
+ sxwd $r34 = $r4
+;;
+ ld.xs $r45 = $r10[$r0]
+;;
+ ld.xs $r44 = $r34[$r0]
+;;
+ compd.leu $r32 = $r45, $r44
+;;
+ cb.wnez $r32?.L102
+;;
+ addd $r4 = $r3, 0
+;;
+.L102:
+ compw.eq $r32 = $r4, $r2
+;;
+ cb.wnez $r32?.L103
+;;
+ sxwd $r46 = $r2
+ sxwd $r6 = $r4
+ addd $r2 = $r4, 0
+;;
+ ld.xs $r33 = $r6[$r0]
+;;
+ ld.xs $r42 = $r46[$r0]
+;;
+ sd.xs $r46[$r0] = $r33
+;;
+ sd.xs $r6[$r0] = $r42
+ goto .L100
+;;
+.L103:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type downheap, @function
+ .size downheap, . - downheap
+ .text
+ .balign 2
+ .globl heapsort
+heapsort:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r1, 0
+;;
+ addw $r6 = $r19, 4294967294
+;;
+ sraw $r32 = $r6, 31
+;;
+ sd 32[$r12] = $r20
+ addd $r20 = $r0, 0
+ srlw $r32 = $r32, 31
+;;
+ addw $r32 = $r6, $r32
+;;
+ sraw $r18 = $r32, 1
+;;
+.L104:
+ cb.wltz $r18?.L105
+;;
+ addd $r2 = $r18, 0
+ addd $r1 = $r19, 0
+ addd $r0 = $r20, 0
+ call downheap
+;;
+ addw $r18 = $r18, 4294967295
+ goto .L104
+;;
+.L105:
+ make $r18, 0
+;;
+.L106:
+ compw.ge $r32 = $r18, $r19
+;;
+ cb.wnez $r32?.L107
+;;
+ sbfw $r3 = $r18, $r19
+ ld $r4 = 0[$r20]
+;;
+ addw $r1 = $r3, 4294967295
+;;
+ sxwd $r7 = $r1
+;;
+ make $r2, 0
+ ld.xs $r9 = $r7[$r20]
+;;
+ sd 0[$r0] = $r4
+ addd $r0 = $r20, 0
+;;
+ sd 0[$r20] = $r9
+ call downheap
+;;
+ addw $r18 = $r18, 1
+ goto .L106
+;;
+.L107:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type heapsort, @function
+ .size heapsort, . - heapsort
+ .data
+ .balign 8
+next:
+ .long 0x4f091c37, 0x0
+ .type next, @object
+ .size next, . - next
+ .text
+ .balign 2
+ .globl data_random
+data_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r32 = next
+ make $r0, 1103515249
+;;
+ ld $r1 = 0[$r32]
+ make $r32 = next
+;;
+ muld $r63 = $r1, $r0
+;;
+ addd $r0 = $r63, 12345
+;;
+ sd 0[$r32] = $r0
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_random, @function
+ .size data_random, . - data_random
+ .text
+ .balign 2
+ .globl data_vec_random
+data_vec_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+ make $r20, 0
+;;
+.L108:
+ compw.geu $r32 = $r20, $r18
+;;
+ cb.wnez $r32?.L109
+;;
+ call data_random
+;;
+ addd $r1 = $r20, 0
+ addw $r20 = $r20, 1
+;;
+ zxwd $r1 = $r1
+;;
+ slld $r2 = $r1, 3
+;;
+ addd $r3 = $r19, $r2
+;;
+ sd 0[$r3] = $r0
+ goto .L108
+;;
+.L109:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type data_vec_random, @function
+ .size data_vec_random, . - data_vec_random
+ .text
+ .balign 2
+ .globl data_vec_is_sorted
+data_vec_is_sorted:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r2, 0
+;;
+.L110:
+ addw $r6 = $r1, 4294967295
+;;
+ compw.geu $r32 = $r2, $r6
+;;
+ cb.wnez $r32?.L111
+;;
+ addd $r3 = $r2, 0
+ addw $r2 = $r2, 1
+;;
+ zxwd $r3 = $r3
+;;
+ slld $r5 = $r3, 3
+ addd $r3 = $r2, 0
+;;
+ addd $r10 = $r0, $r5
+ zxwd $r3 = $r3
+;;
+ slld $r8 = $r3, 3
+;;
+ addd $r3 = $r0, $r8
+;;
+ ld $r4 = 0[$r10]
+;;
+ ld $r9 = 0[$r3]
+;;
+ compd.leu $r32 = $r4, $r9
+;;
+ cb.wnez $r32?.L110
+;;
+ make $r0, 0
+ goto .L112
+;;
+.L111:
+ make $r0, 1
+;;
+.L112:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_vec_is_sorted, @function
+ .size data_vec_is_sorted, . - data_vec_is_sorted
diff --git a/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified7 b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified7
new file mode 100644
index 00000000..0c873f0e
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.modified7
@@ -0,0 +1,328 @@
+# File generated by CompCert 3.4
+# Command line: -Wall -O3 -S heapsort.c -o heapsort.ccomp.kvx.s
+ .text
+ .balign 2
+downheap:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+.L100:
+ sllw $r3 = $r2, 1
+ addd $r4 = $r2, 0
+;;
+ addw $r5 = $r3, 1
+ addw $r3 = $r3, 2
+;;
+ compw.ge $r32 = $r5, $r1
+;;
+ cb.wnez $r32?.L101
+;;
+ sxwd $r11 = $r5
+ sxwd $r43 = $r4
+;;
+ ld.xs $r7 = $r11[$r0]
+;;
+ ld.xs $r35 = $r43[$r0]
+;;
+ compd.leu $r32 = $r7, $r35
+;;
+ cmoved.weqz $r32? $r4 = $r5
+;;
+.L101:
+ compw.ge $r32 = $r3, $r1
+;;
+ cb.wnez $r32?.L102
+;;
+ sxwd $r10 = $r3
+ sxwd $r34 = $r4
+;;
+ ld.xs $r45 = $r10[$r0]
+;;
+ ld.xs $r44 = $r34[$r0]
+;;
+ compd.leu $r32 = $r45, $r44
+;;
+ cmoved.weqz $r32? $r4=$r3
+;;
+.L102:
+ compw.eq $r32 = $r4, $r2
+;;
+ cb.wnez $r32?.L103
+;;
+ sxwd $r46 = $r2
+ sxwd $r6 = $r4
+ addd $r2 = $r4, 0
+;;
+ ld.xs $r33 = $r6[$r0]
+;;
+ ld.xs $r42 = $r46[$r0]
+;;
+ sd.xs $r46[$r0] = $r33
+;;
+ sd.xs $r6[$r0] = $r42
+ goto .L100
+;;
+.L103:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type downheap, @function
+ .size downheap, . - downheap
+ .text
+ .balign 2
+ .globl heapsort
+heapsort:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r1, 0
+;;
+ addw $r6 = $r19, 4294967294
+;;
+ sraw $r32 = $r6, 31
+;;
+ sd 32[$r12] = $r20
+ addd $r20 = $r0, 0
+ srlw $r32 = $r32, 31
+;;
+ addw $r32 = $r6, $r32
+;;
+ sraw $r18 = $r32, 1
+;;
+.L104:
+ cb.wltz $r18?.L105
+;;
+ addd $r2 = $r18, 0
+ addd $r1 = $r19, 0
+ addd $r0 = $r20, 0
+ call downheap
+;;
+ addw $r18 = $r18, 4294967295
+ goto .L104
+;;
+.L105:
+ make $r18, 0
+;;
+.L106:
+ compw.ge $r32 = $r18, $r19
+;;
+ cb.wnez $r32?.L107
+;;
+ sbfw $r3 = $r18, $r19
+ ld $r4 = 0[$r20]
+;;
+ addw $r1 = $r3, 4294967295
+;;
+ sxwd $r7 = $r1
+;;
+ make $r2, 0
+ ld.xs $r9 = $r7[$r20]
+;;
+ sd 0[$r0] = $r4
+ addd $r0 = $r20, 0
+;;
+ sd 0[$r20] = $r9
+ call downheap
+;;
+ addw $r18 = $r18, 1
+ goto .L106
+;;
+.L107:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type heapsort, @function
+ .size heapsort, . - heapsort
+ .data
+ .balign 8
+next:
+ .long 0x4f091c37, 0x0
+ .type next, @object
+ .size next, . - next
+ .text
+ .balign 2
+ .globl data_random
+data_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r32 = next
+ make $r0, 1103515249
+;;
+ ld $r1 = 0[$r32]
+ make $r32 = next
+;;
+ muld $r63 = $r1, $r0
+;;
+ addd $r0 = $r63, 12345
+;;
+ sd 0[$r32] = $r0
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_random, @function
+ .size data_random, . - data_random
+ .text
+ .balign 2
+ .globl data_vec_random
+data_vec_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+ make $r20, 0
+;;
+.L108:
+ compw.geu $r32 = $r20, $r18
+;;
+ cb.wnez $r32?.L109
+;;
+ call data_random
+;;
+ addd $r1 = $r20, 0
+ addw $r20 = $r20, 1
+;;
+ zxwd $r1 = $r1
+;;
+ slld $r2 = $r1, 3
+;;
+ addd $r3 = $r19, $r2
+;;
+ sd 0[$r3] = $r0
+ goto .L108
+;;
+.L109:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type data_vec_random, @function
+ .size data_vec_random, . - data_vec_random
+ .text
+ .balign 2
+ .globl data_vec_is_sorted
+data_vec_is_sorted:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r2, 0
+;;
+.L110:
+ addw $r6 = $r1, 4294967295
+;;
+ compw.geu $r32 = $r2, $r6
+;;
+ cb.wnez $r32?.L111
+;;
+ addd $r3 = $r2, 0
+ addw $r2 = $r2, 1
+;;
+ zxwd $r3 = $r3
+;;
+ slld $r5 = $r3, 3
+ addd $r3 = $r2, 0
+;;
+ addd $r10 = $r0, $r5
+ zxwd $r3 = $r3
+;;
+ slld $r8 = $r3, 3
+;;
+ addd $r3 = $r0, $r8
+;;
+ ld $r4 = 0[$r10]
+;;
+ ld $r9 = 0[$r3]
+;;
+ compd.leu $r32 = $r4, $r9
+;;
+ cb.wnez $r32?.L110
+;;
+ make $r0, 0
+ goto .L112
+;;
+.L111:
+ make $r0, 1
+;;
+.L112:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_vec_is_sorted, @function
+ .size data_vec_is_sorted, . - data_vec_is_sorted
diff --git a/test/monniaux/heapsort/heapsort.ccomp.k1c.s.orig b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.orig
new file mode 100644
index 00000000..0d7d5c0b
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort.ccomp.k1c.s.orig
@@ -0,0 +1,358 @@
+# File generated by CompCert 3.4
+# Command line: -Wall -O3 -S heapsort.c -o heapsort.ccomp.kvx.s
+ .text
+ .balign 2
+downheap:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+.L100:
+ sllw $r3 = $r2, 1
+ addd $r4 = $r2, 0
+;;
+ addw $r5 = $r3, 1
+ addw $r3 = $r3, 2
+;;
+ compw.ge $r32 = $r5, $r1
+;;
+ cb.wnez $r32?.L101
+;;
+ sxwd $r11 = $r5
+ sxwd $r43 = $r4
+;;
+ slld $r38 = $r11, 3
+ slld $r37 = $r43, 3
+;;
+ addd $r8 = $r0, $r38
+ addd $r40 = $r0, $r37
+;;
+ ld $r7 = 0[$r8]
+;;
+ ld $r35 = 0[$r40]
+;;
+ compd.leu $r32 = $r7, $r35
+;;
+ cb.wnez $r32?.L101
+;;
+ addd $r4 = $r5, 0
+;;
+.L101:
+ compw.ge $r32 = $r3, $r1
+;;
+ cb.wnez $r32?.L102
+;;
+ sxwd $r10 = $r3
+ sxwd $r34 = $r4
+;;
+ slld $r17 = $r10, 3
+ slld $r9 = $r34, 3
+;;
+ addd $r36 = $r0, $r17
+ addd $r47 = $r0, $r9
+;;
+ ld $r45 = 0[$r36]
+;;
+ ld $r44 = 0[$r47]
+;;
+ compd.leu $r32 = $r45, $r44
+;;
+ cb.wnez $r32?.L102
+;;
+ addd $r4 = $r3, 0
+;;
+.L102:
+ compw.eq $r32 = $r4, $r2
+;;
+ cb.wnez $r32?.L103
+;;
+ sxwd $r46 = $r2
+ sxwd $r6 = $r4
+ addd $r2 = $r4, 0
+;;
+ slld $r41 = $r46, 3
+ slld $r39 = $r6, 3
+;;
+ addd $r5 = $r0, $r41
+ addd $r3 = $r0, $r39
+;;
+ ld $r33 = 0[$r3]
+;;
+ ld $r42 = 0[$r5]
+;;
+ sd 0[$r5] = $r33
+;;
+ sd 0[$r3] = $r42
+ goto .L100
+;;
+.L103:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type downheap, @function
+ .size downheap, . - downheap
+ .text
+ .balign 2
+ .globl heapsort
+heapsort:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r1, 0
+;;
+ addw $r6 = $r19, 4294967294
+;;
+ sraw $r32 = $r6, 31
+;;
+ sd 32[$r12] = $r20
+ addd $r20 = $r0, 0
+ srlw $r32 = $r32, 31
+;;
+ addw $r32 = $r6, $r32
+;;
+ sraw $r18 = $r32, 1
+;;
+.L104:
+ cb.wltz $r18?.L105
+;;
+ addd $r2 = $r18, 0
+ addd $r1 = $r19, 0
+ addd $r0 = $r20, 0
+ call downheap
+;;
+ addw $r18 = $r18, 4294967295
+ goto .L104
+;;
+.L105:
+ make $r18, 0
+;;
+.L106:
+ compw.ge $r32 = $r18, $r19
+;;
+ cb.wnez $r32?.L107
+;;
+ sbfw $r3 = $r18, $r19
+ ld $r4 = 0[$r20]
+;;
+ addw $r1 = $r3, 4294967295
+;;
+ sxwd $r7 = $r1
+;;
+ slld $r2 = $r7, 3
+;;
+ addd $r0 = $r20, $r2
+ make $r2, 0
+;;
+ ld $r9 = 0[$r0]
+;;
+ sd 0[$r0] = $r4
+ addd $r0 = $r20, 0
+;;
+ sd 0[$r20] = $r9
+ call downheap
+;;
+ addw $r18 = $r18, 1
+ goto .L106
+;;
+.L107:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type heapsort, @function
+ .size heapsort, . - heapsort
+ .data
+ .balign 8
+next:
+ .long 0x4f091c37, 0x0
+ .type next, @object
+ .size next, . - next
+ .text
+ .balign 2
+ .globl data_random
+data_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r32 = next
+ make $r0, 1103515249
+;;
+ ld $r1 = 0[$r32]
+ make $r32 = next
+;;
+ muld $r63 = $r1, $r0
+;;
+ addd $r0 = $r63, 12345
+;;
+ sd 0[$r32] = $r0
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_random, @function
+ .size data_random, . - data_random
+ .text
+ .balign 2
+ .globl data_vec_random
+data_vec_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+ make $r20, 0
+;;
+.L108:
+ compw.geu $r32 = $r20, $r18
+;;
+ cb.wnez $r32?.L109
+;;
+ call data_random
+;;
+ addd $r1 = $r20, 0
+ addw $r20 = $r20, 1
+;;
+ zxwd $r1 = $r1
+;;
+ slld $r2 = $r1, 3
+;;
+ addd $r3 = $r19, $r2
+;;
+ sd 0[$r3] = $r0
+ goto .L108
+;;
+.L109:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type data_vec_random, @function
+ .size data_vec_random, . - data_vec_random
+ .text
+ .balign 2
+ .globl data_vec_is_sorted
+data_vec_is_sorted:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r2, 0
+;;
+.L110:
+ addw $r6 = $r1, 4294967295
+;;
+ compw.geu $r32 = $r2, $r6
+;;
+ cb.wnez $r32?.L111
+;;
+ addd $r3 = $r2, 0
+ addw $r2 = $r2, 1
+;;
+ zxwd $r3 = $r3
+;;
+ slld $r5 = $r3, 3
+ addd $r3 = $r2, 0
+;;
+ addd $r10 = $r0, $r5
+ zxwd $r3 = $r3
+;;
+ slld $r8 = $r3, 3
+;;
+ addd $r3 = $r0, $r8
+;;
+ ld $r4 = 0[$r10]
+;;
+ ld $r9 = 0[$r3]
+;;
+ compd.leu $r32 = $r4, $r9
+;;
+ cb.wnez $r32?.L110
+;;
+ make $r0, 0
+ goto .L112
+;;
+.L111:
+ make $r0, 1
+;;
+.L112:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_vec_is_sorted, @function
+ .size data_vec_is_sorted, . - data_vec_is_sorted
diff --git a/test/monniaux/heapsort/heapsort.h b/test/monniaux/heapsort/heapsort.h
new file mode 100644
index 00000000..247d6773
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort.h
@@ -0,0 +1,7 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef uint64_t data;
+void heapsort(data *A, int len);
+void data_vec_random(data *a, unsigned len);
+bool data_vec_is_sorted(const data *a, unsigned len);
diff --git a/test/monniaux/heapsort/heapsort_run.c b/test/monniaux/heapsort/heapsort_run.c
new file mode 100644
index 00000000..8f2d3fe0
--- /dev/null
+++ b/test/monniaux/heapsort/heapsort_run.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "heapsort.h"
+#include "../cycles.h"
+
+int main (void) {
+ cycle_count_config();
+ unsigned len=30000;
+ data *vec = malloc(sizeof(data) * len);
+ data_vec_random(vec, len);
+ cycle_t heapsort_time = get_cycle();
+ heapsort(vec, len);
+ heapsort_time = get_cycle() - heapsort_time;
+ printf("sorted=%s\n"
+ "time cycles:%" PRIu64 "\n",
+ data_vec_is_sorted(vec, len)?"true":"false",
+ heapsort_time);
+ free(vec);
+ return 0;
+}
+
diff --git a/test/monniaux/heptagon_radio_transmitter/Makefile b/test/monniaux/heptagon_radio_transmitter/Makefile
new file mode 100644
index 00000000..75420a10
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/Makefile
@@ -0,0 +1,3 @@
+TARGET=radiotrans
+
+include ../rules.mk
diff --git a/test/monniaux/heptagon_radio_transmitter/_main.c b/test/monniaux/heptagon_radio_transmitter/_main.c
new file mode 100644
index 00000000..635d660f
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/_main.c
@@ -0,0 +1,74 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "_main.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+static inline int get_bool(void) {
+ return dm_random_uint32() & 1;
+}
+
+Radiotrans__main_mem mem;
+int main(int argc, char** argv) {
+ int step_c;
+ int step_max;
+ int enter_tx;
+ int enter_rx;
+ int exit_rx;
+ int calibrate;
+ int sleep;
+ int wake_up;
+ int irq_tx_done;
+ int irq_on_packet;
+ int irq_end_of_packet;
+ int irq_end_of_calibration;
+ int irq_fifo_threshold;
+ int adc_on;
+ int adc_off;
+ Radiotrans__main_out _res;
+ step_c = 0;
+ step_max = 1000;
+ if ((argc==2)) {
+ step_max = atoi(argv[1]);
+ };
+
+ clock_prepare();
+ clock_start();
+ Radiotrans__main_reset(&mem);
+ while ((!(step_max)||(step_c<step_max))) {
+ step_c = (step_c+1);
+
+ enter_tx = get_bool();
+ enter_rx = get_bool();
+ exit_rx = get_bool();
+ calibrate = get_bool();
+ sleep = get_bool();
+ wake_up = get_bool();
+ irq_tx_done = get_bool();
+ irq_on_packet = get_bool();
+ irq_end_of_packet = get_bool();
+ irq_end_of_calibration = get_bool();
+ irq_fifo_threshold = get_bool();
+ adc_on = get_bool();
+ adc_off = get_bool();
+
+ Radiotrans__main_step(enter_tx, enter_rx, exit_rx, calibrate, sleep,
+ wake_up, irq_tx_done, irq_on_packet,
+ irq_end_of_packet, irq_end_of_calibration,
+ irq_fifo_threshold, adc_on, adc_off, &_res, &mem);
+#if 0
+ printf("%d\n", _res.a_on);
+ printf("%d\n", _res.red);
+ fflush(stdout);
+#endif
+ };
+ clock_stop();
+ print_total_clock();
+ return 0;
+}
+
diff --git a/test/monniaux/heptagon_radio_transmitter/_main.h b/test/monniaux/heptagon_radio_transmitter/_main.h
new file mode 100644
index 00000000..65abec74
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/_main.h
@@ -0,0 +1,9 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#ifndef _MAIN_H
+#define _MAIN_H
+
+#include "radiotrans.h"
+#endif // _MAIN_H
diff --git a/test/monniaux/heptagon_radio_transmitter/pervasives.h b/test/monniaux/heptagon_radio_transmitter/pervasives.h
new file mode 100644
index 00000000..f6d197e4
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/pervasives.h
@@ -0,0 +1,45 @@
+/***********************************************************************/
+/* */
+/* Heptagon */
+/* */
+/* Gwenael Delaval, LIG/INRIA, UJF */
+/* Leonard Gerard, Parkas, ENS */
+/* Adrien Guatto, Parkas, ENS */
+/* Cedric Pasteur, Parkas, ENS */
+/* Marc Pouzet, Parkas, ENS */
+/* */
+/* Copyright 2012 ENS, INRIA, UJF */
+/* */
+/* This file is part of the Heptagon compiler. */
+/* */
+/* Heptagon is free software: you can redistribute it and/or modify it */
+/* under the terms of the GNU General Public License as published by */
+/* the Free Software Foundation, either version 3 of the License, or */
+/* (at your option) any later version. */
+/* */
+/* Heptagon is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
+/* GNU General Public License for more details. */
+/* */
+/* You should have received a copy of the GNU General Public License */
+/* along with Heptagon. If not, see <http://www.gnu.org/licenses/> */
+/* */
+/***********************************************************************/
+
+/* Pervasives module for the Heptagon compiler */
+
+#ifndef DECADES_PERVASIVES_H
+#define DECADES_PERVASIVES_H
+
+typedef float real;
+
+/* between(i, n) returns idx between 0 and n-1. */
+static inline int between(int idx, int n)
+{
+ int o = (idx >= n) ? n-1 : (idx < 0 ? 0 : idx);
+ return o;
+}
+
+#endif
+
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans.c b/test/monniaux/heptagon_radio_transmitter/radiotrans.c
new file mode 100644
index 00000000..30d4f62d
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans.c
@@ -0,0 +1,571 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "radiotrans.h"
+
+void Radiotrans__transceiver_reset(Radiotrans__transceiver_mem* self) {
+ self->ck = Radiotrans__St_Idle;
+ self->pnr = false;
+}
+
+void Radiotrans__transceiver_step(int enter_tx, int enter_rx, int exit_rx,
+ int calibrate, int sleep, int wake_up,
+ int irq_tx_done, int irq_on_packet,
+ int irq_end_of_packet,
+ int irq_end_of_calibration,
+ int irq_fifo_threshold, int ok,
+ Radiotrans__transceiver_out* _out,
+ Radiotrans__transceiver_mem* self) {
+
+ int v_2;
+ Radiotrans__st v_1;
+ int v;
+ int v_3;
+ int v_13;
+ Radiotrans__st v_12;
+ int v_11;
+ Radiotrans__st v_10;
+ int v_9;
+ Radiotrans__st v_8;
+ int v_7;
+ int v_6;
+ int v_5;
+ int v_4;
+ int nr_St_Rx_Packet;
+ Radiotrans__st ns_St_Rx_Packet;
+ int red_St_Rx_Packet;
+ int nr_St_Rx;
+ Radiotrans__st ns_St_Rx;
+ int red_St_Rx;
+ int nr_St_Sleep;
+ Radiotrans__st ns_St_Sleep;
+ int red_St_Sleep;
+ int nr_St_Calibrate;
+ Radiotrans__st ns_St_Calibrate;
+ int red_St_Calibrate;
+ int nr_St_Tx;
+ Radiotrans__st ns_St_Tx;
+ int red_St_Tx;
+ int nr_St_Idle;
+ Radiotrans__st ns_St_Idle;
+ int red_St_Idle;
+ Radiotrans__st ns;
+ int r;
+ int nr;
+ r = self->pnr;
+ switch (self->ck) {
+ case Radiotrans__St_Rx_Packet:
+ if (irq_end_of_packet) {
+ ns_St_Rx_Packet = Radiotrans__St_Idle;
+ nr_St_Rx_Packet = true;
+ } else {
+ ns_St_Rx_Packet = Radiotrans__St_Rx_Packet;
+ nr_St_Rx_Packet = false;
+ };
+ red_St_Rx_Packet = true;
+ ns = ns_St_Rx_Packet;
+ nr = nr_St_Rx_Packet;
+ _out->red = red_St_Rx_Packet;
+ break;
+ case Radiotrans__St_Rx:
+ v = (exit_rx&&ok);
+ if (v) {
+ v_1 = Radiotrans__St_Idle;
+ } else {
+ v_1 = Radiotrans__St_Rx;
+ };
+ if (irq_on_packet) {
+ ns_St_Rx = Radiotrans__St_Rx_Packet;
+ } else {
+ ns_St_Rx = v_1;
+ };
+ if (v) {
+ v_2 = true;
+ } else {
+ v_2 = false;
+ };
+ if (irq_on_packet) {
+ nr_St_Rx = true;
+ } else {
+ nr_St_Rx = v_2;
+ };
+ red_St_Rx = true;
+ ns = ns_St_Rx;
+ nr = nr_St_Rx;
+ _out->red = red_St_Rx;
+ break;
+ case Radiotrans__St_Sleep:
+ v_3 = (wake_up&&ok);
+ if (v_3) {
+ ns_St_Sleep = Radiotrans__St_Idle;
+ nr_St_Sleep = true;
+ } else {
+ ns_St_Sleep = Radiotrans__St_Sleep;
+ nr_St_Sleep = false;
+ };
+ red_St_Sleep = false;
+ ns = ns_St_Sleep;
+ nr = nr_St_Sleep;
+ _out->red = red_St_Sleep;
+ break;
+ case Radiotrans__St_Calibrate:
+ if (irq_end_of_calibration) {
+ ns_St_Calibrate = Radiotrans__St_Idle;
+ nr_St_Calibrate = true;
+ } else {
+ ns_St_Calibrate = Radiotrans__St_Calibrate;
+ nr_St_Calibrate = false;
+ };
+ red_St_Calibrate = false;
+ ns = ns_St_Calibrate;
+ nr = nr_St_Calibrate;
+ _out->red = red_St_Calibrate;
+ break;
+ case Radiotrans__St_Tx:
+ if (irq_tx_done) {
+ ns_St_Tx = Radiotrans__St_Idle;
+ nr_St_Tx = true;
+ } else {
+ ns_St_Tx = Radiotrans__St_Tx;
+ nr_St_Tx = false;
+ };
+ red_St_Tx = true;
+ ns = ns_St_Tx;
+ nr = nr_St_Tx;
+ _out->red = red_St_Tx;
+ break;
+ case Radiotrans__St_Idle:
+ v_4 = (enter_tx&&ok);
+ v_5 = (calibrate&&ok);
+ v_6 = (sleep&&ok);
+ v_7 = (enter_rx&&ok);
+ if (v_7) {
+ v_8 = Radiotrans__St_Rx;
+ } else {
+ v_8 = Radiotrans__St_Idle;
+ };
+ if (v_6) {
+ v_10 = Radiotrans__St_Sleep;
+ } else {
+ v_10 = v_8;
+ };
+ if (v_5) {
+ v_12 = Radiotrans__St_Calibrate;
+ } else {
+ v_12 = v_10;
+ };
+ if (v_4) {
+ ns_St_Idle = Radiotrans__St_Tx;
+ } else {
+ ns_St_Idle = v_12;
+ };
+ ns = ns_St_Idle;
+ if (v_7) {
+ v_9 = true;
+ } else {
+ v_9 = false;
+ };
+ if (v_6) {
+ v_11 = true;
+ } else {
+ v_11 = v_9;
+ };
+ if (v_5) {
+ v_13 = true;
+ } else {
+ v_13 = v_11;
+ };
+ if (v_4) {
+ nr_St_Idle = true;
+ } else {
+ nr_St_Idle = v_13;
+ };
+ nr = nr_St_Idle;
+ red_St_Idle = false;
+ _out->red = red_St_Idle;
+ break;
+ default:
+ break;
+ };
+ self->ck = ns;
+ self->pnr = nr;;
+}
+
+void Radiotrans__adc_reset(Radiotrans__adc_mem* self) {
+ self->ck = Radiotrans__St_1_Off;
+ self->pnr = false;
+}
+
+void Radiotrans__adc_step(int adc_on, int adc_off, int ok,
+ Radiotrans__adc_out* _out,
+ Radiotrans__adc_mem* self) {
+
+ int v;
+ int v_14;
+ int nr_St_1_On;
+ Radiotrans__st_1 ns_St_1_On;
+ int o_St_1_On;
+ int nr_St_1_Off;
+ Radiotrans__st_1 ns_St_1_Off;
+ int o_St_1_Off;
+ Radiotrans__st_1 ns;
+ int r;
+ int nr;
+ r = self->pnr;
+ switch (self->ck) {
+ case Radiotrans__St_1_On:
+ v = (adc_off&&ok);
+ if (v) {
+ ns_St_1_On = Radiotrans__St_1_Off;
+ nr_St_1_On = true;
+ } else {
+ ns_St_1_On = Radiotrans__St_1_On;
+ nr_St_1_On = false;
+ };
+ o_St_1_On = true;
+ ns = ns_St_1_On;
+ nr = nr_St_1_On;
+ _out->o = o_St_1_On;
+ break;
+ case Radiotrans__St_1_Off:
+ v_14 = (adc_on&&ok);
+ if (v_14) {
+ ns_St_1_Off = Radiotrans__St_1_On;
+ } else {
+ ns_St_1_Off = Radiotrans__St_1_Off;
+ };
+ ns = ns_St_1_Off;
+ if (v_14) {
+ nr_St_1_Off = true;
+ } else {
+ nr_St_1_Off = false;
+ };
+ nr = nr_St_1_Off;
+ o_St_1_Off = false;
+ _out->o = o_St_1_Off;
+ break;
+ default:
+ break;
+ };
+ self->ck = ns;
+ self->pnr = nr;;
+}
+
+void Radiotrans__main_reset(Radiotrans__main_mem* self) {
+ self->ck = Radiotrans__St_3_Idle;
+ self->pnr_1 = false;
+ self->ck_1 = Radiotrans__St_2_Off;
+ self->pnr = false;
+}
+
+void Radiotrans__main_step(int enter_tx, int enter_rx, int exit_rx,
+ int calibrate, int sleep, int wake_up,
+ int irq_tx_done, int irq_on_packet,
+ int irq_end_of_packet, int irq_end_of_calibration,
+ int irq_fifo_threshold, int adc_on, int adc_off,
+ Radiotrans__main_out* _out,
+ Radiotrans__main_mem* self) {
+ Radiotrans_controller__main_ctrlr0_out Radiotrans_controller__main_ctrlr0_out_st;
+
+ int v_15;
+ int v;
+ int ok_r;
+ int ok_a;
+ int v_18;
+ Radiotrans__st_3 v_17;
+ int v_16;
+ int v_19;
+ int v_29;
+ Radiotrans__st_3 v_28;
+ int v_27;
+ Radiotrans__st_3 v_26;
+ int v_25;
+ Radiotrans__st_3 v_24;
+ int v_23;
+ int v_22;
+ int v_21;
+ int v_20;
+ int nr_1_St_3_Rx_Packet;
+ Radiotrans__st_3 ns_1_St_3_Rx_Packet;
+ int red_1_St_3_Rx_Packet;
+ int nr_1_St_3_Rx;
+ Radiotrans__st_3 ns_1_St_3_Rx;
+ int red_1_St_3_Rx;
+ int nr_1_St_3_Sleep;
+ Radiotrans__st_3 ns_1_St_3_Sleep;
+ int red_1_St_3_Sleep;
+ int nr_1_St_3_Calibrate;
+ Radiotrans__st_3 ns_1_St_3_Calibrate;
+ int red_1_St_3_Calibrate;
+ int nr_1_St_3_Tx;
+ Radiotrans__st_3 ns_1_St_3_Tx;
+ int red_1_St_3_Tx;
+ int nr_1_St_3_Idle;
+ Radiotrans__st_3 ns_1_St_3_Idle;
+ int red_1_St_3_Idle;
+ int v_30;
+ int v_31;
+ int nr_St_2_On;
+ Radiotrans__st_2 ns_St_2_On;
+ int o_St_2_On;
+ int nr_St_2_Off;
+ Radiotrans__st_2 ns_St_2_Off;
+ int o_St_2_Off;
+ Radiotrans__st_3 ns_1;
+ int r_1;
+ int nr_1;
+ Radiotrans__st_2 ns;
+ int r;
+ int nr;
+ int adc_on_1;
+ int adc_off_1;
+ int ok_1;
+ int o;
+ int enter_tx_1;
+ int enter_rx_1;
+ int exit_rx_1;
+ int calibrate_1;
+ int sleep_1;
+ int wake_up_1;
+ int irq_tx_done_1;
+ int irq_on_packet_1;
+ int irq_end_of_packet_1;
+ int irq_end_of_calibration_1;
+ int irq_fifo_threshold_1;
+ int ok;
+ int red_1;
+ r_1 = self->pnr_1;
+ irq_fifo_threshold_1 = irq_fifo_threshold;
+ irq_end_of_calibration_1 = irq_end_of_calibration;
+ irq_end_of_packet_1 = irq_end_of_packet;
+ irq_on_packet_1 = irq_on_packet;
+ irq_tx_done_1 = irq_tx_done;
+ wake_up_1 = wake_up;
+ sleep_1 = sleep;
+ calibrate_1 = calibrate;
+ exit_rx_1 = exit_rx;
+ enter_rx_1 = enter_rx;
+ enter_tx_1 = enter_tx;
+ r = self->pnr;
+ adc_off_1 = adc_off;
+ adc_on_1 = adc_on;
+ switch (self->ck) {
+ case Radiotrans__St_3_Rx_Packet:
+ if (irq_end_of_packet_1) {
+ ns_1_St_3_Rx_Packet = Radiotrans__St_3_Idle;
+ nr_1_St_3_Rx_Packet = true;
+ } else {
+ ns_1_St_3_Rx_Packet = Radiotrans__St_3_Rx_Packet;
+ nr_1_St_3_Rx_Packet = false;
+ };
+ red_1_St_3_Rx_Packet = true;
+ red_1 = red_1_St_3_Rx_Packet;
+ break;
+ case Radiotrans__St_3_Rx:
+ red_1_St_3_Rx = true;
+ red_1 = red_1_St_3_Rx;
+ break;
+ case Radiotrans__St_3_Sleep:
+ red_1_St_3_Sleep = false;
+ red_1 = red_1_St_3_Sleep;
+ break;
+ case Radiotrans__St_3_Calibrate:
+ if (irq_end_of_calibration_1) {
+ ns_1_St_3_Calibrate = Radiotrans__St_3_Idle;
+ nr_1_St_3_Calibrate = true;
+ } else {
+ ns_1_St_3_Calibrate = Radiotrans__St_3_Calibrate;
+ nr_1_St_3_Calibrate = false;
+ };
+ red_1_St_3_Calibrate = false;
+ red_1 = red_1_St_3_Calibrate;
+ break;
+ case Radiotrans__St_3_Tx:
+ if (irq_tx_done_1) {
+ ns_1_St_3_Tx = Radiotrans__St_3_Idle;
+ nr_1_St_3_Tx = true;
+ } else {
+ ns_1_St_3_Tx = Radiotrans__St_3_Tx;
+ nr_1_St_3_Tx = false;
+ };
+ red_1_St_3_Tx = true;
+ red_1 = red_1_St_3_Tx;
+ break;
+ case Radiotrans__St_3_Idle:
+ red_1_St_3_Idle = false;
+ red_1 = red_1_St_3_Idle;
+ break;
+ default:
+ break;
+ };
+ _out->red = red_1;
+ switch (self->ck_1) {
+ case Radiotrans__St_2_On:
+ o_St_2_On = true;
+ o = o_St_2_On;
+ break;
+ case Radiotrans__St_2_Off:
+ o_St_2_Off = false;
+ o = o_St_2_Off;
+ break;
+ default:
+ break;
+ };
+ _out->a_on = o;
+ Radiotrans_controller__main_ctrlr0_step(adc_off, adc_on, calibrate,
+ self->ck, self->ck_1, enter_rx,
+ enter_tx, exit_rx,
+ irq_end_of_calibration,
+ irq_end_of_packet,
+ irq_fifo_threshold, irq_on_packet,
+ irq_tx_done, self->pnr,
+ self->pnr_1, sleep, wake_up,
+ &Radiotrans_controller__main_ctrlr0_out_st);
+ ok_a = Radiotrans_controller__main_ctrlr0_out_st.ok_a;
+ ok_r = Radiotrans_controller__main_ctrlr0_out_st.ok_r;
+ ok = ok_r;
+ ok_1 = ok_a;
+ switch (self->ck) {
+ case Radiotrans__St_3_Rx:
+ v_16 = (exit_rx_1&&ok);
+ if (v_16) {
+ v_17 = Radiotrans__St_3_Idle;
+ } else {
+ v_17 = Radiotrans__St_3_Rx;
+ };
+ if (irq_on_packet_1) {
+ ns_1_St_3_Rx = Radiotrans__St_3_Rx_Packet;
+ } else {
+ ns_1_St_3_Rx = v_17;
+ };
+ if (v_16) {
+ v_18 = true;
+ } else {
+ v_18 = false;
+ };
+ if (irq_on_packet_1) {
+ nr_1_St_3_Rx = true;
+ } else {
+ nr_1_St_3_Rx = v_18;
+ };
+ ns_1 = ns_1_St_3_Rx;
+ nr_1 = nr_1_St_3_Rx;
+ break;
+ case Radiotrans__St_3_Sleep:
+ v_19 = (wake_up_1&&ok);
+ if (v_19) {
+ ns_1_St_3_Sleep = Radiotrans__St_3_Idle;
+ nr_1_St_3_Sleep = true;
+ } else {
+ ns_1_St_3_Sleep = Radiotrans__St_3_Sleep;
+ nr_1_St_3_Sleep = false;
+ };
+ ns_1 = ns_1_St_3_Sleep;
+ nr_1 = nr_1_St_3_Sleep;
+ break;
+ case Radiotrans__St_3_Idle:
+ v_20 = (enter_tx_1&&ok);
+ v_21 = (calibrate_1&&ok);
+ v_22 = (sleep_1&&ok);
+ v_23 = (enter_rx_1&&ok);
+ if (v_23) {
+ v_24 = Radiotrans__St_3_Rx;
+ } else {
+ v_24 = Radiotrans__St_3_Idle;
+ };
+ if (v_22) {
+ v_26 = Radiotrans__St_3_Sleep;
+ } else {
+ v_26 = v_24;
+ };
+ if (v_21) {
+ v_28 = Radiotrans__St_3_Calibrate;
+ } else {
+ v_28 = v_26;
+ };
+ if (v_20) {
+ ns_1_St_3_Idle = Radiotrans__St_3_Tx;
+ } else {
+ ns_1_St_3_Idle = v_28;
+ };
+ ns_1 = ns_1_St_3_Idle;
+ if (v_23) {
+ v_25 = true;
+ } else {
+ v_25 = false;
+ };
+ if (v_22) {
+ v_27 = true;
+ } else {
+ v_27 = v_25;
+ };
+ if (v_21) {
+ v_29 = true;
+ } else {
+ v_29 = v_27;
+ };
+ if (v_20) {
+ nr_1_St_3_Idle = true;
+ } else {
+ nr_1_St_3_Idle = v_29;
+ };
+ nr_1 = nr_1_St_3_Idle;
+ break;
+ case Radiotrans__St_3_Rx_Packet:
+ ns_1 = ns_1_St_3_Rx_Packet;
+ nr_1 = nr_1_St_3_Rx_Packet;
+ break;
+ case Radiotrans__St_3_Calibrate:
+ ns_1 = ns_1_St_3_Calibrate;
+ nr_1 = nr_1_St_3_Calibrate;
+ break;
+ case Radiotrans__St_3_Tx:
+ ns_1 = ns_1_St_3_Tx;
+ nr_1 = nr_1_St_3_Tx;
+ break;
+ default:
+ break;
+ };
+ switch (self->ck_1) {
+ case Radiotrans__St_2_On:
+ v_30 = (adc_off_1&&ok_1);
+ if (v_30) {
+ ns_St_2_On = Radiotrans__St_2_Off;
+ nr_St_2_On = true;
+ } else {
+ ns_St_2_On = Radiotrans__St_2_On;
+ nr_St_2_On = false;
+ };
+ ns = ns_St_2_On;
+ nr = nr_St_2_On;
+ break;
+ case Radiotrans__St_2_Off:
+ v_31 = (adc_on_1&&ok_1);
+ if (v_31) {
+ ns_St_2_Off = Radiotrans__St_2_On;
+ } else {
+ ns_St_2_Off = Radiotrans__St_2_Off;
+ };
+ ns = ns_St_2_Off;
+ if (v_31) {
+ nr_St_2_Off = true;
+ } else {
+ nr_St_2_Off = false;
+ };
+ nr = nr_St_2_Off;
+ break;
+ default:
+ break;
+ };
+ self->ck = ns_1;
+ self->pnr_1 = nr_1;
+ self->ck_1 = ns;
+ self->pnr = nr;
+ v = (_out->a_on&&_out->red);
+ v_15 = !(v);;
+}
+
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans.h b/test/monniaux/heptagon_radio_transmitter/radiotrans.h
new file mode 100644
index 00000000..1d68ad5c
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans.h
@@ -0,0 +1,67 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#ifndef RADIOTRANS_H
+#define RADIOTRANS_H
+
+#include "radiotrans_types.h"
+#include "radiotrans_controller.h"
+typedef struct Radiotrans__transceiver_mem {
+ Radiotrans__st ck;
+ int pnr;
+} Radiotrans__transceiver_mem;
+
+typedef struct Radiotrans__transceiver_out {
+ int red;
+} Radiotrans__transceiver_out;
+
+void Radiotrans__transceiver_reset(Radiotrans__transceiver_mem* self);
+
+void Radiotrans__transceiver_step(int enter_tx, int enter_rx, int exit_rx,
+ int calibrate, int sleep, int wake_up,
+ int irq_tx_done, int irq_on_packet,
+ int irq_end_of_packet,
+ int irq_end_of_calibration,
+ int irq_fifo_threshold, int ok,
+ Radiotrans__transceiver_out* _out,
+ Radiotrans__transceiver_mem* self);
+
+typedef struct Radiotrans__adc_mem {
+ Radiotrans__st_1 ck;
+ int pnr;
+} Radiotrans__adc_mem;
+
+typedef struct Radiotrans__adc_out {
+ int o;
+} Radiotrans__adc_out;
+
+void Radiotrans__adc_reset(Radiotrans__adc_mem* self);
+
+void Radiotrans__adc_step(int adc_on, int adc_off, int ok,
+ Radiotrans__adc_out* _out,
+ Radiotrans__adc_mem* self);
+
+typedef struct Radiotrans__main_mem {
+ Radiotrans__st_3 ck;
+ Radiotrans__st_2 ck_1;
+ int pnr_1;
+ int pnr;
+} Radiotrans__main_mem;
+
+typedef struct Radiotrans__main_out {
+ int a_on;
+ int red;
+} Radiotrans__main_out;
+
+void Radiotrans__main_reset(Radiotrans__main_mem* self);
+
+void Radiotrans__main_step(int enter_tx, int enter_rx, int exit_rx,
+ int calibrate, int sleep, int wake_up,
+ int irq_tx_done, int irq_on_packet,
+ int irq_end_of_packet, int irq_end_of_calibration,
+ int irq_fifo_threshold, int adc_on, int adc_off,
+ Radiotrans__main_out* _out,
+ Radiotrans__main_mem* self);
+
+#endif // RADIOTRANS_H
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.c b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.c
new file mode 100644
index 00000000..8cc6d512
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.c
@@ -0,0 +1,160 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c radiotrans_controller.ept --- */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "radiotrans_controller.h"
+
+void Radiotrans_controller__main_ctrlr0_step(int adc_off, int adc_on,
+ int calibrate,
+ Radiotrans__st_3 ck,
+ Radiotrans__st_2 ck_1,
+ int enter_rx, int enter_tx,
+ int exit_rx,
+ int irq_end_of_calibration,
+ int irq_end_of_packet,
+ int irq_fifo_threshold,
+ int irq_on_packet,
+ int irq_tx_done, int pnr,
+ int pnr_1, int sleep,
+ int wake_up,
+ Radiotrans_controller__main_ctrlr0_out* _out) {
+
+ int v_22;
+ int v_21;
+ int v_20;
+ int v_19;
+ int v_18;
+ int v_17;
+ int v_16;
+ int v_15;
+ int v_14;
+ int v_13;
+ int v_12;
+ int v_11;
+ int v_10;
+ int v_9;
+ int v_8;
+ int v_7;
+ int v_6;
+ int v_5;
+ int v_4;
+ int v_3;
+ int v_2;
+ int v_1;
+ int v;
+ int l52;
+ int l51;
+ int l50;
+ int l49;
+ int l48;
+ int l47;
+ int l46;
+ int l45;
+ int l44;
+ int l43;
+ int l42;
+ int l41;
+ int l40;
+ int l39;
+ int l38;
+ int l37;
+ int l36;
+ int l35;
+ int l34;
+ int l33;
+ int l32;
+ v_22 = (ck==Radiotrans__St_3_Rx_Packet);
+ v_21 = !(adc_on);
+ l51 = (v_21||irq_end_of_packet);
+ v_20 = (ck==Radiotrans__St_3_Tx);
+ v_19 = !(adc_on);
+ l49 = (v_19||irq_tx_done);
+ v_18 = (ck==Radiotrans__St_3_Rx);
+ v_17 = !(adc_on);
+ v_16 = !(adc_on);
+ v_15 = !(adc_on);
+ v_14 = !(irq_on_packet);
+ l45 = (v_14||v_15);
+ if (exit_rx) {
+ l46 = l45;
+ } else {
+ l46 = v_16;
+ };
+ v_12 = (ck==Radiotrans__St_3_Idle);
+ v_13 = !(v_12);
+ v_11 = (ck_1==Radiotrans__St_2_On);
+ v_9 = !(adc_on);
+ v_8 = !(adc_on);
+ v_7 = !(enter_tx);
+ l38 = (v_7||v_8);
+ if (sleep) {
+ l39 = l38;
+ } else {
+ l39 = v_9;
+ };
+ if (enter_rx) {
+ l40 = l39;
+ } else {
+ l40 = l38;
+ };
+ if (calibrate) {
+ l41 = l38;
+ } else {
+ l41 = l40;
+ };
+ v_5 = (ck==Radiotrans__St_3_Rx);
+ v_3 = (ck==Radiotrans__St_3_Calibrate);
+ v_2 = (ck==Radiotrans__St_3_Sleep);
+ v_4 = (v_2||v_3);
+ v_6 = (v_4||v_5);
+ v_1 = (ck_1==Radiotrans__St_2_Off);
+ v = !(enter_tx);
+ l32 = (v||adc_off);
+ if (sleep) {
+ l33 = l32;
+ } else {
+ l33 = adc_off;
+ };
+ if (enter_rx) {
+ l34 = l33;
+ } else {
+ l34 = l32;
+ };
+ if (calibrate) {
+ l35 = l32;
+ } else {
+ l35 = l34;
+ };
+ l36 = (v_1||l35);
+ l37 = (v_6||l36);
+ _out->ok_r = l37;
+ if (_out->ok_r) {
+ l47 = l46;
+ } else {
+ l47 = v_17;
+ };
+ v_10 = !(_out->ok_r);
+ l42 = (v_10||l41);
+ l43 = (v_11||l42);
+ l44 = (v_13||l43);
+ if (v_18) {
+ l48 = l47;
+ } else {
+ l48 = l44;
+ };
+ if (v_20) {
+ l50 = l49;
+ } else {
+ l50 = l48;
+ };
+ if (v_22) {
+ l52 = l51;
+ } else {
+ l52 = l50;
+ };
+ _out->ok_a = l52;;
+}
+
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.h b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.h
new file mode 100644
index 00000000..45681ada
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller.h
@@ -0,0 +1,30 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c radiotrans_controller.ept --- */
+
+#ifndef RADIOTRANS_CONTROLLER_H
+#define RADIOTRANS_CONTROLLER_H
+
+#include "radiotrans_controller_types.h"
+#include "radiotrans.h"
+typedef struct Radiotrans_controller__main_ctrlr0_out {
+ int ok_a;
+ int ok_r;
+} Radiotrans_controller__main_ctrlr0_out;
+
+void Radiotrans_controller__main_ctrlr0_step(int adc_off, int adc_on,
+ int calibrate,
+ Radiotrans__st_3 ck,
+ Radiotrans__st_2 ck_1,
+ int enter_rx, int enter_tx,
+ int exit_rx,
+ int irq_end_of_calibration,
+ int irq_end_of_packet,
+ int irq_fifo_threshold,
+ int irq_on_packet,
+ int irq_tx_done, int pnr,
+ int pnr_1, int sleep,
+ int wake_up,
+ Radiotrans_controller__main_ctrlr0_out* _out);
+
+#endif // RADIOTRANS_CONTROLLER_H
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.c b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.c
new file mode 100644
index 00000000..a3c3972f
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.c
@@ -0,0 +1,9 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c radiotrans_controller.ept --- */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "radiotrans_controller_types.h"
+
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.h b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.h
new file mode 100644
index 00000000..9a081213
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_controller_types.h
@@ -0,0 +1,12 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c radiotrans_controller.ept --- */
+
+#ifndef RADIOTRANS_CONTROLLER_TYPES_H
+#define RADIOTRANS_CONTROLLER_TYPES_H
+
+#include "stdbool.h"
+#include "assert.h"
+#include "pervasives.h"
+#include "radiotrans_types.h"
+#endif // RADIOTRANS_CONTROLLER_TYPES_H
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_types.c b/test/monniaux/heptagon_radio_transmitter/radiotrans_types.c
new file mode 100644
index 00000000..cc62575d
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_types.c
@@ -0,0 +1,149 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "radiotrans_types.h"
+
+Radiotrans__st_3 Radiotrans__st_3_of_string(char* s) {
+ if ((strcmp(s, "St_3_Tx")==0)) {
+ return Radiotrans__St_3_Tx;
+ };
+ if ((strcmp(s, "St_3_Sleep")==0)) {
+ return Radiotrans__St_3_Sleep;
+ };
+ if ((strcmp(s, "St_3_Rx_Packet")==0)) {
+ return Radiotrans__St_3_Rx_Packet;
+ };
+ if ((strcmp(s, "St_3_Rx")==0)) {
+ return Radiotrans__St_3_Rx;
+ };
+ if ((strcmp(s, "St_3_Idle")==0)) {
+ return Radiotrans__St_3_Idle;
+ };
+ if ((strcmp(s, "St_3_Calibrate")==0)) {
+ return Radiotrans__St_3_Calibrate;
+ };
+}
+
+char* string_of_Radiotrans__st_3(Radiotrans__st_3 x, char* buf) {
+ switch (x) {
+ case Radiotrans__St_3_Tx:
+ strcpy(buf, "St_3_Tx");
+ break;
+ case Radiotrans__St_3_Sleep:
+ strcpy(buf, "St_3_Sleep");
+ break;
+ case Radiotrans__St_3_Rx_Packet:
+ strcpy(buf, "St_3_Rx_Packet");
+ break;
+ case Radiotrans__St_3_Rx:
+ strcpy(buf, "St_3_Rx");
+ break;
+ case Radiotrans__St_3_Idle:
+ strcpy(buf, "St_3_Idle");
+ break;
+ case Radiotrans__St_3_Calibrate:
+ strcpy(buf, "St_3_Calibrate");
+ break;
+ default:
+ break;
+ };
+ return buf;
+}
+
+Radiotrans__st_2 Radiotrans__st_2_of_string(char* s) {
+ if ((strcmp(s, "St_2_On")==0)) {
+ return Radiotrans__St_2_On;
+ };
+ if ((strcmp(s, "St_2_Off")==0)) {
+ return Radiotrans__St_2_Off;
+ };
+}
+
+char* string_of_Radiotrans__st_2(Radiotrans__st_2 x, char* buf) {
+ switch (x) {
+ case Radiotrans__St_2_On:
+ strcpy(buf, "St_2_On");
+ break;
+ case Radiotrans__St_2_Off:
+ strcpy(buf, "St_2_Off");
+ break;
+ default:
+ break;
+ };
+ return buf;
+}
+
+Radiotrans__st_1 Radiotrans__st_1_of_string(char* s) {
+ if ((strcmp(s, "St_1_On")==0)) {
+ return Radiotrans__St_1_On;
+ };
+ if ((strcmp(s, "St_1_Off")==0)) {
+ return Radiotrans__St_1_Off;
+ };
+}
+
+char* string_of_Radiotrans__st_1(Radiotrans__st_1 x, char* buf) {
+ switch (x) {
+ case Radiotrans__St_1_On:
+ strcpy(buf, "St_1_On");
+ break;
+ case Radiotrans__St_1_Off:
+ strcpy(buf, "St_1_Off");
+ break;
+ default:
+ break;
+ };
+ return buf;
+}
+
+Radiotrans__st Radiotrans__st_of_string(char* s) {
+ if ((strcmp(s, "St_Tx")==0)) {
+ return Radiotrans__St_Tx;
+ };
+ if ((strcmp(s, "St_Sleep")==0)) {
+ return Radiotrans__St_Sleep;
+ };
+ if ((strcmp(s, "St_Rx_Packet")==0)) {
+ return Radiotrans__St_Rx_Packet;
+ };
+ if ((strcmp(s, "St_Rx")==0)) {
+ return Radiotrans__St_Rx;
+ };
+ if ((strcmp(s, "St_Idle")==0)) {
+ return Radiotrans__St_Idle;
+ };
+ if ((strcmp(s, "St_Calibrate")==0)) {
+ return Radiotrans__St_Calibrate;
+ };
+}
+
+char* string_of_Radiotrans__st(Radiotrans__st x, char* buf) {
+ switch (x) {
+ case Radiotrans__St_Tx:
+ strcpy(buf, "St_Tx");
+ break;
+ case Radiotrans__St_Sleep:
+ strcpy(buf, "St_Sleep");
+ break;
+ case Radiotrans__St_Rx_Packet:
+ strcpy(buf, "St_Rx_Packet");
+ break;
+ case Radiotrans__St_Rx:
+ strcpy(buf, "St_Rx");
+ break;
+ case Radiotrans__St_Idle:
+ strcpy(buf, "St_Idle");
+ break;
+ case Radiotrans__St_Calibrate:
+ strcpy(buf, "St_Calibrate");
+ break;
+ default:
+ break;
+ };
+ return buf;
+}
+
diff --git a/test/monniaux/heptagon_radio_transmitter/radiotrans_types.h b/test/monniaux/heptagon_radio_transmitter/radiotrans_types.h
new file mode 100644
index 00000000..826ed65c
--- /dev/null
+++ b/test/monniaux/heptagon_radio_transmitter/radiotrans_types.h
@@ -0,0 +1,56 @@
+/* --- Generated the 13/5/2019 at 10:21 --- */
+/* --- heptagon compiler, version 1.05.00 (compiled mon. may. 13 10:18:8 CET 2019) --- */
+/* --- Command line: /local/STATOR/packages/opam-root/4.07.1/bin/heptc -target c -hepts -s main -target ctrln radiotrans.ept --- */
+
+#ifndef RADIOTRANS_TYPES_H
+#define RADIOTRANS_TYPES_H
+
+#include "stdbool.h"
+#include "assert.h"
+#include "pervasives.h"
+#include "radiotrans_controller_types.h"
+typedef enum {
+ Radiotrans__St_3_Tx,
+ Radiotrans__St_3_Sleep,
+ Radiotrans__St_3_Rx_Packet,
+ Radiotrans__St_3_Rx,
+ Radiotrans__St_3_Idle,
+ Radiotrans__St_3_Calibrate
+} Radiotrans__st_3;
+
+Radiotrans__st_3 Radiotrans__st_3_of_string(char* s);
+
+char* string_of_Radiotrans__st_3(Radiotrans__st_3 x, char* buf);
+
+typedef enum {
+ Radiotrans__St_2_On,
+ Radiotrans__St_2_Off
+} Radiotrans__st_2;
+
+Radiotrans__st_2 Radiotrans__st_2_of_string(char* s);
+
+char* string_of_Radiotrans__st_2(Radiotrans__st_2 x, char* buf);
+
+typedef enum {
+ Radiotrans__St_1_On,
+ Radiotrans__St_1_Off
+} Radiotrans__st_1;
+
+Radiotrans__st_1 Radiotrans__st_1_of_string(char* s);
+
+char* string_of_Radiotrans__st_1(Radiotrans__st_1 x, char* buf);
+
+typedef enum {
+ Radiotrans__St_Tx,
+ Radiotrans__St_Sleep,
+ Radiotrans__St_Rx_Packet,
+ Radiotrans__St_Rx,
+ Radiotrans__St_Idle,
+ Radiotrans__St_Calibrate
+} Radiotrans__st;
+
+Radiotrans__st Radiotrans__st_of_string(char* s);
+
+char* string_of_Radiotrans__st(Radiotrans__st x, char* buf);
+
+#endif // RADIOTRANS_TYPES_H
diff --git a/test/monniaux/idea/Makefile b/test/monniaux/idea/Makefile
new file mode 100644
index 00000000..96e1999d
--- /dev/null
+++ b/test/monniaux/idea/Makefile
@@ -0,0 +1,3 @@
+TARGET=idea
+
+include ../rules.mk
diff --git a/test/monniaux/idea/idea.c b/test/monniaux/idea/idea.c
new file mode 100644
index 00000000..884d256d
--- /dev/null
+++ b/test/monniaux/idea/idea.c
@@ -0,0 +1,406 @@
+/*
+https://github.com/bgreenlee/PassKeeper
+from */
+
+/*
+ * idea.c - C source code for IDEA block cipher.
+ * IDEA (International Data Encryption Algorithm), formerly known as
+ * IPES (Improved Proposed Encryption Standard).
+ * Algorithm developed by Xuejia Lai and James L. Massey, of ETH Zurich.
+ * This implementation modified and derived from original C code
+ * developed by Xuejia Lai.
+ * Zero-based indexing added, names changed from IPES to IDEA.
+ * CFB functions added. Random number routines added.
+ *
+ * Extensively optimized and restructured by Colin Plumb.
+ *
+ * There are two adjustments that can be made to this code to
+ * speed it up. Defaults may be used for PCs. Only the -DIDEA32
+ * pays off significantly if selectively set or not set.
+ * Experiment to see what works best for your machine.
+ *
+ * Multiplication: default is inline, -DAVOID_JUMPS uses a
+ * different version that does not do any conditional
+ * jumps (a few percent worse on a SPARC), while
+ * -DSMALL_CACHE takes it out of line to stay
+ * within a small on-chip code cache.
+ * Variables: normally, 16-bit variables are used, but some
+ * machines (notably RISCs) do not have 16-bit registers,
+ * so they do a great deal of masking. -DIDEA32 uses "int"
+ * register variables and masks explicitly only where
+ * necessary. On a SPARC, for example, this boosts
+ * performace by 30%.
+ *
+ * The IDEA(tm) block cipher is covered by patents held by ETH and a
+ * Swiss company called Ascom-Tech AG. The Swiss patent number is
+ * PCT/CH91/00117, the European patent number is EP 0 482 154 B1, and
+ * the U.S. patent number is US005214703. IDEA(tm) is a trademark of
+ * Ascom-Tech AG. There is no license fee required for noncommercial
+ * use. Commercial users may obtain licensing details from Dieter
+ * Profos, Ascom Tech AG, Solothurn Lab, Postfach 151, 4502 Solothurn,
+ * Switzerland, Tel +41 65 242885, Fax +41 65 235761.
+ *
+ * The IDEA block cipher uses a 64-bit block size, and a 128-bit key
+ * size. It breaks the 64-bit cipher block into four 16-bit words
+ * because all of the primitive inner operations are done with 16-bit
+ * arithmetic. It likewise breaks the 128-bit cipher key into eight
+ * 16-bit words.
+ *
+ * For further information on the IDEA cipher, see the book:
+ * Xuejia Lai, "On the Design and Security of Block Ciphers",
+ * ETH Series on Information Processing (ed. J.L. Massey) Vol 1,
+ * Hartung-Gorre Verlag, Konstanz, Switzerland, 1992. ISBN
+ * 3-89191-573-X.
+ *
+ * This code runs on arrays of bytes by taking pairs in big-endian
+ * order to make the 16-bit words that IDEA uses internally. This
+ * produces the same result regardless of the byte order of the
+ * native CPU.
+ */
+
+#define TEST
+#define IDEA32
+#define INT_MOD(x,y) ((x) % (y))
+#define INT_DIV(x,y) ((x) / (y))
+
+#include <string.h>
+#include <stdlib.h>
+#include "idea.h"
+#include "../clock.h"
+
+#ifdef IDEA32 /* Use >16-bit temporaries */
+#define low16(x) ((x) & 0xFFFF)
+typedef unsigned int uint16; /* at LEAST 16 bits, maybe more */
+#else
+#define low16(x) (x) /* this is only ever applied to uint16's */
+typedef word16 uint16;
+#endif
+
+#ifdef _GNUC_
+/* __const__ simply means there are no side effects for this function,
+ * which is useful info for the gcc optimizer
+ */
+#define CONST __const__
+#else
+#define CONST
+#endif
+
+/*
+ * Multiplication, modulo (2**16)+1
+ * Note that this code is structured on the assumption that
+ * untaken branches are cheaper than taken branches, and the
+ * compiler doesn't schedule branches.
+ */
+#ifdef SMALL_CACHE
+CONST static uint16
+mul(register uint16 a, register uint16 b)
+{
+ register word32 p;
+
+ p = (word32)a * b;
+ if (p) {
+ b = low16(p);
+ a = p>>16;
+ return (b - a) + (b < a);
+ } else if (a) {
+ return 1-b;
+ } else {
+ return 1-a;
+ }
+} /* mul */
+#endif /* SMALL_CACHE */
+
+/*
+ * Compute the multiplicative inverse of x, modulo 65537, using Euclid's
+ * algorithm. It is unrolled twice to avoid swapping the registers each
+ * iteration, and some subtracts of t have been changed to adds.
+ */
+CONST static uint16
+mulInv(uint16 x)
+{
+ uint16 t0, t1;
+ uint16 q, y;
+
+ if (x <= 1)
+ return x; /* 0 and 1 are self-inverse */
+ t1 = 0x10001L / x; /* Since x >= 2, this fits into 16 bits */
+ y = 0x10001L % x;
+ if (y == 1)
+ return low16(1-t1);
+ t0 = 1;
+ do {
+ q = INT_DIV(x, y);
+ x = INT_MOD(x, y);
+ t0 += q * t1;
+ if (x == 1)
+ return t0;
+ q = INT_DIV(y, x);
+ y = INT_MOD(y, x);
+ t1 += q * t0;
+ } while (y != 1);
+ return low16(1-t1);
+} /* mukInv */
+
+/*
+ * Expand a 128-bit user key to a working encryption key EK
+ */
+void
+ideaExpandKey(byte const *userkey, word16 *EK)
+{
+ int i,j;
+
+ for (j=0; j<8; j++) {
+ EK[j] = (userkey[0]<<8) + userkey[1];
+ userkey += 2;
+ }
+ for (i=0; j < IDEAKEYLEN; j++) {
+ i++;
+ EK[i+7] = (EK[i & 7] << 9) | (EK[(i+1) & 7] >> 7);
+ EK += i & 8;
+ i &= 7;
+ }
+} /* ideaExpandKey */
+
+/*
+ * Compute IDEA decryption key DK from an expanded IDEA encryption key EK
+ * Note that the input and output may be the same. Thus, the key is
+ * inverted into an internal buffer, and then copied to the output.
+ */
+void
+ideaInvertKey(word16 const *EK, word16 DK[IDEAKEYLEN])
+{
+ int i;
+ uint16 t1, t2, t3;
+ word16 temp[IDEAKEYLEN];
+ word16 *p = temp + IDEAKEYLEN;
+
+ t1 = mulInv(*EK++);
+ t2 = -*EK++;
+ t3 = -*EK++;
+ *--p = mulInv(*EK++);
+ *--p = t3;
+ *--p = t2;
+ *--p = t1;
+
+ for (i = 0; i < IDEAROUNDS-1; i++) {
+ t1 = *EK++;
+ *--p = *EK++;
+ *--p = t1;
+
+ t1 = mulInv(*EK++);
+ t2 = -*EK++;
+ t3 = -*EK++;
+ *--p = mulInv(*EK++);
+ *--p = t2;
+ *--p = t3;
+ *--p = t1;
+ }
+ t1 = *EK++;
+ *--p = *EK++;
+ *--p = t1;
+
+ t1 = mulInv(*EK++);
+ t2 = -*EK++;
+ t3 = -*EK++;
+ *--p = mulInv(*EK++);
+ *--p = t3;
+ *--p = t2;
+ *--p = t1;
+/* Copy and destroy temp copy */
+ memcpy(DK, temp, sizeof(temp));
+ burn(temp);
+} /* ideaInvertKey */
+
+/*
+ * MUL(x,y) computes x = x*y, modulo 0x10001. Requires two temps,
+ * t16 and t32. x is modified, and must me a side-effect-free lvalue.
+ * y may be anything, but unlike x, must be strictly 16 bits even if
+ * low16() is #defined.
+ * All of these are equivalent - see which is faster on your machine
+ */
+#ifdef SMALL_CACHE
+#define MUL(x,y) (x = mul(low16(x),y))
+#else /* !SMALL_CACHE */
+#ifdef AVOID_JUMPS
+#define MUL(x,y) (x = low16(x-1), t16 = low16((y)-1), \
+ t32 = (word32)x*t16 + x + t16 + 1, x = low16(t32), \
+ t16 = t32>>16, x = (x-t16) + (x<t16) )
+#else /* !AVOID_JUMPS (default) */
+#define MUL(x,y) \
+ ((t16 = (y)) ? \
+ (x=low16(x)) ? \
+ t32 = (word32)x*t16, \
+ x = low16(t32), \
+ t16 = t32>>16, \
+ x = (x-t16)+(x<t16) \
+ : \
+ (x = 1-t16) \
+ : \
+ (x = 1-x))
+#endif
+#endif
+
+/* IDEA encryption/decryption algorithm */
+/* Note that in and out can be the same buffer */
+void
+ideaCipher(byte const (inbuf[8]), byte (outbuf[8]), word16 const *key)
+{
+ register uint16 x1, x2, x3, x4, s2, s3;
+ word16 *in, *out;
+#ifndef SMALL_CACHE
+ register uint16 t16; /* Temporaries needed by MUL macro */
+ register word32 t32;
+#endif
+ int r = IDEAROUNDS;
+
+ in = (word16 *)inbuf;
+ x1 = *in++; x2 = *in++;
+ x3 = *in++; x4 = *in;
+#ifndef HIGHFIRST
+ x1 = (x1>>8) | (x1<<8);
+ x2 = (x2>>8) | (x2<<8);
+ x3 = (x3>>8) | (x3<<8);
+ x4 = (x4>>8) | (x4<<8);
+#endif
+ do {
+ MUL(x1,*key++);
+ x2 += *key++;
+ x3 += *key++;
+ MUL(x4, *key++);
+
+ s3 = x3;
+ x3 ^= x1;
+ MUL(x3, *key++);
+ s2 = x2;
+ x2 ^= x4;
+ x2 += x3;
+ MUL(x2, *key++);
+ x3 += x2;
+
+ x1 ^= x2; x4 ^= x3;
+
+ x2 ^= s3; x3 ^= s2;
+ } while (--r);
+ MUL(x1, *key++);
+ x3 += *key++;
+ x2 += *key++;
+ MUL(x4, *key);
+
+ out = (word16 *)outbuf;
+#ifdef HIGHFIRST
+ *out++ = x1;
+ *out++ = x3;
+ *out++ = x2;
+ *out = x4;
+#else /* !HIGHFIRST */
+ x1 = low16(x1);
+ x2 = low16(x2);
+ x3 = low16(x3);
+ x4 = low16(x4);
+ *out++ = (x1>>8) | (x1<<8);
+ *out++ = (x3>>8) | (x3<<8);
+ *out++ = (x2>>8) | (x2<<8);
+ *out = (x4>>8) | (x4<<8);
+#endif
+} /* ideaCipher */
+
+/*-------------------------------------------------------------*/
+
+#ifdef TEST
+
+#include <stdio.h>
+#include <time.h>
+/*
+ * This is the number of Kbytes of test data to encrypt.
+ * It defaults to 1 MByte.
+ */
+#ifndef BLOCKS
+#ifndef KBYTES
+#define KBYTES 1024
+#endif
+#define BLOCKS (64*KBYTES)
+#endif
+
+int
+main(void)
+{ /* Test driver for IDEA cipher */
+ int i, j, k;
+ byte userkey[16];
+ word16 EK[IDEAKEYLEN], DK[IDEAKEYLEN];
+ byte XX[8], YY[8], ZZ[8];
+ long l;
+
+ /* Make a sample user key for testing... */
+ for(i=0; i<16; i++)
+ userkey[i] = i+1;
+
+ /* Compute encryption subkeys from user key... */
+ ideaExpandKey(userkey, EK);
+ printf("\nEncryption key subblocks: ");
+ for (j=0; j<IDEAROUNDS+1; j++) {
+ printf("\nround %d: ", j+1);
+ if (j < IDEAROUNDS)
+ for(i=0; i<6; i++)
+ printf(" %6u", EK[j*6+i]);
+ else
+ for(i=0; i<4; i++)
+ printf(" %6u", EK[j*6+i]);
+ }
+
+ /* Compute decryption subkeys from encryption subkeys... */
+ ideaInvertKey(EK, DK);
+ printf("\nDecryption key subblocks: ");
+ for (j=0; j<IDEAROUNDS+1; j++) {
+ printf("\nround %d: ", j+1);
+ if (j < IDEAROUNDS)
+ for(i=0; i<6; i++)
+ printf(" %6u", DK[j*6+i]);
+ else
+ for(i=0; i<4; i++)
+ printf(" %6u", DK[j*6+i]);
+ }
+
+ /* Make a sample plaintext pattern for testing... */
+ for (k=0; k<8; k++)
+ XX[k] = k;
+
+ printf("\n Encrypting %d bytes (%d blocks)...", BLOCKS*16, BLOCKS);
+ fflush(stdout);
+
+ clock_start();
+ memcpy(YY, XX, 8);
+ for (l = 0; l < BLOCKS; l++)
+ ideaCipher(YY, YY, EK); /* repeated encryption */
+ memcpy(ZZ, YY, 8);
+ for (l = 0; l < BLOCKS; l++)
+ ideaCipher(ZZ, ZZ, DK); /* repeated decryption */
+ clock_stop();
+
+ /*
+ l = end / (CLOCKS_PER_SEC/1000) + 1;
+ i = l/1000;
+ j = l%1000;
+ l = (16 * BLOCKS * (CLOCKS_PER_SEC/1000)) / (end/1000);
+ printf("%d.%03d seconds = %ld bytes per second\n", i, j, l); */
+ print_total_clock();
+
+ printf("\nX %3u %3u %3u %3u %3u %3u %3u %3u\n",
+ XX[0], XX[1], XX[2], XX[3], XX[4], XX[5], XX[6], XX[7]);
+ printf("\nY %3u %3u %3u %3u %3u %3u %3u %3u\n",
+ YY[0], YY[1], YY[2], YY[3], YY[4], YY[5], YY[6], YY[7]);
+ printf("\nZ %3u %3u %3u %3u %3u %3u %3u %3u\n",
+ ZZ[0], ZZ[1], ZZ[2], ZZ[3], ZZ[4], ZZ[5], ZZ[6], ZZ[7]);
+
+ /* Now decrypted ZZ should be same as original XX */
+ for (k=0; k<8; k++)
+ if (XX[k] != ZZ[k]) {
+ printf("\n\07Error! Noninvertable encryption.\n");
+ exit(-1); /* error exit */
+ }
+ printf("\nNormal exit.\n");
+ return 0; /* normal exit */
+} /* main */
+
+#endif /* TEST */
+
+/* end of idea.c */
diff --git a/test/monniaux/idea/idea.h b/test/monniaux/idea/idea.h
new file mode 100644
index 00000000..41521b9a
--- /dev/null
+++ b/test/monniaux/idea/idea.h
@@ -0,0 +1,30 @@
+#ifndef _IDEA_DEFINED
+
+#define _IDEA_DEFINED
+
+/* Defines for the PGP-style types used in IDEA.C */
+
+#include <stdint.h>
+#define byte uint8_t
+#define word16 uint16_t
+#define word32 uint32_t
+
+/* Macros from PGP */
+
+#define burn(x) memset( x, 0, sizeof( x ) )
+
+/* IDEA algorithm constants */
+
+#define IDEAKEYSIZE 16
+#define IDEABLOCKSIZE 8
+
+#define IDEAROUNDS 8
+#define IDEAKEYLEN ( 6 * IDEAROUNDS + 4 )
+
+/* Routines used to implement the IDEA encryption */
+
+void ideaExpandKey( byte const *userkey, word16 *EK );
+void ideaInvertKey( word16 const *EK, word16 DK[IDEAKEYLEN] );
+void ideaCipher( byte const (inbuf[8]), byte (outbuf[8]), word16 const *key );
+
+#endif /* _IDEA_DEFINED */
diff --git a/test/monniaux/if/if2.c b/test/monniaux/if/if2.c
new file mode 100644
index 00000000..2a6d5507
--- /dev/null
+++ b/test/monniaux/if/if2.c
@@ -0,0 +1,11 @@
+int toto(int x) {
+ if (2*x+1 >= 3) {
+ if (2*x+1 >= 3) {
+ return 3;
+ } else {
+ return 2;
+ }
+ } else {
+ return 1;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/Makefile b/test/monniaux/jpeg-6b/Makefile
new file mode 100644
index 00000000..36d230a1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/Makefile
@@ -0,0 +1,60 @@
+TARGET=jpeg-6b
+
+ALL_CFILES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c jmemansi.c
+ALL_CFILES+=rdppm.c rdgif.c rdtarga.c rdrle.c rdbmp.c rdswitch.c cdjpeg.c wrppm.c wrgif.c wrtarga.c wrrle.c wrbmp.c rdcolmap.c
+ALL_CFILES+=cjpeg.c
+
+EXECUTE_ARGS=-dct int -outfile __BASE__.jpg testimg.ppm 2> __BASE__.out
+
+include ../rules.mk
+
+#all: cjpeg.gcc.kvx.out djpeg.gcc.kvx.out cjpeg.gcc.o1.kvx.out djpeg.gcc.o1.kvx.out cjpeg.ccomp.kvx.out djpeg.ccomp.kvx.out
+#
+#LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+# jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+# jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+# jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+# jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+# jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+# jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+# jquant2.c jutils.c jmemmgr.c jmemansi.c
+#CSOURCES=$(LIBSOURCES) rdppm.c rdgif.c rdtarga.c rdrle.c rdbmp.c rdswitch.c cdjpeg.c wrppm.c wrgif.c wrtarga.c wrrle.c wrbmp.c rdcolmap.c
+#
+#LIB_KVX_GCC_OFILES=$(CSOURCES:.c=.gcc.kvx.o)
+#LIB_KVX_GCC_O1_OFILES=$(CSOURCES:.c=.gcc.o1.kvx.o)
+#LIB_KVX_CCOMP_OFILES=$(CSOURCES:.c=.ccomp.kvx.o)
+#
+#include ../rules.mk
+#
+#cjpeg.gcc.kvx: $(LIB_KVX_GCC_OFILES) cjpeg.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS) -o $@ $+ ../clock.gcc.kvx.o
+#djpeg.gcc.kvx: $(LIB_KVX_GCC_OFILES) djpeg.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS) -o $@ $+ ../clock.gcc.kvx.o
+#
+#cjpeg.gcc.o1.kvx: $(LIB_KVX_GCC_O1_OFILES) cjpeg.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS_O1) -o $@ $+ ../clock.gcc.kvx.o
+#djpeg.gcc.o1.kvx: $(LIB_KVX_GCC_O1_OFILES) djpeg.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS_O1) -o $@ $+ ../clock.gcc.kvx.o
+#
+#cjpeg.ccomp.kvx: $(LIB_KVX_CCOMP_OFILES) cjpeg.gcc.kvx.o
+# $(KVX_CCOMP) $(KVX_CCOMPFLAGS) -o $@ $+ ../clock.gcc.kvx.o
+#djpeg.ccomp.kvx: $(LIB_KVX_CCOMP_OFILES) djpeg.gcc.kvx.o
+# $(KVX_CCOMP) $(KVX_CCOMPFLAGS) -o $@ $+ ../clock.gcc.kvx.o
+#
+#
+#djpeg.%.out: djpeg.%
+# $(EXECUTE_CYCLES) $< -dct int -ppm -outfile $@.ppm testorig.jpg 2> $@
+# cmp $@.ppm testimg.ppm 2>> $@
+#
+#cjpeg.%.out: cjpeg.%
+# $(EXECUTE_CYCLES) $< -dct int -outfile $@.jpg testimg.ppm 2> $@
+# cmp $@.jpg testimg.jpg 2>> $@
+#
+#.SECONDARY:
diff --git a/test/monniaux/jpeg-6b/Makefile.orig b/test/monniaux/jpeg-6b/Makefile.orig
new file mode 100644
index 00000000..bd4ba992
--- /dev/null
+++ b/test/monniaux/jpeg-6b/Makefile.orig
@@ -0,0 +1,319 @@
+# Generated automatically from makefile.cfg by configure.
+# Makefile for Independent JPEG Group's software
+
+# makefile.cfg is edited by configure to produce a custom Makefile.
+
+# Read installation instructions before saying "make" !!
+
+# For compiling with source and object files in different directories.
+srcdir = .
+
+# Where to install the programs and man pages.
+prefix = /usr/local
+exec_prefix = ${prefix}
+bindir = $(exec_prefix)/bin
+libdir = $(exec_prefix)/lib
+includedir = $(prefix)/include
+binprefix =
+manprefix =
+manext = 1
+mandir = $(prefix)/man/man$(manext)
+
+# The name of your C compiler:
+CC= gcc
+
+# You may need to adjust these cc options:
+CFLAGS= -O2 -I$(srcdir)
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+# However, any special defines for ansi2knr.c may be included here:
+ANSI2KNRFLAGS=
+
+# Link-time cc options:
+LDFLAGS=
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS=
+
+# If using GNU libtool, LIBTOOL references it; if not, LIBTOOL is empty.
+LIBTOOL =
+# $(O) expands to "lo" if using libtool, plain "o" if not.
+# Similarly, $(A) expands to "la" or "a".
+O = o
+A = a
+
+# Library version ID; libtool uses this for the shared library version number.
+# Note: we suggest this match the macro of the same name in jpeglib.h.
+JPEG_LIB_VERSION = 62
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.$(O)
+
+# miscellaneous OS-dependent stuff
+SHELL= /bin/sh
+# linker
+LN= $(CC)
+# file deletion command
+RM= rm -f
+# directory creation command
+MKDIR= mkdir
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= ranlib
+# installation program
+INSTALL= /usr/bin/install -c
+INSTALL_PROGRAM= ${INSTALL}
+INSTALL_LIB= ${INSTALL} -m 644
+INSTALL_DATA= ${INSTALL} -m 644
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.$(O) jutils.$(O) jerror.$(O) jmemmgr.$(O) $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.$(O) jcapistd.$(O) jctrans.$(O) jcparam.$(O) \
+ jdatadst.$(O) jcinit.$(O) jcmaster.$(O) jcmarker.$(O) jcmainct.$(O) \
+ jcprepct.$(O) jccoefct.$(O) jccolor.$(O) jcsample.$(O) jchuff.$(O) \
+ jcphuff.$(O) jcdctmgr.$(O) jfdctfst.$(O) jfdctflt.$(O) \
+ jfdctint.$(O)
+# decompression library object files
+DLIBOBJECTS= jdapimin.$(O) jdapistd.$(O) jdtrans.$(O) jdatasrc.$(O) \
+ jdmaster.$(O) jdinput.$(O) jdmarker.$(O) jdhuff.$(O) jdphuff.$(O) \
+ jdmainct.$(O) jdcoefct.$(O) jdpostct.$(O) jddctmgr.$(O) \
+ jidctfst.$(O) jidctflt.$(O) jidctint.$(O) jidctred.$(O) \
+ jdsample.$(O) jdcolor.$(O) jquant1.$(O) jquant2.$(O) jdmerge.$(O)
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.$(O) rdppm.$(O) rdgif.$(O) rdtarga.$(O) rdrle.$(O) \
+ rdbmp.$(O) rdswitch.$(O) cdjpeg.$(O)
+DOBJECTS= djpeg.$(O) wrppm.$(O) wrgif.$(O) wrtarga.$(O) wrrle.$(O) \
+ wrbmp.$(O) rdcolmap.$(O) cdjpeg.$(O)
+TROBJECTS= jpegtran.$(O) rdswitch.$(O) cdjpeg.$(O) transupp.$(O)
+
+
+all: libjpeg.$(A) cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+
+# Special compilation rules to support ansi2knr and libtool.
+.SUFFIXES: .lo .la
+
+# How to compile with libtool.
+# .c.lo:
+# $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c $(srcdir)/$*.c
+
+# How to use ansi2knr, when not using libtool.
+# .c.o:
+# ./ansi2knr $(srcdir)/$*.c knr/$*.c
+# $(CC) $(CFLAGS) -c knr/$*.c
+# $(RM) knr/$*.c
+
+# How to use ansi2knr AND libtool.
+# .c.lo:
+# ./ansi2knr $(srcdir)/$*.c knr/$*.c
+# $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c knr/$*.c
+# $(RM) knr/$*.c
+
+ansi2knr: ansi2knr.c
+ $(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr $(srcdir)/ansi2knr.c
+ $(MKDIR) knr
+
+# the library:
+
+# without libtool:
+libjpeg.a: $(LIBOBJECTS)
+ $(RM) libjpeg.a
+ $(AR) libjpeg.a $(LIBOBJECTS)
+ $(AR2) libjpeg.a
+
+# with libtool:
+libjpeg.la: $(LIBOBJECTS)
+ $(LIBTOOL) --mode=link $(CC) -o libjpeg.la $(LIBOBJECTS) \
+ -rpath $(libdir) -version-info $(JPEG_LIB_VERSION)
+
+# sample programs:
+
+cjpeg: $(COBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.$(A) $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.$(A) $(LDLIBS)
+
+jpegtran: $(TROBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o jpegtran $(TROBJECTS) libjpeg.$(A) $(LDLIBS)
+
+rdjpgcom: rdjpgcom.$(O)
+ $(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.$(O) $(LDLIBS)
+
+wrjpgcom: wrjpgcom.$(O)
+ $(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.$(O) $(LDLIBS)
+
+# Installation rules:
+
+install: cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+ $(INSTALL_PROGRAM) cjpeg $(bindir)/$(binprefix)cjpeg
+ $(INSTALL_PROGRAM) djpeg $(bindir)/$(binprefix)djpeg
+ $(INSTALL_PROGRAM) jpegtran $(bindir)/$(binprefix)jpegtran
+ $(INSTALL_PROGRAM) rdjpgcom $(bindir)/$(binprefix)rdjpgcom
+ $(INSTALL_PROGRAM) wrjpgcom $(bindir)/$(binprefix)wrjpgcom
+ $(INSTALL_DATA) $(srcdir)/cjpeg.1 $(mandir)/$(manprefix)cjpeg.$(manext)
+ $(INSTALL_DATA) $(srcdir)/djpeg.1 $(mandir)/$(manprefix)djpeg.$(manext)
+ $(INSTALL_DATA) $(srcdir)/jpegtran.1 $(mandir)/$(manprefix)jpegtran.$(manext)
+ $(INSTALL_DATA) $(srcdir)/rdjpgcom.1 $(mandir)/$(manprefix)rdjpgcom.$(manext)
+ $(INSTALL_DATA) $(srcdir)/wrjpgcom.1 $(mandir)/$(manprefix)wrjpgcom.$(manext)
+
+install-lib: libjpeg.$(A) install-headers
+ $(INSTALL_LIB) libjpeg.$(A) $(libdir)/$(binprefix)libjpeg.$(A)
+
+install-headers: jconfig.h
+ $(INSTALL_DATA) jconfig.h $(includedir)/jconfig.h
+ $(INSTALL_DATA) $(srcdir)/jpeglib.h $(includedir)/jpeglib.h
+ $(INSTALL_DATA) $(srcdir)/jmorecfg.h $(includedir)/jmorecfg.h
+ $(INSTALL_DATA) $(srcdir)/jerror.h $(includedir)/jerror.h
+
+clean:
+ $(RM) *.o *.lo libjpeg.a libjpeg.la
+ $(RM) cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+ $(RM) ansi2knr core testout* config.log config.status
+ $(RM) -r knr .libs _libs
+
+distclean: clean
+ $(RM) Makefile jconfig.h libtool config.cache
+
+test: cjpeg djpeg jpegtran
+ $(RM) testout*
+ ./djpeg -dct int -ppm -outfile testout.ppm $(srcdir)/testorig.jpg
+ ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp $(srcdir)/testorig.jpg
+ ./cjpeg -dct int -outfile testout.jpg $(srcdir)/testimg.ppm
+ ./djpeg -dct int -ppm -outfile testoutp.ppm $(srcdir)/testprog.jpg
+ ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg $(srcdir)/testimg.ppm
+ ./jpegtran -outfile testoutt.jpg $(srcdir)/testprog.jpg
+ cmp $(srcdir)/testimg.ppm testout.ppm
+ cmp $(srcdir)/testimg.bmp testout.bmp
+ cmp $(srcdir)/testimg.jpg testout.jpg
+ cmp $(srcdir)/testimg.ppm testoutp.ppm
+ cmp $(srcdir)/testimgp.jpg testoutp.jpg
+ cmp $(srcdir)/testorig.jpg testoutt.jpg
+
+check: test
+
+# Mistake catcher:
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+# GNU Make likes to know which target names are not really files to be made:
+.PHONY: all install install-lib install-headers clean distclean test check
+
+
+jcapimin.$(O): jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.$(O): jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.$(O): jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.$(O): jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.$(O): jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.$(O): jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.$(O): jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.$(O): jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.$(O): jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.$(O): jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.$(O): jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.$(O): jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.$(O): jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.$(O): jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.$(O): jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.$(O): jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.$(O): jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.$(O): jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.$(O): jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.$(O): jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.$(O): jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.$(O): jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.$(O): jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.$(O): jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.$(O): jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.$(O): jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.$(O): jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.$(O): jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.$(O): jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.$(O): jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.$(O): jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.$(O): jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.$(O): jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.$(O): jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.$(O): jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.$(O): jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.$(O): jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.$(O): jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.$(O): jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.$(O): jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.$(O): jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.$(O): jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.$(O): jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.$(O): jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.$(O): jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.$(O): jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.$(O): jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.$(O): jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.$(O): jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.$(O): jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.$(O): cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.$(O): djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.$(O): jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.$(O): rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.$(O): wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.$(O): cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.$(O): rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.$(O): rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.$(O): transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.$(O): rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.$(O): wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.$(O): rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.$(O): wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.$(O): rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.$(O): wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.$(O): rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.$(O): wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.$(O): rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.$(O): wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/README b/test/monniaux/jpeg-6b/README
new file mode 100644
index 00000000..86cc2066
--- /dev/null
+++ b/test/monniaux/jpeg-6b/README
@@ -0,0 +1,385 @@
+The Independent JPEG Group's JPEG software
+==========================================
+
+README for release 6b of 27-Mar-1998
+====================================
+
+This distribution contains the sixth public release of the Independent JPEG
+Group's free JPEG software. You are welcome to redistribute this software and
+to use it for any purpose, subject to the conditions under LEGAL ISSUES, below.
+
+Serious users of this software (particularly those incorporating it into
+larger programs) should contact IJG at jpeg-info@uunet.uu.net to be added to
+our electronic mailing list. Mailing list members are notified of updates
+and have a chance to participate in technical discussions, etc.
+
+This software is the work of Tom Lane, Philip Gladstone, Jim Boucher,
+Lee Crocker, Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi,
+Guido Vollbeding, Ge' Weijers, and other members of the Independent JPEG
+Group.
+
+IJG is not affiliated with the official ISO JPEG standards committee.
+
+
+DOCUMENTATION ROADMAP
+=====================
+
+This file contains the following sections:
+
+OVERVIEW General description of JPEG and the IJG software.
+LEGAL ISSUES Copyright, lack of warranty, terms of distribution.
+REFERENCES Where to learn more about JPEG.
+ARCHIVE LOCATIONS Where to find newer versions of this software.
+RELATED SOFTWARE Other stuff you should get.
+FILE FORMAT WARS Software *not* to get.
+TO DO Plans for future IJG releases.
+
+Other documentation files in the distribution are:
+
+User documentation:
+ install.doc How to configure and install the IJG software.
+ usage.doc Usage instructions for cjpeg, djpeg, jpegtran,
+ rdjpgcom, and wrjpgcom.
+ *.1 Unix-style man pages for programs (same info as usage.doc).
+ wizard.doc Advanced usage instructions for JPEG wizards only.
+ change.log Version-to-version change highlights.
+Programmer and internal documentation:
+ libjpeg.doc How to use the JPEG library in your own programs.
+ example.c Sample code for calling the JPEG library.
+ structure.doc Overview of the JPEG library's internal structure.
+ filelist.doc Road map of IJG files.
+ coderules.doc Coding style rules --- please read if you contribute code.
+
+Please read at least the files install.doc and usage.doc. Useful information
+can also be found in the JPEG FAQ (Frequently Asked Questions) article. See
+ARCHIVE LOCATIONS below to find out where to obtain the FAQ article.
+
+If you want to understand how the JPEG code works, we suggest reading one or
+more of the REFERENCES, then looking at the documentation files (in roughly
+the order listed) before diving into the code.
+
+
+OVERVIEW
+========
+
+This package contains C software to implement JPEG image compression and
+decompression. JPEG (pronounced "jay-peg") is a standardized compression
+method for full-color and gray-scale images. JPEG is intended for compressing
+"real-world" scenes; line drawings, cartoons and other non-realistic images
+are not its strong suit. JPEG is lossy, meaning that the output image is not
+exactly identical to the input image. Hence you must not use JPEG if you
+have to have identical output bits. However, on typical photographic images,
+very good compression levels can be obtained with no visible change, and
+remarkably high compression levels are possible if you can tolerate a
+low-quality image. For more details, see the references, or just experiment
+with various compression settings.
+
+This software implements JPEG baseline, extended-sequential, and progressive
+compression processes. Provision is made for supporting all variants of these
+processes, although some uncommon parameter settings aren't implemented yet.
+For legal reasons, we are not distributing code for the arithmetic-coding
+variants of JPEG; see LEGAL ISSUES. We have made no provision for supporting
+the hierarchical or lossless processes defined in the standard.
+
+We provide a set of library routines for reading and writing JPEG image files,
+plus two sample applications "cjpeg" and "djpeg", which use the library to
+perform conversion between JPEG and some other popular image file formats.
+The library is intended to be reused in other applications.
+
+In order to support file conversion and viewing software, we have included
+considerable functionality beyond the bare JPEG coding/decoding capability;
+for example, the color quantization modules are not strictly part of JPEG
+decoding, but they are essential for output to colormapped file formats or
+colormapped displays. These extra functions can be compiled out of the
+library if not required for a particular application. We have also included
+"jpegtran", a utility for lossless transcoding between different JPEG
+processes, and "rdjpgcom" and "wrjpgcom", two simple applications for
+inserting and extracting textual comments in JFIF files.
+
+The emphasis in designing this software has been on achieving portability and
+flexibility, while also making it fast enough to be useful. In particular,
+the software is not intended to be read as a tutorial on JPEG. (See the
+REFERENCES section for introductory material.) Rather, it is intended to
+be reliable, portable, industrial-strength code. We do not claim to have
+achieved that goal in every aspect of the software, but we strive for it.
+
+We welcome the use of this software as a component of commercial products.
+No royalty is required, but we do ask for an acknowledgement in product
+documentation, as described under LEGAL ISSUES.
+
+
+LEGAL ISSUES
+============
+
+In plain English:
+
+1. We don't promise that this software works. (But if you find any bugs,
+ please let us know!)
+2. You can use this software for whatever you want. You don't have to pay us.
+3. You may not pretend that you wrote this software. If you use it in a
+ program, you must acknowledge somewhere in your documentation that
+ you've used the IJG code.
+
+In legalese:
+
+The authors make NO WARRANTY or representation, either express or implied,
+with respect to this software, its quality, accuracy, merchantability, or
+fitness for a particular purpose. This software is provided "AS IS", and you,
+its user, assume the entire risk as to its quality and accuracy.
+
+This software is copyright (C) 1991-1998, Thomas G. Lane.
+All Rights Reserved except as specified below.
+
+Permission is hereby granted to use, copy, modify, and distribute this
+software (or portions thereof) for any purpose, without fee, subject to these
+conditions:
+(1) If any part of the source code for this software is distributed, then this
+README file must be included, with this copyright and no-warranty notice
+unaltered; and any additions, deletions, or changes to the original files
+must be clearly indicated in accompanying documentation.
+(2) If only executable code is distributed, then the accompanying
+documentation must state that "this software is based in part on the work of
+the Independent JPEG Group".
+(3) Permission for use of this software is granted only if the user accepts
+full responsibility for any undesirable consequences; the authors accept
+NO LIABILITY for damages of any kind.
+
+These conditions apply to any software derived from or based on the IJG code,
+not just to the unmodified library. If you use our work, you ought to
+acknowledge us.
+
+Permission is NOT granted for the use of any IJG author's name or company name
+in advertising or publicity relating to this software or products derived from
+it. This software may be referred to only as "the Independent JPEG Group's
+software".
+
+We specifically permit and encourage the use of this software as the basis of
+commercial products, provided that all warranty or liability claims are
+assumed by the product vendor.
+
+
+ansi2knr.c is included in this distribution by permission of L. Peter Deutsch,
+sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA.
+ansi2knr.c is NOT covered by the above copyright and conditions, but instead
+by the usual distribution terms of the Free Software Foundation; principally,
+that you must include source code if you redistribute it. (See the file
+ansi2knr.c for full details.) However, since ansi2knr.c is not needed as part
+of any program generated from the IJG code, this does not limit you more than
+the foregoing paragraphs do.
+
+The Unix configuration script "configure" was produced with GNU Autoconf.
+It is copyright by the Free Software Foundation but is freely distributable.
+The same holds for its supporting scripts (config.guess, config.sub,
+ltconfig, ltmain.sh). Another support script, install-sh, is copyright
+by M.I.T. but is also freely distributable.
+
+It appears that the arithmetic coding option of the JPEG spec is covered by
+patents owned by IBM, AT&T, and Mitsubishi. Hence arithmetic coding cannot
+legally be used without obtaining one or more licenses. For this reason,
+support for arithmetic coding has been removed from the free JPEG software.
+(Since arithmetic coding provides only a marginal gain over the unpatented
+Huffman mode, it is unlikely that very many implementations will support it.)
+So far as we are aware, there are no patent restrictions on the remaining
+code.
+
+The IJG distribution formerly included code to read and write GIF files.
+To avoid entanglement with the Unisys LZW patent, GIF reading support has
+been removed altogether, and the GIF writer has been simplified to produce
+"uncompressed GIFs". This technique does not use the LZW algorithm; the
+resulting GIF files are larger than usual, but are readable by all standard
+GIF decoders.
+
+We are required to state that
+ "The Graphics Interchange Format(c) is the Copyright property of
+ CompuServe Incorporated. GIF(sm) is a Service Mark property of
+ CompuServe Incorporated."
+
+
+REFERENCES
+==========
+
+We highly recommend reading one or more of these references before trying to
+understand the innards of the JPEG software.
+
+The best short technical introduction to the JPEG compression algorithm is
+ Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
+ Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
+(Adjacent articles in that issue discuss MPEG motion picture compression,
+applications of JPEG, and related topics.) If you don't have the CACM issue
+handy, a PostScript file containing a revised version of Wallace's article is
+available at ftp://ftp.uu.net/graphics/jpeg/wallace.ps.gz. The file (actually
+a preprint for an article that appeared in IEEE Trans. Consumer Electronics)
+omits the sample images that appeared in CACM, but it includes corrections
+and some added material. Note: the Wallace article is copyright ACM and IEEE,
+and it may not be used for commercial purposes.
+
+A somewhat less technical, more leisurely introduction to JPEG can be found in
+"The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by
+M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1. This book provides
+good explanations and example C code for a multitude of compression methods
+including JPEG. It is an excellent source if you are comfortable reading C
+code but don't know much about data compression in general. The book's JPEG
+sample code is far from industrial-strength, but when you are ready to look
+at a full implementation, you've got one here...
+
+The best full description of JPEG is the textbook "JPEG Still Image Data
+Compression Standard" by William B. Pennebaker and Joan L. Mitchell, published
+by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1. Price US$59.95, 638 pp.
+The book includes the complete text of the ISO JPEG standards (DIS 10918-1
+and draft DIS 10918-2). This is by far the most complete exposition of JPEG
+in existence, and we highly recommend it.
+
+The JPEG standard itself is not available electronically; you must order a
+paper copy through ISO or ITU. (Unless you feel a need to own a certified
+official copy, we recommend buying the Pennebaker and Mitchell book instead;
+it's much cheaper and includes a great deal of useful explanatory material.)
+In the USA, copies of the standard may be ordered from ANSI Sales at (212)
+642-4900, or from Global Engineering Documents at (800) 854-7179. (ANSI
+doesn't take credit card orders, but Global does.) It's not cheap: as of
+1992, ANSI was charging $95 for Part 1 and $47 for Part 2, plus 7%
+shipping/handling. The standard is divided into two parts, Part 1 being the
+actual specification, while Part 2 covers compliance testing methods. Part 1
+is titled "Digital Compression and Coding of Continuous-tone Still Images,
+Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS
+10918-1, ITU-T T.81. Part 2 is titled "Digital Compression and Coding of
+Continuous-tone Still Images, Part 2: Compliance testing" and has document
+numbers ISO/IEC IS 10918-2, ITU-T T.83.
+
+Some extensions to the original JPEG standard are defined in JPEG Part 3,
+a newer ISO standard numbered ISO/IEC IS 10918-3 and ITU-T T.84. IJG
+currently does not support any Part 3 extensions.
+
+The JPEG standard does not specify all details of an interchangeable file
+format. For the omitted details we follow the "JFIF" conventions, revision
+1.02. A copy of the JFIF spec is available from:
+ Literature Department
+ C-Cube Microsystems, Inc.
+ 1778 McCarthy Blvd.
+ Milpitas, CA 95035
+ phone (408) 944-6300, fax (408) 944-6314
+A PostScript version of this document is available by FTP at
+ftp://ftp.uu.net/graphics/jpeg/jfif.ps.gz. There is also a plain text
+version at ftp://ftp.uu.net/graphics/jpeg/jfif.txt.gz, but it is missing
+the figures.
+
+The TIFF 6.0 file format specification can be obtained by FTP from
+ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz. The JPEG incorporation scheme
+found in the TIFF 6.0 spec of 3-June-92 has a number of serious problems.
+IJG does not recommend use of the TIFF 6.0 design (TIFF Compression tag 6).
+Instead, we recommend the JPEG design proposed by TIFF Technical Note #2
+(Compression tag 7). Copies of this Note can be obtained from ftp.sgi.com or
+from ftp://ftp.uu.net/graphics/jpeg/. It is expected that the next revision
+of the TIFF spec will replace the 6.0 JPEG design with the Note's design.
+Although IJG's own code does not support TIFF/JPEG, the free libtiff library
+uses our library to implement TIFF/JPEG per the Note. libtiff is available
+from ftp://ftp.sgi.com/graphics/tiff/.
+
+
+ARCHIVE LOCATIONS
+=================
+
+The "official" archive site for this software is ftp.uu.net (Internet
+address 192.48.96.9). The most recent released version can always be found
+there in directory graphics/jpeg. This particular version will be archived
+as ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz. If you don't have
+direct Internet access, UUNET's archives are also available via UUCP; contact
+help@uunet.uu.net for information on retrieving files that way.
+
+Numerous Internet sites maintain copies of the UUNET files. However, only
+ftp.uu.net is guaranteed to have the latest official version.
+
+You can also obtain this software in DOS-compatible "zip" archive format from
+the SimTel archives (ftp://ftp.simtel.net/pub/simtelnet/msdos/graphics/), or
+on CompuServe in the Graphics Support forum (GO CIS:GRAPHSUP), library 12
+"JPEG Tools". Again, these versions may sometimes lag behind the ftp.uu.net
+release.
+
+The JPEG FAQ (Frequently Asked Questions) article is a useful source of
+general information about JPEG. It is updated constantly and therefore is
+not included in this distribution. The FAQ is posted every two weeks to
+Usenet newsgroups comp.graphics.misc, news.answers, and other groups.
+It is available on the World Wide Web at http://www.faqs.org/faqs/jpeg-faq/
+and other news.answers archive sites, including the official news.answers
+archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/.
+If you don't have Web or FTP access, send e-mail to mail-server@rtfm.mit.edu
+with body
+ send usenet/news.answers/jpeg-faq/part1
+ send usenet/news.answers/jpeg-faq/part2
+
+
+RELATED SOFTWARE
+================
+
+Numerous viewing and image manipulation programs now support JPEG. (Quite a
+few of them use this library to do so.) The JPEG FAQ described above lists
+some of the more popular free and shareware viewers, and tells where to
+obtain them on Internet.
+
+If you are on a Unix machine, we highly recommend Jef Poskanzer's free
+PBMPLUS software, which provides many useful operations on PPM-format image
+files. In particular, it can convert PPM images to and from a wide range of
+other formats, thus making cjpeg/djpeg considerably more useful. The latest
+version is distributed by the NetPBM group, and is available from numerous
+sites, notably ftp://wuarchive.wustl.edu/graphics/graphics/packages/NetPBM/.
+Unfortunately PBMPLUS/NETPBM is not nearly as portable as the IJG software is;
+you are likely to have difficulty making it work on any non-Unix machine.
+
+A different free JPEG implementation, written by the PVRG group at Stanford,
+is available from ftp://havefun.stanford.edu/pub/jpeg/. This program
+is designed for research and experimentation rather than production use;
+it is slower, harder to use, and less portable than the IJG code, but it
+is easier to read and modify. Also, the PVRG code supports lossless JPEG,
+which we do not. (On the other hand, it doesn't do progressive JPEG.)
+
+
+FILE FORMAT WARS
+================
+
+Some JPEG programs produce files that are not compatible with our library.
+The root of the problem is that the ISO JPEG committee failed to specify a
+concrete file format. Some vendors "filled in the blanks" on their own,
+creating proprietary formats that no one else could read. (For example, none
+of the early commercial JPEG implementations for the Macintosh were able to
+exchange compressed files.)
+
+The file format we have adopted is called JFIF (see REFERENCES). This format
+has been agreed to by a number of major commercial JPEG vendors, and it has
+become the de facto standard. JFIF is a minimal or "low end" representation.
+We recommend the use of TIFF/JPEG (TIFF revision 6.0 as modified by TIFF
+Technical Note #2) for "high end" applications that need to record a lot of
+additional data about an image. TIFF/JPEG is fairly new and not yet widely
+supported, unfortunately.
+
+The upcoming JPEG Part 3 standard defines a file format called SPIFF.
+SPIFF is interoperable with JFIF, in the sense that most JFIF decoders should
+be able to read the most common variant of SPIFF. SPIFF has some technical
+advantages over JFIF, but its major claim to fame is simply that it is an
+official standard rather than an informal one. At this point it is unclear
+whether SPIFF will supersede JFIF or whether JFIF will remain the de-facto
+standard. IJG intends to support SPIFF once the standard is frozen, but we
+have not decided whether it should become our default output format or not.
+(In any case, our decoder will remain capable of reading JFIF indefinitely.)
+
+Various proprietary file formats incorporating JPEG compression also exist.
+We have little or no sympathy for the existence of these formats. Indeed,
+one of the original reasons for developing this free software was to help
+force convergence on common, open format standards for JPEG files. Don't
+use a proprietary file format!
+
+
+TO DO
+=====
+
+The major thrust for v7 will probably be improvement of visual quality.
+The current method for scaling the quantization tables is known not to be
+very good at low Q values. We also intend to investigate block boundary
+smoothing, "poor man's variable quantization", and other means of improving
+quality-vs-file-size performance without sacrificing compatibility.
+
+In future versions, we are considering supporting some of the upcoming JPEG
+Part 3 extensions --- principally, variable quantization and the SPIFF file
+format.
+
+As always, speeding things up is of great interest.
+
+Please send bug reports, offers of help, etc. to jpeg-info@uunet.uu.net.
diff --git a/test/monniaux/jpeg-6b/ansi2knr.1 b/test/monniaux/jpeg-6b/ansi2knr.1
new file mode 100644
index 00000000..f9ee5a63
--- /dev/null
+++ b/test/monniaux/jpeg-6b/ansi2knr.1
@@ -0,0 +1,36 @@
+.TH ANSI2KNR 1 "19 Jan 1996"
+.SH NAME
+ansi2knr \- convert ANSI C to Kernighan & Ritchie C
+.SH SYNOPSIS
+.I ansi2knr
+[--varargs] input_file [output_file]
+.SH DESCRIPTION
+If no output_file is supplied, output goes to stdout.
+.br
+There are no error messages.
+.sp
+.I ansi2knr
+recognizes function definitions by seeing a non-keyword identifier at the left
+margin, followed by a left parenthesis, with a right parenthesis as the last
+character on the line, and with a left brace as the first token on the
+following line (ignoring possible intervening comments). It will recognize a
+multi-line header provided that no intervening line ends with a left or right
+brace or a semicolon. These algorithms ignore whitespace and comments, except
+that the function name must be the first thing on the line.
+.sp
+The following constructs will confuse it:
+.br
+ - Any other construct that starts at the left margin and follows the
+above syntax (such as a macro or function call).
+.br
+ - Some macros that tinker with the syntax of the function header.
+.sp
+The --varargs switch is obsolete, and is recognized only for
+backwards compatibility. The present version of
+.I ansi2knr
+will always attempt to convert a ... argument to va_alist and va_dcl.
+.SH AUTHOR
+L. Peter Deutsch <ghost@aladdin.com> wrote the original ansi2knr and
+continues to maintain the current version; most of the code in the current
+version is his work. ansi2knr also includes contributions by Francois
+Pinard <pinard@iro.umontreal.ca> and Jim Avera <jima@netcom.com>.
diff --git a/test/monniaux/jpeg-6b/cderror.h b/test/monniaux/jpeg-6b/cderror.h
new file mode 100644
index 00000000..70435e16
--- /dev/null
+++ b/test/monniaux/jpeg-6b/cderror.h
@@ -0,0 +1,132 @@
+/*
+ * cderror.h
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the error and message codes for the cjpeg/djpeg
+ * applications. These strings are not needed as part of the JPEG library
+ * proper.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ */
+
+/*
+ * To define the enum list of message codes, include this file without
+ * defining macro JMESSAGE. To create a message string table, include it
+ * again with a suitable JMESSAGE definition (see jerror.c for an example).
+ */
+#ifndef JMESSAGE
+#ifndef CDERROR_H
+#define CDERROR_H
+/* First time through, define the enum list */
+#define JMAKE_ENUM_LIST
+#else
+/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
+#define JMESSAGE(code,string)
+#endif /* CDERROR_H */
+#endif /* JMESSAGE */
+
+#ifdef JMAKE_ENUM_LIST
+
+typedef enum {
+
+#define JMESSAGE(code,string) code ,
+
+#endif /* JMAKE_ENUM_LIST */
+
+JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
+
+#ifdef BMP_SUPPORTED
+JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
+JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
+JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
+JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
+JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
+JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
+JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
+JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
+JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
+JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
+JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
+#endif /* BMP_SUPPORTED */
+
+#ifdef GIF_SUPPORTED
+JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
+JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
+JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
+JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
+JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
+JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
+JMESSAGE(JTRC_GIF_BADVERSION,
+ "Warning: unexpected GIF version number '%c%c%c'")
+JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
+JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
+JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
+JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
+JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
+JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
+#endif /* GIF_SUPPORTED */
+
+#ifdef PPM_SUPPORTED
+JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
+JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
+JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
+JMESSAGE(JTRC_PGM, "%ux%u PGM image")
+JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
+JMESSAGE(JTRC_PPM, "%ux%u PPM image")
+JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
+#endif /* PPM_SUPPORTED */
+
+#ifdef RLE_SUPPORTED
+JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
+JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
+JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
+JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
+JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
+JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
+JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
+JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
+JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
+JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
+JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
+JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
+#endif /* RLE_SUPPORTED */
+
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
+JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
+JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
+JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
+JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
+JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
+#else
+JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
+#endif /* TARGA_SUPPORTED */
+
+JMESSAGE(JERR_BAD_CMAP_FILE,
+ "Color map file is invalid or of unsupported format")
+JMESSAGE(JERR_TOO_MANY_COLORS,
+ "Output file format cannot handle %d colormap entries")
+JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_UNKNOWN_FORMAT,
+ "Unrecognized input file format --- perhaps you need -targa")
+#else
+JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
+#endif
+JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
+
+#ifdef JMAKE_ENUM_LIST
+
+ JMSG_LASTADDONCODE
+} ADDON_MESSAGE_CODE;
+
+#undef JMAKE_ENUM_LIST
+#endif /* JMAKE_ENUM_LIST */
+
+/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
+#undef JMESSAGE
diff --git a/test/monniaux/jpeg-6b/cdjpeg.c b/test/monniaux/jpeg-6b/cdjpeg.c
new file mode 100644
index 00000000..b6250ff9
--- /dev/null
+++ b/test/monniaux/jpeg-6b/cdjpeg.c
@@ -0,0 +1,181 @@
+/*
+ * cdjpeg.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains common support routines used by the IJG application
+ * programs (cjpeg, djpeg, jpegtran).
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+#include <ctype.h> /* to declare isupper(), tolower() */
+#ifdef NEED_SIGNAL_CATCHER
+#include <signal.h> /* to declare signal() */
+#endif
+#ifdef USE_SETMODE
+#include <fcntl.h> /* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h> /* to declare setmode() */
+#endif
+
+
+/*
+ * Signal catcher to ensure that temporary files are removed before aborting.
+ * NB: for Amiga Manx C this is actually a global routine named _abort();
+ * we put "#define signal_catcher _abort" in jconfig.h. Talk about bogus...
+ */
+
+#ifdef NEED_SIGNAL_CATCHER
+
+static j_common_ptr sig_cinfo;
+
+void /* must be global for Manx C */
+signal_catcher (int signum)
+{
+ if (sig_cinfo != NULL) {
+ if (sig_cinfo->err != NULL) /* turn off trace output */
+ sig_cinfo->err->trace_level = 0;
+ jpeg_destroy(sig_cinfo); /* clean up memory allocation & temp files */
+ }
+ exit(EXIT_FAILURE);
+}
+
+
+GLOBAL(void)
+enable_signal_catcher (j_common_ptr cinfo)
+{
+ sig_cinfo = cinfo;
+#ifdef SIGINT /* not all systems have SIGINT */
+ signal(SIGINT, signal_catcher);
+#endif
+#ifdef SIGTERM /* not all systems have SIGTERM */
+ signal(SIGTERM, signal_catcher);
+#endif
+}
+
+#endif
+
+
+/*
+ * Optional progress monitor: display a percent-done figure on stderr.
+ */
+
+#ifdef PROGRESS_REPORT
+
+METHODDEF(void)
+progress_monitor (j_common_ptr cinfo)
+{
+ cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
+ int total_passes = prog->pub.total_passes + prog->total_extra_passes;
+ int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
+
+ if (percent_done != prog->percent_done) {
+ prog->percent_done = percent_done;
+ if (total_passes > 1) {
+ fprintf(stderr, "\rPass %d/%d: %3d%% ",
+ prog->pub.completed_passes + prog->completed_extra_passes + 1,
+ total_passes, percent_done);
+ } else {
+ fprintf(stderr, "\r %3d%% ", percent_done);
+ }
+ fflush(stderr);
+ }
+}
+
+
+GLOBAL(void)
+start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
+{
+ /* Enable progress display, unless trace output is on */
+ if (cinfo->err->trace_level == 0) {
+ progress->pub.progress_monitor = progress_monitor;
+ progress->completed_extra_passes = 0;
+ progress->total_extra_passes = 0;
+ progress->percent_done = -1;
+ cinfo->progress = &progress->pub;
+ }
+}
+
+
+GLOBAL(void)
+end_progress_monitor (j_common_ptr cinfo)
+{
+ /* Clear away progress display */
+ if (cinfo->err->trace_level == 0) {
+ fprintf(stderr, "\r \r");
+ fflush(stderr);
+ }
+}
+
+#endif
+
+
+/*
+ * Case-insensitive matching of possibly-abbreviated keyword switches.
+ * keyword is the constant keyword (must be lower case already),
+ * minchars is length of minimum legal abbreviation.
+ */
+
+GLOBAL(boolean)
+keymatch (char * arg, const char * keyword, int minchars)
+{
+ register int ca, ck;
+ register int nmatched = 0;
+
+ while ((ca = *arg++) != '\0') {
+ if ((ck = *keyword++) == '\0')
+ return FALSE; /* arg longer than keyword, no good */
+ if (isupper(ca)) /* force arg to lcase (assume ck is already) */
+ ca = tolower(ca);
+ if (ca != ck)
+ return FALSE; /* no good */
+ nmatched++; /* count matched characters */
+ }
+ /* reached end of argument; fail if it's too short for unique abbrev */
+ if (nmatched < minchars)
+ return FALSE;
+ return TRUE; /* A-OK */
+}
+
+
+/*
+ * Routines to establish binary I/O mode for stdin and stdout.
+ * Non-Unix systems often require some hacking to get out of text mode.
+ */
+
+GLOBAL(FILE *)
+read_stdin (void)
+{
+ FILE * input_file = stdin;
+
+#ifdef USE_SETMODE /* need to hack file mode? */
+ setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN /* need to re-open in binary mode? */
+ if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+ fprintf(stderr, "Cannot reopen stdin\n");
+ exit(EXIT_FAILURE);
+ }
+#endif
+ return input_file;
+}
+
+
+GLOBAL(FILE *)
+write_stdout (void)
+{
+ FILE * output_file = stdout;
+
+#ifdef USE_SETMODE /* need to hack file mode? */
+ setmode(fileno(stdout), O_BINARY);
+#endif
+#ifdef USE_FDOPEN /* need to re-open in binary mode? */
+ if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "Cannot reopen stdout\n");
+ exit(EXIT_FAILURE);
+ }
+#endif
+ return output_file;
+}
diff --git a/test/monniaux/jpeg-6b/cdjpeg.h b/test/monniaux/jpeg-6b/cdjpeg.h
new file mode 100644
index 00000000..2b387b6e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/cdjpeg.h
@@ -0,0 +1,184 @@
+/*
+ * cdjpeg.h
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains common declarations for the sample applications
+ * cjpeg and djpeg. It is NOT used by the core JPEG library.
+ */
+
+#define JPEG_CJPEG_DJPEG /* define proper options in jconfig.h */
+#define JPEG_INTERNAL_OPTIONS /* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h" /* get library error codes too */
+#include "cderror.h" /* get application-specific error codes */
+
+
+/*
+ * Object interface for cjpeg's source file decoding modules
+ */
+
+typedef struct cjpeg_source_struct * cjpeg_source_ptr;
+
+struct cjpeg_source_struct {
+ JMETHOD(void, start_input, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+ JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+ JMETHOD(void, finish_input, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+
+ FILE *input_file;
+
+ JSAMPARRAY buffer;
+ JDIMENSION buffer_height;
+};
+
+
+/*
+ * Object interface for djpeg's output file encoding modules
+ */
+
+typedef struct djpeg_dest_struct * djpeg_dest_ptr;
+
+struct djpeg_dest_struct {
+ /* start_output is called after jpeg_start_decompress finishes.
+ * The color map will be ready at this time, if one is needed.
+ */
+ JMETHOD(void, start_output, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo));
+ /* Emit the specified number of pixel rows from the buffer. */
+ JMETHOD(void, put_pixel_rows, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied));
+ /* Finish up at the end of the image. */
+ JMETHOD(void, finish_output, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo));
+
+ /* Target file spec; filled in by djpeg.c after object is created. */
+ FILE * output_file;
+
+ /* Output pixel-row buffer. Created by module init or start_output.
+ * Width is cinfo->output_width * cinfo->output_components;
+ * height is buffer_height.
+ */
+ JSAMPARRAY buffer;
+ JDIMENSION buffer_height;
+};
+
+
+/*
+ * cjpeg/djpeg may need to perform extra passes to convert to or from
+ * the source/destination file format. The JPEG library does not know
+ * about these passes, but we'd like them to be counted by the progress
+ * monitor. We use an expanded progress monitor object to hold the
+ * additional pass count.
+ */
+
+struct cdjpeg_progress_mgr {
+ struct jpeg_progress_mgr pub; /* fields known to JPEG library */
+ int completed_extra_passes; /* extra passes completed */
+ int total_extra_passes; /* total extra */
+ /* last printed percentage stored here to avoid multiple printouts */
+ int percent_done;
+};
+
+typedef struct cdjpeg_progress_mgr * cd_progress_ptr;
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_read_bmp jIRdBMP
+#define jinit_write_bmp jIWrBMP
+#define jinit_read_gif jIRdGIF
+#define jinit_write_gif jIWrGIF
+#define jinit_read_ppm jIRdPPM
+#define jinit_write_ppm jIWrPPM
+#define jinit_read_rle jIRdRLE
+#define jinit_write_rle jIWrRLE
+#define jinit_read_targa jIRdTarga
+#define jinit_write_targa jIWrTarga
+#define read_quant_tables RdQTables
+#define read_scan_script RdScnScript
+#define set_quant_slots SetQSlots
+#define set_sample_factors SetSFacts
+#define read_color_map RdCMap
+#define enable_signal_catcher EnSigCatcher
+#define start_progress_monitor StProgMon
+#define end_progress_monitor EnProgMon
+#define read_stdin RdStdin
+#define write_stdout WrStdout
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Module selection routines for I/O modules. */
+
+EXTERN(cjpeg_source_ptr) jinit_read_bmp JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_bmp JPP((j_decompress_ptr cinfo,
+ boolean is_os2));
+EXTERN(cjpeg_source_ptr) jinit_read_gif JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_gif JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_ppm JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_ppm JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_rle JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_rle JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_targa JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_targa JPP((j_decompress_ptr cinfo));
+
+/* cjpeg support routines (in rdswitch.c) */
+
+EXTERN(boolean) read_quant_tables JPP((j_compress_ptr cinfo, char * filename,
+ int scale_factor, boolean force_baseline));
+EXTERN(boolean) read_scan_script JPP((j_compress_ptr cinfo, char * filename));
+EXTERN(boolean) set_quant_slots JPP((j_compress_ptr cinfo, char *arg));
+EXTERN(boolean) set_sample_factors JPP((j_compress_ptr cinfo, char *arg));
+
+/* djpeg support routines (in rdcolmap.c) */
+
+EXTERN(void) read_color_map JPP((j_decompress_ptr cinfo, FILE * infile));
+
+/* common support routines (in cdjpeg.c) */
+
+EXTERN(void) enable_signal_catcher JPP((j_common_ptr cinfo));
+EXTERN(void) start_progress_monitor JPP((j_common_ptr cinfo,
+ cd_progress_ptr progress));
+EXTERN(void) end_progress_monitor JPP((j_common_ptr cinfo));
+EXTERN(boolean) keymatch JPP((char * arg, const char * keyword, int minchars));
+EXTERN(FILE *) read_stdin JPP((void));
+EXTERN(FILE *) write_stdout JPP((void));
+
+/* miscellaneous useful macros */
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#define WRITE_BINARY "w"
+#else
+#ifdef VMS /* VMS is very nonstandard */
+#define READ_BINARY "rb", "ctx=stm"
+#define WRITE_BINARY "wb", "ctx=stm"
+#else /* standard ANSI-compliant case */
+#define READ_BINARY "rb"
+#define WRITE_BINARY "wb"
+#endif
+#endif
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS 0
+#endif
+#endif
+#ifndef EXIT_WARNING
+#ifdef VMS
+#define EXIT_WARNING 1 /* VMS is very nonstandard */
+#else
+#define EXIT_WARNING 2
+#endif
+#endif
diff --git a/test/monniaux/jpeg-6b/cjpeg.1 b/test/monniaux/jpeg-6b/cjpeg.1
new file mode 100644
index 00000000..d175a961
--- /dev/null
+++ b/test/monniaux/jpeg-6b/cjpeg.1
@@ -0,0 +1,292 @@
+.TH CJPEG 1 "20 March 1998"
+.SH NAME
+cjpeg \- compress an image file to a JPEG file
+.SH SYNOPSIS
+.B cjpeg
+[
+.I options
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B cjpeg
+compresses the named image file, or the standard input if no file is
+named, and produces a JPEG/JFIF file on the standard output.
+The currently supported input file formats are: PPM (PBMPLUS color
+format), PGM (PBMPLUS gray-scale format), BMP, Targa, and RLE (Utah Raster
+Toolkit format). (RLE is supported only if the URT library is available.)
+.SH OPTIONS
+All switch names may be abbreviated; for example,
+.B \-grayscale
+may be written
+.B \-gray
+or
+.BR \-gr .
+Most of the "basic" switches can be abbreviated to as little as one letter.
+Upper and lower case are equivalent (thus
+.B \-BMP
+is the same as
+.BR \-bmp ).
+British spellings are also accepted (e.g.,
+.BR \-greyscale ),
+though for brevity these are not mentioned below.
+.PP
+The basic switches are:
+.TP
+.BI \-quality " N"
+Scale quantization tables to adjust image quality. Quality is 0 (worst) to
+100 (best); default is 75. (See below for more info.)
+.TP
+.B \-grayscale
+Create monochrome JPEG file from color input. Be sure to use this switch when
+compressing a grayscale BMP file, because
+.B cjpeg
+isn't bright enough to notice whether a BMP file uses only shades of gray.
+By saying
+.BR \-grayscale ,
+you'll get a smaller JPEG file that takes less time to process.
+.TP
+.B \-optimize
+Perform optimization of entropy encoding parameters. Without this, default
+encoding parameters are used.
+.B \-optimize
+usually makes the JPEG file a little smaller, but
+.B cjpeg
+runs somewhat slower and needs much more memory. Image quality and speed of
+decompression are unaffected by
+.BR \-optimize .
+.TP
+.B \-progressive
+Create progressive JPEG file (see below).
+.TP
+.B \-targa
+Input file is Targa format. Targa files that contain an "identification"
+field will not be automatically recognized by
+.BR cjpeg ;
+for such files you must specify
+.B \-targa
+to make
+.B cjpeg
+treat the input as Targa format.
+For most Targa files, you won't need this switch.
+.PP
+The
+.B \-quality
+switch lets you trade off compressed file size against quality of the
+reconstructed image: the higher the quality setting, the larger the JPEG file,
+and the closer the output image will be to the original input. Normally you
+want to use the lowest quality setting (smallest file) that decompresses into
+something visually indistinguishable from the original image. For this
+purpose the quality setting should be between 50 and 95; the default of 75 is
+often about right. If you see defects at
+.B \-quality
+75, then go up 5 or 10 counts at a time until you are happy with the output
+image. (The optimal setting will vary from one image to another.)
+.PP
+.B \-quality
+100 will generate a quantization table of all 1's, minimizing loss in the
+quantization step (but there is still information loss in subsampling, as well
+as roundoff error). This setting is mainly of interest for experimental
+purposes. Quality values above about 95 are
+.B not
+recommended for normal use; the compressed file size goes up dramatically for
+hardly any gain in output image quality.
+.PP
+In the other direction, quality values below 50 will produce very small files
+of low image quality. Settings around 5 to 10 might be useful in preparing an
+index of a large image library, for example. Try
+.B \-quality
+2 (or so) for some amusing Cubist effects. (Note: quality
+values below about 25 generate 2-byte quantization tables, which are
+considered optional in the JPEG standard.
+.B cjpeg
+emits a warning message when you give such a quality value, because some
+other JPEG programs may be unable to decode the resulting file. Use
+.B \-baseline
+if you need to ensure compatibility at low quality values.)
+.PP
+The
+.B \-progressive
+switch creates a "progressive JPEG" file. In this type of JPEG file, the data
+is stored in multiple scans of increasing quality. If the file is being
+transmitted over a slow communications link, the decoder can use the first
+scan to display a low-quality image very quickly, and can then improve the
+display with each subsequent scan. The final image is exactly equivalent to a
+standard JPEG file of the same quality setting, and the total file size is
+about the same --- often a little smaller.
+.B Caution:
+progressive JPEG is not yet widely implemented, so many decoders will be
+unable to view a progressive JPEG file at all.
+.PP
+Switches for advanced users:
+.TP
+.B \-dct int
+Use integer DCT method (default).
+.TP
+.B \-dct fast
+Use fast integer DCT (less accurate).
+.TP
+.B \-dct float
+Use floating-point DCT method.
+The float method is very slightly more accurate than the int method, but is
+much slower unless your machine has very fast floating-point hardware. Also
+note that results of the floating-point method may vary slightly across
+machines, while the integer methods should give the same results everywhere.
+The fast integer method is much less accurate than the other two.
+.TP
+.BI \-restart " N"
+Emit a JPEG restart marker every N MCU rows, or every N MCU blocks if "B" is
+attached to the number.
+.B \-restart 0
+(the default) means no restart markers.
+.TP
+.BI \-smooth " N"
+Smooth the input image to eliminate dithering noise. N, ranging from 1 to
+100, indicates the strength of smoothing. 0 (the default) means no smoothing.
+.TP
+.BI \-maxmemory " N"
+Set limit for amount of memory to use in processing large images. Value is
+in thousands of bytes, or millions of bytes if "M" is attached to the
+number. For example,
+.B \-max 4m
+selects 4000000 bytes. If more space is needed, temporary files will be used.
+.TP
+.BI \-outfile " name"
+Send output image to the named file, not to standard output.
+.TP
+.B \-verbose
+Enable debug printout. More
+.BR \-v 's
+give more output. Also, version information is printed at startup.
+.TP
+.B \-debug
+Same as
+.BR \-verbose .
+.PP
+The
+.B \-restart
+option inserts extra markers that allow a JPEG decoder to resynchronize after
+a transmission error. Without restart markers, any damage to a compressed
+file will usually ruin the image from the point of the error to the end of the
+image; with restart markers, the damage is usually confined to the portion of
+the image up to the next restart marker. Of course, the restart markers
+occupy extra space. We recommend
+.B \-restart 1
+for images that will be transmitted across unreliable networks such as Usenet.
+.PP
+The
+.B \-smooth
+option filters the input to eliminate fine-scale noise. This is often useful
+when converting dithered images to JPEG: a moderate smoothing factor of 10 to
+50 gets rid of dithering patterns in the input file, resulting in a smaller
+JPEG file and a better-looking image. Too large a smoothing factor will
+visibly blur the image, however.
+.PP
+Switches for wizards:
+.TP
+.B \-baseline
+Force baseline-compatible quantization tables to be generated. This clamps
+quantization values to 8 bits even at low quality settings. (This switch is
+poorly named, since it does not ensure that the output is actually baseline
+JPEG. For example, you can use
+.B \-baseline
+and
+.B \-progressive
+together.)
+.TP
+.BI \-qtables " file"
+Use the quantization tables given in the specified text file.
+.TP
+.BI \-qslots " N[,...]"
+Select which quantization table to use for each color component.
+.TP
+.BI \-sample " HxV[,...]"
+Set JPEG sampling factors for each color component.
+.TP
+.BI \-scans " file"
+Use the scan script given in the specified text file.
+.PP
+The "wizard" switches are intended for experimentation with JPEG. If you
+don't know what you are doing, \fBdon't use them\fR. These switches are
+documented further in the file wizard.doc.
+.SH EXAMPLES
+.LP
+This example compresses the PPM file foo.ppm with a quality factor of
+60 and saves the output as foo.jpg:
+.IP
+.B cjpeg \-quality
+.I 60 foo.ppm
+.B >
+.I foo.jpg
+.SH HINTS
+Color GIF files are not the ideal input for JPEG; JPEG is really intended for
+compressing full-color (24-bit) images. In particular, don't try to convert
+cartoons, line drawings, and other images that have only a few distinct
+colors. GIF works great on these, JPEG does not. If you want to convert a
+GIF to JPEG, you should experiment with
+.BR cjpeg 's
+.B \-quality
+and
+.B \-smooth
+options to get a satisfactory conversion.
+.B \-smooth 10
+or so is often helpful.
+.PP
+Avoid running an image through a series of JPEG compression/decompression
+cycles. Image quality loss will accumulate; after ten or so cycles the image
+may be noticeably worse than it was after one cycle. It's best to use a
+lossless format while manipulating an image, then convert to JPEG format when
+you are ready to file the image away.
+.PP
+The
+.B \-optimize
+option to
+.B cjpeg
+is worth using when you are making a "final" version for posting or archiving.
+It's also a win when you are using low quality settings to make very small
+JPEG files; the percentage improvement is often a lot more than it is on
+larger files. (At present,
+.B \-optimize
+mode is always selected when generating progressive JPEG files.)
+.SH ENVIRONMENT
+.TP
+.B JPEGMEM
+If this environment variable is set, its value is the default memory limit.
+The value is specified as described for the
+.B \-maxmemory
+switch.
+.B JPEGMEM
+overrides the default value specified when the program was compiled, and
+itself is overridden by an explicit
+.BR \-maxmemory .
+.SH SEE ALSO
+.BR djpeg (1),
+.BR jpegtran (1),
+.BR rdjpgcom (1),
+.BR wrjpgcom (1)
+.br
+.BR ppm (5),
+.BR pgm (5)
+.br
+Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
+Communications of the ACM, April 1991 (vol. 34, no. 4), pp. 30-44.
+.SH AUTHOR
+Independent JPEG Group
+.SH BUGS
+Arithmetic coding is not supported for legal reasons.
+.PP
+GIF input files are no longer supported, to avoid the Unisys LZW patent.
+Use a Unisys-licensed program if you need to read a GIF file. (Conversion
+of GIF files to JPEG is usually a bad idea anyway.)
+.PP
+Not all variants of BMP and Targa file formats are supported.
+.PP
+The
+.B \-targa
+switch is not a bug, it's a feature. (It would be a bug if the Targa format
+designers had not been clueless.)
+.PP
+Still not as fast as we'd like.
diff --git a/test/monniaux/jpeg-6b/cjpeg.c b/test/monniaux/jpeg-6b/cjpeg.c
new file mode 100644
index 00000000..deade36d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/cjpeg.c
@@ -0,0 +1,619 @@
+/*
+ * cjpeg.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a command-line user interface for the JPEG compressor.
+ * It should work on any system with Unix- or MS-DOS-style command lines.
+ *
+ * Two different command line styles are permitted, depending on the
+ * compile-time switch TWO_FILE_COMMANDLINE:
+ * cjpeg [options] inputfile outputfile
+ * cjpeg [options] [inputfile]
+ * In the second style, output is always to standard output, which you'd
+ * normally redirect to a file or pipe to some other program. Input is
+ * either from a named file or from standard input (typically redirected).
+ * The second style is convenient on Unix but is unhelpful on systems that
+ * don't support pipes. Also, you MUST use the first style if your system
+ * doesn't do binary I/O to stdin/stdout.
+ * To simplify script writing, the "-outfile" switch is provided. The syntax
+ * cjpeg [options] -outfile outputfile inputfile
+ * works regardless of which command line style is used.
+ */
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+#include "jversion.h" /* for version message */
+
+#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h> /* Metrowerks needs this */
+#include <console.h> /* ... and this */
+#endif
+#ifdef THINK_C
+#include <console.h> /* Think declares it here */
+#endif
+#endif
+
+
+/* Create the add-on message string table. */
+
+#define JMESSAGE(code,string) string ,
+
+static const char * const cdjpeg_message_table[] = {
+#include "cderror.h"
+ NULL
+};
+
+
+/*
+ * This routine determines what format the input file is,
+ * and selects the appropriate input-reading module.
+ *
+ * To determine which family of input formats the file belongs to,
+ * we may look only at the first byte of the file, since C does not
+ * guarantee that more than one character can be pushed back with ungetc.
+ * Looking at additional bytes would require one of these approaches:
+ * 1) assume we can fseek() the input file (fails for piped input);
+ * 2) assume we can push back more than one character (works in
+ * some C implementations, but unportable);
+ * 3) provide our own buffering (breaks input readers that want to use
+ * stdio directly, such as the RLE library);
+ * or 4) don't put back the data, and modify the input_init methods to assume
+ * they start reading after the start of file (also breaks RLE library).
+ * #1 is attractive for MS-DOS but is untenable on Unix.
+ *
+ * The most portable solution for file types that can't be identified by their
+ * first byte is to make the user tell us what they are. This is also the
+ * only approach for "raw" file types that contain only arbitrary values.
+ * We presently apply this method for Targa files. Most of the time Targa
+ * files start with 0x00, so we recognize that case. Potentially, however,
+ * a Targa file could start with any byte value (byte 0 is the length of the
+ * seldom-used ID field), so we provide a switch to force Targa input mode.
+ */
+
+static boolean is_targa; /* records user -targa switch */
+
+
+LOCAL(cjpeg_source_ptr)
+select_file_type (j_compress_ptr cinfo, FILE * infile)
+{
+ int c;
+
+ if (is_targa) {
+#ifdef TARGA_SUPPORTED
+ return jinit_read_targa(cinfo);
+#else
+ ERREXIT(cinfo, JERR_TGA_NOTCOMP);
+#endif
+ }
+
+ if ((c = getc(infile)) == EOF)
+ ERREXIT(cinfo, JERR_INPUT_EMPTY);
+ if (ungetc(c, infile) == EOF)
+ ERREXIT(cinfo, JERR_UNGETC_FAILED);
+
+ switch (c) {
+#ifdef BMP_SUPPORTED
+ case 'B':
+ return jinit_read_bmp(cinfo);
+#endif
+#ifdef GIF_SUPPORTED
+ case 'G':
+ return jinit_read_gif(cinfo);
+#endif
+#ifdef PPM_SUPPORTED
+ case 'P':
+ return jinit_read_ppm(cinfo);
+#endif
+#ifdef RLE_SUPPORTED
+ case 'R':
+ return jinit_read_rle(cinfo);
+#endif
+#ifdef TARGA_SUPPORTED
+ case 0x00:
+ return jinit_read_targa(cinfo);
+#endif
+ default:
+ ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
+ break;
+ }
+
+ return NULL; /* suppress compiler warnings */
+}
+
+
+/*
+ * Argument-parsing code.
+ * The switch parser is designed to be useful with DOS-style command line
+ * syntax, ie, intermixed switches and file names, where only the switches
+ * to the left of a given file name affect processing of that file.
+ * The main program in this file doesn't actually use this capability...
+ */
+
+
+static const char * progname; /* program name for error messages */
+static char * outfilename; /* for -outfile switch */
+
+
+LOCAL(void)
+usage (void)
+/* complain about bad command line */
+{
+ fprintf(stderr, "usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+ fprintf(stderr, "inputfile outputfile\n");
+#else
+ fprintf(stderr, "[inputfile]\n");
+#endif
+
+ fprintf(stderr, "Switches (names may be abbreviated):\n");
+ fprintf(stderr, " -quality N Compression quality (0..100; 5-95 is useful range)\n");
+ fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
+#ifdef ENTROPY_OPT_SUPPORTED
+ fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
+#endif
+#ifdef C_PROGRESSIVE_SUPPORTED
+ fprintf(stderr, " -progressive Create progressive JPEG file\n");
+#endif
+#ifdef TARGA_SUPPORTED
+ fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
+#endif
+ fprintf(stderr, "Switches for advanced users:\n");
+#ifdef DCT_ISLOW_SUPPORTED
+ fprintf(stderr, " -dct int Use integer DCT method%s\n",
+ (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
+ (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
+ (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
+#endif
+ fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
+#endif
+ fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
+ fprintf(stderr, " -outfile name Specify name for output file\n");
+ fprintf(stderr, " -verbose or -debug Emit debug output\n");
+ fprintf(stderr, "Switches for wizards:\n");
+#ifdef C_ARITH_CODING_SUPPORTED
+ fprintf(stderr, " -arithmetic Use arithmetic coding\n");
+#endif
+ fprintf(stderr, " -baseline Force baseline quantization tables\n");
+ fprintf(stderr, " -qtables file Use quantization tables given in file\n");
+ fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
+ fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
+#endif
+ exit(EXIT_FAILURE);
+}
+
+
+LOCAL(int)
+parse_switches (j_compress_ptr cinfo, int argc, char **argv,
+ int last_file_arg_seen, boolean for_real)
+/* Parse optional switches.
+ * Returns argv[] index of first file-name argument (== argc if none).
+ * Any file names with indexes <= last_file_arg_seen are ignored;
+ * they have presumably been processed in a previous iteration.
+ * (Pass 0 for last_file_arg_seen on the first or only iteration.)
+ * for_real is FALSE on the first (dummy) pass; we may skip any expensive
+ * processing.
+ */
+{
+ int argn;
+ char * arg;
+ int quality; /* -quality parameter */
+ int q_scale_factor; /* scaling percentage for -qtables */
+ boolean force_baseline;
+ boolean simple_progressive;
+ char * qtablefile = NULL; /* saves -qtables filename if any */
+ char * qslotsarg = NULL; /* saves -qslots parm if any */
+ char * samplearg = NULL; /* saves -sample parm if any */
+ char * scansarg = NULL; /* saves -scans parm if any */
+
+ /* Set up default JPEG parameters. */
+ /* Note that default -quality level need not, and does not,
+ * match the default scaling for an explicit -qtables argument.
+ */
+ quality = 75; /* default -quality value */
+ q_scale_factor = 100; /* default to no scaling for -qtables */
+ force_baseline = FALSE; /* by default, allow 16-bit quantizers */
+ simple_progressive = FALSE;
+ is_targa = FALSE;
+ outfilename = NULL;
+ cinfo->err->trace_level = 0;
+
+ /* Scan command line options, adjust parameters */
+
+ for (argn = 1; argn < argc; argn++) {
+ arg = argv[argn];
+ if (*arg != '-') {
+ /* Not a switch, must be a file name argument */
+ if (argn <= last_file_arg_seen) {
+ outfilename = NULL; /* -outfile applies to just one input file */
+ continue; /* ignore this name if previously processed */
+ }
+ break; /* else done parsing switches */
+ }
+ arg++; /* advance past switch marker character */
+
+ if (keymatch(arg, "arithmetic", 1)) {
+ /* Use arithmetic coding. */
+#ifdef C_ARITH_CODING_SUPPORTED
+ cinfo->arith_code = TRUE;
+#else
+ fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "baseline", 1)) {
+ /* Force baseline-compatible output (8-bit quantizer values). */
+ force_baseline = TRUE;
+
+ } else if (keymatch(arg, "dct", 2)) {
+ /* Select DCT algorithm. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "int", 1)) {
+ cinfo->dct_method = JDCT_ISLOW;
+ } else if (keymatch(argv[argn], "fast", 2)) {
+ cinfo->dct_method = JDCT_IFAST;
+ } else if (keymatch(argv[argn], "float", 2)) {
+ cinfo->dct_method = JDCT_FLOAT;
+ } else
+ usage();
+
+ } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
+ /* Enable debug printouts. */
+ /* On first -d, print version identification */
+ static boolean printed_version = FALSE;
+
+ if (! printed_version) {
+ fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
+ JVERSION, JCOPYRIGHT);
+ printed_version = TRUE;
+ }
+ cinfo->err->trace_level++;
+
+ } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
+ /* Force a monochrome JPEG file to be generated. */
+ jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
+
+ } else if (keymatch(arg, "maxmemory", 3)) {
+ /* Maximum memory in Kb (or Mb with 'm'). */
+ long lval;
+ char ch = 'x';
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+ usage();
+ if (ch == 'm' || ch == 'M')
+ lval *= 1000L;
+ cinfo->mem->max_memory_to_use = lval * 1000L;
+
+ } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
+ /* Enable entropy parm optimization. */
+#ifdef ENTROPY_OPT_SUPPORTED
+ cinfo->optimize_coding = TRUE;
+#else
+ fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "outfile", 4)) {
+ /* Set output file name. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ outfilename = argv[argn]; /* save it away for later use */
+
+ } else if (keymatch(arg, "progressive", 1)) {
+ /* Select simple progressive mode. */
+#ifdef C_PROGRESSIVE_SUPPORTED
+ simple_progressive = TRUE;
+ /* We must postpone execution until num_components is known. */
+#else
+ fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "quality", 1)) {
+ /* Quality factor (quantization table scaling factor). */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%d", &quality) != 1)
+ usage();
+ /* Change scale factor in case -qtables is present. */
+ q_scale_factor = jpeg_quality_scaling(quality);
+
+ } else if (keymatch(arg, "qslots", 2)) {
+ /* Quantization table slot numbers. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ qslotsarg = argv[argn];
+ /* Must delay setting qslots until after we have processed any
+ * colorspace-determining switches, since jpeg_set_colorspace sets
+ * default quant table numbers.
+ */
+
+ } else if (keymatch(arg, "qtables", 2)) {
+ /* Quantization tables fetched from file. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ qtablefile = argv[argn];
+ /* We postpone actually reading the file in case -quality comes later. */
+
+ } else if (keymatch(arg, "restart", 1)) {
+ /* Restart interval in MCU rows (or in MCUs with 'b'). */
+ long lval;
+ char ch = 'x';
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+ usage();
+ if (lval < 0 || lval > 65535L)
+ usage();
+ if (ch == 'b' || ch == 'B') {
+ cinfo->restart_interval = (unsigned int) lval;
+ cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
+ } else {
+ cinfo->restart_in_rows = (int) lval;
+ /* restart_interval will be computed during startup */
+ }
+
+ } else if (keymatch(arg, "sample", 2)) {
+ /* Set sampling factors. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ samplearg = argv[argn];
+ /* Must delay setting sample factors until after we have processed any
+ * colorspace-determining switches, since jpeg_set_colorspace sets
+ * default sampling factors.
+ */
+
+ } else if (keymatch(arg, "scans", 2)) {
+ /* Set scan script. */
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ scansarg = argv[argn];
+ /* We must postpone reading the file in case -progressive appears. */
+#else
+ fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "smooth", 2)) {
+ /* Set input smoothing factor. */
+ int val;
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%d", &val) != 1)
+ usage();
+ if (val < 0 || val > 100)
+ usage();
+ cinfo->smoothing_factor = val;
+
+ } else if (keymatch(arg, "targa", 1)) {
+ /* Input file is Targa format. */
+ is_targa = TRUE;
+
+ } else {
+ usage(); /* bogus switch */
+ }
+ }
+
+ /* Post-switch-scanning cleanup */
+
+ if (for_real) {
+
+ /* Set quantization tables for selected quality. */
+ /* Some or all may be overridden if -qtables is present. */
+ jpeg_set_quality(cinfo, quality, force_baseline);
+
+ if (qtablefile != NULL) /* process -qtables if it was present */
+ if (! read_quant_tables(cinfo, qtablefile,
+ q_scale_factor, force_baseline))
+ usage();
+
+ if (qslotsarg != NULL) /* process -qslots if it was present */
+ if (! set_quant_slots(cinfo, qslotsarg))
+ usage();
+
+ if (samplearg != NULL) /* process -sample if it was present */
+ if (! set_sample_factors(cinfo, samplearg))
+ usage();
+
+#ifdef C_PROGRESSIVE_SUPPORTED
+ if (simple_progressive) /* process -progressive; -scans can override */
+ jpeg_simple_progression(cinfo);
+#endif
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (scansarg != NULL) /* process -scans if it was present */
+ if (! read_scan_script(cinfo, scansarg))
+ usage();
+#endif
+ }
+
+ return argn; /* return index of next arg (file name) */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+#ifdef PROGRESS_REPORT
+ struct cdjpeg_progress_mgr progress;
+#endif
+ int file_index;
+ cjpeg_source_ptr src_mgr;
+ FILE * input_file;
+ FILE * output_file;
+ JDIMENSION num_scanlines;
+
+ /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+ argc = ccommand(&argv);
+#endif
+
+ progname = argv[0];
+ if (progname == NULL || progname[0] == 0)
+ progname = "cjpeg"; /* in case C library doesn't provide it */
+
+ /* Initialize the JPEG compression object with default error handling. */
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_compress(&cinfo);
+ /* Add some application-specific error messages (from cderror.h) */
+ jerr.addon_message_table = cdjpeg_message_table;
+ jerr.first_addon_message = JMSG_FIRSTADDONCODE;
+ jerr.last_addon_message = JMSG_LASTADDONCODE;
+
+ /* Now safe to enable signal catcher. */
+#ifdef NEED_SIGNAL_CATCHER
+ enable_signal_catcher((j_common_ptr) &cinfo);
+#endif
+
+ /* Initialize JPEG parameters.
+ * Much of this may be overridden later.
+ * In particular, we don't yet know the input file's color space,
+ * but we need to provide some value for jpeg_set_defaults() to work.
+ */
+
+ cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
+ jpeg_set_defaults(&cinfo);
+
+ /* Scan command line to find file names.
+ * It is convenient to use just one switch-parsing routine, but the switch
+ * values read here are ignored; we will rescan the switches after opening
+ * the input file.
+ */
+
+ file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
+
+#ifdef TWO_FILE_COMMANDLINE
+ /* Must have either -outfile switch or explicit output file name */
+ if (outfilename == NULL) {
+ if (file_index != argc-2) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ outfilename = argv[file_index+1];
+ } else {
+ if (file_index != argc-1) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ }
+#else
+ /* Unix style: expect zero or one file name */
+ if (file_index < argc-1) {
+ fprintf(stderr, "%s: only one input file\n", progname);
+ usage();
+ }
+#endif /* TWO_FILE_COMMANDLINE */
+
+ /* Open the input file. */
+ if (file_index < argc) {
+ if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default input file is stdin */
+ input_file = read_stdin();
+ }
+
+ /* Open the output file. */
+ if (outfilename != NULL) {
+ if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default output file is stdout */
+ output_file = write_stdout();
+ }
+
+#ifdef PROGRESS_REPORT
+ start_progress_monitor((j_common_ptr) &cinfo, &progress);
+#endif
+
+ /* Figure out the input file format, and set up to read it. */
+ src_mgr = select_file_type(&cinfo, input_file);
+ src_mgr->input_file = input_file;
+
+ /* Read the input file header to obtain file size & colorspace. */
+ (*src_mgr->start_input) (&cinfo, src_mgr);
+
+ /* Now that we know input colorspace, fix colorspace-dependent defaults */
+ jpeg_default_colorspace(&cinfo);
+
+ /* Adjust default compression parameters by re-parsing the options */
+ file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
+
+ /* Specify data destination for compression */
+ jpeg_stdio_dest(&cinfo, output_file);
+
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ /* Start compressor */
+ jpeg_start_compress(&cinfo, TRUE);
+
+ /* Process data */
+ while (cinfo.next_scanline < cinfo.image_height) {
+ num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
+ (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
+ }
+#ifdef VERIMAG
+ clock_stop();
+ printerr_total_clock();
+#endif
+
+ /* Finish compression and release memory */
+ (*src_mgr->finish_input) (&cinfo, src_mgr);
+ jpeg_finish_compress(&cinfo);
+ jpeg_destroy_compress(&cinfo);
+
+ /* Close files, if we opened them */
+ if (input_file != stdin)
+ fclose(input_file);
+ if (output_file != stdout)
+ fclose(output_file);
+
+#ifdef PROGRESS_REPORT
+ end_progress_monitor((j_common_ptr) &cinfo);
+#endif
+
+ /* All done. */
+ exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
+ return 0; /* suppress no-return-value warnings */
+}
diff --git a/test/monniaux/jpeg-6b/ckconfig.c b/test/monniaux/jpeg-6b/ckconfig.c
new file mode 100644
index 00000000..34baf795
--- /dev/null
+++ b/test/monniaux/jpeg-6b/ckconfig.c
@@ -0,0 +1,402 @@
+/*
+ * ckconfig.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ */
+
+/*
+ * This program is intended to help you determine how to configure the JPEG
+ * software for installation on a particular system. The idea is to try to
+ * compile and execute this program. If your compiler fails to compile the
+ * program, make changes as indicated in the comments below. Once you can
+ * compile the program, run it, and it will produce a "jconfig.h" file for
+ * your system.
+ *
+ * As a general rule, each time you try to compile this program,
+ * pay attention only to the *first* error message you get from the compiler.
+ * Many C compilers will issue lots of spurious error messages once they
+ * have gotten confused. Go to the line indicated in the first error message,
+ * and read the comments preceding that line to see what to change.
+ *
+ * Almost all of the edits you may need to make to this program consist of
+ * changing a line that reads "#define SOME_SYMBOL" to "#undef SOME_SYMBOL",
+ * or vice versa. This is called defining or undefining that symbol.
+ */
+
+
+/* First we must see if your system has the include files we need.
+ * We start out with the assumption that your system has all the ANSI-standard
+ * include files. If you get any error trying to include one of these files,
+ * undefine the corresponding HAVE_xxx symbol.
+ */
+
+#define HAVE_STDDEF_H /* replace 'define' by 'undef' if error here */
+#ifdef HAVE_STDDEF_H /* next line will be skipped if you undef... */
+#include <stddef.h>
+#endif
+
+#define HAVE_STDLIB_H /* same thing for stdlib.h */
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include <stdio.h> /* If you ain't got this, you ain't got C. */
+
+/* We have to see if your string functions are defined by
+ * strings.h (old BSD convention) or string.h (everybody else).
+ * We try the non-BSD convention first; define NEED_BSD_STRINGS
+ * if the compiler says it can't find string.h.
+ */
+
+#undef NEED_BSD_STRINGS
+
+#ifdef NEED_BSD_STRINGS
+#include <strings.h>
+#else
+#include <string.h>
+#endif
+
+/* On some systems (especially older Unix machines), type size_t is
+ * defined only in the include file <sys/types.h>. If you get a failure
+ * on the size_t test below, try defining NEED_SYS_TYPES_H.
+ */
+
+#undef NEED_SYS_TYPES_H /* start by assuming we don't need it */
+#ifdef NEED_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+
+/* Usually type size_t is defined in one of the include files we've included
+ * above. If not, you'll get an error on the "typedef size_t my_size_t;" line.
+ * In that case, first try defining NEED_SYS_TYPES_H just above.
+ * If that doesn't work, you'll have to search through your system library
+ * to figure out which include file defines "size_t". Look for a line that
+ * says "typedef something-or-other size_t;". Then, change the line below
+ * that says "#include <someincludefile.h>" to instead include the file
+ * you found size_t in, and define NEED_SPECIAL_INCLUDE. If you can't find
+ * type size_t anywhere, try replacing "#include <someincludefile.h>" with
+ * "typedef unsigned int size_t;".
+ */
+
+#undef NEED_SPECIAL_INCLUDE /* assume we DON'T need it, for starters */
+
+#ifdef NEED_SPECIAL_INCLUDE
+#include <someincludefile.h>
+#endif
+
+typedef size_t my_size_t; /* The payoff: do we have size_t now? */
+
+
+/* The next question is whether your compiler supports ANSI-style function
+ * prototypes. You need to know this in order to choose between using
+ * makefile.ansi and using makefile.unix.
+ * The #define line below is set to assume you have ANSI function prototypes.
+ * If you get an error in this group of lines, undefine HAVE_PROTOTYPES.
+ */
+
+#define HAVE_PROTOTYPES
+
+#ifdef HAVE_PROTOTYPES
+int testfunction (int arg1, int * arg2); /* check prototypes */
+
+struct methods_struct { /* check method-pointer declarations */
+ int (*error_exit) (char *msgtext);
+ int (*trace_message) (char *msgtext);
+ int (*another_method) (void);
+};
+
+int testfunction (int arg1, int * arg2) /* check definitions */
+{
+ return arg2[arg1];
+}
+
+int test2function (void) /* check void arg list */
+{
+ return 0;
+}
+#endif
+
+
+/* Now we want to find out if your compiler knows what "unsigned char" means.
+ * If you get an error on the "unsigned char un_char;" line,
+ * then undefine HAVE_UNSIGNED_CHAR.
+ */
+
+#define HAVE_UNSIGNED_CHAR
+
+#ifdef HAVE_UNSIGNED_CHAR
+unsigned char un_char;
+#endif
+
+
+/* Now we want to find out if your compiler knows what "unsigned short" means.
+ * If you get an error on the "unsigned short un_short;" line,
+ * then undefine HAVE_UNSIGNED_SHORT.
+ */
+
+#define HAVE_UNSIGNED_SHORT
+
+#ifdef HAVE_UNSIGNED_SHORT
+unsigned short un_short;
+#endif
+
+
+/* Now we want to find out if your compiler understands type "void".
+ * If you get an error anywhere in here, undefine HAVE_VOID.
+ */
+
+#define HAVE_VOID
+
+#ifdef HAVE_VOID
+/* Caution: a C++ compiler will insist on complete prototypes */
+typedef void * void_ptr; /* check void * */
+#ifdef HAVE_PROTOTYPES /* check ptr to function returning void */
+typedef void (*void_func) (int a, int b);
+#else
+typedef void (*void_func) ();
+#endif
+
+#ifdef HAVE_PROTOTYPES /* check void function result */
+void test3function (void_ptr arg1, void_func arg2)
+#else
+void test3function (arg1, arg2)
+ void_ptr arg1;
+ void_func arg2;
+#endif
+{
+ char * locptr = (char *) arg1; /* check casting to and from void * */
+ arg1 = (void *) locptr;
+ (*arg2) (1, 2); /* check call of fcn returning void */
+}
+#endif
+
+
+/* Now we want to find out if your compiler knows what "const" means.
+ * If you get an error here, undefine HAVE_CONST.
+ */
+
+#define HAVE_CONST
+
+#ifdef HAVE_CONST
+static const int carray[3] = {1, 2, 3};
+
+#ifdef HAVE_PROTOTYPES
+int test4function (const int arg1)
+#else
+int test4function (arg1)
+ const int arg1;
+#endif
+{
+ return carray[arg1];
+}
+#endif
+
+
+/* If you get an error or warning about this structure definition,
+ * define INCOMPLETE_TYPES_BROKEN.
+ */
+
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifndef INCOMPLETE_TYPES_BROKEN
+typedef struct undefined_structure * undef_struct_ptr;
+#endif
+
+
+/* If you get an error about duplicate names,
+ * define NEED_SHORT_EXTERNAL_NAMES.
+ */
+
+#undef NEED_SHORT_EXTERNAL_NAMES
+
+#ifndef NEED_SHORT_EXTERNAL_NAMES
+
+int possibly_duplicate_function ()
+{
+ return 0;
+}
+
+int possibly_dupli_function ()
+{
+ return 1;
+}
+
+#endif
+
+
+
+/************************************************************************
+ * OK, that's it. You should not have to change anything beyond this
+ * point in order to compile and execute this program. (You might get
+ * some warnings, but you can ignore them.)
+ * When you run the program, it will make a couple more tests that it
+ * can do automatically, and then it will create jconfig.h and print out
+ * any additional suggestions it has.
+ ************************************************************************
+ */
+
+
+#ifdef HAVE_PROTOTYPES
+int is_char_signed (int arg)
+#else
+int is_char_signed (arg)
+ int arg;
+#endif
+{
+ if (arg == 189) { /* expected result for unsigned char */
+ return 0; /* type char is unsigned */
+ }
+ else if (arg != -67) { /* expected result for signed char */
+ printf("Hmm, it seems 'char' is not eight bits wide on your machine.\n");
+ printf("I fear the JPEG software will not work at all.\n\n");
+ }
+ return 1; /* assume char is signed otherwise */
+}
+
+
+#ifdef HAVE_PROTOTYPES
+int is_shifting_signed (long arg)
+#else
+int is_shifting_signed (arg)
+ long arg;
+#endif
+/* See whether right-shift on a long is signed or not. */
+{
+ long res = arg >> 4;
+
+ if (res == -0x7F7E80CL) { /* expected result for signed shift */
+ return 1; /* right shift is signed */
+ }
+ /* see if unsigned-shift hack will fix it. */
+ /* we can't just test exact value since it depends on width of long... */
+ res |= (~0L) << (32-4);
+ if (res == -0x7F7E80CL) { /* expected result now? */
+ return 0; /* right shift is unsigned */
+ }
+ printf("Right shift isn't acting as I expect it to.\n");
+ printf("I fear the JPEG software will not work at all.\n\n");
+ return 0; /* try it with unsigned anyway */
+}
+
+
+#ifdef HAVE_PROTOTYPES
+int main (int argc, char ** argv)
+#else
+int main (argc, argv)
+ int argc;
+ char ** argv;
+#endif
+{
+ char signed_char_check = (char) (-67);
+ FILE *outfile;
+
+ /* Attempt to write jconfig.h */
+ if ((outfile = fopen("jconfig.h", "w")) == NULL) {
+ printf("Failed to write jconfig.h\n");
+ return 1;
+ }
+
+ /* Write out all the info */
+ fprintf(outfile, "/* jconfig.h --- generated by ckconfig.c */\n");
+ fprintf(outfile, "/* see jconfig.doc for explanations */\n\n");
+#ifdef HAVE_PROTOTYPES
+ fprintf(outfile, "#define HAVE_PROTOTYPES\n");
+#else
+ fprintf(outfile, "#undef HAVE_PROTOTYPES\n");
+#endif
+#ifdef HAVE_UNSIGNED_CHAR
+ fprintf(outfile, "#define HAVE_UNSIGNED_CHAR\n");
+#else
+ fprintf(outfile, "#undef HAVE_UNSIGNED_CHAR\n");
+#endif
+#ifdef HAVE_UNSIGNED_SHORT
+ fprintf(outfile, "#define HAVE_UNSIGNED_SHORT\n");
+#else
+ fprintf(outfile, "#undef HAVE_UNSIGNED_SHORT\n");
+#endif
+#ifdef HAVE_VOID
+ fprintf(outfile, "/* #define void char */\n");
+#else
+ fprintf(outfile, "#define void char\n");
+#endif
+#ifdef HAVE_CONST
+ fprintf(outfile, "/* #define const */\n");
+#else
+ fprintf(outfile, "#define const\n");
+#endif
+ if (is_char_signed((int) signed_char_check))
+ fprintf(outfile, "#undef CHAR_IS_UNSIGNED\n");
+ else
+ fprintf(outfile, "#define CHAR_IS_UNSIGNED\n");
+#ifdef HAVE_STDDEF_H
+ fprintf(outfile, "#define HAVE_STDDEF_H\n");
+#else
+ fprintf(outfile, "#undef HAVE_STDDEF_H\n");
+#endif
+#ifdef HAVE_STDLIB_H
+ fprintf(outfile, "#define HAVE_STDLIB_H\n");
+#else
+ fprintf(outfile, "#undef HAVE_STDLIB_H\n");
+#endif
+#ifdef NEED_BSD_STRINGS
+ fprintf(outfile, "#define NEED_BSD_STRINGS\n");
+#else
+ fprintf(outfile, "#undef NEED_BSD_STRINGS\n");
+#endif
+#ifdef NEED_SYS_TYPES_H
+ fprintf(outfile, "#define NEED_SYS_TYPES_H\n");
+#else
+ fprintf(outfile, "#undef NEED_SYS_TYPES_H\n");
+#endif
+ fprintf(outfile, "#undef NEED_FAR_POINTERS\n");
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+ fprintf(outfile, "#define NEED_SHORT_EXTERNAL_NAMES\n");
+#else
+ fprintf(outfile, "#undef NEED_SHORT_EXTERNAL_NAMES\n");
+#endif
+#ifdef INCOMPLETE_TYPES_BROKEN
+ fprintf(outfile, "#define INCOMPLETE_TYPES_BROKEN\n");
+#else
+ fprintf(outfile, "#undef INCOMPLETE_TYPES_BROKEN\n");
+#endif
+ fprintf(outfile, "\n#ifdef JPEG_INTERNALS\n\n");
+ if (is_shifting_signed(-0x7F7E80B1L))
+ fprintf(outfile, "#undef RIGHT_SHIFT_IS_UNSIGNED\n");
+ else
+ fprintf(outfile, "#define RIGHT_SHIFT_IS_UNSIGNED\n");
+ fprintf(outfile, "\n#endif /* JPEG_INTERNALS */\n");
+ fprintf(outfile, "\n#ifdef JPEG_CJPEG_DJPEG\n\n");
+ fprintf(outfile, "#define BMP_SUPPORTED /* BMP image file format */\n");
+ fprintf(outfile, "#define GIF_SUPPORTED /* GIF image file format */\n");
+ fprintf(outfile, "#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */\n");
+ fprintf(outfile, "#undef RLE_SUPPORTED /* Utah RLE image file format */\n");
+ fprintf(outfile, "#define TARGA_SUPPORTED /* Targa image file format */\n\n");
+ fprintf(outfile, "#undef TWO_FILE_COMMANDLINE /* You may need this on non-Unix systems */\n");
+ fprintf(outfile, "#undef NEED_SIGNAL_CATCHER /* Define this if you use jmemname.c */\n");
+ fprintf(outfile, "#undef DONT_USE_B_MODE\n");
+ fprintf(outfile, "/* #define PROGRESS_REPORT */ /* optional */\n");
+ fprintf(outfile, "\n#endif /* JPEG_CJPEG_DJPEG */\n");
+
+ /* Close the jconfig.h file */
+ fclose(outfile);
+
+ /* User report */
+ printf("Configuration check for Independent JPEG Group's software done.\n");
+ printf("\nI have written the jconfig.h file for you.\n\n");
+#ifdef HAVE_PROTOTYPES
+ printf("You should use makefile.ansi as the starting point for your Makefile.\n");
+#else
+ printf("You should use makefile.unix as the starting point for your Makefile.\n");
+#endif
+
+#ifdef NEED_SPECIAL_INCLUDE
+ printf("\nYou'll need to change jconfig.h to include the system include file\n");
+ printf("that you found type size_t in, or add a direct definition of type\n");
+ printf("size_t if that's what you used. Just add it to the end.\n");
+#endif
+
+ return 0;
+}
diff --git a/test/monniaux/jpeg-6b/coderules.doc b/test/monniaux/jpeg-6b/coderules.doc
new file mode 100644
index 00000000..0ab5d9bd
--- /dev/null
+++ b/test/monniaux/jpeg-6b/coderules.doc
@@ -0,0 +1,118 @@
+IJG JPEG LIBRARY: CODING RULES
+
+Copyright (C) 1991-1996, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+Since numerous people will be contributing code and bug fixes, it's important
+to establish a common coding style. The goal of using similar coding styles
+is much more important than the details of just what that style is.
+
+In general we follow the recommendations of "Recommended C Style and Coding
+Standards" revision 6.1 (Cannon et al. as modified by Spencer, Keppel and
+Brader). This document is available in the IJG FTP archive (see
+jpeg/doc/cstyle.ms.tbl.Z, or cstyle.txt.Z for those without nroff/tbl).
+
+Block comments should be laid out thusly:
+
+/*
+ * Block comments in this style.
+ */
+
+We indent statements in K&R style, e.g.,
+ if (test) {
+ then-part;
+ } else {
+ else-part;
+ }
+with two spaces per indentation level. (This indentation convention is
+handled automatically by GNU Emacs and many other text editors.)
+
+Multi-word names should be written in lower case with underscores, e.g.,
+multi_word_name (not multiWordName). Preprocessor symbols and enum constants
+are similar but upper case (MULTI_WORD_NAME). Names should be unique within
+the first fifteen characters. (On some older systems, global names must be
+unique within six characters. We accommodate this without cluttering the
+source code by using macros to substitute shorter names.)
+
+We use function prototypes everywhere; we rely on automatic source code
+transformation to feed prototype-less C compilers. Transformation is done
+by the simple and portable tool 'ansi2knr.c' (courtesy of Ghostscript).
+ansi2knr is not very bright, so it imposes a format requirement on function
+declarations: the function name MUST BEGIN IN COLUMN 1. Thus all functions
+should be written in the following style:
+
+LOCAL(int *)
+function_name (int a, char *b)
+{
+ code...
+}
+
+Note that each function definition must begin with GLOBAL(type), LOCAL(type),
+or METHODDEF(type). These macros expand to "static type" or just "type" as
+appropriate. They provide a readable indication of the routine's usage and
+can readily be changed for special needs. (For instance, special linkage
+keywords can be inserted for use in Windows DLLs.)
+
+ansi2knr does not transform method declarations (function pointers in
+structs). We handle these with a macro JMETHOD, defined as
+ #ifdef HAVE_PROTOTYPES
+ #define JMETHOD(type,methodname,arglist) type (*methodname) arglist
+ #else
+ #define JMETHOD(type,methodname,arglist) type (*methodname) ()
+ #endif
+which is used like this:
+ struct function_pointers {
+ JMETHOD(void, init_entropy_encoder, (int somearg, jparms *jp));
+ JMETHOD(void, term_entropy_encoder, (void));
+ };
+Note the set of parentheses surrounding the parameter list.
+
+A similar solution is used for forward and external function declarations
+(see the EXTERN and JPP macros).
+
+If the code is to work on non-ANSI compilers, we cannot rely on a prototype
+declaration to coerce actual parameters into the right types. Therefore, use
+explicit casts on actual parameters whenever the actual parameter type is not
+identical to the formal parameter. Beware of implicit conversions to "int".
+
+It seems there are some non-ANSI compilers in which the sizeof() operator
+is defined to return int, yet size_t is defined as long. Needless to say,
+this is brain-damaged. Always use the SIZEOF() macro in place of sizeof(),
+so that the result is guaranteed to be of type size_t.
+
+
+The JPEG library is intended to be used within larger programs. Furthermore,
+we want it to be reentrant so that it can be used by applications that process
+multiple images concurrently. The following rules support these requirements:
+
+1. Avoid direct use of file I/O, "malloc", error report printouts, etc;
+pass these through the common routines provided.
+
+2. Minimize global namespace pollution. Functions should be declared static
+wherever possible. (Note that our method-based calling conventions help this
+a lot: in many modules only the initialization function will ever need to be
+called directly, so only that function need be externally visible.) All
+global function names should begin with "jpeg_", and should have an
+abbreviated name (unique in the first six characters) substituted by macro
+when NEED_SHORT_EXTERNAL_NAMES is set.
+
+3. Don't use global variables; anything that must be used in another module
+should be in the common data structures.
+
+4. Don't use static variables except for read-only constant tables. Variables
+that should be private to a module can be placed into private structures (see
+the system architecture document, structure.doc).
+
+5. Source file names should begin with "j" for files that are part of the
+library proper; source files that are not part of the library, such as cjpeg.c
+and djpeg.c, do not begin with "j". Keep source file names to eight
+characters (plus ".c" or ".h", etc) to make life easy for MS-DOSers. Keep
+compression and decompression code in separate source files --- some
+applications may want only one half of the library.
+
+Note: these rules (particularly #4) are not followed religiously in the
+modules that are used in cjpeg/djpeg but are not part of the JPEG library
+proper. Those modules are not really intended to be used in other
+applications.
diff --git a/test/monniaux/jpeg-6b/config.guess b/test/monniaux/jpeg-6b/config.guess
new file mode 100755
index 00000000..413ed41c
--- /dev/null
+++ b/test/monniaux/jpeg-6b/config.guess
@@ -0,0 +1,883 @@
+#! /bin/sh
+# Attempt to guess a canonical system name.
+# Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
+#
+# This file is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# Written by Per Bothner <bothner@cygnus.com>.
+# The master version of this file is at the FSF in /home/gd/gnu/lib.
+#
+# This script attempts to guess a canonical system name similar to
+# config.sub. If it succeeds, it prints the system name on stdout, and
+# exits with 0. Otherwise, it exits with 1.
+#
+# The plan is that this can be called by configure scripts if you
+# don't specify an explicit system type (host/target name).
+#
+# Only a few systems have been added to this list; please add others
+# (but try to keep the structure clean).
+#
+
+# This is needed to find uname on a Pyramid OSx when run in the BSD universe.
+# (ghazi@noc.rutgers.edu 8/24/94.)
+if (test -f /.attbin/uname) >/dev/null 2>&1 ; then
+ PATH=$PATH:/.attbin ; export PATH
+fi
+
+UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
+UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
+UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown
+UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown
+
+trap 'rm -f dummy.c dummy.o dummy; exit 1' 1 2 15
+
+# Note: order is significant - the case branches are not exclusive.
+
+case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
+ alpha:OSF1:*:*)
+ if test $UNAME_RELEASE = "V4.0"; then
+ UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'`
+ fi
+ # A Vn.n version is a released version.
+ # A Tn.n version is a released field test version.
+ # A Xn.n version is an unreleased experimental baselevel.
+ # 1.2 uses "1.2" for uname -r.
+ cat <<EOF >dummy.s
+ .globl main
+ .ent main
+main:
+ .frame \$30,0,\$26,0
+ .prologue 0
+ .long 0x47e03d80 # implver $0
+ lda \$2,259
+ .long 0x47e20c21 # amask $2,$1
+ srl \$1,8,\$2
+ sll \$2,2,\$2
+ sll \$0,3,\$0
+ addl \$1,\$0,\$0
+ addl \$2,\$0,\$0
+ ret \$31,(\$26),1
+ .end main
+EOF
+ ${CC-cc} dummy.s -o dummy 2>/dev/null
+ if test "$?" = 0 ; then
+ ./dummy
+ case "$?" in
+ 7)
+ UNAME_MACHINE="alpha"
+ ;;
+ 15)
+ UNAME_MACHINE="alphaev5"
+ ;;
+ 14)
+ UNAME_MACHINE="alphaev56"
+ ;;
+ 10)
+ UNAME_MACHINE="alphapca56"
+ ;;
+ 16)
+ UNAME_MACHINE="alphaev6"
+ ;;
+ esac
+ fi
+ rm -f dummy.s dummy
+ echo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[VTX]//' | tr [[A-Z]] [[a-z]]`
+ exit 0 ;;
+ 21064:Windows_NT:50:3)
+ echo alpha-dec-winnt3.5
+ exit 0 ;;
+ Amiga*:UNIX_System_V:4.0:*)
+ echo m68k-cbm-sysv4
+ exit 0;;
+ amiga:NetBSD:*:*)
+ echo m68k-cbm-netbsd${UNAME_RELEASE}
+ exit 0 ;;
+ amiga:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ arc64:OpenBSD:*:*)
+ echo mips64el-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ arc:OpenBSD:*:*)
+ echo mipsel-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ hkmips:OpenBSD:*:*)
+ echo mips-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ pmax:OpenBSD:*:*)
+ echo mipsel-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ sgi:OpenBSD:*:*)
+ echo mips-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ wgrisc:OpenBSD:*:*)
+ echo mipsel-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*)
+ echo arm-acorn-riscix${UNAME_RELEASE}
+ exit 0;;
+ arm32:NetBSD:*:*)
+ echo arm-unknown-netbsd`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'`
+ exit 0 ;;
+ SR2?01:HI-UX/MPP:*:*)
+ echo hppa1.1-hitachi-hiuxmpp
+ exit 0;;
+ Pyramid*:OSx*:*:*|MIS*:OSx*:*:*)
+ # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE.
+ if test "`(/bin/universe) 2>/dev/null`" = att ; then
+ echo pyramid-pyramid-sysv3
+ else
+ echo pyramid-pyramid-bsd
+ fi
+ exit 0 ;;
+ NILE:*:*:dcosx)
+ echo pyramid-pyramid-svr4
+ exit 0 ;;
+ sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*)
+ echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ exit 0 ;;
+ i86pc:SunOS:5.*:*)
+ echo i386-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ exit 0 ;;
+ sun4*:SunOS:6*:*)
+ # According to config.sub, this is the proper way to canonicalize
+ # SunOS6. Hard to guess exactly what SunOS6 will be like, but
+ # it's likely to be more like Solaris than SunOS4.
+ echo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ exit 0 ;;
+ sun4*:SunOS:*:*)
+ case "`/usr/bin/arch -k`" in
+ Series*|S4*)
+ UNAME_RELEASE=`uname -v`
+ ;;
+ esac
+ # Japanese Language versions have a version number like `4.1.3-JL'.
+ echo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'`
+ exit 0 ;;
+ sun3*:SunOS:*:*)
+ echo m68k-sun-sunos${UNAME_RELEASE}
+ exit 0 ;;
+ sun*:*:4.2BSD:*)
+ UNAME_RELEASE=`(head -1 /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null`
+ test "x${UNAME_RELEASE}" = "x" && UNAME_RELEASE=3
+ case "`/bin/arch`" in
+ sun3)
+ echo m68k-sun-sunos${UNAME_RELEASE}
+ ;;
+ sun4)
+ echo sparc-sun-sunos${UNAME_RELEASE}
+ ;;
+ esac
+ exit 0 ;;
+ aushp:SunOS:*:*)
+ echo sparc-auspex-sunos${UNAME_RELEASE}
+ exit 0 ;;
+ atari*:NetBSD:*:*)
+ echo m68k-atari-netbsd${UNAME_RELEASE}
+ exit 0 ;;
+ atari*:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ sun3*:NetBSD:*:*)
+ echo m68k-sun-netbsd${UNAME_RELEASE}
+ exit 0 ;;
+ sun3*:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ mac68k:NetBSD:*:*)
+ echo m68k-apple-netbsd${UNAME_RELEASE}
+ exit 0 ;;
+ mac68k:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ mvme68k:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ mvme88k:OpenBSD:*:*)
+ echo m88k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ powerpc:machten:*:*)
+ echo powerpc-apple-machten${UNAME_RELEASE}
+ exit 0 ;;
+ RISC*:Mach:*:*)
+ echo mips-dec-mach_bsd4.3
+ exit 0 ;;
+ RISC*:ULTRIX:*:*)
+ echo mips-dec-ultrix${UNAME_RELEASE}
+ exit 0 ;;
+ VAX*:ULTRIX*:*:*)
+ echo vax-dec-ultrix${UNAME_RELEASE}
+ exit 0 ;;
+ 2020:CLIX:*:*)
+ echo clipper-intergraph-clix${UNAME_RELEASE}
+ exit 0 ;;
+ mips:*:*:UMIPS | mips:*:*:RISCos)
+ sed 's/^ //' << EOF >dummy.c
+ int main (argc, argv) int argc; char **argv; {
+ #if defined (host_mips) && defined (MIPSEB)
+ #if defined (SYSTYPE_SYSV)
+ printf ("mips-mips-riscos%ssysv\n", argv[1]); exit (0);
+ #endif
+ #if defined (SYSTYPE_SVR4)
+ printf ("mips-mips-riscos%ssvr4\n", argv[1]); exit (0);
+ #endif
+ #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD)
+ printf ("mips-mips-riscos%sbsd\n", argv[1]); exit (0);
+ #endif
+ #endif
+ exit (-1);
+ }
+EOF
+ ${CC-cc} dummy.c -o dummy \
+ && ./dummy `echo "${UNAME_RELEASE}" | sed -n 's/\([0-9]*\).*/\1/p'` \
+ && rm dummy.c dummy && exit 0
+ rm -f dummy.c dummy
+ echo mips-mips-riscos${UNAME_RELEASE}
+ exit 0 ;;
+ Night_Hawk:Power_UNIX:*:*)
+ echo powerpc-harris-powerunix
+ exit 0 ;;
+ m88k:CX/UX:7*:*)
+ echo m88k-harris-cxux7
+ exit 0 ;;
+ m88k:*:4*:R4*)
+ echo m88k-motorola-sysv4
+ exit 0 ;;
+ m88k:*:3*:R3*)
+ echo m88k-motorola-sysv3
+ exit 0 ;;
+ AViiON:dgux:*:*)
+ # DG/UX returns AViiON for all architectures
+ UNAME_PROCESSOR=`/usr/bin/uname -p`
+ if [ $UNAME_PROCESSOR = mc88100 -o $UNAME_PROCESSOR = mc88110 ] ; then
+ if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx \
+ -o ${TARGET_BINARY_INTERFACE}x = x ] ; then
+ echo m88k-dg-dgux${UNAME_RELEASE}
+ else
+ echo m88k-dg-dguxbcs${UNAME_RELEASE}
+ fi
+ else echo i586-dg-dgux${UNAME_RELEASE}
+ fi
+ exit 0 ;;
+ M88*:DolphinOS:*:*) # DolphinOS (SVR3)
+ echo m88k-dolphin-sysv3
+ exit 0 ;;
+ M88*:*:R3*:*)
+ # Delta 88k system running SVR3
+ echo m88k-motorola-sysv3
+ exit 0 ;;
+ XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3)
+ echo m88k-tektronix-sysv3
+ exit 0 ;;
+ Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD)
+ echo m68k-tektronix-bsd
+ exit 0 ;;
+ *:IRIX*:*:*)
+ echo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'`
+ exit 0 ;;
+ ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX.
+ echo romp-ibm-aix # uname -m gives an 8 hex-code CPU id
+ exit 0 ;; # Note that: echo "'`uname -s`'" gives 'AIX '
+ i?86:AIX:*:*)
+ echo i386-ibm-aix
+ exit 0 ;;
+ *:AIX:2:3)
+ if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then
+ sed 's/^ //' << EOF >dummy.c
+ #include <sys/systemcfg.h>
+
+ main()
+ {
+ if (!__power_pc())
+ exit(1);
+ puts("powerpc-ibm-aix3.2.5");
+ exit(0);
+ }
+EOF
+ ${CC-cc} dummy.c -o dummy && ./dummy && rm dummy.c dummy && exit 0
+ rm -f dummy.c dummy
+ echo rs6000-ibm-aix3.2.5
+ elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then
+ echo rs6000-ibm-aix3.2.4
+ else
+ echo rs6000-ibm-aix3.2
+ fi
+ exit 0 ;;
+ *:AIX:*:4)
+ if /usr/sbin/lsattr -EHl proc0 | grep POWER >/dev/null 2>&1; then
+ IBM_ARCH=rs6000
+ else
+ IBM_ARCH=powerpc
+ fi
+ if [ -x /usr/bin/oslevel ] ; then
+ IBM_REV=`/usr/bin/oslevel`
+ else
+ IBM_REV=4.${UNAME_RELEASE}
+ fi
+ echo ${IBM_ARCH}-ibm-aix${IBM_REV}
+ exit 0 ;;
+ *:AIX:*:*)
+ echo rs6000-ibm-aix
+ exit 0 ;;
+ ibmrt:4.4BSD:*|romp-ibm:BSD:*)
+ echo romp-ibm-bsd4.4
+ exit 0 ;;
+ ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC NetBSD and
+ echo romp-ibm-bsd${UNAME_RELEASE} # 4.3 with uname added to
+ exit 0 ;; # report: romp-ibm BSD 4.3
+ *:BOSX:*:*)
+ echo rs6000-bull-bosx
+ exit 0 ;;
+ DPX/2?00:B.O.S.:*:*)
+ echo m68k-bull-sysv3
+ exit 0 ;;
+ 9000/[34]??:4.3bsd:1.*:*)
+ echo m68k-hp-bsd
+ exit 0 ;;
+ hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*)
+ echo m68k-hp-bsd4.4
+ exit 0 ;;
+ 9000/[3478]??:HP-UX:*:*)
+ case "${UNAME_MACHINE}" in
+ 9000/31? ) HP_ARCH=m68000 ;;
+ 9000/[34]?? ) HP_ARCH=m68k ;;
+ 9000/7?? | 9000/8?[1679] ) HP_ARCH=hppa1.1 ;;
+ 9000/8?? ) HP_ARCH=hppa1.0 ;;
+ esac
+ HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`
+ echo ${HP_ARCH}-hp-hpux${HPUX_REV}
+ exit 0 ;;
+ 3050*:HI-UX:*:*)
+ sed 's/^ //' << EOF >dummy.c
+ #include <unistd.h>
+ int
+ main ()
+ {
+ long cpu = sysconf (_SC_CPU_VERSION);
+ /* The order matters, because CPU_IS_HP_MC68K erroneously returns
+ true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct
+ results, however. */
+ if (CPU_IS_PA_RISC (cpu))
+ {
+ switch (cpu)
+ {
+ case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break;
+ case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break;
+ case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break;
+ default: puts ("hppa-hitachi-hiuxwe2"); break;
+ }
+ }
+ else if (CPU_IS_HP_MC68K (cpu))
+ puts ("m68k-hitachi-hiuxwe2");
+ else puts ("unknown-hitachi-hiuxwe2");
+ exit (0);
+ }
+EOF
+ ${CC-cc} dummy.c -o dummy && ./dummy && rm dummy.c dummy && exit 0
+ rm -f dummy.c dummy
+ echo unknown-hitachi-hiuxwe2
+ exit 0 ;;
+ 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* )
+ echo hppa1.1-hp-bsd
+ exit 0 ;;
+ 9000/8??:4.3bsd:*:*)
+ echo hppa1.0-hp-bsd
+ exit 0 ;;
+ hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* )
+ echo hppa1.1-hp-osf
+ exit 0 ;;
+ hp8??:OSF1:*:*)
+ echo hppa1.0-hp-osf
+ exit 0 ;;
+ i?86:OSF1:*:*)
+ if [ -x /usr/sbin/sysversion ] ; then
+ echo ${UNAME_MACHINE}-unknown-osf1mk
+ else
+ echo ${UNAME_MACHINE}-unknown-osf1
+ fi
+ exit 0 ;;
+ parisc*:Lites*:*:*)
+ echo hppa1.1-hp-lites
+ exit 0 ;;
+ C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*)
+ echo c1-convex-bsd
+ exit 0 ;;
+ C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*)
+ if getsysinfo -f scalar_acc
+ then echo c32-convex-bsd
+ else echo c2-convex-bsd
+ fi
+ exit 0 ;;
+ C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*)
+ echo c34-convex-bsd
+ exit 0 ;;
+ C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*)
+ echo c38-convex-bsd
+ exit 0 ;;
+ C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*)
+ echo c4-convex-bsd
+ exit 0 ;;
+ CRAY*X-MP:*:*:*)
+ echo xmp-cray-unicos
+ exit 0 ;;
+ CRAY*Y-MP:*:*:*)
+ echo ymp-cray-unicos${UNAME_RELEASE}
+ exit 0 ;;
+ CRAY*[A-Z]90:*:*:*)
+ echo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \
+ | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \
+ -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
+ exit 0 ;;
+ CRAY*TS:*:*:*)
+ echo t90-cray-unicos${UNAME_RELEASE}
+ exit 0 ;;
+ CRAY-2:*:*:*)
+ echo cray2-cray-unicos
+ exit 0 ;;
+ F300:UNIX_System_V:*:*)
+ FUJITSU_SYS=`uname -p | tr [A-Z] [a-z] | sed -e 's/\///'`
+ FUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'`
+ echo "f300-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}"
+ exit 0 ;;
+ F301:UNIX_System_V:*:*)
+ echo f301-fujitsu-uxpv`echo $UNAME_RELEASE | sed 's/ .*//'`
+ exit 0 ;;
+ hp3[0-9][05]:NetBSD:*:*)
+ echo m68k-hp-netbsd${UNAME_RELEASE}
+ exit 0 ;;
+ hp300:OpenBSD:*:*)
+ echo m68k-unknown-openbsd${UNAME_RELEASE}
+ exit 0 ;;
+ i?86:BSD/386:*:* | *:BSD/OS:*:*)
+ echo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE}
+ exit 0 ;;
+ *:FreeBSD:*:*)
+ echo ${UNAME_MACHINE}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`
+ exit 0 ;;
+ *:NetBSD:*:*)
+ echo ${UNAME_MACHINE}-unknown-netbsd`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'`
+ exit 0 ;;
+ *:OpenBSD:*:*)
+ echo ${UNAME_MACHINE}-unknown-openbsd`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'`
+ exit 0 ;;
+ i*:CYGWIN*:*)
+ echo i386-pc-cygwin32
+ exit 0 ;;
+ i*:MINGW*:*)
+ echo i386-pc-mingw32
+ exit 0 ;;
+ p*:CYGWIN*:*)
+ echo powerpcle-unknown-cygwin32
+ exit 0 ;;
+ prep*:SunOS:5.*:*)
+ echo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ exit 0 ;;
+ *:GNU:*:*)
+ echo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-gnu`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'`
+ exit 0 ;;
+ *:Linux:*:*)
+ # The BFD linker knows what the default object file format is, so
+ # first see if it will tell us.
+ ld_help_string=`ld --help 2>&1`
+ ld_supported_emulations=`echo $ld_help_string \
+ | sed -ne '/supported emulations:/!d
+ s/[ ][ ]*/ /g
+ s/.*supported emulations: *//
+ s/ .*//
+ p'`
+ case "$ld_supported_emulations" in
+ i?86linux) echo "${UNAME_MACHINE}-pc-linux-gnuaout" ; exit 0 ;;
+ i?86coff) echo "${UNAME_MACHINE}-pc-linux-gnucoff" ; exit 0 ;;
+ sparclinux) echo "${UNAME_MACHINE}-unknown-linux-gnuaout" ; exit 0 ;;
+ m68klinux) echo "${UNAME_MACHINE}-unknown-linux-gnuaout" ; exit 0 ;;
+ elf32ppc) echo "powerpc-unknown-linux-gnu" ; exit 0 ;;
+ esac
+
+ if test "${UNAME_MACHINE}" = "alpha" ; then
+ sed 's/^ //' <<EOF >dummy.s
+ .globl main
+ .ent main
+ main:
+ .frame \$30,0,\$26,0
+ .prologue 0
+ .long 0x47e03d80 # implver $0
+ lda \$2,259
+ .long 0x47e20c21 # amask $2,$1
+ srl \$1,8,\$2
+ sll \$2,2,\$2
+ sll \$0,3,\$0
+ addl \$1,\$0,\$0
+ addl \$2,\$0,\$0
+ ret \$31,(\$26),1
+ .end main
+EOF
+ LIBC=""
+ ${CC-cc} dummy.s -o dummy 2>/dev/null
+ if test "$?" = 0 ; then
+ ./dummy
+ case "$?" in
+ 7)
+ UNAME_MACHINE="alpha"
+ ;;
+ 15)
+ UNAME_MACHINE="alphaev5"
+ ;;
+ 14)
+ UNAME_MACHINE="alphaev56"
+ ;;
+ 10)
+ UNAME_MACHINE="alphapca56"
+ ;;
+ 16)
+ UNAME_MACHINE="alphaev6"
+ ;;
+ esac
+
+ objdump --private-headers dummy | \
+ grep ld.so.1 > /dev/null
+ if test "$?" = 0 ; then
+ LIBC="libc1"
+ fi
+ fi
+ rm -f dummy.s dummy
+ echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC} ; exit 0
+ elif test "${UNAME_MACHINE}" = "mips" ; then
+ cat >dummy.c <<EOF
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+#ifdef __MIPSEB__
+ printf ("%s-unknown-linux-gnu\n", argv[1]);
+#endif
+#ifdef __MIPSEL__
+ printf ("%sel-unknown-linux-gnu\n", argv[1]);
+#endif
+ return 0;
+}
+EOF
+ ${CC-cc} dummy.c -o dummy 2>/dev/null && ./dummy "${UNAME_MACHINE}" && rm dummy.c dummy && exit 0
+ rm -f dummy.c dummy
+ else
+ # Either a pre-BFD a.out linker (linux-gnuoldld)
+ # or one that does not give us useful --help.
+ # GCC wants to distinguish between linux-gnuoldld and linux-gnuaout.
+ # If ld does not provide *any* "supported emulations:"
+ # that means it is gnuoldld.
+ echo "$ld_help_string" | grep >/dev/null 2>&1 "supported emulations:"
+ test $? != 0 && echo "${UNAME_MACHINE}-pc-linux-gnuoldld" && exit 0
+
+ case "${UNAME_MACHINE}" in
+ i?86)
+ VENDOR=pc;
+ ;;
+ *)
+ VENDOR=unknown;
+ ;;
+ esac
+ # Determine whether the default compiler is a.out or elf
+ cat >dummy.c <<EOF
+#include <features.h>
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+#ifdef __ELF__
+# ifdef __GLIBC__
+# if __GLIBC__ >= 2
+ printf ("%s-${VENDOR}-linux-gnu\n", argv[1]);
+# else
+ printf ("%s-${VENDOR}-linux-gnulibc1\n", argv[1]);
+# endif
+# else
+ printf ("%s-${VENDOR}-linux-gnulibc1\n", argv[1]);
+# endif
+#else
+ printf ("%s-${VENDOR}-linux-gnuaout\n", argv[1]);
+#endif
+ return 0;
+}
+EOF
+ ${CC-cc} dummy.c -o dummy 2>/dev/null && ./dummy "${UNAME_MACHINE}" && rm dummy.c dummy && exit 0
+ rm -f dummy.c dummy
+ fi ;;
+# ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. earlier versions
+# are messed up and put the nodename in both sysname and nodename.
+ i?86:DYNIX/ptx:4*:*)
+ echo i386-sequent-sysv4
+ exit 0 ;;
+ i?86:UNIX_SV:4.2MP:2.*)
+ # Unixware is an offshoot of SVR4, but it has its own version
+ # number series starting with 2...
+ # I am not positive that other SVR4 systems won't match this,
+ # I just have to hope. -- rms.
+ # Use sysv4.2uw... so that sysv4* matches it.
+ echo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION}
+ exit 0 ;;
+ i?86:*:4.*:* | i?86:SYSTEM_V:4.*:*)
+ if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then
+ echo ${UNAME_MACHINE}-univel-sysv${UNAME_RELEASE}
+ else
+ echo ${UNAME_MACHINE}-pc-sysv${UNAME_RELEASE}
+ fi
+ exit 0 ;;
+ i?86:*:3.2:*)
+ if test -f /usr/options/cb.name; then
+ UNAME_REL=`sed -n 's/.*Version //p' </usr/options/cb.name`
+ echo ${UNAME_MACHINE}-pc-isc$UNAME_REL
+ elif /bin/uname -X 2>/dev/null >/dev/null ; then
+ UNAME_REL=`(/bin/uname -X|egrep Release|sed -e 's/.*= //')`
+ (/bin/uname -X|egrep i80486 >/dev/null) && UNAME_MACHINE=i486
+ (/bin/uname -X|egrep '^Machine.*Pentium' >/dev/null) \
+ && UNAME_MACHINE=i586
+ echo ${UNAME_MACHINE}-pc-sco$UNAME_REL
+ else
+ echo ${UNAME_MACHINE}-pc-sysv32
+ fi
+ exit 0 ;;
+ pc:*:*:*)
+ # uname -m prints for DJGPP always 'pc', but it prints nothing about
+ # the processor, so we play safe by assuming i386.
+ echo i386-pc-msdosdjgpp
+ exit 0 ;;
+ Intel:Mach:3*:*)
+ echo i386-pc-mach3
+ exit 0 ;;
+ paragon:*:*:*)
+ echo i860-intel-osf1
+ exit 0 ;;
+ i860:*:4.*:*) # i860-SVR4
+ if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then
+ echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4
+ else # Add other i860-SVR4 vendors below as they are discovered.
+ echo i860-unknown-sysv${UNAME_RELEASE} # Unknown i860-SVR4
+ fi
+ exit 0 ;;
+ mini*:CTIX:SYS*5:*)
+ # "miniframe"
+ echo m68010-convergent-sysv
+ exit 0 ;;
+ M68*:*:R3V[567]*:*)
+ test -r /sysV68 && echo 'm68k-motorola-sysv' && exit 0 ;;
+ 3[34]??:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 4850:*:4.0:3.0)
+ OS_REL=''
+ test -r /etc/.relid \
+ && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid`
+ /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+ && echo i486-ncr-sysv4.3${OS_REL} && exit 0
+ /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \
+ && echo i586-ncr-sysv4.3${OS_REL} && exit 0 ;;
+ 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*)
+ /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+ && echo i486-ncr-sysv4 && exit 0 ;;
+ m68*:LynxOS:2.*:*)
+ echo m68k-unknown-lynxos${UNAME_RELEASE}
+ exit 0 ;;
+ mc68030:UNIX_System_V:4.*:*)
+ echo m68k-atari-sysv4
+ exit 0 ;;
+ i?86:LynxOS:2.*:*)
+ echo i386-unknown-lynxos${UNAME_RELEASE}
+ exit 0 ;;
+ TSUNAMI:LynxOS:2.*:*)
+ echo sparc-unknown-lynxos${UNAME_RELEASE}
+ exit 0 ;;
+ rs6000:LynxOS:2.*:* | PowerPC:LynxOS:2.*:*)
+ echo rs6000-unknown-lynxos${UNAME_RELEASE}
+ exit 0 ;;
+ SM[BE]S:UNIX_SV:*:*)
+ echo mips-dde-sysv${UNAME_RELEASE}
+ exit 0 ;;
+ RM*:SINIX-*:*:*)
+ echo mips-sni-sysv4
+ exit 0 ;;
+ *:SINIX-*:*:*)
+ if uname -p 2>/dev/null >/dev/null ; then
+ UNAME_MACHINE=`(uname -p) 2>/dev/null`
+ echo ${UNAME_MACHINE}-sni-sysv4
+ else
+ echo ns32k-sni-sysv
+ fi
+ exit 0 ;;
+ PENTIUM:CPunix:4.0*:*) # Unisys `ClearPath HMP IX 4000' SVR4/MP effort
+ # says <Richard.M.Bartel@ccMail.Census.GOV>
+ echo i586-unisys-sysv4
+ exit 0 ;;
+ *:UNIX_System_V:4*:FTX*)
+ # From Gerald Hewes <hewes@openmarket.com>.
+ # How about differentiating between stratus architectures? -djm
+ echo hppa1.1-stratus-sysv4
+ exit 0 ;;
+ *:*:*:FTX*)
+ # From seanf@swdc.stratus.com.
+ echo i860-stratus-sysv4
+ exit 0 ;;
+ mc68*:A/UX:*:*)
+ echo m68k-apple-aux${UNAME_RELEASE}
+ exit 0 ;;
+ news*:NEWS-OS:*:6*)
+ echo mips-sony-newsos6
+ exit 0 ;;
+ R3000:*System_V*:*:* | R4000:UNIX_SYSV:*:*)
+ if [ -d /usr/nec ]; then
+ echo mips-nec-sysv${UNAME_RELEASE}
+ else
+ echo mips-unknown-sysv${UNAME_RELEASE}
+ fi
+ exit 0 ;;
+esac
+
+#echo '(No uname command or uname output not recognized.)' 1>&2
+#echo "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" 1>&2
+
+cat >dummy.c <<EOF
+#ifdef _SEQUENT_
+# include <sys/types.h>
+# include <sys/utsname.h>
+#endif
+main ()
+{
+#if defined (sony)
+#if defined (MIPSEB)
+ /* BFD wants "bsd" instead of "newsos". Perhaps BFD should be changed,
+ I don't know.... */
+ printf ("mips-sony-bsd\n"); exit (0);
+#else
+#include <sys/param.h>
+ printf ("m68k-sony-newsos%s\n",
+#ifdef NEWSOS4
+ "4"
+#else
+ ""
+#endif
+ ); exit (0);
+#endif
+#endif
+
+#if defined (__arm) && defined (__acorn) && defined (__unix)
+ printf ("arm-acorn-riscix"); exit (0);
+#endif
+
+#if defined (hp300) && !defined (hpux)
+ printf ("m68k-hp-bsd\n"); exit (0);
+#endif
+
+#if defined (NeXT)
+#if !defined (__ARCHITECTURE__)
+#define __ARCHITECTURE__ "m68k"
+#endif
+ int version;
+ version=`(hostinfo | sed -n 's/.*NeXT Mach \([0-9]*\).*/\1/p') 2>/dev/null`;
+ printf ("%s-next-nextstep%d\n", __ARCHITECTURE__, version);
+ exit (0);
+#endif
+
+#if defined (MULTIMAX) || defined (n16)
+#if defined (UMAXV)
+ printf ("ns32k-encore-sysv\n"); exit (0);
+#else
+#if defined (CMU)
+ printf ("ns32k-encore-mach\n"); exit (0);
+#else
+ printf ("ns32k-encore-bsd\n"); exit (0);
+#endif
+#endif
+#endif
+
+#if defined (__386BSD__)
+ printf ("i386-pc-bsd\n"); exit (0);
+#endif
+
+#if defined (sequent)
+#if defined (i386)
+ printf ("i386-sequent-dynix\n"); exit (0);
+#endif
+#if defined (ns32000)
+ printf ("ns32k-sequent-dynix\n"); exit (0);
+#endif
+#endif
+
+#if defined (_SEQUENT_)
+ struct utsname un;
+
+ uname(&un);
+
+ if (strncmp(un.version, "V2", 2) == 0) {
+ printf ("i386-sequent-ptx2\n"); exit (0);
+ }
+ if (strncmp(un.version, "V1", 2) == 0) { /* XXX is V1 correct? */
+ printf ("i386-sequent-ptx1\n"); exit (0);
+ }
+ printf ("i386-sequent-ptx\n"); exit (0);
+
+#endif
+
+#if defined (vax)
+#if !defined (ultrix)
+ printf ("vax-dec-bsd\n"); exit (0);
+#else
+ printf ("vax-dec-ultrix\n"); exit (0);
+#endif
+#endif
+
+#if defined (alliant) && defined (i860)
+ printf ("i860-alliant-bsd\n"); exit (0);
+#endif
+
+ exit (1);
+}
+EOF
+
+${CC-cc} dummy.c -o dummy 2>/dev/null && ./dummy && rm dummy.c dummy && exit 0
+rm -f dummy.c dummy
+
+# Apollos put the system type in the environment.
+
+test -d /usr/apollo && { echo ${ISP}-apollo-${SYSTYPE}; exit 0; }
+
+# Convex versions that predate uname can use getsysinfo(1)
+
+if [ -x /usr/convex/getsysinfo ]
+then
+ case `getsysinfo -f cpu_type` in
+ c1*)
+ echo c1-convex-bsd
+ exit 0 ;;
+ c2*)
+ if getsysinfo -f scalar_acc
+ then echo c32-convex-bsd
+ else echo c2-convex-bsd
+ fi
+ exit 0 ;;
+ c34*)
+ echo c34-convex-bsd
+ exit 0 ;;
+ c38*)
+ echo c38-convex-bsd
+ exit 0 ;;
+ c4*)
+ echo c4-convex-bsd
+ exit 0 ;;
+ esac
+fi
+
+#echo '(Unable to guess system type)' 1>&2
+
+exit 1
diff --git a/test/monniaux/jpeg-6b/config.sub b/test/monniaux/jpeg-6b/config.sub
new file mode 100755
index 00000000..213a6d47
--- /dev/null
+++ b/test/monniaux/jpeg-6b/config.sub
@@ -0,0 +1,954 @@
+#! /bin/sh
+# Configuration validation subroutine script, version 1.1.
+# Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
+# This file is (in principle) common to ALL GNU software.
+# The presence of a machine in this file suggests that SOME GNU software
+# can handle that machine. It does not imply ALL GNU software can.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# Configuration subroutine to validate and canonicalize a configuration type.
+# Supply the specified configuration type as an argument.
+# If it is invalid, we print an error message on stderr and exit with code 1.
+# Otherwise, we print the canonical config type on stdout and succeed.
+
+# This file is supposed to be the same for all GNU packages
+# and recognize all the CPU types, system types and aliases
+# that are meaningful with *any* GNU software.
+# Each package is responsible for reporting which valid configurations
+# it does not support. The user should be able to distinguish
+# a failure to support a valid configuration from a meaningless
+# configuration.
+
+# The goal of this file is to map all the various variations of a given
+# machine specification into a single specification in the form:
+# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM
+# or in some cases, the newer four-part form:
+# CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM
+# It is wrong to echo any other type of specification.
+
+if [ x$1 = x ]
+then
+ echo Configuration name missing. 1>&2
+ echo "Usage: $0 CPU-MFR-OPSYS" 1>&2
+ echo "or $0 ALIAS" 1>&2
+ echo where ALIAS is a recognized configuration type. 1>&2
+ exit 1
+fi
+
+# First pass through any local machine types.
+case $1 in
+ *local*)
+ echo $1
+ exit 0
+ ;;
+ *)
+ ;;
+esac
+
+# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any).
+# Here we must recognize all the valid KERNEL-OS combinations.
+maybe_os=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'`
+case $maybe_os in
+ linux-gnu*)
+ os=-$maybe_os
+ basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`
+ ;;
+ *)
+ basic_machine=`echo $1 | sed 's/-[^-]*$//'`
+ if [ $basic_machine != $1 ]
+ then os=`echo $1 | sed 's/.*-/-/'`
+ else os=; fi
+ ;;
+esac
+
+### Let's recognize common machines as not being operating systems so
+### that things like config.sub decstation-3100 work. We also
+### recognize some manufacturers as not being operating systems, so we
+### can provide default operating systems below.
+case $os in
+ -sun*os*)
+ # Prevent following clause from handling this invalid input.
+ ;;
+ -dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \
+ -att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \
+ -unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \
+ -convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\
+ -c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \
+ -harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \
+ -apple)
+ os=
+ basic_machine=$1
+ ;;
+ -hiux*)
+ os=-hiuxwe2
+ ;;
+ -sco5)
+ os=sco3.2v5
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -sco4)
+ os=-sco3.2v4
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -sco3.2.[4-9]*)
+ os=`echo $os | sed -e 's/sco3.2./sco3.2v/'`
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -sco3.2v[4-9]*)
+ # Don't forget version if it is 3.2v4 or newer.
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -sco*)
+ os=-sco3.2v2
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -isc)
+ os=-isc2.2
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -clix*)
+ basic_machine=clipper-intergraph
+ ;;
+ -isc*)
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+ ;;
+ -lynx*)
+ os=-lynxos
+ ;;
+ -ptx*)
+ basic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'`
+ ;;
+ -windowsnt*)
+ os=`echo $os | sed -e 's/windowsnt/winnt/'`
+ ;;
+ -psos*)
+ os=-psos
+ ;;
+esac
+
+# Decode aliases for certain CPU-COMPANY combinations.
+case $basic_machine in
+ # Recognize the basic CPU types without company name.
+ # Some are omitted here because they have special meanings below.
+ tahoe | i860 | m32r | m68k | m68000 | m88k | ns32k | arc | arm \
+ | arme[lb] | pyramid | mn10200 | mn10300 \
+ | tron | a29k | 580 | i960 | h8300 | hppa | hppa1.0 | hppa1.1 \
+ | alpha | alphaev5 | alphaev56 | we32k | ns16k | clipper \
+ | i370 | sh | powerpc | powerpcle | 1750a | dsp16xx | pdp11 \
+ | mips64 | mipsel | mips64el | mips64orion | mips64orionel \
+ | mipstx39 | mipstx39el \
+ | sparc | sparclet | sparclite | sparc64 | v850)
+ basic_machine=$basic_machine-unknown
+ ;;
+ # We use `pc' rather than `unknown'
+ # because (1) that's what they normally are, and
+ # (2) the word "unknown" tends to confuse beginning users.
+ i[3456]86)
+ basic_machine=$basic_machine-pc
+ ;;
+ # Object if more than one company name word.
+ *-*-*)
+ echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
+ exit 1
+ ;;
+ # Recognize the basic CPU types with company name.
+ vax-* | tahoe-* | i[3456]86-* | i860-* | m32r-* | m68k-* | m68000-* \
+ | m88k-* | sparc-* | ns32k-* | fx80-* | arc-* | arm-* | c[123]* \
+ | mips-* | pyramid-* | tron-* | a29k-* | romp-* | rs6000-* \
+ | power-* | none-* | 580-* | cray2-* | h8300-* | i960-* \
+ | xmp-* | ymp-* | hppa-* | hppa1.0-* | hppa1.1-* \
+ | alpha-* | alphaev5-* | alphaev56-* | we32k-* | cydra-* \
+ | ns16k-* | pn-* | np1-* | xps100-* | clipper-* | orion-* \
+ | sparclite-* | pdp11-* | sh-* | powerpc-* | powerpcle-* \
+ | sparc64-* | mips64-* | mipsel-* \
+ | mips64el-* | mips64orion-* | mips64orionel-* \
+ | mipstx39-* | mipstx39el-* \
+ | f301-*)
+ ;;
+ # Recognize the various machine names and aliases which stand
+ # for a CPU type and a company and sometimes even an OS.
+ 3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc)
+ basic_machine=m68000-att
+ ;;
+ 3b*)
+ basic_machine=we32k-att
+ ;;
+ alliant | fx80)
+ basic_machine=fx80-alliant
+ ;;
+ altos | altos3068)
+ basic_machine=m68k-altos
+ ;;
+ am29k)
+ basic_machine=a29k-none
+ os=-bsd
+ ;;
+ amdahl)
+ basic_machine=580-amdahl
+ os=-sysv
+ ;;
+ amiga | amiga-*)
+ basic_machine=m68k-cbm
+ ;;
+ amigaos | amigados)
+ basic_machine=m68k-cbm
+ os=-amigaos
+ ;;
+ amigaunix | amix)
+ basic_machine=m68k-cbm
+ os=-sysv4
+ ;;
+ apollo68)
+ basic_machine=m68k-apollo
+ os=-sysv
+ ;;
+ aux)
+ basic_machine=m68k-apple
+ os=-aux
+ ;;
+ balance)
+ basic_machine=ns32k-sequent
+ os=-dynix
+ ;;
+ convex-c1)
+ basic_machine=c1-convex
+ os=-bsd
+ ;;
+ convex-c2)
+ basic_machine=c2-convex
+ os=-bsd
+ ;;
+ convex-c32)
+ basic_machine=c32-convex
+ os=-bsd
+ ;;
+ convex-c34)
+ basic_machine=c34-convex
+ os=-bsd
+ ;;
+ convex-c38)
+ basic_machine=c38-convex
+ os=-bsd
+ ;;
+ cray | ymp)
+ basic_machine=ymp-cray
+ os=-unicos
+ ;;
+ cray2)
+ basic_machine=cray2-cray
+ os=-unicos
+ ;;
+ [ctj]90-cray)
+ basic_machine=c90-cray
+ os=-unicos
+ ;;
+ crds | unos)
+ basic_machine=m68k-crds
+ ;;
+ da30 | da30-*)
+ basic_machine=m68k-da30
+ ;;
+ decstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn)
+ basic_machine=mips-dec
+ ;;
+ delta | 3300 | motorola-3300 | motorola-delta \
+ | 3300-motorola | delta-motorola)
+ basic_machine=m68k-motorola
+ ;;
+ delta88)
+ basic_machine=m88k-motorola
+ os=-sysv3
+ ;;
+ dpx20 | dpx20-*)
+ basic_machine=rs6000-bull
+ os=-bosx
+ ;;
+ dpx2* | dpx2*-bull)
+ basic_machine=m68k-bull
+ os=-sysv3
+ ;;
+ ebmon29k)
+ basic_machine=a29k-amd
+ os=-ebmon
+ ;;
+ elxsi)
+ basic_machine=elxsi-elxsi
+ os=-bsd
+ ;;
+ encore | umax | mmax)
+ basic_machine=ns32k-encore
+ ;;
+ fx2800)
+ basic_machine=i860-alliant
+ ;;
+ genix)
+ basic_machine=ns32k-ns
+ ;;
+ gmicro)
+ basic_machine=tron-gmicro
+ os=-sysv
+ ;;
+ h3050r* | hiux*)
+ basic_machine=hppa1.1-hitachi
+ os=-hiuxwe2
+ ;;
+ h8300hms)
+ basic_machine=h8300-hitachi
+ os=-hms
+ ;;
+ harris)
+ basic_machine=m88k-harris
+ os=-sysv3
+ ;;
+ hp300-*)
+ basic_machine=m68k-hp
+ ;;
+ hp300bsd)
+ basic_machine=m68k-hp
+ os=-bsd
+ ;;
+ hp300hpux)
+ basic_machine=m68k-hp
+ os=-hpux
+ ;;
+ hp9k2[0-9][0-9] | hp9k31[0-9])
+ basic_machine=m68000-hp
+ ;;
+ hp9k3[2-9][0-9])
+ basic_machine=m68k-hp
+ ;;
+ hp9k7[0-9][0-9] | hp7[0-9][0-9] | hp9k8[0-9]7 | hp8[0-9]7)
+ basic_machine=hppa1.1-hp
+ ;;
+ hp9k8[0-9][0-9] | hp8[0-9][0-9])
+ basic_machine=hppa1.0-hp
+ ;;
+ hppa-next)
+ os=-nextstep3
+ ;;
+ i370-ibm* | ibm*)
+ basic_machine=i370-ibm
+ os=-mvs
+ ;;
+# I'm not sure what "Sysv32" means. Should this be sysv3.2?
+ i[3456]86v32)
+ basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+ os=-sysv32
+ ;;
+ i[3456]86v4*)
+ basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+ os=-sysv4
+ ;;
+ i[3456]86v)
+ basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+ os=-sysv
+ ;;
+ i[3456]86sol2)
+ basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+ os=-solaris2
+ ;;
+ iris | iris4d)
+ basic_machine=mips-sgi
+ case $os in
+ -irix*)
+ ;;
+ *)
+ os=-irix4
+ ;;
+ esac
+ ;;
+ isi68 | isi)
+ basic_machine=m68k-isi
+ os=-sysv
+ ;;
+ m88k-omron*)
+ basic_machine=m88k-omron
+ ;;
+ magnum | m3230)
+ basic_machine=mips-mips
+ os=-sysv
+ ;;
+ merlin)
+ basic_machine=ns32k-utek
+ os=-sysv
+ ;;
+ miniframe)
+ basic_machine=m68000-convergent
+ ;;
+ mipsel*-linux*)
+ basic_machine=mipsel-unknown
+ os=-linux-gnu
+ ;;
+ mips*-linux*)
+ basic_machine=mips-unknown
+ os=-linux-gnu
+ ;;
+ mips3*-*)
+ basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`
+ ;;
+ mips3*)
+ basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown
+ ;;
+ ncr3000)
+ basic_machine=i486-ncr
+ os=-sysv4
+ ;;
+ news | news700 | news800 | news900)
+ basic_machine=m68k-sony
+ os=-newsos
+ ;;
+ news1000)
+ basic_machine=m68030-sony
+ os=-newsos
+ ;;
+ news-3600 | risc-news)
+ basic_machine=mips-sony
+ os=-newsos
+ ;;
+ next | m*-next )
+ basic_machine=m68k-next
+ case $os in
+ -nextstep* )
+ ;;
+ -ns2*)
+ os=-nextstep2
+ ;;
+ *)
+ os=-nextstep3
+ ;;
+ esac
+ ;;
+ nh3000)
+ basic_machine=m68k-harris
+ os=-cxux
+ ;;
+ nh[45]000)
+ basic_machine=m88k-harris
+ os=-cxux
+ ;;
+ nindy960)
+ basic_machine=i960-intel
+ os=-nindy
+ ;;
+ np1)
+ basic_machine=np1-gould
+ ;;
+ pa-hitachi)
+ basic_machine=hppa1.1-hitachi
+ os=-hiuxwe2
+ ;;
+ paragon)
+ basic_machine=i860-intel
+ os=-osf
+ ;;
+ pbd)
+ basic_machine=sparc-tti
+ ;;
+ pbb)
+ basic_machine=m68k-tti
+ ;;
+ pc532 | pc532-*)
+ basic_machine=ns32k-pc532
+ ;;
+ pentium | p5)
+ basic_machine=i586-intel
+ ;;
+ pentiumpro | p6)
+ basic_machine=i686-intel
+ ;;
+ pentium-* | p5-*)
+ basic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ pentiumpro-* | p6-*)
+ basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ k5)
+ # We don't have specific support for AMD's K5 yet, so just call it a Pentium
+ basic_machine=i586-amd
+ ;;
+ nexen)
+ # We don't have specific support for Nexgen yet, so just call it a Pentium
+ basic_machine=i586-nexgen
+ ;;
+ pn)
+ basic_machine=pn-gould
+ ;;
+ power) basic_machine=rs6000-ibm
+ ;;
+ ppc) basic_machine=powerpc-unknown
+ ;;
+ ppc-*) basic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ ppcle | powerpclittle | ppc-le | powerpc-little)
+ basic_machine=powerpcle-unknown
+ ;;
+ ppcle-* | powerpclittle-*)
+ basic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ ps2)
+ basic_machine=i386-ibm
+ ;;
+ rm[46]00)
+ basic_machine=mips-siemens
+ ;;
+ rtpc | rtpc-*)
+ basic_machine=romp-ibm
+ ;;
+ sequent)
+ basic_machine=i386-sequent
+ ;;
+ sh)
+ basic_machine=sh-hitachi
+ os=-hms
+ ;;
+ sps7)
+ basic_machine=m68k-bull
+ os=-sysv2
+ ;;
+ spur)
+ basic_machine=spur-unknown
+ ;;
+ sun2)
+ basic_machine=m68000-sun
+ ;;
+ sun2os3)
+ basic_machine=m68000-sun
+ os=-sunos3
+ ;;
+ sun2os4)
+ basic_machine=m68000-sun
+ os=-sunos4
+ ;;
+ sun3os3)
+ basic_machine=m68k-sun
+ os=-sunos3
+ ;;
+ sun3os4)
+ basic_machine=m68k-sun
+ os=-sunos4
+ ;;
+ sun4os3)
+ basic_machine=sparc-sun
+ os=-sunos3
+ ;;
+ sun4os4)
+ basic_machine=sparc-sun
+ os=-sunos4
+ ;;
+ sun4sol2)
+ basic_machine=sparc-sun
+ os=-solaris2
+ ;;
+ sun3 | sun3-*)
+ basic_machine=m68k-sun
+ ;;
+ sun4)
+ basic_machine=sparc-sun
+ ;;
+ sun386 | sun386i | roadrunner)
+ basic_machine=i386-sun
+ ;;
+ symmetry)
+ basic_machine=i386-sequent
+ os=-dynix
+ ;;
+ tx39)
+ basic_machine=mipstx39-unknown
+ ;;
+ tx39el)
+ basic_machine=mipstx39el-unknown
+ ;;
+ tower | tower-32)
+ basic_machine=m68k-ncr
+ ;;
+ udi29k)
+ basic_machine=a29k-amd
+ os=-udi
+ ;;
+ ultra3)
+ basic_machine=a29k-nyu
+ os=-sym1
+ ;;
+ vaxv)
+ basic_machine=vax-dec
+ os=-sysv
+ ;;
+ vms)
+ basic_machine=vax-dec
+ os=-vms
+ ;;
+ vpp*|vx|vx-*)
+ basic_machine=f301-fujitsu
+ ;;
+ vxworks960)
+ basic_machine=i960-wrs
+ os=-vxworks
+ ;;
+ vxworks68)
+ basic_machine=m68k-wrs
+ os=-vxworks
+ ;;
+ vxworks29k)
+ basic_machine=a29k-wrs
+ os=-vxworks
+ ;;
+ xmp)
+ basic_machine=xmp-cray
+ os=-unicos
+ ;;
+ xps | xps100)
+ basic_machine=xps100-honeywell
+ ;;
+ none)
+ basic_machine=none-none
+ os=-none
+ ;;
+
+# Here we handle the default manufacturer of certain CPU types. It is in
+# some cases the only manufacturer, in others, it is the most popular.
+ mips)
+ if [ x$os = x-linux-gnu ]; then
+ basic_machine=mips-unknown
+ else
+ basic_machine=mips-mips
+ fi
+ ;;
+ romp)
+ basic_machine=romp-ibm
+ ;;
+ rs6000)
+ basic_machine=rs6000-ibm
+ ;;
+ vax)
+ basic_machine=vax-dec
+ ;;
+ pdp11)
+ basic_machine=pdp11-dec
+ ;;
+ we32k)
+ basic_machine=we32k-att
+ ;;
+ sparc)
+ basic_machine=sparc-sun
+ ;;
+ cydra)
+ basic_machine=cydra-cydrome
+ ;;
+ orion)
+ basic_machine=orion-highlevel
+ ;;
+ orion105)
+ basic_machine=clipper-highlevel
+ ;;
+ *)
+ echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
+ exit 1
+ ;;
+esac
+
+# Here we canonicalize certain aliases for manufacturers.
+case $basic_machine in
+ *-digital*)
+ basic_machine=`echo $basic_machine | sed 's/digital.*/dec/'`
+ ;;
+ *-commodore*)
+ basic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'`
+ ;;
+ *)
+ ;;
+esac
+
+# Decode manufacturer-specific aliases for certain operating systems.
+
+if [ x"$os" != x"" ]
+then
+case $os in
+ # First match some system type aliases
+ # that might get confused with valid system types.
+ # -solaris* is a basic system type, with this one exception.
+ -solaris1 | -solaris1.*)
+ os=`echo $os | sed -e 's|solaris1|sunos4|'`
+ ;;
+ -solaris)
+ os=-solaris2
+ ;;
+ -svr4*)
+ os=-sysv4
+ ;;
+ -unixware*)
+ os=-sysv4.2uw
+ ;;
+ -gnu/linux*)
+ os=`echo $os | sed -e 's|gnu/linux|linux-gnu|'`
+ ;;
+ # First accept the basic system types.
+ # The portable systems comes first.
+ # Each alternative MUST END IN A *, to match a version number.
+ # -sysv* is not here because it comes later, after sysvr4.
+ -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \
+ | -*vms* | -sco* | -esix* | -isc* | -aix* | -sunos | -sunos[34]*\
+ | -hpux* | -unos* | -osf* | -luna* | -dgux* | -solaris* | -sym* \
+ | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \
+ | -aos* \
+ | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \
+ | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \
+ | -hiux* | -386bsd* | -netbsd* | -openbsd* | -freebsd* | -riscix* \
+ | -lynxos* | -bosx* | -nextstep* | -cxux* | -aout* | -elf* \
+ | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \
+ | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \
+ | -cygwin32* | -pe* | -psos* | -moss* | -proelf* | -rtems* \
+ | -mingw32* | -linux-gnu* | -uxpv*)
+ # Remember, each alternative MUST END IN *, to match a version number.
+ ;;
+ -linux*)
+ os=`echo $os | sed -e 's|linux|linux-gnu|'`
+ ;;
+ -sunos5*)
+ os=`echo $os | sed -e 's|sunos5|solaris2|'`
+ ;;
+ -sunos6*)
+ os=`echo $os | sed -e 's|sunos6|solaris3|'`
+ ;;
+ -osfrose*)
+ os=-osfrose
+ ;;
+ -osf*)
+ os=-osf
+ ;;
+ -utek*)
+ os=-bsd
+ ;;
+ -dynix*)
+ os=-bsd
+ ;;
+ -acis*)
+ os=-aos
+ ;;
+ -ctix* | -uts*)
+ os=-sysv
+ ;;
+ -ns2 )
+ os=-nextstep2
+ ;;
+ # Preserve the version number of sinix5.
+ -sinix5.*)
+ os=`echo $os | sed -e 's|sinix|sysv|'`
+ ;;
+ -sinix*)
+ os=-sysv4
+ ;;
+ -triton*)
+ os=-sysv3
+ ;;
+ -oss*)
+ os=-sysv3
+ ;;
+ -svr4)
+ os=-sysv4
+ ;;
+ -svr3)
+ os=-sysv3
+ ;;
+ -sysvr4)
+ os=-sysv4
+ ;;
+ # This must come after -sysvr4.
+ -sysv*)
+ ;;
+ -xenix)
+ os=-xenix
+ ;;
+ -none)
+ ;;
+ *)
+ # Get rid of the `-' at the beginning of $os.
+ os=`echo $os | sed 's/[^-]*-//'`
+ echo Invalid configuration \`$1\': system \`$os\' not recognized 1>&2
+ exit 1
+ ;;
+esac
+else
+
+# Here we handle the default operating systems that come with various machines.
+# The value should be what the vendor currently ships out the door with their
+# machine or put another way, the most popular os provided with the machine.
+
+# Note that if you're going to try to match "-MANUFACTURER" here (say,
+# "-sun"), then you have to tell the case statement up towards the top
+# that MANUFACTURER isn't an operating system. Otherwise, code above
+# will signal an error saying that MANUFACTURER isn't an operating
+# system, and we'll never get to this point.
+
+case $basic_machine in
+ *-acorn)
+ os=-riscix1.2
+ ;;
+ arm*-semi)
+ os=-aout
+ ;;
+ pdp11-*)
+ os=-none
+ ;;
+ *-dec | vax-*)
+ os=-ultrix4.2
+ ;;
+ m68*-apollo)
+ os=-domain
+ ;;
+ i386-sun)
+ os=-sunos4.0.2
+ ;;
+ m68000-sun)
+ os=-sunos3
+ # This also exists in the configure program, but was not the
+ # default.
+ # os=-sunos4
+ ;;
+ *-tti) # must be before sparc entry or we get the wrong os.
+ os=-sysv3
+ ;;
+ sparc-* | *-sun)
+ os=-sunos4.1.1
+ ;;
+ *-ibm)
+ os=-aix
+ ;;
+ *-hp)
+ os=-hpux
+ ;;
+ *-hitachi)
+ os=-hiux
+ ;;
+ i860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent)
+ os=-sysv
+ ;;
+ *-cbm)
+ os=-amigaos
+ ;;
+ *-dg)
+ os=-dgux
+ ;;
+ *-dolphin)
+ os=-sysv3
+ ;;
+ m68k-ccur)
+ os=-rtu
+ ;;
+ m88k-omron*)
+ os=-luna
+ ;;
+ *-next )
+ os=-nextstep
+ ;;
+ *-sequent)
+ os=-ptx
+ ;;
+ *-crds)
+ os=-unos
+ ;;
+ *-ns)
+ os=-genix
+ ;;
+ i370-*)
+ os=-mvs
+ ;;
+ *-next)
+ os=-nextstep3
+ ;;
+ *-gould)
+ os=-sysv
+ ;;
+ *-highlevel)
+ os=-bsd
+ ;;
+ *-encore)
+ os=-bsd
+ ;;
+ *-sgi)
+ os=-irix
+ ;;
+ *-siemens)
+ os=-sysv4
+ ;;
+ *-masscomp)
+ os=-rtu
+ ;;
+ f301-fujitsu)
+ os=-uxpv
+ ;;
+ *)
+ os=-none
+ ;;
+esac
+fi
+
+# Here we handle the case where we know the os, and the CPU type, but not the
+# manufacturer. We pick the logical manufacturer.
+vendor=unknown
+case $basic_machine in
+ *-unknown)
+ case $os in
+ -riscix*)
+ vendor=acorn
+ ;;
+ -sunos*)
+ vendor=sun
+ ;;
+ -aix*)
+ vendor=ibm
+ ;;
+ -hpux*)
+ vendor=hp
+ ;;
+ -hiux*)
+ vendor=hitachi
+ ;;
+ -unos*)
+ vendor=crds
+ ;;
+ -dgux*)
+ vendor=dg
+ ;;
+ -luna*)
+ vendor=omron
+ ;;
+ -genix*)
+ vendor=ns
+ ;;
+ -mvs*)
+ vendor=ibm
+ ;;
+ -ptx*)
+ vendor=sequent
+ ;;
+ -vxsim* | -vxworks*)
+ vendor=wrs
+ ;;
+ -aux*)
+ vendor=apple
+ ;;
+ esac
+ basic_machine=`echo $basic_machine | sed "s/unknown/$vendor/"`
+ ;;
+esac
+
+echo $basic_machine$os
diff --git a/test/monniaux/jpeg-6b/configure b/test/monniaux/jpeg-6b/configure
new file mode 100755
index 00000000..35c9db5c
--- /dev/null
+++ b/test/monniaux/jpeg-6b/configure
@@ -0,0 +1,2011 @@
+#! /bin/sh
+
+# Guess values for system-dependent variables and create Makefiles.
+# Generated automatically using autoconf version 2.12
+# Copyright (C) 1992, 93, 94, 95, 96 Free Software Foundation, Inc.
+#
+# This configure script is free software; the Free Software Foundation
+# gives unlimited permission to copy, distribute and modify it.
+
+# Defaults:
+ac_help=
+ac_default_prefix=/usr/local
+# Any additions from configure.in:
+ac_help="$ac_help
+ --enable-shared build shared library using GNU libtool"
+ac_help="$ac_help
+ --enable-static build static library using GNU libtool"
+ac_help="$ac_help
+ --enable-maxmem[=N] enable use of temp files, set max mem usage to N MB"
+ac_help="$ac_help
+"
+
+# Initialize some variables set by options.
+# The variables have the same names as the options, with
+# dashes changed to underlines.
+build=NONE
+cache_file=./config.cache
+exec_prefix=NONE
+host=NONE
+no_create=
+nonopt=NONE
+no_recursion=
+prefix=NONE
+program_prefix=NONE
+program_suffix=NONE
+program_transform_name=s,x,x,
+silent=
+site=
+srcdir=
+target=NONE
+verbose=
+x_includes=NONE
+x_libraries=NONE
+bindir='${exec_prefix}/bin'
+sbindir='${exec_prefix}/sbin'
+libexecdir='${exec_prefix}/libexec'
+datadir='${prefix}/share'
+sysconfdir='${prefix}/etc'
+sharedstatedir='${prefix}/com'
+localstatedir='${prefix}/var'
+libdir='${exec_prefix}/lib'
+includedir='${prefix}/include'
+oldincludedir='/usr/include'
+infodir='${prefix}/info'
+mandir='${prefix}/man'
+
+# Initialize some other variables.
+subdirs=
+MFLAGS= MAKEFLAGS=
+# Maximum number of lines to put in a shell here document.
+ac_max_here_lines=12
+
+ac_prev=
+for ac_option
+do
+
+ # If the previous option needs an argument, assign it.
+ if test -n "$ac_prev"; then
+ eval "$ac_prev=\$ac_option"
+ ac_prev=
+ continue
+ fi
+
+ case "$ac_option" in
+ -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
+ *) ac_optarg= ;;
+ esac
+
+ # Accept the important Cygnus configure options, so we can diagnose typos.
+
+ case "$ac_option" in
+
+ -bindir | --bindir | --bindi | --bind | --bin | --bi)
+ ac_prev=bindir ;;
+ -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)
+ bindir="$ac_optarg" ;;
+
+ -build | --build | --buil | --bui | --bu)
+ ac_prev=build ;;
+ -build=* | --build=* | --buil=* | --bui=* | --bu=*)
+ build="$ac_optarg" ;;
+
+ -cache-file | --cache-file | --cache-fil | --cache-fi \
+ | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c)
+ ac_prev=cache_file ;;
+ -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \
+ | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*)
+ cache_file="$ac_optarg" ;;
+
+ -datadir | --datadir | --datadi | --datad | --data | --dat | --da)
+ ac_prev=datadir ;;
+ -datadir=* | --datadir=* | --datadi=* | --datad=* | --data=* | --dat=* \
+ | --da=*)
+ datadir="$ac_optarg" ;;
+
+ -disable-* | --disable-*)
+ ac_feature=`echo $ac_option|sed -e 's/-*disable-//'`
+ # Reject names that are not valid shell variable names.
+ if test -n "`echo $ac_feature| sed 's/[-a-zA-Z0-9_]//g'`"; then
+ { echo "configure: error: $ac_feature: invalid feature name" 1>&2; exit 1; }
+ fi
+ ac_feature=`echo $ac_feature| sed 's/-/_/g'`
+ eval "enable_${ac_feature}=no" ;;
+
+ -enable-* | --enable-*)
+ ac_feature=`echo $ac_option|sed -e 's/-*enable-//' -e 's/=.*//'`
+ # Reject names that are not valid shell variable names.
+ if test -n "`echo $ac_feature| sed 's/[-_a-zA-Z0-9]//g'`"; then
+ { echo "configure: error: $ac_feature: invalid feature name" 1>&2; exit 1; }
+ fi
+ ac_feature=`echo $ac_feature| sed 's/-/_/g'`
+ case "$ac_option" in
+ *=*) ;;
+ *) ac_optarg=yes ;;
+ esac
+ eval "enable_${ac_feature}='$ac_optarg'" ;;
+
+ -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \
+ | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \
+ | --exec | --exe | --ex)
+ ac_prev=exec_prefix ;;
+ -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \
+ | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \
+ | --exec=* | --exe=* | --ex=*)
+ exec_prefix="$ac_optarg" ;;
+
+ -gas | --gas | --ga | --g)
+ # Obsolete; use --with-gas.
+ with_gas=yes ;;
+
+ -help | --help | --hel | --he)
+ # Omit some internal or obsolete options to make the list less imposing.
+ # This message is too long to be a string in the A/UX 3.1 sh.
+ cat << EOF
+Usage: configure [options] [host]
+Options: [defaults in brackets after descriptions]
+Configuration:
+ --cache-file=FILE cache test results in FILE
+ --help print this message
+ --no-create do not create output files
+ --quiet, --silent do not print \`checking...' messages
+ --version print the version of autoconf that created configure
+Directory and file names:
+ --prefix=PREFIX install architecture-independent files in PREFIX
+ [$ac_default_prefix]
+ --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
+ [same as prefix]
+ --bindir=DIR user executables in DIR [EPREFIX/bin]
+ --sbindir=DIR system admin executables in DIR [EPREFIX/sbin]
+ --libexecdir=DIR program executables in DIR [EPREFIX/libexec]
+ --datadir=DIR read-only architecture-independent data in DIR
+ [PREFIX/share]
+ --sysconfdir=DIR read-only single-machine data in DIR [PREFIX/etc]
+ --sharedstatedir=DIR modifiable architecture-independent data in DIR
+ [PREFIX/com]
+ --localstatedir=DIR modifiable single-machine data in DIR [PREFIX/var]
+ --libdir=DIR object code libraries in DIR [EPREFIX/lib]
+ --includedir=DIR C header files in DIR [PREFIX/include]
+ --oldincludedir=DIR C header files for non-gcc in DIR [/usr/include]
+ --infodir=DIR info documentation in DIR [PREFIX/info]
+ --mandir=DIR man documentation in DIR [PREFIX/man]
+ --srcdir=DIR find the sources in DIR [configure dir or ..]
+ --program-prefix=PREFIX prepend PREFIX to installed program names
+ --program-suffix=SUFFIX append SUFFIX to installed program names
+ --program-transform-name=PROGRAM
+ run sed PROGRAM on installed program names
+EOF
+ cat << EOF
+Host type:
+ --build=BUILD configure for building on BUILD [BUILD=HOST]
+ --host=HOST configure for HOST [guessed]
+ --target=TARGET configure for TARGET [TARGET=HOST]
+Features and packages:
+ --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
+ --enable-FEATURE[=ARG] include FEATURE [ARG=yes]
+ --with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
+ --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
+ --x-includes=DIR X include files are in DIR
+ --x-libraries=DIR X library files are in DIR
+EOF
+ if test -n "$ac_help"; then
+ echo "--enable and --with options recognized:$ac_help"
+ fi
+ exit 0 ;;
+
+ -host | --host | --hos | --ho)
+ ac_prev=host ;;
+ -host=* | --host=* | --hos=* | --ho=*)
+ host="$ac_optarg" ;;
+
+ -includedir | --includedir | --includedi | --included | --include \
+ | --includ | --inclu | --incl | --inc)
+ ac_prev=includedir ;;
+ -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \
+ | --includ=* | --inclu=* | --incl=* | --inc=*)
+ includedir="$ac_optarg" ;;
+
+ -infodir | --infodir | --infodi | --infod | --info | --inf)
+ ac_prev=infodir ;;
+ -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*)
+ infodir="$ac_optarg" ;;
+
+ -libdir | --libdir | --libdi | --libd)
+ ac_prev=libdir ;;
+ -libdir=* | --libdir=* | --libdi=* | --libd=*)
+ libdir="$ac_optarg" ;;
+
+ -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \
+ | --libexe | --libex | --libe)
+ ac_prev=libexecdir ;;
+ -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \
+ | --libexe=* | --libex=* | --libe=*)
+ libexecdir="$ac_optarg" ;;
+
+ -localstatedir | --localstatedir | --localstatedi | --localstated \
+ | --localstate | --localstat | --localsta | --localst \
+ | --locals | --local | --loca | --loc | --lo)
+ ac_prev=localstatedir ;;
+ -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \
+ | --localstate=* | --localstat=* | --localsta=* | --localst=* \
+ | --locals=* | --local=* | --loca=* | --loc=* | --lo=*)
+ localstatedir="$ac_optarg" ;;
+
+ -mandir | --mandir | --mandi | --mand | --man | --ma | --m)
+ ac_prev=mandir ;;
+ -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*)
+ mandir="$ac_optarg" ;;
+
+ -nfp | --nfp | --nf)
+ # Obsolete; use --without-fp.
+ with_fp=no ;;
+
+ -no-create | --no-create | --no-creat | --no-crea | --no-cre \
+ | --no-cr | --no-c)
+ no_create=yes ;;
+
+ -no-recursion | --no-recursion | --no-recursio | --no-recursi \
+ | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r)
+ no_recursion=yes ;;
+
+ -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \
+ | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \
+ | --oldin | --oldi | --old | --ol | --o)
+ ac_prev=oldincludedir ;;
+ -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \
+ | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \
+ | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*)
+ oldincludedir="$ac_optarg" ;;
+
+ -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
+ ac_prev=prefix ;;
+ -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
+ prefix="$ac_optarg" ;;
+
+ -program-prefix | --program-prefix | --program-prefi | --program-pref \
+ | --program-pre | --program-pr | --program-p)
+ ac_prev=program_prefix ;;
+ -program-prefix=* | --program-prefix=* | --program-prefi=* \
+ | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*)
+ program_prefix="$ac_optarg" ;;
+
+ -program-suffix | --program-suffix | --program-suffi | --program-suff \
+ | --program-suf | --program-su | --program-s)
+ ac_prev=program_suffix ;;
+ -program-suffix=* | --program-suffix=* | --program-suffi=* \
+ | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*)
+ program_suffix="$ac_optarg" ;;
+
+ -program-transform-name | --program-transform-name \
+ | --program-transform-nam | --program-transform-na \
+ | --program-transform-n | --program-transform- \
+ | --program-transform | --program-transfor \
+ | --program-transfo | --program-transf \
+ | --program-trans | --program-tran \
+ | --progr-tra | --program-tr | --program-t)
+ ac_prev=program_transform_name ;;
+ -program-transform-name=* | --program-transform-name=* \
+ | --program-transform-nam=* | --program-transform-na=* \
+ | --program-transform-n=* | --program-transform-=* \
+ | --program-transform=* | --program-transfor=* \
+ | --program-transfo=* | --program-transf=* \
+ | --program-trans=* | --program-tran=* \
+ | --progr-tra=* | --program-tr=* | --program-t=*)
+ program_transform_name="$ac_optarg" ;;
+
+ -q | -quiet | --quiet | --quie | --qui | --qu | --q \
+ | -silent | --silent | --silen | --sile | --sil)
+ silent=yes ;;
+
+ -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
+ ac_prev=sbindir ;;
+ -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
+ | --sbi=* | --sb=*)
+ sbindir="$ac_optarg" ;;
+
+ -sharedstatedir | --sharedstatedir | --sharedstatedi \
+ | --sharedstated | --sharedstate | --sharedstat | --sharedsta \
+ | --sharedst | --shareds | --shared | --share | --shar \
+ | --sha | --sh)
+ ac_prev=sharedstatedir ;;
+ -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \
+ | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \
+ | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \
+ | --sha=* | --sh=*)
+ sharedstatedir="$ac_optarg" ;;
+
+ -site | --site | --sit)
+ ac_prev=site ;;
+ -site=* | --site=* | --sit=*)
+ site="$ac_optarg" ;;
+
+ -srcdir | --srcdir | --srcdi | --srcd | --src | --sr)
+ ac_prev=srcdir ;;
+ -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*)
+ srcdir="$ac_optarg" ;;
+
+ -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \
+ | --syscon | --sysco | --sysc | --sys | --sy)
+ ac_prev=sysconfdir ;;
+ -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \
+ | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*)
+ sysconfdir="$ac_optarg" ;;
+
+ -target | --target | --targe | --targ | --tar | --ta | --t)
+ ac_prev=target ;;
+ -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
+ target="$ac_optarg" ;;
+
+ -v | -verbose | --verbose | --verbos | --verbo | --verb)
+ verbose=yes ;;
+
+ -version | --version | --versio | --versi | --vers)
+ echo "configure generated by autoconf version 2.12"
+ exit 0 ;;
+
+ -with-* | --with-*)
+ ac_package=`echo $ac_option|sed -e 's/-*with-//' -e 's/=.*//'`
+ # Reject names that are not valid shell variable names.
+ if test -n "`echo $ac_package| sed 's/[-_a-zA-Z0-9]//g'`"; then
+ { echo "configure: error: $ac_package: invalid package name" 1>&2; exit 1; }
+ fi
+ ac_package=`echo $ac_package| sed 's/-/_/g'`
+ case "$ac_option" in
+ *=*) ;;
+ *) ac_optarg=yes ;;
+ esac
+ eval "with_${ac_package}='$ac_optarg'" ;;
+
+ -without-* | --without-*)
+ ac_package=`echo $ac_option|sed -e 's/-*without-//'`
+ # Reject names that are not valid shell variable names.
+ if test -n "`echo $ac_package| sed 's/[-a-zA-Z0-9_]//g'`"; then
+ { echo "configure: error: $ac_package: invalid package name" 1>&2; exit 1; }
+ fi
+ ac_package=`echo $ac_package| sed 's/-/_/g'`
+ eval "with_${ac_package}=no" ;;
+
+ --x)
+ # Obsolete; use --with-x.
+ with_x=yes ;;
+
+ -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \
+ | --x-incl | --x-inc | --x-in | --x-i)
+ ac_prev=x_includes ;;
+ -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \
+ | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*)
+ x_includes="$ac_optarg" ;;
+
+ -x-libraries | --x-libraries | --x-librarie | --x-librari \
+ | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l)
+ ac_prev=x_libraries ;;
+ -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \
+ | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*)
+ x_libraries="$ac_optarg" ;;
+
+ -*) { echo "configure: error: $ac_option: invalid option; use --help to show usage" 1>&2; exit 1; }
+ ;;
+
+ *=*)
+ varname=`echo "$ac_option"|sed -e 's/=.*//'`
+ # Reject names that aren't valid shell variable names.
+ if test -n "`echo $varname| sed 's/[a-zA-Z0-9_]//g'`"; then
+ { echo "configure: error: $varname: invalid shell variable name" 1>&2; exit 1; }
+ fi
+ val="`echo "$ac_option"|sed 's/[^=]*=//'`"
+ test -n "$verbose" && echo " setting shell variable $varname to $val"
+ eval "$varname='$val'"
+ eval "export $varname" ;;
+
+ *)
+ if test -n "`echo $ac_option| sed 's/[-a-z0-9.]//g'`"; then
+ echo "configure: warning: $ac_option: invalid host type" 1>&2
+ fi
+ if test "x$nonopt" != xNONE; then
+ { echo "configure: error: can only configure for one host and one target at a time" 1>&2; exit 1; }
+ fi
+ nonopt="$ac_option"
+ ;;
+
+ esac
+done
+
+if test -n "$ac_prev"; then
+ { echo "configure: error: missing argument to --`echo $ac_prev | sed 's/_/-/g'`" 1>&2; exit 1; }
+fi
+
+trap 'rm -fr conftest* confdefs* core core.* *.core $ac_clean_files; exit 1' 1 2 15
+
+# File descriptor usage:
+# 0 standard input
+# 1 file creation
+# 2 errors and warnings
+# 3 some systems may open it to /dev/tty
+# 4 used on the Kubota Titan
+# 6 checking for... messages and results
+# 5 compiler messages saved in config.log
+if test "$silent" = yes; then
+ exec 6>/dev/null
+else
+ exec 6>&1
+fi
+exec 5>./config.log
+
+echo "\
+This file contains any messages produced by compilers while
+running configure, to aid debugging if configure makes a mistake.
+" 1>&5
+
+# Strip out --no-create and --no-recursion so they do not pile up.
+# Also quote any args containing shell metacharacters.
+ac_configure_args=
+for ac_arg
+do
+ case "$ac_arg" in
+ -no-create | --no-create | --no-creat | --no-crea | --no-cre \
+ | --no-cr | --no-c) ;;
+ -no-recursion | --no-recursion | --no-recursio | --no-recursi \
+ | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r) ;;
+ *" "*|*" "*|*[\[\]\~\#\$\^\&\*\(\)\{\}\\\|\;\<\>\?]*)
+ ac_configure_args="$ac_configure_args '$ac_arg'" ;;
+ *) ac_configure_args="$ac_configure_args $ac_arg" ;;
+ esac
+done
+
+# NLS nuisances.
+# Only set these to C if already set. These must not be set unconditionally
+# because not all systems understand e.g. LANG=C (notably SCO).
+# Fixing LC_MESSAGES prevents Solaris sh from translating var values in `set'!
+# Non-C LC_CTYPE values break the ctype check.
+if test "${LANG+set}" = set; then LANG=C; export LANG; fi
+if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
+if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
+if test "${LC_CTYPE+set}" = set; then LC_CTYPE=C; export LC_CTYPE; fi
+
+# confdefs.h avoids OS command line length limits that DEFS can exceed.
+rm -rf conftest* confdefs.h
+# AIX cpp loses on an empty file, so make sure it contains at least a newline.
+echo > confdefs.h
+
+# A filename unique to this package, relative to the directory that
+# configure is in, which we can look for to find out if srcdir is correct.
+ac_unique_file=jcmaster.c
+
+# Find the source files, if location was not specified.
+if test -z "$srcdir"; then
+ ac_srcdir_defaulted=yes
+ # Try the directory containing this script, then its parent.
+ ac_prog=$0
+ ac_confdir=`echo $ac_prog|sed 's%/[^/][^/]*$%%'`
+ test "x$ac_confdir" = "x$ac_prog" && ac_confdir=.
+ srcdir=$ac_confdir
+ if test ! -r $srcdir/$ac_unique_file; then
+ srcdir=..
+ fi
+else
+ ac_srcdir_defaulted=no
+fi
+if test ! -r $srcdir/$ac_unique_file; then
+ if test "$ac_srcdir_defaulted" = yes; then
+ { echo "configure: error: can not find sources in $ac_confdir or .." 1>&2; exit 1; }
+ else
+ { echo "configure: error: can not find sources in $srcdir" 1>&2; exit 1; }
+ fi
+fi
+srcdir=`echo "${srcdir}" | sed 's%\([^/]\)/*$%\1%'`
+
+# Prefer explicitly selected file to automatically selected ones.
+if test -z "$CONFIG_SITE"; then
+ if test "x$prefix" != xNONE; then
+ CONFIG_SITE="$prefix/share/config.site $prefix/etc/config.site"
+ else
+ CONFIG_SITE="$ac_default_prefix/share/config.site $ac_default_prefix/etc/config.site"
+ fi
+fi
+for ac_site_file in $CONFIG_SITE; do
+ if test -r "$ac_site_file"; then
+ echo "loading site script $ac_site_file"
+ . "$ac_site_file"
+ fi
+done
+
+
+ac_ext=c
+# CFLAGS is not in ac_cpp because -g, -O, etc. are not valid cpp options.
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='${CC-cc} -c $CFLAGS $CPPFLAGS conftest.$ac_ext 1>&5'
+ac_link='${CC-cc} -o conftest $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS 1>&5'
+cross_compiling=$ac_cv_prog_cc_cross
+
+if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
+ # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
+ if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
+ ac_n= ac_c='
+' ac_t=' '
+ else
+ ac_n=-n ac_c= ac_t=
+ fi
+else
+ ac_n= ac_c='\c' ac_t=
+fi
+
+
+
+# Extract the first word of "gcc", so it can be a program name with args.
+set dummy gcc; ac_word=$2
+echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
+echo "configure:538: checking for $ac_word" >&5
+if eval "test \"`echo '$''{'ac_cv_prog_CC'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ if test -n "$CC"; then
+ ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+ IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
+ for ac_dir in $PATH; do
+ test -z "$ac_dir" && ac_dir=.
+ if test -f $ac_dir/$ac_word; then
+ ac_cv_prog_CC="gcc"
+ break
+ fi
+ done
+ IFS="$ac_save_ifs"
+fi
+fi
+CC="$ac_cv_prog_CC"
+if test -n "$CC"; then
+ echo "$ac_t""$CC" 1>&6
+else
+ echo "$ac_t""no" 1>&6
+fi
+
+if test -z "$CC"; then
+ # Extract the first word of "cc", so it can be a program name with args.
+set dummy cc; ac_word=$2
+echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
+echo "configure:567: checking for $ac_word" >&5
+if eval "test \"`echo '$''{'ac_cv_prog_CC'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ if test -n "$CC"; then
+ ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+ IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
+ ac_prog_rejected=no
+ for ac_dir in $PATH; do
+ test -z "$ac_dir" && ac_dir=.
+ if test -f $ac_dir/$ac_word; then
+ if test "$ac_dir/$ac_word" = "/usr/ucb/cc"; then
+ ac_prog_rejected=yes
+ continue
+ fi
+ ac_cv_prog_CC="cc"
+ break
+ fi
+ done
+ IFS="$ac_save_ifs"
+if test $ac_prog_rejected = yes; then
+ # We found a bogon in the path, so make sure we never use it.
+ set dummy $ac_cv_prog_CC
+ shift
+ if test $# -gt 0; then
+ # We chose a different compiler from the bogus one.
+ # However, it has the same basename, so the bogon will be chosen
+ # first if we set CC to just the basename; use the full file name.
+ shift
+ set dummy "$ac_dir/$ac_word" "$@"
+ shift
+ ac_cv_prog_CC="$@"
+ fi
+fi
+fi
+fi
+CC="$ac_cv_prog_CC"
+if test -n "$CC"; then
+ echo "$ac_t""$CC" 1>&6
+else
+ echo "$ac_t""no" 1>&6
+fi
+
+ test -z "$CC" && { echo "configure: error: no acceptable cc found in \$PATH" 1>&2; exit 1; }
+fi
+
+echo $ac_n "checking whether the C compiler ($CC $CFLAGS $LDFLAGS) works""... $ac_c" 1>&6
+echo "configure:615: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) works" >&5
+
+ac_ext=c
+# CFLAGS is not in ac_cpp because -g, -O, etc. are not valid cpp options.
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='${CC-cc} -c $CFLAGS $CPPFLAGS conftest.$ac_ext 1>&5'
+ac_link='${CC-cc} -o conftest $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS 1>&5'
+cross_compiling=$ac_cv_prog_cc_cross
+
+cat > conftest.$ac_ext <<EOF
+#line 625 "configure"
+#include "confdefs.h"
+main(){return(0);}
+EOF
+if { (eval echo configure:629: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ ac_cv_prog_cc_works=yes
+ # If we can't run a trivial program, we are probably using a cross compiler.
+ if (./conftest; exit) 2>/dev/null; then
+ ac_cv_prog_cc_cross=no
+ else
+ ac_cv_prog_cc_cross=yes
+ fi
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ ac_cv_prog_cc_works=no
+fi
+rm -fr conftest*
+
+echo "$ac_t""$ac_cv_prog_cc_works" 1>&6
+if test $ac_cv_prog_cc_works = no; then
+ { echo "configure: error: installation or configuration problem: C compiler cannot create executables." 1>&2; exit 1; }
+fi
+echo $ac_n "checking whether the C compiler ($CC $CFLAGS $LDFLAGS) is a cross-compiler""... $ac_c" 1>&6
+echo "configure:649: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) is a cross-compiler" >&5
+echo "$ac_t""$ac_cv_prog_cc_cross" 1>&6
+cross_compiling=$ac_cv_prog_cc_cross
+
+echo $ac_n "checking whether we are using GNU C""... $ac_c" 1>&6
+echo "configure:654: checking whether we are using GNU C" >&5
+if eval "test \"`echo '$''{'ac_cv_prog_gcc'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.c <<EOF
+#ifdef __GNUC__
+ yes;
+#endif
+EOF
+if { ac_try='${CC-cc} -E conftest.c'; { (eval echo configure:663: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }; } | egrep yes >/dev/null 2>&1; then
+ ac_cv_prog_gcc=yes
+else
+ ac_cv_prog_gcc=no
+fi
+fi
+
+echo "$ac_t""$ac_cv_prog_gcc" 1>&6
+
+if test $ac_cv_prog_gcc = yes; then
+ GCC=yes
+ test "${CFLAGS+set}" = set || CFLAGS="-O2"
+else
+ GCC=
+ test "${CFLAGS+set}" = set || CFLAGS="-O"
+fi
+
+echo $ac_n "checking how to run the C preprocessor""... $ac_c" 1>&6
+echo "configure:681: checking how to run the C preprocessor" >&5
+# On Suns, sometimes $CPP names a directory.
+if test -n "$CPP" && test -d "$CPP"; then
+ CPP=
+fi
+if test -z "$CPP"; then
+if eval "test \"`echo '$''{'ac_cv_prog_CPP'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ # This must be in double quotes, not single quotes, because CPP may get
+ # substituted into the Makefile and "${CC-cc}" will confuse make.
+ CPP="${CC-cc} -E"
+ # On the NeXT, cc -E runs the code through the compiler's parser,
+ # not just through cpp.
+ cat > conftest.$ac_ext <<EOF
+#line 696 "configure"
+#include "confdefs.h"
+#include <assert.h>
+Syntax Error
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:702: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ :
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ CPP="${CC-cc} -E -traditional-cpp"
+ cat > conftest.$ac_ext <<EOF
+#line 713 "configure"
+#include "confdefs.h"
+#include <assert.h>
+Syntax Error
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:719: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ :
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ CPP=/lib/cpp
+fi
+rm -f conftest*
+fi
+rm -f conftest*
+ ac_cv_prog_CPP="$CPP"
+fi
+ CPP="$ac_cv_prog_CPP"
+else
+ ac_cv_prog_CPP="$CPP"
+fi
+echo "$ac_t""$CPP" 1>&6
+
+echo $ac_n "checking for function prototypes""... $ac_c" 1>&6
+echo "configure:742: checking for function prototypes" >&5
+if eval "test \"`echo '$''{'ijg_cv_have_prototypes'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 747 "configure"
+#include "confdefs.h"
+
+int testfunction (int arg1, int * arg2); /* check prototypes */
+struct methods_struct { /* check method-pointer declarations */
+ int (*error_exit) (char *msgtext);
+ int (*trace_message) (char *msgtext);
+ int (*another_method) (void);
+};
+int testfunction (int arg1, int * arg2) /* check definitions */
+{ return arg2[arg1]; }
+int test2function (void) /* check void arg list */
+{ return 0; }
+
+int main() {
+
+; return 0; }
+EOF
+if { (eval echo configure:765: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ijg_cv_have_prototypes=yes
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ ijg_cv_have_prototypes=no
+fi
+rm -f conftest*
+fi
+
+echo "$ac_t""$ijg_cv_have_prototypes" 1>&6
+if test $ijg_cv_have_prototypes = yes; then
+ cat >> confdefs.h <<\EOF
+#define HAVE_PROTOTYPES
+EOF
+
+else
+ echo Your compiler does not seem to know about function prototypes.
+ echo Perhaps it needs a special switch to enable ANSI C mode.
+ echo If so, we recommend running configure like this:
+ echo " ./configure CC='cc -switch'"
+ echo where -switch is the proper switch.
+fi
+ac_safe=`echo "stddef.h" | sed 'y%./+-%__p_%'`
+echo $ac_n "checking for stddef.h""... $ac_c" 1>&6
+echo "configure:792: checking for stddef.h" >&5
+if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 797 "configure"
+#include "confdefs.h"
+#include <stddef.h>
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:802: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=yes"
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=no"
+fi
+rm -f conftest*
+fi
+if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then
+ echo "$ac_t""yes" 1>&6
+ cat >> confdefs.h <<\EOF
+#define HAVE_STDDEF_H
+EOF
+
+else
+ echo "$ac_t""no" 1>&6
+fi
+
+ac_safe=`echo "stdlib.h" | sed 'y%./+-%__p_%'`
+echo $ac_n "checking for stdlib.h""... $ac_c" 1>&6
+echo "configure:828: checking for stdlib.h" >&5
+if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 833 "configure"
+#include "confdefs.h"
+#include <stdlib.h>
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:838: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=yes"
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=no"
+fi
+rm -f conftest*
+fi
+if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then
+ echo "$ac_t""yes" 1>&6
+ cat >> confdefs.h <<\EOF
+#define HAVE_STDLIB_H
+EOF
+
+else
+ echo "$ac_t""no" 1>&6
+fi
+
+ac_safe=`echo "string.h" | sed 'y%./+-%__p_%'`
+echo $ac_n "checking for string.h""... $ac_c" 1>&6
+echo "configure:864: checking for string.h" >&5
+if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 869 "configure"
+#include "confdefs.h"
+#include <string.h>
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:874: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=yes"
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=no"
+fi
+rm -f conftest*
+fi
+if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then
+ echo "$ac_t""yes" 1>&6
+ :
+else
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define NEED_BSD_STRINGS
+EOF
+
+fi
+
+echo $ac_n "checking for size_t""... $ac_c" 1>&6
+echo "configure:900: checking for size_t" >&5
+cat > conftest.$ac_ext <<EOF
+#line 902 "configure"
+#include "confdefs.h"
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#include <stdio.h>
+#ifdef NEED_BSD_STRINGS
+#include <strings.h>
+#else
+#include <string.h>
+#endif
+typedef size_t my_size_t;
+
+int main() {
+ my_size_t foovar;
+; return 0; }
+EOF
+if { (eval echo configure:923: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ijg_size_t_ok=yes
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ ijg_size_t_ok="not ANSI, perhaps it is in sys/types.h"
+fi
+rm -f conftest*
+echo "$ac_t""$ijg_size_t_ok" 1>&6
+if test "$ijg_size_t_ok" != yes; then
+ac_safe=`echo "sys/types.h" | sed 'y%./+-%__p_%'`
+echo $ac_n "checking for sys/types.h""... $ac_c" 1>&6
+echo "configure:937: checking for sys/types.h" >&5
+if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 942 "configure"
+#include "confdefs.h"
+#include <sys/types.h>
+EOF
+ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
+{ (eval echo configure:947: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+ac_err=`grep -v '^ *+' conftest.out`
+if test -z "$ac_err"; then
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=yes"
+else
+ echo "$ac_err" >&5
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ eval "ac_cv_header_$ac_safe=no"
+fi
+rm -f conftest*
+fi
+if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then
+ echo "$ac_t""yes" 1>&6
+ cat >> confdefs.h <<\EOF
+#define NEED_SYS_TYPES_H
+EOF
+
+cat > conftest.$ac_ext <<EOF
+#line 968 "configure"
+#include "confdefs.h"
+#include <sys/types.h>
+EOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+ egrep "size_t" >/dev/null 2>&1; then
+ rm -rf conftest*
+ ijg_size_t_ok="size_t is in sys/types.h"
+else
+ rm -rf conftest*
+ ijg_size_t_ok=no
+fi
+rm -f conftest*
+
+else
+ echo "$ac_t""no" 1>&6
+ijg_size_t_ok=no
+fi
+
+echo "$ac_t""$ijg_size_t_ok" 1>&6
+if test "$ijg_size_t_ok" = no; then
+ echo Type size_t is not defined in any of the usual places.
+ echo Try putting '"typedef unsigned int size_t;"' in jconfig.h.
+fi
+fi
+echo $ac_n "checking for type unsigned char""... $ac_c" 1>&6
+echo "configure:994: checking for type unsigned char" >&5
+cat > conftest.$ac_ext <<EOF
+#line 996 "configure"
+#include "confdefs.h"
+
+int main() {
+ unsigned char un_char;
+; return 0; }
+EOF
+if { (eval echo configure:1003: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ echo "$ac_t""yes" 1>&6
+cat >> confdefs.h <<\EOF
+#define HAVE_UNSIGNED_CHAR
+EOF
+
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""no" 1>&6
+fi
+rm -f conftest*
+echo $ac_n "checking for type unsigned short""... $ac_c" 1>&6
+echo "configure:1018: checking for type unsigned short" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1020 "configure"
+#include "confdefs.h"
+
+int main() {
+ unsigned short un_short;
+; return 0; }
+EOF
+if { (eval echo configure:1027: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ echo "$ac_t""yes" 1>&6
+cat >> confdefs.h <<\EOF
+#define HAVE_UNSIGNED_SHORT
+EOF
+
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""no" 1>&6
+fi
+rm -f conftest*
+echo $ac_n "checking for type void""... $ac_c" 1>&6
+echo "configure:1042: checking for type void" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1044 "configure"
+#include "confdefs.h"
+
+/* Caution: a C++ compiler will insist on valid prototypes */
+typedef void * void_ptr; /* check void * */
+#ifdef HAVE_PROTOTYPES /* check ptr to function returning void */
+typedef void (*void_func) (int a, int b);
+#else
+typedef void (*void_func) ();
+#endif
+
+#ifdef HAVE_PROTOTYPES /* check void function result */
+void test3function (void_ptr arg1, void_func arg2)
+#else
+void test3function (arg1, arg2)
+ void_ptr arg1;
+ void_func arg2;
+#endif
+{
+ char * locptr = (char *) arg1; /* check casting to and from void * */
+ arg1 = (void *) locptr;
+ (*arg2) (1, 2); /* check call of fcn returning void */
+}
+
+int main() {
+
+; return 0; }
+EOF
+if { (eval echo configure:1072: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ echo "$ac_t""yes" 1>&6
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define void char
+EOF
+
+fi
+rm -f conftest*
+
+echo $ac_n "checking for working const""... $ac_c" 1>&6
+echo "configure:1088: checking for working const" >&5
+if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 1093 "configure"
+#include "confdefs.h"
+
+int main() {
+
+/* Ultrix mips cc rejects this. */
+typedef int charset[2]; const charset x;
+/* SunOS 4.1.1 cc rejects this. */
+char const *const *ccp;
+char **p;
+/* NEC SVR4.0.2 mips cc rejects this. */
+struct point {int x, y;};
+static struct point const zero = {0,0};
+/* AIX XL C 1.02.0.0 rejects this.
+ It does not let you subtract one const X* pointer from another in an arm
+ of an if-expression whose if-part is not a constant expression */
+const char *g = "string";
+ccp = &g + (g ? g-g : 0);
+/* HPUX 7.0 cc rejects these. */
+++ccp;
+p = (char**) ccp;
+ccp = (char const *const *) p;
+{ /* SCO 3.2v4 cc rejects this. */
+ char *t;
+ char const *s = 0 ? (char *) 0 : (char const *) 0;
+
+ *t++ = 0;
+}
+{ /* Someone thinks the Sun supposedly-ANSI compiler will reject this. */
+ int x[] = {25, 17};
+ const int *foo = &x[0];
+ ++foo;
+}
+{ /* Sun SC1.0 ANSI compiler rejects this -- but not the above. */
+ typedef const int *iptr;
+ iptr p = 0;
+ ++p;
+}
+{ /* AIX XL C 1.02.0.0 rejects this saying
+ "k.c", line 2.27: 1506-025 (S) Operand must be a modifiable lvalue. */
+ struct s { int j; const int *ap[3]; };
+ struct s *b; b->j = 5;
+}
+{ /* ULTRIX-32 V3.1 (Rev 9) vcc rejects this */
+ const int foo = 10;
+}
+
+; return 0; }
+EOF
+if { (eval echo configure:1142: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ac_cv_c_const=yes
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ ac_cv_c_const=no
+fi
+rm -f conftest*
+fi
+
+echo "$ac_t""$ac_cv_c_const" 1>&6
+if test $ac_cv_c_const = no; then
+ cat >> confdefs.h <<\EOF
+#define const
+EOF
+
+fi
+
+echo $ac_n "checking for inline""... $ac_c" 1>&6
+echo "configure:1163: checking for inline" >&5
+ijg_cv_inline=""
+cat > conftest.$ac_ext <<EOF
+#line 1166 "configure"
+#include "confdefs.h"
+
+int main() {
+} __inline__ int foo() { return 0; }
+int bar() { return foo();
+; return 0; }
+EOF
+if { (eval echo configure:1174: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ijg_cv_inline="__inline__"
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ cat > conftest.$ac_ext <<EOF
+#line 1182 "configure"
+#include "confdefs.h"
+
+int main() {
+} __inline int foo() { return 0; }
+int bar() { return foo();
+; return 0; }
+EOF
+if { (eval echo configure:1190: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ijg_cv_inline="__inline"
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ cat > conftest.$ac_ext <<EOF
+#line 1198 "configure"
+#include "confdefs.h"
+
+int main() {
+} inline int foo() { return 0; }
+int bar() { return foo();
+; return 0; }
+EOF
+if { (eval echo configure:1206: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ ijg_cv_inline="inline"
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+fi
+rm -f conftest*
+fi
+rm -f conftest*
+fi
+rm -f conftest*
+echo "$ac_t""$ijg_cv_inline" 1>&6
+cat >> confdefs.h <<EOF
+#define INLINE $ijg_cv_inline
+EOF
+
+echo $ac_n "checking for broken incomplete types""... $ac_c" 1>&6
+echo "configure:1224: checking for broken incomplete types" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1226 "configure"
+#include "confdefs.h"
+ typedef struct undefined_structure * undef_struct_ptr;
+int main() {
+
+; return 0; }
+EOF
+if { (eval echo configure:1233: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+ rm -rf conftest*
+ echo "$ac_t""ok" 1>&6
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""broken" 1>&6
+cat >> confdefs.h <<\EOF
+#define INCOMPLETE_TYPES_BROKEN
+EOF
+
+fi
+rm -f conftest*
+echo $ac_n "checking for short external names""... $ac_c" 1>&6
+echo "configure:1248: checking for short external names" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1250 "configure"
+#include "confdefs.h"
+
+int possibly_duplicate_function () { return 0; }
+int possibly_dupli_function () { return 1; }
+
+int main() {
+
+; return 0; }
+EOF
+if { (eval echo configure:1260: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ rm -rf conftest*
+ echo "$ac_t""ok" 1>&6
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""short" 1>&6
+cat >> confdefs.h <<\EOF
+#define NEED_SHORT_EXTERNAL_NAMES
+EOF
+
+fi
+rm -f conftest*
+echo $ac_n "checking to see if char is signed""... $ac_c" 1>&6
+echo "configure:1275: checking to see if char is signed" >&5
+if test "$cross_compiling" = yes; then
+ echo Assuming that char is signed on target machine.
+echo If it is unsigned, this will be a little bit inefficient.
+
+else
+ cat > conftest.$ac_ext <<EOF
+#line 1282 "configure"
+#include "confdefs.h"
+
+#ifdef HAVE_PROTOTYPES
+int is_char_signed (int arg)
+#else
+int is_char_signed (arg)
+ int arg;
+#endif
+{
+ if (arg == 189) { /* expected result for unsigned char */
+ return 0; /* type char is unsigned */
+ }
+ else if (arg != -67) { /* expected result for signed char */
+ printf("Hmm, it seems 'char' is not eight bits wide on your machine.\n");
+ printf("I fear the JPEG software will not work at all.\n\n");
+ }
+ return 1; /* assume char is signed otherwise */
+}
+char signed_char_check = (char) (-67);
+main() {
+ exit(is_char_signed((int) signed_char_check));
+}
+EOF
+if { (eval echo configure:1306: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+then
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define CHAR_IS_UNSIGNED
+EOF
+
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -fr conftest*
+ echo "$ac_t""yes" 1>&6
+fi
+rm -fr conftest*
+fi
+
+echo $ac_n "checking to see if right shift is signed""... $ac_c" 1>&6
+echo "configure:1323: checking to see if right shift is signed" >&5
+if test "$cross_compiling" = yes; then
+ echo "$ac_t""Assuming that right shift is signed on target machine." 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 1328 "configure"
+#include "confdefs.h"
+
+#ifdef HAVE_PROTOTYPES
+int is_shifting_signed (long arg)
+#else
+int is_shifting_signed (arg)
+ long arg;
+#endif
+/* See whether right-shift on a long is signed or not. */
+{
+ long res = arg >> 4;
+
+ if (res == -0x7F7E80CL) { /* expected result for signed shift */
+ return 1; /* right shift is signed */
+ }
+ /* see if unsigned-shift hack will fix it. */
+ /* we can't just test exact value since it depends on width of long... */
+ res |= (~0L) << (32-4);
+ if (res == -0x7F7E80CL) { /* expected result now? */
+ return 0; /* right shift is unsigned */
+ }
+ printf("Right shift isn't acting as I expect it to.\n");
+ printf("I fear the JPEG software will not work at all.\n\n");
+ return 0; /* try it with unsigned anyway */
+}
+main() {
+ exit(is_shifting_signed(-0x7F7E80B1L));
+}
+EOF
+if { (eval echo configure:1358: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+then
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define RIGHT_SHIFT_IS_UNSIGNED
+EOF
+
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -fr conftest*
+ echo "$ac_t""yes" 1>&6
+fi
+rm -fr conftest*
+fi
+
+echo $ac_n "checking to see if fopen accepts b spec""... $ac_c" 1>&6
+echo "configure:1375: checking to see if fopen accepts b spec" >&5
+if test "$cross_compiling" = yes; then
+ echo "$ac_t""Assuming that it does." 1>&6
+else
+ cat > conftest.$ac_ext <<EOF
+#line 1380 "configure"
+#include "confdefs.h"
+
+#include <stdio.h>
+main() {
+ if (fopen("conftestdata", "wb") != NULL)
+ exit(0);
+ exit(1);
+}
+EOF
+if { (eval echo configure:1390: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+then
+ echo "$ac_t""yes" 1>&6
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -fr conftest*
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define DONT_USE_B_MODE
+EOF
+
+fi
+rm -fr conftest*
+fi
+
+ac_aux_dir=
+for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do
+ if test -f $ac_dir/install-sh; then
+ ac_aux_dir=$ac_dir
+ ac_install_sh="$ac_aux_dir/install-sh -c"
+ break
+ elif test -f $ac_dir/install.sh; then
+ ac_aux_dir=$ac_dir
+ ac_install_sh="$ac_aux_dir/install.sh -c"
+ break
+ fi
+done
+if test -z "$ac_aux_dir"; then
+ { echo "configure: error: can not find install-sh or install.sh in $srcdir $srcdir/.. $srcdir/../.." 1>&2; exit 1; }
+fi
+ac_config_guess=$ac_aux_dir/config.guess
+ac_config_sub=$ac_aux_dir/config.sub
+ac_configure=$ac_aux_dir/configure # This should be Cygnus configure.
+
+# Find a good install program. We prefer a C program (faster),
+# so one script is as good as another. But avoid the broken or
+# incompatible versions:
+# SysV /etc/install, /usr/sbin/install
+# SunOS /usr/etc/install
+# IRIX /sbin/install
+# AIX /bin/install
+# AFS /usr/afsws/bin/install, which mishandles nonexistent args
+# SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff"
+# ./install, which can be erroneously created by make from ./install.sh.
+echo $ac_n "checking for a BSD compatible install""... $ac_c" 1>&6
+echo "configure:1436: checking for a BSD compatible install" >&5
+if test -z "$INSTALL"; then
+if eval "test \"`echo '$''{'ac_cv_path_install'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ IFS="${IFS= }"; ac_save_IFS="$IFS"; IFS="${IFS}:"
+ for ac_dir in $PATH; do
+ # Account for people who put trailing slashes in PATH elements.
+ case "$ac_dir/" in
+ /|./|.//|/etc/*|/usr/sbin/*|/usr/etc/*|/sbin/*|/usr/afsws/bin/*|/usr/ucb/*) ;;
+ *)
+ # OSF1 and SCO ODT 3.0 have their own names for install.
+ for ac_prog in ginstall installbsd scoinst install; do
+ if test -f $ac_dir/$ac_prog; then
+ if test $ac_prog = install &&
+ grep dspmsg $ac_dir/$ac_prog >/dev/null 2>&1; then
+ # AIX install. It has an incompatible calling convention.
+ # OSF/1 installbsd also uses dspmsg, but is usable.
+ :
+ else
+ ac_cv_path_install="$ac_dir/$ac_prog -c"
+ break 2
+ fi
+ fi
+ done
+ ;;
+ esac
+ done
+ IFS="$ac_save_IFS"
+
+fi
+ if test "${ac_cv_path_install+set}" = set; then
+ INSTALL="$ac_cv_path_install"
+ else
+ # As a last resort, use the slow shell script. We don't cache a
+ # path for INSTALL within a source directory, because that will
+ # break other packages using the cache if that directory is
+ # removed, or if the path is relative.
+ INSTALL="$ac_install_sh"
+ fi
+fi
+echo "$ac_t""$INSTALL" 1>&6
+
+# Use test -z because SunOS4 sh mishandles braces in ${var-val}.
+# It thinks the first close brace ends the variable substitution.
+test -z "$INSTALL_PROGRAM" && INSTALL_PROGRAM='${INSTALL}'
+
+test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644'
+
+# Extract the first word of "ranlib", so it can be a program name with args.
+set dummy ranlib; ac_word=$2
+echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
+echo "configure:1488: checking for $ac_word" >&5
+if eval "test \"`echo '$''{'ac_cv_prog_RANLIB'+set}'`\" = set"; then
+ echo $ac_n "(cached) $ac_c" 1>&6
+else
+ if test -n "$RANLIB"; then
+ ac_cv_prog_RANLIB="$RANLIB" # Let the user override the test.
+else
+ IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
+ for ac_dir in $PATH; do
+ test -z "$ac_dir" && ac_dir=.
+ if test -f $ac_dir/$ac_word; then
+ ac_cv_prog_RANLIB="ranlib"
+ break
+ fi
+ done
+ IFS="$ac_save_ifs"
+ test -z "$ac_cv_prog_RANLIB" && ac_cv_prog_RANLIB=":"
+fi
+fi
+RANLIB="$ac_cv_prog_RANLIB"
+if test -n "$RANLIB"; then
+ echo "$ac_t""$RANLIB" 1>&6
+else
+ echo "$ac_t""no" 1>&6
+fi
+
+
+# Decide whether to use libtool,
+# and if so whether to build shared, static, or both flavors of library.
+LTSHARED="no"
+# Check whether --enable-shared or --disable-shared was given.
+if test "${enable_shared+set}" = set; then
+ enableval="$enable_shared"
+ LTSHARED="$enableval"
+fi
+
+LTSTATIC="no"
+# Check whether --enable-static or --disable-static was given.
+if test "${enable_static+set}" = set; then
+ enableval="$enable_static"
+ LTSTATIC="$enableval"
+fi
+
+if test "x$LTSHARED" != xno -o "x$LTSTATIC" != xno; then
+ USELIBTOOL="yes"
+ LIBTOOL="./libtool"
+ O="lo"
+ A="la"
+ LN='$(LIBTOOL) --mode=link $(CC)'
+ INSTALL_LIB='$(LIBTOOL) --mode=install ${INSTALL}'
+ INSTALL_PROGRAM="\$(LIBTOOL) --mode=install $INSTALL_PROGRAM"
+else
+ USELIBTOOL="no"
+ LIBTOOL=""
+ O="o"
+ A="a"
+ LN='$(CC)'
+ INSTALL_LIB="$INSTALL_DATA"
+fi
+
+
+
+
+
+
+# Configure libtool if needed.
+if test $USELIBTOOL = yes; then
+ disable_shared=
+ disable_static=
+ if test "x$LTSHARED" = xno; then
+ disable_shared="--disable-shared"
+ fi
+ if test "x$LTSTATIC" = xno; then
+ disable_static="--disable-static"
+ fi
+ $srcdir/ltconfig $disable_shared $disable_static $srcdir/ltmain.sh
+fi
+
+# Select memory manager depending on user input.
+# If no "-enable-maxmem", use jmemnobs
+MEMORYMGR='jmemnobs.$(O)'
+MAXMEM="no"
+# Check whether --enable-maxmem or --disable-maxmem was given.
+if test "${enable_maxmem+set}" = set; then
+ enableval="$enable_maxmem"
+ MAXMEM="$enableval"
+fi
+
+# support --with-maxmem for backwards compatibility with IJG V5.
+# Check whether --with-maxmem or --without-maxmem was given.
+if test "${with_maxmem+set}" = set; then
+ withval="$with_maxmem"
+ MAXMEM="$withval"
+fi
+
+if test "x$MAXMEM" = xyes; then
+ MAXMEM=1
+fi
+if test "x$MAXMEM" != xno; then
+ if test -n "`echo $MAXMEM | sed 's/[0-9]//g'`"; then
+ { echo "configure: error: non-numeric argument to --enable-maxmem" 1>&2; exit 1; }
+ fi
+ DEFAULTMAXMEM=`expr $MAXMEM \* 1048576`
+cat >> confdefs.h <<EOF
+#define DEFAULT_MAX_MEM ${DEFAULTMAXMEM}
+EOF
+
+echo $ac_n "checking for 'tmpfile()'""... $ac_c" 1>&6
+echo "configure:1596: checking for 'tmpfile()'" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1598 "configure"
+#include "confdefs.h"
+#include <stdio.h>
+int main() {
+ FILE * tfile = tmpfile();
+; return 0; }
+EOF
+if { (eval echo configure:1605: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ rm -rf conftest*
+ echo "$ac_t""yes" 1>&6
+MEMORYMGR='jmemansi.$(O)'
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""no" 1>&6
+MEMORYMGR='jmemname.$(O)'
+cat >> confdefs.h <<\EOF
+#define NEED_SIGNAL_CATCHER
+EOF
+
+echo $ac_n "checking for 'mktemp()'""... $ac_c" 1>&6
+echo "configure:1620: checking for 'mktemp()'" >&5
+cat > conftest.$ac_ext <<EOF
+#line 1622 "configure"
+#include "confdefs.h"
+
+int main() {
+ char fname[80]; mktemp(fname);
+; return 0; }
+EOF
+if { (eval echo configure:1629: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ rm -rf conftest*
+ echo "$ac_t""yes" 1>&6
+else
+ echo "configure: failed program was:" >&5
+ cat conftest.$ac_ext >&5
+ rm -rf conftest*
+ echo "$ac_t""no" 1>&6
+cat >> confdefs.h <<\EOF
+#define NO_MKTEMP
+EOF
+
+fi
+rm -f conftest*
+fi
+rm -f conftest*
+fi
+
+
+# Extract the library version ID from jpeglib.h.
+echo $ac_n "checking libjpeg version number""... $ac_c" 1>&6
+echo "configure:1650: checking libjpeg version number" >&5
+JPEG_LIB_VERSION=`sed -e '/^#define JPEG_LIB_VERSION/!d' -e 's/^[^0-9]*\([0-9][0-9]*\).*$/\1/' $srcdir/jpeglib.h`
+echo "$ac_t""$JPEG_LIB_VERSION" 1>&6
+
+
+# Prepare to massage makefile.cfg correctly.
+if test $ijg_cv_have_prototypes = yes; then
+ A2K_DEPS=""
+ COM_A2K="# "
+else
+ A2K_DEPS="ansi2knr"
+ COM_A2K=""
+fi
+
+
+# ansi2knr needs -DBSD if string.h is missing
+if test $ac_cv_header_string_h = no; then
+ ANSI2KNRFLAGS="-DBSD"
+else
+ ANSI2KNRFLAGS=""
+fi
+
+# Substitutions to enable or disable libtool-related stuff
+if test $USELIBTOOL = yes -a $ijg_cv_have_prototypes = yes; then
+ COM_LT=""
+else
+ COM_LT="# "
+fi
+
+if test "x$LTSHARED" != xno; then
+ FORCE_INSTALL_LIB="install-lib"
+else
+ FORCE_INSTALL_LIB=""
+fi
+
+# Set up -I directives
+if test "x$srcdir" = x.; then
+ INCLUDEFLAGS='-I$(srcdir)'
+else
+ INCLUDEFLAGS='-I. -I$(srcdir)'
+fi
+
+trap '' 1 2 15
+
+trap 'rm -fr conftest* confdefs* core core.* *.core $ac_clean_files; exit 1' 1 2 15
+
+test "x$prefix" = xNONE && prefix=$ac_default_prefix
+# Let make expand exec_prefix.
+test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
+
+# Any assignment to VPATH causes Sun make to only execute
+# the first set of double-colon rules, so remove it if not needed.
+# If there is a colon in the path, we need to keep it.
+if test "x$srcdir" = x.; then
+ ac_vpsub='/^[ ]*VPATH[ ]*=[^:]*$/d'
+fi
+
+trap 'rm -f $CONFIG_STATUS conftest*; exit 1' 1 2 15
+
+DEFS=-DHAVE_CONFIG_H
+
+# Without the "./", some shells look in PATH for config.status.
+: ${CONFIG_STATUS=./config.status}
+
+echo creating $CONFIG_STATUS
+rm -f $CONFIG_STATUS
+cat > $CONFIG_STATUS <<EOF
+#! /bin/sh
+# Generated automatically by configure.
+# Run this file to recreate the current configuration.
+# This directory was configured as follows,
+# on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
+#
+# $0 $ac_configure_args
+#
+# Compiler output produced by configure, useful for debugging
+# configure, is in ./config.log if it exists.
+
+ac_cs_usage="Usage: $CONFIG_STATUS [--recheck] [--version] [--help]"
+for ac_option
+do
+ case "\$ac_option" in
+ -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
+ echo "running \${CONFIG_SHELL-/bin/sh} $0 $ac_configure_args --no-create --no-recursion"
+ exec \${CONFIG_SHELL-/bin/sh} $0 $ac_configure_args --no-create --no-recursion ;;
+ -version | --version | --versio | --versi | --vers | --ver | --ve | --v)
+ echo "$CONFIG_STATUS generated by autoconf version 2.12"
+ exit 0 ;;
+ -help | --help | --hel | --he | --h)
+ echo "\$ac_cs_usage"; exit 0 ;;
+ *) echo "\$ac_cs_usage"; exit 1 ;;
+ esac
+done
+
+ac_given_srcdir=$srcdir
+ac_given_INSTALL="$INSTALL"
+
+trap 'rm -fr `echo "Makefile:makefile.cfg jconfig.h:jconfig.cfg" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
+EOF
+cat >> $CONFIG_STATUS <<EOF
+
+# Protect against being on the right side of a sed subst in config.status.
+sed 's/%@/@@/; s/@%/@@/; s/%g\$/@g/; /@g\$/s/[\\\\&%]/\\\\&/g;
+ s/@@/%@/; s/@@/@%/; s/@g\$/%g/' > conftest.subs <<\\CEOF
+$ac_vpsub
+$extrasub
+s%@CFLAGS@%$CFLAGS%g
+s%@CPPFLAGS@%$CPPFLAGS%g
+s%@CXXFLAGS@%$CXXFLAGS%g
+s%@DEFS@%$DEFS%g
+s%@LDFLAGS@%$LDFLAGS%g
+s%@LIBS@%$LIBS%g
+s%@exec_prefix@%$exec_prefix%g
+s%@prefix@%$prefix%g
+s%@program_transform_name@%$program_transform_name%g
+s%@bindir@%$bindir%g
+s%@sbindir@%$sbindir%g
+s%@libexecdir@%$libexecdir%g
+s%@datadir@%$datadir%g
+s%@sysconfdir@%$sysconfdir%g
+s%@sharedstatedir@%$sharedstatedir%g
+s%@localstatedir@%$localstatedir%g
+s%@libdir@%$libdir%g
+s%@includedir@%$includedir%g
+s%@oldincludedir@%$oldincludedir%g
+s%@infodir@%$infodir%g
+s%@mandir@%$mandir%g
+s%@CC@%$CC%g
+s%@CPP@%$CPP%g
+s%@INSTALL_PROGRAM@%$INSTALL_PROGRAM%g
+s%@INSTALL_DATA@%$INSTALL_DATA%g
+s%@RANLIB@%$RANLIB%g
+s%@LIBTOOL@%$LIBTOOL%g
+s%@O@%$O%g
+s%@A@%$A%g
+s%@LN@%$LN%g
+s%@INSTALL_LIB@%$INSTALL_LIB%g
+s%@MEMORYMGR@%$MEMORYMGR%g
+s%@JPEG_LIB_VERSION@%$JPEG_LIB_VERSION%g
+s%@A2K_DEPS@%$A2K_DEPS%g
+s%@COM_A2K@%$COM_A2K%g
+s%@ANSI2KNRFLAGS@%$ANSI2KNRFLAGS%g
+s%@COM_LT@%$COM_LT%g
+s%@FORCE_INSTALL_LIB@%$FORCE_INSTALL_LIB%g
+s%@INCLUDEFLAGS@%$INCLUDEFLAGS%g
+
+CEOF
+EOF
+
+cat >> $CONFIG_STATUS <<\EOF
+
+# Split the substitutions into bite-sized pieces for seds with
+# small command number limits, like on Digital OSF/1 and HP-UX.
+ac_max_sed_cmds=90 # Maximum number of lines to put in a sed script.
+ac_file=1 # Number of current file.
+ac_beg=1 # First line for current file.
+ac_end=$ac_max_sed_cmds # Line after last line for current file.
+ac_more_lines=:
+ac_sed_cmds=""
+while $ac_more_lines; do
+ if test $ac_beg -gt 1; then
+ sed "1,${ac_beg}d; ${ac_end}q" conftest.subs > conftest.s$ac_file
+ else
+ sed "${ac_end}q" conftest.subs > conftest.s$ac_file
+ fi
+ if test ! -s conftest.s$ac_file; then
+ ac_more_lines=false
+ rm -f conftest.s$ac_file
+ else
+ if test -z "$ac_sed_cmds"; then
+ ac_sed_cmds="sed -f conftest.s$ac_file"
+ else
+ ac_sed_cmds="$ac_sed_cmds | sed -f conftest.s$ac_file"
+ fi
+ ac_file=`expr $ac_file + 1`
+ ac_beg=$ac_end
+ ac_end=`expr $ac_end + $ac_max_sed_cmds`
+ fi
+done
+if test -z "$ac_sed_cmds"; then
+ ac_sed_cmds=cat
+fi
+EOF
+
+cat >> $CONFIG_STATUS <<EOF
+
+CONFIG_FILES=\${CONFIG_FILES-"Makefile:makefile.cfg"}
+EOF
+cat >> $CONFIG_STATUS <<\EOF
+for ac_file in .. $CONFIG_FILES; do if test "x$ac_file" != x..; then
+ # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in".
+ case "$ac_file" in
+ *:*) ac_file_in=`echo "$ac_file"|sed 's%[^:]*:%%'`
+ ac_file=`echo "$ac_file"|sed 's%:.*%%'` ;;
+ *) ac_file_in="${ac_file}.in" ;;
+ esac
+
+ # Adjust a relative srcdir, top_srcdir, and INSTALL for subdirectories.
+
+ # Remove last slash and all that follows it. Not all systems have dirname.
+ ac_dir=`echo $ac_file|sed 's%/[^/][^/]*$%%'`
+ if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then
+ # The file is in a subdirectory.
+ test ! -d "$ac_dir" && mkdir "$ac_dir"
+ ac_dir_suffix="/`echo $ac_dir|sed 's%^\./%%'`"
+ # A "../" for each directory in $ac_dir_suffix.
+ ac_dots=`echo $ac_dir_suffix|sed 's%/[^/]*%../%g'`
+ else
+ ac_dir_suffix= ac_dots=
+ fi
+
+ case "$ac_given_srcdir" in
+ .) srcdir=.
+ if test -z "$ac_dots"; then top_srcdir=.
+ else top_srcdir=`echo $ac_dots|sed 's%/$%%'`; fi ;;
+ /*) srcdir="$ac_given_srcdir$ac_dir_suffix"; top_srcdir="$ac_given_srcdir" ;;
+ *) # Relative path.
+ srcdir="$ac_dots$ac_given_srcdir$ac_dir_suffix"
+ top_srcdir="$ac_dots$ac_given_srcdir" ;;
+ esac
+
+ case "$ac_given_INSTALL" in
+ [/$]*) INSTALL="$ac_given_INSTALL" ;;
+ *) INSTALL="$ac_dots$ac_given_INSTALL" ;;
+ esac
+
+ echo creating "$ac_file"
+ rm -f "$ac_file"
+ configure_input="Generated automatically from `echo $ac_file_in|sed 's%.*/%%'` by configure."
+ case "$ac_file" in
+ *Makefile*) ac_comsub="1i\\
+# $configure_input" ;;
+ *) ac_comsub= ;;
+ esac
+
+ ac_file_inputs=`echo $ac_file_in|sed -e "s%^%$ac_given_srcdir/%" -e "s%:% $ac_given_srcdir/%g"`
+ sed -e "$ac_comsub
+s%@configure_input@%$configure_input%g
+s%@srcdir@%$srcdir%g
+s%@top_srcdir@%$top_srcdir%g
+s%@INSTALL@%$INSTALL%g
+" $ac_file_inputs | (eval "$ac_sed_cmds") > $ac_file
+fi; done
+rm -f conftest.s*
+
+# These sed commands are passed to sed as "A NAME B NAME C VALUE D", where
+# NAME is the cpp macro being defined and VALUE is the value it is being given.
+#
+# ac_d sets the value in "#define NAME VALUE" lines.
+ac_dA='s%^\([ ]*\)#\([ ]*define[ ][ ]*\)'
+ac_dB='\([ ][ ]*\)[^ ]*%\1#\2'
+ac_dC='\3'
+ac_dD='%g'
+# ac_u turns "#undef NAME" with trailing blanks into "#define NAME VALUE".
+ac_uA='s%^\([ ]*\)#\([ ]*\)undef\([ ][ ]*\)'
+ac_uB='\([ ]\)%\1#\2define\3'
+ac_uC=' '
+ac_uD='\4%g'
+# ac_e turns "#undef NAME" without trailing blanks into "#define NAME VALUE".
+ac_eA='s%^\([ ]*\)#\([ ]*\)undef\([ ][ ]*\)'
+ac_eB='$%\1#\2define\3'
+ac_eC=' '
+ac_eD='%g'
+
+if test "${CONFIG_HEADERS+set}" != set; then
+EOF
+cat >> $CONFIG_STATUS <<EOF
+ CONFIG_HEADERS="jconfig.h:jconfig.cfg"
+EOF
+cat >> $CONFIG_STATUS <<\EOF
+fi
+for ac_file in .. $CONFIG_HEADERS; do if test "x$ac_file" != x..; then
+ # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in".
+ case "$ac_file" in
+ *:*) ac_file_in=`echo "$ac_file"|sed 's%[^:]*:%%'`
+ ac_file=`echo "$ac_file"|sed 's%:.*%%'` ;;
+ *) ac_file_in="${ac_file}.in" ;;
+ esac
+
+ echo creating $ac_file
+
+ rm -f conftest.frag conftest.in conftest.out
+ ac_file_inputs=`echo $ac_file_in|sed -e "s%^%$ac_given_srcdir/%" -e "s%:% $ac_given_srcdir/%g"`
+ cat $ac_file_inputs > conftest.in
+
+EOF
+
+# Transform confdefs.h into a sed script conftest.vals that substitutes
+# the proper values into config.h.in to produce config.h. And first:
+# Protect against being on the right side of a sed subst in config.status.
+# Protect against being in an unquoted here document in config.status.
+rm -f conftest.vals
+cat > conftest.hdr <<\EOF
+s/[\\&%]/\\&/g
+s%[\\$`]%\\&%g
+s%#define \([A-Za-z_][A-Za-z0-9_]*\) *\(.*\)%${ac_dA}\1${ac_dB}\1${ac_dC}\2${ac_dD}%gp
+s%ac_d%ac_u%gp
+s%ac_u%ac_e%gp
+EOF
+sed -n -f conftest.hdr confdefs.h > conftest.vals
+rm -f conftest.hdr
+
+# This sed command replaces #undef with comments. This is necessary, for
+# example, in the case of _POSIX_SOURCE, which is predefined and required
+# on some systems where configure will not decide to define it.
+cat >> conftest.vals <<\EOF
+EOF
+
+# Break up conftest.vals because some shells have a limit on
+# the size of here documents, and old seds have small limits too.
+
+rm -f conftest.tail
+while :
+do
+ ac_lines=`grep -c . conftest.vals`
+ # grep -c gives empty output for an empty file on some AIX systems.
+ if test -z "$ac_lines" || test "$ac_lines" -eq 0; then break; fi
+ # Write a limited-size here document to conftest.frag.
+ echo ' cat > conftest.frag <<CEOF' >> $CONFIG_STATUS
+ sed ${ac_max_here_lines}q conftest.vals >> $CONFIG_STATUS
+ echo 'CEOF
+ sed -f conftest.frag conftest.in > conftest.out
+ rm -f conftest.in
+ mv conftest.out conftest.in
+' >> $CONFIG_STATUS
+ sed 1,${ac_max_here_lines}d conftest.vals > conftest.tail
+ rm -f conftest.vals
+ mv conftest.tail conftest.vals
+done
+rm -f conftest.vals
+
+cat >> $CONFIG_STATUS <<\EOF
+ rm -f conftest.frag conftest.h
+ echo "/* $ac_file. Generated automatically by configure. */" > conftest.h
+ cat conftest.in >> conftest.h
+ rm -f conftest.in
+ if cmp -s $ac_file conftest.h 2>/dev/null; then
+ echo "$ac_file is unchanged"
+ rm -f conftest.h
+ else
+ # Remove last slash and all that follows it. Not all systems have dirname.
+ ac_dir=`echo $ac_file|sed 's%/[^/][^/]*$%%'`
+ if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then
+ # The file is in a subdirectory.
+ test ! -d "$ac_dir" && mkdir "$ac_dir"
+ fi
+ rm -f $ac_file
+ mv conftest.h $ac_file
+ fi
+fi; done
+
+EOF
+cat >> $CONFIG_STATUS <<EOF
+
+EOF
+cat >> $CONFIG_STATUS <<\EOF
+
+exit 0
+EOF
+chmod +x $CONFIG_STATUS
+rm -fr confdefs* $ac_clean_files
+test "$no_create" = yes || ${CONFIG_SHELL-/bin/sh} $CONFIG_STATUS || exit 1
+
diff --git a/test/monniaux/jpeg-6b/djpeg.1 b/test/monniaux/jpeg-6b/djpeg.1
new file mode 100644
index 00000000..11beb6a5
--- /dev/null
+++ b/test/monniaux/jpeg-6b/djpeg.1
@@ -0,0 +1,253 @@
+.TH DJPEG 1 "22 August 1997"
+.SH NAME
+djpeg \- decompress a JPEG file to an image file
+.SH SYNOPSIS
+.B djpeg
+[
+.I options
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B djpeg
+decompresses the named JPEG file, or the standard input if no file is named,
+and produces an image file on the standard output. PBMPLUS (PPM/PGM), BMP,
+GIF, Targa, or RLE (Utah Raster Toolkit) output format can be selected.
+(RLE is supported only if the URT library is available.)
+.SH OPTIONS
+All switch names may be abbreviated; for example,
+.B \-grayscale
+may be written
+.B \-gray
+or
+.BR \-gr .
+Most of the "basic" switches can be abbreviated to as little as one letter.
+Upper and lower case are equivalent (thus
+.B \-BMP
+is the same as
+.BR \-bmp ).
+British spellings are also accepted (e.g.,
+.BR \-greyscale ),
+though for brevity these are not mentioned below.
+.PP
+The basic switches are:
+.TP
+.BI \-colors " N"
+Reduce image to at most N colors. This reduces the number of colors used in
+the output image, so that it can be displayed on a colormapped display or
+stored in a colormapped file format. For example, if you have an 8-bit
+display, you'd need to reduce to 256 or fewer colors.
+.TP
+.BI \-quantize " N"
+Same as
+.BR \-colors .
+.B \-colors
+is the recommended name,
+.B \-quantize
+is provided only for backwards compatibility.
+.TP
+.B \-fast
+Select recommended processing options for fast, low quality output. (The
+default options are chosen for highest quality output.) Currently, this is
+equivalent to \fB\-dct fast \-nosmooth \-onepass \-dither ordered\fR.
+.TP
+.B \-grayscale
+Force gray-scale output even if JPEG file is color. Useful for viewing on
+monochrome displays; also,
+.B djpeg
+runs noticeably faster in this mode.
+.TP
+.BI \-scale " M/N"
+Scale the output image by a factor M/N. Currently the scale factor must be
+1/1, 1/2, 1/4, or 1/8. Scaling is handy if the image is larger than your
+screen; also,
+.B djpeg
+runs much faster when scaling down the output.
+.TP
+.B \-bmp
+Select BMP output format (Windows flavor). 8-bit colormapped format is
+emitted if
+.B \-colors
+or
+.B \-grayscale
+is specified, or if the JPEG file is gray-scale; otherwise, 24-bit full-color
+format is emitted.
+.TP
+.B \-gif
+Select GIF output format. Since GIF does not support more than 256 colors,
+.B \-colors 256
+is assumed (unless you specify a smaller number of colors).
+.TP
+.B \-os2
+Select BMP output format (OS/2 1.x flavor). 8-bit colormapped format is
+emitted if
+.B \-colors
+or
+.B \-grayscale
+is specified, or if the JPEG file is gray-scale; otherwise, 24-bit full-color
+format is emitted.
+.TP
+.B \-pnm
+Select PBMPLUS (PPM/PGM) output format (this is the default format).
+PGM is emitted if the JPEG file is gray-scale or if
+.B \-grayscale
+is specified; otherwise PPM is emitted.
+.TP
+.B \-rle
+Select RLE output format. (Requires URT library.)
+.TP
+.B \-targa
+Select Targa output format. Gray-scale format is emitted if the JPEG file is
+gray-scale or if
+.B \-grayscale
+is specified; otherwise, colormapped format is emitted if
+.B \-colors
+is specified; otherwise, 24-bit full-color format is emitted.
+.PP
+Switches for advanced users:
+.TP
+.B \-dct int
+Use integer DCT method (default).
+.TP
+.B \-dct fast
+Use fast integer DCT (less accurate).
+.TP
+.B \-dct float
+Use floating-point DCT method.
+The float method is very slightly more accurate than the int method, but is
+much slower unless your machine has very fast floating-point hardware. Also
+note that results of the floating-point method may vary slightly across
+machines, while the integer methods should give the same results everywhere.
+The fast integer method is much less accurate than the other two.
+.TP
+.B \-dither fs
+Use Floyd-Steinberg dithering in color quantization.
+.TP
+.B \-dither ordered
+Use ordered dithering in color quantization.
+.TP
+.B \-dither none
+Do not use dithering in color quantization.
+By default, Floyd-Steinberg dithering is applied when quantizing colors; this
+is slow but usually produces the best results. Ordered dither is a compromise
+between speed and quality; no dithering is fast but usually looks awful. Note
+that these switches have no effect unless color quantization is being done.
+Ordered dither is only available in
+.B \-onepass
+mode.
+.TP
+.BI \-map " file"
+Quantize to the colors used in the specified image file. This is useful for
+producing multiple files with identical color maps, or for forcing a
+predefined set of colors to be used. The
+.I file
+must be a GIF or PPM file. This option overrides
+.B \-colors
+and
+.BR \-onepass .
+.TP
+.B \-nosmooth
+Use a faster, lower-quality upsampling routine.
+.TP
+.B \-onepass
+Use one-pass instead of two-pass color quantization. The one-pass method is
+faster and needs less memory, but it produces a lower-quality image.
+.B \-onepass
+is ignored unless you also say
+.B \-colors
+.IR N .
+Also, the one-pass method is always used for gray-scale output (the two-pass
+method is no improvement then).
+.TP
+.BI \-maxmemory " N"
+Set limit for amount of memory to use in processing large images. Value is
+in thousands of bytes, or millions of bytes if "M" is attached to the
+number. For example,
+.B \-max 4m
+selects 4000000 bytes. If more space is needed, temporary files will be used.
+.TP
+.BI \-outfile " name"
+Send output image to the named file, not to standard output.
+.TP
+.B \-verbose
+Enable debug printout. More
+.BR \-v 's
+give more output. Also, version information is printed at startup.
+.TP
+.B \-debug
+Same as
+.BR \-verbose .
+.SH EXAMPLES
+.LP
+This example decompresses the JPEG file foo.jpg, quantizes it to
+256 colors, and saves the output in 8-bit BMP format in foo.bmp:
+.IP
+.B djpeg \-colors 256 \-bmp
+.I foo.jpg
+.B >
+.I foo.bmp
+.SH HINTS
+To get a quick preview of an image, use the
+.B \-grayscale
+and/or
+.B \-scale
+switches.
+.B \-grayscale \-scale 1/8
+is the fastest case.
+.PP
+Several options are available that trade off image quality to gain speed.
+.B \-fast
+turns on the recommended settings.
+.PP
+.B \-dct fast
+and/or
+.B \-nosmooth
+gain speed at a small sacrifice in quality.
+When producing a color-quantized image,
+.B \-onepass \-dither ordered
+is fast but much lower quality than the default behavior.
+.B \-dither none
+may give acceptable results in two-pass mode, but is seldom tolerable in
+one-pass mode.
+.PP
+If you are fortunate enough to have very fast floating point hardware,
+\fB\-dct float\fR may be even faster than \fB\-dct fast\fR. But on most
+machines \fB\-dct float\fR is slower than \fB\-dct int\fR; in this case it is
+not worth using, because its theoretical accuracy advantage is too small to be
+significant in practice.
+.SH ENVIRONMENT
+.TP
+.B JPEGMEM
+If this environment variable is set, its value is the default memory limit.
+The value is specified as described for the
+.B \-maxmemory
+switch.
+.B JPEGMEM
+overrides the default value specified when the program was compiled, and
+itself is overridden by an explicit
+.BR \-maxmemory .
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR jpegtran (1),
+.BR rdjpgcom (1),
+.BR wrjpgcom (1)
+.br
+.BR ppm (5),
+.BR pgm (5)
+.br
+Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
+Communications of the ACM, April 1991 (vol. 34, no. 4), pp. 30-44.
+.SH AUTHOR
+Independent JPEG Group
+.SH BUGS
+Arithmetic coding is not supported for legal reasons.
+.PP
+To avoid the Unisys LZW patent,
+.B djpeg
+produces uncompressed GIF files. These are larger than they should be, but
+are readable by standard GIF decoders.
+.PP
+Still not as fast as we'd like.
diff --git a/test/monniaux/jpeg-6b/djpeg.c b/test/monniaux/jpeg-6b/djpeg.c
new file mode 100644
index 00000000..e3793a4f
--- /dev/null
+++ b/test/monniaux/jpeg-6b/djpeg.c
@@ -0,0 +1,630 @@
+/*
+ * djpeg.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a command-line user interface for the JPEG decompressor.
+ * It should work on any system with Unix- or MS-DOS-style command lines.
+ *
+ * Two different command line styles are permitted, depending on the
+ * compile-time switch TWO_FILE_COMMANDLINE:
+ * djpeg [options] inputfile outputfile
+ * djpeg [options] [inputfile]
+ * In the second style, output is always to standard output, which you'd
+ * normally redirect to a file or pipe to some other program. Input is
+ * either from a named file or from standard input (typically redirected).
+ * The second style is convenient on Unix but is unhelpful on systems that
+ * don't support pipes. Also, you MUST use the first style if your system
+ * doesn't do binary I/O to stdin/stdout.
+ * To simplify script writing, the "-outfile" switch is provided. The syntax
+ * djpeg [options] -outfile outputfile inputfile
+ * works regardless of which command line style is used.
+ */
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+#include "jversion.h" /* for version message */
+
+#include <ctype.h> /* to declare isprint() */
+
+#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h> /* Metrowerks needs this */
+#include <console.h> /* ... and this */
+#endif
+#ifdef THINK_C
+#include <console.h> /* Think declares it here */
+#endif
+#endif
+
+
+/* Create the add-on message string table. */
+
+#define JMESSAGE(code,string) string ,
+
+static const char * const cdjpeg_message_table[] = {
+#include "cderror.h"
+ NULL
+};
+
+
+/*
+ * This list defines the known output image formats
+ * (not all of which need be supported by a given version).
+ * You can change the default output format by defining DEFAULT_FMT;
+ * indeed, you had better do so if you undefine PPM_SUPPORTED.
+ */
+
+typedef enum {
+ FMT_BMP, /* BMP format (Windows flavor) */
+ FMT_GIF, /* GIF format */
+ FMT_OS2, /* BMP format (OS/2 flavor) */
+ FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
+ FMT_RLE, /* RLE format */
+ FMT_TARGA, /* Targa format */
+ FMT_TIFF /* TIFF format */
+} IMAGE_FORMATS;
+
+#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
+#define DEFAULT_FMT FMT_PPM
+#endif
+
+static IMAGE_FORMATS requested_fmt;
+
+
+/*
+ * Argument-parsing code.
+ * The switch parser is designed to be useful with DOS-style command line
+ * syntax, ie, intermixed switches and file names, where only the switches
+ * to the left of a given file name affect processing of that file.
+ * The main program in this file doesn't actually use this capability...
+ */
+
+
+static const char * progname; /* program name for error messages */
+static char * outfilename; /* for -outfile switch */
+
+
+LOCAL(void)
+usage (void)
+/* complain about bad command line */
+{
+ fprintf(stderr, "usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+ fprintf(stderr, "inputfile outputfile\n");
+#else
+ fprintf(stderr, "[inputfile]\n");
+#endif
+
+ fprintf(stderr, "Switches (names may be abbreviated):\n");
+ fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
+ fprintf(stderr, " -fast Fast, low-quality processing\n");
+ fprintf(stderr, " -grayscale Force grayscale output\n");
+#ifdef IDCT_SCALING_SUPPORTED
+ fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
+#endif
+#ifdef BMP_SUPPORTED
+ fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
+ (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
+#endif
+#ifdef GIF_SUPPORTED
+ fprintf(stderr, " -gif Select GIF output format%s\n",
+ (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
+#endif
+#ifdef BMP_SUPPORTED
+ fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
+ (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
+#endif
+#ifdef PPM_SUPPORTED
+ fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
+ (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
+#endif
+#ifdef RLE_SUPPORTED
+ fprintf(stderr, " -rle Select Utah RLE output format%s\n",
+ (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
+#endif
+#ifdef TARGA_SUPPORTED
+ fprintf(stderr, " -targa Select Targa output format%s\n",
+ (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
+#endif
+ fprintf(stderr, "Switches for advanced users:\n");
+#ifdef DCT_ISLOW_SUPPORTED
+ fprintf(stderr, " -dct int Use integer DCT method%s\n",
+ (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
+ (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
+ (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
+#endif
+ fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
+ fprintf(stderr, " -dither none Don't use dithering in quantization\n");
+ fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
+#ifdef QUANT_2PASS_SUPPORTED
+ fprintf(stderr, " -map FILE Map to colors used in named image file\n");
+#endif
+ fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
+#ifdef QUANT_1PASS_SUPPORTED
+ fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
+#endif
+ fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
+ fprintf(stderr, " -outfile name Specify name for output file\n");
+ fprintf(stderr, " -verbose or -debug Emit debug output\n");
+ exit(EXIT_FAILURE);
+}
+
+
+LOCAL(int)
+parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
+ int last_file_arg_seen, boolean for_real)
+/* Parse optional switches.
+ * Returns argv[] index of first file-name argument (== argc if none).
+ * Any file names with indexes <= last_file_arg_seen are ignored;
+ * they have presumably been processed in a previous iteration.
+ * (Pass 0 for last_file_arg_seen on the first or only iteration.)
+ * for_real is FALSE on the first (dummy) pass; we may skip any expensive
+ * processing.
+ */
+{
+ int argn;
+ char * arg;
+
+ /* Set up default JPEG parameters. */
+ requested_fmt = DEFAULT_FMT; /* set default output file format */
+ outfilename = NULL;
+ cinfo->err->trace_level = 0;
+
+ /* Scan command line options, adjust parameters */
+
+ for (argn = 1; argn < argc; argn++) {
+ arg = argv[argn];
+ if (*arg != '-') {
+ /* Not a switch, must be a file name argument */
+ if (argn <= last_file_arg_seen) {
+ outfilename = NULL; /* -outfile applies to just one input file */
+ continue; /* ignore this name if previously processed */
+ }
+ break; /* else done parsing switches */
+ }
+ arg++; /* advance past switch marker character */
+
+ if (keymatch(arg, "bmp", 1)) {
+ /* BMP output format. */
+ requested_fmt = FMT_BMP;
+
+ } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
+ keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
+ /* Do color quantization. */
+ int val;
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%d", &val) != 1)
+ usage();
+ cinfo->desired_number_of_colors = val;
+ cinfo->quantize_colors = TRUE;
+
+ } else if (keymatch(arg, "dct", 2)) {
+ /* Select IDCT algorithm. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "int", 1)) {
+ cinfo->dct_method = JDCT_ISLOW;
+ } else if (keymatch(argv[argn], "fast", 2)) {
+ cinfo->dct_method = JDCT_IFAST;
+ } else if (keymatch(argv[argn], "float", 2)) {
+ cinfo->dct_method = JDCT_FLOAT;
+ } else
+ usage();
+
+ } else if (keymatch(arg, "dither", 2)) {
+ /* Select dithering algorithm. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "fs", 2)) {
+ cinfo->dither_mode = JDITHER_FS;
+ } else if (keymatch(argv[argn], "none", 2)) {
+ cinfo->dither_mode = JDITHER_NONE;
+ } else if (keymatch(argv[argn], "ordered", 2)) {
+ cinfo->dither_mode = JDITHER_ORDERED;
+ } else
+ usage();
+
+ } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
+ /* Enable debug printouts. */
+ /* On first -d, print version identification */
+ static boolean printed_version = FALSE;
+
+ if (! printed_version) {
+ fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
+ JVERSION, JCOPYRIGHT);
+ printed_version = TRUE;
+ }
+ cinfo->err->trace_level++;
+
+ } else if (keymatch(arg, "fast", 1)) {
+ /* Select recommended processing options for quick-and-dirty output. */
+ cinfo->two_pass_quantize = FALSE;
+ cinfo->dither_mode = JDITHER_ORDERED;
+ if (! cinfo->quantize_colors) /* don't override an earlier -colors */
+ cinfo->desired_number_of_colors = 216;
+ cinfo->dct_method = JDCT_FASTEST;
+ cinfo->do_fancy_upsampling = FALSE;
+
+ } else if (keymatch(arg, "gif", 1)) {
+ /* GIF output format. */
+ requested_fmt = FMT_GIF;
+
+ } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
+ /* Force monochrome output. */
+ cinfo->out_color_space = JCS_GRAYSCALE;
+
+ } else if (keymatch(arg, "map", 3)) {
+ /* Quantize to a color map taken from an input file. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (for_real) { /* too expensive to do twice! */
+#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
+ FILE * mapfile;
+
+ if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+ exit(EXIT_FAILURE);
+ }
+ read_color_map(cinfo, mapfile);
+ fclose(mapfile);
+ cinfo->quantize_colors = TRUE;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ }
+
+ } else if (keymatch(arg, "maxmemory", 3)) {
+ /* Maximum memory in Kb (or Mb with 'm'). */
+ long lval;
+ char ch = 'x';
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+ usage();
+ if (ch == 'm' || ch == 'M')
+ lval *= 1000L;
+ cinfo->mem->max_memory_to_use = lval * 1000L;
+
+ } else if (keymatch(arg, "nosmooth", 3)) {
+ /* Suppress fancy upsampling */
+ cinfo->do_fancy_upsampling = FALSE;
+
+ } else if (keymatch(arg, "onepass", 3)) {
+ /* Use fast one-pass quantization. */
+ cinfo->two_pass_quantize = FALSE;
+
+ } else if (keymatch(arg, "os2", 3)) {
+ /* BMP output format (OS/2 flavor). */
+ requested_fmt = FMT_OS2;
+
+ } else if (keymatch(arg, "outfile", 4)) {
+ /* Set output file name. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ outfilename = argv[argn]; /* save it away for later use */
+
+ } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
+ /* PPM/PGM output format. */
+ requested_fmt = FMT_PPM;
+
+ } else if (keymatch(arg, "rle", 1)) {
+ /* RLE output format. */
+ requested_fmt = FMT_RLE;
+
+ } else if (keymatch(arg, "scale", 1)) {
+ /* Scale the output image by a fraction M/N. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%d/%d",
+ &cinfo->scale_num, &cinfo->scale_denom) != 2)
+ usage();
+
+ } else if (keymatch(arg, "targa", 1)) {
+ /* Targa output format. */
+ requested_fmt = FMT_TARGA;
+
+ } else {
+ usage(); /* bogus switch */
+ }
+ }
+
+ return argn; /* return index of next arg (file name) */
+}
+
+
+/*
+ * Marker processor for COM and interesting APPn markers.
+ * This replaces the library's built-in processor, which just skips the marker.
+ * We want to print out the marker as text, to the extent possible.
+ * Note this code relies on a non-suspending data source.
+ */
+
+LOCAL(unsigned int)
+jpeg_getc (j_decompress_ptr cinfo)
+/* Read next byte */
+{
+ struct jpeg_source_mgr * datasrc = cinfo->src;
+
+ if (datasrc->bytes_in_buffer == 0) {
+ if (! (*datasrc->fill_input_buffer) (cinfo))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+ }
+ datasrc->bytes_in_buffer--;
+ return GETJOCTET(*datasrc->next_input_byte++);
+}
+
+
+METHODDEF(boolean)
+print_text_marker (j_decompress_ptr cinfo)
+{
+ boolean traceit = (cinfo->err->trace_level >= 1);
+ INT32 length;
+ unsigned int ch;
+ unsigned int lastch = 0;
+
+ length = jpeg_getc(cinfo) << 8;
+ length += jpeg_getc(cinfo);
+ length -= 2; /* discount the length word itself */
+
+ if (traceit) {
+ if (cinfo->unread_marker == JPEG_COM)
+ fprintf(stderr, "Comment, length %ld:\n", (long) length);
+ else /* assume it is an APPn otherwise */
+ fprintf(stderr, "APP%d, length %ld:\n",
+ cinfo->unread_marker - JPEG_APP0, (long) length);
+ }
+
+ while (--length >= 0) {
+ ch = jpeg_getc(cinfo);
+ if (traceit) {
+ /* Emit the character in a readable form.
+ * Nonprintables are converted to \nnn form,
+ * while \ is converted to \\.
+ * Newlines in CR, CR/LF, or LF form will be printed as one newline.
+ */
+ if (ch == '\r') {
+ fprintf(stderr, "\n");
+ } else if (ch == '\n') {
+ if (lastch != '\r')
+ fprintf(stderr, "\n");
+ } else if (ch == '\\') {
+ fprintf(stderr, "\\\\");
+ } else if (isprint(ch)) {
+ putc(ch, stderr);
+ } else {
+ fprintf(stderr, "\\%03o", ch);
+ }
+ lastch = ch;
+ }
+ }
+
+ if (traceit)
+ fprintf(stderr, "\n");
+
+ return TRUE;
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+#ifdef PROGRESS_REPORT
+ struct cdjpeg_progress_mgr progress;
+#endif
+ int file_index;
+ djpeg_dest_ptr dest_mgr = NULL;
+ FILE * input_file;
+ FILE * output_file;
+ JDIMENSION num_scanlines;
+
+ /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+ argc = ccommand(&argv);
+#endif
+
+ progname = argv[0];
+ if (progname == NULL || progname[0] == 0)
+ progname = "djpeg"; /* in case C library doesn't provide it */
+
+ /* Initialize the JPEG decompression object with default error handling. */
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_decompress(&cinfo);
+ /* Add some application-specific error messages (from cderror.h) */
+ jerr.addon_message_table = cdjpeg_message_table;
+ jerr.first_addon_message = JMSG_FIRSTADDONCODE;
+ jerr.last_addon_message = JMSG_LASTADDONCODE;
+
+ /* Insert custom marker processor for COM and APP12.
+ * APP12 is used by some digital camera makers for textual info,
+ * so we provide the ability to display it as text.
+ * If you like, additional APPn marker types can be selected for display,
+ * but don't try to override APP0 or APP14 this way (see libjpeg.doc).
+ */
+ jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
+ jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
+
+ /* Now safe to enable signal catcher. */
+#ifdef NEED_SIGNAL_CATCHER
+ enable_signal_catcher((j_common_ptr) &cinfo);
+#endif
+
+ /* Scan command line to find file names. */
+ /* It is convenient to use just one switch-parsing routine, but the switch
+ * values read here are ignored; we will rescan the switches after opening
+ * the input file.
+ * (Exception: tracing level set here controls verbosity for COM markers
+ * found during jpeg_read_header...)
+ */
+
+ file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
+
+#ifdef TWO_FILE_COMMANDLINE
+ /* Must have either -outfile switch or explicit output file name */
+ if (outfilename == NULL) {
+ if (file_index != argc-2) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ outfilename = argv[file_index+1];
+ } else {
+ if (file_index != argc-1) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ }
+#else
+ /* Unix style: expect zero or one file name */
+ if (file_index < argc-1) {
+ fprintf(stderr, "%s: only one input file\n", progname);
+ usage();
+ }
+#endif /* TWO_FILE_COMMANDLINE */
+
+ /* Open the input file. */
+ if (file_index < argc) {
+ if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default input file is stdin */
+ input_file = read_stdin();
+ }
+
+ /* Open the output file. */
+ if (outfilename != NULL) {
+ if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default output file is stdout */
+ output_file = write_stdout();
+ }
+
+#ifdef PROGRESS_REPORT
+ start_progress_monitor((j_common_ptr) &cinfo, &progress);
+#endif
+
+ /* Specify data source for decompression */
+ jpeg_stdio_src(&cinfo, input_file);
+
+ /* Read file header, set default decompression parameters */
+ (void) jpeg_read_header(&cinfo, TRUE);
+
+ /* Adjust default decompression parameters by re-parsing the options */
+ file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
+
+ /* Initialize the output module now to let it override any crucial
+ * option settings (for instance, GIF wants to force color quantization).
+ */
+ switch (requested_fmt) {
+#ifdef BMP_SUPPORTED
+ case FMT_BMP:
+ dest_mgr = jinit_write_bmp(&cinfo, FALSE);
+ break;
+ case FMT_OS2:
+ dest_mgr = jinit_write_bmp(&cinfo, TRUE);
+ break;
+#endif
+#ifdef GIF_SUPPORTED
+ case FMT_GIF:
+ dest_mgr = jinit_write_gif(&cinfo);
+ break;
+#endif
+#ifdef PPM_SUPPORTED
+ case FMT_PPM:
+ dest_mgr = jinit_write_ppm(&cinfo);
+ break;
+#endif
+#ifdef RLE_SUPPORTED
+ case FMT_RLE:
+ dest_mgr = jinit_write_rle(&cinfo);
+ break;
+#endif
+#ifdef TARGA_SUPPORTED
+ case FMT_TARGA:
+ dest_mgr = jinit_write_targa(&cinfo);
+ break;
+#endif
+ default:
+ ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
+ break;
+ }
+ dest_mgr->output_file = output_file;
+
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+
+ /* Start decompressor */
+ (void) jpeg_start_decompress(&cinfo);
+
+ /* Write output file header */
+ (*dest_mgr->start_output) (&cinfo, dest_mgr);
+
+ /* Process data */
+ while (cinfo.output_scanline < cinfo.output_height) {
+ num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
+ dest_mgr->buffer_height);
+ (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
+ }
+#ifdef VERIMAG
+ clock_stop();
+ printerr_total_clock();
+#endif
+
+#ifdef PROGRESS_REPORT
+ /* Hack: count final pass as done in case finish_output does an extra pass.
+ * The library won't have updated completed_passes.
+ */
+ progress.pub.completed_passes = progress.pub.total_passes;
+#endif
+
+ /* Finish decompression and release memory.
+ * I must do it in this order because output module has allocated memory
+ * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
+ */
+ (*dest_mgr->finish_output) (&cinfo, dest_mgr);
+ (void) jpeg_finish_decompress(&cinfo);
+ jpeg_destroy_decompress(&cinfo);
+
+ /* Close files, if we opened them */
+ if (input_file != stdin)
+ fclose(input_file);
+ if (output_file != stdout)
+ fclose(output_file);
+
+#ifdef PROGRESS_REPORT
+ end_progress_monitor((j_common_ptr) &cinfo);
+#endif
+
+ /* All done. */
+ exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
+ return 0; /* suppress no-return-value warnings */
+}
diff --git a/test/monniaux/jpeg-6b/example.c b/test/monniaux/jpeg-6b/example.c
new file mode 100644
index 00000000..7fc354f0
--- /dev/null
+++ b/test/monniaux/jpeg-6b/example.c
@@ -0,0 +1,433 @@
+/*
+ * example.c
+ *
+ * This file illustrates how to use the IJG code as a subroutine library
+ * to read or write JPEG image files. You should look at this code in
+ * conjunction with the documentation file libjpeg.doc.
+ *
+ * This code will not do anything useful as-is, but it may be helpful as a
+ * skeleton for constructing routines that call the JPEG library.
+ *
+ * We present these routines in the same coding style used in the JPEG code
+ * (ANSI function definitions, etc); but you are of course free to code your
+ * routines in a different style if you prefer.
+ */
+
+#include <stdio.h>
+
+/*
+ * Include file for users of JPEG library.
+ * You will need to have included system headers that define at least
+ * the typedefs FILE and size_t before you can include jpeglib.h.
+ * (stdio.h is sufficient on ANSI-conforming systems.)
+ * You may also wish to include "jerror.h".
+ */
+
+#include "jpeglib.h"
+
+/*
+ * <setjmp.h> is used for the optional error recovery mechanism shown in
+ * the second part of the example.
+ */
+
+#include <setjmp.h>
+
+
+
+/******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
+
+/* This half of the example shows how to feed data into the JPEG compressor.
+ * We present a minimal version that does not worry about refinements such
+ * as error recovery (the JPEG code will just exit() if it gets an error).
+ */
+
+
+/*
+ * IMAGE DATA FORMATS:
+ *
+ * The standard input image format is a rectangular array of pixels, with
+ * each pixel having the same number of "component" values (color channels).
+ * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
+ * If you are working with color data, then the color values for each pixel
+ * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
+ * RGB color.
+ *
+ * For this example, we'll assume that this data structure matches the way
+ * our application has stored the image in memory, so we can just pass a
+ * pointer to our image buffer. In particular, let's say that the image is
+ * RGB color and is described by:
+ */
+
+extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
+extern int image_height; /* Number of rows in image */
+extern int image_width; /* Number of columns in image */
+
+
+/*
+ * Sample routine for JPEG compression. We assume that the target file name
+ * and a compression quality factor are passed in.
+ */
+
+GLOBAL(void)
+write_JPEG_file (char * filename, int quality)
+{
+ /* This struct contains the JPEG compression parameters and pointers to
+ * working space (which is allocated as needed by the JPEG library).
+ * It is possible to have several such structures, representing multiple
+ * compression/decompression processes, in existence at once. We refer
+ * to any one struct (and its associated working data) as a "JPEG object".
+ */
+ struct jpeg_compress_struct cinfo;
+ /* This struct represents a JPEG error handler. It is declared separately
+ * because applications often want to supply a specialized error handler
+ * (see the second half of this file for an example). But here we just
+ * take the easy way out and use the standard error handler, which will
+ * print a message on stderr and call exit() if compression fails.
+ * Note that this struct must live as long as the main JPEG parameter
+ * struct, to avoid dangling-pointer problems.
+ */
+ struct jpeg_error_mgr jerr;
+ /* More stuff */
+ FILE * outfile; /* target file */
+ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
+ int row_stride; /* physical row width in image buffer */
+
+ /* Step 1: allocate and initialize JPEG compression object */
+
+ /* We have to set up the error handler first, in case the initialization
+ * step fails. (Unlikely, but it could happen if you are out of memory.)
+ * This routine fills in the contents of struct jerr, and returns jerr's
+ * address which we place into the link field in cinfo.
+ */
+ cinfo.err = jpeg_std_error(&jerr);
+ /* Now we can initialize the JPEG compression object. */
+ jpeg_create_compress(&cinfo);
+
+ /* Step 2: specify data destination (eg, a file) */
+ /* Note: steps 2 and 3 can be done in either order. */
+
+ /* Here we use the library-supplied code to send compressed data to a
+ * stdio stream. You can also write your own code to do something else.
+ * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
+ * requires it in order to write binary files.
+ */
+ if ((outfile = fopen(filename, "wb")) == NULL) {
+ fprintf(stderr, "can't open %s\n", filename);
+ exit(1);
+ }
+ jpeg_stdio_dest(&cinfo, outfile);
+
+ /* Step 3: set parameters for compression */
+
+ /* First we supply a description of the input image.
+ * Four fields of the cinfo struct must be filled in:
+ */
+ cinfo.image_width = image_width; /* image width and height, in pixels */
+ cinfo.image_height = image_height;
+ cinfo.input_components = 3; /* # of color components per pixel */
+ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
+ /* Now use the library's routine to set default compression parameters.
+ * (You must set at least cinfo.in_color_space before calling this,
+ * since the defaults depend on the source color space.)
+ */
+ jpeg_set_defaults(&cinfo);
+ /* Now you can set any non-default parameters you wish to.
+ * Here we just illustrate the use of quality (quantization table) scaling:
+ */
+ jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
+
+ /* Step 4: Start compressor */
+
+ /* TRUE ensures that we will write a complete interchange-JPEG file.
+ * Pass TRUE unless you are very sure of what you're doing.
+ */
+ jpeg_start_compress(&cinfo, TRUE);
+
+ /* Step 5: while (scan lines remain to be written) */
+ /* jpeg_write_scanlines(...); */
+
+ /* Here we use the library's state variable cinfo.next_scanline as the
+ * loop counter, so that we don't have to keep track ourselves.
+ * To keep things simple, we pass one scanline per call; you can pass
+ * more if you wish, though.
+ */
+ row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
+
+ while (cinfo.next_scanline < cinfo.image_height) {
+ /* jpeg_write_scanlines expects an array of pointers to scanlines.
+ * Here the array is only one element long, but you could pass
+ * more than one scanline at a time if that's more convenient.
+ */
+ row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
+ (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
+ }
+
+ /* Step 6: Finish compression */
+
+ jpeg_finish_compress(&cinfo);
+ /* After finish_compress, we can close the output file. */
+ fclose(outfile);
+
+ /* Step 7: release JPEG compression object */
+
+ /* This is an important step since it will release a good deal of memory. */
+ jpeg_destroy_compress(&cinfo);
+
+ /* And we're done! */
+}
+
+
+/*
+ * SOME FINE POINTS:
+ *
+ * In the above loop, we ignored the return value of jpeg_write_scanlines,
+ * which is the number of scanlines actually written. We could get away
+ * with this because we were only relying on the value of cinfo.next_scanline,
+ * which will be incremented correctly. If you maintain additional loop
+ * variables then you should be careful to increment them properly.
+ * Actually, for output to a stdio stream you needn't worry, because
+ * then jpeg_write_scanlines will write all the lines passed (or else exit
+ * with a fatal error). Partial writes can only occur if you use a data
+ * destination module that can demand suspension of the compressor.
+ * (If you don't know what that's for, you don't need it.)
+ *
+ * If the compressor requires full-image buffers (for entropy-coding
+ * optimization or a multi-scan JPEG file), it will create temporary
+ * files for anything that doesn't fit within the maximum-memory setting.
+ * (Note that temp files are NOT needed if you use the default parameters.)
+ * On some systems you may need to set up a signal handler to ensure that
+ * temporary files are deleted if the program is interrupted. See libjpeg.doc.
+ *
+ * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
+ * files to be compatible with everyone else's. If you cannot readily read
+ * your data in that order, you'll need an intermediate array to hold the
+ * image. See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
+ * source data using the JPEG code's internal virtual-array mechanisms.
+ */
+
+
+
+/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
+
+/* This half of the example shows how to read data from the JPEG decompressor.
+ * It's a bit more refined than the above, in that we show:
+ * (a) how to modify the JPEG library's standard error-reporting behavior;
+ * (b) how to allocate workspace using the library's memory manager.
+ *
+ * Just to make this example a little different from the first one, we'll
+ * assume that we do not intend to put the whole image into an in-memory
+ * buffer, but to send it line-by-line someplace else. We need a one-
+ * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
+ * memory manager allocate it for us. This approach is actually quite useful
+ * because we don't need to remember to deallocate the buffer separately: it
+ * will go away automatically when the JPEG object is cleaned up.
+ */
+
+
+/*
+ * ERROR HANDLING:
+ *
+ * The JPEG library's standard error handler (jerror.c) is divided into
+ * several "methods" which you can override individually. This lets you
+ * adjust the behavior without duplicating a lot of code, which you might
+ * have to update with each future release.
+ *
+ * Our example here shows how to override the "error_exit" method so that
+ * control is returned to the library's caller when a fatal error occurs,
+ * rather than calling exit() as the standard error_exit method does.
+ *
+ * We use C's setjmp/longjmp facility to return control. This means that the
+ * routine which calls the JPEG library must first execute a setjmp() call to
+ * establish the return point. We want the replacement error_exit to do a
+ * longjmp(). But we need to make the setjmp buffer accessible to the
+ * error_exit routine. To do this, we make a private extension of the
+ * standard JPEG error handler object. (If we were using C++, we'd say we
+ * were making a subclass of the regular error handler.)
+ *
+ * Here's the extended error handler struct:
+ */
+
+struct my_error_mgr {
+ struct jpeg_error_mgr pub; /* "public" fields */
+
+ jmp_buf setjmp_buffer; /* for return to caller */
+};
+
+typedef struct my_error_mgr * my_error_ptr;
+
+/*
+ * Here's the routine that will replace the standard error_exit method:
+ */
+
+METHODDEF(void)
+my_error_exit (j_common_ptr cinfo)
+{
+ /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
+ my_error_ptr myerr = (my_error_ptr) cinfo->err;
+
+ /* Always display the message. */
+ /* We could postpone this until after returning, if we chose. */
+ (*cinfo->err->output_message) (cinfo);
+
+ /* Return control to the setjmp point */
+ longjmp(myerr->setjmp_buffer, 1);
+}
+
+
+/*
+ * Sample routine for JPEG decompression. We assume that the source file name
+ * is passed in. We want to return 1 on success, 0 on error.
+ */
+
+
+GLOBAL(int)
+read_JPEG_file (char * filename)
+{
+ /* This struct contains the JPEG decompression parameters and pointers to
+ * working space (which is allocated as needed by the JPEG library).
+ */
+ struct jpeg_decompress_struct cinfo;
+ /* We use our private extension JPEG error handler.
+ * Note that this struct must live as long as the main JPEG parameter
+ * struct, to avoid dangling-pointer problems.
+ */
+ struct my_error_mgr jerr;
+ /* More stuff */
+ FILE * infile; /* source file */
+ JSAMPARRAY buffer; /* Output row buffer */
+ int row_stride; /* physical row width in output buffer */
+
+ /* In this example we want to open the input file before doing anything else,
+ * so that the setjmp() error recovery below can assume the file is open.
+ * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
+ * requires it in order to read binary files.
+ */
+
+ if ((infile = fopen(filename, "rb")) == NULL) {
+ fprintf(stderr, "can't open %s\n", filename);
+ return 0;
+ }
+
+ /* Step 1: allocate and initialize JPEG decompression object */
+
+ /* We set up the normal JPEG error routines, then override error_exit. */
+ cinfo.err = jpeg_std_error(&jerr.pub);
+ jerr.pub.error_exit = my_error_exit;
+ /* Establish the setjmp return context for my_error_exit to use. */
+ if (setjmp(jerr.setjmp_buffer)) {
+ /* If we get here, the JPEG code has signaled an error.
+ * We need to clean up the JPEG object, close the input file, and return.
+ */
+ jpeg_destroy_decompress(&cinfo);
+ fclose(infile);
+ return 0;
+ }
+ /* Now we can initialize the JPEG decompression object. */
+ jpeg_create_decompress(&cinfo);
+
+ /* Step 2: specify data source (eg, a file) */
+
+ jpeg_stdio_src(&cinfo, infile);
+
+ /* Step 3: read file parameters with jpeg_read_header() */
+
+ (void) jpeg_read_header(&cinfo, TRUE);
+ /* We can ignore the return value from jpeg_read_header since
+ * (a) suspension is not possible with the stdio data source, and
+ * (b) we passed TRUE to reject a tables-only JPEG file as an error.
+ * See libjpeg.doc for more info.
+ */
+
+ /* Step 4: set parameters for decompression */
+
+ /* In this example, we don't need to change any of the defaults set by
+ * jpeg_read_header(), so we do nothing here.
+ */
+
+ /* Step 5: Start decompressor */
+
+ (void) jpeg_start_decompress(&cinfo);
+ /* We can ignore the return value since suspension is not possible
+ * with the stdio data source.
+ */
+
+ /* We may need to do some setup of our own at this point before reading
+ * the data. After jpeg_start_decompress() we have the correct scaled
+ * output image dimensions available, as well as the output colormap
+ * if we asked for color quantization.
+ * In this example, we need to make an output work buffer of the right size.
+ */
+ /* JSAMPLEs per row in output buffer */
+ row_stride = cinfo.output_width * cinfo.output_components;
+ /* Make a one-row-high sample array that will go away when done with image */
+ buffer = (*cinfo.mem->alloc_sarray)
+ ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
+
+ /* Step 6: while (scan lines remain to be read) */
+ /* jpeg_read_scanlines(...); */
+
+ /* Here we use the library's state variable cinfo.output_scanline as the
+ * loop counter, so that we don't have to keep track ourselves.
+ */
+ while (cinfo.output_scanline < cinfo.output_height) {
+ /* jpeg_read_scanlines expects an array of pointers to scanlines.
+ * Here the array is only one element long, but you could ask for
+ * more than one scanline at a time if that's more convenient.
+ */
+ (void) jpeg_read_scanlines(&cinfo, buffer, 1);
+ /* Assume put_scanline_someplace wants a pointer and sample count. */
+ put_scanline_someplace(buffer[0], row_stride);
+ }
+
+ /* Step 7: Finish decompression */
+
+ (void) jpeg_finish_decompress(&cinfo);
+ /* We can ignore the return value since suspension is not possible
+ * with the stdio data source.
+ */
+
+ /* Step 8: Release JPEG decompression object */
+
+ /* This is an important step since it will release a good deal of memory. */
+ jpeg_destroy_decompress(&cinfo);
+
+ /* After finish_decompress, we can close the input file.
+ * Here we postpone it until after no more JPEG errors are possible,
+ * so as to simplify the setjmp error logic above. (Actually, I don't
+ * think that jpeg_destroy can do an error exit, but why assume anything...)
+ */
+ fclose(infile);
+
+ /* At this point you may want to check to see whether any corrupt-data
+ * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
+ */
+
+ /* And we're done! */
+ return 1;
+}
+
+
+/*
+ * SOME FINE POINTS:
+ *
+ * In the above code, we ignored the return value of jpeg_read_scanlines,
+ * which is the number of scanlines actually read. We could get away with
+ * this because we asked for only one line at a time and we weren't using
+ * a suspending data source. See libjpeg.doc for more info.
+ *
+ * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
+ * we should have done it beforehand to ensure that the space would be
+ * counted against the JPEG max_memory setting. In some systems the above
+ * code would risk an out-of-memory error. However, in general we don't
+ * know the output image dimensions before jpeg_start_decompress(), unless we
+ * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
+ *
+ * Scanlines are returned in the same order as they appear in the JPEG file,
+ * which is standardly top-to-bottom. If you must emit data bottom-to-top,
+ * you can use one of the virtual arrays provided by the JPEG memory manager
+ * to invert the data. See wrbmp.c for an example.
+ *
+ * As with compression, some operating modes may require temporary files.
+ * On some systems you may need to set up a signal handler to ensure that
+ * temporary files are deleted if the program is interrupted. See libjpeg.doc.
+ */
diff --git a/test/monniaux/jpeg-6b/filelist.doc b/test/monniaux/jpeg-6b/filelist.doc
new file mode 100644
index 00000000..e14982ca
--- /dev/null
+++ b/test/monniaux/jpeg-6b/filelist.doc
@@ -0,0 +1,210 @@
+IJG JPEG LIBRARY: FILE LIST
+
+Copyright (C) 1994-1998, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+Here is a road map to the files in the IJG JPEG distribution. The
+distribution includes the JPEG library proper, plus two application
+programs ("cjpeg" and "djpeg") which use the library to convert JPEG
+files to and from some other popular image formats. A third application
+"jpegtran" uses the library to do lossless conversion between different
+variants of JPEG. There are also two stand-alone applications,
+"rdjpgcom" and "wrjpgcom".
+
+
+THE JPEG LIBRARY
+================
+
+Include files:
+
+jpeglib.h JPEG library's exported data and function declarations.
+jconfig.h Configuration declarations. Note: this file is not present
+ in the distribution; it is generated during installation.
+jmorecfg.h Additional configuration declarations; need not be changed
+ for a standard installation.
+jerror.h Declares JPEG library's error and trace message codes.
+jinclude.h Central include file used by all IJG .c files to reference
+ system include files.
+jpegint.h JPEG library's internal data structures.
+jchuff.h Private declarations for Huffman encoder modules.
+jdhuff.h Private declarations for Huffman decoder modules.
+jdct.h Private declarations for forward & reverse DCT subsystems.
+jmemsys.h Private declarations for memory management subsystem.
+jversion.h Version information.
+
+Applications using the library should include jpeglib.h (which in turn
+includes jconfig.h and jmorecfg.h). Optionally, jerror.h may be included
+if the application needs to reference individual JPEG error codes. The
+other include files are intended for internal use and would not normally
+be included by an application program. (cjpeg/djpeg/etc do use jinclude.h,
+since its function is to improve portability of the whole IJG distribution.
+Most other applications will directly include the system include files they
+want, and hence won't need jinclude.h.)
+
+
+C source code files:
+
+These files contain most of the functions intended to be called directly by
+an application program:
+
+jcapimin.c Application program interface: core routines for compression.
+jcapistd.c Application program interface: standard compression.
+jdapimin.c Application program interface: core routines for decompression.
+jdapistd.c Application program interface: standard decompression.
+jcomapi.c Application program interface routines common to compression
+ and decompression.
+jcparam.c Compression parameter setting helper routines.
+jctrans.c API and library routines for transcoding compression.
+jdtrans.c API and library routines for transcoding decompression.
+
+Compression side of the library:
+
+jcinit.c Initialization: determines which other modules to use.
+jcmaster.c Master control: setup and inter-pass sequencing logic.
+jcmainct.c Main buffer controller (preprocessor => JPEG compressor).
+jcprepct.c Preprocessor buffer controller.
+jccoefct.c Buffer controller for DCT coefficient buffer.
+jccolor.c Color space conversion.
+jcsample.c Downsampling.
+jcdctmgr.c DCT manager (DCT implementation selection & control).
+jfdctint.c Forward DCT using slow-but-accurate integer method.
+jfdctfst.c Forward DCT using faster, less accurate integer method.
+jfdctflt.c Forward DCT using floating-point arithmetic.
+jchuff.c Huffman entropy coding for sequential JPEG.
+jcphuff.c Huffman entropy coding for progressive JPEG.
+jcmarker.c JPEG marker writing.
+jdatadst.c Data destination manager for stdio output.
+
+Decompression side of the library:
+
+jdmaster.c Master control: determines which other modules to use.
+jdinput.c Input controller: controls input processing modules.
+jdmainct.c Main buffer controller (JPEG decompressor => postprocessor).
+jdcoefct.c Buffer controller for DCT coefficient buffer.
+jdpostct.c Postprocessor buffer controller.
+jdmarker.c JPEG marker reading.
+jdhuff.c Huffman entropy decoding for sequential JPEG.
+jdphuff.c Huffman entropy decoding for progressive JPEG.
+jddctmgr.c IDCT manager (IDCT implementation selection & control).
+jidctint.c Inverse DCT using slow-but-accurate integer method.
+jidctfst.c Inverse DCT using faster, less accurate integer method.
+jidctflt.c Inverse DCT using floating-point arithmetic.
+jidctred.c Inverse DCTs with reduced-size outputs.
+jdsample.c Upsampling.
+jdcolor.c Color space conversion.
+jdmerge.c Merged upsampling/color conversion (faster, lower quality).
+jquant1.c One-pass color quantization using a fixed-spacing colormap.
+jquant2.c Two-pass color quantization using a custom-generated colormap.
+ Also handles one-pass quantization to an externally given map.
+jdatasrc.c Data source manager for stdio input.
+
+Support files for both compression and decompression:
+
+jerror.c Standard error handling routines (application replaceable).
+jmemmgr.c System-independent (more or less) memory management code.
+jutils.c Miscellaneous utility routines.
+
+jmemmgr.c relies on a system-dependent memory management module. The IJG
+distribution includes the following implementations of the system-dependent
+module:
+
+jmemnobs.c "No backing store": assumes adequate virtual memory exists.
+jmemansi.c Makes temporary files with ANSI-standard routine tmpfile().
+jmemname.c Makes temporary files with program-generated file names.
+jmemdos.c Custom implementation for MS-DOS (16-bit environment only):
+ can use extended and expanded memory as well as temp files.
+jmemmac.c Custom implementation for Apple Macintosh.
+
+Exactly one of the system-dependent modules should be configured into an
+installed JPEG library (see install.doc for hints about which one to use).
+On unusual systems you may find it worthwhile to make a special
+system-dependent memory manager.
+
+
+Non-C source code files:
+
+jmemdosa.asm 80x86 assembly code support for jmemdos.c; used only in
+ MS-DOS-specific configurations of the JPEG library.
+
+
+CJPEG/DJPEG/JPEGTRAN
+====================
+
+Include files:
+
+cdjpeg.h Declarations shared by cjpeg/djpeg/jpegtran modules.
+cderror.h Additional error and trace message codes for cjpeg et al.
+transupp.h Declarations for jpegtran support routines in transupp.c.
+
+C source code files:
+
+cjpeg.c Main program for cjpeg.
+djpeg.c Main program for djpeg.
+jpegtran.c Main program for jpegtran.
+cdjpeg.c Utility routines used by all three programs.
+rdcolmap.c Code to read a colormap file for djpeg's "-map" switch.
+rdswitch.c Code to process some of cjpeg's more complex switches.
+ Also used by jpegtran.
+transupp.c Support code for jpegtran: lossless image manipulations.
+
+Image file reader modules for cjpeg:
+
+rdbmp.c BMP file input.
+rdgif.c GIF file input (now just a stub).
+rdppm.c PPM/PGM file input.
+rdrle.c Utah RLE file input.
+rdtarga.c Targa file input.
+
+Image file writer modules for djpeg:
+
+wrbmp.c BMP file output.
+wrgif.c GIF file output (a mere shadow of its former self).
+wrppm.c PPM/PGM file output.
+wrrle.c Utah RLE file output.
+wrtarga.c Targa file output.
+
+
+RDJPGCOM/WRJPGCOM
+=================
+
+C source code files:
+
+rdjpgcom.c Stand-alone rdjpgcom application.
+wrjpgcom.c Stand-alone wrjpgcom application.
+
+These programs do not depend on the IJG library. They do use
+jconfig.h and jinclude.h, only to improve portability.
+
+
+ADDITIONAL FILES
+================
+
+Documentation (see README for a guide to the documentation files):
+
+README Master documentation file.
+*.doc Other documentation files.
+*.1 Documentation in Unix man page format.
+change.log Version-to-version change highlights.
+example.c Sample code for calling JPEG library.
+
+Configuration/installation files and programs (see install.doc for more info):
+
+configure Unix shell script to perform automatic configuration.
+ltconfig Support scripts for configure (from GNU libtool).
+ltmain.sh
+config.guess
+config.sub
+install-sh Install shell script for those Unix systems lacking one.
+ckconfig.c Program to generate jconfig.h on non-Unix systems.
+jconfig.doc Template for making jconfig.h by hand.
+makefile.* Sample makefiles for particular systems.
+jconfig.* Sample jconfig.h for particular systems.
+ansi2knr.c De-ANSIfier for pre-ANSI C compilers (courtesy of
+ L. Peter Deutsch and Aladdin Enterprises).
+
+Test files (see install.doc for test procedure):
+
+test*.* Source and comparison files for confidence test.
+ These are binary image files, NOT text files.
diff --git a/test/monniaux/jpeg-6b/install-sh b/test/monniaux/jpeg-6b/install-sh
new file mode 100755
index 00000000..e8436696
--- /dev/null
+++ b/test/monniaux/jpeg-6b/install-sh
@@ -0,0 +1,250 @@
+#!/bin/sh
+#
+# install - install a program, script, or datafile
+# This comes from X11R5 (mit/util/scripts/install.sh).
+#
+# Copyright 1991 by the Massachusetts Institute of Technology
+#
+# Permission to use, copy, modify, distribute, and sell this software and its
+# documentation for any purpose is hereby granted without fee, provided that
+# the above copyright notice appear in all copies and that both that
+# copyright notice and this permission notice appear in supporting
+# documentation, and that the name of M.I.T. not be used in advertising or
+# publicity pertaining to distribution of the software without specific,
+# written prior permission. M.I.T. makes no representations about the
+# suitability of this software for any purpose. It is provided "as is"
+# without express or implied warranty.
+#
+# Calling this script install-sh is preferred over install.sh, to prevent
+# `make' implicit rules from creating a file called install from it
+# when there is no Makefile.
+#
+# This script is compatible with the BSD install script, but was written
+# from scratch. It can only install one file at a time, a restriction
+# shared with many OS's install programs.
+
+
+# set DOITPROG to echo to test this script
+
+# Don't use :- since 4.3BSD and earlier shells don't like it.
+doit="${DOITPROG-}"
+
+
+# put in absolute paths if you don't have them in your path; or use env. vars.
+
+mvprog="${MVPROG-mv}"
+cpprog="${CPPROG-cp}"
+chmodprog="${CHMODPROG-chmod}"
+chownprog="${CHOWNPROG-chown}"
+chgrpprog="${CHGRPPROG-chgrp}"
+stripprog="${STRIPPROG-strip}"
+rmprog="${RMPROG-rm}"
+mkdirprog="${MKDIRPROG-mkdir}"
+
+transformbasename=""
+transform_arg=""
+instcmd="$mvprog"
+chmodcmd="$chmodprog 0755"
+chowncmd=""
+chgrpcmd=""
+stripcmd=""
+rmcmd="$rmprog -f"
+mvcmd="$mvprog"
+src=""
+dst=""
+dir_arg=""
+
+while [ x"$1" != x ]; do
+ case $1 in
+ -c) instcmd="$cpprog"
+ shift
+ continue;;
+
+ -d) dir_arg=true
+ shift
+ continue;;
+
+ -m) chmodcmd="$chmodprog $2"
+ shift
+ shift
+ continue;;
+
+ -o) chowncmd="$chownprog $2"
+ shift
+ shift
+ continue;;
+
+ -g) chgrpcmd="$chgrpprog $2"
+ shift
+ shift
+ continue;;
+
+ -s) stripcmd="$stripprog"
+ shift
+ continue;;
+
+ -t=*) transformarg=`echo $1 | sed 's/-t=//'`
+ shift
+ continue;;
+
+ -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
+ shift
+ continue;;
+
+ *) if [ x"$src" = x ]
+ then
+ src=$1
+ else
+ # this colon is to work around a 386BSD /bin/sh bug
+ :
+ dst=$1
+ fi
+ shift
+ continue;;
+ esac
+done
+
+if [ x"$src" = x ]
+then
+ echo "install: no input file specified"
+ exit 1
+else
+ true
+fi
+
+if [ x"$dir_arg" != x ]; then
+ dst=$src
+ src=""
+
+ if [ -d $dst ]; then
+ instcmd=:
+ else
+ instcmd=mkdir
+ fi
+else
+
+# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
+# might cause directories to be created, which would be especially bad
+# if $src (and thus $dsttmp) contains '*'.
+
+ if [ -f $src -o -d $src ]
+ then
+ true
+ else
+ echo "install: $src does not exist"
+ exit 1
+ fi
+
+ if [ x"$dst" = x ]
+ then
+ echo "install: no destination specified"
+ exit 1
+ else
+ true
+ fi
+
+# If destination is a directory, append the input filename; if your system
+# does not like double slashes in filenames, you may need to add some logic
+
+ if [ -d $dst ]
+ then
+ dst="$dst"/`basename $src`
+ else
+ true
+ fi
+fi
+
+## this sed command emulates the dirname command
+dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
+
+# Make sure that the destination directory exists.
+# this part is taken from Noah Friedman's mkinstalldirs script
+
+# Skip lots of stat calls in the usual case.
+if [ ! -d "$dstdir" ]; then
+defaultIFS='
+'
+IFS="${IFS-${defaultIFS}}"
+
+oIFS="${IFS}"
+# Some sh's can't handle IFS=/ for some reason.
+IFS='%'
+set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
+IFS="${oIFS}"
+
+pathcomp=''
+
+while [ $# -ne 0 ] ; do
+ pathcomp="${pathcomp}${1}"
+ shift
+
+ if [ ! -d "${pathcomp}" ] ;
+ then
+ $mkdirprog "${pathcomp}"
+ else
+ true
+ fi
+
+ pathcomp="${pathcomp}/"
+done
+fi
+
+if [ x"$dir_arg" != x ]
+then
+ $doit $instcmd $dst &&
+
+ if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
+ if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
+ if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
+ if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
+else
+
+# If we're going to rename the final executable, determine the name now.
+
+ if [ x"$transformarg" = x ]
+ then
+ dstfile=`basename $dst`
+ else
+ dstfile=`basename $dst $transformbasename |
+ sed $transformarg`$transformbasename
+ fi
+
+# don't allow the sed command to completely eliminate the filename
+
+ if [ x"$dstfile" = x ]
+ then
+ dstfile=`basename $dst`
+ else
+ true
+ fi
+
+# Make a temp file name in the proper directory.
+
+ dsttmp=$dstdir/#inst.$$#
+
+# Move or copy the file name to the temp name
+
+ $doit $instcmd $src $dsttmp &&
+
+ trap "rm -f ${dsttmp}" 0 &&
+
+# and set any options; do chmod last to preserve setuid bits
+
+# If any of these fail, we abort the whole thing. If we want to
+# ignore errors from any of these, just make sure not to ignore
+# errors from the above "$doit $instcmd $src $dsttmp" command.
+
+ if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
+ if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
+ if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
+ if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
+
+# Now rename the file to the real destination.
+
+ $doit $rmcmd -f $dstdir/$dstfile &&
+ $doit $mvcmd $dsttmp $dstdir/$dstfile
+
+fi &&
+
+
+exit 0
diff --git a/test/monniaux/jpeg-6b/install.doc b/test/monniaux/jpeg-6b/install.doc
new file mode 100644
index 00000000..3702b986
--- /dev/null
+++ b/test/monniaux/jpeg-6b/install.doc
@@ -0,0 +1,1063 @@
+INSTALLATION INSTRUCTIONS for the Independent JPEG Group's JPEG software
+
+Copyright (C) 1991-1998, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file explains how to configure and install the IJG software. We have
+tried to make this software extremely portable and flexible, so that it can be
+adapted to almost any environment. The downside of this decision is that the
+installation process is complicated. We have provided shortcuts to simplify
+the task on common systems. But in any case, you will need at least a little
+familiarity with C programming and program build procedures for your system.
+
+If you are only using this software as part of a larger program, the larger
+program's installation procedure may take care of configuring the IJG code.
+For example, Ghostscript's installation script will configure the IJG code.
+You don't need to read this file if you just want to compile Ghostscript.
+
+If you are on a Unix machine, you may not need to read this file at all.
+Try doing
+ ./configure
+ make
+ make test
+If that doesn't complain, do
+ make install
+(better do "make -n install" first to see if the makefile will put the files
+where you want them). Read further if you run into snags or want to customize
+the code for your system.
+
+
+TABLE OF CONTENTS
+-----------------
+
+Before you start
+Configuring the software:
+ using the automatic "configure" script
+ using one of the supplied jconfig and makefile files
+ by hand
+Building the software
+Testing the software
+Installing the software
+Optional stuff
+Optimization
+Hints for specific systems
+
+
+BEFORE YOU START
+================
+
+Before installing the software you must unpack the distributed source code.
+Since you are reading this file, you have probably already succeeded in this
+task. However, there is a potential for error if you needed to convert the
+files to the local standard text file format (for example, if you are on
+MS-DOS you may have converted LF end-of-line to CR/LF). You must apply
+such conversion to all the files EXCEPT those whose names begin with "test".
+The test files contain binary data; if you change them in any way then the
+self-test will give bad results.
+
+Please check the last section of this file to see if there are hints for the
+specific machine or compiler you are using.
+
+
+CONFIGURING THE SOFTWARE
+========================
+
+To configure the IJG code for your system, you need to create two files:
+ * jconfig.h: contains values for system-dependent #define symbols.
+ * Makefile: controls the compilation process.
+(On a non-Unix machine, you may create "project files" or some other
+substitute for a Makefile. jconfig.h is needed in any environment.)
+
+We provide three different ways to generate these files:
+ * On a Unix system, you can just run the "configure" script.
+ * We provide sample jconfig files and makefiles for popular machines;
+ if your machine matches one of the samples, just copy the right sample
+ files to jconfig.h and Makefile.
+ * If all else fails, read the instructions below and make your own files.
+
+
+Configuring the software using the automatic "configure" script
+---------------------------------------------------------------
+
+If you are on a Unix machine, you can just type
+ ./configure
+and let the configure script construct appropriate configuration files.
+If you're using "csh" on an old version of System V, you might need to type
+ sh configure
+instead to prevent csh from trying to execute configure itself.
+Expect configure to run for a few minutes, particularly on slower machines;
+it works by compiling a series of test programs.
+
+Configure was created with GNU Autoconf and it follows the usual conventions
+for GNU configure scripts. It makes a few assumptions that you may want to
+override. You can do this by providing optional switches to configure:
+
+* If you want to build libjpeg as a shared library, say
+ ./configure --enable-shared
+To get both shared and static libraries, say
+ ./configure --enable-shared --enable-static
+Note that these switches invoke GNU libtool to take care of system-dependent
+shared library building methods. If things don't work this way, please try
+running configure without either switch; that should build a static library
+without using libtool. If that works, your problem is probably with libtool
+not with the IJG code. libtool is fairly new and doesn't support all flavors
+of Unix yet. (You might be able to find a newer version of libtool than the
+one included with libjpeg; see ftp.gnu.org. Report libtool problems to
+bug-libtool@gnu.org.)
+
+* Configure will use gcc (GNU C compiler) if it's available, otherwise cc.
+To force a particular compiler to be selected, use the CC option, for example
+ ./configure CC='cc'
+The same method can be used to include any unusual compiler switches.
+For example, on HP-UX you probably want to say
+ ./configure CC='cc -Aa'
+to get HP's compiler to run in ANSI mode.
+
+* The default CFLAGS setting is "-O" for non-gcc compilers, "-O2" for gcc.
+You can override this by saying, for example,
+ ./configure CFLAGS='-g'
+if you want to compile with debugging support.
+
+* Configure will set up the makefile so that "make install" will install files
+into /usr/local/bin, /usr/local/man, etc. You can specify an installation
+prefix other than "/usr/local" by giving configure the option "--prefix=PATH".
+
+* If you don't have a lot of swap space, you may need to enable the IJG
+software's internal virtual memory mechanism. To do this, give the option
+"--enable-maxmem=N" where N is the default maxmemory limit in megabytes.
+This is discussed in more detail under "Selecting a memory manager", below.
+You probably don't need to worry about this on reasonably-sized Unix machines,
+unless you plan to process very large images.
+
+Configure has some other features that are useful if you are cross-compiling
+or working in a network of multiple machine types; but if you need those
+features, you probably already know how to use them.
+
+
+Configuring the software using one of the supplied jconfig and makefile files
+-----------------------------------------------------------------------------
+
+If you have one of these systems, you can just use the provided configuration
+files:
+
+Makefile jconfig file System and/or compiler
+
+makefile.manx jconfig.manx Amiga, Manx Aztec C
+makefile.sas jconfig.sas Amiga, SAS C
+makeproj.mac jconfig.mac Apple Macintosh, Metrowerks CodeWarrior
+mak*jpeg.st jconfig.st Atari ST/STE/TT, Pure C or Turbo C
+makefile.bcc jconfig.bcc MS-DOS or OS/2, Borland C
+makefile.dj jconfig.dj MS-DOS, DJGPP (Delorie's port of GNU C)
+makefile.mc6 jconfig.mc6 MS-DOS, Microsoft C (16-bit only)
+makefile.wat jconfig.wat MS-DOS, OS/2, or Windows NT, Watcom C
+makefile.vc jconfig.vc Windows NT/95, MS Visual C++
+make*.ds jconfig.vc Windows NT/95, MS Developer Studio
+makefile.mms jconfig.vms Digital VMS, with MMS software
+makefile.vms jconfig.vms Digital VMS, without MMS software
+
+Copy the proper jconfig file to jconfig.h and the makefile to Makefile (or
+whatever your system uses as the standard makefile name). For more info see
+the appropriate system-specific hints section near the end of this file.
+
+
+Configuring the software by hand
+--------------------------------
+
+First, generate a jconfig.h file. If you are moderately familiar with C,
+the comments in jconfig.doc should be enough information to do this; just
+copy jconfig.doc to jconfig.h and edit it appropriately. Otherwise, you may
+prefer to use the ckconfig.c program. You will need to compile and execute
+ckconfig.c by hand --- we hope you know at least enough to do that.
+ckconfig.c may not compile the first try (in fact, the whole idea is for it
+to fail if anything is going to). If you get compile errors, fix them by
+editing ckconfig.c according to the directions given in ckconfig.c. Once
+you get it to run, it will write a suitable jconfig.h file, and will also
+print out some advice about which makefile to use.
+
+You may also want to look at the canned jconfig files, if there is one for a
+system similar to yours.
+
+Second, select a makefile and copy it to Makefile (or whatever your system
+uses as the standard makefile name). The most generic makefiles we provide
+are
+ makefile.ansi: if your C compiler supports function prototypes
+ makefile.unix: if not.
+(You have function prototypes if ckconfig.c put "#define HAVE_PROTOTYPES"
+in jconfig.h.) You may want to start from one of the other makefiles if
+there is one for a system similar to yours.
+
+Look over the selected Makefile and adjust options as needed. In particular
+you may want to change the CC and CFLAGS definitions. For instance, if you
+are using GCC, set CC=gcc. If you had to use any compiler switches to get
+ckconfig.c to work, make sure the same switches are in CFLAGS.
+
+If you are on a system that doesn't use makefiles, you'll need to set up
+project files (or whatever you do use) to compile all the source files and
+link them into executable files cjpeg, djpeg, jpegtran, rdjpgcom, and wrjpgcom.
+See the file lists in any of the makefiles to find out which files go into
+each program. Note that the provided makefiles all make a "library" file
+libjpeg first, but you don't have to do that if you don't want to; the file
+lists identify which source files are actually needed for compression,
+decompression, or both. As a last resort, you can make a batch script that
+just compiles everything and links it all together; makefile.vms is an example
+of this (it's for VMS systems that have no make-like utility).
+
+Here are comments about some specific configuration decisions you'll
+need to make:
+
+Command line style
+------------------
+
+These programs can use a Unix-like command line style which supports
+redirection and piping, like this:
+ cjpeg inputfile >outputfile
+ cjpeg <inputfile >outputfile
+ source program | cjpeg >outputfile
+The simpler "two file" command line style is just
+ cjpeg inputfile outputfile
+You may prefer the two-file style, particularly if you don't have pipes.
+
+You MUST use two-file style on any system that doesn't cope well with binary
+data fed through stdin/stdout; this is true for some MS-DOS compilers, for
+example. If you're not on a Unix system, it's safest to assume you need
+two-file style. (But if your compiler provides either the Posix-standard
+fdopen() library routine or a Microsoft-compatible setmode() routine, you
+can safely use the Unix command line style, by defining USE_FDOPEN or
+USE_SETMODE respectively.)
+
+To use the two-file style, make jconfig.h say "#define TWO_FILE_COMMANDLINE".
+
+Selecting a memory manager
+--------------------------
+
+The IJG code is capable of working on images that are too big to fit in main
+memory; data is swapped out to temporary files as necessary. However, the
+code to do this is rather system-dependent. We provide five different
+memory managers:
+
+* jmemansi.c This version uses the ANSI-standard library routine tmpfile(),
+ which not all non-ANSI systems have. On some systems
+ tmpfile() may put the temporary file in a non-optimal
+ location; if you don't like what it does, use jmemname.c.
+
+* jmemname.c This version creates named temporary files. For anything
+ except a Unix machine, you'll need to configure the
+ select_file_name() routine appropriately; see the comments
+ near the head of jmemname.c. If you use this version, define
+ NEED_SIGNAL_CATCHER in jconfig.h to make sure the temp files
+ are removed if the program is aborted.
+
+* jmemnobs.c (That stands for No Backing Store :-).) This will compile on
+ almost any system, but it assumes you have enough main memory
+ or virtual memory to hold the biggest images you work with.
+
+* jmemdos.c This should be used with most 16-bit MS-DOS compilers.
+ See the system-specific notes about MS-DOS for more info.
+ IMPORTANT: if you use this, define USE_MSDOS_MEMMGR in
+ jconfig.h, and include the assembly file jmemdosa.asm in the
+ programs. The supplied makefiles and jconfig files for
+ 16-bit MS-DOS compilers already do both.
+
+* jmemmac.c Custom version for Apple Macintosh; see the system-specific
+ notes for Macintosh for more info.
+
+To use a particular memory manager, change the SYSDEPMEM variable in your
+makefile to equal the corresponding object file name (for example, jmemansi.o
+or jmemansi.obj for jmemansi.c).
+
+If you have plenty of (real or virtual) main memory, just use jmemnobs.c.
+"Plenty" means about ten bytes for every pixel in the largest images
+you plan to process, so a lot of systems don't meet this criterion.
+If yours doesn't, try jmemansi.c first. If that doesn't compile, you'll have
+to use jmemname.c; be sure to adjust select_file_name() for local conditions.
+You may also need to change unlink() to remove() in close_backing_store().
+
+Except with jmemnobs.c or jmemmac.c, you need to adjust the DEFAULT_MAX_MEM
+setting to a reasonable value for your system (either by adding a #define for
+DEFAULT_MAX_MEM to jconfig.h, or by adding a -D switch to the Makefile).
+This value limits the amount of data space the program will attempt to
+allocate. Code and static data space isn't counted, so the actual memory
+needs for cjpeg or djpeg are typically 100 to 150Kb more than the max-memory
+setting. Larger max-memory settings reduce the amount of I/O needed to
+process a large image, but too large a value can result in "insufficient
+memory" failures. On most Unix machines (and other systems with virtual
+memory), just set DEFAULT_MAX_MEM to several million and forget it. At the
+other end of the spectrum, for MS-DOS machines you probably can't go much
+above 300K to 400K. (On MS-DOS the value refers to conventional memory only.
+Extended/expanded memory is handled separately by jmemdos.c.)
+
+
+BUILDING THE SOFTWARE
+=====================
+
+Now you should be able to compile the software. Just say "make" (or
+whatever's necessary to start the compilation). Have a cup of coffee.
+
+Here are some things that could go wrong:
+
+If your compiler complains about undefined structures, you should be able to
+shut it up by putting "#define INCOMPLETE_TYPES_BROKEN" in jconfig.h.
+
+If you have trouble with missing system include files or inclusion of the
+wrong ones, read jinclude.h. This shouldn't happen if you used configure
+or ckconfig.c to set up jconfig.h.
+
+There are a fair number of routines that do not use all of their parameters;
+some compilers will issue warnings about this, which you can ignore. There
+are also a few configuration checks that may give "unreachable code" warnings.
+Any other warning deserves investigation.
+
+If you don't have a getenv() library routine, define NO_GETENV.
+
+Also see the system-specific hints, below.
+
+
+TESTING THE SOFTWARE
+====================
+
+As a quick test of functionality we've included a small sample image in
+several forms:
+ testorig.jpg Starting point for the djpeg tests.
+ testimg.ppm The output of djpeg testorig.jpg
+ testimg.bmp The output of djpeg -bmp -colors 256 testorig.jpg
+ testimg.jpg The output of cjpeg testimg.ppm
+ testprog.jpg Progressive-mode equivalent of testorig.jpg.
+ testimgp.jpg The output of cjpeg -progressive -optimize testimg.ppm
+(The first- and second-generation .jpg files aren't identical since JPEG is
+lossy.) If you can generate duplicates of the testimg* files then you
+probably have working programs.
+
+With most of the makefiles, "make test" will perform the necessary
+comparisons.
+
+If you're using a makefile that doesn't provide the test option, run djpeg
+and cjpeg by hand and compare the output files to testimg* with whatever
+binary file comparison tool you have. The files should be bit-for-bit
+identical.
+
+If the programs complain "MAX_ALLOC_CHUNK is wrong, please fix", then you
+need to reduce MAX_ALLOC_CHUNK to a value that fits in type size_t.
+Try adding "#define MAX_ALLOC_CHUNK 65520L" to jconfig.h. A less likely
+configuration error is "ALIGN_TYPE is wrong, please fix": defining ALIGN_TYPE
+as long should take care of that one.
+
+If the cjpeg test run fails with "Missing Huffman code table entry", it's a
+good bet that you needed to define RIGHT_SHIFT_IS_UNSIGNED. Go back to the
+configuration step and run ckconfig.c. (This is a good plan for any other
+test failure, too.)
+
+If you are using Unix (one-file) command line style on a non-Unix system,
+it's a good idea to check that binary I/O through stdin/stdout actually
+works. You should get the same results from "djpeg <testorig.jpg >out.ppm"
+as from "djpeg -outfile out.ppm testorig.jpg". Note that the makefiles all
+use the latter style and therefore do not exercise stdin/stdout! If this
+check fails, try recompiling with USE_SETMODE or USE_FDOPEN defined.
+If it still doesn't work, better use two-file style.
+
+If you chose a memory manager other than jmemnobs.c, you should test that
+temporary-file usage works. Try "djpeg -bmp -colors 256 -max 0 testorig.jpg"
+and make sure its output matches testimg.bmp. If you have any really large
+images handy, try compressing them with -optimize and/or decompressing with
+-colors 256 to make sure your DEFAULT_MAX_MEM setting is not too large.
+
+NOTE: this is far from an exhaustive test of the JPEG software; some modules,
+such as 1-pass color quantization, are not exercised at all. It's just a
+quick test to give you some confidence that you haven't missed something
+major.
+
+
+INSTALLING THE SOFTWARE
+=======================
+
+Once you're done with the above steps, you can install the software by
+copying the executable files (cjpeg, djpeg, jpegtran, rdjpgcom, and wrjpgcom)
+to wherever you normally install programs. On Unix systems, you'll also want
+to put the man pages (cjpeg.1, djpeg.1, jpegtran.1, rdjpgcom.1, wrjpgcom.1)
+in the man-page directory. The pre-fab makefiles don't support this step
+since there's such a wide variety of installation procedures on different
+systems.
+
+If you generated a Makefile with the "configure" script, you can just say
+ make install
+to install the programs and their man pages into the standard places.
+(You'll probably need to be root to do this.) We recommend first saying
+ make -n install
+to see where configure thought the files should go. You may need to edit
+the Makefile, particularly if your system's conventions for man page
+filenames don't match what configure expects.
+
+If you want to install the IJG library itself, for use in compiling other
+programs besides ours, then you need to put the four include files
+ jpeglib.h jerror.h jconfig.h jmorecfg.h
+into your include-file directory, and put the library file libjpeg.a
+(extension may vary depending on system) wherever library files go.
+If you generated a Makefile with "configure", it will do what it thinks
+is the right thing if you say
+ make install-lib
+
+
+OPTIONAL STUFF
+==============
+
+Progress monitor:
+
+If you like, you can #define PROGRESS_REPORT (in jconfig.h) to enable display
+of percent-done progress reports. The routine provided in cdjpeg.c merely
+prints percentages to stderr, but you can customize it to do something
+fancier.
+
+Utah RLE file format support:
+
+We distribute the software with support for RLE image files (Utah Raster
+Toolkit format) disabled, because the RLE support won't compile without the
+Utah library. If you have URT version 3.1 or later, you can enable RLE
+support as follows:
+ 1. #define RLE_SUPPORTED in jconfig.h.
+ 2. Add a -I option to CFLAGS in the Makefile for the directory
+ containing the URT .h files (typically the "include"
+ subdirectory of the URT distribution).
+ 3. Add -L... -lrle to LDLIBS in the Makefile, where ... specifies
+ the directory containing the URT "librle.a" file (typically the
+ "lib" subdirectory of the URT distribution).
+
+Support for 12-bit-deep pixel data:
+
+The JPEG standard allows either 8-bit or 12-bit data precision. (For color,
+this means 8 or 12 bits per channel, of course.) If you need to work with
+deeper than 8-bit data, you can compile the IJG code for 12-bit operation.
+To do so:
+ 1. In jmorecfg.h, define BITS_IN_JSAMPLE as 12 rather than 8.
+ 2. In jconfig.h, undefine BMP_SUPPORTED, RLE_SUPPORTED, and TARGA_SUPPORTED,
+ because the code for those formats doesn't handle 12-bit data and won't
+ even compile. (The PPM code does work, as explained below. The GIF
+ code works too; it scales 8-bit GIF data to and from 12-bit depth
+ automatically.)
+ 3. Compile. Don't expect "make test" to pass, since the supplied test
+ files are for 8-bit data.
+
+Currently, 12-bit support does not work on 16-bit-int machines.
+
+Note that a 12-bit version will not read 8-bit JPEG files, nor vice versa;
+so you'll want to keep around a regular 8-bit compilation as well.
+(Run-time selection of data depth, to allow a single copy that does both,
+is possible but would probably slow things down considerably; it's very low
+on our to-do list.)
+
+The PPM reader (rdppm.c) can read 12-bit data from either text-format or
+binary-format PPM and PGM files. Binary-format PPM/PGM files which have a
+maxval greater than 255 are assumed to use 2 bytes per sample, LSB first
+(little-endian order). As of early 1995, 2-byte binary format is not
+officially supported by the PBMPLUS library, but it is expected that a
+future release of PBMPLUS will support it. Note that the PPM reader will
+read files of any maxval regardless of the BITS_IN_JSAMPLE setting; incoming
+data is automatically rescaled to either maxval=255 or maxval=4095 as
+appropriate for the cjpeg bit depth.
+
+The PPM writer (wrppm.c) will normally write 2-byte binary PPM or PGM
+format, maxval 4095, when compiled with BITS_IN_JSAMPLE=12. Since this
+format is not yet widely supported, you can disable it by compiling wrppm.c
+with PPM_NORAWWORD defined; then the data is scaled down to 8 bits to make a
+standard 1-byte/sample PPM or PGM file. (Yes, this means still another copy
+of djpeg to keep around. But hopefully you won't need it for very long.
+Poskanzer's supposed to get that new PBMPLUS release out Real Soon Now.)
+
+Of course, if you are working with 12-bit data, you probably have it stored
+in some other, nonstandard format. In that case you'll probably want to
+write your own I/O modules to read and write your format.
+
+Note that a 12-bit version of cjpeg always runs in "-optimize" mode, in
+order to generate valid Huffman tables. This is necessary because our
+default Huffman tables only cover 8-bit data.
+
+Removing code:
+
+If you need to make a smaller version of the JPEG software, some optional
+functions can be removed at compile time. See the xxx_SUPPORTED #defines in
+jconfig.h and jmorecfg.h. If at all possible, we recommend that you leave in
+decoder support for all valid JPEG files, to ensure that you can read anyone's
+output. Taking out support for image file formats that you don't use is the
+most painless way to make the programs smaller. Another possibility is to
+remove some of the DCT methods: in particular, the "IFAST" method may not be
+enough faster than the others to be worth keeping on your machine. (If you
+do remove ISLOW or IFAST, be sure to redefine JDCT_DEFAULT or JDCT_FASTEST
+to a supported method, by adding a #define in jconfig.h.)
+
+
+OPTIMIZATION
+============
+
+Unless you own a Cray, you'll probably be interested in making the JPEG
+software go as fast as possible. This section covers some machine-dependent
+optimizations you may want to try. We suggest that before trying any of
+this, you first get the basic installation to pass the self-test step.
+Repeat the self-test after any optimization to make sure that you haven't
+broken anything.
+
+The integer DCT routines perform a lot of multiplications. These
+multiplications must yield 32-bit results, but none of their input values
+are more than 16 bits wide. On many machines, notably the 680x0 and 80x86
+CPUs, a 16x16=>32 bit multiply instruction is faster than a full 32x32=>32
+bit multiply. Unfortunately there is no portable way to specify such a
+multiplication in C, but some compilers can generate one when you use the
+right combination of casts. See the MULTIPLYxxx macro definitions in
+jdct.h. If your compiler makes "int" be 32 bits and "short" be 16 bits,
+defining SHORTxSHORT_32 is fairly likely to work. When experimenting with
+alternate definitions, be sure to test not only whether the code still works
+(use the self-test), but also whether it is actually faster --- on some
+compilers, alternate definitions may compute the right answer, yet be slower
+than the default. Timing cjpeg on a large PGM (grayscale) input file is the
+best way to check this, as the DCT will be the largest fraction of the runtime
+in that mode. (Note: some of the distributed compiler-specific jconfig files
+already contain #define switches to select appropriate MULTIPLYxxx
+definitions.)
+
+If your machine has sufficiently fast floating point hardware, you may find
+that the float DCT method is faster than the integer DCT methods, even
+after tweaking the integer multiply macros. In that case you may want to
+make the float DCT be the default method. (The only objection to this is
+that float DCT results may vary slightly across machines.) To do that, add
+"#define JDCT_DEFAULT JDCT_FLOAT" to jconfig.h. Even if you don't change
+the default, you should redefine JDCT_FASTEST, which is the method selected
+by djpeg's -fast switch. Don't forget to update the documentation files
+(usage.doc and/or cjpeg.1, djpeg.1) to agree with what you've done.
+
+If access to "short" arrays is slow on your machine, it may be a win to
+define type JCOEF as int rather than short. This will cost a good deal of
+memory though, particularly in some multi-pass modes, so don't do it unless
+you have memory to burn and short is REALLY slow.
+
+If your compiler can compile function calls in-line, make sure the INLINE
+macro in jmorecfg.h is defined as the keyword that marks a function
+inline-able. Some compilers have a switch that tells the compiler to inline
+any function it thinks is profitable (e.g., -finline-functions for gcc).
+Enabling such a switch is likely to make the compiled code bigger but faster.
+
+In general, it's worth trying the maximum optimization level of your compiler,
+and experimenting with any optional optimizations such as loop unrolling.
+(Unfortunately, far too many compilers have optimizer bugs ... be prepared to
+back off if the code fails self-test.) If you do any experimentation along
+these lines, please report the optimal settings to jpeg-info@uunet.uu.net so
+we can mention them in future releases. Be sure to specify your machine and
+compiler version.
+
+
+HINTS FOR SPECIFIC SYSTEMS
+==========================
+
+We welcome reports on changes needed for systems not mentioned here. Submit
+'em to jpeg-info@uunet.uu.net. Also, if configure or ckconfig.c is wrong
+about how to configure the JPEG software for your system, please let us know.
+
+
+Acorn RISC OS:
+
+(Thanks to Simon Middleton for these hints on compiling with Desktop C.)
+After renaming the files according to Acorn conventions, take a copy of
+makefile.ansi, change all occurrences of 'libjpeg.a' to 'libjpeg.o' and
+change these definitions as indicated:
+
+CFLAGS= -throwback -IC: -Wn
+LDLIBS=C:o.Stubs
+SYSDEPMEM=jmemansi.o
+LN=Link
+AR=LibFile -c -o
+
+Also add a new line '.c.o:; $(cc) $< $(cflags) -c -o $@'. Remove the
+lines '$(RM) libjpeg.o' and '$(AR2) libjpeg.o' and the 'jconfig.h'
+dependency section.
+
+Copy jconfig.doc to jconfig.h. Edit jconfig.h to define TWO_FILE_COMMANDLINE
+and CHAR_IS_UNSIGNED.
+
+Run the makefile using !AMU not !Make. If you want to use the 'clean' and
+'test' makefile entries then you will have to fiddle with the syntax a bit
+and rename the test files.
+
+
+Amiga:
+
+SAS C 6.50 reportedly is too buggy to compile the IJG code properly.
+A patch to update to 6.51 is available from SAS or AmiNet FTP sites.
+
+The supplied config files are set up to use jmemname.c as the memory
+manager, with temporary files being created on the device named by
+"JPEGTMP:".
+
+
+Atari ST/STE/TT:
+
+Copy the project files makcjpeg.st, makdjpeg.st, maktjpeg.st, and makljpeg.st
+to cjpeg.prj, djpeg.prj, jpegtran.prj, and libjpeg.prj respectively. The
+project files should work as-is with Pure C. For Turbo C, change library
+filenames "pc..." to "tc..." in each project file. Note that libjpeg.prj
+selects jmemansi.c as the recommended memory manager. You'll probably want to
+adjust the DEFAULT_MAX_MEM setting --- you want it to be a couple hundred K
+less than your normal free memory. Put "#define DEFAULT_MAX_MEM nnnn" into
+jconfig.h to do this.
+
+To use the 68881/68882 coprocessor for the floating point DCT, add the
+compiler option "-8" to the project files and replace pcfltlib.lib with
+pc881lib.lib in cjpeg.prj and djpeg.prj. Or if you don't have a
+coprocessor, you may prefer to remove the float DCT code by undefining
+DCT_FLOAT_SUPPORTED in jmorecfg.h (since without a coprocessor, the float
+code will be too slow to be useful). In that case, you can delete
+pcfltlib.lib from the project files.
+
+Note that you must make libjpeg.lib before making cjpeg.ttp, djpeg.ttp,
+or jpegtran.ttp. You'll have to perform the self-test by hand.
+
+We haven't bothered to include project files for rdjpgcom and wrjpgcom.
+Those source files should just be compiled by themselves; they don't
+depend on the JPEG library.
+
+There is a bug in some older versions of the Turbo C library which causes the
+space used by temporary files created with "tmpfile()" not to be freed after
+an abnormal program exit. If you check your disk afterwards, you will find
+cluster chains that are allocated but not used by a file. This should not
+happen in cjpeg/djpeg/jpegtran, since we enable a signal catcher to explicitly
+close temp files before exiting. But if you use the JPEG library with your
+own code, be sure to supply a signal catcher, or else use a different
+system-dependent memory manager.
+
+
+Cray:
+
+Should you be so fortunate as to be running JPEG on a Cray YMP, there is a
+compiler bug in old versions of Cray's Standard C (prior to 3.1). If you
+still have an old compiler, you'll need to insert a line reading
+"#pragma novector" just before the loop
+ for (i = 1; i <= (int) htbl->bits[l]; i++)
+ huffsize[p++] = (char) l;
+in fix_huff_tbl (in V5beta1, line 204 of jchuff.c and line 176 of jdhuff.c).
+[This bug may or may not still occur with the current IJG code, but it's
+probably a dead issue anyway...]
+
+
+HP-UX:
+
+If you have HP-UX 7.05 or later with the "software development" C compiler,
+you should run the compiler in ANSI mode. If using the configure script,
+say
+ ./configure CC='cc -Aa'
+(or -Ae if you prefer). If configuring by hand, use makefile.ansi and add
+"-Aa" to the CFLAGS line in the makefile.
+
+If you have a pre-7.05 system, or if you are using the non-ANSI C compiler
+delivered with a minimum HP-UX system, then you must use makefile.unix
+(and do NOT add -Aa); or just run configure without the CC option.
+
+On HP 9000 series 800 machines, the HP C compiler is buggy in revisions prior
+to A.08.07. If you get complaints about "not a typedef name", you'll have to
+use makefile.unix, or run configure without the CC option.
+
+
+Macintosh, generic comments:
+
+The supplied user-interface files (cjpeg.c, djpeg.c, etc) are set up to
+provide a Unix-style command line interface. You can use this interface on
+the Mac by means of the ccommand() library routine provided by Metrowerks
+CodeWarrior or Think C. This is only appropriate for testing the library,
+however; to make a user-friendly equivalent of cjpeg/djpeg you'd really want
+to develop a Mac-style user interface. There isn't a complete example
+available at the moment, but there are some helpful starting points:
+1. Sam Bushell's free "To JPEG" applet provides drag-and-drop conversion to
+JPEG under System 7 and later. This only illustrates how to use the
+compression half of the library, but it does a very nice job of that part.
+The CodeWarrior source code is available from http://www.pobox.com/~jsam.
+2. Jim Brunner prepared a Mac-style user interface for both compression and
+decompression. Unfortunately, it hasn't been updated since IJG v4, and
+the library's API has changed considerably since then. Still it may be of
+some help, particularly as a guide to compiling the IJG code under Think C.
+Jim's code is available from the Info-Mac archives, at sumex-aim.stanford.edu
+or mirrors thereof; see file /info-mac/dev/src/jpeg-convert-c.hqx.
+
+jmemmac.c is the recommended memory manager back end for Macintosh. It uses
+NewPtr/DisposePtr instead of malloc/free, and has a Mac-specific
+implementation of jpeg_mem_available(). It also creates temporary files that
+follow Mac conventions. (That part of the code relies on System-7-or-later OS
+functions. See the comments in jmemmac.c if you need to run it on System 6.)
+NOTE that USE_MAC_MEMMGR must be defined in jconfig.h to use jmemmac.c.
+
+You can also use jmemnobs.c, if you don't care about handling images larger
+than available memory. If you use any memory manager back end other than
+jmemmac.c, we recommend replacing "malloc" and "free" by "NewPtr" and
+"DisposePtr", because Mac C libraries often have peculiar implementations of
+malloc/free. (For instance, free() may not return the freed space to the
+Mac Memory Manager. This is undesirable for the IJG code because jmemmgr.c
+already clumps space requests.)
+
+
+Macintosh, Metrowerks CodeWarrior:
+
+The Unix-command-line-style interface can be used by defining USE_CCOMMAND.
+You'll also need to define TWO_FILE_COMMANDLINE to avoid stdin/stdout.
+This means that when using the cjpeg/djpeg programs, you'll have to type the
+input and output file names in the "Arguments" text-edit box, rather than
+using the file radio buttons. (Perhaps USE_FDOPEN or USE_SETMODE would
+eliminate the problem, but I haven't heard from anyone who's tried it.)
+
+On 680x0 Macs, Metrowerks defines type "double" as a 10-byte IEEE extended
+float. jmemmgr.c won't like this: it wants sizeof(ALIGN_TYPE) to be a power
+of 2. Add "#define ALIGN_TYPE long" to jconfig.h to eliminate the complaint.
+
+The supplied configuration file jconfig.mac can be used for your jconfig.h;
+it includes all the recommended symbol definitions. If you have AppleScript
+installed, you can run the supplied script makeproj.mac to create CodeWarrior
+project files for the library and the testbed applications, then build the
+library and applications. (Thanks to Dan Sears and Don Agro for this nifty
+hack, which saves us from trying to maintain CodeWarrior project files as part
+of the IJG distribution...)
+
+
+Macintosh, Think C:
+
+The documentation in Jim Brunner's "JPEG Convert" source code (see above)
+includes detailed build instructions for Think C; it's probably somewhat
+out of date for the current release, but may be helpful.
+
+If you want to build the minimal command line version, proceed as follows.
+You'll have to prepare project files for the programs; we don't include any
+in the distribution since they are not text files. Use the file lists in
+any of the supplied makefiles as a guide. Also add the ANSI and Unix C
+libraries in a separate segment. You may need to divide the JPEG files into
+more than one segment; we recommend dividing compression and decompression
+modules. Define USE_CCOMMAND in jconfig.h so that the ccommand() routine is
+called. You must also define TWO_FILE_COMMANDLINE because stdin/stdout
+don't handle binary data correctly.
+
+On 680x0 Macs, Think C defines type "double" as a 12-byte IEEE extended float.
+jmemmgr.c won't like this: it wants sizeof(ALIGN_TYPE) to be a power of 2.
+Add "#define ALIGN_TYPE long" to jconfig.h to eliminate the complaint.
+
+jconfig.mac should work as a jconfig.h configuration file for Think C,
+but the makeproj.mac AppleScript script is specific to CodeWarrior. Sorry.
+
+
+MIPS R3000:
+
+MIPS's cc version 1.31 has a rather nasty optimization bug. Don't use -O
+if you have that compiler version. (Use "cc -V" to check the version.)
+Note that the R3000 chip is found in workstations from DEC and others.
+
+
+MS-DOS, generic comments for 16-bit compilers:
+
+The IJG code is designed to work well in 80x86 "small" or "medium" memory
+models (i.e., data pointers are 16 bits unless explicitly declared "far";
+code pointers can be either size). You may be able to use small model to
+compile cjpeg or djpeg by itself, but you will probably have to use medium
+model for any larger application. This won't make much difference in
+performance. You *will* take a noticeable performance hit if you use a
+large-data memory model, and you should avoid "huge" model if at all
+possible. Be sure that NEED_FAR_POINTERS is defined in jconfig.h if you use
+a small-data memory model; be sure it is NOT defined if you use a large-data
+model. (The supplied makefiles and jconfig files for Borland and Microsoft C
+compile in medium model and define NEED_FAR_POINTERS.)
+
+The DOS-specific memory manager, jmemdos.c, should be used if possible.
+It needs some assembly-code routines which are in jmemdosa.asm; make sure
+your makefile assembles that file and includes it in the library. If you
+don't have a suitable assembler, you can get pre-assembled object files for
+jmemdosa by FTP from ftp.uu.net:/graphics/jpeg/jdosaobj.zip. (DOS-oriented
+distributions of the IJG source code often include these object files.)
+
+When using jmemdos.c, jconfig.h must define USE_MSDOS_MEMMGR and must set
+MAX_ALLOC_CHUNK to less than 64K (65520L is a typical value). If your
+C library's far-heap malloc() can't allocate blocks that large, reduce
+MAX_ALLOC_CHUNK to whatever it can handle.
+
+If you can't use jmemdos.c for some reason --- for example, because you
+don't have an assembler to assemble jmemdosa.asm --- you'll have to fall
+back to jmemansi.c or jmemname.c. You'll probably still need to set
+MAX_ALLOC_CHUNK in jconfig.h, because most DOS C libraries won't malloc()
+more than 64K at a time. IMPORTANT: if you use jmemansi.c or jmemname.c,
+you will have to compile in a large-data memory model in order to get the
+right stdio library. Too bad.
+
+wrjpgcom needs to be compiled in large model, because it malloc()s a 64KB
+work area to hold the comment text. If your C library's malloc can't
+handle that, reduce MAX_COM_LENGTH as necessary in wrjpgcom.c.
+
+Most MS-DOS compilers treat stdin/stdout as text files, so you must use
+two-file command line style. But if your compiler has either fdopen() or
+setmode(), you can use one-file style if you like. To do this, define
+USE_SETMODE or USE_FDOPEN so that stdin/stdout will be set to binary mode.
+(USE_SETMODE seems to work with more DOS compilers than USE_FDOPEN.) You
+should test that I/O through stdin/stdout produces the same results as I/O
+to explicitly named files... the "make test" procedures in the supplied
+makefiles do NOT use stdin/stdout.
+
+
+MS-DOS, generic comments for 32-bit compilers:
+
+None of the above comments about memory models apply if you are using a
+32-bit flat-memory-space environment, such as DJGPP or Watcom C. (And you
+should use one if you have it, as performance will be much better than
+8086-compatible code!) For flat-memory-space compilers, do NOT define
+NEED_FAR_POINTERS, and do NOT use jmemdos.c. Use jmemnobs.c if the
+environment supplies adequate virtual memory, otherwise use jmemansi.c or
+jmemname.c.
+
+You'll still need to be careful about binary I/O through stdin/stdout.
+See the last paragraph of the previous section.
+
+
+MS-DOS, Borland C:
+
+Be sure to convert all the source files to DOS text format (CR/LF newlines).
+Although Borland C will often work OK with unmodified Unix (LF newlines)
+source files, sometimes it will give bogus compile errors.
+"Illegal character '#'" is the most common such error. (This is true with
+Borland C 3.1, but perhaps is fixed in newer releases.)
+
+If you want one-file command line style, just undefine TWO_FILE_COMMANDLINE.
+jconfig.bcc already includes #define USE_SETMODE to make this work.
+(fdopen does not work correctly.)
+
+
+MS-DOS, Microsoft C:
+
+makefile.mc6 works with Microsoft C, DOS Visual C++, etc. It should only
+be used if you want to build a 16-bit (small or medium memory model) program.
+
+If you want one-file command line style, just undefine TWO_FILE_COMMANDLINE.
+jconfig.mc6 already includes #define USE_SETMODE to make this work.
+(fdopen does not work correctly.)
+
+Note that this makefile assumes that the working copy of itself is called
+"makefile". If you want to call it something else, say "makefile.mak",
+be sure to adjust the dependency line that reads "$(RFILE) : makefile".
+Otherwise the make will fail because it doesn't know how to create "makefile".
+Worse, some releases of Microsoft's make utilities give an incorrect error
+message in this situation.
+
+Old versions of MS C fail with an "out of macro expansion space" error
+because they can't cope with the macro TRACEMS8 (defined in jerror.h).
+If this happens to you, the easiest solution is to change TRACEMS8 to
+expand to nothing. You'll lose the ability to dump out JPEG coefficient
+tables with djpeg -debug -debug, but at least you can compile.
+
+Original MS C 6.0 is very buggy; it compiles incorrect code unless you turn
+off optimization entirely (remove -O from CFLAGS). 6.00A is better, but it
+still generates bad code if you enable loop optimizations (-Ol or -Ox).
+
+MS C 8.0 crashes when compiling jquant1.c with optimization switch /Oo ...
+which is on by default. To work around this bug, compile that one file
+with /Oo-.
+
+
+Microsoft Windows (all versions), generic comments:
+
+Some Windows system include files define typedef boolean as "unsigned char".
+The IJG code also defines typedef boolean, but we make it "int" by default.
+This doesn't affect the IJG programs because we don't import those Windows
+include files. But if you use the JPEG library in your own program, and some
+of your program's files import one definition of boolean while some import the
+other, you can get all sorts of mysterious problems. A good preventive step
+is to make the IJG library use "unsigned char" for boolean. To do that,
+add something like this to your jconfig.h file:
+ /* Define "boolean" as unsigned char, not int, per Windows custom */
+ #ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+ typedef unsigned char boolean;
+ #endif
+ #define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+(This is already in jconfig.vc, by the way.)
+
+windef.h contains the declarations
+ #define far
+ #define FAR far
+Since jmorecfg.h tries to define FAR as empty, you may get a compiler
+warning if you include both jpeglib.h and windef.h (which windows.h
+includes). To suppress the warning, you can put "#ifndef FAR"/"#endif"
+around the line "#define FAR" in jmorecfg.h.
+
+When using the library in a Windows application, you will almost certainly
+want to modify or replace the error handler module jerror.c, since our
+default error handler does a couple of inappropriate things:
+ 1. it tries to write error and warning messages on stderr;
+ 2. in event of a fatal error, it exits by calling exit().
+
+A simple stopgap solution for problem 1 is to replace the line
+ fprintf(stderr, "%s\n", buffer);
+(in output_message in jerror.c) with
+ MessageBox(GetActiveWindow(),buffer,"JPEG Error",MB_OK|MB_ICONERROR);
+It's highly recommended that you at least do that much, since otherwise
+error messages will disappear into nowhere. (Beginning with IJG v6b, this
+code is already present in jerror.c; just define USE_WINDOWS_MESSAGEBOX in
+jconfig.h to enable it.)
+
+The proper solution for problem 2 is to return control to your calling
+application after a library error. This can be done with the setjmp/longjmp
+technique discussed in libjpeg.doc and illustrated in example.c. (NOTE:
+some older Windows C compilers provide versions of setjmp/longjmp that
+don't actually work under Windows. You may need to use the Windows system
+functions Catch and Throw instead.)
+
+The recommended memory manager under Windows is jmemnobs.c; in other words,
+let Windows do any virtual memory management needed. You should NOT use
+jmemdos.c nor jmemdosa.asm under Windows.
+
+For Windows 3.1, we recommend compiling in medium or large memory model;
+for newer Windows versions, use a 32-bit flat memory model. (See the MS-DOS
+sections above for more info about memory models.) In the 16-bit memory
+models only, you'll need to put
+ #define MAX_ALLOC_CHUNK 65520L /* Maximum request to malloc() */
+into jconfig.h to limit allocation chunks to 64Kb. (Without that, you'd
+have to use huge memory model, which slows things down unnecessarily.)
+jmemnobs.c works without modification in large or flat memory models, but to
+use medium model, you need to modify its jpeg_get_large and jpeg_free_large
+routines to allocate far memory. In any case, you might like to replace
+its calls to malloc and free with direct calls on Windows memory allocation
+functions.
+
+You may also want to modify jdatasrc.c and jdatadst.c to use Windows file
+operations rather than fread/fwrite. This is only necessary if your C
+compiler doesn't provide a competent implementation of C stdio functions.
+
+You might want to tweak the RGB_xxx macros in jmorecfg.h so that the library
+will accept or deliver color pixels in BGR sample order, not RGB; BGR order
+is usually more convenient under Windows. Note that this change will break
+the sample applications cjpeg/djpeg, but the library itself works fine.
+
+
+Many people want to convert the IJG library into a DLL. This is reasonably
+straightforward, but watch out for the following:
+
+ 1. Don't try to compile as a DLL in small or medium memory model; use
+large model, or even better, 32-bit flat model. Many places in the IJG code
+assume the address of a local variable is an ordinary (not FAR) pointer;
+that isn't true in a medium-model DLL.
+
+ 2. Microsoft C cannot pass file pointers between applications and DLLs.
+(See Microsoft Knowledge Base, PSS ID Number Q50336.) So jdatasrc.c and
+jdatadst.c don't work if you open a file in your application and then pass
+the pointer to the DLL. One workaround is to make jdatasrc.c/jdatadst.c
+part of your main application rather than part of the DLL.
+
+ 3. You'll probably need to modify the macros GLOBAL() and EXTERN() to
+attach suitable linkage keywords to the exported routine names. Similarly,
+you'll want to modify METHODDEF() and JMETHOD() to ensure function pointers
+are declared in a way that lets application routines be called back through
+the function pointers. These macros are in jmorecfg.h. Typical definitions
+for a 16-bit DLL are:
+ #define GLOBAL(type) type _far _pascal _loadds _export
+ #define EXTERN(type) extern type _far _pascal _loadds
+ #define METHODDEF(type) static type _far _pascal
+ #define JMETHOD(type,methodname,arglist) \
+ type (_far _pascal *methodname) arglist
+For a 32-bit DLL you may want something like
+ #define GLOBAL(type) __declspec(dllexport) type
+ #define EXTERN(type) extern __declspec(dllexport) type
+Although not all the GLOBAL routines are actually intended to be called by
+the application, the performance cost of making them all DLL entry points is
+negligible.
+
+The unmodified IJG library presents a very C-specific application interface,
+so the resulting DLL is only usable from C or C++ applications. There has
+been some talk of writing wrapper code that would present a simpler interface
+usable from other languages, such as Visual Basic. This is on our to-do list
+but hasn't been very high priority --- any volunteers out there?
+
+
+Microsoft Windows, Borland C:
+
+The provided jconfig.bcc should work OK in a 32-bit Windows environment,
+but you'll need to tweak it in a 16-bit environment (you'd need to define
+NEED_FAR_POINTERS and MAX_ALLOC_CHUNK). Beware that makefile.bcc will need
+alteration if you want to use it for Windows --- in particular, you should
+use jmemnobs.c not jmemdos.c under Windows.
+
+Borland C++ 4.5 fails with an internal compiler error when trying to compile
+jdmerge.c in 32-bit mode. If enough people complain, perhaps Borland will fix
+it. In the meantime, the simplest known workaround is to add a redundant
+definition of the variable range_limit in h2v1_merged_upsample(), at the head
+of the block that handles odd image width (about line 268 in v6 jdmerge.c):
+ /* If image width is odd, do the last output column separately */
+ if (cinfo->output_width & 1) {
+ register JSAMPLE * range_limit = cinfo->sample_range_limit; /* ADD THIS */
+ cb = GETJSAMPLE(*inptr1);
+Pretty bizarre, especially since the very similar routine h2v2_merged_upsample
+doesn't trigger the bug.
+Recent reports suggest that this bug does not occur with "bcc32a" (the
+Pentium-optimized version of the compiler).
+
+Another report from a user of Borland C 4.5 was that incorrect code (leading
+to a color shift in processed images) was produced if any of the following
+optimization switch combinations were used:
+ -Ot -Og
+ -Ot -Op
+ -Ot -Om
+So try backing off on optimization if you see such a problem. (Are there
+several different releases all numbered "4.5"??)
+
+
+Microsoft Windows, Microsoft Visual C++:
+
+jconfig.vc should work OK with any Microsoft compiler for a 32-bit memory
+model. makefile.vc is intended for command-line use. (If you are using
+the Developer Studio environment, you may prefer the DevStudio project
+files; see below.)
+
+Some users feel that it's easier to call the library from C++ code if you
+force VC++ to treat the library as C++ code, which you can do by renaming
+all the *.c files to *.cpp (and adjusting the makefile to match). This
+avoids the need to put extern "C" { ... } around #include "jpeglib.h" in
+your C++ application.
+
+
+Microsoft Windows, Microsoft Developer Studio:
+
+We include makefiles that should work as project files in DevStudio 4.2 or
+later. There is a library makefile that builds the IJG library as a static
+Win32 library, and an application makefile that builds the sample applications
+as Win32 console applications. (Even if you only want the library, we
+recommend building the applications so that you can run the self-test.)
+
+To use:
+1. Copy jconfig.vc to jconfig.h, makelib.ds to jpeg.mak, and
+ makeapps.ds to apps.mak. (Note that the renaming is critical!)
+2. Click on the .mak files to construct project workspaces.
+ (If you are using DevStudio more recent than 4.2, you'll probably
+ get a message saying that the makefiles are being updated.)
+3. Build the library project, then the applications project.
+4. Move the application .exe files from `app`\Release to an
+ appropriate location on your path.
+5. To perform the self-test, execute the command line
+ NMAKE /f makefile.vc test
+
+
+OS/2, Borland C++:
+
+Watch out for optimization bugs in older Borland compilers; you may need
+to back off the optimization switch settings. See the comments in
+makefile.bcc.
+
+
+SGI:
+
+On some SGI systems, you may need to set "AR2= ar -ts" in the Makefile.
+If you are using configure, you can do this by saying
+ ./configure RANLIB='ar -ts'
+This change is not needed on all SGIs. Use it only if the make fails at the
+stage of linking the completed programs.
+
+On the MIPS R4000 architecture (Indy, etc.), the compiler option "-mips2"
+reportedly speeds up the float DCT method substantially, enough to make it
+faster than the default int method (but still slower than the fast int
+method). If you use -mips2, you may want to alter the default DCT method to
+be float. To do this, put "#define JDCT_DEFAULT JDCT_FLOAT" in jconfig.h.
+
+
+VMS:
+
+On an Alpha/VMS system with MMS, be sure to use the "/Marco=Alpha=1"
+qualifier with MMS when building the JPEG package.
+
+VAX/VMS v5.5-1 may have problems with the test step of the build procedure
+reporting differences when it compares the original and test images. If the
+error points to the last block of the files, it is most likely bogus and may
+be safely ignored. It seems to be because the files are Stream_LF and
+Backup/Compare has difficulty with the (presumably) null padded files.
+This problem was not observed on VAX/VMS v6.1 or AXP/VMS v6.1.
diff --git a/test/monniaux/jpeg-6b/jcapimin.c b/test/monniaux/jpeg-6b/jcapimin.c
new file mode 100644
index 00000000..54fb8c58
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcapimin.c
@@ -0,0 +1,280 @@
+/*
+ * jcapimin.c
+ *
+ * Copyright (C) 1994-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the compression half
+ * of the JPEG library. These are the "minimum" API routines that may be
+ * needed in either the normal full-compression case or the transcoding-only
+ * case.
+ *
+ * Most of the routines intended to be called directly by an application
+ * are in this file or in jcapistd.c. But also see jcparam.c for
+ * parameter-setup helper routines, jcomapi.c for routines shared by
+ * compression and decompression, and jctrans.c for the transcoding case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG compression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL(void)
+jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
+{
+ int i;
+
+ /* Guard against version mismatches between library and caller. */
+ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
+ if (version != JPEG_LIB_VERSION)
+ ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
+ if (structsize != SIZEOF(struct jpeg_compress_struct))
+ ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
+ (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
+
+ /* For debugging purposes, we zero the whole master structure.
+ * But the application has already set the err pointer, and may have set
+ * client_data, so we have to save and restore those fields.
+ * Note: if application hasn't set client_data, tools like Purify may
+ * complain here.
+ */
+ {
+ struct jpeg_error_mgr * err = cinfo->err;
+ void * client_data = cinfo->client_data; /* ignore Purify complaint here */
+ MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
+ cinfo->err = err;
+ cinfo->client_data = client_data;
+ }
+ cinfo->is_decompressor = FALSE;
+
+ /* Initialize a memory manager instance for this object */
+ jinit_memory_mgr((j_common_ptr) cinfo);
+
+ /* Zero out pointers to permanent structures. */
+ cinfo->progress = NULL;
+ cinfo->dest = NULL;
+
+ cinfo->comp_info = NULL;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++)
+ cinfo->quant_tbl_ptrs[i] = NULL;
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ cinfo->dc_huff_tbl_ptrs[i] = NULL;
+ cinfo->ac_huff_tbl_ptrs[i] = NULL;
+ }
+
+ cinfo->script_space = NULL;
+
+ cinfo->input_gamma = 1.0; /* in case application forgets */
+
+ /* OK, I'm ready */
+ cinfo->global_state = CSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG compression object
+ */
+
+GLOBAL(void)
+jpeg_destroy_compress (j_compress_ptr cinfo)
+{
+ jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Abort processing of a JPEG compression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL(void)
+jpeg_abort_compress (j_compress_ptr cinfo)
+{
+ jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Forcibly suppress or un-suppress all quantization and Huffman tables.
+ * Marks all currently defined tables as already written (if suppress)
+ * or not written (if !suppress). This will control whether they get emitted
+ * by a subsequent jpeg_start_compress call.
+ *
+ * This routine is exported for use by applications that want to produce
+ * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
+ * since it is called by jpeg_start_compress, we put it here --- otherwise
+ * jcparam.o would be linked whether the application used it or not.
+ */
+
+GLOBAL(void)
+jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
+{
+ int i;
+ JQUANT_TBL * qtbl;
+ JHUFF_TBL * htbl;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
+ qtbl->sent_table = suppress;
+ }
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
+ htbl->sent_table = suppress;
+ if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
+ htbl->sent_table = suppress;
+ }
+}
+
+
+/*
+ * Finish JPEG compression.
+ *
+ * If a multipass operating mode was selected, this may do a great deal of
+ * work including most of the actual output.
+ */
+
+GLOBAL(void)
+jpeg_finish_compress (j_compress_ptr cinfo)
+{
+ JDIMENSION iMCU_row;
+
+ if (cinfo->global_state == CSTATE_SCANNING ||
+ cinfo->global_state == CSTATE_RAW_OK) {
+ /* Terminate first pass */
+ if (cinfo->next_scanline < cinfo->image_height)
+ ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+ (*cinfo->master->finish_pass) (cinfo);
+ } else if (cinfo->global_state != CSTATE_WRCOEFS)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Perform any remaining passes */
+ while (! cinfo->master->is_last_pass) {
+ (*cinfo->master->prepare_for_pass) (cinfo);
+ for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) iMCU_row;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+ /* We bypass the main controller and invoke coef controller directly;
+ * all work is being done from the coefficient buffer.
+ */
+ if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+ }
+ (*cinfo->master->finish_pass) (cinfo);
+ }
+ /* Write EOI, do final cleanup */
+ (*cinfo->marker->write_file_trailer) (cinfo);
+ (*cinfo->dest->term_destination) (cinfo);
+ /* We can use jpeg_abort to release memory and reset global_state */
+ jpeg_abort((j_common_ptr) cinfo);
+}
+
+
+/*
+ * Write a special marker.
+ * This is only recommended for writing COM or APPn markers.
+ * Must be called after jpeg_start_compress() and before
+ * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
+ */
+
+GLOBAL(void)
+jpeg_write_marker (j_compress_ptr cinfo, int marker,
+ const JOCTET *dataptr, unsigned int datalen)
+{
+ JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
+
+ if (cinfo->next_scanline != 0 ||
+ (cinfo->global_state != CSTATE_SCANNING &&
+ cinfo->global_state != CSTATE_RAW_OK &&
+ cinfo->global_state != CSTATE_WRCOEFS))
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
+ write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
+ while (datalen--) {
+ (*write_marker_byte) (cinfo, *dataptr);
+ dataptr++;
+ }
+}
+
+/* Same, but piecemeal. */
+
+GLOBAL(void)
+jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
+{
+ if (cinfo->next_scanline != 0 ||
+ (cinfo->global_state != CSTATE_SCANNING &&
+ cinfo->global_state != CSTATE_RAW_OK &&
+ cinfo->global_state != CSTATE_WRCOEFS))
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
+}
+
+GLOBAL(void)
+jpeg_write_m_byte (j_compress_ptr cinfo, int val)
+{
+ (*cinfo->marker->write_marker_byte) (cinfo, val);
+}
+
+
+/*
+ * Alternate compression function: just write an abbreviated table file.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * To produce a pair of files containing abbreviated tables and abbreviated
+ * image data, one would proceed as follows:
+ *
+ * initialize JPEG object
+ * set JPEG parameters
+ * set destination to table file
+ * jpeg_write_tables(cinfo);
+ * set destination to image file
+ * jpeg_start_compress(cinfo, FALSE);
+ * write data...
+ * jpeg_finish_compress(cinfo);
+ *
+ * jpeg_write_tables has the side effect of marking all tables written
+ * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
+ * will not re-emit the tables unless it is passed write_all_tables=TRUE.
+ */
+
+GLOBAL(void)
+jpeg_write_tables (j_compress_ptr cinfo)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Initialize the marker writer ... bit of a crock to do it here. */
+ jinit_marker_writer(cinfo);
+ /* Write them tables! */
+ (*cinfo->marker->write_tables_only) (cinfo);
+ /* And clean up. */
+ (*cinfo->dest->term_destination) (cinfo);
+ /*
+ * In library releases up through v6a, we called jpeg_abort() here to free
+ * any working memory allocated by the destination manager and marker
+ * writer. Some applications had a problem with that: they allocated space
+ * of their own from the library memory manager, and didn't want it to go
+ * away during write_tables. So now we do nothing. This will cause a
+ * memory leak if an app calls write_tables repeatedly without doing a full
+ * compression cycle or otherwise resetting the JPEG object. However, that
+ * seems less bad than unexpectedly freeing memory in the normal case.
+ * An app that prefers the old behavior can call jpeg_abort for itself after
+ * each call to jpeg_write_tables().
+ */
+}
diff --git a/test/monniaux/jpeg-6b/jcapistd.c b/test/monniaux/jpeg-6b/jcapistd.c
new file mode 100644
index 00000000..c0320b1b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcapistd.c
@@ -0,0 +1,161 @@
+/*
+ * jcapistd.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the compression half
+ * of the JPEG library. These are the "standard" API routines that are
+ * used in the normal full-compression case. They are not used by a
+ * transcoding-only application. Note that if an application links in
+ * jpeg_start_compress, it will end up linking in the entire compressor.
+ * We thus must separate this file from jcapimin.c to avoid linking the
+ * whole compression library into a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Compression initialization.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * We require a write_all_tables parameter as a failsafe check when writing
+ * multiple datastreams from the same compression object. Since prior runs
+ * will have left all the tables marked sent_table=TRUE, a subsequent run
+ * would emit an abbreviated stream (no tables) by default. This may be what
+ * is wanted, but for safety's sake it should not be the default behavior:
+ * programmers should have to make a deliberate choice to emit abbreviated
+ * images. Therefore the documentation and examples should encourage people
+ * to pass write_all_tables=TRUE; then it will take active thought to do the
+ * wrong thing.
+ */
+
+GLOBAL(void)
+jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (write_all_tables)
+ jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
+
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Perform master selection of active modules */
+ jinit_compress_master(cinfo);
+ /* Set up for the first pass */
+ (*cinfo->master->prepare_for_pass) (cinfo);
+ /* Ready for application to drive first pass through jpeg_write_scanlines
+ * or jpeg_write_raw_data.
+ */
+ cinfo->next_scanline = 0;
+ cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
+}
+
+
+/*
+ * Write some scanlines of data to the JPEG compressor.
+ *
+ * The return value will be the number of lines actually written.
+ * This should be less than the supplied num_lines only in case that
+ * the data destination module has requested suspension of the compressor,
+ * or if more than image_height scanlines are passed in.
+ *
+ * Note: we warn about excess calls to jpeg_write_scanlines() since
+ * this likely signals an application programmer error. However,
+ * excess scanlines passed in the last valid call are *silently* ignored,
+ * so that the application need not adjust num_lines for end-of-image
+ * when using a multiple-scanline buffer.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
+ JDIMENSION num_lines)
+{
+ JDIMENSION row_ctr, rows_left;
+
+ if (cinfo->global_state != CSTATE_SCANNING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->next_scanline >= cinfo->image_height)
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->image_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Give master control module another chance if this is first call to
+ * jpeg_write_scanlines. This lets output of the frame/scan headers be
+ * delayed so that application can write COM, etc, markers between
+ * jpeg_start_compress and jpeg_write_scanlines.
+ */
+ if (cinfo->master->call_pass_startup)
+ (*cinfo->master->pass_startup) (cinfo);
+
+ /* Ignore any extra scanlines at bottom of image. */
+ rows_left = cinfo->image_height - cinfo->next_scanline;
+ if (num_lines > rows_left)
+ num_lines = rows_left;
+
+ row_ctr = 0;
+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
+ cinfo->next_scanline += row_ctr;
+ return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to write raw data.
+ * Processes exactly one iMCU row per call, unless suspended.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
+ JDIMENSION num_lines)
+{
+ JDIMENSION lines_per_iMCU_row;
+
+ if (cinfo->global_state != CSTATE_RAW_OK)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->next_scanline >= cinfo->image_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->image_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Give master control module another chance if this is first call to
+ * jpeg_write_raw_data. This lets output of the frame/scan headers be
+ * delayed so that application can write COM, etc, markers between
+ * jpeg_start_compress and jpeg_write_raw_data.
+ */
+ if (cinfo->master->call_pass_startup)
+ (*cinfo->master->pass_startup) (cinfo);
+
+ /* Verify that at least one iMCU row has been passed. */
+ lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
+ if (num_lines < lines_per_iMCU_row)
+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+ /* Directly compress the row. */
+ if (! (*cinfo->coef->compress_data) (cinfo, data)) {
+ /* If compressor did not consume the whole row, suspend processing. */
+ return 0;
+ }
+
+ /* OK, we processed one iMCU row. */
+ cinfo->next_scanline += lines_per_iMCU_row;
+ return lines_per_iMCU_row;
+}
diff --git a/test/monniaux/jpeg-6b/jccoefct.c b/test/monniaux/jpeg-6b/jccoefct.c
new file mode 100644
index 00000000..1963ddb6
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jccoefct.c
@@ -0,0 +1,449 @@
+/*
+ * jccoefct.c
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the coefficient buffer controller for compression.
+ * This controller is the top level of the JPEG compressor proper.
+ * The coefficient buffer lies between forward-DCT and entropy encoding steps.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* We use a full-image coefficient buffer when doing Huffman optimization,
+ * and also for writing multiple-scan JPEG files. In all cases, the DCT
+ * step is run during the first pass, and subsequent passes need only read
+ * the buffered coefficients.
+ */
+#ifdef ENTROPY_OPT_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#else
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#endif
+#endif
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_coef_controller pub; /* public fields */
+
+ JDIMENSION iMCU_row_num; /* iMCU row # within image */
+ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* For single-pass compression, it's sufficient to buffer just one MCU
+ * (although this may prove a bit slow in practice). We allocate a
+ * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
+ * MCU constructed and sent. (On 80x86, the workspace is FAR even though
+ * it's not really very big; this is to keep the module interfaces unchanged
+ * when a large coefficient buffer is necessary.)
+ * In multi-pass modes, this array points to the current MCU's blocks
+ * within the virtual arrays.
+ */
+ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
+
+ /* In multi-pass modes, we need a virtual block array for each component. */
+ jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+/* Forward declarations */
+METHODDEF(boolean) compress_data
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+METHODDEF(boolean) compress_first_pass
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+METHODDEF(boolean) compress_output
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+#endif
+
+
+LOCAL(void)
+start_iMCU_row (j_compress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->mcu_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ coef->iMCU_row_num = 0;
+ start_iMCU_row(cinfo);
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (coef->whole_image[0] != NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_data;
+ break;
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+ case JBUF_SAVE_AND_PASS:
+ if (coef->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_first_pass;
+ break;
+ case JBUF_CRANK_DEST:
+ if (coef->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_output;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data in the single-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the image.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf contains a plane for each component in image,
+ * which we index according to the component's SOF position.
+ */
+
+METHODDEF(boolean)
+compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, bi, ci, yindex, yoffset, blockcnt;
+ JDIMENSION ypos, xpos;
+ jpeg_component_info *compptr;
+
+ /* Loop to write as much as one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
+ MCU_col_num++) {
+ /* Determine where data comes from in input_buf and do the DCT thing.
+ * Each call on forward_DCT processes a horizontal row of DCT blocks
+ * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
+ * sequentially. Dummy blocks at the right or bottom edge are filled in
+ * specially. The data in them does not matter for image reconstruction,
+ * so we fill them with values that will encode to the smallest amount of
+ * data, viz: all zeroes in the AC entries, DC entries equal to previous
+ * block's DC value. (Thanks to Thomas Kinsman for this idea.)
+ */
+ blkn = 0;
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ xpos = MCU_col_num * compptr->MCU_sample_width;
+ ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (coef->iMCU_row_num < last_iMCU_row ||
+ yoffset+yindex < compptr->last_row_height) {
+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+ input_buf[compptr->component_index],
+ coef->MCU_buffer[blkn],
+ ypos, xpos, (JDIMENSION) blockcnt);
+ if (blockcnt < compptr->MCU_width) {
+ /* Create some dummy blocks at the right edge of the image. */
+ jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
+ (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
+ for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
+ }
+ }
+ } else {
+ /* Create a row of dummy blocks at the bottom of the image. */
+ jzero_far((void FAR *) coef->MCU_buffer[blkn],
+ compptr->MCU_width * SIZEOF(JBLOCK));
+ for (bi = 0; bi < compptr->MCU_width; bi++) {
+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
+ }
+ }
+ blkn += compptr->MCU_width;
+ ypos += DCTSIZE;
+ }
+ }
+ /* Try to write the MCU. In event of a suspension failure, we will
+ * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
+ */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+
+/*
+ * Process some data in the first pass of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the image.
+ * This amount of data is read from the source buffer, DCT'd and quantized,
+ * and saved into the virtual arrays. We also generate suitable dummy blocks
+ * as needed at the right and lower edges. (The dummy blocks are constructed
+ * in the virtual arrays, which have been padded appropriately.) This makes
+ * it possible for subsequent passes not to worry about real vs. dummy blocks.
+ *
+ * We must also emit the data to the entropy encoder. This is conveniently
+ * done by calling compress_output() after we've loaded the current strip
+ * of the virtual arrays.
+ *
+ * NB: input_buf contains a plane for each component in image. All
+ * components are DCT'd and loaded into the virtual arrays in this pass.
+ * However, it may be that only a subset of the components are emitted to
+ * the entropy encoder during this first pass; be careful about looking
+ * at the scan-dependent variables (MCU dimensions, etc).
+ */
+
+METHODDEF(boolean)
+compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION blocks_across, MCUs_across, MCUindex;
+ int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
+ JCOEF lastDC;
+ jpeg_component_info *compptr;
+ JBLOCKARRAY buffer;
+ JBLOCKROW thisblockrow, lastblockrow;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Align the virtual buffer for this component. */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (coef->iMCU_row_num < last_iMCU_row)
+ block_rows = compptr->v_samp_factor;
+ else {
+ /* NB: can't use last_row_height here, since may not be set! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ }
+ blocks_across = compptr->width_in_blocks;
+ h_samp_factor = compptr->h_samp_factor;
+ /* Count number of dummy blocks to be added at the right margin. */
+ ndummy = (int) (blocks_across % h_samp_factor);
+ if (ndummy > 0)
+ ndummy = h_samp_factor - ndummy;
+ /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
+ * on forward_DCT processes a complete horizontal row of DCT blocks.
+ */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ thisblockrow = buffer[block_row];
+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+ input_buf[ci], thisblockrow,
+ (JDIMENSION) (block_row * DCTSIZE),
+ (JDIMENSION) 0, blocks_across);
+ if (ndummy > 0) {
+ /* Create dummy blocks at the right edge of the image. */
+ thisblockrow += blocks_across; /* => first dummy block */
+ jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
+ lastDC = thisblockrow[-1][0];
+ for (bi = 0; bi < ndummy; bi++) {
+ thisblockrow[bi][0] = lastDC;
+ }
+ }
+ }
+ /* If at end of image, create dummy block rows as needed.
+ * The tricky part here is that within each MCU, we want the DC values
+ * of the dummy blocks to match the last real block's DC value.
+ * This squeezes a few more bytes out of the resulting file...
+ */
+ if (coef->iMCU_row_num == last_iMCU_row) {
+ blocks_across += ndummy; /* include lower right corner */
+ MCUs_across = blocks_across / h_samp_factor;
+ for (block_row = block_rows; block_row < compptr->v_samp_factor;
+ block_row++) {
+ thisblockrow = buffer[block_row];
+ lastblockrow = buffer[block_row-1];
+ jzero_far((void FAR *) thisblockrow,
+ (size_t) (blocks_across * SIZEOF(JBLOCK)));
+ for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
+ lastDC = lastblockrow[h_samp_factor-1][0];
+ for (bi = 0; bi < h_samp_factor; bi++) {
+ thisblockrow[bi][0] = lastDC;
+ }
+ thisblockrow += h_samp_factor; /* advance to next MCU in row */
+ lastblockrow += h_samp_factor;
+ }
+ }
+ }
+ }
+ /* NB: compress_output will increment iMCU_row_num if successful.
+ * A suspension return will result in redoing all the work above next time.
+ */
+
+ /* Emit data to the entropy encoder, sharing code with subsequent passes */
+ return compress_output(cinfo, input_buf);
+}
+
+
+/*
+ * Process some data in subsequent passes of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * The data is obtained from the virtual arrays and fed to the entropy coder.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
+ */
+
+METHODDEF(boolean)
+compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ int blkn, ci, xindex, yindex, yoffset;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan.
+ * NB: during first pass, this is safe only because the buffers will
+ * already be aligned properly, so jmemmgr.c won't need to do any I/O.
+ */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+ coef->MCU_buffer[blkn++] = buffer_ptr++;
+ }
+ }
+ }
+ /* Try to write the MCU. */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+#endif /* FULL_COEF_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL(void)
+jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_coef_ptr coef;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_c_coef_controller *) coef;
+ coef->pub.start_pass = start_pass_coef;
+
+ /* Create the coefficient buffer. */
+ if (need_full_buffer) {
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+ /* Allocate a full-image virtual array for each component, */
+ /* padded to a multiple of samp_factor DCT blocks in each direction. */
+ int ci;
+ jpeg_component_info *compptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) compptr->v_samp_factor);
+ }
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ } else {
+ /* We only need a single-MCU buffer. */
+ JBLOCKROW buffer;
+ int i;
+
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
+ coef->MCU_buffer[i] = buffer + i;
+ }
+ coef->whole_image[0] = NULL; /* flag for no virtual arrays */
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jccolor.c b/test/monniaux/jpeg-6b/jccolor.c
new file mode 100644
index 00000000..0a8a4b5d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jccolor.c
@@ -0,0 +1,459 @@
+/*
+ * jccolor.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains input colorspace conversion routines.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_color_converter pub; /* public fields */
+
+ /* Private state for RGB->YCC conversion */
+ INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
+} my_color_converter;
+
+typedef my_color_converter * my_cconvert_ptr;
+
+
+/**************** RGB -> YCbCr conversion: most common case **************/
+
+/*
+ * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
+ * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * The conversion equations to be implemented are therefore
+ * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
+ * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
+ * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
+ * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
+ * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
+ * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
+ * were not represented exactly. Now we sacrifice exact representation of
+ * maximum red and maximum blue in order to get exact grayscales.
+ *
+ * To avoid floating-point arithmetic, we represent the fractional constants
+ * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
+ * the products by 2^16, with appropriate rounding, to get the correct answer.
+ *
+ * For even more speed, we avoid doing any multiplications in the inner loop
+ * by precalculating the constants times R,G,B for all possible values.
+ * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
+ * for 12-bit samples it is still acceptable. It's not very reasonable for
+ * 16-bit samples, but if you want lossless storage you shouldn't be changing
+ * colorspace anyway.
+ * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
+ * in the tables to save adding them separately in the inner loop.
+ */
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+/* We allocate one big table and divide it up into eight parts, instead of
+ * doing eight alloc_small requests. This lets us use a single table base
+ * address, which can be held in a register in the inner loops on many
+ * machines (more than can hold all eight addresses, anyway).
+ */
+
+#define R_Y_OFF 0 /* offset to R => Y section */
+#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
+#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
+#define R_CB_OFF (3*(MAXJSAMPLE+1))
+#define G_CB_OFF (4*(MAXJSAMPLE+1))
+#define B_CB_OFF (5*(MAXJSAMPLE+1))
+#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
+#define G_CR_OFF (6*(MAXJSAMPLE+1))
+#define B_CR_OFF (7*(MAXJSAMPLE+1))
+#define TABLE_SIZE (8*(MAXJSAMPLE+1))
+
+
+/*
+ * Initialize for RGB->YCC colorspace conversion.
+ */
+
+METHODDEF(void)
+rgb_ycc_start (j_compress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ INT32 * rgb_ycc_tab;
+ INT32 i;
+
+ /* Allocate and fill in the conversion tables. */
+ cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (TABLE_SIZE * SIZEOF(INT32)));
+
+ for (i = 0; i <= MAXJSAMPLE; i++) {
+ rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
+ rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
+ rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
+ rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
+ rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
+ /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
+ * This ensures that the maximum output will round to MAXJSAMPLE
+ * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
+ */
+ rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
+/* B=>Cb and R=>Cr tables are the same
+ rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
+*/
+ rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
+ rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ *
+ * Note that we change from the application's interleaved-pixel format
+ * to our internal noninterleaved, one-plane-per-component format.
+ * The input buffer is therefore three times as wide as the output buffer.
+ *
+ * A starting row offset is provided only for the output buffer. The caller
+ * can easily adjust the passed input_buf value to accommodate any row
+ * offset required on that side.
+ */
+
+METHODDEF(void)
+rgb_ycc_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr0, outptr1, outptr2;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr0 = output_buf[0][output_row];
+ outptr1 = output_buf[1][output_row];
+ outptr2 = output_buf[2][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = GETJSAMPLE(inptr[RGB_RED]);
+ g = GETJSAMPLE(inptr[RGB_GREEN]);
+ b = GETJSAMPLE(inptr[RGB_BLUE]);
+ inptr += RGB_PIXELSIZE;
+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
+ * must be too; we do not need an explicit range-limiting operation.
+ * Hence the value being shifted is never negative, and we don't
+ * need the general RIGHT_SHIFT macro.
+ */
+ /* Y */
+ outptr0[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ /* Cb */
+ outptr1[col] = (JSAMPLE)
+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
+ >> SCALEBITS);
+ /* Cr */
+ outptr2[col] = (JSAMPLE)
+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/**************** Cases other than RGB -> YCbCr **************/
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles RGB->grayscale conversion, which is the same
+ * as the RGB->Y portion of RGB->YCbCr.
+ * We assume rgb_ycc_start has been called (we only use the Y tables).
+ */
+
+METHODDEF(void)
+rgb_gray_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr = output_buf[0][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = GETJSAMPLE(inptr[RGB_RED]);
+ g = GETJSAMPLE(inptr[RGB_GREEN]);
+ b = GETJSAMPLE(inptr[RGB_BLUE]);
+ inptr += RGB_PIXELSIZE;
+ /* Y */
+ outptr[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles Adobe-style CMYK->YCCK conversion,
+ * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume rgb_ycc_start has been called.
+ */
+
+METHODDEF(void)
+cmyk_ycck_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr0, outptr1, outptr2, outptr3;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr0 = output_buf[0][output_row];
+ outptr1 = output_buf[1][output_row];
+ outptr2 = output_buf[2][output_row];
+ outptr3 = output_buf[3][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
+ g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
+ b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
+ /* K passes through as-is */
+ outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
+ inptr += 4;
+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
+ * must be too; we do not need an explicit range-limiting operation.
+ * Hence the value being shifted is never negative, and we don't
+ * need the general RIGHT_SHIFT macro.
+ */
+ /* Y */
+ outptr0[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ /* Cb */
+ outptr1[col] = (JSAMPLE)
+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
+ >> SCALEBITS);
+ /* Cr */
+ outptr2[col] = (JSAMPLE)
+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles grayscale output with no conversion.
+ * The source can be either plain grayscale or YCbCr (since Y == gray).
+ */
+
+METHODDEF(void)
+grayscale_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+ int instride = cinfo->input_components;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr = output_buf[0][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
+ inptr += instride;
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles multi-component colorspaces without conversion.
+ * We assume input_components == num_components.
+ */
+
+METHODDEF(void)
+null_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ register int ci;
+ int nc = cinfo->num_components;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ /* It seems fastest to make a separate pass for each component. */
+ for (ci = 0; ci < nc; ci++) {
+ inptr = *input_buf;
+ outptr = output_buf[ci][output_row];
+ for (col = 0; col < num_cols; col++) {
+ outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
+ inptr += nc;
+ }
+ }
+ input_buf++;
+ output_row++;
+ }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF(void)
+null_method (j_compress_ptr cinfo)
+{
+ /* no work needed */
+}
+
+
+/*
+ * Module initialization routine for input colorspace conversion.
+ */
+
+GLOBAL(void)
+jinit_color_converter (j_compress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert;
+
+ cconvert = (my_cconvert_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_color_converter));
+ cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
+ /* set start_pass to null method until we find out differently */
+ cconvert->pub.start_pass = null_method;
+
+ /* Make sure input_components agrees with in_color_space */
+ switch (cinfo->in_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->input_components != 1)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+ if (cinfo->input_components != RGB_PIXELSIZE)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+#endif /* else share code with YCbCr */
+
+ case JCS_YCbCr:
+ if (cinfo->input_components != 3)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ case JCS_CMYK:
+ case JCS_YCCK:
+ if (cinfo->input_components != 4)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ default: /* JCS_UNKNOWN can be anything */
+ if (cinfo->input_components < 1)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+ }
+
+ /* Check num_components, set conversion method based on requested space */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->num_components != 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_GRAYSCALE)
+ cconvert->pub.color_convert = grayscale_convert;
+ else if (cinfo->in_color_space == JCS_RGB) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = rgb_gray_convert;
+ } else if (cinfo->in_color_space == JCS_YCbCr)
+ cconvert->pub.color_convert = grayscale_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_RGB:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_YCbCr:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_RGB) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = rgb_ycc_convert;
+ } else if (cinfo->in_color_space == JCS_YCbCr)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_CMYK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_CMYK)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_YCCK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_CMYK) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = cmyk_ycck_convert;
+ } else if (cinfo->in_color_space == JCS_YCCK)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ default: /* allow null conversion of JCS_UNKNOWN */
+ if (cinfo->jpeg_color_space != cinfo->in_color_space ||
+ cinfo->num_components != cinfo->input_components)
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ cconvert->pub.color_convert = null_convert;
+ break;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jcdctmgr.c b/test/monniaux/jpeg-6b/jcdctmgr.c
new file mode 100644
index 00000000..61fa79b9
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcdctmgr.c
@@ -0,0 +1,387 @@
+/*
+ * jcdctmgr.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the forward-DCT management logic.
+ * This code selects a particular DCT implementation to be used,
+ * and it performs related housekeeping chores including coefficient
+ * quantization.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+ struct jpeg_forward_dct pub; /* public fields */
+
+ /* Pointer to the DCT routine actually in use */
+ forward_DCT_method_ptr do_dct;
+
+ /* The actual post-DCT divisors --- not identical to the quant table
+ * entries, because of scaling (especially for an unnormalized DCT).
+ * Each table is given in normal array order.
+ */
+ DCTELEM * divisors[NUM_QUANT_TBLS];
+
+#ifdef DCT_FLOAT_SUPPORTED
+ /* Same as above for the floating-point case. */
+ float_DCT_method_ptr do_float_dct;
+ FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
+#endif
+} my_fdct_controller;
+
+typedef my_fdct_controller * my_fdct_ptr;
+
+
+/*
+ * Initialize for a processing pass.
+ * Verify that all referenced Q-tables are present, and set up
+ * the divisor table for each one.
+ * In the current implementation, DCT of all components is done during
+ * the first pass, even if only some components will be output in the
+ * first scan. Hence all components should be examined here.
+ */
+
+METHODDEF(void)
+start_pass_fdctmgr (j_compress_ptr cinfo)
+{
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ int ci, qtblno, i;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtbl;
+ DCTELEM * dtbl;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ qtblno = compptr->quant_tbl_no;
+ /* Make sure specified quantization table is present */
+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+ qtbl = cinfo->quant_tbl_ptrs[qtblno];
+ /* Compute divisors for this quant table */
+ /* We may do this more than once for same table, but it's not a big deal */
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ /* For LL&M IDCT method, divisors are equal to raw quantization
+ * coefficients multiplied by 8 (to counteract scaling).
+ */
+ if (fdct->divisors[qtblno] == NULL) {
+ fdct->divisors[qtblno] = (DCTELEM *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(DCTELEM));
+ }
+ dtbl = fdct->divisors[qtblno];
+ for (i = 0; i < DCTSIZE2; i++) {
+ dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
+ }
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ {
+ /* For AA&N IDCT method, divisors are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * We apply a further scale factor of 8.
+ */
+#define CONST_BITS 14
+ static const INT16 aanscales[DCTSIZE2] = {
+ /* precomputed values scaled up by 14 bits */
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
+ };
+ SHIFT_TEMPS
+
+ if (fdct->divisors[qtblno] == NULL) {
+ fdct->divisors[qtblno] = (DCTELEM *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(DCTELEM));
+ }
+ dtbl = fdct->divisors[qtblno];
+ for (i = 0; i < DCTSIZE2; i++) {
+ dtbl[i] = (DCTELEM)
+ DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+ (INT32) aanscales[i]),
+ CONST_BITS-3);
+ }
+ }
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ {
+ /* For float AA&N IDCT method, divisors are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * We apply a further scale factor of 8.
+ * What's actually stored is 1/divisor so that the inner loop can
+ * use a multiplication rather than a division.
+ */
+ FAST_FLOAT * fdtbl;
+ int row, col;
+ static const double aanscalefactor[DCTSIZE] = {
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
+ 1.0, 0.785694958, 0.541196100, 0.275899379
+ };
+
+ if (fdct->float_divisors[qtblno] == NULL) {
+ fdct->float_divisors[qtblno] = (FAST_FLOAT *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(FAST_FLOAT));
+ }
+ fdtbl = fdct->float_divisors[qtblno];
+ i = 0;
+ for (row = 0; row < DCTSIZE; row++) {
+ for (col = 0; col < DCTSIZE; col++) {
+ fdtbl[i] = (FAST_FLOAT)
+ (1.0 / (((double) qtbl->quantval[i] *
+ aanscalefactor[row] * aanscalefactor[col] * 8.0)));
+ i++;
+ }
+ }
+ }
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ }
+}
+
+
+/*
+ * Perform forward DCT on one or more blocks of a component.
+ *
+ * The input samples are taken from the sample_data[] array starting at
+ * position start_row/start_col, and moving to the right for any additional
+ * blocks. The quantized coefficients are returned in coef_blocks[].
+ */
+
+METHODDEF(void)
+forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks)
+/* This version is used for integer DCT implementations. */
+{
+ /* This routine is heavily used, so it's worth coding it tightly. */
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ forward_DCT_method_ptr do_dct = fdct->do_dct;
+ DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
+ DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
+ JDIMENSION bi;
+
+ sample_data += start_row; /* fold in the vertical offset once */
+
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+ /* Load data into workspace, applying unsigned->signed conversion */
+ { register DCTELEM *workspaceptr;
+ register JSAMPROW elemptr;
+ register int elemr;
+
+ workspaceptr = workspace;
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
+ elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8 /* unroll the inner loop */
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+#else
+ { register int elemc;
+ for (elemc = DCTSIZE; elemc > 0; elemc--) {
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ }
+ }
+#endif
+ }
+ }
+
+ /* Perform the DCT */
+ (*do_dct) (workspace);
+
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
+ { register DCTELEM temp, qval;
+ register int i;
+ register JCOEFPTR output_ptr = coef_blocks[bi];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ qval = divisors[i];
+ temp = workspace[i];
+ /* Divide the coefficient value by qval, ensuring proper rounding.
+ * Since C does not specify the direction of rounding for negative
+ * quotients, we have to force the dividend positive for portability.
+ *
+ * In most files, at least half of the output values will be zero
+ * (at default quantization settings, more like three-quarters...)
+ * so we should ensure that this case is fast. On many machines,
+ * a comparison is enough cheaper than a divide to make a special test
+ * a win. Since both inputs will be nonnegative, we need only test
+ * for a < b to discover whether a/b is 0.
+ * If your machine's division is fast enough, define FAST_DIVIDE.
+ */
+#ifdef FAST_DIVIDE
+#define DIVIDE_BY(a,b) a /= b
+#else
+#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
+#endif
+ if (temp < 0) {
+ temp = -temp;
+ temp += qval>>1; /* for rounding */
+ DIVIDE_BY(temp, qval);
+ temp = -temp;
+ } else {
+ temp += qval>>1; /* for rounding */
+ DIVIDE_BY(temp, qval);
+ }
+ output_ptr[i] = (JCOEF) temp;
+ }
+ }
+ }
+}
+
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+METHODDEF(void)
+forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks)
+/* This version is used for floating-point DCT implementations. */
+{
+ /* This routine is heavily used, so it's worth coding it tightly. */
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ float_DCT_method_ptr do_dct = fdct->do_float_dct;
+ FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
+ FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
+ JDIMENSION bi;
+
+ sample_data += start_row; /* fold in the vertical offset once */
+
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+ /* Load data into workspace, applying unsigned->signed conversion */
+ { register FAST_FLOAT *workspaceptr;
+ register JSAMPROW elemptr;
+ register int elemr;
+
+ workspaceptr = workspace;
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
+ elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8 /* unroll the inner loop */
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+#else
+ { register int elemc;
+ for (elemc = DCTSIZE; elemc > 0; elemc--) {
+ *workspaceptr++ = (FAST_FLOAT)
+ (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ }
+ }
+#endif
+ }
+ }
+
+ /* Perform the DCT */
+ (*do_dct) (workspace);
+
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
+ { register FAST_FLOAT temp;
+ register int i;
+ register JCOEFPTR output_ptr = coef_blocks[bi];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ /* Apply the quantization and scaling factor */
+ temp = workspace[i] * divisors[i];
+ /* Round to nearest integer.
+ * Since C does not specify the direction of rounding for negative
+ * quotients, we have to force the dividend positive for portability.
+ * The maximum coefficient size is +-16K (for 12-bit data), so this
+ * code should work for either 16-bit or 32-bit ints.
+ */
+ output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
+ }
+ }
+ }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
+
+
+/*
+ * Initialize FDCT manager.
+ */
+
+GLOBAL(void)
+jinit_forward_dct (j_compress_ptr cinfo)
+{
+ my_fdct_ptr fdct;
+ int i;
+
+ fdct = (my_fdct_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_fdct_controller));
+ cinfo->fdct = (struct jpeg_forward_dct *) fdct;
+ fdct->pub.start_pass = start_pass_fdctmgr;
+
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ fdct->pub.forward_DCT = forward_DCT;
+ fdct->do_dct = jpeg_fdct_islow;
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ fdct->pub.forward_DCT = forward_DCT;
+ fdct->do_dct = jpeg_fdct_ifast;
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ fdct->pub.forward_DCT = forward_DCT_float;
+ fdct->do_float_dct = jpeg_fdct_float;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+
+ /* Mark divisor tables unallocated */
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ fdct->divisors[i] = NULL;
+#ifdef DCT_FLOAT_SUPPORTED
+ fdct->float_divisors[i] = NULL;
+#endif
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jchuff.c b/test/monniaux/jpeg-6b/jchuff.c
new file mode 100644
index 00000000..f2352505
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jchuff.c
@@ -0,0 +1,909 @@
+/*
+ * jchuff.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains Huffman entropy encoding routines.
+ *
+ * Much of the complexity here has to do with supporting output suspension.
+ * If the data destination module demands suspension, we want to be able to
+ * back up to the start of the current MCU. To do this, we copy state
+ * variables into local working storage, and update them back to the
+ * permanent JPEG objects only upon successful completion of an MCU.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jchuff.h" /* Declarations shared with jcphuff.c */
+
+
+/* Expanded entropy encoder object for Huffman encoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+ INT32 put_buffer; /* current bit-accumulation buffer */
+ int put_bits; /* # of bits now in it */
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+/* This macro is to work around compilers with missing or broken
+ * structure assignment. You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src) ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src) \
+ ((dest).put_buffer = (src).put_buffer, \
+ (dest).put_bits = (src).put_bits, \
+ (dest).last_dc_val[0] = (src).last_dc_val[0], \
+ (dest).last_dc_val[1] = (src).last_dc_val[1], \
+ (dest).last_dc_val[2] = (src).last_dc_val[2], \
+ (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+ struct jpeg_entropy_encoder pub; /* public fields */
+
+ savable_state saved; /* Bit buffer & DC state at start of MCU */
+
+ /* These fields are NOT loaded into local working state. */
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
+ int next_restart_num; /* next restart number to write (0-7) */
+
+ /* Pointers to derived tables (these workspaces have image lifespan) */
+ c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
+ c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
+
+#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
+ long * dc_count_ptrs[NUM_HUFF_TBLS];
+ long * ac_count_ptrs[NUM_HUFF_TBLS];
+#endif
+} huff_entropy_encoder;
+
+typedef huff_entropy_encoder * huff_entropy_ptr;
+
+/* Working state while writing an MCU.
+ * This struct contains all the fields that are needed by subroutines.
+ */
+
+typedef struct {
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+ savable_state cur; /* Current bit buffer & DC state */
+ j_compress_ptr cinfo; /* dump_buffer needs access to this */
+} working_state;
+
+
+/* Forward declarations */
+METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
+#ifdef ENTROPY_OPT_SUPPORTED
+METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
+#endif
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ * If gather_statistics is TRUE, we do not output anything during the scan,
+ * just count the Huffman symbols used and generate Huffman code tables.
+ */
+
+METHODDEF(void)
+start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, dctbl, actbl;
+ jpeg_component_info * compptr;
+
+ if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+ entropy->pub.encode_mcu = encode_mcu_gather;
+ entropy->pub.finish_pass = finish_pass_gather;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ entropy->pub.encode_mcu = encode_mcu_huff;
+ entropy->pub.finish_pass = finish_pass_huff;
+ }
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+ /* Check for invalid table indexes */
+ /* (make_c_derived_tbl does this in the other path) */
+ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
+ if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
+ /* Allocate and zero the statistics tables */
+ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
+ if (entropy->dc_count_ptrs[dctbl] == NULL)
+ entropy->dc_count_ptrs[dctbl] = (long *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 257 * SIZEOF(long));
+ MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
+ if (entropy->ac_count_ptrs[actbl] == NULL)
+ entropy->ac_count_ptrs[actbl] = (long *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 257 * SIZEOF(long));
+ MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
+#endif
+ } else {
+ /* Compute derived values for Huffman tables */
+ /* We may do this more than once for a table, but it's not expensive */
+ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
+ & entropy->dc_derived_tbls[dctbl]);
+ jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
+ & entropy->ac_derived_tbls[actbl]);
+ }
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Initialize bit buffer to empty */
+ entropy->saved.put_buffer = 0;
+ entropy->saved.put_bits = 0;
+
+ /* Initialize restart stuff */
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num = 0;
+}
+
+
+/*
+ * Compute the derived values for a Huffman table.
+ * This routine also performs some validation checks on the table.
+ *
+ * Note this is also used by jcphuff.c.
+ */
+
+GLOBAL(void)
+jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
+ c_derived_tbl ** pdtbl)
+{
+ JHUFF_TBL *htbl;
+ c_derived_tbl *dtbl;
+ int p, i, l, lastp, si, maxsymbol;
+ char huffsize[257];
+ unsigned int huffcode[257];
+ unsigned int code;
+
+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
+ * paralleling the order of the symbols themselves in htbl->huffval[].
+ */
+
+ /* Find the input Huffman table */
+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+ htbl =
+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+
+ /* Allocate a workspace if we haven't already done so. */
+ if (*pdtbl == NULL)
+ *pdtbl = (c_derived_tbl *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(c_derived_tbl));
+ dtbl = *pdtbl;
+
+ /* Figure C.1: make table of Huffman code length for each symbol */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ i = (int) htbl->bits[l];
+ if (i < 0 || p + i > 256) /* protect against table overrun */
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ while (i--)
+ huffsize[p++] = (char) l;
+ }
+ huffsize[p] = 0;
+ lastp = p;
+
+ /* Figure C.2: generate the codes themselves */
+ /* We also validate that the counts represent a legal Huffman code tree. */
+
+ code = 0;
+ si = huffsize[0];
+ p = 0;
+ while (huffsize[p]) {
+ while (((int) huffsize[p]) == si) {
+ huffcode[p++] = code;
+ code++;
+ }
+ /* code is now 1 more than the last code used for codelength si; but
+ * it must still fit in si bits, since no code is allowed to be all ones.
+ */
+ if (((INT32) code) >= (((INT32) 1) << si))
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ code <<= 1;
+ si++;
+ }
+
+ /* Figure C.3: generate encoding tables */
+ /* These are code and size indexed by symbol value */
+
+ /* Set all codeless symbols to have code length 0;
+ * this lets us detect duplicate VAL entries here, and later
+ * allows emit_bits to detect any attempt to emit such symbols.
+ */
+ MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
+
+ /* This is also a convenient place to check for out-of-range
+ * and duplicated VAL entries. We allow 0..255 for AC symbols
+ * but only 0..15 for DC. (We could constrain them further
+ * based on data depth and mode, but this seems enough.)
+ */
+ maxsymbol = isDC ? 15 : 255;
+
+ for (p = 0; p < lastp; p++) {
+ i = htbl->huffval[p];
+ if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ dtbl->ehufco[i] = huffcode[p];
+ dtbl->ehufsi[i] = huffsize[p];
+ }
+}
+
+
+/* Outputting bytes to the file */
+
+/* Emit a byte, taking 'action' if must suspend. */
+#define emit_byte(state,val,action) \
+ { *(state)->next_output_byte++ = (JOCTET) (val); \
+ if (--(state)->free_in_buffer == 0) \
+ if (! dump_buffer(state)) \
+ { action; } }
+
+
+LOCAL(boolean)
+dump_buffer (working_state * state)
+/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
+{
+ struct jpeg_destination_mgr * dest = state->cinfo->dest;
+
+ if (! (*dest->empty_output_buffer) (state->cinfo))
+ return FALSE;
+ /* After a successful buffer dump, must reset buffer pointers */
+ state->next_output_byte = dest->next_output_byte;
+ state->free_in_buffer = dest->free_in_buffer;
+ return TRUE;
+}
+
+
+/* Outputting bits to the file */
+
+/* Only the right 24 bits of put_buffer are used; the valid bits are
+ * left-justified in this part. At most 16 bits can be passed to emit_bits
+ * in one call, and we never retain more than 7 bits in put_buffer
+ * between calls, so 24 bits are sufficient.
+ */
+
+INLINE
+LOCAL(boolean)
+emit_bits (working_state * state, unsigned int code, int size)
+/* Emit some bits; return TRUE if successful, FALSE if must suspend */
+{
+ /* This routine is heavily used, so it's worth coding tightly. */
+ register INT32 put_buffer = (INT32) code;
+ register int put_bits = state->cur.put_bits;
+
+ /* if size is 0, caller used an invalid Huffman table entry */
+ if (size == 0)
+ ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
+
+ put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
+
+ put_bits += size; /* new number of bits in buffer */
+
+ put_buffer <<= 24 - put_bits; /* align incoming bits */
+
+ put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
+
+ while (put_bits >= 8) {
+ int c = (int) ((put_buffer >> 16) & 0xFF);
+
+ emit_byte(state, c, return FALSE);
+ if (c == 0xFF) { /* need to stuff a zero byte? */
+ emit_byte(state, 0, return FALSE);
+ }
+ put_buffer <<= 8;
+ put_bits -= 8;
+ }
+
+ state->cur.put_buffer = put_buffer; /* update state variables */
+ state->cur.put_bits = put_bits;
+
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+flush_bits (working_state * state)
+{
+ if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
+ return FALSE;
+ state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
+ state->cur.put_bits = 0;
+ return TRUE;
+}
+
+
+/* Encode a single block's worth of coefficients */
+
+LOCAL(boolean)
+encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
+ c_derived_tbl *dctbl, c_derived_tbl *actbl)
+{
+ register int temp, temp2;
+ register int nbits;
+ register int k, r, i;
+
+ /* Encode the DC coefficient difference per section F.1.2.1 */
+
+ temp = temp2 = block[0] - last_dc_val;
+
+ if (temp < 0) {
+ temp = -temp; /* temp is abs value of input */
+ /* For a negative input, want temp2 = bitwise complement of abs(input) */
+ /* This code assumes we are on a two's complement machine */
+ temp2--;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ while (temp) {
+ nbits++;
+ temp >>= 1;
+ }
+ /* Check for out-of-range coefficient values.
+ * Since we're encoding a difference, the range limit is twice as much.
+ */
+ if (nbits > MAX_COEF_BITS+1)
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
+
+ /* Emit the Huffman-coded symbol for the number of bits */
+ if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
+ return FALSE;
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ if (nbits) /* emit_bits rejects calls with size 0 */
+ if (! emit_bits(state, (unsigned int) temp2, nbits))
+ return FALSE;
+
+ /* Encode the AC coefficients per section F.1.2.2 */
+
+ r = 0; /* r = run length of zeros */
+
+ for (k = 1; k < DCTSIZE2; k++) {
+ if ((temp = block[jpeg_natural_order[k]]) == 0) {
+ r++;
+ } else {
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
+ return FALSE;
+ r -= 16;
+ }
+
+ temp2 = temp;
+ if (temp < 0) {
+ temp = -temp; /* temp is abs value of input */
+ /* This code assumes we are on a two's complement machine */
+ temp2--;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 1; /* there must be at least one 1 bit */
+ while ((temp >>= 1))
+ nbits++;
+ /* Check for out-of-range coefficient values */
+ if (nbits > MAX_COEF_BITS)
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
+
+ /* Emit Huffman symbol for run length / number of bits */
+ i = (r << 4) + nbits;
+ if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
+ return FALSE;
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ if (! emit_bits(state, (unsigned int) temp2, nbits))
+ return FALSE;
+
+ r = 0;
+ }
+ }
+
+ /* If the last coef(s) were zero, emit an end-of-block code */
+ if (r > 0)
+ if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
+ return FALSE;
+
+ return TRUE;
+}
+
+
+/*
+ * Emit a restart marker & resynchronize predictions.
+ */
+
+LOCAL(boolean)
+emit_restart (working_state * state, int restart_num)
+{
+ int ci;
+
+ if (! flush_bits(state))
+ return FALSE;
+
+ emit_byte(state, 0xFF, return FALSE);
+ emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
+
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
+ state->cur.last_dc_val[ci] = 0;
+
+ /* The restart counter is not updated until we successfully write the MCU. */
+
+ return TRUE;
+}
+
+
+/*
+ * Encode and output one MCU's worth of Huffman-compressed coefficients.
+ */
+
+METHODDEF(boolean)
+encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ working_state state;
+ int blkn, ci;
+ jpeg_component_info * compptr;
+
+ /* Load up working state */
+ state.next_output_byte = cinfo->dest->next_output_byte;
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
+ ASSIGN_STATE(state.cur, entropy->saved);
+ state.cinfo = cinfo;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! emit_restart(&state, entropy->next_restart_num))
+ return FALSE;
+ }
+
+ /* Encode the MCU data blocks */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ if (! encode_one_block(&state,
+ MCU_data[blkn][0], state.cur.last_dc_val[ci],
+ entropy->dc_derived_tbls[compptr->dc_tbl_no],
+ entropy->ac_derived_tbls[compptr->ac_tbl_no]))
+ return FALSE;
+ /* Update last_dc_val */
+ state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
+ }
+
+ /* Completed MCU, so update state */
+ cinfo->dest->next_output_byte = state.next_output_byte;
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
+ ASSIGN_STATE(entropy->saved, state.cur);
+
+ /* Update restart-interval state too */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num++;
+ entropy->next_restart_num &= 7;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * Finish up at the end of a Huffman-compressed scan.
+ */
+
+METHODDEF(void)
+finish_pass_huff (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ working_state state;
+
+ /* Load up working state ... flush_bits needs it */
+ state.next_output_byte = cinfo->dest->next_output_byte;
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
+ ASSIGN_STATE(state.cur, entropy->saved);
+ state.cinfo = cinfo;
+
+ /* Flush out the last data */
+ if (! flush_bits(&state))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+
+ /* Update state */
+ cinfo->dest->next_output_byte = state.next_output_byte;
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
+ ASSIGN_STATE(entropy->saved, state.cur);
+}
+
+
+/*
+ * Huffman coding optimization.
+ *
+ * We first scan the supplied data and count the number of uses of each symbol
+ * that is to be Huffman-coded. (This process MUST agree with the code above.)
+ * Then we build a Huffman coding tree for the observed counts.
+ * Symbols which are not needed at all for the particular image are not
+ * assigned any code, which saves space in the DHT marker as well as in
+ * the compressed data.
+ */
+
+#ifdef ENTROPY_OPT_SUPPORTED
+
+
+/* Process a single block's worth of coefficients */
+
+LOCAL(void)
+htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
+ long dc_counts[], long ac_counts[])
+{
+ register int temp;
+ register int nbits;
+ register int k, r;
+
+ /* Encode the DC coefficient difference per section F.1.2.1 */
+
+ temp = block[0] - last_dc_val;
+ if (temp < 0)
+ temp = -temp;
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ while (temp) {
+ nbits++;
+ temp >>= 1;
+ }
+ /* Check for out-of-range coefficient values.
+ * Since we're encoding a difference, the range limit is twice as much.
+ */
+ if (nbits > MAX_COEF_BITS+1)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+
+ /* Count the Huffman symbol for the number of bits */
+ dc_counts[nbits]++;
+
+ /* Encode the AC coefficients per section F.1.2.2 */
+
+ r = 0; /* r = run length of zeros */
+
+ for (k = 1; k < DCTSIZE2; k++) {
+ if ((temp = block[jpeg_natural_order[k]]) == 0) {
+ r++;
+ } else {
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ ac_counts[0xF0]++;
+ r -= 16;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ if (temp < 0)
+ temp = -temp;
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 1; /* there must be at least one 1 bit */
+ while ((temp >>= 1))
+ nbits++;
+ /* Check for out-of-range coefficient values */
+ if (nbits > MAX_COEF_BITS)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+
+ /* Count Huffman symbol for run length / number of bits */
+ ac_counts[(r << 4) + nbits]++;
+
+ r = 0;
+ }
+ }
+
+ /* If the last coef(s) were zero, emit an end-of-block code */
+ if (r > 0)
+ ac_counts[0]++;
+}
+
+
+/*
+ * Trial-encode one MCU's worth of Huffman-compressed coefficients.
+ * No data is actually output, so no suspension return is possible.
+ */
+
+METHODDEF(boolean)
+encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int blkn, ci;
+ jpeg_component_info * compptr;
+
+ /* Take care of restart intervals if needed */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++)
+ entropy->saved.last_dc_val[ci] = 0;
+ /* Update restart state */
+ entropy->restarts_to_go = cinfo->restart_interval;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
+ entropy->dc_count_ptrs[compptr->dc_tbl_no],
+ entropy->ac_count_ptrs[compptr->ac_tbl_no]);
+ entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * Generate the best Huffman code table for the given counts, fill htbl.
+ * Note this is also used by jcphuff.c.
+ *
+ * The JPEG standard requires that no symbol be assigned a codeword of all
+ * one bits (so that padding bits added at the end of a compressed segment
+ * can't look like a valid code). Because of the canonical ordering of
+ * codewords, this just means that there must be an unused slot in the
+ * longest codeword length category. Section K.2 of the JPEG spec suggests
+ * reserving such a slot by pretending that symbol 256 is a valid symbol
+ * with count 1. In theory that's not optimal; giving it count zero but
+ * including it in the symbol set anyway should give a better Huffman code.
+ * But the theoretically better code actually seems to come out worse in
+ * practice, because it produces more all-ones bytes (which incur stuffed
+ * zero bytes in the final file). In any case the difference is tiny.
+ *
+ * The JPEG standard requires Huffman codes to be no more than 16 bits long.
+ * If some symbols have a very small but nonzero probability, the Huffman tree
+ * must be adjusted to meet the code length restriction. We currently use
+ * the adjustment method suggested in JPEG section K.2. This method is *not*
+ * optimal; it may not choose the best possible limited-length code. But
+ * typically only very-low-frequency symbols will be given less-than-optimal
+ * lengths, so the code is almost optimal. Experimental comparisons against
+ * an optimal limited-length-code algorithm indicate that the difference is
+ * microscopic --- usually less than a hundredth of a percent of total size.
+ * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
+ */
+
+GLOBAL(void)
+jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
+{
+#define MAX_CLEN 32 /* assumed maximum initial code length */
+ UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
+ int codesize[257]; /* codesize[k] = code length of symbol k */
+ int others[257]; /* next symbol in current branch of tree */
+ int c1, c2;
+ int p, i, j;
+ long v;
+
+ /* This algorithm is explained in section K.2 of the JPEG standard */
+
+ MEMZERO(bits, SIZEOF(bits));
+ MEMZERO(codesize, SIZEOF(codesize));
+ for (i = 0; i < 257; i++)
+ others[i] = -1; /* init links to empty */
+
+ freq[256] = 1; /* make sure 256 has a nonzero count */
+ /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
+ * that no real symbol is given code-value of all ones, because 256
+ * will be placed last in the largest codeword category.
+ */
+
+ /* Huffman's basic algorithm to assign optimal code lengths to symbols */
+
+ for (;;) {
+ /* Find the smallest nonzero frequency, set c1 = its symbol */
+ /* In case of ties, take the larger symbol number */
+ c1 = -1;
+ v = 1000000000L;
+ for (i = 0; i <= 256; i++) {
+ if (freq[i] && freq[i] <= v) {
+ v = freq[i];
+ c1 = i;
+ }
+ }
+
+ /* Find the next smallest nonzero frequency, set c2 = its symbol */
+ /* In case of ties, take the larger symbol number */
+ c2 = -1;
+ v = 1000000000L;
+ for (i = 0; i <= 256; i++) {
+ if (freq[i] && freq[i] <= v && i != c1) {
+ v = freq[i];
+ c2 = i;
+ }
+ }
+
+ /* Done if we've merged everything into one frequency */
+ if (c2 < 0)
+ break;
+
+ /* Else merge the two counts/trees */
+ freq[c1] += freq[c2];
+ freq[c2] = 0;
+
+ /* Increment the codesize of everything in c1's tree branch */
+ codesize[c1]++;
+ while (others[c1] >= 0) {
+ c1 = others[c1];
+ codesize[c1]++;
+ }
+
+ others[c1] = c2; /* chain c2 onto c1's tree branch */
+
+ /* Increment the codesize of everything in c2's tree branch */
+ codesize[c2]++;
+ while (others[c2] >= 0) {
+ c2 = others[c2];
+ codesize[c2]++;
+ }
+ }
+
+ /* Now count the number of symbols of each code length */
+ for (i = 0; i <= 256; i++) {
+ if (codesize[i]) {
+ /* The JPEG standard seems to think that this can't happen, */
+ /* but I'm paranoid... */
+ if (codesize[i] > MAX_CLEN)
+ ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
+
+ bits[codesize[i]]++;
+ }
+ }
+
+ /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
+ * Huffman procedure assigned any such lengths, we must adjust the coding.
+ * Here is what the JPEG spec says about how this next bit works:
+ * Since symbols are paired for the longest Huffman code, the symbols are
+ * removed from this length category two at a time. The prefix for the pair
+ * (which is one bit shorter) is allocated to one of the pair; then,
+ * skipping the BITS entry for that prefix length, a code word from the next
+ * shortest nonzero BITS entry is converted into a prefix for two code words
+ * one bit longer.
+ */
+
+ for (i = MAX_CLEN; i > 16; i--) {
+ while (bits[i] > 0) {
+ j = i - 2; /* find length of new prefix to be used */
+ while (bits[j] == 0)
+ j--;
+
+ bits[i] -= 2; /* remove two symbols */
+ bits[i-1]++; /* one goes in this length */
+ bits[j+1] += 2; /* two new symbols in this length */
+ bits[j]--; /* symbol of this length is now a prefix */
+ }
+ }
+
+ /* Remove the count for the pseudo-symbol 256 from the largest codelength */
+ while (bits[i] == 0) /* find largest codelength still in use */
+ i--;
+ bits[i]--;
+
+ /* Return final symbol counts (only for lengths 0..16) */
+ MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
+
+ /* Return a list of the symbols sorted by code length */
+ /* It's not real clear to me why we don't need to consider the codelength
+ * changes made above, but the JPEG spec seems to think this works.
+ */
+ p = 0;
+ for (i = 1; i <= MAX_CLEN; i++) {
+ for (j = 0; j <= 255; j++) {
+ if (codesize[j] == i) {
+ htbl->huffval[p] = (UINT8) j;
+ p++;
+ }
+ }
+ }
+
+ /* Set sent_table FALSE so updated table will be written to JPEG file. */
+ htbl->sent_table = FALSE;
+}
+
+
+/*
+ * Finish up a statistics-gathering pass and create the new Huffman tables.
+ */
+
+METHODDEF(void)
+finish_pass_gather (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, dctbl, actbl;
+ jpeg_component_info * compptr;
+ JHUFF_TBL **htblptr;
+ boolean did_dc[NUM_HUFF_TBLS];
+ boolean did_ac[NUM_HUFF_TBLS];
+
+ /* It's important not to apply jpeg_gen_optimal_table more than once
+ * per table, because it clobbers the input frequency counts!
+ */
+ MEMZERO(did_dc, SIZEOF(did_dc));
+ MEMZERO(did_ac, SIZEOF(did_ac));
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ if (! did_dc[dctbl]) {
+ htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
+ did_dc[dctbl] = TRUE;
+ }
+ if (! did_ac[actbl]) {
+ htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
+ did_ac[actbl] = TRUE;
+ }
+ }
+}
+
+
+#endif /* ENTROPY_OPT_SUPPORTED */
+
+
+/*
+ * Module initialization routine for Huffman entropy encoding.
+ */
+
+GLOBAL(void)
+jinit_huff_encoder (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy;
+ int i;
+
+ entropy = (huff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(huff_entropy_encoder));
+ cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
+ entropy->pub.start_pass = start_pass_huff;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
+#ifdef ENTROPY_OPT_SUPPORTED
+ entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
+#endif
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jchuff.h b/test/monniaux/jpeg-6b/jchuff.h
new file mode 100644
index 00000000..a9599fc1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jchuff.h
@@ -0,0 +1,47 @@
+/*
+ * jchuff.h
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains declarations for Huffman entropy encoding routines
+ * that are shared between the sequential encoder (jchuff.c) and the
+ * progressive encoder (jcphuff.c). No other modules need to see these.
+ */
+
+/* The legal range of a DCT coefficient is
+ * -1024 .. +1023 for 8-bit data;
+ * -16384 .. +16383 for 12-bit data.
+ * Hence the magnitude should always fit in 10 or 14 bits respectively.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MAX_COEF_BITS 10
+#else
+#define MAX_COEF_BITS 14
+#endif
+
+/* Derived data constructed for each Huffman table */
+
+typedef struct {
+ unsigned int ehufco[256]; /* code for each symbol */
+ char ehufsi[256]; /* length of code for each symbol */
+ /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
+} c_derived_tbl;
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_make_c_derived_tbl jMkCDerived
+#define jpeg_gen_optimal_table jGenOptTbl
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Expand a Huffman table definition into the derived format */
+EXTERN(void) jpeg_make_c_derived_tbl
+ JPP((j_compress_ptr cinfo, boolean isDC, int tblno,
+ c_derived_tbl ** pdtbl));
+
+/* Generate an optimal table definition given the specified counts */
+EXTERN(void) jpeg_gen_optimal_table
+ JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]));
diff --git a/test/monniaux/jpeg-6b/jcinit.c b/test/monniaux/jpeg-6b/jcinit.c
new file mode 100644
index 00000000..5efffe33
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcinit.c
@@ -0,0 +1,72 @@
+/*
+ * jcinit.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains initialization logic for the JPEG compressor.
+ * This routine is in charge of selecting the modules to be executed and
+ * making an initialization call to each one.
+ *
+ * Logically, this code belongs in jcmaster.c. It's split out because
+ * linking this routine implies linking the entire compression library.
+ * For a transcoding-only application, we want to be able to use jcmaster.c
+ * without linking in the whole library.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Master selection of compression modules.
+ * This is done once at the start of processing an image. We determine
+ * which modules will be used and give them appropriate initialization calls.
+ */
+
+GLOBAL(void)
+jinit_compress_master (j_compress_ptr cinfo)
+{
+ /* Initialize master control (includes parameter checking/processing) */
+ jinit_c_master_control(cinfo, FALSE /* full compression */);
+
+ /* Preprocessing */
+ if (! cinfo->raw_data_in) {
+ jinit_color_converter(cinfo);
+ jinit_downsampler(cinfo);
+ jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
+ }
+ /* Forward DCT */
+ jinit_forward_dct(cinfo);
+ /* Entropy encoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ jinit_phuff_encoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_encoder(cinfo);
+ }
+
+ /* Need a full-image coefficient buffer in any multi-pass mode. */
+ jinit_c_coef_controller(cinfo,
+ (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
+ jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
+
+ jinit_marker_writer(cinfo);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Write the datastream header (SOI) immediately.
+ * Frame and scan headers are postponed till later.
+ * This lets application insert special markers after the SOI.
+ */
+ (*cinfo->marker->write_file_header) (cinfo);
+}
diff --git a/test/monniaux/jpeg-6b/jcmainct.c b/test/monniaux/jpeg-6b/jcmainct.c
new file mode 100644
index 00000000..e0279a7e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcmainct.c
@@ -0,0 +1,293 @@
+/*
+ * jcmainct.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the main buffer controller for compression.
+ * The main buffer lies between the pre-processor and the JPEG
+ * compressor proper; it holds downsampled data in the JPEG colorspace.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Note: currently, there is no operating mode in which a full-image buffer
+ * is needed at this step. If there were, that mode could not be used with
+ * "raw data" input, since this module is bypassed in that case. However,
+ * we've left the code here for possible use in special applications.
+ */
+#undef FULL_MAIN_BUFFER_SUPPORTED
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_main_controller pub; /* public fields */
+
+ JDIMENSION cur_iMCU_row; /* number of current iMCU row */
+ JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
+ boolean suspended; /* remember if we suspended output */
+ J_BUF_MODE pass_mode; /* current operating mode */
+
+ /* If using just a strip buffer, this points to the entire set of buffers
+ * (we allocate one for each component). In the full-image case, this
+ * points to the currently accessible strips of the virtual arrays.
+ */
+ JSAMPARRAY buffer[MAX_COMPONENTS];
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ /* If using full-image storage, this array holds pointers to virtual-array
+ * control blocks for each component. Unused if not full-image storage.
+ */
+ jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
+#endif
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+
+/* Forward declarations */
+METHODDEF(void) process_data_simple_main
+ JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+METHODDEF(void) process_data_buffer_main
+ JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ /* Do nothing in raw-data mode. */
+ if (cinfo->raw_data_in)
+ return;
+
+ main->cur_iMCU_row = 0; /* initialize counters */
+ main->rowgroup_ctr = 0;
+ main->suspended = FALSE;
+ main->pass_mode = pass_mode; /* save mode for use by process_data */
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ if (main->whole_image[0] != NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ main->pub.process_data = process_data_simple_main;
+ break;
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ case JBUF_SAVE_SOURCE:
+ case JBUF_CRANK_DEST:
+ case JBUF_SAVE_AND_PASS:
+ if (main->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ main->pub.process_data = process_data_buffer_main;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This routine handles the simple pass-through mode,
+ * where we have only a strip buffer.
+ */
+
+METHODDEF(void)
+process_data_simple_main (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
+ /* Read input data if we haven't filled the main buffer yet */
+ if (main->rowgroup_ctr < DCTSIZE)
+ (*cinfo->prep->pre_process_data) (cinfo,
+ input_buf, in_row_ctr, in_rows_avail,
+ main->buffer, &main->rowgroup_ctr,
+ (JDIMENSION) DCTSIZE);
+
+ /* If we don't have a full iMCU row buffered, return to application for
+ * more data. Note that preprocessor will always pad to fill the iMCU row
+ * at the bottom of the image.
+ */
+ if (main->rowgroup_ctr != DCTSIZE)
+ return;
+
+ /* Send the completed row to the compressor */
+ if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
+ /* If compressor did not consume the whole row, then we must need to
+ * suspend processing and return to the application. In this situation
+ * we pretend we didn't yet consume the last input row; otherwise, if
+ * it happened to be the last row of the image, the application would
+ * think we were done.
+ */
+ if (! main->suspended) {
+ (*in_row_ctr)--;
+ main->suspended = TRUE;
+ }
+ return;
+ }
+ /* We did finish the row. Undo our little suspension hack if a previous
+ * call suspended; then mark the main buffer empty.
+ */
+ if (main->suspended) {
+ (*in_row_ctr)++;
+ main->suspended = FALSE;
+ }
+ main->rowgroup_ctr = 0;
+ main->cur_iMCU_row++;
+ }
+}
+
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+
+/*
+ * Process some data.
+ * This routine handles all of the modes that use a full-size buffer.
+ */
+
+METHODDEF(void)
+process_data_buffer_main (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci;
+ jpeg_component_info *compptr;
+ boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
+
+ while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
+ /* Realign the virtual buffers if at the start of an iMCU row. */
+ if (main->rowgroup_ctr == 0) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, main->whole_image[ci],
+ main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
+ }
+ /* In a read pass, pretend we just read some source data. */
+ if (! writing) {
+ *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
+ main->rowgroup_ctr = DCTSIZE;
+ }
+ }
+
+ /* If a write pass, read input data until the current iMCU row is full. */
+ /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
+ if (writing) {
+ (*cinfo->prep->pre_process_data) (cinfo,
+ input_buf, in_row_ctr, in_rows_avail,
+ main->buffer, &main->rowgroup_ctr,
+ (JDIMENSION) DCTSIZE);
+ /* Return to application if we need more data to fill the iMCU row. */
+ if (main->rowgroup_ctr < DCTSIZE)
+ return;
+ }
+
+ /* Emit data, unless this is a sink-only pass. */
+ if (main->pass_mode != JBUF_SAVE_SOURCE) {
+ if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
+ /* If compressor did not consume the whole row, then we must need to
+ * suspend processing and return to the application. In this situation
+ * we pretend we didn't yet consume the last input row; otherwise, if
+ * it happened to be the last row of the image, the application would
+ * think we were done.
+ */
+ if (! main->suspended) {
+ (*in_row_ctr)--;
+ main->suspended = TRUE;
+ }
+ return;
+ }
+ /* We did finish the row. Undo our little suspension hack if a previous
+ * call suspended; then mark the main buffer empty.
+ */
+ if (main->suspended) {
+ (*in_row_ctr)++;
+ main->suspended = FALSE;
+ }
+ }
+
+ /* If get here, we are done with this iMCU row. Mark buffer empty. */
+ main->rowgroup_ctr = 0;
+ main->cur_iMCU_row++;
+ }
+}
+
+#endif /* FULL_MAIN_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL(void)
+jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_main_ptr main;
+ int ci;
+ jpeg_component_info *compptr;
+
+ main = (my_main_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_main_controller));
+ cinfo->main = (struct jpeg_c_main_controller *) main;
+ main->pub.start_pass = start_pass_main;
+
+ /* We don't need to create a buffer in raw-data mode. */
+ if (cinfo->raw_data_in)
+ return;
+
+ /* Create the buffer. It holds downsampled data, so each component
+ * may be of a different size.
+ */
+ if (need_full_buffer) {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ /* Allocate a full-image virtual array for each component */
+ /* Note we pad the bottom to a multiple of the iMCU height */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ compptr->width_in_blocks * DCTSIZE,
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor) * DCTSIZE,
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+ }
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ } else {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ main->whole_image[0] = NULL; /* flag for no virtual arrays */
+#endif
+ /* Allocate a strip buffer for each component */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ compptr->width_in_blocks * DCTSIZE,
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+ }
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jcmarker.c b/test/monniaux/jpeg-6b/jcmarker.c
new file mode 100644
index 00000000..3d1e6c6d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcmarker.c
@@ -0,0 +1,664 @@
+/*
+ * jcmarker.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write JPEG datastream markers.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum { /* JPEG marker codes */
+ M_SOF0 = 0xc0,
+ M_SOF1 = 0xc1,
+ M_SOF2 = 0xc2,
+ M_SOF3 = 0xc3,
+
+ M_SOF5 = 0xc5,
+ M_SOF6 = 0xc6,
+ M_SOF7 = 0xc7,
+
+ M_JPG = 0xc8,
+ M_SOF9 = 0xc9,
+ M_SOF10 = 0xca,
+ M_SOF11 = 0xcb,
+
+ M_SOF13 = 0xcd,
+ M_SOF14 = 0xce,
+ M_SOF15 = 0xcf,
+
+ M_DHT = 0xc4,
+
+ M_DAC = 0xcc,
+
+ M_RST0 = 0xd0,
+ M_RST1 = 0xd1,
+ M_RST2 = 0xd2,
+ M_RST3 = 0xd3,
+ M_RST4 = 0xd4,
+ M_RST5 = 0xd5,
+ M_RST6 = 0xd6,
+ M_RST7 = 0xd7,
+
+ M_SOI = 0xd8,
+ M_EOI = 0xd9,
+ M_SOS = 0xda,
+ M_DQT = 0xdb,
+ M_DNL = 0xdc,
+ M_DRI = 0xdd,
+ M_DHP = 0xde,
+ M_EXP = 0xdf,
+
+ M_APP0 = 0xe0,
+ M_APP1 = 0xe1,
+ M_APP2 = 0xe2,
+ M_APP3 = 0xe3,
+ M_APP4 = 0xe4,
+ M_APP5 = 0xe5,
+ M_APP6 = 0xe6,
+ M_APP7 = 0xe7,
+ M_APP8 = 0xe8,
+ M_APP9 = 0xe9,
+ M_APP10 = 0xea,
+ M_APP11 = 0xeb,
+ M_APP12 = 0xec,
+ M_APP13 = 0xed,
+ M_APP14 = 0xee,
+ M_APP15 = 0xef,
+
+ M_JPG0 = 0xf0,
+ M_JPG13 = 0xfd,
+ M_COM = 0xfe,
+
+ M_TEM = 0x01,
+
+ M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_marker_writer pub; /* public fields */
+
+ unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
+} my_marker_writer;
+
+typedef my_marker_writer * my_marker_ptr;
+
+
+/*
+ * Basic output routines.
+ *
+ * Note that we do not support suspension while writing a marker.
+ * Therefore, an application using suspension must ensure that there is
+ * enough buffer space for the initial markers (typ. 600-700 bytes) before
+ * calling jpeg_start_compress, and enough space to write the trailing EOI
+ * (a few bytes) before calling jpeg_finish_compress. Multipass compression
+ * modes are not supported at all with suspension, so those two are the only
+ * points where markers will be written.
+ */
+
+LOCAL(void)
+emit_byte (j_compress_ptr cinfo, int val)
+/* Emit a byte */
+{
+ struct jpeg_destination_mgr * dest = cinfo->dest;
+
+ *(dest->next_output_byte)++ = (JOCTET) val;
+ if (--dest->free_in_buffer == 0) {
+ if (! (*dest->empty_output_buffer) (cinfo))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+ }
+}
+
+
+LOCAL(void)
+emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
+/* Emit a marker code */
+{
+ emit_byte(cinfo, 0xFF);
+ emit_byte(cinfo, (int) mark);
+}
+
+
+LOCAL(void)
+emit_2bytes (j_compress_ptr cinfo, int value)
+/* Emit a 2-byte integer; these are always MSB first in JPEG files */
+{
+ emit_byte(cinfo, (value >> 8) & 0xFF);
+ emit_byte(cinfo, value & 0xFF);
+}
+
+
+/*
+ * Routines to write specific marker types.
+ */
+
+LOCAL(int)
+emit_dqt (j_compress_ptr cinfo, int index)
+/* Emit a DQT marker */
+/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
+{
+ JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
+ int prec;
+ int i;
+
+ if (qtbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
+
+ prec = 0;
+ for (i = 0; i < DCTSIZE2; i++) {
+ if (qtbl->quantval[i] > 255)
+ prec = 1;
+ }
+
+ if (! qtbl->sent_table) {
+ emit_marker(cinfo, M_DQT);
+
+ emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
+
+ emit_byte(cinfo, index + (prec<<4));
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ /* The table entries must be emitted in zigzag order. */
+ unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
+ if (prec)
+ emit_byte(cinfo, (int) (qval >> 8));
+ emit_byte(cinfo, (int) (qval & 0xFF));
+ }
+
+ qtbl->sent_table = TRUE;
+ }
+
+ return prec;
+}
+
+
+LOCAL(void)
+emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
+/* Emit a DHT marker */
+{
+ JHUFF_TBL * htbl;
+ int length, i;
+
+ if (is_ac) {
+ htbl = cinfo->ac_huff_tbl_ptrs[index];
+ index += 0x10; /* output index has AC bit set */
+ } else {
+ htbl = cinfo->dc_huff_tbl_ptrs[index];
+ }
+
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
+
+ if (! htbl->sent_table) {
+ emit_marker(cinfo, M_DHT);
+
+ length = 0;
+ for (i = 1; i <= 16; i++)
+ length += htbl->bits[i];
+
+ emit_2bytes(cinfo, length + 2 + 1 + 16);
+ emit_byte(cinfo, index);
+
+ for (i = 1; i <= 16; i++)
+ emit_byte(cinfo, htbl->bits[i]);
+
+ for (i = 0; i < length; i++)
+ emit_byte(cinfo, htbl->huffval[i]);
+
+ htbl->sent_table = TRUE;
+ }
+}
+
+
+LOCAL(void)
+emit_dac (j_compress_ptr cinfo)
+/* Emit a DAC marker */
+/* Since the useful info is so small, we want to emit all the tables in */
+/* one DAC marker. Therefore this routine does its own scan of the table. */
+{
+#ifdef C_ARITH_CODING_SUPPORTED
+ char dc_in_use[NUM_ARITH_TBLS];
+ char ac_in_use[NUM_ARITH_TBLS];
+ int length, i;
+ jpeg_component_info *compptr;
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++)
+ dc_in_use[i] = ac_in_use[i] = 0;
+
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ dc_in_use[compptr->dc_tbl_no] = 1;
+ ac_in_use[compptr->ac_tbl_no] = 1;
+ }
+
+ length = 0;
+ for (i = 0; i < NUM_ARITH_TBLS; i++)
+ length += dc_in_use[i] + ac_in_use[i];
+
+ emit_marker(cinfo, M_DAC);
+
+ emit_2bytes(cinfo, length*2 + 2);
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ if (dc_in_use[i]) {
+ emit_byte(cinfo, i);
+ emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
+ }
+ if (ac_in_use[i]) {
+ emit_byte(cinfo, i + 0x10);
+ emit_byte(cinfo, cinfo->arith_ac_K[i]);
+ }
+ }
+#endif /* C_ARITH_CODING_SUPPORTED */
+}
+
+
+LOCAL(void)
+emit_dri (j_compress_ptr cinfo)
+/* Emit a DRI marker */
+{
+ emit_marker(cinfo, M_DRI);
+
+ emit_2bytes(cinfo, 4); /* fixed length */
+
+ emit_2bytes(cinfo, (int) cinfo->restart_interval);
+}
+
+
+LOCAL(void)
+emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
+/* Emit a SOF marker */
+{
+ int ci;
+ jpeg_component_info *compptr;
+
+ emit_marker(cinfo, code);
+
+ emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
+
+ /* Make sure image isn't bigger than SOF field can handle */
+ if ((long) cinfo->image_height > 65535L ||
+ (long) cinfo->image_width > 65535L)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
+
+ emit_byte(cinfo, cinfo->data_precision);
+ emit_2bytes(cinfo, (int) cinfo->image_height);
+ emit_2bytes(cinfo, (int) cinfo->image_width);
+
+ emit_byte(cinfo, cinfo->num_components);
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ emit_byte(cinfo, compptr->component_id);
+ emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
+ emit_byte(cinfo, compptr->quant_tbl_no);
+ }
+}
+
+
+LOCAL(void)
+emit_sos (j_compress_ptr cinfo)
+/* Emit a SOS marker */
+{
+ int i, td, ta;
+ jpeg_component_info *compptr;
+
+ emit_marker(cinfo, M_SOS);
+
+ emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
+
+ emit_byte(cinfo, cinfo->comps_in_scan);
+
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ emit_byte(cinfo, compptr->component_id);
+ td = compptr->dc_tbl_no;
+ ta = compptr->ac_tbl_no;
+ if (cinfo->progressive_mode) {
+ /* Progressive mode: only DC or only AC tables are used in one scan;
+ * furthermore, Huffman coding of DC refinement uses no table at all.
+ * We emit 0 for unused field(s); this is recommended by the P&M text
+ * but does not seem to be specified in the standard.
+ */
+ if (cinfo->Ss == 0) {
+ ta = 0; /* DC scan */
+ if (cinfo->Ah != 0 && !cinfo->arith_code)
+ td = 0; /* no DC table either */
+ } else {
+ td = 0; /* AC scan */
+ }
+ }
+ emit_byte(cinfo, (td << 4) + ta);
+ }
+
+ emit_byte(cinfo, cinfo->Ss);
+ emit_byte(cinfo, cinfo->Se);
+ emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
+}
+
+
+LOCAL(void)
+emit_jfif_app0 (j_compress_ptr cinfo)
+/* Emit a JFIF-compliant APP0 marker */
+{
+ /*
+ * Length of APP0 block (2 bytes)
+ * Block ID (4 bytes - ASCII "JFIF")
+ * Zero byte (1 byte to terminate the ID string)
+ * Version Major, Minor (2 bytes - major first)
+ * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
+ * Xdpu (2 bytes - dots per unit horizontal)
+ * Ydpu (2 bytes - dots per unit vertical)
+ * Thumbnail X size (1 byte)
+ * Thumbnail Y size (1 byte)
+ */
+
+ emit_marker(cinfo, M_APP0);
+
+ emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
+
+ emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
+ emit_byte(cinfo, 0x46);
+ emit_byte(cinfo, 0x49);
+ emit_byte(cinfo, 0x46);
+ emit_byte(cinfo, 0);
+ emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
+ emit_byte(cinfo, cinfo->JFIF_minor_version);
+ emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
+ emit_2bytes(cinfo, (int) cinfo->X_density);
+ emit_2bytes(cinfo, (int) cinfo->Y_density);
+ emit_byte(cinfo, 0); /* No thumbnail image */
+ emit_byte(cinfo, 0);
+}
+
+
+LOCAL(void)
+emit_adobe_app14 (j_compress_ptr cinfo)
+/* Emit an Adobe APP14 marker */
+{
+ /*
+ * Length of APP14 block (2 bytes)
+ * Block ID (5 bytes - ASCII "Adobe")
+ * Version Number (2 bytes - currently 100)
+ * Flags0 (2 bytes - currently 0)
+ * Flags1 (2 bytes - currently 0)
+ * Color transform (1 byte)
+ *
+ * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
+ * now in circulation seem to use Version = 100, so that's what we write.
+ *
+ * We write the color transform byte as 1 if the JPEG color space is
+ * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
+ * whether the encoder performed a transformation, which is pretty useless.
+ */
+
+ emit_marker(cinfo, M_APP14);
+
+ emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
+
+ emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
+ emit_byte(cinfo, 0x64);
+ emit_byte(cinfo, 0x6F);
+ emit_byte(cinfo, 0x62);
+ emit_byte(cinfo, 0x65);
+ emit_2bytes(cinfo, 100); /* Version */
+ emit_2bytes(cinfo, 0); /* Flags0 */
+ emit_2bytes(cinfo, 0); /* Flags1 */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_YCbCr:
+ emit_byte(cinfo, 1); /* Color transform = 1 */
+ break;
+ case JCS_YCCK:
+ emit_byte(cinfo, 2); /* Color transform = 2 */
+ break;
+ default:
+ emit_byte(cinfo, 0); /* Color transform = 0 */
+ break;
+ }
+}
+
+
+/*
+ * These routines allow writing an arbitrary marker with parameters.
+ * The only intended use is to emit COM or APPn markers after calling
+ * write_file_header and before calling write_frame_header.
+ * Other uses are not guaranteed to produce desirable results.
+ * Counting the parameter bytes properly is the caller's responsibility.
+ */
+
+METHODDEF(void)
+write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
+/* Emit an arbitrary marker header */
+{
+ if (datalen > (unsigned int) 65533) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ emit_marker(cinfo, (JPEG_MARKER) marker);
+
+ emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
+}
+
+METHODDEF(void)
+write_marker_byte (j_compress_ptr cinfo, int val)
+/* Emit one byte of marker parameters following write_marker_header */
+{
+ emit_byte(cinfo, val);
+}
+
+
+/*
+ * Write datastream header.
+ * This consists of an SOI and optional APPn markers.
+ * We recommend use of the JFIF marker, but not the Adobe marker,
+ * when using YCbCr or grayscale data. The JFIF marker should NOT
+ * be used for any other JPEG colorspace. The Adobe marker is helpful
+ * to distinguish RGB, CMYK, and YCCK colorspaces.
+ * Note that an application can write additional header markers after
+ * jpeg_start_compress returns.
+ */
+
+METHODDEF(void)
+write_file_header (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ emit_marker(cinfo, M_SOI); /* first the SOI */
+
+ /* SOI is defined to reset restart interval to 0 */
+ marker->last_restart_interval = 0;
+
+ if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
+ emit_jfif_app0(cinfo);
+ if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
+ emit_adobe_app14(cinfo);
+}
+
+
+/*
+ * Write frame header.
+ * This consists of DQT and SOFn markers.
+ * Note that we do not emit the SOF until we have emitted the DQT(s).
+ * This avoids compatibility problems with incorrect implementations that
+ * try to error-check the quant table numbers as soon as they see the SOF.
+ */
+
+METHODDEF(void)
+write_frame_header (j_compress_ptr cinfo)
+{
+ int ci, prec;
+ boolean is_baseline;
+ jpeg_component_info *compptr;
+
+ /* Emit DQT for each quantization table.
+ * Note that emit_dqt() suppresses any duplicate tables.
+ */
+ prec = 0;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ prec += emit_dqt(cinfo, compptr->quant_tbl_no);
+ }
+ /* now prec is nonzero iff there are any 16-bit quant tables. */
+
+ /* Check for a non-baseline specification.
+ * Note we assume that Huffman table numbers won't be changed later.
+ */
+ if (cinfo->arith_code || cinfo->progressive_mode ||
+ cinfo->data_precision != 8) {
+ is_baseline = FALSE;
+ } else {
+ is_baseline = TRUE;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
+ is_baseline = FALSE;
+ }
+ if (prec && is_baseline) {
+ is_baseline = FALSE;
+ /* If it's baseline except for quantizer size, warn the user */
+ TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
+ }
+ }
+
+ /* Emit the proper SOF marker */
+ if (cinfo->arith_code) {
+ emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
+ } else {
+ if (cinfo->progressive_mode)
+ emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
+ else if (is_baseline)
+ emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
+ else
+ emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
+ }
+}
+
+
+/*
+ * Write scan header.
+ * This consists of DHT or DAC markers, optional DRI, and SOS.
+ * Compressed data will be written following the SOS.
+ */
+
+METHODDEF(void)
+write_scan_header (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ int i;
+ jpeg_component_info *compptr;
+
+ if (cinfo->arith_code) {
+ /* Emit arith conditioning info. We may have some duplication
+ * if the file has multiple scans, but it's so small it's hardly
+ * worth worrying about.
+ */
+ emit_dac(cinfo);
+ } else {
+ /* Emit Huffman tables.
+ * Note that emit_dht() suppresses any duplicate tables.
+ */
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ if (cinfo->progressive_mode) {
+ /* Progressive mode: only DC or only AC tables are used in one scan */
+ if (cinfo->Ss == 0) {
+ if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
+ emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+ } else {
+ emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+ }
+ } else {
+ /* Sequential mode: need both DC and AC tables */
+ emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+ emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+ }
+ }
+ }
+
+ /* Emit DRI if required --- note that DRI value could change for each scan.
+ * We avoid wasting space with unnecessary DRIs, however.
+ */
+ if (cinfo->restart_interval != marker->last_restart_interval) {
+ emit_dri(cinfo);
+ marker->last_restart_interval = cinfo->restart_interval;
+ }
+
+ emit_sos(cinfo);
+}
+
+
+/*
+ * Write datastream trailer.
+ */
+
+METHODDEF(void)
+write_file_trailer (j_compress_ptr cinfo)
+{
+ emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Write an abbreviated table-specification datastream.
+ * This consists of SOI, DQT and DHT tables, and EOI.
+ * Any table that is defined and not marked sent_table = TRUE will be
+ * emitted. Note that all tables will be marked sent_table = TRUE at exit.
+ */
+
+METHODDEF(void)
+write_tables_only (j_compress_ptr cinfo)
+{
+ int i;
+
+ emit_marker(cinfo, M_SOI);
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ if (cinfo->quant_tbl_ptrs[i] != NULL)
+ (void) emit_dqt(cinfo, i);
+ }
+
+ if (! cinfo->arith_code) {
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
+ emit_dht(cinfo, i, FALSE);
+ if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
+ emit_dht(cinfo, i, TRUE);
+ }
+ }
+
+ emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Initialize the marker writer module.
+ */
+
+GLOBAL(void)
+jinit_marker_writer (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker;
+
+ /* Create the subobject */
+ marker = (my_marker_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_marker_writer));
+ cinfo->marker = (struct jpeg_marker_writer *) marker;
+ /* Initialize method pointers */
+ marker->pub.write_file_header = write_file_header;
+ marker->pub.write_frame_header = write_frame_header;
+ marker->pub.write_scan_header = write_scan_header;
+ marker->pub.write_file_trailer = write_file_trailer;
+ marker->pub.write_tables_only = write_tables_only;
+ marker->pub.write_marker_header = write_marker_header;
+ marker->pub.write_marker_byte = write_marker_byte;
+ /* Initialize private state */
+ marker->last_restart_interval = 0;
+}
diff --git a/test/monniaux/jpeg-6b/jcmaster.c b/test/monniaux/jpeg-6b/jcmaster.c
new file mode 100644
index 00000000..aab4020b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcmaster.c
@@ -0,0 +1,590 @@
+/*
+ * jcmaster.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains master control logic for the JPEG compressor.
+ * These routines are concerned with parameter validation, initial setup,
+ * and inter-pass control (determining the number of passes and the work
+ * to be done in each pass).
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef enum {
+ main_pass, /* input data, also do first output step */
+ huff_opt_pass, /* Huffman code optimization pass */
+ output_pass /* data output pass */
+} c_pass_type;
+
+typedef struct {
+ struct jpeg_comp_master pub; /* public fields */
+
+ c_pass_type pass_type; /* the type of the current pass */
+
+ int pass_number; /* # of passes completed */
+ int total_passes; /* total # of passes needed */
+
+ int scan_number; /* current index in scan_info[] */
+} my_comp_master;
+
+typedef my_comp_master * my_master_ptr;
+
+
+/*
+ * Support routines that do various essential calculations.
+ */
+
+LOCAL(void)
+initial_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
+{
+ int ci;
+ jpeg_component_info *compptr;
+ long samplesperrow;
+ JDIMENSION jd_samplesperrow;
+
+ /* Sanity check on image dimensions */
+ if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+ || cinfo->num_components <= 0 || cinfo->input_components <= 0)
+ ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+ /* Make sure image isn't bigger than I can handle */
+ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+ (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+ /* Width of an input scanline must be representable as JDIMENSION. */
+ samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
+ jd_samplesperrow = (JDIMENSION) samplesperrow;
+ if ((long) jd_samplesperrow != samplesperrow)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+ /* For now, precision must match compiled-in value... */
+ if (cinfo->data_precision != BITS_IN_JSAMPLE)
+ ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+ /* Check that number of components won't exceed internal array sizes */
+ if (cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+
+ /* Compute maximum sampling factors; check factor validity */
+ cinfo->max_h_samp_factor = 1;
+ cinfo->max_v_samp_factor = 1;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
+ compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
+ ERREXIT(cinfo, JERR_BAD_SAMPLING);
+ cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
+ compptr->h_samp_factor);
+ cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
+ compptr->v_samp_factor);
+ }
+
+ /* Compute dimensions of components */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Fill in the correct component_index value; don't rely on application */
+ compptr->component_index = ci;
+ /* For compression, we never do DCT scaling. */
+ compptr->DCT_scaled_size = DCTSIZE;
+ /* Size in DCT blocks */
+ compptr->width_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->height_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ /* Size in samples */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) cinfo->max_h_samp_factor);
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) cinfo->max_v_samp_factor);
+ /* Mark component needed (this flag isn't actually used for compression) */
+ compptr->component_needed = TRUE;
+ }
+
+ /* Compute number of fully interleaved MCU rows (number of times that
+ * main controller will call coefficient controller).
+ */
+ cinfo->total_iMCU_rows = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+}
+
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+
+LOCAL(void)
+validate_script (j_compress_ptr cinfo)
+/* Verify that the scan script in cinfo->scan_info[] is valid; also
+ * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
+ */
+{
+ const jpeg_scan_info * scanptr;
+ int scanno, ncomps, ci, coefi, thisi;
+ int Ss, Se, Ah, Al;
+ boolean component_sent[MAX_COMPONENTS];
+#ifdef C_PROGRESSIVE_SUPPORTED
+ int * last_bitpos_ptr;
+ int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
+ /* -1 until that coefficient has been seen; then last Al for it */
+#endif
+
+ if (cinfo->num_scans <= 0)
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
+
+ /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
+ * for progressive JPEG, no scan can have this.
+ */
+ scanptr = cinfo->scan_info;
+ if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ cinfo->progressive_mode = TRUE;
+ last_bitpos_ptr = & last_bitpos[0][0];
+ for (ci = 0; ci < cinfo->num_components; ci++)
+ for (coefi = 0; coefi < DCTSIZE2; coefi++)
+ *last_bitpos_ptr++ = -1;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ cinfo->progressive_mode = FALSE;
+ for (ci = 0; ci < cinfo->num_components; ci++)
+ component_sent[ci] = FALSE;
+ }
+
+ for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
+ /* Validate component indexes */
+ ncomps = scanptr->comps_in_scan;
+ if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
+ for (ci = 0; ci < ncomps; ci++) {
+ thisi = scanptr->component_index[ci];
+ if (thisi < 0 || thisi >= cinfo->num_components)
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ /* Components must appear in SOF order within each scan */
+ if (ci > 0 && thisi <= scanptr->component_index[ci-1])
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ }
+ /* Validate progression parameters */
+ Ss = scanptr->Ss;
+ Se = scanptr->Se;
+ Ah = scanptr->Ah;
+ Al = scanptr->Al;
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
+ * seems wrong: the upper bound ought to depend on data precision.
+ * Perhaps they really meant 0..N+1 for N-bit precision.
+ * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
+ * out-of-range reconstructed DC values during the first DC scan,
+ * which might cause problems for some decoders.
+ */
+#if BITS_IN_JSAMPLE == 8
+#define MAX_AH_AL 10
+#else
+#define MAX_AH_AL 13
+#endif
+ if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
+ Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ if (Ss == 0) {
+ if (Se != 0) /* DC and AC together not OK */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ } else {
+ if (ncomps != 1) /* AC scans must be for only one component */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ }
+ for (ci = 0; ci < ncomps; ci++) {
+ last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
+ if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ for (coefi = Ss; coefi <= Se; coefi++) {
+ if (last_bitpos_ptr[coefi] < 0) {
+ /* first scan of this coefficient */
+ if (Ah != 0)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ } else {
+ /* not first scan */
+ if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ }
+ last_bitpos_ptr[coefi] = Al;
+ }
+ }
+#endif
+ } else {
+ /* For sequential JPEG, all progression parameters must be these: */
+ if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ /* Make sure components are not sent twice */
+ for (ci = 0; ci < ncomps; ci++) {
+ thisi = scanptr->component_index[ci];
+ if (component_sent[thisi])
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ component_sent[thisi] = TRUE;
+ }
+ }
+ }
+
+ /* Now verify that everything got sent. */
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ /* For progressive mode, we only check that at least some DC data
+ * got sent for each component; the spec does not require that all bits
+ * of all coefficients be transmitted. Would it be wiser to enforce
+ * transmission of all coefficient bits??
+ */
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ if (last_bitpos[ci][0] < 0)
+ ERREXIT(cinfo, JERR_MISSING_DATA);
+ }
+#endif
+ } else {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ if (! component_sent[ci])
+ ERREXIT(cinfo, JERR_MISSING_DATA);
+ }
+ }
+}
+
+#endif /* C_MULTISCAN_FILES_SUPPORTED */
+
+
+LOCAL(void)
+select_scan_parameters (j_compress_ptr cinfo)
+/* Set up the scan parameters for the current scan */
+{
+ int ci;
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (cinfo->scan_info != NULL) {
+ /* Prepare for current scan --- the script is already validated */
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+ const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
+
+ cinfo->comps_in_scan = scanptr->comps_in_scan;
+ for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
+ cinfo->cur_comp_info[ci] =
+ &cinfo->comp_info[scanptr->component_index[ci]];
+ }
+ cinfo->Ss = scanptr->Ss;
+ cinfo->Se = scanptr->Se;
+ cinfo->Ah = scanptr->Ah;
+ cinfo->Al = scanptr->Al;
+ }
+ else
+#endif
+ {
+ /* Prepare for single sequential-JPEG scan containing all components */
+ if (cinfo->num_components > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPS_IN_SCAN);
+ cinfo->comps_in_scan = cinfo->num_components;
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
+ }
+ cinfo->Ss = 0;
+ cinfo->Se = DCTSIZE2-1;
+ cinfo->Ah = 0;
+ cinfo->Al = 0;
+ }
+}
+
+
+LOCAL(void)
+per_scan_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
+{
+ int ci, mcublks, tmp;
+ jpeg_component_info *compptr;
+
+ if (cinfo->comps_in_scan == 1) {
+
+ /* Noninterleaved (single-component) scan */
+ compptr = cinfo->cur_comp_info[0];
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = compptr->width_in_blocks;
+ cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+
+ /* For noninterleaved scan, always one block per MCU */
+ compptr->MCU_width = 1;
+ compptr->MCU_height = 1;
+ compptr->MCU_blocks = 1;
+ compptr->MCU_sample_width = DCTSIZE;
+ compptr->last_col_width = 1;
+ /* For noninterleaved scans, it is convenient to define last_row_height
+ * as the number of block rows present in the last iMCU row.
+ */
+ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (tmp == 0) tmp = compptr->v_samp_factor;
+ compptr->last_row_height = tmp;
+
+ /* Prepare array describing MCU composition */
+ cinfo->blocks_in_MCU = 1;
+ cinfo->MCU_membership[0] = 0;
+
+ } else {
+
+ /* Interleaved (multi-component) scan */
+ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+ MAX_COMPS_IN_SCAN);
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width,
+ (long) (cinfo->max_h_samp_factor*DCTSIZE));
+ cinfo->MCU_rows_in_scan = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ cinfo->blocks_in_MCU = 0;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Sampling factors give # of blocks of component in each MCU */
+ compptr->MCU_width = compptr->h_samp_factor;
+ compptr->MCU_height = compptr->v_samp_factor;
+ compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+ compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
+ /* Figure number of non-dummy blocks in last MCU column & row */
+ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+ if (tmp == 0) tmp = compptr->MCU_width;
+ compptr->last_col_width = tmp;
+ tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+ if (tmp == 0) tmp = compptr->MCU_height;
+ compptr->last_row_height = tmp;
+ /* Prepare array describing MCU composition */
+ mcublks = compptr->MCU_blocks;
+ if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
+ ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+ while (mcublks-- > 0) {
+ cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+ }
+ }
+
+ }
+
+ /* Convert restart specified in rows to actual MCU count. */
+ /* Note that count must fit in 16 bits, so we provide limiting. */
+ if (cinfo->restart_in_rows > 0) {
+ long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
+ cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
+ }
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each pass. We determine which modules
+ * will be active during this pass and give them appropriate start_pass calls.
+ * We also set is_last_pass to indicate whether any more passes will be
+ * required.
+ */
+
+METHODDEF(void)
+prepare_for_pass (j_compress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ switch (master->pass_type) {
+ case main_pass:
+ /* Initial pass: will collect input data, and do either Huffman
+ * optimization or data output for the first scan.
+ */
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ if (! cinfo->raw_data_in) {
+ (*cinfo->cconvert->start_pass) (cinfo);
+ (*cinfo->downsample->start_pass) (cinfo);
+ (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
+ }
+ (*cinfo->fdct->start_pass) (cinfo);
+ (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
+ (*cinfo->coef->start_pass) (cinfo,
+ (master->total_passes > 1 ?
+ JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+ (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+ if (cinfo->optimize_coding) {
+ /* No immediate data output; postpone writing frame/scan headers */
+ master->pub.call_pass_startup = FALSE;
+ } else {
+ /* Will write frame/scan headers at first jpeg_write_scanlines call */
+ master->pub.call_pass_startup = TRUE;
+ }
+ break;
+#ifdef ENTROPY_OPT_SUPPORTED
+ case huff_opt_pass:
+ /* Do Huffman optimization for a scan after the first one. */
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
+ (*cinfo->entropy->start_pass) (cinfo, TRUE);
+ (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+ master->pub.call_pass_startup = FALSE;
+ break;
+ }
+ /* Special case: Huffman DC refinement scans need no Huffman table
+ * and therefore we can skip the optimization pass for them.
+ */
+ master->pass_type = output_pass;
+ master->pass_number++;
+ /*FALLTHROUGH*/
+#endif
+ case output_pass:
+ /* Do a data-output pass. */
+ /* We need not repeat per-scan setup if prior optimization pass did it. */
+ if (! cinfo->optimize_coding) {
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ }
+ (*cinfo->entropy->start_pass) (cinfo, FALSE);
+ (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+ /* We emit frame/scan headers now */
+ if (master->scan_number == 0)
+ (*cinfo->marker->write_frame_header) (cinfo);
+ (*cinfo->marker->write_scan_header) (cinfo);
+ master->pub.call_pass_startup = FALSE;
+ break;
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ }
+
+ master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
+
+ /* Set up progress monitor's pass info if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->completed_passes = master->pass_number;
+ cinfo->progress->total_passes = master->total_passes;
+ }
+}
+
+
+/*
+ * Special start-of-pass hook.
+ * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
+ * In single-pass processing, we need this hook because we don't want to
+ * write frame/scan headers during jpeg_start_compress; we want to let the
+ * application write COM markers etc. between jpeg_start_compress and the
+ * jpeg_write_scanlines loop.
+ * In multi-pass processing, this routine is not used.
+ */
+
+METHODDEF(void)
+pass_startup (j_compress_ptr cinfo)
+{
+ cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
+
+ (*cinfo->marker->write_frame_header) (cinfo);
+ (*cinfo->marker->write_scan_header) (cinfo);
+}
+
+
+/*
+ * Finish up at end of pass.
+ */
+
+METHODDEF(void)
+finish_pass_master (j_compress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ /* The entropy coder always needs an end-of-pass call,
+ * either to analyze statistics or to flush its output buffer.
+ */
+ (*cinfo->entropy->finish_pass) (cinfo);
+
+ /* Update state for next pass */
+ switch (master->pass_type) {
+ case main_pass:
+ /* next pass is either output of scan 0 (after optimization)
+ * or output of scan 1 (if no optimization).
+ */
+ master->pass_type = output_pass;
+ if (! cinfo->optimize_coding)
+ master->scan_number++;
+ break;
+ case huff_opt_pass:
+ /* next pass is always output of current scan */
+ master->pass_type = output_pass;
+ break;
+ case output_pass:
+ /* next pass is either optimization or output of next scan */
+ if (cinfo->optimize_coding)
+ master->pass_type = huff_opt_pass;
+ master->scan_number++;
+ break;
+ }
+
+ master->pass_number++;
+}
+
+
+/*
+ * Initialize master compression control.
+ */
+
+GLOBAL(void)
+jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
+{
+ my_master_ptr master;
+
+ master = (my_master_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_comp_master));
+ cinfo->master = (struct jpeg_comp_master *) master;
+ master->pub.prepare_for_pass = prepare_for_pass;
+ master->pub.pass_startup = pass_startup;
+ master->pub.finish_pass = finish_pass_master;
+ master->pub.is_last_pass = FALSE;
+
+ /* Validate parameters, determine derived values */
+ initial_setup(cinfo);
+
+ if (cinfo->scan_info != NULL) {
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ validate_script(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ cinfo->progressive_mode = FALSE;
+ cinfo->num_scans = 1;
+ }
+
+ if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
+ cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
+
+ /* Initialize my private state */
+ if (transcode_only) {
+ /* no main pass in transcoding */
+ if (cinfo->optimize_coding)
+ master->pass_type = huff_opt_pass;
+ else
+ master->pass_type = output_pass;
+ } else {
+ /* for normal compression, first pass is always this type: */
+ master->pass_type = main_pass;
+ }
+ master->scan_number = 0;
+ master->pass_number = 0;
+ if (cinfo->optimize_coding)
+ master->total_passes = cinfo->num_scans * 2;
+ else
+ master->total_passes = cinfo->num_scans;
+}
diff --git a/test/monniaux/jpeg-6b/jcomapi.c b/test/monniaux/jpeg-6b/jcomapi.c
new file mode 100644
index 00000000..9b1fa756
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcomapi.c
@@ -0,0 +1,106 @@
+/*
+ * jcomapi.c
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface routines that are used for both
+ * compression and decompression.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Abort processing of a JPEG compression or decompression operation,
+ * but don't destroy the object itself.
+ *
+ * For this, we merely clean up all the nonpermanent memory pools.
+ * Note that temp files (virtual arrays) are not allowed to belong to
+ * the permanent pool, so we will be able to close all temp files here.
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL(void)
+jpeg_abort (j_common_ptr cinfo)
+{
+ int pool;
+
+ /* Do nothing if called on a not-initialized or destroyed JPEG object. */
+ if (cinfo->mem == NULL)
+ return;
+
+ /* Releasing pools in reverse order might help avoid fragmentation
+ * with some (brain-damaged) malloc libraries.
+ */
+ for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
+ (*cinfo->mem->free_pool) (cinfo, pool);
+ }
+
+ /* Reset overall state for possible reuse of object */
+ if (cinfo->is_decompressor) {
+ cinfo->global_state = DSTATE_START;
+ /* Try to keep application from accessing now-deleted marker list.
+ * A bit kludgy to do it here, but this is the most central place.
+ */
+ ((j_decompress_ptr) cinfo)->marker_list = NULL;
+ } else {
+ cinfo->global_state = CSTATE_START;
+ }
+}
+
+
+/*
+ * Destruction of a JPEG object.
+ *
+ * Everything gets deallocated except the master jpeg_compress_struct itself
+ * and the error manager struct. Both of these are supplied by the application
+ * and must be freed, if necessary, by the application. (Often they are on
+ * the stack and so don't need to be freed anyway.)
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL(void)
+jpeg_destroy (j_common_ptr cinfo)
+{
+ /* We need only tell the memory manager to release everything. */
+ /* NB: mem pointer is NULL if memory mgr failed to initialize. */
+ if (cinfo->mem != NULL)
+ (*cinfo->mem->self_destruct) (cinfo);
+ cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
+ cinfo->global_state = 0; /* mark it destroyed */
+}
+
+
+/*
+ * Convenience routines for allocating quantization and Huffman tables.
+ * (Would jutils.c be a more reasonable place to put these?)
+ */
+
+GLOBAL(JQUANT_TBL *)
+jpeg_alloc_quant_table (j_common_ptr cinfo)
+{
+ JQUANT_TBL *tbl;
+
+ tbl = (JQUANT_TBL *)
+ (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
+ tbl->sent_table = FALSE; /* make sure this is false in any new table */
+ return tbl;
+}
+
+
+GLOBAL(JHUFF_TBL *)
+jpeg_alloc_huff_table (j_common_ptr cinfo)
+{
+ JHUFF_TBL *tbl;
+
+ tbl = (JHUFF_TBL *)
+ (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
+ tbl->sent_table = FALSE; /* make sure this is false in any new table */
+ return tbl;
+}
diff --git a/test/monniaux/jpeg-6b/jconfig.bcc b/test/monniaux/jpeg-6b/jconfig.bcc
new file mode 100644
index 00000000..c6c53ff6
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.bcc
@@ -0,0 +1,48 @@
+/* jconfig.bcc --- jconfig.h for Borland C (Turbo C) on MS-DOS or OS/2. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#ifdef __MSDOS__
+#define NEED_FAR_POINTERS /* for small or medium memory model */
+#endif
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN /* this assumes you have -w-stu in CFLAGS */
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#ifdef __MSDOS__
+#define USE_MSDOS_MEMMGR /* Define this if you use jmemdos.c */
+#define MAX_ALLOC_CHUNK 65520L /* Maximum request to malloc() */
+#define USE_FMEM /* Borland has _fmemcpy() and _fmemset() */
+#endif
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define USE_SETMODE /* Borland has setmode() */
+#ifdef __MSDOS__
+#define NEED_SIGNAL_CATCHER /* Define this if you use jmemdos.c */
+#endif
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.cfg b/test/monniaux/jpeg-6b/jconfig.cfg
new file mode 100644
index 00000000..36a04fa8
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.cfg
@@ -0,0 +1,44 @@
+/* jconfig.cfg --- source file edited by configure script */
+/* see jconfig.doc for explanations */
+
+#undef HAVE_PROTOTYPES
+#undef HAVE_UNSIGNED_CHAR
+#undef HAVE_UNSIGNED_SHORT
+#undef void
+#undef const
+#undef CHAR_IS_UNSIGNED
+#undef HAVE_STDDEF_H
+#undef HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+/* Define this if you get warnings about undefined structures. */
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+#undef INLINE
+/* These are for configuring the JPEG memory manager. */
+#undef DEFAULT_MAX_MEM
+#undef NO_MKTEMP
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg. */
+#undef PROGRESS_REPORT
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.dj b/test/monniaux/jpeg-6b/jconfig.dj
new file mode 100644
index 00000000..f759a9db
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.dj
@@ -0,0 +1,38 @@
+/* jconfig.dj --- jconfig.h for DJGPP (Delorie's GNU C port) on MS-DOS. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS /* DJGPP uses flat 32-bit addressing */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE /* optional */
+#define USE_SETMODE /* Needed to make one-file style work in DJGPP */
+#undef NEED_SIGNAL_CATCHER /* Define this if you use jmemname.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.doc b/test/monniaux/jpeg-6b/jconfig.doc
new file mode 100644
index 00000000..c18d1c06
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.doc
@@ -0,0 +1,155 @@
+/*
+ * jconfig.doc
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file documents the configuration options that are required to
+ * customize the JPEG software for a particular system.
+ *
+ * The actual configuration options for a particular installation are stored
+ * in jconfig.h. On many machines, jconfig.h can be generated automatically
+ * or copied from one of the "canned" jconfig files that we supply. But if
+ * you need to generate a jconfig.h file by hand, this file tells you how.
+ *
+ * DO NOT EDIT THIS FILE --- IT WON'T ACCOMPLISH ANYTHING.
+ * EDIT A COPY NAMED JCONFIG.H.
+ */
+
+
+/*
+ * These symbols indicate the properties of your machine or compiler.
+ * #define the symbol if yes, #undef it if no.
+ */
+
+/* Does your compiler support function prototypes?
+ * (If not, you also need to use ansi2knr, see install.doc)
+ */
+#define HAVE_PROTOTYPES
+
+/* Does your compiler support the declaration "unsigned char" ?
+ * How about "unsigned short" ?
+ */
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+
+/* Define "void" as "char" if your compiler doesn't know about type void.
+ * NOTE: be sure to define void such that "void *" represents the most general
+ * pointer type, e.g., that returned by malloc().
+ */
+/* #define void char */
+
+/* Define "const" as empty if your compiler doesn't know the "const" keyword.
+ */
+/* #define const */
+
+/* Define this if an ordinary "char" type is unsigned.
+ * If you're not sure, leaving it undefined will work at some cost in speed.
+ * If you defined HAVE_UNSIGNED_CHAR then the speed difference is minimal.
+ */
+#undef CHAR_IS_UNSIGNED
+
+/* Define this if your system has an ANSI-conforming <stddef.h> file.
+ */
+#define HAVE_STDDEF_H
+
+/* Define this if your system has an ANSI-conforming <stdlib.h> file.
+ */
+#define HAVE_STDLIB_H
+
+/* Define this if your system does not have an ANSI/SysV <string.h>,
+ * but does have a BSD-style <strings.h>.
+ */
+#undef NEED_BSD_STRINGS
+
+/* Define this if your system does not provide typedef size_t in any of the
+ * ANSI-standard places (stddef.h, stdlib.h, or stdio.h), but places it in
+ * <sys/types.h> instead.
+ */
+#undef NEED_SYS_TYPES_H
+
+/* For 80x86 machines, you need to define NEED_FAR_POINTERS,
+ * unless you are using a large-data memory model or 80386 flat-memory mode.
+ * On less brain-damaged CPUs this symbol must not be defined.
+ * (Defining this symbol causes large data structures to be referenced through
+ * "far" pointers and to be allocated with a special version of malloc.)
+ */
+#undef NEED_FAR_POINTERS
+
+/* Define this if your linker needs global names to be unique in less
+ * than the first 15 characters.
+ */
+#undef NEED_SHORT_EXTERNAL_NAMES
+
+/* Although a real ANSI C compiler can deal perfectly well with pointers to
+ * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
+ * and pseudo-ANSI compilers get confused. To keep one of these bozos happy,
+ * define INCOMPLETE_TYPES_BROKEN. This is not recommended unless you
+ * actually get "missing structure definition" warnings or errors while
+ * compiling the JPEG code.
+ */
+#undef INCOMPLETE_TYPES_BROKEN
+
+
+/*
+ * The following options affect code selection within the JPEG library,
+ * but they don't need to be visible to applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+
+/* Define this if your compiler implements ">>" on signed values as a logical
+ * (unsigned) shift; leave it undefined if ">>" is a signed (arithmetic) shift,
+ * which is the normal and rational definition.
+ */
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+
+#endif /* JPEG_INTERNALS */
+
+
+/*
+ * The remaining options do not affect the JPEG library proper,
+ * but only the sample applications cjpeg/djpeg (see cjpeg.c, djpeg.c).
+ * Other applications can ignore these.
+ */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+/* These defines indicate which image (non-JPEG) file formats are allowed. */
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+/* Define this if you want to name both input and output files on the command
+ * line, rather than using stdout and optionally stdin. You MUST do this if
+ * your system can't cope with binary I/O to stdin/stdout. See comments at
+ * head of cjpeg.c or djpeg.c.
+ */
+#undef TWO_FILE_COMMANDLINE
+
+/* Define this if your system needs explicit cleanup of temporary files.
+ * This is crucial under MS-DOS, where the temporary "files" may be areas
+ * of extended memory; on most other systems it's not as important.
+ */
+#undef NEED_SIGNAL_CATCHER
+
+/* By default, we open image files with fopen(...,"rb") or fopen(...,"wb").
+ * This is necessary on systems that distinguish text files from binary files,
+ * and is harmless on most systems that don't. If you have one of the rare
+ * systems that complains about the "b" spec, define this symbol.
+ */
+#undef DONT_USE_B_MODE
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg.
+ */
+#undef PROGRESS_REPORT
+
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.h b/test/monniaux/jpeg-6b/jconfig.h
new file mode 100644
index 00000000..9594ec56
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.h
@@ -0,0 +1,45 @@
+/* jconfig.h. Generated automatically by configure. */
+/* jconfig.cfg --- source file edited by configure script */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+#undef void
+#undef const
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+/* Define this if you get warnings about undefined structures. */
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+#define INLINE __inline__
+/* These are for configuring the JPEG memory manager. */
+#undef DEFAULT_MAX_MEM
+#undef NO_MKTEMP
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg. */
+#undef PROGRESS_REPORT
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.mac b/test/monniaux/jpeg-6b/jconfig.mac
new file mode 100644
index 00000000..0de3efe2
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.mac
@@ -0,0 +1,43 @@
+/* jconfig.mac --- jconfig.h for CodeWarrior on Apple Macintosh */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define USE_MAC_MEMMGR /* Define this if you use jmemmac.c */
+
+#define ALIGN_TYPE long /* Needed for 680x0 Macs */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define USE_CCOMMAND /* Command line reader for Macintosh */
+#define TWO_FILE_COMMANDLINE /* Binary I/O thru stdin/stdout doesn't work */
+
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.manx b/test/monniaux/jpeg-6b/jconfig.manx
new file mode 100644
index 00000000..6dd0d008
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.manx
@@ -0,0 +1,43 @@
+/* jconfig.manx --- jconfig.h for Amiga systems using Manx Aztec C ver 5.x. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define TEMP_DIRECTORY "JPEGTMP:" /* recommended setting for Amiga */
+
+#define SHORTxSHORT_32 /* produces better DCT code with Aztec C */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#define signal_catcher _abort /* hack for Aztec C naming requirements */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.mc6 b/test/monniaux/jpeg-6b/jconfig.mc6
new file mode 100644
index 00000000..c55082df
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.mc6
@@ -0,0 +1,52 @@
+/* jconfig.mc6 --- jconfig.h for Microsoft C on MS-DOS, version 6.00A & up. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#define NEED_FAR_POINTERS /* for small or medium memory model */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define USE_MSDOS_MEMMGR /* Define this if you use jmemdos.c */
+
+#define MAX_ALLOC_CHUNK 65520L /* Maximum request to malloc() */
+
+#define USE_FMEM /* Microsoft has _fmemcpy() and _fmemset() */
+
+#define NEED_FHEAPMIN /* far heap management routines are broken */
+
+#define SHORTxLCONST_32 /* enable compiler-specific DCT optimization */
+/* Note: the above define is known to improve the code with Microsoft C 6.00A.
+ * I do not know whether it is good for later compiler versions.
+ * Please report any info on this point to jpeg-info@uunet.uu.net.
+ */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define USE_SETMODE /* Microsoft has setmode() */
+#define NEED_SIGNAL_CATCHER /* Define this if you use jmemdos.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.sas b/test/monniaux/jpeg-6b/jconfig.sas
new file mode 100644
index 00000000..efdac222
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.sas
@@ -0,0 +1,43 @@
+/* jconfig.sas --- jconfig.h for Amiga systems using SAS C 6.0 and up. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define TEMP_DIRECTORY "JPEGTMP:" /* recommended setting for Amiga */
+
+#define NO_MKTEMP /* SAS C doesn't have mktemp() */
+
+#define SHORTxSHORT_32 /* produces better DCT code with SAS C */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.st b/test/monniaux/jpeg-6b/jconfig.st
new file mode 100644
index 00000000..4421b7a1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.st
@@ -0,0 +1,42 @@
+/* jconfig.st --- jconfig.h for Atari ST/STE/TT using Pure C or Turbo C. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#define INCOMPLETE_TYPES_BROKEN /* suppress undefined-structure warnings */
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define ALIGN_TYPE long /* apparently double is a weird size? */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE /* optional -- undef if you like Unix style */
+/* Note: if you undef TWO_FILE_COMMANDLINE, you may need to define
+ * USE_SETMODE. Some Atari compilers require it, some do not.
+ */
+#define NEED_SIGNAL_CATCHER /* needed if you use jmemname.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.vc b/test/monniaux/jpeg-6b/jconfig.vc
new file mode 100644
index 00000000..7e291c75
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.vc
@@ -0,0 +1,45 @@
+/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS /* we presume a 32-bit flat memory model */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+/* Define "boolean" as unsigned char, not int, per Windows custom */
+#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+typedef unsigned char boolean;
+#endif
+#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE /* optional */
+#define USE_SETMODE /* Microsoft has setmode() */
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.vms b/test/monniaux/jpeg-6b/jconfig.vms
new file mode 100644
index 00000000..55a6ffba
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.vms
@@ -0,0 +1,37 @@
+/* jconfig.vms --- jconfig.h for use on Digital VMS. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE /* Needed on VMS */
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jconfig.wat b/test/monniaux/jpeg-6b/jconfig.wat
new file mode 100644
index 00000000..6cc545ba
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jconfig.wat
@@ -0,0 +1,38 @@
+/* jconfig.wat --- jconfig.h for Watcom C/C++ on MS-DOS or OS/2. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#define CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS /* Watcom uses flat 32-bit addressing */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE /* optional */
+#define USE_SETMODE /* Needed to make one-file style work in Watcom */
+#undef NEED_SIGNAL_CATCHER /* Define this if you use jmemname.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/test/monniaux/jpeg-6b/jcparam.c b/test/monniaux/jpeg-6b/jcparam.c
new file mode 100644
index 00000000..6fc48f53
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcparam.c
@@ -0,0 +1,610 @@
+/*
+ * jcparam.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains optional default-setting code for the JPEG compressor.
+ * Applications do not have to use this file, but those that don't use it
+ * must know a lot more about the innards of the JPEG code.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Quantization table setup routines
+ */
+
+GLOBAL(void)
+jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
+ const unsigned int *basic_table,
+ int scale_factor, boolean force_baseline)
+/* Define a quantization table equal to the basic_table times
+ * a scale factor (given as a percentage).
+ * If force_baseline is TRUE, the computed quantization table entries
+ * are limited to 1..255 for JPEG baseline compatibility.
+ */
+{
+ JQUANT_TBL ** qtblptr;
+ int i;
+ long temp;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
+ ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
+
+ qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
+
+ if (*qtblptr == NULL)
+ *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
+ /* limit the values to the valid range */
+ if (temp <= 0L) temp = 1L;
+ if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
+ if (force_baseline && temp > 255L)
+ temp = 255L; /* limit to baseline range if requested */
+ (*qtblptr)->quantval[i] = (UINT16) temp;
+ }
+
+ /* Initialize sent_table FALSE so table will be written to JPEG file. */
+ (*qtblptr)->sent_table = FALSE;
+}
+
+
+GLOBAL(void)
+jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
+ boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables
+ * and a straight percentage-scaling quality scale. In most cases it's better
+ * to use jpeg_set_quality (below); this entry point is provided for
+ * applications that insist on a linear percentage scaling.
+ */
+{
+ /* These are the sample quantization tables given in JPEG spec section K.1.
+ * The spec says that the values given produce "good" quality, and
+ * when divided by 2, "very good" quality.
+ */
+ static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
+ 16, 11, 10, 16, 24, 40, 51, 61,
+ 12, 12, 14, 19, 26, 58, 60, 55,
+ 14, 13, 16, 24, 40, 57, 69, 56,
+ 14, 17, 22, 29, 51, 87, 80, 62,
+ 18, 22, 37, 56, 68, 109, 103, 77,
+ 24, 35, 55, 64, 81, 104, 113, 92,
+ 49, 64, 78, 87, 103, 121, 120, 101,
+ 72, 92, 95, 98, 112, 100, 103, 99
+ };
+ static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
+ 17, 18, 24, 47, 99, 99, 99, 99,
+ 18, 21, 26, 66, 99, 99, 99, 99,
+ 24, 26, 56, 99, 99, 99, 99, 99,
+ 47, 66, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99
+ };
+
+ /* Set up two quantization tables using the specified scaling */
+ jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
+ scale_factor, force_baseline);
+ jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
+ scale_factor, force_baseline);
+}
+
+
+GLOBAL(int)
+jpeg_quality_scaling (int quality)
+/* Convert a user-specified quality rating to a percentage scaling factor
+ * for an underlying quantization table, using our recommended scaling curve.
+ * The input 'quality' factor should be 0 (terrible) to 100 (very good).
+ */
+{
+ /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
+ if (quality <= 0) quality = 1;
+ if (quality > 100) quality = 100;
+
+ /* The basic table is used as-is (scaling 100) for a quality of 50.
+ * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
+ * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
+ * to make all the table entries 1 (hence, minimum quantization loss).
+ * Qualities 1..50 are converted to scaling percentage 5000/Q.
+ */
+ if (quality < 50)
+ quality = 5000 / quality;
+ else
+ quality = 200 - quality*2;
+
+ return quality;
+}
+
+
+GLOBAL(void)
+jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables.
+ * This is the standard quality-adjusting entry point for typical user
+ * interfaces; only those who want detailed control over quantization tables
+ * would use the preceding three routines directly.
+ */
+{
+ /* Convert user 0-100 rating to percentage scaling */
+ quality = jpeg_quality_scaling(quality);
+
+ /* Set up standard quality tables */
+ jpeg_set_linear_quality(cinfo, quality, force_baseline);
+}
+
+
+/*
+ * Huffman table setup routines
+ */
+
+LOCAL(void)
+add_huff_table (j_compress_ptr cinfo,
+ JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
+/* Define a Huffman table */
+{
+ int nsymbols, len;
+
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+
+ /* Copy the number-of-symbols-of-each-code-length counts */
+ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+
+ /* Validate the counts. We do this here mainly so we can copy the right
+ * number of symbols from the val[] array, without risking marching off
+ * the end of memory. jchuff.c will do a more thorough test later.
+ */
+ nsymbols = 0;
+ for (len = 1; len <= 16; len++)
+ nsymbols += bits[len];
+ if (nsymbols < 1 || nsymbols > 256)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+
+ MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
+
+ /* Initialize sent_table FALSE so table will be written to JPEG file. */
+ (*htblptr)->sent_table = FALSE;
+}
+
+
+LOCAL(void)
+std_huff_tables (j_compress_ptr cinfo)
+/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
+/* IMPORTANT: these are only valid for 8-bit data precision! */
+{
+ static const UINT8 bits_dc_luminance[17] =
+ { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
+ static const UINT8 val_dc_luminance[] =
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+
+ static const UINT8 bits_dc_chrominance[17] =
+ { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
+ static const UINT8 val_dc_chrominance[] =
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+
+ static const UINT8 bits_ac_luminance[17] =
+ { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
+ static const UINT8 val_ac_luminance[] =
+ { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
+ 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
+ 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
+ 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
+ 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
+ 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
+ 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+ 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
+ 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+ 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
+ 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
+ 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
+ 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
+ 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
+ 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
+ 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
+ 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
+ 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
+ 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+ 0xf9, 0xfa };
+
+ static const UINT8 bits_ac_chrominance[17] =
+ { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
+ static const UINT8 val_ac_chrominance[] =
+ { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
+ 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
+ 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
+ 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
+ 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
+ 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
+ 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
+ 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
+ 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+ 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+ 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+ 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
+ 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
+ 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
+ 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
+ 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
+ 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
+ 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
+ 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+ 0xf9, 0xfa };
+
+ add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
+ bits_dc_luminance, val_dc_luminance);
+ add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
+ bits_ac_luminance, val_ac_luminance);
+ add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
+ bits_dc_chrominance, val_dc_chrominance);
+ add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
+ bits_ac_chrominance, val_ac_chrominance);
+}
+
+
+/*
+ * Default parameter setup for compression.
+ *
+ * Applications that don't choose to use this routine must do their
+ * own setup of all these parameters. Alternately, you can call this
+ * to establish defaults and then alter parameters selectively. This
+ * is the recommended approach since, if we add any new parameters,
+ * your code will still work (they'll be set to reasonable defaults).
+ */
+
+GLOBAL(void)
+jpeg_set_defaults (j_compress_ptr cinfo)
+{
+ int i;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* Allocate comp_info array large enough for maximum component count.
+ * Array is made permanent in case application wants to compress
+ * multiple images at same param settings.
+ */
+ if (cinfo->comp_info == NULL)
+ cinfo->comp_info = (jpeg_component_info *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ MAX_COMPONENTS * SIZEOF(jpeg_component_info));
+
+ /* Initialize everything not dependent on the color space */
+
+ cinfo->data_precision = BITS_IN_JSAMPLE;
+ /* Set up two quantization tables using default quality of 75 */
+ jpeg_set_quality(cinfo, 75, TRUE);
+ /* Set up two Huffman tables */
+ std_huff_tables(cinfo);
+
+ /* Initialize default arithmetic coding conditioning */
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ cinfo->arith_dc_L[i] = 0;
+ cinfo->arith_dc_U[i] = 1;
+ cinfo->arith_ac_K[i] = 5;
+ }
+
+ /* Default is no multiple-scan output */
+ cinfo->scan_info = NULL;
+ cinfo->num_scans = 0;
+
+ /* Expect normal source image, not raw downsampled data */
+ cinfo->raw_data_in = FALSE;
+
+ /* Use Huffman coding, not arithmetic coding, by default */
+ cinfo->arith_code = FALSE;
+
+ /* By default, don't do extra passes to optimize entropy coding */
+ cinfo->optimize_coding = FALSE;
+ /* The standard Huffman tables are only valid for 8-bit data precision.
+ * If the precision is higher, force optimization on so that usable
+ * tables will be computed. This test can be removed if default tables
+ * are supplied that are valid for the desired precision.
+ */
+ if (cinfo->data_precision > 8)
+ cinfo->optimize_coding = TRUE;
+
+ /* By default, use the simpler non-cosited sampling alignment */
+ cinfo->CCIR601_sampling = FALSE;
+
+ /* No input smoothing */
+ cinfo->smoothing_factor = 0;
+
+ /* DCT algorithm preference */
+ cinfo->dct_method = JDCT_DEFAULT;
+
+ /* No restart markers */
+ cinfo->restart_interval = 0;
+ cinfo->restart_in_rows = 0;
+
+ /* Fill in default JFIF marker parameters. Note that whether the marker
+ * will actually be written is determined by jpeg_set_colorspace.
+ *
+ * By default, the library emits JFIF version code 1.01.
+ * An application that wants to emit JFIF 1.02 extension markers should set
+ * JFIF_minor_version to 2. We could probably get away with just defaulting
+ * to 1.02, but there may still be some decoders in use that will complain
+ * about that; saying 1.01 should minimize compatibility problems.
+ */
+ cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
+ cinfo->JFIF_minor_version = 1;
+ cinfo->density_unit = 0; /* Pixel size is unknown by default */
+ cinfo->X_density = 1; /* Pixel aspect ratio is square by default */
+ cinfo->Y_density = 1;
+
+ /* Choose JPEG colorspace based on input space, set defaults accordingly */
+
+ jpeg_default_colorspace(cinfo);
+}
+
+
+/*
+ * Select an appropriate JPEG colorspace for in_color_space.
+ */
+
+GLOBAL(void)
+jpeg_default_colorspace (j_compress_ptr cinfo)
+{
+ switch (cinfo->in_color_space) {
+ case JCS_GRAYSCALE:
+ jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
+ break;
+ case JCS_RGB:
+ jpeg_set_colorspace(cinfo, JCS_YCbCr);
+ break;
+ case JCS_YCbCr:
+ jpeg_set_colorspace(cinfo, JCS_YCbCr);
+ break;
+ case JCS_CMYK:
+ jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
+ break;
+ case JCS_YCCK:
+ jpeg_set_colorspace(cinfo, JCS_YCCK);
+ break;
+ case JCS_UNKNOWN:
+ jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ }
+}
+
+
+/*
+ * Set the JPEG colorspace, and choose colorspace-dependent default values.
+ */
+
+GLOBAL(void)
+jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
+{
+ jpeg_component_info * compptr;
+ int ci;
+
+#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
+ (compptr = &cinfo->comp_info[index], \
+ compptr->component_id = (id), \
+ compptr->h_samp_factor = (hsamp), \
+ compptr->v_samp_factor = (vsamp), \
+ compptr->quant_tbl_no = (quant), \
+ compptr->dc_tbl_no = (dctbl), \
+ compptr->ac_tbl_no = (actbl) )
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
+ * tables 1 for chrominance components.
+ */
+
+ cinfo->jpeg_color_space = colorspace;
+
+ cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
+ cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
+
+ switch (colorspace) {
+ case JCS_GRAYSCALE:
+ cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+ cinfo->num_components = 1;
+ /* JFIF specifies component ID 1 */
+ SET_COMP(0, 1, 1,1, 0, 0,0);
+ break;
+ case JCS_RGB:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
+ cinfo->num_components = 3;
+ SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
+ SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
+ SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
+ break;
+ case JCS_YCbCr:
+ cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+ cinfo->num_components = 3;
+ /* JFIF specifies component IDs 1,2,3 */
+ /* We default to 2x2 subsamples of chrominance */
+ SET_COMP(0, 1, 2,2, 0, 0,0);
+ SET_COMP(1, 2, 1,1, 1, 1,1);
+ SET_COMP(2, 3, 1,1, 1, 1,1);
+ break;
+ case JCS_CMYK:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
+ cinfo->num_components = 4;
+ SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
+ SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
+ SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
+ SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
+ break;
+ case JCS_YCCK:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
+ cinfo->num_components = 4;
+ SET_COMP(0, 1, 2,2, 0, 0,0);
+ SET_COMP(1, 2, 1,1, 1, 1,1);
+ SET_COMP(2, 3, 1,1, 1, 1,1);
+ SET_COMP(3, 4, 2,2, 0, 0,0);
+ break;
+ case JCS_UNKNOWN:
+ cinfo->num_components = cinfo->input_components;
+ if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ SET_COMP(ci, ci, 1,1, 0, 0,0);
+ }
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ }
+}
+
+
+#ifdef C_PROGRESSIVE_SUPPORTED
+
+LOCAL(jpeg_scan_info *)
+fill_a_scan (jpeg_scan_info * scanptr, int ci,
+ int Ss, int Se, int Ah, int Al)
+/* Support routine: generate one scan for specified component */
+{
+ scanptr->comps_in_scan = 1;
+ scanptr->component_index[0] = ci;
+ scanptr->Ss = Ss;
+ scanptr->Se = Se;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ return scanptr;
+}
+
+LOCAL(jpeg_scan_info *)
+fill_scans (jpeg_scan_info * scanptr, int ncomps,
+ int Ss, int Se, int Ah, int Al)
+/* Support routine: generate one scan for each component */
+{
+ int ci;
+
+ for (ci = 0; ci < ncomps; ci++) {
+ scanptr->comps_in_scan = 1;
+ scanptr->component_index[0] = ci;
+ scanptr->Ss = Ss;
+ scanptr->Se = Se;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ }
+ return scanptr;
+}
+
+LOCAL(jpeg_scan_info *)
+fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
+/* Support routine: generate interleaved DC scan if possible, else N scans */
+{
+ int ci;
+
+ if (ncomps <= MAX_COMPS_IN_SCAN) {
+ /* Single interleaved DC scan */
+ scanptr->comps_in_scan = ncomps;
+ for (ci = 0; ci < ncomps; ci++)
+ scanptr->component_index[ci] = ci;
+ scanptr->Ss = scanptr->Se = 0;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ } else {
+ /* Noninterleaved DC scan for each component */
+ scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
+ }
+ return scanptr;
+}
+
+
+/*
+ * Create a recommended progressive-JPEG script.
+ * cinfo->num_components and cinfo->jpeg_color_space must be correct.
+ */
+
+GLOBAL(void)
+jpeg_simple_progression (j_compress_ptr cinfo)
+{
+ int ncomps = cinfo->num_components;
+ int nscans;
+ jpeg_scan_info * scanptr;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* Figure space needed for script. Calculation must match code below! */
+ if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
+ /* Custom script for YCbCr color images. */
+ nscans = 10;
+ } else {
+ /* All-purpose script for other color spaces. */
+ if (ncomps > MAX_COMPS_IN_SCAN)
+ nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */
+ else
+ nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */
+ }
+
+ /* Allocate space for script.
+ * We need to put it in the permanent pool in case the application performs
+ * multiple compressions without changing the settings. To avoid a memory
+ * leak if jpeg_simple_progression is called repeatedly for the same JPEG
+ * object, we try to re-use previously allocated space, and we allocate
+ * enough space to handle YCbCr even if initially asked for grayscale.
+ */
+ if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
+ cinfo->script_space_size = MAX(nscans, 10);
+ cinfo->script_space = (jpeg_scan_info *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ cinfo->script_space_size * SIZEOF(jpeg_scan_info));
+ }
+ scanptr = cinfo->script_space;
+ cinfo->scan_info = scanptr;
+ cinfo->num_scans = nscans;
+
+ if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
+ /* Custom script for YCbCr color images. */
+ /* Initial DC scan */
+ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
+ /* Initial AC scan: get some luma data out in a hurry */
+ scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
+ /* Chroma data is too small to be worth expending many scans on */
+ scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
+ scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
+ /* Complete spectral selection for luma AC */
+ scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
+ /* Refine next bit of luma AC */
+ scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
+ /* Finish DC successive approximation */
+ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
+ /* Finish AC successive approximation */
+ scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
+ scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
+ /* Luma bottom bit comes last since it's usually largest scan */
+ scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
+ } else {
+ /* All-purpose script for other color spaces. */
+ /* Successive approximation first pass */
+ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
+ scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
+ scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
+ /* Successive approximation second pass */
+ scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
+ /* Successive approximation final pass */
+ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
+ scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
+ }
+}
+
+#endif /* C_PROGRESSIVE_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jcphuff.c b/test/monniaux/jpeg-6b/jcphuff.c
new file mode 100644
index 00000000..07f9178b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcphuff.c
@@ -0,0 +1,833 @@
+/*
+ * jcphuff.c
+ *
+ * Copyright (C) 1995-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains Huffman entropy encoding routines for progressive JPEG.
+ *
+ * We do not support output suspension in this module, since the library
+ * currently does not allow multiple-scan files to be written with output
+ * suspension.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jchuff.h" /* Declarations shared with jchuff.c */
+
+#ifdef C_PROGRESSIVE_SUPPORTED
+
+/* Expanded entropy encoder object for progressive Huffman encoding. */
+
+typedef struct {
+ struct jpeg_entropy_encoder pub; /* public fields */
+
+ /* Mode flag: TRUE for optimization, FALSE for actual data output */
+ boolean gather_statistics;
+
+ /* Bit-level coding status.
+ * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
+ */
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+ INT32 put_buffer; /* current bit-accumulation buffer */
+ int put_bits; /* # of bits now in it */
+ j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
+
+ /* Coding status for DC components */
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+
+ /* Coding status for AC components */
+ int ac_tbl_no; /* the table number of the single component */
+ unsigned int EOBRUN; /* run length of EOBs */
+ unsigned int BE; /* # of buffered correction bits before MCU */
+ char * bit_buffer; /* buffer for correction bits (1 per char) */
+ /* packing correction bits tightly would save some space but cost time... */
+
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
+ int next_restart_num; /* next restart number to write (0-7) */
+
+ /* Pointers to derived tables (these workspaces have image lifespan).
+ * Since any one scan codes only DC or only AC, we only need one set
+ * of tables, not one for DC and one for AC.
+ */
+ c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
+
+ /* Statistics tables for optimization; again, one set is enough */
+ long * count_ptrs[NUM_HUFF_TBLS];
+} phuff_entropy_encoder;
+
+typedef phuff_entropy_encoder * phuff_entropy_ptr;
+
+/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
+ * buffer can hold. Larger sizes may slightly improve compression, but
+ * 1000 is already well into the realm of overkill.
+ * The minimum safe size is 64 bits.
+ */
+
+#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
+
+/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
+ * We assume that int right shift is unsigned if INT32 right shift is,
+ * which should be safe.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define ISHIFT_TEMPS int ishift_temp;
+#define IRIGHT_SHIFT(x,shft) \
+ ((ishift_temp = (x)) < 0 ? \
+ (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
+ (ishift_temp >> (shft)))
+#else
+#define ISHIFT_TEMPS
+#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
+#endif
+
+/* Forward declarations */
+METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
+METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
+
+
+/*
+ * Initialize for a Huffman-compressed scan using progressive JPEG.
+ */
+
+METHODDEF(void)
+start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ boolean is_DC_band;
+ int ci, tbl;
+ jpeg_component_info * compptr;
+
+ entropy->cinfo = cinfo;
+ entropy->gather_statistics = gather_statistics;
+
+ is_DC_band = (cinfo->Ss == 0);
+
+ /* We assume jcmaster.c already validated the scan parameters. */
+
+ /* Select execution routines */
+ if (cinfo->Ah == 0) {
+ if (is_DC_band)
+ entropy->pub.encode_mcu = encode_mcu_DC_first;
+ else
+ entropy->pub.encode_mcu = encode_mcu_AC_first;
+ } else {
+ if (is_DC_band)
+ entropy->pub.encode_mcu = encode_mcu_DC_refine;
+ else {
+ entropy->pub.encode_mcu = encode_mcu_AC_refine;
+ /* AC refinement needs a correction bit buffer */
+ if (entropy->bit_buffer == NULL)
+ entropy->bit_buffer = (char *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ MAX_CORR_BITS * SIZEOF(char));
+ }
+ }
+ if (gather_statistics)
+ entropy->pub.finish_pass = finish_pass_gather_phuff;
+ else
+ entropy->pub.finish_pass = finish_pass_phuff;
+
+ /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
+ * for AC coefficients.
+ */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Initialize DC predictions to 0 */
+ entropy->last_dc_val[ci] = 0;
+ /* Get table index */
+ if (is_DC_band) {
+ if (cinfo->Ah != 0) /* DC refinement needs no table */
+ continue;
+ tbl = compptr->dc_tbl_no;
+ } else {
+ entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
+ }
+ if (gather_statistics) {
+ /* Check for invalid table index */
+ /* (make_c_derived_tbl does this in the other path) */
+ if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
+ /* Allocate and zero the statistics tables */
+ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
+ if (entropy->count_ptrs[tbl] == NULL)
+ entropy->count_ptrs[tbl] = (long *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 257 * SIZEOF(long));
+ MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
+ } else {
+ /* Compute derived values for Huffman table */
+ /* We may do this more than once for a table, but it's not expensive */
+ jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
+ & entropy->derived_tbls[tbl]);
+ }
+ }
+
+ /* Initialize AC stuff */
+ entropy->EOBRUN = 0;
+ entropy->BE = 0;
+
+ /* Initialize bit buffer to empty */
+ entropy->put_buffer = 0;
+ entropy->put_bits = 0;
+
+ /* Initialize restart stuff */
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num = 0;
+}
+
+
+/* Outputting bytes to the file.
+ * NB: these must be called only when actually outputting,
+ * that is, entropy->gather_statistics == FALSE.
+ */
+
+/* Emit a byte */
+#define emit_byte(entropy,val) \
+ { *(entropy)->next_output_byte++ = (JOCTET) (val); \
+ if (--(entropy)->free_in_buffer == 0) \
+ dump_buffer(entropy); }
+
+
+LOCAL(void)
+dump_buffer (phuff_entropy_ptr entropy)
+/* Empty the output buffer; we do not support suspension in this module. */
+{
+ struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
+
+ if (! (*dest->empty_output_buffer) (entropy->cinfo))
+ ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
+ /* After a successful buffer dump, must reset buffer pointers */
+ entropy->next_output_byte = dest->next_output_byte;
+ entropy->free_in_buffer = dest->free_in_buffer;
+}
+
+
+/* Outputting bits to the file */
+
+/* Only the right 24 bits of put_buffer are used; the valid bits are
+ * left-justified in this part. At most 16 bits can be passed to emit_bits
+ * in one call, and we never retain more than 7 bits in put_buffer
+ * between calls, so 24 bits are sufficient.
+ */
+
+INLINE
+LOCAL(void)
+emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
+/* Emit some bits, unless we are in gather mode */
+{
+ /* This routine is heavily used, so it's worth coding tightly. */
+ register INT32 put_buffer = (INT32) code;
+ register int put_bits = entropy->put_bits;
+
+ /* if size is 0, caller used an invalid Huffman table entry */
+ if (size == 0)
+ ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
+
+ if (entropy->gather_statistics)
+ return; /* do nothing if we're only getting stats */
+
+ put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
+
+ put_bits += size; /* new number of bits in buffer */
+
+ put_buffer <<= 24 - put_bits; /* align incoming bits */
+
+ put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
+
+ while (put_bits >= 8) {
+ int c = (int) ((put_buffer >> 16) & 0xFF);
+
+ emit_byte(entropy, c);
+ if (c == 0xFF) { /* need to stuff a zero byte? */
+ emit_byte(entropy, 0);
+ }
+ put_buffer <<= 8;
+ put_bits -= 8;
+ }
+
+ entropy->put_buffer = put_buffer; /* update variables */
+ entropy->put_bits = put_bits;
+}
+
+
+LOCAL(void)
+flush_bits (phuff_entropy_ptr entropy)
+{
+ emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
+ entropy->put_buffer = 0; /* and reset bit-buffer to empty */
+ entropy->put_bits = 0;
+}
+
+
+/*
+ * Emit (or just count) a Huffman symbol.
+ */
+
+INLINE
+LOCAL(void)
+emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
+{
+ if (entropy->gather_statistics)
+ entropy->count_ptrs[tbl_no][symbol]++;
+ else {
+ c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
+ emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
+ }
+}
+
+
+/*
+ * Emit bits from a correction bit buffer.
+ */
+
+LOCAL(void)
+emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
+ unsigned int nbits)
+{
+ if (entropy->gather_statistics)
+ return; /* no real work */
+
+ while (nbits > 0) {
+ emit_bits(entropy, (unsigned int) (*bufstart), 1);
+ bufstart++;
+ nbits--;
+ }
+}
+
+
+/*
+ * Emit any pending EOBRUN symbol.
+ */
+
+LOCAL(void)
+emit_eobrun (phuff_entropy_ptr entropy)
+{
+ register int temp, nbits;
+
+ if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
+ temp = entropy->EOBRUN;
+ nbits = 0;
+ while ((temp >>= 1))
+ nbits++;
+ /* safety check: shouldn't happen given limited correction-bit buffer */
+ if (nbits > 14)
+ ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
+
+ emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
+ if (nbits)
+ emit_bits(entropy, entropy->EOBRUN, nbits);
+
+ entropy->EOBRUN = 0;
+
+ /* Emit any buffered correction bits */
+ emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
+ entropy->BE = 0;
+ }
+}
+
+
+/*
+ * Emit a restart marker & resynchronize predictions.
+ */
+
+LOCAL(void)
+emit_restart (phuff_entropy_ptr entropy, int restart_num)
+{
+ int ci;
+
+ emit_eobrun(entropy);
+
+ if (! entropy->gather_statistics) {
+ flush_bits(entropy);
+ emit_byte(entropy, 0xFF);
+ emit_byte(entropy, JPEG_RST0 + restart_num);
+ }
+
+ if (entropy->cinfo->Ss == 0) {
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
+ entropy->last_dc_val[ci] = 0;
+ } else {
+ /* Re-initialize all AC-related fields to 0 */
+ entropy->EOBRUN = 0;
+ entropy->BE = 0;
+ }
+}
+
+
+/*
+ * MCU encoding for DC initial scan (either spectral selection,
+ * or first pass of successive approximation).
+ */
+
+METHODDEF(boolean)
+encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ register int temp, temp2;
+ register int nbits;
+ int blkn, ci;
+ int Al = cinfo->Al;
+ JBLOCKROW block;
+ jpeg_component_info * compptr;
+ ISHIFT_TEMPS
+
+ entropy->next_output_byte = cinfo->dest->next_output_byte;
+ entropy->free_in_buffer = cinfo->dest->free_in_buffer;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval)
+ if (entropy->restarts_to_go == 0)
+ emit_restart(entropy, entropy->next_restart_num);
+
+ /* Encode the MCU data blocks */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ block = MCU_data[blkn];
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+
+ /* Compute the DC value after the required point transform by Al.
+ * This is simply an arithmetic right shift.
+ */
+ temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
+
+ /* DC differences are figured on the point-transformed values. */
+ temp = temp2 - entropy->last_dc_val[ci];
+ entropy->last_dc_val[ci] = temp2;
+
+ /* Encode the DC coefficient difference per section G.1.2.1 */
+ temp2 = temp;
+ if (temp < 0) {
+ temp = -temp; /* temp is abs value of input */
+ /* For a negative input, want temp2 = bitwise complement of abs(input) */
+ /* This code assumes we are on a two's complement machine */
+ temp2--;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ while (temp) {
+ nbits++;
+ temp >>= 1;
+ }
+ /* Check for out-of-range coefficient values.
+ * Since we're encoding a difference, the range limit is twice as much.
+ */
+ if (nbits > MAX_COEF_BITS+1)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+
+ /* Count/emit the Huffman-coded symbol for the number of bits */
+ emit_symbol(entropy, compptr->dc_tbl_no, nbits);
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ if (nbits) /* emit_bits rejects calls with size 0 */
+ emit_bits(entropy, (unsigned int) temp2, nbits);
+ }
+
+ cinfo->dest->next_output_byte = entropy->next_output_byte;
+ cinfo->dest->free_in_buffer = entropy->free_in_buffer;
+
+ /* Update restart-interval state too */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num++;
+ entropy->next_restart_num &= 7;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * MCU encoding for AC initial scan (either spectral selection,
+ * or first pass of successive approximation).
+ */
+
+METHODDEF(boolean)
+encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ register int temp, temp2;
+ register int nbits;
+ register int r, k;
+ int Se = cinfo->Se;
+ int Al = cinfo->Al;
+ JBLOCKROW block;
+
+ entropy->next_output_byte = cinfo->dest->next_output_byte;
+ entropy->free_in_buffer = cinfo->dest->free_in_buffer;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval)
+ if (entropy->restarts_to_go == 0)
+ emit_restart(entropy, entropy->next_restart_num);
+
+ /* Encode the MCU data block */
+ block = MCU_data[0];
+
+ /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
+
+ r = 0; /* r = run length of zeros */
+
+ for (k = cinfo->Ss; k <= Se; k++) {
+ if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
+ r++;
+ continue;
+ }
+ /* We must apply the point transform by Al. For AC coefficients this
+ * is an integer division with rounding towards 0. To do this portably
+ * in C, we shift after obtaining the absolute value; so the code is
+ * interwoven with finding the abs value (temp) and output bits (temp2).
+ */
+ if (temp < 0) {
+ temp = -temp; /* temp is abs value of input */
+ temp >>= Al; /* apply the point transform */
+ /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
+ temp2 = ~temp;
+ } else {
+ temp >>= Al; /* apply the point transform */
+ temp2 = temp;
+ }
+ /* Watch out for case that nonzero coef is zero after point transform */
+ if (temp == 0) {
+ r++;
+ continue;
+ }
+
+ /* Emit any pending EOBRUN */
+ if (entropy->EOBRUN > 0)
+ emit_eobrun(entropy);
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
+ r -= 16;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 1; /* there must be at least one 1 bit */
+ while ((temp >>= 1))
+ nbits++;
+ /* Check for out-of-range coefficient values */
+ if (nbits > MAX_COEF_BITS)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+
+ /* Count/emit Huffman symbol for run length / number of bits */
+ emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ emit_bits(entropy, (unsigned int) temp2, nbits);
+
+ r = 0; /* reset zero run length */
+ }
+
+ if (r > 0) { /* If there are trailing zeroes, */
+ entropy->EOBRUN++; /* count an EOB */
+ if (entropy->EOBRUN == 0x7FFF)
+ emit_eobrun(entropy); /* force it out to avoid overflow */
+ }
+
+ cinfo->dest->next_output_byte = entropy->next_output_byte;
+ cinfo->dest->free_in_buffer = entropy->free_in_buffer;
+
+ /* Update restart-interval state too */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num++;
+ entropy->next_restart_num &= 7;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * MCU encoding for DC successive approximation refinement scan.
+ * Note: we assume such scans can be multi-component, although the spec
+ * is not very clear on the point.
+ */
+
+METHODDEF(boolean)
+encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ register int temp;
+ int blkn;
+ int Al = cinfo->Al;
+ JBLOCKROW block;
+
+ entropy->next_output_byte = cinfo->dest->next_output_byte;
+ entropy->free_in_buffer = cinfo->dest->free_in_buffer;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval)
+ if (entropy->restarts_to_go == 0)
+ emit_restart(entropy, entropy->next_restart_num);
+
+ /* Encode the MCU data blocks */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ block = MCU_data[blkn];
+
+ /* We simply emit the Al'th bit of the DC coefficient value. */
+ temp = (*block)[0];
+ emit_bits(entropy, (unsigned int) (temp >> Al), 1);
+ }
+
+ cinfo->dest->next_output_byte = entropy->next_output_byte;
+ cinfo->dest->free_in_buffer = entropy->free_in_buffer;
+
+ /* Update restart-interval state too */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num++;
+ entropy->next_restart_num &= 7;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * MCU encoding for AC successive approximation refinement scan.
+ */
+
+METHODDEF(boolean)
+encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ register int temp;
+ register int r, k;
+ int EOB;
+ char *BR_buffer;
+ unsigned int BR;
+ int Se = cinfo->Se;
+ int Al = cinfo->Al;
+ JBLOCKROW block;
+ int absvalues[DCTSIZE2];
+
+ entropy->next_output_byte = cinfo->dest->next_output_byte;
+ entropy->free_in_buffer = cinfo->dest->free_in_buffer;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval)
+ if (entropy->restarts_to_go == 0)
+ emit_restart(entropy, entropy->next_restart_num);
+
+ /* Encode the MCU data block */
+ block = MCU_data[0];
+
+ /* It is convenient to make a pre-pass to determine the transformed
+ * coefficients' absolute values and the EOB position.
+ */
+ EOB = 0;
+ for (k = cinfo->Ss; k <= Se; k++) {
+ temp = (*block)[jpeg_natural_order[k]];
+ /* We must apply the point transform by Al. For AC coefficients this
+ * is an integer division with rounding towards 0. To do this portably
+ * in C, we shift after obtaining the absolute value.
+ */
+ if (temp < 0)
+ temp = -temp; /* temp is abs value of input */
+ temp >>= Al; /* apply the point transform */
+ absvalues[k] = temp; /* save abs value for main pass */
+ if (temp == 1)
+ EOB = k; /* EOB = index of last newly-nonzero coef */
+ }
+
+ /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
+
+ r = 0; /* r = run length of zeros */
+ BR = 0; /* BR = count of buffered bits added now */
+ BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
+
+ for (k = cinfo->Ss; k <= Se; k++) {
+ if ((temp = absvalues[k]) == 0) {
+ r++;
+ continue;
+ }
+
+ /* Emit any required ZRLs, but not if they can be folded into EOB */
+ while (r > 15 && k <= EOB) {
+ /* emit any pending EOBRUN and the BE correction bits */
+ emit_eobrun(entropy);
+ /* Emit ZRL */
+ emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
+ r -= 16;
+ /* Emit buffered correction bits that must be associated with ZRL */
+ emit_buffered_bits(entropy, BR_buffer, BR);
+ BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
+ BR = 0;
+ }
+
+ /* If the coef was previously nonzero, it only needs a correction bit.
+ * NOTE: a straight translation of the spec's figure G.7 would suggest
+ * that we also need to test r > 15. But if r > 15, we can only get here
+ * if k > EOB, which implies that this coefficient is not 1.
+ */
+ if (temp > 1) {
+ /* The correction bit is the next bit of the absolute value. */
+ BR_buffer[BR++] = (char) (temp & 1);
+ continue;
+ }
+
+ /* Emit any pending EOBRUN and the BE correction bits */
+ emit_eobrun(entropy);
+
+ /* Count/emit Huffman symbol for run length / number of bits */
+ emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
+
+ /* Emit output bit for newly-nonzero coef */
+ temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
+ emit_bits(entropy, (unsigned int) temp, 1);
+
+ /* Emit buffered correction bits that must be associated with this code */
+ emit_buffered_bits(entropy, BR_buffer, BR);
+ BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
+ BR = 0;
+ r = 0; /* reset zero run length */
+ }
+
+ if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
+ entropy->EOBRUN++; /* count an EOB */
+ entropy->BE += BR; /* concat my correction bits to older ones */
+ /* We force out the EOB if we risk either:
+ * 1. overflow of the EOB counter;
+ * 2. overflow of the correction bit buffer during the next MCU.
+ */
+ if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
+ emit_eobrun(entropy);
+ }
+
+ cinfo->dest->next_output_byte = entropy->next_output_byte;
+ cinfo->dest->free_in_buffer = entropy->free_in_buffer;
+
+ /* Update restart-interval state too */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num++;
+ entropy->next_restart_num &= 7;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * Finish up at the end of a Huffman-compressed progressive scan.
+ */
+
+METHODDEF(void)
+finish_pass_phuff (j_compress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+
+ entropy->next_output_byte = cinfo->dest->next_output_byte;
+ entropy->free_in_buffer = cinfo->dest->free_in_buffer;
+
+ /* Flush out any buffered data */
+ emit_eobrun(entropy);
+ flush_bits(entropy);
+
+ cinfo->dest->next_output_byte = entropy->next_output_byte;
+ cinfo->dest->free_in_buffer = entropy->free_in_buffer;
+}
+
+
+/*
+ * Finish up a statistics-gathering pass and create the new Huffman tables.
+ */
+
+METHODDEF(void)
+finish_pass_gather_phuff (j_compress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ boolean is_DC_band;
+ int ci, tbl;
+ jpeg_component_info * compptr;
+ JHUFF_TBL **htblptr;
+ boolean did[NUM_HUFF_TBLS];
+
+ /* Flush out buffered data (all we care about is counting the EOB symbol) */
+ emit_eobrun(entropy);
+
+ is_DC_band = (cinfo->Ss == 0);
+
+ /* It's important not to apply jpeg_gen_optimal_table more than once
+ * per table, because it clobbers the input frequency counts!
+ */
+ MEMZERO(did, SIZEOF(did));
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ if (is_DC_band) {
+ if (cinfo->Ah != 0) /* DC refinement needs no table */
+ continue;
+ tbl = compptr->dc_tbl_no;
+ } else {
+ tbl = compptr->ac_tbl_no;
+ }
+ if (! did[tbl]) {
+ if (is_DC_band)
+ htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
+ else
+ htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
+ did[tbl] = TRUE;
+ }
+ }
+}
+
+
+/*
+ * Module initialization routine for progressive Huffman entropy encoding.
+ */
+
+GLOBAL(void)
+jinit_phuff_encoder (j_compress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy;
+ int i;
+
+ entropy = (phuff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(phuff_entropy_encoder));
+ cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
+ entropy->pub.start_pass = start_pass_phuff;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->derived_tbls[i] = NULL;
+ entropy->count_ptrs[i] = NULL;
+ }
+ entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
+}
+
+#endif /* C_PROGRESSIVE_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jcprepct.c b/test/monniaux/jpeg-6b/jcprepct.c
new file mode 100644
index 00000000..fa93333d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcprepct.c
@@ -0,0 +1,354 @@
+/*
+ * jcprepct.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the compression preprocessing controller.
+ * This controller manages the color conversion, downsampling,
+ * and edge expansion steps.
+ *
+ * Most of the complexity here is associated with buffering input rows
+ * as required by the downsampler. See the comments at the head of
+ * jcsample.c for the downsampler's needs.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* At present, jcsample.c can request context rows only for smoothing.
+ * In the future, we might also need context rows for CCIR601 sampling
+ * or other more-complex downsampling procedures. The code to support
+ * context rows should be compiled only if needed.
+ */
+#ifdef INPUT_SMOOTHING_SUPPORTED
+#define CONTEXT_ROWS_SUPPORTED
+#endif
+
+
+/*
+ * For the simple (no-context-row) case, we just need to buffer one
+ * row group's worth of pixels for the downsampling step. At the bottom of
+ * the image, we pad to a full row group by replicating the last pixel row.
+ * The downsampler's last output row is then replicated if needed to pad
+ * out to a full iMCU row.
+ *
+ * When providing context rows, we must buffer three row groups' worth of
+ * pixels. Three row groups are physically allocated, but the row pointer
+ * arrays are made five row groups high, with the extra pointers above and
+ * below "wrapping around" to point to the last and first real row groups.
+ * This allows the downsampler to access the proper context rows.
+ * At the top and bottom of the image, we create dummy context rows by
+ * copying the first or last real pixel row. This copying could be avoided
+ * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
+ * trouble on the compression side.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_prep_controller pub; /* public fields */
+
+ /* Downsampling input buffer. This buffer holds color-converted data
+ * until we have enough to do a downsample step.
+ */
+ JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+ JDIMENSION rows_to_go; /* counts rows remaining in source image */
+ int next_buf_row; /* index of next row to store in color_buf */
+
+#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
+ int this_row_group; /* starting row index of group to process */
+ int next_buf_stop; /* downsample when we reach this index */
+#endif
+} my_prep_controller;
+
+typedef my_prep_controller * my_prep_ptr;
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+
+ if (pass_mode != JBUF_PASS_THRU)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ /* Initialize total-height counter for detecting bottom of image */
+ prep->rows_to_go = cinfo->image_height;
+ /* Mark the conversion buffer empty */
+ prep->next_buf_row = 0;
+#ifdef CONTEXT_ROWS_SUPPORTED
+ /* Preset additional state variables for context mode.
+ * These aren't used in non-context mode, so we needn't test which mode.
+ */
+ prep->this_row_group = 0;
+ /* Set next_buf_stop to stop after two row groups have been read in. */
+ prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
+#endif
+}
+
+
+/*
+ * Expand an image vertically from height input_rows to height output_rows,
+ * by duplicating the bottom row.
+ */
+
+LOCAL(void)
+expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
+ int input_rows, int output_rows)
+{
+ register int row;
+
+ for (row = input_rows; row < output_rows; row++) {
+ jcopy_sample_rows(image_data, input_rows-1, image_data, row,
+ 1, num_cols);
+ }
+}
+
+
+/*
+ * Process some data in the simple no-context case.
+ *
+ * Preprocessor output data is counted in "row groups". A row group
+ * is defined to be v_samp_factor sample rows of each component.
+ * Downsampling will produce this much data from each max_v_samp_factor
+ * input rows.
+ */
+
+METHODDEF(void)
+pre_process_data (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int numrows, ci;
+ JDIMENSION inrows;
+ jpeg_component_info * compptr;
+
+ while (*in_row_ctr < in_rows_avail &&
+ *out_row_group_ctr < out_row_groups_avail) {
+ /* Do color conversion to fill the conversion buffer. */
+ inrows = in_rows_avail - *in_row_ctr;
+ numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
+ numrows = (int) MIN((JDIMENSION) numrows, inrows);
+ (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+ prep->color_buf,
+ (JDIMENSION) prep->next_buf_row,
+ numrows);
+ *in_row_ctr += numrows;
+ prep->next_buf_row += numrows;
+ prep->rows_to_go -= numrows;
+ /* If at bottom of image, pad to fill the conversion buffer. */
+ if (prep->rows_to_go == 0 &&
+ prep->next_buf_row < cinfo->max_v_samp_factor) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+ prep->next_buf_row, cinfo->max_v_samp_factor);
+ }
+ prep->next_buf_row = cinfo->max_v_samp_factor;
+ }
+ /* If we've filled the conversion buffer, empty it. */
+ if (prep->next_buf_row == cinfo->max_v_samp_factor) {
+ (*cinfo->downsample->downsample) (cinfo,
+ prep->color_buf, (JDIMENSION) 0,
+ output_buf, *out_row_group_ctr);
+ prep->next_buf_row = 0;
+ (*out_row_group_ctr)++;
+ }
+ /* If at bottom of image, pad the output to a full iMCU height.
+ * Note we assume the caller is providing a one-iMCU-height output buffer!
+ */
+ if (prep->rows_to_go == 0 &&
+ *out_row_group_ctr < out_row_groups_avail) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ expand_bottom_edge(output_buf[ci],
+ compptr->width_in_blocks * DCTSIZE,
+ (int) (*out_row_group_ctr * compptr->v_samp_factor),
+ (int) (out_row_groups_avail * compptr->v_samp_factor));
+ }
+ *out_row_group_ctr = out_row_groups_avail;
+ break; /* can exit outer loop without test */
+ }
+ }
+}
+
+
+#ifdef CONTEXT_ROWS_SUPPORTED
+
+/*
+ * Process some data in the context case.
+ */
+
+METHODDEF(void)
+pre_process_context (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int numrows, ci;
+ int buf_height = cinfo->max_v_samp_factor * 3;
+ JDIMENSION inrows;
+
+ while (*out_row_group_ctr < out_row_groups_avail) {
+ if (*in_row_ctr < in_rows_avail) {
+ /* Do color conversion to fill the conversion buffer. */
+ inrows = in_rows_avail - *in_row_ctr;
+ numrows = prep->next_buf_stop - prep->next_buf_row;
+ numrows = (int) MIN((JDIMENSION) numrows, inrows);
+ (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+ prep->color_buf,
+ (JDIMENSION) prep->next_buf_row,
+ numrows);
+ /* Pad at top of image, if first time through */
+ if (prep->rows_to_go == cinfo->image_height) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ int row;
+ for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
+ jcopy_sample_rows(prep->color_buf[ci], 0,
+ prep->color_buf[ci], -row,
+ 1, cinfo->image_width);
+ }
+ }
+ }
+ *in_row_ctr += numrows;
+ prep->next_buf_row += numrows;
+ prep->rows_to_go -= numrows;
+ } else {
+ /* Return for more data, unless we are at the bottom of the image. */
+ if (prep->rows_to_go != 0)
+ break;
+ /* When at bottom of image, pad to fill the conversion buffer. */
+ if (prep->next_buf_row < prep->next_buf_stop) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+ prep->next_buf_row, prep->next_buf_stop);
+ }
+ prep->next_buf_row = prep->next_buf_stop;
+ }
+ }
+ /* If we've gotten enough data, downsample a row group. */
+ if (prep->next_buf_row == prep->next_buf_stop) {
+ (*cinfo->downsample->downsample) (cinfo,
+ prep->color_buf,
+ (JDIMENSION) prep->this_row_group,
+ output_buf, *out_row_group_ctr);
+ (*out_row_group_ctr)++;
+ /* Advance pointers with wraparound as necessary. */
+ prep->this_row_group += cinfo->max_v_samp_factor;
+ if (prep->this_row_group >= buf_height)
+ prep->this_row_group = 0;
+ if (prep->next_buf_row >= buf_height)
+ prep->next_buf_row = 0;
+ prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
+ }
+ }
+}
+
+
+/*
+ * Create the wrapped-around downsampling input buffer needed for context mode.
+ */
+
+LOCAL(void)
+create_context_buffer (j_compress_ptr cinfo)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int rgroup_height = cinfo->max_v_samp_factor;
+ int ci, i;
+ jpeg_component_info * compptr;
+ JSAMPARRAY true_buffer, fake_buffer;
+
+ /* Grab enough space for fake row pointers for all the components;
+ * we need five row groups' worth of pointers for each component.
+ */
+ fake_buffer = (JSAMPARRAY)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (cinfo->num_components * 5 * rgroup_height) *
+ SIZEOF(JSAMPROW));
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Allocate the actual buffer space (3 row groups) for this component.
+ * We make the buffer wide enough to allow the downsampler to edge-expand
+ * horizontally within the buffer, if it so chooses.
+ */
+ true_buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+ cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+ (JDIMENSION) (3 * rgroup_height));
+ /* Copy true buffer row pointers into the middle of the fake row array */
+ MEMCOPY(fake_buffer + rgroup_height, true_buffer,
+ 3 * rgroup_height * SIZEOF(JSAMPROW));
+ /* Fill in the above and below wraparound pointers */
+ for (i = 0; i < rgroup_height; i++) {
+ fake_buffer[i] = true_buffer[2 * rgroup_height + i];
+ fake_buffer[4 * rgroup_height + i] = true_buffer[i];
+ }
+ prep->color_buf[ci] = fake_buffer + rgroup_height;
+ fake_buffer += 5 * rgroup_height; /* point to space for next component */
+ }
+}
+
+#endif /* CONTEXT_ROWS_SUPPORTED */
+
+
+/*
+ * Initialize preprocessing controller.
+ */
+
+GLOBAL(void)
+jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_prep_ptr prep;
+ int ci;
+ jpeg_component_info * compptr;
+
+ if (need_full_buffer) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ prep = (my_prep_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_prep_controller));
+ cinfo->prep = (struct jpeg_c_prep_controller *) prep;
+ prep->pub.start_pass = start_pass_prep;
+
+ /* Allocate the color conversion buffer.
+ * We make the buffer wide enough to allow the downsampler to edge-expand
+ * horizontally within the buffer, if it so chooses.
+ */
+ if (cinfo->downsample->need_context_rows) {
+ /* Set up to provide context rows */
+#ifdef CONTEXT_ROWS_SUPPORTED
+ prep->pub.pre_process_data = pre_process_context;
+ create_context_buffer(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ /* No context, just make it tall enough for one row group */
+ prep->pub.pre_process_data = pre_process_data;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+ cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+ (JDIMENSION) cinfo->max_v_samp_factor);
+ }
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jcsample.c b/test/monniaux/jpeg-6b/jcsample.c
new file mode 100644
index 00000000..212ec875
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jcsample.c
@@ -0,0 +1,519 @@
+/*
+ * jcsample.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains downsampling routines.
+ *
+ * Downsampling input data is counted in "row groups". A row group
+ * is defined to be max_v_samp_factor pixel rows of each component,
+ * from which the downsampler produces v_samp_factor sample rows.
+ * A single row group is processed in each call to the downsampler module.
+ *
+ * The downsampler is responsible for edge-expansion of its output data
+ * to fill an integral number of DCT blocks horizontally. The source buffer
+ * may be modified if it is helpful for this purpose (the source buffer is
+ * allocated wide enough to correspond to the desired output width).
+ * The caller (the prep controller) is responsible for vertical padding.
+ *
+ * The downsampler may request "context rows" by setting need_context_rows
+ * during startup. In this case, the input arrays will contain at least
+ * one row group's worth of pixels above and below the passed-in data;
+ * the caller will create dummy rows at image top and bottom by replicating
+ * the first or last real pixel row.
+ *
+ * An excellent reference for image resampling is
+ * Digital Image Warping, George Wolberg, 1990.
+ * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
+ *
+ * The downsampling algorithm used here is a simple average of the source
+ * pixels covered by the output pixel. The hi-falutin sampling literature
+ * refers to this as a "box filter". In general the characteristics of a box
+ * filter are not very good, but for the specific cases we normally use (1:1
+ * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
+ * nearly so bad. If you intend to use other sampling ratios, you'd be well
+ * advised to improve this code.
+ *
+ * A simple input-smoothing capability is provided. This is mainly intended
+ * for cleaning up color-dithered GIF input files (if you find it inadequate,
+ * we suggest using an external filtering program such as pnmconvol). When
+ * enabled, each input pixel P is replaced by a weighted sum of itself and its
+ * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
+ * where SF = (smoothing_factor / 1024).
+ * Currently, smoothing is only supported for 2h2v sampling factors.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to downsample a single component */
+typedef JMETHOD(void, downsample1_ptr,
+ (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data));
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_downsampler pub; /* public fields */
+
+ /* Downsampling method pointers, one per component */
+ downsample1_ptr methods[MAX_COMPONENTS];
+} my_downsampler;
+
+typedef my_downsampler * my_downsample_ptr;
+
+
+/*
+ * Initialize for a downsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_downsample (j_compress_ptr cinfo)
+{
+ /* no work for now */
+}
+
+
+/*
+ * Expand a component horizontally from width input_cols to width output_cols,
+ * by duplicating the rightmost samples.
+ */
+
+LOCAL(void)
+expand_right_edge (JSAMPARRAY image_data, int num_rows,
+ JDIMENSION input_cols, JDIMENSION output_cols)
+{
+ register JSAMPROW ptr;
+ register JSAMPLE pixval;
+ register int count;
+ int row;
+ int numcols = (int) (output_cols - input_cols);
+
+ if (numcols > 0) {
+ for (row = 0; row < num_rows; row++) {
+ ptr = image_data[row] + input_cols;
+ pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
+ for (count = numcols; count > 0; count--)
+ *ptr++ = pixval;
+ }
+ }
+}
+
+
+/*
+ * Do downsampling for a whole row group (all components).
+ *
+ * In this version we simply downsample each component independently.
+ */
+
+METHODDEF(void)
+sep_downsample (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+ JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
+{
+ my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
+ int ci;
+ jpeg_component_info * compptr;
+ JSAMPARRAY in_ptr, out_ptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ in_ptr = input_buf[ci] + in_row_index;
+ out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
+ (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * One row group is processed per call.
+ * This version handles arbitrary integral sampling ratios, without smoothing.
+ * Note that this version is not actually used for customary sampling ratios.
+ */
+
+METHODDEF(void)
+int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
+ JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ JSAMPROW inptr, outptr;
+ INT32 outvalue;
+
+ h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
+ v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
+ numpix = h_expand * v_expand;
+ numpix2 = numpix/2;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * h_expand);
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ for (outcol = 0, outcol_h = 0; outcol < output_cols;
+ outcol++, outcol_h += h_expand) {
+ outvalue = 0;
+ for (v = 0; v < v_expand; v++) {
+ inptr = input_data[inrow+v] + outcol_h;
+ for (h = 0; h < h_expand; h++) {
+ outvalue += (INT32) GETJSAMPLE(*inptr++);
+ }
+ }
+ *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
+ }
+ inrow += v_expand;
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the special case of a full-size component,
+ * without smoothing.
+ */
+
+METHODDEF(void)
+fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ /* Copy the data */
+ jcopy_sample_rows(input_data, 0, output_data, 0,
+ cinfo->max_v_samp_factor, cinfo->image_width);
+ /* Edge-expand */
+ expand_right_edge(output_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the common case of 2:1 horizontal and 1:1 vertical,
+ * without smoothing.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
+ */
+
+METHODDEF(void)
+h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int outrow;
+ JDIMENSION outcol;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr, outptr;
+ register int bias;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * 2);
+
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr = input_data[outrow];
+ bias = 0; /* bias = 0,1,0,1,... for successive samples */
+ for (outcol = 0; outcol < output_cols; outcol++) {
+ *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
+ + bias) >> 1);
+ bias ^= 1; /* 0=>1, 1=>0 */
+ inptr += 2;
+ }
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
+ * without smoothing.
+ */
+
+METHODDEF(void)
+h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow;
+ JDIMENSION outcol;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr0, inptr1, outptr;
+ register int bias;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * 2);
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr0 = input_data[inrow];
+ inptr1 = input_data[inrow+1];
+ bias = 1; /* bias = 1,2,1,2,... for successive samples */
+ for (outcol = 0; outcol < output_cols; outcol++) {
+ *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
+ + bias) >> 2);
+ bias ^= 3; /* 1=>2, 2=>1 */
+ inptr0 += 2; inptr1 += 2;
+ }
+ inrow += 2;
+ }
+}
+
+
+#ifdef INPUT_SMOOTHING_SUPPORTED
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
+ * with smoothing. One row of context is required.
+ */
+
+METHODDEF(void)
+h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow;
+ JDIMENSION colctr;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
+ INT32 membersum, neighsum, memberscale, neighscale;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+ cinfo->image_width, output_cols * 2);
+
+ /* We don't bother to form the individual "smoothed" input pixel values;
+ * we can directly compute the output which is the average of the four
+ * smoothed values. Each of the four member pixels contributes a fraction
+ * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
+ * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
+ * output. The four corner-adjacent neighbor pixels contribute a fraction
+ * SF to just one smoothed pixel, or SF/4 to the final output; while the
+ * eight edge-adjacent neighbors contribute SF to each of two smoothed
+ * pixels, or SF/2 overall. In order to use integer arithmetic, these
+ * factors are scaled by 2^16 = 65536.
+ * Also recall that SF = smoothing_factor / 1024.
+ */
+
+ memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
+ neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr0 = input_data[inrow];
+ inptr1 = input_data[inrow+1];
+ above_ptr = input_data[inrow-1];
+ below_ptr = input_data[inrow+2];
+
+ /* Special case for first column: pretend column -1 is same as column 0 */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
+ neighsum += neighsum;
+ neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
+
+ for (colctr = output_cols - 2; colctr > 0; colctr--) {
+ /* sum of pixels directly mapped to this output element */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ /* sum of edge-neighbor pixels */
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
+ GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
+ /* The edge-neighbors count twice as much as corner-neighbors */
+ neighsum += neighsum;
+ /* Add in the corner-neighbors */
+ neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
+ GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
+ /* form final output scaled up by 2^16 */
+ membersum = membersum * memberscale + neighsum * neighscale;
+ /* round, descale and output it */
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
+ }
+
+ /* Special case for last column */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
+ neighsum += neighsum;
+ neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
+
+ inrow += 2;
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the special case of a full-size component,
+ * with smoothing. One row of context is required.
+ */
+
+METHODDEF(void)
+fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int outrow;
+ JDIMENSION colctr;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr, above_ptr, below_ptr, outptr;
+ INT32 membersum, neighsum, memberscale, neighscale;
+ int colsum, lastcolsum, nextcolsum;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+ cinfo->image_width, output_cols);
+
+ /* Each of the eight neighbor pixels contributes a fraction SF to the
+ * smoothed pixel, while the main pixel contributes (1-8*SF). In order
+ * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
+ * Also recall that SF = smoothing_factor / 1024.
+ */
+
+ memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
+ neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
+
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr = input_data[outrow];
+ above_ptr = input_data[outrow-1];
+ below_ptr = input_data[outrow+1];
+
+ /* Special case for first column */
+ colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
+ GETJSAMPLE(*inptr);
+ membersum = GETJSAMPLE(*inptr++);
+ nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
+ GETJSAMPLE(*inptr);
+ neighsum = colsum + (colsum - membersum) + nextcolsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ lastcolsum = colsum; colsum = nextcolsum;
+
+ for (colctr = output_cols - 2; colctr > 0; colctr--) {
+ membersum = GETJSAMPLE(*inptr++);
+ above_ptr++; below_ptr++;
+ nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
+ GETJSAMPLE(*inptr);
+ neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ lastcolsum = colsum; colsum = nextcolsum;
+ }
+
+ /* Special case for last column */
+ membersum = GETJSAMPLE(*inptr);
+ neighsum = lastcolsum + (colsum - membersum) + colsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
+
+ }
+}
+
+#endif /* INPUT_SMOOTHING_SUPPORTED */
+
+
+/*
+ * Module initialization routine for downsampling.
+ * Note that we must select a routine for each component.
+ */
+
+GLOBAL(void)
+jinit_downsampler (j_compress_ptr cinfo)
+{
+ my_downsample_ptr downsample;
+ int ci;
+ jpeg_component_info * compptr;
+ boolean smoothok = TRUE;
+
+ downsample = (my_downsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_downsampler));
+ cinfo->downsample = (struct jpeg_downsampler *) downsample;
+ downsample->pub.start_pass = start_pass_downsample;
+ downsample->pub.downsample = sep_downsample;
+ downsample->pub.need_context_rows = FALSE;
+
+ if (cinfo->CCIR601_sampling)
+ ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+ /* Verify we can handle the sampling factors, and set up method pointers */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor == cinfo->max_v_samp_factor) {
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor) {
+ downsample->methods[ci] = fullsize_smooth_downsample;
+ downsample->pub.need_context_rows = TRUE;
+ } else
+#endif
+ downsample->methods[ci] = fullsize_downsample;
+ } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor == cinfo->max_v_samp_factor) {
+ smoothok = FALSE;
+ downsample->methods[ci] = h2v1_downsample;
+ } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor) {
+ downsample->methods[ci] = h2v2_smooth_downsample;
+ downsample->pub.need_context_rows = TRUE;
+ } else
+#endif
+ downsample->methods[ci] = h2v2_downsample;
+ } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
+ (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
+ smoothok = FALSE;
+ downsample->methods[ci] = int_downsample;
+ } else
+ ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
+ }
+
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor && !smoothok)
+ TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
+#endif
+}
diff --git a/test/monniaux/jpeg-6b/jctrans.c b/test/monniaux/jpeg-6b/jctrans.c
new file mode 100644
index 00000000..0e6d7076
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jctrans.c
@@ -0,0 +1,388 @@
+/*
+ * jctrans.c
+ *
+ * Copyright (C) 1995-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains library routines for transcoding compression,
+ * that is, writing raw DCT coefficient arrays to an output JPEG file.
+ * The routines in jcapimin.c will also be needed by a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(void) transencode_master_selection
+ JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+LOCAL(void) transencode_coef_controller
+ JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+
+
+/*
+ * Compression initialization for writing raw-coefficient data.
+ * Before calling this, all parameters and a data destination must be set up.
+ * Call jpeg_finish_compress() to actually write the data.
+ *
+ * The number of passed virtual arrays must match cinfo->num_components.
+ * Note that the virtual arrays need not be filled or even realized at
+ * the time write_coefficients is called; indeed, if the virtual arrays
+ * were requested from this compression object's memory manager, they
+ * typically will be realized during this routine and filled afterwards.
+ */
+
+GLOBAL(void)
+jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Mark all tables to be written */
+ jpeg_suppress_tables(cinfo, FALSE);
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Perform master selection of active modules */
+ transencode_master_selection(cinfo, coef_arrays);
+ /* Wait for jpeg_finish_compress() call */
+ cinfo->next_scanline = 0; /* so jpeg_write_marker works */
+ cinfo->global_state = CSTATE_WRCOEFS;
+}
+
+
+/*
+ * Initialize the compression object with default parameters,
+ * then copy from the source object all parameters needed for lossless
+ * transcoding. Parameters that can be varied without loss (such as
+ * scan script and Huffman optimization) are left in their default states.
+ */
+
+GLOBAL(void)
+jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo)
+{
+ JQUANT_TBL ** qtblptr;
+ jpeg_component_info *incomp, *outcomp;
+ JQUANT_TBL *c_quant, *slot_quant;
+ int tblno, ci, coefi;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (dstinfo->global_state != CSTATE_START)
+ ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
+ /* Copy fundamental image dimensions */
+ dstinfo->image_width = srcinfo->image_width;
+ dstinfo->image_height = srcinfo->image_height;
+ dstinfo->input_components = srcinfo->num_components;
+ dstinfo->in_color_space = srcinfo->jpeg_color_space;
+ /* Initialize all parameters to default values */
+ jpeg_set_defaults(dstinfo);
+ /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
+ * Fix it to get the right header markers for the image colorspace.
+ */
+ jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
+ dstinfo->data_precision = srcinfo->data_precision;
+ dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
+ /* Copy the source's quantization tables. */
+ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
+ if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
+ qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
+ if (*qtblptr == NULL)
+ *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
+ MEMCOPY((*qtblptr)->quantval,
+ srcinfo->quant_tbl_ptrs[tblno]->quantval,
+ SIZEOF((*qtblptr)->quantval));
+ (*qtblptr)->sent_table = FALSE;
+ }
+ }
+ /* Copy the source's per-component info.
+ * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
+ */
+ dstinfo->num_components = srcinfo->num_components;
+ if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
+ MAX_COMPONENTS);
+ for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
+ ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
+ outcomp->component_id = incomp->component_id;
+ outcomp->h_samp_factor = incomp->h_samp_factor;
+ outcomp->v_samp_factor = incomp->v_samp_factor;
+ outcomp->quant_tbl_no = incomp->quant_tbl_no;
+ /* Make sure saved quantization table for component matches the qtable
+ * slot. If not, the input file re-used this qtable slot.
+ * IJG encoder currently cannot duplicate this.
+ */
+ tblno = outcomp->quant_tbl_no;
+ if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
+ srcinfo->quant_tbl_ptrs[tblno] == NULL)
+ ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
+ slot_quant = srcinfo->quant_tbl_ptrs[tblno];
+ c_quant = incomp->quant_table;
+ if (c_quant != NULL) {
+ for (coefi = 0; coefi < DCTSIZE2; coefi++) {
+ if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
+ ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
+ }
+ }
+ /* Note: we do not copy the source's Huffman table assignments;
+ * instead we rely on jpeg_set_colorspace to have made a suitable choice.
+ */
+ }
+ /* Also copy JFIF version and resolution information, if available.
+ * Strictly speaking this isn't "critical" info, but it's nearly
+ * always appropriate to copy it if available. In particular,
+ * if the application chooses to copy JFIF 1.02 extension markers from
+ * the source file, we need to copy the version to make sure we don't
+ * emit a file that has 1.02 extensions but a claimed version of 1.01.
+ * We will *not*, however, copy version info from mislabeled "2.01" files.
+ */
+ if (srcinfo->saw_JFIF_marker) {
+ if (srcinfo->JFIF_major_version == 1) {
+ dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
+ dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
+ }
+ dstinfo->density_unit = srcinfo->density_unit;
+ dstinfo->X_density = srcinfo->X_density;
+ dstinfo->Y_density = srcinfo->Y_density;
+ }
+}
+
+
+/*
+ * Master selection of compression modules for transcoding.
+ * This substitutes for jcinit.c's initialization of the full compressor.
+ */
+
+LOCAL(void)
+transencode_master_selection (j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays)
+{
+ /* Although we don't actually use input_components for transcoding,
+ * jcmaster.c's initial_setup will complain if input_components is 0.
+ */
+ cinfo->input_components = 1;
+ /* Initialize master control (includes parameter checking/processing) */
+ jinit_c_master_control(cinfo, TRUE /* transcode only */);
+
+ /* Entropy encoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ jinit_phuff_encoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_encoder(cinfo);
+ }
+
+ /* We need a special coefficient buffer controller. */
+ transencode_coef_controller(cinfo, coef_arrays);
+
+ jinit_marker_writer(cinfo);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Write the datastream header (SOI, JFIF) immediately.
+ * Frame and scan headers are postponed till later.
+ * This lets application insert special markers after the SOI.
+ */
+ (*cinfo->marker->write_file_header) (cinfo);
+}
+
+
+/*
+ * The rest of this file is a special implementation of the coefficient
+ * buffer controller. This is similar to jccoefct.c, but it handles only
+ * output from presupplied virtual arrays. Furthermore, we generate any
+ * dummy padding blocks on-the-fly rather than expecting them to be present
+ * in the arrays.
+ */
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_coef_controller pub; /* public fields */
+
+ JDIMENSION iMCU_row_num; /* iMCU row # within image */
+ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* Virtual block array for each component. */
+ jvirt_barray_ptr * whole_image;
+
+ /* Workspace for constructing dummy blocks at right/bottom edges. */
+ JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+LOCAL(void)
+start_iMCU_row (j_compress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->mcu_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ if (pass_mode != JBUF_CRANK_DEST)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ coef->iMCU_row_num = 0;
+ start_iMCU_row(cinfo);
+}
+
+
+/*
+ * Process some data.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * The data is obtained from the virtual arrays and fed to the entropy coder.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
+ */
+
+METHODDEF(boolean)
+compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, ci, xindex, yindex, yoffset, blockcnt;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan. */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (coef->iMCU_row_num < last_iMCU_row ||
+ yindex+yoffset < compptr->last_row_height) {
+ /* Fill in pointers to real blocks in this row */
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < blockcnt; xindex++)
+ MCU_buffer[blkn++] = buffer_ptr++;
+ } else {
+ /* At bottom of image, need a whole row of dummy blocks */
+ xindex = 0;
+ }
+ /* Fill in any dummy blocks needed in this row.
+ * Dummy blocks are filled in the same way as in jccoefct.c:
+ * all zeroes in the AC entries, DC entries equal to previous
+ * block's DC value. The init routine has already zeroed the
+ * AC entries, so we need only set the DC entries correctly.
+ */
+ for (; xindex < compptr->MCU_width; xindex++) {
+ MCU_buffer[blkn] = coef->dummy_buffer[blkn];
+ MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
+ blkn++;
+ }
+ }
+ }
+ /* Try to write the MCU. */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Initialize coefficient buffer controller.
+ *
+ * Each passed coefficient array must be the right size for that
+ * coefficient: width_in_blocks wide and height_in_blocks high,
+ * with unitheight at least v_samp_factor.
+ */
+
+LOCAL(void)
+transencode_coef_controller (j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays)
+{
+ my_coef_ptr coef;
+ JBLOCKROW buffer;
+ int i;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_c_coef_controller *) coef;
+ coef->pub.start_pass = start_pass_coef;
+ coef->pub.compress_data = compress_output;
+
+ /* Save pointer to virtual arrays */
+ coef->whole_image = coef_arrays;
+
+ /* Allocate and pre-zero space for dummy DCT blocks. */
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
+ coef->dummy_buffer[i] = buffer + i;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdapimin.c b/test/monniaux/jpeg-6b/jdapimin.c
new file mode 100644
index 00000000..cadb59fc
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdapimin.c
@@ -0,0 +1,395 @@
+/*
+ * jdapimin.c
+ *
+ * Copyright (C) 1994-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the decompression half
+ * of the JPEG library. These are the "minimum" API routines that may be
+ * needed in either the normal full-decompression case or the
+ * transcoding-only case.
+ *
+ * Most of the routines intended to be called directly by an application
+ * are in this file or in jdapistd.c. But also see jcomapi.c for routines
+ * shared by compression and decompression, and jdtrans.c for the transcoding
+ * case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG decompression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL(void)
+jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
+{
+ int i;
+
+ /* Guard against version mismatches between library and caller. */
+ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
+ if (version != JPEG_LIB_VERSION)
+ ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
+ if (structsize != SIZEOF(struct jpeg_decompress_struct))
+ ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
+ (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
+
+ /* For debugging purposes, we zero the whole master structure.
+ * But the application has already set the err pointer, and may have set
+ * client_data, so we have to save and restore those fields.
+ * Note: if application hasn't set client_data, tools like Purify may
+ * complain here.
+ */
+ {
+ struct jpeg_error_mgr * err = cinfo->err;
+ void * client_data = cinfo->client_data; /* ignore Purify complaint here */
+ MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
+ cinfo->err = err;
+ cinfo->client_data = client_data;
+ }
+ cinfo->is_decompressor = TRUE;
+
+ /* Initialize a memory manager instance for this object */
+ jinit_memory_mgr((j_common_ptr) cinfo);
+
+ /* Zero out pointers to permanent structures. */
+ cinfo->progress = NULL;
+ cinfo->src = NULL;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++)
+ cinfo->quant_tbl_ptrs[i] = NULL;
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ cinfo->dc_huff_tbl_ptrs[i] = NULL;
+ cinfo->ac_huff_tbl_ptrs[i] = NULL;
+ }
+
+ /* Initialize marker processor so application can override methods
+ * for COM, APPn markers before calling jpeg_read_header.
+ */
+ cinfo->marker_list = NULL;
+ jinit_marker_reader(cinfo);
+
+ /* And initialize the overall input controller. */
+ jinit_input_controller(cinfo);
+
+ /* OK, I'm ready */
+ cinfo->global_state = DSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG decompression object
+ */
+
+GLOBAL(void)
+jpeg_destroy_decompress (j_decompress_ptr cinfo)
+{
+ jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Abort processing of a JPEG decompression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL(void)
+jpeg_abort_decompress (j_decompress_ptr cinfo)
+{
+ jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Set default decompression parameters.
+ */
+
+LOCAL(void)
+default_decompress_parms (j_decompress_ptr cinfo)
+{
+ /* Guess the input colorspace, and set output colorspace accordingly. */
+ /* (Wish JPEG committee had provided a real way to specify this...) */
+ /* Note application may override our guesses. */
+ switch (cinfo->num_components) {
+ case 1:
+ cinfo->jpeg_color_space = JCS_GRAYSCALE;
+ cinfo->out_color_space = JCS_GRAYSCALE;
+ break;
+
+ case 3:
+ if (cinfo->saw_JFIF_marker) {
+ cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
+ } else if (cinfo->saw_Adobe_marker) {
+ switch (cinfo->Adobe_transform) {
+ case 0:
+ cinfo->jpeg_color_space = JCS_RGB;
+ break;
+ case 1:
+ cinfo->jpeg_color_space = JCS_YCbCr;
+ break;
+ default:
+ WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+ break;
+ }
+ } else {
+ /* Saw no special markers, try to guess from the component IDs */
+ int cid0 = cinfo->comp_info[0].component_id;
+ int cid1 = cinfo->comp_info[1].component_id;
+ int cid2 = cinfo->comp_info[2].component_id;
+
+ if (cid0 == 1 && cid1 == 2 && cid2 == 3)
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
+ else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
+ cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
+ else {
+ TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+ }
+ }
+ /* Always guess RGB is proper output colorspace. */
+ cinfo->out_color_space = JCS_RGB;
+ break;
+
+ case 4:
+ if (cinfo->saw_Adobe_marker) {
+ switch (cinfo->Adobe_transform) {
+ case 0:
+ cinfo->jpeg_color_space = JCS_CMYK;
+ break;
+ case 2:
+ cinfo->jpeg_color_space = JCS_YCCK;
+ break;
+ default:
+ WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+ cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
+ break;
+ }
+ } else {
+ /* No special markers, assume straight CMYK. */
+ cinfo->jpeg_color_space = JCS_CMYK;
+ }
+ cinfo->out_color_space = JCS_CMYK;
+ break;
+
+ default:
+ cinfo->jpeg_color_space = JCS_UNKNOWN;
+ cinfo->out_color_space = JCS_UNKNOWN;
+ break;
+ }
+
+ /* Set defaults for other decompression parameters. */
+ cinfo->scale_num = 1; /* 1:1 scaling */
+ cinfo->scale_denom = 1;
+ cinfo->output_gamma = 1.0;
+ cinfo->buffered_image = FALSE;
+ cinfo->raw_data_out = FALSE;
+ cinfo->dct_method = JDCT_DEFAULT;
+ cinfo->do_fancy_upsampling = TRUE;
+ cinfo->do_block_smoothing = TRUE;
+ cinfo->quantize_colors = FALSE;
+ /* We set these in case application only sets quantize_colors. */
+ cinfo->dither_mode = JDITHER_FS;
+#ifdef QUANT_2PASS_SUPPORTED
+ cinfo->two_pass_quantize = TRUE;
+#else
+ cinfo->two_pass_quantize = FALSE;
+#endif
+ cinfo->desired_number_of_colors = 256;
+ cinfo->colormap = NULL;
+ /* Initialize for no mode change in buffered-image mode. */
+ cinfo->enable_1pass_quant = FALSE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+}
+
+
+/*
+ * Decompression startup: read start of JPEG datastream to see what's there.
+ * Need only initialize JPEG object and supply a data source before calling.
+ *
+ * This routine will read as far as the first SOS marker (ie, actual start of
+ * compressed data), and will save all tables and parameters in the JPEG
+ * object. It will also initialize the decompression parameters to default
+ * values, and finally return JPEG_HEADER_OK. On return, the application may
+ * adjust the decompression parameters and then call jpeg_start_decompress.
+ * (Or, if the application only wanted to determine the image parameters,
+ * the data need not be decompressed. In that case, call jpeg_abort or
+ * jpeg_destroy to release any temporary space.)
+ * If an abbreviated (tables only) datastream is presented, the routine will
+ * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
+ * re-use the JPEG object to read the abbreviated image datastream(s).
+ * It is unnecessary (but OK) to call jpeg_abort in this case.
+ * The JPEG_SUSPENDED return code only occurs if the data source module
+ * requests suspension of the decompressor. In this case the application
+ * should load more source data and then re-call jpeg_read_header to resume
+ * processing.
+ * If a non-suspending data source is used and require_image is TRUE, then the
+ * return code need not be inspected since only JPEG_HEADER_OK is possible.
+ *
+ * This routine is now just a front end to jpeg_consume_input, with some
+ * extra error checking.
+ */
+
+GLOBAL(int)
+jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
+{
+ int retcode;
+
+ if (cinfo->global_state != DSTATE_START &&
+ cinfo->global_state != DSTATE_INHEADER)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ retcode = jpeg_consume_input(cinfo);
+
+ switch (retcode) {
+ case JPEG_REACHED_SOS:
+ retcode = JPEG_HEADER_OK;
+ break;
+ case JPEG_REACHED_EOI:
+ if (require_image) /* Complain if application wanted an image */
+ ERREXIT(cinfo, JERR_NO_IMAGE);
+ /* Reset to start state; it would be safer to require the application to
+ * call jpeg_abort, but we can't change it now for compatibility reasons.
+ * A side effect is to free any temporary memory (there shouldn't be any).
+ */
+ jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
+ retcode = JPEG_HEADER_TABLES_ONLY;
+ break;
+ case JPEG_SUSPENDED:
+ /* no work */
+ break;
+ }
+
+ return retcode;
+}
+
+
+/*
+ * Consume data in advance of what the decompressor requires.
+ * This can be called at any time once the decompressor object has
+ * been created and a data source has been set up.
+ *
+ * This routine is essentially a state machine that handles a couple
+ * of critical state-transition actions, namely initial setup and
+ * transition from header scanning to ready-for-start_decompress.
+ * All the actual input is done via the input controller's consume_input
+ * method.
+ */
+
+GLOBAL(int)
+jpeg_consume_input (j_decompress_ptr cinfo)
+{
+ int retcode = JPEG_SUSPENDED;
+
+ /* NB: every possible DSTATE value should be listed in this switch */
+ switch (cinfo->global_state) {
+ case DSTATE_START:
+ /* Start-of-datastream actions: reset appropriate modules */
+ (*cinfo->inputctl->reset_input_controller) (cinfo);
+ /* Initialize application's data source module */
+ (*cinfo->src->init_source) (cinfo);
+ cinfo->global_state = DSTATE_INHEADER;
+ /*FALLTHROUGH*/
+ case DSTATE_INHEADER:
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
+ /* Set up default parameters based on header data */
+ default_decompress_parms(cinfo);
+ /* Set global state: ready for start_decompress */
+ cinfo->global_state = DSTATE_READY;
+ }
+ break;
+ case DSTATE_READY:
+ /* Can't advance past first SOS until start_decompress is called */
+ retcode = JPEG_REACHED_SOS;
+ break;
+ case DSTATE_PRELOAD:
+ case DSTATE_PRESCAN:
+ case DSTATE_SCANNING:
+ case DSTATE_RAW_OK:
+ case DSTATE_BUFIMAGE:
+ case DSTATE_BUFPOST:
+ case DSTATE_STOPPING:
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ break;
+ default:
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ return retcode;
+}
+
+
+/*
+ * Have we finished reading the input file?
+ */
+
+GLOBAL(boolean)
+jpeg_input_complete (j_decompress_ptr cinfo)
+{
+ /* Check for valid jpeg object */
+ if (cinfo->global_state < DSTATE_START ||
+ cinfo->global_state > DSTATE_STOPPING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return cinfo->inputctl->eoi_reached;
+}
+
+
+/*
+ * Is there more than one scan?
+ */
+
+GLOBAL(boolean)
+jpeg_has_multiple_scans (j_decompress_ptr cinfo)
+{
+ /* Only valid after jpeg_read_header completes */
+ if (cinfo->global_state < DSTATE_READY ||
+ cinfo->global_state > DSTATE_STOPPING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return cinfo->inputctl->has_multiple_scans;
+}
+
+
+/*
+ * Finish JPEG decompression.
+ *
+ * This will normally just verify the file trailer and release temp storage.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_finish_decompress (j_decompress_ptr cinfo)
+{
+ if ((cinfo->global_state == DSTATE_SCANNING ||
+ cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
+ /* Terminate final pass of non-buffered mode */
+ if (cinfo->output_scanline < cinfo->output_height)
+ ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+ (*cinfo->master->finish_output_pass) (cinfo);
+ cinfo->global_state = DSTATE_STOPPING;
+ } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
+ /* Finishing after a buffered-image operation */
+ cinfo->global_state = DSTATE_STOPPING;
+ } else if (cinfo->global_state != DSTATE_STOPPING) {
+ /* STOPPING = repeat call after a suspension, anything else is error */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ /* Read until EOI */
+ while (! cinfo->inputctl->eoi_reached) {
+ if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
+ return FALSE; /* Suspend, come back later */
+ }
+ /* Do final cleanup */
+ (*cinfo->src->term_source) (cinfo);
+ /* We can use jpeg_abort to release memory and reset global_state */
+ jpeg_abort((j_common_ptr) cinfo);
+ return TRUE;
+}
diff --git a/test/monniaux/jpeg-6b/jdapistd.c b/test/monniaux/jpeg-6b/jdapistd.c
new file mode 100644
index 00000000..c8e3fa0c
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdapistd.c
@@ -0,0 +1,275 @@
+/*
+ * jdapistd.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the decompression half
+ * of the JPEG library. These are the "standard" API routines that are
+ * used in the normal full-decompression case. They are not used by a
+ * transcoding-only application. Note that if an application links in
+ * jpeg_start_decompress, it will end up linking in the entire decompressor.
+ * We thus must separate this file from jdapimin.c to avoid linking the
+ * whole decompression library into a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Decompression initialization.
+ * jpeg_read_header must be completed before calling this.
+ *
+ * If a multipass operating mode was selected, this will do all but the
+ * last pass, and thus may take a great deal of time.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_start_decompress (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state == DSTATE_READY) {
+ /* First call: initialize master control, select active modules */
+ jinit_master_decompress(cinfo);
+ if (cinfo->buffered_image) {
+ /* No more work here; expecting jpeg_start_output next */
+ cinfo->global_state = DSTATE_BUFIMAGE;
+ return TRUE;
+ }
+ cinfo->global_state = DSTATE_PRELOAD;
+ }
+ if (cinfo->global_state == DSTATE_PRELOAD) {
+ /* If file has multiple scans, absorb them all into the coef buffer */
+ if (cinfo->inputctl->has_multiple_scans) {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ for (;;) {
+ int retcode;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL)
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ /* Absorb some more input */
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_SUSPENDED)
+ return FALSE;
+ if (retcode == JPEG_REACHED_EOI)
+ break;
+ /* Advance progress counter if appropriate */
+ if (cinfo->progress != NULL &&
+ (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+ if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+ /* jdmaster underestimated number of scans; ratchet up one scan */
+ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+ }
+ }
+ }
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+ }
+ cinfo->output_scan_number = cinfo->input_scan_number;
+ } else if (cinfo->global_state != DSTATE_PRESCAN)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Perform any dummy output passes, and set up for the final pass */
+ return output_pass_setup(cinfo);
+}
+
+
+/*
+ * Set up for an output pass, and perform any dummy pass(es) needed.
+ * Common subroutine for jpeg_start_decompress and jpeg_start_output.
+ * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
+ * Exit: If done, returns TRUE and sets global_state for proper output mode.
+ * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
+ */
+
+LOCAL(boolean)
+output_pass_setup (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state != DSTATE_PRESCAN) {
+ /* First call: do pass setup */
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
+ cinfo->output_scanline = 0;
+ cinfo->global_state = DSTATE_PRESCAN;
+ }
+ /* Loop over any required dummy passes */
+ while (cinfo->master->is_dummy_pass) {
+#ifdef QUANT_2PASS_SUPPORTED
+ /* Crank through the dummy pass */
+ while (cinfo->output_scanline < cinfo->output_height) {
+ JDIMENSION last_scanline;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+ /* Process some data */
+ last_scanline = cinfo->output_scanline;
+ (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
+ &cinfo->output_scanline, (JDIMENSION) 0);
+ if (cinfo->output_scanline == last_scanline)
+ return FALSE; /* No progress made, must suspend */
+ }
+ /* Finish up dummy pass, and set up for another one */
+ (*cinfo->master->finish_output_pass) (cinfo);
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
+ cinfo->output_scanline = 0;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* QUANT_2PASS_SUPPORTED */
+ }
+ /* Ready for application to drive output pass through
+ * jpeg_read_scanlines or jpeg_read_raw_data.
+ */
+ cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
+ return TRUE;
+}
+
+
+/*
+ * Read some scanlines of data from the JPEG decompressor.
+ *
+ * The return value will be the number of lines actually read.
+ * This may be less than the number requested in several cases,
+ * including bottom of image, data source suspension, and operating
+ * modes that emit multiple scanlines at a time.
+ *
+ * Note: we warn about excess calls to jpeg_read_scanlines() since
+ * this likely signals an application programmer error. However,
+ * an oversize buffer (max_lines > scanlines remaining) is not an error.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
+ JDIMENSION max_lines)
+{
+ JDIMENSION row_ctr;
+
+ if (cinfo->global_state != DSTATE_SCANNING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->output_scanline >= cinfo->output_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Process some data */
+ row_ctr = 0;
+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
+ cinfo->output_scanline += row_ctr;
+ return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to read raw data.
+ * Processes exactly one iMCU row per call, unless suspended.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
+ JDIMENSION max_lines)
+{
+ JDIMENSION lines_per_iMCU_row;
+
+ if (cinfo->global_state != DSTATE_RAW_OK)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->output_scanline >= cinfo->output_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Verify that at least one iMCU row can be returned. */
+ lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
+ if (max_lines < lines_per_iMCU_row)
+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+ /* Decompress directly into user's buffer. */
+ if (! (*cinfo->coef->decompress_data) (cinfo, data))
+ return 0; /* suspension forced, can do nothing more */
+
+ /* OK, we processed one iMCU row. */
+ cinfo->output_scanline += lines_per_iMCU_row;
+ return lines_per_iMCU_row;
+}
+
+
+/* Additional entry points for buffered-image mode. */
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Initialize for an output pass in buffered-image mode.
+ */
+
+GLOBAL(boolean)
+jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
+{
+ if (cinfo->global_state != DSTATE_BUFIMAGE &&
+ cinfo->global_state != DSTATE_PRESCAN)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Limit scan number to valid range */
+ if (scan_number <= 0)
+ scan_number = 1;
+ if (cinfo->inputctl->eoi_reached &&
+ scan_number > cinfo->input_scan_number)
+ scan_number = cinfo->input_scan_number;
+ cinfo->output_scan_number = scan_number;
+ /* Perform any dummy output passes, and set up for the real pass */
+ return output_pass_setup(cinfo);
+}
+
+
+/*
+ * Finish up after an output pass in buffered-image mode.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_finish_output (j_decompress_ptr cinfo)
+{
+ if ((cinfo->global_state == DSTATE_SCANNING ||
+ cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
+ /* Terminate this pass. */
+ /* We do not require the whole pass to have been completed. */
+ (*cinfo->master->finish_output_pass) (cinfo);
+ cinfo->global_state = DSTATE_BUFPOST;
+ } else if (cinfo->global_state != DSTATE_BUFPOST) {
+ /* BUFPOST = repeat call after a suspension, anything else is error */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ /* Read markers looking for SOS or EOI */
+ while (cinfo->input_scan_number <= cinfo->output_scan_number &&
+ ! cinfo->inputctl->eoi_reached) {
+ if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
+ return FALSE; /* Suspend, come back later */
+ }
+ cinfo->global_state = DSTATE_BUFIMAGE;
+ return TRUE;
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jdatadst.c b/test/monniaux/jpeg-6b/jdatadst.c
new file mode 100644
index 00000000..a8f6fb0e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdatadst.c
@@ -0,0 +1,151 @@
+/*
+ * jdatadst.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains compression data destination routines for the case of
+ * emitting JPEG data to a file (or any stdio stream). While these routines
+ * are sufficient for most applications, some will want to use a different
+ * destination manager.
+ * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
+ * JOCTETs into 8-bit-wide elements on external storage. If char is wider
+ * than 8 bits on your machine, you may need to do some tweaking.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h"
+
+
+/* Expanded data destination object for stdio output */
+
+typedef struct {
+ struct jpeg_destination_mgr pub; /* public fields */
+
+ FILE * outfile; /* target stream */
+ JOCTET * buffer; /* start of buffer */
+} my_destination_mgr;
+
+typedef my_destination_mgr * my_dest_ptr;
+
+#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
+
+
+/*
+ * Initialize destination --- called by jpeg_start_compress
+ * before any data is actually written.
+ */
+
+METHODDEF(void)
+init_destination (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+ /* Allocate the output buffer --- it will be released when done with image */
+ dest->buffer = (JOCTET *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
+
+ dest->pub.next_output_byte = dest->buffer;
+ dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
+}
+
+
+/*
+ * Empty the output buffer --- called whenever buffer fills up.
+ *
+ * In typical applications, this should write the entire output buffer
+ * (ignoring the current state of next_output_byte & free_in_buffer),
+ * reset the pointer & count to the start of the buffer, and return TRUE
+ * indicating that the buffer has been dumped.
+ *
+ * In applications that need to be able to suspend compression due to output
+ * overrun, a FALSE return indicates that the buffer cannot be emptied now.
+ * In this situation, the compressor will return to its caller (possibly with
+ * an indication that it has not accepted all the supplied scanlines). The
+ * application should resume compression after it has made more room in the
+ * output buffer. Note that there are substantial restrictions on the use of
+ * suspension --- see the documentation.
+ *
+ * When suspending, the compressor will back up to a convenient restart point
+ * (typically the start of the current MCU). next_output_byte & free_in_buffer
+ * indicate where the restart point will be if the current call returns FALSE.
+ * Data beyond this point will be regenerated after resumption, so do not
+ * write it out when emptying the buffer externally.
+ */
+
+METHODDEF(boolean)
+empty_output_buffer (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+ if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
+ (size_t) OUTPUT_BUF_SIZE)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+
+ dest->pub.next_output_byte = dest->buffer;
+ dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
+
+ return TRUE;
+}
+
+
+/*
+ * Terminate destination --- called by jpeg_finish_compress
+ * after all data has been written. Usually needs to flush buffer.
+ *
+ * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+ * application must deal with any cleanup that should happen even
+ * for error exit.
+ */
+
+METHODDEF(void)
+term_destination (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+ size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
+
+ /* Write any data remaining in the buffer */
+ if (datacount > 0) {
+ if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+ }
+ fflush(dest->outfile);
+ /* Make sure we wrote the output file OK */
+ if (ferror(dest->outfile))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * Prepare for output to a stdio stream.
+ * The caller must have already opened the stream, and is responsible
+ * for closing it after finishing compression.
+ */
+
+GLOBAL(void)
+jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
+{
+ my_dest_ptr dest;
+
+ /* The destination object is made permanent so that multiple JPEG images
+ * can be written to the same file without re-executing jpeg_stdio_dest.
+ * This makes it dangerous to use this manager and a different destination
+ * manager serially with the same JPEG object, because their private object
+ * sizes may be different. Caveat programmer.
+ */
+ if (cinfo->dest == NULL) { /* first time for this JPEG object? */
+ cinfo->dest = (struct jpeg_destination_mgr *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_destination_mgr));
+ }
+
+ dest = (my_dest_ptr) cinfo->dest;
+ dest->pub.init_destination = init_destination;
+ dest->pub.empty_output_buffer = empty_output_buffer;
+ dest->pub.term_destination = term_destination;
+ dest->outfile = outfile;
+}
diff --git a/test/monniaux/jpeg-6b/jdatasrc.c b/test/monniaux/jpeg-6b/jdatasrc.c
new file mode 100644
index 00000000..edc752bf
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdatasrc.c
@@ -0,0 +1,212 @@
+/*
+ * jdatasrc.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains decompression data source routines for the case of
+ * reading JPEG data from a file (or any stdio stream). While these routines
+ * are sufficient for most applications, some will want to use a different
+ * source manager.
+ * IMPORTANT: we assume that fread() will correctly transcribe an array of
+ * JOCTETs from 8-bit-wide elements on external storage. If char is wider
+ * than 8 bits on your machine, you may need to do some tweaking.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h"
+
+
+/* Expanded data source object for stdio input */
+
+typedef struct {
+ struct jpeg_source_mgr pub; /* public fields */
+
+ FILE * infile; /* source stream */
+ JOCTET * buffer; /* start of buffer */
+ boolean start_of_file; /* have we gotten any data yet? */
+} my_source_mgr;
+
+typedef my_source_mgr * my_src_ptr;
+
+#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
+
+
+/*
+ * Initialize source --- called by jpeg_read_header
+ * before any data is actually read.
+ */
+
+METHODDEF(void)
+init_source (j_decompress_ptr cinfo)
+{
+ my_src_ptr src = (my_src_ptr) cinfo->src;
+
+ /* We reset the empty-input-file flag for each image,
+ * but we don't clear the input buffer.
+ * This is correct behavior for reading a series of images from one source.
+ */
+ src->start_of_file = TRUE;
+}
+
+
+/*
+ * Fill the input buffer --- called whenever buffer is emptied.
+ *
+ * In typical applications, this should read fresh data into the buffer
+ * (ignoring the current state of next_input_byte & bytes_in_buffer),
+ * reset the pointer & count to the start of the buffer, and return TRUE
+ * indicating that the buffer has been reloaded. It is not necessary to
+ * fill the buffer entirely, only to obtain at least one more byte.
+ *
+ * There is no such thing as an EOF return. If the end of the file has been
+ * reached, the routine has a choice of ERREXIT() or inserting fake data into
+ * the buffer. In most cases, generating a warning message and inserting a
+ * fake EOI marker is the best course of action --- this will allow the
+ * decompressor to output however much of the image is there. However,
+ * the resulting error message is misleading if the real problem is an empty
+ * input file, so we handle that case specially.
+ *
+ * In applications that need to be able to suspend compression due to input
+ * not being available yet, a FALSE return indicates that no more data can be
+ * obtained right now, but more may be forthcoming later. In this situation,
+ * the decompressor will return to its caller (with an indication of the
+ * number of scanlines it has read, if any). The application should resume
+ * decompression after it has loaded more data into the input buffer. Note
+ * that there are substantial restrictions on the use of suspension --- see
+ * the documentation.
+ *
+ * When suspending, the decompressor will back up to a convenient restart point
+ * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
+ * indicate where the restart point will be if the current call returns FALSE.
+ * Data beyond this point must be rescanned after resumption, so move it to
+ * the front of the buffer rather than discarding it.
+ */
+
+METHODDEF(boolean)
+fill_input_buffer (j_decompress_ptr cinfo)
+{
+ my_src_ptr src = (my_src_ptr) cinfo->src;
+ size_t nbytes;
+
+ nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
+
+ if (nbytes <= 0) {
+ if (src->start_of_file) /* Treat empty input file as fatal error */
+ ERREXIT(cinfo, JERR_INPUT_EMPTY);
+ WARNMS(cinfo, JWRN_JPEG_EOF);
+ /* Insert a fake EOI marker */
+ src->buffer[0] = (JOCTET) 0xFF;
+ src->buffer[1] = (JOCTET) JPEG_EOI;
+ nbytes = 2;
+ }
+
+ src->pub.next_input_byte = src->buffer;
+ src->pub.bytes_in_buffer = nbytes;
+ src->start_of_file = FALSE;
+
+ return TRUE;
+}
+
+
+/*
+ * Skip data --- used to skip over a potentially large amount of
+ * uninteresting data (such as an APPn marker).
+ *
+ * Writers of suspendable-input applications must note that skip_input_data
+ * is not granted the right to give a suspension return. If the skip extends
+ * beyond the data currently in the buffer, the buffer can be marked empty so
+ * that the next read will cause a fill_input_buffer call that can suspend.
+ * Arranging for additional bytes to be discarded before reloading the input
+ * buffer is the application writer's problem.
+ */
+
+METHODDEF(void)
+skip_input_data (j_decompress_ptr cinfo, long num_bytes)
+{
+ my_src_ptr src = (my_src_ptr) cinfo->src;
+
+ /* Just a dumb implementation for now. Could use fseek() except
+ * it doesn't work on pipes. Not clear that being smart is worth
+ * any trouble anyway --- large skips are infrequent.
+ */
+ if (num_bytes > 0) {
+ while (num_bytes > (long) src->pub.bytes_in_buffer) {
+ num_bytes -= (long) src->pub.bytes_in_buffer;
+ (void) fill_input_buffer(cinfo);
+ /* note we assume that fill_input_buffer will never return FALSE,
+ * so suspension need not be handled.
+ */
+ }
+ src->pub.next_input_byte += (size_t) num_bytes;
+ src->pub.bytes_in_buffer -= (size_t) num_bytes;
+ }
+}
+
+
+/*
+ * An additional method that can be provided by data source modules is the
+ * resync_to_restart method for error recovery in the presence of RST markers.
+ * For the moment, this source module just uses the default resync method
+ * provided by the JPEG library. That method assumes that no backtracking
+ * is possible.
+ */
+
+
+/*
+ * Terminate source --- called by jpeg_finish_decompress
+ * after all data has been read. Often a no-op.
+ *
+ * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+ * application must deal with any cleanup that should happen even
+ * for error exit.
+ */
+
+METHODDEF(void)
+term_source (j_decompress_ptr cinfo)
+{
+ /* no work necessary here */
+}
+
+
+/*
+ * Prepare for input from a stdio stream.
+ * The caller must have already opened the stream, and is responsible
+ * for closing it after finishing decompression.
+ */
+
+GLOBAL(void)
+jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
+{
+ my_src_ptr src;
+
+ /* The source object and input buffer are made permanent so that a series
+ * of JPEG images can be read from the same file by calling jpeg_stdio_src
+ * only before the first one. (If we discarded the buffer at the end of
+ * one image, we'd likely lose the start of the next one.)
+ * This makes it unsafe to use this manager and a different source
+ * manager serially with the same JPEG object. Caveat programmer.
+ */
+ if (cinfo->src == NULL) { /* first time for this JPEG object? */
+ cinfo->src = (struct jpeg_source_mgr *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_source_mgr));
+ src = (my_src_ptr) cinfo->src;
+ src->buffer = (JOCTET *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ INPUT_BUF_SIZE * SIZEOF(JOCTET));
+ }
+
+ src = (my_src_ptr) cinfo->src;
+ src->pub.init_source = init_source;
+ src->pub.fill_input_buffer = fill_input_buffer;
+ src->pub.skip_input_data = skip_input_data;
+ src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
+ src->pub.term_source = term_source;
+ src->infile = infile;
+ src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
+ src->pub.next_input_byte = NULL; /* until buffer loaded */
+}
diff --git a/test/monniaux/jpeg-6b/jdcoefct.c b/test/monniaux/jpeg-6b/jdcoefct.c
new file mode 100644
index 00000000..4938d20f
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdcoefct.c
@@ -0,0 +1,736 @@
+/*
+ * jdcoefct.c
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the coefficient buffer controller for decompression.
+ * This controller is the top level of the JPEG decompressor proper.
+ * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
+ *
+ * In buffered-image mode, this controller is the interface between
+ * input-oriented processing and output-oriented processing.
+ * Also, the input side (only) is used when reading a file for transcoding.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+/* Block smoothing is only applicable for progressive JPEG, so: */
+#ifndef D_PROGRESSIVE_SUPPORTED
+#undef BLOCK_SMOOTHING_SUPPORTED
+#endif
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_coef_controller pub; /* public fields */
+
+ /* These variables keep track of the current location of the input side. */
+ /* cinfo->input_iMCU_row is also used for this. */
+ JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* The output side's location is represented by cinfo->output_iMCU_row. */
+
+ /* In single-pass modes, it's sufficient to buffer just one MCU.
+ * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
+ * and let the entropy decoder write into that workspace each time.
+ * (On 80x86, the workspace is FAR even though it's not really very big;
+ * this is to keep the module interfaces unchanged when a large coefficient
+ * buffer is necessary.)
+ * In multi-pass modes, this array points to the current MCU's blocks
+ * within the virtual arrays; it is used only by the input side.
+ */
+ JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* In multi-pass modes, we need a virtual block array for each component. */
+ jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+#endif
+
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ /* When doing block smoothing, we latch coefficient Al values here */
+ int * coef_bits_latch;
+#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
+#endif
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+/* Forward declarations */
+METHODDEF(int) decompress_onepass
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+METHODDEF(int) decompress_data
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#endif
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
+METHODDEF(int) decompress_smooth_data
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#endif
+
+
+LOCAL(void)
+start_iMCU_row (j_decompress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row (input side) */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->MCU_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for an input processing pass.
+ */
+
+METHODDEF(void)
+start_input_pass (j_decompress_ptr cinfo)
+{
+ cinfo->input_iMCU_row = 0;
+ start_iMCU_row(cinfo);
+}
+
+
+/*
+ * Initialize for an output processing pass.
+ */
+
+METHODDEF(void)
+start_output_pass (j_decompress_ptr cinfo)
+{
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* If multipass, check to see whether to use block smoothing on this pass */
+ if (coef->pub.coef_arrays != NULL) {
+ if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
+ coef->pub.decompress_data = decompress_smooth_data;
+ else
+ coef->pub.decompress_data = decompress_data;
+ }
+#endif
+ cinfo->output_iMCU_row = 0;
+}
+
+
+/*
+ * Decompress and return some data in the single-pass case.
+ * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
+ * Input and output must run in lockstep since we have only a one-MCU buffer.
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ *
+ * NB: output_buf contains a plane for each component in image,
+ * which we index according to the component's SOF position.
+ */
+
+METHODDEF(int)
+decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, ci, xindex, yindex, yoffset, useful_width;
+ JSAMPARRAY output_ptr;
+ JDIMENSION start_col, output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+
+ /* Loop to process as much as one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
+ MCU_col_num++) {
+ /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
+ jzero_far((void FAR *) coef->MCU_buffer[0],
+ (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
+ if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->MCU_ctr = MCU_col_num;
+ return JPEG_SUSPENDED;
+ }
+ /* Determine where data should go in output_buf and do the IDCT thing.
+ * We skip dummy blocks at the right and bottom edges (but blkn gets
+ * incremented past them!). Note the inner loop relies on having
+ * allocated the MCU_buffer[] blocks sequentially.
+ */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed) {
+ blkn += compptr->MCU_blocks;
+ continue;
+ }
+ inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
+ useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ output_ptr = output_buf[compptr->component_index] +
+ yoffset * compptr->DCT_scaled_size;
+ start_col = MCU_col_num * compptr->MCU_sample_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (cinfo->input_iMCU_row < last_iMCU_row ||
+ yoffset+yindex < compptr->last_row_height) {
+ output_col = start_col;
+ for (xindex = 0; xindex < useful_width; xindex++) {
+ (*inverse_DCT) (cinfo, compptr,
+ (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
+ output_ptr, output_col);
+ output_col += compptr->DCT_scaled_size;
+ }
+ }
+ blkn += compptr->MCU_width;
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->MCU_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ cinfo->output_iMCU_row++;
+ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
+ start_iMCU_row(cinfo);
+ return JPEG_ROW_COMPLETED;
+ }
+ /* Completed the scan */
+ (*cinfo->inputctl->finish_input_pass) (cinfo);
+ return JPEG_SCAN_COMPLETED;
+}
+
+
+/*
+ * Dummy consume-input routine for single-pass operation.
+ */
+
+METHODDEF(int)
+dummy_consume_data (j_decompress_ptr cinfo)
+{
+ return JPEG_SUSPENDED; /* Always indicate nothing was done */
+}
+
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Consume input data and store it in the full-image coefficient buffer.
+ * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
+ * ie, v_samp_factor block rows for each component in the scan.
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ */
+
+METHODDEF(int)
+consume_data (j_decompress_ptr cinfo)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ int blkn, ci, xindex, yindex, yoffset;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan. */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ cinfo->input_iMCU_row * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ /* Note: entropy decoder expects buffer to be zeroed,
+ * but this is handled automatically by the memory manager
+ * because we requested a pre-zeroed array.
+ */
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+ coef->MCU_buffer[blkn++] = buffer_ptr++;
+ }
+ }
+ }
+ /* Try to fetch the MCU. */
+ if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->MCU_ctr = MCU_col_num;
+ return JPEG_SUSPENDED;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->MCU_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
+ start_iMCU_row(cinfo);
+ return JPEG_ROW_COMPLETED;
+ }
+ /* Completed the scan */
+ (*cinfo->inputctl->finish_input_pass) (cinfo);
+ return JPEG_SCAN_COMPLETED;
+}
+
+
+/*
+ * Decompress and return some data in the multi-pass case.
+ * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ *
+ * NB: output_buf contains a plane for each component in image.
+ */
+
+METHODDEF(int)
+decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION block_num;
+ int ci, block_row, block_rows;
+ JBLOCKARRAY buffer;
+ JBLOCKROW buffer_ptr;
+ JSAMPARRAY output_ptr;
+ JDIMENSION output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+
+ /* Force some input to be done if we are getting ahead of the input. */
+ while (cinfo->input_scan_number < cinfo->output_scan_number ||
+ (cinfo->input_scan_number == cinfo->output_scan_number &&
+ cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
+ if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
+ return JPEG_SUSPENDED;
+ }
+
+ /* OK, output from the virtual arrays. */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed)
+ continue;
+ /* Align the virtual buffer for this component. */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ cinfo->output_iMCU_row * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (cinfo->output_iMCU_row < last_iMCU_row)
+ block_rows = compptr->v_samp_factor;
+ else {
+ /* NB: can't use last_row_height here; it is input-side-dependent! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ }
+ inverse_DCT = cinfo->idct->inverse_DCT[ci];
+ output_ptr = output_buf[ci];
+ /* Loop over all DCT blocks to be processed. */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ buffer_ptr = buffer[block_row];
+ output_col = 0;
+ for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
+ (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
+ output_ptr, output_col);
+ buffer_ptr++;
+ output_col += compptr->DCT_scaled_size;
+ }
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+
+ if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
+ return JPEG_ROW_COMPLETED;
+ return JPEG_SCAN_COMPLETED;
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+
+/*
+ * This code applies interblock smoothing as described by section K.8
+ * of the JPEG standard: the first 5 AC coefficients are estimated from
+ * the DC values of a DCT block and its 8 neighboring blocks.
+ * We apply smoothing only for progressive JPEG decoding, and only if
+ * the coefficients it can estimate are not yet known to full precision.
+ */
+
+/* Natural-order array positions of the first 5 zigzag-order coefficients */
+#define Q01_POS 1
+#define Q10_POS 8
+#define Q20_POS 16
+#define Q11_POS 9
+#define Q02_POS 2
+
+/*
+ * Determine whether block smoothing is applicable and safe.
+ * We also latch the current states of the coef_bits[] entries for the
+ * AC coefficients; otherwise, if the input side of the decompressor
+ * advances into a new scan, we might think the coefficients are known
+ * more accurately than they really are.
+ */
+
+LOCAL(boolean)
+smoothing_ok (j_decompress_ptr cinfo)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ boolean smoothing_useful = FALSE;
+ int ci, coefi;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtable;
+ int * coef_bits;
+ int * coef_bits_latch;
+
+ if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
+ return FALSE;
+
+ /* Allocate latch area if not already done */
+ if (coef->coef_bits_latch == NULL)
+ coef->coef_bits_latch = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components *
+ (SAVED_COEFS * SIZEOF(int)));
+ coef_bits_latch = coef->coef_bits_latch;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* All components' quantization values must already be latched. */
+ if ((qtable = compptr->quant_table) == NULL)
+ return FALSE;
+ /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
+ if (qtable->quantval[0] == 0 ||
+ qtable->quantval[Q01_POS] == 0 ||
+ qtable->quantval[Q10_POS] == 0 ||
+ qtable->quantval[Q20_POS] == 0 ||
+ qtable->quantval[Q11_POS] == 0 ||
+ qtable->quantval[Q02_POS] == 0)
+ return FALSE;
+ /* DC values must be at least partly known for all components. */
+ coef_bits = cinfo->coef_bits[ci];
+ if (coef_bits[0] < 0)
+ return FALSE;
+ /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
+ for (coefi = 1; coefi <= 5; coefi++) {
+ coef_bits_latch[coefi] = coef_bits[coefi];
+ if (coef_bits[coefi] != 0)
+ smoothing_useful = TRUE;
+ }
+ coef_bits_latch += SAVED_COEFS;
+ }
+
+ return smoothing_useful;
+}
+
+
+/*
+ * Variant of decompress_data for use when doing block smoothing.
+ */
+
+METHODDEF(int)
+decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION block_num, last_block_column;
+ int ci, block_row, block_rows, access_rows;
+ JBLOCKARRAY buffer;
+ JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
+ JSAMPARRAY output_ptr;
+ JDIMENSION output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+ boolean first_row, last_row;
+ JBLOCK workspace;
+ int *coef_bits;
+ JQUANT_TBL *quanttbl;
+ INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
+ int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
+ int Al, pred;
+
+ /* Force some input to be done if we are getting ahead of the input. */
+ while (cinfo->input_scan_number <= cinfo->output_scan_number &&
+ ! cinfo->inputctl->eoi_reached) {
+ if (cinfo->input_scan_number == cinfo->output_scan_number) {
+ /* If input is working on current scan, we ordinarily want it to
+ * have completed the current row. But if input scan is DC,
+ * we want it to keep one row ahead so that next block row's DC
+ * values are up to date.
+ */
+ JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
+ if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
+ break;
+ }
+ if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
+ return JPEG_SUSPENDED;
+ }
+
+ /* OK, output from the virtual arrays. */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed)
+ continue;
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (cinfo->output_iMCU_row < last_iMCU_row) {
+ block_rows = compptr->v_samp_factor;
+ access_rows = block_rows * 2; /* this and next iMCU row */
+ last_row = FALSE;
+ } else {
+ /* NB: can't use last_row_height here; it is input-side-dependent! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ access_rows = block_rows; /* this iMCU row only */
+ last_row = TRUE;
+ }
+ /* Align the virtual buffer for this component. */
+ if (cinfo->output_iMCU_row > 0) {
+ access_rows += compptr->v_samp_factor; /* prior iMCU row too */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
+ (JDIMENSION) access_rows, FALSE);
+ buffer += compptr->v_samp_factor; /* point to current iMCU row */
+ first_row = FALSE;
+ } else {
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
+ first_row = TRUE;
+ }
+ /* Fetch component-dependent info */
+ coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
+ quanttbl = compptr->quant_table;
+ Q00 = quanttbl->quantval[0];
+ Q01 = quanttbl->quantval[Q01_POS];
+ Q10 = quanttbl->quantval[Q10_POS];
+ Q20 = quanttbl->quantval[Q20_POS];
+ Q11 = quanttbl->quantval[Q11_POS];
+ Q02 = quanttbl->quantval[Q02_POS];
+ inverse_DCT = cinfo->idct->inverse_DCT[ci];
+ output_ptr = output_buf[ci];
+ /* Loop over all DCT blocks to be processed. */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ buffer_ptr = buffer[block_row];
+ if (first_row && block_row == 0)
+ prev_block_row = buffer_ptr;
+ else
+ prev_block_row = buffer[block_row-1];
+ if (last_row && block_row == block_rows-1)
+ next_block_row = buffer_ptr;
+ else
+ next_block_row = buffer[block_row+1];
+ /* We fetch the surrounding DC values using a sliding-register approach.
+ * Initialize all nine here so as to do the right thing on narrow pics.
+ */
+ DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
+ DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
+ DC7 = DC8 = DC9 = (int) next_block_row[0][0];
+ output_col = 0;
+ last_block_column = compptr->width_in_blocks - 1;
+ for (block_num = 0; block_num <= last_block_column; block_num++) {
+ /* Fetch current DCT block into workspace so we can modify it. */
+ jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
+ /* Update DC values */
+ if (block_num < last_block_column) {
+ DC3 = (int) prev_block_row[1][0];
+ DC6 = (int) buffer_ptr[1][0];
+ DC9 = (int) next_block_row[1][0];
+ }
+ /* Compute coefficient estimates per K.8.
+ * An estimate is applied only if coefficient is still zero,
+ * and is not known to be fully accurate.
+ */
+ /* AC01 */
+ if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
+ num = 36 * Q00 * (DC4 - DC6);
+ if (num >= 0) {
+ pred = (int) (((Q01<<7) + num) / (Q01<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q01<<7) - num) / (Q01<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[1] = (JCOEF) pred;
+ }
+ /* AC10 */
+ if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
+ num = 36 * Q00 * (DC2 - DC8);
+ if (num >= 0) {
+ pred = (int) (((Q10<<7) + num) / (Q10<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q10<<7) - num) / (Q10<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[8] = (JCOEF) pred;
+ }
+ /* AC20 */
+ if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
+ num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
+ if (num >= 0) {
+ pred = (int) (((Q20<<7) + num) / (Q20<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q20<<7) - num) / (Q20<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[16] = (JCOEF) pred;
+ }
+ /* AC11 */
+ if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
+ num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
+ if (num >= 0) {
+ pred = (int) (((Q11<<7) + num) / (Q11<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q11<<7) - num) / (Q11<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[9] = (JCOEF) pred;
+ }
+ /* AC02 */
+ if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
+ num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
+ if (num >= 0) {
+ pred = (int) (((Q02<<7) + num) / (Q02<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q02<<7) - num) / (Q02<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[2] = (JCOEF) pred;
+ }
+ /* OK, do the IDCT */
+ (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
+ output_ptr, output_col);
+ /* Advance for next column */
+ DC1 = DC2; DC2 = DC3;
+ DC4 = DC5; DC5 = DC6;
+ DC7 = DC8; DC8 = DC9;
+ buffer_ptr++, prev_block_row++, next_block_row++;
+ output_col += compptr->DCT_scaled_size;
+ }
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+
+ if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
+ return JPEG_ROW_COMPLETED;
+ return JPEG_SCAN_COMPLETED;
+}
+
+#endif /* BLOCK_SMOOTHING_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL(void)
+jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_coef_ptr coef;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_d_coef_controller *) coef;
+ coef->pub.start_input_pass = start_input_pass;
+ coef->pub.start_output_pass = start_output_pass;
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ coef->coef_bits_latch = NULL;
+#endif
+
+ /* Create the coefficient buffer. */
+ if (need_full_buffer) {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* Allocate a full-image virtual array for each component, */
+ /* padded to a multiple of samp_factor DCT blocks in each direction. */
+ /* Note we ask for a pre-zeroed array. */
+ int ci, access_rows;
+ jpeg_component_info *compptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ access_rows = compptr->v_samp_factor;
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ /* If block smoothing could be used, need a bigger window */
+ if (cinfo->progressive_mode)
+ access_rows *= 3;
+#endif
+ coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) access_rows);
+ }
+ coef->pub.consume_data = consume_data;
+ coef->pub.decompress_data = decompress_data;
+ coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ /* We only need a single-MCU buffer. */
+ JBLOCKROW buffer;
+ int i;
+
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
+ coef->MCU_buffer[i] = buffer + i;
+ }
+ coef->pub.consume_data = dummy_consume_data;
+ coef->pub.decompress_data = decompress_onepass;
+ coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdcolor.c b/test/monniaux/jpeg-6b/jdcolor.c
new file mode 100644
index 00000000..6c04dfe8
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdcolor.c
@@ -0,0 +1,396 @@
+/*
+ * jdcolor.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains output colorspace conversion routines.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_color_deconverter pub; /* public fields */
+
+ /* Private state for YCC->RGB conversion */
+ int * Cr_r_tab; /* => table for Cr to R conversion */
+ int * Cb_b_tab; /* => table for Cb to B conversion */
+ INT32 * Cr_g_tab; /* => table for Cr to G conversion */
+ INT32 * Cb_g_tab; /* => table for Cb to G conversion */
+} my_color_deconverter;
+
+typedef my_color_deconverter * my_cconvert_ptr;
+
+
+/**************** YCbCr -> RGB conversion: most common case **************/
+
+/*
+ * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
+ * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * The conversion equations to be implemented are therefore
+ * R = Y + 1.40200 * Cr
+ * G = Y - 0.34414 * Cb - 0.71414 * Cr
+ * B = Y + 1.77200 * Cb
+ * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
+ * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ *
+ * To avoid floating-point arithmetic, we represent the fractional constants
+ * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
+ * the products by 2^16, with appropriate rounding, to get the correct answer.
+ * Notice that Y, being an integral input, does not contribute any fraction
+ * so it need not participate in the rounding.
+ *
+ * For even more speed, we avoid doing any multiplications in the inner loop
+ * by precalculating the constants times Cb and Cr for all possible values.
+ * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
+ * for 12-bit samples it is still acceptable. It's not very reasonable for
+ * 16-bit samples, but if you want lossless storage you shouldn't be changing
+ * colorspace anyway.
+ * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
+ * values for the G calculation are left scaled up, since we must add them
+ * together before rounding.
+ */
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+
+/*
+ * Initialize tables for YCC->RGB colorspace conversion.
+ */
+
+LOCAL(void)
+build_ycc_rgb_table (j_decompress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ int i;
+ INT32 x;
+ SHIFT_TEMPS
+
+ cconvert->Cr_r_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ cconvert->Cb_b_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ cconvert->Cr_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+ cconvert->Cb_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+
+ for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+ /* Cr=>R value is nearest int to 1.40200 * x */
+ cconvert->Cr_r_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
+ /* Cb=>B value is nearest int to 1.77200 * x */
+ cconvert->Cb_b_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
+ /* Cr=>G value is scaled-up -0.71414 * x */
+ cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
+ /* Cb=>G value is scaled-up -0.34414 * x */
+ /* We also add in ONE_HALF so that need not do it in inner loop */
+ cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the output colorspace.
+ *
+ * Note that we change from noninterleaved, one-plane-per-component format
+ * to interleaved-pixel format. The output buffer is therefore three times
+ * as wide as the input buffer.
+ * A starting row offset is provided only for the input buffer. The caller
+ * can easily adjust the passed output_buf value to accommodate any row
+ * offset required on that side.
+ */
+
+METHODDEF(void)
+ycc_rgb_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int y, cb, cr;
+ register JSAMPROW outptr;
+ register JSAMPROW inptr0, inptr1, inptr2;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ register int * Crrtab = cconvert->Cr_r_tab;
+ register int * Cbbtab = cconvert->Cb_b_tab;
+ register INT32 * Crgtab = cconvert->Cr_g_tab;
+ register INT32 * Cbgtab = cconvert->Cb_g_tab;
+ SHIFT_TEMPS
+
+ while (--num_rows >= 0) {
+ inptr0 = input_buf[0][input_row];
+ inptr1 = input_buf[1][input_row];
+ inptr2 = input_buf[2][input_row];
+ input_row++;
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ y = GETJSAMPLE(inptr0[col]);
+ cb = GETJSAMPLE(inptr1[col]);
+ cr = GETJSAMPLE(inptr2[col]);
+ /* Range-limiting is essential due to noise introduced by DCT losses. */
+ outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
+ outptr[RGB_GREEN] = range_limit[y +
+ ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+ SCALEBITS))];
+ outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
+ outptr += RGB_PIXELSIZE;
+ }
+ }
+}
+
+
+/**************** Cases other than YCbCr -> RGB **************/
+
+
+/*
+ * Color conversion for no colorspace change: just copy the data,
+ * converting from separate-planes to interleaved representation.
+ */
+
+METHODDEF(void)
+null_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION count;
+ register int num_components = cinfo->num_components;
+ JDIMENSION num_cols = cinfo->output_width;
+ int ci;
+
+ while (--num_rows >= 0) {
+ for (ci = 0; ci < num_components; ci++) {
+ inptr = input_buf[ci][input_row];
+ outptr = output_buf[0] + ci;
+ for (count = num_cols; count > 0; count--) {
+ *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
+ outptr += num_components;
+ }
+ }
+ input_row++;
+ output_buf++;
+ }
+}
+
+
+/*
+ * Color conversion for grayscale: just copy the data.
+ * This also works for YCbCr -> grayscale conversion, in which
+ * we just copy the Y (luminance) component and ignore chrominance.
+ */
+
+METHODDEF(void)
+grayscale_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
+ num_rows, cinfo->output_width);
+}
+
+
+/*
+ * Convert grayscale to RGB: just duplicate the graylevel three times.
+ * This is provided to support applications that don't want to cope
+ * with grayscale as a separate case.
+ */
+
+METHODDEF(void)
+gray_rgb_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+
+ while (--num_rows >= 0) {
+ inptr = input_buf[0][input_row++];
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ /* We can dispense with GETJSAMPLE() here */
+ outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
+ outptr += RGB_PIXELSIZE;
+ }
+ }
+}
+
+
+/*
+ * Adobe-style YCCK->CMYK conversion.
+ * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume build_ycc_rgb_table has been called.
+ */
+
+METHODDEF(void)
+ycck_cmyk_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int y, cb, cr;
+ register JSAMPROW outptr;
+ register JSAMPROW inptr0, inptr1, inptr2, inptr3;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ register int * Crrtab = cconvert->Cr_r_tab;
+ register int * Cbbtab = cconvert->Cb_b_tab;
+ register INT32 * Crgtab = cconvert->Cr_g_tab;
+ register INT32 * Cbgtab = cconvert->Cb_g_tab;
+ SHIFT_TEMPS
+
+ while (--num_rows >= 0) {
+ inptr0 = input_buf[0][input_row];
+ inptr1 = input_buf[1][input_row];
+ inptr2 = input_buf[2][input_row];
+ inptr3 = input_buf[3][input_row];
+ input_row++;
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ y = GETJSAMPLE(inptr0[col]);
+ cb = GETJSAMPLE(inptr1[col]);
+ cr = GETJSAMPLE(inptr2[col]);
+ /* Range-limiting is essential due to noise introduced by DCT losses. */
+ outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
+ outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
+ ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+ SCALEBITS)))];
+ outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
+ /* K passes through unchanged */
+ outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
+ outptr += 4;
+ }
+ }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF(void)
+start_pass_dcolor (j_decompress_ptr cinfo)
+{
+ /* no work needed */
+}
+
+
+/*
+ * Module initialization routine for output colorspace conversion.
+ */
+
+GLOBAL(void)
+jinit_color_deconverter (j_decompress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert;
+ int ci;
+
+ cconvert = (my_cconvert_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_color_deconverter));
+ cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
+ cconvert->pub.start_pass = start_pass_dcolor;
+
+ /* Make sure num_components agrees with jpeg_color_space */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->num_components != 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ case JCS_RGB:
+ case JCS_YCbCr:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ case JCS_CMYK:
+ case JCS_YCCK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ default: /* JCS_UNKNOWN can be anything */
+ if (cinfo->num_components < 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+ }
+
+ /* Set out_color_components and conversion method based on requested space.
+ * Also clear the component_needed flags for any unused components,
+ * so that earlier pipeline stages can avoid useless computation.
+ */
+
+ switch (cinfo->out_color_space) {
+ case JCS_GRAYSCALE:
+ cinfo->out_color_components = 1;
+ if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
+ cinfo->jpeg_color_space == JCS_YCbCr) {
+ cconvert->pub.color_convert = grayscale_convert;
+ /* For color->grayscale conversion, only the Y (0) component is needed */
+ for (ci = 1; ci < cinfo->num_components; ci++)
+ cinfo->comp_info[ci].component_needed = FALSE;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_RGB:
+ cinfo->out_color_components = RGB_PIXELSIZE;
+ if (cinfo->jpeg_color_space == JCS_YCbCr) {
+ cconvert->pub.color_convert = ycc_rgb_convert;
+ build_ycc_rgb_table(cinfo);
+ } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
+ cconvert->pub.color_convert = gray_rgb_convert;
+ } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
+ cconvert->pub.color_convert = null_convert;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_CMYK:
+ cinfo->out_color_components = 4;
+ if (cinfo->jpeg_color_space == JCS_YCCK) {
+ cconvert->pub.color_convert = ycck_cmyk_convert;
+ build_ycc_rgb_table(cinfo);
+ } else if (cinfo->jpeg_color_space == JCS_CMYK) {
+ cconvert->pub.color_convert = null_convert;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ default:
+ /* Permit null conversion to same output space */
+ if (cinfo->out_color_space == cinfo->jpeg_color_space) {
+ cinfo->out_color_components = cinfo->num_components;
+ cconvert->pub.color_convert = null_convert;
+ } else /* unsupported non-null conversion */
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+ }
+
+ if (cinfo->quantize_colors)
+ cinfo->output_components = 1; /* single colormapped output component */
+ else
+ cinfo->output_components = cinfo->out_color_components;
+}
diff --git a/test/monniaux/jpeg-6b/jdct.h b/test/monniaux/jpeg-6b/jdct.h
new file mode 100644
index 00000000..04192a26
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdct.h
@@ -0,0 +1,176 @@
+/*
+ * jdct.h
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This include file contains common declarations for the forward and
+ * inverse DCT modules. These declarations are private to the DCT managers
+ * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
+ * The individual DCT algorithms are kept in separate files to ease
+ * machine-dependent tuning (e.g., assembly coding).
+ */
+
+
+/*
+ * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
+ * the DCT is to be performed in-place in that buffer. Type DCTELEM is int
+ * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT
+ * implementations use an array of type FAST_FLOAT, instead.)
+ * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
+ * The DCT outputs are returned scaled up by a factor of 8; they therefore
+ * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
+ * convention improves accuracy in integer implementations and saves some
+ * work in floating-point ones.
+ * Quantization of the output coefficients is done by jcdctmgr.c.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef int DCTELEM; /* 16 or 32 bits is fine */
+#else
+typedef INT32 DCTELEM; /* must have 32 bits */
+#endif
+
+typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
+typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
+
+
+/*
+ * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
+ * to an output sample array. The routine must dequantize the input data as
+ * well as perform the IDCT; for dequantization, it uses the multiplier table
+ * pointed to by compptr->dct_table. The output data is to be placed into the
+ * sample array starting at a specified column. (Any row offset needed will
+ * be applied to the array pointer before it is passed to the IDCT code.)
+ * Note that the number of samples emitted by the IDCT routine is
+ * DCT_scaled_size * DCT_scaled_size.
+ */
+
+/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
+
+/*
+ * Each IDCT routine has its own ideas about the best dct_table element type.
+ */
+
+typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
+#if BITS_IN_JSAMPLE == 8
+typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
+#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
+#else
+typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
+#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
+#endif
+typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
+
+
+/*
+ * Each IDCT routine is responsible for range-limiting its results and
+ * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
+ * be quite far out of range if the input data is corrupt, so a bulletproof
+ * range-limiting step is required. We use a mask-and-table-lookup method
+ * to do the combined operations quickly. See the comments with
+ * prepare_range_limit_table (in jdmaster.c) for more info.
+ */
+
+#define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE)
+
+#define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_fdct_islow jFDislow
+#define jpeg_fdct_ifast jFDifast
+#define jpeg_fdct_float jFDfloat
+#define jpeg_idct_islow jRDislow
+#define jpeg_idct_ifast jRDifast
+#define jpeg_idct_float jRDfloat
+#define jpeg_idct_4x4 jRD4x4
+#define jpeg_idct_2x2 jRD2x2
+#define jpeg_idct_1x1 jRD1x1
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Extern declarations for the forward and inverse DCT routines. */
+
+EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
+EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
+EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));
+
+EXTERN(void) jpeg_idct_islow
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_ifast
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_float
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_4x4
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_2x2
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_1x1
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+
+
+/*
+ * Macros for handling fixed-point arithmetic; these are used by many
+ * but not all of the DCT/IDCT modules.
+ *
+ * All values are expected to be of type INT32.
+ * Fractional constants are scaled left by CONST_BITS bits.
+ * CONST_BITS is defined within each module using these macros,
+ * and may differ from one module to the next.
+ */
+
+#define ONE ((INT32) 1)
+#define CONST_SCALE (ONE << CONST_BITS)
+
+/* Convert a positive real constant to an integer scaled by CONST_SCALE.
+ * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
+ * thus causing a lot of useless floating-point operations at run time.
+ */
+
+#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))
+
+/* Descale and correctly round an INT32 value that's scaled by N bits.
+ * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
+ * the fudge factor is correct for either sign of X.
+ */
+
+#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * This macro is used only when the two inputs will actually be no more than
+ * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
+ * full 32x32 multiply. This provides a useful speedup on many machines.
+ * Unfortunately there is no way to specify a 16x16->32 multiply portably
+ * in C, but some C compilers will do the right thing if you provide the
+ * correct combination of casts.
+ */
+
+#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
+#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const)))
+#endif
+#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
+#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const)))
+#endif
+
+#ifndef MULTIPLY16C16 /* default definition */
+#define MULTIPLY16C16(var,const) ((var) * (const))
+#endif
+
+/* Same except both inputs are variables. */
+
+#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
+#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2)))
+#endif
+
+#ifndef MULTIPLY16V16 /* default definition */
+#define MULTIPLY16V16(var1,var2) ((var1) * (var2))
+#endif
diff --git a/test/monniaux/jpeg-6b/jddctmgr.c b/test/monniaux/jpeg-6b/jddctmgr.c
new file mode 100644
index 00000000..bbf8d0e9
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jddctmgr.c
@@ -0,0 +1,269 @@
+/*
+ * jddctmgr.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the inverse-DCT management logic.
+ * This code selects a particular IDCT implementation to be used,
+ * and it performs related housekeeping chores. No code in this file
+ * is executed per IDCT step, only during output pass setup.
+ *
+ * Note that the IDCT routines are responsible for performing coefficient
+ * dequantization as well as the IDCT proper. This module sets up the
+ * dequantization multiplier table needed by the IDCT routine.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+
+/*
+ * The decompressor input side (jdinput.c) saves away the appropriate
+ * quantization table for each component at the start of the first scan
+ * involving that component. (This is necessary in order to correctly
+ * decode files that reuse Q-table slots.)
+ * When we are ready to make an output pass, the saved Q-table is converted
+ * to a multiplier table that will actually be used by the IDCT routine.
+ * The multiplier table contents are IDCT-method-dependent. To support
+ * application changes in IDCT method between scans, we can remake the
+ * multiplier tables if necessary.
+ * In buffered-image mode, the first output pass may occur before any data
+ * has been seen for some components, and thus before their Q-tables have
+ * been saved away. To handle this case, multiplier tables are preset
+ * to zeroes; the result of the IDCT will be a neutral gray level.
+ */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+ struct jpeg_inverse_dct pub; /* public fields */
+
+ /* This array contains the IDCT method code that each multiplier table
+ * is currently set up for, or -1 if it's not yet set up.
+ * The actual multiplier tables are pointed to by dct_table in the
+ * per-component comp_info structures.
+ */
+ int cur_method[MAX_COMPONENTS];
+} my_idct_controller;
+
+typedef my_idct_controller * my_idct_ptr;
+
+
+/* Allocated multiplier tables: big enough for any supported variant */
+
+typedef union {
+ ISLOW_MULT_TYPE islow_array[DCTSIZE2];
+#ifdef DCT_IFAST_SUPPORTED
+ IFAST_MULT_TYPE ifast_array[DCTSIZE2];
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ FLOAT_MULT_TYPE float_array[DCTSIZE2];
+#endif
+} multiplier_table;
+
+
+/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
+ * so be sure to compile that code if either ISLOW or SCALING is requested.
+ */
+#ifdef DCT_ISLOW_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#else
+#ifdef IDCT_SCALING_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#endif
+#endif
+
+
+/*
+ * Prepare for an output pass.
+ * Here we select the proper IDCT routine for each component and build
+ * a matching multiplier table.
+ */
+
+METHODDEF(void)
+start_pass (j_decompress_ptr cinfo)
+{
+ my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
+ int ci, i;
+ jpeg_component_info *compptr;
+ int method = 0;
+ inverse_DCT_method_ptr method_ptr = NULL;
+ JQUANT_TBL * qtbl;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Select the proper IDCT routine for this component's scaling */
+ switch (compptr->DCT_scaled_size) {
+#ifdef IDCT_SCALING_SUPPORTED
+ case 1:
+ method_ptr = jpeg_idct_1x1;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+ case 2:
+ method_ptr = jpeg_idct_2x2;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+ case 4:
+ method_ptr = jpeg_idct_4x4;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+#endif
+ case DCTSIZE:
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ method_ptr = jpeg_idct_islow;
+ method = JDCT_ISLOW;
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ method_ptr = jpeg_idct_ifast;
+ method = JDCT_IFAST;
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ method_ptr = jpeg_idct_float;
+ method = JDCT_FLOAT;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ break;
+ default:
+ ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
+ break;
+ }
+ idct->pub.inverse_DCT[ci] = method_ptr;
+ /* Create multiplier table from quant table.
+ * However, we can skip this if the component is uninteresting
+ * or if we already built the table. Also, if no quant table
+ * has yet been saved for the component, we leave the
+ * multiplier table all-zero; we'll be reading zeroes from the
+ * coefficient controller's buffer anyway.
+ */
+ if (! compptr->component_needed || idct->cur_method[ci] == method)
+ continue;
+ qtbl = compptr->quant_table;
+ if (qtbl == NULL) /* happens if no data yet for component */
+ continue;
+ idct->cur_method[ci] = method;
+ switch (method) {
+#ifdef PROVIDE_ISLOW_TABLES
+ case JDCT_ISLOW:
+ {
+ /* For LL&M IDCT method, multipliers are equal to raw quantization
+ * coefficients, but are stored as ints to ensure access efficiency.
+ */
+ ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ for (i = 0; i < DCTSIZE2; i++) {
+ ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
+ }
+ }
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ {
+ /* For AA&N IDCT method, multipliers are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * For integer operation, the multiplier table is to be scaled by
+ * IFAST_SCALE_BITS.
+ */
+ IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
+#define CONST_BITS 14
+ static const INT16 aanscales[DCTSIZE2] = {
+ /* precomputed values scaled up by 14 bits */
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
+ };
+ SHIFT_TEMPS
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ ifmtbl[i] = (IFAST_MULT_TYPE)
+ DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+ (INT32) aanscales[i]),
+ CONST_BITS-IFAST_SCALE_BITS);
+ }
+ }
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ {
+ /* For float AA&N IDCT method, multipliers are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ */
+ FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
+ int row, col;
+ static const double aanscalefactor[DCTSIZE] = {
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
+ 1.0, 0.785694958, 0.541196100, 0.275899379
+ };
+
+ i = 0;
+ for (row = 0; row < DCTSIZE; row++) {
+ for (col = 0; col < DCTSIZE; col++) {
+ fmtbl[i] = (FLOAT_MULT_TYPE)
+ ((double) qtbl->quantval[i] *
+ aanscalefactor[row] * aanscalefactor[col]);
+ i++;
+ }
+ }
+ }
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ }
+}
+
+
+/*
+ * Initialize IDCT manager.
+ */
+
+GLOBAL(void)
+jinit_inverse_dct (j_decompress_ptr cinfo)
+{
+ my_idct_ptr idct;
+ int ci;
+ jpeg_component_info *compptr;
+
+ idct = (my_idct_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_idct_controller));
+ cinfo->idct = (struct jpeg_inverse_dct *) idct;
+ idct->pub.start_pass = start_pass;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Allocate and pre-zero a multiplier table for each component */
+ compptr->dct_table =
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(multiplier_table));
+ MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
+ /* Mark multiplier table not yet set up for any method */
+ idct->cur_method[ci] = -1;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdhuff.c b/test/monniaux/jpeg-6b/jdhuff.c
new file mode 100644
index 00000000..b5ba39f7
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdhuff.c
@@ -0,0 +1,651 @@
+/*
+ * jdhuff.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains Huffman entropy decoding routines.
+ *
+ * Much of the complexity here has to do with supporting input suspension.
+ * If the data source module demands suspension, we want to be able to back
+ * up to the start of the current MCU. To do this, we copy state variables
+ * into local working storage, and update them back to the permanent
+ * storage only upon successful completion of an MCU.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdhuff.h" /* Declarations shared with jdphuff.c */
+
+
+/*
+ * Expanded entropy decoder object for Huffman decoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+/* This macro is to work around compilers with missing or broken
+ * structure assignment. You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src) ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src) \
+ ((dest).last_dc_val[0] = (src).last_dc_val[0], \
+ (dest).last_dc_val[1] = (src).last_dc_val[1], \
+ (dest).last_dc_val[2] = (src).last_dc_val[2], \
+ (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+ struct jpeg_entropy_decoder pub; /* public fields */
+
+ /* These fields are loaded into local variables at start of each MCU.
+ * In case of suspension, we exit WITHOUT updating them.
+ */
+ bitread_perm_state bitstate; /* Bit buffer at start of MCU */
+ savable_state saved; /* Other state at start of MCU */
+
+ /* These fields are NOT loaded into local working state. */
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
+
+ /* Pointers to derived tables (these workspaces have image lifespan) */
+ d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
+ d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
+
+ /* Precalculated info set up by start_pass for use in decode_mcu: */
+
+ /* Pointers to derived tables to be used for each block within an MCU */
+ d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
+ d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
+ /* Whether we care about the DC and AC coefficient values for each block */
+ boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
+ boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
+} huff_entropy_decoder;
+
+typedef huff_entropy_decoder * huff_entropy_ptr;
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ */
+
+METHODDEF(void)
+start_pass_huff_decoder (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, blkn, dctbl, actbl;
+ jpeg_component_info * compptr;
+
+ /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
+ * This ought to be an error condition, but we make it a warning because
+ * there are some baseline files out there with all zeroes in these bytes.
+ */
+ if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
+ cinfo->Ah != 0 || cinfo->Al != 0)
+ WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ /* Compute derived values for Huffman tables */
+ /* We may do this more than once for a table, but it's not expensive */
+ jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
+ & entropy->dc_derived_tbls[dctbl]);
+ jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
+ & entropy->ac_derived_tbls[actbl]);
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Precalculate decoding info for each block in an MCU of this scan */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ /* Precalculate which table to use for each block */
+ entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
+ entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
+ /* Decide whether we really care about the coefficient values */
+ if (compptr->component_needed) {
+ entropy->dc_needed[blkn] = TRUE;
+ /* we don't need the ACs if producing a 1/8th-size image */
+ entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
+ } else {
+ entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
+ }
+ }
+
+ /* Initialize bitread state variables */
+ entropy->bitstate.bits_left = 0;
+ entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
+ entropy->pub.insufficient_data = FALSE;
+
+ /* Initialize restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+}
+
+
+/*
+ * Compute the derived values for a Huffman table.
+ * This routine also performs some validation checks on the table.
+ *
+ * Note this is also used by jdphuff.c.
+ */
+
+GLOBAL(void)
+jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
+ d_derived_tbl ** pdtbl)
+{
+ JHUFF_TBL *htbl;
+ d_derived_tbl *dtbl;
+ int p, i, l, si, numsymbols;
+ int lookbits, ctr;
+ char huffsize[257];
+ unsigned int huffcode[257];
+ unsigned int code;
+
+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
+ * paralleling the order of the symbols themselves in htbl->huffval[].
+ */
+
+ /* Find the input Huffman table */
+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+ htbl =
+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+
+ /* Allocate a workspace if we haven't already done so. */
+ if (*pdtbl == NULL)
+ *pdtbl = (d_derived_tbl *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(d_derived_tbl));
+ dtbl = *pdtbl;
+ dtbl->pub = htbl; /* fill in back link */
+
+ /* Figure C.1: make table of Huffman code length for each symbol */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ i = (int) htbl->bits[l];
+ if (i < 0 || p + i > 256) /* protect against table overrun */
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ while (i--)
+ huffsize[p++] = (char) l;
+ }
+ huffsize[p] = 0;
+ numsymbols = p;
+
+ /* Figure C.2: generate the codes themselves */
+ /* We also validate that the counts represent a legal Huffman code tree. */
+
+ code = 0;
+ si = huffsize[0];
+ p = 0;
+ while (huffsize[p]) {
+ while (((int) huffsize[p]) == si) {
+ huffcode[p++] = code;
+ code++;
+ }
+ /* code is now 1 more than the last code used for codelength si; but
+ * it must still fit in si bits, since no code is allowed to be all ones.
+ */
+ if (((INT32) code) >= (((INT32) 1) << si))
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ code <<= 1;
+ si++;
+ }
+
+ /* Figure F.15: generate decoding tables for bit-sequential decoding */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ if (htbl->bits[l]) {
+ /* valoffset[l] = huffval[] index of 1st symbol of code length l,
+ * minus the minimum code of length l
+ */
+ dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
+ p += htbl->bits[l];
+ dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
+ } else {
+ dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
+ }
+ }
+ dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
+
+ /* Compute lookahead tables to speed up decoding.
+ * First we set all the table entries to 0, indicating "too long";
+ * then we iterate through the Huffman codes that are short enough and
+ * fill in all the entries that correspond to bit sequences starting
+ * with that code.
+ */
+
+ MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
+
+ p = 0;
+ for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
+ for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
+ /* l = current code's length, p = its index in huffcode[] & huffval[]. */
+ /* Generate left-justified code followed by all possible bit sequences */
+ lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
+ for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
+ dtbl->look_nbits[lookbits] = l;
+ dtbl->look_sym[lookbits] = htbl->huffval[p];
+ lookbits++;
+ }
+ }
+ }
+
+ /* Validate symbols as being reasonable.
+ * For AC tables, we make no check, but accept all byte values 0..255.
+ * For DC tables, we require the symbols to be in range 0..15.
+ * (Tighter bounds could be applied depending on the data depth and mode,
+ * but this is sufficient to ensure safe decoding.)
+ */
+ if (isDC) {
+ for (i = 0; i < numsymbols; i++) {
+ int sym = htbl->huffval[i];
+ if (sym < 0 || sym > 15)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ }
+ }
+}
+
+
+/*
+ * Out-of-line code for bit fetching (shared with jdphuff.c).
+ * See jdhuff.h for info about usage.
+ * Note: current values of get_buffer and bits_left are passed as parameters,
+ * but are returned in the corresponding fields of the state struct.
+ *
+ * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
+ * of get_buffer to be used. (On machines with wider words, an even larger
+ * buffer could be used.) However, on some machines 32-bit shifts are
+ * quite slow and take time proportional to the number of places shifted.
+ * (This is true with most PC compilers, for instance.) In this case it may
+ * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
+ * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
+ */
+
+#ifdef SLOW_SHIFT_32
+#define MIN_GET_BITS 15 /* minimum allowable value */
+#else
+#define MIN_GET_BITS (BIT_BUF_SIZE-7)
+#endif
+
+
+GLOBAL(boolean)
+jpeg_fill_bit_buffer (bitread_working_state * state,
+ register bit_buf_type get_buffer, register int bits_left,
+ int nbits)
+/* Load up the bit buffer to a depth of at least nbits */
+{
+ /* Copy heavily used state fields into locals (hopefully registers) */
+ register const JOCTET * next_input_byte = state->next_input_byte;
+ register size_t bytes_in_buffer = state->bytes_in_buffer;
+ j_decompress_ptr cinfo = state->cinfo;
+
+ /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
+ /* (It is assumed that no request will be for more than that many bits.) */
+ /* We fail to do so only if we hit a marker or are forced to suspend. */
+
+ if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
+ while (bits_left < MIN_GET_BITS) {
+ register int c;
+
+ /* Attempt to read a byte */
+ if (bytes_in_buffer == 0) {
+ if (! (*cinfo->src->fill_input_buffer) (cinfo))
+ return FALSE;
+ next_input_byte = cinfo->src->next_input_byte;
+ bytes_in_buffer = cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+
+ /* If it's 0xFF, check and discard stuffed zero byte */
+ if (c == 0xFF) {
+ /* Loop here to discard any padding FF's on terminating marker,
+ * so that we can save a valid unread_marker value. NOTE: we will
+ * accept multiple FF's followed by a 0 as meaning a single FF data
+ * byte. This data pattern is not valid according to the standard.
+ */
+ do {
+ if (bytes_in_buffer == 0) {
+ if (! (*cinfo->src->fill_input_buffer) (cinfo))
+ return FALSE;
+ next_input_byte = cinfo->src->next_input_byte;
+ bytes_in_buffer = cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+ } while (c == 0xFF);
+
+ if (c == 0) {
+ /* Found FF/00, which represents an FF data byte */
+ c = 0xFF;
+ } else {
+ /* Oops, it's actually a marker indicating end of compressed data.
+ * Save the marker code for later use.
+ * Fine point: it might appear that we should save the marker into
+ * bitread working state, not straight into permanent state. But
+ * once we have hit a marker, we cannot need to suspend within the
+ * current MCU, because we will read no more bytes from the data
+ * source. So it is OK to update permanent state right away.
+ */
+ cinfo->unread_marker = c;
+ /* See if we need to insert some fake zero bits. */
+ goto no_more_bytes;
+ }
+ }
+
+ /* OK, load c into get_buffer */
+ get_buffer = (get_buffer << 8) | c;
+ bits_left += 8;
+ } /* end while */
+ } else {
+ no_more_bytes:
+ /* We get here if we've read the marker that terminates the compressed
+ * data segment. There should be enough bits in the buffer register
+ * to satisfy the request; if so, no problem.
+ */
+ if (nbits > bits_left) {
+ /* Uh-oh. Report corrupted data to user and stuff zeroes into
+ * the data stream, so that we can produce some kind of image.
+ * We use a nonvolatile flag to ensure that only one warning message
+ * appears per data segment.
+ */
+ if (! cinfo->entropy->insufficient_data) {
+ WARNMS(cinfo, JWRN_HIT_MARKER);
+ cinfo->entropy->insufficient_data = TRUE;
+ }
+ /* Fill the buffer with zero bits */
+ get_buffer <<= MIN_GET_BITS - bits_left;
+ bits_left = MIN_GET_BITS;
+ }
+ }
+
+ /* Unload the local registers */
+ state->next_input_byte = next_input_byte;
+ state->bytes_in_buffer = bytes_in_buffer;
+ state->get_buffer = get_buffer;
+ state->bits_left = bits_left;
+
+ return TRUE;
+}
+
+
+/*
+ * Out-of-line code for Huffman code decoding.
+ * See jdhuff.h for info about usage.
+ */
+
+GLOBAL(int)
+jpeg_huff_decode (bitread_working_state * state,
+ register bit_buf_type get_buffer, register int bits_left,
+ d_derived_tbl * htbl, int min_bits)
+{
+ register int l = min_bits;
+ register INT32 code;
+
+ /* HUFF_DECODE has determined that the code is at least min_bits */
+ /* bits long, so fetch that many bits in one swoop. */
+
+ CHECK_BIT_BUFFER(*state, l, return -1);
+ code = GET_BITS(l);
+
+ /* Collect the rest of the Huffman code one bit at a time. */
+ /* This is per Figure F.16 in the JPEG spec. */
+
+ while (code > htbl->maxcode[l]) {
+ code <<= 1;
+ CHECK_BIT_BUFFER(*state, 1, return -1);
+ code |= GET_BITS(1);
+ l++;
+ }
+
+ /* Unload the local registers */
+ state->get_buffer = get_buffer;
+ state->bits_left = bits_left;
+
+ /* With garbage input we may reach the sentinel value l = 17. */
+
+ if (l > 16) {
+ WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
+ return 0; /* fake a zero as the safest result */
+ }
+
+ return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
+}
+
+
+/*
+ * Figure F.12: extend sign bit.
+ * On some machines, a shift and add will be faster than a table lookup.
+ */
+
+#ifdef AVOID_TABLES
+
+#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
+
+#else
+
+#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
+
+static const int extend_test[16] = /* entry n is 2**(n-1) */
+ { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
+ 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
+
+static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
+ { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
+ ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
+ ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
+ ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
+
+#endif /* AVOID_TABLES */
+
+
+/*
+ * Check for a restart marker & resynchronize decoder.
+ * Returns FALSE if must suspend.
+ */
+
+LOCAL(boolean)
+process_restart (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci;
+
+ /* Throw away any unused bits remaining in bit buffer; */
+ /* include any full bytes in next_marker's count of discarded bytes */
+ cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
+ entropy->bitstate.bits_left = 0;
+
+ /* Advance past the RSTn marker */
+ if (! (*cinfo->marker->read_restart_marker) (cinfo))
+ return FALSE;
+
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++)
+ entropy->saved.last_dc_val[ci] = 0;
+
+ /* Reset restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+
+ /* Reset out-of-data flag, unless read_restart_marker left us smack up
+ * against a marker. In that case we will end up treating the next data
+ * segment as empty, and we can avoid producing bogus output pixels by
+ * leaving the flag set.
+ */
+ if (cinfo->unread_marker == 0)
+ entropy->pub.insufficient_data = FALSE;
+
+ return TRUE;
+}
+
+
+/*
+ * Decode and return one MCU's worth of Huffman-compressed coefficients.
+ * The coefficients are reordered from zigzag order into natural array order,
+ * but are not dequantized.
+ *
+ * The i'th block of the MCU is stored into the block pointed to by
+ * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
+ * (Wholesale zeroing is usually a little faster than retail...)
+ *
+ * Returns FALSE if data source requested suspension. In that case no
+ * changes have been made to permanent state. (Exception: some output
+ * coefficients may already have been assigned. This is harmless for
+ * this module, since we'll just re-assign them on the next call.)
+ */
+
+METHODDEF(boolean)
+decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int blkn;
+ BITREAD_STATE_VARS;
+ savable_state state;
+
+ /* Process restart marker if needed; may have to suspend */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
+ }
+
+ /* If we've run out of data, just leave the MCU set to zeroes.
+ * This way, we return uniform gray for the remainder of the segment.
+ */
+ if (! entropy->pub.insufficient_data) {
+
+ /* Load up working state */
+ BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ ASSIGN_STATE(state, entropy->saved);
+
+ /* Outer loop handles each block in the MCU */
+
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ JBLOCKROW block = MCU_data[blkn];
+ d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
+ d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
+ register int s, k, r;
+
+ /* Decode a single block's worth of coefficients */
+
+ /* Section F.2.2.1: decode the DC coefficient difference */
+ HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
+ if (s) {
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ r = GET_BITS(s);
+ s = HUFF_EXTEND(r, s);
+ }
+
+ if (entropy->dc_needed[blkn]) {
+ /* Convert DC difference to actual value, update last_dc_val */
+ int ci = cinfo->MCU_membership[blkn];
+ s += state.last_dc_val[ci];
+ state.last_dc_val[ci] = s;
+ /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
+ (*block)[0] = (JCOEF) s;
+ }
+
+ if (entropy->ac_needed[blkn]) {
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* Since zeroes are skipped, output area must be cleared beforehand */
+ for (k = 1; k < DCTSIZE2; k++) {
+ HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
+
+ r = s >> 4;
+ s &= 15;
+
+ if (s) {
+ k += r;
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ r = GET_BITS(s);
+ s = HUFF_EXTEND(r, s);
+ /* Output coefficient in natural (dezigzagged) order.
+ * Note: the extra entries in jpeg_natural_order[] will save us
+ * if k >= DCTSIZE2, which could happen if the data is corrupted.
+ */
+ (*block)[jpeg_natural_order[k]] = (JCOEF) s;
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
+ }
+
+ } else {
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* In this path we just discard the values */
+ for (k = 1; k < DCTSIZE2; k++) {
+ HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
+
+ r = s >> 4;
+ s &= 15;
+
+ if (s) {
+ k += r;
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ DROP_BITS(s);
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
+ }
+
+ }
+ }
+
+ /* Completed MCU, so update state */
+ BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ ASSIGN_STATE(entropy->saved, state);
+ }
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
+}
+
+
+/*
+ * Module initialization routine for Huffman entropy decoding.
+ */
+
+GLOBAL(void)
+jinit_huff_decoder (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy;
+ int i;
+
+ entropy = (huff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(huff_entropy_decoder));
+ cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
+ entropy->pub.start_pass = start_pass_huff_decoder;
+ entropy->pub.decode_mcu = decode_mcu;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdhuff.h b/test/monniaux/jpeg-6b/jdhuff.h
new file mode 100644
index 00000000..ae19b6ca
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdhuff.h
@@ -0,0 +1,201 @@
+/*
+ * jdhuff.h
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains declarations for Huffman entropy decoding routines
+ * that are shared between the sequential decoder (jdhuff.c) and the
+ * progressive decoder (jdphuff.c). No other modules need to see these.
+ */
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_make_d_derived_tbl jMkDDerived
+#define jpeg_fill_bit_buffer jFilBitBuf
+#define jpeg_huff_decode jHufDecode
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Derived data constructed for each Huffman table */
+
+#define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
+
+typedef struct {
+ /* Basic tables: (element [0] of each array is unused) */
+ INT32 maxcode[18]; /* largest code of length k (-1 if none) */
+ /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
+ INT32 valoffset[17]; /* huffval[] offset for codes of length k */
+ /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
+ * the smallest code of length k; so given a code of length k, the
+ * corresponding symbol is huffval[code + valoffset[k]]
+ */
+
+ /* Link to public Huffman table (needed only in jpeg_huff_decode) */
+ JHUFF_TBL *pub;
+
+ /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
+ * the input data stream. If the next Huffman code is no more
+ * than HUFF_LOOKAHEAD bits long, we can obtain its length and
+ * the corresponding symbol directly from these tables.
+ */
+ int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
+ UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
+} d_derived_tbl;
+
+/* Expand a Huffman table definition into the derived format */
+EXTERN(void) jpeg_make_d_derived_tbl
+ JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
+ d_derived_tbl ** pdtbl));
+
+
+/*
+ * Fetching the next N bits from the input stream is a time-critical operation
+ * for the Huffman decoders. We implement it with a combination of inline
+ * macros and out-of-line subroutines. Note that N (the number of bits
+ * demanded at one time) never exceeds 15 for JPEG use.
+ *
+ * We read source bytes into get_buffer and dole out bits as needed.
+ * If get_buffer already contains enough bits, they are fetched in-line
+ * by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough
+ * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
+ * as full as possible (not just to the number of bits needed; this
+ * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
+ * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
+ * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
+ * at least the requested number of bits --- dummy zeroes are inserted if
+ * necessary.
+ */
+
+typedef INT32 bit_buf_type; /* type of bit-extraction buffer */
+#define BIT_BUF_SIZE 32 /* size of buffer in bits */
+
+/* If long is > 32 bits on your machine, and shifting/masking longs is
+ * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
+ * appropriately should be a win. Unfortunately we can't define the size
+ * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
+ * because not all machines measure sizeof in 8-bit bytes.
+ */
+
+typedef struct { /* Bitreading state saved across MCUs */
+ bit_buf_type get_buffer; /* current bit-extraction buffer */
+ int bits_left; /* # of unused bits in it */
+} bitread_perm_state;
+
+typedef struct { /* Bitreading working state within an MCU */
+ /* Current data source location */
+ /* We need a copy, rather than munging the original, in case of suspension */
+ const JOCTET * next_input_byte; /* => next byte to read from source */
+ size_t bytes_in_buffer; /* # of bytes remaining in source buffer */
+ /* Bit input buffer --- note these values are kept in register variables,
+ * not in this struct, inside the inner loops.
+ */
+ bit_buf_type get_buffer; /* current bit-extraction buffer */
+ int bits_left; /* # of unused bits in it */
+ /* Pointer needed by jpeg_fill_bit_buffer. */
+ j_decompress_ptr cinfo; /* back link to decompress master record */
+} bitread_working_state;
+
+/* Macros to declare and load/save bitread local variables. */
+#define BITREAD_STATE_VARS \
+ register bit_buf_type get_buffer; \
+ register int bits_left; \
+ bitread_working_state br_state
+
+#define BITREAD_LOAD_STATE(cinfop,permstate) \
+ br_state.cinfo = cinfop; \
+ br_state.next_input_byte = cinfop->src->next_input_byte; \
+ br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
+ get_buffer = permstate.get_buffer; \
+ bits_left = permstate.bits_left;
+
+#define BITREAD_SAVE_STATE(cinfop,permstate) \
+ cinfop->src->next_input_byte = br_state.next_input_byte; \
+ cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
+ permstate.get_buffer = get_buffer; \
+ permstate.bits_left = bits_left
+
+/*
+ * These macros provide the in-line portion of bit fetching.
+ * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
+ * before using GET_BITS, PEEK_BITS, or DROP_BITS.
+ * The variables get_buffer and bits_left are assumed to be locals,
+ * but the state struct might not be (jpeg_huff_decode needs this).
+ * CHECK_BIT_BUFFER(state,n,action);
+ * Ensure there are N bits in get_buffer; if suspend, take action.
+ * val = GET_BITS(n);
+ * Fetch next N bits.
+ * val = PEEK_BITS(n);
+ * Fetch next N bits without removing them from the buffer.
+ * DROP_BITS(n);
+ * Discard next N bits.
+ * The value N should be a simple variable, not an expression, because it
+ * is evaluated multiple times.
+ */
+
+#define CHECK_BIT_BUFFER(state,nbits,action) \
+ { if (bits_left < (nbits)) { \
+ if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \
+ { action; } \
+ get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
+
+#define GET_BITS(nbits) \
+ (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
+
+#define PEEK_BITS(nbits) \
+ (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1))
+
+#define DROP_BITS(nbits) \
+ (bits_left -= (nbits))
+
+/* Load up the bit buffer to a depth of at least nbits */
+EXTERN(boolean) jpeg_fill_bit_buffer
+ JPP((bitread_working_state * state, register bit_buf_type get_buffer,
+ register int bits_left, int nbits));
+
+
+/*
+ * Code for extracting next Huffman-coded symbol from input bit stream.
+ * Again, this is time-critical and we make the main paths be macros.
+ *
+ * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
+ * without looping. Usually, more than 95% of the Huffman codes will be 8
+ * or fewer bits long. The few overlength codes are handled with a loop,
+ * which need not be inline code.
+ *
+ * Notes about the HUFF_DECODE macro:
+ * 1. Near the end of the data segment, we may fail to get enough bits
+ * for a lookahead. In that case, we do it the hard way.
+ * 2. If the lookahead table contains no entry, the next code must be
+ * more than HUFF_LOOKAHEAD bits long.
+ * 3. jpeg_huff_decode returns -1 if forced to suspend.
+ */
+
+#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
+{ register int nb, look; \
+ if (bits_left < HUFF_LOOKAHEAD) { \
+ if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
+ get_buffer = state.get_buffer; bits_left = state.bits_left; \
+ if (bits_left < HUFF_LOOKAHEAD) { \
+ nb = 1; goto slowlabel; \
+ } \
+ } \
+ look = PEEK_BITS(HUFF_LOOKAHEAD); \
+ if ((nb = htbl->look_nbits[look]) != 0) { \
+ DROP_BITS(nb); \
+ result = htbl->look_sym[look]; \
+ } else { \
+ nb = HUFF_LOOKAHEAD+1; \
+slowlabel: \
+ if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
+ { failaction; } \
+ get_buffer = state.get_buffer; bits_left = state.bits_left; \
+ } \
+}
+
+/* Out-of-line case for Huffman code fetching */
+EXTERN(int) jpeg_huff_decode
+ JPP((bitread_working_state * state, register bit_buf_type get_buffer,
+ register int bits_left, d_derived_tbl * htbl, int min_bits));
diff --git a/test/monniaux/jpeg-6b/jdinput.c b/test/monniaux/jpeg-6b/jdinput.c
new file mode 100644
index 00000000..0c2ac8f1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdinput.c
@@ -0,0 +1,381 @@
+/*
+ * jdinput.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains input control logic for the JPEG decompressor.
+ * These routines are concerned with controlling the decompressor's input
+ * processing (marker reading and coefficient decoding). The actual input
+ * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_input_controller pub; /* public fields */
+
+ boolean inheaders; /* TRUE until first SOS is reached */
+} my_input_controller;
+
+typedef my_input_controller * my_inputctl_ptr;
+
+
+/* Forward declarations */
+METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Routines to calculate various quantities related to the size of the image.
+ */
+
+LOCAL(void)
+initial_setup (j_decompress_ptr cinfo)
+/* Called once, when first SOS marker is reached */
+{
+ int ci;
+ jpeg_component_info *compptr;
+
+ /* Make sure image isn't bigger than I can handle */
+ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+ (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+ /* For now, precision must match compiled-in value... */
+ if (cinfo->data_precision != BITS_IN_JSAMPLE)
+ ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+ /* Check that number of components won't exceed internal array sizes */
+ if (cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+
+ /* Compute maximum sampling factors; check factor validity */
+ cinfo->max_h_samp_factor = 1;
+ cinfo->max_v_samp_factor = 1;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
+ compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
+ ERREXIT(cinfo, JERR_BAD_SAMPLING);
+ cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
+ compptr->h_samp_factor);
+ cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
+ compptr->v_samp_factor);
+ }
+
+ /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
+ * In the full decompressor, this will be overridden by jdmaster.c;
+ * but in the transcoder, jdmaster.c is not used, so we must do it here.
+ */
+ cinfo->min_DCT_scaled_size = DCTSIZE;
+
+ /* Compute dimensions of components */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ compptr->DCT_scaled_size = DCTSIZE;
+ /* Size in DCT blocks */
+ compptr->width_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->height_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ /* downsampled_width and downsampled_height will also be overridden by
+ * jdmaster.c if we are doing full decompression. The transcoder library
+ * doesn't use these values, but the calling application might.
+ */
+ /* Size in samples */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) cinfo->max_h_samp_factor);
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) cinfo->max_v_samp_factor);
+ /* Mark component needed, until color conversion says otherwise */
+ compptr->component_needed = TRUE;
+ /* Mark no quantization table yet saved for component */
+ compptr->quant_table = NULL;
+ }
+
+ /* Compute number of fully interleaved MCU rows. */
+ cinfo->total_iMCU_rows = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ /* Decide whether file contains multiple scans */
+ if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
+ cinfo->inputctl->has_multiple_scans = TRUE;
+ else
+ cinfo->inputctl->has_multiple_scans = FALSE;
+}
+
+
+LOCAL(void)
+per_scan_setup (j_decompress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
+{
+ int ci, mcublks, tmp;
+ jpeg_component_info *compptr;
+
+ if (cinfo->comps_in_scan == 1) {
+
+ /* Noninterleaved (single-component) scan */
+ compptr = cinfo->cur_comp_info[0];
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = compptr->width_in_blocks;
+ cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+
+ /* For noninterleaved scan, always one block per MCU */
+ compptr->MCU_width = 1;
+ compptr->MCU_height = 1;
+ compptr->MCU_blocks = 1;
+ compptr->MCU_sample_width = compptr->DCT_scaled_size;
+ compptr->last_col_width = 1;
+ /* For noninterleaved scans, it is convenient to define last_row_height
+ * as the number of block rows present in the last iMCU row.
+ */
+ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (tmp == 0) tmp = compptr->v_samp_factor;
+ compptr->last_row_height = tmp;
+
+ /* Prepare array describing MCU composition */
+ cinfo->blocks_in_MCU = 1;
+ cinfo->MCU_membership[0] = 0;
+
+ } else {
+
+ /* Interleaved (multi-component) scan */
+ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+ MAX_COMPS_IN_SCAN);
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width,
+ (long) (cinfo->max_h_samp_factor*DCTSIZE));
+ cinfo->MCU_rows_in_scan = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ cinfo->blocks_in_MCU = 0;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Sampling factors give # of blocks of component in each MCU */
+ compptr->MCU_width = compptr->h_samp_factor;
+ compptr->MCU_height = compptr->v_samp_factor;
+ compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+ compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
+ /* Figure number of non-dummy blocks in last MCU column & row */
+ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+ if (tmp == 0) tmp = compptr->MCU_width;
+ compptr->last_col_width = tmp;
+ tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+ if (tmp == 0) tmp = compptr->MCU_height;
+ compptr->last_row_height = tmp;
+ /* Prepare array describing MCU composition */
+ mcublks = compptr->MCU_blocks;
+ if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
+ ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+ while (mcublks-- > 0) {
+ cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+ }
+ }
+
+ }
+}
+
+
+/*
+ * Save away a copy of the Q-table referenced by each component present
+ * in the current scan, unless already saved during a prior scan.
+ *
+ * In a multiple-scan JPEG file, the encoder could assign different components
+ * the same Q-table slot number, but change table definitions between scans
+ * so that each component uses a different Q-table. (The IJG encoder is not
+ * currently capable of doing this, but other encoders might.) Since we want
+ * to be able to dequantize all the components at the end of the file, this
+ * means that we have to save away the table actually used for each component.
+ * We do this by copying the table at the start of the first scan containing
+ * the component.
+ * The JPEG spec prohibits the encoder from changing the contents of a Q-table
+ * slot between scans of a component using that slot. If the encoder does so
+ * anyway, this decoder will simply use the Q-table values that were current
+ * at the start of the first scan for the component.
+ *
+ * The decompressor output side looks only at the saved quant tables,
+ * not at the current Q-table slots.
+ */
+
+LOCAL(void)
+latch_quant_tables (j_decompress_ptr cinfo)
+{
+ int ci, qtblno;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtbl;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* No work if we already saved Q-table for this component */
+ if (compptr->quant_table != NULL)
+ continue;
+ /* Make sure specified quantization table is present */
+ qtblno = compptr->quant_tbl_no;
+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+ /* OK, save away the quantization table */
+ qtbl = (JQUANT_TBL *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(JQUANT_TBL));
+ MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
+ compptr->quant_table = qtbl;
+ }
+}
+
+
+/*
+ * Initialize the input modules to read a scan of compressed data.
+ * The first call to this is done by jdmaster.c after initializing
+ * the entire decompressor (during jpeg_start_decompress).
+ * Subsequent calls come from consume_markers, below.
+ */
+
+METHODDEF(void)
+start_input_pass (j_decompress_ptr cinfo)
+{
+ per_scan_setup(cinfo);
+ latch_quant_tables(cinfo);
+ (*cinfo->entropy->start_pass) (cinfo);
+ (*cinfo->coef->start_input_pass) (cinfo);
+ cinfo->inputctl->consume_input = cinfo->coef->consume_data;
+}
+
+
+/*
+ * Finish up after inputting a compressed-data scan.
+ * This is called by the coefficient controller after it's read all
+ * the expected data of the scan.
+ */
+
+METHODDEF(void)
+finish_input_pass (j_decompress_ptr cinfo)
+{
+ cinfo->inputctl->consume_input = consume_markers;
+}
+
+
+/*
+ * Read JPEG markers before, between, or after compressed-data scans.
+ * Change state as necessary when a new scan is reached.
+ * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ *
+ * The consume_input method pointer points either here or to the
+ * coefficient controller's consume_data routine, depending on whether
+ * we are reading a compressed data segment or inter-segment markers.
+ */
+
+METHODDEF(int)
+consume_markers (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
+ int val;
+
+ if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
+ return JPEG_REACHED_EOI;
+
+ val = (*cinfo->marker->read_markers) (cinfo);
+
+ switch (val) {
+ case JPEG_REACHED_SOS: /* Found SOS */
+ if (inputctl->inheaders) { /* 1st SOS */
+ initial_setup(cinfo);
+ inputctl->inheaders = FALSE;
+ /* Note: start_input_pass must be called by jdmaster.c
+ * before any more input can be consumed. jdapimin.c is
+ * responsible for enforcing this sequencing.
+ */
+ } else { /* 2nd or later SOS marker */
+ if (! inputctl->pub.has_multiple_scans)
+ ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
+ start_input_pass(cinfo);
+ }
+ break;
+ case JPEG_REACHED_EOI: /* Found EOI */
+ inputctl->pub.eoi_reached = TRUE;
+ if (inputctl->inheaders) { /* Tables-only datastream, apparently */
+ if (cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOF_NO_SOS);
+ } else {
+ /* Prevent infinite loop in coef ctlr's decompress_data routine
+ * if user set output_scan_number larger than number of scans.
+ */
+ if (cinfo->output_scan_number > cinfo->input_scan_number)
+ cinfo->output_scan_number = cinfo->input_scan_number;
+ }
+ break;
+ case JPEG_SUSPENDED:
+ break;
+ }
+
+ return val;
+}
+
+
+/*
+ * Reset state to begin a fresh datastream.
+ */
+
+METHODDEF(void)
+reset_input_controller (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
+
+ inputctl->pub.consume_input = consume_markers;
+ inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
+ inputctl->pub.eoi_reached = FALSE;
+ inputctl->inheaders = TRUE;
+ /* Reset other modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->marker->reset_marker_reader) (cinfo);
+ /* Reset progression state -- would be cleaner if entropy decoder did this */
+ cinfo->coef_bits = NULL;
+}
+
+
+/*
+ * Initialize the input controller module.
+ * This is called only once, when the decompression object is created.
+ */
+
+GLOBAL(void)
+jinit_input_controller (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl;
+
+ /* Create subobject in permanent pool */
+ inputctl = (my_inputctl_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_input_controller));
+ cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
+ /* Initialize method pointers */
+ inputctl->pub.consume_input = consume_markers;
+ inputctl->pub.reset_input_controller = reset_input_controller;
+ inputctl->pub.start_input_pass = start_input_pass;
+ inputctl->pub.finish_input_pass = finish_input_pass;
+ /* Initialize state: can't use reset_input_controller since we don't
+ * want to try to reset other modules yet.
+ */
+ inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
+ inputctl->pub.eoi_reached = FALSE;
+ inputctl->inheaders = TRUE;
+}
diff --git a/test/monniaux/jpeg-6b/jdmainct.c b/test/monniaux/jpeg-6b/jdmainct.c
new file mode 100644
index 00000000..13c956f5
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdmainct.c
@@ -0,0 +1,512 @@
+/*
+ * jdmainct.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the main buffer controller for decompression.
+ * The main buffer lies between the JPEG decompressor proper and the
+ * post-processor; it holds downsampled data in the JPEG colorspace.
+ *
+ * Note that this code is bypassed in raw-data mode, since the application
+ * supplies the equivalent of the main buffer in that case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * In the current system design, the main buffer need never be a full-image
+ * buffer; any full-height buffers will be found inside the coefficient or
+ * postprocessing controllers. Nonetheless, the main controller is not
+ * trivial. Its responsibility is to provide context rows for upsampling/
+ * rescaling, and doing this in an efficient fashion is a bit tricky.
+ *
+ * Postprocessor input data is counted in "row groups". A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component. (We require DCT_scaled_size values to be
+ * chosen such that these numbers are integers. In practice DCT_scaled_size
+ * values will likely be powers of two, so we actually have the stronger
+ * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
+ * Upsampling will typically produce max_v_samp_factor pixel rows from each
+ * row group (times any additional scale factor that the upsampler is
+ * applying).
+ *
+ * The coefficient controller will deliver data to us one iMCU row at a time;
+ * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
+ * exactly min_DCT_scaled_size row groups. (This amount of data corresponds
+ * to one row of MCUs when the image is fully interleaved.) Note that the
+ * number of sample rows varies across components, but the number of row
+ * groups does not. Some garbage sample rows may be included in the last iMCU
+ * row at the bottom of the image.
+ *
+ * Depending on the vertical scaling algorithm used, the upsampler may need
+ * access to the sample row(s) above and below its current input row group.
+ * The upsampler is required to set need_context_rows TRUE at global selection
+ * time if so. When need_context_rows is FALSE, this controller can simply
+ * obtain one iMCU row at a time from the coefficient controller and dole it
+ * out as row groups to the postprocessor.
+ *
+ * When need_context_rows is TRUE, this controller guarantees that the buffer
+ * passed to postprocessing contains at least one row group's worth of samples
+ * above and below the row group(s) being processed. Note that the context
+ * rows "above" the first passed row group appear at negative row offsets in
+ * the passed buffer. At the top and bottom of the image, the required
+ * context rows are manufactured by duplicating the first or last real sample
+ * row; this avoids having special cases in the upsampling inner loops.
+ *
+ * The amount of context is fixed at one row group just because that's a
+ * convenient number for this controller to work with. The existing
+ * upsamplers really only need one sample row of context. An upsampler
+ * supporting arbitrary output rescaling might wish for more than one row
+ * group of context when shrinking the image; tough, we don't handle that.
+ * (This is justified by the assumption that downsizing will be handled mostly
+ * by adjusting the DCT_scaled_size values, so that the actual scale factor at
+ * the upsample step needn't be much less than one.)
+ *
+ * To provide the desired context, we have to retain the last two row groups
+ * of one iMCU row while reading in the next iMCU row. (The last row group
+ * can't be processed until we have another row group for its below-context,
+ * and so we have to save the next-to-last group too for its above-context.)
+ * We could do this most simply by copying data around in our buffer, but
+ * that'd be very slow. We can avoid copying any data by creating a rather
+ * strange pointer structure. Here's how it works. We allocate a workspace
+ * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
+ * of row groups per iMCU row). We create two sets of redundant pointers to
+ * the workspace. Labeling the physical row groups 0 to M+1, the synthesized
+ * pointer lists look like this:
+ * M+1 M-1
+ * master pointer --> 0 master pointer --> 0
+ * 1 1
+ * ... ...
+ * M-3 M-3
+ * M-2 M
+ * M-1 M+1
+ * M M-2
+ * M+1 M-1
+ * 0 0
+ * We read alternate iMCU rows using each master pointer; thus the last two
+ * row groups of the previous iMCU row remain un-overwritten in the workspace.
+ * The pointer lists are set up so that the required context rows appear to
+ * be adjacent to the proper places when we pass the pointer lists to the
+ * upsampler.
+ *
+ * The above pictures describe the normal state of the pointer lists.
+ * At top and bottom of the image, we diddle the pointer lists to duplicate
+ * the first or last sample row as necessary (this is cheaper than copying
+ * sample rows around).
+ *
+ * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
+ * situation each iMCU row provides only one row group so the buffering logic
+ * must be different (eg, we must read two iMCU rows before we can emit the
+ * first row group). For now, we simply do not support providing context
+ * rows when min_DCT_scaled_size is 1. That combination seems unlikely to
+ * be worth providing --- if someone wants a 1/8th-size preview, they probably
+ * want it quick and dirty, so a context-free upsampler is sufficient.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_main_controller pub; /* public fields */
+
+ /* Pointer to allocated workspace (M or M+2 row groups). */
+ JSAMPARRAY buffer[MAX_COMPONENTS];
+
+ boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
+ JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */
+
+ /* Remaining fields are only used in the context case. */
+
+ /* These are the master pointers to the funny-order pointer lists. */
+ JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */
+
+ int whichptr; /* indicates which pointer set is now in use */
+ int context_state; /* process_data state machine status */
+ JDIMENSION rowgroups_avail; /* row groups available to postprocessor */
+ JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+/* context_state values: */
+#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */
+#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */
+#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */
+
+
+/* Forward declarations */
+METHODDEF(void) process_data_simple_main
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+METHODDEF(void) process_data_context_main
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF(void) process_data_crank_post
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#endif
+
+
+LOCAL(void)
+alloc_funny_pointers (j_decompress_ptr cinfo)
+/* Allocate space for the funny pointer lists.
+ * This is done only once, not once per pass.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf;
+
+ /* Get top-level space for component array pointers.
+ * We alloc both arrays with one call to save a few cycles.
+ */
+ main->xbuffer[0] = (JSAMPIMAGE)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
+ main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ /* Get space for pointer lists --- M+4 row groups in each list.
+ * We alloc both pointer lists with one call to save a few cycles.
+ */
+ xbuf = (JSAMPARRAY)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
+ xbuf += rgroup; /* want one row group at negative offsets */
+ main->xbuffer[0][ci] = xbuf;
+ xbuf += rgroup * (M + 4);
+ main->xbuffer[1][ci] = xbuf;
+ }
+}
+
+
+LOCAL(void)
+make_funny_pointers (j_decompress_ptr cinfo)
+/* Create the funny pointer lists discussed in the comments above.
+ * The actual workspace is already allocated (in main->buffer),
+ * and the space for the pointer lists is allocated too.
+ * This routine just fills in the curiously ordered lists.
+ * This will be repeated at the beginning of each pass.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY buf, xbuf0, xbuf1;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ xbuf0 = main->xbuffer[0][ci];
+ xbuf1 = main->xbuffer[1][ci];
+ /* First copy the workspace pointers as-is */
+ buf = main->buffer[ci];
+ for (i = 0; i < rgroup * (M + 2); i++) {
+ xbuf0[i] = xbuf1[i] = buf[i];
+ }
+ /* In the second list, put the last four row groups in swapped order */
+ for (i = 0; i < rgroup * 2; i++) {
+ xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
+ xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
+ }
+ /* The wraparound pointers at top and bottom will be filled later
+ * (see set_wraparound_pointers, below). Initially we want the "above"
+ * pointers to duplicate the first actual data line. This only needs
+ * to happen in xbuffer[0].
+ */
+ for (i = 0; i < rgroup; i++) {
+ xbuf0[i - rgroup] = xbuf0[0];
+ }
+ }
+}
+
+
+LOCAL(void)
+set_wraparound_pointers (j_decompress_ptr cinfo)
+/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
+ * This changes the pointer list state from top-of-image to the normal state.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf0, xbuf1;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ xbuf0 = main->xbuffer[0][ci];
+ xbuf1 = main->xbuffer[1][ci];
+ for (i = 0; i < rgroup; i++) {
+ xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
+ xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
+ xbuf0[rgroup*(M+2) + i] = xbuf0[i];
+ xbuf1[rgroup*(M+2) + i] = xbuf1[i];
+ }
+ }
+}
+
+
+LOCAL(void)
+set_bottom_pointers (j_decompress_ptr cinfo)
+/* Change the pointer lists to duplicate the last sample row at the bottom
+ * of the image. whichptr indicates which xbuffer holds the final iMCU row.
+ * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup, iMCUheight, rows_left;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Count sample rows in one iMCU row and in one row group */
+ iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
+ rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
+ /* Count nondummy sample rows remaining for this component */
+ rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
+ if (rows_left == 0) rows_left = iMCUheight;
+ /* Count nondummy row groups. Should get same answer for each component,
+ * so we need only do it once.
+ */
+ if (ci == 0) {
+ main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
+ }
+ /* Duplicate the last real sample row rgroup*2 times; this pads out the
+ * last partial rowgroup and ensures at least one full rowgroup of context.
+ */
+ xbuf = main->xbuffer[main->whichptr][ci];
+ for (i = 0; i < rgroup * 2; i++) {
+ xbuf[rows_left + i] = xbuf[rows_left-1];
+ }
+ }
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (cinfo->upsample->need_context_rows) {
+ main->pub.process_data = process_data_context_main;
+ make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
+ main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */
+ main->context_state = CTX_PREPARE_FOR_IMCU;
+ main->iMCU_row_ctr = 0;
+ } else {
+ /* Simple case with no context needed */
+ main->pub.process_data = process_data_simple_main;
+ }
+ main->buffer_full = FALSE; /* Mark buffer empty */
+ main->rowgroup_ctr = 0;
+ break;
+#ifdef QUANT_2PASS_SUPPORTED
+ case JBUF_CRANK_DEST:
+ /* For last pass of 2-pass quantization, just crank the postprocessor */
+ main->pub.process_data = process_data_crank_post;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This handles the simple case where no context is required.
+ */
+
+METHODDEF(void)
+process_data_simple_main (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ JDIMENSION rowgroups_avail;
+
+ /* Read input data if we haven't filled the main buffer yet */
+ if (! main->buffer_full) {
+ if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
+ return; /* suspension forced, can do nothing more */
+ main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
+ }
+
+ /* There are always min_DCT_scaled_size row groups in an iMCU row. */
+ rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
+ /* Note: at the bottom of the image, we may pass extra garbage row groups
+ * to the postprocessor. The postprocessor has to check for bottom
+ * of image anyway (at row resolution), so no point in us doing it too.
+ */
+
+ /* Feed the postprocessor */
+ (*cinfo->post->post_process_data) (cinfo, main->buffer,
+ &main->rowgroup_ctr, rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+
+ /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
+ if (main->rowgroup_ctr >= rowgroups_avail) {
+ main->buffer_full = FALSE;
+ main->rowgroup_ctr = 0;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This handles the case where context rows must be provided.
+ */
+
+METHODDEF(void)
+process_data_context_main (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ /* Read input data if we haven't filled the main buffer yet */
+ if (! main->buffer_full) {
+ if (! (*cinfo->coef->decompress_data) (cinfo,
+ main->xbuffer[main->whichptr]))
+ return; /* suspension forced, can do nothing more */
+ main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
+ main->iMCU_row_ctr++; /* count rows received */
+ }
+
+ /* Postprocessor typically will not swallow all the input data it is handed
+ * in one call (due to filling the output buffer first). Must be prepared
+ * to exit and restart. This switch lets us keep track of how far we got.
+ * Note that each case falls through to the next on successful completion.
+ */
+ switch (main->context_state) {
+ case CTX_POSTPONED_ROW:
+ /* Call postprocessor using previously set pointers for postponed row */
+ (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+ &main->rowgroup_ctr, main->rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+ if (main->rowgroup_ctr < main->rowgroups_avail)
+ return; /* Need to suspend */
+ main->context_state = CTX_PREPARE_FOR_IMCU;
+ if (*out_row_ctr >= out_rows_avail)
+ return; /* Postprocessor exactly filled output buf */
+ /*FALLTHROUGH*/
+ case CTX_PREPARE_FOR_IMCU:
+ /* Prepare to process first M-1 row groups of this iMCU row */
+ main->rowgroup_ctr = 0;
+ main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
+ /* Check for bottom of image: if so, tweak pointers to "duplicate"
+ * the last sample row, and adjust rowgroups_avail to ignore padding rows.
+ */
+ if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
+ set_bottom_pointers(cinfo);
+ main->context_state = CTX_PROCESS_IMCU;
+ /*FALLTHROUGH*/
+ case CTX_PROCESS_IMCU:
+ /* Call postprocessor using previously set pointers */
+ (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+ &main->rowgroup_ctr, main->rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+ if (main->rowgroup_ctr < main->rowgroups_avail)
+ return; /* Need to suspend */
+ /* After the first iMCU, change wraparound pointers to normal state */
+ if (main->iMCU_row_ctr == 1)
+ set_wraparound_pointers(cinfo);
+ /* Prepare to load new iMCU row using other xbuffer list */
+ main->whichptr ^= 1; /* 0=>1 or 1=>0 */
+ main->buffer_full = FALSE;
+ /* Still need to process last row group of this iMCU row, */
+ /* which is saved at index M+1 of the other xbuffer */
+ main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
+ main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
+ main->context_state = CTX_POSTPONED_ROW;
+ }
+}
+
+
+/*
+ * Process some data.
+ * Final pass of two-pass quantization: just call the postprocessor.
+ * Source data will be the postprocessor controller's internal buffer.
+ */
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+METHODDEF(void)
+process_data_crank_post (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
+ (JDIMENSION *) NULL, (JDIMENSION) 0,
+ output_buf, out_row_ctr, out_rows_avail);
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL(void)
+jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_main_ptr main;
+ int ci, rgroup, ngroups;
+ jpeg_component_info *compptr;
+
+ main = (my_main_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_main_controller));
+ cinfo->main = (struct jpeg_d_main_controller *) main;
+ main->pub.start_pass = start_pass_main;
+
+ if (need_full_buffer) /* shouldn't happen */
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ /* Allocate the workspace.
+ * ngroups is the number of row groups we need.
+ */
+ if (cinfo->upsample->need_context_rows) {
+ if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
+ ERREXIT(cinfo, JERR_NOTIMPL);
+ alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
+ ngroups = cinfo->min_DCT_scaled_size + 2;
+ } else {
+ ngroups = cinfo->min_DCT_scaled_size;
+ }
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ compptr->width_in_blocks * compptr->DCT_scaled_size,
+ (JDIMENSION) (rgroup * ngroups));
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdmarker.c b/test/monniaux/jpeg-6b/jdmarker.c
new file mode 100644
index 00000000..f4cca8cc
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdmarker.c
@@ -0,0 +1,1360 @@
+/*
+ * jdmarker.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to decode JPEG datastream markers.
+ * Most of the complexity arises from our desire to support input
+ * suspension: if not all of the data for a marker is available,
+ * we must exit back to the application. On resumption, we reprocess
+ * the marker.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum { /* JPEG marker codes */
+ M_SOF0 = 0xc0,
+ M_SOF1 = 0xc1,
+ M_SOF2 = 0xc2,
+ M_SOF3 = 0xc3,
+
+ M_SOF5 = 0xc5,
+ M_SOF6 = 0xc6,
+ M_SOF7 = 0xc7,
+
+ M_JPG = 0xc8,
+ M_SOF9 = 0xc9,
+ M_SOF10 = 0xca,
+ M_SOF11 = 0xcb,
+
+ M_SOF13 = 0xcd,
+ M_SOF14 = 0xce,
+ M_SOF15 = 0xcf,
+
+ M_DHT = 0xc4,
+
+ M_DAC = 0xcc,
+
+ M_RST0 = 0xd0,
+ M_RST1 = 0xd1,
+ M_RST2 = 0xd2,
+ M_RST3 = 0xd3,
+ M_RST4 = 0xd4,
+ M_RST5 = 0xd5,
+ M_RST6 = 0xd6,
+ M_RST7 = 0xd7,
+
+ M_SOI = 0xd8,
+ M_EOI = 0xd9,
+ M_SOS = 0xda,
+ M_DQT = 0xdb,
+ M_DNL = 0xdc,
+ M_DRI = 0xdd,
+ M_DHP = 0xde,
+ M_EXP = 0xdf,
+
+ M_APP0 = 0xe0,
+ M_APP1 = 0xe1,
+ M_APP2 = 0xe2,
+ M_APP3 = 0xe3,
+ M_APP4 = 0xe4,
+ M_APP5 = 0xe5,
+ M_APP6 = 0xe6,
+ M_APP7 = 0xe7,
+ M_APP8 = 0xe8,
+ M_APP9 = 0xe9,
+ M_APP10 = 0xea,
+ M_APP11 = 0xeb,
+ M_APP12 = 0xec,
+ M_APP13 = 0xed,
+ M_APP14 = 0xee,
+ M_APP15 = 0xef,
+
+ M_JPG0 = 0xf0,
+ M_JPG13 = 0xfd,
+ M_COM = 0xfe,
+
+ M_TEM = 0x01,
+
+ M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_marker_reader pub; /* public fields */
+
+ /* Application-overridable marker processing methods */
+ jpeg_marker_parser_method process_COM;
+ jpeg_marker_parser_method process_APPn[16];
+
+ /* Limit on marker data length to save for each marker type */
+ unsigned int length_limit_COM;
+ unsigned int length_limit_APPn[16];
+
+ /* Status of COM/APPn marker saving */
+ jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
+ unsigned int bytes_read; /* data bytes read so far in marker */
+ /* Note: cur_marker is not linked into marker_list until it's all read. */
+} my_marker_reader;
+
+typedef my_marker_reader * my_marker_ptr;
+
+
+/*
+ * Macros for fetching data from the data source module.
+ *
+ * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
+ * the current restart point; we update them only when we have reached a
+ * suitable place to restart if a suspension occurs.
+ */
+
+/* Declare and initialize local copies of input pointer/count */
+#define INPUT_VARS(cinfo) \
+ struct jpeg_source_mgr * datasrc = (cinfo)->src; \
+ const JOCTET * next_input_byte = datasrc->next_input_byte; \
+ size_t bytes_in_buffer = datasrc->bytes_in_buffer
+
+/* Unload the local copies --- do this only at a restart boundary */
+#define INPUT_SYNC(cinfo) \
+ ( datasrc->next_input_byte = next_input_byte, \
+ datasrc->bytes_in_buffer = bytes_in_buffer )
+
+/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
+#define INPUT_RELOAD(cinfo) \
+ ( next_input_byte = datasrc->next_input_byte, \
+ bytes_in_buffer = datasrc->bytes_in_buffer )
+
+/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
+ * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
+ * but we must reload the local copies after a successful fill.
+ */
+#define MAKE_BYTE_AVAIL(cinfo,action) \
+ if (bytes_in_buffer == 0) { \
+ if (! (*datasrc->fill_input_buffer) (cinfo)) \
+ { action; } \
+ INPUT_RELOAD(cinfo); \
+ }
+
+/* Read a byte into variable V.
+ * If must suspend, take the specified action (typically "return FALSE").
+ */
+#define INPUT_BYTE(cinfo,V,action) \
+ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V = GETJOCTET(*next_input_byte++); )
+
+/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
+ * V should be declared unsigned int or perhaps INT32.
+ */
+#define INPUT_2BYTES(cinfo,V,action) \
+ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
+ MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V += GETJOCTET(*next_input_byte++); )
+
+
+/*
+ * Routines to process JPEG markers.
+ *
+ * Entry condition: JPEG marker itself has been read and its code saved
+ * in cinfo->unread_marker; input restart point is just after the marker.
+ *
+ * Exit: if return TRUE, have read and processed any parameters, and have
+ * updated the restart point to point after the parameters.
+ * If return FALSE, was forced to suspend before reaching end of
+ * marker parameters; restart point has not been moved. Same routine
+ * will be called again after application supplies more input data.
+ *
+ * This approach to suspension assumes that all of a marker's parameters
+ * can fit into a single input bufferload. This should hold for "normal"
+ * markers. Some COM/APPn markers might have large parameter segments
+ * that might not fit. If we are simply dropping such a marker, we use
+ * skip_input_data to get past it, and thereby put the problem on the
+ * source manager's shoulders. If we are saving the marker's contents
+ * into memory, we use a slightly different convention: when forced to
+ * suspend, the marker processor updates the restart point to the end of
+ * what it's consumed (ie, the end of the buffer) before returning FALSE.
+ * On resumption, cinfo->unread_marker still contains the marker code,
+ * but the data source will point to the next chunk of marker data.
+ * The marker processor must retain internal state to deal with this.
+ *
+ * Note that we don't bother to avoid duplicate trace messages if a
+ * suspension occurs within marker parameters. Other side effects
+ * require more care.
+ */
+
+
+LOCAL(boolean)
+get_soi (j_decompress_ptr cinfo)
+/* Process an SOI marker */
+{
+ int i;
+
+ TRACEMS(cinfo, 1, JTRC_SOI);
+
+ if (cinfo->marker->saw_SOI)
+ ERREXIT(cinfo, JERR_SOI_DUPLICATE);
+
+ /* Reset all parameters that are defined to be reset by SOI */
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ cinfo->arith_dc_L[i] = 0;
+ cinfo->arith_dc_U[i] = 1;
+ cinfo->arith_ac_K[i] = 5;
+ }
+ cinfo->restart_interval = 0;
+
+ /* Set initial assumptions for colorspace etc */
+
+ cinfo->jpeg_color_space = JCS_UNKNOWN;
+ cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
+
+ cinfo->saw_JFIF_marker = FALSE;
+ cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
+ cinfo->JFIF_minor_version = 1;
+ cinfo->density_unit = 0;
+ cinfo->X_density = 1;
+ cinfo->Y_density = 1;
+ cinfo->saw_Adobe_marker = FALSE;
+ cinfo->Adobe_transform = 0;
+
+ cinfo->marker->saw_SOI = TRUE;
+
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
+/* Process a SOFn marker */
+{
+ INT32 length;
+ int c, ci;
+ jpeg_component_info * compptr;
+ INPUT_VARS(cinfo);
+
+ cinfo->progressive_mode = is_prog;
+ cinfo->arith_code = is_arith;
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
+ INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
+ INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
+ INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
+
+ length -= 8;
+
+ TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
+ (int) cinfo->image_width, (int) cinfo->image_height,
+ cinfo->num_components);
+
+ if (cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOF_DUPLICATE);
+
+ /* We don't support files in which the image height is initially specified */
+ /* as 0 and is later redefined by DNL. As long as we have to check that, */
+ /* might as well have a general sanity check. */
+ if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+ || cinfo->num_components <= 0)
+ ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+ if (length != (cinfo->num_components * 3))
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ if (cinfo->comp_info == NULL) /* do only once, even if suspend */
+ cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components * SIZEOF(jpeg_component_info));
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ compptr->component_index = ci;
+ INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
+ INPUT_BYTE(cinfo, c, return FALSE);
+ compptr->h_samp_factor = (c >> 4) & 15;
+ compptr->v_samp_factor = (c ) & 15;
+ INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
+
+ TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
+ compptr->component_id, compptr->h_samp_factor,
+ compptr->v_samp_factor, compptr->quant_tbl_no);
+ }
+
+ cinfo->marker->saw_SOF = TRUE;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_sos (j_decompress_ptr cinfo)
+/* Process a SOS marker */
+{
+ INT32 length;
+ int i, ci, n, c, cc;
+ jpeg_component_info * compptr;
+ INPUT_VARS(cinfo);
+
+ if (! cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOS_NO_SOF);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
+
+ TRACEMS1(cinfo, 1, JTRC_SOS, n);
+
+ if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ cinfo->comps_in_scan = n;
+
+ /* Collect the component-spec parameters */
+
+ for (i = 0; i < n; i++) {
+ INPUT_BYTE(cinfo, cc, return FALSE);
+ INPUT_BYTE(cinfo, c, return FALSE);
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (cc == compptr->component_id)
+ goto id_found;
+ }
+
+ ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
+
+ id_found:
+
+ cinfo->cur_comp_info[i] = compptr;
+ compptr->dc_tbl_no = (c >> 4) & 15;
+ compptr->ac_tbl_no = (c ) & 15;
+
+ TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
+ compptr->dc_tbl_no, compptr->ac_tbl_no);
+ }
+
+ /* Collect the additional scan parameters Ss, Se, Ah/Al. */
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Ss = c;
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Se = c;
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Ah = (c >> 4) & 15;
+ cinfo->Al = (c ) & 15;
+
+ TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
+ cinfo->Ah, cinfo->Al);
+
+ /* Prepare to scan data & restart markers */
+ cinfo->marker->next_restart_num = 0;
+
+ /* Count another SOS marker */
+ cinfo->input_scan_number++;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+#ifdef D_ARITH_CODING_SUPPORTED
+
+LOCAL(boolean)
+get_dac (j_decompress_ptr cinfo)
+/* Process a DAC marker */
+{
+ INT32 length;
+ int index, val;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 0) {
+ INPUT_BYTE(cinfo, index, return FALSE);
+ INPUT_BYTE(cinfo, val, return FALSE);
+
+ length -= 2;
+
+ TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
+
+ if (index < 0 || index >= (2*NUM_ARITH_TBLS))
+ ERREXIT1(cinfo, JERR_DAC_INDEX, index);
+
+ if (index >= NUM_ARITH_TBLS) { /* define AC table */
+ cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
+ } else { /* define DC table */
+ cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
+ cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
+ if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
+ ERREXIT1(cinfo, JERR_DAC_VALUE, val);
+ }
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+#else /* ! D_ARITH_CODING_SUPPORTED */
+
+#define get_dac(cinfo) skip_variable(cinfo)
+
+#endif /* D_ARITH_CODING_SUPPORTED */
+
+
+LOCAL(boolean)
+get_dht (j_decompress_ptr cinfo)
+/* Process a DHT marker */
+{
+ INT32 length;
+ UINT8 bits[17];
+ UINT8 huffval[256];
+ int i, index, count;
+ JHUFF_TBL **htblptr;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 16) {
+ INPUT_BYTE(cinfo, index, return FALSE);
+
+ TRACEMS1(cinfo, 1, JTRC_DHT, index);
+
+ bits[0] = 0;
+ count = 0;
+ for (i = 1; i <= 16; i++) {
+ INPUT_BYTE(cinfo, bits[i], return FALSE);
+ count += bits[i];
+ }
+
+ length -= 1 + 16;
+
+ TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+ bits[1], bits[2], bits[3], bits[4],
+ bits[5], bits[6], bits[7], bits[8]);
+ TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+ bits[9], bits[10], bits[11], bits[12],
+ bits[13], bits[14], bits[15], bits[16]);
+
+ /* Here we just do minimal validation of the counts to avoid walking
+ * off the end of our table space. jdhuff.c will check more carefully.
+ */
+ if (count > 256 || ((INT32) count) > length)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+
+ for (i = 0; i < count; i++)
+ INPUT_BYTE(cinfo, huffval[i], return FALSE);
+
+ length -= count;
+
+ if (index & 0x10) { /* AC table definition */
+ index -= 0x10;
+ htblptr = &cinfo->ac_huff_tbl_ptrs[index];
+ } else { /* DC table definition */
+ htblptr = &cinfo->dc_huff_tbl_ptrs[index];
+ }
+
+ if (index < 0 || index >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_DHT_INDEX, index);
+
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+
+ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+ MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_dqt (j_decompress_ptr cinfo)
+/* Process a DQT marker */
+{
+ INT32 length;
+ int n, i, prec;
+ unsigned int tmp;
+ JQUANT_TBL *quant_ptr;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 0) {
+ INPUT_BYTE(cinfo, n, return FALSE);
+ prec = n >> 4;
+ n &= 0x0F;
+
+ TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
+
+ if (n >= NUM_QUANT_TBLS)
+ ERREXIT1(cinfo, JERR_DQT_INDEX, n);
+
+ if (cinfo->quant_tbl_ptrs[n] == NULL)
+ cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+ quant_ptr = cinfo->quant_tbl_ptrs[n];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ if (prec)
+ INPUT_2BYTES(cinfo, tmp, return FALSE);
+ else
+ INPUT_BYTE(cinfo, tmp, return FALSE);
+ /* We convert the zigzag-order table to natural array order. */
+ quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
+ }
+
+ if (cinfo->err->trace_level >= 2) {
+ for (i = 0; i < DCTSIZE2; i += 8) {
+ TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
+ quant_ptr->quantval[i], quant_ptr->quantval[i+1],
+ quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
+ quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
+ quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
+ }
+ }
+
+ length -= DCTSIZE2+1;
+ if (prec) length -= DCTSIZE2;
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_dri (j_decompress_ptr cinfo)
+/* Process a DRI marker */
+{
+ INT32 length;
+ unsigned int tmp;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ if (length != 4)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_2BYTES(cinfo, tmp, return FALSE);
+
+ TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
+
+ cinfo->restart_interval = tmp;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Routines for processing APPn and COM markers.
+ * These are either saved in memory or discarded, per application request.
+ * APP0 and APP14 are specially checked to see if they are
+ * JFIF and Adobe markers, respectively.
+ */
+
+#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
+#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
+#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
+
+
+LOCAL(void)
+examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
+ unsigned int datalen, INT32 remaining)
+/* Examine first few bytes from an APP0.
+ * Take appropriate action if it is a JFIF marker.
+ * datalen is # of bytes at data[], remaining is length of rest of marker data.
+ */
+{
+ INT32 totallen = (INT32) datalen + remaining;
+
+ if (datalen >= APP0_DATA_LEN &&
+ GETJOCTET(data[0]) == 0x4A &&
+ GETJOCTET(data[1]) == 0x46 &&
+ GETJOCTET(data[2]) == 0x49 &&
+ GETJOCTET(data[3]) == 0x46 &&
+ GETJOCTET(data[4]) == 0) {
+ /* Found JFIF APP0 marker: save info */
+ cinfo->saw_JFIF_marker = TRUE;
+ cinfo->JFIF_major_version = GETJOCTET(data[5]);
+ cinfo->JFIF_minor_version = GETJOCTET(data[6]);
+ cinfo->density_unit = GETJOCTET(data[7]);
+ cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
+ cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
+ /* Check version.
+ * Major version must be 1, anything else signals an incompatible change.
+ * (We used to treat this as an error, but now it's a nonfatal warning,
+ * because some bozo at Hijaak couldn't read the spec.)
+ * Minor version should be 0..2, but process anyway if newer.
+ */
+ if (cinfo->JFIF_major_version != 1)
+ WARNMS2(cinfo, JWRN_JFIF_MAJOR,
+ cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
+ /* Generate trace messages */
+ TRACEMS5(cinfo, 1, JTRC_JFIF,
+ cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
+ cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
+ /* Validate thumbnail dimensions and issue appropriate messages */
+ if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
+ TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
+ GETJOCTET(data[12]), GETJOCTET(data[13]));
+ totallen -= APP0_DATA_LEN;
+ if (totallen !=
+ ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
+ TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
+ } else if (datalen >= 6 &&
+ GETJOCTET(data[0]) == 0x4A &&
+ GETJOCTET(data[1]) == 0x46 &&
+ GETJOCTET(data[2]) == 0x58 &&
+ GETJOCTET(data[3]) == 0x58 &&
+ GETJOCTET(data[4]) == 0) {
+ /* Found JFIF "JFXX" extension APP0 marker */
+ /* The library doesn't actually do anything with these,
+ * but we try to produce a helpful trace message.
+ */
+ switch (GETJOCTET(data[5])) {
+ case 0x10:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
+ break;
+ case 0x11:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
+ break;
+ case 0x13:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
+ break;
+ default:
+ TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
+ GETJOCTET(data[5]), (int) totallen);
+ break;
+ }
+ } else {
+ /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
+ TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
+ }
+}
+
+
+LOCAL(void)
+examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
+ unsigned int datalen, INT32 remaining)
+/* Examine first few bytes from an APP14.
+ * Take appropriate action if it is an Adobe marker.
+ * datalen is # of bytes at data[], remaining is length of rest of marker data.
+ */
+{
+ unsigned int version, flags0, flags1, transform;
+
+ if (datalen >= APP14_DATA_LEN &&
+ GETJOCTET(data[0]) == 0x41 &&
+ GETJOCTET(data[1]) == 0x64 &&
+ GETJOCTET(data[2]) == 0x6F &&
+ GETJOCTET(data[3]) == 0x62 &&
+ GETJOCTET(data[4]) == 0x65) {
+ /* Found Adobe APP14 marker */
+ version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
+ flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
+ flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
+ transform = GETJOCTET(data[11]);
+ TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
+ cinfo->saw_Adobe_marker = TRUE;
+ cinfo->Adobe_transform = (UINT8) transform;
+ } else {
+ /* Start of APP14 does not match "Adobe", or too short */
+ TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
+ }
+}
+
+
+METHODDEF(boolean)
+get_interesting_appn (j_decompress_ptr cinfo)
+/* Process an APP0 or APP14 marker without saving it */
+{
+ INT32 length;
+ JOCTET b[APPN_DATA_LEN];
+ unsigned int i, numtoread;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ /* get the interesting part of the marker data */
+ if (length >= APPN_DATA_LEN)
+ numtoread = APPN_DATA_LEN;
+ else if (length > 0)
+ numtoread = (unsigned int) length;
+ else
+ numtoread = 0;
+ for (i = 0; i < numtoread; i++)
+ INPUT_BYTE(cinfo, b[i], return FALSE);
+ length -= numtoread;
+
+ /* process it */
+ switch (cinfo->unread_marker) {
+ case M_APP0:
+ examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
+ break;
+ case M_APP14:
+ examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
+ break;
+ default:
+ /* can't get here unless jpeg_save_markers chooses wrong processor */
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
+ break;
+ }
+
+ /* skip any remaining data -- could be lots */
+ INPUT_SYNC(cinfo);
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+
+#ifdef SAVE_MARKERS_SUPPORTED
+
+METHODDEF(boolean)
+save_marker (j_decompress_ptr cinfo)
+/* Save an APPn or COM marker into the marker list */
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
+ unsigned int bytes_read, data_length;
+ JOCTET FAR * data;
+ INT32 length = 0;
+ INPUT_VARS(cinfo);
+
+ if (cur_marker == NULL) {
+ /* begin reading a marker */
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+ if (length >= 0) { /* watch out for bogus length word */
+ /* figure out how much we want to save */
+ unsigned int limit;
+ if (cinfo->unread_marker == (int) M_COM)
+ limit = marker->length_limit_COM;
+ else
+ limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
+ if ((unsigned int) length < limit)
+ limit = (unsigned int) length;
+ /* allocate and initialize the marker item */
+ cur_marker = (jpeg_saved_marker_ptr)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(struct jpeg_marker_struct) + limit);
+ cur_marker->next = NULL;
+ cur_marker->marker = (UINT8) cinfo->unread_marker;
+ cur_marker->original_length = (unsigned int) length;
+ cur_marker->data_length = limit;
+ /* data area is just beyond the jpeg_marker_struct */
+ data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
+ marker->cur_marker = cur_marker;
+ marker->bytes_read = 0;
+ bytes_read = 0;
+ data_length = limit;
+ } else {
+ /* deal with bogus length word */
+ bytes_read = data_length = 0;
+ data = NULL;
+ }
+ } else {
+ /* resume reading a marker */
+ bytes_read = marker->bytes_read;
+ data_length = cur_marker->data_length;
+ data = cur_marker->data + bytes_read;
+ }
+
+ while (bytes_read < data_length) {
+ INPUT_SYNC(cinfo); /* move the restart point to here */
+ marker->bytes_read = bytes_read;
+ /* If there's not at least one byte in buffer, suspend */
+ MAKE_BYTE_AVAIL(cinfo, return FALSE);
+ /* Copy bytes with reasonable rapidity */
+ while (bytes_read < data_length && bytes_in_buffer > 0) {
+ *data++ = *next_input_byte++;
+ bytes_in_buffer--;
+ bytes_read++;
+ }
+ }
+
+ /* Done reading what we want to read */
+ if (cur_marker != NULL) { /* will be NULL if bogus length word */
+ /* Add new marker to end of list */
+ if (cinfo->marker_list == NULL) {
+ cinfo->marker_list = cur_marker;
+ } else {
+ jpeg_saved_marker_ptr prev = cinfo->marker_list;
+ while (prev->next != NULL)
+ prev = prev->next;
+ prev->next = cur_marker;
+ }
+ /* Reset pointer & calc remaining data length */
+ data = cur_marker->data;
+ length = cur_marker->original_length - data_length;
+ }
+ /* Reset to initial state for next marker */
+ marker->cur_marker = NULL;
+
+ /* Process the marker if interesting; else just make a generic trace msg */
+ switch (cinfo->unread_marker) {
+ case M_APP0:
+ examine_app0(cinfo, data, data_length, length);
+ break;
+ case M_APP14:
+ examine_app14(cinfo, data, data_length, length);
+ break;
+ default:
+ TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
+ (int) (data_length + length));
+ break;
+ }
+
+ /* skip any remaining data -- could be lots */
+ INPUT_SYNC(cinfo); /* do before skip_input_data */
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+#endif /* SAVE_MARKERS_SUPPORTED */
+
+
+METHODDEF(boolean)
+skip_variable (j_decompress_ptr cinfo)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+ INT32 length;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
+
+ INPUT_SYNC(cinfo); /* do before skip_input_data */
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+
+/*
+ * Find the next JPEG marker, save it in cinfo->unread_marker.
+ * Returns FALSE if had to suspend before reaching a marker;
+ * in that case cinfo->unread_marker is unchanged.
+ *
+ * Note that the result might not be a valid marker code,
+ * but it will never be 0 or FF.
+ */
+
+LOCAL(boolean)
+next_marker (j_decompress_ptr cinfo)
+{
+ int c;
+ INPUT_VARS(cinfo);
+
+ for (;;) {
+ INPUT_BYTE(cinfo, c, return FALSE);
+ /* Skip any non-FF bytes.
+ * This may look a bit inefficient, but it will not occur in a valid file.
+ * We sync after each discarded byte so that a suspending data source
+ * can discard the byte from its buffer.
+ */
+ while (c != 0xFF) {
+ cinfo->marker->discarded_bytes++;
+ INPUT_SYNC(cinfo);
+ INPUT_BYTE(cinfo, c, return FALSE);
+ }
+ /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
+ * pad bytes, so don't count them in discarded_bytes. We assume there
+ * will not be so many consecutive FF bytes as to overflow a suspending
+ * data source's input buffer.
+ */
+ do {
+ INPUT_BYTE(cinfo, c, return FALSE);
+ } while (c == 0xFF);
+ if (c != 0)
+ break; /* found a valid marker, exit loop */
+ /* Reach here if we found a stuffed-zero data sequence (FF/00).
+ * Discard it and loop back to try again.
+ */
+ cinfo->marker->discarded_bytes += 2;
+ INPUT_SYNC(cinfo);
+ }
+
+ if (cinfo->marker->discarded_bytes != 0) {
+ WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
+ cinfo->marker->discarded_bytes = 0;
+ }
+
+ cinfo->unread_marker = c;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+first_marker (j_decompress_ptr cinfo)
+/* Like next_marker, but used to obtain the initial SOI marker. */
+/* For this marker, we do not allow preceding garbage or fill; otherwise,
+ * we might well scan an entire input file before realizing it ain't JPEG.
+ * If an application wants to process non-JFIF files, it must seek to the
+ * SOI before calling the JPEG library.
+ */
+{
+ int c, c2;
+ INPUT_VARS(cinfo);
+
+ INPUT_BYTE(cinfo, c, return FALSE);
+ INPUT_BYTE(cinfo, c2, return FALSE);
+ if (c != 0xFF || c2 != (int) M_SOI)
+ ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
+
+ cinfo->unread_marker = c2;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Read markers until SOS or EOI.
+ *
+ * Returns same codes as are defined for jpeg_consume_input:
+ * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ */
+
+METHODDEF(int)
+read_markers (j_decompress_ptr cinfo)
+{
+ /* Outer loop repeats once for each marker. */
+ for (;;) {
+ /* Collect the marker proper, unless we already did. */
+ /* NB: first_marker() enforces the requirement that SOI appear first. */
+ if (cinfo->unread_marker == 0) {
+ if (! cinfo->marker->saw_SOI) {
+ if (! first_marker(cinfo))
+ return JPEG_SUSPENDED;
+ } else {
+ if (! next_marker(cinfo))
+ return JPEG_SUSPENDED;
+ }
+ }
+ /* At this point cinfo->unread_marker contains the marker code and the
+ * input point is just past the marker proper, but before any parameters.
+ * A suspension will cause us to return with this state still true.
+ */
+ switch (cinfo->unread_marker) {
+ case M_SOI:
+ if (! get_soi(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF0: /* Baseline */
+ case M_SOF1: /* Extended sequential, Huffman */
+ if (! get_sof(cinfo, FALSE, FALSE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF2: /* Progressive, Huffman */
+ if (! get_sof(cinfo, TRUE, FALSE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF9: /* Extended sequential, arithmetic */
+ if (! get_sof(cinfo, FALSE, TRUE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF10: /* Progressive, arithmetic */
+ if (! get_sof(cinfo, TRUE, TRUE))
+ return JPEG_SUSPENDED;
+ break;
+
+ /* Currently unsupported SOFn types */
+ case M_SOF3: /* Lossless, Huffman */
+ case M_SOF5: /* Differential sequential, Huffman */
+ case M_SOF6: /* Differential progressive, Huffman */
+ case M_SOF7: /* Differential lossless, Huffman */
+ case M_JPG: /* Reserved for JPEG extensions */
+ case M_SOF11: /* Lossless, arithmetic */
+ case M_SOF13: /* Differential sequential, arithmetic */
+ case M_SOF14: /* Differential progressive, arithmetic */
+ case M_SOF15: /* Differential lossless, arithmetic */
+ ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
+ break;
+
+ case M_SOS:
+ if (! get_sos(cinfo))
+ return JPEG_SUSPENDED;
+ cinfo->unread_marker = 0; /* processed the marker */
+ return JPEG_REACHED_SOS;
+
+ case M_EOI:
+ TRACEMS(cinfo, 1, JTRC_EOI);
+ cinfo->unread_marker = 0; /* processed the marker */
+ return JPEG_REACHED_EOI;
+
+ case M_DAC:
+ if (! get_dac(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DHT:
+ if (! get_dht(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DQT:
+ if (! get_dqt(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DRI:
+ if (! get_dri(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_APP0:
+ case M_APP1:
+ case M_APP2:
+ case M_APP3:
+ case M_APP4:
+ case M_APP5:
+ case M_APP6:
+ case M_APP7:
+ case M_APP8:
+ case M_APP9:
+ case M_APP10:
+ case M_APP11:
+ case M_APP12:
+ case M_APP13:
+ case M_APP14:
+ case M_APP15:
+ if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
+ cinfo->unread_marker - (int) M_APP0]) (cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_COM:
+ if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_RST0: /* these are all parameterless */
+ case M_RST1:
+ case M_RST2:
+ case M_RST3:
+ case M_RST4:
+ case M_RST5:
+ case M_RST6:
+ case M_RST7:
+ case M_TEM:
+ TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
+ break;
+
+ case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
+ if (! skip_variable(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ default: /* must be DHP, EXP, JPGn, or RESn */
+ /* For now, we treat the reserved markers as fatal errors since they are
+ * likely to be used to signal incompatible JPEG Part 3 extensions.
+ * Once the JPEG 3 version-number marker is well defined, this code
+ * ought to change!
+ */
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
+ break;
+ }
+ /* Successfully processed marker, so reset state variable */
+ cinfo->unread_marker = 0;
+ } /* end loop */
+}
+
+
+/*
+ * Read a restart marker, which is expected to appear next in the datastream;
+ * if the marker is not there, take appropriate recovery action.
+ * Returns FALSE if suspension is required.
+ *
+ * This is called by the entropy decoder after it has read an appropriate
+ * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
+ * has already read a marker from the data source. Under normal conditions
+ * cinfo->unread_marker will be reset to 0 before returning; if not reset,
+ * it holds a marker which the decoder will be unable to read past.
+ */
+
+METHODDEF(boolean)
+read_restart_marker (j_decompress_ptr cinfo)
+{
+ /* Obtain a marker unless we already did. */
+ /* Note that next_marker will complain if it skips any data. */
+ if (cinfo->unread_marker == 0) {
+ if (! next_marker(cinfo))
+ return FALSE;
+ }
+
+ if (cinfo->unread_marker ==
+ ((int) M_RST0 + cinfo->marker->next_restart_num)) {
+ /* Normal case --- swallow the marker and let entropy decoder continue */
+ TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
+ cinfo->unread_marker = 0;
+ } else {
+ /* Uh-oh, the restart markers have been messed up. */
+ /* Let the data source manager determine how to resync. */
+ if (! (*cinfo->src->resync_to_restart) (cinfo,
+ cinfo->marker->next_restart_num))
+ return FALSE;
+ }
+
+ /* Update next-restart state */
+ cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
+
+ return TRUE;
+}
+
+
+/*
+ * This is the default resync_to_restart method for data source managers
+ * to use if they don't have any better approach. Some data source managers
+ * may be able to back up, or may have additional knowledge about the data
+ * which permits a more intelligent recovery strategy; such managers would
+ * presumably supply their own resync method.
+ *
+ * read_restart_marker calls resync_to_restart if it finds a marker other than
+ * the restart marker it was expecting. (This code is *not* used unless
+ * a nonzero restart interval has been declared.) cinfo->unread_marker is
+ * the marker code actually found (might be anything, except 0 or FF).
+ * The desired restart marker number (0..7) is passed as a parameter.
+ * This routine is supposed to apply whatever error recovery strategy seems
+ * appropriate in order to position the input stream to the next data segment.
+ * Note that cinfo->unread_marker is treated as a marker appearing before
+ * the current data-source input point; usually it should be reset to zero
+ * before returning.
+ * Returns FALSE if suspension is required.
+ *
+ * This implementation is substantially constrained by wanting to treat the
+ * input as a data stream; this means we can't back up. Therefore, we have
+ * only the following actions to work with:
+ * 1. Simply discard the marker and let the entropy decoder resume at next
+ * byte of file.
+ * 2. Read forward until we find another marker, discarding intervening
+ * data. (In theory we could look ahead within the current bufferload,
+ * without having to discard data if we don't find the desired marker.
+ * This idea is not implemented here, in part because it makes behavior
+ * dependent on buffer size and chance buffer-boundary positions.)
+ * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
+ * This will cause the entropy decoder to process an empty data segment,
+ * inserting dummy zeroes, and then we will reprocess the marker.
+ *
+ * #2 is appropriate if we think the desired marker lies ahead, while #3 is
+ * appropriate if the found marker is a future restart marker (indicating
+ * that we have missed the desired restart marker, probably because it got
+ * corrupted).
+ * We apply #2 or #3 if the found marker is a restart marker no more than
+ * two counts behind or ahead of the expected one. We also apply #2 if the
+ * found marker is not a legal JPEG marker code (it's certainly bogus data).
+ * If the found marker is a restart marker more than 2 counts away, we do #1
+ * (too much risk that the marker is erroneous; with luck we will be able to
+ * resync at some future point).
+ * For any valid non-restart JPEG marker, we apply #3. This keeps us from
+ * overrunning the end of a scan. An implementation limited to single-scan
+ * files might find it better to apply #2 for markers other than EOI, since
+ * any other marker would have to be bogus data in that case.
+ */
+
+GLOBAL(boolean)
+jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
+{
+ int marker = cinfo->unread_marker;
+ int action = 1;
+
+ /* Always put up a warning. */
+ WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
+
+ /* Outer loop handles repeated decision after scanning forward. */
+ for (;;) {
+ if (marker < (int) M_SOF0)
+ action = 2; /* invalid marker */
+ else if (marker < (int) M_RST0 || marker > (int) M_RST7)
+ action = 3; /* valid non-restart marker */
+ else {
+ if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
+ marker == ((int) M_RST0 + ((desired+2) & 7)))
+ action = 3; /* one of the next two expected restarts */
+ else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
+ marker == ((int) M_RST0 + ((desired-2) & 7)))
+ action = 2; /* a prior restart, so advance */
+ else
+ action = 1; /* desired restart or too far away */
+ }
+ TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
+ switch (action) {
+ case 1:
+ /* Discard marker and let entropy decoder resume processing. */
+ cinfo->unread_marker = 0;
+ return TRUE;
+ case 2:
+ /* Scan to the next marker, and repeat the decision loop. */
+ if (! next_marker(cinfo))
+ return FALSE;
+ marker = cinfo->unread_marker;
+ break;
+ case 3:
+ /* Return without advancing past this marker. */
+ /* Entropy decoder will be forced to process an empty segment. */
+ return TRUE;
+ }
+ } /* end loop */
+}
+
+
+/*
+ * Reset marker processing state to begin a fresh datastream.
+ */
+
+METHODDEF(void)
+reset_marker_reader (j_decompress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ cinfo->comp_info = NULL; /* until allocated by get_sof */
+ cinfo->input_scan_number = 0; /* no SOS seen yet */
+ cinfo->unread_marker = 0; /* no pending marker */
+ marker->pub.saw_SOI = FALSE; /* set internal state too */
+ marker->pub.saw_SOF = FALSE;
+ marker->pub.discarded_bytes = 0;
+ marker->cur_marker = NULL;
+}
+
+
+/*
+ * Initialize the marker reader module.
+ * This is called only once, when the decompression object is created.
+ */
+
+GLOBAL(void)
+jinit_marker_reader (j_decompress_ptr cinfo)
+{
+ my_marker_ptr marker;
+ int i;
+
+ /* Create subobject in permanent pool */
+ marker = (my_marker_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_marker_reader));
+ cinfo->marker = (struct jpeg_marker_reader *) marker;
+ /* Initialize public method pointers */
+ marker->pub.reset_marker_reader = reset_marker_reader;
+ marker->pub.read_markers = read_markers;
+ marker->pub.read_restart_marker = read_restart_marker;
+ /* Initialize COM/APPn processing.
+ * By default, we examine and then discard APP0 and APP14,
+ * but simply discard COM and all other APPn.
+ */
+ marker->process_COM = skip_variable;
+ marker->length_limit_COM = 0;
+ for (i = 0; i < 16; i++) {
+ marker->process_APPn[i] = skip_variable;
+ marker->length_limit_APPn[i] = 0;
+ }
+ marker->process_APPn[0] = get_interesting_appn;
+ marker->process_APPn[14] = get_interesting_appn;
+ /* Reset marker processing state */
+ reset_marker_reader(cinfo);
+}
+
+
+/*
+ * Control saving of COM and APPn markers into marker_list.
+ */
+
+#ifdef SAVE_MARKERS_SUPPORTED
+
+GLOBAL(void)
+jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
+ unsigned int length_limit)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ long maxlength;
+ jpeg_marker_parser_method processor;
+
+ /* Length limit mustn't be larger than what we can allocate
+ * (should only be a concern in a 16-bit environment).
+ */
+ maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
+ if (((long) length_limit) > maxlength)
+ length_limit = (unsigned int) maxlength;
+
+ /* Choose processor routine to use.
+ * APP0/APP14 have special requirements.
+ */
+ if (length_limit) {
+ processor = save_marker;
+ /* If saving APP0/APP14, save at least enough for our internal use. */
+ if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
+ length_limit = APP0_DATA_LEN;
+ else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
+ length_limit = APP14_DATA_LEN;
+ } else {
+ processor = skip_variable;
+ /* If discarding APP0/APP14, use our regular on-the-fly processor. */
+ if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
+ processor = get_interesting_appn;
+ }
+
+ if (marker_code == (int) M_COM) {
+ marker->process_COM = processor;
+ marker->length_limit_COM = length_limit;
+ } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
+ marker->process_APPn[marker_code - (int) M_APP0] = processor;
+ marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
+ } else
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+}
+
+#endif /* SAVE_MARKERS_SUPPORTED */
+
+
+/*
+ * Install a special processing method for COM or APPn markers.
+ */
+
+GLOBAL(void)
+jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
+ jpeg_marker_parser_method routine)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ if (marker_code == (int) M_COM)
+ marker->process_COM = routine;
+ else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
+ marker->process_APPn[marker_code - (int) M_APP0] = routine;
+ else
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+}
diff --git a/test/monniaux/jpeg-6b/jdmaster.c b/test/monniaux/jpeg-6b/jdmaster.c
new file mode 100644
index 00000000..2802c5b7
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdmaster.c
@@ -0,0 +1,557 @@
+/*
+ * jdmaster.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains master control logic for the JPEG decompressor.
+ * These routines are concerned with selecting the modules to be executed
+ * and with determining the number of passes and the work to be done in each
+ * pass.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_decomp_master pub; /* public fields */
+
+ int pass_number; /* # of passes completed */
+
+ boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
+
+ /* Saved references to initialized quantizer modules,
+ * in case we need to switch modes.
+ */
+ struct jpeg_color_quantizer * quantizer_1pass;
+ struct jpeg_color_quantizer * quantizer_2pass;
+} my_decomp_master;
+
+typedef my_decomp_master * my_master_ptr;
+
+
+/*
+ * Determine whether merged upsample/color conversion should be used.
+ * CRUCIAL: this must match the actual capabilities of jdmerge.c!
+ */
+
+LOCAL(boolean)
+use_merged_upsample (j_decompress_ptr cinfo)
+{
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+ /* Merging is the equivalent of plain box-filter upsampling */
+ if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
+ return FALSE;
+ /* jdmerge.c only supports YCC=>RGB color conversion */
+ if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
+ cinfo->out_color_space != JCS_RGB ||
+ cinfo->out_color_components != RGB_PIXELSIZE)
+ return FALSE;
+ /* and it only handles 2h1v or 2h2v sampling ratios */
+ if (cinfo->comp_info[0].h_samp_factor != 2 ||
+ cinfo->comp_info[1].h_samp_factor != 1 ||
+ cinfo->comp_info[2].h_samp_factor != 1 ||
+ cinfo->comp_info[0].v_samp_factor > 2 ||
+ cinfo->comp_info[1].v_samp_factor != 1 ||
+ cinfo->comp_info[2].v_samp_factor != 1)
+ return FALSE;
+ /* furthermore, it doesn't work if we've scaled the IDCTs differently */
+ if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+ cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+ cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
+ return FALSE;
+ /* ??? also need to test for upsample-time rescaling, when & if supported */
+ return TRUE; /* by golly, it'll work... */
+#else
+ return FALSE;
+#endif
+}
+
+
+/*
+ * Compute output image dimensions and related values.
+ * NOTE: this is exported for possible use by application.
+ * Hence it mustn't do anything that can't be done twice.
+ * Also note that it may be called before the master module is initialized!
+ */
+
+GLOBAL(void)
+jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
+{
+#ifdef IDCT_SCALING_SUPPORTED
+ int ci;
+ jpeg_component_info *compptr;
+#endif
+
+ /* Prevent application from calling me at wrong times */
+ if (cinfo->global_state != DSTATE_READY)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+#ifdef IDCT_SCALING_SUPPORTED
+
+ /* Compute actual output image dimensions and DCT scaling choices. */
+ if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
+ /* Provide 1/8 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 8L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 8L);
+ cinfo->min_DCT_scaled_size = 1;
+ } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
+ /* Provide 1/4 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 4L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 4L);
+ cinfo->min_DCT_scaled_size = 2;
+ } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
+ /* Provide 1/2 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 2L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 2L);
+ cinfo->min_DCT_scaled_size = 4;
+ } else {
+ /* Provide 1/1 scaling */
+ cinfo->output_width = cinfo->image_width;
+ cinfo->output_height = cinfo->image_height;
+ cinfo->min_DCT_scaled_size = DCTSIZE;
+ }
+ /* In selecting the actual DCT scaling for each component, we try to
+ * scale up the chroma components via IDCT scaling rather than upsampling.
+ * This saves time if the upsampler gets to use 1:1 scaling.
+ * Note this code assumes that the supported DCT scalings are powers of 2.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ int ssize = cinfo->min_DCT_scaled_size;
+ while (ssize < DCTSIZE &&
+ (compptr->h_samp_factor * ssize * 2 <=
+ cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
+ (compptr->v_samp_factor * ssize * 2 <=
+ cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
+ ssize = ssize * 2;
+ }
+ compptr->DCT_scaled_size = ssize;
+ }
+
+ /* Recompute downsampled dimensions of components;
+ * application needs to know these if using raw downsampled data.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Size in samples, after IDCT scaling */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width *
+ (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height *
+ (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ }
+
+#else /* !IDCT_SCALING_SUPPORTED */
+
+ /* Hardwire it to "no scaling" */
+ cinfo->output_width = cinfo->image_width;
+ cinfo->output_height = cinfo->image_height;
+ /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
+ * and has computed unscaled downsampled_width and downsampled_height.
+ */
+
+#endif /* IDCT_SCALING_SUPPORTED */
+
+ /* Report number of components in selected colorspace. */
+ /* Probably this should be in the color conversion module... */
+ switch (cinfo->out_color_space) {
+ case JCS_GRAYSCALE:
+ cinfo->out_color_components = 1;
+ break;
+ case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+ cinfo->out_color_components = RGB_PIXELSIZE;
+ break;
+#endif /* else share code with YCbCr */
+ case JCS_YCbCr:
+ cinfo->out_color_components = 3;
+ break;
+ case JCS_CMYK:
+ case JCS_YCCK:
+ cinfo->out_color_components = 4;
+ break;
+ default: /* else must be same colorspace as in file */
+ cinfo->out_color_components = cinfo->num_components;
+ break;
+ }
+ cinfo->output_components = (cinfo->quantize_colors ? 1 :
+ cinfo->out_color_components);
+
+ /* See if upsampler will want to emit more than one row at a time */
+ if (use_merged_upsample(cinfo))
+ cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
+ else
+ cinfo->rec_outbuf_height = 1;
+}
+
+
+/*
+ * Several decompression processes need to range-limit values to the range
+ * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
+ * due to noise introduced by quantization, roundoff error, etc. These
+ * processes are inner loops and need to be as fast as possible. On most
+ * machines, particularly CPUs with pipelines or instruction prefetch,
+ * a (subscript-check-less) C table lookup
+ * x = sample_range_limit[x];
+ * is faster than explicit tests
+ * if (x < 0) x = 0;
+ * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
+ * These processes all use a common table prepared by the routine below.
+ *
+ * For most steps we can mathematically guarantee that the initial value
+ * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
+ * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
+ * limiting step (just after the IDCT), a wildly out-of-range value is
+ * possible if the input data is corrupt. To avoid any chance of indexing
+ * off the end of memory and getting a bad-pointer trap, we perform the
+ * post-IDCT limiting thus:
+ * x = range_limit[x & MASK];
+ * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
+ * samples. Under normal circumstances this is more than enough range and
+ * a correct output will be generated; with bogus input data the mask will
+ * cause wraparound, and we will safely generate a bogus-but-in-range output.
+ * For the post-IDCT step, we want to convert the data from signed to unsigned
+ * representation by adding CENTERJSAMPLE at the same time that we limit it.
+ * So the post-IDCT limiting table ends up looking like this:
+ * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
+ * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ * 0,1,...,CENTERJSAMPLE-1
+ * Negative inputs select values from the upper half of the table after
+ * masking.
+ *
+ * We can save some space by overlapping the start of the post-IDCT table
+ * with the simpler range limiting table. The post-IDCT table begins at
+ * sample_range_limit + CENTERJSAMPLE.
+ *
+ * Note that the table is allocated in near data space on PCs; it's small
+ * enough and used often enough to justify this.
+ */
+
+LOCAL(void)
+prepare_range_limit_table (j_decompress_ptr cinfo)
+/* Allocate and fill in the sample_range_limit table */
+{
+ JSAMPLE * table;
+ int i;
+
+ table = (JSAMPLE *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+ table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
+ cinfo->sample_range_limit = table;
+ /* First segment of "simple" table: limit[x] = 0 for x < 0 */
+ MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
+ /* Main part of "simple" table: limit[x] = x */
+ for (i = 0; i <= MAXJSAMPLE; i++)
+ table[i] = (JSAMPLE) i;
+ table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
+ /* End of simple table, rest of first half of post-IDCT table */
+ for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
+ table[i] = MAXJSAMPLE;
+ /* Second half of post-IDCT table */
+ MEMZERO(table + (2 * (MAXJSAMPLE+1)),
+ (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+ MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
+ cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
+}
+
+
+/*
+ * Master selection of decompression modules.
+ * This is done once at jpeg_start_decompress time. We determine
+ * which modules will be used and give them appropriate initialization calls.
+ * We also initialize the decompressor input side to begin consuming data.
+ *
+ * Since jpeg_read_header has finished, we know what is in the SOF
+ * and (first) SOS markers. We also have all the application parameter
+ * settings.
+ */
+
+LOCAL(void)
+master_selection (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+ boolean use_c_buffer;
+ long samplesperrow;
+ JDIMENSION jd_samplesperrow;
+
+ /* Initialize dimensions and other stuff */
+ jpeg_calc_output_dimensions(cinfo);
+ prepare_range_limit_table(cinfo);
+
+ /* Width of an output scanline must be representable as JDIMENSION. */
+ samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
+ jd_samplesperrow = (JDIMENSION) samplesperrow;
+ if ((long) jd_samplesperrow != samplesperrow)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+ /* Initialize my private state */
+ master->pass_number = 0;
+ master->using_merged_upsample = use_merged_upsample(cinfo);
+
+ /* Color quantizer selection */
+ master->quantizer_1pass = NULL;
+ master->quantizer_2pass = NULL;
+ /* No mode changes if not using buffered-image mode. */
+ if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
+ cinfo->enable_1pass_quant = FALSE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+ }
+ if (cinfo->quantize_colors) {
+ if (cinfo->raw_data_out)
+ ERREXIT(cinfo, JERR_NOTIMPL);
+ /* 2-pass quantizer only works in 3-component color space. */
+ if (cinfo->out_color_components != 3) {
+ cinfo->enable_1pass_quant = TRUE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+ cinfo->colormap = NULL;
+ } else if (cinfo->colormap != NULL) {
+ cinfo->enable_external_quant = TRUE;
+ } else if (cinfo->two_pass_quantize) {
+ cinfo->enable_2pass_quant = TRUE;
+ } else {
+ cinfo->enable_1pass_quant = TRUE;
+ }
+
+ if (cinfo->enable_1pass_quant) {
+#ifdef QUANT_1PASS_SUPPORTED
+ jinit_1pass_quantizer(cinfo);
+ master->quantizer_1pass = cinfo->cquantize;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ }
+
+ /* We use the 2-pass code to map to external colormaps. */
+ if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
+#ifdef QUANT_2PASS_SUPPORTED
+ jinit_2pass_quantizer(cinfo);
+ master->quantizer_2pass = cinfo->cquantize;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ }
+ /* If both quantizers are initialized, the 2-pass one is left active;
+ * this is necessary for starting with quantization to an external map.
+ */
+ }
+
+ /* Post-processing: in particular, color conversion first */
+ if (! cinfo->raw_data_out) {
+ if (master->using_merged_upsample) {
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+ jinit_merged_upsampler(cinfo); /* does color conversion too */
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ jinit_color_deconverter(cinfo);
+ jinit_upsampler(cinfo);
+ }
+ jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
+ }
+ /* Inverse DCT */
+ jinit_inverse_dct(cinfo);
+ /* Entropy decoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef D_PROGRESSIVE_SUPPORTED
+ jinit_phuff_decoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_decoder(cinfo);
+ }
+
+ /* Initialize principal buffer controllers. */
+ use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
+ jinit_d_coef_controller(cinfo, use_c_buffer);
+
+ if (! cinfo->raw_data_out)
+ jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Initialize input side of decompressor to consume first scan. */
+ (*cinfo->inputctl->start_input_pass) (cinfo);
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* If jpeg_start_decompress will read the whole file, initialize
+ * progress monitoring appropriately. The input step is counted
+ * as one pass.
+ */
+ if (cinfo->progress != NULL && ! cinfo->buffered_image &&
+ cinfo->inputctl->has_multiple_scans) {
+ int nscans;
+ /* Estimate number of scans to set pass_limit. */
+ if (cinfo->progressive_mode) {
+ /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
+ nscans = 2 + 3 * cinfo->num_components;
+ } else {
+ /* For a nonprogressive multiscan file, estimate 1 scan per component. */
+ nscans = cinfo->num_components;
+ }
+ cinfo->progress->pass_counter = 0L;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
+ cinfo->progress->completed_passes = 0;
+ cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
+ /* Count the input pass as done */
+ master->pass_number++;
+ }
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each output pass. We determine which
+ * modules will be active during this pass and give them appropriate
+ * start_pass calls. We also set is_dummy_pass to indicate whether this
+ * is a "real" output pass or a dummy pass for color quantization.
+ * (In the latter case, jdapistd.c will crank the pass to completion.)
+ */
+
+METHODDEF(void)
+prepare_for_output_pass (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ if (master->pub.is_dummy_pass) {
+#ifdef QUANT_2PASS_SUPPORTED
+ /* Final pass of 2-pass quantization */
+ master->pub.is_dummy_pass = FALSE;
+ (*cinfo->cquantize->start_pass) (cinfo, FALSE);
+ (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
+ (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* QUANT_2PASS_SUPPORTED */
+ } else {
+ if (cinfo->quantize_colors && cinfo->colormap == NULL) {
+ /* Select new quantization method */
+ if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
+ cinfo->cquantize = master->quantizer_2pass;
+ master->pub.is_dummy_pass = TRUE;
+ } else if (cinfo->enable_1pass_quant) {
+ cinfo->cquantize = master->quantizer_1pass;
+ } else {
+ ERREXIT(cinfo, JERR_MODE_CHANGE);
+ }
+ }
+ (*cinfo->idct->start_pass) (cinfo);
+ (*cinfo->coef->start_output_pass) (cinfo);
+ if (! cinfo->raw_data_out) {
+ if (! master->using_merged_upsample)
+ (*cinfo->cconvert->start_pass) (cinfo);
+ (*cinfo->upsample->start_pass) (cinfo);
+ if (cinfo->quantize_colors)
+ (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
+ (*cinfo->post->start_pass) (cinfo,
+ (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+ (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+ }
+ }
+
+ /* Set up progress monitor's pass info if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->completed_passes = master->pass_number;
+ cinfo->progress->total_passes = master->pass_number +
+ (master->pub.is_dummy_pass ? 2 : 1);
+ /* In buffered-image mode, we assume one more output pass if EOI not
+ * yet reached, but no more passes if EOI has been reached.
+ */
+ if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
+ cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
+ }
+ }
+}
+
+
+/*
+ * Finish up at end of an output pass.
+ */
+
+METHODDEF(void)
+finish_output_pass (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ if (cinfo->quantize_colors)
+ (*cinfo->cquantize->finish_pass) (cinfo);
+ master->pass_number++;
+}
+
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Switch to a new external colormap between output passes.
+ */
+
+GLOBAL(void)
+jpeg_new_colormap (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ /* Prevent application from calling me at wrong times */
+ if (cinfo->global_state != DSTATE_BUFIMAGE)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (cinfo->quantize_colors && cinfo->enable_external_quant &&
+ cinfo->colormap != NULL) {
+ /* Select 2-pass quantizer for external colormap use */
+ cinfo->cquantize = master->quantizer_2pass;
+ /* Notify quantizer of colormap change */
+ (*cinfo->cquantize->new_color_map) (cinfo);
+ master->pub.is_dummy_pass = FALSE; /* just in case */
+ } else
+ ERREXIT(cinfo, JERR_MODE_CHANGE);
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+/*
+ * Initialize master decompression control and select active modules.
+ * This is performed at the start of jpeg_start_decompress.
+ */
+
+GLOBAL(void)
+jinit_master_decompress (j_decompress_ptr cinfo)
+{
+ my_master_ptr master;
+
+ master = (my_master_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_decomp_master));
+ cinfo->master = (struct jpeg_decomp_master *) master;
+ master->pub.prepare_for_output_pass = prepare_for_output_pass;
+ master->pub.finish_output_pass = finish_output_pass;
+
+ master->pub.is_dummy_pass = FALSE;
+
+ master_selection(cinfo);
+}
diff --git a/test/monniaux/jpeg-6b/jdmerge.c b/test/monniaux/jpeg-6b/jdmerge.c
new file mode 100644
index 00000000..37444468
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdmerge.c
@@ -0,0 +1,400 @@
+/*
+ * jdmerge.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains code for merged upsampling/color conversion.
+ *
+ * This file combines functions from jdsample.c and jdcolor.c;
+ * read those files first to understand what's going on.
+ *
+ * When the chroma components are to be upsampled by simple replication
+ * (ie, box filtering), we can save some work in color conversion by
+ * calculating all the output pixels corresponding to a pair of chroma
+ * samples at one time. In the conversion equations
+ * R = Y + K1 * Cr
+ * G = Y + K2 * Cb + K3 * Cr
+ * B = Y + K4 * Cb
+ * only the Y term varies among the group of pixels corresponding to a pair
+ * of chroma samples, so the rest of the terms can be calculated just once.
+ * At typical sampling ratios, this eliminates half or three-quarters of the
+ * multiplications needed for color conversion.
+ *
+ * This file currently provides implementations for the following cases:
+ * YCbCr => RGB color conversion only.
+ * Sampling ratios of 2h1v or 2h2v.
+ * No scaling needed at upsample time.
+ * Corner-aligned (non-CCIR601) sampling alignment.
+ * Other special cases could be added, but in most applications these are
+ * the only common cases. (For uncommon cases we fall back on the more
+ * general code in jdsample.c and jdcolor.c.)
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_upsampler pub; /* public fields */
+
+ /* Pointer to routine to do actual upsampling/conversion of one row group */
+ JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf));
+
+ /* Private state for YCC->RGB conversion */
+ int * Cr_r_tab; /* => table for Cr to R conversion */
+ int * Cb_b_tab; /* => table for Cb to B conversion */
+ INT32 * Cr_g_tab; /* => table for Cr to G conversion */
+ INT32 * Cb_g_tab; /* => table for Cb to G conversion */
+
+ /* For 2:1 vertical sampling, we produce two output rows at a time.
+ * We need a "spare" row buffer to hold the second output row if the
+ * application provides just a one-row buffer; we also use the spare
+ * to discard the dummy last row if the image height is odd.
+ */
+ JSAMPROW spare_row;
+ boolean spare_full; /* T if spare buffer is occupied */
+
+ JDIMENSION out_row_width; /* samples per output row */
+ JDIMENSION rows_to_go; /* counts rows remaining in image */
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+
+/*
+ * Initialize tables for YCC->RGB colorspace conversion.
+ * This is taken directly from jdcolor.c; see that file for more info.
+ */
+
+LOCAL(void)
+build_ycc_rgb_table (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ int i;
+ INT32 x;
+ SHIFT_TEMPS
+
+ upsample->Cr_r_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ upsample->Cb_b_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ upsample->Cr_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+ upsample->Cb_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+
+ for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+ /* Cr=>R value is nearest int to 1.40200 * x */
+ upsample->Cr_r_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
+ /* Cb=>B value is nearest int to 1.77200 * x */
+ upsample->Cb_b_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
+ /* Cr=>G value is scaled-up -0.71414 * x */
+ upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
+ /* Cb=>G value is scaled-up -0.34414 * x */
+ /* We also add in ONE_HALF so that need not do it in inner loop */
+ upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+ }
+}
+
+
+/*
+ * Initialize for an upsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_merged_upsample (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Mark the spare buffer empty */
+ upsample->spare_full = FALSE;
+ /* Initialize total-height counter for detecting bottom of image */
+ upsample->rows_to_go = cinfo->output_height;
+}
+
+
+/*
+ * Control routine to do upsampling (and color conversion).
+ *
+ * The control routine just handles the row buffering considerations.
+ */
+
+METHODDEF(void)
+merged_2v_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+/* 2:1 vertical sampling case: may need a spare row. */
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ JSAMPROW work_ptrs[2];
+ JDIMENSION num_rows; /* number of rows returned to caller */
+
+ if (upsample->spare_full) {
+ /* If we have a spare row saved from a previous cycle, just return it. */
+ jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
+ 1, upsample->out_row_width);
+ num_rows = 1;
+ upsample->spare_full = FALSE;
+ } else {
+ /* Figure number of rows to return to caller. */
+ num_rows = 2;
+ /* Not more than the distance to the end of the image. */
+ if (num_rows > upsample->rows_to_go)
+ num_rows = upsample->rows_to_go;
+ /* And not more than what the client can accept: */
+ out_rows_avail -= *out_row_ctr;
+ if (num_rows > out_rows_avail)
+ num_rows = out_rows_avail;
+ /* Create output pointer array for upsampler. */
+ work_ptrs[0] = output_buf[*out_row_ctr];
+ if (num_rows > 1) {
+ work_ptrs[1] = output_buf[*out_row_ctr + 1];
+ } else {
+ work_ptrs[1] = upsample->spare_row;
+ upsample->spare_full = TRUE;
+ }
+ /* Now do the upsampling. */
+ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
+ }
+
+ /* Adjust counts */
+ *out_row_ctr += num_rows;
+ upsample->rows_to_go -= num_rows;
+ /* When the buffer is emptied, declare this input row group consumed */
+ if (! upsample->spare_full)
+ (*in_row_group_ctr)++;
+}
+
+
+METHODDEF(void)
+merged_1v_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+/* 1:1 vertical sampling case: much easier, never need a spare row. */
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Just do the upsampling. */
+ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
+ output_buf + *out_row_ctr);
+ /* Adjust counts */
+ (*out_row_ctr)++;
+ (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by the control routines to do
+ * the actual upsampling/conversion. One row group is processed per call.
+ *
+ * Note: since we may be writing directly into application-supplied buffers,
+ * we have to be honest about the output width; we can't assume the buffer
+ * has been rounded up to an even width.
+ */
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
+ */
+
+METHODDEF(void)
+h2v1_merged_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ register int y, cred, cgreen, cblue;
+ int cb, cr;
+ register JSAMPROW outptr;
+ JSAMPROW inptr0, inptr1, inptr2;
+ JDIMENSION col;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ int * Crrtab = upsample->Cr_r_tab;
+ int * Cbbtab = upsample->Cb_b_tab;
+ INT32 * Crgtab = upsample->Cr_g_tab;
+ INT32 * Cbgtab = upsample->Cb_g_tab;
+ SHIFT_TEMPS
+
+ inptr0 = input_buf[0][in_row_group_ctr];
+ inptr1 = input_buf[1][in_row_group_ctr];
+ inptr2 = input_buf[2][in_row_group_ctr];
+ outptr = output_buf[0];
+ /* Loop for each pair of output pixels */
+ for (col = cinfo->output_width >> 1; col > 0; col--) {
+ /* Do the chroma part of the calculation */
+ cb = GETJSAMPLE(*inptr1++);
+ cr = GETJSAMPLE(*inptr2++);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ /* Fetch 2 Y values and emit 2 pixels */
+ y = GETJSAMPLE(*inptr0++);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ outptr += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr0++);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ outptr += RGB_PIXELSIZE;
+ }
+ /* If image width is odd, do the last output column separately */
+ if (cinfo->output_width & 1) {
+ cb = GETJSAMPLE(*inptr1);
+ cr = GETJSAMPLE(*inptr2);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ y = GETJSAMPLE(*inptr0);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ }
+}
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
+ */
+
+METHODDEF(void)
+h2v2_merged_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ register int y, cred, cgreen, cblue;
+ int cb, cr;
+ register JSAMPROW outptr0, outptr1;
+ JSAMPROW inptr00, inptr01, inptr1, inptr2;
+ JDIMENSION col;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ int * Crrtab = upsample->Cr_r_tab;
+ int * Cbbtab = upsample->Cb_b_tab;
+ INT32 * Crgtab = upsample->Cr_g_tab;
+ INT32 * Cbgtab = upsample->Cb_g_tab;
+ SHIFT_TEMPS
+
+ inptr00 = input_buf[0][in_row_group_ctr*2];
+ inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
+ inptr1 = input_buf[1][in_row_group_ctr];
+ inptr2 = input_buf[2][in_row_group_ctr];
+ outptr0 = output_buf[0];
+ outptr1 = output_buf[1];
+ /* Loop for each group of output pixels */
+ for (col = cinfo->output_width >> 1; col > 0; col--) {
+ /* Do the chroma part of the calculation */
+ cb = GETJSAMPLE(*inptr1++);
+ cr = GETJSAMPLE(*inptr2++);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ /* Fetch 4 Y values and emit 4 pixels */
+ y = GETJSAMPLE(*inptr00++);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ outptr0 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr00++);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ outptr0 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr01++);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ outptr1 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr01++);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ outptr1 += RGB_PIXELSIZE;
+ }
+ /* If image width is odd, do the last output column separately */
+ if (cinfo->output_width & 1) {
+ cb = GETJSAMPLE(*inptr1);
+ cr = GETJSAMPLE(*inptr2);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ y = GETJSAMPLE(*inptr00);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ y = GETJSAMPLE(*inptr01);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ }
+}
+
+
+/*
+ * Module initialization routine for merged upsampling/color conversion.
+ *
+ * NB: this is called under the conditions determined by use_merged_upsample()
+ * in jdmaster.c. That routine MUST correspond to the actual capabilities
+ * of this module; no safety checks are made here.
+ */
+
+GLOBAL(void)
+jinit_merged_upsampler (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample;
+
+ upsample = (my_upsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_upsampler));
+ cinfo->upsample = (struct jpeg_upsampler *) upsample;
+ upsample->pub.start_pass = start_pass_merged_upsample;
+ upsample->pub.need_context_rows = FALSE;
+
+ upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
+
+ if (cinfo->max_v_samp_factor == 2) {
+ upsample->pub.upsample = merged_2v_upsample;
+ upsample->upmethod = h2v2_merged_upsample;
+ /* Allocate a spare row buffer */
+ upsample->spare_row = (JSAMPROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
+ } else {
+ upsample->pub.upsample = merged_1v_upsample;
+ upsample->upmethod = h2v1_merged_upsample;
+ /* No spare row needed */
+ upsample->spare_row = NULL;
+ }
+
+ build_ycc_rgb_table(cinfo);
+}
+
+#endif /* UPSAMPLE_MERGING_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jdosabcc.obj b/test/monniaux/jpeg-6b/jdosabcc.obj
new file mode 100644
index 00000000..eb60d861
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdosabcc.obj
Binary files differ
diff --git a/test/monniaux/jpeg-6b/jdosamsc.obj b/test/monniaux/jpeg-6b/jdosamsc.obj
new file mode 100644
index 00000000..9b2905a3
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdosamsc.obj
Binary files differ
diff --git a/test/monniaux/jpeg-6b/jdosaobj.doc b/test/monniaux/jpeg-6b/jdosaobj.doc
new file mode 100644
index 00000000..4318362e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdosaobj.doc
@@ -0,0 +1,16 @@
+This archive contains already-assembled object files for JMEMDOSA.ASM
+of the Independent JPEG Group's JPEG package. These files will be helpful
+if you want to compile the IJG code for DOS, but don't have an assembler.
+
+These files were prepared from the 3/13/1992 version of JMEMDOSA.ASM,
+which is still unchanged as of mid-1998. You can use these files with
+releases 3 through 6 of the IJG code, and probably future releases too.
+
+To use these files, copy the proper version to JMEMDOSA.OBJ. Make sure
+this file has a newer date than JMEMDOSA.ASM. Then compile the code as
+usual.
+
+Object files included:
+
+JDOSAMSC.OBJ For Microsoft C version 5 or later.
+JDOSABCC.OBJ For Borland C version 3.0 or later.
diff --git a/test/monniaux/jpeg-6b/jdphuff.c b/test/monniaux/jpeg-6b/jdphuff.c
new file mode 100644
index 00000000..22678099
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdphuff.c
@@ -0,0 +1,668 @@
+/*
+ * jdphuff.c
+ *
+ * Copyright (C) 1995-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains Huffman entropy decoding routines for progressive JPEG.
+ *
+ * Much of the complexity here has to do with supporting input suspension.
+ * If the data source module demands suspension, we want to be able to back
+ * up to the start of the current MCU. To do this, we copy state variables
+ * into local working storage, and update them back to the permanent
+ * storage only upon successful completion of an MCU.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdhuff.h" /* Declarations shared with jdhuff.c */
+
+
+#ifdef D_PROGRESSIVE_SUPPORTED
+
+/*
+ * Expanded entropy decoder object for progressive Huffman decoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+ unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+/* This macro is to work around compilers with missing or broken
+ * structure assignment. You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src) ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src) \
+ ((dest).EOBRUN = (src).EOBRUN, \
+ (dest).last_dc_val[0] = (src).last_dc_val[0], \
+ (dest).last_dc_val[1] = (src).last_dc_val[1], \
+ (dest).last_dc_val[2] = (src).last_dc_val[2], \
+ (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+ struct jpeg_entropy_decoder pub; /* public fields */
+
+ /* These fields are loaded into local variables at start of each MCU.
+ * In case of suspension, we exit WITHOUT updating them.
+ */
+ bitread_perm_state bitstate; /* Bit buffer at start of MCU */
+ savable_state saved; /* Other state at start of MCU */
+
+ /* These fields are NOT loaded into local working state. */
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
+
+ /* Pointers to derived tables (these workspaces have image lifespan) */
+ d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
+
+ d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
+} phuff_entropy_decoder;
+
+typedef phuff_entropy_decoder * phuff_entropy_ptr;
+
+/* Forward declarations */
+METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ */
+
+METHODDEF(void)
+start_pass_phuff_decoder (j_decompress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ boolean is_DC_band, bad;
+ int ci, coefi, tbl;
+ int *coef_bit_ptr;
+ jpeg_component_info * compptr;
+
+ is_DC_band = (cinfo->Ss == 0);
+
+ /* Validate scan parameters */
+ bad = FALSE;
+ if (is_DC_band) {
+ if (cinfo->Se != 0)
+ bad = TRUE;
+ } else {
+ /* need not check Ss/Se < 0 since they came from unsigned bytes */
+ if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
+ bad = TRUE;
+ /* AC scans may have only one component */
+ if (cinfo->comps_in_scan != 1)
+ bad = TRUE;
+ }
+ if (cinfo->Ah != 0) {
+ /* Successive approximation refinement scan: must have Al = Ah-1. */
+ if (cinfo->Al != cinfo->Ah-1)
+ bad = TRUE;
+ }
+ if (cinfo->Al > 13) /* need not check for < 0 */
+ bad = TRUE;
+ /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
+ * but the spec doesn't say so, and we try to be liberal about what we
+ * accept. Note: large Al values could result in out-of-range DC
+ * coefficients during early scans, leading to bizarre displays due to
+ * overflows in the IDCT math. But we won't crash.
+ */
+ if (bad)
+ ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
+ cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
+ /* Update progression status, and verify that scan order is legal.
+ * Note that inter-scan inconsistencies are treated as warnings
+ * not fatal errors ... not clear if this is right way to behave.
+ */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ int cindex = cinfo->cur_comp_info[ci]->component_index;
+ coef_bit_ptr = & cinfo->coef_bits[cindex][0];
+ if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
+ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
+ for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
+ int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
+ if (cinfo->Ah != expected)
+ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
+ coef_bit_ptr[coefi] = cinfo->Al;
+ }
+ }
+
+ /* Select MCU decoding routine */
+ if (cinfo->Ah == 0) {
+ if (is_DC_band)
+ entropy->pub.decode_mcu = decode_mcu_DC_first;
+ else
+ entropy->pub.decode_mcu = decode_mcu_AC_first;
+ } else {
+ if (is_DC_band)
+ entropy->pub.decode_mcu = decode_mcu_DC_refine;
+ else
+ entropy->pub.decode_mcu = decode_mcu_AC_refine;
+ }
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Make sure requested tables are present, and compute derived tables.
+ * We may build same derived table more than once, but it's not expensive.
+ */
+ if (is_DC_band) {
+ if (cinfo->Ah == 0) { /* DC refinement needs no table */
+ tbl = compptr->dc_tbl_no;
+ jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
+ & entropy->derived_tbls[tbl]);
+ }
+ } else {
+ tbl = compptr->ac_tbl_no;
+ jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
+ & entropy->derived_tbls[tbl]);
+ /* remember the single active table */
+ entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
+ }
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Initialize bitread state variables */
+ entropy->bitstate.bits_left = 0;
+ entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
+ entropy->pub.insufficient_data = FALSE;
+
+ /* Initialize private state variables */
+ entropy->saved.EOBRUN = 0;
+
+ /* Initialize restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+}
+
+
+/*
+ * Figure F.12: extend sign bit.
+ * On some machines, a shift and add will be faster than a table lookup.
+ */
+
+#ifdef AVOID_TABLES
+
+#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
+
+#else
+
+#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
+
+static const int extend_test[16] = /* entry n is 2**(n-1) */
+ { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
+ 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
+
+static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
+ { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
+ ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
+ ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
+ ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
+
+#endif /* AVOID_TABLES */
+
+
+/*
+ * Check for a restart marker & resynchronize decoder.
+ * Returns FALSE if must suspend.
+ */
+
+LOCAL(boolean)
+process_restart (j_decompress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ int ci;
+
+ /* Throw away any unused bits remaining in bit buffer; */
+ /* include any full bytes in next_marker's count of discarded bytes */
+ cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
+ entropy->bitstate.bits_left = 0;
+
+ /* Advance past the RSTn marker */
+ if (! (*cinfo->marker->read_restart_marker) (cinfo))
+ return FALSE;
+
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++)
+ entropy->saved.last_dc_val[ci] = 0;
+ /* Re-init EOB run count, too */
+ entropy->saved.EOBRUN = 0;
+
+ /* Reset restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+
+ /* Reset out-of-data flag, unless read_restart_marker left us smack up
+ * against a marker. In that case we will end up treating the next data
+ * segment as empty, and we can avoid producing bogus output pixels by
+ * leaving the flag set.
+ */
+ if (cinfo->unread_marker == 0)
+ entropy->pub.insufficient_data = FALSE;
+
+ return TRUE;
+}
+
+
+/*
+ * Huffman MCU decoding.
+ * Each of these routines decodes and returns one MCU's worth of
+ * Huffman-compressed coefficients.
+ * The coefficients are reordered from zigzag order into natural array order,
+ * but are not dequantized.
+ *
+ * The i'th block of the MCU is stored into the block pointed to by
+ * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
+ *
+ * We return FALSE if data source requested suspension. In that case no
+ * changes have been made to permanent state. (Exception: some output
+ * coefficients may already have been assigned. This is harmless for
+ * spectral selection, since we'll just re-assign them on the next call.
+ * Successive approximation AC refinement has to be more careful, however.)
+ */
+
+/*
+ * MCU decoding for DC initial scan (either spectral selection,
+ * or first pass of successive approximation).
+ */
+
+METHODDEF(boolean)
+decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ int Al = cinfo->Al;
+ register int s, r;
+ int blkn, ci;
+ JBLOCKROW block;
+ BITREAD_STATE_VARS;
+ savable_state state;
+ d_derived_tbl * tbl;
+ jpeg_component_info * compptr;
+
+ /* Process restart marker if needed; may have to suspend */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
+ }
+
+ /* If we've run out of data, just leave the MCU set to zeroes.
+ * This way, we return uniform gray for the remainder of the segment.
+ */
+ if (! entropy->pub.insufficient_data) {
+
+ /* Load up working state */
+ BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ ASSIGN_STATE(state, entropy->saved);
+
+ /* Outer loop handles each block in the MCU */
+
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ block = MCU_data[blkn];
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ tbl = entropy->derived_tbls[compptr->dc_tbl_no];
+
+ /* Decode a single block's worth of coefficients */
+
+ /* Section F.2.2.1: decode the DC coefficient difference */
+ HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
+ if (s) {
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ r = GET_BITS(s);
+ s = HUFF_EXTEND(r, s);
+ }
+
+ /* Convert DC difference to actual value, update last_dc_val */
+ s += state.last_dc_val[ci];
+ state.last_dc_val[ci] = s;
+ /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
+ (*block)[0] = (JCOEF) (s << Al);
+ }
+
+ /* Completed MCU, so update state */
+ BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ ASSIGN_STATE(entropy->saved, state);
+ }
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
+}
+
+
+/*
+ * MCU decoding for AC initial scan (either spectral selection,
+ * or first pass of successive approximation).
+ */
+
+METHODDEF(boolean)
+decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ int Se = cinfo->Se;
+ int Al = cinfo->Al;
+ register int s, k, r;
+ unsigned int EOBRUN;
+ JBLOCKROW block;
+ BITREAD_STATE_VARS;
+ d_derived_tbl * tbl;
+
+ /* Process restart marker if needed; may have to suspend */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
+ }
+
+ /* If we've run out of data, just leave the MCU set to zeroes.
+ * This way, we return uniform gray for the remainder of the segment.
+ */
+ if (! entropy->pub.insufficient_data) {
+
+ /* Load up working state.
+ * We can avoid loading/saving bitread state if in an EOB run.
+ */
+ EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
+
+ /* There is always only one block per MCU */
+
+ if (EOBRUN > 0) /* if it's a band of zeroes... */
+ EOBRUN--; /* ...process it now (we do nothing) */
+ else {
+ BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ block = MCU_data[0];
+ tbl = entropy->ac_derived_tbl;
+
+ for (k = cinfo->Ss; k <= Se; k++) {
+ HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
+ r = s >> 4;
+ s &= 15;
+ if (s) {
+ k += r;
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ r = GET_BITS(s);
+ s = HUFF_EXTEND(r, s);
+ /* Scale and output coefficient in natural (dezigzagged) order */
+ (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
+ } else {
+ if (r == 15) { /* ZRL */
+ k += 15; /* skip 15 zeroes in band */
+ } else { /* EOBr, run length is 2^r + appended bits */
+ EOBRUN = 1 << r;
+ if (r) { /* EOBr, r > 0 */
+ CHECK_BIT_BUFFER(br_state, r, return FALSE);
+ r = GET_BITS(r);
+ EOBRUN += r;
+ }
+ EOBRUN--; /* this band is processed at this moment */
+ break; /* force end-of-band */
+ }
+ }
+ }
+
+ BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ }
+
+ /* Completed MCU, so update state */
+ entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
+ }
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
+}
+
+
+/*
+ * MCU decoding for DC successive approximation refinement scan.
+ * Note: we assume such scans can be multi-component, although the spec
+ * is not very clear on the point.
+ */
+
+METHODDEF(boolean)
+decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
+ int blkn;
+ JBLOCKROW block;
+ BITREAD_STATE_VARS;
+
+ /* Process restart marker if needed; may have to suspend */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
+ }
+
+ /* Not worth the cycles to check insufficient_data here,
+ * since we will not change the data anyway if we read zeroes.
+ */
+
+ /* Load up working state */
+ BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+
+ /* Outer loop handles each block in the MCU */
+
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ block = MCU_data[blkn];
+
+ /* Encoded data is simply the next bit of the two's-complement DC value */
+ CHECK_BIT_BUFFER(br_state, 1, return FALSE);
+ if (GET_BITS(1))
+ (*block)[0] |= p1;
+ /* Note: since we use |=, repeating the assignment later is safe */
+ }
+
+ /* Completed MCU, so update state */
+ BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
+}
+
+
+/*
+ * MCU decoding for AC successive approximation refinement scan.
+ */
+
+METHODDEF(boolean)
+decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
+ int Se = cinfo->Se;
+ int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
+ int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
+ register int s, k, r;
+ unsigned int EOBRUN;
+ JBLOCKROW block;
+ JCOEFPTR thiscoef;
+ BITREAD_STATE_VARS;
+ d_derived_tbl * tbl;
+ int num_newnz;
+ int newnz_pos[DCTSIZE2];
+
+ /* Process restart marker if needed; may have to suspend */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
+ }
+
+ /* If we've run out of data, don't modify the MCU.
+ */
+ if (! entropy->pub.insufficient_data) {
+
+ /* Load up working state */
+ BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
+
+ /* There is always only one block per MCU */
+ block = MCU_data[0];
+ tbl = entropy->ac_derived_tbl;
+
+ /* If we are forced to suspend, we must undo the assignments to any newly
+ * nonzero coefficients in the block, because otherwise we'd get confused
+ * next time about which coefficients were already nonzero.
+ * But we need not undo addition of bits to already-nonzero coefficients;
+ * instead, we can test the current bit to see if we already did it.
+ */
+ num_newnz = 0;
+
+ /* initialize coefficient loop counter to start of band */
+ k = cinfo->Ss;
+
+ if (EOBRUN == 0) {
+ for (; k <= Se; k++) {
+ HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
+ r = s >> 4;
+ s &= 15;
+ if (s) {
+ if (s != 1) /* size of new coef should always be 1 */
+ WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
+ CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+ if (GET_BITS(1))
+ s = p1; /* newly nonzero coef is positive */
+ else
+ s = m1; /* newly nonzero coef is negative */
+ } else {
+ if (r != 15) {
+ EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
+ if (r) {
+ CHECK_BIT_BUFFER(br_state, r, goto undoit);
+ r = GET_BITS(r);
+ EOBRUN += r;
+ }
+ break; /* rest of block is handled by EOB logic */
+ }
+ /* note s = 0 for processing ZRL */
+ }
+ /* Advance over already-nonzero coefs and r still-zero coefs,
+ * appending correction bits to the nonzeroes. A correction bit is 1
+ * if the absolute value of the coefficient must be increased.
+ */
+ do {
+ thiscoef = *block + jpeg_natural_order[k];
+ if (*thiscoef != 0) {
+ CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+ if (GET_BITS(1)) {
+ if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
+ if (*thiscoef >= 0)
+ *thiscoef += p1;
+ else
+ *thiscoef += m1;
+ }
+ }
+ } else {
+ if (--r < 0)
+ break; /* reached target zero coefficient */
+ }
+ k++;
+ } while (k <= Se);
+ if (s) {
+ int pos = jpeg_natural_order[k];
+ /* Output newly nonzero coefficient */
+ (*block)[pos] = (JCOEF) s;
+ /* Remember its position in case we have to suspend */
+ newnz_pos[num_newnz++] = pos;
+ }
+ }
+ }
+
+ if (EOBRUN > 0) {
+ /* Scan any remaining coefficient positions after the end-of-band
+ * (the last newly nonzero coefficient, if any). Append a correction
+ * bit to each already-nonzero coefficient. A correction bit is 1
+ * if the absolute value of the coefficient must be increased.
+ */
+ for (; k <= Se; k++) {
+ thiscoef = *block + jpeg_natural_order[k];
+ if (*thiscoef != 0) {
+ CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+ if (GET_BITS(1)) {
+ if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
+ if (*thiscoef >= 0)
+ *thiscoef += p1;
+ else
+ *thiscoef += m1;
+ }
+ }
+ }
+ }
+ /* Count one block completed in EOB run */
+ EOBRUN--;
+ }
+
+ /* Completed MCU, so update state */
+ BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
+ }
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
+
+undoit:
+ /* Re-zero any output coefficients that we made newly nonzero */
+ while (num_newnz > 0)
+ (*block)[newnz_pos[--num_newnz]] = 0;
+
+ return FALSE;
+}
+
+
+/*
+ * Module initialization routine for progressive Huffman entropy decoding.
+ */
+
+GLOBAL(void)
+jinit_phuff_decoder (j_decompress_ptr cinfo)
+{
+ phuff_entropy_ptr entropy;
+ int *coef_bit_ptr;
+ int ci, i;
+
+ entropy = (phuff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(phuff_entropy_decoder));
+ cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
+ entropy->pub.start_pass = start_pass_phuff_decoder;
+
+ /* Mark derived tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->derived_tbls[i] = NULL;
+ }
+
+ /* Create progression status table */
+ cinfo->coef_bits = (int (*)[DCTSIZE2])
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components*DCTSIZE2*SIZEOF(int));
+ coef_bit_ptr = & cinfo->coef_bits[0][0];
+ for (ci = 0; ci < cinfo->num_components; ci++)
+ for (i = 0; i < DCTSIZE2; i++)
+ *coef_bit_ptr++ = -1;
+}
+
+#endif /* D_PROGRESSIVE_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jdpostct.c b/test/monniaux/jpeg-6b/jdpostct.c
new file mode 100644
index 00000000..571563d7
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdpostct.c
@@ -0,0 +1,290 @@
+/*
+ * jdpostct.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the decompression postprocessing controller.
+ * This controller manages the upsampling, color conversion, and color
+ * quantization/reduction steps; specifically, it controls the buffering
+ * between upsample/color conversion and color quantization/reduction.
+ *
+ * If no color quantization/reduction is required, then this module has no
+ * work to do, and it just hands off to the upsample/color conversion code.
+ * An integrated upsample/convert/quantize process would replace this module
+ * entirely.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_post_controller pub; /* public fields */
+
+ /* Color quantization source buffer: this holds output data from
+ * the upsample/color conversion step to be passed to the quantizer.
+ * For two-pass color quantization, we need a full-image buffer;
+ * for one-pass operation, a strip buffer is sufficient.
+ */
+ jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
+ JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
+ JDIMENSION strip_height; /* buffer size in rows */
+ /* for two-pass mode only: */
+ JDIMENSION starting_row; /* row # of first row in current strip */
+ JDIMENSION next_row; /* index of next row to fill/empty in strip */
+} my_post_controller;
+
+typedef my_post_controller * my_post_ptr;
+
+
+/* Forward declarations */
+METHODDEF(void) post_process_1pass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF(void) post_process_prepass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+METHODDEF(void) post_process_2pass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (cinfo->quantize_colors) {
+ /* Single-pass processing with color quantization. */
+ post->pub.post_process_data = post_process_1pass;
+ /* We could be doing buffered-image output before starting a 2-pass
+ * color quantization; in that case, jinit_d_post_controller did not
+ * allocate a strip buffer. Use the virtual-array buffer as workspace.
+ */
+ if (post->buffer == NULL) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ (JDIMENSION) 0, post->strip_height, TRUE);
+ }
+ } else {
+ /* For single-pass processing without color quantization,
+ * I have no work to do; just call the upsampler directly.
+ */
+ post->pub.post_process_data = cinfo->upsample->upsample;
+ }
+ break;
+#ifdef QUANT_2PASS_SUPPORTED
+ case JBUF_SAVE_AND_PASS:
+ /* First pass of 2-pass quantization */
+ if (post->whole_image == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ post->pub.post_process_data = post_process_prepass;
+ break;
+ case JBUF_CRANK_DEST:
+ /* Second pass of 2-pass quantization */
+ if (post->whole_image == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ post->pub.post_process_data = post_process_2pass;
+ break;
+#endif /* QUANT_2PASS_SUPPORTED */
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+ post->starting_row = post->next_row = 0;
+}
+
+
+/*
+ * Process some data in the one-pass (strip buffer) case.
+ * This is used for color precision reduction as well as one-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_1pass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION num_rows, max_rows;
+
+ /* Fill the buffer, but not more than what we can dump out in one go. */
+ /* Note we rely on the upsampler to detect bottom of image. */
+ max_rows = out_rows_avail - *out_row_ctr;
+ if (max_rows > post->strip_height)
+ max_rows = post->strip_height;
+ num_rows = 0;
+ (*cinfo->upsample->upsample) (cinfo,
+ input_buf, in_row_group_ctr, in_row_groups_avail,
+ post->buffer, &num_rows, max_rows);
+ /* Quantize and emit data. */
+ (*cinfo->cquantize->color_quantize) (cinfo,
+ post->buffer, output_buf + *out_row_ctr, (int) num_rows);
+ *out_row_ctr += num_rows;
+}
+
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+/*
+ * Process some data in the first pass of 2-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_prepass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION old_next_row, num_rows;
+
+ /* Reposition virtual buffer if at start of strip. */
+ if (post->next_row == 0) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ post->starting_row, post->strip_height, TRUE);
+ }
+
+ /* Upsample some data (up to a strip height's worth). */
+ old_next_row = post->next_row;
+ (*cinfo->upsample->upsample) (cinfo,
+ input_buf, in_row_group_ctr, in_row_groups_avail,
+ post->buffer, &post->next_row, post->strip_height);
+
+ /* Allow quantizer to scan new data. No data is emitted, */
+ /* but we advance out_row_ctr so outer loop can tell when we're done. */
+ if (post->next_row > old_next_row) {
+ num_rows = post->next_row - old_next_row;
+ (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
+ (JSAMPARRAY) NULL, (int) num_rows);
+ *out_row_ctr += num_rows;
+ }
+
+ /* Advance if we filled the strip. */
+ if (post->next_row >= post->strip_height) {
+ post->starting_row += post->strip_height;
+ post->next_row = 0;
+ }
+}
+
+
+/*
+ * Process some data in the second pass of 2-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_2pass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION num_rows, max_rows;
+
+ /* Reposition virtual buffer if at start of strip. */
+ if (post->next_row == 0) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ post->starting_row, post->strip_height, FALSE);
+ }
+
+ /* Determine number of rows to emit. */
+ num_rows = post->strip_height - post->next_row; /* available in strip */
+ max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
+ if (num_rows > max_rows)
+ num_rows = max_rows;
+ /* We have to check bottom of image here, can't depend on upsampler. */
+ max_rows = cinfo->output_height - post->starting_row;
+ if (num_rows > max_rows)
+ num_rows = max_rows;
+
+ /* Quantize and emit data. */
+ (*cinfo->cquantize->color_quantize) (cinfo,
+ post->buffer + post->next_row, output_buf + *out_row_ctr,
+ (int) num_rows);
+ *out_row_ctr += num_rows;
+
+ /* Advance if we filled the strip. */
+ post->next_row += num_rows;
+ if (post->next_row >= post->strip_height) {
+ post->starting_row += post->strip_height;
+ post->next_row = 0;
+ }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize postprocessing controller.
+ */
+
+GLOBAL(void)
+jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_post_ptr post;
+
+ post = (my_post_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_post_controller));
+ cinfo->post = (struct jpeg_d_post_controller *) post;
+ post->pub.start_pass = start_pass_dpost;
+ post->whole_image = NULL; /* flag for no virtual arrays */
+ post->buffer = NULL; /* flag for no strip buffer */
+
+ /* Create the quantization buffer, if needed */
+ if (cinfo->quantize_colors) {
+ /* The buffer strip height is max_v_samp_factor, which is typically
+ * an efficient number of rows for upsampling to return.
+ * (In the presence of output rescaling, we might want to be smarter?)
+ */
+ post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
+ if (need_full_buffer) {
+ /* Two-pass color quantization: need full-image storage. */
+ /* We round up the number of rows to a multiple of the strip height. */
+#ifdef QUANT_2PASS_SUPPORTED
+ post->whole_image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ cinfo->output_width * cinfo->out_color_components,
+ (JDIMENSION) jround_up((long) cinfo->output_height,
+ (long) post->strip_height),
+ post->strip_height);
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif /* QUANT_2PASS_SUPPORTED */
+ } else {
+ /* One-pass color quantization: just make a strip buffer. */
+ post->buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->output_width * cinfo->out_color_components,
+ post->strip_height);
+ }
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdsample.c b/test/monniaux/jpeg-6b/jdsample.c
new file mode 100644
index 00000000..80ffefb2
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdsample.c
@@ -0,0 +1,478 @@
+/*
+ * jdsample.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains upsampling routines.
+ *
+ * Upsampling input data is counted in "row groups". A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component. Upsampling will normally produce
+ * max_v_samp_factor pixel rows from each row group (but this could vary
+ * if the upsampler is applying a scale factor of its own).
+ *
+ * An excellent reference for image resampling is
+ * Digital Image Warping, George Wolberg, 1990.
+ * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to upsample a single component */
+typedef JMETHOD(void, upsample1_ptr,
+ (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_upsampler pub; /* public fields */
+
+ /* Color conversion buffer. When using separate upsampling and color
+ * conversion steps, this buffer holds one upsampled row group until it
+ * has been color converted and output.
+ * Note: we do not allocate any storage for component(s) which are full-size,
+ * ie do not need rescaling. The corresponding entry of color_buf[] is
+ * simply set to point to the input data array, thereby avoiding copying.
+ */
+ JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+ /* Per-component upsampling method pointers */
+ upsample1_ptr methods[MAX_COMPONENTS];
+
+ int next_row_out; /* counts rows emitted from color_buf */
+ JDIMENSION rows_to_go; /* counts rows remaining in image */
+
+ /* Height of an input row group for each component. */
+ int rowgroup_height[MAX_COMPONENTS];
+
+ /* These arrays save pixel expansion factors so that int_expand need not
+ * recompute them each time. They are unused for other upsampling methods.
+ */
+ UINT8 h_expand[MAX_COMPONENTS];
+ UINT8 v_expand[MAX_COMPONENTS];
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
+
+
+/*
+ * Initialize for an upsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_upsample (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Mark the conversion buffer empty */
+ upsample->next_row_out = cinfo->max_v_samp_factor;
+ /* Initialize total-height counter for detecting bottom of image */
+ upsample->rows_to_go = cinfo->output_height;
+}
+
+
+/*
+ * Control routine to do upsampling (and color conversion).
+ *
+ * In this version we upsample each component independently.
+ * We upsample one row group into the conversion buffer, then apply
+ * color conversion a row at a time.
+ */
+
+METHODDEF(void)
+sep_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ int ci;
+ jpeg_component_info * compptr;
+ JDIMENSION num_rows;
+
+ /* Fill the conversion buffer, if it's empty */
+ if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Invoke per-component upsample method. Notice we pass a POINTER
+ * to color_buf[ci], so that fullsize_upsample can change it.
+ */
+ (*upsample->methods[ci]) (cinfo, compptr,
+ input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
+ upsample->color_buf + ci);
+ }
+ upsample->next_row_out = 0;
+ }
+
+ /* Color-convert and emit rows */
+
+ /* How many we have in the buffer: */
+ num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
+ /* Not more than the distance to the end of the image. Need this test
+ * in case the image height is not a multiple of max_v_samp_factor:
+ */
+ if (num_rows > upsample->rows_to_go)
+ num_rows = upsample->rows_to_go;
+ /* And not more than what the client can accept: */
+ out_rows_avail -= *out_row_ctr;
+ if (num_rows > out_rows_avail)
+ num_rows = out_rows_avail;
+
+ (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
+ (JDIMENSION) upsample->next_row_out,
+ output_buf + *out_row_ctr,
+ (int) num_rows);
+
+ /* Adjust counts */
+ *out_row_ctr += num_rows;
+ upsample->rows_to_go -= num_rows;
+ upsample->next_row_out += num_rows;
+ /* When the buffer is emptied, declare this input row group consumed */
+ if (upsample->next_row_out >= cinfo->max_v_samp_factor)
+ (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by sep_upsample to upsample pixel values
+ * of a single component. One row group is processed per call.
+ */
+
+
+/*
+ * For full-size components, we just make color_buf[ci] point at the
+ * input buffer, and thus avoid copying any data. Note that this is
+ * safe only because sep_upsample doesn't declare the input row group
+ * "consumed" until we are done color converting and emitting it.
+ */
+
+METHODDEF(void)
+fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ *output_data_ptr = input_data;
+}
+
+
+/*
+ * This is a no-op version used for "uninteresting" components.
+ * These components will not be referenced by color conversion.
+ */
+
+METHODDEF(void)
+noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ *output_data_ptr = NULL; /* safety check */
+}
+
+
+/*
+ * This version handles any integral sampling ratios.
+ * This is not used for typical JPEG files, so it need not be fast.
+ * Nor, for that matter, is it particularly accurate: the algorithm is
+ * simple replication of the input pixel onto the corresponding output
+ * pixels. The hi-falutin sampling literature refers to this as a
+ * "box filter". A box filter tends to introduce visible artifacts,
+ * so if you are actually going to use 3:1 or 4:1 sampling ratios
+ * you would be well advised to improve this code.
+ */
+
+METHODDEF(void)
+int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ register int h;
+ JSAMPROW outend;
+ int h_expand, v_expand;
+ int inrow, outrow;
+
+ h_expand = upsample->h_expand[compptr->component_index];
+ v_expand = upsample->v_expand[compptr->component_index];
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ /* Generate one output row with proper horizontal expansion */
+ inptr = input_data[inrow];
+ outptr = output_data[outrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ for (h = h_expand; h > 0; h--) {
+ *outptr++ = invalue;
+ }
+ }
+ /* Generate any additional output rows by duplicating the first one */
+ if (v_expand > 1) {
+ jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+ v_expand-1, cinfo->output_width);
+ }
+ inrow++;
+ outrow += v_expand;
+ }
+}
+
+
+/*
+ * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF(void)
+h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ JSAMPROW outend;
+ int inrow;
+
+ for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
+ inptr = input_data[inrow];
+ outptr = output_data[inrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ *outptr++ = invalue;
+ *outptr++ = invalue;
+ }
+ }
+}
+
+
+/*
+ * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF(void)
+h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ JSAMPROW outend;
+ int inrow, outrow;
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ inptr = input_data[inrow];
+ outptr = output_data[outrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ *outptr++ = invalue;
+ *outptr++ = invalue;
+ }
+ jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+ 1, cinfo->output_width);
+ inrow++;
+ outrow += 2;
+ }
+}
+
+
+/*
+ * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
+ *
+ * The upsampling algorithm is linear interpolation between pixel centers,
+ * also known as a "triangle filter". This is a good compromise between
+ * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
+ * of the way between input pixel centers.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
+ */
+
+METHODDEF(void)
+h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register int invalue;
+ register JDIMENSION colctr;
+ int inrow;
+
+ for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
+ inptr = input_data[inrow];
+ outptr = output_data[inrow];
+ /* Special case for first column */
+ invalue = GETJSAMPLE(*inptr++);
+ *outptr++ = (JSAMPLE) invalue;
+ *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
+
+ for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
+ /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
+ invalue = GETJSAMPLE(*inptr++) * 3;
+ *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
+ *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
+ }
+
+ /* Special case for last column */
+ invalue = GETJSAMPLE(*inptr);
+ *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
+ *outptr++ = (JSAMPLE) invalue;
+ }
+}
+
+
+/*
+ * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * Again a triangle filter; see comments for h2v1 case, above.
+ *
+ * It is OK for us to reference the adjacent input rows because we demanded
+ * context from the main buffer controller (see initialization code).
+ */
+
+METHODDEF(void)
+h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr0, inptr1, outptr;
+#if BITS_IN_JSAMPLE == 8
+ register int thiscolsum, lastcolsum, nextcolsum;
+#else
+ register INT32 thiscolsum, lastcolsum, nextcolsum;
+#endif
+ register JDIMENSION colctr;
+ int inrow, outrow, v;
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ for (v = 0; v < 2; v++) {
+ /* inptr0 points to nearest input row, inptr1 points to next nearest */
+ inptr0 = input_data[inrow];
+ if (v == 0) /* next nearest is row above */
+ inptr1 = input_data[inrow-1];
+ else /* next nearest is row below */
+ inptr1 = input_data[inrow+1];
+ outptr = output_data[outrow++];
+
+ /* Special case for first column */
+ thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
+ lastcolsum = thiscolsum; thiscolsum = nextcolsum;
+
+ for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
+ /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
+ /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
+ nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
+ lastcolsum = thiscolsum; thiscolsum = nextcolsum;
+ }
+
+ /* Special case for last column */
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
+ }
+ inrow++;
+ }
+}
+
+
+/*
+ * Module initialization routine for upsampling.
+ */
+
+GLOBAL(void)
+jinit_upsampler (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample;
+ int ci;
+ jpeg_component_info * compptr;
+ boolean need_buffer, do_fancy;
+ int h_in_group, v_in_group, h_out_group, v_out_group;
+
+ upsample = (my_upsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_upsampler));
+ cinfo->upsample = (struct jpeg_upsampler *) upsample;
+ upsample->pub.start_pass = start_pass_upsample;
+ upsample->pub.upsample = sep_upsample;
+ upsample->pub.need_context_rows = FALSE; /* until we find out differently */
+
+ if (cinfo->CCIR601_sampling) /* this isn't supported */
+ ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+ /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
+ * so don't ask for it.
+ */
+ do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
+
+ /* Verify we can handle the sampling factors, select per-component methods,
+ * and create storage as needed.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Compute size of an "input group" after IDCT scaling. This many samples
+ * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
+ */
+ h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size;
+ v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size;
+ h_out_group = cinfo->max_h_samp_factor;
+ v_out_group = cinfo->max_v_samp_factor;
+ upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
+ need_buffer = TRUE;
+ if (! compptr->component_needed) {
+ /* Don't bother to upsample an uninteresting component. */
+ upsample->methods[ci] = noop_upsample;
+ need_buffer = FALSE;
+ } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
+ /* Fullsize components can be processed without any work. */
+ upsample->methods[ci] = fullsize_upsample;
+ need_buffer = FALSE;
+ } else if (h_in_group * 2 == h_out_group &&
+ v_in_group == v_out_group) {
+ /* Special cases for 2h1v upsampling */
+ if (do_fancy && compptr->downsampled_width > 2)
+ upsample->methods[ci] = h2v1_fancy_upsample;
+ else
+ upsample->methods[ci] = h2v1_upsample;
+ } else if (h_in_group * 2 == h_out_group &&
+ v_in_group * 2 == v_out_group) {
+ /* Special cases for 2h2v upsampling */
+ if (do_fancy && compptr->downsampled_width > 2) {
+ upsample->methods[ci] = h2v2_fancy_upsample;
+ upsample->pub.need_context_rows = TRUE;
+ } else
+ upsample->methods[ci] = h2v2_upsample;
+ } else if ((h_out_group % h_in_group) == 0 &&
+ (v_out_group % v_in_group) == 0) {
+ /* Generic integral-factors upsampling method */
+ upsample->methods[ci] = int_upsample;
+ upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
+ upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
+ } else
+ ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
+ if (need_buffer) {
+ upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) jround_up((long) cinfo->output_width,
+ (long) cinfo->max_h_samp_factor),
+ (JDIMENSION) cinfo->max_v_samp_factor);
+ }
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jdtrans.c b/test/monniaux/jpeg-6b/jdtrans.c
new file mode 100644
index 00000000..6c0ab715
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jdtrans.c
@@ -0,0 +1,143 @@
+/*
+ * jdtrans.c
+ *
+ * Copyright (C) 1995-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains library routines for transcoding decompression,
+ * that is, reading raw DCT coefficient arrays from an input JPEG file.
+ * The routines in jdapimin.c will also be needed by a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Read the coefficient arrays from a JPEG file.
+ * jpeg_read_header must be completed before calling this.
+ *
+ * The entire image is read into a set of virtual coefficient-block arrays,
+ * one per component. The return value is a pointer to the array of
+ * virtual-array descriptors. These can be manipulated directly via the
+ * JPEG memory manager, or handed off to jpeg_write_coefficients().
+ * To release the memory occupied by the virtual arrays, call
+ * jpeg_finish_decompress() when done with the data.
+ *
+ * An alternative usage is to simply obtain access to the coefficient arrays
+ * during a buffered-image-mode decompression operation. This is allowed
+ * after any jpeg_finish_output() call. The arrays can be accessed until
+ * jpeg_finish_decompress() is called. (Note that any call to the library
+ * may reposition the arrays, so don't rely on access_virt_barray() results
+ * to stay valid across library calls.)
+ *
+ * Returns NULL if suspended. This case need be checked only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(jvirt_barray_ptr *)
+jpeg_read_coefficients (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state == DSTATE_READY) {
+ /* First call: initialize active modules */
+ transdecode_master_selection(cinfo);
+ cinfo->global_state = DSTATE_RDCOEFS;
+ }
+ if (cinfo->global_state == DSTATE_RDCOEFS) {
+ /* Absorb whole file into the coef buffer */
+ for (;;) {
+ int retcode;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL)
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ /* Absorb some more input */
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_SUSPENDED)
+ return NULL;
+ if (retcode == JPEG_REACHED_EOI)
+ break;
+ /* Advance progress counter if appropriate */
+ if (cinfo->progress != NULL &&
+ (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+ if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+ /* startup underestimated number of scans; ratchet up one scan */
+ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+ }
+ }
+ }
+ /* Set state so that jpeg_finish_decompress does the right thing */
+ cinfo->global_state = DSTATE_STOPPING;
+ }
+ /* At this point we should be in state DSTATE_STOPPING if being used
+ * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
+ * to the coefficients during a full buffered-image-mode decompression.
+ */
+ if ((cinfo->global_state == DSTATE_STOPPING ||
+ cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
+ return cinfo->coef->coef_arrays;
+ }
+ /* Oops, improper usage */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return NULL; /* keep compiler happy */
+}
+
+
+/*
+ * Master selection of decompression modules for transcoding.
+ * This substitutes for jdmaster.c's initialization of the full decompressor.
+ */
+
+LOCAL(void)
+transdecode_master_selection (j_decompress_ptr cinfo)
+{
+ /* This is effectively a buffered-image operation. */
+ cinfo->buffered_image = TRUE;
+
+ /* Entropy decoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef D_PROGRESSIVE_SUPPORTED
+ jinit_phuff_decoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_decoder(cinfo);
+ }
+
+ /* Always get a full-image coefficient buffer. */
+ jinit_d_coef_controller(cinfo, TRUE);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Initialize input side of decompressor to consume first scan. */
+ (*cinfo->inputctl->start_input_pass) (cinfo);
+
+ /* Initialize progress monitoring. */
+ if (cinfo->progress != NULL) {
+ int nscans;
+ /* Estimate number of scans to set pass_limit. */
+ if (cinfo->progressive_mode) {
+ /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
+ nscans = 2 + 3 * cinfo->num_components;
+ } else if (cinfo->inputctl->has_multiple_scans) {
+ /* For a nonprogressive multiscan file, estimate 1 scan per component. */
+ nscans = cinfo->num_components;
+ } else {
+ nscans = 1;
+ }
+ cinfo->progress->pass_counter = 0L;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
+ cinfo->progress->completed_passes = 0;
+ cinfo->progress->total_passes = 1;
+ }
+}
diff --git a/test/monniaux/jpeg-6b/jerror.c b/test/monniaux/jpeg-6b/jerror.c
new file mode 100644
index 00000000..3da7be86
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jerror.c
@@ -0,0 +1,252 @@
+/*
+ * jerror.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains simple error-reporting and trace-message routines.
+ * These are suitable for Unix-like systems and others where writing to
+ * stderr is the right thing to do. Many applications will want to replace
+ * some or all of these routines.
+ *
+ * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
+ * you get a Windows-specific hack to display error messages in a dialog box.
+ * It ain't much, but it beats dropping error messages into the bit bucket,
+ * which is what happens to output to stderr under most Windows C compilers.
+ *
+ * These routines are used by both the compression and decompression code.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jversion.h"
+#include "jerror.h"
+
+#ifdef USE_WINDOWS_MESSAGEBOX
+#include <windows.h>
+#endif
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+
+
+/*
+ * Create the message string table.
+ * We do this from the master message list in jerror.h by re-reading
+ * jerror.h with a suitable definition for macro JMESSAGE.
+ * The message table is made an external symbol just in case any applications
+ * want to refer to it directly.
+ */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_std_message_table jMsgTable
+#endif
+
+#define JMESSAGE(code,string) string ,
+
+const char * const jpeg_std_message_table[] = {
+#include "jerror.h"
+ NULL
+};
+
+
+/*
+ * Error exit handler: must not return to caller.
+ *
+ * Applications may override this if they want to get control back after
+ * an error. Typically one would longjmp somewhere instead of exiting.
+ * The setjmp buffer can be made a private field within an expanded error
+ * handler object. Note that the info needed to generate an error message
+ * is stored in the error object, so you can generate the message now or
+ * later, at your convenience.
+ * You should make sure that the JPEG object is cleaned up (with jpeg_abort
+ * or jpeg_destroy) at some point.
+ */
+
+METHODDEF(void)
+error_exit (j_common_ptr cinfo)
+{
+ /* Always display the message */
+ (*cinfo->err->output_message) (cinfo);
+
+ /* Let the memory manager delete any temp files before we die */
+ jpeg_destroy(cinfo);
+
+ exit(EXIT_FAILURE);
+}
+
+
+/*
+ * Actual output of an error or trace message.
+ * Applications may override this method to send JPEG messages somewhere
+ * other than stderr.
+ *
+ * On Windows, printing to stderr is generally completely useless,
+ * so we provide optional code to produce an error-dialog popup.
+ * Most Windows applications will still prefer to override this routine,
+ * but if they don't, it'll do something at least marginally useful.
+ *
+ * NOTE: to use the library in an environment that doesn't support the
+ * C stdio library, you may have to delete the call to fprintf() entirely,
+ * not just not use this routine.
+ */
+
+METHODDEF(void)
+output_message (j_common_ptr cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+
+ /* Create the message */
+ (*cinfo->err->format_message) (cinfo, buffer);
+
+#ifdef USE_WINDOWS_MESSAGEBOX
+ /* Display it in a message dialog box */
+ MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
+ MB_OK | MB_ICONERROR);
+#else
+ /* Send it to stderr, adding a newline */
+ fprintf(stderr, "%s\n", buffer);
+#endif
+}
+
+
+/*
+ * Decide whether to emit a trace or warning message.
+ * msg_level is one of:
+ * -1: recoverable corrupt-data warning, may want to abort.
+ * 0: important advisory messages (always display to user).
+ * 1: first level of tracing detail.
+ * 2,3,...: successively more detailed tracing messages.
+ * An application might override this method if it wanted to abort on warnings
+ * or change the policy about which messages to display.
+ */
+
+METHODDEF(void)
+emit_message (j_common_ptr cinfo, int msg_level)
+{
+ struct jpeg_error_mgr * err = cinfo->err;
+
+ if (msg_level < 0) {
+ /* It's a warning message. Since corrupt files may generate many warnings,
+ * the policy implemented here is to show only the first warning,
+ * unless trace_level >= 3.
+ */
+ if (err->num_warnings == 0 || err->trace_level >= 3)
+ (*err->output_message) (cinfo);
+ /* Always count warnings in num_warnings. */
+ err->num_warnings++;
+ } else {
+ /* It's a trace message. Show it if trace_level >= msg_level. */
+ if (err->trace_level >= msg_level)
+ (*err->output_message) (cinfo);
+ }
+}
+
+
+/*
+ * Format a message string for the most recent JPEG error or message.
+ * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
+ * characters. Note that no '\n' character is added to the string.
+ * Few applications should need to override this method.
+ */
+
+METHODDEF(void)
+format_message (j_common_ptr cinfo, char * buffer)
+{
+ struct jpeg_error_mgr * err = cinfo->err;
+ int msg_code = err->msg_code;
+ const char * msgtext = NULL;
+ const char * msgptr;
+ char ch;
+ boolean isstring;
+
+ /* Look up message string in proper table */
+ if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
+ msgtext = err->jpeg_message_table[msg_code];
+ } else if (err->addon_message_table != NULL &&
+ msg_code >= err->first_addon_message &&
+ msg_code <= err->last_addon_message) {
+ msgtext = err->addon_message_table[msg_code - err->first_addon_message];
+ }
+
+ /* Defend against bogus message number */
+ if (msgtext == NULL) {
+ err->msg_parm.i[0] = msg_code;
+ msgtext = err->jpeg_message_table[0];
+ }
+
+ /* Check for string parameter, as indicated by %s in the message text */
+ isstring = FALSE;
+ msgptr = msgtext;
+ while ((ch = *msgptr++) != '\0') {
+ if (ch == '%') {
+ if (*msgptr == 's') isstring = TRUE;
+ break;
+ }
+ }
+
+ /* Format the message into the passed buffer */
+ if (isstring)
+ sprintf(buffer, msgtext, err->msg_parm.s);
+ else
+ sprintf(buffer, msgtext,
+ err->msg_parm.i[0], err->msg_parm.i[1],
+ err->msg_parm.i[2], err->msg_parm.i[3],
+ err->msg_parm.i[4], err->msg_parm.i[5],
+ err->msg_parm.i[6], err->msg_parm.i[7]);
+}
+
+
+/*
+ * Reset error state variables at start of a new image.
+ * This is called during compression startup to reset trace/error
+ * processing to default state, without losing any application-specific
+ * method pointers. An application might possibly want to override
+ * this method if it has additional error processing state.
+ */
+
+METHODDEF(void)
+reset_error_mgr (j_common_ptr cinfo)
+{
+ cinfo->err->num_warnings = 0;
+ /* trace_level is not reset since it is an application-supplied parameter */
+ cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
+}
+
+
+/*
+ * Fill in the standard error-handling methods in a jpeg_error_mgr object.
+ * Typical call is:
+ * struct jpeg_compress_struct cinfo;
+ * struct jpeg_error_mgr err;
+ *
+ * cinfo.err = jpeg_std_error(&err);
+ * after which the application may override some of the methods.
+ */
+
+GLOBAL(struct jpeg_error_mgr *)
+jpeg_std_error (struct jpeg_error_mgr * err)
+{
+ err->error_exit = error_exit;
+ err->emit_message = emit_message;
+ err->output_message = output_message;
+ err->format_message = format_message;
+ err->reset_error_mgr = reset_error_mgr;
+
+ err->trace_level = 0; /* default = no tracing */
+ err->num_warnings = 0; /* no warnings emitted yet */
+ err->msg_code = 0; /* may be useful as a flag for "no error" */
+
+ /* Initialize message table pointers */
+ err->jpeg_message_table = jpeg_std_message_table;
+ err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
+
+ err->addon_message_table = NULL;
+ err->first_addon_message = 0; /* for safety */
+ err->last_addon_message = 0;
+
+ return err;
+}
diff --git a/test/monniaux/jpeg-6b/jerror.h b/test/monniaux/jpeg-6b/jerror.h
new file mode 100644
index 00000000..fc2fffea
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jerror.h
@@ -0,0 +1,291 @@
+/*
+ * jerror.h
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the error and message codes for the JPEG library.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ * A set of error-reporting macros are defined too. Some applications using
+ * the JPEG library may wish to include this file to get the error codes
+ * and/or the macros.
+ */
+
+/*
+ * To define the enum list of message codes, include this file without
+ * defining macro JMESSAGE. To create a message string table, include it
+ * again with a suitable JMESSAGE definition (see jerror.c for an example).
+ */
+#ifndef JMESSAGE
+#ifndef JERROR_H
+/* First time through, define the enum list */
+#define JMAKE_ENUM_LIST
+#else
+/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
+#define JMESSAGE(code,string)
+#endif /* JERROR_H */
+#endif /* JMESSAGE */
+
+#ifdef JMAKE_ENUM_LIST
+
+typedef enum {
+
+#define JMESSAGE(code,string) code ,
+
+#endif /* JMAKE_ENUM_LIST */
+
+JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
+
+/* For maintenance convenience, list is alphabetical by message code name */
+JMESSAGE(JERR_ARITH_NOTIMPL,
+ "Sorry, there are legal restrictions on arithmetic coding")
+JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
+JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
+JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
+JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
+JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
+JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
+JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
+JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
+JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
+JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
+JMESSAGE(JERR_BAD_LIB_VERSION,
+ "Wrong JPEG library version: library is %d, caller expects %d")
+JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
+JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
+JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
+JMESSAGE(JERR_BAD_PROGRESSION,
+ "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
+JMESSAGE(JERR_BAD_PROG_SCRIPT,
+ "Invalid progressive parameters at scan script entry %d")
+JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
+JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
+JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
+JMESSAGE(JERR_BAD_STRUCT_SIZE,
+ "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
+JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
+JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
+JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
+JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
+JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
+JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
+JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
+JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
+JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
+JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
+JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
+JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
+JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
+JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
+JMESSAGE(JERR_FILE_READ, "Input file read error")
+JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
+JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
+JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
+JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
+JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
+JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
+JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
+JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
+ "Cannot transcode due to multiple use of quantization table %d")
+JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
+JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
+JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
+JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
+JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
+JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
+JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
+JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
+JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
+JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
+JMESSAGE(JERR_QUANT_COMPONENTS,
+ "Cannot quantize more than %d color components")
+JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
+JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
+JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
+JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
+JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
+JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
+JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
+JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
+JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
+JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
+JMESSAGE(JERR_TFILE_WRITE,
+ "Write failed on temporary file --- out of disk space?")
+JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
+JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
+JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
+JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
+JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
+JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
+JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
+JMESSAGE(JMSG_VERSION, JVERSION)
+JMESSAGE(JTRC_16BIT_TABLES,
+ "Caution: quantization tables are too coarse for baseline JPEG")
+JMESSAGE(JTRC_ADOBE,
+ "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
+JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
+JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
+JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
+JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
+JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d")
+JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
+JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
+JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
+JMESSAGE(JTRC_EOI, "End Of Image")
+JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d")
+JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d")
+JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
+ "Warning: thumbnail image size does not match data length %u")
+JMESSAGE(JTRC_JFIF_EXTENSION,
+ "JFIF extension marker: type 0x%02x, length %u")
+JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image")
+JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
+JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
+JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u")
+JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
+JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
+JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
+JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
+JMESSAGE(JTRC_RST, "RST%d")
+JMESSAGE(JTRC_SMOOTH_NOTIMPL,
+ "Smoothing not supported with nonstandard sampling ratios")
+JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
+JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d")
+JMESSAGE(JTRC_SOI, "Start of Image")
+JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
+JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d")
+JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d")
+JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
+JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
+JMESSAGE(JTRC_THUMB_JPEG,
+ "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
+JMESSAGE(JTRC_THUMB_PALETTE,
+ "JFIF extension marker: palette thumbnail image, length %u")
+JMESSAGE(JTRC_THUMB_RGB,
+ "JFIF extension marker: RGB thumbnail image, length %u")
+JMESSAGE(JTRC_UNKNOWN_IDS,
+ "Unrecognized component IDs %d %d %d, assuming YCbCr")
+JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
+JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
+JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
+JMESSAGE(JWRN_BOGUS_PROGRESSION,
+ "Inconsistent progression sequence for component %d coefficient %d")
+JMESSAGE(JWRN_EXTRANEOUS_DATA,
+ "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
+JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
+JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
+JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
+JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
+JMESSAGE(JWRN_MUST_RESYNC,
+ "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
+JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
+JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
+
+#ifdef JMAKE_ENUM_LIST
+
+ JMSG_LASTMSGCODE
+} J_MESSAGE_CODE;
+
+#undef JMAKE_ENUM_LIST
+#endif /* JMAKE_ENUM_LIST */
+
+/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
+#undef JMESSAGE
+
+
+#ifndef JERROR_H
+#define JERROR_H
+
+/* Macros to simplify using the error and trace message stuff */
+/* The first parameter is either type of cinfo pointer */
+
+/* Fatal errors (print message and exit) */
+#define ERREXIT(cinfo,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT1(cinfo,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT2(cinfo,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT3(cinfo,code,p1,p2,p3) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (cinfo)->err->msg_parm.i[2] = (p3), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT4(cinfo,code,p1,p2,p3,p4) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (cinfo)->err->msg_parm.i[2] = (p3), \
+ (cinfo)->err->msg_parm.i[3] = (p4), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXITS(cinfo,code,str) \
+ ((cinfo)->err->msg_code = (code), \
+ strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+
+#define MAKESTMT(stuff) do { stuff } while (0)
+
+/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
+#define WARNMS(cinfo,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS1(cinfo,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS2(cinfo,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+
+/* Informational/debugging messages */
+#define TRACEMS(cinfo,lvl,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS1(cinfo,lvl,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS2(cinfo,lvl,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ _mp[4] = (p5); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMSS(cinfo,lvl,code,str) \
+ ((cinfo)->err->msg_code = (code), \
+ strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+
+#endif /* JERROR_H */
diff --git a/test/monniaux/jpeg-6b/jfdctflt.c b/test/monniaux/jpeg-6b/jfdctflt.c
new file mode 100644
index 00000000..79d7a007
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jfdctflt.c
@@ -0,0 +1,168 @@
+/*
+ * jfdctflt.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a floating-point implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * This implementation should be more accurate than either of the integer
+ * DCT implementations. However, it may not give the same results on all
+ * machines because of differences in roundoff behavior. Speed will depend
+ * on the hardware's floating point capacity.
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column. Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with a fixed-point
+ * implementation, accuracy is lost due to imprecise representation of the
+ * scaled quantization values. However, that problem does not arise if
+ * we use floating point arithmetic.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL(void)
+jpeg_fdct_float (FAST_FLOAT * data)
+{
+ FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
+ FAST_FLOAT z1, z2, z3, z4, z5, z11, z13;
+ FAST_FLOAT *dataptr;
+ int ctr;
+
+ /* Pass 1: process rows. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[0] + dataptr[7];
+ tmp7 = dataptr[0] - dataptr[7];
+ tmp1 = dataptr[1] + dataptr[6];
+ tmp6 = dataptr[1] - dataptr[6];
+ tmp2 = dataptr[2] + dataptr[5];
+ tmp5 = dataptr[2] - dataptr[5];
+ tmp3 = dataptr[3] + dataptr[4];
+ tmp4 = dataptr[3] - dataptr[4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[4] = tmp10 - tmp11;
+
+ z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
+ dataptr[2] = tmp13 + z1; /* phase 5 */
+ dataptr[6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
+ z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
+ z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
+ z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[5] = z13 + z2; /* phase 6 */
+ dataptr[3] = z13 - z2;
+ dataptr[1] = z11 + z4;
+ dataptr[7] = z11 - z4;
+
+ dataptr += DCTSIZE; /* advance pointer to next row */
+ }
+
+ /* Pass 2: process columns. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+ tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+ tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+ tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+ tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+ tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+ tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+ tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[DCTSIZE*4] = tmp10 - tmp11;
+
+ z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
+ dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
+ dataptr[DCTSIZE*6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
+ z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
+ z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
+ z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
+ dataptr[DCTSIZE*3] = z13 - z2;
+ dataptr[DCTSIZE*1] = z11 + z4;
+ dataptr[DCTSIZE*7] = z11 - z4;
+
+ dataptr++; /* advance pointer to next column */
+ }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jfdctfst.c b/test/monniaux/jpeg-6b/jfdctfst.c
new file mode 100644
index 00000000..ccb378a3
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jfdctfst.c
@@ -0,0 +1,224 @@
+/*
+ * jfdctfst.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a fast, not so accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column. Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values. The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling decisions are generally the same as in the LL&M algorithm;
+ * see jfdctint.c for more details. However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * Again to save a few shifts, the intermediate results between pass 1 and
+ * pass 2 are not upscaled, but are represented only to integral precision.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13. This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#define CONST_BITS 8
+
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 8
+#define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */
+#define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */
+#define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */
+#define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */
+#else
+#define FIX_0_382683433 FIX(0.382683433)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_707106781 FIX(0.707106781)
+#define FIX_1_306562965 FIX(1.306562965)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift. This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n) RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL(void)
+jpeg_fdct_ifast (DCTELEM * data)
+{
+ DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ DCTELEM tmp10, tmp11, tmp12, tmp13;
+ DCTELEM z1, z2, z3, z4, z5, z11, z13;
+ DCTELEM *dataptr;
+ int ctr;
+ SHIFT_TEMPS
+
+ /* Pass 1: process rows. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[0] + dataptr[7];
+ tmp7 = dataptr[0] - dataptr[7];
+ tmp1 = dataptr[1] + dataptr[6];
+ tmp6 = dataptr[1] - dataptr[6];
+ tmp2 = dataptr[2] + dataptr[5];
+ tmp5 = dataptr[2] - dataptr[5];
+ tmp3 = dataptr[3] + dataptr[4];
+ tmp4 = dataptr[3] - dataptr[4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[4] = tmp10 - tmp11;
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+ dataptr[2] = tmp13 + z1; /* phase 5 */
+ dataptr[6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[5] = z13 + z2; /* phase 6 */
+ dataptr[3] = z13 - z2;
+ dataptr[1] = z11 + z4;
+ dataptr[7] = z11 - z4;
+
+ dataptr += DCTSIZE; /* advance pointer to next row */
+ }
+
+ /* Pass 2: process columns. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+ tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+ tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+ tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+ tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+ tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+ tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+ tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[DCTSIZE*4] = tmp10 - tmp11;
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+ dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
+ dataptr[DCTSIZE*6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
+ dataptr[DCTSIZE*3] = z13 - z2;
+ dataptr[DCTSIZE*1] = z11 + z4;
+ dataptr[DCTSIZE*7] = z11 - z4;
+
+ dataptr++; /* advance pointer to next column */
+ }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jfdctint.c b/test/monniaux/jpeg-6b/jfdctint.c
new file mode 100644
index 00000000..0a78b64a
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jfdctint.c
@@ -0,0 +1,283 @@
+/*
+ * jfdctint.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a slow-but-accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column. Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on an algorithm described in
+ * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
+ * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
+ * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
+ * The primary algorithm described there uses 11 multiplies and 29 adds.
+ * We use their alternate method with 12 multiplies and 32 adds.
+ * The advantage of this method is that no data path contains more than one
+ * multiplication; this allows a very simple and accurate implementation in
+ * scaled fixed-point arithmetic, with a minimal number of shifts.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * The poop on this scaling stuff is as follows:
+ *
+ * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
+ * larger than the true DCT outputs. The final outputs are therefore
+ * a factor of N larger than desired; since N=8 this can be cured by
+ * a simple right shift at the end of the algorithm. The advantage of
+ * this arrangement is that we save two multiplications per 1-D DCT,
+ * because the y0 and y4 outputs need not be divided by sqrt(N).
+ * In the IJG code, this factor of 8 is removed by the quantization step
+ * (in jcdctmgr.c), NOT in this module.
+ *
+ * We have to do addition and subtraction of the integer inputs, which
+ * is no problem, and multiplication by fractional constants, which is
+ * a problem to do in integer arithmetic. We multiply all the constants
+ * by CONST_SCALE and convert them to integer constants (thus retaining
+ * CONST_BITS bits of precision in the constants). After doing a
+ * multiplication we have to divide the product by CONST_SCALE, with proper
+ * rounding, to produce the correct output. This division can be done
+ * cheaply as a right shift of CONST_BITS bits. We postpone shifting
+ * as long as possible so that partial sums can be added together with
+ * full fractional precision.
+ *
+ * The outputs of the first pass are scaled up by PASS1_BITS bits so that
+ * they are represented to better-than-integral precision. These outputs
+ * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
+ * with the recommended scaling. (For 12-bit sample data, the intermediate
+ * array is INT32 anyway.)
+ *
+ * To avoid overflow of the 32-bit intermediate results in pass 2, we must
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
+ * shows that the values given below are the most effective.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS 13
+#define PASS1_BITS 2
+#else
+#define CONST_BITS 13
+#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 13
+#define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */
+#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
+#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
+#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
+#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
+#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
+#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
+#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
+#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
+#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
+#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
+#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
+#else
+#define FIX_0_298631336 FIX(0.298631336)
+#define FIX_0_390180644 FIX(0.390180644)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_765366865 FIX(0.765366865)
+#define FIX_0_899976223 FIX(0.899976223)
+#define FIX_1_175875602 FIX(1.175875602)
+#define FIX_1_501321110 FIX(1.501321110)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_1_961570560 FIX(1.961570560)
+#define FIX_2_053119869 FIX(2.053119869)
+#define FIX_2_562915447 FIX(2.562915447)
+#define FIX_3_072711026 FIX(3.072711026)
+#endif
+
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * For 8-bit samples with the recommended scaling, all the variable
+ * and constant values involved are no more than 16 bits wide, so a
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const) MULTIPLY16C16(var,const)
+#else
+#define MULTIPLY(var,const) ((var) * (const))
+#endif
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL(void)
+jpeg_fdct_islow (DCTELEM * data)
+{
+ INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ INT32 tmp10, tmp11, tmp12, tmp13;
+ INT32 z1, z2, z3, z4, z5;
+ DCTELEM *dataptr;
+ int ctr;
+ SHIFT_TEMPS
+
+ /* Pass 1: process rows. */
+ /* Note results are scaled up by sqrt(8) compared to a true DCT; */
+ /* furthermore, we scale the results by 2**PASS1_BITS. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[0] + dataptr[7];
+ tmp7 = dataptr[0] - dataptr[7];
+ tmp1 = dataptr[1] + dataptr[6];
+ tmp6 = dataptr[1] - dataptr[6];
+ tmp2 = dataptr[2] + dataptr[5];
+ tmp5 = dataptr[2] - dataptr[5];
+ tmp3 = dataptr[3] + dataptr[4];
+ tmp4 = dataptr[3] - dataptr[4];
+
+ /* Even part per LL&M figure 1 --- note that published figure is faulty;
+ * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+ */
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
+ dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+ dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
+ CONST_BITS-PASS1_BITS);
+ dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
+ CONST_BITS-PASS1_BITS);
+
+ /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
+ * cK represents cos(K*pi/16).
+ * i0..i3 in the paper are tmp4..tmp7 here.
+ */
+
+ z1 = tmp4 + tmp7;
+ z2 = tmp5 + tmp6;
+ z3 = tmp4 + tmp6;
+ z4 = tmp5 + tmp7;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
+ dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
+ dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
+ dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
+
+ dataptr += DCTSIZE; /* advance pointer to next row */
+ }
+
+ /* Pass 2: process columns.
+ * We remove the PASS1_BITS scaling, but leave the results scaled up
+ * by an overall factor of 8.
+ */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+ tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+ tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+ tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+ tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+ tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+ tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+ tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+
+ /* Even part per LL&M figure 1 --- note that published figure is faulty;
+ * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+ */
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
+ dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+ dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
+ CONST_BITS+PASS1_BITS);
+
+ /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
+ * cK represents cos(K*pi/16).
+ * i0..i3 in the paper are tmp4..tmp7 here.
+ */
+
+ z1 = tmp4 + tmp7;
+ z2 = tmp5 + tmp6;
+ z3 = tmp4 + tmp6;
+ z4 = tmp5 + tmp7;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
+ CONST_BITS+PASS1_BITS);
+
+ dataptr++; /* advance pointer to next column */
+ }
+}
+
+#endif /* DCT_ISLOW_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jidctflt.c b/test/monniaux/jpeg-6b/jidctflt.c
new file mode 100644
index 00000000..0188ce3d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jidctflt.c
@@ -0,0 +1,242 @@
+/*
+ * jidctflt.c
+ *
+ * Copyright (C) 1994-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a floating-point implementation of the
+ * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * This implementation should be more accurate than either of the integer
+ * IDCT implementations. However, it may not give the same results on all
+ * machines because of differences in roundoff behavior. Speed will depend
+ * on the hardware's floating point capacity.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time). Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with a fixed-point
+ * implementation, accuracy is lost due to imprecise representation of the
+ * scaled quantization values. However, that problem does not arise if
+ * we use floating point arithmetic.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce a float result.
+ */
+
+#define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL(void)
+jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
+ FAST_FLOAT z5, z10, z11, z12, z13;
+ JCOEFPTR inptr;
+ FLOAT_MULT_TYPE * quantptr;
+ FAST_FLOAT * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
+ SHIFT_TEMPS
+
+ /* Pass 1: process columns from input, store into work array. */
+
+ inptr = coef_block;
+ quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; ctr--) {
+ /* Due to quantization, we will usually find that many of the input
+ * coefficients are zero, especially the AC terms. We can exploit this
+ * by short-circuiting the IDCT calculation for any column in which all
+ * the AC terms are zero. In that case each output is equal to the
+ * DC coefficient (with scale factor as needed).
+ * With typical images and quantization tables, half or more of the
+ * column DCT calculations can be simplified this way.
+ */
+
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+ inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero */
+ FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+ wsptr[DCTSIZE*2] = dcval;
+ wsptr[DCTSIZE*3] = dcval;
+ wsptr[DCTSIZE*4] = dcval;
+ wsptr[DCTSIZE*5] = dcval;
+ wsptr[DCTSIZE*6] = dcval;
+ wsptr[DCTSIZE*7] = dcval;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ continue;
+ }
+
+ /* Even part */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+ tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ tmp10 = tmp0 + tmp2; /* phase 3 */
+ tmp11 = tmp0 - tmp2;
+
+ tmp13 = tmp1 + tmp3; /* phases 5-3 */
+ tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
+
+ tmp0 = tmp10 + tmp13; /* phase 2 */
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+ tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+
+ z13 = tmp6 + tmp5; /* phase 6 */
+ z10 = tmp6 - tmp5;
+ z11 = tmp4 + tmp7;
+ z12 = tmp4 - tmp7;
+
+ tmp7 = z11 + z13; /* phase 5 */
+ tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
+
+ z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
+ tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
+ tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7; /* phase 2 */
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ wsptr[DCTSIZE*0] = tmp0 + tmp7;
+ wsptr[DCTSIZE*7] = tmp0 - tmp7;
+ wsptr[DCTSIZE*1] = tmp1 + tmp6;
+ wsptr[DCTSIZE*6] = tmp1 - tmp6;
+ wsptr[DCTSIZE*2] = tmp2 + tmp5;
+ wsptr[DCTSIZE*5] = tmp2 - tmp5;
+ wsptr[DCTSIZE*4] = tmp3 + tmp4;
+ wsptr[DCTSIZE*3] = tmp3 - tmp4;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ }
+
+ /* Pass 2: process rows from work array, store into output array. */
+ /* Note that we must descale the results by a factor of 8 == 2**3. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < DCTSIZE; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* Rows of zeroes can be exploited in the same way as we did with columns.
+ * However, the column calculation has created many nonzero AC terms, so
+ * the simplification applies less often (typically 5% to 10% of the time).
+ * And testing floats for zero is relatively expensive, so we don't bother.
+ */
+
+ /* Even part */
+
+ tmp10 = wsptr[0] + wsptr[4];
+ tmp11 = wsptr[0] - wsptr[4];
+
+ tmp13 = wsptr[2] + wsptr[6];
+ tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
+
+ tmp0 = tmp10 + tmp13;
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ z13 = wsptr[5] + wsptr[3];
+ z10 = wsptr[5] - wsptr[3];
+ z11 = wsptr[1] + wsptr[7];
+ z12 = wsptr[1] - wsptr[7];
+
+ tmp7 = z11 + z13;
+ tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
+
+ z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
+ tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
+ tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7;
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ /* Final output stage: scale down by a factor of 8 and range-limit */
+
+ outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
+ & RANGE_MASK];
+ outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
+ & RANGE_MASK];
+ outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
+ & RANGE_MASK];
+ outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
+ & RANGE_MASK];
+ outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
+ & RANGE_MASK];
+ outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
+ & RANGE_MASK];
+ outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
+ & RANGE_MASK];
+ outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jidctfst.c b/test/monniaux/jpeg-6b/jidctfst.c
new file mode 100644
index 00000000..dba4216f
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jidctfst.c
@@ -0,0 +1,368 @@
+/*
+ * jidctfst.c
+ *
+ * Copyright (C) 1994-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a fast, not so accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time). Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values. The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling decisions are generally the same as in the LL&M algorithm;
+ * see jidctint.c for more details. However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * The dequantized coefficients are not integers because the AA&N scaling
+ * factors have been incorporated. We represent them scaled up by PASS1_BITS,
+ * so that the first and second IDCT rounds have the same input scaling.
+ * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
+ * avoid a descaling shift; this compromises accuracy rather drastically
+ * for small quantization table entries, but it saves a lot of shifts.
+ * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
+ * so we use a much larger scaling factor to preserve accuracy.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13. This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS 8
+#define PASS1_BITS 2
+#else
+#define CONST_BITS 8
+#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 8
+#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */
+#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */
+#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */
+#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */
+#else
+#define FIX_1_082392200 FIX(1.082392200)
+#define FIX_1_414213562 FIX(1.414213562)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_2_613125930 FIX(2.613125930)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift. This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n) RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce a DCTELEM result. For 8-bit data a 16x16->16
+ * multiplication will do. For 12-bit data, the multiplier table is
+ * declared INT32, so a 32-bit multiply will be used.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))
+#else
+#define DEQUANTIZE(coef,quantval) \
+ DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
+#endif
+
+
+/* Like DESCALE, but applies to a DCTELEM and produces an int.
+ * We assume that int right shift is unsigned if INT32 right shift is.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define ISHIFT_TEMPS DCTELEM ishift_temp;
+#if BITS_IN_JSAMPLE == 8
+#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */
+#else
+#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */
+#endif
+#define IRIGHT_SHIFT(x,shft) \
+ ((ishift_temp = (x)) < 0 ? \
+ (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
+ (ishift_temp >> (shft)))
+#else
+#define ISHIFT_TEMPS
+#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
+#endif
+
+#ifdef USE_ACCURATE_ROUNDING
+#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
+#else
+#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n))
+#endif
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL(void)
+jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ DCTELEM tmp10, tmp11, tmp12, tmp13;
+ DCTELEM z5, z10, z11, z12, z13;
+ JCOEFPTR inptr;
+ IFAST_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE2]; /* buffers data between passes */
+ SHIFT_TEMPS /* for DESCALE */
+ ISHIFT_TEMPS /* for IDESCALE */
+
+ /* Pass 1: process columns from input, store into work array. */
+
+ inptr = coef_block;
+ quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; ctr--) {
+ /* Due to quantization, we will usually find that many of the input
+ * coefficients are zero, especially the AC terms. We can exploit this
+ * by short-circuiting the IDCT calculation for any column in which all
+ * the AC terms are zero. In that case each output is equal to the
+ * DC coefficient (with scale factor as needed).
+ * With typical images and quantization tables, half or more of the
+ * column DCT calculations can be simplified this way.
+ */
+
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+ inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero */
+ int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+ wsptr[DCTSIZE*2] = dcval;
+ wsptr[DCTSIZE*3] = dcval;
+ wsptr[DCTSIZE*4] = dcval;
+ wsptr[DCTSIZE*5] = dcval;
+ wsptr[DCTSIZE*6] = dcval;
+ wsptr[DCTSIZE*7] = dcval;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ continue;
+ }
+
+ /* Even part */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+ tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ tmp10 = tmp0 + tmp2; /* phase 3 */
+ tmp11 = tmp0 - tmp2;
+
+ tmp13 = tmp1 + tmp3; /* phases 5-3 */
+ tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
+
+ tmp0 = tmp10 + tmp13; /* phase 2 */
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+ tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+
+ z13 = tmp6 + tmp5; /* phase 6 */
+ z10 = tmp6 - tmp5;
+ z11 = tmp4 + tmp7;
+ z12 = tmp4 - tmp7;
+
+ tmp7 = z11 + z13; /* phase 5 */
+ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7; /* phase 2 */
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
+ wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
+ wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
+ wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
+ wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
+ wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
+ wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
+ wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ }
+
+ /* Pass 2: process rows from work array, store into output array. */
+ /* Note that we must descale the results by a factor of 8 == 2**3, */
+ /* and also undo the PASS1_BITS scaling. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < DCTSIZE; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* Rows of zeroes can be exploited in the same way as we did with columns.
+ * However, the column calculation has created many nonzero AC terms, so
+ * the simplification applies less often (typically 5% to 10% of the time).
+ * On machines with very fast multiplication, it's possible that the
+ * test takes more time than it's worth. In that case this section
+ * may be commented out.
+ */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
+ wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+ /* AC terms all zero */
+ JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
+ & RANGE_MASK];
+
+ outptr[0] = dcval;
+ outptr[1] = dcval;
+ outptr[2] = dcval;
+ outptr[3] = dcval;
+ outptr[4] = dcval;
+ outptr[5] = dcval;
+ outptr[6] = dcval;
+ outptr[7] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part */
+
+ tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
+ tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
+
+ tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
+ tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
+ - tmp13;
+
+ tmp0 = tmp10 + tmp13;
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
+ z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
+ z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
+ z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
+
+ tmp7 = z11 + z13; /* phase 5 */
+ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7; /* phase 2 */
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ /* Final output stage: scale down by a factor of 8 and range-limit */
+
+ outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jidctint.c b/test/monniaux/jpeg-6b/jidctint.c
new file mode 100644
index 00000000..a72b3207
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jidctint.c
@@ -0,0 +1,389 @@
+/*
+ * jidctint.c
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a slow-but-accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time). Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on an algorithm described in
+ * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
+ * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
+ * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
+ * The primary algorithm described there uses 11 multiplies and 29 adds.
+ * We use their alternate method with 12 multiplies and 32 adds.
+ * The advantage of this method is that no data path contains more than one
+ * multiplication; this allows a very simple and accurate implementation in
+ * scaled fixed-point arithmetic, with a minimal number of shifts.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * The poop on this scaling stuff is as follows:
+ *
+ * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
+ * larger than the true IDCT outputs. The final outputs are therefore
+ * a factor of N larger than desired; since N=8 this can be cured by
+ * a simple right shift at the end of the algorithm. The advantage of
+ * this arrangement is that we save two multiplications per 1-D IDCT,
+ * because the y0 and y4 inputs need not be divided by sqrt(N).
+ *
+ * We have to do addition and subtraction of the integer inputs, which
+ * is no problem, and multiplication by fractional constants, which is
+ * a problem to do in integer arithmetic. We multiply all the constants
+ * by CONST_SCALE and convert them to integer constants (thus retaining
+ * CONST_BITS bits of precision in the constants). After doing a
+ * multiplication we have to divide the product by CONST_SCALE, with proper
+ * rounding, to produce the correct output. This division can be done
+ * cheaply as a right shift of CONST_BITS bits. We postpone shifting
+ * as long as possible so that partial sums can be added together with
+ * full fractional precision.
+ *
+ * The outputs of the first pass are scaled up by PASS1_BITS bits so that
+ * they are represented to better-than-integral precision. These outputs
+ * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
+ * with the recommended scaling. (To scale up 12-bit sample data further, an
+ * intermediate INT32 array would be needed.)
+ *
+ * To avoid overflow of the 32-bit intermediate results in pass 2, we must
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
+ * shows that the values given below are the most effective.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS 13
+#define PASS1_BITS 2
+#else
+#define CONST_BITS 13
+#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 13
+#define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */
+#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
+#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
+#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
+#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
+#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
+#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
+#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
+#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
+#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
+#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
+#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
+#else
+#define FIX_0_298631336 FIX(0.298631336)
+#define FIX_0_390180644 FIX(0.390180644)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_765366865 FIX(0.765366865)
+#define FIX_0_899976223 FIX(0.899976223)
+#define FIX_1_175875602 FIX(1.175875602)
+#define FIX_1_501321110 FIX(1.501321110)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_1_961570560 FIX(1.961570560)
+#define FIX_2_053119869 FIX(2.053119869)
+#define FIX_2_562915447 FIX(2.562915447)
+#define FIX_3_072711026 FIX(3.072711026)
+#endif
+
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * For 8-bit samples with the recommended scaling, all the variable
+ * and constant values involved are no more than 16 bits wide, so a
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const) MULTIPLY16C16(var,const)
+#else
+#define MULTIPLY(var,const) ((var) * (const))
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce an int result. In this module, both inputs and result
+ * are 16 bits or less, so either int or short multiply will work.
+ */
+
+#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL(void)
+jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ INT32 tmp0, tmp1, tmp2, tmp3;
+ INT32 tmp10, tmp11, tmp12, tmp13;
+ INT32 z1, z2, z3, z4, z5;
+ JCOEFPTR inptr;
+ ISLOW_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE2]; /* buffers data between passes */
+ SHIFT_TEMPS
+
+ /* Pass 1: process columns from input, store into work array. */
+ /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
+ /* furthermore, we scale the results by 2**PASS1_BITS. */
+
+ inptr = coef_block;
+ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; ctr--) {
+ /* Due to quantization, we will usually find that many of the input
+ * coefficients are zero, especially the AC terms. We can exploit this
+ * by short-circuiting the IDCT calculation for any column in which all
+ * the AC terms are zero. In that case each output is equal to the
+ * DC coefficient (with scale factor as needed).
+ * With typical images and quantization tables, half or more of the
+ * column DCT calculations can be simplified this way.
+ */
+
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+ inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero */
+ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+ wsptr[DCTSIZE*2] = dcval;
+ wsptr[DCTSIZE*3] = dcval;
+ wsptr[DCTSIZE*4] = dcval;
+ wsptr[DCTSIZE*5] = dcval;
+ wsptr[DCTSIZE*6] = dcval;
+ wsptr[DCTSIZE*7] = dcval;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ continue;
+ }
+
+ /* Even part: reverse the even part of the forward DCT. */
+ /* The rotator is sqrt(2)*c(-6). */
+
+ z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+
+ tmp0 = (z2 + z3) << CONST_BITS;
+ tmp1 = (z2 - z3) << CONST_BITS;
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ /* Odd part per figure 8; the matrix is unitary and hence its
+ * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
+ */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+ tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+
+ z1 = tmp0 + tmp3;
+ z2 = tmp1 + tmp2;
+ z3 = tmp0 + tmp2;
+ z4 = tmp1 + tmp3;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ tmp0 += z1 + z3;
+ tmp1 += z2 + z4;
+ tmp2 += z2 + z3;
+ tmp3 += z1 + z4;
+
+ /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+
+ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ }
+
+ /* Pass 2: process rows from work array, store into output array. */
+ /* Note that we must descale the results by a factor of 8 == 2**3, */
+ /* and also undo the PASS1_BITS scaling. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < DCTSIZE; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* Rows of zeroes can be exploited in the same way as we did with columns.
+ * However, the column calculation has created many nonzero AC terms, so
+ * the simplification applies less often (typically 5% to 10% of the time).
+ * On machines with very fast multiplication, it's possible that the
+ * test takes more time than it's worth. In that case this section
+ * may be commented out.
+ */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
+ wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+ /* AC terms all zero */
+ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+ & RANGE_MASK];
+
+ outptr[0] = dcval;
+ outptr[1] = dcval;
+ outptr[2] = dcval;
+ outptr[3] = dcval;
+ outptr[4] = dcval;
+ outptr[5] = dcval;
+ outptr[6] = dcval;
+ outptr[7] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part: reverse the even part of the forward DCT. */
+ /* The rotator is sqrt(2)*c(-6). */
+
+ z2 = (INT32) wsptr[2];
+ z3 = (INT32) wsptr[6];
+
+ z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
+ tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ /* Odd part per figure 8; the matrix is unitary and hence its
+ * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
+ */
+
+ tmp0 = (INT32) wsptr[7];
+ tmp1 = (INT32) wsptr[5];
+ tmp2 = (INT32) wsptr[3];
+ tmp3 = (INT32) wsptr[1];
+
+ z1 = tmp0 + tmp3;
+ z2 = tmp1 + tmp2;
+ z3 = tmp0 + tmp2;
+ z4 = tmp1 + tmp3;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ tmp0 += z1 + z3;
+ tmp1 += z2 + z4;
+ tmp2 += z2 + z3;
+ tmp3 += z1 + z4;
+
+ /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+
+ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+#endif /* DCT_ISLOW_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jidctred.c b/test/monniaux/jpeg-6b/jidctred.c
new file mode 100644
index 00000000..421f3c7c
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jidctred.c
@@ -0,0 +1,398 @@
+/*
+ * jidctred.c
+ *
+ * Copyright (C) 1994-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains inverse-DCT routines that produce reduced-size output:
+ * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
+ *
+ * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
+ * algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step
+ * with an 8-to-4 step that produces the four averages of two adjacent outputs
+ * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
+ * These steps were derived by computing the corresponding values at the end
+ * of the normal LL&M code, then simplifying as much as possible.
+ *
+ * 1x1 is trivial: just take the DC coefficient divided by 8.
+ *
+ * See jidctint.c for additional comments.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef IDCT_SCALING_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+ Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling is the same as in jidctint.c. */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS 13
+#define PASS1_BITS 2
+#else
+#define CONST_BITS 13
+#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 13
+#define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */
+#define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */
+#define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */
+#define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */
+#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
+#define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */
+#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
+#define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */
+#define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */
+#define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */
+#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
+#define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */
+#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
+#define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */
+#else
+#define FIX_0_211164243 FIX(0.211164243)
+#define FIX_0_509795579 FIX(0.509795579)
+#define FIX_0_601344887 FIX(0.601344887)
+#define FIX_0_720959822 FIX(0.720959822)
+#define FIX_0_765366865 FIX(0.765366865)
+#define FIX_0_850430095 FIX(0.850430095)
+#define FIX_0_899976223 FIX(0.899976223)
+#define FIX_1_061594337 FIX(1.061594337)
+#define FIX_1_272758580 FIX(1.272758580)
+#define FIX_1_451774981 FIX(1.451774981)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_2_172734803 FIX(2.172734803)
+#define FIX_2_562915447 FIX(2.562915447)
+#define FIX_3_624509785 FIX(3.624509785)
+#endif
+
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * For 8-bit samples with the recommended scaling, all the variable
+ * and constant values involved are no more than 16 bits wide, so a
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const) MULTIPLY16C16(var,const)
+#else
+#define MULTIPLY(var,const) ((var) * (const))
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce an int result. In this module, both inputs and result
+ * are 16 bits or less, so either int or short multiply will work.
+ */
+
+#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 4x4 output block.
+ */
+
+GLOBAL(void)
+jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ INT32 tmp0, tmp2, tmp10, tmp12;
+ INT32 z1, z2, z3, z4;
+ JCOEFPTR inptr;
+ ISLOW_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE*4]; /* buffers data between passes */
+ SHIFT_TEMPS
+
+ /* Pass 1: process columns from input, store into work array. */
+
+ inptr = coef_block;
+ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
+ /* Don't bother to process column 4, because second pass won't use it */
+ if (ctr == DCTSIZE-4)
+ continue;
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
+ inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero; we need not examine term 4 for 4x4 output */
+ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+ wsptr[DCTSIZE*2] = dcval;
+ wsptr[DCTSIZE*3] = dcval;
+
+ continue;
+ }
+
+ /* Even part */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ tmp0 <<= (CONST_BITS+1);
+
+ z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
+
+ tmp10 = tmp0 + tmp2;
+ tmp12 = tmp0 - tmp2;
+
+ /* Odd part */
+
+ z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+ z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+
+ tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
+ + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
+ + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
+ + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
+
+ tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
+ + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
+ + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
+ + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
+
+ /* Final output stage */
+
+ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
+ wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
+ wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
+ wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
+ }
+
+ /* Pass 2: process 4 rows from work array, store into output array. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < 4; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* It's not clear whether a zero row test is worthwhile here ... */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
+ wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+ /* AC terms all zero */
+ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+ & RANGE_MASK];
+
+ outptr[0] = dcval;
+ outptr[1] = dcval;
+ outptr[2] = dcval;
+ outptr[3] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part */
+
+ tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
+
+ tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
+ + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
+
+ tmp10 = tmp0 + tmp2;
+ tmp12 = tmp0 - tmp2;
+
+ /* Odd part */
+
+ z1 = (INT32) wsptr[7];
+ z2 = (INT32) wsptr[5];
+ z3 = (INT32) wsptr[3];
+ z4 = (INT32) wsptr[1];
+
+ tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
+ + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
+ + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
+ + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
+
+ tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
+ + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
+ + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
+ + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
+
+ /* Final output stage */
+
+ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
+ CONST_BITS+PASS1_BITS+3+1)
+ & RANGE_MASK];
+ outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
+ CONST_BITS+PASS1_BITS+3+1)
+ & RANGE_MASK];
+ outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
+ CONST_BITS+PASS1_BITS+3+1)
+ & RANGE_MASK];
+ outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
+ CONST_BITS+PASS1_BITS+3+1)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 2x2 output block.
+ */
+
+GLOBAL(void)
+jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ INT32 tmp0, tmp10, z1;
+ JCOEFPTR inptr;
+ ISLOW_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE*2]; /* buffers data between passes */
+ SHIFT_TEMPS
+
+ /* Pass 1: process columns from input, store into work array. */
+
+ inptr = coef_block;
+ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
+ /* Don't bother to process columns 2,4,6 */
+ if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
+ continue;
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
+ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+
+ continue;
+ }
+
+ /* Even part */
+
+ z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ tmp10 = z1 << (CONST_BITS+2);
+
+ /* Odd part */
+
+ z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+ tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
+ z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
+ z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
+ z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+ tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
+
+ /* Final output stage */
+
+ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
+ wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
+ }
+
+ /* Pass 2: process 2 rows from work array, store into output array. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < 2; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* It's not clear whether a zero row test is worthwhile here ... */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
+ /* AC terms all zero */
+ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+ & RANGE_MASK];
+
+ outptr[0] = dcval;
+ outptr[1] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part */
+
+ tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
+
+ /* Odd part */
+
+ tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
+ + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
+ + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
+ + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
+
+ /* Final output stage */
+
+ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
+ CONST_BITS+PASS1_BITS+3+2)
+ & RANGE_MASK];
+ outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
+ CONST_BITS+PASS1_BITS+3+2)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 1x1 output block.
+ */
+
+GLOBAL(void)
+jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ int dcval;
+ ISLOW_MULT_TYPE * quantptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ SHIFT_TEMPS
+
+ /* We hardly need an inverse DCT routine for this: just take the
+ * average pixel value, which is one-eighth of the DC coefficient.
+ */
+ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
+ dcval = (int) DESCALE((INT32) dcval, 3);
+
+ output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
+}
+
+#endif /* IDCT_SCALING_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jinclude.h b/test/monniaux/jpeg-6b/jinclude.h
new file mode 100644
index 00000000..0a4f1514
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jinclude.h
@@ -0,0 +1,91 @@
+/*
+ * jinclude.h
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file exists to provide a single place to fix any problems with
+ * including the wrong system include files. (Common problems are taken
+ * care of by the standard jconfig symbols, but on really weird systems
+ * you may have to edit this file.)
+ *
+ * NOTE: this file is NOT intended to be included by applications using the
+ * JPEG library. Most applications need only include jpeglib.h.
+ */
+
+
+/* Include auto-config file to find out which system include files we need. */
+
+#include "jconfig.h" /* auto configuration options */
+#define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */
+
+/*
+ * We need the NULL macro and size_t typedef.
+ * On an ANSI-conforming system it is sufficient to include <stddef.h>.
+ * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
+ * pull in <sys/types.h> as well.
+ * Note that the core JPEG library does not require <stdio.h>;
+ * only the default error handler and data source/destination modules do.
+ * But we must pull it in because of the references to FILE in jpeglib.h.
+ * You can remove those references if you want to compile without <stdio.h>.
+ */
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef NEED_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#include <stdio.h>
+
+/*
+ * We need memory copying and zeroing functions, plus strncpy().
+ * ANSI and System V implementations declare these in <string.h>.
+ * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
+ * Some systems may declare memset and memcpy in <memory.h>.
+ *
+ * NOTE: we assume the size parameters to these functions are of type size_t.
+ * Change the casts in these macros if not!
+ */
+
+#ifdef NEED_BSD_STRINGS
+
+#include <strings.h>
+#define MEMZERO(target,size) bzero((void *)(target), (size_t)(size))
+#define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size))
+
+#else /* not BSD, assume ANSI/SysV string lib */
+
+#include <string.h>
+#define MEMZERO(target,size) memset((void *)(target), 0, (size_t)(size))
+#define MEMCOPY(dest,src,size) memcpy((void *)(dest), (const void *)(src), (size_t)(size))
+
+#endif
+
+/*
+ * In ANSI C, and indeed any rational implementation, size_t is also the
+ * type returned by sizeof(). However, it seems there are some irrational
+ * implementations out there, in which sizeof() returns an int even though
+ * size_t is defined as long or unsigned long. To ensure consistent results
+ * we always use this SIZEOF() macro in place of using sizeof() directly.
+ */
+
+#define SIZEOF(object) ((size_t) sizeof(object))
+
+/*
+ * The modules that use fread() and fwrite() always invoke them through
+ * these macros. On some systems you may need to twiddle the argument casts.
+ * CAUTION: argument order is different from underlying functions!
+ */
+
+#define JFREAD(file,buf,sizeofbuf) \
+ ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
+#define JFWRITE(file,buf,sizeofbuf) \
+ ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
diff --git a/test/monniaux/jpeg-6b/jmemansi.c b/test/monniaux/jpeg-6b/jmemansi.c
new file mode 100644
index 00000000..2d93e496
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemansi.c
@@ -0,0 +1,167 @@
+/*
+ * jmemansi.c
+ *
+ * Copyright (C) 1992-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides a simple generic implementation of the system-
+ * dependent portion of the JPEG memory manager. This implementation
+ * assumes that you have the ANSI-standard library routine tmpfile().
+ * Also, the problem of determining the amount of memory available
+ * is shoved onto the user.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+#endif
+
+#ifndef SEEK_SET /* pre-ANSI systems may not define this; */
+#define SEEK_SET 0 /* if not, assume 0 is correct */
+#endif
+
+
+/*
+ * Memory allocation and freeing are controlled by the regular library
+ * routines malloc() and free().
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ * It's impossible to do this in a portable way; our current solution is
+ * to make the user tell us (with a default value set at compile time).
+ * If you can actually get the available space, it's a good idea to subtract
+ * a slop factor of 5% or so.
+ */
+
+#ifndef DEFAULT_MAX_MEM /* so can override from makefile */
+#define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
+#endif
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+ long max_bytes_needed, long already_allocated)
+{
+ return cinfo->mem->max_memory_to_use - already_allocated;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Backing store objects are only used when the value returned by
+ * jpeg_mem_available is less than the total space needed. You can dispense
+ * with these routines if you have plenty of virtual memory; see jmemnobs.c.
+ */
+
+
+METHODDEF(void)
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (fseek(info->temp_file, file_offset, SEEK_SET))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ if (JFREAD(info->temp_file, buffer_address, byte_count)
+ != (size_t) byte_count)
+ ERREXIT(cinfo, JERR_TFILE_READ);
+}
+
+
+METHODDEF(void)
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (fseek(info->temp_file, file_offset, SEEK_SET))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ if (JFWRITE(info->temp_file, buffer_address, byte_count)
+ != (size_t) byte_count)
+ ERREXIT(cinfo, JERR_TFILE_WRITE);
+}
+
+
+METHODDEF(void)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ fclose(info->temp_file);
+ /* Since this implementation uses tmpfile() to create the file,
+ * no explicit file deletion is needed.
+ */
+}
+
+
+/*
+ * Initial opening of a backing-store object.
+ *
+ * This version uses tmpfile(), which constructs a suitable file name
+ * behind the scenes. We don't have to use info->temp_name[] at all;
+ * indeed, we can't even find out the actual name of the temp file.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ if ((info->temp_file = tmpfile()) == NULL)
+ ERREXITS(cinfo, JERR_TFILE_CREATE, "");
+ info->read_backing_store = read_backing_store;
+ info->write_backing_store = write_backing_store;
+ info->close_backing_store = close_backing_store;
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+ return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+ /* no work */
+}
diff --git a/test/monniaux/jpeg-6b/jmemdos.c b/test/monniaux/jpeg-6b/jmemdos.c
new file mode 100644
index 00000000..60b45c69
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemdos.c
@@ -0,0 +1,638 @@
+/*
+ * jmemdos.c
+ *
+ * Copyright (C) 1992-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides an MS-DOS-compatible implementation of the system-
+ * dependent portion of the JPEG memory manager. Temporary data can be
+ * stored in extended or expanded memory as well as in regular DOS files.
+ *
+ * If you use this file, you must be sure that NEED_FAR_POINTERS is defined
+ * if you compile in a small-data memory model; it should NOT be defined if
+ * you use a large-data memory model. This file is not recommended if you
+ * are using a flat-memory-space 386 environment such as DJGCC or Watcom C.
+ * Also, this code will NOT work if struct fields are aligned on greater than
+ * 2-byte boundaries.
+ *
+ * Based on code contributed by Ge' Weijers.
+ */
+
+/*
+ * If you have both extended and expanded memory, you may want to change the
+ * order in which they are tried in jopen_backing_store. On a 286 machine
+ * expanded memory is usually faster, since extended memory access involves
+ * an expensive protected-mode-and-back switch. On 386 and better, extended
+ * memory is usually faster. As distributed, the code tries extended memory
+ * first (what? not everyone has a 386? :-).
+ *
+ * You can disable use of extended/expanded memory entirely by altering these
+ * definitions or overriding them from the Makefile (eg, -DEMS_SUPPORTED=0).
+ */
+
+#ifndef XMS_SUPPORTED
+#define XMS_SUPPORTED 1
+#endif
+#ifndef EMS_SUPPORTED
+#define EMS_SUPPORTED 1
+#endif
+
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare these */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+extern char * getenv JPP((const char * name));
+#endif
+
+#ifdef NEED_FAR_POINTERS
+
+#ifdef __TURBOC__
+/* These definitions work for Borland C (Turbo C) */
+#include <alloc.h> /* need farmalloc(), farfree() */
+#define far_malloc(x) farmalloc(x)
+#define far_free(x) farfree(x)
+#else
+/* These definitions work for Microsoft C and compatible compilers */
+#include <malloc.h> /* need _fmalloc(), _ffree() */
+#define far_malloc(x) _fmalloc(x)
+#define far_free(x) _ffree(x)
+#endif
+
+#else /* not NEED_FAR_POINTERS */
+
+#define far_malloc(x) malloc(x)
+#define far_free(x) free(x)
+
+#endif /* NEED_FAR_POINTERS */
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#else
+#define READ_BINARY "rb"
+#endif
+
+#ifndef USE_MSDOS_MEMMGR /* make sure user got configuration right */
+ You forgot to define USE_MSDOS_MEMMGR in jconfig.h. /* deliberate syntax error */
+#endif
+
+#if MAX_ALLOC_CHUNK >= 65535L /* make sure jconfig.h got this right */
+ MAX_ALLOC_CHUNK should be less than 64K. /* deliberate syntax error */
+#endif
+
+
+/*
+ * Declarations for assembly-language support routines (see jmemdosa.asm).
+ *
+ * The functions are declared "far" as are all their pointer arguments;
+ * this ensures the assembly source code will work regardless of the
+ * compiler memory model. We assume "short" is 16 bits, "long" is 32.
+ */
+
+typedef void far * XMSDRIVER; /* actually a pointer to code */
+typedef struct { /* registers for calling XMS driver */
+ unsigned short ax, dx, bx;
+ void far * ds_si;
+ } XMScontext;
+typedef struct { /* registers for calling EMS driver */
+ unsigned short ax, dx, bx;
+ void far * ds_si;
+ } EMScontext;
+
+extern short far jdos_open JPP((short far * handle, char far * filename));
+extern short far jdos_close JPP((short handle));
+extern short far jdos_seek JPP((short handle, long offset));
+extern short far jdos_read JPP((short handle, void far * buffer,
+ unsigned short count));
+extern short far jdos_write JPP((short handle, void far * buffer,
+ unsigned short count));
+extern void far jxms_getdriver JPP((XMSDRIVER far *));
+extern void far jxms_calldriver JPP((XMSDRIVER, XMScontext far *));
+extern short far jems_available JPP((void));
+extern void far jems_calldriver JPP((EMScontext far *));
+
+
+/*
+ * Selection of a file name for a temporary file.
+ * This is highly system-dependent, and you may want to customize it.
+ */
+
+static int next_file_num; /* to distinguish among several temp files */
+
+LOCAL(void)
+select_file_name (char * fname)
+{
+ const char * env;
+ char * ptr;
+ FILE * tfile;
+
+ /* Keep generating file names till we find one that's not in use */
+ for (;;) {
+ /* Get temp directory name from environment TMP or TEMP variable;
+ * if none, use "."
+ */
+ if ((env = (const char *) getenv("TMP")) == NULL)
+ if ((env = (const char *) getenv("TEMP")) == NULL)
+ env = ".";
+ if (*env == '\0') /* null string means "." */
+ env = ".";
+ ptr = fname; /* copy name to fname */
+ while (*env != '\0')
+ *ptr++ = *env++;
+ if (ptr[-1] != '\\' && ptr[-1] != '/')
+ *ptr++ = '\\'; /* append backslash if not in env variable */
+ /* Append a suitable file name */
+ next_file_num++; /* advance counter */
+ sprintf(ptr, "JPG%03d.TMP", next_file_num);
+ /* Probe to see if file name is already in use */
+ if ((tfile = fopen(fname, READ_BINARY)) == NULL)
+ break;
+ fclose(tfile); /* oops, it's there; close tfile & try again */
+ }
+}
+
+
+/*
+ * Near-memory allocation and freeing are controlled by the regular library
+ * routines malloc() and free().
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * "Large" objects are allocated in far memory, if possible
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void FAR *) far_malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+ far_free(object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ * It's impossible to do this in a portable way; our current solution is
+ * to make the user tell us (with a default value set at compile time).
+ * If you can actually get the available space, it's a good idea to subtract
+ * a slop factor of 5% or so.
+ */
+
+#ifndef DEFAULT_MAX_MEM /* so can override from makefile */
+#define DEFAULT_MAX_MEM 300000L /* for total usage about 450K */
+#endif
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+ long max_bytes_needed, long already_allocated)
+{
+ return cinfo->mem->max_memory_to_use - already_allocated;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Backing store objects are only used when the value returned by
+ * jpeg_mem_available is less than the total space needed. You can dispense
+ * with these routines if you have plenty of virtual memory; see jmemnobs.c.
+ */
+
+/*
+ * For MS-DOS we support three types of backing storage:
+ * 1. Conventional DOS files. We access these by direct DOS calls rather
+ * than via the stdio package. This provides a bit better performance,
+ * but the real reason is that the buffers to be read or written are FAR.
+ * The stdio library for small-data memory models can't cope with that.
+ * 2. Extended memory, accessed per the XMS V2.0 specification.
+ * 3. Expanded memory, accessed per the LIM/EMS 4.0 specification.
+ * You'll need copies of those specs to make sense of the related code.
+ * The specs are available by Internet FTP from the SIMTEL archives
+ * (oak.oakland.edu and its various mirror sites). See files
+ * pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip.
+ */
+
+
+/*
+ * Access methods for a DOS file.
+ */
+
+
+METHODDEF(void)
+read_file_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (jdos_seek(info->handle.file_handle, file_offset))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
+ if (byte_count > 65535L) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
+ if (jdos_read(info->handle.file_handle, buffer_address,
+ (unsigned short) byte_count))
+ ERREXIT(cinfo, JERR_TFILE_READ);
+}
+
+
+METHODDEF(void)
+write_file_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (jdos_seek(info->handle.file_handle, file_offset))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
+ if (byte_count > 65535L) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
+ if (jdos_write(info->handle.file_handle, buffer_address,
+ (unsigned short) byte_count))
+ ERREXIT(cinfo, JERR_TFILE_WRITE);
+}
+
+
+METHODDEF(void)
+close_file_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ jdos_close(info->handle.file_handle); /* close the file */
+ remove(info->temp_name); /* delete the file */
+/* If your system doesn't have remove(), try unlink() instead.
+ * remove() is the ANSI-standard name for this function, but
+ * unlink() was more common in pre-ANSI systems.
+ */
+ TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
+}
+
+
+LOCAL(boolean)
+open_file_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ short handle;
+
+ select_file_name(info->temp_name);
+ if (jdos_open((short far *) & handle, (char far *) info->temp_name)) {
+ /* might as well exit since jpeg_open_backing_store will fail anyway */
+ ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
+ return FALSE;
+ }
+ info->handle.file_handle = handle;
+ info->read_backing_store = read_file_store;
+ info->write_backing_store = write_file_store;
+ info->close_backing_store = close_file_store;
+ TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
+ return TRUE; /* succeeded */
+}
+
+
+/*
+ * Access methods for extended memory.
+ */
+
+#if XMS_SUPPORTED
+
+static XMSDRIVER xms_driver; /* saved address of XMS driver */
+
+typedef union { /* either long offset or real-mode pointer */
+ long offset;
+ void far * ptr;
+ } XMSPTR;
+
+typedef struct { /* XMS move specification structure */
+ long length;
+ XMSH src_handle;
+ XMSPTR src;
+ XMSH dst_handle;
+ XMSPTR dst;
+ } XMSspec;
+
+#define ODD(X) (((X) & 1L) != 0)
+
+
+METHODDEF(void)
+read_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ XMScontext ctx;
+ XMSspec spec;
+ char endbuffer[2];
+
+ /* The XMS driver can't cope with an odd length, so handle the last byte
+ * specially if byte_count is odd. We don't expect this to be common.
+ */
+
+ spec.length = byte_count & (~ 1L);
+ spec.src_handle = info->handle.xms_handle;
+ spec.src.offset = file_offset;
+ spec.dst_handle = 0;
+ spec.dst.ptr = buffer_address;
+
+ ctx.ds_si = (void far *) & spec;
+ ctx.ax = 0x0b00; /* EMB move */
+ jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
+ if (ctx.ax != 1)
+ ERREXIT(cinfo, JERR_XMS_READ);
+
+ if (ODD(byte_count)) {
+ read_xms_store(cinfo, info, (void FAR *) endbuffer,
+ file_offset + byte_count - 1L, 2L);
+ ((char FAR *) buffer_address)[byte_count - 1L] = endbuffer[0];
+ }
+}
+
+
+METHODDEF(void)
+write_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ XMScontext ctx;
+ XMSspec spec;
+ char endbuffer[2];
+
+ /* The XMS driver can't cope with an odd length, so handle the last byte
+ * specially if byte_count is odd. We don't expect this to be common.
+ */
+
+ spec.length = byte_count & (~ 1L);
+ spec.src_handle = 0;
+ spec.src.ptr = buffer_address;
+ spec.dst_handle = info->handle.xms_handle;
+ spec.dst.offset = file_offset;
+
+ ctx.ds_si = (void far *) & spec;
+ ctx.ax = 0x0b00; /* EMB move */
+ jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
+ if (ctx.ax != 1)
+ ERREXIT(cinfo, JERR_XMS_WRITE);
+
+ if (ODD(byte_count)) {
+ read_xms_store(cinfo, info, (void FAR *) endbuffer,
+ file_offset + byte_count - 1L, 2L);
+ endbuffer[0] = ((char FAR *) buffer_address)[byte_count - 1L];
+ write_xms_store(cinfo, info, (void FAR *) endbuffer,
+ file_offset + byte_count - 1L, 2L);
+ }
+}
+
+
+METHODDEF(void)
+close_xms_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ XMScontext ctx;
+
+ ctx.dx = info->handle.xms_handle;
+ ctx.ax = 0x0a00;
+ jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
+ TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info->handle.xms_handle);
+ /* we ignore any error return from the driver */
+}
+
+
+LOCAL(boolean)
+open_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ XMScontext ctx;
+
+ /* Get address of XMS driver */
+ jxms_getdriver((XMSDRIVER far *) & xms_driver);
+ if (xms_driver == NULL)
+ return FALSE; /* no driver to be had */
+
+ /* Get version number, must be >= 2.00 */
+ ctx.ax = 0x0000;
+ jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
+ if (ctx.ax < (unsigned short) 0x0200)
+ return FALSE;
+
+ /* Try to get space (expressed in kilobytes) */
+ ctx.dx = (unsigned short) ((total_bytes_needed + 1023L) >> 10);
+ ctx.ax = 0x0900;
+ jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
+ if (ctx.ax != 1)
+ return FALSE;
+
+ /* Succeeded, save the handle and away we go */
+ info->handle.xms_handle = ctx.dx;
+ info->read_backing_store = read_xms_store;
+ info->write_backing_store = write_xms_store;
+ info->close_backing_store = close_xms_store;
+ TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx);
+ return TRUE; /* succeeded */
+}
+
+#endif /* XMS_SUPPORTED */
+
+
+/*
+ * Access methods for expanded memory.
+ */
+
+#if EMS_SUPPORTED
+
+/* The EMS move specification structure requires word and long fields aligned
+ * at odd byte boundaries. Some compilers will align struct fields at even
+ * byte boundaries. While it's usually possible to force byte alignment,
+ * that causes an overall performance penalty and may pose problems in merging
+ * JPEG into a larger application. Instead we accept some rather dirty code
+ * here. Note this code would fail if the hardware did not allow odd-byte
+ * word & long accesses, but all 80x86 CPUs do.
+ */
+
+typedef void far * EMSPTR;
+
+typedef union { /* EMS move specification structure */
+ long length; /* It's easy to access first 4 bytes */
+ char bytes[18]; /* Misaligned fields in here! */
+ } EMSspec;
+
+/* Macros for accessing misaligned fields */
+#define FIELD_AT(spec,offset,type) (*((type *) &(spec.bytes[offset])))
+#define SRC_TYPE(spec) FIELD_AT(spec,4,char)
+#define SRC_HANDLE(spec) FIELD_AT(spec,5,EMSH)
+#define SRC_OFFSET(spec) FIELD_AT(spec,7,unsigned short)
+#define SRC_PAGE(spec) FIELD_AT(spec,9,unsigned short)
+#define SRC_PTR(spec) FIELD_AT(spec,7,EMSPTR)
+#define DST_TYPE(spec) FIELD_AT(spec,11,char)
+#define DST_HANDLE(spec) FIELD_AT(spec,12,EMSH)
+#define DST_OFFSET(spec) FIELD_AT(spec,14,unsigned short)
+#define DST_PAGE(spec) FIELD_AT(spec,16,unsigned short)
+#define DST_PTR(spec) FIELD_AT(spec,14,EMSPTR)
+
+#define EMSPAGESIZE 16384L /* gospel, see the EMS specs */
+
+#define HIBYTE(W) (((W) >> 8) & 0xFF)
+#define LOBYTE(W) ((W) & 0xFF)
+
+
+METHODDEF(void)
+read_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ EMScontext ctx;
+ EMSspec spec;
+
+ spec.length = byte_count;
+ SRC_TYPE(spec) = 1;
+ SRC_HANDLE(spec) = info->handle.ems_handle;
+ SRC_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE);
+ SRC_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
+ DST_TYPE(spec) = 0;
+ DST_HANDLE(spec) = 0;
+ DST_PTR(spec) = buffer_address;
+
+ ctx.ds_si = (void far *) & spec;
+ ctx.ax = 0x5700; /* move memory region */
+ jems_calldriver((EMScontext far *) & ctx);
+ if (HIBYTE(ctx.ax) != 0)
+ ERREXIT(cinfo, JERR_EMS_READ);
+}
+
+
+METHODDEF(void)
+write_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ EMScontext ctx;
+ EMSspec spec;
+
+ spec.length = byte_count;
+ SRC_TYPE(spec) = 0;
+ SRC_HANDLE(spec) = 0;
+ SRC_PTR(spec) = buffer_address;
+ DST_TYPE(spec) = 1;
+ DST_HANDLE(spec) = info->handle.ems_handle;
+ DST_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE);
+ DST_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
+
+ ctx.ds_si = (void far *) & spec;
+ ctx.ax = 0x5700; /* move memory region */
+ jems_calldriver((EMScontext far *) & ctx);
+ if (HIBYTE(ctx.ax) != 0)
+ ERREXIT(cinfo, JERR_EMS_WRITE);
+}
+
+
+METHODDEF(void)
+close_ems_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ EMScontext ctx;
+
+ ctx.ax = 0x4500;
+ ctx.dx = info->handle.ems_handle;
+ jems_calldriver((EMScontext far *) & ctx);
+ TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info->handle.ems_handle);
+ /* we ignore any error return from the driver */
+}
+
+
+LOCAL(boolean)
+open_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ EMScontext ctx;
+
+ /* Is EMS driver there? */
+ if (! jems_available())
+ return FALSE;
+
+ /* Get status, make sure EMS is OK */
+ ctx.ax = 0x4000;
+ jems_calldriver((EMScontext far *) & ctx);
+ if (HIBYTE(ctx.ax) != 0)
+ return FALSE;
+
+ /* Get version, must be >= 4.0 */
+ ctx.ax = 0x4600;
+ jems_calldriver((EMScontext far *) & ctx);
+ if (HIBYTE(ctx.ax) != 0 || LOBYTE(ctx.ax) < 0x40)
+ return FALSE;
+
+ /* Try to allocate requested space */
+ ctx.ax = 0x4300;
+ ctx.bx = (unsigned short) ((total_bytes_needed + EMSPAGESIZE-1L) / EMSPAGESIZE);
+ jems_calldriver((EMScontext far *) & ctx);
+ if (HIBYTE(ctx.ax) != 0)
+ return FALSE;
+
+ /* Succeeded, save the handle and away we go */
+ info->handle.ems_handle = ctx.dx;
+ info->read_backing_store = read_ems_store;
+ info->write_backing_store = write_ems_store;
+ info->close_backing_store = close_ems_store;
+ TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx);
+ return TRUE; /* succeeded */
+}
+
+#endif /* EMS_SUPPORTED */
+
+
+/*
+ * Initial opening of a backing-store object.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ /* Try extended memory, then expanded memory, then regular file. */
+#if XMS_SUPPORTED
+ if (open_xms_store(cinfo, info, total_bytes_needed))
+ return;
+#endif
+#if EMS_SUPPORTED
+ if (open_ems_store(cinfo, info, total_bytes_needed))
+ return;
+#endif
+ if (open_file_store(cinfo, info, total_bytes_needed))
+ return;
+ ERREXITS(cinfo, JERR_TFILE_CREATE, "");
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+ next_file_num = 0; /* initialize temp file name generator */
+ return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+ /* Microsoft C, at least in v6.00A, will not successfully reclaim freed
+ * blocks of size > 32Kbytes unless we give it a kick in the rear, like so:
+ */
+#ifdef NEED_FHEAPMIN
+ _fheapmin();
+#endif
+}
diff --git a/test/monniaux/jpeg-6b/jmemdosa.asm b/test/monniaux/jpeg-6b/jmemdosa.asm
new file mode 100644
index 00000000..ecd43729
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemdosa.asm
@@ -0,0 +1,379 @@
+;
+; jmemdosa.asm
+;
+; Copyright (C) 1992, Thomas G. Lane.
+; This file is part of the Independent JPEG Group's software.
+; For conditions of distribution and use, see the accompanying README file.
+;
+; This file contains low-level interface routines to support the MS-DOS
+; backing store manager (jmemdos.c). Routines are provided to access disk
+; files through direct DOS calls, and to access XMS and EMS drivers.
+;
+; This file should assemble with Microsoft's MASM or any compatible
+; assembler (including Borland's Turbo Assembler). If you haven't got
+; a compatible assembler, better fall back to jmemansi.c or jmemname.c.
+;
+; To minimize dependence on the C compiler's register usage conventions,
+; we save and restore all 8086 registers, even though most compilers only
+; require SI,DI,DS to be preserved. Also, we use only 16-bit-wide return
+; values, which everybody returns in AX.
+;
+; Based on code contributed by Ge' Weijers.
+;
+
+JMEMDOSA_TXT segment byte public 'CODE'
+
+ assume cs:JMEMDOSA_TXT
+
+ public _jdos_open
+ public _jdos_close
+ public _jdos_seek
+ public _jdos_read
+ public _jdos_write
+ public _jxms_getdriver
+ public _jxms_calldriver
+ public _jems_available
+ public _jems_calldriver
+
+;
+; short far jdos_open (short far * handle, char far * filename)
+;
+; Create and open a temporary file
+;
+_jdos_open proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov cx,0 ; normal file attributes
+ lds dx,dword ptr [bp+10] ; get filename pointer
+ mov ah,3ch ; create file
+ int 21h
+ jc open_err ; if failed, return error code
+ lds bx,dword ptr [bp+6] ; get handle pointer
+ mov word ptr [bx],ax ; save the handle
+ xor ax,ax ; return zero for OK
+open_err: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jdos_open endp
+
+
+;
+; short far jdos_close (short handle)
+;
+; Close the file handle
+;
+_jdos_close proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov bx,word ptr [bp+6] ; file handle
+ mov ah,3eh ; close file
+ int 21h
+ jc close_err ; if failed, return error code
+ xor ax,ax ; return zero for OK
+close_err: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jdos_close endp
+
+
+;
+; short far jdos_seek (short handle, long offset)
+;
+; Set file position
+;
+_jdos_seek proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov bx,word ptr [bp+6] ; file handle
+ mov dx,word ptr [bp+8] ; LS offset
+ mov cx,word ptr [bp+10] ; MS offset
+ mov ax,4200h ; absolute seek
+ int 21h
+ jc seek_err ; if failed, return error code
+ xor ax,ax ; return zero for OK
+seek_err: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jdos_seek endp
+
+
+;
+; short far jdos_read (short handle, void far * buffer, unsigned short count)
+;
+; Read from file
+;
+_jdos_read proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov bx,word ptr [bp+6] ; file handle
+ lds dx,dword ptr [bp+8] ; buffer address
+ mov cx,word ptr [bp+12] ; number of bytes
+ mov ah,3fh ; read file
+ int 21h
+ jc read_err ; if failed, return error code
+ cmp ax,word ptr [bp+12] ; make sure all bytes were read
+ je read_ok
+ mov ax,1 ; else return 1 for not OK
+ jmp short read_err
+read_ok: xor ax,ax ; return zero for OK
+read_err: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jdos_read endp
+
+
+;
+; short far jdos_write (short handle, void far * buffer, unsigned short count)
+;
+; Write to file
+;
+_jdos_write proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov bx,word ptr [bp+6] ; file handle
+ lds dx,dword ptr [bp+8] ; buffer address
+ mov cx,word ptr [bp+12] ; number of bytes
+ mov ah,40h ; write file
+ int 21h
+ jc write_err ; if failed, return error code
+ cmp ax,word ptr [bp+12] ; make sure all bytes written
+ je write_ok
+ mov ax,1 ; else return 1 for not OK
+ jmp short write_err
+write_ok: xor ax,ax ; return zero for OK
+write_err: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jdos_write endp
+
+
+;
+; void far jxms_getdriver (XMSDRIVER far *)
+;
+; Get the address of the XMS driver, or NULL if not available
+;
+_jxms_getdriver proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov ax,4300h ; call multiplex interrupt with
+ int 2fh ; a magic cookie, hex 4300
+ cmp al,80h ; AL should contain hex 80
+ je xmsavail
+ xor dx,dx ; no XMS driver available
+ xor ax,ax ; return a nil pointer
+ jmp short xmsavail_done
+xmsavail: mov ax,4310h ; fetch driver address with
+ int 2fh ; another magic cookie
+ mov dx,es ; copy address to dx:ax
+ mov ax,bx
+xmsavail_done: les bx,dword ptr [bp+6] ; get pointer to return value
+ mov word ptr es:[bx],ax
+ mov word ptr es:[bx+2],dx
+ pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jxms_getdriver endp
+
+
+;
+; void far jxms_calldriver (XMSDRIVER, XMScontext far *)
+;
+; The XMScontext structure contains values for the AX,DX,BX,SI,DS registers.
+; These are loaded, the XMS call is performed, and the new values of the
+; AX,DX,BX registers are written back to the context structure.
+;
+_jxms_calldriver proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ les bx,dword ptr [bp+10] ; get XMScontext pointer
+ mov ax,word ptr es:[bx] ; load registers
+ mov dx,word ptr es:[bx+2]
+ mov si,word ptr es:[bx+6]
+ mov ds,word ptr es:[bx+8]
+ mov bx,word ptr es:[bx+4]
+ call dword ptr [bp+6] ; call the driver
+ mov cx,bx ; save returned BX for a sec
+ les bx,dword ptr [bp+10] ; get XMScontext pointer
+ mov word ptr es:[bx],ax ; put back ax,dx,bx
+ mov word ptr es:[bx+2],dx
+ mov word ptr es:[bx+4],cx
+ pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jxms_calldriver endp
+
+
+;
+; short far jems_available (void)
+;
+; Have we got an EMS driver? (this comes straight from the EMS 4.0 specs)
+;
+_jems_available proc far
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ mov ax,3567h ; get interrupt vector 67h
+ int 21h
+ push cs
+ pop ds
+ mov di,000ah ; check offs 10 in returned seg
+ lea si,ASCII_device_name ; against literal string
+ mov cx,8
+ cld
+ repe cmpsb
+ jne no_ems
+ mov ax,1 ; match, it's there
+ jmp short avail_done
+no_ems: xor ax,ax ; it's not there
+avail_done: pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ ret
+
+ASCII_device_name db "EMMXXXX0"
+
+_jems_available endp
+
+
+;
+; void far jems_calldriver (EMScontext far *)
+;
+; The EMScontext structure contains values for the AX,DX,BX,SI,DS registers.
+; These are loaded, the EMS trap is performed, and the new values of the
+; AX,DX,BX registers are written back to the context structure.
+;
+_jems_calldriver proc far
+ push bp ; linkage
+ mov bp,sp
+ push si ; save all registers for safety
+ push di
+ push bx
+ push cx
+ push dx
+ push es
+ push ds
+ les bx,dword ptr [bp+6] ; get EMScontext pointer
+ mov ax,word ptr es:[bx] ; load registers
+ mov dx,word ptr es:[bx+2]
+ mov si,word ptr es:[bx+6]
+ mov ds,word ptr es:[bx+8]
+ mov bx,word ptr es:[bx+4]
+ int 67h ; call the EMS driver
+ mov cx,bx ; save returned BX for a sec
+ les bx,dword ptr [bp+6] ; get EMScontext pointer
+ mov word ptr es:[bx],ax ; put back ax,dx,bx
+ mov word ptr es:[bx+2],dx
+ mov word ptr es:[bx+4],cx
+ pop ds ; restore registers and exit
+ pop es
+ pop dx
+ pop cx
+ pop bx
+ pop di
+ pop si
+ pop bp
+ ret
+_jems_calldriver endp
+
+JMEMDOSA_TXT ends
+
+ end
diff --git a/test/monniaux/jpeg-6b/jmemmac.c b/test/monniaux/jpeg-6b/jmemmac.c
new file mode 100644
index 00000000..106f9bea
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemmac.c
@@ -0,0 +1,289 @@
+/*
+ * jmemmac.c
+ *
+ * Copyright (C) 1992-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * jmemmac.c provides an Apple Macintosh implementation of the system-
+ * dependent portion of the JPEG memory manager.
+ *
+ * If you use jmemmac.c, then you must define USE_MAC_MEMMGR in the
+ * JPEG_INTERNALS part of jconfig.h.
+ *
+ * jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr
+ * instead of malloc and free. It accurately determines the amount of
+ * memory available by using CompactMem. Notice that if left to its
+ * own devices, this code can chew up all available space in the
+ * application's zone, with the exception of the rather small "slop"
+ * factor computed in jpeg_mem_available(). The application can ensure
+ * that more space is left over by reducing max_memory_to_use.
+ *
+ * Large images are swapped to disk using temporary files and System 7.0+'s
+ * temporary folder functionality.
+ *
+ * Note that jmemmac.c depends on two features of MacOS that were first
+ * introduced in System 7: FindFolder and the FSSpec-based calls.
+ * If your application uses jmemmac.c and is run under System 6 or earlier,
+ * and the jpeg library decides it needs a temporary file, it will abort,
+ * printing error messages about requiring System 7. (If no temporary files
+ * are created, it will run fine.)
+ *
+ * If you want to use jmemmac.c in an application that might be used with
+ * System 6 or earlier, then you should remove dependencies on FindFolder
+ * and the FSSpec calls. You will need to replace FindFolder with some
+ * other mechanism for finding a place to put temporary files, and you
+ * should replace the FSSpec calls with their HFS equivalents:
+ *
+ * FSpDelete -> HDelete
+ * FSpGetFInfo -> HGetFInfo
+ * FSpCreate -> HCreate
+ * FSpOpenDF -> HOpen *** Note: not HOpenDF ***
+ * FSMakeFSSpec -> (fill in spec by hand.)
+ *
+ * (Use HOpen instead of HOpenDF. HOpen is just a glue-interface to PBHOpen,
+ * which is on all HFS macs. HOpenDF is a System 7 addition which avoids the
+ * ages-old problem of names starting with a period.)
+ *
+ * Contributed by Sam Bushell (jsam@iagu.on.net) and
+ * Dan Gildor (gyld@in-touch.com).
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef USE_MAC_MEMMGR /* make sure user got configuration right */
+ You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */
+#endif
+
+#include <Memory.h> /* we use the MacOS memory manager */
+#include <Files.h> /* we use the MacOS File stuff */
+#include <Folders.h> /* we use the MacOS HFS stuff */
+#include <Script.h> /* for smSystemScript */
+#include <Gestalt.h> /* we use Gestalt to test for specific functionality */
+
+#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
+#define TEMP_FILE_NAME "JPG%03d.TMP"
+#endif
+
+static int next_file_num; /* to distinguish among several temp files */
+
+
+/*
+ * Memory allocation and freeing are controlled by the MacOS library
+ * routines NewPtr() and DisposePtr(), which allocate fixed-address
+ * storage. Unfortunately, the IJG library isn't smart enough to cope
+ * with relocatable storage.
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void *) NewPtr(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+ DisposePtr((Ptr) object);
+}
+
+
+/*
+ * "Large" objects are treated the same as "small" ones.
+ * NB: we include FAR keywords in the routine declarations simply for
+ * consistency with the rest of the IJG code; FAR should expand to empty
+ * on rational architectures like the Mac.
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void FAR *) NewPtr(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+ DisposePtr((Ptr) object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ */
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+ long max_bytes_needed, long already_allocated)
+{
+ long limit = cinfo->mem->max_memory_to_use - already_allocated;
+ long slop, mem;
+
+ /* Don't ask for more than what application has told us we may use */
+ if (max_bytes_needed > limit && limit > 0)
+ max_bytes_needed = limit;
+ /* Find whether there's a big enough free block in the heap.
+ * CompactMem tries to create a contiguous block of the requested size,
+ * and then returns the size of the largest free block (which could be
+ * much more or much less than we asked for).
+ * We add some slop to ensure we don't use up all available memory.
+ */
+ slop = max_bytes_needed / 16 + 32768L;
+ mem = CompactMem(max_bytes_needed + slop) - slop;
+ if (mem < 0)
+ mem = 0; /* sigh, couldn't even get the slop */
+ /* Don't take more than the application says we can have */
+ if (mem > limit && limit > 0)
+ mem = limit;
+ return mem;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Backing store objects are only used when the value returned by
+ * jpeg_mem_available is less than the total space needed. You can dispense
+ * with these routines if you have plenty of virtual memory; see jmemnobs.c.
+ */
+
+
+METHODDEF(void)
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ long bytes = byte_count;
+ long retVal;
+
+ if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+
+ retVal = FSRead ( info->temp_file, &bytes,
+ (unsigned char *) buffer_address );
+ if ( retVal != noErr || bytes != byte_count )
+ ERREXIT(cinfo, JERR_TFILE_READ);
+}
+
+
+METHODDEF(void)
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ long bytes = byte_count;
+ long retVal;
+
+ if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+
+ retVal = FSWrite ( info->temp_file, &bytes,
+ (unsigned char *) buffer_address );
+ if ( retVal != noErr || bytes != byte_count )
+ ERREXIT(cinfo, JERR_TFILE_WRITE);
+}
+
+
+METHODDEF(void)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ FSClose ( info->temp_file );
+ FSpDelete ( &(info->tempSpec) );
+}
+
+
+/*
+ * Initial opening of a backing-store object.
+ *
+ * This version uses FindFolder to find the Temporary Items folder,
+ * and puts the temporary file in there.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ short tmpRef, vRefNum;
+ long dirID;
+ FInfo finderInfo;
+ FSSpec theSpec;
+ Str255 fName;
+ OSErr osErr;
+ long gestaltResponse = 0;
+
+ /* Check that FSSpec calls are available. */
+ osErr = Gestalt( gestaltFSAttr, &gestaltResponse );
+ if ( ( osErr != noErr )
+ || !( gestaltResponse & (1<<gestaltHasFSSpecCalls) ) )
+ ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required");
+ /* TO DO: add a proper error message to jerror.h. */
+
+ /* Check that FindFolder is available. */
+ osErr = Gestalt( gestaltFindFolderAttr, &gestaltResponse );
+ if ( ( osErr != noErr )
+ || !( gestaltResponse & (1<<gestaltFindFolderPresent) ) )
+ ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required.");
+ /* TO DO: add a proper error message to jerror.h. */
+
+ osErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
+ &vRefNum, &dirID );
+ if ( osErr != noErr )
+ ERREXITS(cinfo, JERR_TFILE_CREATE, "- temporary items folder unavailable");
+ /* TO DO: Try putting the temp files somewhere else. */
+
+ /* Keep generating file names till we find one that's not in use */
+ for (;;) {
+ next_file_num++; /* advance counter */
+
+ sprintf(info->temp_name, TEMP_FILE_NAME, next_file_num);
+ strcpy ( (Ptr)fName+1, info->temp_name );
+ *fName = strlen (info->temp_name);
+ osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec );
+
+ if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr )
+ break;
+ }
+
+ osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript );
+ if ( osErr != noErr )
+ ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
+
+ osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) );
+ if ( osErr != noErr )
+ ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
+
+ info->tempSpec = theSpec;
+
+ info->read_backing_store = read_backing_store;
+ info->write_backing_store = write_backing_store;
+ info->close_backing_store = close_backing_store;
+ TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+ next_file_num = 0;
+
+ /* max_memory_to_use will be initialized to FreeMem()'s result;
+ * the calling application might later reduce it, for example
+ * to leave room to invoke multiple JPEG objects.
+ * Note that FreeMem returns the total number of free bytes;
+ * it may not be possible to allocate a single block of this size.
+ */
+ return FreeMem();
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+ /* no work */
+}
diff --git a/test/monniaux/jpeg-6b/jmemmgr.c b/test/monniaux/jpeg-6b/jmemmgr.c
new file mode 100644
index 00000000..d801b322
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemmgr.c
@@ -0,0 +1,1118 @@
+/*
+ * jmemmgr.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the JPEG system-independent memory management
+ * routines. This code is usable across a wide variety of machines; most
+ * of the system dependencies have been isolated in a separate file.
+ * The major functions provided here are:
+ * * pool-based allocation and freeing of memory;
+ * * policy decisions about how to divide available memory among the
+ * virtual arrays;
+ * * control logic for swapping virtual arrays between main memory and
+ * backing storage.
+ * The separate system-dependent file provides the actual backing-storage
+ * access code, and it contains the policy decision about how much total
+ * main memory to use.
+ * This file is system-dependent in the sense that some of its functions
+ * are unnecessary in some systems. For example, if there is enough virtual
+ * memory so that backing storage will never be used, much of the virtual
+ * array control logic could be removed. (Of course, if you have that much
+ * memory then you shouldn't care about a little bit of unused code...)
+ */
+
+#define JPEG_INTERNALS
+#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef NO_GETENV
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
+extern char * getenv JPP((const char * name));
+#endif
+#endif
+
+
+/*
+ * Some important notes:
+ * The allocation routines provided here must never return NULL.
+ * They should exit to error_exit if unsuccessful.
+ *
+ * It's not a good idea to try to merge the sarray and barray routines,
+ * even though they are textually almost the same, because samples are
+ * usually stored as bytes while coefficients are shorts or ints. Thus,
+ * in machines where byte pointers have a different representation from
+ * word pointers, the resulting machine code could not be the same.
+ */
+
+
+/*
+ * Many machines require storage alignment: longs must start on 4-byte
+ * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
+ * always returns pointers that are multiples of the worst-case alignment
+ * requirement, and we had better do so too.
+ * There isn't any really portable way to determine the worst-case alignment
+ * requirement. This module assumes that the alignment requirement is
+ * multiples of sizeof(ALIGN_TYPE).
+ * By default, we define ALIGN_TYPE as double. This is necessary on some
+ * workstations (where doubles really do need 8-byte alignment) and will work
+ * fine on nearly everything. If your machine has lesser alignment needs,
+ * you can save a few bytes by making ALIGN_TYPE smaller.
+ * The only place I know of where this will NOT work is certain Macintosh
+ * 680x0 compilers that define double as a 10-byte IEEE extended float.
+ * Doing 10-byte alignment is counterproductive because longwords won't be
+ * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have
+ * such a compiler.
+ */
+
+#ifndef ALIGN_TYPE /* so can override from jconfig.h */
+#define ALIGN_TYPE double
+#endif
+
+
+/*
+ * We allocate objects from "pools", where each pool is gotten with a single
+ * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
+ * overhead within a pool, except for alignment padding. Each pool has a
+ * header with a link to the next pool of the same class.
+ * Small and large pool headers are identical except that the latter's
+ * link pointer must be FAR on 80x86 machines.
+ * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
+ * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
+ * of the alignment requirement of ALIGN_TYPE.
+ */
+
+typedef union small_pool_struct * small_pool_ptr;
+
+typedef union small_pool_struct {
+ struct {
+ small_pool_ptr next; /* next in list of pools */
+ size_t bytes_used; /* how many bytes already used within pool */
+ size_t bytes_left; /* bytes still available in this pool */
+ } hdr;
+ ALIGN_TYPE dummy; /* included in union to ensure alignment */
+} small_pool_hdr;
+
+typedef union large_pool_struct FAR * large_pool_ptr;
+
+typedef union large_pool_struct {
+ struct {
+ large_pool_ptr next; /* next in list of pools */
+ size_t bytes_used; /* how many bytes already used within pool */
+ size_t bytes_left; /* bytes still available in this pool */
+ } hdr;
+ ALIGN_TYPE dummy; /* included in union to ensure alignment */
+} large_pool_hdr;
+
+
+/*
+ * Here is the full definition of a memory manager object.
+ */
+
+typedef struct {
+ struct jpeg_memory_mgr pub; /* public fields */
+
+ /* Each pool identifier (lifetime class) names a linked list of pools. */
+ small_pool_ptr small_list[JPOOL_NUMPOOLS];
+ large_pool_ptr large_list[JPOOL_NUMPOOLS];
+
+ /* Since we only have one lifetime class of virtual arrays, only one
+ * linked list is necessary (for each datatype). Note that the virtual
+ * array control blocks being linked together are actually stored somewhere
+ * in the small-pool list.
+ */
+ jvirt_sarray_ptr virt_sarray_list;
+ jvirt_barray_ptr virt_barray_list;
+
+ /* This counts total space obtained from jpeg_get_small/large */
+ long total_space_allocated;
+
+ /* alloc_sarray and alloc_barray set this value for use by virtual
+ * array routines.
+ */
+ JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
+} my_memory_mgr;
+
+typedef my_memory_mgr * my_mem_ptr;
+
+
+/*
+ * The control blocks for virtual arrays.
+ * Note that these blocks are allocated in the "small" pool area.
+ * System-dependent info for the associated backing store (if any) is hidden
+ * inside the backing_store_info struct.
+ */
+
+struct jvirt_sarray_control {
+ JSAMPARRAY mem_buffer; /* => the in-memory buffer */
+ JDIMENSION rows_in_array; /* total virtual array height */
+ JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
+ JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
+ JDIMENSION rows_in_mem; /* height of memory buffer */
+ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
+ JDIMENSION cur_start_row; /* first logical row # in the buffer */
+ JDIMENSION first_undef_row; /* row # of first uninitialized row */
+ boolean pre_zero; /* pre-zero mode requested? */
+ boolean dirty; /* do current buffer contents need written? */
+ boolean b_s_open; /* is backing-store data valid? */
+ jvirt_sarray_ptr next; /* link to next virtual sarray control block */
+ backing_store_info b_s_info; /* System-dependent control info */
+};
+
+struct jvirt_barray_control {
+ JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
+ JDIMENSION rows_in_array; /* total virtual array height */
+ JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
+ JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
+ JDIMENSION rows_in_mem; /* height of memory buffer */
+ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
+ JDIMENSION cur_start_row; /* first logical row # in the buffer */
+ JDIMENSION first_undef_row; /* row # of first uninitialized row */
+ boolean pre_zero; /* pre-zero mode requested? */
+ boolean dirty; /* do current buffer contents need written? */
+ boolean b_s_open; /* is backing-store data valid? */
+ jvirt_barray_ptr next; /* link to next virtual barray control block */
+ backing_store_info b_s_info; /* System-dependent control info */
+};
+
+
+#ifdef MEM_STATS /* optional extra stuff for statistics */
+
+LOCAL(void)
+print_mem_stats (j_common_ptr cinfo, int pool_id)
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr shdr_ptr;
+ large_pool_ptr lhdr_ptr;
+
+ /* Since this is only a debugging stub, we can cheat a little by using
+ * fprintf directly rather than going through the trace message code.
+ * This is helpful because message parm array can't handle longs.
+ */
+ fprintf(stderr, "Freeing pool %d, total space = %ld\n",
+ pool_id, mem->total_space_allocated);
+
+ for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
+ lhdr_ptr = lhdr_ptr->hdr.next) {
+ fprintf(stderr, " Large chunk used %ld\n",
+ (long) lhdr_ptr->hdr.bytes_used);
+ }
+
+ for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
+ shdr_ptr = shdr_ptr->hdr.next) {
+ fprintf(stderr, " Small chunk used %ld free %ld\n",
+ (long) shdr_ptr->hdr.bytes_used,
+ (long) shdr_ptr->hdr.bytes_left);
+ }
+}
+
+#endif /* MEM_STATS */
+
+
+LOCAL(void)
+out_of_memory (j_common_ptr cinfo, int which)
+/* Report an out-of-memory error and stop execution */
+/* If we compiled MEM_STATS support, report alloc requests before dying */
+{
+#ifdef MEM_STATS
+ cinfo->err->trace_level = 2; /* force self_destruct to report stats */
+#endif
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
+}
+
+
+/*
+ * Allocation of "small" objects.
+ *
+ * For these, we use pooled storage. When a new pool must be created,
+ * we try to get enough space for the current request plus a "slop" factor,
+ * where the slop will be the amount of leftover space in the new pool.
+ * The speed vs. space tradeoff is largely determined by the slop values.
+ * A different slop value is provided for each pool class (lifetime),
+ * and we also distinguish the first pool of a class from later ones.
+ * NOTE: the values given work fairly well on both 16- and 32-bit-int
+ * machines, but may be too small if longs are 64 bits or more.
+ */
+
+static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
+{
+ 1600, /* first PERMANENT pool */
+ 16000 /* first IMAGE pool */
+};
+
+static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
+{
+ 0, /* additional PERMANENT pools */
+ 5000 /* additional IMAGE pools */
+};
+
+#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
+
+
+METHODDEF(void *)
+alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+/* Allocate a "small" object */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr hdr_ptr, prev_hdr_ptr;
+ char * data_ptr;
+ size_t odd_bytes, min_request, slop;
+
+ /* Check for unsatisfiable request (do now to ensure no overflow below) */
+ if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
+ out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
+
+ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+ odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+ if (odd_bytes > 0)
+ sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+ /* See if space is available in any existing pool */
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+ prev_hdr_ptr = NULL;
+ hdr_ptr = mem->small_list[pool_id];
+ while (hdr_ptr != NULL) {
+ if (hdr_ptr->hdr.bytes_left >= sizeofobject)
+ break; /* found pool with enough space */
+ prev_hdr_ptr = hdr_ptr;
+ hdr_ptr = hdr_ptr->hdr.next;
+ }
+
+ /* Time to make a new pool? */
+ if (hdr_ptr == NULL) {
+ /* min_request is what we need now, slop is what will be leftover */
+ min_request = sizeofobject + SIZEOF(small_pool_hdr);
+ if (prev_hdr_ptr == NULL) /* first pool in class? */
+ slop = first_pool_slop[pool_id];
+ else
+ slop = extra_pool_slop[pool_id];
+ /* Don't ask for more than MAX_ALLOC_CHUNK */
+ if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
+ slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
+ /* Try to get space, if fail reduce slop and try again */
+ for (;;) {
+ hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
+ if (hdr_ptr != NULL)
+ break;
+ slop /= 2;
+ if (slop < MIN_SLOP) /* give up when it gets real small */
+ out_of_memory(cinfo, 2); /* jpeg_get_small failed */
+ }
+ mem->total_space_allocated += min_request + slop;
+ /* Success, initialize the new pool header and add to end of list */
+ hdr_ptr->hdr.next = NULL;
+ hdr_ptr->hdr.bytes_used = 0;
+ hdr_ptr->hdr.bytes_left = sizeofobject + slop;
+ if (prev_hdr_ptr == NULL) /* first pool in class? */
+ mem->small_list[pool_id] = hdr_ptr;
+ else
+ prev_hdr_ptr->hdr.next = hdr_ptr;
+ }
+
+ /* OK, allocate the object from the current pool */
+ data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
+ data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
+ hdr_ptr->hdr.bytes_used += sizeofobject;
+ hdr_ptr->hdr.bytes_left -= sizeofobject;
+
+ return (void *) data_ptr;
+}
+
+
+/*
+ * Allocation of "large" objects.
+ *
+ * The external semantics of these are the same as "small" objects,
+ * except that FAR pointers are used on 80x86. However the pool
+ * management heuristics are quite different. We assume that each
+ * request is large enough that it may as well be passed directly to
+ * jpeg_get_large; the pool management just links everything together
+ * so that we can free it all on demand.
+ * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
+ * structures. The routines that create these structures (see below)
+ * deliberately bunch rows together to ensure a large request size.
+ */
+
+METHODDEF(void FAR *)
+alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+/* Allocate a "large" object */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ large_pool_ptr hdr_ptr;
+ size_t odd_bytes;
+
+ /* Check for unsatisfiable request (do now to ensure no overflow below) */
+ if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
+ out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
+
+ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+ odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+ if (odd_bytes > 0)
+ sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+ /* Always make a new pool */
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
+ SIZEOF(large_pool_hdr));
+ if (hdr_ptr == NULL)
+ out_of_memory(cinfo, 4); /* jpeg_get_large failed */
+ mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
+
+ /* Success, initialize the new pool header and add to list */
+ hdr_ptr->hdr.next = mem->large_list[pool_id];
+ /* We maintain space counts in each pool header for statistical purposes,
+ * even though they are not needed for allocation.
+ */
+ hdr_ptr->hdr.bytes_used = sizeofobject;
+ hdr_ptr->hdr.bytes_left = 0;
+ mem->large_list[pool_id] = hdr_ptr;
+
+ return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
+}
+
+
+/*
+ * Creation of 2-D sample arrays.
+ * The pointers are in near heap, the samples themselves in FAR heap.
+ *
+ * To minimize allocation overhead and to allow I/O of large contiguous
+ * blocks, we allocate the sample rows in groups of as many rows as possible
+ * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
+ * NB: the virtual array control routines, later in this file, know about
+ * this chunking of rows. The rowsperchunk value is left in the mem manager
+ * object so that it can be saved away if this sarray is the workspace for
+ * a virtual array.
+ */
+
+METHODDEF(JSAMPARRAY)
+alloc_sarray (j_common_ptr cinfo, int pool_id,
+ JDIMENSION samplesperrow, JDIMENSION numrows)
+/* Allocate a 2-D sample array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ JSAMPARRAY result;
+ JSAMPROW workspace;
+ JDIMENSION rowsperchunk, currow, i;
+ long ltemp;
+
+ /* Calculate max # of rows allowed in one allocation chunk */
+ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+ ((long) samplesperrow * SIZEOF(JSAMPLE));
+ if (ltemp <= 0)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+ if (ltemp < (long) numrows)
+ rowsperchunk = (JDIMENSION) ltemp;
+ else
+ rowsperchunk = numrows;
+ mem->last_rowsperchunk = rowsperchunk;
+
+ /* Get space for row pointers (small object) */
+ result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
+ (size_t) (numrows * SIZEOF(JSAMPROW)));
+
+ /* Get the rows themselves (large objects) */
+ currow = 0;
+ while (currow < numrows) {
+ rowsperchunk = MIN(rowsperchunk, numrows - currow);
+ workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
+ (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
+ * SIZEOF(JSAMPLE)));
+ for (i = rowsperchunk; i > 0; i--) {
+ result[currow++] = workspace;
+ workspace += samplesperrow;
+ }
+ }
+
+ return result;
+}
+
+
+/*
+ * Creation of 2-D coefficient-block arrays.
+ * This is essentially the same as the code for sample arrays, above.
+ */
+
+METHODDEF(JBLOCKARRAY)
+alloc_barray (j_common_ptr cinfo, int pool_id,
+ JDIMENSION blocksperrow, JDIMENSION numrows)
+/* Allocate a 2-D coefficient-block array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ JBLOCKARRAY result;
+ JBLOCKROW workspace;
+ JDIMENSION rowsperchunk, currow, i;
+ long ltemp;
+
+ /* Calculate max # of rows allowed in one allocation chunk */
+ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+ ((long) blocksperrow * SIZEOF(JBLOCK));
+ if (ltemp <= 0)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+ if (ltemp < (long) numrows)
+ rowsperchunk = (JDIMENSION) ltemp;
+ else
+ rowsperchunk = numrows;
+ mem->last_rowsperchunk = rowsperchunk;
+
+ /* Get space for row pointers (small object) */
+ result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
+ (size_t) (numrows * SIZEOF(JBLOCKROW)));
+
+ /* Get the rows themselves (large objects) */
+ currow = 0;
+ while (currow < numrows) {
+ rowsperchunk = MIN(rowsperchunk, numrows - currow);
+ workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
+ (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
+ * SIZEOF(JBLOCK)));
+ for (i = rowsperchunk; i > 0; i--) {
+ result[currow++] = workspace;
+ workspace += blocksperrow;
+ }
+ }
+
+ return result;
+}
+
+
+/*
+ * About virtual array management:
+ *
+ * The above "normal" array routines are only used to allocate strip buffers
+ * (as wide as the image, but just a few rows high). Full-image-sized buffers
+ * are handled as "virtual" arrays. The array is still accessed a strip at a
+ * time, but the memory manager must save the whole array for repeated
+ * accesses. The intended implementation is that there is a strip buffer in
+ * memory (as high as is possible given the desired memory limit), plus a
+ * backing file that holds the rest of the array.
+ *
+ * The request_virt_array routines are told the total size of the image and
+ * the maximum number of rows that will be accessed at once. The in-memory
+ * buffer must be at least as large as the maxaccess value.
+ *
+ * The request routines create control blocks but not the in-memory buffers.
+ * That is postponed until realize_virt_arrays is called. At that time the
+ * total amount of space needed is known (approximately, anyway), so free
+ * memory can be divided up fairly.
+ *
+ * The access_virt_array routines are responsible for making a specific strip
+ * area accessible (after reading or writing the backing file, if necessary).
+ * Note that the access routines are told whether the caller intends to modify
+ * the accessed strip; during a read-only pass this saves having to rewrite
+ * data to disk. The access routines are also responsible for pre-zeroing
+ * any newly accessed rows, if pre-zeroing was requested.
+ *
+ * In current usage, the access requests are usually for nonoverlapping
+ * strips; that is, successive access start_row numbers differ by exactly
+ * num_rows = maxaccess. This means we can get good performance with simple
+ * buffer dump/reload logic, by making the in-memory buffer be a multiple
+ * of the access height; then there will never be accesses across bufferload
+ * boundaries. The code will still work with overlapping access requests,
+ * but it doesn't handle bufferload overlaps very efficiently.
+ */
+
+
+METHODDEF(jvirt_sarray_ptr)
+request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+ JDIMENSION samplesperrow, JDIMENSION numrows,
+ JDIMENSION maxaccess)
+/* Request a virtual 2-D sample array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ jvirt_sarray_ptr result;
+
+ /* Only IMAGE-lifetime virtual arrays are currently supported */
+ if (pool_id != JPOOL_IMAGE)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ /* get control block */
+ result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
+ SIZEOF(struct jvirt_sarray_control));
+
+ result->mem_buffer = NULL; /* marks array not yet realized */
+ result->rows_in_array = numrows;
+ result->samplesperrow = samplesperrow;
+ result->maxaccess = maxaccess;
+ result->pre_zero = pre_zero;
+ result->b_s_open = FALSE; /* no associated backing-store object */
+ result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
+ mem->virt_sarray_list = result;
+
+ return result;
+}
+
+
+METHODDEF(jvirt_barray_ptr)
+request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+ JDIMENSION blocksperrow, JDIMENSION numrows,
+ JDIMENSION maxaccess)
+/* Request a virtual 2-D coefficient-block array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ jvirt_barray_ptr result;
+
+ /* Only IMAGE-lifetime virtual arrays are currently supported */
+ if (pool_id != JPOOL_IMAGE)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ /* get control block */
+ result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
+ SIZEOF(struct jvirt_barray_control));
+
+ result->mem_buffer = NULL; /* marks array not yet realized */
+ result->rows_in_array = numrows;
+ result->blocksperrow = blocksperrow;
+ result->maxaccess = maxaccess;
+ result->pre_zero = pre_zero;
+ result->b_s_open = FALSE; /* no associated backing-store object */
+ result->next = mem->virt_barray_list; /* add to list of virtual arrays */
+ mem->virt_barray_list = result;
+
+ return result;
+}
+
+
+METHODDEF(void)
+realize_virt_arrays (j_common_ptr cinfo)
+/* Allocate the in-memory buffers for any unrealized virtual arrays */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ long space_per_minheight, maximum_space, avail_mem;
+ long minheights, max_minheights;
+ jvirt_sarray_ptr sptr;
+ jvirt_barray_ptr bptr;
+
+ /* Compute the minimum space needed (maxaccess rows in each buffer)
+ * and the maximum space needed (full image height in each buffer).
+ * These may be of use to the system-dependent jpeg_mem_available routine.
+ */
+ space_per_minheight = 0;
+ maximum_space = 0;
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->mem_buffer == NULL) { /* if not realized yet */
+ space_per_minheight += (long) sptr->maxaccess *
+ (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+ maximum_space += (long) sptr->rows_in_array *
+ (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+ }
+ }
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->mem_buffer == NULL) { /* if not realized yet */
+ space_per_minheight += (long) bptr->maxaccess *
+ (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+ maximum_space += (long) bptr->rows_in_array *
+ (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+ }
+ }
+
+ if (space_per_minheight <= 0)
+ return; /* no unrealized arrays, no work */
+
+ /* Determine amount of memory to actually use; this is system-dependent. */
+ avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
+ mem->total_space_allocated);
+
+ /* If the maximum space needed is available, make all the buffers full
+ * height; otherwise parcel it out with the same number of minheights
+ * in each buffer.
+ */
+ if (avail_mem >= maximum_space)
+ max_minheights = 1000000000L;
+ else {
+ max_minheights = avail_mem / space_per_minheight;
+ /* If there doesn't seem to be enough space, try to get the minimum
+ * anyway. This allows a "stub" implementation of jpeg_mem_available().
+ */
+ if (max_minheights <= 0)
+ max_minheights = 1;
+ }
+
+ /* Allocate the in-memory buffers and initialize backing store as needed. */
+
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->mem_buffer == NULL) { /* if not realized yet */
+ minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
+ if (minheights <= max_minheights) {
+ /* This buffer fits in memory */
+ sptr->rows_in_mem = sptr->rows_in_array;
+ } else {
+ /* It doesn't fit in memory, create backing store. */
+ sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
+ jpeg_open_backing_store(cinfo, & sptr->b_s_info,
+ (long) sptr->rows_in_array *
+ (long) sptr->samplesperrow *
+ (long) SIZEOF(JSAMPLE));
+ sptr->b_s_open = TRUE;
+ }
+ sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
+ sptr->samplesperrow, sptr->rows_in_mem);
+ sptr->rowsperchunk = mem->last_rowsperchunk;
+ sptr->cur_start_row = 0;
+ sptr->first_undef_row = 0;
+ sptr->dirty = FALSE;
+ }
+ }
+
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->mem_buffer == NULL) { /* if not realized yet */
+ minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
+ if (minheights <= max_minheights) {
+ /* This buffer fits in memory */
+ bptr->rows_in_mem = bptr->rows_in_array;
+ } else {
+ /* It doesn't fit in memory, create backing store. */
+ bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
+ jpeg_open_backing_store(cinfo, & bptr->b_s_info,
+ (long) bptr->rows_in_array *
+ (long) bptr->blocksperrow *
+ (long) SIZEOF(JBLOCK));
+ bptr->b_s_open = TRUE;
+ }
+ bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
+ bptr->blocksperrow, bptr->rows_in_mem);
+ bptr->rowsperchunk = mem->last_rowsperchunk;
+ bptr->cur_start_row = 0;
+ bptr->first_undef_row = 0;
+ bptr->dirty = FALSE;
+ }
+ }
+}
+
+
+LOCAL(void)
+do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual sample array */
+{
+ long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+ bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
+ file_offset = ptr->cur_start_row * bytesperrow;
+ /* Loop to read or write each allocation chunk in mem_buffer */
+ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+ /* One chunk, but check for short chunk at end of buffer */
+ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+ /* Transfer no more than is currently defined */
+ thisrow = (long) ptr->cur_start_row + i;
+ rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+ /* Transfer no more than fits in file */
+ rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+ if (rows <= 0) /* this chunk might be past end of file! */
+ break;
+ byte_count = rows * bytesperrow;
+ if (writing)
+ (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ else
+ (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ file_offset += byte_count;
+ }
+}
+
+
+LOCAL(void)
+do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual coefficient-block array */
+{
+ long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+ bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
+ file_offset = ptr->cur_start_row * bytesperrow;
+ /* Loop to read or write each allocation chunk in mem_buffer */
+ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+ /* One chunk, but check for short chunk at end of buffer */
+ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+ /* Transfer no more than is currently defined */
+ thisrow = (long) ptr->cur_start_row + i;
+ rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+ /* Transfer no more than fits in file */
+ rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+ if (rows <= 0) /* this chunk might be past end of file! */
+ break;
+ byte_count = rows * bytesperrow;
+ if (writing)
+ (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ else
+ (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ file_offset += byte_count;
+ }
+}
+
+
+METHODDEF(JSAMPARRAY)
+access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
+ JDIMENSION start_row, JDIMENSION num_rows,
+ boolean writable)
+/* Access the part of a virtual sample array starting at start_row */
+/* and extending for num_rows rows. writable is true if */
+/* caller intends to modify the accessed area. */
+{
+ JDIMENSION end_row = start_row + num_rows;
+ JDIMENSION undef_row;
+
+ /* debugging check */
+ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+ ptr->mem_buffer == NULL)
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+ /* Make the desired part of the virtual array accessible */
+ if (start_row < ptr->cur_start_row ||
+ end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+ if (! ptr->b_s_open)
+ ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+ /* Flush old buffer contents if necessary */
+ if (ptr->dirty) {
+ do_sarray_io(cinfo, ptr, TRUE);
+ ptr->dirty = FALSE;
+ }
+ /* Decide what part of virtual array to access.
+ * Algorithm: if target address > current window, assume forward scan,
+ * load starting at target address. If target address < current window,
+ * assume backward scan, load so that target area is top of window.
+ * Note that when switching from forward write to forward read, will have
+ * start_row = 0, so the limiting case applies and we load from 0 anyway.
+ */
+ if (start_row > ptr->cur_start_row) {
+ ptr->cur_start_row = start_row;
+ } else {
+ /* use long arithmetic here to avoid overflow & unsigned problems */
+ long ltemp;
+
+ ltemp = (long) end_row - (long) ptr->rows_in_mem;
+ if (ltemp < 0)
+ ltemp = 0; /* don't fall off front end of file */
+ ptr->cur_start_row = (JDIMENSION) ltemp;
+ }
+ /* Read in the selected part of the array.
+ * During the initial write pass, we will do no actual read
+ * because the selected part is all undefined.
+ */
+ do_sarray_io(cinfo, ptr, FALSE);
+ }
+ /* Ensure the accessed part of the array is defined; prezero if needed.
+ * To improve locality of access, we only prezero the part of the array
+ * that the caller is about to access, not the entire in-memory array.
+ */
+ if (ptr->first_undef_row < end_row) {
+ if (ptr->first_undef_row < start_row) {
+ if (writable) /* writer skipped over a section of array */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ undef_row = start_row; /* but reader is allowed to read ahead */
+ } else {
+ undef_row = ptr->first_undef_row;
+ }
+ if (writable)
+ ptr->first_undef_row = end_row;
+ if (ptr->pre_zero) {
+ size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
+ undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+ end_row -= ptr->cur_start_row;
+ while (undef_row < end_row) {
+ jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+ undef_row++;
+ }
+ } else {
+ if (! writable) /* reader looking at undefined data */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ }
+ }
+ /* Flag the buffer dirty if caller will write in it */
+ if (writable)
+ ptr->dirty = TRUE;
+ /* Return address of proper part of the buffer */
+ return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+}
+
+
+METHODDEF(JBLOCKARRAY)
+access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
+ JDIMENSION start_row, JDIMENSION num_rows,
+ boolean writable)
+/* Access the part of a virtual block array starting at start_row */
+/* and extending for num_rows rows. writable is true if */
+/* caller intends to modify the accessed area. */
+{
+ JDIMENSION end_row = start_row + num_rows;
+ JDIMENSION undef_row;
+
+ /* debugging check */
+ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+ ptr->mem_buffer == NULL)
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+ /* Make the desired part of the virtual array accessible */
+ if (start_row < ptr->cur_start_row ||
+ end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+ if (! ptr->b_s_open)
+ ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+ /* Flush old buffer contents if necessary */
+ if (ptr->dirty) {
+ do_barray_io(cinfo, ptr, TRUE);
+ ptr->dirty = FALSE;
+ }
+ /* Decide what part of virtual array to access.
+ * Algorithm: if target address > current window, assume forward scan,
+ * load starting at target address. If target address < current window,
+ * assume backward scan, load so that target area is top of window.
+ * Note that when switching from forward write to forward read, will have
+ * start_row = 0, so the limiting case applies and we load from 0 anyway.
+ */
+ if (start_row > ptr->cur_start_row) {
+ ptr->cur_start_row = start_row;
+ } else {
+ /* use long arithmetic here to avoid overflow & unsigned problems */
+ long ltemp;
+
+ ltemp = (long) end_row - (long) ptr->rows_in_mem;
+ if (ltemp < 0)
+ ltemp = 0; /* don't fall off front end of file */
+ ptr->cur_start_row = (JDIMENSION) ltemp;
+ }
+ /* Read in the selected part of the array.
+ * During the initial write pass, we will do no actual read
+ * because the selected part is all undefined.
+ */
+ do_barray_io(cinfo, ptr, FALSE);
+ }
+ /* Ensure the accessed part of the array is defined; prezero if needed.
+ * To improve locality of access, we only prezero the part of the array
+ * that the caller is about to access, not the entire in-memory array.
+ */
+ if (ptr->first_undef_row < end_row) {
+ if (ptr->first_undef_row < start_row) {
+ if (writable) /* writer skipped over a section of array */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ undef_row = start_row; /* but reader is allowed to read ahead */
+ } else {
+ undef_row = ptr->first_undef_row;
+ }
+ if (writable)
+ ptr->first_undef_row = end_row;
+ if (ptr->pre_zero) {
+ size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
+ undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+ end_row -= ptr->cur_start_row;
+ while (undef_row < end_row) {
+ jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+ undef_row++;
+ }
+ } else {
+ if (! writable) /* reader looking at undefined data */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ }
+ }
+ /* Flag the buffer dirty if caller will write in it */
+ if (writable)
+ ptr->dirty = TRUE;
+ /* Return address of proper part of the buffer */
+ return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+}
+
+
+/*
+ * Release all objects belonging to a specified pool.
+ */
+
+METHODDEF(void)
+free_pool (j_common_ptr cinfo, int pool_id)
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr shdr_ptr;
+ large_pool_ptr lhdr_ptr;
+ size_t space_freed;
+
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+#ifdef MEM_STATS
+ if (cinfo->err->trace_level > 1)
+ print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
+#endif
+
+ /* If freeing IMAGE pool, close any virtual arrays first */
+ if (pool_id == JPOOL_IMAGE) {
+ jvirt_sarray_ptr sptr;
+ jvirt_barray_ptr bptr;
+
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->b_s_open) { /* there may be no backing store */
+ sptr->b_s_open = FALSE; /* prevent recursive close if error */
+ (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
+ }
+ }
+ mem->virt_sarray_list = NULL;
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->b_s_open) { /* there may be no backing store */
+ bptr->b_s_open = FALSE; /* prevent recursive close if error */
+ (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
+ }
+ }
+ mem->virt_barray_list = NULL;
+ }
+
+ /* Release large objects */
+ lhdr_ptr = mem->large_list[pool_id];
+ mem->large_list[pool_id] = NULL;
+
+ while (lhdr_ptr != NULL) {
+ large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
+ space_freed = lhdr_ptr->hdr.bytes_used +
+ lhdr_ptr->hdr.bytes_left +
+ SIZEOF(large_pool_hdr);
+ jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
+ mem->total_space_allocated -= space_freed;
+ lhdr_ptr = next_lhdr_ptr;
+ }
+
+ /* Release small objects */
+ shdr_ptr = mem->small_list[pool_id];
+ mem->small_list[pool_id] = NULL;
+
+ while (shdr_ptr != NULL) {
+ small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
+ space_freed = shdr_ptr->hdr.bytes_used +
+ shdr_ptr->hdr.bytes_left +
+ SIZEOF(small_pool_hdr);
+ jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
+ mem->total_space_allocated -= space_freed;
+ shdr_ptr = next_shdr_ptr;
+ }
+}
+
+
+/*
+ * Close up shop entirely.
+ * Note that this cannot be called unless cinfo->mem is non-NULL.
+ */
+
+METHODDEF(void)
+self_destruct (j_common_ptr cinfo)
+{
+ int pool;
+
+ /* Close all backing store, release all memory.
+ * Releasing pools in reverse order might help avoid fragmentation
+ * with some (brain-damaged) malloc libraries.
+ */
+ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+ free_pool(cinfo, pool);
+ }
+
+ /* Release the memory manager control block too. */
+ jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
+ cinfo->mem = NULL; /* ensures I will be called only once */
+
+ jpeg_mem_term(cinfo); /* system-dependent cleanup */
+}
+
+
+/*
+ * Memory manager initialization.
+ * When this is called, only the error manager pointer is valid in cinfo!
+ */
+
+GLOBAL(void)
+jinit_memory_mgr (j_common_ptr cinfo)
+{
+ my_mem_ptr mem;
+ long max_to_use;
+ int pool;
+ size_t test_mac;
+
+ cinfo->mem = NULL; /* for safety if init fails */
+
+ /* Check for configuration errors.
+ * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
+ * doesn't reflect any real hardware alignment requirement.
+ * The test is a little tricky: for X>0, X and X-1 have no one-bits
+ * in common if and only if X is a power of 2, ie has only one one-bit.
+ * Some compilers may give an "unreachable code" warning here; ignore it.
+ */
+ if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
+ ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
+ /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
+ * a multiple of SIZEOF(ALIGN_TYPE).
+ * Again, an "unreachable code" warning may be ignored here.
+ * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
+ */
+ test_mac = (size_t) MAX_ALLOC_CHUNK;
+ if ((long) test_mac != MAX_ALLOC_CHUNK ||
+ (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
+ ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
+
+ max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
+
+ /* Attempt to allocate memory manager's control block */
+ mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
+
+ if (mem == NULL) {
+ jpeg_mem_term(cinfo); /* system-dependent cleanup */
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
+ }
+
+ /* OK, fill in the method pointers */
+ mem->pub.alloc_small = alloc_small;
+ mem->pub.alloc_large = alloc_large;
+ mem->pub.alloc_sarray = alloc_sarray;
+ mem->pub.alloc_barray = alloc_barray;
+ mem->pub.request_virt_sarray = request_virt_sarray;
+ mem->pub.request_virt_barray = request_virt_barray;
+ mem->pub.realize_virt_arrays = realize_virt_arrays;
+ mem->pub.access_virt_sarray = access_virt_sarray;
+ mem->pub.access_virt_barray = access_virt_barray;
+ mem->pub.free_pool = free_pool;
+ mem->pub.self_destruct = self_destruct;
+
+ /* Make MAX_ALLOC_CHUNK accessible to other modules */
+ mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
+
+ /* Initialize working state */
+ mem->pub.max_memory_to_use = max_to_use;
+
+ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+ mem->small_list[pool] = NULL;
+ mem->large_list[pool] = NULL;
+ }
+ mem->virt_sarray_list = NULL;
+ mem->virt_barray_list = NULL;
+
+ mem->total_space_allocated = SIZEOF(my_memory_mgr);
+
+ /* Declare ourselves open for business */
+ cinfo->mem = & mem->pub;
+
+ /* Check for an environment variable JPEGMEM; if found, override the
+ * default max_memory setting from jpeg_mem_init. Note that the
+ * surrounding application may again override this value.
+ * If your system doesn't support getenv(), define NO_GETENV to disable
+ * this feature.
+ */
+#ifndef NO_GETENV
+ { char * memenv;
+
+ if ((memenv = getenv("JPEGMEM")) != NULL) {
+ char ch = 'x';
+
+ if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
+ if (ch == 'm' || ch == 'M')
+ max_to_use *= 1000L;
+ mem->pub.max_memory_to_use = max_to_use * 1000L;
+ }
+ }
+ }
+#endif
+
+}
diff --git a/test/monniaux/jpeg-6b/jmemname.c b/test/monniaux/jpeg-6b/jmemname.c
new file mode 100644
index 00000000..ed96dee1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemname.c
@@ -0,0 +1,276 @@
+/*
+ * jmemname.c
+ *
+ * Copyright (C) 1992-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides a generic implementation of the system-dependent
+ * portion of the JPEG memory manager. This implementation assumes that
+ * you must explicitly construct a name for each temp file.
+ * Also, the problem of determining the amount of memory available
+ * is shoved onto the user.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+#endif
+
+#ifndef SEEK_SET /* pre-ANSI systems may not define this; */
+#define SEEK_SET 0 /* if not, assume 0 is correct */
+#endif
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#define RW_BINARY "w+"
+#else
+#ifdef VMS /* VMS is very nonstandard */
+#define READ_BINARY "rb", "ctx=stm"
+#define RW_BINARY "w+b", "ctx=stm"
+#else /* standard ANSI-compliant case */
+#define READ_BINARY "rb"
+#define RW_BINARY "w+b"
+#endif
+#endif
+
+
+/*
+ * Selection of a file name for a temporary file.
+ * This is system-dependent!
+ *
+ * The code as given is suitable for most Unix systems, and it is easily
+ * modified for most non-Unix systems. Some notes:
+ * 1. The temp file is created in the directory named by TEMP_DIRECTORY.
+ * The default value is /usr/tmp, which is the conventional place for
+ * creating large temp files on Unix. On other systems you'll probably
+ * want to change the file location. You can do this by editing the
+ * #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
+ *
+ * 2. If you need to change the file name as well as its location,
+ * you can override the TEMP_FILE_NAME macro. (Note that this is
+ * actually a printf format string; it must contain %s and %d.)
+ * Few people should need to do this.
+ *
+ * 3. mktemp() is used to ensure that multiple processes running
+ * simultaneously won't select the same file names. If your system
+ * doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
+ * (If you don't have <errno.h>, also define NO_ERRNO_H.)
+ *
+ * 4. You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
+ * will cause the temp files to be removed if you stop the program early.
+ */
+
+#ifndef TEMP_DIRECTORY /* can override from jconfig.h or Makefile */
+#define TEMP_DIRECTORY "/usr/tmp/" /* recommended setting for Unix */
+#endif
+
+static int next_file_num; /* to distinguish among several temp files */
+
+#ifdef NO_MKTEMP
+
+#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
+#define TEMP_FILE_NAME "%sJPG%03d.TMP"
+#endif
+
+#ifndef NO_ERRNO_H
+#include <errno.h> /* to define ENOENT */
+#endif
+
+/* ANSI C specifies that errno is a macro, but on older systems it's more
+ * likely to be a plain int variable. And not all versions of errno.h
+ * bother to declare it, so we have to in order to be most portable. Thus:
+ */
+#ifndef errno
+extern int errno;
+#endif
+
+
+LOCAL(void)
+select_file_name (char * fname)
+{
+ FILE * tfile;
+
+ /* Keep generating file names till we find one that's not in use */
+ for (;;) {
+ next_file_num++; /* advance counter */
+ sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
+ if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
+ /* fopen could have failed for a reason other than the file not
+ * being there; for example, file there but unreadable.
+ * If <errno.h> isn't available, then we cannot test the cause.
+ */
+#ifdef ENOENT
+ if (errno != ENOENT)
+ continue;
+#endif
+ break;
+ }
+ fclose(tfile); /* oops, it's there; close tfile & try again */
+ }
+}
+
+#else /* ! NO_MKTEMP */
+
+/* Note that mktemp() requires the initial filename to end in six X's */
+#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
+#define TEMP_FILE_NAME "%sJPG%dXXXXXX"
+#endif
+
+LOCAL(void)
+select_file_name (char * fname)
+{
+ next_file_num++; /* advance counter */
+ sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
+ mktemp(fname); /* make sure file name is unique */
+ /* mktemp replaces the trailing XXXXXX with a unique string of characters */
+}
+
+#endif /* NO_MKTEMP */
+
+
+/*
+ * Memory allocation and freeing are controlled by the regular library
+ * routines malloc() and free().
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ * It's impossible to do this in a portable way; our current solution is
+ * to make the user tell us (with a default value set at compile time).
+ * If you can actually get the available space, it's a good idea to subtract
+ * a slop factor of 5% or so.
+ */
+
+#ifndef DEFAULT_MAX_MEM /* so can override from makefile */
+#define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
+#endif
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+ long max_bytes_needed, long already_allocated)
+{
+ return cinfo->mem->max_memory_to_use - already_allocated;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Backing store objects are only used when the value returned by
+ * jpeg_mem_available is less than the total space needed. You can dispense
+ * with these routines if you have plenty of virtual memory; see jmemnobs.c.
+ */
+
+
+METHODDEF(void)
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (fseek(info->temp_file, file_offset, SEEK_SET))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ if (JFREAD(info->temp_file, buffer_address, byte_count)
+ != (size_t) byte_count)
+ ERREXIT(cinfo, JERR_TFILE_READ);
+}
+
+
+METHODDEF(void)
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count)
+{
+ if (fseek(info->temp_file, file_offset, SEEK_SET))
+ ERREXIT(cinfo, JERR_TFILE_SEEK);
+ if (JFWRITE(info->temp_file, buffer_address, byte_count)
+ != (size_t) byte_count)
+ ERREXIT(cinfo, JERR_TFILE_WRITE);
+}
+
+
+METHODDEF(void)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
+{
+ fclose(info->temp_file); /* close the file */
+ unlink(info->temp_name); /* delete the file */
+/* If your system doesn't have unlink(), use remove() instead.
+ * remove() is the ANSI-standard name for this function, but if
+ * your system was ANSI you'd be using jmemansi.c, right?
+ */
+ TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
+}
+
+
+/*
+ * Initial opening of a backing-store object.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ select_file_name(info->temp_name);
+ if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
+ ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
+ info->read_backing_store = read_backing_store;
+ info->write_backing_store = write_backing_store;
+ info->close_backing_store = close_backing_store;
+ TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+ next_file_num = 0; /* initialize temp file name generator */
+ return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+ /* no work */
+}
diff --git a/test/monniaux/jpeg-6b/jmemnobs.c b/test/monniaux/jpeg-6b/jmemnobs.c
new file mode 100644
index 00000000..eb8c3377
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemnobs.c
@@ -0,0 +1,109 @@
+/*
+ * jmemnobs.c
+ *
+ * Copyright (C) 1992-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides a really simple implementation of the system-
+ * dependent portion of the JPEG memory manager. This implementation
+ * assumes that no backing-store files are needed: all required space
+ * can be obtained from malloc().
+ * This is very portable in the sense that it'll compile on almost anything,
+ * but you'd better have lots of main memory (or virtual memory) if you want
+ * to process big images.
+ * Note that the max_memory_to_use option is ignored by this implementation.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+#endif
+
+
+/*
+ * Memory allocation and freeing are controlled by the regular library
+ * routines malloc() and free().
+ */
+
+GLOBAL(void *)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
+ */
+
+GLOBAL(void FAR *)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+ return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+ free(object);
+}
+
+
+/*
+ * This routine computes the total memory space available for allocation.
+ * Here we always say, "we got all you want bud!"
+ */
+
+GLOBAL(long)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+ long max_bytes_needed, long already_allocated)
+{
+ return max_bytes_needed;
+}
+
+
+/*
+ * Backing store (temporary file) management.
+ * Since jpeg_mem_available always promised the moon,
+ * this should never be called and we can just error out.
+ */
+
+GLOBAL(void)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+ long total_bytes_needed)
+{
+ ERREXIT(cinfo, JERR_NO_BACKING_STORE);
+}
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required. Here, there isn't any.
+ */
+
+GLOBAL(long)
+jpeg_mem_init (j_common_ptr cinfo)
+{
+ return 0; /* just set max_memory_to_use to 0 */
+}
+
+GLOBAL(void)
+jpeg_mem_term (j_common_ptr cinfo)
+{
+ /* no work */
+}
diff --git a/test/monniaux/jpeg-6b/jmemsys.h b/test/monniaux/jpeg-6b/jmemsys.h
new file mode 100644
index 00000000..6c3c6d34
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmemsys.h
@@ -0,0 +1,198 @@
+/*
+ * jmemsys.h
+ *
+ * Copyright (C) 1992-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This include file defines the interface between the system-independent
+ * and system-dependent portions of the JPEG memory manager. No other
+ * modules need include it. (The system-independent portion is jmemmgr.c;
+ * there are several different versions of the system-dependent portion.)
+ *
+ * This file works as-is for the system-dependent memory managers supplied
+ * in the IJG distribution. You may need to modify it if you write a
+ * custom memory manager. If system-dependent changes are needed in
+ * this file, the best method is to #ifdef them based on a configuration
+ * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
+ * and USE_MAC_MEMMGR.
+ */
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_get_small jGetSmall
+#define jpeg_free_small jFreeSmall
+#define jpeg_get_large jGetLarge
+#define jpeg_free_large jFreeLarge
+#define jpeg_mem_available jMemAvail
+#define jpeg_open_backing_store jOpenBackStore
+#define jpeg_mem_init jMemInit
+#define jpeg_mem_term jMemTerm
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/*
+ * These two functions are used to allocate and release small chunks of
+ * memory. (Typically the total amount requested through jpeg_get_small is
+ * no more than 20K or so; this will be requested in chunks of a few K each.)
+ * Behavior should be the same as for the standard library functions malloc
+ * and free; in particular, jpeg_get_small must return NULL on failure.
+ * On most systems, these ARE malloc and free. jpeg_free_small is passed the
+ * size of the object being freed, just in case it's needed.
+ * On an 80x86 machine using small-data memory model, these manage near heap.
+ */
+
+EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
+EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
+ size_t sizeofobject));
+
+/*
+ * These two functions are used to allocate and release large chunks of
+ * memory (up to the total free space designated by jpeg_mem_available).
+ * The interface is the same as above, except that on an 80x86 machine,
+ * far pointers are used. On most other machines these are identical to
+ * the jpeg_get/free_small routines; but we keep them separate anyway,
+ * in case a different allocation strategy is desirable for large chunks.
+ */
+
+EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
+ size_t sizeofobject));
+EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
+ size_t sizeofobject));
+
+/*
+ * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
+ * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
+ * matter, but that case should never come into play). This macro is needed
+ * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
+ * On those machines, we expect that jconfig.h will provide a proper value.
+ * On machines with 32-bit flat address spaces, any large constant may be used.
+ *
+ * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
+ * size_t and will be a multiple of sizeof(align_type).
+ */
+
+#ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */
+#define MAX_ALLOC_CHUNK 1000000000L
+#endif
+
+/*
+ * This routine computes the total space still available for allocation by
+ * jpeg_get_large. If more space than this is needed, backing store will be
+ * used. NOTE: any memory already allocated must not be counted.
+ *
+ * There is a minimum space requirement, corresponding to the minimum
+ * feasible buffer sizes; jmemmgr.c will request that much space even if
+ * jpeg_mem_available returns zero. The maximum space needed, enough to hold
+ * all working storage in memory, is also passed in case it is useful.
+ * Finally, the total space already allocated is passed. If no better
+ * method is available, cinfo->mem->max_memory_to_use - already_allocated
+ * is often a suitable calculation.
+ *
+ * It is OK for jpeg_mem_available to underestimate the space available
+ * (that'll just lead to more backing-store access than is really necessary).
+ * However, an overestimate will lead to failure. Hence it's wise to subtract
+ * a slop factor from the true available space. 5% should be enough.
+ *
+ * On machines with lots of virtual memory, any large constant may be returned.
+ * Conversely, zero may be returned to always use the minimum amount of memory.
+ */
+
+EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
+ long min_bytes_needed,
+ long max_bytes_needed,
+ long already_allocated));
+
+
+/*
+ * This structure holds whatever state is needed to access a single
+ * backing-store object. The read/write/close method pointers are called
+ * by jmemmgr.c to manipulate the backing-store object; all other fields
+ * are private to the system-dependent backing store routines.
+ */
+
+#define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */
+
+
+#ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */
+
+typedef unsigned short XMSH; /* type of extended-memory handles */
+typedef unsigned short EMSH; /* type of expanded-memory handles */
+
+typedef union {
+ short file_handle; /* DOS file handle if it's a temp file */
+ XMSH xms_handle; /* handle if it's a chunk of XMS */
+ EMSH ems_handle; /* handle if it's a chunk of EMS */
+} handle_union;
+
+#endif /* USE_MSDOS_MEMMGR */
+
+#ifdef USE_MAC_MEMMGR /* Mac-specific junk */
+#include <Files.h>
+#endif /* USE_MAC_MEMMGR */
+
+
+typedef struct backing_store_struct * backing_store_ptr;
+
+typedef struct backing_store_struct {
+ /* Methods for reading/writing/closing this backing-store object */
+ JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count));
+ JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count));
+ JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info));
+
+ /* Private fields for system-dependent backing-store management */
+#ifdef USE_MSDOS_MEMMGR
+ /* For the MS-DOS manager (jmemdos.c), we need: */
+ handle_union handle; /* reference to backing-store storage object */
+ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
+#else
+#ifdef USE_MAC_MEMMGR
+ /* For the Mac manager (jmemmac.c), we need: */
+ short temp_file; /* file reference number to temp file */
+ FSSpec tempSpec; /* the FSSpec for the temp file */
+ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
+#else
+ /* For a typical implementation with temp files, we need: */
+ FILE * temp_file; /* stdio reference to temp file */
+ char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
+#endif
+#endif
+} backing_store_info;
+
+
+/*
+ * Initial opening of a backing-store object. This must fill in the
+ * read/write/close pointers in the object. The read/write routines
+ * may take an error exit if the specified maximum file size is exceeded.
+ * (If jpeg_mem_available always returns a large value, this routine can
+ * just take an error exit.)
+ */
+
+EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
+ backing_store_ptr info,
+ long total_bytes_needed));
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required. jpeg_mem_init will be called before anything is
+ * allocated (and, therefore, nothing in cinfo is of use except the error
+ * manager pointer). It should return a suitable default value for
+ * max_memory_to_use; this may subsequently be overridden by the surrounding
+ * application. (Note that max_memory_to_use is only important if
+ * jpeg_mem_available chooses to consult it ... no one else will.)
+ * jpeg_mem_term may assume that all requested memory has been freed and that
+ * all opened backing-store objects have been closed.
+ */
+
+EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
+EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
diff --git a/test/monniaux/jpeg-6b/jmorecfg.h b/test/monniaux/jpeg-6b/jmorecfg.h
new file mode 100644
index 00000000..54a7d1c4
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jmorecfg.h
@@ -0,0 +1,363 @@
+/*
+ * jmorecfg.h
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains additional configuration options that customize the
+ * JPEG software for special applications or support machine-dependent
+ * optimizations. Most users will not need to touch this file.
+ */
+
+
+/*
+ * Define BITS_IN_JSAMPLE as either
+ * 8 for 8-bit sample values (the usual setting)
+ * 12 for 12-bit sample values
+ * Only 8 and 12 are legal data precisions for lossy JPEG according to the
+ * JPEG standard, and the IJG code does not support anything else!
+ * We do not support run-time selection of data precision, sorry.
+ */
+
+#define BITS_IN_JSAMPLE 8 /* use 8 or 12 */
+
+
+/*
+ * Maximum number of components (color channels) allowed in JPEG image.
+ * To meet the letter of the JPEG spec, set this to 255. However, darn
+ * few applications need more than 4 channels (maybe 5 for CMYK + alpha
+ * mask). We recommend 10 as a reasonable compromise; use 4 if you are
+ * really short on memory. (Each allowed component costs a hundred or so
+ * bytes of storage, whether actually used in an image or not.)
+ */
+
+#define MAX_COMPONENTS 10 /* maximum number of image components */
+
+
+/*
+ * Basic data types.
+ * You may need to change these if you have a machine with unusual data
+ * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
+ * or "long" not 32 bits. We don't care whether "int" is 16 or 32 bits,
+ * but it had better be at least 16.
+ */
+
+/* Representation of a single sample (pixel element value).
+ * We frequently allocate large arrays of these, so it's important to keep
+ * them small. But if you have memory to burn and access to char or short
+ * arrays is very slow on your hardware, you might want to change these.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+/* JSAMPLE should be the smallest type that will hold the values 0..255.
+ * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JSAMPLE;
+#define GETJSAMPLE(value) ((int) (value))
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JSAMPLE;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJSAMPLE(value) ((int) (value))
+#else
+#define GETJSAMPLE(value) ((int) (value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+#define MAXJSAMPLE 255
+#define CENTERJSAMPLE 128
+
+#endif /* BITS_IN_JSAMPLE == 8 */
+
+
+#if BITS_IN_JSAMPLE == 12
+/* JSAMPLE should be the smallest type that will hold the values 0..4095.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value) ((int) (value))
+
+#define MAXJSAMPLE 4095
+#define CENTERJSAMPLE 2048
+
+#endif /* BITS_IN_JSAMPLE == 12 */
+
+
+/* Representation of a DCT frequency coefficient.
+ * This should be a signed value of at least 16 bits; "short" is usually OK.
+ * Again, we allocate large arrays of these, but you can change to int
+ * if you have memory to burn and "short" is really slow.
+ */
+
+typedef short JCOEF;
+
+
+/* Compressed datastreams are represented as arrays of JOCTET.
+ * These must be EXACTLY 8 bits wide, at least once they are written to
+ * external storage. Note that when using the stdio data source/destination
+ * managers, this is also the data type passed to fread/fwrite.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JOCTET;
+#define GETJOCTET(value) (value)
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JOCTET;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJOCTET(value) (value)
+#else
+#define GETJOCTET(value) ((value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+/* These typedefs are used for various table entries and so forth.
+ * They must be at least as wide as specified; but making them too big
+ * won't cost a huge amount of memory, so we don't provide special
+ * extraction code like we did for JSAMPLE. (In other words, these
+ * typedefs live at a different point on the speed/space tradeoff curve.)
+ */
+
+/* UINT8 must hold at least the values 0..255. */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char UINT8;
+#else /* not HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char UINT8;
+#else /* not CHAR_IS_UNSIGNED */
+typedef short UINT8;
+#endif /* CHAR_IS_UNSIGNED */
+#endif /* HAVE_UNSIGNED_CHAR */
+
+/* UINT16 must hold at least the values 0..65535. */
+
+#ifdef HAVE_UNSIGNED_SHORT
+typedef unsigned short UINT16;
+#else /* not HAVE_UNSIGNED_SHORT */
+typedef unsigned int UINT16;
+#endif /* HAVE_UNSIGNED_SHORT */
+
+/* INT16 must hold at least the values -32768..32767. */
+
+#ifndef XMD_H /* X11/xmd.h correctly defines INT16 */
+typedef short INT16;
+#endif
+
+/* INT32 must hold at least signed 32-bit values. */
+
+#ifndef XMD_H /* X11/xmd.h correctly defines INT32 */
+typedef long INT32;
+#endif
+
+/* Datatype used for image dimensions. The JPEG standard only supports
+ * images up to 64K*64K due to 16-bit fields in SOF markers. Therefore
+ * "unsigned int" is sufficient on all machines. However, if you need to
+ * handle larger images and you don't mind deviating from the spec, you
+ * can change this datatype.
+ */
+
+typedef unsigned int JDIMENSION;
+
+#define JPEG_MAX_DIMENSION 65500L /* a tad under 64K to prevent overflows */
+
+
+/* These macros are used in all function definitions and extern declarations.
+ * You could modify them if you need to change function linkage conventions;
+ * in particular, you'll need to do that to make the library a Windows DLL.
+ * Another application is to make all functions global for use with debuggers
+ * or code profilers that require it.
+ */
+
+/* a function called through method pointers: */
+#define METHODDEF(type) static type
+/* a function used only in its module: */
+#define LOCAL(type) static type
+/* a function referenced thru EXTERNs: */
+#define GLOBAL(type) type
+/* a reference to a GLOBAL function: */
+#define EXTERN(type) extern type
+
+
+/* This macro is used to declare a "method", that is, a function pointer.
+ * We want to supply prototype parameters if the compiler can cope.
+ * Note that the arglist parameter must be parenthesized!
+ * Again, you can customize this if you need special linkage keywords.
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JMETHOD(type,methodname,arglist) type (*methodname) arglist
+#else
+#define JMETHOD(type,methodname,arglist) type (*methodname) ()
+#endif
+
+
+/* Here is the pseudo-keyword for declaring pointers that must be "far"
+ * on 80x86 machines. Most of the specialized coding for 80x86 is handled
+ * by just saying "FAR *" where such a pointer is needed. In a few places
+ * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
+ */
+
+#ifdef NEED_FAR_POINTERS
+#define FAR far
+#else
+#define FAR
+#endif
+
+
+/*
+ * On a few systems, type boolean and/or its values FALSE, TRUE may appear
+ * in standard header files. Or you may have conflicts with application-
+ * specific header files that you want to include together with these files.
+ * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
+ */
+
+#ifndef HAVE_BOOLEAN
+typedef int boolean;
+#endif
+#ifndef FALSE /* in case these macros already exist */
+#define FALSE 0 /* values of boolean */
+#endif
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+
+/*
+ * The remaining options affect code selection within the JPEG library,
+ * but they don't need to be visible to most applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+#define JPEG_INTERNAL_OPTIONS
+#endif
+
+#ifdef JPEG_INTERNAL_OPTIONS
+
+
+/*
+ * These defines indicate whether to include various optional functions.
+ * Undefining some of these symbols will produce a smaller but less capable
+ * library. Note that you can leave certain source files out of the
+ * compilation/linking process if you've #undef'd the corresponding symbols.
+ * (You may HAVE to do that if your compiler doesn't like null source files.)
+ */
+
+/* Arithmetic coding is unsupported for legal reasons. Complaints to IBM. */
+
+/* Capability options common to encoder and decoder: */
+
+#define DCT_ISLOW_SUPPORTED /* slow but accurate integer algorithm */
+#define DCT_IFAST_SUPPORTED /* faster, less accurate integer method */
+#define DCT_FLOAT_SUPPORTED /* floating-point: accurate, fast on fast HW */
+
+/* Encoder capability options: */
+
+#undef C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
+#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
+#define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
+#define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */
+/* Note: if you selected 12-bit data precision, it is dangerous to turn off
+ * ENTROPY_OPT_SUPPORTED. The standard Huffman tables are only good for 8-bit
+ * precision, so jchuff.c normally uses entropy optimization to compute
+ * usable tables for higher precision. If you don't want to do optimization,
+ * you'll have to supply different default Huffman tables.
+ * The exact same statements apply for progressive JPEG: the default tables
+ * don't work for progressive mode. (This may get fixed, however.)
+ */
+#define INPUT_SMOOTHING_SUPPORTED /* Input image smoothing option? */
+
+/* Decoder capability options: */
+
+#undef D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
+#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
+#define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
+#define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */
+#define BLOCK_SMOOTHING_SUPPORTED /* Block smoothing? (Progressive only) */
+#define IDCT_SCALING_SUPPORTED /* Output rescaling via IDCT? */
+#undef UPSAMPLE_SCALING_SUPPORTED /* Output rescaling at upsample stage? */
+#define UPSAMPLE_MERGING_SUPPORTED /* Fast path for sloppy upsampling? */
+#define QUANT_1PASS_SUPPORTED /* 1-pass color quantization? */
+#define QUANT_2PASS_SUPPORTED /* 2-pass color quantization? */
+
+/* more capability options later, no doubt */
+
+
+/*
+ * Ordering of RGB data in scanlines passed to or from the application.
+ * If your application wants to deal with data in the order B,G,R, just
+ * change these macros. You can also deal with formats such as R,G,B,X
+ * (one extra byte per pixel) by changing RGB_PIXELSIZE. Note that changing
+ * the offsets will also change the order in which colormap data is organized.
+ * RESTRICTIONS:
+ * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
+ * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
+ * useful if you are using JPEG color spaces other than YCbCr or grayscale.
+ * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
+ * is not 3 (they don't understand about dummy color components!). So you
+ * can't use color quantization if you change that value.
+ */
+
+#define RGB_RED 0 /* Offset of Red in an RGB scanline element */
+#define RGB_GREEN 1 /* Offset of Green */
+#define RGB_BLUE 2 /* Offset of Blue */
+#define RGB_PIXELSIZE 3 /* JSAMPLEs per RGB scanline element */
+
+
+/* Definitions for speed-related optimizations. */
+
+
+/* If your compiler supports inline functions, define INLINE
+ * as the inline keyword; otherwise define it as empty.
+ */
+
+#ifndef INLINE
+#ifdef __GNUC__ /* for instance, GNU C knows about inline */
+#define INLINE __inline__
+#endif
+#ifndef INLINE
+#define INLINE /* default is to define it as empty */
+#endif
+#endif
+
+
+/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
+ * two 16-bit shorts is faster than multiplying two ints. Define MULTIPLIER
+ * as short on such a machine. MULTIPLIER must be at least 16 bits wide.
+ */
+
+#ifndef MULTIPLIER
+#define MULTIPLIER int /* type for fastest integer multiply */
+#endif
+
+
+/* FAST_FLOAT should be either float or double, whichever is done faster
+ * by your compiler. (Note that this type is only used in the floating point
+ * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
+ * Typically, float is faster in ANSI C compilers, while double is faster in
+ * pre-ANSI compilers (because they insist on converting to double anyway).
+ * The code below therefore chooses float if we have ANSI-style prototypes.
+ */
+
+#ifndef FAST_FLOAT
+#ifdef HAVE_PROTOTYPES
+#define FAST_FLOAT float
+#else
+#define FAST_FLOAT double
+#endif
+#endif
+
+#endif /* JPEG_INTERNAL_OPTIONS */
diff --git a/test/monniaux/jpeg-6b/jpegint.h b/test/monniaux/jpeg-6b/jpegint.h
new file mode 100644
index 00000000..95b00d40
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jpegint.h
@@ -0,0 +1,392 @@
+/*
+ * jpegint.h
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides common declarations for the various JPEG modules.
+ * These declarations are considered internal to the JPEG library; most
+ * applications using the library shouldn't need to include this file.
+ */
+
+
+/* Declarations for both compression & decompression */
+
+typedef enum { /* Operating modes for buffer controllers */
+ JBUF_PASS_THRU, /* Plain stripwise operation */
+ /* Remaining modes require a full-image buffer to have been created */
+ JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
+ JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
+ JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
+} J_BUF_MODE;
+
+/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
+#define CSTATE_START 100 /* after create_compress */
+#define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
+#define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
+#define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
+#define DSTATE_START 200 /* after create_decompress */
+#define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
+#define DSTATE_READY 202 /* found SOS, ready for start_decompress */
+#define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
+#define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
+#define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
+#define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
+#define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
+#define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
+#define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
+#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
+
+
+/* Declarations for compression modules */
+
+/* Master control module */
+struct jpeg_comp_master {
+ JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
+ JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean call_pass_startup; /* True if pass_startup must be called */
+ boolean is_last_pass; /* True during last pass */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_c_main_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, process_data, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail));
+};
+
+/* Compression preprocessing (downsampling input buffer control) */
+struct jpeg_c_prep_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf,
+ JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail));
+};
+
+/* Coefficient buffer control */
+struct jpeg_c_coef_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf));
+};
+
+/* Colorspace conversion */
+struct jpeg_color_converter {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, color_convert, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows));
+};
+
+/* Downsampling */
+struct jpeg_downsampler {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, downsample, (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+ JSAMPIMAGE output_buf,
+ JDIMENSION out_row_group_index));
+
+ boolean need_context_rows; /* TRUE if need rows above & below */
+};
+
+/* Forward DCT (also controls coefficient quantization) */
+struct jpeg_forward_dct {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ /* perhaps this should be an array??? */
+ JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
+ jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks));
+};
+
+/* Entropy encoding */
+struct jpeg_entropy_encoder {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
+ JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
+ JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+};
+
+/* Marker writing */
+struct jpeg_marker_writer {
+ JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
+ JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
+ /* These routines are exported to allow insertion of extra markers */
+ /* Probably only COM and APPn markers should be written this way */
+ JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
+ unsigned int datalen));
+ JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
+};
+
+
+/* Declarations for decompression modules */
+
+/* Master control module */
+struct jpeg_decomp_master {
+ JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
+};
+
+/* Input control module */
+struct jpeg_input_controller {
+ JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
+ JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
+ JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean has_multiple_scans; /* True if file has multiple scans */
+ boolean eoi_reached; /* True when EOI has been consumed */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_d_main_controller {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, process_data, (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+};
+
+/* Coefficient buffer control */
+struct jpeg_d_coef_controller {
+ JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
+ JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
+ JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
+ JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
+ JSAMPIMAGE output_buf));
+ /* Pointer to array of coefficient virtual arrays, or NULL if none */
+ jvirt_barray_ptr *coef_arrays;
+};
+
+/* Decompression postprocessing (color quantization buffer control) */
+struct jpeg_d_post_controller {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf,
+ JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+};
+
+/* Marker reading & parsing */
+struct jpeg_marker_reader {
+ JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
+ /* Read markers until SOS or EOI.
+ * Returns same codes as are defined for jpeg_consume_input:
+ * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ */
+ JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
+ /* Read a restart marker --- exported for use by entropy decoder only */
+ jpeg_marker_parser_method read_restart_marker;
+
+ /* State of marker reader --- nominally internal, but applications
+ * supplying COM or APPn handlers might like to know the state.
+ */
+ boolean saw_SOI; /* found SOI? */
+ boolean saw_SOF; /* found SOF? */
+ int next_restart_num; /* next restart number expected (0-7) */
+ unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
+};
+
+/* Entropy decoding */
+struct jpeg_entropy_decoder {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+
+ /* This is here to share code between baseline and progressive decoders; */
+ /* other modules probably should not use it */
+ boolean insufficient_data; /* set TRUE after emitting warning */
+};
+
+/* Inverse DCT (also performs dequantization) */
+typedef JMETHOD(void, inverse_DCT_method_ptr,
+ (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col));
+
+struct jpeg_inverse_dct {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ /* It is useful to allow each component to have a separate IDCT method. */
+ inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
+};
+
+/* Upsampling (note that upsampler must also call color converter) */
+struct jpeg_upsampler {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, upsample, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf,
+ JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+
+ boolean need_context_rows; /* TRUE if need rows above & below */
+};
+
+/* Colorspace conversion */
+struct jpeg_color_deconverter {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows));
+};
+
+/* Color quantization or color precision reduction */
+struct jpeg_color_quantizer {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
+ JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPARRAY output_buf,
+ int num_rows));
+ JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
+};
+
+
+/* Miscellaneous useful macros */
+
+#undef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#undef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
+
+/* We assume that right shift corresponds to signed division by 2 with
+ * rounding towards minus infinity. This is correct for typical "arithmetic
+ * shift" instructions that shift in copies of the sign bit. But some
+ * C compilers implement >> with an unsigned shift. For these machines you
+ * must define RIGHT_SHIFT_IS_UNSIGNED.
+ * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
+ * It is only applied with constant shift counts. SHIFT_TEMPS must be
+ * included in the variables of any routine using RIGHT_SHIFT.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define SHIFT_TEMPS INT32 shift_temp;
+#define RIGHT_SHIFT(x,shft) \
+ ((shift_temp = (x)) < 0 ? \
+ (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
+ (shift_temp >> (shft)))
+#else
+#define SHIFT_TEMPS
+#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_compress_master jICompress
+#define jinit_c_master_control jICMaster
+#define jinit_c_main_controller jICMainC
+#define jinit_c_prep_controller jICPrepC
+#define jinit_c_coef_controller jICCoefC
+#define jinit_color_converter jICColor
+#define jinit_downsampler jIDownsampler
+#define jinit_forward_dct jIFDCT
+#define jinit_huff_encoder jIHEncoder
+#define jinit_phuff_encoder jIPHEncoder
+#define jinit_marker_writer jIMWriter
+#define jinit_master_decompress jIDMaster
+#define jinit_d_main_controller jIDMainC
+#define jinit_d_coef_controller jIDCoefC
+#define jinit_d_post_controller jIDPostC
+#define jinit_input_controller jIInCtlr
+#define jinit_marker_reader jIMReader
+#define jinit_huff_decoder jIHDecoder
+#define jinit_phuff_decoder jIPHDecoder
+#define jinit_inverse_dct jIIDCT
+#define jinit_upsampler jIUpsampler
+#define jinit_color_deconverter jIDColor
+#define jinit_1pass_quantizer jI1Quant
+#define jinit_2pass_quantizer jI2Quant
+#define jinit_merged_upsampler jIMUpsampler
+#define jinit_memory_mgr jIMemMgr
+#define jdiv_round_up jDivRound
+#define jround_up jRound
+#define jcopy_sample_rows jCopySamples
+#define jcopy_block_row jCopyBlocks
+#define jzero_far jZeroFar
+#define jpeg_zigzag_order jZIGTable
+#define jpeg_natural_order jZAGTable
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Compression module initialization routines */
+EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
+ boolean transcode_only));
+EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
+/* Decompression module initialization routines */
+EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
+/* Memory manager initialization */
+EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
+
+/* Utility routines in jutils.c */
+EXTERN(long) jdiv_round_up JPP((long a, long b));
+EXTERN(long) jround_up JPP((long a, long b));
+EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
+ JSAMPARRAY output_array, int dest_row,
+ int num_rows, JDIMENSION num_cols));
+EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
+ JDIMENSION num_blocks));
+EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
+/* Constant tables in jutils.c */
+#if 0 /* This table is not actually needed in v6a */
+extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
+#endif
+extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
+
+/* Suppress undefined-structure complaints if necessary. */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+#endif
+#endif /* INCOMPLETE_TYPES_BROKEN */
diff --git a/test/monniaux/jpeg-6b/jpeglib.h b/test/monniaux/jpeg-6b/jpeglib.h
new file mode 100644
index 00000000..d1be8dde
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jpeglib.h
@@ -0,0 +1,1096 @@
+/*
+ * jpeglib.h
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the application interface for the JPEG library.
+ * Most applications using the library need only include this file,
+ * and perhaps jerror.h if they want to know the exact error codes.
+ */
+
+#ifndef JPEGLIB_H
+#define JPEGLIB_H
+
+/*
+ * First we include the configuration files that record how this
+ * installation of the JPEG library is set up. jconfig.h can be
+ * generated automatically for many systems. jmorecfg.h contains
+ * manual configuration options that most people need not worry about.
+ */
+
+#ifndef JCONFIG_INCLUDED /* in case jinclude.h already did */
+#include "jconfig.h" /* widely used configuration options */
+#endif
+#include "jmorecfg.h" /* seldom changed options */
+
+
+/* Version ID for the JPEG library.
+ * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
+ */
+
+#define JPEG_LIB_VERSION 62 /* Version 6b */
+
+
+/* Various constants determining the sizes of things.
+ * All of these are specified by the JPEG standard, so don't change them
+ * if you want to be compatible.
+ */
+
+#define DCTSIZE 8 /* The basic DCT block is 8x8 samples */
+#define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */
+#define NUM_QUANT_TBLS 4 /* Quantization tables are numbered 0..3 */
+#define NUM_HUFF_TBLS 4 /* Huffman tables are numbered 0..3 */
+#define NUM_ARITH_TBLS 16 /* Arith-coding tables are numbered 0..15 */
+#define MAX_COMPS_IN_SCAN 4 /* JPEG limit on # of components in one scan */
+#define MAX_SAMP_FACTOR 4 /* JPEG limit on sampling factors */
+/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard;
+ * the PostScript DCT filter can emit files with many more than 10 blocks/MCU.
+ * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU
+ * to handle it. We even let you do this from the jconfig.h file. However,
+ * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe
+ * sometimes emits noncompliant files doesn't mean you should too.
+ */
+#define C_MAX_BLOCKS_IN_MCU 10 /* compressor's limit on blocks per MCU */
+#ifndef D_MAX_BLOCKS_IN_MCU
+#define D_MAX_BLOCKS_IN_MCU 10 /* decompressor's limit on blocks per MCU */
+#endif
+
+
+/* Data structures for images (arrays of samples and of DCT coefficients).
+ * On 80x86 machines, the image arrays are too big for near pointers,
+ * but the pointer arrays can fit in near memory.
+ */
+
+typedef JSAMPLE FAR *JSAMPROW; /* ptr to one image row of pixel samples. */
+typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */
+typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
+
+typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */
+typedef JBLOCK FAR *JBLOCKROW; /* pointer to one row of coefficient blocks */
+typedef JBLOCKROW *JBLOCKARRAY; /* a 2-D array of coefficient blocks */
+typedef JBLOCKARRAY *JBLOCKIMAGE; /* a 3-D array of coefficient blocks */
+
+typedef JCOEF FAR *JCOEFPTR; /* useful in a couple of places */
+
+
+/* Types for JPEG compression parameters and working tables. */
+
+
+/* DCT coefficient quantization tables. */
+
+typedef struct {
+ /* This array gives the coefficient quantizers in natural array order
+ * (not the zigzag order in which they are stored in a JPEG DQT marker).
+ * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
+ */
+ UINT16 quantval[DCTSIZE2]; /* quantization step for each coefficient */
+ /* This field is used only during compression. It's initialized FALSE when
+ * the table is created, and set TRUE when it's been output to the file.
+ * You could suppress output of a table by setting this to TRUE.
+ * (See jpeg_suppress_tables for an example.)
+ */
+ boolean sent_table; /* TRUE when table has been output */
+} JQUANT_TBL;
+
+
+/* Huffman coding tables. */
+
+typedef struct {
+ /* These two fields directly represent the contents of a JPEG DHT marker */
+ UINT8 bits[17]; /* bits[k] = # of symbols with codes of */
+ /* length k bits; bits[0] is unused */
+ UINT8 huffval[256]; /* The symbols, in order of incr code length */
+ /* This field is used only during compression. It's initialized FALSE when
+ * the table is created, and set TRUE when it's been output to the file.
+ * You could suppress output of a table by setting this to TRUE.
+ * (See jpeg_suppress_tables for an example.)
+ */
+ boolean sent_table; /* TRUE when table has been output */
+} JHUFF_TBL;
+
+
+/* Basic info about one component (color channel). */
+
+typedef struct {
+ /* These values are fixed over the whole image. */
+ /* For compression, they must be supplied by parameter setup; */
+ /* for decompression, they are read from the SOF marker. */
+ int component_id; /* identifier for this component (0..255) */
+ int component_index; /* its index in SOF or cinfo->comp_info[] */
+ int h_samp_factor; /* horizontal sampling factor (1..4) */
+ int v_samp_factor; /* vertical sampling factor (1..4) */
+ int quant_tbl_no; /* quantization table selector (0..3) */
+ /* These values may vary between scans. */
+ /* For compression, they must be supplied by parameter setup; */
+ /* for decompression, they are read from the SOS marker. */
+ /* The decompressor output side may not use these variables. */
+ int dc_tbl_no; /* DC entropy table selector (0..3) */
+ int ac_tbl_no; /* AC entropy table selector (0..3) */
+
+ /* Remaining fields should be treated as private by applications. */
+
+ /* These values are computed during compression or decompression startup: */
+ /* Component's size in DCT blocks.
+ * Any dummy blocks added to complete an MCU are not counted; therefore
+ * these values do not depend on whether a scan is interleaved or not.
+ */
+ JDIMENSION width_in_blocks;
+ JDIMENSION height_in_blocks;
+ /* Size of a DCT block in samples. Always DCTSIZE for compression.
+ * For decompression this is the size of the output from one DCT block,
+ * reflecting any scaling we choose to apply during the IDCT step.
+ * Values of 1,2,4,8 are likely to be supported. Note that different
+ * components may receive different IDCT scalings.
+ */
+ int DCT_scaled_size;
+ /* The downsampled dimensions are the component's actual, unpadded number
+ * of samples at the main buffer (preprocessing/compression interface), thus
+ * downsampled_width = ceil(image_width * Hi/Hmax)
+ * and similarly for height. For decompression, IDCT scaling is included, so
+ * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
+ */
+ JDIMENSION downsampled_width; /* actual width in samples */
+ JDIMENSION downsampled_height; /* actual height in samples */
+ /* This flag is used only for decompression. In cases where some of the
+ * components will be ignored (eg grayscale output from YCbCr image),
+ * we can skip most computations for the unused components.
+ */
+ boolean component_needed; /* do we need the value of this component? */
+
+ /* These values are computed before starting a scan of the component. */
+ /* The decompressor output side may not use these variables. */
+ int MCU_width; /* number of blocks per MCU, horizontally */
+ int MCU_height; /* number of blocks per MCU, vertically */
+ int MCU_blocks; /* MCU_width * MCU_height */
+ int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_scaled_size */
+ int last_col_width; /* # of non-dummy blocks across in last MCU */
+ int last_row_height; /* # of non-dummy blocks down in last MCU */
+
+ /* Saved quantization table for component; NULL if none yet saved.
+ * See jdinput.c comments about the need for this information.
+ * This field is currently used only for decompression.
+ */
+ JQUANT_TBL * quant_table;
+
+ /* Private per-component storage for DCT or IDCT subsystem. */
+ void * dct_table;
+} jpeg_component_info;
+
+
+/* The script for encoding a multiple-scan file is an array of these: */
+
+typedef struct {
+ int comps_in_scan; /* number of components encoded in this scan */
+ int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
+ int Ss, Se; /* progressive JPEG spectral selection parms */
+ int Ah, Al; /* progressive JPEG successive approx. parms */
+} jpeg_scan_info;
+
+/* The decompressor can save APPn and COM markers in a list of these: */
+
+typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr;
+
+struct jpeg_marker_struct {
+ jpeg_saved_marker_ptr next; /* next in list, or NULL */
+ UINT8 marker; /* marker code: JPEG_COM, or JPEG_APP0+n */
+ unsigned int original_length; /* # bytes of data in the file */
+ unsigned int data_length; /* # bytes of data saved at data[] */
+ JOCTET FAR * data; /* the data contained in the marker */
+ /* the marker length word is not counted in data_length or original_length */
+};
+
+/* Known color spaces. */
+
+typedef enum {
+ JCS_UNKNOWN, /* error/unspecified */
+ JCS_GRAYSCALE, /* monochrome */
+ JCS_RGB, /* red/green/blue */
+ JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */
+ JCS_CMYK, /* C/M/Y/K */
+ JCS_YCCK /* Y/Cb/Cr/K */
+} J_COLOR_SPACE;
+
+/* DCT/IDCT algorithm options. */
+
+typedef enum {
+ JDCT_ISLOW, /* slow but accurate integer algorithm */
+ JDCT_IFAST, /* faster, less accurate integer method */
+ JDCT_FLOAT /* floating-point: accurate, fast on fast HW */
+} J_DCT_METHOD;
+
+#ifndef JDCT_DEFAULT /* may be overridden in jconfig.h */
+#define JDCT_DEFAULT JDCT_ISLOW
+#endif
+#ifndef JDCT_FASTEST /* may be overridden in jconfig.h */
+#define JDCT_FASTEST JDCT_IFAST
+#endif
+
+/* Dithering options for decompression. */
+
+typedef enum {
+ JDITHER_NONE, /* no dithering */
+ JDITHER_ORDERED, /* simple ordered dither */
+ JDITHER_FS /* Floyd-Steinberg error diffusion dither */
+} J_DITHER_MODE;
+
+
+/* Common fields between JPEG compression and decompression master structs. */
+
+#define jpeg_common_fields \
+ struct jpeg_error_mgr * err; /* Error handler module */\
+ struct jpeg_memory_mgr * mem; /* Memory manager module */\
+ struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
+ void * client_data; /* Available for use by application */\
+ boolean is_decompressor; /* So common code can tell which is which */\
+ int global_state /* For checking call sequence validity */
+
+/* Routines that are to be used by both halves of the library are declared
+ * to receive a pointer to this structure. There are no actual instances of
+ * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
+ */
+struct jpeg_common_struct {
+ jpeg_common_fields; /* Fields common to both master struct types */
+ /* Additional fields follow in an actual jpeg_compress_struct or
+ * jpeg_decompress_struct. All three structs must agree on these
+ * initial fields! (This would be a lot cleaner in C++.)
+ */
+};
+
+typedef struct jpeg_common_struct * j_common_ptr;
+typedef struct jpeg_compress_struct * j_compress_ptr;
+typedef struct jpeg_decompress_struct * j_decompress_ptr;
+
+
+/* Master record for a compression instance */
+
+struct jpeg_compress_struct {
+ jpeg_common_fields; /* Fields shared with jpeg_decompress_struct */
+
+ /* Destination for compressed data */
+ struct jpeg_destination_mgr * dest;
+
+ /* Description of source image --- these fields must be filled in by
+ * outer application before starting compression. in_color_space must
+ * be correct before you can even call jpeg_set_defaults().
+ */
+
+ JDIMENSION image_width; /* input image width */
+ JDIMENSION image_height; /* input image height */
+ int input_components; /* # of color components in input image */
+ J_COLOR_SPACE in_color_space; /* colorspace of input image */
+
+ double input_gamma; /* image gamma of input image */
+
+ /* Compression parameters --- these fields must be set before calling
+ * jpeg_start_compress(). We recommend calling jpeg_set_defaults() to
+ * initialize everything to reasonable defaults, then changing anything
+ * the application specifically wants to change. That way you won't get
+ * burnt when new parameters are added. Also note that there are several
+ * helper routines to simplify changing parameters.
+ */
+
+ int data_precision; /* bits of precision in image data */
+
+ int num_components; /* # of color components in JPEG image */
+ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+ jpeg_component_info * comp_info;
+ /* comp_info[i] describes component that appears i'th in SOF */
+
+ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+ /* ptrs to coefficient quantization tables, or NULL if not defined */
+
+ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ /* ptrs to Huffman coding tables, or NULL if not defined */
+
+ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+ int num_scans; /* # of entries in scan_info array */
+ const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */
+ /* The default value of scan_info is NULL, which causes a single-scan
+ * sequential JPEG file to be emitted. To create a multi-scan file,
+ * set num_scans and scan_info to point to an array of scan definitions.
+ */
+
+ boolean raw_data_in; /* TRUE=caller supplies downsampled data */
+ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
+ boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
+ boolean CCIR601_sampling; /* TRUE=first samples are cosited */
+ int smoothing_factor; /* 1..100, or 0 for no input smoothing */
+ J_DCT_METHOD dct_method; /* DCT algorithm selector */
+
+ /* The restart interval can be specified in absolute MCUs by setting
+ * restart_interval, or in MCU rows by setting restart_in_rows
+ * (in which case the correct restart_interval will be figured
+ * for each scan).
+ */
+ unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
+ int restart_in_rows; /* if > 0, MCU rows per restart interval */
+
+ /* Parameters controlling emission of special markers. */
+
+ boolean write_JFIF_header; /* should a JFIF marker be written? */
+ UINT8 JFIF_major_version; /* What to write for the JFIF version number */
+ UINT8 JFIF_minor_version;
+ /* These three values are not used by the JPEG code, merely copied */
+ /* into the JFIF APP0 marker. density_unit can be 0 for unknown, */
+ /* 1 for dots/inch, or 2 for dots/cm. Note that the pixel aspect */
+ /* ratio is defined by X_density/Y_density even when density_unit=0. */
+ UINT8 density_unit; /* JFIF code for pixel size units */
+ UINT16 X_density; /* Horizontal pixel density */
+ UINT16 Y_density; /* Vertical pixel density */
+ boolean write_Adobe_marker; /* should an Adobe marker be written? */
+
+ /* State variable: index of next scanline to be written to
+ * jpeg_write_scanlines(). Application may use this to control its
+ * processing loop, e.g., "while (next_scanline < image_height)".
+ */
+
+ JDIMENSION next_scanline; /* 0 .. image_height-1 */
+
+ /* Remaining fields are known throughout compressor, but generally
+ * should not be touched by a surrounding application.
+ */
+
+ /*
+ * These fields are computed during compression startup
+ */
+ boolean progressive_mode; /* TRUE if scan script uses progressive mode */
+ int max_h_samp_factor; /* largest h_samp_factor */
+ int max_v_samp_factor; /* largest v_samp_factor */
+
+ JDIMENSION total_iMCU_rows; /* # of iMCU rows to be input to coef ctlr */
+ /* The coefficient controller receives data in units of MCU rows as defined
+ * for fully interleaved scans (whether the JPEG file is interleaved or not).
+ * There are v_samp_factor * DCTSIZE sample rows of each component in an
+ * "iMCU" (interleaved MCU) row.
+ */
+
+ /*
+ * These fields are valid during any one scan.
+ * They describe the components and MCUs actually appearing in the scan.
+ */
+ int comps_in_scan; /* # of JPEG components in this scan */
+ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+ /* *cur_comp_info[i] describes component that appears i'th in SOS */
+
+ JDIMENSION MCUs_per_row; /* # of MCUs across the image */
+ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
+
+ int blocks_in_MCU; /* # of DCT blocks per MCU */
+ int MCU_membership[C_MAX_BLOCKS_IN_MCU];
+ /* MCU_membership[i] is index in cur_comp_info of component owning */
+ /* i'th block in an MCU */
+
+ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
+
+ /*
+ * Links to compression subobjects (methods and private variables of modules)
+ */
+ struct jpeg_comp_master * master;
+ struct jpeg_c_main_controller * main;
+ struct jpeg_c_prep_controller * prep;
+ struct jpeg_c_coef_controller * coef;
+ struct jpeg_marker_writer * marker;
+ struct jpeg_color_converter * cconvert;
+ struct jpeg_downsampler * downsample;
+ struct jpeg_forward_dct * fdct;
+ struct jpeg_entropy_encoder * entropy;
+ jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
+ int script_space_size;
+};
+
+
+/* Master record for a decompression instance */
+
+struct jpeg_decompress_struct {
+ jpeg_common_fields; /* Fields shared with jpeg_compress_struct */
+
+ /* Source of compressed data */
+ struct jpeg_source_mgr * src;
+
+ /* Basic description of image --- filled in by jpeg_read_header(). */
+ /* Application may inspect these values to decide how to process image. */
+
+ JDIMENSION image_width; /* nominal image width (from SOF marker) */
+ JDIMENSION image_height; /* nominal image height */
+ int num_components; /* # of color components in JPEG image */
+ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+ /* Decompression processing parameters --- these fields must be set before
+ * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes
+ * them to default values.
+ */
+
+ J_COLOR_SPACE out_color_space; /* colorspace for output */
+
+ unsigned int scale_num, scale_denom; /* fraction by which to scale image */
+
+ double output_gamma; /* image gamma wanted in output */
+
+ boolean buffered_image; /* TRUE=multiple output passes */
+ boolean raw_data_out; /* TRUE=downsampled data wanted */
+
+ J_DCT_METHOD dct_method; /* IDCT algorithm selector */
+ boolean do_fancy_upsampling; /* TRUE=apply fancy upsampling */
+ boolean do_block_smoothing; /* TRUE=apply interblock smoothing */
+
+ boolean quantize_colors; /* TRUE=colormapped output wanted */
+ /* the following are ignored if not quantize_colors: */
+ J_DITHER_MODE dither_mode; /* type of color dithering to use */
+ boolean two_pass_quantize; /* TRUE=use two-pass color quantization */
+ int desired_number_of_colors; /* max # colors to use in created colormap */
+ /* these are significant only in buffered-image mode: */
+ boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */
+ boolean enable_external_quant;/* enable future use of external colormap */
+ boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */
+
+ /* Description of actual output image that will be returned to application.
+ * These fields are computed by jpeg_start_decompress().
+ * You can also use jpeg_calc_output_dimensions() to determine these values
+ * in advance of calling jpeg_start_decompress().
+ */
+
+ JDIMENSION output_width; /* scaled image width */
+ JDIMENSION output_height; /* scaled image height */
+ int out_color_components; /* # of color components in out_color_space */
+ int output_components; /* # of color components returned */
+ /* output_components is 1 (a colormap index) when quantizing colors;
+ * otherwise it equals out_color_components.
+ */
+ int rec_outbuf_height; /* min recommended height of scanline buffer */
+ /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
+ * high, space and time will be wasted due to unnecessary data copying.
+ * Usually rec_outbuf_height will be 1 or 2, at most 4.
+ */
+
+ /* When quantizing colors, the output colormap is described by these fields.
+ * The application can supply a colormap by setting colormap non-NULL before
+ * calling jpeg_start_decompress; otherwise a colormap is created during
+ * jpeg_start_decompress or jpeg_start_output.
+ * The map has out_color_components rows and actual_number_of_colors columns.
+ */
+ int actual_number_of_colors; /* number of entries in use */
+ JSAMPARRAY colormap; /* The color map as a 2-D pixel array */
+
+ /* State variables: these variables indicate the progress of decompression.
+ * The application may examine these but must not modify them.
+ */
+
+ /* Row index of next scanline to be read from jpeg_read_scanlines().
+ * Application may use this to control its processing loop, e.g.,
+ * "while (output_scanline < output_height)".
+ */
+ JDIMENSION output_scanline; /* 0 .. output_height-1 */
+
+ /* Current input scan number and number of iMCU rows completed in scan.
+ * These indicate the progress of the decompressor input side.
+ */
+ int input_scan_number; /* Number of SOS markers seen so far */
+ JDIMENSION input_iMCU_row; /* Number of iMCU rows completed */
+
+ /* The "output scan number" is the notional scan being displayed by the
+ * output side. The decompressor will not allow output scan/row number
+ * to get ahead of input scan/row, but it can fall arbitrarily far behind.
+ */
+ int output_scan_number; /* Nominal scan number being displayed */
+ JDIMENSION output_iMCU_row; /* Number of iMCU rows read */
+
+ /* Current progression status. coef_bits[c][i] indicates the precision
+ * with which component c's DCT coefficient i (in zigzag order) is known.
+ * It is -1 when no data has yet been received, otherwise it is the point
+ * transform (shift) value for the most recent scan of the coefficient
+ * (thus, 0 at completion of the progression).
+ * This pointer is NULL when reading a non-progressive file.
+ */
+ int (*coef_bits)[DCTSIZE2]; /* -1 or current Al value for each coef */
+
+ /* Internal JPEG parameters --- the application usually need not look at
+ * these fields. Note that the decompressor output side may not use
+ * any parameters that can change between scans.
+ */
+
+ /* Quantization and Huffman tables are carried forward across input
+ * datastreams when processing abbreviated JPEG datastreams.
+ */
+
+ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+ /* ptrs to coefficient quantization tables, or NULL if not defined */
+
+ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ /* ptrs to Huffman coding tables, or NULL if not defined */
+
+ /* These parameters are never carried across datastreams, since they
+ * are given in SOF/SOS markers or defined to be reset by SOI.
+ */
+
+ int data_precision; /* bits of precision in image data */
+
+ jpeg_component_info * comp_info;
+ /* comp_info[i] describes component that appears i'th in SOF */
+
+ boolean progressive_mode; /* TRUE if SOFn specifies progressive mode */
+ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
+
+ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+ unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
+
+ /* These fields record data obtained from optional markers recognized by
+ * the JPEG library.
+ */
+ boolean saw_JFIF_marker; /* TRUE iff a JFIF APP0 marker was found */
+ /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */
+ UINT8 JFIF_major_version; /* JFIF version number */
+ UINT8 JFIF_minor_version;
+ UINT8 density_unit; /* JFIF code for pixel size units */
+ UINT16 X_density; /* Horizontal pixel density */
+ UINT16 Y_density; /* Vertical pixel density */
+ boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */
+ UINT8 Adobe_transform; /* Color transform code from Adobe marker */
+
+ boolean CCIR601_sampling; /* TRUE=first samples are cosited */
+
+ /* Aside from the specific data retained from APPn markers known to the
+ * library, the uninterpreted contents of any or all APPn and COM markers
+ * can be saved in a list for examination by the application.
+ */
+ jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */
+
+ /* Remaining fields are known throughout decompressor, but generally
+ * should not be touched by a surrounding application.
+ */
+
+ /*
+ * These fields are computed during decompression startup
+ */
+ int max_h_samp_factor; /* largest h_samp_factor */
+ int max_v_samp_factor; /* largest v_samp_factor */
+
+ int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */
+
+ JDIMENSION total_iMCU_rows; /* # of iMCU rows in image */
+ /* The coefficient controller's input and output progress is measured in
+ * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows
+ * in fully interleaved JPEG scans, but are used whether the scan is
+ * interleaved or not. We define an iMCU row as v_samp_factor DCT block
+ * rows of each component. Therefore, the IDCT output contains
+ * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
+ */
+
+ JSAMPLE * sample_range_limit; /* table for fast range-limiting */
+
+ /*
+ * These fields are valid during any one scan.
+ * They describe the components and MCUs actually appearing in the scan.
+ * Note that the decompressor output side must not use these fields.
+ */
+ int comps_in_scan; /* # of JPEG components in this scan */
+ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+ /* *cur_comp_info[i] describes component that appears i'th in SOS */
+
+ JDIMENSION MCUs_per_row; /* # of MCUs across the image */
+ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
+
+ int blocks_in_MCU; /* # of DCT blocks per MCU */
+ int MCU_membership[D_MAX_BLOCKS_IN_MCU];
+ /* MCU_membership[i] is index in cur_comp_info of component owning */
+ /* i'th block in an MCU */
+
+ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
+
+ /* This field is shared between entropy decoder and marker parser.
+ * It is either zero or the code of a JPEG marker that has been
+ * read from the data source, but has not yet been processed.
+ */
+ int unread_marker;
+
+ /*
+ * Links to decompression subobjects (methods, private variables of modules)
+ */
+ struct jpeg_decomp_master * master;
+ struct jpeg_d_main_controller * main;
+ struct jpeg_d_coef_controller * coef;
+ struct jpeg_d_post_controller * post;
+ struct jpeg_input_controller * inputctl;
+ struct jpeg_marker_reader * marker;
+ struct jpeg_entropy_decoder * entropy;
+ struct jpeg_inverse_dct * idct;
+ struct jpeg_upsampler * upsample;
+ struct jpeg_color_deconverter * cconvert;
+ struct jpeg_color_quantizer * cquantize;
+};
+
+
+/* "Object" declarations for JPEG modules that may be supplied or called
+ * directly by the surrounding application.
+ * As with all objects in the JPEG library, these structs only define the
+ * publicly visible methods and state variables of a module. Additional
+ * private fields may exist after the public ones.
+ */
+
+
+/* Error handler object */
+
+struct jpeg_error_mgr {
+ /* Error exit handler: does not return to caller */
+ JMETHOD(void, error_exit, (j_common_ptr cinfo));
+ /* Conditionally emit a trace or warning message */
+ JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
+ /* Routine that actually outputs a trace or error message */
+ JMETHOD(void, output_message, (j_common_ptr cinfo));
+ /* Format a message string for the most recent JPEG error or message */
+ JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
+#define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */
+ /* Reset error state variables at start of a new image */
+ JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
+
+ /* The message ID code and any parameters are saved here.
+ * A message can have one string parameter or up to 8 int parameters.
+ */
+ int msg_code;
+#define JMSG_STR_PARM_MAX 80
+ union {
+ int i[8];
+ char s[JMSG_STR_PARM_MAX];
+ } msg_parm;
+
+ /* Standard state variables for error facility */
+
+ int trace_level; /* max msg_level that will be displayed */
+
+ /* For recoverable corrupt-data errors, we emit a warning message,
+ * but keep going unless emit_message chooses to abort. emit_message
+ * should count warnings in num_warnings. The surrounding application
+ * can check for bad data by seeing if num_warnings is nonzero at the
+ * end of processing.
+ */
+ long num_warnings; /* number of corrupt-data warnings */
+
+ /* These fields point to the table(s) of error message strings.
+ * An application can change the table pointer to switch to a different
+ * message list (typically, to change the language in which errors are
+ * reported). Some applications may wish to add additional error codes
+ * that will be handled by the JPEG library error mechanism; the second
+ * table pointer is used for this purpose.
+ *
+ * First table includes all errors generated by JPEG library itself.
+ * Error code 0 is reserved for a "no such error string" message.
+ */
+ const char * const * jpeg_message_table; /* Library errors */
+ int last_jpeg_message; /* Table contains strings 0..last_jpeg_message */
+ /* Second table can be added by application (see cjpeg/djpeg for example).
+ * It contains strings numbered first_addon_message..last_addon_message.
+ */
+ const char * const * addon_message_table; /* Non-library errors */
+ int first_addon_message; /* code for first string in addon table */
+ int last_addon_message; /* code for last string in addon table */
+};
+
+
+/* Progress monitor object */
+
+struct jpeg_progress_mgr {
+ JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
+
+ long pass_counter; /* work units completed in this pass */
+ long pass_limit; /* total number of work units in this pass */
+ int completed_passes; /* passes completed so far */
+ int total_passes; /* total number of passes expected */
+};
+
+
+/* Data destination object for compression */
+
+struct jpeg_destination_mgr {
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+
+ JMETHOD(void, init_destination, (j_compress_ptr cinfo));
+ JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
+ JMETHOD(void, term_destination, (j_compress_ptr cinfo));
+};
+
+
+/* Data source object for decompression */
+
+struct jpeg_source_mgr {
+ const JOCTET * next_input_byte; /* => next byte to read from buffer */
+ size_t bytes_in_buffer; /* # of bytes remaining in buffer */
+
+ JMETHOD(void, init_source, (j_decompress_ptr cinfo));
+ JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
+ JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
+ JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
+ JMETHOD(void, term_source, (j_decompress_ptr cinfo));
+};
+
+
+/* Memory manager object.
+ * Allocates "small" objects (a few K total), "large" objects (tens of K),
+ * and "really big" objects (virtual arrays with backing store if needed).
+ * The memory manager does not allow individual objects to be freed; rather,
+ * each created object is assigned to a pool, and whole pools can be freed
+ * at once. This is faster and more convenient than remembering exactly what
+ * to free, especially where malloc()/free() are not too speedy.
+ * NB: alloc routines never return NULL. They exit to error_exit if not
+ * successful.
+ */
+
+#define JPOOL_PERMANENT 0 /* lasts until master record is destroyed */
+#define JPOOL_IMAGE 1 /* lasts until done with image/datastream */
+#define JPOOL_NUMPOOLS 2
+
+typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
+typedef struct jvirt_barray_control * jvirt_barray_ptr;
+
+
+struct jpeg_memory_mgr {
+ /* Method pointers */
+ JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
+ size_t sizeofobject));
+ JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
+ size_t sizeofobject));
+ JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
+ JDIMENSION samplesperrow,
+ JDIMENSION numrows));
+ JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
+ JDIMENSION blocksperrow,
+ JDIMENSION numrows));
+ JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
+ int pool_id,
+ boolean pre_zero,
+ JDIMENSION samplesperrow,
+ JDIMENSION numrows,
+ JDIMENSION maxaccess));
+ JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
+ int pool_id,
+ boolean pre_zero,
+ JDIMENSION blocksperrow,
+ JDIMENSION numrows,
+ JDIMENSION maxaccess));
+ JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
+ JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
+ jvirt_sarray_ptr ptr,
+ JDIMENSION start_row,
+ JDIMENSION num_rows,
+ boolean writable));
+ JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
+ jvirt_barray_ptr ptr,
+ JDIMENSION start_row,
+ JDIMENSION num_rows,
+ boolean writable));
+ JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
+ JMETHOD(void, self_destruct, (j_common_ptr cinfo));
+
+ /* Limit on memory allocation for this JPEG object. (Note that this is
+ * merely advisory, not a guaranteed maximum; it only affects the space
+ * used for virtual-array buffers.) May be changed by outer application
+ * after creating the JPEG object.
+ */
+ long max_memory_to_use;
+
+ /* Maximum allocation request accepted by alloc_large. */
+ long max_alloc_chunk;
+};
+
+
+/* Routine signature for application-supplied marker processing methods.
+ * Need not pass marker code since it is stored in cinfo->unread_marker.
+ */
+typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
+
+
+/* Declarations for routines called by application.
+ * The JPP macro hides prototype parameters from compilers that can't cope.
+ * Note JPP requires double parentheses.
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JPP(arglist) arglist
+#else
+#define JPP(arglist) ()
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers.
+ * We shorten external names to be unique in the first six letters, which
+ * is good enough for all known systems.
+ * (If your compiler itself needs names to be unique in less than 15
+ * characters, you are out of luck. Get a better compiler.)
+ */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_std_error jStdError
+#define jpeg_CreateCompress jCreaCompress
+#define jpeg_CreateDecompress jCreaDecompress
+#define jpeg_destroy_compress jDestCompress
+#define jpeg_destroy_decompress jDestDecompress
+#define jpeg_stdio_dest jStdDest
+#define jpeg_stdio_src jStdSrc
+#define jpeg_set_defaults jSetDefaults
+#define jpeg_set_colorspace jSetColorspace
+#define jpeg_default_colorspace jDefColorspace
+#define jpeg_set_quality jSetQuality
+#define jpeg_set_linear_quality jSetLQuality
+#define jpeg_add_quant_table jAddQuantTable
+#define jpeg_quality_scaling jQualityScaling
+#define jpeg_simple_progression jSimProgress
+#define jpeg_suppress_tables jSuppressTables
+#define jpeg_alloc_quant_table jAlcQTable
+#define jpeg_alloc_huff_table jAlcHTable
+#define jpeg_start_compress jStrtCompress
+#define jpeg_write_scanlines jWrtScanlines
+#define jpeg_finish_compress jFinCompress
+#define jpeg_write_raw_data jWrtRawData
+#define jpeg_write_marker jWrtMarker
+#define jpeg_write_m_header jWrtMHeader
+#define jpeg_write_m_byte jWrtMByte
+#define jpeg_write_tables jWrtTables
+#define jpeg_read_header jReadHeader
+#define jpeg_start_decompress jStrtDecompress
+#define jpeg_read_scanlines jReadScanlines
+#define jpeg_finish_decompress jFinDecompress
+#define jpeg_read_raw_data jReadRawData
+#define jpeg_has_multiple_scans jHasMultScn
+#define jpeg_start_output jStrtOutput
+#define jpeg_finish_output jFinOutput
+#define jpeg_input_complete jInComplete
+#define jpeg_new_colormap jNewCMap
+#define jpeg_consume_input jConsumeInput
+#define jpeg_calc_output_dimensions jCalcDimensions
+#define jpeg_save_markers jSaveMarkers
+#define jpeg_set_marker_processor jSetMarker
+#define jpeg_read_coefficients jReadCoefs
+#define jpeg_write_coefficients jWrtCoefs
+#define jpeg_copy_critical_parameters jCopyCrit
+#define jpeg_abort_compress jAbrtCompress
+#define jpeg_abort_decompress jAbrtDecompress
+#define jpeg_abort jAbort
+#define jpeg_destroy jDestroy
+#define jpeg_resync_to_restart jResyncRestart
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Default error-management setup */
+EXTERN(struct jpeg_error_mgr *) jpeg_std_error
+ JPP((struct jpeg_error_mgr * err));
+
+/* Initialization of JPEG compression objects.
+ * jpeg_create_compress() and jpeg_create_decompress() are the exported
+ * names that applications should call. These expand to calls on
+ * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
+ * passed for version mismatch checking.
+ * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
+ */
+#define jpeg_create_compress(cinfo) \
+ jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
+ (size_t) sizeof(struct jpeg_compress_struct))
+#define jpeg_create_decompress(cinfo) \
+ jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
+ (size_t) sizeof(struct jpeg_decompress_struct))
+EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
+ int version, size_t structsize));
+EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
+ int version, size_t structsize));
+/* Destruction of JPEG compression objects */
+EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
+
+/* Standard data source and destination managers: stdio streams. */
+/* Caller is responsible for opening the file before and closing after. */
+EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile));
+EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
+
+/* Default parameter setup for compression */
+EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
+/* Compression parameter setup aids */
+EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
+ J_COLOR_SPACE colorspace));
+EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
+ boolean force_baseline));
+EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
+ int scale_factor,
+ boolean force_baseline));
+EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
+ const unsigned int *basic_table,
+ int scale_factor,
+ boolean force_baseline));
+EXTERN(int) jpeg_quality_scaling JPP((int quality));
+EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
+ boolean suppress));
+EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
+EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
+
+/* Main entry points for compression */
+EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
+ boolean write_all_tables));
+EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
+ JSAMPARRAY scanlines,
+ JDIMENSION num_lines));
+EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
+
+/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
+EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
+ JSAMPIMAGE data,
+ JDIMENSION num_lines));
+
+/* Write a special marker. See libjpeg.doc concerning safe usage. */
+EXTERN(void) jpeg_write_marker
+ JPP((j_compress_ptr cinfo, int marker,
+ const JOCTET * dataptr, unsigned int datalen));
+/* Same, but piecemeal. */
+EXTERN(void) jpeg_write_m_header
+ JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
+EXTERN(void) jpeg_write_m_byte
+ JPP((j_compress_ptr cinfo, int val));
+
+/* Alternate compression function: just write an abbreviated table file */
+EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));
+
+/* Decompression startup: read start of JPEG datastream to see what's there */
+EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
+ boolean require_image));
+/* Return value is one of: */
+#define JPEG_SUSPENDED 0 /* Suspended due to lack of input data */
+#define JPEG_HEADER_OK 1 /* Found valid image datastream */
+#define JPEG_HEADER_TABLES_ONLY 2 /* Found valid table-specs-only datastream */
+/* If you pass require_image = TRUE (normal case), you need not check for
+ * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
+ * JPEG_SUSPENDED is only possible if you use a data source module that can
+ * give a suspension return (the stdio source module doesn't).
+ */
+
+/* Main entry points for decompression */
+EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
+EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
+ JSAMPARRAY scanlines,
+ JDIMENSION max_lines));
+EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
+
+/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
+EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE data,
+ JDIMENSION max_lines));
+
+/* Additional entry points for buffered-image mode. */
+EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
+EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
+ int scan_number));
+EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
+EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
+EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
+EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
+/* Return value is one of: */
+/* #define JPEG_SUSPENDED 0 Suspended due to lack of input data */
+#define JPEG_REACHED_SOS 1 /* Reached start of new scan */
+#define JPEG_REACHED_EOI 2 /* Reached end of image */
+#define JPEG_ROW_COMPLETED 3 /* Completed one iMCU row */
+#define JPEG_SCAN_COMPLETED 4 /* Completed last iMCU row of a scan */
+
+/* Precalculate output dimensions for current decompression parameters. */
+EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
+
+/* Control saving of COM and APPn markers into marker_list. */
+EXTERN(void) jpeg_save_markers
+ JPP((j_decompress_ptr cinfo, int marker_code,
+ unsigned int length_limit));
+
+/* Install a special processing method for COM or APPn markers. */
+EXTERN(void) jpeg_set_marker_processor
+ JPP((j_decompress_ptr cinfo, int marker_code,
+ jpeg_marker_parser_method routine));
+
+/* Read or write raw DCT coefficients --- useful for lossless transcoding. */
+EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
+EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays));
+EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo));
+
+/* If you choose to abort compression or decompression before completing
+ * jpeg_finish_(de)compress, then you need to clean up to release memory,
+ * temporary files, etc. You can just call jpeg_destroy_(de)compress
+ * if you're done with the JPEG object, but if you want to clean it up and
+ * reuse it, call this:
+ */
+EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
+
+/* Generic versions of jpeg_abort and jpeg_destroy that work on either
+ * flavor of JPEG object. These may be more convenient in some places.
+ */
+EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
+EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));
+
+/* Default restart-marker-resync procedure for use by data source modules */
+EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
+ int desired));
+
+
+/* These marker codes are exported since applications and data source modules
+ * are likely to want to use them.
+ */
+
+#define JPEG_RST0 0xD0 /* RST0 marker code */
+#define JPEG_EOI 0xD9 /* EOI marker code */
+#define JPEG_APP0 0xE0 /* APP0 marker code */
+#define JPEG_COM 0xFE /* COM marker code */
+
+
+/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
+ * for structure definitions that are never filled in, keep it quiet by
+ * supplying dummy definitions for the various substructures.
+ */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef JPEG_INTERNALS /* will be defined in jpegint.h */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+struct jpeg_comp_master { long dummy; };
+struct jpeg_c_main_controller { long dummy; };
+struct jpeg_c_prep_controller { long dummy; };
+struct jpeg_c_coef_controller { long dummy; };
+struct jpeg_marker_writer { long dummy; };
+struct jpeg_color_converter { long dummy; };
+struct jpeg_downsampler { long dummy; };
+struct jpeg_forward_dct { long dummy; };
+struct jpeg_entropy_encoder { long dummy; };
+struct jpeg_decomp_master { long dummy; };
+struct jpeg_d_main_controller { long dummy; };
+struct jpeg_d_coef_controller { long dummy; };
+struct jpeg_d_post_controller { long dummy; };
+struct jpeg_input_controller { long dummy; };
+struct jpeg_marker_reader { long dummy; };
+struct jpeg_entropy_decoder { long dummy; };
+struct jpeg_inverse_dct { long dummy; };
+struct jpeg_upsampler { long dummy; };
+struct jpeg_color_deconverter { long dummy; };
+struct jpeg_color_quantizer { long dummy; };
+#endif /* JPEG_INTERNALS */
+#endif /* INCOMPLETE_TYPES_BROKEN */
+
+
+/*
+ * The JPEG library modules define JPEG_INTERNALS before including this file.
+ * The internal structure declarations are read only when that is true.
+ * Applications using the library should not include jpegint.h, but may wish
+ * to include jerror.h.
+ */
+
+#ifdef JPEG_INTERNALS
+#include "jpegint.h" /* fetch private declarations */
+#include "jerror.h" /* fetch error codes too */
+#endif
+
+#endif /* JPEGLIB_H */
diff --git a/test/monniaux/jpeg-6b/jpegtran.1 b/test/monniaux/jpeg-6b/jpegtran.1
new file mode 100644
index 00000000..6de18e2a
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jpegtran.1
@@ -0,0 +1,238 @@
+.TH JPEGTRAN 1 "3 August 1997"
+.SH NAME
+jpegtran \- lossless transformation of JPEG files
+.SH SYNOPSIS
+.B jpegtran
+[
+.I options
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B jpegtran
+performs various useful transformations of JPEG files.
+It can translate the coded representation from one variant of JPEG to another,
+for example from baseline JPEG to progressive JPEG or vice versa. It can also
+perform some rearrangements of the image data, for example turning an image
+from landscape to portrait format by rotation.
+.PP
+.B jpegtran
+works by rearranging the compressed data (DCT coefficients), without
+ever fully decoding the image. Therefore, its transformations are lossless:
+there is no image degradation at all, which would not be true if you used
+.B djpeg
+followed by
+.B cjpeg
+to accomplish the same conversion. But by the same token,
+.B jpegtran
+cannot perform lossy operations such as changing the image quality.
+.PP
+.B jpegtran
+reads the named JPEG/JFIF file, or the standard input if no file is
+named, and produces a JPEG/JFIF file on the standard output.
+.SH OPTIONS
+All switch names may be abbreviated; for example,
+.B \-optimize
+may be written
+.B \-opt
+or
+.BR \-o .
+Upper and lower case are equivalent.
+British spellings are also accepted (e.g.,
+.BR \-optimise ),
+though for brevity these are not mentioned below.
+.PP
+To specify the coded JPEG representation used in the output file,
+.B jpegtran
+accepts a subset of the switches recognized by
+.BR cjpeg :
+.TP
+.B \-optimize
+Perform optimization of entropy encoding parameters.
+.TP
+.B \-progressive
+Create progressive JPEG file.
+.TP
+.BI \-restart " N"
+Emit a JPEG restart marker every N MCU rows, or every N MCU blocks if "B" is
+attached to the number.
+.TP
+.BI \-scans " file"
+Use the scan script given in the specified text file.
+.PP
+See
+.BR cjpeg (1)
+for more details about these switches.
+If you specify none of these switches, you get a plain baseline-JPEG output
+file. The quality setting and so forth are determined by the input file.
+.PP
+The image can be losslessly transformed by giving one of these switches:
+.TP
+.B \-flip horizontal
+Mirror image horizontally (left-right).
+.TP
+.B \-flip vertical
+Mirror image vertically (top-bottom).
+.TP
+.B \-rotate 90
+Rotate image 90 degrees clockwise.
+.TP
+.B \-rotate 180
+Rotate image 180 degrees.
+.TP
+.B \-rotate 270
+Rotate image 270 degrees clockwise (or 90 ccw).
+.TP
+.B \-transpose
+Transpose image (across UL-to-LR axis).
+.TP
+.B \-transverse
+Transverse transpose (across UR-to-LL axis).
+.PP
+The transpose transformation has no restrictions regarding image dimensions.
+The other transformations operate rather oddly if the image dimensions are not
+a multiple of the iMCU size (usually 8 or 16 pixels), because they can only
+transform complete blocks of DCT coefficient data in the desired way.
+.PP
+.BR jpegtran 's
+default behavior when transforming an odd-size image is designed
+to preserve exact reversibility and mathematical consistency of the
+transformation set. As stated, transpose is able to flip the entire image
+area. Horizontal mirroring leaves any partial iMCU column at the right edge
+untouched, but is able to flip all rows of the image. Similarly, vertical
+mirroring leaves any partial iMCU row at the bottom edge untouched, but is
+able to flip all columns. The other transforms can be built up as sequences
+of transpose and flip operations; for consistency, their actions on edge
+pixels are defined to be the same as the end result of the corresponding
+transpose-and-flip sequence.
+.PP
+For practical use, you may prefer to discard any untransformable edge pixels
+rather than having a strange-looking strip along the right and/or bottom edges
+of a transformed image. To do this, add the
+.B \-trim
+switch:
+.TP
+.B \-trim
+Drop non-transformable edge blocks.
+.PP
+Obviously, a transformation with
+.B \-trim
+is not reversible, so strictly speaking
+.B jpegtran
+with this switch is not lossless. Also, the expected mathematical
+equivalences between the transformations no longer hold. For example,
+.B \-rot 270 -trim
+trims only the bottom edge, but
+.B \-rot 90 -trim
+followed by
+.B \-rot 180 -trim
+trims both edges.
+.PP
+Another not-strictly-lossless transformation switch is:
+.TP
+.B \-grayscale
+Force grayscale output.
+.PP
+This option discards the chrominance channels if the input image is YCbCr
+(ie, a standard color JPEG), resulting in a grayscale JPEG file. The
+luminance channel is preserved exactly, so this is a better method of reducing
+to grayscale than decompression, conversion, and recompression. This switch
+is particularly handy for fixing a monochrome picture that was mistakenly
+encoded as a color JPEG. (In such a case, the space savings from getting rid
+of the near-empty chroma channels won't be large; but the decoding time for
+a grayscale JPEG is substantially less than that for a color JPEG.)
+.PP
+.B jpegtran
+also recognizes these switches that control what to do with "extra" markers,
+such as comment blocks:
+.TP
+.B \-copy none
+Copy no extra markers from source file. This setting suppresses all
+comments and other excess baggage present in the source file.
+.TP
+.B \-copy comments
+Copy only comment markers. This setting copies comments from the source file,
+but discards any other inessential data.
+.TP
+.B \-copy all
+Copy all extra markers. This setting preserves miscellaneous markers
+found in the source file, such as JFIF thumbnails and Photoshop settings.
+In some files these extra markers can be sizable.
+.PP
+The default behavior is
+.BR "\-copy comments" .
+(Note: in IJG releases v6 and v6a,
+.B jpegtran
+always did the equivalent of
+.BR "\-copy none" .)
+.PP
+Additional switches recognized by jpegtran are:
+.TP
+.BI \-maxmemory " N"
+Set limit for amount of memory to use in processing large images. Value is
+in thousands of bytes, or millions of bytes if "M" is attached to the
+number. For example,
+.B \-max 4m
+selects 4000000 bytes. If more space is needed, temporary files will be used.
+.TP
+.BI \-outfile " name"
+Send output image to the named file, not to standard output.
+.TP
+.B \-verbose
+Enable debug printout. More
+.BR \-v 's
+give more output. Also, version information is printed at startup.
+.TP
+.B \-debug
+Same as
+.BR \-verbose .
+.SH EXAMPLES
+.LP
+This example converts a baseline JPEG file to progressive form:
+.IP
+.B jpegtran \-progressive
+.I foo.jpg
+.B >
+.I fooprog.jpg
+.PP
+This example rotates an image 90 degrees clockwise, discarding any
+unrotatable edge pixels:
+.IP
+.B jpegtran \-rot 90 -trim
+.I foo.jpg
+.B >
+.I foo90.jpg
+.SH ENVIRONMENT
+.TP
+.B JPEGMEM
+If this environment variable is set, its value is the default memory limit.
+The value is specified as described for the
+.B \-maxmemory
+switch.
+.B JPEGMEM
+overrides the default value specified when the program was compiled, and
+itself is overridden by an explicit
+.BR \-maxmemory .
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR djpeg (1),
+.BR rdjpgcom (1),
+.BR wrjpgcom (1)
+.br
+Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
+Communications of the ACM, April 1991 (vol. 34, no. 4), pp. 30-44.
+.SH AUTHOR
+Independent JPEG Group
+.SH BUGS
+Arithmetic coding is not supported for legal reasons.
+.PP
+The transform options can't transform odd-size images perfectly. Use
+.B \-trim
+if you don't like the results without it.
+.PP
+The entire image is read into memory and then written out again, even in
+cases where this isn't really necessary. Expect swapping on large images,
+especially when using the more complex transform options.
diff --git a/test/monniaux/jpeg-6b/jpegtran.c b/test/monniaux/jpeg-6b/jpegtran.c
new file mode 100644
index 00000000..20ef111b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jpegtran.c
@@ -0,0 +1,504 @@
+/*
+ * jpegtran.c
+ *
+ * Copyright (C) 1995-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a command-line user interface for JPEG transcoding.
+ * It is very similar to cjpeg.c, but provides lossless transcoding between
+ * different JPEG file formats. It also provides some lossless and sort-of-
+ * lossless transformations of JPEG data.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+#include "transupp.h" /* Support routines for jpegtran */
+#include "jversion.h" /* for version message */
+
+#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h> /* Metrowerks needs this */
+#include <console.h> /* ... and this */
+#endif
+#ifdef THINK_C
+#include <console.h> /* Think declares it here */
+#endif
+#endif
+
+
+/*
+ * Argument-parsing code.
+ * The switch parser is designed to be useful with DOS-style command line
+ * syntax, ie, intermixed switches and file names, where only the switches
+ * to the left of a given file name affect processing of that file.
+ * The main program in this file doesn't actually use this capability...
+ */
+
+
+static const char * progname; /* program name for error messages */
+static char * outfilename; /* for -outfile switch */
+static JCOPY_OPTION copyoption; /* -copy switch */
+static jpeg_transform_info transformoption; /* image transformation options */
+
+
+LOCAL(void)
+usage (void)
+/* complain about bad command line */
+{
+ fprintf(stderr, "usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+ fprintf(stderr, "inputfile outputfile\n");
+#else
+ fprintf(stderr, "[inputfile]\n");
+#endif
+
+ fprintf(stderr, "Switches (names may be abbreviated):\n");
+ fprintf(stderr, " -copy none Copy no extra markers from source file\n");
+ fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
+ fprintf(stderr, " -copy all Copy all extra markers\n");
+#ifdef ENTROPY_OPT_SUPPORTED
+ fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
+#endif
+#ifdef C_PROGRESSIVE_SUPPORTED
+ fprintf(stderr, " -progressive Create progressive JPEG file\n");
+#endif
+#if TRANSFORMS_SUPPORTED
+ fprintf(stderr, "Switches for modifying the image:\n");
+ fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
+ fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
+ fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
+ fprintf(stderr, " -transpose Transpose image\n");
+ fprintf(stderr, " -transverse Transverse transpose image\n");
+ fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
+#endif /* TRANSFORMS_SUPPORTED */
+ fprintf(stderr, "Switches for advanced users:\n");
+ fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
+ fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
+ fprintf(stderr, " -outfile name Specify name for output file\n");
+ fprintf(stderr, " -verbose or -debug Emit debug output\n");
+ fprintf(stderr, "Switches for wizards:\n");
+#ifdef C_ARITH_CODING_SUPPORTED
+ fprintf(stderr, " -arithmetic Use arithmetic coding\n");
+#endif
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
+#endif
+ exit(EXIT_FAILURE);
+}
+
+
+LOCAL(void)
+select_transform (JXFORM_CODE transform)
+/* Silly little routine to detect multiple transform options,
+ * which we can't handle.
+ */
+{
+#if TRANSFORMS_SUPPORTED
+ if (transformoption.transform == JXFORM_NONE ||
+ transformoption.transform == transform) {
+ transformoption.transform = transform;
+ } else {
+ fprintf(stderr, "%s: can only do one image transformation at a time\n",
+ progname);
+ usage();
+ }
+#else
+ fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+}
+
+
+LOCAL(int)
+parse_switches (j_compress_ptr cinfo, int argc, char **argv,
+ int last_file_arg_seen, boolean for_real)
+/* Parse optional switches.
+ * Returns argv[] index of first file-name argument (== argc if none).
+ * Any file names with indexes <= last_file_arg_seen are ignored;
+ * they have presumably been processed in a previous iteration.
+ * (Pass 0 for last_file_arg_seen on the first or only iteration.)
+ * for_real is FALSE on the first (dummy) pass; we may skip any expensive
+ * processing.
+ */
+{
+ int argn;
+ char * arg;
+ boolean simple_progressive;
+ char * scansarg = NULL; /* saves -scans parm if any */
+
+ /* Set up default JPEG parameters. */
+ simple_progressive = FALSE;
+ outfilename = NULL;
+ copyoption = JCOPYOPT_DEFAULT;
+ transformoption.transform = JXFORM_NONE;
+ transformoption.trim = FALSE;
+ transformoption.force_grayscale = FALSE;
+ cinfo->err->trace_level = 0;
+
+ /* Scan command line options, adjust parameters */
+
+ for (argn = 1; argn < argc; argn++) {
+ arg = argv[argn];
+ if (*arg != '-') {
+ /* Not a switch, must be a file name argument */
+ if (argn <= last_file_arg_seen) {
+ outfilename = NULL; /* -outfile applies to just one input file */
+ continue; /* ignore this name if previously processed */
+ }
+ break; /* else done parsing switches */
+ }
+ arg++; /* advance past switch marker character */
+
+ if (keymatch(arg, "arithmetic", 1)) {
+ /* Use arithmetic coding. */
+#ifdef C_ARITH_CODING_SUPPORTED
+ cinfo->arith_code = TRUE;
+#else
+ fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "copy", 1)) {
+ /* Select which extra markers to copy. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "none", 1)) {
+ copyoption = JCOPYOPT_NONE;
+ } else if (keymatch(argv[argn], "comments", 1)) {
+ copyoption = JCOPYOPT_COMMENTS;
+ } else if (keymatch(argv[argn], "all", 1)) {
+ copyoption = JCOPYOPT_ALL;
+ } else
+ usage();
+
+ } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
+ /* Enable debug printouts. */
+ /* On first -d, print version identification */
+ static boolean printed_version = FALSE;
+
+ if (! printed_version) {
+ fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
+ JVERSION, JCOPYRIGHT);
+ printed_version = TRUE;
+ }
+ cinfo->err->trace_level++;
+
+ } else if (keymatch(arg, "flip", 1)) {
+ /* Mirror left-right or top-bottom. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "horizontal", 1))
+ select_transform(JXFORM_FLIP_H);
+ else if (keymatch(argv[argn], "vertical", 1))
+ select_transform(JXFORM_FLIP_V);
+ else
+ usage();
+
+ } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
+ /* Force to grayscale. */
+#if TRANSFORMS_SUPPORTED
+ transformoption.force_grayscale = TRUE;
+#else
+ select_transform(JXFORM_NONE); /* force an error */
+#endif
+
+ } else if (keymatch(arg, "maxmemory", 3)) {
+ /* Maximum memory in Kb (or Mb with 'm'). */
+ long lval;
+ char ch = 'x';
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+ usage();
+ if (ch == 'm' || ch == 'M')
+ lval *= 1000L;
+ cinfo->mem->max_memory_to_use = lval * 1000L;
+
+ } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
+ /* Enable entropy parm optimization. */
+#ifdef ENTROPY_OPT_SUPPORTED
+ cinfo->optimize_coding = TRUE;
+#else
+ fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "outfile", 4)) {
+ /* Set output file name. */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ outfilename = argv[argn]; /* save it away for later use */
+
+ } else if (keymatch(arg, "progressive", 1)) {
+ /* Select simple progressive mode. */
+#ifdef C_PROGRESSIVE_SUPPORTED
+ simple_progressive = TRUE;
+ /* We must postpone execution until num_components is known. */
+#else
+ fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "restart", 1)) {
+ /* Restart interval in MCU rows (or in MCUs with 'b'). */
+ long lval;
+ char ch = 'x';
+
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+ usage();
+ if (lval < 0 || lval > 65535L)
+ usage();
+ if (ch == 'b' || ch == 'B') {
+ cinfo->restart_interval = (unsigned int) lval;
+ cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
+ } else {
+ cinfo->restart_in_rows = (int) lval;
+ /* restart_interval will be computed during startup */
+ }
+
+ } else if (keymatch(arg, "rotate", 2)) {
+ /* Rotate 90, 180, or 270 degrees (measured clockwise). */
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ if (keymatch(argv[argn], "90", 2))
+ select_transform(JXFORM_ROT_90);
+ else if (keymatch(argv[argn], "180", 3))
+ select_transform(JXFORM_ROT_180);
+ else if (keymatch(argv[argn], "270", 3))
+ select_transform(JXFORM_ROT_270);
+ else
+ usage();
+
+ } else if (keymatch(arg, "scans", 1)) {
+ /* Set scan script. */
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (++argn >= argc) /* advance to next argument */
+ usage();
+ scansarg = argv[argn];
+ /* We must postpone reading the file in case -progressive appears. */
+#else
+ fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
+ progname);
+ exit(EXIT_FAILURE);
+#endif
+
+ } else if (keymatch(arg, "transpose", 1)) {
+ /* Transpose (across UL-to-LR axis). */
+ select_transform(JXFORM_TRANSPOSE);
+
+ } else if (keymatch(arg, "transverse", 6)) {
+ /* Transverse transpose (across UR-to-LL axis). */
+ select_transform(JXFORM_TRANSVERSE);
+
+ } else if (keymatch(arg, "trim", 3)) {
+ /* Trim off any partial edge MCUs that the transform can't handle. */
+ transformoption.trim = TRUE;
+
+ } else {
+ usage(); /* bogus switch */
+ }
+ }
+
+ /* Post-switch-scanning cleanup */
+
+ if (for_real) {
+
+#ifdef C_PROGRESSIVE_SUPPORTED
+ if (simple_progressive) /* process -progressive; -scans can override */
+ jpeg_simple_progression(cinfo);
+#endif
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (scansarg != NULL) /* process -scans if it was present */
+ if (! read_scan_script(cinfo, scansarg))
+ usage();
+#endif
+ }
+
+ return argn; /* return index of next arg (file name) */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+ struct jpeg_decompress_struct srcinfo;
+ struct jpeg_compress_struct dstinfo;
+ struct jpeg_error_mgr jsrcerr, jdsterr;
+#ifdef PROGRESS_REPORT
+ struct cdjpeg_progress_mgr progress;
+#endif
+ jvirt_barray_ptr * src_coef_arrays;
+ jvirt_barray_ptr * dst_coef_arrays;
+ int file_index;
+ FILE * input_file;
+ FILE * output_file;
+
+ /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+ argc = ccommand(&argv);
+#endif
+
+ progname = argv[0];
+ if (progname == NULL || progname[0] == 0)
+ progname = "jpegtran"; /* in case C library doesn't provide it */
+
+ /* Initialize the JPEG decompression object with default error handling. */
+ srcinfo.err = jpeg_std_error(&jsrcerr);
+ jpeg_create_decompress(&srcinfo);
+ /* Initialize the JPEG compression object with default error handling. */
+ dstinfo.err = jpeg_std_error(&jdsterr);
+ jpeg_create_compress(&dstinfo);
+
+ /* Now safe to enable signal catcher.
+ * Note: we assume only the decompression object will have virtual arrays.
+ */
+#ifdef NEED_SIGNAL_CATCHER
+ enable_signal_catcher((j_common_ptr) &srcinfo);
+#endif
+
+ /* Scan command line to find file names.
+ * It is convenient to use just one switch-parsing routine, but the switch
+ * values read here are mostly ignored; we will rescan the switches after
+ * opening the input file. Also note that most of the switches affect the
+ * destination JPEG object, so we parse into that and then copy over what
+ * needs to affects the source too.
+ */
+
+ file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
+ jsrcerr.trace_level = jdsterr.trace_level;
+ srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
+
+#ifdef TWO_FILE_COMMANDLINE
+ /* Must have either -outfile switch or explicit output file name */
+ if (outfilename == NULL) {
+ if (file_index != argc-2) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ outfilename = argv[file_index+1];
+ } else {
+ if (file_index != argc-1) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ }
+#else
+ /* Unix style: expect zero or one file name */
+ if (file_index < argc-1) {
+ fprintf(stderr, "%s: only one input file\n", progname);
+ usage();
+ }
+#endif /* TWO_FILE_COMMANDLINE */
+
+ /* Open the input file. */
+ if (file_index < argc) {
+ if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default input file is stdin */
+ input_file = read_stdin();
+ }
+
+ /* Open the output file. */
+ if (outfilename != NULL) {
+ if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default output file is stdout */
+ output_file = write_stdout();
+ }
+
+#ifdef PROGRESS_REPORT
+ start_progress_monitor((j_common_ptr) &dstinfo, &progress);
+#endif
+
+ /* Specify data source for decompression */
+ jpeg_stdio_src(&srcinfo, input_file);
+
+ /* Enable saving of extra markers that we want to copy */
+ jcopy_markers_setup(&srcinfo, copyoption);
+
+ /* Read file header */
+ (void) jpeg_read_header(&srcinfo, TRUE);
+
+ /* Any space needed by a transform option must be requested before
+ * jpeg_read_coefficients so that memory allocation will be done right.
+ */
+#if TRANSFORMS_SUPPORTED
+ jtransform_request_workspace(&srcinfo, &transformoption);
+#endif
+
+ /* Read source file as DCT coefficients */
+ src_coef_arrays = jpeg_read_coefficients(&srcinfo);
+
+ /* Initialize destination compression parameters from source values */
+ jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
+
+ /* Adjust destination parameters if required by transform options;
+ * also find out which set of coefficient arrays will hold the output.
+ */
+#if TRANSFORMS_SUPPORTED
+ dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
+ src_coef_arrays,
+ &transformoption);
+#else
+ dst_coef_arrays = src_coef_arrays;
+#endif
+
+ /* Adjust default compression parameters by re-parsing the options */
+ file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
+
+ /* Specify data destination for compression */
+ jpeg_stdio_dest(&dstinfo, output_file);
+
+ /* Start compressor (note no image data is actually written here) */
+ jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
+
+ /* Copy to the output file any extra markers that we want to preserve */
+ jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
+
+ /* Execute image transformation, if any */
+#if TRANSFORMS_SUPPORTED
+ jtransform_execute_transformation(&srcinfo, &dstinfo,
+ src_coef_arrays,
+ &transformoption);
+#endif
+
+ /* Finish compression and release memory */
+ jpeg_finish_compress(&dstinfo);
+ jpeg_destroy_compress(&dstinfo);
+ (void) jpeg_finish_decompress(&srcinfo);
+ jpeg_destroy_decompress(&srcinfo);
+
+ /* Close files, if we opened them */
+ if (input_file != stdin)
+ fclose(input_file);
+ if (output_file != stdout)
+ fclose(output_file);
+
+#ifdef PROGRESS_REPORT
+ end_progress_monitor((j_common_ptr) &dstinfo);
+#endif
+
+ /* All done. */
+ exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
+ return 0; /* suppress no-return-value warnings */
+}
diff --git a/test/monniaux/jpeg-6b/jquant1.c b/test/monniaux/jpeg-6b/jquant1.c
new file mode 100644
index 00000000..b2f96aa1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jquant1.c
@@ -0,0 +1,856 @@
+/*
+ * jquant1.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains 1-pass color quantization (color mapping) routines.
+ * These routines provide mapping to a fixed color map using equally spaced
+ * color values. Optional Floyd-Steinberg or ordered dithering is available.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+#ifdef QUANT_1PASS_SUPPORTED
+
+
+/*
+ * The main purpose of 1-pass quantization is to provide a fast, if not very
+ * high quality, colormapped output capability. A 2-pass quantizer usually
+ * gives better visual quality; however, for quantized grayscale output this
+ * quantizer is perfectly adequate. Dithering is highly recommended with this
+ * quantizer, though you can turn it off if you really want to.
+ *
+ * In 1-pass quantization the colormap must be chosen in advance of seeing the
+ * image. We use a map consisting of all combinations of Ncolors[i] color
+ * values for the i'th component. The Ncolors[] values are chosen so that
+ * their product, the total number of colors, is no more than that requested.
+ * (In most cases, the product will be somewhat less.)
+ *
+ * Since the colormap is orthogonal, the representative value for each color
+ * component can be determined without considering the other components;
+ * then these indexes can be combined into a colormap index by a standard
+ * N-dimensional-array-subscript calculation. Most of the arithmetic involved
+ * can be precalculated and stored in the lookup table colorindex[].
+ * colorindex[i][j] maps pixel value j in component i to the nearest
+ * representative value (grid plane) for that component; this index is
+ * multiplied by the array stride for component i, so that the
+ * index of the colormap entry closest to a given pixel value is just
+ * sum( colorindex[component-number][pixel-component-value] )
+ * Aside from being fast, this scheme allows for variable spacing between
+ * representative values with no additional lookup cost.
+ *
+ * If gamma correction has been applied in color conversion, it might be wise
+ * to adjust the color grid spacing so that the representative colors are
+ * equidistant in linear space. At this writing, gamma correction is not
+ * implemented by jdcolor, so nothing is done here.
+ */
+
+
+/* Declarations for ordered dithering.
+ *
+ * We use a standard 16x16 ordered dither array. The basic concept of ordered
+ * dithering is described in many references, for instance Dale Schumacher's
+ * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
+ * In place of Schumacher's comparisons against a "threshold" value, we add a
+ * "dither" value to the input pixel and then round the result to the nearest
+ * output value. The dither value is equivalent to (0.5 - threshold) times
+ * the distance between output values. For ordered dithering, we assume that
+ * the output colors are equally spaced; if not, results will probably be
+ * worse, since the dither may be too much or too little at a given point.
+ *
+ * The normal calculation would be to form pixel value + dither, range-limit
+ * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
+ * We can skip the separate range-limiting step by extending the colorindex
+ * table in both directions.
+ */
+
+#define ODITHER_SIZE 16 /* dimension of dither matrix */
+/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
+#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */
+#define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */
+
+typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
+typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
+
+static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
+ /* Bayer's order-4 dither array. Generated by the code given in
+ * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
+ * The values in this array must range from 0 to ODITHER_CELLS-1.
+ */
+ { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
+ { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
+ { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
+ { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
+ { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
+ { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
+ { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
+ { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
+ { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
+ { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
+ { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
+ { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
+ { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
+ { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
+ { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
+ { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
+};
+
+
+/* Declarations for Floyd-Steinberg dithering.
+ *
+ * Errors are accumulated into the array fserrors[], at a resolution of
+ * 1/16th of a pixel count. The error at a given pixel is propagated
+ * to its not-yet-processed neighbors using the standard F-S fractions,
+ * ... (here) 7/16
+ * 3/16 5/16 1/16
+ * We work left-to-right on even rows, right-to-left on odd rows.
+ *
+ * We can get away with a single array (holding one row's worth of errors)
+ * by using it to store the current row's errors at pixel columns not yet
+ * processed, but the next row's errors at columns already processed. We
+ * need only a few extra variables to hold the errors immediately around the
+ * current column. (If we are lucky, those variables are in registers, but
+ * even if not, they're probably cheaper to access than array elements are.)
+ *
+ * The fserrors[] array is indexed [component#][position].
+ * We provide (#columns + 2) entries per component; the extra entry at each
+ * end saves us from special-casing the first and last pixels.
+ *
+ * Note: on a wide image, we might not have enough room in a PC's near data
+ * segment to hold the error array; so it is allocated with alloc_large.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef INT16 FSERROR; /* 16 bits should be enough */
+typedef int LOCFSERROR; /* use 'int' for calculation temps */
+#else
+typedef INT32 FSERROR; /* may need more than 16 bits */
+typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */
+#endif
+
+typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
+
+
+/* Private subobject */
+
+#define MAX_Q_COMPS 4 /* max components I can handle */
+
+typedef struct {
+ struct jpeg_color_quantizer pub; /* public fields */
+
+ /* Initially allocated colormap is saved here */
+ JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */
+ int sv_actual; /* number of entries in use */
+
+ JSAMPARRAY colorindex; /* Precomputed mapping for speed */
+ /* colorindex[i][j] = index of color closest to pixel value j in component i,
+ * premultiplied as described above. Since colormap indexes must fit into
+ * JSAMPLEs, the entries of this array will too.
+ */
+ boolean is_padded; /* is the colorindex padded for odither? */
+
+ int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */
+
+ /* Variables for ordered dithering */
+ int row_index; /* cur row's vertical index in dither matrix */
+ ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
+
+ /* Variables for Floyd-Steinberg dithering */
+ FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
+ boolean on_odd_row; /* flag to remember which row we are on */
+} my_cquantizer;
+
+typedef my_cquantizer * my_cquantize_ptr;
+
+
+/*
+ * Policy-making subroutines for create_colormap and create_colorindex.
+ * These routines determine the colormap to be used. The rest of the module
+ * only assumes that the colormap is orthogonal.
+ *
+ * * select_ncolors decides how to divvy up the available colors
+ * among the components.
+ * * output_value defines the set of representative values for a component.
+ * * largest_input_value defines the mapping from input values to
+ * representative values for a component.
+ * Note that the latter two routines may impose different policies for
+ * different components, though this is not currently done.
+ */
+
+
+LOCAL(int)
+select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
+/* Determine allocation of desired colors to components, */
+/* and fill in Ncolors[] array to indicate choice. */
+/* Return value is total number of colors (product of Ncolors[] values). */
+{
+ int nc = cinfo->out_color_components; /* number of color components */
+ int max_colors = cinfo->desired_number_of_colors;
+ int total_colors, iroot, i, j;
+ boolean changed;
+ long temp;
+ static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
+
+ /* We can allocate at least the nc'th root of max_colors per component. */
+ /* Compute floor(nc'th root of max_colors). */
+ iroot = 1;
+ do {
+ iroot++;
+ temp = iroot; /* set temp = iroot ** nc */
+ for (i = 1; i < nc; i++)
+ temp *= iroot;
+ } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
+ iroot--; /* now iroot = floor(root) */
+
+ /* Must have at least 2 color values per component */
+ if (iroot < 2)
+ ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
+
+ /* Initialize to iroot color values for each component */
+ total_colors = 1;
+ for (i = 0; i < nc; i++) {
+ Ncolors[i] = iroot;
+ total_colors *= iroot;
+ }
+ /* We may be able to increment the count for one or more components without
+ * exceeding max_colors, though we know not all can be incremented.
+ * Sometimes, the first component can be incremented more than once!
+ * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
+ * In RGB colorspace, try to increment G first, then R, then B.
+ */
+ do {
+ changed = FALSE;
+ for (i = 0; i < nc; i++) {
+ j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
+ /* calculate new total_colors if Ncolors[j] is incremented */
+ temp = total_colors / Ncolors[j];
+ temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */
+ if (temp > (long) max_colors)
+ break; /* won't fit, done with this pass */
+ Ncolors[j]++; /* OK, apply the increment */
+ total_colors = (int) temp;
+ changed = TRUE;
+ }
+ } while (changed);
+
+ return total_colors;
+}
+
+
+LOCAL(int)
+output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
+/* Return j'th output value, where j will range from 0 to maxj */
+/* The output values must fall in 0..MAXJSAMPLE in increasing order */
+{
+ /* We always provide values 0 and MAXJSAMPLE for each component;
+ * any additional values are equally spaced between these limits.
+ * (Forcing the upper and lower values to the limits ensures that
+ * dithering can't produce a color outside the selected gamut.)
+ */
+ return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
+}
+
+
+LOCAL(int)
+largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
+/* Return largest input value that should map to j'th output value */
+/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
+{
+ /* Breakpoints are halfway between values returned by output_value */
+ return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
+}
+
+
+/*
+ * Create the colormap.
+ */
+
+LOCAL(void)
+create_colormap (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ JSAMPARRAY colormap; /* Created colormap */
+ int total_colors; /* Number of distinct output colors */
+ int i,j,k, nci, blksize, blkdist, ptr, val;
+
+ /* Select number of colors for each component */
+ total_colors = select_ncolors(cinfo, cquantize->Ncolors);
+
+ /* Report selected color counts */
+ if (cinfo->out_color_components == 3)
+ TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
+ total_colors, cquantize->Ncolors[0],
+ cquantize->Ncolors[1], cquantize->Ncolors[2]);
+ else
+ TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
+
+ /* Allocate and fill in the colormap. */
+ /* The colors are ordered in the map in standard row-major order, */
+ /* i.e. rightmost (highest-indexed) color changes most rapidly. */
+
+ colormap = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
+
+ /* blksize is number of adjacent repeated entries for a component */
+ /* blkdist is distance between groups of identical entries for a component */
+ blkdist = total_colors;
+
+ for (i = 0; i < cinfo->out_color_components; i++) {
+ /* fill in colormap entries for i'th color component */
+ nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
+ blksize = blkdist / nci;
+ for (j = 0; j < nci; j++) {
+ /* Compute j'th output value (out of nci) for component */
+ val = output_value(cinfo, i, j, nci-1);
+ /* Fill in all colormap entries that have this value of this component */
+ for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
+ /* fill in blksize entries beginning at ptr */
+ for (k = 0; k < blksize; k++)
+ colormap[i][ptr+k] = (JSAMPLE) val;
+ }
+ }
+ blkdist = blksize; /* blksize of this color is blkdist of next */
+ }
+
+ /* Save the colormap in private storage,
+ * where it will survive color quantization mode changes.
+ */
+ cquantize->sv_colormap = colormap;
+ cquantize->sv_actual = total_colors;
+}
+
+
+/*
+ * Create the color index table.
+ */
+
+LOCAL(void)
+create_colorindex (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ JSAMPROW indexptr;
+ int i,j,k, nci, blksize, val, pad;
+
+ /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
+ * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
+ * This is not necessary in the other dithering modes. However, we
+ * flag whether it was done in case user changes dithering mode.
+ */
+ if (cinfo->dither_mode == JDITHER_ORDERED) {
+ pad = MAXJSAMPLE*2;
+ cquantize->is_padded = TRUE;
+ } else {
+ pad = 0;
+ cquantize->is_padded = FALSE;
+ }
+
+ cquantize->colorindex = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (MAXJSAMPLE+1 + pad),
+ (JDIMENSION) cinfo->out_color_components);
+
+ /* blksize is number of adjacent repeated entries for a component */
+ blksize = cquantize->sv_actual;
+
+ for (i = 0; i < cinfo->out_color_components; i++) {
+ /* fill in colorindex entries for i'th color component */
+ nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
+ blksize = blksize / nci;
+
+ /* adjust colorindex pointers to provide padding at negative indexes. */
+ if (pad)
+ cquantize->colorindex[i] += MAXJSAMPLE;
+
+ /* in loop, val = index of current output value, */
+ /* and k = largest j that maps to current val */
+ indexptr = cquantize->colorindex[i];
+ val = 0;
+ k = largest_input_value(cinfo, i, 0, nci-1);
+ for (j = 0; j <= MAXJSAMPLE; j++) {
+ while (j > k) /* advance val if past boundary */
+ k = largest_input_value(cinfo, i, ++val, nci-1);
+ /* premultiply so that no multiplication needed in main processing */
+ indexptr[j] = (JSAMPLE) (val * blksize);
+ }
+ /* Pad at both ends if necessary */
+ if (pad)
+ for (j = 1; j <= MAXJSAMPLE; j++) {
+ indexptr[-j] = indexptr[0];
+ indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
+ }
+ }
+}
+
+
+/*
+ * Create an ordered-dither array for a component having ncolors
+ * distinct output values.
+ */
+
+LOCAL(ODITHER_MATRIX_PTR)
+make_odither_array (j_decompress_ptr cinfo, int ncolors)
+{
+ ODITHER_MATRIX_PTR odither;
+ int j,k;
+ INT32 num,den;
+
+ odither = (ODITHER_MATRIX_PTR)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(ODITHER_MATRIX));
+ /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
+ * Hence the dither value for the matrix cell with fill order f
+ * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
+ * On 16-bit-int machine, be careful to avoid overflow.
+ */
+ den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
+ for (j = 0; j < ODITHER_SIZE; j++) {
+ for (k = 0; k < ODITHER_SIZE; k++) {
+ num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
+ * MAXJSAMPLE;
+ /* Ensure round towards zero despite C's lack of consistency
+ * about rounding negative values in integer division...
+ */
+ odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
+ }
+ }
+ return odither;
+}
+
+
+/*
+ * Create the ordered-dither tables.
+ * Components having the same number of representative colors may
+ * share a dither table.
+ */
+
+LOCAL(void)
+create_odither_tables (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ ODITHER_MATRIX_PTR odither;
+ int i, j, nci;
+
+ for (i = 0; i < cinfo->out_color_components; i++) {
+ nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
+ odither = NULL; /* search for matching prior component */
+ for (j = 0; j < i; j++) {
+ if (nci == cquantize->Ncolors[j]) {
+ odither = cquantize->odither[j];
+ break;
+ }
+ }
+ if (odither == NULL) /* need a new table? */
+ odither = make_odither_array(cinfo, nci);
+ cquantize->odither[i] = odither;
+ }
+}
+
+
+/*
+ * Map some rows of pixels to the output colormapped representation.
+ */
+
+METHODDEF(void)
+color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+/* General case, no dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ JSAMPARRAY colorindex = cquantize->colorindex;
+ register int pixcode, ci;
+ register JSAMPROW ptrin, ptrout;
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+ register int nc = cinfo->out_color_components;
+
+ for (row = 0; row < num_rows; row++) {
+ ptrin = input_buf[row];
+ ptrout = output_buf[row];
+ for (col = width; col > 0; col--) {
+ pixcode = 0;
+ for (ci = 0; ci < nc; ci++) {
+ pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
+ }
+ *ptrout++ = (JSAMPLE) pixcode;
+ }
+ }
+}
+
+
+METHODDEF(void)
+color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+/* Fast path for out_color_components==3, no dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ register int pixcode;
+ register JSAMPROW ptrin, ptrout;
+ JSAMPROW colorindex0 = cquantize->colorindex[0];
+ JSAMPROW colorindex1 = cquantize->colorindex[1];
+ JSAMPROW colorindex2 = cquantize->colorindex[2];
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+
+ for (row = 0; row < num_rows; row++) {
+ ptrin = input_buf[row];
+ ptrout = output_buf[row];
+ for (col = width; col > 0; col--) {
+ pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
+ pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
+ pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
+ *ptrout++ = (JSAMPLE) pixcode;
+ }
+ }
+}
+
+
+METHODDEF(void)
+quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+/* General case, with ordered dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ register JSAMPROW input_ptr;
+ register JSAMPROW output_ptr;
+ JSAMPROW colorindex_ci;
+ int * dither; /* points to active row of dither matrix */
+ int row_index, col_index; /* current indexes into dither matrix */
+ int nc = cinfo->out_color_components;
+ int ci;
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+
+ for (row = 0; row < num_rows; row++) {
+ /* Initialize output values to 0 so can process components separately */
+ jzero_far((void FAR *) output_buf[row],
+ (size_t) (width * SIZEOF(JSAMPLE)));
+ row_index = cquantize->row_index;
+ for (ci = 0; ci < nc; ci++) {
+ input_ptr = input_buf[row] + ci;
+ output_ptr = output_buf[row];
+ colorindex_ci = cquantize->colorindex[ci];
+ dither = cquantize->odither[ci][row_index];
+ col_index = 0;
+
+ for (col = width; col > 0; col--) {
+ /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
+ * select output value, accumulate into output code for this pixel.
+ * Range-limiting need not be done explicitly, as we have extended
+ * the colorindex table to produce the right answers for out-of-range
+ * inputs. The maximum dither is +- MAXJSAMPLE; this sets the
+ * required amount of padding.
+ */
+ *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
+ input_ptr += nc;
+ output_ptr++;
+ col_index = (col_index + 1) & ODITHER_MASK;
+ }
+ }
+ /* Advance row index for next row */
+ row_index = (row_index + 1) & ODITHER_MASK;
+ cquantize->row_index = row_index;
+ }
+}
+
+
+METHODDEF(void)
+quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+/* Fast path for out_color_components==3, with ordered dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ register int pixcode;
+ register JSAMPROW input_ptr;
+ register JSAMPROW output_ptr;
+ JSAMPROW colorindex0 = cquantize->colorindex[0];
+ JSAMPROW colorindex1 = cquantize->colorindex[1];
+ JSAMPROW colorindex2 = cquantize->colorindex[2];
+ int * dither0; /* points to active row of dither matrix */
+ int * dither1;
+ int * dither2;
+ int row_index, col_index; /* current indexes into dither matrix */
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+
+ for (row = 0; row < num_rows; row++) {
+ row_index = cquantize->row_index;
+ input_ptr = input_buf[row];
+ output_ptr = output_buf[row];
+ dither0 = cquantize->odither[0][row_index];
+ dither1 = cquantize->odither[1][row_index];
+ dither2 = cquantize->odither[2][row_index];
+ col_index = 0;
+
+ for (col = width; col > 0; col--) {
+ pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
+ dither0[col_index]]);
+ pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
+ dither1[col_index]]);
+ pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
+ dither2[col_index]]);
+ *output_ptr++ = (JSAMPLE) pixcode;
+ col_index = (col_index + 1) & ODITHER_MASK;
+ }
+ row_index = (row_index + 1) & ODITHER_MASK;
+ cquantize->row_index = row_index;
+ }
+}
+
+
+METHODDEF(void)
+quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+/* General case, with Floyd-Steinberg dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ register LOCFSERROR cur; /* current error or pixel value */
+ LOCFSERROR belowerr; /* error for pixel below cur */
+ LOCFSERROR bpreverr; /* error for below/prev col */
+ LOCFSERROR bnexterr; /* error for below/next col */
+ LOCFSERROR delta;
+ register FSERRPTR errorptr; /* => fserrors[] at column before current */
+ register JSAMPROW input_ptr;
+ register JSAMPROW output_ptr;
+ JSAMPROW colorindex_ci;
+ JSAMPROW colormap_ci;
+ int pixcode;
+ int nc = cinfo->out_color_components;
+ int dir; /* 1 for left-to-right, -1 for right-to-left */
+ int dirnc; /* dir * nc */
+ int ci;
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+ JSAMPLE *range_limit = cinfo->sample_range_limit;
+ SHIFT_TEMPS
+
+ for (row = 0; row < num_rows; row++) {
+ /* Initialize output values to 0 so can process components separately */
+ jzero_far((void FAR *) output_buf[row],
+ (size_t) (width * SIZEOF(JSAMPLE)));
+ for (ci = 0; ci < nc; ci++) {
+ input_ptr = input_buf[row] + ci;
+ output_ptr = output_buf[row];
+ if (cquantize->on_odd_row) {
+ /* work right to left in this row */
+ input_ptr += (width-1) * nc; /* so point to rightmost pixel */
+ output_ptr += width-1;
+ dir = -1;
+ dirnc = -nc;
+ errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
+ } else {
+ /* work left to right in this row */
+ dir = 1;
+ dirnc = nc;
+ errorptr = cquantize->fserrors[ci]; /* => entry before first column */
+ }
+ colorindex_ci = cquantize->colorindex[ci];
+ colormap_ci = cquantize->sv_colormap[ci];
+ /* Preset error values: no error propagated to first pixel from left */
+ cur = 0;
+ /* and no error propagated to row below yet */
+ belowerr = bpreverr = 0;
+
+ for (col = width; col > 0; col--) {
+ /* cur holds the error propagated from the previous pixel on the
+ * current line. Add the error propagated from the previous line
+ * to form the complete error correction term for this pixel, and
+ * round the error term (which is expressed * 16) to an integer.
+ * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
+ * for either sign of the error value.
+ * Note: errorptr points to *previous* column's array entry.
+ */
+ cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
+ /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
+ * The maximum error is +- MAXJSAMPLE; this sets the required size
+ * of the range_limit array.
+ */
+ cur += GETJSAMPLE(*input_ptr);
+ cur = GETJSAMPLE(range_limit[cur]);
+ /* Select output value, accumulate into output code for this pixel */
+ pixcode = GETJSAMPLE(colorindex_ci[cur]);
+ *output_ptr += (JSAMPLE) pixcode;
+ /* Compute actual representation error at this pixel */
+ /* Note: we can do this even though we don't have the final */
+ /* pixel code, because the colormap is orthogonal. */
+ cur -= GETJSAMPLE(colormap_ci[pixcode]);
+ /* Compute error fractions to be propagated to adjacent pixels.
+ * Add these into the running sums, and simultaneously shift the
+ * next-line error sums left by 1 column.
+ */
+ bnexterr = cur;
+ delta = cur * 2;
+ cur += delta; /* form error * 3 */
+ errorptr[0] = (FSERROR) (bpreverr + cur);
+ cur += delta; /* form error * 5 */
+ bpreverr = belowerr + cur;
+ belowerr = bnexterr;
+ cur += delta; /* form error * 7 */
+ /* At this point cur contains the 7/16 error value to be propagated
+ * to the next pixel on the current line, and all the errors for the
+ * next line have been shifted over. We are therefore ready to move on.
+ */
+ input_ptr += dirnc; /* advance input ptr to next column */
+ output_ptr += dir; /* advance output ptr to next column */
+ errorptr += dir; /* advance errorptr to current column */
+ }
+ /* Post-loop cleanup: we must unload the final error value into the
+ * final fserrors[] entry. Note we need not unload belowerr because
+ * it is for the dummy column before or after the actual array.
+ */
+ errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
+ }
+ cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
+ }
+}
+
+
+/*
+ * Allocate workspace for Floyd-Steinberg errors.
+ */
+
+LOCAL(void)
+alloc_fs_workspace (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ size_t arraysize;
+ int i;
+
+ arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
+ for (i = 0; i < cinfo->out_color_components; i++) {
+ cquantize->fserrors[i] = (FSERRPTR)
+ (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
+ }
+}
+
+
+/*
+ * Initialize for one-pass color quantization.
+ */
+
+METHODDEF(void)
+start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ size_t arraysize;
+ int i;
+
+ /* Install my colormap. */
+ cinfo->colormap = cquantize->sv_colormap;
+ cinfo->actual_number_of_colors = cquantize->sv_actual;
+
+ /* Initialize for desired dithering mode. */
+ switch (cinfo->dither_mode) {
+ case JDITHER_NONE:
+ if (cinfo->out_color_components == 3)
+ cquantize->pub.color_quantize = color_quantize3;
+ else
+ cquantize->pub.color_quantize = color_quantize;
+ break;
+ case JDITHER_ORDERED:
+ if (cinfo->out_color_components == 3)
+ cquantize->pub.color_quantize = quantize3_ord_dither;
+ else
+ cquantize->pub.color_quantize = quantize_ord_dither;
+ cquantize->row_index = 0; /* initialize state for ordered dither */
+ /* If user changed to ordered dither from another mode,
+ * we must recreate the color index table with padding.
+ * This will cost extra space, but probably isn't very likely.
+ */
+ if (! cquantize->is_padded)
+ create_colorindex(cinfo);
+ /* Create ordered-dither tables if we didn't already. */
+ if (cquantize->odither[0] == NULL)
+ create_odither_tables(cinfo);
+ break;
+ case JDITHER_FS:
+ cquantize->pub.color_quantize = quantize_fs_dither;
+ cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
+ /* Allocate Floyd-Steinberg workspace if didn't already. */
+ if (cquantize->fserrors[0] == NULL)
+ alloc_fs_workspace(cinfo);
+ /* Initialize the propagated errors to zero. */
+ arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
+ for (i = 0; i < cinfo->out_color_components; i++)
+ jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+}
+
+
+/*
+ * Finish up at the end of the pass.
+ */
+
+METHODDEF(void)
+finish_pass_1_quant (j_decompress_ptr cinfo)
+{
+ /* no work in 1-pass case */
+}
+
+
+/*
+ * Switch to a new external colormap between output passes.
+ * Shouldn't get to this module!
+ */
+
+METHODDEF(void)
+new_color_map_1_quant (j_decompress_ptr cinfo)
+{
+ ERREXIT(cinfo, JERR_MODE_CHANGE);
+}
+
+
+/*
+ * Module initialization routine for 1-pass color quantization.
+ */
+
+GLOBAL(void)
+jinit_1pass_quantizer (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize;
+
+ cquantize = (my_cquantize_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_cquantizer));
+ cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
+ cquantize->pub.start_pass = start_pass_1_quant;
+ cquantize->pub.finish_pass = finish_pass_1_quant;
+ cquantize->pub.new_color_map = new_color_map_1_quant;
+ cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
+ cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */
+
+ /* Make sure my internal arrays won't overflow */
+ if (cinfo->out_color_components > MAX_Q_COMPS)
+ ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
+ /* Make sure colormap indexes can be represented by JSAMPLEs */
+ if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
+ ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
+
+ /* Create the colormap and color index table. */
+ create_colormap(cinfo);
+ create_colorindex(cinfo);
+
+ /* Allocate Floyd-Steinberg workspace now if requested.
+ * We do this now since it is FAR storage and may affect the memory
+ * manager's space calculations. If the user changes to FS dither
+ * mode in a later pass, we will allocate the space then, and will
+ * possibly overrun the max_memory_to_use setting.
+ */
+ if (cinfo->dither_mode == JDITHER_FS)
+ alloc_fs_workspace(cinfo);
+}
+
+#endif /* QUANT_1PASS_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jquant2.c b/test/monniaux/jpeg-6b/jquant2.c
new file mode 100644
index 00000000..af601e33
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jquant2.c
@@ -0,0 +1,1310 @@
+/*
+ * jquant2.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains 2-pass color quantization (color mapping) routines.
+ * These routines provide selection of a custom color map for an image,
+ * followed by mapping of the image to that color map, with optional
+ * Floyd-Steinberg dithering.
+ * It is also possible to use just the second pass to map to an arbitrary
+ * externally-given color map.
+ *
+ * Note: ordered dithering is not supported, since there isn't any fast
+ * way to compute intercolor distances; it's unclear that ordered dither's
+ * fundamental assumptions even hold with an irregularly spaced color map.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+
+/*
+ * This module implements the well-known Heckbert paradigm for color
+ * quantization. Most of the ideas used here can be traced back to
+ * Heckbert's seminal paper
+ * Heckbert, Paul. "Color Image Quantization for Frame Buffer Display",
+ * Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
+ *
+ * In the first pass over the image, we accumulate a histogram showing the
+ * usage count of each possible color. To keep the histogram to a reasonable
+ * size, we reduce the precision of the input; typical practice is to retain
+ * 5 or 6 bits per color, so that 8 or 4 different input values are counted
+ * in the same histogram cell.
+ *
+ * Next, the color-selection step begins with a box representing the whole
+ * color space, and repeatedly splits the "largest" remaining box until we
+ * have as many boxes as desired colors. Then the mean color in each
+ * remaining box becomes one of the possible output colors.
+ *
+ * The second pass over the image maps each input pixel to the closest output
+ * color (optionally after applying a Floyd-Steinberg dithering correction).
+ * This mapping is logically trivial, but making it go fast enough requires
+ * considerable care.
+ *
+ * Heckbert-style quantizers vary a good deal in their policies for choosing
+ * the "largest" box and deciding where to cut it. The particular policies
+ * used here have proved out well in experimental comparisons, but better ones
+ * may yet be found.
+ *
+ * In earlier versions of the IJG code, this module quantized in YCbCr color
+ * space, processing the raw upsampled data without a color conversion step.
+ * This allowed the color conversion math to be done only once per colormap
+ * entry, not once per pixel. However, that optimization precluded other
+ * useful optimizations (such as merging color conversion with upsampling)
+ * and it also interfered with desired capabilities such as quantizing to an
+ * externally-supplied colormap. We have therefore abandoned that approach.
+ * The present code works in the post-conversion color space, typically RGB.
+ *
+ * To improve the visual quality of the results, we actually work in scaled
+ * RGB space, giving G distances more weight than R, and R in turn more than
+ * B. To do everything in integer math, we must use integer scale factors.
+ * The 2/3/1 scale factors used here correspond loosely to the relative
+ * weights of the colors in the NTSC grayscale equation.
+ * If you want to use this code to quantize a non-RGB color space, you'll
+ * probably need to change these scale factors.
+ */
+
+#define R_SCALE 2 /* scale R distances by this much */
+#define G_SCALE 3 /* scale G distances by this much */
+#define B_SCALE 1 /* and B by this much */
+
+/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
+ * in jmorecfg.h. As the code stands, it will do the right thing for R,G,B
+ * and B,G,R orders. If you define some other weird order in jmorecfg.h,
+ * you'll get compile errors until you extend this logic. In that case
+ * you'll probably want to tweak the histogram sizes too.
+ */
+
+#if RGB_RED == 0
+#define C0_SCALE R_SCALE
+#endif
+#if RGB_BLUE == 0
+#define C0_SCALE B_SCALE
+#endif
+#if RGB_GREEN == 1
+#define C1_SCALE G_SCALE
+#endif
+#if RGB_RED == 2
+#define C2_SCALE R_SCALE
+#endif
+#if RGB_BLUE == 2
+#define C2_SCALE B_SCALE
+#endif
+
+
+/*
+ * First we have the histogram data structure and routines for creating it.
+ *
+ * The number of bits of precision can be adjusted by changing these symbols.
+ * We recommend keeping 6 bits for G and 5 each for R and B.
+ * If you have plenty of memory and cycles, 6 bits all around gives marginally
+ * better results; if you are short of memory, 5 bits all around will save
+ * some space but degrade the results.
+ * To maintain a fully accurate histogram, we'd need to allocate a "long"
+ * (preferably unsigned long) for each cell. In practice this is overkill;
+ * we can get by with 16 bits per cell. Few of the cell counts will overflow,
+ * and clamping those that do overflow to the maximum value will give close-
+ * enough results. This reduces the recommended histogram size from 256Kb
+ * to 128Kb, which is a useful savings on PC-class machines.
+ * (In the second pass the histogram space is re-used for pixel mapping data;
+ * in that capacity, each cell must be able to store zero to the number of
+ * desired colors. 16 bits/cell is plenty for that too.)
+ * Since the JPEG code is intended to run in small memory model on 80x86
+ * machines, we can't just allocate the histogram in one chunk. Instead
+ * of a true 3-D array, we use a row of pointers to 2-D arrays. Each
+ * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
+ * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that
+ * on 80x86 machines, the pointer row is in near memory but the actual
+ * arrays are in far memory (same arrangement as we use for image arrays).
+ */
+
+#define MAXNUMCOLORS (MAXJSAMPLE+1) /* maximum size of colormap */
+
+/* These will do the right thing for either R,G,B or B,G,R color order,
+ * but you may not like the results for other color orders.
+ */
+#define HIST_C0_BITS 5 /* bits of precision in R/B histogram */
+#define HIST_C1_BITS 6 /* bits of precision in G histogram */
+#define HIST_C2_BITS 5 /* bits of precision in B/R histogram */
+
+/* Number of elements along histogram axes. */
+#define HIST_C0_ELEMS (1<<HIST_C0_BITS)
+#define HIST_C1_ELEMS (1<<HIST_C1_BITS)
+#define HIST_C2_ELEMS (1<<HIST_C2_BITS)
+
+/* These are the amounts to shift an input value to get a histogram index. */
+#define C0_SHIFT (BITS_IN_JSAMPLE-HIST_C0_BITS)
+#define C1_SHIFT (BITS_IN_JSAMPLE-HIST_C1_BITS)
+#define C2_SHIFT (BITS_IN_JSAMPLE-HIST_C2_BITS)
+
+
+typedef UINT16 histcell; /* histogram cell; prefer an unsigned type */
+
+typedef histcell FAR * histptr; /* for pointers to histogram cells */
+
+typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
+typedef hist1d FAR * hist2d; /* type for the 2nd-level pointers */
+typedef hist2d * hist3d; /* type for top-level pointer */
+
+
+/* Declarations for Floyd-Steinberg dithering.
+ *
+ * Errors are accumulated into the array fserrors[], at a resolution of
+ * 1/16th of a pixel count. The error at a given pixel is propagated
+ * to its not-yet-processed neighbors using the standard F-S fractions,
+ * ... (here) 7/16
+ * 3/16 5/16 1/16
+ * We work left-to-right on even rows, right-to-left on odd rows.
+ *
+ * We can get away with a single array (holding one row's worth of errors)
+ * by using it to store the current row's errors at pixel columns not yet
+ * processed, but the next row's errors at columns already processed. We
+ * need only a few extra variables to hold the errors immediately around the
+ * current column. (If we are lucky, those variables are in registers, but
+ * even if not, they're probably cheaper to access than array elements are.)
+ *
+ * The fserrors[] array has (#columns + 2) entries; the extra entry at
+ * each end saves us from special-casing the first and last pixels.
+ * Each entry is three values long, one value for each color component.
+ *
+ * Note: on a wide image, we might not have enough room in a PC's near data
+ * segment to hold the error array; so it is allocated with alloc_large.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef INT16 FSERROR; /* 16 bits should be enough */
+typedef int LOCFSERROR; /* use 'int' for calculation temps */
+#else
+typedef INT32 FSERROR; /* may need more than 16 bits */
+typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */
+#endif
+
+typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_color_quantizer pub; /* public fields */
+
+ /* Space for the eventually created colormap is stashed here */
+ JSAMPARRAY sv_colormap; /* colormap allocated at init time */
+ int desired; /* desired # of colors = size of colormap */
+
+ /* Variables for accumulating image statistics */
+ hist3d histogram; /* pointer to the histogram */
+
+ boolean needs_zeroed; /* TRUE if next pass must zero histogram */
+
+ /* Variables for Floyd-Steinberg dithering */
+ FSERRPTR fserrors; /* accumulated errors */
+ boolean on_odd_row; /* flag to remember which row we are on */
+ int * error_limiter; /* table for clamping the applied error */
+} my_cquantizer;
+
+typedef my_cquantizer * my_cquantize_ptr;
+
+
+/*
+ * Prescan some rows of pixels.
+ * In this module the prescan simply updates the histogram, which has been
+ * initialized to zeroes by start_pass.
+ * An output_buf parameter is required by the method signature, but no data
+ * is actually output (in fact the buffer controller is probably passing a
+ * NULL pointer).
+ */
+
+METHODDEF(void)
+prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ register JSAMPROW ptr;
+ register histptr histp;
+ register hist3d histogram = cquantize->histogram;
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+
+ for (row = 0; row < num_rows; row++) {
+ ptr = input_buf[row];
+ for (col = width; col > 0; col--) {
+ /* get pixel value and index into the histogram */
+ histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
+ [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
+ [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
+ /* increment, check for overflow and undo increment if so. */
+ if (++(*histp) <= 0)
+ (*histp)--;
+ ptr += 3;
+ }
+ }
+}
+
+
+/*
+ * Next we have the really interesting routines: selection of a colormap
+ * given the completed histogram.
+ * These routines work with a list of "boxes", each representing a rectangular
+ * subset of the input color space (to histogram precision).
+ */
+
+typedef struct {
+ /* The bounds of the box (inclusive); expressed as histogram indexes */
+ int c0min, c0max;
+ int c1min, c1max;
+ int c2min, c2max;
+ /* The volume (actually 2-norm) of the box */
+ INT32 volume;
+ /* The number of nonzero histogram cells within this box */
+ long colorcount;
+} box;
+
+typedef box * boxptr;
+
+
+LOCAL(boxptr)
+find_biggest_color_pop (boxptr boxlist, int numboxes)
+/* Find the splittable box with the largest color population */
+/* Returns NULL if no splittable boxes remain */
+{
+ register boxptr boxp;
+ register int i;
+ register long maxc = 0;
+ boxptr which = NULL;
+
+ for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
+ if (boxp->colorcount > maxc && boxp->volume > 0) {
+ which = boxp;
+ maxc = boxp->colorcount;
+ }
+ }
+ return which;
+}
+
+
+LOCAL(boxptr)
+find_biggest_volume (boxptr boxlist, int numboxes)
+/* Find the splittable box with the largest (scaled) volume */
+/* Returns NULL if no splittable boxes remain */
+{
+ register boxptr boxp;
+ register int i;
+ register INT32 maxv = 0;
+ boxptr which = NULL;
+
+ for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
+ if (boxp->volume > maxv) {
+ which = boxp;
+ maxv = boxp->volume;
+ }
+ }
+ return which;
+}
+
+
+LOCAL(void)
+update_box (j_decompress_ptr cinfo, boxptr boxp)
+/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
+/* and recompute its volume and population */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ histptr histp;
+ int c0,c1,c2;
+ int c0min,c0max,c1min,c1max,c2min,c2max;
+ INT32 dist0,dist1,dist2;
+ long ccount;
+
+ c0min = boxp->c0min; c0max = boxp->c0max;
+ c1min = boxp->c1min; c1max = boxp->c1max;
+ c2min = boxp->c2min; c2max = boxp->c2max;
+
+ if (c0max > c0min)
+ for (c0 = c0min; c0 <= c0max; c0++)
+ for (c1 = c1min; c1 <= c1max; c1++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++)
+ if (*histp++ != 0) {
+ boxp->c0min = c0min = c0;
+ goto have_c0min;
+ }
+ }
+ have_c0min:
+ if (c0max > c0min)
+ for (c0 = c0max; c0 >= c0min; c0--)
+ for (c1 = c1min; c1 <= c1max; c1++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++)
+ if (*histp++ != 0) {
+ boxp->c0max = c0max = c0;
+ goto have_c0max;
+ }
+ }
+ have_c0max:
+ if (c1max > c1min)
+ for (c1 = c1min; c1 <= c1max; c1++)
+ for (c0 = c0min; c0 <= c0max; c0++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++)
+ if (*histp++ != 0) {
+ boxp->c1min = c1min = c1;
+ goto have_c1min;
+ }
+ }
+ have_c1min:
+ if (c1max > c1min)
+ for (c1 = c1max; c1 >= c1min; c1--)
+ for (c0 = c0min; c0 <= c0max; c0++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++)
+ if (*histp++ != 0) {
+ boxp->c1max = c1max = c1;
+ goto have_c1max;
+ }
+ }
+ have_c1max:
+ if (c2max > c2min)
+ for (c2 = c2min; c2 <= c2max; c2++)
+ for (c0 = c0min; c0 <= c0max; c0++) {
+ histp = & histogram[c0][c1min][c2];
+ for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
+ if (*histp != 0) {
+ boxp->c2min = c2min = c2;
+ goto have_c2min;
+ }
+ }
+ have_c2min:
+ if (c2max > c2min)
+ for (c2 = c2max; c2 >= c2min; c2--)
+ for (c0 = c0min; c0 <= c0max; c0++) {
+ histp = & histogram[c0][c1min][c2];
+ for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
+ if (*histp != 0) {
+ boxp->c2max = c2max = c2;
+ goto have_c2max;
+ }
+ }
+ have_c2max:
+
+ /* Update box volume.
+ * We use 2-norm rather than real volume here; this biases the method
+ * against making long narrow boxes, and it has the side benefit that
+ * a box is splittable iff norm > 0.
+ * Since the differences are expressed in histogram-cell units,
+ * we have to shift back to JSAMPLE units to get consistent distances;
+ * after which, we scale according to the selected distance scale factors.
+ */
+ dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
+ dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
+ dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
+ boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
+
+ /* Now scan remaining volume of box and compute population */
+ ccount = 0;
+ for (c0 = c0min; c0 <= c0max; c0++)
+ for (c1 = c1min; c1 <= c1max; c1++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++, histp++)
+ if (*histp != 0) {
+ ccount++;
+ }
+ }
+ boxp->colorcount = ccount;
+}
+
+
+LOCAL(int)
+median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
+ int desired_colors)
+/* Repeatedly select and split the largest box until we have enough boxes */
+{
+ int n,lb;
+ int c0,c1,c2,cmax;
+ register boxptr b1,b2;
+
+ while (numboxes < desired_colors) {
+ /* Select box to split.
+ * Current algorithm: by population for first half, then by volume.
+ */
+ if (numboxes*2 <= desired_colors) {
+ b1 = find_biggest_color_pop(boxlist, numboxes);
+ } else {
+ b1 = find_biggest_volume(boxlist, numboxes);
+ }
+ if (b1 == NULL) /* no splittable boxes left! */
+ break;
+ b2 = &boxlist[numboxes]; /* where new box will go */
+ /* Copy the color bounds to the new box. */
+ b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
+ b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
+ /* Choose which axis to split the box on.
+ * Current algorithm: longest scaled axis.
+ * See notes in update_box about scaling distances.
+ */
+ c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
+ c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
+ c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
+ /* We want to break any ties in favor of green, then red, blue last.
+ * This code does the right thing for R,G,B or B,G,R color orders only.
+ */
+#if RGB_RED == 0
+ cmax = c1; n = 1;
+ if (c0 > cmax) { cmax = c0; n = 0; }
+ if (c2 > cmax) { n = 2; }
+#else
+ cmax = c1; n = 1;
+ if (c2 > cmax) { cmax = c2; n = 2; }
+ if (c0 > cmax) { n = 0; }
+#endif
+ /* Choose split point along selected axis, and update box bounds.
+ * Current algorithm: split at halfway point.
+ * (Since the box has been shrunk to minimum volume,
+ * any split will produce two nonempty subboxes.)
+ * Note that lb value is max for lower box, so must be < old max.
+ */
+ switch (n) {
+ case 0:
+ lb = (b1->c0max + b1->c0min) / 2;
+ b1->c0max = lb;
+ b2->c0min = lb+1;
+ break;
+ case 1:
+ lb = (b1->c1max + b1->c1min) / 2;
+ b1->c1max = lb;
+ b2->c1min = lb+1;
+ break;
+ case 2:
+ lb = (b1->c2max + b1->c2min) / 2;
+ b1->c2max = lb;
+ b2->c2min = lb+1;
+ break;
+ }
+ /* Update stats for boxes */
+ update_box(cinfo, b1);
+ update_box(cinfo, b2);
+ numboxes++;
+ }
+ return numboxes;
+}
+
+
+LOCAL(void)
+compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
+/* Compute representative color for a box, put it in colormap[icolor] */
+{
+ /* Current algorithm: mean weighted by pixels (not colors) */
+ /* Note it is important to get the rounding correct! */
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ histptr histp;
+ int c0,c1,c2;
+ int c0min,c0max,c1min,c1max,c2min,c2max;
+ long count;
+ long total = 0;
+ long c0total = 0;
+ long c1total = 0;
+ long c2total = 0;
+
+ c0min = boxp->c0min; c0max = boxp->c0max;
+ c1min = boxp->c1min; c1max = boxp->c1max;
+ c2min = boxp->c2min; c2max = boxp->c2max;
+
+ for (c0 = c0min; c0 <= c0max; c0++)
+ for (c1 = c1min; c1 <= c1max; c1++) {
+ histp = & histogram[c0][c1][c2min];
+ for (c2 = c2min; c2 <= c2max; c2++) {
+ if ((count = *histp++) != 0) {
+ total += count;
+ c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
+ c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
+ c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
+ }
+ }
+ }
+
+ cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
+ cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
+ cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
+}
+
+
+LOCAL(void)
+select_colors (j_decompress_ptr cinfo, int desired_colors)
+/* Master routine for color selection */
+{
+ boxptr boxlist;
+ int numboxes;
+ int i;
+
+ /* Allocate workspace for box list */
+ boxlist = (boxptr) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
+ /* Initialize one box containing whole space */
+ numboxes = 1;
+ boxlist[0].c0min = 0;
+ boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
+ boxlist[0].c1min = 0;
+ boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
+ boxlist[0].c2min = 0;
+ boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
+ /* Shrink it to actually-used volume and set its statistics */
+ update_box(cinfo, & boxlist[0]);
+ /* Perform median-cut to produce final box list */
+ numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
+ /* Compute the representative color for each box, fill colormap */
+ for (i = 0; i < numboxes; i++)
+ compute_color(cinfo, & boxlist[i], i);
+ cinfo->actual_number_of_colors = numboxes;
+ TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
+}
+
+
+/*
+ * These routines are concerned with the time-critical task of mapping input
+ * colors to the nearest color in the selected colormap.
+ *
+ * We re-use the histogram space as an "inverse color map", essentially a
+ * cache for the results of nearest-color searches. All colors within a
+ * histogram cell will be mapped to the same colormap entry, namely the one
+ * closest to the cell's center. This may not be quite the closest entry to
+ * the actual input color, but it's almost as good. A zero in the cache
+ * indicates we haven't found the nearest color for that cell yet; the array
+ * is cleared to zeroes before starting the mapping pass. When we find the
+ * nearest color for a cell, its colormap index plus one is recorded in the
+ * cache for future use. The pass2 scanning routines call fill_inverse_cmap
+ * when they need to use an unfilled entry in the cache.
+ *
+ * Our method of efficiently finding nearest colors is based on the "locally
+ * sorted search" idea described by Heckbert and on the incremental distance
+ * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
+ * Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that
+ * the distances from a given colormap entry to each cell of the histogram can
+ * be computed quickly using an incremental method: the differences between
+ * distances to adjacent cells themselves differ by a constant. This allows a
+ * fairly fast implementation of the "brute force" approach of computing the
+ * distance from every colormap entry to every histogram cell. Unfortunately,
+ * it needs a work array to hold the best-distance-so-far for each histogram
+ * cell (because the inner loop has to be over cells, not colormap entries).
+ * The work array elements have to be INT32s, so the work array would need
+ * 256Kb at our recommended precision. This is not feasible in DOS machines.
+ *
+ * To get around these problems, we apply Thomas' method to compute the
+ * nearest colors for only the cells within a small subbox of the histogram.
+ * The work array need be only as big as the subbox, so the memory usage
+ * problem is solved. Furthermore, we need not fill subboxes that are never
+ * referenced in pass2; many images use only part of the color gamut, so a
+ * fair amount of work is saved. An additional advantage of this
+ * approach is that we can apply Heckbert's locality criterion to quickly
+ * eliminate colormap entries that are far away from the subbox; typically
+ * three-fourths of the colormap entries are rejected by Heckbert's criterion,
+ * and we need not compute their distances to individual cells in the subbox.
+ * The speed of this approach is heavily influenced by the subbox size: too
+ * small means too much overhead, too big loses because Heckbert's criterion
+ * can't eliminate as many colormap entries. Empirically the best subbox
+ * size seems to be about 1/512th of the histogram (1/8th in each direction).
+ *
+ * Thomas' article also describes a refined method which is asymptotically
+ * faster than the brute-force method, but it is also far more complex and
+ * cannot efficiently be applied to small subboxes. It is therefore not
+ * useful for programs intended to be portable to DOS machines. On machines
+ * with plenty of memory, filling the whole histogram in one shot with Thomas'
+ * refined method might be faster than the present code --- but then again,
+ * it might not be any faster, and it's certainly more complicated.
+ */
+
+
+/* log2(histogram cells in update box) for each axis; this can be adjusted */
+#define BOX_C0_LOG (HIST_C0_BITS-3)
+#define BOX_C1_LOG (HIST_C1_BITS-3)
+#define BOX_C2_LOG (HIST_C2_BITS-3)
+
+#define BOX_C0_ELEMS (1<<BOX_C0_LOG) /* # of hist cells in update box */
+#define BOX_C1_ELEMS (1<<BOX_C1_LOG)
+#define BOX_C2_ELEMS (1<<BOX_C2_LOG)
+
+#define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG)
+#define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG)
+#define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG)
+
+
+/*
+ * The next three routines implement inverse colormap filling. They could
+ * all be folded into one big routine, but splitting them up this way saves
+ * some stack space (the mindist[] and bestdist[] arrays need not coexist)
+ * and may allow some compilers to produce better code by registerizing more
+ * inner-loop variables.
+ */
+
+LOCAL(int)
+find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
+ JSAMPLE colorlist[])
+/* Locate the colormap entries close enough to an update box to be candidates
+ * for the nearest entry to some cell(s) in the update box. The update box
+ * is specified by the center coordinates of its first cell. The number of
+ * candidate colormap entries is returned, and their colormap indexes are
+ * placed in colorlist[].
+ * This routine uses Heckbert's "locally sorted search" criterion to select
+ * the colors that need further consideration.
+ */
+{
+ int numcolors = cinfo->actual_number_of_colors;
+ int maxc0, maxc1, maxc2;
+ int centerc0, centerc1, centerc2;
+ int i, x, ncolors;
+ INT32 minmaxdist, min_dist, max_dist, tdist;
+ INT32 mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
+
+ /* Compute true coordinates of update box's upper corner and center.
+ * Actually we compute the coordinates of the center of the upper-corner
+ * histogram cell, which are the upper bounds of the volume we care about.
+ * Note that since ">>" rounds down, the "center" values may be closer to
+ * min than to max; hence comparisons to them must be "<=", not "<".
+ */
+ maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
+ centerc0 = (minc0 + maxc0) >> 1;
+ maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
+ centerc1 = (minc1 + maxc1) >> 1;
+ maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
+ centerc2 = (minc2 + maxc2) >> 1;
+
+ /* For each color in colormap, find:
+ * 1. its minimum squared-distance to any point in the update box
+ * (zero if color is within update box);
+ * 2. its maximum squared-distance to any point in the update box.
+ * Both of these can be found by considering only the corners of the box.
+ * We save the minimum distance for each color in mindist[];
+ * only the smallest maximum distance is of interest.
+ */
+ minmaxdist = 0x7FFFFFFFL;
+
+ for (i = 0; i < numcolors; i++) {
+ /* We compute the squared-c0-distance term, then add in the other two. */
+ x = GETJSAMPLE(cinfo->colormap[0][i]);
+ if (x < minc0) {
+ tdist = (x - minc0) * C0_SCALE;
+ min_dist = tdist*tdist;
+ tdist = (x - maxc0) * C0_SCALE;
+ max_dist = tdist*tdist;
+ } else if (x > maxc0) {
+ tdist = (x - maxc0) * C0_SCALE;
+ min_dist = tdist*tdist;
+ tdist = (x - minc0) * C0_SCALE;
+ max_dist = tdist*tdist;
+ } else {
+ /* within cell range so no contribution to min_dist */
+ min_dist = 0;
+ if (x <= centerc0) {
+ tdist = (x - maxc0) * C0_SCALE;
+ max_dist = tdist*tdist;
+ } else {
+ tdist = (x - minc0) * C0_SCALE;
+ max_dist = tdist*tdist;
+ }
+ }
+
+ x = GETJSAMPLE(cinfo->colormap[1][i]);
+ if (x < minc1) {
+ tdist = (x - minc1) * C1_SCALE;
+ min_dist += tdist*tdist;
+ tdist = (x - maxc1) * C1_SCALE;
+ max_dist += tdist*tdist;
+ } else if (x > maxc1) {
+ tdist = (x - maxc1) * C1_SCALE;
+ min_dist += tdist*tdist;
+ tdist = (x - minc1) * C1_SCALE;
+ max_dist += tdist*tdist;
+ } else {
+ /* within cell range so no contribution to min_dist */
+ if (x <= centerc1) {
+ tdist = (x - maxc1) * C1_SCALE;
+ max_dist += tdist*tdist;
+ } else {
+ tdist = (x - minc1) * C1_SCALE;
+ max_dist += tdist*tdist;
+ }
+ }
+
+ x = GETJSAMPLE(cinfo->colormap[2][i]);
+ if (x < minc2) {
+ tdist = (x - minc2) * C2_SCALE;
+ min_dist += tdist*tdist;
+ tdist = (x - maxc2) * C2_SCALE;
+ max_dist += tdist*tdist;
+ } else if (x > maxc2) {
+ tdist = (x - maxc2) * C2_SCALE;
+ min_dist += tdist*tdist;
+ tdist = (x - minc2) * C2_SCALE;
+ max_dist += tdist*tdist;
+ } else {
+ /* within cell range so no contribution to min_dist */
+ if (x <= centerc2) {
+ tdist = (x - maxc2) * C2_SCALE;
+ max_dist += tdist*tdist;
+ } else {
+ tdist = (x - minc2) * C2_SCALE;
+ max_dist += tdist*tdist;
+ }
+ }
+
+ mindist[i] = min_dist; /* save away the results */
+ if (max_dist < minmaxdist)
+ minmaxdist = max_dist;
+ }
+
+ /* Now we know that no cell in the update box is more than minmaxdist
+ * away from some colormap entry. Therefore, only colors that are
+ * within minmaxdist of some part of the box need be considered.
+ */
+ ncolors = 0;
+ for (i = 0; i < numcolors; i++) {
+ if (mindist[i] <= minmaxdist)
+ colorlist[ncolors++] = (JSAMPLE) i;
+ }
+ return ncolors;
+}
+
+
+LOCAL(void)
+find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
+ int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
+/* Find the closest colormap entry for each cell in the update box,
+ * given the list of candidate colors prepared by find_nearby_colors.
+ * Return the indexes of the closest entries in the bestcolor[] array.
+ * This routine uses Thomas' incremental distance calculation method to
+ * find the distance from a colormap entry to successive cells in the box.
+ */
+{
+ int ic0, ic1, ic2;
+ int i, icolor;
+ register INT32 * bptr; /* pointer into bestdist[] array */
+ JSAMPLE * cptr; /* pointer into bestcolor[] array */
+ INT32 dist0, dist1; /* initial distance values */
+ register INT32 dist2; /* current distance in inner loop */
+ INT32 xx0, xx1; /* distance increments */
+ register INT32 xx2;
+ INT32 inc0, inc1, inc2; /* initial values for increments */
+ /* This array holds the distance to the nearest-so-far color for each cell */
+ INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
+
+ /* Initialize best-distance for each cell of the update box */
+ bptr = bestdist;
+ for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
+ *bptr++ = 0x7FFFFFFFL;
+
+ /* For each color selected by find_nearby_colors,
+ * compute its distance to the center of each cell in the box.
+ * If that's less than best-so-far, update best distance and color number.
+ */
+
+ /* Nominal steps between cell centers ("x" in Thomas article) */
+#define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE)
+#define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE)
+#define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE)
+
+ for (i = 0; i < numcolors; i++) {
+ icolor = GETJSAMPLE(colorlist[i]);
+ /* Compute (square of) distance from minc0/c1/c2 to this color */
+ inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
+ dist0 = inc0*inc0;
+ inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
+ dist0 += inc1*inc1;
+ inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
+ dist0 += inc2*inc2;
+ /* Form the initial difference increments */
+ inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
+ inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
+ inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
+ /* Now loop over all cells in box, updating distance per Thomas method */
+ bptr = bestdist;
+ cptr = bestcolor;
+ xx0 = inc0;
+ for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
+ dist1 = dist0;
+ xx1 = inc1;
+ for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
+ dist2 = dist1;
+ xx2 = inc2;
+ for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
+ if (dist2 < *bptr) {
+ *bptr = dist2;
+ *cptr = (JSAMPLE) icolor;
+ }
+ dist2 += xx2;
+ xx2 += 2 * STEP_C2 * STEP_C2;
+ bptr++;
+ cptr++;
+ }
+ dist1 += xx1;
+ xx1 += 2 * STEP_C1 * STEP_C1;
+ }
+ dist0 += xx0;
+ xx0 += 2 * STEP_C0 * STEP_C0;
+ }
+ }
+}
+
+
+LOCAL(void)
+fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
+/* Fill the inverse-colormap entries in the update box that contains */
+/* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */
+/* we can fill as many others as we wish.) */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ int minc0, minc1, minc2; /* lower left corner of update box */
+ int ic0, ic1, ic2;
+ register JSAMPLE * cptr; /* pointer into bestcolor[] array */
+ register histptr cachep; /* pointer into main cache array */
+ /* This array lists the candidate colormap indexes. */
+ JSAMPLE colorlist[MAXNUMCOLORS];
+ int numcolors; /* number of candidate colors */
+ /* This array holds the actually closest colormap index for each cell. */
+ JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
+
+ /* Convert cell coordinates to update box ID */
+ c0 >>= BOX_C0_LOG;
+ c1 >>= BOX_C1_LOG;
+ c2 >>= BOX_C2_LOG;
+
+ /* Compute true coordinates of update box's origin corner.
+ * Actually we compute the coordinates of the center of the corner
+ * histogram cell, which are the lower bounds of the volume we care about.
+ */
+ minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
+ minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
+ minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
+
+ /* Determine which colormap entries are close enough to be candidates
+ * for the nearest entry to some cell in the update box.
+ */
+ numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
+
+ /* Determine the actually nearest colors. */
+ find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
+ bestcolor);
+
+ /* Save the best color numbers (plus 1) in the main cache array */
+ c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */
+ c1 <<= BOX_C1_LOG;
+ c2 <<= BOX_C2_LOG;
+ cptr = bestcolor;
+ for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
+ for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
+ cachep = & histogram[c0+ic0][c1+ic1][c2];
+ for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
+ *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
+ }
+ }
+ }
+}
+
+
+/*
+ * Map some rows of pixels to the output colormapped representation.
+ */
+
+METHODDEF(void)
+pass2_no_dither (j_decompress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
+/* This version performs no dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ register JSAMPROW inptr, outptr;
+ register histptr cachep;
+ register int c0, c1, c2;
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+
+ for (row = 0; row < num_rows; row++) {
+ inptr = input_buf[row];
+ outptr = output_buf[row];
+ for (col = width; col > 0; col--) {
+ /* get pixel value and index into the cache */
+ c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
+ c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
+ c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
+ cachep = & histogram[c0][c1][c2];
+ /* If we have not seen this color before, find nearest colormap entry */
+ /* and update the cache */
+ if (*cachep == 0)
+ fill_inverse_cmap(cinfo, c0,c1,c2);
+ /* Now emit the colormap index for this cell */
+ *outptr++ = (JSAMPLE) (*cachep - 1);
+ }
+ }
+}
+
+
+METHODDEF(void)
+pass2_fs_dither (j_decompress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
+/* This version performs Floyd-Steinberg dithering */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */
+ LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
+ LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
+ register FSERRPTR errorptr; /* => fserrors[] at column before current */
+ JSAMPROW inptr; /* => current input pixel */
+ JSAMPROW outptr; /* => current output pixel */
+ histptr cachep;
+ int dir; /* +1 or -1 depending on direction */
+ int dir3; /* 3*dir, for advancing inptr & errorptr */
+ int row;
+ JDIMENSION col;
+ JDIMENSION width = cinfo->output_width;
+ JSAMPLE *range_limit = cinfo->sample_range_limit;
+ int *error_limit = cquantize->error_limiter;
+ JSAMPROW colormap0 = cinfo->colormap[0];
+ JSAMPROW colormap1 = cinfo->colormap[1];
+ JSAMPROW colormap2 = cinfo->colormap[2];
+ SHIFT_TEMPS
+
+ for (row = 0; row < num_rows; row++) {
+ inptr = input_buf[row];
+ outptr = output_buf[row];
+ if (cquantize->on_odd_row) {
+ /* work right to left in this row */
+ inptr += (width-1) * 3; /* so point to rightmost pixel */
+ outptr += width-1;
+ dir = -1;
+ dir3 = -3;
+ errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
+ cquantize->on_odd_row = FALSE; /* flip for next time */
+ } else {
+ /* work left to right in this row */
+ dir = 1;
+ dir3 = 3;
+ errorptr = cquantize->fserrors; /* => entry before first real column */
+ cquantize->on_odd_row = TRUE; /* flip for next time */
+ }
+ /* Preset error values: no error propagated to first pixel from left */
+ cur0 = cur1 = cur2 = 0;
+ /* and no error propagated to row below yet */
+ belowerr0 = belowerr1 = belowerr2 = 0;
+ bpreverr0 = bpreverr1 = bpreverr2 = 0;
+
+ for (col = width; col > 0; col--) {
+ /* curN holds the error propagated from the previous pixel on the
+ * current line. Add the error propagated from the previous line
+ * to form the complete error correction term for this pixel, and
+ * round the error term (which is expressed * 16) to an integer.
+ * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
+ * for either sign of the error value.
+ * Note: errorptr points to *previous* column's array entry.
+ */
+ cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
+ cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
+ cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
+ /* Limit the error using transfer function set by init_error_limit.
+ * See comments with init_error_limit for rationale.
+ */
+ cur0 = error_limit[cur0];
+ cur1 = error_limit[cur1];
+ cur2 = error_limit[cur2];
+ /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
+ * The maximum error is +- MAXJSAMPLE (or less with error limiting);
+ * this sets the required size of the range_limit array.
+ */
+ cur0 += GETJSAMPLE(inptr[0]);
+ cur1 += GETJSAMPLE(inptr[1]);
+ cur2 += GETJSAMPLE(inptr[2]);
+ cur0 = GETJSAMPLE(range_limit[cur0]);
+ cur1 = GETJSAMPLE(range_limit[cur1]);
+ cur2 = GETJSAMPLE(range_limit[cur2]);
+ /* Index into the cache with adjusted pixel value */
+ cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
+ /* If we have not seen this color before, find nearest colormap */
+ /* entry and update the cache */
+ if (*cachep == 0)
+ fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
+ /* Now emit the colormap index for this cell */
+ { register int pixcode = *cachep - 1;
+ *outptr = (JSAMPLE) pixcode;
+ /* Compute representation error for this pixel */
+ cur0 -= GETJSAMPLE(colormap0[pixcode]);
+ cur1 -= GETJSAMPLE(colormap1[pixcode]);
+ cur2 -= GETJSAMPLE(colormap2[pixcode]);
+ }
+ /* Compute error fractions to be propagated to adjacent pixels.
+ * Add these into the running sums, and simultaneously shift the
+ * next-line error sums left by 1 column.
+ */
+ { register LOCFSERROR bnexterr, delta;
+
+ bnexterr = cur0; /* Process component 0 */
+ delta = cur0 * 2;
+ cur0 += delta; /* form error * 3 */
+ errorptr[0] = (FSERROR) (bpreverr0 + cur0);
+ cur0 += delta; /* form error * 5 */
+ bpreverr0 = belowerr0 + cur0;
+ belowerr0 = bnexterr;
+ cur0 += delta; /* form error * 7 */
+ bnexterr = cur1; /* Process component 1 */
+ delta = cur1 * 2;
+ cur1 += delta; /* form error * 3 */
+ errorptr[1] = (FSERROR) (bpreverr1 + cur1);
+ cur1 += delta; /* form error * 5 */
+ bpreverr1 = belowerr1 + cur1;
+ belowerr1 = bnexterr;
+ cur1 += delta; /* form error * 7 */
+ bnexterr = cur2; /* Process component 2 */
+ delta = cur2 * 2;
+ cur2 += delta; /* form error * 3 */
+ errorptr[2] = (FSERROR) (bpreverr2 + cur2);
+ cur2 += delta; /* form error * 5 */
+ bpreverr2 = belowerr2 + cur2;
+ belowerr2 = bnexterr;
+ cur2 += delta; /* form error * 7 */
+ }
+ /* At this point curN contains the 7/16 error value to be propagated
+ * to the next pixel on the current line, and all the errors for the
+ * next line have been shifted over. We are therefore ready to move on.
+ */
+ inptr += dir3; /* Advance pixel pointers to next column */
+ outptr += dir;
+ errorptr += dir3; /* advance errorptr to current column */
+ }
+ /* Post-loop cleanup: we must unload the final error values into the
+ * final fserrors[] entry. Note we need not unload belowerrN because
+ * it is for the dummy column before or after the actual array.
+ */
+ errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
+ errorptr[1] = (FSERROR) bpreverr1;
+ errorptr[2] = (FSERROR) bpreverr2;
+ }
+}
+
+
+/*
+ * Initialize the error-limiting transfer function (lookup table).
+ * The raw F-S error computation can potentially compute error values of up to
+ * +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be
+ * much less, otherwise obviously wrong pixels will be created. (Typical
+ * effects include weird fringes at color-area boundaries, isolated bright
+ * pixels in a dark area, etc.) The standard advice for avoiding this problem
+ * is to ensure that the "corners" of the color cube are allocated as output
+ * colors; then repeated errors in the same direction cannot cause cascading
+ * error buildup. However, that only prevents the error from getting
+ * completely out of hand; Aaron Giles reports that error limiting improves
+ * the results even with corner colors allocated.
+ * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
+ * well, but the smoother transfer function used below is even better. Thanks
+ * to Aaron Giles for this idea.
+ */
+
+LOCAL(void)
+init_error_limit (j_decompress_ptr cinfo)
+/* Allocate and fill in the error_limiter table */
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ int * table;
+ int in, out;
+
+ table = (int *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
+ table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
+ cquantize->error_limiter = table;
+
+#define STEPSIZE ((MAXJSAMPLE+1)/16)
+ /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
+ out = 0;
+ for (in = 0; in < STEPSIZE; in++, out++) {
+ table[in] = out; table[-in] = -out;
+ }
+ /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
+ for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
+ table[in] = out; table[-in] = -out;
+ }
+ /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
+ for (; in <= MAXJSAMPLE; in++) {
+ table[in] = out; table[-in] = -out;
+ }
+#undef STEPSIZE
+}
+
+
+/*
+ * Finish up at the end of each pass.
+ */
+
+METHODDEF(void)
+finish_pass1 (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+
+ /* Select the representative colors and fill in cinfo->colormap */
+ cinfo->colormap = cquantize->sv_colormap;
+ select_colors(cinfo, cquantize->desired);
+ /* Force next pass to zero the color index table */
+ cquantize->needs_zeroed = TRUE;
+}
+
+
+METHODDEF(void)
+finish_pass2 (j_decompress_ptr cinfo)
+{
+ /* no work */
+}
+
+
+/*
+ * Initialize for each processing pass.
+ */
+
+METHODDEF(void)
+start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+ hist3d histogram = cquantize->histogram;
+ int i;
+
+ /* Only F-S dithering or no dithering is supported. */
+ /* If user asks for ordered dither, give him F-S. */
+ if (cinfo->dither_mode != JDITHER_NONE)
+ cinfo->dither_mode = JDITHER_FS;
+
+ if (is_pre_scan) {
+ /* Set up method pointers */
+ cquantize->pub.color_quantize = prescan_quantize;
+ cquantize->pub.finish_pass = finish_pass1;
+ cquantize->needs_zeroed = TRUE; /* Always zero histogram */
+ } else {
+ /* Set up method pointers */
+ if (cinfo->dither_mode == JDITHER_FS)
+ cquantize->pub.color_quantize = pass2_fs_dither;
+ else
+ cquantize->pub.color_quantize = pass2_no_dither;
+ cquantize->pub.finish_pass = finish_pass2;
+
+ /* Make sure color count is acceptable */
+ i = cinfo->actual_number_of_colors;
+ if (i < 1)
+ ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
+ if (i > MAXNUMCOLORS)
+ ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
+
+ if (cinfo->dither_mode == JDITHER_FS) {
+ size_t arraysize = (size_t) ((cinfo->output_width + 2) *
+ (3 * SIZEOF(FSERROR)));
+ /* Allocate Floyd-Steinberg workspace if we didn't already. */
+ if (cquantize->fserrors == NULL)
+ cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
+ /* Initialize the propagated errors to zero. */
+ jzero_far((void FAR *) cquantize->fserrors, arraysize);
+ /* Make the error-limit table if we didn't already. */
+ if (cquantize->error_limiter == NULL)
+ init_error_limit(cinfo);
+ cquantize->on_odd_row = FALSE;
+ }
+
+ }
+ /* Zero the histogram or inverse color map, if necessary */
+ if (cquantize->needs_zeroed) {
+ for (i = 0; i < HIST_C0_ELEMS; i++) {
+ jzero_far((void FAR *) histogram[i],
+ HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
+ }
+ cquantize->needs_zeroed = FALSE;
+ }
+}
+
+
+/*
+ * Switch to a new external colormap between output passes.
+ */
+
+METHODDEF(void)
+new_color_map_2_quant (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+
+ /* Reset the inverse color map */
+ cquantize->needs_zeroed = TRUE;
+}
+
+
+/*
+ * Module initialization routine for 2-pass color quantization.
+ */
+
+GLOBAL(void)
+jinit_2pass_quantizer (j_decompress_ptr cinfo)
+{
+ my_cquantize_ptr cquantize;
+ int i;
+
+ cquantize = (my_cquantize_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_cquantizer));
+ cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
+ cquantize->pub.start_pass = start_pass_2_quant;
+ cquantize->pub.new_color_map = new_color_map_2_quant;
+ cquantize->fserrors = NULL; /* flag optional arrays not allocated */
+ cquantize->error_limiter = NULL;
+
+ /* Make sure jdmaster didn't give me a case I can't handle */
+ if (cinfo->out_color_components != 3)
+ ERREXIT(cinfo, JERR_NOTIMPL);
+
+ /* Allocate the histogram/inverse colormap storage */
+ cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
+ for (i = 0; i < HIST_C0_ELEMS; i++) {
+ cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
+ }
+ cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
+
+ /* Allocate storage for the completed colormap, if required.
+ * We do this now since it is FAR storage and may affect
+ * the memory manager's space calculations.
+ */
+ if (cinfo->enable_2pass_quant) {
+ /* Make sure color count is acceptable */
+ int desired = cinfo->desired_number_of_colors;
+ /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
+ if (desired < 8)
+ ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
+ /* Make sure colormap indexes can be represented by JSAMPLEs */
+ if (desired > MAXNUMCOLORS)
+ ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
+ cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
+ cquantize->desired = desired;
+ } else
+ cquantize->sv_colormap = NULL;
+
+ /* Only F-S dithering or no dithering is supported. */
+ /* If user asks for ordered dither, give him F-S. */
+ if (cinfo->dither_mode != JDITHER_NONE)
+ cinfo->dither_mode = JDITHER_FS;
+
+ /* Allocate Floyd-Steinberg workspace if necessary.
+ * This isn't really needed until pass 2, but again it is FAR storage.
+ * Although we will cope with a later change in dither_mode,
+ * we do not promise to honor max_memory_to_use if dither_mode changes.
+ */
+ if (cinfo->dither_mode == JDITHER_FS) {
+ cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
+ /* Might as well create the error-limiting table too. */
+ init_error_limit(cinfo);
+ }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/jutils.c b/test/monniaux/jpeg-6b/jutils.c
new file mode 100644
index 00000000..d18a9555
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jutils.c
@@ -0,0 +1,179 @@
+/*
+ * jutils.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains tables and miscellaneous utility routines needed
+ * for both compression and decompression.
+ * Note we prefix all global names with "j" to minimize conflicts with
+ * a surrounding application.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
+ * of a DCT block read in natural order (left to right, top to bottom).
+ */
+
+#if 0 /* This table is not actually needed in v6a */
+
+const int jpeg_zigzag_order[DCTSIZE2] = {
+ 0, 1, 5, 6, 14, 15, 27, 28,
+ 2, 4, 7, 13, 16, 26, 29, 42,
+ 3, 8, 12, 17, 25, 30, 41, 43,
+ 9, 11, 18, 24, 31, 40, 44, 53,
+ 10, 19, 23, 32, 39, 45, 52, 54,
+ 20, 22, 33, 38, 46, 51, 55, 60,
+ 21, 34, 37, 47, 50, 56, 59, 61,
+ 35, 36, 48, 49, 57, 58, 62, 63
+};
+
+#endif
+
+/*
+ * jpeg_natural_order[i] is the natural-order position of the i'th element
+ * of zigzag order.
+ *
+ * When reading corrupted data, the Huffman decoders could attempt
+ * to reference an entry beyond the end of this array (if the decoded
+ * zero run length reaches past the end of the block). To prevent
+ * wild stores without adding an inner-loop test, we put some extra
+ * "63"s after the real entries. This will cause the extra coefficient
+ * to be stored in location 63 of the block, not somewhere random.
+ * The worst case would be a run-length of 15, which means we need 16
+ * fake entries.
+ */
+
+const int jpeg_natural_order[DCTSIZE2+16] = {
+ 0, 1, 8, 16, 9, 2, 3, 10,
+ 17, 24, 32, 25, 18, 11, 4, 5,
+ 12, 19, 26, 33, 40, 48, 41, 34,
+ 27, 20, 13, 6, 7, 14, 21, 28,
+ 35, 42, 49, 56, 57, 50, 43, 36,
+ 29, 22, 15, 23, 30, 37, 44, 51,
+ 58, 59, 52, 45, 38, 31, 39, 46,
+ 53, 60, 61, 54, 47, 55, 62, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
+ 63, 63, 63, 63, 63, 63, 63, 63
+};
+
+
+/*
+ * Arithmetic utilities
+ */
+
+GLOBAL(long)
+jdiv_round_up (long a, long b)
+/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
+/* Assumes a >= 0, b > 0 */
+{
+ return (a + b - 1L) / b;
+}
+
+
+GLOBAL(long)
+jround_up (long a, long b)
+/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
+/* Assumes a >= 0, b > 0 */
+{
+ a += b - 1L;
+ return a - (a % b);
+}
+
+
+/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
+ * and coefficient-block arrays. This won't work on 80x86 because the arrays
+ * are FAR and we're assuming a small-pointer memory model. However, some
+ * DOS compilers provide far-pointer versions of memcpy() and memset() even
+ * in the small-model libraries. These will be used if USE_FMEM is defined.
+ * Otherwise, the routines below do it the hard way. (The performance cost
+ * is not all that great, because these routines aren't very heavily used.)
+ */
+
+#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
+#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
+#define FMEMZERO(target,size) MEMZERO(target,size)
+#else /* 80x86 case, define if we can */
+#ifdef USE_FMEM
+#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
+#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
+#endif
+#endif
+
+
+GLOBAL(void)
+jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
+ JSAMPARRAY output_array, int dest_row,
+ int num_rows, JDIMENSION num_cols)
+/* Copy some rows of samples from one place to another.
+ * num_rows rows are copied from input_array[source_row++]
+ * to output_array[dest_row++]; these areas may overlap for duplication.
+ * The source and destination arrays must be at least as wide as num_cols.
+ */
+{
+ register JSAMPROW inptr, outptr;
+#ifdef FMEMCOPY
+ register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
+#else
+ register JDIMENSION count;
+#endif
+ register int row;
+
+ input_array += source_row;
+ output_array += dest_row;
+
+ for (row = num_rows; row > 0; row--) {
+ inptr = *input_array++;
+ outptr = *output_array++;
+#ifdef FMEMCOPY
+ FMEMCOPY(outptr, inptr, count);
+#else
+ for (count = num_cols; count > 0; count--)
+ *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
+#endif
+ }
+}
+
+
+GLOBAL(void)
+jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
+ JDIMENSION num_blocks)
+/* Copy a row of coefficient blocks from one place to another. */
+{
+#ifdef FMEMCOPY
+ FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
+#else
+ register JCOEFPTR inptr, outptr;
+ register long count;
+
+ inptr = (JCOEFPTR) input_row;
+ outptr = (JCOEFPTR) output_row;
+ for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
+ *outptr++ = *inptr++;
+ }
+#endif
+}
+
+
+GLOBAL(void)
+jzero_far (void FAR * target, size_t bytestozero)
+/* Zero out a chunk of FAR memory. */
+/* This might be sample-array data, block-array data, or alloc_large data. */
+{
+#ifdef FMEMZERO
+ FMEMZERO(target, bytestozero);
+#else
+ register char FAR * ptr = (char FAR *) target;
+ register size_t count;
+
+ for (count = bytestozero; count > 0; count--) {
+ *ptr++ = 0;
+ }
+#endif
+}
diff --git a/test/monniaux/jpeg-6b/jversion.h b/test/monniaux/jpeg-6b/jversion.h
new file mode 100644
index 00000000..6472c58d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/jversion.h
@@ -0,0 +1,14 @@
+/*
+ * jversion.h
+ *
+ * Copyright (C) 1991-1998, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains software version identification.
+ */
+
+
+#define JVERSION "6b 27-Mar-1998"
+
+#define JCOPYRIGHT "Copyright (C) 1998, Thomas G. Lane"
diff --git a/test/monniaux/jpeg-6b/libjpeg.doc b/test/monniaux/jpeg-6b/libjpeg.doc
new file mode 100644
index 00000000..689b206c
--- /dev/null
+++ b/test/monniaux/jpeg-6b/libjpeg.doc
@@ -0,0 +1,3006 @@
+USING THE IJG JPEG LIBRARY
+
+Copyright (C) 1994-1998, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file describes how to use the IJG JPEG library within an application
+program. Read it if you want to write a program that uses the library.
+
+The file example.c provides heavily commented skeleton code for calling the
+JPEG library. Also see jpeglib.h (the include file to be used by application
+programs) for full details about data structures and function parameter lists.
+The library source code, of course, is the ultimate reference.
+
+Note that there have been *major* changes from the application interface
+presented by IJG version 4 and earlier versions. The old design had several
+inherent limitations, and it had accumulated a lot of cruft as we added
+features while trying to minimize application-interface changes. We have
+sacrificed backward compatibility in the version 5 rewrite, but we think the
+improvements justify this.
+
+
+TABLE OF CONTENTS
+-----------------
+
+Overview:
+ Functions provided by the library
+ Outline of typical usage
+Basic library usage:
+ Data formats
+ Compression details
+ Decompression details
+ Mechanics of usage: include files, linking, etc
+Advanced features:
+ Compression parameter selection
+ Decompression parameter selection
+ Special color spaces
+ Error handling
+ Compressed data handling (source and destination managers)
+ I/O suspension
+ Progressive JPEG support
+ Buffered-image mode
+ Abbreviated datastreams and multiple images
+ Special markers
+ Raw (downsampled) image data
+ Really raw data: DCT coefficients
+ Progress monitoring
+ Memory management
+ Memory usage
+ Library compile-time options
+ Portability considerations
+ Notes for MS-DOS implementors
+
+You should read at least the overview and basic usage sections before trying
+to program with the library. The sections on advanced features can be read
+if and when you need them.
+
+
+OVERVIEW
+========
+
+Functions provided by the library
+---------------------------------
+
+The IJG JPEG library provides C code to read and write JPEG-compressed image
+files. The surrounding application program receives or supplies image data a
+scanline at a time, using a straightforward uncompressed image format. All
+details of color conversion and other preprocessing/postprocessing can be
+handled by the library.
+
+The library includes a substantial amount of code that is not covered by the
+JPEG standard but is necessary for typical applications of JPEG. These
+functions preprocess the image before JPEG compression or postprocess it after
+decompression. They include colorspace conversion, downsampling/upsampling,
+and color quantization. The application indirectly selects use of this code
+by specifying the format in which it wishes to supply or receive image data.
+For example, if colormapped output is requested, then the decompression
+library automatically invokes color quantization.
+
+A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
+and even more so in decompression postprocessing. The decompression library
+provides multiple implementations that cover most of the useful tradeoffs,
+ranging from very-high-quality down to fast-preview operation. On the
+compression side we have generally not provided low-quality choices, since
+compression is normally less time-critical. It should be understood that the
+low-quality modes may not meet the JPEG standard's accuracy requirements;
+nonetheless, they are useful for viewers.
+
+A word about functions *not* provided by the library. We handle a subset of
+the ISO JPEG standard; most baseline, extended-sequential, and progressive
+JPEG processes are supported. (Our subset includes all features now in common
+use.) Unsupported ISO options include:
+ * Hierarchical storage
+ * Lossless JPEG
+ * Arithmetic entropy coding (unsupported for legal reasons)
+ * DNL marker
+ * Nonintegral subsampling ratios
+We support both 8- and 12-bit data precision, but this is a compile-time
+choice rather than a run-time choice; hence it is difficult to use both
+precisions in a single application.
+
+By itself, the library handles only interchange JPEG datastreams --- in
+particular the widely used JFIF file format. The library can be used by
+surrounding code to process interchange or abbreviated JPEG datastreams that
+are embedded in more complex file formats. (For example, this library is
+used by the free LIBTIFF library to support JPEG compression in TIFF.)
+
+
+Outline of typical usage
+------------------------
+
+The rough outline of a JPEG compression operation is:
+
+ Allocate and initialize a JPEG compression object
+ Specify the destination for the compressed data (eg, a file)
+ Set parameters for compression, including image size & colorspace
+ jpeg_start_compress(...);
+ while (scan lines remain to be written)
+ jpeg_write_scanlines(...);
+ jpeg_finish_compress(...);
+ Release the JPEG compression object
+
+A JPEG compression object holds parameters and working state for the JPEG
+library. We make creation/destruction of the object separate from starting
+or finishing compression of an image; the same object can be re-used for a
+series of image compression operations. This makes it easy to re-use the
+same parameter settings for a sequence of images. Re-use of a JPEG object
+also has important implications for processing abbreviated JPEG datastreams,
+as discussed later.
+
+The image data to be compressed is supplied to jpeg_write_scanlines() from
+in-memory buffers. If the application is doing file-to-file compression,
+reading image data from the source file is the application's responsibility.
+The library emits compressed data by calling a "data destination manager",
+which typically will write the data into a file; but the application can
+provide its own destination manager to do something else.
+
+Similarly, the rough outline of a JPEG decompression operation is:
+
+ Allocate and initialize a JPEG decompression object
+ Specify the source of the compressed data (eg, a file)
+ Call jpeg_read_header() to obtain image info
+ Set parameters for decompression
+ jpeg_start_decompress(...);
+ while (scan lines remain to be read)
+ jpeg_read_scanlines(...);
+ jpeg_finish_decompress(...);
+ Release the JPEG decompression object
+
+This is comparable to the compression outline except that reading the
+datastream header is a separate step. This is helpful because information
+about the image's size, colorspace, etc is available when the application
+selects decompression parameters. For example, the application can choose an
+output scaling ratio that will fit the image into the available screen size.
+
+The decompression library obtains compressed data by calling a data source
+manager, which typically will read the data from a file; but other behaviors
+can be obtained with a custom source manager. Decompressed data is delivered
+into in-memory buffers passed to jpeg_read_scanlines().
+
+It is possible to abort an incomplete compression or decompression operation
+by calling jpeg_abort(); or, if you do not need to retain the JPEG object,
+simply release it by calling jpeg_destroy().
+
+JPEG compression and decompression objects are two separate struct types.
+However, they share some common fields, and certain routines such as
+jpeg_destroy() can work on either type of object.
+
+The JPEG library has no static variables: all state is in the compression
+or decompression object. Therefore it is possible to process multiple
+compression and decompression operations concurrently, using multiple JPEG
+objects.
+
+Both compression and decompression can be done in an incremental memory-to-
+memory fashion, if suitable source/destination managers are used. See the
+section on "I/O suspension" for more details.
+
+
+BASIC LIBRARY USAGE
+===================
+
+Data formats
+------------
+
+Before diving into procedural details, it is helpful to understand the
+image data format that the JPEG library expects or returns.
+
+The standard input image format is a rectangular array of pixels, with each
+pixel having the same number of "component" or "sample" values (color
+channels). You must specify how many components there are and the colorspace
+interpretation of the components. Most applications will use RGB data
+(three components per pixel) or grayscale data (one component per pixel).
+PLEASE NOTE THAT RGB DATA IS THREE SAMPLES PER PIXEL, GRAYSCALE ONLY ONE.
+A remarkable number of people manage to miss this, only to find that their
+programs don't work with grayscale JPEG files.
+
+There is no provision for colormapped input. JPEG files are always full-color
+or full grayscale (or sometimes another colorspace such as CMYK). You can
+feed in a colormapped image by expanding it to full-color format. However
+JPEG often doesn't work very well with source data that has been colormapped,
+because of dithering noise. This is discussed in more detail in the JPEG FAQ
+and the other references mentioned in the README file.
+
+Pixels are stored by scanlines, with each scanline running from left to
+right. The component values for each pixel are adjacent in the row; for
+example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color. Each scanline is an
+array of data type JSAMPLE --- which is typically "unsigned char", unless
+you've changed jmorecfg.h. (You can also change the RGB pixel layout, say
+to B,G,R order, by modifying jmorecfg.h. But see the restrictions listed in
+that file before doing so.)
+
+A 2-D array of pixels is formed by making a list of pointers to the starts of
+scanlines; so the scanlines need not be physically adjacent in memory. Even
+if you process just one scanline at a time, you must make a one-element
+pointer array to conform to this structure. Pointers to JSAMPLE rows are of
+type JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.
+
+The library accepts or supplies one or more complete scanlines per call.
+It is not possible to process part of a row at a time. Scanlines are always
+processed top-to-bottom. You can process an entire image in one call if you
+have it all in memory, but usually it's simplest to process one scanline at
+a time.
+
+For best results, source data values should have the precision specified by
+BITS_IN_JSAMPLE (normally 8 bits). For instance, if you choose to compress
+data that's only 6 bits/channel, you should left-justify each value in a
+byte before passing it to the compressor. If you need to compress data
+that has more than 8 bits/channel, compile with BITS_IN_JSAMPLE = 12.
+(See "Library compile-time options", later.)
+
+
+The data format returned by the decompressor is the same in all details,
+except that colormapped output is supported. (Again, a JPEG file is never
+colormapped. But you can ask the decompressor to perform on-the-fly color
+quantization to deliver colormapped output.) If you request colormapped
+output then the returned data array contains a single JSAMPLE per pixel;
+its value is an index into a color map. The color map is represented as
+a 2-D JSAMPARRAY in which each row holds the values of one color component,
+that is, colormap[i][j] is the value of the i'th color component for pixel
+value (map index) j. Note that since the colormap indexes are stored in
+JSAMPLEs, the maximum number of colors is limited by the size of JSAMPLE
+(ie, at most 256 colors for an 8-bit JPEG library).
+
+
+Compression details
+-------------------
+
+Here we revisit the JPEG compression outline given in the overview.
+
+1. Allocate and initialize a JPEG compression object.
+
+A JPEG compression object is a "struct jpeg_compress_struct". (It also has
+a bunch of subsidiary structures which are allocated via malloc(), but the
+application doesn't control those directly.) This struct can be just a local
+variable in the calling routine, if a single routine is going to execute the
+whole JPEG compression sequence. Otherwise it can be static or allocated
+from malloc().
+
+You will also need a structure representing a JPEG error handler. The part
+of this that the library cares about is a "struct jpeg_error_mgr". If you
+are providing your own error handler, you'll typically want to embed the
+jpeg_error_mgr struct in a larger structure; this is discussed later under
+"Error handling". For now we'll assume you are just using the default error
+handler. The default error handler will print JPEG error/warning messages
+on stderr, and it will call exit() if a fatal error occurs.
+
+You must initialize the error handler structure, store a pointer to it into
+the JPEG object's "err" field, and then call jpeg_create_compress() to
+initialize the rest of the JPEG object.
+
+Typical code for this step, if you are using the default error handler, is
+
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ ...
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_compress(&cinfo);
+
+jpeg_create_compress allocates a small amount of memory, so it could fail
+if you are out of memory. In that case it will exit via the error handler;
+that's why the error handler must be initialized first.
+
+
+2. Specify the destination for the compressed data (eg, a file).
+
+As previously mentioned, the JPEG library delivers compressed data to a
+"data destination" module. The library includes one data destination
+module which knows how to write to a stdio stream. You can use your own
+destination module if you want to do something else, as discussed later.
+
+If you use the standard destination module, you must open the target stdio
+stream beforehand. Typical code for this step looks like:
+
+ FILE * outfile;
+ ...
+ if ((outfile = fopen(filename, "wb")) == NULL) {
+ fprintf(stderr, "can't open %s\n", filename);
+ exit(1);
+ }
+ jpeg_stdio_dest(&cinfo, outfile);
+
+where the last line invokes the standard destination module.
+
+WARNING: it is critical that the binary compressed data be delivered to the
+output file unchanged. On non-Unix systems the stdio library may perform
+newline translation or otherwise corrupt binary data. To suppress this
+behavior, you may need to use a "b" option to fopen (as shown above), or use
+setmode() or another routine to put the stdio stream in binary mode. See
+cjpeg.c and djpeg.c for code that has been found to work on many systems.
+
+You can select the data destination after setting other parameters (step 3),
+if that's more convenient. You may not change the destination between
+calling jpeg_start_compress() and jpeg_finish_compress().
+
+
+3. Set parameters for compression, including image size & colorspace.
+
+You must supply information about the source image by setting the following
+fields in the JPEG object (cinfo structure):
+
+ image_width Width of image, in pixels
+ image_height Height of image, in pixels
+ input_components Number of color channels (samples per pixel)
+ in_color_space Color space of source image
+
+The image dimensions are, hopefully, obvious. JPEG supports image dimensions
+of 1 to 64K pixels in either direction. The input color space is typically
+RGB or grayscale, and input_components is 3 or 1 accordingly. (See "Special
+color spaces", later, for more info.) The in_color_space field must be
+assigned one of the J_COLOR_SPACE enum constants, typically JCS_RGB or
+JCS_GRAYSCALE.
+
+JPEG has a large number of compression parameters that determine how the
+image is encoded. Most applications don't need or want to know about all
+these parameters. You can set all the parameters to reasonable defaults by
+calling jpeg_set_defaults(); then, if there are particular values you want
+to change, you can do so after that. The "Compression parameter selection"
+section tells about all the parameters.
+
+You must set in_color_space correctly before calling jpeg_set_defaults(),
+because the defaults depend on the source image colorspace. However the
+other three source image parameters need not be valid until you call
+jpeg_start_compress(). There's no harm in calling jpeg_set_defaults() more
+than once, if that happens to be convenient.
+
+Typical code for a 24-bit RGB source image is
+
+ cinfo.image_width = Width; /* image width and height, in pixels */
+ cinfo.image_height = Height;
+ cinfo.input_components = 3; /* # of color components per pixel */
+ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
+
+ jpeg_set_defaults(&cinfo);
+ /* Make optional parameter settings here */
+
+
+4. jpeg_start_compress(...);
+
+After you have established the data destination and set all the necessary
+source image info and other parameters, call jpeg_start_compress() to begin
+a compression cycle. This will initialize internal state, allocate working
+storage, and emit the first few bytes of the JPEG datastream header.
+
+Typical code:
+
+ jpeg_start_compress(&cinfo, TRUE);
+
+The "TRUE" parameter ensures that a complete JPEG interchange datastream
+will be written. This is appropriate in most cases. If you think you might
+want to use an abbreviated datastream, read the section on abbreviated
+datastreams, below.
+
+Once you have called jpeg_start_compress(), you may not alter any JPEG
+parameters or other fields of the JPEG object until you have completed
+the compression cycle.
+
+
+5. while (scan lines remain to be written)
+ jpeg_write_scanlines(...);
+
+Now write all the required image data by calling jpeg_write_scanlines()
+one or more times. You can pass one or more scanlines in each call, up
+to the total image height. In most applications it is convenient to pass
+just one or a few scanlines at a time. The expected format for the passed
+data is discussed under "Data formats", above.
+
+Image data should be written in top-to-bottom scanline order. The JPEG spec
+contains some weasel wording about how top and bottom are application-defined
+terms (a curious interpretation of the English language...) but if you want
+your files to be compatible with everyone else's, you WILL use top-to-bottom
+order. If the source data must be read in bottom-to-top order, you can use
+the JPEG library's virtual array mechanism to invert the data efficiently.
+Examples of this can be found in the sample application cjpeg.
+
+The library maintains a count of the number of scanlines written so far
+in the next_scanline field of the JPEG object. Usually you can just use
+this variable as the loop counter, so that the loop test looks like
+"while (cinfo.next_scanline < cinfo.image_height)".
+
+Code for this step depends heavily on the way that you store the source data.
+example.c shows the following code for the case of a full-size 2-D source
+array containing 3-byte RGB pixels:
+
+ JSAMPROW row_pointer[1]; /* pointer to a single row */
+ int row_stride; /* physical row width in buffer */
+
+ row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
+
+ while (cinfo.next_scanline < cinfo.image_height) {
+ row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
+ jpeg_write_scanlines(&cinfo, row_pointer, 1);
+ }
+
+jpeg_write_scanlines() returns the number of scanlines actually written.
+This will normally be equal to the number passed in, so you can usually
+ignore the return value. It is different in just two cases:
+ * If you try to write more scanlines than the declared image height,
+ the additional scanlines are ignored.
+ * If you use a suspending data destination manager, output buffer overrun
+ will cause the compressor to return before accepting all the passed lines.
+ This feature is discussed under "I/O suspension", below. The normal
+ stdio destination manager will NOT cause this to happen.
+In any case, the return value is the same as the change in the value of
+next_scanline.
+
+
+6. jpeg_finish_compress(...);
+
+After all the image data has been written, call jpeg_finish_compress() to
+complete the compression cycle. This step is ESSENTIAL to ensure that the
+last bufferload of data is written to the data destination.
+jpeg_finish_compress() also releases working memory associated with the JPEG
+object.
+
+Typical code:
+
+ jpeg_finish_compress(&cinfo);
+
+If using the stdio destination manager, don't forget to close the output
+stdio stream (if necessary) afterwards.
+
+If you have requested a multi-pass operating mode, such as Huffman code
+optimization, jpeg_finish_compress() will perform the additional passes using
+data buffered by the first pass. In this case jpeg_finish_compress() may take
+quite a while to complete. With the default compression parameters, this will
+not happen.
+
+It is an error to call jpeg_finish_compress() before writing the necessary
+total number of scanlines. If you wish to abort compression, call
+jpeg_abort() as discussed below.
+
+After completing a compression cycle, you may dispose of the JPEG object
+as discussed next, or you may use it to compress another image. In that case
+return to step 2, 3, or 4 as appropriate. If you do not change the
+destination manager, the new datastream will be written to the same target.
+If you do not change any JPEG parameters, the new datastream will be written
+with the same parameters as before. Note that you can change the input image
+dimensions freely between cycles, but if you change the input colorspace, you
+should call jpeg_set_defaults() to adjust for the new colorspace; and then
+you'll need to repeat all of step 3.
+
+
+7. Release the JPEG compression object.
+
+When you are done with a JPEG compression object, destroy it by calling
+jpeg_destroy_compress(). This will free all subsidiary memory (regardless of
+the previous state of the object). Or you can call jpeg_destroy(), which
+works for either compression or decompression objects --- this may be more
+convenient if you are sharing code between compression and decompression
+cases. (Actually, these routines are equivalent except for the declared type
+of the passed pointer. To avoid gripes from ANSI C compilers, jpeg_destroy()
+should be passed a j_common_ptr.)
+
+If you allocated the jpeg_compress_struct structure from malloc(), freeing
+it is your responsibility --- jpeg_destroy() won't. Ditto for the error
+handler structure.
+
+Typical code:
+
+ jpeg_destroy_compress(&cinfo);
+
+
+8. Aborting.
+
+If you decide to abort a compression cycle before finishing, you can clean up
+in either of two ways:
+
+* If you don't need the JPEG object any more, just call
+ jpeg_destroy_compress() or jpeg_destroy() to release memory. This is
+ legitimate at any point after calling jpeg_create_compress() --- in fact,
+ it's safe even if jpeg_create_compress() fails.
+
+* If you want to re-use the JPEG object, call jpeg_abort_compress(), or call
+ jpeg_abort() which works on both compression and decompression objects.
+ This will return the object to an idle state, releasing any working memory.
+ jpeg_abort() is allowed at any time after successful object creation.
+
+Note that cleaning up the data destination, if required, is your
+responsibility; neither of these routines will call term_destination().
+(See "Compressed data handling", below, for more about that.)
+
+jpeg_destroy() and jpeg_abort() are the only safe calls to make on a JPEG
+object that has reported an error by calling error_exit (see "Error handling"
+for more info). The internal state of such an object is likely to be out of
+whack. Either of these two routines will return the object to a known state.
+
+
+Decompression details
+---------------------
+
+Here we revisit the JPEG decompression outline given in the overview.
+
+1. Allocate and initialize a JPEG decompression object.
+
+This is just like initialization for compression, as discussed above,
+except that the object is a "struct jpeg_decompress_struct" and you
+call jpeg_create_decompress(). Error handling is exactly the same.
+
+Typical code:
+
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ ...
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_decompress(&cinfo);
+
+(Both here and in the IJG code, we usually use variable name "cinfo" for
+both compression and decompression objects.)
+
+
+2. Specify the source of the compressed data (eg, a file).
+
+As previously mentioned, the JPEG library reads compressed data from a "data
+source" module. The library includes one data source module which knows how
+to read from a stdio stream. You can use your own source module if you want
+to do something else, as discussed later.
+
+If you use the standard source module, you must open the source stdio stream
+beforehand. Typical code for this step looks like:
+
+ FILE * infile;
+ ...
+ if ((infile = fopen(filename, "rb")) == NULL) {
+ fprintf(stderr, "can't open %s\n", filename);
+ exit(1);
+ }
+ jpeg_stdio_src(&cinfo, infile);
+
+where the last line invokes the standard source module.
+
+WARNING: it is critical that the binary compressed data be read unchanged.
+On non-Unix systems the stdio library may perform newline translation or
+otherwise corrupt binary data. To suppress this behavior, you may need to use
+a "b" option to fopen (as shown above), or use setmode() or another routine to
+put the stdio stream in binary mode. See cjpeg.c and djpeg.c for code that
+has been found to work on many systems.
+
+You may not change the data source between calling jpeg_read_header() and
+jpeg_finish_decompress(). If you wish to read a series of JPEG images from
+a single source file, you should repeat the jpeg_read_header() to
+jpeg_finish_decompress() sequence without reinitializing either the JPEG
+object or the data source module; this prevents buffered input data from
+being discarded.
+
+
+3. Call jpeg_read_header() to obtain image info.
+
+Typical code for this step is just
+
+ jpeg_read_header(&cinfo, TRUE);
+
+This will read the source datastream header markers, up to the beginning
+of the compressed data proper. On return, the image dimensions and other
+info have been stored in the JPEG object. The application may wish to
+consult this information before selecting decompression parameters.
+
+More complex code is necessary if
+ * A suspending data source is used --- in that case jpeg_read_header()
+ may return before it has read all the header data. See "I/O suspension",
+ below. The normal stdio source manager will NOT cause this to happen.
+ * Abbreviated JPEG files are to be processed --- see the section on
+ abbreviated datastreams. Standard applications that deal only in
+ interchange JPEG files need not be concerned with this case either.
+
+It is permissible to stop at this point if you just wanted to find out the
+image dimensions and other header info for a JPEG file. In that case,
+call jpeg_destroy() when you are done with the JPEG object, or call
+jpeg_abort() to return it to an idle state before selecting a new data
+source and reading another header.
+
+
+4. Set parameters for decompression.
+
+jpeg_read_header() sets appropriate default decompression parameters based on
+the properties of the image (in particular, its colorspace). However, you
+may well want to alter these defaults before beginning the decompression.
+For example, the default is to produce full color output from a color file.
+If you want colormapped output you must ask for it. Other options allow the
+returned image to be scaled and allow various speed/quality tradeoffs to be
+selected. "Decompression parameter selection", below, gives details.
+
+If the defaults are appropriate, nothing need be done at this step.
+
+Note that all default values are set by each call to jpeg_read_header().
+If you reuse a decompression object, you cannot expect your parameter
+settings to be preserved across cycles, as you can for compression.
+You must set desired parameter values each time.
+
+
+5. jpeg_start_decompress(...);
+
+Once the parameter values are satisfactory, call jpeg_start_decompress() to
+begin decompression. This will initialize internal state, allocate working
+memory, and prepare for returning data.
+
+Typical code is just
+
+ jpeg_start_decompress(&cinfo);
+
+If you have requested a multi-pass operating mode, such as 2-pass color
+quantization, jpeg_start_decompress() will do everything needed before data
+output can begin. In this case jpeg_start_decompress() may take quite a while
+to complete. With a single-scan (non progressive) JPEG file and default
+decompression parameters, this will not happen; jpeg_start_decompress() will
+return quickly.
+
+After this call, the final output image dimensions, including any requested
+scaling, are available in the JPEG object; so is the selected colormap, if
+colormapped output has been requested. Useful fields include
+
+ output_width image width and height, as scaled
+ output_height
+ out_color_components # of color components in out_color_space
+ output_components # of color components returned per pixel
+ colormap the selected colormap, if any
+ actual_number_of_colors number of entries in colormap
+
+output_components is 1 (a colormap index) when quantizing colors; otherwise it
+equals out_color_components. It is the number of JSAMPLE values that will be
+emitted per pixel in the output arrays.
+
+Typically you will need to allocate data buffers to hold the incoming image.
+You will need output_width * output_components JSAMPLEs per scanline in your
+output buffer, and a total of output_height scanlines will be returned.
+
+Note: if you are using the JPEG library's internal memory manager to allocate
+data buffers (as djpeg does), then the manager's protocol requires that you
+request large buffers *before* calling jpeg_start_decompress(). This is a
+little tricky since the output_XXX fields are not normally valid then. You
+can make them valid by calling jpeg_calc_output_dimensions() after setting the
+relevant parameters (scaling, output color space, and quantization flag).
+
+
+6. while (scan lines remain to be read)
+ jpeg_read_scanlines(...);
+
+Now you can read the decompressed image data by calling jpeg_read_scanlines()
+one or more times. At each call, you pass in the maximum number of scanlines
+to be read (ie, the height of your working buffer); jpeg_read_scanlines()
+will return up to that many lines. The return value is the number of lines
+actually read. The format of the returned data is discussed under "Data
+formats", above. Don't forget that grayscale and color JPEGs will return
+different data formats!
+
+Image data is returned in top-to-bottom scanline order. If you must write
+out the image in bottom-to-top order, you can use the JPEG library's virtual
+array mechanism to invert the data efficiently. Examples of this can be
+found in the sample application djpeg.
+
+The library maintains a count of the number of scanlines returned so far
+in the output_scanline field of the JPEG object. Usually you can just use
+this variable as the loop counter, so that the loop test looks like
+"while (cinfo.output_scanline < cinfo.output_height)". (Note that the test
+should NOT be against image_height, unless you never use scaling. The
+image_height field is the height of the original unscaled image.)
+The return value always equals the change in the value of output_scanline.
+
+If you don't use a suspending data source, it is safe to assume that
+jpeg_read_scanlines() reads at least one scanline per call, until the
+bottom of the image has been reached.
+
+If you use a buffer larger than one scanline, it is NOT safe to assume that
+jpeg_read_scanlines() fills it. (The current implementation returns only a
+few scanlines per call, no matter how large a buffer you pass.) So you must
+always provide a loop that calls jpeg_read_scanlines() repeatedly until the
+whole image has been read.
+
+
+7. jpeg_finish_decompress(...);
+
+After all the image data has been read, call jpeg_finish_decompress() to
+complete the decompression cycle. This causes working memory associated
+with the JPEG object to be released.
+
+Typical code:
+
+ jpeg_finish_decompress(&cinfo);
+
+If using the stdio source manager, don't forget to close the source stdio
+stream if necessary.
+
+It is an error to call jpeg_finish_decompress() before reading the correct
+total number of scanlines. If you wish to abort decompression, call
+jpeg_abort() as discussed below.
+
+After completing a decompression cycle, you may dispose of the JPEG object as
+discussed next, or you may use it to decompress another image. In that case
+return to step 2 or 3 as appropriate. If you do not change the source
+manager, the next image will be read from the same source.
+
+
+8. Release the JPEG decompression object.
+
+When you are done with a JPEG decompression object, destroy it by calling
+jpeg_destroy_decompress() or jpeg_destroy(). The previous discussion of
+destroying compression objects applies here too.
+
+Typical code:
+
+ jpeg_destroy_decompress(&cinfo);
+
+
+9. Aborting.
+
+You can abort a decompression cycle by calling jpeg_destroy_decompress() or
+jpeg_destroy() if you don't need the JPEG object any more, or
+jpeg_abort_decompress() or jpeg_abort() if you want to reuse the object.
+The previous discussion of aborting compression cycles applies here too.
+
+
+Mechanics of usage: include files, linking, etc
+-----------------------------------------------
+
+Applications using the JPEG library should include the header file jpeglib.h
+to obtain declarations of data types and routines. Before including
+jpeglib.h, include system headers that define at least the typedefs FILE and
+size_t. On ANSI-conforming systems, including <stdio.h> is sufficient; on
+older Unix systems, you may need <sys/types.h> to define size_t.
+
+If the application needs to refer to individual JPEG library error codes, also
+include jerror.h to define those symbols.
+
+jpeglib.h indirectly includes the files jconfig.h and jmorecfg.h. If you are
+installing the JPEG header files in a system directory, you will want to
+install all four files: jpeglib.h, jerror.h, jconfig.h, jmorecfg.h.
+
+The most convenient way to include the JPEG code into your executable program
+is to prepare a library file ("libjpeg.a", or a corresponding name on non-Unix
+machines) and reference it at your link step. If you use only half of the
+library (only compression or only decompression), only that much code will be
+included from the library, unless your linker is hopelessly brain-damaged.
+The supplied makefiles build libjpeg.a automatically (see install.doc).
+
+While you can build the JPEG library as a shared library if the whim strikes
+you, we don't really recommend it. The trouble with shared libraries is that
+at some point you'll probably try to substitute a new version of the library
+without recompiling the calling applications. That generally doesn't work
+because the parameter struct declarations usually change with each new
+version. In other words, the library's API is *not* guaranteed binary
+compatible across versions; we only try to ensure source-code compatibility.
+(In hindsight, it might have been smarter to hide the parameter structs from
+applications and introduce a ton of access functions instead. Too late now,
+however.)
+
+On some systems your application may need to set up a signal handler to ensure
+that temporary files are deleted if the program is interrupted. This is most
+critical if you are on MS-DOS and use the jmemdos.c memory manager back end;
+it will try to grab extended memory for temp files, and that space will NOT be
+freed automatically. See cjpeg.c or djpeg.c for an example signal handler.
+
+It may be worth pointing out that the core JPEG library does not actually
+require the stdio library: only the default source/destination managers and
+error handler need it. You can use the library in a stdio-less environment
+if you replace those modules and use jmemnobs.c (or another memory manager of
+your own devising). More info about the minimum system library requirements
+may be found in jinclude.h.
+
+
+ADVANCED FEATURES
+=================
+
+Compression parameter selection
+-------------------------------
+
+This section describes all the optional parameters you can set for JPEG
+compression, as well as the "helper" routines provided to assist in this
+task. Proper setting of some parameters requires detailed understanding
+of the JPEG standard; if you don't know what a parameter is for, it's best
+not to mess with it! See REFERENCES in the README file for pointers to
+more info about JPEG.
+
+It's a good idea to call jpeg_set_defaults() first, even if you plan to set
+all the parameters; that way your code is more likely to work with future JPEG
+libraries that have additional parameters. For the same reason, we recommend
+you use a helper routine where one is provided, in preference to twiddling
+cinfo fields directly.
+
+The helper routines are:
+
+jpeg_set_defaults (j_compress_ptr cinfo)
+ This routine sets all JPEG parameters to reasonable defaults, using
+ only the input image's color space (field in_color_space, which must
+ already be set in cinfo). Many applications will only need to use
+ this routine and perhaps jpeg_set_quality().
+
+jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
+ Sets the JPEG file's colorspace (field jpeg_color_space) as specified,
+ and sets other color-space-dependent parameters appropriately. See
+ "Special color spaces", below, before using this. A large number of
+ parameters, including all per-component parameters, are set by this
+ routine; if you want to twiddle individual parameters you should call
+ jpeg_set_colorspace() before rather than after.
+
+jpeg_default_colorspace (j_compress_ptr cinfo)
+ Selects an appropriate JPEG colorspace based on cinfo->in_color_space,
+ and calls jpeg_set_colorspace(). This is actually a subroutine of
+ jpeg_set_defaults(). It's broken out in case you want to change
+ just the colorspace-dependent JPEG parameters.
+
+jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
+ Constructs JPEG quantization tables appropriate for the indicated
+ quality setting. The quality value is expressed on the 0..100 scale
+ recommended by IJG (cjpeg's "-quality" switch uses this routine).
+ Note that the exact mapping from quality values to tables may change
+ in future IJG releases as more is learned about DCT quantization.
+ If the force_baseline parameter is TRUE, then the quantization table
+ entries are constrained to the range 1..255 for full JPEG baseline
+ compatibility. In the current implementation, this only makes a
+ difference for quality settings below 25, and it effectively prevents
+ very small/low quality files from being generated. The IJG decoder
+ is capable of reading the non-baseline files generated at low quality
+ settings when force_baseline is FALSE, but other decoders may not be.
+
+jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
+ boolean force_baseline)
+ Same as jpeg_set_quality() except that the generated tables are the
+ sample tables given in the JPEC spec section K.1, multiplied by the
+ specified scale factor (which is expressed as a percentage; thus
+ scale_factor = 100 reproduces the spec's tables). Note that larger
+ scale factors give lower quality. This entry point is useful for
+ conforming to the Adobe PostScript DCT conventions, but we do not
+ recommend linear scaling as a user-visible quality scale otherwise.
+ force_baseline again constrains the computed table entries to 1..255.
+
+int jpeg_quality_scaling (int quality)
+ Converts a value on the IJG-recommended quality scale to a linear
+ scaling percentage. Note that this routine may change or go away
+ in future releases --- IJG may choose to adopt a scaling method that
+ can't be expressed as a simple scalar multiplier, in which case the
+ premise of this routine collapses. Caveat user.
+
+jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
+ const unsigned int *basic_table,
+ int scale_factor, boolean force_baseline)
+ Allows an arbitrary quantization table to be created. which_tbl
+ indicates which table slot to fill. basic_table points to an array
+ of 64 unsigned ints given in normal array order. These values are
+ multiplied by scale_factor/100 and then clamped to the range 1..65535
+ (or to 1..255 if force_baseline is TRUE).
+ CAUTION: prior to library version 6a, jpeg_add_quant_table expected
+ the basic table to be given in JPEG zigzag order. If you need to
+ write code that works with either older or newer versions of this
+ routine, you must check the library version number. Something like
+ "#if JPEG_LIB_VERSION >= 61" is the right test.
+
+jpeg_simple_progression (j_compress_ptr cinfo)
+ Generates a default scan script for writing a progressive-JPEG file.
+ This is the recommended method of creating a progressive file,
+ unless you want to make a custom scan sequence. You must ensure that
+ the JPEG color space is set correctly before calling this routine.
+
+
+Compression parameters (cinfo fields) include:
+
+J_DCT_METHOD dct_method
+ Selects the algorithm used for the DCT step. Choices are:
+ JDCT_ISLOW: slow but accurate integer algorithm
+ JDCT_IFAST: faster, less accurate integer method
+ JDCT_FLOAT: floating-point method
+ JDCT_DEFAULT: default method (normally JDCT_ISLOW)
+ JDCT_FASTEST: fastest method (normally JDCT_IFAST)
+ The FLOAT method is very slightly more accurate than the ISLOW method,
+ but may give different results on different machines due to varying
+ roundoff behavior. The integer methods should give the same results
+ on all machines. On machines with sufficiently fast FP hardware, the
+ floating-point method may also be the fastest. The IFAST method is
+ considerably less accurate than the other two; its use is not
+ recommended if high quality is a concern. JDCT_DEFAULT and
+ JDCT_FASTEST are macros configurable by each installation.
+
+J_COLOR_SPACE jpeg_color_space
+int num_components
+ The JPEG color space and corresponding number of components; see
+ "Special color spaces", below, for more info. We recommend using
+ jpeg_set_color_space() if you want to change these.
+
+boolean optimize_coding
+ TRUE causes the compressor to compute optimal Huffman coding tables
+ for the image. This requires an extra pass over the data and
+ therefore costs a good deal of space and time. The default is
+ FALSE, which tells the compressor to use the supplied or default
+ Huffman tables. In most cases optimal tables save only a few percent
+ of file size compared to the default tables. Note that when this is
+ TRUE, you need not supply Huffman tables at all, and any you do
+ supply will be overwritten.
+
+unsigned int restart_interval
+int restart_in_rows
+ To emit restart markers in the JPEG file, set one of these nonzero.
+ Set restart_interval to specify the exact interval in MCU blocks.
+ Set restart_in_rows to specify the interval in MCU rows. (If
+ restart_in_rows is not 0, then restart_interval is set after the
+ image width in MCUs is computed.) Defaults are zero (no restarts).
+ One restart marker per MCU row is often a good choice.
+ NOTE: the overhead of restart markers is higher in grayscale JPEG
+ files than in color files, and MUCH higher in progressive JPEGs.
+ If you use restarts, you may want to use larger intervals in those
+ cases.
+
+const jpeg_scan_info * scan_info
+int num_scans
+ By default, scan_info is NULL; this causes the compressor to write a
+ single-scan sequential JPEG file. If not NULL, scan_info points to
+ an array of scan definition records of length num_scans. The
+ compressor will then write a JPEG file having one scan for each scan
+ definition record. This is used to generate noninterleaved or
+ progressive JPEG files. The library checks that the scan array
+ defines a valid JPEG scan sequence. (jpeg_simple_progression creates
+ a suitable scan definition array for progressive JPEG.) This is
+ discussed further under "Progressive JPEG support".
+
+int smoothing_factor
+ If non-zero, the input image is smoothed; the value should be 1 for
+ minimal smoothing to 100 for maximum smoothing. Consult jcsample.c
+ for details of the smoothing algorithm. The default is zero.
+
+boolean write_JFIF_header
+ If TRUE, a JFIF APP0 marker is emitted. jpeg_set_defaults() and
+ jpeg_set_colorspace() set this TRUE if a JFIF-legal JPEG color space
+ (ie, YCbCr or grayscale) is selected, otherwise FALSE.
+
+UINT8 JFIF_major_version
+UINT8 JFIF_minor_version
+ The version number to be written into the JFIF marker.
+ jpeg_set_defaults() initializes the version to 1.01 (major=minor=1).
+ You should set it to 1.02 (major=1, minor=2) if you plan to write
+ any JFIF 1.02 extension markers.
+
+UINT8 density_unit
+UINT16 X_density
+UINT16 Y_density
+ The resolution information to be written into the JFIF marker;
+ not used otherwise. density_unit may be 0 for unknown,
+ 1 for dots/inch, or 2 for dots/cm. The default values are 0,1,1
+ indicating square pixels of unknown size.
+
+boolean write_Adobe_marker
+ If TRUE, an Adobe APP14 marker is emitted. jpeg_set_defaults() and
+ jpeg_set_colorspace() set this TRUE if JPEG color space RGB, CMYK,
+ or YCCK is selected, otherwise FALSE. It is generally a bad idea
+ to set both write_JFIF_header and write_Adobe_marker. In fact,
+ you probably shouldn't change the default settings at all --- the
+ default behavior ensures that the JPEG file's color space can be
+ recognized by the decoder.
+
+JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
+ Pointers to coefficient quantization tables, one per table slot,
+ or NULL if no table is defined for a slot. Usually these should
+ be set via one of the above helper routines; jpeg_add_quant_table()
+ is general enough to define any quantization table. The other
+ routines will set up table slot 0 for luminance quality and table
+ slot 1 for chrominance.
+
+JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
+JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]
+ Pointers to Huffman coding tables, one per table slot, or NULL if
+ no table is defined for a slot. Slots 0 and 1 are filled with the
+ JPEG sample tables by jpeg_set_defaults(). If you need to allocate
+ more table structures, jpeg_alloc_huff_table() may be used.
+ Note that optimal Huffman tables can be computed for an image
+ by setting optimize_coding, as discussed above; there's seldom
+ any need to mess with providing your own Huffman tables.
+
+There are some additional cinfo fields which are not documented here
+because you currently can't change them; for example, you can't set
+arith_code TRUE because arithmetic coding is unsupported.
+
+
+Per-component parameters are stored in the struct cinfo.comp_info[i] for
+component number i. Note that components here refer to components of the
+JPEG color space, *not* the source image color space. A suitably large
+comp_info[] array is allocated by jpeg_set_defaults(); if you choose not
+to use that routine, it's up to you to allocate the array.
+
+int component_id
+ The one-byte identifier code to be recorded in the JPEG file for
+ this component. For the standard color spaces, we recommend you
+ leave the default values alone.
+
+int h_samp_factor
+int v_samp_factor
+ Horizontal and vertical sampling factors for the component; must
+ be 1..4 according to the JPEG standard. Note that larger sampling
+ factors indicate a higher-resolution component; many people find
+ this behavior quite unintuitive. The default values are 2,2 for
+ luminance components and 1,1 for chrominance components, except
+ for grayscale where 1,1 is used.
+
+int quant_tbl_no
+ Quantization table number for component. The default value is
+ 0 for luminance components and 1 for chrominance components.
+
+int dc_tbl_no
+int ac_tbl_no
+ DC and AC entropy coding table numbers. The default values are
+ 0 for luminance components and 1 for chrominance components.
+
+int component_index
+ Must equal the component's index in comp_info[]. (Beginning in
+ release v6, the compressor library will fill this in automatically;
+ you don't have to.)
+
+
+Decompression parameter selection
+---------------------------------
+
+Decompression parameter selection is somewhat simpler than compression
+parameter selection, since all of the JPEG internal parameters are
+recorded in the source file and need not be supplied by the application.
+(Unless you are working with abbreviated files, in which case see
+"Abbreviated datastreams", below.) Decompression parameters control
+the postprocessing done on the image to deliver it in a format suitable
+for the application's use. Many of the parameters control speed/quality
+tradeoffs, in which faster decompression may be obtained at the price of
+a poorer-quality image. The defaults select the highest quality (slowest)
+processing.
+
+The following fields in the JPEG object are set by jpeg_read_header() and
+may be useful to the application in choosing decompression parameters:
+
+JDIMENSION image_width Width and height of image
+JDIMENSION image_height
+int num_components Number of color components
+J_COLOR_SPACE jpeg_color_space Colorspace of image
+boolean saw_JFIF_marker TRUE if a JFIF APP0 marker was seen
+ UINT8 JFIF_major_version Version information from JFIF marker
+ UINT8 JFIF_minor_version
+ UINT8 density_unit Resolution data from JFIF marker
+ UINT16 X_density
+ UINT16 Y_density
+boolean saw_Adobe_marker TRUE if an Adobe APP14 marker was seen
+ UINT8 Adobe_transform Color transform code from Adobe marker
+
+The JPEG color space, unfortunately, is something of a guess since the JPEG
+standard proper does not provide a way to record it. In practice most files
+adhere to the JFIF or Adobe conventions, and the decoder will recognize these
+correctly. See "Special color spaces", below, for more info.
+
+
+The decompression parameters that determine the basic properties of the
+returned image are:
+
+J_COLOR_SPACE out_color_space
+ Output color space. jpeg_read_header() sets an appropriate default
+ based on jpeg_color_space; typically it will be RGB or grayscale.
+ The application can change this field to request output in a different
+ colorspace. For example, set it to JCS_GRAYSCALE to get grayscale
+ output from a color file. (This is useful for previewing: grayscale
+ output is faster than full color since the color components need not
+ be processed.) Note that not all possible color space transforms are
+ currently implemented; you may need to extend jdcolor.c if you want an
+ unusual conversion.
+
+unsigned int scale_num, scale_denom
+ Scale the image by the fraction scale_num/scale_denom. Default is
+ 1/1, or no scaling. Currently, the only supported scaling ratios
+ are 1/1, 1/2, 1/4, and 1/8. (The library design allows for arbitrary
+ scaling ratios but this is not likely to be implemented any time soon.)
+ Smaller scaling ratios permit significantly faster decoding since
+ fewer pixels need be processed and a simpler IDCT method can be used.
+
+boolean quantize_colors
+ If set TRUE, colormapped output will be delivered. Default is FALSE,
+ meaning that full-color output will be delivered.
+
+The next three parameters are relevant only if quantize_colors is TRUE.
+
+int desired_number_of_colors
+ Maximum number of colors to use in generating a library-supplied color
+ map (the actual number of colors is returned in a different field).
+ Default 256. Ignored when the application supplies its own color map.
+
+boolean two_pass_quantize
+ If TRUE, an extra pass over the image is made to select a custom color
+ map for the image. This usually looks a lot better than the one-size-
+ fits-all colormap that is used otherwise. Default is TRUE. Ignored
+ when the application supplies its own color map.
+
+J_DITHER_MODE dither_mode
+ Selects color dithering method. Supported values are:
+ JDITHER_NONE no dithering: fast, very low quality
+ JDITHER_ORDERED ordered dither: moderate speed and quality
+ JDITHER_FS Floyd-Steinberg dither: slow, high quality
+ Default is JDITHER_FS. (At present, ordered dither is implemented
+ only in the single-pass, standard-colormap case. If you ask for
+ ordered dither when two_pass_quantize is TRUE or when you supply
+ an external color map, you'll get F-S dithering.)
+
+When quantize_colors is TRUE, the target color map is described by the next
+two fields. colormap is set to NULL by jpeg_read_header(). The application
+can supply a color map by setting colormap non-NULL and setting
+actual_number_of_colors to the map size. Otherwise, jpeg_start_decompress()
+selects a suitable color map and sets these two fields itself.
+[Implementation restriction: at present, an externally supplied colormap is
+only accepted for 3-component output color spaces.]
+
+JSAMPARRAY colormap
+ The color map, represented as a 2-D pixel array of out_color_components
+ rows and actual_number_of_colors columns. Ignored if not quantizing.
+ CAUTION: if the JPEG library creates its own colormap, the storage
+ pointed to by this field is released by jpeg_finish_decompress().
+ Copy the colormap somewhere else first, if you want to save it.
+
+int actual_number_of_colors
+ The number of colors in the color map.
+
+Additional decompression parameters that the application may set include:
+
+J_DCT_METHOD dct_method
+ Selects the algorithm used for the DCT step. Choices are the same
+ as described above for compression.
+
+boolean do_fancy_upsampling
+ If TRUE, do careful upsampling of chroma components. If FALSE,
+ a faster but sloppier method is used. Default is TRUE. The visual
+ impact of the sloppier method is often very small.
+
+boolean do_block_smoothing
+ If TRUE, interblock smoothing is applied in early stages of decoding
+ progressive JPEG files; if FALSE, not. Default is TRUE. Early
+ progression stages look "fuzzy" with smoothing, "blocky" without.
+ In any case, block smoothing ceases to be applied after the first few
+ AC coefficients are known to full accuracy, so it is relevant only
+ when using buffered-image mode for progressive images.
+
+boolean enable_1pass_quant
+boolean enable_external_quant
+boolean enable_2pass_quant
+ These are significant only in buffered-image mode, which is
+ described in its own section below.
+
+
+The output image dimensions are given by the following fields. These are
+computed from the source image dimensions and the decompression parameters
+by jpeg_start_decompress(). You can also call jpeg_calc_output_dimensions()
+to obtain the values that will result from the current parameter settings.
+This can be useful if you are trying to pick a scaling ratio that will get
+close to a desired target size. It's also important if you are using the
+JPEG library's memory manager to allocate output buffer space, because you
+are supposed to request such buffers *before* jpeg_start_decompress().
+
+JDIMENSION output_width Actual dimensions of output image.
+JDIMENSION output_height
+int out_color_components Number of color components in out_color_space.
+int output_components Number of color components returned.
+int rec_outbuf_height Recommended height of scanline buffer.
+
+When quantizing colors, output_components is 1, indicating a single color map
+index per pixel. Otherwise it equals out_color_components. The output arrays
+are required to be output_width * output_components JSAMPLEs wide.
+
+rec_outbuf_height is the recommended minimum height (in scanlines) of the
+buffer passed to jpeg_read_scanlines(). If the buffer is smaller, the
+library will still work, but time will be wasted due to unnecessary data
+copying. In high-quality modes, rec_outbuf_height is always 1, but some
+faster, lower-quality modes set it to larger values (typically 2 to 4).
+If you are going to ask for a high-speed processing mode, you may as well
+go to the trouble of honoring rec_outbuf_height so as to avoid data copying.
+(An output buffer larger than rec_outbuf_height lines is OK, but won't
+provide any material speed improvement over that height.)
+
+
+Special color spaces
+--------------------
+
+The JPEG standard itself is "color blind" and doesn't specify any particular
+color space. It is customary to convert color data to a luminance/chrominance
+color space before compressing, since this permits greater compression. The
+existing de-facto JPEG file format standards specify YCbCr or grayscale data
+(JFIF), or grayscale, RGB, YCbCr, CMYK, or YCCK (Adobe). For special
+applications such as multispectral images, other color spaces can be used,
+but it must be understood that such files will be unportable.
+
+The JPEG library can handle the most common colorspace conversions (namely
+RGB <=> YCbCr and CMYK <=> YCCK). It can also deal with data of an unknown
+color space, passing it through without conversion. If you deal extensively
+with an unusual color space, you can easily extend the library to understand
+additional color spaces and perform appropriate conversions.
+
+For compression, the source data's color space is specified by field
+in_color_space. This is transformed to the JPEG file's color space given
+by jpeg_color_space. jpeg_set_defaults() chooses a reasonable JPEG color
+space depending on in_color_space, but you can override this by calling
+jpeg_set_colorspace(). Of course you must select a supported transformation.
+jccolor.c currently supports the following transformations:
+ RGB => YCbCr
+ RGB => GRAYSCALE
+ YCbCr => GRAYSCALE
+ CMYK => YCCK
+plus the null transforms: GRAYSCALE => GRAYSCALE, RGB => RGB,
+YCbCr => YCbCr, CMYK => CMYK, YCCK => YCCK, and UNKNOWN => UNKNOWN.
+
+The de-facto file format standards (JFIF and Adobe) specify APPn markers that
+indicate the color space of the JPEG file. It is important to ensure that
+these are written correctly, or omitted if the JPEG file's color space is not
+one of the ones supported by the de-facto standards. jpeg_set_colorspace()
+will set the compression parameters to include or omit the APPn markers
+properly, so long as it is told the truth about the JPEG color space.
+For example, if you are writing some random 3-component color space without
+conversion, don't try to fake out the library by setting in_color_space and
+jpeg_color_space to JCS_YCbCr; use JCS_UNKNOWN. You may want to write an
+APPn marker of your own devising to identify the colorspace --- see "Special
+markers", below.
+
+When told that the color space is UNKNOWN, the library will default to using
+luminance-quality compression parameters for all color components. You may
+well want to change these parameters. See the source code for
+jpeg_set_colorspace(), in jcparam.c, for details.
+
+For decompression, the JPEG file's color space is given in jpeg_color_space,
+and this is transformed to the output color space out_color_space.
+jpeg_read_header's setting of jpeg_color_space can be relied on if the file
+conforms to JFIF or Adobe conventions, but otherwise it is no better than a
+guess. If you know the JPEG file's color space for certain, you can override
+jpeg_read_header's guess by setting jpeg_color_space. jpeg_read_header also
+selects a default output color space based on (its guess of) jpeg_color_space;
+set out_color_space to override this. Again, you must select a supported
+transformation. jdcolor.c currently supports
+ YCbCr => GRAYSCALE
+ YCbCr => RGB
+ GRAYSCALE => RGB
+ YCCK => CMYK
+as well as the null transforms. (Since GRAYSCALE=>RGB is provided, an
+application can force grayscale JPEGs to look like color JPEGs if it only
+wants to handle one case.)
+
+The two-pass color quantizer, jquant2.c, is specialized to handle RGB data
+(it weights distances appropriately for RGB colors). You'll need to modify
+the code if you want to use it for non-RGB output color spaces. Note that
+jquant2.c is used to map to an application-supplied colormap as well as for
+the normal two-pass colormap selection process.
+
+CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK JPEG
+files: 0 represents 100% ink coverage, rather than 0% ink as you'd expect.
+This is arguably a bug in Photoshop, but if you need to work with Photoshop
+CMYK files, you will have to deal with it in your application. We cannot
+"fix" this in the library by inverting the data during the CMYK<=>YCCK
+transform, because that would break other applications, notably Ghostscript.
+Photoshop versions prior to 3.0 write EPS files containing JPEG-encoded CMYK
+data in the same inverted-YCCK representation used in bare JPEG files, but
+the surrounding PostScript code performs an inversion using the PS image
+operator. I am told that Photoshop 3.0 will write uninverted YCCK in
+EPS/JPEG files, and will omit the PS-level inversion. (But the data
+polarity used in bare JPEG files will not change in 3.0.) In either case,
+the JPEG library must not invert the data itself, or else Ghostscript would
+read these EPS files incorrectly.
+
+
+Error handling
+--------------
+
+When the default error handler is used, any error detected inside the JPEG
+routines will cause a message to be printed on stderr, followed by exit().
+You can supply your own error handling routines to override this behavior
+and to control the treatment of nonfatal warnings and trace/debug messages.
+The file example.c illustrates the most common case, which is to have the
+application regain control after an error rather than exiting.
+
+The JPEG library never writes any message directly; it always goes through
+the error handling routines. Three classes of messages are recognized:
+ * Fatal errors: the library cannot continue.
+ * Warnings: the library can continue, but the data is corrupt, and a
+ damaged output image is likely to result.
+ * Trace/informational messages. These come with a trace level indicating
+ the importance of the message; you can control the verbosity of the
+ program by adjusting the maximum trace level that will be displayed.
+
+You may, if you wish, simply replace the entire JPEG error handling module
+(jerror.c) with your own code. However, you can avoid code duplication by
+only replacing some of the routines depending on the behavior you need.
+This is accomplished by calling jpeg_std_error() as usual, but then overriding
+some of the method pointers in the jpeg_error_mgr struct, as illustrated by
+example.c.
+
+All of the error handling routines will receive a pointer to the JPEG object
+(a j_common_ptr which points to either a jpeg_compress_struct or a
+jpeg_decompress_struct; if you need to tell which, test the is_decompressor
+field). This struct includes a pointer to the error manager struct in its
+"err" field. Frequently, custom error handler routines will need to access
+additional data which is not known to the JPEG library or the standard error
+handler. The most convenient way to do this is to embed either the JPEG
+object or the jpeg_error_mgr struct in a larger structure that contains
+additional fields; then casting the passed pointer provides access to the
+additional fields. Again, see example.c for one way to do it. (Beginning
+with IJG version 6b, there is also a void pointer "client_data" in each
+JPEG object, which the application can also use to find related data.
+The library does not touch client_data at all.)
+
+The individual methods that you might wish to override are:
+
+error_exit (j_common_ptr cinfo)
+ Receives control for a fatal error. Information sufficient to
+ generate the error message has been stored in cinfo->err; call
+ output_message to display it. Control must NOT return to the caller;
+ generally this routine will exit() or longjmp() somewhere.
+ Typically you would override this routine to get rid of the exit()
+ default behavior. Note that if you continue processing, you should
+ clean up the JPEG object with jpeg_abort() or jpeg_destroy().
+
+output_message (j_common_ptr cinfo)
+ Actual output of any JPEG message. Override this to send messages
+ somewhere other than stderr. Note that this method does not know
+ how to generate a message, only where to send it.
+
+format_message (j_common_ptr cinfo, char * buffer)
+ Constructs a readable error message string based on the error info
+ stored in cinfo->err. This method is called by output_message. Few
+ applications should need to override this method. One possible
+ reason for doing so is to implement dynamic switching of error message
+ language.
+
+emit_message (j_common_ptr cinfo, int msg_level)
+ Decide whether or not to emit a warning or trace message; if so,
+ calls output_message. The main reason for overriding this method
+ would be to abort on warnings. msg_level is -1 for warnings,
+ 0 and up for trace messages.
+
+Only error_exit() and emit_message() are called from the rest of the JPEG
+library; the other two are internal to the error handler.
+
+The actual message texts are stored in an array of strings which is pointed to
+by the field err->jpeg_message_table. The messages are numbered from 0 to
+err->last_jpeg_message, and it is these code numbers that are used in the
+JPEG library code. You could replace the message texts (for instance, with
+messages in French or German) by changing the message table pointer. See
+jerror.h for the default texts. CAUTION: this table will almost certainly
+change or grow from one library version to the next.
+
+It may be useful for an application to add its own message texts that are
+handled by the same mechanism. The error handler supports a second "add-on"
+message table for this purpose. To define an addon table, set the pointer
+err->addon_message_table and the message numbers err->first_addon_message and
+err->last_addon_message. If you number the addon messages beginning at 1000
+or so, you won't have to worry about conflicts with the library's built-in
+messages. See the sample applications cjpeg/djpeg for an example of using
+addon messages (the addon messages are defined in cderror.h).
+
+Actual invocation of the error handler is done via macros defined in jerror.h:
+ ERREXITn(...) for fatal errors
+ WARNMSn(...) for corrupt-data warnings
+ TRACEMSn(...) for trace and informational messages.
+These macros store the message code and any additional parameters into the
+error handler struct, then invoke the error_exit() or emit_message() method.
+The variants of each macro are for varying numbers of additional parameters.
+The additional parameters are inserted into the generated message using
+standard printf() format codes.
+
+See jerror.h and jerror.c for further details.
+
+
+Compressed data handling (source and destination managers)
+----------------------------------------------------------
+
+The JPEG compression library sends its compressed data to a "destination
+manager" module. The default destination manager just writes the data to a
+stdio stream, but you can provide your own manager to do something else.
+Similarly, the decompression library calls a "source manager" to obtain the
+compressed data; you can provide your own source manager if you want the data
+to come from somewhere other than a stdio stream.
+
+In both cases, compressed data is processed a bufferload at a time: the
+destination or source manager provides a work buffer, and the library invokes
+the manager only when the buffer is filled or emptied. (You could define a
+one-character buffer to force the manager to be invoked for each byte, but
+that would be rather inefficient.) The buffer's size and location are
+controlled by the manager, not by the library. For example, if you desired to
+decompress a JPEG datastream that was all in memory, you could just make the
+buffer pointer and length point to the original data in memory. Then the
+buffer-reload procedure would be invoked only if the decompressor ran off the
+end of the datastream, which would indicate an erroneous datastream.
+
+The work buffer is defined as an array of datatype JOCTET, which is generally
+"char" or "unsigned char". On a machine where char is not exactly 8 bits
+wide, you must define JOCTET as a wider data type and then modify the data
+source and destination modules to transcribe the work arrays into 8-bit units
+on external storage.
+
+A data destination manager struct contains a pointer and count defining the
+next byte to write in the work buffer and the remaining free space:
+
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+
+The library increments the pointer and decrements the count until the buffer
+is filled. The manager's empty_output_buffer method must reset the pointer
+and count. The manager is expected to remember the buffer's starting address
+and total size in private fields not visible to the library.
+
+A data destination manager provides three methods:
+
+init_destination (j_compress_ptr cinfo)
+ Initialize destination. This is called by jpeg_start_compress()
+ before any data is actually written. It must initialize
+ next_output_byte and free_in_buffer. free_in_buffer must be
+ initialized to a positive value.
+
+empty_output_buffer (j_compress_ptr cinfo)
+ This is called whenever the buffer has filled (free_in_buffer
+ reaches zero). In typical applications, it should write out the
+ *entire* buffer (use the saved start address and buffer length;
+ ignore the current state of next_output_byte and free_in_buffer).
+ Then reset the pointer & count to the start of the buffer, and
+ return TRUE indicating that the buffer has been dumped.
+ free_in_buffer must be set to a positive value when TRUE is
+ returned. A FALSE return should only be used when I/O suspension is
+ desired (this operating mode is discussed in the next section).
+
+term_destination (j_compress_ptr cinfo)
+ Terminate destination --- called by jpeg_finish_compress() after all
+ data has been written. In most applications, this must flush any
+ data remaining in the buffer. Use either next_output_byte or
+ free_in_buffer to determine how much data is in the buffer.
+
+term_destination() is NOT called by jpeg_abort() or jpeg_destroy(). If you
+want the destination manager to be cleaned up during an abort, you must do it
+yourself.
+
+You will also need code to create a jpeg_destination_mgr struct, fill in its
+method pointers, and insert a pointer to the struct into the "dest" field of
+the JPEG compression object. This can be done in-line in your setup code if
+you like, but it's probably cleaner to provide a separate routine similar to
+the jpeg_stdio_dest() routine of the supplied destination manager.
+
+Decompression source managers follow a parallel design, but with some
+additional frammishes. The source manager struct contains a pointer and count
+defining the next byte to read from the work buffer and the number of bytes
+remaining:
+
+ const JOCTET * next_input_byte; /* => next byte to read from buffer */
+ size_t bytes_in_buffer; /* # of bytes remaining in buffer */
+
+The library increments the pointer and decrements the count until the buffer
+is emptied. The manager's fill_input_buffer method must reset the pointer and
+count. In most applications, the manager must remember the buffer's starting
+address and total size in private fields not visible to the library.
+
+A data source manager provides five methods:
+
+init_source (j_decompress_ptr cinfo)
+ Initialize source. This is called by jpeg_read_header() before any
+ data is actually read. Unlike init_destination(), it may leave
+ bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
+ will occur immediately).
+
+fill_input_buffer (j_decompress_ptr cinfo)
+ This is called whenever bytes_in_buffer has reached zero and more
+ data is wanted. In typical applications, it should read fresh data
+ into the buffer (ignoring the current state of next_input_byte and
+ bytes_in_buffer), reset the pointer & count to the start of the
+ buffer, and return TRUE indicating that the buffer has been reloaded.
+ It is not necessary to fill the buffer entirely, only to obtain at
+ least one more byte. bytes_in_buffer MUST be set to a positive value
+ if TRUE is returned. A FALSE return should only be used when I/O
+ suspension is desired (this mode is discussed in the next section).
+
+skip_input_data (j_decompress_ptr cinfo, long num_bytes)
+ Skip num_bytes worth of data. The buffer pointer and count should
+ be advanced over num_bytes input bytes, refilling the buffer as
+ needed. This is used to skip over a potentially large amount of
+ uninteresting data (such as an APPn marker). In some applications
+ it may be possible to optimize away the reading of the skipped data,
+ but it's not clear that being smart is worth much trouble; large
+ skips are uncommon. bytes_in_buffer may be zero on return.
+ A zero or negative skip count should be treated as a no-op.
+
+resync_to_restart (j_decompress_ptr cinfo, int desired)
+ This routine is called only when the decompressor has failed to find
+ a restart (RSTn) marker where one is expected. Its mission is to
+ find a suitable point for resuming decompression. For most
+ applications, we recommend that you just use the default resync
+ procedure, jpeg_resync_to_restart(). However, if you are able to back
+ up in the input data stream, or if you have a-priori knowledge about
+ the likely location of restart markers, you may be able to do better.
+ Read the read_restart_marker() and jpeg_resync_to_restart() routines
+ in jdmarker.c if you think you'd like to implement your own resync
+ procedure.
+
+term_source (j_decompress_ptr cinfo)
+ Terminate source --- called by jpeg_finish_decompress() after all
+ data has been read. Often a no-op.
+
+For both fill_input_buffer() and skip_input_data(), there is no such thing
+as an EOF return. If the end of the file has been reached, the routine has
+a choice of exiting via ERREXIT() or inserting fake data into the buffer.
+In most cases, generating a warning message and inserting a fake EOI marker
+is the best course of action --- this will allow the decompressor to output
+however much of the image is there. In pathological cases, the decompressor
+may swallow the EOI and again demand data ... just keep feeding it fake EOIs.
+jdatasrc.c illustrates the recommended error recovery behavior.
+
+term_source() is NOT called by jpeg_abort() or jpeg_destroy(). If you want
+the source manager to be cleaned up during an abort, you must do it yourself.
+
+You will also need code to create a jpeg_source_mgr struct, fill in its method
+pointers, and insert a pointer to the struct into the "src" field of the JPEG
+decompression object. This can be done in-line in your setup code if you
+like, but it's probably cleaner to provide a separate routine similar to the
+jpeg_stdio_src() routine of the supplied source manager.
+
+For more information, consult the stdio source and destination managers
+in jdatasrc.c and jdatadst.c.
+
+
+I/O suspension
+--------------
+
+Some applications need to use the JPEG library as an incremental memory-to-
+memory filter: when the compressed data buffer is filled or emptied, they want
+control to return to the outer loop, rather than expecting that the buffer can
+be emptied or reloaded within the data source/destination manager subroutine.
+The library supports this need by providing an "I/O suspension" mode, which we
+describe in this section.
+
+The I/O suspension mode is not a panacea: nothing is guaranteed about the
+maximum amount of time spent in any one call to the library, so it will not
+eliminate response-time problems in single-threaded applications. If you
+need guaranteed response time, we suggest you "bite the bullet" and implement
+a real multi-tasking capability.
+
+To use I/O suspension, cooperation is needed between the calling application
+and the data source or destination manager; you will always need a custom
+source/destination manager. (Please read the previous section if you haven't
+already.) The basic idea is that the empty_output_buffer() or
+fill_input_buffer() routine is a no-op, merely returning FALSE to indicate
+that it has done nothing. Upon seeing this, the JPEG library suspends
+operation and returns to its caller. The surrounding application is
+responsible for emptying or refilling the work buffer before calling the
+JPEG library again.
+
+Compression suspension:
+
+For compression suspension, use an empty_output_buffer() routine that returns
+FALSE; typically it will not do anything else. This will cause the
+compressor to return to the caller of jpeg_write_scanlines(), with the return
+value indicating that not all the supplied scanlines have been accepted.
+The application must make more room in the output buffer, adjust the output
+buffer pointer/count appropriately, and then call jpeg_write_scanlines()
+again, pointing to the first unconsumed scanline.
+
+When forced to suspend, the compressor will backtrack to a convenient stopping
+point (usually the start of the current MCU); it will regenerate some output
+data when restarted. Therefore, although empty_output_buffer() is only
+called when the buffer is filled, you should NOT write out the entire buffer
+after a suspension. Write only the data up to the current position of
+next_output_byte/free_in_buffer. The data beyond that point will be
+regenerated after resumption.
+
+Because of the backtracking behavior, a good-size output buffer is essential
+for efficiency; you don't want the compressor to suspend often. (In fact, an
+overly small buffer could lead to infinite looping, if a single MCU required
+more data than would fit in the buffer.) We recommend a buffer of at least
+several Kbytes. You may want to insert explicit code to ensure that you don't
+call jpeg_write_scanlines() unless there is a reasonable amount of space in
+the output buffer; in other words, flush the buffer before trying to compress
+more data.
+
+The compressor does not allow suspension while it is trying to write JPEG
+markers at the beginning and end of the file. This means that:
+ * At the beginning of a compression operation, there must be enough free
+ space in the output buffer to hold the header markers (typically 600 or
+ so bytes). The recommended buffer size is bigger than this anyway, so
+ this is not a problem as long as you start with an empty buffer. However,
+ this restriction might catch you if you insert large special markers, such
+ as a JFIF thumbnail image, without flushing the buffer afterwards.
+ * When you call jpeg_finish_compress(), there must be enough space in the
+ output buffer to emit any buffered data and the final EOI marker. In the
+ current implementation, half a dozen bytes should suffice for this, but
+ for safety's sake we recommend ensuring that at least 100 bytes are free
+ before calling jpeg_finish_compress().
+
+A more significant restriction is that jpeg_finish_compress() cannot suspend.
+This means you cannot use suspension with multi-pass operating modes, namely
+Huffman code optimization and multiple-scan output. Those modes write the
+whole file during jpeg_finish_compress(), which will certainly result in
+buffer overrun. (Note that this restriction applies only to compression,
+not decompression. The decompressor supports input suspension in all of its
+operating modes.)
+
+Decompression suspension:
+
+For decompression suspension, use a fill_input_buffer() routine that simply
+returns FALSE (except perhaps during error recovery, as discussed below).
+This will cause the decompressor to return to its caller with an indication
+that suspension has occurred. This can happen at four places:
+ * jpeg_read_header(): will return JPEG_SUSPENDED.
+ * jpeg_start_decompress(): will return FALSE, rather than its usual TRUE.
+ * jpeg_read_scanlines(): will return the number of scanlines already
+ completed (possibly 0).
+ * jpeg_finish_decompress(): will return FALSE, rather than its usual TRUE.
+The surrounding application must recognize these cases, load more data into
+the input buffer, and repeat the call. In the case of jpeg_read_scanlines(),
+increment the passed pointers past any scanlines successfully read.
+
+Just as with compression, the decompressor will typically backtrack to a
+convenient restart point before suspending. When fill_input_buffer() is
+called, next_input_byte/bytes_in_buffer point to the current restart point,
+which is where the decompressor will backtrack to if FALSE is returned.
+The data beyond that position must NOT be discarded if you suspend; it needs
+to be re-read upon resumption. In most implementations, you'll need to shift
+this data down to the start of your work buffer and then load more data after
+it. Again, this behavior means that a several-Kbyte work buffer is essential
+for decent performance; furthermore, you should load a reasonable amount of
+new data before resuming decompression. (If you loaded, say, only one new
+byte each time around, you could waste a LOT of cycles.)
+
+The skip_input_data() source manager routine requires special care in a
+suspension scenario. This routine is NOT granted the ability to suspend the
+decompressor; it can decrement bytes_in_buffer to zero, but no more. If the
+requested skip distance exceeds the amount of data currently in the input
+buffer, then skip_input_data() must set bytes_in_buffer to zero and record the
+additional skip distance somewhere else. The decompressor will immediately
+call fill_input_buffer(), which should return FALSE, which will cause a
+suspension return. The surrounding application must then arrange to discard
+the recorded number of bytes before it resumes loading the input buffer.
+(Yes, this design is rather baroque, but it avoids complexity in the far more
+common case where a non-suspending source manager is used.)
+
+If the input data has been exhausted, we recommend that you emit a warning
+and insert dummy EOI markers just as a non-suspending data source manager
+would do. This can be handled either in the surrounding application logic or
+within fill_input_buffer(); the latter is probably more efficient. If
+fill_input_buffer() knows that no more data is available, it can set the
+pointer/count to point to a dummy EOI marker and then return TRUE just as
+though it had read more data in a non-suspending situation.
+
+The decompressor does not attempt to suspend within standard JPEG markers;
+instead it will backtrack to the start of the marker and reprocess the whole
+marker next time. Hence the input buffer must be large enough to hold the
+longest standard marker in the file. Standard JPEG markers should normally
+not exceed a few hundred bytes each (DHT tables are typically the longest).
+We recommend at least a 2K buffer for performance reasons, which is much
+larger than any correct marker is likely to be. For robustness against
+damaged marker length counts, you may wish to insert a test in your
+application for the case that the input buffer is completely full and yet
+the decoder has suspended without consuming any data --- otherwise, if this
+situation did occur, it would lead to an endless loop. (The library can't
+provide this test since it has no idea whether "the buffer is full", or
+even whether there is a fixed-size input buffer.)
+
+The input buffer would need to be 64K to allow for arbitrary COM or APPn
+markers, but these are handled specially: they are either saved into allocated
+memory, or skipped over by calling skip_input_data(). In the former case,
+suspension is handled correctly, and in the latter case, the problem of
+buffer overrun is placed on skip_input_data's shoulders, as explained above.
+Note that if you provide your own marker handling routine for large markers,
+you should consider how to deal with buffer overflow.
+
+Multiple-buffer management:
+
+In some applications it is desirable to store the compressed data in a linked
+list of buffer areas, so as to avoid data copying. This can be handled by
+having empty_output_buffer() or fill_input_buffer() set the pointer and count
+to reference the next available buffer; FALSE is returned only if no more
+buffers are available. Although seemingly straightforward, there is a
+pitfall in this approach: the backtrack that occurs when FALSE is returned
+could back up into an earlier buffer. For example, when fill_input_buffer()
+is called, the current pointer & count indicate the backtrack restart point.
+Since fill_input_buffer() will set the pointer and count to refer to a new
+buffer, the restart position must be saved somewhere else. Suppose a second
+call to fill_input_buffer() occurs in the same library call, and no
+additional input data is available, so fill_input_buffer must return FALSE.
+If the JPEG library has not moved the pointer/count forward in the current
+buffer, then *the correct restart point is the saved position in the prior
+buffer*. Prior buffers may be discarded only after the library establishes
+a restart point within a later buffer. Similar remarks apply for output into
+a chain of buffers.
+
+The library will never attempt to backtrack over a skip_input_data() call,
+so any skipped data can be permanently discarded. You still have to deal
+with the case of skipping not-yet-received data, however.
+
+It's much simpler to use only a single buffer; when fill_input_buffer() is
+called, move any unconsumed data (beyond the current pointer/count) down to
+the beginning of this buffer and then load new data into the remaining buffer
+space. This approach requires a little more data copying but is far easier
+to get right.
+
+
+Progressive JPEG support
+------------------------
+
+Progressive JPEG rearranges the stored data into a series of scans of
+increasing quality. In situations where a JPEG file is transmitted across a
+slow communications link, a decoder can generate a low-quality image very
+quickly from the first scan, then gradually improve the displayed quality as
+more scans are received. The final image after all scans are complete is
+identical to that of a regular (sequential) JPEG file of the same quality
+setting. Progressive JPEG files are often slightly smaller than equivalent
+sequential JPEG files, but the possibility of incremental display is the main
+reason for using progressive JPEG.
+
+The IJG encoder library generates progressive JPEG files when given a
+suitable "scan script" defining how to divide the data into scans.
+Creation of progressive JPEG files is otherwise transparent to the encoder.
+Progressive JPEG files can also be read transparently by the decoder library.
+If the decoding application simply uses the library as defined above, it
+will receive a final decoded image without any indication that the file was
+progressive. Of course, this approach does not allow incremental display.
+To perform incremental display, an application needs to use the decoder
+library's "buffered-image" mode, in which it receives a decoded image
+multiple times.
+
+Each displayed scan requires about as much work to decode as a full JPEG
+image of the same size, so the decoder must be fairly fast in relation to the
+data transmission rate in order to make incremental display useful. However,
+it is possible to skip displaying the image and simply add the incoming bits
+to the decoder's coefficient buffer. This is fast because only Huffman
+decoding need be done, not IDCT, upsampling, colorspace conversion, etc.
+The IJG decoder library allows the application to switch dynamically between
+displaying the image and simply absorbing the incoming bits. A properly
+coded application can automatically adapt the number of display passes to
+suit the time available as the image is received. Also, a final
+higher-quality display cycle can be performed from the buffered data after
+the end of the file is reached.
+
+Progressive compression:
+
+To create a progressive JPEG file (or a multiple-scan sequential JPEG file),
+set the scan_info cinfo field to point to an array of scan descriptors, and
+perform compression as usual. Instead of constructing your own scan list,
+you can call the jpeg_simple_progression() helper routine to create a
+recommended progression sequence; this method should be used by all
+applications that don't want to get involved in the nitty-gritty of
+progressive scan sequence design. (If you want to provide user control of
+scan sequences, you may wish to borrow the scan script reading code found
+in rdswitch.c, so that you can read scan script files just like cjpeg's.)
+When scan_info is not NULL, the compression library will store DCT'd data
+into a buffer array as jpeg_write_scanlines() is called, and will emit all
+the requested scans during jpeg_finish_compress(). This implies that
+multiple-scan output cannot be created with a suspending data destination
+manager, since jpeg_finish_compress() does not support suspension. We
+should also note that the compressor currently forces Huffman optimization
+mode when creating a progressive JPEG file, because the default Huffman
+tables are unsuitable for progressive files.
+
+Progressive decompression:
+
+When buffered-image mode is not used, the decoder library will read all of
+a multi-scan file during jpeg_start_decompress(), so that it can provide a
+final decoded image. (Here "multi-scan" means either progressive or
+multi-scan sequential.) This makes multi-scan files transparent to the
+decoding application. However, existing applications that used suspending
+input with version 5 of the IJG library will need to be modified to check
+for a suspension return from jpeg_start_decompress().
+
+To perform incremental display, an application must use the library's
+buffered-image mode. This is described in the next section.
+
+
+Buffered-image mode
+-------------------
+
+In buffered-image mode, the library stores the partially decoded image in a
+coefficient buffer, from which it can be read out as many times as desired.
+This mode is typically used for incremental display of progressive JPEG files,
+but it can be used with any JPEG file. Each scan of a progressive JPEG file
+adds more data (more detail) to the buffered image. The application can
+display in lockstep with the source file (one display pass per input scan),
+or it can allow input processing to outrun display processing. By making
+input and display processing run independently, it is possible for the
+application to adapt progressive display to a wide range of data transmission
+rates.
+
+The basic control flow for buffered-image decoding is
+
+ jpeg_create_decompress()
+ set data source
+ jpeg_read_header()
+ set overall decompression parameters
+ cinfo.buffered_image = TRUE; /* select buffered-image mode */
+ jpeg_start_decompress()
+ for (each output pass) {
+ adjust output decompression parameters if required
+ jpeg_start_output() /* start a new output pass */
+ for (all scanlines in image) {
+ jpeg_read_scanlines()
+ display scanlines
+ }
+ jpeg_finish_output() /* terminate output pass */
+ }
+ jpeg_finish_decompress()
+ jpeg_destroy_decompress()
+
+This differs from ordinary unbuffered decoding in that there is an additional
+level of looping. The application can choose how many output passes to make
+and how to display each pass.
+
+The simplest approach to displaying progressive images is to do one display
+pass for each scan appearing in the input file. In this case the outer loop
+condition is typically
+ while (! jpeg_input_complete(&cinfo))
+and the start-output call should read
+ jpeg_start_output(&cinfo, cinfo.input_scan_number);
+The second parameter to jpeg_start_output() indicates which scan of the input
+file is to be displayed; the scans are numbered starting at 1 for this
+purpose. (You can use a loop counter starting at 1 if you like, but using
+the library's input scan counter is easier.) The library automatically reads
+data as necessary to complete each requested scan, and jpeg_finish_output()
+advances to the next scan or end-of-image marker (hence input_scan_number
+will be incremented by the time control arrives back at jpeg_start_output()).
+With this technique, data is read from the input file only as needed, and
+input and output processing run in lockstep.
+
+After reading the final scan and reaching the end of the input file, the
+buffered image remains available; it can be read additional times by
+repeating the jpeg_start_output()/jpeg_read_scanlines()/jpeg_finish_output()
+sequence. For example, a useful technique is to use fast one-pass color
+quantization for display passes made while the image is arriving, followed by
+a final display pass using two-pass quantization for highest quality. This
+is done by changing the library parameters before the final output pass.
+Changing parameters between passes is discussed in detail below.
+
+In general the last scan of a progressive file cannot be recognized as such
+until after it is read, so a post-input display pass is the best approach if
+you want special processing in the final pass.
+
+When done with the image, be sure to call jpeg_finish_decompress() to release
+the buffered image (or just use jpeg_destroy_decompress()).
+
+If input data arrives faster than it can be displayed, the application can
+cause the library to decode input data in advance of what's needed to produce
+output. This is done by calling the routine jpeg_consume_input().
+The return value is one of the following:
+ JPEG_REACHED_SOS: reached an SOS marker (the start of a new scan)
+ JPEG_REACHED_EOI: reached the EOI marker (end of image)
+ JPEG_ROW_COMPLETED: completed reading one MCU row of compressed data
+ JPEG_SCAN_COMPLETED: completed reading last MCU row of current scan
+ JPEG_SUSPENDED: suspended before completing any of the above
+(JPEG_SUSPENDED can occur only if a suspending data source is used.) This
+routine can be called at any time after initializing the JPEG object. It
+reads some additional data and returns when one of the indicated significant
+events occurs. (If called after the EOI marker is reached, it will
+immediately return JPEG_REACHED_EOI without attempting to read more data.)
+
+The library's output processing will automatically call jpeg_consume_input()
+whenever the output processing overtakes the input; thus, simple lockstep
+display requires no direct calls to jpeg_consume_input(). But by adding
+calls to jpeg_consume_input(), you can absorb data in advance of what is
+being displayed. This has two benefits:
+ * You can limit buildup of unprocessed data in your input buffer.
+ * You can eliminate extra display passes by paying attention to the
+ state of the library's input processing.
+
+The first of these benefits only requires interspersing calls to
+jpeg_consume_input() with your display operations and any other processing
+you may be doing. To avoid wasting cycles due to backtracking, it's best to
+call jpeg_consume_input() only after a hundred or so new bytes have arrived.
+This is discussed further under "I/O suspension", above. (Note: the JPEG
+library currently is not thread-safe. You must not call jpeg_consume_input()
+from one thread of control if a different library routine is working on the
+same JPEG object in another thread.)
+
+When input arrives fast enough that more than one new scan is available
+before you start a new output pass, you may as well skip the output pass
+corresponding to the completed scan. This occurs for free if you pass
+cinfo.input_scan_number as the target scan number to jpeg_start_output().
+The input_scan_number field is simply the index of the scan currently being
+consumed by the input processor. You can ensure that this is up-to-date by
+emptying the input buffer just before calling jpeg_start_output(): call
+jpeg_consume_input() repeatedly until it returns JPEG_SUSPENDED or
+JPEG_REACHED_EOI.
+
+The target scan number passed to jpeg_start_output() is saved in the
+cinfo.output_scan_number field. The library's output processing calls
+jpeg_consume_input() whenever the current input scan number and row within
+that scan is less than or equal to the current output scan number and row.
+Thus, input processing can "get ahead" of the output processing but is not
+allowed to "fall behind". You can achieve several different effects by
+manipulating this interlock rule. For example, if you pass a target scan
+number greater than the current input scan number, the output processor will
+wait until that scan starts to arrive before producing any output. (To avoid
+an infinite loop, the target scan number is automatically reset to the last
+scan number when the end of image is reached. Thus, if you specify a large
+target scan number, the library will just absorb the entire input file and
+then perform an output pass. This is effectively the same as what
+jpeg_start_decompress() does when you don't select buffered-image mode.)
+When you pass a target scan number equal to the current input scan number,
+the image is displayed no faster than the current input scan arrives. The
+final possibility is to pass a target scan number less than the current input
+scan number; this disables the input/output interlock and causes the output
+processor to simply display whatever it finds in the image buffer, without
+waiting for input. (However, the library will not accept a target scan
+number less than one, so you can't avoid waiting for the first scan.)
+
+When data is arriving faster than the output display processing can advance
+through the image, jpeg_consume_input() will store data into the buffered
+image beyond the point at which the output processing is reading data out
+again. If the input arrives fast enough, it may "wrap around" the buffer to
+the point where the input is more than one whole scan ahead of the output.
+If the output processing simply proceeds through its display pass without
+paying attention to the input, the effect seen on-screen is that the lower
+part of the image is one or more scans better in quality than the upper part.
+Then, when the next output scan is started, you have a choice of what target
+scan number to use. The recommended choice is to use the current input scan
+number at that time, which implies that you've skipped the output scans
+corresponding to the input scans that were completed while you processed the
+previous output scan. In this way, the decoder automatically adapts its
+speed to the arriving data, by skipping output scans as necessary to keep up
+with the arriving data.
+
+When using this strategy, you'll want to be sure that you perform a final
+output pass after receiving all the data; otherwise your last display may not
+be full quality across the whole screen. So the right outer loop logic is
+something like this:
+ do {
+ absorb any waiting input by calling jpeg_consume_input()
+ final_pass = jpeg_input_complete(&cinfo);
+ adjust output decompression parameters if required
+ jpeg_start_output(&cinfo, cinfo.input_scan_number);
+ ...
+ jpeg_finish_output()
+ } while (! final_pass);
+rather than quitting as soon as jpeg_input_complete() returns TRUE. This
+arrangement makes it simple to use higher-quality decoding parameters
+for the final pass. But if you don't want to use special parameters for
+the final pass, the right loop logic is like this:
+ for (;;) {
+ absorb any waiting input by calling jpeg_consume_input()
+ jpeg_start_output(&cinfo, cinfo.input_scan_number);
+ ...
+ jpeg_finish_output()
+ if (jpeg_input_complete(&cinfo) &&
+ cinfo.input_scan_number == cinfo.output_scan_number)
+ break;
+ }
+In this case you don't need to know in advance whether an output pass is to
+be the last one, so it's not necessary to have reached EOF before starting
+the final output pass; rather, what you want to test is whether the output
+pass was performed in sync with the final input scan. This form of the loop
+will avoid an extra output pass whenever the decoder is able (or nearly able)
+to keep up with the incoming data.
+
+When the data transmission speed is high, you might begin a display pass,
+then find that much or all of the file has arrived before you can complete
+the pass. (You can detect this by noting the JPEG_REACHED_EOI return code
+from jpeg_consume_input(), or equivalently by testing jpeg_input_complete().)
+In this situation you may wish to abort the current display pass and start a
+new one using the newly arrived information. To do so, just call
+jpeg_finish_output() and then start a new pass with jpeg_start_output().
+
+A variant strategy is to abort and restart display if more than one complete
+scan arrives during an output pass; this can be detected by noting
+JPEG_REACHED_SOS returns and/or examining cinfo.input_scan_number. This
+idea should be employed with caution, however, since the display process
+might never get to the bottom of the image before being aborted, resulting
+in the lower part of the screen being several passes worse than the upper.
+In most cases it's probably best to abort an output pass only if the whole
+file has arrived and you want to begin the final output pass immediately.
+
+When receiving data across a communication link, we recommend always using
+the current input scan number for the output target scan number; if a
+higher-quality final pass is to be done, it should be started (aborting any
+incomplete output pass) as soon as the end of file is received. However,
+many other strategies are possible. For example, the application can examine
+the parameters of the current input scan and decide whether to display it or
+not. If the scan contains only chroma data, one might choose not to use it
+as the target scan, expecting that the scan will be small and will arrive
+quickly. To skip to the next scan, call jpeg_consume_input() until it
+returns JPEG_REACHED_SOS or JPEG_REACHED_EOI. Or just use the next higher
+number as the target scan for jpeg_start_output(); but that method doesn't
+let you inspect the next scan's parameters before deciding to display it.
+
+
+In buffered-image mode, jpeg_start_decompress() never performs input and
+thus never suspends. An application that uses input suspension with
+buffered-image mode must be prepared for suspension returns from these
+routines:
+* jpeg_start_output() performs input only if you request 2-pass quantization
+ and the target scan isn't fully read yet. (This is discussed below.)
+* jpeg_read_scanlines(), as always, returns the number of scanlines that it
+ was able to produce before suspending.
+* jpeg_finish_output() will read any markers following the target scan,
+ up to the end of the file or the SOS marker that begins another scan.
+ (But it reads no input if jpeg_consume_input() has already reached the
+ end of the file or a SOS marker beyond the target output scan.)
+* jpeg_finish_decompress() will read until the end of file, and thus can
+ suspend if the end hasn't already been reached (as can be tested by
+ calling jpeg_input_complete()).
+jpeg_start_output(), jpeg_finish_output(), and jpeg_finish_decompress()
+all return TRUE if they completed their tasks, FALSE if they had to suspend.
+In the event of a FALSE return, the application must load more input data
+and repeat the call. Applications that use non-suspending data sources need
+not check the return values of these three routines.
+
+
+It is possible to change decoding parameters between output passes in the
+buffered-image mode. The decoder library currently supports only very
+limited changes of parameters. ONLY THE FOLLOWING parameter changes are
+allowed after jpeg_start_decompress() is called:
+* dct_method can be changed before each call to jpeg_start_output().
+ For example, one could use a fast DCT method for early scans, changing
+ to a higher quality method for the final scan.
+* dither_mode can be changed before each call to jpeg_start_output();
+ of course this has no impact if not using color quantization. Typically
+ one would use ordered dither for initial passes, then switch to
+ Floyd-Steinberg dither for the final pass. Caution: changing dither mode
+ can cause more memory to be allocated by the library. Although the amount
+ of memory involved is not large (a scanline or so), it may cause the
+ initial max_memory_to_use specification to be exceeded, which in the worst
+ case would result in an out-of-memory failure.
+* do_block_smoothing can be changed before each call to jpeg_start_output().
+ This setting is relevant only when decoding a progressive JPEG image.
+ During the first DC-only scan, block smoothing provides a very "fuzzy" look
+ instead of the very "blocky" look seen without it; which is better seems a
+ matter of personal taste. But block smoothing is nearly always a win
+ during later stages, especially when decoding a successive-approximation
+ image: smoothing helps to hide the slight blockiness that otherwise shows
+ up on smooth gradients until the lowest coefficient bits are sent.
+* Color quantization mode can be changed under the rules described below.
+ You *cannot* change between full-color and quantized output (because that
+ would alter the required I/O buffer sizes), but you can change which
+ quantization method is used.
+
+When generating color-quantized output, changing quantization method is a
+very useful way of switching between high-speed and high-quality display.
+The library allows you to change among its three quantization methods:
+1. Single-pass quantization to a fixed color cube.
+ Selected by cinfo.two_pass_quantize = FALSE and cinfo.colormap = NULL.
+2. Single-pass quantization to an application-supplied colormap.
+ Selected by setting cinfo.colormap to point to the colormap (the value of
+ two_pass_quantize is ignored); also set cinfo.actual_number_of_colors.
+3. Two-pass quantization to a colormap chosen specifically for the image.
+ Selected by cinfo.two_pass_quantize = TRUE and cinfo.colormap = NULL.
+ (This is the default setting selected by jpeg_read_header, but it is
+ probably NOT what you want for the first pass of progressive display!)
+These methods offer successively better quality and lesser speed. However,
+only the first method is available for quantizing in non-RGB color spaces.
+
+IMPORTANT: because the different quantizer methods have very different
+working-storage requirements, the library requires you to indicate which
+one(s) you intend to use before you call jpeg_start_decompress(). (If we did
+not require this, the max_memory_to_use setting would be a complete fiction.)
+You do this by setting one or more of these three cinfo fields to TRUE:
+ enable_1pass_quant Fixed color cube colormap
+ enable_external_quant Externally-supplied colormap
+ enable_2pass_quant Two-pass custom colormap
+All three are initialized FALSE by jpeg_read_header(). But
+jpeg_start_decompress() automatically sets TRUE the one selected by the
+current two_pass_quantize and colormap settings, so you only need to set the
+enable flags for any other quantization methods you plan to change to later.
+
+After setting the enable flags correctly at jpeg_start_decompress() time, you
+can change to any enabled quantization method by setting two_pass_quantize
+and colormap properly just before calling jpeg_start_output(). The following
+special rules apply:
+1. You must explicitly set cinfo.colormap to NULL when switching to 1-pass
+ or 2-pass mode from a different mode, or when you want the 2-pass
+ quantizer to be re-run to generate a new colormap.
+2. To switch to an external colormap, or to change to a different external
+ colormap than was used on the prior pass, you must call
+ jpeg_new_colormap() after setting cinfo.colormap.
+NOTE: if you want to use the same colormap as was used in the prior pass,
+you should not do either of these things. This will save some nontrivial
+switchover costs.
+(These requirements exist because cinfo.colormap will always be non-NULL
+after completing a prior output pass, since both the 1-pass and 2-pass
+quantizers set it to point to their output colormaps. Thus you have to
+do one of these two things to notify the library that something has changed.
+Yup, it's a bit klugy, but it's necessary to do it this way for backwards
+compatibility.)
+
+Note that in buffered-image mode, the library generates any requested colormap
+during jpeg_start_output(), not during jpeg_start_decompress().
+
+When using two-pass quantization, jpeg_start_output() makes a pass over the
+buffered image to determine the optimum color map; it therefore may take a
+significant amount of time, whereas ordinarily it does little work. The
+progress monitor hook is called during this pass, if defined. It is also
+important to realize that if the specified target scan number is greater than
+or equal to the current input scan number, jpeg_start_output() will attempt
+to consume input as it makes this pass. If you use a suspending data source,
+you need to check for a FALSE return from jpeg_start_output() under these
+conditions. The combination of 2-pass quantization and a not-yet-fully-read
+target scan is the only case in which jpeg_start_output() will consume input.
+
+
+Application authors who support buffered-image mode may be tempted to use it
+for all JPEG images, even single-scan ones. This will work, but it is
+inefficient: there is no need to create an image-sized coefficient buffer for
+single-scan images. Requesting buffered-image mode for such an image wastes
+memory. Worse, it can cost time on large images, since the buffered data has
+to be swapped out or written to a temporary file. If you are concerned about
+maximum performance on baseline JPEG files, you should use buffered-image
+mode only when the incoming file actually has multiple scans. This can be
+tested by calling jpeg_has_multiple_scans(), which will return a correct
+result at any time after jpeg_read_header() completes.
+
+It is also worth noting that when you use jpeg_consume_input() to let input
+processing get ahead of output processing, the resulting pattern of access to
+the coefficient buffer is quite nonsequential. It's best to use the memory
+manager jmemnobs.c if you can (ie, if you have enough real or virtual main
+memory). If not, at least make sure that max_memory_to_use is set as high as
+possible. If the JPEG memory manager has to use a temporary file, you will
+probably see a lot of disk traffic and poor performance. (This could be
+improved with additional work on the memory manager, but we haven't gotten
+around to it yet.)
+
+In some applications it may be convenient to use jpeg_consume_input() for all
+input processing, including reading the initial markers; that is, you may
+wish to call jpeg_consume_input() instead of jpeg_read_header() during
+startup. This works, but note that you must check for JPEG_REACHED_SOS and
+JPEG_REACHED_EOI return codes as the equivalent of jpeg_read_header's codes.
+Once the first SOS marker has been reached, you must call
+jpeg_start_decompress() before jpeg_consume_input() will consume more input;
+it'll just keep returning JPEG_REACHED_SOS until you do. If you read a
+tables-only file this way, jpeg_consume_input() will return JPEG_REACHED_EOI
+without ever returning JPEG_REACHED_SOS; be sure to check for this case.
+If this happens, the decompressor will not read any more input until you call
+jpeg_abort() to reset it. It is OK to call jpeg_consume_input() even when not
+using buffered-image mode, but in that case it's basically a no-op after the
+initial markers have been read: it will just return JPEG_SUSPENDED.
+
+
+Abbreviated datastreams and multiple images
+-------------------------------------------
+
+A JPEG compression or decompression object can be reused to process multiple
+images. This saves a small amount of time per image by eliminating the
+"create" and "destroy" operations, but that isn't the real purpose of the
+feature. Rather, reuse of an object provides support for abbreviated JPEG
+datastreams. Object reuse can also simplify processing a series of images in
+a single input or output file. This section explains these features.
+
+A JPEG file normally contains several hundred bytes worth of quantization
+and Huffman tables. In a situation where many images will be stored or
+transmitted with identical tables, this may represent an annoying overhead.
+The JPEG standard therefore permits tables to be omitted. The standard
+defines three classes of JPEG datastreams:
+ * "Interchange" datastreams contain an image and all tables needed to decode
+ the image. These are the usual kind of JPEG file.
+ * "Abbreviated image" datastreams contain an image, but are missing some or
+ all of the tables needed to decode that image.
+ * "Abbreviated table specification" (henceforth "tables-only") datastreams
+ contain only table specifications.
+To decode an abbreviated image, it is necessary to load the missing table(s)
+into the decoder beforehand. This can be accomplished by reading a separate
+tables-only file. A variant scheme uses a series of images in which the first
+image is an interchange (complete) datastream, while subsequent ones are
+abbreviated and rely on the tables loaded by the first image. It is assumed
+that once the decoder has read a table, it will remember that table until a
+new definition for the same table number is encountered.
+
+It is the application designer's responsibility to figure out how to associate
+the correct tables with an abbreviated image. While abbreviated datastreams
+can be useful in a closed environment, their use is strongly discouraged in
+any situation where data exchange with other applications might be needed.
+Caveat designer.
+
+The JPEG library provides support for reading and writing any combination of
+tables-only datastreams and abbreviated images. In both compression and
+decompression objects, a quantization or Huffman table will be retained for
+the lifetime of the object, unless it is overwritten by a new table definition.
+
+
+To create abbreviated image datastreams, it is only necessary to tell the
+compressor not to emit some or all of the tables it is using. Each
+quantization and Huffman table struct contains a boolean field "sent_table",
+which normally is initialized to FALSE. For each table used by the image, the
+header-writing process emits the table and sets sent_table = TRUE unless it is
+already TRUE. (In normal usage, this prevents outputting the same table
+definition multiple times, as would otherwise occur because the chroma
+components typically share tables.) Thus, setting this field to TRUE before
+calling jpeg_start_compress() will prevent the table from being written at
+all.
+
+If you want to create a "pure" abbreviated image file containing no tables,
+just call "jpeg_suppress_tables(&cinfo, TRUE)" after constructing all the
+tables. If you want to emit some but not all tables, you'll need to set the
+individual sent_table fields directly.
+
+To create an abbreviated image, you must also call jpeg_start_compress()
+with a second parameter of FALSE, not TRUE. Otherwise jpeg_start_compress()
+will force all the sent_table fields to FALSE. (This is a safety feature to
+prevent abbreviated images from being created accidentally.)
+
+To create a tables-only file, perform the same parameter setup that you
+normally would, but instead of calling jpeg_start_compress() and so on, call
+jpeg_write_tables(&cinfo). This will write an abbreviated datastream
+containing only SOI, DQT and/or DHT markers, and EOI. All the quantization
+and Huffman tables that are currently defined in the compression object will
+be emitted unless their sent_tables flag is already TRUE, and then all the
+sent_tables flags will be set TRUE.
+
+A sure-fire way to create matching tables-only and abbreviated image files
+is to proceed as follows:
+
+ create JPEG compression object
+ set JPEG parameters
+ set destination to tables-only file
+ jpeg_write_tables(&cinfo);
+ set destination to image file
+ jpeg_start_compress(&cinfo, FALSE);
+ write data...
+ jpeg_finish_compress(&cinfo);
+
+Since the JPEG parameters are not altered between writing the table file and
+the abbreviated image file, the same tables are sure to be used. Of course,
+you can repeat the jpeg_start_compress() ... jpeg_finish_compress() sequence
+many times to produce many abbreviated image files matching the table file.
+
+You cannot suppress output of the computed Huffman tables when Huffman
+optimization is selected. (If you could, there'd be no way to decode the
+image...) Generally, you don't want to set optimize_coding = TRUE when
+you are trying to produce abbreviated files.
+
+In some cases you might want to compress an image using tables which are
+not stored in the application, but are defined in an interchange or
+tables-only file readable by the application. This can be done by setting up
+a JPEG decompression object to read the specification file, then copying the
+tables into your compression object. See jpeg_copy_critical_parameters()
+for an example of copying quantization tables.
+
+
+To read abbreviated image files, you simply need to load the proper tables
+into the decompression object before trying to read the abbreviated image.
+If the proper tables are stored in the application program, you can just
+allocate the table structs and fill in their contents directly. For example,
+to load a fixed quantization table into table slot "n":
+
+ if (cinfo.quant_tbl_ptrs[n] == NULL)
+ cinfo.quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) &cinfo);
+ quant_ptr = cinfo.quant_tbl_ptrs[n]; /* quant_ptr is JQUANT_TBL* */
+ for (i = 0; i < 64; i++) {
+ /* Qtable[] is desired quantization table, in natural array order */
+ quant_ptr->quantval[i] = Qtable[i];
+ }
+
+Code to load a fixed Huffman table is typically (for AC table "n"):
+
+ if (cinfo.ac_huff_tbl_ptrs[n] == NULL)
+ cinfo.ac_huff_tbl_ptrs[n] = jpeg_alloc_huff_table((j_common_ptr) &cinfo);
+ huff_ptr = cinfo.ac_huff_tbl_ptrs[n]; /* huff_ptr is JHUFF_TBL* */
+ for (i = 1; i <= 16; i++) {
+ /* counts[i] is number of Huffman codes of length i bits, i=1..16 */
+ huff_ptr->bits[i] = counts[i];
+ }
+ for (i = 0; i < 256; i++) {
+ /* symbols[] is the list of Huffman symbols, in code-length order */
+ huff_ptr->huffval[i] = symbols[i];
+ }
+
+(Note that trying to set cinfo.quant_tbl_ptrs[n] to point directly at a
+constant JQUANT_TBL object is not safe. If the incoming file happened to
+contain a quantization table definition, your master table would get
+overwritten! Instead allocate a working table copy and copy the master table
+into it, as illustrated above. Ditto for Huffman tables, of course.)
+
+You might want to read the tables from a tables-only file, rather than
+hard-wiring them into your application. The jpeg_read_header() call is
+sufficient to read a tables-only file. You must pass a second parameter of
+FALSE to indicate that you do not require an image to be present. Thus, the
+typical scenario is
+
+ create JPEG decompression object
+ set source to tables-only file
+ jpeg_read_header(&cinfo, FALSE);
+ set source to abbreviated image file
+ jpeg_read_header(&cinfo, TRUE);
+ set decompression parameters
+ jpeg_start_decompress(&cinfo);
+ read data...
+ jpeg_finish_decompress(&cinfo);
+
+In some cases, you may want to read a file without knowing whether it contains
+an image or just tables. In that case, pass FALSE and check the return value
+from jpeg_read_header(): it will be JPEG_HEADER_OK if an image was found,
+JPEG_HEADER_TABLES_ONLY if only tables were found. (A third return value,
+JPEG_SUSPENDED, is possible when using a suspending data source manager.)
+Note that jpeg_read_header() will not complain if you read an abbreviated
+image for which you haven't loaded the missing tables; the missing-table check
+occurs later, in jpeg_start_decompress().
+
+
+It is possible to read a series of images from a single source file by
+repeating the jpeg_read_header() ... jpeg_finish_decompress() sequence,
+without releasing/recreating the JPEG object or the data source module.
+(If you did reinitialize, any partial bufferload left in the data source
+buffer at the end of one image would be discarded, causing you to lose the
+start of the next image.) When you use this method, stored tables are
+automatically carried forward, so some of the images can be abbreviated images
+that depend on tables from earlier images.
+
+If you intend to write a series of images into a single destination file,
+you might want to make a specialized data destination module that doesn't
+flush the output buffer at term_destination() time. This would speed things
+up by some trifling amount. Of course, you'd need to remember to flush the
+buffer after the last image. You can make the later images be abbreviated
+ones by passing FALSE to jpeg_start_compress().
+
+
+Special markers
+---------------
+
+Some applications may need to insert or extract special data in the JPEG
+datastream. The JPEG standard provides marker types "COM" (comment) and
+"APP0" through "APP15" (application) to hold application-specific data.
+Unfortunately, the use of these markers is not specified by the standard.
+COM markers are fairly widely used to hold user-supplied text. The JFIF file
+format spec uses APP0 markers with specified initial strings to hold certain
+data. Adobe applications use APP14 markers beginning with the string "Adobe"
+for miscellaneous data. Other APPn markers are rarely seen, but might
+contain almost anything.
+
+If you wish to store user-supplied text, we recommend you use COM markers
+and place readable 7-bit ASCII text in them. Newline conventions are not
+standardized --- expect to find LF (Unix style), CR/LF (DOS style), or CR
+(Mac style). A robust COM reader should be able to cope with random binary
+garbage, including nulls, since some applications generate COM markers
+containing non-ASCII junk. (But yours should not be one of them.)
+
+For program-supplied data, use an APPn marker, and be sure to begin it with an
+identifying string so that you can tell whether the marker is actually yours.
+It's probably best to avoid using APP0 or APP14 for any private markers.
+(NOTE: the upcoming SPIFF standard will use APP8 markers; we recommend you
+not use APP8 markers for any private purposes, either.)
+
+Keep in mind that at most 65533 bytes can be put into one marker, but you
+can have as many markers as you like.
+
+By default, the IJG compression library will write a JFIF APP0 marker if the
+selected JPEG colorspace is grayscale or YCbCr, or an Adobe APP14 marker if
+the selected colorspace is RGB, CMYK, or YCCK. You can disable this, but
+we don't recommend it. The decompression library will recognize JFIF and
+Adobe markers and will set the JPEG colorspace properly when one is found.
+
+
+You can write special markers immediately following the datastream header by
+calling jpeg_write_marker() after jpeg_start_compress() and before the first
+call to jpeg_write_scanlines(). When you do this, the markers appear after
+the SOI and the JFIF APP0 and Adobe APP14 markers (if written), but before
+all else. Specify the marker type parameter as "JPEG_COM" for COM or
+"JPEG_APP0 + n" for APPn. (Actually, jpeg_write_marker will let you write
+any marker type, but we don't recommend writing any other kinds of marker.)
+For example, to write a user comment string pointed to by comment_text:
+ jpeg_write_marker(cinfo, JPEG_COM, comment_text, strlen(comment_text));
+
+If it's not convenient to store all the marker data in memory at once,
+you can instead call jpeg_write_m_header() followed by multiple calls to
+jpeg_write_m_byte(). If you do it this way, it's your responsibility to
+call jpeg_write_m_byte() exactly the number of times given in the length
+parameter to jpeg_write_m_header(). (This method lets you empty the
+output buffer partway through a marker, which might be important when
+using a suspending data destination module. In any case, if you are using
+a suspending destination, you should flush its buffer after inserting
+any special markers. See "I/O suspension".)
+
+Or, if you prefer to synthesize the marker byte sequence yourself,
+you can just cram it straight into the data destination module.
+
+If you are writing JFIF 1.02 extension markers (thumbnail images), don't
+forget to set cinfo.JFIF_minor_version = 2 so that the encoder will write the
+correct JFIF version number in the JFIF header marker. The library's default
+is to write version 1.01, but that's wrong if you insert any 1.02 extension
+markers. (We could probably get away with just defaulting to 1.02, but there
+used to be broken decoders that would complain about unknown minor version
+numbers. To reduce compatibility risks it's safest not to write 1.02 unless
+you are actually using 1.02 extensions.)
+
+
+When reading, two methods of handling special markers are available:
+1. You can ask the library to save the contents of COM and/or APPn markers
+into memory, and then examine them at your leisure afterwards.
+2. You can supply your own routine to process COM and/or APPn markers
+on-the-fly as they are read.
+The first method is simpler to use, especially if you are using a suspending
+data source; writing a marker processor that copes with input suspension is
+not easy (consider what happens if the marker is longer than your available
+input buffer). However, the second method conserves memory since the marker
+data need not be kept around after it's been processed.
+
+For either method, you'd normally set up marker handling after creating a
+decompression object and before calling jpeg_read_header(), because the
+markers of interest will typically be near the head of the file and so will
+be scanned by jpeg_read_header. Once you've established a marker handling
+method, it will be used for the life of that decompression object
+(potentially many datastreams), unless you change it. Marker handling is
+determined separately for COM markers and for each APPn marker code.
+
+
+To save the contents of special markers in memory, call
+ jpeg_save_markers(cinfo, marker_code, length_limit)
+where marker_code is the marker type to save, JPEG_COM or JPEG_APP0+n.
+(To arrange to save all the special marker types, you need to call this
+routine 17 times, for COM and APP0-APP15.) If the incoming marker is longer
+than length_limit data bytes, only length_limit bytes will be saved; this
+parameter allows you to avoid chewing up memory when you only need to see the
+first few bytes of a potentially large marker. If you want to save all the
+data, set length_limit to 0xFFFF; that is enough since marker lengths are only
+16 bits. As a special case, setting length_limit to 0 prevents that marker
+type from being saved at all. (That is the default behavior, in fact.)
+
+After jpeg_read_header() completes, you can examine the special markers by
+following the cinfo->marker_list pointer chain. All the special markers in
+the file appear in this list, in order of their occurrence in the file (but
+omitting any markers of types you didn't ask for). Both the original data
+length and the saved data length are recorded for each list entry; the latter
+will not exceed length_limit for the particular marker type. Note that these
+lengths exclude the marker length word, whereas the stored representation
+within the JPEG file includes it. (Hence the maximum data length is really
+only 65533.)
+
+It is possible that additional special markers appear in the file beyond the
+SOS marker at which jpeg_read_header stops; if so, the marker list will be
+extended during reading of the rest of the file. This is not expected to be
+common, however. If you are short on memory you may want to reset the length
+limit to zero for all marker types after finishing jpeg_read_header, to
+ensure that the max_memory_to_use setting cannot be exceeded due to addition
+of later markers.
+
+The marker list remains stored until you call jpeg_finish_decompress or
+jpeg_abort, at which point the memory is freed and the list is set to empty.
+(jpeg_destroy also releases the storage, of course.)
+
+Note that the library is internally interested in APP0 and APP14 markers;
+if you try to set a small nonzero length limit on these types, the library
+will silently force the length up to the minimum it wants. (But you can set
+a zero length limit to prevent them from being saved at all.) Also, in a
+16-bit environment, the maximum length limit may be constrained to less than
+65533 by malloc() limitations. It is therefore best not to assume that the
+effective length limit is exactly what you set it to be.
+
+
+If you want to supply your own marker-reading routine, you do it by calling
+jpeg_set_marker_processor(). A marker processor routine must have the
+signature
+ boolean jpeg_marker_parser_method (j_decompress_ptr cinfo)
+Although the marker code is not explicitly passed, the routine can find it
+in cinfo->unread_marker. At the time of call, the marker proper has been
+read from the data source module. The processor routine is responsible for
+reading the marker length word and the remaining parameter bytes, if any.
+Return TRUE to indicate success. (FALSE should be returned only if you are
+using a suspending data source and it tells you to suspend. See the standard
+marker processors in jdmarker.c for appropriate coding methods if you need to
+use a suspending data source.)
+
+If you override the default APP0 or APP14 processors, it is up to you to
+recognize JFIF and Adobe markers if you want colorspace recognition to occur
+properly. We recommend copying and extending the default processors if you
+want to do that. (A better idea is to save these marker types for later
+examination by calling jpeg_save_markers(); that method doesn't interfere
+with the library's own processing of these markers.)
+
+jpeg_set_marker_processor() and jpeg_save_markers() are mutually exclusive
+--- if you call one it overrides any previous call to the other, for the
+particular marker type specified.
+
+A simple example of an external COM processor can be found in djpeg.c.
+Also, see jpegtran.c for an example of using jpeg_save_markers.
+
+
+Raw (downsampled) image data
+----------------------------
+
+Some applications need to supply already-downsampled image data to the JPEG
+compressor, or to receive raw downsampled data from the decompressor. The
+library supports this requirement by allowing the application to write or
+read raw data, bypassing the normal preprocessing or postprocessing steps.
+The interface is different from the standard one and is somewhat harder to
+use. If your interest is merely in bypassing color conversion, we recommend
+that you use the standard interface and simply set jpeg_color_space =
+in_color_space (or jpeg_color_space = out_color_space for decompression).
+The mechanism described in this section is necessary only to supply or
+receive downsampled image data, in which not all components have the same
+dimensions.
+
+
+To compress raw data, you must supply the data in the colorspace to be used
+in the JPEG file (please read the earlier section on Special color spaces)
+and downsampled to the sampling factors specified in the JPEG parameters.
+You must supply the data in the format used internally by the JPEG library,
+namely a JSAMPIMAGE array. This is an array of pointers to two-dimensional
+arrays, each of type JSAMPARRAY. Each 2-D array holds the values for one
+color component. This structure is necessary since the components are of
+different sizes. If the image dimensions are not a multiple of the MCU size,
+you must also pad the data correctly (usually, this is done by replicating
+the last column and/or row). The data must be padded to a multiple of a DCT
+block in each component: that is, each downsampled row must contain a
+multiple of 8 valid samples, and there must be a multiple of 8 sample rows
+for each component. (For applications such as conversion of digital TV
+images, the standard image size is usually a multiple of the DCT block size,
+so that no padding need actually be done.)
+
+The procedure for compression of raw data is basically the same as normal
+compression, except that you call jpeg_write_raw_data() in place of
+jpeg_write_scanlines(). Before calling jpeg_start_compress(), you must do
+the following:
+ * Set cinfo->raw_data_in to TRUE. (It is set FALSE by jpeg_set_defaults().)
+ This notifies the library that you will be supplying raw data.
+ * Ensure jpeg_color_space is correct --- an explicit jpeg_set_colorspace()
+ call is a good idea. Note that since color conversion is bypassed,
+ in_color_space is ignored, except that jpeg_set_defaults() uses it to
+ choose the default jpeg_color_space setting.
+ * Ensure the sampling factors, cinfo->comp_info[i].h_samp_factor and
+ cinfo->comp_info[i].v_samp_factor, are correct. Since these indicate the
+ dimensions of the data you are supplying, it's wise to set them
+ explicitly, rather than assuming the library's defaults are what you want.
+
+To pass raw data to the library, call jpeg_write_raw_data() in place of
+jpeg_write_scanlines(). The two routines work similarly except that
+jpeg_write_raw_data takes a JSAMPIMAGE data array rather than JSAMPARRAY.
+The scanlines count passed to and returned from jpeg_write_raw_data is
+measured in terms of the component with the largest v_samp_factor.
+
+jpeg_write_raw_data() processes one MCU row per call, which is to say
+v_samp_factor*DCTSIZE sample rows of each component. The passed num_lines
+value must be at least max_v_samp_factor*DCTSIZE, and the return value will
+be exactly that amount (or possibly some multiple of that amount, in future
+library versions). This is true even on the last call at the bottom of the
+image; don't forget to pad your data as necessary.
+
+The required dimensions of the supplied data can be computed for each
+component as
+ cinfo->comp_info[i].width_in_blocks*DCTSIZE samples per row
+ cinfo->comp_info[i].height_in_blocks*DCTSIZE rows in image
+after jpeg_start_compress() has initialized those fields. If the valid data
+is smaller than this, it must be padded appropriately. For some sampling
+factors and image sizes, additional dummy DCT blocks are inserted to make
+the image a multiple of the MCU dimensions. The library creates such dummy
+blocks itself; it does not read them from your supplied data. Therefore you
+need never pad by more than DCTSIZE samples. An example may help here.
+Assume 2h2v downsampling of YCbCr data, that is
+ cinfo->comp_info[0].h_samp_factor = 2 for Y
+ cinfo->comp_info[0].v_samp_factor = 2
+ cinfo->comp_info[1].h_samp_factor = 1 for Cb
+ cinfo->comp_info[1].v_samp_factor = 1
+ cinfo->comp_info[2].h_samp_factor = 1 for Cr
+ cinfo->comp_info[2].v_samp_factor = 1
+and suppose that the nominal image dimensions (cinfo->image_width and
+cinfo->image_height) are 101x101 pixels. Then jpeg_start_compress() will
+compute downsampled_width = 101 and width_in_blocks = 13 for Y,
+downsampled_width = 51 and width_in_blocks = 7 for Cb and Cr (and the same
+for the height fields). You must pad the Y data to at least 13*8 = 104
+columns and rows, the Cb/Cr data to at least 7*8 = 56 columns and rows. The
+MCU height is max_v_samp_factor = 2 DCT rows so you must pass at least 16
+scanlines on each call to jpeg_write_raw_data(), which is to say 16 actual
+sample rows of Y and 8 each of Cb and Cr. A total of 7 MCU rows are needed,
+so you must pass a total of 7*16 = 112 "scanlines". The last DCT block row
+of Y data is dummy, so it doesn't matter what you pass for it in the data
+arrays, but the scanlines count must total up to 112 so that all of the Cb
+and Cr data gets passed.
+
+Output suspension is supported with raw-data compression: if the data
+destination module suspends, jpeg_write_raw_data() will return 0.
+In this case the same data rows must be passed again on the next call.
+
+
+Decompression with raw data output implies bypassing all postprocessing:
+you cannot ask for rescaling or color quantization, for instance. More
+seriously, you must deal with the color space and sampling factors present in
+the incoming file. If your application only handles, say, 2h1v YCbCr data,
+you must check for and fail on other color spaces or other sampling factors.
+The library will not convert to a different color space for you.
+
+To obtain raw data output, set cinfo->raw_data_out = TRUE before
+jpeg_start_decompress() (it is set FALSE by jpeg_read_header()). Be sure to
+verify that the color space and sampling factors are ones you can handle.
+Then call jpeg_read_raw_data() in place of jpeg_read_scanlines(). The
+decompression process is otherwise the same as usual.
+
+jpeg_read_raw_data() returns one MCU row per call, and thus you must pass a
+buffer of at least max_v_samp_factor*DCTSIZE scanlines (scanline counting is
+the same as for raw-data compression). The buffer you pass must be large
+enough to hold the actual data plus padding to DCT-block boundaries. As with
+compression, any entirely dummy DCT blocks are not processed so you need not
+allocate space for them, but the total scanline count includes them. The
+above example of computing buffer dimensions for raw-data compression is
+equally valid for decompression.
+
+Input suspension is supported with raw-data decompression: if the data source
+module suspends, jpeg_read_raw_data() will return 0. You can also use
+buffered-image mode to read raw data in multiple passes.
+
+
+Really raw data: DCT coefficients
+---------------------------------
+
+It is possible to read or write the contents of a JPEG file as raw DCT
+coefficients. This facility is mainly intended for use in lossless
+transcoding between different JPEG file formats. Other possible applications
+include lossless cropping of a JPEG image, lossless reassembly of a
+multi-strip or multi-tile TIFF/JPEG file into a single JPEG datastream, etc.
+
+To read the contents of a JPEG file as DCT coefficients, open the file and do
+jpeg_read_header() as usual. But instead of calling jpeg_start_decompress()
+and jpeg_read_scanlines(), call jpeg_read_coefficients(). This will read the
+entire image into a set of virtual coefficient-block arrays, one array per
+component. The return value is a pointer to an array of virtual-array
+descriptors. Each virtual array can be accessed directly using the JPEG
+memory manager's access_virt_barray method (see Memory management, below,
+and also read structure.doc's discussion of virtual array handling). Or,
+for simple transcoding to a different JPEG file format, the array list can
+just be handed directly to jpeg_write_coefficients().
+
+Each block in the block arrays contains quantized coefficient values in
+normal array order (not JPEG zigzag order). The block arrays contain only
+DCT blocks containing real data; any entirely-dummy blocks added to fill out
+interleaved MCUs at the right or bottom edges of the image are discarded
+during reading and are not stored in the block arrays. (The size of each
+block array can be determined from the width_in_blocks and height_in_blocks
+fields of the component's comp_info entry.) This is also the data format
+expected by jpeg_write_coefficients().
+
+When you are done using the virtual arrays, call jpeg_finish_decompress()
+to release the array storage and return the decompression object to an idle
+state; or just call jpeg_destroy() if you don't need to reuse the object.
+
+If you use a suspending data source, jpeg_read_coefficients() will return
+NULL if it is forced to suspend; a non-NULL return value indicates successful
+completion. You need not test for a NULL return value when using a
+non-suspending data source.
+
+It is also possible to call jpeg_read_coefficients() to obtain access to the
+decoder's coefficient arrays during a normal decode cycle in buffered-image
+mode. This frammish might be useful for progressively displaying an incoming
+image and then re-encoding it without loss. To do this, decode in buffered-
+image mode as discussed previously, then call jpeg_read_coefficients() after
+the last jpeg_finish_output() call. The arrays will be available for your use
+until you call jpeg_finish_decompress().
+
+
+To write the contents of a JPEG file as DCT coefficients, you must provide
+the DCT coefficients stored in virtual block arrays. You can either pass
+block arrays read from an input JPEG file by jpeg_read_coefficients(), or
+allocate virtual arrays from the JPEG compression object and fill them
+yourself. In either case, jpeg_write_coefficients() is substituted for
+jpeg_start_compress() and jpeg_write_scanlines(). Thus the sequence is
+ * Create compression object
+ * Set all compression parameters as necessary
+ * Request virtual arrays if needed
+ * jpeg_write_coefficients()
+ * jpeg_finish_compress()
+ * Destroy or re-use compression object
+jpeg_write_coefficients() is passed a pointer to an array of virtual block
+array descriptors; the number of arrays is equal to cinfo.num_components.
+
+The virtual arrays need only have been requested, not realized, before
+jpeg_write_coefficients() is called. A side-effect of
+jpeg_write_coefficients() is to realize any virtual arrays that have been
+requested from the compression object's memory manager. Thus, when obtaining
+the virtual arrays from the compression object, you should fill the arrays
+after calling jpeg_write_coefficients(). The data is actually written out
+when you call jpeg_finish_compress(); jpeg_write_coefficients() only writes
+the file header.
+
+When writing raw DCT coefficients, it is crucial that the JPEG quantization
+tables and sampling factors match the way the data was encoded, or the
+resulting file will be invalid. For transcoding from an existing JPEG file,
+we recommend using jpeg_copy_critical_parameters(). This routine initializes
+all the compression parameters to default values (like jpeg_set_defaults()),
+then copies the critical information from a source decompression object.
+The decompression object should have just been used to read the entire
+JPEG input file --- that is, it should be awaiting jpeg_finish_decompress().
+
+jpeg_write_coefficients() marks all tables stored in the compression object
+as needing to be written to the output file (thus, it acts like
+jpeg_start_compress(cinfo, TRUE)). This is for safety's sake, to avoid
+emitting abbreviated JPEG files by accident. If you really want to emit an
+abbreviated JPEG file, call jpeg_suppress_tables(), or set the tables'
+individual sent_table flags, between calling jpeg_write_coefficients() and
+jpeg_finish_compress().
+
+
+Progress monitoring
+-------------------
+
+Some applications may need to regain control from the JPEG library every so
+often. The typical use of this feature is to produce a percent-done bar or
+other progress display. (For a simple example, see cjpeg.c or djpeg.c.)
+Although you do get control back frequently during the data-transferring pass
+(the jpeg_read_scanlines or jpeg_write_scanlines loop), any additional passes
+will occur inside jpeg_finish_compress or jpeg_start_decompress; those
+routines may take a long time to execute, and you don't get control back
+until they are done.
+
+You can define a progress-monitor routine which will be called periodically
+by the library. No guarantees are made about how often this call will occur,
+so we don't recommend you use it for mouse tracking or anything like that.
+At present, a call will occur once per MCU row, scanline, or sample row
+group, whichever unit is convenient for the current processing mode; so the
+wider the image, the longer the time between calls. During the data
+transferring pass, only one call occurs per call of jpeg_read_scanlines or
+jpeg_write_scanlines, so don't pass a large number of scanlines at once if
+you want fine resolution in the progress count. (If you really need to use
+the callback mechanism for time-critical tasks like mouse tracking, you could
+insert additional calls inside some of the library's inner loops.)
+
+To establish a progress-monitor callback, create a struct jpeg_progress_mgr,
+fill in its progress_monitor field with a pointer to your callback routine,
+and set cinfo->progress to point to the struct. The callback will be called
+whenever cinfo->progress is non-NULL. (This pointer is set to NULL by
+jpeg_create_compress or jpeg_create_decompress; the library will not change
+it thereafter. So if you allocate dynamic storage for the progress struct,
+make sure it will live as long as the JPEG object does. Allocating from the
+JPEG memory manager with lifetime JPOOL_PERMANENT will work nicely.) You
+can use the same callback routine for both compression and decompression.
+
+The jpeg_progress_mgr struct contains four fields which are set by the library:
+ long pass_counter; /* work units completed in this pass */
+ long pass_limit; /* total number of work units in this pass */
+ int completed_passes; /* passes completed so far */
+ int total_passes; /* total number of passes expected */
+During any one pass, pass_counter increases from 0 up to (not including)
+pass_limit; the step size is usually but not necessarily 1. The pass_limit
+value may change from one pass to another. The expected total number of
+passes is in total_passes, and the number of passes already completed is in
+completed_passes. Thus the fraction of work completed may be estimated as
+ completed_passes + (pass_counter/pass_limit)
+ --------------------------------------------
+ total_passes
+ignoring the fact that the passes may not be equal amounts of work.
+
+When decompressing, pass_limit can even change within a pass, because it
+depends on the number of scans in the JPEG file, which isn't always known in
+advance. The computed fraction-of-work-done may jump suddenly (if the library
+discovers it has overestimated the number of scans) or even decrease (in the
+opposite case). It is not wise to put great faith in the work estimate.
+
+When using the decompressor's buffered-image mode, the progress monitor work
+estimate is likely to be completely unhelpful, because the library has no way
+to know how many output passes will be demanded of it. Currently, the library
+sets total_passes based on the assumption that there will be one more output
+pass if the input file end hasn't yet been read (jpeg_input_complete() isn't
+TRUE), but no more output passes if the file end has been reached when the
+output pass is started. This means that total_passes will rise as additional
+output passes are requested. If you have a way of determining the input file
+size, estimating progress based on the fraction of the file that's been read
+will probably be more useful than using the library's value.
+
+
+Memory management
+-----------------
+
+This section covers some key facts about the JPEG library's built-in memory
+manager. For more info, please read structure.doc's section about the memory
+manager, and consult the source code if necessary.
+
+All memory and temporary file allocation within the library is done via the
+memory manager. If necessary, you can replace the "back end" of the memory
+manager to control allocation yourself (for example, if you don't want the
+library to use malloc() and free() for some reason).
+
+Some data is allocated "permanently" and will not be freed until the JPEG
+object is destroyed. Most data is allocated "per image" and is freed by
+jpeg_finish_compress, jpeg_finish_decompress, or jpeg_abort. You can call the
+memory manager yourself to allocate structures that will automatically be
+freed at these times. Typical code for this is
+ ptr = (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, size);
+Use JPOOL_PERMANENT to get storage that lasts as long as the JPEG object.
+Use alloc_large instead of alloc_small for anything bigger than a few Kbytes.
+There are also alloc_sarray and alloc_barray routines that automatically
+build 2-D sample or block arrays.
+
+The library's minimum space requirements to process an image depend on the
+image's width, but not on its height, because the library ordinarily works
+with "strip" buffers that are as wide as the image but just a few rows high.
+Some operating modes (eg, two-pass color quantization) require full-image
+buffers. Such buffers are treated as "virtual arrays": only the current strip
+need be in memory, and the rest can be swapped out to a temporary file.
+
+If you use the simplest memory manager back end (jmemnobs.c), then no
+temporary files are used; virtual arrays are simply malloc()'d. Images bigger
+than memory can be processed only if your system supports virtual memory.
+The other memory manager back ends support temporary files of various flavors
+and thus work in machines without virtual memory. They may also be useful on
+Unix machines if you need to process images that exceed available swap space.
+
+When using temporary files, the library will make the in-memory buffers for
+its virtual arrays just big enough to stay within a "maximum memory" setting.
+Your application can set this limit by setting cinfo->mem->max_memory_to_use
+after creating the JPEG object. (Of course, there is still a minimum size for
+the buffers, so the max-memory setting is effective only if it is bigger than
+the minimum space needed.) If you allocate any large structures yourself, you
+must allocate them before jpeg_start_compress() or jpeg_start_decompress() in
+order to have them counted against the max memory limit. Also keep in mind
+that space allocated with alloc_small() is ignored, on the assumption that
+it's too small to be worth worrying about; so a reasonable safety margin
+should be left when setting max_memory_to_use.
+
+If you use the jmemname.c or jmemdos.c memory manager back end, it is
+important to clean up the JPEG object properly to ensure that the temporary
+files get deleted. (This is especially crucial with jmemdos.c, where the
+"temporary files" may be extended-memory segments; if they are not freed,
+DOS will require a reboot to recover the memory.) Thus, with these memory
+managers, it's a good idea to provide a signal handler that will trap any
+early exit from your program. The handler should call either jpeg_abort()
+or jpeg_destroy() for any active JPEG objects. A handler is not needed with
+jmemnobs.c, and shouldn't be necessary with jmemansi.c or jmemmac.c either,
+since the C library is supposed to take care of deleting files made with
+tmpfile().
+
+
+Memory usage
+------------
+
+Working memory requirements while performing compression or decompression
+depend on image dimensions, image characteristics (such as colorspace and
+JPEG process), and operating mode (application-selected options).
+
+As of v6b, the decompressor requires:
+ 1. About 24K in more-or-less-fixed-size data. This varies a bit depending
+ on operating mode and image characteristics (particularly color vs.
+ grayscale), but it doesn't depend on image dimensions.
+ 2. Strip buffers (of size proportional to the image width) for IDCT and
+ upsampling results. The worst case for commonly used sampling factors
+ is about 34 bytes * width in pixels for a color image. A grayscale image
+ only needs about 8 bytes per pixel column.
+ 3. A full-image DCT coefficient buffer is needed to decode a multi-scan JPEG
+ file (including progressive JPEGs), or whenever you select buffered-image
+ mode. This takes 2 bytes/coefficient. At typical 2x2 sampling, that's
+ 3 bytes per pixel for a color image. Worst case (1x1 sampling) requires
+ 6 bytes/pixel. For grayscale, figure 2 bytes/pixel.
+ 4. To perform 2-pass color quantization, the decompressor also needs a
+ 128K color lookup table and a full-image pixel buffer (3 bytes/pixel).
+This does not count any memory allocated by the application, such as a
+buffer to hold the final output image.
+
+The above figures are valid for 8-bit JPEG data precision and a machine with
+32-bit ints. For 12-bit JPEG data, double the size of the strip buffers and
+quantization pixel buffer. The "fixed-size" data will be somewhat smaller
+with 16-bit ints, larger with 64-bit ints. Also, CMYK or other unusual
+color spaces will require different amounts of space.
+
+The full-image coefficient and pixel buffers, if needed at all, do not
+have to be fully RAM resident; you can have the library use temporary
+files instead when the total memory usage would exceed a limit you set.
+(But if your OS supports virtual memory, it's probably better to just use
+jmemnobs and let the OS do the swapping.)
+
+The compressor's memory requirements are similar, except that it has no need
+for color quantization. Also, it needs a full-image DCT coefficient buffer
+if Huffman-table optimization is asked for, even if progressive mode is not
+requested.
+
+If you need more detailed information about memory usage in a particular
+situation, you can enable the MEM_STATS code in jmemmgr.c.
+
+
+Library compile-time options
+----------------------------
+
+A number of compile-time options are available by modifying jmorecfg.h.
+
+The JPEG standard provides for both the baseline 8-bit DCT process and
+a 12-bit DCT process. The IJG code supports 12-bit lossy JPEG if you define
+BITS_IN_JSAMPLE as 12 rather than 8. Note that this causes JSAMPLE to be
+larger than a char, so it affects the surrounding application's image data.
+The sample applications cjpeg and djpeg can support 12-bit mode only for PPM
+and GIF file formats; you must disable the other file formats to compile a
+12-bit cjpeg or djpeg. (install.doc has more information about that.)
+At present, a 12-bit library can handle *only* 12-bit images, not both
+precisions. (If you need to include both 8- and 12-bit libraries in a single
+application, you could probably do it by defining NEED_SHORT_EXTERNAL_NAMES
+for just one of the copies. You'd have to access the 8-bit and 12-bit copies
+from separate application source files. This is untested ... if you try it,
+we'd like to hear whether it works!)
+
+Note that a 12-bit library always compresses in Huffman optimization mode,
+in order to generate valid Huffman tables. This is necessary because our
+default Huffman tables only cover 8-bit data. If you need to output 12-bit
+files in one pass, you'll have to supply suitable default Huffman tables.
+You may also want to supply your own DCT quantization tables; the existing
+quality-scaling code has been developed for 8-bit use, and probably doesn't
+generate especially good tables for 12-bit.
+
+The maximum number of components (color channels) in the image is determined
+by MAX_COMPONENTS. The JPEG standard allows up to 255 components, but we
+expect that few applications will need more than four or so.
+
+On machines with unusual data type sizes, you may be able to improve
+performance or reduce memory space by tweaking the various typedefs in
+jmorecfg.h. In particular, on some RISC CPUs, access to arrays of "short"s
+is quite slow; consider trading memory for speed by making JCOEF, INT16, and
+UINT16 be "int" or "unsigned int". UINT8 is also a candidate to become int.
+You probably don't want to make JSAMPLE be int unless you have lots of memory
+to burn.
+
+You can reduce the size of the library by compiling out various optional
+functions. To do this, undefine xxx_SUPPORTED symbols as necessary.
+
+You can also save a few K by not having text error messages in the library;
+the standard error message table occupies about 5Kb. This is particularly
+reasonable for embedded applications where there's no good way to display
+a message anyway. To do this, remove the creation of the message table
+(jpeg_std_message_table[]) from jerror.c, and alter format_message to do
+something reasonable without it. You could output the numeric value of the
+message code number, for example. If you do this, you can also save a couple
+more K by modifying the TRACEMSn() macros in jerror.h to expand to nothing;
+you don't need trace capability anyway, right?
+
+
+Portability considerations
+--------------------------
+
+The JPEG library has been written to be extremely portable; the sample
+applications cjpeg and djpeg are slightly less so. This section summarizes
+the design goals in this area. (If you encounter any bugs that cause the
+library to be less portable than is claimed here, we'd appreciate hearing
+about them.)
+
+The code works fine on ANSI C, C++, and pre-ANSI C compilers, using any of
+the popular system include file setups, and some not-so-popular ones too.
+See install.doc for configuration procedures.
+
+The code is not dependent on the exact sizes of the C data types. As
+distributed, we make the assumptions that
+ char is at least 8 bits wide
+ short is at least 16 bits wide
+ int is at least 16 bits wide
+ long is at least 32 bits wide
+(These are the minimum requirements of the ANSI C standard.) Wider types will
+work fine, although memory may be used inefficiently if char is much larger
+than 8 bits or short is much bigger than 16 bits. The code should work
+equally well with 16- or 32-bit ints.
+
+In a system where these assumptions are not met, you may be able to make the
+code work by modifying the typedefs in jmorecfg.h. However, you will probably
+have difficulty if int is less than 16 bits wide, since references to plain
+int abound in the code.
+
+char can be either signed or unsigned, although the code runs faster if an
+unsigned char type is available. If char is wider than 8 bits, you will need
+to redefine JOCTET and/or provide custom data source/destination managers so
+that JOCTET represents exactly 8 bits of data on external storage.
+
+The JPEG library proper does not assume ASCII representation of characters.
+But some of the image file I/O modules in cjpeg/djpeg do have ASCII
+dependencies in file-header manipulation; so does cjpeg's select_file_type()
+routine.
+
+The JPEG library does not rely heavily on the C library. In particular, C
+stdio is used only by the data source/destination modules and the error
+handler, all of which are application-replaceable. (cjpeg/djpeg are more
+heavily dependent on stdio.) malloc and free are called only from the memory
+manager "back end" module, so you can use a different memory allocator by
+replacing that one file.
+
+The code generally assumes that C names must be unique in the first 15
+characters. However, global function names can be made unique in the
+first 6 characters by defining NEED_SHORT_EXTERNAL_NAMES.
+
+More info about porting the code may be gleaned by reading jconfig.doc,
+jmorecfg.h, and jinclude.h.
+
+
+Notes for MS-DOS implementors
+-----------------------------
+
+The IJG code is designed to work efficiently in 80x86 "small" or "medium"
+memory models (i.e., data pointers are 16 bits unless explicitly declared
+"far"; code pointers can be either size). You may be able to use small
+model to compile cjpeg or djpeg by itself, but you will probably have to use
+medium model for any larger application. This won't make much difference in
+performance. You *will* take a noticeable performance hit if you use a
+large-data memory model (perhaps 10%-25%), and you should avoid "huge" model
+if at all possible.
+
+The JPEG library typically needs 2Kb-3Kb of stack space. It will also
+malloc about 20K-30K of near heap space while executing (and lots of far
+heap, but that doesn't count in this calculation). This figure will vary
+depending on selected operating mode, and to a lesser extent on image size.
+There is also about 5Kb-6Kb of constant data which will be allocated in the
+near data segment (about 4Kb of this is the error message table).
+Thus you have perhaps 20K available for other modules' static data and near
+heap space before you need to go to a larger memory model. The C library's
+static data will account for several K of this, but that still leaves a good
+deal for your needs. (If you are tight on space, you could reduce the sizes
+of the I/O buffers allocated by jdatasrc.c and jdatadst.c, say from 4K to
+1K. Another possibility is to move the error message table to far memory;
+this should be doable with only localized hacking on jerror.c.)
+
+About 2K of the near heap space is "permanent" memory that will not be
+released until you destroy the JPEG object. This is only an issue if you
+save a JPEG object between compression or decompression operations.
+
+Far data space may also be a tight resource when you are dealing with large
+images. The most memory-intensive case is decompression with two-pass color
+quantization, or single-pass quantization to an externally supplied color
+map. This requires a 128Kb color lookup table plus strip buffers amounting
+to about 40 bytes per column for typical sampling ratios (eg, about 25600
+bytes for a 640-pixel-wide image). You may not be able to process wide
+images if you have large data structures of your own.
+
+Of course, all of these concerns vanish if you use a 32-bit flat-memory-model
+compiler, such as DJGPP or Watcom C. We highly recommend flat model if you
+can use it; the JPEG library is significantly faster in flat model.
diff --git a/test/monniaux/jpeg-6b/ltconfig b/test/monniaux/jpeg-6b/ltconfig
new file mode 100755
index 00000000..2347e694
--- /dev/null
+++ b/test/monniaux/jpeg-6b/ltconfig
@@ -0,0 +1,1512 @@
+#! /bin/sh
+
+# ltconfig - Create a system-specific libtool.
+# Copyright (C) 1996-1998 Free Software Foundation, Inc.
+# Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
+#
+# This file is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# A lot of this script is taken from autoconf-2.10.
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+if test "${CDPATH+set}" = set; then CDPATH=; export CDPATH; fi
+
+echo=echo
+if test "X`($echo '\t') 2>/dev/null`" = 'X\t'; then :
+else
+ # The Solaris and AIX default echo program unquotes backslashes.
+ # This makes it impossible to quote backslashes using
+ # echo "$something" | sed 's/\\/\\\\/g'
+ # So, we emulate echo with printf '%s\n'
+ echo="printf %s\\n"
+ if test "X`($echo '\t') 2>/dev/null`" = 'X\t'; then :
+ else
+ # Oops. We have no working printf. Try to find a not-so-buggy echo.
+ echo=echo
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS="${IFS}:"
+ for dir in $PATH /usr/ucb; do
+ if test -f $dir/echo && test "X`$dir/echo '\t'`" = 'X\t'; then
+ echo="$dir/echo"
+ break
+ fi
+ done
+ IFS="$save_ifs"
+ fi
+fi
+
+# Sed substitution that helps us do robust quoting. It backslashifies
+# metacharacters that are still active within double-quoted strings.
+Xsed='sed -e s/^X//'
+sed_quote_subst='s/\([\\"\\`$\\\\]\)/\\\1/g'
+
+# Same as above, but do not quote variable references.
+double_quote_subst='s/\([\\"\\`\\\\]\)/\\\1/g'
+
+# The name of this program.
+progname=`$echo "X$0" | $Xsed -e 's%^.*/%%'`
+
+# Constants:
+PROGRAM=ltconfig
+PACKAGE=libtool
+VERSION=1.2
+ac_compile='${CC-cc} -c $CFLAGS $CPPFLAGS conftest.c 1>&5'
+ac_link='${CC-cc} -o conftest $CFLAGS $CPPFLAGS $LDFLAGS conftest.c $LIBS 1>&5'
+rm="rm -f"
+
+help="Try \`$progname --help' for more information."
+
+# Global variables:
+can_build_shared=yes
+enable_shared=yes
+# All known linkers require a `.a' archive for static linking.
+enable_static=yes
+ltmain=
+silent=
+srcdir=
+ac_config_guess=
+ac_config_sub=
+host=
+nonopt=
+verify_host=yes
+with_gcc=no
+with_gnu_ld=no
+
+old_AR="$AR"
+old_CC="$CC"
+old_CFLAGS="$CFLAGS"
+old_CPPFLAGS="$CPPFLAGS"
+old_LD="$LD"
+old_LN_S="$LN_S"
+old_NM="$NM"
+old_RANLIB="$RANLIB"
+
+# Parse the command line options.
+args=
+prev=
+for option
+do
+ case "$option" in
+ -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
+ *) optarg= ;;
+ esac
+
+ # If the previous option needs an argument, assign it.
+ if test -n "$prev"; then
+ eval "$prev=\$option"
+ prev=
+ continue
+ fi
+
+ case "$option" in
+ --help) cat <<EOM
+Usage: $progname [OPTION]... LTMAIN [HOST]
+
+Generate a system-specific libtool script.
+
+ --disable-shared do not build shared libraries
+ --disable-static do not build static libraries
+ --help display this help and exit
+ --no-verify do not verify that HOST is a valid host type
+ --quiet same as \`--silent'
+ --silent do not print informational messages
+ --srcdir=DIR find \`config.guess' in DIR
+ --version output version information and exit
+ --with-gcc assume that the GNU C compiler will be used
+ --with-gnu-ld assume that the C compiler uses the GNU linker
+
+LTMAIN is the \`ltmain.sh' shell script fragment that provides basic libtool
+functionality.
+
+HOST is the canonical host system name [default=guessed].
+EOM
+ exit 0
+ ;;
+
+ --disable-shared) enable_shared=no ;;
+
+ --disable-static) enable_static=no ;;
+
+ --quiet | --silent) silent=yes ;;
+
+ --srcdir) prev=srcdir ;;
+ --srcdir=*) srcdir="$optarg" ;;
+
+ --no-verify) verify_host=no ;;
+
+ --version) echo "$PROGRAM (GNU $PACKAGE) $VERSION"; exit 0 ;;
+
+ --with-gcc) with_gcc=yes ;;
+ --with-gnu-ld) with_gnu_ld=yes ;;
+
+ -*)
+ echo "$progname: unrecognized option \`$option'" 1>&2
+ echo "$help" 1>&2
+ exit 1
+ ;;
+
+ *)
+ if test -z "$ltmain"; then
+ ltmain="$option"
+ elif test -z "$host"; then
+# This generates an unnecessary warning for sparc-sun-solaris4.1.3_U1
+# if test -n "`echo $option| sed 's/[-a-z0-9.]//g'`"; then
+# echo "$progname: warning \`$option' is not a valid host type" 1>&2
+# fi
+ host="$option"
+ else
+ echo "$progname: too many arguments" 1>&2
+ echo "$help" 1>&2
+ exit 1
+ fi ;;
+ esac
+done
+
+if test -z "$ltmain"; then
+ echo "$progname: you must specify a LTMAIN file" 1>&2
+ echo "$help" 1>&2
+ exit 1
+fi
+
+if test -f "$ltmain"; then :
+else
+ echo "$progname: \`$ltmain' does not exist" 1>&2
+ echo "$help" 1>&2
+ exit 1
+fi
+
+# Quote any args containing shell metacharacters.
+ltconfig_args=
+for arg
+do
+ case "$arg" in
+ *" "*|*" "*|*[\[\]\~\#\$\^\&\*\(\)\{\}\\\|\;\<\>\?]*)
+ ltconfig_args="$ltconfig_args '$arg'" ;;
+ *) ltconfig_args="$ltconfig_args $arg" ;;
+ esac
+done
+
+# A relevant subset of AC_INIT.
+
+# File descriptor usage:
+# 0 standard input
+# 1 file creation
+# 2 errors and warnings
+# 3 some systems may open it to /dev/tty
+# 4 used on the Kubota Titan
+# 5 compiler messages saved in config.log
+# 6 checking for... messages and results
+if test "$silent" = yes; then
+ exec 6>/dev/null
+else
+ exec 6>&1
+fi
+exec 5>>./config.log
+
+# NLS nuisances.
+# Only set LANG and LC_ALL to C if already set.
+# These must not be set unconditionally because not all systems understand
+# e.g. LANG=C (notably SCO).
+if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
+if test "${LANG+set}" = set; then LANG=C; export LANG; fi
+
+if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
+ # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
+ if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
+ ac_n= ac_c='
+' ac_t=' '
+ else
+ ac_n=-n ac_c= ac_t=
+ fi
+else
+ ac_n= ac_c='\c' ac_t=
+fi
+
+if test -z "$srcdir"; then
+ # Assume the source directory is the same one as the path to ltmain.sh.
+ srcdir=`$echo "$ltmain" | $Xsed -e 's%/[^/]*$%%'`
+ test "$srcdir" = "$ltmain" && srcdir=.
+fi
+
+trap "$rm conftest*; exit 1" 1 2 15
+if test "$verify_host" = yes; then
+ # Check for config.guess and config.sub.
+ ac_aux_dir=
+ for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do
+ if test -f $ac_dir/config.guess; then
+ ac_aux_dir=$ac_dir
+ break
+ fi
+ done
+ if test -z "$ac_aux_dir"; then
+ echo "$progname: cannot find config.guess in $srcdir $srcdir/.. $srcdir/../.." 1>&2
+ echo "$help" 1>&2
+ exit 1
+ fi
+ ac_config_guess=$ac_aux_dir/config.guess
+ ac_config_sub=$ac_aux_dir/config.sub
+
+ # Make sure we can run config.sub.
+ if $ac_config_sub sun4 >/dev/null 2>&1; then :
+ else
+ echo "$progname: cannot run $ac_config_sub" 1>&2
+ echo "$help" 1>&2
+ exit 1
+ fi
+
+ echo $ac_n "checking host system type""... $ac_c" 1>&6
+
+ host_alias=$host
+ case "$host_alias" in
+ "")
+ if host_alias=`$ac_config_guess`; then :
+ else
+ echo "$progname: cannot guess host type; you must specify one" 1>&2
+ echo "$help" 1>&2
+ exit 1
+ fi ;;
+ esac
+ host=`$ac_config_sub $host_alias`
+ echo "$ac_t$host" 1>&6
+
+ # Make sure the host verified.
+ test -z "$host" && exit 1
+
+elif test -z "$host"; then
+ echo "$progname: you must specify a host type if you use \`--no-verify'" 1>&2
+ echo "$help" 1>&2
+ exit 1
+else
+ host_alias=$host
+fi
+
+# Transform linux* to *-*-linux-gnu*, to support old configure scripts.
+case "$host_os" in
+linux-gnu*) ;;
+linux*) host=`echo $host | sed 's/^\(.*-.*-linux\)\(.*\)$/\1-gnu\2/'`
+esac
+
+host_cpu=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
+host_vendor=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'`
+host_os=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
+
+case "$host_os" in
+aix3*)
+ # AIX sometimes has problems with the GCC collect2 program. For some
+ # reason, if we set the COLLECT_NAMES environment variable, the problems
+ # vanish in a puff of smoke.
+ if test "${COLLECT_NAMES+set}" != set; then
+ COLLECT_NAMES=
+ export COLLECT_NAMES
+ fi
+ ;;
+esac
+
+# Determine commands to create old-style static archives.
+old_archive_cmds='$AR cru $oldlib$oldobjs'
+old_postinstall_cmds='chmod 644 $oldlib'
+old_postuninstall_cmds=
+
+# Set a sane default for `AR'.
+test -z "$AR" && AR=ar
+
+# If RANLIB is not set, then run the test.
+if test "${RANLIB+set}" != "set"; then
+ result=no
+
+ echo $ac_n "checking for ranlib... $ac_c" 1>&6
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS="${IFS}:"
+ for dir in $PATH; do
+ test -z "$dir" && dir=.
+ if test -f $dir/ranlib; then
+ RANLIB="ranlib"
+ result="ranlib"
+ break
+ fi
+ done
+ IFS="$save_ifs"
+
+ echo "$ac_t$result" 1>&6
+fi
+
+if test -n "$RANLIB"; then
+ old_archive_cmds="$old_archive_cmds;\$RANLIB \$oldlib"
+ old_postinstall_cmds="\$RANLIB \$oldlib;$old_postinstall_cmds"
+fi
+
+# Check to see if we are using GCC.
+if test "$with_gcc" != yes || test -z "$CC"; then
+ # If CC is not set, then try to find GCC or a usable CC.
+ if test -z "$CC"; then
+ echo $ac_n "checking for gcc... $ac_c" 1>&6
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS="${IFS}:"
+ for dir in $PATH; do
+ IFS="$save_ifs"
+ test -z "$dir" && dir=.
+ if test -f $dir/gcc; then
+ CC="gcc"
+ break
+ fi
+ done
+ IFS="$save_ifs"
+
+ if test -n "$CC"; then
+ echo "$ac_t$CC" 1>&6
+ else
+ echo "$ac_t"no 1>&6
+ fi
+ fi
+
+ # Not "gcc", so try "cc", rejecting "/usr/ucb/cc".
+ if test -z "$CC"; then
+ echo $ac_n "checking for cc... $ac_c" 1>&6
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS="${IFS}:"
+ cc_rejected=no
+ for dir in $PATH; do
+ test -z "$dir" && dir=.
+ if test -f $dir/cc; then
+ if test "$dir/cc" = "/usr/ucb/cc"; then
+ cc_rejected=yes
+ continue
+ fi
+ CC="cc"
+ break
+ fi
+ done
+ IFS="$save_ifs"
+ if test $cc_rejected = yes; then
+ # We found a bogon in the path, so make sure we never use it.
+ set dummy $CC
+ shift
+ if test $# -gt 0; then
+ # We chose a different compiler from the bogus one.
+ # However, it has the same name, so the bogon will be chosen
+ # first if we set CC to just the name; use the full file name.
+ shift
+ set dummy "$dir/cc" "$@"
+ shift
+ CC="$@"
+ fi
+ fi
+
+ if test -n "$CC"; then
+ echo "$ac_t$CC" 1>&6
+ else
+ echo "$ac_t"no 1>&6
+ fi
+
+ if test -z "$CC"; then
+ echo "$progname: error: no acceptable cc found in \$PATH" 1>&2
+ exit 1
+ fi
+ fi
+
+ # Now see if the compiler is really GCC.
+ with_gcc=no
+ echo $ac_n "checking whether we are using GNU C... $ac_c" 1>&6
+ echo "$progname:424: checking whether we are using GNU C" >&5
+
+ $rm conftest.c
+ cat > conftest.c <<EOF
+#ifdef __GNUC__
+ yes;
+#endif
+EOF
+ if { ac_try='${CC-cc} -E conftest.c'; { (eval echo $progname:432: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }; } | egrep yes >/dev/null 2>&1; then
+ with_gcc=yes
+ fi
+ $rm conftest.c
+ echo "$ac_t$with_gcc" 1>&6
+fi
+
+# Allow CC to be a program name with arguments.
+set dummy $CC
+compiler="$2"
+
+echo $ac_n "checking for $compiler option to produce PIC... $ac_c" 1>&6
+pic_flag=
+special_shlib_compile_flags=
+wl=
+link_static_flag=
+no_builtin_flag=
+
+if test "$with_gcc" = yes; then
+ wl='-Wl,'
+ link_static_flag='-static'
+ no_builtin_flag=' -fno-builtin'
+
+ case "$host_os" in
+ aix3* | aix4* | irix5* | irix6* | osf3* | osf4*)
+ # PIC is the default for these OSes.
+ ;;
+ os2*)
+ # We can build DLLs from non-PIC.
+ ;;
+ amigaos*)
+ # FIXME: we need at least 68020 code to build shared libraries, but
+ # adding the `-m68020' flag to GCC prevents building anything better,
+ # like `-m68040'.
+ pic_flag='-m68020 -resident32 -malways-restore-a4'
+ ;;
+ *)
+ pic_flag='-fPIC'
+ ;;
+ esac
+else
+ # PORTME Check for PIC flags for the system compiler.
+ case "$host_os" in
+ aix3* | aix4*)
+ # All AIX code is PIC.
+ link_static_flag='-bnso -bI:/lib/syscalls.exp'
+ ;;
+
+ hpux9* | hpux10*)
+ # Is there a better link_static_flag that works with the bundled CC?
+ wl='-Wl,'
+ link_static_flag="${wl}-a ${wl}archive"
+ pic_flag='+Z'
+ ;;
+
+ irix5* | irix6*)
+ wl='-Wl,'
+ link_static_flag='-non_shared'
+ # PIC (with -KPIC) is the default.
+ ;;
+
+ os2*)
+ # We can build DLLs from non-PIC.
+ ;;
+
+ osf3* | osf4*)
+ # All OSF/1 code is PIC.
+ wl='-Wl,'
+ link_static_flag='-non_shared'
+ ;;
+
+ sco3.2v5*)
+ pic_flag='-Kpic'
+ link_static_flag='-dn'
+ special_shlib_compile_flags='-belf'
+ ;;
+
+ solaris2*)
+ pic_flag='-KPIC'
+ link_static_flag='-Bstatic'
+ wl='-Wl,'
+ ;;
+
+ sunos4*)
+ pic_flag='-PIC'
+ link_static_flag='-Bstatic'
+ wl='-Qoption ld '
+ ;;
+
+ sysv4.2uw2*)
+ pic_flag='-KPIC'
+ link_static_flag='-Bstatic'
+ wl='-Wl,'
+ ;;
+
+ uts4*)
+ pic_flag='-pic'
+ link_static_flag='-Bstatic'
+ ;;
+
+ *)
+ can_build_shared=no
+ ;;
+ esac
+fi
+
+if test -n "$pic_flag"; then
+ echo "$ac_t$pic_flag" 1>&6
+
+ # Check to make sure the pic_flag actually works.
+ echo $ac_n "checking if $compiler PIC flag $pic_flag works... $ac_c" 1>&6
+ $rm conftest*
+ echo > conftest.c
+ save_CFLAGS="$CFLAGS"
+ CFLAGS="$CFLAGS $pic_flag -DPIC"
+ echo "$progname:547: checking if $compiler PIC flag $pic_flag works" >&5
+ if { (eval echo $progname:548: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>conftest.err; } && test -s conftest.o; then
+ # Append any warnings to the config.log.
+ cat conftest.err 1>&5
+
+ # On HP-UX, both CC and GCC only warn that PIC is supported... then they
+ # create non-PIC objects. So, if there were any warnings, we assume that
+ # PIC is not supported.
+ if test -s conftest.err; then
+ echo "$ac_t"no 1>&6
+ can_build_shared=no
+ pic_flag=
+ else
+ echo "$ac_t"yes 1>&6
+ pic_flag=" $pic_flag"
+ fi
+ else
+ # Append any errors to the config.log.
+ cat conftest.err 1>&5
+ can_build_shared=no
+ pic_flag=
+ echo "$ac_t"no 1>&6
+ fi
+ CFLAGS="$save_CFLAGS"
+ $rm conftest*
+else
+ echo "$ac_t"none 1>&6
+fi
+
+# Check for any special shared library compilation flags.
+if test -n "$special_shlib_compile_flags"; then
+ echo "$progname: warning: \`$CC' requires \`$special_shlib_compile_flags' to build shared libraries" 1>&2
+ if echo "$old_CC $old_CFLAGS " | egrep -e "[ ]$special_shlib_compile_flags[ ]" >/dev/null; then :
+ else
+ echo "$progname: add \`$special_shlib_compile_flags' to the CC or CFLAGS env variable and reconfigure" 1>&2
+ can_build_shared=no
+ fi
+fi
+
+echo $ac_n "checking if $compiler static flag $link_static_flag works... $ac_c" 1>&6
+$rm conftest*
+echo 'main(){return(0);}' > conftest.c
+save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS $link_static_flag"
+echo "$progname:591: checking if $compiler static flag $link_static_flag works" >&5
+if { (eval echo $progname:592: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ echo "$ac_t$link_static_flag" 1>&6
+else
+ echo "$ac_t"none 1>&6
+ link_static_flag=
+fi
+LDFLAGS="$save_LDFLAGS"
+$rm conftest*
+
+if test -z "$LN_S"; then
+ # Check to see if we can use ln -s, or we need hard links.
+ echo $ac_n "checking whether ln -s works... $ac_c" 1>&6
+ $rm conftestdata
+ if ln -s X conftestdata 2>/dev/null; then
+ $rm conftestdata
+ LN_S="ln -s"
+ else
+ LN_S=ln
+ fi
+ if test "$LN_S" = "ln -s"; then
+ echo "$ac_t"yes 1>&6
+ else
+ echo "$ac_t"no 1>&6
+ fi
+fi
+
+# Make sure LD is an absolute path.
+if test -z "$LD"; then
+ ac_prog=ld
+ if test "$with_gcc" = yes; then
+ # Check if gcc -print-prog-name=ld gives a path.
+ echo $ac_n "checking for ld used by GCC... $ac_c" 1>&6
+ echo "$progname:624: checking for ld used by GCC" >&5
+ ac_prog=`($CC -print-prog-name=ld) 2>&5`
+ case "$ac_prog" in
+ # Accept absolute paths.
+ /* | [A-Za-z]:\\*)
+ test -z "$LD" && LD="$ac_prog"
+ ;;
+ "")
+ # If it fails, then pretend we are not using GCC.
+ ac_prog=ld
+ ;;
+ *)
+ # If it is relative, then search for the first ld in PATH.
+ with_gnu_ld=unknown
+ ;;
+ esac
+ elif test "$with_gnu_ld" = yes; then
+ echo $ac_n "checking for GNU ld... $ac_c" 1>&6
+ echo "$progname:642: checking for GNU ld" >&5
+ else
+ echo $ac_n "checking for non-GNU ld""... $ac_c" 1>&6
+ echo "$progname:645: checking for non-GNU ld" >&5
+ fi
+
+ if test -z "$LD"; then
+ IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
+ for ac_dir in $PATH; do
+ test -z "$ac_dir" && ac_dir=.
+ if test -f "$ac_dir/$ac_prog"; then
+ LD="$ac_dir/$ac_prog"
+ # Check to see if the program is GNU ld. I'd rather use --version,
+ # but apparently some GNU ld's only accept -v.
+ # Break only if it was the GNU/non-GNU ld that we prefer.
+ if "$LD" -v 2>&1 < /dev/null | egrep '(GNU|with BFD)' > /dev/null; then
+ test "$with_gnu_ld" != no && break
+ else
+ test "$with_gnu_ld" != yes && break
+ fi
+ fi
+ done
+ IFS="$ac_save_ifs"
+ fi
+
+ if test -n "$LD"; then
+ echo "$ac_t$LD" 1>&6
+ else
+ echo "$ac_t"no 1>&6
+ fi
+
+ if test -z "$LD"; then
+ echo "$progname: error: no acceptable ld found in \$PATH" 1>&2
+ exit 1
+ fi
+fi
+
+# Check to see if it really is or is not GNU ld.
+echo $ac_n "checking if the linker ($LD) is GNU ld... $ac_c" 1>&6
+# I'd rather use --version here, but apparently some GNU ld's only accept -v.
+if $LD -v 2>&1 </dev/null | egrep '(GNU|with BFD)' 1>&5; then
+ with_gnu_ld=yes
+else
+ with_gnu_ld=no
+fi
+echo "$ac_t$with_gnu_ld" 1>&6
+
+# See if the linker supports building shared libraries.
+echo $ac_n "checking whether the linker ($LD) supports shared libraries... $ac_c" 1>&6
+
+allow_undefined_flag=
+no_undefined_flag=
+archive_cmds=
+old_archive_from_new_cmds=
+export_dynamic_flag_spec=
+hardcode_libdir_flag_spec=
+hardcode_libdir_separator=
+hardcode_direct=no
+hardcode_minus_L=no
+hardcode_shlibpath_var=unsupported
+runpath_var=
+
+case "$host_os" in
+amigaos* | sunos4*)
+ # On these operating systems, we should treat GNU ld like the system ld.
+ gnu_ld_acts_native=yes
+ ;;
+*)
+ gnu_ld_acts_native=no
+ ;;
+esac
+
+ld_shlibs=yes
+if test "$with_gnu_ld" = yes && test "$gnu_ld_acts_native" != yes; then
+
+ # See if GNU ld supports shared libraries.
+ if $LD --help 2>&1 | egrep ': supported targets:.* elf' > /dev/null; then
+ archive_cmds='$CC -shared ${wl}-soname $wl$soname -o $lib$libobjs'
+ runpath_var=LD_RUN_PATH
+ ld_shlibs=yes
+ else
+ ld_shlibs=no
+ fi
+
+ if test "$ld_shlibs" = yes; then
+ hardcode_libdir_flag_spec='${wl}--rpath ${wl}$libdir'
+ export_dynamic_flag_spec='${wl}--export-dynamic'
+ fi
+else
+ # PORTME fill in a description of your system's linker (not GNU ld)
+ case "$host_os" in
+ aix3*)
+ allow_undefined_flag=unsupported
+ archive_cmds='$NM$libobjs | $global_symbol_pipe | sed '\''s/.* //'\'' > $lib.exp;$LD -o $objdir/$soname$libobjs -bE:$lib.exp -T512 -H512 -bM:SRE;$AR cru $lib $objdir/$soname'
+ # Note: this linker hardcodes the directories in LIBPATH if there
+ # are no directories specified by -L.
+ hardcode_minus_L=yes
+ if test "$with_gcc" = yes && test -z "$link_static_flag"; then
+ # Neither direct hardcoding nor static linking is supported with a
+ # broken collect2.
+ hardcode_direct=unsupported
+ fi
+ ;;
+
+ aix4*)
+ allow_undefined_flag=unsupported
+ archive_cmds='$NM$libobjs | $global_symbol_pipe | sed '\''s/.* //'\'' > $lib.exp;$CC -o $objdir/$soname$libobjs ${wl}-bE:$lib.exp ${wl}-bM:SRE ${wl}-bnoentry;$AR cru $lib $objdir/$soname'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ ;;
+
+ amigaos*)
+ archive_cmds='$rm $objdir/a2ixlibrary.data;$echo "#define NAME $libname" > $objdir/a2ixlibrary.data;$echo "#define LIBRARY_ID 1" >> $objdir/a2ixlibrary.data;$echo "#define VERSION $major" >> $objdir/a2ixlibrary.data;$echo "#define REVISION $revision" >> $objdir/a2ixlibrary.data;$AR cru $lib$libobjs;$RANLIB $lib;(cd $objdir && a2ixlibrary -32)'
+ hardcode_libdir_flag_spec='-L$libdir'
+ hardcode_minus_L=yes
+ ;;
+
+ # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor
+ # support. Future versions do this automatically, but an explicit c++rt0.o
+ # does not break anything, and helps significantly (at the cost of a little
+ # extra space).
+ freebsd2.2*)
+ archive_cmds='$LD -Bshareable -o $lib$libobjs /usr/lib/c++rt0.o'
+ hardcode_libdir_flag_spec='-R$libdir'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ # Unfortunately, older versions of FreeBSD 2 do not have this feature.
+ freebsd2*)
+ archive_cmds='$LD -Bshareable -o $lib$libobjs'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ # FreeBSD 3, at last, uses gcc -shared to do shared libraries.
+ freebsd3*)
+ archive_cmds='$CC -shared -o $lib$libobjs'
+ hardcode_libdir_flag_spec='-R$libdir'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ hpux9*)
+ archive_cmds='$rm $objdir/$soname;$LD -b +s +b $install_libdir -o $objdir/$soname$libobjs;mv $objdir/$soname $lib'
+ hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ export_dynamic_flag_spec='${wl}-E'
+ ;;
+
+ hpux10*)
+ archive_cmds='$LD -b +h $soname +s +b $install_libdir -o $lib$libobjs'
+ hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ export_dynamic_flag_spec='${wl}-E'
+ ;;
+
+ irix5* | irix6*)
+ archive_cmds='$LD -shared -o $lib -soname $soname -set_version $verstring$libobjs'
+ hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir'
+ ;;
+
+ netbsd*)
+ # Tested with NetBSD 1.2 ld
+ archive_cmds='$LD -Bshareable -o $lib$libobjs'
+ hardcode_libdir_flag_spec='-R$libdir'
+ hardcode_direct=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ openbsd*)
+ archive_cmds='$LD -Bshareable -o $lib$libobjs'
+ hardcode_libdir_flag_spec='-R$libdir'
+ hardcode_direct=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ os2*)
+ hardcode_libdir_flag_spec='-L$libdir'
+ hardcode_minus_L=yes
+ allow_undefined_flag=unsupported
+ archive_cmds='$echo "LIBRARY $libname INITINSTANCE" > $objdir/$libname.def;$echo "DESCRIPTION \"$libname\"" >> $objdir/$libname.def;$echo DATA >> $objdir/$libname.def;$echo " SINGLE NONSHARED" >> $objdir/$libname.def;$echo EXPORTS >> $objdir/$libname.def;emxexp$libobjs >> $objdir/$libname.def;$CC -Zdll -Zcrtdll -o $lib$libobjs $objdir/$libname.def'
+ old_archive_from_new_cmds='emximp -o $objdir/$libname.a $objdir/$libname.def'
+ ;;
+
+ osf3* | osf4*)
+ allow_undefined_flag=' -expect_unresolved \*'
+ archive_cmds='$LD -shared${allow_undefined_flag} -o $lib -soname $soname -set_version $verstring$libobjs$deplibs'
+ hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir'
+ hardcode_libdir_separator=:
+ ;;
+
+ sco3.2v5*)
+ archive_cmds='$LD -G -o $lib$libobjs'
+ hardcode_direct=yes
+ ;;
+
+ solaris2*)
+ no_undefined_flag=' -z text'
+ archive_cmds='$LD -G${allow_undefined_flag} -h $soname -o $lib$libobjs'
+ hardcode_libdir_flag_spec='-R$libdir'
+ hardcode_shlibpath_var=no
+
+ # Solaris 2 before 2.5 hardcodes -L paths.
+ case "$host_os" in
+ solaris2.[0-4]*)
+ hardcode_minus_L=yes
+ ;;
+ esac
+ ;;
+
+ sunos4*)
+ if test "$with_gcc" = yes; then
+ archive_cmds='$CC -shared -o $lib$libobjs'
+ else
+ archive_cmds='$LD -assert pure-text -Bstatic -o $lib$libobjs'
+ fi
+
+ if test "$with_gnu_ld" = yes; then
+ export_dynamic_flag_spec='${wl}-export-dynamic'
+ fi
+ hardcode_libdir_flag_spec='-L$libdir'
+ hardcode_direct=yes
+ hardcode_minus_L=yes
+ hardcode_shlibpath_var=no
+ ;;
+
+ uts4*)
+ archive_cmds='$LD -G -h $soname -o $lib$libobjs'
+ hardcode_libdir_flag_spec='-L$libdir'
+ hardcode_direct=no
+ hardcode_minus_L=no
+ hardcode_shlibpath_var=no
+ ;;
+
+ *)
+ ld_shlibs=no
+ can_build_shared=no
+ ;;
+ esac
+fi
+echo "$ac_t$ld_shlibs" 1>&6
+
+if test -z "$NM"; then
+ echo $ac_n "checking for BSD-compatible nm... $ac_c" 1>&6
+ case "$NM" in
+ /* | [A-Za-z]:\\*) ;; # Let the user override the test with a path.
+ *)
+ IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
+ for ac_dir in /usr/ucb /usr/ccs/bin $PATH /bin; do
+ test -z "$ac_dir" && ac_dir=.
+ if test -f $ac_dir/nm; then
+ # Check to see if the nm accepts a BSD-compat flag.
+ # Adding the `sed 1q' prevents false positives on HP-UX, which says:
+ # nm: unknown option "B" ignored
+ if ($ac_dir/nm -B /dev/null 2>&1 | sed '1q'; exit 0) | egrep /dev/null >/dev/null; then
+ NM="$ac_dir/nm -B"
+ elif ($ac_dir/nm -p /dev/null 2>&1 | sed '1q'; exit 0) | egrep /dev/null >/dev/null; then
+ NM="$ac_dir/nm -p"
+ else
+ NM="$ac_dir/nm"
+ fi
+ break
+ fi
+ done
+ IFS="$ac_save_ifs"
+ test -z "$NM" && NM=nm
+ ;;
+ esac
+ echo "$ac_t$NM" 1>&6
+fi
+
+# Check for command to grab the raw symbol name followed by C symbol from nm.
+echo $ac_n "checking command to parse $NM output... $ac_c" 1>&6
+
+# These are sane defaults that work on at least a few old systems.
+# [They come from Ultrix. What could be older than Ultrix?!! ;)]
+
+# Character class describing NM global symbol codes.
+symcode='[BCDEGRSTU]'
+
+# Regexp to match symbols that can be accessed directly from C.
+sympat='\([_A-Za-z][_A-Za-z0-9]*\)'
+
+# Transform the above into a raw symbol and a C symbol.
+symxfrm='\1 \1'
+
+# Define system-specific variables.
+case "$host_os" in
+aix*)
+ symcode='[BCDTU]'
+ ;;
+irix*)
+ # Cannot use undefined symbols on IRIX because inlined functions mess us up.
+ symcode='[BCDEGRST]'
+ ;;
+solaris2*)
+ symcode='[BDTU]'
+ ;;
+esac
+
+# If we're using GNU nm, then use its standard symbol codes.
+if $NM -V 2>&1 | egrep '(GNU|with BFD)' > /dev/null; then
+ symcode='[ABCDGISTUW]'
+fi
+
+# Write the raw and C identifiers.
+global_symbol_pipe="sed -n -e 's/^.* $symcode $sympat$/$symxfrm/p'"
+
+# Check to see that the pipe works correctly.
+pipe_works=no
+$rm conftest*
+cat > conftest.c <<EOF
+#ifdef __cplusplus
+extern "C" {
+#endif
+char nm_test_var;
+void nm_test_func(){}
+#ifdef __cplusplus
+}
+#endif
+main(){nm_test_var='a';nm_test_func();return(0);}
+EOF
+
+echo "$progname:971: checking if global_symbol_pipe works" >&5
+if { (eval echo $progname:972: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; } && test -s conftest.o; then
+ # Now try to grab the symbols.
+ nlist=conftest.nm
+ if { echo "$progname:975: eval \"$NM conftest.o | $global_symbol_pipe > $nlist\"" >&5; eval "$NM conftest.o | $global_symbol_pipe > $nlist 2>&5"; } && test -s "$nlist"; then
+
+ # Try sorting and uniquifying the output.
+ if sort "$nlist" | uniq > "$nlist"T; then
+ mv -f "$nlist"T "$nlist"
+ wcout=`wc "$nlist" 2>/dev/null`
+ count=`$echo "X$wcout" | $Xsed -e 's/^[ ]*\([0-9][0-9]*\).*$/\1/'`
+ (test "$count" -ge 0) 2>/dev/null || count=-1
+ else
+ rm -f "$nlist"T
+ count=-1
+ fi
+
+ # Make sure that we snagged all the symbols we need.
+ if egrep ' nm_test_var$' "$nlist" >/dev/null; then
+ if egrep ' nm_test_func$' "$nlist" >/dev/null; then
+ cat <<EOF > conftest.c
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+EOF
+ # Now generate the symbol file.
+ sed 's/^.* \(.*\)$/extern char \1;/' < "$nlist" >> conftest.c
+
+ cat <<EOF >> conftest.c
+#if defined (__STDC__) && __STDC__
+# define __ptr_t void *
+#else
+# define __ptr_t char *
+#endif
+
+/* The number of symbols in dld_preloaded_symbols, -1 if unsorted. */
+int dld_preloaded_symbol_count = $count;
+
+/* The mapping between symbol names and symbols. */
+struct {
+ char *name;
+ __ptr_t address;
+}
+dld_preloaded_symbols[] =
+{
+EOF
+ sed 's/^\(.*\) \(.*\)$/ {"\1", (__ptr_t) \&\2},/' < "$nlist" >> conftest.c
+ cat <<\EOF >> conftest.c
+ {0, (__ptr_t) 0}
+};
+
+#ifdef __cplusplus
+}
+#endif
+EOF
+ # Now try linking the two files.
+ mv conftest.o conftestm.o
+ save_LIBS="$LIBS"
+ save_CFLAGS="$CFLAGS"
+ LIBS='conftestm.o'
+ CFLAGS="$CFLAGS$no_builtin_flag"
+ if { (eval echo $progname:1033: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+ pipe_works=yes
+ else
+ echo "$progname: failed program was:" >&5
+ cat conftest.c >&5
+ fi
+ LIBS="$save_LIBS"
+ else
+ echo "cannot find nm_test_func in $nlist" >&5
+ fi
+ else
+ echo "cannot find nm_test_var in $nlist" >&5
+ fi
+ else
+ echo "cannot run $global_symbol_pipe" >&5
+ fi
+else
+ echo "$progname: failed program was:" >&5
+ cat conftest.c >&5
+fi
+$rm conftest*
+
+# Do not use the global_symbol_pipe unless it works.
+echo "$ac_t$pipe_works" 1>&6
+test "$pipe_works" = yes || global_symbol_pipe=
+
+# Check hardcoding attributes.
+echo $ac_n "checking how to hardcode library paths into programs... $ac_c" 1>&6
+hardcode_action=
+if test -n "$hardcode_libdir_flag_spec" || \
+ test -n "$runpath_var"; then
+
+ # We can hardcode non-existant directories.
+ if test "$hardcode_direct" != no && \
+ test "$hardcode_minus_L" != no && \
+ test "$hardcode_shlibpath_var" != no; then
+
+ # Linking always hardcodes the temporary library directory.
+ hardcode_action=relink
+ else
+ # We can link without hardcoding, and we can hardcode nonexisting dirs.
+ hardcode_action=immediate
+ fi
+elif test "$hardcode_direct" != yes && \
+ test "$hardcode_minus_L" != yes && \
+ test "$hardcode_shlibpath_var" != yes; then
+ # We cannot hardcode anything.
+ hardcode_action=unsupported
+else
+ # We can only hardcode existing directories.
+ hardcode_action=relink
+fi
+echo "$ac_t$hardcode_action" 1>&6
+test "$hardcode_action" = unsupported && can_build_shared=no
+
+
+reload_flag=
+reload_cmds='$LD$reload_flag -o $output$reload_objs'
+echo $ac_n "checking for $LD option to reload object files... $ac_c" 1>&6
+# PORTME Some linker may need a different reload flag.
+reload_flag='-r'
+echo "$ac_t$reload_flag"
+test -n "$reload_flag" && reload_flag=" $reload_flag"
+
+# PORTME Fill in your ld.so characteristics
+library_names_spec=
+libname_spec='lib$name'
+soname_spec=
+postinstall_cmds=
+postuninstall_cmds=
+finish_cmds=
+finish_eval=
+shlibpath_var=
+version_type=none
+dynamic_linker="$host_os ld.so"
+
+echo $ac_n "checking dynamic linker characteristics... $ac_c" 1>&6
+case "$host_os" in
+aix3* | aix4*)
+ version_type=linux
+ library_names_spec='${libname}${release}.so.$versuffix $libname.a'
+ shlibpath_var=LIBPATH
+
+ # AIX has no versioning support, so we append a major version to the name.
+ soname_spec='${libname}${release}.so.$major'
+ ;;
+
+amigaos*)
+ library_names_spec='$libname.ixlibrary $libname.a'
+ # Create ${libname}_ixlibrary.a entries in /sys/libs.
+ finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`$echo "X$lib" | $Xsed -e '\''s%^.*/\([^/]*\)\.ixlibrary$%\1%'\''`; test $rm /sys/libs/${libname}_ixlibrary.a; $show "(cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a)"; (cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a) || exit 1; done'
+ ;;
+
+freebsd2* | freebsd3*)
+ version_type=sunos
+ library_names_spec='${libname}${release}.so.$versuffix $libname.so'
+ finish_cmds='PATH="$PATH:/sbin" ldconfig -m $libdir'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+gnu*)
+ version_type=sunos
+ library_names_spec='${libname}${release}.so.$versuffix'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+hpux9* | hpux10*)
+ # Give a soname corresponding to the major version so that dld.sl refuses to
+ # link against other versions.
+ dynamic_linker="$host_os dld.sl"
+ version_type=sunos
+ shlibpath_var=SHLIB_PATH
+ library_names_spec='${libname}${release}.sl.$versuffix ${libname}${release}.sl.$major $libname.sl'
+ soname_spec='${libname}${release}.sl.$major'
+ # HP-UX runs *really* slowly unless shared libraries are mode 555.
+ postinstall_cmds='chmod 555 $lib'
+ ;;
+
+irix5* | irix6*)
+ version_type=osf
+ soname_spec='${libname}${release}.so'
+ library_names_spec='${libname}${release}.so.$versuffix $libname.so'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+# No shared lib support for Linux oldld, aout, or coff.
+linux-gnuoldld* | linux-gnuaout* | linux-gnucoff*)
+ dynamic_linker=no
+ ;;
+
+# This must be Linux ELF.
+linux-gnu*)
+ version_type=linux
+ library_names_spec='${libname}${release}.so.$versuffix ${libname}${release}.so.$major $libname.so'
+ soname_spec='${libname}${release}.so.$major'
+ finish_cmds='PATH="$PATH:/sbin" ldconfig -n $libdir'
+ shlibpath_var=LD_LIBRARY_PATH
+
+ if test -f /lib/ld.so.1; then
+ dynamic_linker='GNU ld.so'
+ else
+ # Only the GNU ld.so supports shared libraries on MkLinux.
+ case "$host_cpu" in
+ powerpc*) dynamic_linker=no ;;
+ *) dynamic_linker='Linux ld.so' ;;
+ esac
+ fi
+ ;;
+
+netbsd* | openbsd*)
+ version_type=sunos
+ library_names_spec='${libname}${release}.so.$versuffix'
+ finish_cmds='PATH="$PATH:/sbin" ldconfig -m $libdir'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+os2*)
+ libname_spec='$name'
+ library_names_spec='$libname.dll $libname.a'
+ dynamic_linker='OS/2 ld.exe'
+ shlibpath_var=LIBPATH
+ ;;
+
+osf3* | osf4*)
+ version_type=osf
+ soname_spec='${libname}${release}.so'
+ library_names_spec='${libname}${release}.so.$versuffix $libname.so'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+sco3.2v5*)
+ version_type=osf
+ soname_spec='${libname}${release}.so.$major'
+ library_names_spec='${libname}${release}.so.$versuffix ${libname}${release}.so.$major $libname.so'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+solaris2*)
+ version_type=linux
+ library_names_spec='${libname}${release}.so.$versuffix ${libname}${release}.so.$major $libname.so'
+ soname_spec='${libname}${release}.so.$major'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+sunos4*)
+ version_type=sunos
+ library_names_spec='${libname}${release}.so.$versuffix'
+ finish_cmds='PATH="$PATH:/usr/etc" ldconfig $libdir'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+sysv4.2uw2*)
+ version_type=linux
+ library_names_spec='${libname}${release}.so.$versuffix ${libname}${release}.so.$major $libname.so'
+ soname_spec='${libname}${release}.so.$major'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+uts4*)
+ version_type=linux
+ library_names_spec='${libname}${release}.so.$versuffix ${libname}${release}.so.$major $libname.so'
+ soname_spec='${libname}${release}.so.$major'
+ shlibpath_var=LD_LIBRARY_PATH
+ ;;
+
+*)
+ dynamic_linker=no
+ ;;
+esac
+echo "$ac_t$dynamic_linker"
+test "$dynamic_linker" = no && can_build_shared=no
+
+# Report the final consequences.
+echo "checking if libtool supports shared libraries... $can_build_shared" 1>&6
+
+echo $ac_n "checking whether to build shared libraries... $ac_c" 1>&6
+test "$can_build_shared" = "no" && enable_shared=no
+
+# On AIX, shared libraries and static libraries use the same namespace, and
+# are all built from PIC.
+case "$host_os" in
+aix*)
+ test "$enable_shared" = yes && enable_static=no
+ if test -n "$RANLIB"; then
+ archive_cmds="$archive_cmds;\$RANLIB \$lib"
+ postinstall_cmds='$RANLIB $lib'
+ fi
+ ;;
+esac
+
+echo "$ac_t$enable_shared" 1>&6
+
+# Make sure either enable_shared or enable_static is yes.
+test "$enable_shared" = yes || enable_static=yes
+
+echo "checking whether to build static libraries... $enable_static" 1>&6
+
+echo $ac_n "checking for objdir... $ac_c" 1>&6
+rm -f .libs 2>/dev/null
+mkdir .libs 2>/dev/null
+if test -d .libs; then
+ objdir=.libs
+else
+ # MS-DOS does not allow filenames that begin with a dot.
+ objdir=_libs
+fi
+rmdir .libs 2>/dev/null
+echo "$ac_t$objdir" 1>&6
+
+# Copy echo and quote the copy, instead of the original, because it is
+# used later.
+ltecho="$echo"
+
+# Now quote all the things that may contain metacharacters.
+for var in ltecho old_CC old_CFLAGS old_CPPFLAGS old_LD old_NM old_RANLIB \
+ old_LN_S AR CC LD LN_S NM reload_flag reload_cmds wl pic_flag \
+ link_static_flag no_builtin_flag export_dynamic_flag_spec \
+ libname_spec library_names_spec soname_spec RANLIB \
+ old_archive_cmds old_archive_from_new_cmds old_postinstall_cmds \
+ old_postuninstall_cmds archive_cmds postinstall_cmds postuninstall_cmds \
+ allow_undefined_flag no_undefined_flag \
+ finish_cmds finish_eval global_symbol_pipe \
+ hardcode_libdir_flag_spec hardcode_libdir_separator; do
+
+ case "$var" in
+ reload_cmds | old_archive_cmds | old_archive_from_new_cmds | \
+ old_postinstall_cmds | old_postuninstall_cmds | archive_cmds | \
+ postinstall_cmds | postuninstall_cmds | finish_cmds)
+ # Double-quote double-evaled strings.
+ eval "$var=\`\$echo \"X\$$var\" | \$Xsed -e \"\$double_quote_subst\" -e \"\$sed_quote_subst\"\`"
+ ;;
+ *)
+ eval "$var=\`\$echo \"X\$$var\" | \$Xsed -e \"\$sed_quote_subst\"\`"
+ ;;
+ esac
+done
+
+ofile=libtool
+trap "$rm $ofile; exit 1" 1 2 15
+echo creating $ofile
+$rm $ofile
+cat <<EOF > $ofile
+#! /bin/sh
+
+# libtool - Provide generalized library-building support services.
+# Generated automatically by $PROGRAM - GNU $PACKAGE $VERSION
+# NOTE: Changes made to this file will be lost: look at ltconfig or ltmain.sh.
+#
+# Copyright (C) 1996-1998 Free Software Foundation, Inc.
+# Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# This program was configured as follows,
+# on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
+#
+# CC="$old_CC" CFLAGS="$old_CFLAGS" CPPFLAGS="$old_CPPFLAGS" \\
+# LD="$old_LD" NM="$old_NM" RANLIB="$old_RANLIB" LN_S="$old_LN_S" \\
+# $0$ltconfig_args
+#
+# Compiler and other test output produced by $progname, useful for
+# debugging $progname, is in ./config.log if it exists.
+
+# Sed that helps us avoid accidentally triggering echo(1) options like -n.
+Xsed="sed -e s/^X//"
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+if test "\${CDPATH+set}" = set; then CDPATH=; export CDPATH; fi
+
+# An echo program that does not interpret backslashes.
+echo="$ltecho"
+
+# The version of $progname that generated this script.
+LTCONFIG_VERSION="$VERSION"
+
+# Shell to use when invoking shell scripts.
+SHELL=${CONFIG_SHELL-/bin/sh}
+
+# Whether or not to build libtool libraries.
+build_libtool_libs=$enable_shared
+
+# Whether or not to build old-style libraries.
+build_old_libs=$enable_static
+
+# The host system.
+host_alias="$host_alias"
+host="$host"
+
+# The archiver.
+AR="$AR"
+
+# The default C compiler.
+CC="$CC"
+
+# The linker used to build libraries.
+LD="$LD"
+
+# Whether we need hard or soft links.
+LN_S="$LN_S"
+
+# A BSD-compatible nm program.
+NM="$NM"
+
+# The name of the directory that contains temporary libtool files.
+objdir="$objdir"
+
+# How to create reloadable object files.
+reload_flag="$reload_flag"
+reload_cmds="$reload_cmds"
+
+# How to pass a linker flag through the compiler.
+wl="$wl"
+
+# Additional compiler flags for building library objects.
+pic_flag="$pic_flag"
+
+# Compiler flag to prevent dynamic linking.
+link_static_flag="$link_static_flag"
+
+# Compiler flag to turn off builtin functions.
+no_builtin_flag="$no_builtin_flag"
+
+# Compiler flag to allow reflexive dlopens.
+export_dynamic_flag_spec="$export_dynamic_flag_spec"
+
+# Library versioning type.
+version_type=$version_type
+
+# Format of library name prefix.
+libname_spec="$libname_spec"
+
+# List of archive names. First name is the real one, the rest are links.
+# The last name is the one that the linker finds with -lNAME.
+library_names_spec="$library_names_spec"
+
+# The coded name of the library, if different from the real name.
+soname_spec="$soname_spec"
+
+# Commands used to build and install an old-style archive.
+RANLIB="$RANLIB"
+old_archive_cmds="$old_archive_cmds"
+old_postinstall_cmds="$old_postinstall_cmds"
+old_postuninstall_cmds="$old_postuninstall_cmds"
+
+# Create an old-style archive from a shared archive.
+old_archive_from_new_cmds="$old_archive_from_new_cmds"
+
+# Commands used to build and install a shared archive.
+archive_cmds="$archive_cmds"
+postinstall_cmds="$postinstall_cmds"
+postuninstall_cmds="$postuninstall_cmds"
+
+# Flag that allows shared libraries with undefined symbols to be built.
+allow_undefined_flag="$allow_undefined_flag"
+
+# Flag that forces no undefined symbols.
+no_undefined_flag="$no_undefined_flag"
+
+# Commands used to finish a libtool library installation in a directory.
+finish_cmds="$finish_cmds"
+
+# Same as above, but a single script fragment to be evaled but not shown.
+finish_eval="$finish_eval"
+
+# Take the output of nm and produce a listing of raw symbols and C names.
+global_symbol_pipe="$global_symbol_pipe"
+
+# This is the shared library runtime path variable.
+runpath_var=$runpath_var
+
+# This is the shared library path variable.
+shlibpath_var=$shlibpath_var
+
+# How to hardcode a shared library path into an executable.
+hardcode_action=$hardcode_action
+
+# Flag to hardcode \$libdir into a binary during linking.
+# This must work even if \$libdir does not exist.
+hardcode_libdir_flag_spec="$hardcode_libdir_flag_spec"
+
+# Whether we need a single -rpath flag with a separated argument.
+hardcode_libdir_separator="$hardcode_libdir_separator"
+
+# Set to yes if using DIR/libNAME.so during linking hardcodes DIR into the
+# resulting binary.
+hardcode_direct=$hardcode_direct
+
+# Set to yes if using the -LDIR flag during linking hardcodes DIR into the
+# resulting binary.
+hardcode_minus_L=$hardcode_minus_L
+
+# Set to yes if using SHLIBPATH_VAR=DIR during linking hardcodes DIR into
+# the resulting binary.
+hardcode_shlibpath_var=$hardcode_shlibpath_var
+
+EOF
+
+case "$host_os" in
+aix3*)
+ cat <<\EOF >> $ofile
+# AIX sometimes has problems with the GCC collect2 program. For some
+# reason, if we set the COLLECT_NAMES environment variable, the problems
+# vanish in a puff of smoke.
+if test "${COLLECT_NAMES+set}" != set; then
+ COLLECT_NAMES=
+ export COLLECT_NAMES
+fi
+
+EOF
+ ;;
+esac
+
+# Append the ltmain.sh script.
+cat "$ltmain" >> $ofile || (rm -f $ofile; exit 1)
+
+chmod +x $ofile
+exit 0
+
+# Local Variables:
+# mode:shell-script
+# sh-indentation:2
+# End:
diff --git a/test/monniaux/jpeg-6b/ltmain.sh b/test/monniaux/jpeg-6b/ltmain.sh
new file mode 100644
index 00000000..e9350b3f
--- /dev/null
+++ b/test/monniaux/jpeg-6b/ltmain.sh
@@ -0,0 +1,2453 @@
+# ltmain.sh - Provide generalized library-building support services.
+# NOTE: Changing this file will not affect anything until you rerun ltconfig.
+#
+# Copyright (C) 1996-1998 Free Software Foundation, Inc.
+# Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# The name of this program.
+progname=`$echo "$0" | sed 's%^.*/%%'`
+modename="$progname"
+
+# Constants.
+PROGRAM=ltmain.sh
+PACKAGE=libtool
+VERSION=1.2
+
+default_mode=
+help="Try \`$progname --help' for more information."
+magic="%%%MAGIC variable%%%"
+mkdir="mkdir"
+mv="mv -f"
+rm="rm -f"
+
+# Sed substitution that helps us do robust quoting. It backslashifies
+# metacharacters that are still active within double-quoted strings.
+Xsed='sed -e s/^X//'
+sed_quote_subst='s/\([\\`\\"$\\\\]\)/\\\1/g'
+
+# NLS nuisances.
+# Only set LANG and LC_ALL to C if already set.
+# These must not be set unconditionally because not all systems understand
+# e.g. LANG=C (notably SCO).
+if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
+if test "${LANG+set}" = set; then LANG=C; export LANG; fi
+
+if test "$LTCONFIG_VERSION" != "$VERSION"; then
+ echo "$modename: ltconfig version \`$LTCONFIG_VERSION' does not match $PROGRAM version \`$VERSION'" 1>&2
+ echo "Fatal configuration error. See the $PACKAGE docs for more information." 1>&2
+ exit 1
+fi
+
+if test "$build_libtool_libs" != yes && test "$build_old_libs" != yes; then
+ echo "$modename: not configured to build any kind of library" 1>&2
+ echo "Fatal configuration error. See the $PACKAGE docs for more information." 1>&2
+ exit 1
+fi
+
+# Global variables.
+mode=$default_mode
+nonopt=
+prev=
+prevopt=
+run=
+show="$echo"
+show_help=
+execute_dlfiles=
+
+# Parse our command line options once, thoroughly.
+while test $# -gt 0
+do
+ arg="$1"
+ shift
+
+ case "$arg" in
+ -*=*) optarg=`$echo "X$arg" | $Xsed -e 's/[-_a-zA-Z0-9]*=//'` ;;
+ *) optarg= ;;
+ esac
+
+ # If the previous option needs an argument, assign it.
+ if test -n "$prev"; then
+ case "$prev" in
+ execute_dlfiles)
+ eval "$prev=\"\$$prev \$arg\""
+ ;;
+ *)
+ eval "$prev=\$arg"
+ ;;
+ esac
+
+ prev=
+ prevopt=
+ continue
+ fi
+
+ # Have we seen a non-optional argument yet?
+ case "$arg" in
+ --help)
+ show_help=yes
+ ;;
+
+ --version)
+ echo "$PROGRAM (GNU $PACKAGE) $VERSION"
+ exit 0
+ ;;
+
+ --dry-run | -n)
+ run=:
+ ;;
+
+ --features)
+ echo "host: $host"
+ if test "$build_libtool_libs" = yes; then
+ echo "enable shared libraries"
+ else
+ echo "disable shared libraries"
+ fi
+ if test "$build_old_libs" = yes; then
+ echo "enable static libraries"
+ else
+ echo "disable static libraries"
+ fi
+ exit 0
+ ;;
+
+ --finish) mode="finish" ;;
+
+ --mode) prevopt="--mode" prev=mode ;;
+ --mode=*) mode="$optarg" ;;
+
+ --quiet | --silent)
+ show=:
+ ;;
+
+ -dlopen)
+ prevopt="-dlopen"
+ prev=execute_dlfiles
+ ;;
+
+ -*)
+ $echo "$modename: unrecognized option \`$arg'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+
+ *)
+ nonopt="$arg"
+ break
+ ;;
+ esac
+done
+
+if test -n "$prevopt"; then
+ $echo "$modename: option \`$prevopt' requires an argument" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+fi
+
+if test -z "$show_help"; then
+
+ # Infer the operation mode.
+ if test -z "$mode"; then
+ case "$nonopt" in
+ *cc | *++ | gcc* | *-gcc*)
+ mode=link
+ for arg
+ do
+ case "$arg" in
+ -c)
+ mode=compile
+ break
+ ;;
+ esac
+ done
+ ;;
+ *db | *dbx)
+ mode=execute
+ ;;
+ *install*|cp|mv)
+ mode=install
+ ;;
+ *rm)
+ mode=uninstall
+ ;;
+ *)
+ # If we have no mode, but dlfiles were specified, then do execute mode.
+ test -n "$execute_dlfiles" && mode=execute
+
+ # Just use the default operation mode.
+ if test -z "$mode"; then
+ if test -n "$nonopt"; then
+ $echo "$modename: warning: cannot infer operation mode from \`$nonopt'" 1>&2
+ else
+ $echo "$modename: warning: cannot infer operation mode without MODE-ARGS" 1>&2
+ fi
+ fi
+ ;;
+ esac
+ fi
+
+ # Only execute mode is allowed to have -dlopen flags.
+ if test -n "$execute_dlfiles" && test "$mode" != execute; then
+ $echo "$modename: unrecognized option \`-dlopen'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ # Change the help message to a mode-specific one.
+ generic_help="$help"
+ help="Try \`$modename --help --mode=$mode' for more information."
+
+ # These modes are in order of execution frequency so that they run quickly.
+ case "$mode" in
+ # libtool compile mode
+ compile)
+ modename="$modename: compile"
+ # Get the compilation command and the source file.
+ base_compile=
+ lastarg=
+ srcfile="$nonopt"
+ suppress_output=
+
+ for arg
+ do
+ # Accept any command-line options.
+ case "$arg" in
+ -o)
+ $echo "$modename: you cannot specify the output filename with \`-o'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+
+ -static)
+ build_libtool_libs=no
+ build_old_libs=yes
+ continue
+ ;;
+ esac
+
+ # Accept the current argument as the source file.
+ lastarg="$srcfile"
+ srcfile="$arg"
+
+ # Aesthetically quote the previous argument.
+
+ # Backslashify any backslashes, double quotes, and dollar signs.
+ # These are the only characters that are still specially
+ # interpreted inside of double-quoted scrings.
+ lastarg=`$echo "X$lastarg" | $Xsed -e "$sed_quote_subst"`
+
+ # Double-quote args containing other shell metacharacters.
+ # Many Bourne shells cannot handle close brackets correctly in scan
+ # sets, so we specify it separately.
+ case "$lastarg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ lastarg="\"$lastarg\""
+ ;;
+ esac
+
+ # Add the previous argument to base_compile.
+ if test -z "$base_compile"; then
+ base_compile="$lastarg"
+ else
+ base_compile="$base_compile $lastarg"
+ fi
+ done
+
+ # Get the name of the library object.
+ libobj=`$echo "X$srcfile" | $Xsed -e 's%^.*/%%'`
+
+ # Recognize several different file suffixes.
+ xform='[cCFSfms]'
+ case "$libobj" in
+ *.ada) xform=ada ;;
+ *.adb) xform=adb ;;
+ *.ads) xform=ads ;;
+ *.asm) xform=asm ;;
+ *.c++) xform=c++ ;;
+ *.cc) xform=cc ;;
+ *.cpp) xform=cpp ;;
+ *.cxx) xform=cxx ;;
+ *.f90) xform=f90 ;;
+ *.for) xform=for ;;
+ esac
+
+ libobj=`$echo "X$libobj" | $Xsed -e "s/\.$xform$/.lo/"`
+
+ case "$libobj" in
+ *.lo) obj=`$echo "X$libobj" | $Xsed -e 's/\.lo$/.o/'` ;;
+ *)
+ $echo "$modename: cannot determine name of library object from \`$srcfile'" 1>&2
+ exit 1
+ ;;
+ esac
+
+ if test -z "$base_compile"; then
+ $echo "$modename: you must specify a compilation command" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ # Delete any leftover library objects.
+ if test "$build_old_libs" = yes; then
+ $run $rm $obj $libobj
+ trap "$run $rm $obj $libobj; exit 1" 1 2 15
+ else
+ $run $rm $libobj
+ trap "$run $rm $libobj; exit 1" 1 2 15
+ fi
+
+ # Only build a PIC object if we are building libtool libraries.
+ if test "$build_libtool_libs" = yes; then
+ # Without this assignment, base_compile gets emptied.
+ fbsd_hideous_sh_bug=$base_compile
+
+ # All platforms use -DPIC, to notify preprocessed assembler code.
+ $show "$base_compile$pic_flag -DPIC $srcfile"
+ if $run eval "$base_compile\$pic_flag -DPIC \$srcfile"; then :
+ else
+ test -n "$obj" && $run $rm $obj
+ exit 1
+ fi
+
+ # If we have no pic_flag, then copy the object into place and finish.
+ if test -z "$pic_flag"; then
+ $show "$LN_S $obj $libobj"
+ $run $LN_S $obj $libobj
+ exit $?
+ fi
+
+ # Just move the object, then go on to compile the next one
+ $show "$mv $obj $libobj"
+ $run $mv $obj $libobj || exit 1
+
+ # Allow error messages only from the first compilation.
+ suppress_output=' >/dev/null 2>&1'
+ fi
+
+ # Only build a position-dependent object if we build old libraries.
+ if test "$build_old_libs" = yes; then
+ # Suppress compiler output if we already did a PIC compilation.
+ $show "$base_compile $srcfile$suppress_output"
+ if $run eval "$base_compile \$srcfile$suppress_output"; then :
+ else
+ $run $rm $obj $libobj
+ exit 1
+ fi
+ fi
+
+ # Create an invalid libtool object if no PIC, so that we do not
+ # accidentally link it into a program.
+ if test "$build_libtool_libs" != yes; then
+ $show "echo timestamp > $libobj"
+ $run eval "echo timestamp > \$libobj" || exit $?
+ fi
+
+ exit 0
+ ;;
+
+ # libtool link mode
+ link)
+ modename="$modename: link"
+ CC="$nonopt"
+ allow_undefined=yes
+ compile_command="$CC"
+ finalize_command="$CC"
+
+ compile_shlibpath=
+ finalize_shlibpath=
+ deplibs=
+ dlfiles=
+ dlprefiles=
+ export_dynamic=no
+ hardcode_libdirs=
+ libobjs=
+ link_against_libtool_libs=
+ ltlibs=
+ objs=
+ prev=
+ prevarg=
+ release=
+ rpath=
+ perm_rpath=
+ temp_rpath=
+ vinfo=
+
+ # We need to know -static, to get the right output filenames.
+ for arg
+ do
+ case "$arg" in
+ -all-static | -static)
+ if test "X$arg" = "X-all-static" && test "$build_libtool_libs" = yes && test -z "$link_static_flag"; then
+ $echo "$modename: warning: complete static linking is impossible in this configuration" 1>&2
+ fi
+ build_libtool_libs=no
+ build_old_libs=yes
+ break
+ ;;
+ esac
+ done
+
+ # See if our shared archives depend on static archives.
+ test -n "$old_archive_from_new_cmds" && build_old_libs=yes
+
+ # Go through the arguments, transforming them on the way.
+ for arg
+ do
+ # If the previous option needs an argument, assign it.
+ if test -n "$prev"; then
+ case "$prev" in
+ output)
+ compile_command="$compile_command @OUTPUT@"
+ finalize_command="$finalize_command @OUTPUT@"
+ ;;
+ esac
+
+ case "$prev" in
+ dlfiles|dlprefiles)
+ case "$arg" in
+ *.la | *.lo) ;; # We handle these cases below.
+ *)
+ dlprefiles="$dlprefiles $arg"
+ test "$prev" = dlfiles && dlfiles="$dlfiles $arg"
+ prev=
+ ;;
+ esac
+ ;;
+ release)
+ release="-$arg"
+ prev=
+ continue
+ ;;
+ rpath)
+ rpath="$rpath $arg"
+ prev=
+ continue
+ ;;
+ *)
+ eval "$prev=\"\$arg\""
+ prev=
+ continue
+ ;;
+ esac
+ fi
+
+ prevarg="$arg"
+
+ case "$arg" in
+ -all-static)
+ if test -n "$link_static_flag"; then
+ compile_command="$compile_command $link_static_flag"
+ finalize_command="$finalize_command $link_static_flag"
+ fi
+ continue
+ ;;
+
+ -allow-undefined)
+ # FIXME: remove this flag sometime in the future.
+ $echo "$modename: \`-allow-undefined' is deprecated because it is the default" 1>&2
+ continue
+ ;;
+
+ -dlopen)
+ prev=dlfiles
+ continue
+ ;;
+
+ -dlpreopen)
+ prev=dlprefiles
+ continue
+ ;;
+
+ -export-dynamic)
+ if test "$export_dynamic" != yes; then
+ export_dynamic=yes
+ if test -n "$export_dynamic_flag_spec"; then
+ eval arg=\"$export_dynamic_flag_spec\"
+ else
+ arg=
+ fi
+
+ # Add the symbol object into the linking commands.
+ compile_command="$compile_command @SYMFILE@"
+ finalize_command="$finalize_command @SYMFILE@"
+ fi
+ ;;
+
+ -L*)
+ dir=`$echo "X$arg" | $Xsed -e 's%^-L\(.*\)$%\1%'`
+ case "$dir" in
+ /* | [A-Za-z]:\\*)
+ # Add the corresponding hardcode_libdir_flag, if it is not identical.
+ ;;
+ *)
+ $echo "$modename: \`-L$dir' cannot specify a relative directory" 1>&2
+ exit 1
+ ;;
+ esac
+ deplibs="$deplibs $arg"
+ ;;
+
+ -l*) deplibs="$deplibs $arg" ;;
+
+ -no-undefined)
+ allow_undefined=no
+ continue
+ ;;
+
+ -o) prev=output ;;
+
+ -release)
+ prev=release
+ continue
+ ;;
+
+ -rpath)
+ prev=rpath
+ continue
+ ;;
+
+ -static)
+ # If we have no pic_flag, then this is the same as -all-static.
+ if test -z "$pic_flag" && test -n "$link_static_flag"; then
+ compile_command="$compile_command $link_static_flag"
+ finalize_command="$finalize_command $link_static_flag"
+ fi
+ continue
+ ;;
+
+ -version-info)
+ prev=vinfo
+ continue
+ ;;
+
+ # Some other compiler flag.
+ -* | +*)
+ # Unknown arguments in both finalize_command and compile_command need
+ # to be aesthetically quoted because they are evaled later.
+ arg=`$echo "X$arg" | $Xsed -e "$sed_quote_subst"`
+ case "$arg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ arg="\"$arg\""
+ ;;
+ esac
+ ;;
+
+ *.o | *.a)
+ # A standard object.
+ objs="$objs $arg"
+ ;;
+
+ *.lo)
+ # A library object.
+ if test "$prev" = dlfiles; then
+ dlfiles="$dlfiles $arg"
+ if test "$build_libtool_libs" = yes; then
+ prev=
+ continue
+ else
+ # If libtool objects are unsupported, then we need to preload.
+ prev=dlprefiles
+ fi
+ fi
+
+ if test "$prev" = dlprefiles; then
+ # Preload the old-style object.
+ dlprefiles="$dlprefiles "`$echo "X$arg" | $Xsed -e 's/\.lo$/\.o/'`
+ prev=
+ fi
+ libobjs="$libobjs $arg"
+ ;;
+
+ *.la)
+ # A libtool-controlled library.
+
+ dlname=
+ libdir=
+ library_names=
+ old_library=
+
+ # Check to see that this really is a libtool archive.
+ if (sed -e '2q' $arg | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then :
+ else
+ $echo "$modename: \`$arg' is not a valid libtool archive" 1>&2
+ exit 1
+ fi
+
+ # If there is no directory component, then add one.
+ case "$arg" in
+ */* | *\\*) . $arg ;;
+ *) . ./$arg ;;
+ esac
+
+ if test -z "$libdir"; then
+ $echo "$modename: \`$arg' contains no -rpath information" 1>&2
+ exit 1
+ fi
+
+ # Get the name of the library we link against.
+ linklib=
+ for l in $old_library $library_names; do
+ linklib="$l"
+ done
+
+ if test -z "$linklib"; then
+ $echo "$modename: cannot find name of link library for \`$arg'" 1>&2
+ exit 1
+ fi
+
+ # Find the relevant object directory and library name.
+ name=`$echo "X$arg" | $Xsed -e 's%^.*/%%' -e 's/\.la$//' -e 's/^lib//'`
+ dir=`$echo "X$arg" | $Xsed -e 's%/[^/]*$%%'`
+ if test "X$dir" = "X$arg"; then
+ dir="$objdir"
+ else
+ dir="$dir/$objdir"
+ fi
+
+ # This library was specified with -dlopen.
+ if test "$prev" = dlfiles; then
+ dlfiles="$dlfiles $arg"
+ if test -z "$dlname"; then
+ # If there is no dlname, we need to preload.
+ prev=dlprefiles
+ else
+ # We should not create a dependency on this library, but we
+ # may need any libraries it requires.
+ compile_command="$compile_command$dependency_libs"
+ finalize_command="$finalize_command$dependency_libs"
+ prev=
+ continue
+ fi
+ fi
+
+ # The library was specified with -dlpreopen.
+ if test "$prev" = dlprefiles; then
+ # Prefer using a static library (so that no silly _DYNAMIC symbols
+ # are required to link).
+ if test -n "$old_library"; then
+ dlprefiles="$dlprefiles $dir/$old_library"
+ else
+ dlprefiles="$dlprefiles $dir/$linklib"
+ fi
+ prev=
+ fi
+
+ if test "$build_libtool_libs" = yes && test -n "$library_names"; then
+ link_against_libtool_libs="$link_against_libtool_libs $arg"
+ if test -n "$shlibpath_var"; then
+ # Make sure the rpath contains only unique directories.
+ case "$temp_rpath " in
+ *" $dir "*) ;;
+ *) temp_rpath="$temp_rpath $dir" ;;
+ esac
+ fi
+
+ # This is the magic to use -rpath.
+ if test -n "$hardcode_libdir_flag_spec"; then
+ if test -n "$hardcode_libdir_separator"; then
+ if test -z "$hardcode_libdirs"; then
+ # Put the magic libdir with the hardcode flag.
+ hardcode_libdirs="$libdir"
+ libdir="@HARDCODE_LIBDIRS@"
+ else
+ # Just accumulate the unique libdirs.
+ case "$hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator" in
+ *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*)
+ ;;
+ *)
+ hardcode_libdirs="$hardcode_libdirs$hardcode_libdir_separator$libdir"
+ ;;
+ esac
+ libdir=
+ fi
+ fi
+
+ if test -n "$libdir"; then
+ eval flag=\"$hardcode_libdir_flag_spec\"
+
+ compile_command="$compile_command $flag"
+ finalize_command="$finalize_command $flag"
+ fi
+ elif test -n "$runpath_var"; then
+ # Do the same for the permanent run path.
+ case "$perm_rpath " in
+ *" $libdir "*) ;;
+ *) perm_rpath="$perm_rpath $libdir" ;;
+ esac
+ fi
+
+
+ case "$hardcode_action" in
+ immediate)
+ if test "$hardcode_direct" = no; then
+ compile_command="$compile_command $dir/$linklib"
+ elif test "$hardcode_minus_L" = no; then
+ compile_command="$compile_command -L$dir -l$name"
+ elif test "$hardcode_shlibpath_var" = no; then
+ compile_shlibpath="$compile_shlibpath$dir:"
+ compile_command="$compile_command -l$name"
+ fi
+ ;;
+
+ relink)
+ # We need an absolute path.
+ case "$dir" in
+ /* | [A-Za-z]:\\*) ;;
+ *)
+ absdir=`cd "$dir" && pwd`
+ if test -z "$absdir"; then
+ $echo "$modename: cannot determine absolute directory name of \`$dir'" 1>&2
+ exit 1
+ fi
+ dir="$absdir"
+ ;;
+ esac
+
+ if test "$hardcode_direct" = yes; then
+ compile_command="$compile_command $dir/$linklib"
+ elif test "$hardcode_minus_L" = yes; then
+ compile_command="$compile_command -L$dir -l$name"
+ elif test "$hardcode_shlibpath_var" = yes; then
+ compile_shlibpath="$compile_shlibpath$dir:"
+ compile_command="$compile_command -l$name"
+ fi
+ ;;
+
+ *)
+ $echo "$modename: \`$hardcode_action' is an unknown hardcode action" 1>&2
+ exit 1
+ ;;
+ esac
+
+ # Finalize command for both is simple: just hardcode it.
+ if test "$hardcode_direct" = yes; then
+ finalize_command="$finalize_command $libdir/$linklib"
+ elif test "$hardcode_minus_L" = yes; then
+ finalize_command="$finalize_command -L$libdir -l$name"
+ elif test "$hardcode_shlibpath_var" = yes; then
+ finalize_shlibpath="$finalize_shlibpath$libdir:"
+ finalize_command="$finalize_command -l$name"
+ else
+ # We cannot seem to hardcode it, guess we'll fake it.
+ finalize_command="$finalize_command -L$libdir -l$name"
+ fi
+ else
+ # Transform directly to old archives if we don't build new libraries.
+ if test -n "$pic_flag" && test -z "$old_library"; then
+ $echo "$modename: cannot find static library for \`$arg'" 1>&2
+ exit 1
+ fi
+
+ # Here we assume that one of hardcode_direct or hardcode_minus_L
+ # is not unsupported. This is valid on all known static and
+ # shared platforms.
+ if test "$hardcode_direct" != unsupported; then
+ test -n "$old_library" && linklib="$old_library"
+ compile_command="$compile_command $dir/$linklib"
+ finalize_command="$finalize_command $dir/$linklib"
+ else
+ compile_command="$compile_command -L$dir -l$name"
+ finalize_command="$finalize_command -L$dir -l$name"
+ fi
+ fi
+
+ # Add in any libraries that this one depends upon.
+ compile_command="$compile_command$dependency_libs"
+ finalize_command="$finalize_command$dependency_libs"
+ continue
+ ;;
+
+ # Some other compiler argument.
+ *)
+ # Unknown arguments in both finalize_command and compile_command need
+ # to be aesthetically quoted because they are evaled later.
+ arg=`$echo "X$arg" | $Xsed -e "$sed_quote_subst"`
+ case "$arg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ arg="\"$arg\""
+ ;;
+ esac
+ ;;
+ esac
+
+ # Now actually substitute the argument into the commands.
+ if test -n "$arg"; then
+ compile_command="$compile_command $arg"
+ finalize_command="$finalize_command $arg"
+ fi
+ done
+
+ if test -n "$prev"; then
+ $echo "$modename: the \`$prevarg' option requires an argument" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ if test -n "$vinfo" && test -n "$release"; then
+ $echo "$modename: you cannot specify both \`-version-info' and \`-release'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ oldlib=
+ oldobjs=
+ case "$output" in
+ "")
+ $echo "$modename: you must specify an output file" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+
+ */* | *\\*)
+ $echo "$modename: output file \`$output' must have no directory components" 1>&2
+ exit 1
+ ;;
+
+ *.a)
+ # Now set the variables for building old libraries.
+ build_libtool_libs=no
+ build_old_libs=yes
+ oldlib="$output"
+ $show "$rm $oldlib"
+ $run $rm $oldlib
+ ;;
+
+ *.la)
+ # Make sure we only generate libraries of the form `libNAME.la'.
+ case "$output" in
+ lib*) ;;
+ *)
+ $echo "$modename: libtool library \`$arg' must begin with \`lib'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+ esac
+
+ name=`$echo "X$output" | $Xsed -e 's/\.la$//' -e 's/^lib//'`
+ eval libname=\"$libname_spec\"
+
+ # All the library-specific variables (install_libdir is set above).
+ library_names=
+ old_library=
+ dlname=
+ current=0
+ revision=0
+ age=0
+
+ if test -n "$objs"; then
+ $echo "$modename: cannot build libtool library \`$output' from non-libtool objects:$objs" 2>&1
+ exit 1
+ fi
+
+ # How the heck are we supposed to write a wrapper for a shared library?
+ if test -n "$link_against_libtool_libs"; then
+ $echo "$modename: libtool library \`$output' may not depend on uninstalled libraries:$link_against_libtool_libs" 1>&2
+ exit 1
+ fi
+
+ if test -n "$dlfiles$dlprefiles"; then
+ $echo "$modename: warning: \`-dlopen' is ignored while creating libtool libraries" 1>&2
+ # Nullify the symbol file.
+ compile_command=`$echo "X$compile_command" | $Xsed -e "s% @SYMFILE@%%"`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "s% @SYMFILE@%%"`
+ fi
+
+ if test -z "$rpath"; then
+ $echo "$modename: you must specify an installation directory with \`-rpath'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ set dummy $rpath
+ if test $# -gt 2; then
+ $echo "$modename: warning: ignoring multiple \`-rpath's for a libtool library" 1>&2
+ fi
+ install_libdir="$2"
+
+ # Parse the version information argument.
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=':'
+ set dummy $vinfo
+ IFS="$save_ifs"
+
+ if test -n "$5"; then
+ $echo "$modename: too many parameters to \`-version-info'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ test -n "$2" && current="$2"
+ test -n "$3" && revision="$3"
+ test -n "$4" && age="$4"
+
+ # Check that each of the things are valid numbers.
+ case "$current" in
+ 0 | [1-9] | [1-9][0-9]*) ;;
+ *)
+ $echo "$modename: CURRENT \`$current' is not a nonnegative integer" 1>&2
+ $echo "$modename: \`$vinfo' is not valid version information" 1>&2
+ exit 1
+ ;;
+ esac
+
+ case "$revision" in
+ 0 | [1-9] | [1-9][0-9]*) ;;
+ *)
+ $echo "$modename: REVISION \`$revision' is not a nonnegative integer" 1>&2
+ $echo "$modename: \`$vinfo' is not valid version information" 1>&2
+ exit 1
+ ;;
+ esac
+
+ case "$age" in
+ 0 | [1-9] | [1-9][0-9]*) ;;
+ *)
+ $echo "$modename: AGE \`$age' is not a nonnegative integer" 1>&2
+ $echo "$modename: \`$vinfo' is not valid version information" 1>&2
+ exit 1
+ ;;
+ esac
+
+ if test $age -gt $current; then
+ $echo "$modename: AGE \`$age' is greater than the current interface number \`$current'" 1>&2
+ $echo "$modename: \`$vinfo' is not valid version information" 1>&2
+ exit 1
+ fi
+
+ # Calculate the version variables.
+ version_vars="version_type current age revision"
+ case "$version_type" in
+ none) ;;
+
+ linux)
+ version_vars="$version_vars major versuffix"
+ major=`expr $current - $age`
+ versuffix="$major.$age.$revision"
+ ;;
+
+ osf)
+ version_vars="$version_vars versuffix verstring"
+ major=`expr $current - $age`
+ versuffix="$current.$age.$revision"
+ verstring="$versuffix"
+
+ # Add in all the interfaces that we are compatible with.
+ loop=$age
+ while test $loop != 0; do
+ iface=`expr $current - $loop`
+ loop=`expr $loop - 1`
+ verstring="$verstring:${iface}.0"
+ done
+
+ # Make executables depend on our current version.
+ verstring="$verstring:${current}.0"
+ ;;
+
+ sunos)
+ version_vars="$version_vars major versuffix"
+ major="$current"
+ versuffix="$current.$revision"
+ ;;
+
+ *)
+ $echo "$modename: unknown library version type \`$version_type'" 1>&2
+ echo "Fatal configuration error. See the $PACKAGE docs for more information." 1>&2
+ exit 1
+ ;;
+ esac
+
+ # Create the output directory, or remove our outputs if we need to.
+ if test -d $objdir; then
+ $show "$rm $objdir/$output $objdir/$libname.* $objdir/${libname}${release}.*"
+ $run $rm $objdir/$output $objdir/$libname.* $objdir/${libname}${release}.*
+ else
+ $show "$mkdir $objdir"
+ $run $mkdir $objdir
+ status=$?
+ if test $status -eq 0 || test -d $objdir; then :
+ else
+ exit $status
+ fi
+ fi
+
+ # Check to see if the archive will have undefined symbols.
+ if test "$allow_undefined" = yes; then
+ if test "$allow_undefined_flag" = unsupported; then
+ $echo "$modename: warning: undefined symbols not allowed in $host shared libraries" 1>&2
+ build_libtool_libs=no
+ build_old_libs=yes
+ fi
+ else
+ # Don't allow undefined symbols.
+ allow_undefined_flag="$no_undefined_flag"
+ fi
+
+ # Add libc to deplibs on all systems.
+ dependency_libs="$deplibs"
+ deplibs="$deplibs -lc"
+
+ if test "$build_libtool_libs" = yes; then
+ # Get the real and link names of the library.
+ eval library_names=\"$library_names_spec\"
+ set dummy $library_names
+ realname="$2"
+ shift; shift
+
+ if test -n "$soname_spec"; then
+ eval soname=\"$soname_spec\"
+ else
+ soname="$realname"
+ fi
+
+ lib="$objdir/$realname"
+ for link
+ do
+ linknames="$linknames $link"
+ done
+
+ # Use standard objects if they are PIC.
+ test -z "$pic_flag" && libobjs=`$echo "X$libobjs " | $Xsed -e 's/\.lo /.o /g' -e 's/ $//g'`
+
+ # Do each of the archive commands.
+ eval cmds=\"$archive_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+
+ # Create links to the real library.
+ for linkname in $linknames; do
+ $show "(cd $objdir && $LN_S $realname $linkname)"
+ $run eval '(cd $objdir && $LN_S $realname $linkname)' || exit $?
+ done
+
+ # If -export-dynamic was specified, set the dlname.
+ if test "$export_dynamic" = yes; then
+ # On all known operating systems, these are identical.
+ dlname="$soname"
+ fi
+ fi
+
+ # Now set the variables for building old libraries.
+ oldlib="$objdir/$libname.a"
+ ;;
+
+ *.lo | *.o)
+ if test -n "$link_against_libtool_libs"; then
+ $echo "$modename: error: cannot link libtool libraries into reloadable objects" 1>&2
+ exit 1
+ fi
+
+ if test -n "$deplibs"; then
+ $echo "$modename: warning: \`-l' and \`-L' are ignored while creating objects" 1>&2
+ fi
+
+ if test -n "$dlfiles$dlprefiles"; then
+ $echo "$modename: warning: \`-dlopen' is ignored while creating objects" 1>&2
+ # Nullify the symbol file.
+ compile_command=`$echo "X$compile_command" | $Xsed -e "s% @SYMFILE@%%"`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "s% @SYMFILE@%%"`
+ fi
+
+ if test -n "$rpath"; then
+ $echo "$modename: warning: \`-rpath' is ignored while creating objects" 1>&2
+ fi
+
+ if test -n "$vinfo"; then
+ $echo "$modename: warning: \`-version-info' is ignored while creating objects" 1>&2
+ fi
+
+ if test -n "$release"; then
+ $echo "$modename: warning: \`-release' is ignored while creating objects" 1>&2
+ fi
+
+ case "$output" in
+ *.lo)
+ if test -n "$objs"; then
+ $echo "$modename: cannot build library object \`$output' from non-libtool objects" 1>&2
+ exit 1
+ fi
+ libobj="$output"
+ obj=`$echo "X$output" | $Xsed -e 's/\.lo$/.o/'`
+ ;;
+ *)
+ libobj=
+ obj="$output"
+ ;;
+ esac
+
+ # Delete the old objects.
+ $run $rm $obj $libobj
+
+ # Create the old-style object.
+ reload_objs="$objs"`$echo "X$libobjs " | $Xsed -e 's/[^ ]*\.a //g' -e 's/\.lo /.o /g' -e 's/ $//g'`
+
+ output="$obj"
+ eval cmds=\"$reload_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+
+ # Exit if we aren't doing a library object file.
+ test -z "$libobj" && exit 0
+
+ if test "$build_libtool_libs" != yes; then
+ # Create an invalid libtool object if no PIC, so that we don't
+ # accidentally link it into a program.
+ $show "echo timestamp > $libobj"
+ $run eval "echo timestamp > $libobj" || exit $?
+ exit 0
+ fi
+
+ if test -n "$pic_flag"; then
+ # Only do commands if we really have different PIC objects.
+ reload_objs="$libobjs"
+ output="$libobj"
+ eval cmds=\"$reload_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+ else
+ # Just create a symlink.
+ $show "$LN_S $obj $libobj"
+ $run $LN_S $obj $libobj || exit 1
+ fi
+
+ exit 0
+ ;;
+
+ *)
+ if test -n "$vinfo"; then
+ $echo "$modename: warning: \`-version-info' is ignored while linking programs" 1>&2
+ fi
+
+ if test -n "$release"; then
+ $echo "$modename: warning: \`-release' is ignored while creating objects" 1>&2
+ fi
+
+ if test -n "$rpath"; then
+ # If the user specified any rpath flags, then add them.
+ for libdir in $rpath; do
+ if test -n "$hardcode_libdir_flag_spec"; then
+ if test -n "$hardcode_libdir_separator"; then
+ if test -z "$hardcode_libdirs"; then
+ # Put the magic libdir with the hardcode flag.
+ hardcode_libdirs="$libdir"
+ libdir="@HARDCODE_LIBDIRS@"
+ else
+ # Just accumulate the unique libdirs.
+ case "$hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator" in
+ *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*)
+ ;;
+ *)
+ hardcode_libdirs="$hardcode_libdirs$hardcode_libdir_separator$libdir"
+ ;;
+ esac
+ libdir=
+ fi
+ fi
+
+ if test -n "$libdir"; then
+ eval flag=\"$hardcode_libdir_flag_spec\"
+
+ compile_command="$compile_command $flag"
+ finalize_command="$finalize_command $flag"
+ fi
+ elif test -n "$runpath_var"; then
+ case "$perm_rpath " in
+ *" $libdir "*) ;;
+ *) perm_rpath="$perm_rpath $libdir" ;;
+ esac
+ fi
+ done
+ fi
+
+ # Substitute the hardcoded libdirs into the compile commands.
+ if test -n "$hardcode_libdir_separator"; then
+ compile_command=`$echo "X$compile_command" | $Xsed -e "s%@HARDCODE_LIBDIRS@%$hardcode_libdirs%g"`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "s%@HARDCODE_LIBDIRS@%$hardcode_libdirs%g"`
+ fi
+
+ if test -n "$libobjs" && test "$build_old_libs" = yes; then
+ # Transform all the library objects into standard objects.
+ compile_command=`$echo "X$compile_command " | $Xsed -e 's/\.lo /.o /g' -e 's/ $//'`
+ finalize_command=`$echo "X$finalize_command " | $Xsed -e 's/\.lo /.o /g' -e 's/ $//'`
+ fi
+
+ if test "$export_dynamic" = yes && test -n "$NM" && test -n "$global_symbol_pipe"; then
+ dlsyms="${output}S.c"
+ else
+ dlsyms=
+ fi
+
+ if test -n "$dlsyms"; then
+ # Add our own program objects to the preloaded list.
+ dlprefiles=`$echo "X$objs$dlprefiles " | $Xsed -e 's/\.lo /.o /g' -e 's/ $//'`
+
+ # Discover the nlist of each of the dlfiles.
+ nlist="$objdir/${output}.nm"
+
+ if test -d $objdir; then
+ $show "$rm $nlist ${nlist}T"
+ $run $rm "$nlist" "${nlist}T"
+ else
+ $show "$mkdir $objdir"
+ $run $mkdir $objdir
+ status=$?
+ if test $status -eq 0 || test -d $objdir; then :
+ else
+ exit $status
+ fi
+ fi
+
+ for arg in $dlprefiles; do
+ $show "extracting global C symbols from \`$arg'"
+ $run eval "$NM $arg | $global_symbol_pipe >> '$nlist'"
+ done
+
+ # Parse the name list into a source file.
+ $show "creating $objdir/$dlsyms"
+ if test -z "$run"; then
+ # Make sure we at least have an empty file.
+ test -f "$nlist" || : > "$nlist"
+
+ # Try sorting and uniquifying the output.
+ if sort "$nlist" | uniq > "$nlist"T; then
+ mv -f "$nlist"T "$nlist"
+ wcout=`wc "$nlist" 2>/dev/null`
+ count=`echo "X$wcout" | $Xsed -e 's/^[ ]*\([0-9][0-9]*\).*$/\1/'`
+ (test "$count" -ge 0) 2>/dev/null || count=-1
+ else
+ $rm "$nlist"T
+ count=-1
+ fi
+
+ case "$dlsyms" in
+ "") ;;
+ *.c)
+ $echo > "$objdir/$dlsyms" "\
+/* $dlsyms - symbol resolution table for \`$output' dlsym emulation. */
+/* Generated by $PROGRAM - GNU $PACKAGE $VERSION */
+
+#ifdef __cplusplus
+extern \"C\" {
+#endif
+
+/* Prevent the only kind of declaration conflicts we can make. */
+#define dld_preloaded_symbol_count some_other_symbol
+#define dld_preloaded_symbols some_other_symbol
+
+/* External symbol declarations for the compiler. */\
+"
+
+ if test -f "$nlist"; then
+ sed -e 's/^.* \(.*\)$/extern char \1;/' < "$nlist" >> "$objdir/$dlsyms"
+ else
+ echo '/* NONE */' >> "$objdir/$dlsyms"
+ fi
+
+ $echo >> "$objdir/$dlsyms" "\
+
+#undef dld_preloaded_symbol_count
+#undef dld_preloaded_symbols
+
+#if defined (__STDC__) && __STDC__
+# define __ptr_t void *
+#else
+# define __ptr_t char *
+#endif
+
+/* The number of symbols in dld_preloaded_symbols, -1 if unsorted. */
+int dld_preloaded_symbol_count = $count;
+
+/* The mapping between symbol names and symbols. */
+struct {
+ char *name;
+ __ptr_t address;
+}
+dld_preloaded_symbols[] =
+{\
+"
+
+ if test -f "$nlist"; then
+ sed 's/^\(.*\) \(.*\)$/ {"\1", (__ptr_t) \&\2},/' < "$nlist" >> "$objdir/$dlsyms"
+ fi
+
+ $echo >> "$objdir/$dlsyms" "\
+ {0, (__ptr_t) 0}
+};
+
+#ifdef __cplusplus
+}
+#endif\
+"
+ ;;
+
+ *)
+ $echo "$modename: unknown suffix for \`$dlsyms'" 1>&2
+ exit 1
+ ;;
+ esac
+ fi
+
+ # Now compile the dynamic symbol file.
+ $show "(cd $objdir && $CC -c$no_builtin_flag \"$dlsyms\")"
+ $run eval '(cd $objdir && $CC -c$no_builtin_flag "$dlsyms")' || exit $?
+
+ # Transform the symbol file into the correct name.
+ compile_command=`$echo "X$compile_command" | $Xsed -e "s%@SYMFILE@%$objdir/${output}S.o%"`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "s%@SYMFILE@%$objdir/${output}S.o%"`
+ elif test "$export_dynamic" != yes; then
+ test -n "$dlfiles$dlprefiles" && $echo "$modename: warning: \`-dlopen' and \`-dlpreopen' are ignored without \`-export-dynamic'" 1>&2
+ else
+ # We keep going just in case the user didn't refer to
+ # dld_preloaded_symbols. The linker will fail if global_symbol_pipe
+ # really was required.
+ $echo "$modename: not configured to extract global symbols from dlpreopened files" 1>&2
+
+ # Nullify the symbol file.
+ compile_command=`$echo "X$compile_command" | $Xsed -e "s% @SYMFILE@%%"`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "s% @SYMFILE@%%"`
+ fi
+
+ if test -z "$link_against_libtool_libs" || test "$build_libtool_libs" != yes; then
+ # Replace the output file specification.
+ compile_command=`$echo "X$compile_command" | $Xsed -e 's%@OUTPUT@%'"$output"'%g'`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e 's%@OUTPUT@%'"$output"'%g'`
+
+ # We have no uninstalled library dependencies, so finalize right now.
+ $show "$compile_command"
+ $run eval "$compile_command"
+ exit $?
+ fi
+
+ # Replace the output file specification.
+ compile_command=`$echo "X$compile_command" | $Xsed -e 's%@OUTPUT@%'"$objdir/$output"'%g'`
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e 's%@OUTPUT@%'"$objdir/$output"'T%g'`
+
+ # Create the binary in the object directory, then wrap it.
+ if test -d $objdir; then :
+ else
+ $show "$mkdir $objdir"
+ $run $mkdir $objdir
+ status=$?
+ if test $status -eq 0 || test -d $objdir; then :
+ else
+ exit $status
+ fi
+ fi
+
+ if test -n "$shlibpath_var"; then
+ # We should set the shlibpath_var
+ rpath=
+ for dir in $temp_rpath; do
+ case "$dir" in
+ /* | [A-Za-z]:\\*)
+ # Absolute path.
+ rpath="$rpath$dir:"
+ ;;
+ *)
+ # Relative path: add a thisdir entry.
+ rpath="$rpath\$thisdir/$dir:"
+ ;;
+ esac
+ done
+ temp_rpath="$rpath"
+ fi
+
+ # Delete the old output file.
+ $run $rm $output
+
+ if test -n "$compile_shlibpath"; then
+ compile_command="$shlibpath_var=\"$compile_shlibpath\$$shlibpath_var\" $compile_command"
+ fi
+ if test -n "$finalize_shlibpath"; then
+ finalize_command="$shlibpath_var=\"$finalize_shlibpath\$$shlibpath_var\" $finalize_command"
+ fi
+
+ if test -n "$runpath_var" && test -n "$perm_rpath"; then
+ # We should set the runpath_var.
+ rpath=
+ for dir in $perm_rpath; do
+ rpath="$rpath$dir:"
+ done
+ compile_command="$runpath_var=\"$rpath\$$runpath_var\" $compile_command"
+ finalize_command="$runpath_var=\"$rpath\$$runpath_var\" $finalize_command"
+ fi
+
+ case "$hardcode_action" in
+ relink)
+ # AGH! Flame the AIX and HP-UX people for me, will ya?
+ $echo "$modename: warning: using a buggy system linker" 1>&2
+ $echo "$modename: relinking will be required before \`$output' can be installed" 1>&2
+ ;;
+ esac
+
+ $show "$compile_command"
+ $run eval "$compile_command" || exit $?
+
+ # Now create the wrapper script.
+ $show "creating $output"
+
+ # Quote the finalize command for shipping.
+ finalize_command=`$echo "X$finalize_command" | $Xsed -e "$sed_quote_subst"`
+
+ # Quote $echo for shipping.
+ qecho=`$echo "X$echo" | $Xsed -e "$sed_quote_subst"`
+
+ # Only actually do things if our run command is non-null.
+ if test -z "$run"; then
+ $rm $output
+ trap "$rm $output; exit 1" 1 2 15
+
+ $echo > $output "\
+#! /bin/sh
+
+# $output - temporary wrapper script for $objdir/$output
+# Generated by ltmain.sh - GNU $PACKAGE $VERSION
+#
+# The $output program cannot be directly executed until all the libtool
+# libraries that it depends on are installed.
+#
+# This wrapper script should never be moved out of \``pwd`'.
+# If it is, it will not operate correctly.
+
+# Sed substitution that helps us do robust quoting. It backslashifies
+# metacharacters that are still active within double-quoted strings.
+Xsed='sed -e s/^X//'
+sed_quote_subst='$sed_quote_subst'
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+if test \"\${CDPATH+set}\" = set; then CDPATH=; export CDPATH; fi
+
+# This environment variable determines our operation mode.
+if test \"\$libtool_install_magic\" = \"$magic\"; then
+ # install mode needs the following variables:
+ link_against_libtool_libs='$link_against_libtool_libs'
+ finalize_command=\"$finalize_command\"
+else
+ # When we are sourced in execute mode, \$file and \$echo are already set.
+ if test \"\$libtool_execute_magic\" = \"$magic\"; then :
+ else
+ echo=\"$qecho\"
+ file=\"\$0\"
+ fi\
+"
+ $echo >> $output "\
+
+ # Find the directory that this script lives in.
+ thisdir=\`\$echo \"X\$file\" | \$Xsed -e 's%/[^/]*$%%'\`
+ test \"x\$thisdir\" = \"x\$file\" && thisdir=.
+
+ # Follow symbolic links until we get to the real thisdir.
+ file=\`ls -ld \"\$file\" | sed -n 's/.*-> //p'\`
+ while test -n \"\$file\"; do
+ destdir=\`\$echo \"X\$file\" | \$Xsed -e 's%/[^/]*\$%%'\`
+
+ # If there was a directory component, then change thisdir.
+ if test \"x\$destdir\" != \"x\$file\"; then
+ case \"\$destdir\" in
+ /* | [A-Za-z]:\\*) thisdir=\"\$destdir\" ;;
+ *) thisdir=\"\$thisdir/\$destdir\" ;;
+ esac
+ fi
+
+ file=\`\$echo \"X\$file\" | \$Xsed -e 's%^.*/%%'\`
+ file=\`ls -ld \"\$thisdir/\$file\" | sed -n 's/.*-> //p'\`
+ done
+
+ # Try to get the absolute directory name.
+ absdir=\`cd \"\$thisdir\" && pwd\`
+ test -n \"\$absdir\" && thisdir=\"\$absdir\"
+
+ progdir=\"\$thisdir/$objdir\"
+ program='$output'
+
+ if test -f \"\$progdir/\$program\"; then"
+
+ # Export our shlibpath_var if we have one.
+ if test -n "$shlibpath_var" && test -n "$temp_rpath"; then
+ $echo >> $output "\
+ # Add our own library path to $shlibpath_var
+ $shlibpath_var=\"$temp_rpath\$$shlibpath_var\"
+
+ # Some systems cannot cope with colon-terminated $shlibpath_var
+ $shlibpath_var=\`\$echo \"X\$$shlibpath_var\" | \$Xsed -e 's/:*\$//'\`
+
+ export $shlibpath_var
+"
+ fi
+
+ $echo >> $output "\
+ if test \"\$libtool_execute_magic\" != \"$magic\"; then
+ # Run the actual program with our arguments.
+
+ # Export the path to the program.
+ PATH=\"\$progdir:\$PATH\"
+ export PATH
+
+ exec \$program \${1+\"\$@\"}
+
+ \$echo \"\$0: cannot exec \$program \${1+\"\$@\"}\"
+ exit 1
+ fi
+ else
+ # The program doesn't exist.
+ \$echo \"\$0: error: \$progdir/\$program does not exist\" 1>&2
+ \$echo \"This script is just a wrapper for \$program.\" 1>&2
+ echo \"See the $PACKAGE documentation for more information.\" 1>&2
+ exit 1
+ fi
+fi\
+"
+ chmod +x $output
+ fi
+ exit 0
+ ;;
+ esac
+
+ # See if we need to build an old-fashioned archive.
+ if test "$build_old_libs" = "yes"; then
+ # Transform .lo files to .o files.
+ oldobjs="$objs"`$echo "X$libobjs " | $Xsed -e 's/[^ ]*\.a //g' -e 's/\.lo /.o /g' -e 's/ $//g'`
+
+ # Do each command in the archive commands.
+ if test -n "$old_archive_from_new_cmds" && test "$build_libtool_libs" = yes; then
+ eval cmds=\"$old_archive_from_new_cmds\"
+ else
+ eval cmds=\"$old_archive_cmds\"
+ fi
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+ fi
+
+ # Now create the libtool archive.
+ case "$output" in
+ *.la)
+ old_library=
+ test "$build_old_libs" = yes && old_library="$libname.a"
+
+ $show "creating $output"
+
+ # Only create the output if not a dry run.
+ if test -z "$run"; then
+ $echo > $output "\
+# $output - a libtool library file
+# Generated by ltmain.sh - GNU $PACKAGE $VERSION
+
+# The name that we can dlopen(3).
+dlname='$dlname'
+
+# Names of this library.
+library_names='$library_names'
+
+# The name of the static archive.
+old_library='$old_library'
+
+# Libraries that this one depends upon.
+dependency_libs='$dependency_libs'
+
+# Version information for $libname.
+current=$current
+age=$age
+revision=$revision
+
+# Directory that this library needs to be installed in:
+libdir='$install_libdir'\
+"
+ fi
+
+ # Do a symbolic link so that the libtool archive can be found in
+ # LD_LIBRARY_PATH before the program is installed.
+ $show "(cd $objdir && $LN_S ../$output $output)"
+ $run eval "(cd $objdir && $LN_S ../$output $output)" || exit 1
+ ;;
+ esac
+ exit 0
+ ;;
+
+ # libtool install mode
+ install)
+ modename="$modename: install"
+
+ # There may be an optional /bin/sh argument at the beginning of
+ # install_prog (especially on Windows NT).
+ if test "$nonopt" = "$SHELL"; then
+ # Aesthetically quote it.
+ arg=`$echo "X$nonopt" | $Xsed -e "$sed_quote_subst"`
+ case "$arg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ arg="\"$arg\""
+ ;;
+ esac
+ install_prog="$arg "
+ arg="$1"
+ shift
+ else
+ install_prog=
+ arg="$nonopt"
+ fi
+
+ # The real first argument should be the name of the installation program.
+ # Aesthetically quote it.
+ arg=`$echo "X$arg" | $Xsed -e "$sed_quote_subst"`
+ case "$arg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ arg="\"$arg\""
+ ;;
+ esac
+ install_prog="$install_prog$arg"
+
+ # We need to accept at least all the BSD install flags.
+ dest=
+ files=
+ opts=
+ prev=
+ install_type=
+ isdir=
+ stripme=
+ for arg
+ do
+ if test -n "$dest"; then
+ files="$files $dest"
+ dest="$arg"
+ continue
+ fi
+
+ case "$arg" in
+ -d) isdir=yes ;;
+ -f) prev="-f" ;;
+ -g) prev="-g" ;;
+ -m) prev="-m" ;;
+ -o) prev="-o" ;;
+ -s)
+ stripme=" -s"
+ continue
+ ;;
+ -*) ;;
+
+ *)
+ # If the previous option needed an argument, then skip it.
+ if test -n "$prev"; then
+ prev=
+ else
+ dest="$arg"
+ continue
+ fi
+ ;;
+ esac
+
+ # Aesthetically quote the argument.
+ arg=`$echo "X$arg" | $Xsed -e "$sed_quote_subst"`
+ case "$arg" in
+ *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*)
+ arg="\"$arg\""
+ ;;
+ esac
+ install_prog="$install_prog $arg"
+ done
+
+ if test -z "$install_prog"; then
+ $echo "$modename: you must specify an install program" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ if test -n "$prev"; then
+ $echo "$modename: the \`$prev' option requires an argument" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ if test -z "$files"; then
+ if test -z "$dest"; then
+ $echo "$modename: no file or destination specified" 1>&2
+ else
+ $echo "$modename: you must specify a destination" 1>&2
+ fi
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ # Strip any trailing slash from the destination.
+ dest=`$echo "X$dest" | $Xsed -e 's%/$%%'`
+
+ # Check to see that the destination is a directory.
+ test -d "$dest" && isdir=yes
+ if test -n "$isdir"; then
+ destdir="$dest"
+ destname=
+ else
+ destdir=`$echo "X$dest" | $Xsed -e 's%/[^/]*$%%'`
+ test "X$destdir" = "X$dest" && destdir=.
+ destname=`$echo "X$dest" | $Xsed -e 's%^.*/%%'`
+
+ # Not a directory, so check to see that there is only one file specified.
+ set dummy $files
+ if test $# -gt 2; then
+ $echo "$modename: \`$dest' is not a directory" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+ fi
+ case "$destdir" in
+ /* | [A-Za-z]:\\*) ;;
+ *)
+ for file in $files; do
+ case "$file" in
+ *.lo) ;;
+ *)
+ $echo "$modename: \`$destdir' must be an absolute directory name" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+ esac
+ done
+ ;;
+ esac
+
+ # This variable tells wrapper scripts just to set variables rather
+ # than running their programs.
+ libtool_install_magic="$magic"
+
+ staticlibs=
+ future_libdirs=
+ current_libdirs=
+ for file in $files; do
+
+ # Do each installation.
+ case "$file" in
+ *.a)
+ # Do the static libraries later.
+ staticlibs="$staticlibs $file"
+ ;;
+
+ *.la)
+ # Check to see that this really is a libtool archive.
+ if (sed -e '2q' $file | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then :
+ else
+ $echo "$modename: \`$file' is not a valid libtool archive" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ library_names=
+ old_library=
+ # If there is no directory component, then add one.
+ case "$file" in
+ */* | *\\*) . $file ;;
+ *) . ./$file ;;
+ esac
+
+ # Add the libdir to current_libdirs if it is the destination.
+ if test "X$destdir" = "X$libdir"; then
+ case "$current_libdirs " in
+ *" $libdir "*) ;;
+ *) current_libdirs="$current_libdirs $libdir" ;;
+ esac
+ else
+ # Note the libdir as a future libdir.
+ case "$future_libdirs " in
+ *" $libdir "*) ;;
+ *) future_libdirs="$future_libdirs $libdir" ;;
+ esac
+ fi
+
+ dir="`$echo "X$file" | $Xsed -e 's%/[^/]*$%%'`/"
+ test "X$dir" = "X$file/" && dir=
+ dir="$dir$objdir"
+
+ # See the names of the shared library.
+ set dummy $library_names
+ if test -n "$2"; then
+ realname="$2"
+ shift
+ shift
+
+ # Install the shared library and build the symlinks.
+ $show "$install_prog $dir/$realname $destdir/$realname"
+ $run eval "$install_prog $dir/$realname $destdir/$realname" || exit $?
+ test "X$dlname" = "X$realname" && dlname=
+
+ if test $# -gt 0; then
+ # Delete the old symlinks.
+ rmcmd="$rm"
+ for linkname
+ do
+ rmcmd="$rmcmd $destdir/$linkname"
+ done
+ $show "$rmcmd"
+ $run $rmcmd
+
+ # ... and create new ones.
+ for linkname
+ do
+ test "X$dlname" = "X$linkname" && dlname=
+ $show "(cd $destdir && $LN_S $realname $linkname)"
+ $run eval "(cd $destdir && $LN_S $realname $linkname)"
+ done
+ fi
+
+ if test -n "$dlname"; then
+ # Install the dynamically-loadable library.
+ $show "$install_prog $dir/$dlname $destdir/$dlname"
+ $run eval "$install_prog $dir/$dlname $destdir/$dlname" || exit $?
+ fi
+
+ # Do each command in the postinstall commands.
+ lib="$destdir/$realname"
+ eval cmds=\"$postinstall_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+ fi
+
+ # Install the pseudo-library for information purposes.
+ name=`$echo "X$file" | $Xsed -e 's%^.*/%%'`
+ $show "$install_prog $file $destdir/$name"
+ $run eval "$install_prog $file $destdir/$name" || exit $?
+
+ # Maybe install the static library, too.
+ test -n "$old_library" && staticlibs="$staticlibs $dir/$old_library"
+ ;;
+
+ *.lo)
+ # Install (i.e. copy) a libtool object.
+
+ # Figure out destination file name, if it wasn't already specified.
+ if test -n "$destname"; then
+ destfile="$destdir/$destname"
+ else
+ destfile=`$echo "X$file" | $Xsed -e 's%^.*/%%'`
+ destfile="$destdir/$destfile"
+ fi
+
+ # Deduce the name of the destination old-style object file.
+ case "$destfile" in
+ *.lo)
+ staticdest=`$echo "X$destfile" | $Xsed -e 's/\.lo$/\.o/'`
+ ;;
+ *.o)
+ staticdest="$destfile"
+ destfile=
+ ;;
+ *)
+ $echo "$modename: cannot copy a libtool object to \`$destfile'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+ esac
+
+ # Install the libtool object if requested.
+ if test -n "$destfile"; then
+ $show "$install_prog $file $destfile"
+ $run eval "$install_prog $file $destfile" || exit $?
+ fi
+
+ # Install the old object if enabled.
+ if test "$build_old_libs" = yes; then
+ # Deduce the name of the old-style object file.
+ staticobj=`$echo "X$file" | $Xsed -e 's/\.lo$/\.o/'`
+
+ $show "$install_prog $staticobj $staticdest"
+ $run eval "$install_prog \$staticobj \$staticdest" || exit $?
+ fi
+ exit 0
+ ;;
+
+ *)
+ # Do a test to see if this is really a libtool program.
+ if (sed -e '4q' $file | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then
+ link_against_libtool_libs=
+ finalize_command=
+
+ # If there is no directory component, then add one.
+ case "$file" in
+ */* | *\\*) . $file ;;
+ *) . ./$file ;;
+ esac
+
+ # Check the variables that should have been set.
+ if test -z "$link_against_libtool_libs" || test -z "$finalize_command"; then
+ $echo "$modename: invalid libtool wrapper script \`$file'" 1>&2
+ exit 1
+ fi
+
+ finalize=yes
+ for lib in $link_against_libtool_libs; do
+ # Check to see that each library is installed.
+ libdir=
+ if test -f "$lib"; then
+ # If there is no directory component, then add one.
+ case "$lib" in
+ */* | *\\*) . $lib ;;
+ *) . ./$lib ;;
+ esac
+ fi
+ libfile="$libdir/`$echo "X$lib" | $Xsed -e 's%^.*/%%g'`"
+ if test -z "$libdir"; then
+ $echo "$modename: warning: \`$lib' contains no -rpath information" 1>&2
+ elif test -f "$libfile"; then :
+ else
+ $echo "$modename: warning: \`$lib' has not been installed in \`$libdir'" 1>&2
+ finalize=no
+ fi
+ done
+
+ if test "$hardcode_action" = relink; then
+ if test "$finalize" = yes; then
+ $echo "$modename: warning: relinking \`$file' on behalf of your buggy system linker" 1>&2
+ $show "$finalize_command"
+ if $run eval "$finalize_command"; then :
+ else
+ $echo "$modename: error: relink \`$file' with the above command before installing it" 1>&2
+ continue
+ fi
+ file="$objdir/$file"T
+ else
+ $echo "$modename: warning: cannot relink \`$file' on behalf of your buggy system linker" 1>&2
+ fi
+ else
+ # Install the binary that we compiled earlier.
+ file=`$echo "X$file" | $Xsed -e "s%\([^/]*\)$%$objdir/\1%"`
+ fi
+ fi
+
+ $show "$install_prog$stripme $file $dest"
+ $run eval "$install_prog\$stripme \$file \$dest" || exit $?
+ ;;
+ esac
+ done
+
+ for file in $staticlibs; do
+ name=`$echo "X$file" | $Xsed -e 's%^.*/%%'`
+
+ # Set up the ranlib parameters.
+ oldlib="$destdir/$name"
+
+ $show "$install_prog $file $oldlib"
+ $run eval "$install_prog \$file \$oldlib" || exit $?
+
+ # Do each command in the postinstall commands.
+ eval cmds=\"$old_postinstall_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd" || exit $?
+ done
+ IFS="$save_ifs"
+ done
+
+ if test -n "$future_libdirs"; then
+ $echo "$modename: warning: remember to run \`$progname --finish$future_libdirs'" 1>&2
+ fi
+
+ if test -n "$current_libdirs"; then
+ # Maybe just do a dry run.
+ test -n "$run" && current_libdirs=" -n$current_libdirs"
+ exec $SHELL $0 --finish$current_libdirs
+ exit 1
+ fi
+
+ exit 0
+ ;;
+
+ # libtool finish mode
+ finish)
+ modename="$modename: finish"
+ libdirs="$nonopt"
+
+ if test -n "$finish_cmds$finish_eval" && test -n "$libdirs"; then
+ for dir
+ do
+ libdirs="$libdirs $dir"
+ done
+
+ for libdir in $libdirs; do
+ if test -n "$finish_cmds"; then
+ # Do each command in the finish commands.
+ eval cmds=\"$finish_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd"
+ done
+ IFS="$save_ifs"
+ fi
+ if test -n "$finish_eval"; then
+ # Do the single finish_eval.
+ eval cmds=\"$finish_eval\"
+ $run eval "$cmds"
+ fi
+ done
+ fi
+
+ echo "------------------------------------------------------------------------------"
+ echo "Libraries have been installed in:"
+ for libdir in $libdirs; do
+ echo " $libdir"
+ done
+ echo
+ echo "To link against installed libraries in a given directory, LIBDIR,"
+ echo "you must use the \`-LLIBDIR' flag during linking."
+ echo
+ echo " You will also need to do one of the following:"
+ if test -n "$shlibpath_var"; then
+ echo " - add LIBDIR to the \`$shlibpath_var' environment variable"
+ echo " during execution"
+ fi
+ if test -n "$runpath_var"; then
+ echo " - add LIBDIR to the \`$runpath_var' environment variable"
+ echo " during linking"
+ fi
+ if test -n "$hardcode_libdir_flag_spec"; then
+ libdir=LIBDIR
+ eval flag=\"$hardcode_libdir_flag_spec\"
+
+ echo " - use the \`$flag' linker flag"
+ fi
+ if test -f /etc/ld.so.conf; then
+ echo " - have your system administrator add LIBDIR to \`/etc/ld.so.conf'"
+ fi
+ echo
+ echo "See any operating system documentation about shared libraries for"
+ echo "more information, such as the ld(1) and ld.so(8) manual pages."
+ echo "------------------------------------------------------------------------------"
+ exit 0
+ ;;
+
+ # libtool execute mode
+ execute)
+ modename="$modename: execute"
+
+ # The first argument is the command name.
+ cmd="$nonopt"
+ if test -z "$cmd"; then
+ $echo "$modename: you must specify a COMMAND" 1>&2
+ $echo "$help"
+ exit 1
+ fi
+
+ # Handle -dlopen flags immediately.
+ for file in $execute_dlfiles; do
+ if test -f "$file"; then :
+ else
+ $echo "$modename: \`$file' is not a file" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ dir=
+ case "$file" in
+ *.la)
+ # Check to see that this really is a libtool archive.
+ if (sed -e '2q' $file | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then :
+ else
+ $echo "$modename: \`$lib' is not a valid libtool archive" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ # Read the libtool library.
+ dlname=
+ library_names=
+
+ # If there is no directory component, then add one.
+ case "$file" in
+ */* | *\\*) . $file ;;
+ *) . ./$file ;;
+ esac
+
+ # Skip this library if it cannot be dlopened.
+ if test -z "$dlname"; then
+ # Warn if it was a shared library.
+ test -n "$library_names" && $echo "$modename: warning: \`$file' was not linked with \`-export-dynamic'"
+ continue
+ fi
+
+ dir=`$echo "X$file" | $Xsed -e 's%/[^/]*$%%'`
+ test "X$dir" = "X$file" && dir=.
+
+ if test -f "$dir/$objdir/$dlname"; then
+ dir="$dir/$objdir"
+ else
+ $echo "$modename: cannot find \`$dlname' in \`$dir' or \`$dir/$objdir'" 1>&2
+ exit 1
+ fi
+ ;;
+
+ *.lo)
+ # Just add the directory containing the .lo file.
+ dir=`$echo "X$file" | $Xsed -e 's%/[^/]*$%%'`
+ test "X$dir" = "X$file" && dir=.
+ ;;
+
+ *)
+ $echo "$modename: warning \`-dlopen' is ignored for non-libtool libraries and objects" 1>&2
+ continue
+ ;;
+ esac
+
+ # Get the absolute pathname.
+ absdir=`cd "$dir" && pwd`
+ test -n "$absdir" && dir="$absdir"
+
+ # Now add the directory to shlibpath_var.
+ if eval "test -z \"\$$shlibpath_var\""; then
+ eval "$shlibpath_var=\"\$dir\""
+ else
+ eval "$shlibpath_var=\"\$dir:\$$shlibpath_var\""
+ fi
+ done
+
+ # This variable tells wrapper scripts just to set shlibpath_var
+ # rather than running their programs.
+ libtool_execute_magic="$magic"
+
+ # Check if any of the arguments is a wrapper script.
+ args=
+ for file
+ do
+ case "$file" in
+ -*) ;;
+ *)
+ # Do a test to see if this is really a libtool program.
+ if (sed -e '4q' $file | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then
+ # If there is no directory component, then add one.
+ case "$file" in
+ */* | *\\*) . $file ;;
+ *) . ./$file ;;
+ esac
+
+ # Transform arg to wrapped name.
+ file="$progdir/$program"
+ fi
+ ;;
+ esac
+ # Quote arguments (to preserve shell metacharacters).
+ file=`$echo "X$file" | $Xsed -e "$sed_quote_subst"`
+ args="$args \"$file\""
+ done
+
+ if test -z "$run"; then
+ # Export the shlibpath_var.
+ eval "export $shlibpath_var"
+
+ # Now actually exec the command.
+ eval "exec \$cmd$args"
+
+ $echo "$modename: cannot exec \$cmd$args"
+ exit 1
+ else
+ # Display what would be done.
+ eval "\$echo \"\$shlibpath_var=\$$shlibpath_var\""
+ $echo "export $shlibpath_var"
+ $echo "$cmd$args"
+ exit 0
+ fi
+ ;;
+
+ # libtool uninstall mode
+ uninstall)
+ modename="$modename: uninstall"
+ rm="$nonopt"
+ files=
+
+ for arg
+ do
+ case "$arg" in
+ -*) rm="$rm $arg" ;;
+ *) files="$files $arg" ;;
+ esac
+ done
+
+ if test -z "$rm"; then
+ $echo "$modename: you must specify an RM program" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ fi
+
+ for file in $files; do
+ dir=`$echo "X$file" | $Xsed -e 's%/[^/]*$%%'`
+ test "X$dir" = "X$file" && dir=.
+ name=`$echo "X$file" | $Xsed -e 's%^.*/%%'`
+
+ rmfiles="$file"
+
+ case "$name" in
+ *.la)
+ # Possibly a libtool archive, so verify it.
+ if (sed -e '2q' $file | egrep '^# Generated by ltmain\.sh') >/dev/null 2>&1; then
+ . $dir/$name
+
+ # Delete the libtool libraries and symlinks.
+ for n in $library_names; do
+ rmfiles="$rmfiles $dir/$n"
+ test "X$n" = "X$dlname" && dlname=
+ done
+ test -n "$dlname" && rmfiles="$rmfiles $dir/$dlname"
+ test -n "$old_library" && rmfiles="$rmfiles $dir/$old_library"
+
+ $show "$rm $rmfiles"
+ $run $rm $rmfiles
+
+ if test -n "$library_names"; then
+ # Do each command in the postuninstall commands.
+ eval cmds=\"$postuninstall_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd"
+ done
+ IFS="$save_ifs"
+ fi
+
+ if test -n "$old_library"; then
+ # Do each command in the old_postuninstall commands.
+ eval cmds=\"$old_postuninstall_cmds\"
+ IFS="${IFS= }"; save_ifs="$IFS"; IFS=';'
+ for cmd in $cmds; do
+ IFS="$save_ifs"
+ $show "$cmd"
+ $run eval "$cmd"
+ done
+ IFS="$save_ifs"
+ fi
+
+ # FIXME: should reinstall the best remaining shared library.
+ fi
+ ;;
+
+ *.lo)
+ if test "$build_old_libs" = yes; then
+ oldobj=`$echo "X$name" | $Xsed -e 's/\.lo$/\.o/'`
+ rmfiles="$rmfiles $dir/$oldobj"
+ fi
+ $show "$rm $rmfiles"
+ $run $rm $rmfiles
+ ;;
+
+ *)
+ $show "$rm $rmfiles"
+ $run $rm $rmfiles
+ ;;
+ esac
+ done
+ exit 0
+ ;;
+
+ "")
+ $echo "$modename: you must specify a MODE" 1>&2
+ $echo "$generic_help" 1>&2
+ exit 1
+ ;;
+ esac
+
+ $echo "$modename: invalid operation mode \`$mode'" 1>&2
+ $echo "$generic_help" 1>&2
+ exit 1
+fi # test -z "$show_help"
+
+# We need to display help for each of the modes.
+case "$mode" in
+"") $echo \
+"Usage: $modename [OPTION]... [MODE-ARG]...
+
+Provide generalized library-building support services.
+
+-n, --dry-run display commands without modifying any files
+ --features display configuration information and exit
+ --finish same as \`--mode=finish'
+ --help display this help message and exit
+ --mode=MODE use operation mode MODE [default=inferred from MODE-ARGS]
+ --quiet same as \`--silent'
+ --silent don't print informational messages
+ --version print version information
+
+MODE must be one of the following:
+
+ compile compile a source file into a libtool object
+ execute automatically set library path, then run a program
+ finish complete the installation of libtool libraries
+ install install libraries or executables
+ link create a library or an executable
+ uninstall remove libraries from an installed directory
+
+MODE-ARGS vary depending on the MODE. Try \`$modename --help --mode=MODE' for
+a more detailed description of MODE."
+ exit 0
+ ;;
+
+compile)
+ $echo \
+"Usage: $modename [OPTION]... --mode=compile COMPILE-COMMAND... SOURCEFILE
+
+Compile a source file into a libtool library object.
+
+COMPILE-COMMAND is a command to be used in creating a \`standard' object file
+from the given SOURCEFILE.
+
+The output file name is determined by removing the directory component from
+SOURCEFILE, then substituting the C source code suffix \`.c' with the
+library object suffix, \`.lo'."
+ ;;
+
+execute)
+ $echo \
+"Usage: $modename [OPTION]... --mode=execute COMMAND [ARGS]...
+
+Automatically set library path, then run a program.
+
+This mode accepts the following additional options:
+
+ -dlopen FILE add the directory containing FILE to the library path
+
+This mode sets the library path environment variable according to \`-dlopen'
+flags.
+
+If any of the ARGS are libtool executable wrappers, then they are translated
+into their corresponding uninstalled binary, and any of their required library
+directories are added to the library path.
+
+Then, COMMAND is executed, with ARGS as arguments."
+ ;;
+
+finish)
+ $echo \
+"Usage: $modename [OPTION]... --mode=finish [LIBDIR]...
+
+Complete the installation of libtool libraries.
+
+Each LIBDIR is a directory that contains libtool libraries.
+
+The commands that this mode executes may require superuser privileges. Use
+the \`--dry-run' option if you just want to see what would be executed."
+ ;;
+
+install)
+ $echo \
+"Usage: $modename [OPTION]... --mode=install INSTALL-COMMAND...
+
+Install executables or libraries.
+
+INSTALL-COMMAND is the installation command. The first component should be
+either the \`install' or \`cp' program.
+
+The rest of the components are interpreted as arguments to that command (only
+BSD-compatible install options are recognized)."
+ ;;
+
+link)
+ $echo \
+"Usage: $modename [OPTION]... --mode=link LINK-COMMAND...
+
+Link object files or libraries together to form another library, or to
+create an executable program.
+
+LINK-COMMAND is a command using the C compiler that you would use to create
+a program from several object files.
+
+The following components of LINK-COMMAND are treated specially:
+
+ -all-static do not do any dynamic linking at all
+ -dlopen FILE \`-dlpreopen' FILE if it cannot be dlopened at runtime
+ -dlpreopen FILE link in FILE and add its symbols to dld_preloaded_symbols
+ -export-dynamic allow symbols from OUTPUT-FILE to be resolved with dlsym(3)
+ -LLIBDIR search LIBDIR for required installed libraries
+ -lNAME OUTPUT-FILE requires the installed library libNAME
+ -no-undefined declare that a library does not refer to external symbols
+ -o OUTPUT-FILE create OUTPUT-FILE from the specified objects
+ -release RELEASE specify package release information
+ -rpath LIBDIR the created library will eventually be installed in LIBDIR
+ -static do not do any dynamic linking of libtool libraries
+ -version-info CURRENT[:REVISION[:AGE]]
+ specify library version info [each variable defaults to 0]
+
+All other options (arguments beginning with \`-') are ignored.
+
+Every other argument is treated as a filename. Files ending in \`.la' are
+treated as uninstalled libtool libraries, other files are standard or library
+object files.
+
+If the OUTPUT-FILE ends in \`.la', then a libtool library is created, only
+library objects (\`.lo' files) may be specified, and \`-rpath' is required.
+
+If OUTPUT-FILE ends in \`.a', then a standard library is created using \`ar'
+and \`ranlib'.
+
+If OUTPUT-FILE ends in \`.lo' or \`.o', then a reloadable object file is
+created, otherwise an executable program is created."
+ ;;
+
+uninstall)
+ $echo
+"Usage: $modename [OPTION]... --mode=uninstall RM [RM-OPTION]... FILE...
+
+Remove libraries from an installation directory.
+
+RM is the name of the program to use to delete files associated with each FILE
+(typically \`/bin/rm'). RM-OPTIONS are options (such as \`-f') to be passed
+to RM.
+
+If FILE is a libtool library, all the files associated with it are deleted.
+Otherwise, only FILE itself is deleted using RM."
+ ;;
+
+*)
+ $echo "$modename: invalid operation mode \`$mode'" 1>&2
+ $echo "$help" 1>&2
+ exit 1
+ ;;
+esac
+
+echo
+$echo "Try \`$modename --help' for more information about other modes."
+
+exit 0
+
+# Local Variables:
+# mode:shell-script
+# sh-indentation:2
+# End:
diff --git a/test/monniaux/jpeg-6b/makcjpeg.st b/test/monniaux/jpeg-6b/makcjpeg.st
new file mode 100644
index 00000000..fc72c898
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makcjpeg.st
@@ -0,0 +1,38 @@
+; Project file for Independent JPEG Group's software
+;
+; This project file is for Atari ST/STE/TT systems using Pure C or Turbo C.
+; Thanks to Frank Moehle (Frank.Moehle@arbi.informatik.uni-oldenburg.de),
+; Dr. B. Setzepfandt (bernd@gina.uni-muenster.de),
+; and Guido Vollbeding (guivol@esc.de).
+;
+; To use this file, rename it to cjpeg.prj.
+; If you are using Turbo C, change filenames beginning with "pc..." to "tc..."
+; Read installation instructions before trying to make the program!
+;
+;
+; * * * Output file * * *
+cjpeg.ttp
+;
+; * * * COMPILER OPTIONS * * *
+.C[-P] ; absolute calls
+.C[-M] ; and no string merging, folks
+.C[-w-cln] ; no "constant is long" warnings
+.C[-w-par] ; no "parameter xxxx unused"
+.C[-w-rch] ; no "unreachable code"
+.C[-wsig] ; warn if significant digits may be lost
+=
+; * * * * List of modules * * * *
+pcstart.o
+cjpeg.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h,jversion.h)
+cdjpeg.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdswitch.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdppm.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdgif.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdtarga.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdbmp.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdrle.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+libjpeg.lib ; built by libjpeg.prj
+pcfltlib.lib ; floating point library
+; the float library can be omitted if you've turned off DCT_FLOAT_SUPPORTED
+pcstdlib.lib ; standard library
+pcextlib.lib ; extended library
diff --git a/test/monniaux/jpeg-6b/makdjpeg.st b/test/monniaux/jpeg-6b/makdjpeg.st
new file mode 100644
index 00000000..32267263
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makdjpeg.st
@@ -0,0 +1,38 @@
+; Project file for Independent JPEG Group's software
+;
+; This project file is for Atari ST/STE/TT systems using Pure C or Turbo C.
+; Thanks to Frank Moehle (Frank.Moehle@arbi.informatik.uni-oldenburg.de),
+; Dr. B. Setzepfandt (bernd@gina.uni-muenster.de),
+; and Guido Vollbeding (guivol@esc.de).
+;
+; To use this file, rename it to djpeg.prj.
+; If you are using Turbo C, change filenames beginning with "pc..." to "tc..."
+; Read installation instructions before trying to make the program!
+;
+;
+; * * * Output file * * *
+djpeg.ttp
+;
+; * * * COMPILER OPTIONS * * *
+.C[-P] ; absolute calls
+.C[-M] ; and no string merging, folks
+.C[-w-cln] ; no "constant is long" warnings
+.C[-w-par] ; no "parameter xxxx unused"
+.C[-w-rch] ; no "unreachable code"
+.C[-wsig] ; warn if significant digits may be lost
+=
+; * * * * List of modules * * * *
+pcstart.o
+djpeg.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h,jversion.h)
+cdjpeg.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdcolmap.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrppm.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrgif.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrtarga.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrbmp.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrrle.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+libjpeg.lib ; built by libjpeg.prj
+pcfltlib.lib ; floating point library
+; the float library can be omitted if you've turned off DCT_FLOAT_SUPPORTED
+pcstdlib.lib ; standard library
+pcextlib.lib ; extended library
diff --git a/test/monniaux/jpeg-6b/makeapps.ds b/test/monniaux/jpeg-6b/makeapps.ds
new file mode 100644
index 00000000..bedd038a
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makeapps.ds
@@ -0,0 +1,828 @@
+# Microsoft Developer Studio Generated NMAKE File, Format Version 4.20
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+!IF "$(CFG)" == ""
+CFG=cjpeg - Win32
+!MESSAGE No configuration specified. Defaulting to cjpeg - Win32.
+!ENDIF
+
+!IF "$(CFG)" != "cjpeg - Win32" && "$(CFG)" != "djpeg - Win32" &&\
+ "$(CFG)" != "jpegtran - Win32" && "$(CFG)" != "rdjpgcom - Win32" &&\
+ "$(CFG)" != "wrjpgcom - Win32"
+!MESSAGE Invalid configuration "$(CFG)" specified.
+!MESSAGE You can specify a configuration when running NMAKE on this makefile
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "apps.mak" CFG="cjpeg - Win32"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "cjpeg - Win32" (based on "Win32 (x86) Console Application")
+!MESSAGE "djpeg - Win32" (based on "Win32 (x86) Console Application")
+!MESSAGE "jpegtran - Win32" (based on "Win32 (x86) Console Application")
+!MESSAGE "rdjpgcom - Win32" (based on "Win32 (x86) Console Application")
+!MESSAGE "wrjpgcom - Win32" (based on "Win32 (x86) Console Application")
+!MESSAGE
+!ERROR An invalid configuration is specified.
+!ENDIF
+
+!IF "$(OS)" == "Windows_NT"
+NULL=
+!ELSE
+NULL=nul
+!ENDIF
+################################################################################
+# Begin Project
+# PROP Target_Last_Scanned "cjpeg - Win32"
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "cjpeg - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "cjpeg\Release"
+# PROP BASE Intermediate_Dir "cjpeg\Release"
+# PROP BASE Target_Dir "cjpeg"
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "cjpeg\Release"
+# PROP Intermediate_Dir "cjpeg\Release"
+# PROP Target_Dir "cjpeg"
+OUTDIR=.\cjpeg\Release
+INTDIR=.\cjpeg\Release
+
+ALL : "$(OUTDIR)\cjpeg.exe"
+
+CLEAN :
+ -@erase "$(INTDIR)\cjpeg.obj"
+ -@erase "$(INTDIR)\rdppm.obj"
+ -@erase "$(INTDIR)\rdgif.obj"
+ -@erase "$(INTDIR)\rdtarga.obj"
+ -@erase "$(INTDIR)\rdrle.obj"
+ -@erase "$(INTDIR)\rdbmp.obj"
+ -@erase "$(INTDIR)\rdswitch.obj"
+ -@erase "$(INTDIR)\cdjpeg.obj"
+ -@erase "$(OUTDIR)\cjpeg.exe"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
+ /Fp"$(INTDIR)/cjpeg.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\cjpeg\Release/
+CPP_SBRS=.\.
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/cjpeg.bsc"
+BSC32_SBRS= \
+
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+LINK32_FLAGS=Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib\
+ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib\
+ odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:no\
+ /pdb:"$(OUTDIR)/cjpeg.pdb" /machine:I386 /out:"$(OUTDIR)/cjpeg.exe"
+LINK32_OBJS= \
+ "$(INTDIR)\cjpeg.obj" \
+ "$(INTDIR)\rdppm.obj" \
+ "$(INTDIR)\rdgif.obj" \
+ "$(INTDIR)\rdtarga.obj" \
+ "$(INTDIR)\rdrle.obj" \
+ "$(INTDIR)\rdbmp.obj" \
+ "$(INTDIR)\rdswitch.obj" \
+ "$(INTDIR)\cdjpeg.obj" \
+
+
+"$(OUTDIR)\cjpeg.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
+ $(LINK32) @<<
+ $(LINK32_FLAGS) $(LINK32_OBJS)
+<<
+
+!ELSEIF "$(CFG)" == "djpeg - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "djpeg\Release"
+# PROP BASE Intermediate_Dir "djpeg\Release"
+# PROP BASE Target_Dir "djpeg"
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "djpeg\Release"
+# PROP Intermediate_Dir "djpeg\Release"
+# PROP Target_Dir "djpeg"
+OUTDIR=.\djpeg\Release
+INTDIR=.\djpeg\Release
+
+ALL : "$(OUTDIR)\djpeg.exe"
+
+CLEAN :
+ -@erase "$(INTDIR)\djpeg.obj"
+ -@erase "$(INTDIR)\wrppm.obj"
+ -@erase "$(INTDIR)\wrgif.obj"
+ -@erase "$(INTDIR)\wrtarga.obj"
+ -@erase "$(INTDIR)\wrrle.obj"
+ -@erase "$(INTDIR)\wrbmp.obj"
+ -@erase "$(INTDIR)\rdcolmap.obj"
+ -@erase "$(INTDIR)\cdjpeg.obj"
+ -@erase "$(OUTDIR)\djpeg.exe"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
+ /Fp"$(INTDIR)/djpeg.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\djpeg\Release/
+CPP_SBRS=.\.
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/djpeg.bsc"
+BSC32_SBRS= \
+
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+LINK32_FLAGS=Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib\
+ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib\
+ odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:no\
+ /pdb:"$(OUTDIR)/djpeg.pdb" /machine:I386 /out:"$(OUTDIR)/djpeg.exe"
+LINK32_OBJS= \
+ "$(INTDIR)\djpeg.obj" \
+ "$(INTDIR)\wrppm.obj" \
+ "$(INTDIR)\wrgif.obj" \
+ "$(INTDIR)\wrtarga.obj" \
+ "$(INTDIR)\wrrle.obj" \
+ "$(INTDIR)\wrbmp.obj" \
+ "$(INTDIR)\rdcolmap.obj" \
+ "$(INTDIR)\cdjpeg.obj" \
+
+
+"$(OUTDIR)\djpeg.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
+ $(LINK32) @<<
+ $(LINK32_FLAGS) $(LINK32_OBJS)
+<<
+
+!ELSEIF "$(CFG)" == "jpegtran - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "jpegtran\Release"
+# PROP BASE Intermediate_Dir "jpegtran\Release"
+# PROP BASE Target_Dir "jpegtran"
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "jpegtran\Release"
+# PROP Intermediate_Dir "jpegtran\Release"
+# PROP Target_Dir "jpegtran"
+OUTDIR=.\jpegtran\Release
+INTDIR=.\jpegtran\Release
+
+ALL : "$(OUTDIR)\jpegtran.exe"
+
+CLEAN :
+ -@erase "$(INTDIR)\jpegtran.obj"
+ -@erase "$(INTDIR)\rdswitch.obj"
+ -@erase "$(INTDIR)\cdjpeg.obj"
+ -@erase "$(INTDIR)\transupp.obj"
+ -@erase "$(OUTDIR)\jpegtran.exe"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
+ /Fp"$(INTDIR)/jpegtran.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\jpegtran\Release/
+CPP_SBRS=.\.
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/jpegtran.bsc"
+BSC32_SBRS= \
+
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+LINK32_FLAGS=Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib\
+ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib\
+ odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:no\
+ /pdb:"$(OUTDIR)/jpegtran.pdb" /machine:I386 /out:"$(OUTDIR)/jpegtran.exe"
+LINK32_OBJS= \
+ "$(INTDIR)\jpegtran.obj" \
+ "$(INTDIR)\rdswitch.obj" \
+ "$(INTDIR)\cdjpeg.obj" \
+ "$(INTDIR)\transupp.obj" \
+
+
+"$(OUTDIR)\jpegtran.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
+ $(LINK32) @<<
+ $(LINK32_FLAGS) $(LINK32_OBJS)
+<<
+
+!ELSEIF "$(CFG)" == "rdjpgcom - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "rdjpgcom\Release"
+# PROP BASE Intermediate_Dir "rdjpgcom\Release"
+# PROP BASE Target_Dir "rdjpgcom"
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "rdjpgcom\Release"
+# PROP Intermediate_Dir "rdjpgcom\Release"
+# PROP Target_Dir "rdjpgcom"
+OUTDIR=.\rdjpgcom\Release
+INTDIR=.\rdjpgcom\Release
+
+ALL : "$(OUTDIR)\rdjpgcom.exe"
+
+CLEAN :
+ -@erase "$(INTDIR)\rdjpgcom.obj"
+ -@erase "$(OUTDIR)\rdjpgcom.exe"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
+ /Fp"$(INTDIR)/rdjpgcom.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\rdjpgcom\Release/
+CPP_SBRS=.\.
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/rdjpgcom.bsc"
+BSC32_SBRS= \
+
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+LINK32_FLAGS=Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib\
+ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib\
+ odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:no\
+ /pdb:"$(OUTDIR)/rdjpgcom.pdb" /machine:I386 /out:"$(OUTDIR)/rdjpgcom.exe"
+LINK32_OBJS= \
+ "$(INTDIR)\rdjpgcom.obj"
+
+"$(OUTDIR)\rdjpgcom.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
+ $(LINK32) @<<
+ $(LINK32_FLAGS) $(LINK32_OBJS)
+<<
+
+!ELSEIF "$(CFG)" == "wrjpgcom - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "wrjpgcom\Release"
+# PROP BASE Intermediate_Dir "wrjpgcom\Release"
+# PROP BASE Target_Dir "wrjpgcom"
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "wrjpgcom\Release"
+# PROP Intermediate_Dir "wrjpgcom\Release"
+# PROP Target_Dir "wrjpgcom"
+OUTDIR=.\wrjpgcom\Release
+INTDIR=.\wrjpgcom\Release
+
+ALL : "$(OUTDIR)\wrjpgcom.exe"
+
+CLEAN :
+ -@erase "$(INTDIR)\wrjpgcom.obj"
+ -@erase "$(OUTDIR)\wrjpgcom.exe"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
+ /Fp"$(INTDIR)/wrjpgcom.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\wrjpgcom\Release/
+CPP_SBRS=.\.
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/wrjpgcom.bsc"
+BSC32_SBRS= \
+
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+LINK32_FLAGS=Release\jpeg.lib kernel32.lib user32.lib gdi32.lib winspool.lib\
+ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib\
+ odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:no\
+ /pdb:"$(OUTDIR)/wrjpgcom.pdb" /machine:I386 /out:"$(OUTDIR)/wrjpgcom.exe"
+LINK32_OBJS= \
+ "$(INTDIR)\wrjpgcom.obj"
+
+"$(OUTDIR)\wrjpgcom.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
+ $(LINK32) @<<
+ $(LINK32_FLAGS) $(LINK32_OBJS)
+<<
+
+!ENDIF
+
+.c{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.cpp{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.cxx{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.c{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+.cpp{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+.cxx{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+################################################################################
+# Begin Target
+
+# Name "cjpeg - Win32"
+
+!IF "$(CFG)" == "cjpeg - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="cjpeg.c"
+DEP_CPP_CJPEG=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+ "jversion.h"\
+
+
+"$(INTDIR)\cjpeg.obj" : $(SOURCE) $(DEP_CPP_CJPEG) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="cdjpeg.c"
+DEP_CPP_CDJPE=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\cdjpeg.obj" : $(SOURCE) $(DEP_CPP_CDJPE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdswitch.c"
+DEP_CPP_RDSWI=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdswitch.obj" : $(SOURCE) $(DEP_CPP_RDSWI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdppm.c"
+DEP_CPP_RDPPM=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdppm.obj" : $(SOURCE) $(DEP_CPP_RDPPM) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdgif.c"
+DEP_CPP_RDGIF=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdgif.obj" : $(SOURCE) $(DEP_CPP_RDGIF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdtarga.c"
+DEP_CPP_RDTAR=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdtarga.obj" : $(SOURCE) $(DEP_CPP_RDTAR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdbmp.c"
+DEP_CPP_RDBMP=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdbmp.obj" : $(SOURCE) $(DEP_CPP_RDBMP) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdrle.c"
+DEP_CPP_RDRLE=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdrle.obj" : $(SOURCE) $(DEP_CPP_RDRLE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+################################################################################
+# Begin Target
+
+# Name "djpeg - Win32"
+
+!IF "$(CFG)" == "djpeg - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="djpeg.c"
+DEP_CPP_DJPEG=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+ "jversion.h"\
+
+
+"$(INTDIR)\djpeg.obj" : $(SOURCE) $(DEP_CPP_DJPEG) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="cdjpeg.c"
+DEP_CPP_CDJPE=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\cdjpeg.obj" : $(SOURCE) $(DEP_CPP_CDJPE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdcolmap.c"
+DEP_CPP_RDCOL=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdcolmap.obj" : $(SOURCE) $(DEP_CPP_RDCOL) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="wrppm.c"
+DEP_CPP_WRPPM=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\wrppm.obj" : $(SOURCE) $(DEP_CPP_WRPPM) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="wrgif.c"
+DEP_CPP_WRGIF=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\wrgif.obj" : $(SOURCE) $(DEP_CPP_WRGIF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="wrtarga.c"
+DEP_CPP_WRTAR=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\wrtarga.obj" : $(SOURCE) $(DEP_CPP_WRTAR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="wrbmp.c"
+DEP_CPP_WRBMP=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\wrbmp.obj" : $(SOURCE) $(DEP_CPP_WRBMP) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="wrrle.c"
+DEP_CPP_WRRLE=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\wrrle.obj" : $(SOURCE) $(DEP_CPP_WRRLE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+################################################################################
+# Begin Target
+
+# Name "jpegtran - Win32"
+
+!IF "$(CFG)" == "jpegtran - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="jpegtran.c"
+DEP_CPP_JPEGT=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+ "transupp.h"\
+ "jversion.h"\
+
+
+"$(INTDIR)\jpegtran.obj" : $(SOURCE) $(DEP_CPP_JPEGT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="cdjpeg.c"
+DEP_CPP_CDJPE=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\cdjpeg.obj" : $(SOURCE) $(DEP_CPP_CDJPE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="rdswitch.c"
+DEP_CPP_RDSWI=\
+ "cdjpeg.h"\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+ "cderror.h"\
+
+
+"$(INTDIR)\rdswitch.obj" : $(SOURCE) $(DEP_CPP_RDSWI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="transupp.c"
+DEP_CPP_TRANS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "transupp.h"\
+
+
+"$(INTDIR)\transupp.obj" : $(SOURCE) $(DEP_CPP_TRANS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+################################################################################
+# Begin Target
+
+# Name "rdjpgcom - Win32"
+
+!IF "$(CFG)" == "rdjpgcom - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="rdjpgcom.c"
+DEP_CPP_RDJPG=\
+ "jinclude.h"\
+ "jconfig.h"\
+
+
+"$(INTDIR)\rdjpgcom.obj" : $(SOURCE) $(DEP_CPP_RDJPG) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+################################################################################
+# Begin Target
+
+# Name "wrjpgcom - Win32"
+
+!IF "$(CFG)" == "wrjpgcom - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="wrjpgcom.c"
+DEP_CPP_WRJPG=\
+ "jinclude.h"\
+ "jconfig.h"\
+
+
+"$(INTDIR)\wrjpgcom.obj" : $(SOURCE) $(DEP_CPP_WRJPG) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+# End Project
+################################################################################
+
diff --git a/test/monniaux/jpeg-6b/makefile.ansi b/test/monniaux/jpeg-6b/makefile.ansi
new file mode 100644
index 00000000..82919135
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.ansi
@@ -0,0 +1,214 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is suitable for Unix-like systems with ANSI-capable compilers.
+# If you have a non-ANSI compiler, makefile.unix is a better starting point.
+
+# Read installation instructions before saying "make" !!
+
+# The name of your C compiler:
+CC= cc
+
+# You may need to adjust these cc options:
+CFLAGS= -O
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time cc options:
+LDFLAGS=
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS=
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= $(CC)
+# file deletion command
+RM= rm -f
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= ranlib
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.o jcapistd.o jctrans.o jcparam.o jdatadst.o jcinit.o \
+ jcmaster.o jcmarker.o jcmainct.o jcprepct.o jccoefct.o jccolor.o \
+ jcsample.o jchuff.o jcphuff.o jcdctmgr.o jfdctfst.o jfdctflt.o \
+ jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapimin.o jdapistd.o jdtrans.o jdatasrc.o jdmaster.o \
+ jdinput.o jdmarker.o jdhuff.o jdphuff.o jdmainct.o jdcoefct.o \
+ jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+ jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o rdswitch.o \
+ cdjpeg.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o \
+ cdjpeg.o
+TROBJECTS= jpegtran.o rdswitch.o cdjpeg.o transupp.o
+
+
+all: libjpeg.a cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+
+libjpeg.a: $(LIBOBJECTS)
+ $(RM) libjpeg.a
+ $(AR) libjpeg.a $(LIBOBJECTS)
+ $(AR2) libjpeg.a
+
+cjpeg: $(COBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+jpegtran: $(TROBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o jpegtran $(TROBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+ $(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+ $(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ $(RM) *.o cjpeg djpeg jpegtran libjpeg.a rdjpgcom wrjpgcom
+ $(RM) core testout*
+
+test: cjpeg djpeg jpegtran
+ $(RM) testout*
+ ./djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ ./cjpeg -dct int -outfile testout.jpg testimg.ppm
+ ./djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ ./jpegtran -outfile testoutt.jpg testprog.jpg
+ cmp testimg.ppm testout.ppm
+ cmp testimg.bmp testout.bmp
+ cmp testimg.jpg testout.jpg
+ cmp testimg.ppm testoutp.ppm
+ cmp testimgp.jpg testoutp.jpg
+ cmp testorig.jpg testoutt.jpg
+
+
+jcapimin.o: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.o: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.o: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.o: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.o: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.o: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.o: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.o: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.o: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.o: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.o: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.o: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.o: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.o: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.o: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.o: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.o: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.o: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.o: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.o: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.o: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.o: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.o: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.o: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.o: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.bcc b/test/monniaux/jpeg-6b/makefile.bcc
new file mode 100644
index 00000000..a1cfcde6
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.bcc
@@ -0,0 +1,285 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is suitable for Borland C on MS-DOS or OS/2.
+# It works with Borland C++ for DOS, revision 3.0 or later,
+# and has been tested with Borland C++ for OS/2.
+# Watch out for optimization bugs in the OS/2 compilers --- see notes below!
+# Thanks to Tom Wright and Ge' Weijers (original DOS) and
+# Ken Porter (OS/2) for this file.
+
+# Read installation instructions before saying "make" !!
+
+# Are we under DOS or OS/2?
+!if !$d(DOS) && !$d(OS2)
+!if $d(__OS2__)
+OS2=1
+!else
+DOS=1
+!endif
+!endif
+
+# The name of your C compiler:
+CC= bcc
+
+# You may need to adjust these cc options:
+!if $d(DOS)
+CFLAGS= -O2 -mm -w-par -w-stu -w-ccc -w-rch
+!else
+CFLAGS= -O1 -w-par -w-stu -w-ccc -w-rch
+!endif
+# -O2 enables full code optimization (for pre-3.0 Borland C++, use -O -G -Z).
+# -O2 is buggy in Borland OS/2 C++ revision 2.0, so use -O1 there for now.
+# If you have Borland OS/2 C++ revision 1.0, use -O or no optimization at all.
+# -mm selects medium memory model (near data, far code pointers; DOS only!)
+# -w-par suppresses warnings about unused function parameters
+# -w-stu suppresses warnings about incomplete structures
+# -w-ccc suppresses warnings about compile-time-constant conditions
+# -w-rch suppresses warnings about unreachable code
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time cc options:
+!if $d(DOS)
+LDFLAGS= -mm
+# memory model option here must match CFLAGS!
+!else
+LDFLAGS=
+# -lai full-screen app
+# -lc case-significant link
+!endif
+
+# Put here the object file name for the correct system-dependent memory
+# manager file.
+# For DOS, we recommend jmemdos.c and jmemdosa.asm.
+# For OS/2, we recommend jmemnobs.c (flat memory!)
+# SYSDEPMEMLIB must list the same files with "+" signs for the librarian.
+!if $d(DOS)
+SYSDEPMEM= jmemdos.obj jmemdosa.obj
+SYSDEPMEMLIB= +jmemdos.obj +jmemdosa.obj
+!else
+SYSDEPMEM= jmemnobs.obj
+SYSDEPMEMLIB= +jmemnobs.obj
+!endif
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.obj jcapistd.obj jctrans.obj jcparam.obj jdatadst.obj \
+ jcinit.obj jcmaster.obj jcmarker.obj jcmainct.obj jcprepct.obj \
+ jccoefct.obj jccolor.obj jcsample.obj jchuff.obj jcphuff.obj \
+ jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapimin.obj jdapistd.obj jdtrans.obj jdatasrc.obj \
+ jdmaster.obj jdinput.obj jdmarker.obj jdhuff.obj jdphuff.obj \
+ jdmainct.obj jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj \
+ jidctflt.obj jidctint.obj jidctred.obj jdsample.obj jdcolor.obj \
+ jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj \
+ rdswitch.obj cdjpeg.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+ rdcolmap.obj cdjpeg.obj
+TROBJECTS= jpegtran.obj rdswitch.obj cdjpeg.obj transupp.obj
+
+
+all: libjpeg.lib cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+
+libjpeg.lib: $(LIBOBJECTS)
+ - del libjpeg.lib
+ tlib libjpeg.lib /E /C @&&|
++jcapimin.obj +jcapistd.obj +jctrans.obj +jcparam.obj +jdatadst.obj &
++jcinit.obj +jcmaster.obj +jcmarker.obj +jcmainct.obj +jcprepct.obj &
++jccoefct.obj +jccolor.obj +jcsample.obj +jchuff.obj +jcphuff.obj &
++jcdctmgr.obj +jfdctfst.obj +jfdctflt.obj +jfdctint.obj +jdapimin.obj &
++jdapistd.obj +jdtrans.obj +jdatasrc.obj +jdmaster.obj +jdinput.obj &
++jdmarker.obj +jdhuff.obj +jdphuff.obj +jdmainct.obj +jdcoefct.obj &
++jdpostct.obj +jddctmgr.obj +jidctfst.obj +jidctflt.obj +jidctint.obj &
++jidctred.obj +jdsample.obj +jdcolor.obj +jquant1.obj +jquant2.obj &
++jdmerge.obj +jcomapi.obj +jutils.obj +jerror.obj +jmemmgr.obj &
+$(SYSDEPMEMLIB)
+|
+
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) -ecjpeg.exe $(COBJECTS) libjpeg.lib
+
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) -edjpeg.exe $(DOBJECTS) libjpeg.lib
+
+jpegtran.exe: $(TROBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) -ejpegtran.exe $(TROBJECTS) libjpeg.lib
+
+rdjpgcom.exe: rdjpgcom.c
+!if $d(DOS)
+ $(CC) -ms -O rdjpgcom.c
+!else
+ $(CC) $(CFLAGS) rdjpgcom.c
+!endif
+
+# On DOS, wrjpgcom needs large model so it can malloc a 64K chunk
+wrjpgcom.exe: wrjpgcom.c
+!if $d(DOS)
+ $(CC) -ml -O wrjpgcom.c
+!else
+ $(CC) $(CFLAGS) wrjpgcom.c
+!endif
+
+# This "{}" syntax allows Borland Make to "batch" source files.
+# In this way, each run of the compiler can build many modules.
+.c.obj:
+ $(CC) $(CFLAGS) -c{ $<}
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ - del *.obj
+ - del libjpeg.lib
+ - del cjpeg.exe
+ - del djpeg.exe
+ - del jpegtran.exe
+ - del rdjpgcom.exe
+ - del wrjpgcom.exe
+ - del testout*.*
+
+test: cjpeg.exe djpeg.exe jpegtran.exe
+ - del testout*.*
+ djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ cjpeg -dct int -outfile testout.jpg testimg.ppm
+ djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ jpegtran -outfile testoutt.jpg testprog.jpg
+!if $d(DOS)
+ fc /b testimg.ppm testout.ppm
+ fc /b testimg.bmp testout.bmp
+ fc /b testimg.jpg testout.jpg
+ fc /b testimg.ppm testoutp.ppm
+ fc /b testimgp.jpg testoutp.jpg
+ fc /b testorig.jpg testoutt.jpg
+!else
+ echo n > n.tmp
+ comp testimg.ppm testout.ppm < n.tmp
+ comp testimg.bmp testout.bmp < n.tmp
+ comp testimg.jpg testout.jpg < n.tmp
+ comp testimg.ppm testoutp.ppm < n.tmp
+ comp testimgp.jpg testoutp.jpg < n.tmp
+ comp testorig.jpg testoutt.jpg < n.tmp
+ del n.tmp
+!endif
+
+
+jcapimin.obj: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.obj: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.obj: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.obj: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.obj: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.obj: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.obj: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.obj: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.obj: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.obj: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.obj: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.obj: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.obj: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.obj: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.obj: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.obj: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.obj: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.obj: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.obj: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.obj: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.obj: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.obj: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.obj: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.obj: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.obj: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+jmemdosa.obj: jmemdosa.asm
+ tasm /mx jmemdosa.asm
diff --git a/test/monniaux/jpeg-6b/makefile.cfg b/test/monniaux/jpeg-6b/makefile.cfg
new file mode 100644
index 00000000..f25e42e3
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.cfg
@@ -0,0 +1,319 @@
+# Makefile for Independent JPEG Group's software
+
+# makefile.cfg is edited by configure to produce a custom Makefile.
+
+# Read installation instructions before saying "make" !!
+
+# For compiling with source and object files in different directories.
+srcdir = @srcdir@
+VPATH = @srcdir@
+
+# Where to install the programs and man pages.
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+bindir = $(exec_prefix)/bin
+libdir = $(exec_prefix)/lib
+includedir = $(prefix)/include
+binprefix =
+manprefix =
+manext = 1
+mandir = $(prefix)/man/man$(manext)
+
+# The name of your C compiler:
+CC= @CC@
+
+# You may need to adjust these cc options:
+CFLAGS= @CFLAGS@ @CPPFLAGS@ @INCLUDEFLAGS@
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+# However, any special defines for ansi2knr.c may be included here:
+ANSI2KNRFLAGS= @ANSI2KNRFLAGS@
+
+# Link-time cc options:
+LDFLAGS= @LDFLAGS@
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS= @LIBS@
+
+# If using GNU libtool, LIBTOOL references it; if not, LIBTOOL is empty.
+LIBTOOL = @LIBTOOL@
+# $(O) expands to "lo" if using libtool, plain "o" if not.
+# Similarly, $(A) expands to "la" or "a".
+O = @O@
+A = @A@
+
+# Library version ID; libtool uses this for the shared library version number.
+# Note: we suggest this match the macro of the same name in jpeglib.h.
+JPEG_LIB_VERSION = @JPEG_LIB_VERSION@
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= @MEMORYMGR@
+
+# miscellaneous OS-dependent stuff
+SHELL= /bin/sh
+# linker
+LN= @LN@
+# file deletion command
+RM= rm -f
+# directory creation command
+MKDIR= mkdir
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= @RANLIB@
+# installation program
+INSTALL= @INSTALL@
+INSTALL_PROGRAM= @INSTALL_PROGRAM@
+INSTALL_LIB= @INSTALL_LIB@
+INSTALL_DATA= @INSTALL_DATA@
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.$(O) jutils.$(O) jerror.$(O) jmemmgr.$(O) $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.$(O) jcapistd.$(O) jctrans.$(O) jcparam.$(O) \
+ jdatadst.$(O) jcinit.$(O) jcmaster.$(O) jcmarker.$(O) jcmainct.$(O) \
+ jcprepct.$(O) jccoefct.$(O) jccolor.$(O) jcsample.$(O) jchuff.$(O) \
+ jcphuff.$(O) jcdctmgr.$(O) jfdctfst.$(O) jfdctflt.$(O) \
+ jfdctint.$(O)
+# decompression library object files
+DLIBOBJECTS= jdapimin.$(O) jdapistd.$(O) jdtrans.$(O) jdatasrc.$(O) \
+ jdmaster.$(O) jdinput.$(O) jdmarker.$(O) jdhuff.$(O) jdphuff.$(O) \
+ jdmainct.$(O) jdcoefct.$(O) jdpostct.$(O) jddctmgr.$(O) \
+ jidctfst.$(O) jidctflt.$(O) jidctint.$(O) jidctred.$(O) \
+ jdsample.$(O) jdcolor.$(O) jquant1.$(O) jquant2.$(O) jdmerge.$(O)
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.$(O) rdppm.$(O) rdgif.$(O) rdtarga.$(O) rdrle.$(O) \
+ rdbmp.$(O) rdswitch.$(O) cdjpeg.$(O)
+DOBJECTS= djpeg.$(O) wrppm.$(O) wrgif.$(O) wrtarga.$(O) wrrle.$(O) \
+ wrbmp.$(O) rdcolmap.$(O) cdjpeg.$(O)
+TROBJECTS= jpegtran.$(O) rdswitch.$(O) cdjpeg.$(O) transupp.$(O)
+
+
+all: @A2K_DEPS@ libjpeg.$(A) cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+
+# Special compilation rules to support ansi2knr and libtool.
+.SUFFIXES: .lo .la
+
+# How to compile with libtool.
+@COM_LT@.c.lo:
+@COM_LT@ $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c $(srcdir)/$*.c
+
+# How to use ansi2knr, when not using libtool.
+@COM_A2K@.c.o:
+@COM_A2K@ ./ansi2knr $(srcdir)/$*.c knr/$*.c
+@COM_A2K@ $(CC) $(CFLAGS) -c knr/$*.c
+@COM_A2K@ $(RM) knr/$*.c
+
+# How to use ansi2knr AND libtool.
+@COM_A2K@.c.lo:
+@COM_A2K@ ./ansi2knr $(srcdir)/$*.c knr/$*.c
+@COM_A2K@ $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c knr/$*.c
+@COM_A2K@ $(RM) knr/$*.c
+
+ansi2knr: ansi2knr.c
+ $(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr $(srcdir)/ansi2knr.c
+ $(MKDIR) knr
+
+# the library:
+
+# without libtool:
+libjpeg.a: @A2K_DEPS@ $(LIBOBJECTS)
+ $(RM) libjpeg.a
+ $(AR) libjpeg.a $(LIBOBJECTS)
+ $(AR2) libjpeg.a
+
+# with libtool:
+libjpeg.la: @A2K_DEPS@ $(LIBOBJECTS)
+ $(LIBTOOL) --mode=link $(CC) -o libjpeg.la $(LIBOBJECTS) \
+ -rpath $(libdir) -version-info $(JPEG_LIB_VERSION)
+
+# sample programs:
+
+cjpeg: $(COBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.$(A) $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.$(A) $(LDLIBS)
+
+jpegtran: $(TROBJECTS) libjpeg.$(A)
+ $(LN) $(LDFLAGS) -o jpegtran $(TROBJECTS) libjpeg.$(A) $(LDLIBS)
+
+rdjpgcom: rdjpgcom.$(O)
+ $(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.$(O) $(LDLIBS)
+
+wrjpgcom: wrjpgcom.$(O)
+ $(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.$(O) $(LDLIBS)
+
+# Installation rules:
+
+install: cjpeg djpeg jpegtran rdjpgcom wrjpgcom @FORCE_INSTALL_LIB@
+ $(INSTALL_PROGRAM) cjpeg $(bindir)/$(binprefix)cjpeg
+ $(INSTALL_PROGRAM) djpeg $(bindir)/$(binprefix)djpeg
+ $(INSTALL_PROGRAM) jpegtran $(bindir)/$(binprefix)jpegtran
+ $(INSTALL_PROGRAM) rdjpgcom $(bindir)/$(binprefix)rdjpgcom
+ $(INSTALL_PROGRAM) wrjpgcom $(bindir)/$(binprefix)wrjpgcom
+ $(INSTALL_DATA) $(srcdir)/cjpeg.1 $(mandir)/$(manprefix)cjpeg.$(manext)
+ $(INSTALL_DATA) $(srcdir)/djpeg.1 $(mandir)/$(manprefix)djpeg.$(manext)
+ $(INSTALL_DATA) $(srcdir)/jpegtran.1 $(mandir)/$(manprefix)jpegtran.$(manext)
+ $(INSTALL_DATA) $(srcdir)/rdjpgcom.1 $(mandir)/$(manprefix)rdjpgcom.$(manext)
+ $(INSTALL_DATA) $(srcdir)/wrjpgcom.1 $(mandir)/$(manprefix)wrjpgcom.$(manext)
+
+install-lib: libjpeg.$(A) install-headers
+ $(INSTALL_LIB) libjpeg.$(A) $(libdir)/$(binprefix)libjpeg.$(A)
+
+install-headers: jconfig.h
+ $(INSTALL_DATA) jconfig.h $(includedir)/jconfig.h
+ $(INSTALL_DATA) $(srcdir)/jpeglib.h $(includedir)/jpeglib.h
+ $(INSTALL_DATA) $(srcdir)/jmorecfg.h $(includedir)/jmorecfg.h
+ $(INSTALL_DATA) $(srcdir)/jerror.h $(includedir)/jerror.h
+
+clean:
+ $(RM) *.o *.lo libjpeg.a libjpeg.la
+ $(RM) cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+ $(RM) ansi2knr core testout* config.log config.status
+ $(RM) -r knr .libs _libs
+
+distclean: clean
+ $(RM) Makefile jconfig.h libtool config.cache
+
+test: cjpeg djpeg jpegtran
+ $(RM) testout*
+ ./djpeg -dct int -ppm -outfile testout.ppm $(srcdir)/testorig.jpg
+ ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp $(srcdir)/testorig.jpg
+ ./cjpeg -dct int -outfile testout.jpg $(srcdir)/testimg.ppm
+ ./djpeg -dct int -ppm -outfile testoutp.ppm $(srcdir)/testprog.jpg
+ ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg $(srcdir)/testimg.ppm
+ ./jpegtran -outfile testoutt.jpg $(srcdir)/testprog.jpg
+ cmp $(srcdir)/testimg.ppm testout.ppm
+ cmp $(srcdir)/testimg.bmp testout.bmp
+ cmp $(srcdir)/testimg.jpg testout.jpg
+ cmp $(srcdir)/testimg.ppm testoutp.ppm
+ cmp $(srcdir)/testimgp.jpg testoutp.jpg
+ cmp $(srcdir)/testorig.jpg testoutt.jpg
+
+check: test
+
+# Mistake catcher:
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+# GNU Make likes to know which target names are not really files to be made:
+.PHONY: all install install-lib install-headers clean distclean test check
+
+
+jcapimin.$(O): jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.$(O): jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.$(O): jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.$(O): jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.$(O): jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.$(O): jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.$(O): jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.$(O): jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.$(O): jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.$(O): jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.$(O): jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.$(O): jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.$(O): jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.$(O): jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.$(O): jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.$(O): jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.$(O): jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.$(O): jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.$(O): jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.$(O): jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.$(O): jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.$(O): jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.$(O): jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.$(O): jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.$(O): jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.$(O): jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.$(O): jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.$(O): jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.$(O): jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.$(O): jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.$(O): jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.$(O): jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.$(O): jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.$(O): jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.$(O): jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.$(O): jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.$(O): jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.$(O): jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.$(O): jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.$(O): jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.$(O): jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.$(O): jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.$(O): jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.$(O): jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.$(O): jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.$(O): jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.$(O): jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.$(O): jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.$(O): jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.$(O): jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.$(O): cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.$(O): djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.$(O): jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.$(O): rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.$(O): wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.$(O): cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.$(O): rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.$(O): rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.$(O): transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.$(O): rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.$(O): wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.$(O): rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.$(O): wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.$(O): rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.$(O): wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.$(O): rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.$(O): wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.$(O): rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.$(O): wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.dj b/test/monniaux/jpeg-6b/makefile.dj
new file mode 100644
index 00000000..f766d25e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.dj
@@ -0,0 +1,220 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for DJGPP (Delorie's GNU C port on MS-DOS), v2.0 or later.
+# Thanks to Frank J. Donahoe for this version.
+
+# Read installation instructions before saying "make" !!
+
+# The name of your C compiler:
+CC= gcc
+
+# You may need to adjust these cc options:
+CFLAGS= -O2 -Wall -I.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time cc options:
+LDFLAGS= -s
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS=
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For DJGPP this is usually jmemnobs.o, but you could
+# use jmemname.o if you want to use named temp files instead of swap space.
+SYSDEPMEM= jmemnobs.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= $(CC)
+# file deletion command
+RM= del
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= ranlib
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.o jcapistd.o jctrans.o jcparam.o jdatadst.o jcinit.o \
+ jcmaster.o jcmarker.o jcmainct.o jcprepct.o jccoefct.o jccolor.o \
+ jcsample.o jchuff.o jcphuff.o jcdctmgr.o jfdctfst.o jfdctflt.o \
+ jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapimin.o jdapistd.o jdtrans.o jdatasrc.o jdmaster.o \
+ jdinput.o jdmarker.o jdhuff.o jdphuff.o jdmainct.o jdcoefct.o \
+ jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+ jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o rdswitch.o \
+ cdjpeg.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o \
+ cdjpeg.o
+TROBJECTS= jpegtran.o rdswitch.o cdjpeg.o transupp.o
+
+
+all: libjpeg.a cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+
+libjpeg.a: $(LIBOBJECTS)
+ $(RM) libjpeg.a
+ $(AR) libjpeg.a $(LIBOBJECTS)
+ $(AR2) libjpeg.a
+
+cjpeg.exe: $(COBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o cjpeg.exe $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg.exe: $(DOBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o djpeg.exe $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+jpegtran.exe: $(TROBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o jpegtran.exe $(TROBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom.exe: rdjpgcom.o
+ $(LN) $(LDFLAGS) -o rdjpgcom.exe rdjpgcom.o $(LDLIBS)
+
+wrjpgcom.exe: wrjpgcom.o
+ $(LN) $(LDFLAGS) -o wrjpgcom.exe wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ $(RM) *.o
+ $(RM) cjpeg.exe
+ $(RM) djpeg.exe
+ $(RM) jpegtran.exe
+ $(RM) rdjpgcom.exe
+ $(RM) wrjpgcom.exe
+ $(RM) libjpeg.a
+ $(RM) testout*.*
+
+test: cjpeg.exe djpeg.exe jpegtran.exe
+ $(RM) testout*.*
+ ./djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ ./cjpeg -dct int -outfile testout.jpg testimg.ppm
+ ./djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ ./jpegtran -outfile testoutt.jpg testprog.jpg
+ fc /b testimg.ppm testout.ppm
+ fc /b testimg.bmp testout.bmp
+ fc /b testimg.jpg testout.jpg
+ fc /b testimg.ppm testoutp.ppm
+ fc /b testimgp.jpg testoutp.jpg
+ fc /b testorig.jpg testoutt.jpg
+
+
+jcapimin.o: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.o: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.o: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.o: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.o: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.o: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.o: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.o: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.o: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.o: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.o: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.o: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.o: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.o: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.o: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.o: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.o: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.o: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.o: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.o: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.o: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.o: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.o: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.o: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.o: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.manx b/test/monniaux/jpeg-6b/makefile.manx
new file mode 100644
index 00000000..4cb42d17
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.manx
@@ -0,0 +1,214 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for Amiga systems using Manx Aztec C ver 5.x.
+# Thanks to D.J. James (djjames@cup.portal.com) for this version.
+
+# Read installation instructions before saying "make" !!
+
+# The name of your C compiler:
+CC= cc
+
+# You may need to adjust these cc options:
+# Uncomment for generic 68000 code (will work on any Amiga)
+ARCHFLAGS= -sn
+
+# Uncomment for 68020/68030 code (faster, but won't run on 68000 CPU)
+#ARCHFLAGS= -c2
+
+CFLAGS= -MC -MD $(ARCHFLAGS) -spfam -r4
+
+# Link-time cc options:
+LDFLAGS= -g
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS= -lml -lcl
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Amiga we recommend jmemname.o.
+SYSDEPMEM= jmemname.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= ln
+# file deletion command
+RM= delete quiet
+# library (.lib) file creation command
+AR= lb
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.o jcapistd.o jctrans.o jcparam.o jdatadst.o jcinit.o \
+ jcmaster.o jcmarker.o jcmainct.o jcprepct.o jccoefct.o jccolor.o \
+ jcsample.o jchuff.o jcphuff.o jcdctmgr.o jfdctfst.o jfdctflt.o \
+ jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapimin.o jdapistd.o jdtrans.o jdatasrc.o jdmaster.o \
+ jdinput.o jdmarker.o jdhuff.o jdphuff.o jdmainct.o jdcoefct.o \
+ jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+ jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o rdswitch.o \
+ cdjpeg.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o \
+ cdjpeg.o
+TROBJECTS= jpegtran.o rdswitch.o cdjpeg.o transupp.o
+
+
+all: libjpeg.lib cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+
+libjpeg.lib: $(LIBOBJECTS)
+ -$(RM) libjpeg.lib
+ $(AR) libjpeg.lib $(LIBOBJECTS)
+
+cjpeg: $(COBJECTS) libjpeg.lib
+ $(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.lib $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.lib
+ $(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.lib $(LDLIBS)
+
+jpegtran: $(TROBJECTS) libjpeg.lib
+ $(LN) $(LDFLAGS) -o jpegtran $(TROBJECTS) libjpeg.lib $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+ $(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+ $(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ -$(RM) *.o cjpeg djpeg jpegtran libjpeg.lib rdjpgcom wrjpgcom
+ -$(RM) core testout*.*
+
+test: cjpeg djpeg jpegtran
+ -$(RM) testout*.*
+ djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ cjpeg -dct int -outfile testout.jpg testimg.ppm
+ djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ jpegtran -outfile testoutt.jpg testprog.jpg
+ cmp testimg.ppm testout.ppm
+ cmp testimg.bmp testout.bmp
+ cmp testimg.jpg testout.jpg
+ cmp testimg.ppm testoutp.ppm
+ cmp testimgp.jpg testoutp.jpg
+ cmp testorig.jpg testoutt.jpg
+
+
+jcapimin.o: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.o: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.o: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.o: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.o: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.o: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.o: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.o: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.o: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.o: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.o: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.o: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.o: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.o: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.o: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.o: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.o: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.o: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.o: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.o: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.o: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.o: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.o: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.o: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.o: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.mc6 b/test/monniaux/jpeg-6b/makefile.mc6
new file mode 100644
index 00000000..6aff0546
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.mc6
@@ -0,0 +1,249 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for Microsoft C for MS-DOS, version 6.00A and up.
+# Use NMAKE, not Microsoft's brain-damaged MAKE.
+# Thanks to Alan Wright and Chris Turner of Olivetti Research Ltd.
+
+# Read installation instructions before saying "nmake" !!
+
+# You may need to adjust these compiler options:
+CFLAGS = -AM -Oecigt -Gs -W3
+# -AM medium memory model (or use -AS for small model, if you remove features)
+# -Oecigt -Gs maximum safe optimisation (-Ol has bugs in MSC 6.00A)
+# -W3 warning level 3
+# You might also want to add -G2 if you have an 80286, etc.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Jan-Herman Buining suggests the following switches for MS C 8.0 and a 486:
+# CFLAGS = /AM /f- /FPi87 /G3 /Gs /Gy /Ob1 /Oc /Oe /Og /Oi /Ol /On /Oo /Ot \
+# /OV4 /W3
+# except for jquant1.c, which must be compiled with /Oo- to avoid a compiler
+# crash.
+
+# Ingar Steinsland suggests the following switches when building
+# a 16-bit Windows DLL:
+# CFLAGS = -ALw -Gsw -Zpe -W3 -O2 -Zi -Zd
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For DOS, we recommend jmemdos.c and jmemdosa.asm.
+# (But not for Windows; see install.doc if you use this makefile for Windows.)
+SYSDEPMEM= jmemdos.obj jmemdosa.obj
+# SYSDEPMEMLIB must list the same files with "+" signs for the librarian.
+SYSDEPMEMLIB= +jmemdos.obj +jmemdosa.obj
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.obj jcapistd.obj jctrans.obj jcparam.obj jdatadst.obj \
+ jcinit.obj jcmaster.obj jcmarker.obj jcmainct.obj jcprepct.obj \
+ jccoefct.obj jccolor.obj jcsample.obj jchuff.obj jcphuff.obj \
+ jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapimin.obj jdapistd.obj jdtrans.obj jdatasrc.obj \
+ jdmaster.obj jdinput.obj jdmarker.obj jdhuff.obj jdphuff.obj \
+ jdmainct.obj jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj \
+ jidctflt.obj jidctint.obj jidctred.obj jdsample.obj jdcolor.obj \
+ jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj \
+ rdswitch.obj cdjpeg.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+ rdcolmap.obj cdjpeg.obj
+TROBJECTS= jpegtran.obj rdswitch.obj cdjpeg.obj transupp.obj
+
+# need linker response file because file list > 128 chars
+RFILE = libjpeg.ans
+
+
+all: libjpeg.lib cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+
+libjpeg.lib: $(LIBOBJECTS) $(RFILE)
+ del libjpeg.lib
+ lib @$(RFILE)
+
+# linker response file for building libjpeg.lib
+$(RFILE) : makefile
+ del $(RFILE)
+ echo libjpeg.lib >$(RFILE)
+# silly want-to-create-it prompt:
+ echo y >>$(RFILE)
+ echo +jcapimin.obj +jcapistd.obj +jctrans.obj +jcparam.obj & >>$(RFILE)
+ echo +jdatadst.obj +jcinit.obj +jcmaster.obj +jcmarker.obj & >>$(RFILE)
+ echo +jcmainct.obj +jcprepct.obj +jccoefct.obj & >>$(RFILE)
+ echo +jccolor.obj +jcsample.obj +jchuff.obj +jcphuff.obj & >>$(RFILE)
+ echo +jcdctmgr.obj +jfdctfst.obj +jfdctflt.obj & >>$(RFILE)
+ echo +jfdctint.obj +jdapimin.obj +jdapistd.obj & >>$(RFILE)
+ echo +jdtrans.obj +jdatasrc.obj +jdmaster.obj +jdinput.obj & >>$(RFILE)
+ echo +jdmarker.obj +jdhuff.obj +jdphuff.obj +jdmainct.obj & >>$(RFILE)
+ echo +jdcoefct.obj +jdpostct.obj +jddctmgr.obj & >>$(RFILE)
+ echo +jidctfst.obj +jidctflt.obj +jidctint.obj & >>$(RFILE)
+ echo +jidctred.obj +jdsample.obj +jdcolor.obj +jquant1.obj & >>$(RFILE)
+ echo +jquant2.obj +jdmerge.obj +jcomapi.obj +jutils.obj & >>$(RFILE)
+ echo +jerror.obj +jmemmgr.obj & >>$(RFILE)
+ echo $(SYSDEPMEMLIB) ; >>$(RFILE)
+
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+ echo $(COBJECTS) >cjpeg.lst
+ link /STACK:4096 /EXEPACK @cjpeg.lst, cjpeg.exe, , libjpeg.lib, ;
+ del cjpeg.lst
+
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+ echo $(DOBJECTS) >djpeg.lst
+ link /STACK:4096 /EXEPACK @djpeg.lst, djpeg.exe, , libjpeg.lib, ;
+ del djpeg.lst
+
+jpegtran.exe: $(TROBJECTS) libjpeg.lib
+ link /STACK:4096 /EXEPACK $(TROBJECTS), jpegtran.exe, , libjpeg.lib, ;
+
+rdjpgcom.exe: rdjpgcom.c
+ $(CC) -AS -O -W3 rdjpgcom.c
+
+# wrjpgcom needs large model so it can malloc a 64K chunk
+wrjpgcom.exe: wrjpgcom.c
+ $(CC) -AL -O -W3 wrjpgcom.c
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ del *.obj
+ del libjpeg.lib
+ del cjpeg.exe
+ del djpeg.exe
+ del jpegtran.exe
+ del rdjpgcom.exe
+ del wrjpgcom.exe
+ del testout*.*
+
+test: cjpeg.exe djpeg.exe jpegtran.exe
+ del testout*.*
+ djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ cjpeg -dct int -outfile testout.jpg testimg.ppm
+ djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ jpegtran -outfile testoutt.jpg testprog.jpg
+ fc /b testimg.ppm testout.ppm
+ fc /b testimg.bmp testout.bmp
+ fc /b testimg.jpg testout.jpg
+ fc /b testimg.ppm testoutp.ppm
+ fc /b testimgp.jpg testoutp.jpg
+ fc /b testorig.jpg testoutt.jpg
+
+
+jcapimin.obj: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.obj: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.obj: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.obj: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.obj: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.obj: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.obj: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.obj: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.obj: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.obj: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.obj: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.obj: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.obj: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.obj: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.obj: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.obj: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.obj: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.obj: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.obj: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.obj: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.obj: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.obj: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.obj: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.obj: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.obj: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+jmemdosa.obj : jmemdosa.asm
+ masm /mx $*;
diff --git a/test/monniaux/jpeg-6b/makefile.mms b/test/monniaux/jpeg-6b/makefile.mms
new file mode 100644
index 00000000..cf130e5b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.mms
@@ -0,0 +1,218 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for use with MMS on Digital VMS systems.
+# Thanks to Rick Dyson (dyson@iowasp.physics.uiowa.edu)
+# and Tim Bell (tbell@netcom.com) for their help.
+
+# Read installation instructions before saying "MMS" !!
+
+# You may need to adjust these cc options:
+CFLAGS= $(CFLAGS) /NoDebug /Optimize
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via /Define switches here.
+.ifdef ALPHA
+OPT=
+.else
+OPT= ,Sys$Disk:[]MAKVMS.OPT/Option
+.endif
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.obj
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.obj jcapistd.obj jctrans.obj jcparam.obj jdatadst.obj \
+ jcinit.obj jcmaster.obj jcmarker.obj jcmainct.obj jcprepct.obj \
+ jccoefct.obj jccolor.obj jcsample.obj jchuff.obj jcphuff.obj \
+ jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapimin.obj jdapistd.obj jdtrans.obj jdatasrc.obj \
+ jdmaster.obj jdinput.obj jdmarker.obj jdhuff.obj jdphuff.obj \
+ jdmainct.obj jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj \
+ jidctflt.obj jidctint.obj jidctred.obj jdsample.obj jdcolor.obj \
+ jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.olb
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj \
+ rdswitch.obj cdjpeg.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+ rdcolmap.obj cdjpeg.obj
+TROBJECTS= jpegtran.obj rdswitch.obj cdjpeg.obj transupp.obj
+# objectfile lists with commas --- what a crock
+COBJLIST= cjpeg.obj,rdppm.obj,rdgif.obj,rdtarga.obj,rdrle.obj,rdbmp.obj,\
+ rdswitch.obj,cdjpeg.obj
+DOBJLIST= djpeg.obj,wrppm.obj,wrgif.obj,wrtarga.obj,wrrle.obj,wrbmp.obj,\
+ rdcolmap.obj,cdjpeg.obj
+TROBJLIST= jpegtran.obj,rdswitch.obj,cdjpeg.obj,transupp.obj
+LIBOBJLIST= jcapimin.obj,jcapistd.obj,jctrans.obj,jcparam.obj,jdatadst.obj,\
+ jcinit.obj,jcmaster.obj,jcmarker.obj,jcmainct.obj,jcprepct.obj,\
+ jccoefct.obj,jccolor.obj,jcsample.obj,jchuff.obj,jcphuff.obj,\
+ jcdctmgr.obj,jfdctfst.obj,jfdctflt.obj,jfdctint.obj,jdapimin.obj,\
+ jdapistd.obj,jdtrans.obj,jdatasrc.obj,jdmaster.obj,jdinput.obj,\
+ jdmarker.obj,jdhuff.obj,jdphuff.obj,jdmainct.obj,jdcoefct.obj,\
+ jdpostct.obj,jddctmgr.obj,jidctfst.obj,jidctflt.obj,jidctint.obj,\
+ jidctred.obj,jdsample.obj,jdcolor.obj,jquant1.obj,jquant2.obj,\
+ jdmerge.obj,jcomapi.obj,jutils.obj,jerror.obj,jmemmgr.obj,$(SYSDEPMEM)
+
+
+.first
+ @- Define /NoLog Sys Sys$Library
+
+ALL : libjpeg.olb cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+ @ Continue
+
+libjpeg.olb : $(LIBOBJECTS)
+ Library /Create libjpeg.olb $(LIBOBJLIST)
+
+cjpeg.exe : $(COBJECTS) libjpeg.olb
+ $(LINK) $(LFLAGS) /Executable = cjpeg.exe $(COBJLIST),libjpeg.olb/Library$(OPT)
+
+djpeg.exe : $(DOBJECTS) libjpeg.olb
+ $(LINK) $(LFLAGS) /Executable = djpeg.exe $(DOBJLIST),libjpeg.olb/Library$(OPT)
+
+jpegtran.exe : $(TROBJECTS) libjpeg.olb
+ $(LINK) $(LFLAGS) /Executable = jpegtran.exe $(TROBJLIST),libjpeg.olb/Library$(OPT)
+
+rdjpgcom.exe : rdjpgcom.obj
+ $(LINK) $(LFLAGS) /Executable = rdjpgcom.exe rdjpgcom.obj$(OPT)
+
+wrjpgcom.exe : wrjpgcom.obj
+ $(LINK) $(LFLAGS) /Executable = wrjpgcom.exe wrjpgcom.obj$(OPT)
+
+jconfig.h : jconfig.vms
+ @- Copy jconfig.vms jconfig.h
+
+clean :
+ @- Set Protection = Owner:RWED *.*;-1
+ @- Set Protection = Owner:RWED *.OBJ
+ - Purge /NoLog /NoConfirm *.*
+ - Delete /NoLog /NoConfirm *.OBJ;
+
+test : cjpeg.exe djpeg.exe jpegtran.exe
+ mcr sys$disk:[]djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ mcr sys$disk:[]djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ mcr sys$disk:[]cjpeg -dct int -outfile testout.jpg testimg.ppm
+ mcr sys$disk:[]djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ mcr sys$disk:[]cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ mcr sys$disk:[]jpegtran -outfile testoutt.jpg testprog.jpg
+ - Backup /Compare/Log testimg.ppm testout.ppm
+ - Backup /Compare/Log testimg.bmp testout.bmp
+ - Backup /Compare/Log testimg.jpg testout.jpg
+ - Backup /Compare/Log testimg.ppm testoutp.ppm
+ - Backup /Compare/Log testimgp.jpg testoutp.jpg
+ - Backup /Compare/Log testorig.jpg testoutt.jpg
+
+
+jcapimin.obj : jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.obj : jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.obj : jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.obj : jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.obj : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.obj : jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.obj : jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.obj : jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.obj : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.obj : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.obj : jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.obj : jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.obj : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.obj : jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.obj : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.obj : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.obj : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.obj : jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.obj : jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.obj : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj : wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.obj : cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.obj : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.obj : rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.obj : transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.obj : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.sas b/test/monniaux/jpeg-6b/makefile.sas
new file mode 100644
index 00000000..f296faf0
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.sas
@@ -0,0 +1,252 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for Amiga systems using SAS C 6.0 and up.
+# Thanks to Ed Hanway, Mark Rinfret, and Jim Zepeda.
+
+# Read installation instructions before saying "make" !!
+
+# The name of your C compiler:
+CC= sc
+
+# You may need to adjust these cc options:
+# Uncomment the following lines for generic 680x0 version
+ARCHFLAGS= cpu=any
+SUFFIX=
+
+# Uncomment the following lines for 68030-only version
+#ARCHFLAGS= cpu=68030
+#SUFFIX=.030
+
+CFLAGS= nostackcheck data=near parms=register optimize $(ARCHFLAGS) \
+ ignore=104 ignore=304 ignore=306
+# ignore=104 disables warnings for mismatched const qualifiers
+# ignore=304 disables warnings for variables being optimized out
+# ignore=306 disables warnings for the inlining of functions
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via define switches here.
+
+# Link-time cc options:
+LDFLAGS= SC SD ND BATCH
+
+# To link any special libraries, add the necessary commands here.
+LDLIBS= LIB:scm.lib LIB:sc.lib
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Amiga we recommend jmemname.o.
+SYSDEPMEM= jmemname.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= slink
+# file deletion command
+RM= delete quiet
+# library (.lib) file creation command
+AR= oml
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.o jcapistd.o jctrans.o jcparam.o jdatadst.o jcinit.o \
+ jcmaster.o jcmarker.o jcmainct.o jcprepct.o jccoefct.o jccolor.o \
+ jcsample.o jchuff.o jcphuff.o jcdctmgr.o jfdctfst.o jfdctflt.o \
+ jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapimin.o jdapistd.o jdtrans.o jdatasrc.o jdmaster.o \
+ jdinput.o jdmarker.o jdhuff.o jdphuff.o jdmainct.o jdcoefct.o \
+ jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+ jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o rdswitch.o \
+ cdjpeg.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o \
+ cdjpeg.o
+TROBJECTS= jpegtran.o rdswitch.o cdjpeg.o transupp.o
+
+
+all: libjpeg.lib cjpeg$(SUFFIX) djpeg$(SUFFIX) jpegtran$(SUFFIX) rdjpgcom$(SUFFIX) wrjpgcom$(SUFFIX)
+
+# note: do several AR steps to avoid command line length limitations
+
+libjpeg.lib: $(LIBOBJECTS)
+ -$(RM) libjpeg.lib
+ $(AR) libjpeg.lib r $(CLIBOBJECTS)
+ $(AR) libjpeg.lib r $(DLIBOBJECTS)
+ $(AR) libjpeg.lib r $(COMOBJECTS)
+
+cjpeg$(SUFFIX): $(COBJECTS) libjpeg.lib
+ $(LN) <WITH <
+$(LDFLAGS)
+TO cjpeg$(SUFFIX)
+FROM LIB:c.o $(COBJECTS)
+LIB libjpeg.lib $(LDLIBS)
+<
+
+djpeg$(SUFFIX): $(DOBJECTS) libjpeg.lib
+ $(LN) <WITH <
+$(LDFLAGS)
+TO djpeg$(SUFFIX)
+FROM LIB:c.o $(DOBJECTS)
+LIB libjpeg.lib $(LDLIBS)
+<
+
+jpegtran$(SUFFIX): $(TROBJECTS) libjpeg.lib
+ $(LN) <WITH <
+$(LDFLAGS)
+TO jpegtran$(SUFFIX)
+FROM LIB:c.o $(TROBJECTS)
+LIB libjpeg.lib $(LDLIBS)
+<
+
+rdjpgcom$(SUFFIX): rdjpgcom.o
+ $(LN) <WITH <
+$(LDFLAGS)
+TO rdjpgcom$(SUFFIX)
+FROM LIB:c.o rdjpgcom.o
+LIB $(LDLIBS)
+<
+
+wrjpgcom$(SUFFIX): wrjpgcom.o
+ $(LN) <WITH <
+$(LDFLAGS)
+TO wrjpgcom$(SUFFIX)
+FROM LIB:c.o wrjpgcom.o
+LIB $(LDLIBS)
+<
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ -$(RM) *.o cjpeg djpeg jpegtran cjpeg.030 djpeg.030 jpegtran.030
+ -$(RM) rdjpgcom wrjpgcom rdjpgcom.030 wrjpgcom.030
+ -$(RM) libjpeg.lib core testout*.*
+
+test: cjpeg djpeg jpegtran
+ -$(RM) testout*.*
+ djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ cjpeg -dct int -outfile testout.jpg testimg.ppm
+ djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ jpegtran -outfile testoutt.jpg testprog.jpg
+ cmp testimg.ppm testout.ppm
+ cmp testimg.bmp testout.bmp
+ cmp testimg.jpg testout.jpg
+ cmp testimg.ppm testoutp.ppm
+ cmp testimgp.jpg testoutp.jpg
+ cmp testorig.jpg testoutt.jpg
+
+
+jcapimin.o: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.o: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.o: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.o: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.o: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.o: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.o: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.o: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.o: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.o: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.o: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.o: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.o: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.o: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.o: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.o: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.o: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.o: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.o: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.o: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.o: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.o: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.o: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.o: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.o: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.unix b/test/monniaux/jpeg-6b/makefile.unix
new file mode 100644
index 00000000..00455ab6
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.unix
@@ -0,0 +1,228 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is suitable for Unix-like systems with non-ANSI compilers.
+# If you have an ANSI compiler, makefile.ansi is a better starting point.
+
+# Read installation instructions before saying "make" !!
+
+# The name of your C compiler:
+CC= cc
+
+# You may need to adjust these cc options:
+CFLAGS= -O
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+# However, any special defines for ansi2knr.c may be included here:
+ANSI2KNRFLAGS=
+
+# Link-time cc options:
+LDFLAGS=
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS=
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= $(CC)
+# file deletion command
+RM= rm -f
+# file rename command
+MV= mv
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= ranlib
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.o jcapistd.o jctrans.o jcparam.o jdatadst.o jcinit.o \
+ jcmaster.o jcmarker.o jcmainct.o jcprepct.o jccoefct.o jccolor.o \
+ jcsample.o jchuff.o jcphuff.o jcdctmgr.o jfdctfst.o jfdctflt.o \
+ jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapimin.o jdapistd.o jdtrans.o jdatasrc.o jdmaster.o \
+ jdinput.o jdmarker.o jdhuff.o jdphuff.o jdmainct.o jdcoefct.o \
+ jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+ jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o rdswitch.o \
+ cdjpeg.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o \
+ cdjpeg.o
+TROBJECTS= jpegtran.o rdswitch.o cdjpeg.o transupp.o
+
+
+all: ansi2knr libjpeg.a cjpeg djpeg jpegtran rdjpgcom wrjpgcom
+
+# This rule causes ansi2knr to be invoked.
+.c.o:
+ ./ansi2knr $*.c T$*.c
+ $(CC) $(CFLAGS) -c T$*.c
+ $(RM) T$*.c $*.o
+ $(MV) T$*.o $*.o
+
+ansi2knr: ansi2knr.c
+ $(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr ansi2knr.c
+
+libjpeg.a: ansi2knr $(LIBOBJECTS)
+ $(RM) libjpeg.a
+ $(AR) libjpeg.a $(LIBOBJECTS)
+ $(AR2) libjpeg.a
+
+cjpeg: ansi2knr $(COBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: ansi2knr $(DOBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+jpegtran: ansi2knr $(TROBJECTS) libjpeg.a
+ $(LN) $(LDFLAGS) -o jpegtran $(TROBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+ $(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+ $(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean:
+ $(RM) *.o cjpeg djpeg jpegtran libjpeg.a rdjpgcom wrjpgcom
+ $(RM) ansi2knr core testout*
+
+test: cjpeg djpeg jpegtran
+ $(RM) testout*
+ ./djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ ./cjpeg -dct int -outfile testout.jpg testimg.ppm
+ ./djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ ./jpegtran -outfile testoutt.jpg testprog.jpg
+ cmp testimg.ppm testout.ppm
+ cmp testimg.bmp testout.bmp
+ cmp testimg.jpg testout.jpg
+ cmp testimg.ppm testoutp.ppm
+ cmp testimgp.jpg testoutp.jpg
+ cmp testorig.jpg testoutt.jpg
+
+
+jcapimin.o: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.o: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.o: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.o: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.o: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.o: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.o: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.o: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.o: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.o: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.o: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.o: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.o: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.o: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.o: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.o: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.o: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.o: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.o: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.o: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.o: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.o: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.o: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.o: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.o: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.vc b/test/monniaux/jpeg-6b/makefile.vc
new file mode 100644
index 00000000..2acf0694
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.vc
@@ -0,0 +1,211 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for Microsoft Visual C++ on Windows NT (and 95?).
+# It builds the IJG library as a statically linkable library (.LIB),
+# and builds the sample applications as console-mode apps.
+# Thanks to Xingong Chang, Raymond Everly and others.
+
+# Read installation instructions before saying "nmake" !!
+# To build an optimized library without debug info, say "nmake nodebug=1".
+
+# Pull in standard variable definitions
+!include <win32.mak>
+
+# You may want to adjust these compiler options:
+CFLAGS= $(cflags) $(cdebug) $(cvars) -I.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time options:
+LDFLAGS= $(ldebug) $(conlflags)
+
+# To link any special libraries, add the necessary commands here.
+LDLIBS= $(conlibs)
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. For NT we suggest jmemnobs.obj, which expects the OS to
+# provide adequate virtual memory.
+SYSDEPMEM= jmemnobs.obj
+
+# miscellaneous OS-dependent stuff
+# file deletion command
+RM= del
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c \
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c \
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c \
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c \
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c \
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 \
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc \
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc \
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds \
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st \
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms \
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.obj jcapistd.obj jctrans.obj jcparam.obj jdatadst.obj \
+ jcinit.obj jcmaster.obj jcmarker.obj jcmainct.obj jcprepct.obj \
+ jccoefct.obj jccolor.obj jcsample.obj jchuff.obj jcphuff.obj \
+ jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapimin.obj jdapistd.obj jdtrans.obj jdatasrc.obj \
+ jdmaster.obj jdinput.obj jdmarker.obj jdhuff.obj jdphuff.obj \
+ jdmainct.obj jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj \
+ jidctflt.obj jidctint.obj jidctred.obj jdsample.obj jdcolor.obj \
+ jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj \
+ rdswitch.obj cdjpeg.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+ rdcolmap.obj cdjpeg.obj
+TROBJECTS= jpegtran.obj rdswitch.obj cdjpeg.obj transupp.obj
+
+# Template command for compiling .c to .obj
+.c.obj:
+ $(cc) $(CFLAGS) $*.c
+
+
+all: libjpeg.lib cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+
+libjpeg.lib: $(LIBOBJECTS)
+ $(RM) libjpeg.lib
+ lib -out:libjpeg.lib $(LIBOBJECTS)
+
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+ $(link) $(LDFLAGS) -out:cjpeg.exe $(COBJECTS) libjpeg.lib $(LDLIBS)
+
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+ $(link) $(LDFLAGS) -out:djpeg.exe $(DOBJECTS) libjpeg.lib $(LDLIBS)
+
+jpegtran.exe: $(TROBJECTS) libjpeg.lib
+ $(link) $(LDFLAGS) -out:jpegtran.exe $(TROBJECTS) libjpeg.lib $(LDLIBS)
+
+rdjpgcom.exe: rdjpgcom.obj
+ $(link) $(LDFLAGS) -out:rdjpgcom.exe rdjpgcom.obj $(LDLIBS)
+
+wrjpgcom.exe: wrjpgcom.obj
+ $(link) $(LDFLAGS) -out:wrjpgcom.exe wrjpgcom.obj $(LDLIBS)
+
+
+clean:
+ $(RM) *.obj *.exe libjpeg.lib
+ $(RM) testout*
+
+test: cjpeg.exe djpeg.exe jpegtran.exe
+ $(RM) testout*
+ .\djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ .\djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ .\cjpeg -dct int -outfile testout.jpg testimg.ppm
+ .\djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ .\cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ .\jpegtran -outfile testoutt.jpg testprog.jpg
+ fc /b testimg.ppm testout.ppm
+ fc /b testimg.bmp testout.bmp
+ fc /b testimg.jpg testout.jpg
+ fc /b testimg.ppm testoutp.ppm
+ fc /b testimgp.jpg testoutp.jpg
+ fc /b testorig.jpg testoutt.jpg
+
+
+jcapimin.obj: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.obj: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.obj: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.obj: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.obj: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.obj: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.obj: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.obj: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.obj: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.obj: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.obj: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.obj: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.obj: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.obj: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.obj: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.obj: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.obj: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.obj: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.obj: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.obj: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.obj: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.obj: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.obj: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.obj: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.obj: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makefile.vms b/test/monniaux/jpeg-6b/makefile.vms
new file mode 100644
index 00000000..a42358d0
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.vms
@@ -0,0 +1,142 @@
+$! Makefile for Independent JPEG Group's software
+$!
+$! This is a command procedure for Digital VMS systems that do not have MMS.
+$! It builds the JPEG software by brute force, recompiling everything whether
+$! or not it is necessary. It then runs the basic self-test.
+$! Thanks to Rick Dyson (dyson@iowasp.physics.uiowa.edu)
+$! and Tim Bell (tbell@netcom.com) for their help.
+$!
+$! Read installation instructions before running this!!
+$!
+$ If F$Mode () .eqs. "INTERACTIVE"
+$ Then
+$ VERIFY = F$Verify (0)
+$ Else
+$ VERIFY = F$Verify (1)
+$ EndIf
+$ On Control_Y Then GoTo End
+$ On Error Then GoTo End
+$
+$ If F$GetSyi ("HW_MODEL") .gt. 1023
+$ Then
+$ OPT = ""
+$ Else
+$ OPT = ",Sys$Disk:[]makvms.opt/Option"
+$ EndIf
+$
+$ DoCompile := CC /NoDebug /Optimize /NoList
+$!
+$ DoCompile jcapimin.c
+$ DoCompile jcapistd.c
+$ DoCompile jctrans.c
+$ DoCompile jcparam.c
+$ DoCompile jdatadst.c
+$ DoCompile jcinit.c
+$ DoCompile jcmaster.c
+$ DoCompile jcmarker.c
+$ DoCompile jcmainct.c
+$ DoCompile jcprepct.c
+$ DoCompile jccoefct.c
+$ DoCompile jccolor.c
+$ DoCompile jcsample.c
+$ DoCompile jchuff.c
+$ DoCompile jcphuff.c
+$ DoCompile jcdctmgr.c
+$ DoCompile jfdctfst.c
+$ DoCompile jfdctflt.c
+$ DoCompile jfdctint.c
+$ DoCompile jdapimin.c
+$ DoCompile jdapistd.c
+$ DoCompile jdtrans.c
+$ DoCompile jdatasrc.c
+$ DoCompile jdmaster.c
+$ DoCompile jdinput.c
+$ DoCompile jdmarker.c
+$ DoCompile jdhuff.c
+$ DoCompile jdphuff.c
+$ DoCompile jdmainct.c
+$ DoCompile jdcoefct.c
+$ DoCompile jdpostct.c
+$ DoCompile jddctmgr.c
+$ DoCompile jidctfst.c
+$ DoCompile jidctflt.c
+$ DoCompile jidctint.c
+$ DoCompile jidctred.c
+$ DoCompile jdsample.c
+$ DoCompile jdcolor.c
+$ DoCompile jquant1.c
+$ DoCompile jquant2.c
+$ DoCompile jdmerge.c
+$ DoCompile jcomapi.c
+$ DoCompile jutils.c
+$ DoCompile jerror.c
+$ DoCompile jmemmgr.c
+$ DoCompile jmemnobs.c
+$!
+$ Library /Create libjpeg.olb jcapimin.obj,jcapistd.obj,jctrans.obj, -
+ jcparam.obj,jdatadst.obj,jcinit.obj,jcmaster.obj,jcmarker.obj, -
+ jcmainct.obj,jcprepct.obj,jccoefct.obj,jccolor.obj,jcsample.obj, -
+ jchuff.obj,jcphuff.obj,jcdctmgr.obj,jfdctfst.obj,jfdctflt.obj, -
+ jfdctint.obj,jdapimin.obj,jdapistd.obj,jdtrans.obj,jdatasrc.obj, -
+ jdmaster.obj,jdinput.obj,jdmarker.obj,jdhuff.obj,jdphuff.obj, -
+ jdmainct.obj,jdcoefct.obj,jdpostct.obj,jddctmgr.obj,jidctfst.obj, -
+ jidctflt.obj,jidctint.obj,jidctred.obj,jdsample.obj,jdcolor.obj, -
+ jquant1.obj,jquant2.obj,jdmerge.obj,jcomapi.obj,jutils.obj, -
+ jerror.obj,jmemmgr.obj,jmemnobs.obj
+$!
+$ DoCompile cjpeg.c
+$ DoCompile rdppm.c
+$ DoCompile rdgif.c
+$ DoCompile rdtarga.c
+$ DoCompile rdrle.c
+$ DoCompile rdbmp.c
+$ DoCompile rdswitch.c
+$ DoCompile cdjpeg.c
+$!
+$ Link /NoMap /Executable = cjpeg.exe cjpeg.obj,rdppm.obj,rdgif.obj, -
+ rdtarga.obj,rdrle.obj,rdbmp.obj,rdswitch.obj,cdjpeg.obj,libjpeg.olb/Library'OPT'
+$!
+$ DoCompile djpeg.c
+$ DoCompile wrppm.c
+$ DoCompile wrgif.c
+$ DoCompile wrtarga.c
+$ DoCompile wrrle.c
+$ DoCompile wrbmp.c
+$ DoCompile rdcolmap.c
+$ DoCompile cdjpeg.c
+$!
+$ Link /NoMap /Executable = djpeg.exe djpeg.obj,wrppm.obj,wrgif.obj, -
+ wrtarga.obj,wrrle.obj,wrbmp.obj,rdcolmap.obj,cdjpeg.obj,libjpeg.olb/Library'OPT'
+$!
+$ DoCompile jpegtran.c
+$ DoCompile rdswitch.c
+$ DoCompile cdjpeg.c
+$ DoCompile transupp.c
+$!
+$ Link /NoMap /Executable = jpegtran.exe jpegtran.obj,rdswitch.obj, -
+ cdjpeg.obj,transupp.obj,libjpeg.olb/Library'OPT'
+$!
+$ DoCompile rdjpgcom.c
+$ Link /NoMap /Executable = rdjpgcom.exe rdjpgcom.obj'OPT'
+$!
+$ DoCompile wrjpgcom.c
+$ Link /NoMap /Executable = wrjpgcom.exe wrjpgcom.obj'OPT'
+$!
+$! Run the self-test
+$!
+$ mcr sys$disk:[]djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+$ mcr sys$disk:[]djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+$ mcr sys$disk:[]cjpeg -dct int -outfile testout.jpg testimg.ppm
+$ mcr sys$disk:[]djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+$ mcr sys$disk:[]cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+$ mcr sys$disk:[]jpegtran -outfile testoutt.jpg testprog.jpg
+$ Backup /Compare/Log testimg.ppm testout.ppm
+$ Backup /Compare/Log testimg.bmp testout.bmp
+$ Backup /Compare/Log testimg.jpg testout.jpg
+$ Backup /Compare/Log testimg.ppm testoutp.ppm
+$ Backup /Compare/Log testimgp.jpg testoutp.jpg
+$ Backup /Compare/Log testorig.jpg testoutt.jpg
+$!
+$End:
+$ If Verify Then Set Verify
+$ Exit
diff --git a/test/monniaux/jpeg-6b/makefile.wat b/test/monniaux/jpeg-6b/makefile.wat
new file mode 100644
index 00000000..d953e466
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makefile.wat
@@ -0,0 +1,233 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is suitable for Watcom C/C++ 10.0 on MS-DOS (using
+# dos4g extender), OS/2, and Windows NT console mode.
+# Thanks to Janos Haide, jhaide@btrvtech.com.
+
+# Read installation instructions before saying "wmake" !!
+
+# Uncomment line for desired system
+SYSTEM=DOS
+#SYSTEM=OS2
+#SYSTEM=NT
+
+# The name of your C compiler:
+CC= wcl386
+
+# You may need to adjust these cc options:
+CFLAGS= -4r -ort -wx -zq -bt=$(SYSTEM)
+# Caution: avoid -ol or -ox; these generate bad code with 10.0 or 10.0a.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time cc options:
+!ifeq SYSTEM DOS
+LDFLAGS= -zq -l=dos4g
+!else ifeq SYSTEM OS2
+LDFLAGS= -zq -l=os2v2
+!else ifeq SYSTEM NT
+LDFLAGS= -zq -l=nt
+!endif
+
+# Put here the object file name for the correct system-dependent memory
+# manager file. jmemnobs should work fine for dos4g or OS/2 environment.
+SYSDEPMEM= jmemnobs.obj
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c &
+ jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c &
+ jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c &
+ jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c &
+ jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c &
+ jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c &
+ jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c &
+ jquant2.c jutils.c jmemmgr.c
+# memmgr back ends: compile only one of these into a working library
+SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c
+# source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c jpegtran.c rdjpgcom.c wrjpgcom.c cdjpeg.c &
+ rdcolmap.c rdswitch.c transupp.c rdppm.c wrppm.c rdgif.c wrgif.c &
+ rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c
+SOURCES= $(LIBSOURCES) $(SYSDEPSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jchuff.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h &
+ jpegint.h jpeglib.h jversion.h cdjpeg.h cderror.h transupp.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 &
+ wrjpgcom.1 wizard.doc example.c libjpeg.doc structure.doc &
+ coderules.doc filelist.doc change.log
+MKFILES= configure makefile.cfg makefile.ansi makefile.unix makefile.bcc &
+ makefile.mc6 makefile.dj makefile.wat makefile.vc makelib.ds &
+ makeapps.ds makeproj.mac makcjpeg.st makdjpeg.st makljpeg.st &
+ maktjpeg.st makefile.manx makefile.sas makefile.mms makefile.vms &
+ makvms.opt
+CONFIGFILES= jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat &
+ jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas &
+ jconfig.vms
+CONFIGUREFILES= config.guess config.sub install-sh ltconfig ltmain.sh
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg &
+ testimgp.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) &
+ $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapimin.obj jcapistd.obj jctrans.obj jcparam.obj jdatadst.obj &
+ jcinit.obj jcmaster.obj jcmarker.obj jcmainct.obj jcprepct.obj &
+ jccoefct.obj jccolor.obj jcsample.obj jchuff.obj jcphuff.obj &
+ jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapimin.obj jdapistd.obj jdtrans.obj jdatasrc.obj &
+ jdmaster.obj jdinput.obj jdmarker.obj jdhuff.obj jdphuff.obj &
+ jdmainct.obj jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj &
+ jidctflt.obj jidctint.obj jidctred.obj jdsample.obj jdcolor.obj &
+ jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for sample applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj &
+ rdswitch.obj cdjpeg.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj &
+ rdcolmap.obj cdjpeg.obj
+TROBJECTS= jpegtran.obj rdswitch.obj cdjpeg.obj transupp.obj
+
+
+all: libjpeg.lib cjpeg.exe djpeg.exe jpegtran.exe rdjpgcom.exe wrjpgcom.exe
+
+libjpeg.lib: $(LIBOBJECTS)
+ - del libjpeg.lib
+ * wlib -n libjpeg.lib $(LIBOBJECTS)
+
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) $(COBJECTS) libjpeg.lib
+
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) $(DOBJECTS) libjpeg.lib
+
+jpegtran.exe: $(TROBJECTS) libjpeg.lib
+ $(CC) $(LDFLAGS) $(TROBJECTS) libjpeg.lib
+
+rdjpgcom.exe: rdjpgcom.c
+ $(CC) $(CFLAGS) $(LDFLAGS) rdjpgcom.c
+
+wrjpgcom.exe: wrjpgcom.c
+ $(CC) $(CFLAGS) $(LDFLAGS) wrjpgcom.c
+
+.c.obj:
+ $(CC) $(CFLAGS) -c $<
+
+jconfig.h: jconfig.doc
+ echo You must prepare a system-dependent jconfig.h file.
+ echo Please read the installation directions in install.doc.
+ exit 1
+
+clean: .SYMBOLIC
+ - del *.obj
+ - del libjpeg.lib
+ - del cjpeg.exe
+ - del djpeg.exe
+ - del jpegtran.exe
+ - del rdjpgcom.exe
+ - del wrjpgcom.exe
+ - del testout*.*
+
+test: cjpeg.exe djpeg.exe jpegtran.exe .SYMBOLIC
+ - del testout*.*
+ djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+ djpeg -dct int -bmp -colors 256 -outfile testout.bmp testorig.jpg
+ cjpeg -dct int -outfile testout.jpg testimg.ppm
+ djpeg -dct int -ppm -outfile testoutp.ppm testprog.jpg
+ cjpeg -dct int -progressive -opt -outfile testoutp.jpg testimg.ppm
+ jpegtran -outfile testoutt.jpg testprog.jpg
+!ifeq SYSTEM DOS
+ fc /b testimg.ppm testout.ppm
+ fc /b testimg.bmp testout.bmp
+ fc /b testimg.jpg testout.jpg
+ fc /b testimg.ppm testoutp.ppm
+ fc /b testimgp.jpg testoutp.jpg
+ fc /b testorig.jpg testoutt.jpg
+!else
+ echo n > n.tmp
+ comp testimg.ppm testout.ppm < n.tmp
+ comp testimg.bmp testout.bmp < n.tmp
+ comp testimg.jpg testout.jpg < n.tmp
+ comp testimg.ppm testoutp.ppm < n.tmp
+ comp testimgp.jpg testoutp.jpg < n.tmp
+ comp testorig.jpg testoutt.jpg < n.tmp
+ del n.tmp
+!endif
+
+
+jcapimin.obj: jcapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcapistd.obj: jcapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj: jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj: jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj: jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj: jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcinit.obj: jcinit.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj: jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj: jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj: jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj: jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj: jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcphuff.obj: jcphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jchuff.h
+jcprepct.obj: jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj: jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jctrans.obj: jctrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapimin.obj: jdapimin.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapistd.obj: jdapistd.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatadst.obj: jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatasrc.obj: jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj: jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj: jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj: jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj: jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdinput.obj: jdinput.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj: jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj: jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj: jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj: jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdphuff.obj: jdphuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdhuff.h
+jdpostct.obj: jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj: jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdtrans.obj: jdtrans.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj: jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jfdctflt.obj: jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctfst.obj: jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj: jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj: jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj: jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj: jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj: jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj: jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj: jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jutils.obj: jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj: jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj: jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj: jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj: jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj: jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemmac.obj: jmemmac.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj: cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj: djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+jpegtran.obj: jpegtran.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h transupp.h jversion.h
+rdjpgcom.obj: rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj: wrjpgcom.c jinclude.h jconfig.h
+cdjpeg.obj: cdjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdcolmap.obj: rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdswitch.obj: rdswitch.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+transupp.obj: transupp.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h transupp.h
+rdppm.obj: rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj: wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj: rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj: wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj: rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj: wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj: rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj: wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj: rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj: wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
diff --git a/test/monniaux/jpeg-6b/makelib.ds b/test/monniaux/jpeg-6b/makelib.ds
new file mode 100644
index 00000000..c7ad36d0
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makelib.ds
@@ -0,0 +1,1046 @@
+# Microsoft Developer Studio Generated NMAKE File, Format Version 4.20
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Static Library" 0x0104
+
+!IF "$(CFG)" == ""
+CFG=jpeg - Win32
+!MESSAGE No configuration specified. Defaulting to jpeg - Win32.
+!ENDIF
+
+!IF "$(CFG)" != "jpeg - Win32"
+!MESSAGE Invalid configuration "$(CFG)" specified.
+!MESSAGE You can specify a configuration when running NMAKE on this makefile
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "jpeg.mak" CFG="jpeg - Win32"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "jpeg - Win32" (based on "Win32 (x86) Static Library")
+!MESSAGE
+!ERROR An invalid configuration is specified.
+!ENDIF
+
+!IF "$(OS)" == "Windows_NT"
+NULL=
+!ELSE
+NULL=nul
+!ENDIF
+################################################################################
+# Begin Project
+# PROP Target_Last_Scanned "jpeg - Win32"
+CPP=cl.exe
+
+!IF "$(CFG)" == "jpeg - Win32"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Target_Dir ""
+OUTDIR=.\Release
+INTDIR=.\Release
+
+ALL : "$(OUTDIR)\jpeg.lib"
+
+CLEAN :
+ -@erase "$(INTDIR)\jcapimin.obj"
+ -@erase "$(INTDIR)\jcapistd.obj"
+ -@erase "$(INTDIR)\jctrans.obj"
+ -@erase "$(INTDIR)\jcparam.obj"
+ -@erase "$(INTDIR)\jdatadst.obj"
+ -@erase "$(INTDIR)\jcinit.obj"
+ -@erase "$(INTDIR)\jcmaster.obj"
+ -@erase "$(INTDIR)\jcmarker.obj"
+ -@erase "$(INTDIR)\jcmainct.obj"
+ -@erase "$(INTDIR)\jcprepct.obj"
+ -@erase "$(INTDIR)\jccoefct.obj"
+ -@erase "$(INTDIR)\jccolor.obj"
+ -@erase "$(INTDIR)\jcsample.obj"
+ -@erase "$(INTDIR)\jchuff.obj"
+ -@erase "$(INTDIR)\jcphuff.obj"
+ -@erase "$(INTDIR)\jcdctmgr.obj"
+ -@erase "$(INTDIR)\jfdctfst.obj"
+ -@erase "$(INTDIR)\jfdctflt.obj"
+ -@erase "$(INTDIR)\jfdctint.obj"
+ -@erase "$(INTDIR)\jdapimin.obj"
+ -@erase "$(INTDIR)\jdapistd.obj"
+ -@erase "$(INTDIR)\jdtrans.obj"
+ -@erase "$(INTDIR)\jdatasrc.obj"
+ -@erase "$(INTDIR)\jdmaster.obj"
+ -@erase "$(INTDIR)\jdinput.obj"
+ -@erase "$(INTDIR)\jdmarker.obj"
+ -@erase "$(INTDIR)\jdhuff.obj"
+ -@erase "$(INTDIR)\jdphuff.obj"
+ -@erase "$(INTDIR)\jdmainct.obj"
+ -@erase "$(INTDIR)\jdcoefct.obj"
+ -@erase "$(INTDIR)\jdpostct.obj"
+ -@erase "$(INTDIR)\jddctmgr.obj"
+ -@erase "$(INTDIR)\jidctfst.obj"
+ -@erase "$(INTDIR)\jidctflt.obj"
+ -@erase "$(INTDIR)\jidctint.obj"
+ -@erase "$(INTDIR)\jidctred.obj"
+ -@erase "$(INTDIR)\jdsample.obj"
+ -@erase "$(INTDIR)\jdcolor.obj"
+ -@erase "$(INTDIR)\jquant1.obj"
+ -@erase "$(INTDIR)\jquant2.obj"
+ -@erase "$(INTDIR)\jdmerge.obj"
+ -@erase "$(INTDIR)\jcomapi.obj"
+ -@erase "$(INTDIR)\jutils.obj"
+ -@erase "$(INTDIR)\jerror.obj"
+ -@erase "$(INTDIR)\jmemmgr.obj"
+ -@erase "$(INTDIR)\jmemnobs.obj"
+ -@erase "$(OUTDIR)\jpeg.lib"
+
+"$(OUTDIR)" :
+ if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
+
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /YX /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /YX /c
+CPP_PROJ=/nologo /ML /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS"\
+ /Fp"$(INTDIR)/jpeg.pch" /YX /Fo"$(INTDIR)/" /c
+CPP_OBJS=.\Release/
+CPP_SBRS=.\.
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+BSC32_FLAGS=/nologo /o"$(OUTDIR)/jpeg.bsc"
+BSC32_SBRS= \
+
+LIB32=link.exe -lib
+# ADD BASE LIB32 /nologo
+# ADD LIB32 /nologo
+LIB32_FLAGS=/nologo /out:"$(OUTDIR)/jpeg.lib"
+LIB32_OBJS= \
+ "$(INTDIR)\jcapimin.obj" \
+ "$(INTDIR)\jcapistd.obj" \
+ "$(INTDIR)\jctrans.obj" \
+ "$(INTDIR)\jcparam.obj" \
+ "$(INTDIR)\jdatadst.obj" \
+ "$(INTDIR)\jcinit.obj" \
+ "$(INTDIR)\jcmaster.obj" \
+ "$(INTDIR)\jcmarker.obj" \
+ "$(INTDIR)\jcmainct.obj" \
+ "$(INTDIR)\jcprepct.obj" \
+ "$(INTDIR)\jccoefct.obj" \
+ "$(INTDIR)\jccolor.obj" \
+ "$(INTDIR)\jcsample.obj" \
+ "$(INTDIR)\jchuff.obj" \
+ "$(INTDIR)\jcphuff.obj" \
+ "$(INTDIR)\jcdctmgr.obj" \
+ "$(INTDIR)\jfdctfst.obj" \
+ "$(INTDIR)\jfdctflt.obj" \
+ "$(INTDIR)\jfdctint.obj" \
+ "$(INTDIR)\jdapimin.obj" \
+ "$(INTDIR)\jdapistd.obj" \
+ "$(INTDIR)\jdtrans.obj" \
+ "$(INTDIR)\jdatasrc.obj" \
+ "$(INTDIR)\jdmaster.obj" \
+ "$(INTDIR)\jdinput.obj" \
+ "$(INTDIR)\jdmarker.obj" \
+ "$(INTDIR)\jdhuff.obj" \
+ "$(INTDIR)\jdphuff.obj" \
+ "$(INTDIR)\jdmainct.obj" \
+ "$(INTDIR)\jdcoefct.obj" \
+ "$(INTDIR)\jdpostct.obj" \
+ "$(INTDIR)\jddctmgr.obj" \
+ "$(INTDIR)\jidctfst.obj" \
+ "$(INTDIR)\jidctflt.obj" \
+ "$(INTDIR)\jidctint.obj" \
+ "$(INTDIR)\jidctred.obj" \
+ "$(INTDIR)\jdsample.obj" \
+ "$(INTDIR)\jdcolor.obj" \
+ "$(INTDIR)\jquant1.obj" \
+ "$(INTDIR)\jquant2.obj" \
+ "$(INTDIR)\jdmerge.obj" \
+ "$(INTDIR)\jcomapi.obj" \
+ "$(INTDIR)\jutils.obj" \
+ "$(INTDIR)\jerror.obj" \
+ "$(INTDIR)\jmemmgr.obj" \
+ "$(INTDIR)\jmemnobs.obj"
+
+"$(OUTDIR)\jpeg.lib" : "$(OUTDIR)" $(DEF_FILE) $(LIB32_OBJS)
+ $(LIB32) @<<
+ $(LIB32_FLAGS) $(DEF_FLAGS) $(LIB32_OBJS)
+<<
+
+!ENDIF
+
+.c{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.cpp{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.cxx{$(CPP_OBJS)}.obj:
+ $(CPP) $(CPP_PROJ) $<
+
+.c{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+.cpp{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+.cxx{$(CPP_SBRS)}.sbr:
+ $(CPP) $(CPP_PROJ) $<
+
+################################################################################
+# Begin Target
+
+# Name "jpeg - Win32"
+
+!IF "$(CFG)" == "jpeg - Win32"
+
+!ENDIF
+
+################################################################################
+# Begin Source File
+
+SOURCE="jcapimin.c"
+DEP_CPP_JCAPI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcapimin.obj" : $(SOURCE) $(DEP_CPP_JCAPI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcapistd.c"
+DEP_CPP_JCAPIS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcapistd.obj" : $(SOURCE) $(DEP_CPP_JCAPIS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jccoefct.c"
+DEP_CPP_JCCOE=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jccoefct.obj" : $(SOURCE) $(DEP_CPP_JCCOE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jccolor.c"
+DEP_CPP_JCCOL=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jccolor.obj" : $(SOURCE) $(DEP_CPP_JCCOL) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcdctmgr.c"
+DEP_CPP_JCDCT=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jcdctmgr.obj" : $(SOURCE) $(DEP_CPP_JCDCT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jchuff.c"
+DEP_CPP_JCHUF=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jchuff.h"\
+
+
+"$(INTDIR)\jchuff.obj" : $(SOURCE) $(DEP_CPP_JCHUF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcinit.c"
+DEP_CPP_JCINI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcinit.obj" : $(SOURCE) $(DEP_CPP_JCINI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcmainct.c"
+DEP_CPP_JCMAI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcmainct.obj" : $(SOURCE) $(DEP_CPP_JCMAI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcmarker.c"
+DEP_CPP_JCMAR=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcmarker.obj" : $(SOURCE) $(DEP_CPP_JCMAR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcmaster.c"
+DEP_CPP_JCMAS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcmaster.obj" : $(SOURCE) $(DEP_CPP_JCMAS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcomapi.c"
+DEP_CPP_JCOMA=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcomapi.obj" : $(SOURCE) $(DEP_CPP_JCOMA) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcparam.c"
+DEP_CPP_JCPAR=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcparam.obj" : $(SOURCE) $(DEP_CPP_JCPAR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcphuff.c"
+DEP_CPP_JCPHU=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jchuff.h"\
+
+
+"$(INTDIR)\jcphuff.obj" : $(SOURCE) $(DEP_CPP_JCPHU) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcprepct.c"
+DEP_CPP_JCPRE=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcprepct.obj" : $(SOURCE) $(DEP_CPP_JCPRE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jcsample.c"
+DEP_CPP_JCSAM=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jcsample.obj" : $(SOURCE) $(DEP_CPP_JCSAM) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jctrans.c"
+DEP_CPP_JCTRA=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jctrans.obj" : $(SOURCE) $(DEP_CPP_JCTRA) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdapimin.c"
+DEP_CPP_JDAPI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdapimin.obj" : $(SOURCE) $(DEP_CPP_JDAPI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdapistd.c"
+DEP_CPP_JDAPIS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdapistd.obj" : $(SOURCE) $(DEP_CPP_JDAPIS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdatadst.c"
+DEP_CPP_JDATA=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdatadst.obj" : $(SOURCE) $(DEP_CPP_JDATA) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdatasrc.c"
+DEP_CPP_JDATAS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdatasrc.obj" : $(SOURCE) $(DEP_CPP_JDATAS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdcoefct.c"
+DEP_CPP_JDCOE=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdcoefct.obj" : $(SOURCE) $(DEP_CPP_JDCOE) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdcolor.c"
+DEP_CPP_JDCOL=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdcolor.obj" : $(SOURCE) $(DEP_CPP_JDCOL) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jddctmgr.c"
+DEP_CPP_JDDCT=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jddctmgr.obj" : $(SOURCE) $(DEP_CPP_JDDCT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdhuff.c"
+DEP_CPP_JDHUF=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdhuff.h"\
+
+
+"$(INTDIR)\jdhuff.obj" : $(SOURCE) $(DEP_CPP_JDHUF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdinput.c"
+DEP_CPP_JDINP=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdinput.obj" : $(SOURCE) $(DEP_CPP_JDINP) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdmainct.c"
+DEP_CPP_JDMAI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdmainct.obj" : $(SOURCE) $(DEP_CPP_JDMAI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdmarker.c"
+DEP_CPP_JDMAR=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdmarker.obj" : $(SOURCE) $(DEP_CPP_JDMAR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdmaster.c"
+DEP_CPP_JDMAS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdmaster.obj" : $(SOURCE) $(DEP_CPP_JDMAS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdmerge.c"
+DEP_CPP_JDMER=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdmerge.obj" : $(SOURCE) $(DEP_CPP_JDMER) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdphuff.c"
+DEP_CPP_JDPHU=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdhuff.h"\
+
+
+"$(INTDIR)\jdphuff.obj" : $(SOURCE) $(DEP_CPP_JDPHU) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdpostct.c"
+DEP_CPP_JDPOS=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdpostct.obj" : $(SOURCE) $(DEP_CPP_JDPOS) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdsample.c"
+DEP_CPP_JDSAM=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdsample.obj" : $(SOURCE) $(DEP_CPP_JDSAM) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jdtrans.c"
+DEP_CPP_JDTRA=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jdtrans.obj" : $(SOURCE) $(DEP_CPP_JDTRA) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jerror.c"
+DEP_CPP_JERRO=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jversion.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jerror.obj" : $(SOURCE) $(DEP_CPP_JERRO) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jfdctflt.c"
+DEP_CPP_JFDCT=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jfdctflt.obj" : $(SOURCE) $(DEP_CPP_JFDCT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jfdctfst.c"
+DEP_CPP_JFDCTF=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jfdctfst.obj" : $(SOURCE) $(DEP_CPP_JFDCTF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jfdctint.c"
+DEP_CPP_JFDCTI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jfdctint.obj" : $(SOURCE) $(DEP_CPP_JFDCTI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jidctflt.c"
+DEP_CPP_JIDCT=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jidctflt.obj" : $(SOURCE) $(DEP_CPP_JIDCT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jidctfst.c"
+DEP_CPP_JIDCTF=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jidctfst.obj" : $(SOURCE) $(DEP_CPP_JIDCTF) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jidctint.c"
+DEP_CPP_JIDCTI=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jidctint.obj" : $(SOURCE) $(DEP_CPP_JIDCTI) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jidctred.c"
+DEP_CPP_JIDCTR=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jdct.h"\
+
+
+"$(INTDIR)\jidctred.obj" : $(SOURCE) $(DEP_CPP_JIDCTR) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jquant1.c"
+DEP_CPP_JQUAN=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jquant1.obj" : $(SOURCE) $(DEP_CPP_JQUAN) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jquant2.c"
+DEP_CPP_JQUANT=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jquant2.obj" : $(SOURCE) $(DEP_CPP_JQUANT) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jutils.c"
+DEP_CPP_JUTIL=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+
+
+"$(INTDIR)\jutils.obj" : $(SOURCE) $(DEP_CPP_JUTIL) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jmemmgr.c"
+DEP_CPP_JMEMM=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jmemsys.h"\
+
+
+"$(INTDIR)\jmemmgr.obj" : $(SOURCE) $(DEP_CPP_JMEMM) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+################################################################################
+# Begin Source File
+
+SOURCE="jmemnobs.c"
+DEP_CPP_JMEMN=\
+ "jinclude.h"\
+ "jconfig.h"\
+ "jpeglib.h"\
+ "jmorecfg.h"\
+ "jpegint.h"\
+ "jerror.h"\
+ "jmemsys.h"\
+
+
+"$(INTDIR)\jmemnobs.obj" : $(SOURCE) $(DEP_CPP_JMEMN) "$(INTDIR)"
+ $(CPP) $(CPP_PROJ) $(SOURCE)
+
+
+# End Source File
+# End Target
+# End Project
+################################################################################
+
diff --git a/test/monniaux/jpeg-6b/makeproj.mac b/test/monniaux/jpeg-6b/makeproj.mac
new file mode 100644
index 00000000..ed277c83
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makeproj.mac
@@ -0,0 +1,213 @@
+--
+-- makeproj.mac
+--
+-- This AppleScript builds Code Warrior PRO Release 2 project files for the
+-- libjpeg library as well as the test programs 'cjpeg', 'djpeg', 'jpegtran'.
+-- (We'd distribute real project files, except they're not text
+-- and would create maintenance headaches.)
+--
+-- The script then compiles and links the library and the test programs.
+-- NOTE: if you haven't already created a 'jconfig.h' file, the script
+-- automatically copies 'jconfig.mac' to 'jconfig.h'.
+--
+-- To use this script, you must have AppleScript 1.1 or later installed
+-- and a suitable AppleScript editor like Script Editor or Script Debugger
+-- (http://www.latenightsw.com). Open this file with your AppleScript
+-- editor and execute the "run" command to build the projects.
+--
+-- Thanks to Dan Sears and Don Agro for this script.
+-- Questions about this script can be addressed to dogpark@interlog.com
+--
+
+on run
+
+ choose folder with prompt ">>> Select IJG source folder <<<"
+ set ijg_folder to result
+
+ choose folder with prompt ">>> Select MetroWerks folder <<<"
+ set cw_folder to result
+
+ -- if jconfig.h doesn't already exist, copy jconfig.mac
+
+ tell application "Finder"
+ if not (exists file "jconfig.h" of ijg_folder) then
+ duplicate {file "jconfig.mac" of folder ijg_folder}
+ select file "jconfig.mac copy" of folder ijg_folder
+ set name of selection to "jconfig.h"
+ end if
+ end tell
+
+ tell application "CodeWarrior IDE 2.1"
+ with timeout of 10000 seconds
+
+ -- create libjpeg project
+
+ activate
+ Create Project (ijg_folder as string) & "libjpeg.proj"
+ Set Preferences of panel "Target Settings" to {Target Name:"libjpeg"}
+ Set Preferences of panel "PPC Project" to {File Name:"libjpeg"}
+ Set Preferences of panel "Target Settings" to {Linker:"MacOS PPC Linker"}
+ Set Preferences of panel "PPC Project" to {Project Type:library}
+ Set Preferences of panel "C/C++ Compiler" to {ANSI Strict:true}
+ Set Preferences of panel "C/C++ Compiler" to {Enums Always Ints:true}
+ Set Preferences of panel "PPC Codegen" to {Struct Alignment:PowerPC}
+ Set Preferences of panel "PPC Linker" to {Generate SYM File:false}
+
+ Add Files (ijg_folder as string) & "jcapimin.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcapistd.c" To Segment 1
+ Add Files (ijg_folder as string) & "jctrans.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcparam.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdatadst.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcinit.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcmaster.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcmarker.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcmainct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcprepct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jccoefct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jccolor.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcsample.c" To Segment 1
+ Add Files (ijg_folder as string) & "jchuff.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcphuff.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcdctmgr.c" To Segment 1
+ Add Files (ijg_folder as string) & "jfdctfst.c" To Segment 1
+ Add Files (ijg_folder as string) & "jfdctflt.c" To Segment 1
+ Add Files (ijg_folder as string) & "jfdctint.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdapimin.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdapistd.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdtrans.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdatasrc.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdmaster.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdinput.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdmarker.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdhuff.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdphuff.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdmainct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdcoefct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdpostct.c" To Segment 1
+ Add Files (ijg_folder as string) & "jddctmgr.c" To Segment 1
+ Add Files (ijg_folder as string) & "jidctfst.c" To Segment 1
+ Add Files (ijg_folder as string) & "jidctflt.c" To Segment 1
+ Add Files (ijg_folder as string) & "jidctint.c" To Segment 1
+ Add Files (ijg_folder as string) & "jidctred.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdsample.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdcolor.c" To Segment 1
+ Add Files (ijg_folder as string) & "jquant1.c" To Segment 1
+ Add Files (ijg_folder as string) & "jquant2.c" To Segment 1
+ Add Files (ijg_folder as string) & "jdmerge.c" To Segment 1
+ Add Files (ijg_folder as string) & "jcomapi.c" To Segment 1
+ Add Files (ijg_folder as string) & "jutils.c" To Segment 1
+ Add Files (ijg_folder as string) & "jerror.c" To Segment 1
+ Add Files (ijg_folder as string) & "jmemmgr.c" To Segment 1
+ Add Files (ijg_folder as string) & "jmemmac.c" To Segment 1
+
+ -- compile and link the library
+
+ Make Project
+ Close Project
+
+ -- create cjpeg project
+
+ activate
+ Create Project (ijg_folder as string) & "cjpeg.proj"
+ Set Preferences of panel "Target Settings" to {Target Name:"cjpeg"}
+ Set Preferences of panel "PPC Project" to {File Name:"cjpeg"}
+ Set Preferences of panel "Target Settings" to {Linker:"MacOS PPC Linker"}
+ Set Preferences of panel "C/C++ Compiler" to {ANSI Strict:true}
+ Set Preferences of panel "C/C++ Compiler" to {Enums Always Ints:true}
+ Set Preferences of panel "PPC Codegen" to {Struct Alignment:PowerPC}
+ Set Preferences of panel "PPC Linker" to {Generate SYM File:false}
+
+ Add Files (ijg_folder as string) & "cjpeg.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdppm.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdgif.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdtarga.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdrle.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdbmp.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdswitch.c" To Segment 1
+ Add Files (ijg_folder as string) & "cdjpeg.c" To Segment 1
+
+ Add Files (ijg_folder as string) & "libjpeg" To Segment 2
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL C.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL SIOUX.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:Runtime:Runtime PPC:MSL RuntimePPC.Lib" To Segment 3
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:InterfaceLib" To Segment 4
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:MathLib" To Segment 4
+
+ -- compile and link cjpeg
+
+ Make Project
+ Close Project
+
+ -- create djpeg project
+
+ activate
+ Create Project (ijg_folder as string) & "djpeg.proj"
+ Set Preferences of panel "Target Settings" to {Target Name:"djpeg"}
+ Set Preferences of panel "PPC Project" to {File Name:"djpeg"}
+ Set Preferences of panel "Target Settings" to {Linker:"MacOS PPC Linker"}
+ Set Preferences of panel "C/C++ Compiler" to {ANSI Strict:true}
+ Set Preferences of panel "C/C++ Compiler" to {Enums Always Ints:true}
+ Set Preferences of panel "PPC Codegen" to {Struct Alignment:PowerPC}
+ Set Preferences of panel "PPC Linker" to {Generate SYM File:false}
+
+ Add Files (ijg_folder as string) & "djpeg.c" To Segment 1
+ Add Files (ijg_folder as string) & "wrppm.c" To Segment 1
+ Add Files (ijg_folder as string) & "wrgif.c" To Segment 1
+ Add Files (ijg_folder as string) & "wrtarga.c" To Segment 1
+ Add Files (ijg_folder as string) & "wrrle.c" To Segment 1
+ Add Files (ijg_folder as string) & "wrbmp.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdcolmap.c" To Segment 1
+ Add Files (ijg_folder as string) & "cdjpeg.c" To Segment 1
+
+ Add Files (ijg_folder as string) & "libjpeg" To Segment 2
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL C.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL SIOUX.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:Runtime:Runtime PPC:MSL RuntimePPC.Lib" To Segment 3
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:InterfaceLib" To Segment 4
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:MathLib" To Segment 4
+
+ -- compile and link djpeg
+
+ Make Project
+ Close Project
+
+ -- create jpegtran project
+
+ activate
+ Create Project (ijg_folder as string) & "jpegtran.proj"
+ Set Preferences of panel "Target Settings" to {Target Name:"jpegtran"}
+ Set Preferences of panel "PPC Project" to {File Name:"jpegtran"}
+ Set Preferences of panel "Target Settings" to {Linker:"MacOS PPC Linker"}
+ Set Preferences of panel "C/C++ Compiler" to {ANSI Strict:true}
+ Set Preferences of panel "C/C++ Compiler" to {Enums Always Ints:true}
+ Set Preferences of panel "PPC Codegen" to {Struct Alignment:PowerPC}
+ Set Preferences of panel "PPC Linker" to {Generate SYM File:false}
+
+ Add Files (ijg_folder as string) & "jpegtran.c" To Segment 1
+ Add Files (ijg_folder as string) & "rdswitch.c" To Segment 1
+ Add Files (ijg_folder as string) & "cdjpeg.c" To Segment 1
+ Add Files (ijg_folder as string) & "transupp.c" To Segment 1
+
+ Add Files (ijg_folder as string) & "libjpeg" To Segment 2
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL C.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:Metrowerks Standard Library:MSL C:Bin:MSL SIOUX.PPC.Lib" To Segment 3
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:Runtime:Runtime PPC:MSL RuntimePPC.Lib" To Segment 3
+
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:InterfaceLib" To Segment 4
+ Add Files (cw_folder as string) & "Metrowerks CodeWarrior:MacOS Support:Libraries:MacOS Common:MathLib" To Segment 4
+
+ -- compile and link jpegtran
+
+ Make Project
+ Close Project
+
+ quit
+
+ end timeout
+ end tell
+end run
diff --git a/test/monniaux/jpeg-6b/makljpeg.st b/test/monniaux/jpeg-6b/makljpeg.st
new file mode 100644
index 00000000..813493ea
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makljpeg.st
@@ -0,0 +1,70 @@
+; Project file for Independent JPEG Group's software
+;
+; This project file is for Atari ST/STE/TT systems using Pure C or Turbo C.
+; Thanks to Frank Moehle (Frank.Moehle@arbi.informatik.uni-oldenburg.de),
+; Dr. B. Setzepfandt (bernd@gina.uni-muenster.de),
+; and Guido Vollbeding (guivol@esc.de).
+;
+; To use this file, rename it to libjpeg.prj.
+; Read installation instructions before trying to make the program!
+;
+;
+; * * * Output file * * *
+libjpeg.lib
+;
+; * * * COMPILER OPTIONS * * *
+.C[-P] ; absolute calls
+.C[-M] ; and no string merging, folks
+.C[-w-cln] ; no "constant is long" warnings
+.C[-w-par] ; no "parameter xxxx unused"
+.C[-w-rch] ; no "unreachable code"
+.C[-wsig] ; warn if significant digits may be lost
+.L[-J] ; link new Obj-format (so we get a library)
+=
+; * * * * List of modules * * * *
+jcapimin.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcapistd.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jccoefct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jccolor.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcdctmgr.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jchuff.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jchuff.h)
+jcinit.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmainct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmarker.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmaster.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcomapi.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcparam.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcphuff.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jchuff.h)
+jcprepct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcsample.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jctrans.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdapimin.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdapistd.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdatadst.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h)
+jdatasrc.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h)
+jdcoefct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdcolor.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jddctmgr.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jdhuff.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdhuff.h)
+jdinput.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmainct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmarker.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmaster.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmerge.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdphuff.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdhuff.h)
+jdpostct.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdsample.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdtrans.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jerror.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jversion.h,jerror.h)
+jfdctflt.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jfdctfst.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jfdctint.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctflt.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctfst.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctint.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctred.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jquant1.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jquant2.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jutils.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jmemmgr.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jmemsys.h)
+jmemansi.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jmemsys.h)
diff --git a/test/monniaux/jpeg-6b/maktjpeg.st b/test/monniaux/jpeg-6b/maktjpeg.st
new file mode 100644
index 00000000..31f4d169
--- /dev/null
+++ b/test/monniaux/jpeg-6b/maktjpeg.st
@@ -0,0 +1,32 @@
+; Project file for Independent JPEG Group's software
+;
+; This project file is for Atari ST/STE/TT systems using Pure C or Turbo C.
+; Thanks to Frank Moehle (Frank.Moehle@arbi.informatik.uni-oldenburg.de),
+; Dr. B. Setzepfandt (bernd@gina.uni-muenster.de),
+; and Guido Vollbeding (guivol@esc.de).
+;
+; To use this file, rename it to jpegtran.prj.
+; If you are using Turbo C, change filenames beginning with "pc..." to "tc..."
+; Read installation instructions before trying to make the program!
+;
+;
+; * * * Output file * * *
+jpegtran.ttp
+;
+; * * * COMPILER OPTIONS * * *
+.C[-P] ; absolute calls
+.C[-M] ; and no string merging, folks
+.C[-w-cln] ; no "constant is long" warnings
+.C[-w-par] ; no "parameter xxxx unused"
+.C[-w-rch] ; no "unreachable code"
+.C[-wsig] ; warn if significant digits may be lost
+=
+; * * * * List of modules * * * *
+pcstart.o
+jpegtran.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h,transupp.h,jversion.h)
+cdjpeg.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdswitch.c (cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+transupp.c (jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,transupp.h)
+libjpeg.lib ; built by libjpeg.prj
+pcstdlib.lib ; standard library
+pcextlib.lib ; extended library
diff --git a/test/monniaux/jpeg-6b/makvms.opt b/test/monniaux/jpeg-6b/makvms.opt
new file mode 100644
index 00000000..675e8fe9
--- /dev/null
+++ b/test/monniaux/jpeg-6b/makvms.opt
@@ -0,0 +1,4 @@
+! A pointer to the VAX/VMS C Run-Time Shareable Library.
+! This file is needed by makefile.mms and makefile.vms,
+! but only for the older VAX C compiler. DEC C does not need it.
+Sys$Library:VAXCRTL.EXE /Share
diff --git a/test/monniaux/jpeg-6b/rdbmp.c b/test/monniaux/jpeg-6b/rdbmp.c
new file mode 100644
index 00000000..b05fe2ac
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdbmp.c
@@ -0,0 +1,439 @@
+/*
+ * rdbmp.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Microsoft "BMP"
+ * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
+ * Currently, only 8-bit and 24-bit images are supported, not 1-bit or
+ * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
+ * Also, we don't support RLE-compressed files.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume input from
+ * an ordinary stdio stream. They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed BMP format).
+ *
+ * This code contributed by James Arthur Boucher.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef BMP_SUPPORTED
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x) ((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x) ((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x) ((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/* Private version of data source object */
+
+typedef struct _bmp_source_struct * bmp_source_ptr;
+
+typedef struct _bmp_source_struct {
+ struct cjpeg_source_struct pub; /* public fields */
+
+ j_compress_ptr cinfo; /* back link saves passing separate parm */
+
+ JSAMPARRAY colormap; /* BMP colormap (converted to my format) */
+
+ jvirt_sarray_ptr whole_image; /* Needed to reverse row order */
+ JDIMENSION source_row; /* Current source row number */
+ JDIMENSION row_width; /* Physical width of scanlines in file */
+
+ int bits_per_pixel; /* remembers 8- or 24-bit format */
+} bmp_source_struct;
+
+
+LOCAL(int)
+read_byte (bmp_source_ptr sinfo)
+/* Read next byte from BMP file */
+{
+ register FILE *infile = sinfo->pub.input_file;
+ register int c;
+
+ if ((c = getc(infile)) == EOF)
+ ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+ return c;
+}
+
+
+LOCAL(void)
+read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
+/* Read the colormap from a BMP file */
+{
+ int i;
+
+ switch (mapentrysize) {
+ case 3:
+ /* BGR format (occurs in OS/2 files) */
+ for (i = 0; i < cmaplen; i++) {
+ sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+ }
+ break;
+ case 4:
+ /* BGR0 format (occurs in MS Windows files) */
+ for (i = 0; i < cmaplen; i++) {
+ sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+ (void) read_byte(sinfo);
+ }
+ break;
+ default:
+ ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
+ break;
+ }
+}
+
+
+/*
+ * Read one row of pixels.
+ * The image has been read into the whole_image array, but is otherwise
+ * unprocessed. We must read it out in top-to-bottom row order, and if
+ * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
+ */
+
+METHODDEF(JDIMENSION)
+get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit colormap indexes */
+{
+ bmp_source_ptr source = (bmp_source_ptr) sinfo;
+ register JSAMPARRAY colormap = source->colormap;
+ JSAMPARRAY image_ptr;
+ register int t;
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+
+ /* Fetch next row from virtual array */
+ source->source_row--;
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->whole_image,
+ source->source_row, (JDIMENSION) 1, FALSE);
+
+ /* Expand the colormap indexes to real data */
+ inptr = image_ptr[0];
+ outptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ t = GETJSAMPLE(*inptr++);
+ *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */
+ *outptr++ = colormap[1][t];
+ *outptr++ = colormap[2][t];
+ }
+
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 24-bit pixels */
+{
+ bmp_source_ptr source = (bmp_source_ptr) sinfo;
+ JSAMPARRAY image_ptr;
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+
+ /* Fetch next row from virtual array */
+ source->source_row--;
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->whole_image,
+ source->source_row, (JDIMENSION) 1, FALSE);
+
+ /* Transfer data. Note source values are in BGR order
+ * (even though Microsoft's own documents say the opposite).
+ */
+ inptr = image_ptr[0];
+ outptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
+ outptr[1] = *inptr++;
+ outptr[0] = *inptr++;
+ outptr += 3;
+ }
+
+ return 1;
+}
+
+
+/*
+ * This method loads the image into whole_image during the first call on
+ * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
+ * get_8bit_row or get_24bit_row on subsequent calls.
+ */
+
+METHODDEF(JDIMENSION)
+preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ bmp_source_ptr source = (bmp_source_ptr) sinfo;
+ register FILE *infile = source->pub.input_file;
+ register int c;
+ register JSAMPROW out_ptr;
+ JSAMPARRAY image_ptr;
+ JDIMENSION row, col;
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+ /* Read the data into a virtual array in input-file row order. */
+ for (row = 0; row < cinfo->image_height; row++) {
+ if (progress != NULL) {
+ progress->pub.pass_counter = (long) row;
+ progress->pub.pass_limit = (long) cinfo->image_height;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->whole_image,
+ row, (JDIMENSION) 1, TRUE);
+ out_ptr = image_ptr[0];
+ for (col = source->row_width; col > 0; col--) {
+ /* inline copy of read_byte() for speed */
+ if ((c = getc(infile)) == EOF)
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ *out_ptr++ = (JSAMPLE) c;
+ }
+ }
+ if (progress != NULL)
+ progress->completed_extra_passes++;
+
+ /* Set up to read from the virtual array in top-to-bottom order */
+ switch (source->bits_per_pixel) {
+ case 8:
+ source->pub.get_pixel_rows = get_8bit_row;
+ break;
+ case 24:
+ source->pub.get_pixel_rows = get_24bit_row;
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+ }
+ source->source_row = cinfo->image_height;
+
+ /* And read the first row */
+ return (*source->pub.get_pixel_rows) (cinfo, sinfo);
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF(void)
+start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ bmp_source_ptr source = (bmp_source_ptr) sinfo;
+ U_CHAR bmpfileheader[14];
+ U_CHAR bmpinfoheader[64];
+#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \
+ (((unsigned int) UCH(array[offset+1])) << 8))
+#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \
+ (((INT32) UCH(array[offset+1])) << 8) + \
+ (((INT32) UCH(array[offset+2])) << 16) + \
+ (((INT32) UCH(array[offset+3])) << 24))
+ INT32 bfOffBits;
+ INT32 headerSize;
+ INT32 biWidth = 0; /* initialize to avoid compiler warning */
+ INT32 biHeight = 0;
+ unsigned int biPlanes;
+ INT32 biCompression;
+ INT32 biXPelsPerMeter,biYPelsPerMeter;
+ INT32 biClrUsed = 0;
+ int mapentrysize = 0; /* 0 indicates no colormap */
+ INT32 bPad;
+ JDIMENSION row_width;
+
+ /* Read and verify the bitmap file header */
+ if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
+ ERREXIT(cinfo, JERR_BMP_NOT);
+ bfOffBits = (INT32) GET_4B(bmpfileheader,10);
+ /* We ignore the remaining fileheader fields */
+
+ /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
+ * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which.
+ */
+ if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ headerSize = (INT32) GET_4B(bmpinfoheader,0);
+ if (headerSize < 12 || headerSize > 64)
+ ERREXIT(cinfo, JERR_BMP_BADHEADER);
+ if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+
+ switch ((int) headerSize) {
+ case 12:
+ /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
+ biWidth = (INT32) GET_2B(bmpinfoheader,4);
+ biHeight = (INT32) GET_2B(bmpinfoheader,6);
+ biPlanes = GET_2B(bmpinfoheader,8);
+ source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);
+
+ switch (source->bits_per_pixel) {
+ case 8: /* colormapped image */
+ mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */
+ TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
+ break;
+ case 24: /* RGB image */
+ TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+ break;
+ }
+ if (biPlanes != 1)
+ ERREXIT(cinfo, JERR_BMP_BADPLANES);
+ break;
+ case 40:
+ case 64:
+ /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
+ /* or OS/2 2.x header, which has additional fields that we ignore */
+ biWidth = GET_4B(bmpinfoheader,4);
+ biHeight = GET_4B(bmpinfoheader,8);
+ biPlanes = GET_2B(bmpinfoheader,12);
+ source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);
+ biCompression = GET_4B(bmpinfoheader,16);
+ biXPelsPerMeter = GET_4B(bmpinfoheader,24);
+ biYPelsPerMeter = GET_4B(bmpinfoheader,28);
+ biClrUsed = GET_4B(bmpinfoheader,32);
+ /* biSizeImage, biClrImportant fields are ignored */
+
+ switch (source->bits_per_pixel) {
+ case 8: /* colormapped image */
+ mapentrysize = 4; /* Windows uses RGBQUAD colormap */
+ TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
+ break;
+ case 24: /* RGB image */
+ TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+ break;
+ }
+ if (biPlanes != 1)
+ ERREXIT(cinfo, JERR_BMP_BADPLANES);
+ if (biCompression != 0)
+ ERREXIT(cinfo, JERR_BMP_COMPRESSED);
+
+ if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
+ /* Set JFIF density parameters from the BMP data */
+ cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
+ cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
+ cinfo->density_unit = 2; /* dots/cm */
+ }
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BMP_BADHEADER);
+ break;
+ }
+
+ /* Compute distance to bitmap data --- will adjust for colormap below */
+ bPad = bfOffBits - (headerSize + 14);
+
+ /* Read the colormap, if any */
+ if (mapentrysize > 0) {
+ if (biClrUsed <= 0)
+ biClrUsed = 256; /* assume it's 256 */
+ else if (biClrUsed > 256)
+ ERREXIT(cinfo, JERR_BMP_BADCMAP);
+ /* Allocate space to store the colormap */
+ source->colormap = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) biClrUsed, (JDIMENSION) 3);
+ /* and read it from the file */
+ read_colormap(source, (int) biClrUsed, mapentrysize);
+ /* account for size of colormap */
+ bPad -= biClrUsed * mapentrysize;
+ }
+
+ /* Skip any remaining pad bytes */
+ if (bPad < 0) /* incorrect bfOffBits value? */
+ ERREXIT(cinfo, JERR_BMP_BADHEADER);
+ while (--bPad >= 0) {
+ (void) read_byte(source);
+ }
+
+ /* Compute row width in file, including padding to 4-byte boundary */
+ if (source->bits_per_pixel == 24)
+ row_width = (JDIMENSION) (biWidth * 3);
+ else
+ row_width = (JDIMENSION) biWidth;
+ while ((row_width & 3) != 0) row_width++;
+ source->row_width = row_width;
+
+ /* Allocate space for inversion array, prepare for preload pass */
+ source->whole_image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
+ source->pub.get_pixel_rows = preload_image;
+ if (cinfo->progress != NULL) {
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+ progress->total_extra_passes++; /* count file input as separate pass */
+ }
+
+ /* Allocate one-row buffer for returned data */
+ source->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
+ source->pub.buffer_height = 1;
+
+ cinfo->in_color_space = JCS_RGB;
+ cinfo->input_components = 3;
+ cinfo->data_precision = 8;
+ cinfo->image_width = (JDIMENSION) biWidth;
+ cinfo->image_height = (JDIMENSION) biHeight;
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ /* no work */
+}
+
+
+/*
+ * The module selection routine for BMP format input.
+ */
+
+GLOBAL(cjpeg_source_ptr)
+jinit_read_bmp (j_compress_ptr cinfo)
+{
+ bmp_source_ptr source;
+
+ /* Create module interface object */
+ source = (bmp_source_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(bmp_source_struct));
+ source->cinfo = cinfo; /* make back link for subroutines */
+ /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+ source->pub.start_input = start_input_bmp;
+ source->pub.finish_input = finish_input_bmp;
+
+ return (cjpeg_source_ptr) source;
+}
+
+#endif /* BMP_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/rdcolmap.c b/test/monniaux/jpeg-6b/rdcolmap.c
new file mode 100644
index 00000000..42b34376
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdcolmap.c
@@ -0,0 +1,253 @@
+/*
+ * rdcolmap.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file implements djpeg's "-map file" switch. It reads a source image
+ * and constructs a colormap to be supplied to the JPEG decompressor.
+ *
+ * Currently, these file formats are supported for the map file:
+ * GIF: the contents of the GIF's global colormap are used.
+ * PPM (either text or raw flavor): the entire file is read and
+ * each unique pixel value is entered in the map.
+ * Note that reading a large PPM file will be horrendously slow.
+ * Typically, a PPM-format map file should contain just one pixel
+ * of each desired color. Such a file can be extracted from an
+ * ordinary image PPM file with ppmtomap(1).
+ *
+ * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
+ * currently implemented.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
+
+/* Portions of this code are based on the PBMPLUS library, which is:
+**
+** Copyright (C) 1988 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation. This software is provided "as is" without express or
+** implied warranty.
+*/
+
+
+/*
+ * Add a (potentially) new color to the color map.
+ */
+
+LOCAL(void)
+add_map_entry (j_decompress_ptr cinfo, int R, int G, int B)
+{
+ JSAMPROW colormap0 = cinfo->colormap[0];
+ JSAMPROW colormap1 = cinfo->colormap[1];
+ JSAMPROW colormap2 = cinfo->colormap[2];
+ int ncolors = cinfo->actual_number_of_colors;
+ int index;
+
+ /* Check for duplicate color. */
+ for (index = 0; index < ncolors; index++) {
+ if (GETJSAMPLE(colormap0[index]) == R &&
+ GETJSAMPLE(colormap1[index]) == G &&
+ GETJSAMPLE(colormap2[index]) == B)
+ return; /* color is already in map */
+ }
+
+ /* Check for map overflow. */
+ if (ncolors >= (MAXJSAMPLE+1))
+ ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
+
+ /* OK, add color to map. */
+ colormap0[ncolors] = (JSAMPLE) R;
+ colormap1[ncolors] = (JSAMPLE) G;
+ colormap2[ncolors] = (JSAMPLE) B;
+ cinfo->actual_number_of_colors++;
+}
+
+
+/*
+ * Extract color map from a GIF file.
+ */
+
+LOCAL(void)
+read_gif_map (j_decompress_ptr cinfo, FILE * infile)
+{
+ int header[13];
+ int i, colormaplen;
+ int R, G, B;
+
+ /* Initial 'G' has already been read by read_color_map */
+ /* Read the rest of the GIF header and logical screen descriptor */
+ for (i = 1; i < 13; i++) {
+ if ((header[i] = getc(infile)) == EOF)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ }
+
+ /* Verify GIF Header */
+ if (header[1] != 'I' || header[2] != 'F')
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+ /* There must be a global color map. */
+ if ((header[10] & 0x80) == 0)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+ /* OK, fetch it. */
+ colormaplen = 2 << (header[10] & 0x07);
+
+ for (i = 0; i < colormaplen; i++) {
+ R = getc(infile);
+ G = getc(infile);
+ B = getc(infile);
+ if (R == EOF || G == EOF || B == EOF)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ add_map_entry(cinfo,
+ R << (BITS_IN_JSAMPLE-8),
+ G << (BITS_IN_JSAMPLE-8),
+ B << (BITS_IN_JSAMPLE-8));
+ }
+}
+
+
+/* Support routines for reading PPM */
+
+
+LOCAL(int)
+pbm_getc (FILE * infile)
+/* Read next char, skipping over any comments */
+/* A comment/newline sequence is returned as a newline */
+{
+ register int ch;
+
+ ch = getc(infile);
+ if (ch == '#') {
+ do {
+ ch = getc(infile);
+ } while (ch != '\n' && ch != EOF);
+ }
+ return ch;
+}
+
+
+LOCAL(unsigned int)
+read_pbm_integer (j_decompress_ptr cinfo, FILE * infile)
+/* Read an unsigned decimal integer from the PPM file */
+/* Swallows one trailing character after the integer */
+/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
+/* This should not be a problem in practice. */
+{
+ register int ch;
+ register unsigned int val;
+
+ /* Skip any leading whitespace */
+ do {
+ ch = pbm_getc(infile);
+ if (ch == EOF)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
+
+ if (ch < '0' || ch > '9')
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+ val = ch - '0';
+ while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
+ val *= 10;
+ val += ch - '0';
+ }
+ return val;
+}
+
+
+/*
+ * Extract color map from a PPM file.
+ */
+
+LOCAL(void)
+read_ppm_map (j_decompress_ptr cinfo, FILE * infile)
+{
+ int c;
+ unsigned int w, h, maxval, row, col;
+ int R, G, B;
+
+ /* Initial 'P' has already been read by read_color_map */
+ c = getc(infile); /* save format discriminator for a sec */
+
+ /* while we fetch the remaining header info */
+ w = read_pbm_integer(cinfo, infile);
+ h = read_pbm_integer(cinfo, infile);
+ maxval = read_pbm_integer(cinfo, infile);
+
+ if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+ /* For now, we don't support rescaling from an unusual maxval. */
+ if (maxval != (unsigned int) MAXJSAMPLE)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+ switch (c) {
+ case '3': /* it's a text-format PPM file */
+ for (row = 0; row < h; row++) {
+ for (col = 0; col < w; col++) {
+ R = read_pbm_integer(cinfo, infile);
+ G = read_pbm_integer(cinfo, infile);
+ B = read_pbm_integer(cinfo, infile);
+ add_map_entry(cinfo, R, G, B);
+ }
+ }
+ break;
+
+ case '6': /* it's a raw-format PPM file */
+ for (row = 0; row < h; row++) {
+ for (col = 0; col < w; col++) {
+ R = getc(infile);
+ G = getc(infile);
+ B = getc(infile);
+ if (R == EOF || G == EOF || B == EOF)
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ add_map_entry(cinfo, R, G, B);
+ }
+ }
+ break;
+
+ default:
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ break;
+ }
+}
+
+
+/*
+ * Main entry point from djpeg.c.
+ * Input: opened input file (from file name argument on command line).
+ * Output: colormap and actual_number_of_colors fields are set in cinfo.
+ */
+
+GLOBAL(void)
+read_color_map (j_decompress_ptr cinfo, FILE * infile)
+{
+ /* Allocate space for a color map of maximum supported size. */
+ cinfo->colormap = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3);
+ cinfo->actual_number_of_colors = 0; /* initialize map to empty */
+
+ /* Read first byte to determine file format */
+ switch (getc(infile)) {
+ case 'G':
+ read_gif_map(cinfo, infile);
+ break;
+ case 'P':
+ read_ppm_map(cinfo, infile);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+ break;
+ }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/rdgif.c b/test/monniaux/jpeg-6b/rdgif.c
new file mode 100644
index 00000000..b27c1675
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdgif.c
@@ -0,0 +1,38 @@
+/*
+ * rdgif.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in GIF format.
+ *
+ *****************************************************************************
+ * NOTE: to avoid entanglements with Unisys' patent on LZW compression, *
+ * the ability to read GIF files has been removed from the IJG distribution. *
+ * Sorry about that. *
+ *****************************************************************************
+ *
+ * We are required to state that
+ * "The Graphics Interchange Format(c) is the Copyright property of
+ * CompuServe Incorporated. GIF(sm) is a Service Mark property of
+ * CompuServe Incorporated."
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef GIF_SUPPORTED
+
+/*
+ * The module selection routine for GIF format input.
+ */
+
+GLOBAL(cjpeg_source_ptr)
+jinit_read_gif (j_compress_ptr cinfo)
+{
+ fprintf(stderr, "GIF input is unsupported for legal reasons. Sorry.\n");
+ exit(EXIT_FAILURE);
+ return NULL; /* keep compiler happy */
+}
+
+#endif /* GIF_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/rdjpgcom.1 b/test/monniaux/jpeg-6b/rdjpgcom.1
new file mode 100644
index 00000000..2bba04e1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdjpgcom.1
@@ -0,0 +1,54 @@
+.TH RDJPGCOM 1 "11 October 1997"
+.SH NAME
+rdjpgcom \- display text comments from a JPEG file
+.SH SYNOPSIS
+.B rdjpgcom
+[
+.B \-verbose
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B rdjpgcom
+reads the named JPEG/JFIF file, or the standard input if no file is named,
+and prints any text comments found in the file on the standard output.
+.PP
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings. This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text. COM blocks do not interfere with the image stored in the JPEG
+file. The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+.SH OPTIONS
+.TP
+.B \-verbose
+Causes
+.B rdjpgcom
+to also display the JPEG image dimensions.
+.PP
+Switch names may be abbreviated, and are not case sensitive.
+.SH HINTS
+.B rdjpgcom
+does not depend on the IJG JPEG library. Its source code is intended as an
+illustration of the minimum amount of code required to parse a JPEG file
+header correctly.
+.PP
+In
+.B \-verbose
+mode,
+.B rdjpgcom
+will also attempt to print the contents of any "APP12" markers as text.
+Some digital cameras produce APP12 markers containing useful textual
+information. If you like, you can modify the source code to print
+other APPn marker types as well.
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR djpeg (1),
+.BR jpegtran (1),
+.BR wrjpgcom (1)
+.SH AUTHOR
+Independent JPEG Group
diff --git a/test/monniaux/jpeg-6b/rdjpgcom.c b/test/monniaux/jpeg-6b/rdjpgcom.c
new file mode 100644
index 00000000..ffe6fc62
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdjpgcom.c
@@ -0,0 +1,496 @@
+/*
+ * rdjpgcom.c
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a very simple stand-alone application that displays
+ * the text in COM (comment) markers in a JFIF file.
+ * This may be useful as an example of the minimum logic needed to parse
+ * JPEG markers.
+ */
+
+#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
+#include "jinclude.h" /* get auto-config symbols, <stdio.h> */
+
+#include <ctype.h> /* to declare isupper(), tolower() */
+#ifdef USE_SETMODE
+#include <fcntl.h> /* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h> /* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h> /* Metrowerks needs this */
+#include <console.h> /* ... and this */
+#endif
+#ifdef THINK_C
+#include <console.h> /* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#else
+#ifdef VMS /* VMS is very nonstandard */
+#define READ_BINARY "rb", "ctx=stm"
+#else /* standard ANSI-compliant case */
+#define READ_BINARY "rb"
+#endif
+#endif
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS 0
+#endif
+#endif
+
+
+/*
+ * These macros are used to read the input file.
+ * To reuse this code in another application, you might need to change these.
+ */
+
+static FILE * infile; /* input JPEG file */
+
+/* Return next input byte, or EOF if no more */
+#define NEXTBYTE() getc(infile)
+
+
+/* Error exit handler */
+#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
+
+
+/* Read one byte, testing for EOF */
+static int
+read_1_byte (void)
+{
+ int c;
+
+ c = NEXTBYTE();
+ if (c == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ return c;
+}
+
+/* Read 2 bytes, convert to unsigned int */
+/* All 2-byte quantities in JPEG markers are MSB first */
+static unsigned int
+read_2_bytes (void)
+{
+ int c1, c2;
+
+ c1 = NEXTBYTE();
+ if (c1 == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ c2 = NEXTBYTE();
+ if (c2 == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+}
+
+
+/*
+ * JPEG markers consist of one or more 0xFF bytes, followed by a marker
+ * code byte (which is not an FF). Here are the marker codes of interest
+ * in this program. (See jdmarker.c for a more complete list.)
+ */
+
+#define M_SOF0 0xC0 /* Start Of Frame N */
+#define M_SOF1 0xC1 /* N indicates which compression process */
+#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
+#define M_SOF3 0xC3
+#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
+#define M_SOF6 0xC6
+#define M_SOF7 0xC7
+#define M_SOF9 0xC9
+#define M_SOF10 0xCA
+#define M_SOF11 0xCB
+#define M_SOF13 0xCD
+#define M_SOF14 0xCE
+#define M_SOF15 0xCF
+#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
+#define M_EOI 0xD9 /* End Of Image (end of datastream) */
+#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
+#define M_APP0 0xE0 /* Application-specific marker, type N */
+#define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */
+#define M_COM 0xFE /* COMment */
+
+
+/*
+ * Find the next JPEG marker and return its marker code.
+ * We expect at least one FF byte, possibly more if the compressor used FFs
+ * to pad the file.
+ * There could also be non-FF garbage between markers. The treatment of such
+ * garbage is unspecified; we choose to skip over it but emit a warning msg.
+ * NB: this routine must not be used after seeing SOS marker, since it will
+ * not deal correctly with FF/00 sequences in the compressed image data...
+ */
+
+static int
+next_marker (void)
+{
+ int c;
+ int discarded_bytes = 0;
+
+ /* Find 0xFF byte; count and skip any non-FFs. */
+ c = read_1_byte();
+ while (c != 0xFF) {
+ discarded_bytes++;
+ c = read_1_byte();
+ }
+ /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
+ * are legal as pad bytes, so don't count them in discarded_bytes.
+ */
+ do {
+ c = read_1_byte();
+ } while (c == 0xFF);
+
+ if (discarded_bytes != 0) {
+ fprintf(stderr, "Warning: garbage data found in JPEG file\n");
+ }
+
+ return c;
+}
+
+
+/*
+ * Read the initial marker, which should be SOI.
+ * For a JFIF file, the first two bytes of the file should be literally
+ * 0xFF M_SOI. To be more general, we could use next_marker, but if the
+ * input file weren't actually JPEG at all, next_marker might read the whole
+ * file and then return a misleading error message...
+ */
+
+static int
+first_marker (void)
+{
+ int c1, c2;
+
+ c1 = NEXTBYTE();
+ c2 = NEXTBYTE();
+ if (c1 != 0xFF || c2 != M_SOI)
+ ERREXIT("Not a JPEG file");
+ return c2;
+}
+
+
+/*
+ * Most types of marker are followed by a variable-length parameter segment.
+ * This routine skips over the parameters for any marker we don't otherwise
+ * want to process.
+ * Note that we MUST skip the parameter segment explicitly in order not to
+ * be fooled by 0xFF bytes that might appear within the parameter segment;
+ * such bytes do NOT introduce new markers.
+ */
+
+static void
+skip_variable (void)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+ unsigned int length;
+
+ /* Get the marker parameter length count */
+ length = read_2_bytes();
+ /* Length includes itself, so must be at least 2 */
+ if (length < 2)
+ ERREXIT("Erroneous JPEG marker length");
+ length -= 2;
+ /* Skip over the remaining bytes */
+ while (length > 0) {
+ (void) read_1_byte();
+ length--;
+ }
+}
+
+
+/*
+ * Process a COM marker.
+ * We want to print out the marker contents as legible text;
+ * we must guard against non-text junk and varying newline representations.
+ */
+
+static void
+process_COM (void)
+{
+ unsigned int length;
+ int ch;
+ int lastch = 0;
+
+ /* Get the marker parameter length count */
+ length = read_2_bytes();
+ /* Length includes itself, so must be at least 2 */
+ if (length < 2)
+ ERREXIT("Erroneous JPEG marker length");
+ length -= 2;
+
+ while (length > 0) {
+ ch = read_1_byte();
+ /* Emit the character in a readable form.
+ * Nonprintables are converted to \nnn form,
+ * while \ is converted to \\.
+ * Newlines in CR, CR/LF, or LF form will be printed as one newline.
+ */
+ if (ch == '\r') {
+ printf("\n");
+ } else if (ch == '\n') {
+ if (lastch != '\r')
+ printf("\n");
+ } else if (ch == '\\') {
+ printf("\\\\");
+ } else if (isprint(ch)) {
+ putc(ch, stdout);
+ } else {
+ printf("\\%03o", ch);
+ }
+ lastch = ch;
+ length--;
+ }
+ printf("\n");
+}
+
+
+/*
+ * Process a SOFn marker.
+ * This code is only needed if you want to know the image dimensions...
+ */
+
+static void
+process_SOFn (int marker)
+{
+ unsigned int length;
+ unsigned int image_height, image_width;
+ int data_precision, num_components;
+ const char * process;
+ int ci;
+
+ length = read_2_bytes(); /* usual parameter length count */
+
+ data_precision = read_1_byte();
+ image_height = read_2_bytes();
+ image_width = read_2_bytes();
+ num_components = read_1_byte();
+
+ switch (marker) {
+ case M_SOF0: process = "Baseline"; break;
+ case M_SOF1: process = "Extended sequential"; break;
+ case M_SOF2: process = "Progressive"; break;
+ case M_SOF3: process = "Lossless"; break;
+ case M_SOF5: process = "Differential sequential"; break;
+ case M_SOF6: process = "Differential progressive"; break;
+ case M_SOF7: process = "Differential lossless"; break;
+ case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
+ case M_SOF10: process = "Progressive, arithmetic coding"; break;
+ case M_SOF11: process = "Lossless, arithmetic coding"; break;
+ case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
+ case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
+ case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
+ default: process = "Unknown"; break;
+ }
+
+ printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
+ image_width, image_height, num_components, data_precision);
+ printf("JPEG process: %s\n", process);
+
+ if (length != (unsigned int) (8 + num_components * 3))
+ ERREXIT("Bogus SOF marker length");
+
+ for (ci = 0; ci < num_components; ci++) {
+ (void) read_1_byte(); /* Component ID code */
+ (void) read_1_byte(); /* H, V sampling factors */
+ (void) read_1_byte(); /* Quantization table number */
+ }
+}
+
+
+/*
+ * Parse the marker stream until SOS or EOI is seen;
+ * display any COM markers.
+ * While the companion program wrjpgcom will always insert COM markers before
+ * SOFn, other implementations might not, so we scan to SOS before stopping.
+ * If we were only interested in the image dimensions, we would stop at SOFn.
+ * (Conversely, if we only cared about COM markers, there would be no need
+ * for special code to handle SOFn; we could treat it like other markers.)
+ */
+
+static int
+scan_JPEG_header (int verbose)
+{
+ int marker;
+
+ /* Expect SOI at start of file */
+ if (first_marker() != M_SOI)
+ ERREXIT("Expected SOI marker first");
+
+ /* Scan miscellaneous markers until we reach SOS. */
+ for (;;) {
+ marker = next_marker();
+ switch (marker) {
+ /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
+ * treated as SOFn. C4 in particular is actually DHT.
+ */
+ case M_SOF0: /* Baseline */
+ case M_SOF1: /* Extended sequential, Huffman */
+ case M_SOF2: /* Progressive, Huffman */
+ case M_SOF3: /* Lossless, Huffman */
+ case M_SOF5: /* Differential sequential, Huffman */
+ case M_SOF6: /* Differential progressive, Huffman */
+ case M_SOF7: /* Differential lossless, Huffman */
+ case M_SOF9: /* Extended sequential, arithmetic */
+ case M_SOF10: /* Progressive, arithmetic */
+ case M_SOF11: /* Lossless, arithmetic */
+ case M_SOF13: /* Differential sequential, arithmetic */
+ case M_SOF14: /* Differential progressive, arithmetic */
+ case M_SOF15: /* Differential lossless, arithmetic */
+ if (verbose)
+ process_SOFn(marker);
+ else
+ skip_variable();
+ break;
+
+ case M_SOS: /* stop before hitting compressed data */
+ return marker;
+
+ case M_EOI: /* in case it's a tables-only JPEG stream */
+ return marker;
+
+ case M_COM:
+ process_COM();
+ break;
+
+ case M_APP12:
+ /* Some digital camera makers put useful textual information into
+ * APP12 markers, so we print those out too when in -verbose mode.
+ */
+ if (verbose) {
+ printf("APP12 contains:\n");
+ process_COM();
+ } else
+ skip_variable();
+ break;
+
+ default: /* Anything else just gets skipped */
+ skip_variable(); /* we assume it has a parameter count... */
+ break;
+ }
+ } /* end loop */
+}
+
+
+/* Command line parsing code */
+
+static const char * progname; /* program name for error messages */
+
+
+static void
+usage (void)
+/* complain about bad command line */
+{
+ fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
+
+ fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
+
+ fprintf(stderr, "Switches (names may be abbreviated):\n");
+ fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
+
+ exit(EXIT_FAILURE);
+}
+
+
+static int
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+ register int ca, ck;
+ register int nmatched = 0;
+
+ while ((ca = *arg++) != '\0') {
+ if ((ck = *keyword++) == '\0')
+ return 0; /* arg longer than keyword, no good */
+ if (isupper(ca)) /* force arg to lcase (assume ck is already) */
+ ca = tolower(ca);
+ if (ca != ck)
+ return 0; /* no good */
+ nmatched++; /* count matched characters */
+ }
+ /* reached end of argument; fail if it's too short for unique abbrev */
+ if (nmatched < minchars)
+ return 0;
+ return 1; /* A-OK */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+ int argn;
+ char * arg;
+ int verbose = 0;
+
+ /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+ argc = ccommand(&argv);
+#endif
+
+ progname = argv[0];
+ if (progname == NULL || progname[0] == 0)
+ progname = "rdjpgcom"; /* in case C library doesn't provide it */
+
+ /* Parse switches, if any */
+ for (argn = 1; argn < argc; argn++) {
+ arg = argv[argn];
+ if (arg[0] != '-')
+ break; /* not switch, must be file name */
+ arg++; /* advance over '-' */
+ if (keymatch(arg, "verbose", 1)) {
+ verbose++;
+ } else
+ usage();
+ }
+
+ /* Open the input file. */
+ /* Unix style: expect zero or one file name */
+ if (argn < argc-1) {
+ fprintf(stderr, "%s: only one input file\n", progname);
+ usage();
+ }
+ if (argn < argc) {
+ if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default input file is stdin */
+#ifdef USE_SETMODE /* need to hack file mode? */
+ setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN /* need to re-open in binary mode? */
+ if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open stdin\n", progname);
+ exit(EXIT_FAILURE);
+ }
+#else
+ infile = stdin;
+#endif
+ }
+
+ /* Scan the JPEG headers. */
+ (void) scan_JPEG_header(verbose);
+
+ /* All done. */
+ exit(EXIT_SUCCESS);
+ return 0; /* suppress no-return-value warnings */
+}
diff --git a/test/monniaux/jpeg-6b/rdppm.c b/test/monniaux/jpeg-6b/rdppm.c
new file mode 100644
index 00000000..1df35c1b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdppm.c
@@ -0,0 +1,458 @@
+/*
+ * rdppm.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in PPM/PGM format.
+ * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
+ * The PBMPLUS library is NOT required to compile this software
+ * (but it is highly useful as a set of PPM image manipulation programs).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume input from
+ * an ordinary stdio stream. They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed PPM format).
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef PPM_SUPPORTED
+
+
+/* Portions of this code are based on the PBMPLUS library, which is:
+**
+** Copyright (C) 1988 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation. This software is provided "as is" without express or
+** implied warranty.
+*/
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x) ((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x) ((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x) ((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/*
+ * On most systems, reading individual bytes with getc() is drastically less
+ * efficient than buffering a row at a time with fread(). On PCs, we must
+ * allocate the buffer in near data space, because we are assuming small-data
+ * memory model, wherein fread() can't reach far memory. If you need to
+ * process very wide images on a PC, you might have to compile in large-memory
+ * model, or else replace fread() with a getc() loop --- which will be much
+ * slower.
+ */
+
+
+/* Private version of data source object */
+
+typedef struct {
+ struct cjpeg_source_struct pub; /* public fields */
+
+ U_CHAR *iobuffer; /* non-FAR pointer to I/O buffer */
+ JSAMPROW pixrow; /* FAR pointer to same */
+ size_t buffer_width; /* width of I/O buffer */
+ JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
+} ppm_source_struct;
+
+typedef ppm_source_struct * ppm_source_ptr;
+
+
+LOCAL(int)
+pbm_getc (FILE * infile)
+/* Read next char, skipping over any comments */
+/* A comment/newline sequence is returned as a newline */
+{
+ register int ch;
+
+ ch = getc(infile);
+ if (ch == '#') {
+ do {
+ ch = getc(infile);
+ } while (ch != '\n' && ch != EOF);
+ }
+ return ch;
+}
+
+
+LOCAL(unsigned int)
+read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
+/* Read an unsigned decimal integer from the PPM file */
+/* Swallows one trailing character after the integer */
+/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
+/* This should not be a problem in practice. */
+{
+ register int ch;
+ register unsigned int val;
+
+ /* Skip any leading whitespace */
+ do {
+ ch = pbm_getc(infile);
+ if (ch == EOF)
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
+
+ if (ch < '0' || ch > '9')
+ ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
+
+ val = ch - '0';
+ while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
+ val *= 10;
+ val += ch - '0';
+ }
+ return val;
+}
+
+
+/*
+ * Read one row of pixels.
+ *
+ * We provide several different versions depending on input file format.
+ * In all cases, input is scaled to the size of JSAMPLE.
+ *
+ * A really fast path is provided for reading byte/sample raw files with
+ * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
+ */
+
+
+METHODDEF(JDIMENSION)
+get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading text-format PGM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ FILE * infile = source->pub.input_file;
+ register JSAMPROW ptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+ }
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading text-format PPM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ FILE * infile = source->pub.input_file;
+ register JSAMPROW ptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+ *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+ *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+ }
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-byte-format PGM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register U_CHAR * bufferptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ ptr = source->pub.buffer[0];
+ bufferptr = source->iobuffer;
+ for (col = cinfo->image_width; col > 0; col--) {
+ *ptr++ = rescale[UCH(*bufferptr++)];
+ }
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-byte-format PPM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register U_CHAR * bufferptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ ptr = source->pub.buffer[0];
+ bufferptr = source->iobuffer;
+ for (col = cinfo->image_width; col > 0; col--) {
+ *ptr++ = rescale[UCH(*bufferptr++)];
+ *ptr++ = rescale[UCH(*bufferptr++)];
+ *ptr++ = rescale[UCH(*bufferptr++)];
+ }
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
+ * In this case we just read right into the JSAMPLE buffer!
+ * Note that same code works for PPM and PGM files.
+ */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+
+ if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-word-format PGM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register U_CHAR * bufferptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ ptr = source->pub.buffer[0];
+ bufferptr = source->iobuffer;
+ for (col = cinfo->image_width; col > 0; col--) {
+ register int temp;
+ temp = UCH(*bufferptr++);
+ temp |= UCH(*bufferptr++) << 8;
+ *ptr++ = rescale[temp];
+ }
+ return 1;
+}
+
+
+METHODDEF(JDIMENSION)
+get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-word-format PPM files with any maxval */
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register U_CHAR * bufferptr;
+ register JSAMPLE *rescale = source->rescale;
+ JDIMENSION col;
+
+ if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+ ptr = source->pub.buffer[0];
+ bufferptr = source->iobuffer;
+ for (col = cinfo->image_width; col > 0; col--) {
+ register int temp;
+ temp = UCH(*bufferptr++);
+ temp |= UCH(*bufferptr++) << 8;
+ *ptr++ = rescale[temp];
+ temp = UCH(*bufferptr++);
+ temp |= UCH(*bufferptr++) << 8;
+ *ptr++ = rescale[temp];
+ temp = UCH(*bufferptr++);
+ temp |= UCH(*bufferptr++) << 8;
+ *ptr++ = rescale[temp];
+ }
+ return 1;
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF(void)
+start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ ppm_source_ptr source = (ppm_source_ptr) sinfo;
+ int c;
+ unsigned int w, h, maxval;
+ boolean need_iobuffer, use_raw_buffer, need_rescale;
+
+ if (getc(source->pub.input_file) != 'P')
+ ERREXIT(cinfo, JERR_PPM_NOT);
+
+ c = getc(source->pub.input_file); /* subformat discriminator character */
+
+ /* detect unsupported variants (ie, PBM) before trying to read header */
+ switch (c) {
+ case '2': /* it's a text-format PGM file */
+ case '3': /* it's a text-format PPM file */
+ case '5': /* it's a raw-format PGM file */
+ case '6': /* it's a raw-format PPM file */
+ break;
+ default:
+ ERREXIT(cinfo, JERR_PPM_NOT);
+ break;
+ }
+
+ /* fetch the remaining header info */
+ w = read_pbm_integer(cinfo, source->pub.input_file);
+ h = read_pbm_integer(cinfo, source->pub.input_file);
+ maxval = read_pbm_integer(cinfo, source->pub.input_file);
+
+ if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
+ ERREXIT(cinfo, JERR_PPM_NOT);
+
+ cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
+ cinfo->image_width = (JDIMENSION) w;
+ cinfo->image_height = (JDIMENSION) h;
+
+ /* initialize flags to most common settings */
+ need_iobuffer = TRUE; /* do we need an I/O buffer? */
+ use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
+ need_rescale = TRUE; /* do we need a rescale array? */
+
+ switch (c) {
+ case '2': /* it's a text-format PGM file */
+ cinfo->input_components = 1;
+ cinfo->in_color_space = JCS_GRAYSCALE;
+ TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
+ source->pub.get_pixel_rows = get_text_gray_row;
+ need_iobuffer = FALSE;
+ break;
+
+ case '3': /* it's a text-format PPM file */
+ cinfo->input_components = 3;
+ cinfo->in_color_space = JCS_RGB;
+ TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
+ source->pub.get_pixel_rows = get_text_rgb_row;
+ need_iobuffer = FALSE;
+ break;
+
+ case '5': /* it's a raw-format PGM file */
+ cinfo->input_components = 1;
+ cinfo->in_color_space = JCS_GRAYSCALE;
+ TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
+ if (maxval > 255) {
+ source->pub.get_pixel_rows = get_word_gray_row;
+ } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
+ source->pub.get_pixel_rows = get_raw_row;
+ use_raw_buffer = TRUE;
+ need_rescale = FALSE;
+ } else {
+ source->pub.get_pixel_rows = get_scaled_gray_row;
+ }
+ break;
+
+ case '6': /* it's a raw-format PPM file */
+ cinfo->input_components = 3;
+ cinfo->in_color_space = JCS_RGB;
+ TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
+ if (maxval > 255) {
+ source->pub.get_pixel_rows = get_word_rgb_row;
+ } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
+ source->pub.get_pixel_rows = get_raw_row;
+ use_raw_buffer = TRUE;
+ need_rescale = FALSE;
+ } else {
+ source->pub.get_pixel_rows = get_scaled_rgb_row;
+ }
+ break;
+ }
+
+ /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
+ if (need_iobuffer) {
+ source->buffer_width = (size_t) w * cinfo->input_components *
+ ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR)));
+ source->iobuffer = (U_CHAR *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ source->buffer_width);
+ }
+
+ /* Create compressor input buffer. */
+ if (use_raw_buffer) {
+ /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
+ /* Synthesize a JSAMPARRAY pointer structure */
+ /* Cast here implies near->far pointer conversion on PCs */
+ source->pixrow = (JSAMPROW) source->iobuffer;
+ source->pub.buffer = & source->pixrow;
+ source->pub.buffer_height = 1;
+ } else {
+ /* Need to translate anyway, so make a separate sample buffer. */
+ source->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
+ source->pub.buffer_height = 1;
+ }
+
+ /* Compute the rescaling array if required. */
+ if (need_rescale) {
+ INT32 val, half_maxval;
+
+ /* On 16-bit-int machines we have to be careful of maxval = 65535 */
+ source->rescale = (JSAMPLE *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
+ half_maxval = maxval / 2;
+ for (val = 0; val <= (INT32) maxval; val++) {
+ /* The multiplication here must be done in 32 bits to avoid overflow */
+ source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
+ }
+ }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ /* no work */
+}
+
+
+/*
+ * The module selection routine for PPM format input.
+ */
+
+GLOBAL(cjpeg_source_ptr)
+jinit_read_ppm (j_compress_ptr cinfo)
+{
+ ppm_source_ptr source;
+
+ /* Create module interface object */
+ source = (ppm_source_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(ppm_source_struct));
+ /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+ source->pub.start_input = start_input_ppm;
+ source->pub.finish_input = finish_input_ppm;
+
+ return (cjpeg_source_ptr) source;
+}
+
+#endif /* PPM_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/rdrle.c b/test/monniaux/jpeg-6b/rdrle.c
new file mode 100644
index 00000000..542bc374
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdrle.c
@@ -0,0 +1,387 @@
+/*
+ * rdrle.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Utah RLE format.
+ * The Utah Raster Toolkit library is required (version 3.1 or later).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume input from
+ * an ordinary stdio stream. They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed RLE format).
+ *
+ * Based on code contributed by Mike Lijewski,
+ * with updates from Robert Hutchinson.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef RLE_SUPPORTED
+
+/* rle.h is provided by the Utah Raster Toolkit. */
+
+#include <rle.h>
+
+/*
+ * We assume that JSAMPLE has the same representation as rle_pixel,
+ * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+ Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * We support the following types of RLE files:
+ *
+ * GRAYSCALE - 8 bits, no colormap
+ * MAPPEDGRAY - 8 bits, 1 channel colomap
+ * PSEUDOCOLOR - 8 bits, 3 channel colormap
+ * TRUECOLOR - 24 bits, 3 channel colormap
+ * DIRECTCOLOR - 24 bits, no colormap
+ *
+ * For now, we ignore any alpha channel in the image.
+ */
+
+typedef enum
+ { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
+
+
+/*
+ * Since RLE stores scanlines bottom-to-top, we have to invert the image
+ * to conform to JPEG's top-to-bottom order. To do this, we read the
+ * incoming image into a virtual array on the first get_pixel_rows call,
+ * then fetch the required row from the virtual array on subsequent calls.
+ */
+
+typedef struct _rle_source_struct * rle_source_ptr;
+
+typedef struct _rle_source_struct {
+ struct cjpeg_source_struct pub; /* public fields */
+
+ rle_kind visual; /* actual type of input file */
+ jvirt_sarray_ptr image; /* virtual array to hold the image */
+ JDIMENSION row; /* current row # in the virtual array */
+ rle_hdr header; /* Input file information */
+ rle_pixel** rle_row; /* holds a row returned by rle_getrow() */
+
+} rle_source_struct;
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF(void)
+start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ rle_source_ptr source = (rle_source_ptr) sinfo;
+ JDIMENSION width, height;
+#ifdef PROGRESS_REPORT
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+ /* Use RLE library routine to get the header info */
+ source->header = *rle_hdr_init(NULL);
+ source->header.rle_file = source->pub.input_file;
+ switch (rle_get_setup(&(source->header))) {
+ case RLE_SUCCESS:
+ /* A-OK */
+ break;
+ case RLE_NOT_RLE:
+ ERREXIT(cinfo, JERR_RLE_NOT);
+ break;
+ case RLE_NO_SPACE:
+ ERREXIT(cinfo, JERR_RLE_MEM);
+ break;
+ case RLE_EMPTY:
+ ERREXIT(cinfo, JERR_RLE_EMPTY);
+ break;
+ case RLE_EOF:
+ ERREXIT(cinfo, JERR_RLE_EOF);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_RLE_BADERROR);
+ break;
+ }
+
+ /* Figure out what we have, set private vars and return values accordingly */
+
+ width = source->header.xmax - source->header.xmin + 1;
+ height = source->header.ymax - source->header.ymin + 1;
+ source->header.xmin = 0; /* realign horizontally */
+ source->header.xmax = width-1;
+
+ cinfo->image_width = width;
+ cinfo->image_height = height;
+ cinfo->data_precision = 8; /* we can only handle 8 bit data */
+
+ if (source->header.ncolors == 1 && source->header.ncmap == 0) {
+ source->visual = GRAYSCALE;
+ TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
+ } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
+ source->visual = MAPPEDGRAY;
+ TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
+ 1 << source->header.cmaplen);
+ } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
+ source->visual = PSEUDOCOLOR;
+ TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
+ 1 << source->header.cmaplen);
+ } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
+ source->visual = TRUECOLOR;
+ TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
+ 1 << source->header.cmaplen);
+ } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
+ source->visual = DIRECTCOLOR;
+ TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
+ } else
+ ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
+
+ if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
+ cinfo->in_color_space = JCS_GRAYSCALE;
+ cinfo->input_components = 1;
+ } else {
+ cinfo->in_color_space = JCS_RGB;
+ cinfo->input_components = 3;
+ }
+
+ /*
+ * A place to hold each scanline while it's converted.
+ * (GRAYSCALE scanlines don't need converting)
+ */
+ if (source->visual != GRAYSCALE) {
+ source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
+ }
+
+ /* request a virtual array to hold the image */
+ source->image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) (width * source->header.ncolors),
+ (JDIMENSION) height, (JDIMENSION) 1);
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ /* count file input as separate pass */
+ progress->total_extra_passes++;
+ }
+#endif
+
+ source->pub.buffer_height = 1;
+}
+
+
+/*
+ * Read one row of pixels.
+ * Called only after load_image has read the image into the virtual array.
+ * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
+ */
+
+METHODDEF(JDIMENSION)
+get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ rle_source_ptr source = (rle_source_ptr) sinfo;
+
+ source->row--;
+ source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
+
+ return 1;
+}
+
+/*
+ * Read one row of pixels.
+ * Called only after load_image has read the image into the virtual array.
+ * Used for PSEUDOCOLOR images.
+ */
+
+METHODDEF(JDIMENSION)
+get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ rle_source_ptr source = (rle_source_ptr) sinfo;
+ JSAMPROW src_row, dest_row;
+ JDIMENSION col;
+ rle_map *colormap;
+ int val;
+
+ colormap = source->header.cmap;
+ dest_row = source->pub.buffer[0];
+ source->row--;
+ src_row = * (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
+
+ for (col = cinfo->image_width; col > 0; col--) {
+ val = GETJSAMPLE(*src_row++);
+ *dest_row++ = (JSAMPLE) (colormap[val ] >> 8);
+ *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8);
+ *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8);
+ }
+
+ return 1;
+}
+
+
+/*
+ * Load the image into a virtual array. We have to do this because RLE
+ * files start at the lower left while the JPEG standard has them starting
+ * in the upper left. This is called the first time we want to get a row
+ * of input. What we do is load the RLE data into the array and then call
+ * the appropriate routine to read one row from the array. Before returning,
+ * we set source->pub.get_pixel_rows so that subsequent calls go straight to
+ * the appropriate row-reading routine.
+ */
+
+METHODDEF(JDIMENSION)
+load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ rle_source_ptr source = (rle_source_ptr) sinfo;
+ JDIMENSION row, col;
+ JSAMPROW scanline, red_ptr, green_ptr, blue_ptr;
+ rle_pixel **rle_row;
+ rle_map *colormap;
+ char channel;
+#ifdef PROGRESS_REPORT
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+ colormap = source->header.cmap;
+ rle_row = source->rle_row;
+
+ /* Read the RLE data into our virtual array.
+ * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
+ * and (b) we are not on a machine where FAR pointers differ from regular.
+ */
+ RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_limit = cinfo->image_height;
+ progress->pub.pass_counter = 0;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+
+ switch (source->visual) {
+
+ case GRAYSCALE:
+ case PSEUDOCOLOR:
+ for (row = 0; row < cinfo->image_height; row++) {
+ rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
+ rle_getrow(&source->header, rle_row);
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_counter++;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+ }
+ break;
+
+ case MAPPEDGRAY:
+ case TRUECOLOR:
+ for (row = 0; row < cinfo->image_height; row++) {
+ scanline = * (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
+ rle_row = source->rle_row;
+ rle_getrow(&source->header, rle_row);
+
+ for (col = 0; col < cinfo->image_width; col++) {
+ for (channel = 0; channel < source->header.ncolors; channel++) {
+ *scanline++ = (JSAMPLE)
+ (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8);
+ }
+ }
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_counter++;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+ }
+ break;
+
+ case DIRECTCOLOR:
+ for (row = 0; row < cinfo->image_height; row++) {
+ scanline = * (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
+ rle_getrow(&source->header, rle_row);
+
+ red_ptr = rle_row[0];
+ green_ptr = rle_row[1];
+ blue_ptr = rle_row[2];
+
+ for (col = cinfo->image_width; col > 0; col--) {
+ *scanline++ = *red_ptr++;
+ *scanline++ = *green_ptr++;
+ *scanline++ = *blue_ptr++;
+ }
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_counter++;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+ }
+ }
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL)
+ progress->completed_extra_passes++;
+#endif
+
+ /* Set up to call proper row-extraction routine in future */
+ if (source->visual == PSEUDOCOLOR) {
+ source->pub.buffer = source->rle_row;
+ source->pub.get_pixel_rows = get_pseudocolor_row;
+ } else {
+ source->pub.get_pixel_rows = get_rle_row;
+ }
+ source->row = cinfo->image_height;
+
+ /* And fetch the topmost (bottommost) row */
+ return (*source->pub.get_pixel_rows) (cinfo, sinfo);
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ /* no work */
+}
+
+
+/*
+ * The module selection routine for RLE format input.
+ */
+
+GLOBAL(cjpeg_source_ptr)
+jinit_read_rle (j_compress_ptr cinfo)
+{
+ rle_source_ptr source;
+
+ /* Create module interface object */
+ source = (rle_source_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(rle_source_struct));
+ /* Fill in method ptrs */
+ source->pub.start_input = start_input_rle;
+ source->pub.finish_input = finish_input_rle;
+ source->pub.get_pixel_rows = load_image;
+
+ return (cjpeg_source_ptr) source;
+}
+
+#endif /* RLE_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/rdswitch.c b/test/monniaux/jpeg-6b/rdswitch.c
new file mode 100644
index 00000000..4f4bb4f5
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdswitch.c
@@ -0,0 +1,332 @@
+/*
+ * rdswitch.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to process some of cjpeg's more complicated
+ * command-line switches. Switches processed here are:
+ * -qtables file Read quantization tables from text file
+ * -scans file Read scan script from text file
+ * -qslots N[,N,...] Set component quantization table selectors
+ * -sample HxV[,HxV,...] Set component sampling factors
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+#include <ctype.h> /* to declare isdigit(), isspace() */
+
+
+LOCAL(int)
+text_getc (FILE * file)
+/* Read next char, skipping over any comments (# to end of line) */
+/* A comment/newline sequence is returned as a newline */
+{
+ register int ch;
+
+ ch = getc(file);
+ if (ch == '#') {
+ do {
+ ch = getc(file);
+ } while (ch != '\n' && ch != EOF);
+ }
+ return ch;
+}
+
+
+LOCAL(boolean)
+read_text_integer (FILE * file, long * result, int * termchar)
+/* Read an unsigned decimal integer from a file, store it in result */
+/* Reads one trailing character after the integer; returns it in termchar */
+{
+ register int ch;
+ register long val;
+
+ /* Skip any leading whitespace, detect EOF */
+ do {
+ ch = text_getc(file);
+ if (ch == EOF) {
+ *termchar = ch;
+ return FALSE;
+ }
+ } while (isspace(ch));
+
+ if (! isdigit(ch)) {
+ *termchar = ch;
+ return FALSE;
+ }
+
+ val = ch - '0';
+ while ((ch = text_getc(file)) != EOF) {
+ if (! isdigit(ch))
+ break;
+ val *= 10;
+ val += ch - '0';
+ }
+ *result = val;
+ *termchar = ch;
+ return TRUE;
+}
+
+
+GLOBAL(boolean)
+read_quant_tables (j_compress_ptr cinfo, char * filename,
+ int scale_factor, boolean force_baseline)
+/* Read a set of quantization tables from the specified file.
+ * The file is plain ASCII text: decimal numbers with whitespace between.
+ * Comments preceded by '#' may be included in the file.
+ * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
+ * The tables are implicitly numbered 0,1,etc.
+ * NOTE: does not affect the qslots mapping, which will default to selecting
+ * table 0 for luminance (or primary) components, 1 for chrominance components.
+ * You must use -qslots if you want a different component->table mapping.
+ */
+{
+ FILE * fp;
+ int tblno, i, termchar;
+ long val;
+ unsigned int table[DCTSIZE2];
+
+ if ((fp = fopen(filename, "r")) == NULL) {
+ fprintf(stderr, "Can't open table file %s\n", filename);
+ return FALSE;
+ }
+ tblno = 0;
+
+ while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
+ if (tblno >= NUM_QUANT_TBLS) {
+ fprintf(stderr, "Too many tables in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+ table[0] = (unsigned int) val;
+ for (i = 1; i < DCTSIZE2; i++) {
+ if (! read_text_integer(fp, &val, &termchar)) {
+ fprintf(stderr, "Invalid table data in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+ table[i] = (unsigned int) val;
+ }
+ jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
+ tblno++;
+ }
+
+ if (termchar != EOF) {
+ fprintf(stderr, "Non-numeric data in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+
+ fclose(fp);
+ return TRUE;
+}
+
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+
+LOCAL(boolean)
+read_scan_integer (FILE * file, long * result, int * termchar)
+/* Variant of read_text_integer that always looks for a non-space termchar;
+ * this simplifies parsing of punctuation in scan scripts.
+ */
+{
+ register int ch;
+
+ if (! read_text_integer(file, result, termchar))
+ return FALSE;
+ ch = *termchar;
+ while (ch != EOF && isspace(ch))
+ ch = text_getc(file);
+ if (isdigit(ch)) { /* oops, put it back */
+ if (ungetc(ch, file) == EOF)
+ return FALSE;
+ ch = ' ';
+ } else {
+ /* Any separators other than ';' and ':' are ignored;
+ * this allows user to insert commas, etc, if desired.
+ */
+ if (ch != EOF && ch != ';' && ch != ':')
+ ch = ' ';
+ }
+ *termchar = ch;
+ return TRUE;
+}
+
+
+GLOBAL(boolean)
+read_scan_script (j_compress_ptr cinfo, char * filename)
+/* Read a scan script from the specified text file.
+ * Each entry in the file defines one scan to be emitted.
+ * Entries are separated by semicolons ';'.
+ * An entry contains one to four component indexes,
+ * optionally followed by a colon ':' and four progressive-JPEG parameters.
+ * The component indexes denote which component(s) are to be transmitted
+ * in the current scan. The first component has index 0.
+ * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
+ * The file is free format text: any whitespace may appear between numbers
+ * and the ':' and ';' punctuation marks. Also, other punctuation (such
+ * as commas or dashes) can be placed between numbers if desired.
+ * Comments preceded by '#' may be included in the file.
+ * Note: we do very little validity checking here;
+ * jcmaster.c will validate the script parameters.
+ */
+{
+ FILE * fp;
+ int scanno, ncomps, termchar;
+ long val;
+ jpeg_scan_info * scanptr;
+#define MAX_SCANS 100 /* quite arbitrary limit */
+ jpeg_scan_info scans[MAX_SCANS];
+
+ if ((fp = fopen(filename, "r")) == NULL) {
+ fprintf(stderr, "Can't open scan definition file %s\n", filename);
+ return FALSE;
+ }
+ scanptr = scans;
+ scanno = 0;
+
+ while (read_scan_integer(fp, &val, &termchar)) {
+ if (scanno >= MAX_SCANS) {
+ fprintf(stderr, "Too many scans defined in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+ scanptr->component_index[0] = (int) val;
+ ncomps = 1;
+ while (termchar == ' ') {
+ if (ncomps >= MAX_COMPS_IN_SCAN) {
+ fprintf(stderr, "Too many components in one scan in file %s\n",
+ filename);
+ fclose(fp);
+ return FALSE;
+ }
+ if (! read_scan_integer(fp, &val, &termchar))
+ goto bogus;
+ scanptr->component_index[ncomps] = (int) val;
+ ncomps++;
+ }
+ scanptr->comps_in_scan = ncomps;
+ if (termchar == ':') {
+ if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
+ goto bogus;
+ scanptr->Ss = (int) val;
+ if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
+ goto bogus;
+ scanptr->Se = (int) val;
+ if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
+ goto bogus;
+ scanptr->Ah = (int) val;
+ if (! read_scan_integer(fp, &val, &termchar))
+ goto bogus;
+ scanptr->Al = (int) val;
+ } else {
+ /* set non-progressive parameters */
+ scanptr->Ss = 0;
+ scanptr->Se = DCTSIZE2-1;
+ scanptr->Ah = 0;
+ scanptr->Al = 0;
+ }
+ if (termchar != ';' && termchar != EOF) {
+bogus:
+ fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+ scanptr++, scanno++;
+ }
+
+ if (termchar != EOF) {
+ fprintf(stderr, "Non-numeric data in file %s\n", filename);
+ fclose(fp);
+ return FALSE;
+ }
+
+ if (scanno > 0) {
+ /* Stash completed scan list in cinfo structure.
+ * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
+ * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
+ */
+ scanptr = (jpeg_scan_info *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ scanno * SIZEOF(jpeg_scan_info));
+ MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
+ cinfo->scan_info = scanptr;
+ cinfo->num_scans = scanno;
+ }
+
+ fclose(fp);
+ return TRUE;
+}
+
+#endif /* C_MULTISCAN_FILES_SUPPORTED */
+
+
+GLOBAL(boolean)
+set_quant_slots (j_compress_ptr cinfo, char *arg)
+/* Process a quantization-table-selectors parameter string, of the form
+ * N[,N,...]
+ * If there are more components than parameters, the last value is replicated.
+ */
+{
+ int val = 0; /* default table # */
+ int ci;
+ char ch;
+
+ for (ci = 0; ci < MAX_COMPONENTS; ci++) {
+ if (*arg) {
+ ch = ','; /* if not set by sscanf, will be ',' */
+ if (sscanf(arg, "%d%c", &val, &ch) < 1)
+ return FALSE;
+ if (ch != ',') /* syntax check */
+ return FALSE;
+ if (val < 0 || val >= NUM_QUANT_TBLS) {
+ fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
+ NUM_QUANT_TBLS-1);
+ return FALSE;
+ }
+ cinfo->comp_info[ci].quant_tbl_no = val;
+ while (*arg && *arg++ != ',') /* advance to next segment of arg string */
+ ;
+ } else {
+ /* reached end of parameter, set remaining components to last table */
+ cinfo->comp_info[ci].quant_tbl_no = val;
+ }
+ }
+ return TRUE;
+}
+
+
+GLOBAL(boolean)
+set_sample_factors (j_compress_ptr cinfo, char *arg)
+/* Process a sample-factors parameter string, of the form
+ * HxV[,HxV,...]
+ * If there are more components than parameters, "1x1" is assumed for the rest.
+ */
+{
+ int ci, val1, val2;
+ char ch1, ch2;
+
+ for (ci = 0; ci < MAX_COMPONENTS; ci++) {
+ if (*arg) {
+ ch2 = ','; /* if not set by sscanf, will be ',' */
+ if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
+ return FALSE;
+ if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
+ return FALSE;
+ if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
+ fprintf(stderr, "JPEG sampling factors must be 1..4\n");
+ return FALSE;
+ }
+ cinfo->comp_info[ci].h_samp_factor = val1;
+ cinfo->comp_info[ci].v_samp_factor = val2;
+ while (*arg && *arg++ != ',') /* advance to next segment of arg string */
+ ;
+ } else {
+ /* reached end of parameter, set remaining components to 1x1 sampling */
+ cinfo->comp_info[ci].h_samp_factor = 1;
+ cinfo->comp_info[ci].v_samp_factor = 1;
+ }
+ }
+ return TRUE;
+}
diff --git a/test/monniaux/jpeg-6b/rdtarga.c b/test/monniaux/jpeg-6b/rdtarga.c
new file mode 100644
index 00000000..4c2cd267
--- /dev/null
+++ b/test/monniaux/jpeg-6b/rdtarga.c
@@ -0,0 +1,500 @@
+/*
+ * rdtarga.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Targa format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume input from
+ * an ordinary stdio stream. They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed Targa format).
+ *
+ * Based on code contributed by Lee Daniel Crocker.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef TARGA_SUPPORTED
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x) ((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x) ((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x) ((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/* Private version of data source object */
+
+typedef struct _tga_source_struct * tga_source_ptr;
+
+typedef struct _tga_source_struct {
+ struct cjpeg_source_struct pub; /* public fields */
+
+ j_compress_ptr cinfo; /* back link saves passing separate parm */
+
+ JSAMPARRAY colormap; /* Targa colormap (converted to my format) */
+
+ jvirt_sarray_ptr whole_image; /* Needed if funny input row order */
+ JDIMENSION current_row; /* Current logical row number to read */
+
+ /* Pointer to routine to extract next Targa pixel from input file */
+ JMETHOD(void, read_pixel, (tga_source_ptr sinfo));
+
+ /* Result of read_pixel is delivered here: */
+ U_CHAR tga_pixel[4];
+
+ int pixel_size; /* Bytes per Targa pixel (1 to 4) */
+
+ /* State info for reading RLE-coded pixels; both counts must be init to 0 */
+ int block_count; /* # of pixels remaining in RLE block */
+ int dup_pixel_count; /* # of times to duplicate previous pixel */
+
+ /* This saves the correct pixel-row-expansion method for preload_image */
+ JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+} tga_source_struct;
+
+
+/* For expanding 5-bit pixel values to 8-bit with best rounding */
+
+static const UINT8 c5to8bits[32] = {
+ 0, 8, 16, 25, 33, 41, 49, 58,
+ 66, 74, 82, 90, 99, 107, 115, 123,
+ 132, 140, 148, 156, 165, 173, 181, 189,
+ 197, 206, 214, 222, 230, 239, 247, 255
+};
+
+
+
+LOCAL(int)
+read_byte (tga_source_ptr sinfo)
+/* Read next byte from Targa file */
+{
+ register FILE *infile = sinfo->pub.input_file;
+ register int c;
+
+ if ((c = getc(infile)) == EOF)
+ ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+ return c;
+}
+
+
+LOCAL(void)
+read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize)
+/* Read the colormap from a Targa file */
+{
+ int i;
+
+ /* Presently only handles 24-bit BGR format */
+ if (mapentrysize != 24)
+ ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP);
+
+ for (i = 0; i < cmaplen; i++) {
+ sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+ sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+ }
+}
+
+
+/*
+ * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
+ */
+
+METHODDEF(void)
+read_non_rle_pixel (tga_source_ptr sinfo)
+/* Read one Targa pixel from the input file; no RLE expansion */
+{
+ register FILE *infile = sinfo->pub.input_file;
+ register int i;
+
+ for (i = 0; i < sinfo->pixel_size; i++) {
+ sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
+ }
+}
+
+
+METHODDEF(void)
+read_rle_pixel (tga_source_ptr sinfo)
+/* Read one Targa pixel from the input file, expanding RLE data as needed */
+{
+ register FILE *infile = sinfo->pub.input_file;
+ register int i;
+
+ /* Duplicate previously read pixel? */
+ if (sinfo->dup_pixel_count > 0) {
+ sinfo->dup_pixel_count--;
+ return;
+ }
+
+ /* Time to read RLE block header? */
+ if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */
+ i = read_byte(sinfo);
+ if (i & 0x80) { /* Start of duplicate-pixel block? */
+ sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */
+ sinfo->block_count = 0; /* then read new block header */
+ } else {
+ sinfo->block_count = i & 0x7F; /* number of pixels after this one */
+ }
+ }
+
+ /* Read next pixel */
+ for (i = 0; i < sinfo->pixel_size; i++) {
+ sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
+ }
+}
+
+
+/*
+ * Read one row of pixels.
+ *
+ * We provide several different versions depending on input file format.
+ */
+
+
+METHODDEF(JDIMENSION)
+get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit grayscale pixels */
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+ *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
+ }
+ return 1;
+}
+
+METHODDEF(JDIMENSION)
+get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit colormap indexes */
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ register int t;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+ register JSAMPARRAY colormap = source->colormap;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+ t = UCH(source->tga_pixel[0]);
+ *ptr++ = colormap[0][t];
+ *ptr++ = colormap[1][t];
+ *ptr++ = colormap[2][t];
+ }
+ return 1;
+}
+
+METHODDEF(JDIMENSION)
+get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 16-bit pixels */
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ register int t;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+ t = UCH(source->tga_pixel[0]);
+ t += UCH(source->tga_pixel[1]) << 8;
+ /* We expand 5 bit data to 8 bit sample width.
+ * The format of the 16-bit (LSB first) input word is
+ * xRRRRRGGGGGBBBBB
+ */
+ ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F];
+ t >>= 5;
+ ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F];
+ t >>= 5;
+ ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F];
+ ptr += 3;
+ }
+ return 1;
+}
+
+METHODDEF(JDIMENSION)
+get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 24-bit pixels */
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+
+ ptr = source->pub.buffer[0];
+ for (col = cinfo->image_width; col > 0; col--) {
+ (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+ *ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); /* change BGR to RGB order */
+ *ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]);
+ *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
+ }
+ return 1;
+}
+
+/*
+ * Targa also defines a 32-bit pixel format with order B,G,R,A.
+ * We presently ignore the attribute byte, so the code for reading
+ * these pixels is identical to the 24-bit routine above.
+ * This works because the actual pixel length is only known to read_pixel.
+ */
+
+#define get_32bit_row get_24bit_row
+
+
+/*
+ * This method is for re-reading the input data in standard top-down
+ * row order. The entire image has already been read into whole_image
+ * with proper conversion of pixel format, but it's in a funny row order.
+ */
+
+METHODDEF(JDIMENSION)
+get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ JDIMENSION source_row;
+
+ /* Compute row of source that maps to current_row of normal order */
+ /* For now, assume image is bottom-up and not interlaced. */
+ /* NEEDS WORK to support interlaced images! */
+ source_row = cinfo->image_height - source->current_row - 1;
+
+ /* Fetch that row from virtual array */
+ source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->whole_image,
+ source_row, (JDIMENSION) 1, FALSE);
+
+ source->current_row++;
+ return 1;
+}
+
+
+/*
+ * This method loads the image into whole_image during the first call on
+ * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
+ * get_memory_row on subsequent calls.
+ */
+
+METHODDEF(JDIMENSION)
+preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ JDIMENSION row;
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+ /* Read the data into a virtual array in input-file row order. */
+ for (row = 0; row < cinfo->image_height; row++) {
+ if (progress != NULL) {
+ progress->pub.pass_counter = (long) row;
+ progress->pub.pass_limit = (long) cinfo->image_height;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+ source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, source->whole_image, row, (JDIMENSION) 1, TRUE);
+ (*source->get_pixel_rows) (cinfo, sinfo);
+ }
+ if (progress != NULL)
+ progress->completed_extra_passes++;
+
+ /* Set up to read from the virtual array in unscrambled order */
+ source->pub.get_pixel_rows = get_memory_row;
+ source->current_row = 0;
+ /* And read the first row */
+ return get_memory_row(cinfo, sinfo);
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF(void)
+start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ tga_source_ptr source = (tga_source_ptr) sinfo;
+ U_CHAR targaheader[18];
+ int idlen, cmaptype, subtype, flags, interlace_type, components;
+ unsigned int width, height, maplen;
+ boolean is_bottom_up;
+
+#define GET_2B(offset) ((unsigned int) UCH(targaheader[offset]) + \
+ (((unsigned int) UCH(targaheader[offset+1])) << 8))
+
+ if (! ReadOK(source->pub.input_file, targaheader, 18))
+ ERREXIT(cinfo, JERR_INPUT_EOF);
+
+ /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
+ if (targaheader[16] == 15)
+ targaheader[16] = 16;
+
+ idlen = UCH(targaheader[0]);
+ cmaptype = UCH(targaheader[1]);
+ subtype = UCH(targaheader[2]);
+ maplen = GET_2B(5);
+ width = GET_2B(12);
+ height = GET_2B(14);
+ source->pixel_size = UCH(targaheader[16]) >> 3;
+ flags = UCH(targaheader[17]); /* Image Descriptor byte */
+
+ is_bottom_up = ((flags & 0x20) == 0); /* bit 5 set => top-down */
+ interlace_type = flags >> 6; /* bits 6/7 are interlace code */
+
+ if (cmaptype > 1 || /* cmaptype must be 0 or 1 */
+ source->pixel_size < 1 || source->pixel_size > 4 ||
+ (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
+ interlace_type != 0) /* currently don't allow interlaced image */
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+
+ if (subtype > 8) {
+ /* It's an RLE-coded file */
+ source->read_pixel = read_rle_pixel;
+ source->block_count = source->dup_pixel_count = 0;
+ subtype -= 8;
+ } else {
+ /* Non-RLE file */
+ source->read_pixel = read_non_rle_pixel;
+ }
+
+ /* Now should have subtype 1, 2, or 3 */
+ components = 3; /* until proven different */
+ cinfo->in_color_space = JCS_RGB;
+
+ switch (subtype) {
+ case 1: /* Colormapped image */
+ if (source->pixel_size == 1 && cmaptype == 1)
+ source->get_pixel_rows = get_8bit_row;
+ else
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+ TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height);
+ break;
+ case 2: /* RGB image */
+ switch (source->pixel_size) {
+ case 2:
+ source->get_pixel_rows = get_16bit_row;
+ break;
+ case 3:
+ source->get_pixel_rows = get_24bit_row;
+ break;
+ case 4:
+ source->get_pixel_rows = get_32bit_row;
+ break;
+ default:
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+ break;
+ }
+ TRACEMS2(cinfo, 1, JTRC_TGA, width, height);
+ break;
+ case 3: /* Grayscale image */
+ components = 1;
+ cinfo->in_color_space = JCS_GRAYSCALE;
+ if (source->pixel_size == 1)
+ source->get_pixel_rows = get_8bit_gray_row;
+ else
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+ TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+ break;
+ }
+
+ if (is_bottom_up) {
+ /* Create a virtual array to buffer the upside-down image. */
+ source->whole_image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1);
+ if (cinfo->progress != NULL) {
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+ progress->total_extra_passes++; /* count file input as separate pass */
+ }
+ /* source->pub.buffer will point to the virtual array. */
+ source->pub.buffer_height = 1; /* in case anyone looks at it */
+ source->pub.get_pixel_rows = preload_image;
+ } else {
+ /* Don't need a virtual array, but do need a one-row input buffer. */
+ source->whole_image = NULL;
+ source->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) width * components, (JDIMENSION) 1);
+ source->pub.buffer_height = 1;
+ source->pub.get_pixel_rows = source->get_pixel_rows;
+ }
+
+ while (idlen--) /* Throw away ID field */
+ (void) read_byte(source);
+
+ if (maplen > 0) {
+ if (maplen > 256 || GET_2B(3) != 0)
+ ERREXIT(cinfo, JERR_TGA_BADCMAP);
+ /* Allocate space to store the colormap */
+ source->colormap = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3);
+ /* and read it from the file */
+ read_colormap(source, (int) maplen, UCH(targaheader[7]));
+ } else {
+ if (cmaptype) /* but you promised a cmap! */
+ ERREXIT(cinfo, JERR_TGA_BADPARMS);
+ source->colormap = NULL;
+ }
+
+ cinfo->input_components = components;
+ cinfo->data_precision = 8;
+ cinfo->image_width = width;
+ cinfo->image_height = height;
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+ /* no work */
+}
+
+
+/*
+ * The module selection routine for Targa format input.
+ */
+
+GLOBAL(cjpeg_source_ptr)
+jinit_read_targa (j_compress_ptr cinfo)
+{
+ tga_source_ptr source;
+
+ /* Create module interface object */
+ source = (tga_source_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(tga_source_struct));
+ source->cinfo = cinfo; /* make back link for subroutines */
+ /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+ source->pub.start_input = start_input_tga;
+ source->pub.finish_input = finish_input_tga;
+
+ return (cjpeg_source_ptr) source;
+}
+
+#endif /* TARGA_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/readme.dos b/test/monniaux/jpeg-6b/readme.dos
new file mode 100644
index 00000000..9f73a629
--- /dev/null
+++ b/test/monniaux/jpeg-6b/readme.dos
@@ -0,0 +1,15 @@
+This archive contains a DOS-friendly version of the Independent JPEG Group's
+source code. It differs from the normal distribution in that:
+
+1. The archive format is zip rather than tar+gzip. You should be able to
+unpack it with PKUNZIP (2.04g or later) or Info-Zip's unzip.
+
+2. Newlines have been converted from Unix (LF) to DOS (CR/LF) style in all
+text files, but not in the binary files (test*.*).
+
+3. Object files have been included for jmemdosa.asm. See jdosaobj.doc.
+
+Please see the main README file for the primary documentation.
+
+If you'd rather have a non-DOSified archive, see the ARCHIVE LOCATIONS section
+of README.
diff --git a/test/monniaux/jpeg-6b/structure.doc b/test/monniaux/jpeg-6b/structure.doc
new file mode 100644
index 00000000..51c9def7
--- /dev/null
+++ b/test/monniaux/jpeg-6b/structure.doc
@@ -0,0 +1,948 @@
+IJG JPEG LIBRARY: SYSTEM ARCHITECTURE
+
+Copyright (C) 1991-1995, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file provides an overview of the architecture of the IJG JPEG software;
+that is, the functions of the various modules in the system and the interfaces
+between modules. For more precise details about any data structure or calling
+convention, see the include files and comments in the source code.
+
+We assume that the reader is already somewhat familiar with the JPEG standard.
+The README file includes references for learning about JPEG. The file
+libjpeg.doc describes the library from the viewpoint of an application
+programmer using the library; it's best to read that file before this one.
+Also, the file coderules.doc describes the coding style conventions we use.
+
+In this document, JPEG-specific terminology follows the JPEG standard:
+ A "component" means a color channel, e.g., Red or Luminance.
+ A "sample" is a single component value (i.e., one number in the image data).
+ A "coefficient" is a frequency coefficient (a DCT transform output number).
+ A "block" is an 8x8 group of samples or coefficients.
+ An "MCU" (minimum coded unit) is an interleaved set of blocks of size
+ determined by the sampling factors, or a single block in a
+ noninterleaved scan.
+We do not use the terms "pixel" and "sample" interchangeably. When we say
+pixel, we mean an element of the full-size image, while a sample is an element
+of the downsampled image. Thus the number of samples may vary across
+components while the number of pixels does not. (This terminology is not used
+rigorously throughout the code, but it is used in places where confusion would
+otherwise result.)
+
+
+*** System features ***
+
+The IJG distribution contains two parts:
+ * A subroutine library for JPEG compression and decompression.
+ * cjpeg/djpeg, two sample applications that use the library to transform
+ JFIF JPEG files to and from several other image formats.
+cjpeg/djpeg are of no great intellectual complexity: they merely add a simple
+command-line user interface and I/O routines for several uncompressed image
+formats. This document concentrates on the library itself.
+
+We desire the library to be capable of supporting all JPEG baseline, extended
+sequential, and progressive DCT processes. Hierarchical processes are not
+supported.
+
+The library does not support the lossless (spatial) JPEG process. Lossless
+JPEG shares little or no code with lossy JPEG, and would normally be used
+without the extensive pre- and post-processing provided by this library.
+We feel that lossless JPEG is better handled by a separate library.
+
+Within these limits, any set of compression parameters allowed by the JPEG
+spec should be readable for decompression. (We can be more restrictive about
+what formats we can generate.) Although the system design allows for all
+parameter values, some uncommon settings are not yet implemented and may
+never be; nonintegral sampling ratios are the prime example. Furthermore,
+we treat 8-bit vs. 12-bit data precision as a compile-time switch, not a
+run-time option, because most machines can store 8-bit pixels much more
+compactly than 12-bit.
+
+For legal reasons, JPEG arithmetic coding is not currently supported, but
+extending the library to include it would be straightforward.
+
+By itself, the library handles only interchange JPEG datastreams --- in
+particular the widely used JFIF file format. The library can be used by
+surrounding code to process interchange or abbreviated JPEG datastreams that
+are embedded in more complex file formats. (For example, libtiff uses this
+library to implement JPEG compression within the TIFF file format.)
+
+The library includes a substantial amount of code that is not covered by the
+JPEG standard but is necessary for typical applications of JPEG. These
+functions preprocess the image before JPEG compression or postprocess it after
+decompression. They include colorspace conversion, downsampling/upsampling,
+and color quantization. This code can be omitted if not needed.
+
+A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
+and even more so in decompression postprocessing. The decompression library
+provides multiple implementations that cover most of the useful tradeoffs,
+ranging from very-high-quality down to fast-preview operation. On the
+compression side we have generally not provided low-quality choices, since
+compression is normally less time-critical. It should be understood that the
+low-quality modes may not meet the JPEG standard's accuracy requirements;
+nonetheless, they are useful for viewers.
+
+
+*** Portability issues ***
+
+Portability is an essential requirement for the library. The key portability
+issues that show up at the level of system architecture are:
+
+1. Memory usage. We want the code to be able to run on PC-class machines
+with limited memory. Images should therefore be processed sequentially (in
+strips), to avoid holding the whole image in memory at once. Where a
+full-image buffer is necessary, we should be able to use either virtual memory
+or temporary files.
+
+2. Near/far pointer distinction. To run efficiently on 80x86 machines, the
+code should distinguish "small" objects (kept in near data space) from
+"large" ones (kept in far data space). This is an annoying restriction, but
+fortunately it does not impact code quality for less brain-damaged machines,
+and the source code clutter turns out to be minimal with sufficient use of
+pointer typedefs.
+
+3. Data precision. We assume that "char" is at least 8 bits, "short" and
+"int" at least 16, "long" at least 32. The code will work fine with larger
+data sizes, although memory may be used inefficiently in some cases. However,
+the JPEG compressed datastream must ultimately appear on external storage as a
+sequence of 8-bit bytes if it is to conform to the standard. This may pose a
+problem on machines where char is wider than 8 bits. The library represents
+compressed data as an array of values of typedef JOCTET. If no data type
+exactly 8 bits wide is available, custom data source and data destination
+modules must be written to unpack and pack the chosen JOCTET datatype into
+8-bit external representation.
+
+
+*** System overview ***
+
+The compressor and decompressor are each divided into two main sections:
+the JPEG compressor or decompressor proper, and the preprocessing or
+postprocessing functions. The interface between these two sections is the
+image data that the official JPEG spec regards as its input or output: this
+data is in the colorspace to be used for compression, and it is downsampled
+to the sampling factors to be used. The preprocessing and postprocessing
+steps are responsible for converting a normal image representation to or from
+this form. (Those few applications that want to deal with YCbCr downsampled
+data can skip the preprocessing or postprocessing step.)
+
+Looking more closely, the compressor library contains the following main
+elements:
+
+ Preprocessing:
+ * Color space conversion (e.g., RGB to YCbCr).
+ * Edge expansion and downsampling. Optionally, this step can do simple
+ smoothing --- this is often helpful for low-quality source data.
+ JPEG proper:
+ * MCU assembly, DCT, quantization.
+ * Entropy coding (sequential or progressive, Huffman or arithmetic).
+
+In addition to these modules we need overall control, marker generation,
+and support code (memory management & error handling). There is also a
+module responsible for physically writing the output data --- typically
+this is just an interface to fwrite(), but some applications may need to
+do something else with the data.
+
+The decompressor library contains the following main elements:
+
+ JPEG proper:
+ * Entropy decoding (sequential or progressive, Huffman or arithmetic).
+ * Dequantization, inverse DCT, MCU disassembly.
+ Postprocessing:
+ * Upsampling. Optionally, this step may be able to do more general
+ rescaling of the image.
+ * Color space conversion (e.g., YCbCr to RGB). This step may also
+ provide gamma adjustment [ currently it does not ].
+ * Optional color quantization (e.g., reduction to 256 colors).
+ * Optional color precision reduction (e.g., 24-bit to 15-bit color).
+ [This feature is not currently implemented.]
+
+We also need overall control, marker parsing, and a data source module.
+The support code (memory management & error handling) can be shared with
+the compression half of the library.
+
+There may be several implementations of each of these elements, particularly
+in the decompressor, where a wide range of speed/quality tradeoffs is very
+useful. It must be understood that some of the best speedups involve
+merging adjacent steps in the pipeline. For example, upsampling, color space
+conversion, and color quantization might all be done at once when using a
+low-quality ordered-dither technique. The system architecture is designed to
+allow such merging where appropriate.
+
+
+Note: it is convenient to regard edge expansion (padding to block boundaries)
+as a preprocessing/postprocessing function, even though the JPEG spec includes
+it in compression/decompression. We do this because downsampling/upsampling
+can be simplified a little if they work on padded data: it's not necessary to
+have special cases at the right and bottom edges. Therefore the interface
+buffer is always an integral number of blocks wide and high, and we expect
+compression preprocessing to pad the source data properly. Padding will occur
+only to the next block (8-sample) boundary. In an interleaved-scan situation,
+additional dummy blocks may be used to fill out MCUs, but the MCU assembly and
+disassembly logic will create or discard these blocks internally. (This is
+advantageous for speed reasons, since we avoid DCTing the dummy blocks.
+It also permits a small reduction in file size, because the compressor can
+choose dummy block contents so as to minimize their size in compressed form.
+Finally, it makes the interface buffer specification independent of whether
+the file is actually interleaved or not.) Applications that wish to deal
+directly with the downsampled data must provide similar buffering and padding
+for odd-sized images.
+
+
+*** Poor man's object-oriented programming ***
+
+It should be clear by now that we have a lot of quasi-independent processing
+steps, many of which have several possible behaviors. To avoid cluttering the
+code with lots of switch statements, we use a simple form of object-style
+programming to separate out the different possibilities.
+
+For example, two different color quantization algorithms could be implemented
+as two separate modules that present the same external interface; at runtime,
+the calling code will access the proper module indirectly through an "object".
+
+We can get the limited features we need while staying within portable C.
+The basic tool is a function pointer. An "object" is just a struct
+containing one or more function pointer fields, each of which corresponds to
+a method name in real object-oriented languages. During initialization we
+fill in the function pointers with references to whichever module we have
+determined we need to use in this run. Then invocation of the module is done
+by indirecting through a function pointer; on most machines this is no more
+expensive than a switch statement, which would be the only other way of
+making the required run-time choice. The really significant benefit, of
+course, is keeping the source code clean and well structured.
+
+We can also arrange to have private storage that varies between different
+implementations of the same kind of object. We do this by making all the
+module-specific object structs be separately allocated entities, which will
+be accessed via pointers in the master compression or decompression struct.
+The "public" fields or methods for a given kind of object are specified by
+a commonly known struct. But a module's initialization code can allocate
+a larger struct that contains the common struct as its first member, plus
+additional private fields. With appropriate pointer casting, the module's
+internal functions can access these private fields. (For a simple example,
+see jdatadst.c, which implements the external interface specified by struct
+jpeg_destination_mgr, but adds extra fields.)
+
+(Of course this would all be a lot easier if we were using C++, but we are
+not yet prepared to assume that everyone has a C++ compiler.)
+
+An important benefit of this scheme is that it is easy to provide multiple
+versions of any method, each tuned to a particular case. While a lot of
+precalculation might be done to select an optimal implementation of a method,
+the cost per invocation is constant. For example, the upsampling step might
+have a "generic" method, plus one or more "hardwired" methods for the most
+popular sampling factors; the hardwired methods would be faster because they'd
+use straight-line code instead of for-loops. The cost to determine which
+method to use is paid only once, at startup, and the selection criteria are
+hidden from the callers of the method.
+
+This plan differs a little bit from usual object-oriented structures, in that
+only one instance of each object class will exist during execution. The
+reason for having the class structure is that on different runs we may create
+different instances (choose to execute different modules). You can think of
+the term "method" as denoting the common interface presented by a particular
+set of interchangeable functions, and "object" as denoting a group of related
+methods, or the total shared interface behavior of a group of modules.
+
+
+*** Overall control structure ***
+
+We previously mentioned the need for overall control logic in the compression
+and decompression libraries. In IJG implementations prior to v5, overall
+control was mostly provided by "pipeline control" modules, which proved to be
+large, unwieldy, and hard to understand. To improve the situation, the
+control logic has been subdivided into multiple modules. The control modules
+consist of:
+
+1. Master control for module selection and initialization. This has two
+responsibilities:
+
+ 1A. Startup initialization at the beginning of image processing.
+ The individual processing modules to be used in this run are selected
+ and given initialization calls.
+
+ 1B. Per-pass control. This determines how many passes will be performed
+ and calls each active processing module to configure itself
+ appropriately at the beginning of each pass. End-of-pass processing,
+ where necessary, is also invoked from the master control module.
+
+ Method selection is partially distributed, in that a particular processing
+ module may contain several possible implementations of a particular method,
+ which it will select among when given its initialization call. The master
+ control code need only be concerned with decisions that affect more than
+ one module.
+
+2. Data buffering control. A separate control module exists for each
+ inter-processing-step data buffer. This module is responsible for
+ invoking the processing steps that write or read that data buffer.
+
+Each buffer controller sees the world as follows:
+
+input data => processing step A => buffer => processing step B => output data
+ | | |
+ ------------------ controller ------------------
+
+The controller knows the dataflow requirements of steps A and B: how much data
+they want to accept in one chunk and how much they output in one chunk. Its
+function is to manage its buffer and call A and B at the proper times.
+
+A data buffer control module may itself be viewed as a processing step by a
+higher-level control module; thus the control modules form a binary tree with
+elementary processing steps at the leaves of the tree.
+
+The control modules are objects. A considerable amount of flexibility can
+be had by replacing implementations of a control module. For example:
+* Merging of adjacent steps in the pipeline is done by replacing a control
+ module and its pair of processing-step modules with a single processing-
+ step module. (Hence the possible merges are determined by the tree of
+ control modules.)
+* In some processing modes, a given interstep buffer need only be a "strip"
+ buffer large enough to accommodate the desired data chunk sizes. In other
+ modes, a full-image buffer is needed and several passes are required.
+ The control module determines which kind of buffer is used and manipulates
+ virtual array buffers as needed. One or both processing steps may be
+ unaware of the multi-pass behavior.
+
+In theory, we might be able to make all of the data buffer controllers
+interchangeable and provide just one set of implementations for all. In
+practice, each one contains considerable special-case processing for its
+particular job. The buffer controller concept should be regarded as an
+overall system structuring principle, not as a complete description of the
+task performed by any one controller.
+
+
+*** Compression object structure ***
+
+Here is a sketch of the logical structure of the JPEG compression library:
+
+ |-- Colorspace conversion
+ |-- Preprocessing controller --|
+ | |-- Downsampling
+Main controller --|
+ | |-- Forward DCT, quantize
+ |-- Coefficient controller --|
+ |-- Entropy encoding
+
+This sketch also describes the flow of control (subroutine calls) during
+typical image data processing. Each of the components shown in the diagram is
+an "object" which may have several different implementations available. One
+or more source code files contain the actual implementation(s) of each object.
+
+The objects shown above are:
+
+* Main controller: buffer controller for the subsampled-data buffer, which
+ holds the preprocessed input data. This controller invokes preprocessing to
+ fill the subsampled-data buffer, and JPEG compression to empty it. There is
+ usually no need for a full-image buffer here; a strip buffer is adequate.
+
+* Preprocessing controller: buffer controller for the downsampling input data
+ buffer, which lies between colorspace conversion and downsampling. Note
+ that a unified conversion/downsampling module would probably replace this
+ controller entirely.
+
+* Colorspace conversion: converts application image data into the desired
+ JPEG color space; also changes the data from pixel-interleaved layout to
+ separate component planes. Processes one pixel row at a time.
+
+* Downsampling: performs reduction of chroma components as required.
+ Optionally may perform pixel-level smoothing as well. Processes a "row
+ group" at a time, where a row group is defined as Vmax pixel rows of each
+ component before downsampling, and Vk sample rows afterwards (remember Vk
+ differs across components). Some downsampling or smoothing algorithms may
+ require context rows above and below the current row group; the
+ preprocessing controller is responsible for supplying these rows via proper
+ buffering. The downsampler is responsible for edge expansion at the right
+ edge (i.e., extending each sample row to a multiple of 8 samples); but the
+ preprocessing controller is responsible for vertical edge expansion (i.e.,
+ duplicating the bottom sample row as needed to make a multiple of 8 rows).
+
+* Coefficient controller: buffer controller for the DCT-coefficient data.
+ This controller handles MCU assembly, including insertion of dummy DCT
+ blocks when needed at the right or bottom edge. When performing
+ Huffman-code optimization or emitting a multiscan JPEG file, this
+ controller is responsible for buffering the full image. The equivalent of
+ one fully interleaved MCU row of subsampled data is processed per call,
+ even when the JPEG file is noninterleaved.
+
+* Forward DCT and quantization: Perform DCT, quantize, and emit coefficients.
+ Works on one or more DCT blocks at a time. (Note: the coefficients are now
+ emitted in normal array order, which the entropy encoder is expected to
+ convert to zigzag order as necessary. Prior versions of the IJG code did
+ the conversion to zigzag order within the quantization step.)
+
+* Entropy encoding: Perform Huffman or arithmetic entropy coding and emit the
+ coded data to the data destination module. Works on one MCU per call.
+ For progressive JPEG, the same DCT blocks are fed to the entropy coder
+ during each pass, and the coder must emit the appropriate subset of
+ coefficients.
+
+In addition to the above objects, the compression library includes these
+objects:
+
+* Master control: determines the number of passes required, controls overall
+ and per-pass initialization of the other modules.
+
+* Marker writing: generates JPEG markers (except for RSTn, which is emitted
+ by the entropy encoder when needed).
+
+* Data destination manager: writes the output JPEG datastream to its final
+ destination (e.g., a file). The destination manager supplied with the
+ library knows how to write to a stdio stream; for other behaviors, the
+ surrounding application may provide its own destination manager.
+
+* Memory manager: allocates and releases memory, controls virtual arrays
+ (with backing store management, where required).
+
+* Error handler: performs formatting and output of error and trace messages;
+ determines handling of nonfatal errors. The surrounding application may
+ override some or all of this object's methods to change error handling.
+
+* Progress monitor: supports output of "percent-done" progress reports.
+ This object represents an optional callback to the surrounding application:
+ if wanted, it must be supplied by the application.
+
+The error handler, destination manager, and progress monitor objects are
+defined as separate objects in order to simplify application-specific
+customization of the JPEG library. A surrounding application may override
+individual methods or supply its own all-new implementation of one of these
+objects. The object interfaces for these objects are therefore treated as
+part of the application interface of the library, whereas the other objects
+are internal to the library.
+
+The error handler and memory manager are shared by JPEG compression and
+decompression; the progress monitor, if used, may be shared as well.
+
+
+*** Decompression object structure ***
+
+Here is a sketch of the logical structure of the JPEG decompression library:
+
+ |-- Entropy decoding
+ |-- Coefficient controller --|
+ | |-- Dequantize, Inverse DCT
+Main controller --|
+ | |-- Upsampling
+ |-- Postprocessing controller --| |-- Colorspace conversion
+ |-- Color quantization
+ |-- Color precision reduction
+
+As before, this diagram also represents typical control flow. The objects
+shown are:
+
+* Main controller: buffer controller for the subsampled-data buffer, which
+ holds the output of JPEG decompression proper. This controller's primary
+ task is to feed the postprocessing procedure. Some upsampling algorithms
+ may require context rows above and below the current row group; when this
+ is true, the main controller is responsible for managing its buffer so as
+ to make context rows available. In the current design, the main buffer is
+ always a strip buffer; a full-image buffer is never required.
+
+* Coefficient controller: buffer controller for the DCT-coefficient data.
+ This controller handles MCU disassembly, including deletion of any dummy
+ DCT blocks at the right or bottom edge. When reading a multiscan JPEG
+ file, this controller is responsible for buffering the full image.
+ (Buffering DCT coefficients, rather than samples, is necessary to support
+ progressive JPEG.) The equivalent of one fully interleaved MCU row of
+ subsampled data is processed per call, even when the source JPEG file is
+ noninterleaved.
+
+* Entropy decoding: Read coded data from the data source module and perform
+ Huffman or arithmetic entropy decoding. Works on one MCU per call.
+ For progressive JPEG decoding, the coefficient controller supplies the prior
+ coefficients of each MCU (initially all zeroes), which the entropy decoder
+ modifies in each scan.
+
+* Dequantization and inverse DCT: like it says. Note that the coefficients
+ buffered by the coefficient controller have NOT been dequantized; we
+ merge dequantization and inverse DCT into a single step for speed reasons.
+ When scaled-down output is asked for, simplified DCT algorithms may be used
+ that emit only 1x1, 2x2, or 4x4 samples per DCT block, not the full 8x8.
+ Works on one DCT block at a time.
+
+* Postprocessing controller: buffer controller for the color quantization
+ input buffer, when quantization is in use. (Without quantization, this
+ controller just calls the upsampler.) For two-pass quantization, this
+ controller is responsible for buffering the full-image data.
+
+* Upsampling: restores chroma components to full size. (May support more
+ general output rescaling, too. Note that if undersized DCT outputs have
+ been emitted by the DCT module, this module must adjust so that properly
+ sized outputs are created.) Works on one row group at a time. This module
+ also calls the color conversion module, so its top level is effectively a
+ buffer controller for the upsampling->color conversion buffer. However, in
+ all but the highest-quality operating modes, upsampling and color
+ conversion are likely to be merged into a single step.
+
+* Colorspace conversion: convert from JPEG color space to output color space,
+ and change data layout from separate component planes to pixel-interleaved.
+ Works on one pixel row at a time.
+
+* Color quantization: reduce the data to colormapped form, using either an
+ externally specified colormap or an internally generated one. This module
+ is not used for full-color output. Works on one pixel row at a time; may
+ require two passes to generate a color map. Note that the output will
+ always be a single component representing colormap indexes. In the current
+ design, the output values are JSAMPLEs, so an 8-bit compilation cannot
+ quantize to more than 256 colors. This is unlikely to be a problem in
+ practice.
+
+* Color reduction: this module handles color precision reduction, e.g.,
+ generating 15-bit color (5 bits/primary) from JPEG's 24-bit output.
+ Not quite clear yet how this should be handled... should we merge it with
+ colorspace conversion???
+
+Note that some high-speed operating modes might condense the entire
+postprocessing sequence to a single module (upsample, color convert, and
+quantize in one step).
+
+In addition to the above objects, the decompression library includes these
+objects:
+
+* Master control: determines the number of passes required, controls overall
+ and per-pass initialization of the other modules. This is subdivided into
+ input and output control: jdinput.c controls only input-side processing,
+ while jdmaster.c handles overall initialization and output-side control.
+
+* Marker reading: decodes JPEG markers (except for RSTn).
+
+* Data source manager: supplies the input JPEG datastream. The source
+ manager supplied with the library knows how to read from a stdio stream;
+ for other behaviors, the surrounding application may provide its own source
+ manager.
+
+* Memory manager: same as for compression library.
+
+* Error handler: same as for compression library.
+
+* Progress monitor: same as for compression library.
+
+As with compression, the data source manager, error handler, and progress
+monitor are candidates for replacement by a surrounding application.
+
+
+*** Decompression input and output separation ***
+
+To support efficient incremental display of progressive JPEG files, the
+decompressor is divided into two sections that can run independently:
+
+1. Data input includes marker parsing, entropy decoding, and input into the
+ coefficient controller's DCT coefficient buffer. Note that this
+ processing is relatively cheap and fast.
+
+2. Data output reads from the DCT coefficient buffer and performs the IDCT
+ and all postprocessing steps.
+
+For a progressive JPEG file, the data input processing is allowed to get
+arbitrarily far ahead of the data output processing. (This occurs only
+if the application calls jpeg_consume_input(); otherwise input and output
+run in lockstep, since the input section is called only when the output
+section needs more data.) In this way the application can avoid making
+extra display passes when data is arriving faster than the display pass
+can run. Furthermore, it is possible to abort an output pass without
+losing anything, since the coefficient buffer is read-only as far as the
+output section is concerned. See libjpeg.doc for more detail.
+
+A full-image coefficient array is only created if the JPEG file has multiple
+scans (or if the application specifies buffered-image mode anyway). When
+reading a single-scan file, the coefficient controller normally creates only
+a one-MCU buffer, so input and output processing must run in lockstep in this
+case. jpeg_consume_input() is effectively a no-op in this situation.
+
+The main impact of dividing the decompressor in this fashion is that we must
+be very careful with shared variables in the cinfo data structure. Each
+variable that can change during the course of decompression must be
+classified as belonging to data input or data output, and each section must
+look only at its own variables. For example, the data output section may not
+depend on any of the variables that describe the current scan in the JPEG
+file, because these may change as the data input section advances into a new
+scan.
+
+The progress monitor is (somewhat arbitrarily) defined to treat input of the
+file as one pass when buffered-image mode is not used, and to ignore data
+input work completely when buffered-image mode is used. Note that the
+library has no reliable way to predict the number of passes when dealing
+with a progressive JPEG file, nor can it predict the number of output passes
+in buffered-image mode. So the work estimate is inherently bogus anyway.
+
+No comparable division is currently made in the compression library, because
+there isn't any real need for it.
+
+
+*** Data formats ***
+
+Arrays of pixel sample values use the following data structure:
+
+ typedef something JSAMPLE; a pixel component value, 0..MAXJSAMPLE
+ typedef JSAMPLE *JSAMPROW; ptr to a row of samples
+ typedef JSAMPROW *JSAMPARRAY; ptr to a list of rows
+ typedef JSAMPARRAY *JSAMPIMAGE; ptr to a list of color-component arrays
+
+The basic element type JSAMPLE will typically be one of unsigned char,
+(signed) char, or short. Short will be used if samples wider than 8 bits are
+to be supported (this is a compile-time option). Otherwise, unsigned char is
+used if possible. If the compiler only supports signed chars, then it is
+necessary to mask off the value when reading. Thus, all reads of JSAMPLE
+values must be coded as "GETJSAMPLE(value)", where the macro will be defined
+as "((value) & 0xFF)" on signed-char machines and "((int) (value))" elsewhere.
+
+With these conventions, JSAMPLE values can be assumed to be >= 0. This helps
+simplify correct rounding during downsampling, etc. The JPEG standard's
+specification that sample values run from -128..127 is accommodated by
+subtracting 128 just as the sample value is copied into the source array for
+the DCT step (this will be an array of signed ints). Similarly, during
+decompression the output of the IDCT step will be immediately shifted back to
+0..255. (NB: different values are required when 12-bit samples are in use.
+The code is written in terms of MAXJSAMPLE and CENTERJSAMPLE, which will be
+defined as 255 and 128 respectively in an 8-bit implementation, and as 4095
+and 2048 in a 12-bit implementation.)
+
+We use a pointer per row, rather than a two-dimensional JSAMPLE array. This
+choice costs only a small amount of memory and has several benefits:
+* Code using the data structure doesn't need to know the allocated width of
+ the rows. This simplifies edge expansion/compression, since we can work
+ in an array that's wider than the logical picture width.
+* Indexing doesn't require multiplication; this is a performance win on many
+ machines.
+* Arrays with more than 64K total elements can be supported even on machines
+ where malloc() cannot allocate chunks larger than 64K.
+* The rows forming a component array may be allocated at different times
+ without extra copying. This trick allows some speedups in smoothing steps
+ that need access to the previous and next rows.
+
+Note that each color component is stored in a separate array; we don't use the
+traditional layout in which the components of a pixel are stored together.
+This simplifies coding of modules that work on each component independently,
+because they don't need to know how many components there are. Furthermore,
+we can read or write each component to a temporary file independently, which
+is helpful when dealing with noninterleaved JPEG files.
+
+In general, a specific sample value is accessed by code such as
+ GETJSAMPLE(image[colorcomponent][row][col])
+where col is measured from the image left edge, but row is measured from the
+first sample row currently in memory. Either of the first two indexings can
+be precomputed by copying the relevant pointer.
+
+
+Since most image-processing applications prefer to work on images in which
+the components of a pixel are stored together, the data passed to or from the
+surrounding application uses the traditional convention: a single pixel is
+represented by N consecutive JSAMPLE values, and an image row is an array of
+(# of color components)*(image width) JSAMPLEs. One or more rows of data can
+be represented by a pointer of type JSAMPARRAY in this scheme. This scheme is
+converted to component-wise storage inside the JPEG library. (Applications
+that want to skip JPEG preprocessing or postprocessing will have to contend
+with component-wise storage.)
+
+
+Arrays of DCT-coefficient values use the following data structure:
+
+ typedef short JCOEF; a 16-bit signed integer
+ typedef JCOEF JBLOCK[DCTSIZE2]; an 8x8 block of coefficients
+ typedef JBLOCK *JBLOCKROW; ptr to one horizontal row of 8x8 blocks
+ typedef JBLOCKROW *JBLOCKARRAY; ptr to a list of such rows
+ typedef JBLOCKARRAY *JBLOCKIMAGE; ptr to a list of color component arrays
+
+The underlying type is at least a 16-bit signed integer; while "short" is big
+enough on all machines of interest, on some machines it is preferable to use
+"int" for speed reasons, despite the storage cost. Coefficients are grouped
+into 8x8 blocks (but we always use #defines DCTSIZE and DCTSIZE2 rather than
+"8" and "64").
+
+The contents of a coefficient block may be in either "natural" or zigzagged
+order, and may be true values or divided by the quantization coefficients,
+depending on where the block is in the processing pipeline. In the current
+library, coefficient blocks are kept in natural order everywhere; the entropy
+codecs zigzag or dezigzag the data as it is written or read. The blocks
+contain quantized coefficients everywhere outside the DCT/IDCT subsystems.
+(This latter decision may need to be revisited to support variable
+quantization a la JPEG Part 3.)
+
+Notice that the allocation unit is now a row of 8x8 blocks, corresponding to
+eight rows of samples. Otherwise the structure is much the same as for
+samples, and for the same reasons.
+
+On machines where malloc() can't handle a request bigger than 64Kb, this data
+structure limits us to rows of less than 512 JBLOCKs, or a picture width of
+4000+ pixels. This seems an acceptable restriction.
+
+
+On 80x86 machines, the bottom-level pointer types (JSAMPROW and JBLOCKROW)
+must be declared as "far" pointers, but the upper levels can be "near"
+(implying that the pointer lists are allocated in the DS segment).
+We use a #define symbol FAR, which expands to the "far" keyword when
+compiling on 80x86 machines and to nothing elsewhere.
+
+
+*** Suspendable processing ***
+
+In some applications it is desirable to use the JPEG library as an
+incremental, memory-to-memory filter. In this situation the data source or
+destination may be a limited-size buffer, and we can't rely on being able to
+empty or refill the buffer at arbitrary times. Instead the application would
+like to have control return from the library at buffer overflow/underrun, and
+then resume compression or decompression at a later time.
+
+This scenario is supported for simple cases. (For anything more complex, we
+recommend that the application "bite the bullet" and develop real multitasking
+capability.) The libjpeg.doc file goes into more detail about the usage and
+limitations of this capability; here we address the implications for library
+structure.
+
+The essence of the problem is that the entropy codec (coder or decoder) must
+be prepared to stop at arbitrary times. In turn, the controllers that call
+the entropy codec must be able to stop before having produced or consumed all
+the data that they normally would handle in one call. That part is reasonably
+straightforward: we make the controller call interfaces include "progress
+counters" which indicate the number of data chunks successfully processed, and
+we require callers to test the counter rather than just assume all of the data
+was processed.
+
+Rather than trying to restart at an arbitrary point, the current Huffman
+codecs are designed to restart at the beginning of the current MCU after a
+suspension due to buffer overflow/underrun. At the start of each call, the
+codec's internal state is loaded from permanent storage (in the JPEG object
+structures) into local variables. On successful completion of the MCU, the
+permanent state is updated. (This copying is not very expensive, and may even
+lead to *improved* performance if the local variables can be registerized.)
+If a suspension occurs, the codec simply returns without updating the state,
+thus effectively reverting to the start of the MCU. Note that this implies
+leaving some data unprocessed in the source/destination buffer (ie, the
+compressed partial MCU). The data source/destination module interfaces are
+specified so as to make this possible. This also implies that the data buffer
+must be large enough to hold a worst-case compressed MCU; a couple thousand
+bytes should be enough.
+
+In a successive-approximation AC refinement scan, the progressive Huffman
+decoder has to be able to undo assignments of newly nonzero coefficients if it
+suspends before the MCU is complete, since decoding requires distinguishing
+previously-zero and previously-nonzero coefficients. This is a bit tedious
+but probably won't have much effect on performance. Other variants of Huffman
+decoding need not worry about this, since they will just store the same values
+again if forced to repeat the MCU.
+
+This approach would probably not work for an arithmetic codec, since its
+modifiable state is quite large and couldn't be copied cheaply. Instead it
+would have to suspend and resume exactly at the point of the buffer end.
+
+The JPEG marker reader is designed to cope with suspension at an arbitrary
+point. It does so by backing up to the start of the marker parameter segment,
+so the data buffer must be big enough to hold the largest marker of interest.
+Again, a couple KB should be adequate. (A special "skip" convention is used
+to bypass COM and APPn markers, so these can be larger than the buffer size
+without causing problems; otherwise a 64K buffer would be needed in the worst
+case.)
+
+The JPEG marker writer currently does *not* cope with suspension. I feel that
+this is not necessary; it is much easier simply to require the application to
+ensure there is enough buffer space before starting. (An empty 2K buffer is
+more than sufficient for the header markers; and ensuring there are a dozen or
+two bytes available before calling jpeg_finish_compress() will suffice for the
+trailer.) This would not work for writing multi-scan JPEG files, but
+we simply do not intend to support that capability with suspension.
+
+
+*** Memory manager services ***
+
+The JPEG library's memory manager controls allocation and deallocation of
+memory, and it manages large "virtual" data arrays on machines where the
+operating system does not provide virtual memory. Note that the same
+memory manager serves both compression and decompression operations.
+
+In all cases, allocated objects are tied to a particular compression or
+decompression master record, and they will be released when that master
+record is destroyed.
+
+The memory manager does not provide explicit deallocation of objects.
+Instead, objects are created in "pools" of free storage, and a whole pool
+can be freed at once. This approach helps prevent storage-leak bugs, and
+it speeds up operations whenever malloc/free are slow (as they often are).
+The pools can be regarded as lifetime identifiers for objects. Two
+pools/lifetimes are defined:
+ * JPOOL_PERMANENT lasts until master record is destroyed
+ * JPOOL_IMAGE lasts until done with image (JPEG datastream)
+Permanent lifetime is used for parameters and tables that should be carried
+across from one datastream to another; this includes all application-visible
+parameters. Image lifetime is used for everything else. (A third lifetime,
+JPOOL_PASS = one processing pass, was originally planned. However it was
+dropped as not being worthwhile. The actual usage patterns are such that the
+peak memory usage would be about the same anyway; and having per-pass storage
+substantially complicates the virtual memory allocation rules --- see below.)
+
+The memory manager deals with three kinds of object:
+1. "Small" objects. Typically these require no more than 10K-20K total.
+2. "Large" objects. These may require tens to hundreds of K depending on
+ image size. Semantically they behave the same as small objects, but we
+ distinguish them for two reasons:
+ * On MS-DOS machines, large objects are referenced by FAR pointers,
+ small objects by NEAR pointers.
+ * Pool allocation heuristics may differ for large and small objects.
+ Note that individual "large" objects cannot exceed the size allowed by
+ type size_t, which may be 64K or less on some machines.
+3. "Virtual" objects. These are large 2-D arrays of JSAMPLEs or JBLOCKs
+ (typically large enough for the entire image being processed). The
+ memory manager provides stripwise access to these arrays. On machines
+ without virtual memory, the rest of the array may be swapped out to a
+ temporary file.
+
+(Note: JSAMPARRAY and JBLOCKARRAY data structures are a combination of large
+objects for the data proper and small objects for the row pointers. For
+convenience and speed, the memory manager provides single routines to create
+these structures. Similarly, virtual arrays include a small control block
+and a JSAMPARRAY or JBLOCKARRAY working buffer, all created with one call.)
+
+In the present implementation, virtual arrays are only permitted to have image
+lifespan. (Permanent lifespan would not be reasonable, and pass lifespan is
+not very useful since a virtual array's raison d'etre is to store data for
+multiple passes through the image.) We also expect that only "small" objects
+will be given permanent lifespan, though this restriction is not required by
+the memory manager.
+
+In a non-virtual-memory machine, some performance benefit can be gained by
+making the in-memory buffers for virtual arrays be as large as possible.
+(For small images, the buffers might fit entirely in memory, so blind
+swapping would be very wasteful.) The memory manager will adjust the height
+of the buffers to fit within a prespecified maximum memory usage. In order
+to do this in a reasonably optimal fashion, the manager needs to allocate all
+of the virtual arrays at once. Therefore, there isn't a one-step allocation
+routine for virtual arrays; instead, there is a "request" routine that simply
+allocates the control block, and a "realize" routine (called just once) that
+determines space allocation and creates all of the actual buffers. The
+realize routine must allow for space occupied by non-virtual large objects.
+(We don't bother to factor in the space needed for small objects, on the
+grounds that it isn't worth the trouble.)
+
+To support all this, we establish the following protocol for doing business
+with the memory manager:
+ 1. Modules must request virtual arrays (which may have only image lifespan)
+ during the initial setup phase, i.e., in their jinit_xxx routines.
+ 2. All "large" objects (including JSAMPARRAYs and JBLOCKARRAYs) must also be
+ allocated during initial setup.
+ 3. realize_virt_arrays will be called at the completion of initial setup.
+ The above conventions ensure that sufficient information is available
+ for it to choose a good size for virtual array buffers.
+Small objects of any lifespan may be allocated at any time. We expect that
+the total space used for small objects will be small enough to be negligible
+in the realize_virt_arrays computation.
+
+In a virtual-memory machine, we simply pretend that the available space is
+infinite, thus causing realize_virt_arrays to decide that it can allocate all
+the virtual arrays as full-size in-memory buffers. The overhead of the
+virtual-array access protocol is very small when no swapping occurs.
+
+A virtual array can be specified to be "pre-zeroed"; when this flag is set,
+never-yet-written sections of the array are set to zero before being made
+available to the caller. If this flag is not set, never-written sections
+of the array contain garbage. (This feature exists primarily because the
+equivalent logic would otherwise be needed in jdcoefct.c for progressive
+JPEG mode; we may as well make it available for possible other uses.)
+
+The first write pass on a virtual array is required to occur in top-to-bottom
+order; read passes, as well as any write passes after the first one, may
+access the array in any order. This restriction exists partly to simplify
+the virtual array control logic, and partly because some file systems may not
+support seeking beyond the current end-of-file in a temporary file. The main
+implication of this restriction is that rearrangement of rows (such as
+converting top-to-bottom data order to bottom-to-top) must be handled while
+reading data out of the virtual array, not while putting it in.
+
+
+*** Memory manager internal structure ***
+
+To isolate system dependencies as much as possible, we have broken the
+memory manager into two parts. There is a reasonably system-independent
+"front end" (jmemmgr.c) and a "back end" that contains only the code
+likely to change across systems. All of the memory management methods
+outlined above are implemented by the front end. The back end provides
+the following routines for use by the front end (none of these routines
+are known to the rest of the JPEG code):
+
+jpeg_mem_init, jpeg_mem_term system-dependent initialization/shutdown
+
+jpeg_get_small, jpeg_free_small interface to malloc and free library routines
+ (or their equivalents)
+
+jpeg_get_large, jpeg_free_large interface to FAR malloc/free in MSDOS machines;
+ else usually the same as
+ jpeg_get_small/jpeg_free_small
+
+jpeg_mem_available estimate available memory
+
+jpeg_open_backing_store create a backing-store object
+
+read_backing_store, manipulate a backing-store object
+write_backing_store,
+close_backing_store
+
+On some systems there will be more than one type of backing-store object
+(specifically, in MS-DOS a backing store file might be an area of extended
+memory as well as a disk file). jpeg_open_backing_store is responsible for
+choosing how to implement a given object. The read/write/close routines
+are method pointers in the structure that describes a given object; this
+lets them be different for different object types.
+
+It may be necessary to ensure that backing store objects are explicitly
+released upon abnormal program termination. For example, MS-DOS won't free
+extended memory by itself. To support this, we will expect the main program
+or surrounding application to arrange to call self_destruct (typically via
+jpeg_destroy) upon abnormal termination. This may require a SIGINT signal
+handler or equivalent. We don't want to have the back end module install its
+own signal handler, because that would pre-empt the surrounding application's
+ability to control signal handling.
+
+The IJG distribution includes several memory manager back end implementations.
+Usually the same back end should be suitable for all applications on a given
+system, but it is possible for an application to supply its own back end at
+need.
+
+
+*** Implications of DNL marker ***
+
+Some JPEG files may use a DNL marker to postpone definition of the image
+height (this would be useful for a fax-like scanner's output, for instance).
+In these files the SOF marker claims the image height is 0, and you only
+find out the true image height at the end of the first scan.
+
+We could read these files as follows:
+1. Upon seeing zero image height, replace it by 65535 (the maximum allowed).
+2. When the DNL is found, update the image height in the global image
+ descriptor.
+This implies that control modules must avoid making copies of the image
+height, and must re-test for termination after each MCU row. This would
+be easy enough to do.
+
+In cases where image-size data structures are allocated, this approach will
+result in very inefficient use of virtual memory or much-larger-than-necessary
+temporary files. This seems acceptable for something that probably won't be a
+mainstream usage. People might have to forgo use of memory-hogging options
+(such as two-pass color quantization or noninterleaved JPEG files) if they
+want efficient conversion of such files. (One could improve efficiency by
+demanding a user-supplied upper bound for the height, less than 65536; in most
+cases it could be much less.)
+
+The standard also permits the SOF marker to overestimate the image height,
+with a DNL to give the true, smaller height at the end of the first scan.
+This would solve the space problems if the overestimate wasn't too great.
+However, it implies that you don't even know whether DNL will be used.
+
+This leads to a couple of very serious objections:
+1. Testing for a DNL marker must occur in the inner loop of the decompressor's
+ Huffman decoder; this implies a speed penalty whether the feature is used
+ or not.
+2. There is no way to hide the last-minute change in image height from an
+ application using the decoder. Thus *every* application using the IJG
+ library would suffer a complexity penalty whether it cared about DNL or
+ not.
+We currently do not support DNL because of these problems.
+
+A different approach is to insist that DNL-using files be preprocessed by a
+separate program that reads ahead to the DNL, then goes back and fixes the SOF
+marker. This is a much simpler solution and is probably far more efficient.
+Even if one wants piped input, buffering the first scan of the JPEG file needs
+a lot smaller temp file than is implied by the maximum-height method. For
+this approach we'd simply treat DNL as a no-op in the decompressor (at most,
+check that it matches the SOF image height).
+
+We will not worry about making the compressor capable of outputting DNL.
+Something similar to the first scheme above could be applied if anyone ever
+wants to make that work.
diff --git a/test/monniaux/jpeg-6b/testimg.bmp b/test/monniaux/jpeg-6b/testimg.bmp
new file mode 100644
index 00000000..8603d154
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testimg.bmp
Binary files differ
diff --git a/test/monniaux/jpeg-6b/testimg.jpg b/test/monniaux/jpeg-6b/testimg.jpg
new file mode 100644
index 00000000..b34ca5d3
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testimg.jpg
Binary files differ
diff --git a/test/monniaux/jpeg-6b/testimg.ppm b/test/monniaux/jpeg-6b/testimg.ppm
new file mode 100644
index 00000000..9d81ce24
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testimg.ppm
@@ -0,0 +1,4 @@
+P6
+227 149
+255
+0/-0/-10.21/51.51.62/62/83/83/:3-:3-:3-:3-:3-:3-:2/91.91.80-80-91.91.:2/80-80-80-80-80-80-80-80-6.+6.+6.+5-*5-*4,)4,)4,)4,)4,)4,)4,)4,)4,)4,)2-)/*$/,%/,%0-&1.'2/(30)30)63,63,74-85.85.96/:70:7.A:0B<0D>2F@4IA4JB5KC6KC6MD5MD5OC3NB2OC3OC3PD4RE5R?1Y?2b@4nB5}E6‹H8™G9£F7¯G:¸G9¾E:ÅG;ÇG>ÊG?ËH@ÐE@çFLíCLëDKëEIîCIïBDñ>Bô=Aø;A÷:@ô:?ð<?é?@â@>×?<ËA7»=/µ@.µ@.´?-´?-³@-²?-¯@-­@,ªA,¦A-¢B,Ÿ@*›A)˜@*–A,”>-’?/’?/‘>.‘>,=+’<+’<+”?+”?+”=*”=*”=*•>+–?,–@/–?6•>5—=2Ÿ?1©B3³D3¼D4¿D4¹?0µA2¬F8žH;‡H9oA2T8*C3&=5295495473271160050-50-72/72/72/61.61-50,50,41,//-.0-//-//-0/-0/-2.-2.-5,-4+,4*+3)*7(+=.1E69P:<jBC|IHMM•OOŸW[ªdnªoƒŸt”{£‡®€†º~ˆ½sy­`a‘TKvPDhSJgOG^MH^TQbfdo|}‚‘™ž˜£©Ÿ¢¨šž “’{|lfgUXWEQNEUR[UQbUQb0/-0/-10.10.40-51.62/62/83/83/:3-:3-:3-:3-:3-:3-91.91.80-80-80-80-91.91.80-80-80-80-80-80-80-80-6.+6.+5-*5-*5-*4,)4,)4,)5-*5-*5-*5-*5-*5-*5-*3.*0+%0-&0-&1.'2/(30)41*41*63,63,74-74-85.96/:70:7.@9/A;/C=1E?3H@3IA4JB5JB5LC4LC4MA1MA1MA1NB2OC3QD4P>0U?1^A3jC4xD6„E4’E5œC3§C4¯A4µA4¼B7ÀD:ÄE<ÅF=ÍC@áEIçBIèCIêDHíDGïBDó@Cö?Cø;A÷:@ô:?ð<?é?@àA>Õ@<Ê@6¹>/µ@.´?-´?-´?-²?,°?-¯@-­@,©@+¦A-¡A+Ÿ@*›A)˜@*–A,”>-’?/‘>.‘>.‘>,=+’<+’<+”?+“>*”=*”=*”=*•>+–?,–@/”@5•>5˜>3 >1«A3µD4½C4¿D5»A2·C6¬F8œI;…G:l@3S9*B4)>63:6595484382271161.61.72/72/72/61.61-50,50,41,//-.0-//-//-0/-0/-2.-2.-3--5,-4*+3)*5)+<-0C47N8:d>=vEA†JINLšTV¤aj¥l}rŽ‘{¢†€®…¹{„»ou©[[QHuOCiOFeOG_PH_RN_[Yfnotƒ‡ˆ”™•™ž—š ”™‘ƒ~ojkY][LVSJXSZVRaXQa/.,/.,0/-10.40-40-51.51.72.72.72.72.92,92,92,92,91.80.7/-7/-7/-7/-80.91/80.80.80.80.80.80.80.80.6.,5-+5-+5-+4,*4,*4,*4,*5-+5-+5-+5-+5-+5-+5-+3.*2-'1.'2/(30)30)41*41*52+63,63,63,74-85.96/96/:7.?8.@:.B<0D>2G?4H@5H@3H@3I@1I@1K?1K?1K?/L@0MA1NB2MA1QA1YB2dC2qC3|C2‡B2’A0˜<- :+§;.¯=2µ@6ºD:¿F=ÅD>ÙCEá@FãBGçBFêDFðCEôADø?Dú;@ù:?õ;@ð=@è@@ÜA=Ñ@;Æ@5·=.³@-³@-²?,²?-°?-¯>,­@,ª?-§@-¥@,¡A+A,š?*˜@*•@+”>-‘>.‘>.‘>.=+=+=+=+‘>,‘>,’<+’<+“=,“=,”?+•?.•A6–?5š>3£>2¯A4¹C5¿D5ÁC5ÀD8¸F;®I=™J=G;h@4Q:,B5,?74=77<66:4494183072/72/62/62/62/51.52-41,41,21,.0-,1-.0-.0-//-//-0/-2.-5//4..5,-4*+4*+9-/>24I56[97l?9|E@†IDOM˜[`›fv”mˆŒwžƒ}­}‚¹u~·fm¤TV‰MEvLAkMAeOFcQHcMH^NK\[[eqty…‰ˆ‡Œ†Šˆ…†Š|xzlfiXZ[MVSLZU[ZT`[S`.-+/.,/.,0/-3/,40-40-40-61-61-61-61-81+81+81+81+7/-7/-6.,6.,6.,6.,7/-7/-80.80.80.80.80.80.80.80.5-+5-+5-+4,*4,*4,*3+)3+)6.,6.,6.,6.,6.,6.,6.,4/,30+30)30)41*41*52+52+52+52+52+63,74-85.85.96/96->7-?9-@:.B<0E=2E=2F>1F>1G=1G>/H<.I=/I=/J>.L@0JA0KD2NE4UD4^D3iD2sB1~A/†?-Œ9)”9'9*£<-¬@3³E8¸H<ÁF>ÒDCÚACÞBCâDCçCDìBCó@C÷?Aú;@ù:?õ;@î>@åA@ÚB=Í@9Â@3µ=-°@,°@,°@,¯>,®?,®?,¬?+©@-¦?,£@+ @*œ@+˜@*–@)”?*‘>,‘>.‘>.=-=+=+<*<*=+=+<*<*’<+‘>,”>-’?-•A6–?5œ>2¦@4²B6¼C8ÁC7ÂB7ÂF<ºJ?¬L@—K>|F:b@4L:.A7-@85>96=77<74:5294183083062/62/62/32.52-21,21,12--2.-2./1./1.00.00.10.10.5106005//5,-4+,6,-:01D22T71c;3qA7{E;‚HD‰RU_l‹i‚ƒs˜}y«x}µowµ`f¢QR‹LEyL@pL@hPEgQFfLC^GBVMLZ^^fjnquyxx}wz€vwzokoa`bTWYLTTL]WY]V]]V^------.-+/.,0/-10.3/,40-3/,3/,4/+4/+4/+4/+6/)6/)4/,4/,3.+3.+3.+3.+4/,4/,50-50-50-50-50-50-50-50-3.+3.+2-*2-*2-*1,)1,)1,)4/,4/,4/,4/,4/,4/,4/,4/,41,41,41,41,41,52-52-52-52-52-63.63.74/85096196/<5-=6,?8.@9/B:/C;0C;0C;.D:.D:.F:.G;-H<.I=/J>0I@1JG6MH5RG5YF5bE3jD1uB/}>,‚;)‹:)“:*š=,£B2¬F8²J=»I?ÌGBÔDCØDBÝEBâBBéAAð=@ô<>ù:?ø:<ô<>í?>áB>ÓC:ÅA5¹?0²?-®?,®?,®?,­>-¬>-¬>-ª?-¨>.¤?- ?,ž?+š?,—?+•>*“>*‘>,?.>->->-Ž=,Ž=,Ž=,Ž=,Ž=,Ž=,Ž=,<,>-‘>.?.”B4—A4@1¨@3¶A7¿C9ÅB8ÄA7¾C;·H?¦LCJ@tE;Z>2E9-<5+@93@85?75>63=52<4194083/62/43/43/23.32.12-12-02--2.,2.-2.-2./1./1.00.10.3205105104..2,,4+,7./=/.N5.Y9.e=1n@3sB;yKK€Zeƒg€p—zxªu{·ks´_d¦TT”OGƒLBwNAmNBhMAeJA`GBYGFXKKWMPU]cc`fbbia`f\Z`TW[MUXMXXP^YV`WX`WZ,,,,,,.-+.-+/.,0/-3/,3/,2.+2.+3.*3.*3.*3.*5.(5-*3.+3.+2-*1,)1,)2-*3.+3.+3.+3.+3.+3.+3.+3.+3.+3.+2-*2-*2-*2-*1,)1,)1,)0+(3.+3.+3.+3.+3.+3.+3.+3.+41,41,41,41,41,41,41,41,41,52-52-63.74/85085085.;4,<5+=6,>7-@7.A9.A9.A9.C9/C9-E9-F:.G;/H<.J>0HA1JG6IH6NG5VF6\E3dC2n@0v>-{<+‚;)Œ;*”=,œ@1£F5ªJ:´J=ÄH@ÌEAÑFAÖE@ÞCAä@>ì>?ñ:<÷;<ô:;ð<=é@=ÜC=ÍC8¾@2±>,®?,«@,ª?+ª?+ª?-©>,©>,¨?,¥>-¢?, ?,›>,—?+•>*“>)‘?*>+>->->-Ž=,Ž=,Ž=,<+Ž=,‹<+<+‹<+‹<-Œ=,>/Ž?.”B4—A2 @2¬B5¹C9ÂC:ÅB:ÂA;ºA9±I@£NGNEoG=S?4A;/96-@93A75?74>63<4194083/74/43/43/34/23.23.02-02-.3--3/-3/.3/.3/02/02/11/11/21/32032040/2.-1-,4..8.,G4-O4)X8+`<0e?6mGFyYd‚k…€uŸ||²w|¼nu»dh¯[[¡RMLB~OArL@hI=cH>`HB^ECX@BO;@FBGJDMJJQJJQIIQFKQEOUIVWO^YS`YS`XU++++++,,,---/.,/.,0/-0/-1-*1-*1-*1-*2-)2-)2-)2-)2-*2,,1++1++1++1++2,,2,,1++1++1++1++1++1++1++1++2,,2,,2,,1++1++1++0**0**3--3--3--3--3--3--3--3.+41,41,41,30+30+30+30+30+41,41,52-63.74/74/85085.:3+;4,<5-=6.?6/?6-?6-?7,B8.B8.E8/E9-G;/H<0J>2H@3HE6GF4KE5QD4XC2_B2f?.n=,v=,|:*…9)Œ;*“=.›B2¢F7¬F8»F<ÂF>ÉF>ÐE>ÙD@âC?ê@@ð>>ò::ñ;:ì<<äA<ÖC;ÆD6µ@/ª=)ª?-©@-©@-¨?,¨>.¨>.§=-¥>-£=. ?.ž?-š?-–?,“>*‘?*>)>+>->-Œ=,Œ=.Œ=.‹<-‹<-‹<-Š=-Š;,‰<,Š</‹>.‹=0Œ?/’C2˜B1¡A1®B6¼C:ÂC<ÄC=ÀC=¹FA²QJ¥XRXQsRIWI>CC7<?6>93@72>71=60:5/94.83/63.43.43.34/13.13..3-.3-.3--3/-3/-3/-3/.3/.3/02/02/00.11/22021/0/-/.,2.-4/,?0+D0)K3)T8-Z<4dFFu]jƒs‰€«…„¾~ƒÇtzÆmp½ce¯VSšLC‚K?qI<gG;cE>_FB]DBW?AN;?H:BE>HGDMHGQIGQHJRGNVKUXM^ZOaYNaXO++++++,,,,,,.,-/.,0/-0/-1-*1-*1-*1-*2-)2-)2-)2-*2,,1++1++0**0**1++1++2,,0**0**0**0**0**0**0**0**2,,2,,2,,1++1++0**0**0**2,,2,,2,,2,,2,,2,,2,,2,,3/,30+30+30+30+30+30+30+41,41,52-63.63.74/85085092,:3+;4,<5->5.>5.>5.>5,B8/B8.E8/E8/G:1I=1J>2I?3FC4FC4JB5OA4TA2\@2b>0j<-q<.w9*}8)…8(Œ:,•=/›B4¤B5²F:ºE;ÁF>ÊG?ÔG@ÞFAçCAîB@í;;ë;;ç>;ßB;ÑD:¿D4¯@-¤>(¦A-¦A-¥@,¥@.¥@.¤?-¤?-¤>/¢>.Ÿ@.œ?.˜?-•>+‘?*>)>+>->->-Œ=,Œ=.‹<-‹<-Š=-Š=-ˆ<.ˆ<.ˆ<.ˆ</‡>/ˆ>1‹?1‘D2–C1¢B4­C6ºC;ÁD>ÁD>»EA¹PL²[T¥e\‘f]u_T[UIGMACI?<92?82>71;6094.74-63.43.43.34.24/13./4..3-.3-.3--3/-3/-3/-3/.3/.3/02/02///-00.22022010.0/-0/-3/,8,,;,)C0*K70S<6^IHtbn‡z”Š¶ŒÆ„ˆÏz€ÌrxÆik¶WWŸID„E=nG<dD<aC>^CAY@CV@DP>EKGQRKWUQ^WU`XS_UR^TT^SY_S^[LaZJaZJ,-/,-/,-/,.-------.-+.-+/.,/.,1-*0,)0,)0,)/+(/+(/+*/+*/+*/+*/+*/+*/+*0,+/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*0,+0,+1-,1-,2.-1-,1-,1-,1-,1-,1-,1-,1-,0,)1-*2.+3/,3/,3/,3/,3/,3/,3/,3/,3/,40-51.62/74/80-92,:3-;4.=4/>5.>5.>5.?4.?5,B5-C6.D7/F90G:1F<2G?4H@5J@6P?5T>3X<1^90b6+m9.t8-|8+ƒ9,;/“=0˜?1ž>0§A3­A4µC8¾E:ÊG=ÔG>ÞE?åC@è@?êBAæDAÚE>ÈD8·B1ªA.¢B,¢A.¡@-¢?,¢>.¡=-¡=-¢>. ?/œ<,š=,˜<-•>-“=,=+Ž=*Œ>*‹<+‹<+Š=-‰<,‰<,‰<,ˆ:-‡;-‰=/†</†</‡=0ˆ>3ˆ@2‰A5‹A4‘E5—D4£E9±I>ºG@»D>»EA¸MG´ZR¯f_£qf‘sh~rdjj^V^SIQFLLBJF=B>5<8/95,74+63,33+43.34.14-14-02-/1,,1+,1+-2.-2.-2.-2./1./1./1./1.02/02/11/11/11/11/11/40/4+0;/3A32C4/J;6]OOymy‹…Ÿ“»”–Ï•Ùƒ‰ÓtzÆjn·_b§Z[”LItHBdA>]>>X?BUAIVLU\U`bbqnn}xv†|rulyoguh_k_W_P]\Hb\Fc]G,-/,-/,-/,-/------.,-.-+/.,.-+0,)0,)/+(/+(/+(/+(.*).*).*).*)/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*/+*0,+0,+0,+1-,1-,1-,1-,1-,1-,1-,1-,1-,1-,0,+0,)1-*2.+3/,3/,2.+2.+2.+2.+2.+2.+3/,40-51.62/80.91.:2/;4.=4/>50>50=4-?4.?4.A4.B5-C6.E80G:2H;3H>5H>5L=6O>6Q=4V;2Z90_7/h70p7.w7-9-‰9.<1–<1›=1¢@3§A3¬B5´D8¾E:ÉF<ÔE=ÛD=ßD@àE@ÞFAÔG>ÄF8²C2¥B-žB-ŸC.žB-Ÿ@.ž?-ž?-ž?-ž>.ž>.š=,™>,–=-”=,=+>+Œ>*Œ=,‹<+Š=+‰<,‰<,‰<,‡;+‡;-…<-†</…=/…=1ƒ=1„>2†@6‡A7ˆB6“G9—E7£G<¯J@¸IB¸GA·LF³TN±d\ªqfŸ~oo|mmseZfZNXMLNAKI=EC7@=4=:188.44,11)23-23-03,/2+/1,.0+.0+.0+/1.-2./1./1./1./1./1./1.02/02/11/11/11/11/11/2015+49-7<23?53H?:^VTxr|Šˆž““¹”—Ê–Ô„‹ÏyÂqy¸kt­hnž\_€XZqSUjRWjT^hZgmfvvr‚tˆ~’‡ƒ•‰~Žv†yr€qfteZeT[ZE`Z@b\D,-/,-/,-/,-/,-/,.-------.-+.-+.-+-,*/+(.*'.*'.*',+),*+,*+,*++)*+)*+)*+)*-+,-+,-+,-+,-+,-+,-+,-+,,*+,*+-+,-+,-+,.,-.,-.,-.,-.,-.,-/-./-./-./-./.,0,+0,+1-,2.-2.-2.-2.-1-,1-,1-,1-,1-,2.-3/.40/51.80.91.:2/;30=31=4/=4/=4/?40?4.A4.A4.C60D71F93G:4H;5J;6K<7N=6P;6S:5V72[6.c60k6.t5,}7/‡9/;0”<0–<1?3 @4¢@3¨@3±C6ºD8ÅE:ÍD:ÕF@×H@ÔIBÌI?¾E:®C3¡B.œA.B/œA.œ?.›>-›>-›>-›>/›?0˜<-–=-”<.“=.>-Œ=,‹<+Š=+‰<*‰<*‰<,‡;+‡;-…<-„:-ƒ;-„<0‚<0‚<2‚>3ƒ?4…A8‡C:ˆD9”J=—H; H>¬KD²KF³LG²SM®`V­sg¦qŒz‘Ž{‚‰ws€ocqbXcUNRDMN@HI;DD8@@49;.46+/1&01)01)/0*/0*./*./*//-//-//-.0-//-//-//-//-//-//-00.00.00.00.00.00.00.1/26+97+98/4;63HE>_^Yzz|‹š”±’—¿•Ç„ŽÃ}‰»{‰¶|‹²}Œ«}ˆšwƒq~‡n}‚n~~o‚~yŽ…ƒ™‹¡”‘¨˜¥”ˆŠƒ–ƒ|{j{i\hTXX@]Y<_[@-.0-.0-.0-.0-.0-.0.......-+.-+-,*-,*.*'.*'.*'.*),*++)*+)*+)*+)**()*()*(),*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*+-+,-+,-+,-+,.,-.,-.,-.,-/-./-./-./+*0,+1-,1-,1-,1-,0,+0,+0,+0,+0,+0,+1-,2.-3/.40/91/:20;31<41=31=31=31=4/>3/>3/@2/@3-A4.C60D71E82F93H94I:5J;6L:6N94Q83T50^72e60o6/x8/‚90Š:/<1”>1™?4›?4›?2Ÿ?1¥A2®B5¸D7¿E:ÇG>ËH@ÊJAÃI>¶F:ªB5žA0™@.™A-™A-˜?-—>,—>,™>,™=.—>.•<,”=,“=.=-Ž=,Š=+Š=-‰<,ˆ;)ˆ;)‡;+‡;+„;,„;,ƒ;-€;,;/€<1€<1>5ƒ@7ƒC:‡D<ˆE<”L@–H<žG>§JC®LI®QL­]Vªj^§€o¡yšš‚›ƒ†•€z‹xm{jbm]SZJQUFKO@EI:@D6;=057,13(01)/0(./).-(.-).-)/.,0/-/.,/.,/.,/.,/.,..,/.,..,0/-//-0/-//-//-//-//-2-17,:6*83-1961HJ?bfX{€z‹““£–°Œ•¶ƒ²¯…™²¤¶’¨³“¨«¤£Œ ž‡ž˜‚™}˜‰œ‹ˆ£”°š˜±›”­—‹£…›„’|k|iXfOSV;ZV9^Z=+/2+/2+/2+/2-.0-.0......------.-+-,*-,*,+),+),+),*+,*+,*++)*+)**()*()*(),*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*+,*++)*+)*+)*,*+-+,-+,.,-.,-/-./-./-./+*0,+0,+0,+0,+0,+/+*.*)/+*/+*/+*/+*0,+1-,3/.40/901:20;31<42=32=31<20<20=20=2.?1.?1.@2/A30B5/C60D63C84D95E:6G96H94K84N50X72_60i70r7/}:1†<1Œ>2>2—@6—?5—?5—?3œ@3£C5«C6³E8ºE;½G=¾H>¹G=°D8¦A5›?0—@/—@-—@-–?.•>-”=,•<,–=/–=/•<.“;-’</Ž<.‹<-‰<,‰<,ˆ<,‡;+†:*†:*†:*ƒ:+ƒ:+‚:,€:.€:0€<1€=4?6‚B9ƒD=‡F@ˆH?‘KA’H=šG?£LE¨PL¨XQ©f]¦vh¢Œw›™€•¤‡Œ£‡†ƒ~‘}t‚qjue\eRV_LNUCEL:?F6<@27:/68-01)00(.-(/,'/+(/+*1-,2.-1-,1-,1-,1-,1-,/.,1-,/.,2.-0/-2.-0/-0/-0/-0/-2-18,:5)51++66,GL8`hQzƒpˆ’‡—–š ™¥†–£‡›¢ª©›¹± À³˜·¨š¶§›·¨˜·¥‘±œ‰«’‰«±”’³–•´•­ˆ¢…„›{’xgzdTbIQT9VU9XW;,03,03,03,03./1./1//////....../.,.-+.-+.-+-,*-,*-+,-+,-+,,*++)**()*()*()+)*+)*+)*+)*+)*+)*+)*+)*,*+,*++)*+)*+)**()*()*(),*+,*+,*+-+,.,-.,-/-./-./+*/+*0,+0,+/+*/+*.*)-)(0,+0,+/+*0,+1-,2.-3/.40/:12:12;31<42=32<21<20;1/=20<1/>0->0-?1.@2/A4.?4.@51@72@93A:4B94C84F74H5/Q51X5/a6/l8-u9.€</†>0‹=1“?5•>5“?5“?4•B4šB4 C4¥D4¬B5°D8´E:±E;ªB7¡?4š>1–>0•?.”?+“=,“=,“<+“<+”<.”<.“;-’<-<.;-Š;,‰<,‡;+‡;+†:*†:*…9)ƒ:)ƒ:+9*9-9-€<1<3?6€A8‚C<…F?ˆIBŠICŽJAG>—HCŸNJ§VR§`Z©pe¤ƒrœ”}–¡ƒ¨Šˆ§ˆƒ „~–~yˆup{jdp\]iSR^JJS@BK:>E5:@29<134,22*1.)/,'/))0**1++2,,1++1++1++1++1++0,+0,+0,+1-,1-,1-,1-,/.,/.,/.,2,.8*75(13+(56&EK1\gGu‚d†“yŽ›Š‘ž”Ÿš‹ž˜¥——´ž¢Å¥©Î­¥É­¦Ê°§Ë±¤È¬—¾Ÿ‹³‘ˆ°²Ž²Ž²¬Š„¡‚€™{wŽrdx]Q_ENR7QQ5SR6,03,03,03,03./1./1./1./1/////////////.,/.,.-+.-+/-..,-.,--+,,*++)*+)**()+)*+)*+)*+)*+)*+)*+)*+)*+)*+)*+)**()*()*())'()'(+)*+)*,*+-+,.,-.,-/-./-./+*/+*/+*/+*/+*.*)-)(,('0,+0,+0,+0,+1-,2.-40/40/:12;23;23<34=32<21<21;10<1/<1/>0/=/.>0->0-?1.>3/=60;60;83<94=:5>93@72C60G4.O4+Y4+d5+n8,x:-;.…;.<4‘<5>3@3A2“B1—A2™@0 >1¦@4ªB7ªB9¥@6Ÿ>5—=2•?2’?-’?-‘>,=+‘;*‘;*’<-’<-‘;.‘;.Ž;-;-Š;.ˆ:-†:,„;,…9)…9)ƒ:)‚9(‚9*9*~8,~8,€<1€=4€@7B9„E>…IAˆKFŒMFŒIAŽH@˜JF¡SO¨]X©hb©{n¤Žy™œ’§†‰¬‹‚ªˆ€¡„™€~‹wxnjxadr[ZhQQ]IITCCL;>D6<?467/44,30+0+'1()1()2)*4+,2)*2)*2)*2)*2)*0**0**0**1++1++0,+0,+0,+0,+0,+1+-6)25)-4-%46!AH&Wc;q€W‚’mœ} †¢Œ‹¡Š¨‰•¸Ž È”¤Ð©Ó«¨Ò®§Ñ­¡Ë¥“½—…¯‡‚¬„‡°†ˆ®…Œ±ˆ‹«†Ÿ{{”tqˆk_sWM]BIO3JM0KK/./1,01./1./1.0/.0/.0////00.00.00.0/-0/-0/+/.,1-,2.-1-,1-,0,+/+,,*++)*+)*+)*+)*+)*+)*+)*+)*+),+)*+),+)*-(,*(),())'()'((&'*()+)*,*+,+)-+,.-+/.,/.,-,*-,*/+*/+*.*)-)(,(',('1-,2-*2-*2-*3.+4/,50-61.;31;31<42<42=31<20;1/;1/<1/<1/=/.=/.=/,>0-?1.=2.=4-=6.;819919:29:2;81?6/C2+J1,T2)^3*g7+o9-u=.z</‡=2‹=3‹?2Œ@2Ž@3@1’?1•=/˜</œ>2¢B6¡C7žB7™?4–>2”>1”>/“=.=-<,Ž=,Œ=,Œ=.>/Ž<.Ž<.‹</Š;.ˆ:-ˆ:-†:,„;,‚:+‚:+:*~;*~;+|:*}9,|:.€>2>5€@6‚C:ƒG=…JB‡NE‹OGŒICŽID“PJ˜\Tžg`uiž‡už˜€”¡…ª‰‰¬‹ƒ¨‡€¡„~›zytˆoi}bby]\pUUgOO\HGRAAH8=A388.85.7/,3+)2()2()3)*4*+0*,/+*0*,0**0*,0*,2),2),3*-1+-1+-1+-0,-0,-0,-1+/4)/4*+4-%46!?F%T`8m|Qg‰™tžz¡~ˆ¡zŠ§{‘¶‚›ÄˆŸËŸÉ™žÇÆš˜Á•Œ·Š‚­€…®‚Œµ‰„«‰®…‰©‚~œxvolƒfZnSJZ?GM3FJ1DF./0+.0+/0+01,01,01,12-21-32.43/43/62/51.41,3/,4/,50-50-4/,3.+2,,1-,0,+0,+.,-.,---/--/,,.++-*).))+.)/.)-/(/.)-/)-.)-.*+.*+/+*/+*-,*.-)--+-.)./*./*------.,-/-./.,0/-2.+2.+2-*4,)5-*6.+8/*:1,:1*;2+=4-=4->50>50>50=4/<3.;2-=2.<1-<1/;0.=/.>0/>0/@1.A0)@2);4*77-39-28.56.:3-?-)F*'L)'S*&Z/(`5,d<0k@0yA0@/†A1‹B3@3“=0“;/’8-“;/“=.”A1•B2”C2’A0‘>.‘;,—;0—:2’;1Œ=0ˆ?0„?/‚?.ƒ>.ˆ@1ˆ?0Š>1Š<0‰;/ˆ:.†:-ƒ;-{9+~@3w<.q7)w>-w>-v;-}?2{;/‚@4‡E9‰I?ˆLA†MB„PC‡NE‘KI—SP”bY‘paŒ}j‰‰q‰–|Šž‚¤‡¦‰Ž§Š§Œ‡¤ˆ~Ÿ‚uš{o—uiib„_[zXZsUZnSWeNPWEJK=C?6@93;0.6**3')3'+3'+1&*,*-)+***,*(),'+.(,1(-2'-3(.3(.3(02(00)10)1/*1/*0/)-1++0-(//#<?*V\@pzX‹gœu‹sŒ¡vŒ¦wŠ¨v‰ªuŽ´{™¿†ŸÁ¾–ºŠ³ƒˆ®}…«|ƒª{…ª~ƒ¨}ƒ¦~€ž|y”ur‰mh|cYkUMZFIQ<CH4?B//0*01+01+01+12,12,21,32-43.43.74/74/63.52-50,50,7/,7/,6.,6.,3.+2-*0,+0,+0,-/-./-0--/-,1+*/)*.)(-.)//(//(//(//)-/)-.*+.*+/+*/+*.-).-)./*./*./)./*.....0....../.,/.,1-*3.+5-*5.(7.)8/(:/);0*<2)=3*>5,>5,>5.>5.>5.=4-<3.<3.=2.<1-;0.;0.=/.=/.>0/@1.C0*C0)A2+>4+:6-95,:3-<1-?-+D*)I*(N+'T/)Z5-_:1e>/pA/xA-€A0ˆ@2?4‘=3“:2“;19.‹</‹>.Œ@0A1A1ŽA/‘?1•;2•;2<2‹=0†>0ƒ@/?/‚?.…@1†>/‰=0ˆ<.‡;.„;,‚:,€;,x8,x<1s9-n9+s>.s>.r8*u:,}=1ƒA5‰E:‹I=‰K@†NA„PC‡NE‘JH—SR’f]Œwf„†p€“wž„¥†ˆ§ˆ§Š§§Š¤‰Ÿƒwœ}o˜ve’i`‹`Y‚ZY{X\wXZnSSaJNUCFH;C@7<737/-3*+2)*1(+.(*,*+**,+),+),-(,/)-2(02(02(02(02(10)1/*1-*3-*3-*12-12.-0,)--%8:,SXBox]€‹i‡–o†™l†žnˆ£p†¤p„¥pŠ­w“¶€™¹‡–¶„“³Œ®{†«x‚¨w‚¨yƒ¨|ƒ¦|‚¤{x’uqˆnh{eYjWMZHEL<@D5;=/12,12,12,12,23-23-43.43.54/54/85085085074/72.61-80-80-7/,6.+4/,3.+2,,1-,1-./-.0.1/-0--/,,.+*/+)./(/1'//)-/)-/)-/)-.*+.*+0,+0,+/.*/.*/.)0/*0/*0/+//-///0./0./0/-/.,1-*2-*6.+70*90+:1*<1+=3*>4+?5,?6-?6-@7.@7.?6/>5.=4-=4-=2.<1-;0.;0.;0.;0.<1/?1.C2+E0+H/+K--L,/K+.I*/E+.A-,@.*A.(E.(M.)X1*b3-g5,j:,o;-w;0=4‡<6Œ<5:4:4Š<2‡=2†>2…?3†A2‰A3ŒB5C5Ž?2=/Œ>1Š>.‡>/…>,†=.ˆ<,‰;.‰<,ˆ:-†:*ƒ;,<)~=+{<+}=1z<1v:/u;/x@1x@1v<.v;-?5‚B8‰F=‹H?‰JA‡KA‡NCŠNF‘JH˜TQ–f\ve‡…n‚‘t{¤ƒ…¨‡ˆª‰ªŒ©ŒŽ¥‰ˆŸƒš}y˜xi–ma’e\‹a\†`]‚`[|]UrVPhPFYEBP?:D93:2.3,*/)*,)**(0'(1&*1&*1&*0'*1(+0*.0+//*.-+.,+0+,0)-0(-1(-1)-01/23/..*'**"57*PUAmv[|Ši€k~“j™jƒŸn€ n~ m€¥q‡¬x¯|‹­zˆªx„©v§v§u¦w¦y¤y€¢}}›yw‘tp‡mh|cYkUMZHDH9=?299-23-23-34.34.34.45/54/54/650761961:72:72961940940:2/91.91.80-50-4/,4..4..3/03/01/01/20.1..0--/-+.0)02).0*.0*.0*.0*,/+,/+*1-,1-,0/+0/+10+10+10+10,11/1111/010.10.2.+3.+3.*92,92,;2+<3,>4+@6-@6-A7.B8/A8/B90A8/A81@70>5.>5.=2.=2.<1/;0.;0.<1/=20=2.B3.E2.L..R+0V'/U&.P'/I).C/.<1+;2)?2)G0(R/)_.*d/)i9/k;/u<3}<6…;8‰;9Š;7ˆ:6‰>8†@8†B9…B9…C7†B7‡A7‰A5ˆ@1ˆA/‡@.‡@.‡>-ˆ<,ˆ;+‰:+Š8*ˆ9*‡:*ƒ:)<){=(x>(x>*‚>1{7.z7.z<1v;-w=/|A3{@2€B7ƒE:†H=ˆJ?†J@‡MBˆODŠQHŒSJŽ\QŽh[‹tb…g€‹m~–vž|ƒ§ƒ„©‡‡ª‰‹ªŠ‹¦‡‡ ‚ƒš~}˜yq•oi”ifgfŒeg‹gd†e_~_Zw[PhPK^JBP?8D62:/.4*,/(+*%2&&5%(4%(2&(1'(/)+/+,.,-+++*+-*+-(,-(,/',/',/*+-.*+1++0+'.+"88,PUAiqYv„c{Œhyh|–iœl}Ÿmyžkz m¥r‚§t€¥q}£p}£p}¥s~¥v~¥x}¤x|¡x}Ÿzz™wuqn‡je|bWkRN[GDF9?=1:8,45/45/45/560560560761761872872;83<94<94<94<73<73<41<41;30;3083072/61.61.5106216213122011/00./1-.2,04+.4+.4+.2,.2,,2,,2,,3/.3/,3/,3/,21,21,32-32.32032032032051051.61.61-;4.<5/=4-?6-A7.B8/E8/C9/D:1D:1E;2D:1C90B8/@5/@5/>3/>3/=2.=2.=20=20>31>31@51E31M02T,4X)3W(2R)1K,1B30:6-77+:6*B4)M2)X/)^/)f:1j;3s<7z=:‚<:†<;‡;;†::‚;7>8A8B9ƒC:ƒC:…@9„@7‚C2‚C1ƒB0„?/†=.ˆ<.‰:-‹9-‰7+‡8+…9+‚:+~=+x>*v?*x>*9-|/'‚8/„>4w4+s5*}A6}C7E:€G<‚I>ƒJ?„KB†MD‡QG…WJ|aNzjQ€pYu]‚|bƒ†i†“uˆ|ƒ£~§‚‚ª…‚¬†©„¥€{ž}z™wz’pzlxlwŒkumr‰lm„gkd`rX[iRR[HHL=@@4;7,70&5*$6('5''3''1'&.)&+*(++)+-*',('+*)+*)+***,+),+),-)*,#$2*'50*86*BC5UZFfpWn}^tˆeqŠbuex˜iw›ks™htšiwŸkz¢nx lwŸmx ny£s{¥u{¥w|£wyžuzœww–tsŽom†hd{_WkPN[GCC7>:195,560560671671782782872872983:94=:5>;6>;6>;6>95>95?74?74>63=52;63:5294194184184395484373243151240/6-06-.6-06-.4..4..4..4/,40-40-40-51.32-32-43.43.43/431542540841850940:5/=60>7/@7.A8/C90D:0G:1H;2F<3F<3F<3F<3E;2C90B71A60@51@51?40>3/>31?42@53?53@72C52I35P16T/6S.5P05J22C52=90<:-=:+C7)I6(Q3)W2)]2+d3,l50v64}77‚87ƒ77‚66~75}:4}<6}>5€?9ƒ@8†?9…A8€B3€C1B1ƒ@0…=/‡;.ˆ:.‡9-…9,ƒ9,‚:,<,|=,y>,x?,|=,‡5*‹2,¡LE§XQ‹A8|90‚F;€K=yH:zJ<{M>|N?}OBQE‡UJ‚_LrkOosR|uX‡w]yb˜iŸ‹s ”z––z|‹£ˆ¨…§‚€¤~{Ÿy~™vƒ‘p…ŽoƒŽp€pp{ŽpwŒms‡kj~bfv\_hSV[GOM>GA3@6*=0';,'9+(6+'3+(/,',-'+.').().(+-(-,*/+*3)*4(*7'*7'(3($<3,E>4IG8QR@]bKgqVjyZn‚]k„\l‰]p‘bq•eo•do—eršfuŸmrœjq›itžnx¢rz¤vy¢vyŸvvštw™vu”rokj…fc|^UlON\ECC7@91;4,671782782782893893983:94:94;:5>;6?<7?<7?<7@;7@;7B:7B:7A96@85=84=84<73<73<73<74<74<74;639529338308/09/.8/080.80.80.61.61-61-61-52-52-63.63.74/74/540540841952:63<94=84@93@70A8/C90D:0G:1H<0I=1I=1J=4J=4J=4I<3F<3D:1B8/A7.A81@70@72?61?61@72@72A83?74@85B86D97G96H96H96H94E80E8/E9-E9+G9,I9*K9+Q7*Z/&d/'n3-z63ƒ98‰;9‹;:‹=;‹A>‡@:‚=6<3:3‚<4ˆ=7‰@7ƒA5B3‚@2ƒ?2ƒ=1„<0;/€</~<.|=.{=.|>/|>/}>/=/†9/1+¢<8ÍlfÙ~y­ZTŒC<ˆLAN@tJ<qM=pQ?qR@tS@zWD‚[J~eOmsOo{U„y[˜u_©oc¶mf¾qk½wo»‚w±Œz§—~žŸ€–¡¡~…y…™vˆ‘r‹rˆ‘r„“r€–r|–sx“pt’pm‹ii„edx\]kQV^GMP;ED0B;+@3+?2,;0*70*30)00(./'./)/0*2/*6,+:*+>(+C&+E$+C&(F5-LC4VQ>[YD`bJgmQiwVj{Wl‚[g‚Wf†WlŽ\o”an”an–bršfsko›jo™irœnw ty¢xxžwu›vs—su—vs’rn‹li„cb{[TmMM]CGH:E<5@707827828938938939:4:94:94;:5<;6?<7@=8@=8@=8A<8A<8C;8C;8C;8B:7?:6>95>95=84>95>95>95>95=85<73:51;30:0.:0.91.91.91/91.91.72.61-61-63.63.63.74/74/74/540651952;83<94?:4B;5B;3A8/B:/C9/E;/H<0I=1J>2J>2K>5K>5J=4J=4F<3E;2C90B8/B92B92A83@72@72A83B94A:4?82@93B;5D=7F=6G<6K<5N;4M6.N6,Q6+Q6+Q7*P9+P9)V6'f6*r6,~;3‰@9•D@›HDŸJGŸLF QJ™LDŽD;…;0„7-…7-Œ91=4ˆ>5‡>7†=6…<5…<5=4}=3z>3x@3vA3x@3z>3<3ƒ:3ˆ73‘31˜(&³=;ì|zý•’Åhc–G@‰K@xH:nM<jQ=fT>hV>lX@t[E`L€hPysQ„wUžt\´l^É__ÓV\ÙQ[×T\äouÙzx̆|À~µ•€«•}£’xŸvœŠrœŠt™u•u‘‘u‹’s†‘s‚‘r|‹ny†ju{amoWgbN_TBUE5R</O4-M1-I0,D/*>/(9/&7.'6/'81):/)=.+A,+F)+H(+K'+H)'TB4YQ<d^FgeLilOnuVm{Xl~Xk„Zg„Vg‡Vm]p•bq–br™duit›lo™ko–jr™mwvyŸxwšyt–us”uu”usqoŠki„ea|[TmMM]BMN@KB9F=69:49:49:49:49:4:;5=<7=<7=<7=<7@=8@=8A>9B?:D?;D?;E@<E@<E@:D?9D?9C>8E>8D=7B;5B;5B;5B;5B:7B;5A:4A:4A83A83A83@72@64>71>71>71<71<71;60:5/85.85.74/74/961961961:70<71=82A:2B;1C:1D<1F<2J>2K?3L@2N@3N@5N@7N@7L?6K>5I<3H;2E;2E;2B90A8/@91?80?80?80@93@93<5/MD=M@:K:3T@9R62O0+a<6\3-`5.`4+^/'^/%f6,m=1q=0‰I=G;—I?ŸKAŸF>›>7š=6žD;¦ND¤PE¨VJ«YK¥OB™A5—;0›>6“98‘98Œ65†52†84„?8|@6r>1rB4oA2q=0v:0‚72Œ43’-1š',ÈHIèbaÅEDÍWUáyv«VO†F<„VFmR=i[AibFjeHj`En^D|dLjT’bL²m]ÑnhÛX]à;Lç-Eò+Hõ0Lö@YìI\ä[eånrâ{|Û€}Ûƒ؉‚Ãv½wº„x¶†x²ˆx­Šw¨‹y¥Œxš€o™~m˜xi“oabY„UM{IBxA>u:<i.2d02a11P&'G%#L0-M51G4.E2,D1-F1.F.,G+*K--Q6/YH4`W:f_BgdEkoNu|Zu„]oYlƒWl‡Zp]r’as•bu—dw™fv›hr–js˜os—qq•os•tw™xz˜|y–zx•ysrm‡jk„fi‚dazZTmMO_DNO?SK@PG>9:49:49:49:4:;5;<6>=8?>9>=8>=8A>9A>9B?:C@;D?;D?;FA=E@<E@:E@:D?9D?9E>8E>8E>8E>8E>8D=5C<6C<4D;4C:1B92B92B92B92A83A83@93@93>71<71;60;6096/85.74/74/96196/96/:70<71>:1A:0B<0C;0E;/I=1J>0L@2L@2N@3N@3N@5N@7M?6J=4I<3H;2E;2E;2C:1B90@91@91@91@91A:4B92D93I81L/+V.,j76u99{;;…ECƒD?„H@…IA†G>ŽIB™NH¡PL¢OG“D7”B4šB6ŸC8 >3œ7-œ7-Ÿ=0£C5›?0™@0 E3¤G6§F6®H:¶NE´LM¯HL¥@D™9;‘98>:ƒ@8x>3o>0zI;…LA„?8„,+’).³9DÒLWÜKNßMMÊ>=À@?Ð`\³YQ„@5‚TDyaIe[@^]?ihIslOvgJ~dK–cN¼dXÖ_[æUZëANò,Eþ%Dÿ&Hÿ'Iÿ.Nù3Nò<RðIZëP^åQ]ãS]àXbÛbiÕflÔjnÒmqÏqrÍutÉxwÇywÊ||ÉyzÈvxÇqtÆkrÅfnÅakÃ_kÅ`n³S_¥KUšGQDK†EIm69HT1-M0*H1+J6/K81K:2O>6UE8[O5cZ9gaAifEnrOz‚]z‰bt†^r‰_p‰_p‹`qŽ`u’dw”dw”dw”fs’ix–rz—xy–x~˜}ƒ„…‡ƒ›…™ƒz’zr‰om„hhd`y[TmMNaERSAVPBUOC8938938939:4;<6<=7?>9@?:@?:@?:C@;C@;C@;C@;D?;D?;FA;FA;FA;E@:E@:E@:G@8F?7JA:I@9H?8G>5F=6E<3F<3E;1D:1D:1D:1D:1D;4D;4D;4C<4?80?80<71;60:5/:5/94.94.96/96/:5/;7.>7/@9/B90C;0E;1F<0I=1K?1M?2M@0NA1M@0P?5M?4L>3K=2I<3H;2E;1D:0C:1C:1A:0A:0?;2@<3@<3D;4UD<R3.j23ŽAG­LWÃU`Ë]fÆ_b›B>—I?–LAœNB¬RJ¾URÌOSÄLK¥@4B0 @0¥?1¥;.£7+¤8+§=/«E6¡>+˜7$:'¥>-¬A/²C2¶D:§02¯:B¹DL½LRºMR­KL–A>‚71{8/v6,x2*„1-œ37¹=GÕGWèM[âFIÏ53Ð:9½31ÈNKÆc]”J?Œ[J}cLj_CgdEtoOpQhJhN°hYäbbõQZôDQö7Iý1Jÿ2Nÿ/Mÿ*Hÿ2Oü0K÷1Jõ5Lò4Mð2Kó3Ló7Pò@XïC[îF]íIaëNcêQeèUhèVkåSjäRiãOiäNiçNlëPoòSsóVuþh…ña|å[tÕUlÍYlÉdr¨R]r-2^&'Y0,W:4Q?5B:-:8)>B1LN9VO3aV6e_?heDquT†e~ŒiwˆdxŒiu‹er‹dr‹cvŒeyhzizj€•v„›~Šž… Š’£—¨–˜©™–¦™‘¢’‡˜†zypƒmg~b^wYTmMPcGSXDXUDYVG7827828939:4;<6=>8@?:A@;BA<BA<DA<DA<DA<DA<E@<E@<GB<GB<GB<FA;FA;FB9HA9HA9LC<KB9JA8I@7H>5G=3F<2E;1D:0D:0E;1E;2D;2E<3E<5E<5@91?80=82<71;60:5/:5/:5/96/96-;7.;7,?8.@:.C;0D</F<0F=.J>0K?/M@0M@0M@/M@0O?2O>4L>3K=2J<3G:1E;1D:0D;2D;2B;1@<1@<3A=4B>5F<3S81[*&‰:?¾T`ÙTeæUfãUcËJOŸ30–:/‘>0™A3®F=ÅIGÕBHÍ>@±B7§F5©B3¬@3¯?3°>3³B4´D6²G5©B/£<)¤;(©<(«:(®6&­3(¶97º9=½7>¼7<¿<BÆJLÄTS¾XS™?7Œ4*‰,%61ÃHKÜOWâAQÛ3@Þ:9Í.*Ó84È74ÆE@È]U©WKWFwW@veI~pSpQ„fJdK©o[ÕuiîSWüDP÷;Jø9Iþ=Nÿ@Qÿ:Lù4Hÿ?Sþ9Mú4Ký3Ký1Jÿ/Iÿ0Nÿ6Sÿ3Tý4Vý6Wú7Wø9Zø=\÷>^õ@aøCfö@fõ?eõ?gø?hüCmÿErÿIsÿOuÿQuþUvòSqçTnåaxÖfv·Wb}15j0/X1*P8,G>/>A.;D/?G/PK._T8g`CokN||`ˆŽr‡‘v}‹qzq|s|szozŠm}Œoƒ’uˆ—z•£Šœ©•£®¦±¡¬´§±¹®°·°«µ­¡«¢” ”ƒ‘‚s„qh|c]tXSlNPdHRZCWYDYZH671671782893:;5<=7?>9@?:BA<BA<DA<DA<DA<EB=FA=FA=FC<FC<GB<HC=HD;HD;JC;JC9LC:KB9KA8J@6J=4I=1H<0G;/E;/E;/E;/E;1E;1E;1D;2D;2@9/@9/@91?80<71;60;60:5/;7.;7.;7.<8-?8.A;/C;0D</G=1G>/K?1M@0M@0NA0O?/O?0O?2N=3N=3J<1I;0G;/D:0D:0D<1D<1B;1@<1A=2A?3B?6K=4\5.w32­LSÛ]iæM_å@Q×6E·&+¨1)7(’:&—<)¬@3ÃD=Ô>?Í;;±?5¨B4ª@2­?2²@5¸C9¼H;»J<¬>/ª?/¨=+¦;)ª9)°:,·;/¼<1ÊFAÎDAÐ>?Ð79Ö7<ÞBEâLNÝROÄC>ÆKCÍTLÔSMÙKJÛ?Bà5>á27Û4.Ú7.Ð1+Ï:4¿84³@9µ[P–UC{N9‚bI†kP„`F’[F­fTËrdçmhêEKò:Dê9Cç<DçBHçFKèGLêFMôJSòBMò9Iö5Hù2Gý/Fÿ2Lÿ8Rÿ9Vÿ9Xÿ:Xý:Zþ;]ÿ=_ÿ@bÿAeÿCiþDkþDmýBmüCoüCoüBqýBoÿGpÿ@hÿGmÿMpòIhéOkå[rÙcs½YcŽ?D`&$Q+"V@3VO=IL7@D-RJ3eYCujV€{gŽy™œ‹•œŒˆ“ƒ‚Ž€‰—ˆ›Œ™ˆ‡’‚Š“‚—žŽ¤¨™²¶§¸¼®¿Á¶ÀÁ¹ÂýÅÆÁÃÃþ¾¾°µ±¢©¢—Œy‡vi{e\sYTmORfJQ[BUZCW\F560560671782893:;5=<7>=8@?:@?:C@;DA<EB=EB=GB>GB>GD=GD=HC=ID>IE<IE<KD:LE;LC:KC8LB8KA5L@4K?3J>2I=1G=1G=1F<0F<0E;1E;1D;2C:1A:0A:0@91@91=82<71;60;60;7.;7.;7,<8-?9-A;/D</E=0H?0J>0L?/M@0NA0NA0O?/O?/O?2N=3M<2L;1I;0G;/F90C9/C;0B<0B;1@<1@>2A?3B?6N;4m84—FEÄY_ÛWbßCQÞ8FÔ3;½++­1'¡8%˜:!—9 ¤;&¶>.Ã:2¾71§7,¢:-¥9-§7,¯:1¸B8ºE;·E:®>2­?0§<,¤6'ª6)¶>0ÃD;ÊG=¾8-Î@6áD?ìBBô=Aó9>ë27Þ.0Û75âGBèSLåNGÜ=9Ù10ã/2ê67Ù1(Þ</Í,"Ð71¾3,°7/Ég\¸l\ŽWC‡ZCƒV?‰R=©\LÎlaágbäTTðEKñ=Fä;@Ù=>ÒA>ÒGBÝOMëWWíNRïDMð:Gö5F÷2Fø.Dû1Gþ7Nþ8Sý8Tý8Vþ9Wÿ;\ÿ>aÿAeÿCkþ;eý<hý>jý?mú>nø>oô=mö:jÿHrÿ;aÿ?dÿKnÿKn÷NmïUoãZnèr‚Âaj˜ILt:8\6-P9+PC2UM:^QAreUˆ|n•‚Ÿ‘¦§Ÿ¡¦Ÿ–œ˜•ž›ž§¤¦¯¬¦¬¨¢§£¦¨£µ´°ÂÁ¼ÌÉÀÑÎÅÕÐÌÕÐÍÔÎÎÕÏÑÑËÏËÆÊ¿½À°²¯˜Ÿ˜€Œ~m~k`t[WnRVjNS`FV`EX`H560560560560671893:94;:5=<7>=8A>9C@;DA<FC>HC?HC?GD=HE>ID>ID>JF=JF=MF<MF<ME:LD9MC9MC7NB6MA3N@3MA3JA2JA2I@1H?0G=1F<0D<1D<1C:1C:1A:2@91?82?82<71<71<8/<8-<8-<8-@:.A;/D</E=0H?0K?1M@0NA0P@0P@0O@-O?/O=1O=1M=0L</I;0F:.E8/C9/B:/A;/A:0?;0?=1@>2@@4P91r1/¨JKÁSV»>D½06Â03Á//º1)©1!£9#œ=!™;›< ¡=#¥;%¤6%ž6) 8-¢6,¥4,­81·@:¸C<²@6±B7­A4¨</¦8+¯:0¼C8ÃD=Å@7Ã>-Ï>-Ù5+á*&í"%ú%+ÿ*1ÿ.4í)*ç.+á3,ß4,à3,ã2,é0-ç2+Ú2%Ö6&Ð2&Ï7,Å6.½>7È`UÒq¦eSVBŠM:£WIÈf[ßd_çRTèCIõFMí@Dß??Ô@<É@8Æ@7ÑFAãMLïJNô@Kù9Hý7Hü5Hø3E÷6Gú;Mú8Pü7Rû6Rý6Uÿ6Xÿ9]ÿ;aÿ<gÿ>kÿ?mÿ@qÿBtÿBuþBuüBtùBpÿFmúAaþEeÿKjþKkÿVtÿ_zõ]tòj~æp~Óow®^aƒCAg6/hB7sUJye\Œ}v£–­£¡²®«¶¶¶²¶¹ª¯³°·½¶½Å½ÁÊÀÃÊÃÂÊÉÆÍÕÎÖÝ×ÛâÚØçÝÛéÝÝæÚÜåØßä×ààÔÞÙÏØÊÅ˺ºº¢§£ˆ’‡t‚qexb\sW[oS[jM\iK]gL561561561560561671872983<;6=<7@=8B?:DA<FC<GD=HE>HE>HE>HE<JF=JF;KG;KG;NH:MG;MG;ME:MD;MD;MD=NC?NC=MC7MD5KB3JA2H>2G=1E=2D;2C:1C:3A:2A:4@93?74<73<71>7/>7/<8/=90>:1A:2C<2D=3H@5I?3L@2MA1NA0P@0O@-O@-L?/L>1M=0L;1J91I81E80C90@9/>:/;;/<<0=>0>?1A?0S8-‡<7Ä\[ÊZY®86«.*²1+®.%­2#¬9&¬B*ªF,¦D'£A&£A&¢<#Ÿ9# 9*£;0¦:0¦7.­<4¶C<·E;°@5«=0¨</©;.¬<0»E9ÆLAÅE<¼8,¿6$Í:(Ý;.æ3,ñ+*ù(+ý&+û%'ø**õ.+ï2,ë4,è3*å3)ä2(à3%à:*Ñ2Ü?0Î6)È8-Â?5³A6Ñl`¿gY¥RB¨OAËcZçhbæRRêAFóBJë>Dá;=Ö<<Í@9Á>4¶8,¹6.Ä94âHHé@Eð;Dö:Hú:Iø8G÷;Iû?NþAUÿ?Vþ<Tþ9Tÿ8Uÿ9Zÿ:^ÿ;cÿ>iÿ>lÿ>oÿ>pý>qú@rø@t÷Aq÷CjÿMmÿSs÷Jhë@`ýTsÿg…ÿe‚÷]wï_xçh{Üp}Æqx§gg^X{ZQ—~wª™’¾°­Ä¼ºÆÂÃÈÇÌÇÇÏÃÃÏËÊØÌÊØÑÊÚ×ÎßßÔäæÛéêàëíãëïåæòèæôèèðäæîáèíàéêÝçäØâÑÊÑÁ¿Â««©‘–z†xl{hbu_`rXbpVboScmT21/320431651875984984984;:5<;6==5??7AA9CC9EE;EE;HH@HH>HH<JH;KJ8LK7ML8NK8MJ9JF:GD;JFCNIMNIPGDOD?ENE>LC4J@4KA5MC9LB8F=4B90E<5C:5@93?74@85?75<74<42C:5B94A96>95?:7>:7?;8@<9B?:D@7G@6J@4L@0O@-O@+L?,G@.F?/I;0K81M53K65J88F;9B?:6904</9D4:A/69$?=(bC1¼j^¹JA¦7,¥5)§7+¥5'¥7&¨=+£8$¥<&ª@*­@)¬=)¬9&®6%«6%¤6'¡7)¢8*¦<.ª@2¬B4¬B2¬A1©>,«=,­<,³=/½C4ÄF8À>1º2&Ç9+Ì8*Ð8+Ö8-Û7-â5.é3/î1-ñ.,ô.-ó0,ñ1,ê5*ã7)Ý:)Û:(Ü9&Ô3Ð3 Ï8'Æ7'¾6(ÂB5ÏSIÈNCÏSKá\Wî]ZðPRí>Cò9?÷?GêAFßCDÕAAÈ?9»<3±;/°</²<0ÊL@ÔKCÜGCá>?ë<A÷?Gý?Iù;GþDQüCQþDRÿDUý@Vü<Uÿ<ZÿBdÿFkÿAjú;gõ:gö<mùCsýIyÿKzÿMyÿMtøPsðOpçMiêMjüYxÿgˆÿ_ƒõGlöWwè_yÃ^n½{‡a^xp§‘„»§œÑÄ»ÝÖÐÝÜÚÜÛàÞÜçâÛëæ×ìëØîñÙñöÝó÷âó÷çôõëôôîòùôñùôðùñïöíîóèìîãééÞäæÝâÖÐÔÍÉÊ»º¸£¥ ‹‰yƒxr}op{k}†sxlv}k0./1/0320542653762873872:94;:5==5??7AA7CC9DD8EE;HH>HH>HH<JH9KJ6LK6MM5NM8KJ8KI=KJEPPRWU`YXjVTjRO`OIKMD?H?:F=8G>9H?:F=8B;5D:8A96?74?74@85@85=85;62=4/=4/=52<74=96>:9=<:>=9B?:D?9G@6J@4M@0O@-P?+L@*B?,B?.F<0H:1J65I56F35@65@<9:=6;>5@@4E<-J9)^B4€L>®QB«;- 2#¡6&£:'ž7$ž9%£>*¡;%§<(®?+³@-¶=,·9*¸6(µ7)ª9)¥:*¤9)¥;+¨>.«A1«B/«@.®?,«:(±<+¿E6ÆH9Á?1¼8+À6)Ë7+Ï7,Ð9.Ñ;-Ô</Ø:.à8/ç4-î1-ô.+ô.+ñ0+ê4)á7(×:'Õ:$Þ<'Ù6#Õ8%Ô=*Ì;*Á5&Ä:/ÏF<ÑF?ÚIDéOMóPQôGIð;@õ:AøCHåBEÚDCÒBAÅ>8·;1®:-ª<-«=.¶E5ÆL?ÔNEÜGCã?>ï?Bú?Fþ@JøCJ÷CLúDPÿFTýBSù>Sü>XÿBaÿFhÿCiü?iù@jùBpüFvþJzÿK{ÿJzÿHtõKpñQsõ\zù_{ûXwüNqýEkúDlïEiÚKgÈ_p±nukLI~t°–‰Ì²¥äÓÉìãÜëçæêéîêçòíâóöãùûáúÿâýÿçÿÿìÿÿòþÿöþÿùûþú÷ýúõü÷ôúõòøïðôëîñèíïæëæÝàÜÖØÊÆų²®ž¡šŽ“Œ‰…ˆƒ•‡‰€†Œ~/.,0/-10.21/43/540762761:94::2<<4>>6@@6BB8CC9DD:IF=IG;JH;LI8MJ7NL7NL7OL9KI:NKBTRS_^fihxmm…lkŠkhƒ`YiYQ\OGRH@KH>GG=EE<A@:<C:;?:7<74<73=82>93>:1=9083-94.;60<92=:3>;4?<5@<3E>4G?4I?3L@0O?0P?/P?-L?,<;&:=(?<+A;-B71A62>42;30=84B;5H94N2.Y,)l/.‡;= FE£;0¢2$Ÿ1 ¤9'¥<)ž7$ž8"¢<&¤;&ª=)±A-¶A/¹>.»<-¾:-¾</²<.¬<.©9+©9+«<+­>-­>-­<,²?-°8(¹>.ËL=ËG:¼6*º2&É9.Ð6,Ô6+Ô8,Ö:.Ø:/Ü8.ã6/è3,ð0-ô.+ô.+ð1)ç2'Ý6&Ô8"Ò7!Ý8$Ü5#Ú9'Ú>/Ó=.È6)Å9,ËA7É<5Í>8ÙEAåKIêJJéCCì?AëCCÞCAÕD?ÍB=Â=4¶:.®8*«:*¬;+¬8)ÀD8ÔNEÝIEà@@è>A÷BIÿHPôAGô@IùCOýGTþEUú@Uû?XþA_ÿDfÿCiÿCkÿFpÿIwÿK{ÿL|ÿJzÿFxÿHwÿKtúNtÿZ|ÿa€ýUvñCdûIkÿTvÜ?\ÍI`Ø{…¸||aC;|n®ŽÕ³§óÛÑûìåúòðù÷ú÷ôýôìûúëÿÿêÿÿìÿÿîÿÿóÿÿ÷ÿÿûÿÿýüÿþûÿýùÿúøÿøöÿõöýóôûðôùðóðçêåßáÓÏξ½¹­®¨¢¥ž£™£—£«žž¦—›£–/.,/.,0/-10,21-32.54/650880991;;3==5??5AA7BB8CC9HE<JF;KH9MJ9PK8OM8QL8OL;LJ>QPL^]bmlzzz’‚¡€‚¨€€¦{u—rkŠe_{YSmTLdMEZG@PB<HD?F@;?<87;63;60<8/=9.;8/:70991;;3>;2?=1@<1?;/A;-F?/H?.K?/M@/O?0O?/P>0L?/@=*?>,@=.?;/?;2>93=:5:94<94D95M51V-+j)-„28ž8C«>C¦7.¥7(¥:(ª?-¨?, 9&¡8#¤;&¥:&«<)³>,¹@/½>/Á=0Å=1Ä>2½?3¶=2²9.®8*¯9+°:,²:,³9*¶;,¹;-ÄB4ËE9È>3¾1'À2(Í9/Ö5+Ø4*Ú6-Û7-Þ7.á6,æ3,ì1*ó0,ô.+ô/)ï0(ä2&Û4$Ò6 Ð5×2Ú0Ú4$Ü<.Ö=/Ð9.Í=2ÒD:Ä5-Å60Ê;5ØD@âMIåKIÞC?Ö=8Ó@9ÎA8Ç>4¾</µ:+°8*¯9+±;-¬4&½?3ÑH@ÙHCÝB@å@DóFLýNUð?Eð?EöBMþHUþHWüBWú@XüA^ÿAcÿCfÿFmÿIrÿMyÿO|ÿL{ÿIyÿDvÿN~ÿR}þKrøMoÿVuÿUu÷MjðFcïKfÒ>VádtþŸ§ÓŽd92lJ>¥ynÐ¥œõÔËÿéãÿôòÿýÿÿüÿùóÿüïÿÿïÿÿðÿÿòÿÿôÿÿ÷ÿÿûýÿüûÿýúÿüøÿúøÿù÷ÿö÷ÿõöÿôøþó÷öëïìãäÚÕÒÉÆÁº¹´³´¬±´©°¶ª¶¾³°¸­¬´©10,10,0/+0/+10,21,43.54/77/880991;;3==3??5AA7DA8IE:LF:NH:PJ:RK9RM:SL:QK=OJDVTUfdqwx††¨¹“ÃÆŽ‹À‡ƒ¶{w©pmšid_[~UPnNJaKGXEBM?=B;7895296/85,85,671783891;;1></?<+B=*C<)G?,J?+K@,LA/M@0M?2L@4K>5L?6K>5F<3B92=82:946;47<59<5>:1H4-Y2-w78”?D¦>E§9<ª;0ª<+«@.¬A/©@-¥<)¦;'«>*§8$­:'µ<+¼=.Á<-Å;.Ê<0Ê=3Å?6¾>3º:/·7,·7,¸8+º8+º8+»7+ÇA5ÍC8Å8.Á1&Æ3)Ë7-Ï5+Û4+ß3)á4-â6,ã5,ç5+ì1*ð/*õ/,ö/*ò/)ì0'â2#Ù4!Ð5Ï4Ö1Ù/Ø2"Ø6)×9-Ô:0Ö?6ØE=ÖF>Í@7É<5Ë@9ÖKDÙNGÒE>Å;1Å=1Â<0À</º;,·9*´:+¶<-¹?0µ9-¿<2É@:ÒC?ÛCBãDHíJOöOVì?Eí>CòBLûIUÿJYýFXúCYûC]û@_ûBdÿElÿJsÿNzÿO|ÿLyþIvÿJwÿTÿT}ýJqùImÿStÿVtøNiëD^äF]êXkÿ–¢ÿµ¼óžŽHFk.)¦kcÍ–óÈÁÿãÞÿñðÿüýÿýÿü÷þþöÿÿöÿÿ÷ÿÿøÿÿúÿÿüÿÿýýÿýúÿüøÿûøÿùøÿø÷ÿöøÿõ÷ÿôøÿôøýñóóéêäÜÚÖÑÍÍÊÃÉÉ¿ÊÊÀÊÍÂÉÐȾȿ·Á¸65143.32.10+10+21,32-43.66.77/880::2<<2>>4@@6B@4JD8ME8OH8RK;TK:TM;SL<RKAQLIZW^li|~Œ¸–™Ì™žØšžÞ™˜Ú”‘ÔŒŠÉ†…¿~µxv§nk–fe‡]ZwVTiMKYDAJ><?;:8;74762555457664872<:.?<+C>*F@*G?*H@+IA,IB0IA4HB6HA9G@:K=<I;;C9:=77875384/83/917<574+C1']80~C=–GCž>?ž51§;/ª<+ª?-ª?-©>,©>*¬?+°?-«8%²9(¹:+¿9-Ã9,È:.Í:0Ï<4Ë>5Æ;4Â91Á8.Á8.Â9/Â8.Ã6,Å7-ÑC9Ð@7Ã0&Ã,#Ð7/Ö<4Ó2*à3,ä2(ç2+è3*ê3+í2+ð/*ó-*ö/*õ.)ñ0)ì1(á4$Ú5"Ñ6 Ð5Ø7#Ù3#Ö3$Õ3&Õ5)Ô8,Ô<1Ö?6ìYQáRJÓHAÌC;ÊE<ÌI?ÈE;À>1¼:*¼;(¼:*¹:)·:(¸:+¼>0¿A3ÁA6Á<3Å<6ÏA=×EEÝGHäIMëLPèBFê@CðCIùKTþNYüJZüF\üF_øA_ùBaýEiÿJpÿNwÿOzÿNxÿLvÿQ{ÿRyþOvýOtÿVyÿ]{ÿSoîD^úTlîQd÷dtÿ’žÿ‹•ôƒ‰Ç`d§MMµjgΊXÿÞÙÿïîÿúùÿüþýûÿýúÿþûÿÿûÿÿûÿÿüÿÿûýÿûûÿûøÿûøÿüùÿûúÿûúÿøúÿöøÿó÷ýñóùíïòææçÝÛßØÒÛ×ÎÝÛÏàÞÒßáÖÜãÛÏØÓÅÎÉ<94;8185052+41*41*52+63,74-85.96/;81=:1?<3A>5C?4JB7MC7PG8SJ;WK;UL=UK?SJCSJK]Wcnl‚‚‚¦‘•Åš ÚŸ¦ê¢©ñ¡¥ïž ë™›å•˜Ý”•Ö‘ʉ‰½‚ƒ±zy¡rq‘fc~XVkPN\IGRFCLCBJ??K==G;:@;9:<94?;/C=-E@,F@*FA+EB/EC4CC7BC;AC>@ACB<FD>LCANEEOCHNAIL>HI>GDCHAA=2L:.gF7ƒOAI<Ž?2”8)¥>/©>,«>*¬?+­@,¯@-°?-±<*±8'¶8)½9*Ã9,È8-Ì8.Ò91Ô;5Ï:4Í:3Ë81Ë81Ì92Í:2Ì70Ë6/Ó<5Õ>5Ò91Ì2(Î4*Ø;2Ú<3×3*ä1*é1'ì1*î2)ð1)ò/)ô-(ö,(÷-)ô/)ð1)é3(â5'Ù6%Ò7!Ï7 Ô9%Õ8%Ó6%Ñ4%Ò4(Ó7+Ò8.Ð7/ãNGèXPçZSÚQIÌG>ÄD9¿@7¹>/¹<*¹<&¹<(¹:'¸9(º;*À>0ÃA4ÉD;Æ@7É@:ÑFCÕIHÖHGØGJßIKæFHèBDíDIõLSüQZûO]ûL]ûK`öD^÷DaùFfýIlÿNuÿOxÿOxÿPw÷VxóUvôTvüYxÿ]{ÿZwûQkòI`ýWköUgêM^ö^mâKZæTaåTa×XaÁefȃ~嫧ÿÔÑÿëèÿóòÿøùÿþÿûüÿûüÿûüÿüüþüúýýùúý÷÷ü÷ôÿú÷ÿûøÿüûÿûúÿøøÿóóýîñúëîðáäéÝÝäÙÕâÙÒæßÕëçÛñíáññåìóìÛæâÎÙÕ@=6=:3:7074-52+52+52+63,74-74-96/;81=:1?<3@=4B>3JB7MC7RF8VJ:WK;XL<WK?TICSJM^Xfpmˆ‚„«‘—Ëœ¤ã£­õ§°ýª±ÿ¦¬ú¡§ó ¤îŸ£ê¡á™›Ö•–Ì’’ĉˆ´yyŸji‹`]|XUpRPhOMeNJcKG^FBS@=H?:>?:6@<1A>/C@/CB0BC3BD7?D=>D@<ED;BJ>CVCIaLRhU\o\br`dobbjd`afXWaJDlG>ƒSE•XF“J7‘@+™>+¨A.®@/®A-¯@-°A.´A/²=+°7&µ7(¼8+Â8+È8-Í6-Ñ7/Õ81Ø;4Ò72Ð72Ð72Ñ82Ô94Ô94Ó83Ó6/ÞA:Õ8/Ñ3*Ö8/Þ=5Þ=5Ú91Ü5,ç2)ë0'ï0(ñ0)ô/)÷-)÷,(÷,(÷-)ô/)î2)é4)á5'Ú7&Ô7$Ï8#Î7"Ï8%Î7$Ï6&Ð7)Ò9+Ñ7+Î4*Ë4+ãNGód\ë`YÕOFÄA7º;2µ9-¸>)¸>'¹<&¹;%º9&½9*À</Ä>2ÊD9ÊA9ÎE?ÕKHÓLIÎGDÍEEÖHGãIIæCDèEHòMSøSZùR\÷O^úOaôH^õG`øGdûIiÿNrÿQxÿRyÿTzðUuïZwù^}ý^|ùUpõMgùOiÿYnÿ[mÿctðM^÷TeôO`üUgõL_äR_À^_»vqל˜úÉÅÿåãÿíëÿôôÿÿýüÿÿûÿÿûÿÿûÿþýýýýüúþùöýøõÿøõÿùöÿúùÿúùÿööüððøéìôææëÝÝèÚÙäÙÓèÞÕðèÝùóåÿúìþþòôúöáëêÓÝÜB>5A=4@<3>:1<8/:6-84+73*62)62)73*84+;60>93A<6E>6I@7MC7RF8UI9WJ:XK;ZLA[NHTIMXR`gd|~§’Ê— ãžªö¥°ÿª³ÿ«²ÿ¬´ÿ¬²üª±ùª¯ó¨¬ì©«èŸ¡Ú™šÒÁ±tr¡he’]Z‡XR~XQzYQvVOnMH_C?M?:@?;:B?8>>4@B5BE:?D=<B>:CB>FH?LUDVnQfƒarŽisŽtvx„o€€^lŠ[c’WYžWU¦VM¦M?Ÿ@.œ9$ :$§<(¬=*®?,±@.³@-´?-¶=,¹;,º6'¾6(Ä6*Ë7-Ñ7-Ö8/Ú91Ú83×84Ø95Ù:6Ø93Ø61Ø61Ú83Ý:3æC<à=6Ú70ã@7þ[Rÿlcÿ]Tç?6å3)í2)ñ2*ô/)ô*&õ(%ø(&ø+(÷-+ó0,í2+ã1'Ú0#Ô1"Ô4$Ó:(Ì;&Ê<(Í<)Ï<*Ò:,Ò9+Ò8,Ñ7-Ú@8Ï81ÜGAôc^åXQËB<Å@;·4*·:&¶<$¹<&º<&½:(¿:+Ä<0Æ>2Ç>4ÌC;ÙPJßXRÔOJÃ?:À?:ÏHDÞHGâGEæJKïPTñRWïNVíKXñL\ùRføOf÷Ke÷JfüMlÿQsÿRvýRvôWvîXuõXuû[wÿ\xÿZsÿWnÿVkÿbuýVgÿ\mÿ[lñFXôDXÿQeîVeÆfgªieʋ網øÒÏÿëçýïîüø÷ùýüùÿÿùÿÿúÿþüþýÿþüÿûøþöôþùõÿû÷ÿùöÿöôÿööÿõõöèèæØØãÕÔÞÐÍáÖÐñçÞüôçþøèÿüéÿÿóúÿùèñðØáàC?6B>3A=4?;2=90;7.:6-95,73*73*73*84+:5/=82@;5D=5H?6LB6QE7TH8VI8WJ9YK>ZMEUJNWQ_eby{¤ŠÉ”⛦õ£®þ«´ÿ¬µÿ®¶ÿ¯·ÿ°·ÿ¯¶ü¯´ø¯²õ©ªë¢£ã˜˜ØŒŠÉ€»uq®ie c^˜`WŽ`Vˆ]TVPtMIbFCTBAIAAC<<:AB=FEACD?A?@DBEOMRWVdch†quš}¢‡}Ÿ’}œ {š¥pŠ¤`u¬Yi¶VaºQU¸HF°@4¬;)¬<&­='¨8$¨7%«8&®9(°8'²9(µ7(¸6(»3%Â4(Ê7-Ó:2Ù<3Þ=5ß<5ß<5á>9ß<7ß<7á>9åB=èC=çB<æA;å@:æB9ä@7Ü8/Õ1(Ø6+ëI>ÿ[Oñ@6î6,ê+#î)#ø-)ÿ/-þ,+ö((ú0.õ4/ï61ç6.Þ3)Ö2&Ô2%Ï6&Ë:'È;'Ê<(Í:(Ï9*Ð7)Ñ5)Ð4(Ó9/Ï5-Ô=6åPJåTQÛLHÍB?·1(¼;(»=&½<'¾;'¾9(À8(Â:,Ä:/È>4ËB8ÕOFÜWPÒRIÃD=¿C;ÌHCÛLHßJFåMLëSRíTWëPVéNVìOZ÷VføUhùSiúRküRmÿSqÿStÿRuòMmøUtÿ\zÿ_{ÿ\vÿUoûRiûReüUf÷RbÿZhÿZhóN^øScþYißS^¶`_—^W¶}Ù©¥îÈÅýáÞùééýøõúüùùÿÿøÿÿ÷ÿýúþýÿÿýÿûûÿ÷õýøôþùõþùõþöóÿ÷öÿõõöêêêÜÛäÖÓÝÐÊßÕÌïåÛüõåÿùçÿýéÿÿïúÿøèñîÛáßEA6EA5C?4A=2?;0=9.<8-;7,84+84+73*84+:5/=82?:4C<4G>5JB7ND8RF6TG6WG7YI<YJCWKKXP]b^ysuœ…ŠÂ™Ü˜¤ð «úª³þ­µþ¯·ÿ±¹ÿ³ºÿ´»ÿµ¹ÿµ¹ÿ¯²ù¬¬ô££ë™—àŽÓƒ€Çxu¼sm³k`¢i]›cZ‘]W…VRwNKhEDV@@L85<A<@KABL@BQ>BZEJpW]‚cs˜r—¥z§®«²{£¸vœÀp“ÂcƒÀTnÂG\ÐK\ÑHPÄ<<º5.¸9*µ<'°<%°;'°;)²:)´;*·<,»<-À<-Ä<.Ä6*Ë8.Ó<1Ù?5ßA8á>7à<3ß:4Ü71Ù4.Ø3-Þ93æA;ìE?éB<ä=5ã<4Û4,Ø4+Û7-Ô2'Ì, Ö6*éG:õK>ò@6ï4-ð-)ö,*ú,,ü,,ø*,ð*)ì/+è2.ã5.Ü4+Ø1(Õ1'Ñ3'Í7(Ë:)Í:(Ð:)Ò;*Ó:*Ô8+Ô8,Ñ4+Ö<4Õ:5Õ<7åONíYWÙEEÂ3/À;,¿<(À;*¿:)Á9)À8(À8*À8,Æ=3ÇA6ÏJAØUKÒRGÆG>ÀD:ÇG>ÖKDØICÝLIäSPåSTâPSâMSåNWòXdöZhý[pþZrüVnúRlûQlÿQpýGmÿPvÿZ}ÿ[{ÿVrýTkþWkÿ\mûYhøZhûamö`kí]gïfnìfmÉ\_ ]W{OF˜mfÁ˜”ݺ¶ðÔÑôàßÿõôùù÷ùÿýùÿÿ÷ÿýúþýÿÿÿÿýÿÿøùüùôüùòüùôýøôÿùöÿøöùîìïäâåÚÖÛÐÊÜÒÈîäØýöäÿûèÿýçÿÿíúýôêðìÞãßIE9HD8FB6D@4B>3@<1?;0>:/:6-95,95,95,:5/<71>93@;5E>6I@7LD7OF5RE4UE5WG8WI>XMKXNW^Zqpp–€†ºŒ–Ô•¡é©õ¨°ù©±ù­³ý°·ÿ±·ÿ²¹ÿ´¸ÿ´¸ÿ¯±ü«­ø¥§òŸžê—–⌋ׂÍ}yÅum¶pgªf`ž_ZYX„QQuGGcA>Q=5DH8BT=C`@EnCJ€OU–]d«fx¿kÉnšËo˜ÉiÈ`ƒÉWxÆIgÄ;UÆ2HÙAPàEMÑ;<Æ71Ä?0½@,±:$­6"­5$®5$³5&¶7(¼8+Ã;-È</Ó@6×@7Ú@6Ü?6Þ=5Þ93Ý6.Û4,Ó.(Ò-'Ó.(Ø3-á:4ã<4á81Ü5-Ü5-Ð,"Í)Ù7,âB6Ü?0Ð4%Ë.éF7óI<ýH?ü<7ô-*ñ#%õ')û/2ñ-.ë/.ä2.Þ3,Ú1*×0*Ö/)Ó0'Ó7*Ò9+Ö:+Ø<-Ú>/Û?0Ü@3Ü@3Ò6*æLBåJEÎ50ßGFóZ\ßIKÕA?È</Ä;+Ã:*Ã:*Â:*Â:*Á9+¾9*Â<0Â>2ÈF9ÏOBÎPDÆH<¿C7ÁC7ÎH?ÑF?ÕJEÛPKÝROÙMLÚKMÜKPëX`ó[güaqÿauúXmõOgõMgÿNkÿOuÿRyÿUwÿTqüTmý[pÿcuÿjxÿguúboõamídlãflÙhjÎghµfa^TeG<^U©ˆÍ«©åÇÅïÚÙÿóóùøöùýüøÿÿ÷ÿÿûÿÿÿþÿÿüÿÿùúþûöûûóûúõþûöÿýùÿûøþôòøíéêßÙÞÔËÝÓÉíæÖÿøåÿþèÿÿæÿÿëøúïêïèãæßNH:MG9LF8JD6HB6F@4D=3C<2?80>7/=6.=6.=60>71@93?:4C>8EA8KC8NE6QD3RE2VF6VH;ZMGVMR[Whlk‹~‚±Š“Î’Ÿãš¦î¦®÷©¯ù­°ý¯³ý°³ÿ°´þ°³ÿ¯³ý«®û¨«ø¤§ô ¢ïšœé“•âŠŒÙ†…уÉ{u»mk¬dež_a’Y[„RQsNHbWFY_CQmBLEM˜MT¬U]¼[bÊZhÙRpâRußTuÙPlÒJbÍBWÇ8JÅ.?Ô8EãCKæGKØ>>Í>6ÌF:ÃH6¶?+·>-¸=-º<-½>/Ã?0ÊB4ÐD7ÕE:ÙE9ÚB7Û>5Ù;2Ù6-Ù5,Ú3+×3*Ô1*×4-Ú7.Ý90ß80Þ7/Ý6.Ü5,Ö/&Ù5+×7+Ï2#Í1"Ò9)Õ>-Ô;)Ø<-æD7øJAýD?ù64õ*-ö*-÷/2÷67í55â30Ú1,×0*×/,Ù0-Ù0+ã81ä91â:1â;2à<0ß=0Ü?0Ú>1Ñ7+ïWLøaZÓ;6Ñ;:æPQÜFHæPQÏ<4Ê:/Æ8,Å9,Ä:-Ä<.Ã;-À;,¾:-¾<.ÃA3ÇH9ÇI;ÂD6¾@2¼>0ÉF<ÊE<ÎIBÕPI×RMÕNKÓKK×LOéZ`ð^hüfrÿhxü]qòPeóMeýNkÿVzÿTxÿRrøTmö]qûhxûjw÷erüetó_mì`kêkrßruÃjfªd\žla}fVXH9kXJ”|r½ œÞÂÁîÖÖþîïûõõûûûûÿÿùÿÿýþÿÿþÿÿûÿÿúýÿþùûþõûüöÿþùÿÿúÿþúÿûõÿ÷òñèáäÜÑáÙÌðé×ÿúäÿÿæÿþåÿÿê÷ùëíðçèéáQK=PJ<OI;MG9KE9IC7G@6G@6B;3A:2@91?80?82?82A:4@;5B?8EB9KE9MF6PE3RE2UF3UH7YLCUKLZScii…|«‰“È“žÞš¤ë¦¬ø©¬û¬­ý­°ÿ®±ÿ®±þ­°ý­°ý©¬ù§ª÷¤¨ò¢¦ðŸ£íšžè•™ã’”Þ’’Ú‰ŠÍ|€Àsx²ou©mpigŽj_}y_x‚WjŽP_£O\»S\ÍT]ÖPWÛHRå@Qè?RäCRÜDPÖCKÏ@DÊ9<É46äJLçIJáAAÕ74Î95ÎA8ÈD8¾>1ÃE6ÃE6ÅF7ÉE8ÎF8ÑG:×G<ÚG=Ö>3Õ;1Ö8/Õ4,Ö3*Ø4+Û4,Ú6-Û81Ý<4à=4ß<3Ý90Ü5-Ü5,Û7-Ò.$Ý;0Ú=.Í1"Æ-Î7&Ò=)Í:&Ì9'Ô8)â:/ó=9ÿ@@ÿ<>þ37ó,/ê,.á+*Ø*)Ô+(×/,Þ44ä88é99ï75ð74î73è71ã7-Ü6*×5(Ñ5&Í5(çPEÿmcÛHAÈ42×CCÓ>BêVVÙA<Ò>4Ë8.Æ6+Æ:-Æ</Ã>/Á=.»9+»<-¾?0ÀA2¿A2½@.¼=.»<-ÃC6ÄD9ÈH?ÐPGÓRLÐOIÒNLÕONé^cîagûitÿnzÿduõVjõQiÿUoÿWvÿVtúXpñ^pônyöw€íksÞXañ`mñ`mìboïs}숊Ì|ªth™yjskXON:_VGƒqg¯–’ÜÀ¿ïÕØüéëþôõýûüýþÿúþÿþýÿÿýÿÿûÿÿùþÿÿûùÿõúýöÿÿúÿÿúÿÿøÿý÷ÿþöùðçíåÚéáÔôïÜÿûåÿþåÿÿãÿÿèùúêòôçîðåSM=RL<QK=OI;MG9KE7JD8IC7E>4D=3B;3A:2@93A:4A96@;7A@;CC;IE9MG7OG2RF0UF1UH5WK?RHFXR^jg‚}§‰“Ä’žÚ˜¢ç¤§ö¦§ú«©ü«¬þ¬­ÿ­®þ¬®û¬®û¨¬ö¦ªô¤¨ñ£§ð¡¨î §í¤è›¢æ™žâ’šÛ‹“҆ʃ„ˆ·ƒ¨ˆyšmˆšcz©Xi»Q_ÏMYàIRèAIê=Aé=;ã>8ÝC9ÖH:ÎI8ÅF3ÂC0Å@1äTIàG?Ú;7Ø64Ø88Ø:;Ñ98È74À:/¾<.¿;.À:.Â8+Ã7*Æ4'Æ2&Î4*Ð3*Ñ3(Ô3)Ö3*Ù5,Ý6.Ü8/Þ=3Û=2Ü;1Û8/Û7-Ú6,Ø4*×3)Õ3&Ó3%Ð4%Ï6&Ò=)Ñ>*Ê9$À2È:&Ê4%Õ1'ç51ú<<ÿ=@ÿ7=÷37ê-1á-.Ù--×/.Ü43ã9:é;=ð9=ö26ø03ô01í1/ä1*Þ2(Õ1%Ï3$É1$Ñ=1új_äTLÈ95Ð@?Ë:=äRSäJHÜC=Ñ:3É6.Æ8.Å;0Â<0¿=/º;*¼?-½@.¼?-º;*º;(½;+¾<,¼>0¼>2ÀD8ÈLBÌPHÊNFÌLIÐNLèaeìaføhrÿo{ÿhx÷Zk÷UjþZrÿYsû]tîaræjtë}€ñ‡‰èwyÚ_dñgtþm|óeuëlwö‘•ì¢ŸÄ”Š¢|hjUHQ<WUFth\¤ŒˆÛ¿¾òÕÙùãæÿóöÿúüþþÿüýÿþûÿÿüÿÿúÿÿøýÿÿûùÿõ÷ýóýÿ÷ÿÿøÿýöÿý÷ÿÿöÿùïõïáòìÜúõáÿýçÿýäÿþâÿÿçýþìùúì÷÷ëVO?TN>SK>PJ<OG:LF8LD7JD8H@5F@4E<3B;1B92A:2C:3A<6C@9DD:JF:MG7OG2QF0TH2UH5UI=QGEWQ]jh€~‚¨Œ”Å“Ø™¢çŸ ò£ ÷¦£ú§§ý¨ªÿ©«þ©¬ý§­û¥«õ£ªò£§î¢§ë¤©í¦©î¥¨í£§î˜ è”¡å”Ÿß”Ÿ×•›Íš•¾ Œ¯ªƒ ¥e´]pÂQcÐIWÜBNå>Fë<Aì<<ê?8ãA4ÜE4ÔI4ÊI3ÂF.¿B,Â?-âSEÞD<Ý97æ>>êDFå@DÙ8=Î65ÍB;ÈD8ÉC8ÇA5Æ>2Ã9,Â6)Â2'Æ2(Ë3(Î4*Ó5,Ø5.Ù4.Û4.Û4,äB7Ý=1Ù7,Ú6,ß9-ß9-Û5)Õ1%Ø8(Í2 Ê1!Ï9(Î8'Ç4"Ê7%ÔC0È7&Î8*×6,Þ5.æ3/î53ö:9û?>ÿLKúHFòBBì>=ê<=ç7:å26æ,1ô+1ø)/ô,.ï/.è1+à4*Ù5)Ó7(Æ0!À.ôdYë]SÏ@:ÔDCË;;ßMMêRQàHEÓ;6É6/Æ8.Æ9/Ã;-¾:+¾<,Á@-ÀA.»<)·8%¸9&½<)¾?.µ7)³7+¸>1ÁG:ÅK@ÅIAÈIBËJEåa_æ^`ñdjþnwþkuó]iðXeø]mú^sõbtèdoãlrì~ù‹ö†…éqsøryÿy†õaqàXföˆ‘ÿ´´Ð®¢ ›‡]jPDR9QT?jbU›ˆÚÀ¿òØÙöàãÿô÷ÿúýÿþÿýüÿþüÿÿýÿÿûÿþùýûÿúôÿòòþòùÿôüÿöûýòÿýôÿÿôÿÿóûõç÷ñáþùåÿýçþüãþýáÿÿêÿÿñýýóüüòXO@WP@WN?UN>TK<RK;RI:PH;MC7KC6KA5H@3H>2G?2H>2F@4GC:GE9JG8NI6RJ5UJ4UJ4VK9XNDULMZTbkhƒ{~§‰Ã‘™×˜Ÿç£¤ö¥¢ù¢¢ü¡¢ü ¤ÿ¢¨ÿ£«ÿ¤­ü¡ªõ¡©ñ¡¨ì¥¨íª©ï­©ó±©ö¬©ø ©ø—§ò’¡â–ŸÖ¦£Î¶ž¾¹…œ¸gz¿L]ÑERß>Mç;Gç<Dæ=Bà@@à@@â>?ß<=ÝEBÉ:4ÊD9ÊF:º1)ÛJEèJIéCEë>Bè;?æ<?á=>Ú<=Ô<;ÏB;È?7ÊD;ÑMAÍK>¿=0¸6)½9,¿7+Ã6,È5-Ë2*Ñ/*Ô/+Û2/Þ5.çA5ß9+Ù1$Ý3&å9+ç;-ß8&Õ2Ï2Ì4Ê5!Ç4"Ç4"Ê4%Í5'Ï5)Ê0&Ï2)Ñ5)Ò6*Ò4(Õ3&×3'Ü4)à2)æ3.ë52ë33é/2ç-2è-4í.5ö-3ö+/ï+,ì/-ê5.ã9,Ù7(Ð4%Ã-Ä3"çXHéYNË;3ßNIÅ41ÚHHáOPÜJJÔC@Ì;6È80Æ8,Ç9+È<+Å<*Â;(¾9&¼;(»<)º=)º=+·<,´8,µ;0¹=1»?3»?3¾@4À@5ÄA7ïjaõpiêc_ômjûqqìadôgmÿpyÿlzûhxðdoébiëdjîlnîqoîqoûy{ÿ}…ùZlücwßbpû¬¯¶ªš}‘u]rQJY:LR8snZ¥•ˆË·°íÕÓÿîðÿö÷ÿ÷ûýøüüüþþÿÿþÿÿÿþÿûÿþðÿôåþéæûêïÿîøÿöüÿôþÿóÿþñÿýñÿûîÿúêÿùæÿùãÿùáÿùáüúåÿÿõÿÿûÿÿûYPAXO@XO@VM>UL=TK<SJ;RI:NE6MD5LC4KB3JA2JA2JA2IA4GC8HD9LF8OH6RJ5SK4VK7TK:XNEULMYUckhƒ{}¦†ŒÀ•Ó–šáŸžìŸñžžôžŸù¢ýŸ¦ÿ ªÿ¡¬û ¬ô «íŸ§è¢¥è¥¤ê©£í« î¤Ÿï §÷š¦ðœ¡á£œÐ±˜À¿ªÂq„ÄTbÑCOâ<Fì8Aï7?ê:=å<?Ü@AÛACà<CÚ9?ÚADË;;ÌB?Ê@=Á31åOPæCFè?Dê;Bç8?â8;Û89Õ:8Ï;7ÔE?Æ=5Å<4ÌG>ÌLAÁC5¸:,·9+¹7*¿7+Æ6-Ë4-Ñ2.Ø3/á53ä84ä>2á;-Þ6)ß5&á5'á5'Ú5"Ó4Ñ9$Ë9"Ç9%Ç:&É<+Ë<,Ì8,Î4*Õ3.Ö5-Ó7+Ï7)É6&Ç4"Ë4!Ï4"Ø2$Þ2&ã0+æ.,æ,-æ,/æ-2è.3ì+.í,-ì0.è2.à4*Û5'Õ8'Ò9'Õ?.»*ÖD5Ð>1ÙF>ëVPÚB?ØBAçUVâRRÜKHÔC>Í=5É9.È9+Ç8(È;*Ä;(¿:'½<)½>+º?-¹@/¶>.²:,´;0·=0¸>1º?0½?1ÁB3ÅC5ëi\òmdçb[ðkfõolèbañklütxÿq~ÿo|öirí]fêY`ñbf÷qpþzxûvwÿx~ùRdþ[pä]nö¦©©¦“jŒkZtON_;SZ;us\©ÖĸöáÜÿõñÿúúÿúûÿûüüüüúþýûÿþúþÿõÿúíÿóáÿéáýæéÿëóÿñøÿòýÿòÿÿñÿýïÿýíÿúéÿöåûóàúòÝüôßü÷äýúóüüúÿÿý\PB[OA[OAYM?XL>WK=VJ<VJ<SG9RF8QE7OC5OC5OC5OC5MC7JC9JF;MG9PI7SK6VK5VK7TK:WMCVMNZWbkiy{¡…‰¹‹‘Ë‘–Ö˜˜Þ™™ã˜™éšî› ô£÷Ÿ§ø ©ô ªï¨èœ¤ãœ¢àžŸàžá™ã›™â–›ßššÚ¤•Ð±ŒÀ¿‚¬Çr“ÉYqÇBS×ALß>Dã=?å==å<?ã=?á>Aá>Cà<C×8=Õ?AÏA@ÊC?Ã<8Ä96éWWàBCä>Bè;?ã9<Ü68Ö66Ñ96Ì;6ÕHAÄ;1¾5+ÅA5ÍK>ÆH:»=/·8)º6)¿7)Ç7,Ï6.×50Þ71æ95é=9×3*×5*Ú6*Ü6*Ý5*Ý7)Ú:*×>,Å4!¿4¹4!¹6"¼8)½8)½3(À-%ã@;å>8Þ=3Õ<.Ì9)È7$È7"Ì7#Ò7%Õ3$×/&Ù.'Û.*Ü.-Û//Ü./Û,)ß0+à5-Ü5,Õ3&Ð4%Ð9&Ñ>*ÔA/Â1 çSEÒ;0ÚA;ÞC?èJIðTUíUTçSQãOMÝJCÖC;Ï<2Ê8+Å6&Ä7&À7%¼7&¹:'¹<*·>-µ=,²=,°:,±;/´<.µ=/·=.¹?0¾C4ÃE7Ü\Qêg]äaYðlgöpmça`ìfgójqàR^ï^kõhqòemñ`gôeiùqqþxwútuÿnuóJ]øRhä[mñœ¡£œŠa€`UoHOd=W`Aww]­¥’áÑÂÿîæÿøñÿüøÿþúþÿúûÿüûÿýûÿýùÿûóÿöèÿîÜþãÚøÞáùáèûåïüèöýëýþìýúéÿúêÿ÷æúñàôèØòçÕöëÙøñáþúñýüøÿþû\PB\PB[OAZN@YM?YM?XL>XL>UI;TH:SG9RF8RF8RF8RF8PF:LE;KG<OI;RK9TL7WL6WL8WK;VLBUMK\V`jh~yy›‚…²ŠÂ”Γ•Ò•”Ö••Û–˜ã˜›è˜žìš¢í›£ëœ¥è˜¢ß–ŸÚ•›Õ”˜Õ“—Ö’“Ö‘Ò…„½‘ƒ¶¥}±·s¢ÈfÒVzÕGaÔ<KÞAJàBCÞCAàB?áAAä?Cç>Eç=Fã?FÖ:>Ñ?@ÑEDÆA<º61Å>:êZYÝABã@Cæ=@ã:=Û89Ó97Ð<8ÎA8ÏE;Â:.»2(À</ÇE7ÄE6½>/¸9(½8)Â9)Ë8.Ò:/Ù80à91è;7ë>8Ø1+Ù2*Ü3,Ý5,à5-ß7.Û9.Ö=/É7(Æ:)Á<+¾<,¿;.Á;0Å<6Î95ëC@í@<ä@7Ù=1Ï9*Ç9%È:&É;%Ï<(Ð7'Ï1%Ñ1%Ô1(Ö3,Ô1,Ñ/*Ó2*Ó5,Ó7+Ð7)Í5'Ê7%Ì;(Ì>*Ì;(Ï<,ô^PàF<Ò3/Ò.,æ@@ûWXÿusÿroÿjgö^YçPGÕA7Ç5(½. Ä8'À8(»8&¸;)·<,µ=-²=,°<-¯;.­<.°</°<-±;-¶>.¼B3ÀF7ËMAß`Wâa[ôpløtræ`aä]aæ]dÖHTçYeógrôgoöelükpþsvþvvûpsûenñDXôKbåXkë’– •ƒ_zYQlCSh?\gEy|_°ª”çÜÊÿõèÿùðÿûñþþôýÿ÷ûÿúùÿúöÿøòüóêûëÚøÞÎóÒÊëÌÏìÎ×îÒàðÖéóÛò÷áöõáûöãüõãõìÛíáÑéÝÍíáÑñèÙüõíü÷ñþùó\PB\PB[OA[OAZN@YM?YM?YM?WK=VJ<UI;UI;TH:UI;UI;SI=OG<OI=QI<SL:UM8XM7YL9XL<UKAUKI\U]hexut“|~¥…‡·ŒÂŽÃȒϔԒ•Ü“—à”˜ß•šÞ•Ü’›ÖŽ”ΉDž‹Å‡Ã}ƒÁ~¸}tŸŽp”¨gÁ\„ÖNvâAcè:Uê9KæAGàECÜGAÛGCßEEæAGî<Jí=JæBI×>@Ð@?ÐIE¿>8³2,ÈC<àUPÜDCáACã?@á>?Ú?=Ô@<ÑD;ÏF<Ç=2À8*½5)½9*¿=-¿=-½;+½<)Â:*Ç;*Ï;/Õ;/Û8/à8/ç83é:5æ95ä52ä20æ21ç32æ40Ý2+Ô0'Ë1'Ë7-È:0Ä7.Á4-Ã40Ì:;Ý?@ë8;í76ä71Ø5,Ì4&Æ5"Ä7#Å:%Ë=)Ë8&Ì4&Î5'Ô8,Ô:0Ó9/Ð8-Ð>1Ê;-Å8'Ä7%Æ9'Ç<)Ç<)Ç:(Í>-Í:*Ø>2áC:Ô/-ä::à24ß56×53Ö;6ÞC>åKCêQIêSHèTHäUGË?0Ä?.¾<,¸=-·>-´?.°?/®>0¬<.¬<.¬<.¬=,­<,±=.·A3ºD6ÆLAÞbXâc]ðnlôrrå`cå`eç`g÷kvûoz÷kví`hð_fýlqÿx{ÿy|úmsø^jóAWôC]èUh懣“ƒg]YtI^uIhwPˆf³²–èâÌÿúéÿýíÿþïýÿòûÿôøÿôôÿñíûêäòáØî×Åèǻ伺޺¿Þ¼ÇàÀÍâÃØæÌãëÓìîØôñÞ÷òßóêÙìàÒèÚÍêÜÏïáÖòèßñèáòéâ[OA[OA[OAZN@ZN@YM?YM?YM?XL>XL>WK=VJ<VJ<WK=WK=XL>QI>QI>SK>UL;XM9XM7YL9XM;WK?ULGYSWe^nnjƒut–~}¥„…±†‰´‡‰¹Š‹Á‹ÈŽÏŽŽÔÕӔӋʃˆÀ|¸u|²ov­io«ljœ€lˆ“cy®YvËPoãBdð7Vö4L÷8Hï@EåFBÝJCÛJEáGGèCJò=Nð>LäCI×CCÌA>ÎIDº;4°4,ÉI@ÕLFØBAÚ?=Û;;Ú<;Ø@=ÔE?ÍG<ËG:À8*Á8(À8*¾9(¼9'»8&½:(À;(Ä;)Ê=,Ò<.×;.Ü8.á6.ä6/è50é32è./é,0î02õ47õ77î45æ21Ú.*×4/Õ62Ñ32Ï/1Ò/4Û6=ê:Dí06î02æ3/Ü3,Ñ5)Ê7'È9(É<(Æ8$É6$Î6(Ó:,Ú>2ÝA5ÝA5ØA6ÓG8ÉA1Â;(À9&Ã<)Æ=*Å<*Å8&Ì:+Í7)Í0'æC<Û2/ï?Aã/2Ú*,Õ1/Ò5.Õ81Ö90Ò8.Ì5*Æ2&À1#ÎE5ÆA2¼=.µ:*±9)¯9+«:,ª:,«;/«=0¬>/¬<.«<+­<,²>/µA2ÉSGàg^àc_êjiînoæchðjqõoxúq{ýt~ömuîbködnÿrzÿx~ÿquúipøWfùAYô@[êQfá~ƒ¬—†ykmˆ]o‰ZyŠ`—r¶·˜ßÞÂùôÞüúåÿÿïûÿïõÿïïÿëçúäÜïÙÐãÍÅÞÁ°Ö­¨Ó¦ªÐ§°Ó«¶Ô®¼Ö±ÇÚºÒßÁàæÌèêÔòíÚòéØîâÔìÛÑêÙÏìÛÓéÚÓæÙÑåØÐ[N>[N>[N>[N>ZM=ZM=ZM=ZM=YL<YL<XK;XK;XK;YL<YL<YM?SI?TJ>UL=XL<YN:ZM:ZM:YN<YM?ULEXOR_Wbd_sjfsoyy|~¤~€©‚²ƒ„¼††Æˆ‡Ë‰ˆÌ‰ˆÊˆˆÈƒƒ¿|}µvw­pq§kl¤fg l`Ž‚`q•V^³N^ÒJ^ì@X÷6Kþ3Fþ9Có?BéD@áHBÞICâFIéBJò=Nï>NÝAEÔFDÈA=ÉHB´:/±7,ÎOFÉB<Î=8Ð64Ï10Ð51Ó>8ÏE;ÈF9ÃD5¾9(Ã:(Â:*¿:'½8%¾9&¿:'Á:&Ä9&Ë:)Ó:,Ù9+Ý7+á5+ä3+ê3-ë*+ñ*-ø-3ü/4ÿ17ÿ37ÿ38ý58ù8;õ8<ò9>ò9Aô9D÷7Fû6Hþ5Eô+5ô-2í12ã4/Ù6-Ð8*Î;+Î=,Ë5&Ñ8*Ù;/ß=2â>4ä=4å>5ßA6ÙJ<ÏG7ÉA1Ä<,Â9)Â9'Æ8*È9)É2'Ó9/á@8ëD>Þ0/à..ë46è66Ø3-Õ7.×90×:1Õ;1Ò;0Í:0É;/ÖL?ÌF:¿@1´9*°6)¬6(«7*©8*¬;-­=/®>0­=/«<+«<+­<.±=0ÀKAÚdZÞc^èkiðosêinõrzüvîfp÷oyùryöjsþlvÿwÿv}ùhoùcn÷RbþB[÷<YêKaÝt{µš‰žz}˜k{—f‚•h‘u¬²ŽËÌ­ààÄéìÑîöÞåóÙÙíÑÏæÉÅÞÀ¼Õ·´Ë®ªÈ¤šÁ”–™Ó¡È™§Ë¬Ë¡µÎ§ÁÒ°ÎÙ»ÙÝÄåãÎìåÕïáÖëÚÒçÒÍãÐÊàÏÈÚÌÃÕǾ[N>ZM=ZM=ZM=ZM=ZM=YL<YL<ZM=YL<YL<XK;XK;YL<ZM=ZL?VJ>VJ>XL>YN<ZM<ZM:ZM:ZM<[OAWMDWML\RZ^Xfb]qjfspwv–xxœ|{§~µ„¿…‚ň‚ȉ„Ç‚}¿y·zu­xq§vo¥tk¢pg za‹„QZ™HE³EHÓHOîBNø:Fþ6Aþ9@õ==îA=æD?ãEBäCHèAKî=Më?MØ?BÑGDÅA<ÃG?²9.³:/ÑUKÃ>5É83Ç/,Å*&È/*Í:3ÌC9ÃC6¼>/À;*Ä;)Ã<)Á:'Â;(Ã<)Â;'Ã8%Ä5$Ê7%Ô8)Ù7*Ý5*á3*å3)ê2*õ33ÿ58ÿ8<ÿ4:ÿ-2ÿ(-ÿ(-þ+1ü-3ö)0ò&1÷)6ÿ,?ÿ*Aü 9ó/ö"0ó(.í,1æ0/Ú1,Ô3+Ñ5)Ð6*Ô6+Ý90æ=6ë>7ë:4è71è50â92ßH=×K<ÓE7Ë=/Ä6(Â3%È6)Ï8-Î4*Ô6-æC<ß82à21Ò ì89å63Õ1(Ò4)Ò4)Ï5)Î6+Ê6*È5+Å7+ãYNØRFÊH;¾@2·;/µ;.µ<1µ<1¯9-±;/±=0±=.¯;,­9*­9,®:-±<2ÐYQÜb]ìppôvyîmrõq|ör}ÿzƒÿ‰ÿy€ôhq÷epÿq|ÿuÿktø^jöOaÿA^÷8WéG^Ûntº‹™©„‚žn~šg—hœq¡©‚´º–ÈÍ­ÕܽÌÙ»ÁÔ´²Ì©§ÂŸž¼˜š¶–²Œ±†‰³†µ~¹„•À‹œÂŸÄ‘¨Å™²È¡¾ÎªÉÒµÚÙÄæßÍêÞÒéÖÏáÌÉÛÆÃÔÁ»Î½µÈ·°ZM=ZM=ZM=ZM=ZM=ZM=ZM=ZM=XK;XK;XK;YL<YL<ZM=ZM=ZM=ZL?ZL?ZM=[N=]M=^O<\O<\O>YM=WMCWLHXNOZPX]Ub`Yia\rjfokŽsp›vq§{u³ƒ~ÁŠƒÉŠƒÇˆÀŠ~¼ˆy²ƒr¨†r§t«‡l£†\„HL¤B9¶A:ÍC@Þ@?è:;ô<>ÿDE÷><ó=<ì>=ê@AëBIêCMëANãALÐ>>Å@9¾?6·>3°</²<0ÀD8ÒMDÇ61Ô;6Í2.Ì3.ÖC;Ç?3µ6'»>,º7%Á:'Ã<)Â;(À9%Â9&Å:'Ç:(Ë:'Ð9(Ö6(Ø2$Ú."à/%ì7.õ=5õ82÷40÷0-ø**û&(û%'û%'û%'û&*÷!)ø".ÿ'7ÿ(>ÿ!;ÿ6ý1ÿ'8ï#,æ#+ã+-Ý--Ô+(Ò-)Ü41â62è64î66ò65ò12ï./î,,å/,Ü92Ñ:1Ò91ìSK»"Ä+#ßF>È.&Í.(Þ<7èE@â:7Ø/*Ú.*â51å<7ß>4Õ<.Ï5)Ë3&Ì5*Í;.Ë;0Å8.ìbXàZOÈC:¹6,º:/¸8-±3'´6*°0%°2&®2&¯4%°6'²:*´<.³=1°:0ºC;Ö\Wìppñsvöx|ûyƒõq|ÿ|„ýw€út}üs{ÿr~ÿr}ÿmxÿgsü`nôI\ÿ>\ÿ>[äAVÔek´•€’¡zƒm}™f~”c‡—j”Ÿwž¦ ¨ƒœ¨„’£Š£|€Ÿvzšqyšo|r vƒ¤u‚­wƒ³y‹¸‘¼„“¾†—¾‡œ¾Œ¢½§¼“¯½š¾Â§Ï̹ßÓÇãÒÊÜÇÄÔ¿¼Ìº¶Á²«¹ª¥ZM=ZM=ZM=ZM=ZM=ZM=ZM=ZM=XK;XK;XK;YL<YL<ZM=ZM=ZM=ZL?ZM=\L=]M=]N;^O<\O<\O>]P@[OC[NFYNJZPQ\SX_U^_Wfd\tibƒng‘qjxp¬€y¼‡€ÄŠÄ€Á~ºt«Œn¢—q¤¡u¨šjž™W{=Aª5+±5+Â<3Ó@8Ý>8è?:óA=õ=;õ;<ô;@ñ>Bî@IèAIâ@KÚAFÇ<9½=4¶<1±;/«:,®:+½A5ÐJAÚG@ÜA=Ô63Ð51Õ@9ÍC8»9+²5#½8'Â;(Ä=*Ä=*Ä;(Æ;(Ç:(Ê9(Ï9(Ï6&Ô2%Ú2%ã5*ê8.ï80ð91ç1&ç/%ê.%ï,&ö+'û+)þ,+ÿ-.ü*-ú%+ú$.ÿ'7ÿ$;ÿ7ÿ3ü0õ0ö.9õ3<å*1Û(,Ü.0Þ02Û+.ñ:>ô7=÷4:ö26ö/4ø03ù25ô87Û2-Ú;5×82äE?äE?Î/+Ñ2.Á"Ô51á?:èE@à=8Ø3-Ù2,Ý60Ý:1Ô:.Ï9*É5'Ç5(È9+Ê<0Ê<2Æ9/ícYáXNÉ@8º4+Â91Ã:0¿6,Â9/¿5+¾4*½4*»5)¸6)µ5(´6(°6)´;0»B9ÓYTèljïqtöx|þ|„øv€þ|„üyùv~þuÿsÿp}ÿkwüdqý_nöI]ÿ>\ÿ>\éCYÓeh®Žw‰˜oz•bu’\xŽ]cŠ•k˜p‹•p…”mp‡]l‰]h‰\g^k_q•ey›izŸk¬t„±vŠ·|»}¼“¼‚–½†›»‰›µ†¡³‹­¶—¾½¨ÏÆ·ØÇ¿ÖÁ¼Ï¼¸Æ·´º¬©²¤£\L<\L<\L<\L<\L<\L<\L<\L<ZJ:ZJ:ZJ:[K;[K;\L<\L<\L<\L=\L=]K=]M=]N;^O<^O<\O<_RA]QA\PD[NFZOK[PN]QS]RZ_SgcXvh]…kb‘sk¤vµ†}¾‰}½—…Á™€·›v©žpž®u¢ºv¥µi˜±Rt¸<D¾5-º4+À<0ËC7ÓE9ÛC8ä?9ñ=<÷:>ù:Aö=Eï@GæAHÙ@EÏ@BÀ;6¸90±9+­9*©8(­9*»?3ÏF<ÙD>×96Ù74Ú;7ÜC=ÝOEÎH<µ3#¾9(Á:'Ä;)Æ=+É=,È<+É:)Ì9)Ï7)Ï2#Ó/#ß4*î=3ô?6ñ91é4+à4&ß5&â2%å/$ë,$î+%ñ+(ò+(ó+-ð(+ò&/ø(6þ%:ý7ü3ø3ô%7ò0;ó5Aò8Cð;Dð<Eé5>ä,6ð2<ó/;ö.9ö-7õ.3õ.1ó/1í42Ü0,â=9Ú64ß;9ÿmjåCAÑ/-Î/,Ù:7ß@<àA;Û<6Õ7.Ö5-Ö5+Ò6*È6'Ã6%Â6'Ä8)Å;.Æ<1Æ<1Å;1ë`YáTMÉ<3¿0(Ë;3Ñ>6Î;3Ò=6Ø=8×<7Ô=6Ï<2É;1Ã9.¼6*¶6)¹;/¼@6ÏTMãgeíorøz~ÿˆû|ƒû|ƒûyûx€ÿwÿsÿn}ÿgvû`pü[mõG^ÿ<Zÿ>\ìFZÎ`až~ev…Zi„Qh…OmƒRt…X{ˆ]}ˆ^x„\q‚X`zM_Pa…UeYn•`uœg} j~£mƒ¬r„²tˆ¶x‹¹y‹¹y¹z‘º~”º“±•¬€›©†©­’ºµ¢Ç¹®Ë¸²È¶´¸¬¬ª¡¢ —š[K;[K;[K;[K;[K;[K;[K;[K;ZJ:ZJ:ZJ:[K;[K;\L<\L<\L<]K=]K=]L<^M=^M;^O<^O<^O<^Q@^Q@]OB[OC\NE[NF\OI]OO^P_bTmdY{i^‰pgœ|t­…{·ˆ{³™…º¥…´¯€ª¸z¡ÆxŸÑuœÌcŒÈMlÜFRßA@ÕA=ÏE;ÌH;ÍG;ÓE9ÞA:î@Aõ<Aú=Dø?GðAHâAFÒ@AÆ?<¿?6¶<1¯9+«:*ª9)®8*¼>0ÎD:Õ<7Õ31à;9åA?áD?èSLßUJ¿:+Á9)¾7$À7%Ä;)É=,Ê=,Ë9*Î8)Í4&Ò2&Ù2)å9/ñ>7õ>6ð5.ä/&à6)Þ6)à4*ã1'ç.)ê-)ì*(ì**ê),è',ë'1ò)9ö&<÷!;û=û%Aû6Hä(6ç.<ÿP\ÿ`lÿP\ò<Ió9Gè)8í*8ñ-9ô0:ñ27ê01á+*×(%Ø0-Ú72Ô2/åC@ÿspõVSÝ>;åGDÛ=:Ú<9Õ:5Ò80Ñ7-Ð6*Í4&Æ3#¾5#¹6$»8&¿;,À</¿9-À:/Å<2åXQÜMGÊ70Ç0)×<7ÞA:Ú;5ß<5ç>9é=9æ?9á@8Ù?5Ð<2È:.Á9-»9,»=1ËNHàc_ìnoú|ÿ„ˆý~…øy€øy€üyÿx‚ÿt‚ÿn}ýetù^n÷VhóE\ý8Vÿ<ZîH\ÇYZŠlRcrG\tB\yCdzIl}PtVvƒXrXoWm‡ZlŽ\o•buh{¤l¨o„ªq…«p…­q…±tˆ´u‰·v‰·v‰·v‹¸w¹z‘´|«{’¤|›¤…­«–¾²¤È·¯Ç¸µ´«®¡ž¥—’™[K;[K;[K;[K;[K;[K;[K;[K;ZJ:ZJ:ZJ:[K;[K;\L<\L<\L<]L<^K<^K<^M=^M;_N<^O<^O<^O<^N>^N>\O?^NA]OB^PE^OJbNYbQdcTqdZ}le‘xr¢w¨ƒv¤”}§¬†«Â‰©Î‚žÚw”ál‰ÛXwÚD_óBTúDPïJPáIHÐE>ËE<ÐF<ÙD>èBBð@Cõ@GõBHíDGßCDÏA=Â?7¿@7µ=/®:+«:*ª9)®8*»;.Ì?6áFAâ=;îBBëAAÞ;6ãJBæXLÕK>È@2À8(¼3#Á8(È<-É:*Ë7)Ð8+Î0%×3)à8/é;4í:5í60ë0+å,'ã0,ã1-ç10ì31ó25ö37÷48÷6;ì-5ê,6í.=ñ0Có-Fö)Gý+Nÿ3Rô9Lå3?ð@Mÿ_lÿlzÿWfõCSõ>Pî3Dí3Aí3>é6<ã99Û83Î4*Ç/$Ë2*Ç.(Ð72òYTö\ZòXVÛA?âHFÜB@Ô<7Î70Í6-Ï8-Ï9+Ê7'Â5#¹8#³9$µ:(¸=-¹;,¶6)¼7.Æ=5ÛNGÙEAÎ50Ð1-á>9ç@:ã81ç51ë20î21ë52ç83á:2Ø:/Ð9.È:.¾6*º8+ÈIBÜ_Yënlû}€ÿ…‰ýƒõv}öw~üyÿx‚ÿsƒÿk}ýdvù^pôSeôF]ý8Vþ=ZïL]ÀTRz_BWh<Yq?[vCfyKn~QtVx…Zyˆ_wŒay•exšhz m|§qªr„«r…«p†©o‡­rˆ°t‰³tˆµt‡µt…·r‰¸t‹¸w¶{Ž¬xŽ£x™¤‚­­•À¹§ÎÀµÒÄÁ¼¹À¨ª¶œœ¨ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:ZJ:[K;[K;\L<\L<]L<^K<^K<^K<_L=^M;_N<^O<^O<]N;]N;^O<_O?_O?`P@aQAbPFbNPcN]_Pe`Usga…to—{t}q—v–®ƒŸÌ‹¡Û‚–ær…ëduéPdé;Rù3Lÿ<QûERéDKÕ?@Î@>ÑC?ÖEBãEFèBDîAEíCFçDEÚEAËB:¿?4»?3±;-©8(¨9(ª9)°8*¼:-Ê;3èIEé??ë=>å78Ú2/ÞA:îZPôh[ÚPCÉA3¾5%Â6'Ç9+È9+Ë7+Ò8,Ñ0&Ú6-æ;4é;4ê40è/,ê-+ë--ó49õ3;ø3=ü3=ÿ1>þ0=ý1=ù1>õ0Aò1Bô4Kõ5Nõ0Nö-Oÿ1Xÿ>_æ3HùO\ÿanÿ_nÿWfûP`ôDXé8JóAQê:GÝ2:Ò/2Ê2-Â8-½;+¼:*Ä:/À2(ÙJBúkcÝLGáPKÏ;9Ï;9àLJÔ@<Ê70Ê7-Î</Í<+Ë:)Ä;(´;&­:%¯<)³>-±9)¯3'º:/ÊD;ÖGAØC=Ô72×2.ç;7ì:6è2.í2-ò,-ô,,ò./ï31è71â:1Ú<1Ò>2Â6)º4)ÅB:×XRçjhú|}ÿ†‰þ€„õv}÷xýz‚ÿx‚ÿqÿi{ûbtø]oòQc÷I`ÿ:Xý?[ðO_ºQNqX:Sf8]uEa|InSv†Y{ˆ]~‹`g‚—l€œl}Ÿm|¤o}¨p€©o‚ªnƒ©l…©lˆ«qˆ®qˆ°r†³r„³oƒµp…·r‰¸t‹µv‰¬tŽ¥wš¨„°´™ÈïØ̾ÚÑÌÂÂÌ«±Á›¡±ZK8ZK8ZK8ZK8ZK8ZK8ZK8ZK8ZK8ZK8ZK8[L9[L9\M:\M:]L:^K<`J<^K<_L=^M;_N<^O<^O<^O:_P;`O;aP>aP>aP>aP<bOAeMMdMW`O_aUmgb€uq’|v˜€s‘uŽ­€”͆”ß~‡îqyùclúP[ú;Mÿ*Fÿ2Qÿ<Sñ<Mà<E×@EÕCDÔBCÜCEàBCäADâBDÞDBÔC>É@8¾>3¸>1¯9+¨7'©:)­<*´<,Á=1Ï?7ß=:å99ã03â/2à42Û94æMEülaòh[ÙQCÆ</Æ:-È:.É7*Í6+Õ8/Õ4,Þ7/æ93è62é1/è/,î1/õ15ÿ2Cÿ0Eÿ-Fÿ*Bÿ%@ÿ#=ü 9ö 8÷'Aõ+Eõ.Mõ/Pó+Pô)Tÿ2`ÿAhóCXÿ`mÿhuøVeíKZñL\ëBUÚ2Cë@RÝ7CÉ-1º((±.$«8&¥?&¨@'¸?.¼:,ÝYMá]QÍG>ÍD>Á63É;7äUQÕF@É91È8-É:,È9(È9(Ä=)±?'¦<&¨;&¬=*«7(¬3(¼=4ÓLFÖGAÝE@Û96Ü30é73ì51ë0+ô1-ý,/ÿ+-ú./ô1/í4/æ81ß;1Ø>2É9.½3(À;2ÏNHáa^÷yzÿˆŠÿƒ‡ùzû|ƒÿ{†ÿx„ÿn~þew÷^põZlëL`õIaü9Wû=YîM]²JGhQ1Qd6\tDf~Nuˆ[Žc„h†’jˆ—n‰žsˆ¤t„¦t§r}¨p¨n©mƒ©l…©l…¨n‡«n…­o…¯o°l€²k‚¶n…·r‡³t‡­t§wœ¬…²¹šÈƯ×νÙÒÌ»¿Ê¤¬¿’š­[J8ZK8[J8ZK8[J8ZK8[J8ZK8[J8ZK8[J8[L9\K9\M:]L:]L:^K<^K<^K<^M=^M=^O<^O<\O<`Q>aR=aR?bQ?bQ?aP>aO;bL>hOKgNRbP\cWkjfuv”}|œx—‘z–«•Æ„’Ú}…ðuzþlmÿY[ýDLþ/Iÿ5Rÿ<Uó<Né?LãCKÝBHÖ=@ÛACÜ@AÞ@AÛA?ØC?ÐA9Ç>4½=0µ?1¬;+©8(­<,´?.»@1ÊA7ØC=Ù74á85Þ..â30æ:6Ó0'Ï5)ëUGÿwjë[PÑC7Ê<0Ë;0É7*Í6+Ô:0Ú70ß82å63ç32ê01í12ô36ý4<þ%6ÿ#9ÿ"9ÿ"9ÿ!8ÿ6ý5ø6ï2î9ð%Bï(Gî&Jñ'Mÿ3\ÿEhÿbuÿ`l÷WcñQ]òP]ïMZâ@MÔ2=Ú=FÌ7;»/.®/(¦5'¢;(›?&œ>%ª9'²8+ÒVJ»;2Ä?8¾41½2/ÎC@åWSÕHAÈ91Ä6*Å6(Ä5%Æ5$Ã:(±<(§:&¨7%­9*¬4&¯1%Â?5ÛRJÙIAáHBÞ;6Þ2.ê41í1/ï-+ü22þ,/ý+.ù+-ó++í-*æ1*ß4,Ö8,Ñ=1¿2(¼7.ÈIBÙ\Xówwÿ‰‹ÿ‡Šû€…þ†ÿ}ˆÿv„ÿj|þ_s÷XlöTiêD\õD^õ6Uñ:VàLZ¥HC^K-Mc5Uo?b}JtŒ\€–g‡˜lˆ›nŠ r‹¥vŒ¬z†«wªr~§m|¥i}¥g¥g‚¦i‚¥k‚¨mƒ«m‚¬l®h~°i³j„¶o‰¶s‰¯rŽ¬x™°„­»˜¿Å©ËʵËͶ½Åžªº‹—§]K7\K7]K7\K7]K7\K7]K7\K7^L8]L8^L8]L8^L8]L8^L8]L:]J<]K=^L>\L<\L<[N=]P?^SA`SB_R?`P@bQAcRBdQBdNAdLBjMIhKMdPYf[lnk†w{ž€…­‰ˆ²ƒ©¥‡©¾ˆ Ð‚’ây~ðsqújbÿa^ùJWñ>Që:Lî?NïCQê@Kæ<Gå>Fá<Bß<?Ú<=Ö<:Ï>9Ê=4Á;0¸:,§6&¯A0­<,¬6(¿A5ÌF=Ì=5Ð72Ú85Ü71Þ5.Þ6-Þ8*Ý<*×:'Ó6%Ù;0ðSLòZOÙE9Ç8*Í>.ÔB3Ò:-Ô3+ðGBá//î5:ó6=î,7ÿBMñ'3ú&2ÿ)2ÿ(2ù'ý"*ÿ'/þ'/ð&ð)ù+7ê .ì&7ñ-Aç#;ÿXrç-Dÿ]lÿ_iþZcùU\õPWëHMÝ?@Í84Ë>7ÄA7·?1¦7&™2!™6#š;'Ÿ<)§8-®7/ÂE?¿;7½31Ä64Å54ÔDCÛLHÚKEÑD;È:0Ä4)Ç5(Ë4)Å5*¼</³9,­/#«(³-$Ã9/ÒC;ØH?äPFßE=Ú70Ý0*ç0,ò21ù13ý14÷/1ö01ö01ó0.ò0.í2-æ5-Ý9/Õ=0¾2%À>1¹=3Ö_Yésqÿû„†û‡Šÿ„Œÿxˆÿh}ÿ_wÿ[uÿUoøLføEcñ:Yô;[æ?YÛ]i‹@;VF-L^6YuBg†Mw–]}œcg€¡jƒ¦n‚¨o€§n¦m~¥l}¥i|¤h}£f£e£f¥l¦m«l¬k~­g®f±gƒ³i‡µm‹´r°vŽ®|™´‰§½™±Á¤²Á®¦²°˜žz…‹]K7]K7]K7]K7]K7]K7]K7]K7^L8^L8^L8^L8^L8^L8^L8^K:\K;^L>_M?^N>[N=[N=\Q?^RB_SC]QA]OB_OB`NDbPFcOHeNHiMJgNQeS_g_tnny~¨€‡»‡ŠÁž•Ê£‹»«£¹xÓyƒê|{ôvjöi`ø]aóOZìFRòHSøIVõEPï?Jë;Eä9?à:<Ú::Õ=8Î?7Ç>4À</·<,«<+ª?-©8(²:,ÁA6Å<4Ç61Ô<7Ò50Ö5-×5*Ø4(Ù8&Û:&Û;%Ú9'Ý6.æA;ëMDáI<Ð>/Ç8(Ê8)Ó;.Þ;4öJFä01ð6;ö6Añ.<ÿ@Nï'4ó$,÷%(û),ý+,ü*+ø((ö((÷+,ï$'í%(ê%,ý;DÙ'ð4CüARØ&6ÿjuÿbjþZaüW]ôOSåBC×<8Ñ>6¾8,¿D5¹H6©>,™/)(’(ž-%°:6ÑTPÎHGÃ54Æ45Ñ==ëWUØGBÖG?ÐA9Ç9/Æ3)Ê6,Ð7/Î70Ç=3Â91Â5.Ã2-É4.Ñ83Ø?9ÞE=äJ@ÞA8Ú6-Ý0*ç0,ò21û03û03ô02ó12ô01ó/0ó0.î1-ç4-ß9-×=1¿3$¾</·=2Ó^Wévsÿù…ˆø…Šÿ‚Šÿt…ÿf|ÿ\vÿXtÿQoþIhü@aó:Zñ?_áI`ÁS\{?7RF.M_9ZvCe‡Ks•Yxœ_wb{¡f}¥i}¦j|¥i|¥i|¤h{£e{¡d{¡d}¡c~¢e|£j}¦l}©l}ªi|«e}­c¯c‚±c…°h‡²kˆ¯pˆ«s‹«|¬„ª‡Œ¢‹x‡€dppR^^]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8]L8^K:_N>aP@`P@]P?\O>[O?[RC[QE[QG\OG\OI_PMcQOfTTgUUgUUgV\h]nli†sv£|ƒº‚ŠËŠÔ˜‘× ŠÊª‚´¹}ŸÏ€“䄆îvósjûeg÷S\íGQðFOöHR÷EQòAKî=Eä7=à7:Ù99Ô<7Í@7ÅA4¾?0¶>-­>-ª<+«:,¸B6ÇH?À;2»0)Ê:2Í6/Ð6.Õ7,Ø8,Ù7(Ù8&Ü9&à:*Ý4-Û4.âA9éOCÞH:È5%È2#Ù@2â>5õHDè13ò5;ù7@ø4@ÿESø2?ð)0ë#&ì$'ù13õ-/ï''ì&%õ12ë)*ö8:è+1â)1ë3=ÿR^ï=Kÿ[gþ^fñV\îOTðMRêEIÝ:;Õ74Ô?9À6,Á?2¼B5³=1¬7-¥3)¡,#œ' š%©/*ÇECÂ::¼,,È35×ABô^]Ð?:ÒC;ÐA9Ë=3Ì9/Ô=4Ú@8Û@;Û@<Ø=;Ý>;ãA?ä>>à::ã?=ëIDàB9Ý<4Ù5,Ý2+ç1-ð31õ12ø02ô02ô02ô01ó/0ó0.î1-ç4-ß9-Ø>2À4%º8+µ;0Ð[Tízwÿ“’û‡Šúƒ‰ý~‰ÿr‚ÿdyÿ[tÿTqÿNmÿFgû=_ô=\îEbßTg¢AHp;3SH2Qa<]xEf‡Nr“Zu˜^tš_xžc{£g{£gy¢fy¢fy¡cx byŸby a| bz cz¢f{¤h{§h{¨e{©a{©`}«`®`‡²jŠµn‹²s‰¬t†¦w‚žuy“no†lSeYAPM2A>^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^M9^L8`N:aP<`Q>]P?ZN>YOCXPEXOH[QO_UTbXYeX_iYcl\fk^glaildqok‚rtšy~µ†Ê…ŒÚŒŽáŽ‡×Ÿ‹Ó­ŠÄ³€©½yŽÌ{Þzò‚wÿsrÿcgùU\ôMTõGPôCMò>Gï<Bå6;à88Û97Ô<7Í@6ÅA4¼A1·?.«:*­>-±=0¼F:ÌRGÇH?º5.º1)È91Í81Ó;0Ù=1Û;-Ù6'Ü6&à8+à5-Û2+à=4ìNBãK=Ò:,Ñ8*àB6ß7.ð>:é/0ð16÷4<ü8BÿNZÿLUý>Eò38â$&í/1ê,,ð22è**ë/.ò::Ñæ37Ý,2è9@ðEMÿXcóMWØ=CÚADáBFá>AÞ9=Ü89Ø88Ó97É83Â91½:2ÀA:ÍNHÖVSÐNNÃC@²72¶95ÏKIÙMLãQRïYZêRQñYVÉ51Î>6ÒB9Ð@7Ó?5ÙB9àC<â@;é?@ç8=ì9=ò?Cï:?è58ì>=öMJÞ93Û60Ú3+Þ3,å4.ì30ò21ô01ô02ö/2ö01õ/0õ/.ð0-é4-à8-Ù?3Â6'·5(³9.ËVOï|yÿ“’üˆ‹ý‰þz‡ýoücwÿXqÿOlÿFhÿ@cû9\ô?^éKdØZh…35g;0WO8WgCa{Kj‰Ps’Yt•\u˜^wby¡ez¢fw dv awŸavž`xŸ`xŸ`{Ÿaz cz¢fz£gz§fz§bz¨`z¨]|«]~­_†²g‹´nŒ³tŠ­s†¦u€qtŽii€dQcUDSL8G@^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M;^M9]K5_M5`P9_P;\O>ZN@XOFXQKYPQ`W\g`hnfsshysg{sg}qh}ol}pq†rw—v}«z‚Á‡Ò„ŠàŠ‹å“ãŒÚ „¿žuŸ l‚¬nq¼vlÒviòsmÿllÿgiþ^`ûRWõFMï<Bê7=æ5;á78Ý98Ö=7ÎA7ÆB5½B0¸@/­7)´@3¯:0°;1ÈRHÕ\QÉMC»;0¾90Á7-Ì9/Ö>3Û=1Ú6*Ü4'â6*à3,â70ä=5åE9âF9ÞB3ÞA2â>2Û0&ê72ì0/ï.1ñ.4ú7?ÿPXÿ]dÿZ_ÿOSá.1ã03à,-÷EEè66Ü,,Û-.ë>@Ü25Ù37ÿbhà=BË*2Ê-4Á&*Õ;=á@EÛ6:Ù37ß9=Ü7;Í/0Ì43Í;;ÕGFÞRSì`cõilôekê^aÄ@>ÆE@å^[øhgÿopÿjjåKK×=;É40Ñ>7×D<ÖC;Õ>5Õ<4Ø:1Ü41î5;ñ-7ð,6ò.8ï,4ë,3ó9<þJKß3/Ü3.Ú3+Ü3,ã5.ê40ï4/ò21ô02ö/2÷/1ö.0õ/.ð0-ê3-â7-Ù?3Æ8*´2%²8-ÃNGï|yÿ’‘ÿˆŒÿ‰ÿw…úl|øatûTnýHgÿ>bÿ:_ú6\óAaàNeÂWap/+_?0[W>^lIe}Mk‰St’\u–_v™_xžcy¡cx buŸ`uŸ`u_vž_wž_xŸ`{Ÿaz¡bz¢d{¥e|¦d|§`y§^y¨Z{ª\}¬^€¬a…¯g‡®m‡ªp…¥sƒ r{•nrŠjexe[k^RbU_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N<_N:^L4_N4`N8^O:[N>YOEZSMZTT_ZahbprmzuŒ{u‘{q“zp“vp’qt“rxšt}¨w¶zƒÄ~†Ï„‰Ú‹ŒÞ–Ü™ŠÍ›…¶¢ƒ£ª„‘«~yŸl[šQ>¼MBÔQIâWRëWUñSRñKMí>Cå4:é9<æ9;à;9Ø=8Ó@8ÊB6ÀA0º?/³9,ºA6«5+£.$¾I?Ùg\ÙdZËUI¸<2¸6)À3)Î:0Ù;0Ú6,Þ3)ä6-ß1*ä71ã;2Ý9/Þ>0ãF5äB3ß9+ß1(é4-ð31ï-.í*.ô17ÿEJÿV[ÿ]bÿ\aâ9<ã9<Õ+,ôJKâ89×//Û57Ì()Ì'+øXZÝ>BÕ9=Ñ6:º#Ä(+Ü<>æAEÝ49Ù,2Þ17Ù05Í(.Ò37äKNöaeûhnùcl÷`iú`løbkÖLLÕNJê\ZêVTçMMãHFÏ42Î3/ÙA<àKDäOHßJCÙ@8×:3×6.Ü.-õ2:û-:ø*7õ'4õ)4ø0:ÿ>CÿJJà1.Þ3,Ü3,Ý5,á4-ç4/í4/ð3/ô02÷/2ù.1ø-0ö..ñ/-ë2-â7-Ù=1É;-³1$±7,ºC=ìyvÿÿˆÿ|ˆÿqöhxö_rùRlûDcÿ9^ÿ6]ú6\ðFcÑN`¢IMe5+VD0[Y@^lIe|NkˆRt‘[w•_x™`{ždz cvž`s›]rœ\s›\tœ]v^wž_{Ÿaz¡by¡bz¤b|§b{¦_x¦[x§Yz©Y{ªZ~ª_ƒ­e…¬k‡©m‰§s‰¥u„s|”ru‰pnlfwe_P=_P=_P=_P=_P=_P=_P=_P=^O<^O<^O<^O<^O<^O<^O<_N:aP6aP6aO9^O<[OA[RK\VV_Zakgvso†}z—‚~¡|¥}w£{u¥xv§sy©s}°x¸{„¿~†Ä„‰ÉŽÎ”“Íš“Ç •½«›µ¼¦²É­©Å¦”ªˆmœfJœE2¯A4¶@4ÂB9ÖHDèNNîJKì?Cë=?é;=ä<;ß=:Ù@8Ð@5È>1Á=0½:0¾?6¯4,§1'»I?ÑcVÖh[ÕeWÃOBº>2º4)Ç7,Ö90Ù5,ß2+ç60â0,ã4/â7/Þ7.à>1åC4ä>0Þ4%æ4*é0+ð3/ð..ï-.ò14ó78öBEöMRüY^éFKöQWØ37ëFJÝ8<æAEÚ7:Î.0Ñ05äHKÒ7;Ð7:¹#%Õ;=Û8;å;>ë<Aç6<â-6Ý)2á1;ç<DðLSý]eÿksÿjsú`jöZgùXh÷[fôddèZVèWTØ@=Ï10Ô44Ó33áC@ãJEéRKêSLáJAÙ?7Ø93Û81ã33ó.7ÿ0>ÿ0=ú*7ü.:ÿ;EÿCHûABã1/ß4-Ý4-Ý5,â4-æ3,í4/ñ40ô02÷/2ù.1ø-0ö..ñ/-ë2-ã7-Ø:/Í>0µ1%±7,²;5ésqÿŠŠÿ‡ÿv†þi}óató[p÷PjúCbÿ7`ÿ7aù;_ìLf¿O[~86]=0NF1VX@ZeEcxMj…RrŽ[v”^yša{ždxžas›]p˜Yp™WqšXqšXt›\uœ]y_xŸ`x¡_y¢^z¥^y¤\x¤Yx¤Wy¥V{§X©_‚¬d„«h†¨l‡¥o†¢q€šmx‘jq…ij~cbv]_P=_P=_P=_P=_P=_P=_P=_P=^O<^O<^O<^O<^O<^O<^O<_N:cQ9cQ9aP<]P@ZPG\TR_Zaa_mtrŠyx˜€¨ƒ°~~°yy¯yu°wwµw|¾x€Á}…Ä‚ˆÄ…ŠÁ¿—–¾ ¼¬§»¶¬µ¾³±È¸«Ð¾¦ÖÁ¢Ñ»–Ьˆ¯oT®\G¤G5¨>0ÂG?àWQïWVñNOé@Cê=?æ<=â=;ß=:×=5Ñ:1É90Á80Á<5¹:3¸=5ÁOEÇXMÇ]OÑeXÓcUÂL>¼:-Å8.Ò91×4-ß2,ç60æ40á2-à5.ã<3çC7æC4â:-ß3%ê5,æ.&ð0-ï/,ð31ñ54ç-.â24çBF÷X]öW\ÿouåDJëHMà<Cþ[`ÿouÞ=Bÿ}‚ÿmqêQTÔ>@Æ02Ö:=òHKî9>ð7?ø=Fò7Bè-8ô>Jÿ]gÿmwÿjrÿcn÷]gú^kÿaoÿ]nùYeò\]ãPIâJGÕ74Ò/0Ù54Ò0.Ø95ßD?ãJDâIAÙ@8Ó6/Ö4/Ü71è66ê'/ü0<þ2=÷+6ù0:ÿ>Eý?Aì44â3.à5.Þ6-ß4,á3*è3,ï4/ô41ö01÷/2ù.1ø-0ø..ò/-ë2-ã7-×9.Ñ?2¶0%²6,°61èpoÿ‡ˆÿ†Žÿp‚ücxñ\pôXn÷OiûBbÿ9aÿ<döAbäRg°QWd/'WD3GG/RT<U^?btLiQq‹[v’_z˜b|dxœ_r™Zn—Un—Un—Uo˜Vr™Zt›\w›]wž]wž[x¡[y£[w£Xv¢Wv¢Uw£Ty¥Xz¤Z~¦_¦c¡d~œf{—frŒ]i‚X`vRZoPRgH\L<^N>`P@`P@_O?_O?_O?aQAbRB_O?]M=]M=aQAcSCbRB`Q>dS?aP>]P@^QI[QPZSZeapsq‰yxšz|¥~€±€¹}ºxz·rs´lo¶twÈv{Ì€ƒÊ‹Æ–—䤾²°»¼¹´ËƳÓÌ°ÙϬÛϧÜÏ£ÝУÙ˜٘Ѩˆ¸€g ^FËzgÖue½H?×QN÷ccéKLå?Aâ89ä::â:9Ü75Ù61Ö;6Ë81À5.ÇB;»<3¿I?ÆXK¹OAÎdVÑcTÌXIÅG9Ä:/Í6-Ø7/á6/ã4/ß0-ß2,à70ä=4à<0Ý7)ß5(å7,ä/$ê/&ì/)î1-î20ë40è51â64×79æJNõY]íNS×8=×6<ùX`ÿ~„ÿ~†ÿouÿgoÿipÿflõ\añX[ûZ_ÿUZÿDKü8Bû7Aö2>î+9ì0>ñ=HÿS_ÿ`iÿgrÿepþbmÿcnÿ`oý]gíSSßF@Ö85Ö41×11×/.Ø31Û94áD=ÜB:Ù?7Ö<4×90Û81à93æ66ð7<ó4;ó4;ö4<õ6;ò59ë33ã1/Þ3,Þ5.ß7.à5+ä3+é2,ï2,õ20÷12ø03ú/2ù.1ù//ó0.ì3.å7.à?5Ì8,¿6,¯/&¸;7ÒXWÿ˜šþ{ƒÿm€ÿe|ø\rðPhôGeüCeÿ;dù9`õNlÓRd‰=?P, C;&CG.KM5Y_CZjEcxMqˆZx’bx–`t•\r–Yq˜Yl•Sm–Rn—So˜Tr™Xt›Zuœ]uœ[t›XtWv Xw¡Wv¢Wv¢Uu¡Tt S{¥[{£]zž^u—[m‹Ub~MXrCSj>AW0?U/<R,[K;^N>`P@`P@`P@_O?`P@aQAbRB`P@_O?`P@bRBcSCbRB`P@fVG_QF\OG^TSaZad`omkƒvw–wy¢{}®}¸x|¹quµkp´lo¶nq¾uvÐ|~Õ‰‰Ó˜˜Î¨§Ç¸¶ÁÉźÓαÛתâÛ¥çÞ¥æÝ¢äÛ¤âØ£ÛÑ ÔǛӼš»œ€©}dÅ‹wÊ}m´VJÒa[örpìZ[çKLã?@ç=>ë>@ç;;â66Û64Ö=8É83Ê?8¹6.¸>3½K@¶H;È]MÝo`Ô`QÆH:½5)Ä0&Ô6-Ü5-Û0)â51å95ä=7à<3Ü8.Û5)ß4*ã5*ì7.í5+í2+ë0+ç0,ã2,á4.Ý52Ö87Ø>@äJLóW[ú^bü\dþ]eÿaiôS[ïNVñQYÿ_gÿjqÿflõZ`óNTí4:ñ,5ô*6÷-9ñ'5å!-æ'6í7CþPZÿZdÿblÿdmÿenÿcmý[hõQZåEEÞ<7Û64Þ63ß55Ü30Ú2/Û62à>9Û>7Ø;4×:3Û81ß82å95ç:6é9;ê7:ë7:î79î79ì65å31ß2,Ü3,Ý6-ß7,â6,å2+ê1,ò1,õ20÷12÷03ú/2ù.1÷//ó0.ì3.å7.ß<3Ô=2Â8.¶1*·74ØZ[ÿ“ûs}ÿfzÿ^vùWo÷OiùFfû?bü<cô@cåOh¹KVx;8L2#<:#<B(EG/OS8VdA`rJnƒXu_v“]t“Xq“Vp”Tk’Ok”Pl•Qm–Rp—Vr™Xsš[sšYt›XuWuŸWuŸWtžTržQqRpœQn˜Pl”NhŒLbFXu?Ni6F]/@W+@W-@W-AX.YL<[N>^QA^QA^QA^QA_RB`SCaTD`SC_RB`SCaTDaTD`SC^PCbUM\QM[QRcYbkdtqm„vu•y{¤y{¬y~¶x|»nsµei°`g¯jn¸ruÆ}}ׇ„Ý—”Û©§Ø»¹ÏËÊÈÜØ¿åá´éäªíè¦ðé¥íå¤çá§âÛ§×Ï¡ËÃœ¸¯©››ƒk¨ƒp§o`œRGº]UÝmi÷uuï__äJLå@DëADì>@è8:â66ß=:Ô;6ÔA:Â70·7,·>3®=/¶F8Ûj\ßi[Ù[MÊB6Å2(Ñ4+Ù6-Ù2,Þ52éA>ìGCà=6Õ2)Õ1'Ú2'Þ0%æ4*è1)ç.)æ/)ã2,â7/á=4àB9Æ-(Ê43ÜDCðVXú^b÷ZaóS[ñQ[èFQêHSïOYü\fÿmvÿntôY_ã>Dé.5ô-4û2<ü2>õ+9í'4ö6EÿKW÷ISõPWøW_þ^fÿckÿ`i÷RYéBIÞ97Þ71ã75é<8ë;;æ95á51Ý60Ý:3Ù;2×90×90Ü71á83ç:6é<8ã99ã99å97ç98è88æ74á40Û2+Ú6-Û7-Þ8,â6,ç2)ë0)ó0,÷10õ12õ13ø02÷/1÷//ò0.î3.ç6.Ý90ÜD9Ç80»4.µ/.ä_bÿƒˆølwþbxþWsúPmþLlÿCfö:]ó;_ìIfÔSgDJl;4O>,=<':?(ED/FH0Q\<YkEg|Qr‰[tZqUmRkOiMi‘Ki’Lk”Pm–Tn—Up—Xq˜WuœYuWtœUrœTpšRm—Mk•Mj”LeGaˆE[AUt;Li3E`-AX*>U'B\/E_2Ic6WJ:ZM=\O?^QA]P@^QA_RBaTD`SC`SC`SCaTDaTD`SC_RB\PDZOMZQVbXcjcsqm†vu•xz£z|­w|´sy·mr´bi¯]d¬ag±lr¾x{ʉ‰ß•‘夡䶵áÇÄÙÕÑÎáÝÄçä¹ëæ¯îê­îè¬éâ«ãÛªÚÓ©Ë¡»¶™¥¥›™„“ˆv˜q™rc›bW³i`Ïrmû‹‰õutê[]æLNèCGí@Dì<?é;<ß:8Û<8ÞE?ÓC;Ã:0º:/°6)§1#¿I;ÛaRêk\ÛSEÉ:,Ì4'Ô6+×4-Ù42ñMKþZXíKFÙ80Ö3*Û4+Þ2(ß.&á,%á,%Þ-%Ü1)Ú6,Ù;0×?4Â/'Ä4,Î:6ÙCBßFHàEIäGNêMVñQ]öVbøXbù\eÿgpÿmuôYaÞ;@ê17ñ.4ò-6ð+4ë%2ì(4û=IÿS_í?IèAIêGLôQVÿY`ÿX]ôGMä68á53ä73ê;8ò@>ô@?ï=;æ95à72Ý82Ú91Ù80Ù80Ü71ã73è96é:7â:7à;9ã:7æ:8ç:6å84à5.Ú3+Ú6,Û7-Þ8,â6,ç2)ë0)ò/+ö0/ô22ô23÷12ö01ö0/ò0.î3.ç6.Ü8/àF<É91¾3.¶,,îfjÿy€øgtÿbyýTqþKkÿGjÿ>cò6Yë=^ãQhÄXeˆBBgC7[N;GF1AC-JH3EE-LU6Sc>btLlƒUp‹XnRkPiŽKgIhJh‘Kj“Mk”Pm–To–Wp—Vt›Xs›Uq™So—Pj“MgJeŽHdG`‡D]AWy=Rq8Mi6Lf6Ne7Of8Lf7Oi9Sm>UH8WJ:[N>\O?]P@]P@_RBaTD_RB`SCaTDaTD`SC_RB_RB^QIVMR_Xhnf{tp‰xt—xw¡xz«y{´sy·jr³`g­[c«_g°jr»v|Æ€„Γ•àžžä­®ç»½æËÉßÔÓØÝÛÎáßÆåá¾æâ¼äß¹à׶ÚеÎƱ¾µ¦­§›˜šŒŒŽ€‰…yŠ|q‘vk›si¨meºoi扄î~ósrñedñWYðMNîDEì@@ã:7à;7äB=ÞE?Ì<3Æ=3ÄB5¯1#®0"ÇI;ÜXKÖN@Ì=/Î8*Ð6*Ñ3*Ö42õUUÿkhú]XãD>Ü;3ß;2ã80â4-ã2,â1+Ý0)Ô0'Í/$Æ/$Â0#Å9,À4'Á3)Ë:5×CAÛEGÛBGÚ@HàDOëOZòVaòVaú^iÿgoóYaáBGê:<í57í38ï28ñ3=÷9CÿHSÿV_ä6?Ý4;ß6;ëADùJOüIMô;Aé13è51ë95ï<8ò<9ó;9ð:7é73â70ß;2Ü;1Û:0Ü90ß82ä73è64é75æ:8ä;8æ:8è:9è;7æ95á6/Ü3,Û7-Ü8.ß9-á7*å3)é1)ñ0)ô1-ó32ñ33ô22ô01ô1/ò0.î3.ç6.Þ7/àC:Ê70À1-À13÷jpÿqzúftû]túPmÿCfÿ=dÿ9cõ8^æEdÙZm«V[wE>dJ=aYFTO;KI4PI6GE.GP3L\8ZlFf|Nm…SmŠRlŒMhJfŽHgHg‘Ii’Lj“Ol•Sn•Vo–Ut™Vs˜Un•Rj’LfKcŒHaŠF_ˆFZBX|?St;Pn8Ok:Rl=Wn@ZqCUo?Vq>WqARE5UH8XK;ZM=[N>\O?^QA`SC`SCaTDaTDaTD`SC`SCbUEaVP^Vcje|yu}yšzy¡xy©vy°sw´jr³cj°[c«]e®hp¹v~LJшŒÓ™Ý£¥à¯³ã»¿åÆÇãÏÎÞÔÓØ×ÖÑÙ×ËÙÖÇ×ÑÅÒÉÀÌþŹ¹´¨¬£šŠ‹†x{tyvquleleogŽa[`[ÀsmØyuð{ü|yÿrpýccöSTðHGïC?ê>:á<6àA;Ñ:1ÓC:ÝTJÂ>2¸6)½9,Â=.È?/ÏB1×C5Ö@1Ñ7-Ð2/êLMú_]òWSßD?Ù:4Û81à70æ93ç92ç92â:1Ú9/Ñ9,Ê8+Ä8)°(¸3"È@2ÓI>ÒD@Ë;:Ì7;Ï:@Ñ7AâHRíV_ðYbõ^g÷`iêU[ÜAEå@>ì?;ñACúGJÿLTÿQYÿT]ÿV^á4:Ú/5Ü/3ç7:ô=Aø;?÷48ó12í42ï95ð86í42ë20ê20ç40á4.ã<4à=4ß<3Þ;2á83ä73è43è43è88ç98è88ë99ë97é75â5/Þ3,Û7-Ü8,Ý:+ß7*ã2(ç0(ï0(ò1,ñ42ð43ó32ó11ó1/ñ1.ì3.ç6.à91Ù<3Ì71Á0-Ñ@CÿnuýkuùbqðRiõHfÿ;aÿ4^ÿ6bû>däNiÊ\i‹GFjF:]L<]VCXQ>RJ7OG4ID.DJ.GU2Sc>^sHgMj‡OjŠKgŒGgŒFfŽGfHgJi’Nk”Rm”Um”Uq•Uo”Qj‘NgŽKcŒH`‰E_ˆF^†G\‚E[~DWxAVr?Uo@YpB]rG^uG[sCYtAXs@NB2QE5UI9WK;XL<ZN>\P@^RBaUEbVFbVFaUE_SCaUEfZJh]YngwupŽ|x™{y wx¦vw­pu¯jo¯ah¬^f®_g°em¶pxÀ}…ؙ͉ؗ Ö¡¨Öª±Û´¹ßº¿ßÀÁÝÃÃÛÇÆØÆÃÔÅÁÐÁ»É¼³Ä»°Áµ¨¼¦˜¯–‹œˆ…Œqqqtpopfewhe†nj„_Y•d_›ZT¹f`Ùsnðzvÿ{wÿwsÿhfüZWþRNõHBæ=6çD=Ø;4ÛG=ê\RÅ;0Ä<0½7+¾6(Ç>.ÑD3ÔB3ÔA1×A3Ò86Ø>>ÝCCÜB@Ö=7Õ81Ø7/Þ7/å:3æ93å:3â;3Û>5ÕA5ÑE8ÌG6®-¼;%ÍJ8ÑL=Æ=5»0+À00Ê9>×AJáKTëU`ðZeð]eìYaßLRÔ>@Ï4/Ø7/à;9ê@@òCHõDJñBIî?Fâ5;ß26ã36ï8<õ79ø14û/2ü14ï20ð95ð74ì0.è,*ç0,æ3.à3,ä=5à=4à=4à<3ã:5æ95ê65ì65ì57ì57í57î68î66ë54ä2.Þ1+Þ7.Ý9-Ý:+ß7*á4&æ1&í1(ñ1,ð42î53ñ42ò21ó1/ñ1.ì3.ç6.â;3Ò4+Ò:5Æ20çUXÿpxùcnòZiçI`óEfÿ8bÿ/]ÿ5bÿBhÜLe±PYj5/^G7XK:UN;[P>VI8M@0JC0@F,AO.K[6Xj@bxGfJhˆIf‹FeŠDc‹BcEeŽHgLh‘Oi‘Rk’Sn’RlPhNd‹JaŠH_ˆF_‡H_‡H_…H_‚H]~G]yF]wH`wKcxOczN]uC[vAZu@JC3MF6QH9TK<WK=YM?\O?^QAdWFeXGdXHbVF_UIaWMg^Ulcfwr‰yušxvžtsŸrr¤tu­lp¯bg©Za©_e¯fl¸ms¿v|ȇӓݗœÞ›¡Õ §Ó©­Ú®³Ý±´Ý²´Û´´Ú¶´Ûµ±Ö³­Ñ®§É«¡ÄªžÂ¤˜¼™‹¯ˆ~™|x†dbgiefcYZj[X{c_yVP’c]ŠNF¢UM½`XÖkcñwrÿzÿzwÿnjþa\÷TOéGBîOIÝB=ÞG@êWOº,"¾1'¾4'Ç>.ÑE6Ë>-Á2"Ê7'ÛE7ÞE?Ó97Ì3.Í50Ô;5×=5Ý<4á=4ä;4ä92Þ5.×4+Ï5)Ê8+Å<,Â?-ÔS>ÈG2½<)¾:-Ã=2Ä;5Â74Â43ÜKNÞMRáPUåTYèY]åVXÜMOÓD@É5+Ñ7+Ø7/Ý52â24å26æ18æ.6ì4>ì3;ó5?û9Bý4>ú+5ÿ(3ÿ/9ø-3ù57ù59ð./ë*+é0.è51â5/ã<4à=4à=4á=4ä;6ç:6ë76î66ì46ë35ì25í34í55ê41ã1-Û0(Þ8,Ý:+Ý:+Þ8(à4&ã3$é3&ì4*î50î52ð42ð3/ò2/ï2,ì4,ç6.ä=4Í/&×>9Ì35ùbgÿoy÷[iðNcêC]ùEhÿ:eÿ0]ÿ6aöGhÉJ]“CFR,#VI8SL:OH6[O?UI9F?-HF1>B'@J(IU1Sd:_rEe}Ig„JeˆHcˆCa‰BbŠCcEeŽHfMhPiQlPkMfJcŠIaˆG`‡F`‡H`†I^G^F_}G`|Ia{Kd{Mf|Nf~N]xC]z@^{CDB6FD8JF;PG>RH>WI>]M>_O?`Q>aR?`SB_VGbYRgb_ojnso~wq“zt |y¨yw©pp¦gg£`a¢\_¤`b­fhµopÀyzÌ„‚Ö‹ß–’眙栞ݡ¡×££Ù¤¤Ú¦£Ø¥¢×¥¡Ô¤ Ó¥žÑ£Í¢šÉ¢™ÈŸ–Ù»Ž‚°ƒ|žrn}igld^`fZZkYUrWP}WNŒWO•RI¨ZPµXPÁZSØkdìyrù‚|ÿ‹†ùuqÿusí_]ßPLÙEAïZTÌ71Å2*Ä6*Ã7(À7'Ã7&Æ9(Ê;+Ð<.Õ?1Ò8.Ö<2×?4Ó;0Í5(Í3'Ø:/â@5à90á90Þ7.×7+Ï7)É:*Æ?,ÄA/Â=.Æ@4ËE:ÍG<ÌC;Å<4»2,¶+&Ç<7ÐEBÙOLàVSãYVáXRØOIÐC:Ì;*Ó:(Ü8,â5.è./ì*2ð(3ô'6ú->ú+?û*?ý)?ÿ(?ÿ&?ÿ$<ÿ$<ÿ$8ÿ'9ÿ+<þ,9ó+5í.5è45ç;9Ý84Ý:3Þ;4à;5ã:5æ95é73ë54ë35ì46ë54é54ç53â5/Ý5,Û5)Ü8,Û9*Ü9(Þ9&ß8&â7%ã6%æ4&ì5-í4/ð50ñ4.ñ5,í5+ê5*å7,à90Ø7/Õ:6Û?BÿmwÿbsûSjõIcÿKjö6[ÿ6_ÿ<gø>cçNj¯IV^$"D, ?:'HE4PM<PI9EB1>C/?G/BH&EK%KS.S]8[kDavKd~NdƒJb†F`ˆBa‰@c‹BeFhJlPm‘Ql‘LiŽHgŒGf‹HfŠJe‰Ic…HaƒGdƒJdIbGc~GdJdHe~Ga}C[{<\=_‚B=?4?A6FC<JE?PE?TG>[K>]M=`O;`Q<^SA^VIc[Xhcinlyso†tp•vpžtp¢pm¢gfŸaaŸ]^¡\\¤ed°kjºtrÅ}{φ‚Ù‰à”Žæ˜“å›–Ùœ˜Õ™Öžš×ž˜Ö—Õœ–Ô›•Ñž–Ñœ”Íœ“Ê›’ǚē‰»ˆ®‚xtk|mdif]^f[YiZSoWM{UJŠVKšVK°\RºYRÁXRÓfaàqjãvoí~wý‡…ÿ‡…÷usîgdãUSëZUÇ2,Â/%Å7)Ã7&À7%Á8&Ä8'È;*Ï;-Ó=/Õ9,Õ9,Ó:,Ò:,Ñ9+Ñ9+Ô8+Ø8,ß;1à90Ü8.Ö8,Ï9*È9(Á:&¾9&¾6*Á80Æ=5É@8ÊA9È?7Å<4Â91¿4-Ç>6ÑH@ØOGÚQI×NFÌC;Ã7*Ê7%Ó8$Ý7)å4,í//ô+3ù)7ý(:ÿ+@ÿ*Aÿ)Aÿ&Aÿ#@ÿ!<ÿ=ÿ:ÿ:ÿ 9ÿ%;ÿ);÷-9ð19ê7:å;;Ü94Ü:5Þ;4à;5ã:5æ95é73ë52ë35ì46ë54è64ä71ß6/Ü5,Ù5)Û9*Û9*Ü9(Ü9&Þ9&ß8%á8%ã6%ç5+ê3+ë4,ë5*ë5*ê6(ä7)á7*ß;1Õ4,Ó54óTYÿesÿ[põGbÿIhÿAdÿ;`ý3[ù;aòMmÍLb‰8?S*$A5'69(;<,B@1DA0@?-=B.>G,=CKP'\b<eoJhvRezQd~Od‚LaƒFa†Aa‰@cŒ@gDiŽHkMl‘Nl‘KjGhHf‹FgŠJf‰If…JdƒHfƒKdIdHd€FeGeGfEb~A]€>_„?b‡B69.:<1??7DA:KB;OE<VH=YL<^O<\O<\SB^WMc]]helom{sq‰so”qk™jf˜c`•\[”ZZ˜\] \_¦hjµno¿wxÊ€Ò†„ØŠˆÜ‹à‘ŽÝ“Ó“‘ÑÑҔѓŽÑ“ŒÐ’‹Î•ŒÍ“‹Ç”‹Ä”‹À“‹¾Œ„³ƒz¥€r•|ewu^fi[[d[Vd[RfXKrVH„TH£ZQ½`YÉ\YÏYWÛcbákißokãvqûŠ„ÿŠ†ÿ…ÿ}y÷nhîaXÄ4+À2&Ä8)Á:'À9&À9%Â9&Æ;(Ì:+Ð:,Ö:-Ó5)Î5'Ð8*Ô>/Ô>/Î6(Ë/"Ú:.Ú8-Ù7,Ö8,Ð:+É:)Â9&¾7$½5)À6,Â8.Ä:0Ç=3Ê@6ËA7ÌB8Ç:1Ê@6ÏE;ÐF<ÒH>ÑG=Ê@6Å7+Ì6%Ö6&ß7,ç4/ñ03ö-5û+9ÿ*<ü';ü'=û%=ù#;û!:ú7ü7þ6ÿ8ÿ6ÿ$8ý*;÷2<ñ6=ë8;ä::Ü94Ü:5Þ;4à;5å95ç85é73ë52í36î47í55ê65å61á6/Ü5,Ø4(Ú8)Ù9)Ú9'Û8%Ý8%Þ7$à7$á6%á5'â4)ã5*ã6(ã6(á7(Þ6)Û7+Ý<2Ð1+Ø88ÿkrÿ]nþPiñ<[ÿHlÿ7]ÿ>eù5[î>bæVqª?Qa#(L/);7+07'37&9<)@=*A<(@>)BB&?FXa4s~T€Œdw‡`hSa}La€G`ƒC`…?a‰@cŒ@hŽEjJkMl‘Nn“Mk‘HhHgŒGh‹Kh‹KfˆLd†Je„KdƒJc€FdGf‚Hf‚GeFb€Bc†DeŠEhH25*58-;<4A>7G@:KB;RE<VJ<XL<YM=YQD^WOc^bigron€roŒrm•jg”`^WW‹TUŽWX˜\_¢ac­ln»rsÃ{|΂ƒÕˆ†ÙŠˆÛŒˆÜŠ×ŽÒΌώ‹Ð‰ÑŽˆÐŽ†Ï…Î…ÊŽ…Æ…Á‡¼‡¶‰‚¬€xoŒ€_p|Y`lXYd[T^_Q_\IjWF}SEžUL¾\YÐY[ÙX\äcgçkkáplåxsì}vízsþƒ~ÿ†ÿ{õkaÉ<2Ä8)Â;(¿:'¿;&¾:%Á:&Å:'È9)Î8*Ø:.Ô6*Î5'Î8)Ñ>.Ñ>.Ë8(Ç/!Ò6)Ó5)Ò4(Ò6)Ñ9+Í<+È=*Ä;)Ä:-Ä:/Æ90Æ9/Ç:1È;1É<3Ë=3ÓD<ÓE;ÐA9Ë=3Ë<4ÏA7Ï@8Î;1Î5'Ù5)â5.ë31ò/5÷+6ù)7ü):ù&9÷&;÷&;ø'<û&<ý%:ÿ$:ÿ#:ÿ"7þ#7ú%7ù,;÷5>ò9?è8:à87Ü94Ü:5Þ;4à;5å95ç85é73ë54î47î47î66ê65æ72á6/Ü5,×5*Ø8*Ø8(Ø8(Ù8&Ú7&Ü7$Ý6$Ý6$Û5%Ü6(Ý7'Ü9(Ü9(Û:(Ù9)×:+×:1Ð3.ëHKÿoyÿVjøD_ø<_ÿAgÿ3\ÿ8bø>cèMlÅOe€0=J C1-11'.6'4=,=B.B@+B<$E>$HE$U\0p~MŸr—¨|ƒ˜mj„W_{J]~E_ƒCa†@bŠAfCkJl‘Lm’Om’Oo”Nl’IiŽIhHiŒLiŒLhŠNg‰Mf…LdƒJdGe‚HgƒHh„If‚Gd‚DgŠHiŽIl‘L-3'36+891<;6B=9G@:MD=QG=SI=SK>UNDZUQa^eigumklkŠjhb`XVˆPP†QQXYš`cªfj´orÁuxÉ|Ђ…Ö†‡Ù‡ˆØŠ‰Ù‹ŠÖ‹ŒÏ‹ŠÌŠ‰Í‰‡Ð‰…ш„ÑŠƒÑ‰‚ÐŒ„Ï‹„È…Á†º†°‡¥~w–m…„Yj€SZnVTc\RZaOZ_HeZDxUB’LD¸TRÐSWÛRYå^dèejãklæuqäunâoh÷~vÿ‚{ÿ‡~ôl`É?2Á8(À;(¿='¿='À<'Â;'Å:'Ç8(Í7)Ø:/Ù8.Ò8,Ï9+Ì:+É:*È9)É7(Ï9+Ñ7+Ò8,Ñ7+Ï7*Ì8*Ë<,Ë>-Ê<0É=0Ê<2É;/È:0Æ8,Å7-Å5*Ð@7Ð@5Í=4É9.Ë80Î;1Í:2Ì5,Ð/%Ù0)ä20ì25ô/8ù-9û+;ü,<ü-?û.?ü/@ü/@þ/Aÿ/?ÿ-?ÿ.@ÿ)9ü)8÷+7õ0:ñ6=í8=ã77Ü43Ü94Ü:5Þ;6â:7å95ç85ê65ì44ñ48ñ48ï56í76æ72á6/Ú6,×5*Ø8*Ö9(Ö9(Ø9&Ø9&Ù8&Ú7&Ù8&Õ8%Õ:&Ö;'Õ=(Ô=(Ô=*Ô=,Ô<.Ñ7/Ú;8ÿ^eÿaqÿPi÷<[ÿAiÿ4_ÿ4`û1YôKlÛYq•>NZ%-C),:2/-0)08+7@/:B+<<"A<UH(`[5{…S£lª¾‹¨¾Ž¥wo‹[aK^F`„DcˆBeDj“Go”Np•Po“So“So”Nl’IiŽIgŒGiŒLiŒLi‹OhŠNf…Je„IdƒHe„Ig…Ih†Hg…Ge…DiŽIi‘Kl”N+1%.4*470893>:7B=:HA;KD<NH<NG=OJDWSR_^ffeugggf†ba‰[YŠQQ…NO‡RS“[^£ei³lo¾uwÊz|ÑÖƒ…؆‡Ù†‡×ˆ‡Õ‰‰Ó‡ˆË†‡È†…Ë…ƒÎ…€Ð…€Ò‡Ò‡€Ð…ÒŒ…Ë…ÁŒ…¸‹…«„{v|jz„VcPVnUQd^P[eMXbIb^EsXCOE´WRÐUZÚSZç\cèagågjèpoìyræqh÷|tósjþxmæ\QÄ;+¾5#Â;'Á=(Á=(Ã<(Ä;(È;)Ë9*Ï7*Ö5+Ø7-Ô:.Î:,Ç:)Ä8'Æ:)Ê=,Ï=.Ö@2ÙA4Ö>1Î8*È4&Ê8)Ë<,É:,È:,É;/É;-Ê:/È9+È5+Ç5(Ê6,Ï;/Ò>4Ó?3Ö?6Õ>3Î7.É,#Ò+%Ü-*ç02ð39ö1;ú0<ý0?ý0?ù0@÷1@÷1@ö0=÷/<÷-9ú,9ú*7ý/<ù/;õ0:ð3:í6;æ69ß55Ù42Ü94Ý:5ß:6â:7å95ç85ê65ì44ò59ò59ð67í76æ74ß6/Ú6-Ö6*Õ7+Ô8)Ô8)Ô9'Õ8'Õ8%×7'Ô9%Ð9$Î<%Ï=&Ï?'Î@(Î@*Ð?.Ò;0Ñ61ëHIÿfrÿOdÿHfû7[ÿAmý/]ÿ7aò4XãPj¼Ufm19E(*E697325406906=-3;#38CCja:…TŸ®w©Á‡µÎ—¯Ç“’®}v”be†Q`‚F`…BcˆBgFm–Jr˜Or—Rq•Up”To”Nl‘KhHf‹Fh‹KiŒLi‹Ni‹Og†Kf…Je„If…IhˆIi‰Hi‡Gf†CjJk“Mn–P(0#+1'/4-350764<85@=8C>8IE<HE<KHCRPQ[[eabtbb|``‚_^ˆZXŠTTŠTT\] gi³psÄvxÍ{Ø~‚Ûƒ„Þ„†Ý„…؃„Ö…„Ô†…у†Ë‚…ȃƒË‚̓€Ñ…€Ôˆ€Õˆ€Ó…Ò‹„È‹ƒ¾‹ƒ²‰‚¤|“wp€veo~U[|PQnUNf_O]eMZdIaaGq]E‰TF­]TÈZ[ÔVZâ[aæ]déaeîllõzsírj÷xoÞYPå[PÒD8Ã5'Ç:)Å:'Ä<&Å='È=(Ê;*Í:*Ð8*Ô6*Ó0'Ô3)Ò8,Í;,Ç;*Â;(À;(Ã<)È;*ÕC4áK=ÞH:Ó=/Ê4&É5'Í;,È9)È9)È9+È9)Ê8+Ê8)Ë7+Ë7)Í6+Ò<.×?4Ø@3Ú@6ÛA5×:1Ò1)Ü0.å14î49õ6>ù4>ù1>ú0@ø/?ò.<ï/<í/9í07ï-5ï,2ò+2ñ*1õ2:õ3;ò5;ì59å57á55Þ65Ü75Ü86Ý97ß:6ã:7æ87é77ê67ì46ô5:ô5:ñ7:î87æ74à70Ú6-Ô6*Ó7*Ñ8*Ð8*Ð9(Ð9(Ð9&Ñ8(Ð9&Ë9"È:"È>$Ç?%È@(È@*Ì=-Ï;1×96üU\ÿ^qÿGcÿ?aÿ:aÿ7gþ3bû8`íFdÅNa‰@IS12A64@:<:46?56<7179+4="9CSZ.‚‚P¢§q±ÅŠ¯ËŽ°Ì’¦Ã’®}{™egˆQ_E^ƒ@c‰@iDo•Js™Ps˜Sq•Uo“Sn“NkJgŒGeŠEgŠJh‹Ki‹Ni‹NfˆKd†Id†IeˆHgŠJh‹Ii‰Hf‰Ej’Ll–No™Q&.!(0%-2+13.331764:95<;6GD=DD<GFBOOQZZd_ap^`y\\~`_‰\ZŒYX‘\\šgg­qsÀz|р܄ႅ䅆ㄅჃۂՃ‚Òƒ‚΄‡Ìƒ†É„„̃‚Î…‚Ó‡‚ÖŠ‚׌ƒÔ…ÐŒƒÄ‹‚·Š‚«…›xˆsnuqbevWUtSLjVKf_MbfM_eIcbFp^FzQ?[M·ZSÅSR×VZâW\êY`òce÷rmñnf÷qhÑD;ÔA7Å1%È4&ÔC2Ç9%Ç:&É;'Í<)Ð:+Ó:,Ø8,Ù7,Ï,#Î0%Î6)Ë<,Ç@-Á>*¾;'¼7$À4#ÑB2ãOAäN@ØB4Ë7)Ë7)Ð>/Ê;+É:*Ç8(Æ7'È6'Ê6(Î8*Ð8+Ó;.Õ;/Õ9-Ò6*Õ7,Ú<1Ý<2Ü71è88ñ8=÷:Aû9Bú4Aõ/<ò,;ï,:í/;ê19ê38ë48í57ñ56ô36ó57ð37ï6;í9<é69â45Ý33ß76à;9Ý86Þ97á96ã:7æ87é77ê67ì46ô5:ô5:ñ7:ì89æ95ß82Ø7/Ó7+Ð8+Í:*Í9+Ì;*Í:*Í:(Í:*Ì;(Å9"Â:"Ã=$Â>%Ã?(Æ?+Ê<.Ï81ß<?ÿ\fÿQhÿIfÿ9]ÿ=hù.]ý<hé;\å\p¦LV^..G83BC><89?48E26C41=:)>E&FU*bs?›a­½±ËŒ«É‹¦Ã‹º„Ž¬z{™ef‡PZ|@^?a‡>hŽCo•Jr˜Or—Rp”Tn’Rn“NjIf‹Fd‰Df‰Ih‹Ki‹Ni‹NfˆKe‡Jd‡GeˆFgŠHh‹Gh‹GeŠDk“Lm—OpšR(0!'/"(-&).(.0-3317839:4==5?@8DE@JLKORYVXeZ\s]^}YX‚[Y‹^]–ee£nn¶wxÈØ„…⌌ðŠŠì†‡ä‚‚Ü€€Ö‚€Óƒ‚΀ƒÈ„ǃƒË„„ΆƒÐˆƒÓ‹„ÒŒ„ÏŠ€ÅŠ€¼‰€¯‡}Ÿ€xwozkegf]Xm[Qm[Mi\Lf^Kd_IeaHhaGo_FoP;…UA¥]O¿aYÑYXÝQTîS[ø\`ùheæYPÔD<Î70Ñ7/×:1Ö=/Ô;+Ï9(Í:&Ï9(Ñ:)Ô8)Ø8*Ü5,Ü5,Ð/%Í3)Ì8,Ê>/Å@/Á@-¾=*¼9'Â9)¾0"éWJßK?Ã/#Ñ=1Î</Ç8(Ê;+É:)È9(Ç8'È7&É6&Ë5&Ì4&Ð7)×;.Ü>2Ü<0Ù7,Ú8-â>4ìC<õBEò9?ð2<ö4?ö2@ñ-;í-:í2=ì7>æ5;ä68å78ë99ð:9ñ78ï77ò;=î<<ê::æ87á85ß74Ü75Ü75Þ97á98ã99æ::é9;ì9<í9<ï8<ô7;ó6:ð8:í9:ç;9à;5×90Ï7,Ê8+È9)Ç9+Æ:)Ç:)Ç:(È;*Ç<)Á9#Ä='¾:#¶4¼9%ÉD3Ï?6Ê1,ÿ^eÿVgÿIbÿ<\ø1X÷0Yû6cõBiçYo¬FQo33N3,B?6>C<@78@-1L/3K2.A9&DJ(Zn=~š_ž¸w¨Å‚¬ÊŒŸ¾‚˜¶€”²~‚ nf„RXvBY{?]€>c‡AiDn”Im•Ll”Nk’Ol‘NiŽIfŠDc†Bc†Bf‰Ih‹KfŠLdˆJg‹MhŒNg‹Kf‹HeŠEf‹EjIk“Jq›QržSt U*0")/#).').(-/,130561782;<4>?7AD=HJGOPTSUaXYkZ\u\Z^]‰db”kj¤utº~·‡ß‹Œéð‰‰ë…„ ؀~Ҁς‚Ì…†É†‡È‡ˆË‰ˆÎ‹ˆÏŒ†Î†ÌŽ…ÈŠ€¼‰±†|¡‚x‘{s€tknlb`e]Re`Me^Kf^Kh]Ki\Ij^Hk_Gn^Ew_G[C’YF­ZLÇVPÞRSõQXÿX_ðUSãLEÙ>9Õ60Ù6/Ü90Û9,Ö9(Ó8&Ñ8&Ñ8(Ô8)×7)Ü6*ß4,Ý5,Î0'É5)È9+Æ=-ÄA/Á@-¿<*¾9(Ä8)Ë<.äREÜH<È4(Ì:-È:,Â4&Ê=,Ë<+Ê;*Ê;*Ë:)Ì9)Î8)Ï6(Ó7*Ö8,Ù7,×3)Õ1'Ø1(Þ7.ç<5øDGñ8>í/9ï/:ð0=î.;ë1<ë6?ä5:Ü36Ø22ß:8îEBøIFøDCó?>é:7ç98å97â96á85à85á98á98à87â88å99ç9:é9;ì9<í9<î7;ó6:ò59î68ê88ä;8Ü:5Ó9/Ë7+Æ8*Ã:*Â:*À;*À;*Á<)Á<+Á<)½9$Á?)»:%³5¹:'Á;/Ê:2Ø<=ÿ^gÿPdÿA[ù8Wö6[õ;`ô>dãIe«?Lƒ?>_:2G;/:>08>2F<:R>=N0.S8/VK5ciEx[°pÂ}¡Æš¹}š¹€š¶ƒ©xrŽ^YuETp=\{B_@b†@iCm“Hl”Kk“LiMiMgŒGe‰Cc†Bc†DgŠJiŒLhŒNg‹MiOiMiŽKgŒGgŒFfŽEl’Im•LpœQoRt U-1"-1#,/&,.)//-11/34/45-9;0<>1@B7EHAKMLQRWUUaVWi\Yt^]fdŒpn zx·…ƒÎ‹ßêŽïŽŒíŠ‰ç‡„ßÓ|Ë}ȀƅLjˆÆŠŠÊŒŠÉŒŠÉŒˆÅŒ„¿‹‚·‰€­†|¡€v{p€ujpndbi_Vc]M_aL_aKc^Jg\Jl[In\Ho]Gq]EwaIxX?‡S=£VDÇXOãUSøOTþMSâ><Þ<7Ý84Þ71á6/á6.Ý7+Ú7(Ô7&Ó8$Ó8&Õ8'Ø6)Ý5*à3,Ý5,Ë3(Ä6(Â9)Á<+Â?-Â?-Á<+À8(Ã5'ÜJ=ÚF:ÕA5Î</Ä6(Ä;+¾6&Æ:)Ç:)Ç:)É:*Ê8)Ë7)Ì6(Í3'Õ9-Õ7,Ö3*×3*Ú3+ß6/å:3é<8ð<=í49ë07ì18î3<í4<ë7@ê=CÝ7;áAAìNKøYUýZUúQLïB>å84â62à72à72à72á85â96ä;8ä;8á77ä88æ8:è8;ë8<ë8<ë8<ì8;ï58î47ì57ç77â:7Ù:4Ï8/Ç7,Â8+¿:+½;+»<+»<+»<)»<+»<)¸9&½@*³9$±6$¶;+µ0'Ã40ìPTÿ[iÿJ`õ:Uð7Vñ@]ïHfçHdÍNa‚89e?6R@2DA09=,57)>6+K9/E-#R=,g]Bˆ_’«t—»{–Áz–¿{”³xœ¸…š´…€šm\uKHb5Nh8[xB_}?cƒ@hŒDl’Gj’Ih’JgLfKd‰Dd‡Cc†DeˆFh‹KkŽNkOjŽNiOiOiMhJgHh’Hl”Ko™Ms¡Vs£Wv¤Y24&04&01)01+12-23.34.46+9;-;>-?A3DF9IJDMONRQWSS]XVd\Zoda~nl”zw¬„‚ÃŒ‰Ö‘㔑ê“뎋≅قÌ|Ã|¿€~½„‚Á†…Á‰ˆÂŠÃ‹ˆ¿‰…¸‡®…}¤…{€vŽyn~rgmmaak^Vi[Pc\J]`K]aJc^Jh[JmZKqZJtZIv[FsWAxR;ŠQ=ªYFÍ[PãTPðFIñ>AÛ2/Ü5/á51ã60ã5.á5+à6)Û8)Ö7$Ó8$Ñ8&Ô9'Ø6)Ü6*à3,Ú6-È6)½8'¼7&½:(¿>+Â?-Ã;+Ä8)Æ4'éUIÓ<1Ì8,ÑB4¾5%À=+À=+À8(Ä8'Å9(Ç:)È9)Ë7)Ì6(Í3'Ò6*Ô3)Õ2)Û4,ã:3é>7ì?9ì=:è45ì59ï6;î5;ê2:æ39ä7=â<@óUVübbÿmjÿidóVQäA<ß63Ý4/ß61Þ71Þ71ß82á85â96å97æ:8å78æ89è8;é9<ë8<ë8<ë8<ì7<î5:î68ê67å97ß<7Ö<4Ì9/Å9,¾9*º;*¶;)µ<)µ<+´=)³;*³<(³:'·@,­8&­8'³;-«+"Ã54ý`gÿTdúG\ð<Uë=XçG_áOdÒO_´SZvGAXH9KD2FE1BE0=?*:8#;3?3PG*nkHŠ“f•¬t‘´tŽ·s‘ºxž»…¡¸Š§}l‚[K_:AW0Lc7Wr?_{@dƒ@iŠCk‘Fi‘Hg‘GdHeŽJb‡Bc†Bd‡EgŠHjMlOm‘Qn’Rj‘Pj‘Nj’Lh’Jg‘Gi“Gm—KpœOv¥Wv¦Zx¨\78(68*66,56.56056067/68+;>-=@-@C.DG4IK>MNFQPNRRRVUSYX]a_lkius›€}²ˆ†ÇŒŠÓ•’ᔑâÜ‹‡Ó„Ä}º|µ|³~·„º‡„¹‰…·Š„²†¨z›€xs‡znzrfjm_^j[Ti[PjZKfZJb_Lb_Le^Li\Kn[Lr[Kw\K{ZG~UA†R=›RA¶VFÎRHÚG@â88å33Ý1-ß3/â5/ã5.á3,ß3)Ý5(Ü9(Ó8$Ð9$Ï:&Ñ:'Ô8)Ù7*Ý5,Ø7-Ä;+¹:)µ6%·8'½;+Â=.Ç;.Ç7,Ð90õ\TÖ=5É5+ÏA5¸3$·:(¼?+¿:)Ã:*Æ:+È:,Ê;-Ì:-Ï8-Ñ7-Ð3*Ò1)Ö1+Þ71å<7ê>:è;7æ74é75ï;<ð<?ê69ä15ä5:èBFíMOÿxwÿqnñ`[ÝJCÑ83Ñ2.Ü73ç@:à93à91à72à72â62ã73å76å76æ68ç79ê7;ë8<ë8>ë8>è7=ê7;î7;î79ê7:ä::ß=:Ö=7Ë;2Ã;/º8*µ:*³;*¯<)¯<*¬<(¬;)¬;)ª;(¬=*¦8'§9*ª8-¬/)Ë@CÿhrôM^óH[íDYåH[ÙO^ÉT]·TWœXUdM?OL9LG4JE1FD-?B'=D#>H#KV.\g<xT‹™f‘¤m©n²r—¹}Ÿ¶ˆ’§€u†dRcC?P0BS1Oa9Vl=b{Be‚BiŠCjEi’Fe‘FbHcŽGb‡Bf†CgŠHiŒJkŽNmPn’Ro“Sj‘Nk’Oj“Mi“Ki“Ij–InšKržOv¥Wv§Xw¨Y?=.=>.==1==3==5;<4;=2;=/?B/@D-CG.FJ3KL:NOAQQIRRHYWJZZN_^\gerqn‰{y¡ƒ€·‡…Čьӊϊ…ǃº~y¯zw¦yv¥{v¬|y®€|®ƒ}©ƒ}£x—|t‹zrvksrffm_\j\Sk[NlZLl[Kk[Ki]Mg^Mi]Mj]Mn]Ms^Mz]MZI“`O›VG§N@¸J=Å@7Ï6.Ø1+Þ1+á51á6/â5.â4-à2)Þ4'Ü6&Ú;(Ñ9$Í;$Ì;&Î;'Ñ:)Õ9*Ù7,Ó9-Á>,´;(°5#²5#»9)Ä<.É;/Ë7-Ù?7ø[TãIAÎ:0Ì@3¸6&¬3 µ<)¼9'¿7'À7'Ã7(Å7)É7*Î7,Ñ7-Ù<3Û:2Þ93å<7ê>:ê=9ç85ã41è96ë<9ì::è8:ì>@øPPÿbcÿppùheâTPÊ=6À1+É40Ö;7ß=:à;7â;5á:2â94á83ã73ä73å55æ66é69é69ê7;ë8<ë8>ê7=è7=é6:ï8<ì89è8:ã;:Ý>:Ó>7É<2À</¶8)²:)®;)ª;(©<(¨;'¥:&¥:(¥<) 9( ;) 9*¢6*µ>8ÛTXûepîK\ïI]éJ\ÛM[ÉQZ´VV ZRŠ`RPI7HM9NI5H@+;567>O%Lf6lŠTw”\„›e‰šd›f£k”­t˜°|ƒ•mn}\Q`C<J09F,AP1O_;Wj=czDe‚Bj‹Dk‘Fi’Fe‘FaFcŽGcˆCi‰FjKlMlOlOm‘Qn’Rm”Qm•Om–Pl–Lk—Jm™JrŸNs¢Q{¬]{¬]z«\DA0DB3DB6CC9CC;CC;BB6BC3DF0EH-GJ-JM2ON9RQ?TREUSD\ZC\\D\]Oaaaihxrq{z¦~µ†‚¿ˆƒÃˆ„Á„º{®ys¡to—om•plžrn¡vpžwršwr’to…skzrinmb`l_Wj\Qk[Lm\Lo\Mo\Kq[Mq[No\Nm]Nl_Om`Os`Oz_N†ZMŸ_S¤PE®B8º;2Æ6-Î1(Ö2)Ý5,â70á6/á4-à3,à4*Ý5(Ø7%Ô9%Ï:"Ê="É=$Ê>'Í<)Ò;*Õ9,Ï;-¿@-±<(¬5!®3!º8(Ä<.Ê:/Î5-Ø93ðNIñTMÙB9ÌB5¾?.¦1³<(¾<,Á9+Á7*Â6)Å7+É9.Ï;1Ô;3áD=ãA<ä?;ç>;é=;é;:è88ç85ç85æ95æ74é;:ôJJÿ\[ÿdcøc_Ì=9Á82½4.Ã81ÕA=ÞE@ß=;Ù42â;5â;3â94â94ä84å84ç77é77ê69ë7:ë6;ë8<ë8>è7=è7=é6:ë7:é69æ89à;9Û>9Ñ>6Æ<1¾<.³8)®;)ª;(¦;'¤;(£:'£:' 9&¡>+–7%œ=+š9)š2'ÃPKíkmî\fìP^éJ\ßHWÍKS¹RS¤[R’cQ€jUJM8DL7JB/H9$B7FH#Up=g“V~³o‚³qƒ¥h}’YƒYŽ–c‰—d|Œ_XfCGT89E-6B*:D,=J0JX7Wh>e|Fh‚Cl‹ElFi“Gc‘Fa‘GaGf‹FlŒImNn‘OlOkŽNkOm‘Qp—Tq™Sp™SpšPo›NqNt£Rw¦U|­^z­^y¬]IC3JD6IE9IE:IG;IG;HF9HG5IH3JJ2LL2NN4RP;TQ>XRDYTA]Y>[Z>\YH^[Tdahnk~xs“}x {§ƒ}«…­ƒ|¦}wunoi…kg‚ieˆjf‹mhˆmi‚mh|lfrkbgj`^g]Th\Nh[Kk\In]Kq_Kq_Kp]Lp]Np]Op]Ns]Ow]N~]N‡\L’XL¡UH¤F<«9/º7-Ê7/Ó6-Ú6-Þ6-ß6/ß4-ß4,ß4,ß7,Ý7)Ø6'Ñ6$Ï:$Ì<$Ê<&Ë='Î;)Ñ;*Ó:,Î<-¾A/¯<'¨3!¬3 ¶9'Á=.É;/Ì8.Ì2*àA;ø[TàI@ÐD7ÊF7«2¹>,ÊF7ÌB5Ê@3Ë?2Í?3ÔA7ÜE<ãF?â@;â=9ã:7â64ã54ä65å97å<9ä?9à=6Ü:5ß@:êMHêQLÓ@9¸)#¸-&¾5-É>7Ñ@;Ö>9Ú;8ã;;ç==â96â96â96ä86å95ç:6é99ê::ê88ê88ë8;ë8;é9<è8;ç7:ç7:é6:æ68â88Þ:8Ö=8Ì=5Â<1¹;-®9(ª;*¦;)¢;( ;'ž;&Ÿ:(›:'˜?-Œ5"™=.˜8*—/&Í\X÷y|ßX_æXdÚQ[ÊKR¹LO©VPž`UgU€nVPW8?J*<;C=UQ+lvDƒ¢f”Á~‡¼v†¸s|žan‚Mu~Q‚‡^u}V[b@;D)4<%5=(=D2<E29C+CP2Ue>g{HiƒFl‹Gm‘Gj“Gf’GbGcŽGhJlMo’Pn“PlMiŽKjJj’Ls›TsUsSržQržOs¢Rw¦Uz«Zv¦Zu¥[s£YRK;RK;RJ=RJ=RJ=RJ=RJ=RK;UN<VO=WP>XQ?YR@[TD\UE\UEb[HaZHaYLbYRe][kaiqftuj{zn„|pˆs‹s‰|p„uj{mbre]jc`k`_g_\c_Z^^ZY^YUaZRbZOe[Og[Ki\Kk\Ik\Gm]Fm]Fj^HicMqfTqZJuOB‹WJœ\P£YL¬ZL¥N=¦E4§<,±7(Â8-Ñ80Ü41Ý1-ß6/Þ7.Þ7.Þ7.Þ7.Û7+Ø6)×7)Õ8)Ô8)Ò9)Ò9)Ò9+Ò9+Ô8+Í;,½;+¶>-®9(©1 «2!·9*À</Ã9.È5+Î5-Ô:0ÛC8ÝK>ÔH9Â=,·2#ÝPFØJ@ÔF<ÔD;ÚG?ÞG@àC>à<:à74æ87ì::ì::ç77â64à93Ü=7ÙF>ÏB9Ä:/¼4(·1&¹5)¿;/Ã?3Ã:0É<3Ò?7Ú?:â=;ç;9ì89í9:ç7:å8:å8:å99å97å97å95å95à40á51ä65å76æ89ç9:ç9:ç9;ë;>æ9;ß99Ü=:ÖA=ÍB;ÁA6·?1©:)£<+ =*›<(™:&–:%—8&‘8&A0‹<-‡/#‘/&¼LJämoçloÍVZ½LNµNOªQM¢UO›XO—]R•aTŠjSWZ/:M7FTa)~ŽP›°oŸ¼yœ½x’µs~ah€L\mCYdBT]BHP9?D0:=,@C2<@1:>0<C3:C0AN2Rd>]sBgƒHpKo“Kl’Gj’Ii“KgJlPm‘So“Sl“Pm’Mk“Jm–Jm˜IqœLqžMrŸNr¡Pt£Rv§UyªYzª^y¦cm™ZbŽOWN?WN?WN?WN?WN?WN?WN?WN?XO@YPAYPA[RC\SD]TE^UF_VGd[JcZIcYMcYOf[Uj_]nbdpdhreltgpvirvirsfmm`gg[_bXY^[VZZRYVOWTKXTIXTH\VH^WGcZKcZIf[Ig\Hi]Gj^Fk_Gh`IcbMngTv\MSFšVM«VO°RJ·QE¯H9¬C0¬=,±9)¾8,Ë80Õ62Ù40Ý6.Ü8.Ü8.Ü8.Û7-Ú8-Ù7,×7+×7+Õ7+Õ7+Õ7+Ô8+Ô8+Ô8+Î:,¾<.¶>.®9(¦1 §2 ²:)¹>.¾<.Æ:-Ç4*Ë4)Ò;0ÜE:áOBßQCÝOCÛF?Ø@;Ó<5Ò;4Ö=7Ú=8Ü86Ý33ç77ë78î79ë78ç77å97â=9ÛB:ÊA7¿?4¸:,±6'¯5&²8)¸=-¿@1ÅA4Í@6×@7ß=8ã:7é77ï56ñ7:é6:ç6<ç7:æ89æ87å95å95ã:5â92ã:3ã:5ä;6ä;8ä;8å99å99ä:;ã;;ß<=Ù?=ÐA;Ä?6·=2¬;-¥;+ =*œ=+—<)–;(’:&“8%Œ:%€9'€8)‰5*<5¿QP×eeÑ^a·MM¬NL¤TMYP˜\Q—^S—^S˜_VŒfQ`_/Sg*^r3zO™¯n¥¾|™²pˆ bj‚HauBTe;JX7FP7?H59?158-:</AB4=?4:=2=A3:C0@M3Rd>^tEgƒHqMp”Ln’Hk”Hk“Lh‘Km‘Qn’Tm”Sm”Qk“Lk”Hl—Hm˜HpJpJp Ls¢Qu¦Tx©Xx©Xx¨^m—Xb‹QY‚H]TE]TE]TE]TE]TE]TE]TE]TE[RC\SD]TE^UF_VG`WHaXIaXGe]Je]Hd[Jd[Le[Qf[Uh]Yh][i]]j^^k__k__i^\f[YbWS_VQZVMWTKUQHRNCQMBSOCWQCXRB^WG^WEaYFc[Fg\Hi^Hk`JhbLbaMngUy_PˆXN¢[U±VQ²IE¸E@¹@7¸>1·;/¸8+¼8+Ã9,É;/Ñ:/Ù8.Û7-Û7-Û9.Ú8-Ù7,Ù7,×7+×7+Õ7+Õ7+Ô8+Ô8+Ô8+Ò9+Í;,À>0¶>.­:(£2 £3ª:&³>,¹>.Ä?0Ä8+Å2(È4*Ð<0ØF9ÜM?âNDÙ:6Ø43Ô20Ö42Û97á=<æ<<é;<í9<î5:ë27é26è58å99á<:Ù@:¾8-µ:+±6'®4%­5%°8(µ<+»=.ÈD5ÏC6ÖB8Ü=7â94è64í55ï79ê69é69é69ç77ç77æ95å95å:3ã:3ä;4ä;6ã<6ã;8â:9á99Þ88Ý9:Ü<<Ú@>ÓB=È?9¹:1­7+¥7(¡;,<+™<+”;)“:(9&7%Š9&z6#y6%ˆ9, F=¹SOÀWT¸PO«IF¢MHœSL™YP—^S—aW—aW˜_V‹ePsrFzV“¦n¤¹€«À‡ž³{|[[o<?R$AS+AP/?L2:C06<.69058/9;.@A3=?49<1=A39B/@M3Qc=`vGi…Js’Or–Np”Jm–Jm•Nk”Np”Tq•Uo–Sn–Pl”Kl•Il—Hn™IpLpLpŸNu¤Sx©Xyª[u¦Wq X_‰JVEOx>aXIaXIaXIaXIaXIaXIaXIaXI_VG`WH`WHaXIbYJcZKd[Ld[Je]Hf^Gf^Ig_Lg^Of\Pf\Rf\Sh]Wh]Wh]Wh^Ug]Te[QdZPc[N]YMZVJVRFRNBPL@PM>TN@UO?WQAYTA[VC]XDaZGd]Jf_LfaMdcQleUv\OŠ[Q©b\¸ZX¶FD¹<:Ã<8Æ;6Ä:0Â8-½8)¼:*¾?.Ç?/Ó9-Ù7,Ú8-Ù9-Ú8-Ø8,Ø8,Õ7+Õ7+Ô8+Ô8+Ô8+Ô8+Ô8+Ò9+Í;,Á?1¶>.¬9'¢3 3¢8"¬=)µ@.¼?-¿:+Ã7*Å5*Ë7+Î:.Î</Ó:2Þ65à24Û12Ý34à88å;<ê=?ì<>î7<ì38é06æ25å58á77Ú65Î61·5(¯7'®6&¬7&¬7&®9(²:)µ:*ÃA1ÊB4ÔA7Ü?8â;5æ95ë76î87í68ë78ë78ê86ê86è94ç:4æ;4â92á:2ß:4à;7Þ:8Ü:8Û97Ù99Ö<:Õ@<ÒC=ÊA;»;2¯6+¤6'ž7(œ;*˜;)”;)’;(:&8$Œ7#‡9%€=*v5#5'™E:ªPH¬MG©LG¦QLPJ˜UL”ZO”^R•aV•aV—aWgT‰…_™©x­½Ž¦¸ˆ¡ts†YN`8/A->3B%:F.=F3;A3:=2:=4<=599-?@2<>39<1<@29B/?L2Pb<awHj†Kt“Ps—Or–Lo˜Lo—Pm—Op—Ts˜Up˜Ro—Pm–Jm–Hn™Jp›KqžMrŸNt£Sy¨X{¬]w§[mSe’M[…FS|BNw=cZKcZKcZKcZKcZKcZKcZKcZKcZKcZKd[Ld[Le\Mf]Nf]Nf]Lf^Gg_Hh`IiaLiaNi`Oh_Pg^Oj`Ti`Qi`Qh_Nh_Nh`Mh`MhaNd^N`ZL[UGVPBPM>NK:NK:NK:PM<RO>TQ@VS@XWC[ZF]\H^]Kb`Qf^QmWJ†[R¬ic¾c`ÁMMÈBCÐ;=Ô89Ó84Ì70Á9+º=)µC+¼B+Î;+×7)Ø8*×:+Ù9+Ö9*Ö9*Ô8)Ô8)Ô8)Ô8)Ò9)Ò9)Ò9)Ò9)Í;,Á?1¶>0«:(¡6"š4›7 £=&¯B-³;*º;,Â:,Ç9-Ë8.Î:0Ð<0Ù:4é9<ì4<æ39ã28â38á48â38â25é6<é49ç4:ç7:ã9<Û76Ð21À0(´6(­:(­:(«:(«:(«:(¬9&¯7&¸:+Á;/Î>3Ø?7à=8ä;8è96ì87î66î66î66í74ê84è:3ç:3ä<3á:2ß;2ß<5Ü=7Û=:Ù><×=;Ô>=Ï@<ËB<ÅB:¹=5­7-£5(ž7(š:*–;)“<)‘;*:(Š9&‰8%ˆ7$ƒ8%ƒ@-u4"{3%“G:¤QI¡NFžNG¡WN˜UL’XLZN\N]R”`U—aWhW‰f˜¥z©€Žj]kHDS42@&$2-:&0;*5=.9?3<?4=?4:;3::099->?1;=28;0;?18A.>K1Pb<bxIk‡Lu”Qt˜Ps—MqšNq™Ro™QršSršSršQp™Mm—Km˜Ip›LsžOt Qv¢Sx§Y{©^z¨_qŸVd’JY†C]†JW~GRyBe\Me\Me\Me\Me\Me\Me\Me\Me\Mf]Nf]Nf]Ng^Og^Oh_Ph_Ng_Jh`IiaLjbMjbOjbOjaPjaPjaPiaNh`Mh`Kh`Kh`IiaJjbKf_Mb]J^XHXRBSM=MJ9KH7IH6KJ8LK9LM;NO=PQ?QTASVESVE[YL^VIcQEzXN¡ha¶eaÄVUÖQRÙ@Cß:>Ü87Ô70Ç;.¼?+±C*µA(Ê;*Ô7(Õ8)Õ9*Ö9*Õ9*Õ9*Ô8)Ô8)Ò9)Ò9)Ò9)Ò9)Ò9)Ò9)Í;,Á?1µ=/«<)¢9$™6•5›;"§A*²B.¹@/¿;,Ã7*Å2(Ë4+Ò;0ß=8ê5:î3<ë6=é8>ç:>ä;>ã:=â9<ß58á7:â9>á=>Û=>Ñ96Ã2/¶0'¯9+«>*«>*©>*ª=)©<(©:'«8%¯6%¹7)Å;0Ð=3Ù<5ß<7ä;8è;7î87ï75ï75î85ë:4é;4ç<4ä=4â>5à?7ÞA:ÚA;ÙA>ÕA?Ñ@=ÍB?Ã?:»@8³=3ª8-¡5(›5'˜8(•<*‘<(<)Š<(ˆ:&†9'„7%ƒ6$6%}:)t3!~9*—OA£YNœRG•OE˜VJ•YN[MŽZMŒ[MŽ\Q“_T™`W‘gW~xXyƒ^r|ZU`B;E,/;%0;*0<.3=24<16<2<?6@A9?A6<<077+89+>?1:<17:/:>07@-=J0Oa;bxIk‡Lu”Qu™Qt˜NqšNršSpšRršQsœPr›Op™Kn™Jp›LsžOv¡Ry¥X{§Zz¨]x¦]qŸWg”O]ŠEX‚B`‡NY€ISzCg^Og^Og^Og^Og^Og^Og^Og^Og^Og^Og^Oh_Ph_Ph_Ph_Pi`OjaPjbOjbOjbOi`Oi`OiaNiaNiaNh`Kh`Kh`Ih`IhaGhaGhaGf_Le^Kb[I\WDWR?PM:LI8IH6IH6GH6GJ9GK:HL;IM<HO?JN?PQCXRF[OCiRDƒYM–VL°SLÔZYÛIJâ?Bá99Ú72Ï;1Â?-´B*³<$É:)Ó7(Ô8)Ó:*Õ9*Ó:*Ó:*Ó:*Ò9)Ñ:)Ñ:)Ñ:)Ñ:)Ñ:)Ñ:)Í;,Á=0µ=/¬=,£<)˜8 4•9 ¡A)­C-¶A/¿=/À6+Â/%Ç0'Ð6.Þ:8è59î6>ì;Aê@CæAEàBCÜ@AÙ??Í12Ð66Ó:<Ñ=;É;9¾71µ4.­4)ª<-¥>+¥>+¥@,¥>+¤>(¦;'©:'ª7%²7'¼8+Å8.Ì70Ô94Ü=9æ>;é:7ì95ì95ë:4é;4è;4å=4â>4àB7ÝC9ÚC<ÕB;ÏA=Ê?<Ç=;¿>9±<3©:/¢8+œ6(˜7'”8)‘:):(‹=)‰<*‡<)ƒ:'‚9(€7&6%}6$x3#x5%…B2˜UEWK”PCPD•ZL“YMZN\Q\Q’[T•\U˜[VŽbUkbEV`=JS4=G,6?*2=,4>35?66=68?8=B;CF=EG<CD6@>/;9*78*=>0:<17:/:>06?,=J0N`:awHk‡Lu”Qu™Qt˜Nr›OršSpšPq›OrNqœMp›LošKrNu¡Ty¥X«`~©az¥]qœUf‘L_‰G]‡E^ˆIb‰PY€ISzCh_Ph_Ph_Ph_Ph_Ph_Ph_Ph_Pg^Oh_Ph_Ph_Ph_Ph_Pi`Qi`QmdUlcTjaRi`Qh_Pg^Og^Mh`MiaNiaLiaLiaLh`Ih`IhaGh`IgaKf_Le^Kb[H\WDUR?PM<ML:EF4DG4DH7DH7BI9BK:BK:CJ:IK>SQDSPAXL<eK:tE5•H>ÂYSÙROàDEà::Ü75Õ;3Ê>/º=)µ8"É:)Ï8'Ð9(Ò;*Ò;*Ò;*Ò;*Ò;*Ñ:)Ñ:)Ñ:)Ñ:)Ñ:)Ñ:)Ñ:)Í;,Á;0¶:.­<,§>+™9#3“7Ÿ?'£9#°;)À>0Æ<1Ì8.Ð7/Ù<5ã?=ì=Bî?DéCEâDCØ@?Ê;7À50»1.¹/,¼51¾:6»<6³:2ª8.¤8,¡:+¡=-Ÿ>+ ?,¡@-¡@-¡A+£>*¦=*©;*­:(²7(¹5)¿5+É90Ô?9àC>ä=7è;5è;5ç<4å<5ä=4á>5Þ@5ÜD9ØD:ÒC;ÊA9Ä?:¼;6¸85°93¢7-™8(•5'“6'‘:)<*‹:)…8&ˆ=*…<+ƒ<*€<)~:'|8%{6&{6&y1#€8*ŒG8–SC’PB‹L=RE™_S‘WL’YN”[R–]T–]V—\V–YTŒ^QaX;EM(6@8A&>G2<E43=2-7.1817>6BE<GJ?HI;BC1?>*;:&78(=>09;069.:>06?,<I/N`:awHj†Ku”Qt˜Pt˜Nr›OršSpšPqœMqœLqœLp›Kp›LsžOw£V{§\‚­e€«dx£\j•P]‡EYƒA^ˆHePcŠQZJSzCi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi_Sj`Wj`Wj`Wj`Wj`Vj`Vj`TjaRjaRjaPjaPjbOjbOjbOjbMjbMg_Jg_Jf^Ie_Ib]IZXCSP=ML:HI7EH7AE6@D5?E7?G8?G8>F7GK=IK=AE4KJ6\Q;aE/ƒJ9Àj]Üg`ãUSäFEåA@àA=Õ>5É=0Æ=-Æ4%Ë5&Ì6'Í7(Í7(Î8)Ñ;,Ó=.Ò<-Ò<-Ò<-Ò<-Ñ;,Ñ;,Ñ;,Î<-»2(¶7.±;/¨:)š7"”5•7œ9"®A-³;+º6)Á4*Í6/×<7âC?êHEèDEèEHÝCCÉ:6»61´80®8.©4+«9/©9.¤8. 8-›9,–:+“<+’<+–=+˜=*™>+š?,›@-A, A-£@-¢;*¥:(«7(°6)¶6+¾8-Ä;3Ì<4Ú=6à=6â?8âA7âA9ÞB6Ú@6×@5ØH=ÒE;ÇA8»;2²5/ª3-¦1*Ÿ2+™9-’;*‘9+:*‹:)ˆ;)‡;+…<+€9'}8(|9(|9(|9({8'y6&x3$w- ‹A4—OC“MAŽL@‘QE“UJŽRG˜\R˜\R‘UMTL—ZU•XS”WT“gZ]T5IQ*AK(BK,>H/7@+2=-4?14<1HPCZ^PW[JMO:DF.AA'==%:;)>?1;=2:=2;?14=*;H.Pb<cyJlˆMu”Qs—Or–Lp™MršSq›QpLqžKrŸNrŸNsŸPv¢U{§\ªb‚­fx£^j“O`‰G^‡Ea‰JfŽPhTd‰S[€LTyEi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi_Sj_Yj_[j_[j_Yj_Yj`Wj`Vj`Tj`TjaRjaRjaRjaRjaRjaPjaPh`Mh`Kg_Jg_Jd]J^YEUR?ON:IJ:EH7BD6@D5?E7>F7>F7=E6@F8>E5>F1HL5MI0U?'Q:¼sbËcXà_ZëUTçGGâ@>àA=Ø?9Î:0Í9+Î=,Ï=.Ï>-Ï;-Î;+Í9+Í:*Î:,Ð:+Ð:,Ð:+Ð:,Ð:+Ð:,Í;.É?5ÅB:¼B7®=/Ÿ6#–1–1›2¨5#´6(Ã;/ÑA8ÞE?æIDéJFêKHÞCAÛEDÐB>¼<3¯9/©=0¥?1Ÿ=0Ÿ?1œ>2—>0”>/=/Š>.‡@.ˆ?,Ž=*‘;*’<+“=,”?+•>+—>,™>,Ÿ@.¡=-¤:,¨8,¬8+±8-¶:0¼9/Ê<2Ð<2Ó?5ÕA7×C9ÖC9ÔD9ÑE8ÊB6ÃA4¹=3°:0©6/¤5.Ÿ4,™7,’:.Œ=,Š;,ˆ;+†:*„;*ƒ<*<,~;*|;){9)y:)x9(w8'u6'w4$€4'D7˜NC‘K?ŒJ>QD’TIRG“SJ˜XO˜UO—VPœ]X”WRŽSOŠcT]W5Xc9_jBZfBIT66C)4@*<H4OXGYbQ_fTW\FJN7BE*??#<<"9:(=>0:<19<1<@25>+;H.Pb<dzKlˆMu”Qt˜Pr–LqšNršSq›Qs OrŸLpLqžMt Qx¤W|§_ªc{¦aošUcŒJ]†Da‰JeNeNc‹Od‰S[€LTyEi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`Qi_Uj_[j^^j^^j_]j_Yj`Wj`Vj`Tj`Tj`Tj`Tj`Tj`Tj`Tj`Vj`TlaOk`LiaNiaNf_La\HYVCSR>MK<HI9DF8AE6AE6?F6>D6<D5=F57@-@H1IK3F?%W?'‡ZC·s`¶VJÙbZðebìTSæFHëGHæCDØ;6Ë7+É:*É:,É:*Ë9,Ê8)È6)È6'Ê8+Ë7)Ë7+Ë7)Ë7+Ì8*Ì8,Ë8.Â3-¾5/¶6-­3(¡0"ž/ 1 §3$¼A2ÇC7ÖG?áLFçLGéJFåFBßD@×CAÏDAÁ@:±;1¤</¡A1œE4˜E3“D3‘D4B2ŠA0…@0‚A/~A.@.†:*‹9+‹:)Š;*‹<+<+Ž=*<*–?.–=-›;+9* 8+¥9-©;.®9/¸:.¾90Á;0Ä>3Å?3ÅA4ÃA3ÁA4¶;,²:,ª8-£7+ž6+›6,˜7.”:/;/ˆ<,‡;-„;,‚:+€;+€;+<,};+z;*z;,w9*v8)u7(t6)v4&ˆ;1”F<–NBŽJ=ˆH<OB‘SH’RI’OI™TO™SQšVSš]Z“YU[Tl[}X~‰^€‹alxRLX46D#=J.LZ@eqYeoW]eMPU>EI0?C(=@#:<$89';<.8:/9<1=A36?,<I/Pb<dzKm‰Nv•Rt˜Ps—MqšNršSpšPu¢Qp LnLpŸOu¤Vy§\}¨`|§`p™UfM\„E\„EeŒMiQfN_…HcˆRZLSxEi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`QjaRjaRjaRjaRjaRjaRjaRj`Vk`\k__k`^k`\k`ZkaXkaWkaUkaUkaUkaUkaUkaWkaWkaXkaWnbRmbPjbOjbOhaNe^K_ZGYVERO>MK<GH:DF8CE7BD6@D5<C3<H49B-FJ3LF0O:%qL9šcOª^N¡A5ÆSLçc_ð^^íSUëJOéDHáAAÌ91Ç9-Å7+Å7+È8-Ê:/Ë;0Í:0Ñ>4Ñ>4Ò>4Ò>4Ó?5Ó?5Ó?5Ó>7Ó?;ÑB>ËD>ÆF=ÂF<ÃI>ÇM@ÎPDÙSHÝPGâMGäIEäGBâC?àA=ÛB=ÕFBÉD?º?7«=0Ÿ>.šC0•G3”I6F4ŒE3ˆC3…B1€A0}@.zA.|?-‚:,†8,…9+†:,†:,‡;-‡;+ˆ;+‰:+‹:)Ž8)8*•9,™;/=/¢<0ª:/­8.¯9/°:.°:.°:,¯9+®8*¨7)¥7(ž6)›6*•7+’8-‘9/Ž<0ˆ<.…<-ƒ;,9*:*~9*}:*};+|:,z;,y;,w9*s8*q6(r7)u5)ŒD8’H=‘K?ŠH:‡I<‹OD“TK—TL–QL›TP˜QO”SO–\X_Xh^—~j””p…’fu‚WYh?CR+?O*P`<crQetU\iKNX=CL1?E+>B)=A(;='78(:;-79.9<1=A37@-<I/Oa;bxIlˆMv•Ru™Qt˜Nr›OršSo›Pq OožMožMs¢Rz¨]|ªaz¥^u [cŒJ^‡E[ƒD_‡HgŽOj‘PfL`‡Hb‡QY~KRwDi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`QjaRjaRjaRjaRjaRjaRjaRj`Tk`Zk`\k`ZkaXkaWkaUkbSkbSkbSkbSkbSkaUkaWkaWkaXkaWocUnbRkbQkbQkbQhaOd]M`ZJXRBRO>NK<IG8GE6DE5BC3>B1=I1?G/LG1S>+g=-•ZL­_Sœ>4ž3+¶?9ØXUóigödeëRUæJNèNPÙIAÓF<Í@6Ë>4Î@6ÒD:ÕG=×G>ÚJAÚJAÝJBÝJBÞKCÞKCàKDàKEëSRèTTãSRÜROØSLØSLÛULàUNàLHáIFãEBâC@äB@âC@äEBàFDÙJFÊE@¹>7¬=2¡?2˜B1’E3G4ŒE3ˆC3‡B3ƒ@0?1~?0z?/|>/€:.ƒ9.ƒ9.ƒ9.ƒ;-ƒ;-ƒ;-ƒ;-9*‚9*…9+‡:*‰:+;-<.“;/›;/ž9-Ÿ9- :,¡9,¢8+¢8*¢8*Ÿ8)œ8)˜8(”8)9*Œ:,‰:-‡;-„:-‚:,9+€8*}8)}7+|8+{9+z8,y9-x:-v8+q5*o5)q7+t9+ŒH;I<‰I=„I;…K?ŒRF“WM˜XO˜QMœUQ˜RP“VQ’`YˆbWƒg[†zdnrQZi@JY0AR(IZ0Wi?dvNj|VXiEL[:?L.:D)<D,>C-<A-:=,89+:;-68-8;0>B47@-;H.L^8_uFi…Jt“Pt˜Pt˜Nr›Os›TpœQnMmžLp¡Pw¨Y|ªaz¨`rXi“QZƒA\„EaˆIeŒMgŽMgŽKhJfŠLc†PY{IRtBi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`QkbSkbSkbSkbSkbSkbSkbSkaUlbYlbYlbYlbVlbVlcRlcRldQldQldQlcRlcTlbVlbXlbXlbXocUnbRkbQlcRlcRkbQg`Pd]M^WGYSCSM?MJ;KH9GE6EB3?B/;H.?H-OC-a=-ƒE:«XR´NJž,+²:9¹=;ÓSRòpnþvvñehëX^ð^_ãUQÚPFÔG@ÐC:ÑD=ÔG>ÖGAÖG?ÙHCÚJBÜHDÝJCÞJFÞKDàKGàKGâHHáGIÜHHÙGGÖHD×HDØGBÛFBàBAäABæBAèBBçCBäB@ßA>ÛA?ÛJGÊC?¹<6®>3¢@3˜A0A0C3ŠA2ˆ@1‡?1…=1‚<0€<1=1=1;1;1ƒ:3;1;1€</€<1=/€</€=-<-€;,‚:,9*‚8+ƒ7)Œ:.8.‘9-”:/—;.š=.ž</Ÿ>.™9)–9(“:*:+Š;,‡;+„;,‚:+‚:,9+7)~6({5){5){5)z6)x6*x8,w9,s8*p4)o5)r:-v<0‹M@‰K<„J<‚M=…QC‹VH’XM—WN“NI™TO—VR•\Ue\{^PfVF`^GEO->N)?O(J\2_rEk~QexKXj@M_7@Q-6E&7D*=F1=D4:>07;-8:,:;-68-8;0>B47@-9F,I[5[qBeFr‘Ns—Os—Mr›Os›TpœQmœLo Os¤Ux¨\z¨`s [i“Q`ŠHYB^†GeŒMgŽMfJeGgŒGhŒLa„NXzHQsAi`Qi`Qi`Qi`Qi`Qi`Qi`Qi`QkbSkbSkbSkbSkbSkbSkbSkbSlbXlbVlbVlcRlcRldOldOldMldMldMldOldQlcRlcTlbVlbVocUmaSkbSlcTmdUlcTi`Qf_Ob[K^WGXQARL<OI9JG6IC3AC.:G+@F*U@+xG9£PL´HH¶8<º39ÈAEÄ?@ÊJIßb`ñqpðnpçbeâZ\ÛQNÕLDÏD?ÍB;ÏD?ÐE>ÐB>Í@9ÒC?ÒC=ÔC@ÕD?×CAØD@ÙECÚDCÝADÝADÞDFÝGHßIHàJIãIIåGHçACêADìBEìBEçAAâ@>Û=:Õ=:ÚFFÉ=<¹83°;2¦@4˜?1‘>0@1Š</Š<0‰;1ˆ:0‡81†93…:4„;4;3€<3:4€<3<3}=1}=3|<0|<0|=.|=.{<-|:,{9)z8*{7*‚:.ƒ9.‡9-‰9.9.‘;.•<.–=-’9)‘:):*Š;*‡;+ƒ<*€;+~;*€8*€8,~6*}5)|4(z4(z4*y5*u2)v6,v8-r6+o3)o5*s:/x@3ŠPB†N?N=‚Q@…VF‰XIŽUJ‘RI’OI—TN“TO‘\VŠfZn[JPK7EK1AN0KZ9WgC`rJgyOdzLVl>G]/AV-7H$1@!6B*=F5<D98=67:39;.;<.68-9<1?C57@-7D*FX2Vl=a}BoŽKq•Ms—Mr›OtœUqRožPq¢Qu¦Wv¦Zs¡Yj—RbŒJ[…E^†H`ˆIcŠKeŒKfJfŽHf‹Fe‰I`ƒMWyGPr@i`Qi`Qi`Qi`Qi`Qi`Qi`Qi`QkbSkbSkbSkbSkbSkbSkbSkbSlbVlcTlcTlcRldQldOldOldMldMldOldOldQlcRlcTlcTlcTnbTmaSmaSmaSmdUlcTjaRg`Pd]M`YIZSCUN>PK8MH5ID1DC.@F*DD([B,ˆTG¯\X«>A¬/3ÎINÆ?EÀ;>»;<ÄFGØZ[ãefÛY[ÌGHÑJGÐEBÍB?ÐB>ÔFBÔFBÐB>É>7ÏD=ÏD=ÒD@ÔEAÖEBÙECÚDEÝDFÞ?Cß@DÞBEßEEáGGàFFàBCá>?èBDé@Cè>Aç=>ä>>ãA?áC@ßECÖBBÅ98¶50°;4¦?6š>3=/?2ˆ</‡:0‡:0‡81‡83‡83ˆ94ˆ;5‚92€:2€:2;2~<0~<0|<0|<0x8,y9-z:.z:.{<-{<-z;,z;,};/9/€7.7,„6*…5*†7*†7(Š8*ˆ9*ˆ;+…<-„<-€=,~<,};+}9,}7-|6,y5*z4*x4)x4)x5,r2(u5+v8-r6+n4)n5*t;0xB6ŠUG„PB€O@„SDˆWIŒVJŒRGNG•TN”UN‹RK„XMdSeZDGK2=J,IX9ZkIgxTcxQYnESh=Nc8J_4:L&0?,95>)=C5<B8:<79<5;=2;=079.9<1?C58?-8B)EU1Uh;azCnŠMr“Nr–NršQsSqRr Uu£Xv¤[t¢ZošUfN^ˆH[ƒDdŒNa‰K`‡HcŠIfJgŽKeŠEa…E`ƒMWyGPr@i`Qi`Qi`QjaRjaRkbSkbSkbSjaRjaRjaRjaRjaRjaRjaRjaRkbSkbSkbSkbSkbQkbQkbQkbQlcRlcRlcRlcRlcTlcTlcTnbTk]Pp`QtdWrdWnbVj`Th`ShbTgaSgaQc]M[VCVQ=SN8NH2GA)KG,I?$bI3‘gW¶xm»le¸YW¹OO¶BE½BEÂDGÈFHÍHKÐJKÒLMÓKMÏEEÐDEÓEDÖFEÕFBÒC=ÎC<ÏF<ÈD8ÉE9ÊE<ÌE?ÒD@×CCÚADÛ?CáBFâBDáCDàDEàDEáFDãEDäDDãCCäB@ä@?ã?>ã?>â@=â@=ÞC?ÓB?Å>:¶93ª70Ÿ:0—=2Ž>3†>2ƒ?2?3=2;3„93†93ˆ81ˆ92…;0„<.„<.ƒ;-;/€:.€:.€:.~:-~:-~:-~:-};/};/};/};/}90}90~80~8.€7.€7.€7.~8,€8,~8,}9,|8-{9-z8,x8,x8,x8.x8.w7-v8-v6,t6+s5*q5+m1'r6,n4)m3(o6+m4)q8-I=…SH…SJ†RGˆRH‹PHQJ”QK•RL“TMTK‰[N‚cQo_HYU:MV7O_;j~YbxQYoHUjCRg@Mb;IY5CR17D&4>#2:#6;'<>0@B5@@6<>358-39-4:.7:/9=/;?.=B,<E(S_;arFn†Rs‘Ur•QršSsSrUužZ~§e~§ep˜YbŠK^†G^†H^„G`†IbˆKdŠMfNeŒMcŠK`‡H^„G_‚LTvDKm;i`Qi`Qi`QjaRjaRkbSkbSkbSjaRjaRjaRjaRjaRjaRjaRjaRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTnbTrbSqaRrbUqcVrfZogZmeXjdVgcWkhYoiYlgThcOc^H[U=RL4PJ0H>%Q?)kP=XG„NBˆE=“D?±WV¸TT»QQÀPOÀNMÁMMÂLJÃKJÐTTÈHGÂ=>Ä=:ÐB@ÓEAÏB;Ç>4ÊD9ÉE9ËF=ÍG>ÔFBØDDÜAEÝAEáBFáCDáCDâDEâDCáFDáFDáFDàEAßD@àB?ßA>àA=àA=àA=ÞC?ÔE?ÈC<º>6¬:0 90”:/Š<0‚<0€A2~@3~>4<3ƒ:3†91ˆ81‰90‡;.…<-„;,„;,ƒ;-‚:,‚:,‚:,9-9-9-9-~:/~:/~:/~:/}90}90}90}90}90}90|90|90|90z:0z:0y9/y9/y9/w9.w9.w8/w8/t8.s7-r6,r6,p6+p6+k1&q7,m4)l3(o6+l3(p7,~H>„RI…SLˆQJ‹PJQL”PM•QN”SOŠOG…SH€[K{ePsiPhkLfqQgxT^tMXnGPf?La:K_:K\:GV7CP4<F-8@)6;'7:)<=/?@2>>2;=04:03;05;16<0:</<?.>A,>D*Q[9^mDkRpŒRp“Sq™RsSsžVxŸ^|¢exžak‘T`†I]ƒF]ƒF\‚E`†Ia‡Jc‰Le‹NdŠMbˆK`†I^„I]LRtBIk9i`Qi`Qi`QjaRjaRkbSkbSkbSjaRjaRjaRjaRjaRjaRjaRjaRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTnbTqdTo_Pm]Pm_RqeYog\ldYhbVfbWpl`{xi~{jyizubpiVe`JTT<FF.C>(MB.S@/U8(]7*l=3n70u91~;5‡@:’IB›PJ¥XP­ZR»^YºSN·GE¼B?ÈDBÑFCÓD@Ï@:ÐA;ÐC<ÒE>ÔG@ÖGCÚFFÛEFÞDFßCDßCDßCDßCDàDEßEEßECßECÞDBÞDBÜD?ÛC>ÜC=ÜC=ÜC=ÛC>ÖE@ÎE?ÃB<³>5¢:1”8-ˆ9,ƒ;-‚@2@1~>2=1ƒ:1†91‡81ˆ:0‡;.„;,„;,„;,‚:,‚:,€:.€:.9-9-~:/~:/~:/~:/~:/~:/}90}90}90}90|90{8/{8/{8/{;1{;1z:0y;0x:/x:/x:/x:/t8.t8.s7-s7-r6,q5+o5*o5*j1&o6+m4)k2'l6*i3'm7+}G=‡PI‡PI‹PJŽQL‘RM’SN”UP‘VPŒWO‡[P~^OtbNoiQorUlwYfwUQeBK_:EY6DU3EV6GV9GT8ER8?I0<D-9>*7;*:<.<>0;=/9;.6<25=26<26<0:</<>0=@-=C)MW5Zi@f|MlˆNn‘Qq™RuŸUtŸWz¡`w`n”Wc‰L\‚E\‚E\‚EZ€C_…Ha‡JbˆKdŠMc‰La‡J_…H]ƒH[}JPr@Gi7i`Qi`Qi`QjaRjaRkbSkbSkbSjaRjaRjaRjaRjaRjaRjaRjaRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTnbTrdWoaTm_Rk_SkaWlcZle[jf]jf]xtiˆ„x‘•’Œ~‰‚r}zg`cNOT>DG2FE1HC0F=,L<,VC4P9+R6*V4*Y5)\6+a9/e=1m=1ŠKBSJ®UO¶NK¼FDÆABÒDCØFFÔ@>ÕA?ÖB>ÖE@ÙECÚFDÛEDÝEDÞDDßCDßCDÞDDÞDDÝEDÝEBÞFCÛFBÚEAÙD>ØE>×D<×D<ÙD=ØE>ÔD<ÓF?ÉF>ºA8§;1˜8,Œ9+…<-ƒ@0@1~?0=/ƒ:1„:/‡9/ˆ:.„:-„;,„;,ƒ:+‚:,‚:,9-9-9-9-~:/~:/~:/~:/~:/~:/}90|90}90{8/{8/{8/{8/x8.{;1y;0y;0y;0x:/x:/x:/v:/s7-s7-s7-r6,q5+o5*o5*m4)j1&o6+l3(h2&k5)h2&l6*|F<ŠOGŒOJPKRMSN‘VPWPŒZQ‰]R…aUy_PiZGd_IdhO\fKN]@EU8AQ4=M0<K.?M3CQ7FR:GS;BL4>G2:A/9=,9=.9=.8<-7;-7=36=56;46<2:<1;=/<?,<A*JS4Ve>bxIj†Ln‘Qs›Tw¡Wx¢Z{¢ar˜[e‹N\‚EZ€C\‚E\‚EZ€C_…H`†Ia‡JbˆKa‡J`†I^„G]ƒHY{HNp>Eg5haQhaQhaQibRibRjcSjcSjcSibRibRibRibRibRibRibRibRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTlcTrfXth\sg[mcYjaXle]snhzwp~w† ”¨¥œ«§œ¨¤™£‘™–‡z{kcfSOR?GJ7EH5BE2BE2EH5KL<JH9GD5D>0A9,A7+@6*F4(S5*qB8ŒPHŸPK¯IG¿GIÌEIÓBEÖ@BÙ@BÚBAÛCBÚEAÚEAÜDAÜDAÞDDÞDFÞDFÞDFÞDDÝEDÝEDÚDCÚFB×FAÖF>ÔE=ÔE=ÓF=ÔE=ÕF>ÔA:ÓC;ÎE=ÁB9®>3ž9-‘;.Š</„?0?/€>.€=-;/„:-†:-‡;.„:-ƒ;-‚:,‚:,‚:,9+9-9-~:/~:/~:/~:/}:1}:1}:1}:1|91z:1|91y90y90x8/x8/v7.y:1y:1x90v:0v:0u9/u9/t:/r7/q6.q6.p5-o4,n3+n3+m4+j1(m7-j4*h2(j4*f2'j6+}D;NGNHQJTLUOŽWPŠYRƒ[Q{YMv\OkZJ]UBYXDY^HOYA?K3:H/7E.4B+5A+8D.=I3CL7EN9BK6?H5<E2:A/8?/7>.5<,4:,5<45<56;56;49;0:</;>-;@)GP1Ra:`vGi…Ko’RuVy£Yy£[xŸ`n‘W`ƒIZ}C[~D^G^G]€F`ƒIa„Jb…Kb…Ka„J`ƒI_‚H^IW|ILp@Bf6haQhaQhaQibRibRjcSjcSjcSibRibRibRibRibRibRibRibRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTlcTkaUndZpf\lcZkd\rmg„|‘Œ§£ µ±®ÄÁ¼ÉÆÁÉľÅÀºÀ¹±¹²¨¤œˆ‚rfcTPQ?FI8>E3;D1:F28E38E38C2:B3<B4@C8CE:GE9>4(P:/e?6€EA¡PO¿X[ÊQVÊCIÕFJ×EFÚFFÛEDÝEBÞDBÝD?ÞCAÞDDÞDFÞDFÞDFÜDCÜDCÜDCÚDCÙEA×FAÕF@ÓF=ÒE<ÐF<ÑG=ÔE=Ô?8Õ@9ÐC:ÆC9¶A7¥=0–=/Œ=.†>/‚?/>.€=-<-ƒ;-„:-…;.‚:,‚:,‚:,‚:,9+:+9-~8,~:/~:/~:/~:/}:1}:1}:1}:1z:1z:1y90y90x8/v7.v7.t8.w8/u9/u9/s9.t8.s9.r8-r8-q6.q6.p5-o4,n3+m4+l3*j4*h2(l6,i3)f2'h4)e1&i5*{B9ŽMG’MH‘PJTLVO‡XNYO|\Qs\Nk[L`WFVSBPSBJQ?@I64@,3<+2;*09(09(2;*5>-9B1<E4?H7?H5>G4:F2:C25@/3>-1<,3:33954954928919;.;>-;@*CL/O^7]sDi„Mp“SwŸYy£Yw¡Yq˜Yf‰OZ}CX{A\E]€F]€F^G`ƒI`ƒI`ƒI`ƒI`ƒI_‚H^G]€JV{HJn@Ae7haQhaQhaQibRibRjcSjcSjcSibRibRibRibRibRibRibRibRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTlbVj`VjaXkdZkg^upjˆ…€Ÿžš°°®ÃÂÀÓÒÐâáßçæäçãàãÞÚÞÖÓÙÎÈμ²¸¥——‡zym]_YIKL:@E1;D/>J6=H7=F5;C4;>5:;3983880==3C<2N71a84ˆHH¯]_Å`dÊWZÍQSÓNO×MKÛKJÞHGáFDãDAâBBßCDÞDFÞDFÝCEÝCEÜBDÜBBÛCBÚDCØD@ÕD?ÓF?ÒE<ÐF<ÐG=ÔE=Ù@:Ù@:ÑB:ÈC:¼C:¬@4™;/Œ9+‰=/ƒ>.>-€=,<,ƒ;,ƒ;,„<-‚:,‚:,‚:,9+9-9-~8.}9.~:/~:/}:1}:1}:1}:1}:1}:1z:1y:1x90x90w8/t8.s7-r8-t8.s9.r8-q8-r8-p7,p7,p7,o6-o6-n5,n5,m4+l3*k2)i3)g1'k7,h4)e1&h4)b0%f4)zA8MG’MH‘PJŽULˆXNYOxZOr\Nk^N^WGRPAKN=CJ:9B12:+08)17+17-06,/5+/5)08+2:+4<-9D4;F5=H7<I7;F56C13@,1>-.800621622717829;0:<.:?)?H+KY5[qChƒLq“Vwž[w¡YsœVgP^IUx@WzB[~F[~F[~F]€H_‚J_‚J_‚J_‚J^I^I]€H]€JUvIJk@@a6haQhaQhaQibRibRjcSjcSjcSibRibRibRibRibRibRibRibRkbSkbSkbSkbSkbSkbSkbSkbSlcTlcTlcTlcTlcTlcTlcTlbVpg^ng_lgatqj‡„¡ œ¼¼ºÎÐÏÏÏÏàààòòòúøùû÷ö÷óðñìéðâßèÐÆÝÁµÁ«¢‘}ub^\GKM7CH2AI2AI4@G5BD7AA7B=9C97?:69<5>?7B71J.+d66QR³giÆnmÀ\\ÇZWÐVS×QNÜLKâHHæCDåBCßCFÝDFÝDFÝCEÝCEÜBDÜBBÜBBÜDCÙCB×C?ÓD>ÒE>ÐF<ÐF<ÔE=ÜC=ÛA9ÒB:ÊD;¿E:°A6œ:-‹5&Š</…=.‚=-€=,<,<,ƒ<*„<-‚:,‚:,‚:,:+9-~8,}9.}9.~:/~:/}:1}:1}:1}:1}:1{;1y:1w;1x90u9/t8.s9.r8-q7,r8-q8-p7,n8,p7,m7+o6+o6+o6-o6-n5,m4+l3*j4*i3)h4)f2'k7,h4)c1&e3(b0%e3(xB8ŽMG’OIŽRJŠUMƒXOzZOq[Mh\LZTDIJ:?C4>D69A208+/7*6<247058157247025.06,08-19,6A3:E5=J9>K9<I77F32A,1>-,6..400511606718:/:<.:?+<E*IW4YoAg‚Mp’Vv\užXp™U`†IX{CSv>WzB[~FY|DY|D\G_‚J_‚J^I^I^I]€H]€H]LStIHhA?]7icSicSicSicSicSicSicSicSicSicSicSicSicSicSicSjcSkbSkbSkbSlcTlcTmdUmdUmdUmdUmdUmdUmdUmdUmdUmdUldWibZgd]vsn“’Ž¯¯­ÄÆÅÚÜÛêîïóôöö÷ùüüþÿÿÿÿÿýÿûúü÷ôýòì÷áÔòØÇàʵƶŸª ‡ˆ„ibbFEH-DH/CG0FE1GD5F<3C60F42H:9>=9>?:B:7I75cGF’jjÅ“’è­©Ò‹‡ÇtnÀ]XÆSPÕONßJLçFKéFKßEGßIKÚDFÔ;>Ø>@áGIàDGÖ:=Ú@@Ú@@ÙA@ÖB>ÔC>ÑB<ÐA;ÑA9ÞC>ÛA9Ó@9ÊA9¾B8°@5Ÿ:.8*Œ=0‡>/ƒ>.>-€=,<+=*<,:+9-9-~8,~8.|8-|8-|8-|90|90|90|90z:1z:1z:1z:1y=5w<4w;3u:2t91q8/p7.p7.o6-m7-m7-l8-m7-l8-m7-m7-i2+m6/p92o81k4-g2*g2*h3+e0(i7.d2)]-#`0&_/%b2(s>6ŒOJPKˆQJRJ}YMv_QfYITN>DE5>E5:C25@04</3;04:04:068378366446135016//6..6+.9+2=-6C2:G6:I68G44C.1?.*4+,2.-2..3-45/79.:</:?+6>&DR1WlCh‚Rs”]wž_r›Wk”R[~DXyDTu@Tu@WxCZ{F[|GZ{F^JbƒNbƒN^J\}H_€K`L^~MTsJFd@<W4icSicSicSicSicSicSicSicSicSicSicSicSicSicSicSicSkbSkbSkbSlcTlcTmdUmdUmdUmdUmdUmdUmdUmdUmdUmdUldYgd]onj†…ƒ¤¤¢ÁÃÂÖÚÛêîï÷ûü÷ûþøüÿüýÿþþþÿÿýÿþúÿüùÿúóÿðàûëÔíßÅ×ͲÀ»¤£„‚ƒcgjKLQ3GJ/EC.GB/H>2I:3L95K<9?:7BA?MHE]RPyjg£Ž‹Î²®èÅ¿ÿ×Ñ쳪͂|Àc^ÊVVØRSßHMÚADÖDEÕEEØHHÜJKÛEFÕ<>Ö<>ÛACÙ??Ù??ÙA@×A@ÖB>ÓB=ÒA<Ó@9ÜA<Û@;Ó@9ËB:¿C;±A6 ;1’8-Œ=0‡>/ƒ>.>-€=,<+<,<,:+9-9-~8,}9.|8-|8-|8-|90|90|90|90z:1{;2{;2z;2w<4u<3u:2s:1r90n8.o6-m7-m7-l8-l8-l8-l8-j8-l8-l8-k6.n70o81n91l7/i4,g2*e3*c1(d4*_/%^.$b2(^.$`0&p>5JE‡RL‡XR~XOrVKdRDQJ:>?/:A16A05@03>04<14<15;17:379477577557246116//6./7,.9+1<.4A08E49H58G44E24B1.8/.5..3--2+23+46+8:-9=,7?(DQ3WlEh‚Ut•`wbp˜YgPY|DWxCTu@Tu@VwBYzEYzEYzE[|G_€K_€K\}H[|G^J^J^{MSnKF_A9R4icSicSicSicSicSicSicSicSicSicSicSicSicSicSicSicSkbSkbSkbSlcTlcTmdUmdUmdUmdUmdUmdUmdUmdUneVneVlfZed_y{x—™˜²¶·ÍÑÒäéìôùüúÿÿûÿÿüÿÿýþÿþþþÿþüÿÿûÿÿúÿÿôþüçúùÝïðÑáåÄÒØ´¼ÄŸ ¨ƒ‡‘l`gEQX7DG,DC.JD4LD7KA8I>:C;9SJKia_wvœ’¾³¯ÙÎÈêÜÓþèÝÿóéÿäÛð©£ÃecµBEÇDIÚRVÛUTÑMKÊDAÍCAÔFE×EFØDDÙCDÖ=?×>@Ø@?Ö@?ÖB@ÓB?ÓB=ÔA:Ù@:Ù@:ÒB:ËE<ÀD<²B7¡<2“9.Ž<0‰=0ƒ>/>.=-~<,€=-€=-9-9-~:/}9.}9.|8-{8/{8/}:1}:1{;2{;2{;2{;2z;4x<4t;2q;1r90o9/n8.l8-l6,k7,l8-j8-j8-j8-j8-i9-j8-j8-n91m80m80m80m80i7.f4+c1(d2)a1']-#_0&c4*\-#_0&sD<}PJVP~YQpTI^J?OC7?<-46(2;*0;+1<.2=/4<14<15;17:379479668357257227007/08-.9+0;-2?.6C27E48G48G49F54>33:2/4-,2(/0(13(57*6:)6>'DQ5YmJk„Zw—fwœfl“Z_‡KWxCWuCTr@Tr@VtBXvDYwEYwEZxF]{I_}K]{I]{I_}K^|JZwKPgJAW@3I2icSicSicSicSicSicSicSicSicSicSicSicSicSicSicSicSjcSkbSkbSlcTlcTmdUmdUmdUmdUmdUmdUneVneVneVneVmg[jkfƒ‡ˆ£§¨¹¾ÁÐÕØåíïóûýõýÿûÿÿûÿÿüÿÿüþûýþùþýøÿþ÷ÿÿóøýæôýÞî÷ØæòÎÝéÃÎÚ´¶Á™¡«†€‡efmLMQ6FH0HJ5KI:HD9D@7OGDj`_‹€¤š™·¯¬ËÆÂÞÛÔêæÝúñèÿûñÿ÷íÿÝÕû¶±åŽÊcd°@?ÃPKÉTMÉPHÃF@ÈGBÔMJÖKHÐ@?Ò>>Ó??Õ?>ÕA?ÕA?ÓB?ÓB=ÓB=Ö>9Õ@:ÒC=ÌE?ÀE>²A9¢=5•;2Ž<1‰=0ƒ>/>.=/~<,€=-€=-9-9-~:/}9.}9.|8-{8/{8/~;2~;2|<3{;2{;2{;2z;4x<4r90o9/o9/m9.l8-k7,j6+h6+i7,i7,i7,h8,h8,h8,h8,h8,m;2l7/j5-i7.j8/j8/f4+a1'e5+a2(].$a2(b3)Y* a2(yNE\VvWRiNGWC:I<3@:.:;-8<.08)/:*3;.4</5=25;169069079468368349238139/19.19..9+/:,0=,2?.5B17E4;H6<I89C87?428.-3'.0%/1$24&48'4<'ER8]pPr‹d{šnwœifXU|ESsATr@Sq?Sq?Tr@UsAVtBVtBWuC[yG]{I\zH^|J_}K\zHWrIJ^E<M;.?-gdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSicSjcSkbSkbSlcTlcTmdUmdUmdUmdUmdUmdUneVneVofWofWng]qqo‡Œ¤©¬·¿ÂÍÕØãíïòüþôþÿøÿÿúÿÿûÿüýÿúüýõúüñùùíöøêöÿèóÿäðýáîùÙéôÒÞèÅÊÒ­¶¾™£}‚bY]BGL5FK7FJ9CG9AC8c^X…}z«¡ŸÁ·µÌÂÁ×ÏÍæáÝñîéú÷ðüõëÿñæÿòéÿôìÿåß騤·hc¦LAµPDÁYNÇ[QÆQH¿D=ÂA<ËDAË@=Î@>Ð@?ÑA@ÒC?ÑC?ÑC?ÒC=Ó?;ÒA<ÐE@ÉE@½C>¯@9 >5•=3Ž<1‰<2ƒ=1=0=1~<.=/=/~:/~:/~:/}9.|90{8/{8/{8/|<3|<3|<3{;2z;4y:3y:3w;3n70m80l7/l7/k6.h6-h6-h6-i7.h8.h8.g8.g8.g8.g8.h8.k92h6/f4-g5.i70h70e4-b1*c2+b3+_0(]1(]1(V*!b6-{ULtYRaNHN?8A7.=6,;9-9;.9<139-3;.5;/6<06<07:/58-47.69049238139/39/2:/19.19,.9+.9+/:,0;-3>.6C2:E5<G7=E8:B55;/06(02%/1$13%15$2:%GS;buWwk|›rq•g\‚QJp=Oo>Qn>Qn>Qn>Qn>Qn>Sp@Sp@WtD[xH]zJ]zJ_|L_|LZwGRlECU?6D7(6)gdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSicSjcSkbSkbSlcTlcTmdUmdUmdUmdUmdUneVneVofWpgXpgXng]lnmz‚…“š «µ·ÄÎÐÜæèí÷ùôþÿõþýöÿüúÿúûÿùûþóøúíõöèñôãôýêôýèôüåóùßòöÛéìÍØ×¹ÅÄ¥±°’ŽregOMP;GK:FM=GOBKQG{{sŸš”ž¸ØÎÌßÓÓçÛÛóëéü÷ôðïêüüôÿÿöÿýóÿùïÿóêÿçÞÿ×ËØ“ƒ¸eS£J:´TF½WIµF;·@8ÈKEÇC>ÊC?ÌB?ÎC@ÎC>ÎC>ÍD>ÍB=ÑB<ÒC?ÎDAÅD?¸A=ª=8ž;5•<4Ž;3‰<2ƒ=3=2=1~<0=1=/~:/~:/~:/}9.|90{8/{8/{8/|<3{;2{;2z:1y:3x92w81u91n70l7/l7/j8/i7.i7.h6-g7-g7-g7-f7-f7-f7-f7-f7-f7-h70h6/g5.g5.g6/h70g6/f5.a2*e6.^2)\0'`4+a5,i=4uSJXG@E>6<5/95,;8/8:/57,36+69.5;/7:/69.69.58-57,36+28,19,28.19,19.19,19,19,.9+.9+.9+/:,0;+3>.6A17B2<D5:C28?/5;-46)24'13%04%07%HT>ex\umt’lf‰_RwKBh9Kk<Nk;Pm=Pm=Nk;Nk;Ol<Qn>VsCZwG]zJ^{K_|L_|LVsCNg@<J9/:2$.&gdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSicSjcSkbSkbSlcTlcTmdUmdUmdUmdUmdUneVneVofWpgXqhYoh^hikmtz…Œ”£¬±¿ÈÍÓÞâåðòðüüòüûõþùøÿ÷úÿöüÿòúýìøùçö÷çööê÷÷ëú÷èüöæýõâ÷ìÖçØÃÕƯÀ±šž“}un[ZWFPQCSVK[bZeld•˜²²¨ÕÎÆèÝÙóããúêëÿñôÿ÷ùÿþûþýùùúôøúïüüðÿÿóÿÿñÿûêÿæÑñª”¼o[©VD®RCµPD»LA¿JAÂG?ÆE?ÉE@ÊE@ÊE>ÉD=ÈC<ÉB<ÑC?ÐE@ÌEAÂC=³=9¦;5œ;5•<6Œ<3‡=4…<5=4<3~<0=1=1~:/~:/~:/}9.|90{8/{8/{8/{;2{;2y:3x92w81v70v70s7/n70j8/j8/j8/j8/h8.h8.h8.g7-f7-f7-f7-d8-d8-d8-f7-g6/h70i81i81h70i81j;3l=5g80k<4b6-a5,oC:xLCtI@nMDC9056.45/8918;247.14+25,58-58-57,46+46+46+46)37)/7(.9)08+.9).9+.9)08)08)19*08)08+/7*08+19,3;.4<-9B1:C0;B0:A1:<.68*24&/3$29'IU?cu[m„gf„bWyTInE?d8Hg;Li;Nk=Nk=Li;Li;Mj<Ol>UrDYvH[xJ]zL^{M\yKQn@G_;6B4+4/#)%gdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSgdSicSjcSjcSjbUlbVlbVmcWmdUmdUmdUleUmfVngWogZnh\oh^mjekormtzƒŠ¥®³ÃÌÑÕßáæîñôüþôýú÷ýùùþ÷úÿôüÿòüÿñüÿîýþðûøïþúñÿùïÿúíÿùéÿòßïàËÝηÁ²›¢—|ubb_NXZL]cWmwn|†}¦¬¢¾ÀµÚÕÏîäâüîîÿôôÿô÷þôõÿùùûúøûýøüÿúøÿõôüñ÷ýñÿÿñÿùèÿôãÿÌ»½p›QD©SFµSH²C:ÀJ@ÄH@ÇG>ÉF>ÈE=ÇD<ÆC;ÆC;ÍG>ÌG>ÇG>¼C;­>5Ÿ:2—:3‘>6Š=5†=4ƒ=5=4€<3;0;1;1~:/~:/~:1}90|90{8/{8/{8/z:1z:1x92w81v70u6/u6/q6.m80k90j8/j8/i9/i9/i9/h8.g7-f7-g7-f7-f7-f7-f7-f7-f5.j81m;4k:3j92j:0m>4oC8l@5oD;d<2c=2zVJ†dZyWMbLA>:13814927<54:0/5+25,9<336+25*24)03(13(25*46+47,.6),7)/7*-8*/7*/7*/7*/7*19,08+08-/7,/7,/7,08-08+7?09B1<E4=D4;A38<.26'-4$2;*JVBcr[h|a]vXOmKFfAAa:Hd;Kh<Nk?Nk?Kh<Jg9Li=Nk=TqEWtHZwK[xL]zNZvMNjAC[;2>4*00!''heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjdVjdVjdXldYlcZmeZmeXmfVg`NjeRnkXol]he\feasqr‚†Š“–ž¡«³¶ÃÈËØÝàêîïö÷ùùûúüþýþþüÿÿúýþöúýòùüñúýòûþóùúòúúòùùíüúëþüçù÷ÞéåÊÕÔ¶º¹›˜˜|qtY_dMbkXr}l„‚‘›´¹²ËÌÇãâÞòîë÷óòû÷öýùøüø÷ÿþüþþüþþüýÿúýÿúýÿúýÿúýÿúúü÷ÿÿúÿûöÿêåا £`X›D=µNE¿MCÂF<ÄD9ÊE<ÎI@ÎKAËH@ÇG<ÆG8ÃH8½J8³H8¦E5šB4‘B5‹C5†B5„B6‚@4‚>3‚<2„:1†91†9191~:1|91{80z7/z7/w7.v6-w7.w7.w8/w8/v7.u6-q5+n3+l7/i81i81h70h70g6/g6/g6/f5.g6/i70i81h70g6/e4-e3,h3-j5/m80k90j:0i;.i=0kA3gB2jI:dH:v_O~k\ŠxjŒoRJ=79.4:04:039/39/39/28.28.17-17-36-06,25,/5+14+/5+/4-.5-/4-.5-/4-/4-/4-/4-05./4-/4-.3,.3,/4-/4-/6.1;23=26@58B79D67B45@03>-4?.DQ=WeN[kQPbHEX<@T8BW6Ic>Ke>Kf=Kf=Je<Je:Lg>Oj?UpGYtK\vO]wP]wRWqNHb?;O6&1+&+.$),heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjcYkdZlc\md[meZmfVidQjhSmjWjk[gg_lll~„“š¨¯µ¸ÂÄÑÖÙâçêòóõûüþÿþÿÿþÿþýûÿþúÿÿúþÿùûüôøûòöüò÷ýó÷úñøúïøúí÷úéùûåòõÚßâÃÌЯ´¸—‘–vkqUYaI_kUtoŒ˜Š§œ¿Á¼ÓÓÑççåóóñ÷÷õûûùüüúûûùþþüþþüþþüþþüþþüþþüþþüûÿþõÿÿõÿÿþÿÿÿûúÿíêð¿ºÂ~uŸKA¯M@¹K>ÂL@ÅK>ÃE9¼=4¾?6ÃE9ÄF7ÃJ7½L:³J7¦F6™C2‘C6E7…C7‚B8A7>5‚<4ƒ:3†91„93~92}:2z:1z:1y90x8/v7.v7.u6-v7.t8.t8.t8.r6,o5*m4+l7/i81j81h70i70h70g6/g6/e3,e3,g5.h6/j81j81j81k92k60l71k90k;1k;/j>1j@0iD2gH6kP=gRAufSueŒ…s†ƒrKL<69.39/39/39/28.28.28.17-17-17-17-06,06,/5+/5+/5+/4./4./4./4./4./4./4./4.05//4..3-.3-.3-.3-/4./6/.80/:21=34@66B66B66B45B16C1CP>P]IR`IIW@AO6BP7EU:Jb@Jd?Ke@Ke>Ic<Hc:Id;Je<SnEXrK\vQ]wT[tTTmOC\>6I5&1-',0$)-heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjcYkdZlc\md[meZmfVmhUkiTmjWjk[kkcwww‘’—§¬²¿ÆÌÒÜÞìñô÷üÿþÿÿþÿÿÿþÿÿþÿþýûÿþúÿÿúýþøúûóõøïñ÷ëñ÷ëô÷ìõùëöøêõøåõ÷áîîÔÛÛ¿ÉÊ«°±’“–yuy`kpZt{iˆŸ§œ®µ­ÍÏÊÞÞÜïïíøøöûûùþþüþþüýýûþþüþþüþþüþþüþþüþþüþþüýÿþûÿÿûÿÿÿÿÿÿûùÿúôÿ÷íýÑÆØž”L=¥RB©L;§@1·G;ÏYMÏUJ¼@4ÄD7ÆH:ÃK=¹I;ªB7?3—@7“E;‰@9„A9A8?6~>5}=4<4}=4z;4x<4w;3w;3u:2t91t91t91r90r90r90r90o9/n8.l6,k5+l7/j81l71j81k60i70i70h6/g5.g5.f5.g6/h70i81k:3k:3l;4j;3i:0h<1k?2jC4iD2fE2fI7lVAjYGujV†m‘Ž{€mEF658-28.28.28.17-17-17-06,17-17-17-06,06,/5+/5+/5+/4./4./4./4./4./4./4./4./4./4..3--2,-2,.3-/4..5.+5-+6.-9//;12>24@46B47D38E3?L:ER>CQ:;I28F->L3DT9J_@Jb@LdBKc?Ia=G`9F_8G`9RkDXpL]uS^vVZqTPgK=T8.A.&1-',0%*.heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjcYkdZlc\md[meZmfVniVljUlkWlm]pqi€‚ž¡¦¹¾ÄÄËÑÛåçõúýûÿÿüýÿüýÿþüýüûùÿþüÿÿûÿþùûüôõöîîñæéïãçíßíñâðôåñôãñôßòñÜêêÐØؼÇÇ«²±•¡¡‰‘“}“€œž‘ª­¢¶»´ÀÅ¿ÜÜÚééçööôûûùýýûÿÿýÿÿýýýûþþüþþüþþüþþüþþüþþüþþüþþþûûýÿþÿÿþÿ÷òïüóìÿüóÿûíÿôäìñ¹p•P@¢O?¸XJ·M?µE9ÀG<ÁA6ÈE;ÅI?½G=¯@9¡<4š=6–A<?;‡@<@:}@;z?9y@9x?8x?8v=4v=4v=4u<3s=3r<2r<2q;1o;0o;0o;0n:/k9.j8-j8-i7.m82n72m61l71l71k60i70i70l:3j92h70f7/f7/f7/h91f:1j?6f>4e=1f>2hC3iE5fG3cG2cJ6hV@i\IskV…‚oŽzpq_9=,47,28.17-17-17-06,06,06,17-17-17-06,06,/5+/5+/5+.3-.3-.3-.3-.3-.3-.3-.3-.3-.3--2,-2,-2,-2,.3--4-*4,)4,*6,+7-.:.1=14@25A36C2:G5<I78E12?+2@)8F/>N4DX<E]=K`AJb@K`?G_;H^:F^:SiEXpN`uV^uXZnSLcI9M4):(%.+%*.$)-heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjcYkdZlc\md[meZmfVlgTkiTnmYop`tum…‡†¤§¬¿ÄÊÎÕÛæðòûÿÿûÿÿûüþýþÿÿþÿþýûÿÿýÿÿûþýø÷øðïðèçêßàæÚÞåÕçëÚêïÛíðÛìðÙîîÖèèÐÚØ¿Ìʱ½§·µ ±®¶´§Á¾µÉÈÃÒÓÎÙÙ×èèæòòðúúøüüúýýûÿÿýÿÿýüüúþþüþþüþþüþþüþþüþþüþþüÿþüÿüÿÿüÿÿûúüû÷ýÿùþÿøþÿôÿÿíÿþìøßËÅœŠbP–J:¥L<´P@¼L>¿@7ÆC;ÅF?¿D=±>;¦:7ž:8™>;‘=;Š?<„@=}@=xA<tB;rC;rC;q?6s>6s>6r=5r=5r=5p>5o=4n>4m=3l<2k;1j;1i:0i:0j:0n72p62p62m61m61l71i70i70l;4k:3h91e90e90e90e:1d<2fB6cA5cC4dD5dG5dI6bI3_I2^L6eV?jbMrmW„ƒo„‡r[`L3:(28,28.17-17-17-06,06,06,17-17-17-06,06,/5+/5+/5+.3-.3-.3-.3-.3-.3-.3-.3--2,-2,,1+,1+,1+,1+-2,,3,,6.+5-*4+*4++5,.8-0;-2=/2=-5@/6A05A-1=)1=)5A-8F/=O5@T8FY=H\@J]?I^=K^>K`?TgG[pQcvZauZYkSJ]G6G4&4%$-*$),"'*heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjcYkdZlc\md[meZlfVjgTkjUlnYorasvm…„Ÿ¤¨¸¿ÇÖßäí÷ùûÿÿûÿÿûüÿþÿÿÿþÿþýûÿÿûÿÿúüüôóõêëíâãçÙÝãÕÜãÑäéÓçìÕéíÖêìÔëëÓèæÏßÚÄÓθÓκÌƶËøÐÉÁ×ÒÎàÛØêæåðïíóòðùù÷ýýûýýûýýûÿÿýþþüüüúþþüþþüþþüþþüþþüþþüþþüÿýüÿûüÿøùÿþýþÿýùÿýôÿøïÿõ÷ÿöð÷çÿÿíÿîÛͧ”¤kX¢[GªWE­L;¹D:ÀD<ÀE>¼E?´A>ª=:¢:9š;9•=<?=…?=}@;wB<rC;oD;oD;p?8q?6q?6q?6q?6p>5o?5o?5o@6n?5m>4k<2h<1h<1h<1l<2o83q62p62p62m61l71j81i81h70g80e90e:1d<2e?4f@5dB6^B4aG8cL:dM;cM8`K6]K3]M4]Q9bX?mhRss[€ƒnv{eFO:4=*39-28.28.28.17-17-17-06,17-17-17-06,06,/5+/5+/5+-2,-2,-2,-2,-2,-2,-2,-2,-2,,1++0*+0*+0*+0*,1+,3,.5.,6.*4+*4+*4++5,-7,.9+-8*0;+4?/5@/4?.3?+3>-3@,7F/9K3@O8BT:GW<H[?M]BM`DUeJ\oSdtZ`rZXgRGXE4B1$2%$-*$),"'*heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjdVjdVjcYkdZlc\md[lfZlfVliVlkVkmXkn]lrhy}• ®·¾ÒÛàêóøûÿÿùþÿùúþþÿÿÿþÿúù÷ÿÿûþýøùùññóèéëÞãçØßæÖàèÓäéÒçíÓèìÓçéÑèèÐçåÎàÛÇ×оÚÓÃÙÑÆÞÔËæÝØîäãóéêúñôÿùûúù÷þþüÿÿýýýûýýûÿÿýÿÿýüüúþþüþþüþþüþþüþþüþþüþþüÿýüÿùøÿúùÿþýüÿý÷ÿþîÿùñÿÿòÿýõÿúóùëÿýíÿúèôλ«wbQ:ª\H²N>¸J=¹I>¸I@´G@¯D>¥=:œ:7–=9>:ˆ?9€A:xA:tD:pF:pE<p?8q?8q?8q?8q?8q?8p?8p?8qB:pA9n?7l=5i=4j>5j>5n=6o83r73p62p62n72l71j81i81d8/d90c;1d>3d@4cA5cA5_C5ZE4_N<gVBhWCcS<^O8[O5\R7]V<]Y>nlUtv^|kcmU2>(6B.3;.39/39/39/28.28.28.17-17-17-17-06,06,/5+/5+/5+-2,-2,-2,-2,-2,-2,-2,-2,,1+,1++0**/)*/)+0*,1++2+-4-+5-*4,*4,+5,+5,,6+-7,,6+/:,2=/6A17B27B15@04?.2?-4C.:G3=L5AO8DT:JX?L\BTbI\kTcqZ^mXUbPDRA2>0#/%&/,&,,$**heVheVheVheVheVheVheVheVheVheVheVheVheVheVheVheVjdVjdVjcYkdZlc\md[lfZlfVolYmlWjlWgkZflbr{xŽ˜š¨±¸ÊÓØãìñøýÿ÷üÿúûÿþÿÿÿþÿúùõÿÿúýýõøøîðòåéëÝåéØãêØåíÖæìÒéíÒêíÒèèÎèæÏèãÏáÚÈØÐÃØÎÄÝÒÌêßÛøíëÿôöÿõùÿõûÿùüþüýÿÿýÿÿýþþüþþüÿÿýÿÿýýýûþþüþþüþþüþþüþþüþþüþþüÿþúÿýûÿýûýüúùýüøÿÿõÿÿóÿÿíýúóÿûûÿúÿÿôÿöæÿóßÿãÍ×­•©oY¨WD®Q@¬O>¯OA±OB­K@¤C<™<5•>7?6ˆ?8A7{B7uD6qE8qE8p?8q?8s>8q?8q?8q?8q?8p?8sB;qB:o@8m>6l=5j>5m>6o>7o83q73o83m82m82i81h91f:1f;2d>3d@4bB5`C5]A3\@2WB1TG4[S>f^Gi^HbX?ZS9ZS7\W:ZV;XW;kmUsw^u}fUaI&28D03;.4:04:039/39/39/28.28.17-17-17-06,06,/5+/5+/5+-2,-2,-2,-2,-2,-2,-2,-2,,1++0*+0**/)*/)+0*+0*,1++2+)3+*4,+5-+5,,6--7.-7,,6+.8-2<16A39D69D47B46A10;+2?-5B09F2<I5@N7FT=JX?R`I[hTanZ\iWQ^MBN@/;/",#+1-(.,&,,heVheVheVheVheVheVheVheVifWifWifWifWifWifWifWifWkeWkeWkdZle[md]ne\mg[mgWqn[kmWknYkr`jrgpyv‡‘“Ÿª°»ÄÉÐÙÞêïóöûþüýÿþÿÿÿþÿþýùÿÿúþþöúúðöøëôöèðôãêñßçïØðöÚíòÔëîÑììÒèæÑáÜÉÝÕÈÞÔÊáÔÎèÚÙòääúëîþòôÿ÷ûÿùÿÿüÿÿýþþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþÿúÿþúÿþúþþüýÿþûÿÿúÿÿúÿÿûÿÿþÿÿÿÿýÿûõÿûñÿüìÿûåÿóÛÿêѺ‚i¦bK—O9ŸT?¨ZF£RAJ: M?Ž<0‹=0†>2=/x=/t?/sB3tD6r=5r=7t=8s>8u@:u@:t?9q?8sA:q@9q@9q@9p?8n?7o>7o>7k92m82l:3l;4l;4h<3g<3c=2c?3^>1dG9cG9T=-N9(M8'@1WQ;PN7KI0OK2]Y>ieJeaDVU7XX<]`CosZz€fdlU=I1-9#2>*4</5;14:04:04:039/39/39/39/39/39/28.17-17-17-06,16016005/05//4./4./4./4.,1+,1+,1+,1+,1+,1++0*+0*.5.-4--4-,3,-4--4,.5-.5-+2*-4,07/5=29A6=E8?G<?J<7B25@/3>-1=)2>*9E/BN8IU?O[GVbN]hWYdTLWI<F;/9.)3*)/+(.*'-+heVheVheVheVheVheVheVheVifWifWifWifWifWifWifWifWkeWkeWkdZle[md]ne\mg[khWonZjlVknYkr`jtiq|x‰”–£®´¾ÇÌÒÛàëðô÷üÿýþÿþÿÿÿþüþýùÿÿúþþôûûï÷ùëô÷æðõáêòÝèíÖêïÑæëËäçÊææÌãáÌßÙÉÞÔÊßÔÎçÙØíßßöçêûïóÿôøÿ÷ûÿúþÿüÿÿþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüýÿüûÿúûÿúýÿüýþÿþýÿÿüÿÿüÿÿûÿÿüÿÿüÿÿûùÿüõÿþñÿÿíÿûåÿòÙÿëÑà±—§u\‘[C–ZBšYCšVC™R@ŽG5‹F6ˆE4ƒD3|C2wB0s@/o>/u@8s>8u>9u@:t?9o=6p>7sA:q?8p?8p?8n?7o>7m>6n=6m>6k<4l=5m>6k?6j?6gA6eA5bB5dG9[A2^G7^I8N=+F7$G8%>6!LL4GK2DG,DD*MM1\[?baC`aBYY=dgJsw^sy_X`I:D,/;%7C/6>16<26<26<25;15;15;15;15;15;15;14:04:039/39/39/27127127116016016005/05/.3-.3--2,-2,,1+,1++0*+0**1**1*)0))0))0)*1*+2*,3+.5--4,.5-07/4;39A6?F>BJ?;F8:E57B14?.3?+6B.<H2@L6LXDS_KYdTWbRLVK=G<1;2+5,+2+*0,)/+heVheVheVheVheVheVheVheVifWifWifWifWifWifWifWifWkeWkeWlcZmd[md]ne\mg[khWmlXimVjoYktaiuiq}yŠ˜™¥²¸¿ÊÐÔÝâíòö÷üÿüýÿýÿþÿþüþýøÿÿ÷ýýóúûí÷úéô÷äïôÞçïØåëÑâçÉÞáÂÝÝÁÞÞÆÞÛÈÝ×ËàÕÏäÙ×îââóçëúîòÿôøÿ÷ûÿùüÿúýÿýýÿþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüýÿüøÿüøÿüûÿþþþþÿüÿÿúÿÿùÿÿøÿÿùÿÿúÿÿúúÿýøÿÿöÿÿóýÿïÿúæÿôÜÿøàÿìÓ㿧¬‚j‡YB…Q;’[F—]IS?F2u>)r>)s@+tD0vE4q?4n<3q<4tB9sA8o?5qA7xH>n>4m>4m>4k?4l=3j>3l=3j>3g?5gA6gA6gC7eC7cC6`C5^D5bK;UB1ZI7`S@RG3C;&E=(FA+AE,BH.AE*<@%?B%NN2_`AijKaaEnqTvzagmSJO94<%4=(=F38>28>48>48>47=37=37=36<28>48>47=37=37=36<26<26<25:449349349338238238227105/05//4..3--2,,1++0*+0*).().().().().(*/)+0*,1+160/4.,1+,1+/4.6;4=B<AH@@K=>K:<I78E34A-2?+3A*5C,DQ=KXDQ^MQ^MHTH<H<2>4.8/-4--4-,3,gdUgdUheVheVheVheVifWifWifWifWifWifWifWifWifWifWkeWkeWlcZmd[md]mf\mg[jiWkmXimVjoYjs`hthm|wˆ˜˜¦³¹¿ÊÐÓÜãëðööûÿüýÿýÿþÿÿýÿþùþþöýýñúûí÷úçô÷âîóÜåíÕâèÎÛàÀ×Ú»ÖÖ¼Ù×ÂÝ×ÇÞ×ÍåÛÙìààøìðûðöÿõûÿùþÿúýÿûûÿûûÿýüÿþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüýÿüøÿþøÿþûÿþþþþÿüþÿúÿÿùÿÿøþÿúýÿùüÿùùÿýúÿÿøÿÿøûÿõüþðÿÿíÿüéÿñßÿðÝÿïÚñÒ½´z{R>tJ4uH3vI4zM8}P;yL7nA.e7'{M@sD:qB8sD:oC8g;0d8-g=1i?3i?3i?3g?3f>2f>2f>2e?2b@4bB5`C5_C5_C5[D4ZE4WD3XI6OB/\T?oiSc^HMK4IG0IK3>D*?H-@F*<B&<A#FI,XY:deFmmQxx\tv^[_F@C.6;%8@+<C19?39?59?58>48>47=37=37=39?58>48>48>48>48>48>48>47<67<67<66;56;56;55:45:438238216005//4.-2,,1+,1++0*+0**/)*/)+0*,1+-2,-2,05/.3-+0*+0*-2.2718=9;B:@K=@M;@M;=J68E13@,1?(0>'<I5BO;HUCIVEDPB;G;2>4.:0,6..5.-4-gdUgdUgdUheVheVifWifWifWifWifWifWifWifWifWifWifWkeWldWlcZmd[md]mf\mg[jiWkmXinWiqZgs_drejyt…••£²·½ÈÎÒÛâêïõôùýûüÿýÿþÿÿýÿÿúÿÿøÿÿóþÿñûþë÷úåðõÞæîÖâèÌÙÜ¿ÕÕ¹ÒйÔѾÚÔÈàÙÑêàßôéíþóùÿ÷üÿûÿÿýÿÿþÿÿþüÿþúÿþúþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüýÿþúÿÿúÿÿûÿÿþþþÿýþÿüüÿûüÿûúÿúøÿù÷ÿú÷ÿýøÿÿûÿÿûýÿúýþùùôîÿþöÿýôÿüóÿûîÿ÷çûæÕéÒÀÏ´¡¶š…”uawVCjI6lI6rM;uP>mF7iB3gB2jE5nI9nJ:oK;oK;eA1cB1cB1cB1cB1cB1bC1`C3]F6\G6\G6ZG6ZG6WH5TG4RG3PH3NH2fdM~~frrZVX@JL4GM3<E*<F+?H+AG+AF(DG*MN/TU6xw[{z^nnVUU=CB.?A,>A.:>-;>39?59?58>48>47=37=37=38>48>48>48>48>48>48>48>49>89>89>88=78=78=77<67<66;55:449338227105//4./4.-2.-2.,1-+0,+0,,1--2.-2.,1-,1-,1-,1-.210513764;4;G9=L9@O<@O:<K67F/4C,2A*6E.:I4@O<BP??M>8F71?2-9-,6--4,,3+fcTfcTgdUheVheVifWjgXjgXifWifWifWifWifWifWifWifWldWldWlcZmd[md]mf\mg[jiWkmXinWiqZgs_bpcgxr‚””£²·ÀËÑÔÝäëðöõúþûüÿýÿþÿÿýÿÿúÿÿøÿÿôÿÿóÿÿïúýèò÷àçðÕâèÌ×Ú½ÑѵÍË´Î˺ÖÏÅÞ×Ñìâã÷îóÿöþÿúÿÿýÿÿþÿÿÿýþÿúþÿùþÿúþÿúþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþþýþÿýþÿýþÿýÿþýÿüþÿúþÿùÿÿ÷ýýóýýõþýøþýùÿþüþþþÿýÿÿþÿþùÿÿüÿÿûüûöóÿøòÿýôÿþñÿýíÿúéÿòà÷äÓçÒ¿Ò»©¹ŸŽŸ„srb|]K}^LtUCaD2W:(Y<*[>,X=*`E2`E2`E2_F2`G3`G3`G3]H3\K7\M:\M:YL9XM9UM8RL6QL6NL5QQ9ikS|€gmqXSY?FL2>G,<F+<F+?H+DJ.EJ,DG*HI*LM.yx\tsWfdMTR;LI6KJ6FE3<=-<?49?59?59?58>48>48>48>47=37=38>48>48>48>49?59?5:?9:?9:?9:?99>89>89>88=78=78=77<66;55:4493382382/40.3/-2.,1-+0,+0,+0,+0,+0,,1--10.21/32/32/32-4-3?17F3=L7@O:?N7<K49H18G05D-8G0<K6>M:<J97E61?2-9-+5,-4,,3+fcTfcTgdUheVheVifWjgXjgXifWifWifWifWifWifWifWifWldWldWlcZmd[md]mf\mg[jiWjlWhmVhrZfs_cqdhys…——¥·»ÇÒØÚãêðõûøýÿüýÿýÿþÿÿýÿþùÿÿõÿÿòÿÿðüÿìøûæïôÝâëÐÝãÇÕÕ¹Î̳ÉÄ°ÊÄ´ÐÉ¿ÚÒÏéàãöíòÿ÷ÿÿúÿÿýÿÿþÿÿÿýýÿúüÿøüÿøýÿúþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþþÿüÿÿüÿþýÿýÿþúÿúøÿùøÿõøÿóøÿóøÿõüÿøýÿúÿýþÿüÿÿüÿÿûÿý÷ÿÿúÿÿüÿÿýÿÿþÿþýûûûóúøìÿÿñÿÿïÿÿíÿþíÿýíÿöçÿîàýêÛúãÑÿæÐãÊ´¥xt^GeO8bL5\H0^J2\J2\J2]K3]K3^L4^L4\M6YN8WO:WO:VP:UO9SN8PN7NN6MO7PT;`fLfoTU^CEO4AK0<F+?I.?I.CL/HN2HM/FI,OP1YX:qmRjfKa\F[V@YTATQ>MJ9BC3=@5;A7;A7;A7:@6:@6:@69?58>48>48>49?59?5:@6:@6:@6;@:;@:;@::?9:?99>89>89>8:?9:?99>89>88=78=77<67<6495273162/40-2.,1-+/.+/.,0/,0/-10.21.23.23-12+1-+9*/@-6H2;M7=O7<N4:L2:L25G/7I1:L6<M::K96G70@3-;.,6--4,,3+cdRcdRdeSefTgeVhfWigXigXhfWifWifWifWifWifWifWkeWkeWkeWldYle[md]mf\kg[jiWikVhmWhrZgt`dqgj{u‰›ª¼ÀÍØÞàçíôùÿúÿÿûÿÿýÿüþÿúþþöúúîúûíúûéøúåóöáêî×ßãÊÙÜÁÓѸÌDZž¬Æ¾±ÌüÖÎËæÝàôêòþ÷ÿÿúÿÿþÿÿÿÿþþþýÿúüÿúüÿúýÿüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüþþüÿþüÿþüÿþüÿýþÿüÿÿüÿÿýÿýÿþûÿüøÿù÷ÿ÷÷ÿõøÿöùÿöüÿøÿÿûÿþýÿüÿÿûÿÿúÿÿúÿÿûÿÿüÿÿþÿþýÿûýüüþûþÿúùúòúüñýûïþúîÿùìÿûìÿýïÿýìÿðÛÿûâìÜÞŽu`P7RB)XH/XH.[M3[M3ZN4ZN4[O5[O5\P6[Q8UO7TO9TO9QO8PN7NN6KM5IM4JN5LR8S\APY<@I,;F(AL.?J,DM0EN1JQ2MR4JM0LL0YY=kjNhdIb^E^ZA_ZD`[GZUAQO:KJ8AB4@B5@B5>A6>A6>A6=@7=@79?59?59?5:@6:@6:B7:B7:B7;@9;@9;@::?8:?99>79>89>7;@:;@9;@:;@9:?9:?8:?9:?98?87=94;4382160/4..3/.3/.21.21,2.,2.+2++2*,4))6%$6)=!2F+8L1;O6;M5;M5;M58J29K3;M5<N6<N87H51B0.<--7,.5--4,]eN^fO_gP`hQdiUfiVgjWijXfgUhgUheVheVjdVkeWkeYlfXlfXkhYmg[kg\mf^jf[ieYgeVghVfkUenYbo]dqhsƒ€–¨¬¹ÈÏÒÛââéïò÷û÷üÿùýüüþùüÿöüþñ÷øêùúêø÷åòñÝîíÙëêÖàßÊ×Ò¾×ϼËÀ®½´¥½³©Â·±ÌÂÁÛÒ×êàèúóûüöÿþûÿÿþÿþýÿüýÿüþýüþýþþþþþþþþþþþþþþþþþþÿþüÿþüÿþüÿþüþýùþýùÿüùÿýúÿþûÿþûÿþÿÿýþÿüýÿýþÿÿÿþÿýýÿüûýøÿÿûüý÷ÿýøÿþúÿýøÿúöÿ÷ôÿùùÿúÿÿüÿÿýÿÿÿÿÿÿÿýÿüýÿþüþýþÿÿÿÿÿÿþüÿþùÿþóÿþîÿýêÿýæúôÜÿúàçàƈlUN2WN1TK.SJ+WN1WN1UN1UN1TO2TO2TN4SO4QM2RN5QO6PP6MO7KO6HM6GL5JP6FO4AJ-=F'>H&DK*FM+EL*PV4PT3KN/HK,TT8bbFccIZZ@[Y@\ZA][B][B[Y@XV=US:SQ8HF1GD1DA0A?0@>1>>2??5>@5;>59?59?59A69A48C58C58C5:B79A69@88@58?79A69@8:B7:A9:B79@8:B7;B:<D9=D<>E=8B:8B:7A88@56>14</39/271.40/51-7/-9-.;)0@&6G'7M';V+:W+;U0;T4;R8=P:>O<>P::L6@R8EW=FY=DX=@T;5H2+<),6+,3++2*ZgM[hN\hP]iQbjSckTglXhkXghVghVifWifWkeWlfXmeZmgYmgYkhYkg[kg\jf]jf[hfZefVhiWglVfo\erajwnzŠ‰œ«²ºÈÑÐÙàáéìò÷ûøþþùþúúÿøûþóùýïøùë÷øæõôâíìØéæÓåâÏÜ×ÄÒ˹ʿ­Á´¤¹­¡¼¯¦Á¶²ËÁÀÛÐÖæßæûóþý÷ÿþûÿÿþÿþýÿüýÿüýÿüýÿþþþþþþþþþþþþþþþþþþÿþüÿþüÿþüÿþüÿýúÿüùÿüùÿýúÿýùÿþúÿþýÿýüÿüýÿýþÿþÿÿþÿÿýþýûüÿþýÿþûÿüúÿøóøíçòåÝöèßûðêÿúýÿüÿÿýþÿÿýÿÿûýÿüýÿþüýÿýþÿýþÿÿýÿÿþüÿþõÿÿîÿþèÿÿäúöÝýûâäàÇ‹‡lRM0SN0QK+QK+UO/UO/TO1TO1SO2SO2QP4QP4QM2PO3PN5NQ6MO7JP6HM6EM5HQ6EN1BK.?I'BI(FM+JP,KQ-NR/PT1VZ7^bA_bCY\?TW<UU=VV>WU>XV=YW>XV=XW;WV:XT9QK3OI3LG4ID1DA2B@3A?3>@5<=59?59?59A67B48C57D37D3:B79A69A68@58@59A69A6:B7:B7:B7:B7:B7;C8<D9=E:<F;=G?<H><F;;F6<D5:B39?38=67>74>63=40>-3C)9M*BY/Ga1Op;Ln;Li=Fa>BY?<R=;L::K8=O9DV<K^BM`BL`DH\@:Q71D.+5*+2*)0(ZgM[hN\hP^jRbjSdlUglXilYijXijXkhYkhYlfXmgYnf[nhZmgYkhYkg[jf[jf]ieZhfZfgWhiWejTdmZgtco|sŽ¬³¶ÄÍÍÖÝÞæéñöúøþþúþýùþøøûòôøêóôæðñßêé×áàÌÜÙÆ×ÔÁÐ˸ÇÀ®¿³£½° ¼°¤Ã¶®ËÀ¼ÕËÊãØÞëäëüôÿý÷ÿþûÿÿþÿþýÿüýÿüýÿýÿþþþþþþþþþþþþþþþþþþþÿþüÿþüÿþüÿþüÿýúÿýúÿüùÿüùÿüøÿüøÿýüÿüûÿûüÿüýÿþÿÿþÿÿýþþüýÿþýÿþûÿýûüóîêßÙâÕÍëÝÔ÷ìæÿúýÿüÿÿýþÿÿýÿÿûýÿüýÿþüýÿýþÿýþÿÿþÿÿÿýÿþõÿþíÿýçÿþãû÷ÞþüããßƉ…jPK.QL.QK+QK+TN.UO/TO1UP2TP3SO2QP4PO3QM2ON2PN5MP5MO7JP6IN7FN6HQ6FO2CL/CM+FM,JQ/OU1SY5QY2W_8jpJy[qvVY^@KO4MP5QQ9QQ9RP9SQ8TR9UT8YU:ZV;XR:VP:RM:NI6HE6DB5CA5?A6<=59?5:@69A67B47B47D37D3:B7:B79A69A69A69A6:B7:B7:B7:B7:B7:B7;C8<D9>F;<F;?IA>J@?I>>I9?G8>F7>D8=B;>E>9C;6@74B19I/BV3Pg=XrB^J[}JXuIMhECZ@9O:6G54E2<N8EW=NaESfHSgKNbF@W=6I3/9./6.-4,ZgM[hN\hP^jRblTemVhmYinZklZklZlj[liZnhZnhZog\oi[liZkhYjfZjf[ie\ieZhfZfgWfgUbgQajWerao|s~Ž–¥¬«¹ÂÇÐ×Úáçîó÷öüüøüûõúôóöíîòäéêÜãäÒÜÛÉÓÒ¾Î˸ÉƳþ«½¶¤Ã·§Ç¹¬ÍÁµÖÉÁßÔÐéßÞóèîøñøýõÿþøÿÿüÿþýÿýüÿüýÿüþýýÿþþþþþþþþþþþþþþþþþþþÿþüÿþüþýûÿþüÿýúÿýúÿýúÿüùÿû÷ÿû÷ÿýüÿüûþúûÿûüÿýþÿþÿÿýþþüýÿüûÿþûÿýûýôïíâÜæÙÑðâÙüñëÿúýÿüÿÿýþÿÿýÿÿûýÿúýÿþüýÿýþÿýþÿÿþÿÿÿýÿþõÿþíÿüæÿüáþùãÿþçäßÉŠ†mPJ0RM0SL/SM-TN.UO/UP2UP2TP3SO2PO3ON2PL1NM1OM4LO4LN6JP6JO8HP8JS8GP3GP3IS1MT3OV4V\8\d=grHtUˆ“i›s…lnuTZaBRV;LN6NN6MM5NL5OM6RP7WS:YU<ZT>XR<TO<QL9KH9GE8EC7@B7=>6:@6:@69A67B47B46C26C2;C8:B79A69A69A69A6:B7;C8;C8:B7:B7;C8;C8=E:>F;=G<=G?<H>>H=>I9?G8>F7>D8=B;=D=9C;6@74B19I/DX5Ri?[uEZ{FWyFTqEIdA=T:2H3/@.->+6H2@R8L_CReGSgKOcG@W=6I30:/07/-4,YgMZhN\hP^jRblTemVinZjo[mn\lm[mk\mj[nhZnhZoi]oi[liZkhYjfZieZie\ieZig[ghXghVchRajWerao|s{‹ŠŸ¦¢°¹ÁËÔÔÝäéñôòúüôúøñöðíðçèìÞâãÕÚÛÉÒÑ¿ÌɶÉıþ«¿¸¦¼³¢Ê¾®ÓŸÜÐÄæÙÑíâàöìíÿôúÿùÿÿ÷ÿÿúÿÿüÿþýÿýüÿûüþüþýþÿÿþþþþþþþþþþþþþþþþþþÿþüÿþüþýûþýûÿýúÿýúÿýúÿûøÿúöþùõÿýüÿûúýùúþúûþüýÿýþÿþÿÿýþÿüûÿýúÿüúÿøó÷ìæòåÝøêáÿôîÿúýÿüÿÿýþÿÿýÿÿûýÿúýÿþüýÿüýÿýþÿÿþÿÿÿýÿþõÿýìÿûãþúßÿüæÿÿéçâÌŒˆoRL2TO2TM0UN1TN.UO/VQ3VQ3UQ4SO2ON2NM1PL1NM1NL3KN3LN6KQ7KP9JR:LU:IR5JS6OY7SZ9T[9]c?fnG‚’c’¦sŸ±›ª’¡zˆ”pr{\\bFLP7KM5LK6KI4LJ5PK5TN8VP:WQ;VP:SN;QL9LI:IG:GE9CE:=>6:@6:@69A67B47B46C26C2;C8;C8:B79A69A6:B7;C8;C8;C8;C8;C8;C8<D9=E:>F;=G<=G?<H>=G<=H8>F7=E6=C7=B;:A:7A95?64B19I/BV3Ne;Uo?Tu@RtAPmAE`=9P6-C.+<*+<)1C-<N4H[?PcERfJNbF@W=5H2,6++2*'.& \ No newline at end of file
diff --git a/test/monniaux/jpeg-6b/testimgp.jpg b/test/monniaux/jpeg-6b/testimgp.jpg
new file mode 100644
index 00000000..8cbb658b
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testimgp.jpg
Binary files differ
diff --git a/test/monniaux/jpeg-6b/testorig.jpg b/test/monniaux/jpeg-6b/testorig.jpg
new file mode 100644
index 00000000..9816a0c6
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testorig.jpg
Binary files differ
diff --git a/test/monniaux/jpeg-6b/testprog.jpg b/test/monniaux/jpeg-6b/testprog.jpg
new file mode 100644
index 00000000..920fee2e
--- /dev/null
+++ b/test/monniaux/jpeg-6b/testprog.jpg
Binary files differ
diff --git a/test/monniaux/jpeg-6b/transupp.c b/test/monniaux/jpeg-6b/transupp.c
new file mode 100644
index 00000000..e5ec5642
--- /dev/null
+++ b/test/monniaux/jpeg-6b/transupp.c
@@ -0,0 +1,928 @@
+/*
+ * transupp.c
+ *
+ * Copyright (C) 1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains image transformation routines and other utility code
+ * used by the jpegtran sample application. These are NOT part of the core
+ * JPEG library. But we keep these routines separate from jpegtran.c to
+ * ease the task of maintaining jpegtran-like programs that have other user
+ * interfaces.
+ */
+
+/* Although this file really shouldn't have access to the library internals,
+ * it's helpful to let it call jround_up() and jcopy_block_row().
+ */
+#define JPEG_INTERNALS
+
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "transupp.h" /* My own external interface */
+
+
+#if TRANSFORMS_SUPPORTED
+
+/*
+ * Lossless image transformation routines. These routines work on DCT
+ * coefficient arrays and thus do not require any lossy decompression
+ * or recompression of the image.
+ * Thanks to Guido Vollbeding for the initial design and code of this feature.
+ *
+ * Horizontal flipping is done in-place, using a single top-to-bottom
+ * pass through the virtual source array. It will thus be much the
+ * fastest option for images larger than main memory.
+ *
+ * The other routines require a set of destination virtual arrays, so they
+ * need twice as much memory as jpegtran normally does. The destination
+ * arrays are always written in normal scan order (top to bottom) because
+ * the virtual array manager expects this. The source arrays will be scanned
+ * in the corresponding order, which means multiple passes through the source
+ * arrays for most of the transforms. That could result in much thrashing
+ * if the image is larger than main memory.
+ *
+ * Some notes about the operating environment of the individual transform
+ * routines:
+ * 1. Both the source and destination virtual arrays are allocated from the
+ * source JPEG object, and therefore should be manipulated by calling the
+ * source's memory manager.
+ * 2. The destination's component count should be used. It may be smaller
+ * than the source's when forcing to grayscale.
+ * 3. Likewise the destination's sampling factors should be used. When
+ * forcing to grayscale the destination's sampling factors will be all 1,
+ * and we may as well take that as the effective iMCU size.
+ * 4. When "trim" is in effect, the destination's dimensions will be the
+ * trimmed values but the source's will be untrimmed.
+ * 5. All the routines assume that the source and destination buffers are
+ * padded out to a full iMCU boundary. This is true, although for the
+ * source buffer it is an undocumented property of jdcoefct.c.
+ * Notes 2,3,4 boil down to this: generally we should use the destination's
+ * dimensions and ignore the source's.
+ */
+
+
+LOCAL(void)
+do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays)
+/* Horizontal flip; done in-place, so no separate dest array is required */
+{
+ JDIMENSION MCU_cols, comp_width, blk_x, blk_y;
+ int ci, k, offset_y;
+ JBLOCKARRAY buffer;
+ JCOEFPTR ptr1, ptr2;
+ JCOEF temp1, temp2;
+ jpeg_component_info *compptr;
+
+ /* Horizontal mirroring of DCT blocks is accomplished by swapping
+ * pairs of blocks in-place. Within a DCT block, we perform horizontal
+ * mirroring by changing the signs of odd-numbered columns.
+ * Partial iMCUs at the right edge are left untouched.
+ */
+ MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_width = MCU_cols * compptr->h_samp_factor;
+ for (blk_y = 0; blk_y < compptr->height_in_blocks;
+ blk_y += compptr->v_samp_factor) {
+ buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
+ ptr1 = buffer[offset_y][blk_x];
+ ptr2 = buffer[offset_y][comp_width - blk_x - 1];
+ /* this unrolled loop doesn't need to know which row it's on... */
+ for (k = 0; k < DCTSIZE2; k += 2) {
+ temp1 = *ptr1; /* swap even column */
+ temp2 = *ptr2;
+ *ptr1++ = temp2;
+ *ptr2++ = temp1;
+ temp1 = *ptr1; /* swap odd column with sign change */
+ temp2 = *ptr2;
+ *ptr1++ = -temp2;
+ *ptr2++ = -temp1;
+ }
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* Vertical flip */
+{
+ JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JBLOCKROW src_row_ptr, dst_row_ptr;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ /* We output into a separate array because we can't touch different
+ * rows of the source virtual array simultaneously. Otherwise, this
+ * is a pretty straightforward analog of horizontal flip.
+ * Within a DCT block, vertical mirroring is done by changing the signs
+ * of odd-numbered rows.
+ * Partial iMCUs at the bottom edge are copied verbatim.
+ */
+ MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_height = MCU_rows * compptr->v_samp_factor;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ if (dst_blk_y < comp_height) {
+ /* Row is within the mirrorable area. */
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci],
+ comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ } else {
+ /* Bottom-edge blocks will be copied verbatim. */
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ if (dst_blk_y < comp_height) {
+ /* Row is within the mirrorable area. */
+ dst_row_ptr = dst_buffer[offset_y];
+ src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
+ for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
+ dst_blk_x++) {
+ dst_ptr = dst_row_ptr[dst_blk_x];
+ src_ptr = src_row_ptr[dst_blk_x];
+ for (i = 0; i < DCTSIZE; i += 2) {
+ /* copy even row */
+ for (j = 0; j < DCTSIZE; j++)
+ *dst_ptr++ = *src_ptr++;
+ /* copy odd row with sign change */
+ for (j = 0; j < DCTSIZE; j++)
+ *dst_ptr++ = - *src_ptr++;
+ }
+ }
+ } else {
+ /* Just copy row verbatim. */
+ jcopy_block_row(src_buffer[offset_y], dst_buffer[offset_y],
+ compptr->width_in_blocks);
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* Transpose source into destination */
+{
+ JDIMENSION dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_x, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ /* Transposing pixels within a block just requires transposing the
+ * DCT coefficients.
+ * Partial iMCUs at the edges require no special treatment; we simply
+ * process all the available DCT blocks for every component.
+ */
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
+ dst_blk_x += compptr->h_samp_factor) {
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
+ (JDIMENSION) compptr->h_samp_factor, FALSE);
+ for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
+ src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
+ dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
+ for (i = 0; i < DCTSIZE; i++)
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ }
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* 90 degree rotation is equivalent to
+ * 1. Transposing the image;
+ * 2. Horizontal mirroring.
+ * These two steps are merged into a single processing routine.
+ */
+{
+ JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_x, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ /* Because of the horizontal mirror step, we can't process partial iMCUs
+ * at the (output) right edge properly. They just get transposed and
+ * not mirrored.
+ */
+ MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_width = MCU_cols * compptr->h_samp_factor;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
+ dst_blk_x += compptr->h_samp_factor) {
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
+ (JDIMENSION) compptr->h_samp_factor, FALSE);
+ for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
+ src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
+ if (dst_blk_x < comp_width) {
+ /* Block is within the mirrorable area. */
+ dst_ptr = dst_buffer[offset_y]
+ [comp_width - dst_blk_x - offset_x - 1];
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ i++;
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ }
+ } else {
+ /* Edge blocks are transposed but not mirrored. */
+ dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
+ for (i = 0; i < DCTSIZE; i++)
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* 270 degree rotation is equivalent to
+ * 1. Horizontal mirroring;
+ * 2. Transposing the image.
+ * These two steps are merged into a single processing routine.
+ */
+{
+ JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_x, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ /* Because of the horizontal mirror step, we can't process partial iMCUs
+ * at the (output) bottom edge properly. They just get transposed and
+ * not mirrored.
+ */
+ MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_height = MCU_rows * compptr->v_samp_factor;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
+ dst_blk_x += compptr->h_samp_factor) {
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
+ (JDIMENSION) compptr->h_samp_factor, FALSE);
+ for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
+ dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
+ if (dst_blk_y < comp_height) {
+ /* Block is within the mirrorable area. */
+ src_ptr = src_buffer[offset_x]
+ [comp_height - dst_blk_y - offset_y - 1];
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < DCTSIZE; j++) {
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ j++;
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ }
+ }
+ } else {
+ /* Edge blocks are transposed but not mirrored. */
+ src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
+ for (i = 0; i < DCTSIZE; i++)
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* 180 degree rotation is equivalent to
+ * 1. Vertical mirroring;
+ * 2. Horizontal mirroring.
+ * These two steps are merged into a single processing routine.
+ */
+{
+ JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JBLOCKROW src_row_ptr, dst_row_ptr;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
+ MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_width = MCU_cols * compptr->h_samp_factor;
+ comp_height = MCU_rows * compptr->v_samp_factor;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ if (dst_blk_y < comp_height) {
+ /* Row is within the vertically mirrorable area. */
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci],
+ comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ } else {
+ /* Bottom-edge rows are only mirrored horizontally. */
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ if (dst_blk_y < comp_height) {
+ /* Row is within the mirrorable area. */
+ dst_row_ptr = dst_buffer[offset_y];
+ src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
+ /* Process the blocks that can be mirrored both ways. */
+ for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
+ dst_ptr = dst_row_ptr[dst_blk_x];
+ src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
+ for (i = 0; i < DCTSIZE; i += 2) {
+ /* For even row, negate every odd column. */
+ for (j = 0; j < DCTSIZE; j += 2) {
+ *dst_ptr++ = *src_ptr++;
+ *dst_ptr++ = - *src_ptr++;
+ }
+ /* For odd row, negate every even column. */
+ for (j = 0; j < DCTSIZE; j += 2) {
+ *dst_ptr++ = - *src_ptr++;
+ *dst_ptr++ = *src_ptr++;
+ }
+ }
+ }
+ /* Any remaining right-edge blocks are only mirrored vertically. */
+ for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
+ dst_ptr = dst_row_ptr[dst_blk_x];
+ src_ptr = src_row_ptr[dst_blk_x];
+ for (i = 0; i < DCTSIZE; i += 2) {
+ for (j = 0; j < DCTSIZE; j++)
+ *dst_ptr++ = *src_ptr++;
+ for (j = 0; j < DCTSIZE; j++)
+ *dst_ptr++ = - *src_ptr++;
+ }
+ }
+ } else {
+ /* Remaining rows are just mirrored horizontally. */
+ dst_row_ptr = dst_buffer[offset_y];
+ src_row_ptr = src_buffer[offset_y];
+ /* Process the blocks that can be mirrored. */
+ for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
+ dst_ptr = dst_row_ptr[dst_blk_x];
+ src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
+ for (i = 0; i < DCTSIZE2; i += 2) {
+ *dst_ptr++ = *src_ptr++;
+ *dst_ptr++ = - *src_ptr++;
+ }
+ }
+ /* Any remaining right-edge blocks are only copied. */
+ for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
+ dst_ptr = dst_row_ptr[dst_blk_x];
+ src_ptr = src_row_ptr[dst_blk_x];
+ for (i = 0; i < DCTSIZE2; i++)
+ *dst_ptr++ = *src_ptr++;
+ }
+ }
+ }
+ }
+ }
+}
+
+
+LOCAL(void)
+do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jvirt_barray_ptr *dst_coef_arrays)
+/* Transverse transpose is equivalent to
+ * 1. 180 degree rotation;
+ * 2. Transposition;
+ * or
+ * 1. Horizontal mirroring;
+ * 2. Transposition;
+ * 3. Horizontal mirroring.
+ * These steps are merged into a single processing routine.
+ */
+{
+ JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
+ int ci, i, j, offset_x, offset_y;
+ JBLOCKARRAY src_buffer, dst_buffer;
+ JCOEFPTR src_ptr, dst_ptr;
+ jpeg_component_info *compptr;
+
+ MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
+ MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
+
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ comp_width = MCU_cols * compptr->h_samp_factor;
+ comp_height = MCU_rows * compptr->v_samp_factor;
+ for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
+ dst_blk_y += compptr->v_samp_factor) {
+ dst_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
+ for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
+ dst_blk_x += compptr->h_samp_factor) {
+ src_buffer = (*srcinfo->mem->access_virt_barray)
+ ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
+ (JDIMENSION) compptr->h_samp_factor, FALSE);
+ for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
+ if (dst_blk_y < comp_height) {
+ src_ptr = src_buffer[offset_x]
+ [comp_height - dst_blk_y - offset_y - 1];
+ if (dst_blk_x < comp_width) {
+ /* Block is within the mirrorable area. */
+ dst_ptr = dst_buffer[offset_y]
+ [comp_width - dst_blk_x - offset_x - 1];
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < DCTSIZE; j++) {
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ j++;
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ }
+ i++;
+ for (j = 0; j < DCTSIZE; j++) {
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ j++;
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ }
+ }
+ } else {
+ /* Right-edge blocks are mirrored in y only */
+ dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < DCTSIZE; j++) {
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ j++;
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ }
+ }
+ }
+ } else {
+ src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
+ if (dst_blk_x < comp_width) {
+ /* Bottom-edge blocks are mirrored in x only */
+ dst_ptr = dst_buffer[offset_y]
+ [comp_width - dst_blk_x - offset_x - 1];
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ i++;
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
+ }
+ } else {
+ /* At lower right corner, just transpose, no mirroring */
+ dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
+ for (i = 0; i < DCTSIZE; i++)
+ for (j = 0; j < DCTSIZE; j++)
+ dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+
+/* Request any required workspace.
+ *
+ * We allocate the workspace virtual arrays from the source decompression
+ * object, so that all the arrays (both the original data and the workspace)
+ * will be taken into account while making memory management decisions.
+ * Hence, this routine must be called after jpeg_read_header (which reads
+ * the image dimensions) and before jpeg_read_coefficients (which realizes
+ * the source's virtual arrays).
+ */
+
+GLOBAL(void)
+jtransform_request_workspace (j_decompress_ptr srcinfo,
+ jpeg_transform_info *info)
+{
+ jvirt_barray_ptr *coef_arrays = NULL;
+ jpeg_component_info *compptr;
+ int ci;
+
+ if (info->force_grayscale &&
+ srcinfo->jpeg_color_space == JCS_YCbCr &&
+ srcinfo->num_components == 3) {
+ /* We'll only process the first component */
+ info->num_components = 1;
+ } else {
+ /* Process all the components */
+ info->num_components = srcinfo->num_components;
+ }
+
+ switch (info->transform) {
+ case JXFORM_NONE:
+ case JXFORM_FLIP_H:
+ /* Don't need a workspace array */
+ break;
+ case JXFORM_FLIP_V:
+ case JXFORM_ROT_180:
+ /* Need workspace arrays having same dimensions as source image.
+ * Note that we allocate arrays padded out to the next iMCU boundary,
+ * so that transform routines need not worry about missing edge blocks.
+ */
+ coef_arrays = (jvirt_barray_ptr *)
+ (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
+ SIZEOF(jvirt_barray_ptr) * info->num_components);
+ for (ci = 0; ci < info->num_components; ci++) {
+ compptr = srcinfo->comp_info + ci;
+ coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
+ ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) compptr->v_samp_factor);
+ }
+ break;
+ case JXFORM_TRANSPOSE:
+ case JXFORM_TRANSVERSE:
+ case JXFORM_ROT_90:
+ case JXFORM_ROT_270:
+ /* Need workspace arrays having transposed dimensions.
+ * Note that we allocate arrays padded out to the next iMCU boundary,
+ * so that transform routines need not worry about missing edge blocks.
+ */
+ coef_arrays = (jvirt_barray_ptr *)
+ (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
+ SIZEOF(jvirt_barray_ptr) * info->num_components);
+ for (ci = 0; ci < info->num_components; ci++) {
+ compptr = srcinfo->comp_info + ci;
+ coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
+ ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) compptr->h_samp_factor);
+ }
+ break;
+ }
+ info->workspace_coef_arrays = coef_arrays;
+}
+
+
+/* Transpose destination image parameters */
+
+LOCAL(void)
+transpose_critical_parameters (j_compress_ptr dstinfo)
+{
+ int tblno, i, j, ci, itemp;
+ jpeg_component_info *compptr;
+ JQUANT_TBL *qtblptr;
+ JDIMENSION dtemp;
+ UINT16 qtemp;
+
+ /* Transpose basic image dimensions */
+ dtemp = dstinfo->image_width;
+ dstinfo->image_width = dstinfo->image_height;
+ dstinfo->image_height = dtemp;
+
+ /* Transpose sampling factors */
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ compptr = dstinfo->comp_info + ci;
+ itemp = compptr->h_samp_factor;
+ compptr->h_samp_factor = compptr->v_samp_factor;
+ compptr->v_samp_factor = itemp;
+ }
+
+ /* Transpose quantization tables */
+ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
+ qtblptr = dstinfo->quant_tbl_ptrs[tblno];
+ if (qtblptr != NULL) {
+ for (i = 0; i < DCTSIZE; i++) {
+ for (j = 0; j < i; j++) {
+ qtemp = qtblptr->quantval[i*DCTSIZE+j];
+ qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
+ qtblptr->quantval[j*DCTSIZE+i] = qtemp;
+ }
+ }
+ }
+ }
+}
+
+
+/* Trim off any partial iMCUs on the indicated destination edge */
+
+LOCAL(void)
+trim_right_edge (j_compress_ptr dstinfo)
+{
+ int ci, max_h_samp_factor;
+ JDIMENSION MCU_cols;
+
+ /* We have to compute max_h_samp_factor ourselves,
+ * because it hasn't been set yet in the destination
+ * (and we don't want to use the source's value).
+ */
+ max_h_samp_factor = 1;
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ int h_samp_factor = dstinfo->comp_info[ci].h_samp_factor;
+ max_h_samp_factor = MAX(max_h_samp_factor, h_samp_factor);
+ }
+ MCU_cols = dstinfo->image_width / (max_h_samp_factor * DCTSIZE);
+ if (MCU_cols > 0) /* can't trim to 0 pixels */
+ dstinfo->image_width = MCU_cols * (max_h_samp_factor * DCTSIZE);
+}
+
+LOCAL(void)
+trim_bottom_edge (j_compress_ptr dstinfo)
+{
+ int ci, max_v_samp_factor;
+ JDIMENSION MCU_rows;
+
+ /* We have to compute max_v_samp_factor ourselves,
+ * because it hasn't been set yet in the destination
+ * (and we don't want to use the source's value).
+ */
+ max_v_samp_factor = 1;
+ for (ci = 0; ci < dstinfo->num_components; ci++) {
+ int v_samp_factor = dstinfo->comp_info[ci].v_samp_factor;
+ max_v_samp_factor = MAX(max_v_samp_factor, v_samp_factor);
+ }
+ MCU_rows = dstinfo->image_height / (max_v_samp_factor * DCTSIZE);
+ if (MCU_rows > 0) /* can't trim to 0 pixels */
+ dstinfo->image_height = MCU_rows * (max_v_samp_factor * DCTSIZE);
+}
+
+
+/* Adjust output image parameters as needed.
+ *
+ * This must be called after jpeg_copy_critical_parameters()
+ * and before jpeg_write_coefficients().
+ *
+ * The return value is the set of virtual coefficient arrays to be written
+ * (either the ones allocated by jtransform_request_workspace, or the
+ * original source data arrays). The caller will need to pass this value
+ * to jpeg_write_coefficients().
+ */
+
+GLOBAL(jvirt_barray_ptr *)
+jtransform_adjust_parameters (j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jpeg_transform_info *info)
+{
+ /* If force-to-grayscale is requested, adjust destination parameters */
+ if (info->force_grayscale) {
+ /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
+ * properly. Among other things, the target h_samp_factor & v_samp_factor
+ * will get set to 1, which typically won't match the source.
+ * In fact we do this even if the source is already grayscale; that
+ * provides an easy way of coercing a grayscale JPEG with funny sampling
+ * factors to the customary 1,1. (Some decoders fail on other factors.)
+ */
+ if ((dstinfo->jpeg_color_space == JCS_YCbCr &&
+ dstinfo->num_components == 3) ||
+ (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
+ dstinfo->num_components == 1)) {
+ /* We have to preserve the source's quantization table number. */
+ int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
+ jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
+ dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
+ } else {
+ /* Sorry, can't do it */
+ ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
+ }
+ }
+
+ /* Correct the destination's image dimensions etc if necessary */
+ switch (info->transform) {
+ case JXFORM_NONE:
+ /* Nothing to do */
+ break;
+ case JXFORM_FLIP_H:
+ if (info->trim)
+ trim_right_edge(dstinfo);
+ break;
+ case JXFORM_FLIP_V:
+ if (info->trim)
+ trim_bottom_edge(dstinfo);
+ break;
+ case JXFORM_TRANSPOSE:
+ transpose_critical_parameters(dstinfo);
+ /* transpose does NOT have to trim anything */
+ break;
+ case JXFORM_TRANSVERSE:
+ transpose_critical_parameters(dstinfo);
+ if (info->trim) {
+ trim_right_edge(dstinfo);
+ trim_bottom_edge(dstinfo);
+ }
+ break;
+ case JXFORM_ROT_90:
+ transpose_critical_parameters(dstinfo);
+ if (info->trim)
+ trim_right_edge(dstinfo);
+ break;
+ case JXFORM_ROT_180:
+ if (info->trim) {
+ trim_right_edge(dstinfo);
+ trim_bottom_edge(dstinfo);
+ }
+ break;
+ case JXFORM_ROT_270:
+ transpose_critical_parameters(dstinfo);
+ if (info->trim)
+ trim_bottom_edge(dstinfo);
+ break;
+ }
+
+ /* Return the appropriate output data set */
+ if (info->workspace_coef_arrays != NULL)
+ return info->workspace_coef_arrays;
+ return src_coef_arrays;
+}
+
+
+/* Execute the actual transformation, if any.
+ *
+ * This must be called *after* jpeg_write_coefficients, because it depends
+ * on jpeg_write_coefficients to have computed subsidiary values such as
+ * the per-component width and height fields in the destination object.
+ *
+ * Note that some transformations will modify the source data arrays!
+ */
+
+GLOBAL(void)
+jtransform_execute_transformation (j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jpeg_transform_info *info)
+{
+ jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
+
+ switch (info->transform) {
+ case JXFORM_NONE:
+ break;
+ case JXFORM_FLIP_H:
+ do_flip_h(srcinfo, dstinfo, src_coef_arrays);
+ break;
+ case JXFORM_FLIP_V:
+ do_flip_v(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ case JXFORM_TRANSPOSE:
+ do_transpose(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ case JXFORM_TRANSVERSE:
+ do_transverse(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ case JXFORM_ROT_90:
+ do_rot_90(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ case JXFORM_ROT_180:
+ do_rot_180(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ case JXFORM_ROT_270:
+ do_rot_270(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
+ break;
+ }
+}
+
+#endif /* TRANSFORMS_SUPPORTED */
+
+
+/* Setup decompression object to save desired markers in memory.
+ * This must be called before jpeg_read_header() to have the desired effect.
+ */
+
+GLOBAL(void)
+jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
+{
+#ifdef SAVE_MARKERS_SUPPORTED
+ int m;
+
+ /* Save comments except under NONE option */
+ if (option != JCOPYOPT_NONE) {
+ jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
+ }
+ /* Save all types of APPn markers iff ALL option */
+ if (option == JCOPYOPT_ALL) {
+ for (m = 0; m < 16; m++)
+ jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
+ }
+#endif /* SAVE_MARKERS_SUPPORTED */
+}
+
+/* Copy markers saved in the given source object to the destination object.
+ * This should be called just after jpeg_start_compress() or
+ * jpeg_write_coefficients().
+ * Note that those routines will have written the SOI, and also the
+ * JFIF APP0 or Adobe APP14 markers if selected.
+ */
+
+GLOBAL(void)
+jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ JCOPY_OPTION option)
+{
+ jpeg_saved_marker_ptr marker;
+
+ /* In the current implementation, we don't actually need to examine the
+ * option flag here; we just copy everything that got saved.
+ * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
+ * if the encoder library already wrote one.
+ */
+ for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
+ if (dstinfo->write_JFIF_header &&
+ marker->marker == JPEG_APP0 &&
+ marker->data_length >= 5 &&
+ GETJOCTET(marker->data[0]) == 0x4A &&
+ GETJOCTET(marker->data[1]) == 0x46 &&
+ GETJOCTET(marker->data[2]) == 0x49 &&
+ GETJOCTET(marker->data[3]) == 0x46 &&
+ GETJOCTET(marker->data[4]) == 0)
+ continue; /* reject duplicate JFIF */
+ if (dstinfo->write_Adobe_marker &&
+ marker->marker == JPEG_APP0+14 &&
+ marker->data_length >= 5 &&
+ GETJOCTET(marker->data[0]) == 0x41 &&
+ GETJOCTET(marker->data[1]) == 0x64 &&
+ GETJOCTET(marker->data[2]) == 0x6F &&
+ GETJOCTET(marker->data[3]) == 0x62 &&
+ GETJOCTET(marker->data[4]) == 0x65)
+ continue; /* reject duplicate Adobe */
+#ifdef NEED_FAR_POINTERS
+ /* We could use jpeg_write_marker if the data weren't FAR... */
+ {
+ unsigned int i;
+ jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
+ for (i = 0; i < marker->data_length; i++)
+ jpeg_write_m_byte(dstinfo, marker->data[i]);
+ }
+#else
+ jpeg_write_marker(dstinfo, marker->marker,
+ marker->data, marker->data_length);
+#endif
+ }
+}
diff --git a/test/monniaux/jpeg-6b/transupp.h b/test/monniaux/jpeg-6b/transupp.h
new file mode 100644
index 00000000..5c2d32af
--- /dev/null
+++ b/test/monniaux/jpeg-6b/transupp.h
@@ -0,0 +1,135 @@
+/*
+ * transupp.h
+ *
+ * Copyright (C) 1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains declarations for image transformation routines and
+ * other utility code used by the jpegtran sample application. These are
+ * NOT part of the core JPEG library. But we keep these routines separate
+ * from jpegtran.c to ease the task of maintaining jpegtran-like programs
+ * that have other user interfaces.
+ *
+ * NOTE: all the routines declared here have very specific requirements
+ * about when they are to be executed during the reading and writing of the
+ * source and destination files. See the comments in transupp.c, or see
+ * jpegtran.c for an example of correct usage.
+ */
+
+/* If you happen not to want the image transform support, disable it here */
+#ifndef TRANSFORMS_SUPPORTED
+#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */
+#endif
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jtransform_request_workspace jTrRequest
+#define jtransform_adjust_parameters jTrAdjust
+#define jtransform_execute_transformation jTrExec
+#define jcopy_markers_setup jCMrkSetup
+#define jcopy_markers_execute jCMrkExec
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/*
+ * Codes for supported types of image transformations.
+ */
+
+typedef enum {
+ JXFORM_NONE, /* no transformation */
+ JXFORM_FLIP_H, /* horizontal flip */
+ JXFORM_FLIP_V, /* vertical flip */
+ JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */
+ JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */
+ JXFORM_ROT_90, /* 90-degree clockwise rotation */
+ JXFORM_ROT_180, /* 180-degree rotation */
+ JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */
+} JXFORM_CODE;
+
+/*
+ * Although rotating and flipping data expressed as DCT coefficients is not
+ * hard, there is an asymmetry in the JPEG format specification for images
+ * whose dimensions aren't multiples of the iMCU size. The right and bottom
+ * image edges are padded out to the next iMCU boundary with junk data; but
+ * no padding is possible at the top and left edges. If we were to flip
+ * the whole image including the pad data, then pad garbage would become
+ * visible at the top and/or left, and real pixels would disappear into the
+ * pad margins --- perhaps permanently, since encoders & decoders may not
+ * bother to preserve DCT blocks that appear to be completely outside the
+ * nominal image area. So, we have to exclude any partial iMCUs from the
+ * basic transformation.
+ *
+ * Transpose is the only transformation that can handle partial iMCUs at the
+ * right and bottom edges completely cleanly. flip_h can flip partial iMCUs
+ * at the bottom, but leaves any partial iMCUs at the right edge untouched.
+ * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
+ * The other transforms are defined as combinations of these basic transforms
+ * and process edge blocks in a way that preserves the equivalence.
+ *
+ * The "trim" option causes untransformable partial iMCUs to be dropped;
+ * this is not strictly lossless, but it usually gives the best-looking
+ * result for odd-size images. Note that when this option is active,
+ * the expected mathematical equivalences between the transforms may not hold.
+ * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
+ * followed by -rot 180 -trim trims both edges.)
+ *
+ * We also offer a "force to grayscale" option, which simply discards the
+ * chrominance channels of a YCbCr image. This is lossless in the sense that
+ * the luminance channel is preserved exactly. It's not the same kind of
+ * thing as the rotate/flip transformations, but it's convenient to handle it
+ * as part of this package, mainly because the transformation routines have to
+ * be aware of the option to know how many components to work on.
+ */
+
+typedef struct {
+ /* Options: set by caller */
+ JXFORM_CODE transform; /* image transform operator */
+ boolean trim; /* if TRUE, trim partial MCUs as needed */
+ boolean force_grayscale; /* if TRUE, convert color image to grayscale */
+
+ /* Internal workspace: caller should not touch these */
+ int num_components; /* # of components in workspace */
+ jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
+} jpeg_transform_info;
+
+
+#if TRANSFORMS_SUPPORTED
+
+/* Request any required workspace */
+EXTERN(void) jtransform_request_workspace
+ JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
+/* Adjust output image parameters */
+EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
+ JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jpeg_transform_info *info));
+/* Execute the actual transformation, if any */
+EXTERN(void) jtransform_execute_transformation
+ JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jpeg_transform_info *info));
+
+#endif /* TRANSFORMS_SUPPORTED */
+
+
+/*
+ * Support for copying optional markers from source to destination file.
+ */
+
+typedef enum {
+ JCOPYOPT_NONE, /* copy no optional markers */
+ JCOPYOPT_COMMENTS, /* copy only comment (COM) markers */
+ JCOPYOPT_ALL /* copy all optional markers */
+} JCOPY_OPTION;
+
+#define JCOPYOPT_DEFAULT JCOPYOPT_COMMENTS /* recommended default */
+
+/* Setup decompression object to save desired markers in memory */
+EXTERN(void) jcopy_markers_setup
+ JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
+/* Copy markers saved in the given source object to the destination object */
+EXTERN(void) jcopy_markers_execute
+ JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ JCOPY_OPTION option));
diff --git a/test/monniaux/jpeg-6b/usage.doc b/test/monniaux/jpeg-6b/usage.doc
new file mode 100644
index 00000000..8c4970af
--- /dev/null
+++ b/test/monniaux/jpeg-6b/usage.doc
@@ -0,0 +1,562 @@
+USAGE instructions for the Independent JPEG Group's JPEG software
+=================================================================
+
+This file describes usage of the JPEG conversion programs cjpeg and djpeg,
+as well as the utility programs jpegtran, rdjpgcom and wrjpgcom. (See
+the other documentation files if you wish to use the JPEG library within
+your own programs.)
+
+If you are on a Unix machine you may prefer to read the Unix-style manual
+pages in files cjpeg.1, djpeg.1, jpegtran.1, rdjpgcom.1, wrjpgcom.1.
+
+
+INTRODUCTION
+
+These programs implement JPEG image compression and decompression. JPEG
+(pronounced "jay-peg") is a standardized compression method for full-color
+and gray-scale images. JPEG is designed to handle "real-world" scenes,
+for example scanned photographs. Cartoons, line drawings, and other
+non-realistic images are not JPEG's strong suit; on that sort of material
+you may get poor image quality and/or little compression.
+
+JPEG is lossy, meaning that the output image is not necessarily identical to
+the input image. Hence you should not use JPEG if you have to have identical
+output bits. However, on typical real-world images, very good compression
+levels can be obtained with no visible change, and amazingly high compression
+is possible if you can tolerate a low-quality image. You can trade off image
+quality against file size by adjusting the compressor's "quality" setting.
+
+
+GENERAL USAGE
+
+We provide two programs, cjpeg to compress an image file into JPEG format,
+and djpeg to decompress a JPEG file back into a conventional image format.
+
+On Unix-like systems, you say:
+ cjpeg [switches] [imagefile] >jpegfile
+or
+ djpeg [switches] [jpegfile] >imagefile
+The programs read the specified input file, or standard input if none is
+named. They always write to standard output (with trace/error messages to
+standard error). These conventions are handy for piping images between
+programs.
+
+On most non-Unix systems, you say:
+ cjpeg [switches] imagefile jpegfile
+or
+ djpeg [switches] jpegfile imagefile
+i.e., both the input and output files are named on the command line. This
+style is a little more foolproof, and it loses no functionality if you don't
+have pipes. (You can get this style on Unix too, if you prefer, by defining
+TWO_FILE_COMMANDLINE when you compile the programs; see install.doc.)
+
+You can also say:
+ cjpeg [switches] -outfile jpegfile imagefile
+or
+ djpeg [switches] -outfile imagefile jpegfile
+This syntax works on all systems, so it is useful for scripts.
+
+The currently supported image file formats are: PPM (PBMPLUS color format),
+PGM (PBMPLUS gray-scale format), BMP, Targa, and RLE (Utah Raster Toolkit
+format). (RLE is supported only if the URT library is available.)
+cjpeg recognizes the input image format automatically, with the exception
+of some Targa-format files. You have to tell djpeg which format to generate.
+
+JPEG files are in the defacto standard JFIF file format. There are other,
+less widely used JPEG-based file formats, but we don't support them.
+
+All switch names may be abbreviated; for example, -grayscale may be written
+-gray or -gr. Most of the "basic" switches can be abbreviated to as little as
+one letter. Upper and lower case are equivalent (-BMP is the same as -bmp).
+British spellings are also accepted (e.g., -greyscale), though for brevity
+these are not mentioned below.
+
+
+CJPEG DETAILS
+
+The basic command line switches for cjpeg are:
+
+ -quality N Scale quantization tables to adjust image quality.
+ Quality is 0 (worst) to 100 (best); default is 75.
+ (See below for more info.)
+
+ -grayscale Create monochrome JPEG file from color input.
+ Be sure to use this switch when compressing a grayscale
+ BMP file, because cjpeg isn't bright enough to notice
+ whether a BMP file uses only shades of gray. By
+ saying -grayscale, you'll get a smaller JPEG file that
+ takes less time to process.
+
+ -optimize Perform optimization of entropy encoding parameters.
+ Without this, default encoding parameters are used.
+ -optimize usually makes the JPEG file a little smaller,
+ but cjpeg runs somewhat slower and needs much more
+ memory. Image quality and speed of decompression are
+ unaffected by -optimize.
+
+ -progressive Create progressive JPEG file (see below).
+
+ -targa Input file is Targa format. Targa files that contain
+ an "identification" field will not be automatically
+ recognized by cjpeg; for such files you must specify
+ -targa to make cjpeg treat the input as Targa format.
+ For most Targa files, you won't need this switch.
+
+The -quality switch lets you trade off compressed file size against quality of
+the reconstructed image: the higher the quality setting, the larger the JPEG
+file, and the closer the output image will be to the original input. Normally
+you want to use the lowest quality setting (smallest file) that decompresses
+into something visually indistinguishable from the original image. For this
+purpose the quality setting should be between 50 and 95; the default of 75 is
+often about right. If you see defects at -quality 75, then go up 5 or 10
+counts at a time until you are happy with the output image. (The optimal
+setting will vary from one image to another.)
+
+-quality 100 will generate a quantization table of all 1's, minimizing loss
+in the quantization step (but there is still information loss in subsampling,
+as well as roundoff error). This setting is mainly of interest for
+experimental purposes. Quality values above about 95 are NOT recommended for
+normal use; the compressed file size goes up dramatically for hardly any gain
+in output image quality.
+
+In the other direction, quality values below 50 will produce very small files
+of low image quality. Settings around 5 to 10 might be useful in preparing an
+index of a large image library, for example. Try -quality 2 (or so) for some
+amusing Cubist effects. (Note: quality values below about 25 generate 2-byte
+quantization tables, which are considered optional in the JPEG standard.
+cjpeg emits a warning message when you give such a quality value, because some
+other JPEG programs may be unable to decode the resulting file. Use -baseline
+if you need to ensure compatibility at low quality values.)
+
+The -progressive switch creates a "progressive JPEG" file. In this type of
+JPEG file, the data is stored in multiple scans of increasing quality. If the
+file is being transmitted over a slow communications link, the decoder can use
+the first scan to display a low-quality image very quickly, and can then
+improve the display with each subsequent scan. The final image is exactly
+equivalent to a standard JPEG file of the same quality setting, and the total
+file size is about the same --- often a little smaller. CAUTION: progressive
+JPEG is not yet widely implemented, so many decoders will be unable to view a
+progressive JPEG file at all.
+
+Switches for advanced users:
+
+ -dct int Use integer DCT method (default).
+ -dct fast Use fast integer DCT (less accurate).
+ -dct float Use floating-point DCT method.
+ The float method is very slightly more accurate than
+ the int method, but is much slower unless your machine
+ has very fast floating-point hardware. Also note that
+ results of the floating-point method may vary slightly
+ across machines, while the integer methods should give
+ the same results everywhere. The fast integer method
+ is much less accurate than the other two.
+
+ -restart N Emit a JPEG restart marker every N MCU rows, or every
+ N MCU blocks if "B" is attached to the number.
+ -restart 0 (the default) means no restart markers.
+
+ -smooth N Smooth the input image to eliminate dithering noise.
+ N, ranging from 1 to 100, indicates the strength of
+ smoothing. 0 (the default) means no smoothing.
+
+ -maxmemory N Set limit for amount of memory to use in processing
+ large images. Value is in thousands of bytes, or
+ millions of bytes if "M" is attached to the number.
+ For example, -max 4m selects 4000000 bytes. If more
+ space is needed, temporary files will be used.
+
+ -verbose Enable debug printout. More -v's give more printout.
+ or -debug Also, version information is printed at startup.
+
+The -restart option inserts extra markers that allow a JPEG decoder to
+resynchronize after a transmission error. Without restart markers, any damage
+to a compressed file will usually ruin the image from the point of the error
+to the end of the image; with restart markers, the damage is usually confined
+to the portion of the image up to the next restart marker. Of course, the
+restart markers occupy extra space. We recommend -restart 1 for images that
+will be transmitted across unreliable networks such as Usenet.
+
+The -smooth option filters the input to eliminate fine-scale noise. This is
+often useful when converting dithered images to JPEG: a moderate smoothing
+factor of 10 to 50 gets rid of dithering patterns in the input file, resulting
+in a smaller JPEG file and a better-looking image. Too large a smoothing
+factor will visibly blur the image, however.
+
+Switches for wizards:
+
+ -baseline Force baseline-compatible quantization tables to be
+ generated. This clamps quantization values to 8 bits
+ even at low quality settings. (This switch is poorly
+ named, since it does not ensure that the output is
+ actually baseline JPEG. For example, you can use
+ -baseline and -progressive together.)
+
+ -qtables file Use the quantization tables given in the specified
+ text file.
+
+ -qslots N[,...] Select which quantization table to use for each color
+ component.
+
+ -sample HxV[,...] Set JPEG sampling factors for each color component.
+
+ -scans file Use the scan script given in the specified text file.
+
+The "wizard" switches are intended for experimentation with JPEG. If you
+don't know what you are doing, DON'T USE THEM. These switches are documented
+further in the file wizard.doc.
+
+
+DJPEG DETAILS
+
+The basic command line switches for djpeg are:
+
+ -colors N Reduce image to at most N colors. This reduces the
+ or -quantize N number of colors used in the output image, so that it
+ can be displayed on a colormapped display or stored in
+ a colormapped file format. For example, if you have
+ an 8-bit display, you'd need to reduce to 256 or fewer
+ colors. (-colors is the recommended name, -quantize
+ is provided only for backwards compatibility.)
+
+ -fast Select recommended processing options for fast, low
+ quality output. (The default options are chosen for
+ highest quality output.) Currently, this is equivalent
+ to "-dct fast -nosmooth -onepass -dither ordered".
+
+ -grayscale Force gray-scale output even if JPEG file is color.
+ Useful for viewing on monochrome displays; also,
+ djpeg runs noticeably faster in this mode.
+
+ -scale M/N Scale the output image by a factor M/N. Currently
+ the scale factor must be 1/1, 1/2, 1/4, or 1/8.
+ Scaling is handy if the image is larger than your
+ screen; also, djpeg runs much faster when scaling
+ down the output.
+
+ -bmp Select BMP output format (Windows flavor). 8-bit
+ colormapped format is emitted if -colors or -grayscale
+ is specified, or if the JPEG file is gray-scale;
+ otherwise, 24-bit full-color format is emitted.
+
+ -gif Select GIF output format. Since GIF does not support
+ more than 256 colors, -colors 256 is assumed (unless
+ you specify a smaller number of colors). If you
+ specify -fast, the default number of colors is 216.
+
+ -os2 Select BMP output format (OS/2 1.x flavor). 8-bit
+ colormapped format is emitted if -colors or -grayscale
+ is specified, or if the JPEG file is gray-scale;
+ otherwise, 24-bit full-color format is emitted.
+
+ -pnm Select PBMPLUS (PPM/PGM) output format (this is the
+ default format). PGM is emitted if the JPEG file is
+ gray-scale or if -grayscale is specified; otherwise
+ PPM is emitted.
+
+ -rle Select RLE output format. (Requires URT library.)
+
+ -targa Select Targa output format. Gray-scale format is
+ emitted if the JPEG file is gray-scale or if
+ -grayscale is specified; otherwise, colormapped format
+ is emitted if -colors is specified; otherwise, 24-bit
+ full-color format is emitted.
+
+Switches for advanced users:
+
+ -dct int Use integer DCT method (default).
+ -dct fast Use fast integer DCT (less accurate).
+ -dct float Use floating-point DCT method.
+ The float method is very slightly more accurate than
+ the int method, but is much slower unless your machine
+ has very fast floating-point hardware. Also note that
+ results of the floating-point method may vary slightly
+ across machines, while the integer methods should give
+ the same results everywhere. The fast integer method
+ is much less accurate than the other two.
+
+ -dither fs Use Floyd-Steinberg dithering in color quantization.
+ -dither ordered Use ordered dithering in color quantization.
+ -dither none Do not use dithering in color quantization.
+ By default, Floyd-Steinberg dithering is applied when
+ quantizing colors; this is slow but usually produces
+ the best results. Ordered dither is a compromise
+ between speed and quality; no dithering is fast but
+ usually looks awful. Note that these switches have
+ no effect unless color quantization is being done.
+ Ordered dither is only available in -onepass mode.
+
+ -map FILE Quantize to the colors used in the specified image
+ file. This is useful for producing multiple files
+ with identical color maps, or for forcing a predefined
+ set of colors to be used. The FILE must be a GIF
+ or PPM file. This option overrides -colors and
+ -onepass.
+
+ -nosmooth Use a faster, lower-quality upsampling routine.
+
+ -onepass Use one-pass instead of two-pass color quantization.
+ The one-pass method is faster and needs less memory,
+ but it produces a lower-quality image. -onepass is
+ ignored unless you also say -colors N. Also,
+ the one-pass method is always used for gray-scale
+ output (the two-pass method is no improvement then).
+
+ -maxmemory N Set limit for amount of memory to use in processing
+ large images. Value is in thousands of bytes, or
+ millions of bytes if "M" is attached to the number.
+ For example, -max 4m selects 4000000 bytes. If more
+ space is needed, temporary files will be used.
+
+ -verbose Enable debug printout. More -v's give more printout.
+ or -debug Also, version information is printed at startup.
+
+
+HINTS FOR CJPEG
+
+Color GIF files are not the ideal input for JPEG; JPEG is really intended for
+compressing full-color (24-bit) images. In particular, don't try to convert
+cartoons, line drawings, and other images that have only a few distinct
+colors. GIF works great on these, JPEG does not. If you want to convert a
+GIF to JPEG, you should experiment with cjpeg's -quality and -smooth options
+to get a satisfactory conversion. -smooth 10 or so is often helpful.
+
+Avoid running an image through a series of JPEG compression/decompression
+cycles. Image quality loss will accumulate; after ten or so cycles the image
+may be noticeably worse than it was after one cycle. It's best to use a
+lossless format while manipulating an image, then convert to JPEG format when
+you are ready to file the image away.
+
+The -optimize option to cjpeg is worth using when you are making a "final"
+version for posting or archiving. It's also a win when you are using low
+quality settings to make very small JPEG files; the percentage improvement
+is often a lot more than it is on larger files. (At present, -optimize
+mode is always selected when generating progressive JPEG files.)
+
+GIF input files are no longer supported, to avoid the Unisys LZW patent.
+Use a Unisys-licensed program if you need to read a GIF file. (Conversion
+of GIF files to JPEG is usually a bad idea anyway.)
+
+
+HINTS FOR DJPEG
+
+To get a quick preview of an image, use the -grayscale and/or -scale switches.
+"-grayscale -scale 1/8" is the fastest case.
+
+Several options are available that trade off image quality to gain speed.
+"-fast" turns on the recommended settings.
+
+"-dct fast" and/or "-nosmooth" gain speed at a small sacrifice in quality.
+When producing a color-quantized image, "-onepass -dither ordered" is fast but
+much lower quality than the default behavior. "-dither none" may give
+acceptable results in two-pass mode, but is seldom tolerable in one-pass mode.
+
+If you are fortunate enough to have very fast floating point hardware,
+"-dct float" may be even faster than "-dct fast". But on most machines
+"-dct float" is slower than "-dct int"; in this case it is not worth using,
+because its theoretical accuracy advantage is too small to be significant
+in practice.
+
+Two-pass color quantization requires a good deal of memory; on MS-DOS machines
+it may run out of memory even with -maxmemory 0. In that case you can still
+decompress, with some loss of image quality, by specifying -onepass for
+one-pass quantization.
+
+To avoid the Unisys LZW patent, djpeg produces uncompressed GIF files. These
+are larger than they should be, but are readable by standard GIF decoders.
+
+
+HINTS FOR BOTH PROGRAMS
+
+If more space is needed than will fit in the available main memory (as
+determined by -maxmemory), temporary files will be used. (MS-DOS versions
+will try to get extended or expanded memory first.) The temporary files are
+often rather large: in typical cases they occupy three bytes per pixel, for
+example 3*800*600 = 1.44Mb for an 800x600 image. If you don't have enough
+free disk space, leave out -progressive and -optimize (for cjpeg) or specify
+-onepass (for djpeg).
+
+On MS-DOS, the temporary files are created in the directory named by the TMP
+or TEMP environment variable, or in the current directory if neither of those
+exist. Amiga implementations put the temp files in the directory named by
+JPEGTMP:, so be sure to assign JPEGTMP: to a disk partition with adequate free
+space.
+
+The default memory usage limit (-maxmemory) is set when the software is
+compiled. If you get an "insufficient memory" error, try specifying a smaller
+-maxmemory value, even -maxmemory 0 to use the absolute minimum space. You
+may want to recompile with a smaller default value if this happens often.
+
+On machines that have "environment" variables, you can define the environment
+variable JPEGMEM to set the default memory limit. The value is specified as
+described for the -maxmemory switch. JPEGMEM overrides the default value
+specified when the program was compiled, and itself is overridden by an
+explicit -maxmemory switch.
+
+On MS-DOS machines, -maxmemory is the amount of main (conventional) memory to
+use. (Extended or expanded memory is also used if available.) Most
+DOS-specific versions of this software do their own memory space estimation
+and do not need you to specify -maxmemory.
+
+
+JPEGTRAN
+
+jpegtran performs various useful transformations of JPEG files.
+It can translate the coded representation from one variant of JPEG to another,
+for example from baseline JPEG to progressive JPEG or vice versa. It can also
+perform some rearrangements of the image data, for example turning an image
+from landscape to portrait format by rotation.
+
+jpegtran works by rearranging the compressed data (DCT coefficients), without
+ever fully decoding the image. Therefore, its transformations are lossless:
+there is no image degradation at all, which would not be true if you used
+djpeg followed by cjpeg to accomplish the same conversion. But by the same
+token, jpegtran cannot perform lossy operations such as changing the image
+quality.
+
+jpegtran uses a command line syntax similar to cjpeg or djpeg.
+On Unix-like systems, you say:
+ jpegtran [switches] [inputfile] >outputfile
+On most non-Unix systems, you say:
+ jpegtran [switches] inputfile outputfile
+where both the input and output files are JPEG files.
+
+To specify the coded JPEG representation used in the output file,
+jpegtran accepts a subset of the switches recognized by cjpeg:
+ -optimize Perform optimization of entropy encoding parameters.
+ -progressive Create progressive JPEG file.
+ -restart N Emit a JPEG restart marker every N MCU rows, or every
+ N MCU blocks if "B" is attached to the number.
+ -scans file Use the scan script given in the specified text file.
+See the previous discussion of cjpeg for more details about these switches.
+If you specify none of these switches, you get a plain baseline-JPEG output
+file. The quality setting and so forth are determined by the input file.
+
+The image can be losslessly transformed by giving one of these switches:
+ -flip horizontal Mirror image horizontally (left-right).
+ -flip vertical Mirror image vertically (top-bottom).
+ -rotate 90 Rotate image 90 degrees clockwise.
+ -rotate 180 Rotate image 180 degrees.
+ -rotate 270 Rotate image 270 degrees clockwise (or 90 ccw).
+ -transpose Transpose image (across UL-to-LR axis).
+ -transverse Transverse transpose (across UR-to-LL axis).
+
+The transpose transformation has no restrictions regarding image dimensions.
+The other transformations operate rather oddly if the image dimensions are not
+a multiple of the iMCU size (usually 8 or 16 pixels), because they can only
+transform complete blocks of DCT coefficient data in the desired way.
+
+jpegtran's default behavior when transforming an odd-size image is designed
+to preserve exact reversibility and mathematical consistency of the
+transformation set. As stated, transpose is able to flip the entire image
+area. Horizontal mirroring leaves any partial iMCU column at the right edge
+untouched, but is able to flip all rows of the image. Similarly, vertical
+mirroring leaves any partial iMCU row at the bottom edge untouched, but is
+able to flip all columns. The other transforms can be built up as sequences
+of transpose and flip operations; for consistency, their actions on edge
+pixels are defined to be the same as the end result of the corresponding
+transpose-and-flip sequence.
+
+For practical use, you may prefer to discard any untransformable edge pixels
+rather than having a strange-looking strip along the right and/or bottom edges
+of a transformed image. To do this, add the -trim switch:
+ -trim Drop non-transformable edge blocks.
+Obviously, a transformation with -trim is not reversible, so strictly speaking
+jpegtran with this switch is not lossless. Also, the expected mathematical
+equivalences between the transformations no longer hold. For example,
+"-rot 270 -trim" trims only the bottom edge, but "-rot 90 -trim" followed by
+"-rot 180 -trim" trims both edges.
+
+Another not-strictly-lossless transformation switch is:
+ -grayscale Force grayscale output.
+This option discards the chrominance channels if the input image is YCbCr
+(ie, a standard color JPEG), resulting in a grayscale JPEG file. The
+luminance channel is preserved exactly, so this is a better method of reducing
+to grayscale than decompression, conversion, and recompression. This switch
+is particularly handy for fixing a monochrome picture that was mistakenly
+encoded as a color JPEG. (In such a case, the space savings from getting rid
+of the near-empty chroma channels won't be large; but the decoding time for
+a grayscale JPEG is substantially less than that for a color JPEG.)
+
+jpegtran also recognizes these switches that control what to do with "extra"
+markers, such as comment blocks:
+ -copy none Copy no extra markers from source file. This setting
+ suppresses all comments and other excess baggage
+ present in the source file.
+ -copy comments Copy only comment markers. This setting copies
+ comments from the source file, but discards
+ any other inessential data.
+ -copy all Copy all extra markers. This setting preserves
+ miscellaneous markers found in the source file, such
+ as JFIF thumbnails and Photoshop settings. In some
+ files these extra markers can be sizable.
+The default behavior is -copy comments. (Note: in IJG releases v6 and v6a,
+jpegtran always did the equivalent of -copy none.)
+
+Additional switches recognized by jpegtran are:
+ -outfile filename
+ -maxmemory N
+ -verbose
+ -debug
+These work the same as in cjpeg or djpeg.
+
+
+THE COMMENT UTILITIES
+
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings. This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text. COM blocks do not interfere with the image stored in the JPEG
+file. The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+
+We provide two utility programs to display COM block contents and add COM
+blocks to a JPEG file.
+
+rdjpgcom searches a JPEG file and prints the contents of any COM blocks on
+standard output. The command line syntax is
+ rdjpgcom [-verbose] [inputfilename]
+The switch "-verbose" (or just "-v") causes rdjpgcom to also display the JPEG
+image dimensions. If you omit the input file name from the command line,
+the JPEG file is read from standard input. (This may not work on some
+operating systems, if binary data can't be read from stdin.)
+
+wrjpgcom adds a COM block, containing text you provide, to a JPEG file.
+Ordinarily, the COM block is added after any existing COM blocks, but you
+can delete the old COM blocks if you wish. wrjpgcom produces a new JPEG
+file; it does not modify the input file. DO NOT try to overwrite the input
+file by directing wrjpgcom's output back into it; on most systems this will
+just destroy your file.
+
+The command line syntax for wrjpgcom is similar to cjpeg's. On Unix-like
+systems, it is
+ wrjpgcom [switches] [inputfilename]
+The output file is written to standard output. The input file comes from
+the named file, or from standard input if no input file is named.
+
+On most non-Unix systems, the syntax is
+ wrjpgcom [switches] inputfilename outputfilename
+where both input and output file names must be given explicitly.
+
+wrjpgcom understands three switches:
+ -replace Delete any existing COM blocks from the file.
+ -comment "Comment text" Supply new COM text on command line.
+ -cfile name Read text for new COM block from named file.
+(Switch names can be abbreviated.) If you have only one line of comment text
+to add, you can provide it on the command line with -comment. The comment
+text must be surrounded with quotes so that it is treated as a single
+argument. Longer comments can be read from a text file.
+
+If you give neither -comment nor -cfile, then wrjpgcom will read the comment
+text from standard input. (In this case an input image file name MUST be
+supplied, so that the source JPEG file comes from somewhere else.) You can
+enter multiple lines, up to 64KB worth. Type an end-of-file indicator
+(usually control-D or control-Z) to terminate the comment text entry.
+
+wrjpgcom will not add a COM block if the provided comment string is empty.
+Therefore -replace -comment "" can be used to delete all COM blocks from a
+file.
+
+These utility programs do not depend on the IJG JPEG library. In
+particular, the source code for rdjpgcom is intended as an illustration of
+the minimum amount of code required to parse a JPEG file header correctly.
diff --git a/test/monniaux/jpeg-6b/wizard.doc b/test/monniaux/jpeg-6b/wizard.doc
new file mode 100644
index 00000000..54170b22
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wizard.doc
@@ -0,0 +1,211 @@
+Advanced usage instructions for the Independent JPEG Group's JPEG software
+==========================================================================
+
+This file describes cjpeg's "switches for wizards".
+
+The "wizard" switches are intended for experimentation with JPEG by persons
+who are reasonably knowledgeable about the JPEG standard. If you don't know
+what you are doing, DON'T USE THESE SWITCHES. You'll likely produce files
+with worse image quality and/or poorer compression than you'd get from the
+default settings. Furthermore, these switches must be used with caution
+when making files intended for general use, because not all JPEG decoders
+will support unusual JPEG parameter settings.
+
+
+Quantization Table Adjustment
+-----------------------------
+
+Ordinarily, cjpeg starts with a default set of tables (the same ones given
+as examples in the JPEG standard) and scales them up or down according to
+the -quality setting. The details of the scaling algorithm can be found in
+jcparam.c. At very low quality settings, some quantization table entries
+can get scaled up to values exceeding 255. Although 2-byte quantization
+values are supported by the IJG software, this feature is not in baseline
+JPEG and is not supported by all implementations. If you need to ensure
+wide compatibility of low-quality files, you can constrain the scaled
+quantization values to no more than 255 by giving the -baseline switch.
+Note that use of -baseline will result in poorer quality for the same file
+size, since more bits than necessary are expended on higher AC coefficients.
+
+You can substitute a different set of quantization values by using the
+-qtables switch:
+
+ -qtables file Use the quantization tables given in the named file.
+
+The specified file should be a text file containing decimal quantization
+values. The file should contain one to four tables, each of 64 elements.
+The tables are implicitly numbered 0,1,etc. in order of appearance. Table
+entries appear in normal array order (NOT in the zigzag order in which they
+will be stored in the JPEG file).
+
+Quantization table files are free format, in that arbitrary whitespace can
+appear between numbers. Also, comments can be included: a comment starts
+with '#' and extends to the end of the line. Here is an example file that
+duplicates the default quantization tables:
+
+ # Quantization tables given in JPEG spec, section K.1
+
+ # This is table 0 (the luminance table):
+ 16 11 10 16 24 40 51 61
+ 12 12 14 19 26 58 60 55
+ 14 13 16 24 40 57 69 56
+ 14 17 22 29 51 87 80 62
+ 18 22 37 56 68 109 103 77
+ 24 35 55 64 81 104 113 92
+ 49 64 78 87 103 121 120 101
+ 72 92 95 98 112 100 103 99
+
+ # This is table 1 (the chrominance table):
+ 17 18 24 47 99 99 99 99
+ 18 21 26 66 99 99 99 99
+ 24 26 56 99 99 99 99 99
+ 47 66 99 99 99 99 99 99
+ 99 99 99 99 99 99 99 99
+ 99 99 99 99 99 99 99 99
+ 99 99 99 99 99 99 99 99
+ 99 99 99 99 99 99 99 99
+
+If the -qtables switch is used without -quality, then the specified tables
+are used exactly as-is. If both -qtables and -quality are used, then the
+tables taken from the file are scaled in the same fashion that the default
+tables would be scaled for that quality setting. If -baseline appears, then
+the quantization values are constrained to the range 1-255.
+
+By default, cjpeg will use quantization table 0 for luminance components and
+table 1 for chrominance components. To override this choice, use the -qslots
+switch:
+
+ -qslots N[,...] Select which quantization table to use for
+ each color component.
+
+The -qslots switch specifies a quantization table number for each color
+component, in the order in which the components appear in the JPEG SOF marker.
+For example, to create a separate table for each of Y,Cb,Cr, you could
+provide a -qtables file that defines three quantization tables and say
+"-qslots 0,1,2". If -qslots gives fewer table numbers than there are color
+components, then the last table number is repeated as necessary.
+
+
+Sampling Factor Adjustment
+--------------------------
+
+By default, cjpeg uses 2:1 horizontal and vertical downsampling when
+compressing YCbCr data, and no downsampling for all other color spaces.
+You can override this default with the -sample switch:
+
+ -sample HxV[,...] Set JPEG sampling factors for each color
+ component.
+
+The -sample switch specifies the JPEG sampling factors for each color
+component, in the order in which they appear in the JPEG SOF marker.
+If you specify fewer HxV pairs than there are components, the remaining
+components are set to 1x1 sampling. For example, the default YCbCr setting
+is equivalent to "-sample 2x2,1x1,1x1", which can be abbreviated to
+"-sample 2x2".
+
+There are still some JPEG decoders in existence that support only 2x1
+sampling (also called 4:2:2 sampling). Compatibility with such decoders can
+be achieved by specifying "-sample 2x1". This is not recommended unless
+really necessary, since it increases file size and encoding/decoding time
+with very little quality gain.
+
+
+Multiple Scan / Progression Control
+-----------------------------------
+
+By default, cjpeg emits a single-scan sequential JPEG file. The
+-progressive switch generates a progressive JPEG file using a default series
+of progression parameters. You can create multiple-scan sequential JPEG
+files or progressive JPEG files with custom progression parameters by using
+the -scans switch:
+
+ -scans file Use the scan sequence given in the named file.
+
+The specified file should be a text file containing a "scan script".
+The script specifies the contents and ordering of the scans to be emitted.
+Each entry in the script defines one scan. A scan definition specifies
+the components to be included in the scan, and for progressive JPEG it also
+specifies the progression parameters Ss,Se,Ah,Al for the scan. Scan
+definitions are separated by semicolons (';'). A semicolon after the last
+scan definition is optional.
+
+Each scan definition contains one to four component indexes, optionally
+followed by a colon (':') and the four progressive-JPEG parameters. The
+component indexes denote which color component(s) are to be transmitted in
+the scan. Components are numbered in the order in which they appear in the
+JPEG SOF marker, with the first component being numbered 0. (Note that these
+indexes are not the "component ID" codes assigned to the components, just
+positional indexes.)
+
+The progression parameters for each scan are:
+ Ss Zigzag index of first coefficient included in scan
+ Se Zigzag index of last coefficient included in scan
+ Ah Zero for first scan of a coefficient, else Al of prior scan
+ Al Successive approximation low bit position for scan
+If the progression parameters are omitted, the values 0,63,0,0 are used,
+producing a sequential JPEG file. cjpeg automatically determines whether
+the script represents a progressive or sequential file, by observing whether
+Ss and Se values other than 0 and 63 appear. (The -progressive switch is
+not needed to specify this; in fact, it is ignored when -scans appears.)
+The scan script must meet the JPEG restrictions on progression sequences.
+(cjpeg checks that the spec's requirements are obeyed.)
+
+Scan script files are free format, in that arbitrary whitespace can appear
+between numbers and around punctuation. Also, comments can be included: a
+comment starts with '#' and extends to the end of the line. For additional
+legibility, commas or dashes can be placed between values. (Actually, any
+single punctuation character other than ':' or ';' can be inserted.) For
+example, the following two scan definitions are equivalent:
+ 0 1 2: 0 63 0 0;
+ 0,1,2 : 0-63, 0,0 ;
+
+Here is an example of a scan script that generates a partially interleaved
+sequential JPEG file:
+
+ 0; # Y only in first scan
+ 1 2; # Cb and Cr in second scan
+
+Here is an example of a progressive scan script using only spectral selection
+(no successive approximation):
+
+ # Interleaved DC scan for Y,Cb,Cr:
+ 0,1,2: 0-0, 0, 0 ;
+ # AC scans:
+ 0: 1-2, 0, 0 ; # First two Y AC coefficients
+ 0: 3-5, 0, 0 ; # Three more
+ 1: 1-63, 0, 0 ; # All AC coefficients for Cb
+ 2: 1-63, 0, 0 ; # All AC coefficients for Cr
+ 0: 6-9, 0, 0 ; # More Y coefficients
+ 0: 10-63, 0, 0 ; # Remaining Y coefficients
+
+Here is an example of a successive-approximation script. This is equivalent
+to the default script used by "cjpeg -progressive" for YCbCr images:
+
+ # Initial DC scan for Y,Cb,Cr (lowest bit not sent)
+ 0,1,2: 0-0, 0, 1 ;
+ # First AC scan: send first 5 Y AC coefficients, minus 2 lowest bits:
+ 0: 1-5, 0, 2 ;
+ # Send all Cr,Cb AC coefficients, minus lowest bit:
+ # (chroma data is usually too small to be worth subdividing further;
+ # but note we send Cr first since eye is least sensitive to Cb)
+ 2: 1-63, 0, 1 ;
+ 1: 1-63, 0, 1 ;
+ # Send remaining Y AC coefficients, minus 2 lowest bits:
+ 0: 6-63, 0, 2 ;
+ # Send next-to-lowest bit of all Y AC coefficients:
+ 0: 1-63, 2, 1 ;
+ # At this point we've sent all but the lowest bit of all coefficients.
+ # Send lowest bit of DC coefficients
+ 0,1,2: 0-0, 1, 0 ;
+ # Send lowest bit of AC coefficients
+ 2: 1-63, 1, 0 ;
+ 1: 1-63, 1, 0 ;
+ # Y AC lowest bit scan is last; it's usually the largest scan
+ 0: 1-63, 1, 0 ;
+
+It may be worth pointing out that this script is tuned for quality settings
+of around 50 to 75. For lower quality settings, you'd probably want to use
+a script with fewer stages of successive approximation (otherwise the
+initial scans will be really bad). For higher quality settings, you might
+want to use more stages of successive approximation (so that the initial
+scans are not too large).
diff --git a/test/monniaux/jpeg-6b/wrbmp.c b/test/monniaux/jpeg-6b/wrbmp.c
new file mode 100644
index 00000000..3283b0f1
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrbmp.c
@@ -0,0 +1,442 @@
+/*
+ * wrbmp.c
+ *
+ * Copyright (C) 1994-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in Microsoft "BMP"
+ * format (MS Windows 3.x and OS/2 1.x flavors).
+ * Either 8-bit colormapped or 24-bit full-color format can be written.
+ * No compression is supported.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * This code contributed by James Arthur Boucher.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef BMP_SUPPORTED
+
+
+/*
+ * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
+ * This is not yet implemented.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+ Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * Since BMP stores scanlines bottom-to-top, we have to invert the image
+ * from JPEG's top-to-bottom order. To do this, we save the outgoing data
+ * in a virtual array during put_pixel_row calls, then actually emit the
+ * BMP file during finish_output. The virtual array contains one JSAMPLE per
+ * pixel if the output is grayscale or colormapped, three if it is full color.
+ */
+
+/* Private version of data destination object */
+
+typedef struct {
+ struct djpeg_dest_struct pub; /* public fields */
+
+ boolean is_os2; /* saves the OS2 format request flag */
+
+ jvirt_sarray_ptr whole_image; /* needed to reverse row order */
+ JDIMENSION data_width; /* JSAMPLEs per row */
+ JDIMENSION row_width; /* physical width of one row in the BMP file */
+ int pad_bytes; /* number of padding bytes needed per row */
+ JDIMENSION cur_output_row; /* next row# to write to virtual array */
+} bmp_dest_struct;
+
+typedef bmp_dest_struct * bmp_dest_ptr;
+
+
+/* Forward declarations */
+LOCAL(void) write_colormap
+ JPP((j_decompress_ptr cinfo, bmp_dest_ptr dest,
+ int map_colors, int map_entry_size));
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF(void)
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+/* This version is for writing 24-bit pixels */
+{
+ bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+ JSAMPARRAY image_ptr;
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+ int pad;
+
+ /* Access next row in virtual array */
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->whole_image,
+ dest->cur_output_row, (JDIMENSION) 1, TRUE);
+ dest->cur_output_row++;
+
+ /* Transfer data. Note destination values must be in BGR order
+ * (even though Microsoft's own documents say the opposite).
+ */
+ inptr = dest->pub.buffer[0];
+ outptr = image_ptr[0];
+ for (col = cinfo->output_width; col > 0; col--) {
+ outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
+ outptr[1] = *inptr++;
+ outptr[0] = *inptr++;
+ outptr += 3;
+ }
+
+ /* Zero out the pad bytes. */
+ pad = dest->pad_bytes;
+ while (--pad >= 0)
+ *outptr++ = 0;
+}
+
+METHODDEF(void)
+put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+/* This version is for grayscale OR quantized color output */
+{
+ bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+ JSAMPARRAY image_ptr;
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+ int pad;
+
+ /* Access next row in virtual array */
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->whole_image,
+ dest->cur_output_row, (JDIMENSION) 1, TRUE);
+ dest->cur_output_row++;
+
+ /* Transfer data. */
+ inptr = dest->pub.buffer[0];
+ outptr = image_ptr[0];
+ for (col = cinfo->output_width; col > 0; col--) {
+ *outptr++ = *inptr++; /* can omit GETJSAMPLE() safely */
+ }
+
+ /* Zero out the pad bytes. */
+ pad = dest->pad_bytes;
+ while (--pad >= 0)
+ *outptr++ = 0;
+}
+
+
+/*
+ * Startup: normally writes the file header.
+ * In this module we may as well postpone everything until finish_output.
+ */
+
+METHODDEF(void)
+start_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ /* no work here */
+}
+
+
+/*
+ * Finish up at the end of the file.
+ *
+ * Here is where we really output the BMP file.
+ *
+ * First, routines to write the Windows and OS/2 variants of the file header.
+ */
+
+LOCAL(void)
+write_bmp_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
+/* Write a Windows-style BMP file header, including colormap if needed */
+{
+ char bmpfileheader[14];
+ char bmpinfoheader[40];
+#define PUT_2B(array,offset,value) \
+ (array[offset] = (char) ((value) & 0xFF), \
+ array[offset+1] = (char) (((value) >> 8) & 0xFF))
+#define PUT_4B(array,offset,value) \
+ (array[offset] = (char) ((value) & 0xFF), \
+ array[offset+1] = (char) (((value) >> 8) & 0xFF), \
+ array[offset+2] = (char) (((value) >> 16) & 0xFF), \
+ array[offset+3] = (char) (((value) >> 24) & 0xFF))
+ INT32 headersize, bfSize;
+ int bits_per_pixel, cmap_entries;
+
+ /* Compute colormap size and total file size */
+ if (cinfo->out_color_space == JCS_RGB) {
+ if (cinfo->quantize_colors) {
+ /* Colormapped RGB */
+ bits_per_pixel = 8;
+ cmap_entries = 256;
+ } else {
+ /* Unquantized, full color RGB */
+ bits_per_pixel = 24;
+ cmap_entries = 0;
+ }
+ } else {
+ /* Grayscale output. We need to fake a 256-entry colormap. */
+ bits_per_pixel = 8;
+ cmap_entries = 256;
+ }
+ /* File size */
+ headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */
+ bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
+
+ /* Set unused fields of header to 0 */
+ MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
+ MEMZERO(bmpinfoheader, SIZEOF(bmpinfoheader));
+
+ /* Fill the file header */
+ bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
+ bmpfileheader[1] = 0x4D;
+ PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
+ /* we leave bfReserved1 & bfReserved2 = 0 */
+ PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
+
+ /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */
+ PUT_2B(bmpinfoheader, 0, 40); /* biSize */
+ PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
+ PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
+ PUT_2B(bmpinfoheader, 12, 1); /* biPlanes - must be 1 */
+ PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */
+ /* we leave biCompression = 0, for none */
+ /* we leave biSizeImage = 0; this is correct for uncompressed data */
+ if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */
+ PUT_4B(bmpinfoheader, 24, (INT32) (cinfo->X_density*100)); /* XPels/M */
+ PUT_4B(bmpinfoheader, 28, (INT32) (cinfo->Y_density*100)); /* XPels/M */
+ }
+ PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */
+ /* we leave biClrImportant = 0 */
+
+ if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+ if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t) 40)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+
+ if (cmap_entries > 0)
+ write_colormap(cinfo, dest, cmap_entries, 4);
+}
+
+
+LOCAL(void)
+write_os2_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
+/* Write an OS2-style BMP file header, including colormap if needed */
+{
+ char bmpfileheader[14];
+ char bmpcoreheader[12];
+ INT32 headersize, bfSize;
+ int bits_per_pixel, cmap_entries;
+
+ /* Compute colormap size and total file size */
+ if (cinfo->out_color_space == JCS_RGB) {
+ if (cinfo->quantize_colors) {
+ /* Colormapped RGB */
+ bits_per_pixel = 8;
+ cmap_entries = 256;
+ } else {
+ /* Unquantized, full color RGB */
+ bits_per_pixel = 24;
+ cmap_entries = 0;
+ }
+ } else {
+ /* Grayscale output. We need to fake a 256-entry colormap. */
+ bits_per_pixel = 8;
+ cmap_entries = 256;
+ }
+ /* File size */
+ headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */
+ bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
+
+ /* Set unused fields of header to 0 */
+ MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
+ MEMZERO(bmpcoreheader, SIZEOF(bmpcoreheader));
+
+ /* Fill the file header */
+ bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
+ bmpfileheader[1] = 0x4D;
+ PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
+ /* we leave bfReserved1 & bfReserved2 = 0 */
+ PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
+
+ /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */
+ PUT_2B(bmpcoreheader, 0, 12); /* bcSize */
+ PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
+ PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
+ PUT_2B(bmpcoreheader, 8, 1); /* bcPlanes - must be 1 */
+ PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */
+
+ if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+ if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t) 12)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+
+ if (cmap_entries > 0)
+ write_colormap(cinfo, dest, cmap_entries, 3);
+}
+
+
+/*
+ * Write the colormap.
+ * Windows uses BGR0 map entries; OS/2 uses BGR entries.
+ */
+
+LOCAL(void)
+write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
+ int map_colors, int map_entry_size)
+{
+ JSAMPARRAY colormap = cinfo->colormap;
+ int num_colors = cinfo->actual_number_of_colors;
+ FILE * outfile = dest->pub.output_file;
+ int i;
+
+ if (colormap != NULL) {
+ if (cinfo->out_color_components == 3) {
+ /* Normal case with RGB colormap */
+ for (i = 0; i < num_colors; i++) {
+ putc(GETJSAMPLE(colormap[2][i]), outfile);
+ putc(GETJSAMPLE(colormap[1][i]), outfile);
+ putc(GETJSAMPLE(colormap[0][i]), outfile);
+ if (map_entry_size == 4)
+ putc(0, outfile);
+ }
+ } else {
+ /* Grayscale colormap (only happens with grayscale quantization) */
+ for (i = 0; i < num_colors; i++) {
+ putc(GETJSAMPLE(colormap[0][i]), outfile);
+ putc(GETJSAMPLE(colormap[0][i]), outfile);
+ putc(GETJSAMPLE(colormap[0][i]), outfile);
+ if (map_entry_size == 4)
+ putc(0, outfile);
+ }
+ }
+ } else {
+ /* If no colormap, must be grayscale data. Generate a linear "map". */
+ for (i = 0; i < 256; i++) {
+ putc(i, outfile);
+ putc(i, outfile);
+ putc(i, outfile);
+ if (map_entry_size == 4)
+ putc(0, outfile);
+ }
+ }
+ /* Pad colormap with zeros to ensure specified number of colormap entries */
+ if (i > map_colors)
+ ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
+ for (; i < map_colors; i++) {
+ putc(0, outfile);
+ putc(0, outfile);
+ putc(0, outfile);
+ if (map_entry_size == 4)
+ putc(0, outfile);
+ }
+}
+
+
+METHODDEF(void)
+finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+ register FILE * outfile = dest->pub.output_file;
+ JSAMPARRAY image_ptr;
+ register JSAMPROW data_ptr;
+ JDIMENSION row;
+ register JDIMENSION col;
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+ /* Write the header and colormap */
+ if (dest->is_os2)
+ write_os2_header(cinfo, dest);
+ else
+ write_bmp_header(cinfo, dest);
+
+ /* Write the file body from our virtual array */
+ for (row = cinfo->output_height; row > 0; row--) {
+ if (progress != NULL) {
+ progress->pub.pass_counter = (long) (cinfo->output_height - row);
+ progress->pub.pass_limit = (long) cinfo->output_height;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+ image_ptr = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->whole_image, row-1, (JDIMENSION) 1, FALSE);
+ data_ptr = image_ptr[0];
+ for (col = dest->row_width; col > 0; col--) {
+ putc(GETJSAMPLE(*data_ptr), outfile);
+ data_ptr++;
+ }
+ }
+ if (progress != NULL)
+ progress->completed_extra_passes++;
+
+ /* Make sure we wrote the output file OK */
+ fflush(outfile);
+ if (ferror(outfile))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for BMP format output.
+ */
+
+GLOBAL(djpeg_dest_ptr)
+jinit_write_bmp (j_decompress_ptr cinfo, boolean is_os2)
+{
+ bmp_dest_ptr dest;
+ JDIMENSION row_width;
+
+ /* Create module interface object, fill in method pointers */
+ dest = (bmp_dest_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(bmp_dest_struct));
+ dest->pub.start_output = start_output_bmp;
+ dest->pub.finish_output = finish_output_bmp;
+ dest->is_os2 = is_os2;
+
+ if (cinfo->out_color_space == JCS_GRAYSCALE) {
+ dest->pub.put_pixel_rows = put_gray_rows;
+ } else if (cinfo->out_color_space == JCS_RGB) {
+ if (cinfo->quantize_colors)
+ dest->pub.put_pixel_rows = put_gray_rows;
+ else
+ dest->pub.put_pixel_rows = put_pixel_rows;
+ } else {
+ ERREXIT(cinfo, JERR_BMP_COLORSPACE);
+ }
+
+ /* Calculate output image dimensions so we can allocate space */
+ jpeg_calc_output_dimensions(cinfo);
+
+ /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
+ row_width = cinfo->output_width * cinfo->output_components;
+ dest->data_width = row_width;
+ while ((row_width & 3) != 0) row_width++;
+ dest->row_width = row_width;
+ dest->pad_bytes = (int) (row_width - dest->data_width);
+
+ /* Allocate space for inversion array, prepare for write pass */
+ dest->whole_image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ row_width, cinfo->output_height, (JDIMENSION) 1);
+ dest->cur_output_row = 0;
+ if (cinfo->progress != NULL) {
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+ progress->total_extra_passes++; /* count file input as separate pass */
+ }
+
+ /* Create decompressor output buffer. */
+ dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, row_width, (JDIMENSION) 1);
+ dest->pub.buffer_height = 1;
+
+ return (djpeg_dest_ptr) dest;
+}
+
+#endif /* BMP_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/wrgif.c b/test/monniaux/jpeg-6b/wrgif.c
new file mode 100644
index 00000000..5fe83283
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrgif.c
@@ -0,0 +1,399 @@
+/*
+ * wrgif.c
+ *
+ * Copyright (C) 1991-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in GIF format.
+ *
+ **************************************************************************
+ * NOTE: to avoid entanglements with Unisys' patent on LZW compression, *
+ * this code has been modified to output "uncompressed GIF" files. *
+ * There is no trace of the LZW algorithm in this file. *
+ **************************************************************************
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume output to
+ * an ordinary stdio stream.
+ */
+
+/*
+ * This code is loosely based on ppmtogif from the PBMPLUS distribution
+ * of Feb. 1991. That file contains the following copyright notice:
+ * Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.
+ * Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
+ * Copyright (C) 1989 by Jef Poskanzer.
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation. This software is provided "as is" without express or
+ * implied warranty.
+ *
+ * We are also required to state that
+ * "The Graphics Interchange Format(c) is the Copyright property of
+ * CompuServe Incorporated. GIF(sm) is a Service Mark property of
+ * CompuServe Incorporated."
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef GIF_SUPPORTED
+
+
+/* Private version of data destination object */
+
+typedef struct {
+ struct djpeg_dest_struct pub; /* public fields */
+
+ j_decompress_ptr cinfo; /* back link saves passing separate parm */
+
+ /* State for packing variable-width codes into a bitstream */
+ int n_bits; /* current number of bits/code */
+ int maxcode; /* maximum code, given n_bits */
+ INT32 cur_accum; /* holds bits not yet output */
+ int cur_bits; /* # of bits in cur_accum */
+
+ /* State for GIF code assignment */
+ int ClearCode; /* clear code (doesn't change) */
+ int EOFCode; /* EOF code (ditto) */
+ int code_counter; /* counts output symbols */
+
+ /* GIF data packet construction buffer */
+ int bytesinpkt; /* # of bytes in current packet */
+ char packetbuf[256]; /* workspace for accumulating packet */
+
+} gif_dest_struct;
+
+typedef gif_dest_struct * gif_dest_ptr;
+
+/* Largest value that will fit in N bits */
+#define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
+
+
+/*
+ * Routines to package finished data bytes into GIF data blocks.
+ * A data block consists of a count byte (1..255) and that many data bytes.
+ */
+
+LOCAL(void)
+flush_packet (gif_dest_ptr dinfo)
+/* flush any accumulated data */
+{
+ if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */
+ dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++;
+ if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt)
+ != (size_t) dinfo->bytesinpkt)
+ ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
+ dinfo->bytesinpkt = 0;
+ }
+}
+
+
+/* Add a character to current packet; flush to disk if necessary */
+#define CHAR_OUT(dinfo,c) \
+ { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \
+ if ((dinfo)->bytesinpkt >= 255) \
+ flush_packet(dinfo); \
+ }
+
+
+/* Routine to convert variable-width codes into a byte stream */
+
+LOCAL(void)
+output (gif_dest_ptr dinfo, int code)
+/* Emit a code of n_bits bits */
+/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
+{
+ dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
+ dinfo->cur_bits += dinfo->n_bits;
+
+ while (dinfo->cur_bits >= 8) {
+ CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
+ dinfo->cur_accum >>= 8;
+ dinfo->cur_bits -= 8;
+ }
+}
+
+
+/* The pseudo-compression algorithm.
+ *
+ * In this module we simply output each pixel value as a separate symbol;
+ * thus, no compression occurs. In fact, there is expansion of one bit per
+ * pixel, because we use a symbol width one bit wider than the pixel width.
+ *
+ * GIF ordinarily uses variable-width symbols, and the decoder will expect
+ * to ratchet up the symbol width after a fixed number of symbols.
+ * To simplify the logic and keep the expansion penalty down, we emit a
+ * GIF Clear code to reset the decoder just before the width would ratchet up.
+ * Thus, all the symbols in the output file will have the same bit width.
+ * Note that emitting the Clear codes at the right times is a mere matter of
+ * counting output symbols and is in no way dependent on the LZW patent.
+ *
+ * With a small basic pixel width (low color count), Clear codes will be
+ * needed very frequently, causing the file to expand even more. So this
+ * simplistic approach wouldn't work too well on bilevel images, for example.
+ * But for output of JPEG conversions the pixel width will usually be 8 bits
+ * (129 to 256 colors), so the overhead added by Clear symbols is only about
+ * one symbol in every 256.
+ */
+
+LOCAL(void)
+compress_init (gif_dest_ptr dinfo, int i_bits)
+/* Initialize pseudo-compressor */
+{
+ /* init all the state variables */
+ dinfo->n_bits = i_bits;
+ dinfo->maxcode = MAXCODE(dinfo->n_bits);
+ dinfo->ClearCode = (1 << (i_bits - 1));
+ dinfo->EOFCode = dinfo->ClearCode + 1;
+ dinfo->code_counter = dinfo->ClearCode + 2;
+ /* init output buffering vars */
+ dinfo->bytesinpkt = 0;
+ dinfo->cur_accum = 0;
+ dinfo->cur_bits = 0;
+ /* GIF specifies an initial Clear code */
+ output(dinfo, dinfo->ClearCode);
+}
+
+
+LOCAL(void)
+compress_pixel (gif_dest_ptr dinfo, int c)
+/* Accept and "compress" one pixel value.
+ * The given value must be less than n_bits wide.
+ */
+{
+ /* Output the given pixel value as a symbol. */
+ output(dinfo, c);
+ /* Issue Clear codes often enough to keep the reader from ratcheting up
+ * its symbol size.
+ */
+ if (dinfo->code_counter < dinfo->maxcode) {
+ dinfo->code_counter++;
+ } else {
+ output(dinfo, dinfo->ClearCode);
+ dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */
+ }
+}
+
+
+LOCAL(void)
+compress_term (gif_dest_ptr dinfo)
+/* Clean up at end */
+{
+ /* Send an EOF code */
+ output(dinfo, dinfo->EOFCode);
+ /* Flush the bit-packing buffer */
+ if (dinfo->cur_bits > 0) {
+ CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
+ }
+ /* Flush the packet buffer */
+ flush_packet(dinfo);
+}
+
+
+/* GIF header construction */
+
+
+LOCAL(void)
+put_word (gif_dest_ptr dinfo, unsigned int w)
+/* Emit a 16-bit word, LSB first */
+{
+ putc(w & 0xFF, dinfo->pub.output_file);
+ putc((w >> 8) & 0xFF, dinfo->pub.output_file);
+}
+
+
+LOCAL(void)
+put_3bytes (gif_dest_ptr dinfo, int val)
+/* Emit 3 copies of same byte value --- handy subr for colormap construction */
+{
+ putc(val, dinfo->pub.output_file);
+ putc(val, dinfo->pub.output_file);
+ putc(val, dinfo->pub.output_file);
+}
+
+
+LOCAL(void)
+emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap)
+/* Output the GIF file header, including color map */
+/* If colormap==NULL, synthesize a gray-scale colormap */
+{
+ int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
+ int cshift = dinfo->cinfo->data_precision - 8;
+ int i;
+
+ if (num_colors > 256)
+ ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors);
+ /* Compute bits/pixel and related values */
+ BitsPerPixel = 1;
+ while (num_colors > (1 << BitsPerPixel))
+ BitsPerPixel++;
+ ColorMapSize = 1 << BitsPerPixel;
+ if (BitsPerPixel <= 1)
+ InitCodeSize = 2;
+ else
+ InitCodeSize = BitsPerPixel;
+ /*
+ * Write the GIF header.
+ * Note that we generate a plain GIF87 header for maximum compatibility.
+ */
+ putc('G', dinfo->pub.output_file);
+ putc('I', dinfo->pub.output_file);
+ putc('F', dinfo->pub.output_file);
+ putc('8', dinfo->pub.output_file);
+ putc('7', dinfo->pub.output_file);
+ putc('a', dinfo->pub.output_file);
+ /* Write the Logical Screen Descriptor */
+ put_word(dinfo, (unsigned int) dinfo->cinfo->output_width);
+ put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
+ FlagByte = 0x80; /* Yes, there is a global color table */
+ FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */
+ FlagByte |= (BitsPerPixel-1); /* size of global color table */
+ putc(FlagByte, dinfo->pub.output_file);
+ putc(0, dinfo->pub.output_file); /* Background color index */
+ putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */
+ /* Write the Global Color Map */
+ /* If the color map is more than 8 bits precision, */
+ /* we reduce it to 8 bits by shifting */
+ for (i=0; i < ColorMapSize; i++) {
+ if (i < num_colors) {
+ if (colormap != NULL) {
+ if (dinfo->cinfo->out_color_space == JCS_RGB) {
+ /* Normal case: RGB color map */
+ putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file);
+ putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file);
+ putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file);
+ } else {
+ /* Grayscale "color map": possible if quantizing grayscale image */
+ put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift);
+ }
+ } else {
+ /* Create a gray-scale map of num_colors values, range 0..255 */
+ put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1));
+ }
+ } else {
+ /* fill out the map to a power of 2 */
+ put_3bytes(dinfo, 0);
+ }
+ }
+ /* Write image separator and Image Descriptor */
+ putc(',', dinfo->pub.output_file); /* separator */
+ put_word(dinfo, 0); /* left/top offset */
+ put_word(dinfo, 0);
+ put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */
+ put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
+ /* flag byte: not interlaced, no local color map */
+ putc(0x00, dinfo->pub.output_file);
+ /* Write Initial Code Size byte */
+ putc(InitCodeSize, dinfo->pub.output_file);
+
+ /* Initialize for "compression" of image data */
+ compress_init(dinfo, InitCodeSize+1);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF(void)
+start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+
+ if (cinfo->quantize_colors)
+ emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap);
+ else
+ emit_header(dest, 256, (JSAMPARRAY) NULL);
+}
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF(void)
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+
+ ptr = dest->pub.buffer[0];
+ for (col = cinfo->output_width; col > 0; col--) {
+ compress_pixel(dest, GETJSAMPLE(*ptr++));
+ }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+
+ /* Flush "compression" mechanism */
+ compress_term(dest);
+ /* Write a zero-length data block to end the series */
+ putc(0, dest->pub.output_file);
+ /* Write the GIF terminator mark */
+ putc(';', dest->pub.output_file);
+ /* Make sure we wrote the output file OK */
+ fflush(dest->pub.output_file);
+ if (ferror(dest->pub.output_file))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for GIF format output.
+ */
+
+GLOBAL(djpeg_dest_ptr)
+jinit_write_gif (j_decompress_ptr cinfo)
+{
+ gif_dest_ptr dest;
+
+ /* Create module interface object, fill in method pointers */
+ dest = (gif_dest_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(gif_dest_struct));
+ dest->cinfo = cinfo; /* make back link for subroutines */
+ dest->pub.start_output = start_output_gif;
+ dest->pub.put_pixel_rows = put_pixel_rows;
+ dest->pub.finish_output = finish_output_gif;
+
+ if (cinfo->out_color_space != JCS_GRAYSCALE &&
+ cinfo->out_color_space != JCS_RGB)
+ ERREXIT(cinfo, JERR_GIF_COLORSPACE);
+
+ /* Force quantization if color or if > 8 bits input */
+ if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) {
+ /* Force quantization to at most 256 colors */
+ cinfo->quantize_colors = TRUE;
+ if (cinfo->desired_number_of_colors > 256)
+ cinfo->desired_number_of_colors = 256;
+ }
+
+ /* Calculate output image dimensions so we can allocate space */
+ jpeg_calc_output_dimensions(cinfo);
+
+ if (cinfo->output_components != 1) /* safety check: just one component? */
+ ERREXIT(cinfo, JERR_GIF_BUG);
+
+ /* Create decompressor output buffer. */
+ dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1);
+ dest->pub.buffer_height = 1;
+
+ return (djpeg_dest_ptr) dest;
+}
+
+#endif /* GIF_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/wrjpgcom.1 b/test/monniaux/jpeg-6b/wrjpgcom.1
new file mode 100644
index 00000000..d419a999
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrjpgcom.1
@@ -0,0 +1,103 @@
+.TH WRJPGCOM 1 "15 June 1995"
+.SH NAME
+wrjpgcom \- insert text comments into a JPEG file
+.SH SYNOPSIS
+.B wrjpgcom
+[
+.B \-replace
+]
+[
+.BI \-comment " text"
+]
+[
+.BI \-cfile " name"
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B wrjpgcom
+reads the named JPEG/JFIF file, or the standard input if no file is named,
+and generates a new JPEG/JFIF file on standard output. A comment block is
+added to the file.
+.PP
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings. This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text. COM blocks do not interfere with the image stored in the JPEG
+file. The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+.PP
+.B wrjpgcom
+adds a COM block, containing text you provide, to a JPEG file.
+Ordinarily, the COM block is added after any existing COM blocks; but you
+can delete the old COM blocks if you wish.
+.SH OPTIONS
+Switch names may be abbreviated, and are not case sensitive.
+.TP
+.B \-replace
+Delete any existing COM blocks from the file.
+.TP
+.BI \-comment " text"
+Supply text for new COM block on command line.
+.TP
+.BI \-cfile " name"
+Read text for new COM block from named file.
+.PP
+If you have only one line of comment text to add, you can provide it on the
+command line with
+.BR \-comment .
+The comment text must be surrounded with quotes so that it is treated as a
+single argument. Longer comments can be read from a text file.
+.PP
+If you give neither
+.B \-comment
+nor
+.BR \-cfile ,
+then
+.B wrjpgcom
+will read the comment text from standard input. (In this case an input image
+file name MUST be supplied, so that the source JPEG file comes from somewhere
+else.) You can enter multiple lines, up to 64KB worth. Type an end-of-file
+indicator (usually control-D) to terminate the comment text entry.
+.PP
+.B wrjpgcom
+will not add a COM block if the provided comment string is empty. Therefore
+\fB\-replace \-comment ""\fR can be used to delete all COM blocks from a file.
+.SH EXAMPLES
+.LP
+Add a short comment to in.jpg, producing out.jpg:
+.IP
+.B wrjpgcom \-c
+\fI"View of my back yard" in.jpg
+.B >
+.I out.jpg
+.PP
+Attach a long comment previously stored in comment.txt:
+.IP
+.B wrjpgcom
+.I in.jpg
+.B <
+.I comment.txt
+.B >
+.I out.jpg
+.PP
+or equivalently
+.IP
+.B wrjpgcom
+.B -cfile
+.I comment.txt
+.B <
+.I in.jpg
+.B >
+.I out.jpg
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR djpeg (1),
+.BR jpegtran (1),
+.BR rdjpgcom (1)
+.SH AUTHOR
+Independent JPEG Group
diff --git a/test/monniaux/jpeg-6b/wrjpgcom.c b/test/monniaux/jpeg-6b/wrjpgcom.c
new file mode 100644
index 00000000..8c04b055
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrjpgcom.c
@@ -0,0 +1,583 @@
+/*
+ * wrjpgcom.c
+ *
+ * Copyright (C) 1994-1997, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a very simple stand-alone application that inserts
+ * user-supplied text as a COM (comment) marker in a JFIF file.
+ * This may be useful as an example of the minimum logic needed to parse
+ * JPEG markers.
+ */
+
+#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
+#include "jinclude.h" /* get auto-config symbols, <stdio.h> */
+
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc() */
+extern void * malloc ();
+#endif
+#include <ctype.h> /* to declare isupper(), tolower() */
+#ifdef USE_SETMODE
+#include <fcntl.h> /* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h> /* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h> /* Metrowerks needs this */
+#include <console.h> /* ... and this */
+#endif
+#ifdef THINK_C
+#include <console.h> /* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#define WRITE_BINARY "w"
+#else
+#ifdef VMS /* VMS is very nonstandard */
+#define READ_BINARY "rb", "ctx=stm"
+#define WRITE_BINARY "wb", "ctx=stm"
+#else /* standard ANSI-compliant case */
+#define READ_BINARY "rb"
+#define WRITE_BINARY "wb"
+#endif
+#endif
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS 0
+#endif
+#endif
+
+/* Reduce this value if your malloc() can't allocate blocks up to 64K.
+ * On DOS, compiling in large model is usually a better solution.
+ */
+
+#ifndef MAX_COM_LENGTH
+#define MAX_COM_LENGTH 65000L /* must be <= 65533 in any case */
+#endif
+
+
+/*
+ * These macros are used to read the input file and write the output file.
+ * To reuse this code in another application, you might need to change these.
+ */
+
+static FILE * infile; /* input JPEG file */
+
+/* Return next input byte, or EOF if no more */
+#define NEXTBYTE() getc(infile)
+
+static FILE * outfile; /* output JPEG file */
+
+/* Emit an output byte */
+#define PUTBYTE(x) putc((x), outfile)
+
+
+/* Error exit handler */
+#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
+
+
+/* Read one byte, testing for EOF */
+static int
+read_1_byte (void)
+{
+ int c;
+
+ c = NEXTBYTE();
+ if (c == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ return c;
+}
+
+/* Read 2 bytes, convert to unsigned int */
+/* All 2-byte quantities in JPEG markers are MSB first */
+static unsigned int
+read_2_bytes (void)
+{
+ int c1, c2;
+
+ c1 = NEXTBYTE();
+ if (c1 == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ c2 = NEXTBYTE();
+ if (c2 == EOF)
+ ERREXIT("Premature EOF in JPEG file");
+ return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+}
+
+
+/* Routines to write data to output file */
+
+static void
+write_1_byte (int c)
+{
+ PUTBYTE(c);
+}
+
+static void
+write_2_bytes (unsigned int val)
+{
+ PUTBYTE((val >> 8) & 0xFF);
+ PUTBYTE(val & 0xFF);
+}
+
+static void
+write_marker (int marker)
+{
+ PUTBYTE(0xFF);
+ PUTBYTE(marker);
+}
+
+static void
+copy_rest_of_file (void)
+{
+ int c;
+
+ while ((c = NEXTBYTE()) != EOF)
+ PUTBYTE(c);
+}
+
+
+/*
+ * JPEG markers consist of one or more 0xFF bytes, followed by a marker
+ * code byte (which is not an FF). Here are the marker codes of interest
+ * in this program. (See jdmarker.c for a more complete list.)
+ */
+
+#define M_SOF0 0xC0 /* Start Of Frame N */
+#define M_SOF1 0xC1 /* N indicates which compression process */
+#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
+#define M_SOF3 0xC3
+#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
+#define M_SOF6 0xC6
+#define M_SOF7 0xC7
+#define M_SOF9 0xC9
+#define M_SOF10 0xCA
+#define M_SOF11 0xCB
+#define M_SOF13 0xCD
+#define M_SOF14 0xCE
+#define M_SOF15 0xCF
+#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
+#define M_EOI 0xD9 /* End Of Image (end of datastream) */
+#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
+#define M_COM 0xFE /* COMment */
+
+
+/*
+ * Find the next JPEG marker and return its marker code.
+ * We expect at least one FF byte, possibly more if the compressor used FFs
+ * to pad the file. (Padding FFs will NOT be replicated in the output file.)
+ * There could also be non-FF garbage between markers. The treatment of such
+ * garbage is unspecified; we choose to skip over it but emit a warning msg.
+ * NB: this routine must not be used after seeing SOS marker, since it will
+ * not deal correctly with FF/00 sequences in the compressed image data...
+ */
+
+static int
+next_marker (void)
+{
+ int c;
+ int discarded_bytes = 0;
+
+ /* Find 0xFF byte; count and skip any non-FFs. */
+ c = read_1_byte();
+ while (c != 0xFF) {
+ discarded_bytes++;
+ c = read_1_byte();
+ }
+ /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
+ * are legal as pad bytes, so don't count them in discarded_bytes.
+ */
+ do {
+ c = read_1_byte();
+ } while (c == 0xFF);
+
+ if (discarded_bytes != 0) {
+ fprintf(stderr, "Warning: garbage data found in JPEG file\n");
+ }
+
+ return c;
+}
+
+
+/*
+ * Read the initial marker, which should be SOI.
+ * For a JFIF file, the first two bytes of the file should be literally
+ * 0xFF M_SOI. To be more general, we could use next_marker, but if the
+ * input file weren't actually JPEG at all, next_marker might read the whole
+ * file and then return a misleading error message...
+ */
+
+static int
+first_marker (void)
+{
+ int c1, c2;
+
+ c1 = NEXTBYTE();
+ c2 = NEXTBYTE();
+ if (c1 != 0xFF || c2 != M_SOI)
+ ERREXIT("Not a JPEG file");
+ return c2;
+}
+
+
+/*
+ * Most types of marker are followed by a variable-length parameter segment.
+ * This routine skips over the parameters for any marker we don't otherwise
+ * want to process.
+ * Note that we MUST skip the parameter segment explicitly in order not to
+ * be fooled by 0xFF bytes that might appear within the parameter segment;
+ * such bytes do NOT introduce new markers.
+ */
+
+static void
+copy_variable (void)
+/* Copy an unknown or uninteresting variable-length marker */
+{
+ unsigned int length;
+
+ /* Get the marker parameter length count */
+ length = read_2_bytes();
+ write_2_bytes(length);
+ /* Length includes itself, so must be at least 2 */
+ if (length < 2)
+ ERREXIT("Erroneous JPEG marker length");
+ length -= 2;
+ /* Skip over the remaining bytes */
+ while (length > 0) {
+ write_1_byte(read_1_byte());
+ length--;
+ }
+}
+
+static void
+skip_variable (void)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+ unsigned int length;
+
+ /* Get the marker parameter length count */
+ length = read_2_bytes();
+ /* Length includes itself, so must be at least 2 */
+ if (length < 2)
+ ERREXIT("Erroneous JPEG marker length");
+ length -= 2;
+ /* Skip over the remaining bytes */
+ while (length > 0) {
+ (void) read_1_byte();
+ length--;
+ }
+}
+
+
+/*
+ * Parse the marker stream until SOFn or EOI is seen;
+ * copy data to output, but discard COM markers unless keep_COM is true.
+ */
+
+static int
+scan_JPEG_header (int keep_COM)
+{
+ int marker;
+
+ /* Expect SOI at start of file */
+ if (first_marker() != M_SOI)
+ ERREXIT("Expected SOI marker first");
+ write_marker(M_SOI);
+
+ /* Scan miscellaneous markers until we reach SOFn. */
+ for (;;) {
+ marker = next_marker();
+ switch (marker) {
+ /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
+ * treated as SOFn. C4 in particular is actually DHT.
+ */
+ case M_SOF0: /* Baseline */
+ case M_SOF1: /* Extended sequential, Huffman */
+ case M_SOF2: /* Progressive, Huffman */
+ case M_SOF3: /* Lossless, Huffman */
+ case M_SOF5: /* Differential sequential, Huffman */
+ case M_SOF6: /* Differential progressive, Huffman */
+ case M_SOF7: /* Differential lossless, Huffman */
+ case M_SOF9: /* Extended sequential, arithmetic */
+ case M_SOF10: /* Progressive, arithmetic */
+ case M_SOF11: /* Lossless, arithmetic */
+ case M_SOF13: /* Differential sequential, arithmetic */
+ case M_SOF14: /* Differential progressive, arithmetic */
+ case M_SOF15: /* Differential lossless, arithmetic */
+ return marker;
+
+ case M_SOS: /* should not see compressed data before SOF */
+ ERREXIT("SOS without prior SOFn");
+ break;
+
+ case M_EOI: /* in case it's a tables-only JPEG stream */
+ return marker;
+
+ case M_COM: /* Existing COM: conditionally discard */
+ if (keep_COM) {
+ write_marker(marker);
+ copy_variable();
+ } else {
+ skip_variable();
+ }
+ break;
+
+ default: /* Anything else just gets copied */
+ write_marker(marker);
+ copy_variable(); /* we assume it has a parameter count... */
+ break;
+ }
+ } /* end loop */
+}
+
+
+/* Command line parsing code */
+
+static const char * progname; /* program name for error messages */
+
+
+static void
+usage (void)
+/* complain about bad command line */
+{
+ fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
+ fprintf(stderr, "You can add to or replace any existing comment(s).\n");
+
+ fprintf(stderr, "Usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+ fprintf(stderr, "inputfile outputfile\n");
+#else
+ fprintf(stderr, "[inputfile]\n");
+#endif
+
+ fprintf(stderr, "Switches (names may be abbreviated):\n");
+ fprintf(stderr, " -replace Delete any existing comments\n");
+ fprintf(stderr, " -comment \"text\" Insert comment with given text\n");
+ fprintf(stderr, " -cfile name Read comment from named file\n");
+ fprintf(stderr, "Notice that you must put quotes around the comment text\n");
+ fprintf(stderr, "when you use -comment.\n");
+ fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
+ fprintf(stderr, "then the comment text is read from standard input.\n");
+ fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
+ (unsigned int) MAX_COM_LENGTH);
+#ifndef TWO_FILE_COMMANDLINE
+ fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
+ fprintf(stderr, "comment text from standard input.\n");
+#endif
+
+ exit(EXIT_FAILURE);
+}
+
+
+static int
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+ register int ca, ck;
+ register int nmatched = 0;
+
+ while ((ca = *arg++) != '\0') {
+ if ((ck = *keyword++) == '\0')
+ return 0; /* arg longer than keyword, no good */
+ if (isupper(ca)) /* force arg to lcase (assume ck is already) */
+ ca = tolower(ca);
+ if (ca != ck)
+ return 0; /* no good */
+ nmatched++; /* count matched characters */
+ }
+ /* reached end of argument; fail if it's too short for unique abbrev */
+ if (nmatched < minchars)
+ return 0;
+ return 1; /* A-OK */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+ int argn;
+ char * arg;
+ int keep_COM = 1;
+ char * comment_arg = NULL;
+ FILE * comment_file = NULL;
+ unsigned int comment_length = 0;
+ int marker;
+
+ /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+ argc = ccommand(&argv);
+#endif
+
+ progname = argv[0];
+ if (progname == NULL || progname[0] == 0)
+ progname = "wrjpgcom"; /* in case C library doesn't provide it */
+
+ /* Parse switches, if any */
+ for (argn = 1; argn < argc; argn++) {
+ arg = argv[argn];
+ if (arg[0] != '-')
+ break; /* not switch, must be file name */
+ arg++; /* advance over '-' */
+ if (keymatch(arg, "replace", 1)) {
+ keep_COM = 0;
+ } else if (keymatch(arg, "cfile", 2)) {
+ if (++argn >= argc) usage();
+ if ((comment_file = fopen(argv[argn], "r")) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+ exit(EXIT_FAILURE);
+ }
+ } else if (keymatch(arg, "comment", 1)) {
+ if (++argn >= argc) usage();
+ comment_arg = argv[argn];
+ /* If the comment text starts with '"', then we are probably running
+ * under MS-DOG and must parse out the quoted string ourselves. Sigh.
+ */
+ if (comment_arg[0] == '"') {
+ comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+ if (comment_arg == NULL)
+ ERREXIT("Insufficient memory");
+ strcpy(comment_arg, argv[argn]+1);
+ for (;;) {
+ comment_length = (unsigned int) strlen(comment_arg);
+ if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
+ comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
+ break;
+ }
+ if (++argn >= argc)
+ ERREXIT("Missing ending quote mark");
+ strcat(comment_arg, " ");
+ strcat(comment_arg, argv[argn]);
+ }
+ }
+ comment_length = (unsigned int) strlen(comment_arg);
+ } else
+ usage();
+ }
+
+ /* Cannot use both -comment and -cfile. */
+ if (comment_arg != NULL && comment_file != NULL)
+ usage();
+ /* If there is neither -comment nor -cfile, we will read the comment text
+ * from stdin; in this case there MUST be an input JPEG file name.
+ */
+ if (comment_arg == NULL && comment_file == NULL && argn >= argc)
+ usage();
+
+ /* Open the input file. */
+ if (argn < argc) {
+ if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* default input file is stdin */
+#ifdef USE_SETMODE /* need to hack file mode? */
+ setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN /* need to re-open in binary mode? */
+ if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open stdin\n", progname);
+ exit(EXIT_FAILURE);
+ }
+#else
+ infile = stdin;
+#endif
+ }
+
+ /* Open the output file. */
+#ifdef TWO_FILE_COMMANDLINE
+ /* Must have explicit output file name */
+ if (argn != argc-2) {
+ fprintf(stderr, "%s: must name one input and one output file\n",
+ progname);
+ usage();
+ }
+ if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]);
+ exit(EXIT_FAILURE);
+ }
+#else
+ /* Unix style: expect zero or one file name */
+ if (argn < argc-1) {
+ fprintf(stderr, "%s: only one input file\n", progname);
+ usage();
+ }
+ /* default output file is stdout */
+#ifdef USE_SETMODE /* need to hack file mode? */
+ setmode(fileno(stdout), O_BINARY);
+#endif
+#ifdef USE_FDOPEN /* need to re-open in binary mode? */
+ if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
+ fprintf(stderr, "%s: can't open stdout\n", progname);
+ exit(EXIT_FAILURE);
+ }
+#else
+ outfile = stdout;
+#endif
+#endif /* TWO_FILE_COMMANDLINE */
+
+ /* Collect comment text from comment_file or stdin, if necessary */
+ if (comment_arg == NULL) {
+ FILE * src_file;
+ int c;
+
+ comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+ if (comment_arg == NULL)
+ ERREXIT("Insufficient memory");
+ comment_length = 0;
+ src_file = (comment_file != NULL ? comment_file : stdin);
+ while ((c = getc(src_file)) != EOF) {
+ if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
+ fprintf(stderr, "Comment text may not exceed %u bytes\n",
+ (unsigned int) MAX_COM_LENGTH);
+ exit(EXIT_FAILURE);
+ }
+ comment_arg[comment_length++] = (char) c;
+ }
+ if (comment_file != NULL)
+ fclose(comment_file);
+ }
+
+ /* Copy JPEG headers until SOFn marker;
+ * we will insert the new comment marker just before SOFn.
+ * This (a) causes the new comment to appear after, rather than before,
+ * existing comments; and (b) ensures that comments come after any JFIF
+ * or JFXX markers, as required by the JFIF specification.
+ */
+ marker = scan_JPEG_header(keep_COM);
+ /* Insert the new COM marker, but only if nonempty text has been supplied */
+ if (comment_length > 0) {
+ write_marker(M_COM);
+ write_2_bytes(comment_length + 2);
+ while (comment_length > 0) {
+ write_1_byte(*comment_arg++);
+ comment_length--;
+ }
+ }
+ /* Duplicate the remainder of the source file.
+ * Note that any COM markers occuring after SOF will not be touched.
+ */
+ write_marker(marker);
+ copy_rest_of_file();
+
+ /* All done. */
+ exit(EXIT_SUCCESS);
+ return 0; /* suppress no-return-value warnings */
+}
diff --git a/test/monniaux/jpeg-6b/wrppm.c b/test/monniaux/jpeg-6b/wrppm.c
new file mode 100644
index 00000000..6c6d9088
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrppm.c
@@ -0,0 +1,268 @@
+/*
+ * wrppm.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in PPM/PGM format.
+ * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
+ * The PBMPLUS library is NOT required to compile this software
+ * (but it is highly useful as a set of PPM image manipulation programs).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume output to
+ * an ordinary stdio stream.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef PPM_SUPPORTED
+
+
+/*
+ * For 12-bit JPEG data, we either downscale the values to 8 bits
+ * (to write standard byte-per-sample PPM/PGM files), or output
+ * nonstandard word-per-sample PPM/PGM files. Downscaling is done
+ * if PPM_NORAWWORD is defined (this can be done in the Makefile
+ * or in jconfig.h).
+ * (When the core library supports data precision reduction, a cleaner
+ * implementation will be to ask for that instead.)
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
+#define BYTESPERSAMPLE 1
+#define PPM_MAXVAL 255
+#else
+#ifdef PPM_NORAWWORD
+#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
+#define BYTESPERSAMPLE 1
+#define PPM_MAXVAL 255
+#else
+/* The word-per-sample format always puts the LSB first. */
+#define PUTPPMSAMPLE(ptr,v) \
+ { register int val_ = v; \
+ *ptr++ = (char) (val_ & 0xFF); \
+ *ptr++ = (char) ((val_ >> 8) & 0xFF); \
+ }
+#define BYTESPERSAMPLE 2
+#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
+#endif
+#endif
+
+
+/*
+ * When JSAMPLE is the same size as char, we can just fwrite() the
+ * decompressed data to the PPM or PGM file. On PCs, in order to make this
+ * work the output buffer must be allocated in near data space, because we are
+ * assuming small-data memory model wherein fwrite() can't reach far memory.
+ * If you need to process very wide images on a PC, you might have to compile
+ * in large-memory model, or else replace fwrite() with a putc() loop ---
+ * which will be much slower.
+ */
+
+
+/* Private version of data destination object */
+
+typedef struct {
+ struct djpeg_dest_struct pub; /* public fields */
+
+ /* Usually these two pointers point to the same place: */
+ char *iobuffer; /* fwrite's I/O buffer */
+ JSAMPROW pixrow; /* decompressor output buffer */
+ size_t buffer_width; /* width of I/O buffer */
+ JDIMENSION samples_per_row; /* JSAMPLEs per output row */
+} ppm_dest_struct;
+
+typedef ppm_dest_struct * ppm_dest_ptr;
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ *
+ * put_pixel_rows handles the "normal" 8-bit case where the decompressor
+ * output buffer is physically the same as the fwrite buffer.
+ */
+
+METHODDEF(void)
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * This code is used when we have to copy the data and apply a pixel
+ * format translation. Typically this only happens in 12-bit mode.
+ */
+
+METHODDEF(void)
+copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+ register char * bufferptr;
+ register JSAMPROW ptr;
+ register JDIMENSION col;
+
+ ptr = dest->pub.buffer[0];
+ bufferptr = dest->iobuffer;
+ for (col = dest->samples_per_row; col > 0; col--) {
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Write some pixel data when color quantization is in effect.
+ * We have to demap the color index values to straight data.
+ */
+
+METHODDEF(void)
+put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+ register char * bufferptr;
+ register int pixval;
+ register JSAMPROW ptr;
+ register JSAMPROW color_map0 = cinfo->colormap[0];
+ register JSAMPROW color_map1 = cinfo->colormap[1];
+ register JSAMPROW color_map2 = cinfo->colormap[2];
+ register JDIMENSION col;
+
+ ptr = dest->pub.buffer[0];
+ bufferptr = dest->iobuffer;
+ for (col = cinfo->output_width; col > 0; col--) {
+ pixval = GETJSAMPLE(*ptr++);
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+METHODDEF(void)
+put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+ register char * bufferptr;
+ register JSAMPROW ptr;
+ register JSAMPROW color_map = cinfo->colormap[0];
+ register JDIMENSION col;
+
+ ptr = dest->pub.buffer[0];
+ bufferptr = dest->iobuffer;
+ for (col = cinfo->output_width; col > 0; col--) {
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF(void)
+start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+
+ /* Emit file header */
+ switch (cinfo->out_color_space) {
+ case JCS_GRAYSCALE:
+ /* emit header for raw PGM format */
+ fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
+ (long) cinfo->output_width, (long) cinfo->output_height,
+ PPM_MAXVAL);
+ break;
+ case JCS_RGB:
+ /* emit header for raw PPM format */
+ fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
+ (long) cinfo->output_width, (long) cinfo->output_height,
+ PPM_MAXVAL);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_PPM_COLORSPACE);
+ }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ /* Make sure we wrote the output file OK */
+ fflush(dinfo->output_file);
+ if (ferror(dinfo->output_file))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for PPM format output.
+ */
+
+GLOBAL(djpeg_dest_ptr)
+jinit_write_ppm (j_decompress_ptr cinfo)
+{
+ ppm_dest_ptr dest;
+
+ /* Create module interface object, fill in method pointers */
+ dest = (ppm_dest_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(ppm_dest_struct));
+ dest->pub.start_output = start_output_ppm;
+ dest->pub.finish_output = finish_output_ppm;
+
+ /* Calculate output image dimensions so we can allocate space */
+ jpeg_calc_output_dimensions(cinfo);
+
+ /* Create physical I/O buffer. Note we make this near on a PC. */
+ dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
+ dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
+ dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
+
+ if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
+ SIZEOF(JSAMPLE) != SIZEOF(char)) {
+ /* When quantizing, we need an output buffer for colormap indexes
+ * that's separate from the physical I/O buffer. We also need a
+ * separate buffer if pixel format translation must take place.
+ */
+ dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
+ dest->pub.buffer_height = 1;
+ if (! cinfo->quantize_colors)
+ dest->pub.put_pixel_rows = copy_pixel_rows;
+ else if (cinfo->out_color_space == JCS_GRAYSCALE)
+ dest->pub.put_pixel_rows = put_demapped_gray;
+ else
+ dest->pub.put_pixel_rows = put_demapped_rgb;
+ } else {
+ /* We will fwrite() directly from decompressor output buffer. */
+ /* Synthesize a JSAMPARRAY pointer structure */
+ /* Cast here implies near->far pointer conversion on PCs */
+ dest->pixrow = (JSAMPROW) dest->iobuffer;
+ dest->pub.buffer = & dest->pixrow;
+ dest->pub.buffer_height = 1;
+ dest->pub.put_pixel_rows = put_pixel_rows;
+ }
+
+ return (djpeg_dest_ptr) dest;
+}
+
+#endif /* PPM_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/wrrle.c b/test/monniaux/jpeg-6b/wrrle.c
new file mode 100644
index 00000000..a4e73372
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrrle.c
@@ -0,0 +1,305 @@
+/*
+ * wrrle.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in RLE format.
+ * The Utah Raster Toolkit library is required (version 3.1 or later).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * Based on code contributed by Mike Lijewski,
+ * with updates from Robert Hutchinson.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef RLE_SUPPORTED
+
+/* rle.h is provided by the Utah Raster Toolkit. */
+
+#include <rle.h>
+
+/*
+ * We assume that JSAMPLE has the same representation as rle_pixel,
+ * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+ Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * Since RLE stores scanlines bottom-to-top, we have to invert the image
+ * from JPEG's top-to-bottom order. To do this, we save the outgoing data
+ * in a virtual array during put_pixel_row calls, then actually emit the
+ * RLE file during finish_output.
+ */
+
+
+/*
+ * For now, if we emit an RLE color map then it is always 256 entries long,
+ * though not all of the entries need be used.
+ */
+
+#define CMAPBITS 8
+#define CMAPLENGTH (1<<(CMAPBITS))
+
+typedef struct {
+ struct djpeg_dest_struct pub; /* public fields */
+
+ jvirt_sarray_ptr image; /* virtual array to store the output image */
+ rle_map *colormap; /* RLE-style color map, or NULL if none */
+ rle_pixel **rle_row; /* To pass rows to rle_putrow() */
+
+} rle_dest_struct;
+
+typedef rle_dest_struct * rle_dest_ptr;
+
+/* Forward declarations */
+METHODDEF(void) rle_put_pixel_rows
+ JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied));
+
+
+/*
+ * Write the file header.
+ *
+ * In this module it's easier to wait till finish_output to write anything.
+ */
+
+METHODDEF(void)
+start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+ size_t cmapsize;
+ int i, ci;
+#ifdef PROGRESS_REPORT
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+ /*
+ * Make sure the image can be stored in RLE format.
+ *
+ * - RLE stores image dimensions as *signed* 16 bit integers. JPEG
+ * uses unsigned, so we have to check the width.
+ *
+ * - Colorspace is expected to be grayscale or RGB.
+ *
+ * - The number of channels (components) is expected to be 1 (grayscale/
+ * pseudocolor) or 3 (truecolor/directcolor).
+ * (could be 2 or 4 if using an alpha channel, but we aren't)
+ */
+
+ if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
+ ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
+ cinfo->output_height);
+
+ if (cinfo->out_color_space != JCS_GRAYSCALE &&
+ cinfo->out_color_space != JCS_RGB)
+ ERREXIT(cinfo, JERR_RLE_COLORSPACE);
+
+ if (cinfo->output_components != 1 && cinfo->output_components != 3)
+ ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
+
+ /* Convert colormap, if any, to RLE format. */
+
+ dest->colormap = NULL;
+
+ if (cinfo->quantize_colors) {
+ /* Allocate storage for RLE-style cmap, zero any extra entries */
+ cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map);
+ dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
+ MEMZERO(dest->colormap, cmapsize);
+
+ /* Save away data in RLE format --- note 8-bit left shift! */
+ /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
+ for (ci = 0; ci < cinfo->out_color_components; ci++) {
+ for (i = 0; i < cinfo->actual_number_of_colors; i++) {
+ dest->colormap[ci * CMAPLENGTH + i] =
+ GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
+ }
+ }
+ }
+
+ /* Set the output buffer to the first row */
+ dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
+ dest->pub.buffer_height = 1;
+
+ dest->pub.put_pixel_rows = rle_put_pixel_rows;
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->total_extra_passes++; /* count file writing as separate pass */
+ }
+#endif
+}
+
+
+/*
+ * Write some pixel data.
+ *
+ * This routine just saves the data away in a virtual array.
+ */
+
+METHODDEF(void)
+rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+
+ if (cinfo->output_scanline < cinfo->output_height) {
+ dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->image,
+ cinfo->output_scanline, (JDIMENSION) 1, TRUE);
+ }
+}
+
+/*
+ * Finish up at the end of the file.
+ *
+ * Here is where we really output the RLE file.
+ */
+
+METHODDEF(void)
+finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+ rle_hdr header; /* Output file information */
+ rle_pixel **rle_row, *red, *green, *blue;
+ JSAMPROW output_row;
+ char cmapcomment[80];
+ int row, col;
+ int ci;
+#ifdef PROGRESS_REPORT
+ cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+ /* Initialize the header info */
+ header = *rle_hdr_init(NULL);
+ header.rle_file = dest->pub.output_file;
+ header.xmin = 0;
+ header.xmax = cinfo->output_width - 1;
+ header.ymin = 0;
+ header.ymax = cinfo->output_height - 1;
+ header.alpha = 0;
+ header.ncolors = cinfo->output_components;
+ for (ci = 0; ci < cinfo->output_components; ci++) {
+ RLE_SET_BIT(header, ci);
+ }
+ if (cinfo->quantize_colors) {
+ header.ncmap = cinfo->out_color_components;
+ header.cmaplen = CMAPBITS;
+ header.cmap = dest->colormap;
+ /* Add a comment to the output image with the true colormap length. */
+ sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
+ rle_putcom(cmapcomment, &header);
+ }
+
+ /* Emit the RLE header and color map (if any) */
+ rle_put_setup(&header);
+
+ /* Now output the RLE data from our virtual array.
+ * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
+ * and (b) we are not on a machine where FAR pointers differ from regular.
+ */
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_limit = cinfo->output_height;
+ progress->pub.pass_counter = 0;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+
+ if (cinfo->output_components == 1) {
+ for (row = cinfo->output_height-1; row >= 0; row--) {
+ rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->image,
+ (JDIMENSION) row, (JDIMENSION) 1, FALSE);
+ rle_putrow(rle_row, (int) cinfo->output_width, &header);
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_counter++;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+ }
+ } else {
+ for (row = cinfo->output_height-1; row >= 0; row--) {
+ rle_row = (rle_pixel **) dest->rle_row;
+ output_row = * (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, dest->image,
+ (JDIMENSION) row, (JDIMENSION) 1, FALSE);
+ red = rle_row[0];
+ green = rle_row[1];
+ blue = rle_row[2];
+ for (col = cinfo->output_width; col > 0; col--) {
+ *red++ = GETJSAMPLE(*output_row++);
+ *green++ = GETJSAMPLE(*output_row++);
+ *blue++ = GETJSAMPLE(*output_row++);
+ }
+ rle_putrow(rle_row, (int) cinfo->output_width, &header);
+#ifdef PROGRESS_REPORT
+ if (progress != NULL) {
+ progress->pub.pass_counter++;
+ (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+ }
+#endif
+ }
+ }
+
+#ifdef PROGRESS_REPORT
+ if (progress != NULL)
+ progress->completed_extra_passes++;
+#endif
+
+ /* Emit file trailer */
+ rle_puteof(&header);
+ fflush(dest->pub.output_file);
+ if (ferror(dest->pub.output_file))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for RLE format output.
+ */
+
+GLOBAL(djpeg_dest_ptr)
+jinit_write_rle (j_decompress_ptr cinfo)
+{
+ rle_dest_ptr dest;
+
+ /* Create module interface object, fill in method pointers */
+ dest = (rle_dest_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(rle_dest_struct));
+ dest->pub.start_output = start_output_rle;
+ dest->pub.finish_output = finish_output_rle;
+
+ /* Calculate output image dimensions so we can allocate space */
+ jpeg_calc_output_dimensions(cinfo);
+
+ /* Allocate a work array for output to the RLE library. */
+ dest->rle_row = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->output_width, (JDIMENSION) cinfo->output_components);
+
+ /* Allocate a virtual array to hold the image. */
+ dest->image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) (cinfo->output_width * cinfo->output_components),
+ cinfo->output_height, (JDIMENSION) 1);
+
+ return (djpeg_dest_ptr) dest;
+}
+
+#endif /* RLE_SUPPORTED */
diff --git a/test/monniaux/jpeg-6b/wrtarga.c b/test/monniaux/jpeg-6b/wrtarga.c
new file mode 100644
index 00000000..cf104d2d
--- /dev/null
+++ b/test/monniaux/jpeg-6b/wrtarga.c
@@ -0,0 +1,253 @@
+/*
+ * wrtarga.c
+ *
+ * Copyright (C) 1991-1996, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in Targa format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications. As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * Based on code contributed by Lee Daniel Crocker.
+ */
+
+#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
+
+#ifdef TARGA_SUPPORTED
+
+
+/*
+ * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
+ * This is not yet implemented.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+ Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * The output buffer needs to be writable by fwrite(). On PCs, we must
+ * allocate the buffer in near data space, because we are assuming small-data
+ * memory model, wherein fwrite() can't reach far memory. If you need to
+ * process very wide images on a PC, you might have to compile in large-memory
+ * model, or else replace fwrite() with a putc() loop --- which will be much
+ * slower.
+ */
+
+
+/* Private version of data destination object */
+
+typedef struct {
+ struct djpeg_dest_struct pub; /* public fields */
+
+ char *iobuffer; /* physical I/O buffer */
+ JDIMENSION buffer_width; /* width of one row */
+} tga_dest_struct;
+
+typedef tga_dest_struct * tga_dest_ptr;
+
+
+LOCAL(void)
+write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
+/* Create and write a Targa header */
+{
+ char targaheader[18];
+
+ /* Set unused fields of header to 0 */
+ MEMZERO(targaheader, SIZEOF(targaheader));
+
+ if (num_colors > 0) {
+ targaheader[1] = 1; /* color map type 1 */
+ targaheader[5] = (char) (num_colors & 0xFF);
+ targaheader[6] = (char) (num_colors >> 8);
+ targaheader[7] = 24; /* 24 bits per cmap entry */
+ }
+
+ targaheader[12] = (char) (cinfo->output_width & 0xFF);
+ targaheader[13] = (char) (cinfo->output_width >> 8);
+ targaheader[14] = (char) (cinfo->output_height & 0xFF);
+ targaheader[15] = (char) (cinfo->output_height >> 8);
+ targaheader[17] = 0x20; /* Top-down, non-interlaced */
+
+ if (cinfo->out_color_space == JCS_GRAYSCALE) {
+ targaheader[2] = 3; /* image type = uncompressed gray-scale */
+ targaheader[16] = 8; /* bits per pixel */
+ } else { /* must be RGB */
+ if (num_colors > 0) {
+ targaheader[2] = 1; /* image type = colormapped RGB */
+ targaheader[16] = 8;
+ } else {
+ targaheader[2] = 2; /* image type = uncompressed RGB */
+ targaheader[16] = 24;
+ }
+ }
+
+ if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF(void)
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+/* used for unquantized full-color output */
+{
+ tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+ register JSAMPROW inptr;
+ register char * outptr;
+ register JDIMENSION col;
+
+ inptr = dest->pub.buffer[0];
+ outptr = dest->iobuffer;
+ for (col = cinfo->output_width; col > 0; col--) {
+ outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
+ outptr[1] = (char) GETJSAMPLE(inptr[1]);
+ outptr[2] = (char) GETJSAMPLE(inptr[0]);
+ inptr += 3, outptr += 3;
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+METHODDEF(void)
+put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+/* used for grayscale OR quantized color output */
+{
+ tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+ register JSAMPROW inptr;
+ register char * outptr;
+ register JDIMENSION col;
+
+ inptr = dest->pub.buffer[0];
+ outptr = dest->iobuffer;
+ for (col = cinfo->output_width; col > 0; col--) {
+ *outptr++ = (char) GETJSAMPLE(*inptr++);
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Write some demapped pixel data when color quantization is in effect.
+ * For Targa, this is only applied to grayscale data.
+ */
+
+METHODDEF(void)
+put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied)
+{
+ tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+ register JSAMPROW inptr;
+ register char * outptr;
+ register JSAMPROW color_map0 = cinfo->colormap[0];
+ register JDIMENSION col;
+
+ inptr = dest->pub.buffer[0];
+ outptr = dest->iobuffer;
+ for (col = cinfo->output_width; col > 0; col--) {
+ *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
+ }
+ (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF(void)
+start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+ int num_colors, i;
+ FILE *outfile;
+
+ if (cinfo->out_color_space == JCS_GRAYSCALE) {
+ /* Targa doesn't have a mapped grayscale format, so we will */
+ /* demap quantized gray output. Never emit a colormap. */
+ write_header(cinfo, dinfo, 0);
+ if (cinfo->quantize_colors)
+ dest->pub.put_pixel_rows = put_demapped_gray;
+ else
+ dest->pub.put_pixel_rows = put_gray_rows;
+ } else if (cinfo->out_color_space == JCS_RGB) {
+ if (cinfo->quantize_colors) {
+ /* We only support 8-bit colormap indexes, so only 256 colors */
+ num_colors = cinfo->actual_number_of_colors;
+ if (num_colors > 256)
+ ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
+ write_header(cinfo, dinfo, num_colors);
+ /* Write the colormap. Note Targa uses BGR byte order */
+ outfile = dest->pub.output_file;
+ for (i = 0; i < num_colors; i++) {
+ putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
+ putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
+ putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
+ }
+ dest->pub.put_pixel_rows = put_gray_rows;
+ } else {
+ write_header(cinfo, dinfo, 0);
+ dest->pub.put_pixel_rows = put_pixel_rows;
+ }
+ } else {
+ ERREXIT(cinfo, JERR_TGA_COLORSPACE);
+ }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF(void)
+finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+ /* Make sure we wrote the output file OK */
+ fflush(dinfo->output_file);
+ if (ferror(dinfo->output_file))
+ ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for Targa format output.
+ */
+
+GLOBAL(djpeg_dest_ptr)
+jinit_write_targa (j_decompress_ptr cinfo)
+{
+ tga_dest_ptr dest;
+
+ /* Create module interface object, fill in method pointers */
+ dest = (tga_dest_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(tga_dest_struct));
+ dest->pub.start_output = start_output_tga;
+ dest->pub.finish_output = finish_output_tga;
+
+ /* Calculate output image dimensions so we can allocate space */
+ jpeg_calc_output_dimensions(cinfo);
+
+ /* Create I/O buffer. Note we make this near on a PC. */
+ dest->buffer_width = cinfo->output_width * cinfo->output_components;
+ dest->iobuffer = (char *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (size_t) (dest->buffer_width * SIZEOF(char)));
+
+ /* Create decompressor output buffer. */
+ dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
+ dest->pub.buffer_height = 1;
+
+ return (djpeg_dest_ptr) dest;
+}
+
+#endif /* TARGA_SUPPORTED */
diff --git a/test/monniaux/jumptable/jumptable.c b/test/monniaux/jumptable/jumptable.c
new file mode 100644
index 00000000..8415cbf8
--- /dev/null
+++ b/test/monniaux/jumptable/jumptable.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+#define CASE(x) case x: return 100+x;
+
+int big_switch(int x) {
+ switch (x) {
+ CASE(0)
+ CASE(1)
+ CASE(2)
+ CASE(3)
+ CASE(4)
+ CASE(5)
+ CASE(6)
+ CASE(7)
+ CASE(8)
+ CASE(9)
+ CASE(10)
+ CASE(11)
+ CASE(12)
+ CASE(13)
+ CASE(14)
+ CASE(15)
+ }
+ return 99;
+}
+
+int main() {
+ for(int i=-1; i<16; i++) {
+ if (big_switch(i) != 100+i) {
+ printf("error at %d\n", i);
+ }
+ }
+ return 0;
+}
diff --git a/test/monniaux/k1_builtins/atomics.c b/test/monniaux/k1_builtins/atomics.c
new file mode 100644
index 00000000..481a4aee
--- /dev/null
+++ b/test/monniaux/k1_builtins/atomics.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+int main() {
+ long lval = 45;
+ long lval2 = __builtin_kvx_afaddd(&lval, 6);
+ printf("%ld %ld\n", lval, lval2);
+ int ival = 45;
+ int ival2 = __builtin_kvx_afaddw(&ival, 6);
+ printf("%d %d\n", ival, ival2);
+ return 0;
+}
diff --git a/test/monniaux/k1_builtins/execute_code.c b/test/monniaux/k1_builtins/execute_code.c
new file mode 100644
index 00000000..d3cc35d2
--- /dev/null
+++ b/test/monniaux/k1_builtins/execute_code.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef int fun_type(int x);
+
+int poulet(int x) {
+ return x*2;
+}
+
+int canard(int x) {
+ return x*3;
+}
+
+#define SIZE 1024
+int main() {
+ void *buf=malloc(SIZE);
+ memcpy(buf, poulet, SIZE);
+ int rpoulet = (*((fun_type*) buf))(33);
+ memcpy(buf, canard, SIZE);
+ int rcanard = (*((fun_type*) buf))(33);
+ __builtin_kvx_iinval();
+ int rcanard2 = (*((fun_type*) buf))(33);
+ free(buf);
+ printf("%d %d %d\n", rpoulet, rcanard, rcanard2);
+ return 0;
+}
diff --git a/test/monniaux/k1_builtins/sbmm8.c b/test/monniaux/k1_builtins/sbmm8.c
new file mode 100644
index 00000000..dba7a72b
--- /dev/null
+++ b/test/monniaux/k1_builtins/sbmm8.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main() {
+ {
+ unsigned long a = 0x12345678ABCDEFUL, b=0x12345118ABCD32UL, c;
+ c = __builtin_kvx_sbmm8(a, b);
+ printf("%lx\n", c);
+ }
+ {
+ unsigned long a = 0x0102040810204080UL, b=0x12345118ABCD32UL, c;
+ c = __builtin_kvx_sbmm8(a, b);
+ printf("%lx\n", c);
+ }
+ return 0;
+}
diff --git a/test/monniaux/k1_builtins/sbmm8_demo.sage b/test/monniaux/k1_builtins/sbmm8_demo.sage
new file mode 100755
index 00000000..4a4c9df0
--- /dev/null
+++ b/test/monniaux/k1_builtins/sbmm8_demo.sage
@@ -0,0 +1,21 @@
+#!/usr/bin/env sage
+def mat_from_uint64(n):
+ return Matrix(GF(2), [[(n >> (i*8+j)) & 1 for j in range(8)] for i in range(8)])
+
+def uint64_from_mat(m):
+ s = 0
+ for i in range(8):
+ for j in range(8):
+ if m[i, j]:
+ s += 1 << (i*8 + j)
+ return s
+
+matA=mat_from_uint64(0x12345678ABCDEF)
+matB=mat_from_uint64(0x12345118ABCD32)
+
+print hex(uint64_from_mat(matB * matA))
+
+matA=mat_from_uint64(0x0201040810208040)
+matB=mat_from_uint64(0x0102011701010133)
+
+print matA*matB
diff --git a/test/monniaux/k1_builtins/test_k1_builtins.c b/test/monniaux/k1_builtins/test_k1_builtins.c
new file mode 100644
index 00000000..29903bb6
--- /dev/null
+++ b/test/monniaux/k1_builtins/test_k1_builtins.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <mppa_bare_runtime/kvx/registers.h>
+
+void test_system_regs(void) {
+ __builtin_kvx_wfxl(K1_SFR_EV4, 0x1000ULL);
+ __builtin_kvx_wfxm(K1_SFR_EV4, 0x2000ULL);
+ __builtin_kvx_get(K1_SFR_EV4);
+ __builtin_kvx_set(K1_SFR_EV4, 0x4000ULL);
+}
+
+void test_loads(void *addr) {
+ __builtin_kvx_alclrd(addr);
+ __builtin_kvx_alclrw(addr);
+ __builtin_kvx_lbzu(addr);
+ __builtin_kvx_lhzu(addr);
+ __builtin_kvx_lwzu(addr);
+ __builtin_kvx_ldu(addr);
+ __builtin_kvx_dinvall(addr);
+ __builtin_kvx_dtouchl(addr);
+ __builtin_kvx_dzerol(addr);
+ __builtin_kvx_iinvals(addr);
+ /* __builtin_kvx_itouchl(addr); */
+ __builtin_kvx_dzerol(addr);
+}
+
+void test_stops(void) {
+ __builtin_kvx_await();
+ __builtin_kvx_sleep();
+ __builtin_kvx_stop();
+ __builtin_kvx_barrier();
+ __builtin_kvx_fence();
+ __builtin_kvx_dinval();
+ __builtin_kvx_iinval();
+}
+
+int main() {
+ unsigned long long data = 45;
+ unsigned long long res = __builtin_kvx_alclrd(&data);
+ printf("%llu %llu\n", res, data);
+}
diff --git a/test/monniaux/kill_pragma.h b/test/monniaux/kill_pragma.h
new file mode 100644
index 00000000..a2fde5a9
--- /dev/null
+++ b/test/monniaux/kill_pragma.h
@@ -0,0 +1 @@
+#define _Pragma(x)
diff --git a/test/monniaux/latency/show_latency.c b/test/monniaux/latency/show_latency.c
new file mode 100644
index 00000000..3269450a
--- /dev/null
+++ b/test/monniaux/latency/show_latency.c
@@ -0,0 +1,11 @@
+#include "../clock.h"
+
+extern void latency(int *p);
+
+int main() {
+ int x;
+ clock_start();
+ latency(&x);
+ clock_stop();
+ print_total_clock();
+}
diff --git a/test/monniaux/licm/addv.c b/test/monniaux/licm/addv.c
new file mode 100644
index 00000000..bb0098d0
--- /dev/null
+++ b/test/monniaux/licm/addv.c
@@ -0,0 +1,6 @@
+void addv(double x, double y, int n, int *z)
+{
+ for(int i=0; i<n; i++) {
+ z[i] += (int) (x*y);
+ }
+}
diff --git a/test/monniaux/longjmp/example.c b/test/monniaux/longjmp/example.c
new file mode 100644
index 00000000..d3debfaf
--- /dev/null
+++ b/test/monniaux/longjmp/example.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <setjmp.h>
+
+jmp_buf bufferA, bufferB;
+
+void routineB(); // forward declaration
+
+void routineA()
+{
+ int r ;
+
+ printf("(A1)\n");
+
+ r = setjmp(bufferA);
+ if (r == 0) routineB();
+
+ printf("(A2) r=%d\n",r);
+
+ r = setjmp(bufferA);
+ if (r == 0) longjmp(bufferB, 20001);
+
+ printf("(A3) r=%d\n",r);
+
+ r = setjmp(bufferA);
+ if (r == 0) longjmp(bufferB, 20002);
+
+ printf("(A4) r=%d\n",r);
+}
+
+void routineB()
+{
+ int r;
+
+ printf("(B1)\n");
+
+ r = setjmp(bufferB);
+ if (r == 0) longjmp(bufferA, 10001);
+
+ printf("(B2) r=%d\n", r);
+
+ r = setjmp(bufferB);
+ if (r == 0) longjmp(bufferA, 10002);
+
+ printf("(B3) r=%d\n", r);
+
+ r = setjmp(bufferB);
+ if (r == 0) longjmp(bufferA, 10003);
+}
+
+
+int main(int argc, char **argv)
+{
+ routineA();
+ return 0;
+}
diff --git a/test/monniaux/loop/fill_buffer.c b/test/monniaux/loop/fill_buffer.c
new file mode 100644
index 00000000..9d89a2d1
--- /dev/null
+++ b/test/monniaux/loop/fill_buffer.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "../clock.h"
+
+typedef unsigned char data;
+
+/* 8 cycles / iteration */
+void fill_buffer(data x, unsigned n, data *buf) {
+ for(unsigned i=0; i<n; i++) buf[i] = x;
+}
+
+/* 7 cycles / iteration */
+void fill_buffer2(data x, unsigned n, data *buf) {
+ unsigned i=0;
+ if (i<n) {
+ do {
+ buf[i] = x;
+ i++;
+ } while (i<n);
+ }
+}
+
+void fill_buffer10(data x, data *buf) {
+ for(unsigned i=0; i<10; i++) buf[i] = x;
+}
+
+int main() {
+ const size_t n = 10000;
+ data *buf = malloc(n * sizeof(data));
+ clock_prepare();
+ clock_start();
+ fill_buffer(42, n, buf);
+ clock_stop();
+ free(buf);
+ print_total_clock();
+ return 0;
+}
diff --git a/test/monniaux/loop_nest/polybench.h b/test/monniaux/loop_nest/polybench.h
new file mode 100644
index 00000000..7d092e45
--- /dev/null
+++ b/test/monniaux/loop_nest/polybench.h
@@ -0,0 +1,202 @@
+/**
+ * polybench.h: This file is part of the PolyBench/C 3.2 test suite.
+ *
+ *
+ * Contact: Louis-Noel Pouchet <pouchet@cse.ohio-state.edu>
+ * Web address: http://polybench.sourceforge.net
+ */
+/*
+ * Polybench header for instrumentation.
+ *
+ * Programs must be compiled with `-I utilities utilities/polybench.c'
+ *
+ * Optionally, one can define:
+ *
+ * -DPOLYBENCH_TIME, to report the execution time,
+ * OR (exclusive):
+ * -DPOLYBENCH_PAPI, to use PAPI H/W counters (defined in polybench.c)
+ *
+ *
+ * See README or utilities/polybench.c for additional options.
+ *
+ */
+#ifndef POLYBENCH_H
+# define POLYBENCH_H
+
+# include <stdlib.h>
+
+/* Array padding. By default, none is used. */
+# ifndef POLYBENCH_PADDING_FACTOR
+/* default: */
+# define POLYBENCH_PADDING_FACTOR 0
+# endif
+
+
+/* C99 arrays in function prototype. By default, do not use. */
+# ifdef POLYBENCH_USE_C99_PROTO
+# define POLYBENCH_C99_SELECT(x,y) y
+# else
+/* default: */
+# define POLYBENCH_C99_SELECT(x,y) x
+# endif
+
+
+/* Scalar loop bounds in SCoPs. By default, use parametric loop bounds. */
+# ifdef POLYBENCH_USE_SCALAR_LB
+# define POLYBENCH_LOOP_BOUND(x,y) x
+# else
+/* default: */
+# define POLYBENCH_LOOP_BOUND(x,y) y
+# endif
+
+
+/* Macros to reference an array. Generic for heap and stack arrays
+ (C99). Each array dimensionality has his own macro, to be used at
+ declaration or as a function argument.
+ Example:
+ int b[x] => POLYBENCH_1D_ARRAY(b, x)
+ int A[N][N] => POLYBENCH_2D_ARRAY(A, N, N)
+*/
+# ifndef POLYBENCH_STACK_ARRAYS
+# define POLYBENCH_ARRAY(x) *x
+# define POLYBENCH_FREE_ARRAY(x) free((void*)x);
+# define POLYBENCH_DECL_VAR(x) (*x)
+# else
+# define POLYBENCH_ARRAY(x) x
+# define POLYBENCH_FREE_ARRAY(x)
+# define POLYBENCH_DECL_VAR(x) x
+# endif
+/* Macros for using arrays in the function prototypes. */
+# define POLYBENCH_1D(var, dim1,ddim1) var[POLYBENCH_C99_SELECT(dim1,ddim1) + POLYBENCH_PADDING_FACTOR]
+# define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2) var[POLYBENCH_C99_SELECT(dim1,ddim1) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim2,ddim2) + POLYBENCH_PADDING_FACTOR]
+# define POLYBENCH_3D(var, dim1, dim2, dim3, ddim1, ddim2, ddim3) var[POLYBENCH_C99_SELECT(dim1,ddim1) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim2,ddim2) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim3,ddim3) + POLYBENCH_PADDING_FACTOR]
+# define POLYBENCH_4D(var, dim1, dim2, dim3, dim4, ddim1, ddim2, ddim3, ddim4) var[POLYBENCH_C99_SELECT(dim1,ddim1) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim2,ddim2) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim3,ddim3) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim4,ddim4) + POLYBENCH_PADDING_FACTOR]
+# define POLYBENCH_5D(var, dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, ddim4, ddim5) var[POLYBENCH_C99_SELECT(dim1,ddim1) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim2,ddim2) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim3,ddim3) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim4,ddim4) + POLYBENCH_PADDING_FACTOR][POLYBENCH_C99_SELECT(dim5,ddim5) + POLYBENCH_PADDING_FACTOR]
+
+
+/* Macros to allocate heap arrays.
+ Example:
+ polybench_alloc_2d_array(N, M, double) => allocates N x M x sizeof(double)
+ and returns a pointer to the 2d array
+ */
+# define POLYBENCH_ALLOC_1D_ARRAY(n1, type) \
+ (type(*)[n1 + POLYBENCH_PADDING_FACTOR])polybench_alloc_data (n1 + POLYBENCH_PADDING_FACTOR, sizeof(type))
+# define POLYBENCH_ALLOC_2D_ARRAY(n1, n2, type) \
+ (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR])polybench_alloc_data ((n1 + POLYBENCH_PADDING_FACTOR) * (n2 + POLYBENCH_PADDING_FACTOR), sizeof(type))
+# define POLYBENCH_ALLOC_3D_ARRAY(n1, n2, n3, type) \
+ (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR][n3 + POLYBENCH_PADDING_FACTOR])polybench_alloc_data ((n1 + POLYBENCH_PADDING_FACTOR) * (n2 + POLYBENCH_PADDING_FACTOR) * (n3 + POLYBENCH_PADDING_FACTOR), sizeof(type))
+# define POLYBENCH_ALLOC_4D_ARRAY(n1, n2, n3, n4, type) \
+ (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR][n3 + POLYBENCH_PADDING_FACTOR][n4 + POLYBENCH_PADDING_FACTOR])polybench_alloc_data ((n1 + POLYBENCH_PADDING_FACTOR) * (n2 + POLYBENCH_PADDING_FACTOR) * (n3 + POLYBENCH_PADDING_FACTOR) * (n4 + POLYBENCH_PADDING_FACTOR), sizeof(type))
+# define POLYBENCH_ALLOC_5D_ARRAY(n1, n2, n3, n4, n5, type) \
+ (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR][n3 + POLYBENCH_PADDING_FACTOR][n4 + POLYBENCH_PADDING_FACTOR][n5 + POLYBENCH_PADDING_FACTOR])polybench_alloc_data ((n1 + POLYBENCH_PADDING_FACTOR) * (n2 + POLYBENCH_PADDING_FACTOR) * (n3 + POLYBENCH_PADDING_FACTOR) * (n4 + POLYBENCH_PADDING_FACTOR) * (n5 + POLYBENCH_PADDING_FACTOR), sizeof(type))
+
+/* Macros for array declaration. */
+# ifndef POLYBENCH_STACK_ARRAYS
+# define POLYBENCH_1D_ARRAY_DECL(var, type, dim1, ddim1) \
+ type POLYBENCH_1D(POLYBENCH_DECL_VAR(var), dim1, ddim1); \
+ var = POLYBENCH_ALLOC_1D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), type);
+# define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2) \
+ type POLYBENCH_2D(POLYBENCH_DECL_VAR(var), dim1, dim2, ddim1, ddim2); \
+ var = POLYBENCH_ALLOC_2D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), POLYBENCH_C99_SELECT(dim2, ddim2), type);
+# define POLYBENCH_3D_ARRAY_DECL(var, type, dim1, dim2, dim3, ddim1, ddim2, ddim3) \
+ type POLYBENCH_3D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, ddim1, ddim2, ddim3); \
+ var = POLYBENCH_ALLOC_3D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), POLYBENCH_C99_SELECT(dim2, ddim2), POLYBENCH_C99_SELECT(dim3, ddim3), type);
+# define POLYBENCH_4D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, ddim1, ddim2, ddim3, ddim4) \
+ type POLYBENCH_4D(POLYBENCH_DECL_VAR(var), dim1, dim2, ,dim3, dim4, ddim1, ddim2, ddim3, ddim4); \
+ var = POLYBENCH_ALLOC_4D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), POLYBENCH_C99_SELECT(dim2, ddim2), POLYBENCH_C99_SELECT(dim3, ddim3), POLYBENCH_C99_SELECT(dim4, ddim4), type);
+# define POLYBENCH_5D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, ddim4, ddim5) \
+ type POLYBENCH_5D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, ddim4, ddim5); \
+ var = POLYBENCH_ALLOC_5D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), POLYBENCH_C99_SELECT(dim2, ddim2), POLYBENCH_C99_SELECT(dim3, ddim3), POLYBENCH_C99_SELECT(dim4, ddim4), POLYBENCH_C99_SELECT(dim5, ddim5), type);
+# else
+# define POLYBENCH_1D_ARRAY_DECL(var, type, dim1, ddim1) \
+ type POLYBENCH_1D(POLYBENCH_DECL_VAR(var), dim1, ddim1);
+# define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2) \
+ type POLYBENCH_2D(POLYBENCH_DECL_VAR(var), dim1, dim2, ddim1, ddim2);
+# define POLYBENCH_3D_ARRAY_DECL(var, type, dim1, dim2, dim3, ddim1, ddim2, ddim3) \
+ type POLYBENCH_3D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, ddim1, ddim2, ddim3);
+# define POLYBENCH_4D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, ddim1, ddim2, ddim3, ddim4) \
+ type POLYBENCH_4D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, dim4, ddim1, ddim2, ddim3, ddim4);
+# define POLYBENCH_5D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, ddim4, ddim5) \
+ type POLYBENCH_5D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, ddim4, ddim5);
+# endif
+
+
+/* Dead-code elimination macros. Use argc/argv for the run-time check. */
+# ifndef POLYBENCH_DUMP_ARRAYS
+# define POLYBENCH_DCE_ONLY_CODE if (argc > 42 && ! strcmp(argv[0], ""))
+# else
+# define POLYBENCH_DCE_ONLY_CODE
+# endif
+
+# define polybench_prevent_dce(func) \
+ POLYBENCH_DCE_ONLY_CODE \
+ func
+
+
+/* Performance-related instrumentation. See polybench.c */
+# define polybench_start_instruments
+# define polybench_stop_instruments
+# define polybench_print_instruments
+
+
+/* PAPI support. */
+# ifdef POLYBENCH_PAPI
+extern const unsigned int polybench_papi_eventlist[];
+# undef polybench_start_instruments
+# undef polybench_stop_instruments
+# undef polybench_print_instruments
+# define polybench_set_papi_thread_report(x) \
+ polybench_papi_counters_threadid = x;
+# define polybench_start_instruments \
+ polybench_prepare_instruments(); \
+ polybench_papi_init(); \
+ int evid; \
+ for (evid = 0; polybench_papi_eventlist[evid] != 0; evid++) \
+ { \
+ if (polybench_papi_start_counter(evid)) \
+ continue; \
+
+# define polybench_stop_instruments \
+ polybench_papi_stop_counter(evid); \
+ } \
+ polybench_papi_close(); \
+
+# define polybench_print_instruments polybench_papi_print();
+# endif
+
+
+/* Timing support. */
+# if defined(POLYBENCH_TIME) || defined(POLYBENCH_GFLOPS)
+# undef polybench_start_instruments
+# undef polybench_stop_instruments
+# undef polybench_print_instruments
+# define polybench_start_instruments polybench_timer_start();
+# define polybench_stop_instruments polybench_timer_stop();
+# define polybench_print_instruments polybench_timer_print();
+extern double polybench_program_total_flops;
+extern void polybench_timer_start();
+extern void polybench_timer_stop();
+extern void polybench_timer_print();
+# endif
+
+/* Function declaration. */
+# ifdef POLYBENCH_TIME
+extern void polybench_timer_start();
+extern void polybench_timer_stop();
+extern void polybench_timer_print();
+# endif
+
+# ifdef POLYBENCH_PAPI
+extern void polybench_prepare_instruments();
+extern int polybench_papi_start_counter(int evid);
+extern void polybench_papi_stop_counter(int evid);
+extern void polybench_papi_init();
+extern void polybench_papi_close();
+extern void polybench_papi_print();
+# endif
+
+/* Function prototypes. */
+extern void* polybench_alloc_data(unsigned long long int n, int elt_size);
+
+
+#endif /* !POLYBENCH_H */
diff --git a/test/monniaux/loop_nest/syrk.c b/test/monniaux/loop_nest/syrk.c
new file mode 100644
index 00000000..490d0a01
--- /dev/null
+++ b/test/monniaux/loop_nest/syrk.c
@@ -0,0 +1,28 @@
+/* Include polybench common header. */
+#include "polybench.h"
+
+/* Include benchmark-specific header. */
+/* Default data type is double, default size is 4000. */
+#include "syrk.h"
+
+/* Main computational kernel. The whole function will be timed,
+ including the call and return. */
+void kernel_syrk(int ni, int nj,
+ DATA_TYPE alpha,
+ DATA_TYPE beta,
+ DATA_TYPE POLYBENCH_2D(C,NI,NI,ni,ni),
+ DATA_TYPE POLYBENCH_2D(A,NI,NJ,ni,nj))
+{
+ int i, j, k;
+
+ /* C := alpha*A*A' + beta*C */
+#if 0
+ for (i = 0; i < _PB_NI; i++)
+ for (j = 0; j < _PB_NI; j++)
+ C[i][j] *= beta;
+#endif
+ for (i = 0; i < _PB_NI; i++)
+ for (j = 0; j < _PB_NI; j++)
+ for (k = 0; k < _PB_NJ; k++)
+ C[i][j] += alpha * A[i][k] * A[j][k];
+}
diff --git a/test/monniaux/loop_nest/syrk.h b/test/monniaux/loop_nest/syrk.h
new file mode 100644
index 00000000..c753ff3b
--- /dev/null
+++ b/test/monniaux/loop_nest/syrk.h
@@ -0,0 +1,54 @@
+/**
+ * syrk.h: This file is part of the PolyBench/C 3.2 test suite.
+ *
+ *
+ * Contact: Louis-Noel Pouchet <pouchet@cse.ohio-state.edu>
+ * Web address: http://polybench.sourceforge.net
+ */
+#ifndef SYRK_H
+# define SYRK_H
+
+/* Default to STANDARD_DATASET. */
+# if !defined(MINI_DATASET) && !defined(SMALL_DATASET) && !defined(LARGE_DATASET) && !defined(EXTRALARGE_DATASET)
+# define STANDARD_DATASET
+# endif
+
+/* Do not define anything if the user manually defines the size. */
+# if !defined(NI) && !defined(NJ)
+/* Define the possible dataset sizes. */
+# ifdef MINI_DATASET
+# define NI 32
+# define NJ 32
+# endif
+
+# ifdef SMALL_DATASET
+# define NI 128
+# define NJ 128
+# endif
+
+# ifdef STANDARD_DATASET /* Default if unspecified. */
+# define NI 1024
+# define NJ 1024
+# endif
+
+# ifdef LARGE_DATASET
+# define NI 2000
+# define NJ 2000
+# endif
+
+# ifdef EXTRALARGE_DATASET
+# define NI 4000
+# define NJ 4000
+# endif
+# endif /* !N */
+
+# define _PB_NI POLYBENCH_LOOP_BOUND(NI,ni)
+# define _PB_NJ POLYBENCH_LOOP_BOUND(NJ,nj)
+
+# ifndef DATA_TYPE
+# define DATA_TYPE double
+# define DATA_PRINTF_MODIFIER "%0.2lf "
+# endif
+
+
+#endif /* !SYRK */
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/Makefile b/test/monniaux/lustrev4_lustrec_heater_control/Makefile
new file mode 100644
index 00000000..df7c9e0a
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/Makefile
@@ -0,0 +1,3 @@
+TARGET=lustrec_heater_control
+
+include ../rules.mk
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/arrow.h b/test/monniaux/lustrev4_lustrec_heater_control/arrow.h
new file mode 100644
index 00000000..802057da
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/arrow.h
@@ -0,0 +1,34 @@
+
+#ifndef _ARROW
+#define _ARROW
+
+struct _arrow_mem {struct _arrow_reg {_Bool _first; } _reg; };
+
+extern struct _arrow_mem *_arrow_alloc ();
+
+extern void _arrow_dealloc (struct _arrow_mem *);
+
+#define _arrow_DECLARE(attr, inst)\
+ attr struct _arrow_mem inst;
+
+#define _arrow_LINK(inst) do {\
+ ;\
+} while (0)
+
+#define _arrow_ALLOC(attr, inst)\
+ _arrow_DECLARE(attr, inst);\
+ _arrow_LINK(inst)
+
+#define _arrow_init(self) {}
+
+#define _arrow_clear(self) {}
+
+#define _arrow_step(x,y,output,self) ((self)->_reg._first?((self)->_reg._first=0,(*output = x)):(*output = y))
+
+#define _arrow_reset(self) {(self)->_reg._first = 1;}
+
+/* Step macro for specialized arrows of the form: (true -> false) */
+
+#define _once_step(output,self) { *output = (self)->_reg._first; if ((self)->_reg._first) { (self)->_reg._first=0; }; }
+
+#endif
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/heater_control.c b/test/monniaux/lustrev4_lustrec_heater_control/heater_control.c
new file mode 100644
index 00000000..6025cd13
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/heater_control.c
@@ -0,0 +1,342 @@
+#include <assert.h>
+#include "heater_control.h"
+
+/* C code generated by lustrec
+ Version number 1.6-@GITBRANCH@
+ Code is C99 compliant
+ Using (double) floating-point numbers */
+
+/* Import dependencies */
+
+/* Global constants (definitions) */
+double FAILURE = -999.0;
+double TMIN = 6.0;
+double TMAX = 9.0;
+double DELTA = 0.5;
+
+/* Struct definitions */
+struct not_a_sauna2_mem {struct not_a_sauna2_reg {double __not_a_sauna2_2;
+ } _reg;
+ struct _arrow_mem *ni_0;
+ };
+struct heater_control_mem {struct heater_control_reg {_Bool __heater_control_16;
+ } _reg;
+ struct _arrow_mem *ni_1;
+ };
+struct not_a_sauna_mem {struct not_a_sauna_reg {double __not_a_sauna_2;
+ } _reg;
+ struct _arrow_mem *ni_2;
+ };
+
+
+void not_a_sauna2_reset (struct not_a_sauna2_mem *self) {
+
+ _arrow_reset(self->ni_0);
+ return;
+}
+
+void not_a_sauna2_step (double T, double T1, double T2, double T3,
+ _Bool Heat_on,
+ _Bool (*ok),
+ struct not_a_sauna2_mem *self) {
+ _Bool __not_a_sauna2_1;
+
+
+ _arrow_step (1, 0, &__not_a_sauna2_1, self->ni_0);
+ if (__not_a_sauna2_1) {
+ *ok = 1;
+ } else {
+ *ok = (self->_reg.__not_a_sauna2_2 < (9.0 - 6.0));
+ }
+ self->_reg.__not_a_sauna2_2 = T;
+
+ return;
+}
+
+void heater_control_reset (struct heater_control_mem *self) {
+
+ _arrow_reset(self->ni_1);
+ return;
+}
+
+void heater_control_step (double T, double T1, double T2, double T3,
+ _Bool (*Heat_on),
+ struct heater_control_mem *self) {
+ double Median_42_max2_150_m;
+ double Median_42_max2_152_m;
+ double Median_42_min2_141_m;
+ double Median_42_min2_143_m;
+ double Median_52_max2_150_m;
+ double Median_52_max2_152_m;
+ double Median_52_min2_141_m;
+ double Median_52_min2_143_m;
+ double Tguess;
+ _Bool V12;
+ _Bool V13;
+ _Bool V23;
+ _Bool __heater_control_12;
+ double abs_15_a;
+ double abs_15_v;
+ double abs_23_a;
+ double abs_23_v;
+ double abs_7_a;
+ double abs_7_v;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ abs_7_v = (T1 - T2);
+ if ((abs_7_v >= 0.0)) {
+ abs_7_a = abs_7_v;
+ } else {
+ abs_7_a = (- abs_7_v);
+ }
+ abs_23_v = (T2 - T3);
+ if ((abs_23_v >= 0.0)) {
+ abs_23_a = abs_23_v;
+ } else {
+ abs_23_a = (- abs_23_v);
+ }
+ abs_15_v = (T1 - T3);
+ if ((abs_15_v >= 0.0)) {
+ abs_15_a = abs_15_v;
+ } else {
+ abs_15_a = (- abs_15_v);
+ }
+ V23 = (abs_23_a < 0.5);
+ V13 = (abs_15_a < 0.5);
+ V12 = (abs_7_a < 0.5);
+ if ((T2 < T3)) {
+ Median_52_min2_141_m = T2;
+ } else {
+ Median_52_min2_141_m = T3;
+ }
+ if ((T2 > T3)) {
+ Median_42_max2_150_m = T2;
+ } else {
+ Median_42_max2_150_m = T3;
+ }
+ if ((T2 < T3)) {
+ Median_42_min2_141_m = T2;
+ } else {
+ Median_42_min2_141_m = T3;
+ }
+ if ((T2 > T3)) {
+ Median_52_max2_150_m = T2;
+ } else {
+ Median_52_max2_150_m = T3;
+ }
+ if ((T1 < Median_52_min2_141_m)) {
+ Median_52_min2_143_m = T1;
+ } else {
+ Median_52_min2_143_m = Median_52_min2_141_m;
+ }
+ if ((T1 > Median_52_max2_150_m)) {
+ Median_52_max2_152_m = T1;
+ } else {
+ Median_52_max2_152_m = Median_52_max2_150_m;
+ }
+ if ((T1 < Median_42_min2_141_m)) {
+ Median_42_min2_143_m = T1;
+ } else {
+ Median_42_min2_143_m = Median_42_min2_141_m;
+ }
+ if ((T1 > Median_42_max2_150_m)) {
+ Median_42_max2_152_m = T1;
+ } else {
+ Median_42_max2_152_m = Median_42_max2_150_m;
+ }
+ if ((((!V12) && (!V13)) && (!V23))) {
+ Tguess = -999.0;
+ } else {
+ if (((((V12 && (!V13)) && (!V23)) || ((V13 && (!V12)) && (!V23))) || ((V23 && (!V12)) && (!V13)))) {
+ Tguess = ((((T1 + T2) + T3) - Median_42_min2_143_m) - Median_42_max2_152_m);
+ } else {
+ if (((V12 && V13) && V23)) {
+ Tguess = ((((T1 + T2) + T3) - Median_52_min2_143_m) - Median_52_max2_152_m);
+ } else {
+ if (V12) {
+ Tguess = ((T1 + T2) / 2.0);
+ } else {
+ if (V13) {
+ Tguess = ((T1 + T3) / 2.0);
+ } else {
+ Tguess = ((T2 + T3) / 2.0);
+ }
+ }
+ }
+ }
+ }
+ _arrow_step (1, 0, &__heater_control_12, self->ni_1);
+ if (__heater_control_12) {
+ *Heat_on = 1;
+ } else {
+ if ((Tguess == -999.0)) {
+ *Heat_on = 0;
+ } else {
+ if ((Tguess < 6.0)) {
+ *Heat_on = 1;
+ } else {
+ if ((Tguess > 9.0)) {
+ *Heat_on = 0;
+ } else {
+ *Heat_on = self->_reg.__heater_control_16;
+ }
+ }
+ }
+ }
+ self->_reg.__heater_control_16 = *Heat_on;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ return;
+}
+
+void not_a_sauna_reset (struct not_a_sauna_mem *self) {
+
+ _arrow_reset(self->ni_2);
+ return;
+}
+
+void not_a_sauna_step (double T, double T1, double T2, double T3,
+ _Bool Heat_on,
+ _Bool (*ok),
+ struct not_a_sauna_mem *self) {
+ _Bool __not_a_sauna_1;
+
+
+ _arrow_step (1, 0, &__not_a_sauna_1, self->ni_2);
+ if (__not_a_sauna_1) {
+ *ok = 1;
+ } else {
+ *ok = (self->_reg.__not_a_sauna_2 < (9.0 + 1.0));
+ }
+ self->_reg.__not_a_sauna_2 = T;
+
+ return;
+}
+
+void oneoftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ ) {
+
+ *r = ((((f1 && (!f2)) && (!f3)) || ((f2 && (!f1)) && (!f3))) || ((f3 && (!f1)) && (!f2)));
+ return;
+}
+
+void noneoftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ ) {
+
+ *r = (((!f1) && (!f2)) && (!f3));
+ return;
+}
+
+void alloftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ ) {
+
+ *r = ((f1 && f2) && f3);
+ return;
+}
+
+void abs_step (double v,
+ double (*a)
+ ) {
+
+ if ((v >= 0.0)) {
+ *a = v;
+ } else {
+ *a = (- v);
+ }
+ return;
+}
+
+void Median_step (double a, double b, double c,
+ double (*z)
+ ) {
+ double __Median_1;
+ double __Median_2;
+ double __Median_3;
+ double __Median_4;
+
+
+
+
+
+ min2_step (b, c, &__Median_3);
+ min2_step (a, __Median_3, &__Median_4);
+ max2_step (b, c, &__Median_1);
+ max2_step (a, __Median_1, &__Median_2);
+ *z = ((((a + b) + c) - __Median_4) - __Median_2);
+
+
+
+
+ return;
+}
+
+void Average_step (double a, double b,
+ double (*z)
+ ) {
+
+ *z = ((a + b) / 2.0);
+ return;
+}
+
+void min2_step (double one, double two,
+ double (*m)
+ ) {
+
+ if ((one < two)) {
+ *m = one;
+ } else {
+ *m = two;
+ }
+ return;
+}
+
+void max2_step (double one, double two,
+ double (*m)
+ ) {
+
+ if ((one > two)) {
+ *m = one;
+ } else {
+ *m = two;
+ }
+ return;
+}
+
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/heater_control.h b/test/monniaux/lustrev4_lustrec_heater_control/heater_control.h
new file mode 100644
index 00000000..405f9a74
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/heater_control.h
@@ -0,0 +1,96 @@
+/* C code generated by lustrec
+ Version number 1.6-@GITBRANCH@
+ Code is C99 compliant
+ Using (double) floating-point numbers */
+
+#ifndef _HEATER_CONTROL
+#define _HEATER_CONTROL
+
+/* Imports standard library */
+#include <stdint.h>
+#include "arrow.h"
+
+
+/* Import dependencies */
+
+/* Types definitions */
+
+/* Global constant (declarations, definitions are in C file) */
+extern double FAILURE;
+extern double TMIN;
+extern double TMAX;
+extern double DELTA;
+
+/* Structs declarations */
+struct not_a_sauna2_mem;
+struct heater_control_mem;
+struct not_a_sauna_mem;
+
+/* Nodes declarations */
+extern void not_a_sauna2_reset (struct not_a_sauna2_mem *self);
+
+extern void not_a_sauna2_init (struct not_a_sauna2_mem *self);
+
+extern void not_a_sauna2_clear (struct not_a_sauna2_mem *self);
+
+extern void not_a_sauna2_step (double T, double T1, double T2, double T3,
+ _Bool Heat_on,
+ _Bool (*ok),
+ struct not_a_sauna2_mem *self);
+
+extern void heater_control_reset (struct heater_control_mem *self);
+
+extern void heater_control_init (struct heater_control_mem *self);
+
+extern void heater_control_clear (struct heater_control_mem *self);
+
+extern void heater_control_step (double T, double T1, double T2, double T3,
+ _Bool (*Heat_on),
+ struct heater_control_mem *self);
+
+extern void not_a_sauna_reset (struct not_a_sauna_mem *self);
+
+extern void not_a_sauna_init (struct not_a_sauna_mem *self);
+
+extern void not_a_sauna_clear (struct not_a_sauna_mem *self);
+
+extern void not_a_sauna_step (double T, double T1, double T2, double T3,
+ _Bool Heat_on,
+ _Bool (*ok),
+ struct not_a_sauna_mem *self);
+
+extern void oneoftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ );
+
+extern void noneoftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ );
+
+extern void alloftree_step (_Bool f1, _Bool f2, _Bool f3,
+ _Bool (*r)
+ );
+
+extern void abs_step (double v,
+ double (*a)
+ );
+
+extern void Median_step (double a, double b, double c,
+ double (*z)
+ );
+
+extern void Average_step (double a, double b,
+ double (*z)
+ );
+
+extern void min2_step (double one, double two,
+ double (*m)
+ );
+
+extern void max2_step (double one, double two,
+ double (*m)
+ );
+
+
+#endif
+
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/heater_control_alloc.h b/test/monniaux/lustrev4_lustrec_heater_control/heater_control_alloc.h
new file mode 100644
index 00000000..270ed4a3
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/heater_control_alloc.h
@@ -0,0 +1,73 @@
+/* C code generated by lustrec
+ Version number 1.6-@GITBRANCH@
+ Code is C99 compliant
+ Using (double) floating-point numbers */
+
+#ifndef _HEATER_CONTROL_alloc
+#define _HEATER_CONTROL_alloc
+
+/* Import header from heater_control */
+#include "heater_control.h"
+
+/* Import dependencies */
+
+/* Struct definitions */
+struct not_a_sauna2_mem {struct not_a_sauna2_reg {double __not_a_sauna2_2;
+ } _reg;
+ struct _arrow_mem *ni_0;
+ };
+struct heater_control_mem {struct heater_control_reg {_Bool __heater_control_16;
+ } _reg;
+ struct _arrow_mem *ni_1;
+ };
+struct not_a_sauna_mem {struct not_a_sauna_reg {double __not_a_sauna_2;
+ } _reg;
+ struct _arrow_mem *ni_2;
+ };
+
+/* Node allocation function/macro prototypes */
+#define not_a_sauna2_DECLARE(attr, inst)\
+ attr struct not_a_sauna2_mem inst;\
+ _arrow_DECLARE(attr, inst ## _ni_0);
+
+#define not_a_sauna2_LINK(inst) do {\
+ _arrow_LINK(inst ## _ni_0);\
+ inst.ni_0 = &inst ## _ni_0;\
+} while (0)
+
+#define not_a_sauna2_ALLOC(attr, inst)\
+ not_a_sauna2_DECLARE(attr, inst);\
+ not_a_sauna2_LINK(inst);
+
+
+#define heater_control_DECLARE(attr, inst)\
+ attr struct heater_control_mem inst;\
+ _arrow_DECLARE(attr, inst ## _ni_1);
+
+#define heater_control_LINK(inst) do {\
+ _arrow_LINK(inst ## _ni_1);\
+ inst.ni_1 = &inst ## _ni_1;\
+} while (0)
+
+#define heater_control_ALLOC(attr, inst)\
+ heater_control_DECLARE(attr, inst);\
+ heater_control_LINK(inst);
+
+
+#define not_a_sauna_DECLARE(attr, inst)\
+ attr struct not_a_sauna_mem inst;\
+ _arrow_DECLARE(attr, inst ## _ni_2);
+
+#define not_a_sauna_LINK(inst) do {\
+ _arrow_LINK(inst ## _ni_2);\
+ inst.ni_2 = &inst ## _ni_2;\
+} while (0)
+
+#define not_a_sauna_ALLOC(attr, inst)\
+ not_a_sauna_DECLARE(attr, inst);\
+ not_a_sauna_LINK(inst);
+
+
+
+#endif
+
diff --git a/test/monniaux/lustrev4_lustrec_heater_control/heater_control_main.c b/test/monniaux/lustrev4_lustrec_heater_control/heater_control_main.c
new file mode 100644
index 00000000..d0298840
--- /dev/null
+++ b/test/monniaux/lustrev4_lustrec_heater_control/heater_control_main.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "heater_control_alloc.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+static double _get_double(void) {
+ return (dm_random_uint32()%70000U) * 1E-3 -20.0;
+}
+
+static void _put_bool(const char *name, _Bool b) {
+ printf("%s: %d\n", name, b);
+}
+
+/* C code generated by lustrec
+ Version number 1.6-@GITBRANCH@
+ Code is C99 compliant
+ Using (double) floating-point numbers */
+
+int main (int argc, char *argv[]) {
+
+ /* Main memory allocation */
+ heater_control_ALLOC(static,main_mem);
+
+ clock_prepare();
+ clock_start();
+
+ /* Initialize the main memory */
+ heater_control_reset(&main_mem);
+
+ /* Infinite loop */
+ for(int loop_count=0; loop_count<1000; loop_count++){
+ double T = _get_double();
+ double T1 = _get_double();
+ double T2 = _get_double();
+ double T3 = _get_double();
+ _Bool Heat_on;
+ heater_control_step (T, T1, T2, T3, &Heat_on, &main_mem);
+ // _put_bool("Heat_on", Heat_on);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 1;
+}
diff --git a/test/monniaux/lustrev4_lv4_heater_control/Makefile b/test/monniaux/lustrev4_lv4_heater_control/Makefile
new file mode 100644
index 00000000..cfd37921
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/Makefile
@@ -0,0 +1,3 @@
+TARGET=lv4_heater_control
+
+include ../rules.mk
diff --git a/test/monniaux/lustrev4_lv4_heater_control/heater_control.c b/test/monniaux/lustrev4_lv4_heater_control/heater_control.c
new file mode 100644
index 00000000..4a2b8174
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/heater_control.c
@@ -0,0 +1,275 @@
+/********
+* ec2c version 0.67
+* c file generated for node : heater_control
+* context method = HEAP
+* ext call method = PROCEDURES
+********/
+#include <stdlib.h>
+#include <string.h>
+#define _heater_control_EC2C_SRC_FILE
+#include "heater_control.h"
+/*--------
+Internal structure for the call
+--------*/
+typedef struct {
+ void* client_data;
+ //INPUTS
+ _real _T;
+ _real _T1;
+ _real _T2;
+ _real _T3;
+ //OUTPUTS
+ _boolean _Heat_on;
+ //REGISTERS
+ _boolean M73;
+ _boolean M73_nil;
+ _boolean M5;
+} heater_control_ctx;
+/*--------
+Output procedures must be defined,
+Input procedures must be used:
+--------*/
+void heater_control_I_T(heater_control_ctx* ctx, _real V){
+ ctx->_T = V;
+}
+void heater_control_I_T1(heater_control_ctx* ctx, _real V){
+ ctx->_T1 = V;
+}
+void heater_control_I_T2(heater_control_ctx* ctx, _real V){
+ ctx->_T2 = V;
+}
+void heater_control_I_T3(heater_control_ctx* ctx, _real V){
+ ctx->_T3 = V;
+}
+extern void heater_control_O_Heat_on(void* cdata, _boolean);
+#ifdef CKCHECK
+extern void heater_control_BOT_Heat_on(void* cdata);
+#endif
+/*--------
+Internal reset input procedure
+--------*/
+static void heater_control_reset_input(heater_control_ctx* ctx){
+ //NOTHING FOR THIS VERSION...
+}
+/*--------
+Reset procedure
+--------*/
+void heater_control_reset(heater_control_ctx* ctx){
+ ctx->M73_nil = _true;
+ ctx->M5 = _true;
+ heater_control_reset_input(ctx);
+}
+/*--------
+Copy the value of an internal structure
+--------*/
+void heater_control_copy_ctx(heater_control_ctx* dest, heater_control_ctx
+* src){
+ memcpy((void*)dest, (void*)src, sizeof(heater_control_ctx));
+}
+/*--------
+Dynamic allocation of an internal structure
+--------*/
+heater_control_ctx* heater_control_new_ctx(void* cdata){
+ heater_control_ctx* ctx = (heater_control_ctx*)calloc(1, sizeof(
+heater_control_ctx));
+ ctx->client_data = cdata;
+ heater_control_reset(ctx);
+ return ctx;
+}
+/*--------
+Step procedure
+--------*/
+void heater_control_step(heater_control_ctx* ctx){
+//LOCAL VARIABLES
+ _real L16;
+ _boolean L15;
+ _real L18;
+ _real L14;
+ _boolean L13;
+ _boolean L12;
+ _real L24;
+ _boolean L23;
+ _real L25;
+ _real L22;
+ _boolean L21;
+ _boolean L20;
+ _boolean L11;
+ _real L30;
+ _boolean L29;
+ _real L31;
+ _real L28;
+ _boolean L27;
+ _boolean L26;
+ _boolean L10;
+ _real L32;
+ _boolean L38;
+ _boolean L37;
+ _boolean L40;
+ _boolean L39;
+ _boolean L36;
+ _boolean L42;
+ _boolean L41;
+ _boolean L35;
+ _real L46;
+ _real L45;
+ _boolean L50;
+ _real L49;
+ _boolean L48;
+ _real L47;
+ _real L44;
+ _boolean L54;
+ _real L53;
+ _boolean L52;
+ _real L51;
+ _real L43;
+ _boolean L57;
+ _boolean L56;
+ _real L59;
+ _real L63;
+ _real L62;
+ _real L65;
+ _real L64;
+ _real L61;
+ _real L58;
+ _real L55;
+ _real L34;
+ _real L9;
+ _boolean L8;
+ _boolean L68;
+ _boolean L71;
+ _boolean L70;
+ _boolean L67;
+ _boolean L7;
+ _boolean L4;
+ _boolean T73;
+//CODE
+ L16 = (ctx->_T1 - ctx->_T2);
+ L15 = (L16 >= 0.000000);
+ L18 = (- L16);
+ if (L15) {
+ L14 = L16;
+ } else {
+ L14 = L18;
+ }
+ L13 = (L14 < 0.500000);
+ L12 = (! L13);
+ L24 = (ctx->_T1 - ctx->_T3);
+ L23 = (L24 >= 0.000000);
+ L25 = (- L24);
+ if (L23) {
+ L22 = L24;
+ } else {
+ L22 = L25;
+ }
+ L21 = (L22 < 0.500000);
+ L20 = (! L21);
+ L11 = (L12 && L20);
+ L30 = (ctx->_T2 - ctx->_T3);
+ L29 = (L30 >= 0.000000);
+ L31 = (- L30);
+ if (L29) {
+ L28 = L30;
+ } else {
+ L28 = L31;
+ }
+ L27 = (L28 < 0.500000);
+ L26 = (! L27);
+ L10 = (L11 && L26);
+ L32 = (- 999.000000);
+ L38 = (L13 && L20);
+ L37 = (L38 && L26);
+ L40 = (L21 && L12);
+ L39 = (L40 && L26);
+ L36 = (L37 || L39);
+ L42 = (L27 && L12);
+ L41 = (L42 && L20);
+ L35 = (L36 || L41);
+ L46 = (ctx->_T1 + ctx->_T2);
+ L45 = (L46 + ctx->_T3);
+ L50 = (ctx->_T2 < ctx->_T3);
+ if (L50) {
+ L49 = ctx->_T2;
+ } else {
+ L49 = ctx->_T3;
+ }
+ L48 = (ctx->_T1 < L49);
+ if (L48) {
+ L47 = ctx->_T1;
+ } else {
+ L47 = L49;
+ }
+ L44 = (L45 - L47);
+ L54 = (ctx->_T2 > ctx->_T3);
+ if (L54) {
+ L53 = ctx->_T2;
+ } else {
+ L53 = ctx->_T3;
+ }
+ L52 = (ctx->_T1 > L53);
+ if (L52) {
+ L51 = ctx->_T1;
+ } else {
+ L51 = L53;
+ }
+ L43 = (L44 - L51);
+ L57 = (L13 && L21);
+ L56 = (L57 && L27);
+ L59 = (L46 / 2.000000);
+ L63 = (ctx->_T1 + ctx->_T3);
+ L62 = (L63 / 2.000000);
+ L65 = (ctx->_T2 + ctx->_T3);
+ L64 = (L65 / 2.000000);
+ if (L21) {
+ L61 = L62;
+ } else {
+ L61 = L64;
+ }
+ if (L13) {
+ L58 = L59;
+ } else {
+ L58 = L61;
+ }
+ if (L56) {
+ L55 = L43;
+ } else {
+ L55 = L58;
+ }
+ if (L35) {
+ L34 = L43;
+ } else {
+ L34 = L55;
+ }
+ if (L10) {
+ L9 = L32;
+ } else {
+ L9 = L34;
+ }
+ L8 = (L9 == L32);
+ L68 = (L9 < 6.000000);
+ L71 = (L9 > 9.000000);
+ if (L71) {
+ L70 = _false;
+ } else {
+ L70 = ctx->M73;
+ }
+ if (L68) {
+ L67 = _true;
+ } else {
+ L67 = L70;
+ }
+ if (L8) {
+ L7 = _false;
+ } else {
+ L7 = L67;
+ }
+ if (ctx->M5) {
+ L4 = _true;
+ } else {
+ L4 = L7;
+ }
+ heater_control_O_Heat_on(ctx->client_data, L4);
+ T73 = L4;
+ ctx->M73 = T73;
+ ctx->M73_nil = _false;
+ ctx->M5 = ctx->M5 && !(_true);
+}
diff --git a/test/monniaux/lustrev4_lv4_heater_control/heater_control.h b/test/monniaux/lustrev4_lv4_heater_control/heater_control.h
new file mode 100644
index 00000000..64be774b
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/heater_control.h
@@ -0,0 +1,47 @@
+/********
+* ec2c version 0.67
+* context method = HEAP
+* ext call method = PROCEDURES
+* c header file generated for node : heater_control
+* to be used with : heater_control.c
+********/
+#ifndef _heater_control_EC2C_H_FILE
+#define _heater_control_EC2C_H_FILE
+/*-------- Predefined types ---------*/
+#ifndef _EC2C_PREDEF_TYPES
+#define _EC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+/*--------- Pragmas ----------------*/
+//MODULE: heater_control 4 1
+//IN: _real T
+//IN: _real T1
+//IN: _real T2
+//IN: _real T3
+//OUT: _boolean Heat_on
+#ifndef _heater_control_EC2C_SRC_FILE
+/*--------Context type -------------*/
+struct heater_control_ctx;
+/*-------- Input procedures -------------*/
+extern void heater_control_I_T(struct heater_control_ctx* ctx, _real);
+extern void heater_control_I_T1(struct heater_control_ctx* ctx, _real);
+extern void heater_control_I_T2(struct heater_control_ctx* ctx, _real);
+extern void heater_control_I_T3(struct heater_control_ctx* ctx, _real);
+/*-------- Reset procedure -----------*/
+extern void heater_control_reset(struct heater_control_ctx* ctx);
+/*--------Context copy -------------*/
+extern void heater_control_copy_ctx(struct heater_control_ctx* dest, struct
+heater_control_ctx* src);
+/*--------Context allocation --------*/
+extern struct heater_control_ctx* heater_control_new_ctx(void* client_data);
+/*-------- Step procedure -----------*/
+extern void heater_control_step(struct heater_control_ctx* ctx);
+#endif
+#endif
diff --git a/test/monniaux/lustrev4_lv4_heater_control/heater_control.lus b/test/monniaux/lustrev4_lv4_heater_control/heater_control.lus
new file mode 100644
index 00000000..5d0db95f
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/heater_control.lus
@@ -0,0 +1,126 @@
+--
+-- A fault-tolerant heater controller with 3 sensors.
+--
+-- To guess the temperature (T),
+--
+-- (1) It compares the value of the 3 sensors 2 by 2 to determine
+-- which ones seem are broken -- we consider then broken if they
+-- differ too much.
+--
+-- (2) then, it performs a vote:
+-- o If the tree sensors are broken, it does not heat;
+-- o If the temperature is bigger than TMAX, it does not heat;
+-- o If the temperature is smaller than TMIN, it heats;
+-- o Otherwise, it keeps its previous state.
+
+
+const FAILURE = -999.0; -- a fake temperature given when all sensors are broken
+const TMIN = 6.0;
+const TMAX = 9.0;
+
+
+const DELTA = 0.5;
+-- const DELTA : real;
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+node heater_control(T, T1, T2, T3 : real) returns (Heat_on:bool);
+-- T is supposed to be the real temperature and is not
+-- used in the controller; we add it here in oder to test the
+-- controller to be able to write a sensible oracle.
+
+
+var
+ V12, V13, V23 : bool;
+ Tguess : real;
+
+let
+ V12 = abs(T1-T2) < DELTA; -- Are T1 and T2 valid?
+ V13 = abs(T1-T3) < DELTA; -- Are T1 and T3 valid?
+ V23 = abs(T2-T3) < DELTA; -- Are T2 and T3 valid?
+
+ Tguess =
+ if noneoftree(V12, V13, V23) then FAILURE else
+ if oneoftree(V12, V13, V23) then Median(T1, T2, T3) else
+ if alloftree(V12, V13, V23) then Median(T1, T2, T3) else
+ -- 2 among V1, V2, V3 are false
+ if V12 then Average(T1, T2) else
+ if V13 then Average(T1, T3) else
+ -- V23 is necessarily true, hence T1 is wrong
+ Average(T2, T3) ;
+
+ Heat_on = true ->
+ if Tguess = FAILURE then false else
+ if Tguess < TMIN then true else
+ if Tguess > TMAX then false else
+ pre Heat_on;
+tel
+
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+node not_a_sauna(T, T1, T2, T3 : real; Heat_on: bool) returns (ok:bool);
+
+let
+ ok = true -> pre T < TMAX + 1.0 ;
+tel
+
+node not_a_sauna2(T, T1, T2, T3 : real; Heat_on: bool) returns (ok:bool);
+
+let
+ ok = true -> pre T < TMAX - 6.0 ;
+tel
+
+
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+
+-- returns the absolute value of 2 reals
+node abs (v : real) returns (a : real) ;
+let
+ a = if v >= 0.0 then v else -v ;
+tel
+
+-- returns the average values of 2 reals
+node Average(a, b: real) returns (z : real);
+let
+ z = (a+b)/2.0 ;
+tel
+
+-- returns the median values of 3 reals
+node Median(a, b, c : real) returns (z : real);
+let
+ z = a + b + c - min2 (a, min2(b,c)) - max2 (a, max2(b,c)) ;
+tel
+
+
+-- returns the maximum values of 2 reals
+node max2 (one, two : real) returns (m : real) ;
+let
+ m = if one > two then one else two ;
+tel
+
+-- returns the minimum values of 2 reals
+node min2 (one, two : real) returns (m : real) ;
+let
+ m = if one < two then one else two ;
+tel
+
+
+node noneoftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = not f1 and not f2 and not f3 ;
+tel
+
+node alloftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = f1 and f2 and f3 ;
+tel
+
+node oneoftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = f1 and not f2 and not f3 or
+ f2 and not f1 and not f3 or
+ f3 and not f1 and not f2 ;
+tel
diff --git a/test/monniaux/lustrev4_lv4_heater_control/heater_control_loop.c b/test/monniaux/lustrev4_lv4_heater_control/heater_control_loop.c
new file mode 100644
index 00000000..06bad418
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/heater_control_loop.c
@@ -0,0 +1,64 @@
+/********
+* ec2c version 0.67
+* c main file generated for node : heater_control
+* to be used with : heater_control.c
+* and : heater_control.h
+* context method = HEAP
+* ext call method = PROCEDURES
+********/
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "../clock.h"
+#include "../dm_random.c"
+#include "heater_control.h"
+
+/* Print a promt ? ************************/
+static int ISATTY;
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "true"
+#endif
+#ifndef FF
+#define FF "false"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_real _get_real(char* n){
+ return (dm_random_uint32()%70000U) * 1E-3 -20.0;
+}
+/* Standard Output procedures **************/
+void _put_bool(char* n, _boolean b){
+ printf("%s: %d\n", n, b);
+}
+/* Output procedures **********************/
+void heater_control_O_Heat_on(void* cdata, _boolean _V) {
+ // _put_bool("Heat_on", _V);
+}
+/* Main procedure *************************/
+int main(){
+
+ /* Context allocation */
+ struct heater_control_ctx* ctx = heater_control_new_ctx(NULL);
+ /* Main loop */
+ clock_prepare();
+ clock_start();
+ heater_control_reset(ctx);
+ for(int count=0; count<1000; count++){
+ heater_control_I_T(ctx, _get_real("T"));
+ heater_control_I_T1(ctx, _get_real("T1"));
+ heater_control_I_T2(ctx, _get_real("T2"));
+ heater_control_I_T3(ctx, _get_real("T3"));
+ heater_control_step(ctx);
+
+ }
+ clock_stop();
+ print_total_clock();
+ return 0;
+}
diff --git a/test/monniaux/lustrev4_lv4_heater_control/make.proto b/test/monniaux/lustrev4_lv4_heater_control/make.proto
new file mode 100644
index 00000000..f19fec0f
--- /dev/null
+++ b/test/monniaux/lustrev4_lv4_heater_control/make.proto
@@ -0,0 +1,3 @@
+sources: "$(wildcard *.c)"
+target: lv4_heater_control
+measures: [cycles]
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/Makefile b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/Makefile
new file mode 100644
index 00000000..90d0a4c9
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/Makefile
@@ -0,0 +1,3 @@
+TARGET=lv6-en-2cgc_heater_control
+
+include ../rules.mk
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control.lus b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control.lus
new file mode 100644
index 00000000..9a668a47
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control.lus
@@ -0,0 +1,126 @@
+--
+-- A fault-tolerant heater controller with 3 sensors.
+--
+-- To guess the temperature (T),
+--
+-- (1) It compares the value of the 3 sensors 2 by 2 to determine
+-- which ones seem are broken -- we consider then broken if they
+-- differ too much.
+--
+-- (2) then, it performs a vote:
+-- o If the tree sensors are broken, it does not heat;
+-- o If the temperature is bigger than TMAX, it does not heat;
+-- o If the temperature is smaller than TMIN, it heats;
+-- o Otherwise, it keeps its previous state.
+
+
+const FAILURE = -999.0; -- a fake temperature given when all sensors are broken
+const TMIN = 6.0;
+const TMAX = 9.0;
+
+
+const DELTA = 0.5;
+-- const DELTA : real;
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+node heater_control(T, T1, T2, T3 : real) returns (Heat_on:bool);
+-- T is supposed to be the real temperature and is not
+-- used in the controller; we add it here in oder to test the
+-- controller to be able to write a sensible oracle.
+
+
+var
+ V12, V13, V23 : bool;
+ Tguess : real;
+
+let
+ V12 = abs(T1-T2) < DELTA; -- Are T1 and T2 valid?
+ V13 = abs(T1-T3) < DELTA; -- Are T1 and T3 valid?
+ V23 = abs(T2-T3) < DELTA; -- Are T2 and T3 valid?
+
+ Tguess =
+ if noneoftree(V12, V13, V23) then FAILURE else
+ if oneoftree(V12, V13, V23) then Median(T1, T2, T3) else
+ if alloftree(V12, V13, V23) then Median(T1, T2, T3) else
+ -- 2 among V1, V2, V3 are false
+ if V12 then Average(T1, T2) else
+ if V13 then Average(T1, T3) else
+ -- V23 is necessarily true, hence T1 is wrong
+ Average(T2, T3) ;
+
+ Heat_on = true ->
+ if Tguess = FAILURE then false else
+ if Tguess < TMIN then true else
+ if Tguess > TMAX then false else
+ pre Heat_on;
+tel
+
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+node not_a_sauna(T, T1, T2, T3 : real; Heat_on: bool) returns (ok:bool);
+
+let
+ ok = true -> pre T < TMAX + 1.0 ;
+tel
+
+node not_a_sauna2(T, T1, T2, T3 : real; Heat_on: bool) returns (ok:bool);
+
+let
+ ok = true -> pre T < TMAX - 6.0 ;
+tel
+
+
+
+-----------------------------------------------------------------------
+-----------------------------------------------------------------------
+
+-- returns the absolute value of 2 reals
+function abs (v : real) returns (a : real) ;
+let
+ a = if v >= 0.0 then v else -v ;
+tel
+
+-- returns the average values of 2 reals
+function Average(a, b: real) returns (z : real);
+let
+ z = (a+b)/2.0 ;
+tel
+
+-- returns the median values of 3 reals
+function Median(a, b, c : real) returns (z : real);
+let
+ z = a + b + c - min2 (a, min2(b,c)) - max2 (a, max2(b,c)) ;
+tel
+
+
+-- returns the maximum values of 2 reals
+function max2 (one, two : real) returns (m : real) ;
+let
+ m = if one > two then one else two ;
+tel
+
+-- returns the minimum values of 2 reals
+function min2 (one, two : real) returns (m : real) ;
+let
+ m = if one < two then one else two ;
+tel
+
+
+function noneoftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = not f1 and not f2 and not f3 ;
+tel
+
+function alloftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = f1 and f2 and f3 ;
+tel
+
+function oneoftree (f1, f2, f3 : bool) returns (r : bool)
+let
+ r = f1 and not f2 and not f3 or
+ f2 and not f1 and not f3 or
+ f3 and not f1 and not f2 ;
+tel
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.c b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.c
new file mode 100644
index 00000000..b3995c61
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.c
@@ -0,0 +1,309 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+#include "heater_control_heater_control.h"
+//// Defining step functions
+// Memory initialisation for Lustre_arrow_ctx
+#define DM_INLINE inline
+
+DM_INLINE void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_ctx
+DM_INLINE void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_ctx
+DM_INLINE void Lustre_arrow_step(_boolean i1,_boolean i2,_boolean *out,Lustre_arrow_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_step
+
+// Memory initialisation for Lustre_pre_ctx
+DM_INLINE void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_ctx
+DM_INLINE void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_ctx
+DM_INLINE void Lustre_pre_get(_boolean *out,Lustre_pre_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_get
+
+DM_INLINE void Lustre_pre_set(_boolean i1,Lustre_pre_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_set
+
+// Step function(s) for Lustre_slash_ctx
+#if 0
+DM_INLINE void Lustre_slash_step(_real i1,_real i2,_real *out){
+ *out = (i1 / i2);
+
+}
+#else
+#define Lustre_slash_step(x, y, out) *out = x/y
+#endif
+// End of Lustre_slash_step
+
+// Memory initialisation for heater_control_heater_control_ctx
+DM_INLINE void heater_control_heater_control_ctx_reset(heater_control_heater_control_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of heater_control_heater_control_ctx
+void heater_control_heater_control_ctx_init(heater_control_heater_control_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ heater_control_heater_control_ctx_reset(ctx);
+ }
+// Step function(s) for heater_control_heater_control_ctx
+void heater_control_heater_control_step(_real T,_real T1,_real T2,_real T3,_boolean *Heat_on,heater_control_heater_control_ctx_type* ctx){ _boolean __split_9_3;
+ _real __split_10_3;
+ _boolean __split_9_2;
+ _real __split_10_2;
+ _boolean __split_9_1;
+ _real __split_10_1;
+ _real __split_1_3;
+ _real __split_1_2;
+ _real __split_1_1;
+ _real __split_2_2;
+ _real __split_3_2;
+ _real __split_4_2;
+ _real __split_5_2;
+ _real __split_6_2;
+ _real __split_7_2;
+ _real __split_8_2;
+ _boolean ___split_38_1_2;
+ _boolean ___split_38_2_2;
+ _boolean ___split_37_1_2;
+ _boolean ___split_37_2_2;
+ _boolean __split_11_1;
+ _real __split_2_1;
+ _real __split_3_1;
+ _real __split_4_1;
+ _real __split_5_1;
+ _real __split_6_1;
+ _real __split_7_1;
+ _real __split_8_1;
+ _boolean ___split_38_1_1;
+ _boolean ___split_38_2_1;
+ _boolean ___split_37_1_1;
+ _boolean ___split_37_2_1;
+ _boolean __split_43_1;
+ _boolean __split_44_1;
+ _boolean __split_45_1;
+ _boolean __split_46_1;
+ _boolean __split_47_1;
+ _boolean __split_48_1;
+ _boolean __split_49_1;
+ _boolean __split_50_1;
+ _boolean __split_51_1;
+ _boolean __split_52_1;
+ _boolean __split_53_1;
+ _boolean __split_54_1;
+ _boolean __split_55_1;
+ _boolean __split_39_1;
+ _boolean __split_40_1;
+ _boolean __split_41_1;
+ _boolean __split_42_1;
+ _boolean _split_36;
+ _boolean _split_35;
+ _boolean _split_34;
+ _boolean _split_33;
+ _boolean _split_32;
+ _boolean _split_31;
+ _boolean _split_30;
+ _real _split_29;
+ _real _split_28;
+ _real _split_27;
+ _real _split_26;
+ _real _split_25;
+ _real _split_24;
+ _real _split_23;
+ _real _split_22;
+ _boolean _split_21;
+ _real _split_20;
+ _boolean _split_19;
+ _boolean _split_18;
+ _real _split_17;
+ _real _split_16;
+ _real _split_15;
+ _real _split_14;
+ _real _split_13;
+ _real _split_12;
+ _boolean V12;
+ _boolean V13;
+ _boolean V23;
+ _real Tguess;
+
+ _split_16 = T2 - T3;
+ __split_10_1 = - _split_16;
+ __split_9_1 = _split_16 >= 0.0;
+ if (__split_9_1 == _true) {
+ _split_17 = _split_16;
+ } else {
+ _split_17 = __split_10_1;
+ }
+ V23 = _split_17 < 0.5;
+ __split_42_1 = ! V23;
+ _split_12 = T1 - T2;
+ __split_10_3 = - _split_12;
+ __split_9_3 = _split_12 >= 0.0;
+ if (__split_9_3 == _true) {
+ _split_13 = _split_12;
+ } else {
+ _split_13 = __split_10_3;
+ }
+ V12 = _split_13 < 0.5;
+ __split_39_1 = ! V12;
+ _split_14 = T1 - T3;
+ __split_10_2 = - _split_14;
+ __split_9_2 = _split_14 >= 0.0;
+ if (__split_9_2 == _true) {
+ _split_15 = _split_14;
+ } else {
+ _split_15 = __split_10_2;
+ }
+ V13 = _split_15 < 0.5;
+ __split_40_1 = ! V13;
+ __split_41_1 = __split_39_1 & __split_40_1;
+ _split_18 = __split_41_1 & __split_42_1;
+ __split_43_1 = ! V13;
+ __split_44_1 = V12 & __split_43_1;
+ __split_45_1 = ! V23;
+ __split_46_1 = __split_44_1 & __split_45_1;
+ __split_47_1 = ! V12;
+ __split_48_1 = V13 & __split_47_1;
+ __split_49_1 = ! V23;
+ __split_50_1 = __split_48_1 & __split_49_1;
+ __split_51_1 = __split_46_1 | __split_50_1;
+ __split_54_1 = ! V13;
+ __split_52_1 = ! V12;
+ __split_53_1 = V23 & __split_52_1;
+ __split_55_1 = __split_53_1 & __split_54_1;
+ _split_19 = __split_51_1 | __split_55_1;
+ ___split_37_1_1 = T2 > T3;
+ if (___split_37_1_1 == _true) {
+ __split_7_1 = T2;
+ } else {
+ __split_7_1 = T3;
+ }
+ ___split_37_2_1 = T1 > __split_7_1;
+ if (___split_37_2_1 == _true) {
+ __split_8_1 = T1;
+ } else {
+ __split_8_1 = __split_7_1;
+ }
+ ___split_38_1_1 = T2 < T3;
+ if (___split_38_1_1 == _true) {
+ __split_4_1 = T2;
+ } else {
+ __split_4_1 = T3;
+ }
+ ___split_38_2_1 = T1 < __split_4_1;
+ if (___split_38_2_1 == _true) {
+ __split_5_1 = T1;
+ } else {
+ __split_5_1 = __split_4_1;
+ }
+ __split_2_1 = T1 + T2;
+ __split_3_1 = __split_2_1 + T3;
+ __split_6_1 = __split_3_1 - __split_5_1;
+ _split_20 = __split_6_1 - __split_8_1;
+ __split_1_2 = T1 + T3;
+ Lustre_slash_step(__split_1_2,2.0,&_split_24);
+ __split_1_3 = T2 + T3;
+ Lustre_slash_step(__split_1_3,2.0,&_split_25);
+ if (V13 == _true) {
+ _split_26 = _split_24;
+ } else {
+ _split_26 = _split_25;
+ }
+ __split_1_1 = T1 + T2;
+ Lustre_slash_step(__split_1_1,2.0,&_split_23);
+ if (V12 == _true) {
+ _split_27 = _split_23;
+ } else {
+ _split_27 = _split_26;
+ }
+ __split_11_1 = V12 & V13;
+ _split_21 = __split_11_1 & V23;
+ ___split_37_1_2 = T2 > T3;
+ if (___split_37_1_2 == _true) {
+ __split_7_2 = T2;
+ } else {
+ __split_7_2 = T3;
+ }
+ ___split_37_2_2 = T1 > __split_7_2;
+ if (___split_37_2_2 == _true) {
+ __split_8_2 = T1;
+ } else {
+ __split_8_2 = __split_7_2;
+ }
+ ___split_38_1_2 = T2 < T3;
+ if (___split_38_1_2 == _true) {
+ __split_4_2 = T2;
+ } else {
+ __split_4_2 = T3;
+ }
+ ___split_38_2_2 = T1 < __split_4_2;
+ if (___split_38_2_2 == _true) {
+ __split_5_2 = T1;
+ } else {
+ __split_5_2 = __split_4_2;
+ }
+ __split_2_2 = T1 + T2;
+ __split_3_2 = __split_2_2 + T3;
+ __split_6_2 = __split_3_2 - __split_5_2;
+ _split_22 = __split_6_2 - __split_8_2;
+ if (_split_21 == _true) {
+ _split_28 = _split_22;
+ } else {
+ _split_28 = _split_27;
+ }
+ if (_split_19 == _true) {
+ _split_29 = _split_20;
+ } else {
+ _split_29 = _split_28;
+ }
+ if (_split_18 == _true) {
+ Tguess = -999.0;
+ } else {
+ Tguess = _split_29;
+ }
+ _split_30 = Tguess == -999.0;
+ _split_31 = Tguess < 6.0;
+ Lustre_pre_get(&_split_33,&ctx->Lustre_pre_ctx_tab[0]);
+ _split_32 = Tguess > 9.0;
+ if (_split_32 == _true) {
+ _split_34 = _false;
+ } else {
+ _split_34 = _split_33;
+ }
+ if (_split_31 == _true) {
+ _split_35 = _true;
+ } else {
+ _split_35 = _split_34;
+ }
+ if (_split_30 == _true) {
+ _split_36 = _false;
+ } else {
+ _split_36 = _split_35;
+ }
+ Lustre_arrow_step(_true,_split_36,Heat_on,&ctx->Lustre_arrow_ctx_tab[0]);
+ Lustre_pre_set(*Heat_on,&ctx->Lustre_pre_ctx_tab[0]);
+
+} // End of heater_control_heater_control_step
+
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.h b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.h
new file mode 100644
index 00000000..bfe8df20
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control.h
@@ -0,0 +1,30 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "lustre_types.h"
+#include "lustre_consts.h"
+
+#ifndef _heater_control_heater_control_H_FILE
+#define _heater_control_heater_control_H_FILE
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_step(_boolean ,_boolean ,_boolean *,Lustre_arrow_ctx_type*);
+
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_get(_boolean *,Lustre_pre_ctx_type*);
+
+void Lustre_pre_set(_boolean ,Lustre_pre_ctx_type*);
+
+void Lustre_slash_step(_real ,_real ,_real *);
+
+void heater_control_heater_control_ctx_reset(heater_control_heater_control_ctx_type* ctx);
+void heater_control_heater_control_ctx_init(heater_control_heater_control_ctx_type* ctx);
+void heater_control_heater_control_step(_real ,_real ,_real ,_real ,_boolean *,heater_control_heater_control_ctx_type*);
+
+/////////////////////////////////////////////////
+#endif
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control_loop.c b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control_loop.c
new file mode 100644
index 00000000..d1a01268
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/heater_control_heater_control_loop.c
@@ -0,0 +1,63 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "../dm_random.c"
+#include "../clock.h"
+#include "heater_control_heater_control.h"
+
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "1"
+#endif
+#ifndef FF
+#define FF "0"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_real _get_real(char* n){
+ return (dm_random_uint32()%70000U) * 1E-3 -20.0;
+}
+/* Standard Output procedures **************/
+void _put_bool(char* n, _boolean b){
+ printf("%s: %d\n", n, b);
+}
+/* Main procedure *************************/
+int main(){
+ _real T;
+ _real T1;
+ _real T2;
+ _real T3;
+ _boolean Heat_on;
+ heater_control_heater_control_ctx_type ctx_struct;
+ heater_control_heater_control_ctx_type* ctx = &ctx_struct;
+
+ clock_prepare();
+ clock_start();
+
+ heater_control_heater_control_ctx_init(ctx);
+
+ /* Main loop */
+ for(int step=0; step<1000; step++){
+ T = _get_real("T");
+ T1 = _get_real("T1");
+ T2 = _get_real("T2");
+ T3 = _get_real("T3");
+ heater_control_heater_control_step(T,T1,T2,T3,&Heat_on,ctx);
+ // _put_bool("Heat_on", Heat_on);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 1;
+}
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.c b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.c
new file mode 100644
index 00000000..47bfb5f5
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.c
@@ -0,0 +1,4 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+#include "lustre_consts.h" \ No newline at end of file
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.h b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.h
new file mode 100644
index 00000000..c2f70459
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_consts.h
@@ -0,0 +1,9 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+
+// Constant definitions
+#define heater_control_DELTA 0.5
+#define heater_control_FAILURE -999.0
+#define heater_control_TMAX 9.0
+#define heater_control_TMIN 6.0
diff --git a/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_types.h b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_types.h
new file mode 100644
index 00000000..7fdfa03e
--- /dev/null
+++ b/test/monniaux/lustrev4_lv6-en-2cgc_heater_control/lustre_types.h
@@ -0,0 +1,41 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -en -2cgc heater_control.lus -n heater_control */
+/* on vanoise the 15/05/2019 at 13:20:10 */
+
+#ifndef _SOC2C_PREDEF_TYPES
+#define _SOC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+// end of _SOC2C_PREDEF_TYPES
+// User typedef
+#ifndef _heater_control_heater_control_TYPES
+#define _heater_control_heater_control_TYPES
+#endif // enf of _heater_control_heater_control_TYPES
+// Memoryless soc ctx typedef
+// Memoryfull soc ctx typedef
+/* Lustre_arrow_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_ctx_type;
+
+/* Lustre_pre_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_pre_ctx_type;
+
+/* heater_control_heater_control_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} heater_control_heater_control_ctx_type;
+
diff --git a/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.c b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.c
new file mode 100644
index 00000000..206f854a
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.c
@@ -0,0 +1,282 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+#include "carlightV2_carlight.h"
+//// Defining step functions
+// Memory initialisation for Lustre_arrow_ctx
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_ctx
+Lustre_arrow_ctx_type* Lustre_arrow_ctx_new_ctx(){
+
+ Lustre_arrow_ctx_type* ctx = (Lustre_arrow_ctx_type*)calloc(1, sizeof(Lustre_arrow_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_ctx
+void Lustre_arrow_step(_boolean i1,_boolean i2,_boolean *out,Lustre_arrow_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_step
+
+// Memory initialisation for Lustre_arrow_2_ctx
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_2_ctx
+Lustre_arrow_2_ctx_type* Lustre_arrow_2_ctx_new_ctx(){
+
+ Lustre_arrow_2_ctx_type* ctx = (Lustre_arrow_2_ctx_type*)calloc(1, sizeof(Lustre_arrow_2_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_2_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_2_ctx
+void Lustre_arrow_2_step(_real i1,_real i2,_real *out,Lustre_arrow_2_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_2_step
+
+// Memory initialisation for Lustre_pre_ctx
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_ctx
+Lustre_pre_ctx_type* Lustre_pre_ctx_new_ctx(){
+
+ Lustre_pre_ctx_type* ctx = (Lustre_pre_ctx_type*)calloc(1, sizeof(Lustre_pre_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_ctx
+void Lustre_pre_get(_boolean *out,Lustre_pre_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_get
+
+void Lustre_pre_set(_boolean i1,Lustre_pre_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_set
+
+// Memory initialisation for Lustre_pre_2_ctx
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_2_ctx
+Lustre_pre_2_ctx_type* Lustre_pre_2_ctx_new_ctx(){
+
+ Lustre_pre_2_ctx_type* ctx = (Lustre_pre_2_ctx_type*)calloc(1, sizeof(Lustre_pre_2_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_2_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_2_ctx
+void Lustre_pre_2_get(_real *out,Lustre_pre_2_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_2_get
+
+void Lustre_pre_2_set(_real i1,Lustre_pre_2_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_2_set
+
+// Memory initialisation for carlightV2_carlight_ctx
+void carlightV2_carlight_ctx_reset(carlightV2_carlight_ctx_type* ctx){
+ int _i;
+
+ carlightV2_front_montant_ctx_reset(&ctx->carlightV2_front_montant_ctx_tab[0]);
+ carlightV2_carlight_auto_ctx_reset(&ctx->carlightV2_carlight_auto_ctx_tab[0]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+}
+// Memory allocation for carlightV2_carlight_ctx
+carlightV2_carlight_ctx_type* carlightV2_carlight_ctx_new_ctx(){
+
+ carlightV2_carlight_ctx_type* ctx = (carlightV2_carlight_ctx_type*)calloc(1, sizeof(carlightV2_carlight_ctx_type));
+ // ctx->client_data = cdata;
+ carlightV2_carlight_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for carlightV2_carlight_ctx
+void carlightV2_carlight_step(_integer switch_pos,_real intensity,_boolean *is_on,carlightV2_carlight_ctx_type* ctx){ _boolean _split_8;
+ _boolean _split_7;
+ _boolean _split_6;
+ _real _split_5;
+ _boolean _split_4;
+ _boolean _split_3;
+ _boolean _split_2;
+ _boolean _split_1;
+ _boolean fm_auto;
+ _boolean res_auto;
+
+ _split_1 = switch_pos == carlightV2_AUTO;
+ carlightV2_front_montant_step(_split_1,&fm_auto,&ctx->carlightV2_front_montant_ctx_tab[0]);
+ Lustre_pre_get(&_split_6,&ctx->Lustre_pre_ctx_tab[0]);
+ switch (switch_pos){
+ case carlightV2_AUTO:
+ _split_7 = _split_6;
+ _split_5 = intensity;
+ carlightV2_carlight_auto_step(_split_5,_split_7,&_split_8,&ctx->carlightV2_carlight_auto_ctx_tab[0]);
+ break;
+}
+ _split_3 = intensity <= 70.0;
+ switch (switch_pos){
+ case carlightV2_AUTO:
+ _split_4 = _split_3;
+ _split_2 = fm_auto;
+ if (_split_2 == _true) {
+ res_auto = _split_4;
+ } else {
+ res_auto = _split_8;
+ }
+ *is_on = res_auto;
+ break;
+ case carlightV2_OFF:
+ *is_on = _false;
+ break;
+ case carlightV2_ON:
+ *is_on = _true;
+ break;
+}
+ Lustre_pre_set(*is_on,&ctx->Lustre_pre_ctx_tab[0]);
+
+} // End of carlightV2_carlight_step
+
+// Memory initialisation for carlightV2_carlight_auto_ctx
+void carlightV2_carlight_auto_ctx_reset(carlightV2_carlight_auto_ctx_type* ctx){
+ int _i;
+
+ carlightV2_vrai_depuis_n_secondes_ctx_reset(&ctx->carlightV2_vrai_depuis_n_secondes_ctx_tab[0]);
+ carlightV2_vrai_depuis_n_secondes_ctx_reset(&ctx->carlightV2_vrai_depuis_n_secondes_ctx_tab[1]);
+}
+// Memory allocation for carlightV2_carlight_auto_ctx
+carlightV2_carlight_auto_ctx_type* carlightV2_carlight_auto_ctx_new_ctx(){
+
+ carlightV2_carlight_auto_ctx_type* ctx = (carlightV2_carlight_auto_ctx_type*)calloc(1, sizeof(carlightV2_carlight_auto_ctx_type));
+ // ctx->client_data = cdata;
+ carlightV2_carlight_auto_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for carlightV2_carlight_auto_ctx
+void carlightV2_carlight_auto_step(_real intensity,_boolean pre_is_on,_boolean *res,carlightV2_carlight_auto_ctx_type* ctx){ _boolean _split_17;
+ _boolean _split_16;
+ _boolean _split_15;
+ _boolean _split_14;
+ _boolean _split_13;
+ _boolean _split_12;
+ _boolean _split_11;
+ _boolean _split_10;
+ _boolean _split_9;
+
+ _split_14 = intensity < 60.0;
+ _split_13 = ! pre_is_on;
+ _split_15 = _split_13 & _split_14;
+ carlightV2_vrai_depuis_n_secondes_step(_split_15,2.0,&_split_16,&ctx->carlightV2_vrai_depuis_n_secondes_ctx_tab[0]);
+ if (_split_16 == _true) {
+ _split_17 = _true;
+ } else {
+ _split_17 = pre_is_on;
+ }
+ _split_9 = intensity > 70.0;
+ _split_10 = pre_is_on & _split_9;
+ carlightV2_vrai_depuis_n_secondes_step(_split_10,3.0,&_split_11,&ctx->carlightV2_vrai_depuis_n_secondes_ctx_tab[1]);
+ if (_split_11 == _true) {
+ _split_12 = _false;
+ } else {
+ _split_12 = pre_is_on;
+ }
+ if (pre_is_on == _true) {
+ *res = _split_12;
+ } else {
+ *res = _split_17;
+ }
+
+} // End of carlightV2_carlight_auto_step
+
+// Memory initialisation for carlightV2_front_montant_ctx
+void carlightV2_front_montant_ctx_reset(carlightV2_front_montant_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+// Memory allocation for carlightV2_front_montant_ctx
+carlightV2_front_montant_ctx_type* carlightV2_front_montant_ctx_new_ctx(){
+
+ carlightV2_front_montant_ctx_type* ctx = (carlightV2_front_montant_ctx_type*)calloc(1, sizeof(carlightV2_front_montant_ctx_type));
+ // ctx->client_data = cdata;
+ carlightV2_front_montant_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for carlightV2_front_montant_ctx
+void carlightV2_front_montant_step(_boolean x,_boolean *res,carlightV2_front_montant_ctx_type* ctx){ _boolean _split_20;
+ _boolean _split_19;
+ _boolean _split_18;
+
+ Lustre_pre_get(&_split_18,&ctx->Lustre_pre_ctx_tab[0]);
+ _split_19 = ! _split_18;
+ _split_20 = x & _split_19;
+ Lustre_pre_set(x,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(x,_split_20,res,&ctx->Lustre_arrow_ctx_tab[0]);
+
+} // End of carlightV2_front_montant_step
+
+// Step function(s) for carlightV2_max_ctx
+void carlightV2_max_step(_real x,_real y,_real *res){
+ _boolean _split_21;
+
+ _split_21 = x > y;
+ if (_split_21 == _true) {
+ *res = x;
+ } else {
+ *res = y;
+ }
+
+} // End of carlightV2_max_step
+
+// Memory initialisation for carlightV2_vrai_depuis_n_secondes_ctx
+void carlightV2_vrai_depuis_n_secondes_ctx_reset(carlightV2_vrai_depuis_n_secondes_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[0]);
+}
+// Memory allocation for carlightV2_vrai_depuis_n_secondes_ctx
+carlightV2_vrai_depuis_n_secondes_ctx_type* carlightV2_vrai_depuis_n_secondes_ctx_new_ctx(){
+
+ carlightV2_vrai_depuis_n_secondes_ctx_type* ctx = (carlightV2_vrai_depuis_n_secondes_ctx_type*)calloc(1, sizeof(carlightV2_vrai_depuis_n_secondes_ctx_type));
+ // ctx->client_data = cdata;
+ carlightV2_vrai_depuis_n_secondes_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for carlightV2_vrai_depuis_n_secondes_ctx
+void carlightV2_vrai_depuis_n_secondes_step(_boolean signal,_real n,_boolean *res,carlightV2_vrai_depuis_n_secondes_ctx_type* ctx){ _real _split_26;
+ _real _split_25;
+ _real _split_24;
+ _real _split_23;
+ _boolean _split_22;
+ _real tempo;
+
+ _split_22 = ! signal;
+ Lustre_pre_2_get(&_split_23,&ctx->Lustre_pre_2_ctx_tab[0]);
+ _split_24 = _split_23 - 0.5;
+ carlightV2_max_step(0.0,_split_24,&_split_25);
+ if (_split_22 == _true) {
+ _split_26 = n;
+ } else {
+ _split_26 = _split_25;
+ }
+ Lustre_arrow_2_step(n,_split_26,&tempo,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ Lustre_pre_2_set(tempo,&ctx->Lustre_pre_2_ctx_tab[0]);
+ *res = tempo == 0.0;
+
+} // End of carlightV2_vrai_depuis_n_secondes_step
+
diff --git a/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.h b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.h
new file mode 100644
index 00000000..c56c8fa1
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight.h
@@ -0,0 +1,52 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "lustre_types.h"
+#include "lustre_consts.h"
+
+#ifndef _carlightV2_carlight_H_FILE
+#define _carlightV2_carlight_H_FILE
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx);
+Lustre_arrow_ctx_type* Lustre_arrow_ctx_new_ctx();
+void Lustre_arrow_step(_boolean ,_boolean ,_boolean *,Lustre_arrow_ctx_type*);
+
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx);
+Lustre_arrow_2_ctx_type* Lustre_arrow_2_ctx_new_ctx();
+void Lustre_arrow_2_step(_real ,_real ,_real *,Lustre_arrow_2_ctx_type*);
+
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx);
+Lustre_pre_ctx_type* Lustre_pre_ctx_new_ctx();
+void Lustre_pre_get(_boolean *,Lustre_pre_ctx_type*);
+
+void Lustre_pre_set(_boolean ,Lustre_pre_ctx_type*);
+
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx);
+Lustre_pre_2_ctx_type* Lustre_pre_2_ctx_new_ctx();
+void Lustre_pre_2_get(_real *,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_2_set(_real ,Lustre_pre_2_ctx_type*);
+
+void carlightV2_carlight_ctx_reset(carlightV2_carlight_ctx_type* ctx);
+carlightV2_carlight_ctx_type* carlightV2_carlight_ctx_new_ctx();
+void carlightV2_carlight_step(_integer ,_real ,_boolean *,carlightV2_carlight_ctx_type*);
+
+void carlightV2_carlight_auto_ctx_reset(carlightV2_carlight_auto_ctx_type* ctx);
+carlightV2_carlight_auto_ctx_type* carlightV2_carlight_auto_ctx_new_ctx();
+void carlightV2_carlight_auto_step(_real ,_boolean ,_boolean *,carlightV2_carlight_auto_ctx_type*);
+
+void carlightV2_front_montant_ctx_reset(carlightV2_front_montant_ctx_type* ctx);
+carlightV2_front_montant_ctx_type* carlightV2_front_montant_ctx_new_ctx();
+void carlightV2_front_montant_step(_boolean ,_boolean *,carlightV2_front_montant_ctx_type*);
+
+void carlightV2_max_step(_real ,_real ,_real *);
+
+void carlightV2_vrai_depuis_n_secondes_ctx_reset(carlightV2_vrai_depuis_n_secondes_ctx_type* ctx);
+carlightV2_vrai_depuis_n_secondes_ctx_type* carlightV2_vrai_depuis_n_secondes_ctx_new_ctx();
+void carlightV2_vrai_depuis_n_secondes_step(_boolean ,_real ,_boolean *,carlightV2_vrai_depuis_n_secondes_ctx_type*);
+
+/////////////////////////////////////////////////
+#endif
diff --git a/test/monniaux/lustrev6-carlightV2/carlightV2_carlight_loop.c b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight_loop.c
new file mode 100644
index 00000000..a9b4417a
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/carlightV2_carlight_loop.c
@@ -0,0 +1,62 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "carlightV2_carlight.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "1"
+#endif
+#ifndef FF
+#define FF "0"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_boolean _get_bool(char* n){
+ return dm_random_uint32() & 1;
+}
+_integer _get_int(char* n){
+ return (_integer) (dm_random_uint32() % 21) - 10;
+}
+_real _get_real(char* n){
+ return ((_integer) (dm_random_uint32() % 2000001) - 1000000)*1E-6;
+}
+
+/* Main procedure *************************/
+int main(){
+ int _s = 0;
+ _integer switch_pos;
+ _real intensity;
+ _boolean is_on;
+ carlightV2_carlight_ctx_type* ctx = carlightV2_carlight_ctx_new_ctx(NULL);
+
+ /* Main loop */
+ clock_prepare();
+ clock_start();
+
+ for(int count=0; count<1000; count++){
+ ++_s;
+ switch_pos = _get_int("switch_pos");
+ intensity = _get_real("intensity");
+ carlightV2_carlight_step(switch_pos,intensity,&is_on,ctx);
+ // printf("%d %f #outs %d\n",switch_pos,intensity,is_on);
+ // printf("%d\n",is_on);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+}
diff --git a/test/monniaux/lustrev6-carlightV2/lustre_consts.c b/test/monniaux/lustrev6-carlightV2/lustre_consts.c
new file mode 100644
index 00000000..e1a77c9e
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/lustre_consts.c
@@ -0,0 +1,4 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+#include "lustre_consts.h" \ No newline at end of file
diff --git a/test/monniaux/lustrev6-carlightV2/lustre_consts.h b/test/monniaux/lustrev6-carlightV2/lustre_consts.h
new file mode 100644
index 00000000..9d651d25
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/lustre_consts.h
@@ -0,0 +1,9 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+
+// Constant definitions
+#define carlightV2_AUTO 2
+#define carlightV2_OFF 1
+#define carlightV2_ON 0
+#define carlightV2_period 0.5
diff --git a/test/monniaux/lustrev6-carlightV2/lustre_types.h b/test/monniaux/lustrev6-carlightV2/lustre_types.h
new file mode 100644
index 00000000..e1cfb463
--- /dev/null
+++ b/test/monniaux/lustrev6-carlightV2/lustre_types.h
@@ -0,0 +1,75 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 carlightV2.lus -n carlight --to-c */
+/* on vanoise the 08/05/2019 at 22:54:09 */
+
+#ifndef _SOC2C_PREDEF_TYPES
+#define _SOC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+// end of _SOC2C_PREDEF_TYPES
+// User typedef
+#ifndef _carlightV2_carlight_TYPES
+#define _carlightV2_carlight_TYPES
+typedef _integer carlightV2_SwitchMode;
+#endif // enf of _carlightV2_carlight_TYPES
+// Memoryless soc ctx typedef
+// Memoryfull soc ctx typedef
+/* Lustre_pre_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_pre_ctx_type;
+
+/* Lustre_arrow_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_ctx_type;
+
+/* carlightV2_front_montant_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} carlightV2_front_montant_ctx_type;
+
+/* Lustre_pre_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory ;
+} Lustre_pre_2_ctx_type;
+
+/* Lustre_arrow_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_2_ctx_type;
+
+/* carlightV2_vrai_depuis_n_secondes_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[1];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[1];
+} carlightV2_vrai_depuis_n_secondes_ctx_type;
+
+/* carlightV2_carlight_auto_ctx */
+typedef struct {
+ /*INSTANCES*/
+ carlightV2_vrai_depuis_n_secondes_ctx_type carlightV2_vrai_depuis_n_secondes_ctx_tab[2];
+} carlightV2_carlight_auto_ctx_type;
+
+/* carlightV2_carlight_ctx */
+typedef struct {
+ /*INSTANCES*/
+ carlightV2_front_montant_ctx_type carlightV2_front_montant_ctx_tab[1];
+ carlightV2_carlight_auto_ctx_type carlightV2_carlight_auto_ctx_tab[1];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+} carlightV2_carlight_ctx_type;
+
diff --git a/test/monniaux/lustrev6-convertible-2cgc/convertible_main.c b/test/monniaux/lustrev6-convertible-2cgc/convertible_main.c
new file mode 100644
index 00000000..285f8941
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/convertible_main.c
@@ -0,0 +1,1085 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+#include "convertible_main.h"
+//// Defining step functions
+// Memory initialisation for Lustre_arrow_ctx
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_ctx
+void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_ctx
+void Lustre_arrow_step(_integer i1,_integer i2,_integer *out,Lustre_arrow_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_step
+
+// Memory initialisation for Lustre_arrow_2_ctx
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_2_ctx
+void Lustre_arrow_2_ctx_init(Lustre_arrow_2_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_2_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_2_ctx
+void Lustre_arrow_2_step(_real i1,_real i2,_real *out,Lustre_arrow_2_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_2_step
+
+// Memory initialisation for Lustre_arrow_3_ctx
+void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_3_ctx
+void Lustre_arrow_3_ctx_init(Lustre_arrow_3_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_3_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_3_ctx
+void Lustre_arrow_3_step(_real i1[50],_real i2[50],_real out[50]/*out*/,Lustre_arrow_3_ctx_type* ctx){ _assign_rp50(out, ((ctx->_memory)? i1 : i2), sizeof(_real [50]));
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_3_step
+
+// Step function(s) for Lustre_hat_ctx
+void Lustre_hat_step(_real i1,_real out[50]/*out*/){
+ out[0] = i1;
+ out[1] = i1;
+ out[2] = i1;
+ out[3] = i1;
+ out[4] = i1;
+ out[5] = i1;
+ out[6] = i1;
+ out[7] = i1;
+ out[8] = i1;
+ out[9] = i1;
+ out[10] = i1;
+ out[11] = i1;
+ out[12] = i1;
+ out[13] = i1;
+ out[14] = i1;
+ out[15] = i1;
+ out[16] = i1;
+ out[17] = i1;
+ out[18] = i1;
+ out[19] = i1;
+ out[20] = i1;
+ out[21] = i1;
+ out[22] = i1;
+ out[23] = i1;
+ out[24] = i1;
+ out[25] = i1;
+ out[26] = i1;
+ out[27] = i1;
+ out[28] = i1;
+ out[29] = i1;
+ out[30] = i1;
+ out[31] = i1;
+ out[32] = i1;
+ out[33] = i1;
+ out[34] = i1;
+ out[35] = i1;
+ out[36] = i1;
+ out[37] = i1;
+ out[38] = i1;
+ out[39] = i1;
+ out[40] = i1;
+ out[41] = i1;
+ out[42] = i1;
+ out[43] = i1;
+ out[44] = i1;
+ out[45] = i1;
+ out[46] = i1;
+ out[47] = i1;
+ out[48] = i1;
+ out[49] = i1;
+
+} // End of Lustre_hat_step
+
+// Memory initialisation for Lustre_pre_ctx
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_ctx
+void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_ctx
+void Lustre_pre_get(_integer *out,Lustre_pre_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_get
+
+void Lustre_pre_set(_integer i1,Lustre_pre_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_set
+
+// Memory initialisation for Lustre_pre_2_ctx
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_2_ctx
+void Lustre_pre_2_ctx_init(Lustre_pre_2_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_2_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_2_ctx
+void Lustre_pre_2_get(_real *out,Lustre_pre_2_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_2_get
+
+void Lustre_pre_2_set(_real i1,Lustre_pre_2_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_2_set
+
+// Memory initialisation for Lustre_pre_3_ctx
+void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_3_ctx
+void Lustre_pre_3_ctx_init(Lustre_pre_3_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_3_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_3_ctx
+void Lustre_pre_3_get(_real out[50]/*out*/,Lustre_pre_3_ctx_type* ctx){
+ _assign_rp50(out, ctx->_memory, sizeof(_real [50]));
+
+} // End of Lustre_pre_3_get
+
+void Lustre_pre_3_set(_real i1[50],Lustre_pre_3_ctx_type* ctx){
+ _assign_rp50(ctx->_memory, i1, sizeof(_real [50]));
+
+} // End of Lustre_pre_3_set
+
+// Step function(s) for Lustre_slash_ctx
+void Lustre_slash_step(_real i1,_real i2,_real *out){
+ *out = (i1 / i2);
+
+} // End of Lustre_slash_step
+
+// Step function(s) for assign_50_ctx
+void assign_50_step(_real v,_integer jv,_real t[50],_real nt[50]/*out*/){
+ convertible_update_acc _split_3;
+ convertible_update_acc dummy;
+
+ _split_3.i = 0;
+ _split_3.j = jv;
+ _split_3.v = v;
+ fillred_update_cell_do_50_step(_split_3,t,&dummy,nt);
+
+} // End of assign_50_step
+
+// Step function(s) for convertible_abs_ctx
+void convertible_abs_step(_real x,_real *y){
+ _real _split_2;
+ _boolean _split_1;
+
+ _split_2 = - x;
+ _split_1 = x >= 0.0;
+ if (_split_1 == _true) {
+ *y = x;
+ } else {
+ *y = _split_2;
+ }
+
+} // End of convertible_abs_step
+
+// Step function(s) for convertible_braking_time_ctx
+void convertible_braking_time_step(_real Speed,_real *res){
+ _real _split_4;
+
+ _split_4 = Speed * Speed;
+ Lustre_slash_step(_split_4,5500.0,res);
+
+} // End of convertible_braking_time_step
+
+// Memory initialisation for convertible_main_ctx
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx){
+ int _i;
+
+ convertible_vehicle_ctx_reset(&ctx->convertible_vehicle_ctx_tab[0]);
+ convertible_speed_kmh_ctx_reset(&ctx->convertible_speed_kmh_ctx_tab[0]);
+ convertible_roof_ctx_reset(&ctx->convertible_roof_ctx_tab[0]);
+ convertible_may_collide_ctx_reset(&ctx->convertible_may_collide_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of convertible_main_ctx
+void convertible_main_ctx_init(convertible_main_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_main_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_main_ctx
+void convertible_main_step(_boolean Start,_boolean Parked,_boolean Rot,_boolean Tick,_boolean OnOff,_boolean Done,_real Dist,_boolean *Danger,_boolean *Locked,_real *Speed,_real *Roof_Speed,convertible_main_ctx_type* ctx){ _boolean _split_7;
+ _real _split_6;
+ _real _split_5;
+ _integer St;
+ _boolean _split_8;
+ _boolean _split_9;
+
+ _split_8 = OnOff & Start;
+ _split_9 = ! _split_8;
+ convertible_roof_step(Tick,Parked,OnOff,Done,Locked,Roof_Speed,&ctx->convertible_roof_ctx_tab[0]);
+ convertible_speed_kmh_step(Rot,Tick,Speed,&ctx->convertible_speed_kmh_ctx_tab[0]);
+ convertible_vehicle_step(Start,*Locked,*Speed,Dist,&St,&ctx->convertible_vehicle_ctx_tab[0]);
+ switch (St){
+ case convertible_anti_col:
+ _split_6 = Dist;
+ _split_5 = *Speed;
+ convertible_may_collide_step(_split_5,_split_6,&_split_7,&ctx->convertible_may_collide_ctx_tab[0]);
+ *Danger = _split_7;
+ break;
+ case convertible_run:
+ *Danger = _false;
+ break;
+ case convertible_stationnary:
+ *Danger = _false;
+ break;
+}
+
+} // End of convertible_main_step
+
+// Step function(s) for convertible_maxr_ctx
+void convertible_maxr_step(_real x,_real y,_real *res){
+ _boolean _split_10;
+
+ _split_10 = x < y;
+ if (_split_10 == _true) {
+ *res = y;
+ } else {
+ *res = x;
+ }
+
+} // End of convertible_maxr_step
+
+// Memory initialisation for convertible_may_collide_ctx
+void convertible_may_collide_ctx_reset(convertible_may_collide_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of convertible_may_collide_ctx
+void convertible_may_collide_ctx_init(convertible_may_collide_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_may_collide_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_may_collide_ctx
+void convertible_may_collide_step(_real Speed,_real Dist,_boolean *Res,convertible_may_collide_ctx_type* ctx){ _real _split_17;
+ _real _split_16;
+ _real _split_15;
+ _real _split_14;
+ _real _split_13;
+ _real _split_12;
+ _real _split_11;
+ _real Accel;
+ _real tChoc;
+ _real tBrake;
+
+ Lustre_pre_2_get(&_split_11,&ctx->Lustre_pre_2_ctx_tab[0]);
+ _split_12 = Speed - _split_11;
+ Lustre_slash_step(_split_12,0.1,&_split_13);
+ Lustre_pre_2_set(Speed,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_step(0.0,_split_13,&Accel,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ convertible_braking_time_step(Speed,&tBrake);
+ _split_15 = - 2.0;
+ _split_16 = _split_15 * Dist;
+ _split_14 = 2.0 * Speed;
+ convertible_solve_eq_d2_step(Accel,_split_14,_split_16,&tChoc);
+ _split_17 = 2.0 + tBrake;
+ *Res = tChoc < _split_17;
+
+} // End of convertible_may_collide_step
+
+// Step function(s) for convertible_ms_to_kmh_ctx
+void convertible_ms_to_kmh_step(_real x,_real *res){
+
+ *res = x * 3.6;
+
+} // End of convertible_ms_to_kmh_step
+
+// Memory initialisation for convertible_roof_ctx
+void convertible_roof_ctx_reset(convertible_roof_ctx_type* ctx){
+ int _i;
+
+ convertible_roof_speed_ctx_reset(&ctx->convertible_roof_speed_ctx_tab[0]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of convertible_roof_ctx
+void convertible_roof_ctx_init(convertible_roof_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_roof_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_roof_ctx
+void convertible_roof_step(_boolean Tick,_boolean Parked,_boolean OnOff,_boolean Done,_boolean *Locked,_real *Roof_Speed,convertible_roof_ctx_type* ctx){ _real _split_25;
+ _real _split_24;
+ _integer _split_23;
+ _boolean _split_22;
+ _integer _split_21;
+ _boolean _split_20;
+ _boolean _split_19;
+ _integer _split_18;
+ _integer pst;
+ _integer st;
+ _boolean Tick_on_in_motion;
+
+ Lustre_pre_get(&_split_18,&ctx->Lustre_pre_ctx_tab[0]);
+ switch (pst){
+ case convertible_in_motion:
+ _split_22 = Done;
+ if (_split_22 == _true) {
+ _split_23 = convertible_locked;
+ } else {
+ _split_23 = convertible_in_motion;
+ }
+ st = _split_23;
+ break;
+}
+ _split_19 = OnOff & Parked;
+ switch (pst){
+ case convertible_locked:
+ _split_20 = _split_19;
+ if (_split_20 == _true) {
+ _split_21 = convertible_in_motion;
+ } else {
+ _split_21 = convertible_locked;
+ }
+ st = _split_21;
+ break;
+}
+ Lustre_pre_set(st,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(convertible_locked,_split_18,&pst,&ctx->Lustre_arrow_ctx_tab[0]);
+ *Locked = st == convertible_locked;
+ switch (st){
+ case convertible_in_motion:
+ Tick_on_in_motion = Tick;
+ convertible_roof_speed_step(Tick_on_in_motion,&_split_25,&ctx->convertible_roof_speed_ctx_tab[0]);
+ *Roof_Speed = _split_25;
+ break;
+ case convertible_locked:
+ _split_24 = 0.0;
+ *Roof_Speed = _split_24;
+ break;
+}
+
+} // End of convertible_roof_step
+
+// Memory initialisation for convertible_roof_speed_ctx
+void convertible_roof_speed_ctx_reset(convertible_roof_speed_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[0]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[1]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of convertible_roof_speed_ctx
+void convertible_roof_speed_ctx_init(convertible_roof_speed_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_roof_speed_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_roof_speed_ctx
+void convertible_roof_speed_step(_boolean Tick,_real *Roof_Speed,convertible_roof_speed_ctx_type* ctx){ _real _split_48;
+ _real _split_47;
+ _real _split_46;
+ _real _split_45;
+ _real _split_44;
+ _real _split_43;
+ _real _split_42;
+ _real _split_41;
+ _real _split_40;
+ _real _split_39;
+ _real _split_38;
+ _real _split_37;
+ _real _split_36;
+ _real _split_35;
+ _real _split_34;
+ _real _split_33;
+ _integer _split_32;
+ _boolean _split_31;
+ _real _split_30;
+ _integer _split_29;
+ _boolean _split_28;
+ _real _split_27;
+ _integer _split_26;
+ _integer pst;
+ _integer st;
+ _real kh;
+ _real Roof_Percent;
+ _real pRoof_Percent;
+ _real slow_it_down;
+ _real pRoof_Speed;
+
+ switch (Tick){
+ case _true:
+ Lustre_pre_get(&_split_26,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_pre_2_get(&_split_33,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_step(0.0,_split_33,&pRoof_Percent,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ switch (pst){
+ case convertible_fast:
+ _split_27 = pRoof_Percent;
+ _split_28 = _split_27 < 85.0;
+ if (_split_28 == _true) {
+ _split_29 = convertible_fast;
+ } else {
+ _split_29 = convertible_slow;
+ }
+ st = _split_29;
+ break;
+ case convertible_slow:
+ _split_30 = pRoof_Percent;
+ _split_31 = _split_30 < 100.0;
+ if (_split_31 == _true) {
+ _split_32 = convertible_slow;
+ } else {
+ _split_32 = convertible_wait;
+ }
+ st = _split_32;
+ break;
+ case convertible_wait:
+ st = convertible_fast;
+ break;
+}
+ Lustre_pre_set(st,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(convertible_wait,_split_26,&pst,&ctx->Lustre_arrow_ctx_tab[0]);
+ Lustre_slash_step(5.,0.1,&_split_38);
+ Lustre_slash_step(100.,_split_38,&kh);
+ _split_43 = kh + pRoof_Percent;
+ switch (st){
+ case convertible_fast:
+ _split_44 = _split_43;
+ Roof_Percent = _split_44;
+ break;
+ case convertible_slow:
+ _split_47 = pRoof_Percent;
+ _split_34 = pRoof_Percent;
+ _split_35 = 100.0 - _split_34;
+ Lustre_slash_step(_split_35,5.0,&_split_36);
+ convertible_sqrt_step(_split_36,&_split_37);
+ convertible_sqrt_step(_split_37,&slow_it_down);
+ _split_45 = kh;
+ _split_46 = slow_it_down * _split_45;
+ _split_48 = _split_46 + _split_47;
+ Roof_Percent = _split_48;
+ break;
+ case convertible_wait:
+ Roof_Percent = 0.0;
+ break;
+}
+ Lustre_pre_2_set(Roof_Percent,&ctx->Lustre_pre_2_ctx_tab[0]);
+ break;
+}
+ Lustre_pre_2_get(&_split_39,&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_arrow_2_step(0.0,_split_39,&pRoof_Speed,&ctx->Lustre_arrow_2_ctx_tab[1]);
+ switch (Tick){
+ case _false:
+ _split_40 = pRoof_Speed;
+ *Roof_Speed = _split_40;
+ break;
+ case _true:
+ switch (st){
+ case convertible_fast:
+ _split_42 = 10.0;
+ break;
+ case convertible_slow:
+ _split_41 = 10.0 * slow_it_down;
+ _split_42 = _split_41;
+ break;
+ case convertible_wait:
+ _split_42 = 0.0;
+ break;
+}
+ *Roof_Speed = _split_42;
+ break;
+}
+ Lustre_pre_2_set(*Roof_Speed,&ctx->Lustre_pre_2_ctx_tab[1]);
+
+} // End of convertible_roof_speed_step
+
+// Step function(s) for convertible_solve_eq_d2_ctx
+void convertible_solve_eq_d2_step(_real a,_real b,_real c,_real *res){
+ _real _split_77;
+ _real _split_76;
+ _real _split_75;
+ _real _split_74;
+ _real _split_73;
+ _real _split_72;
+ _real _split_71;
+ _real _split_70;
+ _real _split_69;
+ _real _split_68;
+ _real _split_67;
+ _real _split_66;
+ _real _split_65;
+ _real _split_64;
+ _real _split_63;
+ _real _split_62;
+ _real _split_61;
+ _real _split_60;
+ _real _split_59;
+ _integer _split_58;
+ _integer _split_57;
+ _boolean _split_56;
+ _boolean _split_55;
+ _integer _split_54;
+ _boolean _split_53;
+ _boolean _split_52;
+ _real _split_51;
+ _real _split_50;
+ _real _split_49;
+ _real delta;
+ _integer sol_nb;
+ _real a2;
+ _real b2;
+ _real delta_pos;
+
+ _split_50 = 4.0 * a;
+ _split_51 = _split_50 * c;
+ _split_49 = b * b;
+ delta = _split_49 - _split_51;
+ _split_56 = delta == 0.0;
+ if (_split_56 == _true) {
+ _split_57 = convertible_one_sol;
+ } else {
+ _split_57 = convertible_two_sol;
+ }
+ _split_55 = delta < 0.0;
+ if (_split_55 == _true) {
+ _split_58 = convertible_no_sol;
+ } else {
+ _split_58 = _split_57;
+ }
+ _split_53 = b == 0.0;
+ if (_split_53 == _true) {
+ _split_54 = convertible_no_sol;
+ } else {
+ _split_54 = convertible_deg1;
+ }
+ _split_52 = a == 0.0;
+ if (_split_52 == _true) {
+ sol_nb = _split_54;
+ } else {
+ sol_nb = _split_58;
+ }
+ switch (sol_nb){
+ case convertible_two_sol:
+ delta_pos = delta;
+ a2 = a;
+ b2 = b;
+ convertible_sqrt_step(delta_pos,&_split_68);
+ _split_69 = 2.0 * a2;
+ Lustre_slash_step(_split_68,_split_69,&_split_70);
+ _split_67 = - b2;
+ _split_71 = _split_67 + _split_70;
+ convertible_sqrt_step(delta_pos,&_split_73);
+ _split_74 = 2.0 * a2;
+ Lustre_slash_step(_split_73,_split_74,&_split_75);
+ _split_72 = - b2;
+ _split_76 = _split_72 - _split_75;
+ convertible_maxr_step(_split_71,_split_76,&_split_77);
+ break;
+}
+ _split_63 = - b;
+ _split_64 = 2.0 * a;
+ Lustre_slash_step(_split_63,_split_64,&_split_65);
+ switch (sol_nb){
+ case convertible_one_sol:
+ _split_66 = _split_65;
+ break;
+}
+ _split_60 = - c;
+ Lustre_slash_step(_split_60,b,&_split_61);
+ switch (sol_nb){
+ case convertible_deg1:
+ _split_62 = _split_61;
+ *res = _split_62;
+ break;
+ case convertible_no_sol:
+ _split_59 = - 1.0;
+ *res = _split_59;
+ break;
+ case convertible_two_sol:
+ *res = _split_77;
+ break;
+ case convertible_one_sol:
+ *res = _split_66;
+ break;
+}
+
+} // End of convertible_solve_eq_d2_step
+
+// Memory initialisation for convertible_speed_kmh_ctx
+void convertible_speed_kmh_ctx_reset(convertible_speed_kmh_ctx_type* ctx){
+ int _i;
+
+ sum_50_0d1_ctx_reset(&ctx->sum_50_0d1_ctx_tab[0]);
+ sum_50_0d0_ctx_reset(&ctx->sum_50_0d0_ctx_tab[0]);
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[2]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[0]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[1]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[2]);
+}
+
+// Initialisation of the internal structure of convertible_speed_kmh_ctx
+void convertible_speed_kmh_ctx_init(convertible_speed_kmh_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_speed_kmh_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_speed_kmh_ctx
+void convertible_speed_kmh_step(_boolean Rot,_boolean Tick,_real *Speed,convertible_speed_kmh_ctx_type* ctx){ _real _split_89;
+ _real _split_88;
+ _real _split_87;
+ _real _split_86;
+ _real _split_85;
+ _real _split_84;
+ _real _split_83;
+ _real _split_82;
+ _real _split_81;
+ _real _split_80;
+ _real _split_79;
+ _real _split_78;
+ _real d;
+ _real t;
+ _real pd;
+ _real pt;
+ _real dx;
+ _real tx;
+ _boolean TickOrRot;
+
+ if (Rot == _true) {
+ dx = 1.4;
+ } else {
+ dx = 0.0;
+ }
+ if (Tick == _true) {
+ tx = 0.1;
+ } else {
+ tx = 0.0;
+ }
+ TickOrRot = Tick | Rot;
+ Lustre_pre_2_get(&_split_78,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_step(0.0,_split_78,&pd,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ switch (TickOrRot){
+ case _false:
+ _split_80 = pd;
+ d = _split_80;
+ break;
+ case _true:
+ _split_81 = dx;
+ sum_50_0d0_step(_split_81,&_split_82,&ctx->sum_50_0d0_ctx_tab[0]);
+ d = _split_82;
+ break;
+}
+ Lustre_pre_2_set(d,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_pre_2_get(&_split_79,&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_arrow_2_step(0.0,_split_79,&pt,&ctx->Lustre_arrow_2_ctx_tab[1]);
+ switch (TickOrRot){
+ case _false:
+ _split_85 = pt;
+ _split_86 = _split_85;
+ break;
+ case _true:
+ _split_83 = tx;
+ sum_50_0d1_step(_split_83,&_split_84,&ctx->sum_50_0d1_ctx_tab[0]);
+ _split_86 = _split_84;
+ break;
+}
+ convertible_maxr_step(0.1,_split_86,&t);
+ Lustre_pre_2_set(t,&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_pre_2_get(&_split_89,&ctx->Lustre_pre_2_ctx_tab[2]);
+ Lustre_slash_step(d,t,&_split_87);
+ convertible_ms_to_kmh_step(_split_87,&_split_88);
+ Lustre_pre_2_set(_split_88,&ctx->Lustre_pre_2_ctx_tab[2]);
+ Lustre_arrow_2_step(0.0,_split_89,Speed,&ctx->Lustre_arrow_2_ctx_tab[2]);
+
+} // End of convertible_speed_kmh_step
+
+// Step function(s) for convertible_sqrt_ctx
+void convertible_sqrt_step(_real R,_real *Sqrt){
+
+ squareR_5_step(R,1.0,Sqrt);
+
+} // End of convertible_sqrt_step
+
+// Memory initialisation for convertible_vehicle_ctx
+void convertible_vehicle_ctx_reset(convertible_vehicle_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of convertible_vehicle_ctx
+void convertible_vehicle_ctx_init(convertible_vehicle_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_vehicle_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_vehicle_ctx
+void convertible_vehicle_step(_boolean Start,_boolean Locked,_real Speed,_real Dist,_integer *st,convertible_vehicle_ctx_type* ctx){ _integer _split_149;
+ _boolean _split_148;
+ _boolean _split_147;
+ _integer _split_146;
+ _integer _split_145;
+ _boolean _split_144;
+ _boolean _split_143;
+ _boolean _split_142;
+ _integer _split_141;
+ _boolean _split_140;
+ _boolean _split_139;
+ _integer _split_138;
+ _integer pst;
+ _boolean ac_cond;
+
+ Lustre_pre_get(&_split_138,&ctx->Lustre_pre_ctx_tab[0]);
+ ac_cond = Speed >= 110.0;
+ switch (pst){
+ case convertible_anti_col:
+ _split_147 = ac_cond;
+ _split_148 = ! _split_147;
+ if (_split_148 == _true) {
+ _split_149 = convertible_run;
+ } else {
+ _split_149 = convertible_anti_col;
+ }
+ *st = _split_149;
+ break;
+}
+ _split_143 = Speed == 0.0;
+ switch (pst){
+ case convertible_run:
+ _split_144 = _split_143;
+ if (_split_144 == _true) {
+ _split_145 = convertible_stationnary;
+ } else {
+ _split_145 = convertible_run;
+ }
+ _split_142 = ac_cond;
+ if (_split_142 == _true) {
+ _split_146 = convertible_anti_col;
+ } else {
+ _split_146 = _split_145;
+ }
+ *st = _split_146;
+ break;
+}
+ _split_139 = Start & Locked;
+ switch (pst){
+ case convertible_stationnary:
+ _split_140 = _split_139;
+ if (_split_140 == _true) {
+ _split_141 = convertible_run;
+ } else {
+ _split_141 = convertible_stationnary;
+ }
+ *st = _split_141;
+ break;
+}
+ Lustre_pre_set(*st,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(convertible_stationnary,_split_138,&pst,&ctx->Lustre_arrow_ctx_tab[0]);
+
+} // End of convertible_vehicle_step
+
+// Step function(s) for fillred_update_cell_do_50_ctx
+void fillred_update_cell_do_50_step(convertible_update_acc acc,_real cell[50],convertible_update_acc *nacc,_real ncell[50]/*out*/){
+ int _i;
+ for (_i=0 ; _i<49 ; _i+=2){
+ update_cell_do_50_step(acc,cell[_i],&acc,&ncell[_i]);
+ update_cell_do_50_step(acc,cell[_i+1],&acc,&ncell[_i+1]);
+ }
+ *nacc = acc;
+
+} // End of fillred_update_cell_do_50_step
+
+// Step function(s) for red_rplus_50_real_ctx
+void red_rplus_50_real_step(_real i1,_real i2[50],_real *out){
+ int _i;
+ for (_i=0 ; _i<49 ; _i+=2){
+ i1 = i1 + i2[_i];
+ i1 = i1 + i2[_i+1];
+ }
+ *out = i1;
+
+} // End of red_rplus_50_real_step
+
+// Step function(s) for squareR_1_ctx
+void squareR_1_step(_real x,_real presqrt,_real *Sqrt){
+ _real _split_93;
+ _real _split_92;
+ _real _split_91;
+ _real _split_90;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_split_92);
+ _split_93 = presqrt + _split_92;
+ sqrt = 0.5 * _split_93;
+ _split_90 = presqrt - sqrt;
+ convertible_abs_step(_split_90,&_split_91);
+ ecart = _split_91 < 0.0005;
+ *Sqrt = sqrt;
+
+} // End of squareR_1_step
+
+// Step function(s) for squareR_2_ctx
+void squareR_2_step(_real x,_real presqrt,_real *Sqrt){
+ _real _split_101;
+ _real _split_100;
+ _real _split_99;
+ _real _split_98;
+ _real _split_97;
+ _real _split_96;
+ _real _split_95;
+ _real _split_94;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_split_96);
+ _split_97 = presqrt + _split_96;
+ sqrt = 0.5 * _split_97;
+ _split_94 = presqrt - sqrt;
+ convertible_abs_step(_split_94,&_split_95);
+ ecart = _split_95 < 0.0005;
+ switch (ecart){
+ case _true:
+ _split_101 = sqrt;
+ *Sqrt = _split_101;
+ break;
+ case _false:
+ _split_99 = sqrt;
+ _split_98 = x;
+ squareR_1_step(_split_98,_split_99,&_split_100);
+ *Sqrt = _split_100;
+ break;
+}
+
+} // End of squareR_2_step
+
+// Step function(s) for squareR_3_ctx
+void squareR_3_step(_real x,_real presqrt,_real *Sqrt){
+ _real _split_109;
+ _real _split_108;
+ _real _split_107;
+ _real _split_106;
+ _real _split_105;
+ _real _split_104;
+ _real _split_103;
+ _real _split_102;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_split_104);
+ _split_105 = presqrt + _split_104;
+ sqrt = 0.5 * _split_105;
+ _split_102 = presqrt - sqrt;
+ convertible_abs_step(_split_102,&_split_103);
+ ecart = _split_103 < 0.0005;
+ switch (ecart){
+ case _true:
+ _split_109 = sqrt;
+ *Sqrt = _split_109;
+ break;
+ case _false:
+ _split_107 = sqrt;
+ _split_106 = x;
+ squareR_2_step(_split_106,_split_107,&_split_108);
+ *Sqrt = _split_108;
+ break;
+}
+
+} // End of squareR_3_step
+
+// Step function(s) for squareR_4_ctx
+void squareR_4_step(_real x,_real presqrt,_real *Sqrt){
+ _real _split_117;
+ _real _split_116;
+ _real _split_115;
+ _real _split_114;
+ _real _split_113;
+ _real _split_112;
+ _real _split_111;
+ _real _split_110;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_split_112);
+ _split_113 = presqrt + _split_112;
+ sqrt = 0.5 * _split_113;
+ _split_110 = presqrt - sqrt;
+ convertible_abs_step(_split_110,&_split_111);
+ ecart = _split_111 < 0.0005;
+ switch (ecart){
+ case _true:
+ _split_117 = sqrt;
+ *Sqrt = _split_117;
+ break;
+ case _false:
+ _split_115 = sqrt;
+ _split_114 = x;
+ squareR_3_step(_split_114,_split_115,&_split_116);
+ *Sqrt = _split_116;
+ break;
+}
+
+} // End of squareR_4_step
+
+// Step function(s) for squareR_5_ctx
+void squareR_5_step(_real x,_real presqrt,_real *Sqrt){
+ _real _split_125;
+ _real _split_124;
+ _real _split_123;
+ _real _split_122;
+ _real _split_121;
+ _real _split_120;
+ _real _split_119;
+ _real _split_118;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_split_120);
+ _split_121 = presqrt + _split_120;
+ sqrt = 0.5 * _split_121;
+ _split_118 = presqrt - sqrt;
+ convertible_abs_step(_split_118,&_split_119);
+ ecart = _split_119 < 0.0005;
+ switch (ecart){
+ case _true:
+ _split_125 = sqrt;
+ *Sqrt = _split_125;
+ break;
+ case _false:
+ _split_123 = sqrt;
+ _split_122 = x;
+ squareR_4_step(_split_122,_split_123,&_split_124);
+ *Sqrt = _split_124;
+ break;
+}
+
+} // End of squareR_5_step
+
+// Memory initialisation for sum_50_0d0_ctx
+void sum_50_0d0_ctx_reset(sum_50_0d0_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of sum_50_0d0_ctx
+void sum_50_0d0_ctx_init(sum_50_0d0_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ sum_50_0d0_ctx_reset(ctx);
+ }
+// Step function(s) for sum_50_0d0_ctx
+void sum_50_0d0_step(_real s,_real *res,sum_50_0d0_ctx_type* ctx){ _integer _split_130;
+ _real _split_129[50];
+ _real _split_128[50];
+ _integer _split_127;
+ _integer _split_126;
+ _real a[50];
+ _real pre_a[50];
+ _integer i;
+
+ Lustre_pre_get(&_split_126,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(0,_split_126,&_split_127,&ctx->Lustre_arrow_ctx_tab[0]);
+ i = _split_127 + 1;
+ Lustre_pre_set(i,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_pre_3_get(_split_129,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_hat_step(0.0,_split_128);
+ Lustre_arrow_3_step(_split_128,_split_129,pre_a,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ _split_130 = i % 50;
+ assign_50_step(s,_split_130,pre_a,a);
+ Lustre_pre_3_set(a,&ctx->Lustre_pre_3_ctx_tab[0]);
+ red_rplus_50_real_step(0.0,a,res);
+
+} // End of sum_50_0d0_step
+
+// Memory initialisation for sum_50_0d1_ctx
+void sum_50_0d1_ctx_reset(sum_50_0d1_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+
+// Initialisation of the internal structure of sum_50_0d1_ctx
+void sum_50_0d1_ctx_init(sum_50_0d1_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ sum_50_0d1_ctx_reset(ctx);
+ }
+// Step function(s) for sum_50_0d1_ctx
+void sum_50_0d1_step(_real s,_real *res,sum_50_0d1_ctx_type* ctx){ _integer _split_135;
+ _real _split_134[50];
+ _real _split_133[50];
+ _integer _split_132;
+ _integer _split_131;
+ _real a[50];
+ _real pre_a[50];
+ _integer i;
+
+ Lustre_pre_get(&_split_131,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(0,_split_131,&_split_132,&ctx->Lustre_arrow_ctx_tab[0]);
+ i = _split_132 + 1;
+ Lustre_pre_set(i,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_pre_3_get(_split_134,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_hat_step(0.1,_split_133);
+ Lustre_arrow_3_step(_split_133,_split_134,pre_a,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ _split_135 = i % 50;
+ assign_50_step(s,_split_135,pre_a,a);
+ Lustre_pre_3_set(a,&ctx->Lustre_pre_3_ctx_tab[0]);
+ red_rplus_50_real_step(0.0,a,res);
+
+} // End of sum_50_0d1_step
+
+// Step function(s) for update_cell_do_50_ctx
+void update_cell_do_50_step(convertible_update_acc acc,_real cell,convertible_update_acc *nacc,_real *ncell){
+ _integer _split_137;
+ _boolean _split_136;
+
+ _split_136 = acc.i == acc.j;
+ if (_split_136 == _true) {
+ *ncell = acc.v;
+ } else {
+ *ncell = cell;
+ }
+ _split_137 = acc.i + 1;
+ nacc->i = _split_137;
+ nacc->j = acc.j;
+ nacc->v = acc.v;
+
+} // End of update_cell_do_50_step
+
diff --git a/test/monniaux/lustrev6-convertible-2cgc/convertible_main.h b/test/monniaux/lustrev6-convertible-2cgc/convertible_main.h
new file mode 100644
index 00000000..63b4ea90
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/convertible_main.h
@@ -0,0 +1,110 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "lustre_types.h"
+#include "lustre_consts.h"
+
+#ifndef _convertible_main_H_FILE
+#define _convertible_main_H_FILE
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_step(_integer ,_integer ,_integer *,Lustre_arrow_ctx_type*);
+
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx);
+void Lustre_arrow_2_ctx_init(Lustre_arrow_2_ctx_type* ctx);
+void Lustre_arrow_2_step(_real ,_real ,_real *,Lustre_arrow_2_ctx_type*);
+
+void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx);
+void Lustre_arrow_3_ctx_init(Lustre_arrow_3_ctx_type* ctx);
+void Lustre_arrow_3_step(_real [50],_real [50],_real [50]/*out*/,Lustre_arrow_3_ctx_type*);
+
+void Lustre_hat_step(_real ,_real [50]/*out*/);
+
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_get(_integer *,Lustre_pre_ctx_type*);
+
+void Lustre_pre_set(_integer ,Lustre_pre_ctx_type*);
+
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx);
+void Lustre_pre_2_ctx_init(Lustre_pre_2_ctx_type* ctx);
+void Lustre_pre_2_get(_real *,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_2_set(_real ,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx);
+void Lustre_pre_3_ctx_init(Lustre_pre_3_ctx_type* ctx);
+void Lustre_pre_3_get(_real [50]/*out*/,Lustre_pre_3_ctx_type*);
+
+void Lustre_pre_3_set(_real [50],Lustre_pre_3_ctx_type*);
+
+void Lustre_slash_step(_real ,_real ,_real *);
+
+void assign_50_step(_real ,_integer ,_real [50],_real [50]/*out*/);
+
+void convertible_abs_step(_real ,_real *);
+
+void convertible_braking_time_step(_real ,_real *);
+
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx);
+void convertible_main_ctx_init(convertible_main_ctx_type* ctx);
+void convertible_main_step(_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_real ,_boolean *,_boolean *,_real *,_real *,convertible_main_ctx_type*);
+
+void convertible_maxr_step(_real ,_real ,_real *);
+
+void convertible_may_collide_ctx_reset(convertible_may_collide_ctx_type* ctx);
+void convertible_may_collide_ctx_init(convertible_may_collide_ctx_type* ctx);
+void convertible_may_collide_step(_real ,_real ,_boolean *,convertible_may_collide_ctx_type*);
+
+void convertible_ms_to_kmh_step(_real ,_real *);
+
+void convertible_roof_ctx_reset(convertible_roof_ctx_type* ctx);
+void convertible_roof_ctx_init(convertible_roof_ctx_type* ctx);
+void convertible_roof_step(_boolean ,_boolean ,_boolean ,_boolean ,_boolean *,_real *,convertible_roof_ctx_type*);
+
+void convertible_roof_speed_ctx_reset(convertible_roof_speed_ctx_type* ctx);
+void convertible_roof_speed_ctx_init(convertible_roof_speed_ctx_type* ctx);
+void convertible_roof_speed_step(_boolean ,_real *,convertible_roof_speed_ctx_type*);
+
+void convertible_solve_eq_d2_step(_real ,_real ,_real ,_real *);
+
+void convertible_speed_kmh_ctx_reset(convertible_speed_kmh_ctx_type* ctx);
+void convertible_speed_kmh_ctx_init(convertible_speed_kmh_ctx_type* ctx);
+void convertible_speed_kmh_step(_boolean ,_boolean ,_real *,convertible_speed_kmh_ctx_type*);
+
+void convertible_sqrt_step(_real ,_real *);
+
+void convertible_vehicle_ctx_reset(convertible_vehicle_ctx_type* ctx);
+void convertible_vehicle_ctx_init(convertible_vehicle_ctx_type* ctx);
+void convertible_vehicle_step(_boolean ,_boolean ,_real ,_real ,_integer *,convertible_vehicle_ctx_type*);
+
+void fillred_update_cell_do_50_step(convertible_update_acc ,_real [50],convertible_update_acc *,_real [50]/*out*/);
+
+void red_rplus_50_real_step(_real ,_real [50],_real *);
+
+void squareR_1_step(_real ,_real ,_real *);
+
+void squareR_2_step(_real ,_real ,_real *);
+
+void squareR_3_step(_real ,_real ,_real *);
+
+void squareR_4_step(_real ,_real ,_real *);
+
+void squareR_5_step(_real ,_real ,_real *);
+
+void sum_50_0d0_ctx_reset(sum_50_0d0_ctx_type* ctx);
+void sum_50_0d0_ctx_init(sum_50_0d0_ctx_type* ctx);
+void sum_50_0d0_step(_real ,_real *,sum_50_0d0_ctx_type*);
+
+void sum_50_0d1_ctx_reset(sum_50_0d1_ctx_type* ctx);
+void sum_50_0d1_ctx_init(sum_50_0d1_ctx_type* ctx);
+void sum_50_0d1_step(_real ,_real *,sum_50_0d1_ctx_type*);
+
+void update_cell_do_50_step(convertible_update_acc ,_real ,convertible_update_acc *,_real *);
+
+/////////////////////////////////////////////////
+#endif
diff --git a/test/monniaux/lustrev6-convertible-2cgc/convertible_main_loop.c b/test/monniaux/lustrev6-convertible-2cgc/convertible_main_loop.c
new file mode 100644
index 00000000..9646b39f
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/convertible_main_loop.c
@@ -0,0 +1,86 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "convertible_main.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "1"
+#endif
+#ifndef FF
+#define FF "0"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_boolean _get_bool(char* n){
+ return dm_random_uint32() & 1;
+}
+/*
+_integer _get_int(char* n){
+ return (_integer) (dm_random_uint32() % 21) - 10;
+}
+*/
+_real _get_real(char* n){
+ return ((_integer) (dm_random_uint32() % 2000001) - 1000000)*1E-6;
+}
+/* Output procedures **********************/
+void convertible_main_O_n(void* cdata, _integer _V) {
+}
+
+/* Main procedure *************************/
+int main(){
+ int _s = 0;
+ _boolean Start;
+ _boolean Parked;
+ _boolean Rot;
+ _boolean Tick;
+ _boolean OnOff;
+ _boolean Done;
+ _real Dist;
+ _boolean Danger;
+ _boolean Locked;
+ _real Speed;
+ _real Roof_Speed;
+ convertible_main_ctx_type ctx_struct;
+ convertible_main_ctx_type* ctx = &ctx_struct;
+ convertible_main_ctx_init(ctx);
+ // printf("#inputs \"Start\":bool \"Parked\":bool \"Rot\":bool \"Tick\":bool \"OnOff\":bool \"Done\":bool \"Dist\":real\n");
+ // printf("#outputs \"Danger\":bool \"Locked\":bool \"Speed\":real \"Roof_Speed\":real\n");
+
+ /* Main loop */
+ clock_prepare();
+ clock_start();
+
+ for(int count=0; count<1000; count++){
+ ++_s;
+ Start = _get_bool("Start");
+ Parked = _get_bool("Parked");
+ Rot = _get_bool("Rot");
+ Tick = _get_bool("Tick");
+ OnOff = _get_bool("OnOff");
+ Done = _get_bool("Done");
+ Dist = _get_real("Dist");
+ convertible_main_step(Start,Parked,Rot,Tick,OnOff,Done,Dist,&Danger,&Locked,&Speed,&Roof_Speed,ctx);
+ // printf("%d %d %d %d %d %d %f #outs %d %d %f %f\n",Start,Parked,Rot,Tick,OnOff,Done,Dist,Danger,Locked,Speed,Roof_Speed);
+ // printf("%d %d %f %f\n",Danger,Locked,Speed,Roof_Speed);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+
+}
diff --git a/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.c b/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.c
new file mode 100644
index 00000000..925cbf0b
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.c
@@ -0,0 +1,4 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+#include "lustre_consts.h" \ No newline at end of file
diff --git a/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.h b/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.h
new file mode 100644
index 00000000..a9ba2005
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/lustre_consts.h
@@ -0,0 +1,23 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+
+// Constant definitions
+#define convertible_anti_col 2
+#define convertible_deg1 1
+#define convertible_fast 1
+#define convertible_in_motion 1
+#define convertible_k 5500.0
+#define convertible_locked 0
+#define convertible_max_roof_speed 10.0
+#define convertible_no_sol 0
+#define convertible_one_sol 2
+#define convertible_period 0.1
+#define convertible_run 1
+#define convertible_size 50
+#define convertible_slow 2
+#define convertible_speed_max 110.0
+#define convertible_stationnary 0
+#define convertible_two_sol 3
+#define convertible_wait 0
+#define convertible_wheel_girth 1.4
diff --git a/test/monniaux/lustrev6-convertible-2cgc/lustre_types.h b/test/monniaux/lustrev6-convertible-2cgc/lustre_types.h
new file mode 100644
index 00000000..83a1c722
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-2cgc/lustre_types.h
@@ -0,0 +1,139 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+
+#ifndef _SOC2C_PREDEF_TYPES
+#define _SOC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+// end of _SOC2C_PREDEF_TYPES
+// User typedef
+#ifndef _convertible_main_TYPES
+#define _convertible_main_TYPES
+typedef _integer convertible_eq_case;
+typedef _integer convertible_roof_speed_state;
+typedef _integer convertible_roof_state;
+typedef struct {
+ _integer i;
+ _integer j;
+ _real v;
+ } convertible_update_acc;
+typedef _integer convertible_vehicle_state;
+#endif // enf of _convertible_main_TYPES
+// Memoryless soc ctx typedef
+// Memoryfull soc ctx typedef
+/* Lustre_pre_ctx */
+typedef struct {
+ /*Memory cell*/
+ _integer _memory ;
+} Lustre_pre_ctx_type;
+
+/* Lustre_arrow_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_ctx_type;
+
+/* Lustre_pre_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory ;
+} Lustre_pre_2_ctx_type;
+
+/* Lustre_arrow_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_2_ctx_type;
+
+/* convertible_roof_speed_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[2];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[2];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} convertible_roof_speed_ctx_type;
+
+/* convertible_roof_ctx */
+typedef struct {
+ /*INSTANCES*/
+ convertible_roof_speed_ctx_type convertible_roof_speed_ctx_tab[1];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} convertible_roof_ctx_type;
+
+/* Lustre_pre_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory[50] ;
+} Lustre_pre_3_ctx_type;
+
+/* Lustre_arrow_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_3_ctx_type;
+
+/* sum_50_0d0_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[1];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} sum_50_0d0_ctx_type;
+
+/* sum_50_0d1_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[1];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} sum_50_0d1_ctx_type;
+
+/* convertible_speed_kmh_ctx */
+typedef struct {
+ /*INSTANCES*/
+ sum_50_0d1_ctx_type sum_50_0d1_ctx_tab[1];
+ sum_50_0d0_ctx_type sum_50_0d0_ctx_tab[1];
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[3];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[3];
+} convertible_speed_kmh_ctx_type;
+
+/* convertible_vehicle_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} convertible_vehicle_ctx_type;
+
+/* convertible_may_collide_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[1];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[1];
+} convertible_may_collide_ctx_type;
+
+/* convertible_main_ctx */
+typedef struct {
+ /*INSTANCES*/
+ convertible_vehicle_ctx_type convertible_vehicle_ctx_tab[1];
+ convertible_speed_kmh_ctx_type convertible_speed_kmh_ctx_tab[1];
+ convertible_roof_ctx_type convertible_roof_ctx_tab[1];
+ convertible_may_collide_ctx_type convertible_may_collide_ctx_tab[1];
+} convertible_main_ctx_type;
+
+// Defining array and extern types assignments
+
+#ifndef _assign_rp50
+#define _assign_rp50(dest, source, size) memcpy(dest, source, size)
+#endif
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/Makefile b/test/monniaux/lustrev6-convertible-en-2cgc/Makefile
new file mode 100644
index 00000000..009f7f35
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/Makefile
@@ -0,0 +1,3 @@
+TARGET=lv6-en-2cgc_convertible
+
+include ../rules.mk
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.c b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.c
new file mode 100644
index 00000000..6a4db4c3
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.c
@@ -0,0 +1,3319 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2c -en -2cgc -n main convertible.lus */
+/* on vanoise the 09/05/2019 at 15:28:26 */
+#include "convertible_main.h"
+
+#define DM_INLINE inline
+
+//// Defining step functions
+// Memory initialisation for Lustre_arrow_2_ctx
+DM_INLINE void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_2_ctx
+DM_INLINE void Lustre_arrow_2_ctx_init(Lustre_arrow_2_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_2_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_2_ctx
+DM_INLINE void Lustre_arrow_2_step(_integer i1,_integer i2,_integer *out,Lustre_arrow_2_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_2_step
+
+// Memory initialisation for Lustre_arrow_ctx
+DM_INLINE void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_ctx
+DM_INLINE void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_ctx
+DM_INLINE void Lustre_arrow_step(_real i1,_real i2,_real *out,Lustre_arrow_ctx_type* ctx){ *out = ((ctx->_memory)? i1 : i2);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_step
+
+// Memory initialisation for Lustre_arrow_3_ctx
+DM_INLINE void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+
+// Initialisation of the internal structure of Lustre_arrow_3_ctx
+DM_INLINE void Lustre_arrow_3_ctx_init(Lustre_arrow_3_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_arrow_3_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_arrow_3_ctx
+DM_INLINE void Lustre_arrow_3_step(_real i1[50],_real i2[50],_real out[50]/*out*/,Lustre_arrow_3_ctx_type* ctx){ _assign_rp50(out, ((ctx->_memory)? i1 : i2), sizeof(_real [50]));
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_3_step
+
+// Step function(s) for Lustre_hat_ctx
+DM_INLINE void Lustre_hat_step(_real i1,_real out[50]/*out*/){
+ out[0] = i1;
+ out[1] = i1;
+ out[2] = i1;
+ out[3] = i1;
+ out[4] = i1;
+ out[5] = i1;
+ out[6] = i1;
+ out[7] = i1;
+ out[8] = i1;
+ out[9] = i1;
+ out[10] = i1;
+ out[11] = i1;
+ out[12] = i1;
+ out[13] = i1;
+ out[14] = i1;
+ out[15] = i1;
+ out[16] = i1;
+ out[17] = i1;
+ out[18] = i1;
+ out[19] = i1;
+ out[20] = i1;
+ out[21] = i1;
+ out[22] = i1;
+ out[23] = i1;
+ out[24] = i1;
+ out[25] = i1;
+ out[26] = i1;
+ out[27] = i1;
+ out[28] = i1;
+ out[29] = i1;
+ out[30] = i1;
+ out[31] = i1;
+ out[32] = i1;
+ out[33] = i1;
+ out[34] = i1;
+ out[35] = i1;
+ out[36] = i1;
+ out[37] = i1;
+ out[38] = i1;
+ out[39] = i1;
+ out[40] = i1;
+ out[41] = i1;
+ out[42] = i1;
+ out[43] = i1;
+ out[44] = i1;
+ out[45] = i1;
+ out[46] = i1;
+ out[47] = i1;
+ out[48] = i1;
+ out[49] = i1;
+
+} // End of Lustre_hat_step
+
+// Memory initialisation for Lustre_pre_2_ctx
+DM_INLINE void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_2_ctx
+DM_INLINE void Lustre_pre_2_ctx_init(Lustre_pre_2_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_2_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_2_ctx
+DM_INLINE void Lustre_pre_2_get(_integer *out,Lustre_pre_2_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_2_get
+
+DM_INLINE void Lustre_pre_2_set(_integer i1,Lustre_pre_2_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_2_set
+
+// Memory initialisation for Lustre_pre_ctx
+DM_INLINE void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_ctx
+DM_INLINE void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_ctx
+DM_INLINE void Lustre_pre_get(_real *out,Lustre_pre_ctx_type* ctx){
+ *out = ctx->_memory;
+
+} // End of Lustre_pre_get
+
+DM_INLINE void Lustre_pre_set(_real i1,Lustre_pre_ctx_type* ctx){
+ ctx->_memory = i1;
+
+} // End of Lustre_pre_set
+
+// Memory initialisation for Lustre_pre_3_ctx
+DM_INLINE void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx){
+ int _i;
+
+}
+
+// Initialisation of the internal structure of Lustre_pre_3_ctx
+DM_INLINE void Lustre_pre_3_ctx_init(Lustre_pre_3_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ Lustre_pre_3_ctx_reset(ctx);
+ }
+// Step function(s) for Lustre_pre_3_ctx
+DM_INLINE void Lustre_pre_3_get(_real out[50]/*out*/,Lustre_pre_3_ctx_type* ctx){
+ _assign_rp50(out, ctx->_memory, sizeof(_real [50]));
+
+} // End of Lustre_pre_3_get
+
+DM_INLINE void Lustre_pre_3_set(_real i1[50],Lustre_pre_3_ctx_type* ctx){
+ _assign_rp50(ctx->_memory, i1, sizeof(_real [50]));
+
+} // End of Lustre_pre_3_set
+
+// Step function(s) for Lustre_slash_ctx
+DM_INLINE void Lustre_slash_step(_real i1,_real i2,_real *out){
+ *out = (i1 / i2);
+
+} // End of Lustre_slash_step
+
+// Memory initialisation for convertible_main_ctx
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[1]); for (_i=0 ; _i<6 ; _i+=1){
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[_i]);
+ } for (_i=0 ; _i<5 ; _i+=1){
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[_i]);
+ }
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[1]); for (_i=0 ; _i<6 ; _i+=1){
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[_i]);
+ } for (_i=0 ; _i<5 ; _i+=1){
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[_i]);
+ }
+}
+
+// Initialisation of the internal structure of convertible_main_ctx
+void convertible_main_ctx_init(convertible_main_ctx_type* ctx){
+ // ctx->client_data = cdata;
+ convertible_main_ctx_reset(ctx);
+ }
+// Step function(s) for convertible_main_ctx
+void convertible_main_step(_boolean Start,_boolean Parked,_boolean Rot,_boolean Tick,_boolean OnOff,_boolean Done,_real Dist,_boolean *Danger,_boolean *Locked,_real *Speed,_real *Roof_Speed,convertible_main_ctx_type* ctx){ _boolean _Tick_on_in_motion_1;
+ _integer _st_2;
+ _integer _pst_3;
+ _integer __split_18_1;
+ _boolean __split_19_1;
+ _boolean __split_20_1;
+ _integer __split_21_1;
+ _boolean __split_22_1;
+ _integer __split_23_1;
+ _real __split_24_1;
+ _real __split_25_1;
+ _real ____presqrt_5_4_1_1;
+ _boolean ______split_1_5_1_4_1_1;
+ _real ______split_2_5_1_4_1_1;
+ _boolean _____ecart_4_1_4_1_1;
+ _real _____sqrt_4_1_4_1_1;
+ _real ______split_110_1_1_4_1_1;
+ _real ______split_111_1_1_4_1_1;
+ _real ______split_112_1_1_4_1_1;
+ _real ______split_113_1_1_4_1_1;
+ _real ______split_114_1_1_4_1_1;
+ _real ______split_115_1_1_4_1_1;
+ _real ______split_116_1_1_4_1_1;
+ _real ______split_117_1_1_4_1_1;
+ _boolean ________split_1_3_1_1_1_4_1_1;
+ _real ________split_2_3_1_1_1_4_1_1;
+ _boolean _______ecart_2_1_1_1_4_1_1;
+ _real _______sqrt_2_1_1_1_4_1_1;
+ _real ________split_94_1_1_1_1_4_1_1;
+ _real ________split_95_1_1_1_1_4_1_1;
+ _real ________split_96_1_1_1_1_4_1_1;
+ _real ________split_97_1_1_1_1_4_1_1;
+ _real ________split_98_1_1_1_1_4_1_1;
+ _real ________split_99_1_1_1_1_4_1_1;
+ _real ________split_101_1_1_1_1_4_1_1;
+ _boolean __________split_1_1_1_1_1_1_1_4_1_1;
+ _real __________split_2_1_1_1_1_1_1_4_1_1;
+ _real _________split_93_1_1_1_1_1_4_1_1;
+ _real _________split_92_1_1_1_1_1_4_1_1;
+ _real _________split_91_1_1_1_1_1_4_1_1;
+ _real _________split_90_1_1_1_1_1_4_1_1;
+ _real ________sqrt_1_1_1_1_1_4_1_1;
+ _boolean ________ecart_1_1_1_1_1_4_1_1;
+ _real _________split_2_2_1_1_1_1_4_1_1;
+ _boolean _________split_1_2_1_1_1_1_4_1_1;
+ _real _______split_109_1_1_1_4_1_1;
+ _real _______split_108_1_1_1_4_1_1;
+ _real _______split_107_1_1_1_4_1_1;
+ _real _______split_106_1_1_1_4_1_1;
+ _real _______split_105_1_1_1_4_1_1;
+ _real _______split_104_1_1_1_4_1_1;
+ _real _______split_103_1_1_1_4_1_1;
+ _real _______split_102_1_1_1_4_1_1;
+ _real ______sqrt_3_1_1_4_1_1;
+ _boolean ______ecart_3_1_1_4_1_1;
+ _real _______split_2_4_1_1_4_1_1;
+ _boolean _______split_1_4_1_1_4_1_1;
+ _real _____split_125_1_4_1_1;
+ _real _____split_124_1_4_1_1;
+ _real _____split_123_1_4_1_1;
+ _real _____split_122_1_4_1_1;
+ _real _____split_121_1_4_1_1;
+ _real _____split_120_1_4_1_1;
+ _real _____split_119_1_4_1_1;
+ _real _____split_118_1_4_1_1;
+ _real ____sqrt_5_4_1_1;
+ _boolean ____ecart_5_4_1_1;
+ _real ____presqrt_5_3_1_1;
+ _boolean ______split_1_5_1_3_1_1;
+ _real ______split_2_5_1_3_1_1;
+ _boolean _____ecart_4_1_3_1_1;
+ _real _____sqrt_4_1_3_1_1;
+ _real ______split_110_1_1_3_1_1;
+ _real ______split_111_1_1_3_1_1;
+ _real ______split_112_1_1_3_1_1;
+ _real ______split_113_1_1_3_1_1;
+ _real ______split_114_1_1_3_1_1;
+ _real ______split_115_1_1_3_1_1;
+ _real ______split_116_1_1_3_1_1;
+ _real ______split_117_1_1_3_1_1;
+ _boolean ________split_1_3_1_1_1_3_1_1;
+ _real ________split_2_3_1_1_1_3_1_1;
+ _boolean _______ecart_2_1_1_1_3_1_1;
+ _real _______sqrt_2_1_1_1_3_1_1;
+ _real ________split_94_1_1_1_1_3_1_1;
+ _real ________split_95_1_1_1_1_3_1_1;
+ _real ________split_96_1_1_1_1_3_1_1;
+ _real ________split_97_1_1_1_1_3_1_1;
+ _real ________split_98_1_1_1_1_3_1_1;
+ _real ________split_99_1_1_1_1_3_1_1;
+ _real ________split_101_1_1_1_1_3_1_1;
+ _boolean __________split_1_1_1_1_1_1_1_3_1_1;
+ _real __________split_2_1_1_1_1_1_1_3_1_1;
+ _real _________split_93_1_1_1_1_1_3_1_1;
+ _real _________split_92_1_1_1_1_1_3_1_1;
+ _real _________split_91_1_1_1_1_1_3_1_1;
+ _real _________split_90_1_1_1_1_1_3_1_1;
+ _real ________sqrt_1_1_1_1_1_3_1_1;
+ _boolean ________ecart_1_1_1_1_1_3_1_1;
+ _real _________split_2_2_1_1_1_1_3_1_1;
+ _boolean _________split_1_2_1_1_1_1_3_1_1;
+ _real _______split_109_1_1_1_3_1_1;
+ _real _______split_108_1_1_1_3_1_1;
+ _real _______split_107_1_1_1_3_1_1;
+ _real _______split_106_1_1_1_3_1_1;
+ _real _______split_105_1_1_1_3_1_1;
+ _real _______split_104_1_1_1_3_1_1;
+ _real _______split_103_1_1_1_3_1_1;
+ _real _______split_102_1_1_1_3_1_1;
+ _real ______sqrt_3_1_1_3_1_1;
+ _boolean ______ecart_3_1_1_3_1_1;
+ _real _______split_2_4_1_1_3_1_1;
+ _boolean _______split_1_4_1_1_3_1_1;
+ _real _____split_125_1_3_1_1;
+ _real _____split_124_1_3_1_1;
+ _real _____split_123_1_3_1_1;
+ _real _____split_122_1_3_1_1;
+ _real _____split_121_1_3_1_1;
+ _real _____split_120_1_3_1_1;
+ _real _____split_119_1_3_1_1;
+ _real _____split_118_1_3_1_1;
+ _real ____sqrt_5_3_1_1;
+ _boolean ____ecart_5_3_1_1;
+ _real ___split_48_1_1;
+ _real ___split_47_1_1;
+ _real ___split_46_1_1;
+ _real ___split_45_1_1;
+ _real ___split_44_1_1;
+ _real ___split_43_1_1;
+ _real ___split_42_1_1;
+ _real ___split_41_1_1;
+ _real ___split_40_1_1;
+ _real ___split_39_1_1;
+ _real ___split_38_1_1;
+ _real ___split_37_1_1;
+ _real ___split_36_1_1;
+ _real ___split_35_1_1;
+ _real ___split_34_1_1;
+ _real ___split_33_1_1;
+ _integer ___split_32_1_1;
+ _boolean ___split_31_1_1;
+ _real ___split_30_1_1;
+ _integer ___split_29_1_1;
+ _boolean ___split_28_1_1;
+ _real ___split_27_1_1;
+ _integer ___split_26_1_1;
+ _integer __pst_2_1;
+ _integer __st_1_1;
+ _real __kh_1_1;
+ _real __Roof_Percent_1_1;
+ _real __pRoof_Percent_1_1;
+ _real __slow_it_down_1_1;
+ _real __pRoof_Speed_1_1;
+ _boolean _TickOrRot_1;
+ _real _tx_1;
+ _real _dx_1;
+ _real _pt_1;
+ _real _pd_1;
+ _real _t_3;
+ _real _d_1;
+ _real __split_78_1;
+ _real __split_79_1;
+ _real __split_80_1;
+ _real __split_81_1;
+ _real __split_82_1;
+ _real __split_83_1;
+ _real __split_84_1;
+ _real __split_85_1;
+ _real __split_86_1;
+ _real __split_87_1;
+ _real __split_88_1;
+ _real __split_89_1;
+ _real __x_13_1;
+ _boolean ___split_10_2_1;
+ convertible_update_acc ___dummy_1_1_1;
+ convertible_update_acc ____split_3_1_1_1;
+ _boolean ______split_136_50_1_1_1_1;
+ _integer ______split_137_50_1_1_1_1;
+ _real _____cell_50_1_1_1_1;
+ _real _____ncell_50_1_1_1_1;
+ _boolean ______split_136_49_1_1_1_1;
+ _integer ______split_137_49_1_1_1_1;
+ _real _____cell_49_1_1_1_1;
+ _real _____ncell_49_1_1_1_1;
+ _boolean ______split_136_48_1_1_1_1;
+ _integer ______split_137_48_1_1_1_1;
+ _real _____cell_48_1_1_1_1;
+ _real _____ncell_48_1_1_1_1;
+ _boolean ______split_136_47_1_1_1_1;
+ _integer ______split_137_47_1_1_1_1;
+ _real _____cell_47_1_1_1_1;
+ _real _____ncell_47_1_1_1_1;
+ _boolean ______split_136_46_1_1_1_1;
+ _integer ______split_137_46_1_1_1_1;
+ _real _____cell_46_1_1_1_1;
+ _real _____ncell_46_1_1_1_1;
+ _boolean ______split_136_45_1_1_1_1;
+ _integer ______split_137_45_1_1_1_1;
+ _real _____cell_45_1_1_1_1;
+ _real _____ncell_45_1_1_1_1;
+ _boolean ______split_136_44_1_1_1_1;
+ _integer ______split_137_44_1_1_1_1;
+ _real _____cell_44_1_1_1_1;
+ _real _____ncell_44_1_1_1_1;
+ _boolean ______split_136_43_1_1_1_1;
+ _integer ______split_137_43_1_1_1_1;
+ _real _____cell_43_1_1_1_1;
+ _real _____ncell_43_1_1_1_1;
+ _boolean ______split_136_42_1_1_1_1;
+ _integer ______split_137_42_1_1_1_1;
+ _real _____cell_42_1_1_1_1;
+ _real _____ncell_42_1_1_1_1;
+ _boolean ______split_136_41_1_1_1_1;
+ _integer ______split_137_41_1_1_1_1;
+ _real _____cell_41_1_1_1_1;
+ _real _____ncell_41_1_1_1_1;
+ _boolean ______split_136_40_1_1_1_1;
+ _integer ______split_137_40_1_1_1_1;
+ _real _____cell_40_1_1_1_1;
+ _real _____ncell_40_1_1_1_1;
+ _boolean ______split_136_39_1_1_1_1;
+ _integer ______split_137_39_1_1_1_1;
+ _real _____cell_39_1_1_1_1;
+ _real _____ncell_39_1_1_1_1;
+ _boolean ______split_136_38_1_1_1_1;
+ _integer ______split_137_38_1_1_1_1;
+ _real _____cell_38_1_1_1_1;
+ _real _____ncell_38_1_1_1_1;
+ _boolean ______split_136_37_1_1_1_1;
+ _integer ______split_137_37_1_1_1_1;
+ _real _____cell_37_1_1_1_1;
+ _real _____ncell_37_1_1_1_1;
+ _boolean ______split_136_36_1_1_1_1;
+ _integer ______split_137_36_1_1_1_1;
+ _real _____cell_36_1_1_1_1;
+ _real _____ncell_36_1_1_1_1;
+ _boolean ______split_136_35_1_1_1_1;
+ _integer ______split_137_35_1_1_1_1;
+ _real _____cell_35_1_1_1_1;
+ _real _____ncell_35_1_1_1_1;
+ _boolean ______split_136_34_1_1_1_1;
+ _integer ______split_137_34_1_1_1_1;
+ _real _____cell_34_1_1_1_1;
+ _real _____ncell_34_1_1_1_1;
+ _boolean ______split_136_33_1_1_1_1;
+ _integer ______split_137_33_1_1_1_1;
+ _real _____cell_33_1_1_1_1;
+ _real _____ncell_33_1_1_1_1;
+ _boolean ______split_136_32_1_1_1_1;
+ _integer ______split_137_32_1_1_1_1;
+ _real _____cell_32_1_1_1_1;
+ _real _____ncell_32_1_1_1_1;
+ _boolean ______split_136_31_1_1_1_1;
+ _integer ______split_137_31_1_1_1_1;
+ _real _____cell_31_1_1_1_1;
+ _real _____ncell_31_1_1_1_1;
+ _boolean ______split_136_30_1_1_1_1;
+ _integer ______split_137_30_1_1_1_1;
+ _real _____cell_30_1_1_1_1;
+ _real _____ncell_30_1_1_1_1;
+ _boolean ______split_136_29_1_1_1_1;
+ _integer ______split_137_29_1_1_1_1;
+ _real _____cell_29_1_1_1_1;
+ _real _____ncell_29_1_1_1_1;
+ _boolean ______split_136_28_1_1_1_1;
+ _integer ______split_137_28_1_1_1_1;
+ _real _____cell_28_1_1_1_1;
+ _real _____ncell_28_1_1_1_1;
+ _boolean ______split_136_27_1_1_1_1;
+ _integer ______split_137_27_1_1_1_1;
+ _real _____cell_27_1_1_1_1;
+ _real _____ncell_27_1_1_1_1;
+ _boolean ______split_136_26_1_1_1_1;
+ _integer ______split_137_26_1_1_1_1;
+ _real _____cell_26_1_1_1_1;
+ _real _____ncell_26_1_1_1_1;
+ _boolean ______split_136_25_1_1_1_1;
+ _integer ______split_137_25_1_1_1_1;
+ _real _____cell_25_1_1_1_1;
+ _real _____ncell_25_1_1_1_1;
+ _boolean ______split_136_24_1_1_1_1;
+ _integer ______split_137_24_1_1_1_1;
+ _real _____cell_24_1_1_1_1;
+ _real _____ncell_24_1_1_1_1;
+ _boolean ______split_136_23_1_1_1_1;
+ _integer ______split_137_23_1_1_1_1;
+ _real _____cell_23_1_1_1_1;
+ _real _____ncell_23_1_1_1_1;
+ _boolean ______split_136_22_1_1_1_1;
+ _integer ______split_137_22_1_1_1_1;
+ _real _____cell_22_1_1_1_1;
+ _real _____ncell_22_1_1_1_1;
+ _boolean ______split_136_21_1_1_1_1;
+ _integer ______split_137_21_1_1_1_1;
+ _real _____cell_21_1_1_1_1;
+ _real _____ncell_21_1_1_1_1;
+ _boolean ______split_136_20_1_1_1_1;
+ _integer ______split_137_20_1_1_1_1;
+ _real _____cell_20_1_1_1_1;
+ _real _____ncell_20_1_1_1_1;
+ _boolean ______split_136_19_1_1_1_1;
+ _integer ______split_137_19_1_1_1_1;
+ _real _____cell_19_1_1_1_1;
+ _real _____ncell_19_1_1_1_1;
+ _boolean ______split_136_18_1_1_1_1;
+ _integer ______split_137_18_1_1_1_1;
+ _real _____cell_18_1_1_1_1;
+ _real _____ncell_18_1_1_1_1;
+ _boolean ______split_136_17_1_1_1_1;
+ _integer ______split_137_17_1_1_1_1;
+ _real _____cell_17_1_1_1_1;
+ _real _____ncell_17_1_1_1_1;
+ _boolean ______split_136_16_1_1_1_1;
+ _integer ______split_137_16_1_1_1_1;
+ _real _____cell_16_1_1_1_1;
+ _real _____ncell_16_1_1_1_1;
+ _boolean ______split_136_15_1_1_1_1;
+ _integer ______split_137_15_1_1_1_1;
+ _real _____cell_15_1_1_1_1;
+ _real _____ncell_15_1_1_1_1;
+ _boolean ______split_136_14_1_1_1_1;
+ _integer ______split_137_14_1_1_1_1;
+ _real _____cell_14_1_1_1_1;
+ _real _____ncell_14_1_1_1_1;
+ _boolean ______split_136_13_1_1_1_1;
+ _integer ______split_137_13_1_1_1_1;
+ _real _____cell_13_1_1_1_1;
+ _real _____ncell_13_1_1_1_1;
+ _boolean ______split_136_12_1_1_1_1;
+ _integer ______split_137_12_1_1_1_1;
+ _real _____cell_12_1_1_1_1;
+ _real _____ncell_12_1_1_1_1;
+ _boolean ______split_136_11_1_1_1_1;
+ _integer ______split_137_11_1_1_1_1;
+ _real _____cell_11_1_1_1_1;
+ _real _____ncell_11_1_1_1_1;
+ _boolean ______split_136_10_1_1_1_1;
+ _integer ______split_137_10_1_1_1_1;
+ _real _____cell_10_1_1_1_1;
+ _real _____ncell_10_1_1_1_1;
+ _boolean ______split_136_9_1_1_1_1;
+ _integer ______split_137_9_1_1_1_1;
+ _real _____cell_9_1_1_1_1;
+ _real _____ncell_9_1_1_1_1;
+ _boolean ______split_136_8_1_1_1_1;
+ _integer ______split_137_8_1_1_1_1;
+ _real _____cell_8_1_1_1_1;
+ _real _____ncell_8_1_1_1_1;
+ _boolean ______split_136_7_1_1_1_1;
+ _integer ______split_137_7_1_1_1_1;
+ _real _____cell_7_1_1_1_1;
+ _real _____ncell_7_1_1_1_1;
+ _boolean ______split_136_6_1_1_1_1;
+ _integer ______split_137_6_1_1_1_1;
+ _real _____cell_6_1_1_1_1;
+ _real _____ncell_6_1_1_1_1;
+ _boolean ______split_136_5_1_1_1_1;
+ _integer ______split_137_5_1_1_1_1;
+ _real _____cell_5_1_1_1_1;
+ _real _____ncell_5_1_1_1_1;
+ _boolean ______split_136_4_1_1_1_1;
+ _integer ______split_137_4_1_1_1_1;
+ _real _____cell_4_1_1_1_1;
+ _real _____ncell_4_1_1_1_1;
+ _boolean ______split_136_3_1_1_1_1;
+ _integer ______split_137_3_1_1_1_1;
+ _real _____cell_3_1_1_1_1;
+ _real _____ncell_3_1_1_1_1;
+ _boolean ______split_136_2_1_1_1_1;
+ _integer ______split_137_2_1_1_1_1;
+ _real _____cell_2_1_1_1_1;
+ _real _____ncell_2_1_1_1_1;
+ _boolean ______split_136_1_1_1_1_1;
+ _integer ______split_137_1_1_1_1_1;
+ _real _____cell_1_1_1_1_1;
+ _real _____ncell_1_1_1_1_1;
+ convertible_update_acc _____acc_1_1_1_1_1;
+ convertible_update_acc _____acc_2_1_1_1_1;
+ convertible_update_acc _____acc_3_1_1_1_1;
+ convertible_update_acc _____acc_4_1_1_1_1;
+ convertible_update_acc _____acc_5_1_1_1_1;
+ convertible_update_acc _____acc_6_1_1_1_1;
+ convertible_update_acc _____acc_7_1_1_1_1;
+ convertible_update_acc _____acc_8_1_1_1_1;
+ convertible_update_acc _____acc_9_1_1_1_1;
+ convertible_update_acc _____acc_10_1_1_1_1;
+ convertible_update_acc _____acc_11_1_1_1_1;
+ convertible_update_acc _____acc_12_1_1_1_1;
+ convertible_update_acc _____acc_13_1_1_1_1;
+ convertible_update_acc _____acc_14_1_1_1_1;
+ convertible_update_acc _____acc_15_1_1_1_1;
+ convertible_update_acc _____acc_16_1_1_1_1;
+ convertible_update_acc _____acc_17_1_1_1_1;
+ convertible_update_acc _____acc_18_1_1_1_1;
+ convertible_update_acc _____acc_19_1_1_1_1;
+ convertible_update_acc _____acc_20_1_1_1_1;
+ convertible_update_acc _____acc_21_1_1_1_1;
+ convertible_update_acc _____acc_22_1_1_1_1;
+ convertible_update_acc _____acc_23_1_1_1_1;
+ convertible_update_acc _____acc_24_1_1_1_1;
+ convertible_update_acc _____acc_25_1_1_1_1;
+ convertible_update_acc _____acc_26_1_1_1_1;
+ convertible_update_acc _____acc_27_1_1_1_1;
+ convertible_update_acc _____acc_28_1_1_1_1;
+ convertible_update_acc _____acc_29_1_1_1_1;
+ convertible_update_acc _____acc_30_1_1_1_1;
+ convertible_update_acc _____acc_31_1_1_1_1;
+ convertible_update_acc _____acc_32_1_1_1_1;
+ convertible_update_acc _____acc_33_1_1_1_1;
+ convertible_update_acc _____acc_34_1_1_1_1;
+ convertible_update_acc _____acc_35_1_1_1_1;
+ convertible_update_acc _____acc_36_1_1_1_1;
+ convertible_update_acc _____acc_37_1_1_1_1;
+ convertible_update_acc _____acc_38_1_1_1_1;
+ convertible_update_acc _____acc_39_1_1_1_1;
+ convertible_update_acc _____acc_40_1_1_1_1;
+ convertible_update_acc _____acc_41_1_1_1_1;
+ convertible_update_acc _____acc_42_1_1_1_1;
+ convertible_update_acc _____acc_43_1_1_1_1;
+ convertible_update_acc _____acc_44_1_1_1_1;
+ convertible_update_acc _____acc_45_1_1_1_1;
+ convertible_update_acc _____acc_46_1_1_1_1;
+ convertible_update_acc _____acc_47_1_1_1_1;
+ convertible_update_acc _____acc_48_1_1_1_1;
+ convertible_update_acc _____acc_49_1_1_1_1;
+ _real ____acc_98_1_1_1;
+ _real ____acc_97_1_1_1;
+ _real ____acc_96_1_1_1;
+ _real ____acc_95_1_1_1;
+ _real ____acc_94_1_1_1;
+ _real ____acc_93_1_1_1;
+ _real ____acc_92_1_1_1;
+ _real ____acc_91_1_1_1;
+ _real ____acc_90_1_1_1;
+ _real ____acc_89_1_1_1;
+ _real ____acc_88_1_1_1;
+ _real ____acc_87_1_1_1;
+ _real ____acc_86_1_1_1;
+ _real ____acc_85_1_1_1;
+ _real ____acc_84_1_1_1;
+ _real ____acc_83_1_1_1;
+ _real ____acc_82_1_1_1;
+ _real ____acc_81_1_1_1;
+ _real ____acc_80_1_1_1;
+ _real ____acc_79_1_1_1;
+ _real ____acc_78_1_1_1;
+ _real ____acc_77_1_1_1;
+ _real ____acc_76_1_1_1;
+ _real ____acc_75_1_1_1;
+ _real ____acc_74_1_1_1;
+ _real ____acc_73_1_1_1;
+ _real ____acc_72_1_1_1;
+ _real ____acc_71_1_1_1;
+ _real ____acc_70_1_1_1;
+ _real ____acc_69_1_1_1;
+ _real ____acc_68_1_1_1;
+ _real ____acc_67_1_1_1;
+ _real ____acc_66_1_1_1;
+ _real ____acc_65_1_1_1;
+ _real ____acc_64_1_1_1;
+ _real ____acc_63_1_1_1;
+ _real ____acc_62_1_1_1;
+ _real ____acc_61_1_1_1;
+ _real ____acc_60_1_1_1;
+ _real ____acc_59_1_1_1;
+ _real ____acc_58_1_1_1;
+ _real ____acc_57_1_1_1;
+ _real ____acc_56_1_1_1;
+ _real ____acc_55_1_1_1;
+ _real ____acc_54_1_1_1;
+ _real ____acc_53_1_1_1;
+ _real ____acc_52_1_1_1;
+ _real ____acc_51_1_1_1;
+ _real ____acc_50_1_1_1;
+ _real ___i1_1_1_1;
+ _integer ___split_135_1_1;
+ _real ___split_134_1_1[50];
+ _real ___split_133_1_1[50];
+ _integer ___split_132_1_1;
+ _integer ___split_131_1_1;
+ _real __a_2_1[50];
+ _real __pre_a_1_1[50];
+ _integer __i_1_1;
+ convertible_update_acc ___dummy_2_1_1;
+ convertible_update_acc ____split_3_2_1_1;
+ _boolean ______split_136_50_1_2_1_1;
+ _integer ______split_137_50_1_2_1_1;
+ _real _____cell_50_1_2_1_1;
+ _real _____ncell_50_1_2_1_1;
+ _boolean ______split_136_49_1_2_1_1;
+ _integer ______split_137_49_1_2_1_1;
+ _real _____cell_49_1_2_1_1;
+ _real _____ncell_49_1_2_1_1;
+ _boolean ______split_136_48_1_2_1_1;
+ _integer ______split_137_48_1_2_1_1;
+ _real _____cell_48_1_2_1_1;
+ _real _____ncell_48_1_2_1_1;
+ _boolean ______split_136_47_1_2_1_1;
+ _integer ______split_137_47_1_2_1_1;
+ _real _____cell_47_1_2_1_1;
+ _real _____ncell_47_1_2_1_1;
+ _boolean ______split_136_46_1_2_1_1;
+ _integer ______split_137_46_1_2_1_1;
+ _real _____cell_46_1_2_1_1;
+ _real _____ncell_46_1_2_1_1;
+ _boolean ______split_136_45_1_2_1_1;
+ _integer ______split_137_45_1_2_1_1;
+ _real _____cell_45_1_2_1_1;
+ _real _____ncell_45_1_2_1_1;
+ _boolean ______split_136_44_1_2_1_1;
+ _integer ______split_137_44_1_2_1_1;
+ _real _____cell_44_1_2_1_1;
+ _real _____ncell_44_1_2_1_1;
+ _boolean ______split_136_43_1_2_1_1;
+ _integer ______split_137_43_1_2_1_1;
+ _real _____cell_43_1_2_1_1;
+ _real _____ncell_43_1_2_1_1;
+ _boolean ______split_136_42_1_2_1_1;
+ _integer ______split_137_42_1_2_1_1;
+ _real _____cell_42_1_2_1_1;
+ _real _____ncell_42_1_2_1_1;
+ _boolean ______split_136_41_1_2_1_1;
+ _integer ______split_137_41_1_2_1_1;
+ _real _____cell_41_1_2_1_1;
+ _real _____ncell_41_1_2_1_1;
+ _boolean ______split_136_40_1_2_1_1;
+ _integer ______split_137_40_1_2_1_1;
+ _real _____cell_40_1_2_1_1;
+ _real _____ncell_40_1_2_1_1;
+ _boolean ______split_136_39_1_2_1_1;
+ _integer ______split_137_39_1_2_1_1;
+ _real _____cell_39_1_2_1_1;
+ _real _____ncell_39_1_2_1_1;
+ _boolean ______split_136_38_1_2_1_1;
+ _integer ______split_137_38_1_2_1_1;
+ _real _____cell_38_1_2_1_1;
+ _real _____ncell_38_1_2_1_1;
+ _boolean ______split_136_37_1_2_1_1;
+ _integer ______split_137_37_1_2_1_1;
+ _real _____cell_37_1_2_1_1;
+ _real _____ncell_37_1_2_1_1;
+ _boolean ______split_136_36_1_2_1_1;
+ _integer ______split_137_36_1_2_1_1;
+ _real _____cell_36_1_2_1_1;
+ _real _____ncell_36_1_2_1_1;
+ _boolean ______split_136_35_1_2_1_1;
+ _integer ______split_137_35_1_2_1_1;
+ _real _____cell_35_1_2_1_1;
+ _real _____ncell_35_1_2_1_1;
+ _boolean ______split_136_34_1_2_1_1;
+ _integer ______split_137_34_1_2_1_1;
+ _real _____cell_34_1_2_1_1;
+ _real _____ncell_34_1_2_1_1;
+ _boolean ______split_136_33_1_2_1_1;
+ _integer ______split_137_33_1_2_1_1;
+ _real _____cell_33_1_2_1_1;
+ _real _____ncell_33_1_2_1_1;
+ _boolean ______split_136_32_1_2_1_1;
+ _integer ______split_137_32_1_2_1_1;
+ _real _____cell_32_1_2_1_1;
+ _real _____ncell_32_1_2_1_1;
+ _boolean ______split_136_31_1_2_1_1;
+ _integer ______split_137_31_1_2_1_1;
+ _real _____cell_31_1_2_1_1;
+ _real _____ncell_31_1_2_1_1;
+ _boolean ______split_136_30_1_2_1_1;
+ _integer ______split_137_30_1_2_1_1;
+ _real _____cell_30_1_2_1_1;
+ _real _____ncell_30_1_2_1_1;
+ _boolean ______split_136_29_1_2_1_1;
+ _integer ______split_137_29_1_2_1_1;
+ _real _____cell_29_1_2_1_1;
+ _real _____ncell_29_1_2_1_1;
+ _boolean ______split_136_28_1_2_1_1;
+ _integer ______split_137_28_1_2_1_1;
+ _real _____cell_28_1_2_1_1;
+ _real _____ncell_28_1_2_1_1;
+ _boolean ______split_136_27_1_2_1_1;
+ _integer ______split_137_27_1_2_1_1;
+ _real _____cell_27_1_2_1_1;
+ _real _____ncell_27_1_2_1_1;
+ _boolean ______split_136_26_1_2_1_1;
+ _integer ______split_137_26_1_2_1_1;
+ _real _____cell_26_1_2_1_1;
+ _real _____ncell_26_1_2_1_1;
+ _boolean ______split_136_25_1_2_1_1;
+ _integer ______split_137_25_1_2_1_1;
+ _real _____cell_25_1_2_1_1;
+ _real _____ncell_25_1_2_1_1;
+ _boolean ______split_136_24_1_2_1_1;
+ _integer ______split_137_24_1_2_1_1;
+ _real _____cell_24_1_2_1_1;
+ _real _____ncell_24_1_2_1_1;
+ _boolean ______split_136_23_1_2_1_1;
+ _integer ______split_137_23_1_2_1_1;
+ _real _____cell_23_1_2_1_1;
+ _real _____ncell_23_1_2_1_1;
+ _boolean ______split_136_22_1_2_1_1;
+ _integer ______split_137_22_1_2_1_1;
+ _real _____cell_22_1_2_1_1;
+ _real _____ncell_22_1_2_1_1;
+ _boolean ______split_136_21_1_2_1_1;
+ _integer ______split_137_21_1_2_1_1;
+ _real _____cell_21_1_2_1_1;
+ _real _____ncell_21_1_2_1_1;
+ _boolean ______split_136_20_1_2_1_1;
+ _integer ______split_137_20_1_2_1_1;
+ _real _____cell_20_1_2_1_1;
+ _real _____ncell_20_1_2_1_1;
+ _boolean ______split_136_19_1_2_1_1;
+ _integer ______split_137_19_1_2_1_1;
+ _real _____cell_19_1_2_1_1;
+ _real _____ncell_19_1_2_1_1;
+ _boolean ______split_136_18_1_2_1_1;
+ _integer ______split_137_18_1_2_1_1;
+ _real _____cell_18_1_2_1_1;
+ _real _____ncell_18_1_2_1_1;
+ _boolean ______split_136_17_1_2_1_1;
+ _integer ______split_137_17_1_2_1_1;
+ _real _____cell_17_1_2_1_1;
+ _real _____ncell_17_1_2_1_1;
+ _boolean ______split_136_16_1_2_1_1;
+ _integer ______split_137_16_1_2_1_1;
+ _real _____cell_16_1_2_1_1;
+ _real _____ncell_16_1_2_1_1;
+ _boolean ______split_136_15_1_2_1_1;
+ _integer ______split_137_15_1_2_1_1;
+ _real _____cell_15_1_2_1_1;
+ _real _____ncell_15_1_2_1_1;
+ _boolean ______split_136_14_1_2_1_1;
+ _integer ______split_137_14_1_2_1_1;
+ _real _____cell_14_1_2_1_1;
+ _real _____ncell_14_1_2_1_1;
+ _boolean ______split_136_13_1_2_1_1;
+ _integer ______split_137_13_1_2_1_1;
+ _real _____cell_13_1_2_1_1;
+ _real _____ncell_13_1_2_1_1;
+ _boolean ______split_136_12_1_2_1_1;
+ _integer ______split_137_12_1_2_1_1;
+ _real _____cell_12_1_2_1_1;
+ _real _____ncell_12_1_2_1_1;
+ _boolean ______split_136_11_1_2_1_1;
+ _integer ______split_137_11_1_2_1_1;
+ _real _____cell_11_1_2_1_1;
+ _real _____ncell_11_1_2_1_1;
+ _boolean ______split_136_10_1_2_1_1;
+ _integer ______split_137_10_1_2_1_1;
+ _real _____cell_10_1_2_1_1;
+ _real _____ncell_10_1_2_1_1;
+ _boolean ______split_136_9_1_2_1_1;
+ _integer ______split_137_9_1_2_1_1;
+ _real _____cell_9_1_2_1_1;
+ _real _____ncell_9_1_2_1_1;
+ _boolean ______split_136_8_1_2_1_1;
+ _integer ______split_137_8_1_2_1_1;
+ _real _____cell_8_1_2_1_1;
+ _real _____ncell_8_1_2_1_1;
+ _boolean ______split_136_7_1_2_1_1;
+ _integer ______split_137_7_1_2_1_1;
+ _real _____cell_7_1_2_1_1;
+ _real _____ncell_7_1_2_1_1;
+ _boolean ______split_136_6_1_2_1_1;
+ _integer ______split_137_6_1_2_1_1;
+ _real _____cell_6_1_2_1_1;
+ _real _____ncell_6_1_2_1_1;
+ _boolean ______split_136_5_1_2_1_1;
+ _integer ______split_137_5_1_2_1_1;
+ _real _____cell_5_1_2_1_1;
+ _real _____ncell_5_1_2_1_1;
+ _boolean ______split_136_4_1_2_1_1;
+ _integer ______split_137_4_1_2_1_1;
+ _real _____cell_4_1_2_1_1;
+ _real _____ncell_4_1_2_1_1;
+ _boolean ______split_136_3_1_2_1_1;
+ _integer ______split_137_3_1_2_1_1;
+ _real _____cell_3_1_2_1_1;
+ _real _____ncell_3_1_2_1_1;
+ _boolean ______split_136_2_1_2_1_1;
+ _integer ______split_137_2_1_2_1_1;
+ _real _____cell_2_1_2_1_1;
+ _real _____ncell_2_1_2_1_1;
+ _boolean ______split_136_1_1_2_1_1;
+ _integer ______split_137_1_1_2_1_1;
+ _real _____cell_1_1_2_1_1;
+ _real _____ncell_1_1_2_1_1;
+ convertible_update_acc _____acc_1_1_2_1_1;
+ convertible_update_acc _____acc_2_1_2_1_1;
+ convertible_update_acc _____acc_3_1_2_1_1;
+ convertible_update_acc _____acc_4_1_2_1_1;
+ convertible_update_acc _____acc_5_1_2_1_1;
+ convertible_update_acc _____acc_6_1_2_1_1;
+ convertible_update_acc _____acc_7_1_2_1_1;
+ convertible_update_acc _____acc_8_1_2_1_1;
+ convertible_update_acc _____acc_9_1_2_1_1;
+ convertible_update_acc _____acc_10_1_2_1_1;
+ convertible_update_acc _____acc_11_1_2_1_1;
+ convertible_update_acc _____acc_12_1_2_1_1;
+ convertible_update_acc _____acc_13_1_2_1_1;
+ convertible_update_acc _____acc_14_1_2_1_1;
+ convertible_update_acc _____acc_15_1_2_1_1;
+ convertible_update_acc _____acc_16_1_2_1_1;
+ convertible_update_acc _____acc_17_1_2_1_1;
+ convertible_update_acc _____acc_18_1_2_1_1;
+ convertible_update_acc _____acc_19_1_2_1_1;
+ convertible_update_acc _____acc_20_1_2_1_1;
+ convertible_update_acc _____acc_21_1_2_1_1;
+ convertible_update_acc _____acc_22_1_2_1_1;
+ convertible_update_acc _____acc_23_1_2_1_1;
+ convertible_update_acc _____acc_24_1_2_1_1;
+ convertible_update_acc _____acc_25_1_2_1_1;
+ convertible_update_acc _____acc_26_1_2_1_1;
+ convertible_update_acc _____acc_27_1_2_1_1;
+ convertible_update_acc _____acc_28_1_2_1_1;
+ convertible_update_acc _____acc_29_1_2_1_1;
+ convertible_update_acc _____acc_30_1_2_1_1;
+ convertible_update_acc _____acc_31_1_2_1_1;
+ convertible_update_acc _____acc_32_1_2_1_1;
+ convertible_update_acc _____acc_33_1_2_1_1;
+ convertible_update_acc _____acc_34_1_2_1_1;
+ convertible_update_acc _____acc_35_1_2_1_1;
+ convertible_update_acc _____acc_36_1_2_1_1;
+ convertible_update_acc _____acc_37_1_2_1_1;
+ convertible_update_acc _____acc_38_1_2_1_1;
+ convertible_update_acc _____acc_39_1_2_1_1;
+ convertible_update_acc _____acc_40_1_2_1_1;
+ convertible_update_acc _____acc_41_1_2_1_1;
+ convertible_update_acc _____acc_42_1_2_1_1;
+ convertible_update_acc _____acc_43_1_2_1_1;
+ convertible_update_acc _____acc_44_1_2_1_1;
+ convertible_update_acc _____acc_45_1_2_1_1;
+ convertible_update_acc _____acc_46_1_2_1_1;
+ convertible_update_acc _____acc_47_1_2_1_1;
+ convertible_update_acc _____acc_48_1_2_1_1;
+ convertible_update_acc _____acc_49_1_2_1_1;
+ _real ____acc_98_2_1_1;
+ _real ____acc_97_2_1_1;
+ _real ____acc_96_2_1_1;
+ _real ____acc_95_2_1_1;
+ _real ____acc_94_2_1_1;
+ _real ____acc_93_2_1_1;
+ _real ____acc_92_2_1_1;
+ _real ____acc_91_2_1_1;
+ _real ____acc_90_2_1_1;
+ _real ____acc_89_2_1_1;
+ _real ____acc_88_2_1_1;
+ _real ____acc_87_2_1_1;
+ _real ____acc_86_2_1_1;
+ _real ____acc_85_2_1_1;
+ _real ____acc_84_2_1_1;
+ _real ____acc_83_2_1_1;
+ _real ____acc_82_2_1_1;
+ _real ____acc_81_2_1_1;
+ _real ____acc_80_2_1_1;
+ _real ____acc_79_2_1_1;
+ _real ____acc_78_2_1_1;
+ _real ____acc_77_2_1_1;
+ _real ____acc_76_2_1_1;
+ _real ____acc_75_2_1_1;
+ _real ____acc_74_2_1_1;
+ _real ____acc_73_2_1_1;
+ _real ____acc_72_2_1_1;
+ _real ____acc_71_2_1_1;
+ _real ____acc_70_2_1_1;
+ _real ____acc_69_2_1_1;
+ _real ____acc_68_2_1_1;
+ _real ____acc_67_2_1_1;
+ _real ____acc_66_2_1_1;
+ _real ____acc_65_2_1_1;
+ _real ____acc_64_2_1_1;
+ _real ____acc_63_2_1_1;
+ _real ____acc_62_2_1_1;
+ _real ____acc_61_2_1_1;
+ _real ____acc_60_2_1_1;
+ _real ____acc_59_2_1_1;
+ _real ____acc_58_2_1_1;
+ _real ____acc_57_2_1_1;
+ _real ____acc_56_2_1_1;
+ _real ____acc_55_2_1_1;
+ _real ____acc_54_2_1_1;
+ _real ____acc_53_2_1_1;
+ _real ____acc_52_2_1_1;
+ _real ____acc_51_2_1_1;
+ _real ____acc_50_2_1_1;
+ _real ___i1_2_1_1;
+ _integer ___split_130_1_1;
+ _real ___split_129_1_1[50];
+ _real ___split_128_1_1[50];
+ _integer ___split_127_1_1;
+ _integer ___split_126_1_1;
+ _real __a_3_1[50];
+ _real __pre_a_2_1[50];
+ _integer __i_2_1;
+ _boolean _ac_cond_1;
+ _integer _pst_1;
+ _integer __split_138_1;
+ _boolean __split_139_1;
+ _boolean __split_140_1;
+ _integer __split_141_1;
+ _boolean __split_142_1;
+ _boolean __split_143_1;
+ _boolean __split_144_1;
+ _integer __split_145_1;
+ _integer __split_146_1;
+ _boolean __split_147_1;
+ _boolean __split_148_1;
+ _integer __split_149_1;
+ _real _tBrake_1;
+ _real _tChoc_1;
+ _real _Accel_1;
+ _real __split_11_1;
+ _real __split_12_1;
+ _real __split_13_1;
+ _real __split_14_1;
+ _real __split_15_1;
+ _real __split_16_1;
+ _real __split_17_1;
+ _boolean ____split_10_1_1_1;
+ _real ____presqrt_5_2_1_1;
+ _boolean ______split_1_5_1_2_1_1;
+ _real ______split_2_5_1_2_1_1;
+ _boolean _____ecart_4_1_2_1_1;
+ _real _____sqrt_4_1_2_1_1;
+ _real ______split_110_1_1_2_1_1;
+ _real ______split_111_1_1_2_1_1;
+ _real ______split_112_1_1_2_1_1;
+ _real ______split_113_1_1_2_1_1;
+ _real ______split_114_1_1_2_1_1;
+ _real ______split_115_1_1_2_1_1;
+ _real ______split_116_1_1_2_1_1;
+ _real ______split_117_1_1_2_1_1;
+ _boolean ________split_1_3_1_1_1_2_1_1;
+ _real ________split_2_3_1_1_1_2_1_1;
+ _boolean _______ecart_2_1_1_1_2_1_1;
+ _real _______sqrt_2_1_1_1_2_1_1;
+ _real ________split_94_1_1_1_1_2_1_1;
+ _real ________split_95_1_1_1_1_2_1_1;
+ _real ________split_96_1_1_1_1_2_1_1;
+ _real ________split_97_1_1_1_1_2_1_1;
+ _real ________split_98_1_1_1_1_2_1_1;
+ _real ________split_99_1_1_1_1_2_1_1;
+ _real ________split_101_1_1_1_1_2_1_1;
+ _boolean __________split_1_1_1_1_1_1_1_2_1_1;
+ _real __________split_2_1_1_1_1_1_1_2_1_1;
+ _real _________split_93_1_1_1_1_1_2_1_1;
+ _real _________split_92_1_1_1_1_1_2_1_1;
+ _real _________split_91_1_1_1_1_1_2_1_1;
+ _real _________split_90_1_1_1_1_1_2_1_1;
+ _real ________sqrt_1_1_1_1_1_2_1_1;
+ _boolean ________ecart_1_1_1_1_1_2_1_1;
+ _real _________split_2_2_1_1_1_1_2_1_1;
+ _boolean _________split_1_2_1_1_1_1_2_1_1;
+ _real _______split_109_1_1_1_2_1_1;
+ _real _______split_108_1_1_1_2_1_1;
+ _real _______split_107_1_1_1_2_1_1;
+ _real _______split_106_1_1_1_2_1_1;
+ _real _______split_105_1_1_1_2_1_1;
+ _real _______split_104_1_1_1_2_1_1;
+ _real _______split_103_1_1_1_2_1_1;
+ _real _______split_102_1_1_1_2_1_1;
+ _real ______sqrt_3_1_1_2_1_1;
+ _boolean ______ecart_3_1_1_2_1_1;
+ _real _______split_2_4_1_1_2_1_1;
+ _boolean _______split_1_4_1_1_2_1_1;
+ _real _____split_125_1_2_1_1;
+ _real _____split_124_1_2_1_1;
+ _real _____split_123_1_2_1_1;
+ _real _____split_122_1_2_1_1;
+ _real _____split_121_1_2_1_1;
+ _real _____split_120_1_2_1_1;
+ _real _____split_119_1_2_1_1;
+ _real _____split_118_1_2_1_1;
+ _real ____sqrt_5_2_1_1;
+ _boolean ____ecart_5_2_1_1;
+ _real ____presqrt_5_1_1_1;
+ _boolean ______split_1_5_1_1_1_1;
+ _real ______split_2_5_1_1_1_1;
+ _boolean _____ecart_4_1_1_1_1;
+ _real _____sqrt_4_1_1_1_1;
+ _real ______split_110_1_1_1_1_1;
+ _real ______split_111_1_1_1_1_1;
+ _real ______split_112_1_1_1_1_1;
+ _real ______split_113_1_1_1_1_1;
+ _real ______split_114_1_1_1_1_1;
+ _real ______split_115_1_1_1_1_1;
+ _real ______split_116_1_1_1_1_1;
+ _real ______split_117_1_1_1_1_1;
+ _boolean ________split_1_3_1_1_1_1_1_1;
+ _real ________split_2_3_1_1_1_1_1_1;
+ _boolean _______ecart_2_1_1_1_1_1_1;
+ _real _______sqrt_2_1_1_1_1_1_1;
+ _real ________split_94_1_1_1_1_1_1_1;
+ _real ________split_95_1_1_1_1_1_1_1;
+ _real ________split_96_1_1_1_1_1_1_1;
+ _real ________split_97_1_1_1_1_1_1_1;
+ _real ________split_98_1_1_1_1_1_1_1;
+ _real ________split_99_1_1_1_1_1_1_1;
+ _real ________split_101_1_1_1_1_1_1_1;
+ _boolean __________split_1_1_1_1_1_1_1_1_1_1;
+ _real __________split_2_1_1_1_1_1_1_1_1_1;
+ _real _________split_93_1_1_1_1_1_1_1_1;
+ _real _________split_92_1_1_1_1_1_1_1_1;
+ _real _________split_91_1_1_1_1_1_1_1_1;
+ _real _________split_90_1_1_1_1_1_1_1_1;
+ _real ________sqrt_1_1_1_1_1_1_1_1;
+ _boolean ________ecart_1_1_1_1_1_1_1_1;
+ _real _________split_2_2_1_1_1_1_1_1_1;
+ _boolean _________split_1_2_1_1_1_1_1_1_1;
+ _real _______split_109_1_1_1_1_1_1;
+ _real _______split_108_1_1_1_1_1_1;
+ _real _______split_107_1_1_1_1_1_1;
+ _real _______split_106_1_1_1_1_1_1;
+ _real _______split_105_1_1_1_1_1_1;
+ _real _______split_104_1_1_1_1_1_1;
+ _real _______split_103_1_1_1_1_1_1;
+ _real _______split_102_1_1_1_1_1_1;
+ _real ______sqrt_3_1_1_1_1_1;
+ _boolean ______ecart_3_1_1_1_1_1;
+ _real _______split_2_4_1_1_1_1_1;
+ _boolean _______split_1_4_1_1_1_1_1;
+ _real _____split_125_1_1_1_1;
+ _real _____split_124_1_1_1_1;
+ _real _____split_123_1_1_1_1;
+ _real _____split_122_1_1_1_1;
+ _real _____split_121_1_1_1_1;
+ _real _____split_120_1_1_1_1;
+ _real _____split_119_1_1_1_1;
+ _real _____split_118_1_1_1_1;
+ _real ____sqrt_5_1_1_1;
+ _boolean ____ecart_5_1_1_1;
+ _real ___split_77_1_1;
+ _real ___split_76_1_1;
+ _real ___split_75_1_1;
+ _real ___split_74_1_1;
+ _real ___split_73_1_1;
+ _real ___split_72_1_1;
+ _real ___split_71_1_1;
+ _real ___split_70_1_1;
+ _real ___split_69_1_1;
+ _real ___split_68_1_1;
+ _real ___split_67_1_1;
+ _real ___split_66_1_1;
+ _real ___split_65_1_1;
+ _real ___split_64_1_1;
+ _real ___split_63_1_1;
+ _real ___split_62_1_1;
+ _real ___split_61_1_1;
+ _real ___split_60_1_1;
+ _real ___split_59_1_1;
+ _integer ___split_58_1_1;
+ _integer ___split_57_1_1;
+ _boolean ___split_56_1_1;
+ _boolean ___split_55_1_1;
+ _integer ___split_54_1_1;
+ _boolean ___split_53_1_1;
+ _boolean ___split_52_1_1;
+ _real ___split_51_1_1;
+ _real ___split_50_1_1;
+ _real ___split_49_1_1;
+ _real __delta_1_1;
+ _integer __sol_nb_1_1;
+ _real __a2_1_1;
+ _real __b2_1_1;
+ _real __delta_pos_1_1;
+ _real ___split_4_1_1;
+ _boolean _split_7;
+ _real _split_6;
+ _real _split_5;
+ _integer St;
+ _boolean _split_8;
+ _boolean _split_9;
+
+ Lustre_pre_get(&__split_89_1,&ctx->Lustre_pre_ctx_tab[3]);
+ Lustre_arrow_step(0.0,__split_89_1,Speed,&ctx->Lustre_arrow_ctx_tab[3]);
+ switch (St){
+ case convertible_anti_col:
+ _split_5 = *Speed;
+ ___split_4_1_1 = _split_5 * _split_5;
+ Lustre_slash_step(___split_4_1_1,5500.0,&_tBrake_1);
+ __split_17_1 = 2.0 + _tBrake_1;
+ _split_6 = Dist;
+ __split_15_1 = - 2.0;
+ __split_16_1 = __split_15_1 * _split_6;
+ ___split_60_1_1 = - __split_16_1;
+ __split_14_1 = 2.0 * _split_5;
+ Lustre_slash_step(___split_60_1_1,__split_14_1,&___split_61_1_1);
+ switch (__sol_nb_1_1){
+ case convertible_deg1:
+ ___split_62_1_1 = ___split_61_1_1;
+ _tChoc_1 = ___split_62_1_1;
+ break;
+ case convertible_no_sol:
+ ___split_59_1_1 = - 1.0;
+ _tChoc_1 = ___split_59_1_1;
+ break;
+}
+ ___split_63_1_1 = - __split_14_1;
+ Lustre_pre_get(&__split_11_1,&ctx->Lustre_pre_ctx_tab[0]);
+ __split_12_1 = _split_5 - __split_11_1;
+ Lustre_slash_step(__split_12_1,0.1,&__split_13_1);
+ Lustre_arrow_step(0.0,__split_13_1,&_Accel_1,&ctx->Lustre_arrow_ctx_tab[0]);
+ ___split_64_1_1 = 2.0 * _Accel_1;
+ Lustre_slash_step(___split_63_1_1,___split_64_1_1,&___split_65_1_1);
+ switch (__sol_nb_1_1){
+ case convertible_one_sol:
+ ___split_66_1_1 = ___split_65_1_1;
+ _tChoc_1 = ___split_66_1_1;
+ break;
+}
+ ___split_50_1_1 = 4.0 * _Accel_1;
+ ___split_51_1_1 = ___split_50_1_1 * __split_16_1;
+ ___split_49_1_1 = __split_14_1 * __split_14_1;
+ __delta_1_1 = ___split_49_1_1 - ___split_51_1_1;
+ switch (__sol_nb_1_1){
+ case convertible_two_sol:
+ __delta_pos_1_1 = __delta_1_1;
+ ____presqrt_5_1_1_1 = 1.0;
+ Lustre_slash_step(__delta_pos_1_1,____presqrt_5_1_1_1,&_____split_120_1_1_1_1);
+ _____split_121_1_1_1_1 = ____presqrt_5_1_1_1 + _____split_120_1_1_1_1;
+ ____sqrt_5_1_1_1 = 0.5 * _____split_121_1_1_1_1;
+ _____split_118_1_1_1_1 = ____presqrt_5_1_1_1 - ____sqrt_5_1_1_1;
+ ______split_2_5_1_1_1_1 = - _____split_118_1_1_1_1;
+ ______split_1_5_1_1_1_1 = _____split_118_1_1_1_1 >= 0.0;
+ if (______split_1_5_1_1_1_1 == _true) {
+ _____split_119_1_1_1_1 = _____split_118_1_1_1_1;
+ } else {
+ _____split_119_1_1_1_1 = ______split_2_5_1_1_1_1;
+ }
+ ____ecart_5_1_1_1 = _____split_119_1_1_1_1 < 0.0005;
+ switch (____ecart_5_1_1_1){
+ case _false:
+ _____split_123_1_1_1_1 = ____sqrt_5_1_1_1;
+ _____split_122_1_1_1_1 = __delta_pos_1_1;
+ Lustre_slash_step(_____split_122_1_1_1_1,_____split_123_1_1_1_1,&______split_112_1_1_1_1_1);
+ ______split_113_1_1_1_1_1 = _____split_123_1_1_1_1 + ______split_112_1_1_1_1_1;
+ _____sqrt_4_1_1_1_1 = 0.5 * ______split_113_1_1_1_1_1;
+ ______split_110_1_1_1_1_1 = _____split_123_1_1_1_1 - _____sqrt_4_1_1_1_1;
+ _______split_2_4_1_1_1_1_1 = - ______split_110_1_1_1_1_1;
+ _______split_1_4_1_1_1_1_1 = ______split_110_1_1_1_1_1 >= 0.0;
+ if (_______split_1_4_1_1_1_1_1 == _true) {
+ ______split_111_1_1_1_1_1 = ______split_110_1_1_1_1_1;
+ } else {
+ ______split_111_1_1_1_1_1 = _______split_2_4_1_1_1_1_1;
+ }
+ _____ecart_4_1_1_1_1 = ______split_111_1_1_1_1_1 < 0.0005;
+ switch (_____ecart_4_1_1_1_1){
+ case _false:
+ ______split_115_1_1_1_1_1 = _____sqrt_4_1_1_1_1;
+ ______split_114_1_1_1_1_1 = _____split_122_1_1_1_1;
+ Lustre_slash_step(______split_114_1_1_1_1_1,______split_115_1_1_1_1_1,&_______split_104_1_1_1_1_1_1);
+ _______split_105_1_1_1_1_1_1 = ______split_115_1_1_1_1_1 + _______split_104_1_1_1_1_1_1;
+ ______sqrt_3_1_1_1_1_1 = 0.5 * _______split_105_1_1_1_1_1_1;
+ _______split_102_1_1_1_1_1_1 = ______split_115_1_1_1_1_1 - ______sqrt_3_1_1_1_1_1;
+ ________split_2_3_1_1_1_1_1_1 = - _______split_102_1_1_1_1_1_1;
+ ________split_1_3_1_1_1_1_1_1 = _______split_102_1_1_1_1_1_1 >= 0.0;
+ if (________split_1_3_1_1_1_1_1_1 == _true) {
+ _______split_103_1_1_1_1_1_1 = _______split_102_1_1_1_1_1_1;
+ } else {
+ _______split_103_1_1_1_1_1_1 = ________split_2_3_1_1_1_1_1_1;
+ }
+ ______ecart_3_1_1_1_1_1 = _______split_103_1_1_1_1_1_1 < 0.0005;
+ switch (______ecart_3_1_1_1_1_1){
+ case _false:
+ _______split_107_1_1_1_1_1_1 = ______sqrt_3_1_1_1_1_1;
+ _______split_106_1_1_1_1_1_1 = ______split_114_1_1_1_1_1;
+ Lustre_slash_step(_______split_106_1_1_1_1_1_1,_______split_107_1_1_1_1_1_1,&________split_96_1_1_1_1_1_1_1);
+ ________split_97_1_1_1_1_1_1_1 = _______split_107_1_1_1_1_1_1 + ________split_96_1_1_1_1_1_1_1;
+ _______sqrt_2_1_1_1_1_1_1 = 0.5 * ________split_97_1_1_1_1_1_1_1;
+ ________split_94_1_1_1_1_1_1_1 = _______split_107_1_1_1_1_1_1 - _______sqrt_2_1_1_1_1_1_1;
+ _________split_2_2_1_1_1_1_1_1_1 = - ________split_94_1_1_1_1_1_1_1;
+ _________split_1_2_1_1_1_1_1_1_1 = ________split_94_1_1_1_1_1_1_1 >= 0.0;
+ if (_________split_1_2_1_1_1_1_1_1_1 == _true) {
+ ________split_95_1_1_1_1_1_1_1 = ________split_94_1_1_1_1_1_1_1;
+ } else {
+ ________split_95_1_1_1_1_1_1_1 = _________split_2_2_1_1_1_1_1_1_1;
+ }
+ _______ecart_2_1_1_1_1_1_1 = ________split_95_1_1_1_1_1_1_1 < 0.0005;
+ switch (_______ecart_2_1_1_1_1_1_1){
+ case _false:
+ ________split_98_1_1_1_1_1_1_1 = _______split_106_1_1_1_1_1_1;
+ ________split_99_1_1_1_1_1_1_1 = _______sqrt_2_1_1_1_1_1_1;
+ Lustre_slash_step(________split_98_1_1_1_1_1_1_1,________split_99_1_1_1_1_1_1_1,&_________split_92_1_1_1_1_1_1_1_1);
+ _________split_93_1_1_1_1_1_1_1_1 = ________split_99_1_1_1_1_1_1_1 + _________split_92_1_1_1_1_1_1_1_1;
+ ________sqrt_1_1_1_1_1_1_1_1 = 0.5 * _________split_93_1_1_1_1_1_1_1_1;
+ _______split_108_1_1_1_1_1_1 = ________sqrt_1_1_1_1_1_1_1_1;
+ break;
+ case _true:
+ ________split_101_1_1_1_1_1_1_1 = _______sqrt_2_1_1_1_1_1_1;
+ _______split_108_1_1_1_1_1_1 = ________split_101_1_1_1_1_1_1_1;
+ break;
+}
+ ______split_116_1_1_1_1_1 = _______split_108_1_1_1_1_1_1;
+ break;
+ case _true:
+ _______split_109_1_1_1_1_1_1 = ______sqrt_3_1_1_1_1_1;
+ ______split_116_1_1_1_1_1 = _______split_109_1_1_1_1_1_1;
+ break;
+}
+ _____split_124_1_1_1_1 = ______split_116_1_1_1_1_1;
+ break;
+ case _true:
+ ______split_117_1_1_1_1_1 = _____sqrt_4_1_1_1_1;
+ _____split_124_1_1_1_1 = ______split_117_1_1_1_1_1;
+ break;
+}
+ ___split_68_1_1 = _____split_124_1_1_1_1;
+ break;
+ case _true:
+ _____split_125_1_1_1_1 = ____sqrt_5_1_1_1;
+ ___split_68_1_1 = _____split_125_1_1_1_1;
+ break;
+}
+ __a2_1_1 = _Accel_1;
+ ___split_69_1_1 = 2.0 * __a2_1_1;
+ Lustre_slash_step(___split_68_1_1,___split_69_1_1,&___split_70_1_1);
+ __b2_1_1 = __split_14_1;
+ ___split_67_1_1 = - __b2_1_1;
+ ___split_71_1_1 = ___split_67_1_1 + ___split_70_1_1;
+ ____presqrt_5_2_1_1 = 1.0;
+ Lustre_slash_step(__delta_pos_1_1,____presqrt_5_2_1_1,&_____split_120_1_2_1_1);
+ _____split_121_1_2_1_1 = ____presqrt_5_2_1_1 + _____split_120_1_2_1_1;
+ ____sqrt_5_2_1_1 = 0.5 * _____split_121_1_2_1_1;
+ _____split_118_1_2_1_1 = ____presqrt_5_2_1_1 - ____sqrt_5_2_1_1;
+ ______split_2_5_1_2_1_1 = - _____split_118_1_2_1_1;
+ ______split_1_5_1_2_1_1 = _____split_118_1_2_1_1 >= 0.0;
+ if (______split_1_5_1_2_1_1 == _true) {
+ _____split_119_1_2_1_1 = _____split_118_1_2_1_1;
+ } else {
+ _____split_119_1_2_1_1 = ______split_2_5_1_2_1_1;
+ }
+ ____ecart_5_2_1_1 = _____split_119_1_2_1_1 < 0.0005;
+ switch (____ecart_5_2_1_1){
+ case _false:
+ _____split_123_1_2_1_1 = ____sqrt_5_2_1_1;
+ _____split_122_1_2_1_1 = __delta_pos_1_1;
+ Lustre_slash_step(_____split_122_1_2_1_1,_____split_123_1_2_1_1,&______split_112_1_1_2_1_1);
+ ______split_113_1_1_2_1_1 = _____split_123_1_2_1_1 + ______split_112_1_1_2_1_1;
+ _____sqrt_4_1_2_1_1 = 0.5 * ______split_113_1_1_2_1_1;
+ ______split_110_1_1_2_1_1 = _____split_123_1_2_1_1 - _____sqrt_4_1_2_1_1;
+ _______split_2_4_1_1_2_1_1 = - ______split_110_1_1_2_1_1;
+ _______split_1_4_1_1_2_1_1 = ______split_110_1_1_2_1_1 >= 0.0;
+ if (_______split_1_4_1_1_2_1_1 == _true) {
+ ______split_111_1_1_2_1_1 = ______split_110_1_1_2_1_1;
+ } else {
+ ______split_111_1_1_2_1_1 = _______split_2_4_1_1_2_1_1;
+ }
+ _____ecart_4_1_2_1_1 = ______split_111_1_1_2_1_1 < 0.0005;
+ switch (_____ecart_4_1_2_1_1){
+ case _false:
+ ______split_115_1_1_2_1_1 = _____sqrt_4_1_2_1_1;
+ ______split_114_1_1_2_1_1 = _____split_122_1_2_1_1;
+ Lustre_slash_step(______split_114_1_1_2_1_1,______split_115_1_1_2_1_1,&_______split_104_1_1_1_2_1_1);
+ _______split_105_1_1_1_2_1_1 = ______split_115_1_1_2_1_1 + _______split_104_1_1_1_2_1_1;
+ ______sqrt_3_1_1_2_1_1 = 0.5 * _______split_105_1_1_1_2_1_1;
+ _______split_102_1_1_1_2_1_1 = ______split_115_1_1_2_1_1 - ______sqrt_3_1_1_2_1_1;
+ ________split_2_3_1_1_1_2_1_1 = - _______split_102_1_1_1_2_1_1;
+ ________split_1_3_1_1_1_2_1_1 = _______split_102_1_1_1_2_1_1 >= 0.0;
+ if (________split_1_3_1_1_1_2_1_1 == _true) {
+ _______split_103_1_1_1_2_1_1 = _______split_102_1_1_1_2_1_1;
+ } else {
+ _______split_103_1_1_1_2_1_1 = ________split_2_3_1_1_1_2_1_1;
+ }
+ ______ecart_3_1_1_2_1_1 = _______split_103_1_1_1_2_1_1 < 0.0005;
+ switch (______ecart_3_1_1_2_1_1){
+ case _false:
+ _______split_107_1_1_1_2_1_1 = ______sqrt_3_1_1_2_1_1;
+ _______split_106_1_1_1_2_1_1 = ______split_114_1_1_2_1_1;
+ Lustre_slash_step(_______split_106_1_1_1_2_1_1,_______split_107_1_1_1_2_1_1,&________split_96_1_1_1_1_2_1_1);
+ ________split_97_1_1_1_1_2_1_1 = _______split_107_1_1_1_2_1_1 + ________split_96_1_1_1_1_2_1_1;
+ _______sqrt_2_1_1_1_2_1_1 = 0.5 * ________split_97_1_1_1_1_2_1_1;
+ ________split_94_1_1_1_1_2_1_1 = _______split_107_1_1_1_2_1_1 - _______sqrt_2_1_1_1_2_1_1;
+ _________split_2_2_1_1_1_1_2_1_1 = - ________split_94_1_1_1_1_2_1_1;
+ _________split_1_2_1_1_1_1_2_1_1 = ________split_94_1_1_1_1_2_1_1 >= 0.0;
+ if (_________split_1_2_1_1_1_1_2_1_1 == _true) {
+ ________split_95_1_1_1_1_2_1_1 = ________split_94_1_1_1_1_2_1_1;
+ } else {
+ ________split_95_1_1_1_1_2_1_1 = _________split_2_2_1_1_1_1_2_1_1;
+ }
+ _______ecart_2_1_1_1_2_1_1 = ________split_95_1_1_1_1_2_1_1 < 0.0005;
+ switch (_______ecart_2_1_1_1_2_1_1){
+ case _false:
+ ________split_98_1_1_1_1_2_1_1 = _______split_106_1_1_1_2_1_1;
+ ________split_99_1_1_1_1_2_1_1 = _______sqrt_2_1_1_1_2_1_1;
+ Lustre_slash_step(________split_98_1_1_1_1_2_1_1,________split_99_1_1_1_1_2_1_1,&_________split_92_1_1_1_1_1_2_1_1);
+ _________split_93_1_1_1_1_1_2_1_1 = ________split_99_1_1_1_1_2_1_1 + _________split_92_1_1_1_1_1_2_1_1;
+ ________sqrt_1_1_1_1_1_2_1_1 = 0.5 * _________split_93_1_1_1_1_1_2_1_1;
+ _______split_108_1_1_1_2_1_1 = ________sqrt_1_1_1_1_1_2_1_1;
+ break;
+ case _true:
+ ________split_101_1_1_1_1_2_1_1 = _______sqrt_2_1_1_1_2_1_1;
+ _______split_108_1_1_1_2_1_1 = ________split_101_1_1_1_1_2_1_1;
+ break;
+}
+ ______split_116_1_1_2_1_1 = _______split_108_1_1_1_2_1_1;
+ break;
+ case _true:
+ _______split_109_1_1_1_2_1_1 = ______sqrt_3_1_1_2_1_1;
+ ______split_116_1_1_2_1_1 = _______split_109_1_1_1_2_1_1;
+ break;
+}
+ _____split_124_1_2_1_1 = ______split_116_1_1_2_1_1;
+ break;
+ case _true:
+ ______split_117_1_1_2_1_1 = _____sqrt_4_1_2_1_1;
+ _____split_124_1_2_1_1 = ______split_117_1_1_2_1_1;
+ break;
+}
+ ___split_73_1_1 = _____split_124_1_2_1_1;
+ break;
+ case _true:
+ _____split_125_1_2_1_1 = ____sqrt_5_2_1_1;
+ ___split_73_1_1 = _____split_125_1_2_1_1;
+ break;
+}
+ ___split_74_1_1 = 2.0 * __a2_1_1;
+ Lustre_slash_step(___split_73_1_1,___split_74_1_1,&___split_75_1_1);
+ ___split_72_1_1 = - __b2_1_1;
+ ___split_76_1_1 = ___split_72_1_1 - ___split_75_1_1;
+ ____split_10_1_1_1 = ___split_71_1_1 < ___split_76_1_1;
+ if (____split_10_1_1_1 == _true) {
+ ___split_77_1_1 = ___split_76_1_1;
+ } else {
+ ___split_77_1_1 = ___split_71_1_1;
+ }
+ _tChoc_1 = ___split_77_1_1;
+ break;
+}
+ _split_7 = _tChoc_1 < __split_17_1;
+ *Danger = _split_7;
+ Lustre_pre_set(_split_5,&ctx->Lustre_pre_ctx_tab[0]);
+ switch (__sol_nb_1_1){
+ case convertible_two_sol:
+ switch (____ecart_5_1_1_1){
+ case _false:
+ switch (_____ecart_4_1_1_1_1){
+ case _false:
+ switch (______ecart_3_1_1_1_1_1){
+ case _false:
+ switch (_______ecart_2_1_1_1_1_1_1){
+ case _false:
+ _________split_90_1_1_1_1_1_1_1_1 = ________split_99_1_1_1_1_1_1_1 - ________sqrt_1_1_1_1_1_1_1_1;
+ __________split_2_1_1_1_1_1_1_1_1_1 = - _________split_90_1_1_1_1_1_1_1_1;
+ __________split_1_1_1_1_1_1_1_1_1_1 = _________split_90_1_1_1_1_1_1_1_1 >= 0.0;
+ if (__________split_1_1_1_1_1_1_1_1_1_1 == _true) {
+ _________split_91_1_1_1_1_1_1_1_1 = _________split_90_1_1_1_1_1_1_1_1;
+ } else {
+ _________split_91_1_1_1_1_1_1_1_1 = __________split_2_1_1_1_1_1_1_1_1_1;
+ }
+ ________ecart_1_1_1_1_1_1_1_1 = _________split_91_1_1_1_1_1_1_1_1 < 0.0005;
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ switch (____ecart_5_2_1_1){
+ case _false:
+ switch (_____ecart_4_1_2_1_1){
+ case _false:
+ switch (______ecart_3_1_1_2_1_1){
+ case _false:
+ switch (_______ecart_2_1_1_1_2_1_1){
+ case _false:
+ _________split_90_1_1_1_1_1_2_1_1 = ________split_99_1_1_1_1_2_1_1 - ________sqrt_1_1_1_1_1_2_1_1;
+ __________split_2_1_1_1_1_1_1_2_1_1 = - _________split_90_1_1_1_1_1_2_1_1;
+ __________split_1_1_1_1_1_1_1_2_1_1 = _________split_90_1_1_1_1_1_2_1_1 >= 0.0;
+ if (__________split_1_1_1_1_1_1_1_2_1_1 == _true) {
+ _________split_91_1_1_1_1_1_2_1_1 = _________split_90_1_1_1_1_1_2_1_1;
+ } else {
+ _________split_91_1_1_1_1_1_2_1_1 = __________split_2_1_1_1_1_1_1_2_1_1;
+ }
+ ________ecart_1_1_1_1_1_2_1_1 = _________split_91_1_1_1_1_1_2_1_1 < 0.0005;
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ ___split_52_1_1 = _Accel_1 == 0.0;
+ ___split_53_1_1 = __split_14_1 == 0.0;
+ if (___split_53_1_1 == _true) {
+ ___split_54_1_1 = convertible_no_sol;
+ } else {
+ ___split_54_1_1 = convertible_deg1;
+ }
+ ___split_56_1_1 = __delta_1_1 == 0.0;
+ if (___split_56_1_1 == _true) {
+ ___split_57_1_1 = convertible_one_sol;
+ } else {
+ ___split_57_1_1 = convertible_two_sol;
+ }
+ ___split_55_1_1 = __delta_1_1 < 0.0;
+ if (___split_55_1_1 == _true) {
+ ___split_58_1_1 = convertible_no_sol;
+ } else {
+ ___split_58_1_1 = ___split_57_1_1;
+ }
+ if (___split_52_1_1 == _true) {
+ __sol_nb_1_1 = ___split_54_1_1;
+ } else {
+ __sol_nb_1_1 = ___split_58_1_1;
+ }
+ break;
+ case convertible_run:
+ *Danger = _false;
+ break;
+ case convertible_stationnary:
+ *Danger = _false;
+ break;
+}
+ Lustre_pre_2_get(&__split_138_1,&ctx->Lustre_pre_2_ctx_tab[0]);
+ _ac_cond_1 = *Speed >= 110.0;
+ switch (_pst_1){
+ case convertible_anti_col:
+ __split_147_1 = _ac_cond_1;
+ __split_148_1 = ! __split_147_1;
+ if (__split_148_1 == _true) {
+ __split_149_1 = convertible_run;
+ } else {
+ __split_149_1 = convertible_anti_col;
+ }
+ St = __split_149_1;
+ break;
+}
+ __split_143_1 = *Speed == 0.0;
+ switch (_pst_1){
+ case convertible_run:
+ __split_144_1 = __split_143_1;
+ if (__split_144_1 == _true) {
+ __split_145_1 = convertible_stationnary;
+ } else {
+ __split_145_1 = convertible_run;
+ }
+ __split_142_1 = _ac_cond_1;
+ if (__split_142_1 == _true) {
+ __split_146_1 = convertible_anti_col;
+ } else {
+ __split_146_1 = __split_145_1;
+ }
+ St = __split_146_1;
+ break;
+}
+ switch (_pst_3){
+ case convertible_in_motion:
+ __split_22_1 = Done;
+ if (__split_22_1 == _true) {
+ __split_23_1 = convertible_locked;
+ } else {
+ __split_23_1 = convertible_in_motion;
+ }
+ _st_2 = __split_23_1;
+ break;
+}
+ __split_19_1 = OnOff & Parked;
+ switch (_pst_3){
+ case convertible_locked:
+ __split_20_1 = __split_19_1;
+ if (__split_20_1 == _true) {
+ __split_21_1 = convertible_in_motion;
+ } else {
+ __split_21_1 = convertible_locked;
+ }
+ _st_2 = __split_21_1;
+ break;
+}
+ *Locked = _st_2 == convertible_locked;
+ __split_139_1 = Start & *Locked;
+ switch (_pst_1){
+ case convertible_stationnary:
+ __split_140_1 = __split_139_1;
+ if (__split_140_1 == _true) {
+ __split_141_1 = convertible_run;
+ } else {
+ __split_141_1 = convertible_stationnary;
+ }
+ St = __split_141_1;
+ break;
+}
+ Lustre_pre_2_set(St,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_step(convertible_stationnary,__split_138_1,&_pst_1,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ if (Rot == _true) {
+ _dx_1 = 1.4;
+ } else {
+ _dx_1 = 0.0;
+ }
+ if (Tick == _true) {
+ _tx_1 = 0.1;
+ } else {
+ _tx_1 = 0.0;
+ }
+ _TickOrRot_1 = Tick | Rot;
+ Lustre_pre_get(&__split_78_1,&ctx->Lustre_pre_ctx_tab[1]);
+ Lustre_arrow_step(0.0,__split_78_1,&_pd_1,&ctx->Lustre_arrow_ctx_tab[1]);
+ switch (_TickOrRot_1){
+ case _false:
+ __split_80_1 = _pd_1;
+ _d_1 = __split_80_1;
+ break;
+ case _true:
+ __split_81_1 = _dx_1;
+ ____split_3_2_1_1.v = __split_81_1;
+ _____acc_1_1_2_1_1.v = ____split_3_2_1_1.v;
+ _____acc_2_1_2_1_1.v = _____acc_1_1_2_1_1.v;
+ _____acc_3_1_2_1_1.v = _____acc_2_1_2_1_1.v;
+ _____acc_4_1_2_1_1.v = _____acc_3_1_2_1_1.v;
+ _____acc_5_1_2_1_1.v = _____acc_4_1_2_1_1.v;
+ _____acc_6_1_2_1_1.v = _____acc_5_1_2_1_1.v;
+ _____acc_7_1_2_1_1.v = _____acc_6_1_2_1_1.v;
+ _____acc_8_1_2_1_1.v = _____acc_7_1_2_1_1.v;
+ _____acc_9_1_2_1_1.v = _____acc_8_1_2_1_1.v;
+ _____acc_10_1_2_1_1.v = _____acc_9_1_2_1_1.v;
+ _____acc_11_1_2_1_1.v = _____acc_10_1_2_1_1.v;
+ _____acc_12_1_2_1_1.v = _____acc_11_1_2_1_1.v;
+ _____acc_13_1_2_1_1.v = _____acc_12_1_2_1_1.v;
+ _____acc_14_1_2_1_1.v = _____acc_13_1_2_1_1.v;
+ _____acc_15_1_2_1_1.v = _____acc_14_1_2_1_1.v;
+ _____acc_16_1_2_1_1.v = _____acc_15_1_2_1_1.v;
+ _____acc_17_1_2_1_1.v = _____acc_16_1_2_1_1.v;
+ _____acc_18_1_2_1_1.v = _____acc_17_1_2_1_1.v;
+ _____acc_19_1_2_1_1.v = _____acc_18_1_2_1_1.v;
+ _____acc_20_1_2_1_1.v = _____acc_19_1_2_1_1.v;
+ _____acc_21_1_2_1_1.v = _____acc_20_1_2_1_1.v;
+ _____acc_22_1_2_1_1.v = _____acc_21_1_2_1_1.v;
+ _____acc_23_1_2_1_1.v = _____acc_22_1_2_1_1.v;
+ _____acc_24_1_2_1_1.v = _____acc_23_1_2_1_1.v;
+ _____acc_25_1_2_1_1.v = _____acc_24_1_2_1_1.v;
+ _____acc_26_1_2_1_1.v = _____acc_25_1_2_1_1.v;
+ _____acc_27_1_2_1_1.v = _____acc_26_1_2_1_1.v;
+ _____acc_28_1_2_1_1.v = _____acc_27_1_2_1_1.v;
+ _____acc_29_1_2_1_1.v = _____acc_28_1_2_1_1.v;
+ _____acc_30_1_2_1_1.v = _____acc_29_1_2_1_1.v;
+ _____acc_31_1_2_1_1.v = _____acc_30_1_2_1_1.v;
+ _____acc_32_1_2_1_1.v = _____acc_31_1_2_1_1.v;
+ _____acc_33_1_2_1_1.v = _____acc_32_1_2_1_1.v;
+ _____acc_34_1_2_1_1.v = _____acc_33_1_2_1_1.v;
+ _____acc_35_1_2_1_1.v = _____acc_34_1_2_1_1.v;
+ _____acc_36_1_2_1_1.v = _____acc_35_1_2_1_1.v;
+ _____acc_37_1_2_1_1.v = _____acc_36_1_2_1_1.v;
+ _____acc_38_1_2_1_1.v = _____acc_37_1_2_1_1.v;
+ _____acc_39_1_2_1_1.v = _____acc_38_1_2_1_1.v;
+ _____acc_40_1_2_1_1.v = _____acc_39_1_2_1_1.v;
+ _____acc_41_1_2_1_1.v = _____acc_40_1_2_1_1.v;
+ _____acc_42_1_2_1_1.v = _____acc_41_1_2_1_1.v;
+ _____acc_43_1_2_1_1.v = _____acc_42_1_2_1_1.v;
+ _____acc_44_1_2_1_1.v = _____acc_43_1_2_1_1.v;
+ _____acc_45_1_2_1_1.v = _____acc_44_1_2_1_1.v;
+ _____acc_46_1_2_1_1.v = _____acc_45_1_2_1_1.v;
+ _____acc_47_1_2_1_1.v = _____acc_46_1_2_1_1.v;
+ _____acc_48_1_2_1_1.v = _____acc_47_1_2_1_1.v;
+ _____acc_49_1_2_1_1.v = _____acc_48_1_2_1_1.v;
+ ____split_3_2_1_1.i = 0;
+ ______split_137_1_1_2_1_1 = ____split_3_2_1_1.i + 1;
+ _____acc_1_1_2_1_1.i = ______split_137_1_1_2_1_1;
+ ______split_137_2_1_2_1_1 = _____acc_1_1_2_1_1.i + 1;
+ _____acc_2_1_2_1_1.i = ______split_137_2_1_2_1_1;
+ ______split_137_3_1_2_1_1 = _____acc_2_1_2_1_1.i + 1;
+ _____acc_3_1_2_1_1.i = ______split_137_3_1_2_1_1;
+ ______split_137_4_1_2_1_1 = _____acc_3_1_2_1_1.i + 1;
+ _____acc_4_1_2_1_1.i = ______split_137_4_1_2_1_1;
+ ______split_137_5_1_2_1_1 = _____acc_4_1_2_1_1.i + 1;
+ _____acc_5_1_2_1_1.i = ______split_137_5_1_2_1_1;
+ ______split_137_6_1_2_1_1 = _____acc_5_1_2_1_1.i + 1;
+ _____acc_6_1_2_1_1.i = ______split_137_6_1_2_1_1;
+ ______split_137_7_1_2_1_1 = _____acc_6_1_2_1_1.i + 1;
+ _____acc_7_1_2_1_1.i = ______split_137_7_1_2_1_1;
+ ______split_137_8_1_2_1_1 = _____acc_7_1_2_1_1.i + 1;
+ _____acc_8_1_2_1_1.i = ______split_137_8_1_2_1_1;
+ ______split_137_9_1_2_1_1 = _____acc_8_1_2_1_1.i + 1;
+ _____acc_9_1_2_1_1.i = ______split_137_9_1_2_1_1;
+ ______split_137_10_1_2_1_1 = _____acc_9_1_2_1_1.i + 1;
+ _____acc_10_1_2_1_1.i = ______split_137_10_1_2_1_1;
+ ______split_137_11_1_2_1_1 = _____acc_10_1_2_1_1.i + 1;
+ _____acc_11_1_2_1_1.i = ______split_137_11_1_2_1_1;
+ ______split_137_12_1_2_1_1 = _____acc_11_1_2_1_1.i + 1;
+ _____acc_12_1_2_1_1.i = ______split_137_12_1_2_1_1;
+ ______split_137_13_1_2_1_1 = _____acc_12_1_2_1_1.i + 1;
+ _____acc_13_1_2_1_1.i = ______split_137_13_1_2_1_1;
+ ______split_137_14_1_2_1_1 = _____acc_13_1_2_1_1.i + 1;
+ _____acc_14_1_2_1_1.i = ______split_137_14_1_2_1_1;
+ ______split_137_15_1_2_1_1 = _____acc_14_1_2_1_1.i + 1;
+ _____acc_15_1_2_1_1.i = ______split_137_15_1_2_1_1;
+ ______split_137_16_1_2_1_1 = _____acc_15_1_2_1_1.i + 1;
+ _____acc_16_1_2_1_1.i = ______split_137_16_1_2_1_1;
+ ______split_137_17_1_2_1_1 = _____acc_16_1_2_1_1.i + 1;
+ _____acc_17_1_2_1_1.i = ______split_137_17_1_2_1_1;
+ ______split_137_18_1_2_1_1 = _____acc_17_1_2_1_1.i + 1;
+ _____acc_18_1_2_1_1.i = ______split_137_18_1_2_1_1;
+ ______split_137_19_1_2_1_1 = _____acc_18_1_2_1_1.i + 1;
+ _____acc_19_1_2_1_1.i = ______split_137_19_1_2_1_1;
+ ______split_137_20_1_2_1_1 = _____acc_19_1_2_1_1.i + 1;
+ _____acc_20_1_2_1_1.i = ______split_137_20_1_2_1_1;
+ ______split_137_21_1_2_1_1 = _____acc_20_1_2_1_1.i + 1;
+ _____acc_21_1_2_1_1.i = ______split_137_21_1_2_1_1;
+ ______split_137_22_1_2_1_1 = _____acc_21_1_2_1_1.i + 1;
+ _____acc_22_1_2_1_1.i = ______split_137_22_1_2_1_1;
+ ______split_137_23_1_2_1_1 = _____acc_22_1_2_1_1.i + 1;
+ _____acc_23_1_2_1_1.i = ______split_137_23_1_2_1_1;
+ ______split_137_24_1_2_1_1 = _____acc_23_1_2_1_1.i + 1;
+ _____acc_24_1_2_1_1.i = ______split_137_24_1_2_1_1;
+ ______split_137_25_1_2_1_1 = _____acc_24_1_2_1_1.i + 1;
+ _____acc_25_1_2_1_1.i = ______split_137_25_1_2_1_1;
+ ______split_137_26_1_2_1_1 = _____acc_25_1_2_1_1.i + 1;
+ _____acc_26_1_2_1_1.i = ______split_137_26_1_2_1_1;
+ ______split_137_27_1_2_1_1 = _____acc_26_1_2_1_1.i + 1;
+ _____acc_27_1_2_1_1.i = ______split_137_27_1_2_1_1;
+ ______split_137_28_1_2_1_1 = _____acc_27_1_2_1_1.i + 1;
+ _____acc_28_1_2_1_1.i = ______split_137_28_1_2_1_1;
+ ______split_137_29_1_2_1_1 = _____acc_28_1_2_1_1.i + 1;
+ _____acc_29_1_2_1_1.i = ______split_137_29_1_2_1_1;
+ ______split_137_30_1_2_1_1 = _____acc_29_1_2_1_1.i + 1;
+ _____acc_30_1_2_1_1.i = ______split_137_30_1_2_1_1;
+ ______split_137_31_1_2_1_1 = _____acc_30_1_2_1_1.i + 1;
+ _____acc_31_1_2_1_1.i = ______split_137_31_1_2_1_1;
+ ______split_137_32_1_2_1_1 = _____acc_31_1_2_1_1.i + 1;
+ _____acc_32_1_2_1_1.i = ______split_137_32_1_2_1_1;
+ ______split_137_33_1_2_1_1 = _____acc_32_1_2_1_1.i + 1;
+ _____acc_33_1_2_1_1.i = ______split_137_33_1_2_1_1;
+ ______split_137_34_1_2_1_1 = _____acc_33_1_2_1_1.i + 1;
+ _____acc_34_1_2_1_1.i = ______split_137_34_1_2_1_1;
+ ______split_137_35_1_2_1_1 = _____acc_34_1_2_1_1.i + 1;
+ _____acc_35_1_2_1_1.i = ______split_137_35_1_2_1_1;
+ ______split_137_36_1_2_1_1 = _____acc_35_1_2_1_1.i + 1;
+ _____acc_36_1_2_1_1.i = ______split_137_36_1_2_1_1;
+ ______split_137_37_1_2_1_1 = _____acc_36_1_2_1_1.i + 1;
+ _____acc_37_1_2_1_1.i = ______split_137_37_1_2_1_1;
+ ______split_137_38_1_2_1_1 = _____acc_37_1_2_1_1.i + 1;
+ _____acc_38_1_2_1_1.i = ______split_137_38_1_2_1_1;
+ ______split_137_39_1_2_1_1 = _____acc_38_1_2_1_1.i + 1;
+ _____acc_39_1_2_1_1.i = ______split_137_39_1_2_1_1;
+ ______split_137_40_1_2_1_1 = _____acc_39_1_2_1_1.i + 1;
+ _____acc_40_1_2_1_1.i = ______split_137_40_1_2_1_1;
+ ______split_137_41_1_2_1_1 = _____acc_40_1_2_1_1.i + 1;
+ _____acc_41_1_2_1_1.i = ______split_137_41_1_2_1_1;
+ ______split_137_42_1_2_1_1 = _____acc_41_1_2_1_1.i + 1;
+ _____acc_42_1_2_1_1.i = ______split_137_42_1_2_1_1;
+ ______split_137_43_1_2_1_1 = _____acc_42_1_2_1_1.i + 1;
+ _____acc_43_1_2_1_1.i = ______split_137_43_1_2_1_1;
+ ______split_137_44_1_2_1_1 = _____acc_43_1_2_1_1.i + 1;
+ _____acc_44_1_2_1_1.i = ______split_137_44_1_2_1_1;
+ ______split_137_45_1_2_1_1 = _____acc_44_1_2_1_1.i + 1;
+ _____acc_45_1_2_1_1.i = ______split_137_45_1_2_1_1;
+ ______split_137_46_1_2_1_1 = _____acc_45_1_2_1_1.i + 1;
+ _____acc_46_1_2_1_1.i = ______split_137_46_1_2_1_1;
+ ______split_137_47_1_2_1_1 = _____acc_46_1_2_1_1.i + 1;
+ _____acc_47_1_2_1_1.i = ______split_137_47_1_2_1_1;
+ ______split_137_48_1_2_1_1 = _____acc_47_1_2_1_1.i + 1;
+ _____acc_48_1_2_1_1.i = ______split_137_48_1_2_1_1;
+ ______split_137_49_1_2_1_1 = _____acc_48_1_2_1_1.i + 1;
+ _____acc_49_1_2_1_1.i = ______split_137_49_1_2_1_1;
+ Lustre_pre_2_get(&___split_126_1_1,&ctx->Lustre_pre_2_ctx_tab[1]);
+ Lustre_arrow_2_step(0,___split_126_1_1,&___split_127_1_1,&ctx->Lustre_arrow_2_ctx_tab[1]);
+ __i_2_1 = ___split_127_1_1 + 1;
+ ___split_130_1_1 = __i_2_1 % 50;
+ ____split_3_2_1_1.j = ___split_130_1_1;
+ _____acc_1_1_2_1_1.j = ____split_3_2_1_1.j;
+ _____acc_2_1_2_1_1.j = _____acc_1_1_2_1_1.j;
+ _____acc_3_1_2_1_1.j = _____acc_2_1_2_1_1.j;
+ _____acc_4_1_2_1_1.j = _____acc_3_1_2_1_1.j;
+ _____acc_5_1_2_1_1.j = _____acc_4_1_2_1_1.j;
+ _____acc_6_1_2_1_1.j = _____acc_5_1_2_1_1.j;
+ _____acc_7_1_2_1_1.j = _____acc_6_1_2_1_1.j;
+ _____acc_8_1_2_1_1.j = _____acc_7_1_2_1_1.j;
+ _____acc_9_1_2_1_1.j = _____acc_8_1_2_1_1.j;
+ _____acc_10_1_2_1_1.j = _____acc_9_1_2_1_1.j;
+ _____acc_11_1_2_1_1.j = _____acc_10_1_2_1_1.j;
+ _____acc_12_1_2_1_1.j = _____acc_11_1_2_1_1.j;
+ _____acc_13_1_2_1_1.j = _____acc_12_1_2_1_1.j;
+ _____acc_14_1_2_1_1.j = _____acc_13_1_2_1_1.j;
+ _____acc_15_1_2_1_1.j = _____acc_14_1_2_1_1.j;
+ _____acc_16_1_2_1_1.j = _____acc_15_1_2_1_1.j;
+ _____acc_17_1_2_1_1.j = _____acc_16_1_2_1_1.j;
+ _____acc_18_1_2_1_1.j = _____acc_17_1_2_1_1.j;
+ _____acc_19_1_2_1_1.j = _____acc_18_1_2_1_1.j;
+ _____acc_20_1_2_1_1.j = _____acc_19_1_2_1_1.j;
+ _____acc_21_1_2_1_1.j = _____acc_20_1_2_1_1.j;
+ _____acc_22_1_2_1_1.j = _____acc_21_1_2_1_1.j;
+ _____acc_23_1_2_1_1.j = _____acc_22_1_2_1_1.j;
+ _____acc_24_1_2_1_1.j = _____acc_23_1_2_1_1.j;
+ _____acc_25_1_2_1_1.j = _____acc_24_1_2_1_1.j;
+ _____acc_26_1_2_1_1.j = _____acc_25_1_2_1_1.j;
+ _____acc_27_1_2_1_1.j = _____acc_26_1_2_1_1.j;
+ _____acc_28_1_2_1_1.j = _____acc_27_1_2_1_1.j;
+ _____acc_29_1_2_1_1.j = _____acc_28_1_2_1_1.j;
+ _____acc_30_1_2_1_1.j = _____acc_29_1_2_1_1.j;
+ _____acc_31_1_2_1_1.j = _____acc_30_1_2_1_1.j;
+ _____acc_32_1_2_1_1.j = _____acc_31_1_2_1_1.j;
+ _____acc_33_1_2_1_1.j = _____acc_32_1_2_1_1.j;
+ _____acc_34_1_2_1_1.j = _____acc_33_1_2_1_1.j;
+ _____acc_35_1_2_1_1.j = _____acc_34_1_2_1_1.j;
+ _____acc_36_1_2_1_1.j = _____acc_35_1_2_1_1.j;
+ _____acc_37_1_2_1_1.j = _____acc_36_1_2_1_1.j;
+ _____acc_38_1_2_1_1.j = _____acc_37_1_2_1_1.j;
+ _____acc_39_1_2_1_1.j = _____acc_38_1_2_1_1.j;
+ _____acc_40_1_2_1_1.j = _____acc_39_1_2_1_1.j;
+ _____acc_41_1_2_1_1.j = _____acc_40_1_2_1_1.j;
+ _____acc_42_1_2_1_1.j = _____acc_41_1_2_1_1.j;
+ _____acc_43_1_2_1_1.j = _____acc_42_1_2_1_1.j;
+ _____acc_44_1_2_1_1.j = _____acc_43_1_2_1_1.j;
+ _____acc_45_1_2_1_1.j = _____acc_44_1_2_1_1.j;
+ _____acc_46_1_2_1_1.j = _____acc_45_1_2_1_1.j;
+ _____acc_47_1_2_1_1.j = _____acc_46_1_2_1_1.j;
+ _____acc_48_1_2_1_1.j = _____acc_47_1_2_1_1.j;
+ _____acc_49_1_2_1_1.j = _____acc_48_1_2_1_1.j;
+ ______split_136_50_1_2_1_1 = _____acc_49_1_2_1_1.i == _____acc_49_1_2_1_1.j;
+ Lustre_pre_3_get(___split_129_1_1,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_hat_step(0.0,___split_128_1_1);
+ Lustre_arrow_3_step(___split_128_1_1,___split_129_1_1,__pre_a_2_1,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ _____cell_50_1_2_1_1 = __pre_a_2_1[49];
+ if (______split_136_50_1_2_1_1 == _true) {
+ _____ncell_50_1_2_1_1 = _____acc_49_1_2_1_1.v;
+ } else {
+ _____ncell_50_1_2_1_1 = _____cell_50_1_2_1_1;
+ }
+ __a_3_1[49] = _____ncell_50_1_2_1_1;
+ ______split_136_49_1_2_1_1 = _____acc_48_1_2_1_1.i == _____acc_48_1_2_1_1.j;
+ _____cell_49_1_2_1_1 = __pre_a_2_1[48];
+ if (______split_136_49_1_2_1_1 == _true) {
+ _____ncell_49_1_2_1_1 = _____acc_48_1_2_1_1.v;
+ } else {
+ _____ncell_49_1_2_1_1 = _____cell_49_1_2_1_1;
+ }
+ __a_3_1[48] = _____ncell_49_1_2_1_1;
+ ______split_136_48_1_2_1_1 = _____acc_47_1_2_1_1.i == _____acc_47_1_2_1_1.j;
+ _____cell_48_1_2_1_1 = __pre_a_2_1[47];
+ if (______split_136_48_1_2_1_1 == _true) {
+ _____ncell_48_1_2_1_1 = _____acc_47_1_2_1_1.v;
+ } else {
+ _____ncell_48_1_2_1_1 = _____cell_48_1_2_1_1;
+ }
+ __a_3_1[47] = _____ncell_48_1_2_1_1;
+ ______split_136_47_1_2_1_1 = _____acc_46_1_2_1_1.i == _____acc_46_1_2_1_1.j;
+ _____cell_47_1_2_1_1 = __pre_a_2_1[46];
+ if (______split_136_47_1_2_1_1 == _true) {
+ _____ncell_47_1_2_1_1 = _____acc_46_1_2_1_1.v;
+ } else {
+ _____ncell_47_1_2_1_1 = _____cell_47_1_2_1_1;
+ }
+ __a_3_1[46] = _____ncell_47_1_2_1_1;
+ ______split_136_46_1_2_1_1 = _____acc_45_1_2_1_1.i == _____acc_45_1_2_1_1.j;
+ _____cell_46_1_2_1_1 = __pre_a_2_1[45];
+ if (______split_136_46_1_2_1_1 == _true) {
+ _____ncell_46_1_2_1_1 = _____acc_45_1_2_1_1.v;
+ } else {
+ _____ncell_46_1_2_1_1 = _____cell_46_1_2_1_1;
+ }
+ __a_3_1[45] = _____ncell_46_1_2_1_1;
+ ______split_136_45_1_2_1_1 = _____acc_44_1_2_1_1.i == _____acc_44_1_2_1_1.j;
+ _____cell_45_1_2_1_1 = __pre_a_2_1[44];
+ if (______split_136_45_1_2_1_1 == _true) {
+ _____ncell_45_1_2_1_1 = _____acc_44_1_2_1_1.v;
+ } else {
+ _____ncell_45_1_2_1_1 = _____cell_45_1_2_1_1;
+ }
+ __a_3_1[44] = _____ncell_45_1_2_1_1;
+ ______split_136_44_1_2_1_1 = _____acc_43_1_2_1_1.i == _____acc_43_1_2_1_1.j;
+ _____cell_44_1_2_1_1 = __pre_a_2_1[43];
+ if (______split_136_44_1_2_1_1 == _true) {
+ _____ncell_44_1_2_1_1 = _____acc_43_1_2_1_1.v;
+ } else {
+ _____ncell_44_1_2_1_1 = _____cell_44_1_2_1_1;
+ }
+ __a_3_1[43] = _____ncell_44_1_2_1_1;
+ ______split_136_43_1_2_1_1 = _____acc_42_1_2_1_1.i == _____acc_42_1_2_1_1.j;
+ _____cell_43_1_2_1_1 = __pre_a_2_1[42];
+ if (______split_136_43_1_2_1_1 == _true) {
+ _____ncell_43_1_2_1_1 = _____acc_42_1_2_1_1.v;
+ } else {
+ _____ncell_43_1_2_1_1 = _____cell_43_1_2_1_1;
+ }
+ __a_3_1[42] = _____ncell_43_1_2_1_1;
+ ______split_136_42_1_2_1_1 = _____acc_41_1_2_1_1.i == _____acc_41_1_2_1_1.j;
+ _____cell_42_1_2_1_1 = __pre_a_2_1[41];
+ if (______split_136_42_1_2_1_1 == _true) {
+ _____ncell_42_1_2_1_1 = _____acc_41_1_2_1_1.v;
+ } else {
+ _____ncell_42_1_2_1_1 = _____cell_42_1_2_1_1;
+ }
+ __a_3_1[41] = _____ncell_42_1_2_1_1;
+ ______split_136_41_1_2_1_1 = _____acc_40_1_2_1_1.i == _____acc_40_1_2_1_1.j;
+ _____cell_41_1_2_1_1 = __pre_a_2_1[40];
+ if (______split_136_41_1_2_1_1 == _true) {
+ _____ncell_41_1_2_1_1 = _____acc_40_1_2_1_1.v;
+ } else {
+ _____ncell_41_1_2_1_1 = _____cell_41_1_2_1_1;
+ }
+ __a_3_1[40] = _____ncell_41_1_2_1_1;
+ ______split_136_40_1_2_1_1 = _____acc_39_1_2_1_1.i == _____acc_39_1_2_1_1.j;
+ _____cell_40_1_2_1_1 = __pre_a_2_1[39];
+ if (______split_136_40_1_2_1_1 == _true) {
+ _____ncell_40_1_2_1_1 = _____acc_39_1_2_1_1.v;
+ } else {
+ _____ncell_40_1_2_1_1 = _____cell_40_1_2_1_1;
+ }
+ __a_3_1[39] = _____ncell_40_1_2_1_1;
+ ______split_136_39_1_2_1_1 = _____acc_38_1_2_1_1.i == _____acc_38_1_2_1_1.j;
+ _____cell_39_1_2_1_1 = __pre_a_2_1[38];
+ if (______split_136_39_1_2_1_1 == _true) {
+ _____ncell_39_1_2_1_1 = _____acc_38_1_2_1_1.v;
+ } else {
+ _____ncell_39_1_2_1_1 = _____cell_39_1_2_1_1;
+ }
+ __a_3_1[38] = _____ncell_39_1_2_1_1;
+ ______split_136_38_1_2_1_1 = _____acc_37_1_2_1_1.i == _____acc_37_1_2_1_1.j;
+ _____cell_38_1_2_1_1 = __pre_a_2_1[37];
+ if (______split_136_38_1_2_1_1 == _true) {
+ _____ncell_38_1_2_1_1 = _____acc_37_1_2_1_1.v;
+ } else {
+ _____ncell_38_1_2_1_1 = _____cell_38_1_2_1_1;
+ }
+ __a_3_1[37] = _____ncell_38_1_2_1_1;
+ ______split_136_37_1_2_1_1 = _____acc_36_1_2_1_1.i == _____acc_36_1_2_1_1.j;
+ _____cell_37_1_2_1_1 = __pre_a_2_1[36];
+ if (______split_136_37_1_2_1_1 == _true) {
+ _____ncell_37_1_2_1_1 = _____acc_36_1_2_1_1.v;
+ } else {
+ _____ncell_37_1_2_1_1 = _____cell_37_1_2_1_1;
+ }
+ __a_3_1[36] = _____ncell_37_1_2_1_1;
+ ______split_136_36_1_2_1_1 = _____acc_35_1_2_1_1.i == _____acc_35_1_2_1_1.j;
+ _____cell_36_1_2_1_1 = __pre_a_2_1[35];
+ if (______split_136_36_1_2_1_1 == _true) {
+ _____ncell_36_1_2_1_1 = _____acc_35_1_2_1_1.v;
+ } else {
+ _____ncell_36_1_2_1_1 = _____cell_36_1_2_1_1;
+ }
+ __a_3_1[35] = _____ncell_36_1_2_1_1;
+ ______split_136_35_1_2_1_1 = _____acc_34_1_2_1_1.i == _____acc_34_1_2_1_1.j;
+ _____cell_35_1_2_1_1 = __pre_a_2_1[34];
+ if (______split_136_35_1_2_1_1 == _true) {
+ _____ncell_35_1_2_1_1 = _____acc_34_1_2_1_1.v;
+ } else {
+ _____ncell_35_1_2_1_1 = _____cell_35_1_2_1_1;
+ }
+ __a_3_1[34] = _____ncell_35_1_2_1_1;
+ ______split_136_34_1_2_1_1 = _____acc_33_1_2_1_1.i == _____acc_33_1_2_1_1.j;
+ _____cell_34_1_2_1_1 = __pre_a_2_1[33];
+ if (______split_136_34_1_2_1_1 == _true) {
+ _____ncell_34_1_2_1_1 = _____acc_33_1_2_1_1.v;
+ } else {
+ _____ncell_34_1_2_1_1 = _____cell_34_1_2_1_1;
+ }
+ __a_3_1[33] = _____ncell_34_1_2_1_1;
+ ______split_136_33_1_2_1_1 = _____acc_32_1_2_1_1.i == _____acc_32_1_2_1_1.j;
+ _____cell_33_1_2_1_1 = __pre_a_2_1[32];
+ if (______split_136_33_1_2_1_1 == _true) {
+ _____ncell_33_1_2_1_1 = _____acc_32_1_2_1_1.v;
+ } else {
+ _____ncell_33_1_2_1_1 = _____cell_33_1_2_1_1;
+ }
+ __a_3_1[32] = _____ncell_33_1_2_1_1;
+ ______split_136_32_1_2_1_1 = _____acc_31_1_2_1_1.i == _____acc_31_1_2_1_1.j;
+ _____cell_32_1_2_1_1 = __pre_a_2_1[31];
+ if (______split_136_32_1_2_1_1 == _true) {
+ _____ncell_32_1_2_1_1 = _____acc_31_1_2_1_1.v;
+ } else {
+ _____ncell_32_1_2_1_1 = _____cell_32_1_2_1_1;
+ }
+ __a_3_1[31] = _____ncell_32_1_2_1_1;
+ ______split_136_31_1_2_1_1 = _____acc_30_1_2_1_1.i == _____acc_30_1_2_1_1.j;
+ _____cell_31_1_2_1_1 = __pre_a_2_1[30];
+ if (______split_136_31_1_2_1_1 == _true) {
+ _____ncell_31_1_2_1_1 = _____acc_30_1_2_1_1.v;
+ } else {
+ _____ncell_31_1_2_1_1 = _____cell_31_1_2_1_1;
+ }
+ __a_3_1[30] = _____ncell_31_1_2_1_1;
+ ______split_136_30_1_2_1_1 = _____acc_29_1_2_1_1.i == _____acc_29_1_2_1_1.j;
+ _____cell_30_1_2_1_1 = __pre_a_2_1[29];
+ if (______split_136_30_1_2_1_1 == _true) {
+ _____ncell_30_1_2_1_1 = _____acc_29_1_2_1_1.v;
+ } else {
+ _____ncell_30_1_2_1_1 = _____cell_30_1_2_1_1;
+ }
+ __a_3_1[29] = _____ncell_30_1_2_1_1;
+ ______split_136_29_1_2_1_1 = _____acc_28_1_2_1_1.i == _____acc_28_1_2_1_1.j;
+ _____cell_29_1_2_1_1 = __pre_a_2_1[28];
+ if (______split_136_29_1_2_1_1 == _true) {
+ _____ncell_29_1_2_1_1 = _____acc_28_1_2_1_1.v;
+ } else {
+ _____ncell_29_1_2_1_1 = _____cell_29_1_2_1_1;
+ }
+ __a_3_1[28] = _____ncell_29_1_2_1_1;
+ ______split_136_28_1_2_1_1 = _____acc_27_1_2_1_1.i == _____acc_27_1_2_1_1.j;
+ _____cell_28_1_2_1_1 = __pre_a_2_1[27];
+ if (______split_136_28_1_2_1_1 == _true) {
+ _____ncell_28_1_2_1_1 = _____acc_27_1_2_1_1.v;
+ } else {
+ _____ncell_28_1_2_1_1 = _____cell_28_1_2_1_1;
+ }
+ __a_3_1[27] = _____ncell_28_1_2_1_1;
+ ______split_136_27_1_2_1_1 = _____acc_26_1_2_1_1.i == _____acc_26_1_2_1_1.j;
+ _____cell_27_1_2_1_1 = __pre_a_2_1[26];
+ if (______split_136_27_1_2_1_1 == _true) {
+ _____ncell_27_1_2_1_1 = _____acc_26_1_2_1_1.v;
+ } else {
+ _____ncell_27_1_2_1_1 = _____cell_27_1_2_1_1;
+ }
+ __a_3_1[26] = _____ncell_27_1_2_1_1;
+ ______split_136_26_1_2_1_1 = _____acc_25_1_2_1_1.i == _____acc_25_1_2_1_1.j;
+ _____cell_26_1_2_1_1 = __pre_a_2_1[25];
+ if (______split_136_26_1_2_1_1 == _true) {
+ _____ncell_26_1_2_1_1 = _____acc_25_1_2_1_1.v;
+ } else {
+ _____ncell_26_1_2_1_1 = _____cell_26_1_2_1_1;
+ }
+ __a_3_1[25] = _____ncell_26_1_2_1_1;
+ ______split_136_25_1_2_1_1 = _____acc_24_1_2_1_1.i == _____acc_24_1_2_1_1.j;
+ _____cell_25_1_2_1_1 = __pre_a_2_1[24];
+ if (______split_136_25_1_2_1_1 == _true) {
+ _____ncell_25_1_2_1_1 = _____acc_24_1_2_1_1.v;
+ } else {
+ _____ncell_25_1_2_1_1 = _____cell_25_1_2_1_1;
+ }
+ __a_3_1[24] = _____ncell_25_1_2_1_1;
+ ______split_136_24_1_2_1_1 = _____acc_23_1_2_1_1.i == _____acc_23_1_2_1_1.j;
+ _____cell_24_1_2_1_1 = __pre_a_2_1[23];
+ if (______split_136_24_1_2_1_1 == _true) {
+ _____ncell_24_1_2_1_1 = _____acc_23_1_2_1_1.v;
+ } else {
+ _____ncell_24_1_2_1_1 = _____cell_24_1_2_1_1;
+ }
+ __a_3_1[23] = _____ncell_24_1_2_1_1;
+ ______split_136_23_1_2_1_1 = _____acc_22_1_2_1_1.i == _____acc_22_1_2_1_1.j;
+ _____cell_23_1_2_1_1 = __pre_a_2_1[22];
+ if (______split_136_23_1_2_1_1 == _true) {
+ _____ncell_23_1_2_1_1 = _____acc_22_1_2_1_1.v;
+ } else {
+ _____ncell_23_1_2_1_1 = _____cell_23_1_2_1_1;
+ }
+ __a_3_1[22] = _____ncell_23_1_2_1_1;
+ ______split_136_22_1_2_1_1 = _____acc_21_1_2_1_1.i == _____acc_21_1_2_1_1.j;
+ _____cell_22_1_2_1_1 = __pre_a_2_1[21];
+ if (______split_136_22_1_2_1_1 == _true) {
+ _____ncell_22_1_2_1_1 = _____acc_21_1_2_1_1.v;
+ } else {
+ _____ncell_22_1_2_1_1 = _____cell_22_1_2_1_1;
+ }
+ __a_3_1[21] = _____ncell_22_1_2_1_1;
+ ______split_136_21_1_2_1_1 = _____acc_20_1_2_1_1.i == _____acc_20_1_2_1_1.j;
+ _____cell_21_1_2_1_1 = __pre_a_2_1[20];
+ if (______split_136_21_1_2_1_1 == _true) {
+ _____ncell_21_1_2_1_1 = _____acc_20_1_2_1_1.v;
+ } else {
+ _____ncell_21_1_2_1_1 = _____cell_21_1_2_1_1;
+ }
+ __a_3_1[20] = _____ncell_21_1_2_1_1;
+ ______split_136_20_1_2_1_1 = _____acc_19_1_2_1_1.i == _____acc_19_1_2_1_1.j;
+ _____cell_20_1_2_1_1 = __pre_a_2_1[19];
+ if (______split_136_20_1_2_1_1 == _true) {
+ _____ncell_20_1_2_1_1 = _____acc_19_1_2_1_1.v;
+ } else {
+ _____ncell_20_1_2_1_1 = _____cell_20_1_2_1_1;
+ }
+ __a_3_1[19] = _____ncell_20_1_2_1_1;
+ ______split_136_19_1_2_1_1 = _____acc_18_1_2_1_1.i == _____acc_18_1_2_1_1.j;
+ _____cell_19_1_2_1_1 = __pre_a_2_1[18];
+ if (______split_136_19_1_2_1_1 == _true) {
+ _____ncell_19_1_2_1_1 = _____acc_18_1_2_1_1.v;
+ } else {
+ _____ncell_19_1_2_1_1 = _____cell_19_1_2_1_1;
+ }
+ __a_3_1[18] = _____ncell_19_1_2_1_1;
+ ______split_136_18_1_2_1_1 = _____acc_17_1_2_1_1.i == _____acc_17_1_2_1_1.j;
+ _____cell_18_1_2_1_1 = __pre_a_2_1[17];
+ if (______split_136_18_1_2_1_1 == _true) {
+ _____ncell_18_1_2_1_1 = _____acc_17_1_2_1_1.v;
+ } else {
+ _____ncell_18_1_2_1_1 = _____cell_18_1_2_1_1;
+ }
+ __a_3_1[17] = _____ncell_18_1_2_1_1;
+ ______split_136_17_1_2_1_1 = _____acc_16_1_2_1_1.i == _____acc_16_1_2_1_1.j;
+ _____cell_17_1_2_1_1 = __pre_a_2_1[16];
+ if (______split_136_17_1_2_1_1 == _true) {
+ _____ncell_17_1_2_1_1 = _____acc_16_1_2_1_1.v;
+ } else {
+ _____ncell_17_1_2_1_1 = _____cell_17_1_2_1_1;
+ }
+ __a_3_1[16] = _____ncell_17_1_2_1_1;
+ ______split_136_16_1_2_1_1 = _____acc_15_1_2_1_1.i == _____acc_15_1_2_1_1.j;
+ _____cell_16_1_2_1_1 = __pre_a_2_1[15];
+ if (______split_136_16_1_2_1_1 == _true) {
+ _____ncell_16_1_2_1_1 = _____acc_15_1_2_1_1.v;
+ } else {
+ _____ncell_16_1_2_1_1 = _____cell_16_1_2_1_1;
+ }
+ __a_3_1[15] = _____ncell_16_1_2_1_1;
+ ______split_136_15_1_2_1_1 = _____acc_14_1_2_1_1.i == _____acc_14_1_2_1_1.j;
+ _____cell_15_1_2_1_1 = __pre_a_2_1[14];
+ if (______split_136_15_1_2_1_1 == _true) {
+ _____ncell_15_1_2_1_1 = _____acc_14_1_2_1_1.v;
+ } else {
+ _____ncell_15_1_2_1_1 = _____cell_15_1_2_1_1;
+ }
+ __a_3_1[14] = _____ncell_15_1_2_1_1;
+ ______split_136_14_1_2_1_1 = _____acc_13_1_2_1_1.i == _____acc_13_1_2_1_1.j;
+ _____cell_14_1_2_1_1 = __pre_a_2_1[13];
+ if (______split_136_14_1_2_1_1 == _true) {
+ _____ncell_14_1_2_1_1 = _____acc_13_1_2_1_1.v;
+ } else {
+ _____ncell_14_1_2_1_1 = _____cell_14_1_2_1_1;
+ }
+ __a_3_1[13] = _____ncell_14_1_2_1_1;
+ ______split_136_13_1_2_1_1 = _____acc_12_1_2_1_1.i == _____acc_12_1_2_1_1.j;
+ _____cell_13_1_2_1_1 = __pre_a_2_1[12];
+ if (______split_136_13_1_2_1_1 == _true) {
+ _____ncell_13_1_2_1_1 = _____acc_12_1_2_1_1.v;
+ } else {
+ _____ncell_13_1_2_1_1 = _____cell_13_1_2_1_1;
+ }
+ __a_3_1[12] = _____ncell_13_1_2_1_1;
+ ______split_136_12_1_2_1_1 = _____acc_11_1_2_1_1.i == _____acc_11_1_2_1_1.j;
+ _____cell_12_1_2_1_1 = __pre_a_2_1[11];
+ if (______split_136_12_1_2_1_1 == _true) {
+ _____ncell_12_1_2_1_1 = _____acc_11_1_2_1_1.v;
+ } else {
+ _____ncell_12_1_2_1_1 = _____cell_12_1_2_1_1;
+ }
+ __a_3_1[11] = _____ncell_12_1_2_1_1;
+ ______split_136_11_1_2_1_1 = _____acc_10_1_2_1_1.i == _____acc_10_1_2_1_1.j;
+ _____cell_11_1_2_1_1 = __pre_a_2_1[10];
+ if (______split_136_11_1_2_1_1 == _true) {
+ _____ncell_11_1_2_1_1 = _____acc_10_1_2_1_1.v;
+ } else {
+ _____ncell_11_1_2_1_1 = _____cell_11_1_2_1_1;
+ }
+ __a_3_1[10] = _____ncell_11_1_2_1_1;
+ ______split_136_10_1_2_1_1 = _____acc_9_1_2_1_1.i == _____acc_9_1_2_1_1.j;
+ _____cell_10_1_2_1_1 = __pre_a_2_1[9];
+ if (______split_136_10_1_2_1_1 == _true) {
+ _____ncell_10_1_2_1_1 = _____acc_9_1_2_1_1.v;
+ } else {
+ _____ncell_10_1_2_1_1 = _____cell_10_1_2_1_1;
+ }
+ __a_3_1[9] = _____ncell_10_1_2_1_1;
+ ______split_136_9_1_2_1_1 = _____acc_8_1_2_1_1.i == _____acc_8_1_2_1_1.j;
+ _____cell_9_1_2_1_1 = __pre_a_2_1[8];
+ if (______split_136_9_1_2_1_1 == _true) {
+ _____ncell_9_1_2_1_1 = _____acc_8_1_2_1_1.v;
+ } else {
+ _____ncell_9_1_2_1_1 = _____cell_9_1_2_1_1;
+ }
+ __a_3_1[8] = _____ncell_9_1_2_1_1;
+ ______split_136_8_1_2_1_1 = _____acc_7_1_2_1_1.i == _____acc_7_1_2_1_1.j;
+ _____cell_8_1_2_1_1 = __pre_a_2_1[7];
+ if (______split_136_8_1_2_1_1 == _true) {
+ _____ncell_8_1_2_1_1 = _____acc_7_1_2_1_1.v;
+ } else {
+ _____ncell_8_1_2_1_1 = _____cell_8_1_2_1_1;
+ }
+ __a_3_1[7] = _____ncell_8_1_2_1_1;
+ ______split_136_7_1_2_1_1 = _____acc_6_1_2_1_1.i == _____acc_6_1_2_1_1.j;
+ _____cell_7_1_2_1_1 = __pre_a_2_1[6];
+ if (______split_136_7_1_2_1_1 == _true) {
+ _____ncell_7_1_2_1_1 = _____acc_6_1_2_1_1.v;
+ } else {
+ _____ncell_7_1_2_1_1 = _____cell_7_1_2_1_1;
+ }
+ __a_3_1[6] = _____ncell_7_1_2_1_1;
+ ______split_136_6_1_2_1_1 = _____acc_5_1_2_1_1.i == _____acc_5_1_2_1_1.j;
+ _____cell_6_1_2_1_1 = __pre_a_2_1[5];
+ if (______split_136_6_1_2_1_1 == _true) {
+ _____ncell_6_1_2_1_1 = _____acc_5_1_2_1_1.v;
+ } else {
+ _____ncell_6_1_2_1_1 = _____cell_6_1_2_1_1;
+ }
+ __a_3_1[5] = _____ncell_6_1_2_1_1;
+ ______split_136_5_1_2_1_1 = _____acc_4_1_2_1_1.i == _____acc_4_1_2_1_1.j;
+ _____cell_5_1_2_1_1 = __pre_a_2_1[4];
+ if (______split_136_5_1_2_1_1 == _true) {
+ _____ncell_5_1_2_1_1 = _____acc_4_1_2_1_1.v;
+ } else {
+ _____ncell_5_1_2_1_1 = _____cell_5_1_2_1_1;
+ }
+ __a_3_1[4] = _____ncell_5_1_2_1_1;
+ ______split_136_4_1_2_1_1 = _____acc_3_1_2_1_1.i == _____acc_3_1_2_1_1.j;
+ _____cell_4_1_2_1_1 = __pre_a_2_1[3];
+ if (______split_136_4_1_2_1_1 == _true) {
+ _____ncell_4_1_2_1_1 = _____acc_3_1_2_1_1.v;
+ } else {
+ _____ncell_4_1_2_1_1 = _____cell_4_1_2_1_1;
+ }
+ __a_3_1[3] = _____ncell_4_1_2_1_1;
+ ______split_136_3_1_2_1_1 = _____acc_2_1_2_1_1.i == _____acc_2_1_2_1_1.j;
+ _____cell_3_1_2_1_1 = __pre_a_2_1[2];
+ if (______split_136_3_1_2_1_1 == _true) {
+ _____ncell_3_1_2_1_1 = _____acc_2_1_2_1_1.v;
+ } else {
+ _____ncell_3_1_2_1_1 = _____cell_3_1_2_1_1;
+ }
+ __a_3_1[2] = _____ncell_3_1_2_1_1;
+ ______split_136_2_1_2_1_1 = _____acc_1_1_2_1_1.i == _____acc_1_1_2_1_1.j;
+ _____cell_2_1_2_1_1 = __pre_a_2_1[1];
+ if (______split_136_2_1_2_1_1 == _true) {
+ _____ncell_2_1_2_1_1 = _____acc_1_1_2_1_1.v;
+ } else {
+ _____ncell_2_1_2_1_1 = _____cell_2_1_2_1_1;
+ }
+ __a_3_1[1] = _____ncell_2_1_2_1_1;
+ ______split_136_1_1_2_1_1 = ____split_3_2_1_1.i == ____split_3_2_1_1.j;
+ _____cell_1_1_2_1_1 = __pre_a_2_1[0];
+ if (______split_136_1_1_2_1_1 == _true) {
+ _____ncell_1_1_2_1_1 = ____split_3_2_1_1.v;
+ } else {
+ _____ncell_1_1_2_1_1 = _____cell_1_1_2_1_1;
+ }
+ __a_3_1[0] = _____ncell_1_1_2_1_1;
+ ___i1_2_1_1 = 0.0;
+ ____acc_50_2_1_1 = ___i1_2_1_1 + __a_3_1[0];
+ ____acc_51_2_1_1 = ____acc_50_2_1_1 + __a_3_1[1];
+ ____acc_52_2_1_1 = ____acc_51_2_1_1 + __a_3_1[2];
+ ____acc_53_2_1_1 = ____acc_52_2_1_1 + __a_3_1[3];
+ ____acc_54_2_1_1 = ____acc_53_2_1_1 + __a_3_1[4];
+ ____acc_55_2_1_1 = ____acc_54_2_1_1 + __a_3_1[5];
+ ____acc_56_2_1_1 = ____acc_55_2_1_1 + __a_3_1[6];
+ ____acc_57_2_1_1 = ____acc_56_2_1_1 + __a_3_1[7];
+ ____acc_58_2_1_1 = ____acc_57_2_1_1 + __a_3_1[8];
+ ____acc_59_2_1_1 = ____acc_58_2_1_1 + __a_3_1[9];
+ ____acc_60_2_1_1 = ____acc_59_2_1_1 + __a_3_1[10];
+ ____acc_61_2_1_1 = ____acc_60_2_1_1 + __a_3_1[11];
+ ____acc_62_2_1_1 = ____acc_61_2_1_1 + __a_3_1[12];
+ ____acc_63_2_1_1 = ____acc_62_2_1_1 + __a_3_1[13];
+ ____acc_64_2_1_1 = ____acc_63_2_1_1 + __a_3_1[14];
+ ____acc_65_2_1_1 = ____acc_64_2_1_1 + __a_3_1[15];
+ ____acc_66_2_1_1 = ____acc_65_2_1_1 + __a_3_1[16];
+ ____acc_67_2_1_1 = ____acc_66_2_1_1 + __a_3_1[17];
+ ____acc_68_2_1_1 = ____acc_67_2_1_1 + __a_3_1[18];
+ ____acc_69_2_1_1 = ____acc_68_2_1_1 + __a_3_1[19];
+ ____acc_70_2_1_1 = ____acc_69_2_1_1 + __a_3_1[20];
+ ____acc_71_2_1_1 = ____acc_70_2_1_1 + __a_3_1[21];
+ ____acc_72_2_1_1 = ____acc_71_2_1_1 + __a_3_1[22];
+ ____acc_73_2_1_1 = ____acc_72_2_1_1 + __a_3_1[23];
+ ____acc_74_2_1_1 = ____acc_73_2_1_1 + __a_3_1[24];
+ ____acc_75_2_1_1 = ____acc_74_2_1_1 + __a_3_1[25];
+ ____acc_76_2_1_1 = ____acc_75_2_1_1 + __a_3_1[26];
+ ____acc_77_2_1_1 = ____acc_76_2_1_1 + __a_3_1[27];
+ ____acc_78_2_1_1 = ____acc_77_2_1_1 + __a_3_1[28];
+ ____acc_79_2_1_1 = ____acc_78_2_1_1 + __a_3_1[29];
+ ____acc_80_2_1_1 = ____acc_79_2_1_1 + __a_3_1[30];
+ ____acc_81_2_1_1 = ____acc_80_2_1_1 + __a_3_1[31];
+ ____acc_82_2_1_1 = ____acc_81_2_1_1 + __a_3_1[32];
+ ____acc_83_2_1_1 = ____acc_82_2_1_1 + __a_3_1[33];
+ ____acc_84_2_1_1 = ____acc_83_2_1_1 + __a_3_1[34];
+ ____acc_85_2_1_1 = ____acc_84_2_1_1 + __a_3_1[35];
+ ____acc_86_2_1_1 = ____acc_85_2_1_1 + __a_3_1[36];
+ ____acc_87_2_1_1 = ____acc_86_2_1_1 + __a_3_1[37];
+ ____acc_88_2_1_1 = ____acc_87_2_1_1 + __a_3_1[38];
+ ____acc_89_2_1_1 = ____acc_88_2_1_1 + __a_3_1[39];
+ ____acc_90_2_1_1 = ____acc_89_2_1_1 + __a_3_1[40];
+ ____acc_91_2_1_1 = ____acc_90_2_1_1 + __a_3_1[41];
+ ____acc_92_2_1_1 = ____acc_91_2_1_1 + __a_3_1[42];
+ ____acc_93_2_1_1 = ____acc_92_2_1_1 + __a_3_1[43];
+ ____acc_94_2_1_1 = ____acc_93_2_1_1 + __a_3_1[44];
+ ____acc_95_2_1_1 = ____acc_94_2_1_1 + __a_3_1[45];
+ ____acc_96_2_1_1 = ____acc_95_2_1_1 + __a_3_1[46];
+ ____acc_97_2_1_1 = ____acc_96_2_1_1 + __a_3_1[47];
+ ____acc_98_2_1_1 = ____acc_97_2_1_1 + __a_3_1[48];
+ __split_82_1 = ____acc_98_2_1_1 + __a_3_1[49];
+ _d_1 = __split_82_1;
+ break;
+}
+ Lustre_pre_set(_d_1,&ctx->Lustre_pre_ctx_tab[1]);
+ Lustre_pre_get(&__split_79_1,&ctx->Lustre_pre_ctx_tab[2]);
+ __x_13_1 = 0.1;
+ Lustre_arrow_step(0.0,__split_79_1,&_pt_1,&ctx->Lustre_arrow_ctx_tab[2]);
+ switch (_TickOrRot_1){
+ case _false:
+ __split_85_1 = _pt_1;
+ __split_86_1 = __split_85_1;
+ break;
+ case _true:
+ __split_83_1 = _tx_1;
+ ____split_3_1_1_1.v = __split_83_1;
+ _____acc_1_1_1_1_1.v = ____split_3_1_1_1.v;
+ _____acc_2_1_1_1_1.v = _____acc_1_1_1_1_1.v;
+ _____acc_3_1_1_1_1.v = _____acc_2_1_1_1_1.v;
+ _____acc_4_1_1_1_1.v = _____acc_3_1_1_1_1.v;
+ _____acc_5_1_1_1_1.v = _____acc_4_1_1_1_1.v;
+ _____acc_6_1_1_1_1.v = _____acc_5_1_1_1_1.v;
+ _____acc_7_1_1_1_1.v = _____acc_6_1_1_1_1.v;
+ _____acc_8_1_1_1_1.v = _____acc_7_1_1_1_1.v;
+ _____acc_9_1_1_1_1.v = _____acc_8_1_1_1_1.v;
+ _____acc_10_1_1_1_1.v = _____acc_9_1_1_1_1.v;
+ _____acc_11_1_1_1_1.v = _____acc_10_1_1_1_1.v;
+ _____acc_12_1_1_1_1.v = _____acc_11_1_1_1_1.v;
+ _____acc_13_1_1_1_1.v = _____acc_12_1_1_1_1.v;
+ _____acc_14_1_1_1_1.v = _____acc_13_1_1_1_1.v;
+ _____acc_15_1_1_1_1.v = _____acc_14_1_1_1_1.v;
+ _____acc_16_1_1_1_1.v = _____acc_15_1_1_1_1.v;
+ _____acc_17_1_1_1_1.v = _____acc_16_1_1_1_1.v;
+ _____acc_18_1_1_1_1.v = _____acc_17_1_1_1_1.v;
+ _____acc_19_1_1_1_1.v = _____acc_18_1_1_1_1.v;
+ _____acc_20_1_1_1_1.v = _____acc_19_1_1_1_1.v;
+ _____acc_21_1_1_1_1.v = _____acc_20_1_1_1_1.v;
+ _____acc_22_1_1_1_1.v = _____acc_21_1_1_1_1.v;
+ _____acc_23_1_1_1_1.v = _____acc_22_1_1_1_1.v;
+ _____acc_24_1_1_1_1.v = _____acc_23_1_1_1_1.v;
+ _____acc_25_1_1_1_1.v = _____acc_24_1_1_1_1.v;
+ _____acc_26_1_1_1_1.v = _____acc_25_1_1_1_1.v;
+ _____acc_27_1_1_1_1.v = _____acc_26_1_1_1_1.v;
+ _____acc_28_1_1_1_1.v = _____acc_27_1_1_1_1.v;
+ _____acc_29_1_1_1_1.v = _____acc_28_1_1_1_1.v;
+ _____acc_30_1_1_1_1.v = _____acc_29_1_1_1_1.v;
+ _____acc_31_1_1_1_1.v = _____acc_30_1_1_1_1.v;
+ _____acc_32_1_1_1_1.v = _____acc_31_1_1_1_1.v;
+ _____acc_33_1_1_1_1.v = _____acc_32_1_1_1_1.v;
+ _____acc_34_1_1_1_1.v = _____acc_33_1_1_1_1.v;
+ _____acc_35_1_1_1_1.v = _____acc_34_1_1_1_1.v;
+ _____acc_36_1_1_1_1.v = _____acc_35_1_1_1_1.v;
+ _____acc_37_1_1_1_1.v = _____acc_36_1_1_1_1.v;
+ _____acc_38_1_1_1_1.v = _____acc_37_1_1_1_1.v;
+ _____acc_39_1_1_1_1.v = _____acc_38_1_1_1_1.v;
+ _____acc_40_1_1_1_1.v = _____acc_39_1_1_1_1.v;
+ _____acc_41_1_1_1_1.v = _____acc_40_1_1_1_1.v;
+ _____acc_42_1_1_1_1.v = _____acc_41_1_1_1_1.v;
+ _____acc_43_1_1_1_1.v = _____acc_42_1_1_1_1.v;
+ _____acc_44_1_1_1_1.v = _____acc_43_1_1_1_1.v;
+ _____acc_45_1_1_1_1.v = _____acc_44_1_1_1_1.v;
+ _____acc_46_1_1_1_1.v = _____acc_45_1_1_1_1.v;
+ _____acc_47_1_1_1_1.v = _____acc_46_1_1_1_1.v;
+ _____acc_48_1_1_1_1.v = _____acc_47_1_1_1_1.v;
+ _____acc_49_1_1_1_1.v = _____acc_48_1_1_1_1.v;
+ ____split_3_1_1_1.i = 0;
+ ______split_137_1_1_1_1_1 = ____split_3_1_1_1.i + 1;
+ _____acc_1_1_1_1_1.i = ______split_137_1_1_1_1_1;
+ ______split_137_2_1_1_1_1 = _____acc_1_1_1_1_1.i + 1;
+ _____acc_2_1_1_1_1.i = ______split_137_2_1_1_1_1;
+ ______split_137_3_1_1_1_1 = _____acc_2_1_1_1_1.i + 1;
+ _____acc_3_1_1_1_1.i = ______split_137_3_1_1_1_1;
+ ______split_137_4_1_1_1_1 = _____acc_3_1_1_1_1.i + 1;
+ _____acc_4_1_1_1_1.i = ______split_137_4_1_1_1_1;
+ ______split_137_5_1_1_1_1 = _____acc_4_1_1_1_1.i + 1;
+ _____acc_5_1_1_1_1.i = ______split_137_5_1_1_1_1;
+ ______split_137_6_1_1_1_1 = _____acc_5_1_1_1_1.i + 1;
+ _____acc_6_1_1_1_1.i = ______split_137_6_1_1_1_1;
+ ______split_137_7_1_1_1_1 = _____acc_6_1_1_1_1.i + 1;
+ _____acc_7_1_1_1_1.i = ______split_137_7_1_1_1_1;
+ ______split_137_8_1_1_1_1 = _____acc_7_1_1_1_1.i + 1;
+ _____acc_8_1_1_1_1.i = ______split_137_8_1_1_1_1;
+ ______split_137_9_1_1_1_1 = _____acc_8_1_1_1_1.i + 1;
+ _____acc_9_1_1_1_1.i = ______split_137_9_1_1_1_1;
+ ______split_137_10_1_1_1_1 = _____acc_9_1_1_1_1.i + 1;
+ _____acc_10_1_1_1_1.i = ______split_137_10_1_1_1_1;
+ ______split_137_11_1_1_1_1 = _____acc_10_1_1_1_1.i + 1;
+ _____acc_11_1_1_1_1.i = ______split_137_11_1_1_1_1;
+ ______split_137_12_1_1_1_1 = _____acc_11_1_1_1_1.i + 1;
+ _____acc_12_1_1_1_1.i = ______split_137_12_1_1_1_1;
+ ______split_137_13_1_1_1_1 = _____acc_12_1_1_1_1.i + 1;
+ _____acc_13_1_1_1_1.i = ______split_137_13_1_1_1_1;
+ ______split_137_14_1_1_1_1 = _____acc_13_1_1_1_1.i + 1;
+ _____acc_14_1_1_1_1.i = ______split_137_14_1_1_1_1;
+ ______split_137_15_1_1_1_1 = _____acc_14_1_1_1_1.i + 1;
+ _____acc_15_1_1_1_1.i = ______split_137_15_1_1_1_1;
+ ______split_137_16_1_1_1_1 = _____acc_15_1_1_1_1.i + 1;
+ _____acc_16_1_1_1_1.i = ______split_137_16_1_1_1_1;
+ ______split_137_17_1_1_1_1 = _____acc_16_1_1_1_1.i + 1;
+ _____acc_17_1_1_1_1.i = ______split_137_17_1_1_1_1;
+ ______split_137_18_1_1_1_1 = _____acc_17_1_1_1_1.i + 1;
+ _____acc_18_1_1_1_1.i = ______split_137_18_1_1_1_1;
+ ______split_137_19_1_1_1_1 = _____acc_18_1_1_1_1.i + 1;
+ _____acc_19_1_1_1_1.i = ______split_137_19_1_1_1_1;
+ ______split_137_20_1_1_1_1 = _____acc_19_1_1_1_1.i + 1;
+ _____acc_20_1_1_1_1.i = ______split_137_20_1_1_1_1;
+ ______split_137_21_1_1_1_1 = _____acc_20_1_1_1_1.i + 1;
+ _____acc_21_1_1_1_1.i = ______split_137_21_1_1_1_1;
+ ______split_137_22_1_1_1_1 = _____acc_21_1_1_1_1.i + 1;
+ _____acc_22_1_1_1_1.i = ______split_137_22_1_1_1_1;
+ ______split_137_23_1_1_1_1 = _____acc_22_1_1_1_1.i + 1;
+ _____acc_23_1_1_1_1.i = ______split_137_23_1_1_1_1;
+ ______split_137_24_1_1_1_1 = _____acc_23_1_1_1_1.i + 1;
+ _____acc_24_1_1_1_1.i = ______split_137_24_1_1_1_1;
+ ______split_137_25_1_1_1_1 = _____acc_24_1_1_1_1.i + 1;
+ _____acc_25_1_1_1_1.i = ______split_137_25_1_1_1_1;
+ ______split_137_26_1_1_1_1 = _____acc_25_1_1_1_1.i + 1;
+ _____acc_26_1_1_1_1.i = ______split_137_26_1_1_1_1;
+ ______split_137_27_1_1_1_1 = _____acc_26_1_1_1_1.i + 1;
+ _____acc_27_1_1_1_1.i = ______split_137_27_1_1_1_1;
+ ______split_137_28_1_1_1_1 = _____acc_27_1_1_1_1.i + 1;
+ _____acc_28_1_1_1_1.i = ______split_137_28_1_1_1_1;
+ ______split_137_29_1_1_1_1 = _____acc_28_1_1_1_1.i + 1;
+ _____acc_29_1_1_1_1.i = ______split_137_29_1_1_1_1;
+ ______split_137_30_1_1_1_1 = _____acc_29_1_1_1_1.i + 1;
+ _____acc_30_1_1_1_1.i = ______split_137_30_1_1_1_1;
+ ______split_137_31_1_1_1_1 = _____acc_30_1_1_1_1.i + 1;
+ _____acc_31_1_1_1_1.i = ______split_137_31_1_1_1_1;
+ ______split_137_32_1_1_1_1 = _____acc_31_1_1_1_1.i + 1;
+ _____acc_32_1_1_1_1.i = ______split_137_32_1_1_1_1;
+ ______split_137_33_1_1_1_1 = _____acc_32_1_1_1_1.i + 1;
+ _____acc_33_1_1_1_1.i = ______split_137_33_1_1_1_1;
+ ______split_137_34_1_1_1_1 = _____acc_33_1_1_1_1.i + 1;
+ _____acc_34_1_1_1_1.i = ______split_137_34_1_1_1_1;
+ ______split_137_35_1_1_1_1 = _____acc_34_1_1_1_1.i + 1;
+ _____acc_35_1_1_1_1.i = ______split_137_35_1_1_1_1;
+ ______split_137_36_1_1_1_1 = _____acc_35_1_1_1_1.i + 1;
+ _____acc_36_1_1_1_1.i = ______split_137_36_1_1_1_1;
+ ______split_137_37_1_1_1_1 = _____acc_36_1_1_1_1.i + 1;
+ _____acc_37_1_1_1_1.i = ______split_137_37_1_1_1_1;
+ ______split_137_38_1_1_1_1 = _____acc_37_1_1_1_1.i + 1;
+ _____acc_38_1_1_1_1.i = ______split_137_38_1_1_1_1;
+ ______split_137_39_1_1_1_1 = _____acc_38_1_1_1_1.i + 1;
+ _____acc_39_1_1_1_1.i = ______split_137_39_1_1_1_1;
+ ______split_137_40_1_1_1_1 = _____acc_39_1_1_1_1.i + 1;
+ _____acc_40_1_1_1_1.i = ______split_137_40_1_1_1_1;
+ ______split_137_41_1_1_1_1 = _____acc_40_1_1_1_1.i + 1;
+ _____acc_41_1_1_1_1.i = ______split_137_41_1_1_1_1;
+ ______split_137_42_1_1_1_1 = _____acc_41_1_1_1_1.i + 1;
+ _____acc_42_1_1_1_1.i = ______split_137_42_1_1_1_1;
+ ______split_137_43_1_1_1_1 = _____acc_42_1_1_1_1.i + 1;
+ _____acc_43_1_1_1_1.i = ______split_137_43_1_1_1_1;
+ ______split_137_44_1_1_1_1 = _____acc_43_1_1_1_1.i + 1;
+ _____acc_44_1_1_1_1.i = ______split_137_44_1_1_1_1;
+ ______split_137_45_1_1_1_1 = _____acc_44_1_1_1_1.i + 1;
+ _____acc_45_1_1_1_1.i = ______split_137_45_1_1_1_1;
+ ______split_137_46_1_1_1_1 = _____acc_45_1_1_1_1.i + 1;
+ _____acc_46_1_1_1_1.i = ______split_137_46_1_1_1_1;
+ ______split_137_47_1_1_1_1 = _____acc_46_1_1_1_1.i + 1;
+ _____acc_47_1_1_1_1.i = ______split_137_47_1_1_1_1;
+ ______split_137_48_1_1_1_1 = _____acc_47_1_1_1_1.i + 1;
+ _____acc_48_1_1_1_1.i = ______split_137_48_1_1_1_1;
+ ______split_137_49_1_1_1_1 = _____acc_48_1_1_1_1.i + 1;
+ _____acc_49_1_1_1_1.i = ______split_137_49_1_1_1_1;
+ Lustre_pre_2_get(&___split_131_1_1,&ctx->Lustre_pre_2_ctx_tab[2]);
+ Lustre_arrow_2_step(0,___split_131_1_1,&___split_132_1_1,&ctx->Lustre_arrow_2_ctx_tab[2]);
+ __i_1_1 = ___split_132_1_1 + 1;
+ ___split_135_1_1 = __i_1_1 % 50;
+ ____split_3_1_1_1.j = ___split_135_1_1;
+ _____acc_1_1_1_1_1.j = ____split_3_1_1_1.j;
+ _____acc_2_1_1_1_1.j = _____acc_1_1_1_1_1.j;
+ _____acc_3_1_1_1_1.j = _____acc_2_1_1_1_1.j;
+ _____acc_4_1_1_1_1.j = _____acc_3_1_1_1_1.j;
+ _____acc_5_1_1_1_1.j = _____acc_4_1_1_1_1.j;
+ _____acc_6_1_1_1_1.j = _____acc_5_1_1_1_1.j;
+ _____acc_7_1_1_1_1.j = _____acc_6_1_1_1_1.j;
+ _____acc_8_1_1_1_1.j = _____acc_7_1_1_1_1.j;
+ _____acc_9_1_1_1_1.j = _____acc_8_1_1_1_1.j;
+ _____acc_10_1_1_1_1.j = _____acc_9_1_1_1_1.j;
+ _____acc_11_1_1_1_1.j = _____acc_10_1_1_1_1.j;
+ _____acc_12_1_1_1_1.j = _____acc_11_1_1_1_1.j;
+ _____acc_13_1_1_1_1.j = _____acc_12_1_1_1_1.j;
+ _____acc_14_1_1_1_1.j = _____acc_13_1_1_1_1.j;
+ _____acc_15_1_1_1_1.j = _____acc_14_1_1_1_1.j;
+ _____acc_16_1_1_1_1.j = _____acc_15_1_1_1_1.j;
+ _____acc_17_1_1_1_1.j = _____acc_16_1_1_1_1.j;
+ _____acc_18_1_1_1_1.j = _____acc_17_1_1_1_1.j;
+ _____acc_19_1_1_1_1.j = _____acc_18_1_1_1_1.j;
+ _____acc_20_1_1_1_1.j = _____acc_19_1_1_1_1.j;
+ _____acc_21_1_1_1_1.j = _____acc_20_1_1_1_1.j;
+ _____acc_22_1_1_1_1.j = _____acc_21_1_1_1_1.j;
+ _____acc_23_1_1_1_1.j = _____acc_22_1_1_1_1.j;
+ _____acc_24_1_1_1_1.j = _____acc_23_1_1_1_1.j;
+ _____acc_25_1_1_1_1.j = _____acc_24_1_1_1_1.j;
+ _____acc_26_1_1_1_1.j = _____acc_25_1_1_1_1.j;
+ _____acc_27_1_1_1_1.j = _____acc_26_1_1_1_1.j;
+ _____acc_28_1_1_1_1.j = _____acc_27_1_1_1_1.j;
+ _____acc_29_1_1_1_1.j = _____acc_28_1_1_1_1.j;
+ _____acc_30_1_1_1_1.j = _____acc_29_1_1_1_1.j;
+ _____acc_31_1_1_1_1.j = _____acc_30_1_1_1_1.j;
+ _____acc_32_1_1_1_1.j = _____acc_31_1_1_1_1.j;
+ _____acc_33_1_1_1_1.j = _____acc_32_1_1_1_1.j;
+ _____acc_34_1_1_1_1.j = _____acc_33_1_1_1_1.j;
+ _____acc_35_1_1_1_1.j = _____acc_34_1_1_1_1.j;
+ _____acc_36_1_1_1_1.j = _____acc_35_1_1_1_1.j;
+ _____acc_37_1_1_1_1.j = _____acc_36_1_1_1_1.j;
+ _____acc_38_1_1_1_1.j = _____acc_37_1_1_1_1.j;
+ _____acc_39_1_1_1_1.j = _____acc_38_1_1_1_1.j;
+ _____acc_40_1_1_1_1.j = _____acc_39_1_1_1_1.j;
+ _____acc_41_1_1_1_1.j = _____acc_40_1_1_1_1.j;
+ _____acc_42_1_1_1_1.j = _____acc_41_1_1_1_1.j;
+ _____acc_43_1_1_1_1.j = _____acc_42_1_1_1_1.j;
+ _____acc_44_1_1_1_1.j = _____acc_43_1_1_1_1.j;
+ _____acc_45_1_1_1_1.j = _____acc_44_1_1_1_1.j;
+ _____acc_46_1_1_1_1.j = _____acc_45_1_1_1_1.j;
+ _____acc_47_1_1_1_1.j = _____acc_46_1_1_1_1.j;
+ _____acc_48_1_1_1_1.j = _____acc_47_1_1_1_1.j;
+ _____acc_49_1_1_1_1.j = _____acc_48_1_1_1_1.j;
+ ______split_136_50_1_1_1_1 = _____acc_49_1_1_1_1.i == _____acc_49_1_1_1_1.j;
+ Lustre_pre_3_get(___split_134_1_1,&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_hat_step(0.1,___split_133_1_1);
+ Lustre_arrow_3_step(___split_133_1_1,___split_134_1_1,__pre_a_1_1,&ctx->Lustre_arrow_3_ctx_tab[1]);
+ _____cell_50_1_1_1_1 = __pre_a_1_1[49];
+ if (______split_136_50_1_1_1_1 == _true) {
+ _____ncell_50_1_1_1_1 = _____acc_49_1_1_1_1.v;
+ } else {
+ _____ncell_50_1_1_1_1 = _____cell_50_1_1_1_1;
+ }
+ __a_2_1[49] = _____ncell_50_1_1_1_1;
+ ______split_136_49_1_1_1_1 = _____acc_48_1_1_1_1.i == _____acc_48_1_1_1_1.j;
+ _____cell_49_1_1_1_1 = __pre_a_1_1[48];
+ if (______split_136_49_1_1_1_1 == _true) {
+ _____ncell_49_1_1_1_1 = _____acc_48_1_1_1_1.v;
+ } else {
+ _____ncell_49_1_1_1_1 = _____cell_49_1_1_1_1;
+ }
+ __a_2_1[48] = _____ncell_49_1_1_1_1;
+ ______split_136_48_1_1_1_1 = _____acc_47_1_1_1_1.i == _____acc_47_1_1_1_1.j;
+ _____cell_48_1_1_1_1 = __pre_a_1_1[47];
+ if (______split_136_48_1_1_1_1 == _true) {
+ _____ncell_48_1_1_1_1 = _____acc_47_1_1_1_1.v;
+ } else {
+ _____ncell_48_1_1_1_1 = _____cell_48_1_1_1_1;
+ }
+ __a_2_1[47] = _____ncell_48_1_1_1_1;
+ ______split_136_47_1_1_1_1 = _____acc_46_1_1_1_1.i == _____acc_46_1_1_1_1.j;
+ _____cell_47_1_1_1_1 = __pre_a_1_1[46];
+ if (______split_136_47_1_1_1_1 == _true) {
+ _____ncell_47_1_1_1_1 = _____acc_46_1_1_1_1.v;
+ } else {
+ _____ncell_47_1_1_1_1 = _____cell_47_1_1_1_1;
+ }
+ __a_2_1[46] = _____ncell_47_1_1_1_1;
+ ______split_136_46_1_1_1_1 = _____acc_45_1_1_1_1.i == _____acc_45_1_1_1_1.j;
+ _____cell_46_1_1_1_1 = __pre_a_1_1[45];
+ if (______split_136_46_1_1_1_1 == _true) {
+ _____ncell_46_1_1_1_1 = _____acc_45_1_1_1_1.v;
+ } else {
+ _____ncell_46_1_1_1_1 = _____cell_46_1_1_1_1;
+ }
+ __a_2_1[45] = _____ncell_46_1_1_1_1;
+ ______split_136_45_1_1_1_1 = _____acc_44_1_1_1_1.i == _____acc_44_1_1_1_1.j;
+ _____cell_45_1_1_1_1 = __pre_a_1_1[44];
+ if (______split_136_45_1_1_1_1 == _true) {
+ _____ncell_45_1_1_1_1 = _____acc_44_1_1_1_1.v;
+ } else {
+ _____ncell_45_1_1_1_1 = _____cell_45_1_1_1_1;
+ }
+ __a_2_1[44] = _____ncell_45_1_1_1_1;
+ ______split_136_44_1_1_1_1 = _____acc_43_1_1_1_1.i == _____acc_43_1_1_1_1.j;
+ _____cell_44_1_1_1_1 = __pre_a_1_1[43];
+ if (______split_136_44_1_1_1_1 == _true) {
+ _____ncell_44_1_1_1_1 = _____acc_43_1_1_1_1.v;
+ } else {
+ _____ncell_44_1_1_1_1 = _____cell_44_1_1_1_1;
+ }
+ __a_2_1[43] = _____ncell_44_1_1_1_1;
+ ______split_136_43_1_1_1_1 = _____acc_42_1_1_1_1.i == _____acc_42_1_1_1_1.j;
+ _____cell_43_1_1_1_1 = __pre_a_1_1[42];
+ if (______split_136_43_1_1_1_1 == _true) {
+ _____ncell_43_1_1_1_1 = _____acc_42_1_1_1_1.v;
+ } else {
+ _____ncell_43_1_1_1_1 = _____cell_43_1_1_1_1;
+ }
+ __a_2_1[42] = _____ncell_43_1_1_1_1;
+ ______split_136_42_1_1_1_1 = _____acc_41_1_1_1_1.i == _____acc_41_1_1_1_1.j;
+ _____cell_42_1_1_1_1 = __pre_a_1_1[41];
+ if (______split_136_42_1_1_1_1 == _true) {
+ _____ncell_42_1_1_1_1 = _____acc_41_1_1_1_1.v;
+ } else {
+ _____ncell_42_1_1_1_1 = _____cell_42_1_1_1_1;
+ }
+ __a_2_1[41] = _____ncell_42_1_1_1_1;
+ ______split_136_41_1_1_1_1 = _____acc_40_1_1_1_1.i == _____acc_40_1_1_1_1.j;
+ _____cell_41_1_1_1_1 = __pre_a_1_1[40];
+ if (______split_136_41_1_1_1_1 == _true) {
+ _____ncell_41_1_1_1_1 = _____acc_40_1_1_1_1.v;
+ } else {
+ _____ncell_41_1_1_1_1 = _____cell_41_1_1_1_1;
+ }
+ __a_2_1[40] = _____ncell_41_1_1_1_1;
+ ______split_136_40_1_1_1_1 = _____acc_39_1_1_1_1.i == _____acc_39_1_1_1_1.j;
+ _____cell_40_1_1_1_1 = __pre_a_1_1[39];
+ if (______split_136_40_1_1_1_1 == _true) {
+ _____ncell_40_1_1_1_1 = _____acc_39_1_1_1_1.v;
+ } else {
+ _____ncell_40_1_1_1_1 = _____cell_40_1_1_1_1;
+ }
+ __a_2_1[39] = _____ncell_40_1_1_1_1;
+ ______split_136_39_1_1_1_1 = _____acc_38_1_1_1_1.i == _____acc_38_1_1_1_1.j;
+ _____cell_39_1_1_1_1 = __pre_a_1_1[38];
+ if (______split_136_39_1_1_1_1 == _true) {
+ _____ncell_39_1_1_1_1 = _____acc_38_1_1_1_1.v;
+ } else {
+ _____ncell_39_1_1_1_1 = _____cell_39_1_1_1_1;
+ }
+ __a_2_1[38] = _____ncell_39_1_1_1_1;
+ ______split_136_38_1_1_1_1 = _____acc_37_1_1_1_1.i == _____acc_37_1_1_1_1.j;
+ _____cell_38_1_1_1_1 = __pre_a_1_1[37];
+ if (______split_136_38_1_1_1_1 == _true) {
+ _____ncell_38_1_1_1_1 = _____acc_37_1_1_1_1.v;
+ } else {
+ _____ncell_38_1_1_1_1 = _____cell_38_1_1_1_1;
+ }
+ __a_2_1[37] = _____ncell_38_1_1_1_1;
+ ______split_136_37_1_1_1_1 = _____acc_36_1_1_1_1.i == _____acc_36_1_1_1_1.j;
+ _____cell_37_1_1_1_1 = __pre_a_1_1[36];
+ if (______split_136_37_1_1_1_1 == _true) {
+ _____ncell_37_1_1_1_1 = _____acc_36_1_1_1_1.v;
+ } else {
+ _____ncell_37_1_1_1_1 = _____cell_37_1_1_1_1;
+ }
+ __a_2_1[36] = _____ncell_37_1_1_1_1;
+ ______split_136_36_1_1_1_1 = _____acc_35_1_1_1_1.i == _____acc_35_1_1_1_1.j;
+ _____cell_36_1_1_1_1 = __pre_a_1_1[35];
+ if (______split_136_36_1_1_1_1 == _true) {
+ _____ncell_36_1_1_1_1 = _____acc_35_1_1_1_1.v;
+ } else {
+ _____ncell_36_1_1_1_1 = _____cell_36_1_1_1_1;
+ }
+ __a_2_1[35] = _____ncell_36_1_1_1_1;
+ ______split_136_35_1_1_1_1 = _____acc_34_1_1_1_1.i == _____acc_34_1_1_1_1.j;
+ _____cell_35_1_1_1_1 = __pre_a_1_1[34];
+ if (______split_136_35_1_1_1_1 == _true) {
+ _____ncell_35_1_1_1_1 = _____acc_34_1_1_1_1.v;
+ } else {
+ _____ncell_35_1_1_1_1 = _____cell_35_1_1_1_1;
+ }
+ __a_2_1[34] = _____ncell_35_1_1_1_1;
+ ______split_136_34_1_1_1_1 = _____acc_33_1_1_1_1.i == _____acc_33_1_1_1_1.j;
+ _____cell_34_1_1_1_1 = __pre_a_1_1[33];
+ if (______split_136_34_1_1_1_1 == _true) {
+ _____ncell_34_1_1_1_1 = _____acc_33_1_1_1_1.v;
+ } else {
+ _____ncell_34_1_1_1_1 = _____cell_34_1_1_1_1;
+ }
+ __a_2_1[33] = _____ncell_34_1_1_1_1;
+ ______split_136_33_1_1_1_1 = _____acc_32_1_1_1_1.i == _____acc_32_1_1_1_1.j;
+ _____cell_33_1_1_1_1 = __pre_a_1_1[32];
+ if (______split_136_33_1_1_1_1 == _true) {
+ _____ncell_33_1_1_1_1 = _____acc_32_1_1_1_1.v;
+ } else {
+ _____ncell_33_1_1_1_1 = _____cell_33_1_1_1_1;
+ }
+ __a_2_1[32] = _____ncell_33_1_1_1_1;
+ ______split_136_32_1_1_1_1 = _____acc_31_1_1_1_1.i == _____acc_31_1_1_1_1.j;
+ _____cell_32_1_1_1_1 = __pre_a_1_1[31];
+ if (______split_136_32_1_1_1_1 == _true) {
+ _____ncell_32_1_1_1_1 = _____acc_31_1_1_1_1.v;
+ } else {
+ _____ncell_32_1_1_1_1 = _____cell_32_1_1_1_1;
+ }
+ __a_2_1[31] = _____ncell_32_1_1_1_1;
+ ______split_136_31_1_1_1_1 = _____acc_30_1_1_1_1.i == _____acc_30_1_1_1_1.j;
+ _____cell_31_1_1_1_1 = __pre_a_1_1[30];
+ if (______split_136_31_1_1_1_1 == _true) {
+ _____ncell_31_1_1_1_1 = _____acc_30_1_1_1_1.v;
+ } else {
+ _____ncell_31_1_1_1_1 = _____cell_31_1_1_1_1;
+ }
+ __a_2_1[30] = _____ncell_31_1_1_1_1;
+ ______split_136_30_1_1_1_1 = _____acc_29_1_1_1_1.i == _____acc_29_1_1_1_1.j;
+ _____cell_30_1_1_1_1 = __pre_a_1_1[29];
+ if (______split_136_30_1_1_1_1 == _true) {
+ _____ncell_30_1_1_1_1 = _____acc_29_1_1_1_1.v;
+ } else {
+ _____ncell_30_1_1_1_1 = _____cell_30_1_1_1_1;
+ }
+ __a_2_1[29] = _____ncell_30_1_1_1_1;
+ ______split_136_29_1_1_1_1 = _____acc_28_1_1_1_1.i == _____acc_28_1_1_1_1.j;
+ _____cell_29_1_1_1_1 = __pre_a_1_1[28];
+ if (______split_136_29_1_1_1_1 == _true) {
+ _____ncell_29_1_1_1_1 = _____acc_28_1_1_1_1.v;
+ } else {
+ _____ncell_29_1_1_1_1 = _____cell_29_1_1_1_1;
+ }
+ __a_2_1[28] = _____ncell_29_1_1_1_1;
+ ______split_136_28_1_1_1_1 = _____acc_27_1_1_1_1.i == _____acc_27_1_1_1_1.j;
+ _____cell_28_1_1_1_1 = __pre_a_1_1[27];
+ if (______split_136_28_1_1_1_1 == _true) {
+ _____ncell_28_1_1_1_1 = _____acc_27_1_1_1_1.v;
+ } else {
+ _____ncell_28_1_1_1_1 = _____cell_28_1_1_1_1;
+ }
+ __a_2_1[27] = _____ncell_28_1_1_1_1;
+ ______split_136_27_1_1_1_1 = _____acc_26_1_1_1_1.i == _____acc_26_1_1_1_1.j;
+ _____cell_27_1_1_1_1 = __pre_a_1_1[26];
+ if (______split_136_27_1_1_1_1 == _true) {
+ _____ncell_27_1_1_1_1 = _____acc_26_1_1_1_1.v;
+ } else {
+ _____ncell_27_1_1_1_1 = _____cell_27_1_1_1_1;
+ }
+ __a_2_1[26] = _____ncell_27_1_1_1_1;
+ ______split_136_26_1_1_1_1 = _____acc_25_1_1_1_1.i == _____acc_25_1_1_1_1.j;
+ _____cell_26_1_1_1_1 = __pre_a_1_1[25];
+ if (______split_136_26_1_1_1_1 == _true) {
+ _____ncell_26_1_1_1_1 = _____acc_25_1_1_1_1.v;
+ } else {
+ _____ncell_26_1_1_1_1 = _____cell_26_1_1_1_1;
+ }
+ __a_2_1[25] = _____ncell_26_1_1_1_1;
+ ______split_136_25_1_1_1_1 = _____acc_24_1_1_1_1.i == _____acc_24_1_1_1_1.j;
+ _____cell_25_1_1_1_1 = __pre_a_1_1[24];
+ if (______split_136_25_1_1_1_1 == _true) {
+ _____ncell_25_1_1_1_1 = _____acc_24_1_1_1_1.v;
+ } else {
+ _____ncell_25_1_1_1_1 = _____cell_25_1_1_1_1;
+ }
+ __a_2_1[24] = _____ncell_25_1_1_1_1;
+ ______split_136_24_1_1_1_1 = _____acc_23_1_1_1_1.i == _____acc_23_1_1_1_1.j;
+ _____cell_24_1_1_1_1 = __pre_a_1_1[23];
+ if (______split_136_24_1_1_1_1 == _true) {
+ _____ncell_24_1_1_1_1 = _____acc_23_1_1_1_1.v;
+ } else {
+ _____ncell_24_1_1_1_1 = _____cell_24_1_1_1_1;
+ }
+ __a_2_1[23] = _____ncell_24_1_1_1_1;
+ ______split_136_23_1_1_1_1 = _____acc_22_1_1_1_1.i == _____acc_22_1_1_1_1.j;
+ _____cell_23_1_1_1_1 = __pre_a_1_1[22];
+ if (______split_136_23_1_1_1_1 == _true) {
+ _____ncell_23_1_1_1_1 = _____acc_22_1_1_1_1.v;
+ } else {
+ _____ncell_23_1_1_1_1 = _____cell_23_1_1_1_1;
+ }
+ __a_2_1[22] = _____ncell_23_1_1_1_1;
+ ______split_136_22_1_1_1_1 = _____acc_21_1_1_1_1.i == _____acc_21_1_1_1_1.j;
+ _____cell_22_1_1_1_1 = __pre_a_1_1[21];
+ if (______split_136_22_1_1_1_1 == _true) {
+ _____ncell_22_1_1_1_1 = _____acc_21_1_1_1_1.v;
+ } else {
+ _____ncell_22_1_1_1_1 = _____cell_22_1_1_1_1;
+ }
+ __a_2_1[21] = _____ncell_22_1_1_1_1;
+ ______split_136_21_1_1_1_1 = _____acc_20_1_1_1_1.i == _____acc_20_1_1_1_1.j;
+ _____cell_21_1_1_1_1 = __pre_a_1_1[20];
+ if (______split_136_21_1_1_1_1 == _true) {
+ _____ncell_21_1_1_1_1 = _____acc_20_1_1_1_1.v;
+ } else {
+ _____ncell_21_1_1_1_1 = _____cell_21_1_1_1_1;
+ }
+ __a_2_1[20] = _____ncell_21_1_1_1_1;
+ ______split_136_20_1_1_1_1 = _____acc_19_1_1_1_1.i == _____acc_19_1_1_1_1.j;
+ _____cell_20_1_1_1_1 = __pre_a_1_1[19];
+ if (______split_136_20_1_1_1_1 == _true) {
+ _____ncell_20_1_1_1_1 = _____acc_19_1_1_1_1.v;
+ } else {
+ _____ncell_20_1_1_1_1 = _____cell_20_1_1_1_1;
+ }
+ __a_2_1[19] = _____ncell_20_1_1_1_1;
+ ______split_136_19_1_1_1_1 = _____acc_18_1_1_1_1.i == _____acc_18_1_1_1_1.j;
+ _____cell_19_1_1_1_1 = __pre_a_1_1[18];
+ if (______split_136_19_1_1_1_1 == _true) {
+ _____ncell_19_1_1_1_1 = _____acc_18_1_1_1_1.v;
+ } else {
+ _____ncell_19_1_1_1_1 = _____cell_19_1_1_1_1;
+ }
+ __a_2_1[18] = _____ncell_19_1_1_1_1;
+ ______split_136_18_1_1_1_1 = _____acc_17_1_1_1_1.i == _____acc_17_1_1_1_1.j;
+ _____cell_18_1_1_1_1 = __pre_a_1_1[17];
+ if (______split_136_18_1_1_1_1 == _true) {
+ _____ncell_18_1_1_1_1 = _____acc_17_1_1_1_1.v;
+ } else {
+ _____ncell_18_1_1_1_1 = _____cell_18_1_1_1_1;
+ }
+ __a_2_1[17] = _____ncell_18_1_1_1_1;
+ ______split_136_17_1_1_1_1 = _____acc_16_1_1_1_1.i == _____acc_16_1_1_1_1.j;
+ _____cell_17_1_1_1_1 = __pre_a_1_1[16];
+ if (______split_136_17_1_1_1_1 == _true) {
+ _____ncell_17_1_1_1_1 = _____acc_16_1_1_1_1.v;
+ } else {
+ _____ncell_17_1_1_1_1 = _____cell_17_1_1_1_1;
+ }
+ __a_2_1[16] = _____ncell_17_1_1_1_1;
+ ______split_136_16_1_1_1_1 = _____acc_15_1_1_1_1.i == _____acc_15_1_1_1_1.j;
+ _____cell_16_1_1_1_1 = __pre_a_1_1[15];
+ if (______split_136_16_1_1_1_1 == _true) {
+ _____ncell_16_1_1_1_1 = _____acc_15_1_1_1_1.v;
+ } else {
+ _____ncell_16_1_1_1_1 = _____cell_16_1_1_1_1;
+ }
+ __a_2_1[15] = _____ncell_16_1_1_1_1;
+ ______split_136_15_1_1_1_1 = _____acc_14_1_1_1_1.i == _____acc_14_1_1_1_1.j;
+ _____cell_15_1_1_1_1 = __pre_a_1_1[14];
+ if (______split_136_15_1_1_1_1 == _true) {
+ _____ncell_15_1_1_1_1 = _____acc_14_1_1_1_1.v;
+ } else {
+ _____ncell_15_1_1_1_1 = _____cell_15_1_1_1_1;
+ }
+ __a_2_1[14] = _____ncell_15_1_1_1_1;
+ ______split_136_14_1_1_1_1 = _____acc_13_1_1_1_1.i == _____acc_13_1_1_1_1.j;
+ _____cell_14_1_1_1_1 = __pre_a_1_1[13];
+ if (______split_136_14_1_1_1_1 == _true) {
+ _____ncell_14_1_1_1_1 = _____acc_13_1_1_1_1.v;
+ } else {
+ _____ncell_14_1_1_1_1 = _____cell_14_1_1_1_1;
+ }
+ __a_2_1[13] = _____ncell_14_1_1_1_1;
+ ______split_136_13_1_1_1_1 = _____acc_12_1_1_1_1.i == _____acc_12_1_1_1_1.j;
+ _____cell_13_1_1_1_1 = __pre_a_1_1[12];
+ if (______split_136_13_1_1_1_1 == _true) {
+ _____ncell_13_1_1_1_1 = _____acc_12_1_1_1_1.v;
+ } else {
+ _____ncell_13_1_1_1_1 = _____cell_13_1_1_1_1;
+ }
+ __a_2_1[12] = _____ncell_13_1_1_1_1;
+ ______split_136_12_1_1_1_1 = _____acc_11_1_1_1_1.i == _____acc_11_1_1_1_1.j;
+ _____cell_12_1_1_1_1 = __pre_a_1_1[11];
+ if (______split_136_12_1_1_1_1 == _true) {
+ _____ncell_12_1_1_1_1 = _____acc_11_1_1_1_1.v;
+ } else {
+ _____ncell_12_1_1_1_1 = _____cell_12_1_1_1_1;
+ }
+ __a_2_1[11] = _____ncell_12_1_1_1_1;
+ ______split_136_11_1_1_1_1 = _____acc_10_1_1_1_1.i == _____acc_10_1_1_1_1.j;
+ _____cell_11_1_1_1_1 = __pre_a_1_1[10];
+ if (______split_136_11_1_1_1_1 == _true) {
+ _____ncell_11_1_1_1_1 = _____acc_10_1_1_1_1.v;
+ } else {
+ _____ncell_11_1_1_1_1 = _____cell_11_1_1_1_1;
+ }
+ __a_2_1[10] = _____ncell_11_1_1_1_1;
+ ______split_136_10_1_1_1_1 = _____acc_9_1_1_1_1.i == _____acc_9_1_1_1_1.j;
+ _____cell_10_1_1_1_1 = __pre_a_1_1[9];
+ if (______split_136_10_1_1_1_1 == _true) {
+ _____ncell_10_1_1_1_1 = _____acc_9_1_1_1_1.v;
+ } else {
+ _____ncell_10_1_1_1_1 = _____cell_10_1_1_1_1;
+ }
+ __a_2_1[9] = _____ncell_10_1_1_1_1;
+ ______split_136_9_1_1_1_1 = _____acc_8_1_1_1_1.i == _____acc_8_1_1_1_1.j;
+ _____cell_9_1_1_1_1 = __pre_a_1_1[8];
+ if (______split_136_9_1_1_1_1 == _true) {
+ _____ncell_9_1_1_1_1 = _____acc_8_1_1_1_1.v;
+ } else {
+ _____ncell_9_1_1_1_1 = _____cell_9_1_1_1_1;
+ }
+ __a_2_1[8] = _____ncell_9_1_1_1_1;
+ ______split_136_8_1_1_1_1 = _____acc_7_1_1_1_1.i == _____acc_7_1_1_1_1.j;
+ _____cell_8_1_1_1_1 = __pre_a_1_1[7];
+ if (______split_136_8_1_1_1_1 == _true) {
+ _____ncell_8_1_1_1_1 = _____acc_7_1_1_1_1.v;
+ } else {
+ _____ncell_8_1_1_1_1 = _____cell_8_1_1_1_1;
+ }
+ __a_2_1[7] = _____ncell_8_1_1_1_1;
+ ______split_136_7_1_1_1_1 = _____acc_6_1_1_1_1.i == _____acc_6_1_1_1_1.j;
+ _____cell_7_1_1_1_1 = __pre_a_1_1[6];
+ if (______split_136_7_1_1_1_1 == _true) {
+ _____ncell_7_1_1_1_1 = _____acc_6_1_1_1_1.v;
+ } else {
+ _____ncell_7_1_1_1_1 = _____cell_7_1_1_1_1;
+ }
+ __a_2_1[6] = _____ncell_7_1_1_1_1;
+ ______split_136_6_1_1_1_1 = _____acc_5_1_1_1_1.i == _____acc_5_1_1_1_1.j;
+ _____cell_6_1_1_1_1 = __pre_a_1_1[5];
+ if (______split_136_6_1_1_1_1 == _true) {
+ _____ncell_6_1_1_1_1 = _____acc_5_1_1_1_1.v;
+ } else {
+ _____ncell_6_1_1_1_1 = _____cell_6_1_1_1_1;
+ }
+ __a_2_1[5] = _____ncell_6_1_1_1_1;
+ ______split_136_5_1_1_1_1 = _____acc_4_1_1_1_1.i == _____acc_4_1_1_1_1.j;
+ _____cell_5_1_1_1_1 = __pre_a_1_1[4];
+ if (______split_136_5_1_1_1_1 == _true) {
+ _____ncell_5_1_1_1_1 = _____acc_4_1_1_1_1.v;
+ } else {
+ _____ncell_5_1_1_1_1 = _____cell_5_1_1_1_1;
+ }
+ __a_2_1[4] = _____ncell_5_1_1_1_1;
+ ______split_136_4_1_1_1_1 = _____acc_3_1_1_1_1.i == _____acc_3_1_1_1_1.j;
+ _____cell_4_1_1_1_1 = __pre_a_1_1[3];
+ if (______split_136_4_1_1_1_1 == _true) {
+ _____ncell_4_1_1_1_1 = _____acc_3_1_1_1_1.v;
+ } else {
+ _____ncell_4_1_1_1_1 = _____cell_4_1_1_1_1;
+ }
+ __a_2_1[3] = _____ncell_4_1_1_1_1;
+ ______split_136_3_1_1_1_1 = _____acc_2_1_1_1_1.i == _____acc_2_1_1_1_1.j;
+ _____cell_3_1_1_1_1 = __pre_a_1_1[2];
+ if (______split_136_3_1_1_1_1 == _true) {
+ _____ncell_3_1_1_1_1 = _____acc_2_1_1_1_1.v;
+ } else {
+ _____ncell_3_1_1_1_1 = _____cell_3_1_1_1_1;
+ }
+ __a_2_1[2] = _____ncell_3_1_1_1_1;
+ ______split_136_2_1_1_1_1 = _____acc_1_1_1_1_1.i == _____acc_1_1_1_1_1.j;
+ _____cell_2_1_1_1_1 = __pre_a_1_1[1];
+ if (______split_136_2_1_1_1_1 == _true) {
+ _____ncell_2_1_1_1_1 = _____acc_1_1_1_1_1.v;
+ } else {
+ _____ncell_2_1_1_1_1 = _____cell_2_1_1_1_1;
+ }
+ __a_2_1[1] = _____ncell_2_1_1_1_1;
+ ______split_136_1_1_1_1_1 = ____split_3_1_1_1.i == ____split_3_1_1_1.j;
+ _____cell_1_1_1_1_1 = __pre_a_1_1[0];
+ if (______split_136_1_1_1_1_1 == _true) {
+ _____ncell_1_1_1_1_1 = ____split_3_1_1_1.v;
+ } else {
+ _____ncell_1_1_1_1_1 = _____cell_1_1_1_1_1;
+ }
+ __a_2_1[0] = _____ncell_1_1_1_1_1;
+ ___i1_1_1_1 = 0.0;
+ ____acc_50_1_1_1 = ___i1_1_1_1 + __a_2_1[0];
+ ____acc_51_1_1_1 = ____acc_50_1_1_1 + __a_2_1[1];
+ ____acc_52_1_1_1 = ____acc_51_1_1_1 + __a_2_1[2];
+ ____acc_53_1_1_1 = ____acc_52_1_1_1 + __a_2_1[3];
+ ____acc_54_1_1_1 = ____acc_53_1_1_1 + __a_2_1[4];
+ ____acc_55_1_1_1 = ____acc_54_1_1_1 + __a_2_1[5];
+ ____acc_56_1_1_1 = ____acc_55_1_1_1 + __a_2_1[6];
+ ____acc_57_1_1_1 = ____acc_56_1_1_1 + __a_2_1[7];
+ ____acc_58_1_1_1 = ____acc_57_1_1_1 + __a_2_1[8];
+ ____acc_59_1_1_1 = ____acc_58_1_1_1 + __a_2_1[9];
+ ____acc_60_1_1_1 = ____acc_59_1_1_1 + __a_2_1[10];
+ ____acc_61_1_1_1 = ____acc_60_1_1_1 + __a_2_1[11];
+ ____acc_62_1_1_1 = ____acc_61_1_1_1 + __a_2_1[12];
+ ____acc_63_1_1_1 = ____acc_62_1_1_1 + __a_2_1[13];
+ ____acc_64_1_1_1 = ____acc_63_1_1_1 + __a_2_1[14];
+ ____acc_65_1_1_1 = ____acc_64_1_1_1 + __a_2_1[15];
+ ____acc_66_1_1_1 = ____acc_65_1_1_1 + __a_2_1[16];
+ ____acc_67_1_1_1 = ____acc_66_1_1_1 + __a_2_1[17];
+ ____acc_68_1_1_1 = ____acc_67_1_1_1 + __a_2_1[18];
+ ____acc_69_1_1_1 = ____acc_68_1_1_1 + __a_2_1[19];
+ ____acc_70_1_1_1 = ____acc_69_1_1_1 + __a_2_1[20];
+ ____acc_71_1_1_1 = ____acc_70_1_1_1 + __a_2_1[21];
+ ____acc_72_1_1_1 = ____acc_71_1_1_1 + __a_2_1[22];
+ ____acc_73_1_1_1 = ____acc_72_1_1_1 + __a_2_1[23];
+ ____acc_74_1_1_1 = ____acc_73_1_1_1 + __a_2_1[24];
+ ____acc_75_1_1_1 = ____acc_74_1_1_1 + __a_2_1[25];
+ ____acc_76_1_1_1 = ____acc_75_1_1_1 + __a_2_1[26];
+ ____acc_77_1_1_1 = ____acc_76_1_1_1 + __a_2_1[27];
+ ____acc_78_1_1_1 = ____acc_77_1_1_1 + __a_2_1[28];
+ ____acc_79_1_1_1 = ____acc_78_1_1_1 + __a_2_1[29];
+ ____acc_80_1_1_1 = ____acc_79_1_1_1 + __a_2_1[30];
+ ____acc_81_1_1_1 = ____acc_80_1_1_1 + __a_2_1[31];
+ ____acc_82_1_1_1 = ____acc_81_1_1_1 + __a_2_1[32];
+ ____acc_83_1_1_1 = ____acc_82_1_1_1 + __a_2_1[33];
+ ____acc_84_1_1_1 = ____acc_83_1_1_1 + __a_2_1[34];
+ ____acc_85_1_1_1 = ____acc_84_1_1_1 + __a_2_1[35];
+ ____acc_86_1_1_1 = ____acc_85_1_1_1 + __a_2_1[36];
+ ____acc_87_1_1_1 = ____acc_86_1_1_1 + __a_2_1[37];
+ ____acc_88_1_1_1 = ____acc_87_1_1_1 + __a_2_1[38];
+ ____acc_89_1_1_1 = ____acc_88_1_1_1 + __a_2_1[39];
+ ____acc_90_1_1_1 = ____acc_89_1_1_1 + __a_2_1[40];
+ ____acc_91_1_1_1 = ____acc_90_1_1_1 + __a_2_1[41];
+ ____acc_92_1_1_1 = ____acc_91_1_1_1 + __a_2_1[42];
+ ____acc_93_1_1_1 = ____acc_92_1_1_1 + __a_2_1[43];
+ ____acc_94_1_1_1 = ____acc_93_1_1_1 + __a_2_1[44];
+ ____acc_95_1_1_1 = ____acc_94_1_1_1 + __a_2_1[45];
+ ____acc_96_1_1_1 = ____acc_95_1_1_1 + __a_2_1[46];
+ ____acc_97_1_1_1 = ____acc_96_1_1_1 + __a_2_1[47];
+ ____acc_98_1_1_1 = ____acc_97_1_1_1 + __a_2_1[48];
+ __split_84_1 = ____acc_98_1_1_1 + __a_2_1[49];
+ __split_86_1 = __split_84_1;
+ break;
+}
+ ___split_10_2_1 = __x_13_1 < __split_86_1;
+ if (___split_10_2_1 == _true) {
+ _t_3 = __split_86_1;
+ } else {
+ _t_3 = __x_13_1;
+ }
+ Lustre_pre_set(_t_3,&ctx->Lustre_pre_ctx_tab[2]);
+ switch (_TickOrRot_1){
+ case _true:
+ ______split_137_50_1_2_1_1 = _____acc_49_1_2_1_1.i + 1;
+ ___dummy_2_1_1.i = ______split_137_50_1_2_1_1;
+ ___dummy_2_1_1.j = _____acc_49_1_2_1_1.j;
+ ___dummy_2_1_1.v = _____acc_49_1_2_1_1.v;
+ Lustre_pre_3_set(__a_3_1,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_2_set(__i_2_1,&ctx->Lustre_pre_2_ctx_tab[1]);
+ ______split_137_50_1_1_1_1 = _____acc_49_1_1_1_1.i + 1;
+ ___dummy_1_1_1.i = ______split_137_50_1_1_1_1;
+ ___dummy_1_1_1.j = _____acc_49_1_1_1_1.j;
+ ___dummy_1_1_1.v = _____acc_49_1_1_1_1.v;
+ Lustre_pre_3_set(__a_2_1,&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_pre_2_set(__i_1_1,&ctx->Lustre_pre_2_ctx_tab[2]);
+ break;
+}
+ Lustre_slash_step(_d_1,_t_3,&__split_87_1);
+ __split_88_1 = __split_87_1 * 3.6;
+ Lustre_pre_set(__split_88_1,&ctx->Lustre_pre_ctx_tab[3]);
+ Lustre_pre_2_get(&__split_18_1,&ctx->Lustre_pre_2_ctx_tab[3]);
+ Lustre_pre_2_set(_st_2,&ctx->Lustre_pre_2_ctx_tab[3]);
+ Lustre_arrow_2_step(convertible_locked,__split_18_1,&_pst_3,&ctx->Lustre_arrow_2_ctx_tab[3]);
+ switch (_st_2){
+ case convertible_in_motion:
+ _Tick_on_in_motion_1 = Tick;
+ switch (_Tick_on_in_motion_1){
+ case _true:
+ Lustre_pre_get(&___split_33_1_1,&ctx->Lustre_pre_ctx_tab[5]);
+ Lustre_arrow_step(0.0,___split_33_1_1,&__pRoof_Percent_1_1,&ctx->Lustre_arrow_ctx_tab[5]);
+ switch (__st_1_1){
+ case convertible_slow:
+ ___split_47_1_1 = __pRoof_Percent_1_1;
+ ___split_34_1_1 = __pRoof_Percent_1_1;
+ ___split_35_1_1 = 100.0 - ___split_34_1_1;
+ Lustre_slash_step(___split_35_1_1,5.0,&___split_36_1_1);
+ ____presqrt_5_4_1_1 = 1.0;
+ Lustre_slash_step(___split_36_1_1,____presqrt_5_4_1_1,&_____split_120_1_4_1_1);
+ _____split_121_1_4_1_1 = ____presqrt_5_4_1_1 + _____split_120_1_4_1_1;
+ ____sqrt_5_4_1_1 = 0.5 * _____split_121_1_4_1_1;
+ _____split_118_1_4_1_1 = ____presqrt_5_4_1_1 - ____sqrt_5_4_1_1;
+ ______split_2_5_1_4_1_1 = - _____split_118_1_4_1_1;
+ ______split_1_5_1_4_1_1 = _____split_118_1_4_1_1 >= 0.0;
+ if (______split_1_5_1_4_1_1 == _true) {
+ _____split_119_1_4_1_1 = _____split_118_1_4_1_1;
+ } else {
+ _____split_119_1_4_1_1 = ______split_2_5_1_4_1_1;
+ }
+ ____ecart_5_4_1_1 = _____split_119_1_4_1_1 < 0.0005;
+ switch (____ecart_5_4_1_1){
+ case _false:
+ _____split_123_1_4_1_1 = ____sqrt_5_4_1_1;
+ _____split_122_1_4_1_1 = ___split_36_1_1;
+ Lustre_slash_step(_____split_122_1_4_1_1,_____split_123_1_4_1_1,&______split_112_1_1_4_1_1);
+ ______split_113_1_1_4_1_1 = _____split_123_1_4_1_1 + ______split_112_1_1_4_1_1;
+ _____sqrt_4_1_4_1_1 = 0.5 * ______split_113_1_1_4_1_1;
+ ______split_110_1_1_4_1_1 = _____split_123_1_4_1_1 - _____sqrt_4_1_4_1_1;
+ _______split_2_4_1_1_4_1_1 = - ______split_110_1_1_4_1_1;
+ _______split_1_4_1_1_4_1_1 = ______split_110_1_1_4_1_1 >= 0.0;
+ if (_______split_1_4_1_1_4_1_1 == _true) {
+ ______split_111_1_1_4_1_1 = ______split_110_1_1_4_1_1;
+ } else {
+ ______split_111_1_1_4_1_1 = _______split_2_4_1_1_4_1_1;
+ }
+ _____ecart_4_1_4_1_1 = ______split_111_1_1_4_1_1 < 0.0005;
+ switch (_____ecart_4_1_4_1_1){
+ case _false:
+ ______split_115_1_1_4_1_1 = _____sqrt_4_1_4_1_1;
+ ______split_114_1_1_4_1_1 = _____split_122_1_4_1_1;
+ Lustre_slash_step(______split_114_1_1_4_1_1,______split_115_1_1_4_1_1,&_______split_104_1_1_1_4_1_1);
+ _______split_105_1_1_1_4_1_1 = ______split_115_1_1_4_1_1 + _______split_104_1_1_1_4_1_1;
+ ______sqrt_3_1_1_4_1_1 = 0.5 * _______split_105_1_1_1_4_1_1;
+ _______split_102_1_1_1_4_1_1 = ______split_115_1_1_4_1_1 - ______sqrt_3_1_1_4_1_1;
+ ________split_2_3_1_1_1_4_1_1 = - _______split_102_1_1_1_4_1_1;
+ ________split_1_3_1_1_1_4_1_1 = _______split_102_1_1_1_4_1_1 >= 0.0;
+ if (________split_1_3_1_1_1_4_1_1 == _true) {
+ _______split_103_1_1_1_4_1_1 = _______split_102_1_1_1_4_1_1;
+ } else {
+ _______split_103_1_1_1_4_1_1 = ________split_2_3_1_1_1_4_1_1;
+ }
+ ______ecart_3_1_1_4_1_1 = _______split_103_1_1_1_4_1_1 < 0.0005;
+ switch (______ecart_3_1_1_4_1_1){
+ case _false:
+ _______split_107_1_1_1_4_1_1 = ______sqrt_3_1_1_4_1_1;
+ _______split_106_1_1_1_4_1_1 = ______split_114_1_1_4_1_1;
+ Lustre_slash_step(_______split_106_1_1_1_4_1_1,_______split_107_1_1_1_4_1_1,&________split_96_1_1_1_1_4_1_1);
+ ________split_97_1_1_1_1_4_1_1 = _______split_107_1_1_1_4_1_1 + ________split_96_1_1_1_1_4_1_1;
+ _______sqrt_2_1_1_1_4_1_1 = 0.5 * ________split_97_1_1_1_1_4_1_1;
+ ________split_94_1_1_1_1_4_1_1 = _______split_107_1_1_1_4_1_1 - _______sqrt_2_1_1_1_4_1_1;
+ _________split_2_2_1_1_1_1_4_1_1 = - ________split_94_1_1_1_1_4_1_1;
+ _________split_1_2_1_1_1_1_4_1_1 = ________split_94_1_1_1_1_4_1_1 >= 0.0;
+ if (_________split_1_2_1_1_1_1_4_1_1 == _true) {
+ ________split_95_1_1_1_1_4_1_1 = ________split_94_1_1_1_1_4_1_1;
+ } else {
+ ________split_95_1_1_1_1_4_1_1 = _________split_2_2_1_1_1_1_4_1_1;
+ }
+ _______ecart_2_1_1_1_4_1_1 = ________split_95_1_1_1_1_4_1_1 < 0.0005;
+ switch (_______ecart_2_1_1_1_4_1_1){
+ case _false:
+ ________split_98_1_1_1_1_4_1_1 = _______split_106_1_1_1_4_1_1;
+ ________split_99_1_1_1_1_4_1_1 = _______sqrt_2_1_1_1_4_1_1;
+ Lustre_slash_step(________split_98_1_1_1_1_4_1_1,________split_99_1_1_1_1_4_1_1,&_________split_92_1_1_1_1_1_4_1_1);
+ _________split_93_1_1_1_1_1_4_1_1 = ________split_99_1_1_1_1_4_1_1 + _________split_92_1_1_1_1_1_4_1_1;
+ ________sqrt_1_1_1_1_1_4_1_1 = 0.5 * _________split_93_1_1_1_1_1_4_1_1;
+ _______split_108_1_1_1_4_1_1 = ________sqrt_1_1_1_1_1_4_1_1;
+ break;
+ case _true:
+ ________split_101_1_1_1_1_4_1_1 = _______sqrt_2_1_1_1_4_1_1;
+ _______split_108_1_1_1_4_1_1 = ________split_101_1_1_1_1_4_1_1;
+ break;
+}
+ ______split_116_1_1_4_1_1 = _______split_108_1_1_1_4_1_1;
+ break;
+ case _true:
+ _______split_109_1_1_1_4_1_1 = ______sqrt_3_1_1_4_1_1;
+ ______split_116_1_1_4_1_1 = _______split_109_1_1_1_4_1_1;
+ break;
+}
+ _____split_124_1_4_1_1 = ______split_116_1_1_4_1_1;
+ break;
+ case _true:
+ ______split_117_1_1_4_1_1 = _____sqrt_4_1_4_1_1;
+ _____split_124_1_4_1_1 = ______split_117_1_1_4_1_1;
+ break;
+}
+ ___split_37_1_1 = _____split_124_1_4_1_1;
+ break;
+ case _true:
+ _____split_125_1_4_1_1 = ____sqrt_5_4_1_1;
+ ___split_37_1_1 = _____split_125_1_4_1_1;
+ break;
+}
+ ____presqrt_5_3_1_1 = 1.0;
+ Lustre_slash_step(___split_37_1_1,____presqrt_5_3_1_1,&_____split_120_1_3_1_1);
+ _____split_121_1_3_1_1 = ____presqrt_5_3_1_1 + _____split_120_1_3_1_1;
+ ____sqrt_5_3_1_1 = 0.5 * _____split_121_1_3_1_1;
+ _____split_118_1_3_1_1 = ____presqrt_5_3_1_1 - ____sqrt_5_3_1_1;
+ ______split_2_5_1_3_1_1 = - _____split_118_1_3_1_1;
+ ______split_1_5_1_3_1_1 = _____split_118_1_3_1_1 >= 0.0;
+ if (______split_1_5_1_3_1_1 == _true) {
+ _____split_119_1_3_1_1 = _____split_118_1_3_1_1;
+ } else {
+ _____split_119_1_3_1_1 = ______split_2_5_1_3_1_1;
+ }
+ ____ecart_5_3_1_1 = _____split_119_1_3_1_1 < 0.0005;
+ switch (____ecart_5_3_1_1){
+ case _false:
+ _____split_123_1_3_1_1 = ____sqrt_5_3_1_1;
+ _____split_122_1_3_1_1 = ___split_37_1_1;
+ Lustre_slash_step(_____split_122_1_3_1_1,_____split_123_1_3_1_1,&______split_112_1_1_3_1_1);
+ ______split_113_1_1_3_1_1 = _____split_123_1_3_1_1 + ______split_112_1_1_3_1_1;
+ _____sqrt_4_1_3_1_1 = 0.5 * ______split_113_1_1_3_1_1;
+ ______split_110_1_1_3_1_1 = _____split_123_1_3_1_1 - _____sqrt_4_1_3_1_1;
+ _______split_2_4_1_1_3_1_1 = - ______split_110_1_1_3_1_1;
+ _______split_1_4_1_1_3_1_1 = ______split_110_1_1_3_1_1 >= 0.0;
+ if (_______split_1_4_1_1_3_1_1 == _true) {
+ ______split_111_1_1_3_1_1 = ______split_110_1_1_3_1_1;
+ } else {
+ ______split_111_1_1_3_1_1 = _______split_2_4_1_1_3_1_1;
+ }
+ _____ecart_4_1_3_1_1 = ______split_111_1_1_3_1_1 < 0.0005;
+ switch (_____ecart_4_1_3_1_1){
+ case _false:
+ ______split_115_1_1_3_1_1 = _____sqrt_4_1_3_1_1;
+ ______split_114_1_1_3_1_1 = _____split_122_1_3_1_1;
+ Lustre_slash_step(______split_114_1_1_3_1_1,______split_115_1_1_3_1_1,&_______split_104_1_1_1_3_1_1);
+ _______split_105_1_1_1_3_1_1 = ______split_115_1_1_3_1_1 + _______split_104_1_1_1_3_1_1;
+ ______sqrt_3_1_1_3_1_1 = 0.5 * _______split_105_1_1_1_3_1_1;
+ _______split_102_1_1_1_3_1_1 = ______split_115_1_1_3_1_1 - ______sqrt_3_1_1_3_1_1;
+ ________split_2_3_1_1_1_3_1_1 = - _______split_102_1_1_1_3_1_1;
+ ________split_1_3_1_1_1_3_1_1 = _______split_102_1_1_1_3_1_1 >= 0.0;
+ if (________split_1_3_1_1_1_3_1_1 == _true) {
+ _______split_103_1_1_1_3_1_1 = _______split_102_1_1_1_3_1_1;
+ } else {
+ _______split_103_1_1_1_3_1_1 = ________split_2_3_1_1_1_3_1_1;
+ }
+ ______ecart_3_1_1_3_1_1 = _______split_103_1_1_1_3_1_1 < 0.0005;
+ switch (______ecart_3_1_1_3_1_1){
+ case _false:
+ _______split_107_1_1_1_3_1_1 = ______sqrt_3_1_1_3_1_1;
+ _______split_106_1_1_1_3_1_1 = ______split_114_1_1_3_1_1;
+ Lustre_slash_step(_______split_106_1_1_1_3_1_1,_______split_107_1_1_1_3_1_1,&________split_96_1_1_1_1_3_1_1);
+ ________split_97_1_1_1_1_3_1_1 = _______split_107_1_1_1_3_1_1 + ________split_96_1_1_1_1_3_1_1;
+ _______sqrt_2_1_1_1_3_1_1 = 0.5 * ________split_97_1_1_1_1_3_1_1;
+ ________split_94_1_1_1_1_3_1_1 = _______split_107_1_1_1_3_1_1 - _______sqrt_2_1_1_1_3_1_1;
+ _________split_2_2_1_1_1_1_3_1_1 = - ________split_94_1_1_1_1_3_1_1;
+ _________split_1_2_1_1_1_1_3_1_1 = ________split_94_1_1_1_1_3_1_1 >= 0.0;
+ if (_________split_1_2_1_1_1_1_3_1_1 == _true) {
+ ________split_95_1_1_1_1_3_1_1 = ________split_94_1_1_1_1_3_1_1;
+ } else {
+ ________split_95_1_1_1_1_3_1_1 = _________split_2_2_1_1_1_1_3_1_1;
+ }
+ _______ecart_2_1_1_1_3_1_1 = ________split_95_1_1_1_1_3_1_1 < 0.0005;
+ switch (_______ecart_2_1_1_1_3_1_1){
+ case _false:
+ ________split_98_1_1_1_1_3_1_1 = _______split_106_1_1_1_3_1_1;
+ ________split_99_1_1_1_1_3_1_1 = _______sqrt_2_1_1_1_3_1_1;
+ Lustre_slash_step(________split_98_1_1_1_1_3_1_1,________split_99_1_1_1_1_3_1_1,&_________split_92_1_1_1_1_1_3_1_1);
+ _________split_93_1_1_1_1_1_3_1_1 = ________split_99_1_1_1_1_3_1_1 + _________split_92_1_1_1_1_1_3_1_1;
+ ________sqrt_1_1_1_1_1_3_1_1 = 0.5 * _________split_93_1_1_1_1_1_3_1_1;
+ _______split_108_1_1_1_3_1_1 = ________sqrt_1_1_1_1_1_3_1_1;
+ break;
+ case _true:
+ ________split_101_1_1_1_1_3_1_1 = _______sqrt_2_1_1_1_3_1_1;
+ _______split_108_1_1_1_3_1_1 = ________split_101_1_1_1_1_3_1_1;
+ break;
+}
+ ______split_116_1_1_3_1_1 = _______split_108_1_1_1_3_1_1;
+ break;
+ case _true:
+ _______split_109_1_1_1_3_1_1 = ______sqrt_3_1_1_3_1_1;
+ ______split_116_1_1_3_1_1 = _______split_109_1_1_1_3_1_1;
+ break;
+}
+ _____split_124_1_3_1_1 = ______split_116_1_1_3_1_1;
+ break;
+ case _true:
+ ______split_117_1_1_3_1_1 = _____sqrt_4_1_3_1_1;
+ _____split_124_1_3_1_1 = ______split_117_1_1_3_1_1;
+ break;
+}
+ __slow_it_down_1_1 = _____split_124_1_3_1_1;
+ break;
+ case _true:
+ _____split_125_1_3_1_1 = ____sqrt_5_3_1_1;
+ __slow_it_down_1_1 = _____split_125_1_3_1_1;
+ break;
+}
+ break;
+}
+ Lustre_slash_step(5.,0.1,&___split_38_1_1);
+ Lustre_slash_step(100.,___split_38_1_1,&__kh_1_1);
+ switch (__st_1_1){
+ case convertible_slow:
+ ___split_45_1_1 = __kh_1_1;
+ ___split_46_1_1 = __slow_it_down_1_1 * ___split_45_1_1;
+ ___split_48_1_1 = ___split_46_1_1 + ___split_47_1_1;
+ __Roof_Percent_1_1 = ___split_48_1_1;
+ break;
+}
+ ___split_43_1_1 = __kh_1_1 + __pRoof_Percent_1_1;
+ switch (__st_1_1){
+ case convertible_fast:
+ ___split_44_1_1 = ___split_43_1_1;
+ __Roof_Percent_1_1 = ___split_44_1_1;
+ ___split_42_1_1 = 10.0;
+ break;
+ case convertible_wait:
+ __Roof_Percent_1_1 = 0.0;
+ ___split_42_1_1 = 0.0;
+ break;
+ case convertible_slow:
+ ___split_41_1_1 = 10.0 * __slow_it_down_1_1;
+ ___split_42_1_1 = ___split_41_1_1;
+ break;
+}
+ __split_25_1 = ___split_42_1_1;
+ break;
+}
+ Lustre_pre_get(&___split_39_1_1,&ctx->Lustre_pre_ctx_tab[4]);
+ Lustre_arrow_step(0.0,___split_39_1_1,&__pRoof_Speed_1_1,&ctx->Lustre_arrow_ctx_tab[4]);
+ switch (_Tick_on_in_motion_1){
+ case _false:
+ ___split_40_1_1 = __pRoof_Speed_1_1;
+ __split_25_1 = ___split_40_1_1;
+ break;
+}
+ Lustre_pre_set(__split_25_1,&ctx->Lustre_pre_ctx_tab[4]);
+ switch (_Tick_on_in_motion_1){
+ case _true:
+ switch (__st_1_1){
+ case convertible_slow:
+ switch (____ecart_5_3_1_1){
+ case _false:
+ switch (_____ecart_4_1_3_1_1){
+ case _false:
+ switch (______ecart_3_1_1_3_1_1){
+ case _false:
+ switch (_______ecart_2_1_1_1_3_1_1){
+ case _false:
+ _________split_90_1_1_1_1_1_3_1_1 = ________split_99_1_1_1_1_3_1_1 - ________sqrt_1_1_1_1_1_3_1_1;
+ __________split_2_1_1_1_1_1_1_3_1_1 = - _________split_90_1_1_1_1_1_3_1_1;
+ __________split_1_1_1_1_1_1_1_3_1_1 = _________split_90_1_1_1_1_1_3_1_1 >= 0.0;
+ if (__________split_1_1_1_1_1_1_1_3_1_1 == _true) {
+ _________split_91_1_1_1_1_1_3_1_1 = _________split_90_1_1_1_1_1_3_1_1;
+ } else {
+ _________split_91_1_1_1_1_1_3_1_1 = __________split_2_1_1_1_1_1_1_3_1_1;
+ }
+ ________ecart_1_1_1_1_1_3_1_1 = _________split_91_1_1_1_1_1_3_1_1 < 0.0005;
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ switch (____ecart_5_4_1_1){
+ case _false:
+ switch (_____ecart_4_1_4_1_1){
+ case _false:
+ switch (______ecart_3_1_1_4_1_1){
+ case _false:
+ switch (_______ecart_2_1_1_1_4_1_1){
+ case _false:
+ _________split_90_1_1_1_1_1_4_1_1 = ________split_99_1_1_1_1_4_1_1 - ________sqrt_1_1_1_1_1_4_1_1;
+ __________split_2_1_1_1_1_1_1_4_1_1 = - _________split_90_1_1_1_1_1_4_1_1;
+ __________split_1_1_1_1_1_1_1_4_1_1 = _________split_90_1_1_1_1_1_4_1_1 >= 0.0;
+ if (__________split_1_1_1_1_1_1_1_4_1_1 == _true) {
+ _________split_91_1_1_1_1_1_4_1_1 = _________split_90_1_1_1_1_1_4_1_1;
+ } else {
+ _________split_91_1_1_1_1_1_4_1_1 = __________split_2_1_1_1_1_1_1_4_1_1;
+ }
+ ________ecart_1_1_1_1_1_4_1_1 = _________split_91_1_1_1_1_1_4_1_1 < 0.0005;
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ break;
+}
+ Lustre_pre_set(__Roof_Percent_1_1,&ctx->Lustre_pre_ctx_tab[5]);
+ switch (__pst_2_1){
+ case convertible_slow:
+ ___split_30_1_1 = __pRoof_Percent_1_1;
+ ___split_31_1_1 = ___split_30_1_1 < 100.0;
+ if (___split_31_1_1 == _true) {
+ ___split_32_1_1 = convertible_slow;
+ } else {
+ ___split_32_1_1 = convertible_wait;
+ }
+ __st_1_1 = ___split_32_1_1;
+ break;
+ case convertible_fast:
+ ___split_27_1_1 = __pRoof_Percent_1_1;
+ ___split_28_1_1 = ___split_27_1_1 < 85.0;
+ if (___split_28_1_1 == _true) {
+ ___split_29_1_1 = convertible_fast;
+ } else {
+ ___split_29_1_1 = convertible_slow;
+ }
+ __st_1_1 = ___split_29_1_1;
+ break;
+ case convertible_wait:
+ __st_1_1 = convertible_fast;
+ break;
+}
+ Lustre_pre_2_get(&___split_26_1_1,&ctx->Lustre_pre_2_ctx_tab[4]);
+ Lustre_arrow_2_step(convertible_wait,___split_26_1_1,&__pst_2_1,&ctx->Lustre_arrow_2_ctx_tab[4]);
+ Lustre_pre_2_set(__st_1_1,&ctx->Lustre_pre_2_ctx_tab[4]);
+ break;
+}
+ *Roof_Speed = __split_25_1;
+ break;
+ case convertible_locked:
+ __split_24_1 = 0.0;
+ *Roof_Speed = __split_24_1;
+ break;
+}
+ _split_8 = OnOff & Start;
+ _split_9 = ! _split_8;
+
+} // End of convertible_main_step
+
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.h b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.h
new file mode 100644
index 00000000..4785db8a
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main.h
@@ -0,0 +1,52 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2c -en -2cgc -n main convertible.lus */
+/* on vanoise the 09/05/2019 at 15:28:26 */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "lustre_types.h"
+#include "lustre_consts.h"
+
+#ifndef _convertible_main_H_FILE
+#define _convertible_main_H_FILE
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx);
+void Lustre_arrow_2_ctx_init(Lustre_arrow_2_ctx_type* ctx);
+void Lustre_arrow_2_step(_integer ,_integer ,_integer *,Lustre_arrow_2_ctx_type*);
+
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_ctx_init(Lustre_arrow_ctx_type* ctx);
+void Lustre_arrow_step(_real ,_real ,_real *,Lustre_arrow_ctx_type*);
+
+void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx);
+void Lustre_arrow_3_ctx_init(Lustre_arrow_3_ctx_type* ctx);
+void Lustre_arrow_3_step(_real [50],_real [50],_real [50]/*out*/,Lustre_arrow_3_ctx_type*);
+
+void Lustre_hat_step(_real ,_real [50]/*out*/);
+
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx);
+void Lustre_pre_2_ctx_init(Lustre_pre_2_ctx_type* ctx);
+void Lustre_pre_2_get(_integer *,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_2_set(_integer ,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_ctx_init(Lustre_pre_ctx_type* ctx);
+void Lustre_pre_get(_real *,Lustre_pre_ctx_type*);
+
+void Lustre_pre_set(_real ,Lustre_pre_ctx_type*);
+
+void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx);
+void Lustre_pre_3_ctx_init(Lustre_pre_3_ctx_type* ctx);
+void Lustre_pre_3_get(_real [50]/*out*/,Lustre_pre_3_ctx_type*);
+
+void Lustre_pre_3_set(_real [50],Lustre_pre_3_ctx_type*);
+
+void Lustre_slash_step(_real ,_real ,_real *);
+
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx);
+void convertible_main_ctx_init(convertible_main_ctx_type* ctx);
+void convertible_main_step(_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_real ,_boolean *,_boolean *,_real *,_real *,convertible_main_ctx_type*);
+
+/////////////////////////////////////////////////
+#endif
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main_loop.c b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main_loop.c
new file mode 100644
index 00000000..9646b39f
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/convertible_main_loop.c
@@ -0,0 +1,86 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2cgc -node main convertible.lus */
+/* on vanoise the 08/05/2019 at 23:54:11 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "convertible_main.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "1"
+#endif
+#ifndef FF
+#define FF "0"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_boolean _get_bool(char* n){
+ return dm_random_uint32() & 1;
+}
+/*
+_integer _get_int(char* n){
+ return (_integer) (dm_random_uint32() % 21) - 10;
+}
+*/
+_real _get_real(char* n){
+ return ((_integer) (dm_random_uint32() % 2000001) - 1000000)*1E-6;
+}
+/* Output procedures **********************/
+void convertible_main_O_n(void* cdata, _integer _V) {
+}
+
+/* Main procedure *************************/
+int main(){
+ int _s = 0;
+ _boolean Start;
+ _boolean Parked;
+ _boolean Rot;
+ _boolean Tick;
+ _boolean OnOff;
+ _boolean Done;
+ _real Dist;
+ _boolean Danger;
+ _boolean Locked;
+ _real Speed;
+ _real Roof_Speed;
+ convertible_main_ctx_type ctx_struct;
+ convertible_main_ctx_type* ctx = &ctx_struct;
+ convertible_main_ctx_init(ctx);
+ // printf("#inputs \"Start\":bool \"Parked\":bool \"Rot\":bool \"Tick\":bool \"OnOff\":bool \"Done\":bool \"Dist\":real\n");
+ // printf("#outputs \"Danger\":bool \"Locked\":bool \"Speed\":real \"Roof_Speed\":real\n");
+
+ /* Main loop */
+ clock_prepare();
+ clock_start();
+
+ for(int count=0; count<1000; count++){
+ ++_s;
+ Start = _get_bool("Start");
+ Parked = _get_bool("Parked");
+ Rot = _get_bool("Rot");
+ Tick = _get_bool("Tick");
+ OnOff = _get_bool("OnOff");
+ Done = _get_bool("Done");
+ Dist = _get_real("Dist");
+ convertible_main_step(Start,Parked,Rot,Tick,OnOff,Done,Dist,&Danger,&Locked,&Speed,&Roof_Speed,ctx);
+ // printf("%d %d %d %d %d %d %f #outs %d %d %f %f\n",Start,Parked,Rot,Tick,OnOff,Done,Dist,Danger,Locked,Speed,Roof_Speed);
+ // printf("%d %d %f %f\n",Danger,Locked,Speed,Roof_Speed);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+
+}
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.c b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.c
new file mode 100644
index 00000000..18d80805
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.c
@@ -0,0 +1,4 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2c -en -2cgc -n main convertible.lus */
+/* on vanoise the 09/05/2019 at 15:28:26 */
+#include "lustre_consts.h" \ No newline at end of file
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.h b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.h
new file mode 100644
index 00000000..eaa21a15
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_consts.h
@@ -0,0 +1,23 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2c -en -2cgc -n main convertible.lus */
+/* on vanoise the 09/05/2019 at 15:28:26 */
+
+// Constant definitions
+#define convertible_anti_col 2
+#define convertible_deg1 1
+#define convertible_fast 1
+#define convertible_in_motion 1
+#define convertible_k 5500.0
+#define convertible_locked 0
+#define convertible_max_roof_speed 10.0
+#define convertible_no_sol 0
+#define convertible_one_sol 2
+#define convertible_period 0.1
+#define convertible_run 1
+#define convertible_size 50
+#define convertible_slow 2
+#define convertible_speed_max 110.0
+#define convertible_stationnary 0
+#define convertible_two_sol 3
+#define convertible_wait 0
+#define convertible_wheel_girth 1.4
diff --git a/test/monniaux/lustrev6-convertible-en-2cgc/lustre_types.h b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_types.h
new file mode 100644
index 00000000..68127eb6
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible-en-2cgc/lustre_types.h
@@ -0,0 +1,83 @@
+/* This file was generated by lv6 version master.737 (2727a7744111c84f7984634d2bd3ad6f7c6c7ff9). */
+/* lv6 -2c -en -2cgc -n main convertible.lus */
+/* on vanoise the 09/05/2019 at 15:28:26 */
+
+#ifndef _SOC2C_PREDEF_TYPES
+#define _SOC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+// end of _SOC2C_PREDEF_TYPES
+// User typedef
+#ifndef _convertible_main_TYPES
+#define _convertible_main_TYPES
+typedef _integer convertible_eq_case;
+typedef _integer convertible_roof_speed_state;
+typedef _integer convertible_roof_state;
+typedef struct {
+ _integer i;
+ _integer j;
+ _real v;
+ } convertible_update_acc;
+typedef _integer convertible_vehicle_state;
+#endif // enf of _convertible_main_TYPES
+// Memoryless soc ctx typedef
+// Memoryfull soc ctx typedef
+/* Lustre_pre_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory ;
+} Lustre_pre_ctx_type;
+
+/* Lustre_arrow_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_ctx_type;
+
+/* Lustre_pre_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _integer _memory ;
+} Lustre_pre_2_ctx_type;
+
+/* Lustre_arrow_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_2_ctx_type;
+
+/* Lustre_arrow_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_3_ctx_type;
+
+/* Lustre_pre_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory[50] ;
+} Lustre_pre_3_ctx_type;
+
+/* convertible_main_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[2];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[6];
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[5];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[2];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[6];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[5];
+} convertible_main_ctx_type;
+
+// Defining array and extern types assignments
+
+#ifndef _assign_rp50
+#define _assign_rp50(dest, source, size) memcpy(dest, source, size)
+#endif
diff --git a/test/monniaux/lustrev6-convertible/convertible_main.c b/test/monniaux/lustrev6-convertible/convertible_main.c
new file mode 100644
index 00000000..19bc40b9
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/convertible_main.c
@@ -0,0 +1,1251 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+#include "convertible_main.h"
+//// Defining step functions
+// Memory initialisation for Lustre_arrow_4_ctx
+void Lustre_arrow_4_ctx_reset(Lustre_arrow_4_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_4_ctx
+Lustre_arrow_4_ctx_type* Lustre_arrow_4_ctx_new_ctx(){
+
+ Lustre_arrow_4_ctx_type* ctx = (Lustre_arrow_4_ctx_type*)calloc(1, sizeof(Lustre_arrow_4_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_4_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_4_ctx
+void Lustre_arrow_4_step(_integer x,_integer y,_integer *z,Lustre_arrow_4_ctx_type* ctx){ *z = ((ctx->_memory)? x : y);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_4_step
+
+// Memory initialisation for Lustre_arrow_3_ctx
+void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_3_ctx
+Lustre_arrow_3_ctx_type* Lustre_arrow_3_ctx_new_ctx(){
+
+ Lustre_arrow_3_ctx_type* ctx = (Lustre_arrow_3_ctx_type*)calloc(1, sizeof(Lustre_arrow_3_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_3_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_3_ctx
+void Lustre_arrow_3_step(_real x,_real y,_real *z,Lustre_arrow_3_ctx_type* ctx){ *z = ((ctx->_memory)? x : y);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_3_step
+
+// Memory initialisation for Lustre_arrow_2_ctx
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_2_ctx
+Lustre_arrow_2_ctx_type* Lustre_arrow_2_ctx_new_ctx(){
+
+ Lustre_arrow_2_ctx_type* ctx = (Lustre_arrow_2_ctx_type*)calloc(1, sizeof(Lustre_arrow_2_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_2_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_2_ctx
+void Lustre_arrow_2_step(convertible_hood_speed_state x,convertible_hood_speed_state y,convertible_hood_speed_state *z,Lustre_arrow_2_ctx_type* ctx){ *z = ((ctx->_memory)? x : y);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_2_step
+
+// Memory initialisation for Lustre_arrow_ctx
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_ctx
+Lustre_arrow_ctx_type* Lustre_arrow_ctx_new_ctx(){
+
+ Lustre_arrow_ctx_type* ctx = (Lustre_arrow_ctx_type*)calloc(1, sizeof(Lustre_arrow_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_ctx
+void Lustre_arrow_step(convertible_hood_state x,convertible_hood_state y,convertible_hood_state *z,Lustre_arrow_ctx_type* ctx){ *z = ((ctx->_memory)? x : y);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_step
+
+// Memory initialisation for Lustre_arrow_6_ctx
+void Lustre_arrow_6_ctx_reset(Lustre_arrow_6_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_6_ctx
+Lustre_arrow_6_ctx_type* Lustre_arrow_6_ctx_new_ctx(){
+
+ Lustre_arrow_6_ctx_type* ctx = (Lustre_arrow_6_ctx_type*)calloc(1, sizeof(Lustre_arrow_6_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_6_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_6_ctx
+void Lustre_arrow_6_step(convertible_vehicle_state x,convertible_vehicle_state y,convertible_vehicle_state *z,Lustre_arrow_6_ctx_type* ctx){ *z = ((ctx->_memory)? x : y);
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_6_step
+
+// Memory initialisation for Lustre_arrow_5_ctx
+void Lustre_arrow_5_ctx_reset(Lustre_arrow_5_ctx_type* ctx){
+ int _i;
+ ctx->_memory = _true;
+}
+// Memory allocation for Lustre_arrow_5_ctx
+Lustre_arrow_5_ctx_type* Lustre_arrow_5_ctx_new_ctx(){
+
+ Lustre_arrow_5_ctx_type* ctx = (Lustre_arrow_5_ctx_type*)calloc(1, sizeof(Lustre_arrow_5_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_arrow_5_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_arrow_5_ctx
+void Lustre_arrow_5_step(_real x[50],_real y[50],_real z[50]/*out*/,Lustre_arrow_5_ctx_type* ctx){ _assign_rp50(z, ((ctx->_memory)? x : y), sizeof(_real [50]));
+ ctx->_memory = _false;
+
+} // End of Lustre_arrow_5_step
+
+// Step function(s) for Lustre_eq_ctx
+void Lustre_eq_step(_integer x,_integer y,_boolean *z){
+ *z = (x == y);
+
+} // End of Lustre_eq_step
+
+// Step function(s) for Lustre_eq_2_ctx
+void Lustre_eq_2_step(_real x,_real y,_boolean *z){
+ *z = (x == y);
+
+} // End of Lustre_eq_2_step
+
+// Step function(s) for Lustre_eq_3_ctx
+void Lustre_eq_3_step(convertible_hood_state x,convertible_hood_state y,_boolean *z){
+ *z = (x == y);
+
+} // End of Lustre_eq_3_step
+
+// Step function(s) for Lustre_hat_ctx
+void Lustre_hat_step(_real x,_real z[50]/*out*/){
+ z[0] = x;
+ z[1] = x;
+ z[2] = x;
+ z[3] = x;
+ z[4] = x;
+ z[5] = x;
+ z[6] = x;
+ z[7] = x;
+ z[8] = x;
+ z[9] = x;
+ z[10] = x;
+ z[11] = x;
+ z[12] = x;
+ z[13] = x;
+ z[14] = x;
+ z[15] = x;
+ z[16] = x;
+ z[17] = x;
+ z[18] = x;
+ z[19] = x;
+ z[20] = x;
+ z[21] = x;
+ z[22] = x;
+ z[23] = x;
+ z[24] = x;
+ z[25] = x;
+ z[26] = x;
+ z[27] = x;
+ z[28] = x;
+ z[29] = x;
+ z[30] = x;
+ z[31] = x;
+ z[32] = x;
+ z[33] = x;
+ z[34] = x;
+ z[35] = x;
+ z[36] = x;
+ z[37] = x;
+ z[38] = x;
+ z[39] = x;
+ z[40] = x;
+ z[41] = x;
+ z[42] = x;
+ z[43] = x;
+ z[44] = x;
+ z[45] = x;
+ z[46] = x;
+ z[47] = x;
+ z[48] = x;
+ z[49] = x;
+
+} // End of Lustre_hat_step
+
+// Memory initialisation for Lustre_pre_4_ctx
+void Lustre_pre_4_ctx_reset(Lustre_pre_4_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_4_ctx
+Lustre_pre_4_ctx_type* Lustre_pre_4_ctx_new_ctx(){
+
+ Lustre_pre_4_ctx_type* ctx = (Lustre_pre_4_ctx_type*)calloc(1, sizeof(Lustre_pre_4_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_4_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_4_ctx
+void Lustre_pre_4_get(_integer *z,Lustre_pre_4_ctx_type* ctx){
+ *z = ctx->_memory;
+
+} // End of Lustre_pre_4_get
+
+void Lustre_pre_4_set(_integer x,Lustre_pre_4_ctx_type* ctx){
+ ctx->_memory = x;
+
+} // End of Lustre_pre_4_set
+
+// Memory initialisation for Lustre_pre_3_ctx
+void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_3_ctx
+Lustre_pre_3_ctx_type* Lustre_pre_3_ctx_new_ctx(){
+
+ Lustre_pre_3_ctx_type* ctx = (Lustre_pre_3_ctx_type*)calloc(1, sizeof(Lustre_pre_3_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_3_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_3_ctx
+void Lustre_pre_3_get(_real *z,Lustre_pre_3_ctx_type* ctx){
+ *z = ctx->_memory;
+
+} // End of Lustre_pre_3_get
+
+void Lustre_pre_3_set(_real x,Lustre_pre_3_ctx_type* ctx){
+ ctx->_memory = x;
+
+} // End of Lustre_pre_3_set
+
+// Memory initialisation for Lustre_pre_2_ctx
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_2_ctx
+Lustre_pre_2_ctx_type* Lustre_pre_2_ctx_new_ctx(){
+
+ Lustre_pre_2_ctx_type* ctx = (Lustre_pre_2_ctx_type*)calloc(1, sizeof(Lustre_pre_2_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_2_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_2_ctx
+void Lustre_pre_2_get(convertible_hood_speed_state *z,Lustre_pre_2_ctx_type* ctx){
+ *z = ctx->_memory;
+
+} // End of Lustre_pre_2_get
+
+void Lustre_pre_2_set(convertible_hood_speed_state x,Lustre_pre_2_ctx_type* ctx){
+ ctx->_memory = x;
+
+} // End of Lustre_pre_2_set
+
+// Memory initialisation for Lustre_pre_ctx
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_ctx
+Lustre_pre_ctx_type* Lustre_pre_ctx_new_ctx(){
+
+ Lustre_pre_ctx_type* ctx = (Lustre_pre_ctx_type*)calloc(1, sizeof(Lustre_pre_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_ctx
+void Lustre_pre_get(convertible_hood_state *z,Lustre_pre_ctx_type* ctx){
+ *z = ctx->_memory;
+
+} // End of Lustre_pre_get
+
+void Lustre_pre_set(convertible_hood_state x,Lustre_pre_ctx_type* ctx){
+ ctx->_memory = x;
+
+} // End of Lustre_pre_set
+
+// Memory initialisation for Lustre_pre_6_ctx
+void Lustre_pre_6_ctx_reset(Lustre_pre_6_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_6_ctx
+Lustre_pre_6_ctx_type* Lustre_pre_6_ctx_new_ctx(){
+
+ Lustre_pre_6_ctx_type* ctx = (Lustre_pre_6_ctx_type*)calloc(1, sizeof(Lustre_pre_6_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_6_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_6_ctx
+void Lustre_pre_6_get(convertible_vehicle_state *z,Lustre_pre_6_ctx_type* ctx){
+ *z = ctx->_memory;
+
+} // End of Lustre_pre_6_get
+
+void Lustre_pre_6_set(convertible_vehicle_state x,Lustre_pre_6_ctx_type* ctx){
+ ctx->_memory = x;
+
+} // End of Lustre_pre_6_set
+
+// Memory initialisation for Lustre_pre_5_ctx
+void Lustre_pre_5_ctx_reset(Lustre_pre_5_ctx_type* ctx){
+ int _i;
+
+}
+// Memory allocation for Lustre_pre_5_ctx
+Lustre_pre_5_ctx_type* Lustre_pre_5_ctx_new_ctx(){
+
+ Lustre_pre_5_ctx_type* ctx = (Lustre_pre_5_ctx_type*)calloc(1, sizeof(Lustre_pre_5_ctx_type));
+ // ctx->client_data = cdata;
+ Lustre_pre_5_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for Lustre_pre_5_ctx
+void Lustre_pre_5_get(_real z[50]/*out*/,Lustre_pre_5_ctx_type* ctx){
+ _assign_rp50(z, ctx->_memory, sizeof(_real [50]));
+
+} // End of Lustre_pre_5_get
+
+void Lustre_pre_5_set(_real x[50],Lustre_pre_5_ctx_type* ctx){
+ _assign_rp50(ctx->_memory, x, sizeof(_real [50]));
+
+} // End of Lustre_pre_5_set
+
+// Step function(s) for Lustre_slash_ctx
+void Lustre_slash_step(_real x,_real y,_real *z){
+ *z = (x / y);
+
+} // End of Lustre_slash_step
+
+// Step function(s) for assign_50_ctx
+void assign_50_step(_real v,_integer jv,_real t[50],_real nt[50]/*out*/){
+ convertible_update_acc _v_3;
+ convertible_update_acc dummy;
+
+ _v_3.i = 0;
+ _v_3.j = jv;
+ _v_3.v = v;
+ fillred_update_cell_do_50_step(_v_3,t,&dummy,nt);
+
+} // End of assign_50_step
+
+// Step function(s) for convertible_abs_ctx
+void convertible_abs_step(_real x,_real *y){
+ _real _v_2;
+ _boolean _v_1;
+
+ _v_2 = - x;
+ _v_1 = x >= 0.0;
+ if (_v_1 == _true) {
+ *y = x;
+ } else {
+ *y = _v_2;
+ }
+
+} // End of convertible_abs_step
+
+// Step function(s) for convertible_braking_time_ctx
+void convertible_braking_time_step(_real Speed,_real *res){
+ _real _v_4;
+
+ _v_4 = Speed * Speed;
+ Lustre_slash_step(_v_4,5500.0,res);
+
+} // End of convertible_braking_time_step
+
+// Memory initialisation for convertible_hood_ctx
+void convertible_hood_ctx_reset(convertible_hood_ctx_type* ctx){
+ int _i;
+
+ convertible_hood_speed_ctx_reset(&ctx->convertible_hood_speed_ctx_tab[0]);
+ Lustre_pre_ctx_reset(&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_ctx_reset(&ctx->Lustre_arrow_ctx_tab[0]);
+}
+// Memory allocation for convertible_hood_ctx
+convertible_hood_ctx_type* convertible_hood_ctx_new_ctx(){
+
+ convertible_hood_ctx_type* ctx = (convertible_hood_ctx_type*)calloc(1, sizeof(convertible_hood_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_hood_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_hood_ctx
+void convertible_hood_step(_boolean Tic,_boolean Parked,_boolean OnOff,_boolean Done,_boolean *Locked,_real *Hood_Speed,convertible_hood_ctx_type* ctx){ _real _v_12;
+ _real _v_11;
+ convertible_hood_state _v_10;
+ _boolean _v_9;
+ convertible_hood_state _v_8;
+ _boolean _v_7;
+ _boolean _v_6;
+ convertible_hood_state _v_5;
+ convertible_hood_state pst;
+ convertible_hood_state st;
+ _boolean Tic_on_in_motion;
+
+ Lustre_pre_get(&_v_5,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_arrow_step(convertible_locked,_v_5,&pst,&ctx->Lustre_arrow_ctx_tab[0]);
+ switch (pst){
+ case convertible_in_motion:
+ _v_9 = Done;
+ if (_v_9 == _true) {
+ _v_10 = convertible_locked;
+ } else {
+ _v_10 = convertible_in_motion;
+ }
+ st = _v_10;
+ break;
+}
+ _v_6 = OnOff & Parked;
+ switch (pst){
+ case convertible_locked:
+ _v_7 = _v_6;
+ if (_v_7 == _true) {
+ _v_8 = convertible_in_motion;
+ } else {
+ _v_8 = convertible_locked;
+ }
+ st = _v_8;
+ break;
+}
+ Lustre_pre_set(st,&ctx->Lustre_pre_ctx_tab[0]);
+ Lustre_eq_3_step(st,convertible_locked,Locked);
+ switch (st){
+ case convertible_in_motion:
+ Tic_on_in_motion = Tic;
+ convertible_hood_speed_step(Tic_on_in_motion,&_v_12,&ctx->convertible_hood_speed_ctx_tab[0]);
+ *Hood_Speed = _v_12;
+ break;
+ case convertible_locked:
+ _v_11 = 0.0;
+ *Hood_Speed = _v_11;
+ break;
+}
+
+} // End of convertible_hood_step
+
+// Memory initialisation for convertible_hood_speed_ctx
+void convertible_hood_speed_ctx_reset(convertible_hood_speed_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_2_ctx_reset(&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_arrow_2_ctx_reset(&ctx->Lustre_arrow_2_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[1]);
+}
+// Memory allocation for convertible_hood_speed_ctx
+convertible_hood_speed_ctx_type* convertible_hood_speed_ctx_new_ctx(){
+
+ convertible_hood_speed_ctx_type* ctx = (convertible_hood_speed_ctx_type*)calloc(1, sizeof(convertible_hood_speed_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_hood_speed_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_hood_speed_ctx
+void convertible_hood_speed_step(_boolean Tic,_real *Hood_Speed,convertible_hood_speed_ctx_type* ctx){ _real _v_35;
+ _real _v_34;
+ _real _v_33;
+ _real _v_32;
+ _real _v_31;
+ _real _v_30;
+ _real _v_29;
+ _real _v_28;
+ _real _v_27;
+ _real _v_26;
+ _real _v_25;
+ _real _v_24;
+ _real _v_23;
+ _real _v_22;
+ _real _v_21;
+ _real _v_20;
+ convertible_hood_speed_state _v_19;
+ _boolean _v_18;
+ _real _v_17;
+ convertible_hood_speed_state _v_16;
+ _boolean _v_15;
+ _real _v_14;
+ convertible_hood_speed_state _v_13;
+ convertible_hood_speed_state pst;
+ convertible_hood_speed_state st;
+ _real kh;
+ _real Hood_Percent;
+ _real pHood_Percent;
+ _real slow_it_down;
+ _real pHood_Speed;
+
+ switch (Tic){
+ case _true:
+ Lustre_pre_2_get(&_v_13,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_arrow_2_step(convertible_wait,_v_13,&pst,&ctx->Lustre_arrow_2_ctx_tab[0]);
+ Lustre_pre_3_get(&_v_20,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_arrow_3_step(0.0,_v_20,&pHood_Percent,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ switch (pst){
+ case convertible_fast:
+ _v_14 = pHood_Percent;
+ _v_15 = _v_14 < 85.0;
+ if (_v_15 == _true) {
+ _v_16 = convertible_fast;
+ } else {
+ _v_16 = convertible_slow;
+ }
+ st = _v_16;
+ break;
+ case convertible_slow:
+ _v_17 = pHood_Percent;
+ _v_18 = _v_17 < 100.0;
+ if (_v_18 == _true) {
+ _v_19 = convertible_slow;
+ } else {
+ _v_19 = convertible_wait;
+ }
+ st = _v_19;
+ break;
+ case convertible_wait:
+ st = convertible_fast;
+ break;
+}
+ Lustre_pre_2_set(st,&ctx->Lustre_pre_2_ctx_tab[0]);
+ Lustre_slash_step(5.,0.1,&_v_25);
+ Lustre_slash_step(100.,_v_25,&kh);
+ _v_30 = kh + pHood_Percent;
+ switch (st){
+ case convertible_fast:
+ _v_31 = _v_30;
+ Hood_Percent = _v_31;
+ break;
+ case convertible_slow:
+ _v_34 = pHood_Percent;
+ _v_21 = pHood_Percent;
+ _v_22 = 100.0 - _v_21;
+ Lustre_slash_step(_v_22,5.0,&_v_23);
+ convertible_sqrt_step(_v_23,&_v_24);
+ convertible_sqrt_step(_v_24,&slow_it_down);
+ _v_32 = kh;
+ _v_33 = slow_it_down * _v_32;
+ _v_35 = _v_33 + _v_34;
+ Hood_Percent = _v_35;
+ break;
+ case convertible_wait:
+ Hood_Percent = 0.0;
+ Hood_Percent = 0.0;
+ break;
+}
+ Lustre_pre_3_set(Hood_Percent,&ctx->Lustre_pre_3_ctx_tab[0]);
+ break;
+}
+ Lustre_pre_3_get(&_v_26,&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_arrow_3_step(0.0,_v_26,&pHood_Speed,&ctx->Lustre_arrow_3_ctx_tab[1]);
+ switch (Tic){
+ case _false:
+ _v_27 = pHood_Speed;
+ *Hood_Speed = _v_27;
+ break;
+ case _true:
+ switch (st){
+ case convertible_fast:
+ _v_29 = 10.0;
+ _v_29 = 10.0;
+ break;
+ case convertible_slow:
+ _v_28 = 10.0 * slow_it_down;
+ _v_29 = _v_28;
+ break;
+ case convertible_wait:
+ _v_29 = 0.0;
+ _v_29 = 0.0;
+ break;
+}
+ *Hood_Speed = _v_29;
+ break;
+}
+ Lustre_pre_3_set(*Hood_Speed,&ctx->Lustre_pre_3_ctx_tab[1]);
+
+} // End of convertible_hood_speed_step
+
+// Memory initialisation for convertible_main_ctx
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx){
+ int _i;
+
+ convertible_vehicle_ctx_reset(&ctx->convertible_vehicle_ctx_tab[0]);
+ convertible_speed_kmh_ctx_reset(&ctx->convertible_speed_kmh_ctx_tab[0]);
+ convertible_may_collide_ctx_reset(&ctx->convertible_may_collide_ctx_tab[0]);
+ convertible_hood_ctx_reset(&ctx->convertible_hood_ctx_tab[0]);
+}
+// Memory allocation for convertible_main_ctx
+convertible_main_ctx_type* convertible_main_ctx_new_ctx(){
+
+ convertible_main_ctx_type* ctx = (convertible_main_ctx_type*)calloc(1, sizeof(convertible_main_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_main_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_main_ctx
+void convertible_main_step(_boolean Start,_boolean Parked,_boolean Rot,_boolean Tic,_boolean OnOff,_boolean Done,_real Distance,_boolean *Danger,_boolean *Locked,_real *Speed,_real *Hood_Speed,convertible_main_ctx_type* ctx){ _boolean _v_38;
+ _real _v_37;
+ _real _v_36;
+ convertible_vehicle_state St;
+ _boolean _v_39;
+ _boolean _v_40;
+
+ _v_39 = OnOff & Start;
+ _v_40 = ! _v_39;
+ convertible_hood_step(Tic,Parked,OnOff,Done,Locked,Hood_Speed,&ctx->convertible_hood_ctx_tab[0]);
+ convertible_speed_kmh_step(Rot,Tic,Speed,&ctx->convertible_speed_kmh_ctx_tab[0]);
+ convertible_vehicle_step(Start,*Locked,*Speed,Distance,&St,&ctx->convertible_vehicle_ctx_tab[0]);
+ switch (St){
+ case convertible_anti_col:
+ _v_37 = Distance;
+ _v_36 = *Speed;
+ convertible_may_collide_step(_v_36,_v_37,&_v_38,&ctx->convertible_may_collide_ctx_tab[0]);
+ *Danger = _v_38;
+ break;
+ case convertible_run:
+ *Danger = _false;
+ *Danger = _false;
+ break;
+ case convertible_stationnary:
+ *Danger = _false;
+ *Danger = _false;
+ break;
+}
+
+} // End of convertible_main_step
+
+// Step function(s) for convertible_maxr_ctx
+void convertible_maxr_step(_real x,_real y,_real *res){
+ _boolean _v_41;
+
+ _v_41 = x < y;
+ if (_v_41 == _true) {
+ *res = y;
+ } else {
+ *res = x;
+ }
+
+} // End of convertible_maxr_step
+
+// Memory initialisation for convertible_may_collide_ctx
+void convertible_may_collide_ctx_reset(convertible_may_collide_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+}
+// Memory allocation for convertible_may_collide_ctx
+convertible_may_collide_ctx_type* convertible_may_collide_ctx_new_ctx(){
+
+ convertible_may_collide_ctx_type* ctx = (convertible_may_collide_ctx_type*)calloc(1, sizeof(convertible_may_collide_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_may_collide_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_may_collide_ctx
+void convertible_may_collide_step(_real Speed,_real Distance,_boolean *Res,convertible_may_collide_ctx_type* ctx){ _real _v_47;
+ _real _v_46;
+ _real _v_45;
+ _real _v_44;
+ _real _v_43;
+ _real _v_42;
+ _real Accel;
+ _real tChoc;
+ _real tBrake;
+
+ Lustre_pre_3_get(&_v_42,&ctx->Lustre_pre_3_ctx_tab[0]);
+ _v_43 = Speed - _v_42;
+ Lustre_slash_step(_v_43,0.1,&_v_44);
+ Lustre_pre_3_set(Speed,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_arrow_3_step(0.0,_v_44,&Accel,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ convertible_braking_time_step(Speed,&tBrake);
+ _v_46 = -2.0 * Distance;
+ _v_45 = 2.0 * Speed;
+ convertible_solve_eq_d2_step(Accel,_v_45,_v_46,&tChoc);
+ _v_47 = 2.0 + tBrake;
+ *Res = tChoc < _v_47;
+
+} // End of convertible_may_collide_step
+
+// Step function(s) for convertible_ms_to_kmh_ctx
+void convertible_ms_to_kmh_step(_real x,_real *res){
+
+ *res = x * 3.6;
+
+} // End of convertible_ms_to_kmh_step
+
+// Step function(s) for convertible_solve_eq_d2_ctx
+void convertible_solve_eq_d2_step(_real a,_real b,_real c,_real *res){
+ _real _v_75;
+ _real _v_74;
+ _real _v_73;
+ _real _v_72;
+ _real _v_71;
+ _real _v_70;
+ _real _v_69;
+ _real _v_68;
+ _real _v_67;
+ _real _v_66;
+ _real _v_65;
+ _real _v_64;
+ _real _v_63;
+ _real _v_62;
+ _real _v_61;
+ _real _v_60;
+ _real _v_59;
+ _real _v_58;
+ convertible_eq_case _v_57;
+ convertible_eq_case _v_56;
+ _boolean _v_55;
+ _boolean _v_54;
+ convertible_eq_case _v_53;
+ _boolean _v_52;
+ _boolean _v_51;
+ _real _v_50;
+ _real _v_49;
+ _real _v_48;
+ _real delta;
+ convertible_eq_case sol_nb;
+ _real a2;
+ _real b2;
+ _real delta_pos;
+
+ _v_49 = 4.0 * a;
+ _v_50 = _v_49 * c;
+ _v_48 = b * b;
+ delta = _v_48 - _v_50;
+ Lustre_eq_2_step(delta,0.0,&_v_55);
+ if (_v_55 == _true) {
+ _v_56 = convertible_one_sol;
+ } else {
+ _v_56 = convertible_two_sol;
+ }
+ _v_54 = delta < 0.0;
+ if (_v_54 == _true) {
+ _v_57 = convertible_no_sol;
+ } else {
+ _v_57 = _v_56;
+ }
+ Lustre_eq_2_step(b,0.0,&_v_52);
+ if (_v_52 == _true) {
+ _v_53 = convertible_no_sol;
+ } else {
+ _v_53 = convertible_deg1;
+ }
+ Lustre_eq_2_step(a,0.0,&_v_51);
+ if (_v_51 == _true) {
+ sol_nb = _v_53;
+ } else {
+ sol_nb = _v_57;
+ }
+ switch (sol_nb){
+ case convertible_two_sol:
+ delta_pos = delta;
+ a2 = a;
+ b2 = b;
+ convertible_sqrt_step(delta_pos,&_v_66);
+ _v_67 = 2.0 * a2;
+ Lustre_slash_step(_v_66,_v_67,&_v_68);
+ _v_65 = - b2;
+ _v_69 = _v_65 + _v_68;
+ convertible_sqrt_step(delta_pos,&_v_71);
+ _v_72 = 2.0 * a2;
+ Lustre_slash_step(_v_71,_v_72,&_v_73);
+ _v_70 = - b2;
+ _v_74 = _v_70 - _v_73;
+ convertible_maxr_step(_v_69,_v_74,&_v_75);
+ break;
+}
+ _v_61 = - b;
+ _v_62 = 2.0 * a;
+ Lustre_slash_step(_v_61,_v_62,&_v_63);
+ switch (sol_nb){
+ case convertible_one_sol:
+ _v_64 = _v_63;
+ break;
+}
+ _v_58 = - c;
+ Lustre_slash_step(_v_58,b,&_v_59);
+ switch (sol_nb){
+ case convertible_deg1:
+ _v_60 = _v_59;
+ *res = _v_60;
+ break;
+ case convertible_two_sol:
+ *res = _v_75;
+ break;
+ case convertible_one_sol:
+ *res = _v_64;
+ break;
+ case convertible_no_sol:
+ *res = -1.0;
+ *res = -1.0;
+ break;
+}
+
+} // End of convertible_solve_eq_d2_step
+
+// Memory initialisation for convertible_speed_kmh_ctx
+void convertible_speed_kmh_ctx_reset(convertible_speed_kmh_ctx_type* ctx){
+ int _i;
+
+ sum_50_0d1_ctx_reset(&ctx->sum_50_0d1_ctx_tab[0]);
+ sum_50_0d0_ctx_reset(&ctx->sum_50_0d0_ctx_tab[0]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_pre_3_ctx_reset(&ctx->Lustre_pre_3_ctx_tab[2]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[0]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[1]);
+ Lustre_arrow_3_ctx_reset(&ctx->Lustre_arrow_3_ctx_tab[2]);
+}
+// Memory allocation for convertible_speed_kmh_ctx
+convertible_speed_kmh_ctx_type* convertible_speed_kmh_ctx_new_ctx(){
+
+ convertible_speed_kmh_ctx_type* ctx = (convertible_speed_kmh_ctx_type*)calloc(1, sizeof(convertible_speed_kmh_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_speed_kmh_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_speed_kmh_ctx
+void convertible_speed_kmh_step(_boolean Rot,_boolean Tic,_real *Speed,convertible_speed_kmh_ctx_type* ctx){
+ _real _v_87, _v_86, _v_85, _v_84, _v_83, _v_82, _v_81, _v_80;
+ _real _v_79, _v_78, _v_77, _v_76, d, t, pd, pt, dx, tx;
+ _boolean TicOrRot;
+
+ if (Rot == _true) { dx = 1.4; } else { dx = 0.0; }
+ if (Tic == _true) { tx = 0.1; } else { tx = 0.0; }
+ TicOrRot = Tic | Rot;
+ Lustre_pre_3_get(&_v_76,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_arrow_3_step(0.0,_v_76,&pd,&ctx->Lustre_arrow_3_ctx_tab[0]);
+ switch (TicOrRot){
+ case _false:
+ _v_78 = pd;
+ d = _v_78;
+ break;
+ case _true:
+ _v_79 = dx;
+ sum_50_0d0_step(_v_79,&_v_80,&ctx->sum_50_0d0_ctx_tab[0]);
+ d = _v_80;
+ break;
+ }
+ Lustre_pre_3_set(d,&ctx->Lustre_pre_3_ctx_tab[0]);
+ Lustre_pre_3_get(&_v_77,&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_arrow_3_step(0.0,_v_77,&pt,&ctx->Lustre_arrow_3_ctx_tab[1]);
+ switch (TicOrRot){
+ case _false:
+ _v_83 = pt;
+ _v_84 = _v_83;
+ break;
+ case _true:
+ _v_81 = tx;
+ sum_50_0d1_step(_v_81,&_v_82,&ctx->sum_50_0d1_ctx_tab[0]);
+ _v_84 = _v_82;
+ break;
+ }
+ convertible_maxr_step(0.1,_v_84,&t);
+ Lustre_pre_3_set(t,&ctx->Lustre_pre_3_ctx_tab[1]);
+ Lustre_pre_3_get(&_v_87,&ctx->Lustre_pre_3_ctx_tab[2]);
+ Lustre_slash_step(d,t,&_v_85);
+ convertible_ms_to_kmh_step(_v_85,&_v_86);
+ Lustre_pre_3_set(_v_86,&ctx->Lustre_pre_3_ctx_tab[2]);
+ Lustre_arrow_3_step(0.0,_v_87,Speed,&ctx->Lustre_arrow_3_ctx_tab[2]);
+
+} // End of convertible_speed_kmh_step
+
+// Step function(s) for convertible_sqrt_ctx
+void convertible_sqrt_step(_real R,_real *Sqrt){
+
+ squareR_5_step(R,1.0,Sqrt);
+
+} // End of convertible_sqrt_step
+
+// Memory initialisation for convertible_vehicle_ctx
+void convertible_vehicle_ctx_reset(convertible_vehicle_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_6_ctx_reset(&ctx->Lustre_pre_6_ctx_tab[0]);
+ Lustre_arrow_6_ctx_reset(&ctx->Lustre_arrow_6_ctx_tab[0]);
+}
+// Memory allocation for convertible_vehicle_ctx
+convertible_vehicle_ctx_type* convertible_vehicle_ctx_new_ctx(){
+
+ convertible_vehicle_ctx_type* ctx = (convertible_vehicle_ctx_type*)calloc(1, sizeof(convertible_vehicle_ctx_type));
+ // ctx->client_data = cdata;
+ convertible_vehicle_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for convertible_vehicle_ctx
+void convertible_vehicle_step(_boolean Start,_boolean Locked,_real Speed,_real Distance,convertible_vehicle_state *st,convertible_vehicle_ctx_type* ctx){ convertible_vehicle_state _v_153;
+ _boolean _v_152;
+ _boolean _v_151;
+ convertible_vehicle_state _v_150;
+ convertible_vehicle_state _v_149;
+ _boolean _v_148;
+ _boolean _v_147;
+ _boolean _v_146;
+ convertible_vehicle_state _v_145;
+ _boolean _v_144;
+ _boolean _v_143;
+ convertible_vehicle_state _v_142;
+ convertible_vehicle_state pst;
+ _boolean ac_cond;
+
+ Lustre_pre_6_get(&_v_142,&ctx->Lustre_pre_6_ctx_tab[0]);
+ Lustre_arrow_6_step(convertible_stationnary,_v_142,&pst,&ctx->Lustre_arrow_6_ctx_tab[0]);
+ ac_cond = Speed >= 110.0;
+ switch (pst){
+ case convertible_anti_col:
+ _v_151 = ac_cond;
+ _v_152 = ! _v_151;
+ if (_v_152 == _true) {
+ _v_153 = convertible_run;
+ } else {
+ _v_153 = convertible_anti_col;
+ }
+ *st = _v_153;
+ break;
+}
+ Lustre_eq_2_step(Speed,0.0,&_v_147);
+ switch (pst){
+ case convertible_run:
+ _v_148 = _v_147;
+ if (_v_148 == _true) {
+ _v_149 = convertible_stationnary;
+ } else {
+ _v_149 = convertible_run;
+ }
+ _v_146 = ac_cond;
+ if (_v_146 == _true) {
+ _v_150 = convertible_anti_col;
+ } else {
+ _v_150 = _v_149;
+ }
+ *st = _v_150;
+ break;
+}
+ _v_143 = Start & Locked;
+ switch (pst){
+ case convertible_stationnary:
+ _v_144 = _v_143;
+ if (_v_144 == _true) {
+ _v_145 = convertible_run;
+ } else {
+ _v_145 = convertible_stationnary;
+ }
+ *st = _v_145;
+ break;
+}
+ Lustre_pre_6_set(*st,&ctx->Lustre_pre_6_ctx_tab[0]);
+
+} // End of convertible_vehicle_step
+
+// Step function(s) for fillred_update_cell_do_50_ctx
+void fillred_update_cell_do_50_step(convertible_update_acc acc,_real cell[50],convertible_update_acc *nacc,_real ncell[50]/*out*/){
+ int _i;
+ for (_i=0 ; _i<49 ; _i+=2){
+ update_cell_do_50_step(acc,cell[_i],&acc,&ncell[_i]);
+ update_cell_do_50_step(acc,cell[_i+1],&acc,&ncell[_i+1]);
+ }
+ *nacc = acc;
+
+} // End of fillred_update_cell_do_50_step
+
+// Step function(s) for red_rplus_50_real_ctx
+void red_rplus_50_real_step(_real i1,_real i2[50],_real *o){
+ int _i;
+ for (_i=0 ; _i<49 ; _i+=2){
+ i1 = i1 + i2[_i];
+ i1 = i1 + i2[_i+1];
+ }
+ *o = i1;
+
+} // End of red_rplus_50_real_step
+
+// Step function(s) for squareR_1_ctx
+void squareR_1_step(_real x,_real presqrt,_real *Sqrt){
+ _real _v_91;
+ _real _v_90;
+ _real _v_89;
+ _real _v_88;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_v_90);
+ _v_91 = presqrt + _v_90;
+ sqrt = 0.5 * _v_91;
+ _v_88 = presqrt - sqrt;
+ convertible_abs_step(_v_88,&_v_89);
+ ecart = _v_89 < 0.0005;
+ *Sqrt = sqrt;
+
+} // End of squareR_1_step
+
+// Step function(s) for squareR_2_ctx
+void squareR_2_step(_real x,_real presqrt,_real *Sqrt){
+ _real _v_99;
+ _real _v_98;
+ _real _v_97;
+ _real _v_96;
+ _real _v_95;
+ _real _v_94;
+ _real _v_93;
+ _real _v_92;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_v_94);
+ _v_95 = presqrt + _v_94;
+ sqrt = 0.5 * _v_95;
+ _v_92 = presqrt - sqrt;
+ convertible_abs_step(_v_92,&_v_93);
+ ecart = _v_93 < 0.0005;
+ switch (ecart){
+ case _true:
+ _v_99 = sqrt;
+ *Sqrt = _v_99;
+ break;
+ case _false:
+ _v_97 = sqrt;
+ _v_96 = x;
+ squareR_1_step(_v_96,_v_97,&_v_98);
+ *Sqrt = _v_98;
+ break;
+}
+
+} // End of squareR_2_step
+
+// Step function(s) for squareR_3_ctx
+void squareR_3_step(_real x,_real presqrt,_real *Sqrt){
+ _real _v_107;
+ _real _v_106;
+ _real _v_105;
+ _real _v_104;
+ _real _v_103;
+ _real _v_102;
+ _real _v_101;
+ _real _v_100;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_v_102);
+ _v_103 = presqrt + _v_102;
+ sqrt = 0.5 * _v_103;
+ _v_100 = presqrt - sqrt;
+ convertible_abs_step(_v_100,&_v_101);
+ ecart = _v_101 < 0.0005;
+ switch (ecart){
+ case _true:
+ _v_107 = sqrt;
+ *Sqrt = _v_107;
+ break;
+ case _false:
+ _v_105 = sqrt;
+ _v_104 = x;
+ squareR_2_step(_v_104,_v_105,&_v_106);
+ *Sqrt = _v_106;
+ break;
+}
+
+} // End of squareR_3_step
+
+// Step function(s) for squareR_4_ctx
+void squareR_4_step(_real x,_real presqrt,_real *Sqrt){
+ _real _v_115;
+ _real _v_114;
+ _real _v_113;
+ _real _v_112;
+ _real _v_111;
+ _real _v_110;
+ _real _v_109;
+ _real _v_108;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_v_110);
+ _v_111 = presqrt + _v_110;
+ sqrt = 0.5 * _v_111;
+ _v_108 = presqrt - sqrt;
+ convertible_abs_step(_v_108,&_v_109);
+ ecart = _v_109 < 0.0005;
+ switch (ecart){
+ case _true:
+ _v_115 = sqrt;
+ *Sqrt = _v_115;
+ break;
+ case _false:
+ _v_113 = sqrt;
+ _v_112 = x;
+ squareR_3_step(_v_112,_v_113,&_v_114);
+ *Sqrt = _v_114;
+ break;
+}
+
+} // End of squareR_4_step
+
+// Step function(s) for squareR_5_ctx
+void squareR_5_step(_real x,_real presqrt,_real *Sqrt){
+ _real _v_123;
+ _real _v_122;
+ _real _v_121;
+ _real _v_120;
+ _real _v_119;
+ _real _v_118;
+ _real _v_117;
+ _real _v_116;
+ _real sqrt;
+ _boolean ecart;
+
+ Lustre_slash_step(x,presqrt,&_v_118);
+ _v_119 = presqrt + _v_118;
+ sqrt = 0.5 * _v_119;
+ _v_116 = presqrt - sqrt;
+ convertible_abs_step(_v_116,&_v_117);
+ ecart = _v_117 < 0.0005;
+ switch (ecart){
+ case _true:
+ _v_123 = sqrt;
+ *Sqrt = _v_123;
+ break;
+ case _false:
+ _v_121 = sqrt;
+ _v_120 = x;
+ squareR_4_step(_v_120,_v_121,&_v_122);
+ *Sqrt = _v_122;
+ break;
+}
+
+} // End of squareR_5_step
+
+// Memory initialisation for sum_50_0d0_ctx
+void sum_50_0d0_ctx_reset(sum_50_0d0_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_5_ctx_reset(&ctx->Lustre_pre_5_ctx_tab[0]);
+ Lustre_pre_4_ctx_reset(&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_arrow_5_ctx_reset(&ctx->Lustre_arrow_5_ctx_tab[0]);
+ Lustre_arrow_4_ctx_reset(&ctx->Lustre_arrow_4_ctx_tab[0]);
+}
+// Memory allocation for sum_50_0d0_ctx
+sum_50_0d0_ctx_type* sum_50_0d0_ctx_new_ctx(){
+
+ sum_50_0d0_ctx_type* ctx = (sum_50_0d0_ctx_type*)calloc(1, sizeof(sum_50_0d0_ctx_type));
+ // ctx->client_data = cdata;
+ sum_50_0d0_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for sum_50_0d0_ctx
+void sum_50_0d0_step(_real s,_real *res,sum_50_0d0_ctx_type* ctx){ _integer _v_128;
+ _real _v_127[50];
+ _real _v_126[50];
+ _integer _v_125;
+ _integer _v_124;
+ _real a[50];
+ _real pre_a[50];
+ _integer i;
+
+ Lustre_pre_4_get(&_v_124,&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_arrow_4_step(0,_v_124,&_v_125,&ctx->Lustre_arrow_4_ctx_tab[0]);
+ i = _v_125 + 1;
+ Lustre_pre_4_set(i,&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_pre_5_get(_v_127,&ctx->Lustre_pre_5_ctx_tab[0]);
+ Lustre_hat_step(0.0,_v_126);
+ Lustre_arrow_5_step(_v_126,_v_127,pre_a,&ctx->Lustre_arrow_5_ctx_tab[0]);
+ _v_128 = i % 50;
+ assign_50_step(s,_v_128,pre_a,a);
+ Lustre_pre_5_set(a,&ctx->Lustre_pre_5_ctx_tab[0]);
+ red_rplus_50_real_step(0.0,a,res);
+
+} // End of sum_50_0d0_step
+
+// Memory initialisation for sum_50_0d1_ctx
+void sum_50_0d1_ctx_reset(sum_50_0d1_ctx_type* ctx){
+ int _i;
+
+ Lustre_pre_5_ctx_reset(&ctx->Lustre_pre_5_ctx_tab[0]);
+ Lustre_pre_4_ctx_reset(&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_arrow_5_ctx_reset(&ctx->Lustre_arrow_5_ctx_tab[0]);
+ Lustre_arrow_4_ctx_reset(&ctx->Lustre_arrow_4_ctx_tab[0]);
+}
+// Memory allocation for sum_50_0d1_ctx
+sum_50_0d1_ctx_type* sum_50_0d1_ctx_new_ctx(){
+
+ sum_50_0d1_ctx_type* ctx = (sum_50_0d1_ctx_type*)calloc(1, sizeof(sum_50_0d1_ctx_type));
+ // ctx->client_data = cdata;
+ sum_50_0d1_ctx_reset(ctx);
+ return ctx;
+}
+// Step function(s) for sum_50_0d1_ctx
+void sum_50_0d1_step(_real s,_real *res,sum_50_0d1_ctx_type* ctx){ _integer _v_133;
+ _real _v_132[50];
+ _real _v_131[50];
+ _integer _v_130;
+ _integer _v_129;
+ _real a[50];
+ _real pre_a[50];
+ _integer i;
+
+ Lustre_pre_4_get(&_v_129,&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_arrow_4_step(0,_v_129,&_v_130,&ctx->Lustre_arrow_4_ctx_tab[0]);
+ i = _v_130 + 1;
+ Lustre_pre_4_set(i,&ctx->Lustre_pre_4_ctx_tab[0]);
+ Lustre_pre_5_get(_v_132,&ctx->Lustre_pre_5_ctx_tab[0]);
+ Lustre_hat_step(0.1,_v_131);
+ Lustre_arrow_5_step(_v_131,_v_132,pre_a,&ctx->Lustre_arrow_5_ctx_tab[0]);
+ _v_133 = i % 50;
+ assign_50_step(s,_v_133,pre_a,a);
+ Lustre_pre_5_set(a,&ctx->Lustre_pre_5_ctx_tab[0]);
+ red_rplus_50_real_step(0.0,a,res);
+
+} // End of sum_50_0d1_step
+
+// Step function(s) for update_cell_do_50_ctx
+void update_cell_do_50_step(convertible_update_acc acc,_real cell,convertible_update_acc *nacc,_real *ncell){
+ _integer _v_139;
+ _integer _v_138;
+ _integer _v_140;
+ _real _v_141;
+ _real _v_137;
+ _boolean _v_136;
+ _integer _v_135;
+ _integer _v_134;
+
+ _v_137 = acc.v;
+ _v_134 = acc.i;
+ _v_135 = acc.j;
+ Lustre_eq_step(_v_134,_v_135,&_v_136);
+ if (_v_136 == _true) {
+ *ncell = _v_137;
+ } else {
+ *ncell = cell;
+ }
+ _v_138 = acc.i;
+ _v_139 = _v_138 + 1;
+ _v_140 = acc.j;
+ _v_141 = acc.v;
+ nacc->i = _v_139;
+ nacc->j = _v_140;
+ nacc->v = _v_141;
+
+} // End of update_cell_do_50_step
+
diff --git a/test/monniaux/lustrev6-convertible/convertible_main.h b/test/monniaux/lustrev6-convertible/convertible_main.h
new file mode 100644
index 00000000..6be30a78
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/convertible_main.h
@@ -0,0 +1,287 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "lustre_types.h"
+#include "lustre_consts.h"
+
+#ifndef _convertible_main_H_FILE
+#define _convertible_main_H_FILE
+// Memoryless soc ctx typedef
+// Memoryfull soc ctx typedef
+/* Lustre_pre_ctx */
+typedef struct {
+ /*Memory cell*/
+ convertible_hood_state _memory ;
+} Lustre_pre_ctx_type;
+
+/* Lustre_arrow_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_ctx_type;
+
+/* Lustre_pre_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ convertible_hood_speed_state _memory ;
+} Lustre_pre_2_ctx_type;
+
+/* Lustre_arrow_2_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_2_ctx_type;
+
+/* Lustre_pre_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory ;
+} Lustre_pre_3_ctx_type;
+
+/* Lustre_arrow_3_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_3_ctx_type;
+
+/* convertible_hood_speed_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_2_ctx_type Lustre_pre_2_ctx_tab[1];
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[2];
+ Lustre_arrow_2_ctx_type Lustre_arrow_2_ctx_tab[1];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[2];
+} convertible_hood_speed_ctx_type;
+
+/* convertible_hood_ctx */
+typedef struct {
+ /*INSTANCES*/
+ convertible_hood_speed_ctx_type convertible_hood_speed_ctx_tab[1];
+ Lustre_pre_ctx_type Lustre_pre_ctx_tab[1];
+ Lustre_arrow_ctx_type Lustre_arrow_ctx_tab[1];
+} convertible_hood_ctx_type;
+
+/* Lustre_arrow_4_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_4_ctx_type;
+
+/* Lustre_pre_4_ctx */
+typedef struct {
+ /*Memory cell*/
+ _integer _memory ;
+} Lustre_pre_4_ctx_type;
+
+/* Lustre_pre_5_ctx */
+typedef struct {
+ /*Memory cell*/
+ _real _memory[50] ;
+} Lustre_pre_5_ctx_type;
+
+/* Lustre_arrow_5_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_5_ctx_type;
+
+/* sum_50_0d0_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_5_ctx_type Lustre_pre_5_ctx_tab[1];
+ Lustre_pre_4_ctx_type Lustre_pre_4_ctx_tab[1];
+ Lustre_arrow_5_ctx_type Lustre_arrow_5_ctx_tab[1];
+ Lustre_arrow_4_ctx_type Lustre_arrow_4_ctx_tab[1];
+} sum_50_0d0_ctx_type;
+
+/* sum_50_0d1_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_5_ctx_type Lustre_pre_5_ctx_tab[1];
+ Lustre_pre_4_ctx_type Lustre_pre_4_ctx_tab[1];
+ Lustre_arrow_5_ctx_type Lustre_arrow_5_ctx_tab[1];
+ Lustre_arrow_4_ctx_type Lustre_arrow_4_ctx_tab[1];
+} sum_50_0d1_ctx_type;
+
+/* convertible_speed_kmh_ctx */
+typedef struct {
+ /*INSTANCES*/
+ sum_50_0d1_ctx_type sum_50_0d1_ctx_tab[1];
+ sum_50_0d0_ctx_type sum_50_0d0_ctx_tab[1];
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[3];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[3];
+} convertible_speed_kmh_ctx_type;
+
+/* Lustre_pre_6_ctx */
+typedef struct {
+ /*Memory cell*/
+ convertible_vehicle_state _memory ;
+} Lustre_pre_6_ctx_type;
+
+/* Lustre_arrow_6_ctx */
+typedef struct {
+ /*Memory cell*/
+ _boolean _memory ;
+} Lustre_arrow_6_ctx_type;
+
+/* convertible_vehicle_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_6_ctx_type Lustre_pre_6_ctx_tab[1];
+ Lustre_arrow_6_ctx_type Lustre_arrow_6_ctx_tab[1];
+} convertible_vehicle_ctx_type;
+
+/* convertible_may_collide_ctx */
+typedef struct {
+ /*INSTANCES*/
+ Lustre_pre_3_ctx_type Lustre_pre_3_ctx_tab[1];
+ Lustre_arrow_3_ctx_type Lustre_arrow_3_ctx_tab[1];
+} convertible_may_collide_ctx_type;
+
+/* convertible_main_ctx */
+typedef struct {
+ /*INSTANCES*/
+ convertible_vehicle_ctx_type convertible_vehicle_ctx_tab[1];
+ convertible_speed_kmh_ctx_type convertible_speed_kmh_ctx_tab[1];
+ convertible_may_collide_ctx_type convertible_may_collide_ctx_tab[1];
+ convertible_hood_ctx_type convertible_hood_ctx_tab[1];
+} convertible_main_ctx_type;
+
+void Lustre_arrow_4_ctx_reset(Lustre_arrow_4_ctx_type* ctx);
+Lustre_arrow_4_ctx_type* Lustre_arrow_4_ctx_new_ctx();
+void Lustre_arrow_4_step(_integer ,_integer ,_integer *,Lustre_arrow_4_ctx_type*);
+
+void Lustre_arrow_3_ctx_reset(Lustre_arrow_3_ctx_type* ctx);
+Lustre_arrow_3_ctx_type* Lustre_arrow_3_ctx_new_ctx();
+void Lustre_arrow_3_step(_real ,_real ,_real *,Lustre_arrow_3_ctx_type*);
+
+void Lustre_arrow_2_ctx_reset(Lustre_arrow_2_ctx_type* ctx);
+Lustre_arrow_2_ctx_type* Lustre_arrow_2_ctx_new_ctx();
+void Lustre_arrow_2_step(convertible_hood_speed_state ,convertible_hood_speed_state ,convertible_hood_speed_state *,Lustre_arrow_2_ctx_type*);
+
+void Lustre_arrow_ctx_reset(Lustre_arrow_ctx_type* ctx);
+Lustre_arrow_ctx_type* Lustre_arrow_ctx_new_ctx();
+void Lustre_arrow_step(convertible_hood_state ,convertible_hood_state ,convertible_hood_state *,Lustre_arrow_ctx_type*);
+
+void Lustre_arrow_6_ctx_reset(Lustre_arrow_6_ctx_type* ctx);
+Lustre_arrow_6_ctx_type* Lustre_arrow_6_ctx_new_ctx();
+void Lustre_arrow_6_step(convertible_vehicle_state ,convertible_vehicle_state ,convertible_vehicle_state *,Lustre_arrow_6_ctx_type*);
+
+void Lustre_arrow_5_ctx_reset(Lustre_arrow_5_ctx_type* ctx);
+Lustre_arrow_5_ctx_type* Lustre_arrow_5_ctx_new_ctx();
+void Lustre_arrow_5_step(_real [50],_real [50],_real [50]/*out*/,Lustre_arrow_5_ctx_type*);
+
+void Lustre_eq_step(_integer ,_integer ,_boolean *);
+
+void Lustre_eq_2_step(_real ,_real ,_boolean *);
+
+void Lustre_eq_3_step(convertible_hood_state ,convertible_hood_state ,_boolean *);
+
+void Lustre_hat_step(_real ,_real [50]/*out*/);
+
+void Lustre_pre_4_ctx_reset(Lustre_pre_4_ctx_type* ctx);
+Lustre_pre_4_ctx_type* Lustre_pre_4_ctx_new_ctx();
+void Lustre_pre_4_get(_integer *,Lustre_pre_4_ctx_type*);
+
+void Lustre_pre_4_set(_integer ,Lustre_pre_4_ctx_type*);
+
+void Lustre_pre_3_ctx_reset(Lustre_pre_3_ctx_type* ctx);
+Lustre_pre_3_ctx_type* Lustre_pre_3_ctx_new_ctx();
+void Lustre_pre_3_get(_real *,Lustre_pre_3_ctx_type*);
+
+void Lustre_pre_3_set(_real ,Lustre_pre_3_ctx_type*);
+
+void Lustre_pre_2_ctx_reset(Lustre_pre_2_ctx_type* ctx);
+Lustre_pre_2_ctx_type* Lustre_pre_2_ctx_new_ctx();
+void Lustre_pre_2_get(convertible_hood_speed_state *,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_2_set(convertible_hood_speed_state ,Lustre_pre_2_ctx_type*);
+
+void Lustre_pre_ctx_reset(Lustre_pre_ctx_type* ctx);
+Lustre_pre_ctx_type* Lustre_pre_ctx_new_ctx();
+void Lustre_pre_get(convertible_hood_state *,Lustre_pre_ctx_type*);
+
+void Lustre_pre_set(convertible_hood_state ,Lustre_pre_ctx_type*);
+
+void Lustre_pre_6_ctx_reset(Lustre_pre_6_ctx_type* ctx);
+Lustre_pre_6_ctx_type* Lustre_pre_6_ctx_new_ctx();
+void Lustre_pre_6_get(convertible_vehicle_state *,Lustre_pre_6_ctx_type*);
+
+void Lustre_pre_6_set(convertible_vehicle_state ,Lustre_pre_6_ctx_type*);
+
+void Lustre_pre_5_ctx_reset(Lustre_pre_5_ctx_type* ctx);
+Lustre_pre_5_ctx_type* Lustre_pre_5_ctx_new_ctx();
+void Lustre_pre_5_get(_real [50]/*out*/,Lustre_pre_5_ctx_type*);
+
+void Lustre_pre_5_set(_real [50],Lustre_pre_5_ctx_type*);
+
+void Lustre_slash_step(_real ,_real ,_real *);
+
+void assign_50_step(_real ,_integer ,_real [50],_real [50]/*out*/);
+
+void convertible_abs_step(_real ,_real *);
+
+void convertible_braking_time_step(_real ,_real *);
+
+void convertible_hood_ctx_reset(convertible_hood_ctx_type* ctx);
+convertible_hood_ctx_type* convertible_hood_ctx_new_ctx();
+void convertible_hood_step(_boolean ,_boolean ,_boolean ,_boolean ,_boolean *,_real *,convertible_hood_ctx_type*);
+
+void convertible_hood_speed_ctx_reset(convertible_hood_speed_ctx_type* ctx);
+convertible_hood_speed_ctx_type* convertible_hood_speed_ctx_new_ctx();
+void convertible_hood_speed_step(_boolean ,_real *,convertible_hood_speed_ctx_type*);
+
+void convertible_main_ctx_reset(convertible_main_ctx_type* ctx);
+convertible_main_ctx_type* convertible_main_ctx_new_ctx();
+void convertible_main_step(_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_boolean ,_real ,_boolean *,_boolean *,_real *,_real *,convertible_main_ctx_type*);
+
+void convertible_maxr_step(_real ,_real ,_real *);
+
+void convertible_may_collide_ctx_reset(convertible_may_collide_ctx_type* ctx);
+convertible_may_collide_ctx_type* convertible_may_collide_ctx_new_ctx();
+void convertible_may_collide_step(_real ,_real ,_boolean *,convertible_may_collide_ctx_type*);
+
+void convertible_ms_to_kmh_step(_real ,_real *);
+
+void convertible_solve_eq_d2_step(_real ,_real ,_real ,_real *);
+
+void convertible_speed_kmh_ctx_reset(convertible_speed_kmh_ctx_type* ctx);
+convertible_speed_kmh_ctx_type* convertible_speed_kmh_ctx_new_ctx();
+void convertible_speed_kmh_step(_boolean ,_boolean ,_real *,convertible_speed_kmh_ctx_type*);
+
+void convertible_sqrt_step(_real ,_real *);
+
+void convertible_vehicle_ctx_reset(convertible_vehicle_ctx_type* ctx);
+convertible_vehicle_ctx_type* convertible_vehicle_ctx_new_ctx();
+void convertible_vehicle_step(_boolean ,_boolean ,_real ,_real ,convertible_vehicle_state *,convertible_vehicle_ctx_type*);
+
+void fillred_update_cell_do_50_step(convertible_update_acc ,_real [50],convertible_update_acc *,_real [50]/*out*/);
+
+void red_rplus_50_real_step(_real ,_real [50],_real *);
+
+void squareR_1_step(_real ,_real ,_real *);
+
+void squareR_2_step(_real ,_real ,_real *);
+
+void squareR_3_step(_real ,_real ,_real *);
+
+void squareR_4_step(_real ,_real ,_real *);
+
+void squareR_5_step(_real ,_real ,_real *);
+
+void sum_50_0d0_ctx_reset(sum_50_0d0_ctx_type* ctx);
+sum_50_0d0_ctx_type* sum_50_0d0_ctx_new_ctx();
+void sum_50_0d0_step(_real ,_real *,sum_50_0d0_ctx_type*);
+
+void sum_50_0d1_ctx_reset(sum_50_0d1_ctx_type* ctx);
+sum_50_0d1_ctx_type* sum_50_0d1_ctx_new_ctx();
+void sum_50_0d1_step(_real ,_real *,sum_50_0d1_ctx_type*);
+
+void update_cell_do_50_step(convertible_update_acc ,_real ,convertible_update_acc *,_real *);
+
+/////////////////////////////////////////////////
+#endif
diff --git a/test/monniaux/lustrev6-convertible/convertible_main_loop.c b/test/monniaux/lustrev6-convertible/convertible_main_loop.c
new file mode 100644
index 00000000..46e94cd1
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/convertible_main_loop.c
@@ -0,0 +1,86 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "convertible_main.h"
+#include "../clock.h"
+#include "../dm_random.c"
+
+/* MACROS DEFINITIONS ****************/
+#ifndef TT
+#define TT "1"
+#endif
+#ifndef FF
+#define FF "0"
+#endif
+#ifndef BB
+#define BB "bottom"
+#endif
+#ifdef CKCHECK
+/* set this macro for testing output clocks */
+#endif
+
+/* Standard Input procedures **************/
+_boolean _get_bool(char* n){
+ return dm_random_uint32() & 1;
+}
+/*
+_integer _get_int(char* n){
+ return (_integer) (dm_random_uint32() % 21) - 10;
+}
+*/
+_real _get_real(char* n){
+ return ((_integer) (dm_random_uint32() % 2000001) - 1000000)*1E-6;
+}
+/* Output procedures **********************/
+void convertible_main_O_n(void* cdata, _integer _V) {
+}
+/* Main procedure *************************/
+int main(){
+ int _s = 0;
+ _boolean Start;
+ _boolean Parked;
+ _boolean Rot;
+ _boolean Tic;
+ _boolean OnOff;
+ _boolean Done;
+ _real Distance;
+ _boolean Danger;
+ _boolean Locked;
+ _real Speed;
+ _real Hood_Speed;
+ convertible_main_ctx_type* ctx = convertible_main_ctx_new_ctx(NULL);
+
+#if 0
+ printf("#inputs \"Start\":bool \"Parked\":bool \"Rot\":bool \"Tic\":bool \"OnOff\":bool \"Done\":bool \"Distance\":real\n");
+ printf("#outputs \"Danger\":bool \"Locked\":bool \"Speed\":real \"Hood_Speed\":real\n");
+#endif
+
+ /* Main loop */
+ clock_prepare();
+ clock_start();
+
+ for(int count=0; count<1000; count++){
+ ++_s;
+ Start = _get_bool("Start");
+ Parked = _get_bool("Parked");
+ Rot = _get_bool("Rot");
+ Tic = _get_bool("Tic");
+ OnOff = _get_bool("OnOff");
+ Done = _get_bool("Done");
+ Distance = _get_real("Distance");
+ convertible_main_step(Start,Parked,Rot,Tic,OnOff,Done,Distance,&Danger,&Locked,&Speed,&Hood_Speed,ctx);
+ // printf("%d %d %d %d %d %d %f #outs %d %d %f %f\n",Start,Parked,Rot,Tic,OnOff,Done,Distance,Danger,Locked,Speed,Hood_Speed);
+ // printf("%d %d %f %f\n",Danger,Locked,Speed,Hood_Speed);
+ }
+
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+
+}
diff --git a/test/monniaux/lustrev6-convertible/lustre_consts.c b/test/monniaux/lustrev6-convertible/lustre_consts.c
new file mode 100644
index 00000000..37bd763b
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/lustre_consts.c
@@ -0,0 +1,4 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+#include "lustre_consts.h" \ No newline at end of file
diff --git a/test/monniaux/lustrev6-convertible/lustre_consts.h b/test/monniaux/lustrev6-convertible/lustre_consts.h
new file mode 100644
index 00000000..72ee469e
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/lustre_consts.h
@@ -0,0 +1,23 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+
+// Constant definitions
+#define convertible_anti_col 2
+#define convertible_deg1 1
+#define convertible_fast 1
+#define convertible_in_motion 1
+#define convertible_k 5500.0
+#define convertible_locked 0
+#define convertible_max_hood_speed 10.0
+#define convertible_no_sol 0
+#define convertible_one_sol 2
+#define convertible_period 0.1
+#define convertible_run 1
+#define convertible_size 50
+#define convertible_slow 2
+#define convertible_speed_max 110.0
+#define convertible_stationnary 0
+#define convertible_two_sol 3
+#define convertible_wait 0
+#define convertible_wheel_girth 1.4
diff --git a/test/monniaux/lustrev6-convertible/lustre_types.h b/test/monniaux/lustrev6-convertible/lustre_types.h
new file mode 100644
index 00000000..e721c12a
--- /dev/null
+++ b/test/monniaux/lustrev6-convertible/lustre_types.h
@@ -0,0 +1,34 @@
+/* This file was generated by lus2lic version master.668 (35901e970a0c377cc36d6437dcbc61beb8001b54). */
+/* lus2lic -2c convertible.lus -n main */
+/* on ovaz the 27/10/2016 at 11:39:07 */
+
+#ifndef _SOC2C_PREDEF_TYPES
+#define _SOC2C_PREDEF_TYPES
+typedef int _boolean;
+typedef int _integer;
+typedef char* _string;
+typedef double _real;
+typedef double _double;
+typedef float _float;
+#define _false 0
+#define _true 1
+#endif
+// end of _SOC2C_PREDEF_TYPES
+// User typedef
+#ifndef _convertible_main_TYPES
+#define _convertible_main_TYPES
+typedef _integer convertible_eq_case;
+typedef _integer convertible_hood_speed_state;
+typedef _integer convertible_hood_state;
+typedef struct {
+ _integer i;
+ _integer j;
+ _real v;
+ } convertible_update_acc;
+typedef _integer convertible_vehicle_state;
+#endif // enf of _convertible_main_TYPES
+// Defining array and extern types assignments
+
+#ifndef _assign_rp50
+#define _assign_rp50(dest, source, size) memcpy(dest, source, size)
+#endif
diff --git a/test/monniaux/madd/madd.c b/test/monniaux/madd/madd.c
new file mode 100644
index 00000000..68f348ad
--- /dev/null
+++ b/test/monniaux/madd/madd.c
@@ -0,0 +1,25 @@
+#include <stdint.h>
+
+uint32_t madd(uint32_t a, uint32_t b, uint32_t c) {
+ return a + b*c;
+}
+
+uint32_t maddimm(uint32_t a, uint32_t b) {
+ return a + b*17113;
+}
+
+uint32_t madd2(uint32_t a, uint32_t b, uint32_t c) {
+ return c + b*a;
+}
+
+uint64_t maddl(uint64_t a, uint64_t b, uint64_t c) {
+ return a + b*c;
+}
+
+uint64_t maddlimm(uint64_t a, uint64_t b) {
+ return a + b*17113;
+}
+
+uint64_t maddl2(uint64_t a, uint64_t b, uint64_t c) {
+ return c + b*a;
+}
diff --git a/test/monniaux/madd/madd_run.c b/test/monniaux/madd/madd_run.c
new file mode 100644
index 00000000..28cdf9b3
--- /dev/null
+++ b/test/monniaux/madd/madd_run.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdint.h>
+
+extern uint32_t madd(uint32_t a, uint32_t b, uint32_t c);
+extern uint64_t maddl(uint64_t a, uint64_t b, uint64_t c);
+
+int main() {
+ unsigned a = 7, b = 3, c = 4;
+ printf("res = %u %lu\n", madd(a, b, c), maddl(a, b, c));
+ return 0;
+}
diff --git a/test/monniaux/many_temporaries/matrix_product.py b/test/monniaux/many_temporaries/matrix_product.py
new file mode 100644
index 00000000..911ca92c
--- /dev/null
+++ b/test/monniaux/many_temporaries/matrix_product.py
@@ -0,0 +1,20 @@
+m=4
+n=5
+p=6
+number_type='double'
+with open('matrix_product.c', 'w') as cfile:
+ cfile.write(f'void matrix_product({number_type} c[{m}][{p}], const {number_type} a[{m}][{n}], const {number_type} b[{n}][{p}]) {{\n')
+ for i in range(m):
+ for j in range(n):
+ for k in range(p):
+ cfile.write(f' const {number_type} p_{i}_{j}_{k} = a[{i}][{j}] * b[{j}][{k}];\n')
+ for i in range(m):
+ for k in range(p):
+ cfile.write(f' c[{i}][{k}] = ')
+ for j in range(n):
+ if j>0:
+ cfile.write(' + ')
+ cfile.write(f'p_{i}_{j}_{k}')
+ cfile.write(';\n')
+ cfile.write('}\n')
+
diff --git a/test/monniaux/many_temporaries/matrix_product2.py b/test/monniaux/many_temporaries/matrix_product2.py
new file mode 100644
index 00000000..7baeb09b
--- /dev/null
+++ b/test/monniaux/many_temporaries/matrix_product2.py
@@ -0,0 +1,24 @@
+m=4
+n=5
+p=6
+number_type='double'
+with open('matrix_product2.c', 'w') as cfile:
+ cfile.write(f'void matrix_product({number_type} c[{m}][{p}], const {number_type} a[{m}][{n}], const {number_type} b[{n}][{p}]) {{\n')
+ for i in range(m):
+ for j in range(n):
+ for k in range(p):
+ cfile.write(f' const {number_type} p_{i}_{j}_{k} = a[{i}][{j}] * b[{j}][{k}];\n')
+ for i in range(m):
+ for k in range(p):
+ cfile.write(f' const {number_type} r_{i}_{k} = ')
+ for j in range(n):
+ if j>0:
+ cfile.write(' + ')
+ cfile.write(f'p_{i}_{j}_{k}')
+ cfile.write(';\n')
+ for i in range(m):
+ for k in range(p):
+ cfile.write(f' c[{i}][{k}] = r_{i}_{k};\n')
+
+ cfile.write('}\n')
+
diff --git a/test/monniaux/math/exceptions.c b/test/monniaux/math/exceptions.c
new file mode 100644
index 00000000..84ed54db
--- /dev/null
+++ b/test/monniaux/math/exceptions.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <fenv.h>
+#include <float.h>
+
+#pragma STDC FENV_ACCESS ON
+
+#if defined(__KVX__) && !defined(__COMPCERT__)
+int fetestexcept(int excepts) {
+ int mask = (K1_SFR_CS_IO_MASK | K1_SFR_CS_DZ_MASK | K1_SFR_CS_OV_MASK | K1_SFR_CS_UN_MASK | K1_SFR_CS_IN_MASK) & excepts;
+ unsigned long long cs = __builtin_kvx_get(K1_SFR_CS);
+ return cs & mask;
+}
+
+int feclearexcept(int excepts) {
+ int mask = (K1_SFR_CS_IO_MASK | K1_SFR_CS_DZ_MASK | K1_SFR_CS_OV_MASK | K1_SFR_CS_UN_MASK | K1_SFR_CS_IN_MASK) & excepts;
+ __builtin_kvx_wfxl(K1_SFR_CS, mask);
+ return 0;
+}
+#endif
+
+double add(double x, double y) {
+ return x+y;
+}
+
+double mul(double x, double y) {
+ return x*y;
+}
+
+float double2float(double x) {
+ return x;
+}
+
+float uint2float(unsigned x) {
+ return x;
+}
+
+double ulong2double(unsigned long x) {
+ return x;
+}
+
+unsigned double2uint(double x) {
+ return x;
+}
+
+int main() {
+ printf("%x\n", fetestexcept(FE_ALL_EXCEPT));
+
+ double v1 = add(3.0, 0.1);
+ printf("%x\n", fetestexcept(FE_ALL_EXCEPT));
+ feclearexcept(FE_INEXACT);
+
+ printf("%x\n", fetestexcept(FE_ALL_EXCEPT));
+ double v2 = mul(DBL_MAX, DBL_MAX);
+ printf("%g %x\n", v2, fetestexcept(FE_ALL_EXCEPT));
+ feclearexcept(FE_ALL_EXCEPT);
+
+ double v3 = mul(DBL_MIN, DBL_MIN);
+ printf("%g %x\n", v3, fetestexcept(FE_ALL_EXCEPT));
+ feclearexcept(FE_ALL_EXCEPT);
+
+ double v4 = double2float(DBL_MAX);
+ printf("%g %x\n", v4, fetestexcept(FE_ALL_EXCEPT));
+ feclearexcept(FE_ALL_EXCEPT);
+
+ float v5 = uint2float(0xC07FDFFFU);
+ printf("%g %x\n", v5, fetestexcept(FE_ALL_EXCEPT)); // BUG 0 should have INEXACT
+ feclearexcept(FE_ALL_EXCEPT);
+
+ double v6 = ulong2double(0x11217763AFF77C7CUL);
+ printf("%g %x\n", v6, fetestexcept(FE_ALL_EXCEPT)); // BUG 0 should have INEXACT
+ feclearexcept(FE_ALL_EXCEPT);
+
+ unsigned v7 = double2uint(-0.25); // softfloat says "0 and inexact" but here we have "0 and overflow" (due to negative input for unsigned?)
+ printf("%u %x\n", v7, fetestexcept(FE_ALL_EXCEPT));
+ feclearexcept(FE_ALL_EXCEPT);
+
+ // +41F.307672C5496EF
+ double d8 = 0x1.307672C5496EFp32;
+ unsigned v8 = double2uint(d8);
+ printf("%g %x %x\n", d8, v8, fetestexcept(FE_ALL_EXCEPT));
+ // BUG reports 307672C5 and inexact, but should report overflow
+ // bug comes from x86 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89175
+ feclearexcept(FE_ALL_EXCEPT);
+}
diff --git a/test/monniaux/math/rounding.c b/test/monniaux/math/rounding.c
new file mode 100644
index 00000000..3ac8faf0
--- /dev/null
+++ b/test/monniaux/math/rounding.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <fenv.h>
+
+#ifdef __KVX__
+#include <mppa_bare_runtime/kvx/registers.h>
+int fesetround(int rounding_mode) {
+ if (rounding_mode < 0 || rounding_mode > 3) return 1;
+ unsigned long long cs = __builtin_kvx_get(K1_SFR_CS);
+ cs = (cs & ~(3 << 16)) | (rounding_mode << 16);
+ __builtin_kvx_set(K1_SFR_CS, cs);
+ return 0;
+}
+
+int fegetround(void) {
+ unsigned long long cs = __builtin_kvx_get(K1_SFR_CS);
+ return (cs >> 16) & 3;
+}
+#endif
+
+#pragma STDC FENV_ACCESS ON
+
+double add(double x) {
+ return x+0.1;
+}
+
+int main() {
+ if (fesetround(FE_DOWNWARD)) {
+ printf("fesetround(FE_DOWNWARD) unsuccessful\n");
+ }
+ double low = add(3.0);
+ if (fesetround(FE_UPWARD)) {
+ printf("fesetround(FE_UPWARD) unsuccessful\n");
+ }
+ double high = add(3.0);
+ printf("%d %a %d\n", low==high, high-low, fegetround());
+}
diff --git a/test/monniaux/math/test_gsl_pow.c b/test/monniaux/math/test_gsl_pow.c
new file mode 100644
index 00000000..54e53889
--- /dev/null
+++ b/test/monniaux/math/test_gsl_pow.c
@@ -0,0 +1,44 @@
+#include <math.h>
+#include <stdio.h>
+
+double gsl_pow_uint(double x, unsigned int n);
+
+double gsl_pow_int(double x, int n)
+{
+ unsigned int un;
+
+ if(n < 0) {
+ x = 1.0/x;
+ un = -n;
+ } else {
+ un = n;
+ }
+
+ return gsl_pow_uint(x, un);
+}
+
+double gsl_pow_uint(double x, unsigned int n)
+{
+ double value = 1.0;
+
+ /* repeated squaring method
+ * returns 0.0^0 = 1.0, so continuous in x
+ */
+ do {
+ if(n & 1) value *= x; /* for n odd */
+ n >>= 1;
+ x *= x;
+ } while (n);
+
+ return value;
+}
+
+int main() {
+ double y, y_expected;
+ for (int n = -9; n < 10; n++) {
+ y = gsl_pow_int (-3.14, n);
+ y_expected = pow (-3.14, n);
+ printf("%d %g %g\n", n, y, y_expected);
+ }
+ return 0;
+}
diff --git a/test/monniaux/mbedtls/download_and_patch.sh b/test/monniaux/mbedtls/download_and_patch.sh
new file mode 100755
index 00000000..32f660fa
--- /dev/null
+++ b/test/monniaux/mbedtls/download_and_patch.sh
@@ -0,0 +1,4 @@
+git clone https://github.com/ARMmbed/mbedtls.git
+cd mbedtls
+git checkout f352f75f6bd5734c8f671323dd6ab32472d5da34
+patch -p1 < ../mbedtls_Kalray.patch
diff --git a/test/monniaux/mbedtls/make.sh b/test/monniaux/mbedtls/make.sh
new file mode 100755
index 00000000..62eb87db
--- /dev/null
+++ b/test/monniaux/mbedtls/make.sh
@@ -0,0 +1,3 @@
+CCOMP=`pwd`/../../../ccomp
+cd mbedtls
+make CC=$CCOMP CFLAGS="-fstruct-passing -fbitfields -Dvolatile= -U__STRICT_ANSI__" WARNING_CFLAGS="-Wall -Wno-c11-extensions -fno-unprototyped" "$@"
diff --git a/test/monniaux/mbedtls/mbedtls_Kalray.patch b/test/monniaux/mbedtls/mbedtls_Kalray.patch
new file mode 100644
index 00000000..594ee7fa
--- /dev/null
+++ b/test/monniaux/mbedtls/mbedtls_Kalray.patch
@@ -0,0 +1,70 @@
+diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
+index f89bf9604..9f8a4faeb 100644
+--- a/include/mbedtls/config.h
++++ b/include/mbedtls/config.h
+@@ -1076,7 +1076,7 @@
+ *
+ * Enable functions that use the filesystem.
+ */
+-#define MBEDTLS_FS_IO
++// DMonniaux #define MBEDTLS_FS_IO
+
+ /**
+ * \def MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
+@@ -1100,7 +1100,8 @@
+ *
+ * Uncomment this macro to disable the built-in platform entropy functions.
+ */
+-//#define MBEDTLS_NO_PLATFORM_ENTROPY
++//DMonniaux
++#define MBEDTLS_NO_PLATFORM_ENTROPY
+
+ /**
+ * \def MBEDTLS_ENTROPY_FORCE_SHA256
+@@ -2441,7 +2442,7 @@
+ *
+ * This module provides networking routines.
+ */
+-#define MBEDTLS_NET_C
++// DMonniaux #define MBEDTLS_NET_C
+
+ /**
+ * \def MBEDTLS_OID_C
+@@ -2838,7 +2839,7 @@
+ *
+ * This module is used by the HAVEGE random number generator.
+ */
+-#define MBEDTLS_TIMING_C
++// DMonniaux #define MBEDTLS_TIMING_C
+
+ /**
+ * \def MBEDTLS_VERSION_C
+diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
+index 1255ff4be..103abe9a8 100644
+--- a/tests/suites/helpers.function
++++ b/tests/suites/helpers.function
+@@ -40,10 +40,10 @@ typedef UINT32 uint32_t;
+ #endif
+
+ #include <string.h>
++#include <strings.h>
+
+ #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
+ #include <unistd.h>
+-#include <strings.h>
+ #endif
+
+ /* Type for Hex parameters */
+diff --git a/tests/scripts/run-test-suites.pl b/tests/scripts/run-test-suites.pl
+index d0d4046..00cff26 100755
+--- a/tests/scripts/run-test-suites.pl
++++ b/tests/scripts/run-test-suites.pl
+@@ -59,7 +59,7 @@ for my $suite (@suites)
+ if( $verbose ) {
+ $command .= ' -v';
+ }
+- my $result = `$command`;
++ my $result = `k1-cluster -- $command`;
+
+ $suite_cases_passed = () = $result =~ /.. PASS/g;
+ $suite_cases_failed = () = $result =~ /.. FAILED/g;
diff --git a/test/monniaux/memcpy/test_memcpy.c b/test/monniaux/memcpy/test_memcpy.c
new file mode 100644
index 00000000..4a055692
--- /dev/null
+++ b/test/monniaux/memcpy/test_memcpy.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main() {
+ char buf1[16], buf2[16];
+ for(int i=0; i<16; i++) {
+ buf1[i] = i;
+ }
+ for(int i=0; i<16; i++) {
+ buf2[i] = 100+i;
+ }
+ __builtin_memcpy_aligned(buf1, buf2+3, 4, 1);
+ for(int i=0; i<16; i++) {
+ printf("%d : %d\n", i, buf1[i]);
+ }
+}
diff --git a/test/monniaux/micro-bunzip/Makefile b/test/monniaux/micro-bunzip/Makefile
new file mode 100644
index 00000000..bfcc377a
--- /dev/null
+++ b/test/monniaux/micro-bunzip/Makefile
@@ -0,0 +1,53 @@
+include ../rules.mk
+
+all: testfile.txt testfile.txt.2ccomp testfile.txt.2gcc testfile.ccomp.kvx.out testfile.gcc.kvx.out testfile.ccomp.host.out testfile.gcc.host.out
+ cmp testfile.txt testfile.txt.2ccomp
+ cmp testfile.txt testfile.txt.2gcc
+
+micro-bunzip.ccomp.host: micro-bunzip.c ../clock.gcc.host.o
+ $(CCOMP) $(CCOMPFLAGS) $+ -o $@
+
+micro-bunzip.ccomp.kvx: micro-bunzip.c ../clock.gcc.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+micro-bunzip.gcc.host: micro-bunzip.c ../clock.gcc.host.o
+ $(CC) $(CFLAGS) $+ -o $@
+
+# micro-bunzip.gcc.kvx: micro-bunzip.c ../clock.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS) $+ -o $@
+
+# TODO: -O3 buggy??
+micro-bunzip.gcc.kvx: micro-bunzip.c ../clock.gcc.kvx.o
+ $(KVX_CC) -Wall -O2 $+ -o $@
+
+testfile.txt: micro-bunzip.c
+ cat micro-bunzip.c > $@
+ sha1sum micro-bunzip.c >> $@
+ cat micro-bunzip.c >> $@
+ md5sum micro-bunzip.c >> $@
+# cat micro-bunzip.c >> $@
+# sha224sum micro-bunzip.c >> $@
+# cat micro-bunzip.c >> $@
+# sha256sum micro-bunzip.c >> $@
+# cat micro-bunzip.c >> $@
+# sha384sum micro-bunzip.c >> $@
+# cat micro-bunzip.c >> $@
+# sha512sum micro-bunzip.c >> $@
+#x cat micro-bunzip.c >> $@
+
+testfile.txt.2ccomp testfile.ccomp.kvx.out: testfile.txt micro-bunzip.ccomp.kvx
+ bzip2 <testfile.txt | k1-cluster --cycle-based --syscall=libstd_scalls.so -- micro-bunzip.ccomp.kvx > testfile.txt.2ccomp 2> testfile.ccomp.kvx.out
+
+testfile.txt.2gcc testfile.gcc.kvx.out: testfile.txt micro-bunzip.gcc.kvx
+ bzip2 <testfile.txt | k1-cluster --cycle-based --syscall=libstd_scalls.so -- micro-bunzip.gcc.kvx > testfile.txt.2gcc 2> testfile.gcc.kvx.out
+
+testfile.txt.2host testfile.gcc.host.out: testfile.txt micro-bunzip.gcc.host
+ bzip2 <testfile.txt | ./micro-bunzip.gcc.host > testfile.txt.2host 2> testfile.gcc.host.out
+
+testfile.ccomp.host.out: testfile.txt micro-bunzip.ccomp.host
+ bzip2 <testfile.txt | ./micro-bunzip.ccomp.host > /dev/null 2> testfile.ccomp.host.out
+
+clean:
+ rm -f *.kvx *.out test*txt*
+
+.PHONY: clean
diff --git a/test/monniaux/micro-bunzip/NOTES.txt b/test/monniaux/micro-bunzip/NOTES.txt
new file mode 100644
index 00000000..96ac2432
--- /dev/null
+++ b/test/monniaux/micro-bunzip/NOTES.txt
@@ -0,0 +1,5 @@
+gcc segfault -O3
+
+stdout/stderr in thread-local stuff that does not get correctly handled
+
+fdopen not available?!
diff --git a/test/monniaux/micro-bunzip/micro-bunzip.c b/test/monniaux/micro-bunzip/micro-bunzip.c
new file mode 100644
index 00000000..7197ede5
--- /dev/null
+++ b/test/monniaux/micro-bunzip/micro-bunzip.c
@@ -0,0 +1,525 @@
+/* vi: set sw=4 ts=4: */
+/* http://www.landley.net/code/micro-bunzip.c */
+/* micro-bunzip, a small, simple bzip2 decompression implementation.
+ Copyright 2003 by Rob Landley (rob@landley.net).
+
+ Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
+ which also acknowledges contributions by Mike Burrows, David Wheeler,
+ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
+ Robert Sedgewick, and Jon L. Bentley.
+
+ I hereby release this code under the GNU Library General Public License
+ (LGPL) version 2, available at http://www.gnu.org/copyleft/lgpl.html
+*/
+
+#include "../clock.h"
+
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Constants for huffman coding */
+#define MAX_GROUPS 6
+#define GROUP_SIZE 50 /* 64 would have been more efficient */
+#define MAX_HUFCODE_BITS 20 /* Longest huffman code allowed */
+#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
+#define SYMBOL_RUNA 0
+#define SYMBOL_RUNB 1
+
+/* Status return values */
+#define RETVAL_OK 0
+#define RETVAL_LAST_BLOCK (-1)
+#define RETVAL_NOT_BZIP_DATA (-2)
+#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
+#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
+#define RETVAL_DATA_ERROR (-5)
+#define RETVAL_OUT_OF_MEMORY (-6)
+#define RETVAL_OBSOLETE_INPUT (-7)
+
+/* Other housekeeping constants */
+#define IOBUF_SIZE 4096
+
+char *bunzip_errors[]={NULL,"Bad file checksum","Not bzip data",
+ "Unexpected input EOF","Unexpected output EOF","Data error",
+ "Out of memory","Obsolete (pre 0.9.5) bzip format not supported."};
+
+/* This is what we know about each huffman coding group */
+struct group_data {
+ int limit[MAX_HUFCODE_BITS],base[MAX_HUFCODE_BITS],permute[MAX_SYMBOLS];
+ char minLen, maxLen;
+};
+
+/* Structure holding all the housekeeping data, including IO buffers and
+ memory that persists between calls to bunzip */
+typedef struct {
+ /* For I/O error handling */
+ jmp_buf jmpbuf;
+ /* Input stream, input buffer, input bit buffer */
+ int in_fd,inbufCount,inbufPos;
+ unsigned char *inbuf;
+ unsigned int inbufBitCount, inbufBits;
+ /* Output buffer */
+ char outbuf[IOBUF_SIZE];
+ int outbufPos;
+ /* The CRC values stored in the block header and calculated from the data */
+ unsigned int crc32Table[256],headerCRC, dataCRC, totalCRC;
+ /* Intermediate buffer and its size (in bytes) */
+ unsigned int *dbuf, dbufSize;
+ /* State for interrupting output loop */
+ int writePos,writeRun,writeCount,writeCurrent;
+
+ /* These things are a bit too big to go on the stack */
+ unsigned char selectors[32768]; /* nSelectors=15 bits */
+ struct group_data groups[MAX_GROUPS]; /* huffman coding tables */
+} bunzip_data;
+
+/* Return the next nnn bits of input. All reads from the compressed input
+ are done through this function. All reads are big endian */
+static unsigned int get_bits(bunzip_data *bd, char bits_wanted)
+{
+ unsigned int bits=0;
+
+ /* If we need to get more data from the byte buffer, do so. (Loop getting
+ one byte at a time to enforce endianness and avoid unaligned access.) */
+ while (bd->inbufBitCount<bits_wanted) {
+ /* If we need to read more data from file into byte buffer, do so */
+ if(bd->inbufPos==bd->inbufCount) {
+ clock_stop();
+ if(!(bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE)))
+ longjmp(bd->jmpbuf,RETVAL_UNEXPECTED_INPUT_EOF);
+ clock_start();
+ bd->inbufPos=0;
+ }
+ /* Avoid 32-bit overflow (dump bit buffer to top of output) */
+ if(bd->inbufBitCount>=24) {
+ bits=bd->inbufBits&((1<<bd->inbufBitCount)-1);
+ bits_wanted-=bd->inbufBitCount;
+ bits<<=bits_wanted;
+ bd->inbufBitCount=0;
+ }
+ /* Grab next 8 bits of input from buffer. */
+ bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
+ bd->inbufBitCount+=8;
+ }
+ /* Calculate result */
+ bd->inbufBitCount-=bits_wanted;
+ bits|=(bd->inbufBits>>bd->inbufBitCount)&((1<<bits_wanted)-1);
+
+ return bits;
+}
+
+/* Decompress a block of text to into intermediate buffer */
+
+extern int read_bunzip_data(bunzip_data *bd)
+{
+ struct group_data *hufGroup;
+ int dbufCount,nextSym,dbufSize,origPtr,groupCount,*base,*limit,selector,
+ i,j,k,t,runPos,symCount,symTotal,nSelectors,byteCount[256];
+ unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
+ unsigned int *dbuf;
+
+ /* Read in header signature (borrowing mtfSymbol for temp space). */
+ for(i=0;i<6;i++) mtfSymbol[i]=get_bits(bd,8);
+ mtfSymbol[6]=0;
+ /* Read CRC (which is stored big endian). */
+ bd->headerCRC=get_bits(bd,32);
+ /* Is this the last block (with CRC for file)? */
+ if(!strcmp(mtfSymbol,"\x17\x72\x45\x38\x50\x90"))
+ return RETVAL_LAST_BLOCK;
+ /* If it's not a valid data block, barf. */
+ if(strcmp(mtfSymbol,"\x31\x41\x59\x26\x53\x59"))
+ return RETVAL_NOT_BZIP_DATA;
+
+ dbuf=bd->dbuf;
+ dbufSize=bd->dbufSize;
+ selectors=bd->selectors;
+ /* We can add support for blockRandomised if anybody complains. There was
+ some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
+ it didn't actually work. */
+ if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
+ if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR;
+ /* mapping table: if some byte values are never used (encoding things
+ like ascii text), the compression code removes the gaps to have fewer
+ symbols to deal with, and writes a sparse bitfield indicating which
+ values were present. We make a translation table to convert the symbols
+ back to the corresponding bytes. */
+ t=get_bits(bd, 16);
+ memset(symToByte,0,256);
+ symTotal=0;
+ for (i=0;i<16;i++) {
+ if(t&(1<<(15-i))) {
+ k=get_bits(bd,16);
+ for(j=0;j<16;j++)
+ if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j;
+ }
+ }
+ /* How many different huffman coding groups does this block use? */
+ groupCount=get_bits(bd,3);
+ if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
+ /* nSelectors: Every GROUP_SIZE many symbols we select a new huffman coding
+ group. Read in the group selector list, which is stored as MTF encoded
+ bit runs. */
+ if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR;
+ for(i=0; i<groupCount; i++) mtfSymbol[i] = i;
+ for(i=0; i<nSelectors; i++) {
+ /* Get next value */
+ for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR;
+ /* Decode MTF to get the next selector */
+ uc = mtfSymbol[j];
+ memmove(mtfSymbol+1,mtfSymbol,j);
+ mtfSymbol[0]=selectors[i]=uc;
+ }
+ /* Read the huffman coding tables for each group, which code for symTotal
+ literal symbols, plus two run symbols (RUNA, RUNB) */
+ symCount=symTotal+2;
+ for (j=0; j<groupCount; j++) {
+ unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1];
+ int minLen, maxLen, pp;
+ /* Read lengths */
+ t=get_bits(bd, 5);
+ for (i = 0; i < symCount; i++) {
+ for(;;) {
+ if (t < 1 || t > MAX_HUFCODE_BITS) return RETVAL_DATA_ERROR;
+ if(!get_bits(bd, 1)) break;
+ if(!get_bits(bd, 1)) t++;
+ else t--;
+ }
+ length[i] = t;
+ }
+ /* Find largest and smallest lengths in this group */
+ minLen=maxLen=length[0];
+ for(i = 1; i < symCount; i++) {
+ if(length[i] > maxLen) maxLen = length[i];
+ else if(length[i] < minLen) minLen = length[i];
+ }
+ /* Calculate permute[], base[], and limit[] tables from length[].
+ *
+ * permute[] is the lookup table for converting huffman coded symbols
+ * into decoded symbols. base[] is the amount to subtract from the
+ * value of a huffman symbol of a given length when using permute[].
+ *
+ * limit[] indicates the largest numerical value a symbol with a given
+ * number of bits can have. It lets us know when to stop reading.
+ *
+ * To use these, keep reading bits until value<=limit[bitcount] or
+ * you've read over 20 bits (error). Then the decoded symbol
+ * equals permute[hufcode_value-base[hufcode_bitcount]].
+ */
+ hufGroup=bd->groups+j;
+ hufGroup->minLen = minLen;
+ hufGroup->maxLen = maxLen;
+ /* Note that minLen can't be smaller than 1, so we adjust the base
+ and limit array pointers so we're not always wasting the first
+ entry. We do this again when using them (during symbol decoding).*/
+ base=hufGroup->base-1;
+ limit=hufGroup->limit-1;
+ /* Calculate permute[] */
+ pp = 0;
+ for(i=minLen;i<=maxLen;i++)
+ for(t=0;t<symCount;t++)
+ if(length[t]==i) hufGroup->permute[pp++] = t;
+ /* Count cumulative symbols coded for at each bit length */
+ for (i=minLen;i<=maxLen;i++) temp[i]=limit[i]=0;
+ for (i=0;i<symCount;i++) temp[length[i]]++;
+ /* Calculate limit[] (the largest symbol-coding value at each bit
+ * length, which is (previous limit<<1)+symbols at this level), and
+ * base[] (number of symbols to ignore at each bit length, which is
+ * limit-cumulative count of symbols coded for already). */
+ pp=t=0;
+ for (i=minLen; i<maxLen; i++) {
+ pp+=temp[i];
+ limit[i]=pp-1;
+ pp<<=1;
+ base[i+1]=pp-(t+=temp[i]);
+ }
+ limit[maxLen]=pp+temp[maxLen]-1;
+ base[minLen]=0;
+ }
+ /* We've finished reading and digesting the block header. Now read this
+ block's huffman coded symbols from the file and undo the huffman coding
+ and run length encoding, saving the result into dbuf[dbufCount++]=uc */
+
+ /* Initialize symbol occurrence counters and symbol mtf table */
+ memset(byteCount,0,256*sizeof(int));
+ for(i=0;i<256;i++) mtfSymbol[i]=(unsigned char)i;
+ /* Loop through compressed symbols */
+ runPos=dbufCount=symCount=selector=0;
+ for(;;) {
+ /* Determine which huffman coding group to use. */
+ if(!(symCount--)) {
+ symCount=GROUP_SIZE-1;
+ if(selector>=nSelectors) return RETVAL_DATA_ERROR;
+ hufGroup=bd->groups+selectors[selector++];
+ base=hufGroup->base-1;
+ limit=hufGroup->limit-1;
+ }
+ /* Read next huffman-coded symbol */
+ i = hufGroup->minLen;
+ j=get_bits(bd, i);
+ for(;;) {
+ if (i > hufGroup->maxLen) return RETVAL_DATA_ERROR;
+ if (j <= limit[i]) break;
+ i++;
+
+ j = (j << 1) | get_bits(bd,1);
+ }
+ /* Huffman decode nextSym (with bounds checking) */
+ j-=base[i];
+ if (j < 0 || j >= MAX_SYMBOLS) return RETVAL_DATA_ERROR;
+ nextSym = hufGroup->permute[j];
+ /* If this is a repeated run, loop collecting data */
+ if (nextSym == SYMBOL_RUNA || nextSym == SYMBOL_RUNB) {
+ /* If this is the start of a new run, zero out counter */
+ if(!runPos) {
+ runPos = 1;
+ t = 0;
+ }
+ /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
+ each bit position, add 1 or 2 instead. For example,
+ 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
+ You can make any bit pattern that way using 1 less symbol than
+ the basic or 0/1 method (except all bits 0, which would use no
+ symbols, but a run of length 0 doesn't mean anything in this
+ context). Thus space is saved. */
+ if (nextSym == SYMBOL_RUNA) t += runPos;
+ else t += 2*runPos;
+ runPos <<= 1;
+ continue;
+ }
+ /* When we hit the first non-run symbol after a run, we now know
+ how many times to repeat the last literal, so append that many
+ copies to our buffer of decoded symbols (dbuf) now. (The last
+ literal used is the one at the head of the mtfSymbol array.) */
+ if(runPos) {
+ runPos=0;
+ if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR;
+
+ uc = symToByte[mtfSymbol[0]];
+ byteCount[uc] += t;
+ while(t--) dbuf[dbufCount++]=uc;
+ }
+ /* Is this the terminating symbol? */
+ if(nextSym>symTotal) break;
+ /* At this point, the symbol we just decoded indicates a new literal
+ character. Subtract one to get the position in the MTF array
+ at which this literal is currently to be found. (Note that the
+ result can't be -1 or 0, because 0 and 1 are RUNA and RUNB.
+ Another instance of the first symbol in the mtf array, position 0,
+ would have been handled as part of a run.) */
+ if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR;
+ i = nextSym - 1;
+ uc = mtfSymbol[i];
+ memmove(mtfSymbol+1,mtfSymbol,i);
+ mtfSymbol[0] = uc;
+ uc=symToByte[uc];
+ /* We have our literal byte. Save it into dbuf. */
+ byteCount[uc]++;
+ dbuf[dbufCount++] = (unsigned int)uc;
+ }
+ /* At this point, we've finished reading huffman-coded symbols and
+ compressed runs from the input stream. There are dbufCount many of
+ them in dbuf[]. Now undo the Burrows-Wheeler transform on dbuf.
+ See http://dogma.net/markn/articles/bwt/bwt.htm
+ */
+
+ /* Now we know what dbufCount is, do a better sanity check on origPtr. */
+ if (origPtr<0 || origPtr>=dbufCount) return RETVAL_DATA_ERROR;
+ /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
+ j=0;
+ for(i=0;i<256;i++) {
+ k=j+byteCount[i];
+ byteCount[i] = j;
+ j=k;
+ }
+ /* Figure out what order dbuf would be in if we sorted it. */
+ for (i=0;i<dbufCount;i++) {
+ uc = (unsigned char)(dbuf[i] & 0xff);
+ dbuf[byteCount[uc]] |= (i << 8);
+ byteCount[uc]++;
+ }
+ /* blockRandomised support would go here. */
+
+ /* Using i as position, j as previous character, t as current character,
+ and uc as run count */
+ bd->dataCRC = 0xffffffffL;
+ /* Decode first byte by hand to initialize "previous" byte. Note that it
+ doesn't get output, and if the first three characters are identical
+ it doesn't qualify as a run (hence uc=255, which will either wrap
+ to 1 or get reset). */
+ if(dbufCount) {
+ bd->writePos=dbuf[origPtr];
+ bd->writeCurrent=(unsigned char)(bd->writePos&0xff);
+ bd->writePos>>=8;
+ bd->writeRun=-1;
+ }
+ bd->writeCount=dbufCount;
+
+ return RETVAL_OK;
+}
+
+/* Flush output buffer to disk */
+extern void flush_bunzip_outbuf(bunzip_data *bd, int out_fd)
+{
+ if(bd->outbufPos) {
+ clock_stop();
+ if(write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos)
+ longjmp(bd->jmpbuf,RETVAL_UNEXPECTED_OUTPUT_EOF);
+ clock_start();
+ bd->outbufPos=0;
+ }
+}
+
+
+/* Undo burrows-wheeler transform on intermediate buffer to produce output.
+ If !len, write up to len bytes of data to buf. Otherwise write to out_fd.
+ Returns len ? bytes written : RETVAL_OK. Notice all errors negative #'s. */
+extern int write_bunzip_data(bunzip_data *bd, int out_fd, char *outbuf, int len)
+{
+ unsigned int *dbuf=bd->dbuf;
+ int count,pos,current, run,copies,outbyte,previous,gotcount=0;
+
+ for(;;) {
+ /* If last read was short due to end of file, return last block now */
+ if(bd->writeCount<0) return bd->writeCount;
+ /* If we need to refill dbuf, do it. */
+ if(!bd->writeCount) {
+ int i=read_bunzip_data(bd);
+ if(i) {
+ if(i==RETVAL_LAST_BLOCK) {
+ bd->writeCount=i;
+ return gotcount;
+ } else return i;
+ }
+ }
+ /* Loop generating output */
+ count=bd->writeCount;
+ pos=bd->writePos;
+ current=bd->writeCurrent;
+ run=bd->writeRun;
+ while(count) {
+ /* If somebody (like busybox tar) wants a certain number of bytes of
+ data from memory instead of written to a file, humor them */
+ if(len && bd->outbufPos>=len) goto dataus_interruptus;
+ count--;
+ /* Follow sequence vector to undo Burrows-Wheeler transform */
+ previous=current;
+ pos=dbuf[pos];
+ current=pos&0xff;
+ pos>>=8;
+ /* Whenever we see 3 consecutive copies of the same byte,
+ the 4th is a repeat count */
+ if(run++==3) {
+ copies=current;
+ outbyte=previous;
+ current=-1;
+ } else {
+ copies=1;
+ outbyte=current;
+ }
+ /* Output bytes to buffer, flushing to file if necessary */
+ while(copies--) {
+ if(bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd,out_fd);
+ bd->outbuf[bd->outbufPos++] = outbyte;
+ bd->dataCRC = (bd->dataCRC << 8)
+ ^ bd->crc32Table[(bd->dataCRC >> 24) ^ outbyte];
+ }
+ if(current!=previous) run=0;
+ }
+ /* Decompression of this block completed successfully */
+ bd->dataCRC=~(bd->dataCRC);
+ bd->totalCRC=((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bd->dataCRC;
+ /* If this block had a CRC error, force file level CRC error. */
+ if(bd->dataCRC!=bd->headerCRC) {
+ bd->totalCRC=bd->headerCRC+1;
+ return RETVAL_LAST_BLOCK;
+ }
+dataus_interruptus:
+ bd->writeCount=count;
+ if(len) {
+ gotcount+=bd->outbufPos;
+ memcpy(outbuf,bd->outbuf,len);
+ /* If we got enough data, checkpoint loop state and return */
+ if((len-=bd->outbufPos)<1) {
+ bd->outbufPos-=len;
+ if(bd->outbufPos)
+ memmove(bd->outbuf,bd->outbuf+len,bd->outbufPos);
+ bd->writePos=pos;
+ bd->writeCurrent=current;
+ bd->writeRun=run;
+ return gotcount;
+ }
+ }
+ }
+}
+
+/* Allocate the structure, read file header. If !len, src_fd contains
+ filehandle to read from. Else inbuf contains data. */
+extern int start_bunzip(bunzip_data **bdp, int src_fd, char *inbuf, int len)
+{
+ bunzip_data *bd;
+ unsigned int i,j,c;
+
+ /* Figure out how much data to allocate */
+ i=sizeof(bunzip_data);
+ if(!len) i+=IOBUF_SIZE;
+ /* Allocate bunzip_data. Most fields initialize to zero. */
+ if(!(bd=*bdp=malloc(i))) return RETVAL_OUT_OF_MEMORY;
+ memset(bd,0,sizeof(bunzip_data));
+ if(len) {
+ bd->inbuf=inbuf;
+ bd->inbufCount=len;
+ bd->in_fd=-1;
+ } else {
+ bd->inbuf=(char *)(bd+1);
+ bd->in_fd=src_fd;
+ }
+ /* Init the CRC32 table (big endian) */
+ for(i=0;i<256;i++) {
+ c=i<<24;
+ for(j=8;j;j--)
+ c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
+ bd->crc32Table[i]=c;
+ }
+ /* Setup for I/O error handling via longjmp */
+ i=setjmp(bd->jmpbuf);
+ if(i) return i;
+ /* Ensure that file starts with "BZh" */
+ for(i=0;i<3;i++) if(get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA;
+ /* Next byte ascii '1'-'9', indicates block size in units of 100k of
+ uncompressed data. Allocate intermediate buffer for block. */
+ i=get_bits(bd,8);
+ if (i<'1' || i>'9') return RETVAL_NOT_BZIP_DATA;
+ bd->dbufSize=100000*(i-'0');
+ if(!(bd->dbuf=malloc(bd->dbufSize * sizeof(int))))
+ return RETVAL_OUT_OF_MEMORY;
+ return RETVAL_OK;
+}
+
+/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data,
+ not end of file.) */
+extern char *uncompressStream(int src_fd, int dst_fd)
+{
+ bunzip_data *bd;
+ int i;
+
+ clock_start();
+ if(!(i=start_bunzip(&bd,src_fd,0,0))) {
+ i=write_bunzip_data(bd,dst_fd,0,0);
+ if(i==RETVAL_LAST_BLOCK && bd->headerCRC==bd->totalCRC) i=RETVAL_OK;
+ }
+ flush_bunzip_outbuf(bd,dst_fd);
+ clock_stop();
+ if(bd->dbuf) free(bd->dbuf);
+ free(bd);
+ return bunzip_errors[-i];
+}
+
+/* Dumb little test thing, decompress stdin to stdout */
+int main(int argc, char *argv[])
+{
+ char *c=uncompressStream(0,1);
+ fprintf(stderr, "%s\ncycles=%lu\n", c ? c : "Completed OK", get_total_clock());
+ return 0;
+}
diff --git a/test/monniaux/minisat/LICENSE b/test/monniaux/minisat/LICENSE
new file mode 100644
index 00000000..8a6b9f36
--- /dev/null
+++ b/test/monniaux/minisat/LICENSE
@@ -0,0 +1,20 @@
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/test/monniaux/minisat/Makefile b/test/monniaux/minisat/Makefile
new file mode 100644
index 00000000..3a0268a6
--- /dev/null
+++ b/test/monniaux/minisat/Makefile
@@ -0,0 +1,36 @@
+
+include ../rules.mk
+
+EXECUTE_ARGS=sudoku.sat
+
+src=main.c solver.c
+
+PRODUCTS?=minisat.gcc.host minisat.ccomp.host minisat.gcc.kvx minisat.gcc.o1.kvx minisat.ccomp.kvx
+PRODUCTS_OUT=$(addsuffix .out,$(PRODUCTS))
+
+all: $(PRODUCTS)
+
+.PHONY:
+run: measures.csv
+
+LIBS=-lm
+
+minisat.gcc.host: $(src:.c=.gcc.host.o) ../clock.gcc.host.o
+ $(CC) $(CFLAGS) $+ $(LIBS) -o $@
+minisat.ccomp.host: $(src:.c=.ccomp.host.o) ../clock.gcc.host.o
+ $(CCOMP) $(CCOMPFLAGS) $+ $(LIBS) -o $@
+minisat.gcc.kvx: $(src:.c=.gcc.kvx.o) ../clock.gcc.kvx.o
+ $(KVX_CC) $(KVX_CFLAGS) $+ $(LIBS) -o $@
+minisat.gcc.o1.kvx: $(src:.c=.gcc.o1.kvx.o) ../clock.gcc.kvx.o
+ $(KVX_CC) $(KVX_CFLAGS_O1) $+ $(LIBS) -o $@
+minisat.ccomp.kvx: $(src:.c=.ccomp.kvx.o) ../clock.gcc.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ $(LIBS) -o $@
+measures.csv: $(PRODUCTS_OUT)
+ echo "benches, gcc host,ccomp host,gcc kvx,gcc o1 kvx,ccomp kvx" > $@
+
+.SECONDARY:
+
+.PHONY:
+clean:
+ rm -f *.o *.s *.kvx *.csv
+
diff --git a/test/monniaux/minisat/Makefile.on_marte b/test/monniaux/minisat/Makefile.on_marte
new file mode 100644
index 00000000..af7b9145
--- /dev/null
+++ b/test/monniaux/minisat/Makefile.on_marte
@@ -0,0 +1,16 @@
+EXE=minisat.ccomp.exe minisat.ccomp.trace-linearize.exe \
+ minisat.gcc-O3.exe \
+ minisat.ccomp.profiled.exe minisat.gcc-O3.profiled.exe
+
+LOG=$(EXE:.exe=.dat)
+
+all: $(LOG)
+
+%.log : %.exe
+ rm -f $@
+ for i in `seq 1 1000` ; do ./$< sudoku.sat >> $@; done
+
+%.dat : %.log
+ grep 'time cycles: ' $< | sed -e 's/time cycles: //' | awk '{ total += $$1; count++ } END { print total/count }' > $@
+
+.SECONDARY:
diff --git a/test/monniaux/minisat/Makefile.profiled b/test/monniaux/minisat/Makefile.profiled
new file mode 100644
index 00000000..77ba8b43
--- /dev/null
+++ b/test/monniaux/minisat/Makefile.profiled
@@ -0,0 +1,64 @@
+# -*- mode: makefile; -*-
+
+CFILES=main.c solver.c clock.c
+CCOMP=../../../ccomp
+
+#GCC=aarch64-linux-gnu-gcc
+GCC=k1-cos-gcc
+#EXECUTE=qemu-aarch64
+#EXECUTE=qemu-arm
+#EXECUTE=k1-cluster --
+#EXECUTE_CYCLES=k1-cluster --cycle-based --
+
+LIBS=-lm
+PROFILING_DAT=compcert_profiling.dat
+EXAMPLE=sudoku.sat
+CCOMPFLAGS=-static -finline-asm -finline-auto-threshold 50
+GCCFLAGS=-static
+ALL=minisat.ccomp.log minisat.ccomp.trace-linearize.log minisat.ccomp.profiled.log minisat.gcc-O3.log minisat.gcc-O3.profiled.log
+
+all: $(ALL)
+exe: $(ALL:.log=.exe)
+
+minisat.ccomp.exe: $(CFILES)
+ $(CCOMP) $(CCOMPFLAGS) $(CFILES) -o $@ $(LIBS)
+
+minisat.ccomp.profile-arcs.exe: $(CFILES)
+ $(CCOMP) -DARM_NO_PRIVILEGE $(CCOMPFLAGS) -fprofile-arcs $(CFILES) -o $@ $(LIBS)
+
+minisat.gcc-O3.exe: $(CFILES)
+ $(GCC) $(GCCFLAGS) -O3 $(CFILES) -o $@ $(LIBS)
+
+clock.gcc-O3.noprofile.o : clock.c
+ $(GCC) -DARM_NO_PRIVILEGE $(GCCFLAGS) -O3 -c $< -o @
+
+minisat.gcc-O3.profile-arcs.exe: main.c solver.c clock.gcc-O3.noprofile.o
+ $(GCC) -DARM_NO_PRIVILEGE $(GCCFLAGS) -fprofile-arcs -O3 $+ -o $@ $(LIBS)
+
+gcda: minisat.gcc-O3.profile-arcs.exe
+ $(EXECUTE) ./$< $(EXAMPLE)
+
+main.gcda solver.gcda: gcda
+
+minisat.gcc-O3.profiled.exe: $(CFILES) $(GCDAFILES)
+ $(GCC) $(GCCFLAGS) -O3 -fprofile-use $(CFILES) -o $@ $(LIBS)
+
+minisat.ccomp.trace-linearize.exe: $(CFILES)
+ $(CCOMP) $(CCOMPFLAGS) -fduplicate 0 -ftracelinearize $(CFILES) -o $@ $(LIBS)
+
+$(PROFILING_DAT): minisat.ccomp.profile-arcs.exe
+ -rm -f $(PROFILING_DAT)
+ $(EXECUTE) ./$< $(EXAMPLE)
+
+minisat.ccomp.profiled.exe: $(CFILES) $(PROFILING_DAT)
+ $(CCOMP) $(CCOMPFLAGS) -fprofile-use= $(PROFILING_DAT) -ftracelinearize $(CFILES) -o $@ $(LIBS)
+
+%.log : %.exe
+ $(EXECUTE_CYCLES) $< $(EXAMPLE) 2>&1 | tee $@
+
+clean:
+ -rm -f *.log *.exe $(PROFILING_DAT) $(GCDAFILES)
+
+.PHONY: clean gcda exe all
+
+.SECONDARY:
diff --git a/test/monniaux/minisat/README b/test/monniaux/minisat/README
new file mode 100644
index 00000000..b31e850b
--- /dev/null
+++ b/test/monniaux/minisat/README
@@ -0,0 +1,15 @@
+MiniSat-C v1.14.1
+========================================
+
+* Fixed some serious bugs.
+* Tweaked to be Visual Studio friendly (by Alan Mishchenko).
+ This disabled reading of gzipped DIMACS files and signal handling, but none
+ of these features are essential (and easy to re-enable, if wanted).
+
+MiniSat-C v1.14
+========================================
+
+Ok, we get it. You hate C++. You hate templates. We agree; C++ is a
+seriously messed up language. Although we are more pragmatic about the
+quirks and maldesigns in C++, we sympathize with you. So here is a
+pure C version of MiniSat, put together by Niklas Sörensson.
diff --git a/test/monniaux/minisat/clock.c b/test/monniaux/minisat/clock.c
new file mode 120000
index 00000000..d6bade99
--- /dev/null
+++ b/test/monniaux/minisat/clock.c
@@ -0,0 +1 @@
+../clock.c \ No newline at end of file
diff --git a/test/monniaux/minisat/cycles.h b/test/monniaux/minisat/cycles.h
new file mode 120000
index 00000000..84e54d21
--- /dev/null
+++ b/test/monniaux/minisat/cycles.h
@@ -0,0 +1 @@
+../cycles.h \ No newline at end of file
diff --git a/test/monniaux/minisat/k1c.inline_50.log b/test/monniaux/minisat/k1c.inline_50.log
new file mode 100644
index 00000000..438a06b4
--- /dev/null
+++ b/test/monniaux/minisat/k1c.inline_50.log
@@ -0,0 +1,14 @@
+==> minisat.ccomp.log <==
+time cycles: 3252345
+
+==> minisat.ccomp.profiled.log <==
+time cycles: 3150170
+
+==> minisat.ccomp.trace-linearize.log <==
+time cycles: 3192299
+
+==> minisat.gcc-O3.log <==
+time cycles: 2780324
+
+==> minisat.gcc-O3.profiled.log <==
+time cycles: 2487533
diff --git a/test/monniaux/minisat/main.c b/test/monniaux/minisat/main.c
new file mode 100644
index 00000000..abd3a03a
--- /dev/null
+++ b/test/monniaux/minisat/main.c
@@ -0,0 +1,222 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#include "solver.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <inttypes.h> // DM
+//#include <unistd.h>
+//#include <signal.h>
+//#include <zlib.h>
+//#include <sys/time.h>
+//#include <sys/resource.h>
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+//=================================================================================================
+// Helpers:
+
+
+// Reads an input stream to end-of-file and returns the result as a 'char*' terminated by '\0'
+// (dynamic allocation in case 'in' is standard input).
+//
+char* readFile(FILE * in)
+{
+ char* data = malloc(65536);
+ int cap = 65536;
+ int size = 0;
+
+ while (!feof(in)){
+ if (size == cap){
+ cap *= 2;
+ data = realloc(data, cap); }
+ size += fread(&data[size], 1, 65536, in);
+ }
+ data = realloc(data, size+1);
+ data[size] = '\0';
+
+ return data;
+}
+
+//static inline double cpuTime(void) {
+// struct rusage ru;
+// getrusage(RUSAGE_SELF, &ru);
+// return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000; }
+
+
+//=================================================================================================
+// DIMACS Parser:
+
+
+static inline void skipWhitespace(char** in) {
+ while ((**in >= 9 && **in <= 13) || **in == 32)
+ (*in)++; }
+
+static inline void skipLine(char** in) {
+ for (;;){
+ if (**in == 0) return;
+ if (**in == '\n') { (*in)++; return; }
+ (*in)++; } }
+
+static inline int parseInt(char** in) {
+ int val = 0;
+ int _neg = 0;
+ skipWhitespace(in);
+ if (**in == '-') _neg = 1, (*in)++;
+ else if (**in == '+') (*in)++;
+ if (**in < '0' || **in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **in), exit(1);
+ while (**in >= '0' && **in <= '9')
+ val = val*10 + (**in - '0'),
+ (*in)++;
+ return _neg ? -val : val; }
+
+static void readClause(char** in, solver* s, veci* lits) {
+ int parsed_lit, var;
+ veci_resize(lits,0);
+ for (;;){
+ parsed_lit = parseInt(in);
+ if (parsed_lit == 0) break;
+ var = abs(parsed_lit)-1;
+ veci_push(lits, (parsed_lit > 0 ? toLit(var) : lit_neg(toLit(var))));
+ }
+}
+
+static lbool parse_DIMACS_main(char* in, solver* s) {
+ veci lits;
+ veci_new(&lits);
+
+ for (;;){
+ skipWhitespace(&in);
+ if (*in == 0)
+ break;
+ else if (*in == 'c' || *in == 'p')
+ skipLine(&in);
+ else{
+ lit* begin;
+ readClause(&in, s, &lits);
+ begin = veci_begin(&lits);
+ if (!solver_addclause(s, begin, begin+veci_size(&lits))){
+ veci_delete(&lits);
+ return l_False;
+ }
+ }
+ }
+ veci_delete(&lits);
+ return solver_simplify(s);
+}
+
+
+// Inserts problem into solver. Returns FALSE upon immediate conflict.
+//
+static lbool parse_DIMACS(FILE * in, solver* s) {
+ char* text = readFile(in);
+ lbool ret = parse_DIMACS_main(text, s);
+ free(text);
+ return ret; }
+
+
+//=================================================================================================
+
+
+void printStats(stats* stats, int cpu_time)
+{
+ double Time = (float)(cpu_time)/(float)(CLOCKS_PER_SEC);
+ printf("restarts : %12" PRIu64 "\n", stats->starts); // DM
+ printf("conflicts : %12.0f (%9.0f / sec )\n", (double)stats->conflicts , (double)stats->conflicts /Time);
+ printf("decisions : %12.0f (%9.0f / sec )\n", (double)stats->decisions , (double)stats->decisions /Time);
+ printf("propagations : %12.0f (%9.0f / sec )\n", (double)stats->propagations, (double)stats->propagations/Time);
+ printf("inspects : %12.0f (%9.0f / sec )\n", (double)stats->inspects , (double)stats->inspects /Time);
+ printf("conflict literals : %12.0f (%9.2f %% deleted )\n", (double)stats->tot_literals, (double)(stats->max_literals - stats->tot_literals) * 100.0 / (double)stats->max_literals);
+ printf("CPU time : %12.2f sec\n", Time);
+}
+
+//solver* slv;
+//static void SIGINT_handler(int signum) {
+// printf("\n"); printf("*** INTERRUPTED ***\n");
+// printStats(&slv->stats, cpuTime());
+// printf("\n"); printf("*** INTERRUPTED ***\n");
+// exit(0); }
+
+
+//=================================================================================================
+
+
+int main(int argc, char** argv)
+{
+ solver* s = solver_new();
+ lbool st;
+ FILE * in;
+ int clk = clock();
+
+ if (argc != 2)
+ fprintf(stderr, "ERROR! Not enough command line arguments.\n"),
+ exit(1);
+
+ in = fopen(argv[1], "rb");
+ if (in == NULL)
+ fprintf(stderr, "ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]),
+ exit(1);
+ st = parse_DIMACS(in, s);
+ fclose(in);
+
+ if (st == l_False){
+ solver_delete(s);
+ printf("Trivial problem\nUNSATISFIABLE\n");
+ exit(20);
+ }
+
+ s->verbosity = 1;
+// slv = s;
+// signal(SIGINT,SIGINT_handler);
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ st = solver_solve(s,0,0);
+#ifdef VERIMAG
+ clock_stop();
+#endif
+ printStats(&s->stats, clock() - clk);
+ printf("\n");
+ printf(st == l_True ? "SATISFIABLE\n" : "UNSATISFIABLE\n");
+
+ // print the sat assignment
+ if ( st == l_True )
+ {
+ int k;
+ printf( "\nSatisfying solution: " );
+ for ( k = 0; k < s->model.size; k++ )
+ printf( "x%d=%d ", k, s->model.ptr[k] == l_True );
+ printf( "\n" );
+ }
+
+#ifdef VERIMAG
+ print_total_clock();
+#endif
+
+ solver_delete(s);
+ return 0;
+}
diff --git a/test/monniaux/minisat/make.proto b/test/monniaux/minisat/make.proto
new file mode 100644
index 00000000..d86da4de
--- /dev/null
+++ b/test/monniaux/minisat/make.proto
@@ -0,0 +1,2 @@
+sources: main.c solver.c
+target: minisat \ No newline at end of file
diff --git a/test/monniaux/minisat/solver.c b/test/monniaux/minisat/solver.c
new file mode 100644
index 00000000..d3b99c9d
--- /dev/null
+++ b/test/monniaux/minisat/solver.c
@@ -0,0 +1,1191 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#include <stdio.h>
+#include <assert.h>
+#include <math.h>
+
+#include "solver.h"
+
+//=================================================================================================
+// Debug:
+
+//#define VERBOSEDEBUG
+
+// For derivation output (verbosity level 2)
+#define L_IND "%-*d"
+#define L_ind solver_dlevel(s)*3+3,solver_dlevel(s)
+#define L_LIT "%sx%d"
+#define L_lit(p) lit_sign(p)?"~":"", (lit_var(p))
+
+// Just like 'assert()' but expression will be evaluated in the release version as well.
+static inline void check(int expr) { assert(expr); }
+
+static void printlits(lit* begin, lit* end)
+{
+ int i;
+ for (i = 0; i < end - begin; i++)
+ printf(L_LIT" ",L_lit(begin[i]));
+}
+
+//=================================================================================================
+// Random numbers:
+
+
+// Returns a random float 0 <= x < 1. Seed must never be 0.
+static inline double drand(double* seed) {
+ int q;
+ *seed *= 1389796;
+ q = (int)(*seed / 2147483647);
+ *seed -= (double)q * 2147483647;
+ return *seed / 2147483647; }
+
+
+// Returns a random integer 0 <= x < size. Seed must never be 0.
+static inline int irand(double* seed, int size) {
+ return (int)(drand(seed) * size); }
+
+
+//=================================================================================================
+// Predeclarations:
+
+void sort(void** array, int size, int(*comp)(const void *, const void *));
+
+//=================================================================================================
+// Clause datatype + minor functions:
+
+struct clause_t
+{
+ int size_learnt;
+ lit lits[0];
+};
+
+static inline int clause_size (clause* c) { return c->size_learnt >> 1; }
+static inline lit* clause_begin (clause* c) { return c->lits; }
+static inline int clause_learnt (clause* c) { return c->size_learnt & 1; }
+static inline float clause_activity (clause* c) { return *((float*)&c->lits[c->size_learnt>>1]); }
+static inline void clause_setactivity(clause* c, float a) { *((float*)&c->lits[c->size_learnt>>1]) = a; }
+
+//=================================================================================================
+// Encode literals in clause pointers:
+
+clause* clause_from_lit (lit l) { return (clause*)((unsigned long)l + (unsigned long)l + 1); }
+bool clause_is_lit (clause* c) { return ((unsigned long)c & 1); }
+lit clause_read_lit (clause* c) { return (lit)((unsigned long)c >> 1); }
+
+//=================================================================================================
+// Simple helpers:
+
+static inline int solver_dlevel(solver* s) { return veci_size(&s->trail_lim); }
+static inline vecp* solver_read_wlist (solver* s, lit l){ return &s->wlists[l]; }
+static inline void vecp_remove(vecp* v, void* e)
+{
+ void** ws = vecp_begin(v);
+ int j = 0;
+
+ for (; ws[j] != e ; j++);
+ assert(j < vecp_size(v));
+ for (; j < vecp_size(v)-1; j++) ws[j] = ws[j+1];
+ vecp_resize(v,vecp_size(v)-1);
+}
+
+//=================================================================================================
+// Variable order functions:
+
+static inline void order_update(solver* s, int v) // updateorder
+{
+ int* orderpos = s->orderpos;
+ double* activity = s->activity;
+ int* heap = veci_begin(&s->order);
+ int i = orderpos[v];
+ int x = heap[i];
+ int parent = (i - 1) / 2;
+
+ assert(s->orderpos[v] != -1);
+
+ while (i != 0 && activity[x] > activity[heap[parent]]){
+ heap[i] = heap[parent];
+ orderpos[heap[i]] = i;
+ i = parent;
+ parent = (i - 1) / 2;
+ }
+ heap[i] = x;
+ orderpos[x] = i;
+}
+
+static inline void order_assigned(solver* s, int v)
+{
+}
+
+static inline void order_unassigned(solver* s, int v) // undoorder
+{
+ int* orderpos = s->orderpos;
+ if (orderpos[v] == -1){
+ orderpos[v] = veci_size(&s->order);
+ veci_push(&s->order,v);
+ order_update(s,v);
+ }
+}
+
+static int order_select(solver* s, float random_var_freq) // selectvar
+{
+ int* heap;
+ double* activity;
+ int* orderpos;
+
+ lbool* values = s->assigns;
+
+ // Random decision:
+ if (drand(&s->random_seed) < random_var_freq){
+ int next = irand(&s->random_seed,s->size);
+ assert(next >= 0 && next < s->size);
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ // Activity based decision:
+
+ heap = veci_begin(&s->order);
+ activity = s->activity;
+ orderpos = s->orderpos;
+
+
+ while (veci_size(&s->order) > 0){
+ int next = heap[0];
+ int size = veci_size(&s->order)-1;
+ int x = heap[size];
+
+ veci_resize(&s->order,size);
+
+ orderpos[next] = -1;
+
+ if (size > 0){
+ double act = activity[x];
+
+ int i = 0;
+ int child = 1;
+
+
+ while (child < size){
+ if (child+1 < size && activity[heap[child]] < activity[heap[child+1]])
+ child++;
+
+ assert(child < size);
+
+ if (act >= activity[heap[child]])
+ break;
+
+ heap[i] = heap[child];
+ orderpos[heap[i]] = i;
+ i = child;
+ child = 2 * child + 1;
+ }
+ heap[i] = x;
+ orderpos[heap[i]] = i;
+ }
+
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ return var_Undef;
+}
+
+//=================================================================================================
+// Activity functions:
+
+static inline void act_var_rescale(solver* s) {
+ double* activity = s->activity;
+ int i;
+ for (i = 0; i < s->size; i++)
+ activity[i] *= 1e-100;
+ s->var_inc *= 1e-100;
+}
+
+static inline void act_var_bump(solver* s, int v) {
+ double* activity = s->activity;
+ if ((activity[v] += s->var_inc) > 1e100)
+ act_var_rescale(s);
+
+ //printf("bump %d %f\n", v-1, activity[v]);
+
+ if (s->orderpos[v] != -1)
+ order_update(s,v);
+
+}
+
+static inline void act_var_decay(solver* s) { s->var_inc *= s->var_decay; }
+
+static inline void act_clause_rescale(solver* s) {
+ clause** cs = (clause**)vecp_begin(&s->learnts);
+ int i;
+ for (i = 0; i < vecp_size(&s->learnts); i++){
+ float a = clause_activity(cs[i]);
+ clause_setactivity(cs[i], a * (float)1e-20);
+ }
+ s->cla_inc *= (float)1e-20;
+}
+
+
+static inline void act_clause_bump(solver* s, clause *c) {
+ float a = clause_activity(c) + s->cla_inc;
+ clause_setactivity(c,a);
+ if (a > 1e20) act_clause_rescale(s);
+}
+
+static inline void act_clause_decay(solver* s) { s->cla_inc *= s->cla_decay; }
+
+
+//=================================================================================================
+// Clause functions:
+
+/* pre: size > 1 && no variable occurs twice
+ */
+static clause* clause_new(solver* s, lit* begin, lit* end, int learnt)
+{
+ int size;
+ clause* c;
+ int i;
+
+ assert(end - begin > 1);
+ assert(learnt >= 0 && learnt < 2);
+ size = end - begin;
+ c = (clause*)malloc(sizeof(clause) + sizeof(lit) * size + learnt * sizeof(float));
+ c->size_learnt = (size << 1) | learnt;
+ assert(((unsigned int)c & 1) == 0);
+
+ for (i = 0; i < size; i++)
+ c->lits[i] = begin[i];
+
+ if (learnt)
+ *((float*)&c->lits[size]) = 0.0;
+
+ assert(begin[0] >= 0);
+ assert(begin[0] < s->size*2);
+ assert(begin[1] >= 0);
+ assert(begin[1] < s->size*2);
+
+ assert(lit_neg(begin[0]) < s->size*2);
+ assert(lit_neg(begin[1]) < s->size*2);
+
+ //vecp_push(solver_read_wlist(s,lit_neg(begin[0])),(void*)c);
+ //vecp_push(solver_read_wlist(s,lit_neg(begin[1])),(void*)c);
+
+ vecp_push(solver_read_wlist(s,lit_neg(begin[0])),(void*)(size > 2 ? c : clause_from_lit(begin[1])));
+ vecp_push(solver_read_wlist(s,lit_neg(begin[1])),(void*)(size > 2 ? c : clause_from_lit(begin[0])));
+
+ return c;
+}
+
+
+static void clause_remove(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ assert(lit_neg(lits[0]) < s->size*2);
+ assert(lit_neg(lits[1]) < s->size*2);
+
+ //vecp_remove(solver_read_wlist(s,lit_neg(lits[0])),(void*)c);
+ //vecp_remove(solver_read_wlist(s,lit_neg(lits[1])),(void*)c);
+
+ assert(lits[0] < s->size*2);
+ vecp_remove(solver_read_wlist(s,lit_neg(lits[0])),(void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[1])));
+ vecp_remove(solver_read_wlist(s,lit_neg(lits[1])),(void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[0])));
+
+ if (clause_learnt(c)){
+ s->stats.learnts--;
+ s->stats.learnts_literals -= clause_size(c);
+ }else{
+ s->stats.clauses--;
+ s->stats.clauses_literals -= clause_size(c);
+ }
+
+ free(c);
+}
+
+
+static lbool clause_simplify(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ lbool* values = s->assigns;
+ int i;
+
+ assert(solver_dlevel(s) == 0);
+
+ for (i = 0; i < clause_size(c); i++){
+ lbool sig = !lit_sign(lits[i]); sig += sig - 1;
+ if (values[lit_var(lits[i])] == sig)
+ return l_True;
+ }
+ return l_False;
+}
+
+//=================================================================================================
+// Minor (solver) functions:
+
+void solver_setnvars(solver* s,int n)
+{
+ int var;
+
+ if (s->cap < n){
+
+ while (s->cap < n) s->cap = s->cap*2+1;
+
+ s->wlists = (vecp*) realloc(s->wlists, sizeof(vecp)*s->cap*2);
+ s->activity = (double*) realloc(s->activity, sizeof(double)*s->cap);
+ s->assigns = (lbool*) realloc(s->assigns, sizeof(lbool)*s->cap);
+ s->orderpos = (int*) realloc(s->orderpos, sizeof(int)*s->cap);
+ s->reasons = (clause**)realloc(s->reasons, sizeof(clause*)*s->cap);
+ s->levels = (int*) realloc(s->levels, sizeof(int)*s->cap);
+ s->tags = (lbool*) realloc(s->tags, sizeof(lbool)*s->cap);
+ s->trail = (lit*) realloc(s->trail, sizeof(lit)*s->cap);
+ }
+
+ for (var = s->size; var < n; var++){
+ vecp_new(&s->wlists[2*var]);
+ vecp_new(&s->wlists[2*var+1]);
+ s->activity [var] = 0;
+ s->assigns [var] = l_Undef;
+ s->orderpos [var] = veci_size(&s->order);
+ s->reasons [var] = (clause*)0;
+ s->levels [var] = 0;
+ s->tags [var] = l_Undef;
+
+ /* does not hold because variables enqueued at top level will not be reinserted in the heap
+ assert(veci_size(&s->order) == var);
+ */
+ veci_push(&s->order,var);
+ order_update(s, var);
+ }
+
+ s->size = n > s->size ? n : s->size;
+}
+
+
+static inline bool enqueue(solver* s, lit l, clause* from)
+{
+ lbool* values = s->assigns;
+ int v = lit_var(l);
+ lbool val = values[v];
+#ifdef VERBOSEDEBUG
+ printf(L_IND"enqueue("L_LIT")\n", L_ind, L_lit(l));
+#endif
+
+ lbool sig = !lit_sign(l); sig += sig - 1;
+ if (val != l_Undef){
+ return val == sig;
+ }else{
+ // New fact -- store it.
+#ifdef VERBOSEDEBUG
+ printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ int* levels = s->levels;
+ clause** reasons = s->reasons;
+
+ values [v] = sig;
+ levels [v] = solver_dlevel(s);
+ reasons[v] = from;
+ s->trail[s->qtail++] = l;
+
+ order_assigned(s, v);
+ return true;
+ }
+}
+
+
+static inline void assume(solver* s, lit l){
+ assert(s->qtail == s->qhead);
+ assert(s->assigns[lit_var(l)] == l_Undef);
+#ifdef VERBOSEDEBUG
+ printf(L_IND"assume("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ veci_push(&s->trail_lim,s->qtail);
+ enqueue(s,l,(clause*)0);
+}
+
+
+static inline void solver_canceluntil(solver* s, int level) {
+ lit* trail;
+ lbool* values;
+ clause** reasons;
+ int bound;
+ int c;
+
+ if (solver_dlevel(s) <= level)
+ return;
+
+ trail = s->trail;
+ values = s->assigns;
+ reasons = s->reasons;
+ bound = (veci_begin(&s->trail_lim))[level];
+
+ for (c = s->qtail-1; c >= bound; c--) {
+ int x = lit_var(trail[c]);
+ values [x] = l_Undef;
+ reasons[x] = (clause*)0;
+ }
+
+ for (c = s->qhead-1; c >= bound; c--)
+ order_unassigned(s,lit_var(trail[c]));
+
+ s->qhead = s->qtail = bound;
+ veci_resize(&s->trail_lim,level);
+}
+
+static void solver_record(solver* s, veci* cls)
+{
+ lit* begin = veci_begin(cls);
+ lit* end = begin + veci_size(cls);
+ clause* c = (veci_size(cls) > 1) ? clause_new(s,begin,end,1) : (clause*)0;
+ enqueue(s,*begin,c);
+
+ assert(veci_size(cls) > 0);
+
+ if (c != 0) {
+ vecp_push(&s->learnts,c);
+ act_clause_bump(s,c);
+ s->stats.learnts++;
+ s->stats.learnts_literals += veci_size(cls);
+ }
+}
+
+
+static double solver_progress(solver* s)
+{
+ lbool* values = s->assigns;
+ int* levels = s->levels;
+ int i;
+
+ double progress = 0;
+ double F = 1.0 / s->size;
+ for (i = 0; i < s->size; i++)
+ if (values[i] != l_Undef)
+ progress += pow(F, levels[i]);
+ return progress / s->size;
+}
+
+//=================================================================================================
+// Major methods:
+
+static bool solver_lit_removable(solver* s, lit l, int minl)
+{
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int top = veci_size(&s->tagged);
+
+ assert(lit_var(l) >= 0 && lit_var(l) < s->size);
+ assert(reasons[lit_var(l)] != 0);
+ veci_resize(&s->stack,0);
+ veci_push(&s->stack,lit_var(l));
+
+ while (veci_size(&s->stack) > 0){
+ clause* c;
+ int v = veci_begin(&s->stack)[veci_size(&s->stack)-1];
+ assert(v >= 0 && v < s->size);
+ veci_resize(&s->stack,veci_size(&s->stack)-1);
+ assert(reasons[v] != 0);
+ c = reasons[v];
+
+ if (clause_is_lit(c)){
+ int v = lit_var(clause_read_lit(c));
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0 && ((1 << (levels[v] & 31)) & minl)){
+ veci_push(&s->stack,v);
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ int j;
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }else{
+ lit* lits = clause_begin(c);
+ int i, j;
+
+ for (i = 1; i < clause_size(c); i++){
+ int v = lit_var(lits[i]);
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0 && ((1 << (levels[v] & 31)) & minl)){
+
+ veci_push(&s->stack,lit_var(lits[i]));
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+static void solver_analyze(solver* s, clause* c, veci* learnt)
+{
+ lit* trail = s->trail;
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int cnt = 0;
+ lit p = lit_Undef;
+ int ind = s->qtail-1;
+ lit* lits;
+ int i, j, minl;
+ int* tagged;
+
+ veci_push(learnt,lit_Undef);
+
+ do{
+ assert(c != 0);
+
+ if (clause_is_lit(c)){
+ lit q = clause_read_lit(c);
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }else{
+
+ if (clause_learnt(c))
+ act_clause_bump(s,c);
+
+ lits = clause_begin(c);
+ //printlits(lits,lits+clause_size(c)); printf("\n");
+ for (j = (p == lit_Undef ? 0 : 1); j < clause_size(c); j++){
+ lit q = lits[j];
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }
+ }
+
+ while (tags[lit_var(trail[ind--])] == l_Undef);
+
+ p = trail[ind+1];
+ c = reasons[lit_var(p)];
+ cnt--;
+
+ }while (cnt > 0);
+
+ *veci_begin(learnt) = lit_neg(p);
+
+ lits = veci_begin(learnt);
+ minl = 0;
+ for (i = 1; i < veci_size(learnt); i++){
+ int lev = levels[lit_var(lits[i])];
+ minl |= 1 << (lev & 31);
+ }
+
+ // simplify (full)
+ for (i = j = 1; i < veci_size(learnt); i++){
+ if (reasons[lit_var(lits[i])] == 0 || !solver_lit_removable(s,lits[i],minl))
+ lits[j++] = lits[i];
+ }
+
+ // update size of learnt + statistics
+ s->stats.max_literals += veci_size(learnt);
+ veci_resize(learnt,j);
+ s->stats.tot_literals += j;
+
+ // clear tags
+ tagged = veci_begin(&s->tagged);
+ for (i = 0; i < veci_size(&s->tagged); i++)
+ tags[tagged[i]] = l_Undef;
+ veci_resize(&s->tagged,0);
+
+#ifdef DEBUG
+ for (i = 0; i < s->size; i++)
+ assert(tags[i] == l_Undef);
+#endif
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"Learnt {", L_ind);
+ for (i = 0; i < veci_size(learnt); i++) printf(" "L_LIT, L_lit(lits[i]));
+#endif
+ if (veci_size(learnt) > 1){
+ int max_i = 1;
+ int max = levels[lit_var(lits[1])];
+ lit tmp;
+
+ for (i = 2; i < veci_size(learnt); i++)
+ if (levels[lit_var(lits[i])] > max){
+ max = levels[lit_var(lits[i])];
+ max_i = i;
+ }
+
+ tmp = lits[1];
+ lits[1] = lits[max_i];
+ lits[max_i] = tmp;
+ }
+#ifdef VERBOSEDEBUG
+ {
+ int lev = veci_size(learnt) > 1 ? levels[lit_var(lits[1])] : 0;
+ printf(" } at level %d\n", lev);
+ }
+#endif
+}
+
+
+clause* solver_propagate(solver* s)
+{
+ lbool* values = s->assigns;
+ clause* confl = (clause*)0;
+ lit* lits;
+
+ //printf("solver_propagate\n");
+ while (confl == 0 && s->qtail - s->qhead > 0){
+ lit p = s->trail[s->qhead++];
+ vecp* ws = solver_read_wlist(s,p);
+ clause **begin = (clause**)vecp_begin(ws);
+ clause **end = begin + vecp_size(ws);
+ clause **i, **j;
+
+ s->stats.propagations++;
+ s->simpdb_props--;
+
+ //printf("checking lit %d: "L_LIT"\n", veci_size(ws), L_lit(p));
+ for (i = j = begin; i < end; ){
+ if (clause_is_lit(*i)){
+ *j++ = *i;
+ if (!enqueue(s,clause_read_lit(*i),clause_from_lit(p))){
+ confl = s->binary;
+ (clause_begin(confl))[1] = lit_neg(p);
+ (clause_begin(confl))[0] = clause_read_lit(*i++);
+
+ // Copy the remaining watches:
+ while (i < end)
+ *j++ = *i++;
+ }
+ }else{
+ lit false_lit;
+ lbool sig;
+
+ lits = clause_begin(*i);
+
+ // Make sure the false literal is data[1]:
+ false_lit = lit_neg(p);
+ if (lits[0] == false_lit){
+ lits[0] = lits[1];
+ lits[1] = false_lit;
+ }
+ assert(lits[1] == false_lit);
+ //printf("checking clause: "); printlits(lits, lits+clause_size(*i)); printf("\n");
+
+ // If 0th watch is true, then clause is already satisfied.
+ sig = !lit_sign(lits[0]); sig += sig - 1;
+ if (values[lit_var(lits[0])] == sig){
+ *j++ = *i;
+ }else{
+ // Look for new watch:
+ lit* stop = lits + clause_size(*i);
+ lit* k;
+ for (k = lits + 2; k < stop; k++){
+ lbool sig = lit_sign(*k); sig += sig - 1;
+ if (values[lit_var(*k)] != sig){
+ lits[1] = *k;
+ *k = false_lit;
+ vecp_push(solver_read_wlist(s,lit_neg(lits[1])),*i);
+ goto next; }
+ }
+
+ *j++ = *i;
+ // Clause is unit under assignment:
+ if (!enqueue(s,lits[0], *i)){
+ confl = *i++;
+ // Copy the remaining watches:
+ while (i < end)
+ *j++ = *i++;
+ }
+ }
+ }
+ next:
+ i++;
+ }
+
+ s->stats.inspects += j - (clause**)vecp_begin(ws);
+ vecp_resize(ws,j - (clause**)vecp_begin(ws));
+ }
+
+ return confl;
+}
+
+static inline int clause_cmp (const void* x, const void* y) {
+ return clause_size((clause*)x) > 2 && (clause_size((clause*)y) == 2 || clause_activity((clause*)x) < clause_activity((clause*)y)) ? -1 : 1; }
+
+void solver_reducedb(solver* s)
+{
+ int i, j;
+ double extra_lim = s->cla_inc / vecp_size(&s->learnts); // Remove any clause below this activity
+ clause** learnts = (clause**)vecp_begin(&s->learnts);
+ clause** reasons = s->reasons;
+
+ sort(vecp_begin(&s->learnts), vecp_size(&s->learnts), &clause_cmp);
+
+ for (i = j = 0; i < vecp_size(&s->learnts) / 2; i++){
+ if (clause_size(learnts[i]) > 2 && reasons[lit_var(*clause_begin(learnts[i]))] != learnts[i])
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+ for (; i < vecp_size(&s->learnts); i++){
+ if (clause_size(learnts[i]) > 2 && reasons[lit_var(*clause_begin(learnts[i]))] != learnts[i] && clause_activity(learnts[i]) < extra_lim)
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+
+ //printf("reducedb deleted %d\n", vecp_size(&s->learnts) - j);
+
+
+ vecp_resize(&s->learnts,j);
+}
+
+static lbool solver_search(solver* s, int nof_conflicts, int nof_learnts)
+{
+ int* levels = s->levels;
+ double var_decay = 0.95;
+ double clause_decay = 0.999;
+ double random_var_freq = 0.02;
+
+ int conflictC = 0;
+ veci learnt_clause;
+
+ assert(s->root_level == solver_dlevel(s));
+
+ s->stats.starts++;
+ s->var_decay = (float)(1 / var_decay );
+ s->cla_decay = (float)(1 / clause_decay);
+ veci_resize(&s->model,0);
+ veci_new(&learnt_clause);
+
+ for (;;){
+ clause* confl = solver_propagate(s);
+ if (confl != 0){
+ // CONFLICT
+ int blevel;
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"**CONFLICT**\n", L_ind);
+#endif
+ s->stats.conflicts++; conflictC++;
+ if (solver_dlevel(s) == s->root_level){
+ veci_delete(&learnt_clause);
+ return l_False;
+ }
+
+ veci_resize(&learnt_clause,0);
+ solver_analyze(s, confl, &learnt_clause);
+ blevel = veci_size(&learnt_clause) > 1 ? levels[lit_var(veci_begin(&learnt_clause)[1])] : s->root_level;
+ blevel = s->root_level > blevel ? s->root_level : blevel;
+ solver_canceluntil(s,blevel);
+ solver_record(s,&learnt_clause);
+ act_var_decay(s);
+ act_clause_decay(s);
+
+ }else{
+ // NO CONFLICT
+ int next;
+
+ if (nof_conflicts >= 0 && conflictC >= nof_conflicts){
+ // Reached bound on number of conflicts:
+ s->progress_estimate = solver_progress(s);
+ solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+ return l_Undef; }
+
+ if (solver_dlevel(s) == 0)
+ // Simplify the set of problem clauses:
+ solver_simplify(s);
+
+ if (nof_learnts >= 0 && vecp_size(&s->learnts) - s->qtail >= nof_learnts)
+ // Reduce the set of learnt clauses:
+ solver_reducedb(s);
+
+ // New variable decision:
+ s->stats.decisions++;
+ next = order_select(s,(float)random_var_freq);
+
+ if (next == var_Undef){
+ // Model found:
+ lbool* values = s->assigns;
+ int i;
+ for (i = 0; i < s->size; i++) veci_push(&s->model,(int)values[i]);
+ solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+
+ /*
+ veci apa; veci_new(&apa);
+ for (i = 0; i < s->size; i++)
+ veci_push(&apa,(int)(s->model.ptr[i] == l_True ? toLit(i) : lit_neg(toLit(i))));
+ printf("model: "); printlits((lit*)apa.ptr, (lit*)apa.ptr + veci_size(&apa)); printf("\n");
+ veci_delete(&apa);
+ */
+
+ return l_True;
+ }
+
+ assume(s,lit_neg(toLit(next)));
+ }
+ }
+
+ return l_Undef; // cannot happen
+}
+
+//=================================================================================================
+// External solver functions:
+
+solver* solver_new(void)
+{
+ solver* s = (solver*)malloc(sizeof(solver));
+
+ // initialize vectors
+ vecp_new(&s->clauses);
+ vecp_new(&s->learnts);
+ veci_new(&s->order);
+ veci_new(&s->trail_lim);
+ veci_new(&s->tagged);
+ veci_new(&s->stack);
+ veci_new(&s->model);
+
+ // initialize arrays
+ s->wlists = 0;
+ s->activity = 0;
+ s->assigns = 0;
+ s->orderpos = 0;
+ s->reasons = 0;
+ s->levels = 0;
+ s->tags = 0;
+ s->trail = 0;
+
+
+ // initialize other vars
+ s->size = 0;
+ s->cap = 0;
+ s->qhead = 0;
+ s->qtail = 0;
+ s->cla_inc = 1;
+ s->cla_decay = 1;
+ s->var_inc = 1;
+ s->var_decay = 1;
+ s->root_level = 0;
+ s->simpdb_assigns = 0;
+ s->simpdb_props = 0;
+ s->random_seed = 91648253;
+ s->progress_estimate = 0;
+ s->binary = (clause*)malloc(sizeof(clause) + sizeof(lit)*2);
+ s->binary->size_learnt = (2 << 1);
+ s->verbosity = 0;
+
+ s->stats.starts = 0;
+ s->stats.decisions = 0;
+ s->stats.propagations = 0;
+ s->stats.inspects = 0;
+ s->stats.conflicts = 0;
+ s->stats.clauses = 0;
+ s->stats.clauses_literals = 0;
+ s->stats.learnts = 0;
+ s->stats.learnts_literals = 0;
+ s->stats.max_literals = 0;
+ s->stats.tot_literals = 0;
+
+ return s;
+}
+
+
+void solver_delete(solver* s)
+{
+ int i;
+ for (i = 0; i < vecp_size(&s->clauses); i++)
+ free(vecp_begin(&s->clauses)[i]);
+
+ for (i = 0; i < vecp_size(&s->learnts); i++)
+ free(vecp_begin(&s->learnts)[i]);
+
+ // delete vectors
+ vecp_delete(&s->clauses);
+ vecp_delete(&s->learnts);
+ veci_delete(&s->order);
+ veci_delete(&s->trail_lim);
+ veci_delete(&s->tagged);
+ veci_delete(&s->stack);
+ veci_delete(&s->model);
+ free(s->binary);
+
+ // delete arrays
+ if (s->wlists != 0){
+ int i;
+ for (i = 0; i < s->size*2; i++)
+ vecp_delete(&s->wlists[i]);
+
+ // if one is different from null, all are
+ free(s->wlists);
+ free(s->activity );
+ free(s->assigns );
+ free(s->orderpos );
+ free(s->reasons );
+ free(s->levels );
+ free(s->trail );
+ free(s->tags );
+ }
+
+ free(s);
+}
+
+
+bool solver_addclause(solver* s, lit* begin, lit* end)
+{
+ lit *i,*j;
+ int maxvar;
+ lbool* values;
+ lit last;
+
+ if (begin == end) return false;
+
+ //printlits(begin,end); printf("\n");
+ // insertion sort
+ maxvar = lit_var(*begin);
+ for (i = begin + 1; i < end; i++){
+ lit l = *i;
+ maxvar = lit_var(l) > maxvar ? lit_var(l) : maxvar;
+ for (j = i; j > begin && *(j-1) > l; j--)
+ *j = *(j-1);
+ *j = l;
+ }
+ solver_setnvars(s,maxvar+1);
+
+ //printlits(begin,end); printf("\n");
+ values = s->assigns;
+
+ // delete duplicates
+ last = lit_Undef;
+ for (i = j = begin; i < end; i++){
+ //printf("lit: "L_LIT", value = %d\n", L_lit(*i), (lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)]));
+ lbool sig = !lit_sign(*i); sig += sig - 1;
+ if (*i == lit_neg(last) || sig == values[lit_var(*i)])
+ return true; // tautology
+ else if (*i != last && values[lit_var(*i)] == l_Undef)
+ last = *j++ = *i;
+ }
+
+ //printf("final: "); printlits(begin,j); printf("\n");
+
+ if (j == begin) // empty clause
+ return false;
+ else if (j - begin == 1) // unit clause
+ return enqueue(s,*begin,(clause*)0);
+
+ // create new clause
+ vecp_push(&s->clauses,clause_new(s,begin,j,0));
+
+
+ s->stats.clauses++;
+ s->stats.clauses_literals += j - begin;
+
+ return true;
+}
+
+
+bool solver_simplify(solver* s)
+{
+ clause** reasons;
+ int type;
+
+ assert(solver_dlevel(s) == 0);
+
+ if (solver_propagate(s) != 0)
+ return false;
+
+ if (s->qhead == s->simpdb_assigns || s->simpdb_props > 0)
+ return true;
+
+ reasons = s->reasons;
+ for (type = 0; type < 2; type++){
+ vecp* cs = type ? &s->learnts : &s->clauses;
+ clause** cls = (clause**)vecp_begin(cs);
+
+ int i, j;
+ for (j = i = 0; i < vecp_size(cs); i++){
+ if (reasons[lit_var(*clause_begin(cls[i]))] != cls[i] &&
+ clause_simplify(s,cls[i]) == l_True)
+ clause_remove(s,cls[i]);
+ else
+ cls[j++] = cls[i];
+ }
+ vecp_resize(cs,j);
+ }
+
+ s->simpdb_assigns = s->qhead;
+ // (shouldn't depend on 'stats' really, but it will do for now)
+ s->simpdb_props = (int)(s->stats.clauses_literals + s->stats.learnts_literals);
+
+ return true;
+}
+
+
+bool solver_solve(solver* s, lit* begin, lit* end)
+{
+ double nof_conflicts = 100;
+ double nof_learnts = solver_nclauses(s) / 3;
+ lbool status = l_Undef;
+ lbool* values = s->assigns;
+ lit* i;
+
+ //printf("solve: "); printlits(begin, end); printf("\n");
+ for (i = begin; i < end; i++){
+ switch (lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)]){
+ case 1: /* l_True: */
+ break;
+ case 0: /* l_Undef */
+ assume(s, *i);
+ if (solver_propagate(s) == NULL)
+ break;
+ // falltrough
+ case -1: /* l_False */
+ solver_canceluntil(s, 0);
+ return false;
+ }
+ }
+
+ s->root_level = solver_dlevel(s);
+
+ if (s->verbosity >= 1){
+ printf("==================================[MINISAT]===================================\n");
+ printf("| Conflicts | ORIGINAL | LEARNT | Progress |\n");
+ printf("| | Clauses Literals | Limit Clauses Literals Lit/Cl | |\n");
+ printf("==============================================================================\n");
+ }
+
+ while (status == l_Undef){
+ double Ratio = (s->stats.learnts == 0)? 0.0 :
+ s->stats.learnts_literals / (double)s->stats.learnts;
+
+ if (s->verbosity >= 1){
+ printf("| %9.0f | %7.0f %8.0f | %7.0f %7.0f %8.0f %7.1f | %6.3f %% |\n",
+ (double)s->stats.conflicts,
+ (double)s->stats.clauses,
+ (double)s->stats.clauses_literals,
+ (double)nof_learnts,
+ (double)s->stats.learnts,
+ (double)s->stats.learnts_literals,
+ Ratio,
+ s->progress_estimate*100);
+ fflush(stdout);
+ }
+ status = solver_search(s,(int)nof_conflicts, (int)nof_learnts);
+ nof_conflicts *= 1.5;
+ nof_learnts *= 1.1;
+ }
+ if (s->verbosity >= 1)
+ printf("==============================================================================\n");
+
+ solver_canceluntil(s,0);
+ return status != l_False;
+}
+
+
+int solver_nvars(solver* s)
+{
+ return s->size;
+}
+
+
+int solver_nclauses(solver* s)
+{
+ return vecp_size(&s->clauses);
+}
+
+
+int solver_nconflicts(solver* s)
+{
+ return (int)s->stats.conflicts;
+}
+
+//=================================================================================================
+// Sorting functions (sigh):
+
+static inline void selectionsort(void** array, int size, int(*comp)(const void *, const void *))
+{
+ int i, j, best_i;
+ void* tmp;
+
+ for (i = 0; i < size-1; i++){
+ best_i = i;
+ for (j = i+1; j < size; j++){
+ if (comp(array[j], array[best_i]) < 0)
+ best_i = j;
+ }
+ tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
+ }
+}
+
+
+static void sortrnd(void** array, int size, int(*comp)(const void *, const void *), double* seed)
+{
+ if (size <= 15)
+ selectionsort(array, size, comp);
+
+ else{
+ void* pivot = array[irand(seed, size)];
+ void* tmp;
+ int i = -1;
+ int j = size;
+
+ for(;;){
+ do i++; while(comp(array[i], pivot)<0);
+ do j--; while(comp(pivot, array[j])<0);
+
+ if (i >= j) break;
+
+ tmp = array[i]; array[i] = array[j]; array[j] = tmp;
+ }
+
+ sortrnd(array , i , comp, seed);
+ sortrnd(&array[i], size-i, comp, seed);
+ }
+}
+
+void sort(void** array, int size, int(*comp)(const void *, const void *))
+{
+ double seed = 91648253;
+ sortrnd(array,size,comp,&seed);
+}
diff --git a/test/monniaux/minisat/solver.h b/test/monniaux/minisat/solver.h
new file mode 100644
index 00000000..4b96b017
--- /dev/null
+++ b/test/monniaux/minisat/solver.h
@@ -0,0 +1,142 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#include <stdint.h>
+
+#ifndef solver_h
+#define solver_h
+
+#ifdef _WIN32
+#define inline __inline // compatible with MS VS 6.0
+#endif
+
+#include "vec.h"
+
+//=================================================================================================
+// Simple types:
+
+// does not work for c++
+typedef int bool;
+static const bool true = 1;
+static const bool false = 0;
+
+typedef int lit;
+typedef char lbool;
+
+#if 0
+#ifdef _WIN32
+typedef signed __int64 uint64; // compatible with MS VS 6.0
+#else
+typedef unsigned long long uint64;
+#endif
+#endif
+typedef uint64_t uint64;
+
+static const int var_Undef = -1;
+static const lit lit_Undef = -2;
+
+static const lbool l_Undef = 0;
+static const lbool l_True = 1;
+static const lbool l_False = -1;
+
+static inline lit toLit (int v) { return v + v; }
+static inline lit lit_neg (lit l) { return l ^ 1; }
+static inline int lit_var (lit l) { return l >> 1; }
+static inline int lit_sign(lit l) { return (l & 1); }
+
+
+//=================================================================================================
+// Public interface:
+
+struct solver_t;
+typedef struct solver_t solver;
+
+extern solver* solver_new(void);
+extern void solver_delete(solver* s);
+
+extern bool solver_addclause(solver* s, lit* begin, lit* end);
+extern bool solver_simplify(solver* s);
+extern bool solver_solve(solver* s, lit* begin, lit* end);
+
+extern int solver_nvars(solver* s);
+extern int solver_nclauses(solver* s);
+extern int solver_nconflicts(solver* s);
+
+extern void solver_setnvars(solver* s,int n);
+
+struct stats_t
+{
+ uint64 starts, decisions, propagations, inspects, conflicts;
+ uint64 clauses, clauses_literals, learnts, learnts_literals, max_literals, tot_literals;
+};
+typedef struct stats_t stats;
+
+//=================================================================================================
+// Solver representation:
+
+struct clause_t;
+typedef struct clause_t clause;
+
+struct solver_t
+{
+ int size; // nof variables
+ int cap; // size of varmaps
+ int qhead; // Head index of queue.
+ int qtail; // Tail index of queue.
+
+ // clauses
+ vecp clauses; // List of problem constraints. (contains: clause*)
+ vecp learnts; // List of learnt clauses. (contains: clause*)
+
+ // activities
+ double var_inc; // Amount to bump next variable with.
+ double var_decay; // INVERSE decay factor for variable activity: stores 1/decay.
+ float cla_inc; // Amount to bump next clause with.
+ float cla_decay; // INVERSE decay factor for clause activity: stores 1/decay.
+
+ vecp* wlists; //
+ double* activity; // A heuristic measurement of the activity of a variable.
+ lbool* assigns; // Current values of variables.
+ int* orderpos; // Index in variable order.
+ clause** reasons; //
+ int* levels; //
+ lit* trail;
+
+ clause* binary; // A temporary binary clause
+ lbool* tags; //
+ veci tagged; // (contains: var)
+ veci stack; // (contains: var)
+
+ veci order; // Variable order. (heap) (contains: var)
+ veci trail_lim; // Separator indices for different decision levels in 'trail'. (contains: int)
+ veci model; // If problem is solved, this vector contains the model (contains: lbool).
+
+ int root_level; // Level of first proper decision.
+ int simpdb_assigns;// Number of top-level assignments at last 'simplifyDB()'.
+ int simpdb_props; // Number of propagations before next 'simplifyDB()'.
+ double random_seed;
+ double progress_estimate;
+ int verbosity; // Verbosity level. 0=silent, 1=some progress report, 2=everything
+
+ stats stats;
+};
+
+#endif
diff --git a/test/monniaux/minisat/sudoku.sat b/test/monniaux/minisat/sudoku.sat
new file mode 120000
index 00000000..1b78bb27
--- /dev/null
+++ b/test/monniaux/minisat/sudoku.sat
@@ -0,0 +1 @@
+../picosat-965/sudoku.sat \ No newline at end of file
diff --git a/test/monniaux/minisat/vec.h b/test/monniaux/minisat/vec.h
new file mode 100644
index 00000000..5ccf5fc2
--- /dev/null
+++ b/test/monniaux/minisat/vec.h
@@ -0,0 +1,84 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#ifndef vec_h
+#define vec_h
+
+#include <stdlib.h>
+
+
+// vector of 32-bit intergers (added for 64-bit portability)
+struct veci_t {
+ int size;
+ int cap;
+ int* ptr;
+};
+typedef struct veci_t veci;
+
+static inline void veci_new (veci* v) {
+ v->size = 0;
+ v->cap = 4;
+ v->ptr = (int*)malloc(sizeof(int)*v->cap);
+}
+
+static inline void veci_delete (veci* v) { free(v->ptr); }
+static inline int* veci_begin (veci* v) { return v->ptr; }
+static inline int veci_size (veci* v) { return v->size; }
+static inline void veci_resize (veci* v, int k) { v->size = k; } // only safe to shrink !!
+static inline void veci_push (veci* v, int e)
+{
+ if (v->size == v->cap) {
+ int newsize = v->cap * 2+1;
+ v->ptr = (int*)realloc(v->ptr,sizeof(int)*newsize);
+ v->cap = newsize; }
+ v->ptr[v->size++] = e;
+}
+
+
+// vector of 32- or 64-bit pointers
+struct vecp_t {
+ int size;
+ int cap;
+ void** ptr;
+};
+typedef struct vecp_t vecp;
+
+static inline void vecp_new (vecp* v) {
+ v->size = 0;
+ v->cap = 4;
+ v->ptr = (void**)malloc(sizeof(void*)*v->cap);
+}
+
+static inline void vecp_delete (vecp* v) { free(v->ptr); }
+static inline void** vecp_begin (vecp* v) { return v->ptr; }
+static inline int vecp_size (vecp* v) { return v->size; }
+static inline void vecp_resize (vecp* v, int k) { v->size = k; } // only safe to shrink !!
+static inline void vecp_push (vecp* v, void* e)
+{
+ if (v->size == v->cap) {
+ int newsize = v->cap * 2+1;
+ v->ptr = (void**)realloc(v->ptr,sizeof(void*)*newsize);
+ v->cap = newsize; }
+ v->ptr[v->size++] = e;
+}
+
+
+#endif
diff --git a/test/monniaux/mod_int_mat/Makefile b/test/monniaux/mod_int_mat/Makefile
new file mode 100644
index 00000000..ff90f901
--- /dev/null
+++ b/test/monniaux/mod_int_mat/Makefile
@@ -0,0 +1,40 @@
+CFLAGS=-Wall -O3 -std=c99
+KVX_CC=k1-cos-gcc
+KVX_CFLAGS=-Wall -O3 -std=c99
+KVX_CCOMP=../../../ccomp
+KVX_CCOMPFLAGS=-Wall -O3
+
+PRODUCTS=int_mat.host int_mat.gcc.kvx.out int_mat.ccomp.kvx.out int_mat.ccomp.kvx.s int_mat.gcc.kvx.s int_mat.gcc.kvx int_mat.ccomp.kvx
+
+all: $(PRODUCTS)
+
+%.gcc.kvx.s: %.c
+ $(KVX_CC) $(KVX_CFLAGS) -S $< -o $@
+
+%.gcc.kvx.o: %.gcc.kvx.s
+ $(KVX_CC) $(KVX_CFLAGS) -c $< -o $@
+
+%.ccomp.kvx.s: %.c
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) -S $< -o $@
+
+%.ccomp.kvx.o: %.ccomp.kvx.s
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) -c $< -o $@
+
+int_mat.host: int_mat.c int_mat_run.c modint.h
+ $(CC) $(CFLAGS) int_mat.c int_mat_run.c -o $@
+
+int_mat.gcc.kvx.s int_mat.ccomp.kvx.s int_mat_run.gcc.kvx.s: modint.h
+
+int_mat.gcc.kvx: int_mat.gcc.kvx.o int_mat_run.gcc.kvx.o
+ $(KVX_CC) $(KVX_CFLAGS) $+ -o $@
+
+int_mat.ccomp.kvx: int_mat.ccomp.kvx.o int_mat_run.gcc.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+%.kvx.out: %.kvx
+ k1-cluster --cycle-based -- $< | tee $@
+
+clean:
+ $(RM) -f $(PRODUCTS) int_mat.gcc.kvx.o int_mat.ccomp.kvx.o int_mat_run.gcc.kvx.o
+
+.PHONY: clean
diff --git a/test/monniaux/mod_int_mat/int_mat.c b/test/monniaux/mod_int_mat/int_mat.c
new file mode 100644
index 00000000..167c91fd
--- /dev/null
+++ b/test/monniaux/mod_int_mat/int_mat.c
@@ -0,0 +1,218 @@
+#include "modint.h"
+
+void modint_mat_mul1(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ c[i*stride_c+k] = 0;
+ }
+ }
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ for(unsigned j=0; j<n; j++) {
+ c[i*stride_c+k] += a[i*stride_a+j] * b[j*stride_b+k];
+ }
+ }
+ }
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ c[i*stride_c+k] %= MODULUS;
+ }
+ }
+}
+
+void modint_mat_mul2(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ modint total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total += a[i*stride_a + j] * b[j*stride_b + k];
+ }
+ c[i*stride_c+k] = total % MODULUS;
+ }
+ }
+}
+
+void modint_mat_mul3(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ modint total0 = 0, total1 = 0;
+ unsigned j;
+ for(j=0; j+1<n; j+=2) {
+ total0 += a[i*stride_a + j] * b[j*stride_b + k];
+ total1 += a[i*stride_a + (j+1)] * b[(j+1)*stride_b + k];
+ }
+ if (j < n) {
+ total0 += a[i*stride_a + j] * b[j*stride_b + k];
+ }
+ c[i*stride_c+k] = (total0+total1) % MODULUS;
+ }
+ }
+}
+
+void modint_mat_mul4(unsigned m, unsigned n, unsigned p,
+ modint * c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ const modint *pa_i = a;
+ modint * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const modint *pb_j_k = b+k, *pa_i_j = pa_i;
+ modint total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ }
+ pc_i[k] = total % MODULUS;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+void modint_mat_mul5(unsigned m, unsigned n, unsigned p,
+ modint * c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ const modint *pa_i = a;
+ modint * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const modint *pb_j_k = b+k, *pa_i_j = pa_i;
+ modint total = 0;
+ for(unsigned j2=0, n2=n/2; j2<n2; j2++) {
+ modint p0 = *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ modint p1 = *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total += p0 + p1;
+ }
+ if (n%2) {
+ total += *pa_i_j * *pb_j_k;
+ }
+ pc_i[k] = total % MODULUS;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+void modint_mat_mul6(unsigned m, unsigned n, unsigned p,
+ modint * c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ const modint *pa_i = a;
+ modint * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const modint *pb_j_k = b+k, *pa_i_j = pa_i;
+ modint total = 0;
+ unsigned j2=0, n2=n/2;
+ if (n2 > 0) {
+ do {
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ j2++;
+ } while (j2 < n2);
+ }
+ if (n%2) {
+ total += *pa_i_j * *pb_j_k;
+ }
+ pc_i[k] = total % MODULUS;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+void modint_mat_mul7(unsigned m, unsigned n, unsigned p,
+ modint * c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ const modint *pa_i = a;
+ modint * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const modint *pb_j_k = b+k, *pa_i_j = pa_i;
+ modint total = 0;
+ {
+ unsigned j4=0, n4=n/4;
+ if (n4 > 0) {
+ do {
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%4;
+ if (n4 > 0) {
+ do {
+ total += *pa_i_j * *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k] = total % MODULUS;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+modint modint_random(void) {
+ static uint32_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next % MODULUS;
+}
+
+void modint_mat_random(unsigned m,
+ unsigned n,
+ modint *a, unsigned stride_a) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ a[i*stride_a + j] = modint_random();
+ }
+ }
+}
+
+bool modint_mat_equal(unsigned m,
+ unsigned n,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ if (a[i*stride_a + j] != b[i*stride_b + j]) return false;
+ }
+ }
+ return true;
+}
diff --git a/test/monniaux/mod_int_mat/int_mat_run.c b/test/monniaux/mod_int_mat/int_mat_run.c
new file mode 100644
index 00000000..860c248d
--- /dev/null
+++ b/test/monniaux/mod_int_mat/int_mat_run.c
@@ -0,0 +1,90 @@
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "modint.h"
+#include "../cycles.h"
+
+int main() {
+ const unsigned m = 60, n = 31, p = 50;
+ cycle_count_config();
+ modint *a = malloc(sizeof(modint) * m * n);
+ modint_mat_random(m, n, a, n);
+ modint *b = malloc(sizeof(modint) * n * p);
+ modint_mat_random(n, p, b, p);
+
+ modint *c1 = malloc(sizeof(modint) * m * p);
+ cycle_t c1_time = get_cycle();
+ modint_mat_mul1(m, n, p, c1, p, a, n, b, p);
+ c1_time = get_cycle()-c1_time;
+
+ modint *c2 = malloc(sizeof(modint) * m * p);
+ cycle_t c2_time = get_cycle();
+ modint_mat_mul2(m, n, p, c2, p, a, n, b, p);
+ c2_time = get_cycle()-c2_time;
+
+ modint *c3 = malloc(sizeof(modint) * m * p);
+ cycle_t c3_time = get_cycle();
+ modint_mat_mul3(m, n, p, c3, p, a, n, b, p);
+ c3_time = get_cycle()-c3_time;
+
+ modint *c4 = malloc(sizeof(modint) * m * p);
+ cycle_t c4_time = get_cycle();
+ modint_mat_mul4(m, n, p, c4, p, a, n, b, p);
+ c4_time = get_cycle()-c4_time;
+
+ modint *c5 = malloc(sizeof(modint) * m * p);
+ cycle_t c5_time = get_cycle();
+ modint_mat_mul5(m, n, p, c5, p, a, n, b, p);
+ c5_time = get_cycle()-c5_time;
+
+ modint *c6 = malloc(sizeof(modint) * m * p);
+ cycle_t c6_time = get_cycle();
+ modint_mat_mul6(m, n, p, c6, p, a, n, b, p);
+ c6_time = get_cycle()-c6_time;
+
+ modint *c7 = malloc(sizeof(modint) * m * p);
+ cycle_t c7_time = get_cycle();
+ modint_mat_mul7(m, n, p, c7, p, a, n, b, p);
+ c7_time = get_cycle()-c7_time;
+
+ printf("c1==c2: %s\n"
+ "c1==c3: %s\n"
+ "c1==c4: %s\n"
+ "c1==c5: %s\n"
+ "c1==c6: %s\n"
+ "c1==c7: %s\n"
+ "c1_time = %" PRIu64 "\n"
+ "c2_time = %" PRIu64 "\n"
+ "c3_time = %" PRIu64 "\n"
+ "c4_time = %" PRIu64 "\n"
+ "c5_time = %" PRIu64 "\n"
+ "c6_time = %" PRIu64 "\n"
+ "c7_time = %" PRIu64 "\n",
+
+ modint_mat_equal(m, n, c1, p, c2, p)?"true":"false",
+ modint_mat_equal(m, n, c1, p, c3, p)?"true":"false",
+ modint_mat_equal(m, n, c1, p, c4, p)?"true":"false",
+ modint_mat_equal(m, n, c1, p, c5, p)?"true":"false",
+ modint_mat_equal(m, n, c1, p, c6, p)?"true":"false",
+ modint_mat_equal(m, n, c1, p, c7, p)?"true":"false",
+
+ c1_time,
+ c2_time,
+ c3_time,
+ c4_time,
+ c5_time,
+ c6_time,
+ c7_time);
+
+ free(a);
+ free(b);
+ free(c1);
+ free(c2);
+ free(c3);
+ free(c4);
+ free(c5);
+ free(c6);
+ free(c7);
+ return 0;
+}
diff --git a/test/monniaux/mod_int_mat/modint.h b/test/monniaux/mod_int_mat/modint.h
new file mode 100644
index 00000000..15c70a15
--- /dev/null
+++ b/test/monniaux/mod_int_mat/modint.h
@@ -0,0 +1,51 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef uint32_t modint;
+#define MODULUS 257
+
+void modint_mat_mul1(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul2(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul3(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul4(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul5(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul6(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+void modint_mat_mul7(unsigned m, unsigned n, unsigned p,
+ modint * restrict c, unsigned stride_c,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
+
+modint modint_random(void);
+
+void modint_mat_random(unsigned m,
+ unsigned n,
+ modint *a, unsigned stride_a);
+
+bool modint_mat_equal(unsigned m,
+ unsigned n,
+ const modint *a, unsigned stride_a,
+ const modint *b, unsigned stride_b);
diff --git a/test/monniaux/moves/array.c b/test/monniaux/moves/array.c
new file mode 100644
index 00000000..faa1d96b
--- /dev/null
+++ b/test/monniaux/moves/array.c
@@ -0,0 +1,18 @@
+void incr_double_array(double *t) {
+ double x0 = 1.0;
+ double t0 = t[0];
+ double x1 = 1.0;
+ double t1 = t[1];
+ double x2 = 1.0;
+ double t2 = t[2];
+ double x3 = 1.0;
+ double t3 = t[3];
+ t0 = t0 + x0;
+ t1 = t1 + x1;
+ t2 = t2 + x2;
+ t3 = t3 + x3;
+ t[0] = t0;
+ t[1] = t1;
+ t[2] = t2;
+ t[3] = t3;
+}
diff --git a/test/monniaux/multithreaded_volatile/Makefile b/test/monniaux/multithreaded_volatile/Makefile
new file mode 100644
index 00000000..6c7dd663
--- /dev/null
+++ b/test/monniaux/multithreaded_volatile/Makefile
@@ -0,0 +1,18 @@
+all: volatile.ccomp.kvx volatile.gcc.kvx
+
+volatile.ccomp.kvx : volatile.ccomp.kvx.s
+ k1-cos-gcc $< -o $@
+
+volatile.gcc.kvx : volatile.gcc.kvx.s
+ k1-cos-gcc $< -o $@
+
+volatile.ccomp.kvx.s : volatile.c
+ ../../../ccomp -O2 -Wall -S $< -o $@
+
+volatile.gcc.kvx.s : volatile.c
+ k1-cos-gcc -O2 -Wall -Werror=implicit -std=gnu99 -S $< -o $@
+
+clean:
+ -rm -f *.kvx *.s
+
+.PHONY: clean
diff --git a/test/monniaux/multithreaded_volatile/volatile.c b/test/monniaux/multithreaded_volatile/volatile.c
new file mode 100644
index 00000000..f8ffee2d
--- /dev/null
+++ b/test/monniaux/multithreaded_volatile/volatile.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <time.h>
+#include <pthread.h>
+
+typedef unsigned data;
+
+static inline data powM(data x, unsigned e) {
+ data y = 1;
+ for(unsigned i=0; i<e; i++) {
+ y = (y * x) % 65537;
+ }
+ return y;
+}
+
+void* second_thread_entry(void *ptr) {
+ *((volatile data*) ptr) = powM(3, 65536);
+ return NULL;
+}
+
+int main() {
+ pthread_t second_thread_id;
+ volatile data value;
+ pthread_create(&second_thread_id, NULL,
+ second_thread_entry, (void*) &value);
+ value = 69;
+ data correct = powM(3, 65536*2);
+ data read = value;
+ pthread_join(second_thread_id, NULL);
+ printf("%u %u %s\n", read, correct, read == correct ? "OK" : "FAIL");
+}
diff --git a/test/monniaux/nand/nand.c b/test/monniaux/nand/nand.c
new file mode 100644
index 00000000..e628c1cf
--- /dev/null
+++ b/test/monniaux/nand/nand.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+typedef unsigned long scalar;
+
+scalar not(scalar x) {
+ return ~x;
+}
+
+scalar nand(scalar x, scalar y) {
+ return ~(x & y);
+}
+
+scalar nor(scalar x, scalar y) {
+ return ~(x | y);
+}
+
+scalar nxor(scalar x, scalar y) {
+ return ~(x ^ y);
+}
+
+scalar andn1(scalar x, scalar y) {
+ return ~x & y;
+}
+
+scalar andn2(scalar x, scalar y) {
+ return x & ~y;
+}
+
+scalar orn1(scalar x, scalar y) {
+ return ~x | y;
+}
+
+scalar orn2(scalar x, scalar y) {
+ return x | ~y;
+}
+
+scalar nandimm(scalar x) {
+ return ~x & 45;
+}
+
+scalar notnot(scalar x) {
+ return ~ ~ x;
+}
+
+int main() {
+ scalar x = 0xF4, y = 0x33;
+ printf("%X\n", nxor(x, y));
+}
diff --git a/test/monniaux/ncompress/Makefile b/test/monniaux/ncompress/Makefile
new file mode 100644
index 00000000..14a99d0b
--- /dev/null
+++ b/test/monniaux/ncompress/Makefile
@@ -0,0 +1,4 @@
+TARGET=compress
+EXECUTE_ARGS= < Makefile > __BASE__.Z 2> __BASE__.out
+
+include ../rules.mk
diff --git a/test/monniaux/ncompress/README.md b/test/monniaux/ncompress/README.md
new file mode 100644
index 00000000..be0e0bc2
--- /dev/null
+++ b/test/monniaux/ncompress/README.md
@@ -0,0 +1,100 @@
+# About
+
+This is version 4.2 of (N)compress (an improved version of compress 4.1).
+
+Compress is a fast, simple LZW file compressor. Compress does not have
+the highest compression rate, but it is one of the fastest programs to
+compress data. Compress is the defacto standard in the UNIX community
+for compressing files.
+
+(N)compress 4.2 has a special, fast compression hash algorithm. This
+algorithm uses more memory than the old hash table. If you don't want
+the faster hash table algorithm set 'Memory free for compress' below
+800000.
+
+zcmp, zdiff, zmore were copied from version 4.1 without any changes.
+
+The output of (N)compress 4.2 is fully compatible with that of compress 3.0.
+In other words, the output of (N)compress 4.2 may be fed into uncompress 3.0 or
+the output of compress 3.0 may be fed into uncompress 4.2.
+
+The output of (N)compress 4.2 is not compatible with that of
+compress 2.0. However, compress 4.2 still accepts the output of
+compress 2.0. To generate output that is compatible with compress
+2.0, use the undocumented "-C" flag.
+
+# Building
+
+For recent systems with GNU make, you can simply run `make` as the default
+'GNUMakefile' will get picked up.
+
+'build' is a menu driven shell script for compiling, testing and
+installing (N)compress. So to build and install (N)compress all you have to
+do is run build. Build will first test your system for default
+settings. The current compile settings are stored in a special file
+called compress.def.
+
+For user with problems with build there is a default makefile included
+called 'Makefile.def'. Also build is capable of generating a Makefile with
+all options (option genmake).
+
+# Support
+
+[![Build Status](https://travis-ci.org/vapier/ncompress.svg?branch=ncompress-4.2.4)](https://travis-ci.org/vapier/ncompress)
+
+Send comments, complaints and especially patches relating to
+ https://github.com/vapier/ncompress/issues
+
+# Licensing
+
+The ncompress code is released into the public domain. See the
+[UNLICENSE](UNLICENSE) file for more details.
+
+# Patents
+
+All existing patents on the LZW algorithm have
+[expired world-wide](http://en.wikipedia.org/wiki/LZW#Patent_issues).
+So LZW is now patent free.
+
+# Remarks
+
+- Build is a bourne shell script. On some system it is necessary to type
+ 'sh build'.
+
+- The build script usages tput for nice screen handling of the script.
+ If your system has no tput no problems.
+
+- For configuration testing build uses a lot of small C programs. During
+ those test stderr is redirected to /dev/null.
+ During the compilation of compress output is NOT redirected.
+
+- The /bin/sh under Ultrix can't handle ${var:-str} so use ksh for the
+ build script.
+
+- The output if (N)compress 4.2 is not exactly the same as compress 4.0
+ because of different table reset point. The output of (N)compress 4.2
+ is 100% compatible with compress 4.0
+
+- Some systems has performance problems with reads bigger than BUFSIZ
+ (The read a head function is not working as expected). For those
+ system use the default BSIZE input buffer size.
+
+- (N)compress can by slower on small files (<10Kb) because of a great
+ table reset overhead. Use cpio or tar to make 1 bigger file if
+ possible, it is faster and also gives a better compression ratio most
+ of the time.
+
+- (N)compress is slower in vax systems because of removing some
+ undocumented inline assembler.
+
+- files compressed on a large machine with more bits than allowed by
+ a version of compress on a smaller machine cannot be decompressed! Use the
+ "-b12" flag to generate a file on a large machine that can be uncompressed
+ on a 16-bit machine.
+
+- compatibility with compress 3.0 has not been tested in the 4.2 release of
+ (N)compress.
+
+- There has been 1 problem report in relation to GCC 2.0 on a sparc
+ workstation. GCC 2.0 seems to generate a bad compress. Use the
+ standard c compiler 'cc'.
diff --git a/test/monniaux/ncompress/UNLICENSE b/test/monniaux/ncompress/UNLICENSE
new file mode 100644
index 00000000..68a49daa
--- /dev/null
+++ b/test/monniaux/ncompress/UNLICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
diff --git a/test/monniaux/ncompress/compress42.c b/test/monniaux/ncompress/compress42.c
new file mode 100644
index 00000000..dd1efe3a
--- /dev/null
+++ b/test/monniaux/ncompress/compress42.c
@@ -0,0 +1,1948 @@
+/* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
+ *
+ * Authors:
+ * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
+ * Jim McKie (decvax!mcvax!jim)
+ * Steve Davies (decvax!vax135!petsd!peora!srd)
+ * Ken Turkowski (decvax!decwrl!turtlevax!ken)
+ * James A. Woods (decvax!ihnp4!ames!jaw)
+ * Joe Orost (decvax!vax135!petsd!joe)
+ * Dave Mack (csu@alembic.acs.com)
+ * Peter Jannesen, Network Communication Systems
+ * (peter@ncs.nl)
+ *
+ * Revision 4.2.3 92/03/14 peter@ncs.nl
+ * Optimise compress and decompress function and a lot of cleanups.
+ * New fast hash algoritme added (if more than 800Kb available).
+ *
+ * Revision 4.1 91/05/26 csu@alembic.acs.com
+ * Modified to recursively compress directories ('r' flag). As a side
+ * effect, compress will no longer attempt to compress things that
+ * aren't "regular" files. See Changes.
+ *
+ * Revision 4.0 85/07/30 12:50:00 joe
+ * Removed ferror() calls in output routine on every output except first.
+ * Prepared for release to the world.
+ *
+ * Revision 3.6 85/07/04 01:22:21 joe
+ * Remove much wasted storage by overlaying hash table with the tables
+ * used by decompress: tab_suffix[1<<BITS], stack[8000]. Updated USERMEM
+ * computations. Fixed dump_tab() DEBUG routine.
+ *
+ * Revision 3.5 85/06/30 20:47:21 jaw
+ * Change hash function to use exclusive-or. Rip out hash cache. These
+ * speedups render the megamemory version defunct, for now. Make decoder
+ * stack global. Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
+ *
+ * Revision 3.4 85/06/27 12:00:00 ken
+ * Get rid of all floating-point calculations by doing all compression ratio
+ * calculations in fixed point.
+ *
+ * Revision 3.3 85/06/24 21:53:24 joe
+ * Incorporate portability suggestion for M_XENIX. Got rid of text on #else
+ * and #endif lines. Cleaned up #ifdefs for vax and interdata.
+ *
+ * Revision 3.2 85/06/06 21:53:24 jaw
+ * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
+ * Default to "quiet" output (no compression statistics).
+ *
+ * Revision 3.1 85/05/12 18:56:13 jaw
+ * Integrate decompress() stack speedups (from early pointer mods by McKie).
+ * Repair multi-file USERMEM gaffe. Unify 'force' flags to mimic semantics
+ * of SVR2 'pack'. Streamline block-compress table clear logic. Increase
+ * output byte count by magic number size.
+ *
+ * Revision 3.0 84/11/27 11:50:00 petsd!joe
+ * Set HSIZE depending on BITS. Set BITS depending on USERMEM. Unrolled
+ * loops in clear routines. Added "-C" flag for 2.0 compatibility. Used
+ * unsigned compares on Perkin-Elmer. Fixed foreground check.
+ *
+ * Revision 2.7 84/11/16 19:35:39 ames!jaw
+ * Cache common hash codes based on input statistics; this improves
+ * performance for low-density raster images. Pass on #ifdef bundle
+ * from Turkowski.
+ *
+ * Revision 2.6 84/11/05 19:18:21 ames!jaw
+ * Vary size of hash tables to reduce time for small files.
+ * Tune PDP-11 hash function.
+ *
+ * Revision 2.5 84/10/30 20:15:14 ames!jaw
+ * Junk chaining; replace with the simpler (and, on the VAX, faster)
+ * double hashing, discussed within. Make block compression standard.
+ *
+ * Revision 2.4 84/10/16 11:11:11 ames!jaw
+ * Introduce adaptive reset for block compression, to boost the rate
+ * another several percent. (See mailing list notes.)
+ *
+ * Revision 2.3 84/09/22 22:00:00 petsd!joe
+ * Implemented "-B" block compress. Implemented REVERSE sorting of tab_next.
+ * Bug fix for last bits. Changed fwrite to putchar loop everywhere.
+ *
+ * Revision 2.2 84/09/18 14:12:21 ames!jaw
+ * Fold in news changes, small machine typedef from thomas,
+ * #ifdef interdata from joe.
+ *
+ * Revision 2.1 84/09/10 12:34:56 ames!jaw
+ * Configured fast table lookup for 32-bit machines.
+ * This cuts user time in half for b <= FBITS, and is useful for news batching
+ * from VAX to PDP sites. Also sped up decompress() [fwrite->putc] and
+ * added signal catcher [plus beef in write_error()] to delete effluvia.
+ *
+ * Revision 2.0 84/08/28 22:00:00 petsd!joe
+ * Add check for foreground before prompting user. Insert maxbits into
+ * compressed file. Force file being uncompressed to end with ".Z".
+ * Added "-c" flag and "zcat". Prepared for release.
+ *
+ * Revision 1.10 84/08/24 18:28:00 turtlevax!ken
+ * Will only compress regular files (no directories), added a magic number
+ * header (plus an undocumented -n flag to handle old files without headers),
+ * added -f flag to force overwriting of possibly existing destination file,
+ * otherwise the user is prompted for a response. Will tack on a .Z to a
+ * filename if it doesn't have one when decompressing. Will only replace
+ * file if it was compressed.
+ *
+ * Revision 1.9 84/08/16 17:28:00 turtlevax!ken
+ * Removed scanargs(), getopt(), added .Z extension and unlimited number of
+ * filenames to compress. Flags may be clustered (-Ddvb12) or separated
+ * (-D -d -v -b 12), or combination thereof. Modes and other status is
+ * copied with copystat(). -O bug for 4.2 seems to have disappeared with
+ * 1.8.
+ *
+ * Revision 1.8 84/08/09 23:15:00 joe
+ * Made it compatible with vax version, installed jim's fixes/enhancements
+ *
+ * Revision 1.6 84/08/01 22:08:00 joe
+ * Sped up algorithm significantly by sorting the compress chain.
+ *
+ * Revision 1.5 84/07/13 13:11:00 srd
+ * Added C version of vax asm routines. Changed structure to arrays to
+ * save much memory. Do unsigned compares where possible (faster on
+ * Perkin-Elmer)
+ *
+ * Revision 1.4 84/07/05 03:11:11 thomas
+ * Clean up the code a little and lint it. (Lint complains about all
+ * the regs used in the asm, but I'm not going to "fix" this.)
+ *
+ * Revision 1.3 84/07/05 02:06:54 thomas
+ * Minor fixes.
+ *
+ * Revision 1.2 84/07/05 00:27:27 thomas
+ * Add variable bit length output.
+ *
+ */
+
+/* FIXME DMonniaux */
+#define INT_MOD(x, y) ((x) % (y))
+#include "../clock.h"
+
+/* DMonniaux for utime and strdup */
+#define UTIME_H
+#define _BSD_SOURCE 1
+#undef __STRICT_ANSI__
+
+#ifdef _MSC_VER
+# define WINDOWS
+#endif
+
+#ifdef __MINGW32__
+# define DIRENT
+# define MINGW
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#if !defined(DOS) && !defined(WINDOWS)
+# include <unistd.h>
+#endif
+
+#ifdef DIRENT
+# include <dirent.h>
+# define RECURSIVE 1
+# undef SYSDIR
+#endif
+#ifdef SYSDIR
+# include <sys/dir.h>
+# define RECURSIVE 1
+#endif
+#ifdef UTIME_H
+# include <utime.h>
+#else
+ struct utimbuf {
+ time_t actime;
+ time_t modtime;
+ };
+#endif
+
+#ifdef __STDC__
+# define ARGS(a) a
+#else
+# define ARGS(a) ()
+#endif
+
+#ifndef SIG_TYPE
+# define SIG_TYPE void (*)()
+#endif
+
+#if defined(AMIGA) || defined(DOS) || defined(MINGW) || defined(WINDOWS) || defined(__KVX__)
+# define chmod(pathname, mode) 0
+# define chown(pathname, owner, group) 0
+# define utime(pathname, times) 0
+#endif
+
+#if defined(WINDOWS)
+# define isatty(fd) 0
+# define open _open
+# define close _close
+# define read _read
+# define strdup _strdup
+# define unlink _unlink
+# define write _write
+#endif
+
+#ifndef LSTAT
+# define lstat stat
+#endif
+
+#if defined(DOS) || defined(WINDOWS)
+# define F_OK 0
+static inline access(const char *pathname, int mode)
+{
+ struct stat st;
+ return lstat(pathname, &st) == 0;
+}
+#endif
+
+#ifdef DEF_ERRNO
+ extern int errno;
+#endif
+
+#include "patchlevel.h"
+
+#undef min
+#define min(a,b) ((a>b) ? b : a)
+
+#ifndef IBUFSIZ
+# define IBUFSIZ BUFSIZ /* Defailt input buffer size */
+#endif
+#ifndef OBUFSIZ
+# define OBUFSIZ BUFSIZ /* Default output buffer size */
+#endif
+
+ /* Defines for third byte of header */
+#define MAGIC_1 (char_type)'\037'/* First byte of compressed file */
+#define MAGIC_2 (char_type)'\235'/* Second byte of compressed file */
+#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
+ /* Masks 0x20 and 0x40 are free. */
+ /* I think 0x20 should mean that there is */
+ /* a fourth header byte (for expansion). */
+#define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
+ /* compression rate is dropping flush tables */
+
+ /* the next two codes should not be changed lightly, as they must not */
+ /* lie within the contiguous general code space. */
+#define FIRST 257 /* first free entry */
+#define CLEAR 256 /* table clear output code */
+
+#define INIT_BITS 9 /* initial number of bits/code */
+
+#ifndef SACREDMEM
+ /*
+ * SACREDMEM is the amount of physical memory saved for others; compress
+ * will hog the rest.
+ */
+# define SACREDMEM 0
+#endif
+
+#ifndef USERMEM
+ /*
+ * Set USERMEM to the maximum amount of physical user memory available
+ * in bytes. USERMEM is used to determine the maximum BITS that can be used
+ * for compression.
+ */
+# define USERMEM 450000 /* default user memory */
+#endif
+
+#ifndef BYTEORDER
+# define BYTEORDER 0000
+#endif
+
+#ifndef NOALLIGN
+# define NOALLIGN 0
+#endif
+
+/*
+ * machine variants which require cc -Dmachine: pdp11, z8000, DOS
+ */
+
+#ifdef interdata /* Perkin-Elmer */
+# define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */
+#endif
+
+#ifdef pdp11 /* PDP11: don't forget to compile with -i */
+# define BITS 12 /* max bits/code for 16-bit machine */
+# define NO_UCHAR /* also if "unsigned char" functions as signed char */
+#endif /* pdp11 */
+
+#ifdef z8000 /* Z8000: */
+# define BITS 12 /* 16-bits processor max 12 bits */
+# undef vax /* weird preprocessor */
+#endif /* z8000 */
+
+#ifdef DOS /* PC/XT/AT (8088) processor */
+# define BITS 16 /* 16-bits processor max 12 bits */
+# if BITS == 16
+# define MAXSEG_64K
+# endif
+# undef BYTEORDER
+# define BYTEORDER 4321
+# undef NOALLIGN
+# define NOALLIGN 1
+#endif /* DOS */
+
+#ifndef O_BINARY
+# define O_BINARY 0 /* System has no binary mode */
+#endif
+
+#ifdef M_XENIX /* Stupid compiler can't handle arrays with */
+# if BITS == 16 /* more than 65535 bytes - so we fake it */
+# define MAXSEG_64K
+# else
+# if BITS > 13 /* Code only handles BITS = 12, 13, or 16 */
+# define BITS 13
+# endif
+# endif
+#endif
+
+#ifndef BITS /* General processor calculate BITS */
+# if USERMEM >= (800000+SACREDMEM)
+# define FAST
+# else
+# if USERMEM >= (433484+SACREDMEM)
+# define BITS 16
+# else
+# if USERMEM >= (229600+SACREDMEM)
+# define BITS 15
+# else
+# if USERMEM >= (127536+SACREDMEM)
+# define BITS 14
+# else
+# if USERMEM >= (73464+SACREDMEM)
+# define BITS 13
+# else
+# define BITS 12
+# endif
+# endif
+# endif
+# endif
+# endif
+#endif /* BITS */
+
+#ifdef FAST
+# define HBITS 17 /* 50% occupancy */
+# define HSIZE (1<<HBITS)
+# define HMASK (HSIZE-1)
+# define HPRIME 9941
+# define BITS 16
+# undef MAXSEG_64K
+#else
+# if BITS == 16
+# define HSIZE 69001 /* 95% occupancy */
+# endif
+# if BITS == 15
+# define HSIZE 35023 /* 94% occupancy */
+# endif
+# if BITS == 14
+# define HSIZE 18013 /* 91% occupancy */
+# endif
+# if BITS == 13
+# define HSIZE 9001 /* 91% occupancy */
+# endif
+# if BITS <= 12
+# define HSIZE 5003 /* 80% occupancy */
+# endif
+#endif
+
+#define CHECK_GAP 10000
+
+typedef long int code_int;
+
+#ifdef SIGNED_COMPARE_SLOW
+ typedef unsigned long int count_int;
+ typedef unsigned short int count_short;
+ typedef unsigned long int cmp_code_int; /* Cast to make compare faster */
+#else
+ typedef long int count_int;
+ typedef long int cmp_code_int;
+#endif
+
+typedef unsigned char char_type;
+
+#define ARGVAL() (*++(*argv) || (--argc && *++argv))
+
+#define MAXCODE(n) (1L << (n))
+
+#ifndef REGISTERS
+# define REGISTERS 2
+#endif
+#define REG1
+#define REG2
+#define REG3
+#define REG4
+#define REG5
+#define REG6
+#define REG7
+#define REG8
+#define REG9
+#define REG10
+#define REG11
+#define REG12
+#define REG13
+#define REG14
+#define REG15
+#define REG16
+#if REGISTERS >= 1
+# undef REG1
+# define REG1 register
+#endif
+#if REGISTERS >= 2
+# undef REG2
+# define REG2 register
+#endif
+#if REGISTERS >= 3
+# undef REG3
+# define REG3 register
+#endif
+#if REGISTERS >= 4
+# undef REG4
+# define REG4 register
+#endif
+#if REGISTERS >= 5
+# undef REG5
+# define REG5 register
+#endif
+#if REGISTERS >= 6
+# undef REG6
+# define REG6 register
+#endif
+#if REGISTERS >= 7
+# undef REG7
+# define REG7 register
+#endif
+#if REGISTERS >= 8
+# undef REG8
+# define REG8 register
+#endif
+#if REGISTERS >= 9
+# undef REG9
+# define REG9 register
+#endif
+#if REGISTERS >= 10
+# undef REG10
+# define REG10 register
+#endif
+#if REGISTERS >= 11
+# undef REG11
+# define REG11 register
+#endif
+#if REGISTERS >= 12
+# undef REG12
+# define REG12 register
+#endif
+#if REGISTERS >= 13
+# undef REG13
+# define REG13 register
+#endif
+#if REGISTERS >= 14
+# undef REG14
+# define REG14 register
+#endif
+#if REGISTERS >= 15
+# undef REG15
+# define REG15 register
+#endif
+#if REGISTERS >= 16
+# undef REG16
+# define REG16 register
+#endif
+
+
+union bytes
+{
+ long word;
+ struct
+ {
+#if BYTEORDER == 4321
+ char_type b1;
+ char_type b2;
+ char_type b3;
+ char_type b4;
+#else
+#if BYTEORDER == 1234
+ char_type b4;
+ char_type b3;
+ char_type b2;
+ char_type b1;
+#else
+# undef BYTEORDER
+ int dummy;
+#endif
+#endif
+ } bytes;
+} ;
+#if BYTEORDER == 4321 && NOALLIGN == 1
+#define output(b,o,c,n) { \
+ *(long *)&((b)[(o)>>3]) |= ((long)(c))<<((o)&0x7);\
+ (o) += (n); \
+ }
+#else
+#ifdef BYTEORDER
+#define output(b,o,c,n) { REG1 char_type *p = &(b)[(o)>>3]; \
+ union bytes i; \
+ i.word = ((long)(c))<<((o)&0x7); \
+ p[0] |= i.bytes.b1; \
+ p[1] |= i.bytes.b2; \
+ p[2] |= i.bytes.b3; \
+ (o) += (n); \
+ }
+#else
+#define output(b,o,c,n) { REG1 char_type *p = &(b)[(o)>>3]; \
+ REG2 long i = ((long)(c))<<((o)&0x7); \
+ p[0] |= (char_type)(i); \
+ p[1] |= (char_type)(i>>8); \
+ p[2] |= (char_type)(i>>16); \
+ (o) += (n); \
+ }
+#endif
+#endif
+#if BYTEORDER == 4321 && NOALLIGN == 1
+#define input(b,o,c,n,m){ \
+ (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
+ (o) += (n); \
+ }
+#else
+#define input(b,o,c,n,m){ REG1 char_type *p = &(b)[(o)>>3]; \
+ (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
+ ((long)(p[2])<<16))>>((o)&0x7))&(m); \
+ (o) += (n); \
+ }
+#endif
+
+char *progname; /* Program name */
+int silent = 0; /* don't tell me about errors */
+int quiet = 1; /* don't tell me about compression */
+int do_decomp = 0; /* Decompress mode */
+int force = 0; /* Force overwrite of files and links */
+int nomagic = 0; /* Use a 3-byte magic number header, */
+ /* unless old file */
+int block_mode = BLOCK_MODE;/* Block compress mode -C compatible with 2.0*/
+int maxbits = BITS; /* user settable max # bits/code */
+int zcat_flg = 0; /* Write output on stdout, suppress messages */
+int recursive = 0; /* compress directories */
+int exit_code = -1; /* Exitcode of compress (-1 no file compressed) */
+
+char_type inbuf[IBUFSIZ+64]; /* Input buffer */
+char_type outbuf[OBUFSIZ+2048];/* Output buffer */
+
+struct stat infstat; /* Input file status */
+char *ifname; /* Input filename */
+int remove_ofname = 0; /* Remove output file on a error */
+char *ofname = NULL; /* Output filename */
+int fgnd_flag = 0; /* Running in background (SIGINT=SIGIGN) */
+
+long bytes_in; /* Total number of byte from input */
+long bytes_out; /* Total number of byte to output */
+
+/*
+ * 8086 & 80286 Has a problem with array bigger than 64K so fake the array
+ * For processors with a limited address space and segments.
+ */
+/*
+ * To save much memory, we overlay the table used by compress() with those
+ * used by decompress(). The tab_prefix table is the same size and type
+ * as the codetab. The tab_suffix table needs 2**BITS characters. We
+ * get this from the beginning of htab. The output stack uses the rest
+ * of htab, and contains characters. There is plenty of room for any
+ * possible stack (stack used to be 8000 characters).
+ */
+#ifdef MAXSEG_64K
+ count_int htab0[8192];
+ count_int htab1[8192];
+ count_int htab2[8192];
+ count_int htab3[8192];
+ count_int htab4[8192];
+ count_int htab5[8192];
+ count_int htab6[8192];
+ count_int htab7[8192];
+ count_int htab8[HSIZE-65536];
+ count_int * htab[9] = {htab0,htab1,htab2,htab3,htab4,htab5,htab6,htab7,htab8};
+
+ unsigned short code0tab[16384];
+ unsigned short code1tab[16384];
+ unsigned short code2tab[16384];
+ unsigned short code3tab[16384];
+ unsigned short code4tab[16384];
+ unsigned short * codetab[5] = {code0tab,code1tab,code2tab,code3tab,code4tab};
+
+# define htabof(i) (htab[(i) >> 13][(i) & 0x1fff])
+# define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff])
+# define tab_prefixof(i) codetabof(i)
+# define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
+# define de_stack ((char_type *)(&htab2[8191]))
+ void clear_htab()
+ {
+ memset(htab0, -1, sizeof(htab0));
+ memset(htab1, -1, sizeof(htab1));
+ memset(htab2, -1, sizeof(htab2));
+ memset(htab3, -1, sizeof(htab3));
+ memset(htab4, -1, sizeof(htab4));
+ memset(htab5, -1, sizeof(htab5));
+ memset(htab6, -1, sizeof(htab6));
+ memset(htab7, -1, sizeof(htab7));
+ memset(htab8, -1, sizeof(htab8));
+ }
+# define clear_tab_prefixof() memset(code0tab, 0, 256);
+#else /* Normal machine */
+ count_int htab[HSIZE];
+ unsigned short codetab[HSIZE];
+
+# define htabof(i) htab[i]
+# define codetabof(i) codetab[i]
+# define tab_prefixof(i) codetabof(i)
+# define tab_suffixof(i) ((char_type *)(htab))[i]
+# define de_stack ((char_type *)&(htab[HSIZE-1]))
+# define clear_htab() memset(htab, -1, sizeof(htab))
+# define clear_tab_prefixof() memset(codetab, 0, 256);
+#endif /* MAXSEG_64K */
+
+#ifdef FAST
+ int primetab[256] = /* Special secudary hash table. */
+ {
+ 1013, -1061, 1109, -1181, 1231, -1291, 1361, -1429,
+ 1481, -1531, 1583, -1627, 1699, -1759, 1831, -1889,
+ 1973, -2017, 2083, -2137, 2213, -2273, 2339, -2383,
+ 2441, -2531, 2593, -2663, 2707, -2753, 2819, -2887,
+ 2957, -3023, 3089, -3181, 3251, -3313, 3361, -3449,
+ 3511, -3557, 3617, -3677, 3739, -3821, 3881, -3931,
+ 4013, -4079, 4139, -4219, 4271, -4349, 4423, -4493,
+ 4561, -4639, 4691, -4783, 4831, -4931, 4973, -5023,
+ 5101, -5179, 5261, -5333, 5413, -5471, 5521, -5591,
+ 5659, -5737, 5807, -5857, 5923, -6029, 6089, -6151,
+ 6221, -6287, 6343, -6397, 6491, -6571, 6659, -6709,
+ 6791, -6857, 6917, -6983, 7043, -7129, 7213, -7297,
+ 7369, -7477, 7529, -7577, 7643, -7703, 7789, -7873,
+ 7933, -8017, 8093, -8171, 8237, -8297, 8387, -8461,
+ 8543, -8627, 8689, -8741, 8819, -8867, 8963, -9029,
+ 9109, -9181, 9241, -9323, 9397, -9439, 9511, -9613,
+ 9677, -9743, 9811, -9871, 9941,-10061,10111,-10177,
+ 10259,-10321,10399,-10477,10567,-10639,10711,-10789,
+ 10867,-10949,11047,-11113,11173,-11261,11329,-11423,
+ 11491,-11587,11681,-11777,11827,-11903,11959,-12041,
+ 12109,-12197,12263,-12343,12413,-12487,12541,-12611,
+ 12671,-12757,12829,-12917,12979,-13043,13127,-13187,
+ 13291,-13367,13451,-13523,13619,-13691,13751,-13829,
+ 13901,-13967,14057,-14153,14249,-14341,14419,-14489,
+ 14557,-14633,14717,-14767,14831,-14897,14983,-15083,
+ 15149,-15233,15289,-15359,15427,-15497,15583,-15649,
+ 15733,-15791,15881,-15937,16057,-16097,16189,-16267,
+ 16363,-16447,16529,-16619,16691,-16763,16879,-16937,
+ 17021,-17093,17183,-17257,17341,-17401,17477,-17551,
+ 17623,-17713,17791,-17891,17957,-18041,18097,-18169,
+ 18233,-18307,18379,-18451,18523,-18637,18731,-18803,
+ 18919,-19031,19121,-19211,19273,-19381,19429,-19477
+ } ;
+#endif
+
+int main ARGS((int,char **));
+void Usage ARGS((int));
+void comprexx ARGS((const char *));
+void compdir ARGS((char *));
+void compress ARGS((int,int));
+void decompress ARGS((int,int));
+void read_error ARGS((void));
+void write_error ARGS((void));
+void abort_compress ARGS((void));
+void prratio ARGS((FILE *,long,long));
+void about ARGS((void));
+
+/*****************************************************************
+ * TAG( main )
+ *
+ * Algorithm from "A Technique for High Performance Data Compression",
+ * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
+ *
+ * Usage: compress [-dfvc] [-b bits] [file ...]
+ * Inputs:
+ * -d: If given, decompression is done instead.
+ *
+ * -c: Write output on stdout, don't remove original.
+ *
+ * -b: Parameter limits the max number of bits/code.
+ *
+ * -f: Forces output file to be generated, even if one already
+ * exists, and even if no space is saved by compressing.
+ * If -f is not used, the user will be prompted if stdin is
+ * a tty, otherwise, the output file will not be overwritten.
+ *
+ * -v: Write compression statistics
+ *
+ * -r: Recursive. If a filename is a directory, descend
+ * into it and compress everything in it.
+ *
+ * file ...:
+ * Files to be compressed. If none specified, stdin is used.
+ * Outputs:
+ * file.Z: Compressed form of file with same mode, owner, and utimes
+ * or stdout (if stdin used as input)
+ *
+ * Assumptions:
+ * When filenames are given, replaces with the compressed version
+ * (.Z suffix) only if the file decreases in size.
+ *
+ * Algorithm:
+ * Modified Lempel-Ziv method (LZW). Basically finds common
+ * substrings and replaces them with a variable size code. This is
+ * deterministic, and can be done on the fly. Thus, the decompression
+ * procedure needs no input table, but tracks the way the table was built.
+ */
+int
+main(argc, argv)
+ REG1 int argc;
+ REG2 char *argv[];
+ {
+ clock_prepare();
+ clock_start();
+ REG3 char **filelist;
+ REG4 char **fileptr;
+ int seen_double_dash = 0;
+
+#ifdef SIGINT
+ if ((fgnd_flag = (signal(SIGINT, SIG_IGN)) != SIG_IGN))
+ signal(SIGINT, (SIG_TYPE)abort_compress);
+#endif
+
+#ifdef SIGTERM
+ signal(SIGTERM, (SIG_TYPE)abort_compress);
+#endif
+#ifdef SIGHUP
+ signal(SIGHUP, (SIG_TYPE)abort_compress);
+#endif
+
+#ifdef COMPATIBLE
+ nomagic = 1; /* Original didn't have a magic number */
+#endif
+
+ filelist = (char **)malloc(argc*sizeof(char *));
+ if (filelist == NULL)
+ {
+ fprintf(stderr, "Cannot allocate memory for file list.\n");
+ exit (1);
+ }
+ fileptr = filelist;
+ *filelist = NULL;
+
+ if((progname = strrchr(argv[0], '/')) != 0)
+ progname++;
+ else
+ progname = argv[0];
+
+ if (strcmp(progname, "uncompress") == 0)
+ do_decomp = 1;
+ else
+ if (strcmp(progname, "zcat") == 0)
+ do_decomp = zcat_flg = 1;
+
+ /* Argument Processing
+ * All flags are optional.
+ * -V => print Version; debug verbose
+ * -d => do_decomp
+ * -v => unquiet
+ * -f => force overwrite of output file
+ * -n => no header: useful to uncompress old files
+ * -b maxbits => maxbits. If -b is specified, then maxbits MUST be given also.
+ * -c => cat all output to stdout
+ * -C => generate output compatible with compress 2.0.
+ * -r => recursively compress directories
+ * if a string is left, must be an input filename.
+ */
+
+ for (argc--, argv++; argc > 0; argc--, argv++)
+ {
+ if (strcmp(*argv, "--") == 0)
+ {
+ seen_double_dash = 1;
+ continue;
+ }
+
+ if (seen_double_dash == 0 && **argv == '-')
+ {/* A flag argument */
+ while (*++(*argv))
+ {/* Process all flags in this arg */
+ switch (**argv)
+ {
+ case 'V':
+ about();
+ break;
+
+ case 's':
+ silent = 1;
+ quiet = 1;
+ break;
+
+ case 'v':
+ silent = 0;
+ quiet = 0;
+ break;
+
+ case 'd':
+ do_decomp = 1;
+ break;
+
+ case 'f':
+ case 'F':
+ force = 1;
+ break;
+
+ case 'n':
+ nomagic = 1;
+ break;
+
+ case 'C':
+ block_mode = 0;
+ break;
+
+ case 'b':
+ if (!ARGVAL())
+ {
+ fprintf(stderr, "Missing maxbits\n");
+ Usage(1);
+ }
+
+ maxbits = atoi(*argv);
+ goto nextarg;
+
+ case 'c':
+ zcat_flg = 1;
+ break;
+
+ case 'q':
+ quiet = 1;
+ break;
+ case 'r':
+ case 'R':
+#ifdef RECURSIVE
+ recursive = 1;
+#else
+ fprintf(stderr, "%s -r not available (due to missing directory functions)\n", *argv);
+#endif
+ break;
+
+ case 'h':
+ Usage(0);
+ break;
+
+ default:
+ fprintf(stderr, "Unknown flag: '%c'; ", **argv);
+ Usage(1);
+ }
+ }
+ }
+ else
+ {
+ *fileptr++ = *argv; /* Build input file list */
+ *fileptr = NULL;
+ }
+
+nextarg: continue;
+ }
+
+ if (maxbits < INIT_BITS) maxbits = INIT_BITS;
+ if (maxbits > BITS) maxbits = BITS;
+
+ if (*filelist != NULL)
+ {
+ for (fileptr = filelist; *fileptr; fileptr++)
+ comprexx(*fileptr);
+ }
+ else
+ {/* Standard input */
+ ifname = "";
+ exit_code = 0;
+ remove_ofname = 0;
+
+ if (do_decomp == 0)
+ {
+ compress(0, 1);
+
+ if (zcat_flg == 0 && !quiet)
+ {
+ fprintf(stderr, "Compression: ");
+ prratio(stderr, bytes_in-bytes_out, bytes_in);
+ fprintf(stderr, "\n");
+ }
+
+ if (bytes_out >= bytes_in && !(force))
+ exit_code = 2;
+ }
+ else
+ decompress(0, 1);
+ }
+ clock_stop();
+ printerr_total_clock();
+
+ exit((exit_code== -1) ? 1:exit_code);
+ }
+
+void
+Usage(int status)
+ {
+ fprintf(status ? stderr : stdout, "\
+Usage: %s [-dfhvcVr] [-b maxbits] [--] [file ...]\n\
+ -d If given, decompression is done instead.\n\
+ -c Write output on stdout, don't remove original.\n\
+ -b Parameter limits the max number of bits/code.\n", progname);
+ fprintf(status ? stderr : stdout, "\
+ -f Forces output file to be generated, even if one already.\n\
+ exists, and even if no space is saved by compressing.\n\
+ If -f is not used, the user will be prompted if stdin is.\n\
+ a tty, otherwise, the output file will not be overwritten.\n\
+ -h This help output.\n\
+ -v Write compression statistics.\n\
+ -V Output version and compile options.\n\
+ -r Recursive. If a filename is a directory, descend\n\
+ into it and compress everything in it.\n");
+
+ exit(status);
+ }
+
+void
+comprexx(fileptr)
+ const char *fileptr;
+ {
+ int fdin = -1;
+ int fdout = -1;
+ int has_z_suffix;
+ char *tempname;
+ unsigned long namesize = strlen(fileptr);
+
+ /* Create a temp buffer to add/remove the .Z suffix. */
+ tempname = malloc(namesize + 3);
+ if (tempname == NULL)
+ {
+ perror("malloc");
+ goto error;
+ }
+
+ strcpy(tempname,fileptr);
+ has_z_suffix = (namesize >= 2 && strcmp(&tempname[namesize - 2], ".Z") == 0);
+ errno = 0;
+
+ if (lstat(tempname,&infstat) == -1)
+ {
+ if (do_decomp)
+ {
+ switch (errno)
+ {
+ case ENOENT: /* file doesn't exist */
+ /*
+ ** if the given name doesn't end with .Z, try appending one
+ ** This is obviously the wrong thing to do if it's a
+ ** directory, but it shouldn't do any harm.
+ */
+ if (!has_z_suffix)
+ {
+ memcpy(&tempname[namesize], ".Z", 3);
+ namesize += 2;
+ has_z_suffix = 1;
+ errno = 0;
+ if (lstat(tempname,&infstat) == -1)
+ {
+ perror(tempname);
+ goto error;
+ }
+
+ if ((infstat.st_mode & S_IFMT) != S_IFREG)
+ {
+ fprintf(stderr, "%s: Not a regular file.\n", tempname);
+ goto error;
+ }
+ }
+ else
+ {
+ perror(tempname);
+ goto error;
+ }
+
+ break;
+
+ default:
+ perror(tempname);
+ goto error;
+ }
+ }
+ else
+ {
+ perror(tempname);
+ goto error;
+ }
+ }
+
+ switch (infstat.st_mode & S_IFMT)
+ {
+ case S_IFDIR: /* directory */
+#ifdef RECURSIVE
+ if (recursive)
+ compdir(tempname);
+ else
+#endif
+ if (!quiet)
+ fprintf(stderr,"%s is a directory -- ignored\n", tempname);
+ break;
+
+ case S_IFREG: /* regular file */
+ if (do_decomp != 0)
+ {/* DECOMPRESSION */
+ if (!zcat_flg)
+ {
+ if (!has_z_suffix)
+ {
+ if (!quiet)
+ fprintf(stderr,"%s - no .Z suffix\n",tempname);
+
+ goto error;
+ }
+ }
+
+ free(ofname);
+ ofname = strdup(tempname);
+ if (ofname == NULL)
+ {
+ perror("strdup");
+ goto error;
+ }
+
+ /* Strip of .Z suffix */
+ if (has_z_suffix)
+ ofname[namesize - 2] = '\0';
+ }
+ else
+ {/* COMPRESSION */
+ if (!zcat_flg)
+ {
+ if (has_z_suffix)
+ {
+ fprintf(stderr, "%s: already has .Z suffix -- no change\n", tempname);
+ free(tempname);
+ return;
+ }
+
+ if (infstat.st_nlink > 1 && (!force))
+ {
+ fprintf(stderr, "%s has %jd other links: unchanged\n",
+ tempname, (intmax_t)(infstat.st_nlink - 1));
+ goto error;
+ }
+ }
+
+ ofname = malloc(namesize + 3);
+ if (ofname == NULL)
+ {
+ perror("malloc");
+ goto error;
+ }
+ memcpy(ofname, tempname, namesize);
+ strcpy(&ofname[namesize], ".Z");
+ }
+
+ if ((fdin = open(ifname = tempname, O_RDONLY|O_BINARY)) == -1)
+ {
+ perror(tempname);
+ goto error;
+ }
+
+ if (zcat_flg == 0)
+ {
+ if (access(ofname, F_OK) == 0)
+ {
+ if (!force)
+ {
+ inbuf[0] = 'n';
+
+ fprintf(stderr, "%s already exists.\n", ofname);
+
+ if (fgnd_flag && isatty(0))
+ {
+ fprintf(stderr, "Do you wish to overwrite %s (y or n)? ", ofname);
+ fflush(stderr);
+
+ if (read(0, inbuf, 1) > 0)
+ {
+ if (inbuf[0] != '\n')
+ {
+ do
+ {
+ if (read(0, inbuf+1, 1) <= 0)
+ {
+ perror("stdin");
+ break;
+ }
+ }
+ while (inbuf[1] != '\n');
+ }
+ }
+ else
+ perror("stdin");
+ }
+
+ if (inbuf[0] != 'y')
+ {
+ fprintf(stderr, "%s not overwritten\n", ofname);
+ goto error;
+ }
+ }
+
+ if (unlink(ofname))
+ {
+ fprintf(stderr, "Can't remove old output file\n");
+ perror(ofname);
+ goto error;
+ }
+ }
+
+ if ((fdout = open(ofname, O_WRONLY|O_CREAT|O_EXCL|O_BINARY,0600)) == -1)
+ {
+ perror(tempname);
+ goto error;
+ }
+
+ if(!quiet)
+ fprintf(stderr, "%s: ", tempname);
+
+ remove_ofname = 1;
+ }
+ else
+ {
+ fdout = 1;
+ remove_ofname = 0;
+ }
+
+ if (do_decomp == 0)
+ compress(fdin, fdout);
+ else
+ decompress(fdin, fdout);
+
+ close(fdin);
+
+ if (fdout != 1 && close(fdout))
+ write_error();
+
+ if ( (bytes_in == 0) && (force == 0 ) )
+ {
+ if (remove_ofname)
+ {
+ if(!quiet)
+ fprintf(stderr, "No compression -- %s unchanged\n", ifname);
+ if (unlink(ofname)) /* Remove input file */
+ {
+ fprintf(stderr, "\nunlink error (ignored) ");
+ perror(ofname);
+ exit_code = 1;
+ }
+
+ remove_ofname = 0;
+ exit_code = 2;
+ }
+ }
+ else
+ if (zcat_flg == 0)
+ {
+ struct utimbuf timep;
+
+ if (!do_decomp && bytes_out >= bytes_in && (!force))
+ {/* No compression: remove file.Z */
+ if(!quiet)
+ fprintf(stderr, "No compression -- %s unchanged\n", ifname);
+
+ if (unlink(ofname))
+ {
+ fprintf(stderr, "unlink error (ignored) ");
+ perror(ofname);
+ }
+
+ remove_ofname = 0;
+ exit_code = 2;
+ }
+ else
+ {/* ***** Successful Compression ***** */
+ if(!quiet)
+ {
+ fprintf(stderr, " -- replaced with %s",ofname);
+
+ if (!do_decomp)
+ {
+ fprintf(stderr, " Compression: ");
+ prratio(stderr, bytes_in-bytes_out, bytes_in);
+ }
+
+ fprintf(stderr, "\n");
+ }
+
+ timep.actime = infstat.st_atime;
+ timep.modtime = infstat.st_mtime;
+
+ if (utime(ofname, &timep))
+ {
+ fprintf(stderr, "\nutime error (ignored) ");
+ perror(ofname);
+ exit_code = 1;
+ }
+
+ if (chmod(ofname, infstat.st_mode & 07777)) /* Copy modes */
+ {
+ fprintf(stderr, "\nchmod error (ignored) ");
+ perror(ofname);
+ exit_code = 1;
+ }
+
+ if (chown(ofname, infstat.st_uid, infstat.st_gid)) /* Copy ownership */
+ {
+ fprintf(stderr, "\nchown error (ignored) ");
+ perror(ofname);
+ exit_code = 1;
+ }
+
+ remove_ofname = 0;
+
+ if (unlink(ifname)) /* Remove input file */
+ {
+ fprintf(stderr, "\nunlink error (ignored) ");
+ perror(ifname);
+ exit_code = 1;
+ }
+ }
+ }
+
+ if (exit_code == -1)
+ exit_code = 0;
+
+ break;
+
+ default:
+ fprintf(stderr,"%s is not a directory or a regular file - ignored\n",
+ tempname);
+ break;
+ }
+
+ free(tempname);
+ if (!remove_ofname)
+ {
+ free(ofname);
+ ofname = NULL;
+ }
+ return;
+
+error:
+ free(ofname);
+ ofname = NULL;
+ free(tempname);
+ exit_code = 1;
+ if (fdin != -1)
+ close(fdin);
+ if (fdout != -1)
+ close(fdout);
+ }
+
+#ifdef RECURSIVE
+void
+compdir(dir)
+ REG3 char *dir;
+ {
+#ifndef DIRENT
+ REG1 struct direct *dp;
+#else
+ REG1 struct dirent *dp;
+#endif
+ REG2 DIR *dirp;
+ char *nptr;
+ char *fptr;
+ unsigned long dir_size = strlen(dir);
+ /* The +256 is a lazy optimization. We'll resize on demand. */
+ unsigned long size = dir_size + 256;
+
+ nptr = malloc(size);
+ if (nptr == NULL)
+ {
+ perror("malloc");
+ exit_code = 1;
+ return;
+ }
+ memcpy(nptr, dir, dir_size);
+ nptr[dir_size] = '/';
+ fptr = &nptr[dir_size + 1];
+
+ dirp = opendir(dir);
+
+ if (dirp == NULL)
+ {
+ free(nptr);
+ printf("%s unreadable\n", dir); /* not stderr! */
+ return ;
+ }
+ /*
+ ** WARNING: the following algorithm will occasionally cause
+ ** compress to produce error warnings of the form "<filename>.Z
+ ** already has .Z suffix - ignored". This occurs when the
+ ** .Z output file is inserted into the directory below
+ ** readdir's current pointer.
+ ** These warnings are harmless but annoying. The alternative
+ ** to allowing this would be to store the entire directory
+ ** list in memory, then compress the entries in the stored
+ ** list. Given the depth-first recursive algorithm used here,
+ ** this could use up a tremendous amount of memory. I don't
+ ** think it's worth it. -- Dave Mack
+ */
+
+ while ((dp = readdir(dirp)) != NULL)
+ {
+ if (dp->d_ino == 0)
+ continue;
+
+ if (strcmp(dp->d_name,".") == 0 || strcmp(dp->d_name,"..") == 0)
+ continue;
+
+ if (size < dir_size + strlen(dp->d_name) + 2)
+ {
+ size = dir_size + strlen(dp->d_name) + 2;
+ nptr = realloc(nptr, size);
+ if (nptr == NULL)
+ {
+ perror("realloc");
+ exit_code = 1;
+ break;
+ }
+ fptr = &nptr[dir_size + 1];
+ }
+
+ strcpy(fptr, dp->d_name);
+ comprexx(nptr);
+ }
+
+ closedir(dirp);
+
+ free(nptr);
+ }
+#endif
+/*
+ * compress fdin to fdout
+ *
+ * Algorithm: use open addressing double hashing (no chaining) on the
+ * prefix code / next character combination. We do a variant of Knuth's
+ * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
+ * secondary probe. Here, the modular division first probe is gives way
+ * to a faster exclusive-or manipulation. Also do block compression with
+ * an adaptive reset, whereby the code table is cleared when the compression
+ * ratio decreases, but after the table fills. The variable-length output
+ * codes are re-sized at this point, and a special CLEAR code is generated
+ * for the decompressor. Late addition: construct the table according to
+ * file size for noticeable speed improvement on small files. Please direct
+ * questions about this implementation to ames!jaw.
+ */
+void
+compress(fdin, fdout)
+ int fdin;
+ int fdout;
+ {
+ REG2 long hp;
+ REG3 int rpos;
+#if REGISTERS >= 5
+ REG5 long fc;
+#endif
+ REG6 int outbits;
+ REG7 int rlop;
+ REG8 int rsize;
+ REG9 int stcode;
+ REG10 code_int free_ent;
+ REG11 int boff;
+ REG12 int n_bits;
+ REG13 int ratio;
+ REG14 long checkpoint;
+ REG15 code_int extcode;
+ union
+ {
+ long code;
+ struct
+ {
+ char_type c;
+ unsigned short ent;
+ } e;
+ } fcode;
+
+ ratio = 0;
+ checkpoint = CHECK_GAP;
+ extcode = MAXCODE(n_bits = INIT_BITS)+1;
+ stcode = 1;
+ free_ent = FIRST;
+
+ memset(outbuf, 0, sizeof(outbuf));
+ bytes_out = 0; bytes_in = 0;
+ outbuf[0] = MAGIC_1;
+ outbuf[1] = MAGIC_2;
+ outbuf[2] = (char)(maxbits | block_mode);
+ boff = outbits = (3<<3);
+ fcode.code = 0;
+
+ clear_htab();
+
+ while ((rsize = read(fdin, inbuf, IBUFSIZ)) > 0)
+ {
+ if (bytes_in == 0)
+ {
+ fcode.e.ent = inbuf[0];
+ rpos = 1;
+ }
+ else
+ rpos = 0;
+
+ rlop = 0;
+
+ do
+ {
+ if (free_ent >= extcode && fcode.e.ent < FIRST)
+ {
+ if (n_bits < maxbits)
+ {
+ boff = outbits = (outbits-1)+((n_bits<<3)-
+ (INT_MOD((outbits-boff-1+(n_bits<<3)), (n_bits<<3))));
+ if (++n_bits < maxbits)
+ extcode = MAXCODE(n_bits)+1;
+ else
+ extcode = MAXCODE(n_bits);
+ }
+ else
+ {
+ extcode = MAXCODE(16)+OBUFSIZ;
+ stcode = 0;
+ }
+ }
+
+ if (!stcode && bytes_in >= checkpoint && fcode.e.ent < FIRST)
+ {
+ REG1 long int rat;
+
+ checkpoint = bytes_in + CHECK_GAP;
+
+ if (bytes_in > 0x007fffff)
+ { /* shift will overflow */
+ rat = (bytes_out+(outbits>>3)) >> 8;
+
+ if (rat == 0) /* Don't divide by zero */
+ rat = 0x7fffffff;
+ else
+ rat = bytes_in / rat;
+ }
+ else
+ rat = (bytes_in << 8) / (bytes_out+(outbits>>3)); /* 8 fractional bits */
+ if (rat >= ratio)
+ ratio = (int)rat;
+ else
+ {
+ ratio = 0;
+ clear_htab();
+ output(outbuf,outbits,CLEAR,n_bits);
+ boff = outbits = (outbits-1)+((n_bits<<3)-
+ (INT_MOD((outbits-boff-1+(n_bits<<3)), (n_bits<<3))));
+ extcode = MAXCODE(n_bits = INIT_BITS)+1;
+ free_ent = FIRST;
+ stcode = 1;
+ }
+ }
+
+ if (outbits >= (OBUFSIZ<<3))
+ {
+ if (write(fdout, outbuf, OBUFSIZ) != OBUFSIZ)
+ write_error();
+
+ outbits -= (OBUFSIZ<<3);
+ boff = -(INT_MOD(((OBUFSIZ<<3)-boff), (n_bits<<3)));
+ bytes_out += OBUFSIZ;
+
+ memcpy(outbuf, outbuf+OBUFSIZ, (outbits>>3)+1);
+ memset(outbuf+(outbits>>3)+1, '\0', OBUFSIZ);
+ }
+
+ {
+ REG1 int i;
+
+ i = rsize-rlop;
+
+ if ((code_int)i > extcode-free_ent) i = (int)(extcode-free_ent);
+ if (i > ((sizeof(outbuf) - 32)*8 - outbits)/n_bits)
+ i = ((sizeof(outbuf) - 32)*8 - outbits)/n_bits;
+
+ if (!stcode && (long)i > checkpoint-bytes_in)
+ i = (int)(checkpoint-bytes_in);
+
+ rlop += i;
+ bytes_in += i;
+ }
+
+ goto next;
+hfound: fcode.e.ent = codetabof(hp);
+next: if (rpos >= rlop)
+ goto endlop;
+next2: fcode.e.c = inbuf[rpos++];
+#ifndef FAST
+ {
+ REG1 code_int i;
+#if REGISTERS >= 5
+ fc = fcode.code;
+#else
+# define fc fcode.code
+#endif
+ hp = (((long)(fcode.e.c)) << (BITS-8)) ^ (long)(fcode.e.ent);
+
+ if ((i = htabof(hp)) == fc)
+ goto hfound;
+
+ if (i != -1)
+ {
+ REG4 long disp;
+
+ disp = (HSIZE - hp)-1; /* secondary hash (after G. Knott) */
+
+ do
+ {
+ if ((hp -= disp) < 0) hp += HSIZE;
+
+ if ((i = htabof(hp)) == fc)
+ goto hfound;
+ }
+ while (i != -1);
+ }
+ }
+#else
+ {
+ REG1 long i;
+ REG4 long p;
+#if REGISTERS >= 5
+ fc = fcode.code;
+#else
+# define fc fcode.code
+#endif
+ hp = ((((long)(fcode.e.c)) << (HBITS-8)) ^ (long)(fcode.e.ent));
+
+ if ((i = htabof(hp)) == fc) goto hfound;
+ if (i == -1) goto out;
+
+ p = primetab[fcode.e.c];
+lookup: hp = (hp+p)&HMASK;
+ if ((i = htabof(hp)) == fc) goto hfound;
+ if (i == -1) goto out;
+ hp = (hp+p)&HMASK;
+ if ((i = htabof(hp)) == fc) goto hfound;
+ if (i == -1) goto out;
+ hp = (hp+p)&HMASK;
+ if ((i = htabof(hp)) == fc) goto hfound;
+ if (i == -1) goto out;
+ goto lookup;
+ }
+out: ;
+#endif
+ output(outbuf,outbits,fcode.e.ent,n_bits);
+
+ {
+#if REGISTERS < 5
+# undef fc
+ REG1 long fc;
+ fc = fcode.code;
+#endif
+ fcode.e.ent = fcode.e.c;
+
+
+ if (stcode)
+ {
+ codetabof(hp) = (unsigned short)free_ent++;
+ htabof(hp) = fc;
+ }
+ }
+
+ goto next;
+
+endlop: if (fcode.e.ent >= FIRST && rpos < rsize)
+ goto next2;
+
+ if (rpos > rlop)
+ {
+ bytes_in += rpos-rlop;
+ rlop = rpos;
+ }
+ }
+ while (rlop < rsize);
+ }
+
+ if (rsize < 0)
+ read_error();
+
+ if (bytes_in > 0)
+ output(outbuf,outbits,fcode.e.ent,n_bits);
+
+ if (write(fdout, outbuf, (outbits+7)>>3) != (outbits+7)>>3)
+ write_error();
+
+ bytes_out += (outbits+7)>>3;
+
+ return;
+ }
+
+/*
+ * Decompress stdin to stdout. This routine adapts to the codes in the
+ * file building the "string" table on-the-fly; requiring no table to
+ * be stored in the compressed file. The tables used herein are shared
+ * with those of the compress() routine. See the definitions above.
+ */
+
+void
+decompress(fdin, fdout)
+ int fdin;
+ int fdout;
+ {
+ REG2 char_type *stackp;
+ REG3 code_int code;
+ REG4 int finchar;
+ REG5 code_int oldcode;
+ REG6 code_int incode;
+ REG7 int inbits;
+ REG8 int posbits;
+ REG9 int outpos;
+ REG10 int insize;
+ REG11 int bitmask;
+ REG12 code_int free_ent;
+ REG13 code_int maxcode;
+ REG14 code_int maxmaxcode;
+ REG15 int n_bits;
+ REG16 int rsize;
+
+ bytes_in = 0;
+ bytes_out = 0;
+ insize = 0;
+
+ while (insize < 3 && (rsize = read(fdin, inbuf+insize, IBUFSIZ)) > 0)
+ insize += rsize;
+
+ if (insize < 3 || inbuf[0] != MAGIC_1 || inbuf[1] != MAGIC_2)
+ {
+ if (rsize < 0)
+ read_error();
+
+ if (insize > 0)
+ {
+ fprintf(stderr, "%s: not in compressed format\n",
+ (ifname[0] != '\0'? ifname : "stdin"));
+ exit_code = 1;
+ }
+
+ return ;
+ }
+
+ maxbits = inbuf[2] & BIT_MASK;
+ block_mode = inbuf[2] & BLOCK_MODE;
+
+ if (maxbits > BITS)
+ {
+ fprintf(stderr,
+ "%s: compressed with %d bits, can only handle %d bits\n",
+ (*ifname != '\0' ? ifname : "stdin"), maxbits, BITS);
+ exit_code = 4;
+ return;
+ }
+
+ maxmaxcode = MAXCODE(maxbits);
+
+ bytes_in = insize;
+ maxcode = MAXCODE(n_bits = INIT_BITS)-1;
+ bitmask = (1<<n_bits)-1;
+ oldcode = -1;
+ finchar = 0;
+ outpos = 0;
+ posbits = 3<<3;
+
+ free_ent = ((block_mode) ? FIRST : 256);
+
+ clear_tab_prefixof(); /* As above, initialize the first
+ 256 entries in the table. */
+
+ for (code = 255 ; code >= 0 ; --code)
+ tab_suffixof(code) = (char_type)code;
+
+ do
+ {
+resetbuf: ;
+ {
+ REG1 int i;
+ int e;
+ int o;
+
+ o = posbits >> 3;
+ e = o <= insize ? insize - o : 0;
+
+ for (i = 0 ; i < e ; ++i)
+ inbuf[i] = inbuf[i+o];
+
+ insize = e;
+ posbits = 0;
+ }
+
+ if (insize < sizeof(inbuf)-IBUFSIZ)
+ {
+ if ((rsize = read(fdin, inbuf+insize, IBUFSIZ)) < 0)
+ read_error();
+
+ insize += rsize;
+ }
+
+ inbits = ((rsize > 0) ? (insize - INT_MOD(insize, n_bits))<<3 :
+ (insize<<3)-(n_bits-1));
+
+ while (inbits > posbits)
+ {
+ if (free_ent > maxcode)
+ {
+ posbits = ((posbits-1) + ((n_bits<<3) -
+ INT_MOD((posbits-1+(n_bits<<3)), (n_bits<<3))));
+
+ ++n_bits;
+ if (n_bits == maxbits)
+ maxcode = maxmaxcode;
+ else
+ maxcode = MAXCODE(n_bits)-1;
+
+ bitmask = (1<<n_bits)-1;
+ goto resetbuf;
+ }
+
+ input(inbuf,posbits,code,n_bits,bitmask);
+
+ if (oldcode == -1)
+ {
+ if (code >= 256) {
+ fprintf(stderr, "oldcode:-1 code:%i\n", (int)(code));
+ fprintf(stderr, "uncompress: corrupt input\n");
+ abort_compress();
+ }
+ outbuf[outpos++] = (char_type)(finchar = (int)(oldcode = code));
+ continue;
+ }
+
+ if (code == CLEAR && block_mode)
+ {
+ clear_tab_prefixof();
+ free_ent = FIRST - 1;
+ posbits = ((posbits-1) + ((n_bits<<3) -
+ INT_MOD((posbits-1+(n_bits<<3)), (n_bits<<3))));
+ maxcode = MAXCODE(n_bits = INIT_BITS)-1;
+ bitmask = (1<<n_bits)-1;
+ goto resetbuf;
+ }
+
+ incode = code;
+ stackp = de_stack;
+
+ if (code >= free_ent) /* Special case for KwKwK string. */
+ {
+ if (code > free_ent)
+ {
+ REG1 char_type *p;
+
+ posbits -= n_bits;
+ p = &inbuf[posbits>>3];
+
+ fprintf(stderr, "insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)\n", insize, posbits,
+ p[-1],p[0],p[1],p[2],p[3], (posbits&07));
+ fprintf(stderr, "uncompress: corrupt input\n");
+ abort_compress();
+ }
+
+ *--stackp = (char_type)finchar;
+ code = oldcode;
+ }
+
+ while ((cmp_code_int)code >= (cmp_code_int)256)
+ { /* Generate output characters in reverse order */
+ *--stackp = tab_suffixof(code);
+ code = tab_prefixof(code);
+ }
+
+ *--stackp = (char_type)(finchar = tab_suffixof(code));
+
+ /* And put them out in forward order */
+
+ {
+ REG1 int i;
+
+ if (outpos+(i = (de_stack-stackp)) >= OBUFSIZ)
+ {
+ do
+ {
+ if (i > OBUFSIZ-outpos) i = OBUFSIZ-outpos;
+
+ if (i > 0)
+ {
+ memcpy(outbuf+outpos, stackp, i);
+ outpos += i;
+ }
+
+ if (outpos >= OBUFSIZ)
+ {
+ if (write(fdout, outbuf, outpos) != outpos)
+ write_error();
+
+ outpos = 0;
+ }
+ stackp+= i;
+ }
+ while ((i = (de_stack-stackp)) > 0);
+ }
+ else
+ {
+ memcpy(outbuf+outpos, stackp, i);
+ outpos += i;
+ }
+ }
+
+ if ((code = free_ent) < maxmaxcode) /* Generate the new entry. */
+ {
+ tab_prefixof(code) = (unsigned short)oldcode;
+ tab_suffixof(code) = (char_type)finchar;
+ free_ent = code+1;
+ }
+
+ oldcode = incode; /* Remember previous code. */
+ }
+
+ bytes_in += rsize;
+ }
+ while (rsize > 0);
+
+ if (outpos > 0 && write(fdout, outbuf, outpos) != outpos)
+ write_error();
+ }
+
+void
+read_error()
+ {
+ fprintf(stderr, "\nread error on");
+ perror((ifname[0] != '\0') ? ifname : "stdin");
+ abort_compress();
+ }
+
+void
+write_error()
+ {
+ fprintf(stderr, "\nwrite error on");
+ perror(ofname ? ofname : "stdout");
+ abort_compress();
+ }
+
+void
+abort_compress()
+ {
+ if (remove_ofname)
+ unlink(ofname);
+
+ exit(1);
+ }
+
+void
+prratio(stream, num, den)
+ FILE *stream;
+ long int num;
+ long int den;
+ {
+#ifdef PRINT_RATIO
+ REG1 int q; /* Doesn't need to be long */
+
+ if (den > 0)
+ {
+ if (num > 214748L)
+ q = (int)(num/(den/10000L)); /* 2147483647/10000 */
+ else
+ q = (int)(10000L*num/den); /* Long calculations, though */
+ }
+ else
+ q = 10000;
+
+ if (q < 0)
+ {
+ putc('-', stream);
+ q = -q;
+ }
+
+ fprintf(stream, "%d.%02d%%", q / 100, q % 100);
+#endif
+ }
+
+void
+about()
+ {
+ printf("Compress version: %s\n", version_id);
+ printf("Compile options:\n ");
+#if BYTEORDER == 4321 && NOALLIGN == 1
+ printf("USE_BYTEORDER, ");
+#endif
+#ifdef FAST
+ printf("FAST, ");
+#endif
+#ifdef vax
+ printf("vax, ");
+#endif
+#ifdef DIRENT
+ printf("DIRENT, ");
+#endif
+#ifdef SYSDIR
+ printf("SYSDIR, ");
+#endif
+#ifdef NO_UCHAR
+ printf("NO_UCHAR, ");
+#endif
+#ifdef SIGNED_COMPARE_SLOW
+ printf("SIGNED_COMPARE_SLOW, ");
+#endif
+#ifdef MAXSEG_64K
+ printf("MAXSEG_64K, ");
+#endif
+#ifdef DOS
+ printf("DOS, ");
+#endif
+#ifdef DEBUG
+ printf("DEBUG, ");
+#endif
+#ifdef LSTAT
+ printf("LSTAT, ");
+#endif
+ printf("\n REGISTERS=%d IBUFSIZ=%d, OBUFSIZ=%d, BITS=%d\n",
+ REGISTERS, IBUFSIZ, OBUFSIZ, BITS);
+
+ printf("\n\
+Author version 4.2.4.x (Maintenance):\n\
+ Mike Frysinger (vapier@gmail.com)\n\
+\n\
+Author version 4.2 (Speed improvement & source cleanup):\n\
+ Peter Jannesen (peter@ncs.nl)\n\
+\n\
+Author version 4.1 (Added recursive directory compress):\n\
+ Dave Mack (csu@alembic.acs.com)\n\
+\n\
+Authors version 4.0 (World release in 1985):\n\
+ Spencer W. Thomas, Jim McKie, Steve Davies,\n\
+ Ken Turkowski, James A. Woods, Joe Orost\n");
+
+ exit(0);
+ }
diff --git a/test/monniaux/ncompress/patchlevel.h b/test/monniaux/ncompress/patchlevel.h
new file mode 100644
index 00000000..bdd998d3
--- /dev/null
+++ b/test/monniaux/ncompress/patchlevel.h
@@ -0,0 +1,2 @@
+static char ident[] = "@(#)(N)compress 4.2.4.6";
+#define version_id (ident+4)
diff --git a/test/monniaux/number_theoretic_transform/Makefile b/test/monniaux/number_theoretic_transform/Makefile
new file mode 100644
index 00000000..bd70b946
--- /dev/null
+++ b/test/monniaux/number_theoretic_transform/Makefile
@@ -0,0 +1,3 @@
+TARGET=ntt
+
+include ../rules.mk
diff --git a/test/monniaux/number_theoretic_transform/ntt.c b/test/monniaux/number_theoretic_transform/ntt.c
new file mode 100644
index 00000000..9c978218
--- /dev/null
+++ b/test/monniaux/number_theoretic_transform/ntt.c
@@ -0,0 +1,163 @@
+/*
+D. Monniaux
+CNRS / VERIMAG
+Demonstration of number theoretic transform (Fast Fourier transform in a finite field) in Z/(2^16+1)Z for buffer of length 2^16.
+
+FFT original code from Rosetta Code.
+*/
+
+#include <assert.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../clock.h"
+
+typedef uint32_t modint;
+typedef int32_t smodint;
+
+static modint invm(modint a0, modint b0)
+{
+ smodint a=a0, b=b0, q;
+ smodint x0 = 0, x1 = 1, t;
+ if (b == 1) return 1;
+ while (a > 1) {
+ q = a / b;
+ t = b; b = a % b; a = t;
+ t = x0; x0 = x1 - q * x0; x1 = t;
+ }
+ if (x1 < 0) x1 += b0;
+ assert((x1 * a0) % b0 == 1);
+ return x1;
+}
+
+static inline modint mulm(modint a, modint b, modint m) {
+ return (a * b) % m;
+}
+
+static inline modint addm(modint a, modint b, modint m) {
+ return (a + b) % m;
+}
+
+static inline modint subm(modint a, modint b, modint m) {
+ return (a + (m - b)) % m;
+}
+
+static inline modint powm_u(modint a, modint exponent, modint m) {
+ modint x = 1;
+ while (exponent > 0) {
+ if (exponent % 2) x = mulm(x, a, m);
+ exponent /= 2;
+ a = mulm(a, a, m);
+ }
+ return x;
+}
+
+static inline modint powm(modint a, smodint exponent, modint m) {
+ if (exponent == 0) return 1;
+ else if (exponent > 0) return powm_u(a, exponent, m);
+ else /* exponent < 0 */ return powm(invm(a, m), -exponent, m);
+}
+
+static void _fft(modint modulus,
+ modint root_of_unit,
+ modint buf[], modint out[],
+ unsigned n, unsigned step)
+{
+ if (step < n) {
+ modint root_of_unit2 = mulm(root_of_unit, root_of_unit, modulus);
+ _fft(modulus, root_of_unit2, out, buf, n, step * 2);
+ _fft(modulus, root_of_unit2, out + step, buf + step, n, step * 2);
+
+ modint exp = 1;
+ for (unsigned i = 0; i < n; i += 2 * step) {
+ modint t = mulm(exp, out[i + step], modulus);
+ buf[i / 2] = addm(out[i], t, modulus);
+ buf[(i + n)/2] = subm(out[i], t, modulus);
+ exp = mulm(exp, root_of_unit, modulus);
+ }
+ }
+}
+
+void fft(modint modulus, modint root_of_unit, modint buf[], unsigned n)
+{
+ modint *out = malloc(sizeof(modint) * n);
+ memcpy(out, buf, sizeof(modint) * n);
+
+ _fft(modulus, root_of_unit, buf, out, n, 1);
+ free(out);
+}
+
+static void negate(modint MODULUS, modint buf[restrict], unsigned n) {
+ for(unsigned i=0; i<n; i++) {
+ if (buf[i]) buf[i] = MODULUS-buf[i];
+ }
+}
+
+static void mulvecm(modint modulus, modint buf[restrict], unsigned n, modint coef) {
+ for(unsigned i=0; i<n; i++) {
+ buf[i] = mulm(buf[i], coef, modulus);
+ }
+}
+
+#define LOG_LENGTH 16
+#define LENGTH (1 << LOG_LENGTH)
+#define MUL_MODULUS LENGTH
+#define MODULUS (MUL_MODULUS + 1)
+
+modint randm(modint modulus) {
+ static modint state = 0x42;
+ state = state * UINT64_C(9223372036854775837) + 0x2017;
+ return state % modulus;
+}
+
+int main() {
+#if 0
+ modint root_of_unit = 1;
+ for(modint i=1; i<MODULUS; i++) {
+ if (powm_u(i, MUL_MODULUS/2, MODULUS) != 1) {
+ root_of_unit = i;
+ break;
+ }
+ }
+#else
+ modint root_of_unit = 3;
+#endif
+ assert(root_of_unit != 1);
+ //printf("root of unit = %" PRIu64 "\n", root_of_unit);
+
+ modint *buf = malloc(LENGTH * sizeof(modint)),
+ *save = malloc(LENGTH * sizeof(modint));
+
+ for(unsigned i=0; i<LENGTH; i++) {
+ buf[i] = randm(MODULUS);
+ }
+
+ memcpy(save, buf, LENGTH * sizeof(modint));
+
+ clock_start();
+ fft(MODULUS, root_of_unit, buf, LENGTH);
+ fft(MODULUS, invm(root_of_unit, MODULUS), buf, LENGTH);
+ clock_stop();
+ print_total_clock();
+
+#if 0
+ /* can be replaced by x -> -x */
+ mulvecm(MODULUS, buf, LENGTH, invm(LENGTH, MODULUS));
+#else
+ negate(MODULUS, buf, LENGTH);
+#endif
+ printf("compare = %d\n", memcmp(buf, save, LENGTH * sizeof(modint)));
+
+ /*
+ printf("buf[0] = %" PRIu64 "\n", buf[0]);
+ printf("buf[1] = %" PRIu64 "\n", buf[1]);
+ printf("buf[2] = %" PRIu64 "\n", buf[2]);
+ printf("buf[3] = %" PRIu64 "\n", buf[3]);
+ printf("buf[4] = %" PRIu64 "\n", buf[4]);
+ */
+
+ free(buf);
+ free(save);
+}
diff --git a/test/monniaux/ocaml/Makefile b/test/monniaux/ocaml/Makefile
new file mode 100644
index 00000000..20f32b65
--- /dev/null
+++ b/test/monniaux/ocaml/Makefile
@@ -0,0 +1,6 @@
+TARGET=ocaml
+ALL_CFLAGS=-Ibyterun -lm
+ALL_CFILES=$(wildcard byterun/*.c)
+EXECUTE_ARGS=examples/quicksort
+
+include ../rules.mk
diff --git a/test/monniaux/ocaml/Makefile.common b/test/monniaux/ocaml/Makefile.common
new file mode 100644
index 00000000..cd9f676b
--- /dev/null
+++ b/test/monniaux/ocaml/Makefile.common
@@ -0,0 +1,30 @@
+#**************************************************************************
+#* *
+#* OCaml *
+#* *
+#* Gabriel Scherer, projet Parsifal, INRIA Saclay *
+#* *
+#* Copyright 2018 Institut National de Recherche en Informatique et *
+#* en Automatique. *
+#* *
+#* All rights reserved. This file is distributed under the terms of *
+#* the GNU Lesser General Public License version 2.1, with the *
+#* special exception on linking described in the file LICENSE. *
+#* *
+#**************************************************************************
+
+# This makefile contains common definitions shared by other Makefiles
+# We assume that config/Makefile has already been included
+
+INSTALL ?= install
+INSTALL_DATA ?= $(INSTALL) -m u=rw,g=rw,o=r
+INSTALL_PROG ?= $(INSTALL) -m u=rwx,g=rwx,o=rx
+
+# note: these are defined by lazy expansions
+# as some parts of the makefiles change BINDIR, etc.
+# and expect INSTALL_BINDIR, etc. to stay in synch
+# (see `shellquote` in tools/Makefile)
+INSTALL_BINDIR = $(DESTDIR)$(BINDIR)
+INSTALL_LIBDIR = $(DESTDIR)$(LIBDIR)
+INSTALL_STUBLIBDIR = $(DESTDIR)$(STUBLIBDIR)
+INSTALL_MANDIR = $(DESTDIR)$(MANDIR)
diff --git a/test/monniaux/ocaml/VERSION b/test/monniaux/ocaml/VERSION
new file mode 100644
index 00000000..0e48c0f1
--- /dev/null
+++ b/test/monniaux/ocaml/VERSION
@@ -0,0 +1,4 @@
+4.07.1
+
+# The version string is the first line of this file.
+# It must be in the format described in stdlib/sys.mli
diff --git a/test/monniaux/ocaml/byterun/.depend b/test/monniaux/ocaml/byterun/.depend
new file mode 100644
index 00000000..c0f81615
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/.depend
@@ -0,0 +1,803 @@
+afl.$(O): afl.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/mlvalues.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+alloc.$(O): alloc.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/stacks.h
+array.$(O): array.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h \
+ caml/spacetime.h caml/io.h caml/stack.h
+backtrace.$(O): backtrace.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/backtrace.h \
+ caml/exec.h caml/backtrace_prim.h caml/fail.h
+backtrace_prim.$(O): backtrace_prim.c caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/misc.h caml/alloc.h caml/custom.h caml/io.h \
+ caml/instruct.h caml/intext.h caml/exec.h caml/fix_code.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/startup.h caml/stacks.h \
+ caml/sys.h caml/backtrace.h caml/fail.h caml/backtrace_prim.h
+bigarray.$(O): bigarray.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/bigarray.h caml/custom.h caml/fail.h \
+ caml/intext.h caml/io.h caml/hash.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/signals.h
+callback.$(O): callback.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/interp.h caml/instruct.h caml/fix_code.h caml/stacks.h
+compact.$(O): compact.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/gc_ctrl.h caml/weak.h caml/compact.h
+compare.$(O): compare.c caml/custom.h caml/mlvalues.h caml/config.h caml/m.h \
+ caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+custom.$(O): custom.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/signals.h
+debugger.$(O): debugger.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/debugger.h caml/osdeps.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/fail.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/stacks.h caml/sys.h
+dynlink.$(O): dynlink.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/dynlink.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/prims.h caml/signals.h
+extern.$(O): extern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/gc.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/reverse.h
+fail.$(O): fail.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/io.h caml/gc.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/printexc.h caml/signals.h caml/stacks.h
+finalise.$(O): finalise.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/compact.h caml/fail.h \
+ caml/finalise.h caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h
+fix_code.$(O): fix_code.c caml/config.h caml/m.h caml/s.h caml/debugger.h \
+ caml/misc.h caml/mlvalues.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+floats.$(O): floats.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h caml/stacks.h
+freelist.$(O): freelist.c caml/config.h caml/m.h caml/s.h caml/freelist.h \
+ caml/misc.h caml/mlvalues.h caml/gc.h caml/gc_ctrl.h caml/memory.h \
+ caml/major_gc.h caml/minor_gc.h caml/address_class.h
+gc_ctrl.$(O): gc_ctrl.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/compact.h \
+ caml/custom.h caml/fail.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/gc_ctrl.h caml/signals.h caml/stacks.h \
+ caml/startup_aux.h
+globroots.$(O): globroots.c caml/memory.h caml/config.h caml/m.h caml/s.h \
+ caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/roots.h caml/globroots.h
+hash.$(O): hash.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/custom.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/hash.h
+instrtrace.$(O): instrtrace.c
+intern.$(O): intern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/custom.h caml/fail.h \
+ caml/gc.h caml/intext.h caml/io.h caml/md5.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+interp.$(O): interp.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/fix_code.h caml/instrtrace.h \
+ caml/instruct.h caml/interp.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/signals.h caml/stacks.h caml/startup_aux.h \
+ caml/jumptbl.h
+ints.$(O): ints.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/intext.h caml/io.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+io.$(O): io.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/signals.h caml/sys.h
+lexing.$(O): lexing.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+main.$(O): main.c caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/sys.h caml/osdeps.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+major_gc.$(O): major_gc.c caml/compact.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/custom.h caml/fail.h caml/finalise.h \
+ caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+md5.$(O): md5.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/md5.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/reverse.h
+memory.$(O): memory.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/fail.h caml/freelist.h caml/gc.h \
+ caml/gc_ctrl.h caml/major_gc.h caml/memory.h caml/minor_gc.h \
+ caml/signals.h
+meta.$(O): meta.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/fix_code.h caml/interp.h \
+ caml/intext.h caml/io.h caml/major_gc.h caml/freelist.h caml/memory.h \
+ caml/gc.h caml/minor_gc.h caml/address_class.h caml/prims.h \
+ caml/stacks.h
+minor_gc.$(O): minor_gc.c caml/custom.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/finalise.h caml/roots.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+misc.$(O): misc.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/memory.h \
+ caml/gc.h caml/mlvalues.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/osdeps.h caml/version.h
+obj.$(O): obj.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/gc.h caml/interp.h caml/major_gc.h \
+ caml/freelist.h caml/memory.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/spacetime.h caml/io.h caml/stack.h
+parsing.$(O): parsing.c caml/config.h caml/m.h caml/s.h caml/mlvalues.h \
+ caml/misc.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/alloc.h
+prims.$(O): prims.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/prims.h
+printexc.$(O): printexc.c caml/backtrace.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/printexc.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+roots.$(O): roots.c caml/finalise.h caml/roots.h caml/misc.h caml/config.h \
+ caml/m.h caml/s.h caml/memory.h caml/gc.h caml/mlvalues.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/globroots.h caml/stacks.h
+signals.$(O): signals.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/roots.h caml/signals.h \
+ caml/signals_machdep.h caml/sys.h
+signals_byt.$(O): signals_byt.c caml/config.h caml/m.h caml/s.h \
+ caml/memory.h caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/signals_machdep.h
+spacetime.$(O): spacetime.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h
+stacks.$(O): stacks.c caml/config.h caml/m.h caml/s.h caml/fail.h \
+ caml/misc.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+startup.$(O): startup.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/backtrace.h caml/exec.h \
+ caml/callback.h caml/custom.h caml/debugger.h caml/dynlink.h \
+ caml/fail.h caml/fix_code.h caml/freelist.h caml/gc_ctrl.h \
+ caml/instrtrace.h caml/interp.h caml/intext.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/minor_gc.h caml/address_class.h \
+ caml/osdeps.h caml/prims.h caml/printexc.h caml/reverse.h \
+ caml/signals.h caml/stacks.h caml/sys.h caml/startup.h \
+ caml/startup_aux.h caml/version.h
+startup_aux.$(O): startup_aux.c caml/backtrace.h caml/mlvalues.h \
+ caml/config.h caml/m.h caml/s.h caml/misc.h caml/exec.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/callback.h caml/dynlink.h caml/osdeps.h \
+ caml/startup_aux.h
+str.$(O): str.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h
+sys.$(O): sys.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/debugger.h caml/fail.h caml/gc_ctrl.h caml/io.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/signals.h caml/stacks.h \
+ caml/sys.h caml/version.h caml/callback.h caml/startup_aux.h
+unix.$(O): unix.c caml/config.h caml/m.h caml/s.h caml/fail.h caml/misc.h \
+ caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/sys.h caml/io.h caml/alloc.h
+weak.$(O): weak.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/weak.h
+afl.d.$(O): afl.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/mlvalues.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+alloc.d.$(O): alloc.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/stacks.h
+array.d.$(O): array.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h \
+ caml/spacetime.h caml/io.h caml/stack.h
+backtrace.d.$(O): backtrace.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/backtrace.h \
+ caml/exec.h caml/backtrace_prim.h caml/fail.h
+backtrace_prim.d.$(O): backtrace_prim.c caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/misc.h caml/alloc.h caml/custom.h caml/io.h \
+ caml/instruct.h caml/intext.h caml/exec.h caml/fix_code.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/startup.h caml/stacks.h \
+ caml/sys.h caml/backtrace.h caml/fail.h caml/backtrace_prim.h
+bigarray.d.$(O): bigarray.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/bigarray.h caml/custom.h caml/fail.h \
+ caml/intext.h caml/io.h caml/hash.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/signals.h
+callback.d.$(O): callback.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/interp.h caml/instruct.h caml/fix_code.h caml/stacks.h
+compact.d.$(O): compact.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/gc_ctrl.h caml/weak.h caml/compact.h
+compare.d.$(O): compare.c caml/custom.h caml/mlvalues.h caml/config.h caml/m.h \
+ caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+custom.d.$(O): custom.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/signals.h
+debugger.d.$(O): debugger.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/debugger.h caml/osdeps.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/fail.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/stacks.h caml/sys.h
+dynlink.d.$(O): dynlink.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/dynlink.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/prims.h caml/signals.h
+extern.d.$(O): extern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/gc.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/reverse.h
+fail.d.$(O): fail.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/io.h caml/gc.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/printexc.h caml/signals.h caml/stacks.h
+finalise.d.$(O): finalise.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/compact.h caml/fail.h \
+ caml/finalise.h caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h
+fix_code.d.$(O): fix_code.c caml/config.h caml/m.h caml/s.h caml/debugger.h \
+ caml/misc.h caml/mlvalues.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+floats.d.$(O): floats.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h caml/stacks.h
+freelist.d.$(O): freelist.c caml/config.h caml/m.h caml/s.h caml/freelist.h \
+ caml/misc.h caml/mlvalues.h caml/gc.h caml/gc_ctrl.h caml/memory.h \
+ caml/major_gc.h caml/minor_gc.h caml/address_class.h
+gc_ctrl.d.$(O): gc_ctrl.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/compact.h \
+ caml/custom.h caml/fail.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/gc_ctrl.h caml/signals.h caml/stacks.h \
+ caml/startup_aux.h
+globroots.d.$(O): globroots.c caml/memory.h caml/config.h caml/m.h caml/s.h \
+ caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/roots.h caml/globroots.h
+hash.d.$(O): hash.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/custom.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/hash.h
+instrtrace.d.$(O): instrtrace.c caml/instrtrace.h caml/mlvalues.h \
+ caml/config.h caml/m.h caml/s.h caml/misc.h caml/instruct.h \
+ caml/opnames.h caml/prims.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/startup_aux.h
+intern.d.$(O): intern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/custom.h caml/fail.h \
+ caml/gc.h caml/intext.h caml/io.h caml/md5.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+interp.d.$(O): interp.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/fix_code.h caml/instrtrace.h \
+ caml/instruct.h caml/interp.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/signals.h caml/stacks.h caml/startup_aux.h
+ints.d.$(O): ints.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/intext.h caml/io.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+io.d.$(O): io.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/signals.h caml/sys.h
+lexing.d.$(O): lexing.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+main.d.$(O): main.c caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/sys.h caml/osdeps.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+major_gc.d.$(O): major_gc.c caml/compact.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/custom.h caml/fail.h caml/finalise.h \
+ caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+md5.d.$(O): md5.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/md5.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/reverse.h
+memory.d.$(O): memory.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/fail.h caml/freelist.h caml/gc.h \
+ caml/gc_ctrl.h caml/major_gc.h caml/memory.h caml/minor_gc.h \
+ caml/signals.h
+meta.d.$(O): meta.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/fix_code.h caml/interp.h \
+ caml/intext.h caml/io.h caml/major_gc.h caml/freelist.h caml/memory.h \
+ caml/gc.h caml/minor_gc.h caml/address_class.h caml/prims.h \
+ caml/stacks.h
+minor_gc.d.$(O): minor_gc.c caml/custom.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/finalise.h caml/roots.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+misc.d.$(O): misc.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/memory.h \
+ caml/gc.h caml/mlvalues.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/osdeps.h caml/version.h
+obj.d.$(O): obj.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/gc.h caml/interp.h caml/major_gc.h \
+ caml/freelist.h caml/memory.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/spacetime.h caml/io.h caml/stack.h
+parsing.d.$(O): parsing.c caml/config.h caml/m.h caml/s.h caml/mlvalues.h \
+ caml/misc.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/alloc.h
+prims.d.$(O): prims.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/prims.h
+printexc.d.$(O): printexc.c caml/backtrace.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/printexc.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+roots.d.$(O): roots.c caml/finalise.h caml/roots.h caml/misc.h caml/config.h \
+ caml/m.h caml/s.h caml/memory.h caml/gc.h caml/mlvalues.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/globroots.h caml/stacks.h
+signals.d.$(O): signals.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/roots.h caml/signals.h \
+ caml/signals_machdep.h caml/sys.h
+signals_byt.d.$(O): signals_byt.c caml/config.h caml/m.h caml/s.h \
+ caml/memory.h caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/signals_machdep.h
+spacetime.d.$(O): spacetime.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h
+stacks.d.$(O): stacks.c caml/config.h caml/m.h caml/s.h caml/fail.h \
+ caml/misc.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+startup.d.$(O): startup.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/backtrace.h caml/exec.h \
+ caml/callback.h caml/custom.h caml/debugger.h caml/dynlink.h \
+ caml/fail.h caml/fix_code.h caml/freelist.h caml/gc_ctrl.h \
+ caml/instrtrace.h caml/interp.h caml/intext.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/minor_gc.h caml/address_class.h \
+ caml/osdeps.h caml/prims.h caml/printexc.h caml/reverse.h \
+ caml/signals.h caml/stacks.h caml/sys.h caml/startup.h \
+ caml/startup_aux.h caml/version.h
+startup_aux.d.$(O): startup_aux.c caml/backtrace.h caml/mlvalues.h \
+ caml/config.h caml/m.h caml/s.h caml/misc.h caml/exec.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/callback.h caml/dynlink.h caml/osdeps.h \
+ caml/startup_aux.h
+str.d.$(O): str.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h
+sys.d.$(O): sys.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/debugger.h caml/fail.h caml/gc_ctrl.h caml/io.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/signals.h caml/stacks.h \
+ caml/sys.h caml/version.h caml/callback.h caml/startup_aux.h
+unix.d.$(O): unix.c caml/config.h caml/m.h caml/s.h caml/fail.h caml/misc.h \
+ caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/sys.h caml/io.h caml/alloc.h
+weak.d.$(O): weak.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/weak.h
+afl.i.$(O): afl.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/mlvalues.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+alloc.i.$(O): alloc.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/stacks.h
+array.i.$(O): array.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h \
+ caml/spacetime.h caml/io.h caml/stack.h
+backtrace.i.$(O): backtrace.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/backtrace.h \
+ caml/exec.h caml/backtrace_prim.h caml/fail.h
+backtrace_prim.i.$(O): backtrace_prim.c caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/misc.h caml/alloc.h caml/custom.h caml/io.h \
+ caml/instruct.h caml/intext.h caml/exec.h caml/fix_code.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/startup.h caml/stacks.h \
+ caml/sys.h caml/backtrace.h caml/fail.h caml/backtrace_prim.h
+bigarray.i.$(O): bigarray.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/bigarray.h caml/custom.h caml/fail.h \
+ caml/intext.h caml/io.h caml/hash.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/signals.h
+callback.i.$(O): callback.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/interp.h caml/instruct.h caml/fix_code.h caml/stacks.h
+compact.i.$(O): compact.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/gc_ctrl.h caml/weak.h caml/compact.h
+compare.i.$(O): compare.c caml/custom.h caml/mlvalues.h caml/config.h caml/m.h \
+ caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+custom.i.$(O): custom.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/signals.h
+debugger.i.$(O): debugger.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/debugger.h caml/osdeps.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/fail.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/stacks.h caml/sys.h
+dynlink.i.$(O): dynlink.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/dynlink.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/prims.h caml/signals.h
+extern.i.$(O): extern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/gc.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/reverse.h
+fail.i.$(O): fail.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/io.h caml/gc.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/printexc.h caml/signals.h caml/stacks.h
+finalise.i.$(O): finalise.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/compact.h caml/fail.h \
+ caml/finalise.h caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h
+fix_code.i.$(O): fix_code.c caml/config.h caml/m.h caml/s.h caml/debugger.h \
+ caml/misc.h caml/mlvalues.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+floats.i.$(O): floats.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h caml/stacks.h
+freelist.i.$(O): freelist.c caml/config.h caml/m.h caml/s.h caml/freelist.h \
+ caml/misc.h caml/mlvalues.h caml/gc.h caml/gc_ctrl.h caml/memory.h \
+ caml/major_gc.h caml/minor_gc.h caml/address_class.h
+gc_ctrl.i.$(O): gc_ctrl.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/compact.h \
+ caml/custom.h caml/fail.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/gc_ctrl.h caml/signals.h caml/stacks.h \
+ caml/startup_aux.h
+globroots.i.$(O): globroots.c caml/memory.h caml/config.h caml/m.h caml/s.h \
+ caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/roots.h caml/globroots.h
+hash.i.$(O): hash.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/custom.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/hash.h
+instrtrace.i.$(O): instrtrace.c
+intern.i.$(O): intern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/custom.h caml/fail.h \
+ caml/gc.h caml/intext.h caml/io.h caml/md5.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+interp.i.$(O): interp.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/fix_code.h caml/instrtrace.h \
+ caml/instruct.h caml/interp.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/signals.h caml/stacks.h caml/startup_aux.h \
+ caml/jumptbl.h
+ints.i.$(O): ints.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/intext.h caml/io.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+io.i.$(O): io.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/signals.h caml/sys.h
+lexing.i.$(O): lexing.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+main.i.$(O): main.c caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/sys.h caml/osdeps.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+major_gc.i.$(O): major_gc.c caml/compact.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/custom.h caml/fail.h caml/finalise.h \
+ caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+md5.i.$(O): md5.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/md5.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/reverse.h
+memory.i.$(O): memory.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/fail.h caml/freelist.h caml/gc.h \
+ caml/gc_ctrl.h caml/major_gc.h caml/memory.h caml/minor_gc.h \
+ caml/signals.h
+meta.i.$(O): meta.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/fix_code.h caml/interp.h \
+ caml/intext.h caml/io.h caml/major_gc.h caml/freelist.h caml/memory.h \
+ caml/gc.h caml/minor_gc.h caml/address_class.h caml/prims.h \
+ caml/stacks.h
+minor_gc.i.$(O): minor_gc.c caml/custom.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/finalise.h caml/roots.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+misc.i.$(O): misc.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/memory.h \
+ caml/gc.h caml/mlvalues.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/osdeps.h caml/version.h
+obj.i.$(O): obj.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/gc.h caml/interp.h caml/major_gc.h \
+ caml/freelist.h caml/memory.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/spacetime.h caml/io.h caml/stack.h
+parsing.i.$(O): parsing.c caml/config.h caml/m.h caml/s.h caml/mlvalues.h \
+ caml/misc.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/alloc.h
+prims.i.$(O): prims.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/prims.h
+printexc.i.$(O): printexc.c caml/backtrace.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/printexc.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+roots.i.$(O): roots.c caml/finalise.h caml/roots.h caml/misc.h caml/config.h \
+ caml/m.h caml/s.h caml/memory.h caml/gc.h caml/mlvalues.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/globroots.h caml/stacks.h
+signals.i.$(O): signals.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/roots.h caml/signals.h \
+ caml/signals_machdep.h caml/sys.h
+signals_byt.i.$(O): signals_byt.c caml/config.h caml/m.h caml/s.h \
+ caml/memory.h caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/signals_machdep.h
+spacetime.i.$(O): spacetime.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h
+stacks.i.$(O): stacks.c caml/config.h caml/m.h caml/s.h caml/fail.h \
+ caml/misc.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+startup.i.$(O): startup.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/backtrace.h caml/exec.h \
+ caml/callback.h caml/custom.h caml/debugger.h caml/dynlink.h \
+ caml/fail.h caml/fix_code.h caml/freelist.h caml/gc_ctrl.h \
+ caml/instrtrace.h caml/interp.h caml/intext.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/minor_gc.h caml/address_class.h \
+ caml/osdeps.h caml/prims.h caml/printexc.h caml/reverse.h \
+ caml/signals.h caml/stacks.h caml/sys.h caml/startup.h \
+ caml/startup_aux.h caml/version.h
+startup_aux.i.$(O): startup_aux.c caml/backtrace.h caml/mlvalues.h \
+ caml/config.h caml/m.h caml/s.h caml/misc.h caml/exec.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/callback.h caml/dynlink.h caml/osdeps.h \
+ caml/startup_aux.h
+str.i.$(O): str.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h
+sys.i.$(O): sys.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/debugger.h caml/fail.h caml/gc_ctrl.h caml/io.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/signals.h caml/stacks.h \
+ caml/sys.h caml/version.h caml/callback.h caml/startup_aux.h
+unix.i.$(O): unix.c caml/config.h caml/m.h caml/s.h caml/fail.h caml/misc.h \
+ caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/sys.h caml/io.h caml/alloc.h
+weak.i.$(O): weak.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/weak.h
+afl.pic.$(O): afl.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/mlvalues.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+alloc.pic.$(O): alloc.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/stacks.h
+array.pic.$(O): array.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h \
+ caml/spacetime.h caml/io.h caml/stack.h
+backtrace.pic.$(O): backtrace.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/backtrace.h \
+ caml/exec.h caml/backtrace_prim.h caml/fail.h
+backtrace_prim.pic.$(O): backtrace_prim.c caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/misc.h caml/alloc.h caml/custom.h caml/io.h \
+ caml/instruct.h caml/intext.h caml/exec.h caml/fix_code.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/startup.h caml/stacks.h \
+ caml/sys.h caml/backtrace.h caml/fail.h caml/backtrace_prim.h
+bigarray.pic.$(O): bigarray.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/bigarray.h caml/custom.h caml/fail.h \
+ caml/intext.h caml/io.h caml/hash.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/signals.h
+callback.pic.$(O): callback.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/interp.h caml/instruct.h caml/fix_code.h caml/stacks.h
+compact.pic.$(O): compact.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/gc_ctrl.h caml/weak.h caml/compact.h
+compare.pic.$(O): compare.c caml/custom.h caml/mlvalues.h caml/config.h caml/m.h \
+ caml/s.h caml/misc.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+custom.pic.$(O): custom.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/signals.h
+debugger.pic.$(O): debugger.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/debugger.h caml/osdeps.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/fail.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/stacks.h caml/sys.h
+dynlink.pic.$(O): dynlink.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/dynlink.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/prims.h caml/signals.h
+extern.pic.$(O): extern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/custom.h caml/fail.h caml/gc.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/reverse.h
+fail.pic.$(O): fail.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/io.h caml/gc.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/printexc.h caml/signals.h caml/stacks.h
+finalise.pic.$(O): finalise.c caml/callback.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/compact.h caml/fail.h \
+ caml/finalise.h caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/signals.h
+fix_code.pic.$(O): fix_code.c caml/config.h caml/m.h caml/s.h caml/debugger.h \
+ caml/misc.h caml/mlvalues.h caml/fix_code.h caml/instruct.h \
+ caml/intext.h caml/io.h caml/md5.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+floats.pic.$(O): floats.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h caml/stacks.h
+freelist.pic.$(O): freelist.c caml/config.h caml/m.h caml/s.h caml/freelist.h \
+ caml/misc.h caml/mlvalues.h caml/gc.h caml/gc_ctrl.h caml/memory.h \
+ caml/major_gc.h caml/minor_gc.h caml/address_class.h
+gc_ctrl.pic.$(O): gc_ctrl.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/compact.h \
+ caml/custom.h caml/fail.h caml/finalise.h caml/roots.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/gc_ctrl.h caml/signals.h caml/stacks.h \
+ caml/startup_aux.h
+globroots.pic.$(O): globroots.c caml/memory.h caml/config.h caml/m.h caml/s.h \
+ caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/roots.h caml/globroots.h
+hash.pic.$(O): hash.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/custom.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/hash.h
+instrtrace.pic.$(O): instrtrace.c
+intern.pic.$(O): intern.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/custom.h caml/fail.h \
+ caml/gc.h caml/intext.h caml/io.h caml/md5.h caml/memory.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/reverse.h
+interp.pic.$(O): interp.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/backtrace.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/fix_code.h caml/instrtrace.h \
+ caml/instruct.h caml/interp.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/signals.h caml/stacks.h caml/startup_aux.h \
+ caml/jumptbl.h
+ints.pic.$(O): ints.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/intext.h caml/io.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h
+io.pic.$(O): io.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/custom.h caml/fail.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/osdeps.h caml/signals.h caml/sys.h
+lexing.pic.$(O): lexing.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+main.pic.$(O): main.c caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/sys.h caml/osdeps.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+major_gc.pic.$(O): major_gc.c caml/compact.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/custom.h caml/fail.h caml/finalise.h \
+ caml/roots.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+md5.pic.$(O): md5.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/md5.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/reverse.h
+memory.pic.$(O): memory.c caml/address_class.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/mlvalues.h caml/fail.h caml/freelist.h caml/gc.h \
+ caml/gc_ctrl.h caml/major_gc.h caml/memory.h caml/minor_gc.h \
+ caml/signals.h
+meta.pic.$(O): meta.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/fix_code.h caml/interp.h \
+ caml/intext.h caml/io.h caml/major_gc.h caml/freelist.h caml/memory.h \
+ caml/gc.h caml/minor_gc.h caml/address_class.h caml/prims.h \
+ caml/stacks.h
+minor_gc.pic.$(O): minor_gc.c caml/custom.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/fail.h caml/finalise.h caml/roots.h \
+ caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/gc_ctrl.h caml/signals.h \
+ caml/weak.h
+misc.pic.$(O): misc.c caml/config.h caml/m.h caml/s.h caml/misc.h caml/memory.h \
+ caml/gc.h caml/mlvalues.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/osdeps.h caml/version.h
+obj.pic.$(O): obj.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/gc.h caml/interp.h caml/major_gc.h \
+ caml/freelist.h caml/memory.h caml/minor_gc.h caml/address_class.h \
+ caml/prims.h caml/spacetime.h caml/io.h caml/stack.h
+parsing.pic.$(O): parsing.c caml/config.h caml/m.h caml/s.h caml/mlvalues.h \
+ caml/misc.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/alloc.h
+prims.pic.$(O): prims.c caml/mlvalues.h caml/config.h caml/m.h caml/s.h \
+ caml/misc.h caml/prims.h
+printexc.pic.$(O): printexc.c caml/backtrace.h caml/mlvalues.h caml/config.h \
+ caml/m.h caml/s.h caml/misc.h caml/exec.h caml/callback.h \
+ caml/debugger.h caml/fail.h caml/printexc.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+roots.pic.$(O): roots.c caml/finalise.h caml/roots.h caml/misc.h caml/config.h \
+ caml/m.h caml/s.h caml/memory.h caml/gc.h caml/mlvalues.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h \
+ caml/globroots.h caml/stacks.h
+signals.pic.$(O): signals.c caml/alloc.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h caml/callback.h caml/fail.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/roots.h caml/signals.h \
+ caml/signals_machdep.h caml/sys.h
+signals_byt.pic.$(O): signals_byt.c caml/config.h caml/m.h caml/s.h \
+ caml/memory.h caml/gc.h caml/mlvalues.h caml/misc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/signals_machdep.h
+spacetime.pic.$(O): spacetime.c caml/fail.h caml/misc.h caml/config.h caml/m.h \
+ caml/s.h caml/mlvalues.h
+stacks.pic.$(O): stacks.c caml/config.h caml/m.h caml/s.h caml/fail.h \
+ caml/misc.h caml/mlvalues.h caml/stacks.h caml/memory.h caml/gc.h \
+ caml/major_gc.h caml/freelist.h caml/minor_gc.h caml/address_class.h
+startup.pic.$(O): startup.c caml/config.h caml/m.h caml/s.h caml/alloc.h \
+ caml/misc.h caml/mlvalues.h caml/backtrace.h caml/exec.h \
+ caml/callback.h caml/custom.h caml/debugger.h caml/dynlink.h \
+ caml/fail.h caml/fix_code.h caml/freelist.h caml/gc_ctrl.h \
+ caml/instrtrace.h caml/interp.h caml/intext.h caml/io.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/minor_gc.h caml/address_class.h \
+ caml/osdeps.h caml/prims.h caml/printexc.h caml/reverse.h \
+ caml/signals.h caml/stacks.h caml/sys.h caml/startup.h \
+ caml/startup_aux.h caml/version.h
+startup_aux.pic.$(O): startup_aux.c caml/backtrace.h caml/mlvalues.h \
+ caml/config.h caml/m.h caml/s.h caml/misc.h caml/exec.h caml/memory.h \
+ caml/gc.h caml/major_gc.h caml/freelist.h caml/minor_gc.h \
+ caml/address_class.h caml/callback.h caml/dynlink.h caml/osdeps.h \
+ caml/startup_aux.h
+str.pic.$(O): str.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h
+sys.pic.$(O): sys.c caml/config.h caml/m.h caml/s.h caml/alloc.h caml/misc.h \
+ caml/mlvalues.h caml/debugger.h caml/fail.h caml/gc_ctrl.h caml/io.h \
+ caml/osdeps.h caml/memory.h caml/gc.h caml/major_gc.h caml/freelist.h \
+ caml/minor_gc.h caml/address_class.h caml/signals.h caml/stacks.h \
+ caml/sys.h caml/version.h caml/callback.h caml/startup_aux.h
+unix.pic.$(O): unix.c caml/config.h caml/m.h caml/s.h caml/fail.h caml/misc.h \
+ caml/mlvalues.h caml/memory.h caml/gc.h caml/major_gc.h \
+ caml/freelist.h caml/minor_gc.h caml/address_class.h caml/osdeps.h \
+ caml/signals.h caml/sys.h caml/io.h caml/alloc.h
+weak.pic.$(O): weak.c caml/alloc.h caml/misc.h caml/config.h caml/m.h caml/s.h \
+ caml/mlvalues.h caml/fail.h caml/major_gc.h caml/freelist.h \
+ caml/memory.h caml/gc.h caml/minor_gc.h caml/address_class.h \
+ caml/weak.h
diff --git a/test/monniaux/ocaml/byterun/Makefile b/test/monniaux/ocaml/byterun/Makefile
new file mode 100644
index 00000000..7b9f71ea
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/Makefile
@@ -0,0 +1,234 @@
+#**************************************************************************
+#* *
+#* OCaml *
+#* *
+#* Xavier Leroy, projet Cristal, INRIA Rocquencourt *
+#* *
+#* Copyright 1999 Institut National de Recherche en Informatique et *
+#* en Automatique. *
+#* *
+#* All rights reserved. This file is distributed under the terms of *
+#* the GNU Lesser General Public License version 2.1, with the *
+#* special exception on linking described in the file LICENSE. *
+#* *
+#**************************************************************************
+
+include ../config/Makefile
+include ../Makefile.common
+
+# The PROGRAMS (resp. LIBRARIES) variable list the files to build and
+# install as programs in $(INSTALL_BINDIR) (resp. libraries in
+# $(INSTALL_LIBDIR))
+
+PROGRAMS = ocamlrun$(EXE)
+LIBRARIES = ld.conf libcamlrun.$(A)
+DYNLIBRARIES=
+
+ifeq "$(RUNTIMED)" "true"
+PROGRAMS += ocamlrund$(EXE)
+LIBRARIES += libcamlrund.$(A)
+endif
+
+ifeq "$(RUNTIMEI)" "true"
+PROGRAMS += ocamlruni$(EXE)
+LIBRARIES += libcamlruni.$(A)
+endif
+
+ifeq "$(UNIX_OR_WIN32)" "unix"
+ifeq "$(SUPPORTS_SHARED_LIBRARIES)" "true"
+LIBRARIES += libcamlrun_pic.$(A)
+DYNLIBRARIES += libcamlrun_shared.$(SO)
+endif
+endif
+
+ifdef BOOTSTRAPPING_FLEXLINK
+CFLAGS += -DBOOTSTRAPPING_FLEXLINK
+endif
+
+# On Windows, OCAML_STDLIB_DIR needs to be defined dynamically
+
+ifeq "$(UNIX_OR_WIN32)" "win32"
+# OCAML_STDLIB_DIR needs to arrive in dynlink.c as a string which both gcc and
+# msvc are willing parse without warning. This means we can't pass UTF-8
+# directly since, as far as I can tell, cl can cope, but the pre-processor
+# can't. So the string needs to be directly translated to L"" form. To do this,
+# we take advantage of the fact that Cygwin uses GNU libiconv which includes a
+# Java pseudo-encoding which translates any UTF-8 sequences to \uXXXX (and,
+# unlike the C99 pseudo-encoding, emits two surrogate values when needed, rather
+# than \UXXXXXXXX). The \u is then translated to \x in order to accommodate
+# pre-Visual Studio 2013 compilers where \x is a non-standard alias for \u.
+OCAML_STDLIB_DIR = $(shell echo $(LIBDIR)| iconv -t JAVA | sed -e 's/\\u/\\x/g')
+CFLAGS += -DOCAML_STDLIB_DIR='L"$(OCAML_STDLIB_DIR)"'
+endif
+
+CFLAGS += $(IFLEXDIR)
+
+ifneq "$(CCOMPTYPE)" "msvc"
+CFLAGS += -g
+endif
+
+DFLAGS=$(CFLAGS) -DDEBUG
+IFLAGS=$(CFLAGS)
+PICFLAGS=$(CFLAGS) $(SHAREDCCCOMPOPTS)
+
+DBGO=d.$(O)
+
+ifeq "$(UNIX_OR_WIN32)" "win32"
+LIBS = $(BYTECCLIBS) $(EXTRALIBS)
+ifdef BOOTSTRAPPING_FLEXLINK
+MAKE_OCAMLRUN=$(MKEXE_BOOT)
+else
+MAKE_OCAMLRUN = $(MKEXE) -o $(1) $(2)
+endif
+else
+LIBS = $(BYTECCLIBS)
+MAKE_OCAMLRUN = $(MKEXE) $(LDFLAGS) -o $(1) $(2)
+endif
+
+PRIMS=\
+ alloc.c array.c compare.c extern.c floats.c gc_ctrl.c hash.c \
+ intern.c interp.c ints.c io.c lexing.c md5.c meta.c obj.c parsing.c \
+ signals.c str.c sys.c callback.c weak.c finalise.c stacks.c \
+ dynlink.c backtrace_prim.c backtrace.c spacetime.c afl.c \
+ bigarray.c
+
+OBJS=$(addsuffix .$(O), \
+ interp misc stacks fix_code startup_aux startup \
+ freelist major_gc minor_gc memory alloc roots globroots \
+ fail signals signals_byt printexc backtrace_prim backtrace \
+ compare ints floats str array io extern intern \
+ hash sys meta parsing gc_ctrl md5 obj \
+ lexing callback debugger weak compact finalise custom \
+ dynlink spacetime afl $(UNIX_OR_WIN32) bigarray main)
+
+DOBJS=$(OBJS:.$(O)=.$(DBGO)) instrtrace.$(DBGO)
+IOBJS=$(OBJS:.$(O)=.i.$(O))
+PICOBJS=$(OBJS:.$(O)=.pic.$(O))
+
+.PHONY: all
+all: $(LIBRARIES) $(DYNLIBRARIES) $(PROGRAMS)
+
+ld.conf: ../config/Makefile
+ echo "$(STUBLIBDIR)" > $@
+ echo "$(LIBDIR)" >> $@
+
+INSTALL_INCDIR=$(INSTALL_LIBDIR)/caml
+
+.PHONY: install
+install:
+ $(INSTALL_PROG) $(PROGRAMS) "$(INSTALL_BINDIR)"
+ $(INSTALL_DATA) $(LIBRARIES) "$(INSTALL_LIBDIR)"
+ if test -n "$(DYNLIBRARIES)"; then \
+ $(INSTALL_PROG) $(DYNLIBRARIES) "$(INSTALL_LIBDIR)"; \
+ fi
+ mkdir -p "$(INSTALL_INCDIR)"
+ $(INSTALL_DATA) caml/*.h "$(INSTALL_INCDIR)"
+
+# If primitives contain duplicated lines (e.g. because the code is defined
+# like
+# #ifdef X
+# CAMLprim value caml_foo() ...
+# #else
+# CAMLprim value caml_foo() ...
+# end), horrible things will happen (duplicated entries in Runtimedef ->
+# double registration in Symtable -> empty entry in the PRIM table ->
+# the bytecode interpreter is confused).
+# We sort the primitive file and remove duplicates to avoid this problem.
+
+# Warning: we use "sort | uniq" instead of "sort -u" because in the MSVC
+# port, the "sort" program in the path is Microsoft's and not cygwin's
+
+# Warning: POSIX sort is locale dependent, that's why we set LC_ALL explicitly.
+# Sort is unstable for "is_directory" and "isatty"
+# see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html:
+# "using sort to process pathnames, it is recommended that LC_ALL .. set to C"
+
+
+primitives : $(PRIMS)
+ sed -n -e "s/CAMLprim value \([a-z0-9_][a-z0-9_]*\).*/\1/p" $(PRIMS) \
+ | LC_ALL=C sort | uniq > primitives
+
+prims.c : primitives
+ (echo '#define CAML_INTERNALS'; \
+ echo '#include "caml/mlvalues.h"'; \
+ echo '#include "caml/prims.h"'; \
+ sed -e 's/.*/extern value &();/' primitives; \
+ echo 'c_primitive caml_builtin_cprim[] = {'; \
+ sed -e 's/.*/ &,/' primitives; \
+ echo ' 0 };'; \
+ echo 'char * caml_names_of_builtin_cprim[] = {'; \
+ sed -e 's/.*/ "&",/' primitives; \
+ echo ' 0 };') > prims.c
+
+caml/opnames.h : caml/instruct.h
+ cat $^ | tr -d '\r' | \
+ sed -e '/\/\*/d' \
+ -e '/^#/d' \
+ -e 's/enum /char * names_of_/' \
+ -e 's/{$$/[] = {/' \
+ -e 's/\([[:upper:]][[:upper:]_0-9]*\)/"\1"/g' > $@
+
+# caml/jumptbl.h is required only if you have GCC 2.0 or later
+caml/jumptbl.h : caml/instruct.h
+ cat $^ | tr -d '\r' | \
+ sed -n -e '/^ /s/ \([A-Z]\)/ \&\&lbl_\1/gp' \
+ -e '/^}/q' > $@
+
+caml/version.h : ../VERSION ../tools/make-version-header.sh
+ ../tools/make-version-header.sh ../VERSION > caml/version.h
+
+.PHONY: clean
+clean:
+ rm -f $(LIBRARIES) $(DYNLIBRARIES) $(PROGRAMS) *.$(O) *.$(A) *.$(SO)
+ rm -f primitives prims.c caml/opnames.h caml/jumptbl.h
+ rm -f caml/version.h
+
+ocamlrun$(EXE): prims.$(O) libcamlrun.$(A)
+ $(call MAKE_OCAMLRUN,$@,$^ $(LIBS))
+
+libcamlrun.$(A): $(OBJS)
+ $(call MKLIB,$@, $^)
+
+ocamlrund$(EXE): prims.$(O) libcamlrund.$(A)
+ $(MKEXE) $(MKEXEDEBUGFLAG) -o $@ $^ $(LIBS)
+
+libcamlrund.$(A): $(DOBJS)
+ $(call MKLIB,$@, $^)
+
+ocamlruni$(EXE): prims.$(O) libcamlruni.$(A)
+ $(MKEXE) -o $@ $^ $(LIBS)
+
+libcamlruni.$(A): $(IOBJS)
+ $(call MKLIB,$@, $^)
+
+libcamlrun_pic.$(A): $(PICOBJS)
+ $(call MKLIB,$@, $^)
+
+libcamlrun_shared.$(SO): $(PICOBJS)
+ $(MKDLL) -o $@ $^ $(BYTECCLIBS)
+
+%.$(O): %.c
+ $(CC) -c $(CFLAGS) $(CPPFLAGS) $<
+
+%.$(DBGO): %.c
+ $(CC) -c $(DFLAGS) $(CPPFLAGS) $(OUTPUTOBJ)$@ $<
+
+%.i.$(O): %.c
+ $(CC) -c $(IFLAGS) $(CPPFLAGS) $(OUTPUTOBJ)$@ $<
+
+%.pic.$(O): %.c
+ $(CC) -c $(PICFLAGS) $(CPPFLAGS) $(OUTPUTOBJ)$@ $<
+
+.PHONY: depend
+ifeq "$(TOOLCHAIN)" "msvc"
+depend:
+ $(error Dependencies cannot be regenerated using the MSVC ports)
+else
+depend: prims.c caml/opnames.h caml/jumptbl.h caml/version.h
+ $(CC) -MM $(CFLAGS) $(CPPFLAGS) *.c | sed -e 's/\.o/.$$(O)/'>.$@
+ $(CC) -MM $(DFLAGS) $(CPPFLAGS) *.c | sed -e 's/\.o/.d.$$(O)/'>>.$@
+ $(CC) -MM $(IFLAGS) $(CPPFLAGS) *.c | sed -e 's/\.o/.i.$$(O)/'>>.$@
+ $(CC) -MM $(PICFLAGS) $(CPPFLAGS) *.c | sed -e 's/\.o/.pic.$$(O)/'>>.$@
+endif
+
+include .depend
diff --git a/test/monniaux/ocaml/byterun/afl.c b/test/monniaux/ocaml/byterun/afl.c
new file mode 100644
index 00000000..324a3c34
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/afl.c
@@ -0,0 +1,162 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Stephen Dolan, University of Cambridge */
+/* */
+/* Copyright 2016 Stephen Dolan. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Runtime support for afl-fuzz */
+#include "caml/config.h"
+
+#if !defined(HAS_SYS_SHM_H)
+
+#include "caml/mlvalues.h"
+
+CAMLprim value caml_setup_afl (value unit)
+{
+ return Val_unit;
+}
+
+CAMLprim value caml_reset_afl_instrumentation(value unused)
+{
+ return Val_unit;
+}
+
+#else
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <string.h>
+
+#define CAML_INTERNALS
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/osdeps.h"
+
+static int afl_initialised = 0;
+
+/* afl uses abnormal termination (SIGABRT) to check whether
+ to count a testcase as "crashing" */
+extern int caml_abort_on_uncaught_exn;
+
+/* Values used by the instrumentation logic (see cmmgen.ml) */
+static unsigned char afl_area_initial[1 << 16];
+unsigned char* caml_afl_area_ptr = afl_area_initial;
+uintnat caml_afl_prev_loc;
+
+/* File descriptors used to synchronise with afl-fuzz */
+#define FORKSRV_FD_READ 198
+#define FORKSRV_FD_WRITE 199
+
+static void afl_write(uint32_t msg)
+{
+ if (write(FORKSRV_FD_WRITE, &msg, 4) != 4)
+ caml_fatal_error("writing to afl-fuzz");
+}
+
+static uint32_t afl_read()
+{
+ uint32_t msg;
+ if (read(FORKSRV_FD_READ, &msg, 4) != 4)
+ caml_fatal_error("reading from afl-fuzz");
+ return msg;
+}
+
+CAMLprim value caml_setup_afl(value unit)
+{
+ if (afl_initialised) return Val_unit;
+ afl_initialised = 1;
+
+ char* shm_id_str = caml_secure_getenv("__AFL_SHM_ID");
+ if (shm_id_str == NULL) {
+ /* Not running under afl-fuzz, continue as normal */
+ return Val_unit;
+ }
+
+ /* if afl-fuzz is attached, we want it to know about uncaught exceptions */
+ caml_abort_on_uncaught_exn = 1;
+
+ char* shm_id_end;
+ long int shm_id = strtol(shm_id_str, &shm_id_end, 10);
+ if (!(*shm_id_str != '\0' && *shm_id_end == '\0'))
+ caml_fatal_error("afl-fuzz: bad shm id");
+
+ caml_afl_area_ptr = shmat((int)shm_id, NULL, 0);
+ if (caml_afl_area_ptr == (void*)-1)
+ caml_fatal_error("afl-fuzz: could not attach shm area");
+
+ /* poke the bitmap so that afl-fuzz knows we exist, even if the
+ application has sparse instrumentation */
+ caml_afl_area_ptr[0] = 1;
+
+ /* synchronise with afl-fuzz */
+ uint32_t startup_msg = 0;
+ if (write(FORKSRV_FD_WRITE, &startup_msg, 4) != 4) {
+ /* initial write failed, so assume we're not meant to fork.
+ afl-tmin uses this mode. */
+ return Val_unit;
+ }
+ afl_read();
+
+ while (1) {
+ int child_pid = fork();
+ if (child_pid < 0) caml_fatal_error("afl-fuzz: could not fork");
+ else if (child_pid == 0) {
+ /* Run the program */
+ close(FORKSRV_FD_READ);
+ close(FORKSRV_FD_WRITE);
+ return Val_unit;
+ }
+
+ /* As long as the child keeps raising SIGSTOP, we re-use the same process */
+ while (1) {
+ afl_write((uint32_t)child_pid);
+
+ int status;
+ /* WUNTRACED means wait until termination or SIGSTOP */
+ if (waitpid(child_pid, &status, WUNTRACED) < 0)
+ caml_fatal_error("afl-fuzz: waitpid failed");
+
+ afl_write((uint32_t)status);
+
+ uint32_t was_killed = afl_read();
+ if (WIFSTOPPED(status)) {
+ /* child stopped, waiting for another test case */
+ if (was_killed) {
+ /* we saw the child stop, but since then afl-fuzz killed it.
+ we should wait for it before forking another child */
+ if (waitpid(child_pid, &status, 0) < 0)
+ caml_fatal_error("afl-fuzz: waitpid failed");
+ break;
+ } else {
+ kill(child_pid, SIGCONT);
+ }
+ } else {
+ /* child died */
+ break;
+ }
+ }
+ }
+}
+
+CAMLprim value caml_reset_afl_instrumentation(value full)
+{
+ if (full != Val_int(0)) {
+ memset(caml_afl_area_ptr, 0, sizeof(afl_area_initial));
+ }
+ caml_afl_prev_loc = 0;
+ return Val_unit;
+}
+
+#endif /* HAS_SYS_SHM_H */
diff --git a/test/monniaux/ocaml/byterun/alloc.c b/test/monniaux/ocaml/byterun/alloc.c
new file mode 100644
index 00000000..8924dbc0
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/alloc.c
@@ -0,0 +1,247 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* 1. Allocation functions doing the same work as the macros in the
+ case where [Setup_for_gc] and [Restore_after_gc] are no-ops.
+ 2. Convenience functions related to allocation.
+*/
+
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/custom.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/stacks.h"
+
+#define Setup_for_gc
+#define Restore_after_gc
+
+CAMLexport value caml_alloc (mlsize_t wosize, tag_t tag)
+{
+ value result;
+ mlsize_t i;
+
+ CAMLassert (tag < 256);
+ CAMLassert (tag != Infix_tag);
+ if (wosize <= Max_young_wosize){
+ if (wosize == 0){
+ result = Atom (tag);
+ }else{
+ Alloc_small (result, wosize, tag);
+ if (tag < No_scan_tag){
+ for (i = 0; i < wosize; i++) Field (result, i) = Val_unit;
+ }
+ }
+ }else{
+ result = caml_alloc_shr (wosize, tag);
+ if (tag < No_scan_tag){
+ for (i = 0; i < wosize; i++) Field (result, i) = Val_unit;
+ }
+ result = caml_check_urgent_gc (result);
+ }
+ return result;
+}
+
+CAMLexport value caml_alloc_small (mlsize_t wosize, tag_t tag)
+{
+ value result;
+
+ CAMLassert (wosize > 0);
+ CAMLassert (wosize <= Max_young_wosize);
+ CAMLassert (tag < 256);
+ Alloc_small (result, wosize, tag);
+ return result;
+}
+
+CAMLexport value caml_alloc_small_with_my_or_given_profinfo (mlsize_t wosize,
+ tag_t tag, uintnat profinfo)
+{
+ if (profinfo == 0) {
+ return caml_alloc_small(wosize, tag);
+ }
+ else {
+ value result;
+
+ CAMLassert (wosize > 0);
+ CAMLassert (wosize <= Max_young_wosize);
+ CAMLassert (tag < 256);
+ Alloc_small_with_profinfo (result, wosize, tag, profinfo);
+ return result;
+ }
+}
+
+/* [n] is a number of words (fields) */
+CAMLexport value caml_alloc_tuple(mlsize_t n)
+{
+ return caml_alloc(n, 0);
+}
+
+/* [len] is a number of bytes (chars) */
+CAMLexport value caml_alloc_string (mlsize_t len)
+{
+ value result;
+ mlsize_t offset_index;
+ mlsize_t wosize = (len + sizeof (value)) / sizeof (value);
+
+ if (wosize <= Max_young_wosize) {
+ Alloc_small (result, wosize, String_tag);
+ }else{
+ result = caml_alloc_shr (wosize, String_tag);
+ result = caml_check_urgent_gc (result);
+ }
+ Field (result, wosize - 1) = 0;
+ offset_index = Bsize_wsize (wosize) - 1;
+ Byte (result, offset_index) = offset_index - len;
+ return result;
+}
+
+/* [len] is a number of bytes (chars) */
+CAMLexport value caml_alloc_initialized_string (mlsize_t len, const char *p)
+{
+ value result = caml_alloc_string (len);
+ memcpy((char *)String_val(result), p, len);
+ return result;
+}
+
+/* [len] is a number of words.
+ [mem] and [max] are relative (without unit).
+*/
+CAMLexport value caml_alloc_final (mlsize_t len, final_fun fun,
+ mlsize_t mem, mlsize_t max)
+{
+ return caml_alloc_custom(caml_final_custom_operations(fun),
+ len * sizeof(value), mem, max);
+}
+
+CAMLexport value caml_copy_string(char const *s)
+{
+ int len;
+ value res;
+
+ len = strlen(s);
+ res = caml_alloc_initialized_string(len, s);
+ return res;
+}
+
+CAMLexport value caml_alloc_array(value (*funct)(char const *),
+ char const ** arr)
+{
+ CAMLparam0 ();
+ mlsize_t nbr, n;
+ CAMLlocal2 (v, result);
+
+ nbr = 0;
+ while (arr[nbr] != 0) nbr++;
+ result = caml_alloc (nbr, 0);
+ for (n = 0; n < nbr; n++) {
+ /* The two statements below must be separate because of evaluation
+ order (don't take the address &Field(result, n) before
+ calling funct, which may cause a GC and move result). */
+ v = funct(arr[n]);
+ caml_modify(&Field(result, n), v);
+ }
+ CAMLreturn (result);
+}
+
+/* [len] is a number of floats */
+value caml_alloc_float_array(mlsize_t len)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ mlsize_t wosize = len * Double_wosize;
+ value result;
+ /* For consistency with [caml_make_vect], which can't tell whether it should
+ create a float array or not when the size is zero, the tag is set to
+ zero when the size is zero. */
+ if (wosize <= Max_young_wosize){
+ if (wosize == 0)
+ return Atom(0);
+ else
+ Alloc_small (result, wosize, Double_array_tag);
+ }else {
+ result = caml_alloc_shr (wosize, Double_array_tag);
+ result = caml_check_urgent_gc (result);
+ }
+ return result;
+#else
+ return caml_alloc (len, 0);
+#endif
+}
+
+
+CAMLexport value caml_copy_string_array(char const ** arr)
+{
+ return caml_alloc_array(caml_copy_string, arr);
+}
+
+CAMLexport int caml_convert_flag_list(value list, int *flags)
+{
+ int res;
+ res = 0;
+ while (list != Val_int(0)) {
+ res |= flags[Int_val(Field(list, 0))];
+ list = Field(list, 1);
+ }
+ return res;
+}
+
+/* For compiling let rec over values */
+
+/* [size] is a [value] representing number of words (fields) */
+CAMLprim value caml_alloc_dummy(value size)
+{
+ mlsize_t wosize = Long_val(size);
+ return caml_alloc (wosize, 0);
+}
+
+/* [size] is a [value] representing number of words (fields) */
+CAMLprim value caml_alloc_dummy_function(value size,value arity)
+{
+ /* the arity argument is used by the js_of_ocaml runtime */
+ return caml_alloc_dummy(size);
+}
+
+/* [size] is a [value] representing number of floats. */
+CAMLprim value caml_alloc_dummy_float (value size)
+{
+ mlsize_t wosize = Long_val(size) * Double_wosize;
+ return caml_alloc (wosize, 0);
+}
+
+CAMLprim value caml_update_dummy(value dummy, value newval)
+{
+ mlsize_t size, i;
+ tag_t tag;
+
+ size = Wosize_val(newval);
+ tag = Tag_val (newval);
+ CAMLassert (size == Wosize_val(dummy));
+ CAMLassert (tag < No_scan_tag || tag == Double_array_tag);
+
+ Tag_val(dummy) = tag;
+ if (tag == Double_array_tag){
+ size = Wosize_val (newval) / Double_wosize;
+ for (i = 0; i < size; i++){
+ Store_double_flat_field (dummy, i, Double_flat_field (newval, i));
+ }
+ }else{
+ for (i = 0; i < size; i++){
+ caml_modify (&Field(dummy, i), Field(newval, i));
+ }
+ }
+ return Val_unit;
+}
diff --git a/test/monniaux/ocaml/byterun/array.c b/test/monniaux/ocaml/byterun/array.c
new file mode 100644
index 00000000..5367532b
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/array.c
@@ -0,0 +1,586 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Operations on arrays */
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/signals.h"
+/* Why is caml/spacetime.h included conditionnally sometimes and not here ? */
+#include "caml/spacetime.h"
+
+static const mlsize_t mlsize_t_max = -1;
+
+/* returns number of elements (either fields or floats) */
+/* [ 'a array -> int ] */
+CAMLexport mlsize_t caml_array_length(value array)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(array) == Double_array_tag)
+ return Wosize_val(array) / Double_wosize;
+ else
+#endif
+ return Wosize_val(array);
+}
+
+CAMLexport int caml_is_double_array(value array)
+{
+ return (Tag_val(array) == Double_array_tag);
+}
+
+/* Note: the OCaml types on the following primitives will work both with
+ and without the -no-flat-float-array configure-time option. If you
+ respect them, your C code should work in both configurations.
+*/
+
+/* [ 'a array -> int -> 'a ] where 'a != float */
+CAMLprim value caml_array_get_addr(value array, value index)
+{
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
+ return Field(array, idx);
+}
+
+/* [ float array -> int -> float ] */
+CAMLprim value caml_array_get_float(value array, value index)
+{
+ intnat idx = Long_val(index);
+#ifdef FLAT_FLOAT_ARRAY
+ double d;
+ value res;
+
+ if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
+ caml_array_bound_error();
+ d = Double_flat_field(array, idx);
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small(res, Double_wosize, Double_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ Store_double_val(res, d);
+ return res;
+#else
+ CAMLassert (Tag_val (array) != Double_array_tag);
+ if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
+ return Field(array, idx);
+#endif /* FLAT_FLOAT_ARRAY */
+}
+
+/* [ 'a array -> int -> 'a ] */
+CAMLprim value caml_array_get(value array, value index)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(array) == Double_array_tag)
+ return caml_array_get_float(array, index);
+#else
+ CAMLassert (Tag_val(array) != Double_array_tag);
+#endif
+ return caml_array_get_addr(array, index);
+}
+
+/* [ floatarray -> int -> float ] */
+CAMLprim value caml_floatarray_get(value array, value index)
+{
+ intnat idx = Long_val(index);
+ double d;
+ value res;
+
+ CAMLassert (Tag_val(array) == Double_array_tag);
+ if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
+ caml_array_bound_error();
+ d = Double_flat_field(array, idx);
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small(res, Double_wosize, Double_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ Store_double_val(res, d);
+ return res;
+}
+
+/* [ 'a array -> int -> 'a -> unit ] where 'a != float */
+CAMLprim value caml_array_set_addr(value array, value index, value newval)
+{
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
+ Modify(&Field(array, idx), newval);
+ return Val_unit;
+}
+
+/* [ float array -> int -> float -> unit ] */
+CAMLprim value caml_array_set_float(value array, value index, value newval)
+{
+ intnat idx = Long_val(index);
+#ifdef FLAT_FLOAT_ARRAY
+ double d = Double_val (newval);
+ if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
+ caml_array_bound_error();
+ Store_double_flat_field(array, idx, d);
+#else
+ CAMLassert (Tag_val (array) != Double_array_tag);
+ if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
+ Modify(&Field(array, idx), newval);
+#endif
+ return Val_unit;
+}
+
+/* [ 'a array -> int -> 'a -> unit ] */
+CAMLprim value caml_array_set(value array, value index, value newval)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(array) == Double_array_tag)
+ return caml_array_set_float(array, index, newval);
+#else
+ CAMLassert (Tag_val(array) != Double_array_tag);
+#endif
+ return caml_array_set_addr(array, index, newval);
+}
+
+/* [ floatarray -> int -> float -> unit ] */
+CAMLprim value caml_floatarray_set(value array, value index, value newval)
+{
+ intnat idx = Long_val(index);
+ double d = Double_val (newval);
+ CAMLassert (Tag_val(array) == Double_array_tag);
+ if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
+ caml_array_bound_error();
+ Store_double_flat_field(array, idx, d);
+ return Val_unit;
+}
+
+/* [ float array -> int -> float ] */
+CAMLprim value caml_array_unsafe_get_float(value array, value index)
+{
+ intnat idx = Long_val (index);
+#ifdef FLAT_FLOAT_ARRAY
+ double d;
+ value res;
+
+ d = Double_flat_field(array, idx);
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small(res, Double_wosize, Double_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ Store_double_val(res, d);
+ return res;
+#else /* FLAT_FLOAT_ARRAY */
+ CAMLassert (Tag_val(array) != Double_array_tag);
+ return Field(array, idx);
+#endif /* FLAT_FLOAT_ARRAY */
+}
+
+/* [ 'a array -> int -> 'a ] */
+CAMLprim value caml_array_unsafe_get(value array, value index)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(array) == Double_array_tag)
+ return caml_array_unsafe_get_float(array, index);
+#else
+ CAMLassert (Tag_val(array) != Double_array_tag);
+#endif
+ return Field(array, Long_val(index));
+}
+
+/* [ floatarray -> int -> float ] */
+CAMLprim value caml_floatarray_unsafe_get(value array, value index)
+{
+ intnat idx = Long_val(index);
+ double d;
+ value res;
+
+ CAMLassert (Tag_val(array) == Double_array_tag);
+ d = Double_flat_field(array, idx);
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small(res, Double_wosize, Double_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ Store_double_val(res, d);
+ return res;
+}
+
+/* [ 'a array -> int -> 'a -> unit ] where 'a != float */
+CAMLprim value caml_array_unsafe_set_addr(value array, value index,value newval)
+{
+ intnat idx = Long_val(index);
+ Modify(&Field(array, idx), newval);
+ return Val_unit;
+}
+
+/* [ float array -> int -> float -> unit ] */
+CAMLprim value caml_array_unsafe_set_float(value array,value index,value newval)
+{
+ intnat idx = Long_val(index);
+#ifdef FLAT_FLOAT_ARRAY
+ double d = Double_val (newval);
+ Store_double_flat_field(array, idx, d);
+#else
+ Modify(&Field(array, idx), newval);
+#endif
+ return Val_unit;
+}
+
+/* [ 'a array -> int -> 'a -> unit ] */
+CAMLprim value caml_array_unsafe_set(value array, value index, value newval)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(array) == Double_array_tag)
+ return caml_array_unsafe_set_float(array, index, newval);
+#else
+ CAMLassert (Tag_val(array) != Double_array_tag);
+#endif
+ return caml_array_unsafe_set_addr(array, index, newval);
+}
+
+/* [ floatarray -> int -> float -> unit ] */
+CAMLprim value caml_floatarray_unsafe_set(value array, value index,value newval)
+{
+ intnat idx = Long_val(index);
+ double d = Double_val (newval);
+ Store_double_flat_field(array, idx, d);
+ return Val_unit;
+}
+
+/* [len] is a [value] representing number of floats. */
+/* [ int -> floatarray ] */
+CAMLprim value caml_floatarray_create(value len)
+{
+ mlsize_t wosize = Long_val(len) * Double_wosize;
+ value result;
+ if (wosize <= Max_young_wosize){
+ if (wosize == 0)
+ return Atom(0);
+ else
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small (result, wosize, Double_array_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ }else if (wosize > Max_wosize)
+ caml_invalid_argument("Array.Floatarray.create");
+ else {
+ result = caml_alloc_shr (wosize, Double_array_tag);
+ result = caml_check_urgent_gc (result);
+ }
+ return result;
+}
+
+/* [len] is a [value] representing number of floats */
+/* [ int -> float array ] */
+CAMLprim value caml_make_float_vect(value len)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ return caml_floatarray_create (len);
+#else
+ return caml_alloc (Long_val (len), 0);
+#endif
+}
+
+/* [len] is a [value] representing number of words or floats */
+/* Spacetime profiling assumes that this function is only called from OCaml. */
+CAMLprim value caml_make_vect(value len, value init)
+{
+ CAMLparam2 (len, init);
+ CAMLlocal1 (res);
+ mlsize_t size, i;
+
+ size = Long_val(len);
+ if (size == 0) {
+ res = Atom(0);
+#ifdef FLAT_FLOAT_ARRAY
+ } else if (Is_block(init)
+ && Is_in_value_area(init)
+ && Tag_val(init) == Double_tag) {
+ mlsize_t wsize;
+ double d;
+ d = Double_val(init);
+ wsize = size * Double_wosize;
+ if (wsize > Max_wosize) caml_invalid_argument("Array.make");
+ res = caml_alloc(wsize, Double_array_tag);
+ for (i = 0; i < size; i++) {
+ Store_double_flat_field(res, i, d);
+ }
+#endif
+ } else {
+ if (size <= Max_young_wosize) {
+ uintnat profinfo;
+ Get_my_profinfo_with_cached_backtrace(profinfo, size);
+ res = caml_alloc_small_with_my_or_given_profinfo(size, 0, profinfo);
+ for (i = 0; i < size; i++) Field(res, i) = init;
+ }
+ else if (size > Max_wosize) caml_invalid_argument("Array.make");
+ else if (Is_block(init) && Is_young(init)) {
+ /* We don't want to create so many major-to-minor references,
+ so [init] is moved to the major heap by doing a minor GC. */
+ CAML_INSTR_INT ("force_minor/make_vect@", 1);
+ caml_request_minor_gc ();
+ caml_gc_dispatch ();
+ res = caml_alloc_shr(size, 0);
+ for (i = 0; i < size; i++) Field(res, i) = init;
+ res = caml_check_urgent_gc (res);
+ }
+ else {
+ res = caml_alloc_shr(size, 0);
+ for (i = 0; i < size; i++) caml_initialize(&Field(res, i), init);
+ res = caml_check_urgent_gc (res);
+ }
+ }
+ CAMLreturn (res);
+}
+
+/* This primitive is used internally by the compiler to compile
+ explicit array expressions.
+ For float arrays when FLAT_FLOAT_ARRAY is true, it takes an array of
+ boxed floats and returns the corresponding flat-allocated [float array].
+ In all other cases, it just returns its argument unchanged.
+*/
+CAMLprim value caml_make_array(value init)
+{
+#ifdef FLAT_FLOAT_ARRAY
+ CAMLparam1 (init);
+ mlsize_t wsize, size, i;
+ CAMLlocal2 (v, res);
+
+ size = Wosize_val(init);
+ if (size == 0) {
+ CAMLreturn (init);
+ } else {
+ v = Field(init, 0);
+ if (Is_long(v)
+ || ! Is_in_value_area(v)
+ || Tag_val(v) != Double_tag) {
+ CAMLreturn (init);
+ } else {
+ wsize = size * Double_wosize;
+ if (wsize <= Max_young_wosize) {
+ res = caml_alloc_small(wsize, Double_array_tag);
+ } else {
+ res = caml_alloc_shr(wsize, Double_array_tag);
+ res = caml_check_urgent_gc(res);
+ }
+ for (i = 0; i < size; i++) {
+ double d = Double_val(Field(init, i));
+ Store_double_flat_field(res, i, d);
+ }
+ CAMLreturn (res);
+ }
+ }
+#else
+ return init;
+#endif
+}
+
+/* Blitting */
+
+CAMLprim value caml_array_blit(value a1, value ofs1, value a2, value ofs2,
+ value n)
+{
+ value * src, * dst;
+ intnat count;
+
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(a2) == Double_array_tag) {
+ /* Arrays of floats. The values being copied are floats, not
+ pointer, so we can do a direct copy. memmove takes care of
+ potential overlap between the copied areas. */
+ memmove((double *)a2 + Long_val(ofs2),
+ (double *)a1 + Long_val(ofs1),
+ Long_val(n) * sizeof(double));
+ return Val_unit;
+ }
+#endif
+ CAMLassert (Tag_val(a2) != Double_array_tag);
+ if (Is_young(a2)) {
+ /* Arrays of values, destination is in young generation.
+ Here too we can do a direct copy since this cannot create
+ old-to-young pointers, nor mess up with the incremental major GC.
+ Again, memmove takes care of overlap. */
+ memmove(&Field(a2, Long_val(ofs2)),
+ &Field(a1, Long_val(ofs1)),
+ Long_val(n) * sizeof(value));
+ return Val_unit;
+ }
+ /* Array of values, destination is in old generation.
+ We must use caml_modify. */
+ count = Long_val(n);
+ if (a1 == a2 && Long_val(ofs1) < Long_val(ofs2)) {
+ /* Copy in descending order */
+ for (dst = &Field(a2, Long_val(ofs2) + count - 1),
+ src = &Field(a1, Long_val(ofs1) + count - 1);
+ count > 0;
+ count--, src--, dst--) {
+ caml_modify(dst, *src);
+ }
+ } else {
+ /* Copy in ascending order */
+ for (dst = &Field(a2, Long_val(ofs2)), src = &Field(a1, Long_val(ofs1));
+ count > 0;
+ count--, src++, dst++) {
+ caml_modify(dst, *src);
+ }
+ }
+ /* Many caml_modify in a row can create a lot of old-to-young refs.
+ Give the minor GC a chance to run if it needs to. */
+ caml_check_urgent_gc(Val_unit);
+ return Val_unit;
+}
+
+/* A generic function for extraction and concatenation of sub-arrays */
+
+static value caml_array_gather(intnat num_arrays,
+ value arrays[/*num_arrays*/],
+ intnat offsets[/*num_arrays*/],
+ intnat lengths[/*num_arrays*/])
+{
+ CAMLparamN(arrays, num_arrays);
+ value res; /* no need to register it as a root */
+#ifdef FLAT_FLOAT_ARRAY
+ int isfloat = 0;
+ mlsize_t wsize;
+#endif
+ mlsize_t i, size, count, pos;
+ value * src;
+
+ /* Determine total size and whether result array is an array of floats */
+ size = 0;
+ for (i = 0; i < num_arrays; i++) {
+ if (mlsize_t_max - lengths[i] < size) caml_invalid_argument("Array.concat");
+ size += lengths[i];
+#ifdef FLAT_FLOAT_ARRAY
+ if (Tag_val(arrays[i]) == Double_array_tag) isfloat = 1;
+#endif
+ }
+ if (size == 0) {
+ /* If total size = 0, just return empty array */
+ res = Atom(0);
+ }
+#ifdef FLAT_FLOAT_ARRAY
+ else if (isfloat) {
+ /* This is an array of floats. We can use memcpy directly. */
+ if (size > Max_wosize/Double_wosize) caml_invalid_argument("Array.concat");
+ wsize = size * Double_wosize;
+ res = caml_alloc(wsize, Double_array_tag);
+ for (i = 0, pos = 0; i < num_arrays; i++) {
+ memcpy((double *)res + pos,
+ (double *)arrays[i] + offsets[i],
+ lengths[i] * sizeof(double));
+ pos += lengths[i];
+ }
+ CAMLassert(pos == size);
+ }
+#endif
+ else if (size <= Max_young_wosize) {
+ /* Array of values, small enough to fit in young generation.
+ We can use memcpy directly. */
+ res = caml_alloc_small(size, 0);
+ for (i = 0, pos = 0; i < num_arrays; i++) {
+ memcpy(&Field(res, pos),
+ &Field(arrays[i], offsets[i]),
+ lengths[i] * sizeof(value));
+ pos += lengths[i];
+ }
+ CAMLassert(pos == size);
+ }
+ else if (size > Max_wosize) {
+ /* Array of values, too big. */
+ caml_invalid_argument("Array.concat");
+ } else {
+ /* Array of values, must be allocated in old generation and filled
+ using caml_initialize. */
+ res = caml_alloc_shr(size, 0);
+ for (i = 0, pos = 0; i < num_arrays; i++) {
+ for (src = &Field(arrays[i], offsets[i]), count = lengths[i];
+ count > 0;
+ count--, src++, pos++) {
+ caml_initialize(&Field(res, pos), *src);
+ }
+ }
+ CAMLassert(pos == size);
+
+ /* Many caml_initialize in a row can create a lot of old-to-young
+ refs. Give the minor GC a chance to run if it needs to. */
+ res = caml_check_urgent_gc(res);
+ }
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_array_sub(value a, value ofs, value len)
+{
+ value arrays[1] = { a };
+ intnat offsets[1] = { Long_val(ofs) };
+ intnat lengths[1] = { Long_val(len) };
+ return caml_array_gather(1, arrays, offsets, lengths);
+}
+
+CAMLprim value caml_array_append(value a1, value a2)
+{
+ value arrays[2] = { a1, a2 };
+ intnat offsets[2] = { 0, 0 };
+ intnat lengths[2] = { caml_array_length(a1), caml_array_length(a2) };
+ return caml_array_gather(2, arrays, offsets, lengths);
+}
+
+CAMLprim value caml_array_concat(value al)
+{
+#define STATIC_SIZE 16
+ value static_arrays[STATIC_SIZE], * arrays;
+ intnat static_offsets[STATIC_SIZE], * offsets;
+ intnat static_lengths[STATIC_SIZE], * lengths;
+ intnat n, i;
+ value l, res;
+
+ /* Length of list = number of arrays */
+ for (n = 0, l = al; l != Val_int(0); l = Field(l, 1)) n++;
+ /* Allocate extra storage if too many arrays */
+ if (n <= STATIC_SIZE) {
+ arrays = static_arrays;
+ offsets = static_offsets;
+ lengths = static_lengths;
+ } else {
+ arrays = caml_stat_alloc(n * sizeof(value));
+ offsets = caml_stat_alloc_noexc(n * sizeof(intnat));
+ if (offsets == NULL) {
+ caml_stat_free(arrays);
+ caml_raise_out_of_memory();
+ }
+ lengths = caml_stat_alloc_noexc(n * sizeof(value));
+ if (lengths == NULL) {
+ caml_stat_free(offsets);
+ caml_stat_free(arrays);
+ caml_raise_out_of_memory();
+ }
+ }
+ /* Build the parameters to caml_array_gather */
+ for (i = 0, l = al; l != Val_int(0); l = Field(l, 1), i++) {
+ arrays[i] = Field(l, 0);
+ offsets[i] = 0;
+ lengths[i] = caml_array_length(Field(l, 0));
+ }
+ /* Do the concatenation */
+ res = caml_array_gather(n, arrays, offsets, lengths);
+ /* Free the extra storage if needed */
+ if (n > STATIC_SIZE) {
+ caml_stat_free(arrays);
+ caml_stat_free(offsets);
+ caml_stat_free(lengths);
+ }
+ return res;
+}
diff --git a/test/monniaux/ocaml/byterun/backtrace.c b/test/monniaux/ocaml/byterun/backtrace.c
new file mode 100644
index 00000000..8dfe9b7e
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/backtrace.c
@@ -0,0 +1,349 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Stack backtrace for uncaught exceptions */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "caml/alloc.h"
+#include "caml/memory.h"
+#include "caml/backtrace.h"
+#include "caml/backtrace_prim.h"
+#include "caml/fail.h"
+
+/* The table of debug information fragments */
+struct ext_table caml_debug_info;
+
+CAMLexport int32_t caml_backtrace_active = 0;
+CAMLexport int32_t caml_backtrace_pos = 0;
+CAMLexport backtrace_slot * caml_backtrace_buffer = NULL;
+CAMLexport value caml_backtrace_last_exn = Val_unit;
+
+void caml_init_backtrace(void)
+{
+ caml_register_global_root(&caml_backtrace_last_exn);
+}
+
+/* Start or stop the backtrace machinery */
+CAMLprim value caml_record_backtrace(value vflag)
+{
+ int flag = Int_val(vflag);
+
+ if (flag != caml_backtrace_active) {
+ caml_backtrace_active = flag;
+ caml_backtrace_pos = 0;
+ caml_backtrace_last_exn = Val_unit;
+ /* Note: We do lazy initialization of caml_backtrace_buffer when
+ needed in order to simplify the interface with the thread
+ library (thread creation doesn't need to allocate
+ caml_backtrace_buffer). So we don't have to allocate it here.
+ */
+ }
+ return Val_unit;
+}
+
+/* Return the status of the backtrace machinery */
+CAMLprim value caml_backtrace_status(value vunit)
+{
+ return Val_bool(caml_backtrace_active);
+}
+
+/* Print location information -- same behavior as in Printexc
+
+ note that the test for compiler-inserted raises is slightly redundant:
+ (!li->loc_valid && li->loc_is_raise)
+ caml_debuginfo_location guarantees that when li->loc_valid is
+ 0, then li->loc_is_raise is always 1, so the latter test is
+ useless. We kept it to keep code identical to the byterun/
+ implementation. */
+static void print_location(struct caml_loc_info * li, int index)
+{
+ char * info;
+ char * inlined;
+
+ /* Ignore compiler-inserted raise */
+ if (!li->loc_valid && li->loc_is_raise) return;
+
+ if (li->loc_is_raise) {
+ /* Initial raise if index == 0, re-raise otherwise */
+ if (index == 0)
+ info = "Raised at";
+ else
+ info = "Re-raised at";
+ } else {
+ if (index == 0)
+ info = "Raised by primitive operation at";
+ else
+ info = "Called from";
+ }
+ if (li->loc_is_inlined) {
+ inlined = " (inlined)";
+ } else {
+ inlined = "";
+ }
+ if (! li->loc_valid) {
+ fprintf(stderr, "%s unknown location%s\n", info, inlined);
+ } else {
+ fprintf (stderr, "%s file \"%s\"%s, line %d, characters %d-%d\n",
+ info, li->loc_filename, inlined, li->loc_lnum,
+ li->loc_startchr, li->loc_endchr);
+ }
+}
+
+/* Print a backtrace */
+CAMLexport void caml_print_exception_backtrace(void)
+{
+ int i;
+ struct caml_loc_info li;
+ debuginfo dbg;
+
+ if (!caml_debug_info_available()) {
+ fprintf(stderr, "(Cannot print stack backtrace: "
+ "no debug information available)\n");
+ return;
+ }
+
+ for (i = 0; i < caml_backtrace_pos; i++) {
+ for (dbg = caml_debuginfo_extract(caml_backtrace_buffer[i]);
+ dbg != NULL;
+ dbg = caml_debuginfo_next(dbg))
+ {
+ caml_debuginfo_location(dbg, &li);
+ print_location(&li, i);
+ }
+ }
+}
+
+/* Get a copy of the latest backtrace */
+CAMLprim value caml_get_exception_raw_backtrace(value unit)
+{
+ CAMLparam0();
+ CAMLlocal1(res);
+
+ /* Beware: the allocations below may cause finalizers to be run, and another
+ backtrace---possibly of a different length---to be stashed (for example
+ if the finalizer raises then catches an exception). We choose to ignore
+ any such finalizer backtraces and return the original one. */
+
+ if (!caml_backtrace_active ||
+ caml_backtrace_buffer == NULL ||
+ caml_backtrace_pos == 0) {
+ res = caml_alloc(0, 0);
+ }
+ else {
+ backtrace_slot saved_caml_backtrace_buffer[BACKTRACE_BUFFER_SIZE];
+ int saved_caml_backtrace_pos;
+ intnat i;
+
+ saved_caml_backtrace_pos = caml_backtrace_pos;
+
+ if (saved_caml_backtrace_pos > BACKTRACE_BUFFER_SIZE) {
+ saved_caml_backtrace_pos = BACKTRACE_BUFFER_SIZE;
+ }
+
+ memcpy(saved_caml_backtrace_buffer, caml_backtrace_buffer,
+ saved_caml_backtrace_pos * sizeof(backtrace_slot));
+
+ res = caml_alloc(saved_caml_backtrace_pos, 0);
+ for (i = 0; i < saved_caml_backtrace_pos; i++) {
+ Field(res, i) = Val_backtrace_slot(saved_caml_backtrace_buffer[i]);
+ }
+ }
+
+ CAMLreturn(res);
+}
+
+/* Copy back a backtrace and exception to the global state.
+ This function should be used only with Printexc.raw_backtrace */
+/* noalloc (caml value): so no CAMLparam* CAMLreturn* */
+CAMLprim value caml_restore_raw_backtrace(value exn, value backtrace)
+{
+ intnat i;
+ mlsize_t bt_size;
+
+ caml_backtrace_last_exn = exn;
+
+ bt_size = Wosize_val(backtrace);
+ if(bt_size > BACKTRACE_BUFFER_SIZE){
+ bt_size = BACKTRACE_BUFFER_SIZE;
+ }
+
+ /* We don't allocate if the backtrace is empty (no -g or backtrace
+ not activated) */
+ if(bt_size == 0){
+ caml_backtrace_pos = 0;
+ return Val_unit;
+ }
+
+ /* Allocate if needed and copy the backtrace buffer */
+ if (caml_backtrace_buffer == NULL && caml_alloc_backtrace_buffer() == -1){
+ return Val_unit;
+ }
+
+ caml_backtrace_pos = bt_size;
+ for(i=0; i < caml_backtrace_pos; i++){
+ caml_backtrace_buffer[i] = Backtrace_slot_val(Field(backtrace, i));
+ }
+
+ return Val_unit;
+}
+
+#define Val_debuginfo(bslot) (Val_long((uintnat)(bslot)>>1))
+#define Debuginfo_val(vslot) ((debuginfo)(Long_val(vslot) << 1))
+
+/* Convert the raw backtrace to a data structure usable from OCaml */
+static value caml_convert_debuginfo(debuginfo dbg)
+{
+ CAMLparam0();
+ CAMLlocal2(p, fname);
+ struct caml_loc_info li;
+
+ caml_debuginfo_location(dbg, &li);
+
+ if (li.loc_valid) {
+ fname = caml_copy_string(li.loc_filename);
+ p = caml_alloc_small(6, 0);
+ Field(p, 0) = Val_bool(li.loc_is_raise);
+ Field(p, 1) = fname;
+ Field(p, 2) = Val_int(li.loc_lnum);
+ Field(p, 3) = Val_int(li.loc_startchr);
+ Field(p, 4) = Val_int(li.loc_endchr);
+ Field(p, 5) = Val_bool(li.loc_is_inlined);
+ } else {
+ p = caml_alloc_small(1, 1);
+ Field(p, 0) = Val_bool(li.loc_is_raise);
+ }
+
+ CAMLreturn(p);
+}
+
+CAMLprim value caml_convert_raw_backtrace_slot(value slot)
+{
+ if (!caml_debug_info_available())
+ caml_failwith("No debug information available");
+
+ return (caml_convert_debuginfo(Debuginfo_val(slot)));
+}
+
+/* Convert the raw backtrace to a data structure usable from OCaml */
+CAMLprim value caml_convert_raw_backtrace(value bt)
+{
+ CAMLparam1(bt);
+ CAMLlocal1(array);
+ intnat i, index;
+
+ if (!caml_debug_info_available())
+ caml_failwith("No debug information available");
+
+ for (i = 0, index = 0; i < Wosize_val(bt); ++i)
+ {
+ debuginfo dbg;
+ for (dbg = caml_debuginfo_extract(Backtrace_slot_val(Field(bt, i)));
+ dbg != NULL;
+ dbg = caml_debuginfo_next(dbg))
+ index++;
+ }
+
+ array = caml_alloc(index, 0);
+
+ for (i = 0, index = 0; i < Wosize_val(bt); ++i)
+ {
+ debuginfo dbg;
+ for (dbg = caml_debuginfo_extract(Backtrace_slot_val(Field(bt, i)));
+ dbg != NULL;
+ dbg = caml_debuginfo_next(dbg))
+ {
+ Store_field(array, index, caml_convert_debuginfo(dbg));
+ index++;
+ }
+ }
+
+ CAMLreturn(array);
+}
+
+CAMLprim value caml_raw_backtrace_length(value bt)
+{
+ return Val_int(Wosize_val(bt));
+}
+
+CAMLprim value caml_raw_backtrace_slot(value bt, value index)
+{
+ uintnat i;
+ debuginfo dbg;
+
+ i = Long_val(index);
+ if (i >= Wosize_val(bt))
+ caml_invalid_argument("Printexc.get_raw_backtrace_slot: "
+ "index out of bounds");
+ dbg = caml_debuginfo_extract(Backtrace_slot_val(Field(bt, i)));
+ return Val_debuginfo(dbg);
+}
+
+CAMLprim value caml_raw_backtrace_next_slot(value slot)
+{
+ debuginfo dbg;
+
+ CAMLparam1(slot);
+ CAMLlocal1(v);
+
+ dbg = Debuginfo_val(slot);
+ dbg = caml_debuginfo_next(dbg);
+
+ if (dbg == NULL)
+ v = Val_int(0); /* None */
+ else
+ {
+ v = caml_alloc(1, 0);
+ Field(v, 0) = Val_debuginfo(dbg);
+ }
+
+ CAMLreturn(v);
+}
+
+/* the function below is deprecated: we previously returned directly
+ the OCaml-usable representation, instead of the raw backtrace as an
+ abstract type, but this has a large performance overhead if you
+ store a lot of backtraces and print only some of them.
+
+ It is not used by the Printexc library anymore, or anywhere else in
+ the compiler, but we have kept it in case some user still depends
+ on it as an external. */
+CAMLprim value caml_get_exception_backtrace(value unit)
+{
+ CAMLparam0();
+ CAMLlocal3(arr, res, backtrace);
+ intnat i;
+
+ if (!caml_debug_info_available()) {
+ res = Val_int(0); /* None */
+ } else {
+ backtrace = caml_get_exception_raw_backtrace(Val_unit);
+
+ arr = caml_alloc(Wosize_val(backtrace), 0);
+ for (i = 0; i < Wosize_val(backtrace); i++) {
+ backtrace_slot slot = Backtrace_slot_val(Field(backtrace, i));
+ debuginfo dbg = caml_debuginfo_extract(slot);
+ Store_field(arr, i, caml_convert_debuginfo(dbg));
+ }
+
+ res = caml_alloc_small(1, 0); Field(res, 0) = arr; /* Some */
+ }
+
+ CAMLreturn(res);
+}
diff --git a/test/monniaux/ocaml/byterun/backtrace_prim.c b/test/monniaux/ocaml/byterun/backtrace_prim.c
new file mode 100644
index 00000000..e69b2568
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/backtrace_prim.c
@@ -0,0 +1,457 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Stack backtrace for uncaught exceptions */
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "caml/config.h"
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+
+#include "caml/mlvalues.h"
+#include "caml/alloc.h"
+#include "caml/custom.h"
+#include "caml/io.h"
+#include "caml/instruct.h"
+#include "caml/intext.h"
+#include "caml/exec.h"
+#include "caml/fix_code.h"
+#include "caml/memory.h"
+#include "caml/startup.h"
+#include "caml/stacks.h"
+#include "caml/sys.h"
+#include "caml/backtrace.h"
+#include "caml/fail.h"
+#include "caml/backtrace_prim.h"
+
+/* The table of debug information fragments */
+struct ext_table caml_debug_info;
+
+CAMLexport char_os * caml_cds_file = NULL;
+
+/* Location of fields in the Instruct.debug_event record */
+enum {
+ EV_POS = 0,
+ EV_MODULE = 1,
+ EV_LOC = 2,
+ EV_KIND = 3
+};
+
+/* Location of fields in the Location.t record. */
+enum {
+ LOC_START = 0,
+ LOC_END = 1,
+ LOC_GHOST = 2
+};
+
+/* Location of fields in the Lexing.position record. */
+enum {
+ POS_FNAME = 0,
+ POS_LNUM = 1,
+ POS_BOL = 2,
+ POS_CNUM = 3
+};
+
+/* Runtime representation of the debug information, optimized
+ for quick lookup */
+struct ev_info {
+ code_t ev_pc;
+ char *ev_filename;
+ int ev_lnum;
+ int ev_startchr;
+ int ev_endchr;
+};
+
+struct debug_info {
+ code_t start;
+ code_t end;
+ mlsize_t num_events;
+ struct ev_info *events;
+ int already_read;
+};
+
+static struct debug_info *find_debug_info(code_t pc)
+{
+ int i;
+ for (i = 0; i < caml_debug_info.size; i++) {
+ struct debug_info *di = caml_debug_info.contents[i];
+ if (pc >= di->start && pc < di->end)
+ return di;
+ }
+ return NULL;
+}
+
+static int cmp_ev_info(const void *a, const void *b)
+{
+ code_t pc_a = ((const struct ev_info*)a)->ev_pc;
+ code_t pc_b = ((const struct ev_info*)b)->ev_pc;
+ if (pc_a > pc_b) return 1;
+ if (pc_a < pc_b) return -1;
+ return 0;
+}
+
+static struct ev_info *process_debug_events(code_t code_start, value events_heap,
+ mlsize_t *num_events)
+{
+ CAMLparam1(events_heap);
+ CAMLlocal3(l, ev, ev_start);
+ mlsize_t i, j;
+ struct ev_info *events;
+
+ /* Compute the size of the required event buffer. */
+ *num_events = 0;
+ for (i = 0; i < caml_array_length(events_heap); i++)
+ for (l = Field(events_heap, i); l != Val_int(0); l = Field(l, 1))
+ (*num_events)++;
+
+ if (*num_events == 0)
+ CAMLreturnT(struct ev_info *, NULL);
+
+ events = caml_stat_alloc_noexc(*num_events * sizeof(struct ev_info));
+ if(events == NULL)
+ caml_fatal_error ("caml_add_debug_info: out of memory");
+
+ j = 0;
+ for (i = 0; i < caml_array_length(events_heap); i++) {
+ for (l = Field(events_heap, i); l != Val_int(0); l = Field(l, 1)) {
+ ev = Field(l, 0);
+
+ events[j].ev_pc = (code_t)((char*)code_start
+ + Long_val(Field(ev, EV_POS)));
+
+ ev_start = Field(Field(ev, EV_LOC), LOC_START);
+
+ {
+ uintnat fnsz = caml_string_length(Field(ev_start, POS_FNAME)) + 1;
+ events[j].ev_filename = (char*)caml_stat_alloc_noexc(fnsz);
+ if(events[j].ev_filename == NULL)
+ caml_fatal_error ("caml_add_debug_info: out of memory");
+ memcpy(events[j].ev_filename,
+ String_val(Field(ev_start, POS_FNAME)),
+ fnsz);
+ }
+
+ events[j].ev_lnum = Int_val(Field(ev_start, POS_LNUM));
+ events[j].ev_startchr =
+ Int_val(Field(ev_start, POS_CNUM))
+ - Int_val(Field(ev_start, POS_BOL));
+ events[j].ev_endchr =
+ Int_val(Field(Field(Field(ev, EV_LOC), LOC_END), POS_CNUM))
+ - Int_val(Field(ev_start, POS_BOL));
+
+ j++;
+ }
+ }
+
+ CAMLassert(j == *num_events);
+
+ qsort(events, *num_events, sizeof(struct ev_info), cmp_ev_info);
+
+ CAMLreturnT(struct ev_info *, events);
+}
+
+/* Processes a (Instruct.debug_event list array) into a form suitable
+ for quick lookup and registers it for the (code_start,code_size) pc range. */
+CAMLprim value caml_add_debug_info(code_t code_start, value code_size,
+ value events_heap)
+{
+ CAMLparam1(events_heap);
+ struct debug_info *debug_info;
+
+ /* build the OCaml-side debug_info value */
+ debug_info = caml_stat_alloc(sizeof(struct debug_info));
+
+ debug_info->start = code_start;
+ debug_info->end = (code_t)((char*) code_start + Long_val(code_size));
+ if (events_heap == Val_unit) {
+ debug_info->events = NULL;
+ debug_info->num_events = 0;
+ debug_info->already_read = 0;
+ } else {
+ debug_info->events =
+ process_debug_events(code_start, events_heap, &debug_info->num_events);
+ debug_info->already_read = 1;
+ }
+
+ caml_ext_table_add(&caml_debug_info, debug_info);
+
+ CAMLreturn(Val_unit);
+}
+
+CAMLprim value caml_remove_debug_info(code_t start)
+{
+ CAMLparam0();
+ CAMLlocal2(dis, prev);
+
+ int i;
+ for (i = 0; i < caml_debug_info.size; i++) {
+ struct debug_info *di = caml_debug_info.contents[i];
+ if (di->start == start) {
+ /* note that caml_ext_table_remove calls caml_stat_free on the
+ removed resource, bracketing the caml_stat_alloc call in
+ caml_add_debug_info. */
+ caml_ext_table_remove(&caml_debug_info, di);
+ break;
+ }
+ }
+
+ CAMLreturn(Val_unit);
+}
+
+int caml_alloc_backtrace_buffer(void){
+ CAMLassert(caml_backtrace_pos == 0);
+ caml_backtrace_buffer =
+ caml_stat_alloc_noexc(BACKTRACE_BUFFER_SIZE * sizeof(code_t));
+ if (caml_backtrace_buffer == NULL) return -1;
+ return 0;
+}
+
+/* Store the return addresses contained in the given stack fragment
+ into the backtrace array */
+
+void caml_stash_backtrace(value exn, code_t pc, value * sp, int reraise)
+{
+ if (pc != NULL) pc = pc - 1;
+ if (exn != caml_backtrace_last_exn || !reraise) {
+ caml_backtrace_pos = 0;
+ caml_backtrace_last_exn = exn;
+ }
+
+ if (caml_backtrace_buffer == NULL && caml_alloc_backtrace_buffer() == -1)
+ return;
+
+ if (caml_backtrace_pos >= BACKTRACE_BUFFER_SIZE) return;
+ /* testing the code region is needed: PR#1554 */
+ if (find_debug_info(pc) != NULL)
+ caml_backtrace_buffer[caml_backtrace_pos++] = pc;
+
+ /* Traverse the stack and put all values pointing into bytecode
+ into the backtrace buffer. */
+ for (/*nothing*/; sp < caml_trapsp; sp++) {
+ code_t p = (code_t) *sp;
+ if (caml_backtrace_pos >= BACKTRACE_BUFFER_SIZE) break;
+ if (find_debug_info(p) != NULL)
+ caml_backtrace_buffer[caml_backtrace_pos++] = p;
+ }
+}
+
+/* returns the next frame pointer (or NULL if none is available);
+ updates *sp to point to the following one, and *trsp to the next
+ trap frame, which we will skip when we reach it */
+
+code_t caml_next_frame_pointer(value ** sp, value ** trsp)
+{
+ while (*sp < caml_stack_high) {
+ code_t *p = (code_t*) (*sp)++;
+ if(&Trap_pc(*trsp) == p) {
+ *trsp = Trap_link(*trsp);
+ continue;
+ }
+
+ if (find_debug_info(*p) != NULL)
+ return *p;
+ }
+ return NULL;
+}
+
+/* Stores upto [max_frames_value] frames of the current call stack to
+ return to the user. This is used not in an exception-raising
+ context, but only when the user requests to save the trace
+ (hopefully less often). Instead of using a bounded buffer as
+ [caml_stash_backtrace], we first traverse the stack to compute the
+ right size, then allocate space for the trace. */
+
+CAMLprim value caml_get_current_callstack(value max_frames_value)
+{
+ CAMLparam1(max_frames_value);
+ CAMLlocal1(trace);
+
+ /* we use `intnat` here because, were it only `int`, passing `max_int`
+ from the OCaml side would overflow on 64bits machines. */
+ intnat max_frames = Long_val(max_frames_value);
+ intnat trace_size;
+
+ /* first compute the size of the trace */
+ {
+ value * sp = caml_extern_sp;
+ value * trsp = caml_trapsp;
+
+ for (trace_size = 0; trace_size < max_frames; trace_size++) {
+ code_t p = caml_next_frame_pointer(&sp, &trsp);
+ if (p == NULL) break;
+ }
+ }
+
+ trace = caml_alloc(trace_size, 0);
+
+ /* then collect the trace */
+ {
+ value * sp = caml_extern_sp;
+ value * trsp = caml_trapsp;
+ uintnat trace_pos;
+
+ for (trace_pos = 0; trace_pos < trace_size; trace_pos++) {
+ code_t p = caml_next_frame_pointer(&sp, &trsp);
+ CAMLassert(p != NULL);
+ Field(trace, trace_pos) = Val_backtrace_slot(p);
+ }
+ }
+
+ CAMLreturn(trace);
+}
+
+/* Read the debugging info contained in the current bytecode executable. */
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+static void read_main_debug_info(struct debug_info *di)
+{
+ CAMLparam0();
+ CAMLlocal3(events, evl, l);
+ char_os *exec_name;
+ int fd, num_events, orig, i;
+ struct channel *chan;
+ struct exec_trailer trail;
+
+ CAMLassert(di->already_read == 0);
+ di->already_read = 1;
+
+ if (caml_cds_file != NULL) {
+ exec_name = caml_cds_file;
+ } else {
+ exec_name = caml_exe_name;
+ }
+
+ fd = caml_attempt_open(&exec_name, &trail, 1);
+ if (fd < 0){
+ caml_fatal_error ("executable program file not found");
+ CAMLreturn0;
+ }
+
+ caml_read_section_descriptors(fd, &trail);
+ if (caml_seek_optional_section(fd, &trail, "DBUG") != -1) {
+ chan = caml_open_descriptor_in(fd);
+
+ num_events = caml_getword(chan);
+ events = caml_alloc(num_events, 0);
+
+ for (i = 0; i < num_events; i++) {
+ orig = caml_getword(chan);
+ evl = caml_input_val(chan);
+ caml_input_val(chan); /* Skip the list of absolute directory names */
+ /* Relocate events in event list */
+ for (l = evl; l != Val_int(0); l = Field(l, 1)) {
+ value ev = Field(l, 0);
+ Field(ev, EV_POS) = Val_long(Long_val(Field(ev, EV_POS)) + orig);
+ }
+ /* Record event list */
+ Store_field(events, i, evl);
+ }
+
+ caml_close_channel(chan);
+
+ di->events = process_debug_events(caml_start_code, events, &di->num_events);
+ }
+
+ CAMLreturn0;
+}
+
+CAMLexport void caml_init_debug_info(void)
+{
+ caml_ext_table_init(&caml_debug_info, 1);
+ caml_add_debug_info(caml_start_code, Val_long(caml_code_size), Val_unit);
+}
+
+int caml_debug_info_available(void)
+{
+ return (caml_debug_info.size != 0);
+}
+
+/* Search the event index for the given PC. Return -1 if not found. */
+
+static struct ev_info *event_for_location(code_t pc)
+{
+ uintnat low, high;
+ struct debug_info *di = find_debug_info(pc);
+
+ if (di == NULL)
+ return NULL;
+
+ if (!di->already_read)
+ read_main_debug_info(di);
+
+ if (di->num_events == 0)
+ return NULL;
+
+ low = 0;
+ high = di->num_events;
+ while (low+1 < high) {
+ uintnat m = (low+high)/2;
+ if(pc < di->events[m].ev_pc) high = m;
+ else low = m;
+ }
+ if (di->events[low].ev_pc == pc)
+ return &di->events[low];
+ /* ocamlc sometimes moves an event past a following PUSH instruction;
+ allow mismatch by 1 instruction. */
+ if (di->events[low].ev_pc == pc + 1)
+ return &di->events[low];
+ if (low+1 < di->num_events && di->events[low+1].ev_pc == pc + 1)
+ return &di->events[low+1];
+
+ return NULL;
+}
+
+/* Extract location information for the given PC */
+
+void caml_debuginfo_location(debuginfo dbg,
+ /*out*/ struct caml_loc_info * li)
+{
+ code_t pc = dbg;
+ struct ev_info *event = event_for_location(pc);
+ li->loc_is_raise =
+ caml_is_instruction(*pc, RAISE) ||
+ caml_is_instruction(*pc, RERAISE);
+ if (event == NULL) {
+ li->loc_valid = 0;
+ return;
+ }
+ li->loc_valid = 1;
+ li->loc_is_inlined = 0;
+ li->loc_filename = event->ev_filename;
+ li->loc_lnum = event->ev_lnum;
+ li->loc_startchr = event->ev_startchr;
+ li->loc_endchr = event->ev_endchr;
+}
+
+debuginfo caml_debuginfo_extract(backtrace_slot slot)
+{
+ return (debuginfo)slot;
+}
+
+debuginfo caml_debuginfo_next(debuginfo dbg)
+{
+ /* No inlining in bytecode */
+ return NULL;
+}
diff --git a/test/monniaux/ocaml/byterun/bigarray.c b/test/monniaux/ocaml/byterun/bigarray.c
new file mode 100644
index 00000000..3e376799
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/bigarray.c
@@ -0,0 +1,1223 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <stddef.h>
+#include <stdarg.h>
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/bigarray.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/intext.h"
+#include "caml/hash.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/signals.h"
+
+#define int8 caml_ba_int8
+#define uint8 caml_ba_uint8
+#define int16 caml_ba_int16
+#define uint16 caml_ba_uint16
+
+/* Compute the number of elements of a big array */
+
+CAMLexport uintnat caml_ba_num_elts(struct caml_ba_array * b)
+{
+ uintnat num_elts;
+ int i;
+ num_elts = 1;
+ for (i = 0; i < b->num_dims; i++) num_elts = num_elts * b->dim[i];
+ return num_elts;
+}
+
+/* Size in bytes of a bigarray element, indexed by bigarray kind */
+
+CAMLexport int caml_ba_element_size[] =
+{ 4 /*FLOAT32*/, 8 /*FLOAT64*/,
+ 1 /*SINT8*/, 1 /*UINT8*/,
+ 2 /*SINT16*/, 2 /*UINT16*/,
+ 4 /*INT32*/, 8 /*INT64*/,
+ sizeof(value) /*CAML_INT*/, sizeof(value) /*NATIVE_INT*/,
+ 8 /*COMPLEX32*/, 16 /*COMPLEX64*/,
+ 1 /*CHAR*/
+};
+
+/* Compute the number of bytes for the elements of a big array */
+
+CAMLexport uintnat caml_ba_byte_size(struct caml_ba_array * b)
+{
+ return caml_ba_num_elts(b)
+ * caml_ba_element_size[b->flags & CAML_BA_KIND_MASK];
+}
+
+/* Operation table for bigarrays */
+
+CAMLexport struct custom_operations caml_ba_ops = {
+ "_bigarray",
+ caml_ba_finalize,
+ caml_ba_compare,
+ caml_ba_hash,
+ caml_ba_serialize,
+ caml_ba_deserialize,
+ custom_compare_ext_default
+};
+
+/* Allocation of a big array */
+
+#define CAML_BA_MAX_MEMORY 1024*1024*1024
+/* 1 Gb -- after allocating that much, it's probably worth speeding
+ up the major GC */
+
+/* [caml_ba_alloc] will allocate a new bigarray object in the heap.
+ If [data] is NULL, the memory for the contents is also allocated
+ (with [malloc]) by [caml_ba_alloc].
+ [data] cannot point into the OCaml heap.
+ [dim] may point into an object in the OCaml heap.
+*/
+CAMLexport value
+caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
+{
+ uintnat num_elts, asize, size;
+ int i;
+ value res;
+ struct caml_ba_array * b;
+ intnat dimcopy[CAML_BA_MAX_NUM_DIMS];
+
+ CAMLassert(num_dims >= 0 && num_dims <= CAML_BA_MAX_NUM_DIMS);
+ CAMLassert((flags & CAML_BA_KIND_MASK) <= CAML_BA_CHAR);
+ for (i = 0; i < num_dims; i++) dimcopy[i] = dim[i];
+ size = 0;
+ if (data == NULL) {
+ num_elts = 1;
+ for (i = 0; i < num_dims; i++) {
+ if (caml_umul_overflow(num_elts, dimcopy[i], &num_elts))
+ caml_raise_out_of_memory();
+ }
+ if (caml_umul_overflow(num_elts,
+ caml_ba_element_size[flags & CAML_BA_KIND_MASK],
+ &size))
+ caml_raise_out_of_memory();
+ data = malloc(size);
+ if (data == NULL && size != 0) caml_raise_out_of_memory();
+ flags |= CAML_BA_MANAGED;
+ }
+ asize = SIZEOF_BA_ARRAY + num_dims * sizeof(intnat);
+ res = caml_alloc_custom(&caml_ba_ops, asize, size, CAML_BA_MAX_MEMORY);
+ b = Caml_ba_array_val(res);
+ b->data = data;
+ b->num_dims = num_dims;
+ b->flags = flags;
+ b->proxy = NULL;
+ for (i = 0; i < num_dims; i++) b->dim[i] = dimcopy[i];
+ return res;
+}
+
+/* Same as caml_ba_alloc, but dimensions are passed as a list of
+ arguments */
+
+CAMLexport value caml_ba_alloc_dims(int flags, int num_dims, void * data, ...)
+{
+ va_list ap;
+ intnat dim[CAML_BA_MAX_NUM_DIMS];
+ int i;
+ value res;
+
+ CAMLassert(num_dims <= CAML_BA_MAX_NUM_DIMS);
+ va_start(ap, data);
+ for (i = 0; i < num_dims; i++) dim[i] = va_arg(ap, intnat);
+ va_end(ap);
+ res = caml_ba_alloc(flags, num_dims, data, dim);
+ return res;
+}
+
+/* Finalization of a big array */
+
+CAMLexport void caml_ba_finalize(value v)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(v);
+
+ switch (b->flags & CAML_BA_MANAGED_MASK) {
+ case CAML_BA_EXTERNAL:
+ break;
+ case CAML_BA_MANAGED:
+ if (b->proxy == NULL) {
+ free(b->data);
+ } else {
+ if (-- b->proxy->refcount == 0) {
+ free(b->proxy->data);
+ free(b->proxy);
+ }
+ }
+ break;
+ case CAML_BA_MAPPED_FILE:
+ /* Bigarrays for mapped files use a different finalization method */
+ default:
+ CAMLassert(0);
+ }
+}
+
+/* Comparison of two big arrays */
+
+CAMLexport int caml_ba_compare(value v1, value v2)
+{
+ struct caml_ba_array * b1 = Caml_ba_array_val(v1);
+ struct caml_ba_array * b2 = Caml_ba_array_val(v2);
+ uintnat n, num_elts;
+ intnat flags1, flags2;
+ int i;
+
+ /* Compare kind & layout in case the arguments are of different types */
+ flags1 = b1->flags & (CAML_BA_KIND_MASK | CAML_BA_LAYOUT_MASK);
+ flags2 = b2->flags & (CAML_BA_KIND_MASK | CAML_BA_LAYOUT_MASK);
+ if (flags1 != flags2) return flags2 - flags1;
+ /* Compare number of dimensions */
+ if (b1->num_dims != b2->num_dims) return b2->num_dims - b1->num_dims;
+ /* Same number of dimensions: compare dimensions lexicographically */
+ for (i = 0; i < b1->num_dims; i++) {
+ intnat d1 = b1->dim[i];
+ intnat d2 = b2->dim[i];
+ if (d1 != d2) return d1 < d2 ? -1 : 1;
+ }
+ /* Same dimensions: compare contents lexicographically */
+ num_elts = caml_ba_num_elts(b1);
+
+#define DO_INTEGER_COMPARISON(type) \
+ { type * p1 = b1->data; type * p2 = b2->data; \
+ for (n = 0; n < num_elts; n++) { \
+ type e1 = *p1++; type e2 = *p2++; \
+ if (e1 < e2) return -1; \
+ if (e1 > e2) return 1; \
+ } \
+ return 0; \
+ }
+#define DO_FLOAT_COMPARISON(type) \
+ { type * p1 = b1->data; type * p2 = b2->data; \
+ for (n = 0; n < num_elts; n++) { \
+ type e1 = *p1++; type e2 = *p2++; \
+ if (e1 < e2) return -1; \
+ if (e1 > e2) return 1; \
+ if (e1 != e2) { \
+ caml_compare_unordered = 1; \
+ if (e1 == e1) return 1; \
+ if (e2 == e2) return -1; \
+ } \
+ } \
+ return 0; \
+ }
+
+ switch (b1->flags & CAML_BA_KIND_MASK) {
+ case CAML_BA_COMPLEX32:
+ num_elts *= 2; /*fallthrough*/
+ case CAML_BA_FLOAT32:
+ DO_FLOAT_COMPARISON(float);
+ case CAML_BA_COMPLEX64:
+ num_elts *= 2; /*fallthrough*/
+ case CAML_BA_FLOAT64:
+ DO_FLOAT_COMPARISON(double);
+ case CAML_BA_CHAR:
+ DO_INTEGER_COMPARISON(caml_ba_uint8);
+ case CAML_BA_SINT8:
+ DO_INTEGER_COMPARISON(caml_ba_int8);
+ case CAML_BA_UINT8:
+ DO_INTEGER_COMPARISON(caml_ba_uint8);
+ case CAML_BA_SINT16:
+ DO_INTEGER_COMPARISON(caml_ba_int16);
+ case CAML_BA_UINT16:
+ DO_INTEGER_COMPARISON(caml_ba_uint16);
+ case CAML_BA_INT32:
+ DO_INTEGER_COMPARISON(int32_t);
+ case CAML_BA_INT64:
+ DO_INTEGER_COMPARISON(int64_t);
+ case CAML_BA_CAML_INT:
+ case CAML_BA_NATIVE_INT:
+ DO_INTEGER_COMPARISON(intnat);
+ default:
+ CAMLassert(0);
+ return 0; /* should not happen */
+ }
+#undef DO_INTEGER_COMPARISON
+#undef DO_FLOAT_COMPARISON
+}
+
+/* Hashing of a bigarray */
+
+CAMLexport intnat caml_ba_hash(value v)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(v);
+ intnat num_elts, n;
+ uint32_t h, w;
+ int i;
+
+ num_elts = 1;
+ for (i = 0; i < b->num_dims; i++) num_elts = num_elts * b->dim[i];
+ h = 0;
+
+ switch (b->flags & CAML_BA_KIND_MASK) {
+ case CAML_BA_CHAR:
+ case CAML_BA_SINT8:
+ case CAML_BA_UINT8: {
+ caml_ba_uint8 * p = b->data;
+ if (num_elts > 256) num_elts = 256;
+ for (n = 0; n + 4 <= num_elts; n += 4, p += 4) {
+ w = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
+ h = caml_hash_mix_uint32(h, w);
+ }
+ w = 0;
+ switch (num_elts & 3) {
+ case 3: w = p[2] << 16; /* fallthrough */
+ case 2: w |= p[1] << 8; /* fallthrough */
+ case 1: w |= p[0];
+ h = caml_hash_mix_uint32(h, w);
+ }
+ break;
+ }
+ case CAML_BA_SINT16:
+ case CAML_BA_UINT16: {
+ caml_ba_uint16 * p = b->data;
+ if (num_elts > 128) num_elts = 128;
+ for (n = 0; n + 2 <= num_elts; n += 2, p += 2) {
+ w = p[0] | (p[1] << 16);
+ h = caml_hash_mix_uint32(h, w);
+ }
+ if ((num_elts & 1) != 0)
+ h = caml_hash_mix_uint32(h, p[0]);
+ break;
+ }
+ case CAML_BA_INT32:
+ {
+ uint32_t * p = b->data;
+ if (num_elts > 64) num_elts = 64;
+ for (n = 0; n < num_elts; n++, p++) h = caml_hash_mix_uint32(h, *p);
+ break;
+ }
+ case CAML_BA_CAML_INT:
+ case CAML_BA_NATIVE_INT:
+ {
+ intnat * p = b->data;
+ if (num_elts > 64) num_elts = 64;
+ for (n = 0; n < num_elts; n++, p++) h = caml_hash_mix_intnat(h, *p);
+ break;
+ }
+ case CAML_BA_INT64:
+ {
+ int64_t * p = b->data;
+ if (num_elts > 32) num_elts = 32;
+ for (n = 0; n < num_elts; n++, p++) h = caml_hash_mix_int64(h, *p);
+ break;
+ }
+ case CAML_BA_COMPLEX32:
+ num_elts *= 2; /* fallthrough */
+ case CAML_BA_FLOAT32:
+ {
+ float * p = b->data;
+ if (num_elts > 64) num_elts = 64;
+ for (n = 0; n < num_elts; n++, p++) h = caml_hash_mix_float(h, *p);
+ break;
+ }
+ case CAML_BA_COMPLEX64:
+ num_elts *= 2; /* fallthrough */
+ case CAML_BA_FLOAT64:
+ {
+ double * p = b->data;
+ if (num_elts > 32) num_elts = 32;
+ for (n = 0; n < num_elts; n++, p++) h = caml_hash_mix_double(h, *p);
+ break;
+ }
+ }
+ return h;
+}
+
+static void caml_ba_serialize_longarray(void * data,
+ intnat num_elts,
+ intnat min_val, intnat max_val)
+{
+#ifdef ARCH_SIXTYFOUR
+ int overflow_32 = 0;
+ intnat * p, n;
+ for (n = 0, p = data; n < num_elts; n++, p++) {
+ if (*p < min_val || *p > max_val) { overflow_32 = 1; break; }
+ }
+ if (overflow_32) {
+ caml_serialize_int_1(1);
+ caml_serialize_block_8(data, num_elts);
+ } else {
+ caml_serialize_int_1(0);
+ for (n = 0, p = data; n < num_elts; n++, p++)
+ caml_serialize_int_4((int32_t) *p);
+ }
+#else
+ caml_serialize_int_1(0);
+ caml_serialize_block_4(data, num_elts);
+#endif
+}
+
+CAMLexport void caml_ba_serialize(value v,
+ uintnat * wsize_32,
+ uintnat * wsize_64)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(v);
+ intnat num_elts;
+ int i;
+
+ /* Serialize header information */
+ caml_serialize_int_4(b->num_dims);
+ caml_serialize_int_4(b->flags & (CAML_BA_KIND_MASK | CAML_BA_LAYOUT_MASK));
+ /* On a 64-bit machine, if any of the dimensions is >= 2^32,
+ the size of the marshaled data will be >= 2^32 and
+ extern_value() will fail. So, it is safe to write the dimensions
+ as 32-bit unsigned integers. */
+ for (i = 0; i < b->num_dims; i++) caml_serialize_int_4(b->dim[i]);
+ /* Compute total number of elements */
+ num_elts = 1;
+ for (i = 0; i < b->num_dims; i++) num_elts = num_elts * b->dim[i];
+ /* Serialize elements */
+ switch (b->flags & CAML_BA_KIND_MASK) {
+ case CAML_BA_CHAR:
+ case CAML_BA_SINT8:
+ case CAML_BA_UINT8:
+ caml_serialize_block_1(b->data, num_elts); break;
+ case CAML_BA_SINT16:
+ case CAML_BA_UINT16:
+ caml_serialize_block_2(b->data, num_elts); break;
+ case CAML_BA_FLOAT32:
+ case CAML_BA_INT32:
+ caml_serialize_block_4(b->data, num_elts); break;
+ case CAML_BA_COMPLEX32:
+ caml_serialize_block_4(b->data, num_elts * 2); break;
+ case CAML_BA_FLOAT64:
+ case CAML_BA_INT64:
+ caml_serialize_block_8(b->data, num_elts); break;
+ case CAML_BA_COMPLEX64:
+ caml_serialize_block_8(b->data, num_elts * 2); break;
+ case CAML_BA_CAML_INT:
+ caml_ba_serialize_longarray(b->data, num_elts, -0x40000000, 0x3FFFFFFF);
+ break;
+ case CAML_BA_NATIVE_INT:
+ caml_ba_serialize_longarray(b->data, num_elts, -0x80000000, 0x7FFFFFFF);
+ break;
+ }
+ /* Compute required size in OCaml heap. Assumes struct caml_ba_array
+ is exactly 4 + num_dims words */
+ CAMLassert(SIZEOF_BA_ARRAY == 4 * sizeof(value));
+ *wsize_32 = (4 + b->num_dims) * 4;
+ *wsize_64 = (4 + b->num_dims) * 8;
+}
+
+static void caml_ba_deserialize_longarray(void * dest, intnat num_elts)
+{
+ int sixty = caml_deserialize_uint_1();
+#ifdef ARCH_SIXTYFOUR
+ if (sixty) {
+ caml_deserialize_block_8(dest, num_elts);
+ } else {
+ intnat * p, n;
+ for (n = 0, p = dest; n < num_elts; n++, p++)
+ *p = caml_deserialize_sint_4();
+ }
+#else
+ if (sixty)
+ caml_deserialize_error("input_value: cannot read bigarray "
+ "with 64-bit OCaml ints");
+ caml_deserialize_block_4(dest, num_elts);
+#endif
+}
+
+CAMLexport uintnat caml_ba_deserialize(void * dst)
+{
+ struct caml_ba_array * b = dst;
+ int i;
+ uintnat num_elts, size;
+
+ /* Read back header information */
+ b->num_dims = caml_deserialize_uint_4();
+ if (b->num_dims < 0 || b->num_dims > CAML_BA_MAX_NUM_DIMS)
+ caml_deserialize_error("input_value: wrong number of bigarray dimensions");
+ b->flags = caml_deserialize_uint_4() | CAML_BA_MANAGED;
+ b->proxy = NULL;
+ for (i = 0; i < b->num_dims; i++) b->dim[i] = caml_deserialize_uint_4();
+ /* Compute total number of elements. Watch out for overflows (MPR#7765). */
+ num_elts = 1;
+ for (i = 0; i < b->num_dims; i++) {
+ if (caml_umul_overflow(num_elts, b->dim[i], &num_elts))
+ caml_deserialize_error("input_value: size overflow for bigarray");
+ }
+ /* Determine array size in bytes. Watch out for overflows (MPR#7765). */
+ if ((b->flags & CAML_BA_KIND_MASK) > CAML_BA_CHAR)
+ caml_deserialize_error("input_value: bad bigarray kind");
+ if (caml_umul_overflow(num_elts,
+ caml_ba_element_size[b->flags & CAML_BA_KIND_MASK],
+ &size))
+ caml_deserialize_error("input_value: size overflow for bigarray");
+ /* Allocate room for data */
+ b->data = malloc(size);
+ if (b->data == NULL)
+ caml_deserialize_error("input_value: out of memory for bigarray");
+ /* Read data */
+ switch (b->flags & CAML_BA_KIND_MASK) {
+ case CAML_BA_CHAR:
+ case CAML_BA_SINT8:
+ case CAML_BA_UINT8:
+ caml_deserialize_block_1(b->data, num_elts); break;
+ case CAML_BA_SINT16:
+ case CAML_BA_UINT16:
+ caml_deserialize_block_2(b->data, num_elts); break;
+ case CAML_BA_FLOAT32:
+ case CAML_BA_INT32:
+ caml_deserialize_block_4(b->data, num_elts); break;
+ case CAML_BA_COMPLEX32:
+ caml_deserialize_block_4(b->data, num_elts * 2); break;
+ case CAML_BA_FLOAT64:
+ case CAML_BA_INT64:
+ caml_deserialize_block_8(b->data, num_elts); break;
+ case CAML_BA_COMPLEX64:
+ caml_deserialize_block_8(b->data, num_elts * 2); break;
+ case CAML_BA_CAML_INT:
+ case CAML_BA_NATIVE_INT:
+ caml_ba_deserialize_longarray(b->data, num_elts); break;
+ }
+ /* PR#5516: use C99's flexible array types if possible */
+ return SIZEOF_BA_ARRAY + b->num_dims * sizeof(intnat);
+}
+
+/* Allocate a bigarray from OCaml */
+
+CAMLprim value caml_ba_create(value vkind, value vlayout, value vdim)
+{
+ intnat dim[CAML_BA_MAX_NUM_DIMS];
+ mlsize_t num_dims;
+ int i, flags;
+
+ num_dims = Wosize_val(vdim);
+ /* here num_dims is unsigned (mlsize_t) so no need to check (num_dims >= 0) */
+ if (num_dims > CAML_BA_MAX_NUM_DIMS)
+ caml_invalid_argument("Bigarray.create: bad number of dimensions");
+ for (i = 0; i < num_dims; i++) {
+ dim[i] = Long_val(Field(vdim, i));
+ if (dim[i] < 0)
+ caml_invalid_argument("Bigarray.create: negative dimension");
+ }
+ flags = Caml_ba_kind_val(vkind) | Caml_ba_layout_val(vlayout);
+ return caml_ba_alloc(flags, num_dims, NULL, dim);
+}
+
+/* Given a big array and a vector of indices, check that the indices
+ are within the bounds and return the offset of the corresponding
+ array element in the data part of the array. */
+
+static long caml_ba_offset(struct caml_ba_array * b, intnat * index)
+{
+ intnat offset;
+ int i;
+
+ offset = 0;
+ if ((b->flags & CAML_BA_LAYOUT_MASK) == CAML_BA_C_LAYOUT) {
+ /* C-style layout: row major, indices start at 0 */
+ for (i = 0; i < b->num_dims; i++) {
+ if ((uintnat) index[i] >= (uintnat) b->dim[i])
+ caml_array_bound_error();
+ offset = offset * b->dim[i] + index[i];
+ }
+ } else {
+ /* Fortran-style layout: column major, indices start at 1 */
+ for (i = b->num_dims - 1; i >= 0; i--) {
+ if ((uintnat) (index[i] - 1) >= (uintnat) b->dim[i])
+ caml_array_bound_error();
+ offset = offset * b->dim[i] + (index[i] - 1);
+ }
+ }
+ return offset;
+}
+
+/* Helper function to allocate a record of two double floats */
+
+static value copy_two_doubles(double d0, double d1)
+{
+ value res = caml_alloc_small(2 * Double_wosize, Double_array_tag);
+ Store_double_field(res, 0, d0);
+ Store_double_field(res, 1, d1);
+ return res;
+}
+
+/* Generic code to read from a big array */
+
+value caml_ba_get_N(value vb, value * vind, int nind)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ intnat index[CAML_BA_MAX_NUM_DIMS];
+ int i;
+ intnat offset;
+
+ /* Check number of indices = number of dimensions of array
+ (maybe not necessary if ML typing guarantees this) */
+ if (nind != b->num_dims)
+ caml_invalid_argument("Bigarray.get: wrong number of indices");
+ /* Compute offset and check bounds */
+ for (i = 0; i < b->num_dims; i++) index[i] = Long_val(vind[i]);
+ offset = caml_ba_offset(b, index);
+ /* Perform read */
+ switch ((b->flags) & CAML_BA_KIND_MASK) {
+ default:
+ CAMLassert(0);
+ case CAML_BA_FLOAT32:
+ return caml_copy_double(((float *) b->data)[offset]);
+ case CAML_BA_FLOAT64:
+ return caml_copy_double(((double *) b->data)[offset]);
+ case CAML_BA_SINT8:
+ return Val_int(((int8 *) b->data)[offset]);
+ case CAML_BA_UINT8:
+ return Val_int(((uint8 *) b->data)[offset]);
+ case CAML_BA_SINT16:
+ return Val_int(((int16 *) b->data)[offset]);
+ case CAML_BA_UINT16:
+ return Val_int(((uint16 *) b->data)[offset]);
+ case CAML_BA_INT32:
+ return caml_copy_int32(((int32_t *) b->data)[offset]);
+ case CAML_BA_INT64:
+ return caml_copy_int64(((int64_t *) b->data)[offset]);
+ case CAML_BA_NATIVE_INT:
+ return caml_copy_nativeint(((intnat *) b->data)[offset]);
+ case CAML_BA_CAML_INT:
+ return Val_long(((intnat *) b->data)[offset]);
+ case CAML_BA_COMPLEX32:
+ { float * p = ((float *) b->data) + offset * 2;
+ return copy_two_doubles(p[0], p[1]); }
+ case CAML_BA_COMPLEX64:
+ { double * p = ((double *) b->data) + offset * 2;
+ return copy_two_doubles(p[0], p[1]); }
+ case CAML_BA_CHAR:
+ return Val_int(((unsigned char *) b->data)[offset]);
+ }
+}
+
+CAMLprim value caml_ba_get_1(value vb, value vind1)
+{
+ return caml_ba_get_N(vb, &vind1, 1);
+}
+
+CAMLprim value caml_ba_get_2(value vb, value vind1, value vind2)
+{
+ value vind[2];
+ vind[0] = vind1; vind[1] = vind2;
+ return caml_ba_get_N(vb, vind, 2);
+}
+
+CAMLprim value caml_ba_get_3(value vb, value vind1, value vind2, value vind3)
+{
+ value vind[3];
+ vind[0] = vind1; vind[1] = vind2; vind[2] = vind3;
+ return caml_ba_get_N(vb, vind, 3);
+}
+
+CAMLprim value caml_ba_get_generic(value vb, value vind)
+{
+ return caml_ba_get_N(vb, &Field(vind, 0), Wosize_val(vind));
+}
+
+
+CAMLprim value caml_ba_uint8_get16(value vb, value vind)
+{
+ intnat res;
+ unsigned char b1, b2;
+ intnat idx = Long_val(vind);
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 1) caml_array_bound_error();
+ b1 = ((unsigned char*) b->data)[idx];
+ b2 = ((unsigned char*) b->data)[idx+1];
+#ifdef ARCH_BIG_ENDIAN
+ res = b1 << 8 | b2;
+#else
+ res = b2 << 8 | b1;
+#endif
+ return Val_int(res);
+}
+
+CAMLprim value caml_ba_uint8_get32(value vb, value vind)
+{
+ intnat res;
+ unsigned char b1, b2, b3, b4;
+ intnat idx = Long_val(vind);
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 3) caml_array_bound_error();
+ b1 = ((unsigned char*) b->data)[idx];
+ b2 = ((unsigned char*) b->data)[idx+1];
+ b3 = ((unsigned char*) b->data)[idx+2];
+ b4 = ((unsigned char*) b->data)[idx+3];
+#ifdef ARCH_BIG_ENDIAN
+ res = b1 << 24 | b2 << 16 | b3 << 8 | b4;
+#else
+ res = b4 << 24 | b3 << 16 | b2 << 8 | b1;
+#endif
+ return caml_copy_int32(res);
+}
+
+CAMLprim value caml_ba_uint8_get64(value vb, value vind)
+{
+ uint64_t res;
+ unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
+ intnat idx = Long_val(vind);
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 7) caml_array_bound_error();
+ b1 = ((unsigned char*) b->data)[idx];
+ b2 = ((unsigned char*) b->data)[idx+1];
+ b3 = ((unsigned char*) b->data)[idx+2];
+ b4 = ((unsigned char*) b->data)[idx+3];
+ b5 = ((unsigned char*) b->data)[idx+4];
+ b6 = ((unsigned char*) b->data)[idx+5];
+ b7 = ((unsigned char*) b->data)[idx+6];
+ b8 = ((unsigned char*) b->data)[idx+7];
+#ifdef ARCH_BIG_ENDIAN
+ res = (uint64_t) b1 << 56 | (uint64_t) b2 << 48
+ | (uint64_t) b3 << 40 | (uint64_t) b4 << 32
+ | (uint64_t) b5 << 24 | (uint64_t) b6 << 16
+ | (uint64_t) b7 << 8 | (uint64_t) b8;
+#else
+ res = (uint64_t) b8 << 56 | (uint64_t) b7 << 48
+ | (uint64_t) b6 << 40 | (uint64_t) b5 << 32
+ | (uint64_t) b4 << 24 | (uint64_t) b3 << 16
+ | (uint64_t) b2 << 8 | (uint64_t) b1;
+#endif
+ return caml_copy_int64(res);
+}
+
+/* Generic write to a big array */
+
+static value caml_ba_set_aux(value vb, value * vind, intnat nind, value newval)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ intnat index[CAML_BA_MAX_NUM_DIMS];
+ int i;
+ intnat offset;
+
+ /* Check number of indices = number of dimensions of array
+ (maybe not necessary if ML typing guarantees this) */
+ if (nind != b->num_dims)
+ caml_invalid_argument("Bigarray.set: wrong number of indices");
+ /* Compute offset and check bounds */
+ for (i = 0; i < b->num_dims; i++) index[i] = Long_val(vind[i]);
+ offset = caml_ba_offset(b, index);
+ /* Perform write */
+ switch (b->flags & CAML_BA_KIND_MASK) {
+ default:
+ CAMLassert(0);
+ case CAML_BA_FLOAT32:
+ ((float *) b->data)[offset] = Double_val(newval); break;
+ case CAML_BA_FLOAT64:
+ ((double *) b->data)[offset] = Double_val(newval); break;
+ case CAML_BA_CHAR:
+ case CAML_BA_SINT8:
+ case CAML_BA_UINT8:
+ ((int8 *) b->data)[offset] = Int_val(newval); break;
+ case CAML_BA_SINT16:
+ case CAML_BA_UINT16:
+ ((int16 *) b->data)[offset] = Int_val(newval); break;
+ case CAML_BA_INT32:
+ ((int32_t *) b->data)[offset] = Int32_val(newval); break;
+ case CAML_BA_INT64:
+ ((int64_t *) b->data)[offset] = Int64_val(newval); break;
+ case CAML_BA_NATIVE_INT:
+ ((intnat *) b->data)[offset] = Nativeint_val(newval); break;
+ case CAML_BA_CAML_INT:
+ ((intnat *) b->data)[offset] = Long_val(newval); break;
+ case CAML_BA_COMPLEX32:
+ { float * p = ((float *) b->data) + offset * 2;
+ p[0] = Double_field(newval, 0);
+ p[1] = Double_field(newval, 1);
+ break; }
+ case CAML_BA_COMPLEX64:
+ { double * p = ((double *) b->data) + offset * 2;
+ p[0] = Double_field(newval, 0);
+ p[1] = Double_field(newval, 1);
+ break; }
+ }
+ return Val_unit;
+}
+
+CAMLprim value caml_ba_set_1(value vb, value vind1, value newval)
+{
+ return caml_ba_set_aux(vb, &vind1, 1, newval);
+}
+
+CAMLprim value caml_ba_set_2(value vb, value vind1, value vind2, value newval)
+{
+ value vind[2];
+ vind[0] = vind1; vind[1] = vind2;
+ return caml_ba_set_aux(vb, vind, 2, newval);
+}
+
+CAMLprim value caml_ba_set_3(value vb, value vind1, value vind2, value vind3,
+ value newval)
+{
+ value vind[3];
+ vind[0] = vind1; vind[1] = vind2; vind[2] = vind3;
+ return caml_ba_set_aux(vb, vind, 3, newval);
+}
+
+value caml_ba_set_N(value vb, value * vind, int nargs)
+{
+ return caml_ba_set_aux(vb, vind, nargs - 1, vind[nargs - 1]);
+}
+
+CAMLprim value caml_ba_set_generic(value vb, value vind, value newval)
+{
+ return caml_ba_set_aux(vb, &Field(vind, 0), Wosize_val(vind), newval);
+}
+
+CAMLprim value caml_ba_uint8_set16(value vb, value vind, value newval)
+{
+ unsigned char b1, b2;
+ intnat val;
+ intnat idx = Long_val(vind);
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 1) caml_array_bound_error();
+ val = Long_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 8;
+ b2 = 0xFF & val;
+#else
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ ((unsigned char*) b->data)[idx] = b1;
+ ((unsigned char*) b->data)[idx+1] = b2;
+ return Val_unit;
+}
+
+CAMLprim value caml_ba_uint8_set32(value vb, value vind, value newval)
+{
+ unsigned char b1, b2, b3, b4;
+ intnat idx = Long_val(vind);
+ intnat val;
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 3) caml_array_bound_error();
+ val = Int32_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 24;
+ b2 = 0xFF & val >> 16;
+ b3 = 0xFF & val >> 8;
+ b4 = 0xFF & val;
+#else
+ b4 = 0xFF & val >> 24;
+ b3 = 0xFF & val >> 16;
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ ((unsigned char*) b->data)[idx] = b1;
+ ((unsigned char*) b->data)[idx+1] = b2;
+ ((unsigned char*) b->data)[idx+2] = b3;
+ ((unsigned char*) b->data)[idx+3] = b4;
+ return Val_unit;
+}
+
+CAMLprim value caml_ba_uint8_set64(value vb, value vind, value newval)
+{
+ unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
+ intnat idx = Long_val(vind);
+ int64_t val;
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ if (idx < 0 || idx >= b->dim[0] - 7) caml_array_bound_error();
+ val = Int64_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 56;
+ b2 = 0xFF & val >> 48;
+ b3 = 0xFF & val >> 40;
+ b4 = 0xFF & val >> 32;
+ b5 = 0xFF & val >> 24;
+ b6 = 0xFF & val >> 16;
+ b7 = 0xFF & val >> 8;
+ b8 = 0xFF & val;
+#else
+ b8 = 0xFF & val >> 56;
+ b7 = 0xFF & val >> 48;
+ b6 = 0xFF & val >> 40;
+ b5 = 0xFF & val >> 32;
+ b4 = 0xFF & val >> 24;
+ b3 = 0xFF & val >> 16;
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ ((unsigned char*) b->data)[idx] = b1;
+ ((unsigned char*) b->data)[idx+1] = b2;
+ ((unsigned char*) b->data)[idx+2] = b3;
+ ((unsigned char*) b->data)[idx+3] = b4;
+ ((unsigned char*) b->data)[idx+4] = b5;
+ ((unsigned char*) b->data)[idx+5] = b6;
+ ((unsigned char*) b->data)[idx+6] = b7;
+ ((unsigned char*) b->data)[idx+7] = b8;
+ return Val_unit;
+}
+
+/* Return the number of dimensions of a big array */
+
+CAMLprim value caml_ba_num_dims(value vb)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ return Val_long(b->num_dims);
+}
+
+/* Return the n-th dimension of a big array */
+
+CAMLprim value caml_ba_dim(value vb, value vn)
+{
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ intnat n = Long_val(vn);
+ if (n < 0 || n >= b->num_dims) caml_invalid_argument("Bigarray.dim");
+ return Val_long(b->dim[n]);
+}
+
+CAMLprim value caml_ba_dim_1(value vb)
+{
+ return caml_ba_dim(vb, Val_int(0));
+}
+
+CAMLprim value caml_ba_dim_2(value vb)
+{
+ return caml_ba_dim(vb, Val_int(1));
+}
+
+CAMLprim value caml_ba_dim_3(value vb)
+{
+ return caml_ba_dim(vb, Val_int(2));
+}
+
+/* Return the kind of a big array */
+
+CAMLprim value caml_ba_kind(value vb)
+{
+ return Val_caml_ba_kind(Caml_ba_array_val(vb)->flags & CAML_BA_KIND_MASK);
+}
+
+/* Return the layout of a big array */
+
+CAMLprim value caml_ba_layout(value vb)
+{
+ int layout = Caml_ba_array_val(vb)->flags & CAML_BA_LAYOUT_MASK;
+ return Val_caml_ba_layout(layout);
+}
+
+/* Create / update proxy to indicate that b2 is a sub-array of b1 */
+
+static void caml_ba_update_proxy(struct caml_ba_array * b1,
+ struct caml_ba_array * b2)
+{
+ struct caml_ba_proxy * proxy;
+ /* Nothing to do for un-managed arrays */
+ if ((b1->flags & CAML_BA_MANAGED_MASK) == CAML_BA_EXTERNAL) return;
+ if (b1->proxy != NULL) {
+ /* If b1 is already a proxy for a larger array, increment refcount of
+ proxy */
+ b2->proxy = b1->proxy;
+ ++ b1->proxy->refcount;
+ } else {
+ /* Otherwise, create proxy and attach it to both b1 and b2 */
+ proxy = malloc(sizeof(struct caml_ba_proxy));
+ if (proxy == NULL) caml_raise_out_of_memory();
+ proxy->refcount = 2; /* original array + sub array */
+ proxy->data = b1->data;
+ proxy->size =
+ b1->flags & CAML_BA_MAPPED_FILE ? caml_ba_byte_size(b1) : 0;
+ b1->proxy = proxy;
+ b2->proxy = proxy;
+ }
+}
+
+/* Slicing */
+
+CAMLprim value caml_ba_slice(value vb, value vind)
+{
+ CAMLparam2 (vb, vind);
+ #define b ((struct caml_ba_array *) Caml_ba_array_val(vb))
+ CAMLlocal1 (res);
+ intnat index[CAML_BA_MAX_NUM_DIMS];
+ int num_inds, i;
+ intnat offset;
+ intnat * sub_dims;
+ char * sub_data;
+
+ /* Check number of indices <= number of dimensions of array */
+ num_inds = Wosize_val(vind);
+ if (num_inds > b->num_dims)
+ caml_invalid_argument("Bigarray.slice: too many indices");
+ /* Compute offset and check bounds */
+ if ((b->flags & CAML_BA_LAYOUT_MASK) == CAML_BA_C_LAYOUT) {
+ /* We slice from the left */
+ for (i = 0; i < num_inds; i++) index[i] = Long_val(Field(vind, i));
+ for (/*nothing*/; i < b->num_dims; i++) index[i] = 0;
+ offset = caml_ba_offset(b, index);
+ sub_dims = b->dim + num_inds;
+ } else {
+ /* We slice from the right */
+ for (i = 0; i < num_inds; i++)
+ index[b->num_dims - num_inds + i] = Long_val(Field(vind, i));
+ for (i = 0; i < b->num_dims - num_inds; i++) index[i] = 1;
+ offset = caml_ba_offset(b, index);
+ sub_dims = b->dim;
+ }
+ sub_data =
+ (char *) b->data +
+ offset * caml_ba_element_size[b->flags & CAML_BA_KIND_MASK];
+ /* Allocate an OCaml bigarray to hold the result */
+ res = caml_ba_alloc(b->flags, b->num_dims - num_inds, sub_data, sub_dims);
+ /* Create or update proxy in case of managed bigarray */
+ caml_ba_update_proxy(b, Caml_ba_array_val(res));
+ /* Return result */
+ CAMLreturn (res);
+
+ #undef b
+}
+
+/* Changing the layout of an array (memory is shared) */
+
+CAMLprim value caml_ba_change_layout(value vb, value vlayout)
+{
+ CAMLparam2 (vb, vlayout);
+ CAMLlocal1 (res);
+ #define b ((struct caml_ba_array *) Caml_ba_array_val(vb))
+ /* if the layout is different, change the flags and reverse the dimensions */
+ if (Caml_ba_layout_val(vlayout) != (b->flags & CAML_BA_LAYOUT_MASK)) {
+ /* change the flags to reflect the new layout */
+ int flags = (b->flags & (CAML_BA_KIND_MASK | CAML_BA_MANAGED_MASK))
+ | Caml_ba_layout_val(vlayout);
+ /* reverse the dimensions */
+ intnat new_dim[CAML_BA_MAX_NUM_DIMS];
+ unsigned int i;
+ for(i = 0; i < b->num_dims; i++) new_dim[i] = b->dim[b->num_dims - i - 1];
+ res = caml_ba_alloc(flags, b->num_dims, b->data, new_dim);
+ caml_ba_update_proxy(b, Caml_ba_array_val(res));
+ CAMLreturn(res);
+ } else {
+ /* otherwise, do nothing */
+ CAMLreturn(vb);
+ }
+ #undef b
+}
+
+
+/* Extracting a sub-array of same number of dimensions */
+
+CAMLprim value caml_ba_sub(value vb, value vofs, value vlen)
+{
+ CAMLparam3 (vb, vofs, vlen);
+ CAMLlocal1 (res);
+ #define b ((struct caml_ba_array *) Caml_ba_array_val(vb))
+ intnat ofs = Long_val(vofs);
+ intnat len = Long_val(vlen);
+ int i, changed_dim;
+ intnat mul;
+ char * sub_data;
+
+ /* Compute offset and check bounds */
+ if ((b->flags & CAML_BA_LAYOUT_MASK) == CAML_BA_C_LAYOUT) {
+ /* We reduce the first dimension */
+ mul = 1;
+ for (i = 1; i < b->num_dims; i++) mul *= b->dim[i];
+ changed_dim = 0;
+ } else {
+ /* We reduce the last dimension */
+ mul = 1;
+ for (i = 0; i < b->num_dims - 1; i++) mul *= b->dim[i];
+ changed_dim = b->num_dims - 1;
+ ofs--; /* Fortran arrays start at 1 */
+ }
+ if (ofs < 0 || len < 0 || ofs + len > b->dim[changed_dim])
+ caml_invalid_argument("Bigarray.sub: bad sub-array");
+ sub_data =
+ (char *) b->data +
+ ofs * mul * caml_ba_element_size[b->flags & CAML_BA_KIND_MASK];
+ /* Allocate an OCaml bigarray to hold the result */
+ res = caml_ba_alloc(b->flags, b->num_dims, sub_data, b->dim);
+ /* Doctor the changed dimension */
+ Caml_ba_array_val(res)->dim[changed_dim] = len;
+ /* Create or update proxy in case of managed bigarray */
+ caml_ba_update_proxy(b, Caml_ba_array_val(res));
+ /* Return result */
+ CAMLreturn (res);
+
+ #undef b
+}
+
+/* Copying a big array into another one */
+
+#define LEAVE_RUNTIME_OP_CUTOFF 4096
+#define is_mmapped(ba) ((ba)->flags & CAML_BA_MAPPED_FILE)
+
+CAMLprim value caml_ba_blit(value vsrc, value vdst)
+{
+ CAMLparam2(vsrc, vdst);
+ struct caml_ba_array * src = Caml_ba_array_val(vsrc);
+ struct caml_ba_array * dst = Caml_ba_array_val(vdst);
+ void *src_data = src->data;
+ void *dst_data = dst->data;
+ int i;
+ intnat num_bytes;
+ int leave_runtime;
+
+ /* Check same numbers of dimensions and same dimensions */
+ if (src->num_dims != dst->num_dims) goto blit_error;
+ for (i = 0; i < src->num_dims; i++)
+ if (src->dim[i] != dst->dim[i]) goto blit_error;
+ /* Compute number of bytes in array data */
+ num_bytes =
+ caml_ba_num_elts(src)
+ * caml_ba_element_size[src->flags & CAML_BA_KIND_MASK];
+ leave_runtime =
+ (
+ (num_bytes >= LEAVE_RUNTIME_OP_CUTOFF*sizeof(long))
+ || is_mmapped(src)
+ || is_mmapped(dst)
+ );
+ /* Do the copying */
+ if (leave_runtime) caml_enter_blocking_section();
+ memmove (dst_data, src_data, num_bytes);
+ if (leave_runtime) caml_leave_blocking_section();
+ CAMLreturn (Val_unit);
+ blit_error:
+ caml_invalid_argument("Bigarray.blit: dimension mismatch");
+ CAMLreturn (Val_unit); /* not reached */
+}
+
+/* Filling a big array with a given value */
+
+#define FILL_GEN_LOOP(n_ops, loop) do{ \
+ int leave_runtime = ((n_ops >= LEAVE_RUNTIME_OP_CUTOFF) || is_mmapped(b)); \
+ if (leave_runtime) caml_enter_blocking_section(); \
+ loop; \
+ if (leave_runtime) caml_leave_blocking_section(); \
+}while(0)
+
+#define FILL_SCALAR_LOOP \
+ FILL_GEN_LOOP(num_elts, \
+ for (p = data; num_elts > 0; p++, num_elts--) *p = init)
+
+#define FILL_COMPLEX_LOOP \
+ FILL_GEN_LOOP(num_elts + num_elts, \
+ for (p = data; num_elts > 0; num_elts--) { *p++ = init0; *p++ = init1; })
+
+CAMLprim value caml_ba_fill(value vb, value vinit)
+{
+ CAMLparam1(vb);
+ struct caml_ba_array * b = Caml_ba_array_val(vb);
+ void *data = b->data;
+ intnat num_elts = caml_ba_num_elts(b);
+
+ switch (b->flags & CAML_BA_KIND_MASK) {
+ default:
+ CAMLassert(0);
+ case CAML_BA_FLOAT32: {
+ float init = Double_val(vinit);
+ float * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_FLOAT64: {
+ double init = Double_val(vinit);
+ double * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_CHAR:
+ case CAML_BA_SINT8:
+ case CAML_BA_UINT8: {
+ int init = Int_val(vinit);
+ unsigned char * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_SINT16:
+ case CAML_BA_UINT16: {
+ int init = Int_val(vinit);
+ int16 * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_INT32: {
+ int32_t init = Int32_val(vinit);
+ int32_t * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_INT64: {
+ int64_t init = Int64_val(vinit);
+ int64_t * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_NATIVE_INT: {
+ intnat init = Nativeint_val(vinit);
+ intnat * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_CAML_INT: {
+ intnat init = Long_val(vinit);
+ intnat * p;
+ FILL_SCALAR_LOOP;
+ break;
+ }
+ case CAML_BA_COMPLEX32: {
+ float init0 = Double_field(vinit, 0);
+ float init1 = Double_field(vinit, 1);
+ float * p;
+ FILL_COMPLEX_LOOP;
+ break;
+ }
+ case CAML_BA_COMPLEX64: {
+ double init0 = Double_field(vinit, 0);
+ double init1 = Double_field(vinit, 1);
+ double * p;
+ FILL_COMPLEX_LOOP;
+ break;
+ }
+ }
+ CAMLreturn (Val_unit);
+}
+
+/* Reshape an array: change dimensions and number of dimensions, preserving
+ array contents */
+
+CAMLprim value caml_ba_reshape(value vb, value vdim)
+{
+ CAMLparam2 (vb, vdim);
+ CAMLlocal1 (res);
+#define b ((struct caml_ba_array *) Caml_ba_array_val(vb))
+ intnat dim[CAML_BA_MAX_NUM_DIMS];
+ mlsize_t num_dims;
+ uintnat num_elts;
+ int i;
+
+ num_dims = Wosize_val(vdim);
+ /* here num_dims is unsigned (mlsize_t) so no need to check (num_dims >= 0) */
+ if (num_dims > CAML_BA_MAX_NUM_DIMS)
+ caml_invalid_argument("Bigarray.reshape: bad number of dimensions");
+ num_elts = 1;
+ for (i = 0; i < num_dims; i++) {
+ dim[i] = Long_val(Field(vdim, i));
+ if (dim[i] < 0)
+ caml_invalid_argument("Bigarray.reshape: negative dimension");
+ num_elts *= dim[i];
+ }
+ /* Check that sizes agree */
+ if (num_elts != caml_ba_num_elts(b))
+ caml_invalid_argument("Bigarray.reshape: size mismatch");
+ /* Create bigarray with same data and new dimensions */
+ res = caml_ba_alloc(b->flags, num_dims, b->data, dim);
+ /* Create or update proxy in case of managed bigarray */
+ caml_ba_update_proxy(b, Caml_ba_array_val(res));
+ /* Return result */
+ CAMLreturn (res);
+
+#undef b
+}
diff --git a/test/monniaux/ocaml/byterun/callback.c b/test/monniaux/ocaml/byterun/callback.c
new file mode 100644
index 00000000..9c479b30
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/callback.c
@@ -0,0 +1,262 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Callbacks from C to OCaml */
+
+#include <string.h>
+#include "caml/callback.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+
+#ifndef NATIVE_CODE
+
+/* Bytecode callbacks */
+
+#include "caml/interp.h"
+#include "caml/instruct.h"
+#include "caml/fix_code.h"
+#include "caml/stacks.h"
+
+CAMLexport int caml_callback_depth = 0;
+
+#ifndef LOCAL_CALLBACK_BYTECODE
+static opcode_t callback_code[] = { ACC, 0, APPLY, 0, POP, 1, STOP };
+#endif
+
+
+#ifdef THREADED_CODE
+
+static int callback_code_threaded = 0;
+
+static void thread_callback(void)
+{
+ caml_thread_code(callback_code, sizeof(callback_code));
+ callback_code_threaded = 1;
+}
+
+#define Init_callback() if (!callback_code_threaded) thread_callback()
+
+#else
+
+#define Init_callback()
+
+#endif
+
+CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
+{
+ int i;
+ value res;
+
+ /* some alternate bytecode implementations (e.g. a JIT translator)
+ might require that the bytecode is kept in a local variable on
+ the C stack */
+#ifdef LOCAL_CALLBACK_BYTECODE
+ opcode_t local_callback_code[7];
+#endif
+
+ CAMLassert(narg + 4 <= 256);
+
+ caml_extern_sp -= narg + 4;
+ for (i = 0; i < narg; i++) caml_extern_sp[i] = args[i]; /* arguments */
+#ifndef LOCAL_CALLBACK_BYTECODE
+ caml_extern_sp[narg] = (value) (callback_code + 4); /* return address */
+ caml_extern_sp[narg + 1] = Val_unit; /* environment */
+ caml_extern_sp[narg + 2] = Val_long(0); /* extra args */
+ caml_extern_sp[narg + 3] = closure;
+ Init_callback();
+ callback_code[1] = narg + 3;
+ callback_code[3] = narg;
+ res = caml_interprete(callback_code, sizeof(callback_code));
+#else /*have LOCAL_CALLBACK_BYTECODE*/
+ caml_extern_sp[narg] = (value) (local_callback_code + 4); /* return address */
+ caml_extern_sp[narg + 1] = Val_unit; /* environment */
+ caml_extern_sp[narg + 2] = Val_long(0); /* extra args */
+ caml_extern_sp[narg + 3] = closure;
+ local_callback_code[0] = ACC;
+ local_callback_code[1] = narg + 3;
+ local_callback_code[2] = APPLY;
+ local_callback_code[3] = narg;
+ local_callback_code[4] = POP;
+ local_callback_code[5] = 1;
+ local_callback_code[6] = STOP;
+#ifdef THREADED_CODE
+ caml_thread_code(local_callback_code, sizeof(local_callback_code));
+#endif /*THREADED_CODE*/
+ res = caml_interprete(local_callback_code, sizeof(local_callback_code));
+ caml_release_bytecode(local_callback_code, sizeof(local_callback_code));
+#endif /*LOCAL_CALLBACK_BYTECODE*/
+ if (Is_exception_result(res)) caml_extern_sp += narg + 4; /* PR#1228 */
+ return res;
+}
+
+CAMLexport value caml_callback_exn(value closure, value arg1)
+{
+ value arg[1];
+ arg[0] = arg1;
+ return caml_callbackN_exn(closure, 1, arg);
+}
+
+CAMLexport value caml_callback2_exn(value closure, value arg1, value arg2)
+{
+ value arg[2];
+ arg[0] = arg1;
+ arg[1] = arg2;
+ return caml_callbackN_exn(closure, 2, arg);
+}
+
+CAMLexport value caml_callback3_exn(value closure,
+ value arg1, value arg2, value arg3)
+{
+ value arg[3];
+ arg[0] = arg1;
+ arg[1] = arg2;
+ arg[2] = arg3;
+ return caml_callbackN_exn(closure, 3, arg);
+}
+
+#else
+
+/* Native-code callbacks. caml_callback[123]_exn are implemented in asm. */
+
+CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
+{
+ CAMLparam1 (closure);
+ CAMLxparamN (args, narg);
+ CAMLlocal1 (res);
+ int i;
+
+ res = closure;
+ for (i = 0; i < narg; /*nothing*/) {
+ /* Pass as many arguments as possible */
+ switch (narg - i) {
+ case 1:
+ res = caml_callback_exn(res, args[i]);
+ if (Is_exception_result(res)) CAMLreturn (res);
+ i += 1;
+ break;
+ case 2:
+ res = caml_callback2_exn(res, args[i], args[i + 1]);
+ if (Is_exception_result(res)) CAMLreturn (res);
+ i += 2;
+ break;
+ default:
+ res = caml_callback3_exn(res, args[i], args[i + 1], args[i + 2]);
+ if (Is_exception_result(res)) CAMLreturn (res);
+ i += 3;
+ break;
+ }
+ }
+ CAMLreturn (res);
+}
+
+#endif
+
+/* Exception-propagating variants of the above */
+
+CAMLexport value caml_callback (value closure, value arg)
+{
+ value res = caml_callback_exn(closure, arg);
+ if (Is_exception_result(res)) caml_raise(Extract_exception(res));
+ return res;
+}
+
+CAMLexport value caml_callback2 (value closure, value arg1, value arg2)
+{
+ value res = caml_callback2_exn(closure, arg1, arg2);
+ if (Is_exception_result(res)) caml_raise(Extract_exception(res));
+ return res;
+}
+
+CAMLexport value caml_callback3 (value closure, value arg1, value arg2,
+ value arg3)
+{
+ value res = caml_callback3_exn(closure, arg1, arg2, arg3);
+ if (Is_exception_result(res)) caml_raise(Extract_exception(res));
+ return res;
+}
+
+CAMLexport value caml_callbackN (value closure, int narg, value args[])
+{
+ value res = caml_callbackN_exn(closure, narg, args);
+ if (Is_exception_result(res)) caml_raise(Extract_exception(res));
+ return res;
+}
+
+/* Naming of OCaml values */
+
+struct named_value {
+ value val;
+ struct named_value * next;
+ char name[1];
+};
+
+#define Named_value_size 13
+
+static struct named_value * named_value_table[Named_value_size] = { NULL, };
+
+static unsigned int hash_value_name(char const *name)
+{
+ unsigned int h;
+ for (h = 0; *name != 0; name++) h = h * 19 + *name;
+ return h % Named_value_size;
+}
+
+CAMLprim value caml_register_named_value(value vname, value val)
+{
+ struct named_value * nv;
+ const char * name = String_val(vname);
+ size_t namelen = strlen(name);
+ unsigned int h = hash_value_name(name);
+
+ for (nv = named_value_table[h]; nv != NULL; nv = nv->next) {
+ if (strcmp(name, nv->name) == 0) {
+ nv->val = val;
+ return Val_unit;
+ }
+ }
+ nv = (struct named_value *)
+ caml_stat_alloc(sizeof(struct named_value) + namelen);
+ memcpy(nv->name, name, namelen + 1);
+ nv->val = val;
+ nv->next = named_value_table[h];
+ named_value_table[h] = nv;
+ caml_register_global_root(&nv->val);
+ return Val_unit;
+}
+
+CAMLexport value * caml_named_value(char const *name)
+{
+ struct named_value * nv;
+ for (nv = named_value_table[hash_value_name(name)];
+ nv != NULL;
+ nv = nv->next) {
+ if (strcmp(name, nv->name) == 0) return &nv->val;
+ }
+ return NULL;
+}
+
+CAMLexport void caml_iterate_named_values(caml_named_action f)
+{
+ int i;
+ for(i = 0; i < Named_value_size; i++){
+ struct named_value * nv;
+ for (nv = named_value_table[i]; nv != NULL; nv = nv->next) {
+ f( &nv->val, nv->name );
+ }
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/caml/address_class.h b/test/monniaux/ocaml/byterun/caml/address_class.h
new file mode 100644
index 00000000..85e22d32
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/address_class.h
@@ -0,0 +1,85 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Classification of addresses for GC and runtime purposes. */
+
+#ifndef CAML_ADDRESS_CLASS_H
+#define CAML_ADDRESS_CLASS_H
+
+#include "config.h"
+#include "misc.h"
+#include "mlvalues.h"
+
+/* Use the following macros to test an address for the different classes
+ it might belong to. */
+
+#define Is_young(val) \
+ (CAMLassert (Is_block (val)), \
+ (addr)(val) < (addr)caml_young_end && (addr)(val) > (addr)caml_young_start)
+
+#define Is_in_heap(a) (Classify_addr(a) & In_heap)
+
+#define Is_in_heap_or_young(a) (Classify_addr(a) & (In_heap | In_young))
+
+#define Is_in_value_area(a) \
+ (Classify_addr(a) & (In_heap | In_young | In_static_data))
+
+#define Is_in_code_area(pc) \
+ ( ((char *)(pc) >= caml_code_area_start && \
+ (char *)(pc) <= caml_code_area_end) \
+ || (Classify_addr(pc) & In_code_area) )
+
+#define Is_in_static_data(a) (Classify_addr(a) & In_static_data)
+
+/***********************************************************************/
+/* The rest of this file is private and may change without notice. */
+
+extern value *caml_young_start, *caml_young_end;
+extern char * caml_code_area_start, * caml_code_area_end;
+
+#define Not_in_heap 0
+#define In_heap 1
+#define In_young 2
+#define In_static_data 4
+#define In_code_area 8
+
+#ifdef ARCH_SIXTYFOUR
+
+/* 64 bits: Represent page table as a sparse hash table */
+int caml_page_table_lookup(void * addr);
+#define Classify_addr(a) (caml_page_table_lookup((void *)(a)))
+
+#else
+
+/* 32 bits: Represent page table as a 2-level array */
+#define Pagetable2_log 11
+#define Pagetable2_size (1 << Pagetable2_log)
+#define Pagetable1_log (Page_log + Pagetable2_log)
+#define Pagetable1_size (1 << (32 - Pagetable1_log))
+CAMLextern unsigned char * caml_page_table[Pagetable1_size];
+
+#define Pagetable_index1(a) (((uintnat)(a)) >> Pagetable1_log)
+#define Pagetable_index2(a) \
+ ((((uintnat)(a)) >> Page_log) & (Pagetable2_size - 1))
+#define Classify_addr(a) \
+ caml_page_table[Pagetable_index1(a)][Pagetable_index2(a)]
+
+#endif
+
+int caml_page_table_add(int kind, void * start, void * end);
+int caml_page_table_remove(int kind, void * start, void * end);
+int caml_page_table_initialize(mlsize_t bytesize);
+
+#endif /* CAML_ADDRESS_CLASS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/alloc.h b/test/monniaux/ocaml/byterun/caml/alloc.h
new file mode 100644
index 00000000..81fff858
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/alloc.h
@@ -0,0 +1,82 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_ALLOC_H
+#define CAML_ALLOC_H
+
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLextern value caml_alloc (mlsize_t wosize, tag_t);
+CAMLextern value caml_alloc_small (mlsize_t wosize, tag_t);
+CAMLextern value caml_alloc_tuple (mlsize_t wosize);
+CAMLextern value caml_alloc_float_array (mlsize_t len);
+CAMLextern value caml_alloc_string (mlsize_t len); /* len in bytes (chars) */
+CAMLextern value caml_alloc_initialized_string (mlsize_t len, const char *);
+CAMLextern value caml_copy_string (char const *);
+CAMLextern value caml_copy_string_array (char const **);
+CAMLextern value caml_copy_double (double);
+CAMLextern value caml_copy_int32 (int32_t); /* defined in [ints.c] */
+CAMLextern value caml_copy_int64 (int64_t); /* defined in [ints.c] */
+CAMLextern value caml_copy_nativeint (intnat); /* defined in [ints.c] */
+CAMLextern value caml_alloc_array (value (*funct) (char const *),
+ char const ** array);
+CAMLextern value caml_alloc_sprintf(const char * format, ...)
+#ifdef __GNUC__
+ __attribute__ ((format (printf, 1, 2)))
+#endif
+;
+
+CAMLextern value caml_alloc_with_profinfo (mlsize_t, tag_t, intnat);
+CAMLextern value caml_alloc_small_with_my_or_given_profinfo (
+ mlsize_t, tag_t, uintnat);
+CAMLextern value caml_alloc_small_with_profinfo (mlsize_t, tag_t, intnat);
+
+typedef void (*final_fun)(value);
+CAMLextern value caml_alloc_final (mlsize_t wosize,
+ final_fun, /*finalization function*/
+ mlsize_t, /*resources consumed*/
+ mlsize_t /*max resources*/);
+
+CAMLextern int caml_convert_flag_list (value, int *);
+
+/* Convenience functions to deal with unboxable types. */
+static inline value caml_alloc_unboxed (value arg) { return arg; }
+static inline value caml_alloc_boxed (value arg) {
+ value result = caml_alloc_small (1, 0);
+ Field (result, 0) = arg;
+ return result;
+}
+static inline value caml_field_unboxed (value arg) { return arg; }
+static inline value caml_field_boxed (value arg) { return Field (arg, 0); }
+
+/* Unannotated unboxable types are boxed by default. (may change in the
+ future) */
+#define caml_alloc_unboxable caml_alloc_boxed
+#define caml_field_unboxable caml_field_boxed
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_ALLOC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/backtrace.h b/test/monniaux/ocaml/byterun/caml/backtrace.h
new file mode 100644
index 00000000..fc0baf2d
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/backtrace.h
@@ -0,0 +1,136 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_BACKTRACE_H
+#define CAML_BACKTRACE_H
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+#include "exec.h"
+
+/* Runtime support for backtrace generation.
+ *
+ * It has two kind of users:
+ * - high-level API to capture and decode backtraces;
+ * - low-level runtime routines, to introspect machine state and determine
+ * whether a backtrace should be generated when using "raise".
+ *
+ * Backtrace generation is split in multiple steps.
+ * The lowest-level one, done by [backtrace_prim.c] just fills the
+ * [caml_backtrace_buffer] variable each time a frame is unwinded.
+ * At that point, we don't know whether the backtrace will be useful or not so
+ * this code should be as fast as possible.
+ *
+ * If the backtrace happens to be useful, later passes will read
+ * [caml_backtrace_buffer] and turn it into a [raw_backtrace] and then a
+ * [backtrace].
+ * This is done in [backtrace.c] and [stdlib/printexc.ml].
+ *
+ * Content of buffers
+ * ------------------
+ *
+ * [caml_backtrace_buffer] (really cheap)
+ * Backend and process image dependent, abstracted by C-type backtrace_slot.
+ * [raw_backtrace] (cheap)
+ * OCaml values of abstract type [Printexc.raw_backtrace_slot],
+ * still backend and process image dependent (unsafe to marshal).
+ * [backtrace] (more expensive)
+ * OCaml values of algebraic data-type [Printexc.backtrace_slot]
+ */
+
+/* Non zero iff backtraces are recorded.
+ * One should use to change this variable [caml_record_backtrace].
+ */
+CAMLextern int caml_backtrace_active;
+
+/* The [backtrace_slot] type represents values stored in the
+ * [caml_backtrace_buffer]. In bytecode, it is the same as a
+ * [code_t], in native code it as a [frame_descr *]. The difference
+ * doesn't matter for code outside [backtrace_prim.c], so it is just
+ * exposed as a [backtrace_slot].
+ */
+typedef void * backtrace_slot;
+
+/* The [caml_backtrace_buffer] and [caml_backtrace_last_exn]
+ * variables are valid only if [caml_backtrace_active != 0].
+ *
+ * They are part of the state specific to each thread, and threading libraries
+ * are responsible for copying them on context switch.
+ * See [otherlibs/systhreads/st_stubs.c] and [otherlibs/threads/scheduler.c].
+ */
+
+/* [caml_backtrace_buffer] is filled by runtime when unwinding stack.
+ * It is an array ranging from [0] to [caml_backtrace_pos - 1].
+ * [caml_backtrace_pos] is always zero if [!caml_backtrace_active].
+ *
+ * Its maximum size is determined by [BACKTRACE_BUFFER_SIZE] from
+ * [backtrace_prim.h], but this shouldn't affect users.
+ */
+CAMLextern backtrace_slot * caml_backtrace_buffer;
+CAMLextern int caml_backtrace_pos;
+
+/* [caml_backtrace_last_exn] stores the last exception value that was raised,
+ * iff [caml_backtrace_active != 0].
+ * It is tested for equality to determine whether a raise is a re-raise of the
+ * same exception.
+ *
+ * FIXME: this shouldn't matter anymore. Since OCaml 4.02, non-parameterized
+ * exceptions are constant, so physical equality is no longer appropriate.
+ * raise and re-raise are distinguished by:
+ * - passing reraise = 1 to [caml_stash_backtrace] (see below) in the bytecode
+ * interpreter;
+ * - directly resetting [caml_backtrace_pos] to 0 in native runtimes for raise.
+ */
+CAMLextern value caml_backtrace_last_exn;
+
+/* [caml_record_backtrace] toggle backtrace recording on and off.
+ * This function can be called at runtime by user-code, or during
+ * initialization if backtraces were requested.
+ *
+ * It might be called before GC initialization, so it shouldn't do OCaml
+ * allocation.
+ */
+CAMLprim value caml_record_backtrace(value vflag);
+
+
+#ifndef NATIVE_CODE
+
+/* Path to the file containing debug information, if any, or NULL. */
+CAMLextern char_os * caml_cds_file;
+
+/* Primitive called _only_ by runtime to record unwinded frames to
+ * backtrace. A similar primitive exists for native code, but with a
+ * different prototype. */
+extern void caml_stash_backtrace(value exn, code_t pc, value * sp, int reraise);
+
+#endif
+
+
+/* Default (C-level) printer for backtraces. It is called if an
+ * exception causes a termination of the program or of a thread.
+ *
+ * [Printexc] provide a higher-level printer mimicking its output but making
+ * use of registered exception printers, and is used when possible in place of
+ * this function after [Printexc] initialization.
+ */
+CAMLextern void caml_print_exception_backtrace(void);
+
+void caml_init_backtrace(void);
+CAMLexport void caml_init_debug_info(void);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_BACKTRACE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/backtrace_prim.h b/test/monniaux/ocaml/byterun/caml/backtrace_prim.h
new file mode 100644
index 00000000..2484b294
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/backtrace_prim.h
@@ -0,0 +1,91 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_BACKTRACE_PRIM_H
+#define CAML_BACKTRACE_PRIM_H
+
+#ifdef CAML_INTERNALS
+
+#include "backtrace.h"
+
+/* Backtrace generation is split in [backtrace.c] and [backtrace_prim.c].
+ *
+ * [backtrace_prim.c] contains all backend-specific code, and has two different
+ * implementations in [byterun/backtrace_prim.c] and [asmrun/backtrace_prim.c].
+ *
+ * [backtrace.c] has a unique implementation, and expose a uniform
+ * higher level API above [backtrace_prim.c].
+ */
+
+/* Extract location information for the given raw_backtrace_slot */
+
+struct caml_loc_info {
+ int loc_valid;
+ int loc_is_raise;
+ char * loc_filename;
+ int loc_lnum;
+ int loc_startchr;
+ int loc_endchr;
+ int loc_is_inlined;
+};
+
+/* When compiling with -g, backtrace slots have debug info associated.
+ * When a call is inlined in native mode, debuginfos form a linked list.
+ */
+typedef void * debuginfo;
+
+/* Check availability of debug information before extracting a trace.
+ * Relevant for bytecode, always true for native code. */
+int caml_debug_info_available(void);
+
+/* Return debuginfo associated to a slot or NULL. */
+debuginfo caml_debuginfo_extract(backtrace_slot slot);
+
+/* In case of an inlined call return next debuginfo or NULL otherwise. */
+debuginfo caml_debuginfo_next(debuginfo dbg);
+
+/* Extract locations from backtrace_slot */
+void caml_debuginfo_location(debuginfo dbg, /*out*/ struct caml_loc_info * li);
+
+/* In order to prevent the GC from walking through the debug
+ information (which have no headers), we transform slots to 31/63 bits
+ ocaml integers by shifting them by 1 to the right. We do not lose
+ information as slots are aligned.
+
+ In particular, we do not need to use [caml_modify] when setting
+ an array element with such a value.
+ */
+#define Val_backtrace_slot(bslot) (Val_long(((uintnat)(bslot))>>1))
+#define Backtrace_slot_val(vslot) ((backtrace_slot)(Long_val(vslot) << 1))
+
+/* Allocate the caml_backtrace_buffer. Returns 0 on success, -1 otherwise */
+int caml_alloc_backtrace_buffer(void);
+
+#define BACKTRACE_BUFFER_SIZE 1024
+
+/* Besides decoding backtrace info, [backtrace_prim] has two other
+ * responsibilities:
+ *
+ * It defines the [caml_stash_backtrace] function, which is called to quickly
+ * fill the backtrace buffer by walking the stack when an exception is raised.
+ *
+ * It also defines the [caml_get_current_callstack] OCaml primitive, which also
+ * walks the stack but directly turns it into a [raw_backtrace] and is called
+ * explicitly.
+ */
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_BACKTRACE_PRIM_H */
diff --git a/test/monniaux/ocaml/byterun/caml/bigarray.h b/test/monniaux/ocaml/byterun/caml/bigarray.h
new file mode 100644
index 00000000..fc1fb145
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/bigarray.h
@@ -0,0 +1,134 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_BIGARRAY_H
+#define CAML_BIGARRAY_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "config.h"
+#include "mlvalues.h"
+
+typedef signed char caml_ba_int8;
+typedef unsigned char caml_ba_uint8;
+#if defined(HAS_STDINT_H)
+typedef int16_t caml_ba_int16;
+typedef uint16_t caml_ba_uint16;
+#elif SIZEOF_SHORT == 2
+typedef short caml_ba_int16;
+typedef unsigned short caml_ba_uint16;
+#else
+#error "No 16-bit integer type available"
+#endif
+
+#define CAML_BA_MAX_NUM_DIMS 16
+
+enum caml_ba_kind {
+ CAML_BA_FLOAT32, /* Single-precision floats */
+ CAML_BA_FLOAT64, /* Double-precision floats */
+ CAML_BA_SINT8, /* Signed 8-bit integers */
+ CAML_BA_UINT8, /* Unsigned 8-bit integers */
+ CAML_BA_SINT16, /* Signed 16-bit integers */
+ CAML_BA_UINT16, /* Unsigned 16-bit integers */
+ CAML_BA_INT32, /* Signed 32-bit integers */
+ CAML_BA_INT64, /* Signed 64-bit integers */
+ CAML_BA_CAML_INT, /* OCaml-style integers (signed 31 or 63 bits) */
+ CAML_BA_NATIVE_INT, /* Platform-native long integers (32 or 64 bits) */
+ CAML_BA_COMPLEX32, /* Single-precision complex */
+ CAML_BA_COMPLEX64, /* Double-precision complex */
+ CAML_BA_CHAR, /* Characters */
+ CAML_BA_KIND_MASK = 0xFF /* Mask for kind in flags field */
+};
+
+#define Caml_ba_kind_val(v) Int_val(v)
+
+#define Val_caml_ba_kind(k) Val_int(k)
+
+enum caml_ba_layout {
+ CAML_BA_C_LAYOUT = 0, /* Row major, indices start at 0 */
+ CAML_BA_FORTRAN_LAYOUT = 0x100, /* Column major, indices start at 1 */
+ CAML_BA_LAYOUT_MASK = 0x100, /* Mask for layout in flags field */
+ CAML_BA_LAYOUT_SHIFT = 8 /* Bit offset of layout flag */
+};
+
+#define Caml_ba_layout_val(v) (Int_val(v) << CAML_BA_LAYOUT_SHIFT)
+
+#define Val_caml_ba_layout(l) Val_int(l >> CAML_BA_LAYOUT_SHIFT)
+
+enum caml_ba_managed {
+ CAML_BA_EXTERNAL = 0, /* Data is not allocated by OCaml */
+ CAML_BA_MANAGED = 0x200, /* Data is allocated by OCaml */
+ CAML_BA_MAPPED_FILE = 0x400, /* Data is a memory mapped file */
+ CAML_BA_MANAGED_MASK = 0x600 /* Mask for "managed" bits in flags field */
+};
+
+struct caml_ba_proxy {
+ intnat refcount; /* Reference count */
+ void * data; /* Pointer to base of actual data */
+ uintnat size; /* Size of data in bytes (if mapped file) */
+};
+
+struct caml_ba_array {
+ void * data; /* Pointer to raw data */
+ intnat num_dims; /* Number of dimensions */
+ intnat flags; /* Kind of element array + memory layout + allocation status */
+ struct caml_ba_proxy * proxy; /* The proxy for sub-arrays, or NULL */
+ /* PR#5516: use C99's flexible array types if possible */
+#if (__STDC_VERSION__ >= 199901L)
+ intnat dim[] /*[num_dims]*/; /* Size in each dimension */
+#else
+ intnat dim[1] /*[num_dims]*/; /* Size in each dimension */
+#endif
+};
+
+/* Size of struct caml_ba_array, in bytes, without dummy first dimension */
+#if (__STDC_VERSION__ >= 199901L)
+#define SIZEOF_BA_ARRAY sizeof(struct caml_ba_array)
+#else
+#define SIZEOF_BA_ARRAY (sizeof(struct caml_ba_array) - sizeof(intnat))
+#endif
+
+#define Caml_ba_array_val(v) ((struct caml_ba_array *) Data_custom_val(v))
+
+#define Caml_ba_data_val(v) (Caml_ba_array_val(v)->data)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLextern value
+ caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim);
+CAMLextern value caml_ba_alloc_dims(int flags, int num_dims, void * data,
+ ... /*dimensions, with type intnat */);
+CAMLextern uintnat caml_ba_byte_size(struct caml_ba_array * b);
+CAMLextern uintnat caml_ba_num_elts(struct caml_ba_array * b);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef CAML_INTERNALS
+
+CAMLextern int caml_ba_element_size[];
+CAMLextern void caml_ba_finalize(value v);
+CAMLextern int caml_ba_compare(value v1, value v2);
+CAMLextern intnat caml_ba_hash(value v);
+CAMLextern void caml_ba_serialize(value, uintnat *, uintnat *);
+CAMLextern uintnat caml_ba_deserialize(void * dst);
+
+#endif
+
+#endif /* CAML_BIGARRAY_H */
diff --git a/test/monniaux/ocaml/byterun/caml/callback.h b/test/monniaux/ocaml/byterun/caml/callback.h
new file mode 100644
index 00000000..93208b7a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/callback.h
@@ -0,0 +1,63 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Callbacks from C to OCaml */
+
+#ifndef CAML_CALLBACK_H
+#define CAML_CALLBACK_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLextern value caml_callback (value closure, value arg);
+CAMLextern value caml_callback2 (value closure, value arg1, value arg2);
+CAMLextern value caml_callback3 (value closure, value arg1, value arg2,
+ value arg3);
+CAMLextern value caml_callbackN (value closure, int narg, value args[]);
+
+CAMLextern value caml_callback_exn (value closure, value arg);
+CAMLextern value caml_callback2_exn (value closure, value arg1, value arg2);
+CAMLextern value caml_callback3_exn (value closure,
+ value arg1, value arg2, value arg3);
+CAMLextern value caml_callbackN_exn (value closure, int narg, value args[]);
+
+#define Make_exception_result(v) ((v) | 2)
+#define Is_exception_result(v) (((v) & 3) == 2)
+#define Extract_exception(v) ((v) & ~3)
+
+CAMLextern value * caml_named_value (char const * name);
+typedef void (*caml_named_action) (value*, char *);
+CAMLextern void caml_iterate_named_values(caml_named_action f);
+
+CAMLextern void caml_main (char_os ** argv);
+CAMLextern void caml_startup (char_os ** argv);
+CAMLextern value caml_startup_exn (char_os ** argv);
+CAMLextern void caml_startup_pooled (char_os ** argv);
+CAMLextern value caml_startup_pooled_exn (char_os ** argv);
+CAMLextern void caml_shutdown (void);
+
+CAMLextern int caml_callback_depth;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/caml/compact.h b/test/monniaux/ocaml/byterun/caml/compact.h
new file mode 100644
index 00000000..e29d0e86
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/compact.h
@@ -0,0 +1,31 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_COMPACT_H
+#define CAML_COMPACT_H
+
+#ifdef CAML_INTERNALS
+
+#include "config.h"
+#include "misc.h"
+#include "mlvalues.h"
+
+void caml_compact_heap (void);
+void caml_compact_heap_maybe (void);
+void caml_invert_root (value v, value *p);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_COMPACT_H */
diff --git a/test/monniaux/ocaml/byterun/caml/compare.h b/test/monniaux/ocaml/byterun/caml/compare.h
new file mode 100644
index 00000000..54b71581
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/compare.h
@@ -0,0 +1,25 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, Projet Moscova, INRIA Rocquencourt */
+/* */
+/* Copyright 2003 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_COMPARE_H
+#define CAML_COMPARE_H
+
+#ifdef CAML_INTERNALS
+
+CAMLextern int caml_compare_unordered;
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_COMPARE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/compatibility.h b/test/monniaux/ocaml/byterun/caml/compatibility.h
new file mode 100644
index 00000000..082943c6
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/compatibility.h
@@ -0,0 +1,375 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Moscova, INRIA Rocquencourt */
+/* */
+/* Copyright 2003 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* definitions for compatibility with old identifiers */
+
+#ifndef CAML_COMPATIBILITY_H
+#define CAML_COMPATIBILITY_H
+
+/* internal global variables renamed between 4.02.1 and 4.03.0 */
+#define caml_stat_top_heap_size Bsize_wsize(caml_stat_top_heap_wsz)
+#define caml_stat_heap_size Bsize_wsize(caml_stat_heap_wsz)
+
+#ifndef CAML_NAME_SPACE
+
+/*
+ #define --> CAMLextern (defined with CAMLexport or CAMLprim)
+ (rien) --> CAMLprim
+ g --> global C identifier
+ x --> special case
+
+ SP* signals the special cases:
+ - when the identifier was not simply prefixed with [caml_]
+ - when the [caml_] version was already used for something else, and
+ was renamed out of the way (watch out for [caml_alloc] and
+ [caml_array_bound_error] in *.s)
+*/
+
+/* a faire:
+ - ui_* (reverifier que win32.c n'en depend pas)
+*/
+
+
+/* **** alloc.c */
+#define alloc caml_alloc /*SP*/
+#define alloc_small caml_alloc_small
+#define alloc_tuple caml_alloc_tuple
+#define alloc_string caml_alloc_string
+#define alloc_final caml_alloc_final
+#define copy_string caml_copy_string
+#define alloc_array caml_alloc_array
+#define copy_string_array caml_copy_string_array
+#define convert_flag_list caml_convert_flag_list
+
+/* **** array.c */
+
+/* **** backtrace.c */
+#define backtrace_active caml_backtrace_active
+#define backtrace_pos caml_backtrace_pos
+#define backtrace_buffer caml_backtrace_buffer
+#define backtrace_last_exn caml_backtrace_last_exn
+#define print_exception_backtrace caml_print_exception_backtrace
+
+/* **** callback.c */
+#define callback_depth caml_callback_depth
+#define callbackN_exn caml_callbackN_exn
+#define callback_exn caml_callback_exn
+#define callback2_exn caml_callback2_exn
+#define callback3_exn caml_callback3_exn
+#define callback caml_callback
+#define callback2 caml_callback2
+#define callback3 caml_callback3
+#define callbackN caml_callbackN
+
+/* **** compact.c */
+
+/* **** compare.c */
+#define compare_unordered caml_compare_unordered
+
+/* **** custom.c */
+#define alloc_custom caml_alloc_custom
+#define register_custom_operations caml_register_custom_operations
+
+/* **** debugger.c */
+
+/* **** dynlink.c */
+
+/* **** extern.c */
+#define output_val caml_output_val
+#define output_value_to_malloc caml_output_value_to_malloc
+#define output_value_to_block caml_output_value_to_block
+#define serialize_int_1 caml_serialize_int_1
+#define serialize_int_2 caml_serialize_int_2
+#define serialize_int_4 caml_serialize_int_4
+#define serialize_int_8 caml_serialize_int_8
+#define serialize_float_4 caml_serialize_float_4
+#define serialize_float_8 caml_serialize_float_8
+#define serialize_block_1 caml_serialize_block_1
+#define serialize_block_2 caml_serialize_block_2
+#define serialize_block_4 caml_serialize_block_4
+#define serialize_block_8 caml_serialize_block_8
+#define serialize_block_float_8 caml_serialize_block_float_8
+
+/* **** fail.c */
+#define external_raise caml_external_raise
+#define mlraise caml_raise /*SP*/
+#define raise_constant caml_raise_constant
+#define raise_with_arg caml_raise_with_arg
+#define raise_with_string caml_raise_with_string
+#define failwith caml_failwith
+#define invalid_argument caml_invalid_argument
+#define array_bound_error caml_array_bound_error /*SP*/
+#define raise_out_of_memory caml_raise_out_of_memory
+#define raise_stack_overflow caml_raise_stack_overflow
+#define raise_sys_error caml_raise_sys_error
+#define raise_end_of_file caml_raise_end_of_file
+#define raise_zero_divide caml_raise_zero_divide
+#define raise_not_found caml_raise_not_found
+#define raise_sys_blocked_io caml_raise_sys_blocked_io
+/* **** asmrun/fail.c */
+/* **** asmrun/<arch>.s */
+
+/* **** finalise.c */
+
+/* **** fix_code.c */
+
+/* **** floats.c */
+/*#define Double_val caml_Double_val done in mlvalues.h as needed */
+/*#define Store_double_val caml_Store_double_val done in mlvalues.h as needed */
+#define copy_double caml_copy_double
+
+/* **** freelist.c */
+
+/* **** gc_ctrl.c */
+
+/* **** globroots.c */
+#define register_global_root caml_register_global_root
+#define remove_global_root caml_remove_global_root
+
+/* **** hash.c */
+#define hash_variant caml_hash_variant
+
+/* **** instrtrace.c */
+
+/* **** intern.c */
+#define input_val caml_input_val
+#define input_val_from_string caml_input_val_from_string
+#define input_value_from_malloc caml_input_value_from_malloc
+#define input_value_from_block caml_input_value_from_block
+#define deserialize_uint_1 caml_deserialize_uint_1
+#define deserialize_sint_1 caml_deserialize_sint_1
+#define deserialize_uint_2 caml_deserialize_uint_2
+#define deserialize_sint_2 caml_deserialize_sint_2
+#define deserialize_uint_4 caml_deserialize_uint_4
+#define deserialize_sint_4 caml_deserialize_sint_4
+#define deserialize_uint_8 caml_deserialize_uint_8
+#define deserialize_sint_8 caml_deserialize_sint_8
+#define deserialize_float_4 caml_deserialize_float_4
+#define deserialize_float_8 caml_deserialize_float_8
+#define deserialize_block_1 caml_deserialize_block_1
+#define deserialize_block_2 caml_deserialize_block_2
+#define deserialize_block_4 caml_deserialize_block_4
+#define deserialize_block_8 caml_deserialize_block_8
+#define deserialize_block_float_8 caml_deserialize_block_float_8
+#define deserialize_error caml_deserialize_error
+
+/* **** interp.c */
+
+/* **** ints.c */
+#define int32_ops caml_int32_ops
+#define copy_int32 caml_copy_int32
+/*#define Int64_val caml_Int64_val *** done in mlvalues.h as needed */
+#define int64_ops caml_int64_ops
+#define copy_int64 caml_copy_int64
+#define nativeint_ops caml_nativeint_ops
+#define copy_nativeint caml_copy_nativeint
+
+/* **** io.c */
+#define channel_mutex_free caml_channel_mutex_free
+#define channel_mutex_lock caml_channel_mutex_lock
+#define channel_mutex_unlock caml_channel_mutex_unlock
+#define channel_mutex_unlock_exn caml_channel_mutex_unlock_exn
+#define all_opened_channels caml_all_opened_channels
+#define open_descriptor_in caml_open_descriptor_in /*SP*/
+#define open_descriptor_out caml_open_descriptor_out /*SP*/
+#define close_channel caml_close_channel /*SP*/
+#define channel_size caml_channel_size /*SP*/
+#define channel_binary_mode caml_channel_binary_mode
+#define flush_partial caml_flush_partial /*SP*/
+#define flush caml_flush /*SP*/
+#define putword caml_putword
+#define putblock caml_putblock
+#define really_putblock caml_really_putblock
+#define seek_out caml_seek_out /*SP*/
+#define pos_out caml_pos_out /*SP*/
+#define do_read caml_do_read
+#define refill caml_refill
+#define getword caml_getword
+#define getblock caml_getblock
+#define really_getblock caml_really_getblock
+#define seek_in caml_seek_in /*SP*/
+#define pos_in caml_pos_in /*SP*/
+#define input_scan_line caml_input_scan_line /*SP*/
+#define finalize_channel caml_finalize_channel
+#define alloc_channel caml_alloc_channel
+/*#define Val_file_offset caml_Val_file_offset *** done in io.h as needed */
+/*#define File_offset_val caml_File_offset_val *** done in io.h as needed */
+
+/* **** lexing.c */
+
+/* **** main.c */
+/* *** no change */
+
+/* **** major_gc.c */
+#define heap_start caml_heap_start
+#define page_table caml_page_table
+
+/* **** md5.c */
+#define md5_string caml_md5_string
+#define md5_chan caml_md5_chan
+#define MD5Init caml_MD5Init
+#define MD5Update caml_MD5Update
+#define MD5Final caml_MD5Final
+#define MD5Transform caml_MD5Transform
+
+/* **** memory.c */
+#define alloc_shr caml_alloc_shr
+#define initialize caml_initialize
+#define modify caml_modify
+#define stat_alloc caml_stat_alloc
+#define stat_free caml_stat_free
+#define stat_resize caml_stat_resize
+
+/* **** meta.c */
+
+/* **** minor_gc.c */
+#define young_start caml_young_start
+#define young_end caml_young_end
+#define young_ptr caml_young_ptr
+#define young_limit caml_young_limit
+#define ref_table caml_ref_table
+#define minor_collection caml_minor_collection
+#define check_urgent_gc caml_check_urgent_gc
+
+/* **** misc.c */
+
+/* **** obj.c */
+
+/* **** parsing.c */
+
+/* **** prims.c */
+
+/* **** printexc.c */
+#define format_caml_exception caml_format_exception /*SP*/
+
+/* **** roots.c */
+#define local_roots caml_local_roots
+#define scan_roots_hook caml_scan_roots_hook
+#define do_local_roots caml_do_local_roots
+
+/* **** signals.c */
+#define pending_signals caml_pending_signals
+#define something_to_do caml_something_to_do
+#define enter_blocking_section_hook caml_enter_blocking_section_hook
+#define leave_blocking_section_hook caml_leave_blocking_section_hook
+#define try_leave_blocking_section_hook caml_try_leave_blocking_section_hook
+#define async_action_hook caml_async_action_hook
+#define enter_blocking_section caml_enter_blocking_section
+#define leave_blocking_section caml_leave_blocking_section
+#define convert_signal_number caml_convert_signal_number
+/* **** asmrun/signals.c */
+#define garbage_collection caml_garbage_collection
+
+/* **** stacks.c */
+#define stack_low caml_stack_low
+#define stack_high caml_stack_high
+#define stack_threshold caml_stack_threshold
+#define extern_sp caml_extern_sp
+#define trapsp caml_trapsp
+#define trap_barrier caml_trap_barrier
+
+/* **** startup.c */
+#define atom_table caml_atom_table
+/* **** asmrun/startup.c */
+#define static_data_start caml_static_data_start
+#define static_data_end caml_static_data_end
+
+/* **** str.c */
+#define string_length caml_string_length
+
+/* **** sys.c */
+#define sys_error caml_sys_error
+#define sys_exit caml_sys_exit
+
+/* **** terminfo.c */
+
+/* **** unix.c & win32.c */
+#define search_exe_in_path caml_search_exe_in_path
+
+/* **** weak.c */
+
+/* **** asmcomp/asmlink.ml */
+
+/* **** asmcomp/cmmgen.ml */
+
+/* **** asmcomp/asmlink.ml, asmcomp/cmmgen.ml, asmcomp/compilenv.ml */
+
+/* ************************************************************* */
+
+/* **** otherlibs/bigarray */
+#define int8 caml_ba_int8
+#define uint8 caml_ba_uint8
+#define int16 caml_ba_int16
+#define uint16 caml_ba_uint16
+#define MAX_NUM_DIMS CAML_BA_MAX_NUM_DIMS
+#define caml_bigarray_kind caml_ba_kind
+#define BIGARRAY_FLOAT32 CAML_BA_FLOAT32
+#define BIGARRAY_FLOAT64 CAML_BA_FLOAT64
+#define BIGARRAY_SINT8 CAML_BA_SINT8
+#define BIGARRAY_UINT8 CAML_BA_UINT8
+#define BIGARRAY_SINT16 CAML_BA_SINT16
+#define BIGARRAY_UINT16 CAML_BA_UINT16
+#define BIGARRAY_INT32 CAML_BA_INT32
+#define BIGARRAY_INT64 CAML_BA_INT64
+#define BIGARRAY_CAML_INT CAML_BA_CAML_INT
+#define BIGARRAY_NATIVE_INT CAML_BA_NATIVE_INT
+#define BIGARRAY_COMPLEX32 CAML_BA_COMPLEX32
+#define BIGARRAY_COMPLEX64 CAML_BA_COMPLEX64
+#define BIGARRAY_KIND_MASK CAML_BA_KIND_MASK
+#define caml_bigarray_layout caml_ba_layout
+#define BIGARRAY_C_LAYOUT CAML_BA_C_LAYOUT
+#define BIGARRAY_FORTRAN_LAYOUT CAML_BA_FORTRAN_LAYOUT
+#define BIGARRAY_LAYOUT_MASK CAML_BA_LAYOUT_MASK
+#define caml_bigarray_managed caml_ba_managed
+#define BIGARRAY_EXTERNAL CAML_BA_EXTERNAL
+#define BIGARRAY_MANAGED CAML_BA_MANAGED
+#define BIGARRAY_MAPPED_FILE CAML_BA_MAPPED_FILE
+#define BIGARRAY_MANAGED_MASK CAML_BA_MANAGED_MASK
+#define caml_bigarray_proxy caml_ba_proxy
+#define caml_bigarray caml_ba_array
+#define Bigarray_val Caml_ba_array_val
+#define Data_bigarray_val Caml_ba_data_val
+#define alloc_bigarray caml_ba_alloc
+#define alloc_bigarray_dims caml_ba_alloc_dims
+#define bigarray_map_file caml_ba_map_file
+#define bigarray_unmap_file caml_ba_unmap_file
+#define bigarray_element_size caml_ba_element_size
+#define bigarray_byte_size caml_ba_byte_size
+#define bigarray_deserialize caml_ba_deserialize
+#define MAX_BIGARRAY_MEMORY CAML_BA_MAX_MEMORY
+#define bigarray_create caml_ba_create
+#define bigarray_get_N caml_ba_get_N
+#define bigarray_get_1 caml_ba_get_1
+#define bigarray_get_2 caml_ba_get_2
+#define bigarray_get_3 caml_ba_get_3
+#define bigarray_get_generic caml_ba_get_generic
+#define bigarray_set_1 caml_ba_set_1
+#define bigarray_set_2 caml_ba_set_2
+#define bigarray_set_3 caml_ba_set_3
+#define bigarray_set_N caml_ba_set_N
+#define bigarray_set_generic caml_ba_set_generic
+#define bigarray_num_dims caml_ba_num_dims
+#define bigarray_dim caml_ba_dim
+#define bigarray_kind caml_ba_kind
+#define bigarray_layout caml_ba_layout
+#define bigarray_slice caml_ba_slice
+#define bigarray_sub caml_ba_sub
+#define bigarray_blit caml_ba_blit
+#define bigarray_fill caml_ba_fill
+#define bigarray_reshape caml_ba_reshape
+#define bigarray_init caml_ba_init
+
+#endif /* CAML_NAME_SPACE */
+#endif /* CAML_COMPATIBILITY_H */
diff --git a/test/monniaux/ocaml/byterun/caml/config.h b/test/monniaux/ocaml/byterun/caml/config.h
new file mode 100644
index 00000000..06d8d368
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/config.h
@@ -0,0 +1,208 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_CONFIG_H
+#define CAML_CONFIG_H
+
+/* <include m.h> */
+/* <include s.h> */
+/* <private> */
+#include "m.h"
+#include "s.h"
+#ifdef BOOTSTRAPPING_FLEXLINK
+#undef SUPPORT_DYNAMIC_LINKING
+#endif
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+
+#include <stddef.h>
+
+#ifdef HAS_STDINT_H
+#include <stdint.h>
+#endif
+
+#ifndef ARCH_SIZET_PRINTF_FORMAT
+#define ARCH_SIZET_PRINTF_FORMAT "z"
+#endif
+
+/* Types for 32-bit integers, 64-bit integers, and
+ native integers (as wide as a pointer type) */
+
+#ifndef ARCH_INT32_TYPE
+#if SIZEOF_INT == 4
+#define ARCH_INT32_TYPE int
+#define ARCH_UINT32_TYPE unsigned int
+#define ARCH_INT32_PRINTF_FORMAT ""
+#elif SIZEOF_LONG == 4
+#define ARCH_INT32_TYPE long
+#define ARCH_UINT32_TYPE unsigned long
+#define ARCH_INT32_PRINTF_FORMAT "l"
+#elif SIZEOF_SHORT == 4
+#define ARCH_INT32_TYPE short
+#define ARCH_UINT32_TYPE unsigned short
+#define ARCH_INT32_PRINTF_FORMAT ""
+#else
+#error "No 32-bit integer type available"
+#endif
+#endif
+
+#ifndef ARCH_INT64_TYPE
+#if SIZEOF_LONG == 8
+#define ARCH_INT64_TYPE long
+#define ARCH_UINT64_TYPE unsigned long
+#define ARCH_INT64_PRINTF_FORMAT "l"
+#elif SIZEOF_LONGLONG == 8
+#define ARCH_INT64_TYPE long long
+#define ARCH_UINT64_TYPE unsigned long long
+#define ARCH_INT64_PRINTF_FORMAT "ll"
+#else
+#error "No 64-bit integer type available"
+#endif
+#endif
+
+#ifndef HAS_STDINT_H
+/* Not a C99 compiler, typically MSVC. Define the C99 types we use. */
+typedef ARCH_INT32_TYPE int32_t;
+typedef ARCH_UINT32_TYPE uint32_t;
+typedef ARCH_INT64_TYPE int64_t;
+typedef ARCH_UINT64_TYPE uint64_t;
+#if SIZEOF_SHORT == 2
+typedef short int16_t;
+typedef unsigned short uint16_t;
+#else
+#error "No 16-bit integer type available"
+#endif
+#endif
+
+#if SIZEOF_PTR == SIZEOF_LONG
+/* Standard models: ILP32 or I32LP64 */
+typedef long intnat;
+typedef unsigned long uintnat;
+#define ARCH_INTNAT_PRINTF_FORMAT "l"
+#elif SIZEOF_PTR == SIZEOF_INT
+/* Hypothetical IP32L64 model */
+typedef int intnat;
+typedef unsigned int uintnat;
+#define ARCH_INTNAT_PRINTF_FORMAT ""
+#elif SIZEOF_PTR == 8
+/* Win64 model: IL32P64 */
+typedef int64_t intnat;
+typedef uint64_t uintnat;
+#define ARCH_INTNAT_PRINTF_FORMAT ARCH_INT64_PRINTF_FORMAT
+#else
+#error "No integer type available to represent pointers"
+#endif
+
+/* Endianness of floats */
+
+/* ARCH_FLOAT_ENDIANNESS encodes the byte order of doubles as follows:
+ the value [0xabcdefgh] means that the least significant byte of the
+ float is at byte offset [a], the next lsb at [b], ..., and the
+ most significant byte at [h]. */
+
+#if defined(__arm__) && !defined(__ARM_EABI__)
+#define ARCH_FLOAT_ENDIANNESS 0x45670123
+#elif defined(ARCH_BIG_ENDIAN)
+#define ARCH_FLOAT_ENDIANNESS 0x76543210
+#else
+#define ARCH_FLOAT_ENDIANNESS 0x01234567
+#endif
+
+
+/* We use threaded code interpretation if the compiler provides labels
+ as first-class values (GCC 2.x). */
+
+#if defined(__GNUC__) && __GNUC__ >= 2 && !defined(DEBUG) \
+ && !defined (SHRINKED_GNUC) && !defined(CAML_JIT)
+#define THREADED_CODE
+#endif
+
+
+/* Memory model parameters */
+
+/* The size of a page for memory management (in bytes) is [1 << Page_log].
+ [Page_size] must be a multiple of [sizeof (value)].
+ [Page_log] must be be >= 8 and <= 20.
+ Do not change the definition of [Page_size]. */
+#define Page_log 12 /* A page is 4 kilobytes. */
+#define Page_size (1 << Page_log)
+
+/* Initial size of stack (bytes). */
+#define Stack_size (4096 * sizeof(value))
+
+/* Minimum free size of stack (bytes); below that, it is reallocated. */
+#define Stack_threshold (256 * sizeof(value))
+
+/* Default maximum size of the stack (words). */
+#define Max_stack_def (1024 * 1024)
+
+
+/* Maximum size of a block allocated in the young generation (words). */
+/* Must be > 4 */
+#define Max_young_wosize 256
+#define Max_young_whsize (Whsize_wosize (Max_young_wosize))
+
+
+/* Minimum size of the minor zone (words).
+ This must be at least [2 * Max_young_whsize]. */
+#define Minor_heap_min 4096
+
+/* Maximum size of the minor zone (words).
+ Must be greater than or equal to [Minor_heap_min].
+*/
+#define Minor_heap_max (1 << 28)
+
+/* Default size of the minor zone. (words) */
+#define Minor_heap_def 16384
+
+
+/* Minimum size increment when growing the heap (words).
+ Must be a multiple of [Page_size / sizeof (value)]. */
+#define Heap_chunk_min (15 * Page_size)
+
+/* Default size increment when growing the heap.
+ If this is <= 1000, it's a percentage of the current heap size.
+ If it is > 1000, it's a number of words. */
+#define Heap_chunk_def 15
+
+/* Default initial size of the major heap (words);
+ Must be a multiple of [Page_size / sizeof (value)]. */
+#define Init_heap_def (4 * Page_size)
+/* (about 512 kB for a 32-bit platform, 1 MB for a 64-bit platform.) */
+
+
+/* Default speed setting for the major GC. The heap will grow until
+ the dead objects and the free list represent this percentage of the
+ total size of live objects. */
+#define Percent_free_def 80
+
+/* Default setting for the compacter: 500%
+ (i.e. trigger the compacter when 5/6 of the heap is free or garbage)
+ This can be set quite high because the overhead is over-estimated
+ when fragmentation occurs.
+ */
+#define Max_percent_free_def 500
+
+/* Default setting for the major GC slice smoothing window: 1
+ (i.e. no smoothing)
+*/
+#define Major_window_def 1
+
+/* Maximum size of the major GC slice smoothing window. */
+#define Max_major_window 50
+
+#endif /* CAML_CONFIG_H */
diff --git a/test/monniaux/ocaml/byterun/caml/custom.h b/test/monniaux/ocaml/byterun/caml/custom.h
new file mode 100644
index 00000000..6bc3aa95
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/custom.h
@@ -0,0 +1,73 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_CUSTOM_H
+#define CAML_CUSTOM_H
+
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "mlvalues.h"
+
+struct custom_operations {
+ char *identifier;
+ void (*finalize)(value v);
+ int (*compare)(value v1, value v2);
+ intnat (*hash)(value v);
+ void (*serialize)(value v,
+ /*out*/ uintnat * bsize_32 /*size in bytes*/,
+ /*out*/ uintnat * bsize_64 /*size in bytes*/);
+ uintnat (*deserialize)(void * dst);
+ int (*compare_ext)(value v1, value v2);
+};
+
+#define custom_finalize_default NULL
+#define custom_compare_default NULL
+#define custom_hash_default NULL
+#define custom_serialize_default NULL
+#define custom_deserialize_default NULL
+#define custom_compare_ext_default NULL
+
+#define Custom_ops_val(v) (*((struct custom_operations **) (v)))
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+CAMLextern value caml_alloc_custom(struct custom_operations * ops,
+ uintnat size, /*size in bytes*/
+ mlsize_t mem, /*resources consumed*/
+ mlsize_t max /*max resources*/);
+
+CAMLextern void caml_register_custom_operations(struct custom_operations * ops);
+
+CAMLextern int caml_compare_unordered;
+ /* Used by custom comparison to report unordered NaN-like cases. */
+
+#ifdef CAML_INTERNALS
+extern struct custom_operations * caml_find_custom_operations(char * ident);
+extern struct custom_operations *
+ caml_final_custom_operations(void (*fn)(value));
+
+extern void caml_init_custom_operations(void);
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_CUSTOM_H */
diff --git a/test/monniaux/ocaml/byterun/caml/debugger.h b/test/monniaux/ocaml/byterun/caml/debugger.h
new file mode 100644
index 00000000..c98f35a8
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/debugger.h
@@ -0,0 +1,117 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Interface with the debugger */
+
+#ifndef CAML_DEBUGGER_H
+#define CAML_DEBUGGER_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "mlvalues.h"
+
+CAMLextern int caml_debugger_in_use;
+CAMLextern int caml_debugger_fork_mode; /* non-zero for parent */
+extern uintnat caml_event_count;
+
+enum event_kind {
+ EVENT_COUNT, BREAKPOINT, PROGRAM_START, PROGRAM_EXIT,
+ TRAP_BARRIER, UNCAUGHT_EXC
+};
+
+void caml_debugger_init (void);
+void caml_debugger (enum event_kind event);
+void caml_debugger_cleanup_fork (void);
+
+/* Communication protocol */
+
+/* Requests from the debugger to the runtime system */
+
+enum debugger_request {
+ REQ_SET_EVENT = 'e', /* uint32_t pos */
+ /* Set an event on the instruction at position pos */
+ REQ_SET_BREAKPOINT = 'B', /* uint32_t pos, (char k) */
+ /* Set a breakpoint at position pos */
+ /* In profiling mode, the breakpoint kind is set to k */
+ REQ_RESET_INSTR = 'i', /* uint32_t pos */
+ /* Clear an event or breapoint at position pos, restores initial instr. */
+ REQ_CHECKPOINT = 'c', /* no args */
+ /* Checkpoint the runtime system by forking a child process.
+ Reply is pid of child process or -1 if checkpoint failed. */
+ REQ_GO = 'g', /* uint32_t n */
+ /* Run the program for n events.
+ Reply is one of debugger_reply described below. */
+ REQ_STOP = 's', /* no args */
+ /* Terminate the runtime system */
+ REQ_WAIT = 'w', /* no args */
+ /* Reap one dead child (a discarded checkpoint). */
+ REQ_INITIAL_FRAME = '0', /* no args */
+ /* Set current frame to bottom frame (the one currently executing).
+ Reply is stack offset and current pc. */
+ REQ_GET_FRAME = 'f', /* no args */
+ /* Return current frame location (stack offset + current pc). */
+ REQ_SET_FRAME = 'S', /* uint32_t stack_offset */
+ /* Set current frame to given stack offset. No reply. */
+ REQ_UP_FRAME = 'U', /* uint32_t n */
+ /* Move one frame up. Argument n is size of current frame (in words).
+ Reply is stack offset and current pc, or -1 if top of stack reached. */
+ REQ_SET_TRAP_BARRIER = 'b', /* uint32_t offset */
+ /* Set the trap barrier at the given offset. */
+ REQ_GET_LOCAL = 'L', /* uint32_t slot_number */
+ /* Return the local variable at the given slot in the current frame.
+ Reply is one value. */
+ REQ_GET_ENVIRONMENT = 'E', /* uint32_t slot_number */
+ /* Return the local variable at the given slot in the heap environment
+ of the current frame. Reply is one value. */
+ REQ_GET_GLOBAL = 'G', /* uint32_t global_number */
+ /* Return the specified global variable. Reply is one value. */
+ REQ_GET_ACCU = 'A', /* no args */
+ /* Return the current contents of the accumulator. Reply is one value. */
+ REQ_GET_HEADER = 'H', /* mlvalue v */
+ /* As REQ_GET_OBJ, but sends only the header. */
+ REQ_GET_FIELD = 'F', /* mlvalue v, uint32_t fieldnum */
+ /* As REQ_GET_OBJ, but sends only one field. */
+ REQ_MARSHAL_OBJ = 'M', /* mlvalue v */
+ /* Send a copy of the data structure rooted at v, using the same
+ format as [caml_output_value]. */
+ REQ_GET_CLOSURE_CODE = 'C', /* mlvalue v */
+ /* Send the code address of the given closure.
+ Reply is one uint32_t. */
+ REQ_SET_FORK_MODE = 'K' /* uint32_t m */
+ /* Set whether to follow the child (m=0) or the parent on fork. */
+};
+
+/* Replies to a REQ_GO request. All replies are followed by three uint32_t:
+ - the value of the event counter
+ - the position of the stack
+ - the current pc. */
+
+enum debugger_reply {
+ REP_EVENT = 'e',
+ /* Event counter reached 0. */
+ REP_BREAKPOINT = 'b',
+ /* Breakpoint hit. */
+ REP_EXITED = 'x',
+ /* Program exited by calling exit or reaching the end of the source. */
+ REP_TRAP = 's',
+ /* Trap barrier crossed. */
+ REP_UNCAUGHT_EXC = 'u'
+ /* Program exited due to a stray exception. */
+};
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_DEBUGGER_H */
diff --git a/test/monniaux/ocaml/byterun/caml/dynlink.h b/test/monniaux/ocaml/byterun/caml/dynlink.h
new file mode 100644
index 00000000..92f4e235
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/dynlink.h
@@ -0,0 +1,46 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Dynamic loading of C primitives. */
+
+#ifndef CAML_DYNLINK_H
+#define CAML_DYNLINK_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+
+/* Build the table of primitives, given a search path, a list
+ of shared libraries, and a list of primitive names
+ (all three 0-separated in char arrays).
+ Abort the runtime system on error.
+ Calling this frees caml_shared_libs_path (not touching its contents). */
+extern void caml_build_primitive_table(char_os * lib_path,
+ char_os * libs,
+ char * req_prims);
+
+/* The search path for shared libraries */
+extern struct ext_table caml_shared_libs_path;
+
+/* Build the table of primitives as a copy of the builtin primitive table.
+ Used for executables generated by ocamlc -output-obj. */
+extern void caml_build_primitive_table_builtin(void);
+
+/* Unload all the previously loaded shared libraries */
+extern void caml_free_shared_libs(void);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_DYNLINK_H */
diff --git a/test/monniaux/ocaml/byterun/caml/exec.h b/test/monniaux/ocaml/byterun/caml/exec.h
new file mode 100644
index 00000000..38ab7ae8
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/exec.h
@@ -0,0 +1,65 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* exec.h : format of executable bytecode files */
+
+#ifndef CAML_EXEC_H
+#define CAML_EXEC_H
+
+#ifdef CAML_INTERNALS
+
+/* Executable bytecode files are composed of a number of sections,
+ identified by 4-character names. A table of contents at the
+ end of the file lists the section names along with their sizes,
+ in the order in which they appear in the file:
+
+ offset 0 ---> initial junk
+ data for section 1
+ data for section 2
+ ...
+ data for section N
+ table of contents:
+ descriptor for section 1
+ ...
+ descriptor for section N
+ trailer
+ end of file --->
+*/
+
+/* Structure of t.o.c. entries
+ Numerical quantities are 32-bit unsigned integers, big endian */
+
+struct section_descriptor {
+ char name[4]; /* Section name */
+ uint32_t len; /* Length of data in bytes */
+};
+
+/* Structure of the trailer. */
+
+struct exec_trailer {
+ uint32_t num_sections; /* Number of sections */
+ char magic[12]; /* The magic number */
+ struct section_descriptor * section; /* Not part of file */
+};
+
+#define TRAILER_SIZE (4+12)
+
+/* Magic number for this release */
+
+#define EXEC_MAGIC "Caml1999X023"
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_EXEC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/fail.h b/test/monniaux/ocaml/byterun/caml/fail.h
new file mode 100644
index 00000000..54907e42
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/fail.h
@@ -0,0 +1,144 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_FAIL_H
+#define CAML_FAIL_H
+
+#ifdef CAML_INTERNALS
+#include <setjmp.h>
+#endif /* CAML_INTERNALS */
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef CAML_INTERNALS
+#define OUT_OF_MEMORY_EXN 0 /* "Out_of_memory" */
+#define SYS_ERROR_EXN 1 /* "Sys_error" */
+#define FAILURE_EXN 2 /* "Failure" */
+#define INVALID_EXN 3 /* "Invalid_argument" */
+#define END_OF_FILE_EXN 4 /* "End_of_file" */
+#define ZERO_DIVIDE_EXN 5 /* "Division_by_zero" */
+#define NOT_FOUND_EXN 6 /* "Not_found" */
+#define MATCH_FAILURE_EXN 7 /* "Match_failure" */
+#define STACK_OVERFLOW_EXN 8 /* "Stack_overflow" */
+#define SYS_BLOCKED_IO 9 /* "Sys_blocked_io" */
+#define ASSERT_FAILURE_EXN 10 /* "Assert_failure" */
+#define UNDEFINED_RECURSIVE_MODULE_EXN 11 /* "Undefined_recursive_module" */
+
+#ifdef POSIX_SIGNALS
+struct longjmp_buffer {
+ sigjmp_buf buf;
+};
+#elif defined(__MINGW64__) && defined(__GNUC__) && __GNUC__ >= 4
+/* MPR#7638: issues with setjmp/longjmp in Mingw64, use GCC builtins instead */
+struct longjmp_buffer {
+ intptr_t buf[5];
+};
+#define sigsetjmp(buf,save) __builtin_setjmp(buf)
+#define siglongjmp(buf,val) __builtin_longjmp(buf,val)
+#else
+struct longjmp_buffer {
+ jmp_buf buf;
+};
+#define sigsetjmp(buf,save) setjmp(buf)
+#define siglongjmp(buf,val) longjmp(buf,val)
+#endif
+
+CAMLextern struct longjmp_buffer * caml_external_raise;
+extern value caml_exn_bucket;
+int caml_is_special_exception(value exn);
+
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLnoreturn_start
+CAMLextern void caml_raise (value bucket)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_constant (value tag)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_with_arg (value tag, value arg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_with_args (value tag, int nargs, value arg[])
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_with_string (value tag, char const * msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_failwith (char const *msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_failwith_value (value msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_invalid_argument (char const *msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_invalid_argument_value (value msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_out_of_memory (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_stack_overflow (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_sys_error (value)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_end_of_file (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_zero_divide (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_not_found (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_array_bound_error (void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_raise_sys_blocked_io (void)
+CAMLnoreturn_end;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_FAIL_H */
diff --git a/test/monniaux/ocaml/byterun/caml/finalise.h b/test/monniaux/ocaml/byterun/caml/finalise.h
new file mode 100644
index 00000000..b2052c21
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/finalise.h
@@ -0,0 +1,36 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Moscova, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_FINALISE_H
+#define CAML_FINALISE_H
+
+#ifdef CAML_INTERNALS
+
+#include "roots.h"
+
+void caml_final_update_mark_phase (void);
+void caml_final_update_clean_phase (void);
+void caml_final_do_calls (void);
+void caml_final_do_roots (scanning_action f);
+void caml_final_invert_finalisable_values ();
+void caml_final_oldify_young_roots (void);
+void caml_final_empty_young (void);
+void caml_final_update_minor_roots(void);
+value caml_final_register (value f, value v);
+void caml_final_invariant_check(void);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_FINALISE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/fix_code.h b/test/monniaux/ocaml/byterun/caml/fix_code.h
new file mode 100644
index 00000000..7e5633d6
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/fix_code.h
@@ -0,0 +1,45 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Handling of blocks of bytecode (endianness switch, threading). */
+
+#ifndef CAML_FIX_CODE_H
+#define CAML_FIX_CODE_H
+
+#ifdef CAML_INTERNALS
+
+#include "config.h"
+#include "misc.h"
+#include "mlvalues.h"
+
+extern code_t caml_start_code;
+extern asize_t caml_code_size;
+extern unsigned char * caml_saved_code;
+
+void caml_init_code_fragments(void);
+void caml_load_code (int fd, asize_t len);
+void caml_fixup_endianness (code_t code, asize_t len);
+void caml_set_instruction (code_t pos, opcode_t instr);
+int caml_is_instruction (opcode_t instr1, opcode_t instr2);
+
+#ifdef THREADED_CODE
+extern char ** caml_instr_table;
+extern char * caml_instr_base;
+void caml_thread_code (code_t code, asize_t len);
+#endif
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_FIX_CODE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/freelist.h b/test/monniaux/ocaml/byterun/caml/freelist.h
new file mode 100644
index 00000000..54e0e822
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/freelist.h
@@ -0,0 +1,38 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Free lists of heap blocks. */
+
+#ifndef CAML_FREELIST_H
+#define CAML_FREELIST_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "mlvalues.h"
+
+extern asize_t caml_fl_cur_wsz;
+
+header_t *caml_fl_allocate (mlsize_t wo_sz);
+void caml_fl_init_merge (void);
+void caml_fl_reset (void);
+header_t *caml_fl_merge_block (value);
+void caml_fl_add_blocks (value);
+void caml_make_free_blocks (value *, mlsize_t wsz, int, int);
+void caml_set_allocation_policy (uintnat);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_FREELIST_H */
diff --git a/test/monniaux/ocaml/byterun/caml/gc.h b/test/monniaux/ocaml/byterun/caml/gc.h
new file mode 100644
index 00000000..a646a756
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/gc.h
@@ -0,0 +1,79 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_GC_H
+#define CAML_GC_H
+
+
+#include "mlvalues.h"
+
+#define Caml_white (0 << 8)
+#define Caml_gray (1 << 8)
+#define Caml_blue (2 << 8)
+#define Caml_black (3 << 8)
+
+#define Color_hd(hd) ((color_t) ((hd) & Caml_black))
+#define Color_hp(hp) (Color_hd (Hd_hp (hp)))
+#define Color_val(val) (Color_hd (Hd_val (val)))
+
+#define Is_white_hd(hd) (Color_hd (hd) == Caml_white)
+#define Is_gray_hd(hd) (Color_hd (hd) == Caml_gray)
+#define Is_blue_hd(hd) (Color_hd (hd) == Caml_blue)
+#define Is_black_hd(hd) (Color_hd (hd) == Caml_black)
+
+#define Whitehd_hd(hd) (((hd) & ~Caml_black)/*| Caml_white*/)
+#define Grayhd_hd(hd) (((hd) & ~Caml_black) | Caml_gray)
+#define Blackhd_hd(hd) (((hd)/*& ~Caml_black*/)| Caml_black)
+#define Bluehd_hd(hd) (((hd) & ~Caml_black) | Caml_blue)
+
+/* This depends on the layout of the header. See [mlvalues.h]. */
+#define Make_header(wosize, tag, color) \
+ (/*CAMLassert ((wosize) <= Max_wosize),*/ \
+ ((header_t) (((header_t) (wosize) << 10) \
+ + (color) \
+ + (tag_t) (tag))) \
+ )
+
+#ifdef WITH_PROFINFO
+#define Make_header_with_profinfo(wosize, tag, color, profinfo) \
+ (Make_header(wosize, tag, color) \
+ | ((((intnat) profinfo) & PROFINFO_MASK) << PROFINFO_SHIFT) \
+ )
+#else
+#define Make_header_with_profinfo(wosize, tag, color, profinfo) \
+ Make_header(wosize, tag, color)
+#endif
+
+#ifdef WITH_SPACETIME
+struct ext_table;
+extern uintnat caml_spacetime_my_profinfo(struct ext_table**, uintnat);
+#define Make_header_allocated_here(wosize, tag, color) \
+ (Make_header_with_profinfo(wosize, tag, color, \
+ caml_spacetime_my_profinfo(NULL, wosize)) \
+ )
+#else
+#define Make_header_allocated_here Make_header
+#endif
+
+#define Is_white_val(val) (Color_val(val) == Caml_white)
+#define Is_gray_val(val) (Color_val(val) == Caml_gray)
+#define Is_blue_val(val) (Color_val(val) == Caml_blue)
+#define Is_black_val(val) (Color_val(val) == Caml_black)
+
+/* For extern.c */
+#define Colornum_hd(hd) ((color_t) (((hd) >> 8) & 3))
+#define Coloredhd_hd(hd,colnum) (((hd) & ~Caml_black) | ((colnum) << 8))
+
+#endif /* CAML_GC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/gc_ctrl.h b/test/monniaux/ocaml/byterun/caml/gc_ctrl.h
new file mode 100644
index 00000000..ebf1a40b
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/gc_ctrl.h
@@ -0,0 +1,58 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_GC_CTRL_H
+#define CAML_GC_CTRL_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+
+extern double
+ caml_stat_minor_words,
+ caml_stat_promoted_words,
+ caml_stat_major_words;
+
+extern intnat
+ caml_stat_minor_collections,
+ caml_stat_major_collections,
+ caml_stat_heap_wsz,
+ caml_stat_top_heap_wsz,
+ caml_stat_compactions,
+ caml_stat_heap_chunks;
+
+uintnat caml_normalize_heap_increment (uintnat);
+
+/*
+ minor_size: cf. minor_heap_size in gc.mli
+ major_size: Size in words of the initial major heap
+ major_incr: cf. major_heap_increment in gc.mli
+ percent_fr: cf. space_overhead in gc.mli
+ percent_m : cf. max_overhead in gc.mli
+ window : cf. window_size in gc.mli
+*/
+void caml_init_gc (uintnat minor_size, uintnat major_size, uintnat major_incr,
+ uintnat percent_fr, uintnat percent_m, uintnat window);
+
+
+CAMLextern value caml_gc_stat(value v);
+
+#ifdef DEBUG
+void caml_heap_check (void);
+#endif
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_GC_CTRL_H */
diff --git a/test/monniaux/ocaml/byterun/caml/globroots.h b/test/monniaux/ocaml/byterun/caml/globroots.h
new file mode 100644
index 00000000..10fe66f5
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/globroots.h
@@ -0,0 +1,31 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Registration of global memory roots */
+
+#ifndef CAML_GLOBROOTS_H
+#define CAML_GLOBROOTS_H
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+#include "roots.h"
+
+void caml_scan_global_roots(scanning_action f);
+void caml_scan_global_young_roots(scanning_action f);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_GLOBROOTS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/hash.h b/test/monniaux/ocaml/byterun/caml/hash.h
new file mode 100644
index 00000000..fcc7589f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/hash.h
@@ -0,0 +1,39 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Gallium, INRIA Rocquencourt */
+/* */
+/* Copyright 2011 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Auxiliary functions for custom hash functions */
+
+#ifndef CAML_HASH_H
+#define CAML_HASH_H
+
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLextern uint32_t caml_hash_mix_uint32(uint32_t h, uint32_t d);
+CAMLextern uint32_t caml_hash_mix_intnat(uint32_t h, intnat d);
+CAMLextern uint32_t caml_hash_mix_int64(uint32_t h, int64_t d);
+CAMLextern uint32_t caml_hash_mix_double(uint32_t h, double d);
+CAMLextern uint32_t caml_hash_mix_float(uint32_t h, float d);
+CAMLextern uint32_t caml_hash_mix_string(uint32_t h, value s);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* CAML_HASH_H */
diff --git a/test/monniaux/ocaml/byterun/caml/hooks.h b/test/monniaux/ocaml/byterun/caml/hooks.h
new file mode 100644
index 00000000..5877dd25
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/hooks.h
@@ -0,0 +1,42 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Fabrice Le Fessant, INRIA de Paris */
+/* */
+/* Copyright 2016 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_HOOKS_H
+#define CAML_HOOKS_H
+
+#include "misc.h"
+#include "memory.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef CAML_INTERNALS
+
+#ifdef NATIVE_CODE
+
+/* executed just before calling the entry point of a dynamically
+ loaded native code module. */
+CAMLextern void (*caml_natdynlink_hook)(void* handle, const char* unit);
+
+#endif /* NATIVE_CODE */
+
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_HOOKS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/instrtrace.h b/test/monniaux/ocaml/byterun/caml/instrtrace.h
new file mode 100644
index 00000000..2e42a80a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/instrtrace.h
@@ -0,0 +1,35 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Trace the instructions executed */
+
+#ifndef _instrtrace_
+#define _instrtrace_
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+#include "misc.h"
+
+extern intnat caml_icount;
+void caml_stop_here (void);
+void caml_disasm_instr (code_t pc);
+void caml_trace_value_file (value v, code_t prog, int proglen, FILE * f);
+void caml_trace_accu_sp_file(value accu, value * sp, code_t prog, int proglen,
+ FILE * f);
+
+#endif /* CAML_INTERNALS */
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/caml/instruct.h b/test/monniaux/ocaml/byterun/caml/instruct.h
new file mode 100644
index 00000000..5c10df4f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/instruct.h
@@ -0,0 +1,68 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* The instruction set. */
+
+#ifndef CAML_INSTRUCT_H
+#define CAML_INSTRUCT_H
+
+#ifdef CAML_INTERNALS
+
+enum instructions {
+ ACC0, ACC1, ACC2, ACC3, ACC4, ACC5, ACC6, ACC7,
+ ACC, PUSH,
+ PUSHACC0, PUSHACC1, PUSHACC2, PUSHACC3,
+ PUSHACC4, PUSHACC5, PUSHACC6, PUSHACC7,
+ PUSHACC, POP, ASSIGN,
+ ENVACC1, ENVACC2, ENVACC3, ENVACC4, ENVACC,
+ PUSHENVACC1, PUSHENVACC2, PUSHENVACC3, PUSHENVACC4, PUSHENVACC,
+ PUSH_RETADDR, APPLY, APPLY1, APPLY2, APPLY3,
+ APPTERM, APPTERM1, APPTERM2, APPTERM3,
+ RETURN, RESTART, GRAB,
+ CLOSURE, CLOSUREREC,
+ OFFSETCLOSUREM2, OFFSETCLOSURE0, OFFSETCLOSURE2, OFFSETCLOSURE,
+ PUSHOFFSETCLOSUREM2, PUSHOFFSETCLOSURE0,
+ PUSHOFFSETCLOSURE2, PUSHOFFSETCLOSURE,
+ GETGLOBAL, PUSHGETGLOBAL, GETGLOBALFIELD, PUSHGETGLOBALFIELD, SETGLOBAL,
+ ATOM0, ATOM, PUSHATOM0, PUSHATOM,
+ MAKEBLOCK, MAKEBLOCK1, MAKEBLOCK2, MAKEBLOCK3, MAKEFLOATBLOCK,
+ GETFIELD0, GETFIELD1, GETFIELD2, GETFIELD3, GETFIELD, GETFLOATFIELD,
+ SETFIELD0, SETFIELD1, SETFIELD2, SETFIELD3, SETFIELD, SETFLOATFIELD,
+ VECTLENGTH, GETVECTITEM, SETVECTITEM,
+ GETBYTESCHAR, SETBYTESCHAR,
+ BRANCH, BRANCHIF, BRANCHIFNOT, SWITCH, BOOLNOT,
+ PUSHTRAP, POPTRAP, RAISE,
+ CHECK_SIGNALS,
+ C_CALL1, C_CALL2, C_CALL3, C_CALL4, C_CALL5, C_CALLN,
+ CONST0, CONST1, CONST2, CONST3, CONSTINT,
+ PUSHCONST0, PUSHCONST1, PUSHCONST2, PUSHCONST3, PUSHCONSTINT,
+ NEGINT, ADDINT, SUBINT, MULINT, DIVINT, MODINT,
+ ANDINT, ORINT, XORINT, LSLINT, LSRINT, ASRINT,
+ EQ, NEQ, LTINT, LEINT, GTINT, GEINT,
+ OFFSETINT, OFFSETREF, ISINT,
+ GETMETHOD,
+ BEQ, BNEQ, BLTINT, BLEINT, BGTINT, BGEINT,
+ ULTINT, UGEINT,
+ BULTINT, BUGEINT,
+ GETPUBMET, GETDYNMET,
+ STOP,
+ EVENT, BREAK,
+ RERAISE, RAISE_NOTRACE,
+ GETSTRINGCHAR,
+FIRST_UNIMPLEMENTED_OP};
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_INSTRUCT_H */
diff --git a/test/monniaux/ocaml/byterun/caml/int64_emul.h b/test/monniaux/ocaml/byterun/caml/int64_emul.h
new file mode 100644
index 00000000..c1cddcc0
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/int64_emul.h
@@ -0,0 +1,293 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2002 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Software emulation of 64-bit integer arithmetic, for C compilers
+ that do not support it. */
+
+#ifndef CAML_INT64_EMUL_H
+#define CAML_INT64_EMUL_H
+
+#ifdef CAML_INTERNALS
+
+#include <math.h>
+
+#ifdef ARCH_BIG_ENDIAN
+#define I64_literal(hi,lo) { hi, lo }
+#else
+#define I64_literal(hi,lo) { lo, hi }
+#endif
+
+#define I64_split(x,hi,lo) (hi = (x).h, lo = (x).l)
+
+/* Unsigned comparison */
+static int I64_ucompare(uint64_t x, uint64_t y)
+{
+ if (x.h > y.h) return 1;
+ if (x.h < y.h) return -1;
+ if (x.l > y.l) return 1;
+ if (x.l < y.l) return -1;
+ return 0;
+}
+
+#define I64_ult(x, y) (I64_ucompare(x, y) < 0)
+
+/* Signed comparison */
+static int I64_compare(int64_t x, int64_t y)
+{
+ if ((int32_t)x.h > (int32_t)y.h) return 1;
+ if ((int32_t)x.h < (int32_t)y.h) return -1;
+ if (x.l > y.l) return 1;
+ if (x.l < y.l) return -1;
+ return 0;
+}
+
+/* Negation */
+static int64_t I64_neg(int64_t x)
+{
+ int64_t res;
+ res.l = -x.l;
+ res.h = ~x.h;
+ if (res.l == 0) res.h++;
+ return res;
+}
+
+/* Addition */
+static int64_t I64_add(int64_t x, int64_t y)
+{
+ int64_t res;
+ res.l = x.l + y.l;
+ res.h = x.h + y.h;
+ if (res.l < x.l) res.h++;
+ return res;
+}
+
+/* Subtraction */
+static int64_t I64_sub(int64_t x, int64_t y)
+{
+ int64_t res;
+ res.l = x.l - y.l;
+ res.h = x.h - y.h;
+ if (x.l < y.l) res.h--;
+ return res;
+}
+
+/* Multiplication */
+static int64_t I64_mul(int64_t x, int64_t y)
+{
+ int64_t res;
+ uint32_t prod00 = (x.l & 0xFFFF) * (y.l & 0xFFFF);
+ uint32_t prod10 = (x.l >> 16) * (y.l & 0xFFFF);
+ uint32_t prod01 = (x.l & 0xFFFF) * (y.l >> 16);
+ uint32_t prod11 = (x.l >> 16) * (y.l >> 16);
+ res.l = prod00;
+ res.h = prod11 + (prod01 >> 16) + (prod10 >> 16);
+ prod01 = prod01 << 16; res.l += prod01; if (res.l < prod01) res.h++;
+ prod10 = prod10 << 16; res.l += prod10; if (res.l < prod10) res.h++;
+ res.h += x.l * y.h + x.h * y.l;
+ return res;
+}
+
+#define I64_is_zero(x) (((x).l | (x).h) == 0)
+#define I64_is_negative(x) ((int32_t) (x).h < 0)
+#define I64_is_min_int(x) ((x).l == 0 && (x).h == 0x80000000U)
+#define I64_is_minus_one(x) (((x).l & (x).h) == 0xFFFFFFFFU)
+
+/* Bitwise operations */
+static int64_t I64_and(int64_t x, int64_t y)
+{
+ int64_t res;
+ res.l = x.l & y.l;
+ res.h = x.h & y.h;
+ return res;
+}
+
+static int64_t I64_or(int64_t x, int64_t y)
+{
+ int64_t res;
+ res.l = x.l | y.l;
+ res.h = x.h | y.h;
+ return res;
+}
+
+static int64_t I64_xor(int64_t x, int64_t y)
+{
+ int64_t res;
+ res.l = x.l ^ y.l;
+ res.h = x.h ^ y.h;
+ return res;
+}
+
+/* Shifts */
+static int64_t I64_lsl(int64_t x, int s)
+{
+ int64_t res;
+ s = s & 63;
+ if (s == 0) return x;
+ if (s < 32) {
+ res.l = x.l << s;
+ res.h = (x.h << s) | (x.l >> (32 - s));
+ } else {
+ res.l = 0;
+ res.h = x.l << (s - 32);
+ }
+ return res;
+}
+
+static int64_t I64_lsr(int64_t x, int s)
+{
+ int64_t res;
+ s = s & 63;
+ if (s == 0) return x;
+ if (s < 32) {
+ res.l = (x.l >> s) | (x.h << (32 - s));
+ res.h = x.h >> s;
+ } else {
+ res.l = x.h >> (s - 32);
+ res.h = 0;
+ }
+ return res;
+}
+
+static int64_t I64_asr(int64_t x, int s)
+{
+ int64_t res;
+ s = s & 63;
+ if (s == 0) return x;
+ if (s < 32) {
+ res.l = (x.l >> s) | (x.h << (32 - s));
+ res.h = (int32_t) x.h >> s;
+ } else {
+ res.l = (int32_t) x.h >> (s - 32);
+ res.h = (int32_t) x.h >> 31;
+ }
+ return res;
+}
+
+/* Division and modulus */
+
+#define I64_SHL1(x) x.h = (x.h << 1) | (x.l >> 31); x.l <<= 1
+#define I64_SHR1(x) x.l = (x.l >> 1) | (x.h << 31); x.h >>= 1
+
+static void I64_udivmod(uint64_t modulus, uint64_t divisor,
+ uint64_t * quo, uint64_t * mod)
+{
+ int64_t quotient, mask;
+ int cmp;
+
+ quotient.h = 0; quotient.l = 0;
+ mask.h = 0; mask.l = 1;
+ while ((int32_t) divisor.h >= 0) {
+ cmp = I64_ucompare(divisor, modulus);
+ I64_SHL1(divisor);
+ I64_SHL1(mask);
+ if (cmp >= 0) break;
+ }
+ while (mask.l | mask.h) {
+ if (I64_ucompare(modulus, divisor) >= 0) {
+ quotient.h |= mask.h; quotient.l |= mask.l;
+ modulus = I64_sub(modulus, divisor);
+ }
+ I64_SHR1(mask);
+ I64_SHR1(divisor);
+ }
+ *quo = quotient;
+ *mod = modulus;
+}
+
+static int64_t I64_div(int64_t x, int64_t y)
+{
+ int64_t q, r;
+ int32_t sign;
+
+ sign = x.h ^ y.h;
+ if ((int32_t) x.h < 0) x = I64_neg(x);
+ if ((int32_t) y.h < 0) y = I64_neg(y);
+ I64_udivmod(x, y, &q, &r);
+ if (sign < 0) q = I64_neg(q);
+ return q;
+}
+
+static int64_t I64_mod(int64_t x, int64_t y)
+{
+ int64_t q, r;
+ int32_t sign;
+
+ sign = x.h;
+ if ((int32_t) x.h < 0) x = I64_neg(x);
+ if ((int32_t) y.h < 0) y = I64_neg(y);
+ I64_udivmod(x, y, &q, &r);
+ if (sign < 0) r = I64_neg(r);
+ return r;
+}
+
+/* Coercions */
+
+static int64_t I64_of_int32(int32_t x)
+{
+ int64_t res;
+ res.l = x;
+ res.h = x >> 31;
+ return res;
+}
+
+#define I64_to_int32(x) ((int32_t) (x).l)
+
+/* Note: we assume sizeof(intnat) = 4 here, which is true otherwise
+ autoconfiguration would have selected native 64-bit integers */
+#define I64_of_intnat I64_of_int32
+#define I64_to_intnat I64_to_int32
+
+static double I64_to_double(int64_t x)
+{
+ double res;
+ int32_t sign = x.h;
+ if (sign < 0) x = I64_neg(x);
+ res = ldexp((double) x.h, 32) + x.l;
+ if (sign < 0) res = -res;
+ return res;
+}
+
+static int64_t I64_of_double(double f)
+{
+ int64_t res;
+ double frac, integ;
+ int neg;
+
+ neg = (f < 0);
+ f = fabs(f);
+ frac = modf(ldexp(f, -32), &integ);
+ res.h = (uint32_t) integ;
+ res.l = (uint32_t) ldexp(frac, 32);
+ if (neg) res = I64_neg(res);
+ return res;
+}
+
+static int64_t I64_bswap(int64_t x)
+{
+ int64_t res;
+ res.h = (((x.l & 0x000000FF) << 24) |
+ ((x.l & 0x0000FF00) << 8) |
+ ((x.l & 0x00FF0000) >> 8) |
+ ((x.l & 0xFF000000) >> 24));
+ res.l = (((x.h & 0x000000FF) << 24) |
+ ((x.h & 0x0000FF00) << 8) |
+ ((x.h & 0x00FF0000) >> 8) |
+ ((x.h & 0xFF000000) >> 24));
+ return res;
+}
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_INT64_EMUL_H */
diff --git a/test/monniaux/ocaml/byterun/caml/int64_format.h b/test/monniaux/ocaml/byterun/caml/int64_format.h
new file mode 100644
index 00000000..40250ed9
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/int64_format.h
@@ -0,0 +1,111 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2002 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* printf-like formatting of 64-bit integers, in case the C library
+ printf() function does not support them. */
+
+#ifndef CAML_INT64_FORMAT_H
+#define CAML_INT64_FORMAT_H
+
+#ifdef CAML_INTERNALS
+
+static void I64_format(char * buffer, char * fmt, int64_t x)
+{
+ static char conv_lower[] = "0123456789abcdef";
+ static char conv_upper[] = "0123456789ABCDEF";
+ char rawbuffer[24];
+ char justify, signstyle, filler, alternate, signedconv;
+ int base, width, sign, i, rawlen;
+ char * cvtbl;
+ char * p, * r;
+ int64_t wbase, digit;
+
+ /* Parsing of format */
+ justify = '+';
+ signstyle = '-';
+ filler = ' ';
+ alternate = 0;
+ base = 0;
+ signedconv = 0;
+ width = 0;
+ cvtbl = conv_lower;
+ for (p = fmt; *p != 0; p++) {
+ switch (*p) {
+ case '-':
+ justify = '-'; break;
+ case '+': case ' ':
+ signstyle = *p; break;
+ case '0':
+ filler = '0'; break;
+ case '#':
+ alternate = 1; break;
+ case '1': case '2': case '3': case '4': case '5':
+ case '6': case '7': case '8': case '9':
+ width = atoi(p);
+ while (p[1] >= '0' && p[1] <= '9') p++;
+ break;
+ case 'd': case 'i':
+ signedconv = 1; /* fallthrough */
+ case 'u':
+ base = 10; break;
+ case 'x':
+ base = 16; break;
+ case 'X':
+ base = 16; cvtbl = conv_upper; break;
+ case 'o':
+ base = 8; break;
+ }
+ }
+ if (base == 0) { buffer[0] = 0; return; }
+ /* Do the conversion */
+ sign = 1;
+ if (signedconv && I64_is_negative(x)) { sign = -1; x = I64_neg(x); }
+ r = rawbuffer + sizeof(rawbuffer);
+ wbase = I64_of_int32(base);
+ do {
+ I64_udivmod(x, wbase, &x, &digit);
+ *--r = cvtbl[I64_to_int32(digit)];
+ } while (! I64_is_zero(x));
+ rawlen = rawbuffer + sizeof(rawbuffer) - r;
+ /* Adjust rawlen to reflect additional chars (sign, etc) */
+ if (signedconv && (sign < 0 || signstyle != '-')) rawlen++;
+ if (alternate) {
+ if (base == 8) rawlen += 1;
+ if (base == 16) rawlen += 2;
+ }
+ /* Do the formatting */
+ p = buffer;
+ if (justify == '+' && filler == ' ') {
+ for (i = rawlen; i < width; i++) *p++ = ' ';
+ }
+ if (signedconv) {
+ if (sign < 0) *p++ = '-';
+ else if (signstyle != '-') *p++ = signstyle;
+ }
+ if (alternate && base == 8) *p++ = '0';
+ if (alternate && base == 16) { *p++ = '0'; *p++ = 'x'; }
+ if (justify == '+' && filler == '0') {
+ for (i = rawlen; i < width; i++) *p++ = '0';
+ }
+ while (r < rawbuffer + sizeof(rawbuffer)) *p++ = *r++;
+ if (justify == '-') {
+ for (i = rawlen; i < width; i++) *p++ = ' ';
+ }
+ *p = 0;
+}
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_INT64_FORMAT_H */
diff --git a/test/monniaux/ocaml/byterun/caml/int64_native.h b/test/monniaux/ocaml/byterun/caml/int64_native.h
new file mode 100644
index 00000000..7df66511
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/int64_native.h
@@ -0,0 +1,67 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2002 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Wrapper macros around native 64-bit integer arithmetic,
+ so that it has the same interface as the software emulation
+ provided in int64_emul.h */
+
+#ifndef CAML_INT64_NATIVE_H
+#define CAML_INT64_NATIVE_H
+
+#ifdef CAML_INTERNALS
+
+#define I64_literal(hi,lo) ((int64_t)(hi) << 32 | (lo))
+#define I64_split(x,hi,lo) (hi = (uint32_t)((x)>>32), lo = (uint32_t)(x))
+#define I64_compare(x,y) (((x) > (y)) - ((x) < (y)))
+#define I64_ult(x,y) ((uint64_t)(x) < (uint64_t)(y))
+#define I64_neg(x) (-(x))
+#define I64_add(x,y) ((x) + (y))
+#define I64_sub(x,y) ((x) - (y))
+#define I64_mul(x,y) ((x) * (y))
+#define I64_is_zero(x) ((x) == 0)
+#define I64_is_negative(x) ((x) < 0)
+#define I64_is_min_int(x) ((x) == ((int64_t)1 << 63))
+#define I64_is_minus_one(x) ((x) == -1)
+
+#define I64_div(x,y) ((x) / (y))
+#define I64_mod(x,y) ((x) % (y))
+#define I64_udivmod(x,y,quo,rem) \
+ (*(rem) = (uint64_t)(x) % (uint64_t)(y), \
+ *(quo) = (uint64_t)(x) / (uint64_t)(y))
+#define I64_and(x,y) ((x) & (y))
+#define I64_or(x,y) ((x) | (y))
+#define I64_xor(x,y) ((x) ^ (y))
+#define I64_lsl(x,y) ((x) << (y))
+#define I64_asr(x,y) ((x) >> (y))
+#define I64_lsr(x,y) ((uint64_t)(x) >> (y))
+#define I64_to_intnat(x) ((intnat) (x))
+#define I64_of_intnat(x) ((intnat) (x))
+#define I64_to_int32(x) ((int32_t) (x))
+#define I64_of_int32(x) ((int64_t) (x))
+#define I64_to_double(x) ((double)(x))
+#define I64_of_double(x) ((int64_t)(x))
+
+#define I64_bswap(x) ((((x) & 0x00000000000000FFULL) << 56) | \
+ (((x) & 0x000000000000FF00ULL) << 40) | \
+ (((x) & 0x0000000000FF0000ULL) << 24) | \
+ (((x) & 0x00000000FF000000ULL) << 8) | \
+ (((x) & 0x000000FF00000000ULL) >> 8) | \
+ (((x) & 0x0000FF0000000000ULL) >> 24) | \
+ (((x) & 0x00FF000000000000ULL) >> 40) | \
+ (((x) & 0xFF00000000000000ULL) >> 56))
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_INT64_NATIVE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/interp.h b/test/monniaux/ocaml/byterun/caml/interp.h
new file mode 100644
index 00000000..d1ebdc01
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/interp.h
@@ -0,0 +1,37 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* The bytecode interpreter */
+
+#ifndef CAML_INTERP_H
+#define CAML_INTERP_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "mlvalues.h"
+
+/* interpret a bytecode */
+value caml_interprete (code_t prog, asize_t prog_size);
+
+/* tell the runtime that a bytecode program might be needed */
+void caml_prepare_bytecode(code_t prog, asize_t prog_size);
+
+/* tell the runtime that a bytecode program is no more needed */
+void caml_release_bytecode(code_t prog, asize_t prog_size);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_INTERP_H */
diff --git a/test/monniaux/ocaml/byterun/caml/intext.h b/test/monniaux/ocaml/byterun/caml/intext.h
new file mode 100644
index 00000000..f67c98b5
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/intext.h
@@ -0,0 +1,207 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Structured input/output */
+
+#ifndef CAML_INTEXT_H
+#define CAML_INTEXT_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef CAML_INTERNALS
+#include "io.h"
+
+/* Magic number */
+
+#define Intext_magic_number_small 0x8495A6BE
+#define Intext_magic_number_big 0x8495A6BF
+
+/* Header format for the "small" model: 20 bytes
+ 0 "small" magic number
+ 4 length of marshaled data, in bytes
+ 8 number of shared blocks
+ 12 size in words when read on a 32-bit platform
+ 16 size in words when read on a 64-bit platform
+ The 4 numbers are 32 bits each, in big endian.
+
+ Header format for the "big" model: 32 bytes
+ 0 "big" magic number
+ 4 four reserved bytes, currently set to 0
+ 8 length of marshaled data, in bytes
+ 16 number of shared blocks
+ 24 size in words when read on a 64-bit platform
+ The 3 numbers are 64 bits each, in big endian.
+*/
+
+/* Codes for the compact format */
+
+#define PREFIX_SMALL_BLOCK 0x80
+#define PREFIX_SMALL_INT 0x40
+#define PREFIX_SMALL_STRING 0x20
+#define CODE_INT8 0x0
+#define CODE_INT16 0x1
+#define CODE_INT32 0x2
+#define CODE_INT64 0x3
+#define CODE_SHARED8 0x4
+#define CODE_SHARED16 0x5
+#define CODE_SHARED32 0x6
+#define CODE_SHARED64 0x14
+#define CODE_BLOCK32 0x8
+#define CODE_BLOCK64 0x13
+#define CODE_STRING8 0x9
+#define CODE_STRING32 0xA
+#define CODE_STRING64 0x15
+#define CODE_DOUBLE_BIG 0xB
+#define CODE_DOUBLE_LITTLE 0xC
+#define CODE_DOUBLE_ARRAY8_BIG 0xD
+#define CODE_DOUBLE_ARRAY8_LITTLE 0xE
+#define CODE_DOUBLE_ARRAY32_BIG 0xF
+#define CODE_DOUBLE_ARRAY32_LITTLE 0x7
+#define CODE_DOUBLE_ARRAY64_BIG 0x16
+#define CODE_DOUBLE_ARRAY64_LITTLE 0x17
+#define CODE_CODEPOINTER 0x10
+#define CODE_INFIXPOINTER 0x11
+#define CODE_CUSTOM 0x12
+
+#if ARCH_FLOAT_ENDIANNESS == 0x76543210
+#define CODE_DOUBLE_NATIVE CODE_DOUBLE_BIG
+#define CODE_DOUBLE_ARRAY8_NATIVE CODE_DOUBLE_ARRAY8_BIG
+#define CODE_DOUBLE_ARRAY32_NATIVE CODE_DOUBLE_ARRAY32_BIG
+#define CODE_DOUBLE_ARRAY64_NATIVE CODE_DOUBLE_ARRAY64_BIG
+#else
+#define CODE_DOUBLE_NATIVE CODE_DOUBLE_LITTLE
+#define CODE_DOUBLE_ARRAY8_NATIVE CODE_DOUBLE_ARRAY8_LITTLE
+#define CODE_DOUBLE_ARRAY32_NATIVE CODE_DOUBLE_ARRAY32_LITTLE
+#define CODE_DOUBLE_ARRAY64_NATIVE CODE_DOUBLE_ARRAY64_LITTLE
+#endif
+
+/* Size-ing data structures for extern. Chosen so that
+ sizeof(struct trail_block) and sizeof(struct output_block)
+ are slightly below 8Kb. */
+
+#define ENTRIES_PER_TRAIL_BLOCK 1025
+#define SIZE_EXTERN_OUTPUT_BLOCK 8100
+
+/* The entry points */
+
+void caml_output_val (struct channel * chan, value v, value flags);
+ /* Output [v] with flags [flags] on the channel [chan]. */
+
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CAMLextern void caml_output_value_to_malloc(value v, value flags,
+ /*out*/ char ** buf,
+ /*out*/ intnat * len);
+ /* Output [v] with flags [flags] to a memory buffer allocated with
+ malloc. On return, [*buf] points to the buffer and [*len]
+ contains the number of bytes in buffer. */
+CAMLextern intnat caml_output_value_to_block(value v, value flags,
+ char * data, intnat len);
+ /* Output [v] with flags [flags] to a user-provided memory buffer.
+ [data] points to the start of this buffer, and [len] is its size
+ in bytes. Return the number of bytes actually written in buffer.
+ Raise [Failure] if buffer is too short. */
+
+#ifdef CAML_INTERNALS
+value caml_input_val (struct channel * chan);
+ /* Read a structured value from the channel [chan]. */
+
+extern value caml_input_value_to_outside_heap (value channel);
+ /* As for [caml_input_value], but the value is unmarshalled into
+ malloc blocks that are not added to the heap. Not for the
+ casual user. */
+
+extern int caml_extern_allow_out_of_heap;
+ /* Permit the marshaller to traverse structures that look like OCaml
+ values but do not live in the OCaml heap. */
+
+extern value caml_output_value(value vchan, value v, value flags);
+#endif /* CAML_INTERNALS */
+
+CAMLextern value caml_input_val_from_string (value str, intnat ofs);
+ /* Read a structured value from the OCaml string [str], starting
+ at offset [ofs]. */
+CAMLextern value caml_input_value_from_malloc(char * data, intnat ofs);
+ /* Read a structured value from a malloced buffer. [data] points
+ to the beginning of the buffer, and [ofs] is the offset of the
+ beginning of the externed data in this buffer. The buffer is
+ deallocated with [free] on return, or if an exception is raised. */
+CAMLextern value caml_input_value_from_block(char * data, intnat len);
+ /* Read a structured value from a user-provided buffer. [data] points
+ to the beginning of the externed data in this buffer,
+ and [len] is the length in bytes of valid data in this buffer.
+ The buffer is never deallocated by this routine. */
+
+/* Functions for writing user-defined marshallers */
+
+CAMLextern void caml_serialize_int_1(int i);
+CAMLextern void caml_serialize_int_2(int i);
+CAMLextern void caml_serialize_int_4(int32_t i);
+CAMLextern void caml_serialize_int_8(int64_t i);
+CAMLextern void caml_serialize_float_4(float f);
+CAMLextern void caml_serialize_float_8(double f);
+CAMLextern void caml_serialize_block_1(void * data, intnat len);
+CAMLextern void caml_serialize_block_2(void * data, intnat len);
+CAMLextern void caml_serialize_block_4(void * data, intnat len);
+CAMLextern void caml_serialize_block_8(void * data, intnat len);
+CAMLextern void caml_serialize_block_float_8(void * data, intnat len);
+
+CAMLextern int caml_deserialize_uint_1(void);
+CAMLextern int caml_deserialize_sint_1(void);
+CAMLextern int caml_deserialize_uint_2(void);
+CAMLextern int caml_deserialize_sint_2(void);
+CAMLextern uint32_t caml_deserialize_uint_4(void);
+CAMLextern int32_t caml_deserialize_sint_4(void);
+CAMLextern uint64_t caml_deserialize_uint_8(void);
+CAMLextern int64_t caml_deserialize_sint_8(void);
+CAMLextern float caml_deserialize_float_4(void);
+CAMLextern double caml_deserialize_float_8(void);
+CAMLextern void caml_deserialize_block_1(void * data, intnat len);
+CAMLextern void caml_deserialize_block_2(void * data, intnat len);
+CAMLextern void caml_deserialize_block_4(void * data, intnat len);
+CAMLextern void caml_deserialize_block_8(void * data, intnat len);
+CAMLextern void caml_deserialize_block_float_8(void * data, intnat len);
+CAMLextern void caml_deserialize_error(char * msg);
+
+#ifdef CAML_INTERNALS
+
+/* Auxiliary stuff for sending code pointers */
+
+struct code_fragment {
+ char * code_start;
+ char * code_end;
+ unsigned char digest[16];
+ char digest_computed;
+};
+
+CAMLextern struct code_fragment * caml_extern_find_code(char *addr);
+
+extern struct ext_table caml_code_fragments_table;
+
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_INTEXT_H */
diff --git a/test/monniaux/ocaml/byterun/caml/io.h b/test/monniaux/ocaml/byterun/caml/io.h
new file mode 100644
index 00000000..87de679e
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/io.h
@@ -0,0 +1,126 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Buffered input/output */
+
+#ifndef CAML_IO_H
+#define CAML_IO_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifndef IO_BUFFER_SIZE
+#define IO_BUFFER_SIZE 65536
+#endif
+
+#if defined(_WIN32)
+typedef __int64 file_offset;
+#elif defined(HAS_OFF_T)
+#include <sys/types.h>
+typedef off_t file_offset;
+#else
+typedef long file_offset;
+#endif
+
+struct channel {
+ int fd; /* Unix file descriptor */
+ file_offset offset; /* Absolute position of fd in the file */
+ char * end; /* Physical end of the buffer */
+ char * curr; /* Current position in the buffer */
+ char * max; /* Logical end of the buffer (for input) */
+ void * mutex; /* Placeholder for mutex (for systhreads) */
+ struct channel * next, * prev;/* Double chaining of channels (flush_all) */
+ int revealed; /* For Cash only */
+ int old_revealed; /* For Cash only */
+ int refcount; /* For flush_all and for Cash */
+ int flags; /* Bitfield */
+ char buff[IO_BUFFER_SIZE]; /* The buffer itself */
+ char * name; /* Optional name (to report fd leaks) */
+};
+
+enum {
+ CHANNEL_FLAG_FROM_SOCKET = 1, /* For Windows */
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ CHANNEL_FLAG_BLOCKING_WRITE = 2, /* Don't release master lock when writing */
+#endif
+ CHANNEL_FLAG_MANAGED_BY_GC = 4, /* Free and close using GC finalization */
+};
+
+/* For an output channel:
+ [offset] is the absolute position of the beginning of the buffer [buff].
+ For an input channel:
+ [offset] is the absolute position of the logical end of the buffer, [max].
+*/
+
+/* Functions and macros that can be called from C. Take arguments of
+ type struct channel *. No locking is performed. */
+
+#define caml_putch(channel, ch) do{ \
+ if ((channel)->curr >= (channel)->end) caml_flush_partial(channel); \
+ *((channel)->curr)++ = (ch); \
+}while(0)
+
+#define caml_getch(channel) \
+ ((channel)->curr >= (channel)->max \
+ ? caml_refill(channel) \
+ : (unsigned char) *((channel)->curr)++)
+
+CAMLextern struct channel * caml_open_descriptor_in (int);
+CAMLextern struct channel * caml_open_descriptor_out (int);
+CAMLextern void caml_close_channel (struct channel *);
+CAMLextern int caml_channel_binary_mode (struct channel *);
+CAMLextern value caml_alloc_channel(struct channel *chan);
+
+CAMLextern int caml_flush_partial (struct channel *);
+CAMLextern void caml_flush (struct channel *);
+CAMLextern void caml_putword (struct channel *, uint32_t);
+CAMLextern int caml_putblock (struct channel *, char *, intnat);
+CAMLextern void caml_really_putblock (struct channel *, char *, intnat);
+
+CAMLextern unsigned char caml_refill (struct channel *);
+CAMLextern uint32_t caml_getword (struct channel *);
+CAMLextern int caml_getblock (struct channel *, char *, intnat);
+CAMLextern intnat caml_really_getblock (struct channel *, char *, intnat);
+
+/* Extract a struct channel * from the heap object representing it */
+
+#define Channel(v) (*((struct channel **) (Data_custom_val(v))))
+
+/* The locking machinery */
+
+CAMLextern void (*caml_channel_mutex_free) (struct channel *);
+CAMLextern void (*caml_channel_mutex_lock) (struct channel *);
+CAMLextern void (*caml_channel_mutex_unlock) (struct channel *);
+CAMLextern void (*caml_channel_mutex_unlock_exn) (void);
+
+CAMLextern struct channel * caml_all_opened_channels;
+
+#define Lock(channel) \
+ if (caml_channel_mutex_lock != NULL) (*caml_channel_mutex_lock)(channel)
+#define Unlock(channel) \
+ if (caml_channel_mutex_unlock != NULL) (*caml_channel_mutex_unlock)(channel)
+#define Unlock_exn() \
+ if (caml_channel_mutex_unlock_exn != NULL) (*caml_channel_mutex_unlock_exn)()
+
+/* Conversion between file_offset and int64_t */
+
+#define Val_file_offset(fofs) caml_copy_int64(fofs)
+#define File_offset_val(v) ((file_offset) Int64_val(v))
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_IO_H */
diff --git a/test/monniaux/ocaml/byterun/caml/jumptbl.h b/test/monniaux/ocaml/byterun/caml/jumptbl.h
new file mode 100644
index 00000000..30588756
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/jumptbl.h
@@ -0,0 +1,40 @@
+ &&lbl_ACC0, &&lbl_ACC1, &&lbl_ACC2, &&lbl_ACC3, &&lbl_ACC4, &&lbl_ACC5, &&lbl_ACC6, &&lbl_ACC7,
+ &&lbl_ACC, &&lbl_PUSH,
+ &&lbl_PUSHACC0, &&lbl_PUSHACC1, &&lbl_PUSHACC2, &&lbl_PUSHACC3,
+ &&lbl_PUSHACC4, &&lbl_PUSHACC5, &&lbl_PUSHACC6, &&lbl_PUSHACC7,
+ &&lbl_PUSHACC, &&lbl_POP, &&lbl_ASSIGN,
+ &&lbl_ENVACC1, &&lbl_ENVACC2, &&lbl_ENVACC3, &&lbl_ENVACC4, &&lbl_ENVACC,
+ &&lbl_PUSHENVACC1, &&lbl_PUSHENVACC2, &&lbl_PUSHENVACC3, &&lbl_PUSHENVACC4, &&lbl_PUSHENVACC,
+ &&lbl_PUSH_RETADDR, &&lbl_APPLY, &&lbl_APPLY1, &&lbl_APPLY2, &&lbl_APPLY3,
+ &&lbl_APPTERM, &&lbl_APPTERM1, &&lbl_APPTERM2, &&lbl_APPTERM3,
+ &&lbl_RETURN, &&lbl_RESTART, &&lbl_GRAB,
+ &&lbl_CLOSURE, &&lbl_CLOSUREREC,
+ &&lbl_OFFSETCLOSUREM2, &&lbl_OFFSETCLOSURE0, &&lbl_OFFSETCLOSURE2, &&lbl_OFFSETCLOSURE,
+ &&lbl_PUSHOFFSETCLOSUREM2, &&lbl_PUSHOFFSETCLOSURE0,
+ &&lbl_PUSHOFFSETCLOSURE2, &&lbl_PUSHOFFSETCLOSURE,
+ &&lbl_GETGLOBAL, &&lbl_PUSHGETGLOBAL, &&lbl_GETGLOBALFIELD, &&lbl_PUSHGETGLOBALFIELD, &&lbl_SETGLOBAL,
+ &&lbl_ATOM0, &&lbl_ATOM, &&lbl_PUSHATOM0, &&lbl_PUSHATOM,
+ &&lbl_MAKEBLOCK, &&lbl_MAKEBLOCK1, &&lbl_MAKEBLOCK2, &&lbl_MAKEBLOCK3, &&lbl_MAKEFLOATBLOCK,
+ &&lbl_GETFIELD0, &&lbl_GETFIELD1, &&lbl_GETFIELD2, &&lbl_GETFIELD3, &&lbl_GETFIELD, &&lbl_GETFLOATFIELD,
+ &&lbl_SETFIELD0, &&lbl_SETFIELD1, &&lbl_SETFIELD2, &&lbl_SETFIELD3, &&lbl_SETFIELD, &&lbl_SETFLOATFIELD,
+ &&lbl_VECTLENGTH, &&lbl_GETVECTITEM, &&lbl_SETVECTITEM,
+ &&lbl_GETBYTESCHAR, &&lbl_SETBYTESCHAR,
+ &&lbl_BRANCH, &&lbl_BRANCHIF, &&lbl_BRANCHIFNOT, &&lbl_SWITCH, &&lbl_BOOLNOT,
+ &&lbl_PUSHTRAP, &&lbl_POPTRAP, &&lbl_RAISE,
+ &&lbl_CHECK_SIGNALS,
+ &&lbl_C_CALL1, &&lbl_C_CALL2, &&lbl_C_CALL3, &&lbl_C_CALL4, &&lbl_C_CALL5, &&lbl_C_CALLN,
+ &&lbl_CONST0, &&lbl_CONST1, &&lbl_CONST2, &&lbl_CONST3, &&lbl_CONSTINT,
+ &&lbl_PUSHCONST0, &&lbl_PUSHCONST1, &&lbl_PUSHCONST2, &&lbl_PUSHCONST3, &&lbl_PUSHCONSTINT,
+ &&lbl_NEGINT, &&lbl_ADDINT, &&lbl_SUBINT, &&lbl_MULINT, &&lbl_DIVINT, &&lbl_MODINT,
+ &&lbl_ANDINT, &&lbl_ORINT, &&lbl_XORINT, &&lbl_LSLINT, &&lbl_LSRINT, &&lbl_ASRINT,
+ &&lbl_EQ, &&lbl_NEQ, &&lbl_LTINT, &&lbl_LEINT, &&lbl_GTINT, &&lbl_GEINT,
+ &&lbl_OFFSETINT, &&lbl_OFFSETREF, &&lbl_ISINT,
+ &&lbl_GETMETHOD,
+ &&lbl_BEQ, &&lbl_BNEQ, &&lbl_BLTINT, &&lbl_BLEINT, &&lbl_BGTINT, &&lbl_BGEINT,
+ &&lbl_ULTINT, &&lbl_UGEINT,
+ &&lbl_BULTINT, &&lbl_BUGEINT,
+ &&lbl_GETPUBMET, &&lbl_GETDYNMET,
+ &&lbl_STOP,
+ &&lbl_EVENT, &&lbl_BREAK,
+ &&lbl_RERAISE, &&lbl_RAISE_NOTRACE,
+ &&lbl_GETSTRINGCHAR,
diff --git a/test/monniaux/ocaml/byterun/caml/m.h b/test/monniaux/ocaml/byterun/caml/m.h
new file mode 100644
index 00000000..c54b4f0d
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/m.h
@@ -0,0 +1,86 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Processor dependencies */
+
+#define ARCH_SIXTYFOUR
+
+/* Define ARCH_SIXTYFOUR if the processor has a natural word size of 64 bits.
+ That is, sizeof(char *) = 8.
+ Otherwise, leave ARCH_SIXTYFOUR undefined.
+ This assumes sizeof(char *) = 4. */
+
+/* #define ARCH_BIG_ENDIAN */
+
+/* Define ARCH_BIG_ENDIAN if the processor is big endian (the most
+ significant byte of an integer stored in memory comes first).
+ Leave ARCH_BIG_ENDIAN undefined if the processor is little-endian
+ (the least significant byte comes first).
+*/
+
+#define ARCH_ALIGN_DOUBLE
+
+/* Define ARCH_ALIGN_DOUBLE if the processor requires doubles to be
+ doubleword-aligned. Leave ARCH_ALIGN_DOUBLE undefined if the processor
+ supports word-aligned doubles. */
+
+#undef ARCH_CODE32
+
+/* Define ARCH_CODE32 if, on a 64-bit machine, code pointers fit in 32 bits,
+ i.e. the code segment resides in the low 4G of the addressing space.
+ ARCH_CODE32 is ignored on 32-bit machines. */
+
+#define SIZEOF_INT 4
+#define SIZEOF_LONG 8
+#define SIZEOF_PTR 8
+#define SIZEOF_SHORT 2
+
+/* Define SIZEOF_INT, SIZEOF_LONG, SIZEOF_PTR and SIZEOF_SHORT
+ to the sizes in bytes of the C types "int", "long", "char *" and "short",
+ respectively. */
+
+#define ARCH_INT64_TYPE long long
+#define ARCH_UINT64_TYPE unsigned long long
+
+/* Define ARCH_INT64_TYPE and ARCH_UINT64_TYPE to 64-bit integer types,
+ typically "long long" and "unsigned long long" on 32-bit platforms,
+ and "long" and "unsigned long" on 64-bit platforms.
+ If the C compiler doesn't support any 64-bit integer type,
+ leave both ARCH_INT64_TYPE and ARCH_UINT64_TYPE undefined. */
+
+#define ARCH_INT64_PRINTF_FORMAT "ll"
+
+/* Define ARCH_INT64_PRINTF_FORMAT to the printf format used for formatting
+ values of type ARCH_INT64_TYPE. This is usually "ll" on 32-bit
+ platforms and "l" on 64-bit platforms.
+ Leave undefined if ARCH_INT64_TYPE is undefined. */
+
+#define ARCH_ALIGN_INT64
+
+/* Define ARCH_ALIGN_INT64 if the processor requires 64-bit integers to be
+ doubleword-aligned. Leave ARCH_ALIGN_INT64 undefined if the processor
+ supports word-aligned 64-bit integers. Leave undefined if
+ 64-bit integers are not supported. */
+
+#undef NONSTANDARD_DIV_MOD
+
+/* Leave NONSTANDARD_DIV_MOD undefined if the C operators / and % implement
+ round-towards-zero semantics, as specified by ISO C 9x and implemented
+ by most contemporary processors. Otherwise, or if you don't know,
+ define NONSTANDARD_DIV_MOD: this will select a slower but correct
+ software emulation of division and modulus. */
+
+#define PROFINFO_WIDTH 0
+#define INT64_LITERAL(s) s ## LL
diff --git a/test/monniaux/ocaml/byterun/caml/major_gc.h b/test/monniaux/ocaml/byterun/caml/major_gc.h
new file mode 100644
index 00000000..813f8a78
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/major_gc.h
@@ -0,0 +1,95 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_MAJOR_GC_H
+#define CAML_MAJOR_GC_H
+
+#ifdef CAML_INTERNALS
+
+#include "freelist.h"
+#include "misc.h"
+
+typedef struct {
+ void *block; /* address of the malloced block this chunk lives in */
+ asize_t alloc; /* in bytes, used for compaction */
+ asize_t size; /* in bytes */
+ char *next;
+} heap_chunk_head;
+
+#define Chunk_size(c) (((heap_chunk_head *) (c)) [-1]).size
+#define Chunk_alloc(c) (((heap_chunk_head *) (c)) [-1]).alloc
+#define Chunk_next(c) (((heap_chunk_head *) (c)) [-1]).next
+#define Chunk_block(c) (((heap_chunk_head *) (c)) [-1]).block
+
+extern int caml_gc_phase;
+extern int caml_gc_subphase;
+extern uintnat caml_allocated_words;
+extern double caml_extra_heap_resources;
+extern uintnat caml_dependent_size, caml_dependent_allocated;
+extern uintnat caml_fl_wsz_at_phase_change;
+
+#define Phase_mark 0
+#define Phase_clean 1
+#define Phase_sweep 2
+#define Phase_idle 3
+
+/* Subphase of mark */
+#define Subphase_mark_roots 10
+/* Subphase_mark_roots: At the end of this subphase all the global
+ roots are marked. */
+#define Subphase_mark_main 11
+/* Subphase_mark_main: At the end of this subphase all the value alive at
+ the start of this subphase and created during it are marked. */
+#define Subphase_mark_final 12
+/* Subphase_mark_final: At the start of this subphase register which
+ value with an ocaml finalizer are not marked, the associated
+ finalizer will be run later. So we mark now these values as alive,
+ since they must be available for their finalizer.
+ */
+
+CAMLextern char *caml_heap_start;
+extern uintnat total_heap_size;
+extern char *caml_gc_sweep_hp;
+
+extern int caml_major_window;
+extern double caml_major_ring[Max_major_window];
+extern int caml_major_ring_index;
+extern double caml_major_work_credit;
+extern double caml_gc_clock;
+
+/* [caml_major_gc_hook] is called just between the end of the mark
+ phase and the beginning of the sweep phase of the major GC */
+CAMLextern void (*caml_major_gc_hook)(void);
+
+void caml_init_major_heap (asize_t); /* size in bytes */
+asize_t caml_clip_heap_chunk_wsz (asize_t wsz);
+void caml_darken (value, value *);
+void caml_major_collection_slice (intnat);
+void major_collection (void);
+void caml_finish_major_cycle (void);
+void caml_set_major_window (int);
+
+/* Forces finalisation of all heap-allocated values,
+ disregarding both local and global roots.
+
+ Warning: finalisation is performed by means of forced sweeping, which may
+ result in pointers referencing nonexistent values; therefore the function
+ should only be used on runtime shutdown.
+*/
+void caml_finalise_heap (void);
+
+#endif /* CAML_INTERNALiS */
+
+#endif /* CAML_MAJOR_GC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/md5.h b/test/monniaux/ocaml/byterun/caml/md5.h
new file mode 100644
index 00000000..e83c16cd
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/md5.h
@@ -0,0 +1,47 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1999 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* MD5 message digest */
+
+#ifndef CAML_MD5_H
+#define CAML_MD5_H
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+#include "io.h"
+
+CAMLextern value caml_md5_string (value str, value ofs, value len);
+CAMLextern value caml_md5_chan (value vchan, value len);
+CAMLextern void caml_md5_block(unsigned char digest[16],
+ void * data, uintnat len);
+
+CAMLextern value caml_md5_channel(struct channel *chan, intnat toread);
+
+struct MD5Context {
+ uint32_t buf[4];
+ uint32_t bits[2];
+ unsigned char in[64];
+};
+
+CAMLextern void caml_MD5Init (struct MD5Context *context);
+CAMLextern void caml_MD5Update (struct MD5Context *context, unsigned char *buf,
+ uintnat len);
+CAMLextern void caml_MD5Final (unsigned char *digest, struct MD5Context *ctx);
+CAMLextern void caml_MD5Transform (uint32_t *buf, uint32_t *in);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_MD5_H */
diff --git a/test/monniaux/ocaml/byterun/caml/memory.h b/test/monniaux/ocaml/byterun/caml/memory.h
new file mode 100644
index 00000000..1ba489b2
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/memory.h
@@ -0,0 +1,595 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Allocation macros and functions */
+
+#ifndef CAML_MEMORY_H
+#define CAML_MEMORY_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "config.h"
+#ifdef CAML_INTERNALS
+#include "gc.h"
+#include "major_gc.h"
+#include "minor_gc.h"
+#endif /* CAML_INTERNALS */
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+CAMLextern value caml_alloc_shr (mlsize_t wosize, tag_t);
+#ifdef WITH_PROFINFO
+CAMLextern value caml_alloc_shr_with_profinfo (mlsize_t, tag_t, intnat);
+CAMLextern value caml_alloc_shr_preserving_profinfo (mlsize_t, tag_t,
+ header_t);
+#else
+#define caml_alloc_shr_with_profinfo(size, tag, profinfo) \
+ caml_alloc_shr(size, tag)
+#define caml_alloc_shr_preserving_profinfo(size, tag, header) \
+ caml_alloc_shr(size, tag)
+#endif /* WITH_PROFINFO */
+CAMLextern value caml_alloc_shr_no_raise (mlsize_t wosize, tag_t);
+CAMLextern void caml_adjust_gc_speed (mlsize_t, mlsize_t);
+CAMLextern void caml_alloc_dependent_memory (mlsize_t bsz);
+CAMLextern void caml_free_dependent_memory (mlsize_t bsz);
+CAMLextern void caml_modify (value *, value);
+CAMLextern void caml_initialize (value *, value);
+CAMLextern value caml_check_urgent_gc (value);
+CAMLextern int caml_init_alloc_for_heap (void);
+CAMLextern char *caml_alloc_for_heap (asize_t request); /* Size in bytes. */
+CAMLextern void caml_free_for_heap (char *mem);
+CAMLextern void caml_disown_for_heap (char *mem);
+CAMLextern int caml_add_to_heap (char *mem);
+CAMLextern color_t caml_allocation_color (void *hp);
+
+CAMLextern int caml_huge_fallback_count;
+
+
+/* [caml_stat_*] functions below provide an interface to the static memory
+ manager built into the runtime, which can be used for managing static
+ (that is, non-moving) blocks of heap memory.
+
+ Function arguments that have type [caml_stat_block] must always be pointers
+ to blocks returned by the [caml_stat_*] functions below. Attempting to use
+ these functions on memory blocks allocated by a different memory manager
+ (e.g. the one from the C runtime) will cause undefined behaviour.
+*/
+typedef void* caml_stat_block;
+
+#ifdef CAML_INTERNALS
+
+/* The pool must be initialized with a call to [caml_stat_create_pool]
+ before it is possible to use any of the [caml_stat_*] functions below.
+
+ If the pool is not initialized, [caml_stat_*] functions will still work in
+ backward compatibility mode, becoming thin wrappers around [malloc] family
+ of functions. In this case, calling [caml_stat_destroy_pool] will not free
+ the claimed heap memory, resulting in leaks.
+*/
+CAMLextern void caml_stat_create_pool(void);
+
+/* [caml_stat_destroy_pool] frees all the heap memory claimed by the pool.
+
+ Once the pool is destroyed, [caml_stat_*] functions will continue to work
+ in backward compatibility mode, becoming thin wrappers around [malloc]
+ family of functions.
+*/
+CAMLextern void caml_stat_destroy_pool(void);
+
+#endif /* CAML_INTERNALS */
+
+/* [caml_stat_alloc(size)] allocates a memory block of the requested [size]
+ (in bytes) and returns a pointer to it. It throws an OCaml exception in case
+ the request fails, and so requires the runtime lock to be held.
+*/
+CAMLextern caml_stat_block caml_stat_alloc(asize_t);
+
+/* [caml_stat_alloc_noexc(size)] allocates a memory block of the requested [size]
+ (in bytes) and returns a pointer to it, or NULL in case the request fails.
+*/
+CAMLextern caml_stat_block caml_stat_alloc_noexc(asize_t);
+
+/* [caml_stat_alloc_aligned(size, modulo, block*)] allocates a memory block of
+ the requested [size] (in bytes), the starting address of which is aligned to
+ the provided [modulo] value. The function returns the aligned address, as
+ well as the unaligned [block] (as an output parameter). It throws an OCaml
+ exception in case the request fails, and so requires the runtime lock.
+*/
+CAMLextern void* caml_stat_alloc_aligned(asize_t, int modulo, caml_stat_block*);
+
+/* [caml_stat_alloc_aligned_noexc] is a variant of [caml_stat_alloc_aligned]
+ that returns NULL in case the request fails, and doesn't require the runtime
+ lock to be held.
+*/
+CAMLextern void* caml_stat_alloc_aligned_noexc(asize_t, int modulo,
+ caml_stat_block*);
+
+/* [caml_stat_calloc_noexc(num, size)] allocates a block of memory for an array
+ of [num] elements, each of them [size] bytes long, and initializes all its
+ bits to zero, effectively allocating a zero-initialized memory block of
+ [num * size] bytes. It returns NULL in case the request fails.
+*/
+CAMLextern caml_stat_block caml_stat_calloc_noexc(asize_t, asize_t);
+
+/* [caml_stat_free(block)] deallocates the provided [block]. */
+CAMLextern void caml_stat_free(caml_stat_block);
+
+/* [caml_stat_resize(block, size)] changes the size of the provided [block] to
+ [size] bytes. The function may move the memory block to a new location (whose
+ address is returned by the function). The content of the [block] is preserved
+ up to the smaller of the new and old sizes, even if the block is moved to a
+ new location. If the new size is larger, the value of the newly allocated
+ portion is indeterminate. The function throws an OCaml exception in case the
+ request fails, and so requires the runtime lock to be held.
+*/
+CAMLextern caml_stat_block caml_stat_resize(caml_stat_block, asize_t);
+
+/* [caml_stat_resize_noexc] is a variant of [caml_stat_resize] that returns NULL
+ in case the request fails, and doesn't require the runtime lock.
+*/
+CAMLextern caml_stat_block caml_stat_resize_noexc(caml_stat_block, asize_t);
+
+
+/* A [caml_stat_block] containing a NULL-terminated string */
+typedef char* caml_stat_string;
+
+/* [caml_stat_strdup(s)] returns a pointer to a heap-allocated string which is a
+ copy of the NULL-terminated string [s]. It throws an OCaml exception in case
+ the request fails, and so requires the runtime lock to be held.
+*/
+CAMLextern caml_stat_string caml_stat_strdup(const char *s);
+#ifdef _WIN32
+CAMLextern wchar_t* caml_stat_wcsdup(const wchar_t *s);
+#endif
+
+/* [caml_stat_strdup_noexc] is a variant of [caml_stat_strdup] that returns NULL
+ in case the request fails, and doesn't require the runtime lock.
+*/
+CAMLextern caml_stat_string caml_stat_strdup_noexc(const char *s);
+
+/* [caml_stat_strconcat(nargs, strings)] concatenates NULL-terminated [strings]
+ (an array of [char*] of size [nargs]) into a new string, dropping all NULLs,
+ except for the very last one. It throws an OCaml exception in case the
+ request fails, and so requires the runtime lock to be held.
+*/
+CAMLextern caml_stat_string caml_stat_strconcat(int n, ...);
+#ifdef _WIN32
+CAMLextern wchar_t* caml_stat_wcsconcat(int n, ...);
+#endif
+
+
+/* void caml_shrink_heap (char *); Only used in compact.c */
+
+#ifdef CAML_INTERNALS
+
+extern uintnat caml_use_huge_pages;
+
+#ifdef HAS_HUGE_PAGES
+#include <sys/mman.h>
+#define Heap_page_size HUGE_PAGE_SIZE
+#define Round_mmap_size(x) \
+ (((x) + (Heap_page_size - 1)) & ~ (Heap_page_size - 1))
+#endif
+
+
+int caml_page_table_add(int kind, void * start, void * end);
+int caml_page_table_remove(int kind, void * start, void * end);
+int caml_page_table_initialize(mlsize_t bytesize);
+
+#ifdef DEBUG
+#define DEBUG_clear(result, wosize) do{ \
+ uintnat caml__DEBUG_i; \
+ for (caml__DEBUG_i = 0; caml__DEBUG_i < (wosize); ++ caml__DEBUG_i){ \
+ Field ((result), caml__DEBUG_i) = Debug_uninit_minor; \
+ } \
+}while(0)
+#else
+#define DEBUG_clear(result, wosize)
+#endif
+
+#define Alloc_small_with_profinfo(result, wosize, tag, profinfo) do { \
+ CAMLassert ((wosize) >= 1); \
+ CAMLassert ((tag_t) (tag) < 256); \
+ CAMLassert ((wosize) <= Max_young_wosize); \
+ caml_young_ptr -= Whsize_wosize (wosize); \
+ if (caml_young_ptr < caml_young_trigger){ \
+ caml_young_ptr += Whsize_wosize (wosize); \
+ CAML_INSTR_INT ("force_minor/alloc_small@", 1); \
+ Setup_for_gc; \
+ caml_gc_dispatch (); \
+ Restore_after_gc; \
+ caml_young_ptr -= Whsize_wosize (wosize); \
+ } \
+ Hd_hp (caml_young_ptr) = \
+ Make_header_with_profinfo ((wosize), (tag), Caml_black, profinfo); \
+ (result) = Val_hp (caml_young_ptr); \
+ DEBUG_clear ((result), (wosize)); \
+}while(0)
+
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+extern uintnat caml_spacetime_my_profinfo(struct ext_table**, uintnat);
+#define Alloc_small(result, wosize, tag) \
+ Alloc_small_with_profinfo(result, wosize, tag, \
+ caml_spacetime_my_profinfo(NULL, wosize))
+#else
+#define Alloc_small(result, wosize, tag) \
+ Alloc_small_with_profinfo(result, wosize, tag, (uintnat) 0)
+#endif
+
+/* Deprecated alias for [caml_modify] */
+
+#define Modify(fp,val) caml_modify((fp), (val))
+
+#endif /* CAML_INTERNALS */
+
+struct caml__roots_block {
+ struct caml__roots_block *next;
+ intnat ntables;
+ intnat nitems;
+ value *tables [5];
+};
+
+CAMLextern struct caml__roots_block *caml_local_roots; /* defined in roots.c */
+
+/* The following macros are used to declare C local variables and
+ function parameters of type [value].
+
+ The function body must start with one of the [CAMLparam] macros.
+ If the function has no parameter of type [value], use [CAMLparam0].
+ If the function has 1 to 5 [value] parameters, use the corresponding
+ [CAMLparam] with the parameters as arguments.
+ If the function has more than 5 [value] parameters, use [CAMLparam5]
+ for the first 5 parameters, and one or more calls to the [CAMLxparam]
+ macros for the others.
+ If the function takes an array of [value]s as argument, use
+ [CAMLparamN] to declare it (or [CAMLxparamN] if you already have a
+ call to [CAMLparam] for some other arguments).
+
+ If you need local variables of type [value], declare them with one
+ or more calls to the [CAMLlocal] macros at the beginning of the
+ function, after the call to CAMLparam. Use [CAMLlocalN] (at the
+ beginning of the function) to declare an array of [value]s.
+
+ Your function may raise an exception or return a [value] with the
+ [CAMLreturn] macro. Its argument is simply the [value] returned by
+ your function. Do NOT directly return a [value] with the [return]
+ keyword. If your function returns void, use [CAMLreturn0]. If you
+ un-register the local roots (i.e. undo the effects of the [CAMLparam*]
+ and [CAMLlocal] macros) without returning immediately, use [CAMLdrop].
+
+ All the identifiers beginning with "caml__" are reserved by OCaml.
+ Do not use them for anything (local or global variables, struct or
+ union tags, macros, etc.)
+*/
+
+#define CAMLparam0() \
+ struct caml__roots_block *caml__frame = caml_local_roots
+
+#define CAMLparam1(x) \
+ CAMLparam0 (); \
+ CAMLxparam1 (x)
+
+#define CAMLparam2(x, y) \
+ CAMLparam0 (); \
+ CAMLxparam2 (x, y)
+
+#define CAMLparam3(x, y, z) \
+ CAMLparam0 (); \
+ CAMLxparam3 (x, y, z)
+
+#define CAMLparam4(x, y, z, t) \
+ CAMLparam0 (); \
+ CAMLxparam4 (x, y, z, t)
+
+#define CAMLparam5(x, y, z, t, u) \
+ CAMLparam0 (); \
+ CAMLxparam5 (x, y, z, t, u)
+
+#define CAMLparamN(x, size) \
+ CAMLparam0 (); \
+ CAMLxparamN (x, (size))
+
+/* CAMLunused is preserved for compatibility reasons.
+ Instead of the legacy GCC/Clang-only
+ CAMLunused foo;
+ you should prefer
+ CAMLunused_start foo CAMLunused_end;
+ which supports both GCC/Clang and MSVC.
+*/
+#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 7))
+ #define CAMLunused_start __attribute__ ((unused))
+ #define CAMLunused_end
+ #define CAMLunused __attribute__ ((unused))
+#elif _MSC_VER >= 1500
+ #define CAMLunused_start __pragma( warning (push) ) \
+ __pragma( warning (disable:4189 ) )
+ #define CAMLunused_end __pragma( warning (pop))
+ #define CAMLunused
+#else
+ #define CAMLunused_start
+ #define CAMLunused_end
+ #define CAMLunused
+#endif
+
+#define CAMLxparam1(x) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = 1), \
+ (caml__roots_##x.ntables = 1), \
+ (caml__roots_##x.tables [0] = &x), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLxparam2(x, y) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = 1), \
+ (caml__roots_##x.ntables = 2), \
+ (caml__roots_##x.tables [0] = &x), \
+ (caml__roots_##x.tables [1] = &y), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLxparam3(x, y, z) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = 1), \
+ (caml__roots_##x.ntables = 3), \
+ (caml__roots_##x.tables [0] = &x), \
+ (caml__roots_##x.tables [1] = &y), \
+ (caml__roots_##x.tables [2] = &z), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLxparam4(x, y, z, t) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = 1), \
+ (caml__roots_##x.ntables = 4), \
+ (caml__roots_##x.tables [0] = &x), \
+ (caml__roots_##x.tables [1] = &y), \
+ (caml__roots_##x.tables [2] = &z), \
+ (caml__roots_##x.tables [3] = &t), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLxparam5(x, y, z, t, u) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = 1), \
+ (caml__roots_##x.ntables = 5), \
+ (caml__roots_##x.tables [0] = &x), \
+ (caml__roots_##x.tables [1] = &y), \
+ (caml__roots_##x.tables [2] = &z), \
+ (caml__roots_##x.tables [3] = &t), \
+ (caml__roots_##x.tables [4] = &u), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLxparamN(x, size) \
+ struct caml__roots_block caml__roots_##x; \
+ CAMLunused_start int caml__dummy_##x = ( \
+ (void) caml__frame, \
+ (caml__roots_##x.next = caml_local_roots), \
+ (caml_local_roots = &caml__roots_##x), \
+ (caml__roots_##x.nitems = (size)), \
+ (caml__roots_##x.ntables = 1), \
+ (caml__roots_##x.tables[0] = &(x[0])), \
+ 0) \
+ CAMLunused_end
+
+#define CAMLlocal1(x) \
+ value x = Val_unit; \
+ CAMLxparam1 (x)
+
+#define CAMLlocal2(x, y) \
+ value x = Val_unit, y = Val_unit; \
+ CAMLxparam2 (x, y)
+
+#define CAMLlocal3(x, y, z) \
+ value x = Val_unit, y = Val_unit, z = Val_unit; \
+ CAMLxparam3 (x, y, z)
+
+#define CAMLlocal4(x, y, z, t) \
+ value x = Val_unit, y = Val_unit, z = Val_unit, t = Val_unit; \
+ CAMLxparam4 (x, y, z, t)
+
+#define CAMLlocal5(x, y, z, t, u) \
+ value x = Val_unit, y = Val_unit, z = Val_unit, t = Val_unit, u = Val_unit; \
+ CAMLxparam5 (x, y, z, t, u)
+
+#define CAMLlocalN(x, size) \
+ value x [(size)]; \
+ int caml__i_##x; \
+ for (caml__i_##x = 0; caml__i_##x < size; caml__i_##x ++) { \
+ x[caml__i_##x] = Val_unit; \
+ } \
+ CAMLxparamN (x, (size))
+
+
+#define CAMLdrop caml_local_roots = caml__frame
+
+#define CAMLreturn0 do{ \
+ CAMLdrop; \
+ return; \
+}while (0)
+
+#define CAMLreturnT(type, result) do{ \
+ type caml__temp_result = (result); \
+ CAMLdrop; \
+ return caml__temp_result; \
+}while(0)
+
+#define CAMLreturn(result) CAMLreturnT(value, result)
+
+#define CAMLnoreturn ((void) caml__frame)
+
+
+/* convenience macro */
+#define Store_field(block, offset, val) do{ \
+ mlsize_t caml__temp_offset = (offset); \
+ value caml__temp_val = (val); \
+ caml_modify (&Field ((block), caml__temp_offset), caml__temp_val); \
+}while(0)
+
+/*
+ NOTE: [Begin_roots] and [End_roots] are superseded by [CAMLparam]*,
+ [CAMLxparam]*, [CAMLlocal]*, [CAMLreturn].
+
+ [Begin_roots] and [End_roots] are used for C variables that are GC roots.
+ It must contain all values in C local variables and function parameters
+ at the time the minor GC is called.
+ Usage:
+ After initialising your local variables to legal OCaml values, but before
+ calling allocation functions, insert [Begin_roots_n(v1, ... vn)], where
+ v1 ... vn are your variables of type [value] that you want to be updated
+ across allocations.
+ At the end, insert [End_roots()].
+
+ Note that [Begin_roots] opens a new block, and [End_roots] closes it.
+ Thus they must occur in matching pairs at the same brace nesting level.
+
+ You can use [Val_unit] as a dummy initial value for your variables.
+*/
+
+#define Begin_root Begin_roots1
+
+#define Begin_roots1(r0) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = 1; \
+ caml__roots_block.ntables = 1; \
+ caml__roots_block.tables[0] = &(r0);
+
+#define Begin_roots2(r0, r1) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = 1; \
+ caml__roots_block.ntables = 2; \
+ caml__roots_block.tables[0] = &(r0); \
+ caml__roots_block.tables[1] = &(r1);
+
+#define Begin_roots3(r0, r1, r2) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = 1; \
+ caml__roots_block.ntables = 3; \
+ caml__roots_block.tables[0] = &(r0); \
+ caml__roots_block.tables[1] = &(r1); \
+ caml__roots_block.tables[2] = &(r2);
+
+#define Begin_roots4(r0, r1, r2, r3) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = 1; \
+ caml__roots_block.ntables = 4; \
+ caml__roots_block.tables[0] = &(r0); \
+ caml__roots_block.tables[1] = &(r1); \
+ caml__roots_block.tables[2] = &(r2); \
+ caml__roots_block.tables[3] = &(r3);
+
+#define Begin_roots5(r0, r1, r2, r3, r4) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = 1; \
+ caml__roots_block.ntables = 5; \
+ caml__roots_block.tables[0] = &(r0); \
+ caml__roots_block.tables[1] = &(r1); \
+ caml__roots_block.tables[2] = &(r2); \
+ caml__roots_block.tables[3] = &(r3); \
+ caml__roots_block.tables[4] = &(r4);
+
+#define Begin_roots_block(table, size) { \
+ struct caml__roots_block caml__roots_block; \
+ caml__roots_block.next = caml_local_roots; \
+ caml_local_roots = &caml__roots_block; \
+ caml__roots_block.nitems = (size); \
+ caml__roots_block.ntables = 1; \
+ caml__roots_block.tables[0] = (table);
+
+#define End_roots() caml_local_roots = caml__roots_block.next; }
+
+
+/* [caml_register_global_root] registers a global C variable as a memory root
+ for the duration of the program, or until [caml_remove_global_root] is
+ called. */
+
+CAMLextern void caml_register_global_root (value *);
+
+/* [caml_remove_global_root] removes a memory root registered on a global C
+ variable with [caml_register_global_root]. */
+
+CAMLextern void caml_remove_global_root (value *);
+
+/* [caml_register_generational_global_root] registers a global C
+ variable as a memory root for the duration of the program, or until
+ [caml_remove_generational_global_root] is called.
+ The program guarantees that the value contained in this variable
+ will not be assigned directly. If the program needs to change
+ the value of this variable, it must do so by calling
+ [caml_modify_generational_global_root]. The [value *] pointer
+ passed to [caml_register_generational_global_root] must contain
+ a valid OCaml value before the call.
+ In return for these constraints, scanning of memory roots during
+ minor collection is made more efficient. */
+
+CAMLextern void caml_register_generational_global_root (value *);
+
+/* [caml_remove_generational_global_root] removes a memory root
+ registered on a global C variable with
+ [caml_register_generational_global_root]. */
+
+CAMLextern void caml_remove_generational_global_root (value *);
+
+/* [caml_modify_generational_global_root(r, newval)]
+ modifies the value contained in [r], storing [newval] inside.
+ In other words, the assignment [*r = newval] is performed,
+ but in a way that is compatible with the optimized scanning of
+ generational global roots. [r] must be a global memory root
+ previously registered with [caml_register_generational_global_root]. */
+
+CAMLextern void caml_modify_generational_global_root(value *r, value newval);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_MEMORY_H */
diff --git a/test/monniaux/ocaml/byterun/caml/minor_gc.h b/test/monniaux/ocaml/byterun/caml/minor_gc.h
new file mode 100644
index 00000000..6c48c761
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/minor_gc.h
@@ -0,0 +1,120 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_MINOR_GC_H
+#define CAML_MINOR_GC_H
+
+
+#include "address_class.h"
+#include "config.h"
+
+CAMLextern value *caml_young_start, *caml_young_end;
+CAMLextern value *caml_young_alloc_start, *caml_young_alloc_end;
+CAMLextern value *caml_young_ptr, *caml_young_limit;
+CAMLextern value *caml_young_trigger;
+extern asize_t caml_minor_heap_wsz;
+extern int caml_in_minor_collection;
+extern double caml_extra_heap_resources_minor;
+
+#define CAML_TABLE_STRUCT(t) { \
+ t *base; \
+ t *end; \
+ t *threshold; \
+ t *ptr; \
+ t *limit; \
+ asize_t size; \
+ asize_t reserve; \
+}
+
+struct caml_ref_table CAML_TABLE_STRUCT(value *);
+CAMLextern struct caml_ref_table caml_ref_table;
+
+struct caml_ephe_ref_elt {
+ value ephe; /* an ephemeron in major heap */
+ mlsize_t offset; /* the offset that points in the minor heap */
+};
+
+struct caml_ephe_ref_table CAML_TABLE_STRUCT(struct caml_ephe_ref_elt);
+CAMLextern struct caml_ephe_ref_table caml_ephe_ref_table;
+
+struct caml_custom_elt {
+ value block; /* The finalized block in the minor heap. */
+ mlsize_t mem; /* The parameters for adjusting GC speed. */
+ mlsize_t max;
+};
+
+struct caml_custom_table CAML_TABLE_STRUCT(struct caml_custom_elt);
+CAMLextern struct caml_custom_table caml_custom_table;
+
+extern void caml_set_minor_heap_size (asize_t); /* size in bytes */
+extern void caml_empty_minor_heap (void);
+CAMLextern void caml_gc_dispatch (void);
+CAMLextern void garbage_collection (void); /* def in asmrun/signals_asm.c */
+extern void caml_realloc_ref_table (struct caml_ref_table *);
+extern void caml_alloc_table (struct caml_ref_table *, asize_t, asize_t);
+extern void caml_realloc_ephe_ref_table (struct caml_ephe_ref_table *);
+extern void caml_alloc_ephe_table (struct caml_ephe_ref_table *,
+ asize_t, asize_t);
+extern void caml_realloc_custom_table (struct caml_custom_table *);
+extern void caml_alloc_custom_table (struct caml_custom_table *,
+ asize_t, asize_t);
+extern void caml_oldify_one (value, value *);
+extern void caml_oldify_mopup (void);
+
+#define Oldify(p) do{ \
+ value __oldify__v__ = *p; \
+ if (Is_block (__oldify__v__) && Is_young (__oldify__v__)){ \
+ caml_oldify_one (__oldify__v__, (p)); \
+ } \
+ }while(0)
+
+static inline void add_to_ref_table (struct caml_ref_table *tbl, value *p)
+{
+ if (tbl->ptr >= tbl->limit){
+ CAMLassert (tbl->ptr == tbl->limit);
+ caml_realloc_ref_table (tbl);
+ }
+ *tbl->ptr++ = p;
+}
+
+static inline void add_to_ephe_ref_table (struct caml_ephe_ref_table *tbl,
+ value ar, mlsize_t offset)
+{
+ struct caml_ephe_ref_elt *ephe_ref;
+ if (tbl->ptr >= tbl->limit){
+ CAMLassert (tbl->ptr == tbl->limit);
+ caml_realloc_ephe_ref_table (tbl);
+ }
+ ephe_ref = tbl->ptr++;
+ ephe_ref->ephe = ar;
+ ephe_ref->offset = offset;
+ CAMLassert(ephe_ref->offset < Wosize_val(ephe_ref->ephe));
+}
+
+static inline void add_to_custom_table (struct caml_custom_table *tbl, value v,
+ mlsize_t mem, mlsize_t max)
+{
+ struct caml_custom_elt *elt;
+ if (tbl->ptr >= tbl->limit){
+ CAMLassert (tbl->ptr == tbl->limit);
+ caml_realloc_custom_table (tbl);
+ }
+ elt = tbl->ptr++;
+ elt->block = v;
+ elt->mem = mem;
+ elt->max = max;
+}
+
+#endif /* CAML_MINOR_GC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/misc.h b/test/monniaux/ocaml/byterun/caml/misc.h
new file mode 100644
index 00000000..5d668e56
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/misc.h
@@ -0,0 +1,525 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Miscellaneous macros and variables. */
+
+#ifndef CAML_MISC_H
+#define CAML_MISC_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "config.h"
+
+/* Standard definitions */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+/* Basic types and constants */
+
+typedef size_t asize_t;
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+#ifdef CAML_INTERNALS
+typedef char * addr;
+#endif /* CAML_INTERNALS */
+
+/* Noreturn is preserved for compatibility reasons.
+ Instead of the legacy GCC/Clang-only
+ foo Noreturn;
+ you should prefer
+ CAMLnoreturn_start foo CAMLnoreturn_end;
+ which supports both GCC/Clang and MSVC.
+
+ Note: CAMLnoreturn is a different macro defined in memory.h,
+ to be used in function bodies rather than as a prototype attribute.
+*/
+#ifdef __GNUC__
+ /* Works only in GCC 2.5 and later */
+ #define CAMLnoreturn_start
+ #define CAMLnoreturn_end __attribute__ ((noreturn))
+ #define Noreturn __attribute__ ((noreturn))
+#elif _MSC_VER >= 1500
+ #define CAMLnoreturn_start __declspec(noreturn)
+ #define CAMLnoreturn_end
+ #define Noreturn
+#else
+ #define CAMLnoreturn_start
+ #define CAMLnoreturn_end
+ #define Noreturn
+#endif
+
+
+
+/* Export control (to mark primitives and to handle Windows DLL) */
+
+#define CAMLexport
+#define CAMLprim
+#define CAMLextern extern
+
+/* Weak function definitions that can be overridden by external libs */
+/* Conservatively restricted to ELF and MacOSX platforms */
+#if defined(__GNUC__) && (defined (__ELF__) || defined(__APPLE__))
+#define CAMLweakdef __attribute__((weak))
+#else
+#define CAMLweakdef
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* GC timing hooks. These can be assigned by the user.
+ [caml_minor_gc_begin_hook] must not allocate nor change any heap value.
+ The others can allocate and even call back to OCaml code.
+*/
+typedef void (*caml_timing_hook) (void);
+extern caml_timing_hook caml_major_slice_begin_hook, caml_major_slice_end_hook;
+extern caml_timing_hook caml_minor_gc_begin_hook, caml_minor_gc_end_hook;
+extern caml_timing_hook caml_finalise_begin_hook, caml_finalise_end_hook;
+
+/* Assertions */
+
+#ifdef DEBUG
+#define CAMLassert(x) \
+ ((x) ? (void) 0 : (void) caml_failed_assert ( #x , __FILE__, __LINE__))
+CAMLnoreturn_start
+CAMLextern int caml_failed_assert (char *, char *, int)
+CAMLnoreturn_end;
+#else
+#define CAMLassert(x) ((void) 0)
+#endif
+
+CAMLnoreturn_start
+CAMLextern void caml_fatal_error (char *msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_fatal_error_arg (char *fmt, char *arg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+CAMLextern void caml_fatal_error_arg2 (char *fmt1, char *arg1,
+ char *fmt2, char *arg2)
+CAMLnoreturn_end;
+
+/* Detection of available C built-in functions, the Clang way. */
+
+#ifdef __has_builtin
+#define Caml_has_builtin(x) __has_builtin(x)
+#else
+#define Caml_has_builtin(x) 0
+#endif
+
+/* Integer arithmetic with overflow detection.
+ The functions return 0 if no overflow, 1 if overflow.
+ The result of the operation is always stored at [*res].
+ If no overflow is reported, this is the exact result.
+ If overflow is reported, this is the exact result modulo 2 to the word size.
+*/
+
+static inline int caml_uadd_overflow(uintnat a, uintnat b, uintnat * res)
+{
+#if __GNUC__ >= 5 || Caml_has_builtin(__builtin_add_overflow)
+ return __builtin_add_overflow(a, b, res);
+#else
+ uintnat c = a + b;
+ *res = c;
+ return c < a;
+#endif
+}
+
+static inline int caml_usub_overflow(uintnat a, uintnat b, uintnat * res)
+{
+#if __GNUC__ >= 5 || Caml_has_builtin(__builtin_sub_overflow)
+ return __builtin_sub_overflow(a, b, res);
+#else
+ uintnat c = a - b;
+ *res = c;
+ return a < b;
+#endif
+}
+
+#if __GNUC__ >= 5 || Caml_has_builtin(__builtin_mul_overflow)
+static inline int caml_umul_overflow(uintnat a, uintnat b, uintnat * res)
+{
+ return __builtin_mul_overflow(a, b, res);
+}
+#else
+extern int caml_umul_overflow(uintnat a, uintnat b, uintnat * res);
+#endif
+
+/* Windows Unicode support */
+
+#ifdef _WIN32
+
+typedef wchar_t char_os;
+
+#define _T(x) L ## x
+
+#define access_os _waccess
+#define open_os _wopen
+#define stat_os _wstati64
+#define unlink_os _wunlink
+#define rename_os caml_win32_rename
+#define chdir_os _wchdir
+#define getcwd_os _wgetcwd
+#define system_os _wsystem
+#define rmdir_os _wrmdir
+#define putenv_os _wputenv
+#define chmod_os _wchmod
+#define execv_os _wexecv
+#define execve_os _wexecve
+#define execvp_os _wexecvp
+#define execvpe_os _wexecvpe
+#define strcmp_os wcscmp
+#define strlen_os wcslen
+#define sscanf_os swscanf
+
+#define caml_stat_strdup_os caml_stat_wcsdup
+#define caml_stat_strconcat_os caml_stat_wcsconcat
+
+#define caml_stat_strdup_to_os caml_stat_strdup_to_utf16
+#define caml_stat_strdup_of_os caml_stat_strdup_of_utf16
+#define caml_copy_string_of_os caml_copy_string_of_utf16
+
+#else /* _WIN32 */
+
+typedef char char_os;
+
+#define _T(x) x
+
+#define access_os access
+#define open_os open
+#define stat_os stat
+#define unlink_os unlink
+#define rename_os rename
+#define chdir_os chdir
+#define getcwd_os getcwd
+#define system_os system
+#define rmdir_os rmdir
+#define putenv_os putenv
+#define chmod_os chmod
+#define execv_os execv
+#define execve_os execve
+#define execvp_os execvp
+#define execvpe_os execvpe
+#define strcmp_os strcmp
+#define strlen_os strlen
+#define sscanf_os sscanf
+
+#define caml_stat_strdup_os caml_stat_strdup
+#define caml_stat_strconcat_os caml_stat_strconcat
+
+#define caml_stat_strdup_to_os caml_stat_strdup
+#define caml_stat_strdup_of_os caml_stat_strdup
+#define caml_copy_string_of_os caml_copy_string
+
+#endif /* _WIN32 */
+
+
+/* Use macros for some system calls being called from OCaml itself.
+ These calls can be either traced for security reasons, or changed to
+ virtualize the program. */
+
+
+#ifndef CAML_WITH_CPLUGINS
+
+#define CAML_SYS_EXIT(retcode) exit(retcode)
+#define CAML_SYS_OPEN(filename,flags,perm) open_os(filename,flags,perm)
+#define CAML_SYS_CLOSE(fd) close(fd)
+#define CAML_SYS_STAT(filename,st) stat_os(filename,st)
+#define CAML_SYS_UNLINK(filename) unlink_os(filename)
+#define CAML_SYS_RENAME(old_name,new_name) rename_os(old_name, new_name)
+#define CAML_SYS_CHDIR(dirname) chdir_os(dirname)
+#define CAML_SYS_GETENV(varname) getenv(varname)
+#define CAML_SYS_SYSTEM(command) system_os(command)
+#define CAML_SYS_READ_DIRECTORY(dirname,tbl) caml_read_directory(dirname,tbl)
+
+#else
+
+
+#define CAML_CPLUGINS_EXIT 0
+#define CAML_CPLUGINS_OPEN 1
+#define CAML_CPLUGINS_CLOSE 2
+#define CAML_CPLUGINS_STAT 3
+#define CAML_CPLUGINS_UNLINK 4
+#define CAML_CPLUGINS_RENAME 5
+#define CAML_CPLUGINS_CHDIR 6
+#define CAML_CPLUGINS_GETENV 7
+#define CAML_CPLUGINS_SYSTEM 8
+#define CAML_CPLUGINS_READ_DIRECTORY 9
+#define CAML_CPLUGINS_PRIMS_MAX 9
+
+#define CAML_CPLUGINS_PRIMS_BITMAP ((1 << CAML_CPLUGINS_PRIMS_MAX)-1)
+
+extern intnat (*caml_cplugins_prim)(int,intnat,intnat,intnat);
+
+#define CAML_SYS_PRIM_1(code,prim,arg1) \
+ (caml_cplugins_prim == NULL) ? prim(arg1) : \
+ caml_cplugins_prim(code,(intnat) (arg1),0,0)
+#define CAML_SYS_STRING_PRIM_1(code,prim,arg1) \
+ (caml_cplugins_prim == NULL) ? prim(arg1) : \
+ (char_os*)caml_cplugins_prim(code,(intnat) (arg1),0,0)
+#define CAML_SYS_VOID_PRIM_1(code,prim,arg1) \
+ (caml_cplugins_prim == NULL) ? prim(arg1) : \
+ (void)caml_cplugins_prim(code,(intnat) (arg1),0,0)
+#define CAML_SYS_PRIM_2(code,prim,arg1,arg2) \
+ (caml_cplugins_prim == NULL) ? prim(arg1,arg2) : \
+ caml_cplugins_prim(code,(intnat) (arg1), (intnat) (arg2),0)
+#define CAML_SYS_PRIM_3(code,prim,arg1,arg2,arg3) \
+ (caml_cplugins_prim == NULL) ? prim(arg1,arg2,arg3) : \
+ caml_cplugins_prim(code,(intnat) (arg1), (intnat) (arg2),(intnat) (arg3))
+
+#define CAML_SYS_EXIT(retcode) \
+ CAML_SYS_VOID_PRIM_1(CAML_CPLUGINS_EXIT,exit,retcode)
+#define CAML_SYS_OPEN(filename,flags,perm) \
+ CAML_SYS_PRIM_3(CAML_CPLUGINS_OPEN,open_os,filename,flags,perm)
+#define CAML_SYS_CLOSE(fd) \
+ CAML_SYS_PRIM_1(CAML_CPLUGINS_CLOSE,close,fd)
+#define CAML_SYS_STAT(filename,st) \
+ CAML_SYS_PRIM_2(CAML_CPLUGINS_STAT,stat_os,filename,st)
+#define CAML_SYS_UNLINK(filename) \
+ CAML_SYS_PRIM_1(CAML_CPLUGINS_UNLINK,unlink_os,filename)
+#define CAML_SYS_RENAME(old_name,new_name) \
+ CAML_SYS_PRIM_2(CAML_CPLUGINS_RENAME,rename_os,old_name,new_name)
+#define CAML_SYS_CHDIR(dirname) \
+ CAML_SYS_PRIM_1(CAML_CPLUGINS_CHDIR,chdir_os,dirname)
+#define CAML_SYS_GETENV(varname) \
+ CAML_SYS_STRING_PRIM_1(CAML_CPLUGINS_GETENV,getenv,varname)
+#define CAML_SYS_SYSTEM(command) \
+ CAML_SYS_PRIM_1(CAML_CPLUGINS_SYSTEM,system_os,command)
+#define CAML_SYS_READ_DIRECTORY(dirname,tbl) \
+ CAML_SYS_PRIM_2(CAML_CPLUGINS_READ_DIRECTORY,caml_read_directory, \
+ dirname,tbl)
+
+#define CAML_CPLUGIN_CONTEXT_API 0
+
+struct cplugin_context {
+ int api_version;
+ int prims_bitmap;
+ char_os *exe_name;
+ char_os** argv;
+ char_os *plugin; /* absolute filename of plugin, do a copy if you need it ! */
+ char *ocaml_version;
+/* end of CAML_CPLUGIN_CONTEXT_API version 0 */
+};
+
+extern void caml_cplugins_init(char_os * exe_name, char_os **argv);
+
+/* A plugin MUST define a symbol "caml_cplugin_init" with the prototype:
+
+void caml_cplugin_init(struct cplugin_context *ctx)
+*/
+
+/* to write plugins for CAML_SYS_READ_DIRECTORY, we will need the
+ definition of struct ext_table to be public. */
+
+#endif /* CAML_WITH_CPLUGINS */
+
+/* Data structures */
+
+struct ext_table {
+ int size;
+ int capacity;
+ void ** contents;
+};
+
+extern void caml_ext_table_init(struct ext_table * tbl, int init_capa);
+extern int caml_ext_table_add(struct ext_table * tbl, void * data);
+extern void caml_ext_table_remove(struct ext_table * tbl, void * data);
+extern void caml_ext_table_free(struct ext_table * tbl, int free_entries);
+extern void caml_ext_table_clear(struct ext_table * tbl, int free_entries);
+
+CAMLextern int caml_read_directory(char_os * dirname, struct ext_table * contents);
+
+/* Deprecated aliases */
+#define caml_aligned_malloc caml_stat_alloc_aligned_noexc
+#define caml_strdup caml_stat_strdup
+#define caml_strconcat caml_stat_strconcat
+
+#ifdef CAML_INTERNALS
+
+/* GC flags and messages */
+
+extern uintnat caml_verb_gc;
+void caml_gc_message (int, char *, ...)
+#ifdef __GNUC__
+ __attribute__ ((format (printf, 2, 3)))
+#endif
+;
+
+/* Runtime warnings */
+extern uintnat caml_runtime_warnings;
+int caml_runtime_warnings_active(void);
+
+#ifdef DEBUG
+#ifdef ARCH_SIXTYFOUR
+#define Debug_tag(x) (0xD700D7D7D700D6D7ul \
+ | ((uintnat) (x) << 16) \
+ | ((uintnat) (x) << 48))
+#else
+#define Debug_tag(x) (0xD700D6D7ul | ((uintnat) (x) << 16))
+#endif /* ARCH_SIXTYFOUR */
+
+/*
+ 00 -> free words in minor heap
+ 01 -> fields of free list blocks in major heap
+ 03 -> heap chunks deallocated by heap shrinking
+ 04 -> fields deallocated by [caml_obj_truncate]
+ 10 -> uninitialised fields of minor objects
+ 11 -> uninitialised fields of major objects
+ 15 -> uninitialised words of [caml_stat_alloc_aligned] blocks
+ 85 -> filler bytes of [caml_stat_alloc_aligned]
+ 99 -> the magic prefix of a memory block allocated by [caml_stat_alloc]
+
+ special case (byte by byte):
+ D7 -> uninitialised words of [caml_stat_alloc] blocks
+*/
+#define Debug_free_minor Debug_tag (0x00)
+#define Debug_free_major Debug_tag (0x01)
+#define Debug_free_shrink Debug_tag (0x03)
+#define Debug_free_truncate Debug_tag (0x04)
+#define Debug_uninit_minor Debug_tag (0x10)
+#define Debug_uninit_major Debug_tag (0x11)
+#define Debug_uninit_align Debug_tag (0x15)
+#define Debug_filler_align Debug_tag (0x85)
+#define Debug_pool_magic Debug_tag (0x99)
+
+#define Debug_uninit_stat 0xD7
+
+/* Note: the first argument is in fact a [value] but we don't have this
+ type available yet because we can't include [mlvalues.h] in this file.
+*/
+extern void caml_set_fields (intnat v, unsigned long, unsigned long);
+#endif /* DEBUG */
+
+
+/* snprintf emulation for Win32 */
+
+#if defined(_WIN32) && !defined(_UCRT)
+extern int caml_snprintf(char * buf, size_t size, const char * format, ...);
+#define snprintf caml_snprintf
+#endif
+
+#ifdef CAML_INSTR
+/* Timers and counters for GC latency profiling (Linux-only) */
+
+#include <time.h>
+#include <stdio.h>
+
+extern intnat caml_stat_minor_collections;
+extern intnat CAML_INSTR_STARTTIME, CAML_INSTR_STOPTIME;
+
+struct CAML_INSTR_BLOCK {
+ struct timespec ts[10];
+ char *tag[10];
+ int index;
+ struct CAML_INSTR_BLOCK *next;
+};
+
+extern struct CAML_INSTR_BLOCK *CAML_INSTR_LOG;
+
+/* Declare a timer/counter name. [t] must be a new variable name. */
+#define CAML_INSTR_DECLARE(t) \
+ struct CAML_INSTR_BLOCK *t = NULL
+
+/* Allocate the data block for a given name.
+ [t] must have been declared with [CAML_INSTR_DECLARE]. */
+#define CAML_INSTR_ALLOC(t) do{ \
+ if (caml_stat_minor_collections >= CAML_INSTR_STARTTIME \
+ && caml_stat_minor_collections < CAML_INSTR_STOPTIME){ \
+ t = caml_stat_alloc_noexc (sizeof (struct CAML_INSTR_BLOCK)); \
+ t->index = 0; \
+ t->tag[0] = ""; \
+ t->next = CAML_INSTR_LOG; \
+ CAML_INSTR_LOG = t; \
+ } \
+ }while(0)
+
+/* Allocate the data block and start the timer.
+ [t] must have been declared with [CAML_INSTR_DECLARE]
+ and allocated with [CAML_INSTR_ALLOC]. */
+#define CAML_INSTR_START(t, msg) do{ \
+ if (t != NULL){ \
+ t->tag[0] = msg; \
+ clock_gettime (CLOCK_REALTIME, &(t->ts[0])); \
+ } \
+ }while(0)
+
+/* Declare a timer, allocate its data, and start it.
+ [t] must be a new variable name. */
+#define CAML_INSTR_SETUP(t, msg) \
+ CAML_INSTR_DECLARE (t); \
+ CAML_INSTR_ALLOC (t); \
+ CAML_INSTR_START (t, msg)
+
+/* Record an intermediate time within a given timer.
+ [t] must have been declared, allocated, and started. */
+#define CAML_INSTR_TIME(t, msg) do{ \
+ if (t != NULL){ \
+ ++ t->index; \
+ t->tag[t->index] = (msg); \
+ clock_gettime (CLOCK_REALTIME, &(t->ts[t->index])); \
+ } \
+ }while(0)
+
+/* Record an integer data point.
+ If [msg] ends with # it will be interpreted as an integer-valued event.
+ If it ends with @ it will be interpreted as an event counter.
+*/
+#define CAML_INSTR_INT(msg, data) do{ \
+ CAML_INSTR_SETUP (__caml_tmp, ""); \
+ if (__caml_tmp != NULL){ \
+ __caml_tmp->index = 1; \
+ __caml_tmp->tag[1] = msg; \
+ __caml_tmp->ts[1].tv_sec = 0; \
+ __caml_tmp->ts[1].tv_nsec = (data); \
+ } \
+ }while(0)
+
+/* This function is called at the start of the program to set up
+ the data for the above macros.
+*/
+extern void CAML_INSTR_INIT (void);
+
+/* This function is automatically called by the runtime to output
+ the collected data to the dump file. */
+extern void CAML_INSTR_ATEXIT (void);
+
+#else /* CAML_INSTR */
+
+#define CAML_INSTR_DECLARE(t) /**/
+#define CAML_INSTR_ALLOC(t) /**/
+#define CAML_INSTR_START(t, name) /**/
+#define CAML_INSTR_SETUP(t, name) /**/
+#define CAML_INSTR_TIME(t, msg) /**/
+#define CAML_INSTR_INT(msg, c) /**/
+#define CAML_INSTR_INIT() /**/
+#define CAML_INSTR_ATEXIT() /**/
+
+#endif /* CAML_INSTR */
+
+#endif /* CAML_INTERNALS */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_MISC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/mlvalues.h b/test/monniaux/ocaml/byterun/caml/mlvalues.h
new file mode 100644
index 00000000..ec30b20a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/mlvalues.h
@@ -0,0 +1,377 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_MLVALUES_H
+#define CAML_MLVALUES_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "config.h"
+#include "misc.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Definitions
+
+ word: Four bytes on 32 and 16 bit architectures,
+ eight bytes on 64 bit architectures.
+ long: A C integer having the same number of bytes as a word.
+ val: The ML representation of something. A long or a block or a pointer
+ outside the heap. If it is a block, it is the (encoded) address
+ of an object. If it is a long, it is encoded as well.
+ block: Something allocated. It always has a header and some
+ fields or some number of bytes (a multiple of the word size).
+ field: A word-sized val which is part of a block.
+ bp: Pointer to the first byte of a block. (a char *)
+ op: Pointer to the first field of a block. (a value *)
+ hp: Pointer to the header of a block. (a char *)
+ int32_t: Four bytes on all architectures.
+ int64_t: Eight bytes on all architectures.
+
+ Remark: A block size is always a multiple of the word size, and at least
+ one word plus the header.
+
+ bosize: Size (in bytes) of the "bytes" part.
+ wosize: Size (in words) of the "fields" part.
+ bhsize: Size (in bytes) of the block with its header.
+ whsize: Size (in words) of the block with its header.
+
+ hd: A header.
+ tag: The value of the tag field of the header.
+ color: The value of the color field of the header.
+ This is for use only by the GC.
+*/
+
+typedef intnat value;
+typedef uintnat header_t;
+typedef uintnat mlsize_t;
+typedef unsigned int tag_t; /* Actually, an unsigned char */
+typedef uintnat color_t;
+typedef uintnat mark_t;
+
+/* Longs vs blocks. */
+#define Is_long(x) (((x) & 1) != 0)
+#define Is_block(x) (((x) & 1) == 0)
+
+/* Conversion macro names are always of the form "to_from". */
+/* Example: Val_long as in "Val from long" or "Val of long". */
+#define Val_long(x) ((intnat) (((uintnat)(x) << 1)) + 1)
+#define Long_val(x) ((x) >> 1)
+#define Max_long (((intnat)1 << (8 * sizeof(value) - 2)) - 1)
+#define Min_long (-((intnat)1 << (8 * sizeof(value) - 2)))
+#define Val_int(x) Val_long(x)
+#define Int_val(x) ((int) Long_val(x))
+#define Unsigned_long_val(x) ((uintnat)(x) >> 1)
+#define Unsigned_int_val(x) ((int) Unsigned_long_val(x))
+
+/* Structure of the header:
+
+For 16-bit and 32-bit architectures:
+ +--------+-------+-----+
+ | wosize | color | tag |
+ +--------+-------+-----+
+bits 31 10 9 8 7 0
+
+For 64-bit architectures:
+
+ +--------+-------+-----+
+ | wosize | color | tag |
+ +--------+-------+-----+
+bits 63 10 9 8 7 0
+
+For x86-64 with Spacetime profiling:
+ P = PROFINFO_WIDTH (as set by "configure", currently 26 bits, giving a
+ maximum block size of just under 4Gb)
+ +----------------+----------------+-------------+
+ | profiling info | wosize | color | tag |
+ +----------------+----------------+-------------+
+bits 63 (64-P) (63-P) 10 9 8 7 0
+
+*/
+
+#define Tag_hd(hd) ((tag_t) ((hd) & 0xFF))
+
+#define Gen_profinfo_shift(width) (64 - (width))
+#define Gen_profinfo_mask(width) ((1ull << (width)) - 1ull)
+#define Gen_profinfo_hd(width, hd) \
+ (((mlsize_t) ((hd) >> (Gen_profinfo_shift(width)))) \
+ & (Gen_profinfo_mask(width)))
+
+#ifdef WITH_PROFINFO
+#define PROFINFO_SHIFT (Gen_profinfo_shift(PROFINFO_WIDTH))
+#define PROFINFO_MASK (Gen_profinfo_mask(PROFINFO_WIDTH))
+#define Hd_no_profinfo(hd) ((hd) & ~(PROFINFO_MASK << PROFINFO_SHIFT))
+#define Wosize_hd(hd) ((mlsize_t) ((Hd_no_profinfo(hd)) >> 10))
+#define Profinfo_hd(hd) (Gen_profinfo_hd(PROFINFO_WIDTH, hd))
+#else
+#define Wosize_hd(hd) ((mlsize_t) ((hd) >> 10))
+#endif /* WITH_PROFINFO */
+
+#define Hd_val(val) (((header_t *) (val)) [-1]) /* Also an l-value. */
+#define Hd_op(op) (Hd_val (op)) /* Also an l-value. */
+#define Hd_bp(bp) (Hd_val (bp)) /* Also an l-value. */
+#define Hd_hp(hp) (* ((header_t *) (hp))) /* Also an l-value. */
+#define Hp_val(val) (((header_t *) (val)) - 1)
+#define Hp_op(op) (Hp_val (op))
+#define Hp_bp(bp) (Hp_val (bp))
+#define Val_op(op) ((value) (op))
+#define Val_hp(hp) ((value) (((header_t *) (hp)) + 1))
+#define Op_hp(hp) ((value *) Val_hp (hp))
+#define Bp_hp(hp) ((char *) Val_hp (hp))
+
+#define Num_tags (1 << 8)
+#ifdef ARCH_SIXTYFOUR
+#define Max_wosize (((intnat)1 << (54-PROFINFO_WIDTH)) - 1)
+#else
+#define Max_wosize ((1 << 22) - 1)
+#endif /* ARCH_SIXTYFOUR */
+
+#define Wosize_val(val) (Wosize_hd (Hd_val (val)))
+#define Wosize_op(op) (Wosize_val (op))
+#define Wosize_bp(bp) (Wosize_val (bp))
+#define Wosize_hp(hp) (Wosize_hd (Hd_hp (hp)))
+#define Whsize_wosize(sz) ((sz) + 1)
+#define Wosize_whsize(sz) ((sz) - 1)
+#define Wosize_bhsize(sz) ((sz) / sizeof (value) - 1)
+#define Bsize_wsize(sz) ((sz) * sizeof (value))
+#define Wsize_bsize(sz) ((sz) / sizeof (value))
+#define Bhsize_wosize(sz) (Bsize_wsize (Whsize_wosize (sz)))
+#define Bhsize_bosize(sz) ((sz) + sizeof (header_t))
+#define Bosize_val(val) (Bsize_wsize (Wosize_val (val)))
+#define Bosize_op(op) (Bosize_val (Val_op (op)))
+#define Bosize_bp(bp) (Bosize_val (Val_bp (bp)))
+#define Bosize_hd(hd) (Bsize_wsize (Wosize_hd (hd)))
+#define Whsize_hp(hp) (Whsize_wosize (Wosize_hp (hp)))
+#define Whsize_val(val) (Whsize_hp (Hp_val (val)))
+#define Whsize_bp(bp) (Whsize_val (Val_bp (bp)))
+#define Whsize_hd(hd) (Whsize_wosize (Wosize_hd (hd)))
+#define Bhsize_hp(hp) (Bsize_wsize (Whsize_hp (hp)))
+#define Bhsize_hd(hd) (Bsize_wsize (Whsize_hd (hd)))
+
+#define Profinfo_val(val) (Profinfo_hd (Hd_val (val)))
+
+#ifdef ARCH_BIG_ENDIAN
+#define Tag_val(val) (((unsigned char *) (val)) [-1])
+ /* Also an l-value. */
+#define Tag_hp(hp) (((unsigned char *) (hp)) [sizeof(value)-1])
+ /* Also an l-value. */
+#else
+#define Tag_val(val) (((unsigned char *) (val)) [-sizeof(value)])
+ /* Also an l-value. */
+#define Tag_hp(hp) (((unsigned char *) (hp)) [0])
+ /* Also an l-value. */
+#endif
+
+/* The lowest tag for blocks containing no value. */
+#define No_scan_tag 251
+
+
+/* 1- If tag < No_scan_tag : a tuple of fields. */
+
+/* Pointer to the first field. */
+#define Op_val(x) ((value *) (x))
+/* Fields are numbered from 0. */
+#define Field(x, i) (((value *)(x)) [i]) /* Also an l-value. */
+
+typedef int32_t opcode_t;
+typedef opcode_t * code_t;
+
+/* NOTE: [Forward_tag] and [Infix_tag] must be just under
+ [No_scan_tag], with [Infix_tag] the lower one.
+ See [caml_oldify_one] in minor_gc.c for more details.
+
+ NOTE: Update stdlib/obj.ml whenever you change the tags.
+ */
+
+/* Forward_tag: forwarding pointer that the GC may silently shortcut.
+ See stdlib/lazy.ml. */
+#define Forward_tag 250
+#define Forward_val(v) Field(v, 0)
+
+/* If tag == Infix_tag : an infix header inside a closure */
+/* Infix_tag must be odd so that the infix header is scanned as an integer */
+/* Infix_tag must be 1 modulo 4 and infix headers can only occur in blocks
+ with tag Closure_tag (see compact.c). */
+
+#define Infix_tag 249
+#define Infix_offset_hd(hd) (Bosize_hd(hd))
+#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
+
+/* Another special case: objects */
+#define Object_tag 248
+#define Class_val(val) Field((val), 0)
+#define Oid_val(val) Long_val(Field((val), 1))
+CAMLextern value caml_get_public_method (value obj, value tag);
+/* Called as:
+ caml_callback(caml_get_public_method(obj, caml_hash_variant(name)), obj) */
+/* caml_get_public_method returns 0 if tag not in the table.
+ Note however that tags being hashed, same tag does not necessarily mean
+ same method name. */
+
+/* Special case of tuples of fields: closures */
+#define Closure_tag 247
+#define Code_val(val) (((code_t *) (val)) [0]) /* Also an l-value. */
+
+/* This tag is used (with Forward_tag) to implement lazy values.
+ See major_gc.c and stdlib/lazy.ml. */
+#define Lazy_tag 246
+
+/* Another special case: variants */
+CAMLextern value caml_hash_variant(char const * tag);
+
+/* 2- If tag >= No_scan_tag : a sequence of bytes. */
+
+/* Pointer to the first byte */
+#define Bp_val(v) ((char *) (v))
+#define Val_bp(p) ((value) (p))
+/* Bytes are numbered from 0. */
+#define Byte(x, i) (((char *) (x)) [i]) /* Also an l-value. */
+#define Byte_u(x, i) (((unsigned char *) (x)) [i]) /* Also an l-value. */
+
+/* Abstract things. Their contents is not traced by the GC; therefore they
+ must not contain any [value]. Must have odd number so that headers with
+ this tag cannot be mistaken for pointers (see caml_obj_truncate).
+*/
+#define Abstract_tag 251
+#define Data_abstract_val(v) ((void*) Op_val(v))
+
+/* Strings. */
+#define String_tag 252
+#ifdef CAML_SAFE_STRING
+#define String_val(x) ((const char *) Bp_val(x))
+#else
+#define String_val(x) ((char *) Bp_val(x))
+#endif
+#define Bytes_val(x) ((unsigned char *) Bp_val(x))
+CAMLextern mlsize_t caml_string_length (value); /* size in bytes */
+CAMLextern int caml_string_is_c_safe (value);
+ /* true if string contains no '\0' null characters */
+
+/* Floating-point numbers. */
+#define Double_tag 253
+#define Double_wosize ((sizeof(double) / sizeof(value)))
+#ifndef ARCH_ALIGN_DOUBLE
+#define Double_val(v) (* (double *)(v))
+#define Store_double_val(v,d) (* (double *)(v) = (d))
+#else
+CAMLextern double caml_Double_val (value);
+CAMLextern void caml_Store_double_val (value,double);
+#define Double_val(v) caml_Double_val(v)
+#define Store_double_val(v,d) caml_Store_double_val(v,d)
+#endif
+
+/* Arrays of floating-point numbers. */
+#define Double_array_tag 254
+
+/* The [_flat_field] macros are for [floatarray] values and float-only records.
+*/
+#define Double_flat_field(v,i) Double_val((value)((double *)(v) + (i)))
+#define Store_double_flat_field(v,i,d) do{ \
+ mlsize_t caml__temp_i = (i); \
+ double caml__temp_d = (d); \
+ Store_double_val((value)((double *) (v) + caml__temp_i), caml__temp_d); \
+}while(0)
+
+/* The [_array_field] macros are for [float array]. */
+#ifdef FLAT_FLOAT_ARRAY
+ #define Double_array_field(v,i) Double_flat_field(v,i)
+ #define Store_double_array_field(v,i,d) Store_double_flat_field(v,i,d)
+#else
+ #define Double_array_field(v,i) Double_val (Field(v,i))
+ CAMLextern void caml_Store_double_array_field (value, mlsize_t, double);
+ #define Store_double_array_field(v,i,d) caml_Store_double_array_field (v,i,d)
+#endif
+
+/* The old [_field] macros are for backward compatibility only.
+ They work with [floatarray], float-only records, and [float array]. */
+#ifdef FLAT_FLOAT_ARRAY
+ #define Double_field(v,i) Double_flat_field(v,i)
+ #define Store_double_field(v,i,d) Store_double_flat_field(v,i,d)
+#else
+ static inline double Double_field (value v, mlsize_t i) {
+ if (Tag_val (v) == Double_array_tag){
+ return Double_flat_field (v, i);
+ }else{
+ return Double_array_field (v, i);
+ }
+ }
+ static inline void Store_double_field (value v, mlsize_t i, double d) {
+ if (Tag_val (v) == Double_array_tag){
+ Store_double_flat_field (v, i, d);
+ }else{
+ Store_double_array_field (v, i, d);
+ }
+ }
+#endif /* FLAT_FLOAT_ARRAY */
+
+CAMLextern mlsize_t caml_array_length (value); /* size in items */
+CAMLextern int caml_is_double_array (value); /* 0 is false, 1 is true */
+
+
+/* Custom blocks. They contain a pointer to a "method suite"
+ of functions (for finalization, comparison, hashing, etc)
+ followed by raw data. The contents of custom blocks is not traced by
+ the GC; therefore, they must not contain any [value].
+ See [custom.h] for operations on method suites. */
+#define Custom_tag 255
+#define Data_custom_val(v) ((void *) &Field((v), 1))
+struct custom_operations; /* defined in [custom.h] */
+
+/* Int32.t, Int64.t and Nativeint.t are represented as custom blocks. */
+
+#define Int32_val(v) (*((int32_t *) Data_custom_val(v)))
+#define Nativeint_val(v) (*((intnat *) Data_custom_val(v)))
+#ifndef ARCH_ALIGN_INT64
+#define Int64_val(v) (*((int64_t *) Data_custom_val(v)))
+#else
+CAMLextern int64_t caml_Int64_val(value v);
+#define Int64_val(v) caml_Int64_val(v)
+#endif
+
+/* 3- Atoms are 0-tuples. They are statically allocated once and for all. */
+
+CAMLextern header_t caml_atom_table[];
+#define Atom(tag) (Val_hp (&(caml_atom_table [(tag)])))
+
+/* Booleans are integers 0 or 1 */
+
+#define Val_bool(x) Val_int((x) != 0)
+#define Bool_val(x) Int_val(x)
+#define Val_false Val_int(0)
+#define Val_true Val_int(1)
+#define Val_not(x) (Val_false + Val_true - (x))
+
+/* The unit value is 0 (tagged) */
+
+#define Val_unit Val_int(0)
+
+/* List constructors */
+#define Val_emptylist Val_int(0)
+#define Tag_cons 0
+
+/* The table of global identifiers */
+
+extern value caml_global_data;
+
+CAMLextern value caml_set_oo_id(value obj);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_MLVALUES_H */
diff --git a/test/monniaux/ocaml/byterun/caml/osdeps.h b/test/monniaux/ocaml/byterun/caml/osdeps.h
new file mode 100644
index 00000000..b65503d8
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/osdeps.h
@@ -0,0 +1,152 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Operating system - specific stuff */
+
+#ifndef CAML_OSDEPS_H
+#define CAML_OSDEPS_H
+
+#ifdef _WIN32
+extern unsigned short caml_win32_major;
+extern unsigned short caml_win32_minor;
+extern unsigned short caml_win32_build;
+extern unsigned short caml_win32_revision;
+#endif
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "memory.h"
+
+/* Read at most [n] bytes from file descriptor [fd] into buffer [buf].
+ [flags] indicates whether [fd] is a socket
+ (bit [CHANNEL_FLAG_FROM_SOCKET] is set in this case, see [io.h]).
+ (This distinction matters for Win32, but not for Unix.)
+ Return number of bytes read.
+ In case of error, raises [Sys_error] or [Sys_blocked_io]. */
+extern int caml_read_fd(int fd, int flags, void * buf, int n);
+
+/* Write at most [n] bytes from buffer [buf] onto file descriptor [fd].
+ [flags] indicates whether [fd] is a socket
+ (bit [CHANNEL_FLAG_FROM_SOCKET] is set in this case, see [io.h]).
+ (This distinction matters for Win32, but not for Unix.)
+ Return number of bytes written.
+ In case of error, raises [Sys_error] or [Sys_blocked_io]. */
+extern int caml_write_fd(int fd, int flags, void * buf, int n);
+
+/* Decompose the given path into a list of directories, and add them
+ to the given table. */
+extern char_os * caml_decompose_path(struct ext_table * tbl, char_os * path);
+
+/* Search the given file in the given list of directories.
+ If not found, return a copy of [name]. */
+extern char_os * caml_search_in_path(struct ext_table * path, const char_os * name);
+
+/* Same, but search an executable name in the system path for executables. */
+CAMLextern char_os * caml_search_exe_in_path(const char_os * name);
+
+/* Same, but search a shared library in the given path. */
+extern char_os * caml_search_dll_in_path(struct ext_table * path, const char_os * name);
+
+/* Open a shared library and return a handle on it.
+ If [for_execution] is true, perform full symbol resolution and
+ execute initialization code so that functions from the shared library
+ can be called. If [for_execution] is false, functions from this
+ shared library will not be called, but just checked for presence,
+ so symbol resolution can be skipped.
+ If [global] is true, symbols from the shared library can be used
+ to resolve for other libraries to be opened later on.
+ Return [NULL] on error. */
+extern void * caml_dlopen(char_os * libname, int for_execution, int global);
+
+/* Close a shared library handle */
+extern void caml_dlclose(void * handle);
+
+/* Look up the given symbol in the given shared library.
+ Return [NULL] if not found, or symbol value if found. */
+extern void * caml_dlsym(void * handle, const char * name);
+
+extern void * caml_globalsym(const char * name);
+
+/* Return an error message describing the most recent dynlink failure. */
+extern char * caml_dlerror(void);
+
+/* Add to [contents] the (short) names of the files contained in
+ the directory named [dirname]. No entries are added for [.] and [..].
+ Return 0 on success, -1 on error; set errno in the case of error. */
+extern int caml_read_directory(char_os * dirname, struct ext_table * contents);
+
+/* Recover executable name if possible (/proc/sef/exe under Linux,
+ GetModuleFileName under Windows). Return NULL on error,
+ string allocated with [caml_stat_alloc] on success. */
+extern char_os * caml_executable_name(void);
+
+/* Secure version of [getenv]: returns NULL if the process has special
+ privileges (setuid bit, setgid bit, capabilities).
+*/
+extern char_os *caml_secure_getenv(char_os const *var);
+
+/* If [fd] refers to a terminal or console, return the number of rows
+ (lines) that it displays. Otherwise, or if the number of rows
+ cannot be determined, return -1. */
+extern int caml_num_rows_fd(int fd);
+
+#ifdef _WIN32
+
+extern int caml_win32_rename(const wchar_t *, const wchar_t *);
+
+extern void caml_probe_win32_version(void);
+extern void caml_setup_win32_terminal(void);
+extern void caml_restore_win32_terminal(void);
+
+extern wchar_t *caml_win32_getenv(wchar_t const *);
+
+/* Windows Unicode support */
+
+extern int win_multi_byte_to_wide_char(const char* s, int slen, wchar_t *out, int outlen);
+extern int win_wide_char_to_multi_byte(const wchar_t* s, int slen, char *out, int outlen);
+
+/* [caml_stat_strdup_to_utf16(s)] returns a NULL-terminated copy of [s],
+ re-encoded in UTF-16. The encoding of [s] is assumed to be UTF-8 if
+ [caml_windows_unicode_runtime_enabled] is non-zero **and** [s] is valid
+ UTF-8, or the current Windows code page otherwise.
+
+ The returned string is allocated with [caml_stat_alloc], so it should be free
+ using [caml_stat_free].
+*/
+extern wchar_t* caml_stat_strdup_to_utf16(const char *s);
+
+/* [caml_stat_strdup_of_utf16(s)] returns a NULL-terminated copy of [s],
+ re-encoded in UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero or
+ the current Windows code page otherwise.
+
+ The returned string is allocated with [caml_stat_alloc], so it should be free
+ using [caml_stat_free].
+*/
+extern char* caml_stat_strdup_of_utf16(const wchar_t *s);
+
+/* [caml_copy_string_of_utf16(s)] returns an OCaml string containing a copy of
+ [s] re-encoded in UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero
+ or in the current code page otherwise.
+*/
+extern value caml_copy_string_of_utf16(const wchar_t *s);
+
+extern int caml_win32_isatty(int fd);
+
+#endif /* _WIN32 */
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_OSDEPS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/prims.h b/test/monniaux/ocaml/byterun/caml/prims.h
new file mode 100644
index 00000000..147cd98a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/prims.h
@@ -0,0 +1,40 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Interface with C primitives. */
+
+#ifndef CAML_PRIMS_H
+#define CAML_PRIMS_H
+
+#ifdef CAML_INTERNALS
+
+typedef value (*c_primitive)();
+
+extern c_primitive caml_builtin_cprim[];
+extern char * caml_names_of_builtin_cprim[];
+
+extern struct ext_table caml_prim_table;
+#ifdef DEBUG
+extern struct ext_table caml_prim_name_table;
+#endif
+
+#define Primitive(n) ((c_primitive)(caml_prim_table.contents[n]))
+
+extern char * caml_section_table;
+extern asize_t caml_section_table_size;
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_PRIMS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/printexc.h b/test/monniaux/ocaml/byterun/caml/printexc.h
new file mode 100644
index 00000000..92c5af53
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/printexc.h
@@ -0,0 +1,35 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_PRINTEXC_H
+#define CAML_PRINTEXC_H
+
+
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+CAMLextern char * caml_format_exception (value);
+CAMLnoreturn_start void caml_fatal_uncaught_exception (value) CAMLnoreturn_end;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_PRINTEXC_H */
diff --git a/test/monniaux/ocaml/byterun/caml/reverse.h b/test/monniaux/ocaml/byterun/caml/reverse.h
new file mode 100644
index 00000000..a186078e
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/reverse.h
@@ -0,0 +1,92 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Swap byte-order in 16, 32, and 64-bit integers or floats */
+
+#ifndef CAML_REVERSE_H
+#define CAML_REVERSE_H
+
+#ifdef CAML_INTERNALS
+
+#define Reverse_16(dst,src) { \
+ char * _p, * _q; \
+ char _a; \
+ _p = (char *) (src); \
+ _q = (char *) (dst); \
+ _a = _p[0]; \
+ _q[0] = _p[1]; \
+ _q[1] = _a; \
+}
+
+#define Reverse_32(dst,src) { \
+ char * _p, * _q; \
+ char _a, _b; \
+ _p = (char *) (src); \
+ _q = (char *) (dst); \
+ _a = _p[0]; \
+ _b = _p[1]; \
+ _q[0] = _p[3]; \
+ _q[1] = _p[2]; \
+ _q[3] = _a; \
+ _q[2] = _b; \
+}
+
+#define Reverse_64(dst,src) { \
+ char * _p, * _q; \
+ char _a, _b; \
+ _p = (char *) (src); \
+ _q = (char *) (dst); \
+ _a = _p[0]; \
+ _b = _p[1]; \
+ _q[0] = _p[7]; \
+ _q[1] = _p[6]; \
+ _q[7] = _a; \
+ _q[6] = _b; \
+ _a = _p[2]; \
+ _b = _p[3]; \
+ _q[2] = _p[5]; \
+ _q[3] = _p[4]; \
+ _q[5] = _a; \
+ _q[4] = _b; \
+}
+
+#define Perm_index(perm,i) ((perm >> (i * 4)) & 0xF)
+
+#define Permute_64(dst,perm_dst,src,perm_src) { \
+ char * _p; \
+ char _a, _b, _c, _d, _e, _f, _g, _h; \
+ _p = (char *) (src); \
+ _a = _p[Perm_index(perm_src, 0)]; \
+ _b = _p[Perm_index(perm_src, 1)]; \
+ _c = _p[Perm_index(perm_src, 2)]; \
+ _d = _p[Perm_index(perm_src, 3)]; \
+ _e = _p[Perm_index(perm_src, 4)]; \
+ _f = _p[Perm_index(perm_src, 5)]; \
+ _g = _p[Perm_index(perm_src, 6)]; \
+ _h = _p[Perm_index(perm_src, 7)]; \
+ _p = (char *) (dst); \
+ _p[Perm_index(perm_dst, 0)] = _a; \
+ _p[Perm_index(perm_dst, 1)] = _b; \
+ _p[Perm_index(perm_dst, 2)] = _c; \
+ _p[Perm_index(perm_dst, 3)] = _d; \
+ _p[Perm_index(perm_dst, 4)] = _e; \
+ _p[Perm_index(perm_dst, 5)] = _f; \
+ _p[Perm_index(perm_dst, 6)] = _g; \
+ _p[Perm_index(perm_dst, 7)] = _h; \
+}
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_REVERSE_H */
diff --git a/test/monniaux/ocaml/byterun/caml/roots.h b/test/monniaux/ocaml/byterun/caml/roots.h
new file mode 100644
index 00000000..fed345d3
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/roots.h
@@ -0,0 +1,44 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_ROOTS_H
+#define CAML_ROOTS_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "memory.h"
+
+typedef void (*scanning_action) (value, value *);
+
+void caml_oldify_local_roots (void);
+void caml_darken_all_roots_start (void);
+intnat caml_darken_all_roots_slice (intnat);
+void caml_do_roots (scanning_action, int);
+extern uintnat caml_incremental_roots_count;
+#ifndef NATIVE_CODE
+CAMLextern void caml_do_local_roots (scanning_action, value *, value *,
+ struct caml__roots_block *);
+#else
+CAMLextern void caml_do_local_roots(scanning_action f, char * bottom_of_stack,
+ uintnat last_retaddr, value * gc_regs,
+ struct caml__roots_block * local_roots);
+#endif
+
+CAMLextern void (*caml_scan_roots_hook) (scanning_action);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_ROOTS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/s.h b/test/monniaux/ocaml/byterun/caml/s.h
new file mode 100644
index 00000000..add8c1cc
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/s.h
@@ -0,0 +1,217 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Operating system and standard library dependencies. */
+
+/* 0. Operating system type string. */
+
+#define OCAML_OS_TYPE "Unix"
+/* #define OCAML_OS_TYPE "Win32" */
+/* #define OCAML_OS_TYPE "MacOS" */
+#define HAS_STDINT_H 1
+
+/* 1. For the runtime system. */
+
+/* #define POSIX_SIGNALS */
+
+/* Define POSIX_SIGNALS if signal handling is POSIX-compliant.
+ In particular, sigaction(), sigprocmask() and the operations on
+ sigset_t are provided. */
+
+/* #define BSD_SIGNALS */
+
+/* Define BSD_SIGNALS if signal handlers have the BSD semantics: the handler
+ remains attached to the signal when the signal is received. Leave it
+ undefined if signal handlers have the System V semantics: the signal
+ resets the behavior to default. */
+
+/* #define HAS_SIGSETMASK */
+
+/* Define HAS_SIGSETMASK if you have sigsetmask(), as in BSD. */
+
+/* #define SUPPORT_DYNAMIC_LINKING */
+
+/* Define SUPPORT_DYNAMIC_LINKING if dynamic loading of C stub code
+ via dlopen() is available. */
+
+#define HAS_C99_FLOAT_OPS
+
+/* Define HAS_C99_FLOAT_OPS if <math.h> conforms to ISO C99.
+ In particular, it should provide expm1(), log1p(), hypot(), copysign(). */
+
+/* 2. For the Unix library. */
+
+/* #define HAS_SOCKETS */
+
+/* Define HAS_SOCKETS if you have BSD sockets. */
+
+/* #define HAS_SOCKLEN_T */
+
+/* Define HAS_SOCKLEN_T if the type socklen_t is defined in
+ /usr/include/sys/socket.h. */
+
+#define HAS_UNISTD
+
+/* Define HAS_UNISTD if you have /usr/include/unistd.h. */
+
+#define HAS_DIRENT
+
+/* Define HAS_DIRENT if you have /usr/include/dirent.h and the result of
+ readdir() is of type struct dirent *.
+ Otherwise, we'll load /usr/include/sys/dir.h, and readdir() is expected to
+ return a struct direct *. */
+
+/* #define HAS_REWINDDIR */
+
+/* Define HAS_REWINDDIR if you have rewinddir(). */
+
+/* #define HAS_LOCKF */
+
+/* Define HAS_LOCKF if the library provides the lockf() function. */
+
+/* #define HAS_MKFIFO */
+
+/* Define HAS_MKFIFO if the library provides the mkfifo() function. */
+
+/* #define HAS_GETCWD */
+
+/* Define HAS_GETCWD if the library provides the getcwd() function. */
+
+#define HAS_GETPRIORITY
+
+/* Define HAS_GETPRIORITY if the library provides getpriority() and
+ setpriority(). Otherwise, we'll use nice(). */
+
+#define HAS_UTIME
+#define HAS_UTIMES
+
+/* Define HAS_UTIME if you have /usr/include/utime.h and the library
+ provides utime(). Define HAS_UTIMES if the library provides utimes(). */
+
+#define HAS_DUP2
+
+/* Define HAS_DUP2 if you have dup2(). */
+
+#define HAS_FCHMOD
+
+/* Define HAS_FCHMOD if you have fchmod() and fchown(). */
+
+#define HAS_TRUNCATE
+
+/* Define HAS_TRUNCATE if you have truncate() and
+ ftruncate(). */
+
+#define HAS_SELECT
+
+/* Define HAS_SELECT if you have select(). */
+
+#define HAS_SYS_SELECT_H
+
+/* Define HAS_SYS_SELECT_H if /usr/include/sys/select.h exists
+ and should be included before using select(). */
+
+#define HAS_NANOSLEEP
+/* Define HAS_NANOSLEEP if you have nanosleep(). */
+
+#define HAS_SYMLINK
+
+/* Define HAS_SYMLINK if you have symlink() and readlink() and lstat(). */
+
+#define HAS_WAIT4
+#define HAS_WAITPID
+
+/* Define HAS_WAIT4 if you have wait4().
+ Define HAS_WAITPID if you have waitpid(). */
+
+#define HAS_GETGROUPS
+
+/* Define HAS_GETGROUPS if you have getgroups(). */
+
+#define HAS_SETGROUPS
+
+/* Define HAS_SETGROUPS if you have setgroups(). */
+
+#define HAS_INITGROUPS
+
+/* Define HAS_INITGROUPS if you have initgroups(). */
+
+#define HAS_TERMIOS
+
+/* Define HAS_TERMIOS if you have /usr/include/termios.h and it is
+ Posix-compliant. */
+
+#define HAS_ASYNC_IO
+
+/* Define HAS_ASYNC_IO if BSD-style asynchronous I/O are supported
+ (the process can request to be sent a SIGIO signal when a descriptor
+ is ready for reading). */
+
+#define HAS_SETITIMER
+
+/* Define HAS_SETITIMER if you have setitimer(). */
+
+#define HAS_GETHOSTNAME
+
+/* Define HAS_GETHOSTNAME if you have gethostname(). */
+
+#define HAS_UNAME
+
+/* Define HAS_UNAME if you have uname(). */
+
+#define HAS_GETTIMEOFDAY
+
+/* Define HAS_GETTIMEOFDAY if you have gettimeofday(). */
+
+#define HAS_MKTIME
+
+/* Define HAS_MKTIME if you have mktime(). */
+
+#define HAS_SETSID
+
+/* Define HAS_SETSID if you have setsid(). */
+
+#define HAS_PUTENV
+
+/* Define HAS_PUTENV if you have putenv(). */
+
+#define HAS_LOCALE
+
+/* Define HAS_LOCALE if you have the include file <locale.h> and the
+ setlocale() function. */
+
+#define HAS_MMAP
+
+/* Define HAS_MMAP if you have the include file <sys/mman.h> and the
+ functions mmap() and munmap(). */
+
+#define HAS_GETHOSTBYNAME_R 6
+
+/* Define HAS_GETHOSTBYNAME_R if gethostbyname_r() is available.
+ The value of this symbol is the number of arguments of
+ gethostbyname_r(): either 5 or 6 depending on prototype.
+ (5 is the Solaris version, 6 is the Linux version). */
+
+#define HAS_GETHOSTBYADDR_R 8
+
+/* Define HAS_GETHOSTBYADDR_R if gethostbyname_r() is available.
+ The value of this symbol is the number of arguments of
+ gethostbyaddr_r(): either 7 or 8 depending on prototype.
+ (7 is the Solaris version, 8 is the Linux version). */
+
+#define HAS_NICE
+
+/* Define HAS_NICE if you have nice(). */
+
+#define OCAML_STDLIB_DIR "/opt/ccomp/ocaml/4.07.1/lib/ocaml"
diff --git a/test/monniaux/ocaml/byterun/caml/signals.h b/test/monniaux/ocaml/byterun/caml/signals.h
new file mode 100644
index 00000000..99924e4f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/signals.h
@@ -0,0 +1,59 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_SIGNALS_H
+#define CAML_SIGNALS_H
+
+#ifndef CAML_NAME_SPACE
+#include "compatibility.h"
+#endif
+#include "misc.h"
+#include "mlvalues.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef CAML_INTERNALS
+CAMLextern intnat volatile caml_signals_are_pending;
+CAMLextern intnat volatile caml_pending_signals[];
+CAMLextern int volatile caml_something_to_do;
+extern int volatile caml_requested_major_slice;
+extern int volatile caml_requested_minor_gc;
+
+void caml_request_major_slice (void);
+void caml_request_minor_gc (void);
+CAMLextern int caml_convert_signal_number (int);
+CAMLextern int caml_rev_convert_signal_number (int);
+void caml_execute_signal(int signal_number, int in_signal_handler);
+void caml_record_signal(int signal_number);
+void caml_process_pending_signals(void);
+void caml_process_event(void);
+int caml_set_signal_action(int signo, int action);
+
+CAMLextern void (*caml_enter_blocking_section_hook)(void);
+CAMLextern void (*caml_leave_blocking_section_hook)(void);
+CAMLextern int (*caml_try_leave_blocking_section_hook)(void);
+CAMLextern void (* volatile caml_async_action_hook)(void);
+#endif /* CAML_INTERNALS */
+
+CAMLextern void caml_enter_blocking_section (void);
+CAMLextern void caml_leave_blocking_section (void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_SIGNALS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/signals_machdep.h b/test/monniaux/ocaml/byterun/caml/signals_machdep.h
new file mode 100644
index 00000000..ae2145ba
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/signals_machdep.h
@@ -0,0 +1,74 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Processor-specific operation: atomic "read and clear" */
+
+#ifndef CAML_SIGNALS_MACHDEP_H
+#define CAML_SIGNALS_MACHDEP_H
+
+#ifdef CAML_INTERNALS
+
+#if defined(__GNUC__) && defined(__ATOMIC_SEQ_CST) \
+ && defined(__GCC_ATOMIC_LONG_LOCK_FREE) && 0
+
+/* Use the "atomic" builtins of GCC and Clang */
+#define Read_and_clear(dst,src) \
+ ((dst) = __atomic_exchange_n(&(src), 0, __ATOMIC_SEQ_CST))
+
+#elif defined(__GNUC__) && (defined(__i386__) || (defined(__x86_64__) \
+ && defined(__ILP32__)))
+
+#define Read_and_clear(dst,src) \
+ asm("xorl %0, %0; xchgl %0, %1" \
+ : "=r" (dst), "=m" (src) \
+ : "m" (src))
+
+#elif defined(__GNUC__) && defined(__x86_64__)
+
+#define Read_and_clear(dst,src) \
+ asm("xorq %0, %0; xchgq %0, %1" \
+ : "=r" (dst), "=m" (src) \
+ : "m" (src))
+
+#elif defined(__GNUC__) && defined(__ppc__)
+
+#define Read_and_clear(dst,src) \
+ asm("0: lwarx %0, 0, %1\n\t" \
+ "stwcx. %2, 0, %1\n\t" \
+ "bne- 0b" \
+ : "=&r" (dst) \
+ : "r" (&(src)), "r" (0) \
+ : "cr0", "memory")
+
+#elif defined(__GNUC__) && defined(__ppc64__)
+
+#define Read_and_clear(dst,src) \
+ asm("0: ldarx %0, 0, %1\n\t" \
+ "stdcx. %2, 0, %1\n\t" \
+ "bne- 0b" \
+ : "=&r" (dst) \
+ : "r" (&(src)), "r" (0) \
+ : "cr0", "memory")
+
+#else
+
+/* Default, non-atomic implementation */
+#define Read_and_clear(dst,src) ((dst) = (src), (src) = 0)
+
+#endif
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_SIGNALS_MACHDEP_H */
diff --git a/test/monniaux/ocaml/byterun/caml/spacetime.h b/test/monniaux/ocaml/byterun/caml/spacetime.h
new file mode 100644
index 00000000..5bcc9232
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/spacetime.h
@@ -0,0 +1,203 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Mark Shinwell and Leo White, Jane Street Europe */
+/* */
+/* Copyright 2013--2016, Jane Street Group, LLC */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_SPACETIME_H
+#define CAML_SPACETIME_H
+
+#include "io.h"
+#include "misc.h"
+#include "stack.h"
+
+/* Runtime support for Spacetime profiling.
+ * This header file is not intended for the casual user.
+ *
+ * The implementation is split into three files:
+ * 1. spacetime.c: core management of the instrumentation;
+ * 2. spacetime_snapshot.c: the taking of heap snapshots;
+ * 3. spacetime_offline.c: functions that are also used when examining
+ * saved profiling data.
+ */
+
+typedef enum {
+ CALL,
+ ALLOCATION
+} c_node_type;
+
+/* All pointers between nodes point at the word immediately after the
+ GC headers, and everything is traversable using the normal OCaml rules.
+
+ On entry to an OCaml function:
+ If the node hole pointer register has the bottom bit set, then the function
+ is being tail called or called from a self-recursive call site:
+ - If the node hole is empty, the callee must create a new node and link
+ it into the tail chain. The node hole pointer will point at the tail
+ chain.
+ - Otherwise the node should be used as normal.
+ Otherwise (not a tail call):
+ - If the node hole is empty, the callee must create a new node, but the
+ tail chain is untouched.
+ - Otherwise the node should be used as normal.
+*/
+
+/* Classification of nodes (OCaml or C) with corresponding GC tags. */
+#define OCaml_node_tag 0
+#define C_node_tag 1
+#define Is_ocaml_node(node) (Is_block(node) && Tag_val(node) == OCaml_node_tag)
+#define Is_c_node(node) (Is_block(node) && Tag_val(node) == C_node_tag)
+
+/* The header words are:
+ 1. The node program counter.
+ 2. The tail link. */
+#define Node_num_header_words 2
+
+/* The "node program counter" at the start of an OCaml node. */
+#define Node_pc(node) (Field(node, 0))
+#define Encode_node_pc(pc) (((value) pc) | 1)
+#define Decode_node_pc(encoded_pc) ((void*) (encoded_pc & ~1))
+
+/* The circular linked list of tail-called functions within OCaml nodes. */
+#define Tail_link(node) (Field(node, 1))
+
+/* The convention for pointers from OCaml nodes to other nodes. There are
+ two special cases:
+ 1. [Val_unit] means "uninitialized", and further, that this is not a
+ tail call point. (Tail call points are pre-initialized, as in case 2.)
+ 2. If the bottom bit is set, and the value is not [Val_unit], this is a
+ tail call point. */
+#define Encode_tail_caller_node(node) ((node) | 1)
+#define Decode_tail_caller_node(node) ((node) & ~1)
+#define Is_tail_caller_node_encoded(node) (((node) & 1) == 1)
+
+/* Allocation points within OCaml nodes.
+ The "profinfo" value looks exactly like a black Infix_tag header.
+ This enables us to point just after it and return such pointer as a valid
+ OCaml value. (Used for the list of all allocation points. We could do
+ without this and instead just encode the list pointers as integers, but
+ this would mean that the structure was destroyed on marshalling. This
+ might not be a great problem since it is intended that the total counts
+ be obtained via snapshots, but it seems neater and easier to use
+ Infix_tag.
+ The "count" is just an OCaml integer giving the total number of words
+ (including headers) allocated at the point.
+ The "pointer to next allocation point" points to the "count" word of the
+ next allocation point in the linked list of all allocation points.
+ There is no special encoding needed by virtue of the [Infix_tag] trick. */
+#define Alloc_point_profinfo(node, offset) (Field(node, offset))
+#define Alloc_point_count(node, offset) (Field(node, offset + 1))
+#define Alloc_point_next_ptr(node, offset) (Field(node, offset + 2))
+
+/* Direct call points (tail or non-tail) within OCaml nodes.
+ They hold a pointer to the child node and (if the compiler was so
+ configured) a call count.
+ The call site and callee are both recorded in the shape. */
+#define Direct_callee_node(node,offset) (Field(node, offset))
+#define Direct_call_count(node,offset) (Field(node, offset + 1))
+#define Encode_call_point_pc(pc) (((value) pc) | 1)
+#define Decode_call_point_pc(pc) ((void*) (((value) pc) & ~((uintnat) 1)))
+
+/* Indirect call points (tail or non-tail) within OCaml nodes.
+ They hold a linked list of (PC upon entry to the callee, pointer to
+ child node) pairs. The linked list is encoded using C nodes and should
+ be thought of as part of the OCaml node itself. */
+#define Indirect_num_fields 1
+#define Indirect_pc_linked_list(node,offset) (Field(node, offset))
+
+/* Encodings of the program counter value within a C node. */
+#define Encode_c_node_pc_for_call(pc) ((((value) pc) << 2) | 3)
+#define Encode_c_node_pc_for_alloc_point(pc) ((((value) pc) << 2) | 1)
+#define Decode_c_node_pc(pc) ((void*) (((uintnat) (pc)) >> 2))
+
+typedef struct {
+ /* The layout and encoding of this structure must match that of the
+ allocation points within OCaml nodes, so that the linked list
+ traversal across all allocation points works correctly. */
+ value profinfo; /* encoded using [Infix_tag] (see above) */
+ value count;
+ /* [next] is [Val_unit] for the end of the list.
+ Otherwise it points at the second word of this [allocation_point]
+ structure. */
+ value next;
+} allocation_point;
+
+typedef struct {
+ value callee_node;
+ value call_count;
+} call_point;
+
+typedef struct {
+ /* CR-soon mshinwell: delete [gc_header], all the offset arithmetic will
+ then go away */
+ uintnat gc_header;
+ uintnat pc; /* see above for encodings */
+ union {
+ call_point call; /* for CALL */
+ allocation_point allocation; /* for ALLOCATION */
+ } data;
+ value next; /* [Val_unit] for the end of the list */
+} c_node; /* CR-soon mshinwell: rename to dynamic_node */
+
+typedef struct shape_table {
+ uint64_t* table;
+ struct shape_table* next;
+} shape_table;
+
+extern uint64_t** caml_spacetime_static_shape_tables;
+extern shape_table* caml_spacetime_dynamic_shape_tables;
+
+typedef struct ext_table* spacetime_unwind_info_cache;
+
+extern value caml_spacetime_trie_root;
+extern value* caml_spacetime_trie_node_ptr;
+extern value* caml_spacetime_finaliser_trie_root;
+
+extern allocation_point* caml_all_allocation_points;
+
+extern void caml_spacetime_initialize(void);
+extern uintnat caml_spacetime_my_profinfo(
+ spacetime_unwind_info_cache*, uintnat);
+extern c_node_type caml_spacetime_classify_c_node(c_node* node);
+extern c_node* caml_spacetime_c_node_of_stored_pointer(value);
+extern c_node* caml_spacetime_c_node_of_stored_pointer_not_null(value);
+extern value caml_spacetime_stored_pointer_of_c_node(c_node* node);
+extern void caml_spacetime_register_thread(value*, value*);
+extern void caml_spacetime_register_shapes(void*);
+extern value caml_spacetime_frame_table(void);
+extern value caml_spacetime_shape_table(void);
+extern void caml_spacetime_save_snapshot (struct channel *chan,
+ double time_override,
+ int use_time_override);
+extern value caml_spacetime_timestamp(double time_override,
+ int use_time_override);
+extern void caml_spacetime_automatic_snapshot (void);
+
+/* For use in runtime functions that are executed from OCaml
+ code, to save the overhead of using libunwind every time. */
+#ifdef WITH_SPACETIME
+#define Get_my_profinfo_with_cached_backtrace(profinfo, size) \
+ do { \
+ static spacetime_unwind_info_cache spacetime_unwind_info = NULL; \
+ profinfo = caml_spacetime_my_profinfo(&spacetime_unwind_info, size); \
+ } \
+ while (0);
+#else
+#define Get_my_profinfo_with_cached_backtrace(profinfo, size) \
+ profinfo = (uintnat) 0;
+#endif
+
+#else
+
+#define Get_my_profinfo_with_cached_backtrace(profinfo, size) \
+ profinfo = (uintnat) 0;
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/caml/stack.h b/test/monniaux/ocaml/byterun/caml/stack.h
new file mode 100644
index 00000000..26686398
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/stack.h
@@ -0,0 +1,124 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Machine-dependent interface with the asm code */
+
+#ifndef CAML_STACK_H
+#define CAML_STACK_H
+
+#ifdef CAML_INTERNALS
+
+/* Macros to access the stack frame */
+
+#ifdef TARGET_i386
+#define Saved_return_address(sp) *((intnat *)((sp) - 4))
+#ifndef SYS_win32
+#define Callback_link(sp) ((struct caml_context *)((sp) + 16))
+#else
+#define Callback_link(sp) ((struct caml_context *)((sp) + 8))
+#endif
+#endif
+
+#ifdef TARGET_power
+#if defined(MODEL_ppc)
+#define Saved_return_address(sp) *((intnat *)((sp) - 4))
+#define Callback_link(sp) ((struct caml_context *)((sp) + 16))
+#elif defined(MODEL_ppc64)
+#define Saved_return_address(sp) *((intnat *)((sp) + 16))
+#define Callback_link(sp) ((struct caml_context *)((sp) + (48 + 32)))
+#elif defined(MODEL_ppc64le)
+#define Saved_return_address(sp) *((intnat *)((sp) + 16))
+#define Callback_link(sp) ((struct caml_context *)((sp) + (32 + 32)))
+#else
+#error "TARGET_power: wrong MODEL"
+#endif
+#define Already_scanned(sp, retaddr) ((retaddr) & 1)
+#define Mask_already_scanned(retaddr) ((retaddr) & ~1)
+#define Mark_scanned(sp, retaddr) Saved_return_address(sp) = (retaddr) | 1
+#endif
+
+#ifdef TARGET_s390x
+#define Saved_return_address(sp) *((intnat *)((sp) - SIZEOF_PTR))
+#define Trap_frame_size 16
+#define Callback_link(sp) ((struct caml_context *)((sp) + Trap_frame_size))
+#endif
+
+#ifdef TARGET_arm
+#define Saved_return_address(sp) *((intnat *)((sp) - 4))
+#define Callback_link(sp) ((struct caml_context *)((sp) + 8))
+#endif
+
+#ifdef TARGET_amd64
+#define Saved_return_address(sp) *((intnat *)((sp) - 8))
+#define Callback_link(sp) ((struct caml_context *)((sp) + 16))
+#endif
+
+#ifdef TARGET_arm64
+#define Saved_return_address(sp) *((intnat *)((sp) - 8))
+#define Callback_link(sp) ((struct caml_context *)((sp) + 16))
+#endif
+
+/* Structure of OCaml callback contexts */
+
+struct caml_context {
+ char * bottom_of_stack; /* beginning of OCaml stack chunk */
+ uintnat last_retaddr; /* last return address in OCaml code */
+ value * gc_regs; /* pointer to register block */
+#ifdef WITH_SPACETIME
+ void* trie_node;
+#endif
+};
+
+/* Structure of frame descriptors */
+
+typedef struct {
+ uintnat retaddr;
+ unsigned short frame_size;
+ unsigned short num_live;
+ unsigned short live_ofs[1];
+} frame_descr;
+
+/* Hash table of frame descriptors */
+
+extern frame_descr ** caml_frame_descriptors;
+extern int caml_frame_descriptors_mask;
+
+#define Hash_retaddr(addr) \
+ (((uintnat)(addr) >> 3) & caml_frame_descriptors_mask)
+
+extern void caml_init_frame_descriptors(void);
+extern void caml_register_frametable(intnat *);
+extern void caml_unregister_frametable(intnat *);
+extern void caml_register_dyn_global(void *);
+
+extern uintnat caml_stack_usage (void);
+extern uintnat (*caml_stack_usage_hook)(void);
+
+/* Declaration of variables used in the asm code */
+extern char * caml_top_of_stack;
+extern char * caml_bottom_of_stack;
+extern uintnat caml_last_return_address;
+extern value * caml_gc_regs;
+extern char * caml_exception_pointer;
+extern value * caml_globals[];
+extern char caml_globals_map[];
+extern intnat caml_globals_inited;
+extern intnat * caml_frametable[];
+
+CAMLextern frame_descr * caml_next_frame_descriptor(uintnat * pc, char ** sp);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_STACK_H */
diff --git a/test/monniaux/ocaml/byterun/caml/stacks.h b/test/monniaux/ocaml/byterun/caml/stacks.h
new file mode 100644
index 00000000..18ec0ac3
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/stacks.h
@@ -0,0 +1,46 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* structure of the stacks */
+
+#ifndef CAML_STACKS_H
+#define CAML_STACKS_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+#include "mlvalues.h"
+#include "memory.h"
+
+CAMLextern value * caml_stack_low;
+CAMLextern value * caml_stack_high;
+CAMLextern value * caml_stack_threshold;
+CAMLextern value * caml_extern_sp;
+CAMLextern value * caml_trapsp;
+CAMLextern value * caml_trap_barrier;
+
+#define Trap_pc(tp) (((code_t *)(tp))[0])
+#define Trap_link(tp) (((value **)(tp))[1])
+
+void caml_init_stack (uintnat init_max_size);
+void caml_realloc_stack (asize_t required_size);
+void caml_change_max_stack_size (uintnat new_max_size);
+uintnat caml_stack_usage (void);
+
+CAMLextern uintnat (*caml_stack_usage_hook)(void);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_STACKS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/startup.h b/test/monniaux/ocaml/byterun/caml/startup.h
new file mode 100644
index 00000000..2b26e916
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/startup.h
@@ -0,0 +1,52 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_STARTUP_H
+#define CAML_STARTUP_H
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+#include "exec.h"
+
+CAMLextern void caml_main(char_os **argv);
+
+CAMLextern void caml_startup_code(
+ code_t code, asize_t code_size,
+ char *data, asize_t data_size,
+ char *section_table, asize_t section_table_size,
+ int pooling,
+ char_os **argv);
+
+CAMLextern value caml_startup_code_exn(
+ code_t code, asize_t code_size,
+ char *data, asize_t data_size,
+ char *section_table, asize_t section_table_size,
+ int pooling,
+ char_os **argv);
+
+enum { FILE_NOT_FOUND = -1, BAD_BYTECODE = -2 };
+
+extern int caml_attempt_open(char_os **name, struct exec_trailer *trail,
+ int do_open_script);
+extern void caml_read_section_descriptors(int fd, struct exec_trailer *trail);
+extern int32_t caml_seek_optional_section(int fd, struct exec_trailer *trail,
+ char *name);
+extern int32_t caml_seek_section(int fd, struct exec_trailer *trail,
+ char *name);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_STARTUP_H */
diff --git a/test/monniaux/ocaml/byterun/caml/startup_aux.h b/test/monniaux/ocaml/byterun/caml/startup_aux.h
new file mode 100644
index 00000000..7286bc98
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/startup_aux.h
@@ -0,0 +1,44 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, Jane Street Group, LLC */
+/* */
+/* Copyright 2015 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_STARTUP_AUX_H
+#define CAML_STARTUP_AUX_H
+
+#ifdef CAML_INTERNALS
+
+#include "config.h"
+
+extern void caml_init_atom_table (void);
+
+extern uintnat caml_init_percent_free;
+extern uintnat caml_init_max_percent_free;
+extern uintnat caml_init_minor_heap_wsz;
+extern uintnat caml_init_heap_chunk_sz;
+extern uintnat caml_init_heap_wsz;
+extern uintnat caml_init_max_stack_wsz;
+extern uintnat caml_init_major_window;
+extern uintnat caml_trace_level;
+extern uintnat caml_cleanup_on_exit;
+
+extern void caml_parse_ocamlrunparam (void);
+
+/* Common entry point to caml_startup.
+ Returns 0 if the runtime is already initialized.
+ If [pooling] is 0, [caml_stat_*] functions will not be backed by a pool. */
+extern int caml_startup_aux (int pooling);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_STARTUP_AUX_H */
diff --git a/test/monniaux/ocaml/byterun/caml/sys.h b/test/monniaux/ocaml/byterun/caml/sys.h
new file mode 100644
index 00000000..0f372771
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/sys.h
@@ -0,0 +1,45 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#ifndef CAML_SYS_H
+#define CAML_SYS_H
+
+#ifdef CAML_INTERNALS
+
+#include "misc.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define NO_ARG Val_int(0)
+
+CAMLextern void caml_sys_error (value);
+CAMLextern void caml_sys_io_error (value);
+CAMLextern double caml_sys_time_unboxed(value);
+CAMLextern void caml_sys_init (char_os * exe_name, char_os ** argv);
+CAMLextern value caml_sys_exit (value);
+extern double caml_sys_time_unboxed(value);
+CAMLextern value caml_sys_get_argv(value unit);
+
+extern char_os * caml_exe_name;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_SYS_H */
diff --git a/test/monniaux/ocaml/byterun/caml/ui.h b/test/monniaux/ocaml/byterun/caml/ui.h
new file mode 100644
index 00000000..3047ba7f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/ui.h
@@ -0,0 +1,32 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Function declarations for non-Unix user interfaces */
+
+#ifndef CAML_UI_H
+#define CAML_UI_H
+
+#ifdef CAML_INTERNALS
+
+#include "config.h"
+
+void ui_exit (int return_code);
+int ui_read (int file_desc, char *buf, unsigned int length);
+int ui_write (int file_desc, char *buf, unsigned int length);
+void ui_print_stderr (char *format, void *arg);
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_UI_H */
diff --git a/test/monniaux/ocaml/byterun/caml/version.h b/test/monniaux/ocaml/byterun/caml/version.h
new file mode 100644
index 00000000..68d7000e
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/version.h
@@ -0,0 +1,6 @@
+#define OCAML_VERSION_MAJOR 4
+#define OCAML_VERSION_MINOR 7
+#define OCAML_VERSION_PATCHLEVEL 1
+#undef OCAML_VERSION_ADDITIONAL
+#define OCAML_VERSION 40701
+#define OCAML_VERSION_STRING "4.07.1"
diff --git a/test/monniaux/ocaml/byterun/caml/weak.h b/test/monniaux/ocaml/byterun/caml/weak.h
new file mode 100644
index 00000000..a8563867
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/caml/weak.h
@@ -0,0 +1,93 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1997 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Operations on weak arrays */
+
+#ifndef CAML_WEAK_H
+#define CAML_WEAK_H
+
+#ifdef CAML_INTERNALS
+
+#include "mlvalues.h"
+
+extern value caml_ephe_list_head;
+extern value caml_ephe_none;
+
+
+/** The first field 0: weak list;
+ second field 1: data;
+ others 2..: keys;
+
+ A weak pointer is an ephemeron with the data at caml_ephe_none
+ If fields are added, don't forget to update weak.ml [additional_values].
+ */
+
+#define CAML_EPHE_LINK_OFFSET 0
+#define CAML_EPHE_DATA_OFFSET 1
+#define CAML_EPHE_FIRST_KEY 2
+
+
+/* In the header, in order to let major_gc.c
+ and weak.c see the body of the function */
+static inline void caml_ephe_clean (value v){
+ value child;
+ int release_data = 0;
+ mlsize_t size, i;
+ header_t hd;
+ CAMLassert(caml_gc_phase == Phase_clean);
+
+ hd = Hd_val (v);
+ size = Wosize_hd (hd);
+ for (i = 2; i < size; i++){
+ child = Field (v, i);
+ ephemeron_again:
+ if (child != caml_ephe_none
+ && Is_block (child) && Is_in_heap_or_young (child)){
+ if (Tag_val (child) == Forward_tag){
+ value f = Forward_val (child);
+ if (Is_block (f)) {
+ if (!Is_in_value_area(f) || Tag_val (f) == Forward_tag
+ || Tag_val (f) == Lazy_tag || Tag_val (f) == Double_tag){
+ /* Do not short-circuit the pointer. */
+ }else{
+ Field (v, i) = child = f;
+ if (Is_block (f) && Is_young (f))
+ add_to_ephe_ref_table(&caml_ephe_ref_table, v, i);
+ goto ephemeron_again;
+ }
+ }
+ }
+ if (Is_white_val (child) && !Is_young (child)){
+ release_data = 1;
+ Field (v, i) = caml_ephe_none;
+ }
+ }
+ }
+
+ child = Field (v, 1);
+ if(child != caml_ephe_none){
+ if (release_data){
+ Field (v, 1) = caml_ephe_none;
+ } else {
+ /* The mark phase must have marked it */
+ CAMLassert( !(Is_block (child) && Is_in_heap (child)
+ && Is_white_val (child)) );
+ }
+ }
+}
+
+#endif /* CAML_INTERNALS */
+
+#endif /* CAML_WEAK_H */
diff --git a/test/monniaux/ocaml/byterun/compact.c b/test/monniaux/ocaml/byterun/compact.c
new file mode 100644
index 00000000..83e7ed0a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/compact.c
@@ -0,0 +1,563 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <string.h>
+
+#include "caml/address_class.h"
+#include "caml/config.h"
+#include "caml/finalise.h"
+#include "caml/freelist.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/weak.h"
+#include "caml/compact.h"
+
+extern uintnat caml_percent_free; /* major_gc.c */
+extern void caml_shrink_heap (char *); /* memory.c */
+extern void caml_final_invert_finalisable_values (void);
+
+/* Encoded headers: the color is stored in the 2 least significant bits.
+ (For pointer inversion, we need to distinguish headers from pointers.)
+ s is a Wosize, t is a tag, and c is a color (a two-bit number)
+
+ For the purpose of compaction, "colors" are:
+ 0: pointers (direct or inverted)
+ 1: integer or (unencoded) infix header
+ 2: inverted pointer for infix header
+ 3: integer or encoded (noninfix) header
+
+ XXX Should be fixed:
+ XXX The above assumes that all roots are aligned on a 4-byte boundary,
+ XXX which is not always guaranteed by C.
+ XXX (see [caml_register_global_roots])
+ XXX Should be able to fix it to only assume 2-byte alignment.
+*/
+#ifdef WITH_PROFINFO
+#define Make_ehd(s,t,c,p) \
+ (((s) << 10) | (t) << 2 | (c) | ((p) << PROFINFO_SHIFT))
+#else
+#define Make_ehd(s,t,c,p) (((s) << 10) | (t) << 2 | (c))
+#endif
+#define Whsize_ehd(h) Whsize_hd (h)
+#define Wosize_ehd(h) Wosize_hd (h)
+#define Tag_ehd(h) (((h) >> 2) & 0xFF)
+#define Profinfo_ehd(hd) Profinfo_hd(hd)
+#define Ecolor(w) ((w) & 3)
+
+typedef uintnat word;
+
+static void invert_pointer_at (word *p)
+{
+ word q = *p;
+ CAMLassert (Ecolor ((intnat) p) == 0);
+
+ /* Use Ecolor (q) == 0 instead of Is_block (q) because q could be an
+ inverted pointer for an infix header (with Ecolor == 2). */
+ if (Ecolor (q) == 0 && Is_in_heap (q)){
+ switch (Ecolor (Hd_val (q))){
+ case 0:
+ case 3: /* Pointer or header: insert in inverted list. */
+ *p = Hd_val (q);
+ Hd_val (q) = (header_t) p;
+ break;
+ case 1: /* Infix header: make inverted infix list. */
+ /* Double inversion: the last of the inverted infix list points to
+ the next infix header in this block. The last of the last list
+ contains the original block header. */
+ {
+ /* This block as a value. */
+ value val = (value) q - Infix_offset_val (q);
+ /* Get the block header. */
+ word *hp = (word *) Hp_val (val);
+
+ while (Ecolor (*hp) == 0) hp = (word *) *hp;
+ CAMLassert (Ecolor (*hp) == 3);
+ if (Tag_ehd (*hp) == Closure_tag){
+ /* This is the first infix found in this block. */
+ /* Save original header. */
+ *p = *hp;
+ /* Link inverted infix list. */
+ Hd_val (q) = (header_t) ((word) p | 2);
+ /* Change block header's tag to Infix_tag, and change its size
+ to point to the infix list. */
+ *hp = Make_ehd (Wosize_bhsize (q - val), Infix_tag, 3, (uintnat) 0);
+ }else{
+ CAMLassert (Tag_ehd (*hp) == Infix_tag);
+ /* Point the last of this infix list to the current first infix
+ list of the block. */
+ *p = (word) &Field (val, Wosize_ehd (*hp)) | 1;
+ /* Point the head of this infix list to the above. */
+ Hd_val (q) = (header_t) ((word) p | 2);
+ /* Change block header's size to point to this infix list. */
+ *hp = Make_ehd (Wosize_bhsize (q - val), Infix_tag, 3, (uintnat) 0);
+ }
+ }
+ break;
+ case 2: /* Inverted infix list: insert. */
+ *p = Hd_val (q);
+ Hd_val (q) = (header_t) ((word) p | 2);
+ break;
+ }
+ }
+}
+
+void caml_invert_root (value v, value *p)
+{
+ invert_pointer_at ((word *) p);
+}
+
+static char *compact_fl;
+
+static void init_compact_allocate (void)
+{
+ char *ch = caml_heap_start;
+ while (ch != NULL){
+ Chunk_alloc (ch) = 0;
+ ch = Chunk_next (ch);
+ }
+ compact_fl = caml_heap_start;
+}
+
+/* [size] is a number of bytes and includes the header size */
+static char *compact_allocate (mlsize_t size)
+{
+ char *chunk, *adr;
+
+ while (Chunk_size (compact_fl) - Chunk_alloc (compact_fl) <= Bhsize_wosize (3)
+ && Chunk_size (Chunk_next (compact_fl))
+ - Chunk_alloc (Chunk_next (compact_fl))
+ <= Bhsize_wosize (3)){
+ compact_fl = Chunk_next (compact_fl);
+ }
+ chunk = compact_fl;
+ while (Chunk_size (chunk) - Chunk_alloc (chunk) < size){
+ chunk = Chunk_next (chunk);
+ CAMLassert (chunk != NULL);
+ }
+ adr = chunk + Chunk_alloc (chunk);
+ Chunk_alloc (chunk) += size;
+ return adr;
+}
+
+static void do_compaction (void)
+{
+ char *ch, *chend;
+ CAMLassert (caml_gc_phase == Phase_idle);
+ caml_gc_message (0x10, "Compacting heap...\n");
+
+#ifdef DEBUG
+ caml_heap_check ();
+#endif
+
+ /* First pass: encode all noninfix headers. */
+ {
+ ch = caml_heap_start;
+ while (ch != NULL){
+ header_t *p = (header_t *) ch;
+
+ chend = ch + Chunk_size (ch);
+ while ((char *) p < chend){
+ header_t hd = Hd_hp (p);
+ mlsize_t sz = Wosize_hd (hd);
+
+ if (Is_blue_hd (hd)){
+ /* Free object. Give it a string tag. */
+ Hd_hp (p) = Make_ehd (sz, String_tag, 3, (uintnat) 0);
+ }else{
+ CAMLassert (Is_white_hd (hd));
+ /* Live object. Keep its tag. */
+ Hd_hp (p) = Make_ehd (sz, Tag_hd (hd), 3, Profinfo_hd (hd));
+ }
+ p += Whsize_wosize (sz);
+ }
+ ch = Chunk_next (ch);
+ }
+ }
+
+
+ /* Second pass: invert pointers.
+ Link infix headers in each block in an inverted list of inverted lists.
+ Don't forget roots and weak pointers. */
+ {
+ /* Invert roots first because the threads library needs some heap
+ data structures to find its roots. Fortunately, it doesn't need
+ the headers (see above). */
+ caml_do_roots (caml_invert_root, 1);
+ /* The values to be finalised are not roots but should still be inverted */
+ caml_final_invert_finalisable_values ();
+
+ ch = caml_heap_start;
+ while (ch != NULL){
+ word *p = (word *) ch;
+ chend = ch + Chunk_size (ch);
+
+ while ((char *) p < chend){
+ word q = *p;
+ size_t sz, i;
+ tag_t t;
+ word *infixes;
+
+ while (Ecolor (q) == 0) q = * (word *) q;
+ sz = Whsize_ehd (q);
+ t = Tag_ehd (q);
+
+ if (t == Infix_tag){
+ /* Get the original header of this block. */
+ infixes = p + sz;
+ q = *infixes;
+ while (Ecolor (q) != 3) q = * (word *) (q & ~(uintnat)3);
+ sz = Whsize_ehd (q);
+ t = Tag_ehd (q);
+ }
+
+ if (t < No_scan_tag){
+ for (i = 1; i < sz; i++) invert_pointer_at (&(p[i]));
+ }
+ p += sz;
+ }
+ ch = Chunk_next (ch);
+ }
+ /* Invert weak pointers. */
+ {
+ value *pp = &caml_ephe_list_head;
+ value p;
+ word q;
+ size_t sz, i;
+
+ while (1){
+ p = *pp;
+ if (p == (value) NULL) break;
+ q = Hd_val (p);
+ while (Ecolor (q) == 0) q = * (word *) q;
+ sz = Wosize_ehd (q);
+ for (i = 1; i < sz; i++){
+ if (Field (p,i) != caml_ephe_none){
+ invert_pointer_at ((word *) &(Field (p,i)));
+ }
+ }
+ invert_pointer_at ((word *) pp);
+ pp = &Field (p, 0);
+ }
+ }
+ }
+
+
+ /* Third pass: reallocate virtually; revert pointers; decode headers.
+ Rebuild infix headers. */
+ {
+ init_compact_allocate ();
+ ch = caml_heap_start;
+ while (ch != NULL){
+ word *p = (word *) ch;
+
+ chend = ch + Chunk_size (ch);
+ while ((char *) p < chend){
+ word q = *p;
+
+ if (Ecolor (q) == 0 || Tag_ehd (q) == Infix_tag){
+ /* There were (normal or infix) pointers to this block. */
+ size_t sz;
+ tag_t t;
+ char *newadr;
+#ifdef WITH_PROFINFO
+ uintnat profinfo;
+#endif
+ word *infixes = NULL;
+
+ while (Ecolor (q) == 0) q = * (word *) q;
+ sz = Whsize_ehd (q);
+ t = Tag_ehd (q);
+#ifdef WITH_PROFINFO
+ profinfo = Profinfo_ehd (q);
+#endif
+ if (t == Infix_tag){
+ /* Get the original header of this block. */
+ infixes = p + sz;
+ q = *infixes;
+ CAMLassert (Ecolor (q) == 2);
+ while (Ecolor (q) != 3) q = * (word *) (q & ~(uintnat)3);
+ sz = Whsize_ehd (q);
+ t = Tag_ehd (q);
+ }
+
+ newadr = compact_allocate (Bsize_wsize (sz));
+ q = *p;
+ while (Ecolor (q) == 0){
+ word next = * (word *) q;
+ * (word *) q = (word) Val_hp (newadr);
+ q = next;
+ }
+ *p = Make_header_with_profinfo (Wosize_whsize (sz), t, Caml_white,
+ profinfo);
+
+ if (infixes != NULL){
+ /* Rebuild the infix headers and revert the infix pointers. */
+ while (Ecolor ((word) infixes) != 3){
+ infixes = (word *) ((word) infixes & ~(uintnat) 3);
+ q = *infixes;
+ while (Ecolor (q) == 2){
+ word next;
+ q = (word) q & ~(uintnat) 3;
+ next = * (word *) q;
+ * (word *) q = (word) Val_hp ((word *) newadr + (infixes - p));
+ q = next;
+ }
+ CAMLassert (Ecolor (q) == 1 || Ecolor (q) == 3);
+ /* No need to preserve any profinfo value on the [Infix_tag]
+ headers; the Spacetime profiling heap snapshot code doesn't
+ look at them. */
+ *infixes = Make_header (infixes - p, Infix_tag, Caml_white);
+ infixes = (word *) q;
+ }
+ }
+ p += sz;
+ }else{
+ CAMLassert (Ecolor (q) == 3);
+ /* This is guaranteed only if caml_compact_heap was called after a
+ nonincremental major GC: CAMLassert (Tag_ehd (q) == String_tag);
+ */
+ /* No pointers to the header and no infix header:
+ the object was free. */
+ *p = Make_header (Wosize_ehd (q), Tag_ehd (q), Caml_blue);
+ p += Whsize_ehd (q);
+ }
+ }
+ ch = Chunk_next (ch);
+ }
+ }
+
+
+ /* Fourth pass: reallocate and move objects.
+ Use the exact same allocation algorithm as pass 3. */
+ {
+ init_compact_allocate ();
+ ch = caml_heap_start;
+ while (ch != NULL){
+ word *p = (word *) ch;
+
+ chend = ch + Chunk_size (ch);
+ while ((char *) p < chend){
+ word q = *p;
+ if (Color_hd (q) == Caml_white){
+ size_t sz = Bhsize_hd (q);
+ char *newadr = compact_allocate (sz);
+ memmove (newadr, p, sz);
+ p += Wsize_bsize (sz);
+ }else{
+ CAMLassert (Color_hd (q) == Caml_blue);
+ p += Whsize_hd (q);
+ }
+ }
+ ch = Chunk_next (ch);
+ }
+ }
+
+ /* Shrink the heap if needed. */
+ {
+ /* Find the amount of live data and the unshrinkable free space. */
+ asize_t live = 0;
+ asize_t free = 0;
+ asize_t wanted;
+
+ ch = caml_heap_start;
+ while (ch != NULL){
+ if (Chunk_alloc (ch) != 0){
+ live += Wsize_bsize (Chunk_alloc (ch));
+ free += Wsize_bsize (Chunk_size (ch) - Chunk_alloc (ch));
+ }
+ ch = Chunk_next (ch);
+ }
+
+ /* Add up the empty chunks until there are enough, then remove the
+ other empty chunks. */
+ wanted = caml_percent_free * (live / 100 + 1);
+ ch = caml_heap_start;
+ while (ch != NULL){
+ char *next_chunk = Chunk_next (ch); /* Chunk_next (ch) will be erased */
+
+ if (Chunk_alloc (ch) == 0){
+ if (free < wanted){
+ free += Wsize_bsize (Chunk_size (ch));
+ }else{
+ caml_shrink_heap (ch);
+ }
+ }
+ ch = next_chunk;
+ }
+ }
+
+ /* Rebuild the free list. */
+ {
+ ch = caml_heap_start;
+ caml_fl_reset ();
+ while (ch != NULL){
+ if (Chunk_size (ch) > Chunk_alloc (ch)){
+ caml_make_free_blocks ((value *) (ch + Chunk_alloc (ch)),
+ Wsize_bsize (Chunk_size(ch)-Chunk_alloc(ch)), 1,
+ Caml_white);
+ }
+ ch = Chunk_next (ch);
+ }
+ }
+ ++ caml_stat_compactions;
+ caml_gc_message (0x10, "done.\n");
+}
+
+uintnat caml_percent_max; /* used in gc_ctrl.c and memory.c */
+
+void caml_compact_heap (void)
+{
+ uintnat target_wsz, live;
+ CAML_INSTR_SETUP(tmr, "compact");
+
+ CAMLassert (caml_young_ptr == caml_young_alloc_end);
+ CAMLassert (caml_ref_table.ptr == caml_ref_table.base);
+ CAMLassert (caml_ephe_ref_table.ptr == caml_ephe_ref_table.base);
+ CAMLassert (caml_custom_table.ptr == caml_custom_table.base);
+
+ do_compaction ();
+ CAML_INSTR_TIME (tmr, "compact/main");
+ /* Compaction may fail to shrink the heap to a reasonable size
+ because it deals in complete chunks: if a very large chunk
+ is at the beginning of the heap, everything gets moved to
+ it and it is not freed.
+
+ In that case, we allocate a new chunk of the desired heap
+ size, chain it at the beginning of the heap (thus pretending
+ its address is smaller), and launch a second compaction.
+ This will move all data to this new chunk and free the
+ very large chunk.
+
+ See PR#5389
+ */
+ /* We compute:
+ freewords = caml_fl_cur_wsz (exact)
+ heapwords = Wsize_bsize (caml_heap_size) (exact)
+ live = heapwords - freewords
+ wanted = caml_percent_free * (live / 100 + 1) (same as in do_compaction)
+ target_wsz = live + wanted
+ We add one page to make sure a small difference in counting sizes
+ won't make [do_compaction] keep the second block (and break all sorts
+ of invariants).
+
+ We recompact if target_wsz < heap_size / 2
+ */
+ live = caml_stat_heap_wsz - caml_fl_cur_wsz;
+ target_wsz = live + caml_percent_free * (live / 100 + 1)
+ + Wsize_bsize (Page_size);
+ target_wsz = caml_clip_heap_chunk_wsz (target_wsz);
+
+#ifdef HAS_HUGE_PAGES
+ if (caml_use_huge_pages
+ && Bsize_wsize (caml_stat_heap_wsz) <= HUGE_PAGE_SIZE)
+ return;
+#endif
+
+ if (target_wsz < caml_stat_heap_wsz / 2){
+ /* Recompact. */
+ char *chunk;
+
+ caml_gc_message (0x10, "Recompacting heap (target=%"
+ ARCH_INTNAT_PRINTF_FORMAT "uk words)\n",
+ target_wsz / 1024);
+
+ chunk = caml_alloc_for_heap (Bsize_wsize (target_wsz));
+ if (chunk == NULL) return;
+ /* PR#5757: we need to make the new blocks blue, or they won't be
+ recognized as free by the recompaction. */
+ caml_make_free_blocks ((value *) chunk,
+ Wsize_bsize (Chunk_size (chunk)), 0, Caml_blue);
+ if (caml_page_table_add (In_heap, chunk, chunk + Chunk_size (chunk)) != 0){
+ caml_free_for_heap (chunk);
+ return;
+ }
+ Chunk_next (chunk) = caml_heap_start;
+ caml_heap_start = chunk;
+ ++ caml_stat_heap_chunks;
+ caml_stat_heap_wsz += Wsize_bsize (Chunk_size (chunk));
+ if (caml_stat_heap_wsz > caml_stat_top_heap_wsz){
+ caml_stat_top_heap_wsz = caml_stat_heap_wsz;
+ }
+ do_compaction ();
+ CAMLassert (caml_stat_heap_chunks == 1);
+ CAMLassert (Chunk_next (caml_heap_start) == NULL);
+ CAMLassert (caml_stat_heap_wsz == Wsize_bsize (Chunk_size (chunk)));
+ CAML_INSTR_TIME (tmr, "compact/recompact");
+ }
+}
+
+void caml_compact_heap_maybe (void)
+{
+ /* Estimated free+garbage words in the heap:
+ FW = fl_size_at_phase_change + 3 * (caml_fl_cur_wsz
+ - caml_fl_wsz_at_phase_change)
+ FW = 3 * caml_fl_cur_wsz - 2 * caml_fl_wsz_at_phase_change
+ Estimated live words: LW = caml_stat_heap_wsz - FW
+ Estimated free percentage: FP = 100 * FW / LW
+ We compact the heap if FP > caml_percent_max
+ */
+ float fw, fp;
+ CAMLassert (caml_gc_phase == Phase_idle);
+ if (caml_percent_max >= 1000000) return;
+ if (caml_stat_major_collections < 3) return;
+ if (caml_stat_heap_wsz <= 2 * caml_clip_heap_chunk_wsz (0)) return;
+
+#ifdef HAS_HUGE_PAGES
+ if (caml_use_huge_pages
+ && Bsize_wsize (caml_stat_heap_wsz) <= HUGE_PAGE_SIZE)
+ return;
+#endif
+
+ fw = 3.0 * caml_fl_cur_wsz - 2.0 * caml_fl_wsz_at_phase_change;
+ if (fw < 0) fw = caml_fl_cur_wsz;
+
+ if (fw >= caml_stat_heap_wsz){
+ fp = 1000000.0;
+ }else{
+ fp = 100.0 * fw / (caml_stat_heap_wsz - fw);
+ if (fp > 1000000.0) fp = 1000000.0;
+ }
+ caml_gc_message (0x200, "FL size at phase change = %"
+ ARCH_INTNAT_PRINTF_FORMAT "u words\n",
+ (uintnat) caml_fl_wsz_at_phase_change);
+ caml_gc_message (0x200, "FL current size = %"
+ ARCH_INTNAT_PRINTF_FORMAT "u words\n",
+ (uintnat) caml_fl_cur_wsz);
+ caml_gc_message (0x200, "Estimated overhead = %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
+ (uintnat) fp);
+ if (fp >= caml_percent_max){
+ caml_gc_message (0x200, "Automatic compaction triggered.\n");
+ caml_empty_minor_heap (); /* minor heap must be empty for compaction */
+ caml_finish_major_cycle ();
+
+ fw = caml_fl_cur_wsz;
+ fp = 100.0 * fw / (caml_stat_heap_wsz - fw);
+ caml_gc_message (0x200, "Measured overhead: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
+ (uintnat) fp);
+ if (fp >= caml_percent_max)
+ caml_compact_heap ();
+ else
+ caml_gc_message (0x200, "Automatic compaction aborted.\n");
+
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/compare.c b/test/monniaux/ocaml/byterun/compare.c
new file mode 100644
index 00000000..382c9dff
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/compare.c
@@ -0,0 +1,363 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <string.h>
+#include <stdlib.h>
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+
+#if defined(LACKS_SANE_NAN) && !defined(isnan)
+#define isnan _isnan
+#endif
+
+/* Structural comparison on trees. */
+
+struct compare_item { value * v1, * v2; mlsize_t count; };
+
+#define COMPARE_STACK_INIT_SIZE 8
+#define COMPARE_STACK_MIN_ALLOC_SIZE 32
+#define COMPARE_STACK_MAX_SIZE (1024*1024)
+CAMLexport int caml_compare_unordered;
+
+struct compare_stack {
+ struct compare_item init_stack[COMPARE_STACK_INIT_SIZE];
+ struct compare_item* stack;
+ struct compare_item* limit;
+};
+
+/* Free the compare stack if needed */
+static void compare_free_stack(struct compare_stack* stk)
+{
+ if (stk->stack != stk->init_stack) {
+ caml_stat_free(stk->stack);
+ stk->stack = NULL;
+ }
+}
+
+/* Same, then raise Out_of_memory */
+static void compare_stack_overflow(struct compare_stack* stk)
+{
+ caml_gc_message (0x04, "Stack overflow in structural comparison\n");
+ compare_free_stack(stk);
+ caml_raise_out_of_memory();
+}
+
+/* Grow the compare stack */
+static struct compare_item * compare_resize_stack(struct compare_stack* stk,
+ struct compare_item * sp)
+{
+ asize_t newsize;
+ asize_t sp_offset = sp - stk->stack;
+ struct compare_item * newstack;
+
+ if (stk->stack == stk->init_stack) {
+ newsize = COMPARE_STACK_MIN_ALLOC_SIZE;
+ newstack = caml_stat_alloc_noexc(sizeof(struct compare_item) * newsize);
+ if (newstack == NULL) compare_stack_overflow(stk);
+ memcpy(newstack, stk->init_stack,
+ sizeof(struct compare_item) * COMPARE_STACK_INIT_SIZE);
+ } else {
+ newsize = 2 * (stk->limit - stk->stack);
+ if (newsize >= COMPARE_STACK_MAX_SIZE) compare_stack_overflow(stk);
+ newstack = caml_stat_resize_noexc(stk->stack,
+ sizeof(struct compare_item) * newsize);
+ if (newstack == NULL) compare_stack_overflow(stk);
+ }
+ stk->stack = newstack;
+ stk->limit = newstack + newsize;
+ return newstack + sp_offset;
+}
+
+
+static intnat do_compare_val(struct compare_stack* stk,
+ value v1, value v2, int total);
+
+static intnat compare_val(value v1, value v2, int total)
+{
+ struct compare_stack stk;
+ intnat res;
+ stk.stack = stk.init_stack;
+ stk.limit = stk.stack + COMPARE_STACK_INIT_SIZE;
+ res = do_compare_val(&stk, v1, v2, total);
+ compare_free_stack(&stk);
+ return res;
+}
+
+/* Structural comparison */
+
+
+#define LESS -1
+#define EQUAL 0
+#define GREATER 1
+#define UNORDERED ((intnat)1 << (8 * sizeof(value) - 1))
+
+/* The return value of compare_val is as follows:
+ > 0 v1 is greater than v2
+ 0 v1 is equal to v2
+ < 0 and > UNORDERED v1 is less than v2
+ UNORDERED v1 and v2 cannot be compared */
+
+static intnat do_compare_val(struct compare_stack* stk,
+ value v1, value v2, int total)
+{
+ struct compare_item * sp;
+ tag_t t1, t2;
+
+ sp = stk->stack;
+ while (1) {
+ if (v1 == v2 && total) goto next_item;
+ if (Is_long(v1)) {
+ if (v1 == v2) goto next_item;
+ if (Is_long(v2))
+ return Long_val(v1) - Long_val(v2);
+ /* Subtraction above cannot overflow and cannot result in UNORDERED */
+ if (Is_in_value_area(v2)) {
+ switch (Tag_val(v2)) {
+ case Forward_tag:
+ v2 = Forward_val(v2);
+ continue;
+ case Custom_tag: {
+ int res;
+ int (*compare)(value v1, value v2) = Custom_ops_val(v2)->compare_ext;
+ if (compare == NULL) break; /* for backward compatibility */
+ caml_compare_unordered = 0;
+ res = compare(v1, v2);
+ if (caml_compare_unordered && !total) return UNORDERED;
+ if (res != 0) return res;
+ goto next_item;
+ }
+ default: /*fallthrough*/;
+ }
+ }
+ return LESS; /* v1 long < v2 block */
+ }
+ if (Is_long(v2)) {
+ if (Is_in_value_area(v1)) {
+ switch (Tag_val(v1)) {
+ case Forward_tag:
+ v1 = Forward_val(v1);
+ continue;
+ case Custom_tag: {
+ int res;
+ int (*compare)(value v1, value v2) = Custom_ops_val(v1)->compare_ext;
+ if (compare == NULL) break; /* for backward compatibility */
+ caml_compare_unordered = 0;
+ res = compare(v1, v2);
+ if (caml_compare_unordered && !total) return UNORDERED;
+ if (res != 0) return res;
+ goto next_item;
+ }
+ default: /*fallthrough*/;
+ }
+ }
+ return GREATER; /* v1 block > v2 long */
+ }
+ /* If one of the objects is outside the heap (but is not an atom),
+ use address comparison. Since both addresses are 2-aligned,
+ shift lsb off to avoid overflow in subtraction. */
+ if (! Is_in_value_area(v1) || ! Is_in_value_area(v2)) {
+ if (v1 == v2) goto next_item;
+ return (v1 >> 1) - (v2 >> 1);
+ /* Subtraction above cannot result in UNORDERED */
+ }
+ t1 = Tag_val(v1);
+ t2 = Tag_val(v2);
+ if (t1 == Forward_tag) { v1 = Forward_val (v1); continue; }
+ if (t2 == Forward_tag) { v2 = Forward_val (v2); continue; }
+ if (t1 != t2) return (intnat)t1 - (intnat)t2;
+ switch(t1) {
+ case String_tag: {
+ mlsize_t len1, len2;
+ int res;
+ if (v1 == v2) break;
+ len1 = caml_string_length(v1);
+ len2 = caml_string_length(v2);
+ res = memcmp(String_val(v1), String_val(v2), len1 <= len2 ? len1 : len2);
+ if (res < 0) return LESS;
+ if (res > 0) return GREATER;
+ if (len1 != len2) return len1 - len2;
+ break;
+ }
+ case Double_tag: {
+ double d1 = Double_val(v1);
+ double d2 = Double_val(v2);
+#ifdef LACKS_SANE_NAN
+ if (isnan(d2)) {
+ if (! total) return UNORDERED;
+ if (isnan(d1)) break;
+ return GREATER;
+ } else if (isnan(d1)) {
+ if (! total) return UNORDERED;
+ return LESS;
+ }
+#endif
+ if (d1 < d2) return LESS;
+ if (d1 > d2) return GREATER;
+#ifndef LACKS_SANE_NAN
+ if (d1 != d2) {
+ if (! total) return UNORDERED;
+ /* One or both of d1 and d2 is NaN. Order according to the
+ convention NaN = NaN and NaN < f for all other floats f. */
+ if (d1 == d1) return GREATER; /* d1 is not NaN, d2 is NaN */
+ if (d2 == d2) return LESS; /* d2 is not NaN, d1 is NaN */
+ /* d1 and d2 are both NaN, thus equal: continue comparison */
+ }
+#endif
+ break;
+ }
+ case Double_array_tag: {
+ mlsize_t sz1 = Wosize_val(v1) / Double_wosize;
+ mlsize_t sz2 = Wosize_val(v2) / Double_wosize;
+ mlsize_t i;
+ if (sz1 != sz2) return sz1 - sz2;
+ for (i = 0; i < sz1; i++) {
+ double d1 = Double_flat_field(v1, i);
+ double d2 = Double_flat_field(v2, i);
+ #ifdef LACKS_SANE_NAN
+ if (isnan(d2)) {
+ if (! total) return UNORDERED;
+ if (isnan(d1)) break;
+ return GREATER;
+ } else if (isnan(d1)) {
+ if (! total) return UNORDERED;
+ return LESS;
+ }
+ #endif
+ if (d1 < d2) return LESS;
+ if (d1 > d2) return GREATER;
+ #ifndef LACKS_SANE_NAN
+ if (d1 != d2) {
+ if (! total) return UNORDERED;
+ /* See comment for Double_tag case */
+ if (d1 == d1) return GREATER;
+ if (d2 == d2) return LESS;
+ }
+ #endif
+ }
+ break;
+ }
+ case Abstract_tag:
+ compare_free_stack(stk);
+ caml_invalid_argument("compare: abstract value");
+ case Closure_tag:
+ case Infix_tag:
+ compare_free_stack(stk);
+ caml_invalid_argument("compare: functional value");
+ case Object_tag: {
+ intnat oid1 = Oid_val(v1);
+ intnat oid2 = Oid_val(v2);
+ if (oid1 != oid2) return oid1 - oid2;
+ break;
+ }
+ case Custom_tag: {
+ int res;
+ int (*compare)(value v1, value v2) = Custom_ops_val(v1)->compare;
+ /* Hardening against comparisons between different types */
+ if (compare != Custom_ops_val(v2)->compare) {
+ return strcmp(Custom_ops_val(v1)->identifier,
+ Custom_ops_val(v2)->identifier) < 0
+ ? LESS : GREATER;
+ }
+ if (compare == NULL) {
+ compare_free_stack(stk);
+ caml_invalid_argument("compare: abstract value");
+ }
+ caml_compare_unordered = 0;
+ res = compare(v1, v2);
+ if (caml_compare_unordered && !total) return UNORDERED;
+ if (res != 0) return res;
+ break;
+ }
+ default: {
+ mlsize_t sz1 = Wosize_val(v1);
+ mlsize_t sz2 = Wosize_val(v2);
+ /* Compare sizes first for speed */
+ if (sz1 != sz2) return sz1 - sz2;
+ if (sz1 == 0) break;
+ /* Remember that we still have to compare fields 1 ... sz - 1 */
+ if (sz1 > 1) {
+ sp++;
+ if (sp >= stk->limit) sp = compare_resize_stack(stk, sp);
+ sp->v1 = &Field(v1, 1);
+ sp->v2 = &Field(v2, 1);
+ sp->count = sz1 - 1;
+ }
+ /* Continue comparison with first field */
+ v1 = Field(v1, 0);
+ v2 = Field(v2, 0);
+ continue;
+ }
+ }
+ next_item:
+ /* Pop one more item to compare, if any */
+ if (sp == stk->stack) return EQUAL; /* we're done */
+ v1 = *((sp->v1)++);
+ v2 = *((sp->v2)++);
+ if (--(sp->count) == 0) sp--;
+ }
+}
+
+CAMLprim value caml_compare(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 1);
+ /* Free stack if needed */
+ if (res < 0)
+ return Val_int(LESS);
+ else if (res > 0)
+ return Val_int(GREATER);
+ else
+ return Val_int(EQUAL);
+}
+
+CAMLprim value caml_equal(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res == 0);
+}
+
+CAMLprim value caml_notequal(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res != 0);
+}
+
+CAMLprim value caml_lessthan(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res < 0 && res != UNORDERED);
+}
+
+CAMLprim value caml_lessequal(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res <= 0 && res != UNORDERED);
+}
+
+CAMLprim value caml_greaterthan(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res > 0);
+}
+
+CAMLprim value caml_greaterequal(value v1, value v2)
+{
+ intnat res = compare_val(v1, v2, 0);
+ return Val_int(res >= 0);
+}
diff --git a/test/monniaux/ocaml/byterun/custom.c b/test/monniaux/ocaml/byterun/custom.c
new file mode 100644
index 00000000..b6a5c4e3
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/custom.c
@@ -0,0 +1,124 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <string.h>
+
+#include "caml/alloc.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/signals.h"
+
+/* [size] is a number of bytes */
+CAMLexport value caml_alloc_custom(struct custom_operations * ops,
+ uintnat size,
+ mlsize_t mem,
+ mlsize_t max)
+{
+ mlsize_t wosize;
+ CAMLparam0();
+ CAMLlocal1(result);
+
+ wosize = 1 + (size + sizeof(value) - 1) / sizeof(value);
+ if (wosize <= Max_young_wosize) {
+ result = caml_alloc_small(wosize, Custom_tag);
+ Custom_ops_val(result) = ops;
+ if (ops->finalize != NULL || mem != 0) {
+ /* Remember that the block needs processing after minor GC. */
+ add_to_custom_table (&caml_custom_table, result, mem, max);
+ /* Keep track of extra resources held by custom block in
+ minor heap. */
+ if (mem != 0) {
+ if (max == 0) max = 1;
+ caml_extra_heap_resources_minor += (double) mem / (double) max;
+ if (caml_extra_heap_resources_minor > 1.0) {
+ caml_request_minor_gc ();
+ caml_gc_dispatch ();
+ }
+ }
+ }
+ } else {
+ result = caml_alloc_shr(wosize, Custom_tag);
+ Custom_ops_val(result) = ops;
+ caml_adjust_gc_speed(mem, max);
+ result = caml_check_urgent_gc(result);
+ }
+ CAMLreturn(result);
+}
+
+struct custom_operations_list {
+ struct custom_operations * ops;
+ struct custom_operations_list * next;
+};
+
+static struct custom_operations_list * custom_ops_table = NULL;
+
+CAMLexport void caml_register_custom_operations(struct custom_operations * ops)
+{
+ struct custom_operations_list * l =
+ caml_stat_alloc(sizeof(struct custom_operations_list));
+ CAMLassert(ops->identifier != NULL);
+ CAMLassert(ops->deserialize != NULL);
+ l->ops = ops;
+ l->next = custom_ops_table;
+ custom_ops_table = l;
+}
+
+struct custom_operations * caml_find_custom_operations(char * ident)
+{
+ struct custom_operations_list * l;
+ for (l = custom_ops_table; l != NULL; l = l->next)
+ if (strcmp(l->ops->identifier, ident) == 0) return l->ops;
+ return NULL;
+}
+
+static struct custom_operations_list * custom_ops_final_table = NULL;
+
+struct custom_operations * caml_final_custom_operations(final_fun fn)
+{
+ struct custom_operations_list * l;
+ struct custom_operations * ops;
+ for (l = custom_ops_final_table; l != NULL; l = l->next)
+ if (l->ops->finalize == fn) return l->ops;
+ ops = caml_stat_alloc(sizeof(struct custom_operations));
+ ops->identifier = "_final";
+ ops->finalize = fn;
+ ops->compare = custom_compare_default;
+ ops->hash = custom_hash_default;
+ ops->serialize = custom_serialize_default;
+ ops->deserialize = custom_deserialize_default;
+ ops->compare_ext = custom_compare_ext_default;
+ l = caml_stat_alloc(sizeof(struct custom_operations_list));
+ l->ops = ops;
+ l->next = custom_ops_final_table;
+ custom_ops_final_table = l;
+ return ops;
+}
+
+extern struct custom_operations caml_int32_ops,
+ caml_nativeint_ops,
+ caml_int64_ops,
+ caml_ba_ops;
+
+void caml_init_custom_operations(void)
+{
+ caml_register_custom_operations(&caml_int32_ops);
+ caml_register_custom_operations(&caml_nativeint_ops);
+ caml_register_custom_operations(&caml_int64_ops);
+ caml_register_custom_operations(&caml_ba_ops);
+}
diff --git a/test/monniaux/ocaml/byterun/debugger.c b/test/monniaux/ocaml/byterun/debugger.c
new file mode 100644
index 00000000..1c416cd6
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/debugger.c
@@ -0,0 +1,454 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Interface with the byte-code debugger */
+
+#ifdef _WIN32
+#include <io.h>
+#endif /* _WIN32 */
+
+#include <string.h>
+
+#include "caml/alloc.h"
+#include "caml/config.h"
+#include "caml/debugger.h"
+#include "caml/misc.h"
+#include "caml/osdeps.h"
+
+int caml_debugger_in_use = 0;
+uintnat caml_event_count;
+int caml_debugger_fork_mode = 1; /* parent by default */
+
+#if !defined(HAS_SOCKETS) || defined(NATIVE_CODE)
+
+void caml_debugger_init(void)
+{
+}
+
+void caml_debugger(enum event_kind event)
+{
+}
+
+void caml_debugger_cleanup_fork(void)
+{
+}
+
+#else
+
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#include <errno.h>
+#include <sys/types.h>
+#ifndef _WIN32
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#else
+#define ATOM ATOM_WS
+#include <winsock.h>
+#undef ATOM
+#include <process.h>
+#endif
+
+#include "caml/fail.h"
+#include "caml/fix_code.h"
+#include "caml/instruct.h"
+#include "caml/intext.h"
+#include "caml/io.h"
+#include "caml/mlvalues.h"
+#include "caml/stacks.h"
+#include "caml/sys.h"
+
+static value marshal_flags = Val_emptylist;
+
+static int sock_domain; /* Socket domain for the debugger */
+static union { /* Socket address for the debugger */
+ struct sockaddr s_gen;
+#ifndef _WIN32
+ struct sockaddr_un s_unix;
+#endif
+ struct sockaddr_in s_inet;
+} sock_addr;
+static int sock_addr_len; /* Length of sock_addr */
+
+static int dbg_socket = -1; /* The socket connected to the debugger */
+static struct channel * dbg_in; /* Input channel on the socket */
+static struct channel * dbg_out;/* Output channel on the socket */
+
+static char *dbg_addr = NULL;
+
+static void open_connection(void)
+{
+#ifdef _WIN32
+ /* Set socket to synchronous mode so that file descriptor-oriented
+ functions (read()/write() etc.) can be used */
+
+ int oldvalue, oldvaluelen, newvalue, retcode;
+ oldvaluelen = sizeof(oldvalue);
+ retcode = getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
+ (char *) &oldvalue, &oldvaluelen);
+ if (retcode == 0) {
+ newvalue = SO_SYNCHRONOUS_NONALERT;
+ setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
+ (char *) &newvalue, sizeof(newvalue));
+ }
+#endif
+ dbg_socket = socket(sock_domain, SOCK_STREAM, 0);
+#ifdef _WIN32
+ if (retcode == 0) {
+ /* Restore initial mode */
+ setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
+ (char *) &oldvalue, oldvaluelen);
+ }
+#endif
+ if (dbg_socket == -1 ||
+ connect(dbg_socket, &sock_addr.s_gen, sock_addr_len) == -1){
+ caml_fatal_error_arg2 ("cannot connect to debugger at %s\n", (dbg_addr ? dbg_addr : "(none)"),
+ "error: %s\n", strerror (errno));
+ }
+#ifdef _WIN32
+ dbg_socket = _open_osfhandle(dbg_socket, 0);
+ if (dbg_socket == -1)
+ caml_fatal_error("_open_osfhandle failed");
+#endif
+ dbg_in = caml_open_descriptor_in(dbg_socket);
+ dbg_out = caml_open_descriptor_out(dbg_socket);
+ if (!caml_debugger_in_use) caml_putword(dbg_out, -1); /* first connection */
+#ifdef _WIN32
+ caml_putword(dbg_out, _getpid());
+#else
+ caml_putword(dbg_out, getpid());
+#endif
+ caml_flush(dbg_out);
+}
+
+static void close_connection(void)
+{
+ caml_close_channel(dbg_in);
+ caml_close_channel(dbg_out);
+ dbg_socket = -1; /* was closed by caml_close_channel */
+}
+
+#ifdef _WIN32
+static void winsock_startup(void)
+{
+ WSADATA wsaData;
+ int err = WSAStartup(MAKEWORD(2, 0), &wsaData);
+ if (err) caml_fatal_error("WSAStartup failed");
+}
+
+static void winsock_cleanup(void)
+{
+ WSACleanup();
+}
+#endif
+
+void caml_debugger_init(void)
+{
+ char * address;
+ char_os * a;
+ size_t a_len;
+ char * port, * p;
+ struct hostent * host;
+ int n;
+
+ caml_register_global_root(&marshal_flags);
+ marshal_flags = caml_alloc(2, Tag_cons);
+ Store_field(marshal_flags, 0, Val_int(1)); /* Marshal.Closures */
+ Store_field(marshal_flags, 1, Val_emptylist);
+
+ a = caml_secure_getenv(_T("CAML_DEBUG_SOCKET"));
+ address = a ? caml_stat_strdup_of_os(a) : NULL;
+ if (address == NULL) return;
+ if (dbg_addr != NULL) caml_stat_free(dbg_addr);
+ dbg_addr = address;
+
+#ifdef _WIN32
+ winsock_startup();
+ (void)atexit(winsock_cleanup);
+#endif
+ /* Parse the address */
+ port = NULL;
+ for (p = address; *p != 0; p++) {
+ if (*p == ':') { *p = 0; port = p+1; break; }
+ }
+ if (port == NULL) {
+#ifndef _WIN32
+ /* Unix domain */
+ sock_domain = PF_UNIX;
+ sock_addr.s_unix.sun_family = AF_UNIX;
+ a_len = strlen(address);
+ if (a_len >= sizeof(sock_addr.s_unix.sun_path)) {
+ caml_fatal_error("Debug socket path length exceeds maximum permitted length");
+ }
+ strncpy(sock_addr.s_unix.sun_path, address,
+ sizeof(sock_addr.s_unix.sun_path) - 1);
+ sock_addr.s_unix.sun_path[sizeof(sock_addr.s_unix.sun_path) - 1] = '\0';
+ sock_addr_len =
+ ((char *)&(sock_addr.s_unix.sun_path) - (char *)&(sock_addr.s_unix))
+ + a_len;
+#else
+ caml_fatal_error("Unix sockets not supported");
+#endif
+ } else {
+ /* Internet domain */
+ sock_domain = PF_INET;
+ for (p = (char *) &sock_addr.s_inet, n = sizeof(sock_addr.s_inet);
+ n > 0; n--) *p++ = 0;
+ sock_addr.s_inet.sin_family = AF_INET;
+ sock_addr.s_inet.sin_addr.s_addr = inet_addr(address);
+ if (sock_addr.s_inet.sin_addr.s_addr == -1) {
+ host = gethostbyname(address);
+ if (host == NULL)
+ caml_fatal_error_arg("Unknown debugging host %s\n", address);
+ memmove(&sock_addr.s_inet.sin_addr, host->h_addr, host->h_length);
+ }
+ sock_addr.s_inet.sin_port = htons(atoi(port));
+ sock_addr_len = sizeof(sock_addr.s_inet);
+ }
+ open_connection();
+ caml_debugger_in_use = 1;
+ caml_trap_barrier = caml_stack_high;
+}
+
+static value getval(struct channel *chan)
+{
+ value res;
+ if (caml_really_getblock(chan, (char *) &res, sizeof(res)) < sizeof(res))
+ caml_raise_end_of_file(); /* Bad, but consistent with caml_getword */
+ return res;
+}
+
+static void putval(struct channel *chan, value val)
+{
+ caml_really_putblock(chan, (char *) &val, sizeof(val));
+}
+
+static void safe_output_value(struct channel *chan, value val)
+{
+ struct longjmp_buffer raise_buf, * saved_external_raise;
+
+ /* Catch exceptions raised by [caml_output_val] */
+ saved_external_raise = caml_external_raise;
+ if (sigsetjmp(raise_buf.buf, 0) == 0) {
+ caml_external_raise = &raise_buf;
+ caml_output_val(chan, val, marshal_flags);
+ } else {
+ /* Send wrong magic number, will cause [caml_input_value] to fail */
+ caml_really_putblock(chan, "\000\000\000\000", 4);
+ }
+ caml_external_raise = saved_external_raise;
+}
+
+#define Pc(sp) ((code_t)((sp)[0]))
+#define Env(sp) ((sp)[1])
+#define Extra_args(sp) (Long_val(((sp)[2])))
+#define Locals(sp) ((sp) + 3)
+
+void caml_debugger(enum event_kind event)
+{
+ value * frame;
+ intnat i, pos;
+ value val;
+
+ if (dbg_socket == -1) return; /* Not connected to a debugger. */
+
+ /* Reset current frame */
+ frame = caml_extern_sp + 1;
+
+ /* Report the event to the debugger */
+ switch(event) {
+ case PROGRAM_START: /* Nothing to report */
+ goto command_loop;
+ case EVENT_COUNT:
+ caml_putch(dbg_out, REP_EVENT);
+ break;
+ case BREAKPOINT:
+ caml_putch(dbg_out, REP_BREAKPOINT);
+ break;
+ case PROGRAM_EXIT:
+ caml_putch(dbg_out, REP_EXITED);
+ break;
+ case TRAP_BARRIER:
+ caml_putch(dbg_out, REP_TRAP);
+ break;
+ case UNCAUGHT_EXC:
+ caml_putch(dbg_out, REP_UNCAUGHT_EXC);
+ break;
+ }
+ caml_putword(dbg_out, caml_event_count);
+ if (event == EVENT_COUNT || event == BREAKPOINT) {
+ caml_putword(dbg_out, caml_stack_high - frame);
+ caml_putword(dbg_out, (Pc(frame) - caml_start_code) * sizeof(opcode_t));
+ } else {
+ /* No PC and no stack frame associated with other events */
+ caml_putword(dbg_out, 0);
+ caml_putword(dbg_out, 0);
+ }
+ caml_flush(dbg_out);
+
+ command_loop:
+
+ /* Read and execute the commands sent by the debugger */
+ while(1) {
+ switch(caml_getch(dbg_in)) {
+ case REQ_SET_EVENT:
+ pos = caml_getword(dbg_in);
+ CAMLassert (pos >= 0);
+ CAMLassert (pos < caml_code_size);
+ caml_set_instruction(caml_start_code + pos / sizeof(opcode_t), EVENT);
+ break;
+ case REQ_SET_BREAKPOINT:
+ pos = caml_getword(dbg_in);
+ CAMLassert (pos >= 0);
+ CAMLassert (pos < caml_code_size);
+ caml_set_instruction(caml_start_code + pos / sizeof(opcode_t), BREAK);
+ break;
+ case REQ_RESET_INSTR:
+ pos = caml_getword(dbg_in);
+ CAMLassert (pos >= 0);
+ CAMLassert (pos < caml_code_size);
+ pos = pos / sizeof(opcode_t);
+ caml_set_instruction(caml_start_code + pos, caml_saved_code[pos]);
+ break;
+ case REQ_CHECKPOINT:
+#ifndef _WIN32
+ i = fork();
+ if (i == 0) {
+ close_connection(); /* Close parent connection. */
+ open_connection(); /* Open new connection with debugger */
+ } else {
+ caml_putword(dbg_out, i);
+ caml_flush(dbg_out);
+ }
+#else
+ caml_fatal_error("error: REQ_CHECKPOINT command");
+ exit(-1);
+#endif
+ break;
+ case REQ_GO:
+ caml_event_count = caml_getword(dbg_in);
+ return;
+ case REQ_STOP:
+ exit(0);
+ break;
+ case REQ_WAIT:
+#ifndef _WIN32
+ wait(NULL);
+#else
+ caml_fatal_error("Fatal error: REQ_WAIT command");
+ exit(-1);
+#endif
+ break;
+ case REQ_INITIAL_FRAME:
+ frame = caml_extern_sp + 1;
+ /* Fall through */
+ case REQ_GET_FRAME:
+ caml_putword(dbg_out, caml_stack_high - frame);
+ if (frame < caml_stack_high){
+ caml_putword(dbg_out, (Pc(frame) - caml_start_code) * sizeof(opcode_t));
+ }else{
+ caml_putword (dbg_out, 0);
+ }
+ caml_flush(dbg_out);
+ break;
+ case REQ_SET_FRAME:
+ i = caml_getword(dbg_in);
+ frame = caml_stack_high - i;
+ break;
+ case REQ_UP_FRAME:
+ i = caml_getword(dbg_in);
+ if (frame + Extra_args(frame) + i + 3 >= caml_stack_high) {
+ caml_putword(dbg_out, -1);
+ } else {
+ frame += Extra_args(frame) + i + 3;
+ caml_putword(dbg_out, caml_stack_high - frame);
+ caml_putword(dbg_out, (Pc(frame) - caml_start_code) * sizeof(opcode_t));
+ }
+ caml_flush(dbg_out);
+ break;
+ case REQ_SET_TRAP_BARRIER:
+ i = caml_getword(dbg_in);
+ caml_trap_barrier = caml_stack_high - i;
+ break;
+ case REQ_GET_LOCAL:
+ i = caml_getword(dbg_in);
+ putval(dbg_out, Locals(frame)[i]);
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_ENVIRONMENT:
+ i = caml_getword(dbg_in);
+ putval(dbg_out, Field(Env(frame), i));
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_GLOBAL:
+ i = caml_getword(dbg_in);
+ putval(dbg_out, Field(caml_global_data, i));
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_ACCU:
+ putval(dbg_out, *caml_extern_sp);
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_HEADER:
+ val = getval(dbg_in);
+ caml_putword(dbg_out, Hd_val(val));
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_FIELD:
+ val = getval(dbg_in);
+ i = caml_getword(dbg_in);
+ if (Tag_val(val) != Double_array_tag) {
+ caml_putch(dbg_out, 0);
+ putval(dbg_out, Field(val, i));
+ } else {
+ double d = Double_flat_field(val, i);
+ caml_putch(dbg_out, 1);
+ caml_really_putblock(dbg_out, (char *) &d, 8);
+ }
+ caml_flush(dbg_out);
+ break;
+ case REQ_MARSHAL_OBJ:
+ val = getval(dbg_in);
+ safe_output_value(dbg_out, val);
+ caml_flush(dbg_out);
+ break;
+ case REQ_GET_CLOSURE_CODE:
+ val = getval(dbg_in);
+ caml_putword(dbg_out, (Code_val(val)-caml_start_code) * sizeof(opcode_t));
+ caml_flush(dbg_out);
+ break;
+ case REQ_SET_FORK_MODE:
+ caml_debugger_fork_mode = caml_getword(dbg_in);
+ break;
+ }
+ }
+}
+
+void caml_debugger_cleanup_fork(void)
+{
+ /* We could remove all of the breakpoints, but closing the connection
+ * means that they'll just be skipped anyway. */
+ close_connection();
+ caml_debugger_in_use = 0;
+}
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/dynlink.c b/test/monniaux/ocaml/byterun/dynlink.c
new file mode 100644
index 00000000..7c339bf5
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/dynlink.c
@@ -0,0 +1,300 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Dynamic loading of C primitives. */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include "caml/config.h"
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#include "caml/alloc.h"
+#include "caml/dynlink.h"
+#include "caml/fail.h"
+#include "caml/mlvalues.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/osdeps.h"
+#include "caml/prims.h"
+#include "caml/signals.h"
+
+#ifndef NATIVE_CODE
+
+/* The table of primitives */
+struct ext_table caml_prim_table;
+
+#ifdef DEBUG
+/* The names of primitives (for instrtrace.c) */
+struct ext_table caml_prim_name_table;
+#endif
+
+/* The table of shared libraries currently opened */
+static struct ext_table shared_libs;
+
+/* The search path for shared libraries */
+struct ext_table caml_shared_libs_path;
+
+/* Look up the given primitive name in the built-in primitive table,
+ then in the opened shared libraries (shared_libs) */
+static c_primitive lookup_primitive(char * name)
+{
+ int i;
+ void * res;
+
+ for (i = 0; caml_names_of_builtin_cprim[i] != NULL; i++) {
+ if (strcmp(name, caml_names_of_builtin_cprim[i]) == 0)
+ return caml_builtin_cprim[i];
+ }
+ for (i = 0; i < shared_libs.size; i++) {
+ res = caml_dlsym(shared_libs.contents[i], name);
+ if (res != NULL) return (c_primitive) res;
+ }
+ return NULL;
+}
+
+/* Parse the OCAML_STDLIB_DIR/ld.conf file and add the directories
+ listed there to the search path */
+
+#define LD_CONF_NAME _T("ld.conf")
+
+static char_os * parse_ld_conf(void)
+{
+ char_os * stdlib, * ldconfname, * wconfig, * p, * q;
+ char * config;
+#ifdef _WIN32
+ struct _stati64 st;
+#else
+ struct stat st;
+#endif
+ int ldconf, nread;
+
+ stdlib = caml_secure_getenv(_T("OCAMLLIB"));
+ if (stdlib == NULL) stdlib = caml_secure_getenv(_T("CAMLLIB"));
+ if (stdlib == NULL) stdlib = OCAML_STDLIB_DIR;
+ ldconfname = caml_stat_strconcat_os(3, stdlib, _T("/"), LD_CONF_NAME);
+ if (stat_os(ldconfname, &st) == -1) {
+ caml_stat_free(ldconfname);
+ return NULL;
+ }
+ ldconf = open_os(ldconfname, O_RDONLY, 0);
+ if (ldconf == -1)
+ caml_fatal_error_arg("Fatal error: cannot read loader config file %s\n",
+ caml_stat_strdup_of_os(ldconfname));
+ config = caml_stat_alloc(st.st_size + 1);
+ nread = read(ldconf, config, st.st_size);
+ if (nread == -1)
+ caml_fatal_error_arg
+ ("Fatal error: error while reading loader config file %s\n",
+ caml_stat_strdup_of_os(ldconfname));
+ config[nread] = 0;
+ wconfig = caml_stat_strdup_to_os(config);
+ caml_stat_free(config);
+ q = wconfig;
+ for (p = wconfig; *p != 0; p++) {
+ if (*p == _T('\n')) {
+ *p = 0;
+ caml_ext_table_add(&caml_shared_libs_path, q);
+ q = p + 1;
+ }
+ }
+ if (q < p) caml_ext_table_add(&caml_shared_libs_path, q);
+ close(ldconf);
+ caml_stat_free(ldconfname);
+ return wconfig;
+}
+
+/* Open the given shared library and add it to shared_libs.
+ Abort on error. */
+static void open_shared_lib(char_os * name)
+{
+ char_os * realname;
+ char * u8;
+ void * handle;
+
+ realname = caml_search_dll_in_path(&caml_shared_libs_path, name);
+ u8 = caml_stat_strdup_of_os(realname);
+ caml_gc_message(0x100, "Loading shared library %s\n", u8);
+ caml_stat_free(u8);
+ caml_enter_blocking_section();
+ handle = caml_dlopen(realname, 1, 1);
+ caml_leave_blocking_section();
+ if (handle == NULL)
+ caml_fatal_error_arg2("Fatal error: cannot load shared library %s\n",
+ caml_stat_strdup_of_os(name),
+ "Reason: %s\n", caml_dlerror());
+ caml_ext_table_add(&shared_libs, handle);
+ caml_stat_free(realname);
+}
+
+/* Build the table of primitives, given a search path and a list
+ of shared libraries (both 0-separated in a char array).
+ Abort the runtime system on error. */
+void caml_build_primitive_table(char_os * lib_path,
+ char_os * libs,
+ char * req_prims)
+{
+ char_os * tofree1, * tofree2;
+ char_os * p;
+ char * q;
+
+ /* Initialize the search path for dynamic libraries:
+ - directories specified on the command line with the -I option
+ - directories specified in the CAML_LD_LIBRARY_PATH
+ - directories specified in the executable
+ - directories specified in the file <stdlib>/ld.conf */
+ tofree1 = caml_decompose_path(&caml_shared_libs_path,
+ caml_secure_getenv(_T("CAML_LD_LIBRARY_PATH")));
+ if (lib_path != NULL)
+ for (p = lib_path; *p != 0; p += strlen_os(p) + 1)
+ caml_ext_table_add(&caml_shared_libs_path, p);
+ tofree2 = parse_ld_conf();
+ /* Open the shared libraries */
+ caml_ext_table_init(&shared_libs, 8);
+ if (libs != NULL)
+ for (p = libs; *p != 0; p += strlen_os(p) + 1)
+ open_shared_lib(p);
+ /* Build the primitive table */
+ caml_ext_table_init(&caml_prim_table, 0x180);
+#ifdef DEBUG
+ caml_ext_table_init(&caml_prim_name_table, 0x180);
+#endif
+ for (q = req_prims; *q != 0; q += strlen(q) + 1) {
+ c_primitive prim = lookup_primitive(q);
+ if (prim == NULL)
+ caml_fatal_error_arg("Fatal error: unknown C primitive `%s'\n", q);
+ caml_ext_table_add(&caml_prim_table, (void *) prim);
+#ifdef DEBUG
+ caml_ext_table_add(&caml_prim_name_table, caml_stat_strdup(q));
+#endif
+ }
+ /* Clean up */
+ caml_stat_free(tofree1);
+ caml_stat_free(tofree2);
+ caml_ext_table_free(&caml_shared_libs_path, 0);
+}
+
+/* Build the table of primitives as a copy of the builtin primitive table.
+ Used for executables generated by ocamlc -output-obj. */
+
+void caml_build_primitive_table_builtin(void)
+{
+ int i;
+ caml_ext_table_init(&caml_prim_table, 0x180);
+#ifdef DEBUG
+ caml_ext_table_init(&caml_prim_name_table, 0x180);
+#endif
+ for (i = 0; caml_builtin_cprim[i] != 0; i++) {
+ caml_ext_table_add(&caml_prim_table, (void *) caml_builtin_cprim[i]);
+#ifdef DEBUG
+ caml_ext_table_add(&caml_prim_name_table,
+ caml_stat_strdup(caml_names_of_builtin_cprim[i]));
+#endif
+ }
+}
+
+void caml_free_shared_libs(void)
+{
+ while (shared_libs.size > 0)
+ caml_dlclose(shared_libs.contents[--shared_libs.size]);
+}
+
+#endif /* NATIVE_CODE */
+
+/** dlopen interface for the bytecode linker **/
+
+#define Handle_val(v) (*((void **) (v)))
+
+CAMLprim value caml_dynlink_open_lib(value mode, value filename)
+{
+ void * handle;
+ value result;
+ char_os * p;
+
+ caml_gc_message(0x100, "Opening shared library %s\n",
+ String_val(filename));
+ p = caml_stat_strdup_to_os(String_val(filename));
+ caml_enter_blocking_section();
+ handle = caml_dlopen(p, Int_val(mode), 1);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+ if (handle == NULL) caml_failwith(caml_dlerror());
+ result = caml_alloc_small(1, Abstract_tag);
+ Handle_val(result) = handle;
+ return result;
+}
+
+CAMLprim value caml_dynlink_close_lib(value handle)
+{
+ caml_dlclose(Handle_val(handle));
+ return Val_unit;
+}
+
+/*#include <stdio.h>*/
+CAMLprim value caml_dynlink_lookup_symbol(value handle, value symbolname)
+{
+ void * symb;
+ value result;
+ symb = caml_dlsym(Handle_val(handle), String_val(symbolname));
+ /* printf("%s = 0x%lx\n", String_val(symbolname), symb);
+ fflush(stdout); */
+ if (symb == NULL) return Val_unit /*caml_failwith(caml_dlerror())*/;
+ result = caml_alloc_small(1, Abstract_tag);
+ Handle_val(result) = symb;
+ return result;
+}
+
+#ifndef NATIVE_CODE
+
+CAMLprim value caml_dynlink_add_primitive(value handle)
+{
+ return Val_int(caml_ext_table_add(&caml_prim_table, Handle_val(handle)));
+}
+
+CAMLprim value caml_dynlink_get_current_libs(value unit)
+{
+ CAMLparam0();
+ CAMLlocal1(res);
+ int i;
+
+ res = caml_alloc_tuple(shared_libs.size);
+ for (i = 0; i < shared_libs.size; i++) {
+ value v = caml_alloc_small(1, Abstract_tag);
+ Handle_val(v) = shared_libs.contents[i];
+ Store_field(res, i, v);
+ }
+ CAMLreturn(res);
+}
+
+#else
+
+value caml_dynlink_add_primitive(value handle)
+{
+ caml_invalid_argument("dynlink_add_primitive");
+ return Val_unit; /* not reached */
+}
+
+value caml_dynlink_get_current_libs(value unit)
+{
+ caml_invalid_argument("dynlink_get_current_libs");
+ return Val_unit; /* not reached */
+}
+
+#endif /* NATIVE_CODE */
diff --git a/test/monniaux/ocaml/byterun/extern.c b/test/monniaux/ocaml/byterun/extern.c
new file mode 100644
index 00000000..db7163cd
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/extern.c
@@ -0,0 +1,925 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Structured output */
+
+/* The interface of this file is "caml/intext.h" */
+
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/config.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/gc.h"
+#include "caml/intext.h"
+#include "caml/io.h"
+#include "caml/md5.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/reverse.h"
+
+static uintnat obj_counter; /* Number of objects emitted so far */
+static uintnat size_32; /* Size in words of 32-bit block for struct. */
+static uintnat size_64; /* Size in words of 64-bit block for struct. */
+
+/* Flags affecting marshaling */
+
+enum {
+ NO_SHARING = 1, /* Flag to ignore sharing */
+ CLOSURES = 2, /* Flag to allow marshaling code pointers */
+ COMPAT_32 = 4 /* Flag to ensure that output can safely
+ be read back on a 32-bit platform */
+};
+
+static int extern_flags; /* logical or of some of the flags above */
+
+/* Trail mechanism to undo forwarding pointers put inside objects */
+
+struct trail_entry {
+ value obj; /* address of object + initial color in low 2 bits */
+ value field0; /* initial contents of field 0 */
+};
+
+struct trail_block {
+ struct trail_block * previous;
+ struct trail_entry entries[ENTRIES_PER_TRAIL_BLOCK];
+};
+
+static struct trail_block extern_trail_first;
+static struct trail_block * extern_trail_block;
+static struct trail_entry * extern_trail_cur, * extern_trail_limit;
+
+
+/* Stack for pending values to marshal */
+
+struct extern_item { value * v; mlsize_t count; };
+
+#define EXTERN_STACK_INIT_SIZE 256
+#define EXTERN_STACK_MAX_SIZE (1024*1024*100)
+
+static struct extern_item extern_stack_init[EXTERN_STACK_INIT_SIZE];
+
+static struct extern_item * extern_stack = extern_stack_init;
+static struct extern_item * extern_stack_limit = extern_stack_init
+ + EXTERN_STACK_INIT_SIZE;
+
+/* Forward declarations */
+
+CAMLnoreturn_start
+static void extern_out_of_memory(void)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+static void extern_invalid_argument(char *msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+static void extern_failwith(char *msg)
+CAMLnoreturn_end;
+
+CAMLnoreturn_start
+static void extern_stack_overflow(void)
+CAMLnoreturn_end;
+
+static void extern_replay_trail(void);
+static void free_extern_output(void);
+
+/* Free the extern stack if needed */
+static void extern_free_stack(void)
+{
+ if (extern_stack != extern_stack_init) {
+ caml_stat_free(extern_stack);
+ /* Reinitialize the globals for next time around */
+ extern_stack = extern_stack_init;
+ extern_stack_limit = extern_stack + EXTERN_STACK_INIT_SIZE;
+ }
+}
+
+static struct extern_item * extern_resize_stack(struct extern_item * sp)
+{
+ asize_t newsize = 2 * (extern_stack_limit - extern_stack);
+ asize_t sp_offset = sp - extern_stack;
+ struct extern_item * newstack;
+
+ if (newsize >= EXTERN_STACK_MAX_SIZE) extern_stack_overflow();
+ if (extern_stack == extern_stack_init) {
+ newstack = caml_stat_alloc_noexc(sizeof(struct extern_item) * newsize);
+ if (newstack == NULL) extern_stack_overflow();
+ memcpy(newstack, extern_stack_init,
+ sizeof(struct extern_item) * EXTERN_STACK_INIT_SIZE);
+ } else {
+ newstack = caml_stat_resize_noexc(extern_stack,
+ sizeof(struct extern_item) * newsize);
+ if (newstack == NULL) extern_stack_overflow();
+ }
+ extern_stack = newstack;
+ extern_stack_limit = newstack + newsize;
+ return newstack + sp_offset;
+}
+
+/* Initialize the trail */
+
+static void init_extern_trail(void)
+{
+ extern_trail_block = &extern_trail_first;
+ extern_trail_cur = extern_trail_block->entries;
+ extern_trail_limit = extern_trail_block->entries + ENTRIES_PER_TRAIL_BLOCK;
+}
+
+/* Replay the trail, undoing the in-place modifications
+ performed on objects */
+
+static void extern_replay_trail(void)
+{
+ struct trail_block * blk, * prevblk;
+ struct trail_entry * ent, * lim;
+
+ blk = extern_trail_block;
+ lim = extern_trail_cur;
+ while (1) {
+ for (ent = &(blk->entries[0]); ent < lim; ent++) {
+ value obj = ent->obj;
+ color_t colornum = obj & 3;
+ obj = obj & ~3;
+ Hd_val(obj) = Coloredhd_hd(Hd_val(obj), colornum);
+ Field(obj, 0) = ent->field0;
+ }
+ if (blk == &extern_trail_first) break;
+ prevblk = blk->previous;
+ caml_stat_free(blk);
+ blk = prevblk;
+ lim = &(blk->entries[ENTRIES_PER_TRAIL_BLOCK]);
+ }
+ /* Protect against a second call to extern_replay_trail */
+ extern_trail_block = &extern_trail_first;
+ extern_trail_cur = extern_trail_block->entries;
+}
+
+/* Set forwarding pointer on an object and add corresponding entry
+ to the trail. */
+
+static void extern_record_location(value obj)
+{
+ header_t hdr;
+
+ if (extern_flags & NO_SHARING) return;
+ if (extern_trail_cur == extern_trail_limit) {
+ struct trail_block * new_block = caml_stat_alloc_noexc(sizeof(struct trail_block));
+ if (new_block == NULL) extern_out_of_memory();
+ new_block->previous = extern_trail_block;
+ extern_trail_block = new_block;
+ extern_trail_cur = extern_trail_block->entries;
+ extern_trail_limit = extern_trail_block->entries + ENTRIES_PER_TRAIL_BLOCK;
+ }
+ hdr = Hd_val(obj);
+ extern_trail_cur->obj = obj | Colornum_hd(hdr);
+ extern_trail_cur->field0 = Field(obj, 0);
+ extern_trail_cur++;
+ Hd_val(obj) = Bluehd_hd(hdr);
+ Field(obj, 0) = (value) obj_counter;
+ obj_counter++;
+}
+
+/* To buffer the output */
+
+static char * extern_userprovided_output;
+static char * extern_ptr, * extern_limit;
+
+struct output_block {
+ struct output_block * next;
+ char * end;
+ char data[SIZE_EXTERN_OUTPUT_BLOCK];
+};
+
+static struct output_block * extern_output_first, * extern_output_block;
+
+static void init_extern_output(void)
+{
+ extern_userprovided_output = NULL;
+ extern_output_first = caml_stat_alloc_noexc(sizeof(struct output_block));
+ if (extern_output_first == NULL) caml_raise_out_of_memory();
+ extern_output_block = extern_output_first;
+ extern_output_block->next = NULL;
+ extern_ptr = extern_output_block->data;
+ extern_limit = extern_output_block->data + SIZE_EXTERN_OUTPUT_BLOCK;
+}
+
+static void close_extern_output(void)
+{
+ if (extern_userprovided_output == NULL){
+ extern_output_block->end = extern_ptr;
+ }
+}
+
+static void free_extern_output(void)
+{
+ struct output_block * blk, * nextblk;
+
+ if (extern_userprovided_output != NULL) return;
+ for (blk = extern_output_first; blk != NULL; blk = nextblk) {
+ nextblk = blk->next;
+ caml_stat_free(blk);
+ }
+ extern_output_first = NULL;
+ extern_free_stack();
+}
+
+static void grow_extern_output(intnat required)
+{
+ struct output_block * blk;
+ intnat extra;
+
+ if (extern_userprovided_output != NULL) {
+ extern_failwith("Marshal.to_buffer: buffer overflow");
+ }
+ extern_output_block->end = extern_ptr;
+ if (required <= SIZE_EXTERN_OUTPUT_BLOCK / 2)
+ extra = 0;
+ else
+ extra = required;
+ blk = caml_stat_alloc_noexc(sizeof(struct output_block) + extra);
+ if (blk == NULL) extern_out_of_memory();
+ extern_output_block->next = blk;
+ extern_output_block = blk;
+ extern_output_block->next = NULL;
+ extern_ptr = extern_output_block->data;
+ extern_limit = extern_output_block->data + SIZE_EXTERN_OUTPUT_BLOCK + extra;
+}
+
+static intnat extern_output_length(void)
+{
+ struct output_block * blk;
+ intnat len;
+
+ if (extern_userprovided_output != NULL) {
+ return extern_ptr - extern_userprovided_output;
+ } else {
+ for (len = 0, blk = extern_output_first; blk != NULL; blk = blk->next)
+ len += blk->end - blk->data;
+ return len;
+ }
+}
+
+/* Exception raising, with cleanup */
+
+static void extern_out_of_memory(void)
+{
+ extern_replay_trail();
+ free_extern_output();
+ caml_raise_out_of_memory();
+}
+
+static void extern_invalid_argument(char *msg)
+{
+ extern_replay_trail();
+ free_extern_output();
+ caml_invalid_argument(msg);
+}
+
+static void extern_failwith(char *msg)
+{
+ extern_replay_trail();
+ free_extern_output();
+ caml_failwith(msg);
+}
+
+static void extern_stack_overflow(void)
+{
+ caml_gc_message (0x04, "Stack overflow in marshaling value\n");
+ extern_replay_trail();
+ free_extern_output();
+ caml_raise_out_of_memory();
+}
+
+/* Conversion to big-endian */
+
+static inline void store16(char * dst, int n)
+{
+ dst[0] = n >> 8; dst[1] = n;
+}
+
+static inline void store32(char * dst, intnat n)
+{
+ dst[0] = n >> 24; dst[1] = n >> 16; dst[2] = n >> 8; dst[3] = n;
+}
+
+static inline void store64(char * dst, int64_t n)
+{
+ dst[0] = n >> 56; dst[1] = n >> 48; dst[2] = n >> 40; dst[3] = n >> 32;
+ dst[4] = n >> 24; dst[5] = n >> 16; dst[6] = n >> 8; dst[7] = n;
+}
+
+/* Write characters, integers, and blocks in the output buffer */
+
+static inline void write(int c)
+{
+ if (extern_ptr >= extern_limit) grow_extern_output(1);
+ *extern_ptr++ = c;
+}
+
+static void writeblock(const char * data, intnat len)
+{
+ if (extern_ptr + len > extern_limit) grow_extern_output(len);
+ memcpy(extern_ptr, data, len);
+ extern_ptr += len;
+}
+
+static inline void writeblock_float8(const double * data, intnat ndoubles)
+{
+#if ARCH_FLOAT_ENDIANNESS == 0x01234567 || ARCH_FLOAT_ENDIANNESS == 0x76543210
+ writeblock((const char *) data, ndoubles * 8);
+#else
+ caml_serialize_block_float_8(data, ndoubles);
+#endif
+}
+
+static void writecode8(int code, intnat val)
+{
+ if (extern_ptr + 2 > extern_limit) grow_extern_output(2);
+ extern_ptr[0] = code;
+ extern_ptr[1] = val;
+ extern_ptr += 2;
+}
+
+static void writecode16(int code, intnat val)
+{
+ if (extern_ptr + 3 > extern_limit) grow_extern_output(3);
+ extern_ptr[0] = code;
+ store16(extern_ptr + 1, val);
+ extern_ptr += 3;
+}
+
+static void writecode32(int code, intnat val)
+{
+ if (extern_ptr + 5 > extern_limit) grow_extern_output(5);
+ extern_ptr[0] = code;
+ store32(extern_ptr + 1, val);
+ extern_ptr += 5;
+}
+
+#ifdef ARCH_SIXTYFOUR
+static void writecode64(int code, intnat val)
+{
+ if (extern_ptr + 9 > extern_limit) grow_extern_output(9);
+ extern_ptr[0] = code;
+ store64(extern_ptr + 1, val);
+ extern_ptr += 9;
+}
+#endif
+
+/* Marshal the given value in the output buffer */
+
+int caml_extern_allow_out_of_heap = 0;
+
+static void extern_rec(value v)
+{
+ struct code_fragment * cf;
+ struct extern_item * sp;
+ sp = extern_stack;
+
+ while(1) {
+ if (Is_long(v)) {
+ intnat n = Long_val(v);
+ if (n >= 0 && n < 0x40) {
+ write(PREFIX_SMALL_INT + n);
+ } else if (n >= -(1 << 7) && n < (1 << 7)) {
+ writecode8(CODE_INT8, n);
+ } else if (n >= -(1 << 15) && n < (1 << 15)) {
+ writecode16(CODE_INT16, n);
+#ifdef ARCH_SIXTYFOUR
+ } else if (n < -((intnat)1 << 30) || n >= ((intnat)1 << 30)) {
+ if (extern_flags & COMPAT_32)
+ extern_failwith("output_value: integer cannot be read back on "
+ "32-bit platform");
+ writecode64(CODE_INT64, n);
+#endif
+ } else
+ writecode32(CODE_INT32, n);
+ goto next_item;
+ }
+ if (Is_in_value_area(v) || caml_extern_allow_out_of_heap) {
+ header_t hd = Hd_val(v);
+ tag_t tag = Tag_hd(hd);
+ mlsize_t sz = Wosize_hd(hd);
+
+ if (tag == Forward_tag) {
+ value f = Forward_val (v);
+ if (Is_block (f)
+ && (!Is_in_value_area(f) || Tag_val (f) == Forward_tag
+ || Tag_val (f) == Lazy_tag
+#ifdef FLAT_FLOAT_ARRAY
+ || Tag_val (f) == Double_tag
+#endif
+ )){
+ /* Do not short-circuit the pointer. */
+ }else{
+ v = f;
+ continue;
+ }
+ }
+ /* Atoms are treated specially for two reasons: they are not allocated
+ in the externed block, and they are automatically shared. */
+ if (sz == 0) {
+ if (tag < 16) {
+ write(PREFIX_SMALL_BLOCK + tag);
+ } else {
+#ifdef WITH_PROFINFO
+ writecode32(CODE_BLOCK32, Hd_no_profinfo(hd));
+#else
+ writecode32(CODE_BLOCK32, hd);
+#endif
+ }
+ goto next_item;
+ }
+ /* Check if already seen */
+ if (Color_hd(hd) == Caml_blue) {
+ uintnat d = obj_counter - (uintnat) Field(v, 0);
+ if (d < 0x100) {
+ writecode8(CODE_SHARED8, d);
+ } else if (d < 0x10000) {
+ writecode16(CODE_SHARED16, d);
+#ifdef ARCH_SIXTYFOUR
+ } else if (d >= (uintnat)1 << 32) {
+ writecode64(CODE_SHARED64, d);
+#endif
+ } else {
+ writecode32(CODE_SHARED32, d);
+ }
+ goto next_item;
+ }
+
+ /* Output the contents of the object */
+ switch(tag) {
+ case String_tag: {
+ mlsize_t len = caml_string_length(v);
+ if (len < 0x20) {
+ write(PREFIX_SMALL_STRING + len);
+ } else if (len < 0x100) {
+ writecode8(CODE_STRING8, len);
+ } else {
+#ifdef ARCH_SIXTYFOUR
+ if (len > 0xFFFFFB && (extern_flags & COMPAT_32))
+ extern_failwith("output_value: string cannot be read back on "
+ "32-bit platform");
+ if (len < (uintnat)1 << 32)
+ writecode32(CODE_STRING32, len);
+ else
+ writecode64(CODE_STRING64, len);
+#else
+ writecode32(CODE_STRING32, len);
+#endif
+ }
+ writeblock(String_val(v), len);
+ size_32 += 1 + (len + 4) / 4;
+ size_64 += 1 + (len + 8) / 8;
+ extern_record_location(v);
+ break;
+ }
+ case Double_tag: {
+ if (sizeof(double) != 8)
+ extern_invalid_argument("output_value: non-standard floats");
+ write(CODE_DOUBLE_NATIVE);
+ writeblock_float8((double *) v, 1);
+ size_32 += 1 + 2;
+ size_64 += 1 + 1;
+ extern_record_location(v);
+ break;
+ }
+ case Double_array_tag: {
+ mlsize_t nfloats;
+ if (sizeof(double) != 8)
+ extern_invalid_argument("output_value: non-standard floats");
+ nfloats = Wosize_val(v) / Double_wosize;
+ if (nfloats < 0x100) {
+ writecode8(CODE_DOUBLE_ARRAY8_NATIVE, nfloats);
+ } else {
+#ifdef ARCH_SIXTYFOUR
+ if (nfloats > 0x1FFFFF && (extern_flags & COMPAT_32))
+ extern_failwith("output_value: float array cannot be read back on "
+ "32-bit platform");
+ if (nfloats < (uintnat) 1 << 32)
+ writecode32(CODE_DOUBLE_ARRAY32_NATIVE, nfloats);
+ else
+ writecode64(CODE_DOUBLE_ARRAY64_NATIVE, nfloats);
+#else
+ writecode32(CODE_DOUBLE_ARRAY32_NATIVE, nfloats);
+#endif
+ }
+ writeblock_float8((double *) v, nfloats);
+ size_32 += 1 + nfloats * 2;
+ size_64 += 1 + nfloats;
+ extern_record_location(v);
+ break;
+ }
+ case Abstract_tag:
+ extern_invalid_argument("output_value: abstract value (Abstract)");
+ break;
+ case Infix_tag:
+ writecode32(CODE_INFIXPOINTER, Infix_offset_hd(hd));
+ v = v - Infix_offset_hd(hd); /* PR#5772 */
+ continue;
+ case Custom_tag: {
+ uintnat sz_32, sz_64;
+ char * ident = Custom_ops_val(v)->identifier;
+ void (*serialize)(value v, uintnat * bsize_32,
+ uintnat * bsize_64)
+ = Custom_ops_val(v)->serialize;
+ if (serialize == NULL)
+ extern_invalid_argument("output_value: abstract value (Custom)");
+ write(CODE_CUSTOM);
+ writeblock(ident, strlen(ident) + 1);
+ Custom_ops_val(v)->serialize(v, &sz_32, &sz_64);
+ size_32 += 2 + ((sz_32 + 3) >> 2); /* header + ops + data */
+ size_64 += 2 + ((sz_64 + 7) >> 3);
+ extern_record_location(v);
+ break;
+ }
+ default: {
+ value field0;
+ if (tag < 16 && sz < 8) {
+ write(PREFIX_SMALL_BLOCK + tag + (sz << 4));
+ } else {
+#ifdef ARCH_SIXTYFOUR
+#ifdef WITH_PROFINFO
+ header_t hd_erased = Hd_no_profinfo(hd);
+#else
+ header_t hd_erased = hd;
+#endif
+ if (sz > 0x3FFFFF && (extern_flags & COMPAT_32))
+ extern_failwith("output_value: array cannot be read back on "
+ "32-bit platform");
+ if (hd_erased < (uintnat)1 << 32)
+ writecode32(CODE_BLOCK32, Whitehd_hd (hd_erased));
+ else
+ writecode64(CODE_BLOCK64, Whitehd_hd (hd_erased));
+#else
+ writecode32(CODE_BLOCK32, Whitehd_hd (hd));
+#endif
+ }
+ size_32 += 1 + sz;
+ size_64 += 1 + sz;
+ field0 = Field(v, 0);
+ extern_record_location(v);
+ /* Remember that we still have to serialize fields 1 ... sz - 1 */
+ if (sz > 1) {
+ sp++;
+ if (sp >= extern_stack_limit) sp = extern_resize_stack(sp);
+ sp->v = &Field(v,1);
+ sp->count = sz-1;
+ }
+ /* Continue serialization with the first field */
+ v = field0;
+ continue;
+ }
+ }
+ }
+ else if ((cf = caml_extern_find_code((char *) v)) != NULL) {
+ if ((extern_flags & CLOSURES) == 0)
+ extern_invalid_argument("output_value: functional value");
+ writecode32(CODE_CODEPOINTER, (char *) v - cf->code_start);
+ writeblock((const char *)cf->digest, 16);
+ } else {
+ extern_invalid_argument("output_value: abstract value (outside heap)");
+ }
+ next_item:
+ /* Pop one more item to marshal, if any */
+ if (sp == extern_stack) {
+ /* We are done. Cleanup the stack and leave the function */
+ extern_free_stack();
+ return;
+ }
+ v = *((sp->v)++);
+ if (--(sp->count) == 0) sp--;
+ }
+ /* Never reached as function leaves with return */
+}
+
+static int extern_flag_values[] = { NO_SHARING, CLOSURES, COMPAT_32 };
+
+static intnat extern_value(value v, value flags,
+ /*out*/ char header[32],
+ /*out*/ int * header_len)
+{
+ intnat res_len;
+ /* Parse flag list */
+ extern_flags = caml_convert_flag_list(flags, extern_flag_values);
+ /* Initializations */
+ init_extern_trail();
+ obj_counter = 0;
+ size_32 = 0;
+ size_64 = 0;
+ /* Marshal the object */
+ extern_rec(v);
+ /* Record end of output */
+ close_extern_output();
+ /* Undo the modifications done on externed blocks */
+ extern_replay_trail();
+ /* Write the header */
+ res_len = extern_output_length();
+#ifdef ARCH_SIXTYFOUR
+ if (res_len >= ((intnat)1 << 32) ||
+ size_32 >= ((intnat)1 << 32) || size_64 >= ((intnat)1 << 32)) {
+ /* The object is too big for the small header format.
+ Fail if we are in compat32 mode, or use big header. */
+ if (extern_flags & COMPAT_32) {
+ free_extern_output();
+ caml_failwith("output_value: object too big to be read back on "
+ "32-bit platform");
+ }
+ store32(header, Intext_magic_number_big);
+ store32(header + 4, 0);
+ store64(header + 8, res_len);
+ store64(header + 16, obj_counter);
+ store64(header + 24, size_64);
+ *header_len = 32;
+ return res_len;
+ }
+#endif
+ /* Use the small header format */
+ store32(header, Intext_magic_number_small);
+ store32(header + 4, res_len);
+ store32(header + 8, obj_counter);
+ store32(header + 12, size_32);
+ store32(header + 16, size_64);
+ *header_len = 20;
+ return res_len;
+}
+
+void caml_output_val(struct channel *chan, value v, value flags)
+{
+ char header[32];
+ int header_len;
+ struct output_block * blk, * nextblk;
+
+ if (! caml_channel_binary_mode(chan))
+ caml_failwith("output_value: not a binary channel");
+ init_extern_output();
+ extern_value(v, flags, header, &header_len);
+ /* During [caml_really_putblock], concurrent [caml_output_val] operations
+ can take place (via signal handlers or context switching in systhreads),
+ and [extern_output_first] may change. So, save it in a local variable. */
+ blk = extern_output_first;
+ caml_really_putblock(chan, header, header_len);
+ while (blk != NULL) {
+ caml_really_putblock(chan, blk->data, blk->end - blk->data);
+ nextblk = blk->next;
+ caml_stat_free(blk);
+ blk = nextblk;
+ }
+}
+
+CAMLprim value caml_output_value(value vchan, value v, value flags)
+{
+ CAMLparam3 (vchan, v, flags);
+ struct channel * channel = Channel(vchan);
+
+ Lock(channel);
+ caml_output_val(channel, v, flags);
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_output_value_to_bytes(value v, value flags)
+{
+ char header[32];
+ int header_len;
+ intnat data_len, ofs;
+ value res;
+ struct output_block * blk, * nextblk;
+
+ init_extern_output();
+ data_len = extern_value(v, flags, header, &header_len);
+ /* PR#4030: it is prudent to save extern_output_first before allocating
+ the result, as in caml_output_val */
+ blk = extern_output_first;
+ res = caml_alloc_string(header_len + data_len);
+ ofs = 0;
+ memcpy(&Byte(res, ofs), header, header_len);
+ ofs += header_len;
+ while (blk != NULL) {
+ int n = blk->end - blk->data;
+ memcpy(&Byte(res, ofs), blk->data, n);
+ ofs += n;
+ nextblk = blk->next;
+ caml_stat_free(blk);
+ blk = nextblk;
+ }
+ return res;
+}
+
+CAMLprim value caml_output_value_to_string(value v, value flags)
+{
+ return caml_output_value_to_bytes(v,flags);
+}
+
+CAMLexport intnat caml_output_value_to_block(value v, value flags,
+ char * buf, intnat len)
+{
+ char header[32];
+ int header_len;
+ intnat data_len;
+ /* At this point we don't know the size of the header.
+ Guess that it is small, and fix up later if not. */
+ extern_userprovided_output = buf + 20;
+ extern_ptr = extern_userprovided_output;
+ extern_limit = buf + len;
+ data_len = extern_value(v, flags, header, &header_len);
+ if (header_len != 20) {
+ /* Bad guess! Need to shift the output to make room for big header.
+ Make sure there is room. */
+ if (header_len + data_len > len)
+ caml_failwith("Marshal.to_buffer: buffer overflow");
+ memmove(buf + header_len, buf + 20, data_len);
+ }
+ memcpy(buf, header, header_len);
+ return header_len + data_len;
+}
+
+CAMLprim value caml_output_value_to_buffer(value buf, value ofs, value len,
+ value v, value flags)
+{
+ intnat l =
+ caml_output_value_to_block(v, flags,
+ &Byte(buf, Long_val(ofs)), Long_val(len));
+ return Val_long(l);
+}
+
+CAMLexport void caml_output_value_to_malloc(value v, value flags,
+ /*out*/ char ** buf,
+ /*out*/ intnat * len)
+{
+ char header[32];
+ int header_len;
+ intnat data_len;
+ char * res;
+ struct output_block * blk;
+
+ init_extern_output();
+ data_len = extern_value(v, flags, header, &header_len);
+ res = caml_stat_alloc_noexc(header_len + data_len);
+ if (res == NULL) extern_out_of_memory();
+ *buf = res;
+ *len = header_len + data_len;
+ memcpy(res, header, header_len);
+ res += header_len;
+ for (blk = extern_output_first; blk != NULL; blk = blk->next) {
+ int n = blk->end - blk->data;
+ memcpy(res, blk->data, n);
+ res += n;
+ }
+ free_extern_output();
+}
+
+/* Functions for writing user-defined marshallers */
+
+CAMLexport void caml_serialize_int_1(int i)
+{
+ if (extern_ptr + 1 > extern_limit) grow_extern_output(1);
+ extern_ptr[0] = i;
+ extern_ptr += 1;
+}
+
+CAMLexport void caml_serialize_int_2(int i)
+{
+ if (extern_ptr + 2 > extern_limit) grow_extern_output(2);
+ store16(extern_ptr, i);
+ extern_ptr += 2;
+}
+
+CAMLexport void caml_serialize_int_4(int32_t i)
+{
+ if (extern_ptr + 4 > extern_limit) grow_extern_output(4);
+ store32(extern_ptr, i);
+ extern_ptr += 4;
+}
+
+CAMLexport void caml_serialize_int_8(int64_t i)
+{
+ if (extern_ptr + 8 > extern_limit) grow_extern_output(8);
+ store64(extern_ptr, i);
+ extern_ptr += 8;
+}
+
+CAMLexport void caml_serialize_float_4(float f)
+{
+ caml_serialize_block_4(&f, 1);
+}
+
+CAMLexport void caml_serialize_float_8(double f)
+{
+ caml_serialize_block_float_8(&f, 1);
+}
+
+CAMLexport void caml_serialize_block_1(void * data, intnat len)
+{
+ if (extern_ptr + len > extern_limit) grow_extern_output(len);
+ memcpy(extern_ptr, data, len);
+ extern_ptr += len;
+}
+
+CAMLexport void caml_serialize_block_2(void * data, intnat len)
+{
+ if (extern_ptr + 2 * len > extern_limit) grow_extern_output(2 * len);
+#ifndef ARCH_BIG_ENDIAN
+ {
+ unsigned char * p;
+ char * q;
+ for (p = data, q = extern_ptr; len > 0; len--, p += 2, q += 2)
+ Reverse_16(q, p);
+ extern_ptr = q;
+ }
+#else
+ memcpy(extern_ptr, data, len * 2);
+ extern_ptr += len * 2;
+#endif
+}
+
+CAMLexport void caml_serialize_block_4(void * data, intnat len)
+{
+ if (extern_ptr + 4 * len > extern_limit) grow_extern_output(4 * len);
+#ifndef ARCH_BIG_ENDIAN
+ {
+ unsigned char * p;
+ char * q;
+ for (p = data, q = extern_ptr; len > 0; len--, p += 4, q += 4)
+ Reverse_32(q, p);
+ extern_ptr = q;
+ }
+#else
+ memcpy(extern_ptr, data, len * 4);
+ extern_ptr += len * 4;
+#endif
+}
+
+CAMLexport void caml_serialize_block_8(void * data, intnat len)
+{
+ if (extern_ptr + 8 * len > extern_limit) grow_extern_output(8 * len);
+#ifndef ARCH_BIG_ENDIAN
+ {
+ unsigned char * p;
+ char * q;
+ for (p = data, q = extern_ptr; len > 0; len--, p += 8, q += 8)
+ Reverse_64(q, p);
+ extern_ptr = q;
+ }
+#else
+ memcpy(extern_ptr, data, len * 8);
+ extern_ptr += len * 8;
+#endif
+}
+
+CAMLexport void caml_serialize_block_float_8(void * data, intnat len)
+{
+ if (extern_ptr + 8 * len > extern_limit) grow_extern_output(8 * len);
+#if ARCH_FLOAT_ENDIANNESS == 0x01234567
+ memcpy(extern_ptr, data, len * 8);
+ extern_ptr += len * 8;
+#elif ARCH_FLOAT_ENDIANNESS == 0x76543210
+ {
+ unsigned char * p;
+ char * q;
+ for (p = data, q = extern_ptr; len > 0; len--, p += 8, q += 8)
+ Reverse_64(q, p);
+ extern_ptr = q;
+ }
+#else
+ {
+ unsigned char * p;
+ char * q;
+ for (p = data, q = extern_ptr; len > 0; len--, p += 8, q += 8)
+ Permute_64(q, 0x01234567, p, ARCH_FLOAT_ENDIANNESS);
+ extern_ptr = q;
+ }
+#endif
+}
+
+/* Find where a code pointer comes from */
+
+CAMLexport struct code_fragment * caml_extern_find_code(char *addr)
+{
+ int i;
+ for (i = caml_code_fragments_table.size - 1; i >= 0; i--) {
+ struct code_fragment * cf = caml_code_fragments_table.contents[i];
+ if (! cf->digest_computed) {
+ caml_md5_block(cf->digest, cf->code_start, cf->code_end - cf->code_start);
+ cf->digest_computed = 1;
+ }
+ if (cf->code_start <= addr && addr < cf->code_end) return cf;
+ }
+ return NULL;
+}
diff --git a/test/monniaux/ocaml/byterun/fail.c b/test/monniaux/ocaml/byterun/fail.c
new file mode 100644
index 00000000..6b4dee5f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/fail.c
@@ -0,0 +1,204 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Raising exceptions from C. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/io.h"
+#include "caml/gc.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/printexc.h"
+#include "caml/signals.h"
+#include "caml/stacks.h"
+
+CAMLexport struct longjmp_buffer * caml_external_raise = NULL;
+value caml_exn_bucket;
+
+CAMLexport void caml_raise(value v)
+{
+ Unlock_exn();
+ caml_exn_bucket = v;
+ if (caml_external_raise == NULL) caml_fatal_uncaught_exception(v);
+ siglongjmp(caml_external_raise->buf, 1);
+}
+
+CAMLexport void caml_raise_constant(value tag)
+{
+ caml_raise(tag);
+}
+
+CAMLexport void caml_raise_with_arg(value tag, value arg)
+{
+ CAMLparam2 (tag, arg);
+ CAMLlocal1 (bucket);
+
+ bucket = caml_alloc_small (2, 0);
+ Field(bucket, 0) = tag;
+ Field(bucket, 1) = arg;
+ caml_raise(bucket);
+ CAMLnoreturn;
+}
+
+CAMLexport void caml_raise_with_args(value tag, int nargs, value args[])
+{
+ CAMLparam1 (tag);
+ CAMLxparamN (args, nargs);
+ value bucket;
+ int i;
+
+ CAMLassert(1 + nargs <= Max_young_wosize);
+ bucket = caml_alloc_small (1 + nargs, 0);
+ Field(bucket, 0) = tag;
+ for (i = 0; i < nargs; i++) Field(bucket, 1 + i) = args[i];
+ caml_raise(bucket);
+ CAMLnoreturn;
+}
+
+CAMLexport void caml_raise_with_string(value tag, char const *msg)
+{
+ CAMLparam1(tag);
+ value v_msg = caml_copy_string(msg);
+ caml_raise_with_arg(tag, v_msg);
+ CAMLnoreturn;
+}
+
+/* PR#5115: Built-in exceptions can be triggered by input_value
+ while reading the initial value of [caml_global_data].
+
+ We check against this issue here in byterun/fail.c instead of
+ byterun/intern.c. Having the check here means that these calls will
+ be slightly slower for all bytecode programs (not just the calls
+ coming from intern). Because intern.c is shared between byterun/
+ and asmrun/, putting checks there would slow do input_value for
+ natively-compiled programs that do not need these checks.
+*/
+static void check_global_data(char const *exception_name)
+{
+ if (caml_global_data == 0) {
+ fprintf(stderr, "Fatal error: exception %s\n", exception_name);
+ exit(2);
+ }
+}
+
+static void check_global_data_param(char const *exception_name, char const *msg)
+{
+ if (caml_global_data == 0) {
+ fprintf(stderr, "Fatal error: exception %s(\"%s\")\n", exception_name, msg);
+ exit(2);
+ }
+}
+
+static inline value caml_get_failwith_tag (char const *msg)
+{
+ check_global_data_param("Failure", msg);
+ return Field(caml_global_data, FAILURE_EXN);
+}
+
+CAMLexport void caml_failwith (char const *msg)
+{
+ caml_raise_with_string(caml_get_failwith_tag(msg), msg);
+}
+
+CAMLexport void caml_failwith_value (value msg)
+{
+ CAMLparam1(msg);
+ value tag = caml_get_failwith_tag(String_val(msg));
+ caml_raise_with_arg(tag, msg);
+ CAMLnoreturn;
+}
+
+static inline value caml_get_invalid_argument_tag (char const *msg)
+{
+ check_global_data_param("Invalid_argument", msg);
+ return Field(caml_global_data, INVALID_EXN);
+}
+
+CAMLexport void caml_invalid_argument (char const *msg)
+{
+ caml_raise_with_string(caml_get_invalid_argument_tag(msg), msg);
+}
+
+CAMLexport void caml_invalid_argument_value (value msg)
+{
+ CAMLparam1(msg);
+ value tag = caml_get_invalid_argument_tag(String_val(msg));
+ caml_raise_with_arg(tag, msg);
+ CAMLnoreturn;
+}
+
+CAMLexport void caml_array_bound_error(void)
+{
+ caml_invalid_argument("index out of bounds");
+}
+
+CAMLexport void caml_raise_out_of_memory(void)
+{
+ check_global_data("Out_of_memory");
+ caml_raise_constant(Field(caml_global_data, OUT_OF_MEMORY_EXN));
+}
+
+CAMLexport void caml_raise_stack_overflow(void)
+{
+ check_global_data("Stack_overflow");
+ caml_raise_constant(Field(caml_global_data, STACK_OVERFLOW_EXN));
+}
+
+CAMLexport void caml_raise_sys_error(value msg)
+{
+ check_global_data_param("Sys_error", String_val(msg));
+ caml_raise_with_arg(Field(caml_global_data, SYS_ERROR_EXN), msg);
+}
+
+CAMLexport void caml_raise_end_of_file(void)
+{
+ check_global_data("End_of_file");
+ caml_raise_constant(Field(caml_global_data, END_OF_FILE_EXN));
+}
+
+CAMLexport void caml_raise_zero_divide(void)
+{
+ check_global_data("Division_by_zero");
+ caml_raise_constant(Field(caml_global_data, ZERO_DIVIDE_EXN));
+}
+
+CAMLexport void caml_raise_not_found(void)
+{
+ check_global_data("Not_found");
+ caml_raise_constant(Field(caml_global_data, NOT_FOUND_EXN));
+}
+
+CAMLexport void caml_raise_sys_blocked_io(void)
+{
+ check_global_data("Sys_blocked_io");
+ caml_raise_constant(Field(caml_global_data, SYS_BLOCKED_IO));
+}
+
+int caml_is_special_exception(value exn) {
+ /* this function is only used in caml_format_exception to produce
+ a more readable textual representation of some exceptions. It is
+ better to fall back to the general, less readable representation
+ than to abort with a fatal error as above. */
+ if (caml_global_data == 0) return 0;
+ return exn == Field(caml_global_data, MATCH_FAILURE_EXN)
+ || exn == Field(caml_global_data, ASSERT_FAILURE_EXN)
+ || exn == Field(caml_global_data, UNDEFINED_RECURSIVE_MODULE_EXN);
+}
diff --git a/test/monniaux/ocaml/byterun/finalise.c b/test/monniaux/ocaml/byterun/finalise.c
new file mode 100644
index 00000000..d34913fb
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/finalise.c
@@ -0,0 +1,445 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Moscova, INRIA Rocquencourt */
+/* */
+/* Copyright 2000 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Handling of finalised values. */
+
+#include "caml/callback.h"
+#include "caml/compact.h"
+#include "caml/fail.h"
+#include "caml/finalise.h"
+#include "caml/minor_gc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/signals.h"
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+#include "caml/spacetime.h"
+#endif
+
+struct final {
+ value fun;
+ value val;
+ int offset;
+};
+
+struct finalisable {
+ struct final *table;
+ uintnat old;
+ uintnat young;
+ uintnat size;
+};
+/* [0..old) : finalisable set, the values are in the major heap
+ [old..young) : recent set, the values could be in the minor heap
+ [young..size) : free space
+
+ The element of the finalisable set are moved to the finalising set
+ below when the value are unreachable (for the first or last time).
+
+*/
+
+static struct finalisable finalisable_first = {NULL,0,0,0};
+static struct finalisable finalisable_last = {NULL,0,0,0};
+
+struct to_do {
+ struct to_do *next;
+ int size;
+ struct final item[1]; /* variable size */
+};
+
+static struct to_do *to_do_hd = NULL;
+static struct to_do *to_do_tl = NULL;
+/*
+ to_do_hd: head of the list of finalisation functions that can be run.
+ to_do_tl: tail of the list of finalisation functions that can be run.
+
+ It is the finalising set.
+*/
+
+
+/* [size] is a number of elements for the [to_do.item] array */
+static void alloc_to_do (int size)
+{
+ struct to_do *result = caml_stat_alloc_noexc (sizeof (struct to_do) +
+ size * sizeof (struct final));
+ if (result == NULL) caml_fatal_error ("out of memory");
+ result->next = NULL;
+ result->size = size;
+ if (to_do_tl == NULL){
+ to_do_hd = result;
+ to_do_tl = result;
+ }else{
+ CAMLassert (to_do_tl->next == NULL);
+ to_do_tl->next = result;
+ to_do_tl = result;
+ }
+}
+
+/* Find white finalisable values, move them to the finalising set, and
+ darken them (if darken_value is true).
+*/
+static void generic_final_update (struct finalisable * final, int darken_value)
+{
+ uintnat i, j, k;
+ uintnat todo_count = 0;
+
+ CAMLassert (final->old <= final->young);
+ for (i = 0; i < final->old; i++){
+ CAMLassert (Is_block (final->table[i].val));
+ CAMLassert (Is_in_heap (final->table[i].val));
+ if (Is_white_val (final->table[i].val)){
+ ++ todo_count;
+ }
+ }
+
+ /** invariant:
+ - 0 <= j <= i /\ 0 <= k <= i /\ 0 <= k <= todo_count
+ - i : index in final_table, before i all the values are black
+ (alive or in the minor heap) or the finalizer have been copied
+ in to_do_tl.
+ - j : index in final_table, before j all the values are black
+ (alive or in the minor heap), next available slot.
+ - k : index in to_do_tl, next available slot.
+ */
+ if (todo_count > 0){
+ alloc_to_do (todo_count);
+ j = k = 0;
+ for (i = 0; i < final->old; i++){
+ CAMLassert (Is_block (final->table[i].val));
+ CAMLassert (Is_in_heap (final->table[i].val));
+ CAMLassert (Tag_val (final->table[i].val) != Forward_tag);
+ if(Is_white_val (final->table[i].val)){
+ /** dead */
+ to_do_tl->item[k] = final->table[i];
+ if(!darken_value){
+ /* The value is not darken so the finalisation function
+ is called with unit not with the value */
+ to_do_tl->item[k].val = Val_unit;
+ to_do_tl->item[k].offset = 0;
+ };
+ k++;
+ }else{
+ /** alive */
+ final->table[j++] = final->table[i];
+ }
+ }
+ CAMLassert (i == final->old);
+ CAMLassert (k == todo_count);
+ final->old = j;
+ for(;i < final->young; i++){
+ final->table[j++] = final->table[i];
+ }
+ final->young = j;
+ to_do_tl->size = k;
+ if(darken_value){
+ for (i = 0; i < k; i++){
+ /* Note that item may already be dark due to multiple entries in
+ the final table. */
+ caml_darken (to_do_tl->item[i].val, NULL);
+ }
+ }
+ }
+}
+
+void caml_final_update_mark_phase (){
+ generic_final_update(&finalisable_first, /* darken_value */ 1);
+}
+
+void caml_final_update_clean_phase (){
+ generic_final_update(&finalisable_last, /* darken_value */ 0);
+}
+
+
+static int running_finalisation_function = 0;
+
+/* Call the finalisation functions for the finalising set.
+ Note that this function must be reentrant.
+*/
+void caml_final_do_calls (void)
+{
+ struct final f;
+ value res;
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ void* saved_spacetime_trie_node_ptr;
+#endif
+
+ if (running_finalisation_function) return;
+ if (to_do_hd != NULL){
+ if (caml_finalise_begin_hook != NULL) (*caml_finalise_begin_hook) ();
+ caml_gc_message (0x80, "Calling finalisation functions.\n");
+ while (1){
+ while (to_do_hd != NULL && to_do_hd->size == 0){
+ struct to_do *next_hd = to_do_hd->next;
+ caml_stat_free (to_do_hd);
+ to_do_hd = next_hd;
+ if (to_do_hd == NULL) to_do_tl = NULL;
+ }
+ if (to_do_hd == NULL) break;
+ CAMLassert (to_do_hd->size > 0);
+ -- to_do_hd->size;
+ f = to_do_hd->item[to_do_hd->size];
+ running_finalisation_function = 1;
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ /* We record the finaliser's execution separately.
+ (The code of [caml_callback_exn] will do the hard work of finding
+ the correct place in the trie.) */
+ saved_spacetime_trie_node_ptr = caml_spacetime_trie_node_ptr;
+ caml_spacetime_trie_node_ptr = caml_spacetime_finaliser_trie_root;
+#endif
+ res = caml_callback_exn (f.fun, f.val + f.offset);
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ caml_spacetime_trie_node_ptr = saved_spacetime_trie_node_ptr;
+#endif
+ running_finalisation_function = 0;
+ if (Is_exception_result (res)) caml_raise (Extract_exception (res));
+ }
+ caml_gc_message (0x80, "Done calling finalisation functions.\n");
+ if (caml_finalise_end_hook != NULL) (*caml_finalise_end_hook) ();
+ }
+}
+
+/* Call a scanning_action [f] on [x]. */
+#define Call_action(f,x) (*(f)) ((x), &(x))
+
+/* Call [*f] on the closures of the finalisable set and
+ the closures and values of the finalising set.
+ This is called by the major GC [caml_darken_all_roots]
+ and by the compactor through [caml_do_roots]
+*/
+void caml_final_do_roots (scanning_action f)
+{
+ uintnat i;
+ struct to_do *todo;
+
+ CAMLassert (finalisable_first.old <= finalisable_first.young);
+ for (i = 0; i < finalisable_first.young; i++){
+ Call_action (f, finalisable_first.table[i].fun);
+ };
+
+ CAMLassert (finalisable_last.old <= finalisable_last.young);
+ for (i = 0; i < finalisable_last.young; i++){
+ Call_action (f, finalisable_last.table[i].fun);
+ };
+
+ for (todo = to_do_hd; todo != NULL; todo = todo->next){
+ for (i = 0; i < todo->size; i++){
+ Call_action (f, todo->item[i].fun);
+ Call_action (f, todo->item[i].val);
+ }
+ }
+}
+
+/* Call caml_invert_root on the values of the finalisable set. This is called
+ directly by the compactor.
+*/
+void caml_final_invert_finalisable_values ()
+{
+ uintnat i;
+
+ CAMLassert (finalisable_first.old <= finalisable_first.young);
+ for (i = 0; i < finalisable_first.young; i++){
+ caml_invert_root(finalisable_first.table[i].val,
+ &finalisable_first.table[i].val);
+ };
+
+ CAMLassert (finalisable_last.old <= finalisable_last.young);
+ for (i = 0; i < finalisable_last.young; i++){
+ caml_invert_root(finalisable_last.table[i].val,
+ &finalisable_last.table[i].val);
+ };
+}
+
+/* Call [caml_oldify_one] on the closures and values of the recent set.
+ This is called by the minor GC through [caml_oldify_local_roots].
+*/
+void caml_final_oldify_young_roots ()
+{
+ uintnat i;
+
+ CAMLassert (finalisable_first.old <= finalisable_first.young);
+ for (i = finalisable_first.old; i < finalisable_first.young; i++){
+ caml_oldify_one(finalisable_first.table[i].fun,
+ &finalisable_first.table[i].fun);
+ caml_oldify_one(finalisable_first.table[i].val,
+ &finalisable_first.table[i].val);
+ }
+
+ CAMLassert (finalisable_last.old <= finalisable_last.young);
+ for (i = finalisable_last.old; i < finalisable_last.young; i++){
+ caml_oldify_one(finalisable_last.table[i].fun,
+ &finalisable_last.table[i].fun);
+ }
+
+}
+
+static void generic_final_minor_update (struct finalisable * final)
+{
+ uintnat i, j, k;
+ uintnat todo_count = 0;
+
+ CAMLassert (final->old <= final->young);
+ for (i = final->old; i < final->young; i++){
+ CAMLassert (Is_block (final->table[i].val));
+ CAMLassert (Is_in_heap_or_young (final->table[i].val));
+ if (Is_young(final->table[i].val) && Hd_val(final->table[i].val) != 0){
+ ++ todo_count;
+ }
+ }
+
+ /** invariant:
+ - final->old <= j <= i /\ final->old <= k <= i /\ 0 <= k <= todo_count
+ - i : index in final_table, before i all the values are alive
+ or the finalizer have been copied in to_do_tl.
+ - j : index in final_table, before j all the values are alive,
+ next available slot.
+ - k : index in to_do_tl, next available slot.
+ */
+ if (todo_count > 0){
+ alloc_to_do (todo_count);
+ k = 0;
+ j = final->old;
+ for (i = final->old; i < final->young; i++){
+ CAMLassert (Is_block (final->table[i].val));
+ CAMLassert (Is_in_heap_or_young (final->table[i].val));
+ CAMLassert (Tag_val (final->table[i].val) != Forward_tag);
+ if(Is_young(final->table[i].val) && Hd_val(final->table[i].val) != 0){
+ /** dead */
+ to_do_tl->item[k] = final->table[i];
+ /* The finalisation function is called with unit not with the value */
+ to_do_tl->item[k].val = Val_unit;
+ to_do_tl->item[k].offset = 0;
+ k++;
+ }else{
+ /** alive */
+ final->table[j++] = final->table[i];
+ }
+ }
+ CAMLassert (i == final->young);
+ CAMLassert (k == todo_count);
+ final->young = j;
+ to_do_tl->size = todo_count;
+ }
+
+ /** update the minor value to the copied major value */
+ for (i = final->old; i < final->young; i++){
+ CAMLassert (Is_block (final->table[i].val));
+ CAMLassert (Is_in_heap_or_young (final->table[i].val));
+ if (Is_young(final->table[i].val)) {
+ CAMLassert (Hd_val(final->table[i].val) == 0);
+ final->table[i].val = Field(final->table[i].val,0);
+ }
+ }
+
+ /** check invariant */
+ CAMLassert (final->old <= final->young);
+ for (i = 0; i < final->young; i++){
+ CAMLassert( Is_in_heap(final->table[i].val) );
+ };
+
+}
+
+/* At the end of minor collection update the finalise_last roots in
+ minor heap when moved to major heap or moved them to the finalising
+ set when dead.
+*/
+void caml_final_update_minor_roots ()
+{
+ generic_final_minor_update(&finalisable_last);
+}
+
+/* Empty the recent set into the finalisable set.
+ This is called at the end of each minor collection.
+ The minor heap must be empty when this is called.
+*/
+void caml_final_empty_young (void)
+{
+ finalisable_first.old = finalisable_first.young;
+ finalisable_last.old = finalisable_last.young;
+}
+
+/* Put (f,v) in the recent set. */
+static void generic_final_register (struct finalisable *final, value f, value v)
+{
+ if (!Is_block (v)
+ || !Is_in_heap_or_young(v)
+ || Tag_val (v) == Lazy_tag
+#ifdef FLAT_FLOAT_ARRAY
+ || Tag_val (v) == Double_tag
+#endif
+ || Tag_val (v) == Forward_tag) {
+ caml_invalid_argument ("Gc.finalise");
+ }
+ CAMLassert (final->old <= final->young);
+
+ if (final->young >= final->size){
+ if (final->table == NULL){
+ uintnat new_size = 30;
+ final->table = caml_stat_alloc (new_size * sizeof (struct final));
+ CAMLassert (final->old == 0);
+ CAMLassert (final->young == 0);
+ final->size = new_size;
+ }else{
+ uintnat new_size = final->size * 2;
+ final->table = caml_stat_resize (final->table,
+ new_size * sizeof (struct final));
+ final->size = new_size;
+ }
+ }
+ CAMLassert (final->young < final->size);
+ final->table[final->young].fun = f;
+ if (Tag_val (v) == Infix_tag){
+ final->table[final->young].offset = Infix_offset_val (v);
+ final->table[final->young].val = v - Infix_offset_val (v);
+ }else{
+ final->table[final->young].offset = 0;
+ final->table[final->young].val = v;
+ }
+ ++ final->young;
+
+}
+
+CAMLprim value caml_final_register (value f, value v){
+ generic_final_register(&finalisable_first, f, v);
+ return Val_unit;
+}
+
+CAMLprim value caml_final_register_called_without_value (value f, value v){
+ generic_final_register(&finalisable_last, f, v);
+ return Val_unit;
+}
+
+
+CAMLprim value caml_final_release (value unit)
+{
+ running_finalisation_function = 0;
+ return Val_unit;
+}
+
+static void gen_final_invariant_check(struct finalisable *final){
+ uintnat i;
+
+ CAMLassert (final->old <= final->young);
+ for (i = 0; i < final->old; i++){
+ CAMLassert( Is_in_heap(final->table[i].val) );
+ };
+ for (i = final->old; i < final->young; i++){
+ CAMLassert( Is_in_heap_or_young(final->table[i].val) );
+ };
+}
+
+void caml_final_invariant_check(void){
+ gen_final_invariant_check(&finalisable_first);
+ gen_final_invariant_check(&finalisable_last);
+}
diff --git a/test/monniaux/ocaml/byterun/fix_code.c b/test/monniaux/ocaml/byterun/fix_code.c
new file mode 100644
index 00000000..ec2f08cc
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/fix_code.c
@@ -0,0 +1,195 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Handling of blocks of bytecode (endianness switch, threading). */
+
+#include "caml/config.h"
+
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#else
+#include <io.h>
+#endif
+
+#include "caml/debugger.h"
+#include "caml/fix_code.h"
+#include "caml/instruct.h"
+#include "caml/intext.h"
+#include "caml/md5.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/reverse.h"
+
+code_t caml_start_code;
+asize_t caml_code_size;
+unsigned char * caml_saved_code;
+struct ext_table caml_code_fragments_table;
+
+/* Read the main bytecode block from a file */
+
+void caml_init_code_fragments(void) {
+ struct code_fragment * cf;
+ /* Register the code in the table of code fragments */
+ cf = caml_stat_alloc(sizeof(struct code_fragment));
+ cf->code_start = (char *) caml_start_code;
+ cf->code_end = (char *) caml_start_code + caml_code_size;
+ caml_md5_block(cf->digest, caml_start_code, caml_code_size);
+ cf->digest_computed = 1;
+ caml_ext_table_init(&caml_code_fragments_table, 8);
+ caml_ext_table_add(&caml_code_fragments_table, cf);
+}
+
+void caml_load_code(int fd, asize_t len)
+{
+ int i;
+
+ caml_code_size = len;
+ caml_start_code = (code_t) caml_stat_alloc(caml_code_size);
+ if (read(fd, (char *) caml_start_code, caml_code_size) != caml_code_size)
+ caml_fatal_error("Fatal error: truncated bytecode file.\n");
+ caml_init_code_fragments();
+ /* Prepare the code for execution */
+#ifdef ARCH_BIG_ENDIAN
+ caml_fixup_endianness(caml_start_code, caml_code_size);
+#endif
+ if (caml_debugger_in_use) {
+ len /= sizeof(opcode_t);
+ caml_saved_code = (unsigned char *) caml_stat_alloc(len);
+ for (i = 0; i < len; i++) caml_saved_code[i] = caml_start_code[i];
+ }
+#ifdef THREADED_CODE
+ /* Better to thread now than at the beginning of [caml_interprete],
+ since the debugger interface needs to perform SET_EVENT requests
+ on the code. */
+ caml_thread_code(caml_start_code, caml_code_size);
+#endif
+}
+
+/* This code is needed only if the processor is big endian */
+
+#ifdef ARCH_BIG_ENDIAN
+
+void caml_fixup_endianness(code_t code, asize_t len)
+{
+ code_t p;
+ len /= sizeof(opcode_t);
+ for (p = code; p < code + len; p++) {
+ Reverse_32(p, p);
+ }
+}
+
+#endif
+
+/* This code is needed only if we're using threaded code */
+
+#ifdef THREADED_CODE
+
+char ** caml_instr_table;
+char * caml_instr_base;
+
+static int* opcode_nargs = NULL;
+int* caml_init_opcode_nargs(void)
+{
+ if( opcode_nargs == NULL ){
+ int* l = (int*)caml_stat_alloc(sizeof(int) * FIRST_UNIMPLEMENTED_OP);
+ int i;
+
+ for (i = 0; i < FIRST_UNIMPLEMENTED_OP; i++) {
+ l [i] = 0;
+ }
+ /* Instructions with one operand */
+ l[PUSHACC] = l[ACC] = l[POP] = l[ASSIGN] =
+ l[PUSHENVACC] = l[ENVACC] = l[PUSH_RETADDR] = l[APPLY] =
+ l[APPTERM1] = l[APPTERM2] = l[APPTERM3] = l[RETURN] =
+ l[GRAB] = l[PUSHGETGLOBAL] = l[GETGLOBAL] = l[SETGLOBAL] =
+ l[PUSHATOM] = l[ATOM] = l[MAKEBLOCK1] = l[MAKEBLOCK2] =
+ l[MAKEBLOCK3] = l[MAKEFLOATBLOCK] = l[GETFIELD] =
+ l[GETFLOATFIELD] = l[SETFIELD] = l[SETFLOATFIELD] =
+ l[BRANCH] = l[BRANCHIF] = l[BRANCHIFNOT] = l[PUSHTRAP] =
+ l[C_CALL1] = l[C_CALL2] = l[C_CALL3] = l[C_CALL4] = l[C_CALL5] =
+ l[CONSTINT] = l[PUSHCONSTINT] = l[OFFSETINT] =
+ l[OFFSETREF] = l[OFFSETCLOSURE] = l[PUSHOFFSETCLOSURE] = 1;
+
+ /* Instructions with two operands */
+ l[APPTERM] = l[CLOSURE] = l[PUSHGETGLOBALFIELD] =
+ l[GETGLOBALFIELD] = l[MAKEBLOCK] = l[C_CALLN] =
+ l[BEQ] = l[BNEQ] = l[BLTINT] = l[BLEINT] = l[BGTINT] = l[BGEINT] =
+ l[BULTINT] = l[BUGEINT] = l[GETPUBMET] = 2;
+
+ opcode_nargs = l;
+ }
+ return opcode_nargs;
+}
+
+void caml_thread_code (code_t code, asize_t len)
+{
+ code_t p;
+ int* l = caml_init_opcode_nargs();
+ len /= sizeof(opcode_t);
+ for (p = code; p < code + len; /*nothing*/) {
+ opcode_t instr = *p;
+ if (instr < 0 || instr >= FIRST_UNIMPLEMENTED_OP){
+ /* FIXME -- should Assert(false) ?
+ caml_fatal_error_arg ("Fatal error in fix_code: bad opcode (%lx)\n",
+ (char *)(long)instr);
+ */
+ instr = STOP;
+ }
+ *p++ = (opcode_t)(caml_instr_table[instr] - caml_instr_base);
+ if (instr == SWITCH) {
+ uint32_t sizes = *p++;
+ uint32_t const_size = sizes & 0xFFFF;
+ uint32_t block_size = sizes >> 16;
+ p += const_size + block_size;
+ } else if (instr == CLOSUREREC) {
+ uint32_t nfuncs = *p++;
+ p++; /* skip nvars */
+ p += nfuncs;
+ } else {
+ p += l[instr];
+ }
+ }
+ CAMLassert(p == code + len);
+}
+
+#else
+
+int* caml_init_opcode_nargs()
+{
+ return NULL;
+}
+
+#endif /* THREADED_CODE */
+
+void caml_set_instruction(code_t pos, opcode_t instr)
+{
+#ifdef THREADED_CODE
+ *pos = (opcode_t)(caml_instr_table[instr] - caml_instr_base);
+#else
+ *pos = instr;
+#endif
+}
+
+int caml_is_instruction(opcode_t instr1, opcode_t instr2)
+{
+#ifdef THREADED_CODE
+ return instr1 == (opcode_t)(caml_instr_table[instr2] - caml_instr_base);
+#else
+ return instr1 == instr2;
+#endif
+}
diff --git a/test/monniaux/ocaml/byterun/floats.c b/test/monniaux/ocaml/byterun/floats.c
new file mode 100644
index 00000000..4d2494cf
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/floats.c
@@ -0,0 +1,720 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* The interface of this file is in "caml/mlvalues.h" and "caml/alloc.h" */
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <float.h>
+#include <limits.h>
+
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/misc.h"
+#include "caml/reverse.h"
+#include "caml/stacks.h"
+
+#ifdef _MSC_VER
+#include <float.h>
+#ifndef isnan
+#define isnan _isnan
+#endif
+#ifndef isfinite
+#define isfinite _finite
+#endif
+#endif
+
+#ifdef ARCH_ALIGN_DOUBLE
+
+CAMLexport double caml_Double_val(value val)
+{
+ union { value v[2]; double d; } buffer;
+
+ CAMLassert(sizeof(double) == 2 * sizeof(value));
+ buffer.v[0] = Field(val, 0);
+ buffer.v[1] = Field(val, 1);
+ return buffer.d;
+}
+
+CAMLexport void caml_Store_double_val(value val, double dbl)
+{
+ union { value v[2]; double d; } buffer;
+
+ CAMLassert(sizeof(double) == 2 * sizeof(value));
+ buffer.d = dbl;
+ Field(val, 0) = buffer.v[0];
+ Field(val, 1) = buffer.v[1];
+}
+
+#endif
+
+CAMLexport value caml_copy_double(double d)
+{
+ value res;
+
+#define Setup_for_gc
+#define Restore_after_gc
+ Alloc_small(res, Double_wosize, Double_tag);
+#undef Setup_for_gc
+#undef Restore_after_gc
+ Store_double_val(res, d);
+ return res;
+}
+
+#ifndef FLAT_FLOAT_ARRAY
+CAMLexport void caml_Store_double_array_field(value val, mlsize_t i, double dbl)
+{
+ CAMLparam1 (val);
+ value d = caml_copy_double (dbl);
+
+ CAMLassert (Tag_val (val) != Double_array_tag);
+ caml_modify (&Field(val, i), d);
+ CAMLreturn0;
+}
+#endif /* ! FLAT_FLOAT_ARRAY */
+
+CAMLprim value caml_format_float(value fmt, value arg)
+{
+ value res;
+ double d = Double_val(arg);
+
+#ifdef HAS_BROKEN_PRINTF
+ if (isfinite(d)) {
+#endif
+ res = caml_alloc_sprintf(String_val(fmt), d);
+#ifdef HAS_BROKEN_PRINTF
+ } else {
+ if (isnan(d)) {
+ res = caml_copy_string("nan");
+ } else {
+ if (d > 0)
+ res = caml_copy_string("inf");
+ else
+ res = caml_copy_string("-inf");
+ }
+ }
+#endif
+ return res;
+}
+
+CAMLprim value caml_hexstring_of_float(value arg, value vprec, value vstyle)
+{
+ union { uint64_t i; double d; } u;
+ int sign, exp;
+ uint64_t m;
+ char buffer[64];
+ char * buf, * p;
+ intnat prec;
+ int d;
+ value res;
+
+ /* Allocate output buffer */
+ prec = Long_val(vprec);
+ /* 12 chars for sign, 0x, decimal point, exponent */
+ buf = (prec + 12 <= 64 ? buffer : caml_stat_alloc(prec + 12));
+ /* Extract sign, mantissa, and exponent */
+ u.d = Double_val(arg);
+ sign = u.i >> 63;
+ exp = (u.i >> 52) & 0x7FF;
+ m = u.i & (((uint64_t) 1 << 52) - 1);
+ /* Put sign */
+ p = buf;
+ if (sign) {
+ *p++ = '-';
+ } else {
+ switch (Int_val(vstyle)) {
+ case '+': *p++ = '+'; break;
+ case ' ': *p++ = ' '; break;
+ }
+ }
+ /* Treat special cases */
+ if (exp == 0x7FF) {
+ char * txt;
+ if (m == 0) txt = "infinity"; else txt = "nan";
+ memcpy(p, txt, strlen(txt));
+ p[strlen(txt)] = 0;
+ res = caml_copy_string(buf);
+ } else {
+ /* Output "0x" prefix */
+ *p++ = '0'; *p++ = 'x';
+ /* Normalize exponent and mantissa */
+ if (exp == 0) {
+ if (m != 0) exp = -1022; /* denormal */
+ } else {
+ exp = exp - 1023;
+ m = m | ((uint64_t) 1 << 52);
+ }
+ /* If a precision is given, and is small, round mantissa accordingly */
+ prec = Long_val(vprec);
+ if (prec >= 0 && prec < 13) {
+ int i = 52 - prec * 4;
+ uint64_t unit = (uint64_t) 1 << i;
+ uint64_t half = unit >> 1;
+ uint64_t mask = unit - 1;
+ uint64_t frac = m & mask;
+ m = m & ~mask;
+ /* Round to nearest, ties to even */
+ if (frac > half || (frac == half && (m & unit) != 0)) {
+ m += unit;
+ }
+ }
+ /* Leading digit */
+ d = m >> 52;
+ *p++ = (d < 10 ? d + '0' : d - 10 + 'a');
+ m = (m << 4) & (((uint64_t) 1 << 56) - 1);
+ /* Fractional digits. If a precision is given, print that number of
+ digits. Otherwise, print as many digits as needed to represent
+ the mantissa exactly. */
+ if (prec >= 0 ? prec > 0 : m != 0) {
+ *p++ = '.';
+ while (prec >= 0 ? prec > 0 : m != 0) {
+ d = m >> 52;
+ *p++ = (d < 10 ? d + '0' : d - 10 + 'a');
+ m = (m << 4) & (((uint64_t) 1 << 56) - 1);
+ prec--;
+ }
+ }
+ *p = 0;
+ /* Add exponent */
+ res = caml_alloc_sprintf("%sp%+d", buf, exp);
+ }
+ if (buf != buffer) caml_stat_free(buf);
+ return res;
+}
+
+static int caml_float_of_hex(const char * s, double * res)
+{
+ int64_t m = 0; /* the mantissa - top 60 bits at most */
+ int n_bits = 0; /* total number of bits read */
+ int m_bits = 0; /* number of bits in mantissa */
+ int x_bits = 0; /* number of bits after mantissa */
+ int dec_point = -1; /* bit count corresponding to decimal point */
+ /* -1 if no decimal point seen */
+ int exp = 0; /* exponent */
+ char * p; /* for converting the exponent */
+ double f;
+
+ while (*s != 0) {
+ char c = *s++;
+ switch (c) {
+ case '_':
+ break;
+ case '.':
+ if (dec_point >= 0) return -1; /* multiple decimal points */
+ dec_point = n_bits;
+ break;
+ case 'p': case 'P': {
+ long e;
+ if (*s == 0) return -1; /* nothing after exponent mark */
+ e = strtol(s, &p, 10);
+ if (*p != 0) return -1; /* ill-formed exponent */
+ /* Handle exponents larger than int by returning 0/∞ directly.
+ Mind that INT_MIN/INT_MAX are included in the test so as to capture
+ the overflow case of strtol on Win64 — long and int have the same
+ size there. */
+ if (e <= INT_MIN) {
+ *res = 0.;
+ return 0;
+ }
+ else if (e >= INT_MAX) {
+ *res = m == 0 ? 0. : HUGE_VAL;
+ return 0;
+ }
+ /* regular exponent value */
+ exp = e;
+ s = p; /* stop at next loop iteration */
+ break;
+ }
+ default: { /* Nonzero digit */
+ int d;
+ if (c >= '0' && c <= '9') d = c - '0';
+ else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
+ else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
+ else return -1; /* bad digit */
+ n_bits += 4;
+ if (d == 0 && m == 0) break; /* leading zeros are skipped */
+ if (m_bits < 60) {
+ /* There is still room in m. Add this digit to the mantissa. */
+ m = (m << 4) + d;
+ m_bits += 4;
+ } else {
+ /* We've already collected 60 significant bits in m.
+ Now all we care about is whether there is a nonzero bit
+ after. In this case, round m to odd so that the later
+ rounding of m to FP produces the correct result. */
+ if (d != 0) m |= 1; /* round to odd */
+ x_bits += 4;
+ }
+ break;
+ }
+ }
+ }
+ if (n_bits == 0) return -1;
+ /* Convert mantissa to FP. We use a signed conversion because we can
+ (m has 60 bits at most) and because it is faster
+ on several architectures. */
+ f = (double) (int64_t) m;
+ /* Adjust exponent to take decimal point and extra digits into account */
+ {
+ int adj = x_bits;
+ if (dec_point >= 0) adj = adj + (dec_point - n_bits);
+ /* saturated addition exp + adj */
+ if (adj > 0 && exp > INT_MAX - adj)
+ exp = INT_MAX;
+ else if (adj < 0 && exp < INT_MIN - adj)
+ exp = INT_MIN;
+ else
+ exp = exp + adj;
+ }
+ /* Apply exponent if needed */
+ if (exp != 0) f = ldexp(f, exp);
+ /* Done! */
+ *res = f;
+ return 0;
+}
+
+CAMLprim value caml_float_of_string(value vs)
+{
+ char parse_buffer[64];
+ char * buf, * dst, * end;
+ const char *src;
+ mlsize_t len;
+ int sign;
+ double d;
+
+ /* Check for hexadecimal FP constant */
+ src = String_val(vs);
+ sign = 1;
+ if (*src == '-') { sign = -1; src++; }
+ else if (*src == '+') { src++; };
+ if (src[0] == '0' && (src[1] == 'x' || src[1] == 'X')) {
+ if (caml_float_of_hex(src + 2, &d) == -1)
+ caml_failwith("float_of_string");
+ return caml_copy_double(sign < 0 ? -d : d);
+ }
+ /* Remove '_' characters before calling strtod () */
+ len = caml_string_length(vs);
+ buf = len < sizeof(parse_buffer) ? parse_buffer : caml_stat_alloc(len + 1);
+ src = String_val(vs);
+ dst = buf;
+ while (len--) {
+ char c = *src++;
+ if (c != '_') *dst++ = c;
+ }
+ *dst = 0;
+ if (dst == buf) goto error;
+ /* Convert using strtod */
+ d = strtod((const char *) buf, &end);
+ if (end != dst) goto error;
+ if (buf != parse_buffer) caml_stat_free(buf);
+ return caml_copy_double(d);
+ error:
+ if (buf != parse_buffer) caml_stat_free(buf);
+ caml_failwith("float_of_string");
+ return Val_unit; /* not reached */
+}
+
+CAMLprim value caml_int_of_float(value f)
+{
+ return Val_long((intnat) Double_val(f));
+}
+
+CAMLprim value caml_float_of_int(value n)
+{
+ return caml_copy_double((double) Long_val(n));
+}
+
+CAMLprim value caml_neg_float(value f)
+{
+ return caml_copy_double(- Double_val(f));
+}
+
+CAMLprim value caml_abs_float(value f)
+{
+ return caml_copy_double(fabs(Double_val(f)));
+}
+
+CAMLprim value caml_add_float(value f, value g)
+{
+ return caml_copy_double(Double_val(f) + Double_val(g));
+}
+
+CAMLprim value caml_sub_float(value f, value g)
+{
+ return caml_copy_double(Double_val(f) - Double_val(g));
+}
+
+CAMLprim value caml_mul_float(value f, value g)
+{
+ return caml_copy_double(Double_val(f) * Double_val(g));
+}
+
+CAMLprim value caml_div_float(value f, value g)
+{
+ return caml_copy_double(Double_val(f) / Double_val(g));
+}
+
+CAMLprim value caml_exp_float(value f)
+{
+ return caml_copy_double(exp(Double_val(f)));
+}
+
+CAMLprim value caml_floor_float(value f)
+{
+ return caml_copy_double(floor(Double_val(f)));
+}
+
+CAMLprim value caml_fmod_float(value f1, value f2)
+{
+ return caml_copy_double(fmod(Double_val(f1), Double_val(f2)));
+}
+
+CAMLprim value caml_frexp_float(value f)
+{
+ CAMLparam1 (f);
+ CAMLlocal2 (res, mantissa);
+ int exponent;
+
+ mantissa = caml_copy_double(frexp (Double_val(f), &exponent));
+ res = caml_alloc_tuple(2);
+ Field(res, 0) = mantissa;
+ Field(res, 1) = Val_int(exponent);
+ CAMLreturn (res);
+}
+
+// Seems dumb but intnat could not correspond to int type.
+double caml_ldexp_float_unboxed(double f, intnat i)
+{
+ return ldexp(f, i);
+}
+
+
+CAMLprim value caml_ldexp_float(value f, value i)
+{
+ return caml_copy_double(ldexp(Double_val(f), Int_val(i)));
+}
+
+CAMLprim value caml_log_float(value f)
+{
+ return caml_copy_double(log(Double_val(f)));
+}
+
+CAMLprim value caml_log10_float(value f)
+{
+ return caml_copy_double(log10(Double_val(f)));
+}
+
+CAMLprim value caml_modf_float(value f)
+{
+ double frem;
+
+ CAMLparam1 (f);
+ CAMLlocal3 (res, quo, rem);
+
+ quo = caml_copy_double(modf (Double_val(f), &frem));
+ rem = caml_copy_double(frem);
+ res = caml_alloc_tuple(2);
+ Field(res, 0) = quo;
+ Field(res, 1) = rem;
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_sqrt_float(value f)
+{
+ return caml_copy_double(sqrt(Double_val(f)));
+}
+
+CAMLprim value caml_power_float(value f, value g)
+{
+ return caml_copy_double(pow(Double_val(f), Double_val(g)));
+}
+
+CAMLprim value caml_sin_float(value f)
+{
+ return caml_copy_double(sin(Double_val(f)));
+}
+
+CAMLprim value caml_sinh_float(value f)
+{
+ return caml_copy_double(sinh(Double_val(f)));
+}
+
+CAMLprim value caml_cos_float(value f)
+{
+ return caml_copy_double(cos(Double_val(f)));
+}
+
+CAMLprim value caml_cosh_float(value f)
+{
+ return caml_copy_double(cosh(Double_val(f)));
+}
+
+CAMLprim value caml_tan_float(value f)
+{
+ return caml_copy_double(tan(Double_val(f)));
+}
+
+CAMLprim value caml_tanh_float(value f)
+{
+ return caml_copy_double(tanh(Double_val(f)));
+}
+
+CAMLprim value caml_asin_float(value f)
+{
+ return caml_copy_double(asin(Double_val(f)));
+}
+
+CAMLprim value caml_acos_float(value f)
+{
+ return caml_copy_double(acos(Double_val(f)));
+}
+
+CAMLprim value caml_atan_float(value f)
+{
+ return caml_copy_double(atan(Double_val(f)));
+}
+
+CAMLprim value caml_atan2_float(value f, value g)
+{
+ return caml_copy_double(atan2(Double_val(f), Double_val(g)));
+}
+
+CAMLprim value caml_ceil_float(value f)
+{
+ return caml_copy_double(ceil(Double_val(f)));
+}
+
+CAMLexport double caml_hypot(double x, double y)
+{
+#ifdef HAS_C99_FLOAT_OPS
+ return hypot(x, y);
+#else
+ double tmp, ratio;
+ x = fabs(x); y = fabs(y);
+ if (x != x) /* x is NaN */
+ return y > DBL_MAX ? y : x; /* PR#6321 */
+ if (y != y) /* y is NaN */
+ return x > DBL_MAX ? x : y; /* PR#6321 */
+ if (x < y) { tmp = x; x = y; y = tmp; }
+ if (x == 0.0) return 0.0;
+ ratio = y / x;
+ return x * sqrt(1.0 + ratio * ratio);
+#endif
+}
+
+CAMLprim value caml_hypot_float(value f, value g)
+{
+ return caml_copy_double(caml_hypot(Double_val(f), Double_val(g)));
+}
+
+/* These emulations of expm1() and log1p() are due to William Kahan.
+ See http://www.plunk.org/~hatch/rightway.php */
+CAMLexport double caml_expm1(double x)
+{
+#ifdef HAS_C99_FLOAT_OPS
+ return expm1(x);
+#else
+ double u = exp(x);
+ if (u == 1.)
+ return x;
+ if (u - 1. == -1.)
+ return -1.;
+ return (u - 1.) * x / log(u);
+#endif
+}
+
+CAMLexport double caml_log1p(double x)
+{
+#ifdef HAS_C99_FLOAT_OPS
+ return log1p(x);
+#else
+ double u = 1. + x;
+ if (u == 1.)
+ return x;
+ else
+ return log(u) * x / (u - 1.);
+#endif
+}
+
+CAMLprim value caml_expm1_float(value f)
+{
+ return caml_copy_double(caml_expm1(Double_val(f)));
+}
+
+CAMLprim value caml_log1p_float(value f)
+{
+ return caml_copy_double(caml_log1p(Double_val(f)));
+}
+
+union double_as_two_int32 {
+ double d;
+#if defined(ARCH_BIG_ENDIAN) || (defined(__arm__) && !defined(__ARM_EABI__))
+ struct { uint32_t h; uint32_t l; } i;
+#else
+ struct { uint32_t l; uint32_t h; } i;
+#endif
+};
+
+CAMLexport double caml_copysign(double x, double y)
+{
+#ifdef HAS_C99_FLOAT_OPS
+ return copysign(x, y);
+#else
+ union double_as_two_int32 ux, uy;
+ ux.d = x;
+ uy.d = y;
+ ux.i.h &= 0x7FFFFFFFU;
+ ux.i.h |= (uy.i.h & 0x80000000U);
+ return ux.d;
+#endif
+}
+
+CAMLprim value caml_copysign_float(value f, value g)
+{
+ return caml_copy_double(caml_copysign(Double_val(f), Double_val(g)));
+}
+
+#ifdef LACKS_SANE_NAN
+
+CAMLprim value caml_neq_float(value vf, value vg)
+{
+ double f = Double_val(vf);
+ double g = Double_val(vg);
+ return Val_bool(isnan(f) || isnan(g) || f != g);
+}
+
+#define DEFINE_NAN_CMP(op) (value vf, value vg) \
+{ \
+ double f = Double_val(vf); \
+ double g = Double_val(vg); \
+ return Val_bool(!isnan(f) && !isnan(g) && f op g); \
+}
+
+intnat caml_float_compare_unboxed(double f, double g)
+{
+ /* Insane => nan == everything && nan < everything && nan > everything */
+ if (isnan(f) && isnan(g)) return 0;
+ if (!isnan(g) && f < g) return -1;
+ if (f != g) return 1;
+ return 0;
+}
+
+#else
+
+CAMLprim value caml_neq_float(value f, value g)
+{
+ return Val_bool(Double_val(f) != Double_val(g));
+}
+
+#define DEFINE_NAN_CMP(op) (value f, value g) \
+{ \
+ return Val_bool(Double_val(f) op Double_val(g)); \
+}
+
+intnat caml_float_compare_unboxed(double f, double g)
+{
+ /* If one or both of f and g is NaN, order according to the convention
+ NaN = NaN and NaN < x for all other floats x. */
+ /* This branchless implementation is from GPR#164.
+ Note that [f == f] if and only if f is not NaN. */
+ return (f > g) - (f < g) + (f == f) - (g == g);
+}
+
+#endif
+
+CAMLprim value caml_eq_float DEFINE_NAN_CMP(==)
+CAMLprim value caml_le_float DEFINE_NAN_CMP(<=)
+CAMLprim value caml_lt_float DEFINE_NAN_CMP(<)
+CAMLprim value caml_ge_float DEFINE_NAN_CMP(>=)
+CAMLprim value caml_gt_float DEFINE_NAN_CMP(>)
+
+CAMLprim value caml_float_compare(value vf, value vg)
+{
+ return Val_int(caml_float_compare_unboxed(Double_val(vf),Double_val(vg)));
+}
+
+enum { FP_normal, FP_subnormal, FP_zero, FP_infinite, FP_nan };
+
+value caml_classify_float_unboxed(double vd)
+{
+#ifdef ARCH_SIXTYFOUR
+ union { double d; uint64_t i; } u;
+ uint64_t n;
+ uint32_t e;
+
+ u.d = vd;
+ n = u.i << 1; /* shift sign bit off */
+ if (n == 0) return Val_int(FP_zero);
+ e = n >> 53; /* extract exponent */
+ if (e == 0) return Val_int(FP_subnormal);
+ if (e == 0x7FF) {
+ if (n << 11 == 0) /* shift exponent off */
+ return Val_int(FP_infinite);
+ else
+ return Val_int(FP_nan);
+ }
+ return Val_int(FP_normal);
+#else
+ union double_as_two_int32 u;
+ uint32_t h, l;
+
+ u.d = vd;
+ h = u.i.h; l = u.i.l;
+ l = l | (h & 0xFFFFF);
+ h = h & 0x7FF00000;
+ if ((h | l) == 0)
+ return Val_int(FP_zero);
+ if (h == 0)
+ return Val_int(FP_subnormal);
+ if (h == 0x7FF00000) {
+ if (l == 0)
+ return Val_int(FP_infinite);
+ else
+ return Val_int(FP_nan);
+ }
+ return Val_int(FP_normal);
+#endif
+}
+
+CAMLprim value caml_classify_float(value vd)
+{
+ return caml_classify_float_unboxed(Double_val(vd));
+}
+
+/* The [caml_init_ieee_float] function should initialize floating-point hardware
+ so that it behaves as much as possible like the IEEE standard.
+ In particular, return special numbers like Infinity and NaN instead
+ of signalling exceptions. Currently, everyone is in IEEE mode
+ at program startup, except FreeBSD prior to 4.0R. */
+
+#ifdef __FreeBSD__
+#include <osreldate.h>
+#if (__FreeBSD_version < 400017)
+#include <floatingpoint.h>
+#endif
+#endif
+
+void caml_init_ieee_floats(void)
+{
+#if defined(__FreeBSD__) && (__FreeBSD_version < 400017)
+ fpsetmask(0);
+#endif
+}
diff --git a/test/monniaux/ocaml/byterun/freelist.c b/test/monniaux/ocaml/byterun/freelist.c
new file mode 100644
index 00000000..4782800e
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/freelist.c
@@ -0,0 +1,621 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#define FREELIST_DEBUG 0
+#if FREELIST_DEBUG
+#include <stdio.h>
+#endif
+
+#include <string.h>
+
+#include "caml/config.h"
+#include "caml/freelist.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/memory.h"
+#include "caml/major_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+
+/* The free-list is kept sorted by increasing addresses.
+ This makes the merging of adjacent free blocks possible.
+ (See [caml_fl_merge_block].)
+*/
+
+/* A free list block is a [value] (integer representing a pointer to the
+ first word after the block's header). The end of the list is NULL. */
+#define Val_NULL ((value) NULL)
+
+/* The sentinel can be located anywhere in memory, but it must not be
+ adjacent to any heap object. */
+static struct {
+ value filler1; /* Make sure the sentinel is never adjacent to any block. */
+ header_t h;
+ value first_field;
+ value filler2; /* Make sure the sentinel is never adjacent to any block. */
+} sentinel = {0, Make_header (0, 0, Caml_blue), Val_NULL, 0};
+
+#define Fl_head (Val_bp (&(sentinel.first_field)))
+static value fl_prev = Fl_head; /* Current allocation pointer. */
+static value fl_last = Val_NULL; /* Last block in the list. Only valid
+ just after [caml_fl_allocate] returns NULL. */
+value caml_fl_merge = Fl_head; /* Current insertion pointer. Managed
+ jointly with [sweep_slice]. */
+asize_t caml_fl_cur_wsz = 0; /* Number of words in the free list,
+ including headers but not fragments. */
+
+#define FLP_MAX 1000
+static value flp [FLP_MAX];
+static int flp_size = 0;
+static value beyond = Val_NULL;
+
+#define Next(b) (Field (b, 0))
+
+#define Policy_next_fit 0
+#define Policy_first_fit 1
+uintnat caml_allocation_policy = Policy_next_fit;
+#define policy caml_allocation_policy
+
+#ifdef DEBUG
+static void fl_check (void)
+{
+ value cur, prev;
+ int prev_found = 0, flp_found = 0, merge_found = 0;
+ uintnat size_found = 0;
+ int sz = 0;
+
+ prev = Fl_head;
+ cur = Next (prev);
+ while (cur != Val_NULL){
+ size_found += Whsize_bp (cur);
+ CAMLassert (Is_in_heap (cur));
+ if (cur == fl_prev) prev_found = 1;
+ if (policy == Policy_first_fit && Wosize_bp (cur) > sz){
+ sz = Wosize_bp (cur);
+ if (flp_found < flp_size){
+ CAMLassert (Next (flp[flp_found]) == cur);
+ ++ flp_found;
+ }else{
+ CAMLassert (beyond == Val_NULL || cur >= Next (beyond));
+ }
+ }
+ if (cur == caml_fl_merge) merge_found = 1;
+ prev = cur;
+ cur = Next (prev);
+ }
+ if (policy == Policy_next_fit) CAMLassert (prev_found || fl_prev == Fl_head);
+ if (policy == Policy_first_fit) CAMLassert (flp_found == flp_size);
+ CAMLassert (merge_found || caml_fl_merge == Fl_head);
+ CAMLassert (size_found == caml_fl_cur_wsz);
+}
+
+#endif
+
+/* [allocate_block] is called by [caml_fl_allocate]. Given a suitable free
+ block and the requested size, it allocates a new block from the free
+ block. There are three cases:
+ 0. The free block has the requested size. Detach the block from the
+ free-list and return it.
+ 1. The free block is 1 word longer than the requested size. Detach
+ the block from the free list. The remaining word cannot be linked:
+ turn it into an empty block (header only), and return the rest.
+ 2. The free block is large enough. Split it in two and return the right
+ block.
+ In all cases, the allocated block is right-justified in the free block:
+ it is located in the high-address words of the free block, so that
+ the linking of the free-list does not change in case 2.
+*/
+static header_t *allocate_block (mlsize_t wh_sz, int flpi, value prev,
+ value cur)
+{
+ header_t h = Hd_bp (cur);
+ CAMLassert (Whsize_hd (h) >= wh_sz);
+ if (Wosize_hd (h) < wh_sz + 1){ /* Cases 0 and 1. */
+ caml_fl_cur_wsz -= Whsize_hd (h);
+ Next (prev) = Next (cur);
+ CAMLassert (Is_in_heap (Next (prev)) || Next (prev) == Val_NULL);
+ if (caml_fl_merge == cur) caml_fl_merge = prev;
+#ifdef DEBUG
+ fl_last = Val_NULL;
+#endif
+ /* In case 1, the following creates the empty block correctly.
+ In case 0, it gives an invalid header to the block. The function
+ calling [caml_fl_allocate] will overwrite it. */
+ Hd_op (cur) = Make_header (0, 0, Caml_white);
+ if (policy == Policy_first_fit){
+ if (flpi + 1 < flp_size && flp[flpi + 1] == cur){
+ flp[flpi + 1] = prev;
+ }else if (flpi == flp_size - 1){
+ beyond = (prev == Fl_head) ? Val_NULL : prev;
+ -- flp_size;
+ }
+ }
+ }else{ /* Case 2. */
+ caml_fl_cur_wsz -= wh_sz;
+ Hd_op (cur) = Make_header (Wosize_hd (h) - wh_sz, 0, Caml_blue);
+ }
+ if (policy == Policy_next_fit) fl_prev = prev;
+ return (header_t *) &Field (cur, Wosize_hd (h) - wh_sz);
+}
+
+#ifdef CAML_INSTR
+static uintnat instr_size [20] =
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+static char *instr_name [20] = {
+ NULL,
+ "alloc01@",
+ "alloc02@",
+ "alloc03@",
+ "alloc04@",
+ "alloc05@",
+ "alloc06@",
+ "alloc07@",
+ "alloc08@",
+ "alloc09@",
+ "alloc10-19@",
+ "alloc20-29@",
+ "alloc30-39@",
+ "alloc40-49@",
+ "alloc50-59@",
+ "alloc60-69@",
+ "alloc70-79@",
+ "alloc80-89@",
+ "alloc90-99@",
+ "alloc_large@",
+};
+uintnat caml_instr_alloc_jump = 0;
+/* number of pointers followed to allocate from the free list */
+#endif /*CAML_INSTR*/
+
+/* [caml_fl_allocate] does not set the header of the newly allocated block.
+ The calling function must do it before any GC function gets called.
+ [caml_fl_allocate] returns a head pointer.
+*/
+header_t *caml_fl_allocate (mlsize_t wo_sz)
+{
+ value cur = Val_NULL, prev;
+ header_t *result;
+ int i;
+ mlsize_t sz, prevsz;
+ CAMLassert (sizeof (char *) == sizeof (value));
+ CAMLassert (wo_sz >= 1);
+#ifdef CAML_INSTR
+ if (wo_sz < 10){
+ ++instr_size[wo_sz];
+ }else if (wo_sz < 100){
+ ++instr_size[wo_sz/10 + 9];
+ }else{
+ ++instr_size[19];
+ }
+#endif /* CAML_INSTR */
+
+ switch (policy){
+ case Policy_next_fit:
+ CAMLassert (fl_prev != Val_NULL);
+ /* Search from [fl_prev] to the end of the list. */
+ prev = fl_prev;
+ cur = Next (prev);
+ while (cur != Val_NULL){
+ CAMLassert (Is_in_heap (cur));
+ if (Wosize_bp (cur) >= wo_sz){
+ return allocate_block (Whsize_wosize (wo_sz), 0, prev, cur);
+ }
+ prev = cur;
+ cur = Next (prev);
+#ifdef CAML_INSTR
+ ++ caml_instr_alloc_jump;
+#endif
+ }
+ fl_last = prev;
+ /* Search from the start of the list to [fl_prev]. */
+ prev = Fl_head;
+ cur = Next (prev);
+ while (prev != fl_prev){
+ if (Wosize_bp (cur) >= wo_sz){
+ return allocate_block (Whsize_wosize (wo_sz), 0, prev, cur);
+ }
+ prev = cur;
+ cur = Next (prev);
+#ifdef CAML_INSTR
+ ++ caml_instr_alloc_jump;
+#endif
+ }
+ /* No suitable block was found. */
+ return NULL;
+ break;
+
+ case Policy_first_fit: {
+ /* Search in the flp array. */
+ for (i = 0; i < flp_size; i++){
+ sz = Wosize_bp (Next (flp[i]));
+ if (sz >= wo_sz){
+#if FREELIST_DEBUG
+ if (i > 5) fprintf (stderr, "FLP: found at %d size=%d\n", i, wo_sz);
+#endif
+ result = allocate_block (Whsize_wosize (wo_sz), i, flp[i],
+ Next (flp[i]));
+ goto update_flp;
+ }
+ }
+ /* Extend the flp array. */
+ if (flp_size == 0){
+ prev = Fl_head;
+ prevsz = 0;
+ }else{
+ prev = Next (flp[flp_size - 1]);
+ prevsz = Wosize_bp (prev);
+ if (beyond != Val_NULL) prev = beyond;
+ }
+ while (flp_size < FLP_MAX){
+ cur = Next (prev);
+ if (cur == Val_NULL){
+ fl_last = prev;
+ beyond = (prev == Fl_head) ? Val_NULL : prev;
+ return NULL;
+ }else{
+ sz = Wosize_bp (cur);
+ if (sz > prevsz){
+ flp[flp_size] = prev;
+ ++ flp_size;
+ if (sz >= wo_sz){
+ beyond = cur;
+ i = flp_size - 1;
+#if FREELIST_DEBUG
+ if (flp_size > 5){
+ fprintf (stderr, "FLP: extended to %d\n", flp_size);
+ }
+#endif
+ result = allocate_block (Whsize_wosize (wo_sz), flp_size - 1, prev,
+ cur);
+ goto update_flp;
+ }
+ prevsz = sz;
+ }
+ }
+ prev = cur;
+ }
+ beyond = cur;
+
+ /* The flp table is full. Do a slow first-fit search. */
+#if FREELIST_DEBUG
+ fprintf (stderr, "FLP: table is full -- slow first-fit\n");
+#endif
+ if (beyond != Val_NULL){
+ prev = beyond;
+ }else{
+ prev = flp[flp_size - 1];
+ }
+ prevsz = Wosize_bp (Next (flp[FLP_MAX-1]));
+ CAMLassert (prevsz < wo_sz);
+ cur = Next (prev);
+ while (cur != Val_NULL){
+ CAMLassert (Is_in_heap (cur));
+ sz = Wosize_bp (cur);
+ if (sz < prevsz){
+ beyond = cur;
+ }else if (sz >= wo_sz){
+ return allocate_block (Whsize_wosize (wo_sz), flp_size, prev, cur);
+ }
+ prev = cur;
+ cur = Next (prev);
+ }
+ fl_last = prev;
+ return NULL;
+
+ update_flp: /* (i, sz) */
+ /* The block at [i] was removed or reduced. Update the table. */
+ CAMLassert (0 <= i && i < flp_size + 1);
+ if (i < flp_size){
+ if (i > 0){
+ prevsz = Wosize_bp (Next (flp[i-1]));
+ }else{
+ prevsz = 0;
+ }
+ if (i == flp_size - 1){
+ if (Wosize_bp (Next (flp[i])) <= prevsz){
+ beyond = Next (flp[i]);
+ -- flp_size;
+ }else{
+ beyond = Val_NULL;
+ }
+ }else{
+ value buf [FLP_MAX];
+ int j = 0;
+ mlsize_t oldsz = sz;
+
+ prev = flp[i];
+ while (prev != flp[i+1] && j < FLP_MAX - i){
+ cur = Next (prev);
+ sz = Wosize_bp (cur);
+ if (sz > prevsz){
+ buf[j++] = prev;
+ prevsz = sz;
+ if (sz >= oldsz){
+ CAMLassert (sz == oldsz);
+ break;
+ }
+ }
+ prev = cur;
+ }
+#if FREELIST_DEBUG
+ if (j > 2) fprintf (stderr, "FLP: update; buf size = %d\n", j);
+#endif
+ if (FLP_MAX >= flp_size + j - 1){
+ if (j != 1){
+ memmove (&flp[i+j], &flp[i+1], sizeof (value) * (flp_size-i-1));
+ }
+ if (j > 0) memmove (&flp[i], &buf[0], sizeof (value) * j);
+ flp_size += j - 1;
+ }else{
+ if (FLP_MAX > i + j){
+ if (j != 1){
+ memmove (&flp[i+j], &flp[i+1], sizeof (value) * (FLP_MAX-i-j));
+ }
+ if (j > 0) memmove (&flp[i], &buf[0], sizeof (value) * j);
+ }else{
+ if (i != FLP_MAX){
+ memmove (&flp[i], &buf[0], sizeof (value) * (FLP_MAX - i));
+ }
+ }
+ flp_size = FLP_MAX - 1;
+ beyond = Next (flp[FLP_MAX - 1]);
+ }
+ }
+ }
+ return result;
+ }
+ break;
+
+ default:
+ CAMLassert (0); /* unknown policy */
+ break;
+ }
+ return NULL; /* NOT REACHED */
+}
+
+/* Location of the last fragment seen by the sweeping code.
+ This is a pointer to the first word after the fragment, which is
+ the header of the next block.
+ Note that [last_fragment] doesn't point to the fragment itself,
+ but to the block after it.
+*/
+static header_t *last_fragment;
+
+void caml_fl_init_merge (void)
+{
+#ifdef CAML_INSTR
+ int i;
+ for (i = 1; i < 20; i++){
+ CAML_INSTR_INT (instr_name[i], instr_size[i]);
+ instr_size[i] = 0;
+ }
+#endif /* CAML_INSTR */
+ last_fragment = NULL;
+ caml_fl_merge = Fl_head;
+#ifdef DEBUG
+ fl_check ();
+#endif
+}
+
+static void truncate_flp (value changed)
+{
+ if (changed == Fl_head){
+ flp_size = 0;
+ beyond = Val_NULL;
+ }else{
+ while (flp_size > 0 && Next (flp[flp_size - 1]) >= changed)
+ -- flp_size;
+ if (beyond >= changed) beyond = Val_NULL;
+ }
+}
+
+/* This is called by caml_compact_heap. */
+void caml_fl_reset (void)
+{
+ Next (Fl_head) = Val_NULL;
+ switch (policy){
+ case Policy_next_fit:
+ fl_prev = Fl_head;
+ break;
+ case Policy_first_fit:
+ truncate_flp (Fl_head);
+ break;
+ default:
+ CAMLassert (0);
+ break;
+ }
+ caml_fl_cur_wsz = 0;
+ caml_fl_init_merge ();
+}
+
+/* [caml_fl_merge_block] returns the head pointer of the next block after [bp],
+ because merging blocks may change the size of [bp]. */
+header_t *caml_fl_merge_block (value bp)
+{
+ value prev, cur;
+ header_t *adj;
+ header_t hd = Hd_val (bp);
+ mlsize_t prev_wosz;
+
+ caml_fl_cur_wsz += Whsize_hd (hd);
+
+#ifdef DEBUG
+ caml_set_fields (bp, 0, Debug_free_major);
+#endif
+ prev = caml_fl_merge;
+ cur = Next (prev);
+ /* The sweep code makes sure that this is the right place to insert
+ this block: */
+ CAMLassert (prev < bp || prev == Fl_head);
+ CAMLassert (cur > bp || cur == Val_NULL);
+
+ if (policy == Policy_first_fit) truncate_flp (prev);
+
+ /* If [last_fragment] and [bp] are adjacent, merge them. */
+ if (last_fragment == Hp_bp (bp)){
+ mlsize_t bp_whsz = Whsize_val (bp);
+ if (bp_whsz <= Max_wosize){
+ hd = Make_header (bp_whsz, 0, Caml_white);
+ bp = (value) last_fragment;
+ Hd_val (bp) = hd;
+ caml_fl_cur_wsz += Whsize_wosize (0);
+ }
+ }
+
+ /* If [bp] and [cur] are adjacent, remove [cur] from the free-list
+ and merge them. */
+ adj = (header_t *) &Field (bp, Wosize_hd (hd));
+ if (adj == Hp_val (cur)){
+ value next_cur = Next (cur);
+ mlsize_t cur_whsz = Whsize_val (cur);
+
+ if (Wosize_hd (hd) + cur_whsz <= Max_wosize){
+ Next (prev) = next_cur;
+ if (policy == Policy_next_fit && fl_prev == cur) fl_prev = prev;
+ hd = Make_header (Wosize_hd (hd) + cur_whsz, 0, Caml_blue);
+ Hd_val (bp) = hd;
+ adj = (header_t *) &Field (bp, Wosize_hd (hd));
+#ifdef DEBUG
+ fl_last = Val_NULL;
+ Next (cur) = (value) Debug_free_major;
+ Hd_val (cur) = Debug_free_major;
+#endif
+ cur = next_cur;
+ }
+ }
+ /* If [prev] and [bp] are adjacent merge them, else insert [bp] into
+ the free-list if it is big enough. */
+ prev_wosz = Wosize_val (prev);
+ if ((header_t *) &Field (prev, prev_wosz) == Hp_val (bp)
+ && prev_wosz + Whsize_hd (hd) < Max_wosize){
+ Hd_val (prev) = Make_header (prev_wosz + Whsize_hd (hd), 0,Caml_blue);
+#ifdef DEBUG
+ Hd_val (bp) = Debug_free_major;
+#endif
+ CAMLassert (caml_fl_merge == prev);
+ }else if (Wosize_hd (hd) != 0){
+ Hd_val (bp) = Bluehd_hd (hd);
+ Next (bp) = cur;
+ Next (prev) = bp;
+ caml_fl_merge = bp;
+ }else{
+ /* This is a fragment. Leave it in white but remember it for eventual
+ merging with the next block. */
+ last_fragment = (header_t *) bp;
+ caml_fl_cur_wsz -= Whsize_wosize (0);
+ }
+ return adj;
+}
+
+/* This is a heap extension. We have to insert it in the right place
+ in the free-list.
+ [caml_fl_add_blocks] can only be called right after a call to
+ [caml_fl_allocate] that returned Val_NULL.
+ Most of the heap extensions are expected to be at the end of the
+ free list. (This depends on the implementation of [malloc].)
+
+ [bp] must point to a list of blocks chained by their field 0,
+ terminated by Val_NULL, and field 1 of the first block must point to
+ the last block.
+*/
+void caml_fl_add_blocks (value bp)
+{
+ value cur = bp;
+ CAMLassert (fl_last != Val_NULL);
+ CAMLassert (Next (fl_last) == Val_NULL);
+ do {
+ caml_fl_cur_wsz += Whsize_bp (cur);
+ cur = Field(cur, 0);
+ } while (cur != Val_NULL);
+
+ if (bp > fl_last){
+ Next (fl_last) = bp;
+ if (fl_last == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
+ caml_fl_merge = Field (bp, 1);
+ }
+ if (policy == Policy_first_fit && flp_size < FLP_MAX){
+ flp [flp_size++] = fl_last;
+ }
+ }else{
+ value prev;
+
+ prev = Fl_head;
+ cur = Next (prev);
+ while (cur != Val_NULL && cur < bp){
+ CAMLassert (prev < bp || prev == Fl_head);
+ /* XXX TODO: extend flp on the fly */
+ prev = cur;
+ cur = Next (prev);
+ }
+ CAMLassert (prev < bp || prev == Fl_head);
+ CAMLassert (cur > bp || cur == Val_NULL);
+ Next (Field (bp, 1)) = cur;
+ Next (prev) = bp;
+ /* When inserting blocks between [caml_fl_merge] and [caml_gc_sweep_hp],
+ we must advance [caml_fl_merge] to the new block, so that [caml_fl_merge]
+ is always the last free-list block before [caml_gc_sweep_hp]. */
+ if (prev == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
+ caml_fl_merge = Field (bp, 1);
+ }
+ if (policy == Policy_first_fit) truncate_flp (bp);
+ }
+}
+
+/* Cut a block of memory into Max_wosize pieces, give them headers,
+ and optionally merge them into the free list.
+ arguments:
+ p: pointer to the first word of the block
+ size: size of the block (in words)
+ do_merge: 1 -> do merge; 0 -> do not merge
+ color: which color to give to the pieces; if [do_merge] is 1, this
+ is overridden by the merge code, but we have historically used
+ [Caml_white].
+*/
+void caml_make_free_blocks (value *p, mlsize_t size, int do_merge, int color)
+{
+ mlsize_t sz;
+
+ while (size > 0){
+ if (size > Whsize_wosize (Max_wosize)){
+ sz = Whsize_wosize (Max_wosize);
+ }else{
+ sz = size;
+ }
+ *(header_t *)p =
+ Make_header (Wosize_whsize (sz), 0, color);
+ if (do_merge) caml_fl_merge_block (Val_hp (p));
+ size -= sz;
+ p += sz;
+ }
+}
+
+void caml_set_allocation_policy (uintnat p)
+{
+ switch (p){
+ case Policy_next_fit:
+ fl_prev = Fl_head;
+ policy = p;
+ break;
+ case Policy_first_fit:
+ flp_size = 0;
+ beyond = Val_NULL;
+ policy = p;
+ break;
+ default:
+ break;
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/gc_ctrl.c b/test/monniaux/ocaml/byterun/gc_ctrl.c
new file mode 100644
index 00000000..1cec4a25
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/gc_ctrl.c
@@ -0,0 +1,691 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include "caml/alloc.h"
+#include "caml/backtrace.h"
+#include "caml/compact.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/finalise.h"
+#include "caml/freelist.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/signals.h"
+#ifdef NATIVE_CODE
+#include "caml/stack.h"
+#else
+#include "caml/stacks.h"
+#endif
+#include "caml/startup_aux.h"
+
+#ifndef NATIVE_CODE
+extern uintnat caml_max_stack_size; /* defined in stacks.c */
+#endif
+
+double caml_stat_minor_words = 0.0,
+ caml_stat_promoted_words = 0.0,
+ caml_stat_major_words = 0.0;
+
+intnat caml_stat_minor_collections = 0,
+ caml_stat_major_collections = 0,
+ caml_stat_heap_wsz = 0,
+ caml_stat_top_heap_wsz = 0,
+ caml_stat_compactions = 0,
+ caml_stat_heap_chunks = 0;
+
+extern uintnat caml_major_heap_increment; /* percent or words; see major_gc.c */
+extern uintnat caml_percent_free; /* see major_gc.c */
+extern uintnat caml_percent_max; /* see compact.c */
+extern uintnat caml_allocation_policy; /* see freelist.c */
+
+#define Next(hp) ((hp) + Whsize_hp (hp))
+
+#ifdef DEBUG
+
+/* Check that [v]'s header looks good. [v] must be a block in the heap. */
+static void check_head (value v)
+{
+ CAMLassert (Is_block (v));
+ CAMLassert (Is_in_heap (v));
+
+ CAMLassert (Wosize_val (v) != 0);
+ CAMLassert (Color_hd (Hd_val (v)) != Caml_blue);
+ CAMLassert (Is_in_heap (v));
+ if (Tag_val (v) == Infix_tag){
+ int offset = Wsize_bsize (Infix_offset_val (v));
+ value trueval = Val_op (&Field (v, -offset));
+ CAMLassert (Tag_val (trueval) == Closure_tag);
+ CAMLassert (Wosize_val (trueval) > offset);
+ CAMLassert (Is_in_heap (&Field (trueval, Wosize_val (trueval) - 1)));
+ }else{
+ CAMLassert (Is_in_heap (&Field (v, Wosize_val (v) - 1)));
+ }
+ if (Tag_val (v) == Double_tag){
+ CAMLassert (Wosize_val (v) == Double_wosize);
+ }else if (Tag_val (v) == Double_array_tag){
+ CAMLassert (Wosize_val (v) % Double_wosize == 0);
+ }
+}
+
+static void check_block (header_t *hp)
+{
+ mlsize_t i;
+ value v = Val_hp (hp);
+ value f;
+
+ check_head (v);
+ switch (Tag_hp (hp)){
+ case Abstract_tag: break;
+ case String_tag:
+ break;
+ case Double_tag:
+ CAMLassert (Wosize_val (v) == Double_wosize);
+ break;
+ case Double_array_tag:
+ CAMLassert (Wosize_val (v) % Double_wosize == 0);
+ break;
+ case Custom_tag:
+ CAMLassert (!Is_in_heap (Custom_ops_val (v)));
+ break;
+
+ case Infix_tag:
+ CAMLassert (0);
+ break;
+
+ default:
+ CAMLassert (Tag_hp (hp) < No_scan_tag);
+ for (i = 0; i < Wosize_hp (hp); i++){
+ f = Field (v, i);
+ if (Is_block (f) && Is_in_heap (f)){
+ check_head (f);
+ CAMLassert (Color_val (f) != Caml_blue);
+ }
+ }
+ }
+}
+
+#endif /* DEBUG */
+
+/* Check the heap structure (if compiled in debug mode) and
+ gather statistics; return the stats if [returnstats] is true,
+ otherwise return [Val_unit].
+*/
+static value heap_stats (int returnstats)
+{
+ CAMLparam0 ();
+ intnat live_words = 0, live_blocks = 0,
+ free_words = 0, free_blocks = 0, largest_free = 0,
+ fragments = 0, heap_chunks = 0;
+ char *chunk = caml_heap_start, *chunk_end;
+ header_t *cur_hp;
+#ifdef DEBUG
+ header_t *prev_hp;
+#endif
+ header_t cur_hd;
+
+#ifdef DEBUG
+ caml_gc_message (-1, "### OCaml runtime: heap check ###\n");
+#endif
+
+ while (chunk != NULL){
+ ++ heap_chunks;
+ chunk_end = chunk + Chunk_size (chunk);
+#ifdef DEBUG
+ prev_hp = NULL;
+#endif
+ cur_hp = (header_t *) chunk;
+ while (cur_hp < (header_t *) chunk_end){
+ cur_hd = Hd_hp (cur_hp);
+ CAMLassert (Next (cur_hp) <= (header_t *) chunk_end);
+ switch (Color_hd (cur_hd)){
+ case Caml_white:
+ if (Wosize_hd (cur_hd) == 0){
+ ++ fragments;
+ CAMLassert (prev_hp == NULL
+ || Color_hp (prev_hp) != Caml_blue
+ || cur_hp == (header_t *) caml_gc_sweep_hp);
+ }else{
+ if (caml_gc_phase == Phase_sweep
+ && cur_hp >= (header_t *) caml_gc_sweep_hp){
+ ++ free_blocks;
+ free_words += Whsize_hd (cur_hd);
+ if (Whsize_hd (cur_hd) > largest_free){
+ largest_free = Whsize_hd (cur_hd);
+ }
+ }else{
+ ++ live_blocks;
+ live_words += Whsize_hd (cur_hd);
+#ifdef DEBUG
+ check_block (cur_hp);
+#endif
+ }
+ }
+ break;
+ case Caml_gray: case Caml_black:
+ CAMLassert (Wosize_hd (cur_hd) > 0);
+ ++ live_blocks;
+ live_words += Whsize_hd (cur_hd);
+#ifdef DEBUG
+ check_block (cur_hp);
+#endif
+ break;
+ case Caml_blue:
+ CAMLassert (Wosize_hd (cur_hd) > 0);
+ ++ free_blocks;
+ free_words += Whsize_hd (cur_hd);
+ if (Whsize_hd (cur_hd) > largest_free){
+ largest_free = Whsize_hd (cur_hd);
+ }
+ /* not true any more with big heap chunks
+ CAMLassert (prev_hp == NULL
+ || (Color_hp (prev_hp) != Caml_blue
+ && Wosize_hp (prev_hp) > 0)
+ || cur_hp == caml_gc_sweep_hp);
+ CAMLassert (Next (cur_hp) == chunk_end
+ || (Color_hp (Next (cur_hp)) != Caml_blue
+ && Wosize_hp (Next (cur_hp)) > 0)
+ || (Whsize_hd (cur_hd) + Wosize_hp (Next (cur_hp))
+ > Max_wosize)
+ || Next (cur_hp) == caml_gc_sweep_hp);
+ */
+ break;
+ }
+#ifdef DEBUG
+ prev_hp = cur_hp;
+#endif
+ cur_hp = Next (cur_hp);
+ }
+ CAMLassert (cur_hp == (header_t *) chunk_end);
+ chunk = Chunk_next (chunk);
+ }
+
+#ifdef DEBUG
+ caml_final_invariant_check();
+#endif
+
+ CAMLassert (heap_chunks == caml_stat_heap_chunks);
+ CAMLassert (live_words + free_words + fragments == caml_stat_heap_wsz);
+
+ if (returnstats){
+ CAMLlocal1 (res);
+
+ /* get a copy of these before allocating anything... */
+ double minwords = caml_stat_minor_words
+ + (double) (caml_young_alloc_end - caml_young_ptr);
+ double prowords = caml_stat_promoted_words;
+ double majwords = caml_stat_major_words + (double) caml_allocated_words;
+ intnat mincoll = caml_stat_minor_collections;
+ intnat majcoll = caml_stat_major_collections;
+ intnat heap_words = caml_stat_heap_wsz;
+ intnat cpct = caml_stat_compactions;
+ intnat top_heap_words = caml_stat_top_heap_wsz;
+
+ res = caml_alloc_tuple (16);
+ Store_field (res, 0, caml_copy_double (minwords));
+ Store_field (res, 1, caml_copy_double (prowords));
+ Store_field (res, 2, caml_copy_double (majwords));
+ Store_field (res, 3, Val_long (mincoll));
+ Store_field (res, 4, Val_long (majcoll));
+ Store_field (res, 5, Val_long (heap_words));
+ Store_field (res, 6, Val_long (heap_chunks));
+ Store_field (res, 7, Val_long (live_words));
+ Store_field (res, 8, Val_long (live_blocks));
+ Store_field (res, 9, Val_long (free_words));
+ Store_field (res, 10, Val_long (free_blocks));
+ Store_field (res, 11, Val_long (largest_free));
+ Store_field (res, 12, Val_long (fragments));
+ Store_field (res, 13, Val_long (cpct));
+ Store_field (res, 14, Val_long (top_heap_words));
+ Store_field (res, 15, Val_long (caml_stack_usage()));
+ CAMLreturn (res);
+ }else{
+ CAMLreturn (Val_unit);
+ }
+}
+
+#ifdef DEBUG
+void caml_heap_check (void)
+{
+ heap_stats (0);
+}
+#endif
+
+CAMLprim value caml_gc_stat(value v)
+{
+ value result;
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (v == Val_unit);
+ result = heap_stats (1);
+ CAML_INSTR_TIME (tmr, "explicit/gc_stat");
+ return result;
+}
+
+CAMLprim value caml_gc_quick_stat(value v)
+{
+ CAMLparam0 ();
+ CAMLlocal1 (res);
+
+ /* get a copy of these before allocating anything... */
+ double minwords = caml_stat_minor_words
+ + (double) (caml_young_alloc_end - caml_young_ptr);
+ double prowords = caml_stat_promoted_words;
+ double majwords = caml_stat_major_words + (double) caml_allocated_words;
+ intnat mincoll = caml_stat_minor_collections;
+ intnat majcoll = caml_stat_major_collections;
+ intnat heap_words = caml_stat_heap_wsz;
+ intnat top_heap_words = caml_stat_top_heap_wsz;
+ intnat cpct = caml_stat_compactions;
+ intnat heap_chunks = caml_stat_heap_chunks;
+
+ res = caml_alloc_tuple (16);
+ Store_field (res, 0, caml_copy_double (minwords));
+ Store_field (res, 1, caml_copy_double (prowords));
+ Store_field (res, 2, caml_copy_double (majwords));
+ Store_field (res, 3, Val_long (mincoll));
+ Store_field (res, 4, Val_long (majcoll));
+ Store_field (res, 5, Val_long (heap_words));
+ Store_field (res, 6, Val_long (heap_chunks));
+ Store_field (res, 7, Val_long (0));
+ Store_field (res, 8, Val_long (0));
+ Store_field (res, 9, Val_long (0));
+ Store_field (res, 10, Val_long (0));
+ Store_field (res, 11, Val_long (0));
+ Store_field (res, 12, Val_long (0));
+ Store_field (res, 13, Val_long (cpct));
+ Store_field (res, 14, Val_long (top_heap_words));
+ Store_field (res, 15, Val_long (caml_stack_usage()));
+ CAMLreturn (res);
+}
+
+double caml_gc_minor_words_unboxed()
+{
+ return (caml_stat_minor_words
+ + (double) (caml_young_alloc_end - caml_young_ptr));
+}
+
+CAMLprim value caml_gc_minor_words(value v)
+{
+ CAMLparam0 (); /* v is ignored */
+ CAMLreturn(caml_copy_double(caml_gc_minor_words_unboxed()));
+}
+
+CAMLprim value caml_gc_counters(value v)
+{
+ CAMLparam0 (); /* v is ignored */
+ CAMLlocal1 (res);
+
+ /* get a copy of these before allocating anything... */
+ double minwords = caml_stat_minor_words
+ + (double) (caml_young_alloc_end - caml_young_ptr);
+ double prowords = caml_stat_promoted_words;
+ double majwords = caml_stat_major_words + (double) caml_allocated_words;
+
+ res = caml_alloc_tuple (3);
+ Store_field (res, 0, caml_copy_double (minwords));
+ Store_field (res, 1, caml_copy_double (prowords));
+ Store_field (res, 2, caml_copy_double (majwords));
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_gc_huge_fallback_count (value v)
+{
+ return Val_long (caml_huge_fallback_count);
+}
+
+CAMLprim value caml_gc_get(value v)
+{
+ CAMLparam0 (); /* v is ignored */
+ CAMLlocal1 (res);
+
+ res = caml_alloc_tuple (8);
+ Store_field (res, 0, Val_long (caml_minor_heap_wsz)); /* s */
+ Store_field (res, 1, Val_long (caml_major_heap_increment)); /* i */
+ Store_field (res, 2, Val_long (caml_percent_free)); /* o */
+ Store_field (res, 3, Val_long (caml_verb_gc)); /* v */
+ Store_field (res, 4, Val_long (caml_percent_max)); /* O */
+#ifndef NATIVE_CODE
+ Store_field (res, 5, Val_long (caml_max_stack_size)); /* l */
+#else
+ Store_field (res, 5, Val_long (0));
+#endif
+ Store_field (res, 6, Val_long (caml_allocation_policy)); /* a */
+ Store_field (res, 7, Val_long (caml_major_window)); /* w */
+ CAMLreturn (res);
+}
+
+#define Max(x,y) ((x) < (y) ? (y) : (x))
+
+static uintnat norm_pfree (uintnat p)
+{
+ return Max (p, 1);
+}
+
+static uintnat norm_pmax (uintnat p)
+{
+ return p;
+}
+
+static intnat norm_minsize (intnat s)
+{
+ if (s < Minor_heap_min) s = Minor_heap_min;
+ if (s > Minor_heap_max) s = Minor_heap_max;
+ return s;
+}
+
+static uintnat norm_window (intnat w)
+{
+ if (w < 1) w = 1;
+ if (w > Max_major_window) w = Max_major_window;
+ return w;
+}
+
+CAMLprim value caml_gc_set(value v)
+{
+ uintnat newpf, newpm;
+ asize_t newheapincr;
+ asize_t newminwsz;
+ uintnat oldpolicy;
+ CAML_INSTR_SETUP (tmr, "");
+
+ caml_verb_gc = Long_val (Field (v, 3));
+
+#ifndef NATIVE_CODE
+ caml_change_max_stack_size (Long_val (Field (v, 5)));
+#endif
+
+ newpf = norm_pfree (Long_val (Field (v, 2)));
+ if (newpf != caml_percent_free){
+ caml_percent_free = newpf;
+ caml_gc_message (0x20, "New space overhead: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_free);
+ }
+
+ newpm = norm_pmax (Long_val (Field (v, 4)));
+ if (newpm != caml_percent_max){
+ caml_percent_max = newpm;
+ caml_gc_message (0x20, "New max overhead: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_max);
+ }
+
+ newheapincr = Long_val (Field (v, 1));
+ if (newheapincr != caml_major_heap_increment){
+ caml_major_heap_increment = newheapincr;
+ if (newheapincr > 1000){
+ caml_gc_message (0x20, "New heap increment size: %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk words\n",
+ caml_major_heap_increment/1024);
+ }else{
+ caml_gc_message (0x20, "New heap increment size: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
+ caml_major_heap_increment);
+ }
+ }
+ oldpolicy = caml_allocation_policy;
+ caml_set_allocation_policy (Long_val (Field (v, 6)));
+ if (oldpolicy != caml_allocation_policy){
+ caml_gc_message (0x20, "New allocation policy: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u\n", caml_allocation_policy);
+ }
+
+ /* This field was added in 4.03.0. */
+ if (Wosize_val (v) >= 8){
+ int old_window = caml_major_window;
+ caml_set_major_window (norm_window (Long_val (Field (v, 7))));
+ if (old_window != caml_major_window){
+ caml_gc_message (0x20, "New smoothing window size: %d\n",
+ caml_major_window);
+ }
+ }
+
+ /* Minor heap size comes last because it will trigger a minor collection
+ (thus invalidating [v]) and it can raise [Out_of_memory]. */
+ newminwsz = norm_minsize (Long_val (Field (v, 0)));
+ if (newminwsz != caml_minor_heap_wsz){
+ caml_gc_message (0x20, "New minor heap size: %"
+ ARCH_SIZET_PRINTF_FORMAT "uk words\n", newminwsz / 1024);
+ caml_set_minor_heap_size (Bsize_wsize (newminwsz));
+ }
+ CAML_INSTR_TIME (tmr, "explicit/gc_set");
+ return Val_unit;
+}
+
+CAMLprim value caml_gc_minor(value v)
+{
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (v == Val_unit);
+ caml_request_minor_gc ();
+ caml_gc_dispatch ();
+ CAML_INSTR_TIME (tmr, "explicit/gc_minor");
+ return Val_unit;
+}
+
+static void test_and_compact (void)
+{
+ float fp;
+
+ fp = 100.0 * caml_fl_cur_wsz / (caml_stat_heap_wsz - caml_fl_cur_wsz);
+ if (fp > 999999.0) fp = 999999.0;
+ caml_gc_message (0x200, "Estimated overhead (lower bound) = %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
+ (uintnat) fp);
+ if (fp >= caml_percent_max){
+ caml_gc_message (0x200, "Automatic compaction triggered.\n");
+ caml_compact_heap ();
+ }
+}
+
+CAMLprim value caml_gc_major(value v)
+{
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (v == Val_unit);
+ caml_gc_message (0x1, "Major GC cycle requested\n");
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ test_and_compact ();
+ caml_final_do_calls ();
+ CAML_INSTR_TIME (tmr, "explicit/gc_major");
+ return Val_unit;
+}
+
+CAMLprim value caml_gc_full_major(value v)
+{
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (v == Val_unit);
+ caml_gc_message (0x1, "Full major GC cycle requested\n");
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ caml_final_do_calls ();
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ test_and_compact ();
+ caml_final_do_calls ();
+ CAML_INSTR_TIME (tmr, "explicit/gc_full_major");
+ return Val_unit;
+}
+
+CAMLprim value caml_gc_major_slice (value v)
+{
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (Is_long (v));
+ caml_major_collection_slice (Long_val (v));
+ CAML_INSTR_TIME (tmr, "explicit/gc_major_slice");
+ return Val_long (0);
+}
+
+CAMLprim value caml_gc_compaction(value v)
+{
+ CAML_INSTR_SETUP (tmr, "");
+ CAMLassert (v == Val_unit);
+ caml_gc_message (0x10, "Heap compaction requested\n");
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ caml_final_do_calls ();
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ caml_compact_heap ();
+ caml_final_do_calls ();
+ CAML_INSTR_TIME (tmr, "explicit/gc_compact");
+ return Val_unit;
+}
+
+CAMLprim value caml_get_minor_free (value v)
+{
+ return Val_int (caml_young_ptr - caml_young_alloc_start);
+}
+
+CAMLprim value caml_get_major_bucket (value v)
+{
+ long i = Long_val (v);
+ if (i < 0) caml_invalid_argument ("Gc.get_bucket");
+ if (i < caml_major_window){
+ i += caml_major_ring_index;
+ if (i >= caml_major_window) i -= caml_major_window;
+ CAMLassert (0 <= i && i < caml_major_window);
+ return Val_long ((long) (caml_major_ring[i] * 1e6));
+ }else{
+ return Val_long (0);
+ }
+}
+
+CAMLprim value caml_get_major_credit (value v)
+{
+ CAMLassert (v == Val_unit);
+ return Val_long ((long) (caml_major_work_credit * 1e6));
+}
+
+uintnat caml_normalize_heap_increment (uintnat i)
+{
+ if (i < Bsize_wsize (Heap_chunk_min)){
+ i = Bsize_wsize (Heap_chunk_min);
+ }
+ return ((i + Page_size - 1) >> Page_log) << Page_log;
+}
+
+/* [minor_size] and [major_size] are numbers of words
+ [major_incr] is either a percentage or a number of words */
+void caml_init_gc (uintnat minor_size, uintnat major_size,
+ uintnat major_incr, uintnat percent_fr,
+ uintnat percent_m, uintnat window)
+{
+ uintnat major_heap_size =
+ Bsize_wsize (caml_normalize_heap_increment (major_size));
+
+ CAML_INSTR_INIT ();
+ if (caml_init_alloc_for_heap () != 0){
+ caml_fatal_error ("cannot initialize heap: mmap failed\n");
+ }
+ if (caml_page_table_initialize(Bsize_wsize(minor_size) + major_heap_size)){
+ caml_fatal_error ("OCaml runtime error: cannot initialize page table\n");
+ }
+ caml_set_minor_heap_size (Bsize_wsize (norm_minsize (minor_size)));
+ caml_major_heap_increment = major_incr;
+ caml_percent_free = norm_pfree (percent_fr);
+ caml_percent_max = norm_pmax (percent_m);
+ caml_init_major_heap (major_heap_size);
+ caml_major_window = norm_window (window);
+ caml_gc_message (0x20, "Initial minor heap size: %"
+ ARCH_SIZET_PRINTF_FORMAT "uk words\n",
+ caml_minor_heap_wsz / 1024);
+ caml_gc_message (0x20, "Initial major heap size: %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ major_heap_size / 1024);
+ caml_gc_message (0x20, "Initial space overhead: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_free);
+ caml_gc_message (0x20, "Initial max overhead: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_max);
+ if (caml_major_heap_increment > 1000){
+ caml_gc_message (0x20, "Initial heap increment: %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk words\n",
+ caml_major_heap_increment / 1024);
+ }else{
+ caml_gc_message (0x20, "Initial heap increment: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
+ caml_major_heap_increment);
+ }
+ caml_gc_message (0x20, "Initial allocation policy: %"
+ ARCH_INTNAT_PRINTF_FORMAT "u\n", caml_allocation_policy);
+ caml_gc_message (0x20, "Initial smoothing window: %d\n",
+ caml_major_window);
+}
+
+
+/* FIXME After the startup_aux.c unification, move these functions there. */
+
+CAMLprim value caml_runtime_variant (value unit)
+{
+ CAMLassert (unit == Val_unit);
+#if defined (DEBUG)
+ return caml_copy_string ("d");
+#elif defined (CAML_INSTR)
+ return caml_copy_string ("i");
+#else
+ return caml_copy_string ("");
+#endif
+}
+
+extern int caml_parser_trace;
+
+CAMLprim value caml_runtime_parameters (value unit)
+{
+#define F_Z ARCH_INTNAT_PRINTF_FORMAT
+#define F_S ARCH_SIZET_PRINTF_FORMAT
+
+ CAMLassert (unit == Val_unit);
+ return caml_alloc_sprintf
+ ("a=%d,b=%d,H=%"F_Z"u,i=%"F_Z"u,l=%"F_Z"u,o=%"F_Z"u,O=%"F_Z"u,p=%d,s=%"F_S"u,t=%"F_Z"u,v=%"F_Z"u,w=%d,W=%"F_Z"u",
+ /* a */ (int) caml_allocation_policy,
+ /* b */ caml_backtrace_active,
+ /* h */ /* missing */ /* FIXME add when changed to min_heap_size */
+ /* H */ caml_use_huge_pages,
+ /* i */ caml_major_heap_increment,
+#ifdef NATIVE_CODE
+ /* l */ (uintnat) 0,
+#else
+ /* l */ caml_max_stack_size,
+#endif
+ /* o */ caml_percent_free,
+ /* O */ caml_percent_max,
+ /* p */ caml_parser_trace,
+ /* R */ /* missing */
+ /* s */ caml_minor_heap_wsz,
+ /* t */ caml_trace_level,
+ /* v */ caml_verb_gc,
+ /* w */ caml_major_window,
+ /* W */ caml_runtime_warnings
+ );
+#undef F_Z
+#undef F_S
+}
+
+/* Control runtime warnings */
+
+CAMLprim value caml_ml_enable_runtime_warnings(value vbool)
+{
+ caml_runtime_warnings = Bool_val(vbool);
+ return Val_unit;
+}
+
+CAMLprim value caml_ml_runtime_warnings_enabled(value unit)
+{
+ CAMLassert (unit == Val_unit);
+ return Val_bool(caml_runtime_warnings);
+}
diff --git a/test/monniaux/ocaml/byterun/globroots.c b/test/monniaux/ocaml/byterun/globroots.c
new file mode 100644
index 00000000..f689723c
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/globroots.c
@@ -0,0 +1,291 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Registration of global memory roots */
+
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/globroots.h"
+
+/* The sets of global memory roots are represented as skip lists
+ (see William Pugh, "Skip lists: a probabilistic alternative to
+ balanced binary trees", Comm. ACM 33(6), 1990). */
+
+struct global_root {
+ value * root; /* the address of the root */
+ struct global_root * forward[1]; /* variable-length array */
+};
+
+#define NUM_LEVELS 17
+
+struct global_root_list {
+ value * root; /* dummy value for layout compatibility */
+ struct global_root * forward[NUM_LEVELS]; /* forward chaining */
+ int level; /* max used level */
+};
+
+/* Generate a random level for a new node: 0 with probability 3/4,
+ 1 with probability 3/16, 2 with probability 3/64, etc.
+ We use a simple linear congruential PRNG (see Knuth vol 2) instead
+ of random(), because we need exactly 32 bits of pseudo-random data
+ (i.e. 2 * (NUM_LEVELS - 1)). Moreover, the congruential PRNG
+ is faster and guaranteed to be deterministic (to reproduce bugs). */
+
+static uint32_t random_seed = 0;
+
+static int random_level(void)
+{
+ uint32_t r;
+ int level = 0;
+
+ /* Linear congruence with modulus = 2^32, multiplier = 69069
+ (Knuth vol 2 p. 106, line 15 of table 1), additive = 25173. */
+ r = random_seed = random_seed * 69069 + 25173;
+ /* Knuth (vol 2 p. 13) shows that the least significant bits are
+ "less random" than the most significant bits with a modulus of 2^m,
+ so consume most significant bits first */
+ while ((r & 0xC0000000U) == 0xC0000000U) { level++; r = r << 2; }
+ CAMLassert(level < NUM_LEVELS);
+ return level;
+}
+
+/* Insertion in a global root list */
+
+static void caml_insert_global_root(struct global_root_list * rootlist,
+ value * r)
+{
+ struct global_root * update[NUM_LEVELS];
+ struct global_root * e, * f;
+ int i, new_level;
+
+ CAMLassert(0 <= rootlist->level && rootlist->level < NUM_LEVELS);
+
+ /* Init "cursor" to list head */
+ e = (struct global_root *) rootlist;
+ /* Find place to insert new node */
+ for (i = rootlist->level; i >= 0; i--) {
+ while (1) {
+ f = e->forward[i];
+ if (f == NULL || f->root >= r) break;
+ e = f;
+ }
+ update[i] = e;
+ }
+ e = e->forward[0];
+ /* If already present, don't do anything */
+ if (e != NULL && e->root == r) return;
+ /* Insert additional element, updating list level if necessary */
+ new_level = random_level();
+ if (new_level > rootlist->level) {
+ for (i = rootlist->level + 1; i <= new_level; i++)
+ update[i] = (struct global_root *) rootlist;
+ rootlist->level = new_level;
+ }
+ e = caml_stat_alloc(sizeof(struct global_root) +
+ new_level * sizeof(struct global_root *));
+ e->root = r;
+ for (i = 0; i <= new_level; i++) {
+ e->forward[i] = update[i]->forward[i];
+ update[i]->forward[i] = e;
+ }
+}
+
+/* Deletion in a global root list */
+
+static void caml_delete_global_root(struct global_root_list * rootlist,
+ value * r)
+{
+ struct global_root * update[NUM_LEVELS];
+ struct global_root * e, * f;
+ int i;
+
+ CAMLassert(0 <= rootlist->level && rootlist->level < NUM_LEVELS);
+
+ /* Init "cursor" to list head */
+ e = (struct global_root *) rootlist;
+ /* Find element in list */
+ for (i = rootlist->level; i >= 0; i--) {
+ while (1) {
+ f = e->forward[i];
+ if (f == NULL || f->root >= r) break;
+ e = f;
+ }
+ update[i] = e;
+ }
+ e = e->forward[0];
+ /* If not found, nothing to do */
+ if (e == NULL || e->root != r) return;
+ /* Rebuild list without node */
+ for (i = 0; i <= rootlist->level; i++) {
+ if (update[i]->forward[i] == e)
+ update[i]->forward[i] = e->forward[i];
+ }
+ /* Reclaim list element */
+ caml_stat_free(e);
+ /* Down-correct list level */
+ while (rootlist->level > 0 &&
+ rootlist->forward[rootlist->level] == NULL)
+ rootlist->level--;
+}
+
+/* Iterate over a global root list */
+
+static void caml_iterate_global_roots(scanning_action f,
+ struct global_root_list * rootlist)
+{
+ struct global_root * gr;
+
+ for (gr = rootlist->forward[0]; gr != NULL; gr = gr->forward[0]) {
+ f(*(gr->root), gr->root);
+ }
+}
+
+/* Empty a global root list */
+
+static void caml_empty_global_roots(struct global_root_list * rootlist)
+{
+ struct global_root * gr, * next;
+ int i;
+
+ CAMLassert(0 <= rootlist->level && rootlist->level < NUM_LEVELS);
+
+ for (gr = rootlist->forward[0]; gr != NULL; /**/) {
+ next = gr->forward[0];
+ caml_stat_free(gr);
+ gr = next;
+ }
+ for (i = 0; i <= rootlist->level; i++) rootlist->forward[i] = NULL;
+ rootlist->level = 0;
+}
+
+/* The three global root lists */
+
+struct global_root_list caml_global_roots = { NULL, { NULL, }, 0 };
+ /* mutable roots, don't know whether old or young */
+struct global_root_list caml_global_roots_young = { NULL, { NULL, }, 0 };
+ /* generational roots pointing to minor or major heap */
+struct global_root_list caml_global_roots_old = { NULL, { NULL, }, 0 };
+ /* generational roots pointing to major heap */
+
+/* Register a global C root of the mutable kind */
+
+CAMLexport void caml_register_global_root(value *r)
+{
+ CAMLassert (((intnat) r & 3) == 0); /* compact.c demands this (for now) */
+ caml_insert_global_root(&caml_global_roots, r);
+}
+
+/* Un-register a global C root of the mutable kind */
+
+CAMLexport void caml_remove_global_root(value *r)
+{
+ caml_delete_global_root(&caml_global_roots, r);
+}
+
+/* Register a global C root of the generational kind */
+
+CAMLexport void caml_register_generational_global_root(value *r)
+{
+ value v = *r;
+ CAMLassert (((intnat) r & 3) == 0); /* compact.c demands this (for now) */
+ if (Is_block(v)) {
+ if (Is_young(v))
+ caml_insert_global_root(&caml_global_roots_young, r);
+ else if (Is_in_heap(v))
+ caml_insert_global_root(&caml_global_roots_old, r);
+ }
+}
+
+/* Un-register a global C root of the generational kind */
+
+CAMLexport void caml_remove_generational_global_root(value *r)
+{
+ value v = *r;
+ if (Is_block(v)) {
+ if (Is_in_heap_or_young(v))
+ caml_delete_global_root(&caml_global_roots_young, r);
+ if (Is_in_heap(v))
+ caml_delete_global_root(&caml_global_roots_old, r);
+ }
+}
+
+/* Modify the value of a global C root of the generational kind */
+
+CAMLexport void caml_modify_generational_global_root(value *r, value newval)
+{
+ value oldval = *r;
+
+ /* It is OK to have a root in roots_young that suddenly points to
+ the old generation -- the next minor GC will take care of that.
+ What needs corrective action is a root in roots_old that suddenly
+ points to the young generation. */
+ if (Is_block(newval) && Is_young(newval) &&
+ Is_block(oldval) && Is_in_heap(oldval)) {
+ caml_delete_global_root(&caml_global_roots_old, r);
+ caml_insert_global_root(&caml_global_roots_young, r);
+ }
+ /* PR#4704 */
+ else if (!Is_block(oldval) && Is_block(newval)) {
+ /* The previous value in the root was unboxed but now it is boxed.
+ The root won't appear in any of the root lists thus far (by virtue
+ of the operation of [caml_register_generational_global_root]), so we
+ need to make sure it gets in, or else it will never be scanned. */
+ if (Is_young(newval))
+ caml_insert_global_root(&caml_global_roots_young, r);
+ else if (Is_in_heap(newval))
+ caml_insert_global_root(&caml_global_roots_old, r);
+ }
+ else if (Is_block(oldval) && !Is_block(newval)) {
+ /* The previous value in the root was boxed but now it is unboxed, so
+ the root should be removed. If [oldval] is young, this will happen
+ anyway at the next minor collection, but it is safer to delete it
+ here. */
+ if (Is_in_heap_or_young(oldval))
+ caml_delete_global_root(&caml_global_roots_young, r);
+ if (Is_in_heap(oldval))
+ caml_delete_global_root(&caml_global_roots_old, r);
+ }
+ /* end PR#4704 */
+ *r = newval;
+}
+
+/* Scan all global roots */
+
+void caml_scan_global_roots(scanning_action f)
+{
+ caml_iterate_global_roots(f, &caml_global_roots);
+ caml_iterate_global_roots(f, &caml_global_roots_young);
+ caml_iterate_global_roots(f, &caml_global_roots_old);
+}
+
+/* Scan global roots for a minor collection */
+
+void caml_scan_global_young_roots(scanning_action f)
+{
+ struct global_root * gr;
+
+ caml_iterate_global_roots(f, &caml_global_roots);
+ caml_iterate_global_roots(f, &caml_global_roots_young);
+ /* Move young roots to old roots */
+ for (gr = caml_global_roots_young.forward[0];
+ gr != NULL; gr = gr->forward[0]) {
+ caml_insert_global_root(&caml_global_roots_old, gr->root);
+ }
+ caml_empty_global_roots(&caml_global_roots_young);
+}
diff --git a/test/monniaux/ocaml/byterun/hash.c b/test/monniaux/ocaml/byterun/hash.c
new file mode 100644
index 00000000..f7d0d222
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/hash.c
@@ -0,0 +1,419 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* The generic hashing primitive */
+
+/* The interface of this file is in "mlvalues.h" (for [caml_hash_variant])
+ and in "hash.h" (for the other exported functions). */
+
+#include "caml/mlvalues.h"
+#include "caml/custom.h"
+#include "caml/memory.h"
+#include "caml/hash.h"
+
+/* The new implementation, based on MurmurHash 3,
+ http://code.google.com/p/smhasher/ */
+
+#define ROTL32(x,n) ((x) << n | (x) >> (32-n))
+
+#define MIX(h,d) \
+ d *= 0xcc9e2d51; \
+ d = ROTL32(d, 15); \
+ d *= 0x1b873593; \
+ h ^= d; \
+ h = ROTL32(h, 13); \
+ h = h * 5 + 0xe6546b64;
+
+#define FINAL_MIX(h) \
+ h ^= h >> 16; \
+ h *= 0x85ebca6b; \
+ h ^= h >> 13; \
+ h *= 0xc2b2ae35; \
+ h ^= h >> 16;
+
+CAMLexport uint32_t caml_hash_mix_uint32(uint32_t h, uint32_t d)
+{
+ MIX(h, d);
+ return h;
+}
+
+/* Mix a platform-native integer. */
+
+CAMLexport uint32_t caml_hash_mix_intnat(uint32_t h, intnat d)
+{
+ uint32_t n;
+#ifdef ARCH_SIXTYFOUR
+ /* Mix the low 32 bits and the high 32 bits, in a way that preserves
+ 32/64 compatibility: we want n = (uint32_t) d
+ if d is in the range [-2^31, 2^31-1]. */
+ n = (d >> 32) ^ (d >> 63) ^ d;
+ /* If 0 <= d < 2^31: d >> 32 = 0 d >> 63 = 0
+ If -2^31 <= d < 0: d >> 32 = -1 d >> 63 = -1
+ In both cases, n = (uint32_t) d. */
+#else
+ n = d;
+#endif
+ MIX(h, n);
+ return h;
+}
+
+/* Mix a 64-bit integer. */
+
+CAMLexport uint32_t caml_hash_mix_int64(uint32_t h, int64_t d)
+{
+ uint32_t hi = (uint32_t) (d >> 32), lo = (uint32_t) d;
+ MIX(h, lo);
+ MIX(h, hi);
+ return h;
+}
+
+/* Mix a double-precision float.
+ Treats +0.0 and -0.0 identically.
+ Treats all NaNs identically.
+*/
+
+CAMLexport uint32_t caml_hash_mix_double(uint32_t hash, double d)
+{
+ union {
+ double d;
+#if defined(ARCH_BIG_ENDIAN) || (defined(__arm__) && !defined(__ARM_EABI__))
+ struct { uint32_t h; uint32_t l; } i;
+#else
+ struct { uint32_t l; uint32_t h; } i;
+#endif
+ } u;
+ uint32_t h, l;
+ /* Convert to two 32-bit halves */
+ u.d = d;
+ h = u.i.h; l = u.i.l;
+ /* Normalize NaNs */
+ if ((h & 0x7FF00000) == 0x7FF00000 && (l | (h & 0xFFFFF)) != 0) {
+ h = 0x7FF00000;
+ l = 0x00000001;
+ }
+ /* Normalize -0 into +0 */
+ else if (h == 0x80000000 && l == 0) {
+ h = 0;
+ }
+ MIX(hash, l);
+ MIX(hash, h);
+ return hash;
+}
+
+/* Mix a single-precision float.
+ Treats +0.0 and -0.0 identically.
+ Treats all NaNs identically.
+*/
+
+CAMLexport uint32_t caml_hash_mix_float(uint32_t hash, float d)
+{
+ union {
+ float f;
+ uint32_t i;
+ } u;
+ uint32_t n;
+ /* Convert to int32_t */
+ u.f = d; n = u.i;
+ /* Normalize NaNs */
+ if ((n & 0x7F800000) == 0x7F800000 && (n & 0x007FFFFF) != 0) {
+ n = 0x7F800001;
+ }
+ /* Normalize -0 into +0 */
+ else if (n == 0x80000000) {
+ n = 0;
+ }
+ MIX(hash, n);
+ return hash;
+}
+
+/* Mix an OCaml string */
+
+CAMLexport uint32_t caml_hash_mix_string(uint32_t h, value s)
+{
+ mlsize_t len = caml_string_length(s);
+ mlsize_t i;
+ uint32_t w;
+
+ /* Mix by 32-bit blocks (little-endian) */
+ for (i = 0; i + 4 <= len; i += 4) {
+#ifdef ARCH_BIG_ENDIAN
+ w = Byte_u(s, i)
+ | (Byte_u(s, i+1) << 8)
+ | (Byte_u(s, i+2) << 16)
+ | (Byte_u(s, i+3) << 24);
+#else
+ w = *((uint32_t *) &Byte_u(s, i));
+#endif
+ MIX(h, w);
+ }
+ /* Finish with up to 3 bytes */
+ w = 0;
+ switch (len & 3) {
+ case 3: w = Byte_u(s, i+2) << 16; /* fallthrough */
+ case 2: w |= Byte_u(s, i+1) << 8; /* fallthrough */
+ case 1: w |= Byte_u(s, i);
+ MIX(h, w);
+ default: /*skip*/; /* len & 3 == 0, no extra bytes, do nothing */
+ }
+ /* Finally, mix in the length. Ignore the upper 32 bits, generally 0. */
+ h ^= (uint32_t) len;
+ return h;
+}
+
+/* Maximal size of the queue used for breadth-first traversal. */
+#define HASH_QUEUE_SIZE 256
+/* Maximal number of Forward_tag links followed in one step */
+#define MAX_FORWARD_DEREFERENCE 1000
+
+/* The generic hash function */
+
+CAMLprim value caml_hash(value count, value limit, value seed, value obj)
+{
+ value queue[HASH_QUEUE_SIZE]; /* Queue of values to examine */
+ intnat rd; /* Position of first value in queue */
+ intnat wr; /* One past position of last value in queue */
+ intnat sz; /* Max number of values to put in queue */
+ intnat num; /* Max number of meaningful values to see */
+ uint32_t h; /* Rolling hash */
+ value v;
+ mlsize_t i, len;
+
+ sz = Long_val(limit);
+ if (sz < 0 || sz > HASH_QUEUE_SIZE) sz = HASH_QUEUE_SIZE;
+ num = Long_val(count);
+ h = Int_val(seed);
+ queue[0] = obj; rd = 0; wr = 1;
+
+ while (rd < wr && num > 0) {
+ v = queue[rd++];
+ again:
+ if (Is_long(v)) {
+ h = caml_hash_mix_intnat(h, v);
+ num--;
+ }
+ else if (Is_in_value_area(v)) {
+ switch (Tag_val(v)) {
+ case String_tag:
+ h = caml_hash_mix_string(h, v);
+ num--;
+ break;
+ case Double_tag:
+ h = caml_hash_mix_double(h, Double_val(v));
+ num--;
+ break;
+ case Double_array_tag:
+ for (i = 0, len = Wosize_val(v) / Double_wosize; i < len; i++) {
+ h = caml_hash_mix_double(h, Double_flat_field(v, i));
+ num--;
+ if (num <= 0) break;
+ }
+ break;
+ case Abstract_tag:
+ /* Block contents unknown. Do nothing. */
+ break;
+ case Infix_tag:
+ /* Mix in the offset to distinguish different functions from
+ the same mutually-recursive definition */
+ h = caml_hash_mix_uint32(h, Infix_offset_val(v));
+ v = v - Infix_offset_val(v);
+ goto again;
+ case Forward_tag:
+ /* PR#6361: we can have a loop here, so limit the number of
+ Forward_tag links being followed */
+ for (i = MAX_FORWARD_DEREFERENCE; i > 0; i--) {
+ v = Forward_val(v);
+ if (Is_long(v) || !Is_in_value_area(v) || Tag_val(v) != Forward_tag)
+ goto again;
+ }
+ /* Give up on this object and move to the next */
+ break;
+ case Object_tag:
+ h = caml_hash_mix_intnat(h, Oid_val(v));
+ num--;
+ break;
+ case Custom_tag:
+ /* If no hashing function provided, do nothing. */
+ /* Only use low 32 bits of custom hash, for 32/64 compatibility */
+ if (Custom_ops_val(v)->hash != NULL) {
+ uint32_t n = (uint32_t) Custom_ops_val(v)->hash(v);
+ h = caml_hash_mix_uint32(h, n);
+ num--;
+ }
+ break;
+ default:
+ /* Mix in the tag and size, but do not count this towards [num] */
+ h = caml_hash_mix_uint32(h, Whitehd_hd(Hd_val(v)));
+ /* Copy fields into queue, not exceeding the total size [sz] */
+ for (i = 0, len = Wosize_val(v); i < len; i++) {
+ if (wr >= sz) break;
+ queue[wr++] = Field(v, i);
+ }
+ break;
+ }
+ } else {
+ /* v is a pointer outside the heap, probably a code pointer.
+ Shall we count it? Let's say yes by compatibility with old code. */
+ h = caml_hash_mix_intnat(h, v);
+ num--;
+ }
+ }
+ /* Final mixing of bits */
+ FINAL_MIX(h);
+ /* Fold result to the range [0, 2^30-1] so that it is a nonnegative
+ OCaml integer both on 32 and 64-bit platforms. */
+ return Val_int(h & 0x3FFFFFFFU);
+}
+
+/* The old implementation */
+
+struct hash_state {
+ uintnat accu;
+ intnat univ_limit, univ_count;
+};
+
+static void hash_aux(struct hash_state*, value obj);
+
+CAMLprim value caml_hash_univ_param(value count, value limit, value obj)
+{
+ struct hash_state h;
+ h.univ_limit = Long_val(limit);
+ h.univ_count = Long_val(count);
+ h.accu = 0;
+ hash_aux(&h, obj);
+ return Val_long(h.accu & 0x3FFFFFFF);
+ /* The & has two purposes: ensure that the return value is positive
+ and give the same result on 32 bit and 64 bit architectures. */
+}
+
+#define Alpha 65599
+#define Beta 19
+#define Combine(new) (h->accu = h->accu * Alpha + (new))
+#define Combine_small(new) (h->accu = h->accu * Beta + (new))
+
+static void hash_aux(struct hash_state* h, value obj)
+{
+ unsigned char * p;
+ mlsize_t i, j;
+ tag_t tag;
+
+ h->univ_limit--;
+ if (h->univ_count < 0 || h->univ_limit < 0) return;
+
+ again:
+ if (Is_long(obj)) {
+ h->univ_count--;
+ Combine(Long_val(obj));
+ return;
+ }
+
+ /* Pointers into the heap are well-structured blocks. So are atoms.
+ We can inspect the block contents. */
+
+ CAMLassert (Is_block (obj));
+ if (Is_in_value_area(obj)) {
+ tag = Tag_val(obj);
+ switch (tag) {
+ case String_tag:
+ h->univ_count--;
+ i = caml_string_length(obj);
+ for (p = &Byte_u(obj, 0); i > 0; i--, p++)
+ Combine_small(*p);
+ break;
+ case Double_tag:
+ /* For doubles, we inspect their binary representation, LSB first.
+ The results are consistent among all platforms with IEEE floats. */
+ h->univ_count--;
+#ifdef ARCH_BIG_ENDIAN
+ for (p = &Byte_u(obj, sizeof(double) - 1), i = sizeof(double);
+ i > 0;
+ p--, i--)
+#else
+ for (p = &Byte_u(obj, 0), i = sizeof(double);
+ i > 0;
+ p++, i--)
+#endif
+ Combine_small(*p);
+ break;
+ case Double_array_tag:
+ h->univ_count--;
+ for (j = 0; j < Bosize_val(obj); j += sizeof(double)) {
+#ifdef ARCH_BIG_ENDIAN
+ for (p = &Byte_u(obj, j + sizeof(double) - 1), i = sizeof(double);
+ i > 0;
+ p--, i--)
+#else
+ for (p = &Byte_u(obj, j), i = sizeof(double);
+ i > 0;
+ p++, i--)
+#endif
+ Combine_small(*p);
+ }
+ break;
+ case Abstract_tag:
+ /* We don't know anything about the contents of the block.
+ Better do nothing. */
+ break;
+ case Infix_tag:
+ hash_aux(h, obj - Infix_offset_val(obj));
+ break;
+ case Forward_tag:
+ obj = Forward_val (obj);
+ goto again;
+ case Object_tag:
+ h->univ_count--;
+ Combine(Oid_val(obj));
+ break;
+ case Custom_tag:
+ /* If no hashing function provided, do nothing */
+ if (Custom_ops_val(obj)->hash != NULL) {
+ h->univ_count--;
+ Combine(Custom_ops_val(obj)->hash(obj));
+ }
+ break;
+ default:
+ h->univ_count--;
+ Combine_small(tag);
+ i = Wosize_val(obj);
+ while (i != 0) {
+ i--;
+ hash_aux(h, Field(obj, i));
+ }
+ break;
+ }
+ return;
+ }
+
+ /* Otherwise, obj is a pointer outside the heap, to an object with
+ a priori unknown structure. Use its physical address as hash key. */
+ Combine((intnat) obj);
+}
+
+/* Hashing variant tags */
+
+CAMLexport value caml_hash_variant(char const * tag)
+{
+ value accu;
+ /* Same hashing algorithm as in ../typing/btype.ml, function hash_variant */
+ for (accu = Val_int(0); *tag != 0; tag++)
+ accu = Val_int(223 * Int_val(accu) + *((unsigned char *) tag));
+#ifdef ARCH_SIXTYFOUR
+ accu = accu & Val_long(0x7FFFFFFFL);
+#endif
+ /* Force sign extension of bit 31 for compatibility between 32 and 64-bit
+ platforms */
+ return (int32_t) accu;
+}
diff --git a/test/monniaux/ocaml/byterun/instrtrace.c b/test/monniaux/ocaml/byterun/instrtrace.c
new file mode 100644
index 00000000..824562e1
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/instrtrace.c
@@ -0,0 +1,269 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Trace the instructions executed */
+
+#ifdef DEBUG
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "caml/instrtrace.h"
+#include "caml/instruct.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/opnames.h"
+#include "caml/prims.h"
+#include "caml/stacks.h"
+#include "caml/startup_aux.h"
+
+extern code_t caml_start_code;
+
+intnat caml_icount = 0;
+
+void caml_stop_here () {}
+
+void caml_disasm_instr(pc)
+ code_t pc;
+{
+ int instr = *pc;
+ printf("%6ld %s", (long) (pc - caml_start_code),
+ instr < 0 || instr > STOP ? "???" : names_of_instructions[instr]);
+ pc++;
+ switch(instr) {
+ /* Instructions with one integer operand */
+ case PUSHACC: case ACC: case POP: case ASSIGN:
+ case PUSHENVACC: case ENVACC: case PUSH_RETADDR: case APPLY:
+ case APPTERM1: case APPTERM2: case APPTERM3: case RETURN:
+ case GRAB: case PUSHGETGLOBAL: case GETGLOBAL: case SETGLOBAL:
+ case PUSHATOM: case ATOM: case MAKEBLOCK1: case MAKEBLOCK2:
+ case MAKEBLOCK3: case MAKEFLOATBLOCK:
+ case GETFIELD: case SETFIELD: case GETFLOATFIELD: case SETFLOATFIELD:
+ case BRANCH: case BRANCHIF: case BRANCHIFNOT: case PUSHTRAP:
+ case CONSTINT: case PUSHCONSTINT: case OFFSETINT: case OFFSETREF:
+ case OFFSETCLOSURE: case PUSHOFFSETCLOSURE:
+ printf(" %d\n", pc[0]); break;
+ /* Instructions with two operands */
+ case APPTERM: case CLOSURE: case CLOSUREREC: case PUSHGETGLOBALFIELD:
+ case GETGLOBALFIELD: case MAKEBLOCK:
+ case BEQ: case BNEQ: case BLTINT: case BLEINT: case BGTINT: case BGEINT:
+ case BULTINT: case BUGEINT:
+ printf(" %d, %d\n", pc[0], pc[1]); break;
+ /* Instructions with a C primitive as operand */
+ case C_CALLN:
+ printf(" %d,", pc[0]); pc++;
+ /* fallthrough */
+ case C_CALL1: case C_CALL2: case C_CALL3: case C_CALL4: case C_CALL5:
+ if (pc[0] < 0 || pc[0] >= caml_prim_name_table.size)
+ printf(" unknown primitive %d\n", pc[0]);
+ else
+ printf(" %s\n", (char *) caml_prim_name_table.contents[pc[0]]);
+ break;
+ default:
+ printf("\n");
+ }
+ fflush (stdout);
+}
+
+char * caml_instr_string (code_t pc)
+{
+ static char buf[256];
+ char nambuf[128];
+ int instr = *pc;
+ char *nam;
+
+ nam = (instr < 0 || instr > STOP)
+ ? (snprintf (nambuf, sizeof(nambuf), "???%d", instr), nambuf)
+ : names_of_instructions[instr];
+ pc++;
+ switch (instr) {
+ /* Instructions with one integer operand */
+ case PUSHACC:
+ case ACC:
+ case POP:
+ case ASSIGN:
+ case PUSHENVACC:
+ case ENVACC:
+ case PUSH_RETADDR:
+ case APPLY:
+ case APPTERM1:
+ case APPTERM2:
+ case APPTERM3:
+ case RETURN:
+ case GRAB:
+ case PUSHGETGLOBAL:
+ case GETGLOBAL:
+ case SETGLOBAL:
+ case PUSHATOM:
+ case ATOM:
+ case MAKEBLOCK1:
+ case MAKEBLOCK2:
+ case MAKEBLOCK3:
+ case MAKEFLOATBLOCK:
+ case GETFIELD:
+ case SETFIELD:
+ case GETFLOATFIELD:
+ case SETFLOATFIELD:
+ case BRANCH:
+ case BRANCHIF:
+ case BRANCHIFNOT:
+ case PUSHTRAP:
+ case CONSTINT:
+ case PUSHCONSTINT:
+ case OFFSETINT:
+ case OFFSETREF:
+ case OFFSETCLOSURE:
+ case PUSHOFFSETCLOSURE:
+ snprintf(buf, sizeof(buf), "%s %d", nam, pc[0]);
+ break;
+ /* Instructions with two operands */
+ case APPTERM:
+ case CLOSURE:
+ case CLOSUREREC:
+ case PUSHGETGLOBALFIELD:
+ case GETGLOBALFIELD:
+ case MAKEBLOCK:
+ case BEQ:
+ case BNEQ:
+ case BLTINT:
+ case BLEINT:
+ case BGTINT:
+ case BGEINT:
+ case BULTINT:
+ case BUGEINT:
+ snprintf(buf, sizeof(buf), "%s %d, %d", nam, pc[0], pc[1]);
+ break;
+ case SWITCH:
+ snprintf(buf, sizeof(buf), "SWITCH sz%#lx=%ld::ntag%ld nint%ld",
+ (long) pc[0], (long) pc[0], (unsigned long) pc[0] >> 16,
+ (unsigned long) pc[0] & 0xffff);
+ break;
+ /* Instructions with a C primitive as operand */
+ case C_CALLN:
+ snprintf(buf, sizeof(buf), "%s %d,", nam, pc[0]);
+ pc++;
+ /* fallthrough */
+ case C_CALL1:
+ case C_CALL2:
+ case C_CALL3:
+ case C_CALL4:
+ case C_CALL5:
+ if (pc[0] < 0 || pc[0] >= caml_prim_name_table.size)
+ snprintf(buf, sizeof(buf), "%s unknown primitive %d", nam, pc[0]);
+ else
+ snprintf(buf, sizeof(buf), "%s %s",
+ nam, (char *) caml_prim_name_table.contents[pc[0]]);
+ break;
+ default:
+ snprintf(buf, sizeof(buf), "%s", nam);
+ break;
+ };
+ return buf;
+}
+
+
+void
+caml_trace_value_file (value v, code_t prog, int proglen, FILE * f)
+{
+ int i;
+ fprintf (f, "%#" ARCH_INTNAT_PRINTF_FORMAT "x", v);
+ if (!v)
+ return;
+ if (prog && v % sizeof (int) == 0
+ && (code_t) v >= prog
+ && (code_t) v < (code_t) ((char *) prog + proglen))
+ fprintf (f, "=code@%ld", (long) ((code_t) v - prog));
+ else if (Is_long (v))
+ fprintf (f, "=long%" ARCH_INTNAT_PRINTF_FORMAT "d", Long_val (v));
+ else if ((void*)v >= (void*)caml_stack_low
+ && (void*)v < (void*)caml_stack_high)
+ fprintf (f, "=stack_%ld", (long) ((intnat*)caml_stack_high - (intnat*)v));
+ else if (Is_block (v)) {
+ int s = Wosize_val (v);
+ int tg = Tag_val (v);
+ int l = 0;
+ switch (tg) {
+ case Closure_tag:
+ fprintf (f, "=closure[s%d,cod%ld]",
+ s, (long) ((code_t) (Code_val (v)) - prog));
+ goto displayfields;
+ case String_tag:
+ l = caml_string_length (v);
+ fprintf (f, "=string[s%dL%d]'", s, l);
+ for (i = 0; i < ((l>0x1f)?0x1f:l) ; i++) {
+ if (isprint ((int) Byte (v, i)))
+ putc (Byte (v, i), f);
+ else
+ putc ('?', f);
+ };
+ fprintf (f, "'");
+ goto displayfields;
+ case Double_tag:
+ fprintf (f, "=float[s%d]=%g", s, Double_val (v));
+ goto displayfields;
+ case Double_array_tag:
+ fprintf (f, "=floatarray[s%d]", s);
+ for (i = 0; i < ((s>0xf)?0xf:s); i++)
+ fprintf (f, " %g", Double_flat_field (v, i));
+ goto displayfields;
+ case Abstract_tag:
+ fprintf (f, "=abstract[s%d]", s);
+ goto displayfields;
+ case Custom_tag:
+ fprintf (f, "=custom[s%d]", s);
+ goto displayfields;
+ default:
+ fprintf (f, "=block<T%d/s%d>", tg, s);
+ displayfields:
+ if (s > 0)
+ fputs ("=(", f);
+ for (i = 0; i < s; i++) {
+ if (i > 20) {
+ fputs ("....", f);
+ break;
+ };
+ if (i > 0)
+ putc (' ', f);
+ fprintf (f, "%#" ARCH_INTNAT_PRINTF_FORMAT "x", Field (v, i));
+ };
+ if (s > 0)
+ putc (')', f);
+ };
+ }
+}
+
+void
+caml_trace_accu_sp_file (value accu, value * sp, code_t prog, int proglen,
+ FILE * f)
+{
+ int i;
+ value *p;
+ fprintf (f, "accu=");
+ caml_trace_value_file (accu, prog, proglen, f);
+ fprintf (f, "\n sp=%#" ARCH_INTNAT_PRINTF_FORMAT "x @%ld:",
+ (intnat) sp, (long) (caml_stack_high - sp));
+ for (p = sp, i = 0; i < 12 + (1 << caml_trace_level) && p < caml_stack_high;
+ p++, i++) {
+ fprintf (f, "\n[%ld] ", (long) (caml_stack_high - p));
+ caml_trace_value_file (*p, prog, proglen, f);
+ };
+ putc ('\n', f);
+ fflush (f);
+}
+
+#endif /* DEBUG */
diff --git a/test/monniaux/ocaml/byterun/intern.c b/test/monniaux/ocaml/byterun/intern.c
new file mode 100644
index 00000000..565ed10d
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/intern.c
@@ -0,0 +1,1048 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Structured input, compact format */
+
+/* The interface of this file is "caml/intext.h" */
+
+#include <string.h>
+#include <stdio.h>
+#include "caml/alloc.h"
+#include "caml/callback.h"
+#include "caml/config.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/gc.h"
+#include "caml/intext.h"
+#include "caml/io.h"
+#include "caml/md5.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/misc.h"
+#include "caml/reverse.h"
+
+static unsigned char * intern_src;
+/* Reading pointer in block holding input data. */
+
+static unsigned char * intern_input = NULL;
+/* Pointer to beginning of block holding input data,
+ if non-NULL this pointer will be freed by the cleanup function. */
+
+static header_t * intern_dest;
+/* Writing pointer in destination block */
+
+static char * intern_extra_block = NULL;
+/* If non-NULL, point to new heap chunk allocated with caml_alloc_for_heap. */
+
+static asize_t obj_counter;
+/* Count how many objects seen so far */
+
+static value * intern_obj_table = NULL;
+/* The pointers to objects already seen */
+
+static unsigned int intern_color;
+/* Color to assign to newly created headers */
+
+static header_t intern_header;
+/* Original header of the destination block.
+ Meaningful only if intern_extra_block is NULL. */
+
+static value intern_block = 0;
+/* Point to the heap block allocated as destination block.
+ Meaningful only if intern_extra_block is NULL. */
+
+static char * intern_resolve_code_pointer(unsigned char digest[16],
+ asize_t offset);
+
+CAMLnoreturn_start
+static void intern_bad_code_pointer(unsigned char digest[16])
+CAMLnoreturn_end;
+
+static void intern_free_stack(void);
+
+static inline unsigned char read8u(void)
+{ return *intern_src++; }
+
+static inline signed char read8s(void)
+{ return *intern_src++; }
+
+static inline uint16_t read16u(void)
+{
+ uint16_t res = (intern_src[0] << 8) + intern_src[1];
+ intern_src += 2;
+ return res;
+}
+
+static inline int16_t read16s(void)
+{
+ int16_t res = (intern_src[0] << 8) + intern_src[1];
+ intern_src += 2;
+ return res;
+}
+
+static inline uint32_t read32u(void)
+{
+ uint32_t res =
+ ((uint32_t)(intern_src[0]) << 24) + (intern_src[1] << 16)
+ + (intern_src[2] << 8) + intern_src[3];
+ intern_src += 4;
+ return res;
+}
+
+static inline int32_t read32s(void)
+{
+ int32_t res =
+ ((uint32_t)(intern_src[0]) << 24) + (intern_src[1] << 16)
+ + (intern_src[2] << 8) + intern_src[3];
+ intern_src += 4;
+ return res;
+}
+
+#ifdef ARCH_SIXTYFOUR
+static uintnat read64u(void)
+{
+ uintnat res =
+ ((uintnat) (intern_src[0]) << 56)
+ + ((uintnat) (intern_src[1]) << 48)
+ + ((uintnat) (intern_src[2]) << 40)
+ + ((uintnat) (intern_src[3]) << 32)
+ + ((uintnat) (intern_src[4]) << 24)
+ + ((uintnat) (intern_src[5]) << 16)
+ + ((uintnat) (intern_src[6]) << 8)
+ + (uintnat) (intern_src[7]);
+ intern_src += 8;
+ return res;
+}
+#endif
+
+static inline void readblock(void * dest, intnat len)
+{
+ memcpy(dest, intern_src, len);
+ intern_src += len;
+}
+
+static void intern_init(void * src, void * input)
+{
+ /* This is asserted at the beginning of demarshaling primitives.
+ If it fails, it probably means that an exception was raised
+ without calling intern_cleanup() during the previous demarshaling. */
+ CAMLassert (intern_input == NULL && intern_obj_table == NULL \
+ && intern_extra_block == NULL && intern_block == 0);
+ intern_src = src;
+ intern_input = input;
+}
+
+static void intern_cleanup(void)
+{
+ if (intern_input != NULL) {
+ caml_stat_free(intern_input);
+ intern_input = NULL;
+ }
+ if (intern_obj_table != NULL) {
+ caml_stat_free(intern_obj_table);
+ intern_obj_table = NULL;
+ }
+ if (intern_extra_block != NULL) {
+ /* free newly allocated heap chunk */
+ caml_free_for_heap(intern_extra_block);
+ intern_extra_block = NULL;
+ } else if (intern_block != 0) {
+ /* restore original header for heap block, otherwise GC is confused */
+ Hd_val(intern_block) = intern_header;
+ intern_block = 0;
+ }
+ /* free the recursion stack */
+ intern_free_stack();
+}
+
+static void readfloat(double * dest, unsigned int code)
+{
+ if (sizeof(double) != 8) {
+ intern_cleanup();
+ caml_invalid_argument("input_value: non-standard floats");
+ }
+ readblock((char *) dest, 8);
+ /* Fix up endianness, if needed */
+#if ARCH_FLOAT_ENDIANNESS == 0x76543210
+ /* Host is big-endian; fix up if data read is little-endian */
+ if (code != CODE_DOUBLE_BIG) Reverse_64(dest, dest);
+#elif ARCH_FLOAT_ENDIANNESS == 0x01234567
+ /* Host is little-endian; fix up if data read is big-endian */
+ if (code != CODE_DOUBLE_LITTLE) Reverse_64(dest, dest);
+#else
+ /* Host is neither big nor little; permute as appropriate */
+ if (code == CODE_DOUBLE_LITTLE)
+ Permute_64(dest, ARCH_FLOAT_ENDIANNESS, dest, 0x01234567)
+ else
+ Permute_64(dest, ARCH_FLOAT_ENDIANNESS, dest, 0x76543210);
+#endif
+}
+
+/* [len] is a number of floats */
+static void readfloats(double * dest, mlsize_t len, unsigned int code)
+{
+ mlsize_t i;
+ if (sizeof(double) != 8) {
+ intern_cleanup();
+ caml_invalid_argument("input_value: non-standard floats");
+ }
+ readblock((char *) dest, len * 8);
+ /* Fix up endianness, if needed */
+#if ARCH_FLOAT_ENDIANNESS == 0x76543210
+ /* Host is big-endian; fix up if data read is little-endian */
+ if (code != CODE_DOUBLE_ARRAY8_BIG &&
+ code != CODE_DOUBLE_ARRAY32_BIG) {
+ for (i = 0; i < len; i++) Reverse_64(dest + i, dest + i);
+ }
+#elif ARCH_FLOAT_ENDIANNESS == 0x01234567
+ /* Host is little-endian; fix up if data read is big-endian */
+ if (code != CODE_DOUBLE_ARRAY8_LITTLE &&
+ code != CODE_DOUBLE_ARRAY32_LITTLE) {
+ for (i = 0; i < len; i++) Reverse_64(dest + i, dest + i);
+ }
+#else
+ /* Host is neither big nor little; permute as appropriate */
+ if (code == CODE_DOUBLE_ARRAY8_LITTLE ||
+ code == CODE_DOUBLE_ARRAY32_LITTLE) {
+ for (i = 0; i < len; i++)
+ Permute_64(dest + i, ARCH_FLOAT_ENDIANNESS, dest + i, 0x01234567);
+ } else {
+ for (i = 0; i < len; i++)
+ Permute_64(dest + i, ARCH_FLOAT_ENDIANNESS, dest + i, 0x76543210);
+ }
+#endif
+}
+
+/* Item on the stack with defined operation */
+struct intern_item {
+ value * dest;
+ intnat arg;
+ enum {
+ OReadItems, /* read arg items and store them in dest[0], dest[1], ... */
+ OFreshOID, /* generate a fresh OID and store it in *dest */
+ OShift /* offset *dest by arg */
+ } op;
+};
+
+/* FIXME: This is duplicated in two other places, with the only difference of
+ the type of elements stored in the stack. Possible solution in C would
+ be to instantiate stack these function via. C preprocessor macro.
+ */
+
+#define INTERN_STACK_INIT_SIZE 256
+#define INTERN_STACK_MAX_SIZE (1024*1024*100)
+
+static struct intern_item intern_stack_init[INTERN_STACK_INIT_SIZE];
+
+static struct intern_item * intern_stack = intern_stack_init;
+static struct intern_item * intern_stack_limit = intern_stack_init
+ + INTERN_STACK_INIT_SIZE;
+
+/* Free the recursion stack if needed */
+static void intern_free_stack(void)
+{
+ if (intern_stack != intern_stack_init) {
+ caml_stat_free(intern_stack);
+ /* Reinitialize the globals for next time around */
+ intern_stack = intern_stack_init;
+ intern_stack_limit = intern_stack + INTERN_STACK_INIT_SIZE;
+ }
+}
+
+/* Same, then raise Out_of_memory */
+static void intern_stack_overflow(void)
+{
+ caml_gc_message (0x04, "Stack overflow in un-marshaling value\n");
+ intern_free_stack();
+ caml_raise_out_of_memory();
+}
+
+static struct intern_item * intern_resize_stack(struct intern_item * sp)
+{
+ asize_t newsize = 2 * (intern_stack_limit - intern_stack);
+ asize_t sp_offset = sp - intern_stack;
+ struct intern_item * newstack;
+
+ if (newsize >= INTERN_STACK_MAX_SIZE) intern_stack_overflow();
+ if (intern_stack == intern_stack_init) {
+ newstack = caml_stat_alloc_noexc(sizeof(struct intern_item) * newsize);
+ if (newstack == NULL) intern_stack_overflow();
+ memcpy(newstack, intern_stack_init,
+ sizeof(struct intern_item) * INTERN_STACK_INIT_SIZE);
+ } else {
+ newstack = caml_stat_resize_noexc(intern_stack,
+ sizeof(struct intern_item) * newsize);
+ if (newstack == NULL) intern_stack_overflow();
+ }
+ intern_stack = newstack;
+ intern_stack_limit = newstack + newsize;
+ return newstack + sp_offset;
+}
+
+/* Convenience macros for requesting operation on the stack */
+#define PushItem() \
+ do { \
+ sp++; \
+ if (sp >= intern_stack_limit) sp = intern_resize_stack(sp); \
+ } while(0)
+
+#define ReadItems(_dest,_n) \
+ do { \
+ if (_n > 0) { \
+ PushItem(); \
+ sp->op = OReadItems; \
+ sp->dest = _dest; \
+ sp->arg = _n; \
+ } \
+ } while(0)
+
+static void intern_rec(value *dest)
+{
+ unsigned int code;
+ tag_t tag;
+ mlsize_t size, len, ofs_ind;
+ value v;
+ asize_t ofs;
+ header_t header;
+ unsigned char digest[16];
+ struct custom_operations * ops;
+ char * codeptr;
+ struct intern_item * sp;
+
+ sp = intern_stack;
+
+ /* Initially let's try to read the first object from the stream */
+ ReadItems(dest, 1);
+
+ /* The un-marshaler loop, the recursion is unrolled */
+ while(sp != intern_stack) {
+
+ /* Interpret next item on the stack */
+ dest = sp->dest;
+ switch (sp->op) {
+ case OFreshOID:
+ /* Refresh the object ID */
+ /* but do not do it for predefined exception slots */
+ if (Long_val(Field((value)dest, 1)) >= 0)
+ caml_set_oo_id((value)dest);
+ /* Pop item and iterate */
+ sp--;
+ break;
+ case OShift:
+ /* Shift value by an offset */
+ *dest += sp->arg;
+ /* Pop item and iterate */
+ sp--;
+ break;
+ case OReadItems:
+ /* Pop item */
+ sp->dest++;
+ if (--(sp->arg) == 0) sp--;
+ /* Read a value and set v to this value */
+ code = read8u();
+ if (code >= PREFIX_SMALL_INT) {
+ if (code >= PREFIX_SMALL_BLOCK) {
+ /* Small block */
+ tag = code & 0xF;
+ size = (code >> 4) & 0x7;
+ read_block:
+ if (size == 0) {
+ v = Atom(tag);
+ } else {
+ v = Val_hp(intern_dest);
+ if (intern_obj_table != NULL) intern_obj_table[obj_counter++] = v;
+ *intern_dest = Make_header_allocated_here(size, tag, intern_color);
+ intern_dest += 1 + size;
+ /* For objects, we need to freshen the oid */
+ if (tag == Object_tag) {
+ CAMLassert(size >= 2);
+ /* Request to read rest of the elements of the block */
+ ReadItems(&Field(v, 2), size - 2);
+ /* Request freshing OID */
+ PushItem();
+ sp->op = OFreshOID;
+ sp->dest = (value*) v;
+ sp->arg = 1;
+ /* Finally read first two block elements: method table and old OID */
+ ReadItems(&Field(v, 0), 2);
+ } else
+ /* If it's not an object then read the contents of the block */
+ ReadItems(&Field(v, 0), size);
+ }
+ } else {
+ /* Small integer */
+ v = Val_int(code & 0x3F);
+ }
+ } else {
+ if (code >= PREFIX_SMALL_STRING) {
+ /* Small string */
+ len = (code & 0x1F);
+ read_string:
+ size = (len + sizeof(value)) / sizeof(value);
+ v = Val_hp(intern_dest);
+ if (intern_obj_table != NULL) intern_obj_table[obj_counter++] = v;
+ *intern_dest = Make_header_allocated_here(size, String_tag, intern_color);
+ intern_dest += 1 + size;
+ Field(v, size - 1) = 0;
+ ofs_ind = Bsize_wsize(size) - 1;
+ Byte(v, ofs_ind) = ofs_ind - len;
+ readblock((char *)String_val(v), len);
+ } else {
+ switch(code) {
+ case CODE_INT8:
+ v = Val_long(read8s());
+ break;
+ case CODE_INT16:
+ v = Val_long(read16s());
+ break;
+ case CODE_INT32:
+ v = Val_long(read32s());
+ break;
+ case CODE_INT64:
+#ifdef ARCH_SIXTYFOUR
+ v = Val_long((intnat) (read64u()));
+ break;
+#else
+ intern_cleanup();
+ caml_failwith("input_value: integer too large");
+ break;
+#endif
+ case CODE_SHARED8:
+ ofs = read8u();
+ read_shared:
+ CAMLassert (ofs > 0);
+ CAMLassert (ofs <= obj_counter);
+ CAMLassert (intern_obj_table != NULL);
+ v = intern_obj_table[obj_counter - ofs];
+ break;
+ case CODE_SHARED16:
+ ofs = read16u();
+ goto read_shared;
+ case CODE_SHARED32:
+ ofs = read32u();
+ goto read_shared;
+#ifdef ARCH_SIXTYFOUR
+ case CODE_SHARED64:
+ ofs = read64u();
+ goto read_shared;
+#endif
+ case CODE_BLOCK32:
+ header = (header_t) read32u();
+ tag = Tag_hd(header);
+ size = Wosize_hd(header);
+ goto read_block;
+#ifdef ARCH_SIXTYFOUR
+ case CODE_BLOCK64:
+ header = (header_t) read64u();
+ tag = Tag_hd(header);
+ size = Wosize_hd(header);
+ goto read_block;
+#endif
+ case CODE_STRING8:
+ len = read8u();
+ goto read_string;
+ case CODE_STRING32:
+ len = read32u();
+ goto read_string;
+#ifdef ARCH_SIXTYFOUR
+ case CODE_STRING64:
+ len = read64u();
+ goto read_string;
+#endif
+ case CODE_DOUBLE_LITTLE:
+ case CODE_DOUBLE_BIG:
+ v = Val_hp(intern_dest);
+ if (intern_obj_table != NULL) intern_obj_table[obj_counter++] = v;
+ *intern_dest = Make_header_allocated_here(Double_wosize, Double_tag,
+ intern_color);
+ intern_dest += 1 + Double_wosize;
+ readfloat((double *) v, code);
+ break;
+ case CODE_DOUBLE_ARRAY8_LITTLE:
+ case CODE_DOUBLE_ARRAY8_BIG:
+ len = read8u();
+ read_double_array:
+ size = len * Double_wosize;
+ v = Val_hp(intern_dest);
+ if (intern_obj_table != NULL) intern_obj_table[obj_counter++] = v;
+ *intern_dest = Make_header_allocated_here(size, Double_array_tag,
+ intern_color);
+ intern_dest += 1 + size;
+ readfloats((double *) v, len, code);
+ break;
+ case CODE_DOUBLE_ARRAY32_LITTLE:
+ case CODE_DOUBLE_ARRAY32_BIG:
+ len = read32u();
+ goto read_double_array;
+#ifdef ARCH_SIXTYFOUR
+ case CODE_DOUBLE_ARRAY64_LITTLE:
+ case CODE_DOUBLE_ARRAY64_BIG:
+ len = read64u();
+ goto read_double_array;
+#endif
+ case CODE_CODEPOINTER:
+ ofs = read32u();
+ readblock(digest, 16);
+ codeptr = intern_resolve_code_pointer(digest, ofs);
+ if (codeptr != NULL) {
+ v = (value) codeptr;
+ } else {
+ value * function_placeholder =
+ caml_named_value ("Debugger.function_placeholder");
+ if (function_placeholder != NULL) {
+ v = *function_placeholder;
+ } else {
+ intern_cleanup();
+ intern_bad_code_pointer(digest);
+ }
+ }
+ break;
+ case CODE_INFIXPOINTER:
+ ofs = read32u();
+ /* Read a value to *dest, then offset *dest by ofs */
+ PushItem();
+ sp->dest = dest;
+ sp->op = OShift;
+ sp->arg = ofs;
+ ReadItems(dest, 1);
+ continue; /* with next iteration of main loop, skipping *dest = v */
+ case CODE_CUSTOM:
+ ops = caml_find_custom_operations((char *) intern_src);
+ if (ops == NULL) {
+ intern_cleanup();
+ caml_failwith("input_value: unknown custom block identifier");
+ }
+ while (*intern_src++ != 0) /*nothing*/; /*skip identifier*/
+ size = ops->deserialize((void *) (intern_dest + 2));
+ size = 1 + (size + sizeof(value) - 1) / sizeof(value);
+ v = Val_hp(intern_dest);
+ if (intern_obj_table != NULL) intern_obj_table[obj_counter++] = v;
+ *intern_dest = Make_header_allocated_here(size, Custom_tag,
+ intern_color);
+ Custom_ops_val(v) = ops;
+
+ if (ops->finalize != NULL && Is_young(v)) {
+ /* Remember that the block has a finalizer. */
+ add_to_custom_table (&caml_custom_table, v, 0, 1);
+ }
+
+ intern_dest += 1 + size;
+ break;
+ default:
+ intern_cleanup();
+ caml_failwith("input_value: ill-formed message");
+ }
+ }
+ }
+ /* end of case OReadItems */
+ *dest = v;
+ break;
+ default:
+ CAMLassert(0);
+ }
+ }
+ /* We are done. Cleanup the stack and leave the function */
+ intern_free_stack();
+}
+
+static void intern_alloc(mlsize_t whsize, mlsize_t num_objects,
+ int outside_heap)
+{
+ mlsize_t wosize;
+
+ if (whsize == 0) {
+ CAMLassert (intern_extra_block == NULL && intern_block == 0
+ && intern_obj_table == NULL);
+ return;
+ }
+ wosize = Wosize_whsize(whsize);
+ if (outside_heap || wosize > Max_wosize) {
+ /* Round desired size up to next page */
+ asize_t request =
+ ((Bsize_wsize(whsize) + Page_size - 1) >> Page_log) << Page_log;
+ intern_extra_block = caml_alloc_for_heap(request);
+ if (intern_extra_block == NULL) {
+ intern_cleanup();
+ caml_raise_out_of_memory();
+ }
+ intern_color =
+ outside_heap ? Caml_black : caml_allocation_color(intern_extra_block);
+ intern_dest = (header_t *) intern_extra_block;
+ CAMLassert (intern_block == 0);
+ } else {
+ /* this is a specialised version of caml_alloc from alloc.c */
+ if (wosize <= Max_young_wosize){
+ if (wosize == 0){
+ intern_block = Atom (String_tag);
+ } else {
+ intern_block = caml_alloc_small (wosize, String_tag);
+ }
+ }else{
+ intern_block = caml_alloc_shr_no_raise (wosize, String_tag);
+ /* do not do the urgent_gc check here because it might darken
+ intern_block into gray and break the intern_color assertion below */
+ if (intern_block == 0) {
+ intern_cleanup();
+ caml_raise_out_of_memory();
+ }
+ }
+ intern_header = Hd_val(intern_block);
+ intern_color = Color_hd(intern_header);
+ CAMLassert (intern_color == Caml_white || intern_color == Caml_black);
+ intern_dest = (header_t *) Hp_val(intern_block);
+ CAMLassert (intern_extra_block == NULL);
+ }
+ obj_counter = 0;
+ if (num_objects > 0) {
+ intern_obj_table = (value *) caml_stat_alloc_noexc(num_objects * sizeof(value));
+ if (intern_obj_table == NULL) {
+ intern_cleanup();
+ caml_raise_out_of_memory();
+ }
+ } else
+ CAMLassert(intern_obj_table == NULL);
+}
+
+static void intern_add_to_heap(mlsize_t whsize)
+{
+ /* Add new heap chunk to heap if needed */
+ if (intern_extra_block != NULL) {
+ /* If heap chunk not filled totally, build free block at end */
+ asize_t request = Chunk_size (intern_extra_block);
+ header_t * end_extra_block =
+ (header_t *) intern_extra_block + Wsize_bsize(request);
+ CAMLassert(intern_block == 0);
+ CAMLassert(intern_dest <= end_extra_block);
+ if (intern_dest < end_extra_block){
+ caml_make_free_blocks ((value *) intern_dest,
+ end_extra_block - intern_dest, 0, Caml_white);
+ }
+ caml_allocated_words +=
+ Wsize_bsize ((char *) intern_dest - intern_extra_block);
+ caml_add_to_heap(intern_extra_block);
+ intern_extra_block = NULL; // To prevent intern_cleanup freeing it
+ } else {
+ intern_block = 0; // To prevent intern_cleanup rewriting its header
+ }
+}
+
+/* Parsing the header */
+
+struct marshal_header {
+ uint32_t magic;
+ int header_len;
+ uintnat data_len;
+ uintnat num_objects;
+ uintnat whsize;
+};
+
+static void caml_parse_header(char * fun_name,
+ /*out*/ struct marshal_header * h)
+{
+ char errmsg[100];
+
+ h->magic = read32u();
+ switch(h->magic) {
+ case Intext_magic_number_small:
+ h->header_len = 20;
+ h->data_len = read32u();
+ h->num_objects = read32u();
+#ifdef ARCH_SIXTYFOUR
+ read32u();
+ h->whsize = read32u();
+#else
+ h->whsize = read32u();
+ read32u();
+#endif
+ break;
+ case Intext_magic_number_big:
+#ifdef ARCH_SIXTYFOUR
+ h->header_len = 32;
+ read32u();
+ h->data_len = read64u();
+ h->num_objects = read64u();
+ h->whsize = read64u();
+#else
+ errmsg[sizeof(errmsg) - 1] = 0;
+ snprintf(errmsg, sizeof(errmsg) - 1,
+ "%s: object too large to be read back on a 32-bit platform",
+ fun_name);
+ caml_failwith(errmsg);
+#endif
+ break;
+ default:
+ errmsg[sizeof(errmsg) - 1] = 0;
+ snprintf(errmsg, sizeof(errmsg) - 1,
+ "%s: bad object",
+ fun_name);
+ caml_failwith(errmsg);
+ }
+}
+
+/* Reading from a channel */
+
+static value caml_input_val_core(struct channel *chan, int outside_heap)
+{
+ intnat r;
+ char header[32];
+ struct marshal_header h;
+ char * block;
+ value res;
+
+ if (! caml_channel_binary_mode(chan))
+ caml_failwith("input_value: not a binary channel");
+ /* Read and parse the header */
+ r = caml_really_getblock(chan, header, 20);
+ if (r == 0)
+ caml_raise_end_of_file();
+ else if (r < 20)
+ caml_failwith("input_value: truncated object");
+ intern_src = (unsigned char *) header;
+ if (read32u() == Intext_magic_number_big) {
+ /* Finish reading the header */
+ if (caml_really_getblock(chan, header + 20, 32 - 20) < 32 - 20)
+ caml_failwith("input_value: truncated object");
+ }
+ intern_src = (unsigned char *) header;
+ caml_parse_header("input_value", &h);
+ /* Read block from channel */
+ block = caml_stat_alloc(h.data_len);
+ /* During [caml_really_getblock], concurrent [caml_input_val] operations
+ can take place (via signal handlers or context switching in systhreads),
+ and [intern_input] may change. So, wait until [caml_really_getblock]
+ is over before using [intern_input] and the other global vars. */
+ if (caml_really_getblock(chan, block, h.data_len) < h.data_len) {
+ caml_stat_free(block);
+ caml_failwith("input_value: truncated object");
+ }
+ /* Initialize global state */
+ intern_init(block, block);
+ intern_alloc(h.whsize, h.num_objects, outside_heap);
+ /* Fill it in */
+ intern_rec(&res);
+ if (!outside_heap) {
+ intern_add_to_heap(h.whsize);
+ } else {
+ caml_disown_for_heap(intern_extra_block);
+ intern_extra_block = NULL;
+ intern_block = 0;
+ }
+ /* Free everything */
+ intern_cleanup();
+ return caml_check_urgent_gc(res);
+}
+
+value caml_input_val(struct channel* chan)
+{
+ return caml_input_val_core(chan, 0);
+}
+
+CAMLprim value caml_input_value(value vchan)
+{
+ CAMLparam1 (vchan);
+ struct channel * chan = Channel(vchan);
+ CAMLlocal1 (res);
+
+ Lock(chan);
+ res = caml_input_val(chan);
+ Unlock(chan);
+ CAMLreturn (res);
+}
+
+/* Reading from memory-resident blocks */
+
+CAMLprim value caml_input_value_to_outside_heap(value vchan)
+{
+ CAMLparam1 (vchan);
+ struct channel * chan = Channel(vchan);
+ CAMLlocal1 (res);
+
+ Lock(chan);
+ res = caml_input_val_core(chan, 1);
+ Unlock(chan);
+ CAMLreturn (res);
+}
+
+CAMLexport value caml_input_val_from_bytes(value str, intnat ofs)
+{
+ CAMLparam1 (str);
+ CAMLlocal1 (obj);
+ struct marshal_header h;
+
+ /* Initialize global state */
+ intern_init(&Byte_u(str, ofs), NULL);
+ caml_parse_header("input_val_from_string", &h);
+ if (ofs + h.header_len + h.data_len > caml_string_length(str))
+ caml_failwith("input_val_from_string: bad length");
+ /* Allocate result */
+ intern_alloc(h.whsize, h.num_objects, 0);
+ intern_src = &Byte_u(str, ofs + h.header_len); /* If a GC occurred */
+ /* Fill it in */
+ intern_rec(&obj);
+ intern_add_to_heap(h.whsize);
+ /* Free everything */
+ intern_cleanup();
+ CAMLreturn (caml_check_urgent_gc(obj));
+}
+
+CAMLprim value caml_input_value_from_string(value str, value ofs)
+{
+ return caml_input_val_from_bytes(str, Long_val(ofs));
+}
+
+CAMLprim value caml_input_value_from_bytes(value str, value ofs)
+{
+ return caml_input_val_from_bytes(str, Long_val(ofs));
+}
+
+static value input_val_from_block(struct marshal_header * h)
+{
+ value obj;
+ /* Allocate result */
+ intern_alloc(h->whsize, h->num_objects, 0);
+ /* Fill it in */
+ intern_rec(&obj);
+ intern_add_to_heap(h->whsize);
+ /* Free internal data structures */
+ intern_cleanup();
+ return caml_check_urgent_gc(obj);
+}
+
+CAMLexport value caml_input_value_from_malloc(char * data, intnat ofs)
+{
+ struct marshal_header h;
+
+ intern_init(data + ofs, data);
+
+ caml_parse_header("input_value_from_malloc", &h);
+
+ return input_val_from_block(&h);
+}
+
+/* [len] is a number of bytes */
+CAMLexport value caml_input_value_from_block(char * data, intnat len)
+{
+ struct marshal_header h;
+
+ /* Initialize global state */
+ intern_init(data, NULL);
+ caml_parse_header("input_value_from_block", &h);
+ if (h.header_len + h.data_len > len)
+ caml_failwith("input_val_from_block: bad length");
+ return input_val_from_block(&h);
+}
+
+/* [ofs] is a [value] that represents a number of bytes
+ result is a [value] that represents a number of bytes
+ To handle both the small and the big format,
+ we assume 20 bytes are available at [buff + ofs],
+ and we return the data size + the length of the part of the header
+ that remains to be read. */
+
+CAMLprim value caml_marshal_data_size(value buff, value ofs)
+{
+ uint32_t magic;
+ int header_len;
+ uintnat data_len;
+
+ intern_src = &Byte_u(buff, Long_val(ofs));
+ magic = read32u();
+ switch(magic) {
+ case Intext_magic_number_small:
+ header_len = 20;
+ data_len = read32u();
+ break;
+ case Intext_magic_number_big:
+#ifdef ARCH_SIXTYFOUR
+ header_len = 32;
+ read32u();
+ data_len = read64u();
+#else
+ caml_failwith("Marshal.data_size: "
+ "object too large to be read back on a 32-bit platform");
+#endif
+ break;
+ default:
+ caml_failwith("Marshal.data_size: bad object");
+ }
+ return Val_long((header_len - 20) + data_len);
+}
+
+/* Resolution of code pointers */
+
+static char * intern_resolve_code_pointer(unsigned char digest[16],
+ asize_t offset)
+{
+ int i;
+ for (i = caml_code_fragments_table.size - 1; i >= 0; i--) {
+ struct code_fragment * cf = caml_code_fragments_table.contents[i];
+ if (! cf->digest_computed) {
+ caml_md5_block(cf->digest, cf->code_start, cf->code_end - cf->code_start);
+ cf->digest_computed = 1;
+ }
+ if (memcmp(digest, cf->digest, 16) == 0) {
+ if (cf->code_start + offset < cf->code_end)
+ return cf->code_start + offset;
+ else
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+static void intern_bad_code_pointer(unsigned char digest[16])
+{
+ char msg[256];
+ snprintf(msg, sizeof(msg),
+ "input_value: unknown code module "
+ "%02X%02X%02X%02X%02X%02X%02X%02X"
+ "%02X%02X%02X%02X%02X%02X%02X%02X",
+ digest[0], digest[1], digest[2], digest[3],
+ digest[4], digest[5], digest[6], digest[7],
+ digest[8], digest[9], digest[10], digest[11],
+ digest[12], digest[13], digest[14], digest[15]);
+ caml_failwith(msg);
+}
+
+/* Functions for writing user-defined marshallers */
+
+CAMLexport int caml_deserialize_uint_1(void)
+{
+ return read8u();
+}
+
+CAMLexport int caml_deserialize_sint_1(void)
+{
+ return read8s();
+}
+
+CAMLexport int caml_deserialize_uint_2(void)
+{
+ return read16u();
+}
+
+CAMLexport int caml_deserialize_sint_2(void)
+{
+ return read16s();
+}
+
+CAMLexport uint32_t caml_deserialize_uint_4(void)
+{
+ return read32u();
+}
+
+CAMLexport int32_t caml_deserialize_sint_4(void)
+{
+ return read32s();
+}
+
+CAMLexport uint64_t caml_deserialize_uint_8(void)
+{
+ uint64_t i;
+ caml_deserialize_block_8(&i, 1);
+ return i;
+}
+
+CAMLexport int64_t caml_deserialize_sint_8(void)
+{
+ int64_t i;
+ caml_deserialize_block_8(&i, 1);
+ return i;
+}
+
+CAMLexport float caml_deserialize_float_4(void)
+{
+ float f;
+ caml_deserialize_block_4(&f, 1);
+ return f;
+}
+
+CAMLexport double caml_deserialize_float_8(void)
+{
+ double f;
+ caml_deserialize_block_float_8(&f, 1);
+ return f;
+}
+
+CAMLexport void caml_deserialize_block_1(void * data, intnat len)
+{
+ memcpy(data, intern_src, len);
+ intern_src += len;
+}
+
+CAMLexport void caml_deserialize_block_2(void * data, intnat len)
+{
+#ifndef ARCH_BIG_ENDIAN
+ unsigned char * p, * q;
+ for (p = intern_src, q = data; len > 0; len--, p += 2, q += 2)
+ Reverse_16(q, p);
+ intern_src = p;
+#else
+ memcpy(data, intern_src, len * 2);
+ intern_src += len * 2;
+#endif
+}
+
+CAMLexport void caml_deserialize_block_4(void * data, intnat len)
+{
+#ifndef ARCH_BIG_ENDIAN
+ unsigned char * p, * q;
+ for (p = intern_src, q = data; len > 0; len--, p += 4, q += 4)
+ Reverse_32(q, p);
+ intern_src = p;
+#else
+ memcpy(data, intern_src, len * 4);
+ intern_src += len * 4;
+#endif
+}
+
+CAMLexport void caml_deserialize_block_8(void * data, intnat len)
+{
+#ifndef ARCH_BIG_ENDIAN
+ unsigned char * p, * q;
+ for (p = intern_src, q = data; len > 0; len--, p += 8, q += 8)
+ Reverse_64(q, p);
+ intern_src = p;
+#else
+ memcpy(data, intern_src, len * 8);
+ intern_src += len * 8;
+#endif
+}
+
+CAMLexport void caml_deserialize_block_float_8(void * data, intnat len)
+{
+#if ARCH_FLOAT_ENDIANNESS == 0x01234567
+ memcpy(data, intern_src, len * 8);
+ intern_src += len * 8;
+#elif ARCH_FLOAT_ENDIANNESS == 0x76543210
+ unsigned char * p, * q;
+ for (p = intern_src, q = data; len > 0; len--, p += 8, q += 8)
+ Reverse_64(q, p);
+ intern_src = p;
+#else
+ unsigned char * p, * q;
+ for (p = intern_src, q = data; len > 0; len--, p += 8, q += 8)
+ Permute_64(q, ARCH_FLOAT_ENDIANNESS, p, 0x01234567);
+ intern_src = p;
+#endif
+}
+
+CAMLexport void caml_deserialize_error(char * msg)
+{
+ intern_cleanup();
+ caml_failwith(msg);
+}
diff --git a/test/monniaux/ocaml/byterun/interp.c b/test/monniaux/ocaml/byterun/interp.c
new file mode 100644
index 00000000..0b74df3d
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/interp.c
@@ -0,0 +1,1172 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* The bytecode interpreter */
+#include <stdio.h>
+#include "caml/alloc.h"
+#include "caml/backtrace.h"
+#include "caml/callback.h"
+#include "caml/debugger.h"
+#include "caml/fail.h"
+#include "caml/fix_code.h"
+#include "caml/instrtrace.h"
+#include "caml/instruct.h"
+#include "caml/interp.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/prims.h"
+#include "caml/signals.h"
+#include "caml/stacks.h"
+#include "caml/startup_aux.h"
+
+/* Registers for the abstract machine:
+ pc the code pointer
+ sp the stack pointer (grows downward)
+ accu the accumulator
+ env heap-allocated environment
+ caml_trapsp pointer to the current trap frame
+ extra_args number of extra arguments provided by the caller
+
+sp is a local copy of the global variable caml_extern_sp. */
+
+/* Instruction decoding */
+
+#ifdef THREADED_CODE
+# define Instruct(name) lbl_##name
+# if defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
+# define Jumptbl_base ((char *) &&lbl_ACC0)
+# else
+# define Jumptbl_base ((char *) 0)
+# define jumptbl_base ((char *) 0)
+# endif
+# ifdef DEBUG
+# define Next goto next_instr
+# else
+# define Next goto *(void *)(jumptbl_base + *pc++)
+# endif
+#else
+# define Instruct(name) case name
+# define Next break
+#endif
+
+/* GC interface */
+
+#define Setup_for_gc \
+ { sp -= 2; sp[0] = accu; sp[1] = env; caml_extern_sp = sp; }
+#define Restore_after_gc \
+ { accu = sp[0]; env = sp[1]; sp += 2; }
+#define Setup_for_c_call \
+ { saved_pc = pc; *--sp = env; caml_extern_sp = sp; }
+#define Restore_after_c_call \
+ { sp = caml_extern_sp; env = *sp++; saved_pc = NULL; }
+
+/* An event frame must look like accu + a C_CALL frame + a RETURN 1 frame */
+#define Setup_for_event \
+ { sp -= 6; \
+ sp[0] = accu; /* accu */ \
+ sp[1] = Val_unit; /* C_CALL frame: dummy environment */ \
+ sp[2] = Val_unit; /* RETURN frame: dummy local 0 */ \
+ sp[3] = (value) pc; /* RETURN frame: saved return address */ \
+ sp[4] = env; /* RETURN frame: saved environment */ \
+ sp[5] = Val_long(extra_args); /* RETURN frame: saved extra args */ \
+ caml_extern_sp = sp; }
+#define Restore_after_event \
+ { sp = caml_extern_sp; accu = sp[0]; \
+ pc = (code_t) sp[3]; env = sp[4]; extra_args = Long_val(sp[5]); \
+ sp += 6; }
+
+/* Debugger interface */
+
+#define Setup_for_debugger \
+ { sp -= 4; \
+ sp[0] = accu; sp[1] = (value)(pc - 1); \
+ sp[2] = env; sp[3] = Val_long(extra_args); \
+ caml_extern_sp = sp; }
+#define Restore_after_debugger { sp += 4; }
+
+#ifdef THREADED_CODE
+#define Restart_curr_instr \
+ goto *(jumptable[caml_saved_code[pc - 1 - caml_start_code]])
+#else
+#define Restart_curr_instr \
+ curr_instr = caml_saved_code[pc - 1 - caml_start_code]; \
+ goto dispatch_instr
+#endif
+
+/* Register optimization.
+ Some compilers underestimate the use of the local variables representing
+ the abstract machine registers, and don't put them in hardware registers,
+ which slows down the interpreter considerably.
+ For GCC, I have hand-assigned hardware registers for several architectures.
+*/
+
+#if defined(__GNUC__) && !defined(DEBUG) && !defined(__INTEL_COMPILER) \
+ && !defined(__llvm__)
+#ifdef __mips__
+#define PC_REG asm("$16")
+#define SP_REG asm("$17")
+#define ACCU_REG asm("$18")
+#endif
+#ifdef __sparc__
+#define PC_REG asm("%l0")
+#define SP_REG asm("%l1")
+#define ACCU_REG asm("%l2")
+#endif
+#ifdef __alpha__
+#ifdef __CRAY__
+#define PC_REG asm("r9")
+#define SP_REG asm("r10")
+#define ACCU_REG asm("r11")
+#define JUMPTBL_BASE_REG asm("r12")
+#else
+#define PC_REG asm("$9")
+#define SP_REG asm("$10")
+#define ACCU_REG asm("$11")
+#define JUMPTBL_BASE_REG asm("$12")
+#endif
+#endif
+#ifdef __i386__
+#define PC_REG asm("%esi")
+#define SP_REG asm("%edi")
+#define ACCU_REG
+#endif
+#if defined(__ppc__) || defined(__ppc64__)
+#define PC_REG asm("26")
+#define SP_REG asm("27")
+#define ACCU_REG asm("28")
+#endif
+#ifdef __hppa__
+#define PC_REG asm("%r18")
+#define SP_REG asm("%r17")
+#define ACCU_REG asm("%r16")
+#endif
+#ifdef __mc68000__
+#define PC_REG asm("a5")
+#define SP_REG asm("a4")
+#define ACCU_REG asm("d7")
+#endif
+/* PR#4953: these specific registers not available in Thumb mode */
+#if defined (__arm__) && !defined(__thumb__)
+#define PC_REG asm("r6")
+#define SP_REG asm("r8")
+#define ACCU_REG asm("r7")
+#endif
+#ifdef __ia64__
+#define PC_REG asm("36")
+#define SP_REG asm("37")
+#define ACCU_REG asm("38")
+#define JUMPTBL_BASE_REG asm("39")
+#endif
+#ifdef __x86_64__
+#define PC_REG asm("%r15")
+#define SP_REG asm("%r14")
+#define ACCU_REG asm("%r13")
+#endif
+#ifdef __aarch64__
+#define PC_REG asm("%x19")
+#define SP_REG asm("%x20")
+#define ACCU_REG asm("%x21")
+#define JUMPTBL_BASE_REG asm("%x22")
+#endif
+#endif
+
+#ifdef DEBUG
+static intnat caml_bcodcount;
+#endif
+
+/* The interpreter itself */
+
+value caml_interprete(code_t prog, asize_t prog_size)
+{
+#ifdef PC_REG
+ register code_t pc PC_REG;
+ register value * sp SP_REG;
+ register value accu ACCU_REG;
+#else
+ register code_t pc;
+ register value * sp;
+ register value accu;
+#endif
+#if defined(THREADED_CODE) && defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
+#ifdef JUMPTBL_BASE_REG
+ register char * jumptbl_base JUMPTBL_BASE_REG;
+#else
+ register char * jumptbl_base;
+#endif
+#endif
+ value env;
+ intnat extra_args;
+ struct longjmp_buffer * initial_external_raise;
+ int initial_sp_offset;
+ /* volatile ensures that initial_local_roots and saved_pc
+ will keep correct value across longjmp */
+ struct caml__roots_block * volatile initial_local_roots;
+ volatile code_t saved_pc = NULL;
+ struct longjmp_buffer raise_buf;
+#ifndef THREADED_CODE
+ opcode_t curr_instr;
+#endif
+
+#ifdef THREADED_CODE
+ static void * jumptable[] = {
+# include "caml/jumptbl.h"
+ };
+#endif
+
+ if (prog == NULL) { /* Interpreter is initializing */
+#ifdef THREADED_CODE
+ caml_instr_table = (char **) jumptable;
+ caml_instr_base = Jumptbl_base;
+#endif
+ return Val_unit;
+ }
+
+#if defined(THREADED_CODE) && defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
+ jumptbl_base = Jumptbl_base;
+#endif
+ initial_local_roots = caml_local_roots;
+ initial_sp_offset = (char *) caml_stack_high - (char *) caml_extern_sp;
+ initial_external_raise = caml_external_raise;
+ caml_callback_depth++;
+ saved_pc = NULL;
+
+ if (sigsetjmp(raise_buf.buf, 0)) {
+ caml_local_roots = initial_local_roots;
+ sp = caml_extern_sp;
+ accu = caml_exn_bucket;
+ pc = saved_pc; saved_pc = NULL;
+ if (pc != NULL) pc += 2;
+ /* +2 adjustement for the sole purpose of backtraces */
+ goto raise_exception;
+ }
+ caml_external_raise = &raise_buf;
+
+ sp = caml_extern_sp;
+ pc = prog;
+ extra_args = 0;
+ env = Atom(0);
+ accu = Val_int(0);
+
+#ifdef THREADED_CODE
+#ifdef DEBUG
+ next_instr:
+ if (caml_icount-- == 0) caml_stop_here ();
+ CAMLassert(sp >= caml_stack_low);
+ CAMLassert(sp <= caml_stack_high);
+#endif
+ goto *(void *)(jumptbl_base + *pc++); /* Jump to the first instruction */
+#else
+ while(1) {
+#ifdef DEBUG
+ caml_bcodcount++;
+ if (caml_icount-- == 0) caml_stop_here ();
+ if (caml_trace_level>1) printf("\n##%" ARCH_INTNAT_PRINTF_FORMAT "d\n",
+ caml_bcodcount);
+ if (caml_trace_level>0) caml_disasm_instr(pc);
+ if (caml_trace_level>1) {
+ printf("env=");
+ caml_trace_value_file(env,prog,prog_size,stdout);
+ putchar('\n');
+ caml_trace_accu_sp_file(accu,sp,prog,prog_size,stdout);
+ fflush(stdout);
+ };
+ CAMLassert(sp >= caml_stack_low);
+ CAMLassert(sp <= caml_stack_high);
+#endif
+ curr_instr = *pc++;
+
+ dispatch_instr:
+ switch(curr_instr) {
+#endif
+
+/* Basic stack operations */
+
+ Instruct(ACC0):
+ accu = sp[0]; Next;
+ Instruct(ACC1):
+ accu = sp[1]; Next;
+ Instruct(ACC2):
+ accu = sp[2]; Next;
+ Instruct(ACC3):
+ accu = sp[3]; Next;
+ Instruct(ACC4):
+ accu = sp[4]; Next;
+ Instruct(ACC5):
+ accu = sp[5]; Next;
+ Instruct(ACC6):
+ accu = sp[6]; Next;
+ Instruct(ACC7):
+ accu = sp[7]; Next;
+
+ Instruct(PUSH): Instruct(PUSHACC0):
+ *--sp = accu; Next;
+ Instruct(PUSHACC1):
+ *--sp = accu; accu = sp[1]; Next;
+ Instruct(PUSHACC2):
+ *--sp = accu; accu = sp[2]; Next;
+ Instruct(PUSHACC3):
+ *--sp = accu; accu = sp[3]; Next;
+ Instruct(PUSHACC4):
+ *--sp = accu; accu = sp[4]; Next;
+ Instruct(PUSHACC5):
+ *--sp = accu; accu = sp[5]; Next;
+ Instruct(PUSHACC6):
+ *--sp = accu; accu = sp[6]; Next;
+ Instruct(PUSHACC7):
+ *--sp = accu; accu = sp[7]; Next;
+
+ Instruct(PUSHACC):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(ACC):
+ accu = sp[*pc++];
+ Next;
+
+ Instruct(POP):
+ sp += *pc++;
+ Next;
+ Instruct(ASSIGN):
+ sp[*pc++] = accu;
+ accu = Val_unit;
+ Next;
+
+/* Access in heap-allocated environment */
+
+ Instruct(ENVACC1):
+ accu = Field(env, 1); Next;
+ Instruct(ENVACC2):
+ accu = Field(env, 2); Next;
+ Instruct(ENVACC3):
+ accu = Field(env, 3); Next;
+ Instruct(ENVACC4):
+ accu = Field(env, 4); Next;
+
+ Instruct(PUSHENVACC1):
+ *--sp = accu; accu = Field(env, 1); Next;
+ Instruct(PUSHENVACC2):
+ *--sp = accu; accu = Field(env, 2); Next;
+ Instruct(PUSHENVACC3):
+ *--sp = accu; accu = Field(env, 3); Next;
+ Instruct(PUSHENVACC4):
+ *--sp = accu; accu = Field(env, 4); Next;
+
+ Instruct(PUSHENVACC):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(ENVACC):
+ accu = Field(env, *pc++);
+ Next;
+
+/* Function application */
+
+ Instruct(PUSH_RETADDR): {
+ sp -= 3;
+ sp[0] = (value) (pc + *pc);
+ sp[1] = env;
+ sp[2] = Val_long(extra_args);
+ pc++;
+ Next;
+ }
+ Instruct(APPLY): {
+ extra_args = *pc - 1;
+ pc = Code_val(accu);
+ env = accu;
+ goto check_stacks;
+ }
+ Instruct(APPLY1): {
+ value arg1 = sp[0];
+ sp -= 3;
+ sp[0] = arg1;
+ sp[1] = (value)pc;
+ sp[2] = env;
+ sp[3] = Val_long(extra_args);
+ pc = Code_val(accu);
+ env = accu;
+ extra_args = 0;
+ goto check_stacks;
+ }
+ Instruct(APPLY2): {
+ value arg1 = sp[0];
+ value arg2 = sp[1];
+ sp -= 3;
+ sp[0] = arg1;
+ sp[1] = arg2;
+ sp[2] = (value)pc;
+ sp[3] = env;
+ sp[4] = Val_long(extra_args);
+ pc = Code_val(accu);
+ env = accu;
+ extra_args = 1;
+ goto check_stacks;
+ }
+ Instruct(APPLY3): {
+ value arg1 = sp[0];
+ value arg2 = sp[1];
+ value arg3 = sp[2];
+ sp -= 3;
+ sp[0] = arg1;
+ sp[1] = arg2;
+ sp[2] = arg3;
+ sp[3] = (value)pc;
+ sp[4] = env;
+ sp[5] = Val_long(extra_args);
+ pc = Code_val(accu);
+ env = accu;
+ extra_args = 2;
+ goto check_stacks;
+ }
+
+ Instruct(APPTERM): {
+ int nargs = *pc++;
+ int slotsize = *pc;
+ value * newsp;
+ int i;
+ /* Slide the nargs bottom words of the current frame to the top
+ of the frame, and discard the remainder of the frame */
+ newsp = sp + slotsize - nargs;
+ for (i = nargs - 1; i >= 0; i--) newsp[i] = sp[i];
+ sp = newsp;
+ pc = Code_val(accu);
+ env = accu;
+ extra_args += nargs - 1;
+ goto check_stacks;
+ }
+ Instruct(APPTERM1): {
+ value arg1 = sp[0];
+ sp = sp + *pc - 1;
+ sp[0] = arg1;
+ pc = Code_val(accu);
+ env = accu;
+ goto check_stacks;
+ }
+ Instruct(APPTERM2): {
+ value arg1 = sp[0];
+ value arg2 = sp[1];
+ sp = sp + *pc - 2;
+ sp[0] = arg1;
+ sp[1] = arg2;
+ pc = Code_val(accu);
+ env = accu;
+ extra_args += 1;
+ goto check_stacks;
+ }
+ Instruct(APPTERM3): {
+ value arg1 = sp[0];
+ value arg2 = sp[1];
+ value arg3 = sp[2];
+ sp = sp + *pc - 3;
+ sp[0] = arg1;
+ sp[1] = arg2;
+ sp[2] = arg3;
+ pc = Code_val(accu);
+ env = accu;
+ extra_args += 2;
+ goto check_stacks;
+ }
+
+ Instruct(RETURN): {
+ sp += *pc++;
+ if (extra_args > 0) {
+ extra_args--;
+ pc = Code_val(accu);
+ env = accu;
+ } else {
+ pc = (code_t)(sp[0]);
+ env = sp[1];
+ extra_args = Long_val(sp[2]);
+ sp += 3;
+ }
+ Next;
+ }
+
+ Instruct(RESTART): {
+ int num_args = Wosize_val(env) - 2;
+ int i;
+ sp -= num_args;
+ for (i = 0; i < num_args; i++) sp[i] = Field(env, i + 2);
+ env = Field(env, 1);
+ extra_args += num_args;
+ Next;
+ }
+
+ Instruct(GRAB): {
+ int required = *pc++;
+ if (extra_args >= required) {
+ extra_args -= required;
+ } else {
+ mlsize_t num_args, i;
+ num_args = 1 + extra_args; /* arg1 + extra args */
+ Alloc_small(accu, num_args + 2, Closure_tag);
+ Field(accu, 1) = env;
+ for (i = 0; i < num_args; i++) Field(accu, i + 2) = sp[i];
+ Code_val(accu) = pc - 3; /* Point to the preceding RESTART instr. */
+ sp += num_args;
+ pc = (code_t)(sp[0]);
+ env = sp[1];
+ extra_args = Long_val(sp[2]);
+ sp += 3;
+ }
+ Next;
+ }
+
+ Instruct(CLOSURE): {
+ int nvars = *pc++;
+ int i;
+ if (nvars > 0) *--sp = accu;
+ if (nvars < Max_young_wosize) {
+ /* nvars + 1 <= Max_young_wosize, can allocate in minor heap */
+ Alloc_small(accu, 1 + nvars, Closure_tag);
+ for (i = 0; i < nvars; i++) Field(accu, i + 1) = sp[i];
+ } else {
+ /* PR#6385: must allocate in major heap */
+ /* caml_alloc_shr and caml_initialize never trigger a GC,
+ so no need to Setup_for_gc */
+ accu = caml_alloc_shr(1 + nvars, Closure_tag);
+ for (i = 0; i < nvars; i++) caml_initialize(&Field(accu, i + 1), sp[i]);
+ }
+ /* The code pointer is not in the heap, so no need to go through
+ caml_initialize. */
+ Code_val(accu) = pc + *pc;
+ pc++;
+ sp += nvars;
+ Next;
+ }
+
+ Instruct(CLOSUREREC): {
+ int nfuncs = *pc++;
+ int nvars = *pc++;
+ mlsize_t blksize = nfuncs * 2 - 1 + nvars;
+ int i;
+ value * p;
+ if (nvars > 0) *--sp = accu;
+ if (blksize <= Max_young_wosize) {
+ Alloc_small(accu, blksize, Closure_tag);
+ p = &Field(accu, nfuncs * 2 - 1);
+ for (i = 0; i < nvars; i++, p++) *p = sp[i];
+ } else {
+ /* PR#6385: must allocate in major heap */
+ /* caml_alloc_shr and caml_initialize never trigger a GC,
+ so no need to Setup_for_gc */
+ accu = caml_alloc_shr(blksize, Closure_tag);
+ p = &Field(accu, nfuncs * 2 - 1);
+ for (i = 0; i < nvars; i++, p++) caml_initialize(p, sp[i]);
+ }
+ sp += nvars;
+ /* The code pointers and infix headers are not in the heap,
+ so no need to go through caml_initialize. */
+ p = &Field(accu, 0);
+ *p = (value) (pc + pc[0]);
+ *--sp = accu;
+ p++;
+ for (i = 1; i < nfuncs; i++) {
+ *p = Make_header(i * 2, Infix_tag, Caml_white); /* color irrelevant. */
+ p++;
+ *p = (value) (pc + pc[i]);
+ *--sp = (value) p;
+ p++;
+ }
+ pc += nfuncs;
+ Next;
+ }
+
+ Instruct(PUSHOFFSETCLOSURE):
+ *--sp = accu; /* fallthrough */
+ Instruct(OFFSETCLOSURE):
+ accu = env + *pc++ * sizeof(value); Next;
+
+ Instruct(PUSHOFFSETCLOSUREM2):
+ *--sp = accu; /* fallthrough */
+ Instruct(OFFSETCLOSUREM2):
+ accu = env - 2 * sizeof(value); Next;
+ Instruct(PUSHOFFSETCLOSURE0):
+ *--sp = accu; /* fallthrough */
+ Instruct(OFFSETCLOSURE0):
+ accu = env; Next;
+ Instruct(PUSHOFFSETCLOSURE2):
+ *--sp = accu; /* fallthrough */
+ Instruct(OFFSETCLOSURE2):
+ accu = env + 2 * sizeof(value); Next;
+
+
+/* Access to global variables */
+
+ Instruct(PUSHGETGLOBAL):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(GETGLOBAL):
+ accu = Field(caml_global_data, *pc);
+ pc++;
+ Next;
+
+ Instruct(PUSHGETGLOBALFIELD):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(GETGLOBALFIELD): {
+ accu = Field(caml_global_data, *pc);
+ pc++;
+ accu = Field(accu, *pc);
+ pc++;
+ Next;
+ }
+
+ Instruct(SETGLOBAL):
+ caml_modify(&Field(caml_global_data, *pc), accu);
+ accu = Val_unit;
+ pc++;
+ Next;
+
+/* Allocation of blocks */
+
+ Instruct(PUSHATOM0):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(ATOM0):
+ accu = Atom(0); Next;
+
+ Instruct(PUSHATOM):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(ATOM):
+ accu = Atom(*pc++); Next;
+
+ Instruct(MAKEBLOCK): {
+ mlsize_t wosize = *pc++;
+ tag_t tag = *pc++;
+ mlsize_t i;
+ value block;
+ if (wosize <= Max_young_wosize) {
+ Alloc_small(block, wosize, tag);
+ Field(block, 0) = accu;
+ for (i = 1; i < wosize; i++) Field(block, i) = *sp++;
+ } else {
+ block = caml_alloc_shr(wosize, tag);
+ caml_initialize(&Field(block, 0), accu);
+ for (i = 1; i < wosize; i++) caml_initialize(&Field(block, i), *sp++);
+ }
+ accu = block;
+ Next;
+ }
+ Instruct(MAKEBLOCK1): {
+ tag_t tag = *pc++;
+ value block;
+ Alloc_small(block, 1, tag);
+ Field(block, 0) = accu;
+ accu = block;
+ Next;
+ }
+ Instruct(MAKEBLOCK2): {
+ tag_t tag = *pc++;
+ value block;
+ Alloc_small(block, 2, tag);
+ Field(block, 0) = accu;
+ Field(block, 1) = sp[0];
+ sp += 1;
+ accu = block;
+ Next;
+ }
+ Instruct(MAKEBLOCK3): {
+ tag_t tag = *pc++;
+ value block;
+ Alloc_small(block, 3, tag);
+ Field(block, 0) = accu;
+ Field(block, 1) = sp[0];
+ Field(block, 2) = sp[1];
+ sp += 2;
+ accu = block;
+ Next;
+ }
+ Instruct(MAKEFLOATBLOCK): {
+ mlsize_t size = *pc++;
+ mlsize_t i;
+ value block;
+ if (size <= Max_young_wosize / Double_wosize) {
+ Alloc_small(block, size * Double_wosize, Double_array_tag);
+ } else {
+ block = caml_alloc_shr(size * Double_wosize, Double_array_tag);
+ }
+ Store_double_flat_field(block, 0, Double_val(accu));
+ for (i = 1; i < size; i++){
+ Store_double_flat_field(block, i, Double_val(*sp));
+ ++ sp;
+ }
+ accu = block;
+ Next;
+ }
+
+/* Access to components of blocks */
+
+ Instruct(GETFIELD0):
+ accu = Field(accu, 0); Next;
+ Instruct(GETFIELD1):
+ accu = Field(accu, 1); Next;
+ Instruct(GETFIELD2):
+ accu = Field(accu, 2); Next;
+ Instruct(GETFIELD3):
+ accu = Field(accu, 3); Next;
+ Instruct(GETFIELD):
+ accu = Field(accu, *pc); pc++; Next;
+ Instruct(GETFLOATFIELD): {
+ double d = Double_flat_field(accu, *pc);
+ Alloc_small(accu, Double_wosize, Double_tag);
+ Store_double_val(accu, d);
+ pc++;
+ Next;
+ }
+
+ Instruct(SETFIELD0):
+ caml_modify(&Field(accu, 0), *sp++);
+ accu = Val_unit;
+ Next;
+ Instruct(SETFIELD1):
+ caml_modify(&Field(accu, 1), *sp++);
+ accu = Val_unit;
+ Next;
+ Instruct(SETFIELD2):
+ caml_modify(&Field(accu, 2), *sp++);
+ accu = Val_unit;
+ Next;
+ Instruct(SETFIELD3):
+ caml_modify(&Field(accu, 3), *sp++);
+ accu = Val_unit;
+ Next;
+ Instruct(SETFIELD):
+ caml_modify(&Field(accu, *pc), *sp++);
+ accu = Val_unit;
+ pc++;
+ Next;
+ Instruct(SETFLOATFIELD):
+ Store_double_flat_field(accu, *pc, Double_val(*sp));
+ accu = Val_unit;
+ sp++;
+ pc++;
+ Next;
+
+/* Array operations */
+
+ Instruct(VECTLENGTH): {
+ /* Todo: when FLAT_FLOAT_ARRAY is false, this instruction should
+ be split into VECTLENGTH and FLOATVECTLENGTH because we know
+ statically which one it is. */
+ mlsize_t size = Wosize_val(accu);
+ if (Tag_val(accu) == Double_array_tag) size = size / Double_wosize;
+ accu = Val_long(size);
+ Next;
+ }
+ Instruct(GETVECTITEM):
+ accu = Field(accu, Long_val(sp[0]));
+ sp += 1;
+ Next;
+ Instruct(SETVECTITEM):
+ caml_modify(&Field(accu, Long_val(sp[0])), sp[1]);
+ accu = Val_unit;
+ sp += 2;
+ Next;
+
+/* Bytes/String operations */
+ Instruct(GETSTRINGCHAR):
+ Instruct(GETBYTESCHAR):
+ accu = Val_int(Byte_u(accu, Long_val(sp[0])));
+ sp += 1;
+ Next;
+ Instruct(SETBYTESCHAR):
+ Byte_u(accu, Long_val(sp[0])) = Int_val(sp[1]);
+ sp += 2;
+ accu = Val_unit;
+ Next;
+
+/* Branches and conditional branches */
+
+ Instruct(BRANCH):
+ pc += *pc;
+ Next;
+ Instruct(BRANCHIF):
+ if (accu != Val_false) pc += *pc; else pc++;
+ Next;
+ Instruct(BRANCHIFNOT):
+ if (accu == Val_false) pc += *pc; else pc++;
+ Next;
+ Instruct(SWITCH): {
+ uint32_t sizes = *pc++;
+ if (Is_block(accu)) {
+ intnat index = Tag_val(accu);
+ CAMLassert ((uintnat) index < (sizes >> 16));
+ pc += pc[(sizes & 0xFFFF) + index];
+ } else {
+ intnat index = Long_val(accu);
+ CAMLassert ((uintnat) index < (sizes & 0xFFFF)) ;
+ pc += pc[index];
+ }
+ Next;
+ }
+ Instruct(BOOLNOT):
+ accu = Val_not(accu);
+ Next;
+
+/* Exceptions */
+
+ Instruct(PUSHTRAP):
+ sp -= 4;
+ Trap_pc(sp) = pc + *pc;
+ Trap_link(sp) = caml_trapsp;
+ sp[2] = env;
+ sp[3] = Val_long(extra_args);
+ caml_trapsp = sp;
+ pc++;
+ Next;
+
+ Instruct(POPTRAP):
+ if (caml_something_to_do) {
+ /* We must check here so that if a signal is pending and its
+ handler triggers an exception, the exception is trapped
+ by the current try...with, not the enclosing one. */
+ pc--; /* restart the POPTRAP after processing the signal */
+ goto process_signal;
+ }
+ caml_trapsp = Trap_link(sp);
+ sp += 4;
+ Next;
+
+ Instruct(RAISE_NOTRACE):
+ if (caml_trapsp >= caml_trap_barrier) caml_debugger(TRAP_BARRIER);
+ goto raise_notrace;
+
+ Instruct(RERAISE):
+ if (caml_trapsp >= caml_trap_barrier) caml_debugger(TRAP_BARRIER);
+ if (caml_backtrace_active) caml_stash_backtrace(accu, pc, sp, 1);
+ goto raise_notrace;
+
+ Instruct(RAISE):
+ raise_exception:
+ if (caml_trapsp >= caml_trap_barrier) caml_debugger(TRAP_BARRIER);
+ if (caml_backtrace_active) caml_stash_backtrace(accu, pc, sp, 0);
+ raise_notrace:
+ if ((char *) caml_trapsp
+ >= (char *) caml_stack_high - initial_sp_offset) {
+ caml_external_raise = initial_external_raise;
+ caml_extern_sp = (value *) ((char *) caml_stack_high
+ - initial_sp_offset);
+ caml_callback_depth--;
+ return Make_exception_result(accu);
+ }
+ sp = caml_trapsp;
+ pc = Trap_pc(sp);
+ caml_trapsp = Trap_link(sp);
+ env = sp[2];
+ extra_args = Long_val(sp[3]);
+ sp += 4;
+ Next;
+
+/* Stack checks */
+
+ check_stacks:
+ if (sp < caml_stack_threshold) {
+ caml_extern_sp = sp;
+ caml_realloc_stack(Stack_threshold / sizeof(value));
+ sp = caml_extern_sp;
+ }
+ /* Fall through CHECK_SIGNALS */
+
+/* Signal handling */
+
+ Instruct(CHECK_SIGNALS): /* accu not preserved */
+ if (caml_something_to_do) goto process_signal;
+ Next;
+
+ process_signal:
+ caml_something_to_do = 0;
+ Setup_for_event;
+ caml_process_event();
+ Restore_after_event;
+ Next;
+
+/* Calling C functions */
+
+ Instruct(C_CALL1):
+ Setup_for_c_call;
+ accu = Primitive(*pc)(accu);
+ Restore_after_c_call;
+ pc++;
+ Next;
+ Instruct(C_CALL2):
+ Setup_for_c_call;
+ accu = Primitive(*pc)(accu, sp[1]);
+ Restore_after_c_call;
+ sp += 1;
+ pc++;
+ Next;
+ Instruct(C_CALL3):
+ Setup_for_c_call;
+ accu = Primitive(*pc)(accu, sp[1], sp[2]);
+ Restore_after_c_call;
+ sp += 2;
+ pc++;
+ Next;
+ Instruct(C_CALL4):
+ Setup_for_c_call;
+ accu = Primitive(*pc)(accu, sp[1], sp[2], sp[3]);
+ Restore_after_c_call;
+ sp += 3;
+ pc++;
+ Next;
+ Instruct(C_CALL5):
+ Setup_for_c_call;
+ accu = Primitive(*pc)(accu, sp[1], sp[2], sp[3], sp[4]);
+ Restore_after_c_call;
+ sp += 4;
+ pc++;
+ Next;
+ Instruct(C_CALLN): {
+ int nargs = *pc++;
+ *--sp = accu;
+ Setup_for_c_call;
+ accu = Primitive(*pc)(sp + 1, nargs);
+ Restore_after_c_call;
+ sp += nargs;
+ pc++;
+ Next;
+ }
+
+/* Integer constants */
+
+ Instruct(CONST0):
+ accu = Val_int(0); Next;
+ Instruct(CONST1):
+ accu = Val_int(1); Next;
+ Instruct(CONST2):
+ accu = Val_int(2); Next;
+ Instruct(CONST3):
+ accu = Val_int(3); Next;
+
+ Instruct(PUSHCONST0):
+ *--sp = accu; accu = Val_int(0); Next;
+ Instruct(PUSHCONST1):
+ *--sp = accu; accu = Val_int(1); Next;
+ Instruct(PUSHCONST2):
+ *--sp = accu; accu = Val_int(2); Next;
+ Instruct(PUSHCONST3):
+ *--sp = accu; accu = Val_int(3); Next;
+
+ Instruct(PUSHCONSTINT):
+ *--sp = accu;
+ /* Fallthrough */
+ Instruct(CONSTINT):
+ accu = Val_int(*pc);
+ pc++;
+ Next;
+
+/* Integer arithmetic */
+
+ Instruct(NEGINT):
+ accu = (value)(2 - (intnat)accu); Next;
+ Instruct(ADDINT):
+ accu = (value)((intnat) accu + (intnat) *sp++ - 1); Next;
+ Instruct(SUBINT):
+ accu = (value)((intnat) accu - (intnat) *sp++ + 1); Next;
+ Instruct(MULINT):
+ accu = Val_long(Long_val(accu) * Long_val(*sp++)); Next;
+
+ Instruct(DIVINT): {
+ intnat divisor = Long_val(*sp++);
+ if (divisor == 0) { Setup_for_c_call; caml_raise_zero_divide(); }
+ accu = Val_long(Long_val(accu) / divisor);
+ Next;
+ }
+ Instruct(MODINT): {
+ intnat divisor = Long_val(*sp++);
+ if (divisor == 0) { Setup_for_c_call; caml_raise_zero_divide(); }
+ accu = Val_long(Long_val(accu) % divisor);
+ Next;
+ }
+ Instruct(ANDINT):
+ accu = (value)((intnat) accu & (intnat) *sp++); Next;
+ Instruct(ORINT):
+ accu = (value)((intnat) accu | (intnat) *sp++); Next;
+ Instruct(XORINT):
+ accu = (value)(((intnat) accu ^ (intnat) *sp++) | 1); Next;
+ Instruct(LSLINT):
+ accu = (value)((((intnat) accu - 1) << Long_val(*sp++)) + 1); Next;
+ Instruct(LSRINT):
+ accu = (value)((((uintnat) accu) >> Long_val(*sp++)) | 1); Next;
+ Instruct(ASRINT):
+ accu = (value)((((intnat) accu) >> Long_val(*sp++)) | 1); Next;
+
+#define Integer_comparison(typ,opname,tst) \
+ Instruct(opname): \
+ accu = Val_int((typ) accu tst (typ) *sp++); Next;
+
+ Integer_comparison(intnat,EQ, ==)
+ Integer_comparison(intnat,NEQ, !=)
+ Integer_comparison(intnat,LTINT, <)
+ Integer_comparison(intnat,LEINT, <=)
+ Integer_comparison(intnat,GTINT, >)
+ Integer_comparison(intnat,GEINT, >=)
+ Integer_comparison(uintnat,ULTINT, <)
+ Integer_comparison(uintnat,UGEINT, >=)
+
+#define Integer_branch_comparison(typ,opname,tst,debug) \
+ Instruct(opname): \
+ if ( *pc++ tst (typ) Long_val(accu)) { \
+ pc += *pc ; \
+ } else { \
+ pc++ ; \
+ } ; Next;
+
+ Integer_branch_comparison(intnat,BEQ, ==, "==")
+ Integer_branch_comparison(intnat,BNEQ, !=, "!=")
+ Integer_branch_comparison(intnat,BLTINT, <, "<")
+ Integer_branch_comparison(intnat,BLEINT, <=, "<=")
+ Integer_branch_comparison(intnat,BGTINT, >, ">")
+ Integer_branch_comparison(intnat,BGEINT, >=, ">=")
+ Integer_branch_comparison(uintnat,BULTINT, <, "<")
+ Integer_branch_comparison(uintnat,BUGEINT, >=, ">=")
+
+ Instruct(OFFSETINT):
+ accu += *pc << 1;
+ pc++;
+ Next;
+ Instruct(OFFSETREF):
+ Field(accu, 0) += *pc << 1;
+ accu = Val_unit;
+ pc++;
+ Next;
+ Instruct(ISINT):
+ accu = Val_long(accu & 1);
+ Next;
+
+/* Object-oriented operations */
+
+#define Lookup(obj, lab) Field (Field (obj, 0), Int_val(lab))
+
+ /* please don't forget to keep below code in sync with the
+ functions caml_cache_public_method and
+ caml_cache_public_method2 in obj.c */
+
+ Instruct(GETMETHOD):
+ accu = Lookup(sp[0], accu);
+ Next;
+
+#define CAML_METHOD_CACHE
+#ifdef CAML_METHOD_CACHE
+ Instruct(GETPUBMET): {
+ /* accu == object, pc[0] == tag, pc[1] == cache */
+ value meths = Field (accu, 0);
+ value ofs;
+#ifdef CAML_TEST_CACHE
+ static int calls = 0, hits = 0;
+ if (calls >= 10000000) {
+ fprintf(stderr, "cache hit = %d%%\n", hits / 100000);
+ calls = 0; hits = 0;
+ }
+ calls++;
+#endif
+ *--sp = accu;
+ accu = Val_int(*pc++);
+ ofs = *pc & Field(meths,1);
+ if (*(value*)(((char*)&Field(meths,3)) + ofs) == accu) {
+#ifdef CAML_TEST_CACHE
+ hits++;
+#endif
+ accu = *(value*)(((char*)&Field(meths,2)) + ofs);
+ }
+ else
+ {
+ int li = 3, hi = Field(meths,0), mi;
+ while (li < hi) {
+ mi = ((li+hi) >> 1) | 1;
+ if (accu < Field(meths,mi)) hi = mi-2;
+ else li = mi;
+ }
+ *pc = (li-3)*sizeof(value);
+ accu = Field (meths, li-1);
+ }
+ pc++;
+ Next;
+ }
+#else
+ Instruct(GETPUBMET):
+ *--sp = accu;
+ accu = Val_int(*pc);
+ pc += 2;
+ /* Fallthrough */
+#endif
+ Instruct(GETDYNMET): {
+ /* accu == tag, sp[0] == object, *pc == cache */
+ value meths = Field (sp[0], 0);
+ int li = 3, hi = Field(meths,0), mi;
+ while (li < hi) {
+ mi = ((li+hi) >> 1) | 1;
+ if (accu < Field(meths,mi)) hi = mi-2;
+ else li = mi;
+ }
+ accu = Field (meths, li-1);
+ Next;
+ }
+
+/* Debugging and machine control */
+
+ Instruct(STOP):
+ caml_external_raise = initial_external_raise;
+ caml_extern_sp = sp;
+ caml_callback_depth--;
+ return accu;
+
+ Instruct(EVENT):
+ if (--caml_event_count == 0) {
+ Setup_for_debugger;
+ caml_debugger(EVENT_COUNT);
+ Restore_after_debugger;
+ }
+ Restart_curr_instr;
+
+ Instruct(BREAK):
+ Setup_for_debugger;
+ caml_debugger(BREAKPOINT);
+ Restore_after_debugger;
+ Restart_curr_instr;
+
+#ifndef THREADED_CODE
+ default:
+#if _MSC_VER >= 1200
+ __assume(0);
+#else
+ caml_fatal_error_arg("Fatal error: bad opcode (%"
+ ARCH_INTNAT_PRINTF_FORMAT "x)\n",
+ (char *) (intnat) *(pc-1));
+#endif
+ }
+ }
+#endif
+}
+
+void caml_prepare_bytecode(code_t prog, asize_t prog_size) {
+ /* other implementations of the interpreter (such as an hypothetical
+ JIT translator) might want to do something with a bytecode before
+ running it */
+ CAMLassert(prog);
+ CAMLassert(prog_size>0);
+ /* actually, the threading of the bytecode might be done here */
+}
+
+void caml_release_bytecode(code_t prog, asize_t prog_size) {
+ /* other implementations of the interpreter (such as an hypothetical
+ JIT translator) might want to know when a bytecode is removed */
+ /* check that we have a program */
+ CAMLassert(prog);
+ CAMLassert(prog_size>0);
+}
diff --git a/test/monniaux/ocaml/byterun/ints.c b/test/monniaux/ocaml/byterun/ints.c
new file mode 100644
index 00000000..76ae11d4
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/ints.c
@@ -0,0 +1,830 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <stdio.h>
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/intext.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+
+static const char * parse_sign_and_base(const char * p,
+ /*out*/ int * base,
+ /*out*/ int * signedness,
+ /*out*/ int * sign)
+{
+ *sign = 1;
+ if (*p == '-') {
+ *sign = -1;
+ p++;
+ } else if (*p == '+')
+ p++;
+ *base = 10; *signedness = 1;
+ if (*p == '0') {
+ switch (p[1]) {
+ case 'x': case 'X':
+ *base = 16; *signedness = 0; p += 2; break;
+ case 'o': case 'O':
+ *base = 8; *signedness = 0; p += 2; break;
+ case 'b': case 'B':
+ *base = 2; *signedness = 0; p += 2; break;
+ case 'u': case 'U':
+ *signedness = 0; p += 2; break;
+ }
+ }
+ return p;
+}
+
+static int parse_digit(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ else if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ else if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ else
+ return -1;
+}
+
+#define INT_ERRMSG "int_of_string"
+#define INT32_ERRMSG "Int32.of_string"
+#define INT64_ERRMSG "Int64.of_string"
+#define INTNAT_ERRMSG "Nativeint.of_string"
+
+static intnat parse_intnat(value s, int nbits, const char *errmsg)
+{
+ const char * p;
+ uintnat res, threshold;
+ int sign, base, signedness, d;
+
+ p = parse_sign_and_base(String_val(s), &base, &signedness, &sign);
+ threshold = ((uintnat) -1) / base;
+ d = parse_digit(*p);
+ if (d < 0 || d >= base) caml_failwith(errmsg);
+ for (p++, res = d; /*nothing*/; p++) {
+ char c = *p;
+ if (c == '_') continue;
+ d = parse_digit(c);
+ if (d < 0 || d >= base) break;
+ /* Detect overflow in multiplication base * res */
+ if (res > threshold) caml_failwith(errmsg);
+ res = base * res + d;
+ /* Detect overflow in addition (base * res) + d */
+ if (res < (uintnat) d) caml_failwith(errmsg);
+ }
+ if (p != String_val(s) + caml_string_length(s)){
+ caml_failwith(errmsg);
+ }
+ if (signedness) {
+ /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
+ if (sign >= 0) {
+ if (res >= (uintnat)1 << (nbits - 1)) caml_failwith(errmsg);
+ } else {
+ if (res > (uintnat)1 << (nbits - 1)) caml_failwith(errmsg);
+ }
+ } else {
+ /* Unsigned representation expected, allow 0 to 2^nbits - 1
+ and tolerate -(2^nbits - 1) to 0 */
+ if (nbits < sizeof(uintnat) * 8 && res >= (uintnat)1 << nbits)
+ caml_failwith(errmsg);
+ }
+ return sign < 0 ? -((intnat) res) : (intnat) res;
+}
+
+value caml_bswap16_direct(value x)
+{
+ return ((((x & 0x00FF) << 8) |
+ ((x & 0xFF00) >> 8)));
+}
+
+CAMLprim value caml_bswap16(value v)
+{
+ intnat x = Int_val(v);
+ return (Val_int ((((x & 0x00FF) << 8) |
+ ((x & 0xFF00) >> 8))));
+}
+
+/* Tagged integers */
+
+CAMLprim value caml_int_compare(value v1, value v2)
+{
+ int res = (v1 > v2) - (v1 < v2);
+ return Val_int(res);
+}
+
+CAMLprim value caml_int_of_string(value s)
+{
+ return Val_long(parse_intnat(s, 8 * sizeof(value) - 1, INT_ERRMSG));
+}
+
+#define FORMAT_BUFFER_SIZE 32
+
+static char parse_format(value fmt,
+ char * suffix,
+ char format_string[FORMAT_BUFFER_SIZE])
+{
+ char * p;
+ char lastletter;
+ mlsize_t len, len_suffix;
+
+ /* Copy OCaml format fmt to format_string,
+ adding the suffix before the last letter of the format */
+ len = caml_string_length(fmt);
+ len_suffix = strlen(suffix);
+ if (len + len_suffix + 1 >= FORMAT_BUFFER_SIZE)
+ caml_invalid_argument("format_int: format too long");
+ memmove(format_string, String_val(fmt), len);
+ p = format_string + len - 1;
+ lastletter = *p;
+ /* Compress two-letter formats, ignoring the [lnL] annotation */
+ if (p[-1] == 'l' || p[-1] == 'n' || p[-1] == 'L') p--;
+ memmove(p, suffix, len_suffix); p += len_suffix;
+ *p++ = lastletter;
+ *p = 0;
+ /* Return the conversion type (last letter) */
+ return lastletter;
+}
+
+CAMLprim value caml_format_int(value fmt, value arg)
+{
+ char format_string[FORMAT_BUFFER_SIZE];
+ char conv;
+ value res;
+
+ conv = parse_format(fmt, ARCH_INTNAT_PRINTF_FORMAT, format_string);
+ switch (conv) {
+ case 'u': case 'x': case 'X': case 'o':
+ res = caml_alloc_sprintf(format_string, Unsigned_long_val(arg));
+ break;
+ default:
+ res = caml_alloc_sprintf(format_string, Long_val(arg));
+ break;
+ }
+ return res;
+}
+
+/* 32-bit integers */
+
+static int int32_cmp(value v1, value v2)
+{
+ int32_t i1 = Int32_val(v1);
+ int32_t i2 = Int32_val(v2);
+ return (i1 > i2) - (i1 < i2);
+}
+
+static intnat int32_hash(value v)
+{
+ return Int32_val(v);
+}
+
+static void int32_serialize(value v, uintnat * bsize_32,
+ uintnat * bsize_64)
+{
+ caml_serialize_int_4(Int32_val(v));
+ *bsize_32 = *bsize_64 = 4;
+}
+
+static uintnat int32_deserialize(void * dst)
+{
+ *((int32_t *) dst) = caml_deserialize_sint_4();
+ return 4;
+}
+
+CAMLexport struct custom_operations caml_int32_ops = {
+ "_i",
+ custom_finalize_default,
+ int32_cmp,
+ int32_hash,
+ int32_serialize,
+ int32_deserialize,
+ custom_compare_ext_default
+};
+
+CAMLexport value caml_copy_int32(int32_t i)
+{
+ value res = caml_alloc_custom(&caml_int32_ops, 4, 0, 1);
+ Int32_val(res) = i;
+ return res;
+}
+
+CAMLprim value caml_int32_neg(value v)
+{ return caml_copy_int32(- Int32_val(v)); }
+
+CAMLprim value caml_int32_add(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) + Int32_val(v2)); }
+
+CAMLprim value caml_int32_sub(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) - Int32_val(v2)); }
+
+CAMLprim value caml_int32_mul(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) * Int32_val(v2)); }
+
+CAMLprim value caml_int32_div(value v1, value v2)
+{
+ int32_t dividend = Int32_val(v1);
+ int32_t divisor = Int32_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, division crashes on overflow.
+ Implement the same behavior as for type "int". */
+ if (dividend == (1<<31) && divisor == -1) return v1;
+ return caml_copy_int32(dividend / divisor);
+}
+
+CAMLprim value caml_int32_mod(value v1, value v2)
+{
+ int32_t dividend = Int32_val(v1);
+ int32_t divisor = Int32_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, modulus crashes if division overflows.
+ Implement the same behavior as for type "int". */
+ if (dividend == (1<<31) && divisor == -1) return caml_copy_int32(0);
+ return caml_copy_int32(dividend % divisor);
+}
+
+CAMLprim value caml_int32_and(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) & Int32_val(v2)); }
+
+CAMLprim value caml_int32_or(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) | Int32_val(v2)); }
+
+CAMLprim value caml_int32_xor(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) ^ Int32_val(v2)); }
+
+CAMLprim value caml_int32_shift_left(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) << Int_val(v2)); }
+
+CAMLprim value caml_int32_shift_right(value v1, value v2)
+{ return caml_copy_int32(Int32_val(v1) >> Int_val(v2)); }
+
+CAMLprim value caml_int32_shift_right_unsigned(value v1, value v2)
+{ return caml_copy_int32((uint32_t)Int32_val(v1) >> Int_val(v2)); }
+
+static int32_t caml_swap32(int32_t x)
+{
+ return (((x & 0x000000FF) << 24) |
+ ((x & 0x0000FF00) << 8) |
+ ((x & 0x00FF0000) >> 8) |
+ ((x & 0xFF000000) >> 24));
+}
+
+value caml_int32_direct_bswap(value v)
+{ return caml_swap32(v); }
+
+CAMLprim value caml_int32_bswap(value v)
+{ return caml_copy_int32(caml_swap32(Int32_val(v))); }
+
+CAMLprim value caml_int32_of_int(value v)
+{ return caml_copy_int32(Long_val(v)); }
+
+CAMLprim value caml_int32_to_int(value v)
+{ return Val_long(Int32_val(v)); }
+
+int32_t caml_int32_of_float_unboxed(double x)
+{ return x; }
+
+CAMLprim value caml_int32_of_float(value v)
+{ return caml_copy_int32((int32_t)(Double_val(v))); }
+
+double caml_int32_to_float_unboxed(int32_t x)
+{ return x; }
+
+CAMLprim value caml_int32_to_float(value v)
+{ return caml_copy_double((double)(Int32_val(v))); }
+
+intnat caml_int32_compare_unboxed(int32_t i1, int32_t i2)
+{
+ return (i1 > i2) - (i1 < i2);
+}
+
+CAMLprim value caml_int32_compare(value v1, value v2)
+{
+ return Val_int(caml_int32_compare_unboxed(Int32_val(v1),Int32_val(v2)));
+}
+
+CAMLprim value caml_int32_format(value fmt, value arg)
+{
+ char format_string[FORMAT_BUFFER_SIZE];
+
+ parse_format(fmt, ARCH_INT32_PRINTF_FORMAT, format_string);
+ return caml_alloc_sprintf(format_string, Int32_val(arg));
+}
+
+CAMLprim value caml_int32_of_string(value s)
+{
+ return caml_copy_int32(parse_intnat(s, 32, INT32_ERRMSG));
+}
+
+int32_t caml_int32_bits_of_float_unboxed(double d)
+{
+ union { float d; int32_t i; } u;
+ u.d = d;
+ return u.i;
+}
+
+double caml_int32_float_of_bits_unboxed(int32_t i)
+{
+ union { float d; int32_t i; } u;
+ u.i = i;
+ return u.d;
+}
+
+CAMLprim value caml_int32_bits_of_float(value vd)
+{
+ return caml_copy_int32(caml_int32_bits_of_float_unboxed(Double_val(vd)));
+}
+
+CAMLprim value caml_int32_float_of_bits(value vi)
+{
+ return caml_copy_double(caml_int32_float_of_bits_unboxed(Int32_val(vi)));
+}
+
+/* 64-bit integers */
+
+#ifdef ARCH_ALIGN_INT64
+
+CAMLexport int64_t caml_Int64_val(value v)
+{
+ union { int32_t i[2]; int64_t j; } buffer;
+ buffer.i[0] = ((int32_t *) Data_custom_val(v))[0];
+ buffer.i[1] = ((int32_t *) Data_custom_val(v))[1];
+ return buffer.j;
+}
+
+#endif
+
+static int int64_cmp(value v1, value v2)
+{
+ int64_t i1 = Int64_val(v1);
+ int64_t i2 = Int64_val(v2);
+ return (i1 > i2) - (i1 < i2);
+}
+
+static intnat int64_hash(value v)
+{
+ int64_t x = Int64_val(v);
+ uint32_t lo = (uint32_t) x, hi = (uint32_t) (x >> 32);
+ return hi ^ lo;
+}
+
+static void int64_serialize(value v, uintnat * bsize_32,
+ uintnat * bsize_64)
+{
+ caml_serialize_int_8(Int64_val(v));
+ *bsize_32 = *bsize_64 = 8;
+}
+
+static uintnat int64_deserialize(void * dst)
+{
+#ifndef ARCH_ALIGN_INT64
+ *((int64_t *) dst) = caml_deserialize_sint_8();
+#else
+ union { int32_t i[2]; int64_t j; } buffer;
+ buffer.j = caml_deserialize_sint_8();
+ ((int32_t *) dst)[0] = buffer.i[0];
+ ((int32_t *) dst)[1] = buffer.i[1];
+#endif
+ return 8;
+}
+
+CAMLexport struct custom_operations caml_int64_ops = {
+ "_j",
+ custom_finalize_default,
+ int64_cmp,
+ int64_hash,
+ int64_serialize,
+ int64_deserialize,
+ custom_compare_ext_default
+};
+
+CAMLexport value caml_copy_int64(int64_t i)
+{
+ value res = caml_alloc_custom(&caml_int64_ops, 8, 0, 1);
+#ifndef ARCH_ALIGN_INT64
+ Int64_val(res) = i;
+#else
+ union { int32_t i[2]; int64_t j; } buffer;
+ buffer.j = i;
+ ((int32_t *) Data_custom_val(res))[0] = buffer.i[0];
+ ((int32_t *) Data_custom_val(res))[1] = buffer.i[1];
+#endif
+ return res;
+}
+
+CAMLprim value caml_int64_neg(value v)
+{ return caml_copy_int64(- Int64_val(v)); }
+
+CAMLprim value caml_int64_add(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) + Int64_val(v2)); }
+
+CAMLprim value caml_int64_sub(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) - Int64_val(v2)); }
+
+CAMLprim value caml_int64_mul(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) * Int64_val(v2)); }
+
+#define Int64_min_int ((intnat) 1 << (sizeof(intnat) * 8 - 1))
+
+CAMLprim value caml_int64_div(value v1, value v2)
+{
+ int64_t dividend = Int64_val(v1);
+ int64_t divisor = Int64_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, division crashes on overflow.
+ Implement the same behavior as for type "int". */
+ if (dividend == ((int64_t)1 << 63) && divisor == -1) return v1;
+ return caml_copy_int64(Int64_val(v1) / divisor);
+}
+
+CAMLprim value caml_int64_mod(value v1, value v2)
+{
+ int64_t dividend = Int64_val(v1);
+ int64_t divisor = Int64_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, division crashes on overflow.
+ Implement the same behavior as for type "int". */
+ if (dividend == ((int64_t)1 << 63) && divisor == -1){
+ return caml_copy_int64(0);
+ }
+ return caml_copy_int64(Int64_val(v1) % divisor);
+}
+
+CAMLprim value caml_int64_and(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) & Int64_val(v2)); }
+
+CAMLprim value caml_int64_or(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) | Int64_val(v2)); }
+
+CAMLprim value caml_int64_xor(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) ^ Int64_val(v2)); }
+
+CAMLprim value caml_int64_shift_left(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) << Int_val(v2)); }
+
+CAMLprim value caml_int64_shift_right(value v1, value v2)
+{ return caml_copy_int64(Int64_val(v1) >> Int_val(v2)); }
+
+CAMLprim value caml_int64_shift_right_unsigned(value v1, value v2)
+{ return caml_copy_int64((uint64_t) (Int64_val(v1)) >> Int_val(v2)); }
+
+#ifdef ARCH_SIXTYFOUR
+static value caml_swap64(value x)
+{
+ return (((((x) & 0x00000000000000FF) << 56) |
+ (((x) & 0x000000000000FF00) << 40) |
+ (((x) & 0x0000000000FF0000) << 24) |
+ (((x) & 0x00000000FF000000) << 8) |
+ (((x) & 0x000000FF00000000) >> 8) |
+ (((x) & 0x0000FF0000000000) >> 24) |
+ (((x) & 0x00FF000000000000) >> 40) |
+ (((x) & 0xFF00000000000000) >> 56)));
+}
+
+value caml_int64_direct_bswap(value v)
+{ return caml_swap64(v); }
+#endif
+
+CAMLprim value caml_int64_bswap(value v)
+{
+ int64_t x = Int64_val(v);
+ return caml_copy_int64
+ (((x & INT64_LITERAL(0x00000000000000FFU)) << 56) |
+ ((x & INT64_LITERAL(0x000000000000FF00U)) << 40) |
+ ((x & INT64_LITERAL(0x0000000000FF0000U)) << 24) |
+ ((x & INT64_LITERAL(0x00000000FF000000U)) << 8) |
+ ((x & INT64_LITERAL(0x000000FF00000000U)) >> 8) |
+ ((x & INT64_LITERAL(0x0000FF0000000000U)) >> 24) |
+ ((x & INT64_LITERAL(0x00FF000000000000U)) >> 40) |
+ ((x & INT64_LITERAL(0xFF00000000000000U)) >> 56));
+}
+
+CAMLprim value caml_int64_of_int(value v)
+{ return caml_copy_int64((int64_t) (Long_val(v))); }
+
+CAMLprim value caml_int64_to_int(value v)
+{ return Val_long((intnat) (Int64_val(v))); }
+
+int64_t caml_int64_of_float_unboxed(double x)
+{ return x; }
+
+CAMLprim value caml_int64_of_float(value v)
+{ return caml_copy_int64((int64_t) (Double_val(v))); }
+
+double caml_int64_to_float_unboxed(int64_t x)
+{ return x; }
+
+CAMLprim value caml_int64_to_float(value v)
+{ return caml_copy_double((double) (Int64_val(v))); }
+
+CAMLprim value caml_int64_of_int32(value v)
+{ return caml_copy_int64((int64_t) (Int32_val(v))); }
+
+CAMLprim value caml_int64_to_int32(value v)
+{ return caml_copy_int32((int32_t) (Int64_val(v))); }
+
+CAMLprim value caml_int64_of_nativeint(value v)
+{ return caml_copy_int64((int64_t) (Nativeint_val(v))); }
+
+CAMLprim value caml_int64_to_nativeint(value v)
+{ return caml_copy_nativeint((intnat) (Int64_val(v))); }
+
+intnat caml_int64_compare_unboxed(int64_t i1, int64_t i2)
+{
+ return (i1 > i2) - (i1 < i2);
+}
+
+CAMLprim value caml_int64_compare(value v1, value v2)
+{
+ return Val_int(caml_int64_compare_unboxed(Int64_val(v1),Int64_val(v2)));
+}
+
+CAMLprim value caml_int64_format(value fmt, value arg)
+{
+ char format_string[FORMAT_BUFFER_SIZE];
+
+ parse_format(fmt, ARCH_INT64_PRINTF_FORMAT, format_string);
+ return caml_alloc_sprintf(format_string, Int64_val(arg));
+}
+
+CAMLprim value caml_int64_of_string(value s)
+{
+ const char * p;
+ uint64_t res, threshold;
+ int sign, base, signedness, d;
+
+ p = parse_sign_and_base(String_val(s), &base, &signedness, &sign);
+ threshold = ((uint64_t) -1) / base;
+ d = parse_digit(*p);
+ if (d < 0 || d >= base) caml_failwith(INT64_ERRMSG);
+ res = d;
+ for (p++; /*nothing*/; p++) {
+ char c = *p;
+ if (c == '_') continue;
+ d = parse_digit(c);
+ if (d < 0 || d >= base) break;
+ /* Detect overflow in multiplication base * res */
+ if (res > threshold) caml_failwith(INT64_ERRMSG);
+ res = base * res + d;
+ /* Detect overflow in addition (base * res) + d */
+ if (res < (uint64_t) d) caml_failwith(INT64_ERRMSG);
+ }
+ if (p != String_val(s) + caml_string_length(s)){
+ caml_failwith(INT64_ERRMSG);
+ }
+ if (signedness) {
+ /* Signed representation expected, allow -2^63 to 2^63 - 1 only */
+ if (sign >= 0) {
+ if (res >= (uint64_t)1 << 63) caml_failwith(INT64_ERRMSG);
+ } else {
+ if (res > (uint64_t)1 << 63) caml_failwith(INT64_ERRMSG);
+ }
+ }
+ if (sign < 0) res = - res;
+ return caml_copy_int64(res);
+}
+
+int64_t caml_int64_bits_of_float_unboxed(double d)
+{
+ union { double d; int64_t i; int32_t h[2]; } u;
+ u.d = d;
+#if defined(__arm__) && !defined(__ARM_EABI__)
+ { int32_t t = u.h[0]; u.h[0] = u.h[1]; u.h[1] = t; }
+#endif
+ return u.i;
+}
+
+double caml_int64_float_of_bits_unboxed(int64_t i)
+{
+ union { double d; int64_t i; int32_t h[2]; } u;
+ u.i = i;
+#if defined(__arm__) && !defined(__ARM_EABI__)
+ { int32_t t = u.h[0]; u.h[0] = u.h[1]; u.h[1] = t; }
+#endif
+ return u.d;
+}
+
+CAMLprim value caml_int64_bits_of_float(value vd)
+{
+ return caml_copy_int64(caml_int64_bits_of_float_unboxed(Double_val(vd)));
+}
+
+CAMLprim value caml_int64_float_of_bits(value vi)
+{
+ return caml_copy_double(caml_int64_float_of_bits_unboxed(Int64_val(vi)));
+}
+
+/* Native integers */
+
+static int nativeint_cmp(value v1, value v2)
+{
+ intnat i1 = Nativeint_val(v1);
+ intnat i2 = Nativeint_val(v2);
+ return (i1 > i2) - (i1 < i2);
+}
+
+static intnat nativeint_hash(value v)
+{
+ intnat n = Nativeint_val(v);
+#ifdef ARCH_SIXTYFOUR
+ /* 32/64 bits compatibility trick. See explanations in file "hash.c",
+ function caml_hash_mix_intnat. */
+ return (n >> 32) ^ (n >> 63) ^ n;
+#else
+ return n;
+#endif
+}
+
+static void nativeint_serialize(value v, uintnat * bsize_32,
+ uintnat * bsize_64)
+{
+ intnat l = Nativeint_val(v);
+#ifdef ARCH_SIXTYFOUR
+ if (l >= -((intnat)1 << 31) && l < ((intnat)1 << 31)) {
+ caml_serialize_int_1(1);
+ caml_serialize_int_4((int32_t) l);
+ } else {
+ caml_serialize_int_1(2);
+ caml_serialize_int_8(l);
+ }
+#else
+ caml_serialize_int_1(1);
+ caml_serialize_int_4(l);
+#endif
+ *bsize_32 = 4;
+ *bsize_64 = 8;
+}
+
+static uintnat nativeint_deserialize(void * dst)
+{
+ switch (caml_deserialize_uint_1()) {
+ case 1:
+ *((intnat *) dst) = caml_deserialize_sint_4();
+ break;
+ case 2:
+#ifdef ARCH_SIXTYFOUR
+ *((intnat *) dst) = caml_deserialize_sint_8();
+#else
+ caml_deserialize_error("input_value: native integer value too large");
+#endif
+ break;
+ default:
+ caml_deserialize_error("input_value: ill-formed native integer");
+ }
+ return sizeof(intnat);
+}
+
+CAMLexport struct custom_operations caml_nativeint_ops = {
+ "_n",
+ custom_finalize_default,
+ nativeint_cmp,
+ nativeint_hash,
+ nativeint_serialize,
+ nativeint_deserialize,
+ custom_compare_ext_default
+};
+
+CAMLexport value caml_copy_nativeint(intnat i)
+{
+ value res = caml_alloc_custom(&caml_nativeint_ops, sizeof(intnat), 0, 1);
+ Nativeint_val(res) = i;
+ return res;
+}
+
+CAMLprim value caml_nativeint_neg(value v)
+{ return caml_copy_nativeint(- Nativeint_val(v)); }
+
+CAMLprim value caml_nativeint_add(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) + Nativeint_val(v2)); }
+
+CAMLprim value caml_nativeint_sub(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) - Nativeint_val(v2)); }
+
+CAMLprim value caml_nativeint_mul(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) * Nativeint_val(v2)); }
+
+#define Nativeint_min_int ((intnat) 1 << (sizeof(intnat) * 8 - 1))
+
+CAMLprim value caml_nativeint_div(value v1, value v2)
+{
+ intnat dividend = Nativeint_val(v1);
+ intnat divisor = Nativeint_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, modulus crashes if division overflows.
+ Implement the same behavior as for type "int". */
+ if (dividend == Nativeint_min_int && divisor == -1) return v1;
+ return caml_copy_nativeint(dividend / divisor);
+}
+
+CAMLprim value caml_nativeint_mod(value v1, value v2)
+{
+ intnat dividend = Nativeint_val(v1);
+ intnat divisor = Nativeint_val(v2);
+ if (divisor == 0) caml_raise_zero_divide();
+ /* PR#4740: on some processors, modulus crashes if division overflows.
+ Implement the same behavior as for type "int". */
+ if (dividend == Nativeint_min_int && divisor == -1){
+ return caml_copy_nativeint(0);
+ }
+ return caml_copy_nativeint(dividend % divisor);
+}
+
+CAMLprim value caml_nativeint_and(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) & Nativeint_val(v2)); }
+
+CAMLprim value caml_nativeint_or(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) | Nativeint_val(v2)); }
+
+CAMLprim value caml_nativeint_xor(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) ^ Nativeint_val(v2)); }
+
+CAMLprim value caml_nativeint_shift_left(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) << Int_val(v2)); }
+
+CAMLprim value caml_nativeint_shift_right(value v1, value v2)
+{ return caml_copy_nativeint(Nativeint_val(v1) >> Int_val(v2)); }
+
+CAMLprim value caml_nativeint_shift_right_unsigned(value v1, value v2)
+{ return caml_copy_nativeint((uintnat)Nativeint_val(v1) >> Int_val(v2)); }
+
+value caml_nativeint_direct_bswap(value v)
+{
+#ifdef ARCH_SIXTYFOUR
+ return caml_swap64(v);
+#else
+ return caml_swap32(v);
+#endif
+}
+
+CAMLprim value caml_nativeint_bswap(value v)
+{
+#ifdef ARCH_SIXTYFOUR
+ return caml_copy_nativeint(caml_swap64(Nativeint_val(v)));
+#else
+ return caml_copy_nativeint(caml_swap32(Nativeint_val(v)));
+#endif
+}
+
+CAMLprim value caml_nativeint_of_int(value v)
+{ return caml_copy_nativeint(Long_val(v)); }
+
+CAMLprim value caml_nativeint_to_int(value v)
+{ return Val_long(Nativeint_val(v)); }
+
+intnat caml_nativeint_of_float_unboxed(double x)
+{ return x; }
+
+CAMLprim value caml_nativeint_of_float(value v)
+{ return caml_copy_nativeint((intnat)(Double_val(v))); }
+
+double caml_nativeint_to_float_unboxed(intnat x)
+{ return x; }
+
+CAMLprim value caml_nativeint_to_float(value v)
+{ return caml_copy_double((double)(Nativeint_val(v))); }
+
+CAMLprim value caml_nativeint_of_int32(value v)
+{ return caml_copy_nativeint(Int32_val(v)); }
+
+CAMLprim value caml_nativeint_to_int32(value v)
+{ return caml_copy_int32(Nativeint_val(v)); }
+
+intnat caml_nativeint_compare_unboxed(intnat i1, intnat i2)
+{
+ return (i1 > i2) - (i1 < i2);
+}
+
+CAMLprim value caml_nativeint_compare(value v1, value v2)
+{
+ return Val_int(caml_nativeint_compare_unboxed(Nativeint_val(v1),
+ Nativeint_val(v2)));
+}
+
+CAMLprim value caml_nativeint_format(value fmt, value arg)
+{
+ char format_string[FORMAT_BUFFER_SIZE];
+
+ parse_format(fmt, ARCH_INTNAT_PRINTF_FORMAT, format_string);
+ return caml_alloc_sprintf(format_string, Nativeint_val(arg));
+}
+
+CAMLprim value caml_nativeint_of_string(value s)
+{
+ return caml_copy_nativeint(parse_intnat(s, 8 * sizeof(value), INTNAT_ERRMSG));
+}
diff --git a/test/monniaux/ocaml/byterun/io.c b/test/monniaux/ocaml/byterun/io.c
new file mode 100644
index 00000000..d124b56a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/io.c
@@ -0,0 +1,828 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Buffered input/output. */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include "caml/config.h"
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#ifdef __CYGWIN__
+#include </usr/include/io.h>
+#endif
+#include "caml/alloc.h"
+#include "caml/custom.h"
+#include "caml/fail.h"
+#include "caml/io.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/osdeps.h"
+#include "caml/signals.h"
+#include "caml/sys.h"
+
+#ifndef SEEK_SET
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+#endif
+
+/* Hooks for locking channels */
+
+CAMLexport void (*caml_channel_mutex_free) (struct channel *) = NULL;
+CAMLexport void (*caml_channel_mutex_lock) (struct channel *) = NULL;
+CAMLexport void (*caml_channel_mutex_unlock) (struct channel *) = NULL;
+CAMLexport void (*caml_channel_mutex_unlock_exn) (void) = NULL;
+
+/* List of opened channels */
+CAMLexport struct channel * caml_all_opened_channels = NULL;
+
+/* Basic functions over type struct channel *.
+ These functions can be called directly from C.
+ No locking is performed. */
+
+/* Functions shared between input and output */
+
+CAMLexport struct channel * caml_open_descriptor_in(int fd)
+{
+ struct channel * channel;
+
+ channel = (struct channel *) caml_stat_alloc(sizeof(struct channel));
+ channel->fd = fd;
+ caml_enter_blocking_section();
+ channel->offset = lseek(fd, 0, SEEK_CUR);
+ caml_leave_blocking_section();
+ channel->curr = channel->max = channel->buff;
+ channel->end = channel->buff + IO_BUFFER_SIZE;
+ channel->mutex = NULL;
+ channel->revealed = 0;
+ channel->old_revealed = 0;
+ channel->refcount = 0;
+ channel->flags = 0;
+ channel->next = caml_all_opened_channels;
+ channel->prev = NULL;
+ channel->name = NULL;
+ if (caml_all_opened_channels != NULL)
+ caml_all_opened_channels->prev = channel;
+ caml_all_opened_channels = channel;
+ return channel;
+}
+
+CAMLexport struct channel * caml_open_descriptor_out(int fd)
+{
+ struct channel * channel;
+
+ channel = caml_open_descriptor_in(fd);
+ channel->max = NULL;
+ return channel;
+}
+
+static void unlink_channel(struct channel *channel)
+{
+ if (channel->prev == NULL) {
+ CAMLassert (channel == caml_all_opened_channels);
+ caml_all_opened_channels = caml_all_opened_channels->next;
+ if (caml_all_opened_channels != NULL)
+ caml_all_opened_channels->prev = NULL;
+ } else {
+ channel->prev->next = channel->next;
+ if (channel->next != NULL) channel->next->prev = channel->prev;
+ }
+}
+
+CAMLexport void caml_close_channel(struct channel *channel)
+{
+ CAML_SYS_CLOSE(channel->fd);
+ if (channel->refcount > 0) return;
+ if (caml_channel_mutex_free != NULL) (*caml_channel_mutex_free)(channel);
+ unlink_channel(channel);
+ caml_stat_free(channel->name);
+ caml_stat_free(channel);
+}
+
+CAMLexport file_offset caml_channel_size(struct channel *channel)
+{
+ file_offset offset;
+ file_offset end;
+ int fd;
+
+ /* We extract data from [channel] before dropping the OCaml lock, in case
+ someone else touches the block. */
+ fd = channel->fd;
+ offset = channel->offset;
+ caml_enter_blocking_section();
+ end = lseek(fd, 0, SEEK_END);
+ if (end == -1 || lseek(fd, offset, SEEK_SET) != offset) {
+ caml_leave_blocking_section();
+ caml_sys_error(NO_ARG);
+ }
+ caml_leave_blocking_section();
+ return end;
+}
+
+CAMLexport int caml_channel_binary_mode(struct channel *channel)
+{
+#if defined(_WIN32) || defined(__CYGWIN__)
+ int oldmode = setmode(channel->fd, O_BINARY);
+ if (oldmode == O_TEXT) setmode(channel->fd, O_TEXT);
+ return oldmode == O_BINARY;
+#else
+ return 1;
+#endif
+}
+
+/* Output */
+
+/* Attempt to flush the buffer. This will make room in the buffer for
+ at least one character. Returns true if the buffer is empty at the
+ end of the flush, or false if some data remains in the buffer.
+ */
+
+CAMLexport int caml_flush_partial(struct channel *channel)
+{
+ int towrite, written;
+
+ towrite = channel->curr - channel->buff;
+ CAMLassert (towrite >= 0);
+ if (towrite > 0) {
+ written = caml_write_fd(channel->fd, channel->flags,
+ channel->buff, towrite);
+ channel->offset += written;
+ if (written < towrite)
+ memmove(channel->buff, channel->buff + written, towrite - written);
+ channel->curr -= written;
+ }
+ return (channel->curr == channel->buff);
+}
+
+/* Flush completely the buffer. */
+
+CAMLexport void caml_flush(struct channel *channel)
+{
+ while (! caml_flush_partial(channel)) /*nothing*/;
+}
+
+/* Output data */
+
+CAMLexport void caml_putword(struct channel *channel, uint32_t w)
+{
+ if (! caml_channel_binary_mode(channel))
+ caml_failwith("output_binary_int: not a binary channel");
+ caml_putch(channel, w >> 24);
+ caml_putch(channel, w >> 16);
+ caml_putch(channel, w >> 8);
+ caml_putch(channel, w);
+}
+
+CAMLexport int caml_putblock(struct channel *channel, char *p, intnat len)
+{
+ int n, free, towrite, written;
+
+ n = len >= INT_MAX ? INT_MAX : (int) len;
+ free = channel->end - channel->curr;
+ if (n < free) {
+ /* Write request small enough to fit in buffer: transfer to buffer. */
+ memmove(channel->curr, p, n);
+ channel->curr += n;
+ return n;
+ } else {
+ /* Write request overflows buffer (or just fills it up): transfer whatever
+ fits to buffer and write the buffer */
+ memmove(channel->curr, p, free);
+ towrite = channel->end - channel->buff;
+ written = caml_write_fd(channel->fd, channel->flags,
+ channel->buff, towrite);
+ if (written < towrite)
+ memmove(channel->buff, channel->buff + written, towrite - written);
+ channel->offset += written;
+ channel->curr = channel->end - written;
+ return free;
+ }
+}
+
+CAMLexport void caml_really_putblock(struct channel *channel,
+ char *p, intnat len)
+{
+ int written;
+ while (len > 0) {
+ written = caml_putblock(channel, p, len);
+ p += written;
+ len -= written;
+ }
+}
+
+CAMLexport void caml_seek_out(struct channel *channel, file_offset dest)
+{
+ caml_flush(channel);
+ caml_enter_blocking_section();
+ if (lseek(channel->fd, dest, SEEK_SET) != dest) {
+ caml_leave_blocking_section();
+ caml_sys_error(NO_ARG);
+ }
+ caml_leave_blocking_section();
+ channel->offset = dest;
+}
+
+CAMLexport file_offset caml_pos_out(struct channel *channel)
+{
+ return channel->offset + (file_offset)(channel->curr - channel->buff);
+}
+
+/* Input */
+
+/* caml_do_read is exported for Cash */
+CAMLexport int caml_do_read(int fd, char *p, unsigned int n)
+{
+ return caml_read_fd(fd, 0, p, n);
+}
+
+CAMLexport unsigned char caml_refill(struct channel *channel)
+{
+ int n;
+
+ n = caml_read_fd(channel->fd, channel->flags,
+ channel->buff, channel->end - channel->buff);
+ if (n == 0) caml_raise_end_of_file();
+ channel->offset += n;
+ channel->max = channel->buff + n;
+ channel->curr = channel->buff + 1;
+ return (unsigned char)(channel->buff[0]);
+}
+
+CAMLexport uint32_t caml_getword(struct channel *channel)
+{
+ int i;
+ uint32_t res;
+
+ if (! caml_channel_binary_mode(channel))
+ caml_failwith("input_binary_int: not a binary channel");
+ res = 0;
+ for(i = 0; i < 4; i++) {
+ res = (res << 8) + caml_getch(channel);
+ }
+ return res;
+}
+
+CAMLexport int caml_getblock(struct channel *channel, char *p, intnat len)
+{
+ int n, avail, nread;
+
+ n = len >= INT_MAX ? INT_MAX : (int) len;
+ avail = channel->max - channel->curr;
+ if (n <= avail) {
+ memmove(p, channel->curr, n);
+ channel->curr += n;
+ return n;
+ } else if (avail > 0) {
+ memmove(p, channel->curr, avail);
+ channel->curr += avail;
+ return avail;
+ } else {
+ nread = caml_read_fd(channel->fd, channel->flags, channel->buff,
+ channel->end - channel->buff);
+ channel->offset += nread;
+ channel->max = channel->buff + nread;
+ if (n > nread) n = nread;
+ memmove(p, channel->buff, n);
+ channel->curr = channel->buff + n;
+ return n;
+ }
+}
+
+/* Returns the number of bytes read. */
+CAMLexport intnat caml_really_getblock(struct channel *chan, char *p, intnat n)
+{
+ intnat k = n;
+ int r;
+ while (k > 0) {
+ r = caml_getblock(chan, p, k);
+ if (r == 0) break;
+ p += r;
+ k -= r;
+ }
+ return n - k;
+}
+
+CAMLexport void caml_seek_in(struct channel *channel, file_offset dest)
+{
+ if (dest >= channel->offset - (channel->max - channel->buff) &&
+ dest <= channel->offset) {
+ channel->curr = channel->max - (channel->offset - dest);
+ } else {
+ caml_enter_blocking_section();
+ if (lseek(channel->fd, dest, SEEK_SET) != dest) {
+ caml_leave_blocking_section();
+ caml_sys_error(NO_ARG);
+ }
+ caml_leave_blocking_section();
+ channel->offset = dest;
+ channel->curr = channel->max = channel->buff;
+ }
+}
+
+CAMLexport file_offset caml_pos_in(struct channel *channel)
+{
+ return channel->offset - (file_offset)(channel->max - channel->curr);
+}
+
+CAMLexport intnat caml_input_scan_line(struct channel *channel)
+{
+ char * p;
+ int n;
+
+ p = channel->curr;
+ do {
+ if (p >= channel->max) {
+ /* No more characters available in the buffer */
+ if (channel->curr > channel->buff) {
+ /* Try to make some room in the buffer by shifting the unread
+ portion at the beginning */
+ memmove(channel->buff, channel->curr, channel->max - channel->curr);
+ n = channel->curr - channel->buff;
+ channel->curr -= n;
+ channel->max -= n;
+ p -= n;
+ }
+ if (channel->max >= channel->end) {
+ /* Buffer is full, no room to read more characters from the input.
+ Return the number of characters in the buffer, with negative
+ sign to indicate that no newline was encountered. */
+ return -(channel->max - channel->curr);
+ }
+ /* Fill the buffer as much as possible */
+ n = caml_read_fd(channel->fd, channel->flags,
+ channel->max, channel->end - channel->max);
+ if (n == 0) {
+ /* End-of-file encountered. Return the number of characters in the
+ buffer, with negative sign since we haven't encountered
+ a newline. */
+ return -(channel->max - channel->curr);
+ }
+ channel->offset += n;
+ channel->max += n;
+ }
+ } while (*p++ != '\n');
+ /* Found a newline. Return the length of the line, newline included. */
+ return (p - channel->curr);
+}
+
+/* OCaml entry points for the I/O functions. Wrap struct channel *
+ objects into a heap-allocated object. Perform locking
+ and unlocking around the I/O operations. */
+
+/* FIXME CAMLexport, but not in io.h exported for Cash ? */
+CAMLexport void caml_finalize_channel(value vchan)
+{
+ struct channel * chan = Channel(vchan);
+ if ((chan->flags & CHANNEL_FLAG_MANAGED_BY_GC) == 0) return;
+ if (--chan->refcount > 0) return;
+ if (caml_channel_mutex_free != NULL) (*caml_channel_mutex_free)(chan);
+
+ if (chan->fd != -1 && chan->name && caml_runtime_warnings_active())
+ fprintf(stderr,
+ "[ocaml] channel opened on file '%s' dies without being closed\n",
+ chan->name
+ );
+
+ if (chan->max == NULL && chan->curr != chan->buff){
+ /*
+ This is an unclosed out channel (chan->max == NULL) with a
+ non-empty buffer: keep it around so the OCaml [at_exit] function
+ gets a chance to flush it. We would want to simply flush the
+ channel now, but (i) flushing can raise exceptions, and (ii) it
+ is potentially a blocking operation. Both are forbidden in a
+ finalization function.
+
+ Refs:
+ http://caml.inria.fr/mantis/view.php?id=6902
+ https://github.com/ocaml/ocaml/pull/210
+ */
+ if (chan->name && caml_runtime_warnings_active())
+ fprintf(stderr,
+ "[ocaml] (moreover, it has unflushed data)\n"
+ );
+ } else {
+ unlink_channel(chan);
+ caml_stat_free(chan->name);
+ caml_stat_free(chan);
+ }
+}
+
+static int compare_channel(value vchan1, value vchan2)
+{
+ struct channel * chan1 = Channel(vchan1);
+ struct channel * chan2 = Channel(vchan2);
+ return (chan1 == chan2) ? 0 : (chan1 < chan2) ? -1 : 1;
+}
+
+static intnat hash_channel(value vchan)
+{
+ return (intnat) (Channel(vchan));
+}
+
+static struct custom_operations channel_operations = {
+ "_chan",
+ caml_finalize_channel,
+ compare_channel,
+ hash_channel,
+ custom_serialize_default,
+ custom_deserialize_default,
+ custom_compare_ext_default
+};
+
+CAMLexport value caml_alloc_channel(struct channel *chan)
+{
+ value res;
+ chan->refcount++; /* prevent finalization during next alloc */
+ res = caml_alloc_custom(&channel_operations, sizeof(struct channel *),
+ 1, 1000);
+ Channel(res) = chan;
+ return res;
+}
+
+CAMLprim value caml_ml_open_descriptor_in(value fd)
+{
+ struct channel * chan = caml_open_descriptor_in(Int_val(fd));
+ chan->flags |= CHANNEL_FLAG_MANAGED_BY_GC;
+ return caml_alloc_channel(chan);
+}
+
+CAMLprim value caml_ml_open_descriptor_out(value fd)
+{
+ struct channel * chan = caml_open_descriptor_out(Int_val(fd));
+ chan->flags |= CHANNEL_FLAG_MANAGED_BY_GC;
+ return caml_alloc_channel(chan);
+}
+
+CAMLprim value caml_ml_set_channel_name(value vchannel, value vname)
+{
+ struct channel * channel = Channel(vchannel);
+ caml_stat_free(channel->name);
+ if (caml_string_length(vname) > 0)
+ channel->name = caml_stat_strdup(String_val(vname));
+ else
+ channel->name = NULL;
+ return Val_unit;
+}
+
+#define Pair_tag 0
+
+CAMLprim value caml_ml_out_channels_list (value unit)
+{
+ CAMLparam0 ();
+ CAMLlocal3 (res, tail, chan);
+ struct channel * channel;
+
+ res = Val_emptylist;
+ for (channel = caml_all_opened_channels;
+ channel != NULL;
+ channel = channel->next)
+ /* Testing channel->fd >= 0 looks unnecessary, as
+ caml_ml_close_channel changes max when setting fd to -1. */
+ if (channel->max == NULL) {
+ chan = caml_alloc_channel (channel);
+ tail = res;
+ res = caml_alloc_small (2, Pair_tag);
+ Field (res, 0) = chan;
+ Field (res, 1) = tail;
+ }
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_channel_descriptor(value vchannel)
+{
+ int fd = Channel(vchannel)->fd;
+ if (fd == -1) { errno = EBADF; caml_sys_error(NO_ARG); }
+ return Val_int(fd);
+}
+
+CAMLprim value caml_ml_close_channel(value vchannel)
+{
+ int result;
+ int do_syscall;
+ int fd;
+
+ /* For output channels, must have flushed before */
+ struct channel * channel = Channel(vchannel);
+ if (channel->fd != -1){
+ fd = channel->fd;
+ channel->fd = -1;
+ do_syscall = 1;
+ }else{
+ do_syscall = 0;
+ result = 0;
+ }
+ /* Ensure that every read or write on the channel will cause an
+ immediate caml_flush_partial or caml_refill, thus raising a Sys_error
+ exception */
+ channel->curr = channel->max = channel->end;
+
+ if (do_syscall) {
+ caml_enter_blocking_section();
+ result = CAML_SYS_CLOSE(fd);
+ caml_leave_blocking_section();
+ }
+
+ if (result == -1) caml_sys_error (NO_ARG);
+ return Val_unit;
+}
+
+/* EOVERFLOW is the Unix98 error indicating that a file position or file
+ size is not representable.
+ ERANGE is the ANSI C error indicating that some argument to some
+ function is out of range. This is less precise than EOVERFLOW,
+ but guaranteed to be defined on all ANSI C environments. */
+#ifndef EOVERFLOW
+#define EOVERFLOW ERANGE
+#endif
+
+CAMLprim value caml_ml_channel_size(value vchannel)
+{
+ file_offset size = caml_channel_size(Channel(vchannel));
+ if (size > Max_long) { errno = EOVERFLOW; caml_sys_error(NO_ARG); }
+ return Val_long(size);
+}
+
+CAMLprim value caml_ml_channel_size_64(value vchannel)
+{
+ return Val_file_offset(caml_channel_size(Channel(vchannel)));
+}
+
+CAMLprim value caml_ml_set_binary_mode(value vchannel, value mode)
+{
+#if defined(_WIN32) || defined(__CYGWIN__)
+ struct channel * channel = Channel(vchannel);
+#if defined(_WIN32)
+ /* The implementation of [caml_read_fd] and [caml_write_fd] in win32.c
+ doesn't support socket I/O with CRLF conversion. */
+ if ((channel->flags & CHANNEL_FLAG_FROM_SOCKET) != 0
+ && ! Bool_val(mode)) {
+ errno = EINVAL;
+ caml_sys_error(NO_ARG);
+ }
+#endif
+ if (setmode(channel->fd, Bool_val(mode) ? O_BINARY : O_TEXT) == -1)
+ caml_sys_error(NO_ARG);
+#endif
+ return Val_unit;
+}
+
+/*
+ If the channel is closed, DO NOT raise a "bad file descriptor"
+ exception, but do nothing (the buffer is already empty).
+ This is because some libraries will flush at exit, even on
+ file descriptors that may be closed.
+*/
+
+CAMLprim value caml_ml_flush_partial(value vchannel)
+{
+ CAMLparam1 (vchannel);
+ struct channel * channel = Channel(vchannel);
+ int res;
+
+ if (channel->fd == -1) CAMLreturn(Val_true);
+ Lock(channel);
+ res = caml_flush_partial(channel);
+ Unlock(channel);
+ CAMLreturn (Val_bool(res));
+}
+
+CAMLprim value caml_ml_flush(value vchannel)
+{
+ CAMLparam1 (vchannel);
+ struct channel * channel = Channel(vchannel);
+
+ if (channel->fd == -1) CAMLreturn(Val_unit);
+ Lock(channel);
+ caml_flush(channel);
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_output_char(value vchannel, value ch)
+{
+ CAMLparam2 (vchannel, ch);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_putch(channel, Long_val(ch));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_output_int(value vchannel, value w)
+{
+ CAMLparam2 (vchannel, w);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_putword(channel, Long_val(w));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_output_partial(value vchannel, value buff, value start,
+ value length)
+{
+ CAMLparam4 (vchannel, buff, start, length);
+ struct channel * channel = Channel(vchannel);
+ int res;
+
+ Lock(channel);
+ res = caml_putblock(channel, &Byte(buff, Long_val(start)), Long_val(length));
+ Unlock(channel);
+ CAMLreturn (Val_int(res));
+}
+
+CAMLprim value caml_ml_output_bytes(value vchannel, value buff, value start,
+ value length)
+{
+ CAMLparam4 (vchannel, buff, start, length);
+ struct channel * channel = Channel(vchannel);
+ intnat pos = Long_val(start);
+ intnat len = Long_val(length);
+
+ Lock(channel);
+ /* We cannot call caml_really_putblock here because buff may move
+ during caml_write_fd */
+ while (len > 0) {
+ int written = caml_putblock(channel, &Byte(buff, pos), len);
+ pos += written;
+ len -= written;
+ }
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_output(value vchannel, value buff, value start,
+ value length)
+{
+ return caml_ml_output_bytes (vchannel, buff, start, length);
+}
+
+CAMLprim value caml_ml_seek_out(value vchannel, value pos)
+{
+ CAMLparam2 (vchannel, pos);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_seek_out(channel, Long_val(pos));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_seek_out_64(value vchannel, value pos)
+{
+ CAMLparam2 (vchannel, pos);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_seek_out(channel, File_offset_val(pos));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_pos_out(value vchannel)
+{
+ file_offset pos = caml_pos_out(Channel(vchannel));
+ if (pos > Max_long) { errno = EOVERFLOW; caml_sys_error(NO_ARG); }
+ return Val_long(pos);
+}
+
+CAMLprim value caml_ml_pos_out_64(value vchannel)
+{
+ return Val_file_offset(caml_pos_out(Channel(vchannel)));
+}
+
+CAMLprim value caml_ml_input_char(value vchannel)
+{
+ CAMLparam1 (vchannel);
+ struct channel * channel = Channel(vchannel);
+ unsigned char c;
+
+ Lock(channel);
+ c = caml_getch(channel);
+ Unlock(channel);
+ CAMLreturn (Val_long(c));
+}
+
+CAMLprim value caml_ml_input_int(value vchannel)
+{
+ CAMLparam1 (vchannel);
+ struct channel * channel = Channel(vchannel);
+ intnat i;
+
+ Lock(channel);
+ i = caml_getword(channel);
+ Unlock(channel);
+#ifdef ARCH_SIXTYFOUR
+ i = (i << 32) >> 32; /* Force sign extension */
+#endif
+ CAMLreturn (Val_long(i));
+}
+
+CAMLprim value caml_ml_input(value vchannel, value buff, value vstart,
+ value vlength)
+{
+ CAMLparam4 (vchannel, buff, vstart, vlength);
+ struct channel * channel = Channel(vchannel);
+ intnat start, len;
+ int n, avail, nread;
+
+ Lock(channel);
+ /* We cannot call caml_getblock here because buff may move during
+ caml_read_fd */
+ start = Long_val(vstart);
+ len = Long_val(vlength);
+ n = len >= INT_MAX ? INT_MAX : (int) len;
+ avail = channel->max - channel->curr;
+ if (n <= avail) {
+ memmove(&Byte(buff, start), channel->curr, n);
+ channel->curr += n;
+ } else if (avail > 0) {
+ memmove(&Byte(buff, start), channel->curr, avail);
+ channel->curr += avail;
+ n = avail;
+ } else {
+ nread = caml_read_fd(channel->fd, channel->flags, channel->buff,
+ channel->end - channel->buff);
+ channel->offset += nread;
+ channel->max = channel->buff + nread;
+ if (n > nread) n = nread;
+ memmove(&Byte(buff, start), channel->buff, n);
+ channel->curr = channel->buff + n;
+ }
+ Unlock(channel);
+ CAMLreturn (Val_long(n));
+}
+
+CAMLprim value caml_ml_seek_in(value vchannel, value pos)
+{
+ CAMLparam2 (vchannel, pos);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_seek_in(channel, Long_val(pos));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_seek_in_64(value vchannel, value pos)
+{
+ CAMLparam2 (vchannel, pos);
+ struct channel * channel = Channel(vchannel);
+
+ Lock(channel);
+ caml_seek_in(channel, File_offset_val(pos));
+ Unlock(channel);
+ CAMLreturn (Val_unit);
+}
+
+CAMLprim value caml_ml_pos_in(value vchannel)
+{
+ file_offset pos = caml_pos_in(Channel(vchannel));
+ if (pos > Max_long) { errno = EOVERFLOW; caml_sys_error(NO_ARG); }
+ return Val_long(pos);
+}
+
+CAMLprim value caml_ml_pos_in_64(value vchannel)
+{
+ return Val_file_offset(caml_pos_in(Channel(vchannel)));
+}
+
+CAMLprim value caml_ml_input_scan_line(value vchannel)
+{
+ CAMLparam1 (vchannel);
+ struct channel * channel = Channel(vchannel);
+ intnat res;
+
+ Lock(channel);
+ res = caml_input_scan_line(channel);
+ Unlock(channel);
+ CAMLreturn (Val_long(res));
+}
+
+CAMLprim value caml_terminfo_rows(value vchannel)
+{
+ return Val_int(caml_num_rows_fd(Channel(vchannel)->fd));
+}
diff --git a/test/monniaux/ocaml/byterun/lexing.c b/test/monniaux/ocaml/byterun/lexing.c
new file mode 100644
index 00000000..b1049904
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/lexing.c
@@ -0,0 +1,233 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* The table-driven automaton for lexers generated by camllex. */
+
+#include "caml/fail.h"
+#include "caml/mlvalues.h"
+#include "caml/stacks.h"
+
+struct lexer_buffer {
+ value refill_buff;
+ value lex_buffer;
+ value lex_buffer_len;
+ value lex_abs_pos;
+ value lex_start_pos;
+ value lex_curr_pos;
+ value lex_last_pos;
+ value lex_last_action;
+ value lex_eof_reached;
+ value lex_mem;
+ value lex_start_p;
+ value lex_curr_p;
+};
+
+struct lexing_table {
+ value lex_base;
+ value lex_backtrk;
+ value lex_default;
+ value lex_trans;
+ value lex_check;
+ value lex_base_code;
+ value lex_backtrk_code;
+ value lex_default_code;
+ value lex_trans_code;
+ value lex_check_code;
+ value lex_code;
+};
+
+#if defined(ARCH_BIG_ENDIAN) || SIZEOF_SHORT != 2
+#define Short(tbl,n) \
+ (*((unsigned char *)((tbl) + (n) * 2)) + \
+ (*((signed char *)((tbl) + (n) * 2 + 1)) << 8))
+#else
+#define Short(tbl,n) (((short *)(tbl))[(n)])
+#endif
+
+CAMLprim value caml_lex_engine(struct lexing_table *tbl, value start_state,
+ struct lexer_buffer *lexbuf)
+{
+ int state, base, backtrk, c;
+
+ state = Int_val(start_state);
+ if (state >= 0) {
+ /* First entry */
+ lexbuf->lex_last_pos = lexbuf->lex_start_pos = lexbuf->lex_curr_pos;
+ lexbuf->lex_last_action = Val_int(-1);
+ } else {
+ /* Reentry after refill */
+ state = -state - 1;
+ }
+ while(1) {
+ /* Lookup base address or action number for current state */
+ base = Short(tbl->lex_base, state);
+ if (base < 0) return Val_int(-base-1);
+ /* See if it's a backtrack point */
+ backtrk = Short(tbl->lex_backtrk, state);
+ if (backtrk >= 0) {
+ lexbuf->lex_last_pos = lexbuf->lex_curr_pos;
+ lexbuf->lex_last_action = Val_int(backtrk);
+ }
+ /* See if we need a refill */
+ if (lexbuf->lex_curr_pos >= lexbuf->lex_buffer_len){
+ if (lexbuf->lex_eof_reached == Val_bool (0)){
+ return Val_int(-state - 1);
+ }else{
+ c = 256;
+ }
+ }else{
+ /* Read next input char */
+ c = Byte_u(lexbuf->lex_buffer, Long_val(lexbuf->lex_curr_pos));
+ lexbuf->lex_curr_pos += 2;
+ }
+ /* Determine next state */
+ if (Short(tbl->lex_check, base + c) == state)
+ state = Short(tbl->lex_trans, base + c);
+ else
+ state = Short(tbl->lex_default, state);
+ /* If no transition on this char, return to last backtrack point */
+ if (state < 0) {
+ lexbuf->lex_curr_pos = lexbuf->lex_last_pos;
+ if (lexbuf->lex_last_action == Val_int(-1)) {
+ caml_failwith("lexing: empty token");
+ } else {
+ return lexbuf->lex_last_action;
+ }
+ }else{
+ /* Erase the EOF condition only if the EOF pseudo-character was
+ consumed by the automaton (i.e. there was no backtrack above)
+ */
+ if (c == 256) lexbuf->lex_eof_reached = Val_bool (0);
+ }
+ }
+}
+
+/***********************************************/
+/* New lexer engine, with memory of positions */
+/***********************************************/
+
+static void run_mem(char *pc, value mem, value curr_pos) {
+ for (;;) {
+ unsigned char dst, src ;
+
+ dst = *pc++ ;
+ if (dst == 0xff)
+ return ;
+ src = *pc++ ;
+ if (src == 0xff) {
+ /* fprintf(stderr,"[%hhu] <- %d\n",dst,Int_val(curr_pos)) ;*/
+ Field(mem,dst) = curr_pos ;
+ } else {
+ /* fprintf(stderr,"[%hhu] <- [%hhu]\n",dst,src) ; */
+ Field(mem,dst) = Field(mem,src) ;
+ }
+ }
+}
+
+static void run_tag(char *pc, value mem) {
+ for (;;) {
+ unsigned char dst, src ;
+
+ dst = *pc++ ;
+ if (dst == 0xff)
+ return ;
+ src = *pc++ ;
+ if (src == 0xff) {
+ /* fprintf(stderr,"[%hhu] <- -1\n",dst) ; */
+ Field(mem,dst) = Val_int(-1) ;
+ } else {
+ /* fprintf(stderr,"[%hhu] <- [%hhu]\n",dst,src) ; */
+ Field(mem,dst) = Field(mem,src) ;
+ }
+ }
+}
+
+CAMLprim value caml_new_lex_engine(struct lexing_table *tbl, value start_state,
+ struct lexer_buffer *lexbuf)
+{
+ int state, base, backtrk, c, pstate ;
+ state = Int_val(start_state);
+ if (state >= 0) {
+ /* First entry */
+ lexbuf->lex_last_pos = lexbuf->lex_start_pos = lexbuf->lex_curr_pos;
+ lexbuf->lex_last_action = Val_int(-1);
+ } else {
+ /* Reentry after refill */
+ state = -state - 1;
+ }
+ while(1) {
+ /* Lookup base address or action number for current state */
+ base = Short(tbl->lex_base, state);
+ if (base < 0) {
+ int pc_off = Short(tbl->lex_base_code, state) ;
+ run_tag(Bp_val(tbl->lex_code) + pc_off, lexbuf->lex_mem);
+ /* fprintf(stderr,"Perform: %d\n",-base-1) ; */
+ return Val_int(-base-1);
+ }
+ /* See if it's a backtrack point */
+ backtrk = Short(tbl->lex_backtrk, state);
+ if (backtrk >= 0) {
+ int pc_off = Short(tbl->lex_backtrk_code, state);
+ run_tag(Bp_val(tbl->lex_code) + pc_off, lexbuf->lex_mem);
+ lexbuf->lex_last_pos = lexbuf->lex_curr_pos;
+ lexbuf->lex_last_action = Val_int(backtrk);
+
+ }
+ /* See if we need a refill */
+ if (lexbuf->lex_curr_pos >= lexbuf->lex_buffer_len){
+ if (lexbuf->lex_eof_reached == Val_bool (0)){
+ return Val_int(-state - 1);
+ }else{
+ c = 256;
+ }
+ }else{
+ /* Read next input char */
+ c = Byte_u(lexbuf->lex_buffer, Long_val(lexbuf->lex_curr_pos));
+ lexbuf->lex_curr_pos += 2;
+ }
+ /* Determine next state */
+ pstate=state ;
+ if (Short(tbl->lex_check, base + c) == state)
+ state = Short(tbl->lex_trans, base + c);
+ else
+ state = Short(tbl->lex_default, state);
+ /* If no transition on this char, return to last backtrack point */
+ if (state < 0) {
+ lexbuf->lex_curr_pos = lexbuf->lex_last_pos;
+ if (lexbuf->lex_last_action == Val_int(-1)) {
+ caml_failwith("lexing: empty token");
+ } else {
+ return lexbuf->lex_last_action;
+ }
+ }else{
+ /* If some transition, get and perform memory moves */
+ int base_code = Short(tbl->lex_base_code, pstate) ;
+ int pc_off ;
+ if (Short(tbl->lex_check_code, base_code + c) == pstate)
+ pc_off = Short(tbl->lex_trans_code, base_code + c) ;
+ else
+ pc_off = Short(tbl->lex_default_code, pstate) ;
+ if (pc_off > 0)
+ run_mem(Bp_val(tbl->lex_code) + pc_off, lexbuf->lex_mem,
+ lexbuf->lex_curr_pos) ;
+ /* Erase the EOF condition only if the EOF pseudo-character was
+ consumed by the automaton (i.e. there was no backtrack above)
+ */
+ if (c == 256) lexbuf->lex_eof_reached = Val_bool (0);
+ }
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/main.c b/test/monniaux/ocaml/byterun/main.c
new file mode 100644
index 00000000..498f3d18
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/main.c
@@ -0,0 +1,60 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define VERIMAG_MEASUREMENTS
+#define CAML_INTERNALS
+
+/* Main entry point (can be overridden by a user-provided main()
+ function that calls caml_main() later). */
+
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/sys.h"
+#include "caml/osdeps.h"
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+#ifdef VERIMAG_MEASUREMENTS
+#include "../../clock.h"
+#endif
+
+CAMLextern void caml_main (char_os **);
+
+#ifdef _WIN32
+CAMLextern void caml_expand_command_line (int *, wchar_t ***);
+
+int wmain(int argc, wchar_t **argv)
+#else
+int main(int argc, char **argv)
+#endif
+{
+#ifdef _WIN32
+ /* Expand wildcards and diversions in command line */
+ caml_expand_command_line(&argc, &argv);
+#endif
+
+#ifdef VERIMAG_MEASUREMENTS
+ clock_prepare();
+ clock_start();
+#endif
+ caml_main(argv);
+#ifdef VERIMAG_MEASUREMENTS
+ clock_stop();
+ print_total_clock();
+#endif
+ caml_sys_exit(Val_int(0));
+ return 0; /* not reached */
+}
diff --git a/test/monniaux/ocaml/byterun/major_gc.c b/test/monniaux/ocaml/byterun/major_gc.c
new file mode 100644
index 00000000..493f2cc6
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/major_gc.c
@@ -0,0 +1,943 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* DM */
+#include <stdio.h>
+
+#include <limits.h>
+#include <math.h>
+
+#include "caml/compact.h"
+#include "caml/custom.h"
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/finalise.h"
+#include "caml/freelist.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/major_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/signals.h"
+#include "caml/weak.h"
+
+#if defined (NATIVE_CODE) && defined (NO_NAKED_POINTERS)
+#define NATIVE_CODE_AND_NO_NAKED_POINTERS
+#else
+#undef NATIVE_CODE_AND_NO_NAKED_POINTERS
+#endif
+
+#ifdef _MSC_VER
+static inline double fmin(double a, double b) {
+ return (a < b) ? a : b;
+}
+#endif
+
+uintnat caml_percent_free;
+uintnat caml_major_heap_increment;
+CAMLexport char *caml_heap_start;
+char *caml_gc_sweep_hp;
+int caml_gc_phase; /* always Phase_mark, Pase_clean,
+ Phase_sweep, or Phase_idle */
+static value *gray_vals;
+static value *gray_vals_cur, *gray_vals_end;
+static asize_t gray_vals_size;
+static int heap_is_pure; /* The heap is pure if the only gray objects
+ below [markhp] are also in [gray_vals]. */
+uintnat caml_allocated_words;
+uintnat caml_dependent_size, caml_dependent_allocated;
+double caml_extra_heap_resources;
+uintnat caml_fl_wsz_at_phase_change = 0;
+
+extern char *caml_fl_merge; /* Defined in freelist.c. */
+
+static char *markhp, *chunk, *limit;
+
+int caml_gc_subphase; /* Subphase_{mark_roots,mark_main,mark_final} */
+
+/**
+ Ephemerons:
+ During mark phase the list caml_ephe_list_head of ephemerons
+ is iterated by different pointers that follow the invariants:
+ caml_ephe_list_head ->* ephes_checked_if_pure ->* ephes_to_check ->* null
+ | | |
+ (1) (2) (3)
+
+ At the start of mark phase, (1) and (2) are empty.
+
+ In mark phase:
+ - the ephemerons in (1) have a data alive or none
+ (nb: new ephemerons are added in this part by weak.c)
+ - the ephemerons in (2) have at least a white key or are white
+ if ephe_list_pure is true, otherwise they are in an unknown state and
+ must be checked again.
+ - the ephemerons in (3) are in an unknown state and must be checked
+
+ At the end of mark phase, (3) is empty and ephe_list_pure is true.
+ The ephemeron in (1) and (2) will be cleaned (white keys and datas
+ replaced by none or the ephemeron is removed from the list if it is white)
+ in clean phase.
+
+ In clean phase:
+ caml_ephe_list_head ->* ephes_to_check ->* null
+ | |
+ (1) (3)
+
+ In clean phase, (2) is not used, ephes_to_check is initialized at
+ caml_ephe_list_head:
+ - the ephemerons in (1) are clean.
+ - the ephemerons in (3) should be cleaned or removed if white.
+
+ */
+static int ephe_list_pure;
+/** The ephemerons is pure if since the start of its iteration
+ no value have been darken. */
+static value *ephes_checked_if_pure;
+static value *ephes_to_check;
+
+int caml_major_window = 1;
+double caml_major_ring[Max_major_window] = { 0. };
+int caml_major_ring_index = 0;
+double caml_major_work_credit = 0.0;
+double caml_gc_clock = 0.0;
+
+#ifdef DEBUG
+static unsigned long major_gc_counter = 0;
+#endif
+
+void (*caml_major_gc_hook)(void) = NULL;
+
+static void realloc_gray_vals (void)
+{
+ value *new;
+
+ CAMLassert (gray_vals_cur == gray_vals_end);
+ if (gray_vals_size < caml_stat_heap_wsz / 32){
+ caml_gc_message (0x08, "Growing gray_vals to %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ (intnat) gray_vals_size * sizeof (value) / 512);
+ new = (value *) caml_stat_resize_noexc ((char *) gray_vals,
+ 2 * gray_vals_size *
+ sizeof (value));
+ if (new == NULL){
+ caml_gc_message (0x08, "No room for growing gray_vals\n");
+ gray_vals_cur = gray_vals;
+ heap_is_pure = 0;
+ }else{
+ gray_vals = new;
+ gray_vals_cur = gray_vals + gray_vals_size;
+ gray_vals_size *= 2;
+ gray_vals_end = gray_vals + gray_vals_size;
+ }
+ }else{
+ gray_vals_cur = gray_vals + gray_vals_size / 2;
+ heap_is_pure = 0;
+ }
+}
+
+void caml_darken (value v, value *p /* not used */)
+{
+#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
+ if (Is_block (v) && !Is_young (v) && Wosize_val (v) > 0) {
+#else
+ if (Is_block (v) && Is_in_heap (v)) {
+#endif
+ header_t h = Hd_val (v);
+ tag_t t = Tag_hd (h);
+ if (t == Infix_tag){
+ v -= Infix_offset_val(v);
+ h = Hd_val (v);
+ t = Tag_hd (h);
+ }
+#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
+ /* We insist that naked pointers to outside the heap point to things that
+ look like values with headers coloured black. This isn't always
+ strictly necessary but is essential in certain cases---in particular
+ when the value is allocated in a read-only section. (For the values
+ where it would be safe it is a performance improvement since we avoid
+ putting them on the grey list.) */
+ CAMLassert (Is_in_heap (v) || Is_black_hd (h));
+#endif
+ CAMLassert (!Is_blue_hd (h));
+ if (Is_white_hd (h)){
+ ephe_list_pure = 0;
+ if (t < No_scan_tag){
+ Hd_val (v) = Grayhd_hd (h);
+ *gray_vals_cur++ = v;
+ if (gray_vals_cur >= gray_vals_end) realloc_gray_vals ();
+ }else{
+ Hd_val (v) = Blackhd_hd (h);
+ }
+ }
+ }
+}
+
+static void start_cycle (void)
+{
+ CAMLassert (caml_gc_phase == Phase_idle);
+ CAMLassert (gray_vals_cur == gray_vals);
+ caml_gc_message (0x01, "Starting new major GC cycle\n");
+ caml_darken_all_roots_start ();
+ caml_gc_phase = Phase_mark;
+ caml_gc_subphase = Subphase_mark_roots;
+ markhp = NULL;
+ ephe_list_pure = 1;
+ ephes_checked_if_pure = &caml_ephe_list_head;
+ ephes_to_check = &caml_ephe_list_head;
+#ifdef DEBUG
+ ++ major_gc_counter;
+ caml_heap_check ();
+#endif
+}
+
+/* We may stop the slice inside values, in order to avoid large latencies
+ on large arrays. In this case, [current_value] is the partially-marked
+ value and [current_index] is the index of the next field to be marked.
+*/
+static value current_value = 0;
+static mlsize_t current_index = 0;
+
+/* For instrumentation */
+#ifdef CAML_INSTR
+#define INSTR(x) x
+#else
+#define INSTR(x) /**/
+#endif
+
+static void init_sweep_phase(void)
+{
+ /* Phase_clean is done. */
+ /* Initialise the sweep phase. */
+ caml_gc_sweep_hp = caml_heap_start;
+ caml_fl_init_merge ();
+ caml_gc_phase = Phase_sweep;
+ chunk = caml_heap_start;
+ caml_gc_sweep_hp = chunk;
+ limit = chunk + Chunk_size (chunk);
+ caml_fl_wsz_at_phase_change = caml_fl_cur_wsz;
+ if (caml_major_gc_hook) (*caml_major_gc_hook)();
+}
+
+/* auxillary function of mark_slice */
+static inline value* mark_slice_darken(value *gray_vals_ptr, value v, int i,
+ int in_ephemeron, int *slice_pointers)
+{
+ value child;
+ header_t chd;
+
+ child = Field (v, i);
+
+#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
+ if (Is_block (child)
+ && ! Is_young (child)
+ && Wosize_val (child) > 0 /* Atoms never need to be marked. */
+ /* Closure blocks contain code pointers at offsets that cannot
+ be reliably determined, so we always use the page table when
+ marking such values. */
+ && (!(Tag_val (v) == Closure_tag || Tag_val (v) == Infix_tag) ||
+ Is_in_heap (child))) {
+#else
+ if (Is_block (child) && Is_in_heap (child)) {
+#endif
+ INSTR (++ *slice_pointers;)
+ chd = Hd_val (child);
+ if (Tag_hd (chd) == Forward_tag){
+ value f = Forward_val (child);
+ if ((in_ephemeron && Is_long(f)) ||
+ (Is_block (f)
+ && (!Is_in_value_area(f) || Tag_val (f) == Forward_tag
+ || Tag_val (f) == Lazy_tag
+#ifdef FLAT_FLOAT_ARRAY
+ || Tag_val (f) == Double_tag
+#endif
+ ))){
+ /* Do not short-circuit the pointer. */
+ }else{
+ /* The variable child is not changed because it must be mark alive */
+ Field (v, i) = f;
+ if (Is_block (f) && Is_young (f) && !Is_young (child)){
+ if(in_ephemeron){
+ add_to_ephe_ref_table (&caml_ephe_ref_table, v, i);
+ }else{
+ add_to_ref_table (&caml_ref_table, &Field (v, i));
+ }
+ }
+ }
+ }
+ else if (Tag_hd(chd) == Infix_tag) {
+ child -= Infix_offset_val(child);
+ chd = Hd_val(child);
+ }
+#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
+ /* See [caml_darken] for a description of this assertion. */
+ CAMLassert (Is_in_heap (child) || Is_black_hd (chd));
+#endif
+ if (Is_white_hd (chd)){
+ ephe_list_pure = 0;
+ Hd_val (child) = Grayhd_hd (chd);
+ *gray_vals_ptr++ = child;
+ if (gray_vals_ptr >= gray_vals_end) {
+ gray_vals_cur = gray_vals_ptr;
+ realloc_gray_vals ();
+ gray_vals_ptr = gray_vals_cur;
+ }
+ }
+ }
+
+ return gray_vals_ptr;
+}
+
+static value* mark_ephe_aux (value *gray_vals_ptr, intnat *work,
+ int *slice_pointers)
+{
+ value v, data, key;
+ header_t hd;
+ mlsize_t size, i;
+
+ v = *ephes_to_check;
+ hd = Hd_val(v);
+ CAMLassert(Tag_val (v) == Abstract_tag);
+ data = Field(v,CAML_EPHE_DATA_OFFSET);
+ if ( data != caml_ephe_none &&
+ Is_block (data) && Is_in_heap (data) && Is_white_val (data)){
+
+ int alive_data = 1;
+
+ /* The liveness of the ephemeron is one of the condition */
+ if (Is_white_hd (hd)) alive_data = 0;
+
+ /* The liveness of the keys not caml_ephe_none is the other condition */
+ size = Wosize_hd (hd);
+ for (i = CAML_EPHE_FIRST_KEY; alive_data && i < size; i++){
+ key = Field (v, i);
+ ephemeron_again:
+ if (key != caml_ephe_none &&
+ Is_block (key) && Is_in_heap (key)){
+ if (Tag_val (key) == Forward_tag){
+ value f = Forward_val (key);
+ if (Is_long (f) ||
+ (Is_block (f) &&
+ (!Is_in_value_area(f) || Tag_val (f) == Forward_tag
+ || Tag_val (f) == Lazy_tag
+#ifdef FLAT_FLOAT_ARRAY
+ || Tag_val (f) == Double_tag
+#endif
+ ))){
+ /* Do not short-circuit the pointer. */
+ }else{
+ Field (v, i) = key = f;
+ goto ephemeron_again;
+ }
+ }
+ if (Is_white_val (key)){
+ alive_data = 0;
+ }
+ }
+ }
+ *work -= Whsize_wosize(i);
+
+ if (alive_data){
+ gray_vals_ptr = mark_slice_darken(gray_vals_ptr,v,
+ CAML_EPHE_DATA_OFFSET,
+ /*in_ephemeron=*/1,
+ slice_pointers);
+ } else { /* not triggered move to the next one */
+ ephes_to_check = &Field(v,CAML_EPHE_LINK_OFFSET);
+ return gray_vals_ptr;
+ }
+ } else { /* a simily weak pointer or an already alive data */
+ *work -= 1;
+ }
+
+ /* all keys black or data none or black
+ move the ephemerons from (3) to the end of (1) */
+ if ( ephes_checked_if_pure == ephes_to_check ) {
+ /* corner case and optim */
+ ephes_checked_if_pure = &Field(v,CAML_EPHE_LINK_OFFSET);
+ ephes_to_check = ephes_checked_if_pure;
+ } else {
+ /* - remove v from the list (3) */
+ *ephes_to_check = Field(v,CAML_EPHE_LINK_OFFSET);
+ /* - insert it at the end of (1) */
+ Field(v,CAML_EPHE_LINK_OFFSET) = *ephes_checked_if_pure;
+ *ephes_checked_if_pure = v;
+ ephes_checked_if_pure = &Field(v,CAML_EPHE_LINK_OFFSET);
+ }
+ return gray_vals_ptr;
+}
+
+
+
+static void mark_slice (intnat work)
+{
+ value *gray_vals_ptr; /* Local copy of [gray_vals_cur] */
+ value v;
+ header_t hd;
+ mlsize_t size, i, start, end; /* [start] is a local copy of [current_index] */
+#ifdef CAML_INSTR
+ int slice_fields = 0;
+#endif
+ int slice_pointers = 0; /** gcc removes it when not in CAML_INSTR */
+
+ caml_gc_message (0x40, "Marking %"ARCH_INTNAT_PRINTF_FORMAT"d words\n", work);
+ caml_gc_message (0x40, "Subphase = %d\n", caml_gc_subphase);
+ gray_vals_ptr = gray_vals_cur;
+ v = current_value;
+ start = current_index;
+ while (work > 0){
+ if (v == 0 && gray_vals_ptr > gray_vals){
+ CAMLassert (start == 0);
+ v = *--gray_vals_ptr;
+ CAMLassert (Is_gray_val (v));
+ }
+ if (v != 0){
+ hd = Hd_val(v);
+ CAMLassert (Is_gray_hd (hd));
+ size = Wosize_hd (hd);
+ end = start + work;
+ if (Tag_hd (hd) < No_scan_tag){
+ start = size < start ? size : start;
+ end = size < end ? size : end;
+ CAMLassert (end >= start);
+ INSTR (slice_fields += end - start;)
+ INSTR (if (size > end)
+ CAML_INSTR_INT ("major/mark/slice/remain", size - end);)
+ for (i = start; i < end; i++){
+ gray_vals_ptr = mark_slice_darken(gray_vals_ptr,v,i,
+ /*in_ephemeron=*/ 0,
+ &slice_pointers);
+ }
+ if (end < size){
+ work = 0;
+ start = end;
+ /* [v] doesn't change. */
+ CAMLassert (Is_gray_val (v));
+ }else{
+ CAMLassert (end == size);
+ Hd_val (v) = Blackhd_hd (hd);
+ work -= Whsize_wosize(end - start);
+ start = 0;
+ v = 0;
+ }
+ }else{
+ /* The block doesn't contain any pointers. */
+ CAMLassert (start == 0);
+ Hd_val (v) = Blackhd_hd (hd);
+ work -= Whsize_wosize(size);
+ v = 0;
+ }
+ }else if (markhp != NULL){
+ if (markhp == limit){
+ chunk = Chunk_next (chunk);
+ if (chunk == NULL){
+ markhp = NULL;
+ }else{
+ markhp = chunk;
+ limit = chunk + Chunk_size (chunk);
+ }
+ }else{
+ if (Is_gray_val (Val_hp (markhp))){
+ CAMLassert (gray_vals_ptr == gray_vals);
+ CAMLassert (v == 0 && start == 0);
+ v = Val_hp (markhp);
+ }
+ markhp += Bhsize_hp (markhp);
+ }
+ }else if (!heap_is_pure){
+ heap_is_pure = 1;
+ chunk = caml_heap_start;
+ markhp = chunk;
+ limit = chunk + Chunk_size (chunk);
+ } else if (caml_gc_subphase == Subphase_mark_roots) {
+ gray_vals_cur = gray_vals_ptr;
+ work = caml_darken_all_roots_slice (work);
+ gray_vals_ptr = gray_vals_cur;
+ if (work > 0){
+ caml_gc_subphase = Subphase_mark_main;
+ }
+ } else if (*ephes_to_check != (value) NULL) {
+ /* Continue to scan the list of ephe */
+ gray_vals_ptr = mark_ephe_aux(gray_vals_ptr,&work,&slice_pointers);
+ } else if (!ephe_list_pure){
+ /* We must scan again the list because some value have been darken */
+ ephe_list_pure = 1;
+ ephes_to_check = ephes_checked_if_pure;
+ }else{
+ switch (caml_gc_subphase){
+ case Subphase_mark_main: {
+ /* Subphase_mark_main is done.
+ Mark finalised values. */
+ gray_vals_cur = gray_vals_ptr;
+ caml_final_update_mark_phase ();
+ gray_vals_ptr = gray_vals_cur;
+ if (gray_vals_ptr > gray_vals){
+ v = *--gray_vals_ptr;
+ CAMLassert (start == 0);
+ }
+ /* Complete the marking */
+ ephes_to_check = ephes_checked_if_pure;
+ caml_gc_subphase = Subphase_mark_final;
+ }
+ break;
+ case Subphase_mark_final: {
+ /** The set of unreachable value will not change anymore for
+ this cycle. Start clean phase. */
+ caml_gc_phase = Phase_clean;
+ caml_final_update_clean_phase ();
+ if (caml_ephe_list_head != (value) NULL){
+ /* Initialise the clean phase. */
+ ephes_to_check = &caml_ephe_list_head;
+ } else {
+ /* Initialise the sweep phase. */
+ init_sweep_phase();
+ }
+ work = 0;
+ }
+ break;
+ default: CAMLassert (0);
+ }
+ }
+ }
+ gray_vals_cur = gray_vals_ptr;
+ current_value = v;
+ current_index = start;
+ INSTR (CAML_INSTR_INT ("major/mark/slice/fields#", slice_fields);)
+ INSTR (CAML_INSTR_INT ("major/mark/slice/pointers#", slice_pointers);)
+}
+
+/* Clean ephemerons */
+static void clean_slice (intnat work)
+{
+ value v;
+
+ caml_gc_message (0x40, "Cleaning %"
+ ARCH_INTNAT_PRINTF_FORMAT "d words\n", work);
+ while (work > 0){
+ v = *ephes_to_check;
+ if (v != (value) NULL){
+ if (Is_white_val (v)){
+ /* The whole array is dead, remove it from the list. */
+ *ephes_to_check = Field (v, CAML_EPHE_LINK_OFFSET);
+ work -= 1;
+ }else{
+ caml_ephe_clean(v);
+ ephes_to_check = &Field (v, CAML_EPHE_LINK_OFFSET);
+ work -= Whsize_val (v);
+ }
+ }else{ /* End of list reached */
+ /* Phase_clean is done. */
+ /* Initialise the sweep phase. */
+ init_sweep_phase();
+ work = 0;
+ }
+ }
+}
+
+static void sweep_slice (intnat work)
+{
+ char *hp;
+ header_t hd;
+
+ caml_gc_message (0x40, "Sweeping %"
+ ARCH_INTNAT_PRINTF_FORMAT "d words\n", work);
+ while (work > 0){
+ if (caml_gc_sweep_hp < limit){
+ hp = caml_gc_sweep_hp;
+ hd = Hd_hp (hp);
+ work -= Whsize_hd (hd);
+ caml_gc_sweep_hp += Bhsize_hd (hd);
+ switch (Color_hd (hd)){
+ case Caml_white:
+ if (Tag_hd (hd) == Custom_tag){
+ void (*final_fun)(value) = Custom_ops_val(Val_hp(hp))->finalize;
+ if (final_fun != NULL) final_fun(Val_hp(hp));
+ }
+ caml_gc_sweep_hp = (char *) caml_fl_merge_block (Val_hp (hp));
+ break;
+ case Caml_blue:
+ /* Only the blocks of the free-list are blue. See [freelist.c]. */
+ caml_fl_merge = Bp_hp (hp);
+ break;
+ default: /* gray or black */
+ CAMLassert (Color_hd (hd) == Caml_black);
+ Hd_hp (hp) = Whitehd_hd (hd);
+ break;
+ }
+ CAMLassert (caml_gc_sweep_hp <= limit);
+ }else{
+ chunk = Chunk_next (chunk);
+ if (chunk == NULL){
+ /* Sweeping is done. */
+ ++ caml_stat_major_collections;
+ work = 0;
+ caml_gc_phase = Phase_idle;
+ caml_request_minor_gc ();
+ }else{
+ caml_gc_sweep_hp = chunk;
+ limit = chunk + Chunk_size (chunk);
+ }
+ }
+ }
+}
+
+#ifdef CAML_INSTR
+static char *mark_slice_name[] = {
+ /* 0 */ NULL,
+ /* 1 */ NULL,
+ /* 2 */ NULL,
+ /* 3 */ NULL,
+ /* 4 */ NULL,
+ /* 5 */ NULL,
+ /* 6 */ NULL,
+ /* 7 */ NULL,
+ /* 8 */ NULL,
+ /* 9 */ NULL,
+ /* 10 */ "major/mark_roots",
+ /* 11 */ "major/mark_main",
+ /* 12 */ "major/mark_weak1",
+ /* 13 */ "major/mark_weak2",
+ /* 14 */ "major/mark_final",
+};
+#endif
+
+/* The main entry point for the major GC. Called about once for each
+ minor GC. [howmuch] is the amount of work to do:
+ -1 if the GC is triggered automatically
+ 0 to let the GC compute the amount of work
+ [n] to make the GC do enough work to (on average) free [n] words
+ */
+void caml_major_collection_slice (intnat howmuch)
+{
+ double p, dp, filt_p, spend;
+ intnat computed_work;
+ int i;
+ /*
+ Free memory at the start of the GC cycle (garbage + free list) (assumed):
+ FM = caml_stat_heap_wsz * caml_percent_free
+ / (100 + caml_percent_free)
+
+ Assuming steady state and enforcing a constant allocation rate, then
+ FM is divided in 2/3 for garbage and 1/3 for free list.
+ G = 2 * FM / 3
+ G is also the amount of memory that will be used during this cycle
+ (still assuming steady state).
+
+ Proportion of G consumed since the previous slice:
+ PH = caml_allocated_words / G
+ = caml_allocated_words * 3 * (100 + caml_percent_free)
+ / (2 * caml_stat_heap_wsz * caml_percent_free)
+ Proportion of extra-heap resources consumed since the previous slice:
+ PE = caml_extra_heap_resources
+ Proportion of total work to do in this slice:
+ P = max (PH, PE)
+
+ Here, we insert a time-based filter on the P variable to avoid large
+ latency spikes in the GC, so the P below is a smoothed-out version of
+ the P above.
+
+ Amount of marking work for the GC cycle:
+ MW = caml_stat_heap_wsz * 100 / (100 + caml_percent_free)
+ + caml_incremental_roots_count
+ Amount of sweeping work for the GC cycle:
+ SW = caml_stat_heap_wsz
+
+ In order to finish marking with a non-empty free list, we will
+ use 40% of the time for marking, and 60% for sweeping.
+
+ Let MT be the time spent marking, ST the time spent sweeping, and TT
+ the total time for this cycle. We have:
+ MT = 40/100 * TT
+ ST = 60/100 * TT
+
+ Amount of time to spend on this slice:
+ T = P * TT = P * MT / (40/100) = P * ST / (60/100)
+
+ Since we must do MW work in MT time or SW work in ST time, the amount
+ of work for this slice is:
+ MS = P * MW / (40/100) if marking
+ SS = P * SW / (60/100) if sweeping
+
+ Amount of marking work for a marking slice:
+ MS = P * MW / (40/100)
+ MS = P * (caml_stat_heap_wsz * 250 / (100 + caml_percent_free)
+ + 2.5 * caml_incremental_roots_count)
+ Amount of sweeping work for a sweeping slice:
+ SS = P * SW / (60/100)
+ SS = P * caml_stat_heap_wsz * 5 / 3
+
+ This slice will either mark MS words or sweep SS words.
+ */
+
+ if (caml_major_slice_begin_hook != NULL) (*caml_major_slice_begin_hook) ();
+ CAML_INSTR_SETUP (tmr, "major");
+
+ p = (double) caml_allocated_words * 3.0 * (100 + caml_percent_free)
+ / caml_stat_heap_wsz / caml_percent_free / 2.0;
+ if (caml_dependent_size > 0){
+ dp = (double) caml_dependent_allocated * (100 + caml_percent_free)
+ / caml_dependent_size / caml_percent_free;
+ }else{
+ dp = 0.0;
+ }
+ if (p < dp) p = dp;
+ if (p < caml_extra_heap_resources) p = caml_extra_heap_resources;
+ if (p > 0.3) p = 0.3;
+ CAML_INSTR_INT ("major/work/extra#",
+ (uintnat) (caml_extra_heap_resources * 1000000));
+
+ caml_gc_message (0x40, "ordered work = %"
+ ARCH_INTNAT_PRINTF_FORMAT "d words\n", howmuch);
+ caml_gc_message (0x40, "allocated_words = %"
+ ARCH_INTNAT_PRINTF_FORMAT "u\n",
+ caml_allocated_words);
+ caml_gc_message (0x40, "extra_heap_resources = %"
+ ARCH_INTNAT_PRINTF_FORMAT "uu\n",
+ (uintnat) (caml_extra_heap_resources * 1000000));
+ caml_gc_message (0x40, "raw work-to-do = %"
+ ARCH_INTNAT_PRINTF_FORMAT "du\n",
+ (intnat) (p * 1000000));
+
+ for (i = 0; i < caml_major_window; i++){
+ caml_major_ring[i] += p / caml_major_window;
+ }
+
+ if (caml_gc_clock >= 1.0){
+ caml_gc_clock -= 1.0;
+ ++caml_major_ring_index;
+ if (caml_major_ring_index >= caml_major_window){
+ caml_major_ring_index = 0;
+ }
+ }
+ if (howmuch == -1){
+ /* auto-triggered GC slice: spend work credit on the current bucket,
+ then do the remaining work, if any */
+ /* Note that the minor GC guarantees that the major slice is called in
+ automatic mode (with [howmuch] = -1) at least once per clock tick.
+ This means we never leave a non-empty bucket behind. */
+ spend = fmin (caml_major_work_credit,
+ caml_major_ring[caml_major_ring_index]);
+ caml_major_work_credit -= spend;
+ filt_p = caml_major_ring[caml_major_ring_index] - spend;
+ caml_major_ring[caml_major_ring_index] = 0.0;
+ }else{
+ /* forced GC slice: do work and add it to the credit */
+ if (howmuch == 0){
+ /* automatic setting: size of next bucket
+ we do not use the current bucket, as it may be empty */
+ int i = caml_major_ring_index + 1;
+ if (i >= caml_major_window) i = 0;
+ filt_p = caml_major_ring[i];
+ }else{
+ /* manual setting */
+ filt_p = (double) howmuch * 3.0 * (100 + caml_percent_free)
+ / caml_stat_heap_wsz / caml_percent_free / 2.0;
+ }
+ caml_major_work_credit += filt_p;
+ }
+
+ p = filt_p;
+
+ caml_gc_message (0x40, "filtered work-to-do = %"
+ ARCH_INTNAT_PRINTF_FORMAT "du\n",
+ (intnat) (p * 1000000));
+
+ if (caml_gc_phase == Phase_idle){
+ if (caml_young_ptr == caml_young_alloc_end){
+ /* We can only start a major GC cycle if the minor allocation arena
+ is empty, otherwise we'd have to treat it as a set of roots. */
+ start_cycle ();
+ CAML_INSTR_TIME (tmr, "major/roots");
+ }
+ p = 0;
+ goto finished;
+ }
+
+ if (p < 0){
+ p = 0;
+ goto finished;
+ }
+
+ if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean){
+ computed_work = (intnat) (p * ((double) caml_stat_heap_wsz * 250
+ / (100 + caml_percent_free)
+ + caml_incremental_roots_count));
+ }else{
+ computed_work = (intnat) (p * caml_stat_heap_wsz * 5 / 3);
+ }
+ caml_gc_message (0x40, "computed work = %"
+ ARCH_INTNAT_PRINTF_FORMAT "d words\n", computed_work);
+ if (caml_gc_phase == Phase_mark){
+ CAML_INSTR_INT ("major/work/mark#", computed_work);
+ mark_slice (computed_work);
+ CAML_INSTR_TIME (tmr, mark_slice_name[caml_gc_subphase]);
+ caml_gc_message (0x02, "!");
+ }else if (caml_gc_phase == Phase_clean){
+ clean_slice (computed_work);
+ caml_gc_message (0x02, "%%");
+ }else{
+ CAMLassert (caml_gc_phase == Phase_sweep);
+ CAML_INSTR_INT ("major/work/sweep#", computed_work);
+ sweep_slice (computed_work);
+ CAML_INSTR_TIME (tmr, "major/sweep");
+ caml_gc_message (0x02, "$");
+ }
+
+ if (caml_gc_phase == Phase_idle){
+ caml_compact_heap_maybe ();
+ CAML_INSTR_TIME (tmr, "major/check_and_compact");
+ }
+
+ finished:
+ caml_gc_message (0x40, "work-done = %"
+ ARCH_INTNAT_PRINTF_FORMAT "du\n",
+ (intnat) (p * 1000000));
+
+ /* if some of the work was not done, take it back from the credit
+ or spread it over the buckets. */
+ p = filt_p - p;
+ spend = fmin (p, caml_major_work_credit);
+ caml_major_work_credit -= spend;
+ if (p > spend){
+ p -= spend;
+ p /= caml_major_window;
+ for (i = 0; i < caml_major_window; i++) caml_major_ring[i] += p;
+ }
+
+ caml_stat_major_words += caml_allocated_words;
+ caml_allocated_words = 0;
+ caml_dependent_allocated = 0;
+ caml_extra_heap_resources = 0.0;
+ if (caml_major_slice_end_hook != NULL) (*caml_major_slice_end_hook) ();
+}
+
+/* This does not call [caml_compact_heap_maybe] because the estimates of
+ free and live memory are only valid for a cycle done incrementally.
+ Besides, this function itself is called by [caml_compact_heap_maybe].
+*/
+void caml_finish_major_cycle (void)
+{
+ if (caml_gc_phase == Phase_idle) start_cycle ();
+ while (caml_gc_phase == Phase_mark) mark_slice (LONG_MAX);
+ while (caml_gc_phase == Phase_clean) clean_slice (LONG_MAX);
+ CAMLassert (caml_gc_phase == Phase_sweep);
+ while (caml_gc_phase == Phase_sweep) sweep_slice (LONG_MAX);
+ CAMLassert (caml_gc_phase == Phase_idle);
+ caml_stat_major_words += caml_allocated_words;
+ caml_allocated_words = 0;
+}
+
+/* Call this function to make sure [bsz] is greater than or equal
+ to both [Heap_chunk_min] and the current heap increment.
+*/
+asize_t caml_clip_heap_chunk_wsz (asize_t wsz)
+{
+ asize_t result = wsz;
+ uintnat incr;
+
+ /* Compute the heap increment as a word size. */
+ if (caml_major_heap_increment > 1000){
+ incr = caml_major_heap_increment;
+ }else{
+ incr = caml_stat_heap_wsz / 100 * caml_major_heap_increment;
+ }
+
+ if (result < incr){
+ result = incr;
+ }
+ if (result < Heap_chunk_min){
+ result = Heap_chunk_min;
+ }
+ return result;
+}
+
+/* [heap_size] is a number of bytes */
+void caml_init_major_heap (asize_t heap_size)
+{
+ int i;
+
+ /* DM */ heap_size = 65536;
+
+ caml_stat_heap_wsz = caml_clip_heap_chunk_wsz (Wsize_bsize (heap_size));
+ caml_stat_top_heap_wsz = caml_stat_heap_wsz;
+ CAMLassert (Bsize_wsize (caml_stat_heap_wsz) % Page_size == 0);
+ fprintf(stderr, "allocating %zd\n", Bsize_wsize (caml_stat_heap_wsz));
+ fflush(stderr);
+ caml_heap_start =
+ (char *) caml_alloc_for_heap (Bsize_wsize (caml_stat_heap_wsz));
+ if (caml_heap_start == NULL)
+ caml_fatal_error ("Fatal error: cannot allocate initial major heap.\n");
+ Chunk_next (caml_heap_start) = NULL;
+ caml_stat_heap_wsz = Wsize_bsize (Chunk_size (caml_heap_start));
+ caml_stat_heap_chunks = 1;
+ caml_stat_top_heap_wsz = caml_stat_heap_wsz;
+
+ if (caml_page_table_add(In_heap, caml_heap_start,
+ caml_heap_start + Bsize_wsize (caml_stat_heap_wsz))
+ != 0) {
+ caml_fatal_error ("Fatal error: cannot allocate "
+ "initial page table.\n");
+ }
+
+ caml_fl_init_merge ();
+ caml_make_free_blocks ((value *) caml_heap_start,
+ caml_stat_heap_wsz, 1, Caml_white);
+ caml_gc_phase = Phase_idle;
+ gray_vals_size = 2048;
+ gray_vals = (value *) caml_stat_alloc_noexc (gray_vals_size * sizeof (value));
+ if (gray_vals == NULL)
+ caml_fatal_error ("Fatal error: not enough memory for the gray cache.\n");
+ gray_vals_cur = gray_vals;
+ gray_vals_end = gray_vals + gray_vals_size;
+ heap_is_pure = 1;
+ caml_allocated_words = 0;
+ caml_extra_heap_resources = 0.0;
+ for (i = 0; i < Max_major_window; i++) caml_major_ring[i] = 0.0;
+}
+
+void caml_set_major_window (int w){
+ uintnat total = 0;
+ int i;
+ if (w == caml_major_window) return;
+ CAMLassert (w <= Max_major_window);
+ /* Collect the current work-to-do from the buckets. */
+ for (i = 0; i < caml_major_window; i++){
+ total += caml_major_ring[i];
+ }
+ /* Redistribute to the new buckets. */
+ for (i = 0; i < w; i++){
+ caml_major_ring[i] = total / w;
+ }
+ caml_major_window = w;
+}
+
+void caml_finalise_heap (void)
+{
+ /* Finishing major cycle (all values become white) */
+ caml_empty_minor_heap ();
+ caml_finish_major_cycle ();
+ CAMLassert (caml_gc_phase == Phase_idle);
+
+ /* Finalising all values (by means of forced sweeping) */
+ caml_fl_init_merge ();
+ caml_gc_phase = Phase_sweep;
+ chunk = caml_heap_start;
+ caml_gc_sweep_hp = chunk;
+ limit = chunk + Chunk_size (chunk);
+ while (caml_gc_phase == Phase_sweep)
+ sweep_slice (LONG_MAX);
+}
diff --git a/test/monniaux/ocaml/byterun/md5.c b/test/monniaux/ocaml/byterun/md5.c
new file mode 100644
index 00000000..2e128010
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/md5.c
@@ -0,0 +1,325 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/md5.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/io.h"
+#include "caml/reverse.h"
+
+/* MD5 message digest */
+
+CAMLprim value caml_md5_string(value str, value ofs, value len)
+{
+ struct MD5Context ctx;
+ value res;
+ caml_MD5Init(&ctx);
+ caml_MD5Update(&ctx, &Byte_u(str, Long_val(ofs)), Long_val(len));
+ res = caml_alloc_string(16);
+ caml_MD5Final(&Byte_u(res, 0), &ctx);
+ return res;
+}
+
+CAMLexport value caml_md5_channel(struct channel *chan, intnat toread)
+{
+ CAMLparam0();
+ struct MD5Context ctx;
+ value res;
+ intnat read;
+ char buffer[4096];
+
+ Lock(chan);
+ caml_MD5Init(&ctx);
+ if (toread < 0){
+ while (1){
+ read = caml_getblock (chan, buffer, sizeof(buffer));
+ if (read == 0) break;
+ caml_MD5Update (&ctx, (unsigned char *) buffer, read);
+ }
+ }else{
+ while (toread > 0) {
+ read = caml_getblock(chan, buffer,
+ toread > sizeof(buffer) ? sizeof(buffer) : toread);
+ if (read == 0) caml_raise_end_of_file();
+ caml_MD5Update(&ctx, (unsigned char *) buffer, read);
+ toread -= read;
+ }
+ }
+ res = caml_alloc_string(16);
+ caml_MD5Final(&Byte_u(res, 0), &ctx);
+ Unlock(chan);
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_md5_chan(value vchan, value len)
+{
+ CAMLparam2 (vchan, len);
+ CAMLreturn (caml_md5_channel(Channel(vchan), Long_val(len)));
+}
+
+CAMLexport void caml_md5_block(unsigned char digest[16],
+ void * data, uintnat len)
+{
+ struct MD5Context ctx;
+ caml_MD5Init(&ctx);
+ caml_MD5Update(&ctx, data, len);
+ caml_MD5Final(digest, &ctx);
+}
+
+/*
+ * This code implements the MD5 message-digest algorithm.
+ * The algorithm is due to Ron Rivest. This code was
+ * written by Colin Plumb in 1993, no copyright is claimed.
+ * This code is in the public domain; do with it what you wish.
+ *
+ * Equivalent code is available from RSA Data Security, Inc.
+ * This code has been tested against that, and is equivalent,
+ * except that you don't need to include two pages of legalese
+ * with every copy.
+ *
+ * To compute the message digest of a chunk of bytes, declare an
+ * MD5Context structure, pass it to caml_MD5Init, call caml_MD5Update as
+ * needed on buffers full of bytes, and then call caml_MD5Final, which
+ * will fill a supplied 16-byte array with the digest.
+ */
+
+#ifndef ARCH_BIG_ENDIAN
+#define byteReverse(buf, len) /* Nothing */
+#else
+static void byteReverse(unsigned char * buf, unsigned longs)
+{
+ uint32_t t;
+ do {
+ t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
+ ((unsigned) buf[1] << 8 | buf[0]);
+ *(uint32_t *) buf = t;
+ buf += 4;
+ } while (--longs);
+}
+#endif
+
+/*
+ * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+CAMLexport void caml_MD5Init(struct MD5Context *ctx)
+{
+ ctx->buf[0] = 0x67452301;
+ ctx->buf[1] = 0xefcdab89;
+ ctx->buf[2] = 0x98badcfe;
+ ctx->buf[3] = 0x10325476;
+
+ ctx->bits[0] = 0;
+ ctx->bits[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+CAMLexport void caml_MD5Update(struct MD5Context *ctx, unsigned char *buf,
+ uintnat len)
+{
+ uint32_t t;
+
+ /* Update bitcount */
+
+ t = ctx->bits[0];
+ if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
+ ctx->bits[1]++; /* Carry from low to high */
+ ctx->bits[1] += len >> 29;
+
+ t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
+
+ /* Handle any leading odd-sized chunks */
+
+ if (t) {
+ unsigned char *p = (unsigned char *) ctx->in + t;
+
+ t = 64 - t;
+ if (len < t) {
+ memcpy(p, buf, len);
+ return;
+ }
+ memcpy(p, buf, t);
+ byteReverse(ctx->in, 16);
+ caml_MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += t;
+ len -= t;
+ }
+ /* Process data in 64-byte chunks */
+
+ while (len >= 64) {
+ memcpy(ctx->in, buf, 64);
+ byteReverse(ctx->in, 16);
+ caml_MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += 64;
+ len -= 64;
+ }
+
+ /* Handle any remaining bytes of data. */
+
+ memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+CAMLexport void caml_MD5Final(unsigned char *digest, struct MD5Context *ctx)
+{
+ unsigned count;
+ unsigned char *p;
+
+ /* Compute number of bytes mod 64 */
+ count = (ctx->bits[0] >> 3) & 0x3F;
+
+ /* Set the first char of padding to 0x80. This is safe since there is
+ always at least one byte free */
+ p = ctx->in + count;
+ *p++ = 0x80;
+
+ /* Bytes of padding needed to make 64 bytes */
+ count = 64 - 1 - count;
+
+ /* Pad out to 56 mod 64 */
+ if (count < 8) {
+ /* Two lots of padding: Pad the first block to 64 bytes */
+ memset(p, 0, count);
+ byteReverse(ctx->in, 16);
+ caml_MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+
+ /* Now fill the next block with 56 bytes */
+ memset(ctx->in, 0, 56);
+ } else {
+ /* Pad block to 56 bytes */
+ memset(p, 0, count - 8);
+ }
+ byteReverse(ctx->in, 14);
+
+ /* Append length in bits and transform */
+ ((uint32_t *) ctx->in)[14] = ctx->bits[0];
+ ((uint32_t *) ctx->in)[15] = ctx->bits[1];
+
+ caml_MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ byteReverse((unsigned char *) ctx->buf, 4);
+ memcpy(digest, ctx->buf, 16);
+ memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
+}
+
+/* The four core functions - F1 is optimized somewhat */
+
+/* #define F1(x, y, z) (x & y | ~x & z) */
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
+
+/* This is the central step in the MD5 algorithm. */
+#define MD5STEP(f, w, x, y, z, data, s) \
+ ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
+
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data. caml_MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
+CAMLexport void caml_MD5Transform(uint32_t *buf, uint32_t *in)
+{
+ register uint32_t a, b, c, d;
+
+ a = buf[0];
+ b = buf[1];
+ c = buf[2];
+ d = buf[3];
+
+ MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+ MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+ MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
+ MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+ MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+ MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+ MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
+ MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
+ MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
+ MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+ MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+ MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+ MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
+ MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
+ MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
+ MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+ MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+ MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
+ MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+ MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+ MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+ MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
+ MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+ MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+ MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+ MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+ MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+ MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+ MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+ MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+ MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+ MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+ MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+ MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
+ MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+ MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+ MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+ MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+ MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+ MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+ MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+ MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+ MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+ MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
+ MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+ MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+ MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+ MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+ MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
+ MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
+ MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+ MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+ MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+ MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+ MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+ MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+ MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+ MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+ MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
+ MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+ MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+ MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+ MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+ MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+ buf[0] += a;
+ buf[1] += b;
+ buf[2] += c;
+ buf[3] += d;
+}
diff --git a/test/monniaux/ocaml/byterun/memory.c b/test/monniaux/ocaml/byterun/memory.c
new file mode 100644
index 00000000..f92b23c4
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/memory.c
@@ -0,0 +1,1016 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include "caml/address_class.h"
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/freelist.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/major_gc.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/signals.h"
+
+int caml_huge_fallback_count = 0;
+/* Number of times that mmapping big pages fails and we fell back to small
+ pages. This counter is available to the program through
+ [Gc.huge_fallback_count].
+*/
+
+uintnat caml_use_huge_pages = 0;
+/* True iff the program allocates heap chunks by mmapping huge pages.
+ This is set when parsing [OCAMLRUNPARAM] and must stay constant
+ after that.
+*/
+
+extern uintnat caml_percent_free; /* major_gc.c */
+
+/* Page table management */
+
+#define Page(p) ((uintnat) (p) >> Page_log)
+#define Page_mask ((uintnat) -1 << Page_log)
+
+#ifdef ARCH_SIXTYFOUR
+
+/* 64-bit implementation:
+ The page table is represented sparsely as a hash table
+ with linear probing */
+
+struct page_table {
+ mlsize_t size; /* size == 1 << (wordsize - shift) */
+ int shift;
+ mlsize_t mask; /* mask == size - 1 */
+ mlsize_t occupancy;
+ uintnat * entries; /* [size] */
+};
+
+static struct page_table caml_page_table;
+
+/* Page table entries are the logical 'or' of
+ - the key: address of a page (low Page_log bits = 0)
+ - the data: a 8-bit integer */
+
+#define Page_entry_matches(entry,addr) \
+ ((((entry) ^ (addr)) & Page_mask) == 0)
+
+/* Multiplicative Fibonacci hashing
+ (Knuth, TAOCP vol 3, section 6.4, page 518).
+ HASH_FACTOR is (sqrt(5) - 1) / 2 * 2^wordsize. */
+#ifdef ARCH_SIXTYFOUR
+#define HASH_FACTOR 11400714819323198486UL
+#else
+#define HASH_FACTOR 2654435769UL
+#endif
+#define Hash(v) (((v) * HASH_FACTOR) >> caml_page_table.shift)
+
+int caml_page_table_lookup(void * addr)
+{
+ uintnat h, e;
+
+ h = Hash(Page(addr));
+ /* The first hit is almost always successful, so optimize for this case */
+ e = caml_page_table.entries[h];
+ if (Page_entry_matches(e, (uintnat)addr)) return e & 0xFF;
+ while(1) {
+ if (e == 0) return 0;
+ h = (h + 1) & caml_page_table.mask;
+ e = caml_page_table.entries[h];
+ if (Page_entry_matches(e, (uintnat)addr)) return e & 0xFF;
+ }
+}
+
+int caml_page_table_initialize(mlsize_t bytesize)
+{
+ uintnat pagesize = Page(bytesize);
+
+ caml_page_table.size = 1;
+ caml_page_table.shift = 8 * sizeof(uintnat);
+ /* Aim for initial load factor between 1/4 and 1/2 */
+ while (caml_page_table.size < 2 * pagesize) {
+ caml_page_table.size <<= 1;
+ caml_page_table.shift -= 1;
+ }
+ caml_page_table.mask = caml_page_table.size - 1;
+ caml_page_table.occupancy = 0;
+ caml_page_table.entries =
+ caml_stat_calloc_noexc(caml_page_table.size, sizeof(uintnat));
+ if (caml_page_table.entries == NULL)
+ return -1;
+ else
+ return 0;
+}
+
+static int caml_page_table_resize(void)
+{
+ struct page_table old = caml_page_table;
+ uintnat * new_entries;
+ uintnat i, h;
+
+ caml_gc_message (0x08, "Growing page table to %"
+ ARCH_INTNAT_PRINTF_FORMAT "u entries\n",
+ caml_page_table.size);
+
+ new_entries = caml_stat_calloc_noexc(2 * old.size, sizeof(uintnat));
+ if (new_entries == NULL) {
+ caml_gc_message (0x08, "No room for growing page table\n");
+ return -1;
+ }
+
+ caml_page_table.size = 2 * old.size;
+ caml_page_table.shift = old.shift - 1;
+ caml_page_table.mask = caml_page_table.size - 1;
+ caml_page_table.occupancy = old.occupancy;
+ caml_page_table.entries = new_entries;
+
+ for (i = 0; i < old.size; i++) {
+ uintnat e = old.entries[i];
+ if (e == 0) continue;
+ h = Hash(Page(e));
+ while (caml_page_table.entries[h] != 0)
+ h = (h + 1) & caml_page_table.mask;
+ caml_page_table.entries[h] = e;
+ }
+
+ caml_stat_free(old.entries);
+ return 0;
+}
+
+static int caml_page_table_modify(uintnat page, int toclear, int toset)
+{
+ uintnat h;
+
+ CAMLassert ((page & ~Page_mask) == 0);
+
+ /* Resize to keep load factor below 1/2 */
+ if (caml_page_table.occupancy * 2 >= caml_page_table.size) {
+ if (caml_page_table_resize() != 0) return -1;
+ }
+ h = Hash(Page(page));
+ while (1) {
+ if (caml_page_table.entries[h] == 0) {
+ caml_page_table.entries[h] = page | toset;
+ caml_page_table.occupancy++;
+ break;
+ }
+ if (Page_entry_matches(caml_page_table.entries[h], page)) {
+ caml_page_table.entries[h] =
+ (caml_page_table.entries[h] & ~toclear) | toset;
+ break;
+ }
+ h = (h + 1) & caml_page_table.mask;
+ }
+ return 0;
+}
+
+#else
+
+/* 32-bit implementation:
+ The page table is represented as a 2-level array of unsigned char */
+
+CAMLexport unsigned char * caml_page_table[Pagetable1_size];
+static unsigned char caml_page_table_empty[Pagetable2_size] = { 0, };
+
+int caml_page_table_initialize(mlsize_t bytesize)
+{
+ int i;
+ for (i = 0; i < Pagetable1_size; i++)
+ caml_page_table[i] = caml_page_table_empty;
+ return 0;
+}
+
+static int caml_page_table_modify(uintnat page, int toclear, int toset)
+{
+ uintnat i = Pagetable_index1(page);
+ uintnat j = Pagetable_index2(page);
+
+ if (caml_page_table[i] == caml_page_table_empty) {
+ unsigned char * new_tbl = caml_stat_calloc_noexc(Pagetable2_size, 1);
+ if (new_tbl == 0) return -1;
+ caml_page_table[i] = new_tbl;
+ }
+ caml_page_table[i][j] = (caml_page_table[i][j] & ~toclear) | toset;
+ return 0;
+}
+
+#endif
+
+int caml_page_table_add(int kind, void * start, void * end)
+{
+ uintnat pstart = (uintnat) start & Page_mask;
+ uintnat pend = ((uintnat) end - 1) & Page_mask;
+ uintnat p;
+
+ for (p = pstart; p <= pend; p += Page_size)
+ if (caml_page_table_modify(p, 0, kind) != 0) return -1;
+ return 0;
+}
+
+int caml_page_table_remove(int kind, void * start, void * end)
+{
+ uintnat pstart = (uintnat) start & Page_mask;
+ uintnat pend = ((uintnat) end - 1) & Page_mask;
+ uintnat p;
+
+ for (p = pstart; p <= pend; p += Page_size)
+ if (caml_page_table_modify(p, kind, 0) != 0) return -1;
+ return 0;
+}
+
+
+/* Initialize the [alloc_for_heap] system.
+ This function must be called exactly once, and it must be called
+ before the first call to [alloc_for_heap].
+ It returns 0 on success and -1 on failure.
+*/
+int caml_init_alloc_for_heap (void)
+{
+ return 0;
+}
+
+/* Allocate a block of the requested size, to be passed to
+ [caml_add_to_heap] later.
+ [request] will be rounded up to some implementation-dependent size.
+ The caller must use [Chunk_size] on the result to recover the actual
+ size.
+ Return NULL if the request cannot be satisfied. The returned pointer
+ is a hp, but the header (and the contents) must be initialized by the
+ caller.
+*/
+char *caml_alloc_for_heap (asize_t request)
+{
+ if (caml_use_huge_pages){
+#ifdef HAS_HUGE_PAGES
+ uintnat size = Round_mmap_size (sizeof (heap_chunk_head) + request);
+ void *block;
+ char *mem;
+ block = mmap (NULL, size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
+ if (block == MAP_FAILED) return NULL;
+ mem = (char *) block + sizeof (heap_chunk_head);
+ Chunk_size (mem) = size - sizeof (heap_chunk_head);
+ Chunk_block (mem) = block;
+ return mem;
+#else
+ return NULL;
+#endif
+ }else{
+ char *mem;
+ void *block;
+
+ request = ((request + Page_size - 1) >> Page_log) << Page_log;
+ mem = caml_stat_alloc_aligned_noexc (request + sizeof (heap_chunk_head),
+ sizeof (heap_chunk_head), &block);
+ if (mem == NULL) return NULL;
+ mem += sizeof (heap_chunk_head);
+ Chunk_size (mem) = request;
+ Chunk_block (mem) = block;
+ return mem;
+ }
+}
+
+/* Use this function if a block allocated with [caml_alloc_for_heap] is
+ not actually going to be added to the heap. The caller is responsible
+ for freeing it. */
+void caml_disown_for_heap (char* mem)
+{
+ /* Currently a no-op. */
+ (void)mem; /* can CAMLunused_{start,end} be used here? */
+}
+
+/* Use this function to free a block allocated with [caml_alloc_for_heap]
+ if you don't add it with [caml_add_to_heap].
+*/
+void caml_free_for_heap (char *mem)
+{
+ if (caml_use_huge_pages){
+#ifdef HAS_HUGE_PAGES
+ munmap (Chunk_block (mem), Chunk_size (mem) + sizeof (heap_chunk_head));
+#else
+ CAMLassert (0);
+#endif
+ }else{
+ caml_stat_free (Chunk_block (mem));
+ }
+}
+
+/* Take a chunk of memory as argument, which must be the result of a
+ call to [caml_alloc_for_heap], and insert it into the heap chaining.
+ The contents of the chunk must be a sequence of valid blocks and
+ fragments: no space between blocks and no trailing garbage. If
+ some blocks are blue, they must be added to the free list by the
+ caller. All other blocks must have the color [caml_allocation_color(m)].
+ The caller must update [caml_allocated_words] if applicable.
+ Return value: 0 if no error; -1 in case of error.
+
+ See also: caml_compact_heap, which duplicates most of this function.
+*/
+int caml_add_to_heap (char *m)
+{
+#ifdef DEBUG
+ /* Should check the contents of the block. */
+#endif /* DEBUG */
+
+ caml_gc_message (0x04, "Growing heap to %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ (Bsize_wsize (caml_stat_heap_wsz) + Chunk_size (m)) / 1024);
+
+ /* Register block in page table */
+ if (caml_page_table_add(In_heap, m, m + Chunk_size(m)) != 0)
+ return -1;
+
+ /* Chain this heap chunk. */
+ {
+ char **last = &caml_heap_start;
+ char *cur = *last;
+
+ while (cur != NULL && cur < m){
+ last = &(Chunk_next (cur));
+ cur = *last;
+ }
+ Chunk_next (m) = cur;
+ *last = m;
+
+ ++ caml_stat_heap_chunks;
+ }
+
+ caml_stat_heap_wsz += Wsize_bsize (Chunk_size (m));
+ if (caml_stat_heap_wsz > caml_stat_top_heap_wsz){
+ caml_stat_top_heap_wsz = caml_stat_heap_wsz;
+ }
+ return 0;
+}
+
+/* Allocate more memory from malloc for the heap.
+ Return a blue block of at least the requested size.
+ The blue block is chained to a sequence of blue blocks (through their
+ field 0); the last block of the chain is pointed by field 1 of the
+ first. There may be a fragment after the last block.
+ The caller must insert the blocks into the free list.
+ [request] is a number of words and must be less than or equal
+ to [Max_wosize].
+ Return NULL when out of memory.
+*/
+static value *expand_heap (mlsize_t request)
+{
+ /* these point to headers, but we do arithmetic on them, hence [value *]. */
+ value *mem, *hp, *prev;
+ asize_t over_request, malloc_request, remain;
+
+ CAMLassert (request <= Max_wosize);
+ over_request = request + request / 100 * caml_percent_free;
+ malloc_request = caml_clip_heap_chunk_wsz (over_request);
+ mem = (value *) caml_alloc_for_heap (Bsize_wsize (malloc_request));
+ if (mem == NULL){
+ caml_gc_message (0x04, "No room for growing heap\n");
+ return NULL;
+ }
+ remain = Wsize_bsize (Chunk_size (mem));
+ prev = hp = mem;
+ /* FIXME find a way to do this with a call to caml_make_free_blocks */
+ while (Wosize_whsize (remain) > Max_wosize){
+ Hd_hp (hp) = Make_header (Max_wosize, 0, Caml_blue);
+#ifdef DEBUG
+ caml_set_fields (Val_hp (hp), 0, Debug_free_major);
+#endif
+ hp += Whsize_wosize (Max_wosize);
+ remain -= Whsize_wosize (Max_wosize);
+ Field (Val_hp (mem), 1) = Field (Val_hp (prev), 0) = Val_hp (hp);
+ prev = hp;
+ }
+ if (remain > 1){
+ Hd_hp (hp) = Make_header (Wosize_whsize (remain), 0, Caml_blue);
+#ifdef DEBUG
+ caml_set_fields (Val_hp (hp), 0, Debug_free_major);
+#endif
+ Field (Val_hp (mem), 1) = Field (Val_hp (prev), 0) = Val_hp (hp);
+ Field (Val_hp (hp), 0) = (value) NULL;
+ }else{
+ Field (Val_hp (prev), 0) = (value) NULL;
+ if (remain == 1) {
+ Hd_hp (hp) = Make_header_allocated_here (0, 0, Caml_white);
+ }
+ }
+ CAMLassert (Wosize_hp (mem) >= request);
+ if (caml_add_to_heap ((char *) mem) != 0){
+ caml_free_for_heap ((char *) mem);
+ return NULL;
+ }
+ return Op_hp (mem);
+}
+
+/* Remove the heap chunk [chunk] from the heap and give the memory back
+ to [free].
+*/
+void caml_shrink_heap (char *chunk)
+{
+ char **cp;
+
+ /* Never deallocate the first chunk, because caml_heap_start is both the
+ first block and the base address for page numbers, and we don't
+ want to shift the page table, it's too messy (see above).
+ It will never happen anyway, because of the way compaction works.
+ (see compact.c)
+ XXX FIXME this has become false with the fix to PR#5389 (see compact.c)
+ */
+ if (chunk == caml_heap_start) return;
+
+ caml_stat_heap_wsz -= Wsize_bsize (Chunk_size (chunk));
+ caml_gc_message (0x04, "Shrinking heap to %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk words\n",
+ caml_stat_heap_wsz / 1024);
+
+#ifdef DEBUG
+ {
+ mlsize_t i;
+ for (i = 0; i < Wsize_bsize (Chunk_size (chunk)); i++){
+ ((value *) chunk) [i] = Debug_free_shrink;
+ }
+ }
+#endif
+
+ -- caml_stat_heap_chunks;
+
+ /* Remove [chunk] from the list of chunks. */
+ cp = &caml_heap_start;
+ while (*cp != chunk) cp = &(Chunk_next (*cp));
+ *cp = Chunk_next (chunk);
+
+ /* Remove the pages of [chunk] from the page table. */
+ caml_page_table_remove(In_heap, chunk, chunk + Chunk_size (chunk));
+
+ /* Free the [malloc] block that contains [chunk]. */
+ caml_free_for_heap (chunk);
+}
+
+color_t caml_allocation_color (void *hp)
+{
+ if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean
+ || (caml_gc_phase == Phase_sweep && (addr)hp >= (addr)caml_gc_sweep_hp)){
+ return Caml_black;
+ }else{
+ CAMLassert (caml_gc_phase == Phase_idle
+ || (caml_gc_phase == Phase_sweep
+ && (addr)hp < (addr)caml_gc_sweep_hp));
+ return Caml_white;
+ }
+}
+
+static inline value caml_alloc_shr_aux (mlsize_t wosize, tag_t tag,
+ int raise_oom, uintnat profinfo)
+{
+ header_t *hp;
+ value *new_block;
+
+ if (wosize > Max_wosize) {
+ if (raise_oom)
+ caml_raise_out_of_memory ();
+ else
+ return 0;
+ }
+ hp = caml_fl_allocate (wosize);
+ if (hp == NULL){
+ new_block = expand_heap (wosize);
+ if (new_block == NULL) {
+ if (!raise_oom)
+ return 0;
+ else if (caml_in_minor_collection)
+ caml_fatal_error ("Fatal error: out of memory.\n");
+ else
+ caml_raise_out_of_memory ();
+ }
+ caml_fl_add_blocks ((value) new_block);
+ hp = caml_fl_allocate (wosize);
+ }
+
+ CAMLassert (Is_in_heap (Val_hp (hp)));
+
+ /* Inline expansion of caml_allocation_color. */
+ if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean
+ || (caml_gc_phase == Phase_sweep && (addr)hp >= (addr)caml_gc_sweep_hp)){
+ Hd_hp (hp) = Make_header_with_profinfo (wosize, tag, Caml_black, profinfo);
+ }else{
+ CAMLassert (caml_gc_phase == Phase_idle
+ || (caml_gc_phase == Phase_sweep
+ && (addr)hp < (addr)caml_gc_sweep_hp));
+ Hd_hp (hp) = Make_header_with_profinfo (wosize, tag, Caml_white, profinfo);
+ }
+ CAMLassert (Hd_hp (hp)
+ == Make_header_with_profinfo (wosize, tag, caml_allocation_color (hp),
+ profinfo));
+ caml_allocated_words += Whsize_wosize (wosize);
+ if (caml_allocated_words > caml_minor_heap_wsz){
+ CAML_INSTR_INT ("request_major/alloc_shr@", 1);
+ caml_request_major_slice ();
+ }
+#ifdef DEBUG
+ {
+ uintnat i;
+ for (i = 0; i < wosize; i++){
+ Field (Val_hp (hp), i) = Debug_uninit_major;
+ }
+ }
+#endif
+ return Val_hp (hp);
+}
+
+CAMLexport value caml_alloc_shr_no_raise (mlsize_t wosize, tag_t tag)
+{
+ return caml_alloc_shr_aux(wosize, tag, 0, 0);
+}
+
+#ifdef WITH_PROFINFO
+
+/* Use this to debug problems with macros... */
+#define NO_PROFINFO 0xff
+
+CAMLexport value caml_alloc_shr_with_profinfo (mlsize_t wosize, tag_t tag,
+ intnat profinfo)
+{
+ return caml_alloc_shr_aux(wosize, tag, 1, profinfo);
+}
+
+CAMLexport value caml_alloc_shr_preserving_profinfo (mlsize_t wosize,
+ tag_t tag, header_t old_header)
+{
+ return caml_alloc_shr_with_profinfo (wosize, tag, Profinfo_hd(old_header));
+}
+
+#else
+#define NO_PROFINFO 0
+#endif /* WITH_PROFINFO */
+
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+#include "caml/spacetime.h"
+
+CAMLexport value caml_alloc_shr (mlsize_t wosize, tag_t tag)
+{
+ return caml_alloc_shr_with_profinfo (wosize, tag,
+ caml_spacetime_my_profinfo (NULL, wosize));
+}
+#else
+CAMLexport value caml_alloc_shr (mlsize_t wosize, tag_t tag)
+{
+ return caml_alloc_shr_aux (wosize, tag, 1, NO_PROFINFO);
+}
+#endif
+
+/* Dependent memory is all memory blocks allocated out of the heap
+ that depend on the GC (and finalizers) for deallocation.
+ For the GC to take dependent memory into account when computing
+ its automatic speed setting,
+ you must call [caml_alloc_dependent_memory] when you alloate some
+ dependent memory, and [caml_free_dependent_memory] when you
+ free it. In both cases, you pass as argument the size (in bytes)
+ of the block being allocated or freed.
+*/
+CAMLexport void caml_alloc_dependent_memory (mlsize_t nbytes)
+{
+ caml_dependent_size += nbytes / sizeof (value);
+ caml_dependent_allocated += nbytes / sizeof (value);
+}
+
+CAMLexport void caml_free_dependent_memory (mlsize_t nbytes)
+{
+ if (caml_dependent_size < nbytes / sizeof (value)){
+ caml_dependent_size = 0;
+ }else{
+ caml_dependent_size -= nbytes / sizeof (value);
+ }
+}
+
+/* Use this function to tell the major GC to speed up when you use
+ finalized blocks to automatically deallocate resources (other
+ than memory). The GC will do at least one cycle every [max]
+ allocated resources; [res] is the number of resources allocated
+ this time.
+ Note that only [res/max] is relevant. The units (and kind of
+ resource) can change between calls to [caml_adjust_gc_speed].
+*/
+CAMLexport void caml_adjust_gc_speed (mlsize_t res, mlsize_t max)
+{
+ if (max == 0) max = 1;
+ if (res > max) res = max;
+ caml_extra_heap_resources += (double) res / (double) max;
+ if (caml_extra_heap_resources > 1.0){
+ CAML_INSTR_INT ("request_major/adjust_gc_speed_1@", 1);
+ caml_extra_heap_resources = 1.0;
+ caml_request_major_slice ();
+ }
+ if (caml_extra_heap_resources
+ > (double) caml_minor_heap_wsz / 2.0
+ / (double) caml_stat_heap_wsz) {
+ CAML_INSTR_INT ("request_major/adjust_gc_speed_2@", 1);
+ caml_request_major_slice ();
+ }
+}
+
+/* You must use [caml_initialize] to store the initial value in a field of
+ a shared block, unless you are sure the value is not a young block.
+ A block value [v] is a shared block if and only if [Is_in_heap (v)]
+ is true.
+*/
+/* [caml_initialize] never calls the GC, so you may call it while a block is
+ unfinished (i.e. just after a call to [caml_alloc_shr].) */
+/* PR#6084 workaround: define it as a weak symbol */
+CAMLexport CAMLweakdef void caml_initialize (value *fp, value val)
+{
+ CAMLassert(Is_in_heap_or_young(fp));
+ *fp = val;
+ if (!Is_young((value)fp) && Is_block (val) && Is_young (val)) {
+ add_to_ref_table (&caml_ref_table, fp);
+ }
+}
+
+/* You must use [caml_modify] to change a field of an existing shared block,
+ unless you are sure the value being overwritten is not a shared block and
+ the value being written is not a young block. */
+/* [caml_modify] never calls the GC. */
+/* [caml_modify] can also be used to do assignment on data structures that are
+ in the minor heap instead of in the major heap. In this case, it
+ is a bit slower than simple assignment.
+ In particular, you can use [caml_modify] when you don't know whether the
+ block being changed is in the minor heap or the major heap. */
+/* PR#6084 workaround: define it as a weak symbol */
+
+CAMLexport CAMLweakdef void caml_modify (value *fp, value val)
+{
+ /* The write barrier implemented by [caml_modify] checks for the
+ following two conditions and takes appropriate action:
+ 1- a pointer from the major heap to the minor heap is created
+ --> add [fp] to the remembered set
+ 2- a pointer from the major heap to the major heap is overwritten,
+ while the GC is in the marking phase
+ --> call [caml_darken] on the overwritten pointer so that the
+ major GC treats it as an additional root.
+ */
+ value old;
+
+ if (Is_young((value)fp)) {
+ /* The modified object resides in the minor heap.
+ Conditions 1 and 2 cannot occur. */
+ *fp = val;
+ } else {
+ /* The modified object resides in the major heap. */
+ CAMLassert(Is_in_heap(fp));
+ old = *fp;
+ *fp = val;
+ if (Is_block(old)) {
+ /* If [old] is a pointer within the minor heap, we already
+ have a major->minor pointer and [fp] is already in the
+ remembered set. Conditions 1 and 2 cannot occur. */
+ if (Is_young(old)) return;
+ /* Here, [old] can be a pointer within the major heap.
+ Check for condition 2. */
+ if (caml_gc_phase == Phase_mark) caml_darken(old, NULL);
+ }
+ /* Check for condition 1. */
+ if (Is_block(val) && Is_young(val)) {
+ add_to_ref_table (&caml_ref_table, fp);
+ }
+ }
+}
+
+
+/* Global memory pool.
+
+ The pool is structured as a ring of blocks, where each block's header
+ contains two links: to the previous and to the next block. The data
+ structure allows for insertions and removals of blocks in constant time,
+ given that a pointer to the operated block is provided.
+
+ Initially, the pool contains a single block -- a pivot with no data, the
+ guaranteed existence of which makes for a more concise implementation.
+
+ The API functions that operate on the pool receive not pointers to the
+ block's header, but rather pointers to the block's "data" field. This
+ behaviour is required to maintain compatibility with the interfaces of
+ [malloc], [realloc], and [free] family of functions, as well as to hide
+ the implementation from the user.
+*/
+
+/* A type with the most strict alignment requirements */
+union max_align {
+ char c;
+ short s;
+ long l;
+ int i;
+ float f;
+ double d;
+ void *v;
+ void (*q)(void);
+};
+
+struct pool_block {
+#ifdef DEBUG
+ long magic;
+#endif
+ struct pool_block *next;
+ struct pool_block *prev;
+ /* Use C99's flexible array types if possible */
+#if (__STDC_VERSION__ >= 199901L)
+ union max_align data[]; /* not allocated, used for alignment purposes */
+#else
+ union max_align data[1];
+#endif
+};
+
+#if (__STDC_VERSION__ >= 199901L)
+#define SIZEOF_POOL_BLOCK sizeof(struct pool_block)
+#else
+#define SIZEOF_POOL_BLOCK offsetof(struct pool_block, data)
+#endif
+
+static struct pool_block *pool = NULL;
+
+
+/* Returns a pointer to the block header, given a pointer to "data" */
+static struct pool_block* get_pool_block(caml_stat_block b)
+{
+ if (b == NULL)
+ return NULL;
+
+ else {
+ struct pool_block *pb =
+ (struct pool_block*)(((char*)b) - SIZEOF_POOL_BLOCK);
+#ifdef DEBUG
+ CAMLassert(pb->magic == Debug_pool_magic);
+#endif
+ return pb;
+ }
+}
+
+CAMLexport void caml_stat_create_pool(void)
+{
+ if (pool == NULL) {
+ pool = malloc(SIZEOF_POOL_BLOCK);
+ if (pool == NULL)
+ caml_fatal_error("Fatal error: out of memory.\n");
+#ifdef DEBUG
+ pool->magic = Debug_pool_magic;
+#endif
+ pool->next = pool;
+ pool->prev = pool;
+ }
+}
+
+CAMLexport void caml_stat_destroy_pool(void)
+{
+ if (pool != NULL) {
+ pool->prev->next = NULL;
+ while (pool != NULL) {
+ struct pool_block *next = pool->next;
+ free(pool);
+ pool = next;
+ }
+ pool = NULL;
+ }
+}
+
+/* [sz] and [modulo] are numbers of bytes */
+CAMLexport void* caml_stat_alloc_aligned_noexc(asize_t sz, int modulo,
+ caml_stat_block *b)
+{
+ char *raw_mem;
+ uintnat aligned_mem;
+ CAMLassert (modulo < Page_size);
+ raw_mem = (char *) caml_stat_alloc_noexc(sz + Page_size);
+ if (raw_mem == NULL) return NULL;
+ *b = raw_mem;
+ raw_mem += modulo; /* Address to be aligned */
+ aligned_mem = (((uintnat) raw_mem / Page_size + 1) * Page_size);
+#ifdef DEBUG
+ {
+ uintnat *p;
+ uintnat *p0 = (void *) *b;
+ uintnat *p1 = (void *) (aligned_mem - modulo);
+ uintnat *p2 = (void *) (aligned_mem - modulo + sz);
+ uintnat *p3 = (void *) ((char *) *b + sz + Page_size);
+ for (p = p0; p < p1; p++) *p = Debug_filler_align;
+ for (p = p1; p < p2; p++) *p = Debug_uninit_align;
+ for (p = p2; p < p3; p++) *p = Debug_filler_align;
+ }
+#endif
+ return (char *) (aligned_mem - modulo);
+}
+
+/* [sz] and [modulo] are numbers of bytes */
+CAMLexport void* caml_stat_alloc_aligned(asize_t sz, int modulo,
+ caml_stat_block *b)
+{
+ void *result = caml_stat_alloc_aligned_noexc(sz, modulo, b);
+ /* malloc() may return NULL if size is 0 */
+ if ((result == NULL) && (sz != 0))
+ caml_raise_out_of_memory();
+ return result;
+}
+
+/* [sz] is a number of bytes */
+CAMLexport caml_stat_block caml_stat_alloc_noexc(asize_t sz)
+{
+ /* Backward compatibility mode */
+ if (pool == NULL)
+ return malloc(sz);
+ else {
+ struct pool_block *pb = malloc(sz + SIZEOF_POOL_BLOCK);
+ if (pb == NULL) return NULL;
+#ifdef DEBUG
+ memset(&(pb->data), Debug_uninit_stat, sz);
+ pb->magic = Debug_pool_magic;
+#endif
+
+ /* Linking the block into the ring */
+ pb->next = pool->next;
+ pb->prev = pool;
+ pool->next->prev = pb;
+ pool->next = pb;
+
+ return &(pb->data);
+ }
+}
+
+/* [sz] is a number of bytes */
+CAMLexport caml_stat_block caml_stat_alloc(asize_t sz)
+{
+ void *result = caml_stat_alloc_noexc(sz);
+ /* malloc() may return NULL if size is 0 */
+ if ((result == NULL) && (sz != 0))
+ caml_raise_out_of_memory();
+ return result;
+}
+
+CAMLexport void caml_stat_free(caml_stat_block b)
+{
+ /* Backward compatibility mode */
+ if (pool == NULL)
+ free(b);
+ else {
+ struct pool_block *pb = get_pool_block(b);
+ if (pb == NULL) return;
+
+ /* Unlinking the block from the ring */
+ pb->prev->next = pb->next;
+ pb->next->prev = pb->prev;
+
+ free(pb);
+ }
+}
+
+/* [sz] is a number of bytes */
+CAMLexport caml_stat_block caml_stat_resize_noexc(caml_stat_block b, asize_t sz)
+{
+ /* Backward compatibility mode */
+ if (pool == NULL)
+ return realloc(b, sz);
+ else {
+ struct pool_block *pb = get_pool_block(b);
+ struct pool_block *pb_new = realloc(pb, sz + SIZEOF_POOL_BLOCK);
+ if (pb_new == NULL) return NULL;
+
+ /* Relinking the new block into the ring in place of the old one */
+ pb_new->prev->next = pb_new;
+ pb_new->next->prev = pb_new;
+
+ return &(pb_new->data);
+ }
+}
+
+/* [sz] is a number of bytes */
+CAMLexport caml_stat_block caml_stat_resize(caml_stat_block b, asize_t sz)
+{
+ void *result = caml_stat_resize_noexc(b, sz);
+ if (result == NULL)
+ caml_raise_out_of_memory();
+ return result;
+}
+
+/* [sz] is a number of bytes */
+CAMLexport caml_stat_block caml_stat_calloc_noexc(asize_t num, asize_t sz)
+{
+ uintnat total;
+ if (caml_umul_overflow(sz, num, &total))
+ return NULL;
+ else {
+ caml_stat_block result = caml_stat_alloc_noexc(total);
+ if (result != NULL)
+ memset(result, 0, total);
+ return result;
+ }
+}
+
+CAMLexport caml_stat_string caml_stat_strdup_noexc(const char *s)
+{
+ size_t slen = strlen(s);
+ caml_stat_block result = caml_stat_alloc_noexc(slen + 1);
+ if (result == NULL)
+ return NULL;
+ memcpy(result, s, slen + 1);
+ return result;
+}
+
+CAMLexport caml_stat_string caml_stat_strdup(const char *s)
+{
+ caml_stat_string result = caml_stat_strdup_noexc(s);
+ if (result == NULL)
+ caml_raise_out_of_memory();
+ return result;
+}
+
+#ifdef _WIN32
+
+CAMLexport wchar_t * caml_stat_wcsdup(const wchar_t *s)
+{
+ int slen = wcslen(s);
+ wchar_t* result = caml_stat_alloc((slen + 1)*sizeof(wchar_t));
+ if (result == NULL)
+ caml_raise_out_of_memory();
+ memcpy(result, s, (slen + 1)*sizeof(wchar_t));
+ return result;
+}
+
+#endif
+
+CAMLexport caml_stat_string caml_stat_strconcat(int n, ...)
+{
+ va_list args;
+ char *result, *p;
+ size_t len = 0;
+ int i;
+
+ va_start(args, n);
+ for (i = 0; i < n; i++) {
+ const char *s = va_arg(args, const char*);
+ len += strlen(s);
+ }
+ va_end(args);
+
+ result = caml_stat_alloc(len + 1);
+
+ va_start(args, n);
+ p = result;
+ for (i = 0; i < n; i++) {
+ const char *s = va_arg(args, const char*);
+ size_t l = strlen(s);
+ memcpy(p, s, l);
+ p += l;
+ }
+ va_end(args);
+
+ *p = 0;
+ return result;
+}
+
+#ifdef _WIN32
+
+CAMLexport wchar_t* caml_stat_wcsconcat(int n, ...)
+{
+ va_list args;
+ wchar_t *result, *p;
+ size_t len = 0;
+ int i;
+
+ va_start(args, n);
+ for (i = 0; i < n; i++) {
+ const wchar_t *s = va_arg(args, const wchar_t*);
+ len += wcslen(s);
+ }
+ va_end(args);
+
+ result = caml_stat_alloc((len + 1)*sizeof(wchar_t));
+
+ va_start(args, n);
+ p = result;
+ for (i = 0; i < n; i++) {
+ const wchar_t *s = va_arg(args, const wchar_t*);
+ size_t l = wcslen(s);
+ memcpy(p, s, l*sizeof(wchar_t));
+ p += l;
+ }
+ va_end(args);
+
+ *p = 0;
+ return result;
+}
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/meta.c b/test/monniaux/ocaml/byterun/meta.c
new file mode 100644
index 00000000..03e0479d
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/meta.c
@@ -0,0 +1,234 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Primitives for the toplevel */
+
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/fix_code.h"
+#include "caml/interp.h"
+#include "caml/intext.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/prims.h"
+#include "caml/stacks.h"
+
+#ifndef NATIVE_CODE
+
+CAMLprim value caml_get_global_data(value unit)
+{
+ return caml_global_data;
+}
+
+char * caml_section_table = NULL;
+asize_t caml_section_table_size;
+
+CAMLprim value caml_get_section_table(value unit)
+{
+ if (caml_section_table == NULL) caml_raise_not_found();
+ return caml_input_value_from_block(caml_section_table,
+ caml_section_table_size);
+}
+
+CAMLprim value caml_reify_bytecode(value prog, value len)
+{
+ struct code_fragment * cf = caml_stat_alloc(sizeof(struct code_fragment));
+ value clos;
+
+ cf->code_start = (char *) prog;
+ cf->code_end = (char *) prog + Long_val(len);
+ cf->digest_computed = 0;
+ caml_ext_table_add(&caml_code_fragments_table, cf);
+
+#ifdef ARCH_BIG_ENDIAN
+ caml_fixup_endianness((code_t) prog, (asize_t) Long_val(len));
+#endif
+#ifdef THREADED_CODE
+ caml_thread_code((code_t) prog, (asize_t) Long_val(len));
+#endif
+ caml_prepare_bytecode((code_t) prog, (asize_t) Long_val(len));
+ clos = caml_alloc_small (1, Closure_tag);
+ Code_val(clos) = (code_t) prog;
+ return clos;
+}
+
+/* signal to the interpreter machinery that a bytecode is no more
+ needed (before freeing it) - this might be useful for a JIT
+ implementation */
+
+CAMLprim value caml_static_release_bytecode(value prog, value len)
+{
+ struct code_fragment * cf = NULL, * cfi;
+ int i;
+ for (i = 0; i < caml_code_fragments_table.size; i++) {
+ cfi = (struct code_fragment *) caml_code_fragments_table.contents[i];
+ if (cfi->code_start == (char *) prog &&
+ cfi->code_end == (char *) prog + Long_val(len)) {
+ cf = cfi;
+ break;
+ }
+ }
+
+ if (!cf) {
+ /* [cf] Not matched with a caml_reify_bytecode call; impossible. */
+ CAMLassert (0);
+ } else {
+ caml_ext_table_remove(&caml_code_fragments_table, cf);
+ }
+
+#ifndef NATIVE_CODE
+ caml_release_bytecode((code_t) prog, (asize_t) Long_val(len));
+#else
+ caml_failwith("Meta.static_release_bytecode impossible with native code");
+#endif
+ return Val_unit;
+}
+
+CAMLprim value caml_register_code_fragment(value prog, value len, value digest)
+{
+ struct code_fragment * cf = caml_stat_alloc(sizeof(struct code_fragment));
+ cf->code_start = (char *) prog;
+ cf->code_end = (char *) prog + Long_val(len);
+ memcpy(cf->digest, String_val(digest), 16);
+ cf->digest_computed = 1;
+ caml_ext_table_add(&caml_code_fragments_table, cf);
+ return Val_unit;
+}
+
+CAMLprim value caml_realloc_global(value size)
+{
+ mlsize_t requested_size, actual_size, i;
+ value new_global_data;
+
+ requested_size = Long_val(size);
+ actual_size = Wosize_val(caml_global_data);
+ if (requested_size >= actual_size) {
+ requested_size = (requested_size + 0x100) & 0xFFFFFF00;
+ caml_gc_message (0x08, "Growing global data to %"
+ ARCH_INTNAT_PRINTF_FORMAT "u entries\n",
+ requested_size);
+ new_global_data = caml_alloc_shr(requested_size, 0);
+ for (i = 0; i < actual_size; i++)
+ caml_initialize(&Field(new_global_data, i), Field(caml_global_data, i));
+ for (i = actual_size; i < requested_size; i++){
+ Field (new_global_data, i) = Val_long (0);
+ }
+ caml_global_data = new_global_data;
+ }
+ return Val_unit;
+}
+
+CAMLprim value caml_get_current_environment(value unit)
+{
+ return *caml_extern_sp;
+}
+
+CAMLprim value caml_invoke_traced_function(value codeptr, value env, value arg)
+{
+ /* Stack layout on entry:
+ return frame into instrument_closure function
+ arg3 to call_original_code (arg)
+ arg2 to call_original_code (env)
+ arg1 to call_original_code (codeptr)
+ arg3 to call_original_code (arg)
+ arg2 to call_original_code (env)
+ saved env */
+
+ /* Stack layout on exit:
+ return frame into instrument_closure function
+ actual arg to code (arg)
+ pseudo return frame into codeptr:
+ extra_args = 0
+ environment = env
+ PC = codeptr
+ arg3 to call_original_code (arg) same 6 bottom words as
+ arg2 to call_original_code (env) on entrance, but
+ arg1 to call_original_code (codeptr) shifted down 4 words
+ arg3 to call_original_code (arg)
+ arg2 to call_original_code (env)
+ saved env */
+
+ value * osp, * nsp;
+ int i;
+
+ osp = caml_extern_sp;
+ caml_extern_sp -= 4;
+ nsp = caml_extern_sp;
+ for (i = 0; i < 6; i++) nsp[i] = osp[i];
+ nsp[6] = codeptr;
+ nsp[7] = env;
+ nsp[8] = Val_int(0);
+ nsp[9] = arg;
+ return Val_unit;
+}
+
+#else
+
+/* Dummy definitions to support compilation of ocamlc.opt */
+
+value caml_get_global_data(value unit)
+{
+ caml_invalid_argument("Meta.get_global_data");
+ return Val_unit; /* not reached */
+}
+
+value caml_get_section_table(value unit)
+{
+ caml_invalid_argument("Meta.get_section_table");
+ return Val_unit; /* not reached */
+}
+
+value caml_realloc_global(value size)
+{
+ caml_invalid_argument("Meta.realloc_global");
+ return Val_unit; /* not reached */
+}
+
+value caml_invoke_traced_function(value codeptr, value env, value arg)
+{
+ caml_invalid_argument("Meta.invoke_traced_function");
+ return Val_unit; /* not reached */
+}
+
+value caml_reify_bytecode(value prog, value len)
+{
+ caml_invalid_argument("Meta.reify_bytecode");
+ return Val_unit; /* not reached */
+}
+
+value caml_static_release_bytecode(value prog, value len)
+{
+ caml_invalid_argument("Meta.static_release_bytecode");
+ return Val_unit; /* not reached */
+}
+
+value * caml_stack_low;
+value * caml_stack_high;
+value * caml_stack_threshold;
+value * caml_extern_sp;
+value * caml_trapsp;
+int caml_callback_depth;
+int volatile caml_something_to_do;
+void (* volatile caml_async_action_hook)(void);
+struct longjmp_buffer * caml_external_raise;
+
+#endif
diff --git a/test/monniaux/ocaml/byterun/minor_gc.c b/test/monniaux/ocaml/byterun/minor_gc.c
new file mode 100644
index 00000000..6aa5ed72
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/minor_gc.c
@@ -0,0 +1,558 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <string.h>
+#include "caml/custom.h"
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/finalise.h"
+#include "caml/gc.h"
+#include "caml/gc_ctrl.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/signals.h"
+#include "caml/weak.h"
+
+/* Pointers into the minor heap.
+ [caml_young_base]
+ The [malloc] block that contains the heap.
+ [caml_young_start] ... [caml_young_end]
+ The whole range of the minor heap: all young blocks are inside
+ this interval.
+ [caml_young_alloc_start]...[caml_young_alloc_end]
+ The allocation arena: newly-allocated blocks are carved from
+ this interval, starting at [caml_young_alloc_end].
+ [caml_young_alloc_mid] is the mid-point of this interval.
+ [caml_young_ptr], [caml_young_trigger], [caml_young_limit]
+ These pointers are all inside the allocation arena.
+ - [caml_young_ptr] is where the next allocation will take place.
+ - [caml_young_trigger] is how far we can allocate before triggering
+ [caml_gc_dispatch]. Currently, it is either [caml_young_alloc_start]
+ or the mid-point of the allocation arena.
+ - [caml_young_limit] is the pointer that is compared to
+ [caml_young_ptr] for allocation. It is either
+ [caml_young_alloc_end] if a signal is pending and we are in
+ native code, or [caml_young_trigger].
+*/
+
+struct generic_table CAML_TABLE_STRUCT(char);
+
+asize_t caml_minor_heap_wsz;
+static void *caml_young_base = NULL;
+CAMLexport value *caml_young_start = NULL, *caml_young_end = NULL;
+CAMLexport value *caml_young_alloc_start = NULL,
+ *caml_young_alloc_mid = NULL,
+ *caml_young_alloc_end = NULL;
+CAMLexport value *caml_young_ptr = NULL, *caml_young_limit = NULL;
+CAMLexport value *caml_young_trigger = NULL;
+
+CAMLexport struct caml_ref_table
+ caml_ref_table = { NULL, NULL, NULL, NULL, NULL, 0, 0};
+
+CAMLexport struct caml_ephe_ref_table
+ caml_ephe_ref_table = { NULL, NULL, NULL, NULL, NULL, 0, 0};
+
+CAMLexport struct caml_custom_table
+ caml_custom_table = { NULL, NULL, NULL, NULL, NULL, 0, 0};
+/* Table of custom blocks in the minor heap that contain finalizers
+ or GC speed parameters. */
+
+int caml_in_minor_collection = 0;
+
+double caml_extra_heap_resources_minor = 0;
+
+/* [sz] and [rsv] are numbers of entries */
+static void alloc_generic_table (struct generic_table *tbl, asize_t sz,
+ asize_t rsv, asize_t element_size)
+{
+ void *new_table;
+
+ tbl->size = sz;
+ tbl->reserve = rsv;
+ new_table = (void *) caml_stat_alloc_noexc((tbl->size + tbl->reserve) *
+ element_size);
+ if (new_table == NULL) caml_fatal_error ("Fatal error: not enough memory\n");
+ if (tbl->base != NULL) caml_stat_free (tbl->base);
+ tbl->base = new_table;
+ tbl->ptr = tbl->base;
+ tbl->threshold = tbl->base + tbl->size * element_size;
+ tbl->limit = tbl->threshold;
+ tbl->end = tbl->base + (tbl->size + tbl->reserve) * element_size;
+}
+
+void caml_alloc_table (struct caml_ref_table *tbl, asize_t sz, asize_t rsv)
+{
+ alloc_generic_table ((struct generic_table *) tbl, sz, rsv, sizeof (value *));
+}
+
+void caml_alloc_ephe_table (struct caml_ephe_ref_table *tbl, asize_t sz,
+ asize_t rsv)
+{
+ alloc_generic_table ((struct generic_table *) tbl, sz, rsv,
+ sizeof (struct caml_ephe_ref_elt));
+}
+
+void caml_alloc_custom_table (struct caml_custom_table *tbl, asize_t sz,
+ asize_t rsv)
+{
+ alloc_generic_table ((struct generic_table *) tbl, sz, rsv,
+ sizeof (struct caml_custom_elt));
+}
+
+static void reset_table (struct generic_table *tbl)
+{
+ tbl->size = 0;
+ tbl->reserve = 0;
+ if (tbl->base != NULL) caml_stat_free (tbl->base);
+ tbl->base = tbl->ptr = tbl->threshold = tbl->limit = tbl->end = NULL;
+}
+
+static void clear_table (struct generic_table *tbl)
+{
+ tbl->ptr = tbl->base;
+ tbl->limit = tbl->threshold;
+}
+
+void caml_set_minor_heap_size (asize_t bsz)
+{
+ char *new_heap;
+ void *new_heap_base;
+
+ CAMLassert (bsz >= Bsize_wsize(Minor_heap_min));
+ CAMLassert (bsz <= Bsize_wsize(Minor_heap_max));
+ CAMLassert (bsz % sizeof (value) == 0);
+ if (caml_young_ptr != caml_young_alloc_end){
+ CAML_INSTR_INT ("force_minor/set_minor_heap_size@", 1);
+ caml_requested_minor_gc = 0;
+ caml_young_trigger = caml_young_alloc_mid;
+ caml_young_limit = caml_young_trigger;
+ caml_empty_minor_heap ();
+ }
+ CAMLassert (caml_young_ptr == caml_young_alloc_end);
+ new_heap = caml_stat_alloc_aligned_noexc(bsz, 0, &new_heap_base);
+ if (new_heap == NULL) caml_raise_out_of_memory();
+ if (caml_page_table_add(In_young, new_heap, new_heap + bsz) != 0)
+ caml_raise_out_of_memory();
+
+ if (caml_young_start != NULL){
+ caml_page_table_remove(In_young, caml_young_start, caml_young_end);
+ caml_stat_free (caml_young_base);
+ }
+ caml_young_base = new_heap_base;
+ caml_young_start = (value *) new_heap;
+ caml_young_end = (value *) (new_heap + bsz);
+ caml_young_alloc_start = caml_young_start;
+ caml_young_alloc_mid = caml_young_alloc_start + Wsize_bsize (bsz) / 2;
+ caml_young_alloc_end = caml_young_end;
+ caml_young_trigger = caml_young_alloc_start;
+ caml_young_limit = caml_young_trigger;
+ caml_young_ptr = caml_young_alloc_end;
+ caml_minor_heap_wsz = Wsize_bsize (bsz);
+
+ reset_table ((struct generic_table *) &caml_ref_table);
+ reset_table ((struct generic_table *) &caml_ephe_ref_table);
+ reset_table ((struct generic_table *) &caml_custom_table);
+}
+
+static value oldify_todo_list = 0;
+
+/* Note that the tests on the tag depend on the fact that Infix_tag,
+ Forward_tag, and No_scan_tag are contiguous. */
+
+void caml_oldify_one (value v, value *p)
+{
+ value result;
+ header_t hd;
+ mlsize_t sz, i;
+ tag_t tag;
+
+ tail_call:
+ if (Is_block (v) && Is_young (v)){
+ CAMLassert ((value *) Hp_val (v) >= caml_young_ptr);
+ hd = Hd_val (v);
+ if (hd == 0){ /* If already forwarded */
+ *p = Field (v, 0); /* then forward pointer is first field. */
+ }else{
+ tag = Tag_hd (hd);
+ if (tag < Infix_tag){
+ value field0;
+
+ sz = Wosize_hd (hd);
+ result = caml_alloc_shr_preserving_profinfo (sz, tag, hd);
+ *p = result;
+ field0 = Field (v, 0);
+ Hd_val (v) = 0; /* Set forward flag */
+ Field (v, 0) = result; /* and forward pointer. */
+ if (sz > 1){
+ Field (result, 0) = field0;
+ Field (result, 1) = oldify_todo_list; /* Add this block */
+ oldify_todo_list = v; /* to the "to do" list. */
+ }else{
+ CAMLassert (sz == 1);
+ p = &Field (result, 0);
+ v = field0;
+ goto tail_call;
+ }
+ }else if (tag >= No_scan_tag){
+ sz = Wosize_hd (hd);
+ result = caml_alloc_shr_preserving_profinfo (sz, tag, hd);
+ for (i = 0; i < sz; i++) Field (result, i) = Field (v, i);
+ Hd_val (v) = 0; /* Set forward flag */
+ Field (v, 0) = result; /* and forward pointer. */
+ *p = result;
+ }else if (tag == Infix_tag){
+ mlsize_t offset = Infix_offset_hd (hd);
+ caml_oldify_one (v - offset, p); /* Cannot recurse deeper than 1. */
+ *p += offset;
+ }else{
+ value f = Forward_val (v);
+ tag_t ft = 0;
+ int vv = 1;
+
+ CAMLassert (tag == Forward_tag);
+ if (Is_block (f)){
+ if (Is_young (f)){
+ vv = 1;
+ ft = Tag_val (Hd_val (f) == 0 ? Field (f, 0) : f);
+ }else{
+ vv = Is_in_value_area(f);
+ if (vv){
+ ft = Tag_val (f);
+ }
+ }
+ }
+ if (!vv || ft == Forward_tag || ft == Lazy_tag
+#ifdef FLAT_FLOAT_ARRAY
+ || ft == Double_tag
+#endif
+ ){
+ /* Do not short-circuit the pointer. Copy as a normal block. */
+ CAMLassert (Wosize_hd (hd) == 1);
+ result = caml_alloc_shr_preserving_profinfo (1, Forward_tag, hd);
+ *p = result;
+ Hd_val (v) = 0; /* Set (GC) forward flag */
+ Field (v, 0) = result; /* and forward pointer. */
+ p = &Field (result, 0);
+ v = f;
+ goto tail_call;
+ }else{
+ v = f; /* Follow the forwarding */
+ goto tail_call; /* then oldify. */
+ }
+ }
+ }
+ }else{
+ *p = v;
+ }
+}
+
+/* Test if the ephemeron is alive, everything outside minor heap is alive */
+static inline int ephe_check_alive_data(struct caml_ephe_ref_elt *re){
+ mlsize_t i;
+ value child;
+ for (i = CAML_EPHE_FIRST_KEY; i < Wosize_val(re->ephe); i++){
+ child = Field (re->ephe, i);
+ if(child != caml_ephe_none
+ && Is_block (child) && Is_young (child)
+ && Hd_val (child) != 0){ /* Value not copied to major heap */
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/* Finish the work that was put off by [caml_oldify_one].
+ Note that [caml_oldify_one] itself is called by oldify_mopup, so we
+ have to be careful to remove the first entry from the list before
+ oldifying its fields. */
+void caml_oldify_mopup (void)
+{
+ value v, new_v, f;
+ mlsize_t i;
+ struct caml_ephe_ref_elt *re;
+ int redo = 0;
+
+ while (oldify_todo_list != 0){
+ v = oldify_todo_list; /* Get the head. */
+ CAMLassert (Hd_val (v) == 0); /* It must be forwarded. */
+ new_v = Field (v, 0); /* Follow forward pointer. */
+ oldify_todo_list = Field (new_v, 1); /* Remove from list. */
+
+ f = Field (new_v, 0);
+ if (Is_block (f) && Is_young (f)){
+ caml_oldify_one (f, &Field (new_v, 0));
+ }
+ for (i = 1; i < Wosize_val (new_v); i++){
+ f = Field (v, i);
+ if (Is_block (f) && Is_young (f)){
+ caml_oldify_one (f, &Field (new_v, i));
+ }else{
+ Field (new_v, i) = f;
+ }
+ }
+ }
+
+ /* Oldify the data in the minor heap of alive ephemeron
+ During minor collection keys outside the minor heap are considered alive */
+ for (re = caml_ephe_ref_table.base;
+ re < caml_ephe_ref_table.ptr; re++){
+ /* look only at ephemeron with data in the minor heap */
+ if (re->offset == 1){
+ value *data = &Field(re->ephe,1);
+ if (*data != caml_ephe_none && Is_block (*data) && Is_young (*data)){
+ if (Hd_val (*data) == 0){ /* Value copied to major heap */
+ *data = Field (*data, 0);
+ } else {
+ if (ephe_check_alive_data(re)){
+ caml_oldify_one(*data,data);
+ redo = 1; /* oldify_todo_list can still be 0 */
+ }
+ }
+ }
+ }
+ }
+
+ if (redo) caml_oldify_mopup ();
+}
+
+/* Make sure the minor heap is empty by performing a minor collection
+ if needed.
+*/
+void caml_empty_minor_heap (void)
+{
+ value **r;
+ struct caml_custom_elt *elt;
+ uintnat prev_alloc_words;
+ struct caml_ephe_ref_elt *re;
+
+ if (caml_young_ptr != caml_young_alloc_end){
+ if (caml_minor_gc_begin_hook != NULL) (*caml_minor_gc_begin_hook) ();
+ CAML_INSTR_SETUP (tmr, "minor");
+ prev_alloc_words = caml_allocated_words;
+ caml_in_minor_collection = 1;
+ caml_gc_message (0x02, "<");
+ caml_oldify_local_roots();
+ CAML_INSTR_TIME (tmr, "minor/local_roots");
+ for (r = caml_ref_table.base; r < caml_ref_table.ptr; r++){
+ caml_oldify_one (**r, *r);
+ }
+ CAML_INSTR_TIME (tmr, "minor/ref_table");
+ caml_oldify_mopup ();
+ CAML_INSTR_TIME (tmr, "minor/copy");
+ /* Update the ephemerons */
+ for (re = caml_ephe_ref_table.base;
+ re < caml_ephe_ref_table.ptr; re++){
+ if(re->offset < Wosize_val(re->ephe)){
+ /* If it is not the case, the ephemeron has been truncated */
+ value *key = &Field(re->ephe,re->offset);
+ if (*key != caml_ephe_none && Is_block (*key) && Is_young (*key)){
+ if (Hd_val (*key) == 0){ /* Value copied to major heap */
+ *key = Field (*key, 0);
+ }else{ /* Value not copied so it's dead */
+ CAMLassert(!ephe_check_alive_data(re));
+ *key = caml_ephe_none;
+ Field(re->ephe,1) = caml_ephe_none;
+ }
+ }
+ }
+ }
+ /* Update the OCaml finalise_last values */
+ caml_final_update_minor_roots();
+ /* Run custom block finalisation of dead minor values */
+ for (elt = caml_custom_table.base; elt < caml_custom_table.ptr; elt++){
+ value v = elt->block;
+ if (Hd_val (v) == 0){
+ /* Block was copied to the major heap: adjust GC speed numbers. */
+ caml_adjust_gc_speed(elt->mem, elt->max);
+ }else{
+ /* Block will be freed: call finalization function, if any. */
+ void (*final_fun)(value) = Custom_ops_val(v)->finalize;
+ if (final_fun != NULL) final_fun(v);
+ }
+ }
+ CAML_INSTR_TIME (tmr, "minor/update_weak");
+ caml_stat_minor_words += caml_young_alloc_end - caml_young_ptr;
+ caml_gc_clock += (double) (caml_young_alloc_end - caml_young_ptr)
+ / caml_minor_heap_wsz;
+ caml_young_ptr = caml_young_alloc_end;
+ clear_table ((struct generic_table *) &caml_ref_table);
+ clear_table ((struct generic_table *) &caml_ephe_ref_table);
+ clear_table ((struct generic_table *) &caml_custom_table);
+ caml_extra_heap_resources_minor = 0;
+ caml_gc_message (0x02, ">");
+ caml_in_minor_collection = 0;
+ caml_final_empty_young ();
+ CAML_INSTR_TIME (tmr, "minor/finalized");
+ caml_stat_promoted_words += caml_allocated_words - prev_alloc_words;
+ CAML_INSTR_INT ("minor/promoted#", caml_allocated_words - prev_alloc_words);
+ ++ caml_stat_minor_collections;
+ if (caml_minor_gc_end_hook != NULL) (*caml_minor_gc_end_hook) ();
+ }else{
+ /* The minor heap is empty nothing to do. */
+ caml_final_empty_young ();
+ }
+#ifdef DEBUG
+ {
+ value *p;
+ for (p = caml_young_alloc_start; p < caml_young_alloc_end; ++p){
+ *p = Debug_free_minor;
+ }
+ }
+#endif
+}
+
+#ifdef CAML_INSTR
+extern uintnat caml_instr_alloc_jump;
+#endif
+
+/* Do a minor collection or a slice of major collection, call finalisation
+ functions, etc.
+ Leave enough room in the minor heap to allocate at least one object.
+*/
+CAMLexport void caml_gc_dispatch (void)
+{
+ value *trigger = caml_young_trigger; /* save old value of trigger */
+#ifdef CAML_INSTR
+ CAML_INSTR_SETUP(tmr, "dispatch");
+ CAML_INSTR_TIME (tmr, "overhead");
+ CAML_INSTR_INT ("alloc/jump#", caml_instr_alloc_jump);
+ caml_instr_alloc_jump = 0;
+#endif
+
+ if (trigger == caml_young_alloc_start || caml_requested_minor_gc){
+ /* The minor heap is full, we must do a minor collection. */
+ /* reset the pointers first because the end hooks might allocate */
+ caml_requested_minor_gc = 0;
+ caml_young_trigger = caml_young_alloc_mid;
+ caml_young_limit = caml_young_trigger;
+ caml_empty_minor_heap ();
+ /* The minor heap is empty, we can start a major collection. */
+ if (caml_gc_phase == Phase_idle) caml_major_collection_slice (-1);
+ CAML_INSTR_TIME (tmr, "dispatch/minor");
+
+ caml_final_do_calls ();
+ CAML_INSTR_TIME (tmr, "dispatch/finalizers");
+
+ while (caml_young_ptr - caml_young_alloc_start < Max_young_whsize){
+ /* The finalizers or the hooks have filled up the minor heap, we must
+ repeat the minor collection. */
+ caml_requested_minor_gc = 0;
+ caml_young_trigger = caml_young_alloc_mid;
+ caml_young_limit = caml_young_trigger;
+ caml_empty_minor_heap ();
+ /* The minor heap is empty, we can start a major collection. */
+ if (caml_gc_phase == Phase_idle) caml_major_collection_slice (-1);
+ CAML_INSTR_TIME (tmr, "dispatch/finalizers_minor");
+ }
+ }
+ if (trigger != caml_young_alloc_start || caml_requested_major_slice){
+ /* The minor heap is half-full, do a major GC slice. */
+ caml_requested_major_slice = 0;
+ caml_young_trigger = caml_young_alloc_start;
+ caml_young_limit = caml_young_trigger;
+ caml_major_collection_slice (-1);
+ CAML_INSTR_TIME (tmr, "dispatch/major");
+ }
+}
+
+/* For backward compatibility with Lablgtk: do a minor collection to
+ ensure that the minor heap is empty.
+*/
+CAMLexport void caml_minor_collection (void)
+{
+ caml_requested_minor_gc = 1;
+ caml_gc_dispatch ();
+}
+
+CAMLexport value caml_check_urgent_gc (value extra_root)
+{
+ CAMLparam1 (extra_root);
+ if (caml_requested_major_slice || caml_requested_minor_gc){
+ CAML_INSTR_INT ("force_minor/check_urgent_gc@", 1);
+ caml_gc_dispatch();
+ }
+ CAMLreturn (extra_root);
+}
+
+static void realloc_generic_table
+(struct generic_table *tbl, asize_t element_size,
+ char * msg_intr_int, char *msg_threshold, char *msg_growing, char *msg_error)
+{
+ CAMLassert (tbl->ptr == tbl->limit);
+ CAMLassert (tbl->limit <= tbl->end);
+ CAMLassert (tbl->limit >= tbl->threshold);
+
+ if (tbl->base == NULL){
+ alloc_generic_table (tbl, caml_minor_heap_wsz / 8, 256,
+ element_size);
+ }else if (tbl->limit == tbl->threshold){
+ CAML_INSTR_INT (msg_intr_int, 1);
+ caml_gc_message (0x08, msg_threshold, 0);
+ tbl->limit = tbl->end;
+ caml_request_minor_gc ();
+ }else{
+ asize_t sz;
+ asize_t cur_ptr = tbl->ptr - tbl->base;
+ CAMLassert (caml_requested_minor_gc);
+
+ tbl->size *= 2;
+ sz = (tbl->size + tbl->reserve) * element_size;
+ caml_gc_message (0x08, msg_growing, (intnat) sz/1024);
+ tbl->base = caml_stat_resize_noexc (tbl->base, sz);
+ if (tbl->base == NULL){
+ caml_fatal_error (msg_error);
+ }
+ tbl->end = tbl->base + (tbl->size + tbl->reserve) * element_size;
+ tbl->threshold = tbl->base + tbl->size * element_size;
+ tbl->ptr = tbl->base + cur_ptr;
+ tbl->limit = tbl->end;
+ }
+}
+
+void caml_realloc_ref_table (struct caml_ref_table *tbl)
+{
+ realloc_generic_table
+ ((struct generic_table *) tbl, sizeof (value *),
+ "request_minor/realloc_ref_table@",
+ "ref_table threshold crossed\n",
+ "Growing ref_table to %" ARCH_INTNAT_PRINTF_FORMAT "dk bytes\n",
+ "Fatal error: ref_table overflow\n");
+}
+
+void caml_realloc_ephe_ref_table (struct caml_ephe_ref_table *tbl)
+{
+ realloc_generic_table
+ ((struct generic_table *) tbl, sizeof (struct caml_ephe_ref_elt),
+ "request_minor/realloc_ephe_ref_table@",
+ "ephe_ref_table threshold crossed\n",
+ "Growing ephe_ref_table to %" ARCH_INTNAT_PRINTF_FORMAT "dk bytes\n",
+ "Fatal error: ephe_ref_table overflow\n");
+}
+
+void caml_realloc_custom_table (struct caml_custom_table *tbl)
+{
+ realloc_generic_table
+ ((struct generic_table *) tbl, sizeof (struct caml_custom_elt),
+ "request_minor/realloc_custom_table@",
+ "custom_table threshold crossed\n",
+ "Growing custom_table to %" ARCH_INTNAT_PRINTF_FORMAT "dk bytes\n",
+ "Fatal error: custom_table overflow\n");
+}
diff --git a/test/monniaux/ocaml/byterun/misc.c b/test/monniaux/ocaml/byterun/misc.c
new file mode 100644
index 00000000..46e40992
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/misc.c
@@ -0,0 +1,276 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include "caml/config.h"
+#include "caml/misc.h"
+#include "caml/memory.h"
+#include "caml/osdeps.h"
+#include "caml/version.h"
+
+caml_timing_hook caml_major_slice_begin_hook = NULL;
+caml_timing_hook caml_major_slice_end_hook = NULL;
+caml_timing_hook caml_minor_gc_begin_hook = NULL;
+caml_timing_hook caml_minor_gc_end_hook = NULL;
+caml_timing_hook caml_finalise_begin_hook = NULL;
+caml_timing_hook caml_finalise_end_hook = NULL;
+
+#ifdef DEBUG
+
+int caml_failed_assert (char * expr, char * file, int line)
+{
+ fprintf (stderr, "file %s; line %d ### Assertion failed: %s\n",
+ file, line, expr);
+ fflush (stderr);
+ abort();
+}
+
+void caml_set_fields (value v, unsigned long start, unsigned long filler)
+{
+ mlsize_t i;
+ for (i = start; i < Wosize_val (v); i++){
+ Field (v, i) = (value) filler;
+ }
+}
+
+#endif /* DEBUG */
+
+uintnat caml_verb_gc = 0;
+
+void caml_gc_message (int level, char *msg, ...)
+{
+ if ((caml_verb_gc & level) != 0){
+ va_list ap;
+ va_start(ap, msg);
+ vfprintf (stderr, msg, ap);
+ va_end(ap);
+ fflush (stderr);
+ }
+}
+
+CAMLexport void caml_fatal_error (char *msg)
+{
+ fprintf (stderr, "%s", msg);
+ exit(2);
+}
+
+CAMLexport void caml_fatal_error_arg (char *fmt, char *arg)
+{
+ fprintf (stderr, fmt, arg);
+ exit(2);
+}
+
+CAMLexport void caml_fatal_error_arg2 (char *fmt1, char *arg1,
+ char *fmt2, char *arg2)
+{
+ fprintf (stderr, fmt1, arg1);
+ fprintf (stderr, fmt2, arg2);
+ exit(2);
+}
+
+/* If you change the caml_ext_table* functions, also update
+ asmrun/spacetime.c:find_trie_node_from_libunwind. */
+
+void caml_ext_table_init(struct ext_table * tbl, int init_capa)
+{
+ tbl->size = 0;
+ tbl->capacity = init_capa;
+ tbl->contents = caml_stat_alloc(sizeof(void *) * init_capa);
+}
+
+int caml_ext_table_add(struct ext_table * tbl, caml_stat_block data)
+{
+ int res;
+ if (tbl->size >= tbl->capacity) {
+ tbl->capacity *= 2;
+ tbl->contents =
+ caml_stat_resize(tbl->contents, sizeof(void *) * tbl->capacity);
+ }
+ res = tbl->size;
+ tbl->contents[res] = data;
+ tbl->size++;
+ return res;
+}
+
+void caml_ext_table_remove(struct ext_table * tbl, caml_stat_block data)
+{
+ int i;
+ for (i = 0; i < tbl->size; i++) {
+ if (tbl->contents[i] == data) {
+ caml_stat_free(tbl->contents[i]);
+ memmove(&tbl->contents[i], &tbl->contents[i + 1],
+ (tbl->size - i - 1) * sizeof(void *));
+ tbl->size--;
+ }
+ }
+}
+
+void caml_ext_table_clear(struct ext_table * tbl, int free_entries)
+{
+ int i;
+ if (free_entries) {
+ for (i = 0; i < tbl->size; i++) caml_stat_free(tbl->contents[i]);
+ }
+ tbl->size = 0;
+}
+
+void caml_ext_table_free(struct ext_table * tbl, int free_entries)
+{
+ caml_ext_table_clear(tbl, free_entries);
+ caml_stat_free(tbl->contents);
+}
+
+/* Integer arithmetic with overflow detection */
+
+#if ! (__GNUC__ >= 5 || Caml_has_builtin(__builtin_mul_overflow))
+CAMLexport int caml_umul_overflow(uintnat a, uintnat b, uintnat * res)
+{
+#define HALF_SIZE (sizeof(uintnat) * 4)
+#define HALF_MASK (((uintnat)1 << HALF_SIZE) - 1)
+#define LOW_HALF(x) ((x) & HALF_MASK)
+#define HIGH_HALF(x) ((x) >> HALF_SIZE)
+ /* Cut in half words */
+ uintnat al = LOW_HALF(a);
+ uintnat ah = HIGH_HALF(a);
+ uintnat bl = LOW_HALF(b);
+ uintnat bh = HIGH_HALF(b);
+ /* Exact product is:
+ al * bl
+ + ah * bl << HALF_SIZE
+ + al * bh << HALF_SIZE
+ + ah * bh << 2*HALF_SIZE
+ Overflow occurs if:
+ ah * bh is not 0, i.e. ah != 0 and bh != 0
+ OR ah * bl has high half != 0
+ OR al * bh has high half != 0
+ OR the sum al * bl + LOW_HALF(ah * bl) << HALF_SIZE
+ + LOW_HALF(al * bh) << HALF_SIZE overflows.
+ This sum is equal to p = (a * b) modulo word size. */
+ uintnat p = a * b;
+ uintnat p1 = al * bh;
+ uintnat p2 = ah * bl;
+ *res = p;
+ if (ah == 0 && bh == 0) return 0;
+ if (ah != 0 && bh != 0) return 1;
+ if (HIGH_HALF(p1) != 0 || HIGH_HALF(p2) != 0) return 1;
+ p1 <<= HALF_SIZE;
+ p2 <<= HALF_SIZE;
+ p1 += p2;
+ if (p < p1 || p1 < p2) return 1; /* overflow in sums */
+ return 0;
+#undef HALF_SIZE
+#undef HALF_MASK
+#undef LOW_HALF
+#undef HIGH_HALF
+}
+#endif
+
+/* Runtime warnings */
+
+uintnat caml_runtime_warnings = 0;
+static int caml_runtime_warnings_first = 1;
+
+int caml_runtime_warnings_active(void)
+{
+ if (!caml_runtime_warnings) return 0;
+ if (caml_runtime_warnings_first) {
+ fprintf(stderr, "[ocaml] (use Sys.enable_runtime_warnings to control "
+ "these warnings)\n");
+ caml_runtime_warnings_first = 0;
+ }
+ return 1;
+}
+
+#ifdef CAML_INSTR
+/* Timers for profiling GC and allocation (experimental, Linux-only) */
+
+#include <limits.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+struct CAML_INSTR_BLOCK *CAML_INSTR_LOG = NULL;
+intnat CAML_INSTR_STARTTIME, CAML_INSTR_STOPTIME;
+
+#define Get_time(p,i) ((p)->ts[(i)].tv_nsec + 1000000000 * (p)->ts[(i)].tv_sec)
+
+void CAML_INSTR_INIT (void)
+{
+ char *s;
+
+ CAML_INSTR_STARTTIME = 0;
+ s = caml_secure_getenv ("OCAML_INSTR_START");
+ if (s != NULL) CAML_INSTR_STARTTIME = atol (s);
+ CAML_INSTR_STOPTIME = LONG_MAX;
+ s = caml_secure_getenv ("OCAML_INSTR_STOP");
+ if (s != NULL) CAML_INSTR_STOPTIME = atol (s);
+}
+
+void CAML_INSTR_ATEXIT (void)
+{
+ int i;
+ struct CAML_INSTR_BLOCK *p, *prev, *next;
+ FILE *f = NULL;
+ char *fname;
+
+ fname = caml_secure_getenv ("OCAML_INSTR_FILE");
+ if (fname != NULL){
+ char *mode = "a";
+ char buf [1000];
+ char *name = fname;
+
+ if (name[0] == '@'){
+ snprintf (buf, sizeof(buf), "%s.%d", name + 1, getpid ());
+ name = buf;
+ }
+ if (name[0] == '+'){
+ mode = "a";
+ name = name + 1;
+ }else if (name [0] == '>' || name[0] == '-'){
+ mode = "w";
+ name = name + 1;
+ }
+ f = fopen (name, mode);
+ }
+
+ if (f != NULL){
+ /* reverse the list */
+ prev = NULL;
+ p = CAML_INSTR_LOG;
+ while (p != NULL){
+ next = p->next;
+ p->next = prev;
+ prev = p;
+ p = next;
+ }
+ CAML_INSTR_LOG = prev;
+ fprintf (f, "==== OCAML INSTRUMENTATION DATA %s\n", OCAML_VERSION_STRING);
+ for (p = CAML_INSTR_LOG; p != NULL; p = p->next){
+ for (i = 0; i < p->index; i++){
+ fprintf (f, "@@ %19ld %19ld %s\n",
+ (long) Get_time (p, i), (long) Get_time(p, i+1), p->tag[i+1]);
+ }
+ if (p->tag[0][0] != '\000'){
+ fprintf (f, "@@ %19ld %19ld %s\n",
+ (long) Get_time (p, 0), (long) Get_time(p, p->index), p->tag[0]);
+ }
+ }
+ fclose (f);
+ }
+}
+#endif /* CAML_INSTR */
diff --git a/test/monniaux/ocaml/byterun/obj.c b/test/monniaux/ocaml/byterun/obj.c
new file mode 100644
index 00000000..4567b8ae
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/obj.c
@@ -0,0 +1,390 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Operations on objects */
+
+#include <string.h>
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/gc.h"
+#include "caml/interp.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/prims.h"
+#include "caml/spacetime.h"
+
+/* [size] is a value encoding a number of bytes */
+CAMLprim value caml_static_alloc(value size)
+{
+ return (value) caml_stat_alloc((asize_t) Long_val(size));
+}
+
+CAMLprim value caml_static_free(value blk)
+{
+ caml_stat_free((void *) blk);
+ return Val_unit;
+}
+
+CAMLprim value caml_static_resize(value blk, value new_size)
+{
+ return (value) caml_stat_resize((char *) blk, (asize_t) Long_val(new_size));
+}
+
+/* unused since GPR#427 */
+CAMLprim value caml_obj_is_block(value arg)
+{
+ return Val_bool(Is_block(arg));
+}
+
+CAMLprim value caml_obj_tag(value arg)
+{
+ if (Is_long (arg)){
+ return Val_int (1000); /* int_tag */
+ }else if ((long) arg & (sizeof (value) - 1)){
+ return Val_int (1002); /* unaligned_tag */
+ }else if (Is_in_value_area (arg)){
+ return Val_int(Tag_val(arg));
+ }else{
+ return Val_int (1001); /* out_of_heap_tag */
+ }
+}
+
+CAMLprim value caml_obj_set_tag (value arg, value new_tag)
+{
+ Tag_val (arg) = Int_val (new_tag);
+ return Val_unit;
+}
+
+/* [size] is a value encoding a number of blocks */
+CAMLprim value caml_obj_block(value tag, value size)
+{
+ value res;
+ mlsize_t sz, i;
+ tag_t tg;
+
+ sz = Long_val(size);
+ tg = Long_val(tag);
+ if (sz == 0) return Atom(tg);
+ res = caml_alloc(sz, tg);
+ for (i = 0; i < sz; i++)
+ Field(res, i) = Val_long(0);
+
+ return res;
+}
+
+/* Spacetime profiling assumes that this function is only called from OCaml. */
+CAMLprim value caml_obj_dup(value arg)
+{
+ CAMLparam1 (arg);
+ CAMLlocal1 (res);
+ mlsize_t sz, i;
+ tag_t tg;
+
+ sz = Wosize_val(arg);
+ if (sz == 0) CAMLreturn (arg);
+ tg = Tag_val(arg);
+ if (tg >= No_scan_tag) {
+ res = caml_alloc(sz, tg);
+ memcpy(Bp_val(res), Bp_val(arg), sz * sizeof(value));
+ } else if (sz <= Max_young_wosize) {
+ uintnat profinfo;
+ Get_my_profinfo_with_cached_backtrace(profinfo, sz);
+ res = caml_alloc_small_with_my_or_given_profinfo(sz, tg, profinfo);
+ for (i = 0; i < sz; i++) Field(res, i) = Field(arg, i);
+ } else {
+ res = caml_alloc_shr(sz, tg);
+ for (i = 0; i < sz; i++) caml_initialize(&Field(res, i), Field(arg, i));
+ }
+ CAMLreturn (res);
+}
+
+/* Shorten the given block to the given size and return void.
+ Raise Invalid_argument if the given size is less than or equal
+ to 0 or greater than the current size.
+
+ algorithm:
+ Change the length field of the header. Make up a black object
+ with the leftover part of the object: this is needed in the major
+ heap and harmless in the minor heap. The object cannot be white
+ because there may still be references to it in the ref table. By
+ using a black object we ensure that the ref table will be emptied
+ before the block is reallocated (since there must be a minor
+ collection within each major cycle).
+
+ [newsize] is a value encoding a number of fields (words, except
+ for float arrays on 32-bit architectures).
+*/
+CAMLprim value caml_obj_truncate (value v, value newsize)
+{
+ mlsize_t new_wosize = Long_val (newsize);
+ header_t hd = Hd_val (v);
+ tag_t tag = Tag_hd (hd);
+ color_t color = Color_hd (hd);
+ mlsize_t wosize = Wosize_hd (hd);
+ mlsize_t i;
+
+ if (tag == Double_array_tag) new_wosize *= Double_wosize; /* PR#156 */
+
+ if (new_wosize <= 0 || new_wosize > wosize){
+ caml_invalid_argument ("Obj.truncate");
+ }
+ if (new_wosize == wosize) return Val_unit;
+ /* PR#61: since we're about to lose our references to the elements
+ beyond new_wosize in v, erase them explicitly so that the GC
+ can darken them as appropriate. */
+ if (tag < No_scan_tag) {
+ for (i = new_wosize; i < wosize; i++){
+ caml_modify(&Field(v, i), Val_unit);
+#ifdef DEBUG
+ Field (v, i) = Debug_free_truncate;
+#endif
+ }
+ }
+ /* We must use an odd tag for the header of the leftovers so it does not
+ look like a pointer because there may be some references to it in
+ ref_table. */
+ Field (v, new_wosize) =
+ Make_header (Wosize_whsize (wosize-new_wosize), Abstract_tag, Caml_black);
+ Hd_val (v) =
+ Make_header_with_profinfo (new_wosize, tag, color, Profinfo_val(v));
+ return Val_unit;
+}
+
+CAMLprim value caml_obj_add_offset (value v, value offset)
+{
+ return v + (unsigned long) Int32_val (offset);
+}
+
+/* The following functions are used in stdlib/lazy.ml.
+ They are not written in OCaml because they must be atomic with respect
+ to the GC.
+ */
+
+CAMLprim value caml_lazy_follow_forward (value v)
+{
+ if (Is_block (v) && Is_in_value_area(v)
+ && Tag_val (v) == Forward_tag){
+ return Forward_val (v);
+ }else{
+ return v;
+ }
+}
+
+CAMLprim value caml_lazy_make_forward (value v)
+{
+ CAMLparam1 (v);
+ CAMLlocal1 (res);
+
+ res = caml_alloc_small (1, Forward_tag);
+ Field (res, 0) = v;
+ CAMLreturn (res);
+}
+
+/* For mlvalues.h and camlinternalOO.ml
+ See also GETPUBMET in interp.c
+ */
+
+CAMLprim value caml_get_public_method (value obj, value tag)
+{
+ value meths = Field (obj, 0);
+ int li = 3, hi = Field(meths,0), mi;
+ while (li < hi) {
+ mi = ((li+hi) >> 1) | 1;
+ if (tag < Field(meths,mi)) hi = mi-2;
+ else li = mi;
+ }
+ /* return 0 if tag is not there */
+ return (tag == Field(meths,li) ? Field (meths, li-1) : 0);
+}
+
+/* these two functions might be useful to an hypothetical JIT */
+
+#ifdef CAML_JIT
+#ifdef NATIVE_CODE
+#define MARK 1
+#else
+#define MARK 0
+#endif
+value caml_cache_public_method (value meths, value tag, value *cache)
+{
+ int li = 3, hi = Field(meths,0), mi;
+ while (li < hi) {
+ mi = ((li+hi) >> 1) | 1;
+ if (tag < Field(meths,mi)) hi = mi-2;
+ else li = mi;
+ }
+ *cache = (li-3)*sizeof(value) + MARK;
+ return Field (meths, li-1);
+}
+
+value caml_cache_public_method2 (value *meths, value tag, value *cache)
+{
+ value ofs = *cache & meths[1];
+ if (*(value*)(((char*)(meths+3)) + ofs - MARK) == tag)
+ return *(value*)(((char*)(meths+2)) + ofs - MARK);
+ {
+ int li = 3, hi = meths[0], mi;
+ while (li < hi) {
+ mi = ((li+hi) >> 1) | 1;
+ if (tag < meths[mi]) hi = mi-2;
+ else li = mi;
+ }
+ *cache = (li-3)*sizeof(value) + MARK;
+ return meths[li-1];
+ }
+}
+#endif /*CAML_JIT*/
+
+static value oo_last_id = Val_int(0);
+
+CAMLprim value caml_set_oo_id (value obj) {
+ Field(obj, 1) = oo_last_id;
+ oo_last_id += 2;
+ return obj;
+}
+
+CAMLprim value caml_fresh_oo_id (value v) {
+ v = oo_last_id;
+ oo_last_id += 2;
+ return v;
+}
+
+CAMLprim value caml_int_as_pointer (value n) {
+ return n - 1;
+}
+
+/* Compute how many words in the heap are occupied by blocks accessible
+ from a given value */
+
+#define ENTRIES_PER_QUEUE_CHUNK 4096
+struct queue_chunk {
+ struct queue_chunk *next;
+ value entries[ENTRIES_PER_QUEUE_CHUNK];
+};
+
+
+CAMLprim value caml_obj_reachable_words(value v)
+{
+ static struct queue_chunk first_chunk;
+ struct queue_chunk *read_chunk, *write_chunk;
+ int write_pos, read_pos, i;
+
+ intnat size = 0;
+ header_t hd;
+ mlsize_t sz;
+
+ if (Is_long(v) || !Is_in_heap_or_young(v)) return Val_int(0);
+ if (Tag_hd(Hd_val(v)) == Infix_tag) v -= Infix_offset_hd(Hd_val(v));
+ hd = Hd_val(v);
+ sz = Wosize_hd(hd);
+
+ read_chunk = write_chunk = &first_chunk;
+ read_pos = 0;
+ write_pos = 1;
+ write_chunk->entries[0] = v | Colornum_hd(hd);
+ Hd_val(v) = Bluehd_hd(hd);
+
+ /* We maintain a queue of "interesting" blocks that have been seen.
+ An interesting block is a block in the heap which does not
+ represent an infix pointer. Infix pointers are normalized to the
+ beginning of their block. Blocks in the static data area are excluded.
+
+ The function maintains a queue of block pointers. Concretely,
+ the queue is stored as a linked list of chunks, each chunk
+ holding a number of pointers to interesting blocks. Initially,
+ it contains only the "root" value. The first chunk of the queue
+ is allocated statically. More chunks can be allocated as needed
+ and released before this function exits.
+
+ When a block is inserted in the queue, it is marked as blue.
+ This mark is used to avoid a second visit of the same block.
+ The real color is stored in the last 2 bits of the pointer in the
+ queue. (Same technique as in extern.c.)
+
+ Note: we make the assumption that there is no pointer
+ from the static data area to the heap.
+ */
+
+ /* First pass: mark accessible blocks and compute their total size */
+ while (read_pos != write_pos || read_chunk != write_chunk) {
+ /* Pop the next element from the queue */
+ if (read_pos == ENTRIES_PER_QUEUE_CHUNK) {
+ read_pos = 0;
+ read_chunk = read_chunk->next;
+ }
+ v = read_chunk->entries[read_pos++] & ~3;
+
+ hd = Hd_val(v);
+ sz = Wosize_hd(hd);
+
+ size += Whsize_wosize(sz);
+
+ if (Tag_hd(hd) < No_scan_tag) {
+ /* Push the interesting fields on the queue */
+ for (i = 0; i < sz; i++) {
+ value v2 = Field(v, i);
+ if (Is_block(v2) && Is_in_heap_or_young(v2)) {
+ if (Tag_hd(Hd_val(v2)) == Infix_tag){
+ v2 -= Infix_offset_hd(Hd_val(v2));
+ }
+ hd = Hd_val(v2);
+ if (Color_hd(hd) != Caml_blue) {
+ if (write_pos == ENTRIES_PER_QUEUE_CHUNK) {
+ struct queue_chunk *new_chunk =
+ malloc(sizeof(struct queue_chunk));
+ if (new_chunk == NULL) {
+ size = (-1);
+ goto release;
+ }
+ write_chunk->next = new_chunk;
+ write_pos = 0;
+ write_chunk = new_chunk;
+ }
+ write_chunk->entries[write_pos++] = v2 | Colornum_hd(hd);
+ Hd_val(v2) = Bluehd_hd(hd);
+ }
+ }
+ }
+ }
+ }
+
+ /* Second pass: restore colors and free extra queue chunks */
+ release:
+ read_pos = 0;
+ read_chunk = &first_chunk;
+ while (read_pos != write_pos || read_chunk != write_chunk) {
+ color_t colornum;
+ if (read_pos == ENTRIES_PER_QUEUE_CHUNK) {
+ struct queue_chunk *prev = read_chunk;
+ read_pos = 0;
+ read_chunk = read_chunk->next;
+ if (prev != &first_chunk) free(prev);
+ }
+ v = read_chunk->entries[read_pos++];
+ colornum = v & 3;
+ v &= ~3;
+ Hd_val(v) = Coloredhd_hd(Hd_val(v), colornum);
+ }
+ if (read_chunk != &first_chunk) free(read_chunk);
+
+ if (size < 0)
+ caml_raise_out_of_memory();
+ return Val_int(size);
+}
diff --git a/test/monniaux/ocaml/byterun/parsing.c b/test/monniaux/ocaml/byterun/parsing.c
new file mode 100644
index 00000000..990eb1f6
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/parsing.c
@@ -0,0 +1,304 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* The PDA automaton for parsers generated by camlyacc */
+
+#include <stdio.h>
+#include <string.h>
+#include "caml/config.h"
+#include "caml/mlvalues.h"
+#include "caml/memory.h"
+#include "caml/alloc.h"
+
+#define ERRCODE 256
+
+struct parser_tables { /* Mirrors parse_tables in ../stdlib/parsing.mli */
+ value actions;
+ value transl_const;
+ value transl_block;
+ char * lhs;
+ char * len;
+ char * defred;
+ char * dgoto;
+ char * sindex;
+ char * rindex;
+ char * gindex;
+ value tablesize;
+ char * table;
+ char * check;
+ value error_function;
+ char * names_const;
+ char * names_block;
+};
+
+struct parser_env { /* Mirrors parser_env in ../stdlib/parsing.ml */
+ value s_stack;
+ value v_stack;
+ value symb_start_stack;
+ value symb_end_stack;
+ value stacksize;
+ value stackbase;
+ value curr_char;
+ value lval;
+ value symb_start;
+ value symb_end;
+ value asp;
+ value rule_len;
+ value rule_number;
+ value sp;
+ value state;
+ value errflag;
+};
+
+#if defined(ARCH_BIG_ENDIAN) || SIZEOF_SHORT != 2
+#define Short(tbl,n) \
+ (*((unsigned char *)((tbl) + (n) * 2)) + \
+ (*((signed char *)((tbl) + (n) * 2 + 1)) << 8))
+#else
+#define Short(tbl,n) (((short *)(tbl))[n])
+#endif
+
+int caml_parser_trace = 0;
+
+/* Input codes */
+/* Mirrors parser_input in ../stdlib/parsing.ml */
+#define START 0
+#define TOKEN_READ 1
+#define STACKS_GROWN_1 2
+#define STACKS_GROWN_2 3
+#define SEMANTIC_ACTION_COMPUTED 4
+#define ERROR_DETECTED 5
+
+/* Output codes */
+/* Mirrors parser_output in ../stdlib/parsing.ml */
+#define READ_TOKEN Val_int(0)
+#define RAISE_PARSE_ERROR Val_int(1)
+#define GROW_STACKS_1 Val_int(2)
+#define GROW_STACKS_2 Val_int(3)
+#define COMPUTE_SEMANTIC_ACTION Val_int(4)
+#define CALL_ERROR_FUNCTION Val_int(5)
+
+/* To preserve local variables when communicating with the ML code */
+
+#define SAVE \
+ env->sp = Val_int(sp), \
+ env->state = Val_int(state), \
+ env->errflag = Val_int(errflag)
+
+#define RESTORE \
+ sp = Int_val(env->sp), \
+ state = Int_val(env->state), \
+ errflag = Int_val(env->errflag)
+
+/* Auxiliary for printing token just read */
+
+static char * token_name(char * names, int number)
+{
+ for (/*nothing*/; number > 0; number--) {
+ if (names[0] == 0) return "<unknown token>";
+ names += strlen(names) + 1;
+ }
+ return names;
+}
+
+static void print_token(struct parser_tables *tables, int state, value tok)
+{
+ value v;
+
+ if (Is_long(tok)) {
+ fprintf(stderr, "State %d: read token %s\n",
+ state, token_name(tables->names_const, Int_val(tok)));
+ } else {
+ fprintf(stderr, "State %d: read token %s(",
+ state, token_name(tables->names_block, Tag_val(tok)));
+ v = Field(tok, 0);
+ if (Is_long(v))
+ fprintf(stderr, "%" ARCH_INTNAT_PRINTF_FORMAT "d", Long_val(v));
+ else if (Tag_val(v) == String_tag)
+ fprintf(stderr, "%s", String_val(v));
+ else if (Tag_val(v) == Double_tag)
+ fprintf(stderr, "%g", Double_val(v));
+ else
+ fprintf(stderr, "_");
+ fprintf(stderr, ")\n");
+ }
+}
+
+/* The pushdown automata */
+
+CAMLprim value caml_parse_engine(struct parser_tables *tables,
+ struct parser_env *env, value cmd, value arg)
+{
+ int state;
+ mlsize_t sp, asp;
+ int errflag;
+ int n, n1, n2, m, state1;
+
+ switch(Int_val(cmd)) {
+
+ case START:
+ state = 0;
+ sp = Int_val(env->sp);
+ errflag = 0;
+
+ loop:
+ n = Short(tables->defred, state);
+ if (n != 0) goto reduce;
+ if (Int_val(env->curr_char) >= 0) goto testshift;
+ SAVE;
+ return READ_TOKEN;
+ /* The ML code calls the lexer and updates */
+ /* symb_start and symb_end */
+ case TOKEN_READ:
+ RESTORE;
+ if (Is_block(arg)) {
+ env->curr_char = Field(tables->transl_block, Tag_val(arg));
+ caml_modify(&env->lval, Field(arg, 0));
+ } else {
+ env->curr_char = Field(tables->transl_const, Int_val(arg));
+ caml_modify(&env->lval, Val_long(0));
+ }
+ if (caml_parser_trace) print_token(tables, state, arg);
+
+ testshift:
+ n1 = Short(tables->sindex, state);
+ n2 = n1 + Int_val(env->curr_char);
+ if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
+ Short(tables->check, n2) == Int_val(env->curr_char)) goto shift;
+ n1 = Short(tables->rindex, state);
+ n2 = n1 + Int_val(env->curr_char);
+ if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
+ Short(tables->check, n2) == Int_val(env->curr_char)) {
+ n = Short(tables->table, n2);
+ goto reduce;
+ }
+ if (errflag > 0) goto recover;
+ SAVE;
+ return CALL_ERROR_FUNCTION;
+ /* The ML code calls the error function */
+ case ERROR_DETECTED:
+ RESTORE;
+ recover:
+ if (errflag < 3) {
+ errflag = 3;
+ while (1) {
+ state1 = Int_val(Field(env->s_stack, sp));
+ n1 = Short(tables->sindex, state1);
+ n2 = n1 + ERRCODE;
+ if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
+ Short(tables->check, n2) == ERRCODE) {
+ if (caml_parser_trace)
+ fprintf(stderr, "Recovering in state %d\n", state1);
+ goto shift_recover;
+ } else {
+ if (caml_parser_trace){
+ fprintf(stderr, "Discarding state %d\n", state1);
+ }
+ if (sp <= Int_val(env->stackbase)) {
+ if (caml_parser_trace){
+ fprintf(stderr, "No more states to discard\n");
+ }
+ return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */
+ }
+ sp--;
+ }
+ }
+ } else {
+ if (Int_val(env->curr_char) == 0)
+ return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */
+ if (caml_parser_trace) fprintf(stderr, "Discarding last token read\n");
+ env->curr_char = Val_int(-1);
+ goto loop;
+ }
+
+ shift:
+ env->curr_char = Val_int(-1);
+ if (errflag > 0) errflag--;
+ shift_recover:
+ if (caml_parser_trace)
+ fprintf(stderr, "State %d: shift to state %d\n",
+ state, Short(tables->table, n2));
+ state = Short(tables->table, n2);
+ sp++;
+ if (sp < Long_val(env->stacksize)) goto push;
+ SAVE;
+ return GROW_STACKS_1;
+ /* The ML code resizes the stacks */
+ case STACKS_GROWN_1:
+ RESTORE;
+ push:
+ Field(env->s_stack, sp) = Val_int(state);
+ caml_modify(&Field(env->v_stack, sp), env->lval);
+ Store_field (env->symb_start_stack, sp, env->symb_start);
+ Store_field (env->symb_end_stack, sp, env->symb_end);
+ goto loop;
+
+ reduce:
+ if (caml_parser_trace)
+ fprintf(stderr, "State %d: reduce by rule %d\n", state, n);
+ m = Short(tables->len, n);
+ env->asp = Val_int(sp);
+ env->rule_number = Val_int(n);
+ env->rule_len = Val_int(m);
+ sp = sp - m + 1;
+ m = Short(tables->lhs, n);
+ state1 = Int_val(Field(env->s_stack, sp - 1));
+ n1 = Short(tables->gindex, m);
+ n2 = n1 + state1;
+ if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
+ Short(tables->check, n2) == state1) {
+ state = Short(tables->table, n2);
+ } else {
+ state = Short(tables->dgoto, m);
+ }
+ if (sp < Long_val(env->stacksize)) goto semantic_action;
+ SAVE;
+ return GROW_STACKS_2;
+ /* The ML code resizes the stacks */
+ case STACKS_GROWN_2:
+ RESTORE;
+ semantic_action:
+ SAVE;
+ return COMPUTE_SEMANTIC_ACTION;
+ /* The ML code calls the semantic action */
+ case SEMANTIC_ACTION_COMPUTED:
+ RESTORE;
+ Field(env->s_stack, sp) = Val_int(state);
+ caml_modify(&Field(env->v_stack, sp), arg);
+ asp = Int_val(env->asp);
+ Store_field (env->symb_end_stack, sp, Field(env->symb_end_stack, asp));
+ if (sp > asp) {
+ /* This is an epsilon production. Take symb_start equal to symb_end. */
+ Store_field (env->symb_start_stack, sp, Field(env->symb_end_stack, asp));
+ }
+ goto loop;
+
+ default: /* Should not happen */
+ CAMLassert(0);
+ return RAISE_PARSE_ERROR; /* Keeps gcc -Wall happy */
+ }
+
+}
+
+/* Control printing of debugging info */
+
+CAMLprim value caml_set_parser_trace(value flag)
+{
+ value oldflag = Val_bool(caml_parser_trace);
+ caml_parser_trace = Bool_val(flag);
+ return oldflag;
+}
diff --git a/test/monniaux/ocaml/byterun/prims.c b/test/monniaux/ocaml/byterun/prims.c
new file mode 100644
index 00000000..15ebf593
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/prims.c
@@ -0,0 +1,1153 @@
+#define CAML_INTERNALS
+#include "caml/mlvalues.h"
+#include "caml/prims.h"
+extern value caml_abs_float();
+extern value caml_acos_float();
+extern value caml_add_debug_info();
+extern value caml_add_float();
+extern value caml_alloc_dummy();
+extern value caml_alloc_dummy_float();
+extern value caml_alloc_dummy_function();
+extern value caml_array_append();
+extern value caml_array_blit();
+extern value caml_array_concat();
+extern value caml_array_get();
+extern value caml_array_get_addr();
+extern value caml_array_get_float();
+extern value caml_array_set();
+extern value caml_array_set_addr();
+extern value caml_array_set_float();
+extern value caml_array_sub();
+extern value caml_array_unsafe_get();
+extern value caml_array_unsafe_get_float();
+extern value caml_array_unsafe_set();
+extern value caml_array_unsafe_set_addr();
+extern value caml_array_unsafe_set_float();
+extern value caml_asin_float();
+extern value caml_atan2_float();
+extern value caml_atan_float();
+extern value caml_ba_blit();
+extern value caml_ba_change_layout();
+extern value caml_ba_create();
+extern value caml_ba_dim();
+extern value caml_ba_dim_1();
+extern value caml_ba_dim_2();
+extern value caml_ba_dim_3();
+extern value caml_ba_fill();
+extern value caml_ba_get_1();
+extern value caml_ba_get_2();
+extern value caml_ba_get_3();
+extern value caml_ba_get_generic();
+extern value caml_ba_kind();
+extern value caml_ba_layout();
+extern value caml_ba_num_dims();
+extern value caml_ba_reshape();
+extern value caml_ba_set_1();
+extern value caml_ba_set_2();
+extern value caml_ba_set_3();
+extern value caml_ba_set_generic();
+extern value caml_ba_slice();
+extern value caml_ba_sub();
+extern value caml_ba_uint8_get16();
+extern value caml_ba_uint8_get32();
+extern value caml_ba_uint8_get64();
+extern value caml_ba_uint8_set16();
+extern value caml_ba_uint8_set32();
+extern value caml_ba_uint8_set64();
+extern value caml_backtrace_status();
+extern value caml_blit_bytes();
+extern value caml_blit_string();
+extern value caml_bswap16();
+extern value caml_bytes_compare();
+extern value caml_bytes_equal();
+extern value caml_bytes_get();
+extern value caml_bytes_get16();
+extern value caml_bytes_get32();
+extern value caml_bytes_get64();
+extern value caml_bytes_greaterequal();
+extern value caml_bytes_greaterthan();
+extern value caml_bytes_lessequal();
+extern value caml_bytes_lessthan();
+extern value caml_bytes_notequal();
+extern value caml_bytes_of_string();
+extern value caml_bytes_set();
+extern value caml_bytes_set16();
+extern value caml_bytes_set32();
+extern value caml_bytes_set64();
+extern value caml_ceil_float();
+extern value caml_channel_descriptor();
+extern value caml_classify_float();
+extern value caml_compare();
+extern value caml_convert_raw_backtrace();
+extern value caml_convert_raw_backtrace_slot();
+extern value caml_copysign_float();
+extern value caml_cos_float();
+extern value caml_cosh_float();
+extern value caml_create_bytes();
+extern value caml_create_string();
+extern value caml_div_float();
+extern value caml_dynlink_add_primitive();
+extern value caml_dynlink_close_lib();
+extern value caml_dynlink_get_current_libs();
+extern value caml_dynlink_lookup_symbol();
+extern value caml_dynlink_open_lib();
+extern value caml_ensure_stack_capacity();
+extern value caml_ephe_blit_data();
+extern value caml_ephe_blit_key();
+extern value caml_ephe_check_data();
+extern value caml_ephe_check_key();
+extern value caml_ephe_create();
+extern value caml_ephe_get_data();
+extern value caml_ephe_get_data_copy();
+extern value caml_ephe_get_key();
+extern value caml_ephe_get_key_copy();
+extern value caml_ephe_set_data();
+extern value caml_ephe_set_key();
+extern value caml_ephe_unset_data();
+extern value caml_ephe_unset_key();
+extern value caml_eq_float();
+extern value caml_equal();
+extern value caml_exp_float();
+extern value caml_expm1_float();
+extern value caml_fill_bytes();
+extern value caml_fill_string();
+extern value caml_final_register();
+extern value caml_final_register_called_without_value();
+extern value caml_final_release();
+extern value caml_float_compare();
+extern value caml_float_of_int();
+extern value caml_float_of_string();
+extern value caml_floatarray_create();
+extern value caml_floatarray_get();
+extern value caml_floatarray_set();
+extern value caml_floatarray_unsafe_get();
+extern value caml_floatarray_unsafe_set();
+extern value caml_floor_float();
+extern value caml_fmod_float();
+extern value caml_format_float();
+extern value caml_format_int();
+extern value caml_fresh_oo_id();
+extern value caml_frexp_float();
+extern value caml_gc_compaction();
+extern value caml_gc_counters();
+extern value caml_gc_full_major();
+extern value caml_gc_get();
+extern value caml_gc_huge_fallback_count();
+extern value caml_gc_major();
+extern value caml_gc_major_slice();
+extern value caml_gc_minor();
+extern value caml_gc_minor_words();
+extern value caml_gc_quick_stat();
+extern value caml_gc_set();
+extern value caml_gc_stat();
+extern value caml_ge_float();
+extern value caml_get_current_callstack();
+extern value caml_get_current_environment();
+extern value caml_get_exception_backtrace();
+extern value caml_get_exception_raw_backtrace();
+extern value caml_get_global_data();
+extern value caml_get_major_bucket();
+extern value caml_get_major_credit();
+extern value caml_get_minor_free();
+extern value caml_get_public_method();
+extern value caml_get_section_table();
+extern value caml_greaterequal();
+extern value caml_greaterthan();
+extern value caml_gt_float();
+extern value caml_hash();
+extern value caml_hash_univ_param();
+extern value caml_hexstring_of_float();
+extern value caml_hypot_float();
+extern value caml_input_value();
+extern value caml_input_value_from_bytes();
+extern value caml_input_value_from_string();
+extern value caml_input_value_to_outside_heap();
+extern value caml_install_signal_handler();
+extern value caml_int32_add();
+extern value caml_int32_and();
+extern value caml_int32_bits_of_float();
+extern value caml_int32_bswap();
+extern value caml_int32_compare();
+extern value caml_int32_div();
+extern value caml_int32_float_of_bits();
+extern value caml_int32_format();
+extern value caml_int32_mod();
+extern value caml_int32_mul();
+extern value caml_int32_neg();
+extern value caml_int32_of_float();
+extern value caml_int32_of_int();
+extern value caml_int32_of_string();
+extern value caml_int32_or();
+extern value caml_int32_shift_left();
+extern value caml_int32_shift_right();
+extern value caml_int32_shift_right_unsigned();
+extern value caml_int32_sub();
+extern value caml_int32_to_float();
+extern value caml_int32_to_int();
+extern value caml_int32_xor();
+extern value caml_int64_add();
+extern value caml_int64_and();
+extern value caml_int64_bits_of_float();
+extern value caml_int64_bswap();
+extern value caml_int64_compare();
+extern value caml_int64_div();
+extern value caml_int64_float_of_bits();
+extern value caml_int64_format();
+extern value caml_int64_mod();
+extern value caml_int64_mul();
+extern value caml_int64_neg();
+extern value caml_int64_of_float();
+extern value caml_int64_of_int();
+extern value caml_int64_of_int32();
+extern value caml_int64_of_nativeint();
+extern value caml_int64_of_string();
+extern value caml_int64_or();
+extern value caml_int64_shift_left();
+extern value caml_int64_shift_right();
+extern value caml_int64_shift_right_unsigned();
+extern value caml_int64_sub();
+extern value caml_int64_to_float();
+extern value caml_int64_to_int();
+extern value caml_int64_to_int32();
+extern value caml_int64_to_nativeint();
+extern value caml_int64_xor();
+extern value caml_int_as_pointer();
+extern value caml_int_compare();
+extern value caml_int_of_float();
+extern value caml_int_of_string();
+extern value caml_invoke_traced_function();
+extern value caml_lazy_follow_forward();
+extern value caml_lazy_make_forward();
+extern value caml_ldexp_float();
+extern value caml_le_float();
+extern value caml_lessequal();
+extern value caml_lessthan();
+extern value caml_lex_engine();
+extern value caml_log10_float();
+extern value caml_log1p_float();
+extern value caml_log_float();
+extern value caml_lt_float();
+extern value caml_make_array();
+extern value caml_make_float_vect();
+extern value caml_make_vect();
+extern value caml_marshal_data_size();
+extern value caml_md5_chan();
+extern value caml_md5_string();
+extern value caml_ml_bytes_length();
+extern value caml_ml_channel_size();
+extern value caml_ml_channel_size_64();
+extern value caml_ml_close_channel();
+extern value caml_ml_enable_runtime_warnings();
+extern value caml_ml_flush();
+extern value caml_ml_flush_partial();
+extern value caml_ml_input();
+extern value caml_ml_input_char();
+extern value caml_ml_input_int();
+extern value caml_ml_input_scan_line();
+extern value caml_ml_open_descriptor_in();
+extern value caml_ml_open_descriptor_out();
+extern value caml_ml_out_channels_list();
+extern value caml_ml_output();
+extern value caml_ml_output_bytes();
+extern value caml_ml_output_char();
+extern value caml_ml_output_int();
+extern value caml_ml_output_partial();
+extern value caml_ml_pos_in();
+extern value caml_ml_pos_in_64();
+extern value caml_ml_pos_out();
+extern value caml_ml_pos_out_64();
+extern value caml_ml_runtime_warnings_enabled();
+extern value caml_ml_seek_in();
+extern value caml_ml_seek_in_64();
+extern value caml_ml_seek_out();
+extern value caml_ml_seek_out_64();
+extern value caml_ml_set_binary_mode();
+extern value caml_ml_set_channel_name();
+extern value caml_ml_string_length();
+extern value caml_modf_float();
+extern value caml_mul_float();
+extern value caml_nativeint_add();
+extern value caml_nativeint_and();
+extern value caml_nativeint_bswap();
+extern value caml_nativeint_compare();
+extern value caml_nativeint_div();
+extern value caml_nativeint_format();
+extern value caml_nativeint_mod();
+extern value caml_nativeint_mul();
+extern value caml_nativeint_neg();
+extern value caml_nativeint_of_float();
+extern value caml_nativeint_of_int();
+extern value caml_nativeint_of_int32();
+extern value caml_nativeint_of_string();
+extern value caml_nativeint_or();
+extern value caml_nativeint_shift_left();
+extern value caml_nativeint_shift_right();
+extern value caml_nativeint_shift_right_unsigned();
+extern value caml_nativeint_sub();
+extern value caml_nativeint_to_float();
+extern value caml_nativeint_to_int();
+extern value caml_nativeint_to_int32();
+extern value caml_nativeint_xor();
+extern value caml_neg_float();
+extern value caml_neq_float();
+extern value caml_new_lex_engine();
+extern value caml_notequal();
+extern value caml_obj_add_offset();
+extern value caml_obj_block();
+extern value caml_obj_dup();
+extern value caml_obj_is_block();
+extern value caml_obj_reachable_words();
+extern value caml_obj_set_tag();
+extern value caml_obj_tag();
+extern value caml_obj_truncate();
+extern value caml_output_value();
+extern value caml_output_value_to_buffer();
+extern value caml_output_value_to_bytes();
+extern value caml_output_value_to_string();
+extern value caml_parse_engine();
+extern value caml_power_float();
+extern value caml_raw_backtrace_length();
+extern value caml_raw_backtrace_next_slot();
+extern value caml_raw_backtrace_slot();
+extern value caml_realloc_global();
+extern value caml_record_backtrace();
+extern value caml_register_channel_for_spacetime();
+extern value caml_register_code_fragment();
+extern value caml_register_named_value();
+extern value caml_reify_bytecode();
+extern value caml_remove_debug_info();
+extern value caml_reset_afl_instrumentation();
+extern value caml_restore_raw_backtrace();
+extern value caml_runtime_parameters();
+extern value caml_runtime_variant();
+extern value caml_set_oo_id();
+extern value caml_set_parser_trace();
+extern value caml_setup_afl();
+extern value caml_sin_float();
+extern value caml_sinh_float();
+extern value caml_spacetime_enabled();
+extern value caml_spacetime_only_works_for_native_code();
+extern value caml_sqrt_float();
+extern value caml_static_alloc();
+extern value caml_static_free();
+extern value caml_static_release_bytecode();
+extern value caml_static_resize();
+extern value caml_string_compare();
+extern value caml_string_equal();
+extern value caml_string_get();
+extern value caml_string_get16();
+extern value caml_string_get32();
+extern value caml_string_get64();
+extern value caml_string_greaterequal();
+extern value caml_string_greaterthan();
+extern value caml_string_lessequal();
+extern value caml_string_lessthan();
+extern value caml_string_notequal();
+extern value caml_string_of_bytes();
+extern value caml_string_set();
+extern value caml_sub_float();
+extern value caml_sys_chdir();
+extern value caml_sys_close();
+extern value caml_sys_const_backend_type();
+extern value caml_sys_const_big_endian();
+extern value caml_sys_const_int_size();
+extern value caml_sys_const_max_wosize();
+extern value caml_sys_const_ostype_cygwin();
+extern value caml_sys_const_ostype_unix();
+extern value caml_sys_const_ostype_win32();
+extern value caml_sys_const_word_size();
+extern value caml_sys_exit();
+extern value caml_sys_file_exists();
+extern value caml_sys_get_argv();
+extern value caml_sys_get_config();
+extern value caml_sys_getcwd();
+extern value caml_sys_getenv();
+extern value caml_sys_is_directory();
+extern value caml_sys_isatty();
+extern value caml_sys_open();
+extern value caml_sys_random_seed();
+extern value caml_sys_read_directory();
+extern value caml_sys_remove();
+extern value caml_sys_rename();
+extern value caml_sys_system_command();
+extern value caml_sys_time();
+extern value caml_sys_time_include_children();
+extern value caml_sys_unsafe_getenv();
+extern value caml_tan_float();
+extern value caml_tanh_float();
+extern value caml_terminfo_rows();
+extern value caml_update_dummy();
+extern value caml_weak_blit();
+extern value caml_weak_check();
+extern value caml_weak_create();
+extern value caml_weak_get();
+extern value caml_weak_get_copy();
+extern value caml_weak_set();
+c_primitive caml_builtin_cprim[] = {
+ caml_abs_float,
+ caml_acos_float,
+ caml_add_debug_info,
+ caml_add_float,
+ caml_alloc_dummy,
+ caml_alloc_dummy_float,
+ caml_alloc_dummy_function,
+ caml_array_append,
+ caml_array_blit,
+ caml_array_concat,
+ caml_array_get,
+ caml_array_get_addr,
+ caml_array_get_float,
+ caml_array_set,
+ caml_array_set_addr,
+ caml_array_set_float,
+ caml_array_sub,
+ caml_array_unsafe_get,
+ caml_array_unsafe_get_float,
+ caml_array_unsafe_set,
+ caml_array_unsafe_set_addr,
+ caml_array_unsafe_set_float,
+ caml_asin_float,
+ caml_atan2_float,
+ caml_atan_float,
+ caml_ba_blit,
+ caml_ba_change_layout,
+ caml_ba_create,
+ caml_ba_dim,
+ caml_ba_dim_1,
+ caml_ba_dim_2,
+ caml_ba_dim_3,
+ caml_ba_fill,
+ caml_ba_get_1,
+ caml_ba_get_2,
+ caml_ba_get_3,
+ caml_ba_get_generic,
+ caml_ba_kind,
+ caml_ba_layout,
+ caml_ba_num_dims,
+ caml_ba_reshape,
+ caml_ba_set_1,
+ caml_ba_set_2,
+ caml_ba_set_3,
+ caml_ba_set_generic,
+ caml_ba_slice,
+ caml_ba_sub,
+ caml_ba_uint8_get16,
+ caml_ba_uint8_get32,
+ caml_ba_uint8_get64,
+ caml_ba_uint8_set16,
+ caml_ba_uint8_set32,
+ caml_ba_uint8_set64,
+ caml_backtrace_status,
+ caml_blit_bytes,
+ caml_blit_string,
+ caml_bswap16,
+ caml_bytes_compare,
+ caml_bytes_equal,
+ caml_bytes_get,
+ caml_bytes_get16,
+ caml_bytes_get32,
+ caml_bytes_get64,
+ caml_bytes_greaterequal,
+ caml_bytes_greaterthan,
+ caml_bytes_lessequal,
+ caml_bytes_lessthan,
+ caml_bytes_notequal,
+ caml_bytes_of_string,
+ caml_bytes_set,
+ caml_bytes_set16,
+ caml_bytes_set32,
+ caml_bytes_set64,
+ caml_ceil_float,
+ caml_channel_descriptor,
+ caml_classify_float,
+ caml_compare,
+ caml_convert_raw_backtrace,
+ caml_convert_raw_backtrace_slot,
+ caml_copysign_float,
+ caml_cos_float,
+ caml_cosh_float,
+ caml_create_bytes,
+ caml_create_string,
+ caml_div_float,
+ caml_dynlink_add_primitive,
+ caml_dynlink_close_lib,
+ caml_dynlink_get_current_libs,
+ caml_dynlink_lookup_symbol,
+ caml_dynlink_open_lib,
+ caml_ensure_stack_capacity,
+ caml_ephe_blit_data,
+ caml_ephe_blit_key,
+ caml_ephe_check_data,
+ caml_ephe_check_key,
+ caml_ephe_create,
+ caml_ephe_get_data,
+ caml_ephe_get_data_copy,
+ caml_ephe_get_key,
+ caml_ephe_get_key_copy,
+ caml_ephe_set_data,
+ caml_ephe_set_key,
+ caml_ephe_unset_data,
+ caml_ephe_unset_key,
+ caml_eq_float,
+ caml_equal,
+ caml_exp_float,
+ caml_expm1_float,
+ caml_fill_bytes,
+ caml_fill_string,
+ caml_final_register,
+ caml_final_register_called_without_value,
+ caml_final_release,
+ caml_float_compare,
+ caml_float_of_int,
+ caml_float_of_string,
+ caml_floatarray_create,
+ caml_floatarray_get,
+ caml_floatarray_set,
+ caml_floatarray_unsafe_get,
+ caml_floatarray_unsafe_set,
+ caml_floor_float,
+ caml_fmod_float,
+ caml_format_float,
+ caml_format_int,
+ caml_fresh_oo_id,
+ caml_frexp_float,
+ caml_gc_compaction,
+ caml_gc_counters,
+ caml_gc_full_major,
+ caml_gc_get,
+ caml_gc_huge_fallback_count,
+ caml_gc_major,
+ caml_gc_major_slice,
+ caml_gc_minor,
+ caml_gc_minor_words,
+ caml_gc_quick_stat,
+ caml_gc_set,
+ caml_gc_stat,
+ caml_ge_float,
+ caml_get_current_callstack,
+ caml_get_current_environment,
+ caml_get_exception_backtrace,
+ caml_get_exception_raw_backtrace,
+ caml_get_global_data,
+ caml_get_major_bucket,
+ caml_get_major_credit,
+ caml_get_minor_free,
+ caml_get_public_method,
+ caml_get_section_table,
+ caml_greaterequal,
+ caml_greaterthan,
+ caml_gt_float,
+ caml_hash,
+ caml_hash_univ_param,
+ caml_hexstring_of_float,
+ caml_hypot_float,
+ caml_input_value,
+ caml_input_value_from_bytes,
+ caml_input_value_from_string,
+ caml_input_value_to_outside_heap,
+ caml_install_signal_handler,
+ caml_int32_add,
+ caml_int32_and,
+ caml_int32_bits_of_float,
+ caml_int32_bswap,
+ caml_int32_compare,
+ caml_int32_div,
+ caml_int32_float_of_bits,
+ caml_int32_format,
+ caml_int32_mod,
+ caml_int32_mul,
+ caml_int32_neg,
+ caml_int32_of_float,
+ caml_int32_of_int,
+ caml_int32_of_string,
+ caml_int32_or,
+ caml_int32_shift_left,
+ caml_int32_shift_right,
+ caml_int32_shift_right_unsigned,
+ caml_int32_sub,
+ caml_int32_to_float,
+ caml_int32_to_int,
+ caml_int32_xor,
+ caml_int64_add,
+ caml_int64_and,
+ caml_int64_bits_of_float,
+ caml_int64_bswap,
+ caml_int64_compare,
+ caml_int64_div,
+ caml_int64_float_of_bits,
+ caml_int64_format,
+ caml_int64_mod,
+ caml_int64_mul,
+ caml_int64_neg,
+ caml_int64_of_float,
+ caml_int64_of_int,
+ caml_int64_of_int32,
+ caml_int64_of_nativeint,
+ caml_int64_of_string,
+ caml_int64_or,
+ caml_int64_shift_left,
+ caml_int64_shift_right,
+ caml_int64_shift_right_unsigned,
+ caml_int64_sub,
+ caml_int64_to_float,
+ caml_int64_to_int,
+ caml_int64_to_int32,
+ caml_int64_to_nativeint,
+ caml_int64_xor,
+ caml_int_as_pointer,
+ caml_int_compare,
+ caml_int_of_float,
+ caml_int_of_string,
+ caml_invoke_traced_function,
+ caml_lazy_follow_forward,
+ caml_lazy_make_forward,
+ caml_ldexp_float,
+ caml_le_float,
+ caml_lessequal,
+ caml_lessthan,
+ caml_lex_engine,
+ caml_log10_float,
+ caml_log1p_float,
+ caml_log_float,
+ caml_lt_float,
+ caml_make_array,
+ caml_make_float_vect,
+ caml_make_vect,
+ caml_marshal_data_size,
+ caml_md5_chan,
+ caml_md5_string,
+ caml_ml_bytes_length,
+ caml_ml_channel_size,
+ caml_ml_channel_size_64,
+ caml_ml_close_channel,
+ caml_ml_enable_runtime_warnings,
+ caml_ml_flush,
+ caml_ml_flush_partial,
+ caml_ml_input,
+ caml_ml_input_char,
+ caml_ml_input_int,
+ caml_ml_input_scan_line,
+ caml_ml_open_descriptor_in,
+ caml_ml_open_descriptor_out,
+ caml_ml_out_channels_list,
+ caml_ml_output,
+ caml_ml_output_bytes,
+ caml_ml_output_char,
+ caml_ml_output_int,
+ caml_ml_output_partial,
+ caml_ml_pos_in,
+ caml_ml_pos_in_64,
+ caml_ml_pos_out,
+ caml_ml_pos_out_64,
+ caml_ml_runtime_warnings_enabled,
+ caml_ml_seek_in,
+ caml_ml_seek_in_64,
+ caml_ml_seek_out,
+ caml_ml_seek_out_64,
+ caml_ml_set_binary_mode,
+ caml_ml_set_channel_name,
+ caml_ml_string_length,
+ caml_modf_float,
+ caml_mul_float,
+ caml_nativeint_add,
+ caml_nativeint_and,
+ caml_nativeint_bswap,
+ caml_nativeint_compare,
+ caml_nativeint_div,
+ caml_nativeint_format,
+ caml_nativeint_mod,
+ caml_nativeint_mul,
+ caml_nativeint_neg,
+ caml_nativeint_of_float,
+ caml_nativeint_of_int,
+ caml_nativeint_of_int32,
+ caml_nativeint_of_string,
+ caml_nativeint_or,
+ caml_nativeint_shift_left,
+ caml_nativeint_shift_right,
+ caml_nativeint_shift_right_unsigned,
+ caml_nativeint_sub,
+ caml_nativeint_to_float,
+ caml_nativeint_to_int,
+ caml_nativeint_to_int32,
+ caml_nativeint_xor,
+ caml_neg_float,
+ caml_neq_float,
+ caml_new_lex_engine,
+ caml_notequal,
+ caml_obj_add_offset,
+ caml_obj_block,
+ caml_obj_dup,
+ caml_obj_is_block,
+ caml_obj_reachable_words,
+ caml_obj_set_tag,
+ caml_obj_tag,
+ caml_obj_truncate,
+ caml_output_value,
+ caml_output_value_to_buffer,
+ caml_output_value_to_bytes,
+ caml_output_value_to_string,
+ caml_parse_engine,
+ caml_power_float,
+ caml_raw_backtrace_length,
+ caml_raw_backtrace_next_slot,
+ caml_raw_backtrace_slot,
+ caml_realloc_global,
+ caml_record_backtrace,
+ caml_register_channel_for_spacetime,
+ caml_register_code_fragment,
+ caml_register_named_value,
+ caml_reify_bytecode,
+ caml_remove_debug_info,
+ caml_reset_afl_instrumentation,
+ caml_restore_raw_backtrace,
+ caml_runtime_parameters,
+ caml_runtime_variant,
+ caml_set_oo_id,
+ caml_set_parser_trace,
+ caml_setup_afl,
+ caml_sin_float,
+ caml_sinh_float,
+ caml_spacetime_enabled,
+ caml_spacetime_only_works_for_native_code,
+ caml_sqrt_float,
+ caml_static_alloc,
+ caml_static_free,
+ caml_static_release_bytecode,
+ caml_static_resize,
+ caml_string_compare,
+ caml_string_equal,
+ caml_string_get,
+ caml_string_get16,
+ caml_string_get32,
+ caml_string_get64,
+ caml_string_greaterequal,
+ caml_string_greaterthan,
+ caml_string_lessequal,
+ caml_string_lessthan,
+ caml_string_notequal,
+ caml_string_of_bytes,
+ caml_string_set,
+ caml_sub_float,
+ caml_sys_chdir,
+ caml_sys_close,
+ caml_sys_const_backend_type,
+ caml_sys_const_big_endian,
+ caml_sys_const_int_size,
+ caml_sys_const_max_wosize,
+ caml_sys_const_ostype_cygwin,
+ caml_sys_const_ostype_unix,
+ caml_sys_const_ostype_win32,
+ caml_sys_const_word_size,
+ caml_sys_exit,
+ caml_sys_file_exists,
+ caml_sys_get_argv,
+ caml_sys_get_config,
+ caml_sys_getcwd,
+ caml_sys_getenv,
+ caml_sys_is_directory,
+ caml_sys_isatty,
+ caml_sys_open,
+ caml_sys_random_seed,
+ caml_sys_read_directory,
+ caml_sys_remove,
+ caml_sys_rename,
+ caml_sys_system_command,
+ caml_sys_time,
+ caml_sys_time_include_children,
+ caml_sys_unsafe_getenv,
+ caml_tan_float,
+ caml_tanh_float,
+ caml_terminfo_rows,
+ caml_update_dummy,
+ caml_weak_blit,
+ caml_weak_check,
+ caml_weak_create,
+ caml_weak_get,
+ caml_weak_get_copy,
+ caml_weak_set,
+ 0 };
+char * caml_names_of_builtin_cprim[] = {
+ "caml_abs_float",
+ "caml_acos_float",
+ "caml_add_debug_info",
+ "caml_add_float",
+ "caml_alloc_dummy",
+ "caml_alloc_dummy_float",
+ "caml_alloc_dummy_function",
+ "caml_array_append",
+ "caml_array_blit",
+ "caml_array_concat",
+ "caml_array_get",
+ "caml_array_get_addr",
+ "caml_array_get_float",
+ "caml_array_set",
+ "caml_array_set_addr",
+ "caml_array_set_float",
+ "caml_array_sub",
+ "caml_array_unsafe_get",
+ "caml_array_unsafe_get_float",
+ "caml_array_unsafe_set",
+ "caml_array_unsafe_set_addr",
+ "caml_array_unsafe_set_float",
+ "caml_asin_float",
+ "caml_atan2_float",
+ "caml_atan_float",
+ "caml_ba_blit",
+ "caml_ba_change_layout",
+ "caml_ba_create",
+ "caml_ba_dim",
+ "caml_ba_dim_1",
+ "caml_ba_dim_2",
+ "caml_ba_dim_3",
+ "caml_ba_fill",
+ "caml_ba_get_1",
+ "caml_ba_get_2",
+ "caml_ba_get_3",
+ "caml_ba_get_generic",
+ "caml_ba_kind",
+ "caml_ba_layout",
+ "caml_ba_num_dims",
+ "caml_ba_reshape",
+ "caml_ba_set_1",
+ "caml_ba_set_2",
+ "caml_ba_set_3",
+ "caml_ba_set_generic",
+ "caml_ba_slice",
+ "caml_ba_sub",
+ "caml_ba_uint8_get16",
+ "caml_ba_uint8_get32",
+ "caml_ba_uint8_get64",
+ "caml_ba_uint8_set16",
+ "caml_ba_uint8_set32",
+ "caml_ba_uint8_set64",
+ "caml_backtrace_status",
+ "caml_blit_bytes",
+ "caml_blit_string",
+ "caml_bswap16",
+ "caml_bytes_compare",
+ "caml_bytes_equal",
+ "caml_bytes_get",
+ "caml_bytes_get16",
+ "caml_bytes_get32",
+ "caml_bytes_get64",
+ "caml_bytes_greaterequal",
+ "caml_bytes_greaterthan",
+ "caml_bytes_lessequal",
+ "caml_bytes_lessthan",
+ "caml_bytes_notequal",
+ "caml_bytes_of_string",
+ "caml_bytes_set",
+ "caml_bytes_set16",
+ "caml_bytes_set32",
+ "caml_bytes_set64",
+ "caml_ceil_float",
+ "caml_channel_descriptor",
+ "caml_classify_float",
+ "caml_compare",
+ "caml_convert_raw_backtrace",
+ "caml_convert_raw_backtrace_slot",
+ "caml_copysign_float",
+ "caml_cos_float",
+ "caml_cosh_float",
+ "caml_create_bytes",
+ "caml_create_string",
+ "caml_div_float",
+ "caml_dynlink_add_primitive",
+ "caml_dynlink_close_lib",
+ "caml_dynlink_get_current_libs",
+ "caml_dynlink_lookup_symbol",
+ "caml_dynlink_open_lib",
+ "caml_ensure_stack_capacity",
+ "caml_ephe_blit_data",
+ "caml_ephe_blit_key",
+ "caml_ephe_check_data",
+ "caml_ephe_check_key",
+ "caml_ephe_create",
+ "caml_ephe_get_data",
+ "caml_ephe_get_data_copy",
+ "caml_ephe_get_key",
+ "caml_ephe_get_key_copy",
+ "caml_ephe_set_data",
+ "caml_ephe_set_key",
+ "caml_ephe_unset_data",
+ "caml_ephe_unset_key",
+ "caml_eq_float",
+ "caml_equal",
+ "caml_exp_float",
+ "caml_expm1_float",
+ "caml_fill_bytes",
+ "caml_fill_string",
+ "caml_final_register",
+ "caml_final_register_called_without_value",
+ "caml_final_release",
+ "caml_float_compare",
+ "caml_float_of_int",
+ "caml_float_of_string",
+ "caml_floatarray_create",
+ "caml_floatarray_get",
+ "caml_floatarray_set",
+ "caml_floatarray_unsafe_get",
+ "caml_floatarray_unsafe_set",
+ "caml_floor_float",
+ "caml_fmod_float",
+ "caml_format_float",
+ "caml_format_int",
+ "caml_fresh_oo_id",
+ "caml_frexp_float",
+ "caml_gc_compaction",
+ "caml_gc_counters",
+ "caml_gc_full_major",
+ "caml_gc_get",
+ "caml_gc_huge_fallback_count",
+ "caml_gc_major",
+ "caml_gc_major_slice",
+ "caml_gc_minor",
+ "caml_gc_minor_words",
+ "caml_gc_quick_stat",
+ "caml_gc_set",
+ "caml_gc_stat",
+ "caml_ge_float",
+ "caml_get_current_callstack",
+ "caml_get_current_environment",
+ "caml_get_exception_backtrace",
+ "caml_get_exception_raw_backtrace",
+ "caml_get_global_data",
+ "caml_get_major_bucket",
+ "caml_get_major_credit",
+ "caml_get_minor_free",
+ "caml_get_public_method",
+ "caml_get_section_table",
+ "caml_greaterequal",
+ "caml_greaterthan",
+ "caml_gt_float",
+ "caml_hash",
+ "caml_hash_univ_param",
+ "caml_hexstring_of_float",
+ "caml_hypot_float",
+ "caml_input_value",
+ "caml_input_value_from_bytes",
+ "caml_input_value_from_string",
+ "caml_input_value_to_outside_heap",
+ "caml_install_signal_handler",
+ "caml_int32_add",
+ "caml_int32_and",
+ "caml_int32_bits_of_float",
+ "caml_int32_bswap",
+ "caml_int32_compare",
+ "caml_int32_div",
+ "caml_int32_float_of_bits",
+ "caml_int32_format",
+ "caml_int32_mod",
+ "caml_int32_mul",
+ "caml_int32_neg",
+ "caml_int32_of_float",
+ "caml_int32_of_int",
+ "caml_int32_of_string",
+ "caml_int32_or",
+ "caml_int32_shift_left",
+ "caml_int32_shift_right",
+ "caml_int32_shift_right_unsigned",
+ "caml_int32_sub",
+ "caml_int32_to_float",
+ "caml_int32_to_int",
+ "caml_int32_xor",
+ "caml_int64_add",
+ "caml_int64_and",
+ "caml_int64_bits_of_float",
+ "caml_int64_bswap",
+ "caml_int64_compare",
+ "caml_int64_div",
+ "caml_int64_float_of_bits",
+ "caml_int64_format",
+ "caml_int64_mod",
+ "caml_int64_mul",
+ "caml_int64_neg",
+ "caml_int64_of_float",
+ "caml_int64_of_int",
+ "caml_int64_of_int32",
+ "caml_int64_of_nativeint",
+ "caml_int64_of_string",
+ "caml_int64_or",
+ "caml_int64_shift_left",
+ "caml_int64_shift_right",
+ "caml_int64_shift_right_unsigned",
+ "caml_int64_sub",
+ "caml_int64_to_float",
+ "caml_int64_to_int",
+ "caml_int64_to_int32",
+ "caml_int64_to_nativeint",
+ "caml_int64_xor",
+ "caml_int_as_pointer",
+ "caml_int_compare",
+ "caml_int_of_float",
+ "caml_int_of_string",
+ "caml_invoke_traced_function",
+ "caml_lazy_follow_forward",
+ "caml_lazy_make_forward",
+ "caml_ldexp_float",
+ "caml_le_float",
+ "caml_lessequal",
+ "caml_lessthan",
+ "caml_lex_engine",
+ "caml_log10_float",
+ "caml_log1p_float",
+ "caml_log_float",
+ "caml_lt_float",
+ "caml_make_array",
+ "caml_make_float_vect",
+ "caml_make_vect",
+ "caml_marshal_data_size",
+ "caml_md5_chan",
+ "caml_md5_string",
+ "caml_ml_bytes_length",
+ "caml_ml_channel_size",
+ "caml_ml_channel_size_64",
+ "caml_ml_close_channel",
+ "caml_ml_enable_runtime_warnings",
+ "caml_ml_flush",
+ "caml_ml_flush_partial",
+ "caml_ml_input",
+ "caml_ml_input_char",
+ "caml_ml_input_int",
+ "caml_ml_input_scan_line",
+ "caml_ml_open_descriptor_in",
+ "caml_ml_open_descriptor_out",
+ "caml_ml_out_channels_list",
+ "caml_ml_output",
+ "caml_ml_output_bytes",
+ "caml_ml_output_char",
+ "caml_ml_output_int",
+ "caml_ml_output_partial",
+ "caml_ml_pos_in",
+ "caml_ml_pos_in_64",
+ "caml_ml_pos_out",
+ "caml_ml_pos_out_64",
+ "caml_ml_runtime_warnings_enabled",
+ "caml_ml_seek_in",
+ "caml_ml_seek_in_64",
+ "caml_ml_seek_out",
+ "caml_ml_seek_out_64",
+ "caml_ml_set_binary_mode",
+ "caml_ml_set_channel_name",
+ "caml_ml_string_length",
+ "caml_modf_float",
+ "caml_mul_float",
+ "caml_nativeint_add",
+ "caml_nativeint_and",
+ "caml_nativeint_bswap",
+ "caml_nativeint_compare",
+ "caml_nativeint_div",
+ "caml_nativeint_format",
+ "caml_nativeint_mod",
+ "caml_nativeint_mul",
+ "caml_nativeint_neg",
+ "caml_nativeint_of_float",
+ "caml_nativeint_of_int",
+ "caml_nativeint_of_int32",
+ "caml_nativeint_of_string",
+ "caml_nativeint_or",
+ "caml_nativeint_shift_left",
+ "caml_nativeint_shift_right",
+ "caml_nativeint_shift_right_unsigned",
+ "caml_nativeint_sub",
+ "caml_nativeint_to_float",
+ "caml_nativeint_to_int",
+ "caml_nativeint_to_int32",
+ "caml_nativeint_xor",
+ "caml_neg_float",
+ "caml_neq_float",
+ "caml_new_lex_engine",
+ "caml_notequal",
+ "caml_obj_add_offset",
+ "caml_obj_block",
+ "caml_obj_dup",
+ "caml_obj_is_block",
+ "caml_obj_reachable_words",
+ "caml_obj_set_tag",
+ "caml_obj_tag",
+ "caml_obj_truncate",
+ "caml_output_value",
+ "caml_output_value_to_buffer",
+ "caml_output_value_to_bytes",
+ "caml_output_value_to_string",
+ "caml_parse_engine",
+ "caml_power_float",
+ "caml_raw_backtrace_length",
+ "caml_raw_backtrace_next_slot",
+ "caml_raw_backtrace_slot",
+ "caml_realloc_global",
+ "caml_record_backtrace",
+ "caml_register_channel_for_spacetime",
+ "caml_register_code_fragment",
+ "caml_register_named_value",
+ "caml_reify_bytecode",
+ "caml_remove_debug_info",
+ "caml_reset_afl_instrumentation",
+ "caml_restore_raw_backtrace",
+ "caml_runtime_parameters",
+ "caml_runtime_variant",
+ "caml_set_oo_id",
+ "caml_set_parser_trace",
+ "caml_setup_afl",
+ "caml_sin_float",
+ "caml_sinh_float",
+ "caml_spacetime_enabled",
+ "caml_spacetime_only_works_for_native_code",
+ "caml_sqrt_float",
+ "caml_static_alloc",
+ "caml_static_free",
+ "caml_static_release_bytecode",
+ "caml_static_resize",
+ "caml_string_compare",
+ "caml_string_equal",
+ "caml_string_get",
+ "caml_string_get16",
+ "caml_string_get32",
+ "caml_string_get64",
+ "caml_string_greaterequal",
+ "caml_string_greaterthan",
+ "caml_string_lessequal",
+ "caml_string_lessthan",
+ "caml_string_notequal",
+ "caml_string_of_bytes",
+ "caml_string_set",
+ "caml_sub_float",
+ "caml_sys_chdir",
+ "caml_sys_close",
+ "caml_sys_const_backend_type",
+ "caml_sys_const_big_endian",
+ "caml_sys_const_int_size",
+ "caml_sys_const_max_wosize",
+ "caml_sys_const_ostype_cygwin",
+ "caml_sys_const_ostype_unix",
+ "caml_sys_const_ostype_win32",
+ "caml_sys_const_word_size",
+ "caml_sys_exit",
+ "caml_sys_file_exists",
+ "caml_sys_get_argv",
+ "caml_sys_get_config",
+ "caml_sys_getcwd",
+ "caml_sys_getenv",
+ "caml_sys_is_directory",
+ "caml_sys_isatty",
+ "caml_sys_open",
+ "caml_sys_random_seed",
+ "caml_sys_read_directory",
+ "caml_sys_remove",
+ "caml_sys_rename",
+ "caml_sys_system_command",
+ "caml_sys_time",
+ "caml_sys_time_include_children",
+ "caml_sys_unsafe_getenv",
+ "caml_tan_float",
+ "caml_tanh_float",
+ "caml_terminfo_rows",
+ "caml_update_dummy",
+ "caml_weak_blit",
+ "caml_weak_check",
+ "caml_weak_create",
+ "caml_weak_get",
+ "caml_weak_get_copy",
+ "caml_weak_set",
+ 0 };
diff --git a/test/monniaux/ocaml/byterun/printexc.c b/test/monniaux/ocaml/byterun/printexc.c
new file mode 100644
index 00000000..735c2994
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/printexc.c
@@ -0,0 +1,155 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Print an uncaught exception and abort */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "caml/backtrace.h"
+#include "caml/callback.h"
+#include "caml/debugger.h"
+#include "caml/fail.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/printexc.h"
+#include "caml/memory.h"
+
+struct stringbuf {
+ char * ptr;
+ char * end;
+ char data[256];
+};
+
+static void add_char(struct stringbuf *buf, char c)
+{
+ if (buf->ptr < buf->end) *(buf->ptr++) = c;
+}
+
+static void add_string(struct stringbuf *buf, const char *s)
+{
+ int len = strlen(s);
+ if (buf->ptr + len > buf->end) len = buf->end - buf->ptr;
+ if (len > 0) memmove(buf->ptr, s, len);
+ buf->ptr += len;
+}
+
+CAMLexport char * caml_format_exception(value exn)
+{
+ mlsize_t start, i;
+ value bucket, v;
+ struct stringbuf buf;
+ char intbuf[64];
+ char * res;
+
+ buf.ptr = buf.data;
+ buf.end = buf.data + sizeof(buf.data) - 1;
+ if (Tag_val(exn) == 0) {
+ add_string(&buf, String_val(Field(Field(exn, 0), 0)));
+ /* Check for exceptions in the style of Match_failure and Assert_failure */
+ if (Wosize_val(exn) == 2 &&
+ Is_block(Field(exn, 1)) &&
+ Tag_val(Field(exn, 1)) == 0 &&
+ caml_is_special_exception(Field(exn, 0))) {
+ bucket = Field(exn, 1);
+ start = 0;
+ } else {
+ bucket = exn;
+ start = 1;
+ }
+ add_char(&buf, '(');
+ for (i = start; i < Wosize_val(bucket); i++) {
+ if (i > start) add_string(&buf, ", ");
+ v = Field(bucket, i);
+ if (Is_long(v)) {
+ snprintf(intbuf, sizeof(intbuf),
+ "%" ARCH_INTNAT_PRINTF_FORMAT "d", Long_val(v));
+ add_string(&buf, intbuf);
+ } else if (Tag_val(v) == String_tag) {
+ add_char(&buf, '"');
+ add_string(&buf, String_val(v));
+ add_char(&buf, '"');
+ } else {
+ add_char(&buf, '_');
+ }
+ }
+ add_char(&buf, ')');
+ } else
+ add_string(&buf, String_val(Field(exn, 0)));
+
+ *buf.ptr = 0; /* Terminate string */
+ i = buf.ptr - buf.data + 1;
+ res = caml_stat_alloc_noexc(i);
+ if (res == NULL) return NULL;
+ memmove(res, buf.data, i);
+ return res;
+}
+
+
+#ifdef NATIVE_CODE
+# define DEBUGGER_IN_USE 0
+#else
+# define DEBUGGER_IN_USE caml_debugger_in_use
+#endif
+
+/* Default C implementation in case the OCaml one is not registered. */
+static void default_fatal_uncaught_exception(value exn)
+{
+ char * msg;
+ value * at_exit;
+ int saved_backtrace_active, saved_backtrace_pos;
+
+ /* Build a string representation of the exception */
+ msg = caml_format_exception(exn);
+ /* Perform "at_exit" processing, ignoring all exceptions that may
+ be triggered by this */
+ saved_backtrace_active = caml_backtrace_active;
+ saved_backtrace_pos = caml_backtrace_pos;
+ caml_backtrace_active = 0;
+ at_exit = caml_named_value("Pervasives.do_at_exit");
+ if (at_exit != NULL) caml_callback_exn(*at_exit, Val_unit);
+ caml_backtrace_active = saved_backtrace_active;
+ caml_backtrace_pos = saved_backtrace_pos;
+ /* Display the uncaught exception */
+ fprintf(stderr, "Fatal error: exception %s\n", msg);
+ caml_stat_free(msg);
+ /* Display the backtrace if available */
+ if (caml_backtrace_active && !DEBUGGER_IN_USE)
+ caml_print_exception_backtrace();
+}
+
+int caml_abort_on_uncaught_exn = 0; /* see afl.c */
+
+void caml_fatal_uncaught_exception(value exn)
+{
+ value *handle_uncaught_exception;
+
+ handle_uncaught_exception =
+ caml_named_value("Printexc.handle_uncaught_exception");
+ if (handle_uncaught_exception != NULL)
+ /* [Printexc.handle_uncaught_exception] does not raise exception. */
+ caml_callback2(*handle_uncaught_exception, exn, Val_bool(DEBUGGER_IN_USE));
+ else
+ default_fatal_uncaught_exception(exn);
+ /* Terminate the process */
+ if (caml_abort_on_uncaught_exn) {
+ abort();
+ } else {
+ CAML_SYS_EXIT(2);
+ exit(2); /* Second exit needed for the Noreturn flag */
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/roots.c b/test/monniaux/ocaml/byterun/roots.c
new file mode 100644
index 00000000..1445495a
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/roots.c
@@ -0,0 +1,120 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* To walk the memory roots for garbage collection */
+
+#include "caml/finalise.h"
+#include "caml/globroots.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/stacks.h"
+
+CAMLexport struct caml__roots_block *caml_local_roots = NULL;
+
+CAMLexport void (*caml_scan_roots_hook) (scanning_action f) = NULL;
+
+/* FIXME should rename to [caml_oldify_minor_roots] and synchronise with
+ asmrun/roots.c */
+/* Call [caml_oldify_one] on (at least) all the roots that point to the minor
+ heap. */
+void caml_oldify_local_roots (void)
+{
+ register value * sp;
+ struct caml__roots_block *lr;
+ intnat i, j;
+
+ /* The stack */
+ for (sp = caml_extern_sp; sp < caml_stack_high; sp++) {
+ caml_oldify_one (*sp, sp);
+ }
+ /* Local C roots */ /* FIXME do the old-frame trick ? */
+ for (lr = caml_local_roots; lr != NULL; lr = lr->next) {
+ for (i = 0; i < lr->ntables; i++){
+ for (j = 0; j < lr->nitems; j++){
+ sp = &(lr->tables[i][j]);
+ caml_oldify_one (*sp, sp);
+ }
+ }
+ }
+ /* Global C roots */
+ caml_scan_global_young_roots(&caml_oldify_one);
+ /* Finalised values */
+ caml_final_oldify_young_roots ();
+ /* Hook */
+ if (caml_scan_roots_hook != NULL) (*caml_scan_roots_hook)(&caml_oldify_one);
+}
+
+/* Call [caml_darken] on all roots */
+
+void caml_darken_all_roots_start (void)
+{
+ caml_do_roots (caml_darken, 1);
+}
+
+uintnat caml_incremental_roots_count = 1;
+
+intnat caml_darken_all_roots_slice (intnat work)
+{
+ return work;
+}
+
+/* Note, in byte-code there is only one global root, so [do_globals] is
+ ignored and [caml_darken_all_roots_slice] does nothing. */
+void caml_do_roots (scanning_action f, int do_globals)
+{
+ CAML_INSTR_SETUP (tmr, "major_roots");
+ /* Global variables */
+ f(caml_global_data, &caml_global_data);
+ CAML_INSTR_TIME (tmr, "major_roots/global");
+ /* The stack and the local C roots */
+ caml_do_local_roots(f, caml_extern_sp, caml_stack_high, caml_local_roots);
+ CAML_INSTR_TIME (tmr, "major_roots/local");
+ /* Global C roots */
+ caml_scan_global_roots(f);
+ CAML_INSTR_TIME (tmr, "major_roots/C");
+ /* Finalised values */
+ caml_final_do_roots (f);
+ CAML_INSTR_TIME (tmr, "major_roots/finalised");
+ /* Hook */
+ if (caml_scan_roots_hook != NULL) (*caml_scan_roots_hook)(f);
+ CAML_INSTR_TIME (tmr, "major_roots/hook");
+}
+
+CAMLexport void caml_do_local_roots (scanning_action f, value *stack_low,
+ value *stack_high,
+ struct caml__roots_block *local_roots)
+{
+ register value * sp;
+ struct caml__roots_block *lr;
+ int i, j;
+
+ for (sp = stack_low; sp < stack_high; sp++) {
+ f (*sp, sp);
+ }
+ for (lr = local_roots; lr != NULL; lr = lr->next) {
+ for (i = 0; i < lr->ntables; i++){
+ for (j = 0; j < lr->nitems; j++){
+ sp = &(lr->tables[i][j]);
+ f (*sp, sp);
+ }
+ }
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/signals.c b/test/monniaux/ocaml/byterun/signals.c
new file mode 100644
index 00000000..15addf1b
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/signals.c
@@ -0,0 +1,398 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Signal handling, code common to the bytecode and native systems */
+
+#include <signal.h>
+#include <errno.h>
+#include "caml/alloc.h"
+#include "caml/callback.h"
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/roots.h"
+#include "caml/signals.h"
+#include "caml/signals_machdep.h"
+#include "caml/sys.h"
+
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+#include "caml/spacetime.h"
+#endif
+
+#ifndef NSIG
+#define NSIG 64
+#endif
+
+/* The set of pending signals (received but not yet processed) */
+
+CAMLexport intnat volatile caml_signals_are_pending = 0;
+CAMLexport intnat volatile caml_pending_signals[NSIG];
+
+/* Execute all pending signals */
+
+void caml_process_pending_signals(void)
+{
+ int i;
+
+ if (caml_signals_are_pending) {
+ caml_signals_are_pending = 0;
+ for (i = 0; i < NSIG; i++) {
+ if (caml_pending_signals[i]) {
+ caml_pending_signals[i] = 0;
+ caml_execute_signal(i, 0);
+ }
+ }
+ }
+}
+
+/* Record the delivery of a signal, and arrange for it to be processed
+ as soon as possible:
+ - in bytecode: via caml_something_to_do, processed in caml_process_event
+ - in native-code: by playing with the allocation limit, processed
+ in caml_garbage_collection
+*/
+
+void caml_record_signal(int signal_number)
+{
+ caml_pending_signals[signal_number] = 1;
+ caml_signals_are_pending = 1;
+#ifndef NATIVE_CODE
+ caml_something_to_do = 1;
+#else
+ caml_young_limit = caml_young_alloc_end;
+#endif
+}
+
+/* Management of blocking sections. */
+
+static intnat volatile caml_async_signal_mode = 0;
+
+static void caml_enter_blocking_section_default(void)
+{
+ CAMLassert (caml_async_signal_mode == 0);
+ caml_async_signal_mode = 1;
+}
+
+static void caml_leave_blocking_section_default(void)
+{
+ CAMLassert (caml_async_signal_mode == 1);
+ caml_async_signal_mode = 0;
+}
+
+static int caml_try_leave_blocking_section_default(void)
+{
+ intnat res;
+ Read_and_clear(res, caml_async_signal_mode);
+ return res;
+}
+
+CAMLexport void (*caml_enter_blocking_section_hook)(void) =
+ caml_enter_blocking_section_default;
+CAMLexport void (*caml_leave_blocking_section_hook)(void) =
+ caml_leave_blocking_section_default;
+CAMLexport int (*caml_try_leave_blocking_section_hook)(void) =
+ caml_try_leave_blocking_section_default;
+
+CAMLexport void caml_enter_blocking_section(void)
+{
+ while (1){
+ /* Process all pending signals now */
+ caml_process_pending_signals();
+ caml_enter_blocking_section_hook ();
+ /* Check again for pending signals.
+ If none, done; otherwise, try again */
+ if (! caml_signals_are_pending) break;
+ caml_leave_blocking_section_hook ();
+ }
+}
+
+CAMLexport void caml_leave_blocking_section(void)
+{
+ int saved_errno;
+ /* Save the value of errno (PR#5982). */
+ saved_errno = errno;
+ caml_leave_blocking_section_hook ();
+ caml_process_pending_signals();
+ errno = saved_errno;
+}
+
+/* Execute a signal handler immediately */
+
+static value caml_signal_handlers = 0;
+
+void caml_execute_signal(int signal_number, int in_signal_handler)
+{
+ value res;
+ value handler;
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ void* saved_spacetime_trie_node_ptr;
+#endif
+#ifdef POSIX_SIGNALS
+ sigset_t nsigs, sigs;
+ /* Block the signal before executing the handler, and record in sigs
+ the original signal mask */
+ sigemptyset(&nsigs);
+ sigaddset(&nsigs, signal_number);
+ sigprocmask(SIG_BLOCK, &nsigs, &sigs);
+#endif
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ /* We record the signal handler's execution separately, in the same
+ trie used for finalisers. */
+ saved_spacetime_trie_node_ptr
+ = caml_spacetime_trie_node_ptr;
+ caml_spacetime_trie_node_ptr
+ = caml_spacetime_finaliser_trie_root;
+#endif
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ /* Handled action may have no associated handler, which we interpret
+ as meaning the signal should be handled by a call to exit. This is
+ is used to allow spacetime profiles to be completed on interrupt */
+ if (caml_signal_handlers == 0) {
+ res = caml_sys_exit(Val_int(2));
+ } else {
+ handler = Field(caml_signal_handlers, signal_number);
+ if (!Is_block(handler)) {
+ res = caml_sys_exit(Val_int(2));
+ } else {
+#else
+ handler = Field(caml_signal_handlers, signal_number);
+#endif
+ res = caml_callback_exn(
+ handler,
+ Val_int(caml_rev_convert_signal_number(signal_number)));
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ }
+ }
+ caml_spacetime_trie_node_ptr = saved_spacetime_trie_node_ptr;
+#endif
+#ifdef POSIX_SIGNALS
+ if (! in_signal_handler) {
+ /* Restore the original signal mask */
+ sigprocmask(SIG_SETMASK, &sigs, NULL);
+ } else if (Is_exception_result(res)) {
+ /* Restore the original signal mask and unblock the signal itself */
+ sigdelset(&sigs, signal_number);
+ sigprocmask(SIG_SETMASK, &sigs, NULL);
+ }
+#endif
+ if (Is_exception_result(res)) caml_raise(Extract_exception(res));
+}
+
+/* Arrange for a garbage collection to be performed as soon as possible */
+
+int volatile caml_requested_major_slice = 0;
+int volatile caml_requested_minor_gc = 0;
+
+void caml_request_major_slice (void)
+{
+ caml_requested_major_slice = 1;
+#ifndef NATIVE_CODE
+ caml_something_to_do = 1;
+#else
+ caml_young_limit = caml_young_alloc_end;
+ /* This is only moderately effective on ports that cache [caml_young_limit]
+ in a register, since [caml_modify] is called directly, not through
+ [caml_c_call], so it may take a while before the register is reloaded
+ from [caml_young_limit]. */
+#endif
+}
+
+void caml_request_minor_gc (void)
+{
+ caml_requested_minor_gc = 1;
+#ifndef NATIVE_CODE
+ caml_something_to_do = 1;
+#else
+ caml_young_limit = caml_young_alloc_end;
+ /* Same remark as above in [caml_request_major_slice]. */
+#endif
+}
+
+/* OS-independent numbering of signals */
+
+#ifndef SIGABRT
+#define SIGABRT -1
+#endif
+#ifndef SIGALRM
+#define SIGALRM -1
+#endif
+#ifndef SIGFPE
+#define SIGFPE -1
+#endif
+#ifndef SIGHUP
+#define SIGHUP -1
+#endif
+#ifndef SIGILL
+#define SIGILL -1
+#endif
+#ifndef SIGINT
+#define SIGINT -1
+#endif
+#ifndef SIGKILL
+#define SIGKILL -1
+#endif
+#ifndef SIGPIPE
+#define SIGPIPE -1
+#endif
+#ifndef SIGQUIT
+#define SIGQUIT -1
+#endif
+#ifndef SIGSEGV
+#define SIGSEGV -1
+#endif
+#ifndef SIGTERM
+#define SIGTERM -1
+#endif
+#ifndef SIGUSR1
+#define SIGUSR1 -1
+#endif
+#ifndef SIGUSR2
+#define SIGUSR2 -1
+#endif
+#ifndef SIGCHLD
+#define SIGCHLD -1
+#endif
+#ifndef SIGCONT
+#define SIGCONT -1
+#endif
+#ifndef SIGSTOP
+#define SIGSTOP -1
+#endif
+#ifndef SIGTSTP
+#define SIGTSTP -1
+#endif
+#ifndef SIGTTIN
+#define SIGTTIN -1
+#endif
+#ifndef SIGTTOU
+#define SIGTTOU -1
+#endif
+#ifndef SIGVTALRM
+#define SIGVTALRM -1
+#endif
+#ifndef SIGPROF
+#define SIGPROF -1
+#endif
+#ifndef SIGBUS
+#define SIGBUS -1
+#endif
+#ifndef SIGPOLL
+#define SIGPOLL -1
+#endif
+#ifndef SIGSYS
+#define SIGSYS -1
+#endif
+#ifndef SIGTRAP
+#define SIGTRAP -1
+#endif
+#ifndef SIGURG
+#define SIGURG -1
+#endif
+#ifndef SIGXCPU
+#define SIGXCPU -1
+#endif
+#ifndef SIGXFSZ
+#define SIGXFSZ -1
+#endif
+
+static int posix_signals[] = {
+ SIGABRT, SIGALRM, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE,
+ SIGQUIT, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGCONT,
+ SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGVTALRM, SIGPROF, SIGBUS,
+ SIGPOLL, SIGSYS, SIGTRAP, SIGURG, SIGXCPU, SIGXFSZ
+};
+
+CAMLexport int caml_convert_signal_number(int signo)
+{
+ if (signo < 0 && signo >= -(sizeof(posix_signals) / sizeof(int)))
+ return posix_signals[-signo-1];
+ else
+ return signo;
+}
+
+CAMLexport int caml_rev_convert_signal_number(int signo)
+{
+ int i;
+ for (i = 0; i < sizeof(posix_signals) / sizeof(int); i++)
+ if (signo == posix_signals[i]) return -i - 1;
+ return signo;
+}
+
+/* Installation of a signal handler (as per [Sys.signal]) */
+
+CAMLprim value caml_install_signal_handler(value signal_number, value action)
+{
+ CAMLparam2 (signal_number, action);
+ CAMLlocal1 (res);
+ int sig, act, oldact;
+
+ sig = caml_convert_signal_number(Int_val(signal_number));
+ if (sig < 0 || sig >= NSIG)
+ caml_invalid_argument("Sys.signal: unavailable signal");
+ switch(action) {
+ case Val_int(0): /* Signal_default */
+ act = 0;
+ break;
+ case Val_int(1): /* Signal_ignore */
+ act = 1;
+ break;
+ default: /* Signal_handle */
+ act = 2;
+ break;
+ }
+ oldact = caml_set_signal_action(sig, act);
+ switch (oldact) {
+ case 0: /* was Signal_default */
+ res = Val_int(0);
+ break;
+ case 1: /* was Signal_ignore */
+ res = Val_int(1);
+ break;
+ case 2: /* was Signal_handle */
+ #if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ /* Handled action may have no associated handler
+ which we treat as Signal_default */
+ if (caml_signal_handlers == 0) {
+ res = Val_int(0);
+ } else {
+ if (!Is_block(Field(caml_signal_handlers, sig))) {
+ res = Val_int(0);
+ } else {
+ res = caml_alloc_small (1, 0);
+ Field(res, 0) = Field(caml_signal_handlers, sig);
+ }
+ }
+ #else
+ res = caml_alloc_small (1, 0);
+ Field(res, 0) = Field(caml_signal_handlers, sig);
+ #endif
+ break;
+ default: /* error in caml_set_signal_action */
+ caml_sys_error(NO_ARG);
+ }
+ if (Is_block(action)) {
+ if (caml_signal_handlers == 0) {
+ caml_signal_handlers = caml_alloc(NSIG, 0);
+ caml_register_global_root(&caml_signal_handlers);
+ }
+ caml_modify(&Field(caml_signal_handlers, sig), Field(action, 0));
+ }
+ caml_process_pending_signals();
+ CAMLreturn (res);
+}
diff --git a/test/monniaux/ocaml/byterun/signals_byt.c b/test/monniaux/ocaml/byterun/signals_byt.c
new file mode 100644
index 00000000..bdbcf726
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/signals_byt.c
@@ -0,0 +1,101 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 2007 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Signal handling, code specific to the bytecode interpreter */
+
+#include <signal.h>
+#include <errno.h>
+#include "caml/config.h"
+#include "caml/memory.h"
+#include "caml/osdeps.h"
+#include "caml/signals.h"
+#include "caml/signals_machdep.h"
+
+#ifndef NSIG
+#define NSIG 64
+#endif
+
+#ifdef _WIN32
+typedef void (*sighandler)(int sig);
+extern sighandler caml_win32_signal(int sig, sighandler action);
+#define signal(sig,act) caml_win32_signal(sig,act)
+#endif
+
+CAMLexport int volatile caml_something_to_do = 0;
+CAMLexport void (* volatile caml_async_action_hook)(void) = NULL;
+
+void caml_process_event(void)
+{
+ void (*async_action)(void);
+
+ caml_check_urgent_gc (Val_unit);
+ caml_process_pending_signals();
+ async_action = caml_async_action_hook;
+ if (async_action != NULL) {
+ caml_async_action_hook = NULL;
+ (*async_action)();
+ }
+}
+
+static void handle_signal(int signal_number)
+{
+ int saved_errno;
+ /* Save the value of errno (PR#5982). */
+ saved_errno = errno;
+#if !defined(POSIX_SIGNALS) && !defined(BSD_SIGNALS)
+ signal(signal_number, handle_signal);
+#endif
+ if (signal_number < 0 || signal_number >= NSIG) return;
+ if (caml_try_leave_blocking_section_hook()) {
+ caml_execute_signal(signal_number, 1);
+ caml_enter_blocking_section_hook();
+ }else{
+ caml_record_signal(signal_number);
+ }
+ errno = saved_errno;
+}
+
+int caml_set_signal_action(int signo, int action)
+{
+ void (*act)(int signo), (*oldact)(int signo);
+#ifdef POSIX_SIGNALS
+ struct sigaction sigact, oldsigact;
+#endif
+
+ switch (action) {
+ case 0: act = SIG_DFL; break;
+ case 1: act = SIG_IGN; break;
+ default: act = handle_signal; break;
+ }
+
+#ifdef POSIX_SIGNALS
+ sigact.sa_handler = act;
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = 0;
+ if (sigaction(signo, &sigact, &oldsigact) == -1) return -1;
+ oldact = oldsigact.sa_handler;
+#else
+ oldact = signal(signo, act);
+ if (oldact == SIG_ERR) return -1;
+#endif
+ if (oldact == handle_signal)
+ return 2;
+ else if (oldact == SIG_IGN)
+ return 1;
+ else
+ return 0;
+}
diff --git a/test/monniaux/ocaml/byterun/spacetime.c b/test/monniaux/ocaml/byterun/spacetime.c
new file mode 100644
index 00000000..2b0bf1dc
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/spacetime.c
@@ -0,0 +1,38 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Mark Shinwell and Leo White, Jane Street Europe */
+/* */
+/* Copyright 2013--2016, Jane Street Group, LLC */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#include "caml/fail.h"
+#include "caml/mlvalues.h"
+
+int caml_ensure_spacetime_dot_o_is_included = 42;
+
+CAMLprim value caml_spacetime_only_works_for_native_code(value foo, ...)
+{
+ caml_failwith("Spacetime profiling only works for native code");
+}
+
+uintnat caml_spacetime_my_profinfo (void)
+{
+ return 0;
+}
+
+CAMLprim value caml_spacetime_enabled (value v_unit)
+{
+ return Val_false; /* running in bytecode */
+}
+
+CAMLprim value caml_register_channel_for_spacetime (value v_channel)
+{
+ return Val_unit;
+}
diff --git a/test/monniaux/ocaml/byterun/stacks.c b/test/monniaux/ocaml/byterun/stacks.c
new file mode 100644
index 00000000..d6e7f53c
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/stacks.c
@@ -0,0 +1,119 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* To initialize and resize the stacks */
+
+#include <string.h>
+#include "caml/config.h"
+#include "caml/fail.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/stacks.h"
+
+CAMLexport value * caml_stack_low;
+CAMLexport value * caml_stack_high;
+CAMLexport value * caml_stack_threshold;
+CAMLexport value * caml_extern_sp;
+CAMLexport value * caml_trapsp;
+CAMLexport value * caml_trap_barrier;
+value caml_global_data = 0;
+
+uintnat caml_max_stack_size; /* also used in gc_ctrl.c */
+
+void caml_init_stack (uintnat initial_max_size)
+{
+ caml_stack_low = (value *) caml_stat_alloc(Stack_size);
+ caml_stack_high = caml_stack_low + Stack_size / sizeof (value);
+ caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
+ caml_extern_sp = caml_stack_high;
+ caml_trapsp = caml_stack_high;
+ caml_trap_barrier = caml_stack_high + 1;
+ caml_max_stack_size = initial_max_size;
+ caml_gc_message (0x08, "Initial stack limit: %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ caml_max_stack_size / 1024 * sizeof (value));
+}
+
+void caml_realloc_stack(asize_t required_space)
+{
+ asize_t size;
+ value * new_low, * new_high, * new_sp;
+ value * p;
+
+ CAMLassert(caml_extern_sp >= caml_stack_low);
+ size = caml_stack_high - caml_stack_low;
+ do {
+ if (size >= caml_max_stack_size) caml_raise_stack_overflow();
+ size *= 2;
+ } while (size < caml_stack_high - caml_extern_sp + required_space);
+ caml_gc_message (0x08, "Growing stack to %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ (uintnat) size * sizeof(value) / 1024);
+ new_low = (value *) caml_stat_alloc(size * sizeof(value));
+ new_high = new_low + size;
+
+#define shift(ptr) \
+ ((char *) new_high - ((char *) caml_stack_high - (char *) (ptr)))
+
+ new_sp = (value *) shift(caml_extern_sp);
+ memmove((char *) new_sp,
+ (char *) caml_extern_sp,
+ (caml_stack_high - caml_extern_sp) * sizeof(value));
+ caml_stat_free(caml_stack_low);
+ caml_trapsp = (value *) shift(caml_trapsp);
+ caml_trap_barrier = (value *) shift(caml_trap_barrier);
+ for (p = caml_trapsp; p < new_high; p = Trap_link(p))
+ Trap_link(p) = (value *) shift(Trap_link(p));
+ caml_stack_low = new_low;
+ caml_stack_high = new_high;
+ caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
+ caml_extern_sp = new_sp;
+
+#undef shift
+}
+
+CAMLprim value caml_ensure_stack_capacity(value required_space)
+{
+ asize_t req = Long_val(required_space);
+ if (caml_extern_sp - req < caml_stack_low) caml_realloc_stack(req);
+ return Val_unit;
+}
+
+void caml_change_max_stack_size (uintnat new_max_size)
+{
+ asize_t size = caml_stack_high - caml_extern_sp
+ + Stack_threshold / sizeof (value);
+
+ if (new_max_size < size) new_max_size = size;
+ if (new_max_size != caml_max_stack_size){
+ caml_gc_message (0x08, "Changing stack limit to %"
+ ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
+ new_max_size * sizeof (value) / 1024);
+ }
+ caml_max_stack_size = new_max_size;
+}
+
+CAMLexport uintnat (*caml_stack_usage_hook)(void) = NULL;
+
+uintnat caml_stack_usage(void)
+{
+ uintnat sz;
+ sz = caml_stack_high - caml_extern_sp;
+ if (caml_stack_usage_hook != NULL)
+ sz += (*caml_stack_usage_hook)();
+ return sz;
+}
diff --git a/test/monniaux/ocaml/byterun/startup.c b/test/monniaux/ocaml/byterun/startup.c
new file mode 100644
index 00000000..037f30d0
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/startup.c
@@ -0,0 +1,531 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Start-up code */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include "caml/config.h"
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#ifdef _WIN32
+#include <process.h>
+#endif
+#include "caml/alloc.h"
+#include "caml/backtrace.h"
+#include "caml/callback.h"
+#include "caml/custom.h"
+#include "caml/debugger.h"
+#include "caml/dynlink.h"
+#include "caml/exec.h"
+#include "caml/fail.h"
+#include "caml/fix_code.h"
+#include "caml/freelist.h"
+#include "caml/gc_ctrl.h"
+#include "caml/instrtrace.h"
+#include "caml/interp.h"
+#include "caml/intext.h"
+#include "caml/io.h"
+#include "caml/memory.h"
+#include "caml/minor_gc.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/osdeps.h"
+#include "caml/prims.h"
+#include "caml/printexc.h"
+#include "caml/reverse.h"
+#include "caml/signals.h"
+#include "caml/stacks.h"
+#include "caml/sys.h"
+#include "caml/startup.h"
+#include "caml/startup_aux.h"
+#include "caml/version.h"
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+#ifndef SEEK_END
+#define SEEK_END 2
+#endif
+
+/* Read the trailer of a bytecode file */
+
+static void fixup_endianness_trailer(uint32_t * p)
+{
+#ifndef ARCH_BIG_ENDIAN
+ Reverse_32(p, p);
+#endif
+}
+
+static int read_trailer(int fd, struct exec_trailer *trail)
+{
+ if (lseek(fd, (long) -TRAILER_SIZE, SEEK_END) == -1)
+ return BAD_BYTECODE;
+ if (read(fd, (char *) trail, TRAILER_SIZE) < TRAILER_SIZE)
+ return BAD_BYTECODE;
+ fixup_endianness_trailer(&trail->num_sections);
+ if (strncmp(trail->magic, EXEC_MAGIC, 12) == 0)
+ return 0;
+ else
+ return BAD_BYTECODE;
+}
+
+int caml_attempt_open(char_os **name, struct exec_trailer *trail,
+ int do_open_script)
+{
+ char_os * truename;
+ int fd;
+ int err;
+ char buf [2], * u8;
+
+ truename = caml_search_exe_in_path(*name);
+ u8 = caml_stat_strdup_of_os(truename);
+ caml_gc_message(0x100, "Opening bytecode executable %s\n", u8);
+ caml_stat_free(u8);
+ fd = open_os(truename, O_RDONLY | O_BINARY);
+ if (fd == -1) {
+ caml_stat_free(truename);
+ caml_gc_message(0x100, "Cannot open file\n");
+ return FILE_NOT_FOUND;
+ }
+ if (!do_open_script) {
+ err = read (fd, buf, 2);
+ if (err < 2 || (buf [0] == '#' && buf [1] == '!')) {
+ close(fd);
+ caml_stat_free(truename);
+ caml_gc_message(0x100, "Rejected #! script\n");
+ return BAD_BYTECODE;
+ }
+ }
+ err = read_trailer(fd, trail);
+ if (err != 0) {
+ close(fd);
+ caml_stat_free(truename);
+ caml_gc_message(0x100, "Not a bytecode executable\n");
+ return err;
+ }
+ *name = truename;
+ return fd;
+}
+
+/* Read the section descriptors */
+
+void caml_read_section_descriptors(int fd, struct exec_trailer *trail)
+{
+ int toc_size, i;
+
+ toc_size = trail->num_sections * 8;
+ trail->section = caml_stat_alloc(toc_size);
+ lseek(fd, - (long) (TRAILER_SIZE + toc_size), SEEK_END);
+ if (read(fd, (char *) trail->section, toc_size) != toc_size)
+ caml_fatal_error("Fatal error: cannot read section table\n");
+ /* Fixup endianness of lengths */
+ for (i = 0; i < trail->num_sections; i++)
+ fixup_endianness_trailer(&(trail->section[i].len));
+}
+
+/* Position fd at the beginning of the section having the given name.
+ Return the length of the section data in bytes, or -1 if no section
+ found with that name. */
+
+int32_t caml_seek_optional_section(int fd, struct exec_trailer *trail,
+ char *name)
+{
+ long ofs;
+ int i;
+
+ ofs = TRAILER_SIZE + trail->num_sections * 8;
+ for (i = trail->num_sections - 1; i >= 0; i--) {
+ ofs += trail->section[i].len;
+ if (strncmp(trail->section[i].name, name, 4) == 0) {
+ lseek(fd, -ofs, SEEK_END);
+ return trail->section[i].len;
+ }
+ }
+ return -1;
+}
+
+/* Position fd at the beginning of the section having the given name.
+ Return the length of the section data in bytes. */
+
+int32_t caml_seek_section(int fd, struct exec_trailer *trail, char *name)
+{
+ int32_t len = caml_seek_optional_section(fd, trail, name);
+ if (len == -1)
+ caml_fatal_error_arg("Fatal_error: section `%s' is missing\n", name);
+ return len;
+}
+
+/* Read and return the contents of the section having the given name.
+ Add a terminating 0. Return NULL if no such section. */
+
+static char * read_section(int fd, struct exec_trailer *trail, char *name)
+{
+ int32_t len;
+ char * data;
+
+ len = caml_seek_optional_section(fd, trail, name);
+ if (len == -1) return NULL;
+ data = caml_stat_alloc(len + 1);
+ if (read(fd, data, len) != len)
+ caml_fatal_error_arg("Fatal error: error reading section %s\n", name);
+ data[len] = 0;
+ return data;
+}
+
+#ifdef _WIN32
+
+static char_os * read_section_to_os(int fd, struct exec_trailer *trail, char *name)
+{
+ int32_t len, wlen;
+ char * data;
+ wchar_t * wdata;
+
+ len = caml_seek_optional_section(fd, trail, name);
+ if (len == -1) return NULL;
+ data = caml_stat_alloc(len + 1);
+ if (read(fd, data, len) != len)
+ caml_fatal_error_arg("Fatal error: error reading section %s\n", name);
+ data[len] = 0;
+ wlen = win_multi_byte_to_wide_char(data, len, NULL, 0);
+ wdata = caml_stat_alloc((wlen + 1)*sizeof(wchar_t));
+ win_multi_byte_to_wide_char(data, len, wdata, wlen);
+ wdata[wlen] = 0;
+ caml_stat_free(data);
+ return wdata;
+}
+
+#else
+
+#define read_section_to_os read_section
+
+#endif
+
+/* Invocation of ocamlrun: 4 cases.
+
+ 1. runtime + bytecode
+ user types: ocamlrun [options] bytecode args...
+ arguments: ocamlrun [options] bytecode args...
+
+ 2. bytecode script
+ user types: bytecode args...
+ 2a (kernel 1) arguments: ocamlrun ./bytecode args...
+ 2b (kernel 2) arguments: bytecode bytecode args...
+
+ 3. concatenated runtime and bytecode
+ user types: composite args...
+ arguments: composite args...
+
+Algorithm:
+ 1- If argument 0 is a valid byte-code file that does not start with #!,
+ then we are in case 3 and we pass the same command line to the
+ OCaml program.
+ 2- In all other cases, we parse the command line as:
+ (whatever) [options] bytecode args...
+ and we strip "(whatever) [options]" from the command line.
+
+*/
+
+/* Parse options on the command line */
+
+static int parse_command_line(char_os **argv)
+{
+ int i, j;
+
+ for(i = 1; argv[i] != NULL && argv[i][0] == _T('-'); i++) {
+ switch(argv[i][1]) {
+ case _T('t'):
+ ++ caml_trace_level; /* ignored unless DEBUG mode */
+ break;
+ case _T('v'):
+ if (!strcmp_os (argv[i], _T("-version"))){
+ printf ("The OCaml runtime, version " OCAML_VERSION_STRING "\n");
+ exit (0);
+ }else if (!strcmp_os (argv[i], _T("-vnum"))){
+ printf (OCAML_VERSION_STRING "\n");
+ exit (0);
+ }else{
+ caml_verb_gc = 0x001+0x004+0x008+0x010+0x020;
+ }
+ break;
+ case _T('p'):
+ for (j = 0; caml_names_of_builtin_cprim[j] != NULL; j++)
+ printf("%s\n", caml_names_of_builtin_cprim[j]);
+ exit(0);
+ break;
+ case _T('b'):
+ caml_record_backtrace(Val_true);
+ break;
+ case _T('I'):
+ if (argv[i + 1] != NULL) {
+ caml_ext_table_add(&caml_shared_libs_path, argv[i + 1]);
+ i++;
+ }
+ break;
+ default:
+ caml_fatal_error_arg("Unknown option %s.\n", caml_stat_strdup_of_os(argv[i]));
+ }
+ }
+ return i;
+}
+
+extern void caml_init_ieee_floats (void);
+
+#ifdef _WIN32
+extern void caml_signal_thread(void * lpParam);
+#endif
+
+#if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
+
+/* PR 4887: avoid crash box of windows runtime on some system calls */
+extern void caml_install_invalid_parameter_handler();
+
+#endif
+
+extern int caml_ensure_spacetime_dot_o_is_included;
+
+/* Main entry point when loading code from a file */
+
+CAMLexport void caml_main(char_os **argv)
+{
+ int fd, pos;
+ struct exec_trailer trail;
+ struct channel * chan;
+ value res;
+ char * req_prims;
+ char_os * shared_lib_path, * shared_libs;
+ char_os * exe_name, * proc_self_exe;
+
+ caml_ensure_spacetime_dot_o_is_included++;
+
+ /* Determine options */
+#ifdef DEBUG
+ caml_verb_gc = 0x3F;
+#endif
+ caml_parse_ocamlrunparam();
+#ifdef DEBUG
+ caml_gc_message (-1, "### OCaml runtime: debug mode ###\n");
+#endif
+ if (!caml_startup_aux(/* pooling */ caml_cleanup_on_exit))
+ return;
+
+ /* Machine-dependent initialization of the floating-point hardware
+ so that it behaves as much as possible as specified in IEEE */
+ caml_init_ieee_floats();
+#if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
+ caml_install_invalid_parameter_handler();
+#endif
+ caml_init_custom_operations();
+ caml_ext_table_init(&caml_shared_libs_path, 8);
+ caml_external_raise = NULL;
+
+ /* Determine position of bytecode file */
+ pos = 0;
+
+ /* First, try argv[0] (when ocamlrun is called by a bytecode program) */
+ exe_name = argv[0];
+ fd = caml_attempt_open(&exe_name, &trail, 0);
+
+ /* Little grasshopper wonders why we do that at all, since
+ "The current executable is ocamlrun itself, it's never a bytecode
+ program". Little grasshopper "ocamlc -custom" in mind should keep.
+ With -custom, we have an executable that is ocamlrun itself
+ concatenated with the bytecode. So, if the attempt with argv[0]
+ failed, it is worth trying again with executable_name. */
+ if (fd < 0 && (proc_self_exe = caml_executable_name()) != NULL) {
+ exe_name = proc_self_exe;
+ fd = caml_attempt_open(&exe_name, &trail, 0);
+ }
+
+ if (fd < 0) {
+ pos = parse_command_line(argv);
+ if (argv[pos] == 0)
+ caml_fatal_error("No bytecode file specified.\n");
+ exe_name = argv[pos];
+ fd = caml_attempt_open(&exe_name, &trail, 1);
+ switch(fd) {
+ case FILE_NOT_FOUND:
+ caml_fatal_error_arg("Fatal error: cannot find file '%s'\n", caml_stat_strdup_of_os(argv[pos]));
+ break;
+ case BAD_BYTECODE:
+ caml_fatal_error_arg(
+ "Fatal error: the file '%s' is not a bytecode executable file\n",
+ caml_stat_strdup_of_os(exe_name));
+ break;
+ }
+ }
+ /* Read the table of contents (section descriptors) */
+ caml_read_section_descriptors(fd, &trail);
+ /* Initialize the abstract machine */
+ caml_init_gc (caml_init_minor_heap_wsz, caml_init_heap_wsz,
+ caml_init_heap_chunk_sz, caml_init_percent_free,
+ caml_init_max_percent_free, caml_init_major_window);
+ caml_init_stack (caml_init_max_stack_wsz);
+ caml_init_atom_table();
+ caml_init_backtrace();
+ /* Initialize the interpreter */
+ caml_interprete(NULL, 0);
+ /* Initialize the debugger, if needed */
+ caml_debugger_init();
+ /* Load the code */
+ caml_code_size = caml_seek_section(fd, &trail, "CODE");
+ caml_load_code(fd, caml_code_size);
+ caml_init_debug_info();
+ /* Build the table of primitives */
+ shared_lib_path = read_section_to_os(fd, &trail, "DLPT");
+ shared_libs = read_section_to_os(fd, &trail, "DLLS");
+ req_prims = read_section(fd, &trail, "PRIM");
+ if (req_prims == NULL) caml_fatal_error("Fatal error: no PRIM section\n");
+ caml_build_primitive_table(shared_lib_path, shared_libs, req_prims);
+ caml_stat_free(shared_lib_path);
+ caml_stat_free(shared_libs);
+ caml_stat_free(req_prims);
+ /* Load the globals */
+ caml_seek_section(fd, &trail, "DATA");
+ chan = caml_open_descriptor_in(fd);
+ caml_global_data = caml_input_val(chan);
+ caml_close_channel(chan); /* this also closes fd */
+ caml_stat_free(trail.section);
+ /* Ensure that the globals are in the major heap. */
+ caml_oldify_one (caml_global_data, &caml_global_data);
+ caml_oldify_mopup ();
+ /* Initialize system libraries */
+ caml_sys_init(exe_name, argv + pos);
+#ifdef _WIN32
+ /* Start a thread to handle signals */
+ if (caml_secure_getenv(_T("CAMLSIGPIPE")))
+ _beginthread(caml_signal_thread, 4096, NULL);
+#endif
+ /* Execute the program */
+ caml_debugger(PROGRAM_START);
+ res = caml_interprete(caml_start_code, caml_code_size);
+ if (Is_exception_result(res)) {
+ caml_exn_bucket = Extract_exception(res);
+ if (caml_debugger_in_use) {
+ caml_extern_sp = &caml_exn_bucket; /* The debugger needs the
+ exception value.*/
+ caml_debugger(UNCAUGHT_EXC);
+ }
+ caml_fatal_uncaught_exception(caml_exn_bucket);
+ }
+}
+
+/* Main entry point when code is linked in as initialized data */
+
+CAMLexport value caml_startup_code_exn(
+ code_t code, asize_t code_size,
+ char *data, asize_t data_size,
+ char *section_table, asize_t section_table_size,
+ int pooling,
+ char_os **argv)
+{
+ char_os * cds_file;
+ char_os * exe_name;
+
+ /* Determine options */
+#ifdef DEBUG
+ caml_verb_gc = 0x3F;
+#endif
+ caml_parse_ocamlrunparam();
+#ifdef DEBUG
+ caml_gc_message (-1, "### OCaml runtime: debug mode ###\n");
+#endif
+ if (caml_cleanup_on_exit)
+ pooling = 1;
+ if (!caml_startup_aux(pooling))
+ return Val_unit;
+
+ caml_init_ieee_floats();
+#if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
+ caml_install_invalid_parameter_handler();
+#endif
+ caml_init_custom_operations();
+ cds_file = caml_secure_getenv(_T("CAML_DEBUG_FILE"));
+ if (cds_file != NULL) {
+ caml_cds_file = caml_stat_strdup_os(cds_file);
+ }
+ exe_name = caml_executable_name();
+ if (exe_name == NULL) exe_name = caml_search_exe_in_path(argv[0]);
+ caml_external_raise = NULL;
+ /* Initialize the abstract machine */
+ caml_init_gc (caml_init_minor_heap_wsz, caml_init_heap_wsz,
+ caml_init_heap_chunk_sz, caml_init_percent_free,
+ caml_init_max_percent_free, caml_init_major_window);
+ caml_init_stack (caml_init_max_stack_wsz);
+ caml_init_atom_table();
+ caml_init_backtrace();
+ /* Initialize the interpreter */
+ caml_interprete(NULL, 0);
+ /* Initialize the debugger, if needed */
+ caml_debugger_init();
+ /* Load the code */
+ caml_start_code = code;
+ caml_code_size = code_size;
+ caml_init_code_fragments();
+ caml_init_debug_info();
+ if (caml_debugger_in_use) {
+ int len, i;
+ len = code_size / sizeof(opcode_t);
+ caml_saved_code = (unsigned char *) caml_stat_alloc(len);
+ for (i = 0; i < len; i++) caml_saved_code[i] = caml_start_code[i];
+ }
+#ifdef THREADED_CODE
+ caml_thread_code(caml_start_code, code_size);
+#endif
+ /* Use the builtin table of primitives */
+ caml_build_primitive_table_builtin();
+ /* Load the globals */
+ caml_global_data = caml_input_value_from_block(data, data_size);
+ /* Ensure that the globals are in the major heap. */
+ caml_oldify_one (caml_global_data, &caml_global_data);
+ caml_oldify_mopup ();
+ /* Record the sections (for caml_get_section_table in meta.c) */
+ caml_section_table = section_table;
+ caml_section_table_size = section_table_size;
+ /* Initialize system libraries */
+ caml_sys_init(exe_name, argv);
+ /* Execute the program */
+ caml_debugger(PROGRAM_START);
+ return caml_interprete(caml_start_code, caml_code_size);
+}
+
+CAMLexport void caml_startup_code(
+ code_t code, asize_t code_size,
+ char *data, asize_t data_size,
+ char *section_table, asize_t section_table_size,
+ int pooling,
+ char_os **argv)
+{
+ value res;
+
+ res = caml_startup_code_exn(code, code_size, data, data_size,
+ section_table, section_table_size,
+ pooling, argv);
+ if (Is_exception_result(res)) {
+ caml_exn_bucket = Extract_exception(res);
+ if (caml_debugger_in_use) {
+ caml_extern_sp = &caml_exn_bucket; /* The debugger needs the
+ exception value.*/
+ caml_debugger(UNCAUGHT_EXC);
+ }
+ caml_fatal_uncaught_exception(caml_exn_bucket);
+ }
+}
diff --git a/test/monniaux/ocaml/byterun/startup_aux.c b/test/monniaux/ocaml/byterun/startup_aux.c
new file mode 100644
index 00000000..b913bfae
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/startup_aux.c
@@ -0,0 +1,168 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Some runtime initialization functions that are common to bytecode
+ and native code. */
+
+#include <stdio.h>
+#include "caml/backtrace.h"
+#include "caml/memory.h"
+#include "caml/callback.h"
+#include "caml/major_gc.h"
+#ifndef NATIVE_CODE
+#include "caml/dynlink.h"
+#endif
+#include "caml/osdeps.h"
+#include "caml/startup_aux.h"
+
+
+/* Initialize the atom table */
+
+CAMLexport header_t caml_atom_table[256];
+void caml_init_atom_table(void)
+{
+ int i;
+ for(i = 0; i < 256; i++) {
+#ifdef NATIVE_CODE
+ caml_atom_table[i] = Make_header_allocated_here(0, i, Caml_white);
+#else
+ caml_atom_table[i] = Make_header(0, i, Caml_white);
+#endif
+ }
+ if (caml_page_table_add(In_static_data,
+ caml_atom_table, caml_atom_table + 256) != 0) {
+ caml_fatal_error("Fatal error: not enough memory for initial page table");
+ }
+}
+
+
+/* Parse the OCAMLRUNPARAM environment variable. */
+
+uintnat caml_init_percent_free = Percent_free_def;
+uintnat caml_init_max_percent_free = Max_percent_free_def;
+uintnat caml_init_minor_heap_wsz = Minor_heap_def;
+uintnat caml_init_heap_chunk_sz = Heap_chunk_def;
+uintnat caml_init_heap_wsz = Init_heap_def;
+uintnat caml_init_max_stack_wsz = Max_stack_def;
+uintnat caml_init_major_window = Major_window_def;
+extern int caml_parser_trace;
+uintnat caml_trace_level = 0;
+uintnat caml_cleanup_on_exit = 0;
+
+
+static void scanmult (char_os *opt, uintnat *var)
+{
+ char_os mult = _T(' ');
+ unsigned int val = 1;
+ sscanf_os (opt, _T("=%u%c"), &val, &mult);
+ sscanf_os (opt, _T("=0x%x%c"), &val, &mult);
+ switch (mult) {
+ case _T('k'): *var = (uintnat) val * 1024; break;
+ case _T('M'): *var = (uintnat) val * (1024 * 1024); break;
+ case _T('G'): *var = (uintnat) val * (1024 * 1024 * 1024); break;
+ default: *var = (uintnat) val; break;
+ }
+}
+
+void caml_parse_ocamlrunparam(void)
+{
+ char_os *opt = caml_secure_getenv (_T("OCAMLRUNPARAM"));
+ uintnat p;
+
+ if (opt == NULL) opt = caml_secure_getenv (_T("CAMLRUNPARAM"));
+
+ if (opt != NULL){
+ while (*opt != _T('\0')){
+ switch (*opt++){
+ case _T('a'): scanmult (opt, &p); caml_set_allocation_policy (p); break;
+ case _T('b'): scanmult (opt, &p); caml_record_backtrace(Val_bool (p)); break;
+ case _T('c'): scanmult (opt, &p); caml_cleanup_on_exit = p; break;
+ case _T('h'): scanmult (opt, &caml_init_heap_wsz); break;
+ case _T('H'): scanmult (opt, &caml_use_huge_pages); break;
+ case _T('i'): scanmult (opt, &caml_init_heap_chunk_sz); break;
+ case _T('l'): scanmult (opt, &caml_init_max_stack_wsz); break;
+ case _T('o'): scanmult (opt, &caml_init_percent_free); break;
+ case _T('O'): scanmult (opt, &caml_init_max_percent_free); break;
+ case _T('p'): scanmult (opt, &p); caml_parser_trace = p; break;
+ case _T('R'): break; /* see stdlib/hashtbl.mli */
+ case _T('s'): scanmult (opt, &caml_init_minor_heap_wsz); break;
+ case _T('t'): scanmult (opt, &caml_trace_level); break;
+ case _T('v'): scanmult (opt, &caml_verb_gc); break;
+ case _T('w'): scanmult (opt, &caml_init_major_window); break;
+ case _T('W'): scanmult (opt, &caml_runtime_warnings); break;
+ }
+ while (*opt != _T('\0')){
+ if (*opt++ == ',') break;
+ }
+ }
+ }
+}
+
+
+/* The number of outstanding calls to caml_startup */
+static int startup_count = 0;
+
+/* Has the runtime been shut down already? */
+static int shutdown_happened = 0;
+
+
+int caml_startup_aux(int pooling)
+{
+ if (shutdown_happened == 1)
+ caml_fatal_error("Fatal error: caml_startup was called after the runtime "
+ "was shut down with caml_shutdown");
+
+ /* Second and subsequent calls are ignored,
+ since the runtime has already started */
+ startup_count++;
+ if (startup_count > 1)
+ return 0;
+
+ if (pooling)
+ caml_stat_create_pool();
+
+ return 1;
+}
+
+static void call_registered_value(char* name)
+{
+ value *f = caml_named_value(name);
+ if (f != NULL)
+ caml_callback_exn(*f, Val_unit);
+}
+
+CAMLexport void caml_shutdown(void)
+{
+ if (startup_count <= 0)
+ caml_fatal_error("Fatal error: a call to caml_shutdown has no "
+ "corresponding call to caml_startup");
+
+ /* Do nothing unless it's the last call remaining */
+ startup_count--;
+ if (startup_count > 0)
+ return;
+
+ call_registered_value("Pervasives.do_at_exit");
+ call_registered_value("Thread.at_shutdown");
+ caml_finalise_heap();
+#ifndef NATIVE_CODE
+ caml_free_shared_libs();
+#endif
+ caml_stat_destroy_pool();
+
+ shutdown_happened = 1;
+}
diff --git a/test/monniaux/ocaml/byterun/str.c b/test/monniaux/ocaml/byterun/str.c
new file mode 100644
index 00000000..8e07cb03
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/str.c
@@ -0,0 +1,474 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Operations on strings */
+
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/misc.h"
+
+/* returns a number of bytes (chars) */
+CAMLexport mlsize_t caml_string_length(value s)
+{
+ mlsize_t temp;
+ temp = Bosize_val(s) - 1;
+ CAMLassert (Byte (s, temp - Byte (s, temp)) == 0);
+ return temp - Byte (s, temp);
+}
+
+/* returns a value that represents a number of bytes (chars) */
+CAMLprim value caml_ml_string_length(value s)
+{
+ mlsize_t temp;
+ temp = Bosize_val(s) - 1;
+ CAMLassert (Byte (s, temp - Byte (s, temp)) == 0);
+ return Val_long(temp - Byte (s, temp));
+}
+
+CAMLprim value caml_ml_bytes_length(value s)
+{
+ return caml_ml_string_length(s);
+}
+
+CAMLexport int caml_string_is_c_safe (value s)
+{
+ return strlen(String_val(s)) == caml_string_length(s);
+}
+
+/**
+ * [caml_create_string] is deprecated,
+ * use [caml_create_bytes] instead
+ */
+CAMLprim value caml_create_string(value len)
+{
+ mlsize_t size = Long_val(len);
+ if (size > Bsize_wsize (Max_wosize) - 1){
+ caml_invalid_argument("String.create");
+ }
+ return caml_alloc_string(size);
+}
+
+/* [len] is a value that represents a number of bytes (chars) */
+CAMLprim value caml_create_bytes(value len)
+{
+ mlsize_t size = Long_val(len);
+ if (size > Bsize_wsize (Max_wosize) - 1){
+ caml_invalid_argument("Bytes.create");
+ }
+ return caml_alloc_string(size);
+}
+
+
+
+CAMLprim value caml_string_get(value str, value index)
+{
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx >= caml_string_length(str)) caml_array_bound_error();
+ return Val_int(Byte_u(str, idx));
+}
+
+CAMLprim value caml_bytes_get(value str, value index)
+{
+ return caml_string_get(str, index);
+}
+
+CAMLprim value caml_bytes_set(value str, value index, value newval)
+{
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx >= caml_string_length(str)) caml_array_bound_error();
+ Byte_u(str, idx) = Int_val(newval);
+ return Val_unit;
+}
+
+/**
+ * [caml_string_set] is deprecated,
+ * use [caml_bytes_set] instead
+ */
+CAMLprim value caml_string_set(value str, value index, value newval)
+{
+ return caml_bytes_set(str,index,newval);
+}
+
+
+CAMLprim value caml_string_get16(value str, value index)
+{
+ intnat res;
+ unsigned char b1, b2;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 1 >= caml_string_length(str)) caml_array_bound_error();
+ b1 = Byte_u(str, idx);
+ b2 = Byte_u(str, idx + 1);
+#ifdef ARCH_BIG_ENDIAN
+ res = b1 << 8 | b2;
+#else
+ res = b2 << 8 | b1;
+#endif
+ return Val_int(res);
+}
+
+CAMLprim value caml_bytes_get16(value str, value index)
+{
+ return caml_string_get16(str,index);
+}
+
+CAMLprim value caml_string_get32(value str, value index)
+{
+ intnat res;
+ unsigned char b1, b2, b3, b4;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 3 >= caml_string_length(str)) caml_array_bound_error();
+ b1 = Byte_u(str, idx);
+ b2 = Byte_u(str, idx + 1);
+ b3 = Byte_u(str, idx + 2);
+ b4 = Byte_u(str, idx + 3);
+#ifdef ARCH_BIG_ENDIAN
+ res = b1 << 24 | b2 << 16 | b3 << 8 | b4;
+#else
+ res = b4 << 24 | b3 << 16 | b2 << 8 | b1;
+#endif
+ return caml_copy_int32(res);
+}
+
+CAMLprim value caml_bytes_get32(value str, value index)
+{
+ return caml_string_get32(str,index);
+}
+
+CAMLprim value caml_string_get64(value str, value index)
+{
+ uint64_t res;
+ unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 7 >= caml_string_length(str)) caml_array_bound_error();
+ b1 = Byte_u(str, idx);
+ b2 = Byte_u(str, idx + 1);
+ b3 = Byte_u(str, idx + 2);
+ b4 = Byte_u(str, idx + 3);
+ b5 = Byte_u(str, idx + 4);
+ b6 = Byte_u(str, idx + 5);
+ b7 = Byte_u(str, idx + 6);
+ b8 = Byte_u(str, idx + 7);
+#ifdef ARCH_BIG_ENDIAN
+ res = (uint64_t) b1 << 56 | (uint64_t) b2 << 48
+ | (uint64_t) b3 << 40 | (uint64_t) b4 << 32
+ | (uint64_t) b5 << 24 | (uint64_t) b6 << 16
+ | (uint64_t) b7 << 8 | (uint64_t) b8;
+#else
+ res = (uint64_t) b8 << 56 | (uint64_t) b7 << 48
+ | (uint64_t) b6 << 40 | (uint64_t) b5 << 32
+ | (uint64_t) b4 << 24 | (uint64_t) b3 << 16
+ | (uint64_t) b2 << 8 | (uint64_t) b1;
+#endif
+ return caml_copy_int64(res);
+}
+
+CAMLprim value caml_bytes_get64(value str, value index)
+{
+ return caml_string_get64(str,index);
+}
+
+CAMLprim value caml_bytes_set16(value str, value index, value newval)
+{
+ unsigned char b1, b2;
+ intnat val;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 1 >= caml_string_length(str)) caml_array_bound_error();
+ val = Long_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 8;
+ b2 = 0xFF & val;
+#else
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ Byte_u(str, idx) = b1;
+ Byte_u(str, idx + 1) = b2;
+ return Val_unit;
+}
+
+CAMLprim value caml_bytes_set32(value str, value index, value newval)
+{
+ unsigned char b1, b2, b3, b4;
+ intnat val;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 3 >= caml_string_length(str)) caml_array_bound_error();
+ val = Int32_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 24;
+ b2 = 0xFF & val >> 16;
+ b3 = 0xFF & val >> 8;
+ b4 = 0xFF & val;
+#else
+ b4 = 0xFF & val >> 24;
+ b3 = 0xFF & val >> 16;
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ Byte_u(str, idx) = b1;
+ Byte_u(str, idx + 1) = b2;
+ Byte_u(str, idx + 2) = b3;
+ Byte_u(str, idx + 3) = b4;
+ return Val_unit;
+}
+
+CAMLprim value caml_bytes_set64(value str, value index, value newval)
+{
+ unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
+ int64_t val;
+ intnat idx = Long_val(index);
+ if (idx < 0 || idx + 7 >= caml_string_length(str)) caml_array_bound_error();
+ val = Int64_val(newval);
+#ifdef ARCH_BIG_ENDIAN
+ b1 = 0xFF & val >> 56;
+ b2 = 0xFF & val >> 48;
+ b3 = 0xFF & val >> 40;
+ b4 = 0xFF & val >> 32;
+ b5 = 0xFF & val >> 24;
+ b6 = 0xFF & val >> 16;
+ b7 = 0xFF & val >> 8;
+ b8 = 0xFF & val;
+#else
+ b8 = 0xFF & val >> 56;
+ b7 = 0xFF & val >> 48;
+ b6 = 0xFF & val >> 40;
+ b5 = 0xFF & val >> 32;
+ b4 = 0xFF & val >> 24;
+ b3 = 0xFF & val >> 16;
+ b2 = 0xFF & val >> 8;
+ b1 = 0xFF & val;
+#endif
+ Byte_u(str, idx) = b1;
+ Byte_u(str, idx + 1) = b2;
+ Byte_u(str, idx + 2) = b3;
+ Byte_u(str, idx + 3) = b4;
+ Byte_u(str, idx + 4) = b5;
+ Byte_u(str, idx + 5) = b6;
+ Byte_u(str, idx + 6) = b7;
+ Byte_u(str, idx + 7) = b8;
+ return Val_unit;
+}
+
+CAMLprim value caml_string_equal(value s1, value s2)
+{
+ mlsize_t sz1, sz2;
+ value * p1, * p2;
+
+ if (s1 == s2) return Val_true;
+ sz1 = Wosize_val(s1);
+ sz2 = Wosize_val(s2);
+ if (sz1 != sz2) return Val_false;
+ for(p1 = Op_val(s1), p2 = Op_val(s2); sz1 > 0; sz1--, p1++, p2++)
+ if (*p1 != *p2) return Val_false;
+ return Val_true;
+}
+
+CAMLprim value caml_bytes_equal(value s1, value s2)
+{
+ return caml_string_equal(s1,s2);
+}
+
+CAMLprim value caml_string_notequal(value s1, value s2)
+{
+ return Val_not(caml_string_equal(s1, s2));
+}
+
+CAMLprim value caml_bytes_notequal(value s1, value s2)
+{
+ return caml_string_notequal(s1,s2);
+}
+
+CAMLprim value caml_string_compare(value s1, value s2)
+{
+ mlsize_t len1, len2;
+ int res;
+
+ if (s1 == s2) return Val_int(0);
+ len1 = caml_string_length(s1);
+ len2 = caml_string_length(s2);
+ res = memcmp(String_val(s1), String_val(s2), len1 <= len2 ? len1 : len2);
+ if (res < 0) return Val_int(-1);
+ if (res > 0) return Val_int(1);
+ if (len1 < len2) return Val_int(-1);
+ if (len1 > len2) return Val_int(1);
+ return Val_int(0);
+}
+
+CAMLprim value caml_bytes_compare(value s1, value s2)
+{
+ return caml_string_compare(s1,s2);
+}
+
+CAMLprim value caml_string_lessthan(value s1, value s2)
+{
+ return caml_string_compare(s1, s2) < Val_int(0) ? Val_true : Val_false;
+}
+
+CAMLprim value caml_bytes_lessthan(value s1, value s2)
+{
+ return caml_string_lessthan(s1,s2);
+}
+
+
+CAMLprim value caml_string_lessequal(value s1, value s2)
+{
+ return caml_string_compare(s1, s2) <= Val_int(0) ? Val_true : Val_false;
+}
+
+CAMLprim value caml_bytes_lessequal(value s1, value s2)
+{
+ return caml_string_lessequal(s1,s2);
+}
+
+
+CAMLprim value caml_string_greaterthan(value s1, value s2)
+{
+ return caml_string_compare(s1, s2) > Val_int(0) ? Val_true : Val_false;
+}
+
+CAMLprim value caml_bytes_greaterthan(value s1, value s2)
+{
+ return caml_string_greaterthan(s1,s2);
+}
+
+CAMLprim value caml_string_greaterequal(value s1, value s2)
+{
+ return caml_string_compare(s1, s2) >= Val_int(0) ? Val_true : Val_false;
+}
+
+CAMLprim value caml_bytes_greaterequal(value s1, value s2)
+{
+ return caml_string_greaterequal(s1,s2);
+}
+
+CAMLprim value caml_blit_bytes(value s1, value ofs1, value s2, value ofs2,
+ value n)
+{
+ memmove(&Byte(s2, Long_val(ofs2)), &Byte(s1, Long_val(ofs1)), Long_val(n));
+ return Val_unit;
+}
+
+CAMLprim value caml_blit_string(value s1, value ofs1, value s2, value ofs2,
+ value n)
+{
+ return caml_blit_bytes (s1, ofs1, s2, ofs2, n);
+}
+
+CAMLprim value caml_fill_bytes(value s, value offset, value len, value init)
+{
+ memset(&Byte(s, Long_val(offset)), Int_val(init), Long_val(len));
+ return Val_unit;
+}
+
+/**
+ * [caml_fill_string] is deprecated, use [caml_fill_bytes] instead
+ */
+CAMLprim value caml_fill_string(value s, value offset, value len, value init)
+{
+ return caml_fill_bytes (s, offset, len, init);
+}
+
+CAMLexport value caml_alloc_sprintf(const char * format, ...)
+{
+ va_list args;
+ char buf[128];
+ int n;
+ value res;
+
+#if !defined(_WIN32) || defined(_UCRT)
+ /* C99-compliant implementation */
+ va_start(args, format);
+ /* "vsnprintf(dest, sz, format, args)" writes at most "sz" characters
+ into "dest", including the terminating '\0'.
+ It returns the number of characters of the formatted string,
+ excluding the terminating '\0'. */
+ n = vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ if (n < sizeof(buf)) {
+ /* All output characters were written to buf, including the
+ terminating '\0'. Allocate a Caml string with length "n"
+ as computed by vsnprintf, and copy the output of vsnprintf into it. */
+ res = caml_alloc_initialized_string(n, buf);
+ } else {
+ /* PR#7568: if the format is in the Caml heap, the following
+ caml_alloc_string could move or free the format. To prevent
+ this, take a copy of the format outside the Caml heap. */
+ char * saved_format = caml_stat_strdup(format);
+ /* Allocate a Caml string with length "n" as computed by vsnprintf. */
+ res = caml_alloc_string(n);
+ /* Re-do the formatting, outputting directly in the Caml string.
+ Note that caml_alloc_string left room for a '\0' at position n,
+ so the size passed to vsnprintf is n+1. */
+ va_start(args, format);
+ vsnprintf((char *)String_val(res), n + 1, saved_format, args);
+ va_end(args);
+ caml_stat_free(saved_format);
+ }
+ return res;
+#else
+ /* Implementation specific to the Microsoft CRT library */
+ va_start(args, format);
+ /* "_vsnprintf(dest, sz, format, args)" writes at most "sz" characters
+ into "dest". Let "len" be the number of characters of the formatted
+ string.
+ If "len" < "sz", a null terminator was appended, and "len" is returned.
+ If "len" == "sz", no null termination, and "len" is returned.
+ If "len" > "sz", a negative value is returned. */
+ n = _vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ if (n >= 0 && n <= sizeof(buf)) {
+ /* All output characters were written to buf.
+ "n" is the actual length of the output.
+ Allocate a Caml string of length "n" and copy the characters into it. */
+ res = caml_alloc_string(n);
+ memcpy(String_val(res), buf, n);
+ } else {
+ /* PR#7568: if the format is in the Caml heap, the following
+ caml_alloc_string could move or free the format. To prevent
+ this, take a copy of the format outside the Caml heap. */
+ char * saved_format = caml_stat_strdup(format);
+ /* Determine actual length of output, excluding final '\0' */
+ va_start(args, format);
+ n = _vscprintf(format, args);
+ va_end(args);
+ res = caml_alloc_string(n);
+ /* Re-do the formatting, outputting directly in the Caml string.
+ Note that caml_alloc_string left room for a '\0' at position n,
+ so the size passed to _vsnprintf is n+1. */
+ va_start(args, format);
+ _vsnprintf(String_val(res), n + 1, saved_format, args);
+ va_end(args);
+ caml_stat_free(saved_format);
+ }
+ return res;
+#endif
+}
+
+CAMLprim value caml_string_of_bytes(value bv)
+{
+ return bv;
+}
+
+CAMLprim value caml_bytes_of_string(value bv)
+{
+ return bv;
+}
diff --git a/test/monniaux/ocaml/byterun/sys.c b/test/monniaux/ocaml/byterun/sys.c
new file mode 100644
index 00000000..84c42ddf
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/sys.c
@@ -0,0 +1,723 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* DM HORRIBLE */
+unsigned __bsp_frequency=500000000;
+
+#define CAML_INTERNALS
+
+/* Basic system calls */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef _WIN32
+#include <direct.h> /* for _wchdir and _wgetcwd */
+#else
+#include <sys/wait.h>
+#endif
+#include "caml/config.h"
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#ifdef HAS_TIMES
+#include <sys/times.h>
+#endif
+#ifdef HAS_GETRUSAGE
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+#ifdef HAS_GETTIMEOFDAY
+#include <sys/time.h>
+#endif
+#include "caml/alloc.h"
+#include "caml/debugger.h"
+#include "caml/fail.h"
+#include "caml/gc_ctrl.h"
+#include "caml/io.h"
+#include "caml/misc.h"
+#include "caml/mlvalues.h"
+#include "caml/osdeps.h"
+#include "caml/signals.h"
+#include "caml/stacks.h"
+#include "caml/sys.h"
+#include "caml/version.h"
+#include "caml/callback.h"
+#include "caml/startup_aux.h"
+
+static char * error_message(void)
+{
+ return strerror(errno);
+}
+
+#ifndef EAGAIN
+#define EAGAIN (-1)
+#endif
+#ifndef EWOULDBLOCK
+#define EWOULDBLOCK (-1)
+#endif
+
+CAMLexport void caml_sys_error(value arg)
+{
+ CAMLparam1 (arg);
+ char * err;
+ CAMLlocal1 (str);
+
+ err = error_message();
+ if (arg == NO_ARG) {
+ str = caml_copy_string(err);
+ } else {
+ int err_len = strlen(err);
+ int arg_len = caml_string_length(arg);
+ str = caml_alloc_string(arg_len + 2 + err_len);
+ memmove(&Byte(str, 0), String_val(arg), arg_len);
+ memmove(&Byte(str, arg_len), ": ", 2);
+ memmove(&Byte(str, arg_len + 2), err, err_len);
+ }
+ caml_raise_sys_error(str);
+ CAMLnoreturn;
+}
+
+CAMLexport void caml_sys_io_error(value arg)
+{
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ caml_raise_sys_blocked_io();
+ } else {
+ caml_sys_error(arg);
+ }
+}
+
+/* Check that [name] can safely be used as a file path */
+
+static void caml_sys_check_path(value name)
+{
+ if (! caml_string_is_c_safe(name)) {
+ errno = ENOENT;
+ caml_sys_error(name);
+ }
+}
+
+CAMLprim value caml_sys_exit(value retcode_v)
+{
+ int retcode = Int_val(retcode_v);
+
+ if ((caml_verb_gc & 0x400) != 0) {
+ /* cf caml_gc_counters */
+ double minwords = caml_stat_minor_words
+ + (double) (caml_young_end - caml_young_ptr);
+ double prowords = caml_stat_promoted_words;
+ double majwords = caml_stat_major_words + (double) caml_allocated_words;
+ double allocated_words = minwords + majwords - prowords;
+ intnat mincoll = caml_stat_minor_collections;
+ intnat majcoll = caml_stat_major_collections;
+ intnat heap_words = caml_stat_heap_wsz;
+ intnat heap_chunks = caml_stat_heap_chunks;
+ intnat top_heap_words = caml_stat_top_heap_wsz;
+ intnat cpct = caml_stat_compactions;
+ caml_gc_message(0x400, "allocated_words: %.0f\n", allocated_words);
+ caml_gc_message(0x400, "minor_words: %.0f\n", minwords);
+ caml_gc_message(0x400, "promoted_words: %.0f\n", prowords);
+ caml_gc_message(0x400, "major_words: %.0f\n", majwords);
+ caml_gc_message(0x400, "minor_collections: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ mincoll);
+ caml_gc_message(0x400, "major_collections: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ majcoll);
+ caml_gc_message(0x400, "heap_words: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ heap_words);
+ caml_gc_message(0x400, "heap_chunks: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ heap_chunks);
+ caml_gc_message(0x400, "top_heap_words: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ top_heap_words);
+ caml_gc_message(0x400, "compactions: %"ARCH_INTNAT_PRINTF_FORMAT"d\n",
+ cpct);
+ }
+
+#ifndef NATIVE_CODE
+ caml_debugger(PROGRAM_EXIT);
+#endif
+ CAML_INSTR_ATEXIT ();
+ if (caml_cleanup_on_exit)
+ caml_shutdown();
+#ifdef _WIN32
+ caml_restore_win32_terminal();
+#endif
+ CAML_SYS_EXIT(retcode);
+ return Val_unit;
+}
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+#ifndef O_TEXT
+#define O_TEXT 0
+#endif
+#ifndef O_NONBLOCK
+#ifdef O_NDELAY
+#define O_NONBLOCK O_NDELAY
+#else
+#define O_NONBLOCK 0
+#endif
+#endif
+
+static int sys_open_flags[] = {
+ O_RDONLY, O_WRONLY, O_APPEND | O_WRONLY, O_CREAT, O_TRUNC, O_EXCL,
+ O_BINARY, O_TEXT, O_NONBLOCK
+};
+
+CAMLprim value caml_sys_open(value path, value vflags, value vperm)
+{
+ CAMLparam3(path, vflags, vperm);
+ int fd, flags, perm;
+ char_os * p;
+
+#if defined(O_CLOEXEC)
+ flags = O_CLOEXEC;
+#elif defined(_WIN32)
+ flags = _O_NOINHERIT;
+#else
+ flags = 0;
+#endif
+
+ caml_sys_check_path(path);
+ p = caml_stat_strdup_to_os(String_val(path));
+ flags |= caml_convert_flag_list(vflags, sys_open_flags);
+ perm = Int_val(vperm);
+ /* open on a named FIFO can block (PR#1533) */
+ caml_enter_blocking_section();
+ fd = CAML_SYS_OPEN(p, flags, perm);
+ /* fcntl on a fd can block (PR#5069)*/
+#if defined(F_SETFD) && defined(FD_CLOEXEC) && !defined(_WIN32) \
+ && !defined(O_CLOEXEC)
+ if (fd != -1)
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+ if (fd == -1) caml_sys_error(path);
+ CAMLreturn(Val_long(fd));
+}
+
+CAMLprim value caml_sys_close(value fd_v)
+{
+ int fd = Int_val(fd_v);
+ caml_enter_blocking_section();
+ CAML_SYS_CLOSE(fd);
+ caml_leave_blocking_section();
+ return Val_unit;
+}
+
+CAMLprim value caml_sys_file_exists(value name)
+{
+#ifdef _WIN32
+ struct _stati64 st;
+#else
+ struct stat st;
+#endif
+ char_os * p;
+ int ret;
+
+ if (! caml_string_is_c_safe(name)) return Val_false;
+ p = caml_stat_strdup_to_os(String_val(name));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_STAT(p, &st);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+
+ return Val_bool(ret == 0);
+}
+
+CAMLprim value caml_sys_is_directory(value name)
+{
+ CAMLparam1(name);
+#ifdef _WIN32
+ struct _stati64 st;
+#else
+ struct stat st;
+#endif
+ char_os * p;
+ int ret;
+
+ caml_sys_check_path(name);
+ p = caml_stat_strdup_to_os(String_val(name));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_STAT(p, &st);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+
+ if (ret == -1) caml_sys_error(name);
+#ifdef S_ISDIR
+ CAMLreturn(Val_bool(S_ISDIR(st.st_mode)));
+#else
+ CAMLreturn(Val_bool(st.st_mode & S_IFDIR));
+#endif
+}
+
+CAMLprim value caml_sys_remove(value name)
+{
+ CAMLparam1(name);
+ char_os * p;
+ int ret;
+ caml_sys_check_path(name);
+ p = caml_stat_strdup_to_os(String_val(name));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_UNLINK(p);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+ if (ret != 0) caml_sys_error(name);
+ CAMLreturn(Val_unit);
+}
+
+CAMLprim value caml_sys_rename(value oldname, value newname)
+{
+ char_os * p_old;
+ char_os * p_new;
+ int ret;
+ caml_sys_check_path(oldname);
+ caml_sys_check_path(newname);
+ p_old = caml_stat_strdup_to_os(String_val(oldname));
+ p_new = caml_stat_strdup_to_os(String_val(newname));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_RENAME(p_old, p_new);
+ caml_leave_blocking_section();
+ caml_stat_free(p_new);
+ caml_stat_free(p_old);
+ if (ret != 0)
+ caml_sys_error(NO_ARG);
+ return Val_unit;
+}
+
+CAMLprim value caml_sys_chdir(value dirname)
+{
+ CAMLparam1(dirname);
+ char_os * p;
+ int ret;
+ caml_sys_check_path(dirname);
+ p = caml_stat_strdup_to_os(String_val(dirname));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_CHDIR(p);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+ if (ret != 0) caml_sys_error(dirname);
+ CAMLreturn(Val_unit);
+}
+
+CAMLprim value caml_sys_getcwd(value unit)
+{
+ char_os buff[4096];
+ char_os * ret;
+#ifdef HAS_GETCWD
+ ret = getcwd_os(buff, sizeof(buff)/sizeof(*buff));
+#else
+ caml_invalid_argument("Sys.getcwd not implemented");
+#endif /* HAS_GETCWD */
+ if (ret == 0) caml_sys_error(NO_ARG);
+ return caml_copy_string_of_os(buff);
+}
+
+CAMLprim value caml_sys_unsafe_getenv(value var)
+{
+ char_os * res, * p;
+ value val;
+
+ if (! caml_string_is_c_safe(var)) caml_raise_not_found();
+ p = caml_stat_strdup_to_os(String_val(var));
+#ifdef _WIN32
+ res = caml_win32_getenv(p);
+#else
+ res = CAML_SYS_GETENV(p);
+#endif
+ caml_stat_free(p);
+ if (res == 0) caml_raise_not_found();
+ val = caml_copy_string_of_os(res);
+#ifdef _WIN32
+ caml_stat_free(res);
+#endif
+ return val;
+}
+
+CAMLprim value caml_sys_getenv(value var)
+{
+ char_os * res, * p;
+ value val;
+
+ if (! caml_string_is_c_safe(var)) caml_raise_not_found();
+ p = caml_stat_strdup_to_os(String_val(var));
+#ifdef _WIN32
+ res = caml_win32_getenv(p);
+#else
+ res = caml_secure_getenv(p);
+#endif
+ caml_stat_free(p);
+ if (res == 0) caml_raise_not_found();
+ val = caml_copy_string_of_os(res);
+#ifdef _WIN32
+ caml_stat_free(res);
+#endif
+ return val;
+}
+
+char_os * caml_exe_name;
+char_os ** caml_main_argv;
+
+CAMLprim value caml_sys_get_argv(value unit)
+{
+ CAMLparam0 (); /* unit is unused */
+ CAMLlocal3 (exe_name, argv, res);
+ exe_name = caml_copy_string_of_os(caml_exe_name);
+ argv = caml_alloc_array((void *)caml_copy_string_of_os, (char const **) caml_main_argv);
+ res = caml_alloc_small(2, 0);
+ Field(res, 0) = exe_name;
+ Field(res, 1) = argv;
+ CAMLreturn(res);
+}
+
+void caml_sys_init(char_os * exe_name, char_os **argv)
+{
+#ifdef _WIN32
+ /* Initialises the caml_win32_* globals on Windows with the version of
+ Windows which is running */
+ caml_probe_win32_version();
+#if WINDOWS_UNICODE
+ caml_setup_win32_terminal();
+#endif
+#endif
+#ifdef CAML_WITH_CPLUGINS
+ caml_cplugins_init(exe_name, argv);
+#endif
+ caml_exe_name = exe_name;
+ caml_main_argv = argv;
+}
+
+#ifdef _WIN32
+#define WIFEXITED(status) 1
+#define WEXITSTATUS(status) (status)
+#else
+#if !(defined(WIFEXITED) && defined(WEXITSTATUS))
+/* Assume old-style V7 status word */
+#define WIFEXITED(status) (((status) & 0xFF) == 0)
+#define WEXITSTATUS(status) (((status) >> 8) & 0xFF)
+#endif
+#endif
+
+CAMLprim value caml_sys_system_command(value command)
+{
+ CAMLparam1 (command);
+ int status, retcode;
+ char_os *buf;
+
+ if (! caml_string_is_c_safe (command)) {
+ errno = EINVAL;
+ caml_sys_error(command);
+ }
+ buf = caml_stat_strdup_to_os(String_val(command));
+ caml_enter_blocking_section ();
+ status = CAML_SYS_SYSTEM(buf);
+ caml_leave_blocking_section ();
+ caml_stat_free(buf);
+ if (status == -1) caml_sys_error(command);
+ if (WIFEXITED(status))
+ retcode = WEXITSTATUS(status);
+ else
+ retcode = 255;
+ CAMLreturn (Val_int(retcode));
+}
+
+double caml_sys_time_include_children_unboxed(value include_children)
+{
+#ifdef HAS_GETRUSAGE
+ struct rusage ru;
+ double acc = 0.;
+
+ getrusage (RUSAGE_SELF, &ru);
+ acc += ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6
+ + ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6;
+
+ if (Bool_val(include_children)) {
+ getrusage (RUSAGE_CHILDREN, &ru);
+ acc += ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6
+ + ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6;
+ }
+
+ return acc;
+#else
+ #ifdef HAS_TIMES
+ #ifndef CLK_TCK
+ #ifdef HZ
+ #define CLK_TCK HZ
+ #else
+ #define CLK_TCK 60
+ #endif
+ #endif
+ struct tms t;
+ clock_t acc = 0;
+ times(&t);
+ acc += t.tms_utime + t.tms_stime;
+ if (Bool_val(include_children)) {
+ acc += t.tms_cutime + t.tms_cstime;
+ }
+ return (double)acc / CLK_TCK;
+ #else
+ /* clock() is standard ANSI C. We have no way of getting
+ subprocess times in this branch. */
+ return (double)clock() / CLOCKS_PER_SEC;
+ #endif
+#endif
+}
+
+CAMLprim value caml_sys_time_include_children(value include_children)
+{
+ return caml_copy_double(caml_sys_time_include_children_unboxed(include_children));
+}
+
+double caml_sys_time_unboxed(value unit) {
+ return caml_sys_time_include_children_unboxed(Val_false);
+}
+
+CAMLprim value caml_sys_time(value unit)
+{
+ return caml_copy_double(caml_sys_time_unboxed(unit));
+}
+
+#ifdef _WIN32
+extern int caml_win32_random_seed (intnat data[16]);
+#endif
+
+CAMLprim value caml_sys_random_seed (value unit)
+{
+ intnat data[16];
+ int n, i;
+ value res;
+#ifdef _WIN32
+ n = caml_win32_random_seed(data);
+#else
+ int fd;
+ n = 0;
+ /* Try /dev/urandom first */
+ fd = open("/dev/urandom", O_RDONLY, 0);
+ if (fd != -1) {
+ unsigned char buffer[12];
+ int nread = read(fd, buffer, 12);
+ close(fd);
+ while (nread > 0) data[n++] = buffer[--nread];
+ }
+ /* If the read from /dev/urandom fully succeeded, we now have 96 bits
+ of good random data and can stop here. Otherwise, complement
+ whatever we got (probably nothing) with some not-very-random data. */
+ if (n < 12) {
+#ifdef HAS_GETTIMEOFDAY
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ data[n++] = tv.tv_usec;
+ data[n++] = tv.tv_sec;
+#else
+ data[n++] = time(NULL);
+#endif
+#ifdef HAS_UNISTD
+ data[n++] = getpid();
+#if HAS_PPID
+ data[n++] = getppid();
+#endif
+#endif
+ }
+#endif
+ /* Convert to an OCaml array of ints */
+ res = caml_alloc_small(n, 0);
+ for (i = 0; i < n; i++) Field(res, i) = Val_long(data[i]);
+ return res;
+}
+
+CAMLprim value caml_sys_const_big_endian(value unit)
+{
+#ifdef ARCH_BIG_ENDIAN
+ return Val_true;
+#else
+ return Val_false;
+#endif
+}
+
+/* returns a value that represents a number of bits */
+CAMLprim value caml_sys_const_word_size(value unit)
+{
+ return Val_long(8 * sizeof(value));
+}
+
+/* returns a value that represents a number of bits */
+CAMLprim value caml_sys_const_int_size(value unit)
+{
+ return Val_long(8 * sizeof(value) - 1) ;
+}
+
+/* returns a value that represents a number of words */
+CAMLprim value caml_sys_const_max_wosize(value unit)
+{
+ return Val_long(Max_wosize) ;
+}
+
+CAMLprim value caml_sys_const_ostype_unix(value unit)
+{
+ return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Unix"));
+}
+
+CAMLprim value caml_sys_const_ostype_win32(value unit)
+{
+ return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Win32"));
+}
+
+CAMLprim value caml_sys_const_ostype_cygwin(value unit)
+{
+ return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Cygwin"));
+}
+
+CAMLprim value caml_sys_const_backend_type(value unit)
+{
+ return Val_int(1); /* Bytecode backed */
+}
+CAMLprim value caml_sys_get_config(value unit)
+{
+ CAMLparam0 (); /* unit is unused */
+ CAMLlocal2 (result, ostype);
+
+ ostype = caml_copy_string(OCAML_OS_TYPE);
+ result = caml_alloc_small (3, 0);
+ Field(result, 0) = ostype;
+ Field(result, 1) = Val_long (8 * sizeof(value));
+#ifdef ARCH_BIG_ENDIAN
+ Field(result, 2) = Val_true;
+#else
+ Field(result, 2) = Val_false;
+#endif
+ CAMLreturn (result);
+}
+
+CAMLprim value caml_sys_read_directory(value path)
+{
+ CAMLparam1(path);
+ CAMLlocal1(result);
+ struct ext_table tbl;
+ char_os * p;
+ int ret;
+
+ caml_sys_check_path(path);
+ caml_ext_table_init(&tbl, 50);
+ p = caml_stat_strdup_to_os(String_val(path));
+ caml_enter_blocking_section();
+ ret = CAML_SYS_READ_DIRECTORY(p, &tbl);
+ caml_leave_blocking_section();
+ caml_stat_free(p);
+ if (ret == -1){
+ caml_ext_table_free(&tbl, 1);
+ caml_sys_error(path);
+ }
+ caml_ext_table_add(&tbl, NULL);
+ result = caml_copy_string_array((char const **) tbl.contents);
+ caml_ext_table_free(&tbl, 1);
+ CAMLreturn(result);
+}
+
+/* Return true if the value is a filedescriptor (int) that is
+ * (presumably) open on an interactive terminal */
+CAMLprim value caml_sys_isatty(value chan)
+{
+ int fd;
+ value ret;
+
+ fd = (Channel(chan))->fd;
+#ifdef _WIN32
+ ret = Val_bool(caml_win32_isatty(fd));
+#else
+ ret = Val_bool(isatty(fd));
+#endif
+
+ return ret;
+}
+
+/* Load dynamic plugins indicated in the CAML_CPLUGINS environment
+ variable. These plugins can be used to set currently existing
+ hooks, such as GC hooks and system calls tracing (see misc.h).
+ */
+
+#ifdef CAML_WITH_CPLUGINS
+
+value (*caml_cplugins_prim)(int,value,value,value) = NULL;
+
+#define DLL_EXECUTABLE 1
+#define DLL_NOT_GLOBAL 0
+
+static struct cplugin_context cplugin_context;
+
+void caml_load_plugin(char_os *plugin)
+{
+ void* dll_handle = NULL;
+ char* u8;
+
+ dll_handle = caml_dlopen(plugin, DLL_EXECUTABLE, DLL_NOT_GLOBAL);
+ if( dll_handle != NULL ){
+ void (* dll_init)(struct cplugin_context*) =
+ caml_dlsym(dll_handle, "caml_cplugin_init");
+ if( dll_init != NULL ){
+ cplugin_context.plugin=plugin;
+ dll_init(&cplugin_context);
+ } else {
+ caml_dlclose(dll_handle);
+ }
+ } else {
+ u8 = caml_stat_strdup_of_os(plugin);
+ fprintf(stderr, "Cannot load C plugin %s\nReason: %s\n",
+ u8, caml_dlerror());
+ caml_stat_free(u8);
+ }
+}
+
+void caml_cplugins_load(char_os *env_variable)
+{
+ char_os *plugins = caml_secure_getenv(env_variable);
+ if(plugins != NULL){
+ char_os* curs = plugins;
+ while(*curs != 0){
+ if(*curs == _T(',')){
+ if(curs > plugins){
+ *curs = 0;
+ caml_load_plugin(plugins);
+ }
+ plugins = curs+1;
+ }
+ curs++;
+ }
+ if(curs > plugins) caml_load_plugin(plugins);
+ }
+}
+
+void caml_cplugins_init(char_os * exe_name, char_os **argv)
+{
+ cplugin_context.api_version = CAML_CPLUGIN_CONTEXT_API;
+ cplugin_context.prims_bitmap = CAML_CPLUGINS_PRIMS_BITMAP;
+ cplugin_context.exe_name = exe_name;
+ cplugin_context.argv = argv;
+ cplugin_context.ocaml_version = OCAML_VERSION_STRING;
+ caml_cplugins_load(_T("CAML_CPLUGINS"));
+#ifdef NATIVE_CODE
+ caml_cplugins_load(_T("CAML_NATIVE_CPLUGINS"));
+#else
+ caml_cplugins_load(_T("CAML_BYTE_CPLUGINS"));
+#endif
+}
+
+#endif /* CAML_WITH_CPLUGINS */
diff --git a/test/monniaux/ocaml/byterun/toto b/test/monniaux/ocaml/byterun/toto
new file mode 100644
index 00000000..89897f38
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/toto
@@ -0,0 +1,13996 @@
+12429 execve("/opt/Kalray/usr/local/k1rdtools/bin/k1-cluster", ["k1-cluster", "--syscall=libstd_scalls.so", "--", "./ocamlrun", "/home/monniaux/work/caml/quickso"...], 0x7ffec7d0ab80 /* 71 vars */) = 0
+12429 brk(NULL) = 0x1ef3000
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 4096) = 46
+12429 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a90000
+12429 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../lib64", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/.", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/local/k1rdtools/bin/../../../devimage/toolchain_default/toolroot/usr/local/k1rdtools/lib64/./lao", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/tls/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/tls/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/tls/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/tls", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/haswell/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/haswell", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64/x86_64", 0x7ffeb1c260e0) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/opt/Kalray/usr/lib64", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
+12429 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=248338, ...}) = 0
+12429 mmap(NULL, 248338, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f4c78a53000
+12429 close(3) = 0
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/librt.so.1", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\"\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=31680, ...}) = 0
+12429 mmap(NULL, 2128864, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c78663000
+12429 mprotect(0x7f4c7866a000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c78869000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7f4c78869000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libdl.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\16\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=14560, ...}) = 0
+12429 mmap(NULL, 2109712, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c7845f000
+12429 mprotect(0x7f4c78462000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c78661000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f4c78661000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libmppa_components.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_components.so", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \212\f\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=11681992, ...}) = 0
+12429 mmap(NULL, 13816744, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c77731000
+12429 mprotect(0x7f4c78238000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c78437000, 122880, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xb06000) = 0x7f4c78437000
+12429 mmap(0x7f4c78455000, 37800, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c78455000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libxml2.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libxml2.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libxml2.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libxml2.so.2", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\331\2\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=1834232, ...}) = 0
+12429 mmap(NULL, 3934648, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c77370000
+12429 mprotect(0x7f4c77526000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c77726000, 40960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x7f4c77726000
+12429 mmap(0x7f4c77730000, 2488, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c77730000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libz.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libz.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libz.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\37\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=116960, ...}) = 0
+12429 mmap(NULL, 2212016, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c77153000
+12429 mprotect(0x7f4c7716f000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c7736e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b000) = 0x7f4c7736e000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360\303\10\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=1594832, ...}) = 0
+12429 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a51000
+12429 mmap(NULL, 3702816, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c76dca000
+12429 mprotect(0x7f4c76f43000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c77143000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x179000) = 0x7f4c77143000
+12429 mmap(0x7f4c7714f000, 12320, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c7714f000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\272\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=1700792, ...}) = 0
+12429 mmap(NULL, 3789144, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c76a2c000
+12429 mprotect(0x7f4c76bc9000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c76dc8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19c000) = 0x7f4c76dc8000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300*\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=96616, ...}) = 0
+12429 mmap(NULL, 2192432, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c76814000
+12429 mprotect(0x7f4c7682b000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c76a2a000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f4c76a2a000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000b\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=144976, ...}) = 0
+12429 mmap(NULL, 2221184, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c765f5000
+12429 mprotect(0x7f4c7660f000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c7680e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19000) = 0x7f4c7680e000
+12429 mmap(0x7f4c76810000, 13440, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c76810000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0
+12429 mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c76204000
+12429 mprotect(0x7f4c763eb000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c765eb000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f4c765eb000
+12429 mmap(0x7f4c765f1000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c765f1000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libicuuc.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libicuuc.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libicuuc.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libicuuc.so.60", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\v\6\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=1792008, ...}) = 0
+12429 mmap(NULL, 3894144, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c75e4d000
+12429 mprotect(0x7f4c75ff0000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c761ef000, 81920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a2000) = 0x7f4c761ef000
+12429 mmap(0x7f4c76203000, 2944, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c76203000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/liblzma.so.5", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/liblzma.so.5", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/liblzma.so.5", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/liblzma.so.5", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340(\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=153984, ...}) = 0
+12429 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a4f000
+12429 mmap(NULL, 2248968, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c75c27000
+12429 mprotect(0x7f4c75c4b000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c75e4b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x24000) = 0x7f4c75e4b000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libicudata.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libicudata.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libicudata.so.60", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libicudata.so.60", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\4\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0644, st_size=26904264, ...}) = 0
+12429 mmap(NULL, 28999696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c7407e000
+12429 mprotect(0x7f4c75a26000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c75c25000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19a7000) = 0x7f4c75c25000
+12429 close(3) = 0
+12429 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a4d000
+12429 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a4b000
+12429 arch_prctl(ARCH_SET_FS, 0x7f4c78a4e6c0) = 0
+12429 mprotect(0x7f4c765eb000, 16384, PROT_READ) = 0
+12429 mprotect(0x7f4c75c25000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c78661000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c7680e000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c75e4b000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c76dc8000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c76a2a000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c77143000, 40960, PROT_READ) = 0
+12429 mprotect(0x7f4c761ef000, 77824, PROT_READ) = 0
+12429 mprotect(0x7f4c7736e000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c77726000, 32768, PROT_READ) = 0
+12429 mprotect(0x7f4c78869000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c78437000, 94208, PROT_READ) = 0
+12429 mprotect(0x602000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c78a92000, 4096, PROT_READ) = 0
+12429 munmap(0x7f4c78a53000, 248338) = 0
+12429 set_tid_address(0x7f4c78a4e990) = 12429
+12429 set_robust_list(0x7f4c78a4e9a0, 24) = 0
+12429 rt_sigaction(SIGRTMIN, {sa_handler=0x7f4c765facb0, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f4c76607890}, NULL, 8) = 0
+12429 rt_sigaction(SIGRT_1, {sa_handler=0x7f4c765fad50, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f4c76607890}, NULL, 8) = 0
+12429 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
+12429 prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
+12429 brk(NULL) = 0x1ef3000
+12429 brk(0x1f14000) = 0x1f14000
+12429 futex(0x7f4c7715007c, FUTEX_WAKE_PRIVATE, 2147483647) = 0
+12429 futex(0x7f4c77150088, FUTEX_WAKE_PRIVATE, 2147483647) = 0
+12429 prctl(PR_SET_NAME, "main") = 0
+12429 rt_sigprocmask(SIG_BLOCK, ~[TERM TSTP RTMIN RT_1], NULL, 8) = 0
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f4c7387d000
+12429 mprotect(0x7f4c7387e000, 8388608, PROT_READ|PROT_WRITE) = 0
+12429 clone(child_stack=0x7f4c7407cfb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4c7407d9d0, tls=0x7f4c7407d700, child_tidptr=0x7f4c7407d9d0) = 12430
+12430 set_robust_list(0x7f4c7407d9e0, 24) = 0
+12429 readlink("/proc/self/exe", <unfinished ...>
+12430 prctl(PR_SET_NAME, "sigwait" <unfinished ...>
+12429 <... readlink resumed> "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12430 <... prctl resumed> ) = 0
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_common.so", F_OK <unfinished ...>
+12430 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12429 <... access resumed> ) = -1 ENOENT (No such file or directory)
+12430 <... mmap resumed> ) = 0x7f4c6b87d000
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_common.so", F_OK <unfinished ...>
+12430 munmap(0x7f4c6b87d000, 7876608 <unfinished ...>
+12429 <... access resumed> ) = -1 ENOENT (No such file or directory)
+12430 <... munmap resumed> ) = 0
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_common.so", F_OK <unfinished ...>
+12430 munmap(0x7f4c70000000, 59232256 <unfinished ...>
+12429 <... access resumed> ) = -1 ENOENT (No such file or directory)
+12430 <... munmap resumed> ) = 0
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_common.so", F_OK <unfinished ...>
+12430 mprotect(0x7f4c6c000000, 135168, PROT_READ|PROT_WRITE <unfinished ...>
+12429 <... access resumed> ) = 0
+12430 <... mprotect resumed> ) = 0
+12429 futex(0x7f4c786620c8, FUTEX_WAKE_PRIVATE, 2147483647 <unfinished ...>
+12430 rt_sigtimedwait([INT USR1 ALRM], <unfinished ...>
+12429 <... futex resumed> ) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_common.so", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20p\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=185512, ...}) = 0
+12429 mmap(NULL, 2318056, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c73647000
+12429 mprotect(0x7f4c73672000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c73872000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2b000) = 0x7f4c73872000
+12429 mmap(0x7f4c73874000, 36584, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c73874000
+12429 close(3) = 0
+12429 mprotect(0x7f4c73872000, 4096, PROT_READ) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 g\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=276856, ...}) = 0
+12429 mmap(NULL, 2293640, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c73417000
+12429 mprotect(0x7f4c73444000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c73644000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2d000) = 0x7f4c73644000
+12429 mmap(0x7f4c73646000, 3976, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c73646000
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "BEFORE/tls/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/tls/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/tls/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/tls/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "BEFORE/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/tls", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/multiloader/k1_build/k1_Release_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/tls", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/./work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/kalray_internal/lib64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/tls", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/haswell/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/haswell/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/haswell/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/haswell", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/x86_64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/x86_64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 stat("/work1/hudson/workspace/_RDTools_integration_2/label/centos7-64/rdtools/devimage/toolchain_default/k1-multiloader_host/devimage/lib64", 0x7ffeb1c24410) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libzip.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libzip.so.2", O_RDONLY|O_CLOEXEC) = 3
+12429 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220(\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=70136, ...}) = 0
+12429 mmap(NULL, 2163424, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4c73206000
+12429 mprotect(0x7f4c73216000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c73415000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xf000) = 0x7f4c73415000
+12429 close(3) = 0
+12429 mprotect(0x7f4c73415000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c73644000, 4096, PROT_READ) = 0
+12429 openat(AT_FDCWD, "/etc/kalray", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
+12429 fstat(3, {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
+12429 getdents(3, /* 3 entries */, 32768) = 80
+12429 brk(0x1f35000) = 0x1f35000
+12429 openat(AT_FDCWD, "/etc/kalray/license.xml", O_RDONLY) = 4
+12429 futex(0x7f4c777306a8, FUTEX_WAKE_PRIVATE, 2147483647) = 0
+12429 read(4, "<?xm", 4) = 4
+12429 read(4, "l version=\"1.0\" encoding=\"UTF-8\""..., 4096) = 613
+12429 read(4, "", 4096) = 0
+12429 openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = 5
+12429 fstat(5, {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 fstat(5, {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 read(5, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0\r\0\0\0\0"..., 4096) = 2971
+12429 lseek(5, -1872, SEEK_CUR) = 1099
+12429 read(5, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0\r\0\0\0\0"..., 4096) = 1872
+12429 close(5) = 0
+12429 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2971, ...}) = 0
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/etc/hostid", O_RDONLY) = -1 ENOENT (No such file or directory)
+12429 uname({sysname="Linux", nodename="vanoise", ...}) = 0
+12429 stat("/etc/resolv.conf", {st_mode=S_IFREG|0644, st_size=80, ...}) = 0
+12429 openat(AT_FDCWD, "/etc/host.conf", O_RDONLY|O_CLOEXEC) = 4
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=92, ...}) = 0
+12429 read(4, "# The \"order\" line is only used "..., 4096) = 92
+12429 read(4, "", 4096) = 0
+12429 close(4) = 0
+12429 futex(0x7f4c765f3ba4, FUTEX_WAKE_PRIVATE, 2147483647) = 0
+12429 openat(AT_FDCWD, "/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 4
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=80, ...}) = 0
+12429 read(4, "# Generated by NetworkManager\nna"..., 4096) = 80
+12429 read(4, "", 4096) = 0
+12429 close(4) = 0
+12429 uname({sysname="Linux", nodename="vanoise", ...}) = 0
+12429 socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 4
+12429 connect(4, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
+12429 close(4) = 0
+12429 socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 4
+12429 connect(4, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/etc/nsswitch.conf", O_RDONLY|O_CLOEXEC) = 4
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=556, ...}) = 0
+12429 read(4, "# /etc/nsswitch.conf\n#\n# Example"..., 4096) = 556
+12429 read(4, "", 4096) = 0
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/lib64/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 4
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=248338, ...}) = 0
+12429 mmap(NULL, 248338, PROT_READ, MAP_PRIVATE, 4, 0) = 0x7f4c78a53000
+12429 close(4) = 0
+12429 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P#\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=47568, ...}) = 0
+12429 mmap(NULL, 2168632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c72ff4000
+12429 mprotect(0x7f4c72fff000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c731fe000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0xa000) = 0x7f4c731fe000
+12429 mmap(0x7f4c73200000, 22328, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c73200000
+12429 close(4) = 0
+12429 mprotect(0x7f4c731fe000, 4096, PROT_READ) = 0
+12429 munmap(0x7f4c78a53000, 248338) = 0
+12429 openat(AT_FDCWD, "/etc/hosts", O_RDONLY|O_CLOEXEC) = 4
+12429 fstat(4, {st_mode=S_IFREG|0644, st_size=369, ...}) = 0
+12429 read(4, "127.0.0.1\tlocalhost\n127.0.1.1\tva"..., 4096) = 369
+12429 read(4, "", 4096) = 0
+12429 close(4) = 0
+12429 getdents(3, /* 0 entries */, 32768) = 0
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "/home/monniaux/.kalray", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 ENOENT (No such file or directory)
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/mppa_config.yaml", O_RDONLY) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib/mppa_config.yaml", O_RDONLY) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib32/mppa_config.yaml", O_RDONLY) = -1 ENOENT (No such file or directory)
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/mppa_config.yaml", O_RDONLY) = 3
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=616729, ...}) = 0
+12429 read(3, "# {{{ MPPA:\n- MPPA:\n\n # MPPA "..., 16384) = 16384
+12429 brk(0x1f56000) = 0x1f56000
+12429 read(3, "0\n\n # PRO"..., 16384) = 16384
+12429 mmap(NULL, 200704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c78a5f000
+12429 read(3, " Latency_ECC: 4\n"..., 16384) = 16384
+12429 read(3, " # PROC7 sub-configurati"..., 16384) = 16384
+12429 mremap(0x7f4c78a5f000, 200704, 397312, MREMAP_MAYMOVE) = 0x7f4c789ea000
+12429 read(3, "atency_ECC: 4\n "..., 16384) = 16384
+12429 read(3, " # PROC12 sub-conf"..., 16384) = 16384
+12429 brk(0x1f77000) = 0x1f77000
+12429 read(3, " Late"..., 16384) = 16384
+12429 read(3, " # BANK par"..., 16384) = 16384
+12429 mremap(0x7f4c789ea000, 397312, 790528, MREMAP_MAYMOVE) = 0x7f4c78929000
+12429 read(3, " TYPE: generic_cache\n "..., 16384) = 16384
+12429 brk(0x1f98000) = 0x1f98000
+12429 read(3, " { Debug: Disable, "..., 16384) = 16384
+12429 read(3, "eneric_cache\n "..., 16384) = 16384
+12429 read(3, "bug: Disable, Warning: Enable, L"..., 16384) = 16384
+12429 brk(0x1fb9000) = 0x1fb9000
+12429 read(3, "he\n N"..., 16384) = 16384
+12429 read(3, " { Debug: Disable,"..., 16384) = 16384
+12429 read(3, " TYPE: generic_cac"..., 16384) = 16384
+12429 mremap(0x7f4c78929000, 790528, 1576960, MREMAP_MAYMOVE) = 0x7f4c72e73000
+12429 read(3, " { NA"..., 16384) = 16384
+12429 brk(0x1fda000) = 0x1fda000
+12429 read(3, " DEBUG:\n "..., 16384) = 16384
+12429 read(3, " Latency_MultiBank: 1\n "..., 16384) = 16384
+12429 read(3, " # DEBUG param"..., 16384) = 16384
+12429 brk(0x1ffb000) = 0x1ffb000
+12429 read(3, "Bank: 1\n "..., 16384) = 16384
+12429 read(3, " # DEBU"..., 16384) = 16384
+12429 read(3, " Latency_MultiBan"..., 16384) = 16384
+12429 brk(0x201c000) = 0x201c000
+12429 read(3, "ing_Scheme: Sequential\n "..., 16384) = 16384
+12429 read(3, " CacheSize:"..., 16384) = 16384
+12429 read(3, "}}} DEBUG:\n "..., 16384) = 16384
+12429 read(3, " CacheSize: 16384\n "..., 16384) = 16384
+12429 brk(0x203d000) = 0x203d000
+12429 read(3, " # {{{ C"..., 16384) = 16384
+12429 read(3, "Size: 16384\n "..., 16384) = 16384
+12429 read(3, " }}} DEBUG:\n "..., 16384) = 16384
+12429 brk(0x205e000) = 0x205e000
+12429 read(3, " Cache"..., 16384) = 16384
+12429 mremap(0x7f4c72e73000, 1576960, 3149824, MREMAP_MAYMOVE) = 0x7f4c72b72000
+12429 read(3, " # {{{ DEBUG:\n "..., 16384) = 16384
+12429 read(3, "nable, Level: 0, IOStream: stder"..., 16384) = 16384
+12429 brk(0x207f000) = 0x207f000
+12429 read(3, " DEBUG:\n "..., 16384) = 16384
+12429 read(3, "OStream: stderr }\n "..., 16384) = 16384
+12429 read(3, " DEBUG:\n "..., 16384) = 16384
+12429 brk(0x20a0000) = 0x20a0000
+12429 read(3, "l: 0, IOStream: stderr }\n "..., 16384) = 16384
+12429 read(3, "EBUG:\n "..., 16384) = 16384
+12429 read(3, "ng_Parameter: none\n "..., 16384) = 10521
+12429 read(3, "", 4096) = 0
+12429 read(3, "", 16384) = 0
+12429 brk(0x20c1000) = 0x20c1000
+12429 brk(0x20e2000) = 0x20e2000
+12429 brk(0x2103000) = 0x2103000
+12429 brk(0x2124000) = 0x2124000
+12429 brk(0x2145000) = 0x2145000
+12429 brk(0x2166000) = 0x2166000
+12429 brk(0x2187000) = 0x2187000
+12429 brk(0x21a8000) = 0x21a8000
+12429 brk(0x21c9000) = 0x21c9000
+12429 brk(0x21ea000) = 0x21ea000
+12429 brk(0x220b000) = 0x220b000
+12429 brk(0x222c000) = 0x222c000
+12429 brk(0x224d000) = 0x224d000
+12429 brk(0x226e000) = 0x226e000
+12429 brk(0x228f000) = 0x228f000
+12429 brk(0x22b0000) = 0x22b0000
+12429 brk(0x22d1000) = 0x22d1000
+12429 brk(0x22f2000) = 0x22f2000
+12429 brk(0x2313000) = 0x2313000
+12429 brk(0x2334000) = 0x2334000
+12429 munmap(0x7f4c72b72000, 3149824) = 0
+12429 close(3) = 0
+12429 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
+12429 access("./ocamlrun", F_OK) = 0
+12429 stat("./ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 openat(AT_FDCWD, "./ocamlrun", O_RDONLY) = 3
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 lseek(3, 2289664, SEEK_SET) = 2289664
+12429 read(3, "\3\0\0\0\0\0\0\0\0p\10\0\0\0\0\0\350@\10\0\0\0\0\0\0\200\2\0\0\0\0\0"..., 2104) = 2104
+12429 fstat(3, {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 lseek(3, 2224128, SEEK_SET) = 2224128
+12429 read(3, "\360\235\6\0\0\0\0\0`\0\0\0\0\0\0\0\24\17\0\0\4\0\361\377\0\0\0\0\0\0\0\0"..., 4096) = 4096
+12429 read(3, "F(\0\0\22\0\6\0\350Q\5\0\0\0\0\0D\1\0\0\0\0\0\0\263\31\0\0\22\0\6\0"..., 61440) = 61440
+12429 read(3, "\3\0\0\0\0\0\0\0\0p\10\0\0\0\0\0\350@\10\0\0\0\0\0\0\200\2\0\0\0\0\0"..., 4096) = 2104
+12429 close(3) = 0
+12429 openat(AT_FDCWD, "./ocamlrun", O_RDONLY) = 3
+12429 lseek(3, 0, SEEK_END) = 2291768
+12429 mmap(NULL, 2291768, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = 0x7f4c72dc4000
+12429 munmap(0x7f4c72dc4000, 2291768) = 0
+12429 close(3) = 0
+12429 getpid() = 12429
+12429 getpid() = 12429
+12429 unlink("/tmp/mppa-tlm-input-12429") = -1 ENOENT (No such file or directory)
+12429 unlink("/tmp/mppa-tlm-output-12429") = -1 ENOENT (No such file or directory)
+12429 mknod("/tmp/mppa-tlm-input-12429", S_IFIFO|0666) = 0
+12429 mknod("/tmp/mppa-tlm-output-12429", S_IFIFO|0666) = 0
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f4c727f3000
+12429 mprotect(0x7f4c727f4000, 8388608, PROT_READ|PROT_WRITE) = 0
+12429 clone(child_stack=0x7f4c72ff2fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4c72ff39d0, tls=0x7f4c72ff3700, child_tidptr=0x7f4c72ff39d0) = 12431
+12431 set_robust_list(0x7f4c72ff39e0, 24) = 0
+12431 prctl(PR_SET_NAME, "COM") = 0
+12431 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f4c64000000
+12431 munmap(0x7f4c68000000, 67108864) = 0
+12431 mprotect(0x7f4c64000000, 135168, PROT_READ|PROT_WRITE) = 0
+12431 openat(AT_FDCWD, "/tmp/mppa-tlm-input-12429", O_RDONLY <unfinished ...>
+12429 brk(0x2355000) = 0x2355000
+12429 brk(0x2376000) = 0x2376000
+12429 brk(0x2397000) = 0x2397000
+12429 brk(0x23b8000) = 0x23b8000
+12429 brk(0x23d9000) = 0x23d9000
+12429 brk(0x23fa000) = 0x23fa000
+12429 brk(0x241b000) = 0x241b000
+12429 brk(0x243c000) = 0x243c000
+12429 brk(0x245d000) = 0x245d000
+12429 brk(0x247e000) = 0x247e000
+12429 brk(0x249f000) = 0x249f000
+12429 brk(0x24c0000) = 0x24c0000
+12429 brk(0x24e1000) = 0x24e1000
+12429 brk(0x2502000) = 0x2502000
+12429 brk(0x2523000) = 0x2523000
+12429 brk(0x2544000) = 0x2544000
+12429 brk(0x2565000) = 0x2565000
+12429 brk(0x2586000) = 0x2586000
+12429 brk(0x25a7000) = 0x25a7000
+12429 brk(0x25c8000) = 0x25c8000
+12429 brk(0x25e9000) = 0x25e9000
+12429 brk(0x260a000) = 0x260a000
+12429 brk(0x262b000) = 0x262b000
+12429 brk(0x264f000) = 0x264f000
+12429 brk(0x2670000) = 0x2670000
+12429 brk(0x2691000) = 0x2691000
+12429 brk(0x26b2000) = 0x26b2000
+12429 brk(0x26d3000) = 0x26d3000
+12429 brk(0x26f4000) = 0x26f4000
+12429 brk(0x2715000) = 0x2715000
+12429 brk(0x2736000) = 0x2736000
+12429 brk(0x2757000) = 0x2757000
+12429 brk(0x2778000) = 0x2778000
+12429 brk(0x2799000) = 0x2799000
+12429 brk(0x27ba000) = 0x27ba000
+12429 brk(0x27db000) = 0x27db000
+12429 brk(0x27fc000) = 0x27fc000
+12429 brk(0x281d000) = 0x281d000
+12429 brk(0x283e000) = 0x283e000
+12429 brk(0x285f000) = 0x285f000
+12429 brk(0x2880000) = 0x2880000
+12429 brk(0x28a1000) = 0x28a1000
+12429 brk(0x28c2000) = 0x28c2000
+12429 brk(0x28e3000) = 0x28e3000
+12429 brk(0x2904000) = 0x2904000
+12429 brk(0x2925000) = 0x2925000
+12429 brk(0x2946000) = 0x2946000
+12429 brk(0x2967000) = 0x2967000
+12429 brk(0x2988000) = 0x2988000
+12429 brk(0x29a9000) = 0x29a9000
+12429 brk(0x29ca000) = 0x29ca000
+12429 brk(0x29eb000) = 0x29eb000
+12429 brk(0x2a0c000) = 0x2a0c000
+12429 brk(0x2a2d000) = 0x2a2d000
+12429 brk(0x2a4e000) = 0x2a4e000
+12429 brk(0x2a6f000) = 0x2a6f000
+12429 brk(0x2a90000) = 0x2a90000
+12429 brk(0x2ab1000) = 0x2ab1000
+12429 brk(0x2ad2000) = 0x2ad2000
+12429 brk(0x2af3000) = 0x2af3000
+12429 brk(0x2b14000) = 0x2b14000
+12429 brk(0x2b35000) = 0x2b35000
+12429 brk(0x2b56000) = 0x2b56000
+12429 brk(0x2b77000) = 0x2b77000
+12429 brk(0x2b98000) = 0x2b98000
+12429 brk(0x2bb9000) = 0x2bb9000
+12429 brk(0x2bda000) = 0x2bda000
+12429 brk(0x2bfb000) = 0x2bfb000
+12429 brk(0x2c1c000) = 0x2c1c000
+12429 brk(0x2c3d000) = 0x2c3d000
+12429 brk(0x2c5e000) = 0x2c5e000
+12429 brk(0x2c7f000) = 0x2c7f000
+12429 brk(0x2ca0000) = 0x2ca0000
+12429 brk(0x2cc1000) = 0x2cc1000
+12429 brk(0x2ce2000) = 0x2ce2000
+12429 brk(0x2d03000) = 0x2d03000
+12429 brk(0x2d24000) = 0x2d24000
+12429 brk(0x2d45000) = 0x2d45000
+12429 brk(0x2d66000) = 0x2d66000
+12429 brk(0x2d87000) = 0x2d87000
+12429 brk(0x2da8000) = 0x2da8000
+12429 brk(0x2dce000) = 0x2dce000
+12429 brk(0x2def000) = 0x2def000
+12429 brk(0x2e10000) = 0x2e10000
+12429 brk(0x2e31000) = 0x2e31000
+12429 brk(0x2e52000) = 0x2e52000
+12429 brk(0x2e73000) = 0x2e73000
+12429 brk(0x2e94000) = 0x2e94000
+12429 brk(0x2eb5000) = 0x2eb5000
+12429 brk(0x2ed6000) = 0x2ed6000
+12429 brk(0x2ef7000) = 0x2ef7000
+12429 brk(0x2f18000) = 0x2f18000
+12429 brk(0x2f39000) = 0x2f39000
+12429 brk(0x2f5a000) = 0x2f5a000
+12429 brk(0x2f7b000) = 0x2f7b000
+12429 brk(0x2f9c000) = 0x2f9c000
+12429 brk(0x2fbd000) = 0x2fbd000
+12429 brk(0x2ffe000) = 0x2ffe000
+12429 brk(0x301f000) = 0x301f000
+12429 brk(0x3040000) = 0x3040000
+12429 brk(0x3061000) = 0x3061000
+12429 brk(0x3082000) = 0x3082000
+12429 brk(0x30a3000) = 0x30a3000
+12429 brk(0x30c4000) = 0x30c4000
+12429 brk(0x30e5000) = 0x30e5000
+12429 brk(0x3106000) = 0x3106000
+12429 brk(0x3127000) = 0x3127000
+12429 brk(0x3148000) = 0x3148000
+12429 brk(0x3169000) = 0x3169000
+12429 brk(0x318a000) = 0x318a000
+12429 brk(0x31ab000) = 0x31ab000
+12429 brk(0x31cc000) = 0x31cc000
+12429 brk(0x31ed000) = 0x31ed000
+12429 brk(0x320e000) = 0x320e000
+12429 brk(0x322f000) = 0x322f000
+12429 brk(0x3250000) = 0x3250000
+12429 brk(0x3271000) = 0x3271000
+12429 brk(0x3292000) = 0x3292000
+12429 brk(0x32b3000) = 0x32b3000
+12429 brk(0x32d4000) = 0x32d4000
+12429 brk(0x32f5000) = 0x32f5000
+12429 brk(0x3316000) = 0x3316000
+12429 brk(0x3337000) = 0x3337000
+12429 brk(0x3358000) = 0x3358000
+12429 brk(0x3379000) = 0x3379000
+12429 brk(0x339a000) = 0x339a000
+12429 brk(0x33bb000) = 0x33bb000
+12429 brk(0x33dc000) = 0x33dc000
+12429 brk(0x33fd000) = 0x33fd000
+12429 brk(0x341e000) = 0x341e000
+12429 brk(0x343f000) = 0x343f000
+12429 brk(0x3460000) = 0x3460000
+12429 brk(0x3481000) = 0x3481000
+12429 brk(0x34a2000) = 0x34a2000
+12429 brk(0x34c3000) = 0x34c3000
+12429 brk(0x34e4000) = 0x34e4000
+12429 brk(0x3505000) = 0x3505000
+12429 brk(0x3526000) = 0x3526000
+12429 brk(0x3547000) = 0x3547000
+12429 brk(0x3568000) = 0x3568000
+12429 brk(0x3589000) = 0x3589000
+12429 brk(0x35aa000) = 0x35aa000
+12429 brk(0x35cb000) = 0x35cb000
+12429 brk(0x35ec000) = 0x35ec000
+12429 brk(0x360d000) = 0x360d000
+12429 brk(0x362e000) = 0x362e000
+12429 mmap(NULL, 34607104, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c706f2000
+12429 mmap(NULL, 34607104, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c69eff000
+12429 mmap(NULL, 34607104, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c61eff000
+12429 mmap(NULL, 4294971392, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b61efe000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\214\2\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=1461464, ...}) = 0
+12429 mmap(NULL, 3556520, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c7038d000
+12429 mprotect(0x7f4c704e1000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c706e1000, 69632, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x154000) = 0x7f4c706e1000
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libAIR.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\234\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=135456, ...}) = 0
+12429 mmap(NULL, 2202280, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c70173000
+12429 mprotect(0x7f4c7018c000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c7038b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x18000) = 0x7f4c7038b000
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libMDT.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\353\r\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=2836832, ...}) = 0
+12429 mmap(NULL, 4885520, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c69a56000
+12429 mprotect(0x7f4c69c87000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c69e86000, 475136, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x230000) = 0x7f4c69e86000
+12429 mmap(0x7f4c69efa000, 19472, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f4c69efa000
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libCCL.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`4\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=65408, ...}) = 0
+12429 mmap(NULL, 2151080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c69848000
+12429 mprotect(0x7f4c69854000, 2097152, PROT_NONE) = 0
+12429 mmap(0x7f4c69a54000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0xc000) = 0x7f4c69a54000
+12429 close(4) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/lao/libBSL.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360x\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=178120, ...}) = 0
+12429 mmap(NULL, 2259944, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c69620000
+12429 mprotect(0x7f4c69646000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c69845000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x25000) = 0x7f4c69845000
+12429 close(4) = 0
+12429 mprotect(0x7f4c69845000, 8192, PROT_READ) = 0
+12429 mprotect(0x7f4c69a54000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c69e86000, 208896, PROT_READ) = 0
+12429 mprotect(0x7f4c7038b000, 4096, PROT_READ) = 0
+12429 mprotect(0x7f4c706e1000, 65536, PROT_READ) = 0
+12429 brk(0x364f000) = 0x364f000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 mmap(NULL, 6819840, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c68f9f000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 openat(AT_FDCWD, "/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\23\0\0\0\0\0\0"..., 832) = 832
+12429 fstat(4, {st_mode=S_IFREG|0755, st_size=19216, ...}) = 0
+12429 mmap(NULL, 2114360, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 4, 0) = 0x7f4c68d9a000
+12429 mprotect(0x7f4c68d9e000, 2093056, PROT_NONE) = 0
+12429 mmap(0x7f4c68f9d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 4, 0x3000) = 0x7f4c68f9d000
+12429 close(4) = 0
+12429 mprotect(0x7f4c68f9d000, 4096, PROT_READ) = 0
+12429 brk(0x3670000) = 0x3670000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 mmap(NULL, 6819840, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c68719000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 brk(0x3694000) = 0x3694000
+12429 munmap(0x7f4c68719000, 6819840) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x36b8000) = 0x36b8000
+12429 brk(0x3d39000) = 0x3d39000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x43cb000) = 0x43cb000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 mmap(NULL, 67112960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b5defd000
+12429 brk(0x444b000) = 0x444b000
+12429 brk(0x446c000) = 0x446c000
+12429 brk(0x448d000) = 0x448d000
+12429 brk(0x44ae000) = 0x44ae000
+12429 brk(0x44cf000) = 0x44cf000
+12429 brk(0x44f0000) = 0x44f0000
+12429 brk(0x4511000) = 0x4511000
+12429 brk(0x4532000) = 0x4532000
+12429 brk(0x4553000) = 0x4553000
+12429 brk(0x4574000) = 0x4574000
+12429 brk(0x4595000) = 0x4595000
+12429 brk(0x45b6000) = 0x45b6000
+12429 brk(0x45d7000) = 0x45d7000
+12429 brk(0x45f8000) = 0x45f8000
+12429 brk(0x4619000) = 0x4619000
+12429 brk(0x463a000) = 0x463a000
+12429 brk(0x465b000) = 0x465b000
+12429 brk(0x467c000) = 0x467c000
+12429 brk(0x469d000) = 0x469d000
+12429 brk(0x46be000) = 0x46be000
+12429 brk(0x46df000) = 0x46df000
+12429 brk(0x4700000) = 0x4700000
+12429 brk(0x4721000) = 0x4721000
+12429 brk(0x4742000) = 0x4742000
+12429 brk(0x4763000) = 0x4763000
+12429 brk(0x4784000) = 0x4784000
+12429 brk(0x47a5000) = 0x47a5000
+12429 brk(0x47c6000) = 0x47c6000
+12429 brk(0x47e7000) = 0x47e7000
+12429 brk(0x4808000) = 0x4808000
+12429 brk(0x4829000) = 0x4829000
+12429 brk(0x484a000) = 0x484a000
+12429 brk(0x486b000) = 0x486b000
+12429 brk(0x488c000) = 0x488c000
+12429 brk(0x48ad000) = 0x48ad000
+12429 brk(0x48ce000) = 0x48ce000
+12429 brk(0x48ef000) = 0x48ef000
+12429 brk(0x4910000) = 0x4910000
+12429 brk(0x4931000) = 0x4931000
+12429 brk(0x4952000) = 0x4952000
+12429 brk(0x4973000) = 0x4973000
+12429 brk(0x4994000) = 0x4994000
+12429 brk(0x49b5000) = 0x49b5000
+12429 brk(0x49d6000) = 0x49d6000
+12429 brk(0x49f7000) = 0x49f7000
+12429 brk(0x4a18000) = 0x4a18000
+12429 brk(0x4a39000) = 0x4a39000
+12429 brk(0x4a5a000) = 0x4a5a000
+12429 brk(0x4a7b000) = 0x4a7b000
+12429 brk(0x4a9c000) = 0x4a9c000
+12429 brk(0x4abd000) = 0x4abd000
+12429 brk(0x4ade000) = 0x4ade000
+12429 brk(0x4b0c000) = 0x4b0c000
+12429 brk(0x4b30000) = 0x4b30000
+12429 brk(0x4b51000) = 0x4b51000
+12429 brk(0x4b72000) = 0x4b72000
+12429 brk(0x4b93000) = 0x4b93000
+12429 brk(0x4bb4000) = 0x4bb4000
+12429 brk(0x4bd5000) = 0x4bd5000
+12429 brk(0x4bf6000) = 0x4bf6000
+12429 brk(0x4c17000) = 0x4c17000
+12429 brk(0x4c38000) = 0x4c38000
+12429 brk(0x4c59000) = 0x4c59000
+12429 brk(0x4c7a000) = 0x4c7a000
+12429 brk(0x4c9b000) = 0x4c9b000
+12429 brk(0x4cbc000) = 0x4cbc000
+12429 brk(0x4cdd000) = 0x4cdd000
+12429 brk(0x4cfe000) = 0x4cfe000
+12429 brk(0x4d1f000) = 0x4d1f000
+12429 brk(0x4d40000) = 0x4d40000
+12429 brk(0x4d61000) = 0x4d61000
+12429 brk(0x4d82000) = 0x4d82000
+12429 brk(0x4da3000) = 0x4da3000
+12429 brk(0x4dc4000) = 0x4dc4000
+12429 brk(0x4de5000) = 0x4de5000
+12429 brk(0x4e06000) = 0x4e06000
+12429 brk(0x4e27000) = 0x4e27000
+12429 brk(0x4e48000) = 0x4e48000
+12429 brk(0x4e69000) = 0x4e69000
+12429 brk(0x4e8a000) = 0x4e8a000
+12429 brk(0x4eab000) = 0x4eab000
+12429 brk(0x4ecc000) = 0x4ecc000
+12429 brk(0x4eed000) = 0x4eed000
+12429 brk(0x4f0e000) = 0x4f0e000
+12429 brk(0x4f2f000) = 0x4f2f000
+12429 brk(0x4f50000) = 0x4f50000
+12429 brk(0x4f71000) = 0x4f71000
+12429 brk(0x4f92000) = 0x4f92000
+12429 brk(0x4fb3000) = 0x4fb3000
+12429 brk(0x4fd4000) = 0x4fd4000
+12429 brk(0x4ff5000) = 0x4ff5000
+12429 brk(0x5016000) = 0x5016000
+12429 brk(0x5037000) = 0x5037000
+12429 brk(0x5058000) = 0x5058000
+12429 brk(0x5079000) = 0x5079000
+12429 brk(0x509a000) = 0x509a000
+12429 brk(0x50bb000) = 0x50bb000
+12429 brk(0x50dc000) = 0x50dc000
+12429 brk(0x50fd000) = 0x50fd000
+12429 brk(0x511e000) = 0x511e000
+12429 brk(0x513f000) = 0x513f000
+12429 brk(0x5160000) = 0x5160000
+12429 brk(0x5181000) = 0x5181000
+12429 brk(0x51a2000) = 0x51a2000
+12429 brk(0x51c3000) = 0x51c3000
+12429 brk(0x51e4000) = 0x51e4000
+12429 brk(0x5205000) = 0x5205000
+12429 brk(0x5226000) = 0x5226000
+12429 brk(0x5247000) = 0x5247000
+12429 brk(0x5268000) = 0x5268000
+12429 brk(0x5289000) = 0x5289000
+12429 brk(0x52aa000) = 0x52aa000
+12429 brk(0x52cb000) = 0x52cb000
+12429 brk(0x52ec000) = 0x52ec000
+12429 brk(0x530d000) = 0x530d000
+12429 brk(0x532e000) = 0x532e000
+12429 brk(0x534f000) = 0x534f000
+12429 brk(0x5370000) = 0x5370000
+12429 brk(0x5391000) = 0x5391000
+12429 brk(0x53b2000) = 0x53b2000
+12429 brk(0x53d3000) = 0x53d3000
+12429 brk(0x53f4000) = 0x53f4000
+12429 brk(0x5415000) = 0x5415000
+12429 brk(0x5436000) = 0x5436000
+12429 brk(0x5457000) = 0x5457000
+12429 brk(0x5478000) = 0x5478000
+12429 brk(0x5499000) = 0x5499000
+12429 brk(0x54ba000) = 0x54ba000
+12429 brk(0x54db000) = 0x54db000
+12429 brk(0x54fc000) = 0x54fc000
+12429 brk(0x551d000) = 0x551d000
+12429 brk(0x553e000) = 0x553e000
+12429 brk(0x555f000) = 0x555f000
+12429 brk(0x5580000) = 0x5580000
+12429 brk(0x55a1000) = 0x55a1000
+12429 brk(0x55c2000) = 0x55c2000
+12429 brk(0x55e3000) = 0x55e3000
+12429 brk(0x5604000) = 0x5604000
+12429 brk(0x5625000) = 0x5625000
+12429 brk(0x5646000) = 0x5646000
+12429 brk(0x5667000) = 0x5667000
+12429 brk(0x5688000) = 0x5688000
+12429 brk(0x56a9000) = 0x56a9000
+12429 brk(0x56ca000) = 0x56ca000
+12429 brk(0x56eb000) = 0x56eb000
+12429 brk(0x570c000) = 0x570c000
+12429 brk(0x572d000) = 0x572d000
+12429 brk(0x574e000) = 0x574e000
+12429 brk(0x576f000) = 0x576f000
+12429 brk(0x5790000) = 0x5790000
+12429 brk(0x57b1000) = 0x57b1000
+12429 brk(0x57d2000) = 0x57d2000
+12429 brk(0x57f3000) = 0x57f3000
+12429 brk(0x5814000) = 0x5814000
+12429 brk(0x5835000) = 0x5835000
+12429 brk(0x5856000) = 0x5856000
+12429 brk(0x5877000) = 0x5877000
+12429 brk(0x5898000) = 0x5898000
+12429 brk(0x58b9000) = 0x58b9000
+12429 brk(0x58da000) = 0x58da000
+12429 brk(0x58fb000) = 0x58fb000
+12429 brk(0x591c000) = 0x591c000
+12429 brk(0x593d000) = 0x593d000
+12429 brk(0x595e000) = 0x595e000
+12429 brk(0x597f000) = 0x597f000
+12429 brk(0x59a0000) = 0x59a0000
+12429 brk(0x59c1000) = 0x59c1000
+12429 brk(0x59e2000) = 0x59e2000
+12429 brk(0x5a03000) = 0x5a03000
+12429 brk(0x5a24000) = 0x5a24000
+12429 brk(0x5a45000) = 0x5a45000
+12429 brk(0x5a66000) = 0x5a66000
+12429 brk(0x5a87000) = 0x5a87000
+12429 brk(0x5aa8000) = 0x5aa8000
+12429 brk(0x5ac9000) = 0x5ac9000
+12429 brk(0x5aea000) = 0x5aea000
+12429 brk(0x5b0b000) = 0x5b0b000
+12429 brk(0x5b2c000) = 0x5b2c000
+12429 brk(0x5b4d000) = 0x5b4d000
+12429 brk(0x5b6e000) = 0x5b6e000
+12429 brk(0x5b8f000) = 0x5b8f000
+12429 brk(0x5bb0000) = 0x5bb0000
+12429 brk(0x5bd1000) = 0x5bd1000
+12429 brk(0x5bf2000) = 0x5bf2000
+12429 brk(0x5c13000) = 0x5c13000
+12429 brk(0x5c34000) = 0x5c34000
+12429 brk(0x5c55000) = 0x5c55000
+12429 brk(0x5c76000) = 0x5c76000
+12429 brk(0x5c97000) = 0x5c97000
+12429 brk(0x5cb8000) = 0x5cb8000
+12429 brk(0x5cd9000) = 0x5cd9000
+12429 brk(0x5cfa000) = 0x5cfa000
+12429 brk(0x5d1b000) = 0x5d1b000
+12429 brk(0x5d3c000) = 0x5d3c000
+12429 brk(0x5d60000) = 0x5d60000
+12429 brk(0x5d81000) = 0x5d81000
+12429 brk(0x5da2000) = 0x5da2000
+12429 brk(0x5dc3000) = 0x5dc3000
+12429 brk(0x5de4000) = 0x5de4000
+12429 brk(0x5e05000) = 0x5e05000
+12429 brk(0x5e26000) = 0x5e26000
+12429 brk(0x5e47000) = 0x5e47000
+12429 brk(0x5e68000) = 0x5e68000
+12429 brk(0x5e89000) = 0x5e89000
+12429 brk(0x5eaa000) = 0x5eaa000
+12429 brk(0x5ed0000) = 0x5ed0000
+12429 brk(0x5ef1000) = 0x5ef1000
+12429 brk(0x5f12000) = 0x5f12000
+12429 brk(0x5f33000) = 0x5f33000
+12429 brk(0x5f54000) = 0x5f54000
+12429 brk(0x5f75000) = 0x5f75000
+12429 brk(0x5f96000) = 0x5f96000
+12429 brk(0x5fb7000) = 0x5fb7000
+12429 brk(0x5fd8000) = 0x5fd8000
+12429 brk(0x5ff9000) = 0x5ff9000
+12429 brk(0x601a000) = 0x601a000
+12429 brk(0x603b000) = 0x603b000
+12429 brk(0x605c000) = 0x605c000
+12429 brk(0x607d000) = 0x607d000
+12429 brk(0x609e000) = 0x609e000
+12429 brk(0x60bf000) = 0x60bf000
+12429 brk(0x60e0000) = 0x60e0000
+12429 brk(0x6101000) = 0x6101000
+12429 brk(0x6122000) = 0x6122000
+12429 brk(0x6143000) = 0x6143000
+12429 brk(0x6164000) = 0x6164000
+12429 brk(0x6185000) = 0x6185000
+12429 brk(0x61b0000) = 0x61b0000
+12429 brk(0x61d1000) = 0x61d1000
+12429 brk(0x61f2000) = 0x61f2000
+12429 brk(0x6213000) = 0x6213000
+12429 brk(0x6234000) = 0x6234000
+12429 brk(0x6255000) = 0x6255000
+12429 brk(0x6276000) = 0x6276000
+12429 brk(0x6297000) = 0x6297000
+12429 brk(0x62b8000) = 0x62b8000
+12429 brk(0x62d9000) = 0x62d9000
+12429 brk(0x62fa000) = 0x62fa000
+12429 brk(0x631b000) = 0x631b000
+12429 brk(0x633c000) = 0x633c000
+12429 brk(0x635d000) = 0x635d000
+12429 brk(0x637e000) = 0x637e000
+12429 brk(0x639f000) = 0x639f000
+12429 brk(0x63c0000) = 0x63c0000
+12429 brk(0x63e1000) = 0x63e1000
+12429 brk(0x6402000) = 0x6402000
+12429 brk(0x6423000) = 0x6423000
+12429 brk(0x6444000) = 0x6444000
+12429 brk(0x6465000) = 0x6465000
+12429 brk(0x6486000) = 0x6486000
+12429 brk(0x64a7000) = 0x64a7000
+12429 brk(0x64c8000) = 0x64c8000
+12429 brk(0x64e9000) = 0x64e9000
+12429 brk(0x650a000) = 0x650a000
+12429 brk(0x652b000) = 0x652b000
+12429 brk(0x654c000) = 0x654c000
+12429 brk(0x656d000) = 0x656d000
+12429 brk(0x658e000) = 0x658e000
+12429 brk(0x65af000) = 0x65af000
+12429 brk(0x65d0000) = 0x65d0000
+12429 brk(0x65f1000) = 0x65f1000
+12429 brk(0x6612000) = 0x6612000
+12429 brk(0x6633000) = 0x6633000
+12429 brk(0x6654000) = 0x6654000
+12429 brk(0x6675000) = 0x6675000
+12429 brk(0x6696000) = 0x6696000
+12429 brk(0x66b7000) = 0x66b7000
+12429 brk(0x66d8000) = 0x66d8000
+12429 brk(0x66f9000) = 0x66f9000
+12429 brk(0x671a000) = 0x671a000
+12429 brk(0x673b000) = 0x673b000
+12429 brk(0x6770000) = 0x6770000
+12429 brk(0x6791000) = 0x6791000
+12429 brk(0x67b2000) = 0x67b2000
+12429 brk(0x67d3000) = 0x67d3000
+12429 brk(0x67f4000) = 0x67f4000
+12429 brk(0x6815000) = 0x6815000
+12429 brk(0x6836000) = 0x6836000
+12429 brk(0x6857000) = 0x6857000
+12429 brk(0x6878000) = 0x6878000
+12429 brk(0x6899000) = 0x6899000
+12429 brk(0x68ba000) = 0x68ba000
+12429 brk(0x68db000) = 0x68db000
+12429 brk(0x68fc000) = 0x68fc000
+12429 brk(0x691d000) = 0x691d000
+12429 brk(0x693e000) = 0x693e000
+12429 brk(0x695f000) = 0x695f000
+12429 brk(0x6980000) = 0x6980000
+12429 brk(0x69a1000) = 0x69a1000
+12429 brk(0x69c2000) = 0x69c2000
+12429 brk(0x69e3000) = 0x69e3000
+12429 brk(0x6a04000) = 0x6a04000
+12429 brk(0x6a25000) = 0x6a25000
+12429 brk(0x6a46000) = 0x6a46000
+12429 brk(0x6a67000) = 0x6a67000
+12429 brk(0x6a88000) = 0x6a88000
+12429 brk(0x6aa9000) = 0x6aa9000
+12429 brk(0x6aca000) = 0x6aca000
+12429 brk(0x6aeb000) = 0x6aeb000
+12429 brk(0x6b0c000) = 0x6b0c000
+12429 brk(0x6b2d000) = 0x6b2d000
+12429 brk(0x6b4e000) = 0x6b4e000
+12429 brk(0x6b6f000) = 0x6b6f000
+12429 brk(0x6b90000) = 0x6b90000
+12429 brk(0x6bb1000) = 0x6bb1000
+12429 brk(0x6bd2000) = 0x6bd2000
+12429 brk(0x6bf3000) = 0x6bf3000
+12429 brk(0x6c14000) = 0x6c14000
+12429 brk(0x6c35000) = 0x6c35000
+12429 brk(0x6c56000) = 0x6c56000
+12429 brk(0x6c77000) = 0x6c77000
+12429 brk(0x6c98000) = 0x6c98000
+12429 brk(0x6cb9000) = 0x6cb9000
+12429 brk(0x6cda000) = 0x6cda000
+12429 brk(0x6cfb000) = 0x6cfb000
+12429 brk(0x6d1c000) = 0x6d1c000
+12429 brk(0x6d3d000) = 0x6d3d000
+12429 brk(0x6d5e000) = 0x6d5e000
+12429 brk(0x6d7f000) = 0x6d7f000
+12429 brk(0x6da0000) = 0x6da0000
+12429 brk(0x6dc1000) = 0x6dc1000
+12429 brk(0x6de2000) = 0x6de2000
+12429 brk(0x6e03000) = 0x6e03000
+12429 brk(0x6e24000) = 0x6e24000
+12429 brk(0x6e45000) = 0x6e45000
+12429 brk(0x6e66000) = 0x6e66000
+12429 brk(0x6e87000) = 0x6e87000
+12429 brk(0x6ea8000) = 0x6ea8000
+12429 brk(0x6ec9000) = 0x6ec9000
+12429 brk(0x6eea000) = 0x6eea000
+12429 brk(0x6f0b000) = 0x6f0b000
+12429 brk(0x6f2c000) = 0x6f2c000
+12429 brk(0x6f4d000) = 0x6f4d000
+12429 brk(0x6f6e000) = 0x6f6e000
+12429 brk(0x6f8f000) = 0x6f8f000
+12429 brk(0x6fb0000) = 0x6fb0000
+12429 brk(0x6fd1000) = 0x6fd1000
+12429 brk(0x6ff2000) = 0x6ff2000
+12429 brk(0x7013000) = 0x7013000
+12429 brk(0x7034000) = 0x7034000
+12429 brk(0x7055000) = 0x7055000
+12429 brk(0x7076000) = 0x7076000
+12429 brk(0x7097000) = 0x7097000
+12429 brk(0x70b8000) = 0x70b8000
+12429 brk(0x70d9000) = 0x70d9000
+12429 brk(0x70fa000) = 0x70fa000
+12429 brk(0x711b000) = 0x711b000
+12429 brk(0x713c000) = 0x713c000
+12429 brk(0x715d000) = 0x715d000
+12429 brk(0x717e000) = 0x717e000
+12429 brk(0x719f000) = 0x719f000
+12429 brk(0x71c0000) = 0x71c0000
+12429 brk(0x71e1000) = 0x71e1000
+12429 brk(0x7202000) = 0x7202000
+12429 brk(0x7223000) = 0x7223000
+12429 brk(0x7244000) = 0x7244000
+12429 brk(0x7265000) = 0x7265000
+12429 brk(0x7286000) = 0x7286000
+12429 brk(0x72a7000) = 0x72a7000
+12429 brk(0x74b8000) = 0x74b8000
+12429 brk(0x76b8000) = 0x76b8000
+12429 brk(0x78b8000) = 0x78b8000
+12429 brk(0x78d9000) = 0x78d9000
+12429 brk(0x78fa000) = 0x78fa000
+12429 brk(0x7921000) = 0x7921000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x7fa1000) = 0x7fa1000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 brk(0x7fc2000) = 0x7fc2000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x8650000) = 0x8650000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x8ce3000) = 0x8ce3000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 mmap(NULL, 67112960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b59efc000
+12429 brk(0x8d04000) = 0x8d04000
+12429 brk(0x8d25000) = 0x8d25000
+12429 brk(0x8d46000) = 0x8d46000
+12429 brk(0x8d67000) = 0x8d67000
+12429 brk(0x8d88000) = 0x8d88000
+12429 brk(0x8da9000) = 0x8da9000
+12429 brk(0x8dca000) = 0x8dca000
+12429 brk(0x8deb000) = 0x8deb000
+12429 brk(0x8e0c000) = 0x8e0c000
+12429 brk(0x8e2d000) = 0x8e2d000
+12429 brk(0x8e4e000) = 0x8e4e000
+12429 brk(0x8e6f000) = 0x8e6f000
+12429 brk(0x8e90000) = 0x8e90000
+12429 brk(0x8eb1000) = 0x8eb1000
+12429 brk(0x8ed2000) = 0x8ed2000
+12429 brk(0x8ef3000) = 0x8ef3000
+12429 brk(0x8f14000) = 0x8f14000
+12429 brk(0x8f35000) = 0x8f35000
+12429 brk(0x8f56000) = 0x8f56000
+12429 brk(0x8f77000) = 0x8f77000
+12429 brk(0x8f98000) = 0x8f98000
+12429 brk(0x8fb9000) = 0x8fb9000
+12429 brk(0x8fda000) = 0x8fda000
+12429 brk(0x8ffb000) = 0x8ffb000
+12429 brk(0x901c000) = 0x901c000
+12429 brk(0x903d000) = 0x903d000
+12429 brk(0x905e000) = 0x905e000
+12429 brk(0x907f000) = 0x907f000
+12429 brk(0x90a0000) = 0x90a0000
+12429 brk(0x90c1000) = 0x90c1000
+12429 brk(0x90e2000) = 0x90e2000
+12429 brk(0x9103000) = 0x9103000
+12429 brk(0x9124000) = 0x9124000
+12429 brk(0x9145000) = 0x9145000
+12429 brk(0x9166000) = 0x9166000
+12429 brk(0x9187000) = 0x9187000
+12429 brk(0x91a8000) = 0x91a8000
+12429 brk(0x91c9000) = 0x91c9000
+12429 brk(0x91ea000) = 0x91ea000
+12429 brk(0x920b000) = 0x920b000
+12429 brk(0x922c000) = 0x922c000
+12429 brk(0x924d000) = 0x924d000
+12429 brk(0x926e000) = 0x926e000
+12429 brk(0x928f000) = 0x928f000
+12429 brk(0x92b0000) = 0x92b0000
+12429 brk(0x92d1000) = 0x92d1000
+12429 brk(0x92f2000) = 0x92f2000
+12429 brk(0x9324000) = 0x9324000
+12429 brk(0x9345000) = 0x9345000
+12429 brk(0x9366000) = 0x9366000
+12429 brk(0x9387000) = 0x9387000
+12429 brk(0x93a8000) = 0x93a8000
+12429 brk(0x93c9000) = 0x93c9000
+12429 brk(0x93ea000) = 0x93ea000
+12429 brk(0x940b000) = 0x940b000
+12429 brk(0x942d000) = 0x942d000
+12429 brk(0x944e000) = 0x944e000
+12429 brk(0x946f000) = 0x946f000
+12429 brk(0x9490000) = 0x9490000
+12429 brk(0x94b1000) = 0x94b1000
+12429 brk(0x94d2000) = 0x94d2000
+12429 brk(0x94f3000) = 0x94f3000
+12429 brk(0x9514000) = 0x9514000
+12429 brk(0x9535000) = 0x9535000
+12429 brk(0x9556000) = 0x9556000
+12429 brk(0x9577000) = 0x9577000
+12429 brk(0x9598000) = 0x9598000
+12429 brk(0x95b9000) = 0x95b9000
+12429 brk(0x95da000) = 0x95da000
+12429 brk(0x95fb000) = 0x95fb000
+12429 brk(0x961c000) = 0x961c000
+12429 brk(0x963d000) = 0x963d000
+12429 brk(0x965e000) = 0x965e000
+12429 brk(0x967f000) = 0x967f000
+12429 brk(0x96a0000) = 0x96a0000
+12429 brk(0x96c1000) = 0x96c1000
+12429 brk(0x96e2000) = 0x96e2000
+12429 brk(0x9703000) = 0x9703000
+12429 brk(0x9724000) = 0x9724000
+12429 brk(0x9745000) = 0x9745000
+12429 brk(0x976b000) = 0x976b000
+12429 brk(0x978c000) = 0x978c000
+12429 brk(0x97ad000) = 0x97ad000
+12429 brk(0x97ce000) = 0x97ce000
+12429 brk(0x97ef000) = 0x97ef000
+12429 brk(0x9810000) = 0x9810000
+12429 brk(0x9831000) = 0x9831000
+12429 brk(0x9852000) = 0x9852000
+12429 brk(0x9873000) = 0x9873000
+12429 brk(0x9894000) = 0x9894000
+12429 brk(0x98b5000) = 0x98b5000
+12429 brk(0x98d6000) = 0x98d6000
+12429 brk(0x98f7000) = 0x98f7000
+12429 brk(0x9918000) = 0x9918000
+12429 brk(0x9939000) = 0x9939000
+12429 brk(0x995a000) = 0x995a000
+12429 brk(0x997b000) = 0x997b000
+12429 brk(0x999c000) = 0x999c000
+12429 brk(0x99bd000) = 0x99bd000
+12429 brk(0x99de000) = 0x99de000
+12429 brk(0x99ff000) = 0x99ff000
+12429 brk(0x9a20000) = 0x9a20000
+12429 brk(0x9a41000) = 0x9a41000
+12429 brk(0x9a62000) = 0x9a62000
+12429 brk(0x9a83000) = 0x9a83000
+12429 brk(0x9aa4000) = 0x9aa4000
+12429 brk(0x9ac5000) = 0x9ac5000
+12429 brk(0x9ae6000) = 0x9ae6000
+12429 brk(0x9b07000) = 0x9b07000
+12429 brk(0x9b28000) = 0x9b28000
+12429 brk(0x9b49000) = 0x9b49000
+12429 brk(0x9b6a000) = 0x9b6a000
+12429 brk(0x9b8b000) = 0x9b8b000
+12429 brk(0x9bac000) = 0x9bac000
+12429 brk(0x9bcd000) = 0x9bcd000
+12429 brk(0x9bee000) = 0x9bee000
+12429 brk(0x9c0f000) = 0x9c0f000
+12429 brk(0x9c30000) = 0x9c30000
+12429 brk(0x9c51000) = 0x9c51000
+12429 brk(0x9c72000) = 0x9c72000
+12429 brk(0x9c93000) = 0x9c93000
+12429 brk(0x9cb4000) = 0x9cb4000
+12429 brk(0x9cd5000) = 0x9cd5000
+12429 brk(0x9cf6000) = 0x9cf6000
+12429 brk(0x9d17000) = 0x9d17000
+12429 brk(0x9d38000) = 0x9d38000
+12429 brk(0x9d59000) = 0x9d59000
+12429 brk(0x9d7a000) = 0x9d7a000
+12429 brk(0x9d9b000) = 0x9d9b000
+12429 brk(0x9dbc000) = 0x9dbc000
+12429 brk(0x9ddd000) = 0x9ddd000
+12429 brk(0x9dfe000) = 0x9dfe000
+12429 brk(0x9e1f000) = 0x9e1f000
+12429 brk(0x9e40000) = 0x9e40000
+12429 brk(0x9e61000) = 0x9e61000
+12429 brk(0x9e82000) = 0x9e82000
+12429 brk(0x9ea3000) = 0x9ea3000
+12429 brk(0x9ec4000) = 0x9ec4000
+12429 brk(0x9ee5000) = 0x9ee5000
+12429 brk(0x9f06000) = 0x9f06000
+12429 brk(0x9f27000) = 0x9f27000
+12429 brk(0x9f48000) = 0x9f48000
+12429 brk(0x9f69000) = 0x9f69000
+12429 brk(0x9f8a000) = 0x9f8a000
+12429 brk(0x9fab000) = 0x9fab000
+12429 brk(0x9fcc000) = 0x9fcc000
+12429 brk(0x9fed000) = 0x9fed000
+12429 brk(0xa00e000) = 0xa00e000
+12429 brk(0xa02f000) = 0xa02f000
+12429 brk(0xa050000) = 0xa050000
+12429 brk(0xa071000) = 0xa071000
+12429 brk(0xa092000) = 0xa092000
+12429 brk(0xa0b3000) = 0xa0b3000
+12429 brk(0xa0d4000) = 0xa0d4000
+12429 brk(0xa0f5000) = 0xa0f5000
+12429 brk(0xa116000) = 0xa116000
+12429 brk(0xa137000) = 0xa137000
+12429 brk(0xa158000) = 0xa158000
+12429 brk(0xa179000) = 0xa179000
+12429 brk(0xa19a000) = 0xa19a000
+12429 brk(0xa1bb000) = 0xa1bb000
+12429 brk(0xa1dc000) = 0xa1dc000
+12429 brk(0xa1fd000) = 0xa1fd000
+12429 brk(0xa21e000) = 0xa21e000
+12429 brk(0xa242000) = 0xa242000
+12429 brk(0xa263000) = 0xa263000
+12429 brk(0xa284000) = 0xa284000
+12429 brk(0xa2a5000) = 0xa2a5000
+12429 brk(0xa2c6000) = 0xa2c6000
+12429 brk(0xa2e7000) = 0xa2e7000
+12429 brk(0xa308000) = 0xa308000
+12429 brk(0xa329000) = 0xa329000
+12429 brk(0xa34a000) = 0xa34a000
+12429 brk(0xa36b000) = 0xa36b000
+12429 brk(0xa38c000) = 0xa38c000
+12429 brk(0xa3b2000) = 0xa3b2000
+12429 brk(0xa3d3000) = 0xa3d3000
+12429 brk(0xa3f4000) = 0xa3f4000
+12429 brk(0xa415000) = 0xa415000
+12429 brk(0xa436000) = 0xa436000
+12429 brk(0xa457000) = 0xa457000
+12429 brk(0xa478000) = 0xa478000
+12429 brk(0xa499000) = 0xa499000
+12429 brk(0xa4ba000) = 0xa4ba000
+12429 brk(0xa4db000) = 0xa4db000
+12429 brk(0xa4fc000) = 0xa4fc000
+12429 brk(0xa51d000) = 0xa51d000
+12429 brk(0xa53e000) = 0xa53e000
+12429 brk(0xa55f000) = 0xa55f000
+12429 brk(0xa580000) = 0xa580000
+12429 brk(0xa5a1000) = 0xa5a1000
+12429 brk(0xa5c2000) = 0xa5c2000
+12429 brk(0xa5e3000) = 0xa5e3000
+12429 brk(0xa604000) = 0xa604000
+12429 brk(0xa625000) = 0xa625000
+12429 brk(0xa646000) = 0xa646000
+12429 brk(0xa667000) = 0xa667000
+12429 brk(0xa692000) = 0xa692000
+12429 brk(0xa6b3000) = 0xa6b3000
+12429 brk(0xa6d4000) = 0xa6d4000
+12429 brk(0xa6f5000) = 0xa6f5000
+12429 brk(0xa716000) = 0xa716000
+12429 brk(0xa737000) = 0xa737000
+12429 brk(0xa758000) = 0xa758000
+12429 brk(0xa779000) = 0xa779000
+12429 brk(0xa79a000) = 0xa79a000
+12429 brk(0xa7bb000) = 0xa7bb000
+12429 brk(0xa7dc000) = 0xa7dc000
+12429 brk(0xa7fd000) = 0xa7fd000
+12429 brk(0xa81e000) = 0xa81e000
+12429 brk(0xa83f000) = 0xa83f000
+12429 brk(0xa860000) = 0xa860000
+12429 brk(0xa881000) = 0xa881000
+12429 brk(0xa8a2000) = 0xa8a2000
+12429 brk(0xa8c3000) = 0xa8c3000
+12429 brk(0xa8e4000) = 0xa8e4000
+12429 brk(0xa905000) = 0xa905000
+12429 brk(0xa926000) = 0xa926000
+12429 brk(0xa947000) = 0xa947000
+12429 brk(0xa968000) = 0xa968000
+12429 brk(0xa989000) = 0xa989000
+12429 brk(0xa9aa000) = 0xa9aa000
+12429 brk(0xa9cb000) = 0xa9cb000
+12429 brk(0xa9ec000) = 0xa9ec000
+12429 brk(0xaa0d000) = 0xaa0d000
+12429 brk(0xaa2e000) = 0xaa2e000
+12429 brk(0xaa4f000) = 0xaa4f000
+12429 brk(0xaa70000) = 0xaa70000
+12429 brk(0xaa91000) = 0xaa91000
+12429 brk(0xaab2000) = 0xaab2000
+12429 brk(0xaad3000) = 0xaad3000
+12429 brk(0xaaf4000) = 0xaaf4000
+12429 brk(0xab15000) = 0xab15000
+12429 brk(0xab36000) = 0xab36000
+12429 brk(0xab57000) = 0xab57000
+12429 brk(0xab78000) = 0xab78000
+12429 brk(0xab99000) = 0xab99000
+12429 brk(0xabba000) = 0xabba000
+12429 brk(0xabdb000) = 0xabdb000
+12429 brk(0xabfc000) = 0xabfc000
+12429 brk(0xac1d000) = 0xac1d000
+12429 brk(0xac52000) = 0xac52000
+12429 brk(0xac73000) = 0xac73000
+12429 brk(0xac94000) = 0xac94000
+12429 brk(0xacb5000) = 0xacb5000
+12429 brk(0xacd6000) = 0xacd6000
+12429 brk(0xacf7000) = 0xacf7000
+12429 brk(0xad18000) = 0xad18000
+12429 brk(0xad39000) = 0xad39000
+12429 brk(0xad5a000) = 0xad5a000
+12429 brk(0xad7b000) = 0xad7b000
+12429 brk(0xad9c000) = 0xad9c000
+12429 brk(0xadbd000) = 0xadbd000
+12429 brk(0xadde000) = 0xadde000
+12429 brk(0xadff000) = 0xadff000
+12429 brk(0xae20000) = 0xae20000
+12429 brk(0xae41000) = 0xae41000
+12429 brk(0xae62000) = 0xae62000
+12429 brk(0xae83000) = 0xae83000
+12429 brk(0xaea4000) = 0xaea4000
+12429 brk(0xaec5000) = 0xaec5000
+12429 brk(0xaee6000) = 0xaee6000
+12429 brk(0xaf07000) = 0xaf07000
+12429 brk(0xaf28000) = 0xaf28000
+12429 brk(0xaf49000) = 0xaf49000
+12429 brk(0xaf6a000) = 0xaf6a000
+12429 brk(0xaf8b000) = 0xaf8b000
+12429 brk(0xafac000) = 0xafac000
+12429 brk(0xafcd000) = 0xafcd000
+12429 brk(0xafee000) = 0xafee000
+12429 brk(0xb00f000) = 0xb00f000
+12429 brk(0xb030000) = 0xb030000
+12429 brk(0xb051000) = 0xb051000
+12429 brk(0xb072000) = 0xb072000
+12429 brk(0xb093000) = 0xb093000
+12429 brk(0xb0b4000) = 0xb0b4000
+12429 brk(0xb0d5000) = 0xb0d5000
+12429 brk(0xb0f6000) = 0xb0f6000
+12429 brk(0xb117000) = 0xb117000
+12429 brk(0xb138000) = 0xb138000
+12429 brk(0xb159000) = 0xb159000
+12429 brk(0xb17a000) = 0xb17a000
+12429 brk(0xb19b000) = 0xb19b000
+12429 brk(0xb1bc000) = 0xb1bc000
+12429 brk(0xb1dd000) = 0xb1dd000
+12429 brk(0xb1fe000) = 0xb1fe000
+12429 brk(0xb21f000) = 0xb21f000
+12429 brk(0xb240000) = 0xb240000
+12429 brk(0xb261000) = 0xb261000
+12429 brk(0xb282000) = 0xb282000
+12429 brk(0xb2a3000) = 0xb2a3000
+12429 brk(0xb2c4000) = 0xb2c4000
+12429 brk(0xb2e5000) = 0xb2e5000
+12429 brk(0xb306000) = 0xb306000
+12429 brk(0xb327000) = 0xb327000
+12429 brk(0xb348000) = 0xb348000
+12429 brk(0xb369000) = 0xb369000
+12429 brk(0xb38a000) = 0xb38a000
+12429 brk(0xb3ab000) = 0xb3ab000
+12429 brk(0xb3cc000) = 0xb3cc000
+12429 brk(0xb3ed000) = 0xb3ed000
+12429 brk(0xb40e000) = 0xb40e000
+12429 brk(0xb42f000) = 0xb42f000
+12429 brk(0xb450000) = 0xb450000
+12429 brk(0xb471000) = 0xb471000
+12429 brk(0xb492000) = 0xb492000
+12429 brk(0xb4b3000) = 0xb4b3000
+12429 brk(0xb4d4000) = 0xb4d4000
+12429 brk(0xb4f5000) = 0xb4f5000
+12429 brk(0xb516000) = 0xb516000
+12429 brk(0xb537000) = 0xb537000
+12429 brk(0xb558000) = 0xb558000
+12429 brk(0xb579000) = 0xb579000
+12429 brk(0xb59a000) = 0xb59a000
+12429 brk(0xb5bb000) = 0xb5bb000
+12429 brk(0xb5dc000) = 0xb5dc000
+12429 brk(0xb5fd000) = 0xb5fd000
+12429 brk(0xb61e000) = 0xb61e000
+12429 brk(0xb63f000) = 0xb63f000
+12429 brk(0xb660000) = 0xb660000
+12429 brk(0xb681000) = 0xb681000
+12429 brk(0xb6a2000) = 0xb6a2000
+12429 brk(0xb6c3000) = 0xb6c3000
+12429 brk(0xb6e4000) = 0xb6e4000
+12429 brk(0xb705000) = 0xb705000
+12429 brk(0xb726000) = 0xb726000
+12429 brk(0xb747000) = 0xb747000
+12429 brk(0xb768000) = 0xb768000
+12429 brk(0xb789000) = 0xb789000
+12429 brk(0xb99a000) = 0xb99a000
+12429 brk(0xbb9b000) = 0xbb9b000
+12429 brk(0xbd9b000) = 0xbd9b000
+12429 brk(0xbdbc000) = 0xbdbc000
+12429 brk(0xbddd000) = 0xbddd000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0xc469000) = 0xc469000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 brk(0xc4c7000) = 0xc4c7000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0xcb47000) = 0xcb47000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0xd1cd000) = 0xd1cd000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 mmap(NULL, 67112960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b55efb000
+12429 brk(0xd1ee000) = 0xd1ee000
+12429 brk(0xd20f000) = 0xd20f000
+12429 brk(0xd230000) = 0xd230000
+12429 brk(0xd251000) = 0xd251000
+12429 brk(0xd272000) = 0xd272000
+12429 brk(0xd293000) = 0xd293000
+12429 brk(0xd2b4000) = 0xd2b4000
+12429 brk(0xd2d5000) = 0xd2d5000
+12429 brk(0xd2f6000) = 0xd2f6000
+12429 brk(0xd317000) = 0xd317000
+12429 brk(0xd338000) = 0xd338000
+12429 brk(0xd359000) = 0xd359000
+12429 brk(0xd37a000) = 0xd37a000
+12429 brk(0xd39b000) = 0xd39b000
+12429 brk(0xd3bc000) = 0xd3bc000
+12429 brk(0xd3dd000) = 0xd3dd000
+12429 brk(0xd3fe000) = 0xd3fe000
+12429 brk(0xd41f000) = 0xd41f000
+12429 brk(0xd440000) = 0xd440000
+12429 brk(0xd461000) = 0xd461000
+12429 brk(0xd482000) = 0xd482000
+12429 brk(0xd4a3000) = 0xd4a3000
+12429 brk(0xd4c4000) = 0xd4c4000
+12429 brk(0xd4e5000) = 0xd4e5000
+12429 brk(0xd506000) = 0xd506000
+12429 brk(0xd527000) = 0xd527000
+12429 brk(0xd548000) = 0xd548000
+12429 brk(0xd569000) = 0xd569000
+12429 brk(0xd58a000) = 0xd58a000
+12429 brk(0xd5ab000) = 0xd5ab000
+12429 brk(0xd5cc000) = 0xd5cc000
+12429 brk(0xd5ed000) = 0xd5ed000
+12429 brk(0xd60e000) = 0xd60e000
+12429 brk(0xd62f000) = 0xd62f000
+12429 brk(0xd650000) = 0xd650000
+12429 brk(0xd671000) = 0xd671000
+12429 brk(0xd692000) = 0xd692000
+12429 brk(0xd6b3000) = 0xd6b3000
+12429 brk(0xd6d4000) = 0xd6d4000
+12429 brk(0xd6f5000) = 0xd6f5000
+12429 brk(0xd716000) = 0xd716000
+12429 brk(0xd737000) = 0xd737000
+12429 brk(0xd758000) = 0xd758000
+12429 brk(0xd779000) = 0xd779000
+12429 brk(0xd79a000) = 0xd79a000
+12429 brk(0xd7bb000) = 0xd7bb000
+12429 brk(0xd7dc000) = 0xd7dc000
+12429 brk(0xd80d000) = 0xd80d000
+12429 brk(0xd82e000) = 0xd82e000
+12429 brk(0xd84f000) = 0xd84f000
+12429 brk(0xd870000) = 0xd870000
+12429 brk(0xd891000) = 0xd891000
+12429 brk(0xd8b2000) = 0xd8b2000
+12429 brk(0xd8d3000) = 0xd8d3000
+12429 brk(0xd8f4000) = 0xd8f4000
+12429 brk(0xd916000) = 0xd916000
+12429 brk(0xd937000) = 0xd937000
+12429 brk(0xd958000) = 0xd958000
+12429 brk(0xd979000) = 0xd979000
+12429 brk(0xd99a000) = 0xd99a000
+12429 brk(0xd9bb000) = 0xd9bb000
+12429 brk(0xd9dc000) = 0xd9dc000
+12429 brk(0xd9fd000) = 0xd9fd000
+12429 brk(0xda1e000) = 0xda1e000
+12429 brk(0xda3f000) = 0xda3f000
+12429 brk(0xda60000) = 0xda60000
+12429 brk(0xda81000) = 0xda81000
+12429 brk(0xdaa2000) = 0xdaa2000
+12429 brk(0xdac3000) = 0xdac3000
+12429 brk(0xdae4000) = 0xdae4000
+12429 brk(0xdb05000) = 0xdb05000
+12429 brk(0xdb26000) = 0xdb26000
+12429 brk(0xdb47000) = 0xdb47000
+12429 brk(0xdb68000) = 0xdb68000
+12429 brk(0xdb89000) = 0xdb89000
+12429 brk(0xdbaa000) = 0xdbaa000
+12429 brk(0xdbcb000) = 0xdbcb000
+12429 brk(0xdbec000) = 0xdbec000
+12429 brk(0xdc0d000) = 0xdc0d000
+12429 brk(0xdc2e000) = 0xdc2e000
+12429 brk(0xdc54000) = 0xdc54000
+12429 brk(0xdc75000) = 0xdc75000
+12429 brk(0xdc96000) = 0xdc96000
+12429 brk(0xdcb7000) = 0xdcb7000
+12429 brk(0xdcd8000) = 0xdcd8000
+12429 brk(0xdcf9000) = 0xdcf9000
+12429 brk(0xdd1a000) = 0xdd1a000
+12429 brk(0xdd3b000) = 0xdd3b000
+12429 brk(0xdd5c000) = 0xdd5c000
+12429 brk(0xdd7d000) = 0xdd7d000
+12429 brk(0xdd9e000) = 0xdd9e000
+12429 brk(0xddbf000) = 0xddbf000
+12429 brk(0xdde0000) = 0xdde0000
+12429 brk(0xde01000) = 0xde01000
+12429 brk(0xde22000) = 0xde22000
+12429 brk(0xde43000) = 0xde43000
+12429 brk(0xde64000) = 0xde64000
+12429 brk(0xde85000) = 0xde85000
+12429 brk(0xdea6000) = 0xdea6000
+12429 brk(0xdec7000) = 0xdec7000
+12429 brk(0xdee8000) = 0xdee8000
+12429 brk(0xdf09000) = 0xdf09000
+12429 brk(0xdf2a000) = 0xdf2a000
+12429 brk(0xdf4b000) = 0xdf4b000
+12429 brk(0xdf6c000) = 0xdf6c000
+12429 brk(0xdf8d000) = 0xdf8d000
+12429 brk(0xdfae000) = 0xdfae000
+12429 brk(0xdfcf000) = 0xdfcf000
+12429 brk(0xdff0000) = 0xdff0000
+12429 brk(0xe011000) = 0xe011000
+12429 brk(0xe032000) = 0xe032000
+12429 brk(0xe053000) = 0xe053000
+12429 brk(0xe074000) = 0xe074000
+12429 brk(0xe095000) = 0xe095000
+12429 brk(0xe0b6000) = 0xe0b6000
+12429 brk(0xe0d7000) = 0xe0d7000
+12429 brk(0xe0f8000) = 0xe0f8000
+12429 brk(0xe119000) = 0xe119000
+12429 brk(0xe13a000) = 0xe13a000
+12429 brk(0xe15b000) = 0xe15b000
+12429 brk(0xe17c000) = 0xe17c000
+12429 brk(0xe19d000) = 0xe19d000
+12429 brk(0xe1be000) = 0xe1be000
+12429 brk(0xe1df000) = 0xe1df000
+12429 brk(0xe200000) = 0xe200000
+12429 brk(0xe221000) = 0xe221000
+12429 brk(0xe242000) = 0xe242000
+12429 brk(0xe263000) = 0xe263000
+12429 brk(0xe284000) = 0xe284000
+12429 brk(0xe2a5000) = 0xe2a5000
+12429 brk(0xe2c6000) = 0xe2c6000
+12429 brk(0xe2e7000) = 0xe2e7000
+12429 brk(0xe308000) = 0xe308000
+12429 brk(0xe329000) = 0xe329000
+12429 brk(0xe34a000) = 0xe34a000
+12429 brk(0xe36b000) = 0xe36b000
+12429 brk(0xe38c000) = 0xe38c000
+12429 brk(0xe3ad000) = 0xe3ad000
+12429 brk(0xe3ce000) = 0xe3ce000
+12429 brk(0xe3ef000) = 0xe3ef000
+12429 brk(0xe410000) = 0xe410000
+12429 brk(0xe431000) = 0xe431000
+12429 brk(0xe452000) = 0xe452000
+12429 brk(0xe473000) = 0xe473000
+12429 brk(0xe494000) = 0xe494000
+12429 brk(0xe4b5000) = 0xe4b5000
+12429 brk(0xe4d6000) = 0xe4d6000
+12429 brk(0xe4f7000) = 0xe4f7000
+12429 brk(0xe518000) = 0xe518000
+12429 brk(0xe539000) = 0xe539000
+12429 brk(0xe55a000) = 0xe55a000
+12429 brk(0xe57b000) = 0xe57b000
+12429 brk(0xe59c000) = 0xe59c000
+12429 brk(0xe5bd000) = 0xe5bd000
+12429 brk(0xe5de000) = 0xe5de000
+12429 brk(0xe5ff000) = 0xe5ff000
+12429 brk(0xe620000) = 0xe620000
+12429 brk(0xe641000) = 0xe641000
+12429 brk(0xe662000) = 0xe662000
+12429 brk(0xe683000) = 0xe683000
+12429 brk(0xe6a4000) = 0xe6a4000
+12429 brk(0xe6c5000) = 0xe6c5000
+12429 brk(0xe6e6000) = 0xe6e6000
+12429 brk(0xe707000) = 0xe707000
+12429 brk(0xe72b000) = 0xe72b000
+12429 brk(0xe74c000) = 0xe74c000
+12429 brk(0xe76d000) = 0xe76d000
+12429 brk(0xe78e000) = 0xe78e000
+12429 brk(0xe7af000) = 0xe7af000
+12429 brk(0xe7d0000) = 0xe7d0000
+12429 brk(0xe7f1000) = 0xe7f1000
+12429 brk(0xe812000) = 0xe812000
+12429 brk(0xe833000) = 0xe833000
+12429 brk(0xe854000) = 0xe854000
+12429 brk(0xe875000) = 0xe875000
+12429 brk(0xe89b000) = 0xe89b000
+12429 brk(0xe8bc000) = 0xe8bc000
+12429 brk(0xe8dd000) = 0xe8dd000
+12429 brk(0xe8fe000) = 0xe8fe000
+12429 brk(0xe91f000) = 0xe91f000
+12429 brk(0xe940000) = 0xe940000
+12429 brk(0xe961000) = 0xe961000
+12429 brk(0xe982000) = 0xe982000
+12429 brk(0xe9a3000) = 0xe9a3000
+12429 brk(0xe9c4000) = 0xe9c4000
+12429 brk(0xe9e5000) = 0xe9e5000
+12429 brk(0xea06000) = 0xea06000
+12429 brk(0xea27000) = 0xea27000
+12429 brk(0xea48000) = 0xea48000
+12429 brk(0xea69000) = 0xea69000
+12429 brk(0xea8a000) = 0xea8a000
+12429 brk(0xeaab000) = 0xeaab000
+12429 brk(0xeacc000) = 0xeacc000
+12429 brk(0xeaed000) = 0xeaed000
+12429 brk(0xeb0e000) = 0xeb0e000
+12429 brk(0xeb2f000) = 0xeb2f000
+12429 brk(0xeb50000) = 0xeb50000
+12429 brk(0xeb7b000) = 0xeb7b000
+12429 brk(0xeb9c000) = 0xeb9c000
+12429 brk(0xebbd000) = 0xebbd000
+12429 brk(0xebde000) = 0xebde000
+12429 brk(0xebff000) = 0xebff000
+12429 brk(0xec20000) = 0xec20000
+12429 brk(0xec41000) = 0xec41000
+12429 brk(0xec62000) = 0xec62000
+12429 brk(0xec83000) = 0xec83000
+12429 brk(0xeca4000) = 0xeca4000
+12429 brk(0xecc5000) = 0xecc5000
+12429 brk(0xece6000) = 0xece6000
+12429 brk(0xed07000) = 0xed07000
+12429 brk(0xed28000) = 0xed28000
+12429 brk(0xed49000) = 0xed49000
+12429 brk(0xed6a000) = 0xed6a000
+12429 brk(0xed8b000) = 0xed8b000
+12429 brk(0xedac000) = 0xedac000
+12429 brk(0xedcd000) = 0xedcd000
+12429 brk(0xedee000) = 0xedee000
+12429 brk(0xee0f000) = 0xee0f000
+12429 brk(0xee30000) = 0xee30000
+12429 brk(0xee51000) = 0xee51000
+12429 brk(0xee72000) = 0xee72000
+12429 brk(0xee93000) = 0xee93000
+12429 brk(0xeeb4000) = 0xeeb4000
+12429 brk(0xeed5000) = 0xeed5000
+12429 brk(0xeef6000) = 0xeef6000
+12429 brk(0xef17000) = 0xef17000
+12429 brk(0xef38000) = 0xef38000
+12429 brk(0xef59000) = 0xef59000
+12429 brk(0xef7a000) = 0xef7a000
+12429 brk(0xef9b000) = 0xef9b000
+12429 brk(0xefbc000) = 0xefbc000
+12429 brk(0xefdd000) = 0xefdd000
+12429 brk(0xeffe000) = 0xeffe000
+12429 brk(0xf01f000) = 0xf01f000
+12429 brk(0xf040000) = 0xf040000
+12429 brk(0xf061000) = 0xf061000
+12429 brk(0xf082000) = 0xf082000
+12429 brk(0xf0a3000) = 0xf0a3000
+12429 brk(0xf0c4000) = 0xf0c4000
+12429 brk(0xf0e5000) = 0xf0e5000
+12429 brk(0xf106000) = 0xf106000
+12429 brk(0xf13b000) = 0xf13b000
+12429 brk(0xf15c000) = 0xf15c000
+12429 brk(0xf17d000) = 0xf17d000
+12429 brk(0xf19e000) = 0xf19e000
+12429 brk(0xf1bf000) = 0xf1bf000
+12429 brk(0xf1e0000) = 0xf1e0000
+12429 brk(0xf201000) = 0xf201000
+12429 brk(0xf222000) = 0xf222000
+12429 brk(0xf243000) = 0xf243000
+12429 brk(0xf264000) = 0xf264000
+12429 brk(0xf285000) = 0xf285000
+12429 brk(0xf2a6000) = 0xf2a6000
+12429 brk(0xf2c7000) = 0xf2c7000
+12429 brk(0xf2e8000) = 0xf2e8000
+12429 brk(0xf309000) = 0xf309000
+12429 brk(0xf32a000) = 0xf32a000
+12429 brk(0xf34b000) = 0xf34b000
+12429 brk(0xf36c000) = 0xf36c000
+12429 brk(0xf38d000) = 0xf38d000
+12429 brk(0xf3ae000) = 0xf3ae000
+12429 brk(0xf3cf000) = 0xf3cf000
+12429 brk(0xf3f0000) = 0xf3f0000
+12429 brk(0xf411000) = 0xf411000
+12429 brk(0xf432000) = 0xf432000
+12429 brk(0xf453000) = 0xf453000
+12429 brk(0xf474000) = 0xf474000
+12429 brk(0xf495000) = 0xf495000
+12429 brk(0xf4b6000) = 0xf4b6000
+12429 brk(0xf4d7000) = 0xf4d7000
+12429 brk(0xf4f8000) = 0xf4f8000
+12429 brk(0xf519000) = 0xf519000
+12429 brk(0xf53a000) = 0xf53a000
+12429 brk(0xf55b000) = 0xf55b000
+12429 brk(0xf57c000) = 0xf57c000
+12429 brk(0xf59d000) = 0xf59d000
+12429 brk(0xf5be000) = 0xf5be000
+12429 brk(0xf5df000) = 0xf5df000
+12429 brk(0xf600000) = 0xf600000
+12429 brk(0xf621000) = 0xf621000
+12429 brk(0xf642000) = 0xf642000
+12429 brk(0xf663000) = 0xf663000
+12429 brk(0xf684000) = 0xf684000
+12429 brk(0xf6a5000) = 0xf6a5000
+12429 brk(0xf6c6000) = 0xf6c6000
+12429 brk(0xf6e7000) = 0xf6e7000
+12429 brk(0xf708000) = 0xf708000
+12429 brk(0xf729000) = 0xf729000
+12429 brk(0xf74a000) = 0xf74a000
+12429 brk(0xf76b000) = 0xf76b000
+12429 brk(0xf78c000) = 0xf78c000
+12429 brk(0xf7ad000) = 0xf7ad000
+12429 brk(0xf7ce000) = 0xf7ce000
+12429 brk(0xf7ef000) = 0xf7ef000
+12429 brk(0xf810000) = 0xf810000
+12429 brk(0xf831000) = 0xf831000
+12429 brk(0xf852000) = 0xf852000
+12429 brk(0xf873000) = 0xf873000
+12429 brk(0xf894000) = 0xf894000
+12429 brk(0xf8b5000) = 0xf8b5000
+12429 brk(0xf8d6000) = 0xf8d6000
+12429 brk(0xf8f7000) = 0xf8f7000
+12429 brk(0xf918000) = 0xf918000
+12429 brk(0xf939000) = 0xf939000
+12429 brk(0xf95a000) = 0xf95a000
+12429 brk(0xf97b000) = 0xf97b000
+12429 brk(0xf99c000) = 0xf99c000
+12429 brk(0xf9bd000) = 0xf9bd000
+12429 brk(0xf9de000) = 0xf9de000
+12429 brk(0xf9ff000) = 0xf9ff000
+12429 brk(0xfa20000) = 0xfa20000
+12429 brk(0xfa41000) = 0xfa41000
+12429 brk(0xfa62000) = 0xfa62000
+12429 brk(0xfa83000) = 0xfa83000
+12429 brk(0xfaa4000) = 0xfaa4000
+12429 brk(0xfac5000) = 0xfac5000
+12429 brk(0xfae6000) = 0xfae6000
+12429 brk(0xfb07000) = 0xfb07000
+12429 brk(0xfb28000) = 0xfb28000
+12429 brk(0xfb49000) = 0xfb49000
+12429 brk(0xfb6a000) = 0xfb6a000
+12429 brk(0xfb8b000) = 0xfb8b000
+12429 brk(0xfbac000) = 0xfbac000
+12429 brk(0xfbcd000) = 0xfbcd000
+12429 brk(0xfbee000) = 0xfbee000
+12429 brk(0xfc0f000) = 0xfc0f000
+12429 brk(0xfc30000) = 0xfc30000
+12429 brk(0xfc51000) = 0xfc51000
+12429 brk(0xfc72000) = 0xfc72000
+12429 brk(0xfe84000) = 0xfe84000
+12429 brk(0x10084000) = 0x10084000
+12429 brk(0x10284000) = 0x10284000
+12429 brk(0x102a5000) = 0x102a5000
+12429 brk(0x102c6000) = 0x102c6000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x10952000) = 0x10952000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 brk(0x10973000) = 0x10973000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x11003000) = 0x11003000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x11696000) = 0x11696000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 mmap(NULL, 67112960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b51efa000
+12429 brk(0x116b7000) = 0x116b7000
+12429 brk(0x116d8000) = 0x116d8000
+12429 brk(0x116f9000) = 0x116f9000
+12429 brk(0x1171a000) = 0x1171a000
+12429 brk(0x1173b000) = 0x1173b000
+12429 brk(0x1175c000) = 0x1175c000
+12429 brk(0x1177d000) = 0x1177d000
+12429 brk(0x1179e000) = 0x1179e000
+12429 brk(0x117bf000) = 0x117bf000
+12429 brk(0x117e0000) = 0x117e0000
+12429 brk(0x11801000) = 0x11801000
+12429 brk(0x11822000) = 0x11822000
+12429 brk(0x11843000) = 0x11843000
+12429 brk(0x11864000) = 0x11864000
+12429 brk(0x11885000) = 0x11885000
+12429 brk(0x118a6000) = 0x118a6000
+12429 brk(0x118c7000) = 0x118c7000
+12429 brk(0x118e8000) = 0x118e8000
+12429 brk(0x11909000) = 0x11909000
+12429 brk(0x1192a000) = 0x1192a000
+12429 brk(0x1194b000) = 0x1194b000
+12429 brk(0x1196c000) = 0x1196c000
+12429 brk(0x1198d000) = 0x1198d000
+12429 brk(0x119ae000) = 0x119ae000
+12429 brk(0x119cf000) = 0x119cf000
+12429 brk(0x119f0000) = 0x119f0000
+12429 brk(0x11a11000) = 0x11a11000
+12429 brk(0x11a32000) = 0x11a32000
+12429 brk(0x11a53000) = 0x11a53000
+12429 brk(0x11a74000) = 0x11a74000
+12429 brk(0x11a95000) = 0x11a95000
+12429 brk(0x11ab6000) = 0x11ab6000
+12429 brk(0x11ad7000) = 0x11ad7000
+12429 brk(0x11af8000) = 0x11af8000
+12429 brk(0x11b19000) = 0x11b19000
+12429 brk(0x11b3a000) = 0x11b3a000
+12429 brk(0x11b5b000) = 0x11b5b000
+12429 brk(0x11b7c000) = 0x11b7c000
+12429 brk(0x11b9d000) = 0x11b9d000
+12429 brk(0x11bbe000) = 0x11bbe000
+12429 brk(0x11bdf000) = 0x11bdf000
+12429 brk(0x11c00000) = 0x11c00000
+12429 brk(0x11c21000) = 0x11c21000
+12429 brk(0x11c42000) = 0x11c42000
+12429 brk(0x11c63000) = 0x11c63000
+12429 brk(0x11c84000) = 0x11c84000
+12429 brk(0x11ca5000) = 0x11ca5000
+12429 brk(0x11cd6000) = 0x11cd6000
+12429 brk(0x11cf7000) = 0x11cf7000
+12429 brk(0x11d18000) = 0x11d18000
+12429 brk(0x11d39000) = 0x11d39000
+12429 brk(0x11d5a000) = 0x11d5a000
+12429 brk(0x11d7b000) = 0x11d7b000
+12429 brk(0x11d9c000) = 0x11d9c000
+12429 brk(0x11dbd000) = 0x11dbd000
+12429 brk(0x11ddf000) = 0x11ddf000
+12429 brk(0x11e00000) = 0x11e00000
+12429 brk(0x11e21000) = 0x11e21000
+12429 brk(0x11e42000) = 0x11e42000
+12429 brk(0x11e63000) = 0x11e63000
+12429 brk(0x11e84000) = 0x11e84000
+12429 brk(0x11ea5000) = 0x11ea5000
+12429 brk(0x11ec6000) = 0x11ec6000
+12429 brk(0x11ee7000) = 0x11ee7000
+12429 brk(0x11f08000) = 0x11f08000
+12429 brk(0x11f29000) = 0x11f29000
+12429 brk(0x11f4a000) = 0x11f4a000
+12429 brk(0x11f6b000) = 0x11f6b000
+12429 brk(0x11f8c000) = 0x11f8c000
+12429 brk(0x11fad000) = 0x11fad000
+12429 brk(0x11fce000) = 0x11fce000
+12429 brk(0x11fef000) = 0x11fef000
+12429 brk(0x12010000) = 0x12010000
+12429 brk(0x12031000) = 0x12031000
+12429 brk(0x12052000) = 0x12052000
+12429 brk(0x12073000) = 0x12073000
+12429 brk(0x12094000) = 0x12094000
+12429 brk(0x120b5000) = 0x120b5000
+12429 brk(0x120d6000) = 0x120d6000
+12429 brk(0x120f7000) = 0x120f7000
+12429 brk(0x1211e000) = 0x1211e000
+12429 brk(0x1213f000) = 0x1213f000
+12429 brk(0x12160000) = 0x12160000
+12429 brk(0x12181000) = 0x12181000
+12429 brk(0x121a2000) = 0x121a2000
+12429 brk(0x121c3000) = 0x121c3000
+12429 brk(0x121e4000) = 0x121e4000
+12429 brk(0x12205000) = 0x12205000
+12429 brk(0x12226000) = 0x12226000
+12429 brk(0x12247000) = 0x12247000
+12429 brk(0x12268000) = 0x12268000
+12429 brk(0x12289000) = 0x12289000
+12429 brk(0x122aa000) = 0x122aa000
+12429 brk(0x122cb000) = 0x122cb000
+12429 brk(0x122ec000) = 0x122ec000
+12429 brk(0x1230d000) = 0x1230d000
+12429 brk(0x1232e000) = 0x1232e000
+12429 brk(0x1234f000) = 0x1234f000
+12429 brk(0x12370000) = 0x12370000
+12429 brk(0x12391000) = 0x12391000
+12429 brk(0x123b2000) = 0x123b2000
+12429 brk(0x123d3000) = 0x123d3000
+12429 brk(0x123f4000) = 0x123f4000
+12429 brk(0x12415000) = 0x12415000
+12429 brk(0x12436000) = 0x12436000
+12429 brk(0x12457000) = 0x12457000
+12429 brk(0x12478000) = 0x12478000
+12429 brk(0x12499000) = 0x12499000
+12429 brk(0x124ba000) = 0x124ba000
+12429 brk(0x124db000) = 0x124db000
+12429 brk(0x124fc000) = 0x124fc000
+12429 brk(0x1251d000) = 0x1251d000
+12429 brk(0x1253e000) = 0x1253e000
+12429 brk(0x1255f000) = 0x1255f000
+12429 brk(0x12580000) = 0x12580000
+12429 brk(0x125a1000) = 0x125a1000
+12429 brk(0x125c2000) = 0x125c2000
+12429 brk(0x125e3000) = 0x125e3000
+12429 brk(0x12604000) = 0x12604000
+12429 brk(0x12625000) = 0x12625000
+12429 brk(0x12646000) = 0x12646000
+12429 brk(0x12667000) = 0x12667000
+12429 brk(0x12688000) = 0x12688000
+12429 brk(0x126a9000) = 0x126a9000
+12429 brk(0x126ca000) = 0x126ca000
+12429 brk(0x126eb000) = 0x126eb000
+12429 brk(0x1270c000) = 0x1270c000
+12429 brk(0x1272d000) = 0x1272d000
+12429 brk(0x1274e000) = 0x1274e000
+12429 brk(0x1276f000) = 0x1276f000
+12429 brk(0x12790000) = 0x12790000
+12429 brk(0x127b1000) = 0x127b1000
+12429 brk(0x127d2000) = 0x127d2000
+12429 brk(0x127f3000) = 0x127f3000
+12429 brk(0x12814000) = 0x12814000
+12429 brk(0x12835000) = 0x12835000
+12429 brk(0x12856000) = 0x12856000
+12429 brk(0x12877000) = 0x12877000
+12429 brk(0x12898000) = 0x12898000
+12429 brk(0x128b9000) = 0x128b9000
+12429 brk(0x128da000) = 0x128da000
+12429 brk(0x128fb000) = 0x128fb000
+12429 brk(0x1291c000) = 0x1291c000
+12429 brk(0x1293d000) = 0x1293d000
+12429 brk(0x1295e000) = 0x1295e000
+12429 brk(0x1297f000) = 0x1297f000
+12429 brk(0x129a0000) = 0x129a0000
+12429 brk(0x129c1000) = 0x129c1000
+12429 brk(0x129e2000) = 0x129e2000
+12429 brk(0x12a03000) = 0x12a03000
+12429 brk(0x12a24000) = 0x12a24000
+12429 brk(0x12a45000) = 0x12a45000
+12429 brk(0x12a66000) = 0x12a66000
+12429 brk(0x12a87000) = 0x12a87000
+12429 brk(0x12aa8000) = 0x12aa8000
+12429 brk(0x12ac9000) = 0x12ac9000
+12429 brk(0x12aea000) = 0x12aea000
+12429 brk(0x12b0b000) = 0x12b0b000
+12429 brk(0x12b2c000) = 0x12b2c000
+12429 brk(0x12b4d000) = 0x12b4d000
+12429 brk(0x12b6e000) = 0x12b6e000
+12429 brk(0x12b8f000) = 0x12b8f000
+12429 brk(0x12bb0000) = 0x12bb0000
+12429 brk(0x12bd1000) = 0x12bd1000
+12429 brk(0x12bf5000) = 0x12bf5000
+12429 brk(0x12c16000) = 0x12c16000
+12429 brk(0x12c37000) = 0x12c37000
+12429 brk(0x12c58000) = 0x12c58000
+12429 brk(0x12c79000) = 0x12c79000
+12429 brk(0x12c9a000) = 0x12c9a000
+12429 brk(0x12cbb000) = 0x12cbb000
+12429 brk(0x12cdc000) = 0x12cdc000
+12429 brk(0x12cfd000) = 0x12cfd000
+12429 brk(0x12d1e000) = 0x12d1e000
+12429 brk(0x12d3f000) = 0x12d3f000
+12429 brk(0x12d65000) = 0x12d65000
+12429 brk(0x12d86000) = 0x12d86000
+12429 brk(0x12da7000) = 0x12da7000
+12429 brk(0x12dc8000) = 0x12dc8000
+12429 brk(0x12de9000) = 0x12de9000
+12429 brk(0x12e0a000) = 0x12e0a000
+12429 brk(0x12e2b000) = 0x12e2b000
+12429 brk(0x12e4c000) = 0x12e4c000
+12429 brk(0x12e6d000) = 0x12e6d000
+12429 brk(0x12e8e000) = 0x12e8e000
+12429 brk(0x12eaf000) = 0x12eaf000
+12429 brk(0x12ed0000) = 0x12ed0000
+12429 brk(0x12ef1000) = 0x12ef1000
+12429 brk(0x12f12000) = 0x12f12000
+12429 brk(0x12f33000) = 0x12f33000
+12429 brk(0x12f54000) = 0x12f54000
+12429 brk(0x12f75000) = 0x12f75000
+12429 brk(0x12f96000) = 0x12f96000
+12429 brk(0x12fb7000) = 0x12fb7000
+12429 brk(0x12fd8000) = 0x12fd8000
+12429 brk(0x12ff9000) = 0x12ff9000
+12429 brk(0x1301a000) = 0x1301a000
+12429 brk(0x13045000) = 0x13045000
+12429 brk(0x13066000) = 0x13066000
+12429 brk(0x13087000) = 0x13087000
+12429 brk(0x130a8000) = 0x130a8000
+12429 brk(0x130c9000) = 0x130c9000
+12429 brk(0x130ea000) = 0x130ea000
+12429 brk(0x1310b000) = 0x1310b000
+12429 brk(0x1312c000) = 0x1312c000
+12429 brk(0x1314d000) = 0x1314d000
+12429 brk(0x1316e000) = 0x1316e000
+12429 brk(0x1318f000) = 0x1318f000
+12429 brk(0x131b0000) = 0x131b0000
+12429 brk(0x131d1000) = 0x131d1000
+12429 brk(0x131f2000) = 0x131f2000
+12429 brk(0x13213000) = 0x13213000
+12429 brk(0x13234000) = 0x13234000
+12429 brk(0x13255000) = 0x13255000
+12429 brk(0x13276000) = 0x13276000
+12429 brk(0x13297000) = 0x13297000
+12429 brk(0x132b8000) = 0x132b8000
+12429 brk(0x132d9000) = 0x132d9000
+12429 brk(0x132fa000) = 0x132fa000
+12429 brk(0x1331b000) = 0x1331b000
+12429 brk(0x1333c000) = 0x1333c000
+12429 brk(0x1335d000) = 0x1335d000
+12429 brk(0x1337e000) = 0x1337e000
+12429 brk(0x1339f000) = 0x1339f000
+12429 brk(0x133c0000) = 0x133c0000
+12429 brk(0x133e1000) = 0x133e1000
+12429 brk(0x13402000) = 0x13402000
+12429 brk(0x13423000) = 0x13423000
+12429 brk(0x13444000) = 0x13444000
+12429 brk(0x13465000) = 0x13465000
+12429 brk(0x13486000) = 0x13486000
+12429 brk(0x134a7000) = 0x134a7000
+12429 brk(0x134c8000) = 0x134c8000
+12429 brk(0x134e9000) = 0x134e9000
+12429 brk(0x1350a000) = 0x1350a000
+12429 brk(0x1352b000) = 0x1352b000
+12429 brk(0x1354c000) = 0x1354c000
+12429 brk(0x1356d000) = 0x1356d000
+12429 brk(0x1358e000) = 0x1358e000
+12429 brk(0x135af000) = 0x135af000
+12429 brk(0x135d0000) = 0x135d0000
+12429 brk(0x13605000) = 0x13605000
+12429 brk(0x13626000) = 0x13626000
+12429 brk(0x13647000) = 0x13647000
+12429 brk(0x13668000) = 0x13668000
+12429 brk(0x13689000) = 0x13689000
+12429 brk(0x136aa000) = 0x136aa000
+12429 brk(0x136cb000) = 0x136cb000
+12429 brk(0x136ec000) = 0x136ec000
+12429 brk(0x1370d000) = 0x1370d000
+12429 brk(0x1372e000) = 0x1372e000
+12429 brk(0x1374f000) = 0x1374f000
+12429 brk(0x13770000) = 0x13770000
+12429 brk(0x13791000) = 0x13791000
+12429 brk(0x137b2000) = 0x137b2000
+12429 brk(0x137d3000) = 0x137d3000
+12429 brk(0x137f4000) = 0x137f4000
+12429 brk(0x13815000) = 0x13815000
+12429 brk(0x13836000) = 0x13836000
+12429 brk(0x13857000) = 0x13857000
+12429 brk(0x13878000) = 0x13878000
+12429 brk(0x13899000) = 0x13899000
+12429 brk(0x138ba000) = 0x138ba000
+12429 brk(0x138db000) = 0x138db000
+12429 brk(0x138fc000) = 0x138fc000
+12429 brk(0x1391d000) = 0x1391d000
+12429 brk(0x1393e000) = 0x1393e000
+12429 brk(0x1395f000) = 0x1395f000
+12429 brk(0x13980000) = 0x13980000
+12429 brk(0x139a1000) = 0x139a1000
+12429 brk(0x139c2000) = 0x139c2000
+12429 brk(0x139e3000) = 0x139e3000
+12429 brk(0x13a04000) = 0x13a04000
+12429 brk(0x13a25000) = 0x13a25000
+12429 brk(0x13a46000) = 0x13a46000
+12429 brk(0x13a67000) = 0x13a67000
+12429 brk(0x13a88000) = 0x13a88000
+12429 brk(0x13aa9000) = 0x13aa9000
+12429 brk(0x13aca000) = 0x13aca000
+12429 brk(0x13aeb000) = 0x13aeb000
+12429 brk(0x13b0c000) = 0x13b0c000
+12429 brk(0x13b2d000) = 0x13b2d000
+12429 brk(0x13b4e000) = 0x13b4e000
+12429 brk(0x13b6f000) = 0x13b6f000
+12429 brk(0x13b90000) = 0x13b90000
+12429 brk(0x13bb1000) = 0x13bb1000
+12429 brk(0x13bd2000) = 0x13bd2000
+12429 brk(0x13bf3000) = 0x13bf3000
+12429 brk(0x13c14000) = 0x13c14000
+12429 brk(0x13c35000) = 0x13c35000
+12429 brk(0x13c56000) = 0x13c56000
+12429 brk(0x13c77000) = 0x13c77000
+12429 brk(0x13c98000) = 0x13c98000
+12429 brk(0x13cb9000) = 0x13cb9000
+12429 brk(0x13cda000) = 0x13cda000
+12429 brk(0x13cfb000) = 0x13cfb000
+12429 brk(0x13d1c000) = 0x13d1c000
+12429 brk(0x13d3d000) = 0x13d3d000
+12429 brk(0x13d5e000) = 0x13d5e000
+12429 brk(0x13d7f000) = 0x13d7f000
+12429 brk(0x13da0000) = 0x13da0000
+12429 brk(0x13dc1000) = 0x13dc1000
+12429 brk(0x13de2000) = 0x13de2000
+12429 brk(0x13e03000) = 0x13e03000
+12429 brk(0x13e24000) = 0x13e24000
+12429 brk(0x13e45000) = 0x13e45000
+12429 brk(0x13e66000) = 0x13e66000
+12429 brk(0x13e87000) = 0x13e87000
+12429 brk(0x13ea8000) = 0x13ea8000
+12429 brk(0x13ec9000) = 0x13ec9000
+12429 brk(0x13eea000) = 0x13eea000
+12429 brk(0x13f0b000) = 0x13f0b000
+12429 brk(0x13f2c000) = 0x13f2c000
+12429 brk(0x13f4d000) = 0x13f4d000
+12429 brk(0x13f6e000) = 0x13f6e000
+12429 brk(0x13f8f000) = 0x13f8f000
+12429 brk(0x13fb0000) = 0x13fb0000
+12429 brk(0x13fd1000) = 0x13fd1000
+12429 brk(0x13ff2000) = 0x13ff2000
+12429 brk(0x14013000) = 0x14013000
+12429 brk(0x14034000) = 0x14034000
+12429 brk(0x14055000) = 0x14055000
+12429 brk(0x14076000) = 0x14076000
+12429 brk(0x14097000) = 0x14097000
+12429 brk(0x140b8000) = 0x140b8000
+12429 brk(0x140d9000) = 0x140d9000
+12429 brk(0x140fa000) = 0x140fa000
+12429 brk(0x1411b000) = 0x1411b000
+12429 brk(0x1413c000) = 0x1413c000
+12429 brk(0x1434d000) = 0x1434d000
+12429 brk(0x1454d000) = 0x1454d000
+12429 brk(0x1474d000) = 0x1474d000
+12429 brk(0x1476e000) = 0x1476e000
+12429 brk(0x1478f000) = 0x1478f000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x14e1c000) = 0x14e1c000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 brk(0x14e3d000) = 0x14e3d000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x154cc000) = 0x154cc000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 brk(0x15b5f000) = 0x15b5f000
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libiss_kvx.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libiss_kvx.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libmppa_multiloader.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libmppa_multiloader.so", F_OK) = 0
+12429 readlink("/proc/self/exe", "/opt/Kalray/usr/local/k1rdtools/"..., 1024) = 46
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 1024) = 54
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib32/libstd_scalls.so", F_OK) = -1 ENOENT (No such file or directory)
+12429 access("/opt/Kalray/usr/local/k1rdtools/bin/../lib64/libstd_scalls.so", F_OK) = 0
+12429 mmap(NULL, 67112960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4b4def9000
+12429 brk(0x15bdf000) = 0x15bdf000
+12429 brk(0x15c00000) = 0x15c00000
+12429 brk(0x15c21000) = 0x15c21000
+12429 brk(0x15c42000) = 0x15c42000
+12429 brk(0x15c63000) = 0x15c63000
+12429 brk(0x15c84000) = 0x15c84000
+12429 brk(0x15ca5000) = 0x15ca5000
+12429 brk(0x15cc6000) = 0x15cc6000
+12429 brk(0x15ce7000) = 0x15ce7000
+12429 brk(0x15d08000) = 0x15d08000
+12429 brk(0x15d29000) = 0x15d29000
+12429 brk(0x15d4a000) = 0x15d4a000
+12429 brk(0x15d6b000) = 0x15d6b000
+12429 brk(0x15d8c000) = 0x15d8c000
+12429 brk(0x15dad000) = 0x15dad000
+12429 brk(0x15dce000) = 0x15dce000
+12429 brk(0x15def000) = 0x15def000
+12429 brk(0x15e10000) = 0x15e10000
+12429 brk(0x15e31000) = 0x15e31000
+12429 brk(0x15e52000) = 0x15e52000
+12429 brk(0x15e73000) = 0x15e73000
+12429 brk(0x15e94000) = 0x15e94000
+12429 brk(0x15eb5000) = 0x15eb5000
+12429 brk(0x15ed6000) = 0x15ed6000
+12429 brk(0x15ef7000) = 0x15ef7000
+12429 brk(0x15f18000) = 0x15f18000
+12429 brk(0x15f39000) = 0x15f39000
+12429 brk(0x15f5a000) = 0x15f5a000
+12429 brk(0x15f7b000) = 0x15f7b000
+12429 brk(0x15f9c000) = 0x15f9c000
+12429 brk(0x15fbd000) = 0x15fbd000
+12429 brk(0x15fde000) = 0x15fde000
+12429 brk(0x15fff000) = 0x15fff000
+12429 brk(0x16020000) = 0x16020000
+12429 brk(0x16041000) = 0x16041000
+12429 brk(0x16062000) = 0x16062000
+12429 brk(0x16083000) = 0x16083000
+12429 brk(0x160a4000) = 0x160a4000
+12429 brk(0x160c5000) = 0x160c5000
+12429 brk(0x160e6000) = 0x160e6000
+12429 brk(0x16107000) = 0x16107000
+12429 brk(0x16128000) = 0x16128000
+12429 brk(0x16149000) = 0x16149000
+12429 brk(0x1616a000) = 0x1616a000
+12429 brk(0x1618b000) = 0x1618b000
+12429 brk(0x161ac000) = 0x161ac000
+12429 brk(0x161cd000) = 0x161cd000
+12429 brk(0x161f1000) = 0x161f1000
+12429 brk(0x16212000) = 0x16212000
+12429 brk(0x16233000) = 0x16233000
+12429 brk(0x16254000) = 0x16254000
+12429 brk(0x16275000) = 0x16275000
+12429 brk(0x16296000) = 0x16296000
+12429 brk(0x162b7000) = 0x162b7000
+12429 brk(0x162d8000) = 0x162d8000
+12429 brk(0x162f9000) = 0x162f9000
+12429 brk(0x1631a000) = 0x1631a000
+12429 brk(0x1633b000) = 0x1633b000
+12429 brk(0x1635c000) = 0x1635c000
+12429 brk(0x1637d000) = 0x1637d000
+12429 brk(0x1639e000) = 0x1639e000
+12429 brk(0x163bf000) = 0x163bf000
+12429 brk(0x163e0000) = 0x163e0000
+12429 brk(0x16401000) = 0x16401000
+12429 brk(0x16422000) = 0x16422000
+12429 brk(0x16443000) = 0x16443000
+12429 brk(0x16464000) = 0x16464000
+12429 brk(0x1648b000) = 0x1648b000
+12429 brk(0x164ac000) = 0x164ac000
+12429 brk(0x164cd000) = 0x164cd000
+12429 brk(0x164ee000) = 0x164ee000
+12429 brk(0x1650f000) = 0x1650f000
+12429 brk(0x16530000) = 0x16530000
+12429 brk(0x16551000) = 0x16551000
+12429 brk(0x16572000) = 0x16572000
+12429 brk(0x16593000) = 0x16593000
+12429 brk(0x165b4000) = 0x165b4000
+12429 brk(0x165d5000) = 0x165d5000
+12429 brk(0x165f6000) = 0x165f6000
+12429 brk(0x16617000) = 0x16617000
+12429 brk(0x16638000) = 0x16638000
+12429 brk(0x16659000) = 0x16659000
+12429 brk(0x1667a000) = 0x1667a000
+12429 brk(0x1669b000) = 0x1669b000
+12429 brk(0x166bc000) = 0x166bc000
+12429 brk(0x166dd000) = 0x166dd000
+12429 brk(0x166fe000) = 0x166fe000
+12429 brk(0x1671f000) = 0x1671f000
+12429 brk(0x16740000) = 0x16740000
+12429 brk(0x16761000) = 0x16761000
+12429 brk(0x16782000) = 0x16782000
+12429 brk(0x167a3000) = 0x167a3000
+12429 brk(0x167c4000) = 0x167c4000
+12429 brk(0x167e5000) = 0x167e5000
+12429 brk(0x16806000) = 0x16806000
+12429 brk(0x16827000) = 0x16827000
+12429 brk(0x16848000) = 0x16848000
+12429 brk(0x16869000) = 0x16869000
+12429 brk(0x1688a000) = 0x1688a000
+12429 brk(0x168ab000) = 0x168ab000
+12429 brk(0x168cc000) = 0x168cc000
+12429 brk(0x168ed000) = 0x168ed000
+12429 brk(0x1690e000) = 0x1690e000
+12429 brk(0x1692f000) = 0x1692f000
+12429 brk(0x1695f000) = 0x1695f000
+12429 brk(0x16980000) = 0x16980000
+12429 brk(0x169a1000) = 0x169a1000
+12429 brk(0x169c2000) = 0x169c2000
+12429 brk(0x169e3000) = 0x169e3000
+12429 brk(0x16a04000) = 0x16a04000
+12429 brk(0x16a25000) = 0x16a25000
+12429 brk(0x16a46000) = 0x16a46000
+12429 brk(0x16a67000) = 0x16a67000
+12429 brk(0x16a88000) = 0x16a88000
+12429 brk(0x16aa9000) = 0x16aa9000
+12429 brk(0x16aca000) = 0x16aca000
+12429 brk(0x16aeb000) = 0x16aeb000
+12429 brk(0x16b0c000) = 0x16b0c000
+12429 brk(0x16b2d000) = 0x16b2d000
+12429 brk(0x16b4e000) = 0x16b4e000
+12429 brk(0x16b6f000) = 0x16b6f000
+12429 brk(0x16b90000) = 0x16b90000
+12429 brk(0x16bb1000) = 0x16bb1000
+12429 brk(0x16bd2000) = 0x16bd2000
+12429 brk(0x16bf3000) = 0x16bf3000
+12429 brk(0x16c14000) = 0x16c14000
+12429 brk(0x16c35000) = 0x16c35000
+12429 brk(0x16c56000) = 0x16c56000
+12429 brk(0x16c77000) = 0x16c77000
+12429 brk(0x16c98000) = 0x16c98000
+12429 brk(0x16cb9000) = 0x16cb9000
+12429 brk(0x16cda000) = 0x16cda000
+12429 brk(0x16cfb000) = 0x16cfb000
+12429 brk(0x16d1c000) = 0x16d1c000
+12429 brk(0x16d3d000) = 0x16d3d000
+12429 brk(0x16d5e000) = 0x16d5e000
+12429 brk(0x16d7f000) = 0x16d7f000
+12429 brk(0x16da0000) = 0x16da0000
+12429 brk(0x16dc1000) = 0x16dc1000
+12429 brk(0x16de2000) = 0x16de2000
+12429 brk(0x16e03000) = 0x16e03000
+12429 brk(0x16e24000) = 0x16e24000
+12429 brk(0x16e45000) = 0x16e45000
+12429 brk(0x16e66000) = 0x16e66000
+12429 brk(0x16e87000) = 0x16e87000
+12429 brk(0x16ea8000) = 0x16ea8000
+12429 brk(0x16ec9000) = 0x16ec9000
+12429 brk(0x16eea000) = 0x16eea000
+12429 brk(0x16f0b000) = 0x16f0b000
+12429 brk(0x16f2c000) = 0x16f2c000
+12429 brk(0x16f4d000) = 0x16f4d000
+12429 brk(0x16f6e000) = 0x16f6e000
+12429 brk(0x16f8f000) = 0x16f8f000
+12429 brk(0x16fb0000) = 0x16fb0000
+12429 brk(0x16fd1000) = 0x16fd1000
+12429 brk(0x16ff2000) = 0x16ff2000
+12429 brk(0x17013000) = 0x17013000
+12429 brk(0x17034000) = 0x17034000
+12429 brk(0x17055000) = 0x17055000
+12429 brk(0x17076000) = 0x17076000
+12429 brk(0x17097000) = 0x17097000
+12429 brk(0x170b8000) = 0x170b8000
+12429 brk(0x170d9000) = 0x170d9000
+12429 brk(0x170fa000) = 0x170fa000
+12429 brk(0x1711b000) = 0x1711b000
+12429 brk(0x1713c000) = 0x1713c000
+12429 brk(0x1715d000) = 0x1715d000
+12429 brk(0x1717e000) = 0x1717e000
+12429 brk(0x1719f000) = 0x1719f000
+12429 brk(0x171c0000) = 0x171c0000
+12429 brk(0x171e1000) = 0x171e1000
+12429 brk(0x17202000) = 0x17202000
+12429 brk(0x17223000) = 0x17223000
+12429 brk(0x17244000) = 0x17244000
+12429 brk(0x17265000) = 0x17265000
+12429 brk(0x17286000) = 0x17286000
+12429 brk(0x172a7000) = 0x172a7000
+12429 brk(0x172c8000) = 0x172c8000
+12429 brk(0x172e9000) = 0x172e9000
+12429 brk(0x1730a000) = 0x1730a000
+12429 brk(0x1732b000) = 0x1732b000
+12429 brk(0x1734c000) = 0x1734c000
+12429 brk(0x1736d000) = 0x1736d000
+12429 brk(0x1738e000) = 0x1738e000
+12429 brk(0x173af000) = 0x173af000
+12429 brk(0x173d0000) = 0x173d0000
+12429 brk(0x173f1000) = 0x173f1000
+12429 brk(0x17412000) = 0x17412000
+12429 brk(0x17433000) = 0x17433000
+12429 brk(0x17454000) = 0x17454000
+12429 brk(0x17475000) = 0x17475000
+12429 brk(0x17496000) = 0x17496000
+12429 brk(0x174b7000) = 0x174b7000
+12429 brk(0x174d8000) = 0x174d8000
+12429 brk(0x174f9000) = 0x174f9000
+12429 brk(0x1751a000) = 0x1751a000
+12429 brk(0x1753b000) = 0x1753b000
+12429 brk(0x1755c000) = 0x1755c000
+12429 brk(0x1757d000) = 0x1757d000
+12429 brk(0x1759e000) = 0x1759e000
+12429 brk(0x175bf000) = 0x175bf000
+12429 brk(0x175e0000) = 0x175e0000
+12429 brk(0x17601000) = 0x17601000
+12429 brk(0x17622000) = 0x17622000
+12429 brk(0x17643000) = 0x17643000
+12429 brk(0x17664000) = 0x17664000
+12429 brk(0x17685000) = 0x17685000
+12429 brk(0x176a6000) = 0x176a6000
+12429 brk(0x176c7000) = 0x176c7000
+12429 brk(0x176e8000) = 0x176e8000
+12429 brk(0x17709000) = 0x17709000
+12429 brk(0x1772a000) = 0x1772a000
+12429 brk(0x1774b000) = 0x1774b000
+12429 brk(0x1776c000) = 0x1776c000
+12429 brk(0x1778d000) = 0x1778d000
+12429 brk(0x177ae000) = 0x177ae000
+12429 brk(0x177cf000) = 0x177cf000
+12429 brk(0x177f0000) = 0x177f0000
+12429 brk(0x17811000) = 0x17811000
+12429 brk(0x17832000) = 0x17832000
+12429 brk(0x17853000) = 0x17853000
+12429 brk(0x17874000) = 0x17874000
+12429 brk(0x17895000) = 0x17895000
+12429 brk(0x178b6000) = 0x178b6000
+12429 brk(0x178d7000) = 0x178d7000
+12429 brk(0x178f8000) = 0x178f8000
+12429 brk(0x17919000) = 0x17919000
+12429 brk(0x1793a000) = 0x1793a000
+12429 brk(0x1795b000) = 0x1795b000
+12429 brk(0x1797c000) = 0x1797c000
+12429 brk(0x1799d000) = 0x1799d000
+12429 brk(0x179be000) = 0x179be000
+12429 brk(0x179df000) = 0x179df000
+12429 brk(0x17a00000) = 0x17a00000
+12429 brk(0x17a21000) = 0x17a21000
+12429 brk(0x17a42000) = 0x17a42000
+12429 brk(0x17a63000) = 0x17a63000
+12429 brk(0x17a84000) = 0x17a84000
+12429 brk(0x17aa5000) = 0x17aa5000
+12429 brk(0x17ac6000) = 0x17ac6000
+12429 brk(0x17ae7000) = 0x17ae7000
+12429 brk(0x17b0e000) = 0x17b0e000
+12429 brk(0x17b2f000) = 0x17b2f000
+12429 brk(0x17b50000) = 0x17b50000
+12429 brk(0x17b71000) = 0x17b71000
+12429 brk(0x17b92000) = 0x17b92000
+12429 brk(0x17bb3000) = 0x17bb3000
+12429 brk(0x17bd4000) = 0x17bd4000
+12429 brk(0x17bf5000) = 0x17bf5000
+12429 brk(0x17c16000) = 0x17c16000
+12429 brk(0x17c37000) = 0x17c37000
+12429 brk(0x17c58000) = 0x17c58000
+12429 brk(0x17c79000) = 0x17c79000
+12429 brk(0x17c9a000) = 0x17c9a000
+12429 brk(0x17cbb000) = 0x17cbb000
+12429 brk(0x17cdc000) = 0x17cdc000
+12429 brk(0x17cfd000) = 0x17cfd000
+12429 brk(0x17d1e000) = 0x17d1e000
+12429 brk(0x17d3f000) = 0x17d3f000
+12429 brk(0x17d60000) = 0x17d60000
+12429 brk(0x17d81000) = 0x17d81000
+12429 brk(0x17da2000) = 0x17da2000
+12429 brk(0x17dc3000) = 0x17dc3000
+12429 brk(0x17de4000) = 0x17de4000
+12429 brk(0x17e05000) = 0x17e05000
+12429 brk(0x17e26000) = 0x17e26000
+12429 brk(0x17e47000) = 0x17e47000
+12429 brk(0x17e68000) = 0x17e68000
+12429 brk(0x17e89000) = 0x17e89000
+12429 brk(0x17eaa000) = 0x17eaa000
+12429 brk(0x17ecb000) = 0x17ecb000
+12429 brk(0x17eec000) = 0x17eec000
+12429 brk(0x17f0d000) = 0x17f0d000
+12429 brk(0x17f2e000) = 0x17f2e000
+12429 brk(0x17f4f000) = 0x17f4f000
+12429 brk(0x17f70000) = 0x17f70000
+12429 brk(0x17f91000) = 0x17f91000
+12429 brk(0x17fb2000) = 0x17fb2000
+12429 brk(0x17fd3000) = 0x17fd3000
+12429 brk(0x17ff4000) = 0x17ff4000
+12429 brk(0x18015000) = 0x18015000
+12429 brk(0x18036000) = 0x18036000
+12429 brk(0x18057000) = 0x18057000
+12429 brk(0x18078000) = 0x18078000
+12429 brk(0x18099000) = 0x18099000
+12429 brk(0x180ba000) = 0x180ba000
+12429 brk(0x180db000) = 0x180db000
+12429 brk(0x180fc000) = 0x180fc000
+12429 brk(0x1811d000) = 0x1811d000
+12429 brk(0x1813e000) = 0x1813e000
+12429 brk(0x1815f000) = 0x1815f000
+12429 brk(0x18180000) = 0x18180000
+12429 brk(0x181a1000) = 0x181a1000
+12429 brk(0x181c2000) = 0x181c2000
+12429 brk(0x181e3000) = 0x181e3000
+12429 brk(0x18204000) = 0x18204000
+12429 brk(0x18225000) = 0x18225000
+12429 brk(0x18246000) = 0x18246000
+12429 brk(0x18267000) = 0x18267000
+12429 brk(0x18288000) = 0x18288000
+12429 brk(0x182a9000) = 0x182a9000
+12429 brk(0x182ca000) = 0x182ca000
+12429 brk(0x182eb000) = 0x182eb000
+12429 brk(0x1830c000) = 0x1830c000
+12429 brk(0x1832d000) = 0x1832d000
+12429 brk(0x1834e000) = 0x1834e000
+12429 brk(0x1836f000) = 0x1836f000
+12429 brk(0x18390000) = 0x18390000
+12429 brk(0x183b1000) = 0x183b1000
+12429 brk(0x183d2000) = 0x183d2000
+12429 brk(0x183f3000) = 0x183f3000
+12429 brk(0x18414000) = 0x18414000
+12429 brk(0x18435000) = 0x18435000
+12429 brk(0x18456000) = 0x18456000
+12429 brk(0x18477000) = 0x18477000
+12429 brk(0x18498000) = 0x18498000
+12429 brk(0x184b9000) = 0x184b9000
+12429 brk(0x184da000) = 0x184da000
+12429 brk(0x184fb000) = 0x184fb000
+12429 brk(0x1851c000) = 0x1851c000
+12429 brk(0x1853d000) = 0x1853d000
+12429 brk(0x1855e000) = 0x1855e000
+12429 brk(0x1857f000) = 0x1857f000
+12429 brk(0x185a0000) = 0x185a0000
+12429 brk(0x185c1000) = 0x185c1000
+12429 brk(0x185e2000) = 0x185e2000
+12429 brk(0x18603000) = 0x18603000
+12429 brk(0x18624000) = 0x18624000
+12429 brk(0x18645000) = 0x18645000
+12429 brk(0x18856000) = 0x18856000
+12429 brk(0x18a56000) = 0x18a56000
+12429 brk(0x18c56000) = 0x18c56000
+12429 brk(0x18c77000) = 0x18c77000
+12429 brk(0x18ca5000) = 0x18ca5000
+12429 brk(0x18d05000) = 0x18d05000
+12429 brk(0x18d85000) = 0x18d85000
+12429 brk(0x18e85000) = 0x18e85000
+12429 brk(0x19085000) = 0x19085000
+12429 brk(0x19485000) = 0x19485000
+12429 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4c68599000
+12429 munmap(0x7f4c68599000, 8392704) = 0
+12429 brk(0x1955a000) = 0x1955a000
+12429 brk(0x1975a000) = 0x1975a000
+12429 brk(0x19b5a000) = 0x19b5a000
+12429 brk(0x1a35a000) = 0x1a35a000
+12429 brk(0x1935a000) = 0x1935a000
+12429 brk(0x1937b000) = 0x1937b000
+12429 brk(0x1939c000) = 0x1939c000
+12429 brk(0x193bd000) = 0x193bd000
+12429 brk(0x193de000) = 0x193de000
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f4c68599000
+12429 mprotect(0x7f4c6859a000, 8388608, PROT_READ|PROT_WRITE) = 0
+12429 clone(child_stack=0x7f4c68d98fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4c68d999d0, tls=0x7f4c68d99700, child_tidptr=0x7f4c68d999d0) = 12432
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f4b4d6f8000
+12432 set_robust_list(0x7f4c68d999e0, 24 <unfinished ...>
+12429 mprotect(0x7f4b4d6f9000, 8388608, PROT_READ|PROT_WRITE <unfinished ...>
+12432 <... set_robust_list resumed> ) = 0
+12429 <... mprotect resumed> ) = 0
+12432 prctl(PR_SET_NAME, "node0_SIM" <unfinished ...>
+12429 clone( <unfinished ...>
+12432 <... prctl resumed> ) = 0
+12429 <... clone resumed> child_stack=0x7f4b4def7fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4b4def89d0, tls=0x7f4b4def8700, child_tidptr=0x7f4b4def89d0) = 12433
+12433 set_robust_list(0x7f4b4def89e0, 24 <unfinished ...>
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0 <unfinished ...>
+12433 <... set_robust_list resumed> ) = 0
+12429 <... mmap resumed> ) = 0x7f4b4cef7000
+12433 prctl(PR_SET_NAME, "node1_SIM" <unfinished ...>
+12429 mprotect(0x7f4b4cef8000, 8388608, PROT_READ|PROT_WRITE <unfinished ...>
+12433 <... prctl resumed> ) = 0
+12429 <... mprotect resumed> ) = 0
+12433 mmap(0x7f4c68000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12429 clone( <unfinished ...>
+12432 mmap(0x7f4c68000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12433 <... mmap resumed> ) = 0x7f4b48ef7000
+12432 <... mmap resumed> ) = 0x7f4b44ef7000
+12429 <... clone resumed> child_stack=0x7f4b4d6f6fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4b4d6f79d0, tls=0x7f4b4d6f7700, child_tidptr=0x7f4b4d6f79d0) = 12434
+12433 munmap(0x7f4b48ef7000, 67108864 <unfinished ...>
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0 <unfinished ...>
+12434 set_robust_list(0x7f4b4d6f79e0, 24 <unfinished ...>
+12429 <... mmap resumed> ) = 0x7f4b4c6f6000
+12434 <... set_robust_list resumed> ) = 0
+12429 mprotect(0x7f4b4c6f7000, 8388608, PROT_READ|PROT_WRITE <unfinished ...>
+12434 prctl(PR_SET_NAME, "node2_SIM" <unfinished ...>
+12429 <... mprotect resumed> ) = 0
+12434 <... prctl resumed> ) = 0
+12433 <... munmap resumed> ) = 0
+12429 clone( <unfinished ...>
+12434 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12433 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12434 <... mmap resumed> ) = 0x7f4b3cef7000
+12435 set_robust_list(0x7f4b4cef69e0, 24 <unfinished ...>
+12434 munmap(0x7f4b3cef7000, 51417088 <unfinished ...>
+12435 <... set_robust_list resumed> ) = 0
+12433 <... mmap resumed> ) = 0x7f4b34ef7000
+12435 prctl(PR_SET_NAME, "node3_SIM" <unfinished ...>
+12429 <... clone resumed> child_stack=0x7f4b4cef5fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4b4cef69d0, tls=0x7f4b4cef6700, child_tidptr=0x7f4b4cef69d0) = 12435
+12435 <... prctl resumed> ) = 0
+12429 mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0 <unfinished ...>
+12435 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12429 <... mmap resumed> ) = 0x7f4b4bef5000
+12435 <... mmap resumed> ) = 0x7f4b2cef7000
+12429 mprotect(0x7f4b4bef6000, 8388608, PROT_READ|PROT_WRITE <unfinished ...>
+12435 munmap(0x7f4b2cef7000, 51417088 <unfinished ...>
+12434 <... munmap resumed> ) = 0
+12435 <... munmap resumed> ) = 0
+12434 munmap(0x7f4b44000000, 15691776 <unfinished ...>
+12429 <... mprotect resumed> ) = 0
+12435 munmap(0x7f4b34000000, 15691776 <unfinished ...>
+12434 <... munmap resumed> ) = 0
+12435 <... munmap resumed> ) = 0
+12429 clone( <unfinished ...>
+12435 mprotect(0x7f4b30000000, 135168, PROT_READ|PROT_WRITE <unfinished ...>
+12434 mprotect(0x7f4b40000000, 135168, PROT_READ|PROT_WRITE <unfinished ...>
+12435 <... mprotect resumed> ) = 0
+12429 <... clone resumed> child_stack=0x7f4b4c6f4fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f4b4c6f59d0, tls=0x7f4b4c6f5700, child_tidptr=0x7f4b4c6f59d0) = 12436
+12434 <... mprotect resumed> ) = 0
+12429 futex(0x208778c, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12435 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12433 munmap(0x7f4b34ef7000, 51417088 <unfinished ...>
+12435 <... clock_gettime resumed> {tv_sec=2, tv_nsec=548489603}) = 0
+12435 futex(0x208778c, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12436 set_robust_list(0x7f4b4c6f59e0, 24 <unfinished ...>
+12434 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12436 <... set_robust_list resumed> ) = 0
+12434 <... clock_gettime resumed> {tv_sec=2, tv_nsec=548554155}) = 0
+12436 prctl(PR_SET_NAME, "node4_SIM" <unfinished ...>
+12434 futex(0x208778c, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12436 <... prctl resumed> ) = 0
+12433 <... munmap resumed> ) = 0
+12436 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0 <unfinished ...>
+12433 munmap(0x7f4b3c000000, 15691776 <unfinished ...>
+12436 <... mmap resumed> ) = 0x7f4b28000000
+12433 <... munmap resumed> ) = 0
+12436 munmap(0x7f4b2c000000, 67108864 <unfinished ...>
+12433 mprotect(0x7f4b38000000, 135168, PROT_READ|PROT_WRITE <unfinished ...>
+12436 <... munmap resumed> ) = 0
+12433 <... mprotect resumed> ) = 0
+12436 mprotect(0x7f4b28000000, 135168, PROT_READ|PROT_WRITE <unfinished ...>
+12432 munmap(0x7f4b44ef7000, 67108864 <unfinished ...>
+12436 <... mprotect resumed> ) = 0
+12433 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12432 <... munmap resumed> ) = 0
+12436 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12433 <... clock_gettime resumed> {tv_sec=2, tv_nsec=548717349}) = 0
+12436 <... clock_gettime resumed> {tv_sec=2, tv_nsec=548765067}) = 0
+12433 futex(0x208778c, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12436 futex(0x208778c, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12432 mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f4b20000000
+12432 munmap(0x7f4b24000000, 67108864) = 0
+12432 mprotect(0x7f4b20000000, 135168, PROT_READ|PROT_WRITE) = 0
+12432 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, {tv_sec=2, tv_nsec=548872449}) = 0
+12432 futex(0x208778c, FUTEX_WAKE_PRIVATE, 2147483647 <unfinished ...>
+12429 <... futex resumed> ) = 0
+12436 <... futex resumed> ) = 0
+12429 futex(0x208778c, FUTEX_WAIT_PRIVATE, 6, NULL <unfinished ...>
+12436 futex(0x208778c, FUTEX_WAIT_PRIVATE, 6, NULL <unfinished ...>
+12435 <... futex resumed> ) = 0
+12434 <... futex resumed> ) = 0
+12435 futex(0x208778c, FUTEX_WAIT_PRIVATE, 6, NULL <unfinished ...>
+12434 futex(0x208778c, FUTEX_WAIT_PRIVATE, 6, NULL <unfinished ...>
+12433 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 5
+12433 futex(0x208778c, FUTEX_WAIT_PRIVATE, 6, NULL <unfinished ...>
+12432 futex(0x208778c, FUTEX_WAKE_PRIVATE, 2147483647 <unfinished ...>
+12433 <... futex resumed> ) = -1 EAGAIN (Resource temporarily unavailable)
+12429 <... futex resumed> ) = 0
+12436 <... futex resumed> ) = 0
+12435 <... futex resumed> ) = 0
+12436 futex(0x1475c070, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12435 futex(0x10292840, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12434 <... futex resumed> ) = 0
+12433 futex(0x78cf9b0, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12434 futex(0xbda97d0, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12432 <... futex resumed> ) = 4
+12432 futex(0x36116f0, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12429 openat(AT_FDCWD, "./ocamlrun", O_RDONLY) = 4
+12429 lseek(4, 0, SEEK_END) = 2291768
+12429 mmap(NULL, 2291768, PROT_READ|PROT_WRITE, MAP_PRIVATE, 4, 0) = 0x7f4c68369000
+12429 lseek(4, 65536, SEEK_SET) = 65536
+12429 brk(0x19472000) = 0x19472000
+12429 read(4, "\0\0\0\340\0\0\0\200\0\30\0\0@\0\270\17\205\2\304\17\305\362\3d\0\264\5\361\0\0\0\0"..., 467144) = 467144
+12429 lseek(4, 540872, SEEK_SET) = 540872
+12429 read(4, "\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0", 32) = 32
+12429 lseek(4, 540904, SEEK_SET) = 540904
+12429 read(4, "", 0) = 0
+12429 lseek(4, 589824, SEEK_SET) = 589824
+12429 read(4, "", 0) = 0
+12429 lseek(4, 589824, SEEK_SET) = 589824
+12429 read(4, "", 0) = 0
+12429 munmap(0x7f4c68369000, 2291768) = 0
+12429 close(4) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 getcwd("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun", 4096) = 54
+12429 lstat("/home/monniaux/work/Kalray/tests/ocaml-4.07.1/byterun/ocamlrun", {st_mode=S_IFREG|0755, st_size=2291768, ...}) = 0
+12429 futex(0x36116f0, FUTEX_WAKE_PRIVATE, 1) = 1
+12432 <... futex resumed> ) = 0
+12432 futex(0x36116a0, FUTEX_WAKE_PRIVATE, 1) = 0
+12432 mprotect(0x7f4b20021000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20022000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20023000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20025000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20027000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20029000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2002a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2002c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2002e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20030000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20031000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20033000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20035000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20037000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20038000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2003a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2003c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2003e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2003f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20041000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20043000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20045000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20047000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20048000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2004a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2004c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2004e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2004f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20051000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20053000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20055000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20056000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20058000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2005a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2005c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2005d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2005f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20061000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20063000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20064000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20066000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20068000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2006a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2006c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2006d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2006f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20071000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20073000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20074000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20076000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20078000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2007a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2007b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2007d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2007f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20081000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20082000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20084000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20086000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20088000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20089000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2008b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2008d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2008f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20091000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20092000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20094000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20096000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20098000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20099000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2009b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2009d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2009f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12429 futex(0x20877f8, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
+12432 mprotect(0x7f4b200a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200af000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200bd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200db000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200e9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200f9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b200fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20100000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20101000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20103000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20105000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20107000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20108000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2010a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2010c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2010e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2010f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20111000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20113000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20115000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20116000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20118000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2011a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2011c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2011e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2011f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20121000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20123000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20125000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20126000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20128000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2012a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2012c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2012d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2012f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20131000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20133000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20134000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20136000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20138000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2013a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2013b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2013d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2013f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20141000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20143000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20144000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20146000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20148000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2014a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2014b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2014d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2014f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20151000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20152000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20154000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20156000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20158000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20159000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2015b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2015d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2015f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20160000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20162000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20164000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20166000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20168000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20169000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2016b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2016d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2016f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20170000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20172000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20174000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20176000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20177000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20179000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2017b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2017d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2017e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20180000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20182000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20184000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20185000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20187000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20189000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2018b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2018d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2018e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20190000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20192000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20194000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20195000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20197000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20199000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2019b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2019c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2019e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201a9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201b9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201de000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201e5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201f3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b201ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20201000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20203000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20204000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20206000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20208000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2020a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2020b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2020d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2020f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20211000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20212000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20214000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20216000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20218000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20219000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2021b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2021d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2021f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20220000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20222000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20224000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20226000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20228000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20229000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2022b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2022d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2022f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20230000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20232000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20234000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20236000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20237000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20239000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2023b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2023d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2023e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20240000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20242000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20244000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20245000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20247000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20249000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2024b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2024d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2024e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20250000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20252000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20254000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20255000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20257000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20259000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2025b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2025c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2025e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20260000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20262000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20263000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20265000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20267000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20269000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2026a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2026c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2026e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20270000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20272000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20273000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20275000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20277000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20279000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2027a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2027c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2027e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20280000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20281000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20283000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20285000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20287000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20288000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2028a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2028c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2028e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2028f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20291000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20293000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20295000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20297000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20298000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2029a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2029c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2029e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2029f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202a5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202bc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202c3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202ef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202fd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b202fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20300000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20302000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20304000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20306000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20307000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20309000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2030b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2030d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2030e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20310000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20312000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20314000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20315000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20317000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20319000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2031b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2031c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2031e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20320000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20322000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20323000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20325000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20327000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20329000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2032b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2032c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2032e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20330000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20332000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20333000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20335000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20337000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20339000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2033a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2033c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2033e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20340000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20341000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20343000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20345000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20347000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20348000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2034a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2034c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2034e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20350000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20351000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20353000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20355000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20357000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20358000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2035a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2035c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2035e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2035f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20361000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20363000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20365000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20366000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20368000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2036a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2036c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2036d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2036f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20371000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20373000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20375000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20376000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20378000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2037a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2037c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2037d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2037f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20381000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20383000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20384000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20386000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20388000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2038a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2038b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2038d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2038f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20391000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20392000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20394000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20396000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20398000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2039a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2039b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2039d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2039f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203af000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203db000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203f9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b203fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20400000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20401000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20403000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20405000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20407000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20408000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2040a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2040c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2040e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20410000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20411000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20413000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20415000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20417000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20418000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2041a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2041c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2041e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2041f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20421000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20423000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20425000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20426000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20428000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2042a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2042c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2042d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2042f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20431000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20433000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20435000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20436000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20438000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2043a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2043c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2043d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2043f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20441000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20443000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20444000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20446000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20448000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2044a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2044b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2044d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2044f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20451000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20452000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20454000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20456000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20458000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2045a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2045b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2045d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2045f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20461000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20462000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20464000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20466000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20468000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20469000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2046b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2046d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2046f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20470000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20472000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20474000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20476000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20477000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20479000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2047b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2047d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2047f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20480000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20482000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20484000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20486000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20487000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20489000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2048b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2048d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2048e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20490000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20492000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20494000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20495000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20497000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20499000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2049b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2049c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2049e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204b9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204de000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204e5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b204ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20501000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20503000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20504000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20506000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20508000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2050a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2050b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2050d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2050f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20511000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20513000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20514000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20516000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20518000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2051a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2051b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2051d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2051f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20521000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20522000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20524000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20526000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20528000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20529000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2052b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2052d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2052f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20530000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20532000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20534000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20536000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20538000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20539000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2053b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2053d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2053f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20540000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20542000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20544000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20546000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20547000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20549000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2054b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2054d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2054e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20550000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20552000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20554000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20555000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20557000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20559000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2055b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2055d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2055e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20560000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20562000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20564000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20565000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20567000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20569000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2056b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2056c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2056e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20570000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20572000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20573000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20575000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20577000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20579000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2057a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2057c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2057e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20580000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20582000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20583000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20585000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20587000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20589000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2058a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2058c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2058e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20590000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20591000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20593000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20595000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20597000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20598000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2059a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2059c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2059e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2059f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205a5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205bc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205c3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b205ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20600000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20602000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20604000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20606000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20607000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20609000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2060b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2060d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2060e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20610000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20612000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20614000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20615000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20617000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20619000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2061b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2061d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2061e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20620000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20622000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20624000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20625000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20627000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20629000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2062b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2062c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2062e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20630000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20632000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20633000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20635000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20637000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20639000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2063a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2063c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2063e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20640000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20642000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20643000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20645000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20647000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20649000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2064a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2064c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2064e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20650000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20651000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20653000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20655000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20657000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20658000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2065a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2065c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2065e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2065f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20661000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20663000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20665000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20667000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20668000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2066a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2066c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2066e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2066f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20671000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20673000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20675000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20676000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20678000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2067a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2067c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2067d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2067f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20681000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20683000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20684000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20686000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20688000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2068a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2068c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2068d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2068f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20691000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20693000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20694000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20696000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20698000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2069a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2069b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2069d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2069f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b206fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20700000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20702000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20703000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20705000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20707000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20709000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2070a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2070c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2070e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20710000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20711000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20713000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20715000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20717000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20718000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2071a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2071c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2071e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20720000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20721000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20723000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20725000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20727000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20728000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2072a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2072c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2072e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2072f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20731000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20733000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20735000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20736000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20738000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2073a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2073c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2073d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2073f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20741000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20743000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20745000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20746000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20748000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2074a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2074c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2074d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2074f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20751000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20753000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20754000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20756000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20758000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2075a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2075b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2075d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2075f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20761000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20762000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20764000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20766000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20768000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20769000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2076b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2076d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2076f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20771000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20772000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20774000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20776000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20778000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20779000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2077b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2077d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2077f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20780000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20782000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20784000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20786000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20787000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20789000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2078b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2078d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2078e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20790000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20792000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20794000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20796000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20797000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20799000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2079b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2079d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2079e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b207ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20801000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20803000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20805000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20806000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20808000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2080a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2080c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2080d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2080f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20811000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20813000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20814000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20816000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20818000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2081a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2081b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2081d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2081f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20821000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20822000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20824000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20826000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20828000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2082a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2082b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2082d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2082f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20831000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20832000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20834000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20836000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20838000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20839000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2083b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2083d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2083f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20840000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20842000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20844000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20846000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20847000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20849000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2084b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2084d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2084f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20850000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20852000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20854000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20856000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20857000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20859000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2085b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2085d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2085e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20860000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20862000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20864000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20865000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20867000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20869000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2086b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2086c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2086e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20870000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20872000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20874000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20875000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20877000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20879000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2087b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2087c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2087e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20880000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20882000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20883000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20885000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20887000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20889000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2088a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2088c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2088e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20890000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20891000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20893000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20895000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20897000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20899000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2089a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2089c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2089e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b208ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20900000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20902000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20904000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20906000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20908000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20909000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2090b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2090d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2090f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20910000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20912000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20914000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20916000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20917000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20919000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2091b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2091d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2091e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20920000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20922000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20924000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20925000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20927000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20929000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2092b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2092c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2092e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20930000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20932000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20934000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20935000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20937000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20939000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2093b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2093c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2093e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20940000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20942000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20943000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20945000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20947000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20949000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2094a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2094c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2094e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20950000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20951000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20953000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20955000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20957000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20959000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2095a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2095c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2095e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20960000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20961000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20963000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20965000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20967000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20968000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2096a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2096c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2096e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2096f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20971000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20973000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20975000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20976000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20978000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2097a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2097c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2097e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2097f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20981000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20983000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20985000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20986000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20988000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2098a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2098c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2098d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2098f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20991000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20993000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20994000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20996000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20998000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2099a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2099b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2099d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2099f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b209fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a02000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a09000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a12000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a19000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a20000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a27000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a2e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a37000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a3e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a45000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a4c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a53000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a5c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a63000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a6a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a71000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a78000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a81000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a88000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a8f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a96000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a9d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20a9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aa9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ab9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20abb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20abc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20abe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ac9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20acb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20acc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ace000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ad9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ada000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20adc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ade000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ae8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20af1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20af3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20af5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20af7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20af8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20afa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20afc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20afe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20aff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b05000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b0c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b13000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b1c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b23000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b2a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b31000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b38000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b41000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 openat(AT_FDCWD, "./ocamlrun", O_RDONLY|O_NONBLOCK) = 4
+12432 fcntl(4, F_SETFL, O_RDONLY) = 0
+12432 mprotect(0x7f4b20b42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b48000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b4f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b56000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b66000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b6d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "\177E", 2) = 2
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b20b74000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b7b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b8b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b92000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b99000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20b9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ba0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ba1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -16, SEEK_END) = 2291752
+12432 mprotect(0x7f4b20ba3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ba5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ba7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ba9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20baa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bbc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bbe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 16
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b20bc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bc3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bc5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bcc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bd3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bd5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bdc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20be1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20be3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20be4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20be6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20be8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20beb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bf1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bf3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bf6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bf8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bfa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bfd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20bff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c01000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c08000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c0f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 close(4) = 0
+12432 mprotect(0x7f4b20c1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c1f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c26000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c2d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c34000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c44000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c4b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c52000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c59000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c69000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c70000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c77000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c7e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c8e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c95000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c9c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20c9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ca1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ca3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ca4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ca6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ca8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20caa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20caf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 openat(AT_FDCWD, "/home/monniaux/work/caml/quicksort", O_RDONLY|O_NONBLOCK) = 4
+12432 fcntl(4, F_SETFL, O_RDONLY) = 0
+12432 lseek(4, -16, SEEK_END) = 146122
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "\0\0\0\7Caml1999X023", 16) = 16
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b20cbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cc9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ccb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ccd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ccf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cdf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ce9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ceb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ced000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cf9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20cfd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -72, SEEK_END) = 146066
+12432 mprotect(0x7f4b20cfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d04000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "CODE\0\2\3\230DLPT\0\0\0\0DLLS\0\0\0\0PRIM\0\0\35\177"..., 56) = 56
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b20d05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d0b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d29000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d30000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d37000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d4e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d55000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d5c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d73000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d7a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d81000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d98000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20d9f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20da9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20daf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20db9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dbd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dbe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dc9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dcb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dcc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ddb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ddc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dde000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20de9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20df9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dfc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20dfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e0e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e15000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e33000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e3a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e58000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e7d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20e9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ea9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eaa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ebc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ebe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ec8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ecc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ece000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ecf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ed1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ed3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ed5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ed7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ed8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20edc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ede000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20edf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ee1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ee3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ee5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ee6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ee8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ef1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ef3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ef4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ef6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ef8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20efa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20efb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20efd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20eff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f11000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f1f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f36000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f44000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f5b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f69000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f80000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f8e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20f9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fa1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fa3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fa5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fa6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fa8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20faa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20faf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fcd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fe9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20feb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20fef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ff9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ffb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ffd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b20ffe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21000000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21002000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21004000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21006000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21007000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21009000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2100b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2100d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2100e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21010000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21012000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21014000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21015000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21017000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21019000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2101b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2101c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2101e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21020000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21022000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21023000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21025000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21027000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21029000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2102b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2102c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2102e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21030000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21032000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21033000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21035000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21037000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21039000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2103a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2103c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2103e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21040000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21041000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21043000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21045000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21047000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21048000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2104a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2104c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2104e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21050000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21051000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21053000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21055000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21057000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21058000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2105a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2105c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2105e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2105f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21061000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21063000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21065000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21066000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21068000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2106a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2106c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2106d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2106f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21071000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21073000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21075000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21076000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21078000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2107a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2107c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2107d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2107f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21081000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21083000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21084000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21086000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21088000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2108a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2108b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2108d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2108f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21091000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21092000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21094000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21096000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21098000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2109a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2109b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2109d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2109f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210af000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210bd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210db000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210f9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b210fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21100000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21101000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21103000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21105000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21107000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21108000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2110a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2110c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2110e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21110000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21111000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21113000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21115000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21117000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21118000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2111a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2111c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2111e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2111f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21121000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21123000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21125000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21126000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21128000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2112a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2112c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2112d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2112f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21131000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21133000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21135000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21136000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21138000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2113a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2113c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2113d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2113f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21141000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21143000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21144000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21146000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21148000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2114a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2114b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2114d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2114f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21151000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21152000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21154000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21156000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21158000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2115a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2115b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2115d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2115f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21161000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21162000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21164000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21166000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21168000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21169000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2116b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2116d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2116f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21170000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21172000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21174000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21176000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21177000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21179000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2117b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2117d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2117f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21180000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21182000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21184000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21186000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21187000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21189000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2118b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2118d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2118e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21190000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21192000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21194000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21195000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21197000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21199000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2119b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2119c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2119e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211b9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211de000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211e5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b211ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21201000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21203000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21204000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21206000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21208000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2120a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2120b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2120d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2120f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21211000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21213000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21214000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21216000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21218000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2121a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2121b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2121d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2121f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21221000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21222000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21224000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21226000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21228000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21229000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2122b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2122d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2122f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21230000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21232000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21234000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21236000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21238000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21239000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2123b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2123d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2123f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21240000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21242000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21244000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21246000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21247000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21249000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2124b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2124d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2124e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21250000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21252000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21254000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21255000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21257000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21259000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2125b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2125d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2125e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21260000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21262000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21264000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21265000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21267000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21269000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2126b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2126c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2126e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21270000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21272000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21273000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21275000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21277000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21279000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2127a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2127c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2127e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21280000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21281000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21283000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21285000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21287000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21289000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2128a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2128c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2128e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21290000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21291000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21293000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21295000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21297000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21298000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2129a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2129c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2129e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2129f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212a5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212bc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212c3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b212ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21300000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21302000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21304000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21306000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21307000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21309000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2130b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2130d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2130e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21310000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21312000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21314000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21315000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21317000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21319000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2131b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2131d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2131e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21320000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21322000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21324000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21325000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21327000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21329000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2132b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2132c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2132e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21330000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21332000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21333000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21335000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21337000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21339000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2133a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2133c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2133e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21340000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21342000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21343000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21345000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21347000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21349000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2134a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2134c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2134e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21350000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21351000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21353000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21355000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21357000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21358000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2135a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2135c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2135e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2135f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21361000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21363000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21365000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21367000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21368000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2136a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2136c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2136e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2136f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21371000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21373000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21375000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21376000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21378000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2137a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2137c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2137d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2137f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21381000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21383000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21384000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21386000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21388000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2138a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2138c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2138d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2138f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21391000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21393000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21394000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21396000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21398000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2139a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2139b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2139d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2139f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b213fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21400000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21402000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21403000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21405000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21407000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21409000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2140a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2140c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2140e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21410000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21411000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21413000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21415000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21417000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21418000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2141a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2141c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2141e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21420000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21421000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21423000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21425000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21427000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21428000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2142a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2142c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2142e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2142f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21431000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21433000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21435000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21436000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21438000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2143a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2143c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2143d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2143f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21441000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21443000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21444000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21446000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21448000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2144a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2144c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2144d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2144f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21451000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21453000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21454000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21456000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21458000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2145a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2145b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2145d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2145f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21461000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21462000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21464000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21466000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21468000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21469000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2146b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2146d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2146f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21471000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21472000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21474000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21476000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21478000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21479000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2147b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2147d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2147f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21480000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21482000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21484000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21486000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21487000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21489000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2148b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2148d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2148e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21490000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21492000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21494000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21496000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21497000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21499000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2149b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2149d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2149e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b214ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21501000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21503000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21505000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21506000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21508000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2150a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2150c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2150d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2150f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21511000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21513000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21514000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21516000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21518000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2151a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2151b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2151d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2151f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21521000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21522000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21524000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21526000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21528000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2152a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2152b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2152d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2152f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21531000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21532000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21534000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21536000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21538000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21539000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2153b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2153d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2153f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21540000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21542000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21544000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21546000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21547000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21549000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2154b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2154d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2154f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21550000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21552000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21554000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21556000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21557000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21559000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2155b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2155d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2155e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21560000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21562000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21564000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21565000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21567000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21569000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2156b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2156c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2156e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21570000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21572000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21574000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21575000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21577000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21579000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2157b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2157c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2157e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21580000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21582000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21583000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21585000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21587000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21589000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2158a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2158c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2158e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21590000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21591000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21593000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21595000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21597000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21599000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2159a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2159c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2159e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 write(2, "allocating ", 11) = 11
+12432 mprotect(0x7f4b215df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b215ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21600000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21602000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21604000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21606000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21608000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21609000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2160b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2160d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2160f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21610000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21612000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21614000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21616000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21617000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 write(2, "491520", 6) = 6
+12432 mprotect(0x7f4b21619000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2161b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2161d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2161e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21620000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21622000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21624000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21625000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21627000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21629000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2162b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2162d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2162e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21630000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21632000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21634000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21635000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21637000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21639000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2163b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2163c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2163e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21640000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21642000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21643000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21645000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21647000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21649000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2164a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2164c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2164e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21650000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21652000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21653000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21655000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21657000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21659000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2165a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2165c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2165e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21660000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21661000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21663000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21665000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21667000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21668000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2166a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2166c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2166e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2166f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21671000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21673000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21675000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21677000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21678000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2167a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 write(2, "\n", 1) = 1
+12432 mprotect(0x7f4b2167c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2167e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2167f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21681000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21683000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21685000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21686000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21688000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2168a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2168c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2168d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2168f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21691000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21693000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21694000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21696000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21698000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2169a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2169c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2169d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2169f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b216fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21700000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21702000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21703000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21705000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21707000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21709000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2170a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2170c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2170e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21710000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21712000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21713000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21715000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21717000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21719000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2171a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2171c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2171e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21720000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21721000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21723000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21725000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21727000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21728000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2172a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2172c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2172e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2172f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21731000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21733000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21735000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21737000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21738000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2173a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2173c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2173e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2173f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21741000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21743000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21745000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21746000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21748000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2174a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2174c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2174d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2174f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21751000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21753000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21754000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21756000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21758000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2175a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2175c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2175d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2175f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21761000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21763000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21764000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21766000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21768000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2176a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2176b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2176d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2176f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21771000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21772000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21774000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21776000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21778000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21779000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2177b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2177d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2177f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21781000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21782000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21784000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21786000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21788000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21789000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2178b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2178d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2178f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21790000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21792000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21794000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21796000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21797000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21799000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2179b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2179d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2179e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217d9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b217ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21801000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21803000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21805000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21806000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21808000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2180a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2180c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2180d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2180f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21811000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21813000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21815000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21816000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21818000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2181a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2181c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2181d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2181f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21821000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21823000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21824000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21826000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21828000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2182a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2182b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2182d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2182f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21831000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21832000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21834000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21836000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21838000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2183a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2183b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2183d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2183f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21841000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21842000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21844000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21846000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21848000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21849000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2184b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2184d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2184f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21850000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21852000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21854000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21856000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21857000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21859000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2185b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2185d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2185f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21860000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21862000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21864000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21866000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21867000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21869000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2186b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2186d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2186e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21870000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21872000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21874000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21875000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21877000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21879000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2187b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2187c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2187e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21880000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21882000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21884000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21885000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21887000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21889000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2188b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2188c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2188e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21890000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21892000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21893000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21895000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21897000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21899000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2189a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2189c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2189e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218d5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218dc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218fa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b218ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21901000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21902000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21904000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21906000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21908000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21909000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2190b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2190d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2190f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21910000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21912000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21914000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21916000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21917000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21919000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2191b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2191d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2191f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21920000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21922000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21924000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21926000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21927000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21929000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2192b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2192d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2192e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21930000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21932000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21934000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21935000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21937000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21939000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2193b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2193c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2193e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21940000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21942000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21944000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21945000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21947000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21949000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2194b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2194c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2194e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21950000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21952000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21953000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21955000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21957000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21959000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2195a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2195c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2195e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21960000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21961000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21963000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21965000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21967000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21969000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2196a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2196c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2196e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21970000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21971000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21973000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21975000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21977000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21978000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2197a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2197c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2197e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2197f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21981000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21983000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21985000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21986000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21988000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2198a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2198c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2198e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2198f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21991000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21993000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21995000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21996000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21998000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2199a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2199c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2199d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2199f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219fd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b219fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a04000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a0b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a12000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a19000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a29000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a30000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a37000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a3e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a4e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a55000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a5c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a63000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a6a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a73000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a7a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -146099, SEEK_END) = 39
+12432 mprotect(0x7f4b21a81000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a88000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a8f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a98000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21a9f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aa9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ab0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ab2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ab4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ab5000, 135168, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "T\0\0\0\337\2\0\0\0\0\0\0W\0\0\0\1\0\17\0\20\0\0\0\23\0\0\0\34\0\0\0"..., 131992) = 131992
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b21ad6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ad7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ad9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ada000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21adc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ade000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ae9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21af8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21afa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21afc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21afe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21aff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b0e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b15000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b1c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b23000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b33000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b3a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b41000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b48000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b58000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b66000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b6d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b7d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b8b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b92000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21b9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ba9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21baa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bbc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bbe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bcc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bd3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bd5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bdc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21be1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21be3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21be5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21be6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21be8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bf1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bf3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bf6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bf8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bfa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bfd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21bff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c01000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c11000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c1f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c26000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c2d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c36000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c44000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c4b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c52000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c5b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c69000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c70000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c77000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c80000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c8e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c95000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c9c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21c9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ca1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ca3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ca5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ca6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ca8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21caa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21caf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cc1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ccb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ccd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ccf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cdf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ce9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ceb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ced000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cf9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cfd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21cfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d04000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d0b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d29000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d30000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d39000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d4e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d55000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d5e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d73000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d7a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d83000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d98000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21d9f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21da9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21daf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21db9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dbd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dbe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dc9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dcd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ddb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ddc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dde000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21de9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21df9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dfc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21dfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e0e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e15000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e33000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e3a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e58000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e7d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21e9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ea9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eaa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ebc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ebe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ec8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ecc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ece000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ecf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ed1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ed3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ed5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ed7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ed8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21edc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ede000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21edf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ee1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ee3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ee5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ee6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ee8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ef1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ef3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ef4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ef6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ef8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21efa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21efc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21efd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21eff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f11000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f21000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f36000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f46000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f5b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f6b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f80000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f90000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21f9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fa1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fa3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fa5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fa6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fa8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21faa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21faf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fcd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fe9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21feb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21fef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ff9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ffb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ffd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b21ffe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22000000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22002000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22004000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22006000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22007000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22009000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2200b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2200d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2200e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22010000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22012000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22014000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22015000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22017000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22019000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2201b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2201c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2201e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22020000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22022000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22023000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22025000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22027000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22029000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2202b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2202c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2202e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22030000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22032000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22033000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22035000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22037000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22039000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2203a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2203c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2203e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22040000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22041000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22043000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22045000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22047000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22048000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2204a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2204c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2204e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22050000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22051000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22053000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22055000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22057000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22058000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2205a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2205c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2205e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2205f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22061000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22063000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22065000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22066000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22068000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2206a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2206c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2206d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2206f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22071000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22073000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22075000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22076000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22078000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2207a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2207c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2207d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2207f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22081000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22083000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22084000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22086000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22088000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2208a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2208b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2208d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2208f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22091000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22092000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22094000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22096000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22098000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2209a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2209b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2209d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2209f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220af000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220db000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220f9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b220fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22100000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22101000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22103000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22105000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22107000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22109000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2210a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2210c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2210e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22110000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22111000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22113000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22115000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22117000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22118000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2211a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2211c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2211e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2211f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22121000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22123000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22125000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22126000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22128000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2212a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2212c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2212e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2212f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22131000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22133000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22135000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22136000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22138000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2213a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2213c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2213d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2213f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22141000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22143000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22144000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22146000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22148000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2214a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2214b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2214d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2214f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22151000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22153000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22154000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22156000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22158000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2215a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2215b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2215d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2215f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22161000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22162000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22164000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -14107, SEEK_END) = 132031
+12432 mprotect(0x7f4b22166000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22168000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22169000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2216b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2216d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2216f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22170000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22172000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22174000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22176000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22177000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22179000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2217b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2217d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "", 0) = 0
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b2217f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22180000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22182000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22184000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22186000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22187000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22189000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2218b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2218d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2218e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22190000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22192000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22194000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22195000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22197000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22199000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2219b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2219d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2219e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221b9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221de000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221e5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b221ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22201000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22203000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22204000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22206000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22208000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2220a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2220b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2220d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2220f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22211000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22213000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22214000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22216000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22218000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2221a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2221b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2221d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2221f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22221000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22222000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22224000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22226000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22228000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22229000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2222b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2222d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -14107, SEEK_END) = 132031
+12432 mprotect(0x7f4b2222f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22230000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22232000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22234000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22236000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22238000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22239000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2223b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2223d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2223f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22240000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22242000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22244000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22246000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "", 0) = 0
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b22247000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22249000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2224b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2224d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2224e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22250000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22252000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22254000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22255000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22257000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22259000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2225b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2225d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2225e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22260000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22262000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22264000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22265000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22267000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22269000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2226b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2226c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2226e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22270000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22272000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22273000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22275000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22277000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22279000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2227a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2227c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2227e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22280000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22282000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22283000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22285000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22287000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22289000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2228a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2228c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2228e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22290000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22291000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22293000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22295000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22297000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22298000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2229a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2229c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2229e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2229f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222bc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222c3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222e1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -14107, SEEK_END) = 132031
+12432 mprotect(0x7f4b222e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222e8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b222ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "caml_abs_float\0caml_acos_float\0c"..., 7551) = 7551
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b22300000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22302000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22304000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22306000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22307000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22309000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2230b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2230d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2230e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22310000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22312000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22314000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22316000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22317000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22319000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2231b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2231d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2231e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22320000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22322000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22324000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22325000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22327000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22329000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2232b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2232c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2232e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22330000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22332000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22333000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22335000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22337000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22339000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2233b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2233c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2233e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22340000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22342000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22343000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22345000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22347000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22349000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2234a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2234c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2234e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22350000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22351000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22353000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22355000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22357000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22358000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2235a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2235c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2235e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22360000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22361000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22363000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22365000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22367000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22368000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2236a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2236c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2236e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2236f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22371000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22373000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22375000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22376000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22378000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2237a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2237c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2237d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2237f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22381000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22383000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22384000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22386000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22388000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2238a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2238c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2238d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2238f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22391000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22393000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22394000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22396000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22398000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2239a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2239b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2239d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2239f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b223fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22400000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22402000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22403000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22405000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22407000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22409000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2240a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2240c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2240e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22410000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22411000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22413000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22415000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22417000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22418000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2241a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2241c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2241e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22420000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22421000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22423000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22425000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22427000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22428000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2242a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2242c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2242e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2242f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22431000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22433000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22435000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22436000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22438000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2243a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2243c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2243d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2243f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22441000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22443000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22445000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22446000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22448000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2244a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2244c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2244d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2244f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22451000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22453000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22454000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22456000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22458000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2245a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2245b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2245d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2245f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22461000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22462000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22464000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22466000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22468000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2246a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2246b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2246d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2246f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22471000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22472000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22474000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22476000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22478000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22479000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2247b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2247d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2247f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22480000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22482000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22484000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22486000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22487000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22489000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2248b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2248d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 stat("/opt/ccomp/ocaml/4.07.1/lib/ocaml/ld.conf", {st_mode=S_IFREG|0664, st_size=77, ...}) = 0
+12432 mprotect(0x7f4b2248f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22490000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22492000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22494000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22496000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22497000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22499000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2249b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2249d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2249e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224d9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 openat(AT_FDCWD, "/opt/ccomp/ocaml/4.07.1/lib/ocaml/ld.conf", O_RDONLY|O_NONBLOCK) = 5
+12432 fcntl(5, F_SETFL, O_RDONLY) = 0
+12432 mprotect(0x7f4b224e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(5, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(5, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(5, "/opt/ccomp/ocaml/4.07.1/lib/ocam"..., 77) = 77
+12432 fcntl(5, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b224f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b224ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22501000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22503000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22505000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22506000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22508000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2250a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2250c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2250d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2250f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22511000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22513000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22515000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22516000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22518000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2251a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2251c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2251d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2251f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22521000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22523000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22524000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22526000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22528000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2252a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2252b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2252d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2252f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22531000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22532000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22534000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22536000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22538000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22539000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 close(5) = 0
+12432 mprotect(0x7f4b2253b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2253d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2253f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22541000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22542000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22544000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22546000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22548000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22549000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2254b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2254d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2254f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22550000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22552000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22554000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22556000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22557000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22559000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2255b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2255d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2255e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22560000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22562000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22564000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22566000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22567000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22569000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2256b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2256d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2256e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22570000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22572000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22574000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22575000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22577000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22579000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2257b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2257c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2257e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22580000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22582000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22583000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22585000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22587000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22589000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2258b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2258c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2258e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22590000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22592000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22593000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22595000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22597000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22599000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2259a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2259c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2259e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225d5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225dc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225fa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b225ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22601000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22602000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22604000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22606000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22608000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22609000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2260b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2260d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2260f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22610000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22612000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22614000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22616000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22617000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22619000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2261b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2261d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2261f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22620000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22622000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22624000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22626000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22627000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22629000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2262b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2262d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2262e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22630000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22632000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22634000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22635000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22637000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22639000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2263b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2263c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2263e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22640000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22642000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22644000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22645000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22647000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22649000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2264b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2264c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2264e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22650000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22652000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22653000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22655000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22657000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22659000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2265a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2265c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2265e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22660000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22661000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22663000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22665000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22667000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22669000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2266a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2266c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2266e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22670000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22671000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22673000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22675000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22677000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22678000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2267a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2267c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2267e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, -6556, SEEK_END) = 139582
+12432 mprotect(0x7f4b2267f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22681000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22683000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22685000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22686000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22688000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2268a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2268c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2268e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2268f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22691000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22693000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22695000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22696000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22698000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2269a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2269c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2269d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2269f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(4, 0, SEEK_CUR) = 139582
+12432 mprotect(0x7f4b226eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b226fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22700000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22702000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22704000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22705000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22707000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22709000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2270b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2270c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2270e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22710000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22712000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22713000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22715000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22717000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22719000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2271a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2271c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2271e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22720000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22721000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22723000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22725000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22727000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22729000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2272a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2272c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2272e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22730000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22731000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22733000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22735000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22737000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22738000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2273a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2273c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2273e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2273f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22741000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22743000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22745000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22746000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22748000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2274a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2274c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2274e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2274f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22751000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22753000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22755000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22756000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22758000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2275a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2275c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2275d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2275f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22761000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22763000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22764000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22766000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22768000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2276a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2276b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2276d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2276f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22771000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22773000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22774000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22776000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22778000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2277a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2277b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2277d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2277f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22781000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22782000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22784000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22786000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22788000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22789000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2278b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2278d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2278f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22790000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22792000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22794000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22796000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22798000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22799000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2279b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2279d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2279f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227bd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227d2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227d7000, 65536, PROT_READ|PROT_WRITE) = 0
+12432 fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
+12432 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
+12432 read(4, "\204\225\246\276\0\0\22:\0\0\1\346\0\0\t\26\0\0\7K\10\0\4\360\0\10\0\0\10\370-O"..., 65536) = 6556
+12432 fcntl(4, F_SETFL, O_RDONLY|O_LARGEFILE) = 0
+12432 mprotect(0x7f4b227e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227e9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b227ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22801000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22803000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22805000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22807000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22808000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2280a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2280c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2280e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2280f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22811000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22813000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22815000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22816000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22818000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2281a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2281c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2281d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2281f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22821000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22823000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22824000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22826000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22828000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2282a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2282c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2282d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2282f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22831000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22833000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22834000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22836000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22838000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2283a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2283b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2283d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2283f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22841000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22842000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22844000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22846000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22848000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22849000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2284b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2284d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2284f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22851000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22852000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22854000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22856000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22858000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22859000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2285b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2285d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2285f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22860000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22862000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22864000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22866000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22867000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22869000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2286b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2286d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2286e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22870000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22872000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22874000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22876000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22877000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22879000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2287b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2287d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2287e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22880000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22882000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22884000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22885000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22887000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22889000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2288b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2288c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2288e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22890000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22892000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22893000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22895000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22897000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22899000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2289b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2289c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2289e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228a9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228c7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228d5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228dc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228f3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228fa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b228ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22901000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22902000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22904000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22906000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22908000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22909000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2290b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2290d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2290f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22911000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22912000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22914000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22916000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22918000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22919000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2291b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2291d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2291f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22920000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22922000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22924000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22926000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22927000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22929000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2292b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2292d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2292e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22930000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22932000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22934000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22936000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22937000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22939000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2293b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2293d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2293e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22940000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22942000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22944000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22945000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22947000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22949000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2294b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2294c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2294e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22950000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22952000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22953000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22955000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22957000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22959000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2295b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2295c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2295e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22960000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22962000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22963000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22965000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22967000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22969000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2296a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2296c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2296e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22970000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22971000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22973000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22975000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22977000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22978000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2297a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2297c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2297e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22980000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22981000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22983000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22985000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22987000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22988000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2298a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2298c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2298e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2298f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22991000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22993000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22995000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22996000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22998000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2299a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2299c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2299d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2299f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229a5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229ef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229fd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b229fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a04000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a0b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a29000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a30000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a39000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a4e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a55000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a5e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a73000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a7a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a81000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a98000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22a9f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aa9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aaf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ab9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22abb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22abd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22abe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ac9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22acb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22acc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ace000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ad9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22adb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22adc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ade000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ae9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22aee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22af9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22afa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22afc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22afe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b0e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b15000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b33000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b3a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b58000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b7d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22b9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ba9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22baa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bbc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bbe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bcc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bd3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bd7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bdc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bde000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22be1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22be3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22be5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22be6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22be8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bf1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bf3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bf6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bf8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bfc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bfd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22bff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c11000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c21000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c36000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c44000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c5b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c69000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c80000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c8e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22c9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ca1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ca3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ca5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ca6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ca8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22caa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22caf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ccb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ccd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ccf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ce9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ceb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ced000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cf9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cfd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22cfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d06000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d0d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 close(4) = 0
+12432 mprotect(0x7f4b22d15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d2b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d32000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d39000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d50000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d57000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d5e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d75000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d7c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d83000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d9a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22d9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22da9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22daf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22db9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dbf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dc9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dcd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ddb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ddc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dde000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22de9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22deb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22df9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dfc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22dfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e10000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e17000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e35000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e3c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e5a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e61000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e7f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e86000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22e9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ea9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ebc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ebe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ec9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ecc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ece000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ed8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22edc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ede000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22edf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ee1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ee3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ee5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ee6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ee8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ef1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ef3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ef5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ef6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ef8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22efa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22efc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22efd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22eff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f13000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f1a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f21000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f38000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f3f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f46000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f5d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f64000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f6b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f82000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f89000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f90000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22f9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fa1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fa3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fa5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fa7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fa8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22faa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22faf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fb5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fcd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fd3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fda000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fe9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22feb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ff9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ffb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22ffd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b22fff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23000000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23002000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23004000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23006000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23007000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23009000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2300b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2300d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2300e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23010000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23012000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23014000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23015000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23017000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23019000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2301b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2301d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2301e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23020000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23022000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23024000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23025000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23027000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23029000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2302b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2302c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2302e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23030000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23032000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23033000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23035000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23037000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23039000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2303a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2303c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2303e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23040000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23042000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23043000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23045000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23047000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23049000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2304a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2304c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2304e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23050000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23051000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23053000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23055000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23057000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23058000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2305a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2305c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2305e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2305f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23061000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23063000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23065000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23067000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23068000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2306a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2306c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2306e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2306f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23071000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23073000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23075000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23076000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23078000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2307a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2307c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2307d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2307f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23081000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23083000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23084000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23086000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23088000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2308a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2308c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2308d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2308f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23091000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23093000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23094000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23096000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23098000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2309a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2309b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2309d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2309f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230cd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230eb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b230fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23100000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23102000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23103000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23105000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23107000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23109000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2310a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2310c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2310e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23110000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23111000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23113000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23115000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23117000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23118000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2311a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2311c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2311e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23120000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23121000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23123000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23125000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23127000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23128000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2312a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2312c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2312e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2312f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23131000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23133000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23135000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23136000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23138000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2313a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2313c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2313d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2313f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23141000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23143000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23145000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23146000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23148000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2314a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2314c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2314d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2314f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23151000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23153000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23154000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23156000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23158000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2315a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2315b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2315d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2315f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23161000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23162000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23164000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23166000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23168000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2316a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2316b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2316d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2316f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23171000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23172000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23174000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23176000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23178000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23179000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2317b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2317d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2317f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23180000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23182000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23184000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23186000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23187000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23189000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2318b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2318d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2318e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23190000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23192000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23194000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23196000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23197000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23199000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2319b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(0, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
+12432 mprotect(0x7f4b2319d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2319e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231c9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231f5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231fc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b231ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23201000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23203000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23205000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23206000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23208000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2320a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2320c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2320d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2320f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23211000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23213000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23214000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23216000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23218000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2321a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2321b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2321d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2321f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23221000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23222000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23224000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23226000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23228000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2322a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2322b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2322d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2322f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23231000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23232000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23234000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23236000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23238000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23239000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2323b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2323d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2323f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23240000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23242000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23244000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23246000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23247000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23249000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2324b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2324d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2324f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23250000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23252000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23254000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23256000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23257000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23259000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2325b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2325d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2325e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23260000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23262000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23264000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23265000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23267000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23269000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2326b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2326c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2326e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23270000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23272000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23274000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23275000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23277000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23279000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2327b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2327c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2327e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23280000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23282000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23283000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23285000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23287000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23289000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2328a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2328c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2328e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23290000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23291000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23293000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23295000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23297000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23299000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2329a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2329c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2329e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232b5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232d3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232da000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b232ff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23300000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23302000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23304000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23306000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23308000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23309000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2330b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2330d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2330f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23310000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23312000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23314000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23316000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23317000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23319000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2331b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2331d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2331e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23320000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23322000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23324000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23325000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23327000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23329000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2332b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2332d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2332e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23330000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23332000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23334000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23335000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23337000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23339000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2333b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2333c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2333e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23340000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23342000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23343000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23345000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23347000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23349000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2334a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2334c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2334e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23350000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23351000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23353000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23355000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23357000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23359000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2335a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2335c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2335e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23360000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23361000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23363000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23365000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23367000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23368000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2336a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2336c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2336e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2336f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23371000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23373000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23375000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23376000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23378000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2337a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2337c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2337e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2337f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23381000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23383000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23385000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23386000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23388000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2338a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2338c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2338d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2338f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23391000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23393000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23394000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23396000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23398000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2339a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2339b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2339d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2339f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233bf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233dd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b233fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23400000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23402000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23403000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23405000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23407000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23409000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2340a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2340c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2340e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23410000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23412000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23413000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23415000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23417000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23419000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2341a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2341c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2341e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23420000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23421000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23423000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23425000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23427000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23428000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2342a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2342c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2342e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2342f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23431000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23433000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23435000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23437000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23438000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2343a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2343c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2343e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2343f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23441000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23443000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23445000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23446000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23448000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2344a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2344c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2344d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2344f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23451000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23453000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23454000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23456000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23458000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2345a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2345c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2345d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2345f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23461000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23463000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23464000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23466000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23468000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2346a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2346b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2346d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2346f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23471000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23472000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23474000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23476000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23478000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23479000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2347b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2347d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2347f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23481000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23482000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23484000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23486000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23488000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23489000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2348b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2348d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2348f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23490000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23492000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23494000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23496000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23497000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23499000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2349b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2349d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2349e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234d9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b234ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23501000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23503000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23505000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23506000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23508000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2350a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(1, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
+12432 mprotect(0x7f4b2350c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2350d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2350f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23511000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23513000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23514000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23516000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23518000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2351a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2351c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2351d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2351f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23521000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23523000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23524000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23526000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23528000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2352a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2352b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2352d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2352f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23531000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23532000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23534000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23536000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23538000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23539000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2353b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2353d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2353f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23541000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23542000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23544000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23546000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23548000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23549000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2354b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2354d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2354f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23550000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23552000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23554000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23556000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23557000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23559000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2355b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2355d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2355e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 lseek(2, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
+12432 mprotect(0x7f4b23560000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23562000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23564000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23566000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23567000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23569000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2356b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2356d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2356e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23570000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23572000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23574000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23575000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23577000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23579000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2357b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2357c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2357e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23580000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23582000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23583000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23585000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23587000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23589000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2358b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2358c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2358e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23590000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23592000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23593000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23595000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23597000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23599000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2359a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2359c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2359e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235c5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235cc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235d5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235dc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ea000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235f1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235fa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b235ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23601000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23602000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23604000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23606000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23608000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23609000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2360b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2360d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2360f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23610000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23612000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23614000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23616000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23617000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23619000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2361b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2361d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2361f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23620000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23622000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23624000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23626000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23627000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23629000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2362b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2362d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2362e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23630000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23632000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23634000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23635000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23637000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23639000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2363b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2363c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2363e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23640000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23642000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23644000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23645000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23647000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23649000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2364b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2364c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2364e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23650000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23652000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23653000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23655000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23657000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23659000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2365a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2365c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2365e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23660000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23661000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23663000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23665000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23667000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23669000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2366a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2366c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2366e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23670000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23671000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23673000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23675000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23677000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23678000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2367a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2367c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2367e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2367f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23681000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23683000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23685000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23686000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23688000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2368a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2368c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2368e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2368f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23691000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23693000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23695000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23696000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23698000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2369a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2369c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2369d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2369f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b236fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23700000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23702000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23704000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23705000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23707000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23709000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2370b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2370c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2370e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23710000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23712000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23713000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23715000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23717000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23719000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2371a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2371c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2371e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23720000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23721000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23723000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23725000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23727000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23729000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2372a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2372c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2372e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23730000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23731000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23733000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23735000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23737000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23738000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2373a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2373c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2373e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2373f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23741000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23743000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23745000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23746000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23748000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2374a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2374c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2374e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2374f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23751000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23753000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23755000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23756000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23758000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2375a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2375c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2375d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2375f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23761000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23763000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23764000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23766000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23768000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2376a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2376b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2376d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2376f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23771000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23773000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23774000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23776000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23778000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2377a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2377b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2377d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2377f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23781000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23782000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23784000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23786000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23788000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23789000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2378b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2378d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2378f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23790000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23792000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23794000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23796000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23798000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23799000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2379b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2379d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2379f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237bd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237d9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237e9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b237ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23801000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23803000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23805000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23807000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23808000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2380a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2380c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2380e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2380f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23811000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23813000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23815000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23816000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23818000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2381a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2381c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2381d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2381f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23821000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23823000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23824000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23826000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23828000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2382a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2382c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2382d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2382f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23831000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23833000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23834000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23836000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23838000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2383a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2383b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2383d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2383f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23841000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23842000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23844000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23846000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23848000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23849000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2384b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2384d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2384f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23851000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23852000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23854000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23856000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23858000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23859000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2385b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2385d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2385f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23860000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23862000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23864000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23866000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23867000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23869000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2386b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2386d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2386e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23870000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23872000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23874000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23876000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23877000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23879000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2387b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2387d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2387e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23880000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23882000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23884000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23885000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23887000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23889000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2388b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2388c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2388e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23890000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23892000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23893000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23895000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23897000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23899000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2389a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2389c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2389e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238a9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238be000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238c1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238c3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238c7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238d1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238d5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238d8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238dc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238df000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238e3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238e6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238e8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238f3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238f6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238fa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238fd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b238ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23901000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23902000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23904000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23906000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23908000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23909000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2390b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2390d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2390f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23911000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23912000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23914000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23916000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23918000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23919000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2391b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2391d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2391f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23920000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23922000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23924000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23926000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23927000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23929000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2392b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2392d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2392e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23930000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23932000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23934000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23936000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23937000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23939000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2393b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2393d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2393e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23940000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23942000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23944000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23945000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23947000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23949000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2394b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2394c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2394e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23950000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23952000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23953000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23955000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23957000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23959000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2395b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2395c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2395e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23960000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23962000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23963000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23965000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23967000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23969000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2396a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2396c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2396e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23970000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23971000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23973000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23975000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23977000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23978000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2397a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2397c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2397e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23980000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23981000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23983000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23985000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23987000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23988000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2398a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2398c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2398e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2398f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23991000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23993000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23995000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23996000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23998000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2399a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2399c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2399d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2399f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239a5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239b1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239b3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239c8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239cf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239ef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239fb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239fd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b239fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a04000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a0b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a29000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a30000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a39000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a4e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a55000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a5c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a73000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a7a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a81000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a98000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23a9f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aa9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aaf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ab9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23abb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23abd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23abe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ac9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23acb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23acc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ace000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ad9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23adb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23adc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ade000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ae9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23aee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23af9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23afa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23afc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23afe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b0e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b15000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b33000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b3a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b58000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b5f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b7d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b84000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23b9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ba9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23baa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bbc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bbe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bcc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bce000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bd3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bd7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bdc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bde000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23be1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23be3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23be5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23be6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23be8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bec000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bf1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bf3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bf6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bf8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bfc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bfd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23bff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c11000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c18000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c1f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c36000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c3d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c44000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c5b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c62000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c69000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c80000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c87000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c8e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23c9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ca1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ca3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ca5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ca6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ca8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23caa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cac000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23caf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cb3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cb4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ccb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ccd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ccf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ce9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ceb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ced000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cf9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cfb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cfd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23cfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d00000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d02000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d06000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d07000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d09000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d0d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d10000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d12000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d14000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d17000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d19000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d1b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d1e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d20000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d22000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d25000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d27000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d2b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d2c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d2e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d32000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d35000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d37000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d39000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d3c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d3e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d40000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d43000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d45000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d47000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d4a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d4c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d50000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d51000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d53000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d57000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d5a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d5c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d5e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d61000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d63000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d65000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d68000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d6a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d6c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d6f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d71000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d75000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d76000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d78000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d7c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d7f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d81000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d83000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d86000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d88000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d8a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d8d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d8f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d91000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d94000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d96000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d9a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d9b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d9d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23d9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23da9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23daf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23db9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dbb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dbf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dc9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dcd000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dd9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ddb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ddc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dde000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23de9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23deb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23df9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dfa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dfc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23dfe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e00000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e03000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e05000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e07000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e0a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e0c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e0e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e10000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e13000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e15000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e17000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e1a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e1c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e1e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e21000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e23000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e25000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e28000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e2a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e2c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e2f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e31000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e33000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e35000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e38000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e3a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e3c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e3f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e41000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e43000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e46000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e48000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e4a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e4d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e4f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e51000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e54000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e56000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e58000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e5a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e5d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e5f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e61000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e64000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e66000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e68000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e6b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e6d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e6f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e72000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e74000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e76000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e79000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e7b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e7d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e7f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e82000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e84000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e86000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e89000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e8b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e8d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e90000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e92000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e94000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e97000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e99000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e9b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23e9e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ea9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eab000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eb9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ebc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ebe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ec9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eca000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ecc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ece000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ed8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eda000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23edc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ede000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23edf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ee1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ee3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ee5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ee6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ee8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eee000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eef000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ef1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ef3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ef5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ef6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ef8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23efa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23efc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23efd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23eff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f01000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f03000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f04000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f06000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f08000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f0a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f0b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f0d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f0f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f11000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f13000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f14000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f16000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f18000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f1a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f1b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f1d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f1f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f21000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f22000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f24000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f26000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f28000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f29000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f2b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f2d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f2f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f30000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f32000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f34000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f36000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f38000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f39000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f3b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f3d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f3f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f40000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f42000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f44000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f46000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f47000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f49000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f4b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f4d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f4e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f50000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f52000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f54000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f55000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f57000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f59000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f5b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f5d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f5e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f60000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f62000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f64000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f65000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f67000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f69000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f6b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f6c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f6e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f70000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f72000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f73000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f75000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f77000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f79000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f7a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f7c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f7e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f80000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f82000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f83000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f85000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f87000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f89000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f8a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f8c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f8e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f90000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f91000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f93000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f95000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f97000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f98000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f9a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f9c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f9e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23f9f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fa1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fa3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fa5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fa6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fa8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23faa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fae000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23faf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fb1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fb3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fb5000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fb6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fb8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fba000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fbc000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fbd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fbf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fc1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fc3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fc4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fc6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fc8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fca000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fcb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fcd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fcf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fd1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fd3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fd4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fd6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fd8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fda000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fdb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fdd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fdf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fe9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23feb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fed000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fef000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ff9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ffb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23ffd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b23fff000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mmap(0x7f4b24000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f4b24000000
+12432 mprotect(0x7f4b24000000, 139264, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24022000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24024000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24026000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24027000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24029000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2402b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2402d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2402e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24030000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24032000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24034000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24035000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24037000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24039000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2403b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2403c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2403e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24040000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24042000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24043000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24045000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24047000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24049000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2404b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2404c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2404e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24050000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24052000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24053000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24055000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24057000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24059000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2405a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2405c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2405e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24060000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24061000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24063000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24065000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24067000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24068000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2406a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2406c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2406e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24070000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24071000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24073000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24075000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24077000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24078000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2407a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2407c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2407e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2407f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24081000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24083000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24085000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24086000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24088000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2408a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2408c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2408d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2408f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24091000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24093000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24094000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24096000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24098000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2409a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2409c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2409d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2409f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240a1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240a3000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 write(1, "-31; 0; 1; 2; 4; 65; 83; 99; 782"..., 35) = 35
+12432 mprotect(0x7f4b240a6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240a8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240aa000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240ad000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240af000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240b1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240b4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240b6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240b8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240ba000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240bb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240bd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240bf000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c1000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c6000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c8000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240cb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240cd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240cf000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240d9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240db000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240dd000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240df000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240e9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240eb000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240ed000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240f9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240fb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b240fe000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24100000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24102000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24103000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24105000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24107000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24109000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2410b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2410c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2410e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24110000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24112000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24113000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24115000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24117000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24119000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2411a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2411c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2411e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24120000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24121000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24123000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24125000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24127000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24128000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2412a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2412c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2412e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24130000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24131000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24133000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24135000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24137000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24138000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2413a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2413c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2413e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2413f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24141000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24143000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24145000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24146000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24148000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2414a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2414c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2414d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2414f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24151000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24153000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24155000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24156000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24158000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2415a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2415c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2415d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2415f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24161000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24163000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24164000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24166000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24168000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2416a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2416b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2416d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2416f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24171000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24172000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24174000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24176000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24178000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2417a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2417b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2417d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2417f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24181000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24182000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24184000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24186000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24188000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24189000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2418b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2418d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2418f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24190000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24192000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24194000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24196000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24197000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24199000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2419b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2419d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2419f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a4000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a6000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241a9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ab000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ad000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241b9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241bb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241bc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241be000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c2000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c4000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241c9000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241cb000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241cc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ce000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241d9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241da000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241dc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241de000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241e9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ea000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ec000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ee000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f1000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f7000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241f8000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241fa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241fc000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241fe000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b241ff000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24201000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24203000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24205000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24206000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24208000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2420a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2420c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2420e000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2420f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24211000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24213000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24215000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24216000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24218000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2421a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2421c000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2421d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2421f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24221000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24223000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24224000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24226000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24228000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2422a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2422b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2422d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2422f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24231000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24233000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24234000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24236000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24238000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2423a000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2423b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2423d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2423f000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24241000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24242000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24244000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24246000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24248000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24249000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2424b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2424d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2424f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24250000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24252000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24254000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24256000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24258000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24259000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2425b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2425d000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2425f000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24260000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24262000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24264000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24266000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24267000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24269000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2426b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2426d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2426e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24270000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24272000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24274000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24275000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24277000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24279000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2427b000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2427d000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2427e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24280000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24282000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24284000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24285000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24287000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24289000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2428b000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2428c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2428e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24290000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24292000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24293000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24295000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24297000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b24299000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2429a000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2429c000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b2429e000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a0000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a2000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a3000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a5000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a7000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242a9000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242aa000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242ac000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242ae000, 8192, PROT_READ|PROT_WRITE) = 0
+12432 mprotect(0x7f4b242b0000, 4096, PROT_READ|PROT_WRITE) = 0
+12432 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, {tv_sec=13, tv_nsec=793296369}) = 0
+12432 futex(0x78cf9b0, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12433 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 1
+12433 futex(0x78cf960, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12432 futex(0x78cf960, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12433 <... futex resumed> ) = -1 EAGAIN (Resource temporarily unavailable)
+12432 <... futex resumed> ) = 0
+12433 futex(0x78cf960, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12432 futex(0xbda97d0, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12433 <... futex resumed> ) = 0
+12434 <... futex resumed> ) = 0
+12433 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12434 futex(0xbda9780, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12433 <... clock_gettime resumed> {tv_sec=13, tv_nsec=793373038}) = 0
+12432 <... futex resumed> ) = 1
+12433 futex(0x20877a8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12432 futex(0xbda9780, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12434 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 1
+12434 futex(0xbda9780, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12432 futex(0x10292840, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12434 <... futex resumed> ) = 0
+12435 <... futex resumed> ) = 0
+12434 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12435 futex(0x102927f0, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12434 <... clock_gettime resumed> {tv_sec=13, tv_nsec=793425864}) = 0
+12432 <... futex resumed> ) = 1
+12434 futex(0x20877a8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12432 futex(0x102927f0, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12435 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 1
+12435 futex(0x102927f0, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12432 futex(0x1475c070, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12435 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 1
+12436 <... futex resumed> ) = 0
+12435 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12436 futex(0x1475c020, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12435 <... clock_gettime resumed> {tv_sec=13, tv_nsec=793479454}) = 0
+12436 <... futex resumed> ) = 0
+12435 futex(0x20877a8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12436 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, <unfinished ...>
+12432 futex(0x20877f8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12436 <... clock_gettime resumed> {tv_sec=13, tv_nsec=793494482}) = 0
+12429 <... futex resumed> ) = 0
+12436 futex(0x20877a8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12429 futex(0x20877a8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
+12432 <... futex resumed> ) = 1
+12432 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12433 <... futex resumed> ) = 0
+12432 <... futex resumed> ) = 1
+12433 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1) = 1
+12434 <... futex resumed> ) = 0
+12432 mprotect(0x7f4b242b1000, 4096, PROT_READ|PROT_WRITE <unfinished ...>
+12434 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12432 <... mprotect resumed> ) = 0
+12435 <... futex resumed> ) = 0
+12434 <... futex resumed> ) = 1
+12435 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12434 madvise(0x7f4b4cef7000, 8368128, MADV_DONTNEED <unfinished ...>
+12436 <... futex resumed> ) = 0
+12435 <... futex resumed> ) = 1
+12436 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
+12433 madvise(0x7f4b4d6f8000, 8368128, MADV_DONTNEED <unfinished ...>
+12435 madvise(0x7f4b4c6f6000, 8368128, MADV_DONTNEED <unfinished ...>
+12429 <... futex resumed> ) = 0
+12436 <... futex resumed> ) = 1
+12435 <... madvise resumed> ) = 0
+12429 futex(0x7f4c68d999d0, FUTEX_WAIT, 12432, NULL <unfinished ...>
+12436 madvise(0x7f4b4bef5000, 8368128, MADV_DONTNEED <unfinished ...>
+12435 exit(0 <unfinished ...>
+12436 <... madvise resumed> ) = 0
+12435 <... exit resumed>) = ?
+12436 exit(0 <unfinished ...>
+12434 <... madvise resumed> ) = 0
+12436 <... exit resumed>) = ?
+12435 +++ exited with 0 +++
+12436 +++ exited with 0 +++
+12434 exit(0 <unfinished ...>
+12433 <... madvise resumed> ) = 0
+12434 <... exit resumed>) = ?
+12433 exit(0 <unfinished ...>
+12434 +++ exited with 0 +++
+12433 <... exit resumed>) = ?
+12432 madvise(0x7f4c68599000, 8368128, MADV_DONTNEED <unfinished ...>
+12433 +++ exited with 0 +++
+12432 <... madvise resumed> ) = 0
+12432 exit(0) = ?
+12429 <... futex resumed> ) = 0
+12432 +++ exited with 0 +++
+12429 munmap(0x7f4c68599000, 8392704) = 0
+12429 futex(0x20877a8, FUTEX_WAKE_PRIVATE, 1) = 0
+12429 munmap(0x7f4c706f2000, 34607104) = 0
+12429 munmap(0x7f4c69eff000, 34607104) = 0
+12429 munmap(0x7f4c61eff000, 34607104) = 0
+12429 munmap(0x7f4b61efe000, 4294971392) = 0
+12429 munmap(0x7f4b24000000, 67108864) = 0
+12429 openat(AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY|O_CLOEXEC) = 4
+12429 read(4, "0", 1) = 1
+12429 close(4) = 0
+12429 madvise(0x7f4b22ceb000, 20008960, MADV_DONTNEED) = 0
+12429 madvise(0x7f4b20021000, 46964736, MADV_DONTNEED) = 0
+12429 munmap(0x7f4c68f9f000, 6819840) = 0
+12429 munmap(0x7f4b5defd000, 67112960) = 0
+12429 munmap(0x7f4b59efc000, 67112960) = 0
+12429 munmap(0x7f4b55efb000, 67112960) = 0
+12429 munmap(0x7f4b51efa000, 67112960) = 0
+12429 munmap(0x7f4c68d9a000, 2114360) = 0
+12429 munmap(0x7f4c7038d000, 3556520) = 0
+12429 munmap(0x7f4c70173000, 2202280) = 0
+12429 munmap(0x7f4c69a56000, 4885520) = 0
+12429 munmap(0x7f4c69848000, 2151080) = 0
+12429 munmap(0x7f4c69620000, 2259944) = 0
+12429 munmap(0x7f4b4def9000, 67112960) = 0
+12429 openat(AT_FDCWD, "/tmp/mppa-tlm-input-12429", O_WRONLY) = 4
+12431 <... openat resumed> ) = 3
+12429 close(4 <unfinished ...>
+12431 read(3, <unfinished ...>
+12429 <... close resumed> ) = 0
+12429 unlink("/tmp/mppa-tlm-input-12429" <unfinished ...>
+12431 <... read resumed> "", 1024) = 0
+12429 <... unlink resumed> ) = 0
+12431 madvise(0x7f4c727f3000, 8368128, MADV_DONTNEED <unfinished ...>
+12429 unlink("/tmp/mppa-tlm-output-12429" <unfinished ...>
+12431 <... madvise resumed> ) = 0
+12431 exit(0 <unfinished ...>
+12429 <... unlink resumed> ) = 0
+12431 <... exit resumed>) = ?
+12429 futex(0x7f4c72ff39d0, FUTEX_WAIT, 12431, NULL) = -1 EAGAIN (Resource temporarily unavailable)
+12431 +++ exited with 0 +++
+12429 munmap(0x7f4b4d6f8000, 8392704) = 0
+12429 brk(0x19bfe000) = 0x19bfe000
+12429 exit_group(0) = ?
+12430 <... rt_sigtimedwait resumed> <unfinished ...>) = ?
+12430 +++ exited with 0 +++
+12429 +++ exited with 0 +++
diff --git a/test/monniaux/ocaml/byterun/unix.c b/test/monniaux/ocaml/byterun/unix.c
new file mode 100644
index 00000000..15c67642
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/unix.c
@@ -0,0 +1,459 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 2001 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Unix-specific stuff */
+
+#define _GNU_SOURCE
+ /* Helps finding RTLD_DEFAULT in glibc */
+ /* also secure_getenv */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#if HAS_IOCTL
+#include <sys/ioctl.h>
+#endif
+#include <fcntl.h>
+#include "caml/config.h"
+#ifdef SUPPORT_DYNAMIC_LINKING
+#ifdef __CYGWIN__
+#include "flexdll.h"
+#else
+#include <dlfcn.h>
+#endif
+#endif
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#if HAS_DIR
+#ifdef HAS_DIRENT
+#include <dirent.h>
+#else
+#include <sys/dir.h>
+#endif
+#endif
+#ifdef __APPLE__
+#include <mach-o/dyld.h>
+#endif
+#include "caml/fail.h"
+#include "caml/memory.h"
+#include "caml/misc.h"
+#include "caml/osdeps.h"
+#include "caml/signals.h"
+#include "caml/sys.h"
+#include "caml/io.h"
+#include "caml/alloc.h"
+
+#ifndef S_ISREG
+#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
+#endif
+
+#ifndef EINTR
+#define EINTR (-1)
+#endif
+#ifndef EAGAIN
+#define EAGAIN (-1)
+#endif
+#ifndef EWOULDBLOCK
+#define EWOULDBLOCK (-1)
+#endif
+
+int caml_read_fd(int fd, int flags, void * buf, int n)
+{
+ int retcode;
+ do {
+ caml_enter_blocking_section();
+ retcode = read(fd, buf, n);
+ caml_leave_blocking_section();
+ } while (retcode == -1 && errno == EINTR);
+ if (retcode == -1) caml_sys_io_error(NO_ARG);
+ return retcode;
+}
+
+int caml_write_fd(int fd, int flags, void * buf, int n)
+{
+ int retcode;
+ again:
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ if (flags & CHANNEL_FLAG_BLOCKING_WRITE) {
+ retcode = write(fd, buf, n);
+ } else {
+#endif
+ caml_enter_blocking_section();
+ retcode = write(fd, buf, n);
+ caml_leave_blocking_section();
+#if defined(NATIVE_CODE) && defined(WITH_SPACETIME)
+ }
+#endif
+ if (retcode == -1) {
+ if (errno == EINTR) goto again;
+ if ((errno == EAGAIN || errno == EWOULDBLOCK) && n > 1) {
+ /* We couldn't do a partial write here, probably because
+ n <= PIPE_BUF and POSIX says that writes of less than
+ PIPE_BUF characters must be atomic.
+ We first try again with a partial write of 1 character.
+ If that fails too, we'll return an error code. */
+ n = 1; goto again;
+ }
+ }
+ if (retcode == -1) caml_sys_io_error(NO_ARG);
+ CAMLassert (retcode > 0);
+ return retcode;
+}
+
+caml_stat_string caml_decompose_path(struct ext_table * tbl, char * path)
+{
+ char * p, * q;
+ size_t n;
+
+ if (path == NULL) return NULL;
+ p = caml_stat_strdup(path);
+ q = p;
+ while (1) {
+ for (n = 0; q[n] != 0 && q[n] != ':'; n++) /*nothing*/;
+ caml_ext_table_add(tbl, q);
+ q = q + n;
+ if (*q == 0) break;
+ *q = 0;
+ q += 1;
+ }
+ return p;
+}
+
+caml_stat_string caml_search_in_path(struct ext_table * path, const char * name)
+{
+ const char * p;
+ char * dir, * fullname;
+ int i;
+ struct stat st;
+
+ for (p = name; *p != 0; p++) {
+ if (*p == '/') goto not_found;
+ }
+ for (i = 0; i < path->size; i++) {
+ dir = path->contents[i];
+ if (dir[0] == 0) dir = "."; /* empty path component = current dir */
+ fullname = caml_stat_strconcat(3, dir, "/", name);
+ if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode))
+ return fullname;
+ caml_stat_free(fullname);
+ }
+ not_found:
+ return caml_stat_strdup(name);
+}
+
+#ifdef __CYGWIN__
+
+/* Cygwin needs special treatment because of the implicit ".exe" at the
+ end of executable file names */
+
+static int cygwin_file_exists(const char * name)
+{
+ int fd, ret;
+ struct stat st;
+ /* Cannot use stat() here because it adds ".exe" implicitly */
+ fd = open(name, O_RDONLY);
+ if (fd == -1) return 0;
+ ret = fstat(fd, &st);
+ close(fd);
+ return ret == 0 && S_ISREG(st.st_mode);
+}
+
+static caml_stat_string cygwin_search_exe_in_path(struct ext_table * path, const char * name)
+{
+ const char * p;
+ char * dir, * fullname;
+ int i;
+
+ for (p = name; *p != 0; p++) {
+ if (*p == '/' || *p == '\\') goto not_found;
+ }
+ for (i = 0; i < path->size; i++) {
+ dir = path->contents[i];
+ if (dir[0] == 0) dir = "."; /* empty path component = current dir */
+ fullname = caml_stat_strconcat(3, dir, "/", name);
+ if (cygwin_file_exists(fullname)) return fullname;
+ caml_stat_free(fullname);
+ fullname = caml_stat_strconcat(4, dir, "/", name, ".exe");
+ if (cygwin_file_exists(fullname)) return fullname;
+ caml_stat_free(fullname);
+ }
+ not_found:
+ if (cygwin_file_exists(name)) return caml_stat_strdup(name);
+ fullname = caml_stat_strconcat(2, name, ".exe");
+ if (cygwin_file_exists(fullname)) return fullname;
+ caml_stat_free(fullname);
+ return caml_stat_strdup(name);
+}
+
+#endif
+
+caml_stat_string caml_search_exe_in_path(const char * name)
+{
+ struct ext_table path;
+ char * tofree;
+ caml_stat_string res;
+
+ caml_ext_table_init(&path, 8);
+ tofree = caml_decompose_path(&path, getenv("PATH"));
+#ifndef __CYGWIN__
+ res = caml_search_in_path(&path, name);
+#else
+ res = cygwin_search_exe_in_path(&path, name);
+#endif
+ caml_stat_free(tofree);
+ caml_ext_table_free(&path, 0);
+ return res;
+}
+
+caml_stat_string caml_search_dll_in_path(struct ext_table * path, const char * name)
+{
+ caml_stat_string dllname;
+ caml_stat_string res;
+
+ dllname = caml_stat_strconcat(2, name, ".so");
+ res = caml_search_in_path(path, dllname);
+ caml_stat_free(dllname);
+ return res;
+}
+
+#ifdef SUPPORT_DYNAMIC_LINKING
+#ifdef __CYGWIN__
+/* Use flexdll */
+
+void * caml_dlopen(char * libname, int for_execution, int global)
+{
+ int flags = (global ? FLEXDLL_RTLD_GLOBAL : 0);
+ if (!for_execution) flags |= FLEXDLL_RTLD_NOEXEC;
+ return flexdll_dlopen(libname, flags);
+}
+
+void caml_dlclose(void * handle)
+{
+ flexdll_dlclose(handle);
+}
+
+void * caml_dlsym(void * handle, const char * name)
+{
+ return flexdll_dlsym(handle, name);
+}
+
+void * caml_globalsym(const char * name)
+{
+ return flexdll_dlsym(flexdll_dlopen(NULL,0), name);
+}
+
+char * caml_dlerror(void)
+{
+ return flexdll_dlerror();
+}
+
+#else
+/* Use normal dlopen */
+
+#ifndef RTLD_GLOBAL
+#define RTLD_GLOBAL 0
+#endif
+#ifndef RTLD_LOCAL
+#define RTLD_LOCAL 0
+#endif
+
+void * caml_dlopen(char * libname, int for_execution, int global)
+{
+ return dlopen(libname, RTLD_NOW | (global ? RTLD_GLOBAL : RTLD_LOCAL));
+ /* Could use RTLD_LAZY if for_execution == 0, but needs testing */
+}
+
+void caml_dlclose(void * handle)
+{
+ dlclose(handle);
+}
+
+void * caml_dlsym(void * handle, const char * name)
+{
+#ifdef DL_NEEDS_UNDERSCORE
+ char _name[1000] = "_";
+ strncat (_name, name, 998);
+ name = _name;
+#endif
+ return dlsym(handle, name);
+}
+
+void * caml_globalsym(const char * name)
+{
+#ifdef RTLD_DEFAULT
+ return caml_dlsym(RTLD_DEFAULT, name);
+#else
+ return NULL;
+#endif
+}
+
+char * caml_dlerror(void)
+{
+ return (char*) dlerror();
+}
+
+#endif
+#else
+
+void * caml_dlopen(char * libname, int for_execution, int global)
+{
+ return NULL;
+}
+
+void caml_dlclose(void * handle)
+{
+}
+
+void * caml_dlsym(void * handle, const char * name)
+{
+ return NULL;
+}
+
+void * caml_globalsym(const char * name)
+{
+ return NULL;
+}
+
+char * caml_dlerror(void)
+{
+ return "dynamic loading not supported on this platform";
+}
+
+#endif
+
+/* Add to [contents] the (short) names of the files contained in
+ the directory named [dirname]. No entries are added for [.] and [..].
+ Return 0 on success, -1 on error; set errno in the case of error. */
+
+CAMLexport int caml_read_directory(char * dirname, struct ext_table * contents)
+{
+#if HAS_DIR
+ DIR * d;
+#ifdef HAS_DIRENT
+ struct dirent * e;
+#else
+ struct direct * e;
+#endif
+
+ d = opendir(dirname);
+ if (d == NULL) return -1;
+ while (1) {
+ e = readdir(d);
+ if (e == NULL) break;
+ if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue;
+ caml_ext_table_add(contents, caml_stat_strdup(e->d_name));
+ }
+ closedir(d);
+ return 0;
+#else
+ caml_invalid_argument("Unix.read_directory not implemented");
+#endif
+}
+
+/* Recover executable name from /proc/self/exe if possible */
+
+char * caml_executable_name(void)
+{
+#if defined(__linux__)
+ int namelen, retcode;
+ char * name;
+ struct stat st;
+
+ /* lstat("/proc/self/exe") returns st_size == 0 so we cannot use it
+ to determine the size of the buffer. Instead, we guess and adjust. */
+ namelen = 256;
+ while (1) {
+ name = caml_stat_alloc(namelen);
+ retcode = readlink("/proc/self/exe", name, namelen);
+ if (retcode == -1) { caml_stat_free(name); return NULL; }
+ if (retcode < namelen) break;
+ caml_stat_free(name);
+ if (namelen >= 1024*1024) return NULL; /* avoid runaway and overflow */
+ namelen *= 2;
+ }
+ /* readlink() does not zero-terminate its result.
+ There is room for a final zero since retcode < namelen. */
+ name[retcode] = 0;
+ /* Make sure that the contents of /proc/self/exe is a regular file.
+ (Old Linux kernels return an inode number instead.) */
+ if (stat(name, &st) == -1 || ! S_ISREG(st.st_mode)) {
+ caml_stat_free(name); return NULL;
+ }
+ return name;
+
+#elif defined(__APPLE__)
+ unsigned int namelen;
+ char * name;
+
+ namelen = 256;
+ name = caml_stat_alloc(namelen);
+ if (_NSGetExecutablePath(name, &namelen) == 0) return name;
+ caml_stat_free(name);
+ /* Buffer is too small, but namelen now contains the size needed */
+ name = caml_stat_alloc(namelen);
+ if (_NSGetExecutablePath(name, &namelen) == 0) return name;
+ caml_stat_free(name);
+ return NULL;
+
+#else
+ return NULL;
+
+#endif
+}
+
+char *caml_secure_getenv (char const *var)
+{
+#ifdef HAS_GETENV
+#ifdef HAS_SECURE_GETENV
+ return secure_getenv (var);
+#elif defined (HAS___SECURE_GETENV)
+ return __secure_getenv (var);
+#elif defined(HAS_ISSETUGID)
+ if (!issetugid ())
+ return CAML_SYS_GETENV (var);
+ else
+ return NULL;
+#else
+ if (geteuid () == getuid () && getegid () == getgid ())
+ return CAML_SYS_GETENV (var);
+ else
+ return NULL;
+#endif
+#else
+ return NULL;
+#endif
+}
+
+int caml_num_rows_fd(int fd)
+{
+#ifdef TIOCGWINSZ
+ struct winsize w;
+ w.ws_row = -1;
+ if (ioctl(fd, TIOCGWINSZ, &w) == 0)
+ return w.ws_row;
+ else
+ return -1;
+#else
+ return -1;
+#endif
+}
+
+
diff --git a/test/monniaux/ocaml/byterun/weak.c b/test/monniaux/ocaml/byterun/weak.c
new file mode 100644
index 00000000..f430ef8f
--- /dev/null
+++ b/test/monniaux/ocaml/byterun/weak.c
@@ -0,0 +1,427 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Damien Doligez, projet Para, INRIA Rocquencourt */
+/* */
+/* Copyright 1997 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+#define CAML_INTERNALS
+
+/* Operations on weak arrays and ephemerons (named ephe here)*/
+
+#include <string.h>
+
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/major_gc.h"
+#include "caml/memory.h"
+#include "caml/mlvalues.h"
+#include "caml/weak.h"
+
+value caml_ephe_list_head = 0;
+
+static value ephe_dummy = 0;
+value caml_ephe_none = (value) &ephe_dummy;
+
+#if defined (NATIVE_CODE) && defined (NO_NAKED_POINTERS)
+/** The minor heap is considered alive.
+ Outside minor and major heap, x must be black.
+*/
+static inline int Is_Dead_during_clean(value x){
+ CAMLassert (x != caml_ephe_none);
+ CAMLassert (caml_gc_phase == Phase_clean);
+ return Is_block (x) && !Is_young (x) && Is_white_val(x);
+}
+/** The minor heap doesn't have to be marked, outside they should
+ already be black
+*/
+static inline int Must_be_Marked_during_mark(value x){
+ CAMLassert (x != caml_ephe_none);
+ CAMLassert (caml_gc_phase == Phase_mark);
+ return Is_block (x) && !Is_young (x);
+}
+#else
+static inline int Is_Dead_during_clean(value x){
+ CAMLassert (x != caml_ephe_none);
+ CAMLassert (caml_gc_phase == Phase_clean);
+ return Is_block (x) && Is_in_heap (x) && Is_white_val(x);
+}
+static inline int Must_be_Marked_during_mark(value x){
+ CAMLassert (x != caml_ephe_none);
+ CAMLassert (caml_gc_phase == Phase_mark);
+ return Is_block (x) && Is_in_heap (x);
+}
+#endif
+
+
+/* [len] is a value that represents a number of words (fields) */
+CAMLprim value caml_ephe_create (value len)
+{
+ mlsize_t size, i;
+ value res;
+
+ size = Long_val (len) + CAML_EPHE_FIRST_KEY;
+ if (size < CAML_EPHE_FIRST_KEY || size > Max_wosize)
+ caml_invalid_argument ("Weak.create");
+ res = caml_alloc_shr (size, Abstract_tag);
+ for (i = 1; i < size; i++) Field (res, i) = caml_ephe_none;
+ Field (res, CAML_EPHE_LINK_OFFSET) = caml_ephe_list_head;
+ caml_ephe_list_head = res;
+ return res;
+}
+
+CAMLprim value caml_weak_create (value len)
+{
+ return caml_ephe_create(len);
+}
+
+/**
+ Specificity of the cleaning phase (Phase_clean):
+
+ The dead keys must be removed from the ephemerons and data removed
+ when one the keys is dead. Here we call it cleaning the ephemerons.
+ A specific phase of the GC is dedicated to this, Phase_clean. This
+ phase is just after the mark phase, so the white values are dead
+ values. It iterates the function caml_ephe_clean through all the
+ ephemerons.
+
+ However the GC is incremental and ocaml code can run on the middle
+ of this cleaning phase. In order to respect the semantic of the
+ ephemerons concerning dead values, the getter and setter must work
+ as if the cleaning of all the ephemerons have been done at once.
+
+ - key getter: Even if a dead key have not yet been replaced by
+ caml_ephe_none, getting it should return none.
+ - key setter: If we replace a dead key we need to set the data to
+ caml_ephe_none and clean the ephemeron.
+
+ This two cases are dealt by a call to do_check_key_clean that
+ trigger the cleaning of the ephemerons when the accessed key is
+ dead. This test is fast.
+
+ In the case of value getter and value setter, there is no fast
+ test because the removing of the data depend of the deadliness of the keys.
+ We must always try to clean the ephemerons.
+
+ */
+
+#define None_val (Val_int(0))
+#define Some_tag 0
+
+/* If we are in Phase_clean we need to check if the key
+ that is going to disappear is dead and so should trigger a cleaning
+ */
+static void do_check_key_clean(value ar, mlsize_t offset){
+ CAMLassert (offset >= CAML_EPHE_FIRST_KEY);
+ if (caml_gc_phase == Phase_clean){
+ value elt = Field (ar, offset);
+ if (elt != caml_ephe_none && Is_Dead_during_clean(elt)){
+ Field(ar,offset) = caml_ephe_none;
+ Field(ar,CAML_EPHE_DATA_OFFSET) = caml_ephe_none;
+ };
+ };
+}
+
+/* If we are in Phase_clean we need to do as if the key is empty when
+ it will be cleaned during this phase */
+static inline int is_ephe_key_none(value ar, mlsize_t offset){
+ value elt = Field (ar, offset);
+ if (elt == caml_ephe_none){
+ return 1;
+ }else if (caml_gc_phase == Phase_clean && Is_Dead_during_clean(elt)){
+ Field(ar,offset) = caml_ephe_none;
+ Field(ar,CAML_EPHE_DATA_OFFSET) = caml_ephe_none;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+
+static void do_set (value ar, mlsize_t offset, value v)
+{
+ if (Is_block (v) && Is_young (v)){
+ /* modified version of Modify */
+ value old = Field (ar, offset);
+ Field (ar, offset) = v;
+ if (!(Is_block (old) && Is_young (old))){
+ add_to_ephe_ref_table (&caml_ephe_ref_table, ar, offset);
+ }
+ }else{
+ Field (ar, offset) = v;
+ }
+}
+
+CAMLprim value caml_ephe_set_key (value ar, value n, value el)
+{
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.set");
+ }
+ do_check_key_clean(ar,offset);
+ do_set (ar, offset, el);
+ return Val_unit;
+}
+
+CAMLprim value caml_ephe_unset_key (value ar, value n)
+{
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.set");
+ }
+ do_check_key_clean(ar,offset);
+ Field (ar, offset) = caml_ephe_none;
+ return Val_unit;
+}
+
+value caml_ephe_set_key_option (value ar, value n, value el)
+{
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.set");
+ }
+ do_check_key_clean(ar,offset);
+ if (el != None_val && Is_block (el)){
+ CAMLassert (Wosize_val (el) == 1);
+ do_set (ar, offset, Field (el, 0));
+ }else{
+ Field (ar, offset) = caml_ephe_none;
+ }
+ return Val_unit;
+}
+
+CAMLprim value caml_weak_set (value ar, value n, value el){
+ return caml_ephe_set_key_option(ar,n,el);
+}
+
+CAMLprim value caml_ephe_set_data (value ar, value el)
+{
+ CAMLassert (Is_in_heap (ar));
+ if (caml_gc_phase == Phase_clean){
+ /* During this phase since we don't know which ephemeron have been
+ cleaned we always need to check it. */
+ caml_ephe_clean(ar);
+ };
+ do_set (ar, CAML_EPHE_DATA_OFFSET, el);
+ return Val_unit;
+}
+
+CAMLprim value caml_ephe_unset_data (value ar)
+{
+ CAMLassert (Is_in_heap (ar));
+ Field (ar, CAML_EPHE_DATA_OFFSET) = caml_ephe_none;
+ return Val_unit;
+}
+
+CAMLprim value caml_ephe_get_key (value ar, value n)
+{
+ CAMLparam2 (ar, n);
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLlocal2 (res, elt);
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.get_key");
+ }
+ if (is_ephe_key_none(ar, offset)){
+ res = None_val;
+ }else{
+ elt = Field (ar, offset);
+ if (caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(elt)){
+ caml_darken (elt, NULL);
+ }
+ res = caml_alloc_small (1, Some_tag);
+ Field (res, 0) = elt;
+ }
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_weak_get (value ar, value n){
+ return caml_ephe_get_key(ar, n);
+}
+
+CAMLprim value caml_ephe_get_data (value ar)
+{
+ CAMLparam1 (ar);
+ CAMLlocal2 (res, elt);
+ CAMLassert (Is_in_heap (ar));
+ elt = Field (ar, CAML_EPHE_DATA_OFFSET);
+ if(caml_gc_phase == Phase_clean) caml_ephe_clean(ar);
+ if (elt == caml_ephe_none){
+ res = None_val;
+ }else{
+ if (caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(elt)){
+ caml_darken (elt, NULL);
+ }
+ res = caml_alloc_small (1, Some_tag);
+ Field (res, 0) = elt;
+ }
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_ephe_get_key_copy (value ar, value n)
+{
+ CAMLparam2 (ar, n);
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLlocal2 (res, elt);
+ value v; /* Caution: this is NOT a local root. */
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.get_copy");
+ }
+
+ if (is_ephe_key_none(ar, offset)) CAMLreturn (None_val);
+ v = Field (ar, offset);
+ /** Don't copy custom_block #7279 */
+ if (Is_block (v) && Is_in_heap_or_young(v) && Tag_val(v) != Custom_tag ) {
+ elt = caml_alloc (Wosize_val (v), Tag_val (v));
+ /* The GC may erase or move v during this call to caml_alloc. */
+ v = Field (ar, offset);
+ if (is_ephe_key_none(ar, offset)) CAMLreturn (None_val);
+ if (Tag_val (v) < No_scan_tag){
+ mlsize_t i;
+ for (i = 0; i < Wosize_val (v); i++){
+ value f = Field (v, i);
+ if (caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(f)){
+ caml_darken (f, NULL);
+ }
+ Modify (&Field (elt, i), f);
+ }
+ }else{
+ memmove (Bp_val (elt), Bp_val (v), Bosize_val (v));
+ }
+ }else{
+ if ( caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(v) ){
+ caml_darken (v, NULL);
+ };
+ elt = v;
+ }
+ res = caml_alloc_small (1, Some_tag);
+ Field (res, 0) = elt;
+
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_weak_get_copy (value ar, value n){
+ return caml_ephe_get_key_copy(ar,n);
+}
+
+CAMLprim value caml_ephe_get_data_copy (value ar)
+{
+ CAMLparam1 (ar);
+ mlsize_t offset = CAML_EPHE_DATA_OFFSET;
+ CAMLlocal2 (res, elt);
+ value v; /* Caution: this is NOT a local root. */
+ CAMLassert (Is_in_heap (ar));
+
+ v = Field (ar, offset);
+ if (caml_gc_phase == Phase_clean) caml_ephe_clean(ar);
+ if (v == caml_ephe_none) CAMLreturn (None_val);
+ /** Don't copy custom_block #7279 */
+ if (Is_block (v) && Is_in_heap_or_young(v) && Tag_val(v) != Custom_tag ) {
+ elt = caml_alloc (Wosize_val (v), Tag_val (v));
+ /* The GC may erase or move v during this call to caml_alloc. */
+ v = Field (ar, offset);
+ if (caml_gc_phase == Phase_clean) caml_ephe_clean(ar);
+ if (v == caml_ephe_none) CAMLreturn (None_val);
+ if (Tag_val (v) < No_scan_tag){
+ mlsize_t i;
+ for (i = 0; i < Wosize_val (v); i++){
+ value f = Field (v, i);
+ if (caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(f)){
+ caml_darken (f, NULL);
+ }
+ Modify (&Field (elt, i), f);
+ }
+ }else{
+ memmove (Bp_val (elt), Bp_val (v), Bosize_val (v));
+ }
+ }else{
+ if ( caml_gc_phase == Phase_mark && Must_be_Marked_during_mark(v) ){
+ caml_darken (v, NULL);
+ };
+ elt = v;
+ }
+ res = caml_alloc_small (1, Some_tag);
+ Field (res, 0) = elt;
+
+ CAMLreturn (res);
+}
+
+CAMLprim value caml_ephe_check_key (value ar, value n)
+{
+ mlsize_t offset = Long_val (n) + CAML_EPHE_FIRST_KEY;
+ CAMLassert (Is_in_heap (ar));
+ if (offset < CAML_EPHE_FIRST_KEY || offset >= Wosize_val (ar)){
+ caml_invalid_argument ("Weak.check");
+ }
+ return Val_bool (!is_ephe_key_none(ar, offset));
+}
+
+CAMLprim value caml_weak_check (value ar, value n)
+{
+ return caml_ephe_check_key(ar,n);
+}
+
+CAMLprim value caml_ephe_check_data (value ar)
+{
+ if(caml_gc_phase == Phase_clean) caml_ephe_clean(ar);
+ return Val_bool (Field (ar, CAML_EPHE_DATA_OFFSET) != caml_ephe_none);
+}
+
+CAMLprim value caml_ephe_blit_key (value ars, value ofs,
+ value ard, value ofd, value len)
+{
+ mlsize_t offset_s = Long_val (ofs) + CAML_EPHE_FIRST_KEY;
+ mlsize_t offset_d = Long_val (ofd) + CAML_EPHE_FIRST_KEY;
+ mlsize_t length = Long_val (len);
+ long i;
+ CAMLassert (Is_in_heap (ars));
+ CAMLassert (Is_in_heap (ard));
+ if (offset_s < CAML_EPHE_FIRST_KEY || offset_s + length > Wosize_val (ars)){
+ caml_invalid_argument ("Weak.blit");
+ }
+ if (offset_d < CAML_EPHE_FIRST_KEY || offset_d + length > Wosize_val (ard)){
+ caml_invalid_argument ("Weak.blit");
+ }
+ if (caml_gc_phase == Phase_clean){
+ caml_ephe_clean(ars);
+ caml_ephe_clean(ard);
+ }
+ if (offset_d < offset_s){
+ for (i = 0; i < length; i++){
+ do_set (ard, offset_d + i, Field (ars, offset_s + i));
+ }
+ }else{
+ for (i = length - 1; i >= 0; i--){
+ do_set (ard, offset_d + i, Field (ars, offset_s + i));
+ }
+ }
+ return Val_unit;
+}
+
+CAMLprim value caml_ephe_blit_data (value ars, value ard)
+{
+ if(caml_gc_phase == Phase_clean) {
+ caml_ephe_clean(ars);
+ caml_ephe_clean(ard);
+ };
+ do_set (ard, CAML_EPHE_DATA_OFFSET, Field (ars, CAML_EPHE_DATA_OFFSET));
+ return Val_unit;
+}
+
+CAMLprim value caml_weak_blit (value ars, value ofs,
+ value ard, value ofd, value len)
+{
+ return caml_ephe_blit_key (ars, ofs, ard, ofd, len);
+}
diff --git a/test/monniaux/ocaml/config/Makefile b/test/monniaux/ocaml/config/Makefile
new file mode 100644
index 00000000..161d8a1b
--- /dev/null
+++ b/test/monniaux/ocaml/config/Makefile
@@ -0,0 +1,109 @@
+# generated by ./configure -prefix /opt/ccomp/ocaml/4.07.1 -cc /opt/CompCert/3.4/x86_64-linux/bin/ccomp -D_DEFAULT_SOURCE --no-shared-libs
+CONFIGURE_ARGS=-prefix /opt/ccomp/ocaml/4.07.1 -cc /opt/CompCert/3.4/x86_64-linux/bin/ccomp -D_DEFAULT_SOURCE --no-shared-libs
+PREFIX=/opt/ccomp/ocaml/4.07.1
+BINDIR=$(PREFIX)/bin
+BYTERUN=$(BINDIR)/ocamlrun
+LIBDIR=$(PREFIX)/lib/ocaml
+STUBLIBDIR=$(LIBDIR)/stublibs
+MANDIR=$(PREFIX)/man
+PROGRAMS_MAN_SECTION=1
+LIBRARIES_MAN_SECTION=3
+RANLIB=ranlib
+RANLIBCMD=ranlib
+ARCMD=ar
+HASHBANGSCRIPTS=true
+UNIX_OR_WIN32=unix
+UNIXLIB=unix
+GRAPHLIB=graph
+PTHREAD_LINK=-lpthread
+PTHREAD_CAML_LINK=-cclib -lpthread
+X11_INCLUDES=
+X11_LINK=-lX11
+LIBBFD_LINK=
+LIBBFD_INCLUDE=
+# DM CC=k1-mbr-gcc
+CC=/home/monniaux/work/Kalray/mppa-work/ccomp
+CPP=$(CC) -E
+CFLAGS=-O -Wall -fall
+# DM CFLAGS=-O3 -Wall
+CPPFLAGS= -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE
+OCAMLC_CFLAGS=-O
+OCAMLC_CPPFLAGS= -D_FILE_OFFSET_BITS=64 -D_REENTRANT
+LDFLAGS=
+BYTECCLIBS= -lm
+RPATH=
+EXE=
+EMPTY=
+OUTPUTEXE=-o $(EMPTY)
+SUPPORTS_SHARED_LIBRARIES=false
+SHAREDCCCOMPOPTS=
+MKSHAREDLIBRPATH=
+NATDYNLINKOPTS=
+SYSLIB=-l$(1)
+#ml let syslib x = "-l"^x;;
+
+### How to build a static library
+MKLIB=rm -f $(1) && ar rc $(1) $(2) && ranlib $(1)
+#ml let mklib out files opts = (* "" *)
+#ml Printf.sprintf "ar rc %s %s %s && ranlib %s"
+#ml out opts files out;;
+ARCH=amd64
+MODEL=default
+SYSTEM=linux
+OCAMLOPT_CFLAGS=-O
+OCAMLOPT_CPPFLAGS= -D_FILE_OFFSET_BITS=64 -D_REENTRANT
+NATIVECCPROFOPTS=
+NATIVECCLIBS= -lm -ldl
+ASM=as
+ASPP=gcc -c
+ASPPPROFFLAGS=-DPROFILING
+PROFILING=true
+DYNLINKOPTS= -ldl
+OTHERLIBRARIES=unix str dynlink bigarray raw_spacetime_lib systhreads threads graph
+CC_PROFILE=-pg
+SYSTHREAD_SUPPORT=true
+PACKLD=ld -r -o\ $(EMPTY)
+IFLEXDIR=
+O=o
+A=a
+SO=so
+EXT_OBJ=.o
+OUTPUTOBJ=-o $(EMPTY)
+EXT_ASM=.s
+EXT_LIB=.a
+EXT_DLL=.so
+EXTRALIBS=
+CCOMPTYPE=cc
+TOOLCHAIN=cc
+NATDYNLINK=false
+CMXS=cmxa
+MKEXE=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
+MKEXEDEBUGFLAG=-g
+MKDLL=shared-libs-not-available
+MKMAINDLL=shared-libs-not-available
+RUNTIMED=true
+RUNTIMEI=true
+WITH_DEBUGGER=ocamldebugger
+WITH_OCAMLDOC=ocamldoc
+ASM_CFI_SUPPORTED=true
+WITH_FRAME_POINTERS=false
+WITH_SPACETIME=false
+ENABLE_CALL_COUNTS=true
+WITH_PROFINFO=false
+LIBUNWIND_AVAILABLE=false
+LIBUNWIND_INCLUDE_FLAGS=
+LIBUNWIND_LINK_FLAGS=
+PROFINFO_WIDTH=0
+WITH_CPLUGINS=false
+WITH_FPIC=false
+TARGET=x86_64-unknown-linux-gnu
+HOST=x86_64-unknown-linux-gnu
+FLAMBDA=false
+WITH_FLAMBDA_INVARIANTS=false
+FORCE_SAFE_STRING=false
+DEFAULT_SAFE_STRING=true
+WINDOWS_UNICODE=0
+AFL_INSTRUMENT=false
+MAX_TESTSUITE_DIR_RETRIES=0
+FLAT_FLOAT_ARRAY=true
+AWK=awk
diff --git a/test/monniaux/ocaml/config/Makefile-templ b/test/monniaux/ocaml/config/Makefile-templ
new file mode 100644
index 00000000..34af691e
--- /dev/null
+++ b/test/monniaux/ocaml/config/Makefile-templ
@@ -0,0 +1,202 @@
+#**************************************************************************
+#* *
+#* OCaml *
+#* *
+#* Xavier Leroy, projet Cristal, INRIA Rocquencourt *
+#* *
+#* Copyright 1999 Institut National de Recherche en Informatique et *
+#* en Automatique. *
+#* *
+#* All rights reserved. This file is distributed under the terms of *
+#* the GNU Lesser General Public License version 2.1, with the *
+#* special exception on linking described in the file LICENSE. *
+#* *
+#**************************************************************************
+
+### Compile-time configuration
+
+########## General configuration
+
+### Where to install the binaries
+BINDIR=/usr/local/bin
+
+### Where to install the standard library
+LIBDIR=/usr/local/lib/ocaml
+STUBLIBDIR=$(LIBDIR)/stublibs
+
+### Where to install the man pages
+# Man pages for commands go in $(MANDIR)/man$(MANEXT)
+# Man pages for the library go in $(MANDIR)/mano
+MANDIR=/usr/local/man
+MANEXT=1
+
+### Do #! scripts work on your system?
+### Beware: on some systems (e.g. SunOS 4), this will work only if
+### the string "#!$(BINDIR)/ocamlrun" is less than 32 characters long.
+### In doubt, set HASHBANGSCRIPTS to false.
+HASHBANGSCRIPTS=true
+#HASHBANGSCRIPTS=false
+
+########## Configuration for the bytecode compiler
+
+### Which C compiler to use for the bytecode interpreter.
+### Performance of the bytecode interpreter is *much* improved
+### if Gnu CC version 2 is used.
+#CC=gcc
+#BYTECFLAGS=
+
+### Additional compile-time options for $(BYTECC).
+# If using gcc on Intel x86:
+# (the -fno-defer-pop option circumvents a bug in certain versions of gcc)
+#BYTECCCOMPOPTS=-fno-defer-pop -Wall
+# If using gcc and being cautious:
+#BYTECCCOMPOPTS=-Wall
+# Otherwise:
+#BYTECCCOMPOPTS=
+
+### Additional link-time options for $(BYTECC)
+# To support dynamic loading of shared libraries (they need to look at
+# our own symbols):
+#LDFLAGS=-Wl,-E
+# Otherwise:
+#LDFLAGS=
+
+### Libraries needed
+# On most platforms:
+#CCLIBS=-lcurses -ltermcap -lm
+
+### How to invoke the C preprocessor
+# This is not needed anymore. Leave these lines commented out.
+# On most machines:
+#CPP=/lib/cpp -P
+# Under Solaris:
+#CPP=/usr/ccs/lib/cpp -P
+# Under FreeBSD:
+#CPP=cpp -P
+
+### How to invoke ranlib
+RANLIB=ranlib
+RANLIBCMD=ranlib
+
+# If ranlib is not needed:
+#RANLIB=ar rs
+#RANLIBCMD=
+
+### How to invoke ar
+#ARCMD=ar
+
+### Shared library support
+# Extension for shared libraries: so if supported, a if not supported
+#SO=so
+#SO=a
+# Set to nothing if shared libraries supported, and to -custom if not supported
+#CUSTOM_IF_NOT_SHARED=
+#CUSTOM_IF_NOT_SHARED=-custom
+# Options to $(BYTECC) to produce shared objects (e.g. PIC)
+#SHAREDCCCOMPOPTS=-fPIC
+# How to build a shared library, invoked with output .so as first arg
+# and object files as remaining args
+#MKSHAREDLIB=gcc -shared -o
+# Compile-time option to $(BYTECC) to add a directory to be searched
+# at run-time for shared libraries
+#RPATH=-Wl,-rpath
+
+############# Configuration for the native-code compiler
+
+### Name of architecture for the native-code compiler
+### Currently supported:
+###
+### i386 Intel Pentium PCs under Linux, *BSD*, NextStep
+### power Macintosh under Mac OS X and Linux
+### arm ARM under Linux
+###
+### Set ARCH=none if your machine is not supported
+#ARCH=i386
+#ARCH=power
+#ARCH=arm
+#ARCH=none
+
+### Name of architecture model for the native-code compiler.
+### Some architectures come in several slightly different flavors
+### that share a common code generator. This variable tailors the
+### behavior of the code generator to the particular flavor used.
+### Currently needed only if ARCH=power; leave MODEL=default for
+### other architectures.
+### If ARCH=power: set MODEL=ppc
+### For other architectures: leave MODEL=default
+###
+#MODEL=ppc
+#MODEL=default
+
+### Name of operating system family for the native-code compiler.
+#SYSTEM=solaris
+#SYSTEM=linux
+#SYSTEM=linux_elf
+#SYSTEM=bsd
+#SYSTEM=unknown
+
+#NATIVECFLAGS=
+
+# For gcc if cautious:
+#NATIVECCCOMPOPTS=-Wall
+
+# Compile-time option to $(NATIVECC) to add a directory to be searched
+# at run-time for shared libraries
+#RPATH=-Wl,-rpath
+
+### Command and flags to use for assembling ocamlopt-generated code
+#ASM=as
+
+### Command and flags to use for assembling .S files (often with preprocessing)
+# If gcc is available:
+#ASPP=gcc -c
+# On Solaris:
+#ASPP=as -P
+
+### Extra flags to use for assembling .S files in profiling mode
+#ASPPPROFFLAGS=-DPROFILING
+
+### Whether profiling with gprof is supported
+# If yes: (e.g. x86/Linux):
+#PROFILING=true
+# If no:
+#PROFILING=false
+
+### Option to give to the C compiler for profiling
+#CC_PROFILE=-pg
+#CC_PROFILE=-xpg
+
+############# Configuration for the contributed libraries
+
+### Which libraries to compile and install
+# Currently available:
+# unix Unix system calls
+# str Regular expressions and high-level string processing
+# threads Lightweight concurrent processes
+# systhreads Same as threads, requires POSIX threads
+# graph Portable drawing primitives for X11
+# dynlink Dynamic linking of bytecode
+# bigarray Large, multidimensional numerical arrays
+
+OTHERLIBRARIES=unix str threads graph dynlink bigarray
+
+### Link-time options to ocamlc or ocamlopt for linking with POSIX threads
+# Needed for the "systhreads" package
+# Usually:
+#PTHREAD_LINK=-cclib -lpthread
+# For Solaris:
+#PTHREAD_LINK=-cclib -lpthread -cclib -lposix4
+
+### -I options for finding the X11/*.h includes
+# Needed for the "graph" package
+# Usually:
+#X11_INCLUDES=-I/usr/X11R6/include
+# For SunOS with OpenLook:
+#X11_INCLUDES=/usr/openwin/include
+
+### Link-time options to ocamlc or ocamlopt for linking with X11 libraries
+# Needed for the "graph" package
+# Usually:
+#X11_LINK=-lX11
+# For SunOS with OpenLook:
+#X11_LINK=-L$(X11_LIB) -lX11
diff --git a/test/monniaux/ocaml/config/m-nt.h b/test/monniaux/ocaml/config/m-nt.h
new file mode 100644
index 00000000..97ae9cf0
--- /dev/null
+++ b/test/monniaux/ocaml/config/m-nt.h
@@ -0,0 +1,65 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Machine configuration, Intel x86 processors, Win32,
+ Visual C++ or Mingw compiler */
+
+#ifdef _WIN64
+#define ARCH_SIXTYFOUR
+#else
+#undef ARCH_SIXTYFOUR
+#endif
+#undef ARCH_BIG_ENDIAN
+#undef ARCH_ALIGN_DOUBLE
+
+#define SIZEOF_INT 4
+#define SIZEOF_LONG 4
+#ifdef _WIN64
+#define SIZEOF_PTR 8
+#else
+#define SIZEOF_PTR 4
+#endif
+#define SIZEOF_SHORT 2
+
+#ifdef __MINGW32__
+#define ARCH_INT64_TYPE long long
+#define ARCH_UINT64_TYPE unsigned long long
+#else
+#define ARCH_INT64_TYPE __int64
+#define ARCH_UINT64_TYPE unsigned __int64
+#endif
+#define ARCH_INT64_PRINTF_FORMAT "I64"
+#if _MSC_VER >= 1800
+#define ARCH_SIZET_PRINTF_FORMAT "z"
+#else
+#define ARCH_SIZET_PRINTF_FORMAT "I"
+#endif
+
+#if defined(_MSC_VER) && !defined(__cplusplus)
+#define inline __inline
+#endif
+
+#undef NONSTANDARD_DIV_MOD
+
+#define PROFINFO_WIDTH 0
+
+/* Microsoft introduced the LL integer literal suffix in Visual C++ .NET 2003 */
+#if defined(_MSC_VER) && _MSC_VER < 1400
+#define INT64_LITERAL(s) s ## i64
+#else
+#define INT64_LITERAL(s) s ## LL
+#endif
+
+#define FLAT_FLOAT_ARRAY
diff --git a/test/monniaux/ocaml/config/m-templ.h b/test/monniaux/ocaml/config/m-templ.h
new file mode 100644
index 00000000..74ce64e1
--- /dev/null
+++ b/test/monniaux/ocaml/config/m-templ.h
@@ -0,0 +1,83 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Processor dependencies */
+
+#define ARCH_SIXTYFOUR
+
+/* Define ARCH_SIXTYFOUR if the processor has a natural word size of 64 bits.
+ That is, sizeof(char *) = 8.
+ Otherwise, leave ARCH_SIXTYFOUR undefined.
+ This assumes sizeof(char *) = 4. */
+
+#define ARCH_BIG_ENDIAN
+
+/* Define ARCH_BIG_ENDIAN if the processor is big endian (the most
+ significant byte of an integer stored in memory comes first).
+ Leave ARCH_BIG_ENDIAN undefined if the processor is little-endian
+ (the least significant byte comes first).
+*/
+
+#define ARCH_ALIGN_DOUBLE
+
+/* Define ARCH_ALIGN_DOUBLE if the processor requires doubles to be
+ doubleword-aligned. Leave ARCH_ALIGN_DOUBLE undefined if the processor
+ supports word-aligned doubles. */
+
+#undef ARCH_CODE32
+
+/* Define ARCH_CODE32 if, on a 64-bit machine, code pointers fit in 32 bits,
+ i.e. the code segment resides in the low 4G of the addressing space.
+ ARCH_CODE32 is ignored on 32-bit machines. */
+
+#define SIZEOF_INT 4
+#define SIZEOF_LONG 4
+#define SIZEOF_PTR 4
+#define SIZEOF_SHORT 2
+
+/* Define SIZEOF_INT, SIZEOF_LONG, SIZEOF_PTR and SIZEOF_SHORT
+ to the sizes in bytes of the C types "int", "long", "char *" and "short",
+ respectively. */
+
+#define ARCH_INT64_TYPE long long
+#define ARCH_UINT64_TYPE unsigned long long
+
+/* Define ARCH_INT64_TYPE and ARCH_UINT64_TYPE to 64-bit integer types,
+ typically "long long" and "unsigned long long" on 32-bit platforms,
+ and "long" and "unsigned long" on 64-bit platforms.
+ If the C compiler doesn't support any 64-bit integer type,
+ leave both ARCH_INT64_TYPE and ARCH_UINT64_TYPE undefined. */
+
+#define ARCH_INT64_PRINTF_FORMAT "ll"
+
+/* Define ARCH_INT64_PRINTF_FORMAT to the printf format used for formatting
+ values of type ARCH_INT64_TYPE. This is usually "ll" on 32-bit
+ platforms and "l" on 64-bit platforms.
+ Leave undefined if ARCH_INT64_TYPE is undefined. */
+
+#define ARCH_ALIGN_INT64
+
+/* Define ARCH_ALIGN_INT64 if the processor requires 64-bit integers to be
+ doubleword-aligned. Leave ARCH_ALIGN_INT64 undefined if the processor
+ supports word-aligned 64-bit integers. Leave undefined if
+ 64-bit integers are not supported. */
+
+#undef NONSTANDARD_DIV_MOD
+
+/* Leave NONSTANDARD_DIV_MOD undefined if the C operators / and % implement
+ round-towards-zero semantics, as specified by ISO C 9x and implemented
+ by most contemporary processors. Otherwise, or if you don't know,
+ define NONSTANDARD_DIV_MOD: this will select a slower but correct
+ software emulation of division and modulus. */
diff --git a/test/monniaux/ocaml/config/s-nt.h b/test/monniaux/ocaml/config/s-nt.h
new file mode 100644
index 00000000..ab4046b3
--- /dev/null
+++ b/test/monniaux/ocaml/config/s-nt.h
@@ -0,0 +1,41 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Operating system dependencies, Intel x86 processors, Windows NT */
+
+#define OCAML_OS_TYPE "Win32"
+
+#if defined(__MINGW32__) || _MSC_VER >= 1600
+#define HAS_STDINT_H
+#endif
+#undef BSD_SIGNALS
+#define HAS_STRERROR
+#define HAS_SOCKETS
+#define HAS_GETCWD
+#define HAS_UTIME
+#define HAS_DUP2
+#define HAS_GETHOSTNAME
+#define HAS_MKTIME
+#define HAS_PUTENV
+#define HAS_LOCALE
+#define HAS_BROKEN_PRINTF
+#define HAS_IPV6
+#define HAS_NICE
+#define SUPPORT_DYNAMIC_LINKING
+#define HAS_EXECVPE
+#if defined(_MSC_VER) && _MSC_VER < 1300
+#define LACKS_SANE_NAN
+#define LACKS_VSCPRINTF
+#endif
diff --git a/test/monniaux/ocaml/config/s-templ.h b/test/monniaux/ocaml/config/s-templ.h
new file mode 100644
index 00000000..1df4eb31
--- /dev/null
+++ b/test/monniaux/ocaml/config/s-templ.h
@@ -0,0 +1,214 @@
+/**************************************************************************/
+/* */
+/* OCaml */
+/* */
+/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
+/* */
+/* Copyright 1996 Institut National de Recherche en Informatique et */
+/* en Automatique. */
+/* */
+/* All rights reserved. This file is distributed under the terms of */
+/* the GNU Lesser General Public License version 2.1, with the */
+/* special exception on linking described in the file LICENSE. */
+/* */
+/**************************************************************************/
+
+/* Operating system and standard library dependencies. */
+
+/* 0. Operating system type string. */
+
+#define OCAML_OS_TYPE "Unix"
+/* #define OCAML_OS_TYPE "Win32" */
+/* #define OCAML_OS_TYPE "MacOS" */
+
+/* 1. For the runtime system. */
+
+#define POSIX_SIGNALS
+
+/* Define POSIX_SIGNALS if signal handling is POSIX-compliant.
+ In particular, sigaction(), sigprocmask() and the operations on
+ sigset_t are provided. */
+
+#define BSD_SIGNALS
+
+/* Define BSD_SIGNALS if signal handlers have the BSD semantics: the handler
+ remains attached to the signal when the signal is received. Leave it
+ undefined if signal handlers have the System V semantics: the signal
+ resets the behavior to default. */
+
+#define HAS_SIGSETMASK
+
+/* Define HAS_SIGSETMASK if you have sigsetmask(), as in BSD. */
+
+#define SUPPORT_DYNAMIC_LINKING
+
+/* Define SUPPORT_DYNAMIC_LINKING if dynamic loading of C stub code
+ via dlopen() is available. */
+
+#define HAS_C99_FLOAT_OPS
+
+/* Define HAS_C99_FLOAT_OPS if <math.h> conforms to ISO C99.
+ In particular, it should provide expm1(), log1p(), hypot(), copysign(). */
+
+/* 2. For the Unix library. */
+
+#define HAS_SOCKETS
+
+/* Define HAS_SOCKETS if you have BSD sockets. */
+
+#define HAS_SOCKLEN_T
+
+/* Define HAS_SOCKLEN_T if the type socklen_t is defined in
+ /usr/include/sys/socket.h. */
+
+#define HAS_UNISTD
+
+/* Define HAS_UNISTD if you have /usr/include/unistd.h. */
+
+#define HAS_DIRENT
+
+/* Define HAS_DIRENT if you have /usr/include/dirent.h and the result of
+ readdir() is of type struct dirent *.
+ Otherwise, we'll load /usr/include/sys/dir.h, and readdir() is expected to
+ return a struct direct *. */
+
+#define HAS_REWINDDIR
+
+/* Define HAS_REWINDDIR if you have rewinddir(). */
+
+#define HAS_LOCKF
+
+/* Define HAS_LOCKF if the library provides the lockf() function. */
+
+#define HAS_MKFIFO
+
+/* Define HAS_MKFIFO if the library provides the mkfifo() function. */
+
+#define HAS_GETCWD
+
+/* Define HAS_GETCWD if the library provides the getcwd() function. */
+
+#define HAS_GETPRIORITY
+
+/* Define HAS_GETPRIORITY if the library provides getpriority() and
+ setpriority(). Otherwise, we'll use nice(). */
+
+#define HAS_UTIME
+#define HAS_UTIMES
+
+/* Define HAS_UTIME if you have /usr/include/utime.h and the library
+ provides utime(). Define HAS_UTIMES if the library provides utimes(). */
+
+#define HAS_DUP2
+
+/* Define HAS_DUP2 if you have dup2(). */
+
+#define HAS_FCHMOD
+
+/* Define HAS_FCHMOD if you have fchmod() and fchown(). */
+
+#define HAS_TRUNCATE
+
+/* Define HAS_TRUNCATE if you have truncate() and
+ ftruncate(). */
+
+#define HAS_SELECT
+
+/* Define HAS_SELECT if you have select(). */
+
+#define HAS_SYS_SELECT_H
+
+/* Define HAS_SYS_SELECT_H if /usr/include/sys/select.h exists
+ and should be included before using select(). */
+
+#define HAS_NANOSLEEP
+/* Define HAS_NANOSLEEP if you have nanosleep(). */
+
+#define HAS_SYMLINK
+
+/* Define HAS_SYMLINK if you have symlink() and readlink() and lstat(). */
+
+#define HAS_WAIT4
+#define HAS_WAITPID
+
+/* Define HAS_WAIT4 if you have wait4().
+ Define HAS_WAITPID if you have waitpid(). */
+
+#define HAS_GETGROUPS
+
+/* Define HAS_GETGROUPS if you have getgroups(). */
+
+#define HAS_SETGROUPS
+
+/* Define HAS_SETGROUPS if you have setgroups(). */
+
+#define HAS_INITGROUPS
+
+/* Define HAS_INITGROUPS if you have initgroups(). */
+
+#define HAS_TERMIOS
+
+/* Define HAS_TERMIOS if you have /usr/include/termios.h and it is
+ Posix-compliant. */
+
+#define HAS_ASYNC_IO
+
+/* Define HAS_ASYNC_IO if BSD-style asynchronous I/O are supported
+ (the process can request to be sent a SIGIO signal when a descriptor
+ is ready for reading). */
+
+#define HAS_SETITIMER
+
+/* Define HAS_SETITIMER if you have setitimer(). */
+
+#define HAS_GETHOSTNAME
+
+/* Define HAS_GETHOSTNAME if you have gethostname(). */
+
+#define HAS_UNAME
+
+/* Define HAS_UNAME if you have uname(). */
+
+#define HAS_GETTIMEOFDAY
+
+/* Define HAS_GETTIMEOFDAY if you have gettimeofday(). */
+
+#define HAS_MKTIME
+
+/* Define HAS_MKTIME if you have mktime(). */
+
+#define HAS_SETSID
+
+/* Define HAS_SETSID if you have setsid(). */
+
+#define HAS_PUTENV
+
+/* Define HAS_PUTENV if you have putenv(). */
+
+#define HAS_LOCALE
+
+/* Define HAS_LOCALE if you have the include file <locale.h> and the
+ setlocale() function. */
+
+#define HAS_MMAP
+
+/* Define HAS_MMAP if you have the include file <sys/mman.h> and the
+ functions mmap() and munmap(). */
+
+#define HAS_GETHOSTBYNAME_R 6
+
+/* Define HAS_GETHOSTBYNAME_R if gethostbyname_r() is available.
+ The value of this symbol is the number of arguments of
+ gethostbyname_r(): either 5 or 6 depending on prototype.
+ (5 is the Solaris version, 6 is the Linux version). */
+
+#define HAS_GETHOSTBYADDR_R 8
+
+/* Define HAS_GETHOSTBYADDR_R if gethostbyname_r() is available.
+ The value of this symbol is the number of arguments of
+ gethostbyaddr_r(): either 7 or 8 depending on prototype.
+ (7 is the Solaris version, 8 is the Linux version). */
+
+#define HAS_NICE
+
+/* Define HAS_NICE if you have nice(). */
diff --git a/test/monniaux/ocaml/examples/quicksort b/test/monniaux/ocaml/examples/quicksort
new file mode 100755
index 00000000..0b820167
--- /dev/null
+++ b/test/monniaux/ocaml/examples/quicksort
Binary files differ
diff --git a/test/monniaux/ocaml/examples/quicksort.ml b/test/monniaux/ocaml/examples/quicksort.ml
new file mode 100644
index 00000000..57ca5a03
--- /dev/null
+++ b/test/monniaux/ocaml/examples/quicksort.ml
@@ -0,0 +1,11 @@
+let rec quicksort gt = function
+ | [] -> []
+ | x::xs ->
+ let ys, zs = List.partition (gt x) xs in
+ (quicksort gt ys) @ (x :: (quicksort gt zs));;
+
+let l =
+ quicksort ( > ) [4; 65; 2; -31; 0; 99; 83; 782; 1]
+in
+List.iter (fun x -> Printf.printf "%d; " x) l;
+print_newline();
diff --git a/test/monniaux/ocaml/tools/make-version-header.sh b/test/monniaux/ocaml/tools/make-version-header.sh
new file mode 100755
index 00000000..707d04fa
--- /dev/null
+++ b/test/monniaux/ocaml/tools/make-version-header.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#**************************************************************************
+#* *
+#* OCaml *
+#* *
+#* Damien Doligez, projet Gallium, INRIA Rocquencourt *
+#* *
+#* Copyright 2003 Institut National de Recherche en Informatique et *
+#* en Automatique. *
+#* *
+#* All rights reserved. As an exception to the licensing rules of *
+#* OCaml, this file is freely redistributable, modified or not, *
+#* without constraints. *
+#* *
+#**************************************************************************
+
+# This script extracts the components from an OCaml version number
+# and provides them as C defines:
+# OCAML_VERSION_MAJOR: the major version number
+# OCAML_VERSION_MAJOR: the minor version number
+# OCAML_VERSION_PATCHLEVEL: the patchlevel number if present, or 0 if absent
+# OCAML_VERSION_ADDITIONAL: this is defined only if the additional-info
+# field is present, and is a string that contains that field.
+# Note that additional-info is always absent in officially-released
+# versions of OCaml.
+
+# usage:
+# make-version-header.sh [version-file]
+# The argument is the VERSION file from the OCaml sources.
+# If the argument is not given, the version number from "ocamlc -v" will
+# be used.
+
+case $# in
+ 0) version="`ocamlc -v | tr -d '\r' | sed -n -e 's/.*version //p'`";;
+ 1) version="`sed -e 1q $1 | tr -d '\r'`";;
+ *) echo "usage: make-version-header.sh [version-file]" >&2
+ exit 2;;
+esac
+
+major="`echo "$version" | sed -n -e '1s/^\([0-9]*\)\..*/\1/p'`"
+minor="`echo "$version" | sed -n -e '1s/^[0-9]*\.0*\([0-9]*\).*/\1/p'`"
+patchlvl="`echo "$version" | sed -n -e '1s/^[0-9]*\.[0-9]*\.\([0-9]*\).*/\1/p'`"
+suffix="`echo "$version" | sed -n -e '1s/^[^+]*+\(.*\)/\1/p'`"
+
+echo "#define OCAML_VERSION_MAJOR $major"
+printf "#define OCAML_VERSION_MINOR %d\n" $minor
+case $patchlvl in "") patchlvl=0;; esac
+echo "#define OCAML_VERSION_PATCHLEVEL $patchlvl"
+case "$suffix" in
+ "") echo "#undef OCAML_VERSION_ADDITIONAL";;
+ *) echo "#define OCAML_VERSION_ADDITIONAL \"$suffix\"";;
+esac
+printf "#define OCAML_VERSION %d%02d%02d\n" $major $minor $patchlvl
+echo "#define OCAML_VERSION_STRING \"$version\""
diff --git a/test/monniaux/pcre2-10.32/Makefile b/test/monniaux/pcre2-10.32/Makefile
new file mode 100644
index 00000000..b6b66c37
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/Makefile
@@ -0,0 +1,35 @@
+TARGET=pcre2
+ALL_CFLAGS = -DHAVE_CONFIG_H -DPCRE2_CODE_UNIT_WIDTH=8
+EXECUTE_ARGS=testdata/testinput6 > /dev/null 2> __BASE__.out
+ALL_CFILES = \
+ pcre2_auto_possess.c \
+ pcre2_chartables.c \
+ pcre2_compile.c \
+ pcre2_config.c \
+ pcre2_context.c \
+ pcre2_convert.c \
+ pcre2_dfa_match.c \
+ pcre2_error.c \
+ pcre2_extuni.c \
+ pcre2_find_bracket.c \
+ pcre2_jit_compile.c \
+ pcre2_maketables.c \
+ pcre2_match.c \
+ pcre2_match_data.c \
+ pcre2_newline.c \
+ pcre2_ord2utf.c \
+ pcre2_pattern_info.c \
+ pcre2_serialize.c \
+ pcre2_string_utils.c \
+ pcre2_study.c \
+ pcre2_substitute.c \
+ pcre2_substring.c \
+ pcre2_tables.c \
+ pcre2_ucd.c \
+ pcre2_valid_utf.c \
+ pcre2_xclass.c \
+ pcre2posix.c \
+ pcre2test.c
+
+include ../rules.mk
+
diff --git a/test/monniaux/pcre2-10.32/config.h b/test/monniaux/pcre2-10.32/config.h
new file mode 100644
index 00000000..41061058
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/config.h
@@ -0,0 +1,366 @@
+/* src/config.h. Generated from config.h.in by configure. */
+/* src/config.h.in. Generated from configure.ac by autoheader. */
+
+/* PCRE2 is written in Standard C, but there are a few non-standard things it
+can cope with, allowing it to run on SunOS4 and other "close to standard"
+systems.
+
+In environments that support the GNU autotools, config.h.in is converted into
+config.h by the "configure" script. In environments that use CMake,
+config-cmake.in is converted into config.h. If you are going to build PCRE2 "by
+hand" without using "configure" or CMake, you should copy the distributed
+config.h.generic to config.h, and edit the macro definitions to be the way you
+need them. You must then add -DHAVE_CONFIG_H to all of your compile commands,
+so that config.h is included at the start of every source.
+
+Alternatively, you can avoid editing by using -D on the compiler command line
+to set the macro values. In this case, you do not have to set -DHAVE_CONFIG_H,
+but if you do, default values will be taken from config.h for non-boolean
+macros that are not defined on the command line.
+
+Boolean macros such as HAVE_STDLIB_H and SUPPORT_PCRE2_8 should either be
+defined (conventionally to 1) for TRUE, and not defined at all for FALSE. All
+such macros are listed as a commented #undef in config.h.generic. Macros such
+as MATCH_LIMIT, whose actual value is relevant, have defaults defined, but are
+surrounded by #ifndef/#endif lines so that the value can be overridden by -D.
+
+PCRE2 uses memmove() if HAVE_MEMMOVE is defined; otherwise it uses bcopy() if
+HAVE_BCOPY is defined. If your system has neither bcopy() nor memmove(), make
+sure both macros are undefined; an emulation function will then be used. */
+
+/* By default, the \R escape sequence matches any Unicode line ending
+ character or sequence of characters. If BSR_ANYCRLF is defined (to any
+ value), this is changed so that backslash-R matches only CR, LF, or CRLF.
+ The build-time default can be overridden by the user of PCRE2 at runtime.
+ */
+/* #undef BSR_ANYCRLF */
+
+/* If you are compiling for a system that uses EBCDIC instead of ASCII
+ character codes, define this macro to any value. When EBCDIC is set, PCRE2
+ assumes that all input strings are in EBCDIC. If you do not define this
+ macro, PCRE2 will assume input strings are ASCII or UTF-8/16/32 Unicode. It
+ is not possible to build a version of PCRE2 that supports both EBCDIC and
+ UTF-8/16/32. */
+/* #undef EBCDIC */
+
+/* In an EBCDIC environment, define this macro to any value to arrange for the
+ NL character to be 0x25 instead of the default 0x15. NL plays the role that
+ LF does in an ASCII/Unicode environment. */
+/* #undef EBCDIC_NL25 */
+
+/* Define to 1 if you have the `bcopy' function. */
+/* #undef HAVE_BCOPY */
+
+/* Define to 1 if you have the <bzlib.h> header file. */
+/* #undef HAVE_BZLIB_H */
+
+/* Define to 1 if you have the <dirent.h> header file. */
+/* #undef HAVE_DIRENT_H */
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+/* #undef HAVE_DLFCN_H */
+
+/* Define to 1 if you have the <editline/readline.h> header file. */
+/* #undef HAVE_EDITLINE_READLINE_H */
+
+/* Define to 1 if you have the <edit/readline/readline.h> header file. */
+/* #undef HAVE_EDIT_READLINE_READLINE_H */
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* Define to 1 if you have the <limits.h> header file. */
+#define HAVE_LIMITS_H 1
+
+/* Define to 1 if you have the `memmove' function. */
+#define HAVE_MEMMOVE 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `mkostemp' function. */
+/* #undef HAVE_MKOSTEMP */
+
+/* Define if you have POSIX threads libraries and header files. */
+/* #undef HAVE_PTHREAD */
+
+/* Have PTHREAD_PRIO_INHERIT. */
+/* #undef HAVE_PTHREAD_PRIO_INHERIT */
+
+/* Define to 1 if you have the <readline/history.h> header file. */
+/* #undef HAVE_READLINE_HISTORY_H */
+
+/* Define to 1 if you have the <readline/readline.h> header file. */
+/* #undef HAVE_READLINE_READLINE_H */
+
+/* Define to 1 if you have the `secure_getenv' function. */
+/* #undef HAVE_SECURE_GETENV */
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the `strerror' function. */
+#define HAVE_STRERROR
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <sys/wait.h> header file. */
+#define HAVE_SYS_WAIT_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Define to 1 if the compiler supports simple visibility declarations. */
+/* #undef HAVE_VISIBILITY */
+
+/* Define to 1 if you have the <windows.h> header file. */
+/* #undef HAVE_WINDOWS_H */
+
+/* Define to 1 if you have the <zlib.h> header file. */
+/* #undef HAVE_ZLIB_H */
+
+/* This limits the amount of memory that may be used while matching a pattern.
+ It applies to both pcre2_match() and pcre2_dfa_match(). It does not apply
+ to JIT matching. The value is in kibibytes (units of 1024 bytes). */
+#ifndef HEAP_LIMIT
+#define HEAP_LIMIT 2500
+#endif
+
+/* The value of LINK_SIZE determines the number of bytes used to store links
+ as offsets within the compiled regex. The default is 2, which allows for
+ compiled patterns up to 65535 code units long. This covers the vast
+ majority of cases. However, PCRE2 can also be compiled to use 3 or 4 bytes
+ instead. This allows for longer patterns in extreme cases. */
+#ifndef LINK_SIZE
+#define LINK_SIZE 2
+#endif
+
+/* Define to the sub-directory where libtool stores uninstalled libraries. */
+/* This is ignored unless you are using libtool. */
+#ifndef LT_OBJDIR
+#define LT_OBJDIR ".libs/"
+#endif
+
+/* The value of MATCH_LIMIT determines the default number of times the
+ pcre2_match() function can record a backtrack position during a single
+ matching attempt. The value is also used to limit a loop counter in
+ pcre2_dfa_match(). There is a runtime interface for setting a different
+ limit. The limit exists in order to catch runaway regular expressions that
+ take for ever to determine that they do not match. The default is set very
+ large so that it does not accidentally catch legitimate cases. */
+#ifndef MATCH_LIMIT
+#define MATCH_LIMIT 10000000
+#endif
+
+/* The above limit applies to all backtracks, whether or not they are nested.
+ In some environments it is desirable to limit the nesting of backtracking
+ (that is, the depth of tree that is searched) more strictly, in order to
+ restrict the maximum amount of heap memory that is used. The value of
+ MATCH_LIMIT_DEPTH provides this facility. To have any useful effect, it
+ must be less than the value of MATCH_LIMIT. The default is to use the same
+ value as MATCH_LIMIT. There is a runtime method for setting a different
+ limit. In the case of pcre2_dfa_match(), this limit controls the depth of
+ the internal nested function calls that are used for pattern recursions,
+ lookarounds, and atomic groups. */
+#ifndef MATCH_LIMIT_DEPTH
+#define MATCH_LIMIT_DEPTH MATCH_LIMIT
+#endif
+
+/* This limit is parameterized just in case anybody ever wants to change it.
+ Care must be taken if it is increased, because it guards against integer
+ overflow caused by enormously large patterns. */
+#ifndef MAX_NAME_COUNT
+#define MAX_NAME_COUNT 10000
+#endif
+
+/* This limit is parameterized just in case anybody ever wants to change it.
+ Care must be taken if it is increased, because it guards against integer
+ overflow caused by enormously large patterns. */
+#ifndef MAX_NAME_SIZE
+#define MAX_NAME_SIZE 32
+#endif
+
+/* Defining NEVER_BACKSLASH_C locks out the use of \C in all patterns. */
+/* #undef NEVER_BACKSLASH_C */
+
+/* The value of NEWLINE_DEFAULT determines the default newline character
+ sequence. PCRE2 client programs can override this by selecting other values
+ at run time. The valid values are 1 (CR), 2 (LF), 3 (CRLF), 4 (ANY), 5
+ (ANYCRLF), and 6 (NUL). */
+#ifndef NEWLINE_DEFAULT
+#define NEWLINE_DEFAULT 2
+#endif
+
+/* Name of package */
+#define PACKAGE "pcre2"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT ""
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "PCRE2"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "PCRE2 10.32"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "pcre2"
+
+/* Define to the home page for this package. */
+#define PACKAGE_URL ""
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "10.32"
+
+/* The value of PARENS_NEST_LIMIT specifies the maximum depth of nested
+ parentheses (of any kind) in a pattern. This limits the amount of system
+ stack that is used while compiling a pattern. */
+#ifndef PARENS_NEST_LIMIT
+#define PARENS_NEST_LIMIT 250
+#endif
+
+/* The value of PCRE2GREP_BUFSIZE is the starting size of the buffer used by
+ pcre2grep to hold parts of the file it is searching. The buffer will be
+ expanded up to PCRE2GREP_MAX_BUFSIZE if necessary, for files containing
+ very long lines. The actual amount of memory used by pcre2grep is three
+ times this number, because it allows for the buffering of "before" and
+ "after" lines. */
+#ifndef PCRE2GREP_BUFSIZE
+#define PCRE2GREP_BUFSIZE 20480
+#endif
+
+/* The value of PCRE2GREP_MAX_BUFSIZE specifies the maximum size of the buffer
+ used by pcre2grep to hold parts of the file it is searching. The actual
+ amount of memory used by pcre2grep is three times this number, because it
+ allows for the buffering of "before" and "after" lines. */
+#ifndef PCRE2GREP_MAX_BUFSIZE
+#define PCRE2GREP_MAX_BUFSIZE 1048576
+#endif
+
+/* Define to any value to include debugging code. */
+/* #undef PCRE2_DEBUG */
+
+/* If you are compiling for a system other than a Unix-like system or
+ Win32, and it needs some magic to be inserted before the definition
+ of a function that is exported by the library, define this macro to
+ contain the relevant magic. If you do not define this macro, a suitable
+ __declspec value is used for Windows systems; in other environments
+ "extern" is used for a C compiler and "extern C" for a C++ compiler.
+ This macro apears at the start of every exported function that is part
+ of the external API. It does not appear on functions that are "external"
+ in the C sense, but which are internal to the library. */
+/* #undef PCRE2_EXP_DEFN */
+
+/* Define to any value if linking statically (TODO: make nice with Libtool) */
+#define PCRE2_STATIC 1
+
+/* Define to necessary symbol if this constant uses a non-standard name on
+ your system. */
+/* #undef PTHREAD_CREATE_JOINABLE */
+
+/* Define to any non-zero number to enable support for SELinux compatible
+ executable memory allocator in JIT. Note that this will have no effect
+ unless SUPPORT_JIT is also defined. */
+/* #undef SLJIT_PROT_EXECUTABLE_ALLOCATOR */
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Define to any value to enable support for Just-In-Time compiling. */
+/* #undef SUPPORT_JIT */
+
+/* Define to any value to allow pcre2grep to be linked with libbz2, so that it
+ is able to handle .bz2 files. */
+/* #undef SUPPORT_LIBBZ2 */
+
+/* Define to any value to allow pcre2test to be linked with libedit. */
+/* #undef SUPPORT_LIBEDIT */
+
+/* Define to any value to allow pcre2test to be linked with libreadline. */
+/* #undef SUPPORT_LIBREADLINE */
+
+/* Define to any value to allow pcre2grep to be linked with libz, so that it
+ is able to handle .gz files. */
+/* #undef SUPPORT_LIBZ */
+
+/* Define to any value to enable callout script support in pcre2grep. */
+/* #undef SUPPORT_PCRE2GREP_CALLOUT */
+
+/* Define to any value to enable JIT support in pcre2grep. Note that this will
+ have no effect unless SUPPORT_JIT is also defined. */
+/* #undef SUPPORT_PCRE2GREP_JIT */
+
+/* Define to any value to enable the 16 bit PCRE2 library. */
+/* #undef SUPPORT_PCRE2_16 */
+
+/* Define to any value to enable the 32 bit PCRE2 library. */
+/* #undef SUPPORT_PCRE2_32 */
+
+/* Define to any value to enable the 8 bit PCRE2 library. */
+#define SUPPORT_PCRE2_8 1
+
+/* Define to any value to enable support for Unicode and UTF encoding. This
+ will work even in an EBCDIC environment, but it is incompatible with the
+ EBCDIC macro. That is, PCRE2 can support *either* EBCDIC code *or*
+ ASCII/Unicode, but not both at once. */
+/* #undef SUPPORT_UNICODE */
+
+/* Define to any value for valgrind support to find invalid memory reads. */
+/* #undef SUPPORT_VALGRIND */
+
+/* Enable extensions on AIX 3, Interix. */
+#ifndef _ALL_SOURCE
+# define _ALL_SOURCE 1
+#endif
+/* Enable GNU extensions on systems that have them. */
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE 1
+#endif
+/* Enable threading extensions on Solaris. */
+#ifndef _POSIX_PTHREAD_SEMANTICS
+# define _POSIX_PTHREAD_SEMANTICS 1
+#endif
+/* Enable extensions on HP NonStop. */
+#ifndef _TANDEM_SOURCE
+# define _TANDEM_SOURCE 1
+#endif
+/* Enable general extensions on Solaris. */
+#ifndef __EXTENSIONS__
+# define __EXTENSIONS__ 1
+#endif
+
+/* Version number of package */
+#define VERSION "10.32"
+
+/* Define to 1 if on MINIX. */
+/* #undef _MINIX */
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+ this defined. */
+#define _POSIX_1_SOURCE 1
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+#define _POSIX_SOURCE 1
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to the type of a signed integer type of width exactly 64 bits if
+ such a type exists and the standard includes do not define it. */
+/* #undef int64_t */
+
+/* Define to `unsigned int' if <sys/types.h> does not define. */
+/* #undef size_t */
diff --git a/test/monniaux/pcre2-10.32/pcre2.h b/test/monniaux/pcre2-10.32/pcre2.h
new file mode 100644
index 00000000..3d2feb7a
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2.h
@@ -0,0 +1,974 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* This is the public header file for the PCRE library, second API, to be
+#included by applications that call PCRE2 functions.
+
+ Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#ifndef PCRE2_H_IDEMPOTENT_GUARD
+#define PCRE2_H_IDEMPOTENT_GUARD
+
+/* The current PCRE version information. */
+
+#define PCRE2_MAJOR 10
+#define PCRE2_MINOR 32
+#define PCRE2_PRERELEASE
+#define PCRE2_DATE 2018-09-10
+
+/* For the benefit of systems without stdint.h, an alternative is to use
+inttypes.h. The existence of these headers is checked by configure or CMake. */
+
+#define PCRE2_HAVE_STDINT_H 1
+#define PCRE2_HAVE_INTTYPES_H 1
+
+/* When an application links to a PCRE DLL in Windows, the symbols that are
+imported have to be identified as such. When building PCRE2, the appropriate
+export setting is defined in pcre2_internal.h, which includes this file. So we
+don't change existing definitions of PCRE2_EXP_DECL. */
+
+#if defined(_WIN32) && !defined(PCRE2_STATIC)
+# ifndef PCRE2_EXP_DECL
+# define PCRE2_EXP_DECL extern __declspec(dllimport)
+# endif
+#endif
+
+/* By default, we use the standard "extern" declarations. */
+
+#ifndef PCRE2_EXP_DECL
+# ifdef __cplusplus
+# define PCRE2_EXP_DECL extern "C"
+# else
+# define PCRE2_EXP_DECL extern
+# endif
+#endif
+
+/* When compiling with the MSVC compiler, it is sometimes necessary to include
+a "calling convention" before exported function names. (This is secondhand
+information; I know nothing about MSVC myself). For example, something like
+
+ void __cdecl function(....)
+
+might be needed. In order so make this easy, all the exported functions have
+PCRE2_CALL_CONVENTION just before their names. It is rarely needed; if not
+set, we ensure here that it has no effect. */
+
+#ifndef PCRE2_CALL_CONVENTION
+#define PCRE2_CALL_CONVENTION
+#endif
+
+/* Have to include limits.h, stdlib.h and stdint.h (or inttypes.h) to ensure
+that size_t and uint8_t, UCHAR_MAX, etc are defined. If the system has neither
+header, the relevant values must be provided by some other means. */
+
+#include <limits.h>
+#include <stdlib.h>
+
+#if PCRE2_HAVE_STDINT_H
+#include <stdint.h>
+#elif PCRE2_HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
+/* Allow for C++ users compiling this directly. */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* The following option bits can be passed to pcre2_compile(), pcre2_match(),
+or pcre2_dfa_match(). PCRE2_NO_UTF_CHECK affects only the function to which it
+is passed. Put these bits at the most significant end of the options word so
+others can be added next to them */
+
+#define PCRE2_ANCHORED 0x80000000u
+#define PCRE2_NO_UTF_CHECK 0x40000000u
+#define PCRE2_ENDANCHORED 0x20000000u
+
+/* The following option bits can be passed only to pcre2_compile(). However,
+they may affect compilation, JIT compilation, and/or interpretive execution.
+The following tags indicate which:
+
+C alters what is compiled by pcre2_compile()
+J alters what is compiled by pcre2_jit_compile()
+M is inspected during pcre2_match() execution
+D is inspected during pcre2_dfa_match() execution
+*/
+
+#define PCRE2_ALLOW_EMPTY_CLASS 0x00000001u /* C */
+#define PCRE2_ALT_BSUX 0x00000002u /* C */
+#define PCRE2_AUTO_CALLOUT 0x00000004u /* C */
+#define PCRE2_CASELESS 0x00000008u /* C */
+#define PCRE2_DOLLAR_ENDONLY 0x00000010u /* J M D */
+#define PCRE2_DOTALL 0x00000020u /* C */
+#define PCRE2_DUPNAMES 0x00000040u /* C */
+#define PCRE2_EXTENDED 0x00000080u /* C */
+#define PCRE2_FIRSTLINE 0x00000100u /* J M D */
+#define PCRE2_MATCH_UNSET_BACKREF 0x00000200u /* C J M */
+#define PCRE2_MULTILINE 0x00000400u /* C */
+#define PCRE2_NEVER_UCP 0x00000800u /* C */
+#define PCRE2_NEVER_UTF 0x00001000u /* C */
+#define PCRE2_NO_AUTO_CAPTURE 0x00002000u /* C */
+#define PCRE2_NO_AUTO_POSSESS 0x00004000u /* C */
+#define PCRE2_NO_DOTSTAR_ANCHOR 0x00008000u /* C */
+#define PCRE2_NO_START_OPTIMIZE 0x00010000u /* J M D */
+#define PCRE2_UCP 0x00020000u /* C J M D */
+#define PCRE2_UNGREEDY 0x00040000u /* C */
+#define PCRE2_UTF 0x00080000u /* C J M D */
+#define PCRE2_NEVER_BACKSLASH_C 0x00100000u /* C */
+#define PCRE2_ALT_CIRCUMFLEX 0x00200000u /* J M D */
+#define PCRE2_ALT_VERBNAMES 0x00400000u /* C */
+#define PCRE2_USE_OFFSET_LIMIT 0x00800000u /* J M D */
+#define PCRE2_EXTENDED_MORE 0x01000000u /* C */
+#define PCRE2_LITERAL 0x02000000u /* C */
+
+/* An additional compile options word is available in the compile context. */
+
+#define PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES 0x00000001u /* C */
+#define PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL 0x00000002u /* C */
+#define PCRE2_EXTRA_MATCH_WORD 0x00000004u /* C */
+#define PCRE2_EXTRA_MATCH_LINE 0x00000008u /* C */
+
+/* These are for pcre2_jit_compile(). */
+
+#define PCRE2_JIT_COMPLETE 0x00000001u /* For full matching */
+#define PCRE2_JIT_PARTIAL_SOFT 0x00000002u
+#define PCRE2_JIT_PARTIAL_HARD 0x00000004u
+
+/* These are for pcre2_match(), pcre2_dfa_match(), and pcre2_jit_match(). Note
+that PCRE2_ANCHORED and PCRE2_NO_UTF_CHECK can also be passed to these
+functions (though pcre2_jit_match() ignores the latter since it bypasses all
+sanity checks). */
+
+#define PCRE2_NOTBOL 0x00000001u
+#define PCRE2_NOTEOL 0x00000002u
+#define PCRE2_NOTEMPTY 0x00000004u /* ) These two must be kept */
+#define PCRE2_NOTEMPTY_ATSTART 0x00000008u /* ) adjacent to each other. */
+#define PCRE2_PARTIAL_SOFT 0x00000010u
+#define PCRE2_PARTIAL_HARD 0x00000020u
+
+/* These are additional options for pcre2_dfa_match(). */
+
+#define PCRE2_DFA_RESTART 0x00000040u
+#define PCRE2_DFA_SHORTEST 0x00000080u
+
+/* These are additional options for pcre2_substitute(), which passes any others
+through to pcre2_match(). */
+
+#define PCRE2_SUBSTITUTE_GLOBAL 0x00000100u
+#define PCRE2_SUBSTITUTE_EXTENDED 0x00000200u
+#define PCRE2_SUBSTITUTE_UNSET_EMPTY 0x00000400u
+#define PCRE2_SUBSTITUTE_UNKNOWN_UNSET 0x00000800u
+#define PCRE2_SUBSTITUTE_OVERFLOW_LENGTH 0x00001000u
+
+/* A further option for pcre2_match(), not allowed for pcre2_dfa_match(),
+ignored for pcre2_jit_match(). */
+
+#define PCRE2_NO_JIT 0x00002000u
+
+/* Options for pcre2_pattern_convert(). */
+
+#define PCRE2_CONVERT_UTF 0x00000001u
+#define PCRE2_CONVERT_NO_UTF_CHECK 0x00000002u
+#define PCRE2_CONVERT_POSIX_BASIC 0x00000004u
+#define PCRE2_CONVERT_POSIX_EXTENDED 0x00000008u
+#define PCRE2_CONVERT_GLOB 0x00000010u
+#define PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR 0x00000030u
+#define PCRE2_CONVERT_GLOB_NO_STARSTAR 0x00000050u
+
+/* Newline and \R settings, for use in compile contexts. The newline values
+must be kept in step with values set in config.h and both sets must all be
+greater than zero. */
+
+#define PCRE2_NEWLINE_CR 1
+#define PCRE2_NEWLINE_LF 2
+#define PCRE2_NEWLINE_CRLF 3
+#define PCRE2_NEWLINE_ANY 4
+#define PCRE2_NEWLINE_ANYCRLF 5
+#define PCRE2_NEWLINE_NUL 6
+
+#define PCRE2_BSR_UNICODE 1
+#define PCRE2_BSR_ANYCRLF 2
+
+/* Error codes for pcre2_compile(). Some of these are also used by
+pcre2_pattern_convert(). */
+
+#define PCRE2_ERROR_END_BACKSLASH 101
+#define PCRE2_ERROR_END_BACKSLASH_C 102
+#define PCRE2_ERROR_UNKNOWN_ESCAPE 103
+#define PCRE2_ERROR_QUANTIFIER_OUT_OF_ORDER 104
+#define PCRE2_ERROR_QUANTIFIER_TOO_BIG 105
+#define PCRE2_ERROR_MISSING_SQUARE_BRACKET 106
+#define PCRE2_ERROR_ESCAPE_INVALID_IN_CLASS 107
+#define PCRE2_ERROR_CLASS_RANGE_ORDER 108
+#define PCRE2_ERROR_QUANTIFIER_INVALID 109
+#define PCRE2_ERROR_INTERNAL_UNEXPECTED_REPEAT 110
+#define PCRE2_ERROR_INVALID_AFTER_PARENS_QUERY 111
+#define PCRE2_ERROR_POSIX_CLASS_NOT_IN_CLASS 112
+#define PCRE2_ERROR_POSIX_NO_SUPPORT_COLLATING 113
+#define PCRE2_ERROR_MISSING_CLOSING_PARENTHESIS 114
+#define PCRE2_ERROR_BAD_SUBPATTERN_REFERENCE 115
+#define PCRE2_ERROR_NULL_PATTERN 116
+#define PCRE2_ERROR_BAD_OPTIONS 117
+#define PCRE2_ERROR_MISSING_COMMENT_CLOSING 118
+#define PCRE2_ERROR_PARENTHESES_NEST_TOO_DEEP 119
+#define PCRE2_ERROR_PATTERN_TOO_LARGE 120
+#define PCRE2_ERROR_HEAP_FAILED 121
+#define PCRE2_ERROR_UNMATCHED_CLOSING_PARENTHESIS 122
+#define PCRE2_ERROR_INTERNAL_CODE_OVERFLOW 123
+#define PCRE2_ERROR_MISSING_CONDITION_CLOSING 124
+#define PCRE2_ERROR_LOOKBEHIND_NOT_FIXED_LENGTH 125
+#define PCRE2_ERROR_ZERO_RELATIVE_REFERENCE 126
+#define PCRE2_ERROR_TOO_MANY_CONDITION_BRANCHES 127
+#define PCRE2_ERROR_CONDITION_ASSERTION_EXPECTED 128
+#define PCRE2_ERROR_BAD_RELATIVE_REFERENCE 129
+#define PCRE2_ERROR_UNKNOWN_POSIX_CLASS 130
+#define PCRE2_ERROR_INTERNAL_STUDY_ERROR 131
+#define PCRE2_ERROR_UNICODE_NOT_SUPPORTED 132
+#define PCRE2_ERROR_PARENTHESES_STACK_CHECK 133
+#define PCRE2_ERROR_CODE_POINT_TOO_BIG 134
+#define PCRE2_ERROR_LOOKBEHIND_TOO_COMPLICATED 135
+#define PCRE2_ERROR_LOOKBEHIND_INVALID_BACKSLASH_C 136
+#define PCRE2_ERROR_UNSUPPORTED_ESCAPE_SEQUENCE 137
+#define PCRE2_ERROR_CALLOUT_NUMBER_TOO_BIG 138
+#define PCRE2_ERROR_MISSING_CALLOUT_CLOSING 139
+#define PCRE2_ERROR_ESCAPE_INVALID_IN_VERB 140
+#define PCRE2_ERROR_UNRECOGNIZED_AFTER_QUERY_P 141
+#define PCRE2_ERROR_MISSING_NAME_TERMINATOR 142
+#define PCRE2_ERROR_DUPLICATE_SUBPATTERN_NAME 143
+#define PCRE2_ERROR_INVALID_SUBPATTERN_NAME 144
+#define PCRE2_ERROR_UNICODE_PROPERTIES_UNAVAILABLE 145
+#define PCRE2_ERROR_MALFORMED_UNICODE_PROPERTY 146
+#define PCRE2_ERROR_UNKNOWN_UNICODE_PROPERTY 147
+#define PCRE2_ERROR_SUBPATTERN_NAME_TOO_LONG 148
+#define PCRE2_ERROR_TOO_MANY_NAMED_SUBPATTERNS 149
+#define PCRE2_ERROR_CLASS_INVALID_RANGE 150
+#define PCRE2_ERROR_OCTAL_BYTE_TOO_BIG 151
+#define PCRE2_ERROR_INTERNAL_OVERRAN_WORKSPACE 152
+#define PCRE2_ERROR_INTERNAL_MISSING_SUBPATTERN 153
+#define PCRE2_ERROR_DEFINE_TOO_MANY_BRANCHES 154
+#define PCRE2_ERROR_BACKSLASH_O_MISSING_BRACE 155
+#define PCRE2_ERROR_INTERNAL_UNKNOWN_NEWLINE 156
+#define PCRE2_ERROR_BACKSLASH_G_SYNTAX 157
+#define PCRE2_ERROR_PARENS_QUERY_R_MISSING_CLOSING 158
+/* Error 159 is obsolete and should now never occur */
+#define PCRE2_ERROR_VERB_ARGUMENT_NOT_ALLOWED 159
+#define PCRE2_ERROR_VERB_UNKNOWN 160
+#define PCRE2_ERROR_SUBPATTERN_NUMBER_TOO_BIG 161
+#define PCRE2_ERROR_SUBPATTERN_NAME_EXPECTED 162
+#define PCRE2_ERROR_INTERNAL_PARSED_OVERFLOW 163
+#define PCRE2_ERROR_INVALID_OCTAL 164
+#define PCRE2_ERROR_SUBPATTERN_NAMES_MISMATCH 165
+#define PCRE2_ERROR_MARK_MISSING_ARGUMENT 166
+#define PCRE2_ERROR_INVALID_HEXADECIMAL 167
+#define PCRE2_ERROR_BACKSLASH_C_SYNTAX 168
+#define PCRE2_ERROR_BACKSLASH_K_SYNTAX 169
+#define PCRE2_ERROR_INTERNAL_BAD_CODE_LOOKBEHINDS 170
+#define PCRE2_ERROR_BACKSLASH_N_IN_CLASS 171
+#define PCRE2_ERROR_CALLOUT_STRING_TOO_LONG 172
+#define PCRE2_ERROR_UNICODE_DISALLOWED_CODE_POINT 173
+#define PCRE2_ERROR_UTF_IS_DISABLED 174
+#define PCRE2_ERROR_UCP_IS_DISABLED 175
+#define PCRE2_ERROR_VERB_NAME_TOO_LONG 176
+#define PCRE2_ERROR_BACKSLASH_U_CODE_POINT_TOO_BIG 177
+#define PCRE2_ERROR_MISSING_OCTAL_OR_HEX_DIGITS 178
+#define PCRE2_ERROR_VERSION_CONDITION_SYNTAX 179
+#define PCRE2_ERROR_INTERNAL_BAD_CODE_AUTO_POSSESS 180
+#define PCRE2_ERROR_CALLOUT_NO_STRING_DELIMITER 181
+#define PCRE2_ERROR_CALLOUT_BAD_STRING_DELIMITER 182
+#define PCRE2_ERROR_BACKSLASH_C_CALLER_DISABLED 183
+#define PCRE2_ERROR_QUERY_BARJX_NEST_TOO_DEEP 184
+#define PCRE2_ERROR_BACKSLASH_C_LIBRARY_DISABLED 185
+#define PCRE2_ERROR_PATTERN_TOO_COMPLICATED 186
+#define PCRE2_ERROR_LOOKBEHIND_TOO_LONG 187
+#define PCRE2_ERROR_PATTERN_STRING_TOO_LONG 188
+#define PCRE2_ERROR_INTERNAL_BAD_CODE 189
+#define PCRE2_ERROR_INTERNAL_BAD_CODE_IN_SKIP 190
+#define PCRE2_ERROR_NO_SURROGATES_IN_UTF16 191
+#define PCRE2_ERROR_BAD_LITERAL_OPTIONS 192
+#define PCRE2_ERROR_SUPPORTED_ONLY_IN_UNICODE 193
+#define PCRE2_ERROR_INVALID_HYPHEN_IN_OPTIONS 194
+
+
+/* "Expected" matching error codes: no match and partial match. */
+
+#define PCRE2_ERROR_NOMATCH (-1)
+#define PCRE2_ERROR_PARTIAL (-2)
+
+/* Error codes for UTF-8 validity checks */
+
+#define PCRE2_ERROR_UTF8_ERR1 (-3)
+#define PCRE2_ERROR_UTF8_ERR2 (-4)
+#define PCRE2_ERROR_UTF8_ERR3 (-5)
+#define PCRE2_ERROR_UTF8_ERR4 (-6)
+#define PCRE2_ERROR_UTF8_ERR5 (-7)
+#define PCRE2_ERROR_UTF8_ERR6 (-8)
+#define PCRE2_ERROR_UTF8_ERR7 (-9)
+#define PCRE2_ERROR_UTF8_ERR8 (-10)
+#define PCRE2_ERROR_UTF8_ERR9 (-11)
+#define PCRE2_ERROR_UTF8_ERR10 (-12)
+#define PCRE2_ERROR_UTF8_ERR11 (-13)
+#define PCRE2_ERROR_UTF8_ERR12 (-14)
+#define PCRE2_ERROR_UTF8_ERR13 (-15)
+#define PCRE2_ERROR_UTF8_ERR14 (-16)
+#define PCRE2_ERROR_UTF8_ERR15 (-17)
+#define PCRE2_ERROR_UTF8_ERR16 (-18)
+#define PCRE2_ERROR_UTF8_ERR17 (-19)
+#define PCRE2_ERROR_UTF8_ERR18 (-20)
+#define PCRE2_ERROR_UTF8_ERR19 (-21)
+#define PCRE2_ERROR_UTF8_ERR20 (-22)
+#define PCRE2_ERROR_UTF8_ERR21 (-23)
+
+/* Error codes for UTF-16 validity checks */
+
+#define PCRE2_ERROR_UTF16_ERR1 (-24)
+#define PCRE2_ERROR_UTF16_ERR2 (-25)
+#define PCRE2_ERROR_UTF16_ERR3 (-26)
+
+/* Error codes for UTF-32 validity checks */
+
+#define PCRE2_ERROR_UTF32_ERR1 (-27)
+#define PCRE2_ERROR_UTF32_ERR2 (-28)
+
+/* Miscellaneous error codes for pcre2[_dfa]_match(), substring extraction
+functions, context functions, and serializing functions. They are in numerical
+order. Originally they were in alphabetical order too, but now that PCRE2 is
+released, the numbers must not be changed. */
+
+#define PCRE2_ERROR_BADDATA (-29)
+#define PCRE2_ERROR_MIXEDTABLES (-30) /* Name was changed */
+#define PCRE2_ERROR_BADMAGIC (-31)
+#define PCRE2_ERROR_BADMODE (-32)
+#define PCRE2_ERROR_BADOFFSET (-33)
+#define PCRE2_ERROR_BADOPTION (-34)
+#define PCRE2_ERROR_BADREPLACEMENT (-35)
+#define PCRE2_ERROR_BADUTFOFFSET (-36)
+#define PCRE2_ERROR_CALLOUT (-37) /* Never used by PCRE2 itself */
+#define PCRE2_ERROR_DFA_BADRESTART (-38)
+#define PCRE2_ERROR_DFA_RECURSE (-39)
+#define PCRE2_ERROR_DFA_UCOND (-40)
+#define PCRE2_ERROR_DFA_UFUNC (-41)
+#define PCRE2_ERROR_DFA_UITEM (-42)
+#define PCRE2_ERROR_DFA_WSSIZE (-43)
+#define PCRE2_ERROR_INTERNAL (-44)
+#define PCRE2_ERROR_JIT_BADOPTION (-45)
+#define PCRE2_ERROR_JIT_STACKLIMIT (-46)
+#define PCRE2_ERROR_MATCHLIMIT (-47)
+#define PCRE2_ERROR_NOMEMORY (-48)
+#define PCRE2_ERROR_NOSUBSTRING (-49)
+#define PCRE2_ERROR_NOUNIQUESUBSTRING (-50)
+#define PCRE2_ERROR_NULL (-51)
+#define PCRE2_ERROR_RECURSELOOP (-52)
+#define PCRE2_ERROR_DEPTHLIMIT (-53)
+#define PCRE2_ERROR_RECURSIONLIMIT (-53) /* Obsolete synonym */
+#define PCRE2_ERROR_UNAVAILABLE (-54)
+#define PCRE2_ERROR_UNSET (-55)
+#define PCRE2_ERROR_BADOFFSETLIMIT (-56)
+#define PCRE2_ERROR_BADREPESCAPE (-57)
+#define PCRE2_ERROR_REPMISSINGBRACE (-58)
+#define PCRE2_ERROR_BADSUBSTITUTION (-59)
+#define PCRE2_ERROR_BADSUBSPATTERN (-60)
+#define PCRE2_ERROR_TOOMANYREPLACE (-61)
+#define PCRE2_ERROR_BADSERIALIZEDDATA (-62)
+#define PCRE2_ERROR_HEAPLIMIT (-63)
+#define PCRE2_ERROR_CONVERT_SYNTAX (-64)
+#define PCRE2_ERROR_INTERNAL_DUPMATCH (-65)
+
+
+/* Request types for pcre2_pattern_info() */
+
+#define PCRE2_INFO_ALLOPTIONS 0
+#define PCRE2_INFO_ARGOPTIONS 1
+#define PCRE2_INFO_BACKREFMAX 2
+#define PCRE2_INFO_BSR 3
+#define PCRE2_INFO_CAPTURECOUNT 4
+#define PCRE2_INFO_FIRSTCODEUNIT 5
+#define PCRE2_INFO_FIRSTCODETYPE 6
+#define PCRE2_INFO_FIRSTBITMAP 7
+#define PCRE2_INFO_HASCRORLF 8
+#define PCRE2_INFO_JCHANGED 9
+#define PCRE2_INFO_JITSIZE 10
+#define PCRE2_INFO_LASTCODEUNIT 11
+#define PCRE2_INFO_LASTCODETYPE 12
+#define PCRE2_INFO_MATCHEMPTY 13
+#define PCRE2_INFO_MATCHLIMIT 14
+#define PCRE2_INFO_MAXLOOKBEHIND 15
+#define PCRE2_INFO_MINLENGTH 16
+#define PCRE2_INFO_NAMECOUNT 17
+#define PCRE2_INFO_NAMEENTRYSIZE 18
+#define PCRE2_INFO_NAMETABLE 19
+#define PCRE2_INFO_NEWLINE 20
+#define PCRE2_INFO_DEPTHLIMIT 21
+#define PCRE2_INFO_RECURSIONLIMIT 21 /* Obsolete synonym */
+#define PCRE2_INFO_SIZE 22
+#define PCRE2_INFO_HASBACKSLASHC 23
+#define PCRE2_INFO_FRAMESIZE 24
+#define PCRE2_INFO_HEAPLIMIT 25
+#define PCRE2_INFO_EXTRAOPTIONS 26
+
+/* Request types for pcre2_config(). */
+
+#define PCRE2_CONFIG_BSR 0
+#define PCRE2_CONFIG_JIT 1
+#define PCRE2_CONFIG_JITTARGET 2
+#define PCRE2_CONFIG_LINKSIZE 3
+#define PCRE2_CONFIG_MATCHLIMIT 4
+#define PCRE2_CONFIG_NEWLINE 5
+#define PCRE2_CONFIG_PARENSLIMIT 6
+#define PCRE2_CONFIG_DEPTHLIMIT 7
+#define PCRE2_CONFIG_RECURSIONLIMIT 7 /* Obsolete synonym */
+#define PCRE2_CONFIG_STACKRECURSE 8 /* Obsolete */
+#define PCRE2_CONFIG_UNICODE 9
+#define PCRE2_CONFIG_UNICODE_VERSION 10
+#define PCRE2_CONFIG_VERSION 11
+#define PCRE2_CONFIG_HEAPLIMIT 12
+#define PCRE2_CONFIG_NEVER_BACKSLASH_C 13
+#define PCRE2_CONFIG_COMPILED_WIDTHS 14
+
+
+/* Types for code units in patterns and subject strings. */
+
+typedef uint8_t PCRE2_UCHAR8;
+typedef uint16_t PCRE2_UCHAR16;
+typedef uint32_t PCRE2_UCHAR32;
+
+typedef const PCRE2_UCHAR8 *PCRE2_SPTR8;
+typedef const PCRE2_UCHAR16 *PCRE2_SPTR16;
+typedef const PCRE2_UCHAR32 *PCRE2_SPTR32;
+
+/* The PCRE2_SIZE type is used for all string lengths and offsets in PCRE2,
+including pattern offsets for errors and subject offsets after a match. We
+define special values to indicate zero-terminated strings and unset offsets in
+the offset vector (ovector). */
+
+#define PCRE2_SIZE size_t
+#define PCRE2_SIZE_MAX SIZE_MAX
+#define PCRE2_ZERO_TERMINATED (~(PCRE2_SIZE)0)
+#define PCRE2_UNSET (~(PCRE2_SIZE)0)
+
+/* Generic types for opaque structures and JIT callback functions. These
+declarations are defined in a macro that is expanded for each width later. */
+
+#define PCRE2_TYPES_LIST \
+struct pcre2_real_general_context; \
+typedef struct pcre2_real_general_context pcre2_general_context; \
+\
+struct pcre2_real_compile_context; \
+typedef struct pcre2_real_compile_context pcre2_compile_context; \
+\
+struct pcre2_real_match_context; \
+typedef struct pcre2_real_match_context pcre2_match_context; \
+\
+struct pcre2_real_convert_context; \
+typedef struct pcre2_real_convert_context pcre2_convert_context; \
+\
+struct pcre2_real_code; \
+typedef struct pcre2_real_code pcre2_code; \
+\
+struct pcre2_real_match_data; \
+typedef struct pcre2_real_match_data pcre2_match_data; \
+\
+struct pcre2_real_jit_stack; \
+typedef struct pcre2_real_jit_stack pcre2_jit_stack; \
+\
+typedef pcre2_jit_stack *(*pcre2_jit_callback)(void *);
+
+
+/* The structure for passing out data via the pcre_callout_function. We use a
+structure so that new fields can be added on the end in future versions,
+without changing the API of the function, thereby allowing old clients to work
+without modification. Define the generic version in a macro; the width-specific
+versions are generated from this macro below. */
+
+/* Flags for the callout_flags field. These are cleared after a callout. */
+
+#define PCRE2_CALLOUT_STARTMATCH 0x00000001u /* Set for each bumpalong */
+#define PCRE2_CALLOUT_BACKTRACK 0x00000002u /* Set after a backtrack */
+
+#define PCRE2_STRUCTURE_LIST \
+typedef struct pcre2_callout_block { \
+ uint32_t version; /* Identifies version of block */ \
+ /* ------------------------ Version 0 ------------------------------- */ \
+ uint32_t callout_number; /* Number compiled into pattern */ \
+ uint32_t capture_top; /* Max current capture */ \
+ uint32_t capture_last; /* Most recently closed capture */ \
+ PCRE2_SIZE *offset_vector; /* The offset vector */ \
+ PCRE2_SPTR mark; /* Pointer to current mark or NULL */ \
+ PCRE2_SPTR subject; /* The subject being matched */ \
+ PCRE2_SIZE subject_length; /* The length of the subject */ \
+ PCRE2_SIZE start_match; /* Offset to start of this match attempt */ \
+ PCRE2_SIZE current_position; /* Where we currently are in the subject */ \
+ PCRE2_SIZE pattern_position; /* Offset to next item in the pattern */ \
+ PCRE2_SIZE next_item_length; /* Length of next item in the pattern */ \
+ /* ------------------- Added for Version 1 -------------------------- */ \
+ PCRE2_SIZE callout_string_offset; /* Offset to string within pattern */ \
+ PCRE2_SIZE callout_string_length; /* Length of string compiled into pattern */ \
+ PCRE2_SPTR callout_string; /* String compiled into pattern */ \
+ /* ------------------- Added for Version 2 -------------------------- */ \
+ uint32_t callout_flags; /* See above for list */ \
+ /* ------------------------------------------------------------------ */ \
+} pcre2_callout_block; \
+\
+typedef struct pcre2_callout_enumerate_block { \
+ uint32_t version; /* Identifies version of block */ \
+ /* ------------------------ Version 0 ------------------------------- */ \
+ PCRE2_SIZE pattern_position; /* Offset to next item in the pattern */ \
+ PCRE2_SIZE next_item_length; /* Length of next item in the pattern */ \
+ uint32_t callout_number; /* Number compiled into pattern */ \
+ PCRE2_SIZE callout_string_offset; /* Offset to string within pattern */ \
+ PCRE2_SIZE callout_string_length; /* Length of string compiled into pattern */ \
+ PCRE2_SPTR callout_string; /* String compiled into pattern */ \
+ /* ------------------------------------------------------------------ */ \
+} pcre2_callout_enumerate_block;
+
+
+/* List the generic forms of all other functions in macros, which will be
+expanded for each width below. Start with functions that give general
+information. */
+
+#define PCRE2_GENERAL_INFO_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION pcre2_config(uint32_t, void *);
+
+
+/* Functions for manipulating contexts. */
+
+#define PCRE2_GENERAL_CONTEXT_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_general_context PCRE2_CALL_CONVENTION \
+ *pcre2_general_context_copy(pcre2_general_context *); \
+PCRE2_EXP_DECL pcre2_general_context PCRE2_CALL_CONVENTION \
+ *pcre2_general_context_create(void *(*)(PCRE2_SIZE, void *), \
+ void (*)(void *, void *), void *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_general_context_free(pcre2_general_context *);
+
+#define PCRE2_COMPILE_CONTEXT_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_compile_context PCRE2_CALL_CONVENTION \
+ *pcre2_compile_context_copy(pcre2_compile_context *); \
+PCRE2_EXP_DECL pcre2_compile_context PCRE2_CALL_CONVENTION \
+ *pcre2_compile_context_create(pcre2_general_context *);\
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_compile_context_free(pcre2_compile_context *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_bsr(pcre2_compile_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_character_tables(pcre2_compile_context *, const unsigned char *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_compile_extra_options(pcre2_compile_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_max_pattern_length(pcre2_compile_context *, PCRE2_SIZE); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_newline(pcre2_compile_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_parens_nest_limit(pcre2_compile_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_compile_recursion_guard(pcre2_compile_context *, \
+ int (*)(uint32_t, void *), void *);
+
+#define PCRE2_MATCH_CONTEXT_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_match_context PCRE2_CALL_CONVENTION \
+ *pcre2_match_context_copy(pcre2_match_context *); \
+PCRE2_EXP_DECL pcre2_match_context PCRE2_CALL_CONVENTION \
+ *pcre2_match_context_create(pcre2_general_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_match_context_free(pcre2_match_context *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_callout(pcre2_match_context *, \
+ int (*)(pcre2_callout_block *, void *), void *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_depth_limit(pcre2_match_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_heap_limit(pcre2_match_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_match_limit(pcre2_match_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_offset_limit(pcre2_match_context *, PCRE2_SIZE); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_recursion_limit(pcre2_match_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_recursion_memory_management(pcre2_match_context *, \
+ void *(*)(PCRE2_SIZE, void *), void (*)(void *, void *), void *);
+
+#define PCRE2_CONVERT_CONTEXT_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_convert_context PCRE2_CALL_CONVENTION \
+ *pcre2_convert_context_copy(pcre2_convert_context *); \
+PCRE2_EXP_DECL pcre2_convert_context PCRE2_CALL_CONVENTION \
+ *pcre2_convert_context_create(pcre2_general_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_convert_context_free(pcre2_convert_context *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_glob_escape(pcre2_convert_context *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_set_glob_separator(pcre2_convert_context *, uint32_t);
+
+
+/* Functions concerned with compiling a pattern to PCRE internal code. */
+
+#define PCRE2_COMPILE_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_code PCRE2_CALL_CONVENTION \
+ *pcre2_compile(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, \
+ pcre2_compile_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_code_free(pcre2_code *); \
+PCRE2_EXP_DECL pcre2_code PCRE2_CALL_CONVENTION \
+ *pcre2_code_copy(const pcre2_code *); \
+PCRE2_EXP_DECL pcre2_code PCRE2_CALL_CONVENTION \
+ *pcre2_code_copy_with_tables(const pcre2_code *);
+
+
+/* Functions that give information about a compiled pattern. */
+
+#define PCRE2_PATTERN_INFO_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_pattern_info(const pcre2_code *, uint32_t, void *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_callout_enumerate(const pcre2_code *, \
+ int (*)(pcre2_callout_enumerate_block *, void *), void *);
+
+
+/* Functions for running a match and inspecting the result. */
+
+#define PCRE2_MATCH_FUNCTIONS \
+PCRE2_EXP_DECL pcre2_match_data PCRE2_CALL_CONVENTION \
+ *pcre2_match_data_create(uint32_t, pcre2_general_context *); \
+PCRE2_EXP_DECL pcre2_match_data PCRE2_CALL_CONVENTION \
+ *pcre2_match_data_create_from_pattern(const pcre2_code *, \
+ pcre2_general_context *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_dfa_match(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, \
+ uint32_t, pcre2_match_data *, pcre2_match_context *, int *, PCRE2_SIZE); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_match(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, \
+ uint32_t, pcre2_match_data *, pcre2_match_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_match_data_free(pcre2_match_data *); \
+PCRE2_EXP_DECL PCRE2_SPTR PCRE2_CALL_CONVENTION \
+ pcre2_get_mark(pcre2_match_data *); \
+PCRE2_EXP_DECL uint32_t PCRE2_CALL_CONVENTION \
+ pcre2_get_ovector_count(pcre2_match_data *); \
+PCRE2_EXP_DECL PCRE2_SIZE PCRE2_CALL_CONVENTION \
+ *pcre2_get_ovector_pointer(pcre2_match_data *); \
+PCRE2_EXP_DECL PCRE2_SIZE PCRE2_CALL_CONVENTION \
+ pcre2_get_startchar(pcre2_match_data *);
+
+
+/* Convenience functions for handling matched substrings. */
+
+#define PCRE2_SUBSTRING_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_copy_byname(pcre2_match_data *, PCRE2_SPTR, PCRE2_UCHAR *, \
+ PCRE2_SIZE *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_copy_bynumber(pcre2_match_data *, uint32_t, PCRE2_UCHAR *, \
+ PCRE2_SIZE *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_substring_free(PCRE2_UCHAR *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_get_byname(pcre2_match_data *, PCRE2_SPTR, PCRE2_UCHAR **, \
+ PCRE2_SIZE *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_get_bynumber(pcre2_match_data *, uint32_t, PCRE2_UCHAR **, \
+ PCRE2_SIZE *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_length_byname(pcre2_match_data *, PCRE2_SPTR, PCRE2_SIZE *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_length_bynumber(pcre2_match_data *, uint32_t, PCRE2_SIZE *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_nametable_scan(const pcre2_code *, PCRE2_SPTR, PCRE2_SPTR *, \
+ PCRE2_SPTR *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_number_from_name(const pcre2_code *, PCRE2_SPTR); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_substring_list_free(PCRE2_SPTR *); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substring_list_get(pcre2_match_data *, PCRE2_UCHAR ***, PCRE2_SIZE **);
+
+/* Functions for serializing / deserializing compiled patterns. */
+
+#define PCRE2_SERIALIZE_FUNCTIONS \
+PCRE2_EXP_DECL int32_t PCRE2_CALL_CONVENTION \
+ pcre2_serialize_encode(const pcre2_code **, int32_t, uint8_t **, \
+ PCRE2_SIZE *, pcre2_general_context *); \
+PCRE2_EXP_DECL int32_t PCRE2_CALL_CONVENTION \
+ pcre2_serialize_decode(pcre2_code **, int32_t, const uint8_t *, \
+ pcre2_general_context *); \
+PCRE2_EXP_DECL int32_t PCRE2_CALL_CONVENTION \
+ pcre2_serialize_get_number_of_codes(const uint8_t *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_serialize_free(uint8_t *);
+
+
+/* Convenience function for match + substitute. */
+
+#define PCRE2_SUBSTITUTE_FUNCTION \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_substitute(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, \
+ uint32_t, pcre2_match_data *, pcre2_match_context *, PCRE2_SPTR, \
+ PCRE2_SIZE, PCRE2_UCHAR *, PCRE2_SIZE *);
+
+
+/* Functions for converting pattern source strings. */
+
+#define PCRE2_CONVERT_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_pattern_convert(PCRE2_SPTR, PCRE2_SIZE, uint32_t, PCRE2_UCHAR **, \
+ PCRE2_SIZE *, pcre2_convert_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_converted_pattern_free(PCRE2_UCHAR *);
+
+
+/* Functions for JIT processing */
+
+#define PCRE2_JIT_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_jit_compile(pcre2_code *, uint32_t); \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_jit_match(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, \
+ uint32_t, pcre2_match_data *, pcre2_match_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_jit_free_unused_memory(pcre2_general_context *); \
+PCRE2_EXP_DECL pcre2_jit_stack PCRE2_CALL_CONVENTION \
+ *pcre2_jit_stack_create(PCRE2_SIZE, PCRE2_SIZE, pcre2_general_context *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_jit_stack_assign(pcre2_match_context *, pcre2_jit_callback, void *); \
+PCRE2_EXP_DECL void PCRE2_CALL_CONVENTION \
+ pcre2_jit_stack_free(pcre2_jit_stack *);
+
+
+/* Other miscellaneous functions. */
+
+#define PCRE2_OTHER_FUNCTIONS \
+PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
+ pcre2_get_error_message(int, PCRE2_UCHAR *, PCRE2_SIZE); \
+PCRE2_EXP_DECL const uint8_t PCRE2_CALL_CONVENTION \
+ *pcre2_maketables(pcre2_general_context *); \
+
+
+/* Define macros that generate width-specific names from generic versions. The
+three-level macro scheme is necessary to get the macros expanded when we want
+them to be. First we get the width from PCRE2_LOCAL_WIDTH, which is used for
+generating three versions of everything below. After that, PCRE2_SUFFIX will be
+re-defined to use PCRE2_CODE_UNIT_WIDTH, for use when macros such as
+pcre2_compile are called by application code. */
+
+#define PCRE2_JOIN(a,b) a ## b
+#define PCRE2_GLUE(a,b) PCRE2_JOIN(a,b)
+#define PCRE2_SUFFIX(a) PCRE2_GLUE(a,PCRE2_LOCAL_WIDTH)
+
+
+/* Data types */
+
+#define PCRE2_UCHAR PCRE2_SUFFIX(PCRE2_UCHAR)
+#define PCRE2_SPTR PCRE2_SUFFIX(PCRE2_SPTR)
+
+#define pcre2_code PCRE2_SUFFIX(pcre2_code_)
+#define pcre2_jit_callback PCRE2_SUFFIX(pcre2_jit_callback_)
+#define pcre2_jit_stack PCRE2_SUFFIX(pcre2_jit_stack_)
+
+#define pcre2_real_code PCRE2_SUFFIX(pcre2_real_code_)
+#define pcre2_real_general_context PCRE2_SUFFIX(pcre2_real_general_context_)
+#define pcre2_real_compile_context PCRE2_SUFFIX(pcre2_real_compile_context_)
+#define pcre2_real_convert_context PCRE2_SUFFIX(pcre2_real_convert_context_)
+#define pcre2_real_match_context PCRE2_SUFFIX(pcre2_real_match_context_)
+#define pcre2_real_jit_stack PCRE2_SUFFIX(pcre2_real_jit_stack_)
+#define pcre2_real_match_data PCRE2_SUFFIX(pcre2_real_match_data_)
+
+
+/* Data blocks */
+
+#define pcre2_callout_block PCRE2_SUFFIX(pcre2_callout_block_)
+#define pcre2_callout_enumerate_block PCRE2_SUFFIX(pcre2_callout_enumerate_block_)
+#define pcre2_general_context PCRE2_SUFFIX(pcre2_general_context_)
+#define pcre2_compile_context PCRE2_SUFFIX(pcre2_compile_context_)
+#define pcre2_convert_context PCRE2_SUFFIX(pcre2_convert_context_)
+#define pcre2_match_context PCRE2_SUFFIX(pcre2_match_context_)
+#define pcre2_match_data PCRE2_SUFFIX(pcre2_match_data_)
+
+
+/* Functions: the complete list in alphabetical order */
+
+#define pcre2_callout_enumerate PCRE2_SUFFIX(pcre2_callout_enumerate_)
+#define pcre2_code_copy PCRE2_SUFFIX(pcre2_code_copy_)
+#define pcre2_code_copy_with_tables PCRE2_SUFFIX(pcre2_code_copy_with_tables_)
+#define pcre2_code_free PCRE2_SUFFIX(pcre2_code_free_)
+#define pcre2_compile PCRE2_SUFFIX(pcre2_compile_)
+#define pcre2_compile_context_copy PCRE2_SUFFIX(pcre2_compile_context_copy_)
+#define pcre2_compile_context_create PCRE2_SUFFIX(pcre2_compile_context_create_)
+#define pcre2_compile_context_free PCRE2_SUFFIX(pcre2_compile_context_free_)
+#define pcre2_config PCRE2_SUFFIX(pcre2_config_)
+#define pcre2_convert_context_copy PCRE2_SUFFIX(pcre2_convert_context_copy_)
+#define pcre2_convert_context_create PCRE2_SUFFIX(pcre2_convert_context_create_)
+#define pcre2_convert_context_free PCRE2_SUFFIX(pcre2_convert_context_free_)
+#define pcre2_converted_pattern_free PCRE2_SUFFIX(pcre2_converted_pattern_free_)
+#define pcre2_dfa_match PCRE2_SUFFIX(pcre2_dfa_match_)
+#define pcre2_general_context_copy PCRE2_SUFFIX(pcre2_general_context_copy_)
+#define pcre2_general_context_create PCRE2_SUFFIX(pcre2_general_context_create_)
+#define pcre2_general_context_free PCRE2_SUFFIX(pcre2_general_context_free_)
+#define pcre2_get_error_message PCRE2_SUFFIX(pcre2_get_error_message_)
+#define pcre2_get_mark PCRE2_SUFFIX(pcre2_get_mark_)
+#define pcre2_get_ovector_pointer PCRE2_SUFFIX(pcre2_get_ovector_pointer_)
+#define pcre2_get_ovector_count PCRE2_SUFFIX(pcre2_get_ovector_count_)
+#define pcre2_get_startchar PCRE2_SUFFIX(pcre2_get_startchar_)
+#define pcre2_jit_compile PCRE2_SUFFIX(pcre2_jit_compile_)
+#define pcre2_jit_match PCRE2_SUFFIX(pcre2_jit_match_)
+#define pcre2_jit_free_unused_memory PCRE2_SUFFIX(pcre2_jit_free_unused_memory_)
+#define pcre2_jit_stack_assign PCRE2_SUFFIX(pcre2_jit_stack_assign_)
+#define pcre2_jit_stack_create PCRE2_SUFFIX(pcre2_jit_stack_create_)
+#define pcre2_jit_stack_free PCRE2_SUFFIX(pcre2_jit_stack_free_)
+#define pcre2_maketables PCRE2_SUFFIX(pcre2_maketables_)
+#define pcre2_match PCRE2_SUFFIX(pcre2_match_)
+#define pcre2_match_context_copy PCRE2_SUFFIX(pcre2_match_context_copy_)
+#define pcre2_match_context_create PCRE2_SUFFIX(pcre2_match_context_create_)
+#define pcre2_match_context_free PCRE2_SUFFIX(pcre2_match_context_free_)
+#define pcre2_match_data_create PCRE2_SUFFIX(pcre2_match_data_create_)
+#define pcre2_match_data_create_from_pattern PCRE2_SUFFIX(pcre2_match_data_create_from_pattern_)
+#define pcre2_match_data_free PCRE2_SUFFIX(pcre2_match_data_free_)
+#define pcre2_pattern_convert PCRE2_SUFFIX(pcre2_pattern_convert_)
+#define pcre2_pattern_info PCRE2_SUFFIX(pcre2_pattern_info_)
+#define pcre2_serialize_decode PCRE2_SUFFIX(pcre2_serialize_decode_)
+#define pcre2_serialize_encode PCRE2_SUFFIX(pcre2_serialize_encode_)
+#define pcre2_serialize_free PCRE2_SUFFIX(pcre2_serialize_free_)
+#define pcre2_serialize_get_number_of_codes PCRE2_SUFFIX(pcre2_serialize_get_number_of_codes_)
+#define pcre2_set_bsr PCRE2_SUFFIX(pcre2_set_bsr_)
+#define pcre2_set_callout PCRE2_SUFFIX(pcre2_set_callout_)
+#define pcre2_set_character_tables PCRE2_SUFFIX(pcre2_set_character_tables_)
+#define pcre2_set_compile_extra_options PCRE2_SUFFIX(pcre2_set_compile_extra_options_)
+#define pcre2_set_compile_recursion_guard PCRE2_SUFFIX(pcre2_set_compile_recursion_guard_)
+#define pcre2_set_depth_limit PCRE2_SUFFIX(pcre2_set_depth_limit_)
+#define pcre2_set_glob_escape PCRE2_SUFFIX(pcre2_set_glob_escape_)
+#define pcre2_set_glob_separator PCRE2_SUFFIX(pcre2_set_glob_separator_)
+#define pcre2_set_heap_limit PCRE2_SUFFIX(pcre2_set_heap_limit_)
+#define pcre2_set_match_limit PCRE2_SUFFIX(pcre2_set_match_limit_)
+#define pcre2_set_max_pattern_length PCRE2_SUFFIX(pcre2_set_max_pattern_length_)
+#define pcre2_set_newline PCRE2_SUFFIX(pcre2_set_newline_)
+#define pcre2_set_parens_nest_limit PCRE2_SUFFIX(pcre2_set_parens_nest_limit_)
+#define pcre2_set_offset_limit PCRE2_SUFFIX(pcre2_set_offset_limit_)
+#define pcre2_substitute PCRE2_SUFFIX(pcre2_substitute_)
+#define pcre2_substring_copy_byname PCRE2_SUFFIX(pcre2_substring_copy_byname_)
+#define pcre2_substring_copy_bynumber PCRE2_SUFFIX(pcre2_substring_copy_bynumber_)
+#define pcre2_substring_free PCRE2_SUFFIX(pcre2_substring_free_)
+#define pcre2_substring_get_byname PCRE2_SUFFIX(pcre2_substring_get_byname_)
+#define pcre2_substring_get_bynumber PCRE2_SUFFIX(pcre2_substring_get_bynumber_)
+#define pcre2_substring_length_byname PCRE2_SUFFIX(pcre2_substring_length_byname_)
+#define pcre2_substring_length_bynumber PCRE2_SUFFIX(pcre2_substring_length_bynumber_)
+#define pcre2_substring_list_get PCRE2_SUFFIX(pcre2_substring_list_get_)
+#define pcre2_substring_list_free PCRE2_SUFFIX(pcre2_substring_list_free_)
+#define pcre2_substring_nametable_scan PCRE2_SUFFIX(pcre2_substring_nametable_scan_)
+#define pcre2_substring_number_from_name PCRE2_SUFFIX(pcre2_substring_number_from_name_)
+
+/* Keep this old function name for backwards compatibility */
+#define pcre2_set_recursion_limit PCRE2_SUFFIX(pcre2_set_recursion_limit_)
+
+/* Keep this obsolete function for backwards compatibility: it is now a noop. */
+#define pcre2_set_recursion_memory_management PCRE2_SUFFIX(pcre2_set_recursion_memory_management_)
+
+/* Now generate all three sets of width-specific structures and function
+prototypes. */
+
+#define PCRE2_TYPES_STRUCTURES_AND_FUNCTIONS \
+PCRE2_TYPES_LIST \
+PCRE2_STRUCTURE_LIST \
+PCRE2_GENERAL_INFO_FUNCTIONS \
+PCRE2_GENERAL_CONTEXT_FUNCTIONS \
+PCRE2_COMPILE_CONTEXT_FUNCTIONS \
+PCRE2_CONVERT_CONTEXT_FUNCTIONS \
+PCRE2_CONVERT_FUNCTIONS \
+PCRE2_MATCH_CONTEXT_FUNCTIONS \
+PCRE2_COMPILE_FUNCTIONS \
+PCRE2_PATTERN_INFO_FUNCTIONS \
+PCRE2_MATCH_FUNCTIONS \
+PCRE2_SUBSTRING_FUNCTIONS \
+PCRE2_SERIALIZE_FUNCTIONS \
+PCRE2_SUBSTITUTE_FUNCTION \
+PCRE2_JIT_FUNCTIONS \
+PCRE2_OTHER_FUNCTIONS
+
+#define PCRE2_LOCAL_WIDTH 8
+PCRE2_TYPES_STRUCTURES_AND_FUNCTIONS
+#undef PCRE2_LOCAL_WIDTH
+
+#define PCRE2_LOCAL_WIDTH 16
+PCRE2_TYPES_STRUCTURES_AND_FUNCTIONS
+#undef PCRE2_LOCAL_WIDTH
+
+#define PCRE2_LOCAL_WIDTH 32
+PCRE2_TYPES_STRUCTURES_AND_FUNCTIONS
+#undef PCRE2_LOCAL_WIDTH
+
+/* Undefine the list macros; they are no longer needed. */
+
+#undef PCRE2_TYPES_LIST
+#undef PCRE2_STRUCTURE_LIST
+#undef PCRE2_GENERAL_INFO_FUNCTIONS
+#undef PCRE2_GENERAL_CONTEXT_FUNCTIONS
+#undef PCRE2_COMPILE_CONTEXT_FUNCTIONS
+#undef PCRE2_CONVERT_CONTEXT_FUNCTIONS
+#undef PCRE2_MATCH_CONTEXT_FUNCTIONS
+#undef PCRE2_COMPILE_FUNCTIONS
+#undef PCRE2_PATTERN_INFO_FUNCTIONS
+#undef PCRE2_MATCH_FUNCTIONS
+#undef PCRE2_SUBSTRING_FUNCTIONS
+#undef PCRE2_SERIALIZE_FUNCTIONS
+#undef PCRE2_SUBSTITUTE_FUNCTION
+#undef PCRE2_JIT_FUNCTIONS
+#undef PCRE2_OTHER_FUNCTIONS
+#undef PCRE2_TYPES_STRUCTURES_AND_FUNCTIONS
+
+/* PCRE2_CODE_UNIT_WIDTH must be defined. If it is 8, 16, or 32, redefine
+PCRE2_SUFFIX to use it. If it is 0, undefine the other macros and make
+PCRE2_SUFFIX a no-op. Otherwise, generate an error. */
+
+#undef PCRE2_SUFFIX
+#ifndef PCRE2_CODE_UNIT_WIDTH
+#error PCRE2_CODE_UNIT_WIDTH must be defined before including pcre2.h.
+#error Use 8, 16, or 32; or 0 for a multi-width application.
+#else /* PCRE2_CODE_UNIT_WIDTH is defined */
+#if PCRE2_CODE_UNIT_WIDTH == 8 || \
+ PCRE2_CODE_UNIT_WIDTH == 16 || \
+ PCRE2_CODE_UNIT_WIDTH == 32
+#define PCRE2_SUFFIX(a) PCRE2_GLUE(a, PCRE2_CODE_UNIT_WIDTH)
+#elif PCRE2_CODE_UNIT_WIDTH == 0
+#undef PCRE2_JOIN
+#undef PCRE2_GLUE
+#define PCRE2_SUFFIX(a) a
+#else
+#error PCRE2_CODE_UNIT_WIDTH must be 0, 8, 16, or 32.
+#endif
+#endif /* PCRE2_CODE_UNIT_WIDTH is defined */
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* PCRE2_H_IDEMPOTENT_GUARD */
+
+/* End of pcre2.h */
diff --git a/test/monniaux/pcre2-10.32/pcre2_auto_possess.c b/test/monniaux/pcre2-10.32/pcre2_auto_possess.c
new file mode 100644
index 00000000..2ce152e9
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_auto_possess.c
@@ -0,0 +1,1322 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains functions that scan a compiled pattern and change
+repeats into possessive repeats where possible. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+#include "pcre2_internal.h"
+
+
+/*************************************************
+* Tables for auto-possessification *
+*************************************************/
+
+/* This table is used to check whether auto-possessification is possible
+between adjacent character-type opcodes. The left-hand (repeated) opcode is
+used to select the row, and the right-hand opcode is use to select the column.
+A value of 1 means that auto-possessification is OK. For example, the second
+value in the first row means that \D+\d can be turned into \D++\d.
+
+The Unicode property types (\P and \p) have to be present to fill out the table
+because of what their opcode values are, but the table values should always be
+zero because property types are handled separately in the code. The last four
+columns apply to items that cannot be repeated, so there is no need to have
+rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
+*not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
+
+#define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
+#define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
+
+static const uint8_t autoposstab[APTROWS][APTCOLS] = {
+/* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */
+ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */
+ { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */
+ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */
+ { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */
+ { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */
+ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */
+ { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */
+ { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */
+ { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */
+};
+
+#ifdef SUPPORT_UNICODE
+/* This table is used to check whether auto-possessification is possible
+between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
+left-hand (repeated) opcode is used to select the row, and the right-hand
+opcode is used to select the column. The values are as follows:
+
+ 0 Always return FALSE (never auto-possessify)
+ 1 Character groups are distinct (possessify if both are OP_PROP)
+ 2 Check character categories in the same group (general or particular)
+ 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP)
+
+ 4 Check left general category vs right particular category
+ 5 Check right general category vs left particular category
+
+ 6 Left alphanum vs right general category
+ 7 Left space vs right general category
+ 8 Left word vs right general category
+
+ 9 Right alphanum vs left general category
+ 10 Right space vs left general category
+ 11 Right word vs left general category
+
+ 12 Left alphanum vs right particular category
+ 13 Left space vs right particular category
+ 14 Left word vs right particular category
+
+ 15 Right alphanum vs left particular category
+ 16 Right space vs left particular category
+ 17 Right word vs left particular category
+*/
+
+static const uint8_t propposstab[PT_TABSIZE][PT_TABSIZE] = {
+/* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */
+ { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */
+ { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */
+ { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */
+ { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */
+ { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */
+ { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */
+ { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */
+ { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */
+};
+
+/* This table is used to check whether auto-possessification is possible
+between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
+specifies a general category and the other specifies a particular category. The
+row is selected by the general category and the column by the particular
+category. The value is 1 if the particular category is not part of the general
+category. */
+
+static const uint8_t catposstab[7][30] = {
+/* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
+ { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */
+ { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */
+ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */
+ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */
+ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */
+ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */
+ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */
+};
+
+/* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
+a general or particular category. The properties in each row are those
+that apply to the character set in question. Duplication means that a little
+unnecessary work is done when checking, but this keeps things much simpler
+because they can all use the same code. For more details see the comment where
+this table is used.
+
+Note: SPACE and PXSPACE used to be different because Perl excluded VT from
+"space", but from Perl 5.18 it's included, so both categories are treated the
+same here. */
+
+static const uint8_t posspropstab[3][4] = {
+ { ucp_L, ucp_N, ucp_N, ucp_Nl }, /* ALNUM, 3rd and 4th values redundant */
+ { ucp_Z, ucp_Z, ucp_C, ucp_Cc }, /* SPACE and PXSPACE, 2nd value redundant */
+ { ucp_L, ucp_N, ucp_P, ucp_Po } /* WORD */
+};
+#endif /* SUPPORT_UNICODE */
+
+
+
+#ifdef SUPPORT_UNICODE
+/*************************************************
+* Check a character and a property *
+*************************************************/
+
+/* This function is called by compare_opcodes() when a property item is
+adjacent to a fixed character.
+
+Arguments:
+ c the character
+ ptype the property type
+ pdata the data for the type
+ negated TRUE if it's a negated property (\P or \p{^)
+
+Returns: TRUE if auto-possessifying is OK
+*/
+
+static BOOL
+check_char_prop(uint32_t c, unsigned int ptype, unsigned int pdata,
+ BOOL negated)
+{
+const uint32_t *p;
+const ucd_record *prop = GET_UCD(c);
+
+switch(ptype)
+ {
+ case PT_LAMP:
+ return (prop->chartype == ucp_Lu ||
+ prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt) == negated;
+
+ case PT_GC:
+ return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
+
+ case PT_PC:
+ return (pdata == prop->chartype) == negated;
+
+ case PT_SC:
+ return (pdata == prop->script) == negated;
+
+ /* These are specials */
+
+ case PT_ALNUM:
+ return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
+ means that Perl space and POSIX space are now identical. PCRE was changed
+ at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ return negated;
+
+ default:
+ return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
+ }
+ break; /* Control never reaches here */
+
+ case PT_WORD:
+ return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ c == CHAR_UNDERSCORE) == negated;
+
+ case PT_CLIST:
+ p = PRIV(ucd_caseless_sets) + prop->caseset;
+ for (;;)
+ {
+ if (c < *p) return !negated;
+ if (c == *p++) return negated;
+ }
+ break; /* Control never reaches here */
+ }
+
+return FALSE;
+}
+#endif /* SUPPORT_UNICODE */
+
+
+
+/*************************************************
+* Base opcode of repeated opcodes *
+*************************************************/
+
+/* Returns the base opcode for repeated single character type opcodes. If the
+opcode is not a repeated character type, it returns with the original value.
+
+Arguments: c opcode
+Returns: base opcode for the type
+*/
+
+static PCRE2_UCHAR
+get_repeat_base(PCRE2_UCHAR c)
+{
+return (c > OP_TYPEPOSUPTO)? c :
+ (c >= OP_TYPESTAR)? OP_TYPESTAR :
+ (c >= OP_NOTSTARI)? OP_NOTSTARI :
+ (c >= OP_NOTSTAR)? OP_NOTSTAR :
+ (c >= OP_STARI)? OP_STARI :
+ OP_STAR;
+}
+
+
+/*************************************************
+* Fill the character property list *
+*************************************************/
+
+/* Checks whether the code points to an opcode that can take part in auto-
+possessification, and if so, fills a list with its properties.
+
+Arguments:
+ code points to start of expression
+ utf TRUE if in UTF mode
+ fcc points to the case-flipping table
+ list points to output list
+ list[0] will be filled with the opcode
+ list[1] will be non-zero if this opcode
+ can match an empty character string
+ list[2..7] depends on the opcode
+
+Returns: points to the start of the next opcode if *code is accepted
+ NULL if *code is not accepted
+*/
+
+static PCRE2_SPTR
+get_chr_property_list(PCRE2_SPTR code, BOOL utf, const uint8_t *fcc,
+ uint32_t *list)
+{
+PCRE2_UCHAR c = *code;
+PCRE2_UCHAR base;
+PCRE2_SPTR end;
+uint32_t chr;
+
+#ifdef SUPPORT_UNICODE
+uint32_t *clist_dest;
+const uint32_t *clist_src;
+#else
+(void)utf; /* Suppress "unused parameter" compiler warning */
+#endif
+
+list[0] = c;
+list[1] = FALSE;
+code++;
+
+if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
+ {
+ base = get_repeat_base(c);
+ c -= (base - OP_STAR);
+
+ if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
+ code += IMM2_SIZE;
+
+ list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT &&
+ c != OP_POSPLUS);
+
+ switch(base)
+ {
+ case OP_STAR:
+ list[0] = OP_CHAR;
+ break;
+
+ case OP_STARI:
+ list[0] = OP_CHARI;
+ break;
+
+ case OP_NOTSTAR:
+ list[0] = OP_NOT;
+ break;
+
+ case OP_NOTSTARI:
+ list[0] = OP_NOTI;
+ break;
+
+ case OP_TYPESTAR:
+ list[0] = *code;
+ code++;
+ break;
+ }
+ c = list[0];
+ }
+
+switch(c)
+ {
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+ case OP_ANYNL:
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ case OP_EXTUNI:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_DOLL:
+ case OP_DOLLM:
+ return code;
+
+ case OP_CHAR:
+ case OP_NOT:
+ GETCHARINCTEST(chr, code);
+ list[2] = chr;
+ list[3] = NOTACHAR;
+ return code;
+
+ case OP_CHARI:
+ case OP_NOTI:
+ list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
+ GETCHARINCTEST(chr, code);
+ list[2] = chr;
+
+#ifdef SUPPORT_UNICODE
+ if (chr < 128 || (chr < 256 && !utf))
+ list[3] = fcc[chr];
+ else
+ list[3] = UCD_OTHERCASE(chr);
+#elif defined SUPPORT_WIDE_CHARS
+ list[3] = (chr < 256) ? fcc[chr] : chr;
+#else
+ list[3] = fcc[chr];
+#endif
+
+ /* The othercase might be the same value. */
+
+ if (chr == list[3])
+ list[3] = NOTACHAR;
+ else
+ list[4] = NOTACHAR;
+ return code;
+
+#ifdef SUPPORT_UNICODE
+ case OP_PROP:
+ case OP_NOTPROP:
+ if (code[0] != PT_CLIST)
+ {
+ list[2] = code[0];
+ list[3] = code[1];
+ return code + 2;
+ }
+
+ /* Convert only if we have enough space. */
+
+ clist_src = PRIV(ucd_caseless_sets) + code[1];
+ clist_dest = list + 2;
+ code += 2;
+
+ do {
+ if (clist_dest >= list + 8)
+ {
+ /* Early return if there is not enough space. This should never
+ happen, since all clists are shorter than 5 character now. */
+ list[2] = code[0];
+ list[3] = code[1];
+ return code;
+ }
+ *clist_dest++ = *clist_src;
+ }
+ while(*clist_src++ != NOTACHAR);
+
+ /* All characters are stored. The terminating NOTACHAR is copied from the
+ clist itself. */
+
+ list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
+ return code;
+#endif
+
+ case OP_NCLASS:
+ case OP_CLASS:
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ if (c == OP_XCLASS)
+ end = code + GET(code, 0) - 1;
+ else
+#endif
+ end = code + 32 / sizeof(PCRE2_UCHAR);
+
+ switch(*end)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSQUERY:
+ list[1] = TRUE;
+ end++;
+ break;
+
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSPLUS:
+ end++;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ list[1] = (GET2(end, 1) == 0);
+ end += 1 + 2 * IMM2_SIZE;
+ break;
+ }
+ list[2] = (uint32_t)(end - code);
+ return end;
+ }
+return NULL; /* Opcode not accepted */
+}
+
+
+
+/*************************************************
+* Scan further character sets for match *
+*************************************************/
+
+/* Checks whether the base and the current opcode have a common character, in
+which case the base cannot be possessified.
+
+Arguments:
+ code points to the byte code
+ utf TRUE in UTF mode
+ cb compile data block
+ base_list the data list of the base opcode
+ base_end the end of the base opcode
+ rec_limit points to recursion depth counter
+
+Returns: TRUE if the auto-possessification is possible
+*/
+
+static BOOL
+compare_opcodes(PCRE2_SPTR code, BOOL utf, const compile_block *cb,
+ const uint32_t *base_list, PCRE2_SPTR base_end, int *rec_limit)
+{
+PCRE2_UCHAR c;
+uint32_t list[8];
+const uint32_t *chr_ptr;
+const uint32_t *ochr_ptr;
+const uint32_t *list_ptr;
+PCRE2_SPTR next_code;
+#ifdef SUPPORT_WIDE_CHARS
+PCRE2_SPTR xclass_flags;
+#endif
+const uint8_t *class_bitset;
+const uint8_t *set1, *set2, *set_end;
+uint32_t chr;
+BOOL accepted, invert_bits;
+BOOL entered_a_group = FALSE;
+
+if (--(*rec_limit) <= 0) return FALSE; /* Recursion has gone too deep */
+
+/* Note: the base_list[1] contains whether the current opcode has a greedy
+(represented by a non-zero value) quantifier. This is a different from
+other character type lists, which store here that the character iterator
+matches to an empty string (also represented by a non-zero value). */
+
+for(;;)
+ {
+ /* All operations move the code pointer forward.
+ Therefore infinite recursions are not possible. */
+
+ c = *code;
+
+ /* Skip over callouts */
+
+ if (c == OP_CALLOUT)
+ {
+ code += PRIV(OP_lengths)[c];
+ continue;
+ }
+
+ if (c == OP_CALLOUT_STR)
+ {
+ code += GET(code, 1 + 2*LINK_SIZE);
+ continue;
+ }
+
+ /* At the end of a branch, skip to the end of the group. */
+
+ if (c == OP_ALT)
+ {
+ do code += GET(code, 1); while (*code == OP_ALT);
+ c = *code;
+ }
+
+ /* Inspect the next opcode. */
+
+ switch(c)
+ {
+ /* We can always possessify a greedy iterator at the end of the pattern,
+ which is reached after skipping over the final OP_KET. A non-greedy
+ iterator must never be possessified. */
+
+ case OP_END:
+ return base_list[1] != 0;
+
+ /* When an iterator is at the end of certain kinds of group we can inspect
+ what follows the group by skipping over the closing ket. Note that this
+ does not apply to OP_KETRMAX or OP_KETRMIN because what follows any given
+ iteration is variable (could be another iteration or could be the next
+ item). As these two opcodes are not listed in the next switch, they will
+ end up as the next code to inspect, and return FALSE by virtue of being
+ unsupported. */
+
+ case OP_KET:
+ case OP_KETRPOS:
+ /* The non-greedy case cannot be converted to a possessive form. */
+
+ if (base_list[1] == 0) return FALSE;
+
+ /* If the bracket is capturing it might be referenced by an OP_RECURSE
+ so its last iterator can never be possessified if the pattern contains
+ recursions. (This could be improved by keeping a list of group numbers that
+ are called by recursion.) */
+
+ switch(*(code - GET(code, 1)))
+ {
+ case OP_CBRA:
+ case OP_SCBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ if (cb->had_recurse) return FALSE;
+ break;
+
+ /* Atomic sub-patterns and assertions can always auto-possessify their
+ last iterator. However, if the group was entered as a result of checking
+ a previous iterator, this is not possible. */
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+
+ return !entered_a_group;
+ }
+
+ /* Skip over the bracket and inspect what comes next. */
+
+ code += PRIV(OP_lengths)[c];
+ continue;
+
+ /* Handle cases where the next item is a group. */
+
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_CBRA:
+ next_code = code + GET(code, 1);
+ code += PRIV(OP_lengths)[c];
+
+ /* Check each branch. We have to recurse a level for all but the last
+ branch. */
+
+ while (*next_code == OP_ALT)
+ {
+ if (!compare_opcodes(code, utf, cb, base_list, base_end, rec_limit))
+ return FALSE;
+ code = next_code + 1 + LINK_SIZE;
+ next_code += GET(next_code, 1);
+ }
+
+ entered_a_group = TRUE;
+ continue;
+
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+
+ next_code = code + 1;
+ if (*next_code != OP_BRA && *next_code != OP_CBRA &&
+ *next_code != OP_ONCE) return FALSE;
+
+ do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
+
+ /* The bracket content will be checked by the OP_BRA/OP_CBRA case above. */
+
+ next_code += 1 + LINK_SIZE;
+ if (!compare_opcodes(next_code, utf, cb, base_list, base_end, rec_limit))
+ return FALSE;
+
+ code += PRIV(OP_lengths)[c];
+ continue;
+
+ /* The next opcode does not need special handling; fall through and use it
+ to see if the base can be possessified. */
+
+ default:
+ break;
+ }
+
+ /* We now have the next appropriate opcode to compare with the base. Check
+ for a supported opcode, and load its properties. */
+
+ code = get_chr_property_list(code, utf, cb->fcc, list);
+ if (code == NULL) return FALSE; /* Unsupported */
+
+ /* If either opcode is a small character list, set pointers for comparing
+ characters from that list with another list, or with a property. */
+
+ if (base_list[0] == OP_CHAR)
+ {
+ chr_ptr = base_list + 2;
+ list_ptr = list;
+ }
+ else if (list[0] == OP_CHAR)
+ {
+ chr_ptr = list + 2;
+ list_ptr = base_list;
+ }
+
+ /* Character bitsets can also be compared to certain opcodes. */
+
+ else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
+ || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
+#endif
+ )
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
+#else
+ if (base_list[0] == OP_CLASS)
+#endif
+ {
+ set1 = (uint8_t *)(base_end - base_list[2]);
+ list_ptr = list;
+ }
+ else
+ {
+ set1 = (uint8_t *)(code - list[2]);
+ list_ptr = base_list;
+ }
+
+ invert_bits = FALSE;
+ switch(list_ptr[0])
+ {
+ case OP_CLASS:
+ case OP_NCLASS:
+ set2 = (uint8_t *)
+ ((list_ptr == list ? code : base_end) - list_ptr[2]);
+ break;
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
+ if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
+ if ((*xclass_flags & XCL_MAP) == 0)
+ {
+ /* No bits are set for characters < 256. */
+ if (list[1] == 0) return (*xclass_flags & XCL_NOT) == 0;
+ /* Might be an empty repeat. */
+ continue;
+ }
+ set2 = (uint8_t *)(xclass_flags + 1);
+ break;
+#endif
+
+ case OP_NOT_DIGIT:
+ invert_bits = TRUE;
+ /* Fall through */
+ case OP_DIGIT:
+ set2 = (uint8_t *)(cb->cbits + cbit_digit);
+ break;
+
+ case OP_NOT_WHITESPACE:
+ invert_bits = TRUE;
+ /* Fall through */
+ case OP_WHITESPACE:
+ set2 = (uint8_t *)(cb->cbits + cbit_space);
+ break;
+
+ case OP_NOT_WORDCHAR:
+ invert_bits = TRUE;
+ /* Fall through */
+ case OP_WORDCHAR:
+ set2 = (uint8_t *)(cb->cbits + cbit_word);
+ break;
+
+ default:
+ return FALSE;
+ }
+
+ /* Because the bit sets are unaligned bytes, we need to perform byte
+ comparison here. */
+
+ set_end = set1 + 32;
+ if (invert_bits)
+ {
+ do
+ {
+ if ((*set1++ & ~(*set2++)) != 0) return FALSE;
+ }
+ while (set1 < set_end);
+ }
+ else
+ {
+ do
+ {
+ if ((*set1++ & *set2++) != 0) return FALSE;
+ }
+ while (set1 < set_end);
+ }
+
+ if (list[1] == 0) return TRUE;
+ /* Might be an empty repeat. */
+ continue;
+ }
+
+ /* Some property combinations also acceptable. Unicode property opcodes are
+ processed specially; the rest can be handled with a lookup table. */
+
+ else
+ {
+ uint32_t leftop, rightop;
+
+ leftop = base_list[0];
+ rightop = list[0];
+
+#ifdef SUPPORT_UNICODE
+ accepted = FALSE; /* Always set in non-unicode case. */
+ if (leftop == OP_PROP || leftop == OP_NOTPROP)
+ {
+ if (rightop == OP_EOD)
+ accepted = TRUE;
+ else if (rightop == OP_PROP || rightop == OP_NOTPROP)
+ {
+ int n;
+ const uint8_t *p;
+ BOOL same = leftop == rightop;
+ BOOL lisprop = leftop == OP_PROP;
+ BOOL risprop = rightop == OP_PROP;
+ BOOL bothprop = lisprop && risprop;
+
+ /* There's a table that specifies how each combination is to be
+ processed:
+ 0 Always return FALSE (never auto-possessify)
+ 1 Character groups are distinct (possessify if both are OP_PROP)
+ 2 Check character categories in the same group (general or particular)
+ 3 Return TRUE if the two opcodes are not the same
+ ... see comments below
+ */
+
+ n = propposstab[base_list[2]][list[2]];
+ switch(n)
+ {
+ case 0: break;
+ case 1: accepted = bothprop; break;
+ case 2: accepted = (base_list[3] == list[3]) != same; break;
+ case 3: accepted = !same; break;
+
+ case 4: /* Left general category, right particular category */
+ accepted = risprop && catposstab[base_list[3]][list[3]] == same;
+ break;
+
+ case 5: /* Right general category, left particular category */
+ accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
+ break;
+
+ /* This code is logically tricky. Think hard before fiddling with it.
+ The posspropstab table has four entries per row. Each row relates to
+ one of PCRE's special properties such as ALNUM or SPACE or WORD.
+ Only WORD actually needs all four entries, but using repeats for the
+ others means they can all use the same code below.
+
+ The first two entries in each row are Unicode general categories, and
+ apply always, because all the characters they include are part of the
+ PCRE character set. The third and fourth entries are a general and a
+ particular category, respectively, that include one or more relevant
+ characters. One or the other is used, depending on whether the check
+ is for a general or a particular category. However, in both cases the
+ category contains more characters than the specials that are defined
+ for the property being tested against. Therefore, it cannot be used
+ in a NOTPROP case.
+
+ Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
+ Underscore is covered by ucp_P or ucp_Po. */
+
+ case 6: /* Left alphanum vs right general category */
+ case 7: /* Left space vs right general category */
+ case 8: /* Left word vs right general category */
+ p = posspropstab[n-6];
+ accepted = risprop && lisprop ==
+ (list[3] != p[0] &&
+ list[3] != p[1] &&
+ (list[3] != p[2] || !lisprop));
+ break;
+
+ case 9: /* Right alphanum vs left general category */
+ case 10: /* Right space vs left general category */
+ case 11: /* Right word vs left general category */
+ p = posspropstab[n-9];
+ accepted = lisprop && risprop ==
+ (base_list[3] != p[0] &&
+ base_list[3] != p[1] &&
+ (base_list[3] != p[2] || !risprop));
+ break;
+
+ case 12: /* Left alphanum vs right particular category */
+ case 13: /* Left space vs right particular category */
+ case 14: /* Left word vs right particular category */
+ p = posspropstab[n-12];
+ accepted = risprop && lisprop ==
+ (catposstab[p[0]][list[3]] &&
+ catposstab[p[1]][list[3]] &&
+ (list[3] != p[3] || !lisprop));
+ break;
+
+ case 15: /* Right alphanum vs left particular category */
+ case 16: /* Right space vs left particular category */
+ case 17: /* Right word vs left particular category */
+ p = posspropstab[n-15];
+ accepted = lisprop && risprop ==
+ (catposstab[p[0]][base_list[3]] &&
+ catposstab[p[1]][base_list[3]] &&
+ (base_list[3] != p[3] || !risprop));
+ break;
+ }
+ }
+ }
+
+ else
+#endif /* SUPPORT_UNICODE */
+
+ accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
+ rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
+ autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
+
+ if (!accepted) return FALSE;
+
+ if (list[1] == 0) return TRUE;
+ /* Might be an empty repeat. */
+ continue;
+ }
+
+ /* Control reaches here only if one of the items is a small character list.
+ All characters are checked against the other side. */
+
+ do
+ {
+ chr = *chr_ptr;
+
+ switch(list_ptr[0])
+ {
+ case OP_CHAR:
+ ochr_ptr = list_ptr + 2;
+ do
+ {
+ if (chr == *ochr_ptr) return FALSE;
+ ochr_ptr++;
+ }
+ while(*ochr_ptr != NOTACHAR);
+ break;
+
+ case OP_NOT:
+ ochr_ptr = list_ptr + 2;
+ do
+ {
+ if (chr == *ochr_ptr)
+ break;
+ ochr_ptr++;
+ }
+ while(*ochr_ptr != NOTACHAR);
+ if (*ochr_ptr == NOTACHAR) return FALSE; /* Not found */
+ break;
+
+ /* Note that OP_DIGIT etc. are generated only when PCRE2_UCP is *not*
+ set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
+
+ case OP_DIGIT:
+ if (chr < 256 && (cb->ctypes[chr] & ctype_digit) != 0) return FALSE;
+ break;
+
+ case OP_NOT_DIGIT:
+ if (chr > 255 || (cb->ctypes[chr] & ctype_digit) == 0) return FALSE;
+ break;
+
+ case OP_WHITESPACE:
+ if (chr < 256 && (cb->ctypes[chr] & ctype_space) != 0) return FALSE;
+ break;
+
+ case OP_NOT_WHITESPACE:
+ if (chr > 255 || (cb->ctypes[chr] & ctype_space) == 0) return FALSE;
+ break;
+
+ case OP_WORDCHAR:
+ if (chr < 255 && (cb->ctypes[chr] & ctype_word) != 0) return FALSE;
+ break;
+
+ case OP_NOT_WORDCHAR:
+ if (chr > 255 || (cb->ctypes[chr] & ctype_word) == 0) return FALSE;
+ break;
+
+ case OP_HSPACE:
+ switch(chr)
+ {
+ HSPACE_CASES: return FALSE;
+ default: break;
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ switch(chr)
+ {
+ HSPACE_CASES: break;
+ default: return FALSE;
+ }
+ break;
+
+ case OP_ANYNL:
+ case OP_VSPACE:
+ switch(chr)
+ {
+ VSPACE_CASES: return FALSE;
+ default: break;
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ switch(chr)
+ {
+ VSPACE_CASES: break;
+ default: return FALSE;
+ }
+ break;
+
+ case OP_DOLL:
+ case OP_EODN:
+ switch (chr)
+ {
+ case CHAR_CR:
+ case CHAR_LF:
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ return FALSE;
+ }
+ break;
+
+ case OP_EOD: /* Can always possessify before \z */
+ break;
+
+#ifdef SUPPORT_UNICODE
+ case OP_PROP:
+ case OP_NOTPROP:
+ if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
+ list_ptr[0] == OP_NOTPROP))
+ return FALSE;
+ break;
+#endif
+
+ case OP_NCLASS:
+ if (chr > 255) return FALSE;
+ /* Fall through */
+
+ case OP_CLASS:
+ if (chr > 255) break;
+ class_bitset = (uint8_t *)
+ ((list_ptr == list ? code : base_end) - list_ptr[2]);
+ if ((class_bitset[chr >> 3] & (1 << (chr & 7))) != 0) return FALSE;
+ break;
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
+ list_ptr[2] + LINK_SIZE, utf)) return FALSE;
+ break;
+#endif
+
+ default:
+ return FALSE;
+ }
+
+ chr_ptr++;
+ }
+ while(*chr_ptr != NOTACHAR);
+
+ /* At least one character must be matched from this opcode. */
+
+ if (list[1] == 0) return TRUE;
+ }
+
+/* Control never reaches here. There used to be a fail-save return FALSE; here,
+but some compilers complain about an unreachable statement. */
+}
+
+
+
+/*************************************************
+* Scan compiled regex for auto-possession *
+*************************************************/
+
+/* Replaces single character iterations with their possessive alternatives
+if appropriate. This function modifies the compiled opcode! Hitting a
+non-existent opcode may indicate a bug in PCRE2, but it can also be caused if a
+bad UTF string was compiled with PCRE2_NO_UTF_CHECK. The rec_limit catches
+overly complicated or large patterns. In these cases, the check just stops,
+leaving the remainder of the pattern unpossessified.
+
+Arguments:
+ code points to start of the byte code
+ utf TRUE in UTF mode
+ cb compile data block
+
+Returns: 0 for success
+ -1 if a non-existant opcode is encountered
+*/
+
+int
+PRIV(auto_possessify)(PCRE2_UCHAR *code, BOOL utf, const compile_block *cb)
+{
+PCRE2_UCHAR c;
+PCRE2_SPTR end;
+PCRE2_UCHAR *repeat_opcode;
+uint32_t list[8];
+int rec_limit = 1000; /* Was 10,000 but clang+ASAN uses a lot of stack. */
+
+for (;;)
+ {
+ c = *code;
+
+ if (c >= OP_TABLE_LENGTH) return -1; /* Something gone wrong */
+
+ if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
+ {
+ c -= get_repeat_base(c) - OP_STAR;
+ end = (c <= OP_MINUPTO) ?
+ get_chr_property_list(code, utf, cb->fcc, list) : NULL;
+ list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
+
+ if (end != NULL && compare_opcodes(end, utf, cb, list, end, &rec_limit))
+ {
+ switch(c)
+ {
+ case OP_STAR:
+ *code += OP_POSSTAR - OP_STAR;
+ break;
+
+ case OP_MINSTAR:
+ *code += OP_POSSTAR - OP_MINSTAR;
+ break;
+
+ case OP_PLUS:
+ *code += OP_POSPLUS - OP_PLUS;
+ break;
+
+ case OP_MINPLUS:
+ *code += OP_POSPLUS - OP_MINPLUS;
+ break;
+
+ case OP_QUERY:
+ *code += OP_POSQUERY - OP_QUERY;
+ break;
+
+ case OP_MINQUERY:
+ *code += OP_POSQUERY - OP_MINQUERY;
+ break;
+
+ case OP_UPTO:
+ *code += OP_POSUPTO - OP_UPTO;
+ break;
+
+ case OP_MINUPTO:
+ *code += OP_POSUPTO - OP_MINUPTO;
+ break;
+ }
+ }
+ c = *code;
+ }
+ else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
+ {
+#ifdef SUPPORT_WIDE_CHARS
+ if (c == OP_XCLASS)
+ repeat_opcode = code + GET(code, 1);
+ else
+#endif
+ repeat_opcode = code + 1 + (32 / sizeof(PCRE2_UCHAR));
+
+ c = *repeat_opcode;
+ if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
+ {
+ /* end must not be NULL. */
+ end = get_chr_property_list(code, utf, cb->fcc, list);
+
+ list[1] = (c & 1) == 0;
+
+ if (compare_opcodes(end, utf, cb, list, end, &rec_limit))
+ {
+ switch (c)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ *repeat_opcode = OP_CRPOSSTAR;
+ break;
+
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ *repeat_opcode = OP_CRPOSPLUS;
+ break;
+
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ *repeat_opcode = OP_CRPOSQUERY;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ *repeat_opcode = OP_CRPOSRANGE;
+ break;
+ }
+ }
+ }
+ c = *code;
+ }
+
+ switch(c)
+ {
+ case OP_END:
+ return 0;
+
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
+ break;
+
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSUPTO:
+ if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
+ code += 2;
+ break;
+
+ case OP_CALLOUT_STR:
+ code += GET(code, 1 + 2*LINK_SIZE);
+ break;
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ code += GET(code, 1);
+ break;
+#endif
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ code += code[1];
+ break;
+ }
+
+ /* Add in the fixed length from the table */
+
+ code += PRIV(OP_lengths)[c];
+
+ /* In UTF-8 and UTF-16 modes, opcodes that are followed by a character may be
+ followed by a multi-byte character. The length in the table is a minimum, so
+ we have to arrange to skip the extra code units. */
+
+#ifdef MAYBE_UTF_MULTI
+ if (utf) switch(c)
+ {
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+ if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
+ break;
+ }
+#else
+ (void)(utf); /* Keep compiler happy by referencing function argument */
+#endif /* SUPPORT_WIDE_CHARS */
+ }
+}
+
+/* End of pcre2_auto_possess.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_chartables.c b/test/monniaux/pcre2-10.32/pcre2_chartables.c
new file mode 100644
index 00000000..4046500c
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_chartables.c
@@ -0,0 +1,198 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* This file was automatically written by the dftables auxiliary
+program. It contains character tables that are used when no external
+tables are passed to PCRE2 by the application that calls it. The tables
+are used only for characters whose code values are less than 256. */
+
+/*The dftables program (which is distributed with PCRE2) can be used to
+build alternative versions of this file. This is necessary if you are
+running in an EBCDIC environment, or if you want to default to a different
+encoding, for example ISO-8859-1. When dftables is run, it creates these
+tables in the current locale. This happens automatically if PCRE2 is
+configured with --enable-rebuild-chartables. */
+
+/* The following #include is present because without it gcc 4.x may remove
+the array definition from the final binary if PCRE2 is built into a static
+library and dead code stripping is activated. This leads to link errors.
+Pulling in the header ensures that the array gets flagged as "someone
+outside this compilation unit might reference this" and so it will always
+be supplied to the linker. */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+const uint8_t PRIV(default_tables)[] = {
+
+/* This table is a lower casing table. */
+
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+ 64, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122, 91, 92, 93, 94, 95,
+ 96, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122,123,124,125,126,127,
+ 128,129,130,131,132,133,134,135,
+ 136,137,138,139,140,141,142,143,
+ 144,145,146,147,148,149,150,151,
+ 152,153,154,155,156,157,158,159,
+ 160,161,162,163,164,165,166,167,
+ 168,169,170,171,172,173,174,175,
+ 176,177,178,179,180,181,182,183,
+ 184,185,186,187,188,189,190,191,
+ 192,193,194,195,196,197,198,199,
+ 200,201,202,203,204,205,206,207,
+ 208,209,210,211,212,213,214,215,
+ 216,217,218,219,220,221,222,223,
+ 224,225,226,227,228,229,230,231,
+ 232,233,234,235,236,237,238,239,
+ 240,241,242,243,244,245,246,247,
+ 248,249,250,251,252,253,254,255,
+
+/* This table is a case flipping table. */
+
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+ 64, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122, 91, 92, 93, 94, 95,
+ 96, 65, 66, 67, 68, 69, 70, 71,
+ 72, 73, 74, 75, 76, 77, 78, 79,
+ 80, 81, 82, 83, 84, 85, 86, 87,
+ 88, 89, 90,123,124,125,126,127,
+ 128,129,130,131,132,133,134,135,
+ 136,137,138,139,140,141,142,143,
+ 144,145,146,147,148,149,150,151,
+ 152,153,154,155,156,157,158,159,
+ 160,161,162,163,164,165,166,167,
+ 168,169,170,171,172,173,174,175,
+ 176,177,178,179,180,181,182,183,
+ 184,185,186,187,188,189,190,191,
+ 192,193,194,195,196,197,198,199,
+ 200,201,202,203,204,205,206,207,
+ 208,209,210,211,212,213,214,215,
+ 216,217,218,219,220,221,222,223,
+ 224,225,226,227,228,229,230,231,
+ 232,233,234,235,236,237,238,239,
+ 240,241,242,243,244,245,246,247,
+ 248,249,250,251,252,253,254,255,
+
+/* This table contains bit maps for various character classes. Each map is 32
+bytes long and the bits run from the least significant end of each byte. The
+classes that have their own maps are: space, xdigit, digit, upper, lower, word,
+graph print, punct, and cntrl. Other classes are built from combinations. */
+
+ 0x00,0x3e,0x00,0x00,0x01,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0x7e,0x00,0x00,0x00,0x7e,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0xfe,0xff,0xff,0x07,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x07,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0xfe,0xff,0xff,0x87,0xfe,0xff,0xff,0x07,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0xff,
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0x00,0xfc,
+ 0x01,0x00,0x00,0xf8,0x01,0x00,0x00,0x78,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+/* This table identifies various classes of character by individual bits:
+ 0x01 white space character
+ 0x02 letter
+ 0x04 decimal digit
+ 0x08 hexadecimal digit
+ 0x10 alphanumeric or '_'
+*/
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
+ 0x00,0x01,0x01,0x01,0x01,0x01,0x00,0x00, /* 8- 15 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
+ 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
+ 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
+ 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
+ 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* @ - G */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* H - O */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* P - W */
+ 0x12,0x12,0x12,0x00,0x00,0x00,0x00,0x10, /* X - _ */
+ 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* ` - g */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* h - o */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* p - w */
+ 0x12,0x12,0x12,0x00,0x00,0x00,0x00,0x00, /* x -127 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
+
+/* End of pcre2_chartables.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_compile.c b/test/monniaux/pcre2-10.32/pcre2_compile.c
new file mode 100644
index 00000000..6bb1de36
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_compile.c
@@ -0,0 +1,9921 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define NLBLOCK cb /* Block containing newline information */
+#define PSSTART start_pattern /* Field containing processed string start */
+#define PSEND end_pattern /* Field containing processed string end */
+
+#include "pcre2_internal.h"
+
+/* In rare error cases debugging might require calling pcre2_printint(). */
+
+#if 0
+#ifdef EBCDIC
+#define PRINTABLE(c) ((c) >= 64 && (c) < 255)
+#else
+#define PRINTABLE(c) ((c) >= 32 && (c) < 127)
+#endif
+#include "pcre2_printint.c"
+#define DEBUG_CALL_PRINTINT
+#endif
+
+/* Other debugging code can be enabled by these defines. */
+
+/* #define DEBUG_SHOW_CAPTURES */
+/* #define DEBUG_SHOW_PARSED */
+
+/* There are a few things that vary with different code unit sizes. Handle them
+by defining macros in order to minimize #if usage. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define STRING_UTFn_RIGHTPAR STRING_UTF8_RIGHTPAR, 5
+#define XDIGIT(c) xdigitab[c]
+
+#else /* Either 16-bit or 32-bit */
+#define XDIGIT(c) (MAX_255(c)? xdigitab[c] : 0xff)
+
+#if PCRE2_CODE_UNIT_WIDTH == 16
+#define STRING_UTFn_RIGHTPAR STRING_UTF16_RIGHTPAR, 6
+
+#else /* 32-bit */
+#define STRING_UTFn_RIGHTPAR STRING_UTF32_RIGHTPAR, 6
+#endif
+#endif
+
+/* Macros to store and retrieve a PCRE2_SIZE value in the parsed pattern, which
+consists of uint32_t elements. Assume that if uint32_t can't hold it, two of
+them will be able to (i.e. assume a 64-bit world). */
+
+#if PCRE2_SIZE_MAX <= UINT32_MAX
+#define PUTOFFSET(s,p) *p++ = s
+#define GETOFFSET(s,p) s = *p++
+#define GETPLUSOFFSET(s,p) s = *(++p)
+#define READPLUSOFFSET(s,p) s = p[1]
+#define SKIPOFFSET(p) p++
+#define SIZEOFFSET 1
+#else
+#define PUTOFFSET(s,p) \
+ { *p++ = (uint32_t)(s >> 32); *p++ = (uint32_t)(s & 0xffffffff); }
+#define GETOFFSET(s,p) \
+ { s = ((PCRE2_SIZE)p[0] << 32) | (PCRE2_SIZE)p[1]; p += 2; }
+#define GETPLUSOFFSET(s,p) \
+ { s = ((PCRE2_SIZE)p[1] << 32) | (PCRE2_SIZE)p[2]; p += 2; }
+#define READPLUSOFFSET(s,p) \
+ { s = ((PCRE2_SIZE)p[1] << 32) | (PCRE2_SIZE)p[2]; }
+#define SKIPOFFSET(p) p += 2
+#define SIZEOFFSET 2
+#endif
+
+/* Macros for manipulating elements of the parsed pattern vector. */
+
+#define META_CODE(x) (x & 0xffff0000u)
+#define META_DATA(x) (x & 0x0000ffffu)
+#define META_DIFF(x,y) ((x-y)>>16)
+
+/* Function definitions to allow mutual recursion */
+
+#ifdef SUPPORT_UNICODE
+static unsigned int
+ add_list_to_class_internal(uint8_t *, PCRE2_UCHAR **, uint32_t,
+ compile_block *, const uint32_t *, unsigned int);
+#endif
+
+static int
+ compile_regex(uint32_t, PCRE2_UCHAR **, uint32_t **, int *, uint32_t,
+ uint32_t *, int32_t *, uint32_t *, int32_t *, branch_chain *,
+ compile_block *, PCRE2_SIZE *);
+
+static int
+ get_branchlength(uint32_t **, int *, int *, parsed_recurse_check *,
+ compile_block *);
+
+static BOOL
+ set_lookbehind_lengths(uint32_t **, int *, int *, parsed_recurse_check *,
+ compile_block *);
+
+
+
+/*************************************************
+* Code parameters and static tables *
+*************************************************/
+
+#define MAX_GROUP_NUMBER 65535u
+#define MAX_REPEAT_COUNT 65535u
+#define REPEAT_UNLIMITED (MAX_REPEAT_COUNT+1)
+
+/* COMPILE_WORK_SIZE specifies the size of stack workspace, which is used in
+different ways in the different pattern scans. The parsing and group-
+identifying pre-scan uses it to handle nesting, and needs it to be 16-bit
+aligned for this. Having defined the size in code units, we set up
+C16_WORK_SIZE as the number of elements in the 16-bit vector.
+
+During the first compiling phase, when determining how much memory is required,
+the regex is partly compiled into this space, but the compiled parts are
+discarded as soon as they can be, so that hopefully there will never be an
+overrun. The code does, however, check for an overrun, which can occur for
+pathological patterns. The size of the workspace depends on LINK_SIZE because
+the length of compiled items varies with this.
+
+In the real compile phase, this workspace is not currently used. */
+
+#define COMPILE_WORK_SIZE (3000*LINK_SIZE) /* Size in code units */
+
+#define C16_WORK_SIZE \
+ ((COMPILE_WORK_SIZE * sizeof(PCRE2_UCHAR))/sizeof(uint16_t))
+
+/* A uint32_t vector is used for caching information about the size of
+capturing groups, to improve performance. A default is created on the stack of
+this size. */
+
+#define GROUPINFO_DEFAULT_SIZE 256
+
+/* The overrun tests check for a slightly smaller size so that they detect the
+overrun before it actually does run off the end of the data block. */
+
+#define WORK_SIZE_SAFETY_MARGIN (100)
+
+/* This value determines the size of the initial vector that is used for
+remembering named groups during the pre-compile. It is allocated on the stack,
+but if it is too small, it is expanded, in a similar way to the workspace. The
+value is the number of slots in the list. */
+
+#define NAMED_GROUP_LIST_SIZE 20
+
+/* The pre-compiling pass over the pattern creates a parsed pattern in a vector
+of uint32_t. For short patterns this lives on the stack, with this size. Heap
+memory is used for longer patterns. */
+
+#define PARSED_PATTERN_DEFAULT_SIZE 1024
+
+/* Maximum length value to check against when making sure that the variable
+that holds the compiled pattern length does not overflow. We make it a bit less
+than INT_MAX to allow for adding in group terminating code units, so that we
+don't have to check them every time. */
+
+#define OFLOW_MAX (INT_MAX - 20)
+
+/* Code values for parsed patterns, which are stored in a vector of 32-bit
+unsigned ints. Values less than META_END are literal data values. The coding
+for identifying the item is in the top 16-bits, leaving 16 bits for the
+additional data that some of them need. The META_CODE, META_DATA, and META_DIFF
+macros are used to manipulate parsed pattern elements.
+
+NOTE: When these definitions are changed, the table of extra lengths for each
+code (meta_extra_lengths, just below) must be updated to remain in step. */
+
+#define META_END 0x80000000u /* End of pattern */
+
+#define META_ALT 0x80010000u /* alternation */
+#define META_ATOMIC 0x80020000u /* atomic group */
+#define META_BACKREF 0x80030000u /* Back ref */
+#define META_BACKREF_BYNAME 0x80040000u /* \k'name' */
+#define META_BIGVALUE 0x80050000u /* Next is a literal > META_END */
+#define META_CALLOUT_NUMBER 0x80060000u /* (?C with numerical argument */
+#define META_CALLOUT_STRING 0x80070000u /* (?C with string argument */
+#define META_CAPTURE 0x80080000u /* Capturing parenthesis */
+#define META_CIRCUMFLEX 0x80090000u /* ^ metacharacter */
+#define META_CLASS 0x800a0000u /* start non-empty class */
+#define META_CLASS_EMPTY 0x800b0000u /* empty class */
+#define META_CLASS_EMPTY_NOT 0x800c0000u /* negative empty class */
+#define META_CLASS_END 0x800d0000u /* end of non-empty class */
+#define META_CLASS_NOT 0x800e0000u /* start non-empty negative class */
+#define META_COND_ASSERT 0x800f0000u /* (?(?assertion)... */
+#define META_COND_DEFINE 0x80100000u /* (?(DEFINE)... */
+#define META_COND_NAME 0x80110000u /* (?(<name>)... */
+#define META_COND_NUMBER 0x80120000u /* (?(digits)... */
+#define META_COND_RNAME 0x80130000u /* (?(R&name)... */
+#define META_COND_RNUMBER 0x80140000u /* (?(Rdigits)... */
+#define META_COND_VERSION 0x80150000u /* (?(VERSION<op>x.y)... */
+#define META_DOLLAR 0x80160000u /* $ metacharacter */
+#define META_DOT 0x80170000u /* . metacharacter */
+#define META_ESCAPE 0x80180000u /* \d and friends */
+#define META_KET 0x80190000u /* closing parenthesis */
+#define META_NOCAPTURE 0x801a0000u /* no capture parens */
+#define META_OPTIONS 0x801b0000u /* (?i) and friends */
+#define META_POSIX 0x801c0000u /* POSIX class item */
+#define META_POSIX_NEG 0x801d0000u /* negative POSIX class item */
+#define META_RANGE_ESCAPED 0x801e0000u /* range with at least one escape */
+#define META_RANGE_LITERAL 0x801f0000u /* range defined literally */
+#define META_RECURSE 0x80200000u /* Recursion */
+#define META_RECURSE_BYNAME 0x80210000u /* (?&name) */
+
+/* These must be kept together to make it easy to check that an assertion
+is present where expected in a conditional group. */
+
+#define META_LOOKAHEAD 0x80220000u /* (?= */
+#define META_LOOKAHEADNOT 0x80230000u /* (?! */
+#define META_LOOKBEHIND 0x80240000u /* (?<= */
+#define META_LOOKBEHINDNOT 0x80250000u /* (?<! */
+
+/* These must be kept in this order, with consecutive values, and the _ARG
+versions of COMMIT, PRUNE, SKIP, and THEN immediately after their non-argument
+versions. */
+
+#define META_MARK 0x80260000u /* (*MARK) */
+#define META_ACCEPT 0x80270000u /* (*ACCEPT) */
+#define META_FAIL 0x80280000u /* (*FAIL) */
+#define META_COMMIT 0x80290000u /* These */
+#define META_COMMIT_ARG 0x802a0000u /* pairs */
+#define META_PRUNE 0x802b0000u /* must */
+#define META_PRUNE_ARG 0x802c0000u /* be */
+#define META_SKIP 0x802d0000u /* kept */
+#define META_SKIP_ARG 0x802e0000u /* in */
+#define META_THEN 0x802f0000u /* this */
+#define META_THEN_ARG 0x80300000u /* order */
+
+/* These must be kept in groups of adjacent 3 values, and all together. */
+
+#define META_ASTERISK 0x80310000u /* * */
+#define META_ASTERISK_PLUS 0x80320000u /* *+ */
+#define META_ASTERISK_QUERY 0x80330000u /* *? */
+#define META_PLUS 0x80340000u /* + */
+#define META_PLUS_PLUS 0x80350000u /* ++ */
+#define META_PLUS_QUERY 0x80360000u /* +? */
+#define META_QUERY 0x80370000u /* ? */
+#define META_QUERY_PLUS 0x80380000u /* ?+ */
+#define META_QUERY_QUERY 0x80390000u /* ?? */
+#define META_MINMAX 0x803a0000u /* {n,m} repeat */
+#define META_MINMAX_PLUS 0x803b0000u /* {n,m}+ repeat */
+#define META_MINMAX_QUERY 0x803c0000u /* {n,m}? repeat */
+
+#define META_FIRST_QUANTIFIER META_ASTERISK
+#define META_LAST_QUANTIFIER META_MINMAX_QUERY
+
+/* Table of extra lengths for each of the meta codes. Must be kept in step with
+the definitions above. For some items these values are a basic length to which
+a variable amount has to be added. */
+
+static unsigned char meta_extra_lengths[] = {
+ 0, /* META_END */
+ 0, /* META_ALT */
+ 0, /* META_ATOMIC */
+ 0, /* META_BACKREF - more if group is >= 10 */
+ 1+SIZEOFFSET, /* META_BACKREF_BYNAME */
+ 1, /* META_BIGVALUE */
+ 3, /* META_CALLOUT_NUMBER */
+ 3+SIZEOFFSET, /* META_CALLOUT_STRING */
+ 0, /* META_CAPTURE */
+ 0, /* META_CIRCUMFLEX */
+ 0, /* META_CLASS */
+ 0, /* META_CLASS_EMPTY */
+ 0, /* META_CLASS_EMPTY_NOT */
+ 0, /* META_CLASS_END */
+ 0, /* META_CLASS_NOT */
+ 0, /* META_COND_ASSERT */
+ SIZEOFFSET, /* META_COND_DEFINE */
+ 1+SIZEOFFSET, /* META_COND_NAME */
+ 1+SIZEOFFSET, /* META_COND_NUMBER */
+ 1+SIZEOFFSET, /* META_COND_RNAME */
+ 1+SIZEOFFSET, /* META_COND_RNUMBER */
+ 3, /* META_COND_VERSION */
+ 0, /* META_DOLLAR */
+ 0, /* META_DOT */
+ 0, /* META_ESCAPE - more for ESC_P, ESC_p, ESC_g, ESC_k */
+ 0, /* META_KET */
+ 0, /* META_NOCAPTURE */
+ 1, /* META_OPTIONS */
+ 1, /* META_POSIX */
+ 1, /* META_POSIX_NEG */
+ 0, /* META_RANGE_ESCAPED */
+ 0, /* META_RANGE_LITERAL */
+ SIZEOFFSET, /* META_RECURSE */
+ 1+SIZEOFFSET, /* META_RECURSE_BYNAME */
+ 0, /* META_LOOKAHEAD */
+ 0, /* META_LOOKAHEADNOT */
+ SIZEOFFSET, /* META_LOOKBEHIND */
+ SIZEOFFSET, /* META_LOOKBEHINDNOT */
+ 1, /* META_MARK - plus the string length */
+ 0, /* META_ACCEPT */
+ 0, /* META_FAIL */
+ 0, /* META_COMMIT */
+ 1, /* META_COMMIT_ARG - plus the string length */
+ 0, /* META_PRUNE */
+ 1, /* META_PRUNE_ARG - plus the string length */
+ 0, /* META_SKIP */
+ 1, /* META_SKIP_ARG - plus the string length */
+ 0, /* META_THEN */
+ 1, /* META_THEN_ARG - plus the string length */
+ 0, /* META_ASTERISK */
+ 0, /* META_ASTERISK_PLUS */
+ 0, /* META_ASTERISK_QUERY */
+ 0, /* META_PLUS */
+ 0, /* META_PLUS_PLUS */
+ 0, /* META_PLUS_QUERY */
+ 0, /* META_QUERY */
+ 0, /* META_QUERY_PLUS */
+ 0, /* META_QUERY_QUERY */
+ 2, /* META_MINMAX */
+ 2, /* META_MINMAX_PLUS */
+ 2 /* META_MINMAX_QUERY */
+};
+
+/* Types for skipping parts of a parsed pattern. */
+
+enum { PSKIP_ALT, PSKIP_CLASS, PSKIP_KET };
+
+/* Macro for setting individual bits in class bitmaps. It took some
+experimenting to figure out how to stop gcc 5.3.0 from warning with
+-Wconversion. This version gets a warning:
+
+ #define SETBIT(a,b) a[(b)/8] |= (uint8_t)(1 << ((b)&7))
+
+Let's hope the apparently less efficient version isn't actually so bad if the
+compiler is clever with identical subexpressions. */
+
+#define SETBIT(a,b) a[(b)/8] = (uint8_t)(a[(b)/8] | (1 << ((b)&7)))
+
+/* Private flags added to firstcu and reqcu. */
+
+#define REQ_CASELESS (1 << 0) /* Indicates caselessness */
+#define REQ_VARY (1 << 1) /* reqcu followed non-literal item */
+/* Negative values for the firstcu and reqcu flags */
+#define REQ_UNSET (-2) /* Not yet found anything */
+#define REQ_NONE (-1) /* Found not fixed char */
+
+/* These flags are used in the groupinfo vector. */
+
+#define GI_SET_FIXED_LENGTH 0x80000000u
+#define GI_NOT_FIXED_LENGTH 0x40000000u
+#define GI_FIXED_LENGTH_MASK 0x0000ffffu
+
+/* This simple test for a decimal digit works for both ASCII/Unicode and EBCDIC
+and is fast (a good compiler can turn it into a subtraction and unsigned
+comparison). */
+
+#define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
+
+/* Table to identify hex digits. The tables in chartables are dependent on the
+locale, and may mark arbitrary characters as digits. We want to recognize only
+0-9, a-z, and A-Z as hex digits, which is why we have a private table here. It
+costs 256 bytes, but it is a lot faster than doing character value tests (at
+least in some simple cases I timed), and in some applications one wants PCRE2
+to compile efficiently as well as match efficiently. The value in the table is
+the binary hex digit value, or 0xff for non-hex digits. */
+
+/* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
+UTF-8 mode. */
+
+#ifndef EBCDIC
+static const uint8_t xdigitab[] =
+ {
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0- 7 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 8- 15 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 16- 23 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 24- 31 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* - ' */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* ( - / */
+ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, /* 0 - 7 */
+ 0x08,0x09,0xff,0xff,0xff,0xff,0xff,0xff, /* 8 - ? */
+ 0xff,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xff, /* @ - G */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* H - O */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* P - W */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* X - _ */
+ 0xff,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xff, /* ` - g */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* h - o */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* p - w */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* x -127 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 128-135 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 136-143 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 144-151 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 152-159 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 160-167 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 168-175 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 176-183 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 184-191 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 192-199 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 2ff-207 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 208-215 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 216-223 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 224-231 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 232-239 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 240-247 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};/* 248-255 */
+
+#else
+
+/* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
+
+static const uint8_t xdigitab[] =
+ {
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0- 7 0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 8- 15 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 16- 23 10 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 24- 31 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 32- 39 20 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 40- 47 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 48- 55 30 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 56- 63 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* - 71 40 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 72- | */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* & - 87 50 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 88- 95 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* - -103 60 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 104- ? */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 112-119 70 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 120- " */
+ 0xff,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xff, /* 128- g 80 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* h -143 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 144- p 90 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* q -159 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 160- x A0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* y -175 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* ^ -183 B0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 184-191 */
+ 0xff,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xff, /* { - G C0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* H -207 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* } - P D0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* Q -223 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* \ - X E0 */
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* Y -239 */
+ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, /* 0 - 7 F0 */
+ 0x08,0x09,0xff,0xff,0xff,0xff,0xff,0xff};/* 8 -255 */
+#endif /* EBCDIC */
+
+
+/* Table for handling alphanumeric escaped characters. Positive returns are
+simple data values; negative values are for special things like \d and so on.
+Zero means further processing is needed (for things like \x), or the escape is
+invalid. */
+
+/* This is the "normal" table for ASCII systems or for EBCDIC systems running
+in UTF-8 mode. It runs from '0' to 'z'. */
+
+#ifndef EBCDIC
+#define ESCAPES_FIRST CHAR_0
+#define ESCAPES_LAST CHAR_z
+#define UPPER_CASE(c) (c-32)
+
+static const short int escapes[] = {
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ CHAR_COLON, CHAR_SEMICOLON,
+ CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
+ CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
+ CHAR_COMMERCIAL_AT, -ESC_A,
+ -ESC_B, -ESC_C,
+ -ESC_D, -ESC_E,
+ 0, -ESC_G,
+ -ESC_H, 0,
+ 0, -ESC_K,
+ 0, 0,
+ -ESC_N, 0,
+ -ESC_P, -ESC_Q,
+ -ESC_R, -ESC_S,
+ 0, 0,
+ -ESC_V, -ESC_W,
+ -ESC_X, 0,
+ -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
+ CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
+ CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
+ CHAR_GRAVE_ACCENT, CHAR_BEL,
+ -ESC_b, 0,
+ -ESC_d, CHAR_ESC,
+ CHAR_FF, 0,
+ -ESC_h, 0,
+ 0, -ESC_k,
+ 0, 0,
+ CHAR_LF, 0,
+ -ESC_p, 0,
+ CHAR_CR, -ESC_s,
+ CHAR_HT, 0,
+ -ESC_v, -ESC_w,
+ 0, 0,
+ -ESC_z
+};
+
+#else
+
+/* This is the "abnormal" table for EBCDIC systems without UTF-8 support.
+It runs from 'a' to '9'. For some minimal testing of EBCDIC features, the code
+is sometimes compiled on an ASCII system. In this case, we must not use CHAR_a
+because it is defined as 'a', which of course picks up the ASCII value. */
+
+#if 'a' == 0x81 /* Check for a real EBCDIC environment */
+#define ESCAPES_FIRST CHAR_a
+#define ESCAPES_LAST CHAR_9
+#define UPPER_CASE(c) (c+64)
+#else /* Testing in an ASCII environment */
+#define ESCAPES_FIRST ((unsigned char)'\x81') /* EBCDIC 'a' */
+#define ESCAPES_LAST ((unsigned char)'\xf9') /* EBCDIC '9' */
+#define UPPER_CASE(c) (c-32)
+#endif
+
+static const short int escapes[] = {
+/* 80 */ CHAR_BEL, -ESC_b, 0, -ESC_d, CHAR_ESC, CHAR_FF, 0,
+/* 88 */ -ESC_h, 0, 0, '{', 0, 0, 0, 0,
+/* 90 */ 0, 0, -ESC_k, 0, 0, CHAR_LF, 0, -ESC_p,
+/* 98 */ 0, CHAR_CR, 0, '}', 0, 0, 0, 0,
+/* A0 */ 0, '~', -ESC_s, CHAR_HT, 0, -ESC_v, -ESC_w, 0,
+/* A8 */ 0, -ESC_z, 0, 0, 0, '[', 0, 0,
+/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
+/* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
+/* C0 */ '{', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E, 0, -ESC_G,
+/* C8 */ -ESC_H, 0, 0, 0, 0, 0, 0, 0,
+/* D0 */ '}', 0, -ESC_K, 0, 0, -ESC_N, 0, -ESC_P,
+/* D8 */ -ESC_Q, -ESC_R, 0, 0, 0, 0, 0, 0,
+/* E0 */ '\\', 0, -ESC_S, 0, 0, -ESC_V, -ESC_W, -ESC_X,
+/* E8 */ 0, -ESC_Z, 0, 0, 0, 0, 0, 0,
+/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
+/* F8 */ 0, 0
+};
+
+/* We also need a table of characters that may follow \c in an EBCDIC
+environment for characters 0-31. */
+
+static unsigned char ebcdic_escape_c[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
+
+#endif /* EBCDIC */
+
+
+/* Table of special "verbs" like (*PRUNE). This is a short table, so it is
+searched linearly. Put all the names into a single string, in order to reduce
+the number of relocations when a shared library is dynamically linked. The
+string is built from string macros so that it works in UTF-8 mode on EBCDIC
+platforms. */
+
+typedef struct verbitem {
+ unsigned int len; /* Length of verb name */
+ uint32_t meta; /* Base META_ code */
+ int has_arg; /* Argument requirement */
+} verbitem;
+
+static const char verbnames[] =
+ "\0" /* Empty name is a shorthand for MARK */
+ STRING_MARK0
+ STRING_ACCEPT0
+ STRING_F0
+ STRING_FAIL0
+ STRING_COMMIT0
+ STRING_PRUNE0
+ STRING_SKIP0
+ STRING_THEN;
+
+static const verbitem verbs[] = {
+ { 0, META_MARK, +1 }, /* > 0 => must have an argument */
+ { 4, META_MARK, +1 },
+ { 6, META_ACCEPT, -1 }, /* < 0 => Optional argument, convert to pre-MARK */
+ { 1, META_FAIL, -1 },
+ { 4, META_FAIL, -1 },
+ { 6, META_COMMIT, 0 },
+ { 5, META_PRUNE, 0 }, /* Optional argument; bump META code if found */
+ { 4, META_SKIP, 0 },
+ { 4, META_THEN, 0 }
+};
+
+static const int verbcount = sizeof(verbs)/sizeof(verbitem);
+
+/* Verb opcodes, indexed by their META code offset from META_MARK. */
+
+static const uint32_t verbops[] = {
+ OP_MARK, OP_ACCEPT, OP_FAIL, OP_COMMIT, OP_COMMIT_ARG, OP_PRUNE,
+ OP_PRUNE_ARG, OP_SKIP, OP_SKIP_ARG, OP_THEN, OP_THEN_ARG };
+
+/* Offsets from OP_STAR for case-independent and negative repeat opcodes. */
+
+static uint32_t chartypeoffset[] = {
+ OP_STAR - OP_STAR, OP_STARI - OP_STAR,
+ OP_NOTSTAR - OP_STAR, OP_NOTSTARI - OP_STAR };
+
+/* Tables of names of POSIX character classes and their lengths. The names are
+now all in a single string, to reduce the number of relocations when a shared
+library is dynamically loaded. The list of lengths is terminated by a zero
+length entry. The first three must be alpha, lower, upper, as this is assumed
+for handling case independence. The indices for graph, print, and punct are
+needed, so identify them. */
+
+static const char posix_names[] =
+ STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
+ STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
+ STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
+ STRING_word0 STRING_xdigit;
+
+static const uint8_t posix_name_lengths[] = {
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
+
+#define PC_GRAPH 8
+#define PC_PRINT 9
+#define PC_PUNCT 10
+
+/* Table of class bit maps for each POSIX class. Each class is formed from a
+base map, with an optional addition or removal of another map. Then, for some
+classes, there is some additional tweaking: for [:blank:] the vertical space
+characters are removed, and for [:alpha:] and [:alnum:] the underscore
+character is removed. The triples in the table consist of the base map offset,
+second map offset or -1 if no second map, and a non-negative value for map
+addition or a negative value for map subtraction (if there are two maps). The
+absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
+remove vertical space characters, 2 => remove underscore. */
+
+static const int posix_class_maps[] = {
+ cbit_word, cbit_digit, -2, /* alpha */
+ cbit_lower, -1, 0, /* lower */
+ cbit_upper, -1, 0, /* upper */
+ cbit_word, -1, 2, /* alnum - word without underscore */
+ cbit_print, cbit_cntrl, 0, /* ascii */
+ cbit_space, -1, 1, /* blank - a GNU extension */
+ cbit_cntrl, -1, 0, /* cntrl */
+ cbit_digit, -1, 0, /* digit */
+ cbit_graph, -1, 0, /* graph */
+ cbit_print, -1, 0, /* print */
+ cbit_punct, -1, 0, /* punct */
+ cbit_space, -1, 0, /* space */
+ cbit_word, -1, 0, /* word - a Perl extension */
+ cbit_xdigit,-1, 0 /* xdigit */
+};
+
+#ifdef SUPPORT_UNICODE
+
+/* The POSIX class Unicode property substitutes that are used in UCP mode must
+be in the order of the POSIX class names, defined above. */
+
+static int posix_substitutes[] = {
+ PT_GC, ucp_L, /* alpha */
+ PT_PC, ucp_Ll, /* lower */
+ PT_PC, ucp_Lu, /* upper */
+ PT_ALNUM, 0, /* alnum */
+ -1, 0, /* ascii, treat as non-UCP */
+ -1, 1, /* blank, treat as \h */
+ PT_PC, ucp_Cc, /* cntrl */
+ PT_PC, ucp_Nd, /* digit */
+ PT_PXGRAPH, 0, /* graph */
+ PT_PXPRINT, 0, /* print */
+ PT_PXPUNCT, 0, /* punct */
+ PT_PXSPACE, 0, /* space */ /* Xps is POSIX space, but from 8.34 */
+ PT_WORD, 0, /* word */ /* Perl and POSIX space are the same */
+ -1, 0 /* xdigit, treat as non-UCP */
+};
+#define POSIX_SUBSIZE (sizeof(posix_substitutes) / (2*sizeof(uint32_t)))
+#endif /* SUPPORT_UNICODE */
+
+/* Masks for checking option settings. When PCRE2_LITERAL is set, only a subset
+are allowed. */
+
+#define PUBLIC_LITERAL_COMPILE_OPTIONS \
+ (PCRE2_ANCHORED|PCRE2_AUTO_CALLOUT|PCRE2_CASELESS|PCRE2_ENDANCHORED| \
+ PCRE2_FIRSTLINE|PCRE2_LITERAL|PCRE2_NO_START_OPTIMIZE| \
+ PCRE2_NO_UTF_CHECK|PCRE2_USE_OFFSET_LIMIT|PCRE2_UTF)
+
+#define PUBLIC_COMPILE_OPTIONS \
+ (PUBLIC_LITERAL_COMPILE_OPTIONS| \
+ PCRE2_ALLOW_EMPTY_CLASS|PCRE2_ALT_BSUX|PCRE2_ALT_CIRCUMFLEX| \
+ PCRE2_ALT_VERBNAMES|PCRE2_DOLLAR_ENDONLY|PCRE2_DOTALL|PCRE2_DUPNAMES| \
+ PCRE2_EXTENDED|PCRE2_EXTENDED_MORE|PCRE2_MATCH_UNSET_BACKREF| \
+ PCRE2_MULTILINE|PCRE2_NEVER_BACKSLASH_C|PCRE2_NEVER_UCP| \
+ PCRE2_NEVER_UTF|PCRE2_NO_AUTO_CAPTURE|PCRE2_NO_AUTO_POSSESS| \
+ PCRE2_NO_DOTSTAR_ANCHOR|PCRE2_UCP|PCRE2_UNGREEDY)
+
+#define PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS \
+ (PCRE2_EXTRA_MATCH_LINE|PCRE2_EXTRA_MATCH_WORD)
+
+#define PUBLIC_COMPILE_EXTRA_OPTIONS \
+ (PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS| \
+ PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES|PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL)
+
+/* Compile time error code numbers. They are given names so that they can more
+easily be tracked. When a new number is added, the tables called eint1 and
+eint2 in pcre2posix.c may need to be updated, and a new error text must be
+added to compile_error_texts in pcre2_error.c. */
+
+enum { ERR0 = COMPILE_ERROR_BASE,
+ ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
+ ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
+ ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29, ERR30,
+ ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, ERR40,
+ ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49, ERR50,
+ ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57, ERR58, ERR59, ERR60,
+ ERR61, ERR62, ERR63, ERR64, ERR65, ERR66, ERR67, ERR68, ERR69, ERR70,
+ ERR71, ERR72, ERR73, ERR74, ERR75, ERR76, ERR77, ERR78, ERR79, ERR80,
+ ERR81, ERR82, ERR83, ERR84, ERR85, ERR86, ERR87, ERR88, ERR89, ERR90,
+ ERR91, ERR92, ERR93, ERR94 };
+
+/* This is a table of start-of-pattern options such as (*UTF) and settings such
+as (*LIMIT_MATCH=nnnn) and (*CRLF). For completeness and backward
+compatibility, (*UTFn) is supported in the relevant libraries, but (*UTF) is
+generic and always supported. */
+
+enum { PSO_OPT, /* Value is an option bit */
+ PSO_FLG, /* Value is a flag bit */
+ PSO_NL, /* Value is a newline type */
+ PSO_BSR, /* Value is a \R type */
+ PSO_LIMH, /* Read integer value for heap limit */
+ PSO_LIMM, /* Read integer value for match limit */
+ PSO_LIMD }; /* Read integer value for depth limit */
+
+typedef struct pso {
+ const uint8_t *name;
+ uint16_t length;
+ uint16_t type;
+ uint32_t value;
+} pso;
+
+/* NB: STRING_UTFn_RIGHTPAR contains the length as well */
+
+static pso pso_list[] = {
+ { (uint8_t *)STRING_UTFn_RIGHTPAR, PSO_OPT, PCRE2_UTF },
+ { (uint8_t *)STRING_UTF_RIGHTPAR, 4, PSO_OPT, PCRE2_UTF },
+ { (uint8_t *)STRING_UCP_RIGHTPAR, 4, PSO_OPT, PCRE2_UCP },
+ { (uint8_t *)STRING_NOTEMPTY_RIGHTPAR, 9, PSO_FLG, PCRE2_NOTEMPTY_SET },
+ { (uint8_t *)STRING_NOTEMPTY_ATSTART_RIGHTPAR, 17, PSO_FLG, PCRE2_NE_ATST_SET },
+ { (uint8_t *)STRING_NO_AUTO_POSSESS_RIGHTPAR, 16, PSO_OPT, PCRE2_NO_AUTO_POSSESS },
+ { (uint8_t *)STRING_NO_DOTSTAR_ANCHOR_RIGHTPAR, 18, PSO_OPT, PCRE2_NO_DOTSTAR_ANCHOR },
+ { (uint8_t *)STRING_NO_JIT_RIGHTPAR, 7, PSO_FLG, PCRE2_NOJIT },
+ { (uint8_t *)STRING_NO_START_OPT_RIGHTPAR, 13, PSO_OPT, PCRE2_NO_START_OPTIMIZE },
+ { (uint8_t *)STRING_LIMIT_HEAP_EQ, 11, PSO_LIMH, 0 },
+ { (uint8_t *)STRING_LIMIT_MATCH_EQ, 12, PSO_LIMM, 0 },
+ { (uint8_t *)STRING_LIMIT_DEPTH_EQ, 12, PSO_LIMD, 0 },
+ { (uint8_t *)STRING_LIMIT_RECURSION_EQ, 16, PSO_LIMD, 0 },
+ { (uint8_t *)STRING_CR_RIGHTPAR, 3, PSO_NL, PCRE2_NEWLINE_CR },
+ { (uint8_t *)STRING_LF_RIGHTPAR, 3, PSO_NL, PCRE2_NEWLINE_LF },
+ { (uint8_t *)STRING_CRLF_RIGHTPAR, 5, PSO_NL, PCRE2_NEWLINE_CRLF },
+ { (uint8_t *)STRING_ANY_RIGHTPAR, 4, PSO_NL, PCRE2_NEWLINE_ANY },
+ { (uint8_t *)STRING_NUL_RIGHTPAR, 4, PSO_NL, PCRE2_NEWLINE_NUL },
+ { (uint8_t *)STRING_ANYCRLF_RIGHTPAR, 8, PSO_NL, PCRE2_NEWLINE_ANYCRLF },
+ { (uint8_t *)STRING_BSR_ANYCRLF_RIGHTPAR, 12, PSO_BSR, PCRE2_BSR_ANYCRLF },
+ { (uint8_t *)STRING_BSR_UNICODE_RIGHTPAR, 12, PSO_BSR, PCRE2_BSR_UNICODE }
+};
+
+/* This table is used when converting repeating opcodes into possessified
+versions as a result of an explicit possessive quantifier such as ++. A zero
+value means there is no possessified version - in those cases the item in
+question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
+because all relevant opcodes are less than that. */
+
+static const uint8_t opcode_possessify[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */
+
+ 0, /* NOTI */
+ OP_POSSTAR, 0, /* STAR, MINSTAR */
+ OP_POSPLUS, 0, /* PLUS, MINPLUS */
+ OP_POSQUERY, 0, /* QUERY, MINQUERY */
+ OP_POSUPTO, 0, /* UPTO, MINUPTO */
+ 0, /* EXACT */
+ 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */
+
+ OP_POSSTARI, 0, /* STARI, MINSTARI */
+ OP_POSPLUSI, 0, /* PLUSI, MINPLUSI */
+ OP_POSQUERYI, 0, /* QUERYI, MINQUERYI */
+ OP_POSUPTOI, 0, /* UPTOI, MINUPTOI */
+ 0, /* EXACTI */
+ 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */
+
+ OP_NOTPOSSTAR, 0, /* NOTSTAR, NOTMINSTAR */
+ OP_NOTPOSPLUS, 0, /* NOTPLUS, NOTMINPLUS */
+ OP_NOTPOSQUERY, 0, /* NOTQUERY, NOTMINQUERY */
+ OP_NOTPOSUPTO, 0, /* NOTUPTO, NOTMINUPTO */
+ 0, /* NOTEXACT */
+ 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
+
+ OP_NOTPOSSTARI, 0, /* NOTSTARI, NOTMINSTARI */
+ OP_NOTPOSPLUSI, 0, /* NOTPLUSI, NOTMINPLUSI */
+ OP_NOTPOSQUERYI, 0, /* NOTQUERYI, NOTMINQUERYI */
+ OP_NOTPOSUPTOI, 0, /* NOTUPTOI, NOTMINUPTOI */
+ 0, /* NOTEXACTI */
+ 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
+
+ OP_TYPEPOSSTAR, 0, /* TYPESTAR, TYPEMINSTAR */
+ OP_TYPEPOSPLUS, 0, /* TYPEPLUS, TYPEMINPLUS */
+ OP_TYPEPOSQUERY, 0, /* TYPEQUERY, TYPEMINQUERY */
+ OP_TYPEPOSUPTO, 0, /* TYPEUPTO, TYPEMINUPTO */
+ 0, /* TYPEEXACT */
+ 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
+
+ OP_CRPOSSTAR, 0, /* CRSTAR, CRMINSTAR */
+ OP_CRPOSPLUS, 0, /* CRPLUS, CRMINPLUS */
+ OP_CRPOSQUERY, 0, /* CRQUERY, CRMINQUERY */
+ OP_CRPOSRANGE, 0, /* CRRANGE, CRMINRANGE */
+ 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */
+
+ 0, 0, 0, /* CLASS, NCLASS, XCLASS */
+ 0, 0, /* REF, REFI */
+ 0, 0, /* DNREF, DNREFI */
+ 0, 0 /* RECURSE, CALLOUT */
+};
+
+
+#ifdef DEBUG_SHOW_PARSED
+/*************************************************
+* Show the parsed pattern for debugging *
+*************************************************/
+
+/* For debugging the pre-scan, this code, which outputs the parsed data vector,
+can be enabled. */
+
+static void show_parsed(compile_block *cb)
+{
+uint32_t *pptr = cb->parsed_pattern;
+
+for (;;)
+ {
+ int max, min;
+ PCRE2_SIZE offset;
+ uint32_t i;
+ uint32_t length;
+ uint32_t meta_arg = META_DATA(*pptr);
+
+ fprintf(stderr, "+++ %02d %.8x ", (int)(pptr - cb->parsed_pattern), *pptr);
+
+ if (*pptr < META_END)
+ {
+ if (*pptr > 32 && *pptr < 128) fprintf(stderr, "%c", *pptr);
+ pptr++;
+ }
+
+ else switch (META_CODE(*pptr++))
+ {
+ default:
+ fprintf(stderr, "**** OOPS - unknown META value - giving up ****\n");
+ return;
+
+ case META_END:
+ fprintf(stderr, "META_END\n");
+ return;
+
+ case META_CAPTURE:
+ fprintf(stderr, "META_CAPTURE %d", meta_arg);
+ break;
+
+ case META_RECURSE:
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "META_RECURSE %d %zd", meta_arg, offset);
+ break;
+
+ case META_BACKREF:
+ if (meta_arg < 10)
+ offset = cb->small_ref_offset[meta_arg];
+ else
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "META_BACKREF %d %zd", meta_arg, offset);
+ break;
+
+ case META_ESCAPE:
+ if (meta_arg == ESC_P || meta_arg == ESC_p)
+ {
+ uint32_t ptype = *pptr >> 16;
+ uint32_t pvalue = *pptr++ & 0xffff;
+ fprintf(stderr, "META \\%c %d %d", (meta_arg == ESC_P)? 'P':'p',
+ ptype, pvalue);
+ }
+ else
+ {
+ uint32_t cc;
+ /* There's just one escape we might have here that isn't negated in the
+ escapes table. */
+ if (meta_arg == ESC_g) cc = CHAR_g;
+ else for (cc = ESCAPES_FIRST; cc <= ESCAPES_LAST; cc++)
+ {
+ if (meta_arg == (uint32_t)(-escapes[cc - ESCAPES_FIRST])) break;
+ }
+ if (cc > ESCAPES_LAST) cc = CHAR_QUESTION_MARK;
+ fprintf(stderr, "META \\%c", cc);
+ }
+ break;
+
+ case META_MINMAX:
+ min = *pptr++;
+ max = *pptr++;
+ if (max != REPEAT_UNLIMITED)
+ fprintf(stderr, "META {%d,%d}", min, max);
+ else
+ fprintf(stderr, "META {%d,}", min);
+ break;
+
+ case META_MINMAX_QUERY:
+ min = *pptr++;
+ max = *pptr++;
+ if (max != REPEAT_UNLIMITED)
+ fprintf(stderr, "META {%d,%d}?", min, max);
+ else
+ fprintf(stderr, "META {%d,}?", min);
+ break;
+
+ case META_MINMAX_PLUS:
+ min = *pptr++;
+ max = *pptr++;
+ if (max != REPEAT_UNLIMITED)
+ fprintf(stderr, "META {%d,%d}+", min, max);
+ else
+ fprintf(stderr, "META {%d,}+", min);
+ break;
+
+ case META_BIGVALUE: fprintf(stderr, "META_BIGVALUE %.8x", *pptr++); break;
+ case META_CIRCUMFLEX: fprintf(stderr, "META_CIRCUMFLEX"); break;
+ case META_COND_ASSERT: fprintf(stderr, "META_COND_ASSERT"); break;
+ case META_DOLLAR: fprintf(stderr, "META_DOLLAR"); break;
+ case META_DOT: fprintf(stderr, "META_DOT"); break;
+ case META_ASTERISK: fprintf(stderr, "META *"); break;
+ case META_ASTERISK_QUERY: fprintf(stderr, "META *?"); break;
+ case META_ASTERISK_PLUS: fprintf(stderr, "META *+"); break;
+ case META_PLUS: fprintf(stderr, "META +"); break;
+ case META_PLUS_QUERY: fprintf(stderr, "META +?"); break;
+ case META_PLUS_PLUS: fprintf(stderr, "META ++"); break;
+ case META_QUERY: fprintf(stderr, "META ?"); break;
+ case META_QUERY_QUERY: fprintf(stderr, "META ??"); break;
+ case META_QUERY_PLUS: fprintf(stderr, "META ?+"); break;
+
+ case META_ATOMIC: fprintf(stderr, "META (?>"); break;
+ case META_NOCAPTURE: fprintf(stderr, "META (?:"); break;
+ case META_LOOKAHEAD: fprintf(stderr, "META (?="); break;
+ case META_LOOKAHEADNOT: fprintf(stderr, "META (?!"); break;
+ case META_KET: fprintf(stderr, "META )"); break;
+ case META_ALT: fprintf(stderr, "META | %d", meta_arg); break;
+
+ case META_CLASS: fprintf(stderr, "META ["); break;
+ case META_CLASS_NOT: fprintf(stderr, "META [^"); break;
+ case META_CLASS_END: fprintf(stderr, "META ]"); break;
+ case META_CLASS_EMPTY: fprintf(stderr, "META []"); break;
+ case META_CLASS_EMPTY_NOT: fprintf(stderr, "META [^]"); break;
+
+ case META_RANGE_LITERAL: fprintf(stderr, "META - (literal)"); break;
+ case META_RANGE_ESCAPED: fprintf(stderr, "META - (escaped)"); break;
+
+ case META_POSIX: fprintf(stderr, "META_POSIX %d", *pptr++); break;
+ case META_POSIX_NEG: fprintf(stderr, "META_POSIX_NEG %d", *pptr++); break;
+
+ case META_ACCEPT: fprintf(stderr, "META (*ACCEPT)"); break;
+ case META_FAIL: fprintf(stderr, "META (*FAIL)"); break;
+ case META_COMMIT: fprintf(stderr, "META (*COMMIT)"); break;
+ case META_PRUNE: fprintf(stderr, "META (*PRUNE)"); break;
+ case META_SKIP: fprintf(stderr, "META (*SKIP)"); break;
+ case META_THEN: fprintf(stderr, "META (*THEN)"); break;
+
+ case META_OPTIONS: fprintf(stderr, "META_OPTIONS 0x%02x", *pptr++); break;
+
+ case META_LOOKBEHIND:
+ fprintf(stderr, "META (?<= %d offset=", meta_arg);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_LOOKBEHINDNOT:
+ fprintf(stderr, "META (?<! %d offset=", meta_arg);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_CALLOUT_NUMBER:
+ fprintf(stderr, "META (?C%d) next=%d/%d", pptr[2], pptr[0],
+ pptr[1]);
+ pptr += 3;
+ break;
+
+ case META_CALLOUT_STRING:
+ {
+ uint32_t patoffset = *pptr++; /* Offset of next pattern item */
+ uint32_t patlength = *pptr++; /* Length of next pattern item */
+ fprintf(stderr, "META (?Cstring) length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd next=%d/%d", offset, patoffset, patlength);
+ }
+ break;
+
+ case META_RECURSE_BYNAME:
+ fprintf(stderr, "META (?(&name) length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_BACKREF_BYNAME:
+ fprintf(stderr, "META_BACKREF_BYNAME length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_COND_NUMBER:
+ fprintf(stderr, "META_COND_NUMBER %d offset=", pptr[SIZEOFFSET]);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ pptr++;
+ break;
+
+ case META_COND_DEFINE:
+ fprintf(stderr, "META (?(DEFINE) offset=");
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_COND_VERSION:
+ fprintf(stderr, "META (?(VERSION%s", (*pptr++ == 0)? "=" : ">=");
+ fprintf(stderr, "%d.", *pptr++);
+ fprintf(stderr, "%d)", *pptr++);
+ break;
+
+ case META_COND_NAME:
+ fprintf(stderr, "META (?(<name>) length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_COND_RNAME:
+ fprintf(stderr, "META (?(R&name) length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ /* This is kept as a name, because it might be. */
+
+ case META_COND_RNUMBER:
+ fprintf(stderr, "META (?(Rnumber) length=%d offset=", *pptr++);
+ GETOFFSET(offset, pptr);
+ fprintf(stderr, "%zd", offset);
+ break;
+
+ case META_MARK:
+ fprintf(stderr, "META (*MARK:");
+ goto SHOWARG;
+
+ case META_COMMIT_ARG:
+ fprintf(stderr, "META (*COMMIT:");
+ goto SHOWARG;
+
+ case META_PRUNE_ARG:
+ fprintf(stderr, "META (*PRUNE:");
+ goto SHOWARG;
+
+ case META_SKIP_ARG:
+ fprintf(stderr, "META (*SKIP:");
+ goto SHOWARG;
+
+ case META_THEN_ARG:
+ fprintf(stderr, "META (*THEN:");
+ SHOWARG:
+ length = *pptr++;
+ for (i = 0; i < length; i++)
+ {
+ uint32_t cc = *pptr++;
+ if (cc > 32 && cc < 128) fprintf(stderr, "%c", cc);
+ else fprintf(stderr, "\\x{%x}", cc);
+ }
+ fprintf(stderr, ") length=%u", length);
+ break;
+ }
+ fprintf(stderr, "\n");
+ }
+return;
+}
+#endif /* DEBUG_SHOW_PARSED */
+
+
+
+/*************************************************
+* Copy compiled code *
+*************************************************/
+
+/* Compiled JIT code cannot be copied, so the new compiled block has no
+associated JIT data. */
+
+PCRE2_EXP_DEFN pcre2_code * PCRE2_CALL_CONVENTION
+pcre2_code_copy(const pcre2_code *code)
+{
+PCRE2_SIZE* ref_count;
+pcre2_code *newcode;
+
+if (code == NULL) return NULL;
+newcode = code->memctl.malloc(code->blocksize, code->memctl.memory_data);
+if (newcode == NULL) return NULL;
+memcpy(newcode, code, code->blocksize);
+newcode->executable_jit = NULL;
+
+/* If the code is one that has been deserialized, increment the reference count
+in the decoded tables. */
+
+if ((code->flags & PCRE2_DEREF_TABLES) != 0)
+ {
+ ref_count = (PCRE2_SIZE *)(code->tables + tables_length);
+ (*ref_count)++;
+ }
+
+return newcode;
+}
+
+
+
+/*************************************************
+* Copy compiled code and character tables *
+*************************************************/
+
+/* Compiled JIT code cannot be copied, so the new compiled block has no
+associated JIT data. This version of code_copy also makes a separate copy of
+the character tables. */
+
+PCRE2_EXP_DEFN pcre2_code * PCRE2_CALL_CONVENTION
+pcre2_code_copy_with_tables(const pcre2_code *code)
+{
+PCRE2_SIZE* ref_count;
+pcre2_code *newcode;
+uint8_t *newtables;
+
+if (code == NULL) return NULL;
+newcode = code->memctl.malloc(code->blocksize, code->memctl.memory_data);
+if (newcode == NULL) return NULL;
+memcpy(newcode, code, code->blocksize);
+newcode->executable_jit = NULL;
+
+newtables = code->memctl.malloc(tables_length + sizeof(PCRE2_SIZE),
+ code->memctl.memory_data);
+if (newtables == NULL)
+ {
+ code->memctl.free((void *)newcode, code->memctl.memory_data);
+ return NULL;
+ }
+memcpy(newtables, code->tables, tables_length);
+ref_count = (PCRE2_SIZE *)(newtables + tables_length);
+*ref_count = 1;
+
+newcode->tables = newtables;
+newcode->flags |= PCRE2_DEREF_TABLES;
+return newcode;
+}
+
+
+
+/*************************************************
+* Free compiled code *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_code_free(pcre2_code *code)
+{
+PCRE2_SIZE* ref_count;
+
+if (code != NULL)
+ {
+ if (code->executable_jit != NULL)
+ PRIV(jit_free)(code->executable_jit, &code->memctl);
+
+ if ((code->flags & PCRE2_DEREF_TABLES) != 0)
+ {
+ /* Decoded tables belong to the codes after deserialization, and they must
+ be freed when there are no more reference to them. The *ref_count should
+ always be > 0. */
+
+ ref_count = (PCRE2_SIZE *)(code->tables + tables_length);
+ if (*ref_count > 0)
+ {
+ (*ref_count)--;
+ if (*ref_count == 0)
+ code->memctl.free((void *)code->tables, code->memctl.memory_data);
+ }
+ }
+
+ code->memctl.free(code, code->memctl.memory_data);
+ }
+}
+
+
+
+/*************************************************
+* Read a number, possibly signed *
+*************************************************/
+
+/* This function is used to read numbers in the pattern. The initial pointer
+must be the sign or first digit of the number. When relative values (introduced
+by + or -) are allowed, they are relative group numbers, and the result must be
+greater than zero.
+
+Arguments:
+ ptrptr points to the character pointer variable
+ ptrend points to the end of the input string
+ allow_sign if < 0, sign not allowed; if >= 0, sign is relative to this
+ max_value the largest number allowed
+ max_error the error to give for an over-large number
+ intptr where to put the result
+ errcodeptr where to put an error code
+
+Returns: TRUE - a number was read
+ FALSE - errorcode == 0 => no number was found
+ errorcode != 0 => an error occurred
+*/
+
+static BOOL
+read_number(PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend, int32_t allow_sign,
+ uint32_t max_value, uint32_t max_error, int *intptr, int *errorcodeptr)
+{
+int sign = 0;
+uint32_t n = 0;
+PCRE2_SPTR ptr = *ptrptr;
+BOOL yield = FALSE;
+
+*errorcodeptr = 0;
+
+if (allow_sign >= 0 && ptr < ptrend)
+ {
+ if (*ptr == CHAR_PLUS)
+ {
+ sign = +1;
+ max_value -= allow_sign;
+ ptr++;
+ }
+ else if (*ptr == CHAR_MINUS)
+ {
+ sign = -1;
+ ptr++;
+ }
+ }
+
+if (ptr >= ptrend || !IS_DIGIT(*ptr)) return FALSE;
+while (ptr < ptrend && IS_DIGIT(*ptr))
+ {
+ n = n * 10 + *ptr++ - CHAR_0;
+ if (n > max_value)
+ {
+ *errorcodeptr = max_error;
+ goto EXIT;
+ }
+ }
+
+if (allow_sign >= 0 && sign != 0)
+ {
+ if (n == 0)
+ {
+ *errorcodeptr = ERR26; /* +0 and -0 are not allowed */
+ goto EXIT;
+ }
+
+ if (sign > 0) n += allow_sign;
+ else if ((int)n > allow_sign)
+ {
+ *errorcodeptr = ERR15; /* Non-existent subpattern */
+ goto EXIT;
+ }
+ else n = allow_sign + 1 - n;
+ }
+
+yield = TRUE;
+
+EXIT:
+*intptr = n;
+*ptrptr = ptr;
+return yield;
+}
+
+
+
+/*************************************************
+* Read repeat counts *
+*************************************************/
+
+/* Read an item of the form {n,m} and return the values if non-NULL pointers
+are supplied. Repeat counts must be less than 65536 (MAX_REPEAT_COUNT); a
+larger value is used for "unlimited". We have to use signed arguments for
+read_number() because it is capable of returning a signed value.
+
+Arguments:
+ ptrptr points to pointer to character after'{'
+ ptrend pointer to end of input
+ minp if not NULL, pointer to int for min
+ maxp if not NULL, pointer to int for max (-1 if no max)
+ returned as -1 if no max
+ errorcodeptr points to error code variable
+
+Returns: FALSE if not a repeat quantifier, errorcode set zero
+ FALSE on error, with errorcode set non-zero
+ TRUE on success, with pointer updated to point after '}'
+*/
+
+static BOOL
+read_repeat_counts(PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend, uint32_t *minp,
+ uint32_t *maxp, int *errorcodeptr)
+{
+PCRE2_SPTR p = *ptrptr;
+BOOL yield = FALSE;
+int32_t min = 0;
+int32_t max = REPEAT_UNLIMITED; /* This value is larger than MAX_REPEAT_COUNT */
+
+/* NB read_number() initializes the error code to zero. The only error is for a
+number that is too big. */
+
+if (!read_number(&p, ptrend, -1, MAX_REPEAT_COUNT, ERR5, &min, errorcodeptr))
+ goto EXIT;
+
+if (p >= ptrend) goto EXIT;
+
+if (*p == CHAR_RIGHT_CURLY_BRACKET)
+ {
+ p++;
+ max = min;
+ }
+
+else
+ {
+ if (*p++ != CHAR_COMMA || p >= ptrend) goto EXIT;
+ if (*p != CHAR_RIGHT_CURLY_BRACKET)
+ {
+ if (!read_number(&p, ptrend, -1, MAX_REPEAT_COUNT, ERR5, &max,
+ errorcodeptr) || p >= ptrend || *p != CHAR_RIGHT_CURLY_BRACKET)
+ goto EXIT;
+ if (max < min)
+ {
+ *errorcodeptr = ERR4;
+ goto EXIT;
+ }
+ }
+ p++;
+ }
+
+yield = TRUE;
+if (minp != NULL) *minp = (uint32_t)min;
+if (maxp != NULL) *maxp = (uint32_t)max;
+
+/* Update the pattern pointer on success, or after an error, but not when
+the result is "not a repeat quantifier". */
+
+EXIT:
+if (yield || *errorcodeptr != 0) *ptrptr = p;
+return yield;
+
+
+
+}
+
+
+
+/*************************************************
+* Handle escapes *
+*************************************************/
+
+/* This function is called when a \ has been encountered. It either returns a
+positive value for a simple escape such as \d, or 0 for a data character, which
+is placed in chptr. A backreference to group n is returned as negative n. On
+entry, ptr is pointing at the character after \. On exit, it points after the
+final code unit of the escape sequence.
+
+This function is also called from pcre2_substitute() to handle escape sequences
+in replacement strings. In this case, the cb argument is NULL, and in the case
+of escapes that have further processing, only sequences that define a data
+character are recognised. The isclass argument is not relevant; the options
+argument is the final value of the compiled pattern's options.
+
+Arguments:
+ ptrptr points to the input position pointer
+ ptrend points to the end of the input
+ chptr points to a returned data character
+ errorcodeptr points to the errorcode variable (containing zero)
+ options the current options bits
+ isclass TRUE if inside a character class
+ cb compile data block
+
+Returns: zero => a data character
+ positive => a special escape sequence
+ negative => a numerical back reference
+ on error, errorcodeptr is set non-zero
+*/
+
+int
+PRIV(check_escape)(PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend, uint32_t *chptr,
+ int *errorcodeptr, uint32_t options, BOOL isclass, compile_block *cb)
+{
+BOOL utf = (options & PCRE2_UTF) != 0;
+PCRE2_SPTR ptr = *ptrptr;
+uint32_t c, cc;
+int escape = 0;
+int i;
+
+/* If backslash is at the end of the string, it's an error. */
+
+if (ptr >= ptrend)
+ {
+ *errorcodeptr = ERR1;
+ return 0;
+ }
+
+GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
+*errorcodeptr = 0; /* Be optimistic */
+
+/* Non-alphanumerics are literals, so we just leave the value in c. An initial
+value test saves a memory lookup for code points outside the alphanumeric
+range. Otherwise, do a table lookup. A non-zero result is something that can be
+returned immediately. Otherwise further processing is required. */
+
+if (c < ESCAPES_FIRST || c > ESCAPES_LAST) {} /* Definitely literal */
+
+else if ((i = escapes[c - ESCAPES_FIRST]) != 0)
+ {
+ if (i > 0) c = (uint32_t)i; else /* Positive is a data character */
+ {
+ escape = -i; /* Else return a special escape */
+ if (cb != NULL && (escape == ESC_P || escape == ESC_p || escape == ESC_X))
+ cb->external_flags |= PCRE2_HASBKPORX; /* Note \P, \p, or \X */
+
+ /* Perl supports \N{name} for character names and \N{U+dddd} for numerical
+ Unicode code points, as well as plain \N for "not newline". PCRE does not
+ support \N{name}. However, it does support quantification such as \N{2,3},
+ so if \N{ is not followed by U+dddd we check for a quantifier. */
+
+ if (escape == ESC_N && ptr < ptrend && *ptr == CHAR_LEFT_CURLY_BRACKET)
+ {
+ PCRE2_SPTR p = ptr + 1;
+
+ /* \N{U+ can be handled by the \x{ code. However, this construction is
+ not valid in EBCDIC environments because it specifies a Unicode
+ character, not a codepoint in the local code. For example \N{U+0041}
+ must be "A" in all environments. Also, in Perl, \N{U+ forces Unicode
+ casing semantics for the entire pattern, so allow it only in UTF (i.e.
+ Unicode) mode. */
+
+ if (ptrend - p > 1 && *p == CHAR_U && p[1] == CHAR_PLUS)
+ {
+#ifdef EBCDIC
+ *errorcodeptr = ERR93;
+#else
+ if (utf)
+ {
+ ptr = p + 1;
+ escape = 0; /* Not a fancy escape after all */
+ goto COME_FROM_NU;
+ }
+ else *errorcodeptr = ERR93;
+#endif
+ }
+
+ /* Give an error if what follows is not a quantifier, but don't override
+ an error set by the quantifier reader (e.g. number overflow). */
+
+ else
+ {
+ if (!read_repeat_counts(&p, ptrend, NULL, NULL, errorcodeptr) &&
+ *errorcodeptr == 0)
+ *errorcodeptr = ERR37;
+ }
+ }
+ }
+ }
+
+/* Escapes that need further processing, including those that are unknown.
+When called from pcre2_substitute(), only \c, \o, and \x are recognized (and \u
+when BSUX is set). */
+
+else
+ {
+ PCRE2_SPTR oldptr;
+ BOOL overflow;
+ int s;
+
+ /* Filter calls from pcre2_substitute(). */
+
+ if (cb == NULL && c != CHAR_c && c != CHAR_o && c != CHAR_x &&
+ (c != CHAR_u || (options & PCRE2_ALT_BSUX) != 0))
+ {
+ *errorcodeptr = ERR3;
+ return 0;
+ }
+
+ switch (c)
+ {
+ /* A number of Perl escapes are not handled by PCRE. We give an explicit
+ error. */
+
+ case CHAR_F:
+ case CHAR_l:
+ case CHAR_L:
+ *errorcodeptr = ERR37;
+ break;
+
+ /* \u is unrecognized when PCRE2_ALT_BSUX is not set. When it is treated
+ specially, \u must be followed by four hex digits. Otherwise it is a
+ lowercase u letter. */
+
+ case CHAR_u:
+ if ((options & PCRE2_ALT_BSUX) == 0) *errorcodeptr = ERR37; else
+ {
+ uint32_t xc;
+ if (ptrend - ptr < 4) break; /* Less than 4 chars */
+ if ((cc = XDIGIT(ptr[0])) == 0xff) break; /* Not a hex digit */
+ if ((xc = XDIGIT(ptr[1])) == 0xff) break; /* Not a hex digit */
+ cc = (cc << 4) | xc;
+ if ((xc = XDIGIT(ptr[2])) == 0xff) break; /* Not a hex digit */
+ cc = (cc << 4) | xc;
+ if ((xc = XDIGIT(ptr[3])) == 0xff) break; /* Not a hex digit */
+ c = (cc << 4) | xc;
+ ptr += 4;
+ if (utf)
+ {
+ if (c > 0x10ffffU) *errorcodeptr = ERR77;
+ else
+ if (c >= 0xd800 && c <= 0xdfff &&
+ (cb->cx->extra_options & PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES) == 0)
+ *errorcodeptr = ERR73;
+ }
+ else if (c > MAX_NON_UTF_CHAR) *errorcodeptr = ERR77;
+ }
+ break;
+
+ /* \U is unrecognized unless PCRE2_ALT_BSUX is set, in which case it is an
+ upper case letter. */
+
+ case CHAR_U:
+ if ((options & PCRE2_ALT_BSUX) == 0) *errorcodeptr = ERR37;
+ break;
+
+ /* In a character class, \g is just a literal "g". Outside a character
+ class, \g must be followed by one of a number of specific things:
+
+ (1) A number, either plain or braced. If positive, it is an absolute
+ backreference. If negative, it is a relative backreference. This is a Perl
+ 5.10 feature.
+
+ (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
+ is part of Perl's movement towards a unified syntax for back references. As
+ this is synonymous with \k{name}, we fudge it up by pretending it really
+ was \k{name}.
+
+ (3) For Oniguruma compatibility we also support \g followed by a name or a
+ number either in angle brackets or in single quotes. However, these are
+ (possibly recursive) subroutine calls, _not_ backreferences. We return
+ the ESC_g code.
+
+ Summary: Return a negative number for a numerical back reference, ESC_k for
+ a named back reference, and ESC_g for a named or numbered subroutine call.
+ */
+
+ case CHAR_g:
+ if (isclass) break;
+
+ if (ptr >= ptrend)
+ {
+ *errorcodeptr = ERR57;
+ break;
+ }
+
+ if (*ptr == CHAR_LESS_THAN_SIGN || *ptr == CHAR_APOSTROPHE)
+ {
+ escape = ESC_g;
+ break;
+ }
+
+ /* If there is a brace delimiter, try to read a numerical reference. If
+ there isn't one, assume we have a name and treat it as \k. */
+
+ if (*ptr == CHAR_LEFT_CURLY_BRACKET)
+ {
+ PCRE2_SPTR p = ptr + 1;
+ if (!read_number(&p, ptrend, cb->bracount, MAX_GROUP_NUMBER, ERR61, &s,
+ errorcodeptr))
+ {
+ if (*errorcodeptr == 0) escape = ESC_k; /* No number found */
+ break;
+ }
+ if (p >= ptrend || *p != CHAR_RIGHT_CURLY_BRACKET)
+ {
+ *errorcodeptr = ERR57;
+ break;
+ }
+ ptr = p + 1;
+ }
+
+ /* Read an undelimited number */
+
+ else
+ {
+ if (!read_number(&ptr, ptrend, cb->bracount, MAX_GROUP_NUMBER, ERR61, &s,
+ errorcodeptr))
+ {
+ if (*errorcodeptr == 0) *errorcodeptr = ERR57; /* No number found */
+ break;
+ }
+ }
+
+ if (s <= 0)
+ {
+ *errorcodeptr = ERR15;
+ break;
+ }
+
+ escape = -s;
+ break;
+
+ /* The handling of escape sequences consisting of a string of digits
+ starting with one that is not zero is not straightforward. Perl has changed
+ over the years. Nowadays \g{} for backreferences and \o{} for octal are
+ recommended to avoid the ambiguities in the old syntax.
+
+ Outside a character class, the digits are read as a decimal number. If the
+ number is less than 10, or if there are that many previous extracting left
+ brackets, it is a back reference. Otherwise, up to three octal digits are
+ read to form an escaped character code. Thus \123 is likely to be octal 123
+ (cf \0123, which is octal 012 followed by the literal 3).
+
+ Inside a character class, \ followed by a digit is always either a literal
+ 8 or 9 or an octal number. */
+
+ case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
+ case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
+
+ if (!isclass)
+ {
+ oldptr = ptr;
+ ptr--; /* Back to the digit */
+ if (!read_number(&ptr, ptrend, -1, INT_MAX/10 - 1, ERR61, &s,
+ errorcodeptr))
+ break;
+
+ /* \1 to \9 are always back references. \8x and \9x are too; \1x to \7x
+ are octal escapes if there are not that many previous captures. */
+
+ if (s < 10 || oldptr[-1] >= CHAR_8 || s <= (int)cb->bracount)
+ {
+ if (s > (int)MAX_GROUP_NUMBER) *errorcodeptr = ERR61;
+ else escape = -s; /* Indicates a back reference */
+ break;
+ }
+ ptr = oldptr; /* Put the pointer back and fall through */
+ }
+
+ /* Handle a digit following \ when the number is not a back reference, or
+ we are within a character class. If the first digit is 8 or 9, Perl used to
+ generate a binary zero and then treat the digit as a following literal. At
+ least by Perl 5.18 this changed so as not to insert the binary zero. */
+
+ if (c >= CHAR_8) break;
+
+ /* Fall through */
+
+ /* \0 always starts an octal number, but we may drop through to here with a
+ larger first octal digit. The original code used just to take the least
+ significant 8 bits of octal numbers (I think this is what early Perls used
+ to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
+ but no more than 3 octal digits. */
+
+ case CHAR_0:
+ c -= CHAR_0;
+ while(i++ < 2 && ptr < ptrend && *ptr >= CHAR_0 && *ptr <= CHAR_7)
+ c = c * 8 + *ptr++ - CHAR_0;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (!utf && c > 0xff) *errorcodeptr = ERR51;
+#endif
+ break;
+
+ /* \o is a relatively new Perl feature, supporting a more general way of
+ specifying character codes in octal. The only supported form is \o{ddd}. */
+
+ case CHAR_o:
+ if (ptr >= ptrend || *ptr++ != CHAR_LEFT_CURLY_BRACKET)
+ {
+ ptr--;
+ *errorcodeptr = ERR55;
+ }
+ else if (ptr >= ptrend || *ptr == CHAR_RIGHT_CURLY_BRACKET)
+ *errorcodeptr = ERR78;
+ else
+ {
+ c = 0;
+ overflow = FALSE;
+ while (ptr < ptrend && *ptr >= CHAR_0 && *ptr <= CHAR_7)
+ {
+ cc = *ptr++;
+ if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
+#if PCRE2_CODE_UNIT_WIDTH == 32
+ if (c >= 0x20000000l) { overflow = TRUE; break; }
+#endif
+ c = (c << 3) + (cc - CHAR_0);
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
+#endif
+ }
+ if (overflow)
+ {
+ while (ptr < ptrend && *ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
+ *errorcodeptr = ERR34;
+ }
+ else if (ptr < ptrend && *ptr++ == CHAR_RIGHT_CURLY_BRACKET)
+ {
+ if (utf && c >= 0xd800 && c <= 0xdfff && (cb == NULL ||
+ (cb->cx->extra_options & PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES) == 0))
+ {
+ ptr--;
+ *errorcodeptr = ERR73;
+ }
+ }
+ else
+ {
+ ptr--;
+ *errorcodeptr = ERR64;
+ }
+ }
+ break;
+
+ /* \x is complicated. When PCRE2_ALT_BSUX is set, \x must be followed by
+ two hexadecimal digits. Otherwise it is a lowercase x letter. */
+
+ case CHAR_x:
+ if ((options & PCRE2_ALT_BSUX) != 0)
+ {
+ uint32_t xc;
+ if (ptrend - ptr < 2) break; /* Less than 2 characters */
+ if ((cc = XDIGIT(ptr[0])) == 0xff) break; /* Not a hex digit */
+ if ((xc = XDIGIT(ptr[1])) == 0xff) break; /* Not a hex digit */
+ c = (cc << 4) | xc;
+ ptr += 2;
+ } /* End PCRE2_ALT_BSUX handling */
+
+ /* Handle \x in Perl's style. \x{ddd} is a character number which can be
+ greater than 0xff in UTF-8 or non-8bit mode, but only if the ddd are hex
+ digits. If not, { used to be treated as a data character. However, Perl
+ seems to read hex digits up to the first non-such, and ignore the rest, so
+ that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
+ now gives an error. */
+
+ else
+ {
+ if (ptr < ptrend && *ptr == CHAR_LEFT_CURLY_BRACKET)
+ {
+#ifndef EBCDIC
+ COME_FROM_NU:
+#endif
+ if (++ptr >= ptrend || *ptr == CHAR_RIGHT_CURLY_BRACKET)
+ {
+ *errorcodeptr = ERR78;
+ break;
+ }
+ c = 0;
+ overflow = FALSE;
+
+ while (ptr < ptrend && (cc = XDIGIT(*ptr)) != 0xff)
+ {
+ ptr++;
+ if (c == 0 && cc == 0) continue; /* Leading zeroes */
+#if PCRE2_CODE_UNIT_WIDTH == 32
+ if (c >= 0x10000000l) { overflow = TRUE; break; }
+#endif
+ c = (c << 4) | cc;
+ if ((utf && c > 0x10ffffU) || (!utf && c > MAX_NON_UTF_CHAR))
+ {
+ overflow = TRUE;
+ break;
+ }
+ }
+
+ if (overflow)
+ {
+ while (ptr < ptrend && XDIGIT(*ptr) != 0xff) ptr++;
+ *errorcodeptr = ERR34;
+ }
+ else if (ptr < ptrend && *ptr++ == CHAR_RIGHT_CURLY_BRACKET)
+ {
+ if (utf && c >= 0xd800 && c <= 0xdfff && (cb == NULL ||
+ (cb->cx->extra_options & PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES) == 0))
+ {
+ ptr--;
+ *errorcodeptr = ERR73;
+ }
+ }
+
+ /* If the sequence of hex digits does not end with '}', give an error.
+ We used just to recognize this construct and fall through to the normal
+ \x handling, but nowadays Perl gives an error, which seems much more
+ sensible, so we do too. */
+
+ else
+ {
+ ptr--;
+ *errorcodeptr = ERR67;
+ }
+ } /* End of \x{} processing */
+
+ /* Read a up to two hex digits after \x */
+
+ else
+ {
+ c = 0;
+ if (ptr >= ptrend || (cc = XDIGIT(*ptr)) == 0xff) break; /* Not a hex digit */
+ ptr++;
+ c = cc;
+ if (ptr >= ptrend || (cc = XDIGIT(*ptr)) == 0xff) break; /* Not a hex digit */
+ ptr++;
+ c = (c << 4) | cc;
+ } /* End of \xdd handling */
+ } /* End of Perl-style \x handling */
+ break;
+
+ /* The handling of \c is different in ASCII and EBCDIC environments. In an
+ ASCII (or Unicode) environment, an error is given if the character
+ following \c is not a printable ASCII character. Otherwise, the following
+ character is upper-cased if it is a letter, and after that the 0x40 bit is
+ flipped. The result is the value of the escape.
+
+ In an EBCDIC environment the handling of \c is compatible with the
+ specification in the perlebcdic document. The following character must be
+ a letter or one of small number of special characters. These provide a
+ means of defining the character values 0-31.
+
+ For testing the EBCDIC handling of \c in an ASCII environment, recognize
+ the EBCDIC value of 'c' explicitly. */
+
+#if defined EBCDIC && 'a' != 0x81
+ case 0x83:
+#else
+ case CHAR_c:
+#endif
+ if (ptr >= ptrend)
+ {
+ *errorcodeptr = ERR2;
+ break;
+ }
+ c = *ptr;
+ if (c >= CHAR_a && c <= CHAR_z) c = UPPER_CASE(c);
+
+ /* Handle \c in an ASCII/Unicode environment. */
+
+#ifndef EBCDIC /* ASCII/UTF-8 coding */
+ if (c < 32 || c > 126) /* Excludes all non-printable ASCII */
+ {
+ *errorcodeptr = ERR68;
+ break;
+ }
+ c ^= 0x40;
+
+ /* Handle \c in an EBCDIC environment. The special case \c? is converted to
+ 255 (0xff) or 95 (0x5f) if other character suggest we are using th POSIX-BC
+ encoding. (This is the way Perl indicates that it handles \c?.) The other
+ valid sequences correspond to a list of specific characters. */
+
+#else
+ if (c == CHAR_QUESTION_MARK)
+ c = ('\\' == 188 && '`' == 74)? 0x5f : 0xff;
+ else
+ {
+ for (i = 0; i < 32; i++)
+ {
+ if (c == ebcdic_escape_c[i]) break;
+ }
+ if (i < 32) c = i; else *errorcodeptr = ERR68;
+ }
+#endif /* EBCDIC */
+
+ ptr++;
+ break;
+
+ /* Any other alphanumeric following \ is an error. Perl gives an error only
+ if in warning mode, but PCRE doesn't have a warning mode. */
+
+ default:
+ *errorcodeptr = ERR3;
+ *ptrptr = ptr - 1; /* Point to the character at fault */
+ return 0;
+ }
+ }
+
+/* Set the pointer to the next character before returning. */
+
+*ptrptr = ptr;
+*chptr = c;
+return escape;
+}
+
+
+
+#ifdef SUPPORT_UNICODE
+/*************************************************
+* Handle \P and \p *
+*************************************************/
+
+/* This function is called after \P or \p has been encountered, provided that
+PCRE2 is compiled with support for UTF and Unicode properties. On entry, the
+contents of ptrptr are pointing after the P or p. On exit, it is left pointing
+after the final code unit of the escape sequence.
+
+Arguments:
+ ptrptr the pattern position pointer
+ negptr a boolean that is set TRUE for negation else FALSE
+ ptypeptr an unsigned int that is set to the type value
+ pdataptr an unsigned int that is set to the detailed property value
+ errorcodeptr the error code variable
+ cb the compile data
+
+Returns: TRUE if the type value was found, or FALSE for an invalid type
+*/
+
+static BOOL
+get_ucp(PCRE2_SPTR *ptrptr, BOOL *negptr, uint16_t *ptypeptr,
+ uint16_t *pdataptr, int *errorcodeptr, compile_block *cb)
+{
+PCRE2_UCHAR c;
+PCRE2_SIZE i, bot, top;
+PCRE2_SPTR ptr = *ptrptr;
+PCRE2_UCHAR name[32];
+
+if (ptr >= cb->end_pattern) goto ERROR_RETURN;
+c = *ptr++;
+*negptr = FALSE;
+
+/* \P or \p can be followed by a name in {}, optionally preceded by ^ for
+negation. */
+
+if (c == CHAR_LEFT_CURLY_BRACKET)
+ {
+ if (ptr >= cb->end_pattern) goto ERROR_RETURN;
+ if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
+ {
+ *negptr = TRUE;
+ ptr++;
+ }
+ for (i = 0; i < (int)(sizeof(name) / sizeof(PCRE2_UCHAR)) - 1; i++)
+ {
+ if (ptr >= cb->end_pattern) goto ERROR_RETURN;
+ c = *ptr++;
+ if (c == CHAR_NUL) goto ERROR_RETURN;
+ if (c == CHAR_RIGHT_CURLY_BRACKET) break;
+ name[i] = c;
+ }
+ if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
+ name[i] = 0;
+ }
+
+/* Otherwise there is just one following character, which must be an ASCII
+letter. */
+
+else if (MAX_255(c) && (cb->ctypes[c] & ctype_letter) != 0)
+ {
+ name[0] = c;
+ name[1] = 0;
+ }
+else goto ERROR_RETURN;
+
+*ptrptr = ptr;
+
+/* Search for a recognized property name using binary chop. */
+
+bot = 0;
+top = PRIV(utt_size);
+
+while (bot < top)
+ {
+ int r;
+ i = (bot + top) >> 1;
+ r = PRIV(strcmp_c8)(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
+ if (r == 0)
+ {
+ *ptypeptr = PRIV(utt)[i].type;
+ *pdataptr = PRIV(utt)[i].value;
+ return TRUE;
+ }
+ if (r > 0) bot = i + 1; else top = i;
+ }
+*errorcodeptr = ERR47; /* Unrecognized name */
+return FALSE;
+
+ERROR_RETURN: /* Malformed \P or \p */
+*errorcodeptr = ERR46;
+*ptrptr = ptr;
+return FALSE;
+}
+#endif
+
+
+
+/*************************************************
+* Check for POSIX class syntax *
+*************************************************/
+
+/* This function is called when the sequence "[:" or "[." or "[=" is
+encountered in a character class. It checks whether this is followed by a
+sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
+reach an unescaped ']' without the special preceding character, return FALSE.
+
+Originally, this function only recognized a sequence of letters between the
+terminators, but it seems that Perl recognizes any sequence of characters,
+though of course unknown POSIX names are subsequently rejected. Perl gives an
+"Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
+didn't consider this to be a POSIX class. Likewise for [:1234:].
+
+The problem in trying to be exactly like Perl is in the handling of escapes. We
+have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
+class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
+below handles the special cases \\ and \], but does not try to do any other
+escape processing. This makes it different from Perl for cases such as
+[:l\ower:] where Perl recognizes it as the POSIX class "lower" but PCRE does
+not recognize "l\ower". This is a lesser evil than not diagnosing bad classes
+when Perl does, I think.
+
+A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
+It seems that the appearance of a nested POSIX class supersedes an apparent
+external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
+a digit. This is handled by returning FALSE if the start of a new group with
+the same terminator is encountered, since the next closing sequence must close
+the nested group, not the outer one.
+
+In Perl, unescaped square brackets may also appear as part of class names. For
+example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
+[:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
+seem right at all. PCRE does not allow closing square brackets in POSIX class
+names.
+
+Arguments:
+ ptr pointer to the character after the initial [ (colon, dot, equals)
+ ptrend pointer to the end of the pattern
+ endptr where to return a pointer to the terminating ':', '.', or '='
+
+Returns: TRUE or FALSE
+*/
+
+static BOOL
+check_posix_syntax(PCRE2_SPTR ptr, PCRE2_SPTR ptrend, PCRE2_SPTR *endptr)
+{
+PCRE2_UCHAR terminator; /* Don't combine these lines; the Solaris cc */
+terminator = *ptr++; /* compiler warns about "non-constant" initializer. */
+
+for (; ptrend - ptr >= 2; ptr++)
+ {
+ if (*ptr == CHAR_BACKSLASH &&
+ (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET || ptr[1] == CHAR_BACKSLASH))
+ ptr++;
+
+ else if ((*ptr == CHAR_LEFT_SQUARE_BRACKET && ptr[1] == terminator) ||
+ *ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
+
+ else if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ *endptr = ptr;
+ return TRUE;
+ }
+ }
+
+return FALSE;
+}
+
+
+
+/*************************************************
+* Check POSIX class name *
+*************************************************/
+
+/* This function is called to check the name given in a POSIX-style class entry
+such as [:alnum:].
+
+Arguments:
+ ptr points to the first letter
+ len the length of the name
+
+Returns: a value representing the name, or -1 if unknown
+*/
+
+static int
+check_posix_name(PCRE2_SPTR ptr, int len)
+{
+const char *pn = posix_names;
+int yield = 0;
+while (posix_name_lengths[yield] != 0)
+ {
+ if (len == posix_name_lengths[yield] &&
+ PRIV(strncmp_c8)(ptr, pn, (unsigned int)len) == 0) return yield;
+ pn += posix_name_lengths[yield] + 1;
+ yield++;
+ }
+return -1;
+}
+
+
+
+/*************************************************
+* Read a subpattern or VERB name *
+*************************************************/
+
+/* This function is called from parse_regex() below whenever it needs to read
+the name of a subpattern or a (*VERB). The initial pointer must be to the
+character before the name. If that character is '*' we are reading a verb name.
+The pointer is updated to point after the name, for a VERB, or after tha name's
+terminator for a subpattern name. Returning both the offset and the name
+pointer is redundant information, but some callers use one and some the other,
+so it is simplest just to return both.
+
+Arguments:
+ ptrptr points to the character pointer variable
+ ptrend points to the end of the input string
+ terminator the terminator of a subpattern name must be this
+ offsetptr where to put the offset from the start of the pattern
+ nameptr where to put a pointer to the name in the input
+ namelenptr where to put the length of the name
+ errcodeptr where to put an error code
+ cb pointer to the compile data block
+
+Returns: TRUE if a name was read
+ FALSE otherwise, with error code set
+*/
+
+static BOOL
+read_name(PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend, uint32_t terminator,
+ PCRE2_SIZE *offsetptr, PCRE2_SPTR *nameptr, uint32_t *namelenptr,
+ int *errorcodeptr, compile_block *cb)
+{
+PCRE2_SPTR ptr = *ptrptr;
+BOOL is_verb = (*ptr == CHAR_ASTERISK);
+uint32_t namelen = 0;
+uint32_t ctype = is_verb? ctype_letter : ctype_word;
+
+if (++ptr >= ptrend)
+ {
+ *errorcodeptr = is_verb? ERR60: /* Verb not recognized or malformed */
+ ERR62; /* Subpattern name expected */
+ goto FAILED;
+ }
+
+*nameptr = ptr;
+*offsetptr = (PCRE2_SIZE)(ptr - cb->start_pattern);
+
+if (IS_DIGIT(*ptr))
+ {
+ *errorcodeptr = ERR44; /* Group name must not start with digit */
+ goto FAILED;
+ }
+
+while (ptr < ptrend && MAX_255(*ptr) && (cb->ctypes[*ptr] & ctype) != 0)
+ {
+ ptr++;
+ namelen++;
+ if (namelen > MAX_NAME_SIZE)
+ {
+ *errorcodeptr = ERR48;
+ goto FAILED;
+ }
+ }
+
+/* Subpattern names must not be empty, and their terminator is checked here.
+(What follows a verb name is checked separately.) */
+
+if (!is_verb)
+ {
+ if (namelen == 0)
+ {
+ *errorcodeptr = ERR62; /* Subpattern name expected */
+ goto FAILED;
+ }
+ if (ptr >= ptrend || *ptr != (PCRE2_UCHAR)terminator)
+ {
+ *errorcodeptr = ERR42;
+ goto FAILED;
+ }
+ ptr++;
+ }
+
+*namelenptr = namelen;
+*ptrptr = ptr;
+return TRUE;
+
+FAILED:
+*ptrptr = ptr;
+return FALSE;
+}
+
+
+
+/*************************************************
+* Manage callouts at start of cycle *
+*************************************************/
+
+/* At the start of a new item in parse_regex() we are able to record the
+details of the previous item in a prior callout, and also to set up an
+automatic callout if enabled. Avoid having two adjacent automatic callouts,
+which would otherwise happen for items such as \Q that contribute nothing to
+the parsed pattern.
+
+Arguments:
+ ptr current pattern pointer
+ pcalloutptr points to a pointer to previous callout, or NULL
+ auto_callout TRUE if auto_callouts are enabled
+ parsed_pattern the parsed pattern pointer
+ cb compile block
+
+Returns: possibly updated parsed_pattern pointer.
+*/
+
+static uint32_t *
+manage_callouts(PCRE2_SPTR ptr, uint32_t **pcalloutptr, BOOL auto_callout,
+ uint32_t *parsed_pattern, compile_block *cb)
+{
+uint32_t *previous_callout = *pcalloutptr;
+
+if (previous_callout != NULL) previous_callout[2] = (uint32_t)(ptr -
+ cb->start_pattern - (PCRE2_SIZE)previous_callout[1]);
+
+if (!auto_callout) previous_callout = NULL; else
+ {
+ if (previous_callout == NULL ||
+ previous_callout != parsed_pattern - 4 ||
+ previous_callout[3] != 255)
+ {
+ previous_callout = parsed_pattern; /* Set up new automatic callout */
+ parsed_pattern += 4;
+ previous_callout[0] = META_CALLOUT_NUMBER;
+ previous_callout[2] = 0;
+ previous_callout[3] = 255;
+ }
+ previous_callout[1] = (uint32_t)(ptr - cb->start_pattern);
+ }
+
+*pcalloutptr = previous_callout;
+return parsed_pattern;
+}
+
+
+
+/*************************************************
+* Parse regex and identify named groups *
+*************************************************/
+
+/* This function is called first of all. It scans the pattern and does two
+things: (1) It identifies capturing groups and makes a table of named capturing
+groups so that information about them is fully available to both the compiling
+scans. (2) It writes a parsed version of the pattern with comments omitted and
+escapes processed into the parsed_pattern vector.
+
+Arguments:
+ ptr points to the start of the pattern
+ options compiling dynamic options (may change during the scan)
+ has_lookbehind points to a boolean, set TRUE if a lookbehind is found
+ cb pointer to the compile data block
+
+Returns: zero on success or a non-zero error code, with the
+ error offset placed in the cb field
+*/
+
+/* A structure and some flags for dealing with nested groups. */
+
+typedef struct nest_save {
+ uint16_t nest_depth;
+ uint16_t reset_group;
+ uint16_t max_group;
+ uint16_t flags;
+ uint32_t options;
+} nest_save;
+
+#define NSF_RESET 0x0001u
+#define NSF_CONDASSERT 0x0002u
+
+/* Options that are changeable within the pattern must be tracked during
+parsing. Some (e.g. PCRE2_EXTENDED) are implemented entirely during parsing,
+but all must be tracked so that META_OPTIONS items set the correct values for
+the main compiling phase. */
+
+#define PARSE_TRACKED_OPTIONS (PCRE2_CASELESS|PCRE2_DOTALL|PCRE2_DUPNAMES| \
+ PCRE2_EXTENDED|PCRE2_EXTENDED_MORE|PCRE2_MULTILINE|PCRE2_NO_AUTO_CAPTURE| \
+ PCRE2_UNGREEDY)
+
+/* States used for analyzing ranges in character classes. The two OK values
+must be last. */
+
+enum { RANGE_NO, RANGE_STARTED, RANGE_OK_ESCAPED, RANGE_OK_LITERAL };
+
+/* Only in 32-bit mode can there be literals > META_END. A macros encapsulates
+the storing of literal values in the parsed pattern. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+#define PARSED_LITERAL(c, p) \
+ { \
+ if (c >= META_END) *p++ = META_BIGVALUE; \
+ *p++ = c; \
+ okquantifier = TRUE; \
+ }
+#else
+#define PARSED_LITERAL(c, p) *p++ = c; okquantifier = TRUE;
+#endif
+
+/* Here's the actual function. */
+
+static int parse_regex(PCRE2_SPTR ptr, uint32_t options, BOOL *has_lookbehind,
+ compile_block *cb)
+{
+uint32_t c;
+uint32_t delimiter;
+uint32_t namelen;
+uint32_t class_range_state;
+uint32_t *verblengthptr = NULL; /* Value avoids compiler warning */
+uint32_t *previous_callout = NULL;
+uint32_t *parsed_pattern = cb->parsed_pattern;
+uint32_t *parsed_pattern_end = cb->parsed_pattern_end;
+uint32_t meta_quantifier = 0;
+uint32_t add_after_mark = 0;
+uint16_t nest_depth = 0;
+int after_manual_callout = 0;
+int expect_cond_assert = 0;
+int errorcode = 0;
+int escape;
+int i;
+BOOL inescq = FALSE;
+BOOL inverbname = FALSE;
+BOOL utf = (options & PCRE2_UTF) != 0;
+BOOL auto_callout = (options & PCRE2_AUTO_CALLOUT) != 0;
+BOOL isdupname;
+BOOL negate_class;
+BOOL okquantifier = FALSE;
+PCRE2_SPTR thisptr;
+PCRE2_SPTR name;
+PCRE2_SPTR ptrend = cb->end_pattern;
+PCRE2_SPTR verbnamestart = NULL; /* Value avoids compiler warning */
+named_group *ng;
+nest_save *top_nest, *end_nests;
+
+/* Insert leading items for word and line matching (features provided for the
+benefit of pcre2grep). */
+
+if ((cb->cx->extra_options & PCRE2_EXTRA_MATCH_LINE) != 0)
+ {
+ *parsed_pattern++ = META_CIRCUMFLEX;
+ *parsed_pattern++ = META_NOCAPTURE;
+ }
+else if ((cb->cx->extra_options & PCRE2_EXTRA_MATCH_WORD) != 0)
+ {
+ *parsed_pattern++ = META_ESCAPE + ESC_b;
+ *parsed_pattern++ = META_NOCAPTURE;
+ }
+
+/* If the pattern is actually a literal string, process it separately to avoid
+cluttering up the main loop. */
+
+if ((options & PCRE2_LITERAL) != 0)
+ {
+ while (ptr < ptrend)
+ {
+ if (parsed_pattern >= parsed_pattern_end)
+ {
+ errorcode = ERR63; /* Internal error (parsed pattern overflow) */
+ goto FAILED;
+ }
+ thisptr = ptr;
+ GETCHARINCTEST(c, ptr);
+ if (auto_callout)
+ parsed_pattern = manage_callouts(thisptr, &previous_callout,
+ auto_callout, parsed_pattern, cb);
+ PARSED_LITERAL(c, parsed_pattern);
+ }
+ goto PARSED_END;
+ }
+
+/* Process a real regex which may contain meta-characters. */
+
+top_nest = NULL;
+end_nests = (nest_save *)(cb->start_workspace + cb->workspace_size);
+
+/* The size of the nest_save structure might not be a factor of the size of the
+workspace. Therefore we must round down end_nests so as to correctly avoid
+creating a nest_save that spans the end of the workspace. */
+
+end_nests = (nest_save *)((char *)end_nests -
+ ((cb->workspace_size * sizeof(PCRE2_UCHAR)) % sizeof(nest_save)));
+
+/* PCRE2_EXTENDED_MORE implies PCRE2_EXTENDED */
+
+if ((options & PCRE2_EXTENDED_MORE) != 0) options |= PCRE2_EXTENDED;
+
+/* Now scan the pattern */
+
+while (ptr < ptrend)
+ {
+ int prev_expect_cond_assert;
+ uint32_t min_repeat, max_repeat;
+ uint32_t set, unset, *optset;
+ uint32_t terminator;
+ uint32_t prev_meta_quantifier;
+ BOOL prev_okquantifier;
+ PCRE2_SPTR tempptr;
+ PCRE2_SIZE offset;
+
+ if (parsed_pattern >= parsed_pattern_end)
+ {
+ errorcode = ERR63; /* Internal error (parsed pattern overflow) */
+ goto FAILED;
+ }
+
+ if (nest_depth > cb->cx->parens_nest_limit)
+ {
+ errorcode = ERR19;
+ goto FAILED; /* Parentheses too deeply nested */
+ }
+
+ /* Get next input character, save its position for callout handling. */
+
+ thisptr = ptr;
+ GETCHARINCTEST(c, ptr);
+
+ /* Copy quoted literals until \E, allowing for the possibility of automatic
+ callouts, except when processing a (*VERB) "name". */
+
+ if (inescq)
+ {
+ if (c == CHAR_BACKSLASH && ptr < ptrend && *ptr == CHAR_E)
+ {
+ inescq = FALSE;
+ ptr++; /* Skip E */
+ }
+ else
+ {
+ if (expect_cond_assert > 0) /* A literal is not allowed if we are */
+ { /* expecting a conditional assertion, */
+ ptr--; /* but an empty \Q\E sequence is OK. */
+ errorcode = ERR28;
+ goto FAILED;
+ }
+ if (!inverbname && after_manual_callout-- <= 0)
+ parsed_pattern = manage_callouts(thisptr, &previous_callout,
+ auto_callout, parsed_pattern, cb);
+ PARSED_LITERAL(c, parsed_pattern);
+ meta_quantifier = 0;
+ }
+ continue; /* Next character */
+ }
+
+ /* If we are processing the "name" part of a (*VERB:NAME) item, all
+ characters up to the closing parenthesis are literals except when
+ PCRE2_ALT_VERBNAMES is set. That causes backslash interpretation, but only \Q
+ and \E and escaped characters are allowed (no character types such as \d). If
+ PCRE2_EXTENDED is also set, we must ignore white space and # comments. Do
+ this by not entering the special (*VERB:NAME) processing - they are then
+ picked up below. Note that c is a character, not a code unit, so we must not
+ use MAX_255 to test its size because MAX_255 tests code units and is assumed
+ TRUE in 8-bit mode. */
+
+ if (inverbname &&
+ (
+ /* EITHER: not both options set */
+ ((options & (PCRE2_EXTENDED | PCRE2_ALT_VERBNAMES)) !=
+ (PCRE2_EXTENDED | PCRE2_ALT_VERBNAMES)) ||
+#ifdef SUPPORT_UNICODE
+ /* OR: character > 255 AND not Unicode Pattern White Space */
+ (c > 255 && (c|1) != 0x200f && (c|1) != 0x2029) ||
+#endif
+ /* OR: not a # comment or isspace() white space */
+ (c < 256 && c != CHAR_NUMBER_SIGN && (cb->ctypes[c] & ctype_space) == 0
+#ifdef SUPPORT_UNICODE
+ /* and not CHAR_NEL when Unicode is supported */
+ && c != CHAR_NEL
+#endif
+ )))
+ {
+ PCRE2_SIZE verbnamelength;
+
+ switch(c)
+ {
+ default:
+ PARSED_LITERAL(c, parsed_pattern);
+ break;
+
+ case CHAR_RIGHT_PARENTHESIS:
+ inverbname = FALSE;
+ okquantifier = FALSE; /* Was probably set by literals */
+ /* This is the length in characters */
+ verbnamelength = (PCRE2_SIZE)(parsed_pattern - verblengthptr - 1);
+ /* But the limit on the length is in code units */
+ if (ptr - verbnamestart - 1 > (int)MAX_MARK)
+ {
+ ptr--;
+ errorcode = ERR76;
+ goto FAILED;
+ }
+ *verblengthptr = (uint32_t)verbnamelength;
+
+ /* If this name was on a verb such as (*ACCEPT) which does not continue,
+ a (*MARK) was generated for the name. We now add the original verb as the
+ next item. */
+
+ if (add_after_mark != 0)
+ {
+ *parsed_pattern++ = add_after_mark;
+ add_after_mark = 0;
+ }
+ break;
+
+ case CHAR_BACKSLASH:
+ if ((options & PCRE2_ALT_VERBNAMES) != 0)
+ {
+ escape = PRIV(check_escape)(&ptr, ptrend, &c, &errorcode, options,
+ FALSE, cb);
+ if (errorcode != 0) goto FAILED;
+ }
+ else escape = 0; /* Treat all as literal */
+
+ switch(escape)
+ {
+ case 0:
+ PARSED_LITERAL(c, parsed_pattern);
+ break;
+
+ case ESC_Q:
+ inescq = TRUE;
+ break;
+
+ case ESC_E: /* Ignore */
+ break;
+
+ default:
+ errorcode = ERR40; /* Invalid in verb name */
+ goto FAILED;
+ }
+ }
+ continue; /* Next character in pattern */
+ }
+
+ /* Not a verb name character. At this point we must process everything that
+ must not change the quantification state. This is mainly comments, but we
+ handle \Q and \E here as well, so that an item such as A\Q\E+ is treated as
+ A+, as in Perl. An isolated \E is ignored. */
+
+ if (c == CHAR_BACKSLASH && ptr < ptrend)
+ {
+ if (*ptr == CHAR_Q || *ptr == CHAR_E)
+ {
+ inescq = *ptr == CHAR_Q;
+ ptr++;
+ continue;
+ }
+ }
+
+ /* Skip over whitespace and # comments in extended mode. Note that c is a
+ character, not a code unit, so we must not use MAX_255 to test its size
+ because MAX_255 tests code units and is assumed TRUE in 8-bit mode. The
+ whitespace characters are those designated as "Pattern White Space" by
+ Unicode, which are the isspace() characters plus CHAR_NEL (newline), which is
+ U+0085 in Unicode, plus U+200E, U+200F, U+2028, and U+2029. These are a
+ subset of space characters that match \h and \v. */
+
+ if ((options & PCRE2_EXTENDED) != 0)
+ {
+ if (c < 256 && (cb->ctypes[c] & ctype_space) != 0) continue;
+#ifdef SUPPORT_UNICODE
+ if (c == CHAR_NEL || (c|1) == 0x200f || (c|1) == 0x2029) continue;
+#endif
+ if (c == CHAR_NUMBER_SIGN)
+ {
+ while (ptr < ptrend)
+ {
+ if (IS_NEWLINE(ptr)) /* For non-fixed-length newline cases, */
+ { /* IS_NEWLINE sets cb->nllen. */
+ ptr += cb->nllen;
+ break;
+ }
+ ptr++;
+#ifdef SUPPORT_UNICODE
+ if (utf) FORWARDCHARTEST(ptr, ptrend);
+#endif
+ }
+ continue; /* Next character in pattern */
+ }
+ }
+
+ /* Skip over bracketed comments */
+
+ if (c == CHAR_LEFT_PARENTHESIS && ptrend - ptr >= 2 &&
+ ptr[0] == CHAR_QUESTION_MARK && ptr[1] == CHAR_NUMBER_SIGN)
+ {
+ while (++ptr < ptrend && *ptr != CHAR_RIGHT_PARENTHESIS);
+ if (ptr >= ptrend)
+ {
+ errorcode = ERR18; /* A special error for missing ) in a comment */
+ goto FAILED; /* to make it easier to debug. */
+ }
+ ptr++;
+ continue; /* Next character in pattern */
+ }
+
+ /* If the next item is not a quantifier, fill in length of any previous
+ callout and create an auto callout if required. */
+
+ if (c != CHAR_ASTERISK && c != CHAR_PLUS && c != CHAR_QUESTION_MARK &&
+ (c != CHAR_LEFT_CURLY_BRACKET ||
+ (tempptr = ptr,
+ !read_repeat_counts(&tempptr, ptrend, NULL, NULL, &errorcode))))
+ {
+ if (after_manual_callout-- <= 0)
+ parsed_pattern = manage_callouts(thisptr, &previous_callout, auto_callout,
+ parsed_pattern, cb);
+ }
+
+ /* If expect_cond_assert is 2, we have just passed (?( and are expecting an
+ assertion, possibly preceded by a callout. If the value is 1, we have just
+ had the callout and expect an assertion. There must be at least 3 more
+ characters in all cases. When expect_cond_assert is 2, we know that the
+ current character is an opening parenthesis, as otherwise we wouldn't be
+ here. However, when it is 1, we need to check, and it's easiest just to check
+ always. Note that expect_cond_assert may be negative, since all callouts just
+ decrement it. */
+
+ if (expect_cond_assert > 0)
+ {
+ BOOL ok = c == CHAR_LEFT_PARENTHESIS && ptrend - ptr >= 3 &&
+ ptr[0] == CHAR_QUESTION_MARK;
+ if (ok) switch(ptr[1])
+ {
+ case CHAR_C:
+ ok = expect_cond_assert == 2;
+ break;
+
+ case CHAR_EQUALS_SIGN:
+ case CHAR_EXCLAMATION_MARK:
+ break;
+
+ case CHAR_LESS_THAN_SIGN:
+ ok = ptr[2] == CHAR_EQUALS_SIGN || ptr[2] == CHAR_EXCLAMATION_MARK;
+ break;
+
+ default:
+ ok = FALSE;
+ }
+
+ if (!ok)
+ {
+ ptr--; /* Adjust error offset */
+ errorcode = ERR28;
+ goto FAILED;
+ }
+ }
+
+ /* Remember whether we are expecting a conditional assertion, and set the
+ default for this item. */
+
+ prev_expect_cond_assert = expect_cond_assert;
+ expect_cond_assert = 0;
+
+ /* Remember quantification status for the previous significant item, then set
+ default for this item. */
+
+ prev_okquantifier = okquantifier;
+ prev_meta_quantifier = meta_quantifier;
+ okquantifier = FALSE;
+ meta_quantifier = 0;
+
+ /* If the previous significant item was a quantifier, adjust the parsed code
+ if there is a following modifier. The base meta value is always followed by
+ the PLUS and QUERY values, in that order. We do this here rather than after
+ reading a quantifier so that intervening comments and /x whitespace can be
+ ignored without having to replicate code. */
+
+ if (prev_meta_quantifier != 0 && (c == CHAR_QUESTION_MARK || c == CHAR_PLUS))
+ {
+ parsed_pattern[(prev_meta_quantifier == META_MINMAX)? -3 : -1] =
+ prev_meta_quantifier + ((c == CHAR_QUESTION_MARK)?
+ 0x00020000u : 0x00010000u);
+ continue; /* Next character in pattern */
+ }
+
+
+ /* Process the next item in the main part of a pattern. */
+
+ switch(c)
+ {
+ default: /* Non-special character */
+ PARSED_LITERAL(c, parsed_pattern);
+ break;
+
+
+ /* ---- Escape sequence ---- */
+
+ case CHAR_BACKSLASH:
+ tempptr = ptr;
+ escape = PRIV(check_escape)(&ptr, ptrend, &c, &errorcode, options,
+ FALSE, cb);
+ if (errorcode != 0)
+ {
+ ESCAPE_FAILED:
+ if ((cb->cx->extra_options & PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL) == 0)
+ goto FAILED;
+ ptr = tempptr;
+ if (ptr >= ptrend) c = CHAR_BACKSLASH; else
+ {
+ GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
+ }
+ escape = 0; /* Treat as literal character */
+ }
+
+ /* The escape was a data escape or literal character. */
+
+ if (escape == 0)
+ {
+ PARSED_LITERAL(c, parsed_pattern);
+ }
+
+ /* The escape was a back (or forward) reference. We keep the offset in
+ order to give a more useful diagnostic for a bad forward reference. For
+ references to groups numbered less than 10 we can't use more than two items
+ in parsed_pattern because they may be just two characters in the input (and
+ in a 64-bit world an offset may need two elements). So for them, the offset
+ of the first occurrent is held in a special vector. */
+
+ else if (escape < 0)
+ {
+ offset = (PCRE2_SIZE)(ptr - cb->start_pattern - 1);
+ escape = -escape;
+ *parsed_pattern++ = META_BACKREF | (uint32_t)escape;
+ if (escape < 10)
+ {
+ if (cb->small_ref_offset[escape] == PCRE2_UNSET)
+ cb->small_ref_offset[escape] = offset;
+ }
+ else
+ {
+ PUTOFFSET(offset, parsed_pattern);
+ }
+ okquantifier = TRUE;
+ }
+
+ /* The escape was a character class such as \d etc. or other special
+ escape indicator such as \A or \X. Most of them generate just a single
+ parsed item, but \P and \p are followed by a 16-bit type and a 16-bit
+ value. They are supported only when Unicode is available. The type and
+ value are packed into a single 32-bit value so that the whole sequences
+ uses only two elements in the parsed_vector. This is because the same
+ coding is used if \d (for example) is turned into \p{Nd} when PCRE2_UCP is
+ set.
+
+ There are also some cases where the escape sequence is followed by a name:
+ \k{name}, \k<name>, and \k'name' are backreferences by name, and \g<name>
+ and \g'name' are subroutine calls by name; \g{name} is a synonym for
+ \k{name}. Note that \g<number> and \g'number' are handled by check_escape()
+ and returned as a negative value (handled above). A name is coded as an
+ offset into the pattern and a length. */
+
+ else switch (escape)
+ {
+ case ESC_C:
+#ifdef NEVER_BACKSLASH_C
+ errorcode = ERR85;
+ goto ESCAPE_FAILED;
+#else
+ if ((options & PCRE2_NEVER_BACKSLASH_C) != 0)
+ {
+ errorcode = ERR83;
+ goto ESCAPE_FAILED;
+ }
+#endif
+ okquantifier = TRUE;
+ *parsed_pattern++ = META_ESCAPE + escape;
+ break;
+
+ case ESC_X:
+#ifndef SUPPORT_UNICODE
+ errorcode = ERR45; /* Supported only with Unicode support */
+ goto ESCAPE_FAILED;
+#endif
+ case ESC_H:
+ case ESC_h:
+ case ESC_N:
+ case ESC_R:
+ case ESC_V:
+ case ESC_v:
+ okquantifier = TRUE;
+ *parsed_pattern++ = META_ESCAPE + escape;
+ break;
+
+ default: /* \A, \B, \b, \G, \K, \Z, \z cannot be quantified. */
+ *parsed_pattern++ = META_ESCAPE + escape;
+ break;
+
+ /* Escapes that change in UCP mode. Note that PCRE2_UCP will never be set
+ without Unicode support because it is checked when pcre2_compile() is
+ called. */
+
+ case ESC_d:
+ case ESC_D:
+ case ESC_s:
+ case ESC_S:
+ case ESC_w:
+ case ESC_W:
+ okquantifier = TRUE;
+ if ((options & PCRE2_UCP) == 0)
+ {
+ *parsed_pattern++ = META_ESCAPE + escape;
+ }
+ else
+ {
+ *parsed_pattern++ = META_ESCAPE +
+ ((escape == ESC_d || escape == ESC_s || escape == ESC_w)?
+ ESC_p : ESC_P);
+ switch(escape)
+ {
+ case ESC_d:
+ case ESC_D:
+ *parsed_pattern++ = (PT_PC << 16) | ucp_Nd;
+ break;
+
+ case ESC_s:
+ case ESC_S:
+ *parsed_pattern++ = PT_SPACE << 16;
+ break;
+
+ case ESC_w:
+ case ESC_W:
+ *parsed_pattern++ = PT_WORD << 16;
+ break;
+ }
+ }
+ break;
+
+ /* Unicode property matching */
+
+ case ESC_P:
+ case ESC_p:
+#ifdef SUPPORT_UNICODE
+ {
+ BOOL negated;
+ uint16_t ptype = 0, pdata = 0;
+ if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcode, cb))
+ goto ESCAPE_FAILED;
+ if (negated) escape = (escape == ESC_P)? ESC_p : ESC_P;
+ *parsed_pattern++ = META_ESCAPE + escape;
+ *parsed_pattern++ = (ptype << 16) | pdata;
+ okquantifier = TRUE;
+ }
+#else
+ errorcode = ERR45;
+ goto ESCAPE_FAILED;
+#endif
+ break; /* End \P and \p */
+
+ /* When \g is used with quotes or angle brackets as delimiters, it is a
+ numerical or named subroutine call, and control comes here. When used
+ with brace delimiters it is a numberical back reference and does not come
+ here because check_escape() returns it directly as a reference. \k is
+ always a named back reference. */
+
+ case ESC_g:
+ case ESC_k:
+ if (ptr >= ptrend || (*ptr != CHAR_LEFT_CURLY_BRACKET &&
+ *ptr != CHAR_LESS_THAN_SIGN && *ptr != CHAR_APOSTROPHE))
+ {
+ errorcode = (escape == ESC_g)? ERR57 : ERR69;
+ goto ESCAPE_FAILED;
+ }
+ terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
+ CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
+ CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET;
+
+ /* For a non-braced \g, check for a numerical recursion. */
+
+ if (escape == ESC_g && terminator != CHAR_RIGHT_CURLY_BRACKET)
+ {
+ PCRE2_SPTR p = ptr + 1;
+
+ if (read_number(&p, ptrend, cb->bracount, MAX_GROUP_NUMBER, ERR61, &i,
+ &errorcode))
+ {
+ if (p >= ptrend || *p != terminator)
+ {
+ errorcode = ERR57;
+ goto ESCAPE_FAILED;
+ }
+ ptr = p;
+ goto SET_RECURSION;
+ }
+ if (errorcode != 0) goto ESCAPE_FAILED;
+ }
+
+ /* Not a numerical recursion */
+
+ if (!read_name(&ptr, ptrend, terminator, &offset, &name, &namelen,
+ &errorcode, cb)) goto ESCAPE_FAILED;
+
+ /* \k and \g when used with braces are back references, whereas \g used
+ with quotes or angle brackets is a recursion */
+
+ *parsed_pattern++ =
+ (escape == ESC_k || terminator == CHAR_RIGHT_CURLY_BRACKET)?
+ META_BACKREF_BYNAME : META_RECURSE_BYNAME;
+ *parsed_pattern++ = namelen;
+
+ PUTOFFSET(offset, parsed_pattern);
+ okquantifier = TRUE;
+ break; /* End special escape processing */
+ }
+ break; /* End escape sequence processing */
+
+
+ /* ---- Single-character special items ---- */
+
+ case CHAR_CIRCUMFLEX_ACCENT:
+ *parsed_pattern++ = META_CIRCUMFLEX;
+ break;
+
+ case CHAR_DOLLAR_SIGN:
+ *parsed_pattern++ = META_DOLLAR;
+ break;
+
+ case CHAR_DOT:
+ *parsed_pattern++ = META_DOT;
+ okquantifier = TRUE;
+ break;
+
+
+ /* ---- Single-character quantifiers ---- */
+
+ case CHAR_ASTERISK:
+ meta_quantifier = META_ASTERISK;
+ goto CHECK_QUANTIFIER;
+
+ case CHAR_PLUS:
+ meta_quantifier = META_PLUS;
+ goto CHECK_QUANTIFIER;
+
+ case CHAR_QUESTION_MARK:
+ meta_quantifier = META_QUERY;
+ goto CHECK_QUANTIFIER;
+
+
+ /* ---- Potential {n,m} quantifier ---- */
+
+ case CHAR_LEFT_CURLY_BRACKET:
+ if (!read_repeat_counts(&ptr, ptrend, &min_repeat, &max_repeat,
+ &errorcode))
+ {
+ if (errorcode != 0) goto FAILED; /* Error in quantifier. */
+ PARSED_LITERAL(c, parsed_pattern); /* Not a quantifier */
+ break; /* No more quantifier processing */
+ }
+ meta_quantifier = META_MINMAX;
+ /* Fall through */
+
+
+ /* ---- Quantifier post-processing ---- */
+
+ /* Check that a quantifier is allowed after the previous item. */
+
+ CHECK_QUANTIFIER:
+ if (!prev_okquantifier)
+ {
+ errorcode = ERR9;
+ goto FAILED_BACK;
+ }
+
+ /* Now we can put the quantifier into the parsed pattern vector. At this
+ stage, we have only the basic quantifier. The check for a following + or ?
+ modifier happens at the top of the loop, after any intervening comments
+ have been removed. */
+
+ *parsed_pattern++ = meta_quantifier;
+ if (c == CHAR_LEFT_CURLY_BRACKET)
+ {
+ *parsed_pattern++ = min_repeat;
+ *parsed_pattern++ = max_repeat;
+ }
+ break;
+
+
+ /* ---- Character class ---- */
+
+ case CHAR_LEFT_SQUARE_BRACKET:
+ okquantifier = TRUE;
+
+ /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
+ used for "start of word" and "end of word". As these are otherwise illegal
+ sequences, we don't break anything by recognizing them. They are replaced
+ by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
+ erroneous and are handled by the normal code below. */
+
+ if (ptrend - ptr >= 6 &&
+ (PRIV(strncmp_c8)(ptr, STRING_WEIRD_STARTWORD, 6) == 0 ||
+ PRIV(strncmp_c8)(ptr, STRING_WEIRD_ENDWORD, 6) == 0))
+ {
+ *parsed_pattern++ = META_ESCAPE + ESC_b;
+
+ if (ptr[2] == CHAR_LESS_THAN_SIGN)
+ {
+ *parsed_pattern++ = META_LOOKAHEAD;
+ }
+ else
+ {
+ *parsed_pattern++ = META_LOOKBEHIND;
+ *has_lookbehind = TRUE;
+
+ /* The offset is used only for the "non-fixed length" error; this won't
+ occur here, so just store zero. */
+
+ PUTOFFSET((PCRE2_SIZE)0, parsed_pattern);
+ }
+
+ if ((options & PCRE2_UCP) == 0)
+ *parsed_pattern++ = META_ESCAPE + ESC_w;
+ else
+ {
+ *parsed_pattern++ = META_ESCAPE + ESC_p;
+ *parsed_pattern++ = PT_WORD << 16;
+ }
+ *parsed_pattern++ = META_KET;
+ ptr += 6;
+ break;
+ }
+
+ /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
+ they are encountered at the top level, so we'll do that too. */
+
+ if (ptr < ptrend && (*ptr == CHAR_COLON || *ptr == CHAR_DOT ||
+ *ptr == CHAR_EQUALS_SIGN) &&
+ check_posix_syntax(ptr, ptrend, &tempptr))
+ {
+ errorcode = (*ptr-- == CHAR_COLON)? ERR12 : ERR13;
+ goto FAILED;
+ }
+
+ /* Process a regular character class. If the first character is '^', set
+ the negation flag. If the first few characters (either before or after ^)
+ are \Q\E or \E or space or tab in extended-more mode, we skip them too.
+ This makes for compatibility with Perl. */
+
+ negate_class = FALSE;
+ while (ptr < ptrend)
+ {
+ GETCHARINCTEST(c, ptr);
+ if (c == CHAR_BACKSLASH)
+ {
+ if (ptr < ptrend && *ptr == CHAR_E) ptr++;
+ else if (ptrend - ptr >= 3 &&
+ PRIV(strncmp_c8)(ptr, STR_Q STR_BACKSLASH STR_E, 3) == 0)
+ ptr += 3;
+ else
+ break;
+ }
+ else if ((options & PCRE2_EXTENDED_MORE) != 0 &&
+ (c == CHAR_SPACE || c == CHAR_HT)) /* Note: just these two */
+ continue;
+ else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
+ negate_class = TRUE;
+ else break;
+ }
+
+ /* Now the real contents of the class; c has the first "real" character.
+ Empty classes are permitted only if the option is set. */
+
+ if (c == CHAR_RIGHT_SQUARE_BRACKET &&
+ (cb->external_options & PCRE2_ALLOW_EMPTY_CLASS) != 0)
+ {
+ *parsed_pattern++ = negate_class? META_CLASS_EMPTY_NOT : META_CLASS_EMPTY;
+ break; /* End of class processing */
+ }
+
+ /* Process a non-empty class. */
+
+ *parsed_pattern++ = negate_class? META_CLASS_NOT : META_CLASS;
+ class_range_state = RANGE_NO;
+
+ /* In an EBCDIC environment, Perl treats alphabetic ranges specially
+ because there are holes in the encoding, and simply using the range A-Z
+ (for example) would include the characters in the holes. This applies only
+ to ranges where both values are literal; [\xC1-\xE9] is different to [A-Z]
+ in this respect. In order to accommodate this, we keep track of whether
+ character values are literal or not, and a state variable for handling
+ ranges. */
+
+ /* Loop for the contents of the class */
+
+ for (;;)
+ {
+ BOOL char_is_literal = TRUE;
+
+ /* Inside \Q...\E everything is literal except \E */
+
+ if (inescq)
+ {
+ if (c == CHAR_BACKSLASH && ptr < ptrend && *ptr == CHAR_E)
+ {
+ inescq = FALSE; /* Reset literal state */
+ ptr++; /* Skip the 'E' */
+ goto CLASS_CONTINUE;
+ }
+ goto CLASS_LITERAL;
+ }
+
+ /* Skip over space and tab (only) in extended-more mode. */
+
+ if ((options & PCRE2_EXTENDED_MORE) != 0 &&
+ (c == CHAR_SPACE || c == CHAR_HT))
+ goto CLASS_CONTINUE;
+
+ /* Handle POSIX class names. Perl allows a negation extension of the
+ form [:^name:]. A square bracket that doesn't match the syntax is
+ treated as a literal. We also recognize the POSIX constructions
+ [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
+ 5.6 and 5.8 do. */
+
+ if (c == CHAR_LEFT_SQUARE_BRACKET &&
+ ptrend - ptr >= 3 &&
+ (*ptr == CHAR_COLON || *ptr == CHAR_DOT ||
+ *ptr == CHAR_EQUALS_SIGN) &&
+ check_posix_syntax(ptr, ptrend, &tempptr))
+ {
+ BOOL posix_negate = FALSE;
+ int posix_class;
+
+ /* Perl treats a hyphen before a POSIX class as a literal, not the
+ start of a range. However, it gives a warning in its warning mode. PCRE
+ does not have a warning mode, so we give an error, because this is
+ likely an error on the user's part. */
+
+ if (class_range_state == RANGE_STARTED)
+ {
+ errorcode = ERR50;
+ goto FAILED;
+ }
+
+ if (*ptr != CHAR_COLON)
+ {
+ errorcode = ERR13;
+ goto FAILED_BACK;
+ }
+
+ if (*(++ptr) == CHAR_CIRCUMFLEX_ACCENT)
+ {
+ posix_negate = TRUE;
+ ptr++;
+ }
+
+ posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
+ if (posix_class < 0)
+ {
+ errorcode = ERR30;
+ goto FAILED;
+ }
+ ptr = tempptr + 2;
+
+ /* Perl treats a hyphen after a POSIX class as a literal, not the
+ start of a range. However, it gives a warning in its warning mode
+ unless the hyphen is the last character in the class. PCRE does not
+ have a warning mode, so we give an error, because this is likely an
+ error on the user's part. */
+
+ if (ptr < ptrend - 1 && *ptr == CHAR_MINUS &&
+ ptr[1] != CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ errorcode = ERR50;
+ goto FAILED;
+ }
+
+ /* Set "a hyphen is not the start of a range" for the -] case, and also
+ in case the POSIX class is followed by \E or \Q\E (possibly repeated -
+ fuzzers do that kind of thing) and *then* a hyphen. This causes that
+ hyphen to be treated as a literal. I don't think it's worth setting up
+ special apparatus to do otherwise. */
+
+ class_range_state = RANGE_NO;
+
+ /* When PCRE2_UCP is set, some of the POSIX classes are converted to
+ use Unicode properties \p or \P or, in one case, \h or \H. The
+ substitutes table has two values per class, containing the type and
+ value of a \p or \P item. The special cases are specified with a
+ negative type: a non-zero value causes \h or \H to be used, and a zero
+ value falls through to behave like a non-UCP POSIX class. */
+
+#ifdef SUPPORT_UNICODE
+ if ((options & PCRE2_UCP) != 0)
+ {
+ int ptype = posix_substitutes[2*posix_class];
+ int pvalue = posix_substitutes[2*posix_class + 1];
+ if (ptype >= 0)
+ {
+ *parsed_pattern++ = META_ESCAPE + (posix_negate? ESC_P : ESC_p);
+ *parsed_pattern++ = (ptype << 16) | pvalue;
+ goto CLASS_CONTINUE;
+ }
+
+ if (pvalue != 0)
+ {
+ *parsed_pattern++ = META_ESCAPE + (posix_negate? ESC_H : ESC_h);
+ goto CLASS_CONTINUE;
+ }
+
+ /* Fall through */
+ }
+#endif /* SUPPORT_UNICODE */
+
+ /* Non-UCP POSIX class */
+
+ *parsed_pattern++ = posix_negate? META_POSIX_NEG : META_POSIX;
+ *parsed_pattern++ = posix_class;
+ }
+
+ /* Handle potential start of range */
+
+ else if (c == CHAR_MINUS && class_range_state >= RANGE_OK_ESCAPED)
+ {
+ *parsed_pattern++ = (class_range_state == RANGE_OK_LITERAL)?
+ META_RANGE_LITERAL : META_RANGE_ESCAPED;
+ class_range_state = RANGE_STARTED;
+ }
+
+ /* Handle a literal character */
+
+ else if (c != CHAR_BACKSLASH)
+ {
+ CLASS_LITERAL:
+ if (class_range_state == RANGE_STARTED)
+ {
+ if (c == parsed_pattern[-2]) /* Optimize one-char range */
+ parsed_pattern--;
+ else if (parsed_pattern[-2] > c) /* Check range is in order */
+ {
+ errorcode = ERR8;
+ goto FAILED_BACK;
+ }
+ else
+ {
+ if (!char_is_literal && parsed_pattern[-1] == META_RANGE_LITERAL)
+ parsed_pattern[-1] = META_RANGE_ESCAPED;
+ PARSED_LITERAL(c, parsed_pattern);
+ }
+ class_range_state = RANGE_NO;
+ }
+ else /* Potential start of range */
+ {
+ class_range_state = char_is_literal?
+ RANGE_OK_LITERAL : RANGE_OK_ESCAPED;
+ PARSED_LITERAL(c, parsed_pattern);
+ }
+ }
+
+ /* Handle escapes in a class */
+
+ else
+ {
+ tempptr = ptr;
+ escape = PRIV(check_escape)(&ptr, ptrend, &c, &errorcode,
+ options, TRUE, cb);
+ if (errorcode != 0)
+ {
+ CLASS_ESCAPE_FAILED:
+ if ((cb->cx->extra_options & PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL) == 0)
+ goto FAILED;
+ ptr = tempptr;
+ if (ptr >= ptrend) c = CHAR_BACKSLASH; else
+ {
+ GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
+ }
+ escape = 0; /* Treat as literal character */
+ }
+
+ if (escape == 0) /* Escaped character code point is in c */
+ {
+ char_is_literal = FALSE;
+ goto CLASS_LITERAL;
+ }
+
+ /* These three escapes do not alter the class range state. */
+
+ if (escape == ESC_b)
+ {
+ c = CHAR_BS; /* \b is backspace in a class */
+ char_is_literal = FALSE;
+ goto CLASS_LITERAL;
+ }
+
+ else if (escape == ESC_Q)
+ {
+ inescq = TRUE; /* Enter literal mode */
+ goto CLASS_CONTINUE;
+ }
+
+ else if (escape == ESC_E) /* Ignore orphan \E */
+ goto CLASS_CONTINUE;
+
+ /* The second part of a range can be a single-character escape
+ sequence (detected above), but not any of the other escapes. Perl
+ treats a hyphen as a literal in such circumstances. However, in Perl's
+ warning mode, a warning is given, so PCRE now faults it, as it is
+ almost certainly a mistake on the user's part. */
+
+ if (class_range_state == RANGE_STARTED)
+ {
+ errorcode = ERR50;
+ goto CLASS_ESCAPE_FAILED;
+ }
+
+ /* Of the remaining escapes, only those that define characters are
+ allowed in a class. None may start a range. */
+
+ class_range_state = RANGE_NO;
+ switch(escape)
+ {
+ case ESC_N:
+ errorcode = ERR71; /* Not supported in a class */
+ goto CLASS_ESCAPE_FAILED;
+
+ case ESC_H:
+ case ESC_h:
+ case ESC_V:
+ case ESC_v:
+ *parsed_pattern++ = META_ESCAPE + escape;
+ break;
+
+ /* These escapes are converted to Unicode property tests when
+ PCRE2_UCP is set. */
+
+ case ESC_d:
+ case ESC_D:
+ case ESC_s:
+ case ESC_S:
+ case ESC_w:
+ case ESC_W:
+ if ((options & PCRE2_UCP) == 0)
+ {
+ *parsed_pattern++ = META_ESCAPE + escape;
+ }
+ else
+ {
+ *parsed_pattern++ = META_ESCAPE +
+ ((escape == ESC_d || escape == ESC_s || escape == ESC_w)?
+ ESC_p : ESC_P);
+ switch(escape)
+ {
+ case ESC_d:
+ case ESC_D:
+ *parsed_pattern++ = (PT_PC << 16) | ucp_Nd;
+ break;
+
+ case ESC_s:
+ case ESC_S:
+ *parsed_pattern++ = PT_SPACE << 16;
+ break;
+
+ case ESC_w:
+ case ESC_W:
+ *parsed_pattern++ = PT_WORD << 16;
+ break;
+ }
+ }
+ break;
+
+ /* Explicit Unicode property matching */
+
+ case ESC_P:
+ case ESC_p:
+#ifdef SUPPORT_UNICODE
+ {
+ BOOL negated;
+ uint16_t ptype = 0, pdata = 0;
+ if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcode, cb))
+ goto FAILED;
+ if (negated) escape = (escape == ESC_P)? ESC_p : ESC_P;
+ *parsed_pattern++ = META_ESCAPE + escape;
+ *parsed_pattern++ = (ptype << 16) | pdata;
+ }
+#else
+ errorcode = ERR45;
+ goto CLASS_ESCAPE_FAILED;
+#endif
+ break; /* End \P and \p */
+
+ default: /* All others are not allowed in a class */
+ errorcode = ERR7;
+ ptr--;
+ goto CLASS_ESCAPE_FAILED;
+ }
+
+ /* Perl gives a warning unless a following hyphen is the last character
+ in the class. PCRE throws an error. */
+
+ if (ptr < ptrend - 1 && *ptr == CHAR_MINUS &&
+ ptr[1] != CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ errorcode = ERR50;
+ goto FAILED;
+ }
+ }
+
+ /* Proceed to next thing in the class. */
+
+ CLASS_CONTINUE:
+ if (ptr >= ptrend)
+ {
+ errorcode = ERR6; /* Missing terminating ']' */
+ goto FAILED;
+ }
+ GETCHARINCTEST(c, ptr);
+ if (c == CHAR_RIGHT_SQUARE_BRACKET && !inescq) break;
+ } /* End of class-processing loop */
+
+ if (class_range_state == RANGE_STARTED)
+ {
+ parsed_pattern[-1] = CHAR_MINUS;
+ class_range_state = RANGE_NO;
+ }
+
+ *parsed_pattern++ = META_CLASS_END;
+ break; /* End of character class */
+
+
+ /* ---- Opening parenthesis ---- */
+
+ case CHAR_LEFT_PARENTHESIS:
+ if (ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+
+ /* If ( is not followed by ? it is either a capture or a special verb. */
+
+ if (*ptr != CHAR_QUESTION_MARK)
+ {
+ const char *vn;
+
+ /* Handle capturing brackets (or non-capturing if auto-capture is turned
+ off). */
+
+ if (*ptr != CHAR_ASTERISK)
+ {
+ nest_depth++;
+ if ((options & PCRE2_NO_AUTO_CAPTURE) == 0)
+ {
+ cb->bracount++;
+ *parsed_pattern++ = META_CAPTURE | cb->bracount;
+ }
+ else *parsed_pattern++ = META_NOCAPTURE;
+ }
+
+
+ /* ---- Handle (*VERB) and (*VERB:NAME) ---- */
+
+ /* Do nothing for (*) so it gives a "bad quantifier" error rather than
+ "(*MARK) must have an argument". */
+
+ else if (ptrend - ptr > 1 && ptr[1] != CHAR_RIGHT_PARENTHESIS)
+ {
+ vn = verbnames;
+ if (!read_name(&ptr, ptrend, 0, &offset, &name, &namelen, &errorcode,
+ cb)) goto FAILED;
+ if (ptr >= ptrend || (*ptr != CHAR_COLON &&
+ *ptr != CHAR_RIGHT_PARENTHESIS))
+ {
+ errorcode = ERR60; /* Malformed */
+ goto FAILED;
+ }
+
+ /* Scan the table of verb names */
+
+ for (i = 0; i < verbcount; i++)
+ {
+ if (namelen == verbs[i].len &&
+ PRIV(strncmp_c8)(name, vn, namelen) == 0)
+ break;
+ vn += verbs[i].len + 1;
+ }
+
+ if (i >= verbcount)
+ {
+ errorcode = ERR60; /* Verb not recognized */
+ goto FAILED;
+ }
+
+ /* An empty argument is treated as no argument. */
+
+ if (*ptr == CHAR_COLON && ptr + 1 < ptrend &&
+ ptr[1] == CHAR_RIGHT_PARENTHESIS)
+ ptr++; /* Advance to the closing parens */
+
+ /* Check for mandatory non-empty argument; this is (*MARK) */
+
+ if (verbs[i].has_arg > 0 && *ptr != CHAR_COLON)
+ {
+ errorcode = ERR66;
+ goto FAILED;
+ }
+
+ /* It appears that Perl allows any characters whatsoever, other than a
+ closing parenthesis, to appear in arguments ("names"), so we no longer
+ insist on letters, digits, and underscores. Perl does not, however, do
+ any interpretation within arguments, and has no means of including a
+ closing parenthesis. PCRE supports escape processing but only when it
+ is requested by an option. We set inverbname TRUE here, and let the
+ main loop take care of this so that escape and \x processing is done by
+ the main code above. */
+
+ if (*ptr++ == CHAR_COLON) /* Skip past : or ) */
+ {
+ /* Some optional arguments can be treated as a preceding (*MARK) */
+
+ if (verbs[i].has_arg < 0)
+ {
+ add_after_mark = verbs[i].meta;
+ *parsed_pattern++ = META_MARK;
+ }
+
+ /* The remaining verbs with arguments (except *MARK) need a different
+ opcode. */
+
+ else
+ {
+ *parsed_pattern++ = verbs[i].meta +
+ ((verbs[i].meta != META_MARK)? 0x00010000u:0);
+ }
+
+ /* Set up for reading the name in the main loop. */
+
+ verblengthptr = parsed_pattern++;
+ verbnamestart = ptr;
+ inverbname = TRUE;
+ }
+ else /* No verb "name" argument */
+ {
+ *parsed_pattern++ = verbs[i].meta;
+ }
+ } /* End of (*VERB) handling */
+ break; /* Done with this parenthesis */
+ } /* End of groups that don't start with (? */
+
+
+ /* ---- Items starting (? ---- */
+
+ /* The type of item is determined by what follows (?. Handle (?| and option
+ changes under "default" because both need a new block on the nest stack.
+ Comments starting with (?# are handled above. Note that there is some
+ ambiguity about the sequence (?- because if a digit follows it's a relative
+ recursion or subroutine call whereas otherwise it's an option unsetting. */
+
+ if (++ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+
+ switch(*ptr)
+ {
+ default:
+ if (*ptr == CHAR_MINUS && ptrend - ptr > 1 && IS_DIGIT(ptr[1]))
+ goto RECURSION_BYNUMBER; /* The + case is handled by CHAR_PLUS */
+
+ /* We now have either (?| or a (possibly empty) option setting,
+ optionally followed by a non-capturing group. */
+
+ nest_depth++;
+ if (top_nest == NULL) top_nest = (nest_save *)(cb->start_workspace);
+ else if (++top_nest >= end_nests)
+ {
+ errorcode = ERR84;
+ goto FAILED;
+ }
+ top_nest->nest_depth = nest_depth;
+ top_nest->flags = 0;
+ top_nest->options = options & PARSE_TRACKED_OPTIONS;
+
+ /* Start of non-capturing group that resets the capture count for each
+ branch. */
+
+ if (*ptr == CHAR_VERTICAL_LINE)
+ {
+ top_nest->reset_group = (uint16_t)cb->bracount;
+ top_nest->max_group = (uint16_t)cb->bracount;
+ top_nest->flags |= NSF_RESET;
+ cb->external_flags |= PCRE2_DUPCAPUSED;
+ *parsed_pattern++ = META_NOCAPTURE;
+ ptr++;
+ }
+
+ /* Scan for options imnsxJU to be set or unset. */
+
+ else
+ {
+ BOOL hyphenok = TRUE;
+ uint32_t oldoptions = options;
+
+ top_nest->reset_group = 0;
+ top_nest->max_group = 0;
+ set = unset = 0;
+ optset = &set;
+
+ /* ^ at the start unsets imnsx and disables the subsequent use of - */
+
+ if (ptr < ptrend && *ptr == CHAR_CIRCUMFLEX_ACCENT)
+ {
+ options &= ~(PCRE2_CASELESS|PCRE2_MULTILINE|PCRE2_NO_AUTO_CAPTURE|
+ PCRE2_DOTALL|PCRE2_EXTENDED|PCRE2_EXTENDED_MORE);
+ hyphenok = FALSE;
+ ptr++;
+ }
+
+ while (ptr < ptrend && *ptr != CHAR_RIGHT_PARENTHESIS &&
+ *ptr != CHAR_COLON)
+ {
+ switch (*ptr++)
+ {
+ case CHAR_MINUS:
+ if (!hyphenok)
+ {
+ errorcode = ERR94;
+ ptr--; /* Correct the offset */
+ goto FAILED;
+ }
+ optset = &unset;
+ hyphenok = FALSE;
+ break;
+
+ case CHAR_J: /* Record that it changed in the external options */
+ *optset |= PCRE2_DUPNAMES;
+ cb->external_flags |= PCRE2_JCHANGED;
+ break;
+
+ case CHAR_i: *optset |= PCRE2_CASELESS; break;
+ case CHAR_m: *optset |= PCRE2_MULTILINE; break;
+ case CHAR_n: *optset |= PCRE2_NO_AUTO_CAPTURE; break;
+ case CHAR_s: *optset |= PCRE2_DOTALL; break;
+ case CHAR_U: *optset |= PCRE2_UNGREEDY; break;
+
+ /* If x appears twice it sets the extended extended option. */
+
+ case CHAR_x:
+ *optset |= PCRE2_EXTENDED;
+ if (ptr < ptrend && *ptr == CHAR_x)
+ {
+ *optset |= PCRE2_EXTENDED_MORE;
+ ptr++;
+ }
+ break;
+
+ default:
+ errorcode = ERR11;
+ ptr--; /* Correct the offset */
+ goto FAILED;
+ }
+ }
+
+ /* If we are setting extended without extended-more, ensure that any
+ existing extended-more gets unset. Also, unsetting extended must also
+ unset extended-more. */
+
+ if ((set & (PCRE2_EXTENDED|PCRE2_EXTENDED_MORE)) == PCRE2_EXTENDED ||
+ (unset & PCRE2_EXTENDED) != 0)
+ unset |= PCRE2_EXTENDED_MORE;
+
+ options = (options | set) & (~unset);
+
+ /* If the options ended with ')' this is not the start of a nested
+ group with option changes, so the options change at this level.
+ In this case, if the previous level set up a nest block, discard the
+ one we have just created. Otherwise adjust it for the previous level.
+ If the options ended with ':' we are starting a non-capturing group,
+ possibly with an options setting. */
+
+ if (ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+ if (*ptr++ == CHAR_RIGHT_PARENTHESIS)
+ {
+ nest_depth--; /* This is not a nested group after all. */
+ if (top_nest > (nest_save *)(cb->start_workspace) &&
+ (top_nest-1)->nest_depth == nest_depth) top_nest--;
+ else top_nest->nest_depth = nest_depth;
+ }
+ else *parsed_pattern++ = META_NOCAPTURE;
+
+ /* If nothing changed, no need to record. */
+
+ if (options != oldoptions)
+ {
+ *parsed_pattern++ = META_OPTIONS;
+ *parsed_pattern++ = options;
+ }
+ } /* End options processing */
+ break; /* End default case after (? */
+
+
+ /* ---- Python syntax support ---- */
+
+ case CHAR_P:
+ if (++ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+
+ /* (?P<name> is the same as (?<name>, which defines a named group. */
+
+ if (*ptr == CHAR_LESS_THAN_SIGN)
+ {
+ terminator = CHAR_GREATER_THAN_SIGN;
+ goto DEFINE_NAME;
+ }
+
+ /* (?P>name) is the same as (?&name), which is a recursion or subroutine
+ call. */
+
+ if (*ptr == CHAR_GREATER_THAN_SIGN) goto RECURSE_BY_NAME;
+
+ /* (?P=name) is the same as \k<name>, a back reference by name. Anything
+ else after (?P is an error. */
+
+ if (*ptr != CHAR_EQUALS_SIGN)
+ {
+ errorcode = ERR41;
+ goto FAILED;
+ }
+ if (!read_name(&ptr, ptrend, CHAR_RIGHT_PARENTHESIS, &offset, &name,
+ &namelen, &errorcode, cb)) goto FAILED;
+ *parsed_pattern++ = META_BACKREF_BYNAME;
+ *parsed_pattern++ = namelen;
+ PUTOFFSET(offset, parsed_pattern);
+ okquantifier = TRUE;
+ break; /* End of (?P processing */
+
+
+ /* ---- Recursion/subroutine calls by number ---- */
+
+ case CHAR_R:
+ i = 0; /* (?R) == (?R0) */
+ ptr++;
+ if (ptr >= ptrend || *ptr != CHAR_RIGHT_PARENTHESIS)
+ {
+ errorcode = ERR58;
+ goto FAILED;
+ }
+ goto SET_RECURSION;
+
+ /* An item starting (?- followed by a digit comes here via the "default"
+ case because (?- followed by a non-digit is an options setting. */
+
+ case CHAR_PLUS:
+ if (ptrend - ptr < 2 || !IS_DIGIT(ptr[1]))
+ {
+ errorcode = ERR29; /* Missing number */
+ goto FAILED;
+ }
+ /* Fall through */
+
+ case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
+ case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
+ RECURSION_BYNUMBER:
+ if (!read_number(&ptr, ptrend,
+ (IS_DIGIT(*ptr))? -1:(int)(cb->bracount), /* + and - are relative */
+ MAX_GROUP_NUMBER, ERR61,
+ &i, &errorcode)) goto FAILED;
+ if (i < 0) /* NB (?0) is permitted */
+ {
+ errorcode = ERR15; /* Unknown group */
+ goto FAILED_BACK;
+ }
+ if (ptr >= ptrend || *ptr != CHAR_RIGHT_PARENTHESIS)
+ goto UNCLOSED_PARENTHESIS;
+
+ SET_RECURSION:
+ *parsed_pattern++ = META_RECURSE | (uint32_t)i;
+ offset = (PCRE2_SIZE)(ptr - cb->start_pattern);
+ ptr++;
+ PUTOFFSET(offset, parsed_pattern);
+ okquantifier = TRUE;
+ break; /* End of recursive call by number handling */
+
+
+ /* ---- Recursion/subroutine calls by name ---- */
+
+ case CHAR_AMPERSAND:
+ RECURSE_BY_NAME:
+ if (!read_name(&ptr, ptrend, CHAR_RIGHT_PARENTHESIS, &offset, &name,
+ &namelen, &errorcode, cb)) goto FAILED;
+ *parsed_pattern++ = META_RECURSE_BYNAME;
+ *parsed_pattern++ = namelen;
+ PUTOFFSET(offset, parsed_pattern);
+ okquantifier = TRUE;
+ break;
+
+ /* ---- Callout with numerical or string argument ---- */
+
+ case CHAR_C:
+ if (++ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+
+ /* If the previous item was a condition starting (?(? an assertion,
+ optionally preceded by a callout, is expected. This is checked later on,
+ during actual compilation. However we need to identify this kind of
+ assertion in this pass because it must not be qualified. The value of
+ expect_cond_assert is set to 2 after (?(? is processed. We decrement it
+ for a callout - still leaving a positive value that identifies the
+ assertion. Multiple callouts or any other items will make it zero or
+ less, which doesn't matter because they will cause an error later. */
+
+ expect_cond_assert = prev_expect_cond_assert - 1;
+
+ /* If previous_callout is not NULL, it means this follows a previous
+ callout. If it was a manual callout, do nothing; this means its "length
+ of next pattern item" field will remain zero. If it was an automatic
+ callout, abolish it. */
+
+ if (previous_callout != NULL && (options & PCRE2_AUTO_CALLOUT) != 0 &&
+ previous_callout == parsed_pattern - 4 &&
+ parsed_pattern[-1] == 255)
+ parsed_pattern = previous_callout;
+
+ /* Save for updating next pattern item length, and skip one item before
+ completing. */
+
+ previous_callout = parsed_pattern;
+ after_manual_callout = 1;
+
+ /* Handle a string argument; specific delimiter is required. */
+
+ if (*ptr != CHAR_RIGHT_PARENTHESIS && !IS_DIGIT(*ptr))
+ {
+ PCRE2_SIZE calloutlength;
+ PCRE2_SPTR startptr = ptr;
+
+ delimiter = 0;
+ for (i = 0; PRIV(callout_start_delims)[i] != 0; i++)
+ {
+ if (*ptr == PRIV(callout_start_delims)[i])
+ {
+ delimiter = PRIV(callout_end_delims)[i];
+ break;
+ }
+ }
+ if (delimiter == 0)
+ {
+ errorcode = ERR82;
+ goto FAILED;
+ }
+
+ *parsed_pattern = META_CALLOUT_STRING;
+ parsed_pattern += 3; /* Skip pattern info */
+
+ for (;;)
+ {
+ if (++ptr >= ptrend)
+ {
+ errorcode = ERR81;
+ ptr = startptr; /* To give a more useful message */
+ goto FAILED;
+ }
+ if (*ptr == delimiter && (++ptr >= ptrend || *ptr != delimiter))
+ break;
+ }
+
+ calloutlength = (PCRE2_SIZE)(ptr - startptr);
+ if (calloutlength > UINT32_MAX)
+ {
+ errorcode = ERR72;
+ goto FAILED;
+ }
+ *parsed_pattern++ = (uint32_t)calloutlength;
+ offset = (PCRE2_SIZE)(startptr - cb->start_pattern);
+ PUTOFFSET(offset, parsed_pattern);
+ }
+
+ /* Handle a callout with an optional numerical argument, which must be
+ less than or equal to 255. A missing argument gives 0. */
+
+ else
+ {
+ int n = 0;
+ *parsed_pattern = META_CALLOUT_NUMBER; /* Numerical callout */
+ parsed_pattern += 3; /* Skip pattern info */
+ while (ptr < ptrend && IS_DIGIT(*ptr))
+ {
+ n = n * 10 + *ptr++ - CHAR_0;
+ if (n > 255)
+ {
+ errorcode = ERR38;
+ goto FAILED;
+ }
+ }
+ *parsed_pattern++ = n;
+ }
+
+ /* Both formats must have a closing parenthesis */
+
+ if (ptr >= ptrend || *ptr != CHAR_RIGHT_PARENTHESIS)
+ {
+ errorcode = ERR39;
+ goto FAILED;
+ }
+ ptr++;
+
+ /* Remember the offset to the next item in the pattern, and set a default
+ length. This should get updated after the next item is read. */
+
+ previous_callout[1] = (uint32_t)(ptr - cb->start_pattern);
+ previous_callout[2] = 0;
+ break; /* End callout */
+
+
+ /* ---- Conditional group ---- */
+
+ /* A condition can be an assertion, a number (referring to a numbered
+ group's having been set), a name (referring to a named group), or 'R',
+ referring to overall recursion. R<digits> and R&name are also permitted
+ for recursion state tests. Numbers may be preceded by + or - to specify a
+ relative group number.
+
+ There are several syntaxes for testing a named group: (?(name)) is used
+ by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
+
+ There are two unfortunate ambiguities. 'R' can be the recursive thing or
+ the name 'R' (and similarly for 'R' followed by digits). 'DEFINE' can be
+ the Perl DEFINE feature or the Python named test. We look for a name
+ first; if not found, we try the other case.
+
+ For compatibility with auto-callouts, we allow a callout to be specified
+ before a condition that is an assertion. */
+
+ case CHAR_LEFT_PARENTHESIS:
+ if (++ptr >= ptrend) goto UNCLOSED_PARENTHESIS;
+ nest_depth++;
+
+ /* If the next character is ? there must be an assertion next (optionally
+ preceded by a callout). We do not check this here, but instead we set
+ expect_cond_assert to 2. If this is still greater than zero (callouts
+ decrement it) when the next assertion is read, it will be marked as a
+ condition that must not be repeated. A value greater than zero also
+ causes checking that an assertion (possibly with callout) follows. */
+
+ if (*ptr == CHAR_QUESTION_MARK)
+ {
+ *parsed_pattern++ = META_COND_ASSERT;
+ ptr--; /* Pull pointer back to the opening parenthesis. */
+ expect_cond_assert = 2;
+ break; /* End of conditional */
+ }
+
+ /* Handle (?([+-]number)... */
+
+ if (read_number(&ptr, ptrend, cb->bracount, MAX_GROUP_NUMBER, ERR61, &i,
+ &errorcode))
+ {
+ if (i <= 0)
+ {
+ errorcode = ERR15;
+ goto FAILED;
+ }
+ *parsed_pattern++ = META_COND_NUMBER;
+ offset = (PCRE2_SIZE)(ptr - cb->start_pattern - 2);
+ PUTOFFSET(offset, parsed_pattern);
+ *parsed_pattern++ = i;
+ }
+ else if (errorcode != 0) goto FAILED; /* Number too big */
+
+ /* No number found. Handle the special case (?(VERSION[>]=n.m)... */
+
+ else if (ptrend - ptr >= 10 &&
+ PRIV(strncmp_c8)(ptr, STRING_VERSION, 7) == 0 &&
+ ptr[7] != CHAR_RIGHT_PARENTHESIS)
+ {
+ uint32_t ge = 0;
+ int major = 0;
+ int minor = 0;
+
+ ptr += 7;
+ if (*ptr == CHAR_GREATER_THAN_SIGN)
+ {
+ ge = 1;
+ ptr++;
+ }
+
+ /* NOTE: cannot write IS_DIGIT(*(++ptr)) here because IS_DIGIT
+ references its argument twice. */
+
+ if (*ptr != CHAR_EQUALS_SIGN || (ptr++, !IS_DIGIT(*ptr)))
+ goto BAD_VERSION_CONDITION;
+
+ if (!read_number(&ptr, ptrend, -1, 1000, ERR79, &major, &errorcode))
+ goto FAILED;
+
+ if (ptr >= ptrend) goto BAD_VERSION_CONDITION;
+ if (*ptr == CHAR_DOT)
+ {
+ if (++ptr >= ptrend || !IS_DIGIT(*ptr)) goto BAD_VERSION_CONDITION;
+ minor = (*ptr++ - CHAR_0) * 10;
+ if (IS_DIGIT(*ptr)) minor += *ptr++ - CHAR_0;
+ if (ptr >= ptrend || *ptr != CHAR_RIGHT_PARENTHESIS)
+ goto BAD_VERSION_CONDITION;
+ }
+
+ *parsed_pattern++ = META_COND_VERSION;
+ *parsed_pattern++ = ge;
+ *parsed_pattern++ = major;
+ *parsed_pattern++ = minor;
+ }
+
+ /* All the remaining cases now require us to read a name. We cannot at
+ this stage distinguish ambiguous cases such as (?(R12) which might be a
+ recursion test by number or a name, because the named groups have not yet
+ all been identified. Those cases are treated as names, but given a
+ different META code. */
+
+ else
+ {
+ BOOL was_r_ampersand = FALSE;
+
+ if (*ptr == CHAR_R && ptrend - ptr > 1 && ptr[1] == CHAR_AMPERSAND)
+ {
+ terminator = CHAR_RIGHT_PARENTHESIS;
+ was_r_ampersand = TRUE;
+ ptr++;
+ }
+ else if (*ptr == CHAR_LESS_THAN_SIGN)
+ terminator = CHAR_GREATER_THAN_SIGN;
+ else if (*ptr == CHAR_APOSTROPHE)
+ terminator = CHAR_APOSTROPHE;
+ else
+ {
+ terminator = CHAR_RIGHT_PARENTHESIS;
+ ptr--; /* Point to char before name */
+ }
+ if (!read_name(&ptr, ptrend, terminator, &offset, &name, &namelen,
+ &errorcode, cb)) goto FAILED;
+
+ /* Handle (?(R&name) */
+
+ if (was_r_ampersand)
+ {
+ *parsed_pattern = META_COND_RNAME;
+ ptr--; /* Back to closing parens */
+ }
+
+ /* Handle (?(name). If the name is "DEFINE" we identify it with a
+ special code. Likewise if the name consists of R followed only by
+ digits. Otherwise, handle it like a quoted name. */
+
+ else if (terminator == CHAR_RIGHT_PARENTHESIS)
+ {
+ if (namelen == 6 && PRIV(strncmp_c8)(name, STRING_DEFINE, 6) == 0)
+ *parsed_pattern = META_COND_DEFINE;
+ else
+ {
+ for (i = 1; i < (int)namelen; i++)
+ if (!IS_DIGIT(name[i])) break;
+ *parsed_pattern = (*name == CHAR_R && i >= (int)namelen)?
+ META_COND_RNUMBER : META_COND_NAME;
+ }
+ ptr--; /* Back to closing parens */
+ }
+
+ /* Handle (?('name') or (?(<name>) */
+
+ else *parsed_pattern = META_COND_NAME;
+
+ /* All these cases except DEFINE end with the name length and offset;
+ DEFINE just has an offset (for the "too many branches" error). */
+
+ if (*parsed_pattern++ != META_COND_DEFINE) *parsed_pattern++ = namelen;
+ PUTOFFSET(offset, parsed_pattern);
+ } /* End cases that read a name */
+
+ /* Check the closing parenthesis of the condition */
+
+ if (ptr >= ptrend || *ptr != CHAR_RIGHT_PARENTHESIS)
+ {
+ errorcode = ERR24;
+ goto FAILED;
+ }
+ ptr++;
+ break; /* End of condition processing */
+
+
+ /* ---- Atomic group ---- */
+
+ case CHAR_GREATER_THAN_SIGN:
+ *parsed_pattern++ = META_ATOMIC;
+ nest_depth++;
+ ptr++;
+ break;
+
+
+ /* ---- Lookahead assertions ---- */
+
+ case CHAR_EQUALS_SIGN:
+ *parsed_pattern++ = META_LOOKAHEAD;
+ ptr++;
+ goto POST_ASSERTION;
+
+ case CHAR_EXCLAMATION_MARK:
+ *parsed_pattern++ = META_LOOKAHEADNOT;
+ ptr++;
+ goto POST_ASSERTION;
+
+
+ /* ---- Lookbehind assertions ---- */
+
+ /* (?< followed by = or ! is a lookbehind assertion. Otherwise (?< is the
+ start of the name of a capturing group. */
+
+ case CHAR_LESS_THAN_SIGN:
+ if (ptrend - ptr <= 1 ||
+ (ptr[1] != CHAR_EQUALS_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK))
+ {
+ terminator = CHAR_GREATER_THAN_SIGN;
+ goto DEFINE_NAME;
+ }
+ *parsed_pattern++ = (ptr[1] == CHAR_EQUALS_SIGN)?
+ META_LOOKBEHIND : META_LOOKBEHINDNOT;
+ *has_lookbehind = TRUE;
+ offset = (PCRE2_SIZE)(ptr - cb->start_pattern - 2);
+ PUTOFFSET(offset, parsed_pattern);
+ ptr += 2;
+ /* Fall through */
+
+ /* If the previous item was a condition starting (?(? an assertion,
+ optionally preceded by a callout, is expected. This is checked later on,
+ during actual compilation. However we need to identify this kind of
+ assertion in this pass because it must not be qualified. The value of
+ expect_cond_assert is set to 2 after (?(? is processed. We decrement it
+ for a callout - still leaving a positive value that identifies the
+ assertion. Multiple callouts or any other items will make it zero or
+ less, which doesn't matter because they will cause an error later. */
+
+ POST_ASSERTION:
+ nest_depth++;
+ if (prev_expect_cond_assert > 0)
+ {
+ if (top_nest == NULL) top_nest = (nest_save *)(cb->start_workspace);
+ else if (++top_nest >= end_nests)
+ {
+ errorcode = ERR84;
+ goto FAILED;
+ }
+ top_nest->nest_depth = nest_depth;
+ top_nest->flags = NSF_CONDASSERT;
+ top_nest->options = options & PARSE_TRACKED_OPTIONS;
+ }
+ break;
+
+
+ /* ---- Define a named group ---- */
+
+ /* A named group may be defined as (?'name') or (?<name>). In the latter
+ case we jump to DEFINE_NAME from the disambiguation of (?< above with the
+ terminator set to '>'. */
+
+ case CHAR_APOSTROPHE:
+ terminator = CHAR_APOSTROPHE; /* Terminator */
+
+ DEFINE_NAME:
+ if (!read_name(&ptr, ptrend, terminator, &offset, &name, &namelen,
+ &errorcode, cb)) goto FAILED;
+
+ /* We have a name for this capturing group. It is also assigned a number,
+ which is its primary means of identification. */
+
+ cb->bracount++;
+ *parsed_pattern++ = META_CAPTURE | cb->bracount;
+ nest_depth++;
+
+ /* Check not too many names */
+
+ if (cb->names_found >= MAX_NAME_COUNT)
+ {
+ errorcode = ERR49;
+ goto FAILED;
+ }
+
+ /* Adjust the entry size to accommodate the longest name found. */
+
+ if (namelen + IMM2_SIZE + 1 > cb->name_entry_size)
+ cb->name_entry_size = (uint16_t)(namelen + IMM2_SIZE + 1);
+
+ /* Scan the list to check for duplicates. For duplicate names, if the
+ number is the same, break the loop, which causes the name to be
+ discarded; otherwise, if DUPNAMES is not set, give an error.
+ If it is set, allow the name with a different number, but continue
+ scanning in case this is a duplicate with the same number. For
+ non-duplicate names, give an error if the number is duplicated. */
+
+ isdupname = FALSE;
+ ng = cb->named_groups;
+ for (i = 0; i < cb->names_found; i++, ng++)
+ {
+ if (namelen == ng->length &&
+ PRIV(strncmp)(name, ng->name, (PCRE2_SIZE)namelen) == 0)
+ {
+ if (ng->number == cb->bracount) break;
+ if ((options & PCRE2_DUPNAMES) == 0)
+ {
+ errorcode = ERR43;
+ goto FAILED;
+ }
+ isdupname = ng->isdup = TRUE; /* Mark as a duplicate */
+ cb->dupnames = TRUE; /* Duplicate names exist */
+ }
+ else if (ng->number == cb->bracount)
+ {
+ errorcode = ERR65;
+ goto FAILED;
+ }
+ }
+
+ if (i < cb->names_found) break; /* Ignore duplicate with same number */
+
+ /* Increase the list size if necessary */
+
+ if (cb->names_found >= cb->named_group_list_size)
+ {
+ uint32_t newsize = cb->named_group_list_size * 2;
+ named_group *newspace =
+ cb->cx->memctl.malloc(newsize * sizeof(named_group),
+ cb->cx->memctl.memory_data);
+ if (newspace == NULL)
+ {
+ errorcode = ERR21;
+ goto FAILED;
+ }
+
+ memcpy(newspace, cb->named_groups,
+ cb->named_group_list_size * sizeof(named_group));
+ if (cb->named_group_list_size > NAMED_GROUP_LIST_SIZE)
+ cb->cx->memctl.free((void *)cb->named_groups,
+ cb->cx->memctl.memory_data);
+ cb->named_groups = newspace;
+ cb->named_group_list_size = newsize;
+ }
+
+ /* Add this name to the list */
+
+ cb->named_groups[cb->names_found].name = name;
+ cb->named_groups[cb->names_found].length = (uint16_t)namelen;
+ cb->named_groups[cb->names_found].number = cb->bracount;
+ cb->named_groups[cb->names_found].isdup = (uint16_t)isdupname;
+ cb->names_found++;
+ break;
+ } /* End of (? switch */
+ break; /* End of ( handling */
+
+
+ /* ---- Branch terminators ---- */
+
+ /* Alternation: reset the capture count if we are in a (?| group. */
+
+ case CHAR_VERTICAL_LINE:
+ if (top_nest != NULL && top_nest->nest_depth == nest_depth &&
+ (top_nest->flags & NSF_RESET) != 0)
+ {
+ if (cb->bracount > top_nest->max_group)
+ top_nest->max_group = (uint16_t)cb->bracount;
+ cb->bracount = top_nest->reset_group;
+ }
+ *parsed_pattern++ = META_ALT;
+ break;
+
+ /* End of group; reset the capture count to the maximum if we are in a (?|
+ group and/or reset the options that are tracked during parsing. Disallow
+ quantifier for a condition that is an assertion. */
+
+ case CHAR_RIGHT_PARENTHESIS:
+ okquantifier = TRUE;
+ if (top_nest != NULL && top_nest->nest_depth == nest_depth)
+ {
+ options = (options & ~PARSE_TRACKED_OPTIONS) | top_nest->options;
+ if ((top_nest->flags & NSF_RESET) != 0 &&
+ top_nest->max_group > cb->bracount)
+ cb->bracount = top_nest->max_group;
+ if ((top_nest->flags & NSF_CONDASSERT) != 0)
+ okquantifier = FALSE;
+ if (top_nest == (nest_save *)(cb->start_workspace)) top_nest = NULL;
+ else top_nest--;
+ }
+ if (nest_depth == 0) /* Unmatched closing parenthesis */
+ {
+ errorcode = ERR22;
+ goto FAILED_BACK;
+ }
+ nest_depth--;
+ *parsed_pattern++ = META_KET;
+ break;
+ } /* End of switch on pattern character */
+ } /* End of main character scan loop */
+
+/* End of pattern reached. Check for missing ) at the end of a verb name. */
+
+if (inverbname && ptr >= ptrend)
+ {
+ errorcode = ERR60;
+ goto FAILED;
+ }
+
+/* Manage callout for the final item */
+
+PARSED_END:
+parsed_pattern = manage_callouts(ptr, &previous_callout, auto_callout,
+ parsed_pattern, cb);
+
+/* Insert trailing items for word and line matching (features provided for the
+benefit of pcre2grep). */
+
+if ((cb->cx->extra_options & PCRE2_EXTRA_MATCH_LINE) != 0)
+ {
+ *parsed_pattern++ = META_KET;
+ *parsed_pattern++ = META_DOLLAR;
+ }
+else if ((cb->cx->extra_options & PCRE2_EXTRA_MATCH_WORD) != 0)
+ {
+ *parsed_pattern++ = META_KET;
+ *parsed_pattern++ = META_ESCAPE + ESC_b;
+ }
+
+/* Terminate the parsed pattern, then return success if all groups are closed.
+Otherwise we have unclosed parentheses. */
+
+if (parsed_pattern >= parsed_pattern_end)
+ {
+ errorcode = ERR63; /* Internal error (parsed pattern overflow) */
+ goto FAILED;
+ }
+
+*parsed_pattern = META_END;
+if (nest_depth == 0) return 0;
+
+UNCLOSED_PARENTHESIS:
+errorcode = ERR14;
+
+/* Come here for all failures. */
+
+FAILED:
+cb->erroroffset = (PCRE2_SIZE)(ptr - cb->start_pattern);
+return errorcode;
+
+/* Some errors need to indicate the previous character. */
+
+FAILED_BACK:
+ptr--;
+goto FAILED;
+
+/* This failure happens several times. */
+
+BAD_VERSION_CONDITION:
+errorcode = ERR79;
+goto FAILED;
+}
+
+
+
+/*************************************************
+* Find first significant opcode *
+*************************************************/
+
+/* This is called by several functions that scan a compiled expression looking
+for a fixed first character, or an anchoring opcode etc. It skips over things
+that do not influence this. For some calls, it makes sense to skip negative
+forward and all backward assertions, and also the \b assertion; for others it
+does not.
+
+Arguments:
+ code pointer to the start of the group
+ skipassert TRUE if certain assertions are to be skipped
+
+Returns: pointer to the first significant opcode
+*/
+
+static const PCRE2_UCHAR*
+first_significant_code(PCRE2_SPTR code, BOOL skipassert)
+{
+for (;;)
+ {
+ switch ((int)*code)
+ {
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ if (!skipassert) return code;
+ do code += GET(code, 1); while (*code == OP_ALT);
+ code += PRIV(OP_lengths)[*code];
+ break;
+
+ case OP_WORD_BOUNDARY:
+ case OP_NOT_WORD_BOUNDARY:
+ if (!skipassert) return code;
+ /* Fall through */
+
+ case OP_CALLOUT:
+ case OP_CREF:
+ case OP_DNCREF:
+ case OP_RREF:
+ case OP_DNRREF:
+ case OP_FALSE:
+ case OP_TRUE:
+ code += PRIV(OP_lengths)[*code];
+ break;
+
+ case OP_CALLOUT_STR:
+ code += GET(code, 1 + 2*LINK_SIZE);
+ break;
+
+ case OP_SKIPZERO:
+ code += 2 + GET(code, 2) + LINK_SIZE;
+ break;
+
+ case OP_COND:
+ case OP_SCOND:
+ if (code[1+LINK_SIZE] != OP_FALSE || /* Not DEFINE */
+ code[GET(code, 1)] != OP_KET) /* More than one branch */
+ return code;
+ code += GET(code, 1) + 1 + LINK_SIZE;
+ break;
+
+ default:
+ return code;
+ }
+ }
+/* Control never reaches here */
+}
+
+
+
+#ifdef SUPPORT_UNICODE
+/*************************************************
+* Get othercase range *
+*************************************************/
+
+/* This function is passed the start and end of a class range in UCP mode. It
+searches up the characters, looking for ranges of characters in the "other"
+case. Each call returns the next one, updating the start address. A character
+with multiple other cases is returned on its own with a special return value.
+
+Arguments:
+ cptr points to starting character value; updated
+ d end value
+ ocptr where to put start of othercase range
+ odptr where to put end of othercase range
+
+Yield: -1 when no more
+ 0 when a range is returned
+ >0 the CASESET offset for char with multiple other cases
+ in this case, ocptr contains the original
+*/
+
+static int
+get_othercase_range(uint32_t *cptr, uint32_t d, uint32_t *ocptr,
+ uint32_t *odptr)
+{
+uint32_t c, othercase, next;
+unsigned int co;
+
+/* Find the first character that has an other case. If it has multiple other
+cases, return its case offset value. */
+
+for (c = *cptr; c <= d; c++)
+ {
+ if ((co = UCD_CASESET(c)) != 0)
+ {
+ *ocptr = c++; /* Character that has the set */
+ *cptr = c; /* Rest of input range */
+ return (int)co;
+ }
+ if ((othercase = UCD_OTHERCASE(c)) != c) break;
+ }
+
+if (c > d) return -1; /* Reached end of range */
+
+/* Found a character that has a single other case. Search for the end of the
+range, which is either the end of the input range, or a character that has zero
+or more than one other cases. */
+
+*ocptr = othercase;
+next = othercase + 1;
+
+for (++c; c <= d; c++)
+ {
+ if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break;
+ next++;
+ }
+
+*odptr = next - 1; /* End of othercase range */
+*cptr = c; /* Rest of input range */
+return 0;
+}
+#endif /* SUPPORT_UNICODE */
+
+
+
+/*************************************************
+* Add a character or range to a class (internal) *
+*************************************************/
+
+/* This function packages up the logic of adding a character or range of
+characters to a class. The character values in the arguments will be within the
+valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
+called only from within the "add to class" group of functions, some of which
+are recursive and mutually recursive. The external entry point is
+add_to_class().
+
+Arguments:
+ classbits the bit map for characters < 256
+ uchardptr points to the pointer for extra data
+ options the options word
+ cb compile data
+ start start of range character
+ end end of range character
+
+Returns: the number of < 256 characters added
+ the pointer to extra data is updated
+*/
+
+static unsigned int
+add_to_class_internal(uint8_t *classbits, PCRE2_UCHAR **uchardptr,
+ uint32_t options, compile_block *cb, uint32_t start, uint32_t end)
+{
+uint32_t c;
+uint32_t classbits_end = (end <= 0xff ? end : 0xff);
+unsigned int n8 = 0;
+
+/* If caseless matching is required, scan the range and process alternate
+cases. In Unicode, there are 8-bit characters that have alternate cases that
+are greater than 255 and vice-versa. Sometimes we can just extend the original
+range. */
+
+if ((options & PCRE2_CASELESS) != 0)
+ {
+#ifdef SUPPORT_UNICODE
+ if ((options & PCRE2_UTF) != 0)
+ {
+ int rc;
+ uint32_t oc, od;
+
+ options &= ~PCRE2_CASELESS; /* Remove for recursive calls */
+ c = start;
+
+ while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
+ {
+ /* Handle a single character that has more than one other case. */
+
+ if (rc > 0) n8 += add_list_to_class_internal(classbits, uchardptr, options, cb,
+ PRIV(ucd_caseless_sets) + rc, oc);
+
+ /* Do nothing if the other case range is within the original range. */
+
+ else if (oc >= cb->class_range_start && od <= cb->class_range_end) continue;
+
+ /* Extend the original range if there is overlap, noting that if oc < c, we
+ can't have od > end because a subrange is always shorter than the basic
+ range. Otherwise, use a recursive call to add the additional range. */
+
+ else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
+ else if (od > end && oc <= end + 1)
+ {
+ end = od; /* Extend upwards */
+ if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff);
+ }
+ else n8 += add_to_class_internal(classbits, uchardptr, options, cb, oc, od);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+
+ for (c = start; c <= classbits_end; c++)
+ {
+ SETBIT(classbits, cb->fcc[c]);
+ n8++;
+ }
+ }
+
+/* Now handle the originally supplied range. Adjust the final value according
+to the bit length - this means that the same lists of (e.g.) horizontal spaces
+can be used in all cases. */
+
+if ((options & PCRE2_UTF) == 0 && end > MAX_NON_UTF_CHAR)
+ end = MAX_NON_UTF_CHAR;
+
+if (start > cb->class_range_start && end < cb->class_range_end) return n8;
+
+/* Use the bitmap for characters < 256. Otherwise use extra data.*/
+
+for (c = start; c <= classbits_end; c++)
+ {
+ /* Regardless of start, c will always be <= 255. */
+ SETBIT(classbits, c);
+ n8++;
+ }
+
+#ifdef SUPPORT_WIDE_CHARS
+if (start <= 0xff) start = 0xff + 1;
+
+if (end >= start)
+ {
+ PCRE2_UCHAR *uchardata = *uchardptr;
+
+#ifdef SUPPORT_UNICODE
+ if ((options & PCRE2_UTF) != 0)
+ {
+ if (start < end)
+ {
+ *uchardata++ = XCL_RANGE;
+ uchardata += PRIV(ord2utf)(start, uchardata);
+ uchardata += PRIV(ord2utf)(end, uchardata);
+ }
+ else if (start == end)
+ {
+ *uchardata++ = XCL_SINGLE;
+ uchardata += PRIV(ord2utf)(start, uchardata);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Without UTF support, character values are constrained by the bit length,
+ and can only be > 256 for 16-bit and 32-bit libraries. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ {}
+#else
+ if (start < end)
+ {
+ *uchardata++ = XCL_RANGE;
+ *uchardata++ = start;
+ *uchardata++ = end;
+ }
+ else if (start == end)
+ {
+ *uchardata++ = XCL_SINGLE;
+ *uchardata++ = start;
+ }
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ *uchardptr = uchardata; /* Updata extra data pointer */
+ }
+#else /* SUPPORT_WIDE_CHARS */
+ (void)uchardptr; /* Avoid compiler warning */
+#endif /* SUPPORT_WIDE_CHARS */
+
+return n8; /* Number of 8-bit characters */
+}
+
+
+
+#ifdef SUPPORT_UNICODE
+/*************************************************
+* Add a list of characters to a class (internal) *
+*************************************************/
+
+/* This function is used for adding a list of case-equivalent characters to a
+class when in UTF mode. This function is called only from within
+add_to_class_internal(), with which it is mutually recursive.
+
+Arguments:
+ classbits the bit map for characters < 256
+ uchardptr points to the pointer for extra data
+ options the options word
+ cb contains pointers to tables etc.
+ p points to row of 32-bit values, terminated by NOTACHAR
+ except character to omit; this is used when adding lists of
+ case-equivalent characters to avoid including the one we
+ already know about
+
+Returns: the number of < 256 characters added
+ the pointer to extra data is updated
+*/
+
+static unsigned int
+add_list_to_class_internal(uint8_t *classbits, PCRE2_UCHAR **uchardptr,
+ uint32_t options, compile_block *cb, const uint32_t *p, unsigned int except)
+{
+unsigned int n8 = 0;
+while (p[0] < NOTACHAR)
+ {
+ unsigned int n = 0;
+ if (p[0] != except)
+ {
+ while(p[n+1] == p[0] + n + 1) n++;
+ n8 += add_to_class_internal(classbits, uchardptr, options, cb, p[0], p[n]);
+ }
+ p += n + 1;
+ }
+return n8;
+}
+#endif
+
+
+
+/*************************************************
+* External entry point for add range to class *
+*************************************************/
+
+/* This function sets the overall range so that the internal functions can try
+to avoid duplication when handling case-independence.
+
+Arguments:
+ classbits the bit map for characters < 256
+ uchardptr points to the pointer for extra data
+ options the options word
+ cb compile data
+ start start of range character
+ end end of range character
+
+Returns: the number of < 256 characters added
+ the pointer to extra data is updated
+*/
+
+static unsigned int
+add_to_class(uint8_t *classbits, PCRE2_UCHAR **uchardptr, uint32_t options,
+ compile_block *cb, uint32_t start, uint32_t end)
+{
+cb->class_range_start = start;
+cb->class_range_end = end;
+return add_to_class_internal(classbits, uchardptr, options, cb, start, end);
+}
+
+
+/*************************************************
+* External entry point for add list to class *
+*************************************************/
+
+/* This function is used for adding a list of horizontal or vertical whitespace
+characters to a class. The list must be in order so that ranges of characters
+can be detected and handled appropriately. This function sets the overall range
+so that the internal functions can try to avoid duplication when handling
+case-independence.
+
+Arguments:
+ classbits the bit map for characters < 256
+ uchardptr points to the pointer for extra data
+ options the options word
+ cb contains pointers to tables etc.
+ p points to row of 32-bit values, terminated by NOTACHAR
+ except character to omit; this is used when adding lists of
+ case-equivalent characters to avoid including the one we
+ already know about
+
+Returns: the number of < 256 characters added
+ the pointer to extra data is updated
+*/
+
+static unsigned int
+add_list_to_class(uint8_t *classbits, PCRE2_UCHAR **uchardptr, uint32_t options,
+ compile_block *cb, const uint32_t *p, unsigned int except)
+{
+unsigned int n8 = 0;
+while (p[0] < NOTACHAR)
+ {
+ unsigned int n = 0;
+ if (p[0] != except)
+ {
+ while(p[n+1] == p[0] + n + 1) n++;
+ cb->class_range_start = p[0];
+ cb->class_range_end = p[n];
+ n8 += add_to_class_internal(classbits, uchardptr, options, cb, p[0], p[n]);
+ }
+ p += n + 1;
+ }
+return n8;
+}
+
+
+
+/*************************************************
+* Add characters not in a list to a class *
+*************************************************/
+
+/* This function is used for adding the complement of a list of horizontal or
+vertical whitespace to a class. The list must be in order.
+
+Arguments:
+ classbits the bit map for characters < 256
+ uchardptr points to the pointer for extra data
+ options the options word
+ cb contains pointers to tables etc.
+ p points to row of 32-bit values, terminated by NOTACHAR
+
+Returns: the number of < 256 characters added
+ the pointer to extra data is updated
+*/
+
+static unsigned int
+add_not_list_to_class(uint8_t *classbits, PCRE2_UCHAR **uchardptr,
+ uint32_t options, compile_block *cb, const uint32_t *p)
+{
+BOOL utf = (options & PCRE2_UTF) != 0;
+unsigned int n8 = 0;
+if (p[0] > 0)
+ n8 += add_to_class(classbits, uchardptr, options, cb, 0, p[0] - 1);
+while (p[0] < NOTACHAR)
+ {
+ while (p[1] == p[0] + 1) p++;
+ n8 += add_to_class(classbits, uchardptr, options, cb, p[0] + 1,
+ (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
+ p++;
+ }
+return n8;
+}
+
+
+
+/*************************************************
+* Find details of duplicate group names *
+*************************************************/
+
+/* This is called from compile_branch() when it needs to know the index and
+count of duplicates in the names table when processing named backreferences,
+either directly, or as conditions.
+
+Arguments:
+ name points to the name
+ length the length of the name
+ indexptr where to put the index
+ countptr where to put the count of duplicates
+ errorcodeptr where to put an error code
+ cb the compile block
+
+Returns: TRUE if OK, FALSE if not, error code set
+*/
+
+static BOOL
+find_dupname_details(PCRE2_SPTR name, uint32_t length, int *indexptr,
+ int *countptr, int *errorcodeptr, compile_block *cb)
+{
+uint32_t i, groupnumber;
+int count;
+PCRE2_UCHAR *slot = cb->name_table;
+
+/* Find the first entry in the table */
+
+for (i = 0; i < cb->names_found; i++)
+ {
+ if (PRIV(strncmp)(name, slot+IMM2_SIZE, length) == 0 &&
+ slot[IMM2_SIZE+length] == 0) break;
+ slot += cb->name_entry_size;
+ }
+
+/* This should not occur, because this function is called only when we know we
+have duplicate names. Give an internal error. */
+
+if (i >= cb->names_found)
+ {
+ *errorcodeptr = ERR53;
+ cb->erroroffset = name - cb->start_pattern;
+ return FALSE;
+ }
+
+/* Record the index and then see how many duplicates there are, updating the
+backref map and maximum back reference as we do. */
+
+*indexptr = i;
+count = 0;
+
+for (;;)
+ {
+ count++;
+ groupnumber = GET2(slot,0);
+ cb->backref_map |= (groupnumber < 32)? (1u << groupnumber) : 1;
+ if (groupnumber > cb->top_backref) cb->top_backref = groupnumber;
+ if (++i >= cb->names_found) break;
+ slot += cb->name_entry_size;
+ if (PRIV(strncmp)(name, slot+IMM2_SIZE, length) != 0 ||
+ (slot+IMM2_SIZE)[length] != 0) break;
+ }
+
+*countptr = count;
+return TRUE;
+}
+
+
+
+/*************************************************
+* Compile one branch *
+*************************************************/
+
+/* Scan the parsed pattern, compiling it into the a vector of PCRE2_UCHAR. If
+the options are changed during the branch, the pointer is used to change the
+external options bits. This function is used during the pre-compile phase when
+we are trying to find out the amount of memory needed, as well as during the
+real compile phase. The value of lengthptr distinguishes the two phases.
+
+Arguments:
+ optionsptr pointer to the option bits
+ codeptr points to the pointer to the current code point
+ pptrptr points to the current parsed pattern pointer
+ errorcodeptr points to error code variable
+ firstcuptr place to put the first required code unit
+ firstcuflagsptr place to put the first code unit flags, or a negative number
+ reqcuptr place to put the last required code unit
+ reqcuflagsptr place to put the last required code unit flags, or a negative number
+ bcptr points to current branch chain
+ cb contains pointers to tables etc.
+ lengthptr NULL during the real compile phase
+ points to length accumulator during pre-compile phase
+
+Returns: 0 There's been an error, *errorcodeptr is non-zero
+ +1 Success, this branch must match at least one character
+ -1 Success, this branch may match an empty string
+*/
+
+static int
+compile_branch(uint32_t *optionsptr, PCRE2_UCHAR **codeptr, uint32_t **pptrptr,
+ int *errorcodeptr, uint32_t *firstcuptr, int32_t *firstcuflagsptr,
+ uint32_t *reqcuptr, int32_t *reqcuflagsptr, branch_chain *bcptr,
+ compile_block *cb, PCRE2_SIZE *lengthptr)
+{
+int bravalue = 0;
+int okreturn = -1;
+int group_return = 0;
+uint32_t repeat_min = 0, repeat_max = 0; /* To please picky compilers */
+uint32_t greedy_default, greedy_non_default;
+uint32_t repeat_type, op_type;
+uint32_t options = *optionsptr; /* May change dynamically */
+uint32_t firstcu, reqcu;
+uint32_t zeroreqcu, zerofirstcu;
+uint32_t escape;
+uint32_t *pptr = *pptrptr;
+uint32_t meta, meta_arg;
+int32_t firstcuflags, reqcuflags;
+int32_t zeroreqcuflags, zerofirstcuflags;
+int32_t req_caseopt, reqvary, tempreqvary;
+PCRE2_SIZE offset = 0;
+PCRE2_SIZE length_prevgroup = 0;
+PCRE2_UCHAR *code = *codeptr;
+PCRE2_UCHAR *last_code = code;
+PCRE2_UCHAR *orig_code = code;
+PCRE2_UCHAR *tempcode;
+PCRE2_UCHAR *previous = NULL;
+PCRE2_UCHAR op_previous;
+BOOL groupsetfirstcu = FALSE;
+BOOL matched_char = FALSE;
+BOOL previous_matched_char = FALSE;
+const uint8_t *cbits = cb->cbits;
+uint8_t classbits[32];
+
+/* We can fish out the UTF setting once and for all into a BOOL, but we must
+not do this for other options (e.g. PCRE2_EXTENDED) because they may change
+dynamically as we process the pattern. */
+
+#ifdef SUPPORT_UNICODE
+BOOL utf = (options & PCRE2_UTF) != 0;
+#else /* No UTF support */
+BOOL utf = FALSE;
+#endif
+
+/* Helper variables for OP_XCLASS opcode (for characters > 255). We define
+class_uchardata always so that it can be passed to add_to_class() always,
+though it will not be used in non-UTF 8-bit cases. This avoids having to supply
+alternative calls for the different cases. */
+
+PCRE2_UCHAR *class_uchardata;
+#ifdef SUPPORT_WIDE_CHARS
+BOOL xclass;
+PCRE2_UCHAR *class_uchardata_base;
+#endif
+
+/* Set up the default and non-default settings for greediness */
+
+greedy_default = ((options & PCRE2_UNGREEDY) != 0);
+greedy_non_default = greedy_default ^ 1;
+
+/* Initialize no first unit, no required unit. REQ_UNSET means "no char
+matching encountered yet". It gets changed to REQ_NONE if we hit something that
+matches a non-fixed first unit; reqcu just remains unset if we never find one.
+
+When we hit a repeat whose minimum is zero, we may have to adjust these values
+to take the zero repeat into account. This is implemented by setting them to
+zerofirstcu and zeroreqcu when such a repeat is encountered. The individual
+item types that can be repeated set these backoff variables appropriately. */
+
+firstcu = reqcu = zerofirstcu = zeroreqcu = 0;
+firstcuflags = reqcuflags = zerofirstcuflags = zeroreqcuflags = REQ_UNSET;
+
+/* The variable req_caseopt contains either the REQ_CASELESS value or zero,
+according to the current setting of the caseless flag. The REQ_CASELESS value
+leaves the lower 28 bit empty. It is added into the firstcu or reqcu variables
+to record the case status of the value. This is used only for ASCII characters.
+*/
+
+req_caseopt = ((options & PCRE2_CASELESS) != 0)? REQ_CASELESS:0;
+
+/* Switch on next META item until the end of the branch */
+
+for (;; pptr++)
+ {
+#ifdef SUPPORT_WIDE_CHARS
+ BOOL xclass_has_prop;
+#endif
+ BOOL negate_class;
+ BOOL should_flip_negation;
+ BOOL match_all_or_no_wide_chars;
+ BOOL possessive_quantifier;
+ BOOL note_group_empty;
+ int class_has_8bitchar;
+ int i;
+ uint32_t mclength;
+ uint32_t skipunits;
+ uint32_t subreqcu, subfirstcu;
+ uint32_t groupnumber;
+ uint32_t verbarglen, verbculen;
+ int32_t subreqcuflags, subfirstcuflags; /* Must be signed */
+ open_capitem *oc;
+ PCRE2_UCHAR mcbuffer[8];
+
+ /* Get next META item in the pattern and its potential argument. */
+
+ meta = META_CODE(*pptr);
+ meta_arg = META_DATA(*pptr);
+
+ /* If we are in the pre-compile phase, accumulate the length used for the
+ previous cycle of this loop, unless the next item is a quantifier. */
+
+ if (lengthptr != NULL)
+ {
+ if (code > cb->start_workspace + cb->workspace_size -
+ WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */
+ {
+ *errorcodeptr = (code >= cb->start_workspace + cb->workspace_size)?
+ ERR52 : ERR86;
+ return 0;
+ }
+
+ /* There is at least one situation where code goes backwards: this is the
+ case of a zero quantifier after a class (e.g. [ab]{0}). When the quantifier
+ is processed, the whole class is eliminated. However, it is created first,
+ so we have to allow memory for it. Therefore, don't ever reduce the length
+ at this point. */
+
+ if (code < last_code) code = last_code;
+
+ /* If the next thing is not a quantifier, we add the length of the previous
+ item into the total, and reset the code pointer to the start of the
+ workspace. Otherwise leave the previous item available to be quantified. */
+
+ if (meta < META_ASTERISK || meta > META_MINMAX_QUERY)
+ {
+ if (OFLOW_MAX - *lengthptr < (PCRE2_SIZE)(code - orig_code))
+ {
+ *errorcodeptr = ERR20; /* Integer overflow */
+ return 0;
+ }
+ *lengthptr += (PCRE2_SIZE)(code - orig_code);
+ if (*lengthptr > MAX_PATTERN_SIZE)
+ {
+ *errorcodeptr = ERR20; /* Pattern is too large */
+ return 0;
+ }
+ code = orig_code;
+ }
+
+ /* Remember where this code item starts so we can catch the "backwards"
+ case above next time round. */
+
+ last_code = code;
+ }
+
+ /* Process the next parsed pattern item. If it is not a quantifier, remember
+ where it starts so that it can be quantified when a quantifier follows.
+ Checking for the legality of quantifiers happens in parse_regex(), except for
+ a quantifier after an assertion that is a condition. */
+
+ if (meta < META_ASTERISK || meta > META_MINMAX_QUERY)
+ {
+ previous = code;
+ if (matched_char) okreturn = 1;
+ }
+
+ previous_matched_char = matched_char;
+ matched_char = FALSE;
+ note_group_empty = FALSE;
+ skipunits = 0; /* Default value for most subgroups */
+
+ switch(meta)
+ {
+ /* ===================================================================*/
+ /* The branch terminates at pattern end or | or ) */
+
+ case META_END:
+ case META_ALT:
+ case META_KET:
+ *firstcuptr = firstcu;
+ *firstcuflagsptr = firstcuflags;
+ *reqcuptr = reqcu;
+ *reqcuflagsptr = reqcuflags;
+ *codeptr = code;
+ *pptrptr = pptr;
+ return okreturn;
+
+
+ /* ===================================================================*/
+ /* Handle single-character metacharacters. In multiline mode, ^ disables
+ the setting of any following char as a first character. */
+
+ case META_CIRCUMFLEX:
+ if ((options & PCRE2_MULTILINE) != 0)
+ {
+ if (firstcuflags == REQ_UNSET)
+ zerofirstcuflags = firstcuflags = REQ_NONE;
+ *code++ = OP_CIRCM;
+ }
+ else *code++ = OP_CIRC;
+ break;
+
+ case META_DOLLAR:
+ *code++ = ((options & PCRE2_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
+ break;
+
+ /* There can never be a first char if '.' is first, whatever happens about
+ repeats. The value of reqcu doesn't change either. */
+
+ case META_DOT:
+ matched_char = TRUE;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+ *code++ = ((options & PCRE2_DOTALL) != 0)? OP_ALLANY: OP_ANY;
+ break;
+
+
+ /* ===================================================================*/
+ /* Empty character classes are allowed if PCRE2_ALLOW_EMPTY_CLASS is set.
+ Otherwise, an initial ']' is taken as a data character. When empty classes
+ are allowed, [] must always fail, so generate OP_FAIL, whereas [^] must
+ match any character, so generate OP_ALLANY. */
+
+ case META_CLASS_EMPTY:
+ case META_CLASS_EMPTY_NOT:
+ matched_char = TRUE;
+ *code++ = (meta == META_CLASS_EMPTY_NOT)? OP_ALLANY : OP_FAIL;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ break;
+
+
+ /* ===================================================================*/
+ /* Non-empty character class. If the included characters are all < 256, we
+ build a 32-byte bitmap of the permitted characters, except in the special
+ case where there is only one such character. For negated classes, we build
+ the map as usual, then invert it at the end. However, we use a different
+ opcode so that data characters > 255 can be handled correctly.
+
+ If the class contains characters outside the 0-255 range, a different
+ opcode is compiled. It may optionally have a bit map for characters < 256,
+ but those above are are explicitly listed afterwards. A flag code unit
+ tells whether the bitmap is present, and whether this is a negated class or
+ not. */
+
+ case META_CLASS_NOT:
+ case META_CLASS:
+ matched_char = TRUE;
+ negate_class = meta == META_CLASS_NOT;
+
+ /* We can optimize the case of a single character in a class by generating
+ OP_CHAR or OP_CHARI if it's positive, or OP_NOT or OP_NOTI if it's
+ negative. In the negative case there can be no first char if this item is
+ first, whatever repeat count may follow. In the case of reqcu, save the
+ previous value for reinstating. */
+
+ /* NOTE: at present this optimization is not effective if the only
+ character in a class in 32-bit, non-UCP mode has its top bit set. */
+
+ if (pptr[1] < META_END && pptr[2] == META_CLASS_END)
+ {
+#ifdef SUPPORT_UNICODE
+ uint32_t d;
+#endif
+ uint32_t c = pptr[1];
+
+ pptr += 2; /* Move on to class end */
+ if (meta == META_CLASS) /* A positive one-char class can be */
+ { /* handled as a normal literal character. */
+ meta = c; /* Set up the character */
+ goto NORMAL_CHAR_SET;
+ }
+
+ /* Handle a negative one-character class */
+
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+
+ /* For caseless UTF mode, check whether this character has more than
+ one other case. If so, generate a special OP_NOTPROP item instead of
+ OP_NOTI. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf && (options & PCRE2_CASELESS) != 0 &&
+ (d = UCD_CASESET(c)) != 0)
+ {
+ *code++ = OP_NOTPROP;
+ *code++ = PT_CLIST;
+ *code++ = d;
+ break; /* We are finished with this class */
+ }
+#endif
+ /* Char has only one other case, or UCP not available */
+
+ *code++ = ((options & PCRE2_CASELESS) != 0)? OP_NOTI: OP_NOT;
+ code += PUTCHAR(c, code);
+ break; /* We are finished with this class */
+ } /* End of 1-char optimization */
+
+ /* Handle character classes that contain more than just one literal
+ character. */
+
+ /* If a non-extended class contains a negative special such as \S, we need
+ to flip the negation flag at the end, so that support for characters > 255
+ works correctly (they are all included in the class). An extended class may
+ need to insert specific matching or non-matching code for wide characters.
+ */
+
+ should_flip_negation = match_all_or_no_wide_chars = FALSE;
+
+ /* Extended class (xclass) will be used when characters > 255
+ might match. */
+
+#ifdef SUPPORT_WIDE_CHARS
+ xclass = FALSE;
+ class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */
+ class_uchardata_base = class_uchardata; /* Save the start */
+#endif
+
+ /* For optimization purposes, we track some properties of the class:
+ class_has_8bitchar will be non-zero if the class contains at least one
+ character with a code point less than 256; xclass_has_prop will be TRUE if
+ Unicode property checks are present in the class. */
+
+ class_has_8bitchar = 0;
+#ifdef SUPPORT_WIDE_CHARS
+ xclass_has_prop = FALSE;
+#endif
+
+ /* Initialize the 256-bit (32-byte) bit map to all zeros. We build the map
+ in a temporary bit of memory, in case the class contains fewer than two
+ 8-bit characters because in that case the compiled code doesn't use the bit
+ map. */
+
+ memset(classbits, 0, 32 * sizeof(uint8_t));
+
+ /* Process items until META_CLASS_END is reached. */
+
+ while ((meta = *(++pptr)) != META_CLASS_END)
+ {
+ /* Handle POSIX classes such as [:alpha:] etc. */
+
+ if (meta == META_POSIX || meta == META_POSIX_NEG)
+ {
+ BOOL local_negate = (meta == META_POSIX_NEG);
+ int posix_class = *(++pptr);
+ int taboffset, tabopt;
+ uint8_t pbits[32];
+
+ should_flip_negation = local_negate; /* Note negative special */
+
+ /* If matching is caseless, upper and lower are converted to alpha.
+ This relies on the fact that the class table starts with alpha,
+ lower, upper as the first 3 entries. */
+
+ if ((options & PCRE2_CASELESS) != 0 && posix_class <= 2)
+ posix_class = 0;
+
+ /* When PCRE2_UCP is set, some of the POSIX classes are converted to
+ different escape sequences that use Unicode properties \p or \P.
+ Others that are not available via \p or \P have to generate
+ XCL_PROP/XCL_NOTPROP directly, which is done here. */
+
+#ifdef SUPPORT_UNICODE
+ if ((options & PCRE2_UCP) != 0) switch(posix_class)
+ {
+ case PC_GRAPH:
+ case PC_PRINT:
+ case PC_PUNCT:
+ *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP;
+ *class_uchardata++ = (PCRE2_UCHAR)
+ ((posix_class == PC_GRAPH)? PT_PXGRAPH :
+ (posix_class == PC_PRINT)? PT_PXPRINT : PT_PXPUNCT);
+ *class_uchardata++ = 0;
+ xclass_has_prop = TRUE;
+ goto CONTINUE_CLASS;
+
+ /* For the other POSIX classes (ascii, xdigit) we are going to
+ fall through to the non-UCP case and build a bit map for
+ characters with code points less than 256. However, if we are in
+ a negated POSIX class, characters with code points greater than
+ 255 must either all match or all not match, depending on whether
+ the whole class is not or is negated. For example, for
+ [[:^ascii:]... they must all match, whereas for [^[:^xdigit:]...
+ they must not.
+
+ In the special case where there are no xclass items, this is
+ automatically handled by the use of OP_CLASS or OP_NCLASS, but an
+ explicit range is needed for OP_XCLASS. Setting a flag here
+ causes the range to be generated later when it is known that
+ OP_XCLASS is required. In the 8-bit library this is relevant only in
+ utf mode, since no wide characters can exist otherwise. */
+
+ default:
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (utf)
+#endif
+ match_all_or_no_wide_chars |= local_negate;
+ break;
+ }
+#endif /* SUPPORT_UNICODE */
+
+ /* In the non-UCP case, or when UCP makes no difference, we build the
+ bit map for the POSIX class in a chunk of local store because we may
+ be adding and subtracting from it, and we don't want to subtract bits
+ that may be in the main map already. At the end we or the result into
+ the bit map that is being built. */
+
+ posix_class *= 3;
+
+ /* Copy in the first table (always present) */
+
+ memcpy(pbits, cbits + posix_class_maps[posix_class],
+ 32 * sizeof(uint8_t));
+
+ /* If there is a second table, add or remove it as required. */
+
+ taboffset = posix_class_maps[posix_class + 1];
+ tabopt = posix_class_maps[posix_class + 2];
+
+ if (taboffset >= 0)
+ {
+ if (tabopt >= 0)
+ for (i = 0; i < 32; i++) pbits[i] |= cbits[(int)i + taboffset];
+ else
+ for (i = 0; i < 32; i++) pbits[i] &= ~cbits[(int)i + taboffset];
+ }
+
+ /* Now see if we need to remove any special characters. An option
+ value of 1 removes vertical space and 2 removes underscore. */
+
+ if (tabopt < 0) tabopt = -tabopt;
+ if (tabopt == 1) pbits[1] &= ~0x3c;
+ else if (tabopt == 2) pbits[11] &= 0x7f;
+
+ /* Add the POSIX table or its complement into the main table that is
+ being built and we are done. */
+
+ if (local_negate)
+ for (i = 0; i < 32; i++) classbits[i] |= ~pbits[i];
+ else
+ for (i = 0; i < 32; i++) classbits[i] |= pbits[i];
+
+ /* Every class contains at least one < 256 character. */
+
+ class_has_8bitchar = 1;
+ goto CONTINUE_CLASS; /* End of POSIX handling */
+ }
+
+ /* Other than POSIX classes, the only items we should encounter are
+ \d-type escapes and literal characters (possibly as ranges). */
+
+ if (meta == META_BIGVALUE)
+ {
+ meta = *(++pptr);
+ goto CLASS_LITERAL;
+ }
+
+ /* Any other non-literal must be an escape */
+
+ if (meta >= META_END)
+ {
+ if (META_CODE(meta) != META_ESCAPE)
+ {
+#ifdef DEBUG_SHOW_PARSED
+ fprintf(stderr, "** Unrecognized parsed pattern item 0x%.8x "
+ "in character class\n", meta);
+#endif
+ *errorcodeptr = ERR89; /* Internal error - unrecognized. */
+ return 0;
+ }
+ escape = META_DATA(meta);
+
+ /* Every class contains at least one < 256 character. */
+
+ class_has_8bitchar++;
+
+ switch(escape)
+ {
+ case ESC_d:
+ for (i = 0; i < 32; i++) classbits[i] |= cbits[i+cbit_digit];
+ break;
+
+ case ESC_D:
+ should_flip_negation = TRUE;
+ for (i = 0; i < 32; i++) classbits[i] |= ~cbits[i+cbit_digit];
+ break;
+
+ case ESC_w:
+ for (i = 0; i < 32; i++) classbits[i] |= cbits[i+cbit_word];
+ break;
+
+ case ESC_W:
+ should_flip_negation = TRUE;
+ for (i = 0; i < 32; i++) classbits[i] |= ~cbits[i+cbit_word];
+ break;
+
+ /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
+ 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
+ previously set by something earlier in the character class.
+ Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
+ we could just adjust the appropriate bit. From PCRE 8.34 we no
+ longer treat \s and \S specially. */
+
+ case ESC_s:
+ for (i = 0; i < 32; i++) classbits[i] |= cbits[i+cbit_space];
+ break;
+
+ case ESC_S:
+ should_flip_negation = TRUE;
+ for (i = 0; i < 32; i++) classbits[i] |= ~cbits[i+cbit_space];
+ break;
+
+ /* When adding the horizontal or vertical space lists to a class, or
+ their complements, disable PCRE2_CASELESS, because it justs wastes
+ time, and in the "not-x" UTF cases can create unwanted duplicates in
+ the XCLASS list (provoked by characters that have more than one other
+ case and by both cases being in the same "not-x" sublist). */
+
+ case ESC_h:
+ (void)add_list_to_class(classbits, &class_uchardata,
+ options & ~PCRE2_CASELESS, cb, PRIV(hspace_list), NOTACHAR);
+ break;
+
+ case ESC_H:
+ (void)add_not_list_to_class(classbits, &class_uchardata,
+ options & ~PCRE2_CASELESS, cb, PRIV(hspace_list));
+ break;
+
+ case ESC_v:
+ (void)add_list_to_class(classbits, &class_uchardata,
+ options & ~PCRE2_CASELESS, cb, PRIV(vspace_list), NOTACHAR);
+ break;
+
+ case ESC_V:
+ (void)add_not_list_to_class(classbits, &class_uchardata,
+ options & ~PCRE2_CASELESS, cb, PRIV(vspace_list));
+ break;
+
+ /* If Unicode is not supported, \P and \p are not allowed and are
+ faulted at parse time, so will never appear here. */
+
+#ifdef SUPPORT_UNICODE
+ case ESC_p:
+ case ESC_P:
+ {
+ uint32_t ptype = *(++pptr) >> 16;
+ uint32_t pdata = *pptr & 0xffff;
+ *class_uchardata++ = (escape == ESC_p)? XCL_PROP : XCL_NOTPROP;
+ *class_uchardata++ = ptype;
+ *class_uchardata++ = pdata;
+ xclass_has_prop = TRUE;
+ class_has_8bitchar--; /* Undo! */
+ }
+ break;
+#endif
+ }
+
+ goto CONTINUE_CLASS;
+ } /* End handling \d-type escapes */
+
+ /* A literal character may be followed by a range meta. At parse time
+ there are checks for out-of-order characters, for ranges where the two
+ characters are equal, and for hyphens that cannot indicate a range. At
+ this point, therefore, no checking is needed. */
+
+ else
+ {
+ uint32_t c, d;
+
+ CLASS_LITERAL:
+ c = d = meta;
+
+ /* Remember if \r or \n were explicitly used */
+
+ if (c == CHAR_CR || c == CHAR_NL) cb->external_flags |= PCRE2_HASCRORLF;
+
+ /* Process a character range */
+
+ if (pptr[1] == META_RANGE_LITERAL || pptr[1] == META_RANGE_ESCAPED)
+ {
+#ifdef EBCDIC
+ BOOL range_is_literal = (pptr[1] == META_RANGE_LITERAL);
+#endif
+ pptr += 2;
+ d = *pptr;
+ if (d == META_BIGVALUE) d = *(++pptr);
+
+ /* Remember an explicit \r or \n, and add the range to the class. */
+
+ if (d == CHAR_CR || d == CHAR_NL) cb->external_flags |= PCRE2_HASCRORLF;
+
+ /* In an EBCDIC environment, Perl treats alphabetic ranges specially
+ because there are holes in the encoding, and simply using the range
+ A-Z (for example) would include the characters in the holes. This
+ applies only to literal ranges; [\xC1-\xE9] is different to [A-Z]. */
+
+#ifdef EBCDIC
+ if (range_is_literal &&
+ (cb->ctypes[c] & ctype_letter) != 0 &&
+ (cb->ctypes[d] & ctype_letter) != 0 &&
+ (d <= CHAR_z) == (d <= CHAR_z))
+ {
+ uint32_t uc = (d <= CHAR_z)? 0 : 64;
+ uint32_t C = d - uc;
+ uint32_t D = d - uc;
+
+ if (C <= CHAR_i)
+ {
+ class_has_8bitchar +=
+ add_to_class(classbits, &class_uchardata, options, cb, C + uc,
+ ((D < CHAR_i)? D : CHAR_i) + uc);
+ C = CHAR_j;
+ }
+
+ if (C <= D && C <= CHAR_r)
+ {
+ class_has_8bitchar +=
+ add_to_class(classbits, &class_uchardata, options, cb, C + uc,
+ ((D < CHAR_r)? D : CHAR_r) + uc);
+ C = CHAR_s;
+ }
+
+ if (C <= D)
+ {
+ class_has_8bitchar +=
+ add_to_class(classbits, &class_uchardata, options, cb, C + uc,
+ D + uc);
+ }
+ }
+ else
+#endif
+ /* Not an EBCDIC special range */
+
+ class_has_8bitchar +=
+ add_to_class(classbits, &class_uchardata, options, cb, c, d);
+ goto CONTINUE_CLASS; /* Go get the next char in the class */
+ } /* End of range handling */
+
+
+ /* Handle a single character. */
+
+ class_has_8bitchar +=
+ add_to_class(classbits, &class_uchardata, options, cb, meta, meta);
+ }
+
+ /* Continue to the next item in the class. */
+
+ CONTINUE_CLASS:
+
+#ifdef SUPPORT_WIDE_CHARS
+ /* If any wide characters or Unicode properties have been encountered,
+ set xclass = TRUE. Then, in the pre-compile phase, accumulate the length
+ of the extra data and reset the pointer. This is so that very large
+ classes that contain a zillion wide characters or Unicode property tests
+ do not overwrite the workspace (which is on the stack). */
+
+ if (class_uchardata > class_uchardata_base)
+ {
+ xclass = TRUE;
+ if (lengthptr != NULL)
+ {
+ *lengthptr += class_uchardata - class_uchardata_base;
+ class_uchardata = class_uchardata_base;
+ }
+ }
+#endif
+
+ continue; /* Needed to avoid error when not supporting wide chars */
+ } /* End of main class-processing loop */
+
+ /* If this class is the first thing in the branch, there can be no first
+ char setting, whatever the repeat count. Any reqcu setting must remain
+ unchanged after any kind of repeat. */
+
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+
+ /* If there are characters with values > 255, or Unicode property settings
+ (\p or \P), we have to compile an extended class, with its own opcode,
+ unless there were no property settings and there was a negated special such
+ as \S in the class, and PCRE2_UCP is not set, because in that case all
+ characters > 255 are in or not in the class, so any that were explicitly
+ given as well can be ignored.
+
+ In the UCP case, if certain negated POSIX classes ([:^ascii:] or
+ [^:xdigit:]) were present in a class, we either have to match or not match
+ all wide characters (depending on whether the whole class is or is not
+ negated). This requirement is indicated by match_all_or_no_wide_chars being
+ true. We do this by including an explicit range, which works in both cases.
+ This applies only in UTF and 16-bit and 32-bit non-UTF modes, since there
+ cannot be any wide characters in 8-bit non-UTF mode.
+
+ When there *are* properties in a positive UTF-8 or any 16-bit or 32_bit
+ class where \S etc is present without PCRE2_UCP, causing an extended class
+ to be compiled, we make sure that all characters > 255 are included by
+ forcing match_all_or_no_wide_chars to be true.
+
+ If, when generating an xclass, there are no characters < 256, we can omit
+ the bitmap in the actual compiled code. */
+
+#ifdef SUPPORT_WIDE_CHARS /* Defined for 16/32 bits, or 8-bit with Unicode */
+ if (xclass && (
+#ifdef SUPPORT_UNICODE
+ (options & PCRE2_UCP) != 0 ||
+#endif
+ xclass_has_prop || !should_flip_negation))
+ {
+ if (match_all_or_no_wide_chars || (
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ utf &&
+#endif
+ should_flip_negation && !negate_class && (options & PCRE2_UCP) == 0))
+ {
+ *class_uchardata++ = XCL_RANGE;
+ if (utf) /* Will always be utf in the 8-bit library */
+ {
+ class_uchardata += PRIV(ord2utf)(0x100, class_uchardata);
+ class_uchardata += PRIV(ord2utf)(MAX_UTF_CODE_POINT, class_uchardata);
+ }
+ else /* Can only happen for the 16-bit & 32-bit libraries */
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 16
+ *class_uchardata++ = 0x100;
+ *class_uchardata++ = 0xffffu;
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ *class_uchardata++ = 0x100;
+ *class_uchardata++ = 0xffffffffu;
+#endif
+ }
+ }
+ *class_uchardata++ = XCL_END; /* Marks the end of extra data */
+ *code++ = OP_XCLASS;
+ code += LINK_SIZE;
+ *code = negate_class? XCL_NOT:0;
+ if (xclass_has_prop) *code |= XCL_HASPROP;
+
+ /* If the map is required, move up the extra data to make room for it;
+ otherwise just move the code pointer to the end of the extra data. */
+
+ if (class_has_8bitchar > 0)
+ {
+ *code++ |= XCL_MAP;
+ (void)memmove(code + (32 / sizeof(PCRE2_UCHAR)), code,
+ CU2BYTES(class_uchardata - code));
+ if (negate_class && !xclass_has_prop)
+ for (i = 0; i < 32; i++) classbits[i] = ~classbits[i];
+ memcpy(code, classbits, 32);
+ code = class_uchardata + (32 / sizeof(PCRE2_UCHAR));
+ }
+ else code = class_uchardata;
+
+ /* Now fill in the complete length of the item */
+
+ PUT(previous, 1, (int)(code - previous));
+ break; /* End of class handling */
+ }
+#endif /* SUPPORT_WIDE_CHARS */
+
+ /* If there are no characters > 255, or they are all to be included or
+ excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
+ whole class was negated and whether there were negative specials such as \S
+ (non-UCP) in the class. Then copy the 32-byte map into the code vector,
+ negating it if necessary. */
+
+ *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
+ if (lengthptr == NULL) /* Save time in the pre-compile phase */
+ {
+ if (negate_class)
+ for (i = 0; i < 32; i++) classbits[i] = ~classbits[i];
+ memcpy(code, classbits, 32);
+ }
+ code += 32 / sizeof(PCRE2_UCHAR);
+ break; /* End of class processing */
+
+
+ /* ===================================================================*/
+ /* Deal with (*VERB)s. */
+
+ /* Check for open captures before ACCEPT and close those that are within
+ the same assertion level, also converting ACCEPT to ASSERT_ACCEPT in an
+ assertion. In the first pass, just accumulate the length required;
+ otherwise hitting (*ACCEPT) inside many nested parentheses can cause
+ workspace overflow. Do not set firstcu after *ACCEPT. */
+
+ case META_ACCEPT:
+ cb->had_accept = TRUE;
+ for (oc = cb->open_caps;
+ oc != NULL && oc->assert_depth >= cb->assert_depth;
+ oc = oc->next)
+ {
+ if (lengthptr != NULL)
+ {
+ *lengthptr += CU2BYTES(1) + IMM2_SIZE;
+ }
+ else
+ {
+ *code++ = OP_CLOSE;
+ PUT2INC(code, 0, oc->number);
+ }
+ }
+ *code++ = (cb->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ break;
+
+ case META_PRUNE:
+ case META_SKIP:
+ cb->had_pruneorskip = TRUE;
+ /* Fall through */
+ case META_COMMIT:
+ case META_FAIL:
+ *code++ = verbops[(meta - META_MARK) >> 16];
+ break;
+
+ case META_THEN:
+ cb->external_flags |= PCRE2_HASTHEN;
+ *code++ = OP_THEN;
+ break;
+
+ /* Handle verbs with arguments. Arguments can be very long, especially in
+ 16- and 32-bit modes, and can overflow the workspace in the first pass.
+ However, the argument length is constrained to be small enough to fit in
+ one code unit. This check happens in parse_regex(). In the first pass,
+ instead of putting the argument into memory, we just update the length
+ counter and set up an empty argument. */
+
+ case META_THEN_ARG:
+ cb->external_flags |= PCRE2_HASTHEN;
+ goto VERB_ARG;
+
+ case META_PRUNE_ARG:
+ case META_SKIP_ARG:
+ cb->had_pruneorskip = TRUE;
+ /* Fall through */
+ case META_MARK:
+ case META_COMMIT_ARG:
+ VERB_ARG:
+ *code++ = verbops[(meta - META_MARK) >> 16];
+ /* The length is in characters. */
+ verbarglen = *(++pptr);
+ verbculen = 0;
+ tempcode = code++;
+ for (i = 0; i < (int)verbarglen; i++)
+ {
+ meta = *(++pptr);
+#ifdef SUPPORT_UNICODE
+ if (utf) mclength = PRIV(ord2utf)(meta, mcbuffer); else
+#endif
+ {
+ mclength = 1;
+ mcbuffer[0] = meta;
+ }
+ if (lengthptr != NULL) *lengthptr += mclength; else
+ {
+ memcpy(code, mcbuffer, CU2BYTES(mclength));
+ code += mclength;
+ verbculen += mclength;
+ }
+ }
+
+ *tempcode = verbculen; /* Fill in the code unit length */
+ *code++ = 0; /* Terminating zero */
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle options change. The new setting must be passed back for use in
+ subsequent branches. Reset the greedy defaults and the case value for
+ firstcu and reqcu. */
+
+ case META_OPTIONS:
+ *optionsptr = options = *(++pptr);
+ greedy_default = ((options & PCRE2_UNGREEDY) != 0);
+ greedy_non_default = greedy_default ^ 1;
+ req_caseopt = ((options & PCRE2_CASELESS) != 0)? REQ_CASELESS : 0;
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle conditional subpatterns. The case of (?(Rdigits) is ambiguous
+ because it could be a numerical check on recursion, or a name check on a
+ group's being set. The pre-pass sets up META_COND_RNUMBER as a name so that
+ we can handle it either way. We first try for a name; if not found, process
+ the number. */
+
+ case META_COND_RNUMBER: /* (?(Rdigits) */
+ case META_COND_NAME: /* (?(name) or (?'name') or ?(<name>) */
+ case META_COND_RNAME: /* (?(R&name) - test for recursion */
+ bravalue = OP_COND;
+ {
+ int count, index;
+ PCRE2_SPTR name;
+ named_group *ng = cb->named_groups;
+ uint32_t length = *(++pptr);
+
+ GETPLUSOFFSET(offset, pptr);
+ name = cb->start_pattern + offset;
+
+ /* In the first pass, the names generated in the pre-pass are available,
+ but the main name table has not yet been created. Scan the list of names
+ generated in the pre-pass in order to get a number and whether or not
+ this name is duplicated. If it is not duplicated, we can handle it as a
+ numerical group. */
+
+ for (i = 0; i < cb->names_found; i++, ng++)
+ {
+ if (length == ng->length &&
+ PRIV(strncmp)(name, ng->name, length) == 0)
+ {
+ if (!ng->isdup)
+ {
+ code[1+LINK_SIZE] = (meta == META_COND_RNAME)? OP_RREF : OP_CREF;
+ PUT2(code, 2+LINK_SIZE, ng->number);
+ if (ng->number > cb->top_backref) cb->top_backref = ng->number;
+ skipunits = 1+IMM2_SIZE;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+ }
+ break; /* Found a duplicated name */
+ }
+ }
+
+ /* If the name was not found we have a bad reference, unless we are
+ dealing with R<digits>, which is treated as a recursion test by number.
+ */
+
+ if (i >= cb->names_found)
+ {
+ groupnumber = 0;
+ if (meta == META_COND_RNUMBER)
+ {
+ for (i = 1; i < (int)length; i++)
+ {
+ groupnumber = groupnumber * 10 + name[i] - CHAR_0;
+ if (groupnumber > MAX_GROUP_NUMBER)
+ {
+ *errorcodeptr = ERR61;
+ cb->erroroffset = offset + i;
+ return 0;
+ }
+ }
+ }
+
+ if (meta != META_COND_RNUMBER || groupnumber > cb->bracount)
+ {
+ *errorcodeptr = ERR15;
+ cb->erroroffset = offset;
+ return 0;
+ }
+
+ /* (?Rdigits) treated as a recursion reference by number. A value of
+ zero (which is the result of both (?R) and (?R0)) means "any", and is
+ translated into RREF_ANY (which is 0xffff). */
+
+ if (groupnumber == 0) groupnumber = RREF_ANY;
+ code[1+LINK_SIZE] = OP_RREF;
+ PUT2(code, 2+LINK_SIZE, groupnumber);
+ skipunits = 1+IMM2_SIZE;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+ }
+
+ /* A duplicated name was found. Note that if an R<digits> name is found
+ (META_COND_RNUMBER), it is a reference test, not a recursion test. */
+
+ code[1+LINK_SIZE] = (meta == META_COND_RNAME)? OP_RREF : OP_CREF;
+
+ /* We have a duplicated name. In the compile pass we have to search the
+ main table in order to get the index and count values. */
+
+ count = 0; /* Values for first pass (avoids compiler warning) */
+ index = 0;
+ if (lengthptr == NULL && !find_dupname_details(name, length, &index,
+ &count, errorcodeptr, cb)) return 0;
+
+ /* Add one to the opcode to change CREF/RREF into DNCREF/DNRREF and
+ insert appropriate data values. */
+
+ code[1+LINK_SIZE]++;
+ skipunits = 1+2*IMM2_SIZE;
+ PUT2(code, 2+LINK_SIZE, index);
+ PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count);
+ }
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+ /* The DEFINE condition is always false. It's internal groups may never
+ be called, so matched_char must remain false, hence the jump to
+ GROUP_PROCESS rather than GROUP_PROCESS_NOTE_EMPTY. */
+
+ case META_COND_DEFINE:
+ bravalue = OP_COND;
+ GETPLUSOFFSET(offset, pptr);
+ code[1+LINK_SIZE] = OP_DEFINE;
+ skipunits = 1;
+ goto GROUP_PROCESS;
+
+ /* Conditional test of a group's being set. */
+
+ case META_COND_NUMBER:
+ bravalue = OP_COND;
+ GETPLUSOFFSET(offset, pptr);
+ groupnumber = *(++pptr);
+ if (groupnumber > cb->bracount)
+ {
+ *errorcodeptr = ERR15;
+ cb->erroroffset = offset;
+ return 0;
+ }
+ if (groupnumber > cb->top_backref) cb->top_backref = groupnumber;
+ offset -= 2; /* Point at initial ( for too many branches error */
+ code[1+LINK_SIZE] = OP_CREF;
+ skipunits = 1+IMM2_SIZE;
+ PUT2(code, 2+LINK_SIZE, groupnumber);
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+ /* Test for the PCRE2 version. */
+
+ case META_COND_VERSION:
+ bravalue = OP_COND;
+ if (pptr[1] > 0)
+ code[1+LINK_SIZE] = ((PCRE2_MAJOR > pptr[2]) ||
+ (PCRE2_MAJOR == pptr[2] && PCRE2_MINOR >= pptr[3]))?
+ OP_TRUE : OP_FALSE;
+ else
+ code[1+LINK_SIZE] = (PCRE2_MAJOR == pptr[2] && PCRE2_MINOR == pptr[3])?
+ OP_TRUE : OP_FALSE;
+ skipunits = 1;
+ pptr += 3;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+ /* The condition is an assertion, possibly preceded by a callout. */
+
+ case META_COND_ASSERT:
+ bravalue = OP_COND;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+
+ /* ===================================================================*/
+ /* Handle all kinds of nested bracketed groups. The non-capturing,
+ non-conditional cases are here; others come to GROUP_PROCESS via goto. */
+
+ case META_LOOKAHEAD:
+ bravalue = OP_ASSERT;
+ cb->assert_depth += 1;
+ goto GROUP_PROCESS;
+
+ /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird
+ thing to do, but Perl allows all assertions to be quantified, and when
+ they contain capturing parentheses there may be a potential use for
+ this feature. Not that that applies to a quantified (?!) but we allow
+ it for uniformity. */
+
+ case META_LOOKAHEADNOT:
+ if (pptr[1] == META_KET &&
+ (pptr[2] < META_ASTERISK || pptr[2] > META_MINMAX_QUERY))
+ {
+ *code++ = OP_FAIL;
+ pptr++;
+ }
+ else
+ {
+ bravalue = OP_ASSERT_NOT;
+ cb->assert_depth += 1;
+ goto GROUP_PROCESS;
+ }
+ break;
+
+ case META_LOOKBEHIND:
+ bravalue = OP_ASSERTBACK;
+ cb->assert_depth += 1;
+ goto GROUP_PROCESS;
+
+ case META_LOOKBEHINDNOT:
+ bravalue = OP_ASSERTBACK_NOT;
+ cb->assert_depth += 1;
+ goto GROUP_PROCESS;
+
+ case META_ATOMIC:
+ bravalue = OP_ONCE;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+ case META_NOCAPTURE:
+ bravalue = OP_BRA;
+ /* Fall through */
+
+ /* Process nested bracketed regex. The nesting depth is maintained for the
+ benefit of the stackguard function. The test for too deep nesting is now
+ done in parse_regex(). Assertion and DEFINE groups come to GROUP_PROCESS;
+ others come to GROUP_PROCESS_NOTE_EMPTY, to indicate that we need to take
+ note of whether or not they may match an empty string. */
+
+ GROUP_PROCESS_NOTE_EMPTY:
+ note_group_empty = TRUE;
+
+ GROUP_PROCESS:
+ cb->parens_depth += 1;
+ *code = bravalue;
+ pptr++;
+ tempcode = code;
+ tempreqvary = cb->req_varyopt; /* Save value before group */
+ length_prevgroup = 0; /* Initialize for pre-compile phase */
+
+ if ((group_return =
+ compile_regex(
+ options, /* The option state */
+ &tempcode, /* Where to put code (updated) */
+ &pptr, /* Input pointer (updated) */
+ errorcodeptr, /* Where to put an error message */
+ skipunits, /* Skip over bracket number */
+ &subfirstcu, /* For possible first char */
+ &subfirstcuflags,
+ &subreqcu, /* For possible last char */
+ &subreqcuflags,
+ bcptr, /* Current branch chain */
+ cb, /* Compile data block */
+ (lengthptr == NULL)? NULL : /* Actual compile phase */
+ &length_prevgroup /* Pre-compile phase */
+ )) == 0)
+ return 0; /* Error */
+
+ cb->parens_depth -= 1;
+
+ /* If that was a non-conditional significant group (not an assertion, not a
+ DEFINE) that matches at least one character, then the current item matches
+ a character. Conditionals are handled below. */
+
+ if (note_group_empty && bravalue != OP_COND && group_return > 0)
+ matched_char = TRUE;
+
+ /* If we've just compiled an assertion, pop the assert depth. */
+
+ if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
+ cb->assert_depth -= 1;
+
+ /* At the end of compiling, code is still pointing to the start of the
+ group, while tempcode has been updated to point past the end of the group.
+ The parsed pattern pointer (pptr) is on the closing META_KET.
+
+ If this is a conditional bracket, check that there are no more than
+ two branches in the group, or just one if it's a DEFINE group. We do this
+ in the real compile phase, not in the pre-pass, where the whole group may
+ not be available. */
+
+ if (bravalue == OP_COND && lengthptr == NULL)
+ {
+ PCRE2_UCHAR *tc = code;
+ int condcount = 0;
+
+ do {
+ condcount++;
+ tc += GET(tc,1);
+ }
+ while (*tc != OP_KET);
+
+ /* A DEFINE group is never obeyed inline (the "condition" is always
+ false). It must have only one branch. Having checked this, change the
+ opcode to OP_FALSE. */
+
+ if (code[LINK_SIZE+1] == OP_DEFINE)
+ {
+ if (condcount > 1)
+ {
+ cb->erroroffset = offset;
+ *errorcodeptr = ERR54;
+ return 0;
+ }
+ code[LINK_SIZE+1] = OP_FALSE;
+ bravalue = OP_DEFINE; /* A flag to suppress char handling below */
+ }
+
+ /* A "normal" conditional group. If there is just one branch, we must not
+ make use of its firstcu or reqcu, because this is equivalent to an
+ empty second branch. Also, it may match an empty string. If there are two
+ branches, this item must match a character if the group must. */
+
+ else
+ {
+ if (condcount > 2)
+ {
+ cb->erroroffset = offset;
+ *errorcodeptr = ERR27;
+ return 0;
+ }
+ if (condcount == 1) subfirstcuflags = subreqcuflags = REQ_NONE;
+ else if (group_return > 0) matched_char = TRUE;
+ }
+ }
+
+ /* In the pre-compile phase, update the length by the length of the group,
+ less the brackets at either end. Then reduce the compiled code to just a
+ set of non-capturing brackets so that it doesn't use much memory if it is
+ duplicated by a quantifier.*/
+
+ if (lengthptr != NULL)
+ {
+ if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
+ {
+ *errorcodeptr = ERR20;
+ return 0;
+ }
+ *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
+ code++; /* This already contains bravalue */
+ PUTINC(code, 0, 1 + LINK_SIZE);
+ *code++ = OP_KET;
+ PUTINC(code, 0, 1 + LINK_SIZE);
+ break; /* No need to waste time with special character handling */
+ }
+
+ /* Otherwise update the main code pointer to the end of the group. */
+
+ code = tempcode;
+
+ /* For a DEFINE group, required and first character settings are not
+ relevant. */
+
+ if (bravalue == OP_DEFINE) break;
+
+ /* Handle updating of the required and first code units for other types of
+ group. Update for normal brackets of all kinds, and conditions with two
+ branches (see code above). If the bracket is followed by a quantifier with
+ zero repeat, we have to back off. Hence the definition of zeroreqcu and
+ zerofirstcu outside the main loop so that they can be accessed for the back
+ off. */
+
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ groupsetfirstcu = FALSE;
+
+ if (bravalue >= OP_ONCE) /* Not an assertion */
+ {
+ /* If we have not yet set a firstcu in this branch, take it from the
+ subpattern, remembering that it was set here so that a repeat of more
+ than one can replicate it as reqcu if necessary. If the subpattern has
+ no firstcu, set "none" for the whole branch. In both cases, a zero
+ repeat forces firstcu to "none". */
+
+ if (firstcuflags == REQ_UNSET && subfirstcuflags != REQ_UNSET)
+ {
+ if (subfirstcuflags >= 0)
+ {
+ firstcu = subfirstcu;
+ firstcuflags = subfirstcuflags;
+ groupsetfirstcu = TRUE;
+ }
+ else firstcuflags = REQ_NONE;
+ zerofirstcuflags = REQ_NONE;
+ }
+
+ /* If firstcu was previously set, convert the subpattern's firstcu
+ into reqcu if there wasn't one, using the vary flag that was in
+ existence beforehand. */
+
+ else if (subfirstcuflags >= 0 && subreqcuflags < 0)
+ {
+ subreqcu = subfirstcu;
+ subreqcuflags = subfirstcuflags | tempreqvary;
+ }
+
+ /* If the subpattern set a required code unit (or set a first code unit
+ that isn't really the first code unit - see above), set it. */
+
+ if (subreqcuflags >= 0)
+ {
+ reqcu = subreqcu;
+ reqcuflags = subreqcuflags;
+ }
+ }
+
+ /* For a forward assertion, we take the reqcu, if set, provided that the
+ group has also set a firstcu. This can be helpful if the pattern that
+ follows the assertion doesn't set a different char. For example, it's
+ useful for /(?=abcde).+/. We can't set firstcu for an assertion, however
+ because it leads to incorrect effect for patterns such as /(?=a)a.+/ when
+ the "real" "a" would then become a reqcu instead of a firstcu. This is
+ overcome by a scan at the end if there's no firstcu, looking for an
+ asserted first char. A similar effect for patterns like /(?=.*X)X$/ means
+ we must only take the reqcu when the group also set a firstcu. Otherwise,
+ in that example, 'X' ends up set for both. */
+
+ else if (bravalue == OP_ASSERT && subreqcuflags >= 0 &&
+ subfirstcuflags >= 0)
+ {
+ reqcu = subreqcu;
+ reqcuflags = subreqcuflags;
+ }
+
+ break; /* End of nested group handling */
+
+
+ /* ===================================================================*/
+ /* Handle named backreferences and recursions. */
+
+ case META_BACKREF_BYNAME:
+ case META_RECURSE_BYNAME:
+ {
+ int count, index;
+ PCRE2_SPTR name;
+ BOOL is_dupname = FALSE;
+ named_group *ng = cb->named_groups;
+ uint32_t length = *(++pptr);
+
+ GETPLUSOFFSET(offset, pptr);
+ name = cb->start_pattern + offset;
+
+ /* In the first pass, the names generated in the pre-pass are available,
+ but the main name table has not yet been created. Scan the list of names
+ generated in the pre-pass in order to get a number and whether or not
+ this name is duplicated. */
+
+ groupnumber = 0;
+ for (i = 0; i < cb->names_found; i++, ng++)
+ {
+ if (length == ng->length &&
+ PRIV(strncmp)(name, ng->name, length) == 0)
+ {
+ is_dupname = ng->isdup;
+ groupnumber = ng->number;
+
+ /* For a recursion, that's all that is needed. We can now go to
+ the code above that handles numerical recursion, applying it to
+ the first group with the given name. */
+
+ if (meta == META_RECURSE_BYNAME)
+ {
+ meta_arg = groupnumber;
+ goto HANDLE_NUMERICAL_RECURSION;
+ }
+
+ /* For a back reference, update the back reference map and the
+ maximum back reference. Then, for each group, we must check to
+ see if it is recursive, that is, it is inside the group that it
+ references. A flag is set so that the group can be made atomic.
+ */
+
+ cb->backref_map |= (groupnumber < 32)? (1u << groupnumber) : 1;
+ if (groupnumber > cb->top_backref)
+ cb->top_backref = groupnumber;
+
+ for (oc = cb->open_caps; oc != NULL; oc = oc->next)
+ {
+ if (oc->number == groupnumber)
+ {
+ oc->flag = TRUE;
+ break;
+ }
+ }
+ }
+ }
+
+ /* If the name was not found we have a bad reference. */
+
+ if (groupnumber == 0)
+ {
+ *errorcodeptr = ERR15;
+ cb->erroroffset = offset;
+ return 0;
+ }
+
+ /* If a back reference name is not duplicated, we can handle it as
+ a numerical reference. */
+
+ if (!is_dupname)
+ {
+ meta_arg = groupnumber;
+ goto HANDLE_SINGLE_REFERENCE;
+ }
+
+ /* If a back reference name is duplicated, we generate a different
+ opcode to a numerical back reference. In the second pass we must
+ search for the index and count in the final name table. */
+
+ count = 0; /* Values for first pass (avoids compiler warning) */
+ index = 0;
+ if (lengthptr == NULL && !find_dupname_details(name, length, &index,
+ &count, errorcodeptr, cb)) return 0;
+
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ *code++ = ((options & PCRE2_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
+ PUT2INC(code, 0, index);
+ PUT2INC(code, 0, count);
+ }
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle a numerical callout. */
+
+ case META_CALLOUT_NUMBER:
+ code[0] = OP_CALLOUT;
+ PUT(code, 1, pptr[1]); /* Offset to next pattern item */
+ PUT(code, 1 + LINK_SIZE, pptr[2]); /* Length of next pattern item */
+ code[1 + 2*LINK_SIZE] = pptr[3];
+ pptr += 3;
+ code += PRIV(OP_lengths)[OP_CALLOUT];
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle a callout with a string argument. In the pre-pass we just compute
+ the length without generating anything. The length in pptr[3] includes both
+ delimiters; in the actual compile only the first one is copied, but a
+ terminating zero is added. Any doubled delimiters within the string make
+ this an overestimate, but it is not worth bothering about. */
+
+ case META_CALLOUT_STRING:
+ if (lengthptr != NULL)
+ {
+ *lengthptr += pptr[3] + (1 + 4*LINK_SIZE);
+ pptr += 3;
+ SKIPOFFSET(pptr);
+ }
+
+ /* In the real compile we can copy the string. The starting delimiter is
+ included so that the client can discover it if they want. We also pass the
+ start offset to help a script language give better error messages. */
+
+ else
+ {
+ PCRE2_SPTR pp;
+ uint32_t delimiter;
+ uint32_t length = pptr[3];
+ PCRE2_UCHAR *callout_string = code + (1 + 4*LINK_SIZE);
+
+ code[0] = OP_CALLOUT_STR;
+ PUT(code, 1, pptr[1]); /* Offset to next pattern item */
+ PUT(code, 1 + LINK_SIZE, pptr[2]); /* Length of next pattern item */
+
+ pptr += 3;
+ GETPLUSOFFSET(offset, pptr); /* Offset to string in pattern */
+ pp = cb->start_pattern + offset;
+ delimiter = *callout_string++ = *pp++;
+ if (delimiter == CHAR_LEFT_CURLY_BRACKET)
+ delimiter = CHAR_RIGHT_CURLY_BRACKET;
+ PUT(code, 1 + 3*LINK_SIZE, (int)(offset + 1)); /* One after delimiter */
+
+ /* The syntax of the pattern was checked in the parsing scan. The length
+ includes both delimiters, but we have passed the opening one just above,
+ so we reduce length before testing it. The test is for > 1 because we do
+ not want to copy the final delimiter. This also ensures that pp[1] is
+ accessible. */
+
+ while (--length > 1)
+ {
+ if (*pp == delimiter && pp[1] == delimiter)
+ {
+ *callout_string++ = delimiter;
+ pp += 2;
+ length--;
+ }
+ else *callout_string++ = *pp++;
+ }
+ *callout_string++ = CHAR_NUL;
+
+ /* Set the length of the entire item, the advance to its end. */
+
+ PUT(code, 1 + 2*LINK_SIZE, (int)(callout_string - code));
+ code = callout_string;
+ }
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle repetition. The different types are all sorted out in the parsing
+ pass. */
+
+ case META_MINMAX_PLUS:
+ case META_MINMAX_QUERY:
+ case META_MINMAX:
+ repeat_min = *(++pptr);
+ repeat_max = *(++pptr);
+ goto REPEAT;
+
+ case META_ASTERISK:
+ case META_ASTERISK_PLUS:
+ case META_ASTERISK_QUERY:
+ repeat_min = 0;
+ repeat_max = REPEAT_UNLIMITED;
+ goto REPEAT;
+
+ case META_PLUS:
+ case META_PLUS_PLUS:
+ case META_PLUS_QUERY:
+ repeat_min = 1;
+ repeat_max = REPEAT_UNLIMITED;
+ goto REPEAT;
+
+ case META_QUERY:
+ case META_QUERY_PLUS:
+ case META_QUERY_QUERY:
+ repeat_min = 0;
+ repeat_max = 1;
+
+ REPEAT:
+ if (previous_matched_char && repeat_min > 0) matched_char = TRUE;
+
+ /* Remember whether this is a variable length repeat, and default to
+ single-char opcodes. */
+
+ reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
+ op_type = 0;
+
+ /* If the repeat is {1} we can ignore it. */
+
+ if (repeat_max == 1 && repeat_min == 1) goto END_REPEAT;
+
+ /* Adjust first and required code units for a zero repeat. */
+
+ if (repeat_min == 0)
+ {
+ firstcu = zerofirstcu;
+ firstcuflags = zerofirstcuflags;
+ reqcu = zeroreqcu;
+ reqcuflags = zeroreqcuflags;
+ }
+
+ /* Note the greediness and possessiveness. */
+
+ switch (meta)
+ {
+ case META_MINMAX_PLUS:
+ case META_ASTERISK_PLUS:
+ case META_PLUS_PLUS:
+ case META_QUERY_PLUS:
+ repeat_type = 0; /* Force greedy */
+ possessive_quantifier = TRUE;
+ break;
+
+ case META_MINMAX_QUERY:
+ case META_ASTERISK_QUERY:
+ case META_PLUS_QUERY:
+ case META_QUERY_QUERY:
+ repeat_type = greedy_non_default;
+ possessive_quantifier = FALSE;
+ break;
+
+ default:
+ repeat_type = greedy_default;
+ possessive_quantifier = FALSE;
+ break;
+ }
+
+ /* Save start of previous item, in case we have to move it up in order to
+ insert something before it, and remember what it was. */
+
+ tempcode = previous;
+ op_previous = *previous;
+
+ /* Now handle repetition for the different types of item. */
+
+ switch (op_previous)
+ {
+ /* If previous was a character or negated character match, abolish the
+ item and generate a repeat item instead. If a char item has a minimum of
+ more than one, ensure that it is set in reqcu - it might not be if a
+ sequence such as x{3} is the first thing in a branch because the x will
+ have gone into firstcu instead. */
+
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ op_type = chartypeoffset[op_previous - OP_CHAR];
+
+ /* Deal with UTF characters that take up more than one code unit. */
+
+#ifdef MAYBE_UTF_MULTI
+ if (utf && NOT_FIRSTCU(code[-1]))
+ {
+ PCRE2_UCHAR *lastchar = code - 1;
+ BACKCHAR(lastchar);
+ mclength = (uint32_t)(code - lastchar); /* Length of UTF character */
+ memcpy(mcbuffer, lastchar, CU2BYTES(mclength)); /* Save the char */
+ }
+ else
+#endif /* MAYBE_UTF_MULTI */
+
+ /* Handle the case of a single code unit - either with no UTF support, or
+ with UTF disabled, or for a single-code-unit UTF character. */
+ {
+ mcbuffer[0] = code[-1];
+ mclength = 1;
+ if (op_previous <= OP_CHARI && repeat_min > 1)
+ {
+ reqcu = mcbuffer[0];
+ reqcuflags = req_caseopt | cb->req_varyopt;
+ }
+ }
+ goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
+
+ /* If previous was a character class or a back reference, we put the
+ repeat stuff after it, but just skip the item if the repeat was {0,0}. */
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+#endif
+ case OP_CLASS:
+ case OP_NCLASS:
+ case OP_REF:
+ case OP_REFI:
+ case OP_DNREF:
+ case OP_DNREFI:
+
+ if (repeat_max == 0)
+ {
+ code = previous;
+ goto END_REPEAT;
+ }
+
+ if (repeat_min == 0 && repeat_max == REPEAT_UNLIMITED)
+ *code++ = OP_CRSTAR + repeat_type;
+ else if (repeat_min == 1 && repeat_max == REPEAT_UNLIMITED)
+ *code++ = OP_CRPLUS + repeat_type;
+ else if (repeat_min == 0 && repeat_max == 1)
+ *code++ = OP_CRQUERY + repeat_type;
+ else
+ {
+ *code++ = OP_CRRANGE + repeat_type;
+ PUT2INC(code, 0, repeat_min);
+ if (repeat_max == REPEAT_UNLIMITED) repeat_max = 0; /* 2-byte encoding for max */
+ PUT2INC(code, 0, repeat_max);
+ }
+ break;
+
+ /* If previous is OP_FAIL, it was generated by an empty class []
+ (PCRE2_ALLOW_EMPTY_CLASS is set). The other ways in which OP_FAIL can be
+ generated, that is by (*FAIL) or (?!), disallow a quantifier at parse
+ time. We can just ignore this repeat. */
+
+ case OP_FAIL:
+ goto END_REPEAT;
+
+ /* Prior to 10.30, repeated recursions were wrapped in OP_ONCE brackets
+ because pcre2_match() could not handle backtracking into recursively
+ called groups. Now that this backtracking is available, we no longer need
+ to do this. However, we still need to replicate recursions as we do for
+ groups so as to have independent backtracking points. We can replicate
+ for the minimum number of repeats directly. For optional repeats we now
+ wrap the recursion in OP_BRA brackets and make use of the bracket
+ repetition. */
+
+ case OP_RECURSE:
+
+ /* Generate unwrapped repeats for a non-zero minimum, except when the
+ minimum is 1 and the maximum unlimited, because that can be handled with
+ OP_BRA terminated by OP_KETRMAX/MIN. When the maximum is equal to the
+ minimum, we just need to generate the appropriate additional copies.
+ Otherwise we need to generate one more, to simulate the situation when
+ the minimum is zero. */
+
+ if (repeat_min > 0 && (repeat_min != 1 || repeat_max != REPEAT_UNLIMITED))
+ {
+ int replicate = repeat_min;
+ if (repeat_min == repeat_max) replicate--;
+
+ /* In the pre-compile phase, we don't actually do the replication. We
+ just adjust the length as if we had. Do some paranoid checks for
+ potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
+ integer type when available, otherwise double. */
+
+ if (lengthptr != NULL)
+ {
+ PCRE2_SIZE delta = replicate*(1 + LINK_SIZE);
+ if ((INT64_OR_DOUBLE)replicate*
+ (INT64_OR_DOUBLE)(1 + LINK_SIZE) >
+ (INT64_OR_DOUBLE)INT_MAX ||
+ OFLOW_MAX - *lengthptr < delta)
+ {
+ *errorcodeptr = ERR20;
+ return 0;
+ }
+ *lengthptr += delta;
+ }
+
+ else for (i = 0; i < replicate; i++)
+ {
+ memcpy(code, previous, CU2BYTES(1 + LINK_SIZE));
+ previous = code;
+ code += 1 + LINK_SIZE;
+ }
+
+ /* If the number of repeats is fixed, we are done. Otherwise, adjust
+ the counts and fall through. */
+
+ if (repeat_min == repeat_max) break;
+ if (repeat_max != REPEAT_UNLIMITED) repeat_max -= repeat_min;
+ repeat_min = 0;
+ }
+
+ /* Wrap the recursion call in OP_BRA brackets. */
+
+ (void)memmove(previous + 1 + LINK_SIZE, previous, CU2BYTES(1 + LINK_SIZE));
+ op_previous = *previous = OP_BRA;
+ PUT(previous, 1, 2 + 2*LINK_SIZE);
+ previous[2 + 2*LINK_SIZE] = OP_KET;
+ PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
+ code += 2 + 2 * LINK_SIZE;
+ length_prevgroup = 3 + 3*LINK_SIZE;
+ group_return = -1; /* Set "may match empty string" */
+
+ /* Now treat as a repeated OP_BRA. */
+ /* Fall through */
+
+ /* If previous was a bracket group, we may have to replicate it in
+ certain cases. Note that at this point we can encounter only the "basic"
+ bracket opcodes such as BRA and CBRA, as this is the place where they get
+ converted into the more special varieties such as BRAPOS and SBRA.
+ Originally, PCRE did not allow repetition of assertions, but now it does,
+ for Perl compatibility. */
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_CBRA:
+ case OP_COND:
+ {
+ int len = (int)(code - previous);
+ PCRE2_UCHAR *bralink = NULL;
+ PCRE2_UCHAR *brazeroptr = NULL;
+
+ /* Repeating a DEFINE group (or any group where the condition is always
+ FALSE and there is only one branch) is pointless, but Perl allows the
+ syntax, so we just ignore the repeat. */
+
+ if (op_previous == OP_COND && previous[LINK_SIZE+1] == OP_FALSE &&
+ previous[GET(previous, 1)] != OP_ALT)
+ goto END_REPEAT;
+
+ /* There is no sense in actually repeating assertions. The only
+ potential use of repetition is in cases when the assertion is optional.
+ Therefore, if the minimum is greater than zero, just ignore the repeat.
+ If the maximum is not zero or one, set it to 1. */
+
+ if (op_previous < OP_ONCE) /* Assertion */
+ {
+ if (repeat_min > 0) goto END_REPEAT;
+ if (repeat_max > 1) repeat_max = 1;
+ }
+
+ /* The case of a zero minimum is special because of the need to stick
+ OP_BRAZERO in front of it, and because the group appears once in the
+ data, whereas in other cases it appears the minimum number of times. For
+ this reason, it is simplest to treat this case separately, as otherwise
+ the code gets far too messy. There are several special subcases when the
+ minimum is zero. */
+
+ if (repeat_min == 0)
+ {
+ /* If the maximum is also zero, we used to just omit the group from
+ the output altogether, like this:
+
+ ** if (repeat_max == 0)
+ ** {
+ ** code = previous;
+ ** goto END_REPEAT;
+ ** }
+
+ However, that fails when a group or a subgroup within it is
+ referenced as a subroutine from elsewhere in the pattern, so now we
+ stick in OP_SKIPZERO in front of it so that it is skipped on
+ execution. As we don't have a list of which groups are referenced, we
+ cannot do this selectively.
+
+ If the maximum is 1 or unlimited, we just have to stick in the
+ BRAZERO and do no more at this point. */
+
+ if (repeat_max <= 1 || repeat_max == REPEAT_UNLIMITED)
+ {
+ (void)memmove(previous + 1, previous, CU2BYTES(len));
+ code++;
+ if (repeat_max == 0)
+ {
+ *previous++ = OP_SKIPZERO;
+ goto END_REPEAT;
+ }
+ brazeroptr = previous; /* Save for possessive optimizing */
+ *previous++ = OP_BRAZERO + repeat_type;
+ }
+
+ /* If the maximum is greater than 1 and limited, we have to replicate
+ in a nested fashion, sticking OP_BRAZERO before each set of brackets.
+ The first one has to be handled carefully because it's the original
+ copy, which has to be moved up. The remainder can be handled by code
+ that is common with the non-zero minimum case below. We have to
+ adjust the value or repeat_max, since one less copy is required. */
+
+ else
+ {
+ int linkoffset;
+ (void)memmove(previous + 2 + LINK_SIZE, previous, CU2BYTES(len));
+ code += 2 + LINK_SIZE;
+ *previous++ = OP_BRAZERO + repeat_type;
+ *previous++ = OP_BRA;
+
+ /* We chain together the bracket link offset fields that have to be
+ filled in later when the ends of the brackets are reached. */
+
+ linkoffset = (bralink == NULL)? 0 : (int)(previous - bralink);
+ bralink = previous;
+ PUTINC(previous, 0, linkoffset);
+ }
+
+ if (repeat_max != REPEAT_UNLIMITED) repeat_max--;
+ }
+
+ /* If the minimum is greater than zero, replicate the group as many
+ times as necessary, and adjust the maximum to the number of subsequent
+ copies that we need. */
+
+ else
+ {
+ if (repeat_min > 1)
+ {
+ /* In the pre-compile phase, we don't actually do the replication.
+ We just adjust the length as if we had. Do some paranoid checks for
+ potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
+ integer type when available, otherwise double. */
+
+ if (lengthptr != NULL)
+ {
+ PCRE2_SIZE delta = (repeat_min - 1)*length_prevgroup;
+ if ((INT64_OR_DOUBLE)(repeat_min - 1)*
+ (INT64_OR_DOUBLE)length_prevgroup >
+ (INT64_OR_DOUBLE)INT_MAX ||
+ OFLOW_MAX - *lengthptr < delta)
+ {
+ *errorcodeptr = ERR20;
+ return 0;
+ }
+ *lengthptr += delta;
+ }
+
+ /* This is compiling for real. If there is a set first code unit
+ for the group, and we have not yet set a "required code unit", set
+ it. */
+
+ else
+ {
+ if (groupsetfirstcu && reqcuflags < 0)
+ {
+ reqcu = firstcu;
+ reqcuflags = firstcuflags;
+ }
+ for (i = 1; (uint32_t)i < repeat_min; i++)
+ {
+ memcpy(code, previous, CU2BYTES(len));
+ code += len;
+ }
+ }
+ }
+
+ if (repeat_max != REPEAT_UNLIMITED) repeat_max -= repeat_min;
+ }
+
+ /* This code is common to both the zero and non-zero minimum cases. If
+ the maximum is limited, it replicates the group in a nested fashion,
+ remembering the bracket starts on a stack. In the case of a zero
+ minimum, the first one was set up above. In all cases the repeat_max
+ now specifies the number of additional copies needed. Again, we must
+ remember to replicate entries on the forward reference list. */
+
+ if (repeat_max != REPEAT_UNLIMITED)
+ {
+ /* In the pre-compile phase, we don't actually do the replication. We
+ just adjust the length as if we had. For each repetition we must add
+ 1 to the length for BRAZERO and for all but the last repetition we
+ must add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
+ paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type
+ is a 64-bit integer type when available, otherwise double. */
+
+ if (lengthptr != NULL && repeat_max > 0)
+ {
+ PCRE2_SIZE delta = repeat_max*(length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
+ 2 - 2*LINK_SIZE; /* Last one doesn't nest */
+ if ((INT64_OR_DOUBLE)repeat_max *
+ (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
+ > (INT64_OR_DOUBLE)INT_MAX ||
+ OFLOW_MAX - *lengthptr < delta)
+ {
+ *errorcodeptr = ERR20;
+ return 0;
+ }
+ *lengthptr += delta;
+ }
+
+ /* This is compiling for real */
+
+ else for (i = repeat_max - 1; i >= 0; i--)
+ {
+ *code++ = OP_BRAZERO + repeat_type;
+
+ /* All but the final copy start a new nesting, maintaining the
+ chain of brackets outstanding. */
+
+ if (i != 0)
+ {
+ int linkoffset;
+ *code++ = OP_BRA;
+ linkoffset = (bralink == NULL)? 0 : (int)(code - bralink);
+ bralink = code;
+ PUTINC(code, 0, linkoffset);
+ }
+
+ memcpy(code, previous, CU2BYTES(len));
+ code += len;
+ }
+
+ /* Now chain through the pending brackets, and fill in their length
+ fields (which are holding the chain links pro tem). */
+
+ while (bralink != NULL)
+ {
+ int oldlinkoffset;
+ int linkoffset = (int)(code - bralink + 1);
+ PCRE2_UCHAR *bra = code - linkoffset;
+ oldlinkoffset = GET(bra, 1);
+ bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
+ *code++ = OP_KET;
+ PUTINC(code, 0, linkoffset);
+ PUT(bra, 1, linkoffset);
+ }
+ }
+
+ /* If the maximum is unlimited, set a repeater in the final copy. For
+ ONCE brackets, that's all we need to do. However, possessively repeated
+ ONCE brackets can be converted into non-capturing brackets, as the
+ behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
+ deal with possessive ONCEs specially.
+
+ Otherwise, when we are doing the actual compile phase, check to see
+ whether this group is one that could match an empty string. If so,
+ convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
+ that runtime checking can be done. [This check is also applied to ONCE
+ groups at runtime, but in a different way.]
+
+ Then, if the quantifier was possessive and the bracket is not a
+ conditional, we convert the BRA code to the POS form, and the KET code to
+ KETRPOS. (It turns out to be convenient at runtime to detect this kind of
+ subpattern at both the start and at the end.) The use of special opcodes
+ makes it possible to reduce greatly the stack usage in pcre2_match(). If
+ the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
+
+ Then, if the minimum number of matches is 1 or 0, cancel the possessive
+ flag so that the default action below, of wrapping everything inside
+ atomic brackets, does not happen. When the minimum is greater than 1,
+ there will be earlier copies of the group, and so we still have to wrap
+ the whole thing. */
+
+ else
+ {
+ PCRE2_UCHAR *ketcode = code - 1 - LINK_SIZE;
+ PCRE2_UCHAR *bracode = ketcode - GET(ketcode, 1);
+
+ /* Convert possessive ONCE brackets to non-capturing */
+
+ if (*bracode == OP_ONCE && possessive_quantifier) *bracode = OP_BRA;
+
+ /* For non-possessive ONCE brackets, all we need to do is to
+ set the KET. */
+
+ if (*bracode == OP_ONCE) *ketcode = OP_KETRMAX + repeat_type;
+
+ /* Handle non-ONCE brackets and possessive ONCEs (which have been
+ converted to non-capturing above). */
+
+ else
+ {
+ /* In the compile phase, adjust the opcode if the group can match
+ an empty string. For a conditional group with only one branch, the
+ value of group_return will not show "could be empty", so we must
+ check that separately. */
+
+ if (lengthptr == NULL)
+ {
+ if (group_return < 0) *bracode += OP_SBRA - OP_BRA;
+ if (*bracode == OP_COND && bracode[GET(bracode,1)] != OP_ALT)
+ *bracode = OP_SCOND;
+ }
+
+ /* Handle possessive quantifiers. */
+
+ if (possessive_quantifier)
+ {
+ /* For COND brackets, we wrap the whole thing in a possessively
+ repeated non-capturing bracket, because we have not invented POS
+ versions of the COND opcodes. */
+
+ if (*bracode == OP_COND || *bracode == OP_SCOND)
+ {
+ int nlen = (int)(code - bracode);
+ (void)memmove(bracode + 1 + LINK_SIZE, bracode, CU2BYTES(nlen));
+ code += 1 + LINK_SIZE;
+ nlen += 1 + LINK_SIZE;
+ *bracode = (*bracode == OP_COND)? OP_BRAPOS : OP_SBRAPOS;
+ *code++ = OP_KETRPOS;
+ PUTINC(code, 0, nlen);
+ PUT(bracode, 1, nlen);
+ }
+
+ /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
+
+ else
+ {
+ *bracode += 1; /* Switch to xxxPOS opcodes */
+ *ketcode = OP_KETRPOS;
+ }
+
+ /* If the minimum is zero, mark it as possessive, then unset the
+ possessive flag when the minimum is 0 or 1. */
+
+ if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
+ if (repeat_min < 2) possessive_quantifier = FALSE;
+ }
+
+ /* Non-possessive quantifier */
+
+ else *ketcode = OP_KETRMAX + repeat_type;
+ }
+ }
+ }
+ break;
+
+ /* If previous was a character type match (\d or similar), abolish it and
+ create a suitable repeat item. The code is shared with single-character
+ repeats by setting op_type to add a suitable offset into repeat_type.
+ Note the the Unicode property types will be present only when
+ SUPPORT_UNICODE is defined, but we don't wrap the little bits of code
+ here because it just makes it horribly messy. */
+
+ default:
+ if (op_previous >= OP_EODN) /* Not a character type - internal error */
+ {
+ *errorcodeptr = ERR10;
+ return 0;
+ }
+ else
+ {
+ int prop_type, prop_value;
+ PCRE2_UCHAR *oldcode;
+
+ op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
+ mclength = 0; /* Not a character */
+
+ if (op_previous == OP_PROP || op_previous == OP_NOTPROP)
+ {
+ prop_type = previous[1];
+ prop_value = previous[2];
+ }
+ else
+ {
+ /* Come here from just above with a character in mcbuffer/mclength. */
+ OUTPUT_SINGLE_REPEAT:
+ prop_type = prop_value = -1;
+ }
+
+ /* At this point, if prop_type == prop_value == -1 we either have a
+ character in mcbuffer when mclength is greater than zero, or we have
+ mclength zero, in which case there is a non-property character type in
+ op_previous. If prop_type/value are not negative, we have a property
+ character type in op_previous. */
+
+ oldcode = code; /* Save where we were */
+ code = previous; /* Usually overwrite previous item */
+
+ /* If the maximum is zero then the minimum must also be zero; Perl allows
+ this case, so we do too - by simply omitting the item altogether. */
+
+ if (repeat_max == 0) goto END_REPEAT;
+
+ /* Combine the op_type with the repeat_type */
+
+ repeat_type += op_type;
+
+ /* A minimum of zero is handled either as the special case * or ?, or as
+ an UPTO, with the maximum given. */
+
+ if (repeat_min == 0)
+ {
+ if (repeat_max == REPEAT_UNLIMITED) *code++ = OP_STAR + repeat_type;
+ else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
+ else
+ {
+ *code++ = OP_UPTO + repeat_type;
+ PUT2INC(code, 0, repeat_max);
+ }
+ }
+
+ /* A repeat minimum of 1 is optimized into some special cases. If the
+ maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
+ left in place and, if the maximum is greater than 1, we use OP_UPTO with
+ one less than the maximum. */
+
+ else if (repeat_min == 1)
+ {
+ if (repeat_max == REPEAT_UNLIMITED)
+ *code++ = OP_PLUS + repeat_type;
+ else
+ {
+ code = oldcode; /* Leave previous item in place */
+ if (repeat_max == 1) goto END_REPEAT;
+ *code++ = OP_UPTO + repeat_type;
+ PUT2INC(code, 0, repeat_max - 1);
+ }
+ }
+
+ /* The case {n,n} is just an EXACT, while the general case {n,m} is
+ handled as an EXACT followed by an UPTO or STAR or QUERY. */
+
+ else
+ {
+ *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
+ PUT2INC(code, 0, repeat_min);
+
+ /* Unless repeat_max equals repeat_min, fill in the data for EXACT,
+ and then generate the second opcode. For a repeated Unicode property
+ match, there are two extra values that define the required property,
+ and mclength is set zero to indicate this. */
+
+ if (repeat_max != repeat_min)
+ {
+ if (mclength > 0)
+ {
+ memcpy(code, mcbuffer, CU2BYTES(mclength));
+ code += mclength;
+ }
+ else
+ {
+ *code++ = op_previous;
+ if (prop_type >= 0)
+ {
+ *code++ = prop_type;
+ *code++ = prop_value;
+ }
+ }
+
+ /* Now set up the following opcode */
+
+ if (repeat_max == REPEAT_UNLIMITED)
+ *code++ = OP_STAR + repeat_type;
+ else
+ {
+ repeat_max -= repeat_min;
+ if (repeat_max == 1)
+ {
+ *code++ = OP_QUERY + repeat_type;
+ }
+ else
+ {
+ *code++ = OP_UPTO + repeat_type;
+ PUT2INC(code, 0, repeat_max);
+ }
+ }
+ }
+ }
+
+ /* Fill in the character or character type for the final opcode. */
+
+ if (mclength > 0)
+ {
+ memcpy(code, mcbuffer, CU2BYTES(mclength));
+ code += mclength;
+ }
+ else
+ {
+ *code++ = op_previous;
+ if (prop_type >= 0)
+ {
+ *code++ = prop_type;
+ *code++ = prop_value;
+ }
+ }
+ }
+ break;
+ } /* End of switch on different op_previous values */
+
+
+ /* If the character following a repeat is '+', possessive_quantifier is
+ TRUE. For some opcodes, there are special alternative opcodes for this
+ case. For anything else, we wrap the entire repeated item inside OP_ONCE
+ brackets. Logically, the '+' notation is just syntactic sugar, taken from
+ Sun's Java package, but the special opcodes can optimize it.
+
+ Some (but not all) possessively repeated subpatterns have already been
+ completely handled in the code just above. For them, possessive_quantifier
+ is always FALSE at this stage. Note that the repeated item starts at
+ tempcode, not at previous, which might be the first part of a string whose
+ (former) last char we repeated. */
+
+ if (possessive_quantifier)
+ {
+ int len;
+
+ /* Possessifying an EXACT quantifier has no effect, so we can ignore it.
+ However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6},
+ {5,}, or {5,10}). We skip over an EXACT item; if the length of what
+ remains is greater than zero, there's a further opcode that can be
+ handled. If not, do nothing, leaving the EXACT alone. */
+
+ switch(*tempcode)
+ {
+ case OP_TYPEEXACT:
+ tempcode += PRIV(OP_lengths)[*tempcode] +
+ ((tempcode[1 + IMM2_SIZE] == OP_PROP
+ || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
+ break;
+
+ /* CHAR opcodes are used for exacts whose count is 1. */
+
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_EXACT:
+ case OP_EXACTI:
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ tempcode += PRIV(OP_lengths)[*tempcode];
+#ifdef SUPPORT_UNICODE
+ if (utf && HAS_EXTRALEN(tempcode[-1]))
+ tempcode += GET_EXTRALEN(tempcode[-1]);
+#endif
+ break;
+
+ /* For the class opcodes, the repeat operator appears at the end;
+ adjust tempcode to point to it. */
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ tempcode += 1 + 32/sizeof(PCRE2_UCHAR);
+ break;
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ tempcode += GET(tempcode, 1);
+ break;
+#endif
+ }
+
+ /* If tempcode is equal to code (which points to the end of the repeated
+ item), it means we have skipped an EXACT item but there is no following
+ QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In
+ all other cases, tempcode will be pointing to the repeat opcode, and will
+ be less than code, so the value of len will be greater than 0. */
+
+ len = (int)(code - tempcode);
+ if (len > 0)
+ {
+ unsigned int repcode = *tempcode;
+
+ /* There is a table for possessifying opcodes, all of which are less
+ than OP_CALLOUT. A zero entry means there is no possessified version.
+ */
+
+ if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0)
+ *tempcode = opcode_possessify[repcode];
+
+ /* For opcode without a special possessified version, wrap the item in
+ ONCE brackets. */
+
+ else
+ {
+ (void)memmove(tempcode + 1 + LINK_SIZE, tempcode, CU2BYTES(len));
+ code += 1 + LINK_SIZE;
+ len += 1 + LINK_SIZE;
+ tempcode[0] = OP_ONCE;
+ *code++ = OP_KET;
+ PUTINC(code, 0, len);
+ PUT(tempcode, 1, len);
+ }
+ }
+ }
+
+ /* We set the "follows varying string" flag for subsequently encountered
+ reqcus if it isn't already set and we have just passed a varying length
+ item. */
+
+ END_REPEAT:
+ cb->req_varyopt |= reqvary;
+ break;
+
+
+ /* ===================================================================*/
+ /* Handle a 32-bit data character with a value greater than META_END. */
+
+ case META_BIGVALUE:
+ pptr++;
+ goto NORMAL_CHAR;
+
+
+ /* ===============================================================*/
+ /* Handle a back reference by number, which is the meta argument. The
+ pattern offsets for back references to group numbers less than 10 are held
+ in a special vector, to avoid using more than two parsed pattern elements
+ in 64-bit environments. We only need the offset to the first occurrence,
+ because if that doesn't fail, subsequent ones will also be OK. */
+
+ case META_BACKREF:
+ if (meta_arg < 10) offset = cb->small_ref_offset[meta_arg];
+ else GETPLUSOFFSET(offset, pptr);
+
+ if (meta_arg > cb->bracount)
+ {
+ cb->erroroffset = offset;
+ *errorcodeptr = ERR15; /* Non-existent subpattern */
+ return 0;
+ }
+
+ /* Come here from named backref handling when the reference is to a
+ single group (that is, not to a duplicated name). The back reference
+ data will have already been updated. We must disable firstcu if not
+ set, to cope with cases like (?=(\w+))\1: which would otherwise set ':'
+ later. */
+
+ HANDLE_SINGLE_REFERENCE:
+ if (firstcuflags == REQ_UNSET) zerofirstcuflags = firstcuflags = REQ_NONE;
+ *code++ = ((options & PCRE2_CASELESS) != 0)? OP_REFI : OP_REF;
+ PUT2INC(code, 0, meta_arg);
+
+ /* Update the map of back references, and keep the highest one. We
+ could do this in parse_regex() for numerical back references, but not
+ for named back references, because we don't know the numbers to which
+ named back references refer. So we do it all in this function. */
+
+ cb->backref_map |= (meta_arg < 32)? (1u << meta_arg) : 1;
+ if (meta_arg > cb->top_backref) cb->top_backref = meta_arg;
+
+ /* Check to see if this back reference is recursive, that it, it
+ is inside the group that it references. A flag is set so that the
+ group can be made atomic. */
+
+ for (oc = cb->open_caps; oc != NULL; oc = oc->next)
+ {
+ if (oc->number == meta_arg)
+ {
+ oc->flag = TRUE;
+ break;
+ }
+ }
+ break;
+
+
+ /* ===============================================================*/
+ /* Handle recursion by inserting the number of the called group (which is
+ the meta argument) after OP_RECURSE. At the end of compiling the pattern is
+ scanned and these numbers are replaced by offsets within the pattern. It is
+ done like this to avoid problems with forward references and adjusting
+ offsets when groups are duplicated and moved (as discovered in previous
+ implementations). Note that a recursion does not have a set first character
+ (relevant if it is repeated, because it will then be wrapped with ONCE
+ brackets). */
+
+ case META_RECURSE:
+ GETPLUSOFFSET(offset, pptr);
+ if (meta_arg > cb->bracount)
+ {
+ cb->erroroffset = offset;
+ *errorcodeptr = ERR15; /* Non-existent subpattern */
+ return 0;
+ }
+ HANDLE_NUMERICAL_RECURSION:
+ *code = OP_RECURSE;
+ PUT(code, 1, meta_arg);
+ code += 1 + LINK_SIZE;
+ groupsetfirstcu = FALSE;
+ cb->had_recurse = TRUE;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ break;
+
+
+ /* ===============================================================*/
+ /* Handle capturing parentheses; the number is the meta argument. */
+
+ case META_CAPTURE:
+ bravalue = OP_CBRA;
+ skipunits = IMM2_SIZE;
+ PUT2(code, 1+LINK_SIZE, meta_arg);
+ cb->lastcapture = meta_arg;
+ goto GROUP_PROCESS_NOTE_EMPTY;
+
+
+ /* ===============================================================*/
+ /* Handle escape sequence items. For ones like \d, the ESC_values are
+ arranged to be the same as the corresponding OP_values in the default case
+ when PCRE2_UCP is not set (which is the only case in which they will appear
+ here).
+
+ Note: \Q and \E are never seen here, as they were dealt with in
+ parse_pattern(). Neither are numerical back references or recursions, which
+ were turned into META_BACKREF or META_RECURSE items, respectively. \k and
+ \g, when followed by names, are turned into META_BACKREF_BYNAME or
+ META_RECURSE_BYNAME. */
+
+ case META_ESCAPE:
+
+ /* We can test for escape sequences that consume a character because their
+ values lie between ESC_b and ESC_Z; this may have to change if any new ones
+ are ever created. For these sequences, we disable the setting of a first
+ character if it hasn't already been set. */
+
+ if (meta_arg > ESC_b && meta_arg < ESC_Z)
+ {
+ matched_char = TRUE;
+ if (firstcuflags == REQ_UNSET) firstcuflags = REQ_NONE;
+ }
+
+ /* Set values to reset to if this is followed by a zero repeat. */
+
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+
+ /* If Unicode is not supported, \P and \p are not allowed and are
+ faulted at parse time, so will never appear here. */
+
+#ifdef SUPPORT_UNICODE
+ if (meta_arg == ESC_P || meta_arg == ESC_p)
+ {
+ uint32_t ptype = *(++pptr) >> 16;
+ uint32_t pdata = *pptr & 0xffff;
+ *code++ = (meta_arg == ESC_p)? OP_PROP : OP_NOTPROP;
+ *code++ = ptype;
+ *code++ = pdata;
+ break; /* End META_ESCAPE */
+ }
+#endif
+
+ /* For the rest (including \X when Unicode is supported - if not it's
+ faulted at parse time), the OP value is the escape value when PCRE2_UCP is
+ not set; if it is set, these escapes do not show up here because they are
+ converted into Unicode property tests in parse_regex(). Note that \b and \B
+ do a one-character lookbehind, and \A also behaves as if it does. */
+
+ if (meta_arg == ESC_C) cb->external_flags |= PCRE2_HASBKC; /* Record */
+ if ((meta_arg == ESC_b || meta_arg == ESC_B || meta_arg == ESC_A) &&
+ cb->max_lookbehind == 0)
+ cb->max_lookbehind = 1;
+
+ /* In non-UTF mode, and for both 32-bit modes, we turn \C into OP_ALLANY
+ instead of OP_ANYBYTE so that it works in DFA mode and in lookbehinds. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+ *code++ = (meta_arg == ESC_C)? OP_ALLANY : meta_arg;
+#else
+ *code++ = (!utf && meta_arg == ESC_C)? OP_ALLANY : meta_arg;
+#endif
+ break; /* End META_ESCAPE */
+
+
+ /* ===================================================================*/
+ /* Handle an unrecognized meta value. A parsed pattern value less than
+ META_END is a literal. Otherwise we have a problem. */
+
+ default:
+ if (meta >= META_END)
+ {
+#ifdef DEBUG_SHOW_PARSED
+ fprintf(stderr, "** Unrecognized parsed pattern item 0x%.8x\n", *pptr);
+#endif
+ *errorcodeptr = ERR89; /* Internal error - unrecognized. */
+ return 0;
+ }
+
+ /* Handle a literal character. We come here by goto in the case of a
+ 32-bit, non-UTF character whose value is greater than META_END. */
+
+ NORMAL_CHAR:
+ meta = *pptr; /* Get the full 32 bits */
+ NORMAL_CHAR_SET: /* Character is already in meta */
+ matched_char = TRUE;
+
+ /* For caseless UTF mode, check whether this character has more than one
+ other case. If so, generate a special OP_PROP item instead of OP_CHARI. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf && (options & PCRE2_CASELESS) != 0)
+ {
+ uint32_t caseset = UCD_CASESET(meta);
+ if (caseset != 0)
+ {
+ *code++ = OP_PROP;
+ *code++ = PT_CLIST;
+ *code++ = caseset;
+ if (firstcuflags == REQ_UNSET)
+ firstcuflags = zerofirstcuflags = REQ_NONE;
+ break; /* End handling this meta item */
+ }
+ }
+#endif
+
+ /* Caseful matches, or not one of the multicase characters. Get the
+ character's code units into mcbuffer, with the length in mclength. When not
+ in UTF mode, the length is always 1. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf) mclength = PRIV(ord2utf)(meta, mcbuffer); else
+#endif
+ {
+ mclength = 1;
+ mcbuffer[0] = meta;
+ }
+
+ /* Generate the appropriate code */
+
+ *code++ = ((options & PCRE2_CASELESS) != 0)? OP_CHARI : OP_CHAR;
+ memcpy(code, mcbuffer, CU2BYTES(mclength));
+ code += mclength;
+
+ /* Remember if \r or \n were seen */
+
+ if (mcbuffer[0] == CHAR_CR || mcbuffer[0] == CHAR_NL)
+ cb->external_flags |= PCRE2_HASCRORLF;
+
+ /* Set the first and required code units appropriately. If no previous
+ first code unit, set it from this character, but revert to none on a zero
+ repeat. Otherwise, leave the firstcu value alone, and don't change it on
+ a zero repeat. */
+
+ if (firstcuflags == REQ_UNSET)
+ {
+ zerofirstcuflags = REQ_NONE;
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+
+ /* If the character is more than one code unit long, we can set firstcu
+ only if it is not to be matched caselessly. */
+
+ if (mclength == 1 || req_caseopt == 0)
+ {
+ firstcu = mcbuffer[0];
+ firstcuflags = req_caseopt;
+ if (mclength != 1)
+ {
+ reqcu = code[-1];
+ reqcuflags = cb->req_varyopt;
+ }
+ }
+ else firstcuflags = reqcuflags = REQ_NONE;
+ }
+
+ /* firstcu was previously set; we can set reqcu only if the length is
+ 1 or the matching is caseful. */
+
+ else
+ {
+ zerofirstcu = firstcu;
+ zerofirstcuflags = firstcuflags;
+ zeroreqcu = reqcu;
+ zeroreqcuflags = reqcuflags;
+ if (mclength == 1 || req_caseopt == 0)
+ {
+ reqcu = code[-1];
+ reqcuflags = req_caseopt | cb->req_varyopt;
+ }
+ }
+ break; /* End default meta handling */
+ } /* End of big switch */
+ } /* End of big loop */
+
+/* Control never reaches here. */
+}
+
+
+
+/*************************************************
+* Compile regex: a sequence of alternatives *
+*************************************************/
+
+/* On entry, pptr is pointing past the bracket meta, but on return it points to
+the closing bracket or META_END. The code variable is pointing at the code unit
+into which the BRA operator has been stored. This function is used during the
+pre-compile phase when we are trying to find out the amount of memory needed,
+as well as during the real compile phase. The value of lengthptr distinguishes
+the two phases.
+
+Arguments:
+ options option bits, including any changes for this subpattern
+ codeptr -> the address of the current code pointer
+ pptrptr -> the address of the current parsed pattern pointer
+ errorcodeptr -> pointer to error code variable
+ skipunits skip this many code units at start (for brackets and OP_COND)
+ firstcuptr place to put the first required code unit
+ firstcuflagsptr place to put the first code unit flags, or a negative number
+ reqcuptr place to put the last required code unit
+ reqcuflagsptr place to put the last required code unit flags, or a negative number
+ bcptr pointer to the chain of currently open branches
+ cb points to the data block with tables pointers etc.
+ lengthptr NULL during the real compile phase
+ points to length accumulator during pre-compile phase
+
+Returns: 0 There has been an error
+ +1 Success, this group must match at least one character
+ -1 Success, this group may match an empty string
+*/
+
+static int
+compile_regex(uint32_t options, PCRE2_UCHAR **codeptr, uint32_t **pptrptr,
+ int *errorcodeptr, uint32_t skipunits, uint32_t *firstcuptr,
+ int32_t *firstcuflagsptr, uint32_t *reqcuptr,int32_t *reqcuflagsptr,
+ branch_chain *bcptr, compile_block *cb, PCRE2_SIZE *lengthptr)
+{
+PCRE2_UCHAR *code = *codeptr;
+PCRE2_UCHAR *last_branch = code;
+PCRE2_UCHAR *start_bracket = code;
+BOOL lookbehind;
+open_capitem capitem;
+int capnumber = 0;
+int okreturn = 1;
+uint32_t *pptr = *pptrptr;
+uint32_t firstcu, reqcu;
+uint32_t lookbehindlength;
+int32_t firstcuflags, reqcuflags;
+uint32_t branchfirstcu, branchreqcu;
+int32_t branchfirstcuflags, branchreqcuflags;
+PCRE2_SIZE length;
+branch_chain bc;
+
+/* If set, call the external function that checks for stack availability. */
+
+if (cb->cx->stack_guard != NULL &&
+ cb->cx->stack_guard(cb->parens_depth, cb->cx->stack_guard_data))
+ {
+ *errorcodeptr= ERR33;
+ return 0;
+ }
+
+/* Miscellaneous initialization */
+
+bc.outer = bcptr;
+bc.current_branch = code;
+
+firstcu = reqcu = 0;
+firstcuflags = reqcuflags = REQ_UNSET;
+
+/* Accumulate the length for use in the pre-compile phase. Start with the
+length of the BRA and KET and any extra code units that are required at the
+beginning. We accumulate in a local variable to save frequent testing of
+lengthptr for NULL. We cannot do this by looking at the value of 'code' at the
+start and end of each alternative, because compiled items are discarded during
+the pre-compile phase so that the workspace is not exceeded. */
+
+length = 2 + 2*LINK_SIZE + skipunits;
+
+/* Remember if this is a lookbehind assertion, and if it is, save its length
+and skip over the pattern offset. */
+
+lookbehind = *code == OP_ASSERTBACK || *code == OP_ASSERTBACK_NOT;
+if (lookbehind)
+ {
+ lookbehindlength = META_DATA(pptr[-1]);
+ pptr += SIZEOFFSET;
+ }
+else lookbehindlength = 0;
+
+/* If this is a capturing subpattern, add to the chain of open capturing items
+so that we can detect them if (*ACCEPT) is encountered. Note that only OP_CBRA
+need be tested here; changing this opcode to one of its variants, e.g.
+OP_SCBRAPOS, happens later, after the group has been compiled. */
+
+if (*code == OP_CBRA)
+ {
+ capnumber = GET2(code, 1 + LINK_SIZE);
+ capitem.number = capnumber;
+ capitem.next = cb->open_caps;
+ capitem.flag = FALSE;
+ capitem.assert_depth = cb->assert_depth;
+ cb->open_caps = &capitem;
+ }
+
+/* Offset is set zero to mark that this bracket is still open */
+
+PUT(code, 1, 0);
+code += 1 + LINK_SIZE + skipunits;
+
+/* Loop for each alternative branch */
+
+for (;;)
+ {
+ int branch_return;
+
+ /* Insert OP_REVERSE if this is as lookbehind assertion. */
+
+ if (lookbehind && lookbehindlength > 0)
+ {
+ *code++ = OP_REVERSE;
+ PUTINC(code, 0, lookbehindlength);
+ length += 1 + LINK_SIZE;
+ }
+
+ /* Now compile the branch; in the pre-compile phase its length gets added
+ into the length. */
+
+ if ((branch_return =
+ compile_branch(&options, &code, &pptr, errorcodeptr, &branchfirstcu,
+ &branchfirstcuflags, &branchreqcu, &branchreqcuflags, &bc,
+ cb, (lengthptr == NULL)? NULL : &length)) == 0)
+ return 0;
+
+ /* If a branch can match an empty string, so can the whole group. */
+
+ if (branch_return < 0) okreturn = -1;
+
+ /* In the real compile phase, there is some post-processing to be done. */
+
+ if (lengthptr == NULL)
+ {
+ /* If this is the first branch, the firstcu and reqcu values for the
+ branch become the values for the regex. */
+
+ if (*last_branch != OP_ALT)
+ {
+ firstcu = branchfirstcu;
+ firstcuflags = branchfirstcuflags;
+ reqcu = branchreqcu;
+ reqcuflags = branchreqcuflags;
+ }
+
+ /* If this is not the first branch, the first char and reqcu have to
+ match the values from all the previous branches, except that if the
+ previous value for reqcu didn't have REQ_VARY set, it can still match,
+ and we set REQ_VARY for the regex. */
+
+ else
+ {
+ /* If we previously had a firstcu, but it doesn't match the new branch,
+ we have to abandon the firstcu for the regex, but if there was
+ previously no reqcu, it takes on the value of the old firstcu. */
+
+ if (firstcuflags != branchfirstcuflags || firstcu != branchfirstcu)
+ {
+ if (firstcuflags >= 0)
+ {
+ if (reqcuflags < 0)
+ {
+ reqcu = firstcu;
+ reqcuflags = firstcuflags;
+ }
+ }
+ firstcuflags = REQ_NONE;
+ }
+
+ /* If we (now or from before) have no firstcu, a firstcu from the
+ branch becomes a reqcu if there isn't a branch reqcu. */
+
+ if (firstcuflags < 0 && branchfirstcuflags >= 0 &&
+ branchreqcuflags < 0)
+ {
+ branchreqcu = branchfirstcu;
+ branchreqcuflags = branchfirstcuflags;
+ }
+
+ /* Now ensure that the reqcus match */
+
+ if (((reqcuflags & ~REQ_VARY) != (branchreqcuflags & ~REQ_VARY)) ||
+ reqcu != branchreqcu)
+ reqcuflags = REQ_NONE;
+ else
+ {
+ reqcu = branchreqcu;
+ reqcuflags |= branchreqcuflags; /* To "or" REQ_VARY */
+ }
+ }
+ }
+
+ /* Handle reaching the end of the expression, either ')' or end of pattern.
+ In the real compile phase, go back through the alternative branches and
+ reverse the chain of offsets, with the field in the BRA item now becoming an
+ offset to the first alternative. If there are no alternatives, it points to
+ the end of the group. The length in the terminating ket is always the length
+ of the whole bracketed item. Return leaving the pointer at the terminating
+ char. */
+
+ if (META_CODE(*pptr) != META_ALT)
+ {
+ if (lengthptr == NULL)
+ {
+ PCRE2_SIZE branch_length = code - last_branch;
+ do
+ {
+ PCRE2_SIZE prev_length = GET(last_branch, 1);
+ PUT(last_branch, 1, branch_length);
+ branch_length = prev_length;
+ last_branch -= branch_length;
+ }
+ while (branch_length > 0);
+ }
+
+ /* Fill in the ket */
+
+ *code = OP_KET;
+ PUT(code, 1, (int)(code - start_bracket));
+ code += 1 + LINK_SIZE;
+
+ /* If it was a capturing subpattern, check to see if it contained any
+ recursive back references. If so, we must wrap it in atomic brackets. In
+ any event, remove the block from the chain. */
+
+ if (capnumber > 0)
+ {
+ if (cb->open_caps->flag)
+ {
+ (void)memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
+ CU2BYTES(code - start_bracket));
+ *start_bracket = OP_ONCE;
+ code += 1 + LINK_SIZE;
+ PUT(start_bracket, 1, (int)(code - start_bracket));
+ *code = OP_KET;
+ PUT(code, 1, (int)(code - start_bracket));
+ code += 1 + LINK_SIZE;
+ length += 2 + 2*LINK_SIZE;
+ }
+ cb->open_caps = cb->open_caps->next;
+ }
+
+ /* Set values to pass back */
+
+ *codeptr = code;
+ *pptrptr = pptr;
+ *firstcuptr = firstcu;
+ *firstcuflagsptr = firstcuflags;
+ *reqcuptr = reqcu;
+ *reqcuflagsptr = reqcuflags;
+ if (lengthptr != NULL)
+ {
+ if (OFLOW_MAX - *lengthptr < length)
+ {
+ *errorcodeptr = ERR20;
+ return 0;
+ }
+ *lengthptr += length;
+ }
+ return okreturn;
+ }
+
+ /* Another branch follows. In the pre-compile phase, we can move the code
+ pointer back to where it was for the start of the first branch. (That is,
+ pretend that each branch is the only one.)
+
+ In the real compile phase, insert an ALT node. Its length field points back
+ to the previous branch while the bracket remains open. At the end the chain
+ is reversed. It's done like this so that the start of the bracket has a
+ zero offset until it is closed, making it possible to detect recursion. */
+
+ if (lengthptr != NULL)
+ {
+ code = *codeptr + 1 + LINK_SIZE + skipunits;
+ length += 1 + LINK_SIZE;
+ }
+ else
+ {
+ *code = OP_ALT;
+ PUT(code, 1, (int)(code - last_branch));
+ bc.current_branch = last_branch = code;
+ code += 1 + LINK_SIZE;
+ }
+
+ /* Set the lookbehind length (if not in a lookbehind the value will be zero)
+ and then advance past the vertical bar. */
+
+ lookbehindlength = META_DATA(*pptr);
+ pptr++;
+ }
+/* Control never reaches here */
+}
+
+
+
+/*************************************************
+* Check for anchored pattern *
+*************************************************/
+
+/* Try to find out if this is an anchored regular expression. Consider each
+alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
+all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
+it's anchored. However, if this is a multiline pattern, then only OP_SOD will
+be found, because ^ generates OP_CIRCM in that mode.
+
+We can also consider a regex to be anchored if OP_SOM starts all its branches.
+This is the code for \G, which means "match at start of match position, taking
+into account the match offset".
+
+A branch is also implicitly anchored if it starts with .* and DOTALL is set,
+because that will try the rest of the pattern at all possible matching points,
+so there is no point trying again.... er ....
+
+.... except when the .* appears inside capturing parentheses, and there is a
+subsequent back reference to those parentheses. We haven't enough information
+to catch that case precisely.
+
+At first, the best we could do was to detect when .* was in capturing brackets
+and the highest back reference was greater than or equal to that level.
+However, by keeping a bitmap of the first 31 back references, we can catch some
+of the more common cases more precisely.
+
+... A second exception is when the .* appears inside an atomic group, because
+this prevents the number of characters it matches from being adjusted.
+
+Arguments:
+ code points to start of the compiled pattern
+ bracket_map a bitmap of which brackets we are inside while testing; this
+ handles up to substring 31; after that we just have to take
+ the less precise approach
+ cb points to the compile data block
+ atomcount atomic group level
+ inassert TRUE if in an assertion
+
+Returns: TRUE or FALSE
+*/
+
+static BOOL
+is_anchored(PCRE2_SPTR code, unsigned int bracket_map, compile_block *cb,
+ int atomcount, BOOL inassert)
+{
+do {
+ PCRE2_SPTR scode = first_significant_code(
+ code + PRIV(OP_lengths)[*code], FALSE);
+ int op = *scode;
+
+ /* Non-capturing brackets */
+
+ if (op == OP_BRA || op == OP_BRAPOS ||
+ op == OP_SBRA || op == OP_SBRAPOS)
+ {
+ if (!is_anchored(scode, bracket_map, cb, atomcount, inassert))
+ return FALSE;
+ }
+
+ /* Capturing brackets */
+
+ else if (op == OP_CBRA || op == OP_CBRAPOS ||
+ op == OP_SCBRA || op == OP_SCBRAPOS)
+ {
+ int n = GET2(scode, 1+LINK_SIZE);
+ int new_map = bracket_map | ((n < 32)? (1u << n) : 1);
+ if (!is_anchored(scode, new_map, cb, atomcount, inassert)) return FALSE;
+ }
+
+ /* Positive forward assertion */
+
+ else if (op == OP_ASSERT)
+ {
+ if (!is_anchored(scode, bracket_map, cb, atomcount, TRUE)) return FALSE;
+ }
+
+ /* Condition. If there is no second branch, it can't be anchored. */
+
+ else if (op == OP_COND || op == OP_SCOND)
+ {
+ if (scode[GET(scode,1)] != OP_ALT) return FALSE;
+ if (!is_anchored(scode, bracket_map, cb, atomcount, inassert))
+ return FALSE;
+ }
+
+ /* Atomic groups */
+
+ else if (op == OP_ONCE)
+ {
+ if (!is_anchored(scode, bracket_map, cb, atomcount + 1, inassert))
+ return FALSE;
+ }
+
+ /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
+ it isn't in brackets that are or may be referenced or inside an atomic
+ group or an assertion. Also the pattern must not contain *PRUNE or *SKIP,
+ because these break the feature. Consider, for example, /(?s).*?(*PRUNE)b/
+ with the subject "aab", which matches "b", i.e. not at the start of a line.
+ There is also an option that disables auto-anchoring. */
+
+ else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
+ op == OP_TYPEPOSSTAR))
+ {
+ if (scode[1] != OP_ALLANY || (bracket_map & cb->backref_map) != 0 ||
+ atomcount > 0 || cb->had_pruneorskip || inassert ||
+ (cb->external_options & PCRE2_NO_DOTSTAR_ANCHOR) != 0)
+ return FALSE;
+ }
+
+ /* Check for explicit anchoring */
+
+ else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
+
+ code += GET(code, 1);
+ }
+while (*code == OP_ALT); /* Loop for each alternative */
+return TRUE;
+}
+
+
+
+/*************************************************
+* Check for starting with ^ or .* *
+*************************************************/
+
+/* This is called to find out if every branch starts with ^ or .* so that
+"first char" processing can be done to speed things up in multiline
+matching and for non-DOTALL patterns that start with .* (which must start at
+the beginning or after \n). As in the case of is_anchored() (see above), we
+have to take account of back references to capturing brackets that contain .*
+because in that case we can't make the assumption. Also, the appearance of .*
+inside atomic brackets or in an assertion, or in a pattern that contains *PRUNE
+or *SKIP does not count, because once again the assumption no longer holds.
+
+Arguments:
+ code points to start of the compiled pattern or a group
+ bracket_map a bitmap of which brackets we are inside while testing; this
+ handles up to substring 31; after that we just have to take
+ the less precise approach
+ cb points to the compile data
+ atomcount atomic group level
+ inassert TRUE if in an assertion
+
+Returns: TRUE or FALSE
+*/
+
+static BOOL
+is_startline(PCRE2_SPTR code, unsigned int bracket_map, compile_block *cb,
+ int atomcount, BOOL inassert)
+{
+do {
+ PCRE2_SPTR scode = first_significant_code(
+ code + PRIV(OP_lengths)[*code], FALSE);
+ int op = *scode;
+
+ /* If we are at the start of a conditional assertion group, *both* the
+ conditional assertion *and* what follows the condition must satisfy the test
+ for start of line. Other kinds of condition fail. Note that there may be an
+ auto-callout at the start of a condition. */
+
+ if (op == OP_COND)
+ {
+ scode += 1 + LINK_SIZE;
+
+ if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT];
+ else if (*scode == OP_CALLOUT_STR) scode += GET(scode, 1 + 2*LINK_SIZE);
+
+ switch (*scode)
+ {
+ case OP_CREF:
+ case OP_DNCREF:
+ case OP_RREF:
+ case OP_DNRREF:
+ case OP_FAIL:
+ case OP_FALSE:
+ case OP_TRUE:
+ return FALSE;
+
+ default: /* Assertion */
+ if (!is_startline(scode, bracket_map, cb, atomcount, TRUE)) return FALSE;
+ do scode += GET(scode, 1); while (*scode == OP_ALT);
+ scode += 1 + LINK_SIZE;
+ break;
+ }
+ scode = first_significant_code(scode, FALSE);
+ op = *scode;
+ }
+
+ /* Non-capturing brackets */
+
+ if (op == OP_BRA || op == OP_BRAPOS ||
+ op == OP_SBRA || op == OP_SBRAPOS)
+ {
+ if (!is_startline(scode, bracket_map, cb, atomcount, inassert))
+ return FALSE;
+ }
+
+ /* Capturing brackets */
+
+ else if (op == OP_CBRA || op == OP_CBRAPOS ||
+ op == OP_SCBRA || op == OP_SCBRAPOS)
+ {
+ int n = GET2(scode, 1+LINK_SIZE);
+ int new_map = bracket_map | ((n < 32)? (1u << n) : 1);
+ if (!is_startline(scode, new_map, cb, atomcount, inassert)) return FALSE;
+ }
+
+ /* Positive forward assertions */
+
+ else if (op == OP_ASSERT)
+ {
+ if (!is_startline(scode, bracket_map, cb, atomcount, TRUE))
+ return FALSE;
+ }
+
+ /* Atomic brackets */
+
+ else if (op == OP_ONCE)
+ {
+ if (!is_startline(scode, bracket_map, cb, atomcount + 1, inassert))
+ return FALSE;
+ }
+
+ /* .* means "start at start or after \n" if it isn't in atomic brackets or
+ brackets that may be referenced or an assertion, and as long as the pattern
+ does not contain *PRUNE or *SKIP, because these break the feature. Consider,
+ for example, /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab",
+ i.e. not at the start of a line. There is also an option that disables this
+ optimization. */
+
+ else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
+ {
+ if (scode[1] != OP_ANY || (bracket_map & cb->backref_map) != 0 ||
+ atomcount > 0 || cb->had_pruneorskip || inassert ||
+ (cb->external_options & PCRE2_NO_DOTSTAR_ANCHOR) != 0)
+ return FALSE;
+ }
+
+ /* Check for explicit circumflex; anything else gives a FALSE result. Note
+ in particular that this includes atomic brackets OP_ONCE because the number
+ of characters matched by .* cannot be adjusted inside them. */
+
+ else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
+
+ /* Move on to the next alternative */
+
+ code += GET(code, 1);
+ }
+while (*code == OP_ALT); /* Loop for each alternative */
+return TRUE;
+}
+
+
+
+/*************************************************
+* Scan compiled regex for recursion reference *
+*************************************************/
+
+/* This function scans through a compiled pattern until it finds an instance of
+OP_RECURSE.
+
+Arguments:
+ code points to start of expression
+ utf TRUE in UTF mode
+
+Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
+*/
+
+static PCRE2_SPTR
+find_recurse(PCRE2_SPTR code, BOOL utf)
+{
+for (;;)
+ {
+ PCRE2_UCHAR c = *code;
+ if (c == OP_END) return NULL;
+ if (c == OP_RECURSE) return code;
+
+ /* XCLASS is used for classes that cannot be represented just by a bit map.
+ This includes negated single high-valued characters. CALLOUT_STR is used for
+ callouts with string arguments. In both cases the length in the table is
+ zero; the actual length is stored in the compiled code. */
+
+ if (c == OP_XCLASS) code += GET(code, 1);
+ else if (c == OP_CALLOUT_STR) code += GET(code, 1 + 2*LINK_SIZE);
+
+ /* Otherwise, we can get the item's length from the table, except that for
+ repeated character types, we have to test for \p and \P, which have an extra
+ two code units of parameters, and for MARK/PRUNE/SKIP/THEN with an argument,
+ we must add in its length. */
+
+ else
+ {
+ switch(c)
+ {
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
+ break;
+
+ case OP_TYPEPOSUPTO:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
+ code += 2;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ code += code[1];
+ break;
+ }
+
+ /* Add in the fixed length from the table */
+
+ code += PRIV(OP_lengths)[c];
+
+ /* In UTF-8 and UTF-16 modes, opcodes that are followed by a character may
+ be followed by a multi-unit character. The length in the table is a
+ minimum, so we have to arrange to skip the extra units. */
+
+#ifdef MAYBE_UTF_MULTI
+ if (utf) switch(c)
+ {
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_EXACT:
+ case OP_EXACTI:
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ case OP_UPTO:
+ case OP_UPTOI:
+ case OP_NOTUPTO:
+ case OP_NOTUPTOI:
+ case OP_MINUPTO:
+ case OP_MINUPTOI:
+ case OP_NOTMINUPTO:
+ case OP_NOTMINUPTOI:
+ case OP_POSUPTO:
+ case OP_POSUPTOI:
+ case OP_NOTPOSUPTO:
+ case OP_NOTPOSUPTOI:
+ case OP_STAR:
+ case OP_STARI:
+ case OP_NOTSTAR:
+ case OP_NOTSTARI:
+ case OP_MINSTAR:
+ case OP_MINSTARI:
+ case OP_NOTMINSTAR:
+ case OP_NOTMINSTARI:
+ case OP_POSSTAR:
+ case OP_POSSTARI:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSSTARI:
+ case OP_PLUS:
+ case OP_PLUSI:
+ case OP_NOTPLUS:
+ case OP_NOTPLUSI:
+ case OP_MINPLUS:
+ case OP_MINPLUSI:
+ case OP_NOTMINPLUS:
+ case OP_NOTMINPLUSI:
+ case OP_POSPLUS:
+ case OP_POSPLUSI:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSPLUSI:
+ case OP_QUERY:
+ case OP_QUERYI:
+ case OP_NOTQUERY:
+ case OP_NOTQUERYI:
+ case OP_MINQUERY:
+ case OP_MINQUERYI:
+ case OP_NOTMINQUERY:
+ case OP_NOTMINQUERYI:
+ case OP_POSQUERY:
+ case OP_POSQUERYI:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSQUERYI:
+ if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
+ break;
+ }
+#else
+ (void)(utf); /* Keep compiler happy by referencing function argument */
+#endif /* MAYBE_UTF_MULTI */
+ }
+ }
+}
+
+
+
+/*************************************************
+* Check for asserted fixed first code unit *
+*************************************************/
+
+/* During compilation, the "first code unit" settings from forward assertions
+are discarded, because they can cause conflicts with actual literals that
+follow. However, if we end up without a first code unit setting for an
+unanchored pattern, it is worth scanning the regex to see if there is an
+initial asserted first code unit. If all branches start with the same asserted
+code unit, or with a non-conditional bracket all of whose alternatives start
+with the same asserted code unit (recurse ad lib), then we return that code
+unit, with the flags set to zero or REQ_CASELESS; otherwise return zero with
+REQ_NONE in the flags.
+
+Arguments:
+ code points to start of compiled pattern
+ flags points to the first code unit flags
+ inassert non-zero if in an assertion
+
+Returns: the fixed first code unit, or 0 with REQ_NONE in flags
+*/
+
+static uint32_t
+find_firstassertedcu(PCRE2_SPTR code, int32_t *flags, uint32_t inassert)
+{
+uint32_t c = 0;
+int cflags = REQ_NONE;
+
+*flags = REQ_NONE;
+do {
+ uint32_t d;
+ int dflags;
+ int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
+ *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0;
+ PCRE2_SPTR scode = first_significant_code(code + 1+LINK_SIZE + xl, TRUE);
+ PCRE2_UCHAR op = *scode;
+
+ switch(op)
+ {
+ default:
+ return 0;
+
+ case OP_BRA:
+ case OP_BRAPOS:
+ case OP_CBRA:
+ case OP_SCBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ case OP_ASSERT:
+ case OP_ONCE:
+ d = find_firstassertedcu(scode, &dflags, inassert + ((op==OP_ASSERT)?1:0));
+ if (dflags < 0)
+ return 0;
+ if (cflags < 0) { c = d; cflags = dflags; }
+ else if (c != d || cflags != dflags) return 0;
+ break;
+
+ case OP_EXACT:
+ scode += IMM2_SIZE;
+ /* Fall through */
+
+ case OP_CHAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSPLUS:
+ if (inassert == 0) return 0;
+ if (cflags < 0) { c = scode[1]; cflags = 0; }
+ else if (c != scode[1]) return 0;
+ break;
+
+ case OP_EXACTI:
+ scode += IMM2_SIZE;
+ /* Fall through */
+
+ case OP_CHARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSPLUSI:
+ if (inassert == 0) return 0;
+ if (cflags < 0) { c = scode[1]; cflags = REQ_CASELESS; }
+ else if (c != scode[1]) return 0;
+ break;
+ }
+
+ code += GET(code, 1);
+ }
+while (*code == OP_ALT);
+
+*flags = cflags;
+return c;
+}
+
+
+
+/*************************************************
+* Add an entry to the name/number table *
+*************************************************/
+
+/* This function is called between compiling passes to add an entry to the
+name/number table, maintaining alphabetical order. Checking for permitted
+and forbidden duplicates has already been done.
+
+Arguments:
+ cb the compile data block
+ name the name to add
+ length the length of the name
+ groupno the group number
+ tablecount the count of names in the table so far
+
+Returns: nothing
+*/
+
+static void
+add_name_to_table(compile_block *cb, PCRE2_SPTR name, int length,
+ unsigned int groupno, uint32_t tablecount)
+{
+uint32_t i;
+PCRE2_UCHAR *slot = cb->name_table;
+
+for (i = 0; i < tablecount; i++)
+ {
+ int crc = memcmp(name, slot+IMM2_SIZE, CU2BYTES(length));
+ if (crc == 0 && slot[IMM2_SIZE+length] != 0)
+ crc = -1; /* Current name is a substring */
+
+ /* Make space in the table and break the loop for an earlier name. For a
+ duplicate or later name, carry on. We do this for duplicates so that in the
+ simple case (when ?(| is not used) they are in order of their numbers. In all
+ cases they are in the order in which they appear in the pattern. */
+
+ if (crc < 0)
+ {
+ (void)memmove(slot + cb->name_entry_size, slot,
+ CU2BYTES((tablecount - i) * cb->name_entry_size));
+ break;
+ }
+
+ /* Continue the loop for a later or duplicate name */
+
+ slot += cb->name_entry_size;
+ }
+
+PUT2(slot, 0, groupno);
+memcpy(slot + IMM2_SIZE, name, CU2BYTES(length));
+
+/* Add a terminating zero and fill the rest of the slot with zeroes so that
+the memory is all initialized. Otherwise valgrind moans about uninitialized
+memory when saving serialized compiled patterns. */
+
+memset(slot + IMM2_SIZE + length, 0,
+ CU2BYTES(cb->name_entry_size - length - IMM2_SIZE));
+}
+
+
+
+/*************************************************
+* Skip in parsed pattern *
+*************************************************/
+
+/* This function is called to skip parts of the parsed pattern when finding the
+length of a lookbehind branch. It is called after (*ACCEPT) and (*FAIL) to find
+the end of the branch, it is called to skip over an internal lookaround, and it
+is also called to skip to the end of a class, during which it will never
+encounter nested groups (but there's no need to have special code for that).
+
+When called to find the end of a branch or group, pptr must point to the first
+meta code inside the branch, not the branch-starting code. In other cases it
+can point to the item that causes the function to be called.
+
+Arguments:
+ pptr current pointer to skip from
+ skiptype PSKIP_CLASS when skipping to end of class
+ PSKIP_ALT when META_ALT ends the skip
+ PSKIP_KET when only META_KET ends the skip
+
+Returns: new value of pptr
+ NULL if META_END is reached - should never occur
+ or for an unknown meta value - likewise
+*/
+
+static uint32_t *
+parsed_skip(uint32_t *pptr, uint32_t skiptype)
+{
+uint32_t nestlevel = 0;
+
+for (;; pptr++)
+ {
+ uint32_t meta = META_CODE(*pptr);
+
+ switch(meta)
+ {
+ default: /* Just skip over most items */
+ if (meta < META_END) continue; /* Literal */
+ break;
+
+ /* This should never occur. */
+
+ case META_END:
+ return NULL;
+
+ /* The data for these items is variable in length. */
+
+ case META_BACKREF: /* Offset is present only if group >= 10 */
+ if (META_DATA(*pptr) >= 10) pptr += SIZEOFFSET;
+ break;
+
+ case META_ESCAPE: /* A few escapes are followed by data items. */
+ switch (META_DATA(*pptr))
+ {
+ case ESC_P:
+ case ESC_p:
+ pptr += 1;
+ break;
+
+ case ESC_g:
+ case ESC_k:
+ pptr += 1 + SIZEOFFSET;
+ break;
+ }
+ break;
+
+ case META_MARK: /* Add the length of the name. */
+ case META_COMMIT_ARG:
+ case META_PRUNE_ARG:
+ case META_SKIP_ARG:
+ case META_THEN_ARG:
+ pptr += pptr[1];
+ break;
+
+ /* These are the "active" items in this loop. */
+
+ case META_CLASS_END:
+ if (skiptype == PSKIP_CLASS) return pptr;
+ break;
+
+ case META_ATOMIC:
+ case META_CAPTURE:
+ case META_COND_ASSERT:
+ case META_COND_DEFINE:
+ case META_COND_NAME:
+ case META_COND_NUMBER:
+ case META_COND_RNAME:
+ case META_COND_RNUMBER:
+ case META_COND_VERSION:
+ case META_LOOKAHEAD:
+ case META_LOOKAHEADNOT:
+ case META_LOOKBEHIND:
+ case META_LOOKBEHINDNOT:
+ case META_NOCAPTURE:
+ nestlevel++;
+ break;
+
+ case META_ALT:
+ if (nestlevel == 0 && skiptype == PSKIP_ALT) return pptr;
+ break;
+
+ case META_KET:
+ if (nestlevel == 0) return pptr;
+ nestlevel--;
+ break;
+ }
+
+ /* The extra data item length for each meta is in a table. */
+
+ meta = (meta >> 16) & 0x7fff;
+ if (meta >= sizeof(meta_extra_lengths)) return NULL;
+ pptr += meta_extra_lengths[meta];
+ }
+/* Control never reaches here */
+return pptr;
+}
+
+
+
+/*************************************************
+* Find length of a parsed group *
+*************************************************/
+
+/* This is called for nested groups within a branch of a lookbehind whose
+length is being computed. If all the branches in the nested group have the same
+length, that is OK. On entry, the pointer must be at the first element after
+the group initializing code. On exit it points to OP_KET. Caching is used to
+improve processing speed when the same capturing group occurs many times.
+
+Arguments:
+ pptrptr pointer to pointer in the parsed pattern
+ isinline FALSE if a reference or recursion; TRUE for inline group
+ errcodeptr pointer to the errorcode
+ lcptr pointer to the loop counter
+ group number of captured group or -1 for a non-capturing group
+ recurses chain of recurse_check to catch mutual recursion
+ cb pointer to the compile data
+
+Returns: the group length or a negative number
+*/
+
+static int
+get_grouplength(uint32_t **pptrptr, BOOL isinline, int *errcodeptr, int *lcptr,
+ int group, parsed_recurse_check *recurses, compile_block *cb)
+{
+int branchlength;
+int grouplength = -1;
+
+/* The cache can be used only if there is no possibility of there being two
+groups with the same number. We do not need to set the end pointer for a group
+that is being processed as a back reference or recursion, but we must do so for
+an inline group. */
+
+if (group > 0 && (cb->external_flags & PCRE2_DUPCAPUSED) == 0)
+ {
+ uint32_t groupinfo = cb->groupinfo[group];
+ if ((groupinfo & GI_NOT_FIXED_LENGTH) != 0) return -1;
+ if ((groupinfo & GI_SET_FIXED_LENGTH) != 0)
+ {
+ if (isinline) *pptrptr = parsed_skip(*pptrptr, PSKIP_KET);
+ return groupinfo & GI_FIXED_LENGTH_MASK;
+ }
+ }
+
+/* Scan the group. In this case we find the end pointer of necessity. */
+
+for(;;)
+ {
+ branchlength = get_branchlength(pptrptr, errcodeptr, lcptr, recurses, cb);
+ if (branchlength < 0) goto ISNOTFIXED;
+ if (grouplength == -1) grouplength = branchlength;
+ else if (grouplength != branchlength) goto ISNOTFIXED;
+ if (**pptrptr == META_KET) break;
+ *pptrptr += 1; /* Skip META_ALT */
+ }
+
+if (group > 0)
+ cb->groupinfo[group] |= (uint32_t)(GI_SET_FIXED_LENGTH | grouplength);
+return grouplength;
+
+ISNOTFIXED:
+if (group > 0) cb->groupinfo[group] |= GI_NOT_FIXED_LENGTH;
+return -1;
+}
+
+
+
+/*************************************************
+* Find length of a parsed branch *
+*************************************************/
+
+/* Return a fixed length for a branch in a lookbehind, giving an error if the
+length is not fixed. If any lookbehinds are encountered on the way, they get
+their length set. On entry, *pptrptr points to the first element inside the
+branch. On exit it is set to point to the ALT or KET.
+
+Arguments:
+ pptrptr pointer to pointer in the parsed pattern
+ errcodeptr pointer to error code
+ lcptr pointer to loop counter
+ recurses chain of recurse_check to catch mutual recursion
+ cb pointer to compile block
+
+Returns: the length, or a negative value on error
+*/
+
+static int
+get_branchlength(uint32_t **pptrptr, int *errcodeptr, int *lcptr,
+ parsed_recurse_check *recurses, compile_block *cb)
+{
+int branchlength = 0;
+int grouplength;
+uint32_t lastitemlength = 0;
+uint32_t *pptr = *pptrptr;
+PCRE2_SIZE offset;
+parsed_recurse_check this_recurse;
+
+/* A large and/or complex regex can take too long to process. This can happen
+more often when (?| groups are present in the pattern because their length
+cannot be cached. */
+
+if ((*lcptr)++ > 2000)
+ {
+ *errcodeptr = ERR35; /* Lookbehind is too complicated */
+ return -1;
+ }
+
+/* Scan the branch, accumulating the length. */
+
+for (;; pptr++)
+ {
+ parsed_recurse_check *r;
+ uint32_t *gptr, *gptrend;
+ uint32_t escape;
+ uint32_t group = 0;
+ uint32_t itemlength = 0;
+
+ if (*pptr < META_END)
+ {
+ itemlength = 1;
+ }
+
+ else switch (META_CODE(*pptr))
+ {
+ case META_KET:
+ case META_ALT:
+ goto EXIT;
+
+ /* (*ACCEPT) and (*FAIL) terminate the branch, but we must skip to the
+ actual termination. */
+
+ case META_ACCEPT:
+ case META_FAIL:
+ pptr = parsed_skip(pptr, PSKIP_ALT);
+ if (pptr == NULL) goto PARSED_SKIP_FAILED;
+ goto EXIT;
+
+ case META_MARK:
+ case META_COMMIT_ARG:
+ case META_PRUNE_ARG:
+ case META_SKIP_ARG:
+ case META_THEN_ARG:
+ pptr += pptr[1] + 1;
+ break;
+
+ case META_CIRCUMFLEX:
+ case META_COMMIT:
+ case META_DOLLAR:
+ case META_PRUNE:
+ case META_SKIP:
+ case META_THEN:
+ break;
+
+ case META_OPTIONS:
+ pptr += 1;
+ break;
+
+ case META_BIGVALUE:
+ itemlength = 1;
+ pptr += 1;
+ break;
+
+ case META_CLASS:
+ case META_CLASS_NOT:
+ itemlength = 1;
+ pptr = parsed_skip(pptr, PSKIP_CLASS);
+ if (pptr == NULL) goto PARSED_SKIP_FAILED;
+ break;
+
+ case META_CLASS_EMPTY_NOT:
+ case META_DOT:
+ itemlength = 1;
+ break;
+
+ case META_CALLOUT_NUMBER:
+ pptr += 3;
+ break;
+
+ case META_CALLOUT_STRING:
+ pptr += 3 + SIZEOFFSET;
+ break;
+
+ /* Only some escapes consume a character. Of those, \R and \X are never
+ allowed because they might match more than character. \C is allowed only in
+ 32-bit and non-UTF 8/16-bit modes. */
+
+ case META_ESCAPE:
+ escape = META_DATA(*pptr);
+ if (escape == ESC_R || escape == ESC_X) return -1;
+ if (escape > ESC_b && escape < ESC_Z)
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 32
+ if ((cb->external_options & PCRE2_UTF) != 0 && escape == ESC_C)
+ {
+ *errcodeptr = ERR36;
+ return -1;
+ }
+#endif
+ itemlength = 1;
+ if (escape == ESC_p || escape == ESC_P) pptr++; /* Skip prop data */
+ }
+ break;
+
+ /* Lookaheads can be ignored, but we must start the skip inside the group
+ so that it isn't treated as a group within the branch. */
+
+ case META_LOOKAHEAD:
+ case META_LOOKAHEADNOT:
+ pptr = parsed_skip(pptr + 1, PSKIP_KET);
+ if (pptr == NULL) goto PARSED_SKIP_FAILED;
+
+ /* Also ignore any qualifiers that follow a lookahead assertion. */
+
+ switch (pptr[1])
+ {
+ case META_ASTERISK:
+ case META_ASTERISK_PLUS:
+ case META_ASTERISK_QUERY:
+ case META_PLUS:
+ case META_PLUS_PLUS:
+ case META_PLUS_QUERY:
+ case META_QUERY:
+ case META_QUERY_PLUS:
+ case META_QUERY_QUERY:
+ pptr++;
+ break;
+
+ case META_MINMAX:
+ case META_MINMAX_PLUS:
+ case META_MINMAX_QUERY:
+ pptr += 3;
+ break;
+
+ default:
+ break;
+ }
+ break;
+
+ /* Lookbehinds can be ignored, but must themselves be checked. */
+
+ case META_LOOKBEHIND:
+ case META_LOOKBEHINDNOT:
+ if (!set_lookbehind_lengths(&pptr, errcodeptr, lcptr, recurses, cb))
+ return -1;
+ break;
+
+ /* Back references and recursions are handled by very similar code. At this
+ stage, the names generated in the parsing pass are available, but the main
+ name table has not yet been created. So for the named varieties, scan the
+ list of names in order to get the number of the first one in the pattern,
+ and whether or not this name is duplicated. */
+
+ case META_BACKREF_BYNAME:
+ if ((cb->external_options & PCRE2_MATCH_UNSET_BACKREF) != 0)
+ goto ISNOTFIXED;
+ /* Fall through */
+
+ case META_RECURSE_BYNAME:
+ {
+ int i;
+ PCRE2_SPTR name;
+ BOOL is_dupname = FALSE;
+ named_group *ng = cb->named_groups;
+ uint32_t meta_code = META_CODE(*pptr);
+ uint32_t length = *(++pptr);
+
+ GETPLUSOFFSET(offset, pptr);
+ name = cb->start_pattern + offset;
+ for (i = 0; i < cb->names_found; i++, ng++)
+ {
+ if (length == ng->length && PRIV(strncmp)(name, ng->name, length) == 0)
+ {
+ group = ng->number;
+ is_dupname = ng->isdup;
+ break;
+ }
+ }
+
+ if (group == 0)
+ {
+ *errcodeptr = ERR15; /* Non-existent subpattern */
+ cb->erroroffset = offset;
+ return -1;
+ }
+
+ /* A numerical back reference can be fixed length if duplicate capturing
+ groups are not being used. A non-duplicate named back reference can also
+ be handled. */
+
+ if (meta_code == META_RECURSE_BYNAME ||
+ (!is_dupname && (cb->external_flags & PCRE2_DUPCAPUSED) == 0))
+ goto RECURSE_OR_BACKREF_LENGTH; /* Handle as a numbered version. */
+ }
+ goto ISNOTFIXED; /* Duplicate name or number */
+
+ /* The offset values for back references < 10 are in a separate vector
+ because otherwise they would use more than two parsed pattern elements on
+ 64-bit systems. */
+
+ case META_BACKREF:
+ if ((cb->external_options & PCRE2_MATCH_UNSET_BACKREF) != 0 ||
+ (cb->external_flags & PCRE2_DUPCAPUSED) != 0)
+ goto ISNOTFIXED;
+ group = META_DATA(*pptr);
+ if (group < 10)
+ {
+ offset = cb->small_ref_offset[group];
+ goto RECURSE_OR_BACKREF_LENGTH;
+ }
+
+ /* Fall through */
+ /* For groups >= 10 - picking up group twice does no harm. */
+
+ /* A true recursion implies not fixed length, but a subroutine call may
+ be OK. Back reference "recursions" are also failed. */
+
+ case META_RECURSE:
+ group = META_DATA(*pptr);
+ GETPLUSOFFSET(offset, pptr);
+
+ RECURSE_OR_BACKREF_LENGTH:
+ if (group > cb->bracount)
+ {
+ cb->erroroffset = offset;
+ *errcodeptr = ERR15; /* Non-existent subpattern */
+ return -1;
+ }
+ if (group == 0) goto ISNOTFIXED; /* Local recursion */
+ for (gptr = cb->parsed_pattern; *gptr != META_END; gptr++)
+ {
+ if (META_CODE(*gptr) == META_BIGVALUE) gptr++;
+ else if (*gptr == (META_CAPTURE | group)) break;
+ }
+
+ /* We must start the search for the end of the group at the first meta code
+ inside the group. Otherwise it will be treated as an enclosed group. */
+
+ gptrend = parsed_skip(gptr + 1, PSKIP_KET);
+ if (gptrend == NULL) goto PARSED_SKIP_FAILED;
+ if (pptr > gptr && pptr < gptrend) goto ISNOTFIXED; /* Local recursion */
+ for (r = recurses; r != NULL; r = r->prev) if (r->groupptr == gptr) break;
+ if (r != NULL) goto ISNOTFIXED; /* Mutual recursion */
+ this_recurse.prev = recurses;
+ this_recurse.groupptr = gptr;
+
+ /* We do not need to know the position of the end of the group, that is,
+ gptr is not used after the call to get_grouplength(). Setting the second
+ argument FALSE stops it scanning for the end when the length can be found
+ in the cache. */
+
+ gptr++;
+ grouplength = get_grouplength(&gptr, FALSE, errcodeptr, lcptr, group,
+ &this_recurse, cb);
+ if (grouplength < 0)
+ {
+ if (*errcodeptr == 0) goto ISNOTFIXED;
+ return -1; /* Error already set */
+ }
+ itemlength = grouplength;
+ break;
+
+ /* Check nested groups - advance past the initial data for each type and
+ then seek a fixed length with get_grouplength(). */
+
+ case META_COND_NAME:
+ case META_COND_NUMBER:
+ case META_COND_RNAME:
+ case META_COND_RNUMBER:
+ case META_COND_DEFINE:
+ pptr += 2 + SIZEOFFSET;
+ goto CHECK_GROUP;
+
+ case META_COND_ASSERT:
+ pptr += 1;
+ goto CHECK_GROUP;
+
+ case META_COND_VERSION:
+ pptr += 4;
+ goto CHECK_GROUP;
+
+ case META_CAPTURE:
+ group = META_DATA(*pptr);
+ /* Fall through */
+
+ case META_ATOMIC:
+ case META_NOCAPTURE:
+ pptr++;
+ CHECK_GROUP:
+ grouplength = get_grouplength(&pptr, TRUE, errcodeptr, lcptr, group,
+ recurses, cb);
+ if (grouplength < 0) return -1;
+ itemlength = grouplength;
+ break;
+
+ /* Exact repetition is OK; variable repetition is not. A repetition of zero
+ must subtract the length that has already been added. */
+
+ case META_MINMAX:
+ case META_MINMAX_PLUS:
+ case META_MINMAX_QUERY:
+ if (pptr[1] == pptr[2])
+ {
+ if (pptr[1] == 0) branchlength -= lastitemlength;
+ else itemlength = (pptr[1] - 1) * lastitemlength;
+ pptr += 2;
+ break;
+ }
+ /* Fall through */
+
+ /* Any other item means this branch does not have a fixed length. */
+
+ default:
+ ISNOTFIXED:
+ *errcodeptr = ERR25; /* Not fixed length */
+ return -1;
+ }
+
+ /* Add the item length to the branchlength, and save it for use if the next
+ thing is a quantifier. */
+
+ branchlength += itemlength;
+ lastitemlength = itemlength;
+
+ /* Ensure that the length does not overflow the limit. */
+
+ if (branchlength > LOOKBEHIND_MAX)
+ {
+ *errcodeptr = ERR87;
+ return -1;
+ }
+ }
+
+EXIT:
+*pptrptr = pptr;
+if (branchlength > cb->max_lookbehind) cb->max_lookbehind = branchlength;
+return branchlength;
+
+PARSED_SKIP_FAILED:
+*errcodeptr = ERR90;
+return -1;
+}
+
+
+
+/*************************************************
+* Set lengths in a lookbehind *
+*************************************************/
+
+/* This function is called for each lookbehind, to set the lengths in its
+branches. An error occurs if any branch does not have a fixed length that is
+less than the maximum (65535). On exit, the pointer must be left on the final
+ket.
+
+Arguments:
+ pptrptr pointer to pointer in the parsed pattern
+ errcodeptr pointer to error code
+ lcptr pointer to loop counter
+ recurses chain of recurse_check to catch mutual recursion
+ cb pointer to compile block
+
+Returns: TRUE if all is well
+ FALSE otherwise, with error code and offset set
+*/
+
+static BOOL
+set_lookbehind_lengths(uint32_t **pptrptr, int *errcodeptr, int *lcptr,
+ parsed_recurse_check *recurses, compile_block *cb)
+{
+PCRE2_SIZE offset;
+int branchlength;
+uint32_t *bptr = *pptrptr;
+
+READPLUSOFFSET(offset, bptr); /* Offset for error messages */
+*pptrptr += SIZEOFFSET;
+
+do
+ {
+ *pptrptr += 1;
+ branchlength = get_branchlength(pptrptr, errcodeptr, lcptr, recurses, cb);
+ if (branchlength < 0)
+ {
+ /* The errorcode and offset may already be set from a nested lookbehind. */
+ if (*errcodeptr == 0) *errcodeptr = ERR25;
+ if (cb->erroroffset == PCRE2_UNSET) cb->erroroffset = offset;
+ return FALSE;
+ }
+ *bptr |= branchlength; /* branchlength never more than 65535 */
+ bptr = *pptrptr;
+ }
+while (*bptr == META_ALT);
+
+return TRUE;
+}
+
+
+
+/*************************************************
+* Check parsed pattern lookbehinds *
+*************************************************/
+
+/* This function is called at the end of parsing a pattern if any lookbehinds
+were encountered. It scans the parsed pattern for them, calling
+set_lookbehind_lengths() for each one. At the start, the errorcode is zero and
+the error offset is marked unset. The enables the functions above not to
+override settings from deeper nestings.
+
+Arguments cb points to the compile block
+Returns: 0 on success, or an errorcode (cb->erroroffset will be set)
+*/
+
+static int
+check_lookbehinds(compile_block *cb)
+{
+uint32_t *pptr;
+int errorcode = 0;
+int loopcount = 0;
+
+cb->erroroffset = PCRE2_UNSET;
+
+for (pptr = cb->parsed_pattern; *pptr != META_END; pptr++)
+ {
+ if (*pptr < META_END) continue; /* Literal */
+
+ switch (META_CODE(*pptr))
+ {
+ default:
+ return ERR70; /* Unrecognized meta code */
+
+ case META_ESCAPE:
+ if (*pptr - META_ESCAPE == ESC_P || *pptr - META_ESCAPE == ESC_p)
+ pptr += 1;
+ break;
+
+ case META_ACCEPT:
+ case META_ALT:
+ case META_ASTERISK:
+ case META_ASTERISK_PLUS:
+ case META_ASTERISK_QUERY:
+ case META_ATOMIC:
+ case META_BACKREF:
+ case META_CAPTURE:
+ case META_CIRCUMFLEX:
+ case META_CLASS:
+ case META_CLASS_EMPTY:
+ case META_CLASS_EMPTY_NOT:
+ case META_CLASS_END:
+ case META_CLASS_NOT:
+ case META_COMMIT:
+ case META_COND_ASSERT:
+ case META_DOLLAR:
+ case META_DOT:
+ case META_FAIL:
+ case META_KET:
+ case META_LOOKAHEAD:
+ case META_LOOKAHEADNOT:
+ case META_NOCAPTURE:
+ case META_PLUS:
+ case META_PLUS_PLUS:
+ case META_PLUS_QUERY:
+ case META_PRUNE:
+ case META_QUERY:
+ case META_QUERY_PLUS:
+ case META_QUERY_QUERY:
+ case META_RANGE_ESCAPED:
+ case META_RANGE_LITERAL:
+ case META_SKIP:
+ case META_THEN:
+ break;
+
+ case META_RECURSE:
+ pptr += SIZEOFFSET;
+ break;
+
+ case META_BACKREF_BYNAME:
+ case META_COND_DEFINE:
+ case META_COND_NAME:
+ case META_COND_NUMBER:
+ case META_COND_RNAME:
+ case META_COND_RNUMBER:
+ case META_RECURSE_BYNAME:
+ pptr += 1 + SIZEOFFSET;
+ break;
+
+ case META_CALLOUT_STRING:
+ pptr += 3 + SIZEOFFSET;
+ break;
+
+ case META_BIGVALUE:
+ case META_OPTIONS:
+ case META_POSIX:
+ case META_POSIX_NEG:
+ pptr += 1;
+ break;
+
+ case META_MINMAX:
+ case META_MINMAX_QUERY:
+ case META_MINMAX_PLUS:
+ pptr += 2;
+ break;
+
+ case META_CALLOUT_NUMBER:
+ case META_COND_VERSION:
+ pptr += 3;
+ break;
+
+ case META_MARK:
+ case META_COMMIT_ARG:
+ case META_PRUNE_ARG:
+ case META_SKIP_ARG:
+ case META_THEN_ARG:
+ pptr += 1 + pptr[1];
+ break;
+
+ case META_LOOKBEHIND:
+ case META_LOOKBEHINDNOT:
+ if (!set_lookbehind_lengths(&pptr, &errorcode, &loopcount, NULL, cb))
+ return errorcode;
+ break;
+ }
+ }
+
+return 0;
+}
+
+
+
+/*************************************************
+* External function to compile a pattern *
+*************************************************/
+
+/* This function reads a regular expression in the form of a string and returns
+a pointer to a block of store holding a compiled version of the expression.
+
+Arguments:
+ pattern the regular expression
+ patlen the length of the pattern, or PCRE2_ZERO_TERMINATED
+ options option bits
+ errorptr pointer to errorcode
+ erroroffset pointer to error offset
+ ccontext points to a compile context or is NULL
+
+Returns: pointer to compiled data block, or NULL on error,
+ with errorcode and erroroffset set
+*/
+
+PCRE2_EXP_DEFN pcre2_code * PCRE2_CALL_CONVENTION
+pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE patlen, uint32_t options,
+ int *errorptr, PCRE2_SIZE *erroroffset, pcre2_compile_context *ccontext)
+{
+BOOL utf; /* Set TRUE for UTF mode */
+BOOL has_lookbehind = FALSE; /* Set TRUE if a lookbehind is found */
+BOOL zero_terminated; /* Set TRUE for zero-terminated pattern */
+pcre2_real_code *re = NULL; /* What we will return */
+compile_block cb; /* "Static" compile-time data */
+const uint8_t *tables; /* Char tables base pointer */
+
+PCRE2_UCHAR *code; /* Current pointer in compiled code */
+PCRE2_SPTR codestart; /* Start of compiled code */
+PCRE2_SPTR ptr; /* Current pointer in pattern */
+uint32_t *pptr; /* Current pointer in parsed pattern */
+
+PCRE2_SIZE length = 1; /* Allow for final END opcode */
+PCRE2_SIZE usedlength; /* Actual length used */
+PCRE2_SIZE re_blocksize; /* Size of memory block */
+PCRE2_SIZE big32count = 0; /* 32-bit literals >= 0x80000000 */
+PCRE2_SIZE parsed_size_needed; /* Needed for parsed pattern */
+
+int32_t firstcuflags, reqcuflags; /* Type of first/req code unit */
+uint32_t firstcu, reqcu; /* Value of first/req code unit */
+uint32_t setflags = 0; /* NL and BSR set flags */
+
+uint32_t skipatstart; /* When checking (*UTF) etc */
+uint32_t limit_heap = UINT32_MAX;
+uint32_t limit_match = UINT32_MAX; /* Unset match limits */
+uint32_t limit_depth = UINT32_MAX;
+
+int newline = 0; /* Unset; can be set by the pattern */
+int bsr = 0; /* Unset; can be set by the pattern */
+int errorcode = 0; /* Initialize to avoid compiler warn */
+int regexrc; /* Return from compile */
+
+uint32_t i; /* Local loop counter */
+
+/* Comments at the head of this file explain about these variables. */
+
+uint32_t stack_groupinfo[GROUPINFO_DEFAULT_SIZE];
+uint32_t stack_parsed_pattern[PARSED_PATTERN_DEFAULT_SIZE];
+named_group named_groups[NAMED_GROUP_LIST_SIZE];
+
+/* The workspace is used in different ways in the different compiling phases.
+It needs to be 16-bit aligned for the preliminary parsing scan. */
+
+uint32_t c16workspace[C16_WORK_SIZE];
+PCRE2_UCHAR *cworkspace = (PCRE2_UCHAR *)c16workspace;
+
+
+/* -------------- Check arguments and set up the pattern ----------------- */
+
+/* There must be error code and offset pointers. */
+
+if (errorptr == NULL || erroroffset == NULL) return NULL;
+*errorptr = ERR0;
+*erroroffset = 0;
+
+/* There must be a pattern! */
+
+if (pattern == NULL)
+ {
+ *errorptr = ERR16;
+ return NULL;
+ }
+
+/* A NULL compile context means "use a default context" */
+
+if (ccontext == NULL)
+ ccontext = (pcre2_compile_context *)(&PRIV(default_compile_context));
+
+/* Check that all undefined public option bits are zero. */
+
+if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0 ||
+ (ccontext->extra_options & ~PUBLIC_COMPILE_EXTRA_OPTIONS) != 0)
+ {
+ *errorptr = ERR17;
+ return NULL;
+ }
+
+if ((options & PCRE2_LITERAL) != 0 &&
+ ((options & ~PUBLIC_LITERAL_COMPILE_OPTIONS) != 0 ||
+ (ccontext->extra_options & ~PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS) != 0))
+ {
+ *errorptr = ERR92;
+ return NULL;
+ }
+
+/* A zero-terminated pattern is indicated by the special length value
+PCRE2_ZERO_TERMINATED. Check for an overlong pattern. */
+
+if ((zero_terminated = (patlen == PCRE2_ZERO_TERMINATED)))
+ patlen = PRIV(strlen)(pattern);
+
+if (patlen > ccontext->max_pattern_length)
+ {
+ *errorptr = ERR88;
+ return NULL;
+ }
+
+/* From here on, all returns from this function should end up going via the
+EXIT label. */
+
+
+/* ------------ Initialize the "static" compile data -------------- */
+
+tables = (ccontext->tables != NULL)? ccontext->tables : PRIV(default_tables);
+
+cb.lcc = tables + lcc_offset; /* Individual */
+cb.fcc = tables + fcc_offset; /* character */
+cb.cbits = tables + cbits_offset; /* tables */
+cb.ctypes = tables + ctypes_offset;
+
+cb.assert_depth = 0;
+cb.bracount = 0;
+cb.cx = ccontext;
+cb.dupnames = FALSE;
+cb.end_pattern = pattern + patlen;
+cb.erroroffset = 0;
+cb.external_flags = 0;
+cb.external_options = options;
+cb.groupinfo = stack_groupinfo;
+cb.had_recurse = FALSE;
+cb.lastcapture = 0;
+cb.max_lookbehind = 0;
+cb.name_entry_size = 0;
+cb.name_table = NULL;
+cb.named_groups = named_groups;
+cb.named_group_list_size = NAMED_GROUP_LIST_SIZE;
+cb.names_found = 0;
+cb.open_caps = NULL;
+cb.parens_depth = 0;
+cb.parsed_pattern = stack_parsed_pattern;
+cb.req_varyopt = 0;
+cb.start_code = cworkspace;
+cb.start_pattern = pattern;
+cb.start_workspace = cworkspace;
+cb.workspace_size = COMPILE_WORK_SIZE;
+
+/* Maximum back reference and backref bitmap. The bitmap records up to 31 back
+references to help in deciding whether (.*) can be treated as anchored or not.
+*/
+
+cb.top_backref = 0;
+cb.backref_map = 0;
+
+/* Escape sequences \1 to \9 are always back references, but as they are only
+two characters long, only two elements can be used in the parsed_pattern
+vector. The first contains the reference, and we'd like to use the second to
+record the offset in the pattern, so that forward references to non-existent
+groups can be diagnosed later with an offset. However, on 64-bit systems,
+PCRE2_SIZE won't fit. Instead, we have a vector of offsets for the first
+occurrence of \1 to \9, indexed by the second parsed_pattern value. All other
+references have enough space for the offset to be put into the parsed pattern.
+*/
+
+for (i = 0; i < 10; i++) cb.small_ref_offset[i] = PCRE2_UNSET;
+
+
+/* --------------- Start looking at the pattern --------------- */
+
+/* Unless PCRE2_LITERAL is set, check for global one-time option settings at
+the start of the pattern, and remember the offset to the actual regex. With
+valgrind support, make the terminator of a zero-terminated pattern
+inaccessible. This catches bugs that would otherwise only show up for
+non-zero-terminated patterns. */
+
+#ifdef SUPPORT_VALGRIND
+if (zero_terminated) VALGRIND_MAKE_MEM_NOACCESS(pattern + patlen, CU2BYTES(1));
+#endif
+
+ptr = pattern;
+skipatstart = 0;
+
+if ((options & PCRE2_LITERAL) == 0)
+ {
+ while (patlen - skipatstart >= 2 &&
+ ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&
+ ptr[skipatstart+1] == CHAR_ASTERISK)
+ {
+ for (i = 0; i < sizeof(pso_list)/sizeof(pso); i++)
+ {
+ uint32_t c, pp;
+ pso *p = pso_list + i;
+
+ if (patlen - skipatstart - 2 >= p->length &&
+ PRIV(strncmp_c8)(ptr + skipatstart + 2, (char *)(p->name),
+ p->length) == 0)
+ {
+ skipatstart += p->length + 2;
+ switch(p->type)
+ {
+ case PSO_OPT:
+ cb.external_options |= p->value;
+ break;
+
+ case PSO_FLG:
+ setflags |= p->value;
+ break;
+
+ case PSO_NL:
+ newline = p->value;
+ setflags |= PCRE2_NL_SET;
+ break;
+
+ case PSO_BSR:
+ bsr = p->value;
+ setflags |= PCRE2_BSR_SET;
+ break;
+
+ case PSO_LIMM:
+ case PSO_LIMD:
+ case PSO_LIMH:
+ c = 0;
+ pp = skipatstart;
+ if (!IS_DIGIT(ptr[pp]))
+ {
+ errorcode = ERR60;
+ ptr += pp;
+ goto HAD_EARLY_ERROR;
+ }
+ while (IS_DIGIT(ptr[pp]))
+ {
+ if (c > UINT32_MAX / 10 - 1) break; /* Integer overflow */
+ c = c*10 + (ptr[pp++] - CHAR_0);
+ }
+ if (ptr[pp++] != CHAR_RIGHT_PARENTHESIS)
+ {
+ errorcode = ERR60;
+ ptr += pp;
+ goto HAD_EARLY_ERROR;
+ }
+ if (p->type == PSO_LIMH) limit_heap = c;
+ else if (p->type == PSO_LIMM) limit_match = c;
+ else limit_depth = c;
+ skipatstart += pp - skipatstart;
+ break;
+ }
+ break; /* Out of the table scan loop */
+ }
+ }
+ if (i >= sizeof(pso_list)/sizeof(pso)) break; /* Out of pso loop */
+ }
+ }
+
+/* End of pattern-start options; advance to start of real regex. */
+
+ptr += skipatstart;
+
+/* Can't support UTF or UCP unless PCRE2 has been compiled with UTF support. */
+
+#ifndef SUPPORT_UNICODE
+if ((cb.external_options & (PCRE2_UTF|PCRE2_UCP)) != 0)
+ {
+ errorcode = ERR32;
+ goto HAD_EARLY_ERROR;
+ }
+#endif
+
+/* Check UTF. We have the original options in 'options', with that value as
+modified by (*UTF) etc in cb->external_options. The extra option
+PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is not permitted in UTF-16 mode because the
+surrogate code points cannot be represented in UTF-16. */
+
+utf = (cb.external_options & PCRE2_UTF) != 0;
+if (utf)
+ {
+ if ((options & PCRE2_NEVER_UTF) != 0)
+ {
+ errorcode = ERR74;
+ goto HAD_EARLY_ERROR;
+ }
+ if ((options & PCRE2_NO_UTF_CHECK) == 0 &&
+ (errorcode = PRIV(valid_utf)(pattern, patlen, erroroffset)) != 0)
+ goto HAD_ERROR; /* Offset was set by valid_utf() */
+
+#if PCRE2_CODE_UNIT_WIDTH == 16
+ if ((ccontext->extra_options & PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES) != 0)
+ {
+ errorcode = ERR91;
+ goto HAD_EARLY_ERROR;
+ }
+#endif
+ }
+
+/* Check UCP lockout. */
+
+if ((cb.external_options & (PCRE2_UCP|PCRE2_NEVER_UCP)) ==
+ (PCRE2_UCP|PCRE2_NEVER_UCP))
+ {
+ errorcode = ERR75;
+ goto HAD_EARLY_ERROR;
+ }
+
+/* Process the BSR setting. */
+
+if (bsr == 0) bsr = ccontext->bsr_convention;
+
+/* Process the newline setting. */
+
+if (newline == 0) newline = ccontext->newline_convention;
+cb.nltype = NLTYPE_FIXED;
+switch(newline)
+ {
+ case PCRE2_NEWLINE_CR:
+ cb.nllen = 1;
+ cb.nl[0] = CHAR_CR;
+ break;
+
+ case PCRE2_NEWLINE_LF:
+ cb.nllen = 1;
+ cb.nl[0] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_NUL:
+ cb.nllen = 1;
+ cb.nl[0] = CHAR_NUL;
+ break;
+
+ case PCRE2_NEWLINE_CRLF:
+ cb.nllen = 2;
+ cb.nl[0] = CHAR_CR;
+ cb.nl[1] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_ANY:
+ cb.nltype = NLTYPE_ANY;
+ break;
+
+ case PCRE2_NEWLINE_ANYCRLF:
+ cb.nltype = NLTYPE_ANYCRLF;
+ break;
+
+ default:
+ errorcode = ERR56;
+ goto HAD_EARLY_ERROR;
+ }
+
+/* Pre-scan the pattern to do two things: (1) Discover the named groups and
+their numerical equivalents, so that this information is always available for
+the remaining processing. (2) At the same time, parse the pattern and put a
+processed version into the parsed_pattern vector. This has escapes interpreted
+and comments removed (amongst other things).
+
+In all but one case, when PCRE2_AUTO_CALLOUT is not set, the number of unsigned
+32-bit ints in the parsed pattern is bounded by the length of the pattern plus
+one (for the terminator) plus four if PCRE2_EXTRA_WORD or PCRE2_EXTRA_LINE is
+set. The exceptional case is when running in 32-bit, non-UTF mode, when literal
+characters greater than META_END (0x80000000) have to be coded as two units. In
+this case, therefore, we scan the pattern to check for such values. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+if (!utf)
+ {
+ PCRE2_SPTR p;
+ for (p = ptr; p < cb.end_pattern; p++) if (*p >= META_END) big32count++;
+ }
+#endif
+
+/* Ensure that the parsed pattern buffer is big enough. When PCRE2_AUTO_CALLOUT
+is set we have to assume a numerical callout (4 elements) for each character
+plus one at the end. This is overkill, but memory is plentiful these days. For
+many smaller patterns the vector on the stack (which was set up above) can be
+used. */
+
+parsed_size_needed = patlen - skipatstart + big32count;
+
+if ((ccontext->extra_options &
+ (PCRE2_EXTRA_MATCH_WORD|PCRE2_EXTRA_MATCH_LINE)) != 0)
+ parsed_size_needed += 4;
+
+if ((options & PCRE2_AUTO_CALLOUT) != 0)
+ parsed_size_needed = (parsed_size_needed + 1) * 5;
+
+if (parsed_size_needed >= PARSED_PATTERN_DEFAULT_SIZE)
+ {
+ uint32_t *heap_parsed_pattern = ccontext->memctl.malloc(
+ (parsed_size_needed + 1) * sizeof(uint32_t), ccontext->memctl.memory_data);
+ if (heap_parsed_pattern == NULL)
+ {
+ *errorptr = ERR21;
+ goto EXIT;
+ }
+ cb.parsed_pattern = heap_parsed_pattern;
+ }
+cb.parsed_pattern_end = cb.parsed_pattern + parsed_size_needed + 1;
+
+/* Do the parsing scan. */
+
+errorcode = parse_regex(ptr, cb.external_options, &has_lookbehind, &cb);
+if (errorcode != 0) goto HAD_CB_ERROR;
+
+/* Workspace is needed to remember information about numbered groups: whether a
+group can match an empty string and what its fixed length is. This is done to
+avoid the possibility of recursive references causing very long compile times
+when checking these features. Unnumbered groups do not have this exposure since
+they cannot be referenced. We use an indexed vector for this purpose. If there
+are sufficiently few groups, the default vector on the stack, as set up above,
+can be used. Otherwise we have to get/free a special vector. The vector must be
+initialized to zero. */
+
+if (cb.bracount >= GROUPINFO_DEFAULT_SIZE)
+ {
+ cb.groupinfo = ccontext->memctl.malloc(
+ (cb.bracount + 1)*sizeof(uint32_t), ccontext->memctl.memory_data);
+ if (cb.groupinfo == NULL)
+ {
+ errorcode = ERR21;
+ cb.erroroffset = 0;
+ goto HAD_CB_ERROR;
+ }
+ }
+memset(cb.groupinfo, 0, (cb.bracount + 1) * sizeof(uint32_t));
+
+/* If there were any lookbehinds, scan the parsed pattern to figure out their
+lengths. */
+
+if (has_lookbehind)
+ {
+ errorcode = check_lookbehinds(&cb);
+ if (errorcode != 0) goto HAD_CB_ERROR;
+ }
+
+/* For debugging, there is a function that shows the parsed data vector. */
+
+#ifdef DEBUG_SHOW_PARSED
+fprintf(stderr, "+++ Pre-scan complete:\n");
+show_parsed(&cb);
+#endif
+
+/* For debugging capturing information this code can be enabled. */
+
+#ifdef DEBUG_SHOW_CAPTURES
+ {
+ named_group *ng = cb.named_groups;
+ fprintf(stderr, "+++Captures: %d\n", cb.bracount);
+ for (i = 0; i < cb.names_found; i++, ng++)
+ {
+ fprintf(stderr, "+++%3d %.*s\n", ng->number, ng->length, ng->name);
+ }
+ }
+#endif
+
+/* Pretend to compile the pattern while actually just accumulating the amount
+of memory required in the 'length' variable. This behaviour is triggered by
+passing a non-NULL final argument to compile_regex(). We pass a block of
+workspace (cworkspace) for it to compile parts of the pattern into; the
+compiled code is discarded when it is no longer needed, so hopefully this
+workspace will never overflow, though there is a test for its doing so.
+
+On error, errorcode will be set non-zero, so we don't need to look at the
+result of the function. The initial options have been put into the cb block,
+but we still have to pass a separate options variable (the first argument)
+because the options may change as the pattern is processed. */
+
+cb.erroroffset = patlen; /* For any subsequent errors that do not set it */
+pptr = cb.parsed_pattern;
+code = cworkspace;
+*code = OP_BRA;
+
+(void)compile_regex(cb.external_options, &code, &pptr, &errorcode, 0, &firstcu,
+ &firstcuflags, &reqcu, &reqcuflags, NULL, &cb, &length);
+
+if (errorcode != 0) goto HAD_CB_ERROR; /* Offset is in cb.erroroffset */
+
+/* This should be caught in compile_regex(), but just in case... */
+
+if (length > MAX_PATTERN_SIZE)
+ {
+ errorcode = ERR20;
+ goto HAD_CB_ERROR;
+ }
+
+/* Compute the size of, and then get and initialize, the data block for storing
+the compiled pattern and names table. Integer overflow should no longer be
+possible because nowadays we limit the maximum value of cb.names_found and
+cb.name_entry_size. */
+
+re_blocksize = sizeof(pcre2_real_code) +
+ CU2BYTES(length +
+ (PCRE2_SIZE)cb.names_found * (PCRE2_SIZE)cb.name_entry_size);
+re = (pcre2_real_code *)
+ ccontext->memctl.malloc(re_blocksize, ccontext->memctl.memory_data);
+if (re == NULL)
+ {
+ errorcode = ERR21;
+ goto HAD_CB_ERROR;
+ }
+
+/* The compiler may put padding at the end of the pcre2_real_code structure in
+order to round it up to a multiple of 4 or 8 bytes. This means that when a
+compiled pattern is copied (for example, when serialized) undefined bytes are
+read, and this annoys debuggers such as valgrind. To avoid this, we explicitly
+write to the last 8 bytes of the structure before setting the fields. */
+
+memset((char *)re + sizeof(pcre2_real_code) - 8, 0, 8);
+re->memctl = ccontext->memctl;
+re->tables = tables;
+re->executable_jit = NULL;
+memset(re->start_bitmap, 0, 32 * sizeof(uint8_t));
+re->blocksize = re_blocksize;
+re->magic_number = MAGIC_NUMBER;
+re->compile_options = options;
+re->overall_options = cb.external_options;
+re->extra_options = ccontext->extra_options;
+re->flags = PCRE2_CODE_UNIT_WIDTH/8 | cb.external_flags | setflags;
+re->limit_heap = limit_heap;
+re->limit_match = limit_match;
+re->limit_depth = limit_depth;
+re->first_codeunit = 0;
+re->last_codeunit = 0;
+re->bsr_convention = bsr;
+re->newline_convention = newline;
+re->max_lookbehind = 0;
+re->minlength = 0;
+re->top_bracket = 0;
+re->top_backref = 0;
+re->name_entry_size = cb.name_entry_size;
+re->name_count = cb.names_found;
+
+/* The basic block is immediately followed by the name table, and the compiled
+code follows after that. */
+
+codestart = (PCRE2_SPTR)((uint8_t *)re + sizeof(pcre2_real_code)) +
+ re->name_entry_size * re->name_count;
+
+/* Update the compile data block for the actual compile. The starting points of
+the name/number translation table and of the code are passed around in the
+compile data block. The start/end pattern and initial options are already set
+from the pre-compile phase, as is the name_entry_size field. */
+
+cb.parens_depth = 0;
+cb.assert_depth = 0;
+cb.lastcapture = 0;
+cb.name_table = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code));
+cb.start_code = codestart;
+cb.req_varyopt = 0;
+cb.had_accept = FALSE;
+cb.had_pruneorskip = FALSE;
+cb.open_caps = NULL;
+
+/* If any named groups were found, create the name/number table from the list
+created in the pre-pass. */
+
+if (cb.names_found > 0)
+ {
+ named_group *ng = cb.named_groups;
+ for (i = 0; i < cb.names_found; i++, ng++)
+ add_name_to_table(&cb, ng->name, ng->length, ng->number, i);
+ }
+
+/* Set up a starting, non-extracting bracket, then compile the expression. On
+error, errorcode will be set non-zero, so we don't need to look at the result
+of the function here. */
+
+pptr = cb.parsed_pattern;
+code = (PCRE2_UCHAR *)codestart;
+*code = OP_BRA;
+regexrc = compile_regex(re->overall_options, &code, &pptr, &errorcode, 0,
+ &firstcu, &firstcuflags, &reqcu, &reqcuflags, NULL, &cb, NULL);
+if (regexrc < 0) re->flags |= PCRE2_MATCH_EMPTY;
+re->top_bracket = cb.bracount;
+re->top_backref = cb.top_backref;
+re->max_lookbehind = cb.max_lookbehind;
+
+if (cb.had_accept)
+ {
+ reqcu = 0; /* Must disable after (*ACCEPT) */
+ reqcuflags = REQ_NONE;
+ }
+
+/* Fill in the final opcode and check for disastrous overflow. If no overflow,
+but the estimated length exceeds the really used length, adjust the value of
+re->blocksize, and if valgrind support is configured, mark the extra allocated
+memory as unaddressable, so that any out-of-bound reads can be detected. */
+
+*code++ = OP_END;
+usedlength = code - codestart;
+if (usedlength > length) errorcode = ERR23; else
+ {
+ re->blocksize -= CU2BYTES(length - usedlength);
+#ifdef SUPPORT_VALGRIND
+ VALGRIND_MAKE_MEM_NOACCESS(code, CU2BYTES(length - usedlength));
+#endif
+ }
+
+/* Scan the pattern for recursion/subroutine calls and convert the group
+numbers into offsets. Maintain a small cache so that repeated groups containing
+recursions are efficiently handled. */
+
+#define RSCAN_CACHE_SIZE 8
+
+if (errorcode == 0 && cb.had_recurse)
+ {
+ PCRE2_UCHAR *rcode;
+ PCRE2_SPTR rgroup;
+ unsigned int ccount = 0;
+ int start = RSCAN_CACHE_SIZE;
+ recurse_cache rc[RSCAN_CACHE_SIZE];
+
+ for (rcode = (PCRE2_UCHAR *)find_recurse(codestart, utf);
+ rcode != NULL;
+ rcode = (PCRE2_UCHAR *)find_recurse(rcode + 1 + LINK_SIZE, utf))
+ {
+ int p, groupnumber;
+
+ groupnumber = (int)GET(rcode, 1);
+ if (groupnumber == 0) rgroup = codestart; else
+ {
+ PCRE2_SPTR search_from = codestart;
+ rgroup = NULL;
+ for (i = 0, p = start; i < ccount; i++, p = (p + 1) & 7)
+ {
+ if (groupnumber == rc[p].groupnumber)
+ {
+ rgroup = rc[p].group;
+ break;
+ }
+
+ /* Group n+1 must always start to the right of group n, so we can save
+ search time below when the new group number is greater than any of the
+ previously found groups. */
+
+ if (groupnumber > rc[p].groupnumber) search_from = rc[p].group;
+ }
+
+ if (rgroup == NULL)
+ {
+ rgroup = PRIV(find_bracket)(search_from, utf, groupnumber);
+ if (rgroup == NULL)
+ {
+ errorcode = ERR53;
+ break;
+ }
+ if (--start < 0) start = RSCAN_CACHE_SIZE - 1;
+ rc[start].groupnumber = groupnumber;
+ rc[start].group = rgroup;
+ if (ccount < RSCAN_CACHE_SIZE) ccount++;
+ }
+ }
+
+ PUT(rcode, 1, rgroup - codestart);
+ }
+ }
+
+/* In rare debugging situations we sometimes need to look at the compiled code
+at this stage. */
+
+#ifdef DEBUG_CALL_PRINTINT
+pcre2_printint(re, stderr, TRUE);
+fprintf(stderr, "Length=%lu Used=%lu\n", length, usedlength);
+#endif
+
+/* Unless disabled, check whether any single character iterators can be
+auto-possessified. The function overwrites the appropriate opcode values, so
+the type of the pointer must be cast. NOTE: the intermediate variable "temp" is
+used in this code because at least one compiler gives a warning about loss of
+"const" attribute if the cast (PCRE2_UCHAR *)codestart is used directly in the
+function call. */
+
+if (errorcode == 0 && (re->overall_options & PCRE2_NO_AUTO_POSSESS) == 0)
+ {
+ PCRE2_UCHAR *temp = (PCRE2_UCHAR *)codestart;
+ if (PRIV(auto_possessify)(temp, utf, &cb) != 0) errorcode = ERR80;
+ }
+
+/* Failed to compile, or error while post-processing. */
+
+if (errorcode != 0) goto HAD_CB_ERROR;
+
+/* Successful compile. If the anchored option was not passed, set it if
+we can determine that the pattern is anchored by virtue of ^ characters or \A
+or anything else, such as starting with non-atomic .* when DOTALL is set and
+there are no occurrences of *PRUNE or *SKIP (though there is an option to
+disable this case). */
+
+if ((re->overall_options & PCRE2_ANCHORED) == 0 &&
+ is_anchored(codestart, 0, &cb, 0, FALSE))
+ re->overall_options |= PCRE2_ANCHORED;
+
+/* Set up the first code unit or startline flag, the required code unit, and
+then study the pattern. This code need not be obeyed if PCRE2_NO_START_OPTIMIZE
+is set, as the data it would create will not be used. Note that a first code
+unit (but not the startline flag) is useful for anchored patterns because it
+can still give a quick "no match" and also avoid searching for a last code
+unit. */
+
+if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ /* If we do not have a first code unit, see if there is one that is asserted
+ (these are not saved during the compile because they can cause conflicts with
+ actual literals that follow). */
+
+ if (firstcuflags < 0)
+ firstcu = find_firstassertedcu(codestart, &firstcuflags, 0);
+
+ /* Save the data for a first code unit. */
+
+ if (firstcuflags >= 0)
+ {
+ re->first_codeunit = firstcu;
+ re->flags |= PCRE2_FIRSTSET;
+
+ /* Handle caseless first code units. */
+
+ if ((firstcuflags & REQ_CASELESS) != 0)
+ {
+ if (firstcu < 128 || (!utf && firstcu < 255))
+ {
+ if (cb.fcc[firstcu] != firstcu) re->flags |= PCRE2_FIRSTCASELESS;
+ }
+
+ /* The first code unit is > 128 in UTF mode, or > 255 otherwise. In
+ 8-bit UTF mode, codepoints in the range 128-255 are introductory code
+ points and cannot have another case. In 16-bit and 32-bit modes, we can
+ check wide characters when UTF (and therefore UCP) is supported. */
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ else if (firstcu <= MAX_UTF_CODE_POINT &&
+ UCD_OTHERCASE(firstcu) != firstcu)
+ re->flags |= PCRE2_FIRSTCASELESS;
+#endif
+ }
+ }
+
+ /* When there is no first code unit, for non-anchored patterns, see if we can
+ set the PCRE2_STARTLINE flag. This is helpful for multiline matches when all
+ branches start with ^ and also when all branches start with non-atomic .* for
+ non-DOTALL matches when *PRUNE and SKIP are not present. (There is an option
+ that disables this case.) */
+
+ else if ((re->overall_options & PCRE2_ANCHORED) == 0 &&
+ is_startline(codestart, 0, &cb, 0, FALSE))
+ re->flags |= PCRE2_STARTLINE;
+
+ /* Handle the "required code unit", if one is set. In the case of an anchored
+ pattern, do this only if it follows a variable length item in the pattern. */
+
+ if (reqcuflags >= 0 &&
+ ((re->overall_options & PCRE2_ANCHORED) == 0 ||
+ (reqcuflags & REQ_VARY) != 0))
+ {
+ re->last_codeunit = reqcu;
+ re->flags |= PCRE2_LASTSET;
+
+ /* Handle caseless required code units as for first code units (above). */
+
+ if ((reqcuflags & REQ_CASELESS) != 0)
+ {
+ if (reqcu < 128 || (!utf && reqcu < 255))
+ {
+ if (cb.fcc[reqcu] != reqcu) re->flags |= PCRE2_LASTCASELESS;
+ }
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ else if (reqcu <= MAX_UTF_CODE_POINT && UCD_OTHERCASE(reqcu) != reqcu)
+ re->flags |= PCRE2_LASTCASELESS;
+#endif
+ }
+ }
+
+ /* Finally, study the compiled pattern to set up information such as a bitmap
+ of starting code units and a minimum matching length. */
+
+ if (PRIV(study)(re) != 0)
+ {
+ errorcode = ERR31;
+ goto HAD_CB_ERROR;
+ }
+ } /* End of start-of-match optimizations. */
+
+/* Control ends up here in all cases. When running under valgrind, make a
+pattern's terminating zero defined again. If memory was obtained for the parsed
+version of the pattern, free it before returning. Also free the list of named
+groups if a larger one had to be obtained, and likewise the group information
+vector. */
+
+EXIT:
+#ifdef SUPPORT_VALGRIND
+if (zero_terminated) VALGRIND_MAKE_MEM_DEFINED(pattern + patlen, CU2BYTES(1));
+#endif
+if (cb.parsed_pattern != stack_parsed_pattern)
+ ccontext->memctl.free(cb.parsed_pattern, ccontext->memctl.memory_data);
+if (cb.named_group_list_size > NAMED_GROUP_LIST_SIZE)
+ ccontext->memctl.free((void *)cb.named_groups, ccontext->memctl.memory_data);
+if (cb.groupinfo != stack_groupinfo)
+ ccontext->memctl.free((void *)cb.groupinfo, ccontext->memctl.memory_data);
+return re; /* Will be NULL after an error */
+
+/* Errors discovered in parse_regex() set the offset value in the compile
+block. Errors discovered before it is called must compute it from the ptr
+value. After parse_regex() is called, the offset in the compile block is set to
+the end of the pattern, but certain errors in compile_regex() may reset it if
+an offset is available in the parsed pattern. */
+
+HAD_CB_ERROR:
+ptr = pattern + cb.erroroffset;
+
+HAD_EARLY_ERROR:
+*erroroffset = ptr - pattern;
+
+HAD_ERROR:
+*errorptr = errorcode;
+pcre2_code_free(re);
+re = NULL;
+goto EXIT;
+}
+
+/* End of pcre2_compile.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_config.c b/test/monniaux/pcre2-10.32/pcre2_config.c
new file mode 100644
index 00000000..e487b102
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_config.c
@@ -0,0 +1,246 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2017 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* Save the configured link size, which is in bytes. In 16-bit and 32-bit modes
+its value gets changed by pcre2_internal.h to be in code units. */
+
+static int configured_link_size = LINK_SIZE;
+
+#include "pcre2_internal.h"
+
+/* These macros are the standard way of turning unquoted text into C strings.
+They allow macros like PCRE2_MAJOR to be defined without quotes, which is
+convenient for user programs that want to test their values. */
+
+#define STRING(a) # a
+#define XSTRING(s) STRING(s)
+
+
+/*************************************************
+* Return info about what features are configured *
+*************************************************/
+
+/* If where is NULL, the length of memory required is returned.
+
+Arguments:
+ what what information is required
+ where where to put the information
+
+Returns: 0 if a numerical value is returned
+ >= 0 if a string value
+ PCRE2_ERROR_BADOPTION if "where" not recognized
+ or JIT target requested when JIT not enabled
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_config(uint32_t what, void *where)
+{
+if (where == NULL) /* Requests a length */
+ {
+ switch(what)
+ {
+ default:
+ return PCRE2_ERROR_BADOPTION;
+
+ case PCRE2_CONFIG_BSR:
+ case PCRE2_CONFIG_COMPILED_WIDTHS:
+ case PCRE2_CONFIG_DEPTHLIMIT:
+ case PCRE2_CONFIG_HEAPLIMIT:
+ case PCRE2_CONFIG_JIT:
+ case PCRE2_CONFIG_LINKSIZE:
+ case PCRE2_CONFIG_MATCHLIMIT:
+ case PCRE2_CONFIG_NEVER_BACKSLASH_C:
+ case PCRE2_CONFIG_NEWLINE:
+ case PCRE2_CONFIG_PARENSLIMIT:
+ case PCRE2_CONFIG_STACKRECURSE: /* Obsolete */
+ case PCRE2_CONFIG_UNICODE:
+ return sizeof(uint32_t);
+
+ /* These are handled below */
+
+ case PCRE2_CONFIG_JITTARGET:
+ case PCRE2_CONFIG_UNICODE_VERSION:
+ case PCRE2_CONFIG_VERSION:
+ break;
+ }
+ }
+
+switch (what)
+ {
+ default:
+ return PCRE2_ERROR_BADOPTION;
+
+ case PCRE2_CONFIG_BSR:
+#ifdef BSR_ANYCRLF
+ *((uint32_t *)where) = PCRE2_BSR_ANYCRLF;
+#else
+ *((uint32_t *)where) = PCRE2_BSR_UNICODE;
+#endif
+ break;
+
+ case PCRE2_CONFIG_COMPILED_WIDTHS:
+ *((uint32_t *)where) = 0
+#ifdef SUPPORT_PCRE2_8
+ + 1
+#endif
+#ifdef SUPPORT_PCRE2_16
+ + 2
+#endif
+#ifdef SUPPORT_PCRE2_32
+ + 4
+#endif
+ ;
+ break;
+
+ case PCRE2_CONFIG_DEPTHLIMIT:
+ *((uint32_t *)where) = MATCH_LIMIT_DEPTH;
+ break;
+
+ case PCRE2_CONFIG_HEAPLIMIT:
+ *((uint32_t *)where) = HEAP_LIMIT;
+ break;
+
+ case PCRE2_CONFIG_JIT:
+#ifdef SUPPORT_JIT
+ *((uint32_t *)where) = 1;
+#else
+ *((uint32_t *)where) = 0;
+#endif
+ break;
+
+ case PCRE2_CONFIG_JITTARGET:
+#ifdef SUPPORT_JIT
+ {
+ const char *v = PRIV(jit_get_target)();
+ return (int)(1 + ((where == NULL)?
+ strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
+ }
+#else
+ return PCRE2_ERROR_BADOPTION;
+#endif
+
+ case PCRE2_CONFIG_LINKSIZE:
+ *((uint32_t *)where) = (uint32_t)configured_link_size;
+ break;
+
+ case PCRE2_CONFIG_MATCHLIMIT:
+ *((uint32_t *)where) = MATCH_LIMIT;
+ break;
+
+ case PCRE2_CONFIG_NEWLINE:
+ *((uint32_t *)where) = NEWLINE_DEFAULT;
+ break;
+
+ case PCRE2_CONFIG_NEVER_BACKSLASH_C:
+#ifdef NEVER_BACKSLASH_C
+ *((uint32_t *)where) = 1;
+#else
+ *((uint32_t *)where) = 0;
+#endif
+ break;
+
+ case PCRE2_CONFIG_PARENSLIMIT:
+ *((uint32_t *)where) = PARENS_NEST_LIMIT;
+ break;
+
+ /* This is now obsolete. The stack is no longer used via recursion for
+ handling backtracking in pcre2_match(). */
+
+ case PCRE2_CONFIG_STACKRECURSE:
+ *((uint32_t *)where) = 0;
+ break;
+
+ case PCRE2_CONFIG_UNICODE_VERSION:
+ {
+#if defined SUPPORT_UNICODE
+ const char *v = PRIV(unicode_version);
+#else
+ const char *v = "Unicode not supported";
+#endif
+ return (int)(1 + ((where == NULL)?
+ strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
+ }
+ break;
+
+ case PCRE2_CONFIG_UNICODE:
+#if defined SUPPORT_UNICODE
+ *((uint32_t *)where) = 1;
+#else
+ *((uint32_t *)where) = 0;
+#endif
+ break;
+
+ /* The hackery in setting "v" below is to cope with the case when
+ PCRE2_PRERELEASE is set to an empty string (which it is for real releases).
+ If the second alternative is used in this case, it does not leave a space
+ before the date. On the other hand, if all four macros are put into a single
+ XSTRING when PCRE2_PRERELEASE is not empty, an unwanted space is inserted.
+ There are problems using an "obvious" approach like this:
+
+ XSTRING(PCRE2_MAJOR) "." XSTRING(PCRE_MINOR)
+ XSTRING(PCRE2_PRERELEASE) " " XSTRING(PCRE_DATE)
+
+ because, when PCRE2_PRERELEASE is empty, this leads to an attempted expansion
+ of STRING(). The C standard states: "If (before argument substitution) any
+ argument consists of no preprocessing tokens, the behavior is undefined." It
+ turns out the gcc treats this case as a single empty string - which is what
+ we really want - but Visual C grumbles about the lack of an argument for the
+ macro. Unfortunately, both are within their rights. As there seems to be no
+ way to test for a macro's value being empty at compile time, we have to
+ resort to a runtime test. */
+
+ case PCRE2_CONFIG_VERSION:
+ {
+ const char *v = (XSTRING(Z PCRE2_PRERELEASE)[1] == 0)?
+ XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) :
+ XSTRING(PCRE2_MAJOR.PCRE2_MINOR) XSTRING(PCRE2_PRERELEASE PCRE2_DATE);
+ return (int)(1 + ((where == NULL)?
+ strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
+ }
+ }
+
+return 0;
+}
+
+/* End of pcre2_config.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_context.c b/test/monniaux/pcre2-10.32/pcre2_context.c
new file mode 100644
index 00000000..2c14df00
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_context.c
@@ -0,0 +1,476 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2017 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+
+/*************************************************
+* Default malloc/free functions *
+*************************************************/
+
+/* Ignore the "user data" argument in each case. */
+
+static void *default_malloc(size_t size, void *data)
+{
+(void)data;
+return malloc(size);
+}
+
+
+static void default_free(void *block, void *data)
+{
+(void)data;
+free(block);
+}
+
+
+
+/*************************************************
+* Get a block and save memory control *
+*************************************************/
+
+/* This internal function is called to get a block of memory in which the
+memory control data is to be stored at the start for future use.
+
+Arguments:
+ size amount of memory required
+ memctl pointer to a memctl block or NULL
+
+Returns: pointer to memory or NULL on failure
+*/
+
+extern void *
+PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
+{
+pcre2_memctl *newmemctl;
+void *yield = (memctl == NULL)? malloc(size) :
+ memctl->malloc(size, memctl->memory_data);
+if (yield == NULL) return NULL;
+newmemctl = (pcre2_memctl *)yield;
+if (memctl == NULL)
+ {
+ newmemctl->malloc = default_malloc;
+ newmemctl->free = default_free;
+ newmemctl->memory_data = NULL;
+ }
+else *newmemctl = *memctl;
+return yield;
+}
+
+
+
+/*************************************************
+* Create and initialize contexts *
+*************************************************/
+
+/* Initializing for compile and match contexts is done in separate, private
+functions so that these can be called from functions such as pcre2_compile()
+when an external context is not supplied. The initializing functions have an
+option to set up default memory management. */
+
+PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
+pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
+ void (*private_free)(void *, void *), void *memory_data)
+{
+pcre2_general_context *gcontext;
+if (private_malloc == NULL) private_malloc = default_malloc;
+if (private_free == NULL) private_free = default_free;
+gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);
+if (gcontext == NULL) return NULL;
+gcontext->memctl.malloc = private_malloc;
+gcontext->memctl.free = private_free;
+gcontext->memctl.memory_data = memory_data;
+return gcontext;
+}
+
+
+/* A default compile context is set up to save having to initialize at run time
+when no context is supplied to the compile function. */
+
+const pcre2_compile_context PRIV(default_compile_context) = {
+ { default_malloc, default_free, NULL }, /* Default memory handling */
+ NULL, /* Stack guard */
+ NULL, /* Stack guard data */
+ PRIV(default_tables), /* Character tables */
+ PCRE2_UNSET, /* Max pattern length */
+ BSR_DEFAULT, /* Backslash R default */
+ NEWLINE_DEFAULT, /* Newline convention */
+ PARENS_NEST_LIMIT, /* As it says */
+ 0 }; /* Extra options */
+
+/* The create function copies the default into the new memory, but must
+override the default memory handling functions if a gcontext was provided. */
+
+PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
+pcre2_compile_context_create(pcre2_general_context *gcontext)
+{
+pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
+ sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
+if (ccontext == NULL) return NULL;
+*ccontext = PRIV(default_compile_context);
+if (gcontext != NULL)
+ *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
+return ccontext;
+}
+
+
+/* A default match context is set up to save having to initialize at run time
+when no context is supplied to a match function. */
+
+const pcre2_match_context PRIV(default_match_context) = {
+ { default_malloc, default_free, NULL },
+#ifdef SUPPORT_JIT
+ NULL,
+ NULL,
+#endif
+ NULL,
+ NULL,
+ PCRE2_UNSET, /* Offset limit */
+ HEAP_LIMIT,
+ MATCH_LIMIT,
+ MATCH_LIMIT_DEPTH };
+
+/* The create function copies the default into the new memory, but must
+override the default memory handling functions if a gcontext was provided. */
+
+PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
+pcre2_match_context_create(pcre2_general_context *gcontext)
+{
+pcre2_match_context *mcontext = PRIV(memctl_malloc)(
+ sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
+if (mcontext == NULL) return NULL;
+*mcontext = PRIV(default_match_context);
+if (gcontext != NULL)
+ *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
+return mcontext;
+}
+
+
+/* A default convert context is set up to save having to initialize at run time
+when no context is supplied to the convert function. */
+
+const pcre2_convert_context PRIV(default_convert_context) = {
+ { default_malloc, default_free, NULL }, /* Default memory handling */
+#ifdef _WIN32
+ CHAR_BACKSLASH, /* Default path separator */
+ CHAR_GRAVE_ACCENT /* Default escape character */
+#else /* Not Windows */
+ CHAR_SLASH, /* Default path separator */
+ CHAR_BACKSLASH /* Default escape character */
+#endif
+ };
+
+/* The create function copies the default into the new memory, but must
+override the default memory handling functions if a gcontext was provided. */
+
+PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
+pcre2_convert_context_create(pcre2_general_context *gcontext)
+{
+pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
+ sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
+if (ccontext == NULL) return NULL;
+*ccontext = PRIV(default_convert_context);
+if (gcontext != NULL)
+ *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
+return ccontext;
+}
+
+
+/*************************************************
+* Context copy functions *
+*************************************************/
+
+PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
+pcre2_general_context_copy(pcre2_general_context *gcontext)
+{
+pcre2_general_context *new =
+ gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
+ gcontext->memctl.memory_data);
+if (new == NULL) return NULL;
+memcpy(new, gcontext, sizeof(pcre2_real_general_context));
+return new;
+}
+
+
+PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
+pcre2_compile_context_copy(pcre2_compile_context *ccontext)
+{
+pcre2_compile_context *new =
+ ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
+ ccontext->memctl.memory_data);
+if (new == NULL) return NULL;
+memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
+return new;
+}
+
+
+PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
+pcre2_match_context_copy(pcre2_match_context *mcontext)
+{
+pcre2_match_context *new =
+ mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
+ mcontext->memctl.memory_data);
+if (new == NULL) return NULL;
+memcpy(new, mcontext, sizeof(pcre2_real_match_context));
+return new;
+}
+
+
+
+PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
+pcre2_convert_context_copy(pcre2_convert_context *ccontext)
+{
+pcre2_convert_context *new =
+ ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
+ ccontext->memctl.memory_data);
+if (new == NULL) return NULL;
+memcpy(new, ccontext, sizeof(pcre2_real_convert_context));
+return new;
+}
+
+
+/*************************************************
+* Context free functions *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_general_context_free(pcre2_general_context *gcontext)
+{
+if (gcontext != NULL)
+ gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
+}
+
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_compile_context_free(pcre2_compile_context *ccontext)
+{
+if (ccontext != NULL)
+ ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
+}
+
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_match_context_free(pcre2_match_context *mcontext)
+{
+if (mcontext != NULL)
+ mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
+}
+
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_convert_context_free(pcre2_convert_context *ccontext)
+{
+if (ccontext != NULL)
+ ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
+}
+
+
+/*************************************************
+* Set values in contexts *
+*************************************************/
+
+/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
+data is given. Only some of the functions are able to test the validity of the
+data. */
+
+
+/* ------------ Compile context ------------ */
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_character_tables(pcre2_compile_context *ccontext,
+ const unsigned char *tables)
+{
+ccontext->tables = tables;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
+{
+switch(value)
+ {
+ case PCRE2_BSR_ANYCRLF:
+ case PCRE2_BSR_UNICODE:
+ ccontext->bsr_convention = value;
+ return 0;
+
+ default:
+ return PCRE2_ERROR_BADDATA;
+ }
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
+{
+ccontext->max_pattern_length = length;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
+{
+switch(newline)
+ {
+ case PCRE2_NEWLINE_CR:
+ case PCRE2_NEWLINE_LF:
+ case PCRE2_NEWLINE_CRLF:
+ case PCRE2_NEWLINE_ANY:
+ case PCRE2_NEWLINE_ANYCRLF:
+ case PCRE2_NEWLINE_NUL:
+ ccontext->newline_convention = newline;
+ return 0;
+
+ default:
+ return PCRE2_ERROR_BADDATA;
+ }
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
+{
+ccontext->parens_nest_limit = limit;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
+{
+ccontext->extra_options = options;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
+ int (*guard)(uint32_t, void *), void *user_data)
+{
+ccontext->stack_guard = guard;
+ccontext->stack_guard_data = user_data;
+return 0;
+}
+
+
+/* ------------ Match context ------------ */
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_callout(pcre2_match_context *mcontext,
+ int (*callout)(pcre2_callout_block *, void *), void *callout_data)
+{
+mcontext->callout = callout;
+mcontext->callout_data = callout_data;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
+{
+mcontext->heap_limit = limit;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
+{
+mcontext->match_limit = limit;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
+{
+mcontext->depth_limit = limit;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
+{
+mcontext->offset_limit = limit;
+return 0;
+}
+
+/* This function became obsolete at release 10.30. It is kept as a synonym for
+backwards compatibility. */
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
+{
+return pcre2_set_depth_limit(mcontext, limit);
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
+ void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
+ void *mydata)
+{
+(void)mcontext;
+(void)mymalloc;
+(void)myfree;
+(void)mydata;
+return 0;
+}
+
+/* ------------ Convert context ------------ */
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
+{
+if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
+ separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
+ccontext->glob_separator = separator;
+return 0;
+}
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
+{
+if (escape > 255 || (escape != 0 && !ispunct(escape)))
+ return PCRE2_ERROR_BADDATA;
+ccontext->glob_escape = escape;
+return 0;
+}
+
+/* End of pcre2_context.c */
+
diff --git a/test/monniaux/pcre2-10.32/pcre2_convert.c b/test/monniaux/pcre2-10.32/pcre2_convert.c
new file mode 100644
index 00000000..1dd5c337
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_convert.c
@@ -0,0 +1,1182 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+#define TYPE_OPTIONS (PCRE2_CONVERT_GLOB| \
+ PCRE2_CONVERT_POSIX_BASIC|PCRE2_CONVERT_POSIX_EXTENDED)
+
+#define ALL_OPTIONS (PCRE2_CONVERT_UTF|PCRE2_CONVERT_NO_UTF_CHECK| \
+ PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR| \
+ PCRE2_CONVERT_GLOB_NO_STARSTAR| \
+ TYPE_OPTIONS)
+
+#define DUMMY_BUFFER_SIZE 100
+
+/* Generated pattern fragments */
+
+#define STR_BACKSLASH_A STR_BACKSLASH STR_A
+#define STR_BACKSLASH_z STR_BACKSLASH STR_z
+#define STR_COLON_RIGHT_SQUARE_BRACKET STR_COLON STR_RIGHT_SQUARE_BRACKET
+#define STR_DOT_STAR_LOOKBEHIND STR_DOT STR_ASTERISK STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_LESS_THAN_SIGN STR_EQUALS_SIGN
+#define STR_LOOKAHEAD_NOT_DOT STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_EXCLAMATION_MARK STR_BACKSLASH STR_DOT STR_RIGHT_PARENTHESIS
+#define STR_QUERY_s STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_s STR_RIGHT_PARENTHESIS
+#define STR_STAR_NUL STR_LEFT_PARENTHESIS STR_ASTERISK STR_N STR_U STR_L STR_RIGHT_PARENTHESIS
+
+/* States for range and POSIX processing */
+
+enum { RANGE_NOT_STARTED, RANGE_STARTING, RANGE_STARTED };
+enum { POSIX_START_REGEX, POSIX_ANCHORED, POSIX_NOT_BRACKET,
+ POSIX_CLASS_NOT_STARTED, POSIX_CLASS_STARTING, POSIX_CLASS_STARTED };
+
+/* Macro to add a character string to the output buffer, checking for overflow. */
+
+#define PUTCHARS(string) \
+ { \
+ for (s = (char *)(string); *s != 0; s++) \
+ { \
+ if (p >= endp) return PCRE2_ERROR_NOMEMORY; \
+ *p++ = *s; \
+ } \
+ }
+
+/* Literals that must be escaped: \ ? * + | . ^ $ { } [ ] ( ) */
+
+static const char *pcre2_escaped_literals =
+ STR_BACKSLASH STR_QUESTION_MARK STR_ASTERISK STR_PLUS
+ STR_VERTICAL_LINE STR_DOT STR_CIRCUMFLEX_ACCENT STR_DOLLAR_SIGN
+ STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
+ STR_LEFT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET
+ STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS;
+
+/* Recognized escaped metacharacters in POSIX basic patterns. */
+
+static const char *posix_meta_escapes =
+ STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS
+ STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
+ STR_1 STR_2 STR_3 STR_4 STR_5 STR_6 STR_7 STR_8 STR_9;
+
+
+
+/*************************************************
+* Convert a POSIX pattern *
+*************************************************/
+
+/* This function handles both basic and extended POSIX patterns.
+
+Arguments:
+ pattype the pattern type
+ pattern the pattern
+ plength length in code units
+ utf TRUE if UTF
+ use_buffer where to put the output
+ use_length length of use_buffer
+ bufflenptr where to put the used length
+ dummyrun TRUE if a dummy run
+ ccontext the convert context
+
+Returns: 0 => success
+ !0 => error code
+*/
+
+static int
+convert_posix(uint32_t pattype, PCRE2_SPTR pattern, PCRE2_SIZE plength,
+ BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
+ PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
+{
+char *s;
+PCRE2_SPTR posix = pattern;
+PCRE2_UCHAR *p = use_buffer;
+PCRE2_UCHAR *pp = p;
+PCRE2_UCHAR *endp = p + use_length - 1; /* Allow for trailing zero */
+PCRE2_SIZE convlength = 0;
+
+uint32_t bracount = 0;
+uint32_t posix_state = POSIX_START_REGEX;
+uint32_t lastspecial = 0;
+BOOL extended = (pattype & PCRE2_CONVERT_POSIX_EXTENDED) != 0;
+BOOL nextisliteral = FALSE;
+
+(void)utf; /* Not used when Unicode not supported */
+(void)ccontext; /* Not currently used */
+
+/* Initialize default for error offset as end of input. */
+
+*bufflenptr = plength;
+PUTCHARS(STR_STAR_NUL);
+
+/* Now scan the input. */
+
+while (plength > 0)
+ {
+ uint32_t c, sc;
+ int clength = 1;
+
+ /* Add in the length of the last item, then, if in the dummy run, pull the
+ pointer back to the start of the (temporary) buffer and then remember the
+ start of the next item. */
+
+ convlength += p - pp;
+ if (dummyrun) p = use_buffer;
+ pp = p;
+
+ /* Pick up the next character */
+
+#ifndef SUPPORT_UNICODE
+ c = *posix;
+#else
+ GETCHARLENTEST(c, posix, clength);
+#endif
+ posix += clength;
+ plength -= clength;
+
+ sc = nextisliteral? 0 : c;
+ nextisliteral = FALSE;
+
+ /* Handle a character within a class. */
+
+ if (posix_state >= POSIX_CLASS_NOT_STARTED)
+ {
+ if (c == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
+ posix_state = POSIX_NOT_BRACKET;
+ }
+
+ /* Not the end of the class */
+
+ else
+ {
+ switch (posix_state)
+ {
+ case POSIX_CLASS_STARTED:
+ if (c <= 127 && islower(c)) break; /* Remain in started state */
+ posix_state = POSIX_CLASS_NOT_STARTED;
+ if (c == CHAR_COLON && plength > 0 &&
+ *posix == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ PUTCHARS(STR_COLON_RIGHT_SQUARE_BRACKET);
+ plength--;
+ posix++;
+ continue; /* With next character after :] */
+ }
+ /* Fall through */
+
+ case POSIX_CLASS_NOT_STARTED:
+ if (c == CHAR_LEFT_SQUARE_BRACKET)
+ posix_state = POSIX_CLASS_STARTING;
+ break;
+
+ case POSIX_CLASS_STARTING:
+ if (c == CHAR_COLON) posix_state = POSIX_CLASS_STARTED;
+ break;
+ }
+
+ if (c == CHAR_BACKSLASH) PUTCHARS(STR_BACKSLASH);
+ if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
+ memcpy(p, posix - clength, CU2BYTES(clength));
+ p += clength;
+ }
+ }
+
+ /* Handle a character not within a class. */
+
+ else switch(sc)
+ {
+ case CHAR_LEFT_SQUARE_BRACKET:
+ PUTCHARS(STR_LEFT_SQUARE_BRACKET);
+
+#ifdef NEVER
+ /* We could handle special cases [[:<:]] and [[:>:]] (which PCRE does
+ support) but they are not part of POSIX 1003.1. */
+
+ if (plength >= 6)
+ {
+ if (posix[0] == CHAR_LEFT_SQUARE_BRACKET &&
+ posix[1] == CHAR_COLON &&
+ (posix[2] == CHAR_LESS_THAN_SIGN ||
+ posix[2] == CHAR_GREATER_THAN_SIGN) &&
+ posix[3] == CHAR_COLON &&
+ posix[4] == CHAR_RIGHT_SQUARE_BRACKET &&
+ posix[5] == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ if (p + 6 > endp) return PCRE2_ERROR_NOMEMORY;
+ memcpy(p, posix, CU2BYTES(6));
+ p += 6;
+ posix += 6;
+ plength -= 6;
+ continue; /* With next character */
+ }
+ }
+#endif
+
+ /* Handle start of "normal" character classes */
+
+ posix_state = POSIX_CLASS_NOT_STARTED;
+
+ /* Handle ^ and ] as first characters */
+
+ if (plength > 0)
+ {
+ if (*posix == CHAR_CIRCUMFLEX_ACCENT)
+ {
+ posix++;
+ plength--;
+ PUTCHARS(STR_CIRCUMFLEX_ACCENT);
+ }
+ if (plength > 0 && *posix == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ posix++;
+ plength--;
+ PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
+ }
+ }
+ break;
+
+ case CHAR_BACKSLASH:
+ if (plength <= 0) return PCRE2_ERROR_END_BACKSLASH;
+ if (extended) nextisliteral = TRUE; else
+ {
+ if (*posix < 127 && strchr(posix_meta_escapes, *posix) != NULL)
+ {
+ if (isdigit(*posix)) PUTCHARS(STR_BACKSLASH);
+ if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
+ lastspecial = *p++ = *posix++;
+ plength--;
+ }
+ else nextisliteral = TRUE;
+ }
+ break;
+
+ case CHAR_RIGHT_PARENTHESIS:
+ if (!extended || bracount == 0) goto ESCAPE_LITERAL;
+ bracount--;
+ goto COPY_SPECIAL;
+
+ case CHAR_LEFT_PARENTHESIS:
+ bracount++;
+ /* Fall through */
+
+ case CHAR_QUESTION_MARK:
+ case CHAR_PLUS:
+ case CHAR_LEFT_CURLY_BRACKET:
+ case CHAR_RIGHT_CURLY_BRACKET:
+ case CHAR_VERTICAL_LINE:
+ if (!extended) goto ESCAPE_LITERAL;
+ /* Fall through */
+
+ case CHAR_DOT:
+ case CHAR_DOLLAR_SIGN:
+ posix_state = POSIX_NOT_BRACKET;
+ COPY_SPECIAL:
+ lastspecial = c;
+ if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
+ *p++ = c;
+ break;
+
+ case CHAR_ASTERISK:
+ if (lastspecial != CHAR_ASTERISK)
+ {
+ if (!extended && (posix_state < POSIX_NOT_BRACKET ||
+ lastspecial == CHAR_LEFT_PARENTHESIS))
+ goto ESCAPE_LITERAL;
+ goto COPY_SPECIAL;
+ }
+ break; /* Ignore second and subsequent asterisks */
+
+ case CHAR_CIRCUMFLEX_ACCENT:
+ if (extended) goto COPY_SPECIAL;
+ if (posix_state == POSIX_START_REGEX ||
+ lastspecial == CHAR_LEFT_PARENTHESIS)
+ {
+ posix_state = POSIX_ANCHORED;
+ goto COPY_SPECIAL;
+ }
+ /* Fall through */
+
+ default:
+ if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
+ {
+ ESCAPE_LITERAL:
+ PUTCHARS(STR_BACKSLASH);
+ }
+ lastspecial = 0xff; /* Indicates nothing special */
+ if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
+ memcpy(p, posix - clength, CU2BYTES(clength));
+ p += clength;
+ posix_state = POSIX_NOT_BRACKET;
+ break;
+ }
+ }
+
+if (posix_state >= POSIX_CLASS_NOT_STARTED)
+ return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
+convlength += p - pp; /* Final segment */
+*bufflenptr = convlength;
+*p++ = 0;
+return 0;
+}
+
+
+/*************************************************
+* Convert a glob pattern *
+*************************************************/
+
+/* Context for writing the output into a buffer. */
+
+typedef struct pcre2_output_context {
+ PCRE2_UCHAR *output; /* current output position */
+ PCRE2_SPTR output_end; /* output end */
+ PCRE2_SIZE output_size; /* size of the output */
+ uint8_t out_str[8]; /* string copied to the output */
+} pcre2_output_context;
+
+
+/* Write a character into the output.
+
+Arguments:
+ out output context
+ chr the next character
+*/
+
+static void
+convert_glob_write(pcre2_output_context *out, PCRE2_UCHAR chr)
+{
+out->output_size++;
+
+if (out->output < out->output_end)
+ *out->output++ = chr;
+}
+
+
+/* Write a string into the output.
+
+Arguments:
+ out output context
+ length length of out->out_str
+*/
+
+static void
+convert_glob_write_str(pcre2_output_context *out, PCRE2_SIZE length)
+{
+uint8_t *out_str = out->out_str;
+PCRE2_UCHAR *output = out->output;
+PCRE2_SPTR output_end = out->output_end;
+PCRE2_SIZE output_size = out->output_size;
+
+do
+ {
+ output_size++;
+
+ if (output < output_end)
+ *output++ = *out_str++;
+ }
+while (--length != 0);
+
+out->output = output;
+out->output_size = output_size;
+}
+
+
+/* Prints the separator into the output.
+
+Arguments:
+ out output context
+ separator glob separator
+ with_escape backslash is needed before separator
+*/
+
+static void
+convert_glob_print_separator(pcre2_output_context *out,
+ PCRE2_UCHAR separator, BOOL with_escape)
+{
+if (with_escape)
+ convert_glob_write(out, CHAR_BACKSLASH);
+
+convert_glob_write(out, separator);
+}
+
+
+/* Prints a wildcard into the output.
+
+Arguments:
+ out output context
+ separator glob separator
+ with_escape backslash is needed before separator
+*/
+
+static void
+convert_glob_print_wildcard(pcre2_output_context *out,
+ PCRE2_UCHAR separator, BOOL with_escape)
+{
+out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
+out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
+convert_glob_write_str(out, 2);
+
+convert_glob_print_separator(out, separator, with_escape);
+
+convert_glob_write(out, CHAR_RIGHT_SQUARE_BRACKET);
+}
+
+
+/* Parse a posix class.
+
+Arguments:
+ from starting point of scanning the range
+ pattern_end end of pattern
+ out output context
+
+Returns: >0 => class index
+ 0 => malformed class
+*/
+
+static int
+convert_glob_parse_class(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
+ pcre2_output_context *out)
+{
+static const char *posix_classes = "alnum:alpha:ascii:blank:cntrl:digit:"
+ "graph:lower:print:punct:space:upper:word:xdigit:";
+PCRE2_SPTR start = *from + 1;
+PCRE2_SPTR pattern = start;
+const char *class_ptr;
+PCRE2_UCHAR c;
+int class_index;
+
+while (TRUE)
+ {
+ if (pattern >= pattern_end) return 0;
+
+ c = *pattern++;
+
+ if (c < CHAR_a || c > CHAR_z) break;
+ }
+
+if (c != CHAR_COLON || pattern >= pattern_end ||
+ *pattern != CHAR_RIGHT_SQUARE_BRACKET)
+ return 0;
+
+class_ptr = posix_classes;
+class_index = 1;
+
+while (TRUE)
+ {
+ if (*class_ptr == CHAR_NUL) return 0;
+
+ pattern = start;
+
+ while (*pattern == (PCRE2_UCHAR) *class_ptr)
+ {
+ if (*pattern == CHAR_COLON)
+ {
+ pattern += 2;
+ start -= 2;
+
+ do convert_glob_write(out, *start++); while (start < pattern);
+
+ *from = pattern;
+ return class_index;
+ }
+ pattern++;
+ class_ptr++;
+ }
+
+ while (*class_ptr != CHAR_COLON) class_ptr++;
+ class_ptr++;
+ class_index++;
+ }
+}
+
+/* Checks whether the character is in the class.
+
+Arguments:
+ class_index class index
+ c character
+
+Returns: !0 => character is found in the class
+ 0 => otherwise
+*/
+
+static BOOL
+convert_glob_char_in_class(int class_index, PCRE2_UCHAR c)
+{
+switch (class_index)
+ {
+ case 1: return isalnum(c);
+ case 2: return isalpha(c);
+ case 3: return 1;
+ case 4: return c == CHAR_HT || c == CHAR_SPACE;
+ case 5: return iscntrl(c);
+ case 6: return isdigit(c);
+ case 7: return isgraph(c);
+ case 8: return islower(c);
+ case 9: return isprint(c);
+ case 10: return ispunct(c);
+ case 11: return isspace(c);
+ case 12: return isupper(c);
+ case 13: return isalnum(c) || c == CHAR_UNDERSCORE;
+ default: return isxdigit(c);
+ }
+}
+
+/* Parse a range of characters.
+
+Arguments:
+ from starting point of scanning the range
+ pattern_end end of pattern
+ out output context
+ separator glob separator
+ with_escape backslash is needed before separator
+
+Returns: 0 => success
+ !0 => error code
+*/
+
+static int
+convert_glob_parse_range(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
+ pcre2_output_context *out, BOOL utf, PCRE2_UCHAR separator,
+ BOOL with_escape, PCRE2_UCHAR escape, BOOL no_wildsep)
+{
+BOOL is_negative = FALSE;
+BOOL separator_seen = FALSE;
+BOOL has_prev_c;
+PCRE2_SPTR pattern = *from;
+PCRE2_SPTR char_start = NULL;
+uint32_t c, prev_c;
+int len, class_index;
+
+(void)utf; /* Avoid compiler warning. */
+
+if (pattern >= pattern_end)
+ {
+ *from = pattern;
+ return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
+ }
+
+if (*pattern == CHAR_EXCLAMATION_MARK
+ || *pattern == CHAR_CIRCUMFLEX_ACCENT)
+ {
+ pattern++;
+
+ if (pattern >= pattern_end)
+ {
+ *from = pattern;
+ return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
+ }
+
+ is_negative = TRUE;
+
+ out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
+ out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
+ len = 2;
+
+ if (!no_wildsep)
+ {
+ if (with_escape)
+ {
+ out->out_str[len] = CHAR_BACKSLASH;
+ len++;
+ }
+ out->out_str[len] = (uint8_t) separator;
+ }
+
+ convert_glob_write_str(out, len + 1);
+ }
+else
+ convert_glob_write(out, CHAR_LEFT_SQUARE_BRACKET);
+
+has_prev_c = FALSE;
+prev_c = 0;
+
+if (*pattern == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ out->out_str[0] = CHAR_BACKSLASH;
+ out->out_str[1] = CHAR_RIGHT_SQUARE_BRACKET;
+ convert_glob_write_str(out, 2);
+ has_prev_c = TRUE;
+ prev_c = CHAR_RIGHT_SQUARE_BRACKET;
+ pattern++;
+ }
+
+while (pattern < pattern_end)
+ {
+ char_start = pattern;
+ GETCHARINCTEST(c, pattern);
+
+ if (c == CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ convert_glob_write(out, c);
+
+ if (!is_negative && !no_wildsep && separator_seen)
+ {
+ out->out_str[0] = CHAR_LEFT_PARENTHESIS;
+ out->out_str[1] = CHAR_QUESTION_MARK;
+ out->out_str[2] = CHAR_LESS_THAN_SIGN;
+ out->out_str[3] = CHAR_EXCLAMATION_MARK;
+ convert_glob_write_str(out, 4);
+
+ convert_glob_print_separator(out, separator, with_escape);
+ convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
+ }
+
+ *from = pattern;
+ return 0;
+ }
+
+ if (pattern >= pattern_end) break;
+
+ if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
+ {
+ *from = pattern;
+ class_index = convert_glob_parse_class(from, pattern_end, out);
+
+ if (class_index != 0)
+ {
+ pattern = *from;
+
+ has_prev_c = FALSE;
+ prev_c = 0;
+
+ if (!is_negative &&
+ convert_glob_char_in_class (class_index, separator))
+ separator_seen = TRUE;
+ continue;
+ }
+ }
+ else if (c == CHAR_MINUS && has_prev_c &&
+ *pattern != CHAR_RIGHT_SQUARE_BRACKET)
+ {
+ convert_glob_write(out, CHAR_MINUS);
+
+ char_start = pattern;
+ GETCHARINCTEST(c, pattern);
+
+ if (pattern >= pattern_end) break;
+
+ if (escape != 0 && c == escape)
+ {
+ char_start = pattern;
+ GETCHARINCTEST(c, pattern);
+ }
+ else if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
+ {
+ *from = pattern;
+ return PCRE2_ERROR_CONVERT_SYNTAX;
+ }
+
+ if (prev_c > c)
+ {
+ *from = pattern;
+ return PCRE2_ERROR_CONVERT_SYNTAX;
+ }
+
+ if (prev_c < separator && separator < c) separator_seen = TRUE;
+
+ has_prev_c = FALSE;
+ prev_c = 0;
+ }
+ else
+ {
+ if (escape != 0 && c == escape)
+ {
+ char_start = pattern;
+ GETCHARINCTEST(c, pattern);
+
+ if (pattern >= pattern_end) break;
+ }
+
+ has_prev_c = TRUE;
+ prev_c = c;
+ }
+
+ if (c == CHAR_LEFT_SQUARE_BRACKET || c == CHAR_RIGHT_SQUARE_BRACKET ||
+ c == CHAR_BACKSLASH || c == CHAR_MINUS)
+ convert_glob_write(out, CHAR_BACKSLASH);
+
+ if (c == separator) separator_seen = TRUE;
+
+ do convert_glob_write(out, *char_start++); while (char_start < pattern);
+ }
+
+*from = pattern;
+return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
+}
+
+
+/* Prints a (*COMMIT) into the output.
+
+Arguments:
+ out output context
+*/
+
+static void
+convert_glob_print_commit(pcre2_output_context *out)
+{
+out->out_str[0] = CHAR_LEFT_PARENTHESIS;
+out->out_str[1] = CHAR_ASTERISK;
+out->out_str[2] = CHAR_C;
+out->out_str[3] = CHAR_O;
+out->out_str[4] = CHAR_M;
+out->out_str[5] = CHAR_M;
+out->out_str[6] = CHAR_I;
+out->out_str[7] = CHAR_T;
+convert_glob_write_str(out, 8);
+convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
+}
+
+
+/* Bash glob converter.
+
+Arguments:
+ pattype the pattern type
+ pattern the pattern
+ plength length in code units
+ utf TRUE if UTF
+ use_buffer where to put the output
+ use_length length of use_buffer
+ bufflenptr where to put the used length
+ dummyrun TRUE if a dummy run
+ ccontext the convert context
+
+Returns: 0 => success
+ !0 => error code
+*/
+
+static int
+convert_glob(uint32_t options, PCRE2_SPTR pattern, PCRE2_SIZE plength,
+ BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
+ PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
+{
+pcre2_output_context out;
+PCRE2_SPTR pattern_start = pattern;
+PCRE2_SPTR pattern_end = pattern + plength;
+PCRE2_UCHAR separator = ccontext->glob_separator;
+PCRE2_UCHAR escape = ccontext->glob_escape;
+PCRE2_UCHAR c;
+BOOL no_wildsep = (options & PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR) != 0;
+BOOL no_starstar = (options & PCRE2_CONVERT_GLOB_NO_STARSTAR) != 0;
+BOOL in_atomic = FALSE;
+BOOL after_starstar = FALSE;
+BOOL no_slash_z = FALSE;
+BOOL with_escape, is_start, after_separator;
+int result = 0;
+
+(void)utf; /* Avoid compiler warning. */
+
+#ifdef SUPPORT_UNICODE
+if (utf && (separator >= 128 || escape >= 128))
+ {
+ /* Currently only ASCII characters are supported. */
+ *bufflenptr = 0;
+ return PCRE2_ERROR_CONVERT_SYNTAX;
+ }
+#endif
+
+with_escape = strchr(pcre2_escaped_literals, separator) != NULL;
+
+/* Initialize default for error offset as end of input. */
+out.output = use_buffer;
+out.output_end = use_buffer + use_length;
+out.output_size = 0;
+
+out.out_str[0] = CHAR_LEFT_PARENTHESIS;
+out.out_str[1] = CHAR_QUESTION_MARK;
+out.out_str[2] = CHAR_s;
+out.out_str[3] = CHAR_RIGHT_PARENTHESIS;
+convert_glob_write_str(&out, 4);
+
+is_start = TRUE;
+
+if (pattern < pattern_end && pattern[0] == CHAR_ASTERISK)
+ {
+ if (no_wildsep)
+ is_start = FALSE;
+ else if (!no_starstar && pattern + 1 < pattern_end &&
+ pattern[1] == CHAR_ASTERISK)
+ is_start = FALSE;
+ }
+
+if (is_start)
+ {
+ out.out_str[0] = CHAR_BACKSLASH;
+ out.out_str[1] = CHAR_A;
+ convert_glob_write_str(&out, 2);
+ }
+
+while (pattern < pattern_end)
+ {
+ c = *pattern++;
+
+ if (c == CHAR_ASTERISK)
+ {
+ is_start = pattern == pattern_start + 1;
+
+ if (in_atomic)
+ {
+ convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
+ in_atomic = FALSE;
+ }
+
+ if (!no_starstar && pattern < pattern_end && *pattern == CHAR_ASTERISK)
+ {
+ after_separator = is_start || (pattern[-2] == separator);
+
+ do pattern++; while (pattern < pattern_end &&
+ *pattern == CHAR_ASTERISK);
+
+ if (pattern >= pattern_end)
+ {
+ no_slash_z = TRUE;
+ break;
+ }
+
+ after_starstar = TRUE;
+
+ if (after_separator && escape != 0 && *pattern == escape &&
+ pattern + 1 < pattern_end && pattern[1] == separator)
+ pattern++;
+
+ if (is_start)
+ {
+ if (*pattern != separator) continue;
+
+ out.out_str[0] = CHAR_LEFT_PARENTHESIS;
+ out.out_str[1] = CHAR_QUESTION_MARK;
+ out.out_str[2] = CHAR_COLON;
+ out.out_str[3] = CHAR_BACKSLASH;
+ out.out_str[4] = CHAR_A;
+ out.out_str[5] = CHAR_VERTICAL_LINE;
+ convert_glob_write_str(&out, 6);
+
+ convert_glob_print_separator(&out, separator, with_escape);
+ convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
+
+ pattern++;
+ continue;
+ }
+
+ convert_glob_print_commit(&out);
+
+ if (!after_separator || *pattern != separator)
+ {
+ out.out_str[0] = CHAR_DOT;
+ out.out_str[1] = CHAR_ASTERISK;
+ out.out_str[2] = CHAR_QUESTION_MARK;
+ convert_glob_write_str(&out, 3);
+ continue;
+ }
+
+ out.out_str[0] = CHAR_LEFT_PARENTHESIS;
+ out.out_str[1] = CHAR_QUESTION_MARK;
+ out.out_str[2] = CHAR_COLON;
+ out.out_str[3] = CHAR_DOT;
+ out.out_str[4] = CHAR_ASTERISK;
+ out.out_str[5] = CHAR_QUESTION_MARK;
+
+ convert_glob_write_str(&out, 6);
+
+ convert_glob_print_separator(&out, separator, with_escape);
+
+ out.out_str[0] = CHAR_RIGHT_PARENTHESIS;
+ out.out_str[1] = CHAR_QUESTION_MARK;
+ out.out_str[2] = CHAR_QUESTION_MARK;
+ convert_glob_write_str(&out, 3);
+
+ pattern++;
+ continue;
+ }
+
+ if (pattern < pattern_end && *pattern == CHAR_ASTERISK)
+ {
+ do pattern++; while (pattern < pattern_end &&
+ *pattern == CHAR_ASTERISK);
+ }
+
+ if (no_wildsep)
+ {
+ if (pattern >= pattern_end)
+ {
+ no_slash_z = TRUE;
+ break;
+ }
+
+ /* Start check must be after the end check. */
+ if (is_start) continue;
+ }
+
+ if (!is_start)
+ {
+ if (after_starstar)
+ {
+ out.out_str[0] = CHAR_LEFT_PARENTHESIS;
+ out.out_str[1] = CHAR_QUESTION_MARK;
+ out.out_str[2] = CHAR_GREATER_THAN_SIGN;
+ convert_glob_write_str(&out, 3);
+ in_atomic = TRUE;
+ }
+ else
+ convert_glob_print_commit(&out);
+ }
+
+ if (no_wildsep)
+ convert_glob_write(&out, CHAR_DOT);
+ else
+ convert_glob_print_wildcard(&out, separator, with_escape);
+
+ out.out_str[0] = CHAR_ASTERISK;
+ out.out_str[1] = CHAR_QUESTION_MARK;
+ if (pattern >= pattern_end)
+ out.out_str[1] = CHAR_PLUS;
+ convert_glob_write_str(&out, 2);
+ continue;
+ }
+
+ if (c == CHAR_QUESTION_MARK)
+ {
+ if (no_wildsep)
+ convert_glob_write(&out, CHAR_DOT);
+ else
+ convert_glob_print_wildcard(&out, separator, with_escape);
+ continue;
+ }
+
+ if (c == CHAR_LEFT_SQUARE_BRACKET)
+ {
+ result = convert_glob_parse_range(&pattern, pattern_end,
+ &out, utf, separator, with_escape, escape, no_wildsep);
+ if (result != 0) break;
+ continue;
+ }
+
+ if (escape != 0 && c == escape)
+ {
+ if (pattern >= pattern_end)
+ {
+ result = PCRE2_ERROR_CONVERT_SYNTAX;
+ break;
+ }
+ c = *pattern++;
+ }
+
+ if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
+ convert_glob_write(&out, CHAR_BACKSLASH);
+
+ convert_glob_write(&out, c);
+ }
+
+if (result == 0)
+ {
+ if (!no_slash_z)
+ {
+ out.out_str[0] = CHAR_BACKSLASH;
+ out.out_str[1] = CHAR_z;
+ convert_glob_write_str(&out, 2);
+ }
+
+ if (in_atomic)
+ convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
+
+ convert_glob_write(&out, CHAR_NUL);
+
+ if (!dummyrun && out.output_size != (PCRE2_SIZE) (out.output - use_buffer))
+ result = PCRE2_ERROR_NOMEMORY;
+ }
+
+if (result != 0)
+ {
+ *bufflenptr = pattern - pattern_start;
+ return result;
+ }
+
+*bufflenptr = out.output_size - 1;
+return 0;
+}
+
+
+/*************************************************
+* Convert pattern *
+*************************************************/
+
+/* This is the external-facing function for converting other forms of pattern
+into PCRE2 regular expression patterns. On error, the bufflenptr argument is
+used to return an offset in the original pattern.
+
+Arguments:
+ pattern the input pattern
+ plength length of input, or PCRE2_ZERO_TERMINATED
+ options options bits
+ buffptr pointer to pointer to output buffer
+ bufflenptr pointer to length of output buffer
+ ccontext convert context or NULL
+
+Returns: 0 for success, else an error code (+ve or -ve)
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_pattern_convert(PCRE2_SPTR pattern, PCRE2_SIZE plength, uint32_t options,
+ PCRE2_UCHAR **buffptr, PCRE2_SIZE *bufflenptr,
+ pcre2_convert_context *ccontext)
+{
+int i, rc;
+PCRE2_UCHAR dummy_buffer[DUMMY_BUFFER_SIZE];
+PCRE2_UCHAR *use_buffer = dummy_buffer;
+PCRE2_SIZE use_length = DUMMY_BUFFER_SIZE;
+BOOL utf = (options & PCRE2_CONVERT_UTF) != 0;
+uint32_t pattype = options & TYPE_OPTIONS;
+
+if (pattern == NULL || bufflenptr == NULL) return PCRE2_ERROR_NULL;
+
+if ((options & ~ALL_OPTIONS) != 0 || /* Undefined bit set */
+ (pattype & (~pattype+1)) != pattype || /* More than one type set */
+ pattype == 0) /* No type set */
+ {
+ *bufflenptr = 0; /* Error offset */
+ return PCRE2_ERROR_BADOPTION;
+ }
+
+if (plength == PCRE2_ZERO_TERMINATED) plength = PRIV(strlen)(pattern);
+if (ccontext == NULL) ccontext =
+ (pcre2_convert_context *)(&PRIV(default_convert_context));
+
+/* Check UTF if required. */
+
+#ifndef SUPPORT_UNICODE
+if (utf)
+ {
+ *bufflenptr = 0; /* Error offset */
+ return PCRE2_ERROR_UNICODE_NOT_SUPPORTED;
+ }
+#else
+if (utf && (options & PCRE2_CONVERT_NO_UTF_CHECK) == 0)
+ {
+ PCRE2_SIZE erroroffset;
+ rc = PRIV(valid_utf)(pattern, plength, &erroroffset);
+ if (rc != 0)
+ {
+ *bufflenptr = erroroffset;
+ return rc;
+ }
+ }
+#endif
+
+/* If buffptr is not NULL, and what it points to is not NULL, we are being
+provided with a buffer and a length, so set them as the buffer to use. */
+
+if (buffptr != NULL && *buffptr != NULL)
+ {
+ use_buffer = *buffptr;
+ use_length = *bufflenptr;
+ }
+
+/* Call an individual converter, either just once (if a buffer was provided or
+just the length is needed), or twice (if a memory allocation is required). */
+
+for (i = 0; i < 2; i++)
+ {
+ PCRE2_UCHAR *allocated;
+ BOOL dummyrun = buffptr == NULL || *buffptr == NULL;
+
+ switch(pattype)
+ {
+ case PCRE2_CONVERT_GLOB:
+ rc = convert_glob(options & ~PCRE2_CONVERT_GLOB, pattern, plength, utf,
+ use_buffer, use_length, bufflenptr, dummyrun, ccontext);
+ break;
+
+ case PCRE2_CONVERT_POSIX_BASIC:
+ case PCRE2_CONVERT_POSIX_EXTENDED:
+ rc = convert_posix(pattype, pattern, plength, utf, use_buffer, use_length,
+ bufflenptr, dummyrun, ccontext);
+ break;
+
+ default:
+ *bufflenptr = 0; /* Error offset */
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ if (rc != 0 || /* Error */
+ buffptr == NULL || /* Just the length is required */
+ *buffptr != NULL) /* Buffer was provided or allocated */
+ return rc;
+
+ /* Allocate memory for the buffer, with hidden space for an allocator at
+ the start. The next time round the loop runs the conversion for real. */
+
+ allocated = PRIV(memctl_malloc)(sizeof(pcre2_memctl) +
+ (*bufflenptr + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)ccontext);
+ if (allocated == NULL) return PCRE2_ERROR_NOMEMORY;
+ *buffptr = (PCRE2_UCHAR *)(((char *)allocated) + sizeof(pcre2_memctl));
+
+ use_buffer = *buffptr;
+ use_length = *bufflenptr + 1;
+ }
+
+/* Control should never get here. */
+
+return PCRE2_ERROR_INTERNAL;
+}
+
+
+/*************************************************
+* Free converted pattern *
+*************************************************/
+
+/* This frees a converted pattern that was put in newly-allocated memory.
+
+Argument: the converted pattern
+Returns: nothing
+*/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_converted_pattern_free(PCRE2_UCHAR *converted)
+{
+if (converted != NULL)
+ {
+ pcre2_memctl *memctl =
+ (pcre2_memctl *)((char *)converted - sizeof(pcre2_memctl));
+ memctl->free(memctl, memctl->memory_data);
+ }
+}
+
+/* End of pcre2_convert.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_dfa_match.c b/test/monniaux/pcre2-10.32/pcre2_dfa_match.c
new file mode 100644
index 00000000..9b43237d
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_dfa_match.c
@@ -0,0 +1,3864 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains the external function pcre2_dfa_match(), which is an
+alternative matching function that uses a sort of DFA algorithm (not a true
+FSM). This is NOT Perl-compatible, but it has advantages in certain
+applications. */
+
+
+/* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved
+the performance of his patterns greatly. I could not use it as it stood, as it
+was not thread safe, and made assumptions about pattern sizes. Also, it caused
+test 7 to loop, and test 9 to crash with a segfault.
+
+The issue is the check for duplicate states, which is done by a simple linear
+search up the state list. (Grep for "duplicate" below to find the code.) For
+many patterns, there will never be many states active at one time, so a simple
+linear search is fine. In patterns that have many active states, it might be a
+bottleneck. The suggested code used an indexing scheme to remember which states
+had previously been used for each character, and avoided the linear search when
+it knew there was no chance of a duplicate. This was implemented when adding
+states to the state lists.
+
+I wrote some thread-safe, not-limited code to try something similar at the time
+of checking for duplicates (instead of when adding states), using index vectors
+on the stack. It did give a 13% improvement with one specially constructed
+pattern for certain subject strings, but on other strings and on many of the
+simpler patterns in the test suite it did worse. The major problem, I think,
+was the extra time to initialize the index. This had to be done for each call
+of internal_dfa_match(). (The supplied patch used a static vector, initialized
+only once - I suspect this was the cause of the problems with the tests.)
+
+Overall, I concluded that the gains in some cases did not outweigh the losses
+in others, so I abandoned this code. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define NLBLOCK mb /* Block containing newline information */
+#define PSSTART start_subject /* Field containing processed string start */
+#define PSEND end_subject /* Field containing processed string end */
+
+#include "pcre2_internal.h"
+
+#define PUBLIC_DFA_MATCH_OPTIONS \
+ (PCRE2_ANCHORED|PCRE2_ENDANCHORED|PCRE2_NOTBOL|PCRE2_NOTEOL|PCRE2_NOTEMPTY| \
+ PCRE2_NOTEMPTY_ATSTART|PCRE2_NO_UTF_CHECK|PCRE2_PARTIAL_HARD| \
+ PCRE2_PARTIAL_SOFT|PCRE2_DFA_SHORTEST|PCRE2_DFA_RESTART)
+
+
+/*************************************************
+* Code parameters and static tables *
+*************************************************/
+
+/* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
+into others, under special conditions. A gap of 20 between the blocks should be
+enough. The resulting opcodes don't have to be less than 256 because they are
+never stored, so we push them well clear of the normal opcodes. */
+
+#define OP_PROP_EXTRA 300
+#define OP_EXTUNI_EXTRA 320
+#define OP_ANYNL_EXTRA 340
+#define OP_HSPACE_EXTRA 360
+#define OP_VSPACE_EXTRA 380
+
+
+/* This table identifies those opcodes that are followed immediately by a
+character that is to be tested in some way. This makes it possible to
+centralize the loading of these characters. In the case of Type * etc, the
+"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
+small value. Non-zero values in the table are the offsets from the opcode where
+the character is to be found. ***NOTE*** If the start of this table is
+modified, the three tables that follow must also be modified. */
+
+static const uint8_t coptable[] = {
+ 0, /* End */
+ 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */
+ 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */
+ 0, 0, 0, /* Any, AllAny, Anybyte */
+ 0, 0, /* \P, \p */
+ 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */
+ 0, /* \X */
+ 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
+ 1, /* Char */
+ 1, /* Chari */
+ 1, /* not */
+ 1, /* noti */
+ /* Positive single-char repeats */
+ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
+ 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto, minupto */
+ 1+IMM2_SIZE, /* exact */
+ 1, 1, 1, 1+IMM2_SIZE, /* *+, ++, ?+, upto+ */
+ 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
+ 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto I, minupto I */
+ 1+IMM2_SIZE, /* exact I */
+ 1, 1, 1, 1+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */
+ /* Negative single-char repeats - only for chars < 256 */
+ 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
+ 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto, minupto */
+ 1+IMM2_SIZE, /* NOT exact */
+ 1, 1, 1, 1+IMM2_SIZE, /* NOT *+, ++, ?+, upto+ */
+ 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
+ 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto I, minupto I */
+ 1+IMM2_SIZE, /* NOT exact I */
+ 1, 1, 1, 1+IMM2_SIZE, /* NOT *+I, ++I, ?+I, upto+I */
+ /* Positive type repeats */
+ 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
+ 1+IMM2_SIZE, 1+IMM2_SIZE, /* Type upto, minupto */
+ 1+IMM2_SIZE, /* Type exact */
+ 1, 1, 1, 1+IMM2_SIZE, /* Type *+, ++, ?+, upto+ */
+ /* Character class & ref repeats */
+ 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */
+ 0, 0, /* CRRANGE, CRMINRANGE */
+ 0, 0, 0, 0, /* Possessive *+, ++, ?+, CRPOSRANGE */
+ 0, /* CLASS */
+ 0, /* NCLASS */
+ 0, /* XCLASS - variable length */
+ 0, /* REF */
+ 0, /* REFI */
+ 0, /* DNREF */
+ 0, /* DNREFI */
+ 0, /* RECURSE */
+ 0, /* CALLOUT */
+ 0, /* CALLOUT_STR */
+ 0, /* Alt */
+ 0, /* Ket */
+ 0, /* KetRmax */
+ 0, /* KetRmin */
+ 0, /* KetRpos */
+ 0, /* Reverse */
+ 0, /* Assert */
+ 0, /* Assert not */
+ 0, /* Assert behind */
+ 0, /* Assert behind not */
+ 0, /* ONCE */
+ 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
+ 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
+ 0, 0, /* CREF, DNCREF */
+ 0, 0, /* RREF, DNRREF */
+ 0, 0, /* FALSE, TRUE */
+ 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
+ 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
+ 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
+ 0, 0, /* COMMIT, COMMIT_ARG */
+ 0, 0, 0, /* FAIL, ACCEPT, ASSERT_ACCEPT */
+ 0, 0, 0 /* CLOSE, SKIPZERO, DEFINE */
+};
+
+/* This table identifies those opcodes that inspect a character. It is used to
+remember the fact that a character could have been inspected when the end of
+the subject is reached. ***NOTE*** If the start of this table is modified, the
+two tables that follow must also be modified. */
+
+static const uint8_t poptable[] = {
+ 0, /* End */
+ 0, 0, 0, 1, 1, /* \A, \G, \K, \B, \b */
+ 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */
+ 1, 1, 1, /* Any, AllAny, Anybyte */
+ 1, 1, /* \P, \p */
+ 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */
+ 1, /* \X */
+ 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
+ 1, /* Char */
+ 1, /* Chari */
+ 1, /* not */
+ 1, /* noti */
+ /* Positive single-char repeats */
+ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
+ 1, 1, 1, /* upto, minupto, exact */
+ 1, 1, 1, 1, /* *+, ++, ?+, upto+ */
+ 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
+ 1, 1, 1, /* upto I, minupto I, exact I */
+ 1, 1, 1, 1, /* *+I, ++I, ?+I, upto+I */
+ /* Negative single-char repeats - only for chars < 256 */
+ 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
+ 1, 1, 1, /* NOT upto, minupto, exact */
+ 1, 1, 1, 1, /* NOT *+, ++, ?+, upto+ */
+ 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
+ 1, 1, 1, /* NOT upto I, minupto I, exact I */
+ 1, 1, 1, 1, /* NOT *+I, ++I, ?+I, upto+I */
+ /* Positive type repeats */
+ 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
+ 1, 1, 1, /* Type upto, minupto, exact */
+ 1, 1, 1, 1, /* Type *+, ++, ?+, upto+ */
+ /* Character class & ref repeats */
+ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
+ 1, 1, /* CRRANGE, CRMINRANGE */
+ 1, 1, 1, 1, /* Possessive *+, ++, ?+, CRPOSRANGE */
+ 1, /* CLASS */
+ 1, /* NCLASS */
+ 1, /* XCLASS - variable length */
+ 0, /* REF */
+ 0, /* REFI */
+ 0, /* DNREF */
+ 0, /* DNREFI */
+ 0, /* RECURSE */
+ 0, /* CALLOUT */
+ 0, /* CALLOUT_STR */
+ 0, /* Alt */
+ 0, /* Ket */
+ 0, /* KetRmax */
+ 0, /* KetRmin */
+ 0, /* KetRpos */
+ 0, /* Reverse */
+ 0, /* Assert */
+ 0, /* Assert not */
+ 0, /* Assert behind */
+ 0, /* Assert behind not */
+ 0, /* ONCE */
+ 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
+ 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
+ 0, 0, /* CREF, DNCREF */
+ 0, 0, /* RREF, DNRREF */
+ 0, 0, /* FALSE, TRUE */
+ 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
+ 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
+ 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
+ 0, 0, /* COMMIT, COMMIT_ARG */
+ 0, 0, 0, /* FAIL, ACCEPT, ASSERT_ACCEPT */
+ 0, 0, 0 /* CLOSE, SKIPZERO, DEFINE */
+};
+
+/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
+and \w */
+
+static const uint8_t toptable1[] = {
+ 0, 0, 0, 0, 0, 0,
+ ctype_digit, ctype_digit,
+ ctype_space, ctype_space,
+ ctype_word, ctype_word,
+ 0, 0 /* OP_ANY, OP_ALLANY */
+};
+
+static const uint8_t toptable2[] = {
+ 0, 0, 0, 0, 0, 0,
+ ctype_digit, 0,
+ ctype_space, 0,
+ ctype_word, 0,
+ 1, 1 /* OP_ANY, OP_ALLANY */
+};
+
+
+/* Structure for holding data about a particular state, which is in effect the
+current data for an active path through the match tree. It must consist
+entirely of ints because the working vector we are passed, and which we put
+these structures in, is a vector of ints. */
+
+typedef struct stateblock {
+ int offset; /* Offset to opcode (-ve has meaning) */
+ int count; /* Count for repeats */
+ int data; /* Some use extra data */
+} stateblock;
+
+#define INTS_PER_STATEBLOCK (int)(sizeof(stateblock)/sizeof(int))
+
+
+/* Before version 10.32 the recursive calls of internal_dfa_match() were passed
+local working space and output vectors that were created on the stack. This has
+caused issues for some patterns, especially in small-stack environments such as
+Windows. A new scheme is now in use which sets up a vector on the stack, but if
+this is too small, heap memory is used, up to the heap_limit. The main
+parameters are all numbers of ints because the workspace is a vector of ints.
+
+The size of the starting stack vector, DFA_START_RWS_SIZE, is in bytes, and is
+defined in pcre2_internal.h so as to be available to pcre2test when it is
+finding the minimum heap requirement for a match. */
+
+#define OVEC_UNIT (sizeof(PCRE2_SIZE)/sizeof(int))
+
+#define RWS_BASE_SIZE (DFA_START_RWS_SIZE/sizeof(int)) /* Stack vector */
+#define RWS_RSIZE 1000 /* Work size for recursion */
+#define RWS_OVEC_RSIZE (1000*OVEC_UNIT) /* Ovector for recursion */
+#define RWS_OVEC_OSIZE (2*OVEC_UNIT) /* Ovector in other cases */
+
+/* This structure is at the start of each workspace block. */
+
+typedef struct RWS_anchor {
+ struct RWS_anchor *next;
+ unsigned int size; /* Number of ints */
+ unsigned int free; /* Number of ints */
+} RWS_anchor;
+
+#define RWS_ANCHOR_SIZE (sizeof(RWS_anchor)/sizeof(int))
+
+
+
+/*************************************************
+* Process a callout *
+*************************************************/
+
+/* This function is called to perform a callout.
+
+Arguments:
+ code current code pointer
+ offsets points to current capture offsets
+ current_subject start of current subject match
+ ptr current position in subject
+ mb the match block
+ extracode extra code offset when called from condition
+ lengthptr where to return the callout length
+
+Returns: the return from the callout
+*/
+
+static int
+do_callout(PCRE2_SPTR code, PCRE2_SIZE *offsets, PCRE2_SPTR current_subject,
+ PCRE2_SPTR ptr, dfa_match_block *mb, PCRE2_SIZE extracode,
+ PCRE2_SIZE *lengthptr)
+{
+pcre2_callout_block *cb = mb->cb;
+
+*lengthptr = (code[extracode] == OP_CALLOUT)?
+ (PCRE2_SIZE)PRIV(OP_lengths)[OP_CALLOUT] :
+ (PCRE2_SIZE)GET(code, 1 + 2*LINK_SIZE + extracode);
+
+if (mb->callout == NULL) return 0; /* No callout provided */
+
+/* Fixed fields in the callout block are set once and for all at the start of
+matching. */
+
+cb->offset_vector = offsets;
+cb->start_match = (PCRE2_SIZE)(current_subject - mb->start_subject);
+cb->current_position = (PCRE2_SIZE)(ptr - mb->start_subject);
+cb->pattern_position = GET(code, 1 + extracode);
+cb->next_item_length = GET(code, 1 + LINK_SIZE + extracode);
+
+if (code[extracode] == OP_CALLOUT)
+ {
+ cb->callout_number = code[1 + 2*LINK_SIZE + extracode];
+ cb->callout_string_offset = 0;
+ cb->callout_string = NULL;
+ cb->callout_string_length = 0;
+ }
+else
+ {
+ cb->callout_number = 0;
+ cb->callout_string_offset = GET(code, 1 + 3*LINK_SIZE + extracode);
+ cb->callout_string = code + (1 + 4*LINK_SIZE + extracode) + 1;
+ cb->callout_string_length = *lengthptr - (1 + 4*LINK_SIZE) - 2;
+ }
+
+return (mb->callout)(cb, mb->callout_data);
+}
+
+
+
+/*************************************************
+* Expand local workspace memory *
+*************************************************/
+
+/* This function is called when internal_dfa_match() is about to be called
+recursively and there is insufficient working space left in the current
+workspace block. If there's an existing next block, use it; otherwise get a new
+block unless the heap limit is reached.
+
+Arguments:
+ rwsptr pointer to block pointer (updated)
+ ovecsize space needed for an ovector
+ mb the match block
+
+Returns: 0 rwsptr has been updated
+ !0 an error code
+*/
+
+static int
+more_workspace(RWS_anchor **rwsptr, unsigned int ovecsize, dfa_match_block *mb)
+{
+RWS_anchor *rws = *rwsptr;
+RWS_anchor *new;
+
+if (rws->next != NULL)
+ {
+ new = rws->next;
+ }
+
+/* All sizes are in units of sizeof(int), except for mb->heaplimit, which is in
+kibibytes. */
+
+else
+ {
+ unsigned int newsize = rws->size * 2;
+ unsigned int heapleft = (unsigned int)
+ (((1024/sizeof(int))*mb->heap_limit - mb->heap_used));
+ if (newsize > heapleft) newsize = heapleft;
+ if (newsize < RWS_RSIZE + ovecsize + RWS_ANCHOR_SIZE)
+ return PCRE2_ERROR_HEAPLIMIT;
+ new = mb->memctl.malloc(newsize*sizeof(int), mb->memctl.memory_data);
+ if (new == NULL) return PCRE2_ERROR_NOMEMORY;
+ mb->heap_used += newsize;
+ new->next = NULL;
+ new->size = newsize;
+ rws->next = new;
+ }
+
+new->free = new->size - RWS_ANCHOR_SIZE;
+*rwsptr = new;
+return 0;
+}
+
+
+
+/*************************************************
+* Match a Regular Expression - DFA engine *
+*************************************************/
+
+/* This internal function applies a compiled pattern to a subject string,
+starting at a given point, using a DFA engine. This function is called from the
+external one, possibly multiple times if the pattern is not anchored. The
+function calls itself recursively for some kinds of subpattern.
+
+Arguments:
+ mb the match_data block with fixed information
+ this_start_code the opening bracket of this subexpression's code
+ current_subject where we currently are in the subject string
+ start_offset start offset in the subject string
+ offsets vector to contain the matching string offsets
+ offsetcount size of same
+ workspace vector of workspace
+ wscount size of same
+ rlevel function call recursion level
+
+Returns: > 0 => number of match offset pairs placed in offsets
+ = 0 => offsets overflowed; longest matches are present
+ -1 => failed to match
+ < -1 => some kind of unexpected problem
+
+The following macros are used for adding states to the two state vectors (one
+for the current character, one for the following character). */
+
+#define ADD_ACTIVE(x,y) \
+ if (active_count++ < wscount) \
+ { \
+ next_active_state->offset = (x); \
+ next_active_state->count = (y); \
+ next_active_state++; \
+ } \
+ else return PCRE2_ERROR_DFA_WSSIZE
+
+#define ADD_ACTIVE_DATA(x,y,z) \
+ if (active_count++ < wscount) \
+ { \
+ next_active_state->offset = (x); \
+ next_active_state->count = (y); \
+ next_active_state->data = (z); \
+ next_active_state++; \
+ } \
+ else return PCRE2_ERROR_DFA_WSSIZE
+
+#define ADD_NEW(x,y) \
+ if (new_count++ < wscount) \
+ { \
+ next_new_state->offset = (x); \
+ next_new_state->count = (y); \
+ next_new_state++; \
+ } \
+ else return PCRE2_ERROR_DFA_WSSIZE
+
+#define ADD_NEW_DATA(x,y,z) \
+ if (new_count++ < wscount) \
+ { \
+ next_new_state->offset = (x); \
+ next_new_state->count = (y); \
+ next_new_state->data = (z); \
+ next_new_state++; \
+ } \
+ else return PCRE2_ERROR_DFA_WSSIZE
+
+/* And now, here is the code */
+
+static int
+internal_dfa_match(
+ dfa_match_block *mb,
+ PCRE2_SPTR this_start_code,
+ PCRE2_SPTR current_subject,
+ PCRE2_SIZE start_offset,
+ PCRE2_SIZE *offsets,
+ uint32_t offsetcount,
+ int *workspace,
+ int wscount,
+ uint32_t rlevel,
+ int *RWS)
+{
+stateblock *active_states, *new_states, *temp_states;
+stateblock *next_active_state, *next_new_state;
+const uint8_t *ctypes, *lcc, *fcc;
+PCRE2_SPTR ptr;
+PCRE2_SPTR end_code;
+dfa_recursion_info new_recursive;
+int active_count, new_count, match_count;
+
+/* Some fields in the mb block are frequently referenced, so we load them into
+independent variables in the hope that this will perform better. */
+
+PCRE2_SPTR start_subject = mb->start_subject;
+PCRE2_SPTR end_subject = mb->end_subject;
+PCRE2_SPTR start_code = mb->start_code;
+
+#ifdef SUPPORT_UNICODE
+BOOL utf = (mb->poptions & PCRE2_UTF) != 0;
+#else
+BOOL utf = FALSE;
+#endif
+
+BOOL reset_could_continue = FALSE;
+
+if (mb->match_call_count++ >= mb->match_limit) return PCRE2_ERROR_MATCHLIMIT;
+if (rlevel++ > mb->match_limit_depth) return PCRE2_ERROR_DEPTHLIMIT;
+offsetcount &= (uint32_t)(-2); /* Round down */
+
+wscount -= 2;
+wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
+ (2 * INTS_PER_STATEBLOCK);
+
+ctypes = mb->tables + ctypes_offset;
+lcc = mb->tables + lcc_offset;
+fcc = mb->tables + fcc_offset;
+
+match_count = PCRE2_ERROR_NOMATCH; /* A negative number */
+
+active_states = (stateblock *)(workspace + 2);
+next_new_state = new_states = active_states + wscount;
+new_count = 0;
+
+/* The first thing in any (sub) pattern is a bracket of some sort. Push all
+the alternative states onto the list, and find out where the end is. This
+makes is possible to use this function recursively, when we want to stop at a
+matching internal ket rather than at the end.
+
+If we are dealing with a backward assertion we have to find out the maximum
+amount to move back, and set up each alternative appropriately. */
+
+if (*this_start_code == OP_ASSERTBACK || *this_start_code == OP_ASSERTBACK_NOT)
+ {
+ size_t max_back = 0;
+ size_t gone_back;
+
+ end_code = this_start_code;
+ do
+ {
+ size_t back = (size_t)GET(end_code, 2+LINK_SIZE);
+ if (back > max_back) max_back = back;
+ end_code += GET(end_code, 1);
+ }
+ while (*end_code == OP_ALT);
+
+ /* If we can't go back the amount required for the longest lookbehind
+ pattern, go back as far as we can; some alternatives may still be viable. */
+
+#ifdef SUPPORT_UNICODE
+ /* In character mode we have to step back character by character */
+
+ if (utf)
+ {
+ for (gone_back = 0; gone_back < max_back; gone_back++)
+ {
+ if (current_subject <= start_subject) break;
+ current_subject--;
+ ACROSSCHAR(current_subject > start_subject, current_subject,
+ current_subject--);
+ }
+ }
+ else
+#endif
+
+ /* In byte-mode we can do this quickly. */
+
+ {
+ size_t current_offset = (size_t)(current_subject - start_subject);
+ gone_back = (current_offset < max_back)? current_offset : max_back;
+ current_subject -= gone_back;
+ }
+
+ /* Save the earliest consulted character */
+
+ if (current_subject < mb->start_used_ptr)
+ mb->start_used_ptr = current_subject;
+
+ /* Now we can process the individual branches. There will be an OP_REVERSE at
+ the start of each branch, except when the length of the branch is zero. */
+
+ end_code = this_start_code;
+ do
+ {
+ uint32_t revlen = (end_code[1+LINK_SIZE] == OP_REVERSE)? 1 + LINK_SIZE : 0;
+ size_t back = (revlen == 0)? 0 : (size_t)GET(end_code, 2+LINK_SIZE);
+ if (back <= gone_back)
+ {
+ int bstate = (int)(end_code - start_code + 1 + LINK_SIZE + revlen);
+ ADD_NEW_DATA(-bstate, 0, (int)(gone_back - back));
+ }
+ end_code += GET(end_code, 1);
+ }
+ while (*end_code == OP_ALT);
+ }
+
+/* This is the code for a "normal" subpattern (not a backward assertion). The
+start of a whole pattern is always one of these. If we are at the top level,
+we may be asked to restart matching from the same point that we reached for a
+previous partial match. We still have to scan through the top-level branches to
+find the end state. */
+
+else
+ {
+ end_code = this_start_code;
+
+ /* Restarting */
+
+ if (rlevel == 1 && (mb->moptions & PCRE2_DFA_RESTART) != 0)
+ {
+ do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
+ new_count = workspace[1];
+ if (!workspace[0])
+ memcpy(new_states, active_states, (size_t)new_count * sizeof(stateblock));
+ }
+
+ /* Not restarting */
+
+ else
+ {
+ int length = 1 + LINK_SIZE +
+ ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
+ *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
+ ? IMM2_SIZE:0);
+ do
+ {
+ ADD_NEW((int)(end_code - start_code + length), 0);
+ end_code += GET(end_code, 1);
+ length = 1 + LINK_SIZE;
+ }
+ while (*end_code == OP_ALT);
+ }
+ }
+
+workspace[0] = 0; /* Bit indicating which vector is current */
+
+/* Loop for scanning the subject */
+
+ptr = current_subject;
+for (;;)
+ {
+ int i, j;
+ int clen, dlen;
+ uint32_t c, d;
+ int forced_fail = 0;
+ BOOL partial_newline = FALSE;
+ BOOL could_continue = reset_could_continue;
+ reset_could_continue = FALSE;
+
+ if (ptr > mb->last_used_ptr) mb->last_used_ptr = ptr;
+
+ /* Make the new state list into the active state list and empty the
+ new state list. */
+
+ temp_states = active_states;
+ active_states = new_states;
+ new_states = temp_states;
+ active_count = new_count;
+ new_count = 0;
+
+ workspace[0] ^= 1; /* Remember for the restarting feature */
+ workspace[1] = active_count;
+
+ /* Set the pointers for adding new states */
+
+ next_active_state = active_states + active_count;
+ next_new_state = new_states;
+
+ /* Load the current character from the subject outside the loop, as many
+ different states may want to look at it, and we assume that at least one
+ will. */
+
+ if (ptr < end_subject)
+ {
+ clen = 1; /* Number of data items in the character */
+#ifdef SUPPORT_UNICODE
+ GETCHARLENTEST(c, ptr, clen);
+#else
+ c = *ptr;
+#endif /* SUPPORT_UNICODE */
+ }
+ else
+ {
+ clen = 0; /* This indicates the end of the subject */
+ c = NOTACHAR; /* This value should never actually be used */
+ }
+
+ /* Scan up the active states and act on each one. The result of an action
+ may be to add more states to the currently active list (e.g. on hitting a
+ parenthesis) or it may be to put states on the new list, for considering
+ when we move the character pointer on. */
+
+ for (i = 0; i < active_count; i++)
+ {
+ stateblock *current_state = active_states + i;
+ BOOL caseless = FALSE;
+ PCRE2_SPTR code;
+ uint32_t codevalue;
+ int state_offset = current_state->offset;
+ int rrc;
+ int count;
+
+ /* A negative offset is a special case meaning "hold off going to this
+ (negated) state until the number of characters in the data field have
+ been skipped". If the could_continue flag was passed over from a previous
+ state, arrange for it to passed on. */
+
+ if (state_offset < 0)
+ {
+ if (current_state->data > 0)
+ {
+ ADD_NEW_DATA(state_offset, current_state->count,
+ current_state->data - 1);
+ if (could_continue) reset_could_continue = TRUE;
+ continue;
+ }
+ else
+ {
+ current_state->offset = state_offset = -state_offset;
+ }
+ }
+
+ /* Check for a duplicate state with the same count, and skip if found.
+ See the note at the head of this module about the possibility of improving
+ performance here. */
+
+ for (j = 0; j < i; j++)
+ {
+ if (active_states[j].offset == state_offset &&
+ active_states[j].count == current_state->count)
+ goto NEXT_ACTIVE_STATE;
+ }
+
+ /* The state offset is the offset to the opcode */
+
+ code = start_code + state_offset;
+ codevalue = *code;
+
+ /* If this opcode inspects a character, but we are at the end of the
+ subject, remember the fact for use when testing for a partial match. */
+
+ if (clen == 0 && poptable[codevalue] != 0)
+ could_continue = TRUE;
+
+ /* If this opcode is followed by an inline character, load it. It is
+ tempting to test for the presence of a subject character here, but that
+ is wrong, because sometimes zero repetitions of the subject are
+ permitted.
+
+ We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
+ argument that is not a data character - but is always one byte long because
+ the values are small. We have to take special action to deal with \P, \p,
+ \H, \h, \V, \v and \X in this case. To keep the other cases fast, convert
+ these ones to new opcodes. */
+
+ if (coptable[codevalue] > 0)
+ {
+ dlen = 1;
+#ifdef SUPPORT_UNICODE
+ if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
+#endif /* SUPPORT_UNICODE */
+ d = code[coptable[codevalue]];
+ if (codevalue >= OP_TYPESTAR)
+ {
+ switch(d)
+ {
+ case OP_ANYBYTE: return PCRE2_ERROR_DFA_UITEM;
+ case OP_NOTPROP:
+ case OP_PROP: codevalue += OP_PROP_EXTRA; break;
+ case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;
+ case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;
+ case OP_NOT_HSPACE:
+ case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;
+ case OP_NOT_VSPACE:
+ case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;
+ default: break;
+ }
+ }
+ }
+ else
+ {
+ dlen = 0; /* Not strictly necessary, but compilers moan */
+ d = NOTACHAR; /* if these variables are not set. */
+ }
+
+
+ /* Now process the individual opcodes */
+
+ switch (codevalue)
+ {
+/* ========================================================================== */
+ /* These cases are never obeyed. This is a fudge that causes a compile-
+ time error if the vectors coptable or poptable, which are indexed by
+ opcode, are not the correct length. It seems to be the only way to do
+ such a check at compile time, as the sizeof() operator does not work
+ in the C preprocessor. */
+
+ case OP_TABLE_LENGTH:
+ case OP_TABLE_LENGTH +
+ ((sizeof(coptable) == OP_TABLE_LENGTH) &&
+ (sizeof(poptable) == OP_TABLE_LENGTH)):
+ return 0;
+
+/* ========================================================================== */
+ /* Reached a closing bracket. If not at the end of the pattern, carry
+ on with the next opcode. For repeating opcodes, also add the repeat
+ state. Note that KETRPOS will always be encountered at the end of the
+ subpattern, because the possessive subpattern repeats are always handled
+ using recursive calls. Thus, it never adds any new states.
+
+ At the end of the (sub)pattern, unless we have an empty string and
+ PCRE2_NOTEMPTY is set, or PCRE2_NOTEMPTY_ATSTART is set and we are at the
+ start of the subject, save the match data, shifting up all previous
+ matches so we always have the longest first. */
+
+ case OP_KET:
+ case OP_KETRMIN:
+ case OP_KETRMAX:
+ case OP_KETRPOS:
+ if (code != end_code)
+ {
+ ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
+ if (codevalue != OP_KET)
+ {
+ ADD_ACTIVE(state_offset - (int)GET(code, 1), 0);
+ }
+ }
+ else
+ {
+ if (ptr > current_subject ||
+ ((mb->moptions & PCRE2_NOTEMPTY) == 0 &&
+ ((mb->moptions & PCRE2_NOTEMPTY_ATSTART) == 0 ||
+ current_subject > start_subject + mb->start_offset)))
+ {
+ if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
+ else if (match_count > 0 && ++match_count * 2 > (int)offsetcount)
+ match_count = 0;
+ count = ((match_count == 0)? (int)offsetcount : match_count * 2) - 2;
+ if (count > 0) (void)memmove(offsets + 2, offsets,
+ (size_t)count * sizeof(PCRE2_SIZE));
+ if (offsetcount >= 2)
+ {
+ offsets[0] = (PCRE2_SIZE)(current_subject - start_subject);
+ offsets[1] = (PCRE2_SIZE)(ptr - start_subject);
+ }
+ if ((mb->moptions & PCRE2_DFA_SHORTEST) != 0) return match_count;
+ }
+ }
+ break;
+
+/* ========================================================================== */
+ /* These opcodes add to the current list of states without looking
+ at the current character. */
+
+ /*-----------------------------------------------------------------*/
+ case OP_ALT:
+ do { code += GET(code, 1); } while (*code == OP_ALT);
+ ADD_ACTIVE((int)(code - start_code), 0);
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_BRA:
+ case OP_SBRA:
+ do
+ {
+ ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
+ code += GET(code, 1);
+ }
+ while (*code == OP_ALT);
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_CBRA:
+ case OP_SCBRA:
+ ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE), 0);
+ code += GET(code, 1);
+ while (*code == OP_ALT)
+ {
+ ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
+ code += GET(code, 1);
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+ ADD_ACTIVE(state_offset + 1, 0);
+ code += 1 + GET(code, 2);
+ while (*code == OP_ALT) code += GET(code, 1);
+ ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_SKIPZERO:
+ code += 1 + GET(code, 2);
+ while (*code == OP_ALT) code += GET(code, 1);
+ ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_CIRC:
+ if (ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0)
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_CIRCM:
+ if ((ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0) ||
+ ((ptr != end_subject || (mb->poptions & PCRE2_ALT_CIRCUMFLEX) != 0 )
+ && WAS_NEWLINE(ptr)))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EOD:
+ if (ptr >= end_subject)
+ {
+ if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ could_continue = TRUE;
+ else { ADD_ACTIVE(state_offset + 1, 0); }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_SOD:
+ if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_SOM:
+ if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+
+/* ========================================================================== */
+ /* These opcodes inspect the next subject character, and sometimes
+ the previous one as well, but do not have an argument. The variable
+ clen contains the length of the current character and is zero if we are
+ at the end of the subject. */
+
+ /*-----------------------------------------------------------------*/
+ case OP_ANY:
+ if (clen > 0 && !IS_NEWLINE(ptr))
+ {
+ if (ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else
+ {
+ ADD_NEW(state_offset + 1, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_ALLANY:
+ if (clen > 0)
+ { ADD_NEW(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EODN:
+ if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ could_continue = TRUE;
+ else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - mb->nllen))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_DOLL:
+ if ((mb->moptions & PCRE2_NOTEOL) == 0)
+ {
+ if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ could_continue = TRUE;
+ else if (clen == 0 ||
+ ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
+ (ptr == end_subject - mb->nllen)
+ ))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ else if (ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ {
+ reset_could_continue = TRUE;
+ ADD_NEW_DATA(-(state_offset + 1), 0, 1);
+ }
+ else could_continue = partial_newline = TRUE;
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_DOLLM:
+ if ((mb->moptions & PCRE2_NOTEOL) == 0)
+ {
+ if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ could_continue = TRUE;
+ else if (clen == 0 ||
+ ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ else if (ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ {
+ reset_could_continue = TRUE;
+ ADD_NEW_DATA(-(state_offset + 1), 0, 1);
+ }
+ else could_continue = partial_newline = TRUE;
+ }
+ }
+ else if (IS_NEWLINE(ptr))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+
+ case OP_DIGIT:
+ case OP_WHITESPACE:
+ case OP_WORDCHAR:
+ if (clen > 0 && c < 256 &&
+ ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
+ { ADD_NEW(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_NOT_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ if (clen > 0 && (c >= 256 ||
+ ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
+ { ADD_NEW(state_offset + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_WORD_BOUNDARY:
+ case OP_NOT_WORD_BOUNDARY:
+ {
+ int left_word, right_word;
+
+ if (ptr > start_subject)
+ {
+ PCRE2_SPTR temp = ptr - 1;
+ if (temp < mb->start_used_ptr) mb->start_used_ptr = temp;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (utf) { BACKCHAR(temp); }
+#endif
+ GETCHARTEST(d, temp);
+#ifdef SUPPORT_UNICODE
+ if ((mb->poptions & PCRE2_UCP) != 0)
+ {
+ if (d == '_') left_word = TRUE; else
+ {
+ uint32_t cat = UCD_CATEGORY(d);
+ left_word = (cat == ucp_L || cat == ucp_N);
+ }
+ }
+ else
+#endif
+ left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
+ }
+ else left_word = FALSE;
+
+ if (clen > 0)
+ {
+ if (ptr >= mb->last_used_ptr)
+ {
+ PCRE2_SPTR temp = ptr + 1;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (utf) { FORWARDCHARTEST(temp, mb->end_subject); }
+#endif
+ mb->last_used_ptr = temp;
+ }
+#ifdef SUPPORT_UNICODE
+ if ((mb->poptions & PCRE2_UCP) != 0)
+ {
+ if (c == '_') right_word = TRUE; else
+ {
+ uint32_t cat = UCD_CATEGORY(c);
+ right_word = (cat == ucp_L || cat == ucp_N);
+ }
+ }
+ else
+#endif
+ right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
+ }
+ else right_word = FALSE;
+
+ if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
+ { ADD_ACTIVE(state_offset + 1, 0); }
+ }
+ break;
+
+
+ /*-----------------------------------------------------------------*/
+ /* Check the next character by Unicode property. We will get here only
+ if the support is in the binary; otherwise a compile-time error occurs.
+ */
+
+#ifdef SUPPORT_UNICODE
+ case OP_PROP:
+ case OP_NOTPROP:
+ if (clen > 0)
+ {
+ BOOL OK;
+ const uint32_t *cp;
+ const ucd_record * prop = GET_UCD(c);
+ switch(code[1])
+ {
+ case PT_ANY:
+ OK = TRUE;
+ break;
+
+ case PT_LAMP:
+ OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt;
+ break;
+
+ case PT_GC:
+ OK = PRIV(ucp_gentype)[prop->chartype] == code[2];
+ break;
+
+ case PT_PC:
+ OK = prop->chartype == code[2];
+ break;
+
+ case PT_SC:
+ OK = prop->script == code[2];
+ break;
+
+ /* These are specials for combination cases. */
+
+ case PT_ALNUM:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N;
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ c == CHAR_UNDERSCORE;
+ break;
+
+ case PT_CLIST:
+ cp = PRIV(ucd_caseless_sets) + code[2];
+ for (;;)
+ {
+ if (c < *cp) { OK = FALSE; break; }
+ if (c == *cp++) { OK = TRUE; break; }
+ }
+ break;
+
+ case PT_UCNC:
+ OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
+ c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
+ c >= 0xe000;
+ break;
+
+ /* Should never occur, but keep compilers from grumbling. */
+
+ default:
+ OK = codevalue != OP_PROP;
+ break;
+ }
+
+ if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }
+ }
+ break;
+#endif
+
+
+
+/* ========================================================================== */
+ /* These opcodes likewise inspect the subject character, but have an
+ argument that is not a data character. It is one of these opcodes:
+ OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
+ OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
+
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
+ if (clen > 0)
+ {
+ if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
+ (c < 256 &&
+ (d != OP_ANY || !IS_NEWLINE(ptr)) &&
+ ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
+ {
+ if (count > 0 && codevalue == OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW(state_offset, count);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSQUERY:
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
+ (c < 256 &&
+ (d != OP_ANY || !IS_NEWLINE(ptr)) &&
+ ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
+ {
+ if (codevalue == OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset + 2, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPOSSTAR:
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
+ (c < 256 &&
+ (d != OP_ANY || !IS_NEWLINE(ptr)) &&
+ ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
+ {
+ if (codevalue == OP_TYPEPOSSTAR)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_TYPEEXACT:
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
+ (c < 256 &&
+ (d != OP_ANY || !IS_NEWLINE(ptr)) &&
+ ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
+ {
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEPOSUPTO:
+ ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0);
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
+ (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ c == NLBLOCK->nl[0])
+ {
+ could_continue = partial_newline = TRUE;
+ }
+ else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
+ (c < 256 &&
+ (d != OP_ANY || !IS_NEWLINE(ptr)) &&
+ ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
+ {
+ if (codevalue == OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ }
+ break;
+
+/* ========================================================================== */
+ /* These are virtual opcodes that are used when something like
+ OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its
+ argument. It keeps the code above fast for the other cases. The argument
+ is in the d variable. */
+
+#ifdef SUPPORT_UNICODE
+ case OP_PROP_EXTRA + OP_TYPEPLUS:
+ case OP_PROP_EXTRA + OP_TYPEMINPLUS:
+ case OP_PROP_EXTRA + OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }
+ if (clen > 0)
+ {
+ BOOL OK;
+ const uint32_t *cp;
+ const ucd_record * prop = GET_UCD(c);
+ switch(code[2])
+ {
+ case PT_ANY:
+ OK = TRUE;
+ break;
+
+ case PT_LAMP:
+ OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt;
+ break;
+
+ case PT_GC:
+ OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
+ break;
+
+ case PT_PC:
+ OK = prop->chartype == code[3];
+ break;
+
+ case PT_SC:
+ OK = prop->script == code[3];
+ break;
+
+ /* These are specials for combination cases. */
+
+ case PT_ALNUM:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N;
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ c == CHAR_UNDERSCORE;
+ break;
+
+ case PT_CLIST:
+ cp = PRIV(ucd_caseless_sets) + code[3];
+ for (;;)
+ {
+ if (c < *cp) { OK = FALSE; break; }
+ if (c == *cp++) { OK = TRUE; break; }
+ }
+ break;
+
+ case PT_UCNC:
+ OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
+ c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
+ c >= 0xe000;
+ break;
+
+ /* Should never occur, but keep compilers from grumbling. */
+
+ default:
+ OK = codevalue != OP_PROP;
+ break;
+ }
+
+ if (OK == (d == OP_PROP))
+ {
+ if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW(state_offset, count);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
+ case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
+ case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
+ if (clen > 0)
+ {
+ int ncount = 0;
+ if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ (void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
+ &ncount);
+ count++;
+ ADD_NEW_DATA(-state_offset, count, ncount);
+ }
+ break;
+#endif
+
+ /*-----------------------------------------------------------------*/
+ case OP_ANYNL_EXTRA + OP_TYPEPLUS:
+ case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:
+ case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
+ if (clen > 0)
+ {
+ int ncount = 0;
+ switch (c)
+ {
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
+ goto ANYNL01;
+
+ case CHAR_CR:
+ if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
+ /* Fall through */
+
+ ANYNL01:
+ case CHAR_LF:
+ if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW_DATA(-state_offset, count, ncount);
+ break;
+
+ default:
+ break;
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_VSPACE_EXTRA + OP_TYPEPLUS:
+ case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:
+ case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ break;
+ }
+
+ if (OK == (d == OP_VSPACE))
+ {
+ if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW_DATA(-state_offset, count, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_HSPACE_EXTRA + OP_TYPEPLUS:
+ case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:
+ case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ HSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ break;
+ }
+
+ if (OK == (d == OP_HSPACE))
+ {
+ if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW_DATA(-state_offset, count, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+#ifdef SUPPORT_UNICODE
+ case OP_PROP_EXTRA + OP_TYPEQUERY:
+ case OP_PROP_EXTRA + OP_TYPEMINQUERY:
+ case OP_PROP_EXTRA + OP_TYPEPOSQUERY:
+ count = 4;
+ goto QS1;
+
+ case OP_PROP_EXTRA + OP_TYPESTAR:
+ case OP_PROP_EXTRA + OP_TYPEMINSTAR:
+ case OP_PROP_EXTRA + OP_TYPEPOSSTAR:
+ count = 0;
+
+ QS1:
+
+ ADD_ACTIVE(state_offset + 4, 0);
+ if (clen > 0)
+ {
+ BOOL OK;
+ const uint32_t *cp;
+ const ucd_record * prop = GET_UCD(c);
+ switch(code[2])
+ {
+ case PT_ANY:
+ OK = TRUE;
+ break;
+
+ case PT_LAMP:
+ OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt;
+ break;
+
+ case PT_GC:
+ OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
+ break;
+
+ case PT_PC:
+ OK = prop->chartype == code[3];
+ break;
+
+ case PT_SC:
+ OK = prop->script == code[3];
+ break;
+
+ /* These are specials for combination cases. */
+
+ case PT_ALNUM:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N;
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ c == CHAR_UNDERSCORE;
+ break;
+
+ case PT_CLIST:
+ cp = PRIV(ucd_caseless_sets) + code[3];
+ for (;;)
+ {
+ if (c < *cp) { OK = FALSE; break; }
+ if (c == *cp++) { OK = TRUE; break; }
+ }
+ break;
+
+ case PT_UCNC:
+ OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
+ c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
+ c >= 0xe000;
+ break;
+
+ /* Should never occur, but keep compilers from grumbling. */
+
+ default:
+ OK = codevalue != OP_PROP;
+ break;
+ }
+
+ if (OK == (d == OP_PROP))
+ {
+ if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||
+ codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset + count, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
+ case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
+ case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:
+ count = 2;
+ goto QS2;
+
+ case OP_EXTUNI_EXTRA + OP_TYPESTAR:
+ case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
+ case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:
+ count = 0;
+
+ QS2:
+
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ int ncount = 0;
+ if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||
+ codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ (void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
+ &ncount);
+ ADD_NEW_DATA(-(state_offset + count), 0, ncount);
+ }
+ break;
+#endif
+
+ /*-----------------------------------------------------------------*/
+ case OP_ANYNL_EXTRA + OP_TYPEQUERY:
+ case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:
+ case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:
+ count = 2;
+ goto QS3;
+
+ case OP_ANYNL_EXTRA + OP_TYPESTAR:
+ case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:
+ case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:
+ count = 0;
+
+ QS3:
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ int ncount = 0;
+ switch (c)
+ {
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
+ goto ANYNL02;
+
+ case CHAR_CR:
+ if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
+ /* Fall through */
+
+ ANYNL02:
+ case CHAR_LF:
+ if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
+ codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount);
+ break;
+
+ default:
+ break;
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_VSPACE_EXTRA + OP_TYPEQUERY:
+ case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:
+ case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:
+ count = 2;
+ goto QS4;
+
+ case OP_VSPACE_EXTRA + OP_TYPESTAR:
+ case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:
+ case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:
+ count = 0;
+
+ QS4:
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ break;
+ }
+ if (OK == (d == OP_VSPACE))
+ {
+ if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||
+ codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_HSPACE_EXTRA + OP_TYPEQUERY:
+ case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:
+ case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:
+ count = 2;
+ goto QS5;
+
+ case OP_HSPACE_EXTRA + OP_TYPESTAR:
+ case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:
+ case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:
+ count = 0;
+
+ QS5:
+ ADD_ACTIVE(state_offset + 2, 0);
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ HSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ break;
+ }
+
+ if (OK == (d == OP_HSPACE))
+ {
+ if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||
+ codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+#ifdef SUPPORT_UNICODE
+ case OP_PROP_EXTRA + OP_TYPEEXACT:
+ case OP_PROP_EXTRA + OP_TYPEUPTO:
+ case OP_PROP_EXTRA + OP_TYPEMINUPTO:
+ case OP_PROP_EXTRA + OP_TYPEPOSUPTO:
+ if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
+ { ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); }
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ BOOL OK;
+ const uint32_t *cp;
+ const ucd_record * prop = GET_UCD(c);
+ switch(code[1 + IMM2_SIZE + 1])
+ {
+ case PT_ANY:
+ OK = TRUE;
+ break;
+
+ case PT_LAMP:
+ OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt;
+ break;
+
+ case PT_GC:
+ OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2];
+ break;
+
+ case PT_PC:
+ OK = prop->chartype == code[1 + IMM2_SIZE + 2];
+ break;
+
+ case PT_SC:
+ OK = prop->script == code[1 + IMM2_SIZE + 2];
+ break;
+
+ /* These are specials for combination cases. */
+
+ case PT_ALNUM:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N;
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ c == CHAR_UNDERSCORE;
+ break;
+
+ case PT_CLIST:
+ cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2];
+ for (;;)
+ {
+ if (c < *cp) { OK = FALSE; break; }
+ if (c == *cp++) { OK = TRUE; break; }
+ }
+ break;
+
+ case PT_UCNC:
+ OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
+ c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
+ c >= 0xe000;
+ break;
+
+ /* Should never occur, but keep compilers from grumbling. */
+
+ default:
+ OK = codevalue != OP_PROP;
+ break;
+ }
+
+ if (OK == (d == OP_PROP))
+ {
+ if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
+ case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
+ case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
+ case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:
+ if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
+ { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ PCRE2_SPTR nptr;
+ int ncount = 0;
+ if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
+ &ncount);
+ if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ reset_could_continue = TRUE;
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
+ else
+ { ADD_NEW_DATA(-state_offset, count, ncount); }
+ }
+ break;
+#endif
+
+ /*-----------------------------------------------------------------*/
+ case OP_ANYNL_EXTRA + OP_TYPEEXACT:
+ case OP_ANYNL_EXTRA + OP_TYPEUPTO:
+ case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:
+ case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:
+ if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)
+ { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ int ncount = 0;
+ switch (c)
+ {
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
+ goto ANYNL03;
+
+ case CHAR_CR:
+ if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
+ /* Fall through */
+
+ ANYNL03:
+ case CHAR_LF:
+ if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
+ else
+ { ADD_NEW_DATA(-state_offset, count, ncount); }
+ break;
+
+ default:
+ break;
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_VSPACE_EXTRA + OP_TYPEEXACT:
+ case OP_VSPACE_EXTRA + OP_TYPEUPTO:
+ case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:
+ case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:
+ if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)
+ { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ VSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ }
+
+ if (OK == (d == OP_VSPACE))
+ {
+ if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
+ else
+ { ADD_NEW_DATA(-state_offset, count, 0); }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_HSPACE_EXTRA + OP_TYPEEXACT:
+ case OP_HSPACE_EXTRA + OP_TYPEUPTO:
+ case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:
+ case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:
+ if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)
+ { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ BOOL OK;
+ switch (c)
+ {
+ HSPACE_CASES:
+ OK = TRUE;
+ break;
+
+ default:
+ OK = FALSE;
+ break;
+ }
+
+ if (OK == (d == OP_HSPACE))
+ {
+ if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
+ else
+ { ADD_NEW_DATA(-state_offset, count, 0); }
+ }
+ }
+ break;
+
+/* ========================================================================== */
+ /* These opcodes are followed by a character that is usually compared
+ to the current subject character; it is loaded into d. We still get
+ here even if there is no subject character, because in some cases zero
+ repetitions are permitted. */
+
+ /*-----------------------------------------------------------------*/
+ case OP_CHAR:
+ if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_CHARI:
+ if (clen == 0) break;
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
+ {
+ unsigned int othercase;
+ if (c < 128)
+ othercase = fcc[c];
+ else
+ othercase = UCD_OTHERCASE(c);
+ if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ /* Not UTF mode */
+ {
+ if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d))
+ { ADD_NEW(state_offset + 2, 0); }
+ }
+ break;
+
+
+#ifdef SUPPORT_UNICODE
+ /*-----------------------------------------------------------------*/
+ /* This is a tricky one because it can match more than one character.
+ Find out how many characters to skip, and then set up a negative state
+ to wait for them to pass before continuing. */
+
+ case OP_EXTUNI:
+ if (clen > 0)
+ {
+ int ncount = 0;
+ PCRE2_SPTR nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject,
+ end_subject, utf, &ncount);
+ if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ reset_could_continue = TRUE;
+ ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
+ }
+ break;
+#endif
+
+ /*-----------------------------------------------------------------*/
+ /* This is a tricky like EXTUNI because it too can match more than one
+ character (when CR is followed by LF). In this case, set up a negative
+ state to wait for one character to pass before continuing. */
+
+ case OP_ANYNL:
+ if (clen > 0) switch(c)
+ {
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
+ /* Fall through */
+
+ case CHAR_LF:
+ ADD_NEW(state_offset + 1, 0);
+ break;
+
+ case CHAR_CR:
+ if (ptr + 1 >= end_subject)
+ {
+ ADD_NEW(state_offset + 1, 0);
+ if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
+ reset_could_continue = TRUE;
+ }
+ else if (UCHAR21TEST(ptr + 1) == CHAR_LF)
+ {
+ ADD_NEW_DATA(-(state_offset + 1), 0, 1);
+ }
+ else
+ {
+ ADD_NEW(state_offset + 1, 0);
+ }
+ break;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_NOT_VSPACE:
+ if (clen > 0) switch(c)
+ {
+ VSPACE_CASES:
+ break;
+
+ default:
+ ADD_NEW(state_offset + 1, 0);
+ break;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_VSPACE:
+ if (clen > 0) switch(c)
+ {
+ VSPACE_CASES:
+ ADD_NEW(state_offset + 1, 0);
+ break;
+
+ default:
+ break;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_NOT_HSPACE:
+ if (clen > 0) switch(c)
+ {
+ HSPACE_CASES:
+ break;
+
+ default:
+ ADD_NEW(state_offset + 1, 0);
+ break;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_HSPACE:
+ if (clen > 0) switch(c)
+ {
+ HSPACE_CASES:
+ ADD_NEW(state_offset + 1, 0);
+ break;
+
+ default:
+ break;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ /* Match a negated single character casefully. */
+
+ case OP_NOT:
+ if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ /* Match a negated single character caselessly. */
+
+ case OP_NOTI:
+ if (clen > 0)
+ {
+ uint32_t otherd;
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ if (c != d && c != otherd)
+ { ADD_NEW(state_offset + dlen + 1, 0); }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSPLUSI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTPOSPLUSI:
+ caseless = TRUE;
+ codevalue -= OP_STARI - OP_STAR;
+
+ /* Fall through */
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSPLUS:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
+ if (clen > 0)
+ {
+ uint32_t otherd = NOTACHAR;
+ if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ }
+ if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
+ {
+ if (count > 0 &&
+ (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW(state_offset, count);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_POSQUERYI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTPOSQUERYI:
+ caseless = TRUE;
+ codevalue -= OP_STARI - OP_STAR;
+ /* Fall through */
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_POSQUERY:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTPOSQUERY:
+ ADD_ACTIVE(state_offset + dlen + 1, 0);
+ if (clen > 0)
+ {
+ uint32_t otherd = NOTACHAR;
+ if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ }
+ if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
+ {
+ if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset + dlen + 1, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_POSSTARI:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPOSSTARI:
+ caseless = TRUE;
+ codevalue -= OP_STARI - OP_STAR;
+ /* Fall through */
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_POSSTAR:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPOSSTAR:
+ ADD_ACTIVE(state_offset + dlen + 1, 0);
+ if (clen > 0)
+ {
+ uint32_t otherd = NOTACHAR;
+ if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ }
+ if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
+ {
+ if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset, 0);
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_EXACTI:
+ case OP_NOTEXACTI:
+ caseless = TRUE;
+ codevalue -= OP_STARI - OP_STAR;
+ /* Fall through */
+ case OP_EXACT:
+ case OP_NOTEXACT:
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ uint32_t otherd = NOTACHAR;
+ if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ }
+ if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
+ {
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_POSUPTOI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTPOSUPTOI:
+ caseless = TRUE;
+ codevalue -= OP_STARI - OP_STAR;
+ /* Fall through */
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_POSUPTO:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTPOSUPTO:
+ ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0);
+ count = current_state->count; /* Number already matched */
+ if (clen > 0)
+ {
+ uint32_t otherd = NOTACHAR;
+ if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && d >= 128)
+ otherd = UCD_OTHERCASE(d);
+ else
+#endif /* SUPPORT_UNICODE */
+ otherd = TABLE_GET(d, fcc, d);
+ }
+ if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
+ {
+ if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ if (++count >= (int)GET2(code, 1))
+ { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ }
+ break;
+
+
+/* ========================================================================== */
+ /* These are the class-handling opcodes */
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ case OP_XCLASS:
+ {
+ BOOL isinclass = FALSE;
+ int next_state_offset;
+ PCRE2_SPTR ecode;
+
+ /* For a simple class, there is always just a 32-byte table, and we
+ can set isinclass from it. */
+
+ if (codevalue != OP_XCLASS)
+ {
+ ecode = code + 1 + (32 / sizeof(PCRE2_UCHAR));
+ if (clen > 0)
+ {
+ isinclass = (c > 255)? (codevalue == OP_NCLASS) :
+ ((((uint8_t *)(code + 1))[c/8] & (1 << (c&7))) != 0);
+ }
+ }
+
+ /* An extended class may have a table or a list of single characters,
+ ranges, or both, and it may be positive or negative. There's a
+ function that sorts all this out. */
+
+ else
+ {
+ ecode = code + GET(code, 1);
+ if (clen > 0) isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE, utf);
+ }
+
+ /* At this point, isinclass is set for all kinds of class, and ecode
+ points to the byte after the end of the class. If there is a
+ quantifier, this is where it will be. */
+
+ next_state_offset = (int)(ecode - start_code);
+
+ switch (*ecode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPOSSTAR:
+ ADD_ACTIVE(next_state_offset + 1, 0);
+ if (isinclass)
+ {
+ if (*ecode == OP_CRPOSSTAR)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(state_offset, 0);
+ }
+ break;
+
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSPLUS:
+ count = current_state->count; /* Already matched */
+ if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
+ if (isinclass)
+ {
+ if (count > 0 && *ecode == OP_CRPOSPLUS)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ count++;
+ ADD_NEW(state_offset, count);
+ }
+ break;
+
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSQUERY:
+ ADD_ACTIVE(next_state_offset + 1, 0);
+ if (isinclass)
+ {
+ if (*ecode == OP_CRPOSQUERY)
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+ ADD_NEW(next_state_offset + 1, 0);
+ }
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ count = current_state->count; /* Already matched */
+ if (count >= (int)GET2(ecode, 1))
+ { ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
+ if (isinclass)
+ {
+ int max = (int)GET2(ecode, 1 + IMM2_SIZE);
+
+ if (*ecode == OP_CRPOSRANGE && count >= (int)GET2(ecode, 1))
+ {
+ active_count--; /* Remove non-match possibility */
+ next_active_state--;
+ }
+
+ if (++count >= max && max != 0) /* Max 0 => no limit */
+ { ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
+ else
+ { ADD_NEW(state_offset, count); }
+ }
+ break;
+
+ default:
+ if (isinclass) { ADD_NEW(next_state_offset, 0); }
+ break;
+ }
+ }
+ break;
+
+/* ========================================================================== */
+ /* These are the opcodes for fancy brackets of various kinds. We have
+ to use recursion in order to handle them. The "always failing" assertion
+ (?!) is optimised to OP_FAIL when compiling, so we have to support that,
+ though the other "backtracking verbs" are not supported. */
+
+ case OP_FAIL:
+ forced_fail++; /* Count FAILs for multiple states */
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ {
+ int rc;
+ int *local_workspace;
+ PCRE2_SIZE *local_offsets;
+ PCRE2_SPTR endasscode = code + GET(code, 1);
+ RWS_anchor *rws = (RWS_anchor *)RWS;
+
+ if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
+ {
+ rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
+ if (rc != 0) return rc;
+ RWS = (int *)rws;
+ }
+
+ local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
+ local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
+ rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
+
+ rc = internal_dfa_match(
+ mb, /* static match data */
+ code, /* this subexpression's code */
+ ptr, /* where we currently are */
+ (PCRE2_SIZE)(ptr - start_subject), /* start offset */
+ local_offsets, /* offset vector */
+ RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */
+ local_workspace, /* workspace vector */
+ RWS_RSIZE, /* size of same */
+ rlevel, /* function recursion level */
+ RWS); /* recursion workspace */
+
+ rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;
+ if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
+ { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_COND:
+ case OP_SCOND:
+ {
+ int codelink = (int)GET(code, 1);
+ PCRE2_UCHAR condcode;
+
+ /* Because of the way auto-callout works during compile, a callout item
+ is inserted between OP_COND and an assertion condition. This does not
+ happen for the other conditions. */
+
+ if (code[LINK_SIZE + 1] == OP_CALLOUT
+ || code[LINK_SIZE + 1] == OP_CALLOUT_STR)
+ {
+ PCRE2_SIZE callout_length;
+ rrc = do_callout(code, offsets, current_subject, ptr, mb,
+ 1 + LINK_SIZE, &callout_length);
+ if (rrc < 0) return rrc; /* Abandon */
+ if (rrc > 0) break; /* Fail this thread */
+ code += callout_length; /* Skip callout data */
+ }
+
+ condcode = code[LINK_SIZE+1];
+
+ /* Back reference conditions and duplicate named recursion conditions
+ are not supported */
+
+ if (condcode == OP_CREF || condcode == OP_DNCREF ||
+ condcode == OP_DNRREF)
+ return PCRE2_ERROR_DFA_UCOND;
+
+ /* The DEFINE condition is always false, and the assertion (?!) is
+ converted to OP_FAIL. */
+
+ if (condcode == OP_FALSE || condcode == OP_FAIL)
+ { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
+
+ /* There is also an always-true condition */
+
+ else if (condcode == OP_TRUE)
+ { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }
+
+ /* The only supported version of OP_RREF is for the value RREF_ANY,
+ which means "test if in any recursion". We can't test for specifically
+ recursed groups. */
+
+ else if (condcode == OP_RREF)
+ {
+ unsigned int value = GET2(code, LINK_SIZE + 2);
+ if (value != RREF_ANY) return PCRE2_ERROR_DFA_UCOND;
+ if (mb->recursive != NULL)
+ { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }
+ else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
+ }
+
+ /* Otherwise, the condition is an assertion */
+
+ else
+ {
+ int rc;
+ int *local_workspace;
+ PCRE2_SIZE *local_offsets;
+ PCRE2_SPTR asscode = code + LINK_SIZE + 1;
+ PCRE2_SPTR endasscode = asscode + GET(asscode, 1);
+ RWS_anchor *rws = (RWS_anchor *)RWS;
+
+ if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
+ {
+ rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
+ if (rc != 0) return rc;
+ RWS = (int *)rws;
+ }
+
+ local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
+ local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
+ rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
+
+ rc = internal_dfa_match(
+ mb, /* fixed match data */
+ asscode, /* this subexpression's code */
+ ptr, /* where we currently are */
+ (PCRE2_SIZE)(ptr - start_subject), /* start offset */
+ local_offsets, /* offset vector */
+ RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */
+ local_workspace, /* workspace vector */
+ RWS_RSIZE, /* size of same */
+ rlevel, /* function recursion level */
+ RWS); /* recursion workspace */
+
+ rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;
+ if ((rc >= 0) ==
+ (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
+ { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
+ else
+ { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_RECURSE:
+ {
+ int rc;
+ int *local_workspace;
+ PCRE2_SIZE *local_offsets;
+ RWS_anchor *rws = (RWS_anchor *)RWS;
+ dfa_recursion_info *ri;
+ PCRE2_SPTR callpat = start_code + GET(code, 1);
+ uint32_t recno = (callpat == mb->start_code)? 0 :
+ GET2(callpat, 1 + LINK_SIZE);
+
+ if (rws->free < RWS_RSIZE + RWS_OVEC_RSIZE)
+ {
+ rc = more_workspace(&rws, RWS_OVEC_RSIZE, mb);
+ if (rc != 0) return rc;
+ RWS = (int *)rws;
+ }
+
+ local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
+ local_workspace = ((int *)local_offsets) + RWS_OVEC_RSIZE;
+ rws->free -= RWS_RSIZE + RWS_OVEC_RSIZE;
+
+ /* Check for repeating a recursion without advancing the subject
+ pointer. This should catch convoluted mutual recursions. (Some simple
+ cases are caught at compile time.) */
+
+ for (ri = mb->recursive; ri != NULL; ri = ri->prevrec)
+ if (recno == ri->group_num && ptr == ri->subject_position)
+ return PCRE2_ERROR_RECURSELOOP;
+
+ /* Remember this recursion and where we started it so as to
+ catch infinite loops. */
+
+ new_recursive.group_num = recno;
+ new_recursive.subject_position = ptr;
+ new_recursive.prevrec = mb->recursive;
+ mb->recursive = &new_recursive;
+
+ rc = internal_dfa_match(
+ mb, /* fixed match data */
+ callpat, /* this subexpression's code */
+ ptr, /* where we currently are */
+ (PCRE2_SIZE)(ptr - start_subject), /* start offset */
+ local_offsets, /* offset vector */
+ RWS_OVEC_RSIZE/OVEC_UNIT, /* size of same */
+ local_workspace, /* workspace vector */
+ RWS_RSIZE, /* size of same */
+ rlevel, /* function recursion level */
+ RWS); /* recursion workspace */
+
+ rws->free += RWS_RSIZE + RWS_OVEC_RSIZE;
+ mb->recursive = new_recursive.prevrec; /* Done this recursion */
+
+ /* Ran out of internal offsets */
+
+ if (rc == 0) return PCRE2_ERROR_DFA_RECURSE;
+
+ /* For each successful matched substring, set up the next state with a
+ count of characters to skip before trying it. Note that the count is in
+ characters, not bytes. */
+
+ if (rc > 0)
+ {
+ for (rc = rc*2 - 2; rc >= 0; rc -= 2)
+ {
+ PCRE2_SIZE charcount = local_offsets[rc+1] - local_offsets[rc];
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (utf)
+ {
+ PCRE2_SPTR p = start_subject + local_offsets[rc];
+ PCRE2_SPTR pp = start_subject + local_offsets[rc+1];
+ while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
+ }
+#endif
+ if (charcount > 0)
+ {
+ ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0,
+ (int)(charcount - 1));
+ }
+ else
+ {
+ ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
+ }
+ }
+ }
+ else if (rc != PCRE2_ERROR_NOMATCH) return rc;
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_BRAPOS:
+ case OP_SBRAPOS:
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ case OP_BRAPOSZERO:
+ {
+ int rc;
+ int *local_workspace;
+ PCRE2_SIZE *local_offsets;
+ PCRE2_SIZE charcount, matched_count;
+ PCRE2_SPTR local_ptr = ptr;
+ RWS_anchor *rws = (RWS_anchor *)RWS;
+ BOOL allow_zero;
+
+ if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
+ {
+ rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
+ if (rc != 0) return rc;
+ RWS = (int *)rws;
+ }
+
+ local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
+ local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
+ rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ if (codevalue == OP_BRAPOSZERO)
+ {
+ allow_zero = TRUE;
+ codevalue = *(++code); /* Codevalue will be one of above BRAs */
+ }
+ else allow_zero = FALSE;
+
+ /* Loop to match the subpattern as many times as possible as if it were
+ a complete pattern. */
+
+ for (matched_count = 0;; matched_count++)
+ {
+ rc = internal_dfa_match(
+ mb, /* fixed match data */
+ code, /* this subexpression's code */
+ local_ptr, /* where we currently are */
+ (PCRE2_SIZE)(ptr - start_subject), /* start offset */
+ local_offsets, /* offset vector */
+ RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */
+ local_workspace, /* workspace vector */
+ RWS_RSIZE, /* size of same */
+ rlevel, /* function recursion level */
+ RWS); /* recursion workspace */
+
+ /* Failed to match */
+
+ if (rc < 0)
+ {
+ if (rc != PCRE2_ERROR_NOMATCH) return rc;
+ break;
+ }
+
+ /* Matched: break the loop if zero characters matched. */
+
+ charcount = local_offsets[1] - local_offsets[0];
+ if (charcount == 0) break;
+ local_ptr += charcount; /* Advance temporary position ptr */
+ }
+
+ rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ /* At this point we have matched the subpattern matched_count
+ times, and local_ptr is pointing to the character after the end of the
+ last match. */
+
+ if (matched_count > 0 || allow_zero)
+ {
+ PCRE2_SPTR end_subpattern = code;
+ int next_state_offset;
+
+ do { end_subpattern += GET(end_subpattern, 1); }
+ while (*end_subpattern == OP_ALT);
+ next_state_offset =
+ (int)(end_subpattern - start_code + LINK_SIZE + 1);
+
+ /* Optimization: if there are no more active states, and there
+ are no new states yet set up, then skip over the subject string
+ right here, to save looping. Otherwise, set up the new state to swing
+ into action when the end of the matched substring is reached. */
+
+ if (i + 1 >= active_count && new_count == 0)
+ {
+ ptr = local_ptr;
+ clen = 0;
+ ADD_NEW(next_state_offset, 0);
+ }
+ else
+ {
+ PCRE2_SPTR p = ptr;
+ PCRE2_SPTR pp = local_ptr;
+ charcount = (PCRE2_SIZE)(pp - p);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (utf) while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
+#endif
+ ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));
+ }
+ }
+ }
+ break;
+
+ /*-----------------------------------------------------------------*/
+ case OP_ONCE:
+ {
+ int rc;
+ int *local_workspace;
+ PCRE2_SIZE *local_offsets;
+ RWS_anchor *rws = (RWS_anchor *)RWS;
+
+ if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
+ {
+ rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
+ if (rc != 0) return rc;
+ RWS = (int *)rws;
+ }
+
+ local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
+ local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
+ rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ rc = internal_dfa_match(
+ mb, /* fixed match data */
+ code, /* this subexpression's code */
+ ptr, /* where we currently are */
+ (PCRE2_SIZE)(ptr - start_subject), /* start offset */
+ local_offsets, /* offset vector */
+ RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */
+ local_workspace, /* workspace vector */
+ RWS_RSIZE, /* size of same */
+ rlevel, /* function recursion level */
+ RWS); /* recursion workspace */
+
+ rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
+
+ if (rc >= 0)
+ {
+ PCRE2_SPTR end_subpattern = code;
+ PCRE2_SIZE charcount = local_offsets[1] - local_offsets[0];
+ int next_state_offset, repeat_state_offset;
+
+ do { end_subpattern += GET(end_subpattern, 1); }
+ while (*end_subpattern == OP_ALT);
+ next_state_offset =
+ (int)(end_subpattern - start_code + LINK_SIZE + 1);
+
+ /* If the end of this subpattern is KETRMAX or KETRMIN, we must
+ arrange for the repeat state also to be added to the relevant list.
+ Calculate the offset, or set -1 for no repeat. */
+
+ repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
+ *end_subpattern == OP_KETRMIN)?
+ (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
+
+ /* If we have matched an empty string, add the next state at the
+ current character pointer. This is important so that the duplicate
+ checking kicks in, which is what breaks infinite loops that match an
+ empty string. */
+
+ if (charcount == 0)
+ {
+ ADD_ACTIVE(next_state_offset, 0);
+ }
+
+ /* Optimization: if there are no more active states, and there
+ are no new states yet set up, then skip over the subject string
+ right here, to save looping. Otherwise, set up the new state to swing
+ into action when the end of the matched substring is reached. */
+
+ else if (i + 1 >= active_count && new_count == 0)
+ {
+ ptr += charcount;
+ clen = 0;
+ ADD_NEW(next_state_offset, 0);
+
+ /* If we are adding a repeat state at the new character position,
+ we must fudge things so that it is the only current state.
+ Otherwise, it might be a duplicate of one we processed before, and
+ that would cause it to be skipped. */
+
+ if (repeat_state_offset >= 0)
+ {
+ next_active_state = active_states;
+ active_count = 0;
+ i = -1;
+ ADD_ACTIVE(repeat_state_offset, 0);
+ }
+ }
+ else
+ {
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (utf)
+ {
+ PCRE2_SPTR p = start_subject + local_offsets[0];
+ PCRE2_SPTR pp = start_subject + local_offsets[1];
+ while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
+ }
+#endif
+ ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));
+ if (repeat_state_offset >= 0)
+ { ADD_NEW_DATA(-repeat_state_offset, 0, (int)(charcount - 1)); }
+ }
+ }
+ else if (rc != PCRE2_ERROR_NOMATCH) return rc;
+ }
+ break;
+
+
+/* ========================================================================== */
+ /* Handle callouts */
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+ {
+ PCRE2_SIZE callout_length;
+ rrc = do_callout(code, offsets, current_subject, ptr, mb, 0,
+ &callout_length);
+ if (rrc < 0) return rrc; /* Abandon */
+ if (rrc == 0)
+ { ADD_ACTIVE(state_offset + (int)callout_length, 0); }
+ }
+ break;
+
+
+/* ========================================================================== */
+ default: /* Unsupported opcode */
+ return PCRE2_ERROR_DFA_UITEM;
+ }
+
+ NEXT_ACTIVE_STATE: continue;
+
+ } /* End of loop scanning active states */
+
+ /* We have finished the processing at the current subject character. If no
+ new states have been set for the next character, we have found all the
+ matches that we are going to find. If we are at the top level and partial
+ matching has been requested, check for appropriate conditions.
+
+ The "forced_ fail" variable counts the number of (*F) encountered for the
+ character. If it is equal to the original active_count (saved in
+ workspace[1]) it means that (*F) was found on every active state. In this
+ case we don't want to give a partial match.
+
+ The "could_continue" variable is true if a state could have continued but
+ for the fact that the end of the subject was reached. */
+
+ if (new_count <= 0)
+ {
+ if (rlevel == 1 && /* Top level, and */
+ could_continue && /* Some could go on, and */
+ forced_fail != workspace[1] && /* Not all forced fail & */
+ ( /* either... */
+ (mb->moptions & PCRE2_PARTIAL_HARD) != 0 /* Hard partial */
+ || /* or... */
+ ((mb->moptions & PCRE2_PARTIAL_SOFT) != 0 && /* Soft partial and */
+ match_count < 0) /* no matches */
+ ) && /* And... */
+ (
+ partial_newline || /* Either partial NL */
+ ( /* or ... */
+ ptr >= end_subject && /* End of subject and */
+ ptr > mb->start_used_ptr) /* Inspected non-empty string */
+ )
+ )
+ match_count = PCRE2_ERROR_PARTIAL;
+ break; /* Exit from loop along the subject string */
+ }
+
+ /* One or more states are active for the next character. */
+
+ ptr += clen; /* Advance to next subject character */
+ } /* Loop to move along the subject string */
+
+/* Control gets here from "break" a few lines above. If we have a match and
+PCRE2_ENDANCHORED is set, the match fails. */
+
+if (match_count >= 0 &&
+ ((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0 &&
+ ptr < end_subject)
+ match_count = PCRE2_ERROR_NOMATCH;
+
+return match_count;
+}
+
+
+
+/*************************************************
+* Match a pattern using the DFA algorithm *
+*************************************************/
+
+/* This function matches a compiled pattern to a subject string, using the
+alternate matching algorithm that finds all matches at once.
+
+Arguments:
+ code points to the compiled pattern
+ subject subject string
+ length length of subject string
+ startoffset where to start matching in the subject
+ options option bits
+ match_data points to a match data structure
+ gcontext points to a match context
+ workspace pointer to workspace
+ wscount size of workspace
+
+Returns: > 0 => number of match offset pairs placed in offsets
+ = 0 => offsets overflowed; longest matches are present
+ -1 => failed to match
+ < -1 => some kind of unexpected problem
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_dfa_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
+ PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
+ pcre2_match_context *mcontext, int *workspace, PCRE2_SIZE wscount)
+{
+int rc;
+const pcre2_real_code *re = (const pcre2_real_code *)code;
+
+PCRE2_SPTR start_match;
+PCRE2_SPTR end_subject;
+PCRE2_SPTR bumpalong_limit;
+PCRE2_SPTR req_cu_ptr;
+
+BOOL utf, anchored, startline, firstline;
+BOOL has_first_cu = FALSE;
+BOOL has_req_cu = FALSE;
+
+PCRE2_UCHAR first_cu = 0;
+PCRE2_UCHAR first_cu2 = 0;
+PCRE2_UCHAR req_cu = 0;
+PCRE2_UCHAR req_cu2 = 0;
+
+const uint8_t *start_bits = NULL;
+
+/* We need to have mb pointing to a match block, because the IS_NEWLINE macro
+is used below, and it expects NLBLOCK to be defined as a pointer. */
+
+pcre2_callout_block cb;
+dfa_match_block actual_match_block;
+dfa_match_block *mb = &actual_match_block;
+
+/* Set up a starting block of memory for use during recursive calls to
+internal_dfa_match(). By putting this on the stack, it minimizes resource use
+in the case when it is not needed. If this is too small, more memory is
+obtained from the heap. At the start of each block is an anchor structure.*/
+
+int base_recursion_workspace[RWS_BASE_SIZE];
+RWS_anchor *rws = (RWS_anchor *)base_recursion_workspace;
+rws->next = NULL;
+rws->size = RWS_BASE_SIZE;
+rws->free = RWS_BASE_SIZE - RWS_ANCHOR_SIZE;
+
+/* A length equal to PCRE2_ZERO_TERMINATED implies a zero-terminated
+subject string. */
+
+if (length == PCRE2_ZERO_TERMINATED) length = PRIV(strlen)(subject);
+
+/* Plausibility checks */
+
+if ((options & ~PUBLIC_DFA_MATCH_OPTIONS) != 0) return PCRE2_ERROR_BADOPTION;
+if (re == NULL || subject == NULL || workspace == NULL || match_data == NULL)
+ return PCRE2_ERROR_NULL;
+if (wscount < 20) return PCRE2_ERROR_DFA_WSSIZE;
+if (start_offset > length) return PCRE2_ERROR_BADOFFSET;
+
+/* Partial matching and PCRE2_ENDANCHORED are currently not allowed at the same
+time. */
+
+if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
+ ((re->overall_options | options) & PCRE2_ENDANCHORED) != 0)
+ return PCRE2_ERROR_BADOPTION;
+
+/* Check that the first field in the block is the magic number. If it is not,
+return with PCRE2_ERROR_BADMAGIC. */
+
+if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
+
+/* Check the code unit width. */
+
+if ((re->flags & PCRE2_MODE_MASK) != PCRE2_CODE_UNIT_WIDTH/8)
+ return PCRE2_ERROR_BADMODE;
+
+/* PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART are match-time flags in the
+options variable for this function. Users of PCRE2 who are not calling the
+function directly would like to have a way of setting these flags, in the same
+way that they can set pcre2_compile() flags like PCRE2_NO_AUTOPOSSESS with
+constructions like (*NO_AUTOPOSSESS). To enable this, (*NOTEMPTY) and
+(*NOTEMPTY_ATSTART) set bits in the pattern's "flag" function which can now be
+transferred to the options for this function. The bits are guaranteed to be
+adjacent, but do not have the same values. This bit of Boolean trickery assumes
+that the match-time bits are not more significant than the flag bits. If by
+accident this is not the case, a compile-time division by zero error will
+occur. */
+
+#define FF (PCRE2_NOTEMPTY_SET|PCRE2_NE_ATST_SET)
+#define OO (PCRE2_NOTEMPTY|PCRE2_NOTEMPTY_ATSTART)
+options |= (re->flags & FF) / ((FF & (~FF+1)) / (OO & (~OO+1)));
+#undef FF
+#undef OO
+
+/* If restarting after a partial match, do some sanity checks on the contents
+of the workspace. */
+
+if ((options & PCRE2_DFA_RESTART) != 0)
+ {
+ if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 ||
+ workspace[1] > (int)((wscount - 2)/INTS_PER_STATEBLOCK))
+ return PCRE2_ERROR_DFA_BADRESTART;
+ }
+
+/* Set some local values */
+
+utf = (re->overall_options & PCRE2_UTF) != 0;
+start_match = subject + start_offset;
+end_subject = subject + length;
+req_cu_ptr = start_match - 1;
+anchored = (options & (PCRE2_ANCHORED|PCRE2_DFA_RESTART)) != 0 ||
+ (re->overall_options & PCRE2_ANCHORED) != 0;
+
+/* The "must be at the start of a line" flags are used in a loop when finding
+where to start. */
+
+startline = (re->flags & PCRE2_STARTLINE) != 0;
+firstline = (re->overall_options & PCRE2_FIRSTLINE) != 0;
+bumpalong_limit = end_subject;
+
+/* Initialize and set up the fixed fields in the callout block, with a pointer
+in the match block. */
+
+mb->cb = &cb;
+cb.version = 2;
+cb.subject = subject;
+cb.subject_length = (PCRE2_SIZE)(end_subject - subject);
+cb.callout_flags = 0;
+cb.capture_top = 1; /* No capture support */
+cb.capture_last = 0;
+cb.mark = NULL; /* No (*MARK) support */
+
+/* Get data from the match context, if present, and fill in the remaining
+fields in the match block. It is an error to set an offset limit without
+setting the flag at compile time. */
+
+if (mcontext == NULL)
+ {
+ mb->callout = NULL;
+ mb->memctl = re->memctl;
+ mb->match_limit = PRIV(default_match_context).match_limit;
+ mb->match_limit_depth = PRIV(default_match_context).depth_limit;
+ mb->heap_limit = PRIV(default_match_context).heap_limit;
+ }
+else
+ {
+ if (mcontext->offset_limit != PCRE2_UNSET)
+ {
+ if ((re->overall_options & PCRE2_USE_OFFSET_LIMIT) == 0)
+ return PCRE2_ERROR_BADOFFSETLIMIT;
+ bumpalong_limit = subject + mcontext->offset_limit;
+ }
+ mb->callout = mcontext->callout;
+ mb->callout_data = mcontext->callout_data;
+ mb->memctl = mcontext->memctl;
+ mb->match_limit = mcontext->match_limit;
+ mb->match_limit_depth = mcontext->depth_limit;
+ mb->heap_limit = mcontext->heap_limit;
+ }
+
+if (mb->match_limit > re->limit_match)
+ mb->match_limit = re->limit_match;
+
+if (mb->match_limit_depth > re->limit_depth)
+ mb->match_limit_depth = re->limit_depth;
+
+if (mb->heap_limit > re->limit_heap)
+ mb->heap_limit = re->limit_heap;
+
+mb->start_code = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
+ re->name_count * re->name_entry_size;
+mb->tables = re->tables;
+mb->start_subject = subject;
+mb->end_subject = end_subject;
+mb->start_offset = start_offset;
+mb->moptions = options;
+mb->poptions = re->overall_options;
+mb->match_call_count = 0;
+mb->heap_used = 0;
+
+/* Process the \R and newline settings. */
+
+mb->bsr_convention = re->bsr_convention;
+mb->nltype = NLTYPE_FIXED;
+switch(re->newline_convention)
+ {
+ case PCRE2_NEWLINE_CR:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_CR;
+ break;
+
+ case PCRE2_NEWLINE_LF:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_NUL:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_NUL;
+ break;
+
+ case PCRE2_NEWLINE_CRLF:
+ mb->nllen = 2;
+ mb->nl[0] = CHAR_CR;
+ mb->nl[1] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_ANY:
+ mb->nltype = NLTYPE_ANY;
+ break;
+
+ case PCRE2_NEWLINE_ANYCRLF:
+ mb->nltype = NLTYPE_ANYCRLF;
+ break;
+
+ default: return PCRE2_ERROR_INTERNAL;
+ }
+
+/* Check a UTF string for validity if required. For 8-bit and 16-bit strings,
+we must also check that a starting offset does not point into the middle of a
+multiunit character. We check only the portion of the subject that is going to
+be inspected during matching - from the offset minus the maximum back reference
+to the given length. This saves time when a small part of a large subject is
+being matched by the use of a starting offset. Note that the maximum lookbehind
+is a number of characters, not code units. */
+
+#ifdef SUPPORT_UNICODE
+if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
+ {
+ PCRE2_SPTR check_subject = start_match; /* start_match includes offset */
+
+ if (start_offset > 0)
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 32
+ unsigned int i;
+ if (start_match < end_subject && NOT_FIRSTCU(*start_match))
+ return PCRE2_ERROR_BADUTFOFFSET;
+ for (i = re->max_lookbehind; i > 0 && check_subject > subject; i--)
+ {
+ check_subject--;
+ while (check_subject > subject &&
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ (*check_subject & 0xc0) == 0x80)
+#else /* 16-bit */
+ (*check_subject & 0xfc00) == 0xdc00)
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ check_subject--;
+ }
+#else /* In the 32-bit library, one code unit equals one character. */
+ check_subject -= re->max_lookbehind;
+ if (check_subject < subject) check_subject = subject;
+#endif /* PCRE2_CODE_UNIT_WIDTH != 32 */
+ }
+
+ /* Validate the relevant portion of the subject. After an error, adjust the
+ offset to be an absolute offset in the whole string. */
+
+ match_data->rc = PRIV(valid_utf)(check_subject,
+ length - (PCRE2_SIZE)(check_subject - subject), &(match_data->startchar));
+ if (match_data->rc != 0)
+ {
+ match_data->startchar += (PCRE2_SIZE)(check_subject - subject);
+ return match_data->rc;
+ }
+ }
+#endif /* SUPPORT_UNICODE */
+
+/* Set up the first code unit to match, if available. If there's no first code
+unit there may be a bitmap of possible first characters. */
+
+if ((re->flags & PCRE2_FIRSTSET) != 0)
+ {
+ has_first_cu = TRUE;
+ first_cu = first_cu2 = (PCRE2_UCHAR)(re->first_codeunit);
+ if ((re->flags & PCRE2_FIRSTCASELESS) != 0)
+ {
+ first_cu2 = TABLE_GET(first_cu, mb->tables + fcc_offset, first_cu);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (utf && first_cu > 127)
+ first_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(first_cu);
+#endif
+ }
+ }
+else
+ if (!startline && (re->flags & PCRE2_FIRSTMAPSET) != 0)
+ start_bits = re->start_bitmap;
+
+/* There may be a "last known required code unit" set. */
+
+if ((re->flags & PCRE2_LASTSET) != 0)
+ {
+ has_req_cu = TRUE;
+ req_cu = req_cu2 = (PCRE2_UCHAR)(re->last_codeunit);
+ if ((re->flags & PCRE2_LASTCASELESS) != 0)
+ {
+ req_cu2 = TABLE_GET(req_cu, mb->tables + fcc_offset, req_cu);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (utf && req_cu > 127) req_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(req_cu);
+#endif
+ }
+ }
+
+/* Fill in fields that are always returned in the match data. */
+
+match_data->code = re;
+match_data->subject = subject;
+match_data->mark = NULL;
+match_data->matchedby = PCRE2_MATCHEDBY_DFA_INTERPRETER;
+
+/* Call the main matching function, looping for a non-anchored regex after a
+failed match. If not restarting, perform certain optimizations at the start of
+a match. */
+
+for (;;)
+ {
+ /* ----------------- Start of match optimizations ---------------- */
+
+ /* There are some optimizations that avoid running the match if a known
+ starting point is not found, or if a known later code unit is not present.
+ However, there is an option (settable at compile time) that disables
+ these, for testing and for ensuring that all callouts do actually occur.
+ The optimizations must also be avoided when restarting a DFA match. */
+
+ if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0 &&
+ (options & PCRE2_DFA_RESTART) == 0)
+ {
+ /* If firstline is TRUE, the start of the match is constrained to the first
+ line of a multiline string. That is, the match must be before or at the
+ first newline following the start of matching. Temporarily adjust
+ end_subject so that we stop the optimization scans for a first code unit
+ immediately after the first character of a newline (the first code unit can
+ legitimately be a newline). If the match fails at the newline, later code
+ breaks this loop. */
+
+ if (firstline)
+ {
+ PCRE2_SPTR t = start_match;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ while (t < end_subject && !IS_NEWLINE(t))
+ {
+ t++;
+ ACROSSCHAR(t < end_subject, t, t++);
+ }
+ }
+ else
+#endif
+ while (t < end_subject && !IS_NEWLINE(t)) t++;
+ end_subject = t;
+ }
+
+ /* Anchored: check the first code unit if one is recorded. This may seem
+ pointless but it can help in detecting a no match case without scanning for
+ the required code unit. */
+
+ if (anchored)
+ {
+ if (has_first_cu || start_bits != NULL)
+ {
+ BOOL ok = start_match < end_subject;
+ if (ok)
+ {
+ PCRE2_UCHAR c = UCHAR21TEST(start_match);
+ ok = has_first_cu && (c == first_cu || c == first_cu2);
+ if (!ok && start_bits != NULL)
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (c > 255) c = 255;
+#endif
+ ok = (start_bits[c/8] & (1 << (c&7))) != 0;
+ }
+ }
+ if (!ok) break;
+ }
+ }
+
+ /* Not anchored. Advance to a unique first code unit if there is one. In
+ 8-bit mode, the use of memchr() gives a big speed up, even though we have
+ to call it twice in caseless mode, in order to find the earliest occurrence
+ of the character in either of its cases. */
+
+ else
+ {
+ if (has_first_cu)
+ {
+ if (first_cu != first_cu2) /* Caseless */
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ PCRE2_UCHAR smc;
+ while (start_match < end_subject &&
+ (smc = UCHAR21TEST(start_match)) != first_cu &&
+ smc != first_cu2)
+ start_match++;
+#else /* 8-bit code units */
+ PCRE2_SPTR pp1 =
+ memchr(start_match, first_cu, end_subject-start_match);
+ PCRE2_SPTR pp2 =
+ memchr(start_match, first_cu2, end_subject-start_match);
+ if (pp1 == NULL)
+ start_match = (pp2 == NULL)? end_subject : pp2;
+ else
+ start_match = (pp2 == NULL || pp1 < pp2)? pp1 : pp2;
+#endif
+ }
+
+ /* The caseful case */
+
+ else
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ while (start_match < end_subject && UCHAR21TEST(start_match) !=
+ first_cu)
+ start_match++;
+#else
+ start_match = memchr(start_match, first_cu, end_subject - start_match);
+ if (start_match == NULL) start_match = end_subject;
+#endif
+ }
+
+ /* If we can't find the required code unit, having reached the true end
+ of the subject, break the bumpalong loop, to force a match failure,
+ except when doing partial matching, when we let the next cycle run at
+ the end of the subject. To see why, consider the pattern /(?<=abc)def/,
+ which partially matches "abc", even though the string does not contain
+ the starting character "d". If we have not reached the true end of the
+ subject (PCRE2_FIRSTLINE caused end_subject to be temporarily modified)
+ we also let the cycle run, because the matching string is legitimately
+ allowed to start with the first code unit of a newline. */
+
+ if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&
+ start_match >= mb->end_subject)
+ break;
+ }
+
+ /* If there's no first code unit, advance to just after a linebreak for a
+ multiline match if required. */
+
+ else if (startline)
+ {
+ if (start_match > mb->start_subject + start_offset)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ while (start_match < end_subject && !WAS_NEWLINE(start_match))
+ {
+ start_match++;
+ ACROSSCHAR(start_match < end_subject, start_match, start_match++);
+ }
+ }
+ else
+#endif
+ while (start_match < end_subject && !WAS_NEWLINE(start_match))
+ start_match++;
+
+ /* If we have just passed a CR and the newline option is ANY or
+ ANYCRLF, and we are now at a LF, advance the match position by one
+ more code unit. */
+
+ if (start_match[-1] == CHAR_CR &&
+ (mb->nltype == NLTYPE_ANY || mb->nltype == NLTYPE_ANYCRLF) &&
+ start_match < end_subject &&
+ UCHAR21TEST(start_match) == CHAR_NL)
+ start_match++;
+ }
+ }
+
+ /* If there's no first code unit or a requirement for a multiline line
+ start, advance to a non-unique first code unit if any have been
+ identified. The bitmap contains only 256 bits. When code units are 16 or
+ 32 bits wide, all code units greater than 254 set the 255 bit. */
+
+ else if (start_bits != NULL)
+ {
+ while (start_match < end_subject)
+ {
+ uint32_t c = UCHAR21TEST(start_match);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (c > 255) c = 255;
+#endif
+ if ((start_bits[c/8] & (1 << (c&7))) != 0) break;
+ start_match++;
+ }
+
+ /* See comment above in first_cu checking about the next line. */
+
+ if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&
+ start_match >= mb->end_subject)
+ break;
+ }
+ } /* End of first code unit handling */
+
+ /* Restore fudged end_subject */
+
+ end_subject = mb->end_subject;
+
+ /* The following two optimizations are disabled for partial matching. */
+
+ if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0)
+ {
+ /* The minimum matching length is a lower bound; no actual string of that
+ length may actually match the pattern. Although the value is, strictly,
+ in characters, we treat it as code units to avoid spending too much time
+ in this optimization. */
+
+ if (end_subject - start_match < re->minlength) goto NOMATCH_EXIT;
+
+ /* If req_cu is set, we know that that code unit must appear in the
+ subject for the match to succeed. If the first code unit is set, req_cu
+ must be later in the subject; otherwise the test starts at the match
+ point. This optimization can save a huge amount of backtracking in
+ patterns with nested unlimited repeats that aren't going to match.
+ Writing separate code for cased/caseless versions makes it go faster, as
+ does using an autoincrement and backing off on a match.
+
+ HOWEVER: when the subject string is very, very long, searching to its end
+ can take a long time, and give bad performance on quite ordinary
+ patterns. This showed up when somebody was matching something like
+ /^\d+C/ on a 32-megabyte string... so we don't do this when the string is
+ sufficiently long. */
+
+ if (has_req_cu && end_subject - start_match < REQ_CU_MAX)
+ {
+ PCRE2_SPTR p = start_match + (has_first_cu? 1:0);
+
+ /* We don't need to repeat the search if we haven't yet reached the
+ place we found it at last time. */
+
+ if (p > req_cu_ptr)
+ {
+ if (req_cu != req_cu2)
+ {
+ while (p < end_subject)
+ {
+ uint32_t pp = UCHAR21INCTEST(p);
+ if (pp == req_cu || pp == req_cu2) { p--; break; }
+ }
+ }
+ else
+ {
+ while (p < end_subject)
+ {
+ if (UCHAR21INCTEST(p) == req_cu) { p--; break; }
+ }
+ }
+
+ /* If we can't find the required code unit, break the matching loop,
+ forcing a match failure. */
+
+ if (p >= end_subject) break;
+
+ /* If we have found the required code unit, save the point where we
+ found it, so that we don't search again next time round the loop if
+ the start hasn't passed this code unit yet. */
+
+ req_cu_ptr = p;
+ }
+ }
+ }
+ }
+
+ /* ------------ End of start of match optimizations ------------ */
+
+ /* Give no match if we have passed the bumpalong limit. */
+
+ if (start_match > bumpalong_limit) break;
+
+ /* OK, now we can do the business */
+
+ mb->start_used_ptr = start_match;
+ mb->last_used_ptr = start_match;
+ mb->recursive = NULL;
+
+ rc = internal_dfa_match(
+ mb, /* fixed match data */
+ mb->start_code, /* this subexpression's code */
+ start_match, /* where we currently are */
+ start_offset, /* start offset in subject */
+ match_data->ovector, /* offset vector */
+ (uint32_t)match_data->oveccount * 2, /* actual size of same */
+ workspace, /* workspace vector */
+ (int)wscount, /* size of same */
+ 0, /* function recurse level */
+ base_recursion_workspace); /* initial workspace for recursion */
+
+ /* Anything other than "no match" means we are done, always; otherwise, carry
+ on only if not anchored. */
+
+ if (rc != PCRE2_ERROR_NOMATCH || anchored)
+ {
+ if (rc == PCRE2_ERROR_PARTIAL && match_data->oveccount > 0)
+ {
+ match_data->ovector[0] = (PCRE2_SIZE)(start_match - subject);
+ match_data->ovector[1] = (PCRE2_SIZE)(end_subject - subject);
+ }
+ match_data->leftchar = (PCRE2_SIZE)(mb->start_used_ptr - subject);
+ match_data->rightchar = (PCRE2_SIZE)( mb->last_used_ptr - subject);
+ match_data->startchar = (PCRE2_SIZE)(start_match - subject);
+ match_data->rc = rc;
+ goto EXIT;
+ }
+
+ /* Advance to the next subject character unless we are at the end of a line
+ and firstline is set. */
+
+ if (firstline && IS_NEWLINE(start_match)) break;
+ start_match++;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ ACROSSCHAR(start_match < end_subject, start_match, start_match++);
+ }
+#endif
+ if (start_match > end_subject) break;
+
+ /* If we have just passed a CR and we are now at a LF, and the pattern does
+ not contain any explicit matches for \r or \n, and the newline option is CRLF
+ or ANY or ANYCRLF, advance the match position by one more character. */
+
+ if (UCHAR21TEST(start_match - 1) == CHAR_CR &&
+ start_match < end_subject &&
+ UCHAR21TEST(start_match) == CHAR_NL &&
+ (re->flags & PCRE2_HASCRORLF) == 0 &&
+ (mb->nltype == NLTYPE_ANY ||
+ mb->nltype == NLTYPE_ANYCRLF ||
+ mb->nllen == 2))
+ start_match++;
+
+ } /* "Bumpalong" loop */
+
+NOMATCH_EXIT:
+rc = PCRE2_ERROR_NOMATCH;
+
+EXIT:
+while (rws->next != NULL)
+ {
+ RWS_anchor *next = rws->next;
+ rws->next = next->next;
+ mb->memctl.free(next, mb->memctl.memory_data);
+ }
+
+return rc;
+}
+
+/* End of pcre2_dfa_match.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_error.c b/test/monniaux/pcre2-10.32/pcre2_error.c
new file mode 100644
index 00000000..4b3b3f1b
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_error.c
@@ -0,0 +1,334 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+#define STRING(a) # a
+#define XSTRING(s) STRING(s)
+
+/* The texts of compile-time error messages. Compile-time error numbers start
+at COMPILE_ERROR_BASE (100).
+
+This used to be a table of strings, but in order to reduce the number of
+relocations needed when a shared library is loaded dynamically, it is now one
+long string. We cannot use a table of offsets, because the lengths of inserts
+such as XSTRING(MAX_NAME_SIZE) are not known. Instead,
+pcre2_get_error_message() counts through to the one it wants - this isn't a
+performance issue because these strings are used only when there is an error.
+
+Each substring ends with \0 to insert a null character. This includes the final
+substring, so that the whole string ends with \0\0, which can be detected when
+counting through. */
+
+static const unsigned char compile_error_texts[] =
+ "no error\0"
+ "\\ at end of pattern\0"
+ "\\c at end of pattern\0"
+ "unrecognized character follows \\\0"
+ "numbers out of order in {} quantifier\0"
+ /* 5 */
+ "number too big in {} quantifier\0"
+ "missing terminating ] for character class\0"
+ "invalid escape sequence in character class\0"
+ "range out of order in character class\0"
+ "quantifier does not follow a repeatable item\0"
+ /* 10 */
+ "internal error: unexpected repeat\0"
+ "unrecognized character after (? or (?-\0"
+ "POSIX named classes are supported only within a class\0"
+ "POSIX collating elements are not supported\0"
+ "missing closing parenthesis\0"
+ /* 15 */
+ "reference to non-existent subpattern\0"
+ "pattern passed as NULL\0"
+ "unrecognised compile-time option bit(s)\0"
+ "missing ) after (?# comment\0"
+ "parentheses are too deeply nested\0"
+ /* 20 */
+ "regular expression is too large\0"
+ "failed to allocate heap memory\0"
+ "unmatched closing parenthesis\0"
+ "internal error: code overflow\0"
+ "missing closing parenthesis for condition\0"
+ /* 25 */
+ "lookbehind assertion is not fixed length\0"
+ "a relative value of zero is not allowed\0"
+ "conditional group contains more than two branches\0"
+ "assertion expected after (?( or (?(?C)\0"
+ "digit expected after (?+ or (?-\0"
+ /* 30 */
+ "unknown POSIX class name\0"
+ "internal error in pcre2_study(): should not occur\0"
+ "this version of PCRE2 does not have Unicode support\0"
+ "parentheses are too deeply nested (stack check)\0"
+ "character code point value in \\x{} or \\o{} is too large\0"
+ /* 35 */
+ "lookbehind is too complicated\0"
+ "\\C is not allowed in a lookbehind assertion in UTF-" XSTRING(PCRE2_CODE_UNIT_WIDTH) " mode\0"
+ "PCRE2 does not support \\F, \\L, \\l, \\N{name}, \\U, or \\u\0"
+ "number after (?C is greater than 255\0"
+ "closing parenthesis for (?C expected\0"
+ /* 40 */
+ "invalid escape sequence in (*VERB) name\0"
+ "unrecognized character after (?P\0"
+ "syntax error in subpattern name (missing terminator)\0"
+ "two named subpatterns have the same name (PCRE2_DUPNAMES not set)\0"
+ "group name must start with a non-digit\0"
+ /* 45 */
+ "this version of PCRE2 does not have support for \\P, \\p, or \\X\0"
+ "malformed \\P or \\p sequence\0"
+ "unknown property name after \\P or \\p\0"
+ "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
+ "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
+ /* 50 */
+ "invalid range in character class\0"
+ "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
+ "internal error: overran compiling workspace\0"
+ "internal error: previously-checked referenced subpattern not found\0"
+ "DEFINE group contains more than one branch\0"
+ /* 55 */
+ "missing opening brace after \\o\0"
+ "internal error: unknown newline setting\0"
+ "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
+ "(?R (recursive pattern call) must be followed by a closing parenthesis\0"
+ /* "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0" */
+ "obsolete error (should not occur)\0" /* Was the above */
+ /* 60 */
+ "(*VERB) not recognized or malformed\0"
+ "group number is too big\0"
+ "subpattern name expected\0"
+ "internal error: parsed pattern overflow\0"
+ "non-octal character in \\o{} (closing brace missing?)\0"
+ /* 65 */
+ "different names for subpatterns of the same number are not allowed\0"
+ "(*MARK) must have an argument\0"
+ "non-hex character in \\x{} (closing brace missing?)\0"
+#ifndef EBCDIC
+ "\\c must be followed by a printable ASCII character\0"
+#else
+ "\\c must be followed by a letter or one of [\\]^_?\0"
+#endif
+ "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
+ /* 70 */
+ "internal error: unknown meta code in check_lookbehinds()\0"
+ "\\N is not supported in a class\0"
+ "callout string is too long\0"
+ "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
+ "using UTF is disabled by the application\0"
+ /* 75 */
+ "using UCP is disabled by the application\0"
+ "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
+ "character code point value in \\u.... sequence is too large\0"
+ "digits missing in \\x{} or \\o{} or \\N{U+}\0"
+ "syntax error or number too big in (?(VERSION condition\0"
+ /* 80 */
+ "internal error: unknown opcode in auto_possessify()\0"
+ "missing terminating delimiter for callout with string argument\0"
+ "unrecognized string delimiter follows (?C\0"
+ "using \\C is disabled by the application\0"
+ "(?| and/or (?J: or (?x: parentheses are too deeply nested\0"
+ /* 85 */
+ "using \\C is disabled in this PCRE2 library\0"
+ "regular expression is too complicated\0"
+ "lookbehind assertion is too long\0"
+ "pattern string is longer than the limit set by the application\0"
+ "internal error: unknown code in parsed pattern\0"
+ /* 90 */
+ "internal error: bad code value in parsed_skip()\0"
+ "PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is not allowed in UTF-16 mode\0"
+ "invalid option bits with PCRE2_LITERAL\0"
+ "\\N{U+dddd} is supported only in Unicode (UTF) mode\0"
+ "invalid hyphen in option setting\0"
+ ;
+
+/* Match-time and UTF error texts are in the same format. */
+
+static const unsigned char match_error_texts[] =
+ "no error\0"
+ "no match\0"
+ "partial match\0"
+ "UTF-8 error: 1 byte missing at end\0"
+ "UTF-8 error: 2 bytes missing at end\0"
+ /* 5 */
+ "UTF-8 error: 3 bytes missing at end\0"
+ "UTF-8 error: 4 bytes missing at end\0"
+ "UTF-8 error: 5 bytes missing at end\0"
+ "UTF-8 error: byte 2 top bits not 0x80\0"
+ "UTF-8 error: byte 3 top bits not 0x80\0"
+ /* 10 */
+ "UTF-8 error: byte 4 top bits not 0x80\0"
+ "UTF-8 error: byte 5 top bits not 0x80\0"
+ "UTF-8 error: byte 6 top bits not 0x80\0"
+ "UTF-8 error: 5-byte character is not allowed (RFC 3629)\0"
+ "UTF-8 error: 6-byte character is not allowed (RFC 3629)\0"
+ /* 15 */
+ "UTF-8 error: code points greater than 0x10ffff are not defined\0"
+ "UTF-8 error: code points 0xd800-0xdfff are not defined\0"
+ "UTF-8 error: overlong 2-byte sequence\0"
+ "UTF-8 error: overlong 3-byte sequence\0"
+ "UTF-8 error: overlong 4-byte sequence\0"
+ /* 20 */
+ "UTF-8 error: overlong 5-byte sequence\0"
+ "UTF-8 error: overlong 6-byte sequence\0"
+ "UTF-8 error: isolated byte with 0x80 bit set\0"
+ "UTF-8 error: illegal byte (0xfe or 0xff)\0"
+ "UTF-16 error: missing low surrogate at end\0"
+ /* 25 */
+ "UTF-16 error: invalid low surrogate\0"
+ "UTF-16 error: isolated low surrogate\0"
+ "UTF-32 error: code points 0xd800-0xdfff are not defined\0"
+ "UTF-32 error: code points greater than 0x10ffff are not defined\0"
+ "bad data value\0"
+ /* 30 */
+ "patterns do not all use the same character tables\0"
+ "magic number missing\0"
+ "pattern compiled in wrong mode: 8/16/32-bit error\0"
+ "bad offset value\0"
+ "bad option value\0"
+ /* 35 */
+ "invalid replacement string\0"
+ "bad offset into UTF string\0"
+ "callout error code\0" /* Never returned by PCRE2 itself */
+ "invalid data in workspace for DFA restart\0"
+ "too much recursion for DFA matching\0"
+ /* 40 */
+ "backreference condition or recursion test is not supported for DFA matching\0"
+ "function is not supported for DFA matching\0"
+ "pattern contains an item that is not supported for DFA matching\0"
+ "workspace size exceeded in DFA matching\0"
+ "internal error - pattern overwritten?\0"
+ /* 45 */
+ "bad JIT option\0"
+ "JIT stack limit reached\0"
+ "match limit exceeded\0"
+ "no more memory\0"
+ "unknown substring\0"
+ /* 50 */
+ "non-unique substring name\0"
+ "NULL argument passed\0"
+ "nested recursion at the same subject position\0"
+ "matching depth limit exceeded\0"
+ "requested value is not available\0"
+ /* 55 */
+ "requested value is not set\0"
+ "offset limit set without PCRE2_USE_OFFSET_LIMIT\0"
+ "bad escape sequence in replacement string\0"
+ "expected closing curly bracket in replacement string\0"
+ "bad substitution in replacement string\0"
+ /* 60 */
+ "match with end before start or start moved backwards is not supported\0"
+ "too many replacements (more than INT_MAX)\0"
+ "bad serialized data\0"
+ "heap limit exceeded\0"
+ "invalid syntax\0"
+ /* 65 */
+ "internal error - duplicate substitution match\0"
+ ;
+
+
+/*************************************************
+* Return error message *
+*************************************************/
+
+/* This function copies an error message into a buffer whose units are of an
+appropriate width. Error numbers are positive for compile-time errors, and
+negative for match-time errors (except for UTF errors), but the numbers are all
+distinct.
+
+Arguments:
+ enumber error number
+ buffer where to put the message (zero terminated)
+ size size of the buffer in code units
+
+Returns: length of message if all is well
+ negative on error
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_get_error_message(int enumber, PCRE2_UCHAR *buffer, PCRE2_SIZE size)
+{
+const unsigned char *message;
+PCRE2_SIZE i;
+int n;
+
+if (size == 0) return PCRE2_ERROR_NOMEMORY;
+
+if (enumber >= COMPILE_ERROR_BASE) /* Compile error */
+ {
+ message = compile_error_texts;
+ n = enumber - COMPILE_ERROR_BASE;
+ }
+else if (enumber < 0) /* Match or UTF error */
+ {
+ message = match_error_texts;
+ n = -enumber;
+ }
+else /* Invalid error number */
+ {
+ message = (unsigned char *)"\0"; /* Empty message list */
+ n = 1;
+ }
+
+for (; n > 0; n--)
+ {
+ while (*message++ != CHAR_NUL) {};
+ if (*message == CHAR_NUL) return PCRE2_ERROR_BADDATA;
+ }
+
+for (i = 0; *message != 0; i++)
+ {
+ if (i >= size - 1)
+ {
+ buffer[i] = 0; /* Terminate partial message */
+ return PCRE2_ERROR_NOMEMORY;
+ }
+ buffer[i] = *message++;
+ }
+
+buffer[i] = 0;
+return (int)i;
+}
+
+/* End of pcre2_error.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_extuni.c b/test/monniaux/pcre2-10.32/pcre2_extuni.c
new file mode 100644
index 00000000..237211ab
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_extuni.c
@@ -0,0 +1,148 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains an internal function that is used to match a Unicode
+extended grapheme sequence. It is used by both pcre2_match() and
+pcre2_def_match(). However, it is called only when Unicode support is being
+compiled. Nevertheless, we provide a dummy function when there is no Unicode
+support, because some compilers do not like functionless source files. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+#include "pcre2_internal.h"
+
+
+/* Dummy function */
+
+#ifndef SUPPORT_UNICODE
+PCRE2_SPTR
+PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
+ PCRE2_SPTR end_subject, BOOL utf, int *xcount)
+{
+(void)c;
+(void)eptr;
+(void)start_subject;
+(void)end_subject;
+(void)utf;
+(void)xcount;
+return NULL;
+}
+#else
+
+
+/*************************************************
+* Match an extended grapheme sequence *
+*************************************************/
+
+/*
+Arguments:
+ c the first character
+ eptr pointer to next character
+ start_subject pointer to start of subject
+ end_subject pointer to end of subject
+ utf TRUE if in UTF mode
+ xcount pointer to count of additional characters,
+ or NULL if count not needed
+
+Returns: pointer after the end of the sequence
+*/
+
+PCRE2_SPTR
+PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
+ PCRE2_SPTR end_subject, BOOL utf, int *xcount)
+{
+int lgb = UCD_GRAPHBREAK(c);
+
+while (eptr < end_subject)
+ {
+ int rgb;
+ int len = 1;
+ if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
+ rgb = UCD_GRAPHBREAK(c);
+ if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
+
+ /* Not breaking between Regional Indicators is allowed only if there
+ are an even number of preceding RIs. */
+
+ if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
+ {
+ int ricount = 0;
+ PCRE2_SPTR bptr = eptr - 1;
+ if (utf) BACKCHAR(bptr);
+
+ /* bptr is pointing to the left-hand character */
+
+ while (bptr > start_subject)
+ {
+ bptr--;
+ if (utf)
+ {
+ BACKCHAR(bptr);
+ GETCHAR(c, bptr);
+ }
+ else
+ c = *bptr;
+ if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
+ ricount++;
+ }
+ if ((ricount & 1) != 0) break; /* Grapheme break required */
+ }
+
+ /* If Extend or ZWJ follows Extended_Pictographic, do not update lgb; this
+ allows any number of them before a following Extended_Pictographic. */
+
+ if ((rgb != ucp_gbExtend && rgb != ucp_gbZWJ) ||
+ lgb != ucp_gbExtended_Pictographic)
+ lgb = rgb;
+
+ eptr += len;
+ if (xcount != NULL) *xcount += 1;
+ }
+
+return eptr;
+}
+
+#endif /* SUPPORT_UNICODE */
+
+/* End of pcre2_extuni.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_find_bracket.c b/test/monniaux/pcre2-10.32/pcre2_find_bracket.c
new file mode 100644
index 00000000..70baa139
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_find_bracket.c
@@ -0,0 +1,219 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains a single function that scans through a compiled pattern
+until it finds a capturing bracket with the given number, or, if the number is
+negative, an instance of OP_REVERSE for a lookbehind. The function is called
+from pcre2_compile.c and also from pcre2_study.c when finding the minimum
+matching length. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+/*************************************************
+* Scan compiled regex for specific bracket *
+*************************************************/
+
+/*
+Arguments:
+ code points to start of expression
+ utf TRUE in UTF mode
+ number the required bracket number or negative to find a lookbehind
+
+Returns: pointer to the opcode for the bracket, or NULL if not found
+*/
+
+PCRE2_SPTR
+PRIV(find_bracket)(PCRE2_SPTR code, BOOL utf, int number)
+{
+for (;;)
+ {
+ PCRE2_UCHAR c = *code;
+
+ if (c == OP_END) return NULL;
+
+ /* XCLASS is used for classes that cannot be represented just by a bit map.
+ This includes negated single high-valued characters. CALLOUT_STR is used for
+ callouts with string arguments. In both cases the length in the table is
+ zero; the actual length is stored in the compiled code. */
+
+ if (c == OP_XCLASS) code += GET(code, 1);
+ else if (c == OP_CALLOUT_STR) code += GET(code, 1 + 2*LINK_SIZE);
+
+ /* Handle lookbehind */
+
+ else if (c == OP_REVERSE)
+ {
+ if (number < 0) return (PCRE2_UCHAR *)code;
+ code += PRIV(OP_lengths)[c];
+ }
+
+ /* Handle capturing bracket */
+
+ else if (c == OP_CBRA || c == OP_SCBRA ||
+ c == OP_CBRAPOS || c == OP_SCBRAPOS)
+ {
+ int n = (int)GET2(code, 1+LINK_SIZE);
+ if (n == number) return (PCRE2_UCHAR *)code;
+ code += PRIV(OP_lengths)[c];
+ }
+
+ /* Otherwise, we can get the item's length from the table, except that for
+ repeated character types, we have to test for \p and \P, which have an extra
+ two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
+ must add in its length. */
+
+ else
+ {
+ switch(c)
+ {
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
+ break;
+
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSUPTO:
+ if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
+ code += 2;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ code += code[1];
+ break;
+ }
+
+ /* Add in the fixed length from the table */
+
+ code += PRIV(OP_lengths)[c];
+
+ /* In UTF-8 and UTF-16 modes, opcodes that are followed by a character may be
+ followed by a multi-byte character. The length in the table is a minimum, so
+ we have to arrange to skip the extra bytes. */
+
+#ifdef MAYBE_UTF_MULTI
+ if (utf) switch(c)
+ {
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_EXACT:
+ case OP_EXACTI:
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ case OP_UPTO:
+ case OP_UPTOI:
+ case OP_NOTUPTO:
+ case OP_NOTUPTOI:
+ case OP_MINUPTO:
+ case OP_MINUPTOI:
+ case OP_NOTMINUPTO:
+ case OP_NOTMINUPTOI:
+ case OP_POSUPTO:
+ case OP_POSUPTOI:
+ case OP_NOTPOSUPTO:
+ case OP_NOTPOSUPTOI:
+ case OP_STAR:
+ case OP_STARI:
+ case OP_NOTSTAR:
+ case OP_NOTSTARI:
+ case OP_MINSTAR:
+ case OP_MINSTARI:
+ case OP_NOTMINSTAR:
+ case OP_NOTMINSTARI:
+ case OP_POSSTAR:
+ case OP_POSSTARI:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSSTARI:
+ case OP_PLUS:
+ case OP_PLUSI:
+ case OP_NOTPLUS:
+ case OP_NOTPLUSI:
+ case OP_MINPLUS:
+ case OP_MINPLUSI:
+ case OP_NOTMINPLUS:
+ case OP_NOTMINPLUSI:
+ case OP_POSPLUS:
+ case OP_POSPLUSI:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSPLUSI:
+ case OP_QUERY:
+ case OP_QUERYI:
+ case OP_NOTQUERY:
+ case OP_NOTQUERYI:
+ case OP_MINQUERY:
+ case OP_MINQUERYI:
+ case OP_NOTMINQUERY:
+ case OP_NOTMINQUERYI:
+ case OP_POSQUERY:
+ case OP_POSQUERYI:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSQUERYI:
+ if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
+ break;
+ }
+#else
+ (void)(utf); /* Keep compiler happy by referencing function argument */
+#endif /* MAYBE_UTF_MULTI */
+ }
+ }
+}
+
+/* End of pcre2_find_bracket.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_internal.h b/test/monniaux/pcre2-10.32/pcre2_internal.h
new file mode 100644
index 00000000..8750f2f1
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_internal.h
@@ -0,0 +1,1945 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE2 is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#ifndef PCRE2_INTERNAL_H_IDEMPOTENT_GUARD
+#define PCRE2_INTERNAL_H_IDEMPOTENT_GUARD
+
+/* We do not support both EBCDIC and Unicode at the same time. The "configure"
+script prevents both being selected, but not everybody uses "configure". EBCDIC
+is only supported for the 8-bit library, but the check for this has to be later
+in this file, because the first part is not width-dependent, and is included by
+pcre2test.c with CODE_UNIT_WIDTH == 0. */
+
+#if defined EBCDIC && defined SUPPORT_UNICODE
+#error The use of both EBCDIC and SUPPORT_UNICODE is not supported.
+#endif
+
+/* Standard C headers */
+
+#include <ctype.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Macros to make boolean values more obvious. The #ifndef is to pacify
+compiler warnings in environments where these macros are defined elsewhere.
+Unfortunately, there is no way to do the same for the typedef. */
+
+typedef int BOOL;
+#ifndef FALSE
+#define FALSE 0
+#define TRUE 1
+#endif
+
+/* Valgrind (memcheck) support */
+
+#ifdef SUPPORT_VALGRIND
+#include <valgrind/memcheck.h>
+#endif
+
+/* Older versions of MSVC lack snprintf(). This define allows for
+warning/error-free compilation and testing with MSVC compilers back to at least
+MSVC 10/2010. Except for VC6 (which is missing some fundamentals and fails). */
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+#define snprintf _snprintf
+#endif
+
+/* When compiling a DLL for Windows, the exported symbols have to be declared
+using some MS magic. I found some useful information on this web page:
+http://msdn2.microsoft.com/en-us/library/y4h7bcy6(VS.80).aspx. According to the
+information there, using __declspec(dllexport) without "extern" we have a
+definition; with "extern" we have a declaration. The settings here override the
+setting in pcre2.h (which is included below); it defines only PCRE2_EXP_DECL,
+which is all that is needed for applications (they just import the symbols). We
+use:
+
+ PCRE2_EXP_DECL for declarations
+ PCRE2_EXP_DEFN for definitions
+
+The reason for wrapping this in #ifndef PCRE2_EXP_DECL is so that pcre2test,
+which is an application, but needs to import this file in order to "peek" at
+internals, can #include pcre2.h first to get an application's-eye view.
+
+In principle, people compiling for non-Windows, non-Unix-like (i.e. uncommon,
+special-purpose environments) might want to stick other stuff in front of
+exported symbols. That's why, in the non-Windows case, we set PCRE2_EXP_DEFN
+only if it is not already set. */
+
+#ifndef PCRE2_EXP_DECL
+# ifdef _WIN32
+# ifndef PCRE2_STATIC
+# define PCRE2_EXP_DECL extern __declspec(dllexport)
+# define PCRE2_EXP_DEFN __declspec(dllexport)
+# else
+# define PCRE2_EXP_DECL extern
+# define PCRE2_EXP_DEFN
+# endif
+# else
+# ifdef __cplusplus
+# define PCRE2_EXP_DECL extern "C"
+# else
+# define PCRE2_EXP_DECL extern
+# endif
+# ifndef PCRE2_EXP_DEFN
+# define PCRE2_EXP_DEFN PCRE2_EXP_DECL
+# endif
+# endif
+#endif
+
+/* Include the public PCRE2 header and the definitions of UCP character
+property values. This must follow the setting of PCRE2_EXP_DECL above. */
+
+#include "pcre2.h"
+#include "pcre2_ucp.h"
+
+/* When PCRE2 is compiled as a C++ library, the subject pointer can be replaced
+with a custom type. This makes it possible, for example, to allow pcre2_match()
+to process subject strings that are discontinuous by using a smart pointer
+class. It must always be possible to inspect all of the subject string in
+pcre2_match() because of the way it backtracks. */
+
+/* WARNING: This is as yet untested for PCRE2. */
+
+#ifdef CUSTOM_SUBJECT_PTR
+#undef PCRE2_SPTR
+#define PCRE2_SPTR CUSTOM_SUBJECT_PTR
+#endif
+
+/* When checking for integer overflow in pcre2_compile(), we need to handle
+large integers. If a 64-bit integer type is available, we can use that.
+Otherwise we have to cast to double, which of course requires floating point
+arithmetic. Handle this by defining a macro for the appropriate type. If
+stdint.h is available, include it; it may define INT64_MAX. Systems that do not
+have stdint.h (e.g. Solaris) may have inttypes.h. The macro int64_t may be set
+by "configure". */
+
+#if defined HAVE_STDINT_H
+#include <stdint.h>
+#elif defined HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
+#if defined INT64_MAX || defined int64_t
+#define INT64_OR_DOUBLE int64_t
+#else
+#define INT64_OR_DOUBLE double
+#endif
+
+/* External (in the C sense) functions and tables that are private to the
+libraries are always referenced using the PRIV macro. This makes it possible
+for pcre2test.c to include some of the source files from the libraries using a
+different PRIV definition to avoid name clashes. It also makes it clear in the
+code that a non-static object is being referenced. */
+
+#ifndef PRIV
+#define PRIV(name) _pcre2_##name
+#endif
+
+/* When compiling for use with the Virtual Pascal compiler, these functions
+need to have their names changed. PCRE2 must be compiled with the -DVPCOMPAT
+option on the command line. */
+
+#ifdef VPCOMPAT
+#define strlen(s) _strlen(s)
+#define strncmp(s1,s2,m) _strncmp(s1,s2,m)
+#define memcmp(s,c,n) _memcmp(s,c,n)
+#define memcpy(d,s,n) _memcpy(d,s,n)
+#define memmove(d,s,n) _memmove(d,s,n)
+#define memset(s,c,n) _memset(s,c,n)
+#else /* VPCOMPAT */
+
+/* Otherwise, to cope with SunOS4 and other systems that lack memmove(), define
+a macro that calls an emulating function. */
+
+#ifndef HAVE_MEMMOVE
+#undef memmove /* Some systems may have a macro */
+#define memmove(a, b, c) PRIV(memmove)(a, b, c)
+#endif /* not HAVE_MEMMOVE */
+#endif /* not VPCOMPAT */
+
+/* This is an unsigned int value that no UTF character can ever have, as
+Unicode doesn't go beyond 0x0010ffff. */
+
+#define NOTACHAR 0xffffffff
+
+/* This is the largest valid UTF/Unicode code point. */
+
+#define MAX_UTF_CODE_POINT 0x10ffff
+
+/* Compile-time positive error numbers (all except UTF errors, which are
+negative) start at this value. It should probably never be changed, in case
+some application is checking for specific numbers. There is a copy of this
+#define in pcre2posix.c (which now no longer includes this file). Ideally, a
+way of having a single definition should be found, but as the number is
+unlikely to change, this is not a pressing issue. The original reason for
+having a base other than 0 was to keep the absolute values of compile-time and
+run-time error numbers numerically different, but in the event the code does
+not rely on this. */
+
+#define COMPILE_ERROR_BASE 100
+
+/* The initial frames vector for remembering backtracking points in
+pcre2_match() is allocated on the system stack, of this size (bytes). The size
+must be a multiple of sizeof(PCRE2_SPTR) in all environments, so making it a
+multiple of 8 is best. Typical frame sizes are a few hundred bytes (it depends
+on the number of capturing parentheses) so 20KiB handles quite a few frames. A
+larger vector on the heap is obtained for patterns that need more frames. The
+maximum size of this can be limited. */
+
+#define START_FRAMES_SIZE 20480
+
+/* Similarly, for DFA matching, an initial internal workspace vector is
+allocated on the stack. */
+
+#define DFA_START_RWS_SIZE 30720
+
+/* Define the default BSR convention. */
+
+#ifdef BSR_ANYCRLF
+#define BSR_DEFAULT PCRE2_BSR_ANYCRLF
+#else
+#define BSR_DEFAULT PCRE2_BSR_UNICODE
+#endif
+
+
+/* ---------------- Basic UTF-8 macros ---------------- */
+
+/* These UTF-8 macros are always defined because they are used in pcre2test for
+handling wide characters in 16-bit and 32-bit modes, even if an 8-bit library
+is not supported. */
+
+/* Tests whether a UTF-8 code point needs extra bytes to decode. */
+
+#define HASUTF8EXTRALEN(c) ((c) >= 0xc0)
+
+/* The following macros were originally written in the form of loops that used
+data from the tables whose names start with PRIV(utf8_table). They were
+rewritten by a user so as not to use loops, because in some environments this
+gives a significant performance advantage, and it seems never to do any harm.
+*/
+
+/* Base macro to pick up the remaining bytes of a UTF-8 character, not
+advancing the pointer. */
+
+#define GETUTF8(c, eptr) \
+ { \
+ if ((c & 0x20u) == 0) \
+ c = ((c & 0x1fu) << 6) | (eptr[1] & 0x3fu); \
+ else if ((c & 0x10u) == 0) \
+ c = ((c & 0x0fu) << 12) | ((eptr[1] & 0x3fu) << 6) | (eptr[2] & 0x3fu); \
+ else if ((c & 0x08u) == 0) \
+ c = ((c & 0x07u) << 18) | ((eptr[1] & 0x3fu) << 12) | \
+ ((eptr[2] & 0x3fu) << 6) | (eptr[3] & 0x3fu); \
+ else if ((c & 0x04u) == 0) \
+ c = ((c & 0x03u) << 24) | ((eptr[1] & 0x3fu) << 18) | \
+ ((eptr[2] & 0x3fu) << 12) | ((eptr[3] & 0x3fu) << 6) | \
+ (eptr[4] & 0x3fu); \
+ else \
+ c = ((c & 0x01u) << 30) | ((eptr[1] & 0x3fu) << 24) | \
+ ((eptr[2] & 0x3fu) << 18) | ((eptr[3] & 0x3fu) << 12) | \
+ ((eptr[4] & 0x3fu) << 6) | (eptr[5] & 0x3fu); \
+ }
+
+/* Base macro to pick up the remaining bytes of a UTF-8 character, advancing
+the pointer. */
+
+#define GETUTF8INC(c, eptr) \
+ { \
+ if ((c & 0x20u) == 0) \
+ c = ((c & 0x1fu) << 6) | (*eptr++ & 0x3fu); \
+ else if ((c & 0x10u) == 0) \
+ { \
+ c = ((c & 0x0fu) << 12) | ((*eptr & 0x3fu) << 6) | (eptr[1] & 0x3fu); \
+ eptr += 2; \
+ } \
+ else if ((c & 0x08u) == 0) \
+ { \
+ c = ((c & 0x07u) << 18) | ((*eptr & 0x3fu) << 12) | \
+ ((eptr[1] & 0x3fu) << 6) | (eptr[2] & 0x3fu); \
+ eptr += 3; \
+ } \
+ else if ((c & 0x04u) == 0) \
+ { \
+ c = ((c & 0x03u) << 24) | ((*eptr & 0x3fu) << 18) | \
+ ((eptr[1] & 0x3fu) << 12) | ((eptr[2] & 0x3fu) << 6) | \
+ (eptr[3] & 0x3fu); \
+ eptr += 4; \
+ } \
+ else \
+ { \
+ c = ((c & 0x01u) << 30) | ((*eptr & 0x3fu) << 24) | \
+ ((eptr[1] & 0x3fu) << 18) | ((eptr[2] & 0x3fu) << 12) | \
+ ((eptr[3] & 0x3fu) << 6) | (eptr[4] & 0x3fu); \
+ eptr += 5; \
+ } \
+ }
+
+/* Base macro to pick up the remaining bytes of a UTF-8 character, not
+advancing the pointer, incrementing the length. */
+
+#define GETUTF8LEN(c, eptr, len) \
+ { \
+ if ((c & 0x20u) == 0) \
+ { \
+ c = ((c & 0x1fu) << 6) | (eptr[1] & 0x3fu); \
+ len++; \
+ } \
+ else if ((c & 0x10u) == 0) \
+ { \
+ c = ((c & 0x0fu) << 12) | ((eptr[1] & 0x3fu) << 6) | (eptr[2] & 0x3fu); \
+ len += 2; \
+ } \
+ else if ((c & 0x08u) == 0) \
+ {\
+ c = ((c & 0x07u) << 18) | ((eptr[1] & 0x3fu) << 12) | \
+ ((eptr[2] & 0x3fu) << 6) | (eptr[3] & 0x3fu); \
+ len += 3; \
+ } \
+ else if ((c & 0x04u) == 0) \
+ { \
+ c = ((c & 0x03u) << 24) | ((eptr[1] & 0x3fu) << 18) | \
+ ((eptr[2] & 0x3fu) << 12) | ((eptr[3] & 0x3fu) << 6) | \
+ (eptr[4] & 0x3fu); \
+ len += 4; \
+ } \
+ else \
+ {\
+ c = ((c & 0x01u) << 30) | ((eptr[1] & 0x3fu) << 24) | \
+ ((eptr[2] & 0x3fu) << 18) | ((eptr[3] & 0x3fu) << 12) | \
+ ((eptr[4] & 0x3fu) << 6) | (eptr[5] & 0x3fu); \
+ len += 5; \
+ } \
+ }
+
+/* --------------- Whitespace macros ---------------- */
+
+/* Tests for Unicode horizontal and vertical whitespace characters must check a
+number of different values. Using a switch statement for this generates the
+fastest code (no loop, no memory access), and there are several places in the
+interpreter code where this happens. In order to ensure that all the case lists
+remain in step, we use macros so that there is only one place where the lists
+are defined.
+
+These values are also required as lists in pcre2_compile.c when processing \h,
+\H, \v and \V in a character class. The lists are defined in pcre2_tables.c,
+but macros that define the values are here so that all the definitions are
+together. The lists must be in ascending character order, terminated by
+NOTACHAR (which is 0xffffffff).
+
+Any changes should ensure that the various macros are kept in step with each
+other. NOTE: The values also appear in pcre2_jit_compile.c. */
+
+/* -------------- ASCII/Unicode environments -------------- */
+
+#ifndef EBCDIC
+
+/* Character U+180E (Mongolian Vowel Separator) is not included in the list of
+spaces in the Unicode file PropList.txt, and Perl does not recognize it as a
+space. However, in many other sources it is listed as a space and has been in
+PCRE (both APIs) for a long time. */
+
+#define HSPACE_LIST \
+ CHAR_HT, CHAR_SPACE, CHAR_NBSP, \
+ 0x1680, 0x180e, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, \
+ 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202f, 0x205f, 0x3000, \
+ NOTACHAR
+
+#define HSPACE_MULTIBYTE_CASES \
+ case 0x1680: /* OGHAM SPACE MARK */ \
+ case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */ \
+ case 0x2000: /* EN QUAD */ \
+ case 0x2001: /* EM QUAD */ \
+ case 0x2002: /* EN SPACE */ \
+ case 0x2003: /* EM SPACE */ \
+ case 0x2004: /* THREE-PER-EM SPACE */ \
+ case 0x2005: /* FOUR-PER-EM SPACE */ \
+ case 0x2006: /* SIX-PER-EM SPACE */ \
+ case 0x2007: /* FIGURE SPACE */ \
+ case 0x2008: /* PUNCTUATION SPACE */ \
+ case 0x2009: /* THIN SPACE */ \
+ case 0x200A: /* HAIR SPACE */ \
+ case 0x202f: /* NARROW NO-BREAK SPACE */ \
+ case 0x205f: /* MEDIUM MATHEMATICAL SPACE */ \
+ case 0x3000 /* IDEOGRAPHIC SPACE */
+
+#define HSPACE_BYTE_CASES \
+ case CHAR_HT: \
+ case CHAR_SPACE: \
+ case CHAR_NBSP
+
+#define HSPACE_CASES \
+ HSPACE_BYTE_CASES: \
+ HSPACE_MULTIBYTE_CASES
+
+#define VSPACE_LIST \
+ CHAR_LF, CHAR_VT, CHAR_FF, CHAR_CR, CHAR_NEL, 0x2028, 0x2029, NOTACHAR
+
+#define VSPACE_MULTIBYTE_CASES \
+ case 0x2028: /* LINE SEPARATOR */ \
+ case 0x2029 /* PARAGRAPH SEPARATOR */
+
+#define VSPACE_BYTE_CASES \
+ case CHAR_LF: \
+ case CHAR_VT: \
+ case CHAR_FF: \
+ case CHAR_CR: \
+ case CHAR_NEL
+
+#define VSPACE_CASES \
+ VSPACE_BYTE_CASES: \
+ VSPACE_MULTIBYTE_CASES
+
+/* -------------- EBCDIC environments -------------- */
+
+#else
+#define HSPACE_LIST CHAR_HT, CHAR_SPACE, CHAR_NBSP, NOTACHAR
+
+#define HSPACE_BYTE_CASES \
+ case CHAR_HT: \
+ case CHAR_SPACE: \
+ case CHAR_NBSP
+
+#define HSPACE_CASES HSPACE_BYTE_CASES
+
+#ifdef EBCDIC_NL25
+#define VSPACE_LIST \
+ CHAR_VT, CHAR_FF, CHAR_CR, CHAR_NEL, CHAR_LF, NOTACHAR
+#else
+#define VSPACE_LIST \
+ CHAR_VT, CHAR_FF, CHAR_CR, CHAR_LF, CHAR_NEL, NOTACHAR
+#endif
+
+#define VSPACE_BYTE_CASES \
+ case CHAR_LF: \
+ case CHAR_VT: \
+ case CHAR_FF: \
+ case CHAR_CR: \
+ case CHAR_NEL
+
+#define VSPACE_CASES VSPACE_BYTE_CASES
+#endif /* EBCDIC */
+
+/* -------------- End of whitespace macros -------------- */
+
+
+/* PCRE2 is able to support several different kinds of newline (CR, LF, CRLF,
+"any" and "anycrlf" at present). The following macros are used to package up
+testing for newlines. NLBLOCK, PSSTART, and PSEND are defined in the various
+modules to indicate in which datablock the parameters exist, and what the
+start/end of string field names are. */
+
+#define NLTYPE_FIXED 0 /* Newline is a fixed length string */
+#define NLTYPE_ANY 1 /* Newline is any Unicode line ending */
+#define NLTYPE_ANYCRLF 2 /* Newline is CR, LF, or CRLF */
+
+/* This macro checks for a newline at the given position */
+
+#define IS_NEWLINE(p) \
+ ((NLBLOCK->nltype != NLTYPE_FIXED)? \
+ ((p) < NLBLOCK->PSEND && \
+ PRIV(is_newline)((p), NLBLOCK->nltype, NLBLOCK->PSEND, \
+ &(NLBLOCK->nllen), utf)) \
+ : \
+ ((p) <= NLBLOCK->PSEND - NLBLOCK->nllen && \
+ UCHAR21TEST(p) == NLBLOCK->nl[0] && \
+ (NLBLOCK->nllen == 1 || UCHAR21TEST(p+1) == NLBLOCK->nl[1]) \
+ ) \
+ )
+
+/* This macro checks for a newline immediately preceding the given position */
+
+#define WAS_NEWLINE(p) \
+ ((NLBLOCK->nltype != NLTYPE_FIXED)? \
+ ((p) > NLBLOCK->PSSTART && \
+ PRIV(was_newline)((p), NLBLOCK->nltype, NLBLOCK->PSSTART, \
+ &(NLBLOCK->nllen), utf)) \
+ : \
+ ((p) >= NLBLOCK->PSSTART + NLBLOCK->nllen && \
+ UCHAR21TEST(p - NLBLOCK->nllen) == NLBLOCK->nl[0] && \
+ (NLBLOCK->nllen == 1 || UCHAR21TEST(p - NLBLOCK->nllen + 1) == NLBLOCK->nl[1]) \
+ ) \
+ )
+
+/* Private flags containing information about the compiled pattern. The first
+three must not be changed, because whichever is set is actually the number of
+bytes in a code unit in that mode. */
+
+#define PCRE2_MODE8 0x00000001 /* compiled in 8 bit mode */
+#define PCRE2_MODE16 0x00000002 /* compiled in 16 bit mode */
+#define PCRE2_MODE32 0x00000004 /* compiled in 32 bit mode */
+#define PCRE2_FIRSTSET 0x00000010 /* first_code unit is set */
+#define PCRE2_FIRSTCASELESS 0x00000020 /* caseless first code unit */
+#define PCRE2_FIRSTMAPSET 0x00000040 /* bitmap of first code units is set */
+#define PCRE2_LASTSET 0x00000080 /* last code unit is set */
+#define PCRE2_LASTCASELESS 0x00000100 /* caseless last code unit */
+#define PCRE2_STARTLINE 0x00000200 /* start after \n for multiline */
+#define PCRE2_JCHANGED 0x00000400 /* j option used in pattern */
+#define PCRE2_HASCRORLF 0x00000800 /* explicit \r or \n in pattern */
+#define PCRE2_HASTHEN 0x00001000 /* pattern contains (*THEN) */
+#define PCRE2_MATCH_EMPTY 0x00002000 /* pattern can match empty string */
+#define PCRE2_BSR_SET 0x00004000 /* BSR was set in the pattern */
+#define PCRE2_NL_SET 0x00008000 /* newline was set in the pattern */
+#define PCRE2_NOTEMPTY_SET 0x00010000 /* (*NOTEMPTY) used ) keep */
+#define PCRE2_NE_ATST_SET 0x00020000 /* (*NOTEMPTY_ATSTART) used) together */
+#define PCRE2_DEREF_TABLES 0x00040000 /* release character tables */
+#define PCRE2_NOJIT 0x00080000 /* (*NOJIT) used */
+#define PCRE2_HASBKPORX 0x00100000 /* contains \P, \p, or \X */
+#define PCRE2_DUPCAPUSED 0x00200000 /* contains (?| */
+#define PCRE2_HASBKC 0x00400000 /* contains \C */
+
+#define PCRE2_MODE_MASK (PCRE2_MODE8 | PCRE2_MODE16 | PCRE2_MODE32)
+
+/* Values for the matchedby field in a match data block. */
+
+enum { PCRE2_MATCHEDBY_INTERPRETER, /* pcre2_match() */
+ PCRE2_MATCHEDBY_DFA_INTERPRETER, /* pcre2_dfa_match() */
+ PCRE2_MATCHEDBY_JIT }; /* pcre2_jit_match() */
+
+/* Magic number to provide a small check against being handed junk. */
+
+#define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */
+
+/* The maximum remaining length of subject we are prepared to search for a
+req_unit match. In 8-bit mode, memchr() is used and is much faster than the
+search loop that has to be used in 16-bit and 32-bit modes. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define REQ_CU_MAX 2000
+#else
+#define REQ_CU_MAX 1000
+#endif
+
+/* Offsets for the bitmap tables in the cbits set of tables. Each table
+contains a set of bits for a class map. Some classes are built by combining
+these tables. */
+
+#define cbit_space 0 /* [:space:] or \s */
+#define cbit_xdigit 32 /* [:xdigit:] */
+#define cbit_digit 64 /* [:digit:] or \d */
+#define cbit_upper 96 /* [:upper:] */
+#define cbit_lower 128 /* [:lower:] */
+#define cbit_word 160 /* [:word:] or \w */
+#define cbit_graph 192 /* [:graph:] */
+#define cbit_print 224 /* [:print:] */
+#define cbit_punct 256 /* [:punct:] */
+#define cbit_cntrl 288 /* [:cntrl:] */
+#define cbit_length 320 /* Length of the cbits table */
+
+/* Bit definitions for entries in the ctypes table. Do not change these values
+without checking pcre2_jit_compile.c, which has an assertion to ensure that
+ctype_word has the value 16. */
+
+#define ctype_space 0x01
+#define ctype_letter 0x02
+#define ctype_digit 0x04
+#define ctype_xdigit 0x08 /* not actually used any more */
+#define ctype_word 0x10 /* alphanumeric or '_' */
+
+/* Offsets of the various tables from the base tables pointer, and
+total length of the tables. */
+
+#define lcc_offset 0 /* Lower case */
+#define fcc_offset 256 /* Flip case */
+#define cbits_offset 512 /* Character classes */
+#define ctypes_offset (cbits_offset + cbit_length) /* Character types */
+#define tables_length (ctypes_offset + 256)
+
+
+/* -------------------- Character and string names ------------------------ */
+
+/* If PCRE2 is to support UTF-8 on EBCDIC platforms, we cannot use normal
+character constants like '*' because the compiler would emit their EBCDIC code,
+which is different from their ASCII/UTF-8 code. Instead we define macros for
+the characters so that they always use the ASCII/UTF-8 code when UTF-8 support
+is enabled. When UTF-8 support is not enabled, the definitions use character
+literals. Both character and string versions of each character are needed, and
+there are some longer strings as well.
+
+This means that, on EBCDIC platforms, the PCRE2 library can handle either
+EBCDIC, or UTF-8, but not both. To support both in the same compiled library
+would need different lookups depending on whether PCRE2_UTF was set or not.
+This would make it impossible to use characters in switch/case statements,
+which would reduce performance. For a theoretical use (which nobody has asked
+for) in a minority area (EBCDIC platforms), this is not sensible. Any
+application that did need both could compile two versions of the library, using
+macros to give the functions distinct names. */
+
+#ifndef SUPPORT_UNICODE
+
+/* UTF-8 support is not enabled; use the platform-dependent character literals
+so that PCRE2 works in both ASCII and EBCDIC environments, but only in non-UTF
+mode. Newline characters are problematic in EBCDIC. Though it has CR and LF
+characters, a common practice has been to use its NL (0x15) character as the
+line terminator in C-like processing environments. However, sometimes the LF
+(0x25) character is used instead, according to this Unicode document:
+
+http://unicode.org/standard/reports/tr13/tr13-5.html
+
+PCRE2 defaults EBCDIC NL to 0x15, but has a build-time option to select 0x25
+instead. Whichever is *not* chosen is defined as NEL.
+
+In both ASCII and EBCDIC environments, CHAR_NL and CHAR_LF are synonyms for the
+same code point. */
+
+#ifdef EBCDIC
+
+#ifndef EBCDIC_NL25
+#define CHAR_NL '\x15'
+#define CHAR_NEL '\x25'
+#define STR_NL "\x15"
+#define STR_NEL "\x25"
+#else
+#define CHAR_NL '\x25'
+#define CHAR_NEL '\x15'
+#define STR_NL "\x25"
+#define STR_NEL "\x15"
+#endif
+
+#define CHAR_LF CHAR_NL
+#define STR_LF STR_NL
+
+#define CHAR_ESC '\047'
+#define CHAR_DEL '\007'
+#define CHAR_NBSP ((unsigned char)'\x41')
+#define STR_ESC "\047"
+#define STR_DEL "\007"
+
+#else /* Not EBCDIC */
+
+/* In ASCII/Unicode, linefeed is '\n' and we equate this to NL for
+compatibility. NEL is the Unicode newline character; make sure it is
+a positive value. */
+
+#define CHAR_LF '\n'
+#define CHAR_NL CHAR_LF
+#define CHAR_NEL ((unsigned char)'\x85')
+#define CHAR_ESC '\033'
+#define CHAR_DEL '\177'
+#define CHAR_NBSP ((unsigned char)'\xa0')
+
+#define STR_LF "\n"
+#define STR_NL STR_LF
+#define STR_NEL "\x85"
+#define STR_ESC "\033"
+#define STR_DEL "\177"
+
+#endif /* EBCDIC */
+
+/* The remaining definitions work in both environments. */
+
+#define CHAR_NUL '\0'
+#define CHAR_HT '\t'
+#define CHAR_VT '\v'
+#define CHAR_FF '\f'
+#define CHAR_CR '\r'
+#define CHAR_BS '\b'
+#define CHAR_BEL '\a'
+
+#define CHAR_SPACE ' '
+#define CHAR_EXCLAMATION_MARK '!'
+#define CHAR_QUOTATION_MARK '"'
+#define CHAR_NUMBER_SIGN '#'
+#define CHAR_DOLLAR_SIGN '$'
+#define CHAR_PERCENT_SIGN '%'
+#define CHAR_AMPERSAND '&'
+#define CHAR_APOSTROPHE '\''
+#define CHAR_LEFT_PARENTHESIS '('
+#define CHAR_RIGHT_PARENTHESIS ')'
+#define CHAR_ASTERISK '*'
+#define CHAR_PLUS '+'
+#define CHAR_COMMA ','
+#define CHAR_MINUS '-'
+#define CHAR_DOT '.'
+#define CHAR_SLASH '/'
+#define CHAR_0 '0'
+#define CHAR_1 '1'
+#define CHAR_2 '2'
+#define CHAR_3 '3'
+#define CHAR_4 '4'
+#define CHAR_5 '5'
+#define CHAR_6 '6'
+#define CHAR_7 '7'
+#define CHAR_8 '8'
+#define CHAR_9 '9'
+#define CHAR_COLON ':'
+#define CHAR_SEMICOLON ';'
+#define CHAR_LESS_THAN_SIGN '<'
+#define CHAR_EQUALS_SIGN '='
+#define CHAR_GREATER_THAN_SIGN '>'
+#define CHAR_QUESTION_MARK '?'
+#define CHAR_COMMERCIAL_AT '@'
+#define CHAR_A 'A'
+#define CHAR_B 'B'
+#define CHAR_C 'C'
+#define CHAR_D 'D'
+#define CHAR_E 'E'
+#define CHAR_F 'F'
+#define CHAR_G 'G'
+#define CHAR_H 'H'
+#define CHAR_I 'I'
+#define CHAR_J 'J'
+#define CHAR_K 'K'
+#define CHAR_L 'L'
+#define CHAR_M 'M'
+#define CHAR_N 'N'
+#define CHAR_O 'O'
+#define CHAR_P 'P'
+#define CHAR_Q 'Q'
+#define CHAR_R 'R'
+#define CHAR_S 'S'
+#define CHAR_T 'T'
+#define CHAR_U 'U'
+#define CHAR_V 'V'
+#define CHAR_W 'W'
+#define CHAR_X 'X'
+#define CHAR_Y 'Y'
+#define CHAR_Z 'Z'
+#define CHAR_LEFT_SQUARE_BRACKET '['
+#define CHAR_BACKSLASH '\\'
+#define CHAR_RIGHT_SQUARE_BRACKET ']'
+#define CHAR_CIRCUMFLEX_ACCENT '^'
+#define CHAR_UNDERSCORE '_'
+#define CHAR_GRAVE_ACCENT '`'
+#define CHAR_a 'a'
+#define CHAR_b 'b'
+#define CHAR_c 'c'
+#define CHAR_d 'd'
+#define CHAR_e 'e'
+#define CHAR_f 'f'
+#define CHAR_g 'g'
+#define CHAR_h 'h'
+#define CHAR_i 'i'
+#define CHAR_j 'j'
+#define CHAR_k 'k'
+#define CHAR_l 'l'
+#define CHAR_m 'm'
+#define CHAR_n 'n'
+#define CHAR_o 'o'
+#define CHAR_p 'p'
+#define CHAR_q 'q'
+#define CHAR_r 'r'
+#define CHAR_s 's'
+#define CHAR_t 't'
+#define CHAR_u 'u'
+#define CHAR_v 'v'
+#define CHAR_w 'w'
+#define CHAR_x 'x'
+#define CHAR_y 'y'
+#define CHAR_z 'z'
+#define CHAR_LEFT_CURLY_BRACKET '{'
+#define CHAR_VERTICAL_LINE '|'
+#define CHAR_RIGHT_CURLY_BRACKET '}'
+#define CHAR_TILDE '~'
+
+#define STR_HT "\t"
+#define STR_VT "\v"
+#define STR_FF "\f"
+#define STR_CR "\r"
+#define STR_BS "\b"
+#define STR_BEL "\a"
+
+#define STR_SPACE " "
+#define STR_EXCLAMATION_MARK "!"
+#define STR_QUOTATION_MARK "\""
+#define STR_NUMBER_SIGN "#"
+#define STR_DOLLAR_SIGN "$"
+#define STR_PERCENT_SIGN "%"
+#define STR_AMPERSAND "&"
+#define STR_APOSTROPHE "'"
+#define STR_LEFT_PARENTHESIS "("
+#define STR_RIGHT_PARENTHESIS ")"
+#define STR_ASTERISK "*"
+#define STR_PLUS "+"
+#define STR_COMMA ","
+#define STR_MINUS "-"
+#define STR_DOT "."
+#define STR_SLASH "/"
+#define STR_0 "0"
+#define STR_1 "1"
+#define STR_2 "2"
+#define STR_3 "3"
+#define STR_4 "4"
+#define STR_5 "5"
+#define STR_6 "6"
+#define STR_7 "7"
+#define STR_8 "8"
+#define STR_9 "9"
+#define STR_COLON ":"
+#define STR_SEMICOLON ";"
+#define STR_LESS_THAN_SIGN "<"
+#define STR_EQUALS_SIGN "="
+#define STR_GREATER_THAN_SIGN ">"
+#define STR_QUESTION_MARK "?"
+#define STR_COMMERCIAL_AT "@"
+#define STR_A "A"
+#define STR_B "B"
+#define STR_C "C"
+#define STR_D "D"
+#define STR_E "E"
+#define STR_F "F"
+#define STR_G "G"
+#define STR_H "H"
+#define STR_I "I"
+#define STR_J "J"
+#define STR_K "K"
+#define STR_L "L"
+#define STR_M "M"
+#define STR_N "N"
+#define STR_O "O"
+#define STR_P "P"
+#define STR_Q "Q"
+#define STR_R "R"
+#define STR_S "S"
+#define STR_T "T"
+#define STR_U "U"
+#define STR_V "V"
+#define STR_W "W"
+#define STR_X "X"
+#define STR_Y "Y"
+#define STR_Z "Z"
+#define STR_LEFT_SQUARE_BRACKET "["
+#define STR_BACKSLASH "\\"
+#define STR_RIGHT_SQUARE_BRACKET "]"
+#define STR_CIRCUMFLEX_ACCENT "^"
+#define STR_UNDERSCORE "_"
+#define STR_GRAVE_ACCENT "`"
+#define STR_a "a"
+#define STR_b "b"
+#define STR_c "c"
+#define STR_d "d"
+#define STR_e "e"
+#define STR_f "f"
+#define STR_g "g"
+#define STR_h "h"
+#define STR_i "i"
+#define STR_j "j"
+#define STR_k "k"
+#define STR_l "l"
+#define STR_m "m"
+#define STR_n "n"
+#define STR_o "o"
+#define STR_p "p"
+#define STR_q "q"
+#define STR_r "r"
+#define STR_s "s"
+#define STR_t "t"
+#define STR_u "u"
+#define STR_v "v"
+#define STR_w "w"
+#define STR_x "x"
+#define STR_y "y"
+#define STR_z "z"
+#define STR_LEFT_CURLY_BRACKET "{"
+#define STR_VERTICAL_LINE "|"
+#define STR_RIGHT_CURLY_BRACKET "}"
+#define STR_TILDE "~"
+
+#define STRING_ACCEPT0 "ACCEPT\0"
+#define STRING_COMMIT0 "COMMIT\0"
+#define STRING_F0 "F\0"
+#define STRING_FAIL0 "FAIL\0"
+#define STRING_MARK0 "MARK\0"
+#define STRING_PRUNE0 "PRUNE\0"
+#define STRING_SKIP0 "SKIP\0"
+#define STRING_THEN "THEN"
+
+#define STRING_alpha0 "alpha\0"
+#define STRING_lower0 "lower\0"
+#define STRING_upper0 "upper\0"
+#define STRING_alnum0 "alnum\0"
+#define STRING_ascii0 "ascii\0"
+#define STRING_blank0 "blank\0"
+#define STRING_cntrl0 "cntrl\0"
+#define STRING_digit0 "digit\0"
+#define STRING_graph0 "graph\0"
+#define STRING_print0 "print\0"
+#define STRING_punct0 "punct\0"
+#define STRING_space0 "space\0"
+#define STRING_word0 "word\0"
+#define STRING_xdigit "xdigit"
+
+#define STRING_DEFINE "DEFINE"
+#define STRING_VERSION "VERSION"
+#define STRING_WEIRD_STARTWORD "[:<:]]"
+#define STRING_WEIRD_ENDWORD "[:>:]]"
+
+#define STRING_CR_RIGHTPAR "CR)"
+#define STRING_LF_RIGHTPAR "LF)"
+#define STRING_CRLF_RIGHTPAR "CRLF)"
+#define STRING_ANY_RIGHTPAR "ANY)"
+#define STRING_ANYCRLF_RIGHTPAR "ANYCRLF)"
+#define STRING_NUL_RIGHTPAR "NUL)"
+#define STRING_BSR_ANYCRLF_RIGHTPAR "BSR_ANYCRLF)"
+#define STRING_BSR_UNICODE_RIGHTPAR "BSR_UNICODE)"
+#define STRING_UTF8_RIGHTPAR "UTF8)"
+#define STRING_UTF16_RIGHTPAR "UTF16)"
+#define STRING_UTF32_RIGHTPAR "UTF32)"
+#define STRING_UTF_RIGHTPAR "UTF)"
+#define STRING_UCP_RIGHTPAR "UCP)"
+#define STRING_NO_AUTO_POSSESS_RIGHTPAR "NO_AUTO_POSSESS)"
+#define STRING_NO_DOTSTAR_ANCHOR_RIGHTPAR "NO_DOTSTAR_ANCHOR)"
+#define STRING_NO_JIT_RIGHTPAR "NO_JIT)"
+#define STRING_NO_START_OPT_RIGHTPAR "NO_START_OPT)"
+#define STRING_NOTEMPTY_RIGHTPAR "NOTEMPTY)"
+#define STRING_NOTEMPTY_ATSTART_RIGHTPAR "NOTEMPTY_ATSTART)"
+#define STRING_LIMIT_HEAP_EQ "LIMIT_HEAP="
+#define STRING_LIMIT_MATCH_EQ "LIMIT_MATCH="
+#define STRING_LIMIT_DEPTH_EQ "LIMIT_DEPTH="
+#define STRING_LIMIT_RECURSION_EQ "LIMIT_RECURSION="
+#define STRING_MARK "MARK"
+
+#else /* SUPPORT_UNICODE */
+
+/* UTF-8 support is enabled; always use UTF-8 (=ASCII) character codes. This
+works in both modes non-EBCDIC platforms, and on EBCDIC platforms in UTF-8 mode
+only. */
+
+#define CHAR_HT '\011'
+#define CHAR_VT '\013'
+#define CHAR_FF '\014'
+#define CHAR_CR '\015'
+#define CHAR_LF '\012'
+#define CHAR_NL CHAR_LF
+#define CHAR_NEL ((unsigned char)'\x85')
+#define CHAR_BS '\010'
+#define CHAR_BEL '\007'
+#define CHAR_ESC '\033'
+#define CHAR_DEL '\177'
+
+#define CHAR_NUL '\0'
+#define CHAR_SPACE '\040'
+#define CHAR_EXCLAMATION_MARK '\041'
+#define CHAR_QUOTATION_MARK '\042'
+#define CHAR_NUMBER_SIGN '\043'
+#define CHAR_DOLLAR_SIGN '\044'
+#define CHAR_PERCENT_SIGN '\045'
+#define CHAR_AMPERSAND '\046'
+#define CHAR_APOSTROPHE '\047'
+#define CHAR_LEFT_PARENTHESIS '\050'
+#define CHAR_RIGHT_PARENTHESIS '\051'
+#define CHAR_ASTERISK '\052'
+#define CHAR_PLUS '\053'
+#define CHAR_COMMA '\054'
+#define CHAR_MINUS '\055'
+#define CHAR_DOT '\056'
+#define CHAR_SLASH '\057'
+#define CHAR_0 '\060'
+#define CHAR_1 '\061'
+#define CHAR_2 '\062'
+#define CHAR_3 '\063'
+#define CHAR_4 '\064'
+#define CHAR_5 '\065'
+#define CHAR_6 '\066'
+#define CHAR_7 '\067'
+#define CHAR_8 '\070'
+#define CHAR_9 '\071'
+#define CHAR_COLON '\072'
+#define CHAR_SEMICOLON '\073'
+#define CHAR_LESS_THAN_SIGN '\074'
+#define CHAR_EQUALS_SIGN '\075'
+#define CHAR_GREATER_THAN_SIGN '\076'
+#define CHAR_QUESTION_MARK '\077'
+#define CHAR_COMMERCIAL_AT '\100'
+#define CHAR_A '\101'
+#define CHAR_B '\102'
+#define CHAR_C '\103'
+#define CHAR_D '\104'
+#define CHAR_E '\105'
+#define CHAR_F '\106'
+#define CHAR_G '\107'
+#define CHAR_H '\110'
+#define CHAR_I '\111'
+#define CHAR_J '\112'
+#define CHAR_K '\113'
+#define CHAR_L '\114'
+#define CHAR_M '\115'
+#define CHAR_N '\116'
+#define CHAR_O '\117'
+#define CHAR_P '\120'
+#define CHAR_Q '\121'
+#define CHAR_R '\122'
+#define CHAR_S '\123'
+#define CHAR_T '\124'
+#define CHAR_U '\125'
+#define CHAR_V '\126'
+#define CHAR_W '\127'
+#define CHAR_X '\130'
+#define CHAR_Y '\131'
+#define CHAR_Z '\132'
+#define CHAR_LEFT_SQUARE_BRACKET '\133'
+#define CHAR_BACKSLASH '\134'
+#define CHAR_RIGHT_SQUARE_BRACKET '\135'
+#define CHAR_CIRCUMFLEX_ACCENT '\136'
+#define CHAR_UNDERSCORE '\137'
+#define CHAR_GRAVE_ACCENT '\140'
+#define CHAR_a '\141'
+#define CHAR_b '\142'
+#define CHAR_c '\143'
+#define CHAR_d '\144'
+#define CHAR_e '\145'
+#define CHAR_f '\146'
+#define CHAR_g '\147'
+#define CHAR_h '\150'
+#define CHAR_i '\151'
+#define CHAR_j '\152'
+#define CHAR_k '\153'
+#define CHAR_l '\154'
+#define CHAR_m '\155'
+#define CHAR_n '\156'
+#define CHAR_o '\157'
+#define CHAR_p '\160'
+#define CHAR_q '\161'
+#define CHAR_r '\162'
+#define CHAR_s '\163'
+#define CHAR_t '\164'
+#define CHAR_u '\165'
+#define CHAR_v '\166'
+#define CHAR_w '\167'
+#define CHAR_x '\170'
+#define CHAR_y '\171'
+#define CHAR_z '\172'
+#define CHAR_LEFT_CURLY_BRACKET '\173'
+#define CHAR_VERTICAL_LINE '\174'
+#define CHAR_RIGHT_CURLY_BRACKET '\175'
+#define CHAR_TILDE '\176'
+#define CHAR_NBSP ((unsigned char)'\xa0')
+
+#define STR_HT "\011"
+#define STR_VT "\013"
+#define STR_FF "\014"
+#define STR_CR "\015"
+#define STR_NL "\012"
+#define STR_BS "\010"
+#define STR_BEL "\007"
+#define STR_ESC "\033"
+#define STR_DEL "\177"
+
+#define STR_SPACE "\040"
+#define STR_EXCLAMATION_MARK "\041"
+#define STR_QUOTATION_MARK "\042"
+#define STR_NUMBER_SIGN "\043"
+#define STR_DOLLAR_SIGN "\044"
+#define STR_PERCENT_SIGN "\045"
+#define STR_AMPERSAND "\046"
+#define STR_APOSTROPHE "\047"
+#define STR_LEFT_PARENTHESIS "\050"
+#define STR_RIGHT_PARENTHESIS "\051"
+#define STR_ASTERISK "\052"
+#define STR_PLUS "\053"
+#define STR_COMMA "\054"
+#define STR_MINUS "\055"
+#define STR_DOT "\056"
+#define STR_SLASH "\057"
+#define STR_0 "\060"
+#define STR_1 "\061"
+#define STR_2 "\062"
+#define STR_3 "\063"
+#define STR_4 "\064"
+#define STR_5 "\065"
+#define STR_6 "\066"
+#define STR_7 "\067"
+#define STR_8 "\070"
+#define STR_9 "\071"
+#define STR_COLON "\072"
+#define STR_SEMICOLON "\073"
+#define STR_LESS_THAN_SIGN "\074"
+#define STR_EQUALS_SIGN "\075"
+#define STR_GREATER_THAN_SIGN "\076"
+#define STR_QUESTION_MARK "\077"
+#define STR_COMMERCIAL_AT "\100"
+#define STR_A "\101"
+#define STR_B "\102"
+#define STR_C "\103"
+#define STR_D "\104"
+#define STR_E "\105"
+#define STR_F "\106"
+#define STR_G "\107"
+#define STR_H "\110"
+#define STR_I "\111"
+#define STR_J "\112"
+#define STR_K "\113"
+#define STR_L "\114"
+#define STR_M "\115"
+#define STR_N "\116"
+#define STR_O "\117"
+#define STR_P "\120"
+#define STR_Q "\121"
+#define STR_R "\122"
+#define STR_S "\123"
+#define STR_T "\124"
+#define STR_U "\125"
+#define STR_V "\126"
+#define STR_W "\127"
+#define STR_X "\130"
+#define STR_Y "\131"
+#define STR_Z "\132"
+#define STR_LEFT_SQUARE_BRACKET "\133"
+#define STR_BACKSLASH "\134"
+#define STR_RIGHT_SQUARE_BRACKET "\135"
+#define STR_CIRCUMFLEX_ACCENT "\136"
+#define STR_UNDERSCORE "\137"
+#define STR_GRAVE_ACCENT "\140"
+#define STR_a "\141"
+#define STR_b "\142"
+#define STR_c "\143"
+#define STR_d "\144"
+#define STR_e "\145"
+#define STR_f "\146"
+#define STR_g "\147"
+#define STR_h "\150"
+#define STR_i "\151"
+#define STR_j "\152"
+#define STR_k "\153"
+#define STR_l "\154"
+#define STR_m "\155"
+#define STR_n "\156"
+#define STR_o "\157"
+#define STR_p "\160"
+#define STR_q "\161"
+#define STR_r "\162"
+#define STR_s "\163"
+#define STR_t "\164"
+#define STR_u "\165"
+#define STR_v "\166"
+#define STR_w "\167"
+#define STR_x "\170"
+#define STR_y "\171"
+#define STR_z "\172"
+#define STR_LEFT_CURLY_BRACKET "\173"
+#define STR_VERTICAL_LINE "\174"
+#define STR_RIGHT_CURLY_BRACKET "\175"
+#define STR_TILDE "\176"
+
+#define STRING_ACCEPT0 STR_A STR_C STR_C STR_E STR_P STR_T "\0"
+#define STRING_COMMIT0 STR_C STR_O STR_M STR_M STR_I STR_T "\0"
+#define STRING_F0 STR_F "\0"
+#define STRING_FAIL0 STR_F STR_A STR_I STR_L "\0"
+#define STRING_MARK0 STR_M STR_A STR_R STR_K "\0"
+#define STRING_PRUNE0 STR_P STR_R STR_U STR_N STR_E "\0"
+#define STRING_SKIP0 STR_S STR_K STR_I STR_P "\0"
+#define STRING_THEN STR_T STR_H STR_E STR_N
+
+#define STRING_alpha0 STR_a STR_l STR_p STR_h STR_a "\0"
+#define STRING_lower0 STR_l STR_o STR_w STR_e STR_r "\0"
+#define STRING_upper0 STR_u STR_p STR_p STR_e STR_r "\0"
+#define STRING_alnum0 STR_a STR_l STR_n STR_u STR_m "\0"
+#define STRING_ascii0 STR_a STR_s STR_c STR_i STR_i "\0"
+#define STRING_blank0 STR_b STR_l STR_a STR_n STR_k "\0"
+#define STRING_cntrl0 STR_c STR_n STR_t STR_r STR_l "\0"
+#define STRING_digit0 STR_d STR_i STR_g STR_i STR_t "\0"
+#define STRING_graph0 STR_g STR_r STR_a STR_p STR_h "\0"
+#define STRING_print0 STR_p STR_r STR_i STR_n STR_t "\0"
+#define STRING_punct0 STR_p STR_u STR_n STR_c STR_t "\0"
+#define STRING_space0 STR_s STR_p STR_a STR_c STR_e "\0"
+#define STRING_word0 STR_w STR_o STR_r STR_d "\0"
+#define STRING_xdigit STR_x STR_d STR_i STR_g STR_i STR_t
+
+#define STRING_DEFINE STR_D STR_E STR_F STR_I STR_N STR_E
+#define STRING_VERSION STR_V STR_E STR_R STR_S STR_I STR_O STR_N
+#define STRING_WEIRD_STARTWORD STR_LEFT_SQUARE_BRACKET STR_COLON STR_LESS_THAN_SIGN STR_COLON STR_RIGHT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET
+#define STRING_WEIRD_ENDWORD STR_LEFT_SQUARE_BRACKET STR_COLON STR_GREATER_THAN_SIGN STR_COLON STR_RIGHT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET
+
+#define STRING_CR_RIGHTPAR STR_C STR_R STR_RIGHT_PARENTHESIS
+#define STRING_LF_RIGHTPAR STR_L STR_F STR_RIGHT_PARENTHESIS
+#define STRING_CRLF_RIGHTPAR STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS
+#define STRING_ANY_RIGHTPAR STR_A STR_N STR_Y STR_RIGHT_PARENTHESIS
+#define STRING_ANYCRLF_RIGHTPAR STR_A STR_N STR_Y STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS
+#define STRING_NUL_RIGHTPAR STR_N STR_U STR_L STR_RIGHT_PARENTHESIS
+#define STRING_BSR_ANYCRLF_RIGHTPAR STR_B STR_S STR_R STR_UNDERSCORE STR_A STR_N STR_Y STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS
+#define STRING_BSR_UNICODE_RIGHTPAR STR_B STR_S STR_R STR_UNDERSCORE STR_U STR_N STR_I STR_C STR_O STR_D STR_E STR_RIGHT_PARENTHESIS
+#define STRING_UTF8_RIGHTPAR STR_U STR_T STR_F STR_8 STR_RIGHT_PARENTHESIS
+#define STRING_UTF16_RIGHTPAR STR_U STR_T STR_F STR_1 STR_6 STR_RIGHT_PARENTHESIS
+#define STRING_UTF32_RIGHTPAR STR_U STR_T STR_F STR_3 STR_2 STR_RIGHT_PARENTHESIS
+#define STRING_UTF_RIGHTPAR STR_U STR_T STR_F STR_RIGHT_PARENTHESIS
+#define STRING_UCP_RIGHTPAR STR_U STR_C STR_P STR_RIGHT_PARENTHESIS
+#define STRING_NO_AUTO_POSSESS_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_A STR_U STR_T STR_O STR_UNDERSCORE STR_P STR_O STR_S STR_S STR_E STR_S STR_S STR_RIGHT_PARENTHESIS
+#define STRING_NO_DOTSTAR_ANCHOR_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_D STR_O STR_T STR_S STR_T STR_A STR_R STR_UNDERSCORE STR_A STR_N STR_C STR_H STR_O STR_R STR_RIGHT_PARENTHESIS
+#define STRING_NO_JIT_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_J STR_I STR_T STR_RIGHT_PARENTHESIS
+#define STRING_NO_START_OPT_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_S STR_T STR_A STR_R STR_T STR_UNDERSCORE STR_O STR_P STR_T STR_RIGHT_PARENTHESIS
+#define STRING_NOTEMPTY_RIGHTPAR STR_N STR_O STR_T STR_E STR_M STR_P STR_T STR_Y STR_RIGHT_PARENTHESIS
+#define STRING_NOTEMPTY_ATSTART_RIGHTPAR STR_N STR_O STR_T STR_E STR_M STR_P STR_T STR_Y STR_UNDERSCORE STR_A STR_T STR_S STR_T STR_A STR_R STR_T STR_RIGHT_PARENTHESIS
+#define STRING_LIMIT_HEAP_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_H STR_E STR_A STR_P STR_EQUALS_SIGN
+#define STRING_LIMIT_MATCH_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_M STR_A STR_T STR_C STR_H STR_EQUALS_SIGN
+#define STRING_LIMIT_DEPTH_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_D STR_E STR_P STR_T STR_H STR_EQUALS_SIGN
+#define STRING_LIMIT_RECURSION_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_R STR_E STR_C STR_U STR_R STR_S STR_I STR_O STR_N STR_EQUALS_SIGN
+#define STRING_MARK STR_M STR_A STR_R STR_K
+
+#endif /* SUPPORT_UNICODE */
+
+/* -------------------- End of character and string names -------------------*/
+
+/* -------------------- Definitions for compiled patterns -------------------*/
+
+/* Codes for different types of Unicode property */
+
+#define PT_ANY 0 /* Any property - matches all chars */
+#define PT_LAMP 1 /* L& - the union of Lu, Ll, Lt */
+#define PT_GC 2 /* Specified general characteristic (e.g. L) */
+#define PT_PC 3 /* Specified particular characteristic (e.g. Lu) */
+#define PT_SC 4 /* Script (e.g. Han) */
+#define PT_ALNUM 5 /* Alphanumeric - the union of L and N */
+#define PT_SPACE 6 /* Perl space - Z plus 9,10,12,13 */
+#define PT_PXSPACE 7 /* POSIX space - Z plus 9,10,11,12,13 */
+#define PT_WORD 8 /* Word - L plus N plus underscore */
+#define PT_CLIST 9 /* Pseudo-property: match character list */
+#define PT_UCNC 10 /* Universal Character nameable character */
+#define PT_TABSIZE 11 /* Size of square table for autopossessify tests */
+
+/* The following special properties are used only in XCLASS items, when POSIX
+classes are specified and PCRE2_UCP is set - in other words, for Unicode
+handling of these classes. They are not available via the \p or \P escapes like
+those in the above list, and so they do not take part in the autopossessifying
+table. */
+
+#define PT_PXGRAPH 11 /* [:graph:] - characters that mark the paper */
+#define PT_PXPRINT 12 /* [:print:] - [:graph:] plus non-control spaces */
+#define PT_PXPUNCT 13 /* [:punct:] - punctuation characters */
+
+/* Flag bits and data types for the extended class (OP_XCLASS) for classes that
+contain characters with values greater than 255. */
+
+#define XCL_NOT 0x01 /* Flag: this is a negative class */
+#define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
+#define XCL_HASPROP 0x04 /* Flag: property checks are present. */
+
+#define XCL_END 0 /* Marks end of individual items */
+#define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
+#define XCL_RANGE 2 /* A range (two multibyte chars) follows */
+#define XCL_PROP 3 /* Unicode property (2-byte property code follows) */
+#define XCL_NOTPROP 4 /* Unicode inverted property (ditto) */
+
+/* These are escaped items that aren't just an encoding of a particular data
+value such as \n. They must have non-zero values, as check_escape() returns 0
+for a data character. In the escapes[] table in pcre2_compile.c their values
+are negated in order to distinguish them from data values.
+
+They must appear here in the same order as in the opcode definitions below, up
+to ESC_z. There's a dummy for OP_ALLANY because it corresponds to "." in DOTALL
+mode rather than an escape sequence. It is also used for [^] in JavaScript
+compatibility mode, and for \C in non-utf mode. In non-DOTALL mode, "." behaves
+like \N.
+
+Negative numbers are used to encode a backreference (\1, \2, \3, etc.) in
+check_escape(). There are tests in the code for an escape greater than ESC_b
+and less than ESC_Z to detect the types that may be repeated. These are the
+types that consume characters. If any new escapes are put in between that don't
+consume a character, that code will have to change. */
+
+enum { ESC_A = 1, ESC_G, ESC_K, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s,
+ ESC_W, ESC_w, ESC_N, ESC_dum, ESC_C, ESC_P, ESC_p, ESC_R, ESC_H,
+ ESC_h, ESC_V, ESC_v, ESC_X, ESC_Z, ESC_z,
+ ESC_E, ESC_Q, ESC_g, ESC_k };
+
+
+/********************** Opcode definitions ******************/
+
+/****** NOTE NOTE NOTE ******
+
+Starting from 1 (i.e. after OP_END), the values up to OP_EOD must correspond in
+order to the list of escapes immediately above. Furthermore, values up to
+OP_DOLLM must not be changed without adjusting the table called autoposstab in
+pcre2_auto_possess.c
+
+Whenever this list is updated, the two macro definitions that follow must be
+updated to match. The possessification table called "opcode_possessify" in
+pcre2_compile.c must also be updated, and also the tables called "coptable"
+and "poptable" in pcre2_dfa_match.c.
+
+****** NOTE NOTE NOTE ******/
+
+
+/* The values between FIRST_AUTOTAB_OP and LAST_AUTOTAB_RIGHT_OP, inclusive,
+are used in a table for deciding whether a repeated character type can be
+auto-possessified. */
+
+#define FIRST_AUTOTAB_OP OP_NOT_DIGIT
+#define LAST_AUTOTAB_LEFT_OP OP_EXTUNI
+#define LAST_AUTOTAB_RIGHT_OP OP_DOLLM
+
+enum {
+ OP_END, /* 0 End of pattern */
+
+ /* Values corresponding to backslashed metacharacters */
+
+ OP_SOD, /* 1 Start of data: \A */
+ OP_SOM, /* 2 Start of match (subject + offset): \G */
+ OP_SET_SOM, /* 3 Set start of match (\K) */
+ OP_NOT_WORD_BOUNDARY, /* 4 \B */
+ OP_WORD_BOUNDARY, /* 5 \b */
+ OP_NOT_DIGIT, /* 6 \D */
+ OP_DIGIT, /* 7 \d */
+ OP_NOT_WHITESPACE, /* 8 \S */
+ OP_WHITESPACE, /* 9 \s */
+ OP_NOT_WORDCHAR, /* 10 \W */
+ OP_WORDCHAR, /* 11 \w */
+
+ OP_ANY, /* 12 Match any character except newline (\N) */
+ OP_ALLANY, /* 13 Match any character */
+ OP_ANYBYTE, /* 14 Match any byte (\C); different to OP_ANY for UTF-8 */
+ OP_NOTPROP, /* 15 \P (not Unicode property) */
+ OP_PROP, /* 16 \p (Unicode property) */
+ OP_ANYNL, /* 17 \R (any newline sequence) */
+ OP_NOT_HSPACE, /* 18 \H (not horizontal whitespace) */
+ OP_HSPACE, /* 19 \h (horizontal whitespace) */
+ OP_NOT_VSPACE, /* 20 \V (not vertical whitespace) */
+ OP_VSPACE, /* 21 \v (vertical whitespace) */
+ OP_EXTUNI, /* 22 \X (extended Unicode sequence */
+ OP_EODN, /* 23 End of data or \n at end of data (\Z) */
+ OP_EOD, /* 24 End of data (\z) */
+
+ /* Line end assertions */
+
+ OP_DOLL, /* 25 End of line - not multiline */
+ OP_DOLLM, /* 26 End of line - multiline */
+ OP_CIRC, /* 27 Start of line - not multiline */
+ OP_CIRCM, /* 28 Start of line - multiline */
+
+ /* Single characters; caseful must precede the caseless ones, and these
+ must remain in this order, and adjacent. */
+
+ OP_CHAR, /* 29 Match one character, casefully */
+ OP_CHARI, /* 30 Match one character, caselessly */
+ OP_NOT, /* 31 Match one character, not the given one, casefully */
+ OP_NOTI, /* 32 Match one character, not the given one, caselessly */
+
+ /* The following sets of 13 opcodes must always be kept in step because
+ the offset from the first one is used to generate the others. */
+
+ /* Repeated characters; caseful must precede the caseless ones */
+
+ OP_STAR, /* 33 The maximizing and minimizing versions of */
+ OP_MINSTAR, /* 34 these six opcodes must come in pairs, with */
+ OP_PLUS, /* 35 the minimizing one second. */
+ OP_MINPLUS, /* 36 */
+ OP_QUERY, /* 37 */
+ OP_MINQUERY, /* 38 */
+
+ OP_UPTO, /* 39 From 0 to n matches of one character, caseful*/
+ OP_MINUPTO, /* 40 */
+ OP_EXACT, /* 41 Exactly n matches */
+
+ OP_POSSTAR, /* 42 Possessified star, caseful */
+ OP_POSPLUS, /* 43 Possessified plus, caseful */
+ OP_POSQUERY, /* 44 Posesssified query, caseful */
+ OP_POSUPTO, /* 45 Possessified upto, caseful */
+
+ /* Repeated characters; caseless must follow the caseful ones */
+
+ OP_STARI, /* 46 */
+ OP_MINSTARI, /* 47 */
+ OP_PLUSI, /* 48 */
+ OP_MINPLUSI, /* 49 */
+ OP_QUERYI, /* 50 */
+ OP_MINQUERYI, /* 51 */
+
+ OP_UPTOI, /* 52 From 0 to n matches of one character, caseless */
+ OP_MINUPTOI, /* 53 */
+ OP_EXACTI, /* 54 */
+
+ OP_POSSTARI, /* 55 Possessified star, caseless */
+ OP_POSPLUSI, /* 56 Possessified plus, caseless */
+ OP_POSQUERYI, /* 57 Posesssified query, caseless */
+ OP_POSUPTOI, /* 58 Possessified upto, caseless */
+
+ /* The negated ones must follow the non-negated ones, and match them */
+ /* Negated repeated character, caseful; must precede the caseless ones */
+
+ OP_NOTSTAR, /* 59 The maximizing and minimizing versions of */
+ OP_NOTMINSTAR, /* 60 these six opcodes must come in pairs, with */
+ OP_NOTPLUS, /* 61 the minimizing one second. They must be in */
+ OP_NOTMINPLUS, /* 62 exactly the same order as those above. */
+ OP_NOTQUERY, /* 63 */
+ OP_NOTMINQUERY, /* 64 */
+
+ OP_NOTUPTO, /* 65 From 0 to n matches, caseful */
+ OP_NOTMINUPTO, /* 66 */
+ OP_NOTEXACT, /* 67 Exactly n matches */
+
+ OP_NOTPOSSTAR, /* 68 Possessified versions, caseful */
+ OP_NOTPOSPLUS, /* 69 */
+ OP_NOTPOSQUERY, /* 70 */
+ OP_NOTPOSUPTO, /* 71 */
+
+ /* Negated repeated character, caseless; must follow the caseful ones */
+
+ OP_NOTSTARI, /* 72 */
+ OP_NOTMINSTARI, /* 73 */
+ OP_NOTPLUSI, /* 74 */
+ OP_NOTMINPLUSI, /* 75 */
+ OP_NOTQUERYI, /* 76 */
+ OP_NOTMINQUERYI, /* 77 */
+
+ OP_NOTUPTOI, /* 78 From 0 to n matches, caseless */
+ OP_NOTMINUPTOI, /* 79 */
+ OP_NOTEXACTI, /* 80 Exactly n matches */
+
+ OP_NOTPOSSTARI, /* 81 Possessified versions, caseless */
+ OP_NOTPOSPLUSI, /* 82 */
+ OP_NOTPOSQUERYI, /* 83 */
+ OP_NOTPOSUPTOI, /* 84 */
+
+ /* Character types */
+
+ OP_TYPESTAR, /* 85 The maximizing and minimizing versions of */
+ OP_TYPEMINSTAR, /* 86 these six opcodes must come in pairs, with */
+ OP_TYPEPLUS, /* 87 the minimizing one second. These codes must */
+ OP_TYPEMINPLUS, /* 88 be in exactly the same order as those above. */
+ OP_TYPEQUERY, /* 89 */
+ OP_TYPEMINQUERY, /* 90 */
+
+ OP_TYPEUPTO, /* 91 From 0 to n matches */
+ OP_TYPEMINUPTO, /* 92 */
+ OP_TYPEEXACT, /* 93 Exactly n matches */
+
+ OP_TYPEPOSSTAR, /* 94 Possessified versions */
+ OP_TYPEPOSPLUS, /* 95 */
+ OP_TYPEPOSQUERY, /* 96 */
+ OP_TYPEPOSUPTO, /* 97 */
+
+ /* These are used for character classes and back references; only the
+ first six are the same as the sets above. */
+
+ OP_CRSTAR, /* 98 The maximizing and minimizing versions of */
+ OP_CRMINSTAR, /* 99 all these opcodes must come in pairs, with */
+ OP_CRPLUS, /* 100 the minimizing one second. These codes must */
+ OP_CRMINPLUS, /* 101 be in exactly the same order as those above. */
+ OP_CRQUERY, /* 102 */
+ OP_CRMINQUERY, /* 103 */
+
+ OP_CRRANGE, /* 104 These are different to the three sets above. */
+ OP_CRMINRANGE, /* 105 */
+
+ OP_CRPOSSTAR, /* 106 Possessified versions */
+ OP_CRPOSPLUS, /* 107 */
+ OP_CRPOSQUERY, /* 108 */
+ OP_CRPOSRANGE, /* 109 */
+
+ /* End of quantifier opcodes */
+
+ OP_CLASS, /* 110 Match a character class, chars < 256 only */
+ OP_NCLASS, /* 111 Same, but the bitmap was created from a negative
+ class - the difference is relevant only when a
+ character > 255 is encountered. */
+ OP_XCLASS, /* 112 Extended class for handling > 255 chars within the
+ class. This does both positive and negative. */
+ OP_REF, /* 113 Match a back reference, casefully */
+ OP_REFI, /* 114 Match a back reference, caselessly */
+ OP_DNREF, /* 115 Match a duplicate name backref, casefully */
+ OP_DNREFI, /* 116 Match a duplicate name backref, caselessly */
+ OP_RECURSE, /* 117 Match a numbered subpattern (possibly recursive) */
+ OP_CALLOUT, /* 118 Call out to external function if provided */
+ OP_CALLOUT_STR, /* 119 Call out with string argument */
+
+ OP_ALT, /* 120 Start of alternation */
+ OP_KET, /* 121 End of group that doesn't have an unbounded repeat */
+ OP_KETRMAX, /* 122 These two must remain together and in this */
+ OP_KETRMIN, /* 123 order. They are for groups the repeat for ever. */
+ OP_KETRPOS, /* 124 Possessive unlimited repeat. */
+
+ /* The assertions must come before BRA, CBRA, ONCE, and COND, and the four
+ asserts must remain in order. */
+
+ OP_REVERSE, /* 125 Move pointer back - used in lookbehind assertions */
+ OP_ASSERT, /* 126 Positive lookahead */
+ OP_ASSERT_NOT, /* 127 Negative lookahead */
+ OP_ASSERTBACK, /* 128 Positive lookbehind */
+ OP_ASSERTBACK_NOT, /* 129 Negative lookbehind */
+
+ /* ONCE, BRA, BRAPOS, CBRA, CBRAPOS, and COND must come immediately after the
+ assertions, with ONCE first, as there's a test for >= ONCE for a subpattern
+ that isn't an assertion. The POS versions must immediately follow the non-POS
+ versions in each case. */
+
+ OP_ONCE, /* 130 Atomic group, contains captures */
+ OP_BRA, /* 131 Start of non-capturing bracket */
+ OP_BRAPOS, /* 132 Ditto, with unlimited, possessive repeat */
+ OP_CBRA, /* 133 Start of capturing bracket */
+ OP_CBRAPOS, /* 134 Ditto, with unlimited, possessive repeat */
+ OP_COND, /* 135 Conditional group */
+
+ /* These five must follow the previous five, in the same order. There's a
+ check for >= SBRA to distinguish the two sets. */
+
+ OP_SBRA, /* 136 Start of non-capturing bracket, check empty */
+ OP_SBRAPOS, /* 137 Ditto, with unlimited, possessive repeat */
+ OP_SCBRA, /* 138 Start of capturing bracket, check empty */
+ OP_SCBRAPOS, /* 139 Ditto, with unlimited, possessive repeat */
+ OP_SCOND, /* 140 Conditional group, check empty */
+
+ /* The next two pairs must (respectively) be kept together. */
+
+ OP_CREF, /* 141 Used to hold a capture number as condition */
+ OP_DNCREF, /* 142 Used to point to duplicate names as a condition */
+ OP_RREF, /* 143 Used to hold a recursion number as condition */
+ OP_DNRREF, /* 144 Used to point to duplicate names as a condition */
+ OP_FALSE, /* 145 Always false (used by DEFINE and VERSION) */
+ OP_TRUE, /* 146 Always true (used by VERSION) */
+
+ OP_BRAZERO, /* 147 These two must remain together and in this */
+ OP_BRAMINZERO, /* 148 order. */
+ OP_BRAPOSZERO, /* 149 */
+
+ /* These are backtracking control verbs */
+
+ OP_MARK, /* 150 always has an argument */
+ OP_PRUNE, /* 151 */
+ OP_PRUNE_ARG, /* 152 same, but with argument */
+ OP_SKIP, /* 153 */
+ OP_SKIP_ARG, /* 154 same, but with argument */
+ OP_THEN, /* 155 */
+ OP_THEN_ARG, /* 156 same, but with argument */
+ OP_COMMIT, /* 157 */
+ OP_COMMIT_ARG, /* 158 same, but with argument */
+
+ /* These are forced failure and success verbs. FAIL and ACCEPT do accept an
+ argument, but these cases can be compiled as, for example, (*MARK:X)(*FAIL)
+ without the need for a special opcode. */
+
+ OP_FAIL, /* 159 */
+ OP_ACCEPT, /* 160 */
+ OP_ASSERT_ACCEPT, /* 161 Used inside assertions */
+ OP_CLOSE, /* 162 Used before OP_ACCEPT to close open captures */
+
+ /* This is used to skip a subpattern with a {0} quantifier */
+
+ OP_SKIPZERO, /* 163 */
+
+ /* This is used to identify a DEFINE group during compilation so that it can
+ be checked for having only one branch. It is changed to OP_FALSE before
+ compilation finishes. */
+
+ OP_DEFINE, /* 164 */
+
+ /* This is not an opcode, but is used to check that tables indexed by opcode
+ are the correct length, in order to catch updating errors - there have been
+ some in the past. */
+
+ OP_TABLE_LENGTH
+
+};
+
+/* *** NOTE NOTE NOTE *** Whenever the list above is updated, the two macro
+definitions that follow must also be updated to match. There are also tables
+called "opcode_possessify" in pcre2_compile.c and "coptable" and "poptable" in
+pcre2_dfa_exec.c that must be updated. */
+
+
+/* This macro defines textual names for all the opcodes. These are used only
+for debugging, and some of them are only partial names. The macro is referenced
+only in pcre2_printint.c, which fills out the full names in many cases (and in
+some cases doesn't actually use these names at all). */
+
+#define OP_NAME_LIST \
+ "End", "\\A", "\\G", "\\K", "\\B", "\\b", "\\D", "\\d", \
+ "\\S", "\\s", "\\W", "\\w", "Any", "AllAny", "Anybyte", \
+ "notprop", "prop", "\\R", "\\H", "\\h", "\\V", "\\v", \
+ "extuni", "\\Z", "\\z", \
+ "$", "$", "^", "^", "char", "chari", "not", "noti", \
+ "*", "*?", "+", "+?", "?", "??", \
+ "{", "{", "{", \
+ "*+","++", "?+", "{", \
+ "*", "*?", "+", "+?", "?", "??", \
+ "{", "{", "{", \
+ "*+","++", "?+", "{", \
+ "*", "*?", "+", "+?", "?", "??", \
+ "{", "{", "{", \
+ "*+","++", "?+", "{", \
+ "*", "*?", "+", "+?", "?", "??", \
+ "{", "{", "{", \
+ "*+","++", "?+", "{", \
+ "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \
+ "*+","++", "?+", "{", \
+ "*", "*?", "+", "+?", "?", "??", "{", "{", \
+ "*+","++", "?+", "{", \
+ "class", "nclass", "xclass", "Ref", "Refi", "DnRef", "DnRefi", \
+ "Recurse", "Callout", "CalloutStr", \
+ "Alt", "Ket", "KetRmax", "KetRmin", "KetRpos", \
+ "Reverse", "Assert", "Assert not", "AssertB", "AssertB not", \
+ "Once", \
+ "Bra", "BraPos", "CBra", "CBraPos", \
+ "Cond", \
+ "SBra", "SBraPos", "SCBra", "SCBraPos", \
+ "SCond", \
+ "Cond ref", "Cond dnref", "Cond rec", "Cond dnrec", \
+ "Cond false", "Cond true", \
+ "Brazero", "Braminzero", "Braposzero", \
+ "*MARK", "*PRUNE", "*PRUNE", "*SKIP", "*SKIP", \
+ "*THEN", "*THEN", "*COMMIT", "*COMMIT", "*FAIL", \
+ "*ACCEPT", "*ASSERT_ACCEPT", \
+ "Close", "Skip zero", "Define"
+
+
+/* This macro defines the length of fixed length operations in the compiled
+regex. The lengths are used when searching for specific things, and also in the
+debugging printing of a compiled regex. We use a macro so that it can be
+defined close to the definitions of the opcodes themselves.
+
+As things have been extended, some of these are no longer fixed lenths, but are
+minima instead. For example, the length of a single-character repeat may vary
+in UTF-8 mode. The code that uses this table must know about such things. */
+
+#define OP_LENGTHS \
+ 1, /* End */ \
+ 1, 1, 1, 1, 1, /* \A, \G, \K, \B, \b */ \
+ 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */ \
+ 1, 1, 1, /* Any, AllAny, Anybyte */ \
+ 3, 3, /* \P, \p */ \
+ 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */ \
+ 1, /* \X */ \
+ 1, 1, 1, 1, 1, 1, /* \Z, \z, $, $M ^, ^M */ \
+ 2, /* Char - the minimum length */ \
+ 2, /* Chari - the minimum length */ \
+ 2, /* not */ \
+ 2, /* noti */ \
+ /* Positive single-char repeats ** These are */ \
+ 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \
+ 2+IMM2_SIZE, 2+IMM2_SIZE, /* upto, minupto ** mode */ \
+ 2+IMM2_SIZE, /* exact */ \
+ 2, 2, 2, 2+IMM2_SIZE, /* *+, ++, ?+, upto+ */ \
+ 2, 2, 2, 2, 2, 2, /* *I, *?I, +I, +?I, ?I, ??I ** UTF-8 */ \
+ 2+IMM2_SIZE, 2+IMM2_SIZE, /* upto I, minupto I */ \
+ 2+IMM2_SIZE, /* exact I */ \
+ 2, 2, 2, 2+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */ \
+ /* Negative single-char repeats - only for chars < 256 */ \
+ 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \
+ 2+IMM2_SIZE, 2+IMM2_SIZE, /* NOT upto, minupto */ \
+ 2+IMM2_SIZE, /* NOT exact */ \
+ 2, 2, 2, 2+IMM2_SIZE, /* Possessive NOT *, +, ?, upto */ \
+ 2, 2, 2, 2, 2, 2, /* NOT *I, *?I, +I, +?I, ?I, ??I */ \
+ 2+IMM2_SIZE, 2+IMM2_SIZE, /* NOT upto I, minupto I */ \
+ 2+IMM2_SIZE, /* NOT exact I */ \
+ 2, 2, 2, 2+IMM2_SIZE, /* Possessive NOT *I, +I, ?I, upto I */ \
+ /* Positive type repeats */ \
+ 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \
+ 2+IMM2_SIZE, 2+IMM2_SIZE, /* Type upto, minupto */ \
+ 2+IMM2_SIZE, /* Type exact */ \
+ 2, 2, 2, 2+IMM2_SIZE, /* Possessive *+, ++, ?+, upto+ */ \
+ /* Character class & ref repeats */ \
+ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \
+ 1+2*IMM2_SIZE, 1+2*IMM2_SIZE, /* CRRANGE, CRMINRANGE */ \
+ 1, 1, 1, 1+2*IMM2_SIZE, /* Possessive *+, ++, ?+, CRPOSRANGE */ \
+ 1+(32/sizeof(PCRE2_UCHAR)), /* CLASS */ \
+ 1+(32/sizeof(PCRE2_UCHAR)), /* NCLASS */ \
+ 0, /* XCLASS - variable length */ \
+ 1+IMM2_SIZE, /* REF */ \
+ 1+IMM2_SIZE, /* REFI */ \
+ 1+2*IMM2_SIZE, /* DNREF */ \
+ 1+2*IMM2_SIZE, /* DNREFI */ \
+ 1+LINK_SIZE, /* RECURSE */ \
+ 1+2*LINK_SIZE+1, /* CALLOUT */ \
+ 0, /* CALLOUT_STR - variable length */ \
+ 1+LINK_SIZE, /* Alt */ \
+ 1+LINK_SIZE, /* Ket */ \
+ 1+LINK_SIZE, /* KetRmax */ \
+ 1+LINK_SIZE, /* KetRmin */ \
+ 1+LINK_SIZE, /* KetRpos */ \
+ 1+LINK_SIZE, /* Reverse */ \
+ 1+LINK_SIZE, /* Assert */ \
+ 1+LINK_SIZE, /* Assert not */ \
+ 1+LINK_SIZE, /* Assert behind */ \
+ 1+LINK_SIZE, /* Assert behind not */ \
+ 1+LINK_SIZE, /* ONCE */ \
+ 1+LINK_SIZE, /* BRA */ \
+ 1+LINK_SIZE, /* BRAPOS */ \
+ 1+LINK_SIZE+IMM2_SIZE, /* CBRA */ \
+ 1+LINK_SIZE+IMM2_SIZE, /* CBRAPOS */ \
+ 1+LINK_SIZE, /* COND */ \
+ 1+LINK_SIZE, /* SBRA */ \
+ 1+LINK_SIZE, /* SBRAPOS */ \
+ 1+LINK_SIZE+IMM2_SIZE, /* SCBRA */ \
+ 1+LINK_SIZE+IMM2_SIZE, /* SCBRAPOS */ \
+ 1+LINK_SIZE, /* SCOND */ \
+ 1+IMM2_SIZE, 1+2*IMM2_SIZE, /* CREF, DNCREF */ \
+ 1+IMM2_SIZE, 1+2*IMM2_SIZE, /* RREF, DNRREF */ \
+ 1, 1, /* FALSE, TRUE */ \
+ 1, 1, 1, /* BRAZERO, BRAMINZERO, BRAPOSZERO */ \
+ 3, 1, 3, /* MARK, PRUNE, PRUNE_ARG */ \
+ 1, 3, /* SKIP, SKIP_ARG */ \
+ 1, 3, /* THEN, THEN_ARG */ \
+ 1, 3, /* COMMIT, COMMIT_ARG */ \
+ 1, 1, 1, /* FAIL, ACCEPT, ASSERT_ACCEPT */ \
+ 1+IMM2_SIZE, 1, /* CLOSE, SKIPZERO */ \
+ 1 /* DEFINE */
+
+/* A magic value for OP_RREF to indicate the "any recursion" condition. */
+
+#define RREF_ANY 0xffff
+
+
+/* ---------- Private structures that are mode-independent. ---------- */
+
+/* Structure to hold data for custom memory management. */
+
+typedef struct pcre2_memctl {
+ void * (*malloc)(size_t, void *);
+ void (*free)(void *, void *);
+ void *memory_data;
+} pcre2_memctl;
+
+/* Structure for building a chain of open capturing subpatterns during
+compiling, so that instructions to close them can be compiled when (*ACCEPT) is
+encountered. This is also used to identify subpatterns that contain recursive
+back references to themselves, so that they can be made atomic. */
+
+typedef struct open_capitem {
+ struct open_capitem *next; /* Chain link */
+ uint16_t number; /* Capture number */
+ uint16_t flag; /* Set TRUE if recursive back ref */
+ uint16_t assert_depth; /* Assertion depth when opened */
+} open_capitem;
+
+/* Layout of the UCP type table that translates property names into types and
+codes. Each entry used to point directly to a name, but to reduce the number of
+relocations in shared libraries, it now has an offset into a single string
+instead. */
+
+typedef struct {
+ uint16_t name_offset;
+ uint16_t type;
+ uint16_t value;
+} ucp_type_table;
+
+/* Unicode character database (UCD) record format */
+
+typedef struct {
+ uint8_t script; /* ucp_Arabic, etc. */
+ uint8_t chartype; /* ucp_Cc, etc. (general categories) */
+ uint8_t gbprop; /* ucp_gbControl, etc. (grapheme break property) */
+ uint8_t caseset; /* offset to multichar other cases or zero */
+ int32_t other_case; /* offset to other case, or zero if none */
+} ucd_record;
+
+/* UCD access macros */
+
+#define UCD_BLOCK_SIZE 128
+#define REAL_GET_UCD(ch) (PRIV(ucd_records) + \
+ PRIV(ucd_stage2)[PRIV(ucd_stage1)[(int)(ch) / UCD_BLOCK_SIZE] * \
+ UCD_BLOCK_SIZE + (int)(ch) % UCD_BLOCK_SIZE])
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+#define GET_UCD(ch) ((ch > MAX_UTF_CODE_POINT)? \
+ PRIV(dummy_ucd_record) : REAL_GET_UCD(ch))
+#else
+#define GET_UCD(ch) REAL_GET_UCD(ch)
+#endif
+
+#define UCD_CHARTYPE(ch) GET_UCD(ch)->chartype
+#define UCD_SCRIPT(ch) GET_UCD(ch)->script
+#define UCD_CATEGORY(ch) PRIV(ucp_gentype)[UCD_CHARTYPE(ch)]
+#define UCD_GRAPHBREAK(ch) GET_UCD(ch)->gbprop
+#define UCD_CASESET(ch) GET_UCD(ch)->caseset
+#define UCD_OTHERCASE(ch) ((uint32_t)((int)ch + (int)(GET_UCD(ch)->other_case)))
+
+/* Header for serialized pcre2 codes. */
+
+typedef struct pcre2_serialized_data {
+ uint32_t magic;
+ uint32_t version;
+ uint32_t config;
+ int32_t number_of_codes;
+} pcre2_serialized_data;
+
+
+
+/* ----------------- Items that need PCRE2_CODE_UNIT_WIDTH ----------------- */
+
+/* When this file is included by pcre2test, PCRE2_CODE_UNIT_WIDTH is defined as
+0, so the following items are omitted. */
+
+#if defined PCRE2_CODE_UNIT_WIDTH && PCRE2_CODE_UNIT_WIDTH != 0
+
+/* EBCDIC is supported only for the 8-bit library. */
+
+#if defined EBCDIC && PCRE2_CODE_UNIT_WIDTH != 8
+#error EBCDIC is not supported for the 16-bit or 32-bit libraries
+#endif
+
+/* This is the largest non-UTF code point. */
+
+#define MAX_NON_UTF_CHAR (0xffffffffU >> (32 - PCRE2_CODE_UNIT_WIDTH))
+
+/* Internal shared data tables and variables. These are used by more than one
+of the exported public functions. They have to be "external" in the C sense,
+but are not part of the PCRE2 public API. Although the data for some of them is
+identical in all libraries, they must have different names so that multiple
+libraries can be simultaneously linked to a single application. However, UTF-8
+tables are needed only when compiling the 8-bit library. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+extern const int PRIV(utf8_table1)[];
+extern const int PRIV(utf8_table1_size);
+extern const int PRIV(utf8_table2)[];
+extern const int PRIV(utf8_table3)[];
+extern const uint8_t PRIV(utf8_table4)[];
+#endif
+
+#define _pcre2_OP_lengths PCRE2_SUFFIX(_pcre2_OP_lengths_)
+#define _pcre2_callout_end_delims PCRE2_SUFFIX(_pcre2_callout_end_delims_)
+#define _pcre2_callout_start_delims PCRE2_SUFFIX(_pcre2_callout_start_delims_)
+#define _pcre2_default_compile_context PCRE2_SUFFIX(_pcre2_default_compile_context_)
+#define _pcre2_default_convert_context PCRE2_SUFFIX(_pcre2_default_convert_context_)
+#define _pcre2_default_match_context PCRE2_SUFFIX(_pcre2_default_match_context_)
+#define _pcre2_default_tables PCRE2_SUFFIX(_pcre2_default_tables_)
+#if PCRE2_CODE_UNIT_WIDTH == 32
+#define _pcre2_dummy_ucd_record PCRE2_SUFFIX(_pcre2_dummy_ucd_record_)
+#endif
+#define _pcre2_hspace_list PCRE2_SUFFIX(_pcre2_hspace_list_)
+#define _pcre2_vspace_list PCRE2_SUFFIX(_pcre2_vspace_list_)
+#define _pcre2_ucd_caseless_sets PCRE2_SUFFIX(_pcre2_ucd_caseless_sets_)
+#define _pcre2_ucd_records PCRE2_SUFFIX(_pcre2_ucd_records_)
+#define _pcre2_ucd_stage1 PCRE2_SUFFIX(_pcre2_ucd_stage1_)
+#define _pcre2_ucd_stage2 PCRE2_SUFFIX(_pcre2_ucd_stage2_)
+#define _pcre2_ucp_gbtable PCRE2_SUFFIX(_pcre2_ucp_gbtable_)
+#define _pcre2_ucp_gentype PCRE2_SUFFIX(_pcre2_ucp_gentype_)
+#define _pcre2_ucp_typerange PCRE2_SUFFIX(_pcre2_ucp_typerange_)
+#define _pcre2_unicode_version PCRE2_SUFFIX(_pcre2_unicode_version_)
+#define _pcre2_utt PCRE2_SUFFIX(_pcre2_utt_)
+#define _pcre2_utt_names PCRE2_SUFFIX(_pcre2_utt_names_)
+#define _pcre2_utt_size PCRE2_SUFFIX(_pcre2_utt_size_)
+
+extern const uint8_t PRIV(OP_lengths)[];
+extern const uint32_t PRIV(callout_end_delims)[];
+extern const uint32_t PRIV(callout_start_delims)[];
+extern const pcre2_compile_context PRIV(default_compile_context);
+extern const pcre2_convert_context PRIV(default_convert_context);
+extern const pcre2_match_context PRIV(default_match_context);
+extern const uint8_t PRIV(default_tables)[];
+extern const uint32_t PRIV(hspace_list)[];
+extern const uint32_t PRIV(vspace_list)[];
+extern const uint32_t PRIV(ucd_caseless_sets)[];
+extern const ucd_record PRIV(ucd_records)[];
+#if PCRE2_CODE_UNIT_WIDTH == 32
+extern const ucd_record PRIV(dummy_ucd_record)[];
+#endif
+extern const uint16_t PRIV(ucd_stage1)[];
+extern const uint16_t PRIV(ucd_stage2)[];
+extern const uint32_t PRIV(ucp_gbtable)[];
+extern const uint32_t PRIV(ucp_gentype)[];
+#ifdef SUPPORT_JIT
+extern const int PRIV(ucp_typerange)[];
+#endif
+extern const char *PRIV(unicode_version);
+extern const ucp_type_table PRIV(utt)[];
+extern const char PRIV(utt_names)[];
+extern const size_t PRIV(utt_size);
+
+/* Mode-dependent macros and hidden and private structures are defined in a
+separate file so that pcre2test can include them at all supported widths. When
+compiling the library, PCRE2_CODE_UNIT_WIDTH will be defined, and we can
+include them at the appropriate width, after setting up suffix macros for the
+private structures. */
+
+#define branch_chain PCRE2_SUFFIX(branch_chain_)
+#define compile_block PCRE2_SUFFIX(compile_block_)
+#define dfa_match_block PCRE2_SUFFIX(dfa_match_block_)
+#define match_block PCRE2_SUFFIX(match_block_)
+#define named_group PCRE2_SUFFIX(named_group_)
+
+#include "pcre2_intmodedep.h"
+
+/* Private "external" functions. These are internal functions that are called
+from modules other than the one in which they are defined. They have to be
+"external" in the C sense, but are not part of the PCRE2 public API. They are
+not referenced from pcre2test, and must not be defined when no code unit width
+is available. */
+
+#define _pcre2_auto_possessify PCRE2_SUFFIX(_pcre2_auto_possessify_)
+#define _pcre2_check_escape PCRE2_SUFFIX(_pcre2_check_escape_)
+#define _pcre2_extuni PCRE2_SUFFIX(_pcre2_extuni_)
+#define _pcre2_find_bracket PCRE2_SUFFIX(_pcre2_find_bracket_)
+#define _pcre2_is_newline PCRE2_SUFFIX(_pcre2_is_newline_)
+#define _pcre2_jit_free_rodata PCRE2_SUFFIX(_pcre2_jit_free_rodata_)
+#define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_)
+#define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_)
+#define _pcre2_jit_get_target PCRE2_SUFFIX(_pcre2_jit_get_target_)
+#define _pcre2_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_)
+#define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_)
+#define _pcre2_strcmp PCRE2_SUFFIX(_pcre2_strcmp_)
+#define _pcre2_strcmp_c8 PCRE2_SUFFIX(_pcre2_strcmp_c8_)
+#define _pcre2_strcpy_c8 PCRE2_SUFFIX(_pcre2_strcpy_c8_)
+#define _pcre2_strlen PCRE2_SUFFIX(_pcre2_strlen_)
+#define _pcre2_strncmp PCRE2_SUFFIX(_pcre2_strncmp_)
+#define _pcre2_strncmp_c8 PCRE2_SUFFIX(_pcre2_strncmp_c8_)
+#define _pcre2_study PCRE2_SUFFIX(_pcre2_study_)
+#define _pcre2_valid_utf PCRE2_SUFFIX(_pcre2_valid_utf_)
+#define _pcre2_was_newline PCRE2_SUFFIX(_pcre2_was_newline_)
+#define _pcre2_xclass PCRE2_SUFFIX(_pcre2_xclass_)
+
+extern int _pcre2_auto_possessify(PCRE2_UCHAR *, BOOL,
+ const compile_block *);
+extern int _pcre2_check_escape(PCRE2_SPTR *, PCRE2_SPTR, uint32_t *,
+ int *, uint32_t, BOOL, compile_block *);
+extern PCRE2_SPTR _pcre2_extuni(uint32_t, PCRE2_SPTR, PCRE2_SPTR, PCRE2_SPTR,
+ BOOL, int *);
+extern PCRE2_SPTR _pcre2_find_bracket(PCRE2_SPTR, BOOL, int);
+extern BOOL _pcre2_is_newline(PCRE2_SPTR, uint32_t, PCRE2_SPTR,
+ uint32_t *, BOOL);
+extern void _pcre2_jit_free_rodata(void *, void *);
+extern void _pcre2_jit_free(void *, pcre2_memctl *);
+extern size_t _pcre2_jit_get_size(void *);
+const char * _pcre2_jit_get_target(void);
+extern void * _pcre2_memctl_malloc(size_t, pcre2_memctl *);
+extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *);
+extern int _pcre2_strcmp(PCRE2_SPTR, PCRE2_SPTR);
+extern int _pcre2_strcmp_c8(PCRE2_SPTR, const char *);
+extern PCRE2_SIZE _pcre2_strcpy_c8(PCRE2_UCHAR *, const char *);
+extern PCRE2_SIZE _pcre2_strlen(PCRE2_SPTR);
+extern int _pcre2_strncmp(PCRE2_SPTR, PCRE2_SPTR, size_t);
+extern int _pcre2_strncmp_c8(PCRE2_SPTR, const char *, size_t);
+extern int _pcre2_study(pcre2_real_code *);
+extern int _pcre2_valid_utf(PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE *);
+extern BOOL _pcre2_was_newline(PCRE2_SPTR, uint32_t, PCRE2_SPTR,
+ uint32_t *, BOOL);
+extern BOOL _pcre2_xclass(uint32_t, PCRE2_SPTR, BOOL);
+
+/* This function is needed only when memmove() is not available. */
+
+#if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
+#define _pcre2_memmove PCRE2_SUFFIX(_pcre2_memmove)
+extern void * _pcre2_memmove(void *, const void *, size_t);
+#endif
+
+#endif /* PCRE2_CODE_UNIT_WIDTH */
+#endif /* PCRE2_INTERNAL_H_IDEMPOTENT_GUARD */
+
+/* End of pcre2_internal.h */
diff --git a/test/monniaux/pcre2-10.32/pcre2_intmodedep.h b/test/monniaux/pcre2-10.32/pcre2_intmodedep.h
new file mode 100644
index 00000000..62626d0a
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_intmodedep.h
@@ -0,0 +1,918 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains mode-dependent macro and structure definitions. The
+file is #included by pcre2_internal.h if PCRE2_CODE_UNIT_WIDTH is defined.
+These mode-dependent items are kept in a separate file so that they can also be
+#included multiple times for different code unit widths by pcre2test in order
+to have access to the hidden structures at all supported widths.
+
+Some of the mode-dependent macros are required at different widths for
+different parts of the pcre2test code (in particular, the included
+pcre_printint.c file). We undefine them here so that they can be re-defined for
+multiple inclusions. Not all of these are used in pcre2test, but it's easier
+just to undefine them all. */
+
+#undef ACROSSCHAR
+#undef BACKCHAR
+#undef BYTES2CU
+#undef CHMAX_255
+#undef CU2BYTES
+#undef FORWARDCHAR
+#undef FORWARDCHARTEST
+#undef GET
+#undef GET2
+#undef GETCHAR
+#undef GETCHARINC
+#undef GETCHARINCTEST
+#undef GETCHARLEN
+#undef GETCHARLENTEST
+#undef GETCHARTEST
+#undef GET_EXTRALEN
+#undef HAS_EXTRALEN
+#undef IMM2_SIZE
+#undef MAX_255
+#undef MAX_MARK
+#undef MAX_PATTERN_SIZE
+#undef MAX_UTF_SINGLE_CU
+#undef NOT_FIRSTCU
+#undef PUT
+#undef PUT2
+#undef PUT2INC
+#undef PUTCHAR
+#undef PUTINC
+#undef TABLE_GET
+
+
+
+/* -------------------------- MACROS ----------------------------- */
+
+/* PCRE keeps offsets in its compiled code as at least 16-bit quantities
+(always stored in big-endian order in 8-bit mode) by default. These are used,
+for example, to link from the start of a subpattern to its alternatives and its
+end. The use of 16 bits per offset limits the size of an 8-bit compiled regex
+to around 64K, which is big enough for almost everybody. However, I received a
+request for an even bigger limit. For this reason, and also to make the code
+easier to maintain, the storing and loading of offsets from the compiled code
+unit string is now handled by the macros that are defined here.
+
+The macros are controlled by the value of LINK_SIZE. This defaults to 2, but
+values of 3 or 4 are also supported. */
+
+/* ------------------- 8-bit support ------------------ */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+
+#if LINK_SIZE == 2
+#define PUT(a,n,d) \
+ (a[n] = (PCRE2_UCHAR)((d) >> 8)), \
+ (a[(n)+1] = (PCRE2_UCHAR)((d) & 255))
+#define GET(a,n) \
+ (unsigned int)(((a)[n] << 8) | (a)[(n)+1])
+#define MAX_PATTERN_SIZE (1 << 16)
+
+#elif LINK_SIZE == 3
+#define PUT(a,n,d) \
+ (a[n] = (PCRE2_UCHAR)((d) >> 16)), \
+ (a[(n)+1] = (PCRE2_UCHAR)((d) >> 8)), \
+ (a[(n)+2] = (PCRE2_UCHAR)((d) & 255))
+#define GET(a,n) \
+ (unsigned int)(((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2])
+#define MAX_PATTERN_SIZE (1 << 24)
+
+#elif LINK_SIZE == 4
+#define PUT(a,n,d) \
+ (a[n] = (PCRE2_UCHAR)((d) >> 24)), \
+ (a[(n)+1] = (PCRE2_UCHAR)((d) >> 16)), \
+ (a[(n)+2] = (PCRE2_UCHAR)((d) >> 8)), \
+ (a[(n)+3] = (PCRE2_UCHAR)((d) & 255))
+#define GET(a,n) \
+ (unsigned int)(((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3])
+#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */
+
+#else
+#error LINK_SIZE must be 2, 3, or 4
+#endif
+
+
+/* ------------------- 16-bit support ------------------ */
+
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+
+#if LINK_SIZE == 2
+#undef LINK_SIZE
+#define LINK_SIZE 1
+#define PUT(a,n,d) \
+ (a[n] = (PCRE2_UCHAR)(d))
+#define GET(a,n) \
+ (a[n])
+#define MAX_PATTERN_SIZE (1 << 16)
+
+#elif LINK_SIZE == 3 || LINK_SIZE == 4
+#undef LINK_SIZE
+#define LINK_SIZE 2
+#define PUT(a,n,d) \
+ (a[n] = (PCRE2_UCHAR)((d) >> 16)), \
+ (a[(n)+1] = (PCRE2_UCHAR)((d) & 65535))
+#define GET(a,n) \
+ (unsigned int)(((a)[n] << 16) | (a)[(n)+1])
+#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */
+
+#else
+#error LINK_SIZE must be 2, 3, or 4
+#endif
+
+
+/* ------------------- 32-bit support ------------------ */
+
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+#undef LINK_SIZE
+#define LINK_SIZE 1
+#define PUT(a,n,d) \
+ (a[n] = (d))
+#define GET(a,n) \
+ (a[n])
+#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */
+
+#else
+#error Unsupported compiling mode
+#endif
+
+
+/* --------------- Other mode-specific macros ----------------- */
+
+/* PCRE uses some other (at least) 16-bit quantities that do not change when
+the size of offsets changes. There are used for repeat counts and for other
+things such as capturing parenthesis numbers in back references.
+
+Define the number of code units required to hold a 16-bit count/offset, and
+macros to load and store such a value. For reasons that I do not understand,
+the expression in the 8-bit GET2 macro is treated by gcc as a signed
+expression, even when a is declared as unsigned. It seems that any kind of
+arithmetic results in a signed value. Hence the cast. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define IMM2_SIZE 2
+#define GET2(a,n) (unsigned int)(((a)[n] << 8) | (a)[(n)+1])
+#define PUT2(a,n,d) a[n] = (d) >> 8, a[(n)+1] = (d) & 255
+
+#else /* Code units are 16 or 32 bits */
+#define IMM2_SIZE 1
+#define GET2(a,n) a[n]
+#define PUT2(a,n,d) a[n] = d
+#endif
+
+/* Other macros that are different for 8-bit mode. The MAX_255 macro checks
+whether its argument, which is assumed to be one code unit, is less than 256.
+The CHMAX_255 macro does not assume one code unit. The maximum length of a MARK
+name must fit in one code unit; currently it is set to 255 or 65535. The
+TABLE_GET macro is used to access elements of tables containing exactly 256
+items. When code points can be greater than 255, a check is needed before
+accessing these tables. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define MAX_255(c) TRUE
+#define MAX_MARK ((1u << 8) - 1)
+#ifdef SUPPORT_UNICODE
+#define SUPPORT_WIDE_CHARS
+#define CHMAX_255(c) ((c) <= 255u)
+#else
+#define CHMAX_255(c) TRUE
+#endif /* SUPPORT_UNICODE */
+#define TABLE_GET(c, table, default) ((table)[c])
+
+#else /* Code units are 16 or 32 bits */
+#define CHMAX_255(c) ((c) <= 255u)
+#define MAX_255(c) ((c) <= 255u)
+#define MAX_MARK ((1u << 16) - 1)
+#define SUPPORT_WIDE_CHARS
+#define TABLE_GET(c, table, default) (MAX_255(c)? ((table)[c]):(default))
+#endif
+
+
+
+/* ----------------- Character-handling macros ----------------- */
+
+/* There is a proposed future special "UTF-21" mode, in which only the lowest
+21 bits of a 32-bit character are interpreted as UTF, with the remaining 11
+high-order bits available to the application for other uses. In preparation for
+the future implementation of this mode, there are macros that load a data item
+and, if in this special mode, mask it to 21 bits. These macros all have names
+starting with UCHAR21. In all other modes, including the normal 32-bit
+library, the macros all have the same simple definitions. When the new mode is
+implemented, it is expected that these definitions will be varied appropriately
+using #ifdef when compiling the library that supports the special mode. */
+
+#define UCHAR21(eptr) (*(eptr))
+#define UCHAR21TEST(eptr) (*(eptr))
+#define UCHAR21INC(eptr) (*(eptr)++)
+#define UCHAR21INCTEST(eptr) (*(eptr)++)
+
+/* When UTF encoding is being used, a character is no longer just a single
+byte in 8-bit mode or a single short in 16-bit mode. The macros for character
+handling generate simple sequences when used in the basic mode, and more
+complicated ones for UTF characters. GETCHARLENTEST and other macros are not
+used when UTF is not supported. To make sure they can never even appear when
+UTF support is omitted, we don't even define them. */
+
+#ifndef SUPPORT_UNICODE
+
+/* #define MAX_UTF_SINGLE_CU */
+/* #define HAS_EXTRALEN(c) */
+/* #define GET_EXTRALEN(c) */
+/* #define NOT_FIRSTCU(c) */
+#define GETCHAR(c, eptr) c = *eptr;
+#define GETCHARTEST(c, eptr) c = *eptr;
+#define GETCHARINC(c, eptr) c = *eptr++;
+#define GETCHARINCTEST(c, eptr) c = *eptr++;
+#define GETCHARLEN(c, eptr, len) c = *eptr;
+#define PUTCHAR(c, p) (*p = c, 1)
+/* #define GETCHARLENTEST(c, eptr, len) */
+/* #define BACKCHAR(eptr) */
+/* #define FORWARDCHAR(eptr) */
+/* #define FORWARCCHARTEST(eptr,end) */
+/* #define ACROSSCHAR(condition, eptr, action) */
+
+#else /* SUPPORT_UNICODE */
+
+/* ------------------- 8-bit support ------------------ */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define MAYBE_UTF_MULTI /* UTF chars may use multiple code units */
+
+/* The largest UTF code point that can be encoded as a single code unit. */
+
+#define MAX_UTF_SINGLE_CU 127
+
+/* Tests whether the code point needs extra characters to decode. */
+
+#define HAS_EXTRALEN(c) HASUTF8EXTRALEN(c)
+
+/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE.
+Otherwise it has an undefined behaviour. */
+
+#define GET_EXTRALEN(c) (PRIV(utf8_table4)[(c) & 0x3fu])
+
+/* Returns TRUE, if the given value is not the first code unit of a UTF
+sequence. */
+
+#define NOT_FIRSTCU(c) (((c) & 0xc0u) == 0x80u)
+
+/* Get the next UTF-8 character, not advancing the pointer. This is called when
+we know we are in UTF-8 mode. */
+
+#define GETCHAR(c, eptr) \
+ c = *eptr; \
+ if (c >= 0xc0u) GETUTF8(c, eptr);
+
+/* Get the next UTF-8 character, testing for UTF-8 mode, and not advancing the
+pointer. */
+
+#define GETCHARTEST(c, eptr) \
+ c = *eptr; \
+ if (utf && c >= 0xc0u) GETUTF8(c, eptr);
+
+/* Get the next UTF-8 character, advancing the pointer. This is called when we
+know we are in UTF-8 mode. */
+
+#define GETCHARINC(c, eptr) \
+ c = *eptr++; \
+ if (c >= 0xc0u) GETUTF8INC(c, eptr);
+
+/* Get the next character, testing for UTF-8 mode, and advancing the pointer.
+This is called when we don't know if we are in UTF-8 mode. */
+
+#define GETCHARINCTEST(c, eptr) \
+ c = *eptr++; \
+ if (utf && c >= 0xc0u) GETUTF8INC(c, eptr);
+
+/* Get the next UTF-8 character, not advancing the pointer, incrementing length
+if there are extra bytes. This is called when we know we are in UTF-8 mode. */
+
+#define GETCHARLEN(c, eptr, len) \
+ c = *eptr; \
+ if (c >= 0xc0u) GETUTF8LEN(c, eptr, len);
+
+/* Get the next UTF-8 character, testing for UTF-8 mode, not advancing the
+pointer, incrementing length if there are extra bytes. This is called when we
+do not know if we are in UTF-8 mode. */
+
+#define GETCHARLENTEST(c, eptr, len) \
+ c = *eptr; \
+ if (utf && c >= 0xc0u) GETUTF8LEN(c, eptr, len);
+
+/* If the pointer is not at the start of a character, move it back until
+it is. This is called only in UTF-8 mode - we don't put a test within the macro
+because almost all calls are already within a block of UTF-8 only code. */
+
+#define BACKCHAR(eptr) while((*eptr & 0xc0u) == 0x80u) eptr--
+
+/* Same as above, just in the other direction. */
+#define FORWARDCHAR(eptr) while((*eptr & 0xc0u) == 0x80u) eptr++
+#define FORWARDCHARTEST(eptr,end) while(eptr < end && (*eptr & 0xc0u) == 0x80u) eptr++
+
+/* Same as above, but it allows a fully customizable form. */
+#define ACROSSCHAR(condition, eptr, action) \
+ while((condition) && ((*eptr) & 0xc0u) == 0x80u) action
+
+/* Deposit a character into memory, returning the number of code units. */
+
+#define PUTCHAR(c, p) ((utf && c > MAX_UTF_SINGLE_CU)? \
+ PRIV(ord2utf)(c,p) : (*p = c, 1))
+
+
+/* ------------------- 16-bit support ------------------ */
+
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+#define MAYBE_UTF_MULTI /* UTF chars may use multiple code units */
+
+/* The largest UTF code point that can be encoded as a single code unit. */
+
+#define MAX_UTF_SINGLE_CU 65535
+
+/* Tests whether the code point needs extra characters to decode. */
+
+#define HAS_EXTRALEN(c) (((c) & 0xfc00u) == 0xd800u)
+
+/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE.
+Otherwise it has an undefined behaviour. */
+
+#define GET_EXTRALEN(c) 1
+
+/* Returns TRUE, if the given value is not the first code unit of a UTF
+sequence. */
+
+#define NOT_FIRSTCU(c) (((c) & 0xfc00u) == 0xdc00u)
+
+/* Base macro to pick up the low surrogate of a UTF-16 character, not
+advancing the pointer. */
+
+#define GETUTF16(c, eptr) \
+ { c = (((c & 0x3ffu) << 10) | (eptr[1] & 0x3ffu)) + 0x10000u; }
+
+/* Get the next UTF-16 character, not advancing the pointer. This is called when
+we know we are in UTF-16 mode. */
+
+#define GETCHAR(c, eptr) \
+ c = *eptr; \
+ if ((c & 0xfc00u) == 0xd800u) GETUTF16(c, eptr);
+
+/* Get the next UTF-16 character, testing for UTF-16 mode, and not advancing the
+pointer. */
+
+#define GETCHARTEST(c, eptr) \
+ c = *eptr; \
+ if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16(c, eptr);
+
+/* Base macro to pick up the low surrogate of a UTF-16 character, advancing
+the pointer. */
+
+#define GETUTF16INC(c, eptr) \
+ { c = (((c & 0x3ffu) << 10) | (*eptr++ & 0x3ffu)) + 0x10000u; }
+
+/* Get the next UTF-16 character, advancing the pointer. This is called when we
+know we are in UTF-16 mode. */
+
+#define GETCHARINC(c, eptr) \
+ c = *eptr++; \
+ if ((c & 0xfc00u) == 0xd800u) GETUTF16INC(c, eptr);
+
+/* Get the next character, testing for UTF-16 mode, and advancing the pointer.
+This is called when we don't know if we are in UTF-16 mode. */
+
+#define GETCHARINCTEST(c, eptr) \
+ c = *eptr++; \
+ if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16INC(c, eptr);
+
+/* Base macro to pick up the low surrogate of a UTF-16 character, not
+advancing the pointer, incrementing the length. */
+
+#define GETUTF16LEN(c, eptr, len) \
+ { c = (((c & 0x3ffu) << 10) | (eptr[1] & 0x3ffu)) + 0x10000u; len++; }
+
+/* Get the next UTF-16 character, not advancing the pointer, incrementing
+length if there is a low surrogate. This is called when we know we are in
+UTF-16 mode. */
+
+#define GETCHARLEN(c, eptr, len) \
+ c = *eptr; \
+ if ((c & 0xfc00u) == 0xd800u) GETUTF16LEN(c, eptr, len);
+
+/* Get the next UTF-816character, testing for UTF-16 mode, not advancing the
+pointer, incrementing length if there is a low surrogate. This is called when
+we do not know if we are in UTF-16 mode. */
+
+#define GETCHARLENTEST(c, eptr, len) \
+ c = *eptr; \
+ if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16LEN(c, eptr, len);
+
+/* If the pointer is not at the start of a character, move it back until
+it is. This is called only in UTF-16 mode - we don't put a test within the
+macro because almost all calls are already within a block of UTF-16 only
+code. */
+
+#define BACKCHAR(eptr) if ((*eptr & 0xfc00u) == 0xdc00u) eptr--
+
+/* Same as above, just in the other direction. */
+#define FORWARDCHAR(eptr) if ((*eptr & 0xfc00u) == 0xdc00u) eptr++
+#define FORWARDCHARTEST(eptr,end) if (eptr < end && (*eptr & 0xfc00u) == 0xdc00u) eptr++
+
+/* Same as above, but it allows a fully customizable form. */
+#define ACROSSCHAR(condition, eptr, action) \
+ if ((condition) && ((*eptr) & 0xfc00u) == 0xdc00u) action
+
+/* Deposit a character into memory, returning the number of code units. */
+
+#define PUTCHAR(c, p) ((utf && c > MAX_UTF_SINGLE_CU)? \
+ PRIV(ord2utf)(c,p) : (*p = c, 1))
+
+
+/* ------------------- 32-bit support ------------------ */
+
+#else
+
+/* These are trivial for the 32-bit library, since all UTF-32 characters fit
+into one PCRE2_UCHAR unit. */
+
+#define MAX_UTF_SINGLE_CU (0x10ffffu)
+#define HAS_EXTRALEN(c) (0)
+#define GET_EXTRALEN(c) (0)
+#define NOT_FIRSTCU(c) (0)
+
+/* Get the next UTF-32 character, not advancing the pointer. This is called when
+we know we are in UTF-32 mode. */
+
+#define GETCHAR(c, eptr) \
+ c = *(eptr);
+
+/* Get the next UTF-32 character, testing for UTF-32 mode, and not advancing the
+pointer. */
+
+#define GETCHARTEST(c, eptr) \
+ c = *(eptr);
+
+/* Get the next UTF-32 character, advancing the pointer. This is called when we
+know we are in UTF-32 mode. */
+
+#define GETCHARINC(c, eptr) \
+ c = *((eptr)++);
+
+/* Get the next character, testing for UTF-32 mode, and advancing the pointer.
+This is called when we don't know if we are in UTF-32 mode. */
+
+#define GETCHARINCTEST(c, eptr) \
+ c = *((eptr)++);
+
+/* Get the next UTF-32 character, not advancing the pointer, not incrementing
+length (since all UTF-32 is of length 1). This is called when we know we are in
+UTF-32 mode. */
+
+#define GETCHARLEN(c, eptr, len) \
+ GETCHAR(c, eptr)
+
+/* Get the next UTF-32character, testing for UTF-32 mode, not advancing the
+pointer, not incrementing the length (since all UTF-32 is of length 1).
+This is called when we do not know if we are in UTF-32 mode. */
+
+#define GETCHARLENTEST(c, eptr, len) \
+ GETCHARTEST(c, eptr)
+
+/* If the pointer is not at the start of a character, move it back until
+it is. This is called only in UTF-32 mode - we don't put a test within the
+macro because almost all calls are already within a block of UTF-32 only
+code.
+
+These are all no-ops since all UTF-32 characters fit into one pcre_uchar. */
+
+#define BACKCHAR(eptr) do { } while (0)
+
+/* Same as above, just in the other direction. */
+
+#define FORWARDCHAR(eptr) do { } while (0)
+#define FORWARDCHARTEST(eptr,end) do { } while (0)
+
+/* Same as above, but it allows a fully customizable form. */
+
+#define ACROSSCHAR(condition, eptr, action) do { } while (0)
+
+/* Deposit a character into memory, returning the number of code units. */
+
+#define PUTCHAR(c, p) (*p = c, 1)
+
+#endif /* UTF-32 character handling */
+#endif /* SUPPORT_UNICODE */
+
+
+/* Mode-dependent macros that have the same definition in all modes. */
+
+#define CU2BYTES(x) ((x)*((PCRE2_CODE_UNIT_WIDTH/8)))
+#define BYTES2CU(x) ((x)/((PCRE2_CODE_UNIT_WIDTH/8)))
+#define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE
+#define PUT2INC(a,n,d) PUT2(a,n,d), a += IMM2_SIZE
+
+
+/* ----------------------- HIDDEN STRUCTURES ----------------------------- */
+
+/* NOTE: All these structures *must* start with a pcre2_memctl structure. The
+code that uses them is simpler because it assumes this. */
+
+/* The real general context structure. At present it holds only data for custom
+memory control. */
+
+typedef struct pcre2_real_general_context {
+ pcre2_memctl memctl;
+} pcre2_real_general_context;
+
+/* The real compile context structure */
+
+typedef struct pcre2_real_compile_context {
+ pcre2_memctl memctl;
+ int (*stack_guard)(uint32_t, void *);
+ void *stack_guard_data;
+ const uint8_t *tables;
+ PCRE2_SIZE max_pattern_length;
+ uint16_t bsr_convention;
+ uint16_t newline_convention;
+ uint32_t parens_nest_limit;
+ uint32_t extra_options;
+} pcre2_real_compile_context;
+
+/* The real match context structure. */
+
+typedef struct pcre2_real_match_context {
+ pcre2_memctl memctl;
+#ifdef SUPPORT_JIT
+ pcre2_jit_callback jit_callback;
+ void *jit_callback_data;
+#endif
+ int (*callout)(pcre2_callout_block *, void *);
+ void *callout_data;
+ PCRE2_SIZE offset_limit;
+ uint32_t heap_limit;
+ uint32_t match_limit;
+ uint32_t depth_limit;
+} pcre2_real_match_context;
+
+/* The real convert context structure. */
+
+typedef struct pcre2_real_convert_context {
+ pcre2_memctl memctl;
+ uint32_t glob_separator;
+ uint32_t glob_escape;
+} pcre2_real_convert_context;
+
+/* The real compiled code structure. The type for the blocksize field is
+defined specially because it is required in pcre2_serialize_decode() when
+copying the size from possibly unaligned memory into a variable of the same
+type. Use a macro rather than a typedef to avoid compiler warnings when this
+file is included multiple times by pcre2test. LOOKBEHIND_MAX specifies the
+largest lookbehind that is supported. (OP_REVERSE in a pattern has a 16-bit
+argument in 8-bit and 16-bit modes, so we need no more than a 16-bit field
+here.) */
+
+#undef CODE_BLOCKSIZE_TYPE
+#define CODE_BLOCKSIZE_TYPE size_t
+
+#undef LOOKBEHIND_MAX
+#define LOOKBEHIND_MAX UINT16_MAX
+
+typedef struct pcre2_real_code {
+ pcre2_memctl memctl; /* Memory control fields */
+ const uint8_t *tables; /* The character tables */
+ void *executable_jit; /* Pointer to JIT code */
+ uint8_t start_bitmap[32]; /* Bitmap for starting code unit < 256 */
+ CODE_BLOCKSIZE_TYPE blocksize; /* Total (bytes) that was malloc-ed */
+ uint32_t magic_number; /* Paranoid and endianness check */
+ uint32_t compile_options; /* Options passed to pcre2_compile() */
+ uint32_t overall_options; /* Options after processing the pattern */
+ uint32_t extra_options; /* Taken from compile_context */
+ uint32_t flags; /* Various state flags */
+ uint32_t limit_heap; /* Limit set in the pattern */
+ uint32_t limit_match; /* Limit set in the pattern */
+ uint32_t limit_depth; /* Limit set in the pattern */
+ uint32_t first_codeunit; /* Starting code unit */
+ uint32_t last_codeunit; /* This codeunit must be seen */
+ uint16_t bsr_convention; /* What \R matches */
+ uint16_t newline_convention; /* What is a newline? */
+ uint16_t max_lookbehind; /* Longest lookbehind (characters) */
+ uint16_t minlength; /* Minimum length of match */
+ uint16_t top_bracket; /* Highest numbered group */
+ uint16_t top_backref; /* Highest numbered back reference */
+ uint16_t name_entry_size; /* Size (code units) of table entries */
+ uint16_t name_count; /* Number of name entries in the table */
+} pcre2_real_code;
+
+/* The real match data structure. Define ovector as large as it can ever
+actually be so that array bound checkers don't grumble. Memory for this
+structure is obtained by calling pcre2_match_data_create(), which sets the size
+as the offset of ovector plus a pair of elements for each capturable string, so
+the size varies from call to call. As the maximum number of capturing
+subpatterns is 65535 we must allow for 65536 strings to include the overall
+match. (See also the heapframe structure below.) */
+
+typedef struct pcre2_real_match_data {
+ pcre2_memctl memctl;
+ const pcre2_real_code *code; /* The pattern used for the match */
+ PCRE2_SPTR subject; /* The subject that was matched */
+ PCRE2_SPTR mark; /* Pointer to last mark */
+ PCRE2_SIZE leftchar; /* Offset to leftmost code unit */
+ PCRE2_SIZE rightchar; /* Offset to rightmost code unit */
+ PCRE2_SIZE startchar; /* Offset to starting code unit */
+ uint16_t matchedby; /* Type of match (normal, JIT, DFA) */
+ uint16_t oveccount; /* Number of pairs */
+ int rc; /* The return code from the match */
+ PCRE2_SIZE ovector[131072]; /* Must be last in the structure */
+} pcre2_real_match_data;
+
+
+/* ----------------------- PRIVATE STRUCTURES ----------------------------- */
+
+/* These structures are not needed for pcre2test. */
+
+#ifndef PCRE2_PCRE2TEST
+
+/* Structures for checking for mutual recursion when scanning compiled or
+parsed code. */
+
+typedef struct recurse_check {
+ struct recurse_check *prev;
+ PCRE2_SPTR group;
+} recurse_check;
+
+typedef struct parsed_recurse_check {
+ struct parsed_recurse_check *prev;
+ uint32_t *groupptr;
+} parsed_recurse_check;
+
+/* Structure for building a cache when filling in recursion offsets. */
+
+typedef struct recurse_cache {
+ PCRE2_SPTR group;
+ int groupnumber;
+} recurse_cache;
+
+/* Structure for maintaining a chain of pointers to the currently incomplete
+branches, for testing for left recursion while compiling. */
+
+typedef struct branch_chain {
+ struct branch_chain *outer;
+ PCRE2_UCHAR *current_branch;
+} branch_chain;
+
+/* Structure for building a list of named groups during the first pass of
+compiling. */
+
+typedef struct named_group {
+ PCRE2_SPTR name; /* Points to the name in the pattern */
+ uint32_t number; /* Group number */
+ uint16_t length; /* Length of the name */
+ uint16_t isdup; /* TRUE if a duplicate */
+} named_group;
+
+/* Structure for passing "static" information around between the functions
+doing the compiling, so that they are thread-safe. */
+
+typedef struct compile_block {
+ pcre2_real_compile_context *cx; /* Points to the compile context */
+ const uint8_t *lcc; /* Points to lower casing table */
+ const uint8_t *fcc; /* Points to case-flipping table */
+ const uint8_t *cbits; /* Points to character type table */
+ const uint8_t *ctypes; /* Points to table of type maps */
+ PCRE2_SPTR start_workspace; /* The start of working space */
+ PCRE2_SPTR start_code; /* The start of the compiled code */
+ PCRE2_SPTR start_pattern; /* The start of the pattern */
+ PCRE2_SPTR end_pattern; /* The end of the pattern */
+ PCRE2_UCHAR *name_table; /* The name/number table */
+ PCRE2_SIZE workspace_size; /* Size of workspace */
+ PCRE2_SIZE small_ref_offset[10]; /* Offsets for \1 to \9 */
+ PCRE2_SIZE erroroffset; /* Offset of error in pattern */
+ uint16_t names_found; /* Number of entries so far */
+ uint16_t name_entry_size; /* Size of each entry */
+ uint16_t parens_depth; /* Depth of nested parentheses */
+ uint16_t assert_depth; /* Depth of nested assertions */
+ open_capitem *open_caps; /* Chain of open capture items */
+ named_group *named_groups; /* Points to vector in pre-compile */
+ uint32_t named_group_list_size; /* Number of entries in the list */
+ uint32_t external_options; /* External (initial) options */
+ uint32_t external_flags; /* External flag bits to be set */
+ uint32_t bracount; /* Count of capturing parentheses */
+ uint32_t lastcapture; /* Last capture encountered */
+ uint32_t *parsed_pattern; /* Parsed pattern buffer */
+ uint32_t *parsed_pattern_end; /* Parsed pattern should not get here */
+ uint32_t *groupinfo; /* Group info vector */
+ uint32_t top_backref; /* Maximum back reference */
+ uint32_t backref_map; /* Bitmap of low back refs */
+ uint32_t nltype; /* Newline type */
+ uint32_t nllen; /* Newline string length */
+ uint32_t class_range_start; /* Overall class range start */
+ uint32_t class_range_end; /* Overall class range end */
+ PCRE2_UCHAR nl[4]; /* Newline string when fixed length */
+ int max_lookbehind; /* Maximum lookbehind (characters) */
+ int req_varyopt; /* "After variable item" flag for reqbyte */
+ BOOL had_accept; /* (*ACCEPT) encountered */
+ BOOL had_pruneorskip; /* (*PRUNE) or (*SKIP) encountered */
+ BOOL had_recurse; /* Had a recursion or subroutine call */
+ BOOL dupnames; /* Duplicate names exist */
+} compile_block;
+
+/* Structure for keeping the properties of the in-memory stack used
+by the JIT matcher. */
+
+typedef struct pcre2_real_jit_stack {
+ pcre2_memctl memctl;
+ void* stack;
+} pcre2_real_jit_stack;
+
+/* Structure for items in a linked list that represents an explicit recursive
+call within the pattern when running pcre_dfa_match(). */
+
+typedef struct dfa_recursion_info {
+ struct dfa_recursion_info *prevrec;
+ PCRE2_SPTR subject_position;
+ uint32_t group_num;
+} dfa_recursion_info;
+
+/* Structure for "stack" frames that are used for remembering backtracking
+positions during matching. As these are used in a vector, with the ovector item
+being extended, the size of the structure must be a multiple of PCRE2_SIZE. The
+only way to check this at compile time is to force an error by generating an
+array with a negative size. By putting this in a typedef (which is never used),
+we don't generate any code when all is well. */
+
+typedef struct heapframe {
+
+ /* The first set of fields are variables that have to be preserved over calls
+ to RRMATCH(), but which do not need to be copied to new frames. */
+
+ PCRE2_SPTR ecode; /* The current position in the pattern */
+ PCRE2_SPTR temp_sptr[2]; /* Used for short-term PCRE_SPTR values */
+ PCRE2_SIZE length; /* Used for character, string, or code lengths */
+ PCRE2_SIZE back_frame; /* Amount to subtract on RRETURN */
+ PCRE2_SIZE temp_size; /* Used for short-term PCRE2_SIZE values */
+ uint32_t rdepth; /* "Recursion" depth */
+ uint32_t group_frame_type; /* Type information for group frames */
+ uint32_t temp_32[4]; /* Used for short-term 32-bit or BOOL values */
+ uint8_t return_id; /* Where to go on in internal "return" */
+ uint8_t op; /* Processing opcode */
+
+ /* At this point, the structure is 16-bit aligned. On most architectures
+ the alignment requirement for a pointer will ensure that the eptr field below
+ is 32-bit or 64-bit aligned. However, on m68k it is fine to have a pointer
+ that is 16-bit aligned. We must therefore ensure that what comes between here
+ and eptr is an odd multiple of 16 bits so as to get back into 32-bit
+ alignment. This happens naturally when PCRE2_UCHAR is 8 bits wide, but needs
+ fudges in the other cases. In the 32-bit case the padding comes first so that
+ the occu field itself is 32-bit aligned. Without the padding, this structure
+ is no longer a multiple of PCRE2_SIZE on m68k, and the check below fails. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ PCRE2_UCHAR occu[6]; /* Used for other case code units */
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ PCRE2_UCHAR occu[2]; /* Used for other case code units */
+ uint8_t unused[2]; /* Ensure 32-bit alignment (see above) */
+#else
+ uint8_t unused[2]; /* Ensure 32-bit alignment (see above) */
+ PCRE2_UCHAR occu[1]; /* Used for other case code units */
+#endif
+
+ /* The rest have to be copied from the previous frame whenever a new frame
+ becomes current. The final field is specified as a large vector so that
+ runtime array bound checks don't catch references to it. However, for any
+ specific call to pcre2_match() the memory allocated for each frame structure
+ allows for exactly the right size ovector for the number of capturing
+ parentheses. (See also the comment for pcre2_real_match_data above.) */
+
+ PCRE2_SPTR eptr; /* MUST BE FIRST */
+ PCRE2_SPTR start_match; /* Can be adjusted by \K */
+ PCRE2_SPTR mark; /* Most recent mark on the success path */
+ uint32_t current_recurse; /* Current (deepest) recursion number */
+ uint32_t capture_last; /* Most recent capture */
+ PCRE2_SIZE last_group_offset; /* Saved offset to most recent group frame */
+ PCRE2_SIZE offset_top; /* Offset after highest capture */
+ PCRE2_SIZE ovector[131072]; /* Must be last in the structure */
+} heapframe;
+
+/* This typedef is a check that the size of the heapframe structure is a
+multiple of PCRE2_SIZE. See various comments above. */
+
+typedef char check_heapframe_size[
+ ((sizeof(heapframe) % sizeof(PCRE2_SIZE)) == 0)? (+1):(-1)];
+
+/* Structure for passing "static" information around between the functions
+doing traditional NFA matching (pcre2_match() and friends). */
+
+typedef struct match_block {
+ pcre2_memctl memctl; /* For general use */
+ PCRE2_SIZE frame_vector_size; /* Size of a backtracking frame */
+ heapframe *match_frames; /* Points to vector of frames */
+ heapframe *match_frames_top; /* Points after the end of the vector */
+ heapframe *stack_frames; /* The original vector on the stack */
+ PCRE2_SIZE heap_limit; /* As it says */
+ uint32_t match_limit; /* As it says */
+ uint32_t match_limit_depth; /* As it says */
+ uint32_t match_call_count; /* Number of times a new frame is created */
+ BOOL hitend; /* Hit the end of the subject at some point */
+ BOOL hasthen; /* Pattern contains (*THEN) */
+ const uint8_t *lcc; /* Points to lower casing table */
+ const uint8_t *fcc; /* Points to case-flipping table */
+ const uint8_t *ctypes; /* Points to table of type maps */
+ PCRE2_SIZE start_offset; /* The start offset value */
+ PCRE2_SIZE end_offset_top; /* Highwater mark at end of match */
+ uint16_t partial; /* PARTIAL options */
+ uint16_t bsr_convention; /* \R interpretation */
+ uint16_t name_count; /* Number of names in name table */
+ uint16_t name_entry_size; /* Size of entry in names table */
+ PCRE2_SPTR name_table; /* Table of group names */
+ PCRE2_SPTR start_code; /* For use when recursing */
+ PCRE2_SPTR start_subject; /* Start of the subject string */
+ PCRE2_SPTR end_subject; /* End of the subject string */
+ PCRE2_SPTR end_match_ptr; /* Subject position at end match */
+ PCRE2_SPTR start_used_ptr; /* Earliest consulted character */
+ PCRE2_SPTR last_used_ptr; /* Latest consulted character */
+ PCRE2_SPTR mark; /* Mark pointer to pass back on success */
+ PCRE2_SPTR nomatch_mark; /* Mark pointer to pass back on failure */
+ PCRE2_SPTR verb_ecode_ptr; /* For passing back info */
+ PCRE2_SPTR verb_skip_ptr; /* For passing back a (*SKIP) name */
+ uint32_t verb_current_recurse; /* Current recurse when (*VERB) happens */
+ uint32_t moptions; /* Match options */
+ uint32_t poptions; /* Pattern options */
+ uint32_t skip_arg_count; /* For counting SKIP_ARGs */
+ uint32_t ignore_skip_arg; /* For re-run when SKIP arg name not found */
+ uint32_t nltype; /* Newline type */
+ uint32_t nllen; /* Newline string length */
+ PCRE2_UCHAR nl[4]; /* Newline string when fixed */
+ pcre2_callout_block *cb; /* Points to a callout block */
+ void *callout_data; /* To pass back to callouts */
+ int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */
+} match_block;
+
+/* A similar structure is used for the same purpose by the DFA matching
+functions. */
+
+typedef struct dfa_match_block {
+ pcre2_memctl memctl; /* For general use */
+ PCRE2_SPTR start_code; /* Start of the compiled pattern */
+ PCRE2_SPTR start_subject ; /* Start of the subject string */
+ PCRE2_SPTR end_subject; /* End of subject string */
+ PCRE2_SPTR start_used_ptr; /* Earliest consulted character */
+ PCRE2_SPTR last_used_ptr; /* Latest consulted character */
+ const uint8_t *tables; /* Character tables */
+ PCRE2_SIZE start_offset; /* The start offset value */
+ PCRE2_SIZE heap_limit; /* As it says */
+ PCRE2_SIZE heap_used; /* As it says */
+ uint32_t match_limit; /* As it says */
+ uint32_t match_limit_depth; /* As it says */
+ uint32_t match_call_count; /* Number of calls of internal function */
+ uint32_t moptions; /* Match options */
+ uint32_t poptions; /* Pattern options */
+ uint32_t nltype; /* Newline type */
+ uint32_t nllen; /* Newline string length */
+ PCRE2_UCHAR nl[4]; /* Newline string when fixed */
+ uint16_t bsr_convention; /* \R interpretation */
+ pcre2_callout_block *cb; /* Points to a callout block */
+ void *callout_data; /* To pass back to callouts */
+ int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */
+ dfa_recursion_info *recursive; /* Linked list of recursion data */
+} dfa_match_block;
+
+#endif /* PCRE2_PCRE2TEST */
+
+/* End of pcre2_intmodedep.h */
diff --git a/test/monniaux/pcre2-10.32/pcre2_jit_compile.c b/test/monniaux/pcre2-10.32/pcre2_jit_compile.c
new file mode 100644
index 00000000..32e985b7
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_jit_compile.c
@@ -0,0 +1,12708 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+#ifdef SUPPORT_JIT
+
+/* All-in-one: Since we use the JIT compiler only from here,
+we just include it. This way we don't need to touch the build
+system files. */
+
+#define SLJIT_CONFIG_AUTO 1
+#define SLJIT_CONFIG_STATIC 1
+#define SLJIT_VERBOSE 0
+
+#ifdef PCRE2_DEBUG
+#define SLJIT_DEBUG 1
+#else
+#define SLJIT_DEBUG 0
+#endif
+
+#define SLJIT_MALLOC(size, allocator_data) pcre2_jit_malloc(size, allocator_data)
+#define SLJIT_FREE(ptr, allocator_data) pcre2_jit_free(ptr, allocator_data)
+
+static void * pcre2_jit_malloc(size_t size, void *allocator_data)
+{
+pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
+return allocator->malloc(size, allocator->memory_data);
+}
+
+static void pcre2_jit_free(void *ptr, void *allocator_data)
+{
+pcre2_memctl *allocator = ((pcre2_memctl*)allocator_data);
+allocator->free(ptr, allocator->memory_data);
+}
+
+#include "sljit/sljitLir.c"
+
+#if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
+#error Unsupported architecture
+#endif
+
+/* Defines for debugging purposes. */
+
+/* 1 - Use unoptimized capturing brackets.
+ 2 - Enable capture_last_ptr (includes option 1). */
+/* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */
+
+/* 1 - Always have a control head. */
+/* #define DEBUG_FORCE_CONTROL_HEAD 1 */
+
+/* Allocate memory for the regex stack on the real machine stack.
+Fast, but limited size. */
+#define MACHINE_STACK_SIZE 32768
+
+/* Growth rate for stack allocated by the OS. Should be the multiply
+of page size. */
+#define STACK_GROWTH_RATE 8192
+
+/* Enable to check that the allocation could destroy temporaries. */
+#if defined SLJIT_DEBUG && SLJIT_DEBUG
+#define DESTROY_REGISTERS 1
+#endif
+
+/*
+Short summary about the backtracking mechanism empolyed by the jit code generator:
+
+The code generator follows the recursive nature of the PERL compatible regular
+expressions. The basic blocks of regular expressions are condition checkers
+whose execute different commands depending on the result of the condition check.
+The relationship between the operators can be horizontal (concatenation) and
+vertical (sub-expression) (See struct backtrack_common for more details).
+
+ 'ab' - 'a' and 'b' regexps are concatenated
+ 'a+' - 'a' is the sub-expression of the '+' operator
+
+The condition checkers are boolean (true/false) checkers. Machine code is generated
+for the checker itself and for the actions depending on the result of the checker.
+The 'true' case is called as the matching path (expected path), and the other is called as
+the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
+branches on the matching path.
+
+ Greedy star operator (*) :
+ Matching path: match happens.
+ Backtrack path: match failed.
+ Non-greedy star operator (*?) :
+ Matching path: no need to perform a match.
+ Backtrack path: match is required.
+
+The following example shows how the code generated for a capturing bracket
+with two alternatives. Let A, B, C, D are arbirary regular expressions, and
+we have the following regular expression:
+
+ A(B|C)D
+
+The generated code will be the following:
+
+ A matching path
+ '(' matching path (pushing arguments to the stack)
+ B matching path
+ ')' matching path (pushing arguments to the stack)
+ D matching path
+ return with successful match
+
+ D backtrack path
+ ')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
+ B backtrack path
+ C expected path
+ jump to D matching path
+ C backtrack path
+ A backtrack path
+
+ Notice, that the order of backtrack code paths are the opposite of the fast
+ code paths. In this way the topmost value on the stack is always belong
+ to the current backtrack code path. The backtrack path must check
+ whether there is a next alternative. If so, it needs to jump back to
+ the matching path eventually. Otherwise it needs to clear out its own stack
+ frame and continue the execution on the backtrack code paths.
+*/
+
+/*
+Saved stack frames:
+
+Atomic blocks and asserts require reloading the values of private data
+when the backtrack mechanism performed. Because of OP_RECURSE, the data
+are not necessarly known in compile time, thus we need a dynamic restore
+mechanism.
+
+The stack frames are stored in a chain list, and have the following format:
+([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
+
+Thus we can restore the private data to a particular point in the stack.
+*/
+
+typedef struct jit_arguments {
+ /* Pointers first. */
+ struct sljit_stack *stack;
+ PCRE2_SPTR str;
+ PCRE2_SPTR begin;
+ PCRE2_SPTR end;
+ pcre2_match_data *match_data;
+ PCRE2_SPTR startchar_ptr;
+ PCRE2_UCHAR *mark_ptr;
+ int (*callout)(pcre2_callout_block *, void *);
+ void *callout_data;
+ /* Everything else after. */
+ sljit_uw offset_limit;
+ sljit_u32 limit_match;
+ sljit_u32 oveccount;
+ sljit_u32 options;
+} jit_arguments;
+
+#define JIT_NUMBER_OF_COMPILE_MODES 3
+
+typedef struct executable_functions {
+ void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
+ void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES];
+ sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
+ sljit_u32 top_bracket;
+ sljit_u32 limit_match;
+} executable_functions;
+
+typedef struct jump_list {
+ struct sljit_jump *jump;
+ struct jump_list *next;
+} jump_list;
+
+typedef struct stub_list {
+ struct sljit_jump *start;
+ struct sljit_label *quit;
+ struct stub_list *next;
+} stub_list;
+
+typedef struct label_addr_list {
+ struct sljit_label *label;
+ sljit_uw *update_addr;
+ struct label_addr_list *next;
+} label_addr_list;
+
+enum frame_types {
+ no_frame = -1,
+ no_stack = -2
+};
+
+enum control_types {
+ type_mark = 0,
+ type_then_trap = 1
+};
+
+typedef int (SLJIT_FUNC *jit_function)(jit_arguments *args);
+
+/* The following structure is the key data type for the recursive
+code generator. It is allocated by compile_matchingpath, and contains
+the arguments for compile_backtrackingpath. Must be the first member
+of its descendants. */
+typedef struct backtrack_common {
+ /* Concatenation stack. */
+ struct backtrack_common *prev;
+ jump_list *nextbacktracks;
+ /* Internal stack (for component operators). */
+ struct backtrack_common *top;
+ jump_list *topbacktracks;
+ /* Opcode pointer. */
+ PCRE2_SPTR cc;
+} backtrack_common;
+
+typedef struct assert_backtrack {
+ backtrack_common common;
+ jump_list *condfailed;
+ /* Less than 0 if a frame is not needed. */
+ int framesize;
+ /* Points to our private memory word on the stack. */
+ int private_data_ptr;
+ /* For iterators. */
+ struct sljit_label *matchingpath;
+} assert_backtrack;
+
+typedef struct bracket_backtrack {
+ backtrack_common common;
+ /* Where to coninue if an alternative is successfully matched. */
+ struct sljit_label *alternative_matchingpath;
+ /* For rmin and rmax iterators. */
+ struct sljit_label *recursive_matchingpath;
+ /* For greedy ? operator. */
+ struct sljit_label *zero_matchingpath;
+ /* Contains the branches of a failed condition. */
+ union {
+ /* Both for OP_COND, OP_SCOND. */
+ jump_list *condfailed;
+ assert_backtrack *assert;
+ /* For OP_ONCE. Less than 0 if not needed. */
+ int framesize;
+ } u;
+ /* Points to our private memory word on the stack. */
+ int private_data_ptr;
+} bracket_backtrack;
+
+typedef struct bracketpos_backtrack {
+ backtrack_common common;
+ /* Points to our private memory word on the stack. */
+ int private_data_ptr;
+ /* Reverting stack is needed. */
+ int framesize;
+ /* Allocated stack size. */
+ int stacksize;
+} bracketpos_backtrack;
+
+typedef struct braminzero_backtrack {
+ backtrack_common common;
+ struct sljit_label *matchingpath;
+} braminzero_backtrack;
+
+typedef struct char_iterator_backtrack {
+ backtrack_common common;
+ /* Next iteration. */
+ struct sljit_label *matchingpath;
+ union {
+ jump_list *backtracks;
+ struct {
+ unsigned int othercasebit;
+ PCRE2_UCHAR chr;
+ BOOL enabled;
+ } charpos;
+ } u;
+} char_iterator_backtrack;
+
+typedef struct ref_iterator_backtrack {
+ backtrack_common common;
+ /* Next iteration. */
+ struct sljit_label *matchingpath;
+} ref_iterator_backtrack;
+
+typedef struct recurse_entry {
+ struct recurse_entry *next;
+ /* Contains the function entry label. */
+ struct sljit_label *entry_label;
+ /* Contains the function entry label. */
+ struct sljit_label *backtrack_label;
+ /* Collects the entry calls until the function is not created. */
+ jump_list *entry_calls;
+ /* Collects the backtrack calls until the function is not created. */
+ jump_list *backtrack_calls;
+ /* Points to the starting opcode. */
+ sljit_sw start;
+} recurse_entry;
+
+typedef struct recurse_backtrack {
+ backtrack_common common;
+ /* Return to the matching path. */
+ struct sljit_label *matchingpath;
+ /* Recursive pattern. */
+ recurse_entry *entry;
+ /* Pattern is inlined. */
+ BOOL inlined_pattern;
+} recurse_backtrack;
+
+#define OP_THEN_TRAP OP_TABLE_LENGTH
+
+typedef struct then_trap_backtrack {
+ backtrack_common common;
+ /* If then_trap is not NULL, this structure contains the real
+ then_trap for the backtracking path. */
+ struct then_trap_backtrack *then_trap;
+ /* Points to the starting opcode. */
+ sljit_sw start;
+ /* Exit point for the then opcodes of this alternative. */
+ jump_list *quit;
+ /* Frame size of the current alternative. */
+ int framesize;
+} then_trap_backtrack;
+
+#define MAX_N_CHARS 12
+#define MAX_DIFF_CHARS 5
+
+typedef struct fast_forward_char_data {
+ /* Number of characters in the chars array, 255 for any character. */
+ sljit_u8 count;
+ /* Number of last UTF-8 characters in the chars array. */
+ sljit_u8 last_count;
+ /* Available characters in the current position. */
+ PCRE2_UCHAR chars[MAX_DIFF_CHARS];
+} fast_forward_char_data;
+
+#define MAX_CLASS_RANGE_SIZE 4
+#define MAX_CLASS_CHARS_SIZE 3
+
+typedef struct compiler_common {
+ /* The sljit ceneric compiler. */
+ struct sljit_compiler *compiler;
+ /* Compiled regular expression. */
+ pcre2_real_code *re;
+ /* First byte code. */
+ PCRE2_SPTR start;
+ /* Maps private data offset to each opcode. */
+ sljit_s32 *private_data_ptrs;
+ /* Chain list of read-only data ptrs. */
+ void *read_only_data_head;
+ /* Tells whether the capturing bracket is optimized. */
+ sljit_u8 *optimized_cbracket;
+ /* Tells whether the starting offset is a target of then. */
+ sljit_u8 *then_offsets;
+ /* Current position where a THEN must jump. */
+ then_trap_backtrack *then_trap;
+ /* Starting offset of private data for capturing brackets. */
+ sljit_s32 cbra_ptr;
+ /* Output vector starting point. Must be divisible by 2. */
+ sljit_s32 ovector_start;
+ /* Points to the starting character of the current match. */
+ sljit_s32 start_ptr;
+ /* Last known position of the requested byte. */
+ sljit_s32 req_char_ptr;
+ /* Head of the last recursion. */
+ sljit_s32 recursive_head_ptr;
+ /* First inspected character for partial matching.
+ (Needed for avoiding zero length partial matches.) */
+ sljit_s32 start_used_ptr;
+ /* Starting pointer for partial soft matches. */
+ sljit_s32 hit_start;
+ /* Pointer of the match end position. */
+ sljit_s32 match_end_ptr;
+ /* Points to the marked string. */
+ sljit_s32 mark_ptr;
+ /* Recursive control verb management chain. */
+ sljit_s32 control_head_ptr;
+ /* Points to the last matched capture block index. */
+ sljit_s32 capture_last_ptr;
+ /* Fast forward skipping byte code pointer. */
+ PCRE2_SPTR fast_forward_bc_ptr;
+ /* Locals used by fast fail optimization. */
+ sljit_s32 fast_fail_start_ptr;
+ sljit_s32 fast_fail_end_ptr;
+
+ /* Flipped and lower case tables. */
+ const sljit_u8 *fcc;
+ sljit_sw lcc;
+ /* Mode can be PCRE2_JIT_COMPLETE and others. */
+ int mode;
+ /* TRUE, when minlength is greater than 0. */
+ BOOL might_be_empty;
+ /* \K is found in the pattern. */
+ BOOL has_set_som;
+ /* (*SKIP:arg) is found in the pattern. */
+ BOOL has_skip_arg;
+ /* (*THEN) is found in the pattern. */
+ BOOL has_then;
+ /* (*SKIP) or (*SKIP:arg) is found in lookbehind assertion. */
+ BOOL has_skip_in_assert_back;
+ /* Quit is redirected by recurse, negative assertion, or positive assertion in conditional block. */
+ BOOL local_quit_available;
+ /* Currently in a positive assertion. */
+ BOOL in_positive_assertion;
+ /* Newline control. */
+ int nltype;
+ sljit_u32 nlmax;
+ sljit_u32 nlmin;
+ int newline;
+ int bsr_nltype;
+ sljit_u32 bsr_nlmax;
+ sljit_u32 bsr_nlmin;
+ /* Dollar endonly. */
+ int endonly;
+ /* Tables. */
+ sljit_sw ctypes;
+ /* Named capturing brackets. */
+ PCRE2_SPTR name_table;
+ sljit_sw name_count;
+ sljit_sw name_entry_size;
+
+ /* Labels and jump lists. */
+ struct sljit_label *partialmatchlabel;
+ struct sljit_label *quit_label;
+ struct sljit_label *abort_label;
+ struct sljit_label *accept_label;
+ struct sljit_label *ff_newline_shortcut;
+ stub_list *stubs;
+ label_addr_list *label_addrs;
+ recurse_entry *entries;
+ recurse_entry *currententry;
+ jump_list *partialmatch;
+ jump_list *quit;
+ jump_list *positive_assertion_quit;
+ jump_list *abort;
+ jump_list *failed_match;
+ jump_list *accept;
+ jump_list *calllimit;
+ jump_list *stackalloc;
+ jump_list *revertframes;
+ jump_list *wordboundary;
+ jump_list *anynewline;
+ jump_list *hspace;
+ jump_list *vspace;
+ jump_list *casefulcmp;
+ jump_list *caselesscmp;
+ jump_list *reset_match;
+ BOOL unset_backref;
+ BOOL alt_circumflex;
+#ifdef SUPPORT_UNICODE
+ BOOL utf;
+ BOOL use_ucp;
+ jump_list *getucd;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ jump_list *utfreadchar;
+ jump_list *utfreadchar16;
+ jump_list *utfreadtype8;
+#endif
+#endif /* SUPPORT_UNICODE */
+} compiler_common;
+
+/* For byte_sequence_compare. */
+
+typedef struct compare_context {
+ int length;
+ int sourcereg;
+#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
+ int ucharptr;
+ union {
+ sljit_s32 asint;
+ sljit_u16 asushort;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ sljit_u8 asbyte;
+ sljit_u8 asuchars[4];
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ sljit_u16 asuchars[2];
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ sljit_u32 asuchars[1];
+#endif
+ } c;
+ union {
+ sljit_s32 asint;
+ sljit_u16 asushort;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ sljit_u8 asbyte;
+ sljit_u8 asuchars[4];
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ sljit_u16 asuchars[2];
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ sljit_u32 asuchars[1];
+#endif
+ } oc;
+#endif
+} compare_context;
+
+/* Undefine sljit macros. */
+#undef CMP
+
+/* Used for accessing the elements of the stack. */
+#define STACK(i) ((i) * (int)sizeof(sljit_sw))
+
+#ifdef SLJIT_PREF_SHIFT_REG
+#if SLJIT_PREF_SHIFT_REG == SLJIT_R2
+/* Nothing. */
+#elif SLJIT_PREF_SHIFT_REG == SLJIT_R3
+#define SHIFT_REG_IS_R3
+#else
+#error "Unsupported shift register"
+#endif
+#endif
+
+#define TMP1 SLJIT_R0
+#ifdef SHIFT_REG_IS_R3
+#define TMP2 SLJIT_R3
+#define TMP3 SLJIT_R2
+#else
+#define TMP2 SLJIT_R2
+#define TMP3 SLJIT_R3
+#endif
+#define STR_PTR SLJIT_R1
+#define STR_END SLJIT_S0
+#define STACK_TOP SLJIT_S1
+#define STACK_LIMIT SLJIT_S2
+#define COUNT_MATCH SLJIT_S3
+#define ARGUMENTS SLJIT_S4
+#define RETURN_ADDR SLJIT_R4
+
+/* Local space layout. */
+/* These two locals can be used by the current opcode. */
+#define LOCALS0 (0 * sizeof(sljit_sw))
+#define LOCALS1 (1 * sizeof(sljit_sw))
+/* Two local variables for possessive quantifiers (char1 cannot use them). */
+#define POSSESSIVE0 (2 * sizeof(sljit_sw))
+#define POSSESSIVE1 (3 * sizeof(sljit_sw))
+/* Max limit of recursions. */
+#define LIMIT_MATCH (4 * sizeof(sljit_sw))
+/* The output vector is stored on the stack, and contains pointers
+to characters. The vector data is divided into two groups: the first
+group contains the start / end character pointers, and the second is
+the start pointers when the end of the capturing group has not yet reached. */
+#define OVECTOR_START (common->ovector_start)
+#define OVECTOR(i) (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw))
+#define OVECTOR_PRIV(i) (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw))
+#define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define MOV_UCHAR SLJIT_MOV_U8
+#define IN_UCHARS(x) (x)
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+#define MOV_UCHAR SLJIT_MOV_U16
+#define UCHAR_SHIFT (1)
+#define IN_UCHARS(x) ((x) * 2)
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+#define MOV_UCHAR SLJIT_MOV_U32
+#define UCHAR_SHIFT (2)
+#define IN_UCHARS(x) ((x) * 4)
+#else
+#error Unsupported compiling mode
+#endif
+
+/* Shortcuts. */
+#define DEFINE_COMPILER \
+ struct sljit_compiler *compiler = common->compiler
+#define OP1(op, dst, dstw, src, srcw) \
+ sljit_emit_op1(compiler, (op), (dst), (dstw), (src), (srcw))
+#define OP2(op, dst, dstw, src1, src1w, src2, src2w) \
+ sljit_emit_op2(compiler, (op), (dst), (dstw), (src1), (src1w), (src2), (src2w))
+#define LABEL() \
+ sljit_emit_label(compiler)
+#define JUMP(type) \
+ sljit_emit_jump(compiler, (type))
+#define JUMPTO(type, label) \
+ sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
+#define JUMPHERE(jump) \
+ sljit_set_label((jump), sljit_emit_label(compiler))
+#define SET_LABEL(jump, label) \
+ sljit_set_label((jump), (label))
+#define CMP(type, src1, src1w, src2, src2w) \
+ sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
+#define CMPTO(type, src1, src1w, src2, src2w, label) \
+ sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label))
+#define OP_FLAGS(op, dst, dstw, type) \
+ sljit_emit_op_flags(compiler, (op), (dst), (dstw), (type))
+#define CMOV(type, dst_reg, src, srcw) \
+ sljit_emit_cmov(compiler, (type), (dst_reg), (src), (srcw))
+#define GET_LOCAL_BASE(dst, dstw, offset) \
+ sljit_get_local_base(compiler, (dst), (dstw), (offset))
+
+#define READ_CHAR_MAX 0x7fffffff
+
+#define INVALID_UTF_CHAR 888
+
+static PCRE2_SPTR bracketend(PCRE2_SPTR cc)
+{
+SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
+do cc += GET(cc, 1); while (*cc == OP_ALT);
+SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
+cc += 1 + LINK_SIZE;
+return cc;
+}
+
+static int no_alternatives(PCRE2_SPTR cc)
+{
+int count = 0;
+SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND));
+do
+ {
+ cc += GET(cc, 1);
+ count++;
+ }
+while (*cc == OP_ALT);
+SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS);
+return count;
+}
+
+/* Functions whose might need modification for all new supported opcodes:
+ next_opcode
+ check_opcode_types
+ set_private_data_ptrs
+ get_framesize
+ init_frame
+ get_recurse_data_length
+ copy_recurse_data
+ compile_matchingpath
+ compile_backtrackingpath
+*/
+
+static PCRE2_SPTR next_opcode(compiler_common *common, PCRE2_SPTR cc)
+{
+SLJIT_UNUSED_ARG(common);
+switch(*cc)
+ {
+ case OP_SOD:
+ case OP_SOM:
+ case OP_SET_SOM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+ case OP_NOTPROP:
+ case OP_PROP:
+ case OP_ANYNL:
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ case OP_EXTUNI:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSPLUS:
+ case OP_CRPOSQUERY:
+ case OP_CRPOSRANGE:
+ case OP_CLASS:
+ case OP_NCLASS:
+ case OP_REF:
+ case OP_REFI:
+ case OP_DNREF:
+ case OP_DNREFI:
+ case OP_RECURSE:
+ case OP_CALLOUT:
+ case OP_ALT:
+ case OP_KET:
+ case OP_KETRMAX:
+ case OP_KETRMIN:
+ case OP_KETRPOS:
+ case OP_REVERSE:
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_BRAPOS:
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ case OP_COND:
+ case OP_SBRA:
+ case OP_SBRAPOS:
+ case OP_SCBRA:
+ case OP_SCBRAPOS:
+ case OP_SCOND:
+ case OP_CREF:
+ case OP_DNCREF:
+ case OP_RREF:
+ case OP_DNRREF:
+ case OP_FALSE:
+ case OP_TRUE:
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+ case OP_BRAPOSZERO:
+ case OP_PRUNE:
+ case OP_SKIP:
+ case OP_THEN:
+ case OP_COMMIT:
+ case OP_FAIL:
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ case OP_CLOSE:
+ case OP_SKIPZERO:
+ return cc + PRIV(OP_lengths)[*cc];
+
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+ cc += PRIV(OP_lengths)[*cc];
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ return cc;
+
+ /* Special cases. */
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ case OP_TYPEPOSUPTO:
+ return cc + PRIV(OP_lengths)[*cc] - 1;
+
+ case OP_ANYBYTE:
+#ifdef SUPPORT_UNICODE
+ if (common->utf) return NULL;
+#endif
+ return cc + 1;
+
+ case OP_CALLOUT_STR:
+ return cc + GET(cc, 1 + 2*LINK_SIZE);
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ return cc + GET(cc, 1);
+#endif
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ return cc + 1 + 2 + cc[1];
+
+ default:
+ /* All opcodes are supported now! */
+ SLJIT_UNREACHABLE();
+ return NULL;
+ }
+}
+
+static BOOL check_opcode_types(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend)
+{
+int count;
+PCRE2_SPTR slot;
+PCRE2_SPTR assert_back_end = cc - 1;
+
+/* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
+while (cc < ccend)
+ {
+ switch(*cc)
+ {
+ case OP_SET_SOM:
+ common->has_set_som = TRUE;
+ common->might_be_empty = TRUE;
+ cc += 1;
+ break;
+
+ case OP_REF:
+ case OP_REFI:
+ common->optimized_cbracket[GET2(cc, 1)] = 0;
+ cc += 1 + IMM2_SIZE;
+ break;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_COND:
+ case OP_SCOND:
+ /* Only AUTO_CALLOUT can insert this opcode. We do
+ not intend to support this case. */
+ if (cc[1 + LINK_SIZE] == OP_CALLOUT || cc[1 + LINK_SIZE] == OP_CALLOUT_STR)
+ return FALSE;
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CREF:
+ common->optimized_cbracket[GET2(cc, 1)] = 0;
+ cc += 1 + IMM2_SIZE;
+ break;
+
+ case OP_DNREF:
+ case OP_DNREFI:
+ case OP_DNCREF:
+ count = GET2(cc, 1 + IMM2_SIZE);
+ slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
+ while (count-- > 0)
+ {
+ common->optimized_cbracket[GET2(slot, 0)] = 0;
+ slot += common->name_entry_size;
+ }
+ cc += 1 + 2 * IMM2_SIZE;
+ break;
+
+ case OP_RECURSE:
+ /* Set its value only once. */
+ if (common->recursive_head_ptr == 0)
+ {
+ common->recursive_head_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+ if (common->capture_last_ptr == 0)
+ {
+ common->capture_last_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+ cc += (*cc == OP_CALLOUT) ? PRIV(OP_lengths)[OP_CALLOUT] : GET(cc, 1 + 2*LINK_SIZE);
+ break;
+
+ case OP_ASSERTBACK:
+ slot = bracketend(cc);
+ if (slot > assert_back_end)
+ assert_back_end = slot;
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_THEN_ARG:
+ common->has_then = TRUE;
+ common->control_head_ptr = 1;
+ /* Fall through. */
+
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_MARK:
+ if (common->mark_ptr == 0)
+ {
+ common->mark_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_THEN:
+ common->has_then = TRUE;
+ common->control_head_ptr = 1;
+ cc += 1;
+ break;
+
+ case OP_SKIP:
+ if (cc < assert_back_end)
+ common->has_skip_in_assert_back = TRUE;
+ cc += 1;
+ break;
+
+ case OP_SKIP_ARG:
+ common->control_head_ptr = 1;
+ common->has_skip_arg = TRUE;
+ if (cc < assert_back_end)
+ common->has_skip_in_assert_back = TRUE;
+ cc += 1 + 2 + cc[1];
+ break;
+
+ default:
+ cc = next_opcode(common, cc);
+ if (cc == NULL)
+ return FALSE;
+ break;
+ }
+ }
+return TRUE;
+}
+
+static BOOL is_accelerated_repeat(PCRE2_SPTR cc)
+{
+switch(*cc)
+ {
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ return (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI);
+
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ return TRUE;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ cc += (*cc == OP_XCLASS) ? GET(cc, 1) : (int)(1 + (32 / sizeof(PCRE2_UCHAR)));
+#else
+ cc += (1 + (32 / sizeof(PCRE2_UCHAR)));
+#endif
+
+ switch(*cc)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSPLUS:
+ return TRUE;
+ }
+ break;
+ }
+return FALSE;
+}
+
+static SLJIT_INLINE BOOL detect_fast_forward_skip(compiler_common *common, int *private_data_start)
+{
+PCRE2_SPTR cc = common->start;
+PCRE2_SPTR end;
+
+/* Skip not repeated brackets. */
+while (TRUE)
+ {
+ switch(*cc)
+ {
+ case OP_SOD:
+ case OP_SOM:
+ case OP_SET_SOM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ /* Zero width assertions. */
+ cc++;
+ continue;
+ }
+
+ if (*cc != OP_BRA && *cc != OP_CBRA)
+ break;
+
+ end = cc + GET(cc, 1);
+ if (*end != OP_KET || PRIVATE_DATA(end) != 0)
+ return FALSE;
+ if (*cc == OP_CBRA)
+ {
+ if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
+ return FALSE;
+ cc += IMM2_SIZE;
+ }
+ cc += 1 + LINK_SIZE;
+ }
+
+if (is_accelerated_repeat(cc))
+ {
+ common->fast_forward_bc_ptr = cc;
+ common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start;
+ *private_data_start += sizeof(sljit_sw);
+ return TRUE;
+ }
+return FALSE;
+}
+
+static SLJIT_INLINE void detect_fast_fail(compiler_common *common, PCRE2_SPTR cc, int *private_data_start, sljit_s32 depth)
+{
+ PCRE2_SPTR next_alt;
+
+ SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA);
+
+ if (*cc == OP_CBRA && common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
+ return;
+
+ next_alt = bracketend(cc) - (1 + LINK_SIZE);
+ if (*next_alt != OP_KET || PRIVATE_DATA(next_alt) != 0)
+ return;
+
+ do
+ {
+ next_alt = cc + GET(cc, 1);
+
+ cc += 1 + LINK_SIZE + ((*cc == OP_CBRA) ? IMM2_SIZE : 0);
+
+ while (TRUE)
+ {
+ switch(*cc)
+ {
+ case OP_SOD:
+ case OP_SOM:
+ case OP_SET_SOM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ /* Zero width assertions. */
+ cc++;
+ continue;
+ }
+ break;
+ }
+
+ if (depth > 0 && (*cc == OP_BRA || *cc == OP_CBRA))
+ detect_fast_fail(common, cc, private_data_start, depth - 1);
+
+ if (is_accelerated_repeat(cc))
+ {
+ common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start;
+
+ if (common->fast_fail_start_ptr == 0)
+ common->fast_fail_start_ptr = *private_data_start;
+
+ *private_data_start += sizeof(sljit_sw);
+ common->fast_fail_end_ptr = *private_data_start;
+
+ if (*private_data_start > SLJIT_MAX_LOCAL_SIZE)
+ return;
+ }
+
+ cc = next_alt;
+ }
+ while (*cc == OP_ALT);
+}
+
+static int get_class_iterator_size(PCRE2_SPTR cc)
+{
+sljit_u32 min;
+sljit_u32 max;
+switch(*cc)
+ {
+ case OP_CRSTAR:
+ case OP_CRPLUS:
+ return 2;
+
+ case OP_CRMINSTAR:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ return 1;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ min = GET2(cc, 1);
+ max = GET2(cc, 1 + IMM2_SIZE);
+ if (max == 0)
+ return (*cc == OP_CRRANGE) ? 2 : 1;
+ max -= min;
+ if (max > 2)
+ max = 2;
+ return max;
+
+ default:
+ return 0;
+ }
+}
+
+static BOOL detect_repeat(compiler_common *common, PCRE2_SPTR begin)
+{
+PCRE2_SPTR end = bracketend(begin);
+PCRE2_SPTR next;
+PCRE2_SPTR next_end;
+PCRE2_SPTR max_end;
+PCRE2_UCHAR type;
+sljit_sw length = end - begin;
+sljit_s32 min, max, i;
+
+/* Detect fixed iterations first. */
+if (end[-(1 + LINK_SIZE)] != OP_KET)
+ return FALSE;
+
+/* Already detected repeat. */
+if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0)
+ return TRUE;
+
+next = end;
+min = 1;
+while (1)
+ {
+ if (*next != *begin)
+ break;
+ next_end = bracketend(next);
+ if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0)
+ break;
+ next = next_end;
+ min++;
+ }
+
+if (min == 2)
+ return FALSE;
+
+max = 0;
+max_end = next;
+if (*next == OP_BRAZERO || *next == OP_BRAMINZERO)
+ {
+ type = *next;
+ while (1)
+ {
+ if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin)
+ break;
+ next_end = bracketend(next + 2 + LINK_SIZE);
+ if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0)
+ break;
+ next = next_end;
+ max++;
+ }
+
+ if (next[0] == type && next[1] == *begin && max >= 1)
+ {
+ next_end = bracketend(next + 1);
+ if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0)
+ {
+ for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE)
+ if (*next_end != OP_KET)
+ break;
+
+ if (i == max)
+ {
+ common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end;
+ common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO;
+ /* +2 the original and the last. */
+ common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2;
+ if (min == 1)
+ return TRUE;
+ min--;
+ max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE);
+ }
+ }
+ }
+ }
+
+if (min >= 3)
+ {
+ common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end;
+ common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT;
+ common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min;
+ return TRUE;
+ }
+
+return FALSE;
+}
+
+#define CASE_ITERATOR_PRIVATE_DATA_1 \
+ case OP_MINSTAR: \
+ case OP_MINPLUS: \
+ case OP_QUERY: \
+ case OP_MINQUERY: \
+ case OP_MINSTARI: \
+ case OP_MINPLUSI: \
+ case OP_QUERYI: \
+ case OP_MINQUERYI: \
+ case OP_NOTMINSTAR: \
+ case OP_NOTMINPLUS: \
+ case OP_NOTQUERY: \
+ case OP_NOTMINQUERY: \
+ case OP_NOTMINSTARI: \
+ case OP_NOTMINPLUSI: \
+ case OP_NOTQUERYI: \
+ case OP_NOTMINQUERYI:
+
+#define CASE_ITERATOR_PRIVATE_DATA_2A \
+ case OP_STAR: \
+ case OP_PLUS: \
+ case OP_STARI: \
+ case OP_PLUSI: \
+ case OP_NOTSTAR: \
+ case OP_NOTPLUS: \
+ case OP_NOTSTARI: \
+ case OP_NOTPLUSI:
+
+#define CASE_ITERATOR_PRIVATE_DATA_2B \
+ case OP_UPTO: \
+ case OP_MINUPTO: \
+ case OP_UPTOI: \
+ case OP_MINUPTOI: \
+ case OP_NOTUPTO: \
+ case OP_NOTMINUPTO: \
+ case OP_NOTUPTOI: \
+ case OP_NOTMINUPTOI:
+
+#define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
+ case OP_TYPEMINSTAR: \
+ case OP_TYPEMINPLUS: \
+ case OP_TYPEQUERY: \
+ case OP_TYPEMINQUERY:
+
+#define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
+ case OP_TYPESTAR: \
+ case OP_TYPEPLUS:
+
+#define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
+ case OP_TYPEUPTO: \
+ case OP_TYPEMINUPTO:
+
+static void set_private_data_ptrs(compiler_common *common, int *private_data_start, PCRE2_SPTR ccend)
+{
+PCRE2_SPTR cc = common->start;
+PCRE2_SPTR alternative;
+PCRE2_SPTR end = NULL;
+int private_data_ptr = *private_data_start;
+int space, size, bracketlen;
+BOOL repeat_check = TRUE;
+
+while (cc < ccend)
+ {
+ space = 0;
+ size = 0;
+ bracketlen = 0;
+ if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE)
+ break;
+
+ if (repeat_check && (*cc == OP_ONCE || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND))
+ {
+ if (detect_repeat(common, cc))
+ {
+ /* These brackets are converted to repeats, so no global
+ based single character repeat is allowed. */
+ if (cc >= end)
+ end = bracketend(cc);
+ }
+ }
+ repeat_check = TRUE;
+
+ switch(*cc)
+ {
+ case OP_KET:
+ if (common->private_data_ptrs[cc + 1 - common->start] != 0)
+ {
+ common->private_data_ptrs[cc - common->start] = private_data_ptr;
+ private_data_ptr += sizeof(sljit_sw);
+ cc += common->private_data_ptrs[cc + 1 - common->start];
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_BRAPOS:
+ case OP_SBRA:
+ case OP_SBRAPOS:
+ case OP_SCOND:
+ common->private_data_ptrs[cc - common->start] = private_data_ptr;
+ private_data_ptr += sizeof(sljit_sw);
+ bracketlen = 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ common->private_data_ptrs[cc - common->start] = private_data_ptr;
+ private_data_ptr += sizeof(sljit_sw);
+ bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_COND:
+ /* Might be a hidden SCOND. */
+ alternative = cc + GET(cc, 1);
+ if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
+ {
+ common->private_data_ptrs[cc - common->start] = private_data_ptr;
+ private_data_ptr += sizeof(sljit_sw);
+ }
+ bracketlen = 1 + LINK_SIZE;
+ break;
+
+ case OP_BRA:
+ bracketlen = 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRA:
+ case OP_SCBRA:
+ bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+ case OP_BRAPOSZERO:
+ repeat_check = FALSE;
+ size = 1;
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_1
+ space = 1;
+ size = -2;
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2A
+ space = 2;
+ size = -2;
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2B
+ space = 2;
+ size = -(2 + IMM2_SIZE);
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_1
+ space = 1;
+ size = 1;
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
+ if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
+ space = 2;
+ size = 1;
+ break;
+
+ case OP_TYPEUPTO:
+ if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
+ space = 2;
+ size = 1 + IMM2_SIZE;
+ break;
+
+ case OP_TYPEMINUPTO:
+ space = 2;
+ size = 1 + IMM2_SIZE;
+ break;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ space = get_class_iterator_size(cc + size);
+ size = 1 + 32 / sizeof(PCRE2_UCHAR);
+ break;
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ space = get_class_iterator_size(cc + size);
+ size = GET(cc, 1);
+ break;
+#endif
+
+ default:
+ cc = next_opcode(common, cc);
+ SLJIT_ASSERT(cc != NULL);
+ break;
+ }
+
+ /* Character iterators, which are not inside a repeated bracket,
+ gets a private slot instead of allocating it on the stack. */
+ if (space > 0 && cc >= end)
+ {
+ common->private_data_ptrs[cc - common->start] = private_data_ptr;
+ private_data_ptr += sizeof(sljit_sw) * space;
+ }
+
+ if (size != 0)
+ {
+ if (size < 0)
+ {
+ cc += -size;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ }
+ else
+ cc += size;
+ }
+
+ if (bracketlen > 0)
+ {
+ if (cc >= end)
+ {
+ end = bracketend(cc);
+ if (end[-1 - LINK_SIZE] == OP_KET)
+ end = NULL;
+ }
+ cc += bracketlen;
+ }
+ }
+*private_data_start = private_data_ptr;
+}
+
+/* Returns with a frame_types (always < 0) if no need for frame. */
+static int get_framesize(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, BOOL recursive, BOOL *needs_control_head)
+{
+int length = 0;
+int possessive = 0;
+BOOL stack_restore = FALSE;
+BOOL setsom_found = recursive;
+BOOL setmark_found = recursive;
+/* The last capture is a local variable even for recursions. */
+BOOL capture_last_found = FALSE;
+
+#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
+SLJIT_ASSERT(common->control_head_ptr != 0);
+*needs_control_head = TRUE;
+#else
+*needs_control_head = FALSE;
+#endif
+
+if (ccend == NULL)
+ {
+ ccend = bracketend(cc) - (1 + LINK_SIZE);
+ if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
+ {
+ possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
+ /* This is correct regardless of common->capture_last_ptr. */
+ capture_last_found = TRUE;
+ }
+ cc = next_opcode(common, cc);
+ }
+
+SLJIT_ASSERT(cc != NULL);
+while (cc < ccend)
+ switch(*cc)
+ {
+ case OP_SET_SOM:
+ SLJIT_ASSERT(common->has_set_som);
+ stack_restore = TRUE;
+ if (!setsom_found)
+ {
+ length += 2;
+ setsom_found = TRUE;
+ }
+ cc += 1;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_THEN_ARG:
+ SLJIT_ASSERT(common->mark_ptr != 0);
+ stack_restore = TRUE;
+ if (!setmark_found)
+ {
+ length += 2;
+ setmark_found = TRUE;
+ }
+ if (common->control_head_ptr != 0)
+ *needs_control_head = TRUE;
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_RECURSE:
+ stack_restore = TRUE;
+ if (common->has_set_som && !setsom_found)
+ {
+ length += 2;
+ setsom_found = TRUE;
+ }
+ if (common->mark_ptr != 0 && !setmark_found)
+ {
+ length += 2;
+ setmark_found = TRUE;
+ }
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ length += 2;
+ capture_last_found = TRUE;
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRA:
+ case OP_SCBRAPOS:
+ stack_restore = TRUE;
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ length += 2;
+ capture_last_found = TRUE;
+ }
+ length += 3;
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_THEN:
+ stack_restore = TRUE;
+ if (common->control_head_ptr != 0)
+ *needs_control_head = TRUE;
+ cc ++;
+ break;
+
+ default:
+ stack_restore = TRUE;
+ /* Fall through. */
+
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+ case OP_ANYBYTE:
+ case OP_NOTPROP:
+ case OP_PROP:
+ case OP_ANYNL:
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ case OP_EXTUNI:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ case OP_TYPEPOSUPTO:
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ case OP_XCLASS:
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+
+ cc = next_opcode(common, cc);
+ SLJIT_ASSERT(cc != NULL);
+ break;
+ }
+
+/* Possessive quantifiers can use a special case. */
+if (SLJIT_UNLIKELY(possessive == length))
+ return stack_restore ? no_frame : no_stack;
+
+if (length > 0)
+ return length + 1;
+return stack_restore ? no_frame : no_stack;
+}
+
+static void init_frame(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, int stackpos, int stacktop)
+{
+DEFINE_COMPILER;
+BOOL setsom_found = FALSE;
+BOOL setmark_found = FALSE;
+/* The last capture is a local variable even for recursions. */
+BOOL capture_last_found = FALSE;
+int offset;
+
+/* >= 1 + shortest item size (2) */
+SLJIT_UNUSED_ARG(stacktop);
+SLJIT_ASSERT(stackpos >= stacktop + 2);
+
+stackpos = STACK(stackpos);
+if (ccend == NULL)
+ {
+ ccend = bracketend(cc) - (1 + LINK_SIZE);
+ if (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS)
+ cc = next_opcode(common, cc);
+ }
+
+SLJIT_ASSERT(cc != NULL);
+while (cc < ccend)
+ switch(*cc)
+ {
+ case OP_SET_SOM:
+ SLJIT_ASSERT(common->has_set_som);
+ if (!setsom_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ setsom_found = TRUE;
+ }
+ cc += 1;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_THEN_ARG:
+ SLJIT_ASSERT(common->mark_ptr != 0);
+ if (!setmark_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ setmark_found = TRUE;
+ }
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_RECURSE:
+ if (common->has_set_som && !setsom_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ setsom_found = TRUE;
+ }
+ if (common->mark_ptr != 0 && !setmark_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ setmark_found = TRUE;
+ }
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ capture_last_found = TRUE;
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRA:
+ case OP_SCBRAPOS:
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ capture_last_found = TRUE;
+ }
+ offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
+ stackpos -= (int)sizeof(sljit_sw);
+
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ default:
+ cc = next_opcode(common, cc);
+ SLJIT_ASSERT(cc != NULL);
+ break;
+ }
+
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
+SLJIT_ASSERT(stackpos == STACK(stacktop));
+}
+
+#define RECURSE_TMP_REG_COUNT 3
+
+typedef struct delayed_mem_copy_status {
+ struct sljit_compiler *compiler;
+ int store_bases[RECURSE_TMP_REG_COUNT];
+ int store_offsets[RECURSE_TMP_REG_COUNT];
+ int tmp_regs[RECURSE_TMP_REG_COUNT];
+ int saved_tmp_regs[RECURSE_TMP_REG_COUNT];
+ int next_tmp_reg;
+} delayed_mem_copy_status;
+
+static void delayed_mem_copy_init(delayed_mem_copy_status *status, compiler_common *common)
+{
+int i;
+
+for (i = 0; i < RECURSE_TMP_REG_COUNT; i++)
+ {
+ SLJIT_ASSERT(status->tmp_regs[i] >= 0);
+ SLJIT_ASSERT(sljit_get_register_index(status->saved_tmp_regs[i]) < 0 || status->tmp_regs[i] == status->saved_tmp_regs[i]);
+
+ status->store_bases[i] = -1;
+ }
+status->next_tmp_reg = 0;
+status->compiler = common->compiler;
+}
+
+static void delayed_mem_copy_move(delayed_mem_copy_status *status, int load_base, sljit_sw load_offset,
+ int store_base, sljit_sw store_offset)
+{
+struct sljit_compiler *compiler = status->compiler;
+int next_tmp_reg = status->next_tmp_reg;
+int tmp_reg = status->tmp_regs[next_tmp_reg];
+
+SLJIT_ASSERT(load_base > 0 && store_base > 0);
+
+if (status->store_bases[next_tmp_reg] == -1)
+ {
+ /* Preserve virtual registers. */
+ if (sljit_get_register_index(status->saved_tmp_regs[next_tmp_reg]) < 0)
+ OP1(SLJIT_MOV, status->saved_tmp_regs[next_tmp_reg], 0, tmp_reg, 0);
+ }
+else
+ OP1(SLJIT_MOV, SLJIT_MEM1(status->store_bases[next_tmp_reg]), status->store_offsets[next_tmp_reg], tmp_reg, 0);
+
+OP1(SLJIT_MOV, tmp_reg, 0, SLJIT_MEM1(load_base), load_offset);
+status->store_bases[next_tmp_reg] = store_base;
+status->store_offsets[next_tmp_reg] = store_offset;
+
+status->next_tmp_reg = (next_tmp_reg + 1) % RECURSE_TMP_REG_COUNT;
+}
+
+static void delayed_mem_copy_finish(delayed_mem_copy_status *status)
+{
+struct sljit_compiler *compiler = status->compiler;
+int next_tmp_reg = status->next_tmp_reg;
+int tmp_reg, saved_tmp_reg, i;
+
+for (i = 0; i < RECURSE_TMP_REG_COUNT; i++)
+ {
+ if (status->store_bases[next_tmp_reg] != -1)
+ {
+ tmp_reg = status->tmp_regs[next_tmp_reg];
+ saved_tmp_reg = status->saved_tmp_regs[next_tmp_reg];
+
+ OP1(SLJIT_MOV, SLJIT_MEM1(status->store_bases[next_tmp_reg]), status->store_offsets[next_tmp_reg], tmp_reg, 0);
+
+ /* Restore virtual registers. */
+ if (sljit_get_register_index(saved_tmp_reg) < 0)
+ OP1(SLJIT_MOV, tmp_reg, 0, saved_tmp_reg, 0);
+ }
+
+ next_tmp_reg = (next_tmp_reg + 1) % RECURSE_TMP_REG_COUNT;
+ }
+}
+
+#undef RECURSE_TMP_REG_COUNT
+
+static int get_recurse_data_length(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend,
+ BOOL *needs_control_head, BOOL *has_quit, BOOL *has_accept)
+{
+int length = 1;
+int size;
+PCRE2_SPTR alternative;
+BOOL quit_found = FALSE;
+BOOL accept_found = FALSE;
+BOOL setsom_found = FALSE;
+BOOL setmark_found = FALSE;
+BOOL capture_last_found = FALSE;
+BOOL control_head_found = FALSE;
+
+#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
+SLJIT_ASSERT(common->control_head_ptr != 0);
+control_head_found = TRUE;
+#endif
+
+/* Calculate the sum of the private machine words. */
+while (cc < ccend)
+ {
+ size = 0;
+ switch(*cc)
+ {
+ case OP_SET_SOM:
+ SLJIT_ASSERT(common->has_set_som);
+ setsom_found = TRUE;
+ cc += 1;
+ break;
+
+ case OP_RECURSE:
+ if (common->has_set_som)
+ setsom_found = TRUE;
+ if (common->mark_ptr != 0)
+ setmark_found = TRUE;
+ if (common->capture_last_ptr != 0)
+ capture_last_found = TRUE;
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_KET:
+ if (PRIVATE_DATA(cc) != 0)
+ {
+ length++;
+ SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
+ cc += PRIVATE_DATA(cc + 1);
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_BRAPOS:
+ case OP_SBRA:
+ case OP_SBRAPOS:
+ case OP_SCOND:
+ length++;
+ SLJIT_ASSERT(PRIVATE_DATA(cc) != 0);
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRA:
+ case OP_SCBRA:
+ length += 2;
+ if (common->capture_last_ptr != 0)
+ capture_last_found = TRUE;
+ if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
+ length++;
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ length += 2 + 2;
+ if (common->capture_last_ptr != 0)
+ capture_last_found = TRUE;
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_COND:
+ /* Might be a hidden SCOND. */
+ alternative = cc + GET(cc, 1);
+ if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
+ length++;
+ cc += 1 + LINK_SIZE;
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_1
+ if (PRIVATE_DATA(cc) != 0)
+ length++;
+ cc += 2;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2A
+ if (PRIVATE_DATA(cc) != 0)
+ length += 2;
+ cc += 2;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2B
+ if (PRIVATE_DATA(cc) != 0)
+ length += 2;
+ cc += 2 + IMM2_SIZE;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_1
+ if (PRIVATE_DATA(cc) != 0)
+ length++;
+ cc += 1;
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
+ if (PRIVATE_DATA(cc) != 0)
+ length += 2;
+ cc += 1;
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
+ if (PRIVATE_DATA(cc) != 0)
+ length += 2;
+ cc += 1 + IMM2_SIZE;
+ break;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(PCRE2_UCHAR);
+#else
+ size = 1 + 32 / (int)sizeof(PCRE2_UCHAR);
+#endif
+ if (PRIVATE_DATA(cc) != 0)
+ length += get_class_iterator_size(cc + size);
+ cc += size;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_THEN_ARG:
+ SLJIT_ASSERT(common->mark_ptr != 0);
+ if (!setmark_found)
+ setmark_found = TRUE;
+ if (common->control_head_ptr != 0)
+ control_head_found = TRUE;
+ if (*cc != OP_MARK)
+ quit_found = TRUE;
+
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_PRUNE:
+ case OP_SKIP:
+ case OP_COMMIT:
+ quit_found = TRUE;
+ cc++;
+ break;
+
+ case OP_SKIP_ARG:
+ quit_found = TRUE;
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_THEN:
+ SLJIT_ASSERT(common->control_head_ptr != 0);
+ quit_found = TRUE;
+ if (!control_head_found)
+ control_head_found = TRUE;
+ cc++;
+ break;
+
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ accept_found = TRUE;
+ cc++;
+ break;
+
+ default:
+ cc = next_opcode(common, cc);
+ SLJIT_ASSERT(cc != NULL);
+ break;
+ }
+ }
+SLJIT_ASSERT(cc == ccend);
+
+if (control_head_found)
+ length++;
+if (capture_last_found)
+ length++;
+if (quit_found)
+ {
+ if (setsom_found)
+ length++;
+ if (setmark_found)
+ length++;
+ }
+
+*needs_control_head = control_head_found;
+*has_quit = quit_found;
+*has_accept = accept_found;
+return length;
+}
+
+enum copy_recurse_data_types {
+ recurse_copy_from_global,
+ recurse_copy_private_to_global,
+ recurse_copy_shared_to_global,
+ recurse_copy_kept_shared_to_global,
+ recurse_swap_global
+};
+
+static void copy_recurse_data(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend,
+ int type, int stackptr, int stacktop, BOOL has_quit)
+{
+delayed_mem_copy_status status;
+PCRE2_SPTR alternative;
+sljit_sw private_srcw[2];
+sljit_sw shared_srcw[3];
+sljit_sw kept_shared_srcw[2];
+int private_count, shared_count, kept_shared_count;
+int from_sp, base_reg, offset, i;
+BOOL setsom_found = FALSE;
+BOOL setmark_found = FALSE;
+BOOL capture_last_found = FALSE;
+BOOL control_head_found = FALSE;
+
+#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
+SLJIT_ASSERT(common->control_head_ptr != 0);
+control_head_found = TRUE;
+#endif
+
+switch (type)
+ {
+ case recurse_copy_from_global:
+ from_sp = TRUE;
+ base_reg = STACK_TOP;
+ break;
+
+ case recurse_copy_private_to_global:
+ case recurse_copy_shared_to_global:
+ case recurse_copy_kept_shared_to_global:
+ from_sp = FALSE;
+ base_reg = STACK_TOP;
+ break;
+
+ default:
+ SLJIT_ASSERT(type == recurse_swap_global);
+ from_sp = FALSE;
+ base_reg = TMP2;
+ break;
+ }
+
+stackptr = STACK(stackptr);
+stacktop = STACK(stacktop);
+
+status.tmp_regs[0] = TMP1;
+status.saved_tmp_regs[0] = TMP1;
+
+if (base_reg != TMP2)
+ {
+ status.tmp_regs[1] = TMP2;
+ status.saved_tmp_regs[1] = TMP2;
+ }
+else
+ {
+ status.saved_tmp_regs[1] = RETURN_ADDR;
+ if (sljit_get_register_index (RETURN_ADDR) == -1)
+ status.tmp_regs[1] = STR_PTR;
+ else
+ status.tmp_regs[1] = RETURN_ADDR;
+ }
+
+status.saved_tmp_regs[2] = TMP3;
+if (sljit_get_register_index (TMP3) == -1)
+ status.tmp_regs[2] = STR_END;
+else
+ status.tmp_regs[2] = TMP3;
+
+delayed_mem_copy_init(&status, common);
+
+if (type != recurse_copy_shared_to_global && type != recurse_copy_kept_shared_to_global)
+ {
+ SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_private_to_global || type == recurse_swap_global);
+
+ if (!from_sp)
+ delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, common->recursive_head_ptr);
+
+ if (from_sp || type == recurse_swap_global)
+ delayed_mem_copy_move(&status, SLJIT_SP, common->recursive_head_ptr, base_reg, stackptr);
+ }
+
+stackptr += sizeof(sljit_sw);
+
+#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
+if (type != recurse_copy_shared_to_global)
+ {
+ if (!from_sp)
+ delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, common->control_head_ptr);
+
+ if (from_sp || type == recurse_swap_global)
+ delayed_mem_copy_move(&status, SLJIT_SP, common->control_head_ptr, base_reg, stackptr);
+ }
+
+stackptr += sizeof(sljit_sw);
+#endif
+
+while (cc < ccend)
+ {
+ private_count = 0;
+ shared_count = 0;
+ kept_shared_count = 0;
+
+ switch(*cc)
+ {
+ case OP_SET_SOM:
+ SLJIT_ASSERT(common->has_set_som);
+ if (has_quit && !setsom_found)
+ {
+ kept_shared_srcw[0] = OVECTOR(0);
+ kept_shared_count = 1;
+ setsom_found = TRUE;
+ }
+ cc += 1;
+ break;
+
+ case OP_RECURSE:
+ if (has_quit)
+ {
+ if (common->has_set_som && !setsom_found)
+ {
+ kept_shared_srcw[0] = OVECTOR(0);
+ kept_shared_count = 1;
+ setsom_found = TRUE;
+ }
+ if (common->mark_ptr != 0 && !setmark_found)
+ {
+ kept_shared_srcw[kept_shared_count] = common->mark_ptr;
+ kept_shared_count++;
+ setmark_found = TRUE;
+ }
+ }
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ shared_srcw[0] = common->capture_last_ptr;
+ shared_count = 1;
+ capture_last_found = TRUE;
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_KET:
+ if (PRIVATE_DATA(cc) != 0)
+ {
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0);
+ cc += PRIVATE_DATA(cc + 1);
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_BRAPOS:
+ case OP_SBRA:
+ case OP_SBRAPOS:
+ case OP_SCOND:
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ cc += 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRA:
+ case OP_SCBRA:
+ offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
+ shared_srcw[0] = OVECTOR(offset);
+ shared_srcw[1] = OVECTOR(offset + 1);
+ shared_count = 2;
+
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ shared_srcw[2] = common->capture_last_ptr;
+ shared_count = 3;
+ capture_last_found = TRUE;
+ }
+
+ if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
+ {
+ private_count = 1;
+ private_srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
+ }
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
+ shared_srcw[0] = OVECTOR(offset);
+ shared_srcw[1] = OVECTOR(offset + 1);
+ shared_count = 2;
+
+ if (common->capture_last_ptr != 0 && !capture_last_found)
+ {
+ shared_srcw[2] = common->capture_last_ptr;
+ shared_count = 3;
+ capture_last_found = TRUE;
+ }
+
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
+ cc += 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ case OP_COND:
+ /* Might be a hidden SCOND. */
+ alternative = cc + GET(cc, 1);
+ if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
+ {
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ }
+ cc += 1 + LINK_SIZE;
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_1
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ }
+ cc += 2;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2A
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
+ }
+ cc += 2;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_PRIVATE_DATA_2B
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
+ }
+ cc += 2 + IMM2_SIZE;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_1
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ }
+ cc += 1;
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
+ }
+ cc += 1;
+ break;
+
+ CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
+ if (PRIVATE_DATA(cc))
+ {
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
+ }
+ cc += 1 + IMM2_SIZE;
+ break;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ i = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(PCRE2_UCHAR);
+#else
+ i = 1 + 32 / (int)sizeof(PCRE2_UCHAR);
+#endif
+ if (PRIVATE_DATA(cc) != 0)
+ switch(get_class_iterator_size(cc + i))
+ {
+ case 1:
+ private_count = 1;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ break;
+
+ case 2:
+ private_count = 2;
+ private_srcw[0] = PRIVATE_DATA(cc);
+ private_srcw[1] = private_srcw[0] + sizeof(sljit_sw);
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ cc += i;
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_THEN_ARG:
+ SLJIT_ASSERT(common->mark_ptr != 0);
+ if (has_quit && !setmark_found)
+ {
+ kept_shared_srcw[0] = common->mark_ptr;
+ kept_shared_count = 1;
+ setmark_found = TRUE;
+ }
+ if (common->control_head_ptr != 0 && !control_head_found)
+ {
+ shared_srcw[0] = common->control_head_ptr;
+ shared_count = 1;
+ control_head_found = TRUE;
+ }
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_THEN:
+ SLJIT_ASSERT(common->control_head_ptr != 0);
+ if (!control_head_found)
+ {
+ shared_srcw[0] = common->control_head_ptr;
+ shared_count = 1;
+ control_head_found = TRUE;
+ }
+ cc++;
+ break;
+
+ default:
+ cc = next_opcode(common, cc);
+ SLJIT_ASSERT(cc != NULL);
+ break;
+ }
+
+ if (type != recurse_copy_shared_to_global && type != recurse_copy_kept_shared_to_global)
+ {
+ SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_private_to_global || type == recurse_swap_global);
+
+ for (i = 0; i < private_count; i++)
+ {
+ SLJIT_ASSERT(private_srcw[i] != 0);
+
+ if (!from_sp)
+ delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, private_srcw[i]);
+
+ if (from_sp || type == recurse_swap_global)
+ delayed_mem_copy_move(&status, SLJIT_SP, private_srcw[i], base_reg, stackptr);
+
+ stackptr += sizeof(sljit_sw);
+ }
+ }
+ else
+ stackptr += sizeof(sljit_sw) * private_count;
+
+ if (type != recurse_copy_private_to_global && type != recurse_copy_kept_shared_to_global)
+ {
+ SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_shared_to_global || type == recurse_swap_global);
+
+ for (i = 0; i < shared_count; i++)
+ {
+ SLJIT_ASSERT(shared_srcw[i] != 0);
+
+ if (!from_sp)
+ delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, shared_srcw[i]);
+
+ if (from_sp || type == recurse_swap_global)
+ delayed_mem_copy_move(&status, SLJIT_SP, shared_srcw[i], base_reg, stackptr);
+
+ stackptr += sizeof(sljit_sw);
+ }
+ }
+ else
+ stackptr += sizeof(sljit_sw) * shared_count;
+
+ if (type != recurse_copy_private_to_global && type != recurse_swap_global)
+ {
+ SLJIT_ASSERT(type == recurse_copy_from_global || type == recurse_copy_shared_to_global || type == recurse_copy_kept_shared_to_global);
+
+ for (i = 0; i < kept_shared_count; i++)
+ {
+ SLJIT_ASSERT(kept_shared_srcw[i] != 0);
+
+ if (!from_sp)
+ delayed_mem_copy_move(&status, base_reg, stackptr, SLJIT_SP, kept_shared_srcw[i]);
+
+ if (from_sp || type == recurse_swap_global)
+ delayed_mem_copy_move(&status, SLJIT_SP, kept_shared_srcw[i], base_reg, stackptr);
+
+ stackptr += sizeof(sljit_sw);
+ }
+ }
+ else
+ stackptr += sizeof(sljit_sw) * kept_shared_count;
+ }
+
+SLJIT_ASSERT(cc == ccend && stackptr == stacktop);
+
+delayed_mem_copy_finish(&status);
+}
+
+static SLJIT_INLINE PCRE2_SPTR set_then_offsets(compiler_common *common, PCRE2_SPTR cc, sljit_u8 *current_offset)
+{
+PCRE2_SPTR end = bracketend(cc);
+BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT;
+
+/* Assert captures then. */
+if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT)
+ current_offset = NULL;
+/* Conditional block does not. */
+if (*cc == OP_COND || *cc == OP_SCOND)
+ has_alternatives = FALSE;
+
+cc = next_opcode(common, cc);
+if (has_alternatives)
+ current_offset = common->then_offsets + (cc - common->start);
+
+while (cc < end)
+ {
+ if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND))
+ cc = set_then_offsets(common, cc, current_offset);
+ else
+ {
+ if (*cc == OP_ALT && has_alternatives)
+ current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start);
+ if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL)
+ *current_offset = 1;
+ cc = next_opcode(common, cc);
+ }
+ }
+
+return end;
+}
+
+#undef CASE_ITERATOR_PRIVATE_DATA_1
+#undef CASE_ITERATOR_PRIVATE_DATA_2A
+#undef CASE_ITERATOR_PRIVATE_DATA_2B
+#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
+#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
+#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
+
+static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
+{
+return (value & (value - 1)) == 0;
+}
+
+static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label)
+{
+while (list)
+ {
+ /* sljit_set_label is clever enough to do nothing
+ if either the jump or the label is NULL. */
+ SET_LABEL(list->jump, label);
+ list = list->next;
+ }
+}
+
+static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump)
+{
+jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list));
+if (list_item)
+ {
+ list_item->next = *list;
+ list_item->jump = jump;
+ *list = list_item;
+ }
+}
+
+static void add_stub(compiler_common *common, struct sljit_jump *start)
+{
+DEFINE_COMPILER;
+stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
+
+if (list_item)
+ {
+ list_item->start = start;
+ list_item->quit = LABEL();
+ list_item->next = common->stubs;
+ common->stubs = list_item;
+ }
+}
+
+static void flush_stubs(compiler_common *common)
+{
+DEFINE_COMPILER;
+stub_list *list_item = common->stubs;
+
+while (list_item)
+ {
+ JUMPHERE(list_item->start);
+ add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
+ JUMPTO(SLJIT_JUMP, list_item->quit);
+ list_item = list_item->next;
+ }
+common->stubs = NULL;
+}
+
+static void add_label_addr(compiler_common *common, sljit_uw *update_addr)
+{
+DEFINE_COMPILER;
+label_addr_list *label_addr;
+
+label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list));
+if (label_addr == NULL)
+ return;
+label_addr->label = LABEL();
+label_addr->update_addr = update_addr;
+label_addr->next = common->label_addrs;
+common->label_addrs = label_addr;
+}
+
+static SLJIT_INLINE void count_match(compiler_common *common)
+{
+DEFINE_COMPILER;
+
+OP2(SLJIT_SUB | SLJIT_SET_Z, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
+add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
+}
+
+static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
+{
+/* May destroy all locals and registers except TMP2. */
+DEFINE_COMPILER;
+
+SLJIT_ASSERT(size > 0);
+OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
+#ifdef DESTROY_REGISTERS
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
+OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
+OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0);
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
+#endif
+add_stub(common, CMP(SLJIT_LESS, STACK_TOP, 0, STACK_LIMIT, 0));
+}
+
+static SLJIT_INLINE void free_stack(compiler_common *common, int size)
+{
+DEFINE_COMPILER;
+
+SLJIT_ASSERT(size > 0);
+OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
+}
+
+static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size)
+{
+DEFINE_COMPILER;
+sljit_uw *result;
+
+if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+
+result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data);
+if (SLJIT_UNLIKELY(result == NULL))
+ {
+ sljit_set_compiler_memory_error(compiler);
+ return NULL;
+ }
+
+*(void**)result = common->read_only_data_head;
+common->read_only_data_head = (void *)result;
+return result + 1;
+}
+
+static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
+{
+DEFINE_COMPILER;
+struct sljit_label *loop;
+sljit_s32 i;
+
+/* At this point we can freely use all temporary registers. */
+SLJIT_ASSERT(length > 1);
+/* TMP1 returns with begin - 1. */
+OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
+if (length < 8)
+ {
+ for (i = 1; i < length; i++)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0);
+ }
+else
+ {
+ if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw)) == SLJIT_SUCCESS)
+ {
+ GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START);
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
+ loop = LABEL();
+ sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw));
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, loop);
+ }
+ else
+ {
+ GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START + sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1);
+ loop = LABEL();
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R0, 0);
+ OP2(SLJIT_ADD, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, sizeof(sljit_sw));
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, loop);
+ }
+ }
+}
+
+static SLJIT_INLINE void reset_fast_fail(compiler_common *common)
+{
+DEFINE_COMPILER;
+sljit_s32 i;
+
+SLJIT_ASSERT(common->fast_fail_start_ptr < common->fast_fail_end_ptr);
+
+OP2(SLJIT_SUB, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+for (i = common->fast_fail_start_ptr; i < common->fast_fail_end_ptr; i += sizeof(sljit_sw))
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), i, TMP1, 0);
+}
+
+static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
+{
+DEFINE_COMPILER;
+struct sljit_label *loop;
+int i;
+
+SLJIT_ASSERT(length > 1);
+/* OVECTOR(1) contains the "string begin - 1" constant. */
+if (length > 2)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
+if (length < 8)
+ {
+ for (i = 2; i < length; i++)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0);
+ }
+else
+ {
+ if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw)) == SLJIT_SUCCESS)
+ {
+ GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
+ loop = LABEL();
+ sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+ OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, loop);
+ }
+ else
+ {
+ GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + 2 * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
+ loop = LABEL();
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP1, 0);
+ OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, sizeof(sljit_sw));
+ OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, loop);
+ }
+ }
+
+OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
+if (common->mark_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
+if (common->control_head_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, end));
+}
+
+static sljit_sw SLJIT_FUNC do_search_mark(sljit_sw *current, PCRE2_SPTR skip_arg)
+{
+while (current != NULL)
+ {
+ switch (current[1])
+ {
+ case type_then_trap:
+ break;
+
+ case type_mark:
+ if (PRIV(strcmp)(skip_arg, (PCRE2_SPTR)current[2]) == 0)
+ return current[3];
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ SLJIT_ASSERT(current[0] == 0 || current < (sljit_sw*)current[0]);
+ current = (sljit_sw*)current[0];
+ }
+return 0;
+}
+
+static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
+{
+DEFINE_COMPILER;
+struct sljit_label *loop;
+BOOL has_pre;
+
+/* At this point we can freely use all registers. */
+OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0);
+
+OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
+OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
+if (common->mark_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
+OP1(SLJIT_MOV_U32, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, oveccount));
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, startchar_ptr), SLJIT_S0, 0);
+if (common->mark_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0);
+OP2(SLJIT_ADD, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, match_data),
+ SLJIT_IMM, SLJIT_OFFSETOF(pcre2_match_data, ovector) - sizeof(PCRE2_SIZE));
+
+has_pre = sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw)) == SLJIT_SUCCESS;
+
+GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START - (has_pre ? sizeof(sljit_sw) : 0));
+OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin));
+
+loop = LABEL();
+
+if (has_pre)
+ sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw));
+else
+ {
+ OP1(SLJIT_MOV, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0);
+ OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw));
+ }
+
+OP2(SLJIT_ADD, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, sizeof(PCRE2_SIZE));
+OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_R0, 0);
+/* Copy the integer value to the output buffer */
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+
+SLJIT_ASSERT(sizeof(PCRE2_SIZE) == 4 || sizeof(PCRE2_SIZE) == 8);
+OP1(((sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV), SLJIT_MEM1(SLJIT_R2), 0, SLJIT_S1, 0);
+
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
+JUMPTO(SLJIT_NOT_ZERO, loop);
+
+/* Calculate the return value, which is the maximum ovector value. */
+if (topbracket > 1)
+ {
+ if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw))) == SLJIT_SUCCESS)
+ {
+ GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
+
+ /* OVECTOR(0) is never equal to SLJIT_S2. */
+ loop = LABEL();
+ sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw)));
+ OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
+ CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
+ }
+ else
+ {
+ GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + (topbracket - 1) * 2 * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1);
+
+ /* OVECTOR(0) is never equal to SLJIT_S2. */
+ loop = LABEL();
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), 0);
+ OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 2 * (sljit_sw)sizeof(sljit_sw));
+ OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1);
+ CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop);
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0);
+ }
+ }
+else
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
+}
+
+static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
+{
+DEFINE_COMPILER;
+sljit_s32 mov_opcode;
+
+SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S0, str_end_must_be_saved_reg0);
+SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
+ && (common->mode == PCRE2_JIT_PARTIAL_SOFT ? common->hit_start != 0 : common->hit_start == 0));
+
+OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0);
+OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP),
+ common->mode == PCRE2_JIT_PARTIAL_SOFT ? common->hit_start : common->start_ptr);
+OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_PARTIAL);
+
+/* Store match begin and end. */
+OP1(SLJIT_MOV, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin));
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, startchar_ptr), SLJIT_R2, 0);
+OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, match_data));
+
+mov_opcode = (sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV;
+
+OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S1, 0);
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+OP1(mov_opcode, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(pcre2_match_data, ovector), SLJIT_R2, 0);
+
+OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_S1, 0);
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+OP2(SLJIT_ASHR, STR_END, 0, STR_END, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+OP1(mov_opcode, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(pcre2_match_data, ovector) + sizeof(PCRE2_SIZE), STR_END, 0);
+
+JUMPTO(SLJIT_JUMP, quit);
+}
+
+static SLJIT_INLINE void check_start_used_ptr(compiler_common *common)
+{
+/* May destroy TMP1. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ /* The value of -1 must be kept for start_used_ptr! */
+ OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1);
+ /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
+ is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
+ jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+ JUMPHERE(jump);
+ }
+else if (common->mode == PCRE2_JIT_PARTIAL_HARD)
+ {
+ jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+ JUMPHERE(jump);
+ }
+}
+
+static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, PCRE2_SPTR cc)
+{
+/* Detects if the character has an othercase. */
+unsigned int c;
+
+#ifdef SUPPORT_UNICODE
+if (common->utf)
+ {
+ GETCHAR(c, cc);
+ if (c > 127)
+ {
+ return c != UCD_OTHERCASE(c);
+ }
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ return common->fcc[c] != c;
+#endif
+ }
+else
+#endif
+ c = *cc;
+return MAX_255(c) ? common->fcc[c] != c : FALSE;
+}
+
+static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)
+{
+/* Returns with the othercase. */
+#ifdef SUPPORT_UNICODE
+if (common->utf && c > 127)
+ {
+ return UCD_OTHERCASE(c);
+ }
+#endif
+return TABLE_GET(c, common->fcc, c);
+}
+
+static unsigned int char_get_othercase_bit(compiler_common *common, PCRE2_SPTR cc)
+{
+/* Detects if the character and its othercase has only 1 bit difference. */
+unsigned int c, oc, bit;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+int n;
+#endif
+
+#ifdef SUPPORT_UNICODE
+if (common->utf)
+ {
+ GETCHAR(c, cc);
+ if (c <= 127)
+ oc = common->fcc[c];
+ else
+ {
+ oc = UCD_OTHERCASE(c);
+ }
+ }
+else
+ {
+ c = *cc;
+ oc = TABLE_GET(c, common->fcc, c);
+ }
+#else
+c = *cc;
+oc = TABLE_GET(c, common->fcc, c);
+#endif
+
+SLJIT_ASSERT(c != oc);
+
+bit = c ^ oc;
+/* Optimized for English alphabet. */
+if (c <= 127 && bit == 0x20)
+ return (0 << 8) | 0x20;
+
+/* Since c != oc, they must have at least 1 bit difference. */
+if (!is_powerof2(bit))
+ return 0;
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+
+#ifdef SUPPORT_UNICODE
+if (common->utf && c > 127)
+ {
+ n = GET_EXTRALEN(*cc);
+ while ((bit & 0x3f) == 0)
+ {
+ n--;
+ bit >>= 6;
+ }
+ return (n << 8) | bit;
+ }
+#endif /* SUPPORT_UNICODE */
+return (0 << 8) | bit;
+
+#elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+
+#ifdef SUPPORT_UNICODE
+if (common->utf && c > 65535)
+ {
+ if (bit >= (1 << 10))
+ bit >>= 10;
+ else
+ return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8));
+ }
+#endif /* SUPPORT_UNICODE */
+return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
+
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
+}
+
+static void check_partial(compiler_common *common, BOOL force)
+{
+/* Checks whether a partial matching is occurred. Does not modify registers. */
+DEFINE_COMPILER;
+struct sljit_jump *jump = NULL;
+
+SLJIT_ASSERT(!force || common->mode != PCRE2_JIT_COMPLETE);
+
+if (common->mode == PCRE2_JIT_COMPLETE)
+ return;
+
+if (!force)
+ jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+else if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
+ jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
+
+if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
+else
+ {
+ if (common->partialmatchlabel != NULL)
+ JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
+ else
+ add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
+ }
+
+if (jump != NULL)
+ JUMPHERE(jump);
+}
+
+static void check_str_end(compiler_common *common, jump_list **end_reached)
+{
+/* Does not affect registers. Usually used in a tight spot. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+if (common->mode == PCRE2_JIT_COMPLETE)
+ {
+ add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+ return;
+ }
+
+jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
+if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
+ add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
+ }
+else
+ {
+ add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
+ if (common->partialmatchlabel != NULL)
+ JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
+ else
+ add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
+ }
+JUMPHERE(jump);
+}
+
+static void detect_partial_match(compiler_common *common, jump_list **backtracks)
+{
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+if (common->mode == PCRE2_JIT_COMPLETE)
+ {
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+ return;
+ }
+
+/* Partial matching mode. */
+jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
+add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0));
+if (common->mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ }
+else
+ {
+ if (common->partialmatchlabel != NULL)
+ JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
+ else
+ add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
+ }
+JUMPHERE(jump);
+}
+
+static void peek_char(compiler_common *common, sljit_u32 max)
+{
+/* Reads the character into TMP1, keeps STR_PTR.
+Does not check STR_END. TMP2 Destroyed. */
+DEFINE_COMPILER;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+struct sljit_jump *jump;
+#endif
+
+SLJIT_UNUSED_ARG(max);
+
+OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+ if (max < 128) return;
+
+ jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+ JUMPHERE(jump);
+ }
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
+if (common->utf)
+ {
+ if (max < 0xd800) return;
+
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
+ jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
+ /* TMP2 contains the high surrogate. */
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ JUMPHERE(jump);
+ }
+#endif
+}
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+
+static BOOL is_char7_bitset(const sljit_u8 *bitset, BOOL nclass)
+{
+/* Tells whether the character codes below 128 are enough
+to determine a match. */
+const sljit_u8 value = nclass ? 0xff : 0;
+const sljit_u8 *end = bitset + 32;
+
+bitset += 16;
+do
+ {
+ if (*bitset++ != value)
+ return FALSE;
+ }
+while (bitset < end);
+return TRUE;
+}
+
+static void read_char7_type(compiler_common *common, BOOL full_read)
+{
+/* Reads the precise character type of a character into TMP1, if the character
+is less than 128. Otherwise it returns with zero. Does not check STR_END. The
+full_read argument tells whether characters above max are accepted or not. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+SLJIT_ASSERT(common->utf);
+
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
+
+if (full_read)
+ {
+ jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
+ OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+ JUMPHERE(jump);
+ }
+}
+
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
+
+static void read_char_range(compiler_common *common, sljit_u32 min, sljit_u32 max, BOOL update_str_ptr)
+{
+/* Reads the precise value of a character into TMP1, if the character is
+between min and max (c >= min && c <= max). Otherwise it returns with a value
+outside the range. Does not check STR_END. */
+DEFINE_COMPILER;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+struct sljit_jump *jump;
+#endif
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+struct sljit_jump *jump2;
+#endif
+
+SLJIT_UNUSED_ARG(update_str_ptr);
+SLJIT_UNUSED_ARG(min);
+SLJIT_UNUSED_ARG(max);
+SLJIT_ASSERT(min <= max);
+
+OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+ if (max < 128 && !update_str_ptr) return;
+
+ jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
+ if (min >= 0x10000)
+ {
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0);
+ if (update_str_ptr)
+ OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
+ if (!update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ JUMPHERE(jump2);
+ if (update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
+ }
+ else if (min >= 0x800 && max <= 0xffff)
+ {
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0);
+ if (update_str_ptr)
+ OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ if (!update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ JUMPHERE(jump2);
+ if (update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
+ }
+ else if (max >= 0x800)
+ add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
+ else if (max < 128)
+ {
+ OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+ }
+ else
+ {
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ if (!update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ else
+ OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ if (update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0);
+ }
+ JUMPHERE(jump);
+ }
+#endif
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
+if (common->utf)
+ {
+ if (max >= 0x10000)
+ {
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
+ jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
+ /* TMP2 contains the high surrogate. */
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+ JUMPHERE(jump);
+ return;
+ }
+
+ if (max < 0xd800 && !update_str_ptr) return;
+
+ /* Skip low surrogate if necessary. */
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800);
+ jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
+ if (update_str_ptr)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ if (max >= 0xd800)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000);
+ JUMPHERE(jump);
+ }
+#endif
+}
+
+static SLJIT_INLINE void read_char(compiler_common *common)
+{
+read_char_range(common, 0, READ_CHAR_MAX, TRUE);
+}
+
+static void read_char8_type(compiler_common *common, BOOL update_str_ptr)
+{
+/* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */
+DEFINE_COMPILER;
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+struct sljit_jump *jump;
+#endif
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+struct sljit_jump *jump2;
+#endif
+
+SLJIT_UNUSED_ARG(update_str_ptr);
+
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+ /* This can be an extra read in some situations, but hopefully
+ it is needed in most cases. */
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
+ jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
+ if (!update_str_ptr)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+ OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+ jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
+ JUMPHERE(jump2);
+ }
+ else
+ add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
+ JUMPHERE(jump);
+ return;
+ }
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+/* The ctypes array contains only 256 values. */
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255);
+#endif
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+JUMPHERE(jump);
+#endif
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
+if (common->utf && update_str_ptr)
+ {
+ /* Skip low surrogate if necessary. */
+ OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800);
+ jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ JUMPHERE(jump);
+ }
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16 */
+}
+
+static void skip_char_back(compiler_common *common)
+{
+/* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
+DEFINE_COMPILER;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+#if PCRE2_CODE_UNIT_WIDTH == 8
+struct sljit_label *label;
+
+if (common->utf)
+ {
+ label = LABEL();
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
+ CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
+ return;
+ }
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+if (common->utf)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ /* Skip low surrogate if necessary. */
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
+ OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ return;
+ }
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+}
+
+static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch)
+{
+/* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+if (nltype == NLTYPE_ANY)
+ {
+ add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
+ sljit_set_current_flags(compiler, SLJIT_SET_Z);
+ add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO));
+ }
+else if (nltype == NLTYPE_ANYCRLF)
+ {
+ if (jumpifmatch)
+ {
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR));
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
+ }
+ else
+ {
+ jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
+ JUMPHERE(jump);
+ }
+ }
+else
+ {
+ SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
+ add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
+ }
+}
+
+#ifdef SUPPORT_UNICODE
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+static void do_utfreadchar(compiler_common *common)
+{
+/* Fast decoding a UTF-8 character. TMP1 contains the first byte
+of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+
+/* Searching for the first zero. */
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
+jump = JUMP(SLJIT_NOT_ZERO);
+/* Two byte sequence. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+JUMPHERE(jump);
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
+OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000);
+jump = JUMP(SLJIT_NOT_ZERO);
+/* Three byte sequence. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+/* Four byte sequence. */
+JUMPHERE(jump);
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
+OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
+OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4));
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+static void do_utfreadchar16(compiler_common *common)
+{
+/* Fast decoding a UTF-8 character. TMP1 contains the first byte
+of the character (>= 0xc0). Return value in TMP1. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+
+/* Searching for the first zero. */
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800);
+jump = JUMP(SLJIT_NOT_ZERO);
+/* Two byte sequence. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+JUMPHERE(jump);
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400);
+OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_NOT_ZERO);
+/* This code runs only in 8 bit mode. No need to shift the value. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800);
+OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
+/* Three byte sequence. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+static void do_utfreadtype8(compiler_common *common)
+{
+/* Fast decoding a UTF-8 character type. TMP2 contains the first byte
+of the character (>= 0xc0). Return value in TMP1. */
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+struct sljit_jump *compare;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
+jump = JUMP(SLJIT_NOT_ZERO);
+/* Two byte sequence. */
+OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
+/* The upper 5 bits are known at this point. */
+compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3);
+OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
+OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
+OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0);
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+JUMPHERE(compare);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+/* We only have types for characters less than 256. */
+JUMPHERE(jump);
+OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+
+/* UCD_BLOCK_SIZE must be 128 (see the assert below). */
+#define UCD_BLOCK_MASK 127
+#define UCD_BLOCK_SHIFT 7
+
+static void do_getucd(compiler_common *common)
+{
+/* Search the UCD record for the character comes in TMP1.
+Returns chartype in TMP1 and UCD offset in TMP2. */
+DEFINE_COMPILER;
+#if PCRE2_CODE_UNIT_WIDTH == 32
+struct sljit_jump *jump;
+#endif
+
+#if defined SLJIT_DEBUG && SLJIT_DEBUG
+/* dummy_ucd_record */
+const ucd_record *record = GET_UCD(INVALID_UTF_CHAR);
+SLJIT_ASSERT(record->script == ucp_Common && record->chartype == ucp_Cn && record->gbprop == ucp_gbOther);
+SLJIT_ASSERT(record->caseset == 0 && record->other_case == 0);
+#endif
+
+SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+if (!common->utf)
+ {
+ jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, MAX_UTF_CODE_POINT + 1);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR);
+ JUMPHERE(jump);
+ }
+#endif
+
+OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
+OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
+OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
+OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
+OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
+OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
+OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+#endif /* SUPPORT_UNICODE */
+
+static SLJIT_INLINE struct sljit_label *mainloop_entry(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_label *mainloop;
+struct sljit_label *newlinelabel = NULL;
+struct sljit_jump *start;
+struct sljit_jump *end = NULL;
+struct sljit_jump *end2 = NULL;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+struct sljit_jump *singlechar;
+#endif
+jump_list *newline = NULL;
+sljit_u32 overall_options = common->re->overall_options;
+BOOL hascrorlf = (common->re->flags & PCRE2_HASCRORLF) != 0;
+BOOL newlinecheck = FALSE;
+BOOL readuchar = FALSE;
+
+if (!(hascrorlf || (overall_options & PCRE2_FIRSTLINE) != 0)
+ && (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF || common->newline > 255))
+ newlinecheck = TRUE;
+
+SLJIT_ASSERT(common->abort_label == NULL);
+
+if ((overall_options & PCRE2_FIRSTLINE) != 0)
+ {
+ /* Search for the end of the first line. */
+ SLJIT_ASSERT(common->match_end_ptr != 0);
+ OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
+
+ if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ mainloop = LABEL();
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
+ CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
+ JUMPHERE(end);
+ OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+ else
+ {
+ end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ mainloop = LABEL();
+ /* Continual stores does not cause data dependency. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0);
+ read_char_range(common, common->nlmin, common->nlmax, TRUE);
+ check_newlinechar(common, common->nltype, &newline, TRUE);
+ CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop);
+ JUMPHERE(end);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0);
+ set_jumps(newline, LABEL());
+ }
+
+ OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
+ }
+else if ((overall_options & PCRE2_USE_OFFSET_LIMIT) != 0)
+ {
+ /* Check whether offset limit is set and valid. */
+ SLJIT_ASSERT(common->match_end_ptr != 0);
+
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, offset_limit));
+ OP1(SLJIT_MOV, TMP2, 0, STR_END, 0);
+ end = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (sljit_sw) PCRE2_UNSET);
+ OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
+#if PCRE2_CODE_UNIT_WIDTH == 16
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 2);
+#endif
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
+ OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
+ end2 = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0);
+ OP1(SLJIT_MOV, TMP2, 0, STR_END, 0);
+ JUMPHERE(end2);
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
+ add_jump(compiler, &common->abort, CMP(SLJIT_LESS, TMP2, 0, STR_PTR, 0));
+ JUMPHERE(end);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, TMP2, 0);
+ }
+
+start = JUMP(SLJIT_JUMP);
+
+if (newlinecheck)
+ {
+ newlinelabel = LABEL();
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff);
+ OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ end2 = JUMP(SLJIT_JUMP);
+ }
+
+mainloop = LABEL();
+
+/* Increasing the STR_PTR here requires one less jump in the most common case. */
+#ifdef SUPPORT_UNICODE
+if (common->utf) readuchar = TRUE;
+#endif
+if (newlinecheck) readuchar = TRUE;
+
+if (readuchar)
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+
+if (newlinecheck)
+ CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
+
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+ singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ JUMPHERE(singlechar);
+ }
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+if (common->utf)
+ {
+ singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
+ OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ JUMPHERE(singlechar);
+ }
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32 */
+JUMPHERE(start);
+
+if (newlinecheck)
+ {
+ JUMPHERE(end);
+ JUMPHERE(end2);
+ }
+
+return mainloop;
+}
+
+
+static SLJIT_INLINE void add_prefix_char(PCRE2_UCHAR chr, fast_forward_char_data *chars, BOOL last)
+{
+sljit_u32 i, count = chars->count;
+
+if (count == 255)
+ return;
+
+if (count == 0)
+ {
+ chars->count = 1;
+ chars->chars[0] = chr;
+
+ if (last)
+ chars->last_count = 1;
+ return;
+ }
+
+for (i = 0; i < count; i++)
+ if (chars->chars[i] == chr)
+ return;
+
+if (count >= MAX_DIFF_CHARS)
+ {
+ chars->count = 255;
+ return;
+ }
+
+chars->chars[count] = chr;
+chars->count = count + 1;
+
+if (last)
+ chars->last_count++;
+}
+
+static int scan_prefix(compiler_common *common, PCRE2_SPTR cc, fast_forward_char_data *chars, int max_chars, sljit_u32 *rec_count)
+{
+/* Recursive function, which scans prefix literals. */
+BOOL last, any, class, caseless;
+int len, repeat, len_save, consumed = 0;
+sljit_u32 chr; /* Any unicode character. */
+sljit_u8 *bytes, *bytes_end, byte;
+PCRE2_SPTR alternative, cc_save, oc;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+PCRE2_UCHAR othercase[4];
+#elif defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 16
+PCRE2_UCHAR othercase[2];
+#else
+PCRE2_UCHAR othercase[1];
+#endif
+
+repeat = 1;
+while (TRUE)
+ {
+ if (*rec_count == 0)
+ return 0;
+ (*rec_count)--;
+
+ last = TRUE;
+ any = FALSE;
+ class = FALSE;
+ caseless = FALSE;
+
+ switch (*cc)
+ {
+ case OP_CHARI:
+ caseless = TRUE;
+ /* Fall through */
+ case OP_CHAR:
+ last = FALSE;
+ cc++;
+ break;
+
+ case OP_SOD:
+ case OP_SOM:
+ case OP_SET_SOM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ /* Zero width assertions. */
+ cc++;
+ continue;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ cc = bracketend(cc);
+ continue;
+
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSPLUSI:
+ caseless = TRUE;
+ /* Fall through */
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSPLUS:
+ cc++;
+ break;
+
+ case OP_EXACTI:
+ caseless = TRUE;
+ /* Fall through */
+ case OP_EXACT:
+ repeat = GET2(cc, 1);
+ last = FALSE;
+ cc += 1 + IMM2_SIZE;
+ break;
+
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_POSQUERYI:
+ caseless = TRUE;
+ /* Fall through */
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_POSQUERY:
+ len = 1;
+ cc++;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
+#endif
+ max_chars = scan_prefix(common, cc + len, chars, max_chars, rec_count);
+ if (max_chars == 0)
+ return consumed;
+ last = FALSE;
+ break;
+
+ case OP_KET:
+ cc += 1 + LINK_SIZE;
+ continue;
+
+ case OP_ALT:
+ cc += GET(cc, 1);
+ continue;
+
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_BRAPOS:
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ alternative = cc + GET(cc, 1);
+ while (*alternative == OP_ALT)
+ {
+ max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars, rec_count);
+ if (max_chars == 0)
+ return consumed;
+ alternative += GET(alternative, 1);
+ }
+
+ if (*cc == OP_CBRA || *cc == OP_CBRAPOS)
+ cc += IMM2_SIZE;
+ cc += 1 + LINK_SIZE;
+ continue;
+
+ case OP_CLASS:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && !is_char7_bitset((const sljit_u8 *)(cc + 1), FALSE))
+ return consumed;
+#endif
+ class = TRUE;
+ break;
+
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf) return consumed;
+#endif
+ class = TRUE;
+ break;
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf) return consumed;
+#endif
+ any = TRUE;
+ cc += GET(cc, 1);
+ break;
+#endif
+
+ case OP_DIGIT:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_digit, FALSE))
+ return consumed;
+#endif
+ any = TRUE;
+ cc++;
+ break;
+
+ case OP_WHITESPACE:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_space, FALSE))
+ return consumed;
+#endif
+ any = TRUE;
+ cc++;
+ break;
+
+ case OP_WORDCHAR:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_word, FALSE))
+ return consumed;
+#endif
+ any = TRUE;
+ cc++;
+ break;
+
+ case OP_NOT:
+ case OP_NOTI:
+ cc++;
+ /* Fall through. */
+ case OP_NOT_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf) return consumed;
+#endif
+ any = TRUE;
+ cc++;
+ break;
+
+#ifdef SUPPORT_UNICODE
+ case OP_NOTPROP:
+ case OP_PROP:
+#if PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf) return consumed;
+#endif
+ any = TRUE;
+ cc += 1 + 2;
+ break;
+#endif
+
+ case OP_TYPEEXACT:
+ repeat = GET2(cc, 1);
+ cc += 1 + IMM2_SIZE;
+ continue;
+
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf) return consumed;
+#endif
+ any = TRUE;
+ repeat = GET2(cc, 1);
+ cc += 1 + IMM2_SIZE + 1;
+ break;
+
+ default:
+ return consumed;
+ }
+
+ if (any)
+ {
+ do
+ {
+ chars->count = 255;
+
+ consumed++;
+ if (--max_chars == 0)
+ return consumed;
+ chars++;
+ }
+ while (--repeat > 0);
+
+ repeat = 1;
+ continue;
+ }
+
+ if (class)
+ {
+ bytes = (sljit_u8*) (cc + 1);
+ cc += 1 + 32 / sizeof(PCRE2_UCHAR);
+
+ switch (*cc)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPOSSTAR:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSQUERY:
+ max_chars = scan_prefix(common, cc + 1, chars, max_chars, rec_count);
+ if (max_chars == 0)
+ return consumed;
+ break;
+
+ default:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSPLUS:
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ repeat = GET2(cc, 1);
+ if (repeat <= 0)
+ return consumed;
+ break;
+ }
+
+ do
+ {
+ if (bytes[31] & 0x80)
+ chars->count = 255;
+ else if (chars->count != 255)
+ {
+ bytes_end = bytes + 32;
+ chr = 0;
+ do
+ {
+ byte = *bytes++;
+ SLJIT_ASSERT((chr & 0x7) == 0);
+ if (byte == 0)
+ chr += 8;
+ else
+ {
+ do
+ {
+ if ((byte & 0x1) != 0)
+ add_prefix_char(chr, chars, TRUE);
+ byte >>= 1;
+ chr++;
+ }
+ while (byte != 0);
+ chr = (chr + 7) & ~7;
+ }
+ }
+ while (chars->count != 255 && bytes < bytes_end);
+ bytes = bytes_end - 32;
+ }
+
+ consumed++;
+ if (--max_chars == 0)
+ return consumed;
+ chars++;
+ }
+ while (--repeat > 0);
+
+ switch (*cc)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPOSSTAR:
+ return consumed;
+
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSQUERY:
+ cc++;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ if (GET2(cc, 1) != GET2(cc, 1 + IMM2_SIZE))
+ return consumed;
+ cc += 1 + 2 * IMM2_SIZE;
+ break;
+ }
+
+ repeat = 1;
+ continue;
+ }
+
+ len = 1;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc);
+#endif
+
+ if (caseless && char_has_othercase(common, cc))
+ {
+#ifdef SUPPORT_UNICODE
+ if (common->utf)
+ {
+ GETCHAR(chr, cc);
+ if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len)
+ return consumed;
+ }
+ else
+#endif
+ {
+ chr = *cc;
+ othercase[0] = TABLE_GET(chr, common->fcc, chr);
+ }
+ }
+ else
+ {
+ caseless = FALSE;
+ othercase[0] = 0; /* Stops compiler warning - PH */
+ }
+
+ len_save = len;
+ cc_save = cc;
+ while (TRUE)
+ {
+ oc = othercase;
+ do
+ {
+ len--;
+ consumed++;
+
+ chr = *cc;
+ add_prefix_char(*cc, chars, len == 0);
+
+ if (caseless)
+ add_prefix_char(*oc, chars, len == 0);
+
+ if (--max_chars == 0)
+ return consumed;
+ chars++;
+ cc++;
+ oc++;
+ }
+ while (len > 0);
+
+ if (--repeat == 0)
+ break;
+
+ len = len_save;
+ cc = cc_save;
+ }
+
+ repeat = 1;
+ if (last)
+ return consumed;
+ }
+}
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+static void jumpto_if_not_utf_char_start(struct sljit_compiler *compiler, sljit_s32 reg, struct sljit_label *label)
+{
+#if PCRE2_CODE_UNIT_WIDTH == 8
+OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xc0);
+CMPTO(SLJIT_EQUAL, reg, 0, SLJIT_IMM, 0x80, label);
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xfc00);
+CMPTO(SLJIT_EQUAL, reg, 0, SLJIT_IMM, 0xdc00, label);
+#else
+#error "Unknown code width"
+#endif
+}
+#endif
+
+#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND)
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+static struct sljit_jump *jump_if_utf_char_start(struct sljit_compiler *compiler, sljit_s32 reg)
+{
+#if PCRE2_CODE_UNIT_WIDTH == 8
+OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xc0);
+return CMP(SLJIT_NOT_EQUAL, reg, 0, SLJIT_IMM, 0x80);
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+OP2(SLJIT_AND, reg, 0, reg, 0, SLJIT_IMM, 0xfc00);
+return CMP(SLJIT_NOT_EQUAL, reg, 0, SLJIT_IMM, 0xdc00);
+#else
+#error "Unknown code width"
+#endif
+}
+#endif
+
+static sljit_s32 character_to_int32(PCRE2_UCHAR chr)
+{
+sljit_s32 value = (sljit_s32)chr;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#define SSE2_COMPARE_TYPE_INDEX 0
+return (value << 24) | (value << 16) | (value << 8) | value;
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+#define SSE2_COMPARE_TYPE_INDEX 1
+return (value << 16) | value;
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+#define SSE2_COMPARE_TYPE_INDEX 2
+return value;
+#else
+#error "Unsupported unit width"
+#endif
+}
+
+static void load_from_mem_sse2(struct sljit_compiler *compiler, sljit_s32 dst_xmm_reg, sljit_s32 src_general_reg)
+{
+#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
+sljit_u8 instruction[5];
+#else
+sljit_u8 instruction[4];
+#endif
+
+SLJIT_ASSERT(dst_xmm_reg < 8);
+
+/* MOVDQA xmm1, xmm2/m128 */
+#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
+if (src_general_reg < 8)
+ {
+ instruction[0] = 0x66;
+ instruction[1] = 0x0f;
+ instruction[2] = 0x6f;
+ instruction[3] = (dst_xmm_reg << 3) | src_general_reg;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+else
+ {
+ instruction[0] = 0x66;
+ instruction[1] = 0x41;
+ instruction[2] = 0x0f;
+ instruction[3] = 0x6f;
+ instruction[4] = (dst_xmm_reg << 3) | (src_general_reg & 0x7);
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+#else
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+instruction[2] = 0x6f;
+instruction[3] = (dst_xmm_reg << 3) | src_general_reg;
+sljit_emit_op_custom(compiler, instruction, 4);
+#endif
+}
+
+static void fast_forward_char_pair_sse2_compare(struct sljit_compiler *compiler, PCRE2_UCHAR char1, PCRE2_UCHAR char2,
+ sljit_u32 bit, sljit_s32 dst_ind, sljit_s32 cmp1_ind, sljit_s32 cmp2_ind, sljit_s32 tmp_ind)
+{
+sljit_u8 instruction[4];
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+
+if (char1 == char2 || bit != 0)
+ {
+ if (bit != 0)
+ {
+ /* POR xmm1, xmm2/m128 */
+ /* instruction[0] = 0x66; */
+ /* instruction[1] = 0x0f; */
+ instruction[2] = 0xeb;
+ instruction[3] = 0xc0 | (dst_ind << 3) | cmp2_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+
+ /* PCMPEQB/W/D xmm1, xmm2/m128 */
+ /* instruction[0] = 0x66; */
+ /* instruction[1] = 0x0f; */
+ instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX;
+ instruction[3] = 0xc0 | (dst_ind << 3) | cmp1_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+else
+ {
+ /* MOVDQA xmm1, xmm2/m128 */
+ /* instruction[0] = 0x66; */
+ /* instruction[1] = 0x0f; */
+ instruction[2] = 0x6f;
+ instruction[3] = 0xc0 | (tmp_ind << 3) | dst_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+
+ /* PCMPEQB/W/D xmm1, xmm2/m128 */
+ /* instruction[0] = 0x66; */
+ /* instruction[1] = 0x0f; */
+ instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX;
+ instruction[3] = 0xc0 | (dst_ind << 3) | cmp1_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+
+ instruction[3] = 0xc0 | (tmp_ind << 3) | cmp2_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+
+ /* POR xmm1, xmm2/m128 */
+ /* instruction[0] = 0x66; */
+ /* instruction[1] = 0x0f; */
+ instruction[2] = 0xeb;
+ instruction[3] = 0xc0 | (dst_ind << 3) | tmp_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+}
+
+static void fast_forward_first_char2_sse2(compiler_common *common, PCRE2_UCHAR char1, PCRE2_UCHAR char2, sljit_s32 offset)
+{
+DEFINE_COMPILER;
+struct sljit_label *start;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+struct sljit_label *restart;
+#endif
+struct sljit_jump *quit;
+struct sljit_jump *partial_quit[2];
+sljit_u8 instruction[8];
+sljit_s32 tmp1_ind = sljit_get_register_index(TMP1);
+sljit_s32 str_ptr_ind = sljit_get_register_index(STR_PTR);
+sljit_s32 data_ind = 0;
+sljit_s32 tmp_ind = 1;
+sljit_s32 cmp1_ind = 2;
+sljit_s32 cmp2_ind = 3;
+sljit_u32 bit = 0;
+
+SLJIT_UNUSED_ARG(offset);
+
+if (char1 != char2)
+ {
+ bit = char1 ^ char2;
+ if (!is_powerof2(bit))
+ bit = 0;
+ }
+
+partial_quit[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, &common->failed_match, partial_quit[0]);
+
+/* First part (unaligned start) */
+
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1 | bit));
+
+SLJIT_ASSERT(tmp1_ind < 8);
+
+/* MOVD xmm, r/m32 */
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+instruction[2] = 0x6e;
+instruction[3] = 0xc0 | (cmp1_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+if (char1 != char2)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(bit != 0 ? bit : char2));
+
+ /* MOVD xmm, r/m32 */
+ instruction[3] = 0xc0 | (cmp2_ind << 3) | tmp1_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+
+OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
+
+/* PSHUFD xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x70;
+instruction[3] = 0xc0 | (cmp1_ind << 3) | 2;
+instruction[4] = 0;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+if (char1 != char2)
+ {
+ /* PSHUFD xmm1, xmm2/m128, imm8 */
+ instruction[3] = 0xc0 | (cmp2_ind << 3) | 3;
+ sljit_emit_op_custom(compiler, instruction, 5);
+ }
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+restart = LABEL();
+#endif
+OP2(SLJIT_AND, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, ~0xf);
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xf);
+
+load_from_mem_sse2(compiler, data_ind, str_ptr_ind);
+fast_forward_char_pair_sse2_compare(compiler, char1, char2, bit, data_ind, cmp1_ind, cmp2_ind, tmp_ind);
+
+/* PMOVMSKB reg, xmm */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xd7;
+instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, TMP2, 0);
+
+/* BSF r32, r/m32 */
+instruction[0] = 0x0f;
+instruction[1] = 0xbc;
+instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 3);
+sljit_set_current_flags(compiler, SLJIT_SET_Z);
+
+quit = JUMP(SLJIT_NOT_ZERO);
+
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+
+start = LABEL();
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 16);
+
+partial_quit[1] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, &common->failed_match, partial_quit[1]);
+
+/* Second part (aligned) */
+
+load_from_mem_sse2(compiler, 0, str_ptr_ind);
+fast_forward_char_pair_sse2_compare(compiler, char1, char2, bit, data_ind, cmp1_ind, cmp2_ind, tmp_ind);
+
+/* PMOVMSKB reg, xmm */
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+instruction[2] = 0xd7;
+instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* BSF r32, r/m32 */
+instruction[0] = 0x0f;
+instruction[1] = 0xbc;
+instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 3);
+sljit_set_current_flags(compiler, SLJIT_SET_Z);
+
+JUMPTO(SLJIT_ZERO, start);
+
+JUMPHERE(quit);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+
+if (common->mode != PCRE2_JIT_COMPLETE)
+ {
+ JUMPHERE(partial_quit[0]);
+ JUMPHERE(partial_quit[1]);
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_PTR, 0, STR_END, 0);
+ CMOV(SLJIT_GREATER, STR_PTR, STR_END, 0);
+ }
+else
+ add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+if (common->utf && offset > 0)
+ {
+ SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE);
+
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offset));
+
+ quit = jump_if_utf_char_start(compiler, TMP1);
+
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+ OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, restart);
+
+ JUMPHERE(quit);
+ }
+#endif
+}
+
+#ifndef _WIN64
+
+static SLJIT_INLINE sljit_u32 max_fast_forward_char_pair_sse2_offset(void)
+{
+#if PCRE2_CODE_UNIT_WIDTH == 8
+return 15;
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+return 7;
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+return 3;
+#else
+#error "Unsupported unit width"
+#endif
+}
+
+static void fast_forward_char_pair_sse2(compiler_common *common, sljit_s32 offs1,
+ PCRE2_UCHAR char1a, PCRE2_UCHAR char1b, sljit_s32 offs2, PCRE2_UCHAR char2a, PCRE2_UCHAR char2b)
+{
+DEFINE_COMPILER;
+sljit_u32 bit1 = 0;
+sljit_u32 bit2 = 0;
+sljit_u32 diff = IN_UCHARS(offs1 - offs2);
+sljit_s32 tmp1_ind = sljit_get_register_index(TMP1);
+sljit_s32 tmp2_ind = sljit_get_register_index(TMP2);
+sljit_s32 str_ptr_ind = sljit_get_register_index(STR_PTR);
+sljit_s32 data1_ind = 0;
+sljit_s32 data2_ind = 1;
+sljit_s32 tmp_ind = 2;
+sljit_s32 cmp1a_ind = 3;
+sljit_s32 cmp1b_ind = 4;
+sljit_s32 cmp2a_ind = 5;
+sljit_s32 cmp2b_ind = 6;
+struct sljit_label *start;
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+struct sljit_label *restart;
+#endif
+struct sljit_jump *jump[2];
+
+sljit_u8 instruction[8];
+
+SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE && offs1 > offs2);
+SLJIT_ASSERT(diff <= IN_UCHARS(max_fast_forward_char_pair_sse2_offset()));
+SLJIT_ASSERT(tmp1_ind < 8 && tmp2_ind == 1);
+
+/* Initialize. */
+if (common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+ OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(offs1 + 1));
+
+ OP2(SLJIT_SUB | SLJIT_SET_LESS, SLJIT_UNUSED, 0, TMP1, 0, STR_END, 0);
+ CMOV(SLJIT_LESS, STR_END, TMP1, 0);
+ }
+
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1));
+add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+
+/* MOVD xmm, r/m32 */
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+instruction[2] = 0x6e;
+
+if (char1a == char1b)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a));
+else
+ {
+ bit1 = char1a ^ char1b;
+ if (is_powerof2(bit1))
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a | bit1));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(bit1));
+ }
+ else
+ {
+ bit1 = 0;
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1a));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(char1b));
+ }
+ }
+
+instruction[3] = 0xc0 | (cmp1a_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+if (char1a != char1b)
+ {
+ instruction[3] = 0xc0 | (cmp1b_ind << 3) | tmp2_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+
+if (char2a == char2b)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a));
+else
+ {
+ bit2 = char2a ^ char2b;
+ if (is_powerof2(bit2))
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a | bit2));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(bit2));
+ }
+ else
+ {
+ bit2 = 0;
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char2a));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, character_to_int32(char2b));
+ }
+ }
+
+instruction[3] = 0xc0 | (cmp2a_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+if (char2a != char2b)
+ {
+ instruction[3] = 0xc0 | (cmp2b_ind << 3) | tmp2_ind;
+ sljit_emit_op_custom(compiler, instruction, 4);
+ }
+
+/* PSHUFD xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x70;
+instruction[4] = 0;
+
+instruction[3] = 0xc0 | (cmp1a_ind << 3) | cmp1a_ind;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+if (char1a != char1b)
+ {
+ instruction[3] = 0xc0 | (cmp1b_ind << 3) | cmp1b_ind;
+ sljit_emit_op_custom(compiler, instruction, 5);
+ }
+
+instruction[3] = 0xc0 | (cmp2a_ind << 3) | cmp2a_ind;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+if (char2a != char2b)
+ {
+ instruction[3] = 0xc0 | (cmp2b_ind << 3) | cmp2b_ind;
+ sljit_emit_op_custom(compiler, instruction, 5);
+ }
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+restart = LABEL();
+#endif
+
+OP2(SLJIT_SUB, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1 - offs2));
+OP1(SLJIT_MOV, TMP2, 0, STR_PTR, 0);
+OP2(SLJIT_AND, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, ~0xf);
+OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, ~0xf);
+
+load_from_mem_sse2(compiler, data1_ind, str_ptr_ind);
+
+jump[0] = CMP(SLJIT_EQUAL, STR_PTR, 0, TMP1, 0);
+
+load_from_mem_sse2(compiler, data2_ind, tmp1_ind);
+
+/* MOVDQA xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x6f;
+instruction[3] = 0xc0 | (tmp_ind << 3) | data1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* PSLLDQ xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x73;
+instruction[3] = 0xc0 | (7 << 3) | tmp_ind;
+instruction[4] = diff;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+/* PSRLDQ xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+/* instruction[2] = 0x73; */
+instruction[3] = 0xc0 | (3 << 3) | data2_ind;
+instruction[4] = 16 - diff;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+/* POR xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xeb;
+instruction[3] = 0xc0 | (data2_ind << 3) | tmp_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+jump[1] = JUMP(SLJIT_JUMP);
+
+JUMPHERE(jump[0]);
+
+/* MOVDQA xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x6f;
+instruction[3] = 0xc0 | (data2_ind << 3) | data1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* PSLLDQ xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x73;
+instruction[3] = 0xc0 | (7 << 3) | data2_ind;
+instruction[4] = diff;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+JUMPHERE(jump[1]);
+
+OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xf);
+
+fast_forward_char_pair_sse2_compare(compiler, char2a, char2b, bit2, data2_ind, cmp2a_ind, cmp2b_ind, tmp_ind);
+fast_forward_char_pair_sse2_compare(compiler, char1a, char1b, bit1, data1_ind, cmp1a_ind, cmp1b_ind, tmp_ind);
+
+/* PAND xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xdb;
+instruction[3] = 0xc0 | (data1_ind << 3) | data2_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* PMOVMSKB reg, xmm */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xd7;
+instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* Ignore matches before the first STR_PTR. */
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, TMP2, 0);
+
+/* BSF r32, r/m32 */
+instruction[0] = 0x0f;
+instruction[1] = 0xbc;
+instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 3);
+sljit_set_current_flags(compiler, SLJIT_SET_Z);
+
+jump[0] = JUMP(SLJIT_NOT_ZERO);
+
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+
+/* Main loop. */
+instruction[0] = 0x66;
+instruction[1] = 0x0f;
+
+start = LABEL();
+
+load_from_mem_sse2(compiler, data2_ind, str_ptr_ind);
+
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 16);
+add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+
+load_from_mem_sse2(compiler, data1_ind, str_ptr_ind);
+
+/* PSRLDQ xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x73;
+instruction[3] = 0xc0 | (3 << 3) | data2_ind;
+instruction[4] = 16 - diff;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+/* MOVDQA xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x6f;
+instruction[3] = 0xc0 | (tmp_ind << 3) | data1_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* PSLLDQ xmm1, xmm2/m128, imm8 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0x73;
+instruction[3] = 0xc0 | (7 << 3) | tmp_ind;
+instruction[4] = diff;
+sljit_emit_op_custom(compiler, instruction, 5);
+
+/* POR xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xeb;
+instruction[3] = 0xc0 | (data2_ind << 3) | tmp_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+fast_forward_char_pair_sse2_compare(compiler, char1a, char1b, bit1, data1_ind, cmp1a_ind, cmp1b_ind, tmp_ind);
+fast_forward_char_pair_sse2_compare(compiler, char2a, char2b, bit2, data2_ind, cmp2a_ind, cmp2b_ind, tmp_ind);
+
+/* PAND xmm1, xmm2/m128 */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xdb;
+instruction[3] = 0xc0 | (data1_ind << 3) | data2_ind;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* PMOVMSKB reg, xmm */
+/* instruction[0] = 0x66; */
+/* instruction[1] = 0x0f; */
+instruction[2] = 0xd7;
+instruction[3] = 0xc0 | (tmp1_ind << 3) | 0;
+sljit_emit_op_custom(compiler, instruction, 4);
+
+/* BSF r32, r/m32 */
+instruction[0] = 0x0f;
+instruction[1] = 0xbc;
+instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind;
+sljit_emit_op_custom(compiler, instruction, 3);
+sljit_set_current_flags(compiler, SLJIT_SET_Z);
+
+JUMPTO(SLJIT_ZERO, start);
+
+JUMPHERE(jump[0]);
+
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+
+add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+
+if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+if (common->utf)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offs1));
+
+ jump[0] = jump_if_utf_char_start(compiler, TMP1);
+
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, restart);
+
+ add_jump(compiler, &common->failed_match, JUMP(SLJIT_JUMP));
+
+ JUMPHERE(jump[0]);
+ }
+#endif
+
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offs1));
+
+if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+}
+
+static BOOL check_fast_forward_char_pair_sse2(compiler_common *common, fast_forward_char_data *chars, int max)
+{
+sljit_s32 i, j, priority, count;
+sljit_u32 priorities;
+PCRE2_UCHAR a1, a2, b1, b2;
+
+priorities = 0;
+
+count = 0;
+for (i = 0; i < max; i++)
+ {
+ if (chars[i].last_count > 2)
+ {
+ SLJIT_ASSERT(chars[i].last_count <= 7);
+
+ priorities |= (1 << chars[i].last_count);
+ count++;
+ }
+ }
+
+if (count < 2)
+ return FALSE;
+
+for (priority = 7; priority > 2; priority--)
+ {
+ if ((priorities & (1 << priority)) == 0)
+ continue;
+
+ for (i = max - 1; i >= 1; i--)
+ if (chars[i].last_count >= priority)
+ {
+ SLJIT_ASSERT(chars[i].count <= 2 && chars[i].count >= 1);
+
+ a1 = chars[i].chars[0];
+ a2 = chars[i].chars[1];
+
+ j = i - max_fast_forward_char_pair_sse2_offset();
+ if (j < 0)
+ j = 0;
+
+ while (j < i)
+ {
+ if (chars[j].last_count >= priority)
+ {
+ b1 = chars[j].chars[0];
+ b2 = chars[j].chars[1];
+
+ if (a1 != b1 && a1 != b2 && a2 != b1 && a2 != b2)
+ {
+ fast_forward_char_pair_sse2(common, i, a1, a2, j, b1, b2);
+ return TRUE;
+ }
+ }
+ j++;
+ }
+ }
+ }
+
+return FALSE;
+}
+
+#endif
+
+#undef SSE2_COMPARE_TYPE_INDEX
+
+#endif
+
+static void fast_forward_first_char2(compiler_common *common, PCRE2_UCHAR char1, PCRE2_UCHAR char2, sljit_s32 offset)
+{
+DEFINE_COMPILER;
+struct sljit_label *start;
+struct sljit_jump *match;
+struct sljit_jump *partial_quit;
+PCRE2_UCHAR mask;
+BOOL has_match_end = (common->match_end_ptr != 0);
+
+SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE || offset == 0);
+
+if (has_match_end)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+
+if (offset > 0)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset));
+
+if (has_match_end)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
+
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(offset + 1));
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
+ CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
+ }
+
+#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND)
+
+/* SSE2 accelerated first character search. */
+
+if (sljit_has_cpu_feature(SLJIT_HAS_SSE2))
+ {
+ fast_forward_first_char2_sse2(common, char1, char2, offset);
+
+ if (offset > 0)
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset));
+
+ if (has_match_end)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+ return;
+ }
+
+#endif
+
+start = LABEL();
+
+partial_quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, &common->failed_match, partial_quit);
+
+OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+if (char1 == char2)
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char1, start);
+else
+ {
+ mask = char1 ^ char2;
+ if (is_powerof2(mask))
+ {
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char1 | mask, start);
+ }
+ else
+ {
+ match = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, char1);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, char2, start);
+ JUMPHERE(match);
+ }
+ }
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+if (common->utf && offset > 0)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-(offset + 1)));
+ jumpto_if_not_utf_char_start(compiler, TMP1, start);
+ }
+#endif
+
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset + 1));
+
+if (common->mode != PCRE2_JIT_COMPLETE)
+ JUMPHERE(partial_quit);
+
+if (has_match_end)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+}
+
+static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_label *start;
+struct sljit_jump *match;
+fast_forward_char_data chars[MAX_N_CHARS];
+sljit_s32 offset;
+PCRE2_UCHAR mask;
+PCRE2_UCHAR *char_set, *char_set_end;
+int i, max, from;
+int range_right = -1, range_len;
+sljit_u8 *update_table = NULL;
+BOOL in_range;
+sljit_u32 rec_count;
+
+for (i = 0; i < MAX_N_CHARS; i++)
+ {
+ chars[i].count = 0;
+ chars[i].last_count = 0;
+ }
+
+rec_count = 10000;
+max = scan_prefix(common, common->start, chars, MAX_N_CHARS, &rec_count);
+
+if (max < 1)
+ return FALSE;
+
+/* Convert last_count to priority. */
+for (i = 0; i < max; i++)
+ {
+ SLJIT_ASSERT(chars[i].count > 0 && chars[i].last_count <= chars[i].count);
+
+ if (chars[i].count == 1)
+ {
+ chars[i].last_count = (chars[i].last_count == 1) ? 7 : 5;
+ /* Simplifies algorithms later. */
+ chars[i].chars[1] = chars[i].chars[0];
+ }
+ else if (chars[i].count == 2)
+ {
+ SLJIT_ASSERT(chars[i].chars[0] != chars[i].chars[1]);
+
+ if (is_powerof2(chars[i].chars[0] ^ chars[i].chars[1]))
+ chars[i].last_count = (chars[i].last_count == 2) ? 6 : 4;
+ else
+ chars[i].last_count = (chars[i].last_count == 2) ? 3 : 2;
+ }
+ else
+ chars[i].last_count = (chars[i].count == 255) ? 0 : 1;
+ }
+
+#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND) && !(defined _WIN64)
+if (check_fast_forward_char_pair_sse2(common, chars, max))
+ return TRUE;
+#endif
+
+in_range = FALSE;
+/* Prevent compiler "uninitialized" warning */
+from = 0;
+range_len = 4 /* minimum length */ - 1;
+for (i = 0; i <= max; i++)
+ {
+ if (in_range && (i - from) > range_len && (chars[i - 1].count < 255))
+ {
+ range_len = i - from;
+ range_right = i - 1;
+ }
+
+ if (i < max && chars[i].count < 255)
+ {
+ SLJIT_ASSERT(chars[i].count > 0);
+ if (!in_range)
+ {
+ in_range = TRUE;
+ from = i;
+ }
+ }
+ else
+ in_range = FALSE;
+ }
+
+if (range_right >= 0)
+ {
+ update_table = (sljit_u8 *)allocate_read_only_data(common, 256);
+ if (update_table == NULL)
+ return TRUE;
+ memset(update_table, IN_UCHARS(range_len), 256);
+
+ for (i = 0; i < range_len; i++)
+ {
+ SLJIT_ASSERT(chars[range_right - i].count > 0 && chars[range_right - i].count < 255);
+
+ char_set = chars[range_right - i].chars;
+ char_set_end = char_set + chars[range_right - i].count;
+ do
+ {
+ if (update_table[(*char_set) & 0xff] > IN_UCHARS(i))
+ update_table[(*char_set) & 0xff] = IN_UCHARS(i);
+ char_set++;
+ }
+ while (char_set < char_set_end);
+ }
+ }
+
+offset = -1;
+/* Scan forward. */
+for (i = 0; i < max; i++)
+ {
+ if (range_right == i)
+ continue;
+
+ if (offset == -1)
+ {
+ if (chars[i].last_count >= 2)
+ offset = i;
+ }
+ else if (chars[offset].last_count < chars[i].last_count)
+ offset = i;
+ }
+
+SLJIT_ASSERT(offset == -1 || (chars[offset].count >= 1 && chars[offset].count <= 2));
+
+if (range_right < 0)
+ {
+ if (offset < 0)
+ return FALSE;
+ /* Works regardless the value is 1 or 2. */
+ fast_forward_first_char2(common, chars[offset].chars[0], chars[offset].chars[1], offset);
+ return TRUE;
+ }
+
+SLJIT_ASSERT(range_right != offset);
+
+if (common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+ OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
+ OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
+ CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
+ }
+else
+ OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
+
+SLJIT_ASSERT(range_right >= 0);
+
+#if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
+OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table);
+#endif
+
+start = LABEL();
+add_jump(compiler, &common->failed_match, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
+
+#if PCRE2_CODE_UNIT_WIDTH == 8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right));
+#else
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1);
+#endif
+
+#if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0);
+#else
+OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table);
+#endif
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start);
+
+if (offset >= 0)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offset));
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+ if (chars[offset].count == 1)
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0], start);
+ else
+ {
+ mask = chars[offset].chars[0] ^ chars[offset].chars[1];
+ if (is_powerof2(mask))
+ {
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0] | mask, start);
+ }
+ else
+ {
+ match = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[0]);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset].chars[1], start);
+ JUMPHERE(match);
+ }
+ }
+ }
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+if (common->utf && offset != 0)
+ {
+ if (offset < 0)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+ else
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
+
+ jumpto_if_not_utf_char_start(compiler, TMP1, start);
+
+ if (offset < 0)
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+#endif
+
+if (offset >= 0)
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+else
+ OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max));
+return TRUE;
+}
+
+static SLJIT_INLINE void fast_forward_first_char(compiler_common *common)
+{
+PCRE2_UCHAR first_char = (PCRE2_UCHAR)(common->re->first_codeunit);
+PCRE2_UCHAR oc;
+
+oc = first_char;
+if ((common->re->flags & PCRE2_FIRSTCASELESS) != 0)
+ {
+ oc = TABLE_GET(first_char, common->fcc, first_char);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (first_char > 127 && common->utf)
+ oc = UCD_OTHERCASE(first_char);
+#endif
+ }
+
+fast_forward_first_char2(common, first_char, oc, 0);
+}
+
+static SLJIT_INLINE void fast_forward_newline(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_label *loop;
+struct sljit_jump *lastchar;
+struct sljit_jump *firstchar;
+struct sljit_jump *quit;
+struct sljit_jump *foundcr = NULL;
+struct sljit_jump *notfoundnl;
+jump_list *newline = NULL;
+
+if (common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
+ OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+ }
+
+if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
+ firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
+
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER_EQUAL);
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+
+ loop = LABEL();
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);
+ CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);
+
+ JUMPHERE(quit);
+ JUMPHERE(firstchar);
+ JUMPHERE(lastchar);
+
+ if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+ return;
+ }
+
+OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
+skip_char_back(common);
+
+loop = LABEL();
+common->ff_newline_shortcut = loop;
+
+read_char_range(common, common->nlmin, common->nlmax, TRUE);
+lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
+ foundcr = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
+check_newlinechar(common, common->nltype, &newline, FALSE);
+set_jumps(newline, loop);
+
+if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
+ {
+ quit = JUMP(SLJIT_JUMP);
+ JUMPHERE(foundcr);
+ notfoundnl = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
+ OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
+#if PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
+#endif
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ JUMPHERE(notfoundnl);
+ JUMPHERE(quit);
+ }
+JUMPHERE(lastchar);
+JUMPHERE(firstchar);
+
+if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+}
+
+static BOOL optimize_class(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks);
+
+static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common)
+{
+DEFINE_COMPILER;
+const sljit_u8 *start_bits = common->re->start_bitmap;
+struct sljit_label *start;
+struct sljit_jump *partial_quit;
+#if PCRE2_CODE_UNIT_WIDTH != 8
+struct sljit_jump *found = NULL;
+#endif
+jump_list *matches = NULL;
+
+if (common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+ OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP1, 0);
+ CMOV(SLJIT_GREATER, STR_END, TMP1, 0);
+ }
+
+start = LABEL();
+
+partial_quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, &common->failed_match, partial_quit);
+
+OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+if (!optimize_class(common, start_bits, (start_bits[31] & 0x80) != 0, FALSE, &matches))
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if ((start_bits[31] & 0x80) != 0)
+ found = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 255);
+ else
+ CMPTO(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 255, start);
+#elif defined SUPPORT_UNICODE
+ if (common->utf && is_char7_bitset(start_bits, FALSE))
+ CMPTO(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 127, start);
+#endif
+ OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
+ OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits);
+ if (sljit_get_register_index(TMP3) >= 0)
+ {
+ OP2(SLJIT_SHL, TMP3, 0, SLJIT_IMM, 1, TMP2, 0);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP3, 0);
+ }
+ else
+ {
+ OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
+ }
+ JUMPTO(SLJIT_ZERO, start);
+ }
+else
+ set_jumps(matches, start);
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+if (found != NULL)
+ JUMPHERE(found);
+#endif
+
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+if (common->mode != PCRE2_JIT_COMPLETE)
+ JUMPHERE(partial_quit);
+
+if (common->match_end_ptr != 0)
+ OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0);
+}
+
+static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, PCRE2_UCHAR req_char, BOOL caseless, BOOL has_firstchar)
+{
+DEFINE_COMPILER;
+struct sljit_label *loop;
+struct sljit_jump *toolong;
+struct sljit_jump *alreadyfound;
+struct sljit_jump *found;
+struct sljit_jump *foundoc = NULL;
+struct sljit_jump *notfound;
+sljit_u32 oc, bit;
+
+SLJIT_ASSERT(common->req_char_ptr != 0);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr);
+OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_CU_MAX);
+toolong = CMP(SLJIT_LESS, TMP1, 0, STR_END, 0);
+alreadyfound = CMP(SLJIT_LESS, STR_PTR, 0, TMP2, 0);
+
+if (has_firstchar)
+ OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+else
+ OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);
+
+loop = LABEL();
+notfound = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, STR_END, 0);
+
+OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0);
+oc = req_char;
+if (caseless)
+ {
+ oc = TABLE_GET(req_char, common->fcc, req_char);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (req_char > 127 && common->utf)
+ oc = UCD_OTHERCASE(req_char);
+#endif
+ }
+if (req_char == oc)
+ found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
+else
+ {
+ bit = req_char ^ oc;
+ if (is_powerof2(bit))
+ {
+ OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
+ found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
+ }
+ else
+ {
+ found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
+ foundoc = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, oc);
+ }
+ }
+OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+JUMPTO(SLJIT_JUMP, loop);
+
+JUMPHERE(found);
+if (foundoc)
+ JUMPHERE(foundoc);
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, TMP1, 0);
+JUMPHERE(alreadyfound);
+JUMPHERE(toolong);
+return notfound;
+}
+
+static void do_revertframes(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+struct sljit_label *mainloop;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+GET_LOCAL_BASE(TMP1, 0, 0);
+
+/* Drop frames until we reach STACK_TOP. */
+mainloop = LABEL();
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), -sizeof(sljit_sw));
+jump = CMP(SLJIT_SIG_LESS_EQUAL, TMP2, 0, SLJIT_IMM, 0);
+
+OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
+if (sljit_get_register_index (TMP3) < 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(STACK_TOP), -(3 * sizeof(sljit_sw)));
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
+ }
+else
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
+ OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(STACK_TOP), -(3 * sizeof(sljit_sw)));
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP1, 0);
+ GET_LOCAL_BASE(TMP1, 0, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP3, 0);
+ }
+JUMPTO(SLJIT_JUMP, mainloop);
+
+JUMPHERE(jump);
+jump = CMP(SLJIT_NOT_ZERO /* SIG_LESS */, TMP2, 0, SLJIT_IMM, 0);
+/* End of reverting values. */
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+
+JUMPHERE(jump);
+OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
+OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0);
+if (sljit_get_register_index (TMP3) < 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
+ }
+else
+ {
+ OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(STACK_TOP), -(2 * sizeof(sljit_sw)));
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP3, 0);
+ }
+JUMPTO(SLJIT_JUMP, mainloop);
+}
+
+static void check_wordboundary(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_jump *skipread;
+jump_list *skipread_list = NULL;
+#if PCRE2_CODE_UNIT_WIDTH != 8 || defined SUPPORT_UNICODE
+struct sljit_jump *jump;
+#endif
+
+SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16);
+
+sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+/* Get type of the previous char, and put it to LOCALS1. */
+OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, SLJIT_IMM, 0);
+skipread = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
+skip_char_back(common);
+check_start_used_ptr(common);
+read_char(common);
+
+/* Testing char type. */
+#ifdef SUPPORT_UNICODE
+if (common->use_ucp)
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
+ jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
+ add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ JUMPHERE(jump);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP2, 0);
+ }
+else
+#endif
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+#elif defined SUPPORT_UNICODE
+ /* Here LOCALS1 has already been zeroed. */
+ jump = NULL;
+ if (common->utf)
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);
+ OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ JUMPHERE(jump);
+#elif defined SUPPORT_UNICODE
+ if (jump != NULL)
+ JUMPHERE(jump);
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ }
+JUMPHERE(skipread);
+
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
+check_str_end(common, &skipread_list);
+peek_char(common, READ_CHAR_MAX);
+
+/* Testing char type. This is a code duplication. */
+#ifdef SUPPORT_UNICODE
+if (common->use_ucp)
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
+ jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
+ add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ JUMPHERE(jump);
+ }
+else
+#endif
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ /* TMP2 may be destroyed by peek_char. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+#elif defined SUPPORT_UNICODE
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
+ jump = NULL;
+ if (common->utf)
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+#endif
+ OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
+ OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
+ OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ JUMPHERE(jump);
+#elif defined SUPPORT_UNICODE
+ if (jump != NULL)
+ JUMPHERE(jump);
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ }
+set_jumps(skipread_list, LABEL());
+
+OP2(SLJIT_XOR | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
+sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+}
+
+static BOOL optimize_class_ranges(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
+{
+/* May destroy TMP1. */
+DEFINE_COMPILER;
+int ranges[MAX_CLASS_RANGE_SIZE];
+sljit_u8 bit, cbit, all;
+int i, byte, length = 0;
+
+bit = bits[0] & 0x1;
+/* All bits will be zero or one (since bit is zero or one). */
+all = -bit;
+
+for (i = 0; i < 256; )
+ {
+ byte = i >> 3;
+ if ((i & 0x7) == 0 && bits[byte] == all)
+ i += 8;
+ else
+ {
+ cbit = (bits[byte] >> (i & 0x7)) & 0x1;
+ if (cbit != bit)
+ {
+ if (length >= MAX_CLASS_RANGE_SIZE)
+ return FALSE;
+ ranges[length] = i;
+ length++;
+ bit = cbit;
+ all = -cbit;
+ }
+ i++;
+ }
+ }
+
+if (((bit == 0) && nclass) || ((bit == 1) && !nclass))
+ {
+ if (length >= MAX_CLASS_RANGE_SIZE)
+ return FALSE;
+ ranges[length] = 256;
+ length++;
+ }
+
+if (length < 0 || length > 4)
+ return FALSE;
+
+bit = bits[0] & 0x1;
+if (invert) bit ^= 0x1;
+
+/* No character is accepted. */
+if (length == 0 && bit == 0)
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+
+switch(length)
+ {
+ case 0:
+ /* When bit != 0, all characters are accepted. */
+ return TRUE;
+
+ case 1:
+ add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
+ return TRUE;
+
+ case 2:
+ if (ranges[0] + 1 != ranges[1])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
+ add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
+ return TRUE;
+
+ case 3:
+ if (bit != 0)
+ {
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
+ if (ranges[0] + 1 != ranges[1])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
+ return TRUE;
+ }
+
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[0]));
+ if (ranges[1] + 1 != ranges[2])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]);
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1]));
+ return TRUE;
+
+ case 4:
+ if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2])
+ && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2]
+ && (ranges[1] & (ranges[2] - ranges[0])) == 0
+ && is_powerof2(ranges[2] - ranges[0]))
+ {
+ SLJIT_ASSERT((ranges[0] & (ranges[2] - ranges[0])) == 0 && (ranges[2] & ranges[3] & (ranges[2] - ranges[0])) != 0);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]);
+ if (ranges[2] + 1 != ranges[3])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
+ add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
+ return TRUE;
+ }
+
+ if (bit != 0)
+ {
+ i = 0;
+ if (ranges[0] + 1 != ranges[1])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
+ i = ranges[0];
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0]));
+
+ if (ranges[2] + 1 != ranges[3])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i);
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i));
+ return TRUE;
+ }
+
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]);
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0]));
+ if (ranges[1] + 1 != ranges[2])
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]);
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1]));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]));
+ return TRUE;
+
+ default:
+ SLJIT_UNREACHABLE();
+ return FALSE;
+ }
+}
+
+static BOOL optimize_class_chars(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
+{
+/* May destroy TMP1. */
+DEFINE_COMPILER;
+uint16_t char_list[MAX_CLASS_CHARS_SIZE];
+uint8_t byte;
+sljit_s32 type;
+int i, j, k, len, c;
+
+if (!sljit_has_cpu_feature(SLJIT_HAS_CMOV))
+ return FALSE;
+
+if (invert)
+ nclass = !nclass;
+
+len = 0;
+
+for (i = 0; i < 32; i++)
+ {
+ byte = bits[i];
+
+ if (nclass)
+ byte = ~byte;
+
+ j = 0;
+ while (byte != 0)
+ {
+ if (byte & 0x1)
+ {
+ c = i * 8 + j;
+
+ k = len;
+
+ if ((c & 0x20) != 0)
+ {
+ for (k = 0; k < len; k++)
+ if (char_list[k] == c - 0x20)
+ {
+ char_list[k] |= 0x120;
+ break;
+ }
+ }
+
+ if (k == len)
+ {
+ if (len >= MAX_CLASS_CHARS_SIZE)
+ return FALSE;
+
+ char_list[len++] = (uint16_t) c;
+ }
+ }
+
+ byte >>= 1;
+ j++;
+ }
+ }
+
+if (len == 0) return FALSE; /* Should never occur, but stops analyzers complaining. */
+
+i = 0;
+j = 0;
+
+if (char_list[0] == 0)
+ {
+ i++;
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_ZERO);
+ }
+else
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
+
+while (i < len)
+ {
+ if ((char_list[i] & 0x100) != 0)
+ j++;
+ else
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char_list[i]);
+ CMOV(SLJIT_ZERO, TMP2, TMP1, 0);
+ }
+ i++;
+ }
+
+if (j != 0)
+ {
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x20);
+
+ for (i = 0; i < len; i++)
+ if ((char_list[i] & 0x100) != 0)
+ {
+ j--;
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char_list[i] & 0xff);
+ CMOV(SLJIT_ZERO, TMP2, TMP1, 0);
+ }
+ }
+
+type = nclass ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
+add_jump(compiler, backtracks, CMP(type, TMP2, 0, SLJIT_IMM, 0));
+return TRUE;
+}
+
+static BOOL optimize_class(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks)
+{
+/* May destroy TMP1. */
+if (optimize_class_ranges(common, bits, nclass, invert, backtracks))
+ return TRUE;
+return optimize_class_chars(common, bits, nclass, invert, backtracks);
+}
+
+static void check_anynewline(compiler_common *common)
+{
+/* Check whether TMP1 contains a newline character. TMP2 destroyed. */
+DEFINE_COMPILER;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+
+OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
+OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
+OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+#endif
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ }
+#endif
+#endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
+OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+static void check_hspace(compiler_common *common)
+{
+/* Check whether TMP1 contains a newline character. TMP2 destroyed. */
+DEFINE_COMPILER;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x09);
+OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
+OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xa0);
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+#endif
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x1680);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x2000);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x200A - 0x2000);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x202f - 0x2000);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x205f - 0x2000);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x3000 - 0x2000);
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ }
+#endif
+#endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
+OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
+
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+static void check_vspace(compiler_common *common)
+{
+/* Check whether TMP1 contains a newline character. TMP2 destroyed. */
+DEFINE_COMPILER;
+
+sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
+
+OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a);
+OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a);
+OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a);
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utf)
+ {
+#endif
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a);
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ }
+#endif
+#endif /* SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == [16|32] */
+OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
+
+sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
+}
+
+static void do_casefulcmp(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+struct sljit_label *label;
+int char1_reg;
+int char2_reg;
+
+if (sljit_get_register_index(TMP3) < 0)
+ {
+ char1_reg = STR_END;
+ char2_reg = STACK_TOP;
+ }
+else
+ {
+ char1_reg = TMP3;
+ char2_reg = RETURN_ADDR;
+ }
+
+sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+
+if (char1_reg == STR_END)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, char1_reg, 0);
+ OP1(SLJIT_MOV, RETURN_ADDR, 0, char2_reg, 0);
+ }
+
+if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
+ {
+ label = LABEL();
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
+ JUMPTO(SLJIT_NOT_ZERO, label);
+
+ JUMPHERE(jump);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+ }
+else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+ label = LABEL();
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
+ JUMPTO(SLJIT_NOT_ZERO, label);
+
+ JUMPHERE(jump);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+else
+ {
+ label = LABEL();
+ OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0);
+ OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
+ JUMPTO(SLJIT_NOT_ZERO, label);
+
+ JUMPHERE(jump);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+ }
+
+if (char1_reg == STR_END)
+ {
+ OP1(SLJIT_MOV, char1_reg, 0, TMP3, 0);
+ OP1(SLJIT_MOV, char2_reg, 0, RETURN_ADDR, 0);
+ }
+
+sljit_emit_fast_return(compiler, TMP1, 0);
+}
+
+static void do_caselesscmp(compiler_common *common)
+{
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+struct sljit_label *label;
+int char1_reg = STR_END;
+int char2_reg;
+int lcc_table;
+int opt_type = 0;
+
+if (sljit_get_register_index(TMP3) < 0)
+ {
+ char2_reg = STACK_TOP;
+ lcc_table = STACK_LIMIT;
+ }
+else
+ {
+ char2_reg = RETURN_ADDR;
+ lcc_table = TMP3;
+ }
+
+if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
+ opt_type = 1;
+else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS)
+ opt_type = 2;
+
+sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, char1_reg, 0);
+
+if (char2_reg == STACK_TOP)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, char2_reg, 0);
+ OP1(SLJIT_MOV, RETURN_ADDR, 0, lcc_table, 0);
+ }
+
+OP1(SLJIT_MOV, lcc_table, 0, SLJIT_IMM, common->lcc);
+
+if (opt_type == 1)
+ {
+ label = LABEL();
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ }
+else if (opt_type == 2)
+ {
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+ label = LABEL();
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1));
+ sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ }
+else
+ {
+ label = LABEL();
+ OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0);
+ OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+jump = CMP(SLJIT_GREATER, char1_reg, 0, SLJIT_IMM, 255);
+#endif
+OP1(SLJIT_MOV_U8, char1_reg, 0, SLJIT_MEM2(lcc_table, char1_reg), 0);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+JUMPHERE(jump);
+jump = CMP(SLJIT_GREATER, char2_reg, 0, SLJIT_IMM, 255);
+#endif
+OP1(SLJIT_MOV_U8, char2_reg, 0, SLJIT_MEM2(lcc_table, char2_reg), 0);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+JUMPHERE(jump);
+#endif
+
+if (opt_type == 0)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0);
+OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
+JUMPTO(SLJIT_NOT_ZERO, label);
+
+JUMPHERE(jump);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+
+if (opt_type == 2)
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+if (char2_reg == STACK_TOP)
+ {
+ OP1(SLJIT_MOV, char2_reg, 0, TMP3, 0);
+ OP1(SLJIT_MOV, lcc_table, 0, RETURN_ADDR, 0);
+ }
+
+OP1(SLJIT_MOV, char1_reg, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
+sljit_emit_fast_return(compiler, TMP1, 0);
+}
+
+#if defined SUPPORT_UNICODE
+
+static PCRE2_SPTR SLJIT_FUNC do_utf_caselesscmp(PCRE2_SPTR src1, PCRE2_SPTR src2, PCRE2_SPTR end1, PCRE2_SPTR end2)
+{
+/* This function would be ineffective to do in JIT level. */
+sljit_u32 c1, c2;
+const ucd_record *ur;
+const sljit_u32 *pp;
+
+while (src1 < end1)
+ {
+ if (src2 >= end2)
+ return (PCRE2_SPTR)1;
+ GETCHARINC(c1, src1);
+ GETCHARINC(c2, src2);
+ ur = GET_UCD(c2);
+ if (c1 != c2 && c1 != c2 + ur->other_case)
+ {
+ pp = PRIV(ucd_caseless_sets) + ur->caseset;
+ for (;;)
+ {
+ if (c1 < *pp) return NULL;
+ if (c1 == *pp++) break;
+ }
+ }
+ }
+return src2;
+}
+
+#endif /* SUPPORT_UNICODE */
+
+static PCRE2_SPTR byte_sequence_compare(compiler_common *common, BOOL caseless, PCRE2_SPTR cc,
+ compare_context *context, jump_list **backtracks)
+{
+DEFINE_COMPILER;
+unsigned int othercasebit = 0;
+PCRE2_SPTR othercasechar = NULL;
+#ifdef SUPPORT_UNICODE
+int utflength;
+#endif
+
+if (caseless && char_has_othercase(common, cc))
+ {
+ othercasebit = char_get_othercase_bit(common, cc);
+ SLJIT_ASSERT(othercasebit);
+ /* Extracting bit difference info. */
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ othercasechar = cc + (othercasebit >> 8);
+ othercasebit &= 0xff;
+#elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ /* Note that this code only handles characters in the BMP. If there
+ ever are characters outside the BMP whose othercase differs in only one
+ bit from itself (there currently are none), this code will need to be
+ revised for PCRE2_CODE_UNIT_WIDTH == 32. */
+ othercasechar = cc + (othercasebit >> 9);
+ if ((othercasebit & 0x100) != 0)
+ othercasebit = (othercasebit & 0xff) << 8;
+ else
+ othercasebit &= 0xff;
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
+ }
+
+if (context->sourcereg == -1)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
+ if (context->length >= 4)
+ OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+ else if (context->length >= 2)
+ OP1(SLJIT_MOV_U16, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+ else
+#endif
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
+ if (context->length >= 4)
+ OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+ else
+#endif
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+#elif PCRE2_CODE_UNIT_WIDTH == 32
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length);
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16|32] */
+ context->sourcereg = TMP2;
+ }
+
+#ifdef SUPPORT_UNICODE
+utflength = 1;
+if (common->utf && HAS_EXTRALEN(*cc))
+ utflength += GET_EXTRALEN(*cc);
+
+do
+ {
+#endif
+
+ context->length -= IN_UCHARS(1);
+#if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED) && (PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16)
+
+ /* Unaligned read is supported. */
+ if (othercasebit != 0 && othercasechar == cc)
+ {
+ context->c.asuchars[context->ucharptr] = *cc | othercasebit;
+ context->oc.asuchars[context->ucharptr] = othercasebit;
+ }
+ else
+ {
+ context->c.asuchars[context->ucharptr] = *cc;
+ context->oc.asuchars[context->ucharptr] = 0;
+ }
+ context->ucharptr++;
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (context->ucharptr >= 4 || context->length == 0 || (context->ucharptr == 2 && context->length == 1))
+#else
+ if (context->ucharptr >= 2 || context->length == 0)
+#endif
+ {
+ if (context->length >= 4)
+ OP1(SLJIT_MOV_S32, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
+ else if (context->length >= 2)
+ OP1(SLJIT_MOV_U16, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ else if (context->length >= 1)
+ OP1(SLJIT_MOV_U8, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
+
+ switch(context->ucharptr)
+ {
+ case 4 / sizeof(PCRE2_UCHAR):
+ if (context->oc.asint != 0)
+ OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asint);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asint | context->oc.asint));
+ break;
+
+ case 2 / sizeof(PCRE2_UCHAR):
+ if (context->oc.asushort != 0)
+ OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asushort);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asushort | context->oc.asushort));
+ break;
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ case 1:
+ if (context->oc.asbyte != 0)
+ OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asbyte);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asbyte | context->oc.asbyte));
+ break;
+#endif
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ context->ucharptr = 0;
+ }
+
+#else
+
+ /* Unaligned read is unsupported or in 32 bit mode. */
+ if (context->length >= 1)
+ OP1(MOV_UCHAR, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length);
+
+ context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1;
+
+ if (othercasebit != 0 && othercasechar == cc)
+ {
+ OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, othercasebit);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc | othercasebit));
+ }
+ else
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc));
+
+#endif
+
+ cc++;
+#ifdef SUPPORT_UNICODE
+ utflength--;
+ }
+while (utflength > 0);
+#endif
+
+return cc;
+}
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+
+#define SET_TYPE_OFFSET(value) \
+ if ((value) != typeoffset) \
+ { \
+ if ((value) < typeoffset) \
+ OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \
+ else \
+ OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \
+ } \
+ typeoffset = (value);
+
+#define SET_CHAR_OFFSET(value) \
+ if ((value) != charoffset) \
+ { \
+ if ((value) < charoffset) \
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \
+ else \
+ OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \
+ } \
+ charoffset = (value);
+
+static PCRE2_SPTR compile_char1_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks, BOOL check_str_ptr);
+
+static void compile_xclass_matchingpath(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks)
+{
+DEFINE_COMPILER;
+jump_list *found = NULL;
+jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks;
+sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX;
+struct sljit_jump *jump = NULL;
+PCRE2_SPTR ccbegin;
+int compares, invertcmp, numberofcmps;
+#if defined SUPPORT_UNICODE && (PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16)
+BOOL utf = common->utf;
+#endif
+
+#ifdef SUPPORT_UNICODE
+BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE;
+BOOL charsaved = FALSE;
+int typereg = TMP1;
+const sljit_u32 *other_cases;
+sljit_uw typeoffset;
+#endif
+
+/* Scanning the necessary info. */
+cc++;
+ccbegin = cc;
+compares = 0;
+
+if (cc[-1] & XCL_MAP)
+ {
+ min = 0;
+ cc += 32 / sizeof(PCRE2_UCHAR);
+ }
+
+while (*cc != XCL_END)
+ {
+ compares++;
+ if (*cc == XCL_SINGLE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+ if (c > max) max = c;
+ if (c < min) min = c;
+#ifdef SUPPORT_UNICODE
+ needschar = TRUE;
+#endif
+ }
+ else if (*cc == XCL_RANGE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+ if (c < min) min = c;
+ GETCHARINCTEST(c, cc);
+ if (c > max) max = c;
+#ifdef SUPPORT_UNICODE
+ needschar = TRUE;
+#endif
+ }
+#ifdef SUPPORT_UNICODE
+ else
+ {
+ SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
+ cc++;
+ if (*cc == PT_CLIST)
+ {
+ other_cases = PRIV(ucd_caseless_sets) + cc[1];
+ while (*other_cases != NOTACHAR)
+ {
+ if (*other_cases > max) max = *other_cases;
+ if (*other_cases < min) min = *other_cases;
+ other_cases++;
+ }
+ }
+ else
+ {
+ max = READ_CHAR_MAX;
+ min = 0;
+ }
+
+ switch(*cc)
+ {
+ case PT_ANY:
+ /* Any either accepts everything or ignored. */
+ if (cc[-1] == XCL_PROP)
+ {
+ compile_char1_matchingpath(common, OP_ALLANY, cc, backtracks, FALSE);
+ if (list == backtracks)
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ return;
+ }
+ break;
+
+ case PT_LAMP:
+ case PT_GC:
+ case PT_PC:
+ case PT_ALNUM:
+ needstype = TRUE;
+ break;
+
+ case PT_SC:
+ needsscript = TRUE;
+ break;
+
+ case PT_SPACE:
+ case PT_PXSPACE:
+ case PT_WORD:
+ case PT_PXGRAPH:
+ case PT_PXPRINT:
+ case PT_PXPUNCT:
+ needstype = TRUE;
+ needschar = TRUE;
+ break;
+
+ case PT_CLIST:
+ case PT_UCNC:
+ needschar = TRUE;
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ cc += 2;
+ }
+#endif
+ }
+SLJIT_ASSERT(compares > 0);
+
+/* We are not necessary in utf mode even in 8 bit mode. */
+cc = ccbegin;
+read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0);
+
+if ((cc[-1] & XCL_HASPROP) == 0)
+ {
+ if ((cc[-1] & XCL_MAP) != 0)
+ {
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+ if (!optimize_class(common, (const sljit_u8 *)cc, (((const sljit_u8 *)cc)[31] & 0x80) != 0, TRUE, &found))
+ {
+ OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
+ OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
+ OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
+ add_jump(compiler, &found, JUMP(SLJIT_NOT_ZERO));
+ }
+
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ JUMPHERE(jump);
+
+ cc += 32 / sizeof(PCRE2_UCHAR);
+ }
+ else
+ {
+ OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min);
+ add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, max - min));
+ }
+ }
+else if ((cc[-1] & XCL_MAP) != 0)
+ {
+ OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
+#ifdef SUPPORT_UNICODE
+ charsaved = TRUE;
+#endif
+ if (!optimize_class(common, (const sljit_u8 *)cc, FALSE, TRUE, list))
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ jump = NULL;
+ if (common->utf)
+#endif
+ jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+
+ OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
+ OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
+ OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
+ add_jump(compiler, list, JUMP(SLJIT_NOT_ZERO));
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf)
+#endif
+ JUMPHERE(jump);
+ }
+
+ OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0);
+ cc += 32 / sizeof(PCRE2_UCHAR);
+ }
+
+#ifdef SUPPORT_UNICODE
+if (needstype || needsscript)
+ {
+ if (needschar && !charsaved)
+ OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+ if (!common->utf)
+ {
+ jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, MAX_UTF_CODE_POINT + 1);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR);
+ JUMPHERE(jump);
+ }
+#endif
+
+ OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
+ OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
+ OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
+
+ /* Before anything else, we deal with scripts. */
+ if (needsscript)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script));
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
+
+ ccbegin = cc;
+
+ while (*cc != XCL_END)
+ {
+ if (*cc == XCL_SINGLE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+ }
+ else if (*cc == XCL_RANGE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+ GETCHARINCTEST(c, cc);
+ }
+ else
+ {
+ SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
+ cc++;
+ if (*cc == PT_SC)
+ {
+ compares--;
+ invertcmp = (compares == 0 && list != backtracks);
+ if (cc[-1] == XCL_NOTPROP)
+ invertcmp ^= 0x1;
+ jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (int)cc[1]);
+ add_jump(compiler, compares > 0 ? list : backtracks, jump);
+ }
+ cc += 2;
+ }
+ }
+
+ cc = ccbegin;
+ }
+
+ if (needschar)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0);
+ }
+
+ if (needstype)
+ {
+ if (!needschar)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
+ }
+ else
+ {
+ OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 3);
+ OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
+ typereg = RETURN_ADDR;
+ }
+ }
+ }
+#endif
+
+/* Generating code. */
+charoffset = 0;
+numberofcmps = 0;
+#ifdef SUPPORT_UNICODE
+typeoffset = 0;
+#endif
+
+while (*cc != XCL_END)
+ {
+ compares--;
+ invertcmp = (compares == 0 && list != backtracks);
+ jump = NULL;
+
+ if (*cc == XCL_SINGLE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+
+ if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ numberofcmps++;
+ }
+ else if (numberofcmps > 0)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ numberofcmps = 0;
+ }
+ else
+ {
+ jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ numberofcmps = 0;
+ }
+ }
+ else if (*cc == XCL_RANGE)
+ {
+ cc ++;
+ GETCHARINCTEST(c, cc);
+ SET_CHAR_OFFSET(c);
+ GETCHARINCTEST(c, cc);
+
+ if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE))
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ numberofcmps++;
+ }
+ else if (numberofcmps > 0)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ numberofcmps = 0;
+ }
+ else
+ {
+ jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset));
+ numberofcmps = 0;
+ }
+ }
+#ifdef SUPPORT_UNICODE
+ else
+ {
+ SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP);
+ if (*cc == XCL_NOTPROP)
+ invertcmp ^= 0x1;
+ cc++;
+ switch(*cc)
+ {
+ case PT_ANY:
+ if (!invertcmp)
+ jump = JUMP(SLJIT_JUMP);
+ break;
+
+ case PT_LAMP:
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - typeoffset);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ll - typeoffset);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lt - typeoffset);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ case PT_GC:
+ c = PRIV(ucp_typerange)[(int)cc[1] * 2];
+ SET_TYPE_OFFSET(c);
+ jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, PRIV(ucp_typerange)[(int)cc[1] * 2 + 1] - c);
+ break;
+
+ case PT_PC:
+ jump = CMP(SLJIT_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, (int)cc[1] - typeoffset);
+ break;
+
+ case PT_SC:
+ compares++;
+ /* Do nothing. */
+ break;
+
+ case PT_SPACE:
+ case PT_PXSPACE:
+ SET_CHAR_OFFSET(9);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ SET_TYPE_OFFSET(ucp_Zl);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ case PT_WORD:
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset));
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+ /* Fall through. */
+
+ case PT_ALNUM:
+ SET_TYPE_OFFSET(ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
+ OP_FLAGS((*cc == PT_ALNUM) ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ SET_TYPE_OFFSET(ucp_Nd);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_No - ucp_Nd);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ case PT_CLIST:
+ other_cases = PRIV(ucd_caseless_sets) + cc[1];
+
+ /* At least three characters are required.
+ Otherwise this case would be handled by the normal code path. */
+ SLJIT_ASSERT(other_cases[0] != NOTACHAR && other_cases[1] != NOTACHAR && other_cases[2] != NOTACHAR);
+ SLJIT_ASSERT(other_cases[0] < other_cases[1] && other_cases[1] < other_cases[2]);
+
+ /* Optimizing character pairs, if their difference is power of 2. */
+ if (is_powerof2(other_cases[1] ^ other_cases[0]))
+ {
+ if (charoffset == 0)
+ OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
+ else
+ {
+ OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
+ OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
+ }
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[1]);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+ other_cases += 2;
+ }
+ else if (is_powerof2(other_cases[2] ^ other_cases[1]))
+ {
+ if (charoffset == 0)
+ OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[2] ^ other_cases[1]);
+ else
+ {
+ OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset);
+ OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]);
+ }
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset));
+ OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL);
+
+ other_cases += 3;
+ }
+ else
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+ }
+
+ while (*other_cases != NOTACHAR)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset));
+ OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL);
+ }
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ case PT_UCNC:
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset));
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset));
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset));
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ SET_CHAR_OFFSET(0xa0);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset));
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL);
+ SET_CHAR_OFFSET(0);
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_GREATER_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ case PT_PXGRAPH:
+ /* C and Z groups are the farthest two groups. */
+ SET_TYPE_OFFSET(ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER);
+
+ jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
+
+ /* In case of ucp_Cf, we overwrite the result. */
+ SET_CHAR_OFFSET(0x2066);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ JUMPHERE(jump);
+ jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
+ break;
+
+ case PT_PXPRINT:
+ /* C and Z groups are the farthest two groups. */
+ SET_TYPE_OFFSET(ucp_Ll);
+ OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll);
+ OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_NOT_EQUAL);
+
+ jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
+
+ /* In case of ucp_Cf, we overwrite the result. */
+ SET_CHAR_OFFSET(0x2066);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
+ OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
+
+ JUMPHERE(jump);
+ jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
+ break;
+
+ case PT_PXPUNCT:
+ SET_TYPE_OFFSET(ucp_Sc);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL);
+
+ SET_CHAR_OFFSET(0);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x7f);
+ OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_LESS_EQUAL);
+
+ SET_TYPE_OFFSET(ucp_Pc);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL);
+ jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp);
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ cc += 2;
+ }
+#endif
+
+ if (jump != NULL)
+ add_jump(compiler, compares > 0 ? list : backtracks, jump);
+ }
+
+if (found != NULL)
+ set_jumps(found, LABEL());
+}
+
+#undef SET_TYPE_OFFSET
+#undef SET_CHAR_OFFSET
+
+#endif
+
+static PCRE2_SPTR compile_simple_assertion_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks)
+{
+DEFINE_COMPILER;
+int length;
+struct sljit_jump *jump[4];
+#ifdef SUPPORT_UNICODE
+struct sljit_label *label;
+#endif /* SUPPORT_UNICODE */
+
+switch(type)
+ {
+ case OP_SOD:
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
+ return cc;
+
+ case OP_SOM:
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0));
+ return cc;
+
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ add_jump(compiler, &common->wordboundary, JUMP(SLJIT_FAST_CALL));
+ sljit_set_current_flags(compiler, SLJIT_SET_Z);
+ add_jump(compiler, backtracks, JUMP(type == OP_NOT_WORD_BOUNDARY ? SLJIT_NOT_ZERO : SLJIT_ZERO));
+ return cc;
+
+ case OP_EODN:
+ /* Requires rather complex checks. */
+ jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
+ if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
+ else
+ {
+ jump[1] = CMP(SLJIT_EQUAL, TMP2, 0, STR_END, 0);
+ OP2(SLJIT_SUB | SLJIT_SET_LESS, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
+ OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
+ OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_NOT_EQUAL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL));
+ check_partial(common, TRUE);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ JUMPHERE(jump[1]);
+ }
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
+ }
+ else if (common->nltype == NLTYPE_FIXED)
+ {
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
+ }
+ else
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+ OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0);
+ jump[2] = JUMP(SLJIT_GREATER);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL) /* LESS */);
+ /* Equal. */
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ jump[3] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+
+ JUMPHERE(jump[1]);
+ if (common->nltype == NLTYPE_ANYCRLF)
+ {
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, STR_END, 0));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL));
+ }
+ else
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0);
+ read_char_range(common, common->nlmin, common->nlmax, TRUE);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
+ add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
+ sljit_set_current_flags(compiler, SLJIT_SET_Z);
+ add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
+ }
+ JUMPHERE(jump[2]);
+ JUMPHERE(jump[3]);
+ }
+ JUMPHERE(jump[0]);
+ check_partial(common, FALSE);
+ return cc;
+
+ case OP_EOD:
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
+ check_partial(common, FALSE);
+ return cc;
+
+ case OP_DOLL:
+ OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
+ OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTEOL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
+
+ if (!common->endonly)
+ compile_simple_assertion_matchingpath(common, OP_EODN, cc, backtracks);
+ else
+ {
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0));
+ check_partial(common, FALSE);
+ }
+ return cc;
+
+ case OP_DOLLM:
+ jump[1] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0);
+ OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
+ OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTEOL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
+ check_partial(common, FALSE);
+ jump[0] = JUMP(SLJIT_JUMP);
+ JUMPHERE(jump[1]);
+
+ if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0));
+ else
+ {
+ jump[1] = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0);
+ /* STR_PTR = STR_END - IN_UCHARS(1) */
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
+ check_partial(common, TRUE);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ JUMPHERE(jump[1]);
+ }
+
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
+ }
+ else
+ {
+ peek_char(common, common->nlmax);
+ check_newlinechar(common, common->nltype, backtracks, FALSE);
+ }
+ JUMPHERE(jump[0]);
+ return cc;
+
+ case OP_CIRC:
+ OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0));
+ OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTBOL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
+ return cc;
+
+ case OP_CIRCM:
+ OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin));
+ jump[1] = CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0);
+ OP2(SLJIT_AND32 | SLJIT_SET_Z, SLJIT_UNUSED, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, options), SLJIT_IMM, PCRE2_NOTBOL);
+ add_jump(compiler, backtracks, JUMP(SLJIT_NOT_ZERO32));
+ jump[0] = JUMP(SLJIT_JUMP);
+ JUMPHERE(jump[1]);
+
+ if (!common->alt_circumflex)
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+
+ if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ OP2(SLJIT_SUB, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, TMP1, 0));
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
+ OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff));
+ }
+ else
+ {
+ skip_char_back(common);
+ read_char_range(common, common->nlmin, common->nlmax, TRUE);
+ check_newlinechar(common, common->nltype, backtracks, FALSE);
+ }
+ JUMPHERE(jump[0]);
+ return cc;
+
+ case OP_REVERSE:
+ length = GET(cc, 0);
+ if (length == 0)
+ return cc + LINK_SIZE;
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+#ifdef SUPPORT_UNICODE
+ if (common->utf)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, length);
+ label = LABEL();
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP3, 0));
+ skip_char_back(common);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ }
+ else
+#endif
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, TMP1, 0));
+ }
+ check_start_used_ptr(common);
+ return cc + LINK_SIZE;
+ }
+SLJIT_UNREACHABLE();
+return cc;
+}
+
+#ifdef SUPPORT_UNICODE
+
+#if PCRE2_CODE_UNIT_WIDTH != 32
+
+static PCRE2_SPTR SLJIT_FUNC do_extuni_utf(jit_arguments *args, PCRE2_SPTR cc)
+{
+PCRE2_SPTR start_subject = args->begin;
+PCRE2_SPTR end_subject = args->end;
+int lgb, rgb, len, ricount;
+PCRE2_SPTR prevcc, bptr;
+uint32_t c;
+
+prevcc = cc;
+GETCHARINC(c, cc);
+lgb = UCD_GRAPHBREAK(c);
+
+while (cc < end_subject)
+ {
+ len = 1;
+ GETCHARLEN(c, cc, len);
+ rgb = UCD_GRAPHBREAK(c);
+
+ if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
+
+ /* Not breaking between Regional Indicators is allowed only if there
+ are an even number of preceding RIs. */
+
+ if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
+ {
+ ricount = 0;
+ bptr = prevcc;
+
+ /* bptr is pointing to the left-hand character */
+ while (bptr > start_subject)
+ {
+ bptr--;
+ BACKCHAR(bptr);
+ GETCHAR(c, bptr);
+
+ if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
+
+ ricount++;
+ }
+
+ if ((ricount & 1) != 0) break; /* Grapheme break required */
+ }
+
+ /* If Extend or ZWJ follows Extended_Pictographic, do not update lgb; this
+ allows any number of them before a following Extended_Pictographic. */
+
+ if ((rgb != ucp_gbExtend && rgb != ucp_gbZWJ) ||
+ lgb != ucp_gbExtended_Pictographic)
+ lgb = rgb;
+
+ prevcc = cc;
+ cc += len;
+ }
+
+return cc;
+}
+
+#endif
+
+static PCRE2_SPTR SLJIT_FUNC do_extuni_no_utf(jit_arguments *args, PCRE2_SPTR cc)
+{
+PCRE2_SPTR start_subject = args->begin;
+PCRE2_SPTR end_subject = args->end;
+int lgb, rgb, ricount;
+PCRE2_SPTR bptr;
+uint32_t c;
+
+GETCHARINC(c, cc);
+lgb = UCD_GRAPHBREAK(c);
+
+while (cc < end_subject)
+ {
+ c = *cc;
+ rgb = UCD_GRAPHBREAK(c);
+
+ if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
+
+ /* Not breaking between Regional Indicators is allowed only if there
+ are an even number of preceding RIs. */
+
+ if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
+ {
+ ricount = 0;
+ bptr = cc - 1;
+
+ /* bptr is pointing to the left-hand character */
+ while (bptr > start_subject)
+ {
+ bptr--;
+ c = *bptr;
+
+ if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
+
+ ricount++;
+ }
+
+ if ((ricount & 1) != 0) break; /* Grapheme break required */
+ }
+
+ /* If Extend or ZWJ follows Extended_Pictographic, do not update lgb; this
+ allows any number of them before a following Extended_Pictographic. */
+
+ if ((rgb != ucp_gbExtend && rgb != ucp_gbZWJ) ||
+ lgb != ucp_gbExtended_Pictographic)
+ lgb = rgb;
+
+ cc++;
+ }
+
+return cc;
+}
+
+#endif
+
+static PCRE2_SPTR compile_char1_matchingpath(compiler_common *common, PCRE2_UCHAR type, PCRE2_SPTR cc, jump_list **backtracks, BOOL check_str_ptr)
+{
+DEFINE_COMPILER;
+int length;
+unsigned int c, oc, bit;
+compare_context context;
+struct sljit_jump *jump[3];
+jump_list *end_list;
+#ifdef SUPPORT_UNICODE
+PCRE2_UCHAR propdata[5];
+#endif /* SUPPORT_UNICODE */
+
+switch(type)
+ {
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ /* Digits are usually 0-9, so it is worth to optimize them. */
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_digit, FALSE))
+ read_char7_type(common, type == OP_NOT_DIGIT);
+ else
+#endif
+ read_char8_type(common, type == OP_NOT_DIGIT);
+ /* Flip the starting bit in the negative case. */
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit);
+ add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_ZERO : SLJIT_NOT_ZERO));
+ return cc;
+
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_space, FALSE))
+ read_char7_type(common, type == OP_NOT_WHITESPACE);
+ else
+#endif
+ read_char8_type(common, type == OP_NOT_WHITESPACE);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space);
+ add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_ZERO : SLJIT_NOT_ZERO));
+ return cc;
+
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (common->utf && is_char7_bitset((const sljit_u8*)common->ctypes - cbit_length + cbit_word, FALSE))
+ read_char7_type(common, type == OP_NOT_WORDCHAR);
+ else
+#endif
+ read_char8_type(common, type == OP_NOT_WORDCHAR);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word);
+ add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_ZERO : SLJIT_NOT_ZERO));
+ return cc;
+
+ case OP_ANY:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ read_char_range(common, common->nlmin, common->nlmax, TRUE);
+ if (common->nltype == NLTYPE_FIXED && common->newline > 255)
+ {
+ jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff);
+ end_list = NULL;
+ if (common->mode != PCRE2_JIT_PARTIAL_HARD)
+ add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+ else
+ check_str_end(common, &end_list);
+
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff));
+ set_jumps(end_list, LABEL());
+ JUMPHERE(jump[0]);
+ }
+ else
+ check_newlinechar(common, common->nltype, backtracks, TRUE);
+ return cc;
+
+ case OP_ALLANY:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf)
+ {
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+#if PCRE2_CODE_UNIT_WIDTH == 8 || PCRE2_CODE_UNIT_WIDTH == 16
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
+ OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
+ OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL);
+ OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+#endif
+ JUMPHERE(jump[0]);
+#endif /* PCRE2_CODE_UNIT_WIDTH == [8|16] */
+ return cc;
+ }
+#endif
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ return cc;
+
+ case OP_ANYBYTE:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ return cc;
+
+#ifdef SUPPORT_UNICODE
+ case OP_NOTPROP:
+ case OP_PROP:
+ propdata[0] = XCL_HASPROP;
+ propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP;
+ propdata[2] = cc[0];
+ propdata[3] = cc[1];
+ propdata[4] = XCL_END;
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ compile_xclass_matchingpath(common, propdata, backtracks);
+ return cc + 2;
+#endif
+
+ case OP_ANYNL:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE);
+ jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR);
+ /* We don't need to handle soft partial matching case. */
+ end_list = NULL;
+ if (common->mode != PCRE2_JIT_PARTIAL_HARD)
+ add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
+ else
+ check_str_end(common, &end_list);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ jump[2] = JUMP(SLJIT_JUMP);
+ JUMPHERE(jump[0]);
+ check_newlinechar(common, common->bsr_nltype, backtracks, FALSE);
+ set_jumps(end_list, LABEL());
+ JUMPHERE(jump[1]);
+ JUMPHERE(jump[2]);
+ return cc;
+
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE);
+ add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL));
+ sljit_set_current_flags(compiler, SLJIT_SET_Z);
+ add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
+ return cc;
+
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE);
+ add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL));
+ sljit_set_current_flags(compiler, SLJIT_SET_Z);
+ add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO));
+ return cc;
+
+#ifdef SUPPORT_UNICODE
+ case OP_EXTUNI:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+
+ SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
+ OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
+
+#if PCRE2_CODE_UNIT_WIDTH != 32
+ sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM,
+ common->utf ? SLJIT_FUNC_OFFSET(do_extuni_utf) : SLJIT_FUNC_OFFSET(do_extuni_no_utf));
+#else
+ sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_extuni_no_utf));
+#endif
+
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
+
+ if (common->mode == PCRE2_JIT_PARTIAL_HARD)
+ {
+ jump[0] = CMP(SLJIT_LESS, SLJIT_RETURN_REG, 0, STR_END, 0);
+ /* Since we successfully read a char above, partial matching must occure. */
+ check_partial(common, TRUE);
+ JUMPHERE(jump[0]);
+ }
+ return cc;
+#endif
+
+ case OP_CHAR:
+ case OP_CHARI:
+ length = 1;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(*cc)) length += GET_EXTRALEN(*cc);
+#endif
+ if (common->mode == PCRE2_JIT_COMPLETE && check_str_ptr
+ && (type == OP_CHAR || !char_has_othercase(common, cc) || char_get_othercase_bit(common, cc) != 0))
+ {
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length));
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
+
+ context.length = IN_UCHARS(length);
+ context.sourcereg = -1;
+#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
+ context.ucharptr = 0;
+#endif
+ return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks);
+ }
+
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+#ifdef SUPPORT_UNICODE
+ if (common->utf)
+ {
+ GETCHAR(c, cc);
+ }
+ else
+#endif
+ c = *cc;
+
+ if (type == OP_CHAR || !char_has_othercase(common, cc))
+ {
+ read_char_range(common, c, c, FALSE);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c));
+ return cc + length;
+ }
+ oc = char_othercase(common, c);
+ read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE);
+ bit = c ^ oc;
+ if (is_powerof2(bit))
+ {
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
+ return cc + length;
+ }
+ jump[0] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c);
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
+ JUMPHERE(jump[0]);
+ return cc + length;
+
+ case OP_NOT:
+ case OP_NOTI:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+
+ length = 1;
+#ifdef SUPPORT_UNICODE
+ if (common->utf)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ c = *cc;
+ if (c < 128)
+ {
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
+ if (type == OP_NOT || !char_has_othercase(common, cc))
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
+ else
+ {
+ /* Since UTF8 code page is fixed, we know that c is in [a-z] or [A-Z] range. */
+ OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x20);
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, c | 0x20));
+ }
+ /* Skip the variable-length character. */
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
+ JUMPHERE(jump[0]);
+ return cc + 1;
+ }
+ else
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ {
+ GETCHARLEN(c, cc, length);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ c = *cc;
+
+ if (type == OP_NOT || !char_has_othercase(common, cc))
+ {
+ read_char_range(common, c, c, TRUE);
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
+ }
+ else
+ {
+ oc = char_othercase(common, c);
+ read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE);
+ bit = c ^ oc;
+ if (is_powerof2(bit))
+ {
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit);
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit));
+ }
+ else
+ {
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c));
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, oc));
+ }
+ }
+ return cc + length;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ bit = (common->utf && is_char7_bitset((const sljit_u8 *)cc, type == OP_NCLASS)) ? 127 : 255;
+ read_char_range(common, 0, bit, type == OP_NCLASS);
+#else
+ read_char_range(common, 0, 255, type == OP_NCLASS);
+#endif
+
+ if (optimize_class(common, (const sljit_u8 *)cc, type == OP_NCLASS, FALSE, backtracks))
+ return cc + 32 / sizeof(PCRE2_UCHAR);
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ jump[0] = NULL;
+ if (common->utf)
+ {
+ jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, bit);
+ if (type == OP_CLASS)
+ {
+ add_jump(compiler, backtracks, jump[0]);
+ jump[0] = NULL;
+ }
+ }
+#elif PCRE2_CODE_UNIT_WIDTH != 8
+ jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255);
+ if (type == OP_CLASS)
+ {
+ add_jump(compiler, backtracks, jump[0]);
+ jump[0] = NULL;
+ }
+#endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
+
+ OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
+ OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
+ OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc);
+ OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
+ add_jump(compiler, backtracks, JUMP(SLJIT_ZERO));
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ if (jump[0] != NULL)
+ JUMPHERE(jump[0]);
+#endif
+ return cc + 32 / sizeof(PCRE2_UCHAR);
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ case OP_XCLASS:
+ if (check_str_ptr)
+ detect_partial_match(common, backtracks);
+ compile_xclass_matchingpath(common, cc + LINK_SIZE, backtracks);
+ return cc + GET(cc, 0) - 1;
+#endif
+ }
+SLJIT_UNREACHABLE();
+return cc;
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_charn_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, jump_list **backtracks)
+{
+/* This function consumes at least one input character. */
+/* To decrease the number of length checks, we try to concatenate the fixed length character sequences. */
+DEFINE_COMPILER;
+PCRE2_SPTR ccbegin = cc;
+compare_context context;
+int size;
+
+context.length = 0;
+do
+ {
+ if (cc >= ccend)
+ break;
+
+ if (*cc == OP_CHAR)
+ {
+ size = 1;
+#ifdef SUPPORT_UNICODE
+ if (common->utf && HAS_EXTRALEN(cc[1]))
+ size += GET_EXTRALEN(cc[1]);
+#endif
+ }
+ else if (*cc == OP_CHARI)
+ {
+ size = 1;
+#ifdef SUPPORT_UNICODE
+ if (common->utf)
+ {
+ if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
+ size = 0;
+ else if (HAS_EXTRALEN(cc[1]))
+ size += GET_EXTRALEN(cc[1]);
+ }
+ else
+#endif
+ if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0)
+ size = 0;
+ }
+ else
+ size = 0;
+
+ cc += 1 + size;
+ context.length += IN_UCHARS(size);
+ }
+while (size > 0 && context.length <= 128);
+
+cc = ccbegin;
+if (context.length > 0)
+ {
+ /* We have a fixed-length byte sequence. */
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, context.length);
+ add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0));
+
+ context.sourcereg = -1;
+#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
+ context.ucharptr = 0;
+#endif
+ do cc = byte_sequence_compare(common, *cc == OP_CHARI, cc + 1, &context, backtracks); while (context.length > 0);
+ return cc;
+ }
+
+/* A non-fixed length character will be checked if length == 0. */
+return compile_char1_matchingpath(common, *cc, cc + 1, backtracks, TRUE);
+}
+
+/* Forward definitions. */
+static void compile_matchingpath(compiler_common *, PCRE2_SPTR, PCRE2_SPTR, backtrack_common *);
+static void compile_backtrackingpath(compiler_common *, struct backtrack_common *);
+
+#define PUSH_BACKTRACK(size, ccstart, error) \
+ do \
+ { \
+ backtrack = sljit_alloc_memory(compiler, (size)); \
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
+ return error; \
+ memset(backtrack, 0, size); \
+ backtrack->prev = parent->top; \
+ backtrack->cc = (ccstart); \
+ parent->top = backtrack; \
+ } \
+ while (0)
+
+#define PUSH_BACKTRACK_NOVALUE(size, ccstart) \
+ do \
+ { \
+ backtrack = sljit_alloc_memory(compiler, (size)); \
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
+ return; \
+ memset(backtrack, 0, size); \
+ backtrack->prev = parent->top; \
+ backtrack->cc = (ccstart); \
+ parent->top = backtrack; \
+ } \
+ while (0)
+
+#define BACKTRACK_AS(type) ((type *)backtrack)
+
+static void compile_dnref_search(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks)
+{
+/* The OVECTOR offset goes to TMP2. */
+DEFINE_COMPILER;
+int count = GET2(cc, 1 + IMM2_SIZE);
+PCRE2_SPTR slot = common->name_table + GET2(cc, 1) * common->name_entry_size;
+unsigned int offset;
+jump_list *found = NULL;
+
+SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI);
+
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
+
+count--;
+while (count-- > 0)
+ {
+ offset = GET2(slot, 0) << 1;
+ GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
+ add_jump(compiler, &found, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
+ slot += common->name_entry_size;
+ }
+
+offset = GET2(slot, 0) << 1;
+GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset));
+if (backtracks != NULL && !common->unset_backref)
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0));
+
+set_jumps(found, LABEL());
+}
+
+static void compile_ref_matchingpath(compiler_common *common, PCRE2_SPTR cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail)
+{
+DEFINE_COMPILER;
+BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
+int offset = 0;
+struct sljit_jump *jump = NULL;
+struct sljit_jump *partial;
+struct sljit_jump *nopartial;
+
+if (ref)
+ {
+ offset = GET2(cc, 1) << 1;
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ /* OVECTOR(1) contains the "string begin - 1" constant. */
+ if (withchecks && !common->unset_backref)
+ add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
+ }
+else
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
+
+#if defined SUPPORT_UNICODE
+if (common->utf && *cc == OP_REFI)
+ {
+ SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
+ if (ref)
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ else
+ OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+
+ if (withchecks)
+ jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_R2, 0);
+ /* No free saved registers so save data on stack. */
+
+ OP1(SLJIT_MOV, SLJIT_R3, 0, STR_END, 0);
+ sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW) | SLJIT_ARG4(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp));
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0);
+
+ if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1));
+ else
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_LESS, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
+
+ add_jump(compiler, backtracks, JUMP(SLJIT_LESS));
+
+ nopartial = JUMP(SLJIT_NOT_EQUAL);
+ OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
+ check_partial(common, FALSE);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ JUMPHERE(nopartial);
+ }
+ }
+else
+#endif /* SUPPORT_UNICODE */
+ {
+ if (ref)
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0);
+ else
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0);
+
+ if (withchecks)
+ jump = JUMP(SLJIT_ZERO);
+
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
+ partial = CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0);
+ if (common->mode == PCRE2_JIT_COMPLETE)
+ add_jump(compiler, backtracks, partial);
+
+ add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
+
+ if (common->mode != PCRE2_JIT_COMPLETE)
+ {
+ nopartial = JUMP(SLJIT_JUMP);
+ JUMPHERE(partial);
+ /* TMP2 -= STR_END - STR_PTR */
+ OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, STR_PTR, 0);
+ OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, STR_END, 0);
+ partial = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0);
+ add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL));
+ add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0));
+ JUMPHERE(partial);
+ check_partial(common, FALSE);
+ add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
+ JUMPHERE(nopartial);
+ }
+ }
+
+if (jump != NULL)
+ {
+ if (emptyfail)
+ add_jump(compiler, backtracks, jump);
+ else
+ JUMPHERE(jump);
+ }
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_ref_iterator_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
+backtrack_common *backtrack;
+PCRE2_UCHAR type;
+int offset = 0;
+struct sljit_label *label;
+struct sljit_jump *zerolength;
+struct sljit_jump *jump = NULL;
+PCRE2_SPTR ccbegin = cc;
+int min = 0, max = 0;
+BOOL minimize;
+
+PUSH_BACKTRACK(sizeof(ref_iterator_backtrack), cc, NULL);
+
+if (ref)
+ offset = GET2(cc, 1) << 1;
+else
+ cc += IMM2_SIZE;
+type = cc[1 + IMM2_SIZE];
+
+SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even);
+minimize = (type & 0x1) != 0;
+switch(type)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ min = 0;
+ max = 0;
+ cc += 1 + IMM2_SIZE + 1;
+ break;
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ min = 1;
+ max = 0;
+ cc += 1 + IMM2_SIZE + 1;
+ break;
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ min = 0;
+ max = 1;
+ cc += 1 + IMM2_SIZE + 1;
+ break;
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ min = GET2(cc, 1 + IMM2_SIZE + 1);
+ max = GET2(cc, 1 + IMM2_SIZE + 1 + IMM2_SIZE);
+ cc += 1 + IMM2_SIZE + 1 + 2 * IMM2_SIZE;
+ break;
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+
+if (!minimize)
+ {
+ if (min == 0)
+ {
+ allocate_stack(common, 2);
+ if (ref)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
+ /* Temporary release of STR_PTR. */
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ /* Handles both invalid and empty cases. Since the minimum repeat,
+ is zero the invalid case is basically the same as an empty case. */
+ if (ref)
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ else
+ {
+ compile_dnref_search(common, ccbegin, NULL);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+ }
+ /* Restore if not zero length. */
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ }
+ else
+ {
+ allocate_stack(common, 1);
+ if (ref)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ if (ref)
+ {
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ }
+ else
+ {
+ compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0);
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+ }
+ }
+
+ if (min > 1 || max > 1)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, 0);
+
+ label = LABEL();
+ if (!ref)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1);
+ compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE);
+
+ if (min > 1 || max > 1)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0);
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0);
+ if (min > 1)
+ CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, label);
+ if (max > 1)
+ {
+ jump = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max);
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, label);
+ JUMPHERE(jump);
+ }
+ }
+
+ if (max == 0)
+ {
+ /* Includes min > 1 case as well. */
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, label);
+ }
+
+ JUMPHERE(zerolength);
+ BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL();
+
+ count_match(common);
+ return cc;
+ }
+
+allocate_stack(common, ref ? 2 : 3);
+if (ref)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+if (type != OP_CRMINSTAR)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
+
+if (min == 0)
+ {
+ /* Handles both invalid and empty cases. Since the minimum repeat,
+ is zero the invalid case is basically the same as an empty case. */
+ if (ref)
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ else
+ {
+ compile_dnref_search(common, ccbegin, NULL);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+ }
+ /* Length is non-zero, we can match real repeats. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ jump = JUMP(SLJIT_JUMP);
+ }
+else
+ {
+ if (ref)
+ {
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ }
+ else
+ {
+ compile_dnref_search(common, ccbegin, &backtrack->topbacktracks);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0);
+ zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw));
+ }
+ }
+
+BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL();
+if (max > 0)
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max));
+
+if (!ref)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
+compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE);
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+
+if (min > 1)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
+ CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, BACKTRACK_AS(ref_iterator_backtrack)->matchingpath);
+ }
+else if (max > 0)
+ OP2(SLJIT_ADD, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 1);
+
+if (jump != NULL)
+ JUMPHERE(jump);
+JUMPHERE(zerolength);
+
+count_match(common);
+return cc;
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_recurse_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+recurse_entry *entry = common->entries;
+recurse_entry *prev = NULL;
+sljit_sw start = GET(cc, 1);
+PCRE2_SPTR start_cc;
+BOOL needs_control_head;
+
+PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL);
+
+/* Inlining simple patterns. */
+if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack)
+ {
+ start_cc = common->start + start;
+ compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack);
+ BACKTRACK_AS(recurse_backtrack)->inlined_pattern = TRUE;
+ return cc + 1 + LINK_SIZE;
+ }
+
+while (entry != NULL)
+ {
+ if (entry->start == start)
+ break;
+ prev = entry;
+ entry = entry->next;
+ }
+
+if (entry == NULL)
+ {
+ entry = sljit_alloc_memory(compiler, sizeof(recurse_entry));
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+ entry->next = NULL;
+ entry->entry_label = NULL;
+ entry->backtrack_label = NULL;
+ entry->entry_calls = NULL;
+ entry->backtrack_calls = NULL;
+ entry->start = start;
+
+ if (prev != NULL)
+ prev->next = entry;
+ else
+ common->entries = entry;
+ }
+
+BACKTRACK_AS(recurse_backtrack)->entry = entry;
+
+if (entry->entry_label == NULL)
+ add_jump(compiler, &entry->entry_calls, JUMP(SLJIT_FAST_CALL));
+else
+ JUMPTO(SLJIT_FAST_CALL, entry->entry_label);
+/* Leave if the match is failed. */
+add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0));
+BACKTRACK_AS(recurse_backtrack)->matchingpath = LABEL();
+return cc + 1 + LINK_SIZE;
+}
+
+static sljit_s32 SLJIT_FUNC do_callout(struct jit_arguments *arguments, pcre2_callout_block *callout_block, PCRE2_SPTR *jit_ovector)
+{
+PCRE2_SPTR begin;
+PCRE2_SIZE *ovector;
+sljit_u32 oveccount, capture_top;
+
+if (arguments->callout == NULL)
+ return 0;
+
+SLJIT_COMPILE_ASSERT(sizeof (PCRE2_SIZE) <= sizeof (sljit_sw), pcre2_size_must_be_lower_than_sljit_sw_size);
+
+begin = arguments->begin;
+ovector = (PCRE2_SIZE*)(callout_block + 1);
+oveccount = callout_block->capture_top;
+
+SLJIT_ASSERT(oveccount >= 1);
+
+callout_block->version = 2;
+callout_block->callout_flags = 0;
+
+/* Offsets in subject. */
+callout_block->subject_length = arguments->end - arguments->begin;
+callout_block->start_match = jit_ovector[0] - begin;
+callout_block->current_position = (PCRE2_SPTR)callout_block->offset_vector - begin;
+callout_block->subject = begin;
+
+/* Convert and copy the JIT offset vector to the ovector array. */
+callout_block->capture_top = 1;
+callout_block->offset_vector = ovector;
+
+ovector[0] = PCRE2_UNSET;
+ovector[1] = PCRE2_UNSET;
+ovector += 2;
+jit_ovector += 2;
+capture_top = 1;
+
+/* Convert pointers to sizes. */
+while (--oveccount != 0)
+ {
+ capture_top++;
+
+ ovector[0] = (PCRE2_SIZE)(jit_ovector[0] - begin);
+ ovector[1] = (PCRE2_SIZE)(jit_ovector[1] - begin);
+
+ if (ovector[0] != PCRE2_UNSET)
+ callout_block->capture_top = capture_top;
+
+ ovector += 2;
+ jit_ovector += 2;
+ }
+
+return (arguments->callout)(callout_block, arguments->callout_data);
+}
+
+#define CALLOUT_ARG_OFFSET(arg) \
+ SLJIT_OFFSETOF(pcre2_callout_block, arg)
+
+static SLJIT_INLINE PCRE2_SPTR compile_callout_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+sljit_s32 mov_opcode;
+unsigned int callout_length = (*cc == OP_CALLOUT)
+ ? PRIV(OP_lengths)[OP_CALLOUT] : GET(cc, 1 + 2 * LINK_SIZE);
+sljit_sw value1;
+sljit_sw value2;
+sljit_sw value3;
+sljit_uw callout_arg_size = (common->re->top_bracket + 1) * 2 * sizeof(sljit_sw);
+
+PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
+
+callout_arg_size = (sizeof(pcre2_callout_block) + callout_arg_size + sizeof(sljit_sw) - 1) / sizeof(sljit_sw);
+
+allocate_stack(common, callout_arg_size);
+
+SLJIT_ASSERT(common->capture_last_ptr != 0);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
+OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+value1 = (*cc == OP_CALLOUT) ? cc[1 + 2 * LINK_SIZE] : 0;
+OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, value1);
+OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0);
+OP1(SLJIT_MOV_U32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_top), SLJIT_IMM, common->re->top_bracket + 1);
+
+/* These pointer sized fields temporarly stores internal variables. */
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(offset_vector), STR_PTR, 0);
+
+if (common->mark_ptr != 0)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr));
+mov_opcode = (sizeof(PCRE2_SIZE) == 4) ? SLJIT_MOV_U32 : SLJIT_MOV;
+OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 1));
+OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 1 + LINK_SIZE));
+
+if (*cc == OP_CALLOUT)
+ {
+ value1 = 0;
+ value2 = 0;
+ value3 = 0;
+ }
+else
+ {
+ value1 = (sljit_sw) (cc + (1 + 4*LINK_SIZE) + 1);
+ value2 = (callout_length - (1 + 4*LINK_SIZE + 2));
+ value3 = (sljit_sw) (GET(cc, 1 + 3*LINK_SIZE));
+ }
+
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string), SLJIT_IMM, value1);
+OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string_length), SLJIT_IMM, value2);
+OP1(mov_opcode, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_string_offset), SLJIT_IMM, value3);
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0);
+
+SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
+
+/* Needed to save important temporary registers. */
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STR_PTR, 0);
+/* SLJIT_R0 = arguments */
+OP1(SLJIT_MOV, SLJIT_R1, 0, STACK_TOP, 0);
+GET_LOCAL_BASE(SLJIT_R2, 0, OVECTOR_START);
+sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(S32) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout));
+OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+free_stack(common, callout_arg_size);
+
+/* Check return value. */
+OP2(SLJIT_SUB32 | SLJIT_SET_Z | SLJIT_SET_SIG_GREATER, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
+add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER32));
+if (common->abort_label == NULL)
+ add_jump(compiler, &common->abort, JUMP(SLJIT_NOT_EQUAL32) /* SIG_LESS */);
+else
+ JUMPTO(SLJIT_NOT_EQUAL32 /* SIG_LESS */, common->abort_label);
+return cc + callout_length;
+}
+
+#undef CALLOUT_ARG_SIZE
+#undef CALLOUT_ARG_OFFSET
+
+static SLJIT_INLINE BOOL assert_needs_str_ptr_saving(PCRE2_SPTR cc)
+{
+while (TRUE)
+ {
+ switch (*cc)
+ {
+ case OP_CALLOUT_STR:
+ cc += GET(cc, 1 + 2*LINK_SIZE);
+ break;
+
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_CALLOUT:
+ case OP_ALT:
+ cc += PRIV(OP_lengths)[*cc];
+ break;
+
+ case OP_KET:
+ return FALSE;
+
+ default:
+ return TRUE;
+ }
+ }
+}
+
+static PCRE2_SPTR compile_assert_matchingpath(compiler_common *common, PCRE2_SPTR cc, assert_backtrack *backtrack, BOOL conditional)
+{
+DEFINE_COMPILER;
+int framesize;
+int extrasize;
+BOOL local_quit_available = FALSE;
+BOOL needs_control_head;
+int private_data_ptr;
+backtrack_common altbacktrack;
+PCRE2_SPTR ccbegin;
+PCRE2_UCHAR opcode;
+PCRE2_UCHAR bra = OP_BRA;
+jump_list *tmp = NULL;
+jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks;
+jump_list **found;
+/* Saving previous accept variables. */
+BOOL save_local_quit_available = common->local_quit_available;
+BOOL save_in_positive_assertion = common->in_positive_assertion;
+then_trap_backtrack *save_then_trap = common->then_trap;
+struct sljit_label *save_quit_label = common->quit_label;
+struct sljit_label *save_accept_label = common->accept_label;
+jump_list *save_quit = common->quit;
+jump_list *save_positive_assertion_quit = common->positive_assertion_quit;
+jump_list *save_accept = common->accept;
+struct sljit_jump *jump;
+struct sljit_jump *brajump = NULL;
+
+/* Assert captures then. */
+common->then_trap = NULL;
+
+if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
+ {
+ SLJIT_ASSERT(!conditional);
+ bra = *cc;
+ cc++;
+ }
+private_data_ptr = PRIVATE_DATA(cc);
+SLJIT_ASSERT(private_data_ptr != 0);
+framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
+backtrack->framesize = framesize;
+backtrack->private_data_ptr = private_data_ptr;
+opcode = *cc;
+SLJIT_ASSERT(opcode >= OP_ASSERT && opcode <= OP_ASSERTBACK_NOT);
+found = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) ? &tmp : target;
+ccbegin = cc;
+cc += GET(cc, 1);
+
+if (bra == OP_BRAMINZERO)
+ {
+ /* This is a braminzero backtrack path. */
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ }
+
+if (framesize < 0)
+ {
+ extrasize = 1;
+ if (bra == OP_BRA && !assert_needs_str_ptr_saving(ccbegin + 1 + LINK_SIZE))
+ extrasize = 0;
+
+ if (needs_control_head)
+ extrasize++;
+
+ if (framesize == no_frame)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
+
+ if (extrasize > 0)
+ allocate_stack(common, extrasize);
+
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+
+ if (extrasize > 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+
+ if (needs_control_head)
+ {
+ SLJIT_ASSERT(extrasize == 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
+ }
+ }
+else
+ {
+ extrasize = needs_control_head ? 3 : 2;
+ allocate_stack(common, framesize + extrasize);
+
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+
+ if (needs_control_head)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
+ }
+ else
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0);
+
+ init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize);
+ }
+
+memset(&altbacktrack, 0, sizeof(backtrack_common));
+if (conditional || (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT))
+ {
+ /* Control verbs cannot escape from these asserts. */
+ local_quit_available = TRUE;
+ common->local_quit_available = TRUE;
+ common->quit_label = NULL;
+ common->quit = NULL;
+ }
+
+common->in_positive_assertion = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK);
+common->positive_assertion_quit = NULL;
+
+while (1)
+ {
+ common->accept_label = NULL;
+ common->accept = NULL;
+ altbacktrack.top = NULL;
+ altbacktrack.topbacktracks = NULL;
+
+ if (*ccbegin == OP_ALT && extrasize > 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+
+ altbacktrack.cc = ccbegin;
+ compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ {
+ if (local_quit_available)
+ {
+ common->local_quit_available = save_local_quit_available;
+ common->quit_label = save_quit_label;
+ common->quit = save_quit;
+ }
+ common->in_positive_assertion = save_in_positive_assertion;
+ common->then_trap = save_then_trap;
+ common->accept_label = save_accept_label;
+ common->positive_assertion_quit = save_positive_assertion_quit;
+ common->accept = save_accept;
+ return NULL;
+ }
+ common->accept_label = LABEL();
+ if (common->accept != NULL)
+ set_jumps(common->accept, common->accept_label);
+
+ /* Reset stack. */
+ if (framesize < 0)
+ {
+ if (framesize == no_frame)
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ else if (extrasize > 0)
+ free_stack(common, extrasize);
+
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
+ }
+ else
+ {
+ if ((opcode != OP_ASSERT_NOT && opcode != OP_ASSERTBACK_NOT) || conditional)
+ {
+ /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
+ }
+ else
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 2));
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize - 1) * sizeof(sljit_sw));
+ }
+ }
+
+ if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT)
+ {
+ /* We know that STR_PTR was stored on the top of the stack. */
+ if (conditional)
+ {
+ if (extrasize > 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), needs_control_head ? STACK(-2) : STACK(-1));
+ }
+ else if (bra == OP_BRAZERO)
+ {
+ if (framesize < 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize));
+ else
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1));
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - extrasize));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else if (framesize >= 0)
+ {
+ /* For OP_BRA and OP_BRAMINZERO. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1));
+ }
+ }
+ add_jump(compiler, found, JUMP(SLJIT_JUMP));
+
+ compile_backtrackingpath(common, altbacktrack.top);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ {
+ if (local_quit_available)
+ {
+ common->local_quit_available = save_local_quit_available;
+ common->quit_label = save_quit_label;
+ common->quit = save_quit;
+ }
+ common->in_positive_assertion = save_in_positive_assertion;
+ common->then_trap = save_then_trap;
+ common->accept_label = save_accept_label;
+ common->positive_assertion_quit = save_positive_assertion_quit;
+ common->accept = save_accept;
+ return NULL;
+ }
+ set_jumps(altbacktrack.topbacktracks, LABEL());
+
+ if (*cc != OP_ALT)
+ break;
+
+ ccbegin = cc;
+ cc += GET(cc, 1);
+ }
+
+if (local_quit_available)
+ {
+ SLJIT_ASSERT(common->positive_assertion_quit == NULL);
+ /* Makes the check less complicated below. */
+ common->positive_assertion_quit = common->quit;
+ }
+
+/* None of them matched. */
+if (common->positive_assertion_quit != NULL)
+ {
+ jump = JUMP(SLJIT_JUMP);
+ set_jumps(common->positive_assertion_quit, LABEL());
+ SLJIT_ASSERT(framesize != no_stack);
+ if (framesize < 0)
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw));
+ else
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (extrasize + 1) * sizeof(sljit_sw));
+ }
+ JUMPHERE(jump);
+ }
+
+if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(1));
+
+if (opcode == OP_ASSERT || opcode == OP_ASSERTBACK)
+ {
+ /* Assert is failed. */
+ if ((conditional && extrasize > 0) || bra == OP_BRAZERO)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+
+ if (framesize < 0)
+ {
+ /* The topmost item should be 0. */
+ if (bra == OP_BRAZERO)
+ {
+ if (extrasize == 2)
+ free_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else if (extrasize > 0)
+ free_stack(common, extrasize);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
+ /* The topmost item should be 0. */
+ if (bra == OP_BRAZERO)
+ {
+ free_stack(common, framesize + extrasize - 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else
+ free_stack(common, framesize + extrasize);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+ jump = JUMP(SLJIT_JUMP);
+ if (bra != OP_BRAZERO)
+ add_jump(compiler, target, jump);
+
+ /* Assert is successful. */
+ set_jumps(tmp, LABEL());
+ if (framesize < 0)
+ {
+ /* We know that STR_PTR was stored on the top of the stack. */
+ if (extrasize > 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize));
+
+ /* Keep the STR_PTR on the top of the stack. */
+ if (bra == OP_BRAZERO)
+ {
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ if (extrasize == 2)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ }
+ else if (bra == OP_BRAMINZERO)
+ {
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ }
+ else
+ {
+ if (bra == OP_BRA)
+ {
+ /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize + 1));
+ }
+ else
+ {
+ /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 2) * sizeof(sljit_sw));
+ if (extrasize == 2)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ if (bra == OP_BRAMINZERO)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), bra == OP_BRAZERO ? STR_PTR : SLJIT_IMM, 0);
+ }
+ }
+ }
+
+ if (bra == OP_BRAZERO)
+ {
+ backtrack->matchingpath = LABEL();
+ SET_LABEL(jump, backtrack->matchingpath);
+ }
+ else if (bra == OP_BRAMINZERO)
+ {
+ JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
+ JUMPHERE(brajump);
+ if (framesize >= 0)
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize - 1) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+ set_jumps(backtrack->common.topbacktracks, LABEL());
+ }
+ }
+else
+ {
+ /* AssertNot is successful. */
+ if (framesize < 0)
+ {
+ if (extrasize > 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+
+ if (bra != OP_BRA)
+ {
+ if (extrasize == 2)
+ free_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else if (extrasize > 0)
+ free_stack(common, extrasize);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1));
+ /* The topmost item should be 0. */
+ if (bra != OP_BRA)
+ {
+ free_stack(common, framesize + extrasize - 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ else
+ free_stack(common, framesize + extrasize);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+
+ if (bra == OP_BRAZERO)
+ backtrack->matchingpath = LABEL();
+ else if (bra == OP_BRAMINZERO)
+ {
+ JUMPTO(SLJIT_JUMP, backtrack->matchingpath);
+ JUMPHERE(brajump);
+ }
+
+ if (bra != OP_BRA)
+ {
+ SLJIT_ASSERT(found == &backtrack->common.topbacktracks);
+ set_jumps(backtrack->common.topbacktracks, LABEL());
+ backtrack->common.topbacktracks = NULL;
+ }
+ }
+
+if (local_quit_available)
+ {
+ common->local_quit_available = save_local_quit_available;
+ common->quit_label = save_quit_label;
+ common->quit = save_quit;
+ }
+common->in_positive_assertion = save_in_positive_assertion;
+common->then_trap = save_then_trap;
+common->accept_label = save_accept_label;
+common->positive_assertion_quit = save_positive_assertion_quit;
+common->accept = save_accept;
+return cc + 1 + LINK_SIZE;
+}
+
+static SLJIT_INLINE void match_once_common(compiler_common *common, PCRE2_UCHAR ket, int framesize, int private_data_ptr, BOOL has_alternatives, BOOL needs_control_head)
+{
+DEFINE_COMPILER;
+int stacksize;
+
+if (framesize < 0)
+ {
+ if (framesize == no_frame)
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ else
+ {
+ stacksize = needs_control_head ? 1 : 0;
+ if (ket != OP_KET || has_alternatives)
+ stacksize++;
+
+ if (stacksize > 0)
+ free_stack(common, stacksize);
+ }
+
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), (ket != OP_KET || has_alternatives) ? STACK(-2) : STACK(-1));
+
+ /* TMP2 which is set here used by OP_KETRMAX below. */
+ if (ket == OP_KETRMAX)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(-1));
+ else if (ket == OP_KETRMIN)
+ {
+ /* Move the STR_PTR to the private_data_ptr. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1));
+ }
+ }
+else
+ {
+ stacksize = (ket != OP_KET || has_alternatives) ? 2 : 1;
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + stacksize) * sizeof(sljit_sw));
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-1));
+
+ if (ket == OP_KETRMAX)
+ {
+ /* TMP2 which is set here used by OP_KETRMAX below. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ }
+ }
+if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0);
+}
+
+static SLJIT_INLINE int match_capture_common(compiler_common *common, int stacksize, int offset, int private_data_ptr)
+{
+DEFINE_COMPILER;
+
+if (common->capture_last_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
+ stacksize++;
+ }
+if (common->optimized_cbracket[offset >> 1] == 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ stacksize += 2;
+ }
+return stacksize;
+}
+
+/*
+ Handling bracketed expressions is probably the most complex part.
+
+ Stack layout naming characters:
+ S - Push the current STR_PTR
+ 0 - Push a 0 (NULL)
+ A - Push the current STR_PTR. Needed for restoring the STR_PTR
+ before the next alternative. Not pushed if there are no alternatives.
+ M - Any values pushed by the current alternative. Can be empty, or anything.
+ C - Push the previous OVECTOR(i), OVECTOR(i+1) and OVECTOR_PRIV(i) to the stack.
+ L - Push the previous local (pointed by localptr) to the stack
+ () - opional values stored on the stack
+ ()* - optonal, can be stored multiple times
+
+ The following list shows the regular expression templates, their PCRE byte codes
+ and stack layout supported by pcre-sljit.
+
+ (?:) OP_BRA | OP_KET A M
+ () OP_CBRA | OP_KET C M
+ (?:)+ OP_BRA | OP_KETRMAX 0 A M S ( A M S )*
+ OP_SBRA | OP_KETRMAX 0 L M S ( L M S )*
+ (?:)+? OP_BRA | OP_KETRMIN 0 A M S ( A M S )*
+ OP_SBRA | OP_KETRMIN 0 L M S ( L M S )*
+ ()+ OP_CBRA | OP_KETRMAX 0 C M S ( C M S )*
+ OP_SCBRA | OP_KETRMAX 0 C M S ( C M S )*
+ ()+? OP_CBRA | OP_KETRMIN 0 C M S ( C M S )*
+ OP_SCBRA | OP_KETRMIN 0 C M S ( C M S )*
+ (?:)? OP_BRAZERO | OP_BRA | OP_KET S ( A M 0 )
+ (?:)?? OP_BRAMINZERO | OP_BRA | OP_KET S ( A M 0 )
+ ()? OP_BRAZERO | OP_CBRA | OP_KET S ( C M 0 )
+ ()?? OP_BRAMINZERO | OP_CBRA | OP_KET S ( C M 0 )
+ (?:)* OP_BRAZERO | OP_BRA | OP_KETRMAX S 0 ( A M S )*
+ OP_BRAZERO | OP_SBRA | OP_KETRMAX S 0 ( L M S )*
+ (?:)*? OP_BRAMINZERO | OP_BRA | OP_KETRMIN S 0 ( A M S )*
+ OP_BRAMINZERO | OP_SBRA | OP_KETRMIN S 0 ( L M S )*
+ ()* OP_BRAZERO | OP_CBRA | OP_KETRMAX S 0 ( C M S )*
+ OP_BRAZERO | OP_SCBRA | OP_KETRMAX S 0 ( C M S )*
+ ()*? OP_BRAMINZERO | OP_CBRA | OP_KETRMIN S 0 ( C M S )*
+ OP_BRAMINZERO | OP_SCBRA | OP_KETRMIN S 0 ( C M S )*
+
+
+ Stack layout naming characters:
+ A - Push the alternative index (starting from 0) on the stack.
+ Not pushed if there is no alternatives.
+ M - Any values pushed by the current alternative. Can be empty, or anything.
+
+ The next list shows the possible content of a bracket:
+ (|) OP_*BRA | OP_ALT ... M A
+ (?()|) OP_*COND | OP_ALT M A
+ (?>|) OP_ONCE | OP_ALT ... [stack trace] M A
+ Or nothing, if trace is unnecessary
+*/
+
+static PCRE2_SPTR compile_bracket_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+PCRE2_UCHAR opcode;
+int private_data_ptr = 0;
+int offset = 0;
+int i, stacksize;
+int repeat_ptr = 0, repeat_length = 0;
+int repeat_type = 0, repeat_count = 0;
+PCRE2_SPTR ccbegin;
+PCRE2_SPTR matchingpath;
+PCRE2_SPTR slot;
+PCRE2_UCHAR bra = OP_BRA;
+PCRE2_UCHAR ket;
+assert_backtrack *assert;
+BOOL has_alternatives;
+BOOL needs_control_head = FALSE;
+struct sljit_jump *jump;
+struct sljit_jump *skip;
+struct sljit_label *rmax_label = NULL;
+struct sljit_jump *braminzero = NULL;
+
+PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL);
+
+if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
+ {
+ bra = *cc;
+ cc++;
+ opcode = *cc;
+ }
+
+opcode = *cc;
+ccbegin = cc;
+matchingpath = bracketend(cc) - 1 - LINK_SIZE;
+ket = *matchingpath;
+if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0)
+ {
+ repeat_ptr = PRIVATE_DATA(matchingpath);
+ repeat_length = PRIVATE_DATA(matchingpath + 1);
+ repeat_type = PRIVATE_DATA(matchingpath + 2);
+ repeat_count = PRIVATE_DATA(matchingpath + 3);
+ SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0);
+ if (repeat_type == OP_UPTO)
+ ket = OP_KETRMAX;
+ if (repeat_type == OP_MINUPTO)
+ ket = OP_KETRMIN;
+ }
+
+matchingpath = ccbegin + 1 + LINK_SIZE;
+SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN);
+SLJIT_ASSERT(!((bra == OP_BRAZERO && ket == OP_KETRMIN) || (bra == OP_BRAMINZERO && ket == OP_KETRMAX)));
+cc += GET(cc, 1);
+
+has_alternatives = *cc == OP_ALT;
+if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND))
+ {
+ SLJIT_COMPILE_ASSERT(OP_DNRREF == OP_RREF + 1 && OP_FALSE == OP_RREF + 2 && OP_TRUE == OP_RREF + 3,
+ compile_time_checks_must_be_grouped_together);
+ has_alternatives = ((*matchingpath >= OP_RREF && *matchingpath <= OP_TRUE) || *matchingpath == OP_FAIL) ? FALSE : TRUE;
+ }
+
+if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
+ opcode = OP_SCOND;
+
+if (opcode == OP_CBRA || opcode == OP_SCBRA)
+ {
+ /* Capturing brackets has a pre-allocated space. */
+ offset = GET2(ccbegin, 1 + LINK_SIZE);
+ if (common->optimized_cbracket[offset] == 0)
+ {
+ private_data_ptr = OVECTOR_PRIV(offset);
+ offset <<= 1;
+ }
+ else
+ {
+ offset <<= 1;
+ private_data_ptr = OVECTOR(offset);
+ }
+ BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
+ matchingpath += IMM2_SIZE;
+ }
+else if (opcode == OP_ONCE || opcode == OP_SBRA || opcode == OP_SCOND)
+ {
+ /* Other brackets simply allocate the next entry. */
+ private_data_ptr = PRIVATE_DATA(ccbegin);
+ SLJIT_ASSERT(private_data_ptr != 0);
+ BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr;
+ if (opcode == OP_ONCE)
+ BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, NULL, FALSE, &needs_control_head);
+ }
+
+/* Instructions before the first alternative. */
+stacksize = 0;
+if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
+ stacksize++;
+if (bra == OP_BRAZERO)
+ stacksize++;
+
+if (stacksize > 0)
+ allocate_stack(common, stacksize);
+
+stacksize = 0;
+if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO))
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
+ stacksize++;
+ }
+
+if (bra == OP_BRAZERO)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+
+if (bra == OP_BRAMINZERO)
+ {
+ /* This is a backtrack path! (Since the try-path of OP_BRAMINZERO matches to the empty string) */
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ if (ket != OP_KETRMIN)
+ {
+ free_stack(common, 1);
+ braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ }
+ else
+ {
+ if (opcode == OP_ONCE || opcode >= OP_SBRA)
+ {
+ jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ /* Nothing stored during the first run. */
+ skip = JUMP(SLJIT_JUMP);
+ JUMPHERE(jump);
+ /* Checking zero-length iteration. */
+ if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
+ {
+ /* When we come from outside, private_data_ptr contains the previous STR_PTR. */
+ braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ }
+ else
+ {
+ /* Except when the whole stack frame must be saved. */
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-BACKTRACK_AS(bracket_backtrack)->u.framesize - 2));
+ }
+ JUMPHERE(skip);
+ }
+ else
+ {
+ jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ JUMPHERE(jump);
+ }
+ }
+ }
+
+if (repeat_type != 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, repeat_count);
+ if (repeat_type == OP_EXACT)
+ rmax_label = LABEL();
+ }
+
+if (ket == OP_KETRMIN)
+ BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
+
+if (ket == OP_KETRMAX)
+ {
+ rmax_label = LABEL();
+ if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0)
+ BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label;
+ }
+
+/* Handling capturing brackets and alternatives. */
+if (opcode == OP_ONCE)
+ {
+ stacksize = 0;
+ if (needs_control_head)
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ stacksize++;
+ }
+
+ if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0)
+ {
+ /* Neither capturing brackets nor recursions are found in the block. */
+ if (ket == OP_KETRMIN)
+ {
+ stacksize += 2;
+ if (!needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ }
+ else
+ {
+ if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
+ if (ket == OP_KETRMAX || has_alternatives)
+ stacksize++;
+ }
+
+ if (stacksize > 0)
+ allocate_stack(common, stacksize);
+
+ stacksize = 0;
+ if (needs_control_head)
+ {
+ stacksize++;
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+ }
+
+ if (ket == OP_KETRMIN)
+ {
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+ if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame)
+ OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, needs_control_head ? (2 * sizeof(sljit_sw)) : sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0);
+ }
+ else if (ket == OP_KETRMAX || has_alternatives)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+ }
+ else
+ {
+ if (ket != OP_KET || has_alternatives)
+ stacksize++;
+
+ stacksize += BACKTRACK_AS(bracket_backtrack)->u.framesize + 1;
+ allocate_stack(common, stacksize);
+
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
+
+ stacksize = needs_control_head ? 1 : 0;
+ if (ket != OP_KET || has_alternatives)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
+ stacksize++;
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0);
+ }
+ init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1);
+ }
+ }
+else if (opcode == OP_CBRA || opcode == OP_SCBRA)
+ {
+ /* Saving the previous values. */
+ if (common->optimized_cbracket[offset >> 1] != 0)
+ {
+ SLJIT_ASSERT(private_data_ptr == OVECTOR(offset));
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr + sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+ }
+ }
+else if (opcode == OP_SBRA || opcode == OP_SCOND)
+ {
+ /* Saving the previous value. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+ }
+else if (has_alternatives)
+ {
+ /* Pushing the starting string pointer. */
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ }
+
+/* Generating code for the first alternative. */
+if (opcode == OP_COND || opcode == OP_SCOND)
+ {
+ if (*matchingpath == OP_CREF)
+ {
+ SLJIT_ASSERT(has_alternatives);
+ add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed),
+ CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(matchingpath, 1) << 1), SLJIT_MEM1(SLJIT_SP), OVECTOR(1)));
+ matchingpath += 1 + IMM2_SIZE;
+ }
+ else if (*matchingpath == OP_DNCREF)
+ {
+ SLJIT_ASSERT(has_alternatives);
+
+ i = GET2(matchingpath, 1 + IMM2_SIZE);
+ slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
+ OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1));
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
+ slot += common->name_entry_size;
+ i--;
+ while (i-- > 0)
+ {
+ OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0);
+ OP2(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, TMP2, 0, STR_PTR, 0);
+ slot += common->name_entry_size;
+ }
+ OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
+ add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_ZERO));
+ matchingpath += 1 + 2 * IMM2_SIZE;
+ }
+ else if ((*matchingpath >= OP_RREF && *matchingpath <= OP_TRUE) || *matchingpath == OP_FAIL)
+ {
+ /* Never has other case. */
+ BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL;
+ SLJIT_ASSERT(!has_alternatives);
+
+ if (*matchingpath == OP_TRUE)
+ {
+ stacksize = 1;
+ matchingpath++;
+ }
+ else if (*matchingpath == OP_FALSE || *matchingpath == OP_FAIL)
+ stacksize = 0;
+ else if (*matchingpath == OP_RREF)
+ {
+ stacksize = GET2(matchingpath, 1);
+ if (common->currententry == NULL)
+ stacksize = 0;
+ else if (stacksize == RREF_ANY)
+ stacksize = 1;
+ else if (common->currententry->start == 0)
+ stacksize = stacksize == 0;
+ else
+ stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
+
+ if (stacksize != 0)
+ matchingpath += 1 + IMM2_SIZE;
+ }
+ else
+ {
+ if (common->currententry == NULL || common->currententry->start == 0)
+ stacksize = 0;
+ else
+ {
+ stacksize = GET2(matchingpath, 1 + IMM2_SIZE);
+ slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size;
+ i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE);
+ while (stacksize > 0)
+ {
+ if ((int)GET2(slot, 0) == i)
+ break;
+ slot += common->name_entry_size;
+ stacksize--;
+ }
+ }
+
+ if (stacksize != 0)
+ matchingpath += 1 + 2 * IMM2_SIZE;
+ }
+
+ /* The stacksize == 0 is a common "else" case. */
+ if (stacksize == 0)
+ {
+ if (*cc == OP_ALT)
+ {
+ matchingpath = cc + 1 + LINK_SIZE;
+ cc += GET(cc, 1);
+ }
+ else
+ matchingpath = cc;
+ }
+ }
+ else
+ {
+ SLJIT_ASSERT(has_alternatives && *matchingpath >= OP_ASSERT && *matchingpath <= OP_ASSERTBACK_NOT);
+ /* Similar code as PUSH_BACKTRACK macro. */
+ assert = sljit_alloc_memory(compiler, sizeof(assert_backtrack));
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+ memset(assert, 0, sizeof(assert_backtrack));
+ assert->common.cc = matchingpath;
+ BACKTRACK_AS(bracket_backtrack)->u.assert = assert;
+ matchingpath = compile_assert_matchingpath(common, matchingpath, assert, TRUE);
+ }
+ }
+
+compile_matchingpath(common, matchingpath, cc, backtrack);
+if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+
+if (opcode == OP_ONCE)
+ match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
+
+stacksize = 0;
+if (repeat_type == OP_MINUPTO)
+ {
+ /* We need to preserve the counter. TMP2 will be used below. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
+ stacksize++;
+ }
+if (ket != OP_KET || bra != OP_BRA)
+ stacksize++;
+if (offset != 0)
+ {
+ if (common->capture_last_ptr != 0)
+ stacksize++;
+ if (common->optimized_cbracket[offset >> 1] == 0)
+ stacksize += 2;
+ }
+if (has_alternatives && opcode != OP_ONCE)
+ stacksize++;
+
+if (stacksize > 0)
+ allocate_stack(common, stacksize);
+
+stacksize = 0;
+if (repeat_type == OP_MINUPTO)
+ {
+ /* TMP2 was set above. */
+ OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
+ stacksize++;
+ }
+
+if (ket != OP_KET || bra != OP_BRA)
+ {
+ if (ket != OP_KET)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+ else
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
+ stacksize++;
+ }
+
+if (offset != 0)
+ stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
+
+if (has_alternatives)
+ {
+ if (opcode != OP_ONCE)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
+ if (ket != OP_KETRMAX)
+ BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
+ }
+
+/* Must be after the matchingpath label. */
+if (offset != 0 && common->optimized_cbracket[offset >> 1] != 0)
+ {
+ SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+ }
+
+if (ket == OP_KETRMAX)
+ {
+ if (repeat_type != 0)
+ {
+ if (has_alternatives)
+ BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, rmax_label);
+ /* Drop STR_PTR for greedy plus quantifier. */
+ if (opcode != OP_ONCE)
+ free_stack(common, 1);
+ }
+ else if (opcode == OP_ONCE || opcode >= OP_SBRA)
+ {
+ if (has_alternatives)
+ BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL();
+ /* Checking zero-length iteration. */
+ if (opcode != OP_ONCE)
+ {
+ CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0, rmax_label);
+ /* Drop STR_PTR for greedy plus quantifier. */
+ if (bra != OP_BRAZERO)
+ free_stack(common, 1);
+ }
+ else
+ /* TMP2 must contain the starting STR_PTR. */
+ CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label);
+ }
+ else
+ JUMPTO(SLJIT_JUMP, rmax_label);
+ BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL();
+ }
+
+if (repeat_type == OP_EXACT)
+ {
+ count_match(common);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, rmax_label);
+ }
+else if (repeat_type == OP_UPTO)
+ {
+ /* We need to preserve the counter. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+ }
+
+if (bra == OP_BRAZERO)
+ BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL();
+
+if (bra == OP_BRAMINZERO)
+ {
+ /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */
+ JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath);
+ if (braminzero != NULL)
+ {
+ JUMPHERE(braminzero);
+ /* We need to release the end pointer to perform the
+ backtrack for the zero-length iteration. When
+ framesize is < 0, OP_ONCE will do the release itself. */
+ if (opcode == OP_ONCE && BACKTRACK_AS(bracket_backtrack)->u.framesize >= 0)
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (BACKTRACK_AS(bracket_backtrack)->u.framesize - 1) * sizeof(sljit_sw));
+ }
+ else if (ket == OP_KETRMIN && opcode != OP_ONCE)
+ free_stack(common, 1);
+ }
+ /* Continue to the normal backtrack. */
+ }
+
+if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO)
+ count_match(common);
+
+/* Skip the other alternatives. */
+while (*cc == OP_ALT)
+ cc += GET(cc, 1);
+cc += 1 + LINK_SIZE;
+
+if (opcode == OP_ONCE)
+ {
+ /* We temporarily encode the needs_control_head in the lowest bit.
+ Note: on the target architectures of SLJIT the ((x << 1) >> 1) returns
+ the same value for small signed numbers (including negative numbers). */
+ BACKTRACK_AS(bracket_backtrack)->u.framesize = (BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0);
+ }
+return cc + repeat_length;
+}
+
+static PCRE2_SPTR compile_bracketpos_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+PCRE2_UCHAR opcode;
+int private_data_ptr;
+int cbraprivptr = 0;
+BOOL needs_control_head;
+int framesize;
+int stacksize;
+int offset = 0;
+BOOL zero = FALSE;
+PCRE2_SPTR ccbegin = NULL;
+int stack; /* Also contains the offset of control head. */
+struct sljit_label *loop = NULL;
+struct jump_list *emptymatch = NULL;
+
+PUSH_BACKTRACK(sizeof(bracketpos_backtrack), cc, NULL);
+if (*cc == OP_BRAPOSZERO)
+ {
+ zero = TRUE;
+ cc++;
+ }
+
+opcode = *cc;
+private_data_ptr = PRIVATE_DATA(cc);
+SLJIT_ASSERT(private_data_ptr != 0);
+BACKTRACK_AS(bracketpos_backtrack)->private_data_ptr = private_data_ptr;
+switch(opcode)
+ {
+ case OP_BRAPOS:
+ case OP_SBRAPOS:
+ ccbegin = cc + 1 + LINK_SIZE;
+ break;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ offset = GET2(cc, 1 + LINK_SIZE);
+ /* This case cannot be optimized in the same was as
+ normal capturing brackets. */
+ SLJIT_ASSERT(common->optimized_cbracket[offset] == 0);
+ cbraprivptr = OVECTOR_PRIV(offset);
+ offset <<= 1;
+ ccbegin = cc + 1 + LINK_SIZE + IMM2_SIZE;
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+
+framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head);
+BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize;
+if (framesize < 0)
+ {
+ if (offset != 0)
+ {
+ stacksize = 2;
+ if (common->capture_last_ptr != 0)
+ stacksize++;
+ }
+ else
+ stacksize = 1;
+
+ if (needs_control_head)
+ stacksize++;
+ if (!zero)
+ stacksize++;
+
+ BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
+ allocate_stack(common, stacksize);
+ if (framesize == no_frame)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0);
+
+ stack = 0;
+ if (offset != 0)
+ {
+ stack = 2;
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
+ if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0);
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ if (common->capture_last_ptr != 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0);
+ stack = 3;
+ }
+ }
+ else
+ {
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ stack = 1;
+ }
+
+ if (needs_control_head)
+ stack++;
+ if (!zero)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), SLJIT_IMM, 1);
+ if (needs_control_head)
+ {
+ stack--;
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
+ }
+ }
+else
+ {
+ stacksize = framesize + 1;
+ if (!zero)
+ stacksize++;
+ if (needs_control_head)
+ stacksize++;
+ if (offset == 0)
+ stacksize++;
+ BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize;
+
+ allocate_stack(common, stacksize);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ if (needs_control_head)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
+
+ stack = 0;
+ if (!zero)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1);
+ stack = 1;
+ }
+ if (needs_control_head)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0);
+ stack++;
+ }
+ if (offset == 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0);
+ stack++;
+ }
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0);
+ init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize);
+ stack -= 1 + (offset == 0);
+ }
+
+if (offset != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
+
+loop = LABEL();
+while (*cc != OP_KETRPOS)
+ {
+ backtrack->top = NULL;
+ backtrack->topbacktracks = NULL;
+ cc += GET(cc, 1);
+
+ compile_matchingpath(common, ccbegin, cc, backtrack);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+
+ if (framesize < 0)
+ {
+ if (framesize == no_frame)
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+
+ if (offset != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
+ if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ }
+ else
+ {
+ if (opcode == OP_SBRAPOS)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ }
+
+ /* Even if the match is empty, we need to reset the control head. */
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
+
+ if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS)
+ add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0));
+
+ if (!zero)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0);
+ }
+ else
+ {
+ if (offset != 0)
+ {
+ OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, stacksize * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0);
+ if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP2(SLJIT_SUB, STACK_TOP, 0, TMP2, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw));
+ if (opcode == OP_SBRAPOS)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2));
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), STACK(-framesize - 2), STR_PTR, 0);
+ }
+
+ /* Even if the match is empty, we need to reset the control head. */
+ if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack));
+
+ if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS)
+ add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0));
+
+ if (!zero)
+ {
+ if (framesize < 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0);
+ else
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ }
+ }
+
+ JUMPTO(SLJIT_JUMP, loop);
+ flush_stubs(common);
+
+ compile_backtrackingpath(common, backtrack->top);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return NULL;
+ set_jumps(backtrack->topbacktracks, LABEL());
+
+ if (framesize < 0)
+ {
+ if (offset != 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
+ else
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ }
+ else
+ {
+ if (offset != 0)
+ {
+ /* Last alternative. */
+ if (*cc == OP_KETRPOS)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2));
+ }
+ }
+
+ if (*cc == OP_KETRPOS)
+ break;
+ ccbegin = cc + 1 + LINK_SIZE;
+ }
+
+/* We don't have to restore the control head in case of a failed match. */
+
+backtrack->topbacktracks = NULL;
+if (!zero)
+ {
+ if (framesize < 0)
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0));
+ else /* TMP2 is set to [private_data_ptr] above. */
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(TMP2), STACK(-stacksize), SLJIT_IMM, 0));
+ }
+
+/* None of them matched. */
+set_jumps(emptymatch, LABEL());
+count_match(common);
+return cc + 1 + LINK_SIZE;
+}
+
+static SLJIT_INLINE PCRE2_SPTR get_iterator_parameters(compiler_common *common, PCRE2_SPTR cc, PCRE2_UCHAR *opcode, PCRE2_UCHAR *type, sljit_u32 *max, sljit_u32 *exact, PCRE2_SPTR *end)
+{
+int class_len;
+
+*opcode = *cc;
+*exact = 0;
+
+if (*opcode >= OP_STAR && *opcode <= OP_POSUPTO)
+ {
+ cc++;
+ *type = OP_CHAR;
+ }
+else if (*opcode >= OP_STARI && *opcode <= OP_POSUPTOI)
+ {
+ cc++;
+ *type = OP_CHARI;
+ *opcode -= OP_STARI - OP_STAR;
+ }
+else if (*opcode >= OP_NOTSTAR && *opcode <= OP_NOTPOSUPTO)
+ {
+ cc++;
+ *type = OP_NOT;
+ *opcode -= OP_NOTSTAR - OP_STAR;
+ }
+else if (*opcode >= OP_NOTSTARI && *opcode <= OP_NOTPOSUPTOI)
+ {
+ cc++;
+ *type = OP_NOTI;
+ *opcode -= OP_NOTSTARI - OP_STAR;
+ }
+else if (*opcode >= OP_TYPESTAR && *opcode <= OP_TYPEPOSUPTO)
+ {
+ cc++;
+ *opcode -= OP_TYPESTAR - OP_STAR;
+ *type = OP_END;
+ }
+else
+ {
+ SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS);
+ *type = *opcode;
+ cc++;
+ class_len = (*type < OP_XCLASS) ? (int)(1 + (32 / sizeof(PCRE2_UCHAR))) : GET(cc, 0);
+ *opcode = cc[class_len - 1];
+
+ if (*opcode >= OP_CRSTAR && *opcode <= OP_CRMINQUERY)
+ {
+ *opcode -= OP_CRSTAR - OP_STAR;
+ *end = cc + class_len;
+
+ if (*opcode == OP_PLUS || *opcode == OP_MINPLUS)
+ {
+ *exact = 1;
+ *opcode -= OP_PLUS - OP_STAR;
+ }
+ }
+ else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY)
+ {
+ *opcode -= OP_CRPOSSTAR - OP_POSSTAR;
+ *end = cc + class_len;
+
+ if (*opcode == OP_POSPLUS)
+ {
+ *exact = 1;
+ *opcode = OP_POSSTAR;
+ }
+ }
+ else
+ {
+ SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE);
+ *max = GET2(cc, (class_len + IMM2_SIZE));
+ *exact = GET2(cc, class_len);
+
+ if (*max == 0)
+ {
+ if (*opcode == OP_CRPOSRANGE)
+ *opcode = OP_POSSTAR;
+ else
+ *opcode -= OP_CRRANGE - OP_STAR;
+ }
+ else
+ {
+ *max -= *exact;
+ if (*max == 0)
+ *opcode = OP_EXACT;
+ else if (*max == 1)
+ {
+ if (*opcode == OP_CRPOSRANGE)
+ *opcode = OP_POSQUERY;
+ else
+ *opcode -= OP_CRRANGE - OP_QUERY;
+ }
+ else
+ {
+ if (*opcode == OP_CRPOSRANGE)
+ *opcode = OP_POSUPTO;
+ else
+ *opcode -= OP_CRRANGE - OP_UPTO;
+ }
+ }
+ *end = cc + class_len + 2 * IMM2_SIZE;
+ }
+ return cc;
+ }
+
+switch(*opcode)
+ {
+ case OP_EXACT:
+ *exact = GET2(cc, 0);
+ cc += IMM2_SIZE;
+ break;
+
+ case OP_PLUS:
+ case OP_MINPLUS:
+ *exact = 1;
+ *opcode -= OP_PLUS - OP_STAR;
+ break;
+
+ case OP_POSPLUS:
+ *exact = 1;
+ *opcode = OP_POSSTAR;
+ break;
+
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_POSUPTO:
+ *max = GET2(cc, 0);
+ cc += IMM2_SIZE;
+ break;
+ }
+
+if (*type == OP_END)
+ {
+ *type = *cc;
+ *end = next_opcode(common, cc);
+ cc++;
+ return cc;
+ }
+
+*end = cc + 1;
+#ifdef SUPPORT_UNICODE
+if (common->utf && HAS_EXTRALEN(*cc)) *end += GET_EXTRALEN(*cc);
+#endif
+return cc;
+}
+
+static PCRE2_SPTR compile_iterator_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+PCRE2_UCHAR opcode;
+PCRE2_UCHAR type;
+sljit_u32 max = 0, exact;
+BOOL fast_fail;
+sljit_s32 fast_str_ptr;
+BOOL charpos_enabled;
+PCRE2_UCHAR charpos_char;
+unsigned int charpos_othercasebit;
+PCRE2_SPTR end;
+jump_list *no_match = NULL;
+jump_list *no_char1_match = NULL;
+struct sljit_jump *jump = NULL;
+struct sljit_label *label;
+int private_data_ptr = PRIVATE_DATA(cc);
+int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP);
+int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
+int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);
+int tmp_base, tmp_offset;
+
+PUSH_BACKTRACK(sizeof(char_iterator_backtrack), cc, NULL);
+
+fast_str_ptr = PRIVATE_DATA(cc + 1);
+fast_fail = TRUE;
+
+SLJIT_ASSERT(common->fast_forward_bc_ptr == NULL || fast_str_ptr == 0 || cc == common->fast_forward_bc_ptr);
+
+if (cc == common->fast_forward_bc_ptr)
+ fast_fail = FALSE;
+else if (common->fast_fail_start_ptr == 0)
+ fast_str_ptr = 0;
+
+SLJIT_ASSERT(common->fast_forward_bc_ptr != NULL || fast_str_ptr == 0
+ || (fast_str_ptr >= common->fast_fail_start_ptr && fast_str_ptr <= common->fast_fail_end_ptr));
+
+cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end);
+
+if (type != OP_EXTUNI)
+ {
+ tmp_base = TMP3;
+ tmp_offset = 0;
+ }
+else
+ {
+ tmp_base = SLJIT_MEM1(SLJIT_SP);
+ tmp_offset = POSSESSIVE0;
+ }
+
+if (fast_fail && fast_str_ptr != 0)
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), fast_str_ptr));
+
+/* Handle fixed part first. */
+if (exact > 1)
+ {
+ SLJIT_ASSERT(fast_str_ptr == 0);
+ if (common->mode == PCRE2_JIT_COMPLETE
+#ifdef SUPPORT_UNICODE
+ && !common->utf
+#endif
+ )
+ {
+ OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(exact));
+ add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER, TMP1, 0, STR_END, 0));
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact);
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact);
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ }
+ }
+else if (exact == 1)
+ compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE);
+
+switch(opcode)
+ {
+ case OP_STAR:
+ case OP_UPTO:
+ SLJIT_ASSERT(fast_str_ptr == 0 || opcode == OP_STAR);
+
+ if (type == OP_ANYNL || type == OP_EXTUNI)
+ {
+ SLJIT_ASSERT(private_data_ptr == 0);
+ SLJIT_ASSERT(fast_str_ptr == 0);
+
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0);
+
+ if (opcode == OP_UPTO)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, max);
+
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE);
+ if (opcode == OP_UPTO)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ jump = JUMP(SLJIT_ZERO);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0);
+ }
+
+ /* We cannot use TMP3 because of this allocate_stack. */
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, label);
+ if (jump != NULL)
+ JUMPHERE(jump);
+ }
+ else
+ {
+ charpos_enabled = FALSE;
+ charpos_char = 0;
+ charpos_othercasebit = 0;
+
+ if ((type != OP_CHAR && type != OP_CHARI) && (*end == OP_CHAR || *end == OP_CHARI))
+ {
+ charpos_enabled = TRUE;
+#ifdef SUPPORT_UNICODE
+ charpos_enabled = !common->utf || !HAS_EXTRALEN(end[1]);
+#endif
+ if (charpos_enabled && *end == OP_CHARI && char_has_othercase(common, end + 1))
+ {
+ charpos_othercasebit = char_get_othercase_bit(common, end + 1);
+ if (charpos_othercasebit == 0)
+ charpos_enabled = FALSE;
+ }
+
+ if (charpos_enabled)
+ {
+ charpos_char = end[1];
+ /* Consumpe the OP_CHAR opcode. */
+ end += 2;
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ SLJIT_ASSERT((charpos_othercasebit >> 8) == 0);
+#elif PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ SLJIT_ASSERT((charpos_othercasebit >> 9) == 0);
+ if ((charpos_othercasebit & 0x100) != 0)
+ charpos_othercasebit = (charpos_othercasebit & 0xff) << 8;
+#endif
+ if (charpos_othercasebit != 0)
+ charpos_char |= charpos_othercasebit;
+
+ BACKTRACK_AS(char_iterator_backtrack)->u.charpos.enabled = TRUE;
+ BACKTRACK_AS(char_iterator_backtrack)->u.charpos.chr = charpos_char;
+ BACKTRACK_AS(char_iterator_backtrack)->u.charpos.othercasebit = charpos_othercasebit;
+ }
+ }
+
+ if (charpos_enabled)
+ {
+ if (opcode == OP_UPTO)
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max + 1);
+
+ /* Search the first instance of charpos_char. */
+ jump = JUMP(SLJIT_JUMP);
+ label = LABEL();
+ if (opcode == OP_UPTO)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_ZERO));
+ }
+ compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE);
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ JUMPHERE(jump);
+
+ detect_partial_match(common, &backtrack->topbacktracks);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ if (charpos_othercasebit != 0)
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label);
+
+ if (private_data_ptr == 0)
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
+ if (opcode == OP_UPTO)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ add_jump(compiler, &no_match, JUMP(SLJIT_ZERO));
+ }
+
+ /* Search the last instance of charpos_char. */
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &no_match, FALSE);
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ detect_partial_match(common, &no_match);
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
+ if (charpos_othercasebit != 0)
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit);
+ if (opcode == OP_STAR)
+ {
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ }
+ else
+ {
+ jump = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ JUMPHERE(jump);
+ }
+
+ if (opcode == OP_UPTO)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ }
+ else
+ JUMPTO(SLJIT_JUMP, label);
+
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ }
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ else if (common->utf)
+ {
+ if (private_data_ptr == 0)
+ allocate_stack(common, 2);
+
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
+
+ if (opcode == OP_UPTO)
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
+
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+
+ if (opcode == OP_UPTO)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ }
+ else
+ JUMPTO(SLJIT_JUMP, label);
+
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ }
+#endif
+ else
+ {
+ if (private_data_ptr == 0)
+ allocate_stack(common, 2);
+
+ OP1(SLJIT_MOV, base, offset1, STR_PTR, 0);
+ if (opcode == OP_UPTO)
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
+
+ label = LABEL();
+ detect_partial_match(common, &no_match);
+ compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
+ if (opcode == OP_UPTO)
+ {
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ }
+ else
+ JUMPTO(SLJIT_JUMP, label);
+
+ set_jumps(no_char1_match, LABEL());
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ }
+ }
+ BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
+ break;
+
+ case OP_MINSTAR:
+ if (private_data_ptr == 0)
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ break;
+
+ case OP_MINUPTO:
+ SLJIT_ASSERT(fast_str_ptr == 0);
+ if (private_data_ptr == 0)
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ OP1(SLJIT_MOV, base, offset1, SLJIT_IMM, max + 1);
+ BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
+ break;
+
+ case OP_QUERY:
+ case OP_MINQUERY:
+ SLJIT_ASSERT(fast_str_ptr == 0);
+ if (private_data_ptr == 0)
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ if (opcode == OP_QUERY)
+ compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE);
+ BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL();
+ break;
+
+ case OP_EXACT:
+ break;
+
+ case OP_POSSTAR:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf)
+ {
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, label);
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ break;
+ }
+#endif
+ label = LABEL();
+ detect_partial_match(common, &no_match);
+ compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
+ JUMPTO(SLJIT_JUMP, label);
+ set_jumps(no_char1_match, LABEL());
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ set_jumps(no_match, LABEL());
+ if (fast_str_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0);
+ break;
+
+ case OP_POSUPTO:
+ SLJIT_ASSERT(fast_str_ptr == 0);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
+ if (common->utf)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0);
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
+ label = LABEL();
+ compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1);
+ break;
+ }
+#endif
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max);
+ label = LABEL();
+ detect_partial_match(common, &no_match);
+ compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1);
+ JUMPTO(SLJIT_NOT_ZERO, label);
+ OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ set_jumps(no_char1_match, LABEL());
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+ set_jumps(no_match, LABEL());
+ break;
+
+ case OP_POSQUERY:
+ SLJIT_ASSERT(fast_str_ptr == 0);
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
+ compile_char1_matchingpath(common, type, cc, &no_match, TRUE);
+ OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0);
+ set_jumps(no_match, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset);
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+
+count_match(common);
+return end;
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_fail_accept_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+
+PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
+
+if (*cc == OP_FAIL)
+ {
+ add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP));
+ return cc + 1;
+ }
+
+if (*cc == OP_ACCEPT && common->currententry == NULL && (common->re->overall_options & PCRE2_ENDANCHORED) != 0)
+ add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0));
+
+if (*cc == OP_ASSERT_ACCEPT || common->currententry != NULL || !common->might_be_empty)
+ {
+ /* No need to check notempty conditions. */
+ if (common->accept_label == NULL)
+ add_jump(compiler, &common->accept, JUMP(SLJIT_JUMP));
+ else
+ JUMPTO(SLJIT_JUMP, common->accept_label);
+ return cc + 1;
+ }
+
+if (common->accept_label == NULL)
+ add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)));
+else
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), common->accept_label);
+OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+OP1(SLJIT_MOV_U32, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, options));
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY);
+add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_NOT_ZERO));
+OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY_ATSTART);
+if (common->accept_label == NULL)
+ add_jump(compiler, &common->accept, JUMP(SLJIT_ZERO));
+else
+ JUMPTO(SLJIT_ZERO, common->accept_label);
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+if (common->accept_label == NULL)
+ add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0));
+else
+ CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, common->accept_label);
+add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP));
+return cc + 1;
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_close_matchingpath(compiler_common *common, PCRE2_SPTR cc)
+{
+DEFINE_COMPILER;
+int offset = GET2(cc, 1);
+BOOL optimized_cbracket = common->optimized_cbracket[offset] != 0;
+
+/* Data will be discarded anyway... */
+if (common->currententry != NULL)
+ return cc + 1 + IMM2_SIZE;
+
+if (!optimized_cbracket)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR_PRIV(offset));
+offset <<= 1;
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+if (!optimized_cbracket)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+return cc + 1 + IMM2_SIZE;
+}
+
+static SLJIT_INLINE PCRE2_SPTR compile_control_verb_matchingpath(compiler_common *common, PCRE2_SPTR cc, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+PCRE2_UCHAR opcode = *cc;
+PCRE2_SPTR ccend = cc + 1;
+
+if (opcode == OP_COMMIT_ARG || opcode == OP_PRUNE_ARG ||
+ opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG)
+ ccend += 2 + cc[1];
+
+PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL);
+
+if (opcode == OP_SKIP)
+ {
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ return ccend;
+ }
+
+if (opcode == OP_COMMIT_ARG || opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
+ }
+
+return ccend;
+}
+
+static PCRE2_UCHAR then_trap_opcode[1] = { OP_THEN_TRAP };
+
+static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+BOOL needs_control_head;
+int size;
+
+PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
+common->then_trap = BACKTRACK_AS(then_trap_backtrack);
+BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
+BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start);
+BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head);
+
+size = BACKTRACK_AS(then_trap_backtrack)->framesize;
+size = 3 + (size < 0 ? 0 : size);
+
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+allocate_stack(common, size);
+if (size > 3)
+ OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw));
+else
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0);
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start);
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap);
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0);
+
+size = BACKTRACK_AS(then_trap_backtrack)->framesize;
+if (size >= 0)
+ init_frame(common, cc, ccend, size - 1, 0);
+}
+
+static void compile_matchingpath(compiler_common *common, PCRE2_SPTR cc, PCRE2_SPTR ccend, backtrack_common *parent)
+{
+DEFINE_COMPILER;
+backtrack_common *backtrack;
+BOOL has_then_trap = FALSE;
+then_trap_backtrack *save_then_trap = NULL;
+
+SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS));
+
+if (common->has_then && common->then_offsets[cc - common->start] != 0)
+ {
+ SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0);
+ has_then_trap = TRUE;
+ save_then_trap = common->then_trap;
+ /* Tail item on backtrack. */
+ compile_then_trap_matchingpath(common, cc, ccend, parent);
+ }
+
+while (cc < ccend)
+ {
+ switch(*cc)
+ {
+ case OP_SOD:
+ case OP_SOM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ case OP_EODN:
+ case OP_EOD:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_REVERSE:
+ cc = compile_simple_assertion_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
+ break;
+
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+ case OP_ANYBYTE:
+ case OP_NOTPROP:
+ case OP_PROP:
+ case OP_ANYNL:
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ case OP_EXTUNI:
+ case OP_NOT:
+ case OP_NOTI:
+ cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
+ break;
+
+ case OP_SET_SOM:
+ PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+ cc++;
+ break;
+
+ case OP_CHAR:
+ case OP_CHARI:
+ if (common->mode == PCRE2_JIT_COMPLETE)
+ cc = compile_charn_matchingpath(common, cc, ccend, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
+ else
+ cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
+ break;
+
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ case OP_TYPEPOSUPTO:
+ cc = compile_iterator_matchingpath(common, cc, parent);
+ break;
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ if (cc[1 + (32 / sizeof(PCRE2_UCHAR))] >= OP_CRSTAR && cc[1 + (32 / sizeof(PCRE2_UCHAR))] <= OP_CRPOSRANGE)
+ cc = compile_iterator_matchingpath(common, cc, parent);
+ else
+ cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
+ break;
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH == 16 || PCRE2_CODE_UNIT_WIDTH == 32
+ case OP_XCLASS:
+ if (*(cc + GET(cc, 1)) >= OP_CRSTAR && *(cc + GET(cc, 1)) <= OP_CRPOSRANGE)
+ cc = compile_iterator_matchingpath(common, cc, parent);
+ else
+ cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE);
+ break;
+#endif
+
+ case OP_REF:
+ case OP_REFI:
+ if (cc[1 + IMM2_SIZE] >= OP_CRSTAR && cc[1 + IMM2_SIZE] <= OP_CRPOSRANGE)
+ cc = compile_ref_iterator_matchingpath(common, cc, parent);
+ else
+ {
+ compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
+ cc += 1 + IMM2_SIZE;
+ }
+ break;
+
+ case OP_DNREF:
+ case OP_DNREFI:
+ if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE)
+ cc = compile_ref_iterator_matchingpath(common, cc, parent);
+ else
+ {
+ compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks);
+ compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE);
+ cc += 1 + 2 * IMM2_SIZE;
+ }
+ break;
+
+ case OP_RECURSE:
+ cc = compile_recurse_matchingpath(common, cc, parent);
+ break;
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+ cc = compile_callout_matchingpath(common, cc, parent);
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc);
+ cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE);
+ break;
+
+ case OP_BRAMINZERO:
+ PUSH_BACKTRACK_NOVALUE(sizeof(braminzero_backtrack), cc);
+ cc = bracketend(cc + 1);
+ if (*(cc - 1 - LINK_SIZE) != OP_KETRMIN)
+ {
+ allocate_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+ }
+ else
+ {
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), STR_PTR, 0);
+ }
+ BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL();
+ count_match(common);
+ break;
+
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_CBRA:
+ case OP_COND:
+ case OP_SBRA:
+ case OP_SCBRA:
+ case OP_SCOND:
+ cc = compile_bracket_matchingpath(common, cc, parent);
+ break;
+
+ case OP_BRAZERO:
+ if (cc[1] > OP_ASSERTBACK_NOT)
+ cc = compile_bracket_matchingpath(common, cc, parent);
+ else
+ {
+ PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc);
+ cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE);
+ }
+ break;
+
+ case OP_BRAPOS:
+ case OP_CBRAPOS:
+ case OP_SBRAPOS:
+ case OP_SCBRAPOS:
+ case OP_BRAPOSZERO:
+ cc = compile_bracketpos_matchingpath(common, cc, parent);
+ break;
+
+ case OP_MARK:
+ PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc);
+ SLJIT_ASSERT(common->mark_ptr != 0);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr);
+ allocate_stack(common, common->has_skip_arg ? 5 : 1);
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0), TMP2, 0);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0);
+ if (common->has_skip_arg)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0);
+ }
+ cc += 1 + 2 + cc[1];
+ break;
+
+ case OP_PRUNE:
+ case OP_PRUNE_ARG:
+ case OP_SKIP:
+ case OP_SKIP_ARG:
+ case OP_THEN:
+ case OP_THEN_ARG:
+ case OP_COMMIT:
+ case OP_COMMIT_ARG:
+ cc = compile_control_verb_matchingpath(common, cc, parent);
+ break;
+
+ case OP_FAIL:
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ cc = compile_fail_accept_matchingpath(common, cc, parent);
+ break;
+
+ case OP_CLOSE:
+ cc = compile_close_matchingpath(common, cc);
+ break;
+
+ case OP_SKIPZERO:
+ cc = bracketend(cc + 1);
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ return;
+ }
+ if (cc == NULL)
+ return;
+ }
+
+if (has_then_trap)
+ {
+ /* Head item on backtrack. */
+ PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc);
+ BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode;
+ BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap;
+ common->then_trap = save_then_trap;
+ }
+SLJIT_ASSERT(cc == ccend);
+}
+
+#undef PUSH_BACKTRACK
+#undef PUSH_BACKTRACK_NOVALUE
+#undef BACKTRACK_AS
+
+#define COMPILE_BACKTRACKINGPATH(current) \
+ do \
+ { \
+ compile_backtrackingpath(common, (current)); \
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \
+ return; \
+ } \
+ while (0)
+
+#define CURRENT_AS(type) ((type *)current)
+
+static void compile_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+PCRE2_SPTR cc = current->cc;
+PCRE2_UCHAR opcode;
+PCRE2_UCHAR type;
+sljit_u32 max = 0, exact;
+struct sljit_label *label = NULL;
+struct sljit_jump *jump = NULL;
+jump_list *jumplist = NULL;
+PCRE2_SPTR end;
+int private_data_ptr = PRIVATE_DATA(cc);
+int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP);
+int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr;
+int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw);
+
+cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end);
+
+switch(opcode)
+ {
+ case OP_STAR:
+ case OP_UPTO:
+ if (type == OP_ANYNL || type == OP_EXTUNI)
+ {
+ SLJIT_ASSERT(private_data_ptr == 0);
+ set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ }
+ else
+ {
+ if (CURRENT_AS(char_iterator_backtrack)->u.charpos.enabled)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP1(SLJIT_MOV, TMP2, 0, base, offset1);
+ OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
+
+ jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
+ label = LABEL();
+ OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ if (CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit != 0)
+ OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit);
+ CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.chr, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ skip_char_back(common);
+ CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP2, 0, label);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, base, offset1);
+ skip_char_back(common);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ }
+ JUMPHERE(jump);
+ if (private_data_ptr == 0)
+ free_stack(common, 2);
+ }
+ break;
+
+ case OP_MINSTAR:
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ set_jumps(jumplist, LABEL());
+ if (private_data_ptr == 0)
+ free_stack(common, 1);
+ break;
+
+ case OP_MINUPTO:
+ OP1(SLJIT_MOV, TMP1, 0, base, offset1);
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+ add_jump(compiler, &jumplist, JUMP(SLJIT_ZERO));
+
+ OP1(SLJIT_MOV, base, offset1, TMP1, 0);
+ compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
+ OP1(SLJIT_MOV, base, offset0, STR_PTR, 0);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+
+ set_jumps(jumplist, LABEL());
+ if (private_data_ptr == 0)
+ free_stack(common, 2);
+ break;
+
+ case OP_QUERY:
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ jump = JUMP(SLJIT_JUMP);
+ set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ JUMPHERE(jump);
+ if (private_data_ptr == 0)
+ free_stack(common, 1);
+ break;
+
+ case OP_MINQUERY:
+ OP1(SLJIT_MOV, STR_PTR, 0, base, offset0);
+ OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0);
+ jump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ compile_char1_matchingpath(common, type, cc, &jumplist, TRUE);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath);
+ set_jumps(jumplist, LABEL());
+ JUMPHERE(jump);
+ if (private_data_ptr == 0)
+ free_stack(common, 1);
+ break;
+
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+
+set_jumps(current->topbacktracks, LABEL());
+}
+
+static SLJIT_INLINE void compile_ref_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+PCRE2_SPTR cc = current->cc;
+BOOL ref = (*cc == OP_REF || *cc == OP_REFI);
+PCRE2_UCHAR type;
+
+type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE];
+
+if ((type & 0x1) == 0)
+ {
+ /* Maximize case. */
+ set_jumps(current->topbacktracks, LABEL());
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath);
+ return;
+ }
+
+OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath);
+set_jumps(current->topbacktracks, LABEL());
+free_stack(common, ref ? 2 : 3);
+}
+
+static SLJIT_INLINE void compile_recurse_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+recurse_entry *entry;
+
+if (!CURRENT_AS(recurse_backtrack)->inlined_pattern)
+ {
+ entry = CURRENT_AS(recurse_backtrack)->entry;
+ if (entry->backtrack_label == NULL)
+ add_jump(compiler, &entry->backtrack_calls, JUMP(SLJIT_FAST_CALL));
+ else
+ JUMPTO(SLJIT_FAST_CALL, entry->backtrack_label);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(recurse_backtrack)->matchingpath);
+ }
+else
+ compile_backtrackingpath(common, current->top);
+
+set_jumps(current->topbacktracks, LABEL());
+}
+
+static void compile_assert_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+PCRE2_SPTR cc = current->cc;
+PCRE2_UCHAR bra = OP_BRA;
+struct sljit_jump *brajump = NULL;
+
+SLJIT_ASSERT(*cc != OP_BRAMINZERO);
+if (*cc == OP_BRAZERO)
+ {
+ bra = *cc;
+ cc++;
+ }
+
+if (bra == OP_BRAZERO)
+ {
+ SLJIT_ASSERT(current->topbacktracks == NULL);
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ }
+
+if (CURRENT_AS(assert_backtrack)->framesize < 0)
+ {
+ set_jumps(current->topbacktracks, LABEL());
+
+ if (bra == OP_BRAZERO)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath);
+ free_stack(common, 1);
+ }
+ return;
+ }
+
+if (bra == OP_BRAZERO)
+ {
+ if (*cc == OP_ASSERT_NOT || *cc == OP_ASSERTBACK_NOT)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath);
+ free_stack(common, 1);
+ return;
+ }
+ free_stack(common, 1);
+ brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0);
+ }
+
+if (*cc == OP_ASSERT || *cc == OP_ASSERTBACK)
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(assert_backtrack)->framesize - 1) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr, TMP1, 0);
+
+ set_jumps(current->topbacktracks, LABEL());
+ }
+else
+ set_jumps(current->topbacktracks, LABEL());
+
+if (bra == OP_BRAZERO)
+ {
+ /* We know there is enough place on the stack. */
+ OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0);
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(assert_backtrack)->matchingpath);
+ JUMPHERE(brajump);
+ }
+}
+
+static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+int opcode, stacksize, alt_count, alt_max;
+int offset = 0;
+int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr;
+int repeat_ptr = 0, repeat_type = 0, repeat_count = 0;
+PCRE2_SPTR cc = current->cc;
+PCRE2_SPTR ccbegin;
+PCRE2_SPTR ccprev;
+PCRE2_UCHAR bra = OP_BRA;
+PCRE2_UCHAR ket;
+assert_backtrack *assert;
+sljit_uw *next_update_addr = NULL;
+BOOL has_alternatives;
+BOOL needs_control_head = FALSE;
+struct sljit_jump *brazero = NULL;
+struct sljit_jump *alt1 = NULL;
+struct sljit_jump *alt2 = NULL;
+struct sljit_jump *once = NULL;
+struct sljit_jump *cond = NULL;
+struct sljit_label *rmin_label = NULL;
+struct sljit_label *exact_label = NULL;
+
+if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO)
+ {
+ bra = *cc;
+ cc++;
+ }
+
+opcode = *cc;
+ccbegin = bracketend(cc) - 1 - LINK_SIZE;
+ket = *ccbegin;
+if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0)
+ {
+ repeat_ptr = PRIVATE_DATA(ccbegin);
+ repeat_type = PRIVATE_DATA(ccbegin + 2);
+ repeat_count = PRIVATE_DATA(ccbegin + 3);
+ SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0);
+ if (repeat_type == OP_UPTO)
+ ket = OP_KETRMAX;
+ if (repeat_type == OP_MINUPTO)
+ ket = OP_KETRMIN;
+ }
+ccbegin = cc;
+cc += GET(cc, 1);
+has_alternatives = *cc == OP_ALT;
+if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
+ has_alternatives = (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT) || CURRENT_AS(bracket_backtrack)->u.condfailed != NULL;
+if (opcode == OP_CBRA || opcode == OP_SCBRA)
+ offset = (GET2(ccbegin, 1 + LINK_SIZE)) << 1;
+if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN))
+ opcode = OP_SCOND;
+
+alt_max = has_alternatives ? no_alternatives(ccbegin) : 0;
+
+/* Decoding the needs_control_head in framesize. */
+if (opcode == OP_ONCE)
+ {
+ needs_control_head = (CURRENT_AS(bracket_backtrack)->u.framesize & 0x1) != 0;
+ CURRENT_AS(bracket_backtrack)->u.framesize >>= 1;
+ }
+
+if (ket != OP_KET && repeat_type != 0)
+ {
+ /* TMP1 is used in OP_KETRMIN below. */
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ if (repeat_type == OP_UPTO)
+ OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0, SLJIT_IMM, 1);
+ else
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0);
+ }
+
+if (ket == OP_KETRMAX)
+ {
+ if (bra == OP_BRAZERO)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ brazero = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
+ }
+ }
+else if (ket == OP_KETRMIN)
+ {
+ if (bra != OP_BRAMINZERO)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ if (repeat_type != 0)
+ {
+ /* TMP1 was set a few lines above. */
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
+ /* Drop STR_PTR for non-greedy plus quantifier. */
+ if (opcode != OP_ONCE)
+ free_stack(common, 1);
+ }
+ else if (opcode >= OP_SBRA || opcode == OP_ONCE)
+ {
+ /* Checking zero-length iteration. */
+ if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0)
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
+ else
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 2), CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
+ }
+ /* Drop STR_PTR for non-greedy plus quantifier. */
+ if (opcode != OP_ONCE)
+ free_stack(common, 1);
+ }
+ else
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
+ }
+ rmin_label = LABEL();
+ if (repeat_type != 0)
+ OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
+ }
+else if (bra == OP_BRAZERO)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ brazero = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0);
+ }
+else if (repeat_type == OP_EXACT)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
+ exact_label = LABEL();
+ }
+
+if (offset != 0)
+ {
+ if (common->capture_last_ptr != 0)
+ {
+ SLJIT_ASSERT(common->optimized_cbracket[offset >> 1] == 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
+ free_stack(common, 3);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP2, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0);
+ }
+ else if (common->optimized_cbracket[offset >> 1] == 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ free_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
+ }
+ }
+
+if (SLJIT_UNLIKELY(opcode == OP_ONCE))
+ {
+ if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(bracket_backtrack)->u.framesize - 1) * sizeof(sljit_sw));
+ }
+ once = JUMP(SLJIT_JUMP);
+ }
+else if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
+ {
+ if (has_alternatives)
+ {
+ /* Always exactly one alternative. */
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+
+ alt_max = 2;
+ alt1 = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
+ }
+ }
+else if (has_alternatives)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+
+ if (alt_max > 4)
+ {
+ /* Table jump if alt_max is greater than 4. */
+ next_update_addr = allocate_read_only_data(common, alt_max * sizeof(sljit_uw));
+ if (SLJIT_UNLIKELY(next_update_addr == NULL))
+ return;
+ sljit_emit_ijump(compiler, SLJIT_JUMP, SLJIT_MEM1(TMP1), (sljit_sw)next_update_addr);
+ add_label_addr(common, next_update_addr++);
+ }
+ else
+ {
+ if (alt_max == 4)
+ alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
+ alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
+ }
+ }
+
+COMPILE_BACKTRACKINGPATH(current->top);
+if (current->topbacktracks)
+ set_jumps(current->topbacktracks, LABEL());
+
+if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND))
+ {
+ /* Conditional block always has at most one alternative. */
+ if (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT)
+ {
+ SLJIT_ASSERT(has_alternatives);
+ assert = CURRENT_AS(bracket_backtrack)->u.assert;
+ if (assert->framesize >= 0 && (ccbegin[1 + LINK_SIZE] == OP_ASSERT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK))
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (assert->framesize - 1) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, TMP1, 0);
+ }
+ cond = JUMP(SLJIT_JUMP);
+ set_jumps(CURRENT_AS(bracket_backtrack)->u.assert->condfailed, LABEL());
+ }
+ else if (CURRENT_AS(bracket_backtrack)->u.condfailed != NULL)
+ {
+ SLJIT_ASSERT(has_alternatives);
+ cond = JUMP(SLJIT_JUMP);
+ set_jumps(CURRENT_AS(bracket_backtrack)->u.condfailed, LABEL());
+ }
+ else
+ SLJIT_ASSERT(!has_alternatives);
+ }
+
+if (has_alternatives)
+ {
+ alt_count = sizeof(sljit_uw);
+ do
+ {
+ current->top = NULL;
+ current->topbacktracks = NULL;
+ current->nextbacktracks = NULL;
+ /* Conditional blocks always have an additional alternative, even if it is empty. */
+ if (*cc == OP_ALT)
+ {
+ ccprev = cc + 1 + LINK_SIZE;
+ cc += GET(cc, 1);
+ if (opcode != OP_COND && opcode != OP_SCOND)
+ {
+ if (opcode != OP_ONCE)
+ {
+ if (private_data_ptr != 0)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr);
+ else
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ }
+ else
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(needs_control_head ? 1 : 0));
+ }
+ compile_matchingpath(common, ccprev, cc, current);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return;
+ }
+
+ /* Instructions after the current alternative is successfully matched. */
+ /* There is a similar code in compile_bracket_matchingpath. */
+ if (opcode == OP_ONCE)
+ match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head);
+
+ stacksize = 0;
+ if (repeat_type == OP_MINUPTO)
+ {
+ /* We need to preserve the counter. TMP2 will be used below. */
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr);
+ stacksize++;
+ }
+ if (ket != OP_KET || bra != OP_BRA)
+ stacksize++;
+ if (offset != 0)
+ {
+ if (common->capture_last_ptr != 0)
+ stacksize++;
+ if (common->optimized_cbracket[offset >> 1] == 0)
+ stacksize += 2;
+ }
+ if (opcode != OP_ONCE)
+ stacksize++;
+
+ if (stacksize > 0)
+ allocate_stack(common, stacksize);
+
+ stacksize = 0;
+ if (repeat_type == OP_MINUPTO)
+ {
+ /* TMP2 was set above. */
+ OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1);
+ stacksize++;
+ }
+
+ if (ket != OP_KET || bra != OP_BRA)
+ {
+ if (ket != OP_KET)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0);
+ else
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0);
+ stacksize++;
+ }
+
+ if (offset != 0)
+ stacksize = match_capture_common(common, stacksize, offset, private_data_ptr);
+
+ if (opcode != OP_ONCE)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, alt_count);
+
+ if (offset != 0 && ket == OP_KETRMAX && common->optimized_cbracket[offset >> 1] != 0)
+ {
+ /* If ket is not OP_KETRMAX, this code path is executed after the jump to alternative_matchingpath. */
+ SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0);
+ }
+
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->alternative_matchingpath);
+
+ if (opcode != OP_ONCE)
+ {
+ if (alt_max > 4)
+ add_label_addr(common, next_update_addr++);
+ else
+ {
+ if (alt_count != 2 * sizeof(sljit_uw))
+ {
+ JUMPHERE(alt1);
+ if (alt_max == 3 && alt_count == sizeof(sljit_uw))
+ alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
+ }
+ else
+ {
+ JUMPHERE(alt2);
+ if (alt_max == 4)
+ alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_uw));
+ }
+ }
+ alt_count += sizeof(sljit_uw);
+ }
+
+ COMPILE_BACKTRACKINGPATH(current->top);
+ if (current->topbacktracks)
+ set_jumps(current->topbacktracks, LABEL());
+ SLJIT_ASSERT(!current->nextbacktracks);
+ }
+ while (*cc == OP_ALT);
+
+ if (cond != NULL)
+ {
+ SLJIT_ASSERT(opcode == OP_COND || opcode == OP_SCOND);
+ assert = CURRENT_AS(bracket_backtrack)->u.assert;
+ if ((ccbegin[1 + LINK_SIZE] == OP_ASSERT_NOT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK_NOT) && assert->framesize >= 0)
+ {
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr);
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-2));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (assert->framesize - 1) * sizeof(sljit_sw));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, TMP1, 0);
+ }
+ JUMPHERE(cond);
+ }
+
+ /* Free the STR_PTR. */
+ if (private_data_ptr == 0)
+ free_stack(common, 1);
+ }
+
+if (offset != 0)
+ {
+ /* Using both tmp register is better for instruction scheduling. */
+ if (common->optimized_cbracket[offset >> 1] != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ free_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
+ }
+ else
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+ }
+else if (opcode == OP_SBRA || opcode == OP_SCOND)
+ {
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ }
+else if (opcode == OP_ONCE)
+ {
+ cc = ccbegin + GET(ccbegin, 1);
+ stacksize = needs_control_head ? 1 : 0;
+
+ if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
+ {
+ /* Reset head and drop saved frame. */
+ stacksize += CURRENT_AS(bracket_backtrack)->u.framesize + ((ket != OP_KET || *cc == OP_ALT) ? 2 : 1);
+ }
+ else if (ket == OP_KETRMAX || (*cc == OP_ALT && ket != OP_KETRMIN))
+ {
+ /* The STR_PTR must be released. */
+ stacksize++;
+ }
+
+ if (stacksize > 0)
+ free_stack(common, stacksize);
+
+ JUMPHERE(once);
+ /* Restore previous private_data_ptr */
+ if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 1));
+ else if (ket == OP_KETRMIN)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ /* See the comment below. */
+ free_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0);
+ }
+ }
+
+if (repeat_type == OP_EXACT)
+ {
+ OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0);
+ CMPTO(SLJIT_LESS_EQUAL, TMP1, 0, SLJIT_IMM, repeat_count, exact_label);
+ }
+else if (ket == OP_KETRMAX)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ if (bra != OP_BRAZERO)
+ free_stack(common, 1);
+
+ CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath);
+ if (bra == OP_BRAZERO)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath);
+ JUMPHERE(brazero);
+ free_stack(common, 1);
+ }
+ }
+else if (ket == OP_KETRMIN)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+
+ /* OP_ONCE removes everything in case of a backtrack, so we don't
+ need to explicitly release the STR_PTR. The extra release would
+ affect badly the free_stack(2) above. */
+ if (opcode != OP_ONCE)
+ free_stack(common, 1);
+ CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rmin_label);
+ if (opcode == OP_ONCE)
+ free_stack(common, bra == OP_BRAMINZERO ? 2 : 1);
+ else if (bra == OP_BRAMINZERO)
+ free_stack(common, 1);
+ }
+else if (bra == OP_BRAZERO)
+ {
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath);
+ JUMPHERE(brazero);
+ }
+}
+
+static SLJIT_INLINE void compile_bracketpos_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+int offset;
+struct sljit_jump *jump;
+
+if (CURRENT_AS(bracketpos_backtrack)->framesize < 0)
+ {
+ if (*current->cc == OP_CBRAPOS || *current->cc == OP_SCBRAPOS)
+ {
+ offset = (GET2(current->cc, 1 + LINK_SIZE)) << 1;
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0);
+ if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2));
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0);
+ if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0);
+ }
+ set_jumps(current->topbacktracks, LABEL());
+ free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize);
+ return;
+ }
+
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr);
+add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(bracketpos_backtrack)->framesize - 1) * sizeof(sljit_sw));
+
+if (current->topbacktracks)
+ {
+ jump = JUMP(SLJIT_JUMP);
+ set_jumps(current->topbacktracks, LABEL());
+ /* Drop the stack frame. */
+ free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize);
+ JUMPHERE(jump);
+ }
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracketpos_backtrack)->framesize - 1));
+}
+
+static SLJIT_INLINE void compile_braminzero_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+assert_backtrack backtrack;
+
+current->top = NULL;
+current->topbacktracks = NULL;
+current->nextbacktracks = NULL;
+if (current->cc[1] > OP_ASSERTBACK_NOT)
+ {
+ /* Manual call of compile_bracket_matchingpath and compile_bracket_backtrackingpath. */
+ compile_bracket_matchingpath(common, current->cc, current);
+ compile_bracket_backtrackingpath(common, current->top);
+ }
+else
+ {
+ memset(&backtrack, 0, sizeof(backtrack));
+ backtrack.common.cc = current->cc;
+ backtrack.matchingpath = CURRENT_AS(braminzero_backtrack)->matchingpath;
+ /* Manual call of compile_assert_matchingpath. */
+ compile_assert_matchingpath(common, current->cc, &backtrack, FALSE);
+ }
+SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks);
+}
+
+static SLJIT_INLINE void compile_control_verb_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+PCRE2_UCHAR opcode = *current->cc;
+struct sljit_label *loop;
+struct sljit_jump *jump;
+
+if (opcode == OP_THEN || opcode == OP_THEN_ARG)
+ {
+ if (common->then_trap != NULL)
+ {
+ SLJIT_ASSERT(common->control_head_ptr != 0);
+
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, type_then_trap);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, common->then_trap->start);
+ jump = JUMP(SLJIT_JUMP);
+
+ loop = LABEL();
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ JUMPHERE(jump);
+ CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0, loop);
+ CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0, loop);
+ add_jump(compiler, &common->then_trap->quit, JUMP(SLJIT_JUMP));
+ return;
+ }
+ else if (!common->local_quit_available && common->in_positive_assertion)
+ {
+ add_jump(compiler, &common->positive_assertion_quit, JUMP(SLJIT_JUMP));
+ return;
+ }
+ }
+
+if (common->local_quit_available)
+ {
+ /* Abort match with a fail. */
+ if (common->quit_label == NULL)
+ add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
+ else
+ JUMPTO(SLJIT_JUMP, common->quit_label);
+ return;
+ }
+
+if (opcode == OP_SKIP_ARG)
+ {
+ SLJIT_ASSERT(common->control_head_ptr != 0 && TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr);
+ OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, (sljit_sw)(current->cc + 2));
+ sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_search_mark));
+
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_R0, 0);
+ add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, SLJIT_R0, 0, SLJIT_IMM, 0));
+ return;
+ }
+
+if (opcode == OP_SKIP)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+else
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_IMM, 0);
+add_jump(compiler, &common->reset_match, JUMP(SLJIT_JUMP));
+}
+
+static SLJIT_INLINE void compile_then_trap_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+struct sljit_jump *jump;
+int size;
+
+if (CURRENT_AS(then_trap_backtrack)->then_trap)
+ {
+ common->then_trap = CURRENT_AS(then_trap_backtrack)->then_trap;
+ return;
+ }
+
+size = CURRENT_AS(then_trap_backtrack)->framesize;
+size = 3 + (size < 0 ? 0 : size);
+
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(size - 3));
+free_stack(common, size);
+jump = JUMP(SLJIT_JUMP);
+
+set_jumps(CURRENT_AS(then_trap_backtrack)->quit, LABEL());
+/* STACK_TOP is set by THEN. */
+if (CURRENT_AS(then_trap_backtrack)->framesize >= 0)
+ {
+ add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL));
+ OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (CURRENT_AS(then_trap_backtrack)->framesize - 1) * sizeof(sljit_sw));
+ }
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+free_stack(common, 3);
+
+JUMPHERE(jump);
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0);
+}
+
+static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current)
+{
+DEFINE_COMPILER;
+then_trap_backtrack *save_then_trap = common->then_trap;
+
+while (current)
+ {
+ if (current->nextbacktracks != NULL)
+ set_jumps(current->nextbacktracks, LABEL());
+ switch(*current->cc)
+ {
+ case OP_SET_SOM:
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), TMP1, 0);
+ break;
+
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ case OP_TYPEPOSUPTO:
+ case OP_CLASS:
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+#endif
+ compile_iterator_backtrackingpath(common, current);
+ break;
+
+ case OP_REF:
+ case OP_REFI:
+ case OP_DNREF:
+ case OP_DNREFI:
+ compile_ref_iterator_backtrackingpath(common, current);
+ break;
+
+ case OP_RECURSE:
+ compile_recurse_backtrackingpath(common, current);
+ break;
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ compile_assert_backtrackingpath(common, current);
+ break;
+
+ case OP_ONCE:
+ case OP_BRA:
+ case OP_CBRA:
+ case OP_COND:
+ case OP_SBRA:
+ case OP_SCBRA:
+ case OP_SCOND:
+ compile_bracket_backtrackingpath(common, current);
+ break;
+
+ case OP_BRAZERO:
+ if (current->cc[1] > OP_ASSERTBACK_NOT)
+ compile_bracket_backtrackingpath(common, current);
+ else
+ compile_assert_backtrackingpath(common, current);
+ break;
+
+ case OP_BRAPOS:
+ case OP_CBRAPOS:
+ case OP_SBRAPOS:
+ case OP_SCBRAPOS:
+ case OP_BRAPOSZERO:
+ compile_bracketpos_backtrackingpath(common, current);
+ break;
+
+ case OP_BRAMINZERO:
+ compile_braminzero_backtrackingpath(common, current);
+ break;
+
+ case OP_MARK:
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0));
+ if (common->has_skip_arg)
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ free_stack(common, common->has_skip_arg ? 5 : 1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP1, 0);
+ if (common->has_skip_arg)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP2, 0);
+ break;
+
+ case OP_THEN:
+ case OP_THEN_ARG:
+ case OP_PRUNE:
+ case OP_PRUNE_ARG:
+ case OP_SKIP:
+ case OP_SKIP_ARG:
+ compile_control_verb_backtrackingpath(common, current);
+ break;
+
+ case OP_COMMIT:
+ case OP_COMMIT_ARG:
+ if (!common->local_quit_available)
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
+ if (common->quit_label == NULL)
+ add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP));
+ else
+ JUMPTO(SLJIT_JUMP, common->quit_label);
+ break;
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+ case OP_FAIL:
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ set_jumps(current->topbacktracks, LABEL());
+ break;
+
+ case OP_THEN_TRAP:
+ /* A virtual opcode for then traps. */
+ compile_then_trap_backtrackingpath(common, current);
+ break;
+
+ default:
+ SLJIT_UNREACHABLE();
+ break;
+ }
+ current = current->prev;
+ }
+common->then_trap = save_then_trap;
+}
+
+static SLJIT_INLINE void compile_recurse(compiler_common *common)
+{
+DEFINE_COMPILER;
+PCRE2_SPTR cc = common->start + common->currententry->start;
+PCRE2_SPTR ccbegin = cc + 1 + LINK_SIZE + (*cc == OP_BRA ? 0 : IMM2_SIZE);
+PCRE2_SPTR ccend = bracketend(cc) - (1 + LINK_SIZE);
+BOOL needs_control_head;
+BOOL has_quit;
+BOOL has_accept;
+int private_data_size = get_recurse_data_length(common, ccbegin, ccend, &needs_control_head, &has_quit, &has_accept);
+int alt_count, alt_max, local_size;
+backtrack_common altbacktrack;
+jump_list *match = NULL;
+sljit_uw *next_update_addr = NULL;
+struct sljit_jump *alt1 = NULL;
+struct sljit_jump *alt2 = NULL;
+struct sljit_jump *accept_exit = NULL;
+struct sljit_label *quit;
+
+/* Recurse captures then. */
+common->then_trap = NULL;
+
+SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA || *cc == OP_CBRAPOS || *cc == OP_SCBRA || *cc == OP_SCBRAPOS);
+
+alt_max = no_alternatives(cc);
+alt_count = 0;
+
+/* Matching path. */
+SLJIT_ASSERT(common->currententry->entry_label == NULL && common->recursive_head_ptr != 0);
+common->currententry->entry_label = LABEL();
+set_jumps(common->currententry->entry_calls, common->currententry->entry_label);
+
+sljit_emit_fast_enter(compiler, TMP2, 0);
+count_match(common);
+
+local_size = (alt_max > 1) ? 2 : 1;
+
+/* (Reversed) stack layout:
+ [private data][return address][optional: str ptr] ... [optional: alternative index][recursive_head_ptr] */
+
+allocate_stack(common, private_data_size + local_size);
+/* Save return address. */
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1), TMP2, 0);
+
+copy_recurse_data(common, ccbegin, ccend, recurse_copy_from_global, local_size, private_data_size + local_size, has_quit);
+
+/* This variable is saved and restored all time when we enter or exit from a recursive context. */
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr, STACK_TOP, 0);
+
+if (needs_control_head)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
+
+if (alt_max > 1)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0);
+
+memset(&altbacktrack, 0, sizeof(backtrack_common));
+common->quit_label = NULL;
+common->accept_label = NULL;
+common->quit = NULL;
+common->accept = NULL;
+altbacktrack.cc = ccbegin;
+cc += GET(cc, 1);
+while (1)
+ {
+ altbacktrack.top = NULL;
+ altbacktrack.topbacktracks = NULL;
+
+ if (altbacktrack.cc != ccbegin)
+ OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+
+ compile_matchingpath(common, altbacktrack.cc, cc, &altbacktrack);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return;
+
+ allocate_stack(common, (alt_max > 1 || has_accept) ? 2 : 1);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
+
+ if (alt_max > 1 || has_accept)
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_count);
+
+ add_jump(compiler, &match, JUMP(SLJIT_JUMP));
+
+ if (alt_count == 0)
+ {
+ /* Backtracking path entry. */
+ SLJIT_ASSERT(common->currententry->backtrack_label == NULL);
+ common->currententry->backtrack_label = LABEL();
+ set_jumps(common->currententry->backtrack_calls, common->currententry->backtrack_label);
+
+ sljit_emit_fast_enter(compiler, TMP1, 0);
+
+ if (has_accept)
+ accept_exit = CMP(SLJIT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_max * sizeof (sljit_sw));
+
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0));
+ /* Save return address. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), STACK(local_size - 1), TMP1, 0);
+
+ copy_recurse_data(common, ccbegin, ccend, recurse_swap_global, local_size, private_data_size + local_size, has_quit);
+
+ if (alt_max > 1)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1));
+ free_stack(common, 2);
+
+ if (alt_max > 4)
+ {
+ /* Table jump if alt_max is greater than 4. */
+ next_update_addr = allocate_read_only_data(common, alt_max * sizeof(sljit_uw));
+ if (SLJIT_UNLIKELY(next_update_addr == NULL))
+ return;
+ sljit_emit_ijump(compiler, SLJIT_JUMP, SLJIT_MEM1(TMP1), (sljit_sw)next_update_addr);
+ add_label_addr(common, next_update_addr++);
+ }
+ else
+ {
+ if (alt_max == 4)
+ alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
+ alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw));
+ }
+ }
+ else
+ free_stack(common, has_accept ? 2 : 1);
+ }
+ else if (alt_max > 4)
+ add_label_addr(common, next_update_addr++);
+ else
+ {
+ if (alt_count != 2 * sizeof(sljit_uw))
+ {
+ JUMPHERE(alt1);
+ if (alt_max == 3 && alt_count == sizeof(sljit_uw))
+ alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw));
+ }
+ else
+ {
+ JUMPHERE(alt2);
+ if (alt_max == 4)
+ alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_uw));
+ }
+ }
+
+ alt_count += sizeof(sljit_uw);
+
+ compile_backtrackingpath(common, altbacktrack.top);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ return;
+ set_jumps(altbacktrack.topbacktracks, LABEL());
+
+ if (*cc != OP_ALT)
+ break;
+
+ altbacktrack.cc = cc + 1 + LINK_SIZE;
+ cc += GET(cc, 1);
+ }
+
+/* No alternative is matched. */
+
+quit = LABEL();
+
+copy_recurse_data(common, ccbegin, ccend, recurse_copy_private_to_global, local_size, private_data_size + local_size, has_quit);
+
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1));
+free_stack(common, private_data_size + local_size);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+sljit_emit_fast_return(compiler, TMP2, 0);
+
+if (common->quit != NULL)
+ {
+ SLJIT_ASSERT(has_quit);
+
+ set_jumps(common->quit, LABEL());
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
+ copy_recurse_data(common, ccbegin, ccend, recurse_copy_shared_to_global, local_size, private_data_size + local_size, has_quit);
+ JUMPTO(SLJIT_JUMP, quit);
+ }
+
+if (has_accept)
+ {
+ JUMPHERE(accept_exit);
+ free_stack(common, 2);
+
+ /* Save return address. */
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1), TMP1, 0);
+
+ copy_recurse_data(common, ccbegin, ccend, recurse_copy_kept_shared_to_global, local_size, private_data_size + local_size, has_quit);
+
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(local_size - 1));
+ free_stack(common, private_data_size + local_size);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
+ sljit_emit_fast_return(compiler, TMP2, 0);
+ }
+
+if (common->accept != NULL)
+ {
+ SLJIT_ASSERT(has_accept);
+
+ set_jumps(common->accept, LABEL());
+
+ OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr);
+ OP1(SLJIT_MOV, TMP2, 0, STACK_TOP, 0);
+
+ allocate_stack(common, 2);
+ OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, alt_count);
+ }
+
+set_jumps(match, LABEL());
+
+OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0);
+
+copy_recurse_data(common, ccbegin, ccend, recurse_swap_global, local_size, private_data_size + local_size, has_quit);
+
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP2), STACK(local_size - 1));
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 1);
+sljit_emit_fast_return(compiler, TMP2, 0);
+}
+
+#undef COMPILE_BACKTRACKINGPATH
+#undef CURRENT_AS
+
+static int jit_compile(pcre2_code *code, sljit_u32 mode)
+{
+pcre2_real_code *re = (pcre2_real_code *)code;
+struct sljit_compiler *compiler;
+backtrack_common rootbacktrack;
+compiler_common common_data;
+compiler_common *common = &common_data;
+const sljit_u8 *tables = re->tables;
+void *allocator_data = &re->memctl;
+int private_data_size;
+PCRE2_SPTR ccend;
+executable_functions *functions;
+void *executable_func;
+sljit_uw executable_size;
+sljit_uw total_length;
+label_addr_list *label_addr;
+struct sljit_label *mainloop_label = NULL;
+struct sljit_label *continue_match_label;
+struct sljit_label *empty_match_found_label = NULL;
+struct sljit_label *empty_match_backtrack_label = NULL;
+struct sljit_label *reset_match_label;
+struct sljit_label *quit_label;
+struct sljit_jump *jump;
+struct sljit_jump *minlength_check_failed = NULL;
+struct sljit_jump *reqbyte_notfound = NULL;
+struct sljit_jump *empty_match = NULL;
+struct sljit_jump *end_anchor_failed = NULL;
+
+SLJIT_ASSERT(tables);
+
+memset(&rootbacktrack, 0, sizeof(backtrack_common));
+memset(common, 0, sizeof(compiler_common));
+common->re = re;
+common->name_table = (PCRE2_SPTR)((uint8_t *)re + sizeof(pcre2_real_code));
+rootbacktrack.cc = common->name_table + re->name_count * re->name_entry_size;
+
+common->start = rootbacktrack.cc;
+common->read_only_data_head = NULL;
+common->fcc = tables + fcc_offset;
+common->lcc = (sljit_sw)(tables + lcc_offset);
+common->mode = mode;
+common->might_be_empty = re->minlength == 0;
+common->nltype = NLTYPE_FIXED;
+switch(re->newline_convention)
+ {
+ case PCRE2_NEWLINE_CR: common->newline = CHAR_CR; break;
+ case PCRE2_NEWLINE_LF: common->newline = CHAR_NL; break;
+ case PCRE2_NEWLINE_CRLF: common->newline = (CHAR_CR << 8) | CHAR_NL; break;
+ case PCRE2_NEWLINE_ANY: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANY; break;
+ case PCRE2_NEWLINE_ANYCRLF: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANYCRLF; break;
+ default: return PCRE2_ERROR_INTERNAL;
+ }
+common->nlmax = READ_CHAR_MAX;
+common->nlmin = 0;
+if (re->bsr_convention == PCRE2_BSR_UNICODE)
+ common->bsr_nltype = NLTYPE_ANY;
+else if (re->bsr_convention == PCRE2_BSR_ANYCRLF)
+ common->bsr_nltype = NLTYPE_ANYCRLF;
+else
+ {
+#ifdef BSR_ANYCRLF
+ common->bsr_nltype = NLTYPE_ANYCRLF;
+#else
+ common->bsr_nltype = NLTYPE_ANY;
+#endif
+ }
+common->bsr_nlmax = READ_CHAR_MAX;
+common->bsr_nlmin = 0;
+common->endonly = (re->overall_options & PCRE2_DOLLAR_ENDONLY) != 0;
+common->ctypes = (sljit_sw)(tables + ctypes_offset);
+common->name_count = re->name_count;
+common->name_entry_size = re->name_entry_size;
+common->unset_backref = (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) != 0;
+common->alt_circumflex = (re->overall_options & PCRE2_ALT_CIRCUMFLEX) != 0;
+#ifdef SUPPORT_UNICODE
+/* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
+common->utf = (re->overall_options & PCRE2_UTF) != 0;
+common->use_ucp = (re->overall_options & PCRE2_UCP) != 0;
+if (common->utf)
+ {
+ if (common->nltype == NLTYPE_ANY)
+ common->nlmax = 0x2029;
+ else if (common->nltype == NLTYPE_ANYCRLF)
+ common->nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL;
+ else
+ {
+ /* We only care about the first newline character. */
+ common->nlmax = common->newline & 0xff;
+ }
+
+ if (common->nltype == NLTYPE_FIXED)
+ common->nlmin = common->newline & 0xff;
+ else
+ common->nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL;
+
+ if (common->bsr_nltype == NLTYPE_ANY)
+ common->bsr_nlmax = 0x2029;
+ else
+ common->bsr_nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL;
+ common->bsr_nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL;
+ }
+#endif /* SUPPORT_UNICODE */
+ccend = bracketend(common->start);
+
+/* Calculate the local space size on the stack. */
+common->ovector_start = LIMIT_MATCH + sizeof(sljit_sw);
+common->optimized_cbracket = (sljit_u8 *)SLJIT_MALLOC(re->top_bracket + 1, allocator_data);
+if (!common->optimized_cbracket)
+ return PCRE2_ERROR_NOMEMORY;
+#if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 1
+memset(common->optimized_cbracket, 0, re->top_bracket + 1);
+#else
+memset(common->optimized_cbracket, 1, re->top_bracket + 1);
+#endif
+
+SLJIT_ASSERT(*common->start == OP_BRA && ccend[-(1 + LINK_SIZE)] == OP_KET);
+#if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 2
+common->capture_last_ptr = common->ovector_start;
+common->ovector_start += sizeof(sljit_sw);
+#endif
+if (!check_opcode_types(common, common->start, ccend))
+ {
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+/* Checking flags and updating ovector_start. */
+if (mode == PCRE2_JIT_COMPLETE && (re->flags & PCRE2_LASTSET) != 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ common->req_char_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+if (mode != PCRE2_JIT_COMPLETE)
+ {
+ common->start_used_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ if (mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ common->hit_start = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+ }
+if ((re->overall_options & (PCRE2_FIRSTLINE | PCRE2_USE_OFFSET_LIMIT)) != 0)
+ {
+ common->match_end_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD
+common->control_head_ptr = 1;
+#endif
+if (common->control_head_ptr != 0)
+ {
+ common->control_head_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+if (common->has_set_som)
+ {
+ /* Saving the real start pointer is necessary. */
+ common->start_ptr = common->ovector_start;
+ common->ovector_start += sizeof(sljit_sw);
+ }
+
+/* Aligning ovector to even number of sljit words. */
+if ((common->ovector_start & sizeof(sljit_sw)) != 0)
+ common->ovector_start += sizeof(sljit_sw);
+
+if (common->start_ptr == 0)
+ common->start_ptr = OVECTOR(0);
+
+/* Capturing brackets cannot be optimized if callouts are allowed. */
+if (common->capture_last_ptr != 0)
+ memset(common->optimized_cbracket, 0, re->top_bracket + 1);
+
+SLJIT_ASSERT(!(common->req_char_ptr != 0 && common->start_used_ptr != 0));
+common->cbra_ptr = OVECTOR_START + (re->top_bracket + 1) * 2 * sizeof(sljit_sw);
+
+total_length = ccend - common->start;
+common->private_data_ptrs = (sljit_s32 *)SLJIT_MALLOC(total_length * (sizeof(sljit_s32) + (common->has_then ? 1 : 0)), allocator_data);
+if (!common->private_data_ptrs)
+ {
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+memset(common->private_data_ptrs, 0, total_length * sizeof(sljit_s32));
+
+private_data_size = common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw);
+set_private_data_ptrs(common, &private_data_size, ccend);
+if ((re->overall_options & PCRE2_ANCHORED) == 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ if (!detect_fast_forward_skip(common, &private_data_size) && !common->has_skip_in_assert_back)
+ detect_fast_fail(common, common->start, &private_data_size, 4);
+ }
+
+SLJIT_ASSERT(common->fast_fail_start_ptr <= common->fast_fail_end_ptr);
+
+if (private_data_size > SLJIT_MAX_LOCAL_SIZE)
+ {
+ SLJIT_FREE(common->private_data_ptrs, allocator_data);
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+if (common->has_then)
+ {
+ common->then_offsets = (sljit_u8 *)(common->private_data_ptrs + total_length);
+ memset(common->then_offsets, 0, total_length);
+ set_then_offsets(common, common->start, NULL);
+ }
+
+compiler = sljit_create_compiler(allocator_data);
+if (!compiler)
+ {
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ SLJIT_FREE(common->private_data_ptrs, allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+common->compiler = compiler;
+
+/* Main pcre_jit_exec entry. */
+sljit_emit_enter(compiler, 0, SLJIT_ARG1(SW), 5, 5, 0, 0, private_data_size);
+
+/* Register init. */
+reset_ovector(common, (re->top_bracket + 1) * 2);
+if (common->req_char_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, SLJIT_R0, 0);
+
+OP1(SLJIT_MOV, ARGUMENTS, 0, SLJIT_S0, 0);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_S0, 0);
+OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end));
+OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack));
+OP1(SLJIT_MOV_U32, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, limit_match));
+OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, end));
+OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, start));
+OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH, TMP1, 0);
+
+if (common->fast_fail_start_ptr < common->fast_fail_end_ptr)
+ reset_fast_fail(common);
+
+if (mode == PCRE2_JIT_PARTIAL_SOFT)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1);
+if (common->mark_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0);
+if (common->control_head_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0);
+
+/* Main part of the matching */
+if ((re->overall_options & PCRE2_ANCHORED) == 0)
+ {
+ mainloop_label = mainloop_entry(common);
+ continue_match_label = LABEL();
+ /* Forward search if possible. */
+ if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ if (mode == PCRE2_JIT_COMPLETE && fast_forward_first_n_chars(common))
+ ;
+ else if ((re->flags & PCRE2_FIRSTSET) != 0)
+ fast_forward_first_char(common);
+ else if ((re->flags & PCRE2_STARTLINE) != 0)
+ fast_forward_newline(common);
+ else if ((re->flags & PCRE2_FIRSTMAPSET) != 0)
+ fast_forward_start_bits(common);
+ }
+ }
+else
+ continue_match_label = LABEL();
+
+if (mode == PCRE2_JIT_COMPLETE && re->minlength > 0 && (re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
+ OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(re->minlength));
+ minlength_check_failed = CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0);
+ }
+if (common->req_char_ptr != 0)
+ reqbyte_notfound = search_requested_char(common, (PCRE2_UCHAR)(re->last_codeunit), (re->flags & PCRE2_LASTCASELESS) != 0, (re->flags & PCRE2_FIRSTSET) != 0);
+
+/* Store the current STR_PTR in OVECTOR(0). */
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0);
+/* Copy the limit of allowed recursions. */
+OP1(SLJIT_MOV, COUNT_MATCH, 0, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH);
+if (common->capture_last_ptr != 0)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, 0);
+if (common->fast_forward_bc_ptr != NULL)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), PRIVATE_DATA(common->fast_forward_bc_ptr + 1), STR_PTR, 0);
+
+if (common->start_ptr != OVECTOR(0))
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_ptr, STR_PTR, 0);
+
+/* Copy the beginning of the string. */
+if (mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+ JUMPHERE(jump);
+ }
+else if (mode == PCRE2_JIT_PARTIAL_HARD)
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0);
+
+compile_matchingpath(common, common->start, ccend, &rootbacktrack);
+if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ {
+ sljit_free_compiler(compiler);
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ SLJIT_FREE(common->private_data_ptrs, allocator_data);
+ PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+if ((re->overall_options & PCRE2_ENDANCHORED) != 0)
+ end_anchor_failed = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0);
+
+if (common->might_be_empty)
+ {
+ empty_match = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0));
+ empty_match_found_label = LABEL();
+ }
+
+common->accept_label = LABEL();
+if (common->accept != NULL)
+ set_jumps(common->accept, common->accept_label);
+
+/* This means we have a match. Update the ovector. */
+copy_ovector(common, re->top_bracket + 1);
+common->quit_label = common->abort_label = LABEL();
+if (common->quit != NULL)
+ set_jumps(common->quit, common->quit_label);
+if (common->abort != NULL)
+ set_jumps(common->abort, common->abort_label);
+if (minlength_check_failed != NULL)
+ SET_LABEL(minlength_check_failed, common->abort_label);
+sljit_emit_return(compiler, SLJIT_MOV, SLJIT_RETURN_REG, 0);
+
+if (common->failed_match != NULL)
+ {
+ SLJIT_ASSERT(common->mode == PCRE2_JIT_COMPLETE);
+ set_jumps(common->failed_match, LABEL());
+ OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
+ JUMPTO(SLJIT_JUMP, common->abort_label);
+ }
+
+if ((re->overall_options & PCRE2_ENDANCHORED) != 0)
+ JUMPHERE(end_anchor_failed);
+
+if (mode != PCRE2_JIT_COMPLETE)
+ {
+ common->partialmatchlabel = LABEL();
+ set_jumps(common->partialmatch, common->partialmatchlabel);
+ return_with_partial_match(common, common->quit_label);
+ }
+
+if (common->might_be_empty)
+ empty_match_backtrack_label = LABEL();
+compile_backtrackingpath(common, rootbacktrack.top);
+if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ {
+ sljit_free_compiler(compiler);
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ SLJIT_FREE(common->private_data_ptrs, allocator_data);
+ PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+SLJIT_ASSERT(rootbacktrack.prev == NULL);
+reset_match_label = LABEL();
+
+if (mode == PCRE2_JIT_PARTIAL_SOFT)
+ {
+ /* Update hit_start only in the first time. */
+ jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0);
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1);
+ OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, TMP1, 0);
+ JUMPHERE(jump);
+ }
+
+/* Check we have remaining characters. */
+if ((re->overall_options & PCRE2_ANCHORED) == 0 && common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr);
+ }
+
+OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP),
+ (common->fast_forward_bc_ptr != NULL) ? (PRIVATE_DATA(common->fast_forward_bc_ptr + 1)) : common->start_ptr);
+
+if ((re->overall_options & PCRE2_ANCHORED) == 0)
+ {
+ if (common->ff_newline_shortcut != NULL)
+ {
+ /* There cannot be more newlines if PCRE2_FIRSTLINE is set. */
+ if ((re->overall_options & PCRE2_FIRSTLINE) == 0)
+ {
+ if (common->match_end_ptr != 0)
+ {
+ OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
+ OP1(SLJIT_MOV, STR_END, 0, TMP1, 0);
+ CMPTO(SLJIT_LESS, STR_PTR, 0, TMP1, 0, common->ff_newline_shortcut);
+ OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
+ }
+ else
+ CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, common->ff_newline_shortcut);
+ }
+ }
+ else
+ CMPTO(SLJIT_LESS, STR_PTR, 0, (common->match_end_ptr == 0) ? STR_END : TMP1, 0, mainloop_label);
+ }
+
+/* No more remaining characters. */
+if (reqbyte_notfound != NULL)
+ JUMPHERE(reqbyte_notfound);
+
+if (mode == PCRE2_JIT_PARTIAL_SOFT)
+ CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1, common->partialmatchlabel);
+
+OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_NOMATCH);
+JUMPTO(SLJIT_JUMP, common->quit_label);
+
+flush_stubs(common);
+
+if (common->might_be_empty)
+ {
+ JUMPHERE(empty_match);
+ OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
+ OP1(SLJIT_MOV_U32, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, options));
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY);
+ JUMPTO(SLJIT_NOT_ZERO, empty_match_backtrack_label);
+ OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, PCRE2_NOTEMPTY_ATSTART);
+ JUMPTO(SLJIT_ZERO, empty_match_found_label);
+ OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str));
+ CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, empty_match_found_label);
+ JUMPTO(SLJIT_JUMP, empty_match_backtrack_label);
+ }
+
+common->fast_forward_bc_ptr = NULL;
+common->fast_fail_start_ptr = 0;
+common->fast_fail_end_ptr = 0;
+common->currententry = common->entries;
+common->local_quit_available = TRUE;
+quit_label = common->quit_label;
+while (common->currententry != NULL)
+ {
+ /* Might add new entries. */
+ compile_recurse(common);
+ if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler)))
+ {
+ sljit_free_compiler(compiler);
+ SLJIT_FREE(common->optimized_cbracket, allocator_data);
+ SLJIT_FREE(common->private_data_ptrs, allocator_data);
+ PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+ flush_stubs(common);
+ common->currententry = common->currententry->next;
+ }
+common->local_quit_available = FALSE;
+common->quit_label = quit_label;
+
+/* Allocating stack, returns with PCRE_ERROR_JIT_STACKLIMIT if fails. */
+/* This is a (really) rare case. */
+set_jumps(common->stackalloc, LABEL());
+/* RETURN_ADDR is not a saved register. */
+sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+
+SLJIT_ASSERT(TMP1 == SLJIT_R0 && STR_PTR == SLJIT_R1);
+
+OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0);
+OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0);
+OP2(SLJIT_SUB, SLJIT_R1, 0, STACK_LIMIT, 0, SLJIT_IMM, STACK_GROWTH_RATE);
+OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, stack));
+OP1(SLJIT_MOV, STACK_LIMIT, 0, TMP2, 0);
+
+sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_stack_resize));
+
+jump = CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0);
+OP1(SLJIT_MOV, TMP2, 0, STACK_LIMIT, 0);
+OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_RETURN_REG, 0);
+OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0);
+OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1);
+sljit_emit_fast_return(compiler, TMP1, 0);
+
+/* Allocation failed. */
+JUMPHERE(jump);
+/* We break the return address cache here, but this is a really rare case. */
+OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_JIT_STACKLIMIT);
+JUMPTO(SLJIT_JUMP, common->quit_label);
+
+/* Call limit reached. */
+set_jumps(common->calllimit, LABEL());
+OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE2_ERROR_MATCHLIMIT);
+JUMPTO(SLJIT_JUMP, common->quit_label);
+
+if (common->revertframes != NULL)
+ {
+ set_jumps(common->revertframes, LABEL());
+ do_revertframes(common);
+ }
+if (common->wordboundary != NULL)
+ {
+ set_jumps(common->wordboundary, LABEL());
+ check_wordboundary(common);
+ }
+if (common->anynewline != NULL)
+ {
+ set_jumps(common->anynewline, LABEL());
+ check_anynewline(common);
+ }
+if (common->hspace != NULL)
+ {
+ set_jumps(common->hspace, LABEL());
+ check_hspace(common);
+ }
+if (common->vspace != NULL)
+ {
+ set_jumps(common->vspace, LABEL());
+ check_vspace(common);
+ }
+if (common->casefulcmp != NULL)
+ {
+ set_jumps(common->casefulcmp, LABEL());
+ do_casefulcmp(common);
+ }
+if (common->caselesscmp != NULL)
+ {
+ set_jumps(common->caselesscmp, LABEL());
+ do_caselesscmp(common);
+ }
+if (common->reset_match != NULL)
+ {
+ set_jumps(common->reset_match, LABEL());
+ do_reset_match(common, (re->top_bracket + 1) * 2);
+ CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP1, 0, continue_match_label);
+ OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0);
+ JUMPTO(SLJIT_JUMP, reset_match_label);
+ }
+#ifdef SUPPORT_UNICODE
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if (common->utfreadchar != NULL)
+ {
+ set_jumps(common->utfreadchar, LABEL());
+ do_utfreadchar(common);
+ }
+if (common->utfreadchar16 != NULL)
+ {
+ set_jumps(common->utfreadchar16, LABEL());
+ do_utfreadchar16(common);
+ }
+if (common->utfreadtype8 != NULL)
+ {
+ set_jumps(common->utfreadtype8, LABEL());
+ do_utfreadtype8(common);
+ }
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+if (common->getucd != NULL)
+ {
+ set_jumps(common->getucd, LABEL());
+ do_getucd(common);
+ }
+#endif /* SUPPORT_UNICODE */
+
+SLJIT_FREE(common->optimized_cbracket, allocator_data);
+SLJIT_FREE(common->private_data_ptrs, allocator_data);
+
+executable_func = sljit_generate_code(compiler);
+executable_size = sljit_get_generated_code_size(compiler);
+label_addr = common->label_addrs;
+while (label_addr != NULL)
+ {
+ *label_addr->update_addr = sljit_get_label_addr(label_addr->label);
+ label_addr = label_addr->next;
+ }
+sljit_free_compiler(compiler);
+if (executable_func == NULL)
+ {
+ PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+/* Reuse the function descriptor if possible. */
+if (re->executable_jit != NULL)
+ functions = (executable_functions *)re->executable_jit;
+else
+ {
+ functions = SLJIT_MALLOC(sizeof(executable_functions), allocator_data);
+ if (functions == NULL)
+ {
+ /* This case is highly unlikely since we just recently
+ freed a lot of memory. Not impossible though. */
+ sljit_free_code(executable_func);
+ PRIV(jit_free_rodata)(common->read_only_data_head, compiler->allocator_data);
+ return PCRE2_ERROR_NOMEMORY;
+ }
+ memset(functions, 0, sizeof(executable_functions));
+ functions->top_bracket = re->top_bracket + 1;
+ functions->limit_match = re->limit_match;
+ re->executable_jit = functions;
+ }
+
+/* Turn mode into an index. */
+if (mode == PCRE2_JIT_COMPLETE)
+ mode = 0;
+else
+ mode = (mode == PCRE2_JIT_PARTIAL_SOFT) ? 1 : 2;
+
+SLJIT_ASSERT(mode < JIT_NUMBER_OF_COMPILE_MODES);
+functions->executable_funcs[mode] = executable_func;
+functions->read_only_data_heads[mode] = common->read_only_data_head;
+functions->executable_sizes[mode] = executable_size;
+return 0;
+}
+
+#endif
+
+/*************************************************
+* JIT compile a Regular Expression *
+*************************************************/
+
+/* This function used JIT to convert a previously-compiled pattern into machine
+code.
+
+Arguments:
+ code a compiled pattern
+ options JIT option bits
+
+Returns: 0: success or (*NOJIT) was used
+ <0: an error code
+*/
+
+#define PUBLIC_JIT_COMPILE_OPTIONS \
+ (PCRE2_JIT_COMPLETE|PCRE2_JIT_PARTIAL_SOFT|PCRE2_JIT_PARTIAL_HARD)
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_jit_compile(pcre2_code *code, uint32_t options)
+{
+#ifndef SUPPORT_JIT
+
+(void)code;
+(void)options;
+return PCRE2_ERROR_JIT_BADOPTION;
+
+#else /* SUPPORT_JIT */
+
+pcre2_real_code *re = (pcre2_real_code *)code;
+executable_functions *functions;
+int result;
+
+if (code == NULL)
+ return PCRE2_ERROR_NULL;
+
+if ((options & ~PUBLIC_JIT_COMPILE_OPTIONS) != 0)
+ return PCRE2_ERROR_JIT_BADOPTION;
+
+if ((re->flags & PCRE2_NOJIT) != 0) return 0;
+
+functions = (executable_functions *)re->executable_jit;
+
+if ((options & PCRE2_JIT_COMPLETE) != 0 && (functions == NULL
+ || functions->executable_funcs[0] == NULL)) {
+ result = jit_compile(code, PCRE2_JIT_COMPLETE);
+ if (result != 0)
+ return result;
+ }
+
+if ((options & PCRE2_JIT_PARTIAL_SOFT) != 0 && (functions == NULL
+ || functions->executable_funcs[1] == NULL)) {
+ result = jit_compile(code, PCRE2_JIT_PARTIAL_SOFT);
+ if (result != 0)
+ return result;
+ }
+
+if ((options & PCRE2_JIT_PARTIAL_HARD) != 0 && (functions == NULL
+ || functions->executable_funcs[2] == NULL)) {
+ result = jit_compile(code, PCRE2_JIT_PARTIAL_HARD);
+ if (result != 0)
+ return result;
+ }
+
+return 0;
+
+#endif /* SUPPORT_JIT */
+}
+
+/* JIT compiler uses an all-in-one approach. This improves security,
+ since the code generator functions are not exported. */
+
+#define INCLUDED_FROM_PCRE2_JIT_COMPILE
+
+#include "pcre2_jit_match.c"
+#include "pcre2_jit_misc.c"
+
+/* End of pcre2_jit_compile.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_jit_match.c b/test/monniaux/pcre2-10.32/pcre2_jit_match.c
new file mode 100644
index 00000000..5a66545b
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_jit_match.c
@@ -0,0 +1,189 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
+#error This file must be included from pcre2_jit_compile.c.
+#endif
+
+#ifdef SUPPORT_JIT
+
+static SLJIT_NOINLINE int jit_machine_stack_exec(jit_arguments *arguments, jit_function executable_func)
+{
+sljit_u8 local_space[MACHINE_STACK_SIZE];
+struct sljit_stack local_stack;
+
+local_stack.min_start = local_space;
+local_stack.start = local_space;
+local_stack.end = local_space + MACHINE_STACK_SIZE;
+local_stack.top = local_space + MACHINE_STACK_SIZE;
+arguments->stack = &local_stack;
+return executable_func(arguments);
+}
+
+#endif
+
+
+/*************************************************
+* Do a JIT pattern match *
+*************************************************/
+
+/* This function runs a JIT pattern match.
+
+Arguments:
+ code points to the compiled expression
+ subject points to the subject string
+ length length of subject string (may contain binary zeros)
+ start_offset where to start in the subject string
+ options option bits
+ match_data points to a match_data block
+ mcontext points to a match context
+ jit_stack points to a JIT stack
+
+Returns: > 0 => success; value is the number of ovector pairs filled
+ = 0 => success, but ovector is not big enough
+ -1 => failed to match (PCRE_ERROR_NOMATCH)
+ < -1 => some kind of unexpected problem
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
+ PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
+ pcre2_match_context *mcontext)
+{
+#ifndef SUPPORT_JIT
+
+(void)code;
+(void)subject;
+(void)length;
+(void)start_offset;
+(void)options;
+(void)match_data;
+(void)mcontext;
+return PCRE2_ERROR_JIT_BADOPTION;
+
+#else /* SUPPORT_JIT */
+
+pcre2_real_code *re = (pcre2_real_code *)code;
+executable_functions *functions = (executable_functions *)re->executable_jit;
+pcre2_jit_stack *jit_stack;
+uint32_t oveccount = match_data->oveccount;
+uint32_t max_oveccount;
+union {
+ void *executable_func;
+ jit_function call_executable_func;
+} convert_executable_func;
+jit_arguments arguments;
+int rc;
+int index = 0;
+
+if ((options & PCRE2_PARTIAL_HARD) != 0)
+ index = 2;
+else if ((options & PCRE2_PARTIAL_SOFT) != 0)
+ index = 1;
+
+if (functions == NULL || functions->executable_funcs[index] == NULL)
+ return PCRE2_ERROR_JIT_BADOPTION;
+
+/* Sanity checks should be handled by pcre_exec. */
+arguments.str = subject + start_offset;
+arguments.begin = subject;
+arguments.end = subject + length;
+arguments.match_data = match_data;
+arguments.startchar_ptr = subject;
+arguments.mark_ptr = NULL;
+arguments.options = options;
+
+if (mcontext != NULL)
+ {
+ arguments.callout = mcontext->callout;
+ arguments.callout_data = mcontext->callout_data;
+ arguments.offset_limit = mcontext->offset_limit;
+ arguments.limit_match = (mcontext->match_limit < re->limit_match)?
+ mcontext->match_limit : re->limit_match;
+ if (mcontext->jit_callback != NULL)
+ jit_stack = mcontext->jit_callback(mcontext->jit_callback_data);
+ else
+ jit_stack = (pcre2_jit_stack *)mcontext->jit_callback_data;
+ }
+else
+ {
+ arguments.callout = NULL;
+ arguments.callout_data = NULL;
+ arguments.offset_limit = PCRE2_UNSET;
+ arguments.limit_match = (MATCH_LIMIT < re->limit_match)?
+ MATCH_LIMIT : re->limit_match;
+ jit_stack = NULL;
+ }
+
+/* JIT only need two offsets for each ovector entry. Hence
+ the last 1/3 of the ovector will never be touched. */
+
+max_oveccount = functions->top_bracket;
+if (oveccount > max_oveccount)
+ oveccount = max_oveccount;
+arguments.oveccount = oveccount << 1;
+
+
+convert_executable_func.executable_func = functions->executable_funcs[index];
+if (jit_stack != NULL)
+ {
+ arguments.stack = (struct sljit_stack *)(jit_stack->stack);
+ rc = convert_executable_func.call_executable_func(&arguments);
+ }
+else
+ rc = jit_machine_stack_exec(&arguments, convert_executable_func.call_executable_func);
+
+if (rc > (int)oveccount)
+ rc = 0;
+match_data->code = re;
+match_data->subject = subject;
+match_data->rc = rc;
+match_data->startchar = arguments.startchar_ptr - subject;
+match_data->leftchar = 0;
+match_data->rightchar = 0;
+match_data->mark = arguments.mark_ptr;
+match_data->matchedby = PCRE2_MATCHEDBY_JIT;
+
+return match_data->rc;
+
+#endif /* SUPPORT_JIT */
+}
+
+/* End of pcre2_jit_match.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_jit_misc.c b/test/monniaux/pcre2-10.32/pcre2_jit_misc.c
new file mode 100644
index 00000000..efdb0558
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_jit_misc.c
@@ -0,0 +1,227 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
+#error This file must be included from pcre2_jit_compile.c.
+#endif
+
+
+
+/*************************************************
+* Free JIT read-only data *
+*************************************************/
+
+void
+PRIV(jit_free_rodata)(void *current, void *allocator_data)
+{
+#ifndef SUPPORT_JIT
+(void)current;
+(void)allocator_data;
+#else /* SUPPORT_JIT */
+void *next;
+
+SLJIT_UNUSED_ARG(allocator_data);
+
+while (current != NULL)
+ {
+ next = *(void**)current;
+ SLJIT_FREE(current, allocator_data);
+ current = next;
+ }
+
+#endif /* SUPPORT_JIT */
+}
+
+/*************************************************
+* Free JIT compiled code *
+*************************************************/
+
+void
+PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)
+{
+#ifndef SUPPORT_JIT
+(void)executable_jit;
+(void)memctl;
+#else /* SUPPORT_JIT */
+
+executable_functions *functions = (executable_functions *)executable_jit;
+void *allocator_data = memctl;
+int i;
+
+for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)
+ {
+ if (functions->executable_funcs[i] != NULL)
+ sljit_free_code(functions->executable_funcs[i]);
+ PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);
+ }
+
+SLJIT_FREE(functions, allocator_data);
+
+#endif /* SUPPORT_JIT */
+}
+
+
+/*************************************************
+* Free unused JIT memory *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)
+{
+#ifndef SUPPORT_JIT
+(void)gcontext; /* Suppress warning */
+#else /* SUPPORT_JIT */
+SLJIT_UNUSED_ARG(gcontext);
+sljit_free_unused_memory_exec();
+#endif /* SUPPORT_JIT */
+}
+
+
+
+/*************************************************
+* Allocate a JIT stack *
+*************************************************/
+
+PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION
+pcre2_jit_stack_create(size_t startsize, size_t maxsize,
+ pcre2_general_context *gcontext)
+{
+#ifndef SUPPORT_JIT
+
+(void)gcontext;
+(void)startsize;
+(void)maxsize;
+return NULL;
+
+#else /* SUPPORT_JIT */
+
+pcre2_jit_stack *jit_stack;
+
+if (startsize < 1 || maxsize < 1)
+ return NULL;
+if (startsize > maxsize)
+ startsize = maxsize;
+startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
+maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
+
+jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);
+if (jit_stack == NULL) return NULL;
+jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);
+return jit_stack;
+
+#endif
+}
+
+
+/*************************************************
+* Assign a JIT stack to a pattern *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,
+ void *callback_data)
+{
+#ifndef SUPPORT_JIT
+(void)mcontext;
+(void)callback;
+(void)callback_data;
+#else /* SUPPORT_JIT */
+
+if (mcontext == NULL) return;
+mcontext->jit_callback = callback;
+mcontext->jit_callback_data = callback_data;
+
+#endif /* SUPPORT_JIT */
+}
+
+
+/*************************************************
+* Free a JIT stack *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)
+{
+#ifndef SUPPORT_JIT
+(void)jit_stack;
+#else /* SUPPORT_JIT */
+if (jit_stack != NULL)
+ {
+ sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);
+ jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
+ }
+#endif /* SUPPORT_JIT */
+}
+
+
+/*************************************************
+* Get target CPU type *
+*************************************************/
+
+const char*
+PRIV(jit_get_target)(void)
+{
+#ifndef SUPPORT_JIT
+return "JIT is not supported";
+#else /* SUPPORT_JIT */
+return sljit_get_platform_name();
+#endif /* SUPPORT_JIT */
+}
+
+
+/*************************************************
+* Get size of JIT code *
+*************************************************/
+
+size_t
+PRIV(jit_get_size)(void *executable_jit)
+{
+#ifndef SUPPORT_JIT
+(void)executable_jit;
+return 0;
+#else /* SUPPORT_JIT */
+sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;
+SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);
+return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];
+#endif
+}
+
+/* End of pcre2_jit_misc.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_maketables.c b/test/monniaux/pcre2-10.32/pcre2_maketables.c
new file mode 100644
index 00000000..537edba8
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_maketables.c
@@ -0,0 +1,150 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains the external function pcre2_maketables(), which builds
+character tables for PCRE2 in the current locale. The file is compiled on its
+own as part of the PCRE2 library. However, it is also included in the
+compilation of dftables.c, in which case the macro DFTABLES is defined. */
+
+#ifndef DFTABLES
+# ifdef HAVE_CONFIG_H
+# include "config.h"
+# endif
+# include "pcre2_internal.h"
+#endif
+
+
+
+/*************************************************
+* Create PCRE2 character tables *
+*************************************************/
+
+/* This function builds a set of character tables for use by PCRE2 and returns
+a pointer to them. They are build using the ctype functions, and consequently
+their contents will depend upon the current locale setting. When compiled as
+part of the library, the store is obtained via a general context malloc, if
+supplied, but when DFTABLES is defined (when compiling the dftables auxiliary
+program) malloc() is used, and the function has a different name so as not to
+clash with the prototype in pcre2.h.
+
+Arguments: none when DFTABLES is defined
+ else a PCRE2 general context or NULL
+Returns: pointer to the contiguous block of data
+*/
+
+#ifdef DFTABLES /* Included in freestanding dftables.c program */
+static const uint8_t *maketables(void)
+{
+uint8_t *yield = (uint8_t *)malloc(tables_length);
+
+#else /* Not DFTABLES, compiling the library */
+PCRE2_EXP_DEFN const uint8_t * PCRE2_CALL_CONVENTION
+pcre2_maketables(pcre2_general_context *gcontext)
+{
+uint8_t *yield = (uint8_t *)((gcontext != NULL)?
+ gcontext->memctl.malloc(tables_length, gcontext->memctl.memory_data) :
+ malloc(tables_length));
+#endif /* DFTABLES */
+
+int i;
+uint8_t *p;
+
+if (yield == NULL) return NULL;
+p = yield;
+
+/* First comes the lower casing table */
+
+for (i = 0; i < 256; i++) *p++ = tolower(i);
+
+/* Next the case-flipping table */
+
+for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
+
+/* Then the character class tables. Don't try to be clever and save effort on
+exclusive ones - in some locales things may be different.
+
+Note that the table for "space" includes everything "isspace" gives, including
+VT in the default locale. This makes it work for the POSIX class [:space:].
+From release 8.34 is is also correct for Perl space, because Perl added VT at
+release 5.18.
+
+Note also that it is possible for a character to be alnum or alpha without
+being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the
+fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must
+test for alnum specially. */
+
+memset(p, 0, cbit_length);
+for (i = 0; i < 256; i++)
+ {
+ if (isdigit(i)) p[cbit_digit + i/8] |= 1 << (i&7);
+ if (isupper(i)) p[cbit_upper + i/8] |= 1 << (i&7);
+ if (islower(i)) p[cbit_lower + i/8] |= 1 << (i&7);
+ if (isalnum(i)) p[cbit_word + i/8] |= 1 << (i&7);
+ if (i == '_') p[cbit_word + i/8] |= 1 << (i&7);
+ if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7);
+ if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
+ if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7);
+ if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7);
+ if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7);
+ if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7);
+ }
+p += cbit_length;
+
+/* Finally, the character type table. In this, we used to exclude VT from the
+white space chars, because Perl didn't recognize it as such for \s and for
+comments within regexes. However, Perl changed at release 5.18, so PCRE changed
+at release 8.34. */
+
+for (i = 0; i < 256; i++)
+ {
+ int x = 0;
+ if (isspace(i)) x += ctype_space;
+ if (isalpha(i)) x += ctype_letter;
+ if (isdigit(i)) x += ctype_digit;
+ if (isxdigit(i)) x += ctype_xdigit;
+ if (isalnum(i) || i == '_') x += ctype_word;
+ *p++ = x;
+ }
+
+return yield;
+}
+
+/* End of pcre2_maketables.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_match.c b/test/monniaux/pcre2-10.32/pcre2_match.c
new file mode 100644
index 00000000..8741e143
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_match.c
@@ -0,0 +1,6860 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2015-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* These defines enable debugging code */
+
+/* #define DEBUG_FRAMES_DISPLAY */
+/* #define DEBUG_SHOW_OPS */
+/* #define DEBUG_SHOW_RMATCH */
+
+#ifdef DEBUG_FRAME_DISPLAY
+#include <stdarg.h>
+#endif
+
+/* These defines identify the name of the block containing "static"
+information, and fields within it. */
+
+#define NLBLOCK mb /* Block containing newline information */
+#define PSSTART start_subject /* Field containing processed string start */
+#define PSEND end_subject /* Field containing processed string end */
+
+#include "pcre2_internal.h"
+
+#define RECURSE_UNSET 0xffffffffu /* Bigger than max group number */
+
+/* Masks for identifying the public options that are permitted at match time. */
+
+#define PUBLIC_MATCH_OPTIONS \
+ (PCRE2_ANCHORED|PCRE2_ENDANCHORED|PCRE2_NOTBOL|PCRE2_NOTEOL|PCRE2_NOTEMPTY| \
+ PCRE2_NOTEMPTY_ATSTART|PCRE2_NO_UTF_CHECK|PCRE2_PARTIAL_HARD| \
+ PCRE2_PARTIAL_SOFT|PCRE2_NO_JIT)
+
+#define PUBLIC_JIT_MATCH_OPTIONS \
+ (PCRE2_NO_UTF_CHECK|PCRE2_NOTBOL|PCRE2_NOTEOL|PCRE2_NOTEMPTY|\
+ PCRE2_NOTEMPTY_ATSTART|PCRE2_PARTIAL_SOFT|PCRE2_PARTIAL_HARD)
+
+/* Non-error returns from and within the match() function. Error returns are
+externally defined PCRE2_ERROR_xxx codes, which are all negative. */
+
+#define MATCH_MATCH 1
+#define MATCH_NOMATCH 0
+
+/* Special internal returns used in the match() function. Make them
+sufficiently negative to avoid the external error codes. */
+
+#define MATCH_ACCEPT (-999)
+#define MATCH_KETRPOS (-998)
+/* The next 5 must be kept together and in sequence so that a test that checks
+for any one of them can use a range. */
+#define MATCH_COMMIT (-997)
+#define MATCH_PRUNE (-996)
+#define MATCH_SKIP (-995)
+#define MATCH_SKIP_ARG (-994)
+#define MATCH_THEN (-993)
+#define MATCH_BACKTRACK_MAX MATCH_THEN
+#define MATCH_BACKTRACK_MIN MATCH_COMMIT
+
+/* Group frame type values. Zero means the frame is not a group frame. The
+lower 16 bits are used for data (e.g. the capture number). Group frames are
+used for most groups so that information about the start is easily available at
+the end without having to scan back through intermediate frames (backtrack
+points). */
+
+#define GF_CAPTURE 0x00010000u
+#define GF_NOCAPTURE 0x00020000u
+#define GF_CONDASSERT 0x00030000u
+#define GF_RECURSE 0x00040000u
+
+/* Masks for the identity and data parts of the group frame type. */
+
+#define GF_IDMASK(a) ((a) & 0xffff0000u)
+#define GF_DATAMASK(a) ((a) & 0x0000ffffu)
+
+/* Repetition types */
+
+enum { REPTYPE_MIN, REPTYPE_MAX, REPTYPE_POS };
+
+/* Min and max values for the common repeats; a maximum of UINT32_MAX =>
+infinity. */
+
+static const uint32_t rep_min[] = {
+ 0, 0, /* * and *? */
+ 1, 1, /* + and +? */
+ 0, 0, /* ? and ?? */
+ 0, 0, /* dummy placefillers for OP_CR[MIN]RANGE */
+ 0, 1, 0 }; /* OP_CRPOS{STAR, PLUS, QUERY} */
+
+static const uint32_t rep_max[] = {
+ UINT32_MAX, UINT32_MAX, /* * and *? */
+ UINT32_MAX, UINT32_MAX, /* + and +? */
+ 1, 1, /* ? and ?? */
+ 0, 0, /* dummy placefillers for OP_CR[MIN]RANGE */
+ UINT32_MAX, UINT32_MAX, 1 }; /* OP_CRPOS{STAR, PLUS, QUERY} */
+
+/* Repetition types - must include OP_CRPOSRANGE (not needed above) */
+
+static const uint32_t rep_typ[] = {
+ REPTYPE_MAX, REPTYPE_MIN, /* * and *? */
+ REPTYPE_MAX, REPTYPE_MIN, /* + and +? */
+ REPTYPE_MAX, REPTYPE_MIN, /* ? and ?? */
+ REPTYPE_MAX, REPTYPE_MIN, /* OP_CRRANGE and OP_CRMINRANGE */
+ REPTYPE_POS, REPTYPE_POS, /* OP_CRPOSSTAR, OP_CRPOSPLUS */
+ REPTYPE_POS, REPTYPE_POS }; /* OP_CRPOSQUERY, OP_CRPOSRANGE */
+
+/* Numbers for RMATCH calls at backtracking points. When these lists are
+changed, the code at RETURN_SWITCH below must be updated in sync. */
+
+enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10,
+ RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
+ RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
+ RM31, RM32, RM33, RM34, RM35, RM36 };
+
+#ifdef SUPPORT_WIDE_CHARS
+enum { RM100=100, RM101 };
+#endif
+
+#ifdef SUPPORT_UNICODE
+enum { RM200=200, RM201, RM202, RM203, RM204, RM205, RM206, RM207,
+ RM208, RM209, RM210, RM211, RM212, RM213, RM214, RM215,
+ RM216, RM217, RM218, RM219, RM220, RM221, RM222 };
+#endif
+
+/* Define short names for general fields in the current backtrack frame, which
+is always pointed to by the F variable. Occasional references to fields in
+other frames are written out explicitly. There are also some fields in the
+current frame whose names start with "temp" that are used for short-term,
+localised backtracking memory. These are #defined with Lxxx names at the point
+of use and undefined afterwards. */
+
+#define Fback_frame F->back_frame
+#define Fcapture_last F->capture_last
+#define Fcurrent_recurse F->current_recurse
+#define Fecode F->ecode
+#define Feptr F->eptr
+#define Fgroup_frame_type F->group_frame_type
+#define Flast_group_offset F->last_group_offset
+#define Flength F->length
+#define Fmark F->mark
+#define Frdepth F->rdepth
+#define Fstart_match F->start_match
+#define Foffset_top F->offset_top
+#define Foccu F->occu
+#define Fop F->op
+#define Fovector F->ovector
+#define Freturn_id F->return_id
+
+
+#ifdef DEBUG_FRAMES_DISPLAY
+/*************************************************
+* Display current frames and contents *
+*************************************************/
+
+/* This debugging function displays the current set of frames and their
+contents. It is not called automatically from anywhere, the intention being
+that calls can be inserted where necessary when debugging frame-related
+problems.
+
+Arguments:
+ f the file to write to
+ F the current top frame
+ P a previous frame of interest
+ frame_size the frame size
+ mb points to the match block
+ s identification text
+
+Returns: nothing
+*/
+
+static void
+display_frames(FILE *f, heapframe *F, heapframe *P, PCRE2_SIZE frame_size,
+ match_block *mb, const char *s, ...)
+{
+uint32_t i;
+heapframe *Q;
+va_list ap;
+va_start(ap, s);
+
+fprintf(f, "FRAMES ");
+vfprintf(f, s, ap);
+va_end(ap);
+
+if (P != NULL) fprintf(f, " P=%lu",
+ ((char *)P - (char *)(mb->match_frames))/frame_size);
+fprintf(f, "\n");
+
+for (i = 0, Q = mb->match_frames;
+ Q <= F;
+ i++, Q = (heapframe *)((char *)Q + frame_size))
+ {
+ fprintf(f, "Frame %d type=%x subj=%lu code=%d back=%lu id=%d",
+ i, Q->group_frame_type, Q->eptr - mb->start_subject, *(Q->ecode),
+ Q->back_frame, Q->return_id);
+
+ if (Q->last_group_offset == PCRE2_UNSET)
+ fprintf(f, " lgoffset=unset\n");
+ else
+ fprintf(f, " lgoffset=%lu\n", Q->last_group_offset/frame_size);
+ }
+}
+
+#endif
+
+
+
+/*************************************************
+* Process a callout *
+*************************************************/
+
+/* This function is called for all callouts, whether "standalone" or at the
+start of a conditional group. Feptr will be pointing to either OP_CALLOUT or
+OP_CALLOUT_STR. A callout block is allocated in pcre2_match() and initialized
+with fixed values.
+
+Arguments:
+ F points to the current backtracking frame
+ mb points to the match block
+ lengthptr where to return the length of the callout item
+
+Returns: the return from the callout
+ or 0 if no callout function exists
+*/
+
+static int
+do_callout(heapframe *F, match_block *mb, PCRE2_SIZE *lengthptr)
+{
+int rc;
+PCRE2_SIZE save0, save1;
+PCRE2_SIZE *callout_ovector;
+pcre2_callout_block *cb;
+
+*lengthptr = (*Fecode == OP_CALLOUT)?
+ PRIV(OP_lengths)[OP_CALLOUT] : GET(Fecode, 1 + 2*LINK_SIZE);
+
+if (mb->callout == NULL) return 0; /* No callout function provided */
+
+/* The original matching code (pre 10.30) worked directly with the ovector
+passed by the user, and this was passed to callouts. Now that the working
+ovector is in the backtracking frame, it no longer needs to reserve space for
+the overall match offsets (which would waste space in the frame). For backward
+compatibility, however, we pass capture_top and offset_vector to the callout as
+if for the extended ovector, and we ensure that the first two slots are unset
+by preserving and restoring their current contents. Picky compilers complain if
+references such as Fovector[-2] are use directly, so we set up a separate
+pointer. */
+
+callout_ovector = (PCRE2_SIZE *)(Fovector) - 2;
+
+/* The cb->version, cb->subject, cb->subject_length, and cb->start_match fields
+are set externally. The first 3 never change; the last is updated for each
+bumpalong. */
+
+cb = mb->cb;
+cb->capture_top = (uint32_t)Foffset_top/2 + 1;
+cb->capture_last = Fcapture_last;
+cb->offset_vector = callout_ovector;
+cb->mark = mb->nomatch_mark;
+cb->current_position = (PCRE2_SIZE)(Feptr - mb->start_subject);
+cb->pattern_position = GET(Fecode, 1);
+cb->next_item_length = GET(Fecode, 1 + LINK_SIZE);
+
+if (*Fecode == OP_CALLOUT) /* Numerical callout */
+ {
+ cb->callout_number = Fecode[1 + 2*LINK_SIZE];
+ cb->callout_string_offset = 0;
+ cb->callout_string = NULL;
+ cb->callout_string_length = 0;
+ }
+else /* String callout */
+ {
+ cb->callout_number = 0;
+ cb->callout_string_offset = GET(Fecode, 1 + 3*LINK_SIZE);
+ cb->callout_string = Fecode + (1 + 4*LINK_SIZE) + 1;
+ cb->callout_string_length =
+ *lengthptr - (1 + 4*LINK_SIZE) - 2;
+ }
+
+save0 = callout_ovector[0];
+save1 = callout_ovector[1];
+callout_ovector[0] = callout_ovector[1] = PCRE2_UNSET;
+rc = mb->callout(cb, mb->callout_data);
+callout_ovector[0] = save0;
+callout_ovector[1] = save1;
+cb->callout_flags = 0;
+return rc;
+}
+
+
+
+/*************************************************
+* Match a back-reference *
+*************************************************/
+
+/* This function is called only when it is known that the offset lies within
+the offsets that have so far been used in the match. Note that in caseless
+UTF-8 mode, the number of subject bytes matched may be different to the number
+of reference bytes. (In theory this could also happen in UTF-16 mode, but it
+seems unlikely.)
+
+Arguments:
+ offset index into the offset vector
+ caseless TRUE if caseless
+ F the current backtracking frame pointer
+ mb points to match block
+ lengthptr pointer for returning the length matched
+
+Returns: = 0 sucessful match; number of code units matched is set
+ < 0 no match
+ > 0 partial match
+*/
+
+static int
+match_ref(PCRE2_SIZE offset, BOOL caseless, heapframe *F, match_block *mb,
+ PCRE2_SIZE *lengthptr)
+{
+PCRE2_SPTR p;
+PCRE2_SIZE length;
+PCRE2_SPTR eptr;
+PCRE2_SPTR eptr_start;
+
+/* Deal with an unset group. The default is no match, but there is an option to
+match an empty string. */
+
+if (offset >= Foffset_top || Fovector[offset] == PCRE2_UNSET)
+ {
+ if ((mb->poptions & PCRE2_MATCH_UNSET_BACKREF) != 0)
+ {
+ *lengthptr = 0;
+ return 0; /* Match */
+ }
+ else return -1; /* No match */
+ }
+
+/* Separate the caseless and UTF cases for speed. */
+
+eptr = eptr_start = Feptr;
+p = mb->start_subject + Fovector[offset];
+length = Fovector[offset+1] - Fovector[offset];
+
+if (caseless)
+ {
+#if defined SUPPORT_UNICODE
+ if ((mb->poptions & PCRE2_UTF) != 0)
+ {
+ /* Match characters up to the end of the reference. NOTE: the number of
+ code units matched may differ, because in UTF-8 there are some characters
+ whose upper and lower case codes have different numbers of bytes. For
+ example, U+023A (2 bytes in UTF-8) is the upper case version of U+2C65 (3
+ bytes in UTF-8); a sequence of 3 of the former uses 6 bytes, as does a
+ sequence of two of the latter. It is important, therefore, to check the
+ length along the reference, not along the subject (earlier code did this
+ wrong). */
+
+ PCRE2_SPTR endptr = p + length;
+ while (p < endptr)
+ {
+ uint32_t c, d;
+ const ucd_record *ur;
+ if (eptr >= mb->end_subject) return 1; /* Partial match */
+ GETCHARINC(c, eptr);
+ GETCHARINC(d, p);
+ ur = GET_UCD(d);
+ if (c != d && c != (uint32_t)((int)d + ur->other_case))
+ {
+ const uint32_t *pp = PRIV(ucd_caseless_sets) + ur->caseset;
+ for (;;)
+ {
+ if (c < *pp) return -1; /* No match */
+ if (c == *pp++) break;
+ }
+ }
+ }
+ }
+ else
+#endif
+
+ /* Not in UTF mode */
+
+ {
+ for (; length > 0; length--)
+ {
+ uint32_t cc, cp;
+ if (eptr >= mb->end_subject) return 1; /* Partial match */
+ cc = UCHAR21TEST(eptr);
+ cp = UCHAR21TEST(p);
+ if (TABLE_GET(cp, mb->lcc, cp) != TABLE_GET(cc, mb->lcc, cc))
+ return -1; /* No match */
+ p++;
+ eptr++;
+ }
+ }
+ }
+
+/* In the caseful case, we can just compare the code units, whether or not we
+are in UTF mode. When partial matching, we have to do this unit-by-unit. */
+
+else
+ {
+ if (mb->partial != 0)
+ {
+ for (; length > 0; length--)
+ {
+ if (eptr >= mb->end_subject) return 1; /* Partial match */
+ if (UCHAR21INCTEST(p) != UCHAR21INCTEST(eptr)) return -1; /* No match */
+ }
+ }
+
+ /* Not partial matching */
+
+ else
+ {
+ if ((PCRE2_SIZE)(mb->end_subject - eptr) < length) return 1; /* Partial */
+ if (memcmp(p, eptr, CU2BYTES(length)) != 0) return -1; /* No match */
+ eptr += length;
+ }
+ }
+
+*lengthptr = eptr - eptr_start;
+return 0; /* Match */
+}
+
+
+
+/******************************************************************************
+*******************************************************************************
+ "Recursion" in the match() function
+
+The original match() function was highly recursive, but this proved to be the
+source of a number of problems over the years, mostly because of the relatively
+small system stacks that are commonly found. As new features were added to
+patterns, various kludges were invented to reduce the amount of stack used,
+making the code hard to understand in places.
+
+A version did exist that used individual frames on the heap instead of calling
+match() recursively, but this ran substantially slower. The current version is
+a refactoring that uses a vector of frames to remember backtracking points.
+This runs no slower, and possibly even a bit faster than the original recursive
+implementation. An initial vector of size START_FRAMES_SIZE (enough for maybe
+50 frames) is allocated on the system stack. If this is not big enough, the
+heap is used for a larger vector.
+
+*******************************************************************************
+******************************************************************************/
+
+
+
+
+/*************************************************
+* Macros for the match() function *
+*************************************************/
+
+/* These macros pack up tests that are used for partial matching several times
+in the code. We set the "hit end" flag if the pointer is at the end of the
+subject and also past the earliest inspected character (i.e. something has been
+matched, even if not part of the actual matched string). For hard partial
+matching, we then return immediately. The second one is used when we already
+know we are past the end of the subject. */
+
+#define CHECK_PARTIAL()\
+ if (mb->partial != 0 && Feptr >= mb->end_subject && \
+ Feptr > mb->start_used_ptr) \
+ { \
+ mb->hitend = TRUE; \
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL; \
+ }
+
+#define SCHECK_PARTIAL()\
+ if (mb->partial != 0 && Feptr > mb->start_used_ptr) \
+ { \
+ mb->hitend = TRUE; \
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL; \
+ }
+
+/* These macros are used to implement backtracking. They simulate a recursive
+call to the match() function by means of a local vector of frames which
+remember the backtracking points. */
+
+#define RMATCH(ra,rb)\
+ {\
+ start_ecode = ra;\
+ Freturn_id = rb;\
+ goto MATCH_RECURSE;\
+ L_##rb:;\
+ }
+
+#define RRETURN(ra)\
+ {\
+ rrc = ra;\
+ goto RETURN_SWITCH;\
+ }
+
+
+
+/*************************************************
+* Match from current position *
+*************************************************/
+
+/* This function is called to run one match attempt at a single starting point
+in the subject.
+
+Performance note: It might be tempting to extract commonly used fields from the
+mb structure (e.g. end_subject) into individual variables to improve
+performance. Tests using gcc on a SPARC disproved this; in the first case, it
+made performance worse.
+
+Arguments:
+ start_eptr starting character in subject
+ start_ecode starting position in compiled code
+ ovector pointer to the final output vector
+ oveccount number of pairs in ovector
+ top_bracket number of capturing parentheses in the pattern
+ frame_size size of each backtracking frame
+ mb pointer to "static" variables block
+
+Returns: MATCH_MATCH if matched ) these values are >= 0
+ MATCH_NOMATCH if failed to match )
+ negative MATCH_xxx value for PRUNE, SKIP, etc
+ negative PCRE2_ERROR_xxx value if aborted by an error condition
+ (e.g. stopped by repeated call or depth limit)
+*/
+
+static int
+match(PCRE2_SPTR start_eptr, PCRE2_SPTR start_ecode, PCRE2_SIZE *ovector,
+ uint16_t oveccount, uint16_t top_bracket, PCRE2_SIZE frame_size,
+ match_block *mb)
+{
+/* Frame-handling variables */
+
+heapframe *F; /* Current frame pointer */
+heapframe *N = NULL; /* Temporary frame pointers */
+heapframe *P = NULL;
+heapframe *assert_accept_frame; /* For passing back the frame with captures */
+PCRE2_SIZE frame_copy_size; /* Amount to copy when creating a new frame */
+
+/* Local variables that do not need to be preserved over calls to RRMATCH(). */
+
+PCRE2_SPTR bracode; /* Temp pointer to start of group */
+PCRE2_SIZE offset; /* Used for group offsets */
+PCRE2_SIZE length; /* Used for various length calculations */
+
+int rrc; /* Return from functions & backtracking "recursions" */
+#ifdef SUPPORT_UNICODE
+int proptype; /* Type of character property */
+#endif
+
+uint32_t i; /* Used for local loops */
+uint32_t fc; /* Character values */
+uint32_t number; /* Used for group and other numbers */
+uint32_t reptype = 0; /* Type of repetition (0 to avoid compiler warning) */
+uint32_t group_frame_type; /* Specifies type for new group frames */
+
+BOOL condition; /* Used in conditional groups */
+BOOL cur_is_word; /* Used in "word" tests */
+BOOL prev_is_word; /* Used in "word" tests */
+
+/* UTF flag */
+
+#ifdef SUPPORT_UNICODE
+BOOL utf = (mb->poptions & PCRE2_UTF) != 0;
+#else
+BOOL utf = FALSE;
+#endif
+
+/* This is the length of the last part of a backtracking frame that must be
+copied when a new frame is created. */
+
+frame_copy_size = frame_size - offsetof(heapframe, eptr);
+
+/* Set up the first current frame at the start of the vector, and initialize
+fields that are not reset for new frames. */
+
+F = mb->match_frames;
+Frdepth = 0; /* "Recursion" depth */
+Fcapture_last = 0; /* Number of most recent capture */
+Fcurrent_recurse = RECURSE_UNSET; /* Not pattern recursing. */
+Fstart_match = Feptr = start_eptr; /* Current data pointer and start match */
+Fmark = NULL; /* Most recent mark */
+Foffset_top = 0; /* End of captures within the frame */
+Flast_group_offset = PCRE2_UNSET; /* Saved frame of most recent group */
+group_frame_type = 0; /* Not a start of group frame */
+goto NEW_FRAME; /* Start processing with this frame */
+
+/* Come back here when we want to create a new frame for remembering a
+backtracking point. */
+
+MATCH_RECURSE:
+
+/* Set up a new backtracking frame. If the vector is full, get a new one
+on the heap, doubling the size, but constrained by the heap limit. */
+
+N = (heapframe *)((char *)F + frame_size);
+if (N >= mb->match_frames_top)
+ {
+ PCRE2_SIZE newsize = mb->frame_vector_size * 2;
+ heapframe *new;
+
+ if ((newsize / 1024) > mb->heap_limit)
+ {
+ PCRE2_SIZE maxsize = ((mb->heap_limit * 1024)/frame_size) * frame_size;
+ if (mb->frame_vector_size >= maxsize) return PCRE2_ERROR_HEAPLIMIT;
+ newsize = maxsize;
+ }
+
+ new = mb->memctl.malloc(newsize, mb->memctl.memory_data);
+ if (new == NULL) return PCRE2_ERROR_NOMEMORY;
+ memcpy(new, mb->match_frames, mb->frame_vector_size);
+
+ F = (heapframe *)((char *)new + ((char *)F - (char *)mb->match_frames));
+ N = (heapframe *)((char *)F + frame_size);
+
+ if (mb->match_frames != mb->stack_frames)
+ mb->memctl.free(mb->match_frames, mb->memctl.memory_data);
+ mb->match_frames = new;
+ mb->match_frames_top = (heapframe *)((char *)mb->match_frames + newsize);
+ mb->frame_vector_size = newsize;
+ }
+
+#ifdef DEBUG_SHOW_RMATCH
+fprintf(stderr, "++ RMATCH %2d frame=%d", Freturn_id, Frdepth + 1);
+if (group_frame_type != 0)
+ {
+ fprintf(stderr, " type=%x ", group_frame_type);
+ switch (GF_IDMASK(group_frame_type))
+ {
+ case GF_CAPTURE:
+ fprintf(stderr, "capture=%d", GF_DATAMASK(group_frame_type));
+ break;
+
+ case GF_NOCAPTURE:
+ fprintf(stderr, "nocapture op=%d", GF_DATAMASK(group_frame_type));
+ break;
+
+ case GF_CONDASSERT:
+ fprintf(stderr, "condassert op=%d", GF_DATAMASK(group_frame_type));
+ break;
+
+ case GF_RECURSE:
+ fprintf(stderr, "recurse=%d", GF_DATAMASK(group_frame_type));
+ break;
+
+ default:
+ fprintf(stderr, "*** unknown ***");
+ break;
+ }
+ }
+fprintf(stderr, "\n");
+#endif
+
+/* Copy those fields that must be copied into the new frame, increase the
+"recursion" depth (i.e. the new frame's index) and then make the new frame
+current. */
+
+memcpy((char *)N + offsetof(heapframe, eptr),
+ (char *)F + offsetof(heapframe, eptr),
+ frame_copy_size);
+
+N->rdepth = Frdepth + 1;
+F = N;
+
+/* Carry on processing with a new frame. */
+
+NEW_FRAME:
+Fgroup_frame_type = group_frame_type;
+Fecode = start_ecode; /* Starting code pointer */
+Fback_frame = frame_size; /* Default is go back one frame */
+
+/* If this is a special type of group frame, remember its offset for quick
+access at the end of the group. If this is a recursion, set a new current
+recursion value. */
+
+if (group_frame_type != 0)
+ {
+ Flast_group_offset = (char *)F - (char *)mb->match_frames;
+ if (GF_IDMASK(group_frame_type) == GF_RECURSE)
+ Fcurrent_recurse = GF_DATAMASK(group_frame_type);
+ group_frame_type = 0;
+ }
+
+
+/* ========================================================================= */
+/* This is the main processing loop. First check that we haven't recorded too
+many backtracks (search tree is too large), or that we haven't exceeded the
+recursive depth limit (used too many backtracking frames). If not, process the
+opcodes. */
+
+if (mb->match_call_count++ >= mb->match_limit) return PCRE2_ERROR_MATCHLIMIT;
+if (Frdepth >= mb->match_limit_depth) return PCRE2_ERROR_DEPTHLIMIT;
+
+for (;;)
+ {
+#ifdef DEBUG_SHOW_OPS
+fprintf(stderr, "++ op=%d\n", *Fecode);
+#endif
+
+ Fop = (uint8_t)(*Fecode); /* Cast needed for 16-bit and 32-bit modes */
+ switch(Fop)
+ {
+ /* ===================================================================== */
+ /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes, to close
+ any currently open capturing brackets. Unlike reaching the end of a group,
+ where we know the starting frame is at the top of the chained frames, in
+ this case we have to search back for the relevant frame in case other types
+ of group that use chained frames have intervened. Multiple OP_CLOSEs always
+ come innermost first, which matches the chain order. We can ignore this in
+ a recursion, because captures are not passed out of recursions. */
+
+ case OP_CLOSE:
+ if (Fcurrent_recurse == RECURSE_UNSET)
+ {
+ number = GET2(Fecode, 1);
+ offset = Flast_group_offset;
+ for(;;)
+ {
+ if (offset == PCRE2_UNSET) return PCRE2_ERROR_INTERNAL;
+ N = (heapframe *)((char *)mb->match_frames + offset);
+ P = (heapframe *)((char *)N - frame_size);
+ if (N->group_frame_type == (GF_CAPTURE | number)) break;
+ offset = P->last_group_offset;
+ }
+ offset = (number << 1) - 2;
+ Fcapture_last = number;
+ Fovector[offset] = P->eptr - mb->start_subject;
+ Fovector[offset+1] = Feptr - mb->start_subject;
+ if (offset >= Foffset_top) Foffset_top = offset + 2;
+ }
+ Fecode += PRIV(OP_lengths)[*Fecode];
+ break;
+
+
+ /* ===================================================================== */
+ /* Real or forced end of the pattern, assertion, or recursion. In an
+ assertion ACCEPT, update the last used pointer and remember the current
+ frame so that the captures and mark can be fished out of it. */
+
+ case OP_ASSERT_ACCEPT:
+ if (Feptr > mb->last_used_ptr) mb->last_used_ptr = Feptr;
+ assert_accept_frame = F;
+ RRETURN(MATCH_ACCEPT);
+
+ /* If recursing, we have to find the most recent recursion. */
+
+ case OP_ACCEPT:
+ case OP_END:
+
+ /* Handle end of a recursion. */
+
+ if (Fcurrent_recurse != RECURSE_UNSET)
+ {
+ offset = Flast_group_offset;
+ for(;;)
+ {
+ if (offset == PCRE2_UNSET) return PCRE2_ERROR_INTERNAL;
+ N = (heapframe *)((char *)mb->match_frames + offset);
+ P = (heapframe *)((char *)N - frame_size);
+ if (GF_IDMASK(N->group_frame_type) == GF_RECURSE) break;
+ offset = P->last_group_offset;
+ }
+
+ /* N is now the frame of the recursion; the previous frame is at the
+ OP_RECURSE position. Go back there, copying the current subject position
+ and mark, and move on past the OP_RECURSE. */
+
+ P->eptr = Feptr;
+ P->mark = Fmark;
+ F = P;
+ Fecode += 1 + LINK_SIZE;
+ continue;
+ }
+
+ /* Not a recursion. Fail for an empty string match if either PCRE2_NOTEMPTY
+ is set, or if PCRE2_NOTEMPTY_ATSTART is set and we have matched at the
+ start of the subject. In both cases, backtracking will then try other
+ alternatives, if any. */
+
+ if (Feptr == Fstart_match &&
+ ((mb->moptions & PCRE2_NOTEMPTY) != 0 ||
+ ((mb->moptions & PCRE2_NOTEMPTY_ATSTART) != 0 &&
+ Fstart_match == mb->start_subject + mb->start_offset)))
+ RRETURN(MATCH_NOMATCH);
+
+ /* Also fail if PCRE2_ENDANCHORED is set and the end of the match is not
+ the end of the subject. After (*ACCEPT) we fail the entire match (at this
+ position) but backtrack on reaching the end of the pattern. */
+
+ if (Feptr < mb->end_subject &&
+ ((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0)
+ {
+ if (Fop == OP_END) RRETURN(MATCH_NOMATCH);
+ return MATCH_NOMATCH;
+ }
+
+ /* We have a successful match of the whole pattern. Record the result and
+ then do a direct return from the function. If there is space in the offset
+ vector, set any pairs that follow the highest-numbered captured string but
+ are less than the number of capturing groups in the pattern to PCRE2_UNSET.
+ It is documented that this happens. "Gaps" are set to PCRE2_UNSET
+ dynamically. It is only those at the end that need setting here. */
+
+ mb->end_match_ptr = Feptr; /* Record where we ended */
+ mb->end_offset_top = Foffset_top; /* and how many extracts were taken */
+ mb->mark = Fmark; /* and the last success mark */
+ if (Feptr > mb->last_used_ptr) mb->last_used_ptr = Feptr;
+
+ ovector[0] = Fstart_match - mb->start_subject;
+ ovector[1] = Feptr - mb->start_subject;
+
+ /* Set i to the smaller of the sizes of the external and frame ovectors. */
+
+ i = 2 * ((top_bracket + 1 > oveccount)? oveccount : top_bracket + 1);
+ memcpy(ovector + 2, Fovector, (i - 2) * sizeof(PCRE2_SIZE));
+ while (--i >= Foffset_top + 2) ovector[i] = PCRE2_UNSET;
+ return MATCH_MATCH; /* Note: NOT RRETURN */
+
+
+ /*===================================================================== */
+ /* Match any single character type except newline; have to take care with
+ CRLF newlines and partial matching. */
+
+ case OP_ANY:
+ if (IS_NEWLINE(Feptr)) RRETURN(MATCH_NOMATCH);
+ if (mb->partial != 0 &&
+ Feptr == mb->end_subject - 1 &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ UCHAR21TEST(Feptr) == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ /* Fall through */
+
+ /* Match any single character whatsoever. */
+
+ case OP_ALLANY:
+ if (Feptr >= mb->end_subject) /* DO NOT merge the Feptr++ here; it must */
+ { /* not be updated before SCHECK_PARTIAL. */
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr++;
+#ifdef SUPPORT_UNICODE
+ if (utf) ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+#endif
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Match a single code unit, even in UTF mode. This opcode really does
+ match any code unit, even newline. (It really should be called ANYCODEUNIT,
+ of course - the byte name is from pre-16 bit days.) */
+
+ case OP_ANYBYTE:
+ if (Feptr >= mb->end_subject) /* DO NOT merge the Feptr++ here; it must */
+ { /* not be updated before SCHECK_PARTIAL. */
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr++;
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Match a single character, casefully */
+
+ case OP_CHAR:
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ Flength = 1;
+ Fecode++;
+ GETCHARLEN(fc, Fecode, Flength);
+ if (Flength > (PCRE2_SIZE)(mb->end_subject - Feptr))
+ {
+ CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */
+ RRETURN(MATCH_NOMATCH);
+ }
+ for (; Flength > 0; Flength--)
+ {
+ if (*Fecode++ != UCHAR21INC(Feptr)) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ if (mb->end_subject - Feptr < 1)
+ {
+ SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Fecode[1] != *Feptr++) RRETURN(MATCH_NOMATCH);
+ Fecode += 2;
+ }
+ break;
+
+
+ /* ===================================================================== */
+ /* Match a single character, caselessly. If we are at the end of the
+ subject, give up immediately. We get here only when the pattern character
+ has at most one other case. Characters with more than two cases are coded
+ as OP_PROP with the pseudo-property PT_CLIST. */
+
+ case OP_CHARI:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ Flength = 1;
+ Fecode++;
+ GETCHARLEN(fc, Fecode, Flength);
+
+ /* If the pattern character's value is < 128, we know that its other case
+ (if any) is also < 128 (and therefore only one code unit long in all
+ code-unit widths), so we can use the fast lookup table. We checked above
+ that there is at least one character left in the subject. */
+
+ if (fc < 128)
+ {
+ uint32_t cc = UCHAR21(Feptr);
+ if (mb->lcc[fc] != TABLE_GET(cc, mb->lcc, cc)) RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ Feptr++;
+ }
+
+ /* Otherwise we must pick up the subject character and use Unicode
+ property support to test its other case. Note that we cannot use the
+ value of "Flength" to check for sufficient bytes left, because the other
+ case of the character may have more or fewer code units. */
+
+ else
+ {
+ uint32_t dc;
+ GETCHARINC(dc, Feptr);
+ Fecode += Flength;
+ if (dc != fc && dc != UCD_OTHERCASE(fc)) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode; use the table for characters < 256. */
+ {
+ if (TABLE_GET(Fecode[1], mb->lcc, Fecode[1])
+ != TABLE_GET(*Feptr, mb->lcc, *Feptr)) RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ Fecode += 2;
+ }
+ break;
+
+
+ /* ===================================================================== */
+ /* Match not a single character. */
+
+ case OP_NOT:
+ case OP_NOTI:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t ch;
+ Fecode++;
+ GETCHARINC(ch, Fecode);
+ GETCHARINC(fc, Feptr);
+ if (ch == fc)
+ {
+ RRETURN(MATCH_NOMATCH); /* Caseful match */
+ }
+ else if (Fop == OP_NOTI) /* If caseless */
+ {
+ if (ch > 127)
+ ch = UCD_OTHERCASE(ch);
+ else
+ ch = TABLE_GET(ch, mb->fcc, ch);
+ if (ch == fc) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ {
+ uint32_t ch = Fecode[1];
+ fc = *Feptr++;
+ if (ch == fc || (Fop == OP_NOTI && TABLE_GET(ch, mb->fcc, ch) == fc))
+ RRETURN(MATCH_NOMATCH);
+ Fecode += 2;
+ }
+ break;
+
+
+ /* ===================================================================== */
+ /* Match a single character repeatedly. */
+
+#define Loclength F->temp_size
+#define Lstart_eptr F->temp_sptr[0]
+#define Lcharptr F->temp_sptr[1]
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+#define Lc F->temp_32[2]
+#define Loc F->temp_32[3]
+
+ case OP_EXACT:
+ case OP_EXACTI:
+ Lmin = Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATCHAR;
+
+ case OP_POSUPTO:
+ case OP_POSUPTOI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATCHAR;
+
+ case OP_UPTO:
+ case OP_UPTOI:
+ reptype = REPTYPE_MAX;
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATCHAR;
+
+ case OP_MINUPTO:
+ case OP_MINUPTOI:
+ reptype = REPTYPE_MIN;
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATCHAR;
+
+ case OP_POSSTAR:
+ case OP_POSSTARI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATCHAR;
+
+ case OP_POSPLUS:
+ case OP_POSPLUSI:
+ reptype = REPTYPE_POS;
+ Lmin = 1;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATCHAR;
+
+ case OP_POSQUERY:
+ case OP_POSQUERYI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = 1;
+ Fecode++;
+ goto REPEATCHAR;
+
+ case OP_STAR:
+ case OP_STARI:
+ case OP_MINSTAR:
+ case OP_MINSTARI:
+ case OP_PLUS:
+ case OP_PLUSI:
+ case OP_MINPLUS:
+ case OP_MINPLUSI:
+ case OP_QUERY:
+ case OP_QUERYI:
+ case OP_MINQUERY:
+ case OP_MINQUERYI:
+ fc = *Fecode++ - ((Fop < OP_STARI)? OP_STAR : OP_STARI);
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+
+ /* Common code for all repeated single-character matches. We first check
+ for the minimum number of characters. If the minimum equals the maximum, we
+ are done. Otherwise, if minimizing, check the rest of the pattern for a
+ match; if there isn't one, advance up to the maximum, one character at a
+ time.
+
+ If maximizing, advance up to the maximum number of matching characters,
+ until Feptr is past the end of the maximum run. If possessive, we are
+ then done (no backing up). Otherwise, match at this position; anything
+ other than no match is immediately returned. For nomatch, back up one
+ character, unless we are matching \R and the last thing matched was
+ \r\n, in which case, back up two code units until we reach the first
+ optional character position.
+
+ The various UTF/non-UTF and caseful/caseless cases are handled separately,
+ for speed. */
+
+ REPEATCHAR:
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ Flength = 1;
+ Lcharptr = Fecode;
+ GETCHARLEN(fc, Fecode, Flength);
+ Fecode += Flength;
+
+ /* Handle multi-code-unit character matching, caseful and caseless. */
+
+ if (Flength > 1)
+ {
+ uint32_t othercase;
+
+ if (Fop >= OP_STARI && /* Caseless */
+ (othercase = UCD_OTHERCASE(fc)) != fc)
+ Loclength = PRIV(ord2utf)(othercase, Foccu);
+ else Loclength = 0;
+
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr <= mb->end_subject - Flength &&
+ memcmp(Feptr, Lcharptr, CU2BYTES(Flength)) == 0) Feptr += Flength;
+ else if (Loclength > 0 &&
+ Feptr <= mb->end_subject - Loclength &&
+ memcmp(Feptr, Foccu, CU2BYTES(Loclength)) == 0)
+ Feptr += Loclength;
+ else
+ {
+ CHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+
+ if (Lmin == Lmax) continue;
+
+ if (reptype == REPTYPE_MIN)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM202);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr <= mb->end_subject - Flength &&
+ memcmp(Feptr, Lcharptr, CU2BYTES(Flength)) == 0) Feptr += Flength;
+ else if (Loclength > 0 &&
+ Feptr <= mb->end_subject - Loclength &&
+ memcmp(Feptr, Foccu, CU2BYTES(Loclength)) == 0)
+ Feptr += Loclength;
+ else
+ {
+ CHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+ /* Control never gets here */
+ }
+
+ else /* Maximize */
+ {
+ Lstart_eptr = Feptr;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr <= mb->end_subject - Flength &&
+ memcmp(Feptr, Lcharptr, CU2BYTES(Flength)) == 0)
+ Feptr += Flength;
+ else if (Loclength > 0 &&
+ Feptr <= mb->end_subject - Loclength &&
+ memcmp(Feptr, Foccu, CU2BYTES(Loclength)) == 0)
+ Feptr += Loclength;
+ else
+ {
+ CHECK_PARTIAL();
+ break;
+ }
+ }
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ if (reptype != REPTYPE_POS) for(;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM203);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ BACKCHAR(Feptr);
+ }
+ }
+ break; /* End of repeated wide character handling */
+ }
+
+ /* Length of UTF character is 1. Put it into the preserved variable and
+ fall through to the non-UTF code. */
+
+ Lc = fc;
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* When not in UTF mode, load a single-code-unit character. Then proceed as
+ above. */
+
+ Lc = *Fecode++;
+
+ /* Caseless comparison */
+
+ if (Fop >= OP_STARI)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ /* Lc must be < 128 in UTF-8 mode. */
+ Loc = mb->fcc[Lc];
+#else /* 16-bit & 32-bit */
+#ifdef SUPPORT_UNICODE
+ if (utf && Lc > 127) Loc = UCD_OTHERCASE(Lc);
+ else
+#endif /* SUPPORT_UNICODE */
+ Loc = TABLE_GET(Lc, mb->fcc, Lc);
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc; /* Faster than PCRE2_UCHAR */
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21TEST(Feptr);
+ if (Lc != cc && Loc != cc) RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ if (Lmin == Lmax) continue;
+
+ if (reptype == REPTYPE_MIN)
+ {
+ for (;;)
+ {
+ uint32_t cc; /* Faster than PCRE2_UCHAR */
+ RMATCH(Fecode, RM25);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21TEST(Feptr);
+ if (Lc != cc && Loc != cc) RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ /* Control never gets here */
+ }
+
+ else /* Maximize */
+ {
+ Lstart_eptr = Feptr;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ uint32_t cc; /* Faster than PCRE2_UCHAR */
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ cc = UCHAR21TEST(Feptr);
+ if (Lc != cc && Loc != cc) break;
+ Feptr++;
+ }
+ if (reptype != REPTYPE_POS) for (;;)
+ {
+ if (Feptr == Lstart_eptr) break;
+ RMATCH(Fecode, RM26);
+ Feptr--;
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ }
+ }
+ }
+
+ /* Caseful comparisons (includes all multi-byte characters) */
+
+ else
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc != UCHAR21INCTEST(Feptr)) RRETURN(MATCH_NOMATCH);
+ }
+
+ if (Lmin == Lmax) continue;
+
+ if (reptype == REPTYPE_MIN)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM27);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc != UCHAR21INCTEST(Feptr)) RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+ }
+ else /* Maximize */
+ {
+ Lstart_eptr = Feptr;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+
+ if (Lc != UCHAR21TEST(Feptr)) break;
+ Feptr++;
+ }
+
+ if (reptype != REPTYPE_POS) for (;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM28);
+ Feptr--;
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ }
+ }
+ }
+ break;
+
+#undef Loclength
+#undef Lstart_eptr
+#undef Lcharptr
+#undef Lmin
+#undef Lmax
+#undef Lc
+#undef Loc
+
+
+ /* ===================================================================== */
+ /* Match a negated single one-byte character repeatedly. This is almost a
+ repeat of the code for a repeated single character, but I haven't found a
+ nice way of commoning these up that doesn't require a test of the
+ positive/negative option for each character match. Maybe that wouldn't add
+ very much to the time taken, but character matching *is* what this is all
+ about... */
+
+#define Lstart_eptr F->temp_sptr[0]
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+#define Lc F->temp_32[2]
+#define Loc F->temp_32[3]
+
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ Lmin = Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTUPTO:
+ case OP_NOTUPTOI:
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ reptype = REPTYPE_MAX;
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTMINUPTO:
+ case OP_NOTMINUPTOI:
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ reptype = REPTYPE_MIN;
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSSTARI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSPLUSI:
+ reptype = REPTYPE_POS;
+ Lmin = 1;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSQUERYI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = 1;
+ Fecode++;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTPOSUPTO:
+ case OP_NOTPOSUPTOI:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATNOTCHAR;
+
+ case OP_NOTSTAR:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTAR:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUS:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUS:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERY:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERY:
+ case OP_NOTMINQUERYI:
+ fc = *Fecode++ - ((Fop >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+
+ /* Common code for all repeated single-character non-matches. */
+
+ REPEATNOTCHAR:
+ GETCHARINCTEST(Lc, Fecode);
+
+ /* The code is duplicated for the caseless and caseful cases, for speed,
+ since matching characters is likely to be quite common. First, ensure the
+ minimum number of matches are present. If Lmin = Lmax, we are done.
+ Otherwise, if minimizing, keep trying the rest of the expression and
+ advancing one matching character if failing, up to the maximum.
+ Alternatively, if maximizing, find the maximum number of characters and
+ work backwards. */
+
+ if (Fop >= OP_NOTSTARI) /* Caseless */
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf && Lc > 127)
+ Loc = UCD_OTHERCASE(Lc);
+ else
+#endif /* SUPPORT_UNICODE */
+
+ Loc = TABLE_GET(Lc, mb->fcc, Lc); /* Other case from table */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(d, Feptr);
+ if (Lc == d || Loc == d) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc == *Feptr || Loc == *Feptr) RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ }
+
+ if (Lmin == Lmax) continue; /* Finished for exact count */
+
+ if (reptype == REPTYPE_MIN)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (;;)
+ {
+ RMATCH(Fecode, RM204);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(d, Feptr);
+ if (Lc == d || Loc == d) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif /*SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM29);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc == *Feptr || Loc == *Feptr) RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ }
+ /* Control never gets here */
+ }
+
+ /* Maximize case */
+
+ else
+ {
+ Lstart_eptr = Feptr;
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(d, Feptr, len);
+ if (Lc == d || Loc == d) break;
+ Feptr += len;
+ }
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ if (reptype != REPTYPE_POS) for(;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM205);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ BACKCHAR(Feptr);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (Lc == *Feptr || Loc == *Feptr) break;
+ Feptr++;
+ }
+ if (reptype != REPTYPE_POS) for (;;)
+ {
+ if (Feptr == Lstart_eptr) break;
+ RMATCH(Fecode, RM30);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ }
+ }
+ }
+ }
+
+ /* Caseful comparisons */
+
+ else
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(d, Feptr);
+ if (Lc == d) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc == *Feptr++) RRETURN(MATCH_NOMATCH);
+ }
+ }
+
+ if (Lmin == Lmax) continue;
+
+ if (reptype == REPTYPE_MIN)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (;;)
+ {
+ RMATCH(Fecode, RM206);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(d, Feptr);
+ if (Lc == d) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM31);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lc == *Feptr++) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ /* Control never gets here */
+ }
+
+ /* Maximize case */
+
+ else
+ {
+ Lstart_eptr = Feptr;
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t d;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(d, Feptr, len);
+ if (Lc == d) break;
+ Feptr += len;
+ }
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ if (reptype != REPTYPE_POS) for(;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM207);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ BACKCHAR(Feptr);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (Lc == *Feptr) break;
+ Feptr++;
+ }
+ if (reptype != REPTYPE_POS) for (;;)
+ {
+ if (Feptr == Lstart_eptr) break;
+ RMATCH(Fecode, RM32);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ }
+ }
+ }
+ }
+ break;
+
+#undef Lstart_eptr
+#undef Lmin
+#undef Lmax
+#undef Lc
+#undef Loc
+
+
+ /* ===================================================================== */
+ /* Match a bit-mapped character class, possibly repeatedly. These opcodes
+ are used when all the characters in the class have values in the range
+ 0-255, and either the matching is caseful, or the characters are in the
+ range 0-127 when UTF processing is enabled. The only difference between
+ OP_CLASS and OP_NCLASS occurs when a data character outside the range is
+ encountered. */
+
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+#define Lstart_eptr F->temp_sptr[0]
+#define Lbyte_map_address F->temp_sptr[1]
+#define Lbyte_map ((unsigned char *)Lbyte_map_address)
+
+ case OP_NCLASS:
+ case OP_CLASS:
+ {
+ Lbyte_map_address = Fecode + 1; /* Save for matching */
+ Fecode += 1 + (32 / sizeof(PCRE2_UCHAR)); /* Advance past the item */
+
+ /* Look past the end of the item to see if there is repeat information
+ following. Then obey similar code to character type repeats. */
+
+ switch (*Fecode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSPLUS:
+ case OP_CRPOSQUERY:
+ fc = *Fecode++ - OP_CRSTAR;
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ Lmin = GET2(Fecode, 1);
+ Lmax = GET2(Fecode, 1 + IMM2_SIZE);
+ if (Lmax == 0) Lmax = UINT32_MAX; /* Max 0 => infinity */
+ reptype = rep_typ[*Fecode - OP_CRSTAR];
+ Fecode += 1 + 2 * IMM2_SIZE;
+ break;
+
+ default: /* No repeat follows */
+ Lmin = Lmax = 1;
+ break;
+ }
+
+ /* First, ensure the minimum number of matches are present. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) RRETURN(MATCH_NOMATCH);
+ }
+ else
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ fc = *Feptr++;
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) RRETURN(MATCH_NOMATCH);
+ }
+ else
+#endif
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) RRETURN(MATCH_NOMATCH);
+ }
+ }
+
+ /* If Lmax == Lmin we are done. Continue with main loop. */
+
+ if (Lmin == Lmax) continue;
+
+ /* If minimizing, keep testing the rest of the expression and advancing
+ the pointer while it matches the class. */
+
+ if (reptype == REPTYPE_MIN)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM200);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) RRETURN(MATCH_NOMATCH);
+ }
+ else
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM23);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ fc = *Feptr++;
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) RRETURN(MATCH_NOMATCH);
+ }
+ else
+#endif
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) RRETURN(MATCH_NOMATCH);
+ }
+ }
+ /* Control never gets here */
+ }
+
+ /* If maximizing, find the longest possible run, then work backwards. */
+
+ else
+ {
+ Lstart_eptr = Feptr;
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) break;
+ }
+ else
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) break;
+ Feptr += len;
+ }
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ for (;;)
+ {
+ RMATCH(Fecode, RM201);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Feptr-- <= Lstart_eptr) break; /* Tried at original position */
+ BACKCHAR(Feptr);
+ }
+ }
+ else
+#endif
+ /* Not UTF mode */
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ fc = *Feptr;
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (fc > 255)
+ {
+ if (Fop == OP_CLASS) break;
+ }
+ else
+#endif
+ if ((Lbyte_map[fc/8] & (1 << (fc&7))) == 0) break;
+ Feptr++;
+ }
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ while (Feptr >= Lstart_eptr)
+ {
+ RMATCH(Fecode, RM24);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ }
+ }
+
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+ /* Control never gets here */
+
+#undef Lbyte_map_address
+#undef Lbyte_map
+#undef Lstart_eptr
+#undef Lmin
+#undef Lmax
+
+
+ /* ===================================================================== */
+ /* Match an extended character class. In the 8-bit library, this opcode is
+ encountered only when UTF-8 mode mode is supported. In the 16-bit and
+ 32-bit libraries, codepoints greater than 255 may be encountered even when
+ UTF is not supported. */
+
+#define Lstart_eptr F->temp_sptr[0]
+#define Lxclass_data F->temp_sptr[1]
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ {
+ Lxclass_data = Fecode + 1 + LINK_SIZE; /* Save for matching */
+ Fecode += GET(Fecode, 1); /* Advance past the item */
+
+ switch (*Fecode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSPLUS:
+ case OP_CRPOSQUERY:
+ fc = *Fecode++ - OP_CRSTAR;
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ Lmin = GET2(Fecode, 1);
+ Lmax = GET2(Fecode, 1 + IMM2_SIZE);
+ if (Lmax == 0) Lmax = UINT32_MAX; /* Max 0 => infinity */
+ reptype = rep_typ[*Fecode - OP_CRSTAR];
+ Fecode += 1 + 2 * IMM2_SIZE;
+ break;
+
+ default: /* No repeat follows */
+ Lmin = Lmax = 1;
+ break;
+ }
+
+ /* First, ensure the minimum number of matches are present. */
+
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (!PRIV(xclass)(fc, Lxclass_data, utf)) RRETURN(MATCH_NOMATCH);
+ }
+
+ /* If Lmax == Lmin we can just continue with the main loop. */
+
+ if (Lmin == Lmax) continue;
+
+ /* If minimizing, keep testing the rest of the expression and advancing
+ the pointer while it matches the class. */
+
+ if (reptype == REPTYPE_MIN)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM100);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (!PRIV(xclass)(fc, Lxclass_data, utf)) RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+ }
+
+ /* If maximizing, find the longest possible run, then work backwards. */
+
+ else
+ {
+ Lstart_eptr = Feptr;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+#ifdef SUPPORT_UNICODE
+ GETCHARLENTEST(fc, Feptr, len);
+#else
+ fc = *Feptr;
+#endif
+ if (!PRIV(xclass)(fc, Lxclass_data, utf)) break;
+ Feptr += len;
+ }
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ for(;;)
+ {
+ RMATCH(Fecode, RM101);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Feptr-- <= Lstart_eptr) break; /* Tried at original position */
+#ifdef SUPPORT_UNICODE
+ if (utf) BACKCHAR(Feptr);
+#endif
+ }
+ RRETURN(MATCH_NOMATCH);
+ }
+
+ /* Control never gets here */
+ }
+#endif /* SUPPORT_WIDE_CHARS: end of XCLASS */
+
+#undef Lstart_eptr
+#undef Lxclass_data
+#undef Lmin
+#undef Lmax
+
+
+ /* ===================================================================== */
+ /* Match various character types when PCRE2_UCP is not set. These opcodes
+ are not generated when PCRE2_UCP is set - instead appropriate property
+ tests are compiled. */
+
+ case OP_NOT_DIGIT:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (CHMAX_255(fc) && (mb->ctypes[fc] & ctype_digit) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_DIGIT:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (!CHMAX_255(fc) || (mb->ctypes[fc] & ctype_digit) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_NOT_WHITESPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (CHMAX_255(fc) && (mb->ctypes[fc] & ctype_space) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_WHITESPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (!CHMAX_255(fc) || (mb->ctypes[fc] & ctype_space) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_NOT_WORDCHAR:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (CHMAX_255(fc) && (mb->ctypes[fc] & ctype_word) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_WORDCHAR:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (!CHMAX_255(fc) || (mb->ctypes[fc] & ctype_word) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_ANYNL:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+
+ case CHAR_CR:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ }
+ else if (UCHAR21TEST(Feptr) == CHAR_LF) Feptr++;
+ break;
+
+ case CHAR_LF:
+ break;
+
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ Fecode++;
+ break;
+
+ case OP_NOT_HSPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES: RRETURN(MATCH_NOMATCH); /* Byte and multibyte cases */
+ default: break;
+ }
+ Fecode++;
+ break;
+
+ case OP_HSPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES: break; /* Byte and multibyte cases */
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ Fecode++;
+ break;
+
+ case OP_NOT_VSPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ VSPACE_CASES: RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ Fecode++;
+ break;
+
+ case OP_VSPACE:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ VSPACE_CASES: break;
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ Fecode++;
+ break;
+
+
+#ifdef SUPPORT_UNICODE
+
+ /* ===================================================================== */
+ /* Check the next character by Unicode property. We will get here only
+ if the support is in the binary; otherwise a compile-time error occurs. */
+
+ case OP_PROP:
+ case OP_NOTPROP:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ {
+ const uint32_t *cp;
+ const ucd_record *prop = GET_UCD(fc);
+
+ switch(Fecode[1])
+ {
+ case PT_ANY:
+ if (Fop == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+
+ case PT_LAMP:
+ if ((prop->chartype == ucp_Lu ||
+ prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt) == (Fop == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case PT_GC:
+ if ((Fecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (Fop == OP_PROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case PT_PC:
+ if ((Fecode[2] != prop->chartype) == (Fop == OP_PROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case PT_SC:
+ if ((Fecode[2] != prop->script) == (Fop == OP_PROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ /* These are specials */
+
+ case PT_ALNUM:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (Fop == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(fc)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ if (Fop == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+
+ default:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) ==
+ (Fop == OP_NOTPROP)) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
+ fc == CHAR_UNDERSCORE) == (Fop == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case PT_CLIST:
+ cp = PRIV(ucd_caseless_sets) + Fecode[2];
+ for (;;)
+ {
+ if (fc < *cp)
+ { if (Fop == OP_PROP) { RRETURN(MATCH_NOMATCH); } else break; }
+ if (fc == *cp++)
+ { if (Fop == OP_PROP) break; else { RRETURN(MATCH_NOMATCH); } }
+ }
+ break;
+
+ case PT_UCNC:
+ if ((fc == CHAR_DOLLAR_SIGN || fc == CHAR_COMMERCIAL_AT ||
+ fc == CHAR_GRAVE_ACCENT || (fc >= 0xa0 && fc <= 0xd7ff) ||
+ fc >= 0xe000) == (Fop == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ /* This should never occur */
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ Fecode += 3;
+ }
+ break;
+
+
+ /* ===================================================================== */
+ /* Match an extended Unicode sequence. We will get here only if the support
+ is in the binary; otherwise a compile-time error occurs. */
+
+ case OP_EXTUNI:
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ else
+ {
+ GETCHARINCTEST(fc, Feptr);
+ Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject, utf,
+ NULL);
+ }
+ CHECK_PARTIAL();
+ Fecode++;
+ break;
+
+#endif /* SUPPORT_UNICODE */
+
+
+ /* ===================================================================== */
+ /* Match a single character type repeatedly. Note that the property type
+ does not need to be in a stack frame as it is not used within an RMATCH()
+ loop. */
+
+#define Lstart_eptr F->temp_sptr[0]
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+#define Lctype F->temp_32[2]
+#define Lpropvalue F->temp_32[3]
+
+ case OP_TYPEEXACT:
+ Lmin = Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATTYPE;
+
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ reptype = (*Fecode == OP_TYPEMINUPTO)? REPTYPE_MIN : REPTYPE_MAX;
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATTYPE;
+
+ case OP_TYPEPOSSTAR:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATTYPE;
+
+ case OP_TYPEPOSPLUS:
+ reptype = REPTYPE_POS;
+ Lmin = 1;
+ Lmax = UINT32_MAX;
+ Fecode++;
+ goto REPEATTYPE;
+
+ case OP_TYPEPOSQUERY:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = 1;
+ Fecode++;
+ goto REPEATTYPE;
+
+ case OP_TYPEPOSUPTO:
+ reptype = REPTYPE_POS;
+ Lmin = 0;
+ Lmax = GET2(Fecode, 1);
+ Fecode += 1 + IMM2_SIZE;
+ goto REPEATTYPE;
+
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ fc = *Fecode++ - OP_TYPESTAR;
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+
+ /* Common code for all repeated character type matches. */
+
+ REPEATTYPE:
+ Lctype = *Fecode++; /* Code for the character type */
+
+#ifdef SUPPORT_UNICODE
+ if (Lctype == OP_PROP || Lctype == OP_NOTPROP)
+ {
+ proptype = *Fecode++;
+ Lpropvalue = *Fecode++;
+ }
+ else proptype = -1;
+#endif
+
+ /* First, ensure the minimum number of matches are present. Use inline
+ code for maximizing the speed, and do the type test once at the start
+ (i.e. keep it out of the loop). The code for UTF mode is separated out for
+ tidiness, except for Unicode property tests. */
+
+ if (Lmin > 0)
+ {
+#ifdef SUPPORT_UNICODE
+ if (proptype >= 0) /* Property tests in all modes */
+ {
+ switch(proptype)
+ {
+ case PT_ANY:
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ }
+ break;
+
+ case PT_LAMP:
+ for (i = 1; i <= Lmin; i++)
+ {
+ int chartype;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ chartype = UCD_CHARTYPE(fc);
+ if ((chartype == ucp_Lu ||
+ chartype == ucp_Ll ||
+ chartype == ucp_Lt) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case PT_GC:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_CATEGORY(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case PT_PC:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_CHARTYPE(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case PT_SC:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_SCRIPT(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case PT_ALNUM:
+ for (i = 1; i <= Lmin; i++)
+ {
+ int category;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L || category == ucp_N) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+
+ default:
+ if ((UCD_CATEGORY(fc) == ucp_Z) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ break;
+
+ case PT_WORD:
+ for (i = 1; i <= Lmin; i++)
+ {
+ int category;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L || category == ucp_N ||
+ fc == CHAR_UNDERSCORE) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case PT_CLIST:
+ for (i = 1; i <= Lmin; i++)
+ {
+ const uint32_t *cp;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ cp = PRIV(ucd_caseless_sets) + Lpropvalue;
+ for (;;)
+ {
+ if (fc < *cp)
+ {
+ if (Lctype == OP_NOTPROP) break;
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (fc == *cp++)
+ {
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ }
+ break;
+
+ case PT_UCNC:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((fc == CHAR_DOLLAR_SIGN || fc == CHAR_COMMERCIAL_AT ||
+ fc == CHAR_GRAVE_ACCENT || (fc >= 0xa0 && fc <= 0xd7ff) ||
+ fc >= 0xe000) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ /* This should not occur */
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+ }
+
+ /* Match extended Unicode sequences. We will get here only if the
+ support is in the binary; otherwise a compile-time error occurs. */
+
+ else if (Lctype == OP_EXTUNI)
+ {
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ else
+ {
+ GETCHARINCTEST(fc, Feptr);
+ Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject,
+ mb->end_subject, utf, NULL);
+ }
+ CHECK_PARTIAL();
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+/* Handle all other cases in UTF mode */
+
+#ifdef SUPPORT_UNICODE
+ if (utf) switch(Lctype)
+ {
+ case OP_ANY:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (IS_NEWLINE(Feptr)) RRETURN(MATCH_NOMATCH);
+ if (mb->partial != 0 &&
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ UCHAR21(Feptr) == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ break;
+
+ case OP_ALLANY:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ break;
+
+ case OP_ANYBYTE:
+ if (Feptr > mb->end_subject - Lmin) RRETURN(MATCH_NOMATCH);
+ Feptr += Lmin;
+ break;
+
+ case OP_ANYNL:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+
+ case CHAR_CR:
+ if (Feptr < mb->end_subject && UCHAR21(Feptr) == CHAR_LF) Feptr++;
+ break;
+
+ case CHAR_LF:
+ break;
+
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES: RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ }
+ break;
+
+ case OP_HSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES: break;
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ switch(fc)
+ {
+ VSPACE_CASES: RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ }
+ break;
+
+ case OP_VSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ switch(fc)
+ {
+ VSPACE_CASES: break;
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ }
+ break;
+
+ case OP_NOT_DIGIT:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINC(fc, Feptr);
+ if (fc < 128 && (mb->ctypes[fc] & ctype_digit) != 0)
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case OP_DIGIT:
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21(Feptr);
+ if (cc >= 128 || (mb->ctypes[cc] & ctype_digit) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ /* No need to skip more code units - we know it has only one. */
+ }
+ break;
+
+ case OP_NOT_WHITESPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21(Feptr);
+ if (cc < 128 && (mb->ctypes[cc] & ctype_space) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ break;
+
+ case OP_WHITESPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21(Feptr);
+ if (cc >= 128 || (mb->ctypes[cc] & ctype_space) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ /* No need to skip more code units - we know it has only one. */
+ }
+ break;
+
+ case OP_NOT_WORDCHAR:
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21(Feptr);
+ if (cc < 128 && (mb->ctypes[cc] & ctype_word) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ break;
+
+ case OP_WORDCHAR:
+ for (i = 1; i <= Lmin; i++)
+ {
+ uint32_t cc;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ cc = UCHAR21(Feptr);
+ if (cc >= 128 || (mb->ctypes[cc] & ctype_word) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ /* No need to skip more code units - we know it has only one. */
+ }
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ } /* End switch(Lctype) */
+
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Code for the non-UTF case for minimum matching of operators other
+ than OP_PROP and OP_NOTPROP. */
+
+ switch(Lctype)
+ {
+ case OP_ANY:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (IS_NEWLINE(Feptr)) RRETURN(MATCH_NOMATCH);
+ if (mb->partial != 0 &&
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ *Feptr == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ Feptr++;
+ }
+ break;
+
+ case OP_ALLANY:
+ if (Feptr > mb->end_subject - Lmin)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr += Lmin;
+ break;
+
+ /* This OP_ANYBYTE case will never be reached because \C gets turned
+ into OP_ALLANY in non-UTF mode. Cut out the code so that coverage
+ reports don't complain about it's never being used. */
+
+/* case OP_ANYBYTE:
+* if (Feptr > mb->end_subject - Lmin)
+* {
+* SCHECK_PARTIAL();
+* RRETURN(MATCH_NOMATCH);
+* }
+* Feptr += Lmin;
+* break;
+*/
+ case OP_ANYNL:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ switch(*Feptr++)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+
+ case CHAR_CR:
+ if (Feptr < mb->end_subject && *Feptr == CHAR_LF) Feptr++;
+ break;
+
+ case CHAR_LF:
+ break;
+
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ case 0x2028:
+ case 0x2029:
+#endif
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ switch(*Feptr++)
+ {
+ default: break;
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+ break;
+
+ case OP_HSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ switch(*Feptr++)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ break;
+ }
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ switch(*Feptr++)
+ {
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ }
+ break;
+
+ case OP_VSPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ switch(*Feptr++)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ break;
+ }
+ }
+ break;
+
+ case OP_NOT_DIGIT:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_digit) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ case OP_DIGIT:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_digit) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ case OP_NOT_WHITESPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_space) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ case OP_WHITESPACE:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_space) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ case OP_NOT_WORDCHAR:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_word) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ case OP_WORDCHAR:
+ for (i = 1; i <= Lmin; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_word) == 0)
+ RRETURN(MATCH_NOMATCH);
+ Feptr++;
+ }
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+ }
+
+ /* If Lmin = Lmax we are done. Continue with the main loop. */
+
+ if (Lmin == Lmax) continue;
+
+ /* If minimizing, we have to test the rest of the pattern before each
+ subsequent match. */
+
+ if (reptype == REPTYPE_MIN)
+ {
+#ifdef SUPPORT_UNICODE
+ if (proptype >= 0)
+ {
+ switch(proptype)
+ {
+ case PT_ANY:
+ for (;;)
+ {
+ RMATCH(Fecode, RM208);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_LAMP:
+ for (;;)
+ {
+ int chartype;
+ RMATCH(Fecode, RM209);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ chartype = UCD_CHARTYPE(fc);
+ if ((chartype == ucp_Lu ||
+ chartype == ucp_Ll ||
+ chartype == ucp_Lt) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_GC:
+ for (;;)
+ {
+ RMATCH(Fecode, RM210);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_CATEGORY(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_PC:
+ for (;;)
+ {
+ RMATCH(Fecode, RM211);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_CHARTYPE(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_SC:
+ for (;;)
+ {
+ RMATCH(Fecode, RM212);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((UCD_SCRIPT(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_ALNUM:
+ for (;;)
+ {
+ int category;
+ RMATCH(Fecode, RM213);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L || category == ucp_N) ==
+ (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ for (;;)
+ {
+ RMATCH(Fecode, RM214);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ switch(fc)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+
+ default:
+ if ((UCD_CATEGORY(fc) == ucp_Z) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ /* Control never gets here */
+
+ case PT_WORD:
+ for (;;)
+ {
+ int category;
+ RMATCH(Fecode, RM215);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L ||
+ category == ucp_N ||
+ fc == CHAR_UNDERSCORE) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ case PT_CLIST:
+ for (;;)
+ {
+ const uint32_t *cp;
+ RMATCH(Fecode, RM216);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ cp = PRIV(ucd_caseless_sets) + Lpropvalue;
+ for (;;)
+ {
+ if (fc < *cp)
+ {
+ if (Lctype == OP_NOTPROP) break;
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (fc == *cp++)
+ {
+ if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ }
+ }
+ /* Control never gets here */
+
+ case PT_UCNC:
+ for (;;)
+ {
+ RMATCH(Fecode, RM217);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ GETCHARINCTEST(fc, Feptr);
+ if ((fc == CHAR_DOLLAR_SIGN || fc == CHAR_COMMERCIAL_AT ||
+ fc == CHAR_GRAVE_ACCENT || (fc >= 0xa0 && fc <= 0xd7ff) ||
+ fc >= 0xe000) == (Lctype == OP_NOTPROP))
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+ /* This should never occur */
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+ }
+
+ /* Match extended Unicode sequences. We will get here only if the
+ support is in the binary; otherwise a compile-time error occurs. */
+
+ else if (Lctype == OP_EXTUNI)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM218);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ else
+ {
+ GETCHARINCTEST(fc, Feptr);
+ Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject,
+ utf, NULL);
+ }
+ CHECK_PARTIAL();
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* UTF mode for non-property testing character types. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM219);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lctype == OP_ANY && IS_NEWLINE(Feptr)) RRETURN(MATCH_NOMATCH);
+ GETCHARINC(fc, Feptr);
+ switch(Lctype)
+ {
+ case OP_ANY: /* This is the non-NL case */
+ if (mb->partial != 0 && /* Take care with CRLF partial */
+ Feptr >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ fc == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ break;
+
+ case OP_ALLANY:
+ case OP_ANYBYTE:
+ break;
+
+ case OP_ANYNL:
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+
+ case CHAR_CR:
+ if (Feptr < mb->end_subject && UCHAR21(Feptr) == CHAR_LF) Feptr++;
+ break;
+
+ case CHAR_LF:
+ break;
+
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#ifndef EBCDIC
+ case 0x2028:
+ case 0x2029:
+#endif /* Not EBCDIC */
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF)
+ RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ switch(fc)
+ {
+ HSPACE_CASES: RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ break;
+
+ case OP_HSPACE:
+ switch(fc)
+ {
+ HSPACE_CASES: break;
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ switch(fc)
+ {
+ VSPACE_CASES: RRETURN(MATCH_NOMATCH);
+ default: break;
+ }
+ break;
+
+ case OP_VSPACE:
+ switch(fc)
+ {
+ VSPACE_CASES: break;
+ default: RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case OP_NOT_DIGIT:
+ if (fc < 256 && (mb->ctypes[fc] & ctype_digit) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_DIGIT:
+ if (fc >= 256 || (mb->ctypes[fc] & ctype_digit) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_NOT_WHITESPACE:
+ if (fc < 256 && (mb->ctypes[fc] & ctype_space) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_WHITESPACE:
+ if (fc >= 256 || (mb->ctypes[fc] & ctype_space) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_NOT_WORDCHAR:
+ if (fc < 256 && (mb->ctypes[fc] & ctype_word) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_WORDCHAR:
+ if (fc >= 256 || (mb->ctypes[fc] & ctype_word) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+ {
+ for (;;)
+ {
+ RMATCH(Fecode, RM33);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ if (Lctype == OP_ANY && IS_NEWLINE(Feptr))
+ RRETURN(MATCH_NOMATCH);
+ fc = *Feptr++;
+ switch(Lctype)
+ {
+ case OP_ANY: /* This is the non-NL case */
+ if (mb->partial != 0 && /* Take care with CRLF partial */
+ Feptr >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ fc == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ break;
+
+ case OP_ALLANY:
+ case OP_ANYBYTE:
+ break;
+
+ case OP_ANYNL:
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+
+ case CHAR_CR:
+ if (Feptr < mb->end_subject && *Feptr == CHAR_LF) Feptr++;
+ break;
+
+ case CHAR_LF:
+ break;
+
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_NEL:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ case 0x2028:
+ case 0x2029:
+#endif
+ if (mb->bsr_convention == PCRE2_BSR_ANYCRLF)
+ RRETURN(MATCH_NOMATCH);
+ break;
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ switch(fc)
+ {
+ default: break;
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case OP_HSPACE:
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ break;
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ switch(fc)
+ {
+ default: break;
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ RRETURN(MATCH_NOMATCH);
+ }
+ break;
+
+ case OP_VSPACE:
+ switch(fc)
+ {
+ default: RRETURN(MATCH_NOMATCH);
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ break;
+ }
+ break;
+
+ case OP_NOT_DIGIT:
+ if (MAX_255(fc) && (mb->ctypes[fc] & ctype_digit) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_DIGIT:
+ if (!MAX_255(fc) || (mb->ctypes[fc] & ctype_digit) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_NOT_WHITESPACE:
+ if (MAX_255(fc) && (mb->ctypes[fc] & ctype_space) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_WHITESPACE:
+ if (!MAX_255(fc) || (mb->ctypes[fc] & ctype_space) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_NOT_WORDCHAR:
+ if (MAX_255(fc) && (mb->ctypes[fc] & ctype_word) != 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ case OP_WORDCHAR:
+ if (!MAX_255(fc) || (mb->ctypes[fc] & ctype_word) == 0)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+ }
+ }
+ /* Control never gets here */
+ }
+
+ /* If maximizing, it is worth using inline code for speed, doing the type
+ test once at the start (i.e. keep it out of the loop). */
+
+ else
+ {
+ Lstart_eptr = Feptr; /* Remember where we started */
+
+#ifdef SUPPORT_UNICODE
+ if (proptype >= 0)
+ {
+ switch(proptype)
+ {
+ case PT_ANY:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ if (Lctype == OP_NOTPROP) break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_LAMP:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int chartype;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ chartype = UCD_CHARTYPE(fc);
+ if ((chartype == ucp_Lu ||
+ chartype == ucp_Ll ||
+ chartype == ucp_Lt) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_GC:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ if ((UCD_CATEGORY(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_PC:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ if ((UCD_CHARTYPE(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_SC:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ if ((UCD_SCRIPT(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_ALNUM:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int category;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L || category == ucp_N) ==
+ (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ switch(fc)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ if (Lctype == OP_NOTPROP) goto ENDLOOP99; /* Break the loop */
+ break;
+
+ default:
+ if ((UCD_CATEGORY(fc) == ucp_Z) == (Lctype == OP_NOTPROP))
+ goto ENDLOOP99; /* Break the loop */
+ break;
+ }
+ Feptr+= len;
+ }
+ ENDLOOP99:
+ break;
+
+ case PT_WORD:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int category;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ category = UCD_CATEGORY(fc);
+ if ((category == ucp_L || category == ucp_N ||
+ fc == CHAR_UNDERSCORE) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr+= len;
+ }
+ break;
+
+ case PT_CLIST:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ const uint32_t *cp;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ cp = PRIV(ucd_caseless_sets) + Lpropvalue;
+ for (;;)
+ {
+ if (fc < *cp)
+ { if (Lctype == OP_NOTPROP) break; else goto GOT_MAX; }
+ if (fc == *cp++)
+ { if (Lctype == OP_NOTPROP) goto GOT_MAX; else break; }
+ }
+ Feptr += len;
+ }
+ GOT_MAX:
+ break;
+
+ case PT_UCNC:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLENTEST(fc, Feptr, len);
+ if ((fc == CHAR_DOLLAR_SIGN || fc == CHAR_COMMERCIAL_AT ||
+ fc == CHAR_GRAVE_ACCENT || (fc >= 0xa0 && fc <= 0xd7ff) ||
+ fc >= 0xe000) == (Lctype == OP_NOTPROP))
+ break;
+ Feptr += len;
+ }
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ /* Feptr is now past the end of the maximum run */
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't
+ go too far. */
+
+ for(;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM222);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ if (utf) BACKCHAR(Feptr);
+ }
+ }
+
+ /* Match extended Unicode grapheme clusters. We will get here only if the
+ support is in the binary; otherwise a compile-time error occurs. */
+
+ else if (Lctype == OP_EXTUNI)
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ else
+ {
+ GETCHARINCTEST(fc, Feptr);
+ Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject,
+ utf, NULL);
+ }
+ CHECK_PARTIAL();
+ }
+
+ /* Feptr is now past the end of the maximum run */
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ /* We use <= Lstart_eptr rather than == Lstart_eptr to detect the start
+ of the run while backtracking because the use of \C in UTF mode can
+ cause BACKCHAR to move back past Lstart_eptr. This is just palliative;
+ the use of \C in UTF mode is fraught with danger. */
+
+ for(;;)
+ {
+ int lgb, rgb;
+ PCRE2_SPTR fptr;
+
+ if (Feptr <= Lstart_eptr) break; /* At start of char run */
+ RMATCH(Fecode, RM220);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+
+ /* Backtracking over an extended grapheme cluster involves inspecting
+ the previous two characters (if present) to see if a break is
+ permitted between them. */
+
+ Feptr--;
+ if (!utf) fc = *Feptr; else
+ {
+ BACKCHAR(Feptr);
+ GETCHAR(fc, Feptr);
+ }
+ rgb = UCD_GRAPHBREAK(fc);
+
+ for (;;)
+ {
+ if (Feptr <= Lstart_eptr) break; /* At start of char run */
+ fptr = Feptr - 1;
+ if (!utf) fc = *fptr; else
+ {
+ BACKCHAR(fptr);
+ GETCHAR(fc, fptr);
+ }
+ lgb = UCD_GRAPHBREAK(fc);
+ if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
+ Feptr = fptr;
+ rgb = lgb;
+ }
+ }
+ }
+
+ else
+#endif /* SUPPORT_UNICODE */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ switch(Lctype)
+ {
+ case OP_ANY:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (IS_NEWLINE(Feptr)) break;
+ if (mb->partial != 0 && /* Take care with CRLF partial */
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ UCHAR21(Feptr) == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ break;
+
+ case OP_ALLANY:
+ if (Lmax < UINT32_MAX)
+ {
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ Feptr++;
+ ACROSSCHAR(Feptr < mb->end_subject, Feptr, Feptr++);
+ }
+ }
+ else
+ {
+ Feptr = mb->end_subject; /* Unlimited UTF-8 repeat */
+ SCHECK_PARTIAL();
+ }
+ break;
+
+ /* The "byte" (i.e. "code unit") case is the same as non-UTF */
+
+ case OP_ANYBYTE:
+ fc = Lmax - Lmin;
+ if (fc > (uint32_t)(mb->end_subject - Feptr))
+ {
+ Feptr = mb->end_subject;
+ SCHECK_PARTIAL();
+ }
+ else Feptr += fc;
+ break;
+
+ case OP_ANYNL:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc == CHAR_CR)
+ {
+ if (++Feptr >= mb->end_subject) break;
+ if (UCHAR21(Feptr) == CHAR_LF) Feptr++;
+ }
+ else
+ {
+ if (fc != CHAR_LF &&
+ (mb->bsr_convention == PCRE2_BSR_ANYCRLF ||
+ (fc != CHAR_VT && fc != CHAR_FF && fc != CHAR_NEL
+#ifndef EBCDIC
+ && fc != 0x2028 && fc != 0x2029
+#endif /* Not EBCDIC */
+ )))
+ break;
+ Feptr += len;
+ }
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ case OP_HSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ BOOL gotspace;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ switch(fc)
+ {
+ HSPACE_CASES: gotspace = TRUE; break;
+ default: gotspace = FALSE; break;
+ }
+ if (gotspace == (Lctype == OP_NOT_HSPACE)) break;
+ Feptr += len;
+ }
+ break;
+
+ case OP_NOT_VSPACE:
+ case OP_VSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ BOOL gotspace;
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ switch(fc)
+ {
+ VSPACE_CASES: gotspace = TRUE; break;
+ default: gotspace = FALSE; break;
+ }
+ if (gotspace == (Lctype == OP_NOT_VSPACE)) break;
+ Feptr += len;
+ }
+ break;
+
+ case OP_NOT_DIGIT:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc < 256 && (mb->ctypes[fc] & ctype_digit) != 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ case OP_DIGIT:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc >= 256 ||(mb->ctypes[fc] & ctype_digit) == 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ case OP_NOT_WHITESPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc < 256 && (mb->ctypes[fc] & ctype_space) != 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ case OP_WHITESPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc >= 256 ||(mb->ctypes[fc] & ctype_space) == 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ case OP_NOT_WORDCHAR:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc < 256 && (mb->ctypes[fc] & ctype_word) != 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ case OP_WORDCHAR:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ int len = 1;
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ GETCHARLEN(fc, Feptr, len);
+ if (fc >= 256 || (mb->ctypes[fc] & ctype_word) == 0) break;
+ Feptr+= len;
+ }
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ /* After \C in UTF mode, Lstart_eptr might be in the middle of a
+ Unicode character. Use <= Lstart_eptr to ensure backtracking doesn't go
+ too far. */
+
+ for(;;)
+ {
+ if (Feptr <= Lstart_eptr) break;
+ RMATCH(Fecode, RM221);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ BACKCHAR(Feptr);
+ if (Lctype == OP_ANYNL && Feptr > Lstart_eptr &&
+ UCHAR21(Feptr) == CHAR_NL && UCHAR21(Feptr - 1) == CHAR_CR)
+ Feptr--;
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF mode */
+ {
+ switch(Lctype)
+ {
+ case OP_ANY:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (IS_NEWLINE(Feptr)) break;
+ if (mb->partial != 0 && /* Take care with CRLF partial */
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ *Feptr == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ Feptr++;
+ }
+ break;
+
+ case OP_ALLANY:
+ case OP_ANYBYTE:
+ fc = Lmax - Lmin;
+ if (fc > (uint32_t)(mb->end_subject - Feptr))
+ {
+ Feptr = mb->end_subject;
+ SCHECK_PARTIAL();
+ }
+ else Feptr += fc;
+ break;
+
+ case OP_ANYNL:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ fc = *Feptr;
+ if (fc == CHAR_CR)
+ {
+ if (++Feptr >= mb->end_subject) break;
+ if (*Feptr == CHAR_LF) Feptr++;
+ }
+ else
+ {
+ if (fc != CHAR_LF && (mb->bsr_convention == PCRE2_BSR_ANYCRLF ||
+ (fc != CHAR_VT && fc != CHAR_FF && fc != CHAR_NEL
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ && fc != 0x2028 && fc != 0x2029
+#endif
+ ))) break;
+ Feptr++;
+ }
+ }
+ break;
+
+ case OP_NOT_HSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ switch(*Feptr)
+ {
+ default: Feptr++; break;
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ goto ENDLOOP00;
+ }
+ }
+ ENDLOOP00:
+ break;
+
+ case OP_HSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ switch(*Feptr)
+ {
+ default: goto ENDLOOP01;
+ HSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ HSPACE_MULTIBYTE_CASES:
+#endif
+ Feptr++; break;
+ }
+ }
+ ENDLOOP01:
+ break;
+
+ case OP_NOT_VSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ switch(*Feptr)
+ {
+ default: Feptr++; break;
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ goto ENDLOOP02;
+ }
+ }
+ ENDLOOP02:
+ break;
+
+ case OP_VSPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ switch(*Feptr)
+ {
+ default: goto ENDLOOP03;
+ VSPACE_BYTE_CASES:
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ VSPACE_MULTIBYTE_CASES:
+#endif
+ Feptr++; break;
+ }
+ }
+ ENDLOOP03:
+ break;
+
+ case OP_NOT_DIGIT:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_digit) != 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ case OP_DIGIT:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_digit) == 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ case OP_NOT_WHITESPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_space) != 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ case OP_WHITESPACE:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_space) == 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ case OP_NOT_WORDCHAR:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (MAX_255(*Feptr) && (mb->ctypes[*Feptr] & ctype_word) != 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ case OP_WORDCHAR:
+ for (i = Lmin; i < Lmax; i++)
+ {
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ break;
+ }
+ if (!MAX_255(*Feptr) || (mb->ctypes[*Feptr] & ctype_word) == 0)
+ break;
+ Feptr++;
+ }
+ break;
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ if (reptype == REPTYPE_POS) continue; /* No backtracking */
+
+ for (;;)
+ {
+ if (Feptr == Lstart_eptr) break;
+ RMATCH(Fecode, RM34);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr--;
+ if (Lctype == OP_ANYNL && Feptr > Lstart_eptr && *Feptr == CHAR_LF &&
+ Feptr[-1] == CHAR_CR) Feptr--;
+ }
+ }
+ }
+ break; /* End of repeat character type processing */
+
+#undef Lstart_eptr
+#undef Lmin
+#undef Lmax
+#undef Lctype
+#undef Lpropvalue
+
+
+ /* ===================================================================== */
+ /* Match a back reference, possibly repeatedly. Look past the end of the
+ item to see if there is repeat information following. The OP_REF and
+ OP_REFI opcodes are used for a reference to a numbered group or to a
+ non-duplicated named group. For a duplicated named group, OP_DNREF and
+ OP_DNREFI are used. In this case we must scan the list of groups to which
+ the name refers, and use the first one that is set. */
+
+#define Lmin F->temp_32[0]
+#define Lmax F->temp_32[1]
+#define Lcaseless F->temp_32[2]
+#define Lstart F->temp_sptr[0]
+#define Loffset F->temp_size
+
+ case OP_DNREF:
+ case OP_DNREFI:
+ Lcaseless = (Fop == OP_DNREFI);
+ {
+ int count = GET2(Fecode, 1+IMM2_SIZE);
+ PCRE2_SPTR slot = mb->name_table + GET2(Fecode, 1) * mb->name_entry_size;
+ Fecode += 1 + 2*IMM2_SIZE;
+
+ while (count-- > 0)
+ {
+ Loffset = (GET2(slot, 0) << 1) - 2;
+ if (Loffset < Foffset_top && Fovector[Loffset] != PCRE2_UNSET) break;
+ slot += mb->name_entry_size;
+ }
+ }
+ goto REF_REPEAT;
+
+ case OP_REF:
+ case OP_REFI:
+ Lcaseless = (Fop == OP_REFI);
+ Loffset = (GET2(Fecode, 1) << 1) - 2;
+ Fecode += 1 + IMM2_SIZE;
+
+ /* Set up for repetition, or handle the non-repeated case. The maximum and
+ minimum must be in the heap frame, but as they are short-term values, we
+ use temporary fields. */
+
+ REF_REPEAT:
+ switch (*Fecode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ fc = *Fecode++ - OP_CRSTAR;
+ Lmin = rep_min[fc];
+ Lmax = rep_max[fc];
+ reptype = rep_typ[fc];
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ Lmin = GET2(Fecode, 1);
+ Lmax = GET2(Fecode, 1 + IMM2_SIZE);
+ reptype = rep_typ[*Fecode - OP_CRSTAR];
+ if (Lmax == 0) Lmax = UINT32_MAX; /* Max 0 => infinity */
+ Fecode += 1 + 2 * IMM2_SIZE;
+ break;
+
+ default: /* No repeat follows */
+ {
+ rrc = match_ref(Loffset, Lcaseless, F, mb, &length);
+ if (rrc != 0)
+ {
+ if (rrc > 0) Feptr = mb->end_subject; /* Partial match */
+ CHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+ Feptr += length;
+ continue; /* With the main loop */
+ }
+
+ /* Handle repeated back references. If a set group has length zero, just
+ continue with the main loop, because it matches however many times. For an
+ unset reference, if the minimum is zero, we can also just continue. We can
+ also continue if PCRE2_MATCH_UNSET_BACKREF is set, because this makes unset
+ group behave as a zero-length group. For any other unset cases, carrying
+ on will result in NOMATCH. */
+
+ if (Loffset < Foffset_top && Fovector[Loffset] != PCRE2_UNSET)
+ {
+ if (Fovector[Loffset] == Fovector[Loffset + 1]) continue;
+ }
+ else /* Group is not set */
+ {
+ if (Lmin == 0 || (mb->poptions & PCRE2_MATCH_UNSET_BACKREF) != 0)
+ continue;
+ }
+
+ /* First, ensure the minimum number of matches are present. */
+
+ for (i = 1; i <= Lmin; i++)
+ {
+ PCRE2_SIZE slength;
+ rrc = match_ref(Loffset, Lcaseless, F, mb, &slength);
+ if (rrc != 0)
+ {
+ if (rrc > 0) Feptr = mb->end_subject; /* Partial match */
+ CHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr += slength;
+ }
+
+ /* If min = max, we are done. They are not both allowed to be zero. */
+
+ if (Lmin == Lmax) continue;
+
+ /* If minimizing, keep trying and advancing the pointer. */
+
+ if (reptype == REPTYPE_MIN)
+ {
+ for (;;)
+ {
+ PCRE2_SIZE slength;
+ RMATCH(Fecode, RM20);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Lmin++ >= Lmax) RRETURN(MATCH_NOMATCH);
+ rrc = match_ref(Loffset, Lcaseless, F, mb, &slength);
+ if (rrc != 0)
+ {
+ if (rrc > 0) Feptr = mb->end_subject; /* Partial match */
+ CHECK_PARTIAL();
+ RRETURN(MATCH_NOMATCH);
+ }
+ Feptr += slength;
+ }
+ /* Control never gets here */
+ }
+
+ /* If maximizing, find the longest string and work backwards, as long as
+ the matched lengths for each iteration are the same. */
+
+ else
+ {
+ BOOL samelengths = TRUE;
+ Lstart = Feptr; /* Starting position */
+ Flength = Fovector[Loffset+1] - Fovector[Loffset];
+
+ for (i = Lmin; i < Lmax; i++)
+ {
+ PCRE2_SIZE slength;
+ rrc = match_ref(Loffset, Lcaseless, F, mb, &slength);
+ if (rrc != 0)
+ {
+ /* Can't use CHECK_PARTIAL because we don't want to update Feptr in
+ the soft partial matching case. */
+
+ if (rrc > 0 && mb->partial != 0 &&
+ mb->end_subject > mb->start_used_ptr)
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ break;
+ }
+
+ if (slength != Flength) samelengths = FALSE;
+ Feptr += slength;
+ }
+
+ /* If the length matched for each repetition is the same as the length of
+ the captured group, we can easily work backwards. This is the normal
+ case. However, in caseless UTF-8 mode there are pairs of case-equivalent
+ characters whose lengths (in terms of code units) differ. However, this
+ is very rare, so we handle it by re-matching fewer and fewer times. */
+
+ if (samelengths)
+ {
+ while (Feptr >= Lstart)
+ {
+ RMATCH(Fecode, RM21);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Feptr -= Flength;
+ }
+ }
+
+ /* The rare case of non-matching lengths. Re-scan the repetition for each
+ iteration. We know that match_ref() will succeed every time. */
+
+ else
+ {
+ Lmax = i;
+ for (;;)
+ {
+ RMATCH(Fecode, RM22);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ if (Feptr == Lstart) break; /* Failed after minimal repetition */
+ Feptr = Lstart;
+ Lmax--;
+ for (i = Lmin; i < Lmax; i++)
+ {
+ PCRE2_SIZE slength;
+ (void)match_ref(Loffset, Lcaseless, F, mb, &slength);
+ Feptr += slength;
+ }
+ }
+ }
+
+ RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never gets here */
+
+#undef Lcaseless
+#undef Lmin
+#undef Lmax
+#undef Lstart
+#undef Loffset
+
+
+
+/* ========================================================================= */
+/* Opcodes for the start of various parenthesized items */
+/* ========================================================================= */
+
+ /* In all cases, if the result of RMATCH() is MATCH_THEN, check whether the
+ (*THEN) is within the current branch by comparing the address of OP_THEN
+ that is passed back with the end of the branch. If (*THEN) is within the
+ current branch, and the branch is one of two or more alternatives (it
+ either starts or ends with OP_ALT), we have reached the limit of THEN's
+ action, so convert the return code to NOMATCH, which will cause normal
+ backtracking to happen from now on. Otherwise, THEN is passed back to an
+ outer alternative. This implements Perl's treatment of parenthesized
+ groups, where a group not containing | does not affect the current
+ alternative, that is, (X) is NOT the same as (X|(*F)). */
+
+
+ /* ===================================================================== */
+ /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a non-possessive
+ bracket group, indicating that it may occur zero times. It may repeat
+ infinitely, or not at all - i.e. it could be ()* or ()? or even (){0} in
+ the pattern. Brackets with fixed upper repeat limits are compiled as a
+ number of copies, with the optional ones preceded by BRAZERO or BRAMINZERO.
+ Possessive groups with possible zero repeats are preceded by BRAPOSZERO. */
+
+#define Lnext_ecode F->temp_sptr[0]
+
+ case OP_BRAZERO:
+ Lnext_ecode = Fecode + 1;
+ RMATCH(Lnext_ecode, RM9);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ do Lnext_ecode += GET(Lnext_ecode, 1); while (*Lnext_ecode == OP_ALT);
+ Fecode = Lnext_ecode + 1 + LINK_SIZE;
+ break;
+
+ case OP_BRAMINZERO:
+ Lnext_ecode = Fecode + 1;
+ do Lnext_ecode += GET(Lnext_ecode, 1); while (*Lnext_ecode == OP_ALT);
+ RMATCH(Lnext_ecode + 1 + LINK_SIZE, RM10);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Fecode++;
+ break;
+
+#undef Lnext_ecode
+
+ case OP_SKIPZERO:
+ Fecode++;
+ do Fecode += GET(Fecode,1); while (*Fecode == OP_ALT);
+ Fecode += 1 + LINK_SIZE;
+ break;
+
+
+ /* ===================================================================== */
+ /* Handle possessive brackets with an unlimited repeat. The end of these
+ brackets will always be OP_KETRPOS, which returns MATCH_KETRPOS without
+ going further in the pattern. */
+
+#define Lframe_type F->temp_32[0]
+#define Lmatched_once F->temp_32[1]
+#define Lzero_allowed F->temp_32[2]
+#define Lstart_eptr F->temp_sptr[0]
+#define Lstart_group F->temp_sptr[1]
+
+ case OP_BRAPOSZERO:
+ Lzero_allowed = TRUE; /* Zero repeat is allowed */
+ Fecode += 1;
+ if (*Fecode == OP_CBRAPOS || *Fecode == OP_SCBRAPOS)
+ goto POSSESSIVE_CAPTURE;
+ goto POSSESSIVE_NON_CAPTURE;
+
+ case OP_BRAPOS:
+ case OP_SBRAPOS:
+ Lzero_allowed = FALSE; /* Zero repeat not allowed */
+
+ POSSESSIVE_NON_CAPTURE:
+ Lframe_type = GF_NOCAPTURE; /* Remembered frame type */
+ goto POSSESSIVE_GROUP;
+
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ Lzero_allowed = FALSE; /* Zero repeat not allowed */
+
+ POSSESSIVE_CAPTURE:
+ number = GET2(Fecode, 1+LINK_SIZE);
+ Lframe_type = GF_CAPTURE | number; /* Remembered frame type */
+
+ POSSESSIVE_GROUP:
+ Lmatched_once = FALSE; /* Never matched */
+ Lstart_group = Fecode; /* Start of this group */
+
+ for (;;)
+ {
+ Lstart_eptr = Feptr; /* Position at group start */
+ group_frame_type = Lframe_type;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM8);
+ if (rrc == MATCH_KETRPOS)
+ {
+ Lmatched_once = TRUE; /* Matched at least once */
+ if (Feptr == Lstart_eptr) /* Empty match; skip to end */
+ {
+ do Fecode += GET(Fecode, 1); while (*Fecode == OP_ALT);
+ break;
+ }
+
+ Fecode = Lstart_group;
+ continue;
+ }
+
+ /* See comment above about handling THEN. */
+
+ if (rrc == MATCH_THEN)
+ {
+ PCRE2_SPTR next_ecode = Fecode + GET(Fecode,1);
+ if (mb->verb_ecode_ptr < next_ecode &&
+ (*Fecode == OP_ALT || *next_ecode == OP_ALT))
+ rrc = MATCH_NOMATCH;
+ }
+
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Fecode += GET(Fecode, 1);
+ if (*Fecode != OP_ALT) break;
+ }
+
+ /* Success if matched something or zero repeat allowed */
+
+ if (Lmatched_once || Lzero_allowed)
+ {
+ Fecode += 1 + LINK_SIZE;
+ break;
+ }
+
+ RRETURN(MATCH_NOMATCH);
+
+#undef Lmatched_once
+#undef Lzero_allowed
+#undef Lframe_type
+#undef Lstart_eptr
+#undef Lstart_group
+
+
+ /* ===================================================================== */
+ /* Handle non-capturing brackets that cannot match an empty string. When we
+ get to the final alternative within the brackets, as long as there are no
+ THEN's in the pattern, we can optimize by not recording a new backtracking
+ point. (Ideally we should test for a THEN within this group, but we don't
+ have that information.) Don't do this if we are at the very top level,
+ however, because that would make handling assertions and once-only brackets
+ messier when there is nothing to go back to. */
+
+#define Lframe_type F->temp_32[0] /* Set for all that use GROUPLOOP */
+#define Lnext_branch F->temp_sptr[0] /* Used only in OP_BRA handling */
+
+ case OP_BRA:
+ if (mb->hasthen || Frdepth == 0)
+ {
+ Lframe_type = 0;
+ goto GROUPLOOP;
+ }
+
+ for (;;)
+ {
+ Lnext_branch = Fecode + GET(Fecode, 1);
+ if (*Lnext_branch != OP_ALT) break;
+
+ /* This is never the final branch. We do not need to test for MATCH_THEN
+ here because this code is not used when there is a THEN in the pattern. */
+
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM1);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Fecode = Lnext_branch;
+ }
+
+ /* Hit the start of the final branch. Continue at this level. */
+
+ Fecode += PRIV(OP_lengths)[*Fecode];
+ break;
+
+#undef Lnext_branch
+
+
+ /* ===================================================================== */
+ /* Handle a capturing bracket, other than those that are possessive with an
+ unlimited repeat. */
+
+ case OP_CBRA:
+ case OP_SCBRA:
+ Lframe_type = GF_CAPTURE | GET2(Fecode, 1+LINK_SIZE);
+ goto GROUPLOOP;
+
+
+ /* ===================================================================== */
+ /* Atomic groups and non-capturing brackets that can match an empty string
+ must record a backtracking point and also set up a chained frame. */
+
+ case OP_ONCE:
+ case OP_SBRA:
+ Lframe_type = GF_NOCAPTURE | Fop;
+
+ GROUPLOOP:
+ for (;;)
+ {
+ group_frame_type = Lframe_type;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM2);
+ if (rrc == MATCH_THEN)
+ {
+ PCRE2_SPTR next_ecode = Fecode + GET(Fecode,1);
+ if (mb->verb_ecode_ptr < next_ecode &&
+ (*Fecode == OP_ALT || *next_ecode == OP_ALT))
+ rrc = MATCH_NOMATCH;
+ }
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Fecode += GET(Fecode, 1);
+ if (*Fecode != OP_ALT) RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never reaches here. */
+
+#undef Lframe_type
+
+
+ /* ===================================================================== */
+ /* Recursion either matches the current regex, or some subexpression. The
+ offset data is the offset to the starting bracket from the start of the
+ whole pattern. (This is so that it works from duplicated subpatterns.) */
+
+#define Lframe_type F->temp_32[0]
+#define Lstart_branch F->temp_sptr[0]
+
+ case OP_RECURSE:
+ bracode = mb->start_code + GET(Fecode, 1);
+ number = (bracode == mb->start_code)? 0 : GET2(bracode, 1 + LINK_SIZE);
+
+ /* If we are already in a recursion, check for repeating the same one
+ without advancing the subject pointer. This should catch convoluted mutual
+ recursions. (Some simple cases are caught at compile time.) */
+
+ if (Fcurrent_recurse != RECURSE_UNSET)
+ {
+ offset = Flast_group_offset;
+ while (offset != PCRE2_UNSET)
+ {
+ N = (heapframe *)((char *)mb->match_frames + offset);
+ P = (heapframe *)((char *)N - frame_size);
+ if (N->group_frame_type == (GF_RECURSE | number))
+ {
+ if (Feptr == P->eptr) return PCRE2_ERROR_RECURSELOOP;
+ break;
+ }
+ offset = P->last_group_offset;
+ }
+ }
+
+ /* Now run the recursion, branch by branch. */
+
+ Lstart_branch = bracode;
+ Lframe_type = GF_RECURSE | number;
+
+ for (;;)
+ {
+ PCRE2_SPTR next_ecode;
+
+ group_frame_type = Lframe_type;
+ RMATCH(Lstart_branch + PRIV(OP_lengths)[*Lstart_branch], RM11);
+ next_ecode = Lstart_branch + GET(Lstart_branch,1);
+
+ /* Handle backtracking verbs, which are defined in a range that can
+ easily be tested for. PCRE does not allow THEN, SKIP, PRUNE or COMMIT to
+ escape beyond a recursion; they cause a NOMATCH for the entire recursion.
+
+ When one of these verbs triggers, the current recursion group number is
+ recorded. If it matches the recursion we are processing, the verb
+ happened within the recursion and we must deal with it. Otherwise it must
+ have happened after the recursion completed, and so has to be passed
+ back. See comment above about handling THEN. */
+
+ if (rrc >= MATCH_BACKTRACK_MIN && rrc <= MATCH_BACKTRACK_MAX &&
+ mb->verb_current_recurse == (Lframe_type ^ GF_RECURSE))
+ {
+ if (rrc == MATCH_THEN && mb->verb_ecode_ptr < next_ecode &&
+ (*Lstart_branch == OP_ALT || *next_ecode == OP_ALT))
+ rrc = MATCH_NOMATCH;
+ else RRETURN(MATCH_NOMATCH);
+ }
+
+ /* Note that carrying on after (*ACCEPT) in a recursion is handled in the
+ OP_ACCEPT code. Nothing needs to be done here. */
+
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Lstart_branch = next_ecode;
+ if (*Lstart_branch != OP_ALT) RRETURN(MATCH_NOMATCH);
+ }
+ /* Control never reaches here. */
+
+#undef Lframe_type
+#undef Lstart_branch
+
+
+ /* ===================================================================== */
+ /* Positive assertions are like other groups except that PCRE doesn't allow
+ the effect of (*THEN) to escape beyond an assertion; it is therefore
+ treated as NOMATCH. (*ACCEPT) is treated as successful assertion, with its
+ captures and mark retained. Any other return is an error. */
+
+#define Lframe_type F->temp_32[0]
+
+ case OP_ASSERT:
+ case OP_ASSERTBACK:
+ Lframe_type = GF_NOCAPTURE | Fop;
+ for (;;)
+ {
+ group_frame_type = Lframe_type;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM3);
+ if (rrc == MATCH_ACCEPT)
+ {
+ memcpy(Fovector,
+ (char *)assert_accept_frame + offsetof(heapframe, ovector),
+ assert_accept_frame->offset_top * sizeof(PCRE2_SIZE));
+ Foffset_top = assert_accept_frame->offset_top;
+ Fmark = assert_accept_frame->mark;
+ break;
+ }
+ if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
+ Fecode += GET(Fecode, 1);
+ if (*Fecode != OP_ALT) RRETURN(MATCH_NOMATCH);
+ }
+
+ do Fecode += GET(Fecode, 1); while (*Fecode == OP_ALT);
+ Fecode += 1 + LINK_SIZE;
+ break;
+
+#undef Lframe_type
+
+
+ /* ===================================================================== */
+ /* Handle negative assertions. Loop for each non-matching branch as for
+ positive assertions. */
+
+#define Lframe_type F->temp_32[0]
+
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK_NOT:
+ Lframe_type = GF_NOCAPTURE | Fop;
+
+ for (;;)
+ {
+ group_frame_type = Lframe_type;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM4);
+ switch(rrc)
+ {
+ case MATCH_ACCEPT: /* Assertion matched, therefore it fails. */
+ case MATCH_MATCH:
+ RRETURN (MATCH_NOMATCH);
+
+ case MATCH_NOMATCH: /* Branch failed, try next if present. */
+ case MATCH_THEN:
+ Fecode += GET(Fecode, 1);
+ if (*Fecode != OP_ALT) goto ASSERT_NOT_FAILED;
+ break;
+
+ case MATCH_COMMIT: /* Assertion forced to fail, therefore continue. */
+ case MATCH_SKIP:
+ case MATCH_PRUNE:
+ do Fecode += GET(Fecode, 1); while (*Fecode == OP_ALT);
+ goto ASSERT_NOT_FAILED;
+
+ default: /* Pass back any other return */
+ RRETURN(rrc);
+ }
+ }
+
+ /* None of the branches have matched or there was a backtrack to (*COMMIT),
+ (*SKIP), (*PRUNE), or (*THEN) in the last branch. This is success for a
+ negative assertion, so carry on. */
+
+ ASSERT_NOT_FAILED:
+ Fecode += 1 + LINK_SIZE;
+ break;
+
+#undef Lframe_type
+
+
+ /* ===================================================================== */
+ /* The callout item calls an external function, if one is provided, passing
+ details of the match so far. This is mainly for debugging, though the
+ function is able to force a failure. */
+
+ case OP_CALLOUT:
+ case OP_CALLOUT_STR:
+ rrc = do_callout(F, mb, &length);
+ if (rrc > 0) RRETURN(MATCH_NOMATCH);
+ if (rrc < 0) RRETURN(rrc);
+ Fecode += length;
+ break;
+
+
+ /* ===================================================================== */
+ /* Conditional group: compilation checked that there are no more than two
+ branches. If the condition is false, skipping the first branch takes us
+ past the end of the item if there is only one branch, but that's exactly
+ what we want. */
+
+ case OP_COND:
+ case OP_SCOND:
+
+ /* The variable Flength will be added to Fecode when the condition is
+ false, to get to the second branch. Setting it to the offset to the ALT or
+ KET, then incrementing Fecode achieves this effect. However, if the second
+ branch is non-existent, we must point to the KET so that the end of the
+ group is correctly processed. We now have Fecode pointing to the condition
+ or callout. */
+
+ Flength = GET(Fecode, 1); /* Offset to the second branch */
+ if (Fecode[Flength] != OP_ALT) Flength -= 1 + LINK_SIZE;
+ Fecode += 1 + LINK_SIZE; /* From this opcode */
+
+ /* Because of the way auto-callout works during compile, a callout item is
+ inserted between OP_COND and an assertion condition. Such a callout can
+ also be inserted manually. */
+
+ if (*Fecode == OP_CALLOUT || *Fecode == OP_CALLOUT_STR)
+ {
+ rrc = do_callout(F, mb, &length);
+ if (rrc > 0) RRETURN(MATCH_NOMATCH);
+ if (rrc < 0) RRETURN(rrc);
+
+ /* Advance Fecode past the callout, so it now points to the condition. We
+ must adjust Flength so that the value of Fecode+Flength is unchanged. */
+
+ Fecode += length;
+ Flength -= length;
+ }
+
+ /* Test the various possible conditions */
+
+ condition = FALSE;
+ switch(*Fecode)
+ {
+ case OP_RREF: /* Group recursion test */
+ if (Fcurrent_recurse != RECURSE_UNSET)
+ {
+ number = GET2(Fecode, 1);
+ condition = (number == RREF_ANY || number == Fcurrent_recurse);
+ }
+ break;
+
+ case OP_DNRREF: /* Duplicate named group recursion test */
+ if (Fcurrent_recurse != RECURSE_UNSET)
+ {
+ int count = GET2(Fecode, 1 + IMM2_SIZE);
+ PCRE2_SPTR slot = mb->name_table + GET2(Fecode, 1) * mb->name_entry_size;
+ while (count-- > 0)
+ {
+ number = GET2(slot, 0);
+ condition = number == Fcurrent_recurse;
+ if (condition) break;
+ slot += mb->name_entry_size;
+ }
+ }
+ break;
+
+ case OP_CREF: /* Numbered group used test */
+ offset = (GET2(Fecode, 1) << 1) - 2; /* Doubled ref number */
+ condition = offset < Foffset_top && Fovector[offset] != PCRE2_UNSET;
+ break;
+
+ case OP_DNCREF: /* Duplicate named group used test */
+ {
+ int count = GET2(Fecode, 1 + IMM2_SIZE);
+ PCRE2_SPTR slot = mb->name_table + GET2(Fecode, 1) * mb->name_entry_size;
+ while (count-- > 0)
+ {
+ offset = (GET2(slot, 0) << 1) - 2;
+ condition = offset < Foffset_top && Fovector[offset] != PCRE2_UNSET;
+ if (condition) break;
+ slot += mb->name_entry_size;
+ }
+ }
+ break;
+
+ case OP_FALSE:
+ case OP_FAIL: /* The assertion (?!) becomes OP_FAIL */
+ break;
+
+ case OP_TRUE:
+ condition = TRUE;
+ break;
+
+ /* The condition is an assertion. Run code similar to the assertion code
+ above. */
+
+#define Lpositive F->temp_32[0]
+#define Lstart_branch F->temp_sptr[0]
+
+ default:
+ Lpositive = (*Fecode == OP_ASSERT || *Fecode == OP_ASSERTBACK);
+ Lstart_branch = Fecode;
+
+ for (;;)
+ {
+ group_frame_type = GF_CONDASSERT | *Fecode;
+ RMATCH(Lstart_branch + PRIV(OP_lengths)[*Lstart_branch], RM5);
+
+ switch(rrc)
+ {
+ case MATCH_ACCEPT: /* Save captures */
+ memcpy(Fovector,
+ (char *)assert_accept_frame + offsetof(heapframe, ovector),
+ assert_accept_frame->offset_top * sizeof(PCRE2_SIZE));
+ Foffset_top = assert_accept_frame->offset_top;
+
+ /* Fall through */
+ /* In the case of a match, the captures have already been put into
+ the current frame. */
+
+ case MATCH_MATCH:
+ condition = Lpositive; /* TRUE for positive assertion */
+ break;
+
+ /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
+ assertion; it is therefore always treated as NOMATCH. */
+
+ case MATCH_NOMATCH:
+ case MATCH_THEN:
+ Lstart_branch += GET(Lstart_branch, 1);
+ if (*Lstart_branch == OP_ALT) continue; /* Try next branch */
+ condition = !Lpositive; /* TRUE for negative assertion */
+ break;
+
+ /* These force no match without checking other branches. */
+
+ case MATCH_COMMIT:
+ case MATCH_SKIP:
+ case MATCH_PRUNE:
+ condition = !Lpositive;
+ break;
+
+ default:
+ RRETURN(rrc);
+ }
+ break; /* Out of the branch loop */
+ }
+
+ /* If the condition is true, find the end of the assertion so that
+ advancing past it gets us to the start of the first branch. */
+
+ if (condition)
+ {
+ do Fecode += GET(Fecode, 1); while (*Fecode == OP_ALT);
+ }
+ break; /* End of assertion condition */
+ }
+
+#undef Lpositive
+#undef Lstart_branch
+
+ /* Choose branch according to the condition. */
+
+ Fecode += condition? PRIV(OP_lengths)[*Fecode] : Flength;
+
+ /* If the opcode is OP_SCOND it means we are at a repeated conditional
+ group that might match an empty string. We must therefore descend a level
+ so that the start is remembered for checking. For OP_COND we can just
+ continue at this level. */
+
+ if (Fop == OP_SCOND)
+ {
+ group_frame_type = GF_NOCAPTURE | Fop;
+ RMATCH(Fecode, RM35);
+ RRETURN(rrc);
+ }
+ break;
+
+
+
+/* ========================================================================= */
+/* End of start of parenthesis opcodes */
+/* ========================================================================= */
+
+
+ /* ===================================================================== */
+ /* Move the subject pointer back. This occurs only at the start of each
+ branch of a lookbehind assertion. If we are too close to the start to move
+ back, fail. When working with UTF-8 we move back a number of characters,
+ not bytes. */
+
+ case OP_REVERSE:
+ number = GET(Fecode, 1);
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ while (number-- > 0)
+ {
+ if (Feptr <= mb->start_subject) RRETURN(MATCH_NOMATCH);
+ Feptr--;
+ BACKCHAR(Feptr);
+ }
+ }
+ else
+#endif
+
+ /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
+
+ {
+ if ((ptrdiff_t)number > Feptr - mb->start_subject) RRETURN(MATCH_NOMATCH);
+ Feptr -= number;
+ }
+
+ /* Save the earliest consulted character, then skip to next opcode */
+
+ if (Feptr < mb->start_used_ptr) mb->start_used_ptr = Feptr;
+ Fecode += 1 + LINK_SIZE;
+ break;
+
+
+ /* ===================================================================== */
+ /* An alternation is the end of a branch; scan along to find the end of the
+ bracketed group. */
+
+ case OP_ALT:
+ do Fecode += GET(Fecode,1); while (*Fecode == OP_ALT);
+ break;
+
+
+ /* ===================================================================== */
+ /* The end of a parenthesized group. For all but OP_BRA and OP_COND, the
+ starting frame was added to the chained frames in order to remember the
+ starting subject position for the group. */
+
+ case OP_KET:
+ case OP_KETRMIN:
+ case OP_KETRMAX:
+ case OP_KETRPOS:
+
+ bracode = Fecode - GET(Fecode, 1);
+
+ /* Point N to the frame at the start of the most recent group.
+ Remember the subject pointer at the start of the group. */
+
+ if (*bracode != OP_BRA && *bracode != OP_COND)
+ {
+ N = (heapframe *)((char *)mb->match_frames + Flast_group_offset);
+ P = (heapframe *)((char *)N - frame_size);
+ Flast_group_offset = P->last_group_offset;
+
+#ifdef DEBUG_SHOW_RMATCH
+ fprintf(stderr, "++ KET for frame=%d type=%x prev char offset=%lu\n",
+ N->rdepth, N->group_frame_type,
+ (char *)P->eptr - (char *)mb->start_subject);
+#endif
+
+ /* If we are at the end of an assertion that is a condition, return a
+ match, discarding any intermediate backtracking points. Copy back the
+ captures into the frame before N so that they are set on return. Doing
+ this for all assertions, both positive and negative, seems to match what
+ Perl does. */
+
+ if (GF_IDMASK(N->group_frame_type) == GF_CONDASSERT)
+ {
+ memcpy((char *)P + offsetof(heapframe, ovector), Fovector,
+ Foffset_top * sizeof(PCRE2_SIZE));
+ P->offset_top = Foffset_top;
+ Fback_frame = (char *)F - (char *)P;
+ RRETURN(MATCH_MATCH);
+ }
+ }
+ else P = NULL; /* Indicates starting frame not recorded */
+
+ /* The group was not a conditional assertion. */
+
+ switch (*bracode)
+ {
+ case OP_BRA: /* No need to do anything for these */
+ case OP_COND:
+ case OP_SCOND:
+ break;
+
+ /* Positive assertions are like OP_ONCE, except that in addition the
+ subject pointer must be put back to where it was at the start of the
+ assertion. */
+
+ case OP_ASSERT:
+ case OP_ASSERTBACK:
+ if (Feptr > mb->last_used_ptr) mb->last_used_ptr = Feptr;
+ Feptr = P->eptr;
+ /* Fall through */
+
+ /* For an atomic group, discard internal backtracking points. We must
+ also ensure that any remaining branches within the top-level of the group
+ are not tried. Do this by adjusting the code pointer within the backtrack
+ frame so that it points to the final branch. */
+
+ case OP_ONCE:
+ Fback_frame = ((char *)F - (char *)P);
+ for (;;)
+ {
+ uint32_t y = GET(P->ecode,1);
+ if ((P->ecode)[y] != OP_ALT) break;
+ P->ecode += y;
+ }
+ break;
+
+ /* A matching negative assertion returns MATCH, which is turned into
+ NOMATCH at the assertion level. */
+
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK_NOT:
+ RRETURN(MATCH_MATCH);
+
+ /* Whole-pattern recursion is coded as a recurse into group 0, so it
+ won't be picked up here. Instead, we catch it when the OP_END is reached.
+ Other recursion is handled here. */
+
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRA:
+ case OP_SCBRAPOS:
+ number = GET2(bracode, 1+LINK_SIZE);
+
+ /* Handle a recursively called group. We reinstate the previous set of
+ captures and then carry on after the recursion call. */
+
+ if (Fcurrent_recurse == number)
+ {
+ P = (heapframe *)((char *)N - frame_size);
+ memcpy((char *)F + offsetof(heapframe, ovector), P->ovector,
+ P->offset_top * sizeof(PCRE2_SIZE));
+ Foffset_top = P->offset_top;
+ Fcapture_last = P->capture_last;
+ Fcurrent_recurse = P->current_recurse;
+ Fecode = P->ecode + 1 + LINK_SIZE;
+ continue; /* With next opcode */
+ }
+
+ /* Deal with actual capturing. */
+
+ offset = (number << 1) - 2;
+ Fcapture_last = number;
+ Fovector[offset] = P->eptr - mb->start_subject;
+ Fovector[offset+1] = Feptr - mb->start_subject;
+ if (offset >= Foffset_top) Foffset_top = offset + 2;
+ break;
+ } /* End actions relating to the starting opcode */
+
+ /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
+ and return the MATCH_KETRPOS. This makes it possible to do the repeats one
+ at a time from the outer level. This must precede the empty string test -
+ in this case that test is done at the outer level. */
+
+ if (*Fecode == OP_KETRPOS)
+ {
+ memcpy((char *)P + offsetof(heapframe, eptr),
+ (char *)F + offsetof(heapframe, eptr),
+ frame_copy_size);
+ RRETURN(MATCH_KETRPOS);
+ }
+
+ /* Handle the different kinds of closing brackets. A non-repeating ket
+ needs no special action, just continuing at this level. This also happens
+ for the repeating kets if the group matched no characters, in order to
+ forcibly break infinite loops. Otherwise, the repeating kets try the rest
+ of the pattern or restart from the preceding bracket, in the appropriate
+ order. */
+
+ if (Fop != OP_KET && (P == NULL || Feptr != P->eptr))
+ {
+ if (Fop == OP_KETRMIN)
+ {
+ RMATCH(Fecode + 1 + LINK_SIZE, RM6);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ Fecode -= GET(Fecode, 1);
+ break; /* End of ket processing */
+ }
+
+ /* Repeat the maximum number of times (KETRMAX) */
+
+ RMATCH(bracode, RM7);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ }
+
+ /* Carry on at this level for a non-repeating ket, or after matching an
+ empty string, or after repeating for a maximum number of times. */
+
+ Fecode += 1 + LINK_SIZE;
+ break;
+
+
+ /* ===================================================================== */
+ /* Start and end of line assertions, not multiline mode. */
+
+ case OP_CIRC: /* Start of line, unless PCRE2_NOTBOL is set. */
+ if (Feptr != mb->start_subject || (mb->moptions & PCRE2_NOTBOL) != 0)
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ case OP_SOD: /* Unconditional start of subject */
+ if (Feptr != mb->start_subject) RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ /* When PCRE2_NOTEOL is unset, assert before the subject end, or a
+ terminating newline unless PCRE2_DOLLAR_ENDONLY is set. */
+
+ case OP_DOLL:
+ if ((mb->moptions & PCRE2_NOTEOL) != 0) RRETURN(MATCH_NOMATCH);
+ if ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0) goto ASSERT_NL_OR_EOS;
+
+ /* Fall through */
+ /* Unconditional end of subject assertion (\z) */
+
+ case OP_EOD:
+ if (Feptr < mb->end_subject) RRETURN(MATCH_NOMATCH);
+ SCHECK_PARTIAL();
+ Fecode++;
+ break;
+
+ /* End of subject or ending \n assertion (\Z) */
+
+ case OP_EODN:
+ ASSERT_NL_OR_EOS:
+ if (Feptr < mb->end_subject &&
+ (!IS_NEWLINE(Feptr) || Feptr != mb->end_subject - mb->nllen))
+ {
+ if (mb->partial != 0 &&
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ UCHAR21TEST(Feptr) == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ RRETURN(MATCH_NOMATCH);
+ }
+
+ /* Either at end of string or \n before end. */
+
+ SCHECK_PARTIAL();
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Start and end of line assertions, multiline mode. */
+
+ /* Start of subject unless notbol, or after any newline except for one at
+ the very end, unless PCRE2_ALT_CIRCUMFLEX is set. */
+
+ case OP_CIRCM:
+ if ((mb->moptions & PCRE2_NOTBOL) != 0 && Feptr == mb->start_subject)
+ RRETURN(MATCH_NOMATCH);
+ if (Feptr != mb->start_subject &&
+ ((Feptr == mb->end_subject &&
+ (mb->poptions & PCRE2_ALT_CIRCUMFLEX) == 0) ||
+ !WAS_NEWLINE(Feptr)))
+ RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+ /* Assert before any newline, or before end of subject unless noteol is
+ set. */
+
+ case OP_DOLLM:
+ if (Feptr < mb->end_subject)
+ {
+ if (!IS_NEWLINE(Feptr))
+ {
+ if (mb->partial != 0 &&
+ Feptr + 1 >= mb->end_subject &&
+ NLBLOCK->nltype == NLTYPE_FIXED &&
+ NLBLOCK->nllen == 2 &&
+ UCHAR21TEST(Feptr) == NLBLOCK->nl[0])
+ {
+ mb->hitend = TRUE;
+ if (mb->partial > 1) return PCRE2_ERROR_PARTIAL;
+ }
+ RRETURN(MATCH_NOMATCH);
+ }
+ }
+ else
+ {
+ if ((mb->moptions & PCRE2_NOTEOL) != 0) RRETURN(MATCH_NOMATCH);
+ SCHECK_PARTIAL();
+ }
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Start of match assertion */
+
+ case OP_SOM:
+ if (Feptr != mb->start_subject + mb->start_offset) RRETURN(MATCH_NOMATCH);
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Reset the start of match point */
+
+ case OP_SET_SOM:
+ Fstart_match = Feptr;
+ Fecode++;
+ break;
+
+
+ /* ===================================================================== */
+ /* Word boundary assertions. Find out if the previous and current
+ characters are "word" characters. It takes a bit more work in UTF mode.
+ Characters > 255 are assumed to be "non-word" characters when PCRE2_UCP is
+ not set. When it is set, use Unicode properties if available, even when not
+ in UTF mode. Remember the earliest and latest consulted characters. */
+
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ if (Feptr == mb->start_subject) prev_is_word = FALSE; else
+ {
+ PCRE2_SPTR lastptr = Feptr - 1;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ BACKCHAR(lastptr);
+ GETCHAR(fc, lastptr);
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ fc = *lastptr;
+ if (lastptr < mb->start_used_ptr) mb->start_used_ptr = lastptr;
+#ifdef SUPPORT_UNICODE
+ if ((mb->poptions & PCRE2_UCP) != 0)
+ {
+ if (fc == '_') prev_is_word = TRUE; else
+ {
+ int cat = UCD_CATEGORY(fc);
+ prev_is_word = (cat == ucp_L || cat == ucp_N);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ prev_is_word = CHMAX_255(fc) && (mb->ctypes[fc] & ctype_word) != 0;
+ }
+
+ /* Get status of next character */
+
+ if (Feptr >= mb->end_subject)
+ {
+ SCHECK_PARTIAL();
+ cur_is_word = FALSE;
+ }
+ else
+ {
+ PCRE2_SPTR nextptr = Feptr + 1;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ FORWARDCHARTEST(nextptr, mb->end_subject);
+ GETCHAR(fc, Feptr);
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ fc = *Feptr;
+ if (nextptr > mb->last_used_ptr) mb->last_used_ptr = nextptr;
+#ifdef SUPPORT_UNICODE
+ if ((mb->poptions & PCRE2_UCP) != 0)
+ {
+ if (fc == '_') cur_is_word = TRUE; else
+ {
+ int cat = UCD_CATEGORY(fc);
+ cur_is_word = (cat == ucp_L || cat == ucp_N);
+ }
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+ cur_is_word = CHMAX_255(fc) && (mb->ctypes[fc] & ctype_word) != 0;
+ }
+
+ /* Now see if the situation is what we want */
+
+ if ((*Fecode++ == OP_WORD_BOUNDARY)?
+ cur_is_word == prev_is_word : cur_is_word != prev_is_word)
+ RRETURN(MATCH_NOMATCH);
+ break;
+
+
+ /* ===================================================================== */
+ /* Backtracking (*VERB)s, with and without arguments. Note that if the
+ pattern is successfully matched, we do not come back from RMATCH. */
+
+ case OP_MARK:
+ Fmark = mb->nomatch_mark = Fecode + 2;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode] + Fecode[1], RM12);
+
+ /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
+ argument, and we must check whether that argument matches this MARK's
+ argument. It is passed back in mb->verb_skip_ptr. If it does match, we
+ return MATCH_SKIP with mb->verb_skip_ptr now pointing to the subject
+ position that corresponds to this mark. Otherwise, pass back the return
+ code unaltered. */
+
+ if (rrc == MATCH_SKIP_ARG &&
+ PRIV(strcmp)(Fecode + 2, mb->verb_skip_ptr) == 0)
+ {
+ mb->verb_skip_ptr = Feptr; /* Pass back current position */
+ RRETURN(MATCH_SKIP);
+ }
+ RRETURN(rrc);
+
+ case OP_FAIL:
+ RRETURN(MATCH_NOMATCH);
+
+ /* Record the current recursing group number in mb->verb_current_recurse
+ when a backtracking return such as MATCH_COMMIT is given. This enables the
+ recurse processing to catch verbs from within the recursion. */
+
+ case OP_COMMIT:
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM13);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_COMMIT);
+
+ case OP_COMMIT_ARG:
+ Fmark = mb->nomatch_mark = Fecode + 2;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode] + Fecode[1], RM36);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_COMMIT);
+
+ case OP_PRUNE:
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM14);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_PRUNE);
+
+ case OP_PRUNE_ARG:
+ Fmark = mb->nomatch_mark = Fecode + 2;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode] + Fecode[1], RM15);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_PRUNE);
+
+ case OP_SKIP:
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM16);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_skip_ptr = Feptr; /* Pass back current position */
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_SKIP);
+
+ /* Note that, for Perl compatibility, SKIP with an argument does NOT set
+ nomatch_mark. When a pattern match ends with a SKIP_ARG for which there was
+ not a matching mark, we have to re-run the match, ignoring the SKIP_ARG
+ that failed and any that precede it (either they also failed, or were not
+ triggered). To do this, we maintain a count of executed SKIP_ARGs. If a
+ SKIP_ARG gets to top level, the match is re-run with mb->ignore_skip_arg
+ set to the count of the one that failed. */
+
+ case OP_SKIP_ARG:
+ mb->skip_arg_count++;
+ if (mb->skip_arg_count <= mb->ignore_skip_arg)
+ {
+ Fecode += PRIV(OP_lengths)[*Fecode] + Fecode[1];
+ break;
+ }
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode] + Fecode[1], RM17);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+
+ /* Pass back the current skip name and return the special MATCH_SKIP_ARG
+ return code. This will either be caught by a matching MARK, or get to the
+ top, where it causes a rematch with mb->ignore_skip_arg set to the value of
+ mb->skip_arg_count. */
+
+ mb->verb_skip_ptr = Fecode + 2;
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_SKIP_ARG);
+
+ /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
+ the branch in which it occurs can be determined. */
+
+ case OP_THEN:
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode], RM18);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_ecode_ptr = Fecode;
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_THEN);
+
+ case OP_THEN_ARG:
+ Fmark = mb->nomatch_mark = Fecode + 2;
+ RMATCH(Fecode + PRIV(OP_lengths)[*Fecode] + Fecode[1], RM19);
+ if (rrc != MATCH_NOMATCH) RRETURN(rrc);
+ mb->verb_ecode_ptr = Fecode;
+ mb->verb_current_recurse = Fcurrent_recurse;
+ RRETURN(MATCH_THEN);
+
+
+ /* ===================================================================== */
+ /* There's been some horrible disaster. Arrival here can only mean there is
+ something seriously wrong in the code above or the OP_xxx definitions. */
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+
+ /* Do not insert any code in here without much thought; it is assumed
+ that "continue" in the code above comes out to here to repeat the main
+ loop. */
+
+ } /* End of main loop */
+/* Control never reaches here */
+
+
+/* ========================================================================= */
+/* The RRETURN() macro jumps here. The number that is saved in Freturn_id
+indicates which label we actually want to return to. The value in Frdepth is
+the index number of the frame in the vector. The return value has been placed
+in rrc. */
+
+#define LBL(val) case val: goto L_RM##val;
+
+RETURN_SWITCH:
+if (Frdepth == 0) return rrc; /* Exit from the top level */
+F = (heapframe *)((char *)F - Fback_frame); /* Backtrack */
+mb->cb->callout_flags |= PCRE2_CALLOUT_BACKTRACK; /* Note for callouts */
+
+#ifdef DEBUG_SHOW_RMATCH
+fprintf(stderr, "++ RETURN %d to %d\n", rrc, Freturn_id);
+#endif
+
+switch (Freturn_id)
+ {
+ LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8)
+ LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(16)
+ LBL(17) LBL(18) LBL(19) LBL(20) LBL(21) LBL(22) LBL(23) LBL(24)
+ LBL(25) LBL(26) LBL(27) LBL(28) LBL(29) LBL(30) LBL(31) LBL(32)
+ LBL(33) LBL(34) LBL(35) LBL(36)
+
+#ifdef SUPPORT_WIDE_CHARS
+ LBL(100) LBL(101)
+#endif
+
+#ifdef SUPPORT_UNICODE
+ LBL(200) LBL(201) LBL(202) LBL(203) LBL(204) LBL(205) LBL(206)
+ LBL(207) LBL(208) LBL(209) LBL(210) LBL(211) LBL(212) LBL(213)
+ LBL(214) LBL(215) LBL(216) LBL(217) LBL(218) LBL(219) LBL(220)
+ LBL(221) LBL(222)
+#endif
+
+ default:
+ return PCRE2_ERROR_INTERNAL;
+ }
+#undef LBL
+}
+
+
+/*************************************************
+* Match a Regular Expression *
+*************************************************/
+
+/* This function applies a compiled pattern to a subject string and picks out
+portions of the string if it matches. Two elements in the vector are set for
+each substring: the offsets to the start and end of the substring.
+
+Arguments:
+ code points to the compiled expression
+ subject points to the subject string
+ length length of subject string (may contain binary zeros)
+ start_offset where to start in the subject string
+ options option bits
+ match_data points to a match_data block
+ mcontext points a PCRE2 context
+
+Returns: > 0 => success; value is the number of ovector pairs filled
+ = 0 => success, but ovector is not big enough
+ -1 => failed to match (PCRE2_ERROR_NOMATCH)
+ -2 => partial match (PCRE2_ERROR_PARTIAL)
+ < -2 => some kind of unexpected problem
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
+ PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
+ pcre2_match_context *mcontext)
+{
+int rc;
+const uint8_t *start_bits = NULL;
+
+const pcre2_real_code *re = (const pcre2_real_code *)code;
+
+BOOL anchored;
+BOOL firstline;
+BOOL has_first_cu = FALSE;
+BOOL has_req_cu = FALSE;
+BOOL startline;
+BOOL utf;
+
+PCRE2_UCHAR first_cu = 0;
+PCRE2_UCHAR first_cu2 = 0;
+PCRE2_UCHAR req_cu = 0;
+PCRE2_UCHAR req_cu2 = 0;
+
+PCRE2_SPTR bumpalong_limit;
+PCRE2_SPTR end_subject;
+PCRE2_SPTR start_match = subject + start_offset;
+PCRE2_SPTR req_cu_ptr = start_match - 1;
+PCRE2_SPTR start_partial = NULL;
+PCRE2_SPTR match_partial = NULL;
+
+PCRE2_SIZE frame_size;
+
+/* We need to have mb as a pointer to a match block, because the IS_NEWLINE
+macro is used below, and it expects NLBLOCK to be defined as a pointer. */
+
+pcre2_callout_block cb;
+match_block actual_match_block;
+match_block *mb = &actual_match_block;
+
+/* Allocate an initial vector of backtracking frames on the stack. If this
+proves to be too small, it is replaced by a larger one on the heap. To get a
+vector of the size required that is aligned for pointers, allocate it as a
+vector of pointers. */
+
+PCRE2_SPTR stack_frames_vector[START_FRAMES_SIZE/sizeof(PCRE2_SPTR)];
+mb->stack_frames = (heapframe *)stack_frames_vector;
+
+/* A length equal to PCRE2_ZERO_TERMINATED implies a zero-terminated
+subject string. */
+
+if (length == PCRE2_ZERO_TERMINATED) length = PRIV(strlen)(subject);
+end_subject = subject + length;
+
+/* Plausibility checks */
+
+if ((options & ~PUBLIC_MATCH_OPTIONS) != 0) return PCRE2_ERROR_BADOPTION;
+if (code == NULL || subject == NULL || match_data == NULL)
+ return PCRE2_ERROR_NULL;
+if (start_offset > length) return PCRE2_ERROR_BADOFFSET;
+
+/* Check that the first field in the block is the magic number. */
+
+if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
+
+/* Check the code unit width. */
+
+if ((re->flags & PCRE2_MODE_MASK) != PCRE2_CODE_UNIT_WIDTH/8)
+ return PCRE2_ERROR_BADMODE;
+
+/* PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART are match-time flags in the
+options variable for this function. Users of PCRE2 who are not calling the
+function directly would like to have a way of setting these flags, in the same
+way that they can set pcre2_compile() flags like PCRE2_NO_AUTOPOSSESS with
+constructions like (*NO_AUTOPOSSESS). To enable this, (*NOTEMPTY) and
+(*NOTEMPTY_ATSTART) set bits in the pattern's "flag" function which we now
+transfer to the options for this function. The bits are guaranteed to be
+adjacent, but do not have the same values. This bit of Boolean trickery assumes
+that the match-time bits are not more significant than the flag bits. If by
+accident this is not the case, a compile-time division by zero error will
+occur. */
+
+#define FF (PCRE2_NOTEMPTY_SET|PCRE2_NE_ATST_SET)
+#define OO (PCRE2_NOTEMPTY|PCRE2_NOTEMPTY_ATSTART)
+options |= (re->flags & FF) / ((FF & (~FF+1)) / (OO & (~OO+1)));
+#undef FF
+#undef OO
+
+/* These two settings are used in the code for checking a UTF string that
+follows immediately afterwards. Other values in the mb block are used only
+during interpretive processing, not when the JIT support is in use, so they are
+set up later. */
+
+utf = (re->overall_options & PCRE2_UTF) != 0;
+mb->partial = ((options & PCRE2_PARTIAL_HARD) != 0)? 2 :
+ ((options & PCRE2_PARTIAL_SOFT) != 0)? 1 : 0;
+
+/* Partial matching and PCRE2_ENDANCHORED are currently not allowed at the same
+time. */
+
+if (mb->partial != 0 &&
+ ((re->overall_options | options) & PCRE2_ENDANCHORED) != 0)
+ return PCRE2_ERROR_BADOPTION;
+
+/* Check a UTF string for validity if required. For 8-bit and 16-bit strings,
+we must also check that a starting offset does not point into the middle of a
+multiunit character. We check only the portion of the subject that is going to
+be inspected during matching - from the offset minus the maximum back reference
+to the given length. This saves time when a small part of a large subject is
+being matched by the use of a starting offset. Note that the maximum lookbehind
+is a number of characters, not code units. */
+
+#ifdef SUPPORT_UNICODE
+if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
+ {
+ PCRE2_SPTR check_subject = start_match; /* start_match includes offset */
+
+ if (start_offset > 0)
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 32
+ unsigned int i;
+ if (start_match < end_subject && NOT_FIRSTCU(*start_match))
+ return PCRE2_ERROR_BADUTFOFFSET;
+ for (i = re->max_lookbehind; i > 0 && check_subject > subject; i--)
+ {
+ check_subject--;
+ while (check_subject > subject &&
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ (*check_subject & 0xc0) == 0x80)
+#else /* 16-bit */
+ (*check_subject & 0xfc00) == 0xdc00)
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+ check_subject--;
+ }
+#else
+ /* In the 32-bit library, one code unit equals one character. However,
+ we cannot just subtract the lookbehind and then compare pointers, because
+ a very large lookbehind could create an invalid pointer. */
+
+ if (start_offset >= re->max_lookbehind)
+ check_subject -= re->max_lookbehind;
+ else
+ check_subject = subject;
+#endif /* PCRE2_CODE_UNIT_WIDTH != 32 */
+ }
+
+ /* Validate the relevant portion of the subject. After an error, adjust the
+ offset to be an absolute offset in the whole string. */
+
+ match_data->rc = PRIV(valid_utf)(check_subject,
+ length - (check_subject - subject), &(match_data->startchar));
+ if (match_data->rc != 0)
+ {
+ match_data->startchar += check_subject - subject;
+ return match_data->rc;
+ }
+ }
+#endif /* SUPPORT_UNICODE */
+
+/* It is an error to set an offset limit without setting the flag at compile
+time. */
+
+if (mcontext != NULL && mcontext->offset_limit != PCRE2_UNSET &&
+ (re->overall_options & PCRE2_USE_OFFSET_LIMIT) == 0)
+ return PCRE2_ERROR_BADOFFSETLIMIT;
+
+/* If the pattern was successfully studied with JIT support, run the JIT
+executable instead of the rest of this function. Most options must be set at
+compile time for the JIT code to be usable. Fallback to the normal code path if
+an unsupported option is set or if JIT returns BADOPTION (which means that the
+selected normal or partial matching mode was not compiled). */
+
+#ifdef SUPPORT_JIT
+if (re->executable_jit != NULL && (options & ~PUBLIC_JIT_MATCH_OPTIONS) == 0)
+ {
+ rc = pcre2_jit_match(code, subject, length, start_offset, options,
+ match_data, mcontext);
+ if (rc != PCRE2_ERROR_JIT_BADOPTION) return rc;
+ }
+#endif
+
+/* Carry on with non-JIT matching. A NULL match context means "use a default
+context", but we take the memory control functions from the pattern. */
+
+if (mcontext == NULL)
+ {
+ mcontext = (pcre2_match_context *)(&PRIV(default_match_context));
+ mb->memctl = re->memctl;
+ }
+else mb->memctl = mcontext->memctl;
+
+anchored = ((re->overall_options | options) & PCRE2_ANCHORED) != 0;
+firstline = (re->overall_options & PCRE2_FIRSTLINE) != 0;
+startline = (re->flags & PCRE2_STARTLINE) != 0;
+bumpalong_limit = (mcontext->offset_limit == PCRE2_UNSET)?
+ end_subject : subject + mcontext->offset_limit;
+
+/* Initialize and set up the fixed fields in the callout block, with a pointer
+in the match block. */
+
+mb->cb = &cb;
+cb.version = 2;
+cb.subject = subject;
+cb.subject_length = (PCRE2_SIZE)(end_subject - subject);
+cb.callout_flags = 0;
+
+/* Fill in the remaining fields in the match block. */
+
+mb->callout = mcontext->callout;
+mb->callout_data = mcontext->callout_data;
+
+mb->start_subject = subject;
+mb->start_offset = start_offset;
+mb->end_subject = end_subject;
+mb->hasthen = (re->flags & PCRE2_HASTHEN) != 0;
+
+mb->moptions = options; /* Match options */
+mb->poptions = re->overall_options; /* Pattern options */
+
+mb->ignore_skip_arg = 0;
+mb->mark = mb->nomatch_mark = NULL; /* In case never set */
+mb->hitend = FALSE;
+
+/* The name table is needed for finding all the numbers associated with a
+given name, for condition testing. The code follows the name table. */
+
+mb->name_table = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code));
+mb->name_count = re->name_count;
+mb->name_entry_size = re->name_entry_size;
+mb->start_code = mb->name_table + re->name_count * re->name_entry_size;
+
+/* Process the \R and newline settings. */
+
+mb->bsr_convention = re->bsr_convention;
+mb->nltype = NLTYPE_FIXED;
+switch(re->newline_convention)
+ {
+ case PCRE2_NEWLINE_CR:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_CR;
+ break;
+
+ case PCRE2_NEWLINE_LF:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_NUL:
+ mb->nllen = 1;
+ mb->nl[0] = CHAR_NUL;
+ break;
+
+ case PCRE2_NEWLINE_CRLF:
+ mb->nllen = 2;
+ mb->nl[0] = CHAR_CR;
+ mb->nl[1] = CHAR_NL;
+ break;
+
+ case PCRE2_NEWLINE_ANY:
+ mb->nltype = NLTYPE_ANY;
+ break;
+
+ case PCRE2_NEWLINE_ANYCRLF:
+ mb->nltype = NLTYPE_ANYCRLF;
+ break;
+
+ default: return PCRE2_ERROR_INTERNAL;
+ }
+
+/* The backtracking frames have fixed data at the front, and a PCRE2_SIZE
+vector at the end, whose size depends on the number of capturing parentheses in
+the pattern. It is not used at all if there are no capturing parentheses.
+
+ frame_size is the total size of each frame
+ mb->frame_vector_size is the total usable size of the vector (rounded down
+ to a whole number of frames)
+
+The last of these is changed within the match() function if the frame vector
+has to be expanded. We therefore put it into the match block so that it is
+correct when calling match() more than once for non-anchored patterns. */
+
+frame_size = offsetof(heapframe, ovector) +
+ re->top_bracket * 2 * sizeof(PCRE2_SIZE);
+
+/* Limits set in the pattern override the match context only if they are
+smaller. */
+
+mb->heap_limit = (mcontext->heap_limit < re->limit_heap)?
+ mcontext->heap_limit : re->limit_heap;
+
+mb->match_limit = (mcontext->match_limit < re->limit_match)?
+ mcontext->match_limit : re->limit_match;
+
+mb->match_limit_depth = (mcontext->depth_limit < re->limit_depth)?
+ mcontext->depth_limit : re->limit_depth;
+
+/* If a pattern has very many capturing parentheses, the frame size may be very
+large. Ensure that there are at least 10 available frames by getting an initial
+vector on the heap if necessary, except when the heap limit prevents this. Get
+fewer if possible. (The heap limit is in kibibytes.) */
+
+if (frame_size <= START_FRAMES_SIZE/10)
+ {
+ mb->match_frames = mb->stack_frames; /* Initial frame vector on the stack */
+ mb->frame_vector_size = ((START_FRAMES_SIZE/frame_size) * frame_size);
+ }
+else
+ {
+ mb->frame_vector_size = frame_size * 10;
+ if ((mb->frame_vector_size / 1024) > mb->heap_limit)
+ {
+ if (frame_size > mb->heap_limit * 1024) return PCRE2_ERROR_HEAPLIMIT;
+ mb->frame_vector_size = ((mb->heap_limit * 1024)/frame_size) * frame_size;
+ }
+ mb->match_frames = mb->memctl.malloc(mb->frame_vector_size,
+ mb->memctl.memory_data);
+ if (mb->match_frames == NULL) return PCRE2_ERROR_NOMEMORY;
+ }
+
+mb->match_frames_top =
+ (heapframe *)((char *)mb->match_frames + mb->frame_vector_size);
+
+/* Write to the ovector within the first frame to mark every capture unset and
+to avoid uninitialized memory read errors when it is copied to a new frame. */
+
+memset((char *)(mb->match_frames) + offsetof(heapframe, ovector), 0xff,
+ re->top_bracket * 2 * sizeof(PCRE2_SIZE));
+
+/* Pointers to the individual character tables */
+
+mb->lcc = re->tables + lcc_offset;
+mb->fcc = re->tables + fcc_offset;
+mb->ctypes = re->tables + ctypes_offset;
+
+/* Set up the first code unit to match, if available. If there's no first code
+unit there may be a bitmap of possible first characters. */
+
+if ((re->flags & PCRE2_FIRSTSET) != 0)
+ {
+ has_first_cu = TRUE;
+ first_cu = first_cu2 = (PCRE2_UCHAR)(re->first_codeunit);
+ if ((re->flags & PCRE2_FIRSTCASELESS) != 0)
+ {
+ first_cu2 = TABLE_GET(first_cu, mb->fcc, first_cu);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (utf && first_cu > 127) first_cu2 = UCD_OTHERCASE(first_cu);
+#endif
+ }
+ }
+else
+ if (!startline && (re->flags & PCRE2_FIRSTMAPSET) != 0)
+ start_bits = re->start_bitmap;
+
+/* There may also be a "last known required character" set. */
+
+if ((re->flags & PCRE2_LASTSET) != 0)
+ {
+ has_req_cu = TRUE;
+ req_cu = req_cu2 = (PCRE2_UCHAR)(re->last_codeunit);
+ if ((re->flags & PCRE2_LASTCASELESS) != 0)
+ {
+ req_cu2 = TABLE_GET(req_cu, mb->fcc, req_cu);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 8
+ if (utf && req_cu > 127) req_cu2 = UCD_OTHERCASE(req_cu);
+#endif
+ }
+ }
+
+
+/* ==========================================================================*/
+
+/* Loop for handling unanchored repeated matching attempts; for anchored regexs
+the loop runs just once. */
+
+for(;;)
+ {
+ PCRE2_SPTR new_start_match;
+
+ /* ----------------- Start of match optimizations ---------------- */
+
+ /* There are some optimizations that avoid running the match if a known
+ starting point is not found, or if a known later code unit is not present.
+ However, there is an option (settable at compile time) that disables these,
+ for testing and for ensuring that all callouts do actually occur. */
+
+ if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0)
+ {
+ /* If firstline is TRUE, the start of the match is constrained to the first
+ line of a multiline string. That is, the match must be before or at the
+ first newline following the start of matching. Temporarily adjust
+ end_subject so that we stop the scans for a first code unit at a newline.
+ If the match fails at the newline, later code breaks the loop. */
+
+ if (firstline)
+ {
+ PCRE2_SPTR t = start_match;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ while (t < end_subject && !IS_NEWLINE(t))
+ {
+ t++;
+ ACROSSCHAR(t < end_subject, t, t++);
+ }
+ }
+ else
+#endif
+ while (t < end_subject && !IS_NEWLINE(t)) t++;
+ end_subject = t;
+ }
+
+ /* Anchored: check the first code unit if one is recorded. This may seem
+ pointless but it can help in detecting a no match case without scanning for
+ the required code unit. */
+
+ if (anchored)
+ {
+ if (has_first_cu || start_bits != NULL)
+ {
+ BOOL ok = start_match < end_subject;
+ if (ok)
+ {
+ PCRE2_UCHAR c = UCHAR21TEST(start_match);
+ ok = has_first_cu && (c == first_cu || c == first_cu2);
+ if (!ok && start_bits != NULL)
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (c > 255) c = 255;
+#endif
+ ok = (start_bits[c/8] & (1 << (c&7))) != 0;
+ }
+ }
+ if (!ok)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+ }
+ }
+
+ /* Not anchored. Advance to a unique first code unit if there is one. In
+ 8-bit mode, the use of memchr() gives a big speed up, even though we have
+ to call it twice in caseless mode, in order to find the earliest occurrence
+ of the character in either of its cases. */
+
+ else
+ {
+ if (has_first_cu)
+ {
+ if (first_cu != first_cu2) /* Caseless */
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ PCRE2_UCHAR smc;
+ while (start_match < end_subject &&
+ (smc = UCHAR21TEST(start_match)) != first_cu &&
+ smc != first_cu2)
+ start_match++;
+#else /* 8-bit code units */
+ PCRE2_SPTR pp1 =
+ memchr(start_match, first_cu, end_subject-start_match);
+ PCRE2_SPTR pp2 =
+ memchr(start_match, first_cu2, end_subject-start_match);
+ if (pp1 == NULL)
+ start_match = (pp2 == NULL)? end_subject : pp2;
+ else
+ start_match = (pp2 == NULL || pp1 < pp2)? pp1 : pp2;
+#endif
+ }
+
+ /* The caseful case */
+
+ else
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ while (start_match < end_subject && UCHAR21TEST(start_match) !=
+ first_cu)
+ start_match++;
+#else
+ start_match = memchr(start_match, first_cu, end_subject - start_match);
+ if (start_match == NULL) start_match = end_subject;
+#endif
+ }
+
+ /* If we can't find the required code unit, having reached the true end
+ of the subject, break the bumpalong loop, to force a match failure,
+ except when doing partial matching, when we let the next cycle run at
+ the end of the subject. To see why, consider the pattern /(?<=abc)def/,
+ which partially matches "abc", even though the string does not contain
+ the starting character "d". If we have not reached the true end of the
+ subject (PCRE2_FIRSTLINE caused end_subject to be temporarily modified)
+ we also let the cycle run, because the matching string is legitimately
+ allowed to start with the first code unit of a newline. */
+
+ if (!mb->partial && start_match >= mb->end_subject)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+ }
+
+ /* If there's no first code unit, advance to just after a linebreak for a
+ multiline match if required. */
+
+ else if (startline)
+ {
+ if (start_match > mb->start_subject + start_offset)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ while (start_match < end_subject && !WAS_NEWLINE(start_match))
+ {
+ start_match++;
+ ACROSSCHAR(start_match < end_subject, start_match, start_match++);
+ }
+ }
+ else
+#endif
+ while (start_match < end_subject && !WAS_NEWLINE(start_match))
+ start_match++;
+
+ /* If we have just passed a CR and the newline option is ANY or
+ ANYCRLF, and we are now at a LF, advance the match position by one
+ more code unit. */
+
+ if (start_match[-1] == CHAR_CR &&
+ (mb->nltype == NLTYPE_ANY || mb->nltype == NLTYPE_ANYCRLF) &&
+ start_match < end_subject &&
+ UCHAR21TEST(start_match) == CHAR_NL)
+ start_match++;
+ }
+ }
+
+ /* If there's no first code unit or a requirement for a multiline line
+ start, advance to a non-unique first code unit if any have been
+ identified. The bitmap contains only 256 bits. When code units are 16 or
+ 32 bits wide, all code units greater than 254 set the 255 bit. */
+
+ else if (start_bits != NULL)
+ {
+ while (start_match < end_subject)
+ {
+ uint32_t c = UCHAR21TEST(start_match);
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ if (c > 255) c = 255;
+#endif
+ if ((start_bits[c/8] & (1 << (c&7))) != 0) break;
+ start_match++;
+ }
+
+ /* See comment above in first_cu checking about the next few lines. */
+
+ if (!mb->partial && start_match >= mb->end_subject)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+ }
+ } /* End first code unit handling */
+
+ /* Restore fudged end_subject */
+
+ end_subject = mb->end_subject;
+
+ /* The following two optimizations must be disabled for partial matching. */
+
+ if (!mb->partial)
+ {
+ /* The minimum matching length is a lower bound; no string of that length
+ may actually match the pattern. Although the value is, strictly, in
+ characters, we treat it as code units to avoid spending too much time in
+ this optimization. */
+
+ if (end_subject - start_match < re->minlength)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+
+ /* If req_cu is set, we know that that code unit must appear in the
+ subject for the (non-partial) match to succeed. If the first code unit is
+ set, req_cu must be later in the subject; otherwise the test starts at
+ the match point. This optimization can save a huge amount of backtracking
+ in patterns with nested unlimited repeats that aren't going to match.
+ Writing separate code for caseful/caseless versions makes it go faster,
+ as does using an autoincrement and backing off on a match. As in the case
+ of the first code unit, using memchr() in the 8-bit library gives a big
+ speed up. Unlike the first_cu check above, we do not need to call
+ memchr() twice in the caseless case because we only need to check for the
+ presence of the character in either case, not find the first occurrence.
+
+ HOWEVER: when the subject string is very, very long, searching to its end
+ can take a long time, and give bad performance on quite ordinary
+ patterns. This showed up when somebody was matching something like
+ /^\d+C/ on a 32-megabyte string... so we don't do this when the string is
+ sufficiently long. */
+
+ if (has_req_cu && end_subject - start_match < REQ_CU_MAX)
+ {
+ PCRE2_SPTR p = start_match + (has_first_cu? 1:0);
+
+ /* We don't need to repeat the search if we haven't yet reached the
+ place we found it last time round the bumpalong loop. */
+
+ if (p > req_cu_ptr)
+ {
+ if (p < end_subject)
+ {
+ if (req_cu != req_cu2) /* Caseless */
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ do
+ {
+ uint32_t pp = UCHAR21INCTEST(p);
+ if (pp == req_cu || pp == req_cu2) { p--; break; }
+ }
+ while (p < end_subject);
+
+#else /* 8-bit code units */
+ PCRE2_SPTR pp = p;
+ p = memchr(pp, req_cu, end_subject - pp);
+ if (p == NULL)
+ {
+ p = memchr(pp, req_cu2, end_subject - pp);
+ if (p == NULL) p = end_subject;
+ }
+#endif /* PCRE2_CODE_UNIT_WIDTH != 8 */
+ }
+
+ /* The caseful case */
+
+ else
+ {
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ do
+ {
+ if (UCHAR21INCTEST(p) == req_cu) { p--; break; }
+ }
+ while (p < end_subject);
+
+#else /* 8-bit code units */
+ p = memchr(p, req_cu, end_subject - p);
+ if (p == NULL) p = end_subject;
+#endif
+ }
+ }
+
+ /* If we can't find the required code unit, break the bumpalong loop,
+ forcing a match failure. */
+
+ if (p >= end_subject)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+
+ /* If we have found the required code unit, save the point where we
+ found it, so that we don't search again next time round the bumpalong
+ loop if the start hasn't yet passed this code unit. */
+
+ req_cu_ptr = p;
+ }
+ }
+ }
+ }
+
+ /* ------------ End of start of match optimizations ------------ */
+
+ /* Give no match if we have passed the bumpalong limit. */
+
+ if (start_match > bumpalong_limit)
+ {
+ rc = MATCH_NOMATCH;
+ break;
+ }
+
+ /* OK, we can now run the match. If "hitend" is set afterwards, remember the
+ first starting point for which a partial match was found. */
+
+ cb.start_match = (PCRE2_SIZE)(start_match - subject);
+ cb.callout_flags |= PCRE2_CALLOUT_STARTMATCH;
+
+ mb->start_used_ptr = start_match;
+ mb->last_used_ptr = start_match;
+ mb->match_call_count = 0;
+ mb->end_offset_top = 0;
+ mb->skip_arg_count = 0;
+
+ rc = match(start_match, mb->start_code, match_data->ovector,
+ match_data->oveccount, re->top_bracket, frame_size, mb);
+
+ if (mb->hitend && start_partial == NULL)
+ {
+ start_partial = mb->start_used_ptr;
+ match_partial = start_match;
+ }
+
+ switch(rc)
+ {
+ /* If MATCH_SKIP_ARG reaches this level it means that a MARK that matched
+ the SKIP's arg was not found. In this circumstance, Perl ignores the SKIP
+ entirely. The only way we can do that is to re-do the match at the same
+ point, with a flag to force SKIP with an argument to be ignored. Just
+ treating this case as NOMATCH does not work because it does not check other
+ alternatives in patterns such as A(*SKIP:A)B|AC when the subject is AC. */
+
+ case MATCH_SKIP_ARG:
+ new_start_match = start_match;
+ mb->ignore_skip_arg = mb->skip_arg_count;
+ break;
+
+ /* SKIP passes back the next starting point explicitly, but if it is no
+ greater than the match we have just done, treat it as NOMATCH. */
+
+ case MATCH_SKIP:
+ if (mb->verb_skip_ptr > start_match)
+ {
+ new_start_match = mb->verb_skip_ptr;
+ break;
+ }
+ /* Fall through */
+
+ /* NOMATCH and PRUNE advance by one character. THEN at this level acts
+ exactly like PRUNE. Unset ignore SKIP-with-argument. */
+
+ case MATCH_NOMATCH:
+ case MATCH_PRUNE:
+ case MATCH_THEN:
+ mb->ignore_skip_arg = 0;
+ new_start_match = start_match + 1;
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ ACROSSCHAR(new_start_match < end_subject, new_start_match,
+ new_start_match++);
+#endif
+ break;
+
+ /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */
+
+ case MATCH_COMMIT:
+ rc = MATCH_NOMATCH;
+ goto ENDLOOP;
+
+ /* Any other return is either a match, or some kind of error. */
+
+ default:
+ goto ENDLOOP;
+ }
+
+ /* Control reaches here for the various types of "no match at this point"
+ result. Reset the code to MATCH_NOMATCH for subsequent checking. */
+
+ rc = MATCH_NOMATCH;
+
+ /* If PCRE2_FIRSTLINE is set, the match must happen before or at the first
+ newline in the subject (though it may continue over the newline). Therefore,
+ if we have just failed to match, starting at a newline, do not continue. */
+
+ if (firstline && IS_NEWLINE(start_match)) break;
+
+ /* Advance to new matching position */
+
+ start_match = new_start_match;
+
+ /* Break the loop if the pattern is anchored or if we have passed the end of
+ the subject. */
+
+ if (anchored || start_match > end_subject) break;
+
+ /* If we have just passed a CR and we are now at a LF, and the pattern does
+ not contain any explicit matches for \r or \n, and the newline option is CRLF
+ or ANY or ANYCRLF, advance the match position by one more code unit. In
+ normal matching start_match will aways be greater than the first position at
+ this stage, but a failed *SKIP can cause a return at the same point, which is
+ why the first test exists. */
+
+ if (start_match > subject + start_offset &&
+ start_match[-1] == CHAR_CR &&
+ start_match < end_subject &&
+ *start_match == CHAR_NL &&
+ (re->flags & PCRE2_HASCRORLF) == 0 &&
+ (mb->nltype == NLTYPE_ANY ||
+ mb->nltype == NLTYPE_ANYCRLF ||
+ mb->nllen == 2))
+ start_match++;
+
+ mb->mark = NULL; /* Reset for start of next match attempt */
+ } /* End of for(;;) "bumpalong" loop */
+
+/* ==========================================================================*/
+
+/* When we reach here, one of the following stopping conditions is true:
+
+(1) The match succeeded, either completely, or partially;
+
+(2) The pattern is anchored or the match was failed after (*COMMIT);
+
+(3) We are past the end of the subject or the bumpalong limit;
+
+(4) PCRE2_FIRSTLINE is set and we have failed to match at a newline, because
+ this option requests that a match occur at or before the first newline in
+ the subject.
+
+(5) Some kind of error occurred.
+
+*/
+
+ENDLOOP:
+
+/* Release an enlarged frame vector that is on the heap. */
+
+if (mb->match_frames != mb->stack_frames)
+ mb->memctl.free(mb->match_frames, mb->memctl.memory_data);
+
+/* Fill in fields that are always returned in the match data. */
+
+match_data->code = re;
+match_data->subject = subject;
+match_data->mark = mb->mark;
+match_data->matchedby = PCRE2_MATCHEDBY_INTERPRETER;
+
+/* Handle a fully successful match. Set the return code to the number of
+captured strings, or 0 if there were too many to fit into the ovector, and then
+set the remaining returned values before returning. */
+
+if (rc == MATCH_MATCH)
+ {
+ match_data->rc = ((int)mb->end_offset_top >= 2 * match_data->oveccount)?
+ 0 : (int)mb->end_offset_top/2 + 1;
+ match_data->startchar = start_match - subject;
+ match_data->leftchar = mb->start_used_ptr - subject;
+ match_data->rightchar = ((mb->last_used_ptr > mb->end_match_ptr)?
+ mb->last_used_ptr : mb->end_match_ptr) - subject;
+ return match_data->rc;
+ }
+
+/* Control gets here if there has been a partial match, an error, or if the
+overall match attempt has failed at all permitted starting positions. Any mark
+data is in the nomatch_mark field. */
+
+match_data->mark = mb->nomatch_mark;
+
+/* For anything other than nomatch or partial match, just return the code. */
+
+if (rc != MATCH_NOMATCH && rc != PCRE2_ERROR_PARTIAL) match_data->rc = rc;
+
+/* Handle a partial match. */
+
+else if (match_partial != NULL)
+ {
+ match_data->ovector[0] = match_partial - subject;
+ match_data->ovector[1] = end_subject - subject;
+ match_data->startchar = match_partial - subject;
+ match_data->leftchar = start_partial - subject;
+ match_data->rightchar = end_subject - subject;
+ match_data->rc = PCRE2_ERROR_PARTIAL;
+ }
+
+/* Else this is the classic nomatch case. */
+
+else match_data->rc = PCRE2_ERROR_NOMATCH;
+
+return match_data->rc;
+}
+
+/* End of pcre2_match.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_match_data.c b/test/monniaux/pcre2-10.32/pcre2_match_data.c
new file mode 100644
index 00000000..b297f326
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_match_data.c
@@ -0,0 +1,147 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2017 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+
+/*************************************************
+* Create a match data block given ovector size *
+*************************************************/
+
+/* A minimum of 1 is imposed on the number of ovector pairs. */
+
+PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
+pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)
+{
+pcre2_match_data *yield;
+if (oveccount < 1) oveccount = 1;
+yield = PRIV(memctl_malloc)(
+ offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),
+ (pcre2_memctl *)gcontext);
+if (yield == NULL) return NULL;
+yield->oveccount = oveccount;
+return yield;
+}
+
+
+
+/*************************************************
+* Create a match data block using pattern data *
+*************************************************/
+
+/* If no context is supplied, use the memory allocator from the code. */
+
+PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
+pcre2_match_data_create_from_pattern(const pcre2_code *code,
+ pcre2_general_context *gcontext)
+{
+if (gcontext == NULL) gcontext = (pcre2_general_context *)code;
+return pcre2_match_data_create(((pcre2_real_code *)code)->top_bracket + 1,
+ gcontext);
+}
+
+
+
+/*************************************************
+* Free a match data block *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_match_data_free(pcre2_match_data *match_data)
+{
+if (match_data != NULL)
+ match_data->memctl.free(match_data, match_data->memctl.memory_data);
+}
+
+
+
+/*************************************************
+* Get last mark in match *
+*************************************************/
+
+PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION
+pcre2_get_mark(pcre2_match_data *match_data)
+{
+return match_data->mark;
+}
+
+
+
+/*************************************************
+* Get pointer to ovector *
+*************************************************/
+
+PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION
+pcre2_get_ovector_pointer(pcre2_match_data *match_data)
+{
+return match_data->ovector;
+}
+
+
+
+/*************************************************
+* Get number of ovector slots *
+*************************************************/
+
+PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION
+pcre2_get_ovector_count(pcre2_match_data *match_data)
+{
+return match_data->oveccount;
+}
+
+
+
+/*************************************************
+* Get starting code unit in match *
+*************************************************/
+
+PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION
+pcre2_get_startchar(pcre2_match_data *match_data)
+{
+return match_data->startchar;
+}
+
+/* End of pcre2_match_data.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_newline.c b/test/monniaux/pcre2-10.32/pcre2_newline.c
new file mode 100644
index 00000000..6e9366db
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_newline.c
@@ -0,0 +1,243 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains internal functions for testing newlines when more than
+one kind of newline is to be recognized. When a newline is found, its length is
+returned. In principle, we could implement several newline "types", each
+referring to a different set of newline characters. At present, PCRE2 supports
+only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
+and NLTYPE_ANY. The full list of Unicode newline characters is taken from
+http://unicode.org/unicode/reports/tr18/. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+
+/*************************************************
+* Check for newline at given position *
+*************************************************/
+
+/* This function is called only via the IS_NEWLINE macro, which does so only
+when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
+newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit
+pointed to by ptr is less than the end of the string.
+
+Arguments:
+ ptr pointer to possible newline
+ type the newline type
+ endptr pointer to the end of the string
+ lenptr where to return the length
+ utf TRUE if in utf mode
+
+Returns: TRUE or FALSE
+*/
+
+BOOL
+PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr,
+ uint32_t *lenptr, BOOL utf)
+{
+uint32_t c;
+
+#ifdef SUPPORT_UNICODE
+if (utf) { GETCHAR(c, ptr); } else c = *ptr;
+#else
+(void)utf;
+c = *ptr;
+#endif /* SUPPORT_UNICODE */
+
+if (type == NLTYPE_ANYCRLF) switch(c)
+ {
+ case CHAR_LF:
+ *lenptr = 1;
+ return TRUE;
+
+ case CHAR_CR:
+ *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
+ return TRUE;
+
+ default:
+ return FALSE;
+ }
+
+/* NLTYPE_ANY */
+
+else switch(c)
+ {
+#ifdef EBCDIC
+ case CHAR_NEL:
+#endif
+ case CHAR_LF:
+ case CHAR_VT:
+ case CHAR_FF:
+ *lenptr = 1;
+ return TRUE;
+
+ case CHAR_CR:
+ *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
+ return TRUE;
+
+#ifndef EBCDIC
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ case CHAR_NEL:
+ *lenptr = utf? 2 : 1;
+ return TRUE;
+
+ case 0x2028: /* LS */
+ case 0x2029: /* PS */
+ *lenptr = 3;
+ return TRUE;
+
+#else /* 16-bit or 32-bit code units */
+ case CHAR_NEL:
+ case 0x2028: /* LS */
+ case 0x2029: /* PS */
+ *lenptr = 1;
+ return TRUE;
+#endif
+#endif /* Not EBCDIC */
+
+ default:
+ return FALSE;
+ }
+}
+
+
+
+/*************************************************
+* Check for newline at previous position *
+*************************************************/
+
+/* This function is called only via the WAS_NEWLINE macro, which does so only
+when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
+newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial
+value of ptr is greater than the start of the string that is being processed.
+
+Arguments:
+ ptr pointer to possible newline
+ type the newline type
+ startptr pointer to the start of the string
+ lenptr where to return the length
+ utf TRUE if in utf mode
+
+Returns: TRUE or FALSE
+*/
+
+BOOL
+PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr,
+ uint32_t *lenptr, BOOL utf)
+{
+uint32_t c;
+ptr--;
+
+#ifdef SUPPORT_UNICODE
+if (utf)
+ {
+ BACKCHAR(ptr);
+ GETCHAR(c, ptr);
+ }
+else c = *ptr;
+#else
+(void)utf;
+c = *ptr;
+#endif /* SUPPORT_UNICODE */
+
+if (type == NLTYPE_ANYCRLF) switch(c)
+ {
+ case CHAR_LF:
+ *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
+ return TRUE;
+
+ case CHAR_CR:
+ *lenptr = 1;
+ return TRUE;
+
+ default:
+ return FALSE;
+ }
+
+/* NLTYPE_ANY */
+
+else switch(c)
+ {
+ case CHAR_LF:
+ *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
+ return TRUE;
+
+#ifdef EBCDIC
+ case CHAR_NEL:
+#endif
+ case CHAR_VT:
+ case CHAR_FF:
+ case CHAR_CR:
+ *lenptr = 1;
+ return TRUE;
+
+#ifndef EBCDIC
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ case CHAR_NEL:
+ *lenptr = utf? 2 : 1;
+ return TRUE;
+
+ case 0x2028: /* LS */
+ case 0x2029: /* PS */
+ *lenptr = 3;
+ return TRUE;
+
+#else /* 16-bit or 32-bit code units */
+ case CHAR_NEL:
+ case 0x2028: /* LS */
+ case 0x2029: /* PS */
+ *lenptr = 1;
+ return TRUE;
+#endif
+#endif /* Not EBCDIC */
+
+ default:
+ return FALSE;
+ }
+}
+
+/* End of pcre2_newline.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_ord2utf.c b/test/monniaux/pcre2-10.32/pcre2_ord2utf.c
new file mode 100644
index 00000000..14037309
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_ord2utf.c
@@ -0,0 +1,120 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This file contains a function that converts a Unicode character code point
+into a UTF string. The behaviour is different for each code unit width. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+/* If SUPPORT_UNICODE is not defined, this function will never be called.
+Supply a dummy function because some compilers do not like empty source
+modules. */
+
+#ifndef SUPPORT_UNICODE
+unsigned int
+PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
+{
+(void)(cvalue);
+(void)(buffer);
+return 0;
+}
+#else /* SUPPORT_UNICODE */
+
+
+/*************************************************
+* Convert code point to UTF *
+*************************************************/
+
+/*
+Arguments:
+ cvalue the character value
+ buffer pointer to buffer for result
+
+Returns: number of code units placed in the buffer
+*/
+
+unsigned int
+PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
+{
+/* Convert to UTF-8 */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+int i, j;
+for (i = 0; i < PRIV(utf8_table1_size); i++)
+ if ((int)cvalue <= PRIV(utf8_table1)[i]) break;
+buffer += i;
+for (j = i; j > 0; j--)
+ {
+ *buffer-- = 0x80 | (cvalue & 0x3f);
+ cvalue >>= 6;
+ }
+*buffer = PRIV(utf8_table2)[i] | cvalue;
+return i + 1;
+
+/* Convert to UTF-16 */
+
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+if (cvalue <= 0xffff)
+ {
+ *buffer = (PCRE2_UCHAR)cvalue;
+ return 1;
+ }
+cvalue -= 0x10000;
+*buffer++ = 0xd800 | (cvalue >> 10);
+*buffer = 0xdc00 | (cvalue & 0x3ff);
+return 2;
+
+/* Convert to UTF-32 */
+
+#else
+*buffer = (PCRE2_UCHAR)cvalue;
+return 1;
+#endif
+}
+#endif /* SUPPORT_UNICODE */
+
+/* End of pcre_ord2utf.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_pattern_info.c b/test/monniaux/pcre2-10.32/pcre2_pattern_info.c
new file mode 100644
index 00000000..a29f5eff
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_pattern_info.c
@@ -0,0 +1,432 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+/*************************************************
+* Return info about compiled pattern *
+*************************************************/
+
+/*
+Arguments:
+ code points to compiled code
+ what what information is required
+ where where to put the information; if NULL, return length
+
+Returns: 0 when data returned
+ > 0 when length requested
+ < 0 on error or unset value
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_pattern_info(const pcre2_code *code, uint32_t what, void *where)
+{
+const pcre2_real_code *re = (pcre2_real_code *)code;
+
+if (where == NULL) /* Requests field length */
+ {
+ switch(what)
+ {
+ case PCRE2_INFO_ALLOPTIONS:
+ case PCRE2_INFO_ARGOPTIONS:
+ case PCRE2_INFO_BACKREFMAX:
+ case PCRE2_INFO_BSR:
+ case PCRE2_INFO_CAPTURECOUNT:
+ case PCRE2_INFO_DEPTHLIMIT:
+ case PCRE2_INFO_EXTRAOPTIONS:
+ case PCRE2_INFO_FIRSTCODETYPE:
+ case PCRE2_INFO_FIRSTCODEUNIT:
+ case PCRE2_INFO_HASBACKSLASHC:
+ case PCRE2_INFO_HASCRORLF:
+ case PCRE2_INFO_HEAPLIMIT:
+ case PCRE2_INFO_JCHANGED:
+ case PCRE2_INFO_LASTCODETYPE:
+ case PCRE2_INFO_LASTCODEUNIT:
+ case PCRE2_INFO_MATCHEMPTY:
+ case PCRE2_INFO_MATCHLIMIT:
+ case PCRE2_INFO_MAXLOOKBEHIND:
+ case PCRE2_INFO_MINLENGTH:
+ case PCRE2_INFO_NAMEENTRYSIZE:
+ case PCRE2_INFO_NAMECOUNT:
+ case PCRE2_INFO_NEWLINE:
+ return sizeof(uint32_t);
+
+ case PCRE2_INFO_FIRSTBITMAP:
+ return sizeof(const uint8_t *);
+
+ case PCRE2_INFO_JITSIZE:
+ case PCRE2_INFO_SIZE:
+ case PCRE2_INFO_FRAMESIZE:
+ return sizeof(size_t);
+
+ case PCRE2_INFO_NAMETABLE:
+ return sizeof(PCRE2_SPTR);
+ }
+ }
+
+if (re == NULL) return PCRE2_ERROR_NULL;
+
+/* Check that the first field in the block is the magic number. If it is not,
+return with PCRE2_ERROR_BADMAGIC. */
+
+if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
+
+/* Check that this pattern was compiled in the correct bit mode */
+
+if ((re->flags & (PCRE2_CODE_UNIT_WIDTH/8)) == 0) return PCRE2_ERROR_BADMODE;
+
+switch(what)
+ {
+ case PCRE2_INFO_ALLOPTIONS:
+ *((uint32_t *)where) = re->overall_options;
+ break;
+
+ case PCRE2_INFO_ARGOPTIONS:
+ *((uint32_t *)where) = re->compile_options;
+ break;
+
+ case PCRE2_INFO_BACKREFMAX:
+ *((uint32_t *)where) = re->top_backref;
+ break;
+
+ case PCRE2_INFO_BSR:
+ *((uint32_t *)where) = re->bsr_convention;
+ break;
+
+ case PCRE2_INFO_CAPTURECOUNT:
+ *((uint32_t *)where) = re->top_bracket;
+ break;
+
+ case PCRE2_INFO_DEPTHLIMIT:
+ *((uint32_t *)where) = re->limit_depth;
+ if (re->limit_depth == UINT32_MAX) return PCRE2_ERROR_UNSET;
+ break;
+
+ case PCRE2_INFO_EXTRAOPTIONS:
+ *((uint32_t *)where) = re->extra_options;
+ break;
+
+ case PCRE2_INFO_FIRSTCODETYPE:
+ *((uint32_t *)where) = ((re->flags & PCRE2_FIRSTSET) != 0)? 1 :
+ ((re->flags & PCRE2_STARTLINE) != 0)? 2 : 0;
+ break;
+
+ case PCRE2_INFO_FIRSTCODEUNIT:
+ *((uint32_t *)where) = ((re->flags & PCRE2_FIRSTSET) != 0)?
+ re->first_codeunit : 0;
+ break;
+
+ case PCRE2_INFO_FIRSTBITMAP:
+ *((const uint8_t **)where) = ((re->flags & PCRE2_FIRSTMAPSET) != 0)?
+ &(re->start_bitmap[0]) : NULL;
+ break;
+
+ case PCRE2_INFO_FRAMESIZE:
+ *((size_t *)where) = offsetof(heapframe, ovector) +
+ re->top_bracket * 2 * sizeof(PCRE2_SIZE);
+ break;
+
+ case PCRE2_INFO_HASBACKSLASHC:
+ *((uint32_t *)where) = (re->flags & PCRE2_HASBKC) != 0;
+ break;
+
+ case PCRE2_INFO_HASCRORLF:
+ *((uint32_t *)where) = (re->flags & PCRE2_HASCRORLF) != 0;
+ break;
+
+ case PCRE2_INFO_HEAPLIMIT:
+ *((uint32_t *)where) = re->limit_heap;
+ if (re->limit_heap == UINT32_MAX) return PCRE2_ERROR_UNSET;
+ break;
+
+ case PCRE2_INFO_JCHANGED:
+ *((uint32_t *)where) = (re->flags & PCRE2_JCHANGED) != 0;
+ break;
+
+ case PCRE2_INFO_JITSIZE:
+#ifdef SUPPORT_JIT
+ *((size_t *)where) = (re->executable_jit != NULL)?
+ PRIV(jit_get_size)(re->executable_jit) : 0;
+#else
+ *((size_t *)where) = 0;
+#endif
+ break;
+
+ case PCRE2_INFO_LASTCODETYPE:
+ *((uint32_t *)where) = ((re->flags & PCRE2_LASTSET) != 0)? 1 : 0;
+ break;
+
+ case PCRE2_INFO_LASTCODEUNIT:
+ *((uint32_t *)where) = ((re->flags & PCRE2_LASTSET) != 0)?
+ re->last_codeunit : 0;
+ break;
+
+ case PCRE2_INFO_MATCHEMPTY:
+ *((uint32_t *)where) = (re->flags & PCRE2_MATCH_EMPTY) != 0;
+ break;
+
+ case PCRE2_INFO_MATCHLIMIT:
+ *((uint32_t *)where) = re->limit_match;
+ if (re->limit_match == UINT32_MAX) return PCRE2_ERROR_UNSET;
+ break;
+
+ case PCRE2_INFO_MAXLOOKBEHIND:
+ *((uint32_t *)where) = re->max_lookbehind;
+ break;
+
+ case PCRE2_INFO_MINLENGTH:
+ *((uint32_t *)where) = re->minlength;
+ break;
+
+ case PCRE2_INFO_NAMEENTRYSIZE:
+ *((uint32_t *)where) = re->name_entry_size;
+ break;
+
+ case PCRE2_INFO_NAMECOUNT:
+ *((uint32_t *)where) = re->name_count;
+ break;
+
+ case PCRE2_INFO_NAMETABLE:
+ *((PCRE2_SPTR *)where) = (PCRE2_SPTR)((char *)re + sizeof(pcre2_real_code));
+ break;
+
+ case PCRE2_INFO_NEWLINE:
+ *((uint32_t *)where) = re->newline_convention;
+ break;
+
+ case PCRE2_INFO_SIZE:
+ *((size_t *)where) = re->blocksize;
+ break;
+
+ default: return PCRE2_ERROR_BADOPTION;
+ }
+
+return 0;
+}
+
+
+
+/*************************************************
+* Callout enumerator *
+*************************************************/
+
+/*
+Arguments:
+ code points to compiled code
+ callback function called for each callout block
+ callout_data user data passed to the callback
+
+Returns: 0 when successfully completed
+ < 0 on local error
+ != 0 for callback error
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_callout_enumerate(const pcre2_code *code,
+ int (*callback)(pcre2_callout_enumerate_block *, void *), void *callout_data)
+{
+pcre2_real_code *re = (pcre2_real_code *)code;
+pcre2_callout_enumerate_block cb;
+PCRE2_SPTR cc;
+#ifdef SUPPORT_UNICODE
+BOOL utf;
+#endif
+
+if (re == NULL) return PCRE2_ERROR_NULL;
+
+#ifdef SUPPORT_UNICODE
+utf = (re->overall_options & PCRE2_UTF) != 0;
+#endif
+
+/* Check that the first field in the block is the magic number. If it is not,
+return with PCRE2_ERROR_BADMAGIC. */
+
+if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
+
+/* Check that this pattern was compiled in the correct bit mode */
+
+if ((re->flags & (PCRE2_CODE_UNIT_WIDTH/8)) == 0) return PCRE2_ERROR_BADMODE;
+
+cb.version = 0;
+cc = (PCRE2_SPTR)((uint8_t *)re + sizeof(pcre2_real_code))
+ + re->name_count * re->name_entry_size;
+
+while (TRUE)
+ {
+ int rc;
+ switch (*cc)
+ {
+ case OP_END:
+ return 0;
+
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_EXACT:
+ case OP_POSSTAR:
+ case OP_POSPLUS:
+ case OP_POSQUERY:
+ case OP_POSUPTO:
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_EXACTI:
+ case OP_POSSTARI:
+ case OP_POSPLUSI:
+ case OP_POSQUERYI:
+ case OP_POSUPTOI:
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTEXACT:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSUPTO:
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTEXACTI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSUPTOI:
+ cc += PRIV(OP_lengths)[*cc];
+#ifdef SUPPORT_UNICODE
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEEXACT:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEPOSQUERY:
+ case OP_TYPEPOSUPTO:
+ cc += PRIV(OP_lengths)[*cc];
+#ifdef SUPPORT_UNICODE
+ if (cc[-1] == OP_PROP || cc[-1] == OP_NOTPROP) cc += 2;
+#endif
+ break;
+
+#if defined SUPPORT_UNICODE || PCRE2_CODE_UNIT_WIDTH != 8
+ case OP_XCLASS:
+ cc += GET(cc, 1);
+ break;
+#endif
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ cc += PRIV(OP_lengths)[*cc] + cc[1];
+ break;
+
+ case OP_CALLOUT:
+ cb.pattern_position = GET(cc, 1);
+ cb.next_item_length = GET(cc, 1 + LINK_SIZE);
+ cb.callout_number = cc[1 + 2*LINK_SIZE];
+ cb.callout_string_offset = 0;
+ cb.callout_string_length = 0;
+ cb.callout_string = NULL;
+ rc = callback(&cb, callout_data);
+ if (rc != 0) return rc;
+ cc += PRIV(OP_lengths)[*cc];
+ break;
+
+ case OP_CALLOUT_STR:
+ cb.pattern_position = GET(cc, 1);
+ cb.next_item_length = GET(cc, 1 + LINK_SIZE);
+ cb.callout_number = 0;
+ cb.callout_string_offset = GET(cc, 1 + 3*LINK_SIZE);
+ cb.callout_string_length =
+ GET(cc, 1 + 2*LINK_SIZE) - (1 + 4*LINK_SIZE) - 2;
+ cb.callout_string = cc + (1 + 4*LINK_SIZE) + 1;
+ rc = callback(&cb, callout_data);
+ if (rc != 0) return rc;
+ cc += GET(cc, 1 + 2*LINK_SIZE);
+ break;
+
+ default:
+ cc += PRIV(OP_lengths)[*cc];
+ break;
+ }
+ }
+}
+
+/* End of pcre2_pattern_info.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_printint.c b/test/monniaux/pcre2-10.32/pcre2_printint.c
new file mode 100644
index 00000000..bd10c6b1
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_printint.c
@@ -0,0 +1,832 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains a PCRE private debugging function for printing out the
+internal form of a compiled regular expression, along with some supporting
+local functions. This source file is #included in pcre2test.c at each supported
+code unit width, with PCRE2_SUFFIX set appropriately, just like the functions
+that comprise the library. It can also optionally be included in
+pcre2_compile.c for detailed debugging in error situations. */
+
+
+/* Tables of operator names. The same 8-bit table is used for all code unit
+widths, so it must be defined only once. The list itself is defined in
+pcre2_internal.h, which is #included by pcre2test before this file. */
+
+#ifndef OP_LISTS_DEFINED
+static const char *OP_names[] = { OP_NAME_LIST };
+#define OP_LISTS_DEFINED
+#endif
+
+/* The functions and tables herein must all have mode-dependent names. */
+
+#define OP_lengths PCRE2_SUFFIX(OP_lengths_)
+#define get_ucpname PCRE2_SUFFIX(get_ucpname_)
+#define pcre2_printint PCRE2_SUFFIX(pcre2_printint_)
+#define print_char PCRE2_SUFFIX(print_char_)
+#define print_custring PCRE2_SUFFIX(print_custring_)
+#define print_custring_bylen PCRE2_SUFFIX(print_custring_bylen_)
+#define print_prop PCRE2_SUFFIX(print_prop_)
+
+/* Table of sizes for the fixed-length opcodes. It's defined in a macro so that
+the definition is next to the definition of the opcodes in pcre2_internal.h.
+The contents of the table are, however, mode-dependent. */
+
+static const uint8_t OP_lengths[] = { OP_LENGTHS };
+
+
+
+/*************************************************
+* Print one character from a string *
+*************************************************/
+
+/* In UTF mode the character may occupy more than one code unit.
+
+Arguments:
+ f file to write to
+ ptr pointer to first code unit of the character
+ utf TRUE if string is UTF (will be FALSE if UTF is not supported)
+
+Returns: number of additional code units used
+*/
+
+static unsigned int
+print_char(FILE *f, PCRE2_SPTR ptr, BOOL utf)
+{
+uint32_t c = *ptr;
+BOOL one_code_unit = !utf;
+
+/* If UTF is supported and requested, check for a valid single code unit. */
+
+#ifdef SUPPORT_UNICODE
+if (utf)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ one_code_unit = c < 0x80;
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ one_code_unit = (c & 0xfc00) != 0xd800;
+#else
+ one_code_unit = (c & 0xfffff800u) != 0xd800u;
+#endif /* CODE_UNIT_WIDTH */
+ }
+#endif /* SUPPORT_UNICODE */
+
+/* Handle a valid one-code-unit character at any width. */
+
+if (one_code_unit)
+ {
+ if (PRINTABLE(c)) fprintf(f, "%c", (char)c);
+ else if (c < 0x80) fprintf(f, "\\x%02x", c);
+ else fprintf(f, "\\x{%02x}", c);
+ return 0;
+ }
+
+/* Code for invalid UTF code units and multi-unit UTF characters is different
+for each width. If UTF is not supported, control should never get here, but we
+need a return statement to keep the compiler happy. */
+
+#ifndef SUPPORT_UNICODE
+return 0;
+#else
+
+/* Malformed UTF-8 should occur only if the sanity check has been turned off.
+Rather than swallow random bytes, just stop if we hit a bad one. Print it with
+\X instead of \x as an indication. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+if ((c & 0xc0) != 0xc0)
+ {
+ fprintf(f, "\\X{%x}", c); /* Invalid starting byte */
+ return 0;
+ }
+else
+ {
+ int i;
+ int a = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes */
+ int s = 6*a;
+ c = (c & PRIV(utf8_table3)[a]) << s;
+ for (i = 1; i <= a; i++)
+ {
+ if ((ptr[i] & 0xc0) != 0x80)
+ {
+ fprintf(f, "\\X{%x}", c); /* Invalid secondary byte */
+ return i - 1;
+ }
+ s -= 6;
+ c |= (ptr[i] & 0x3f) << s;
+ }
+ fprintf(f, "\\x{%x}", c);
+ return a;
+}
+#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
+
+/* UTF-16: rather than swallow a low surrogate, just stop if we hit a bad one.
+Print it with \X instead of \x as an indication. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 16
+if ((ptr[1] & 0xfc00) != 0xdc00)
+ {
+ fprintf(f, "\\X{%x}", c);
+ return 0;
+ }
+c = (((c & 0x3ff) << 10) | (ptr[1] & 0x3ff)) + 0x10000;
+fprintf(f, "\\x{%x}", c);
+return 1;
+#endif /* PCRE2_CODE_UNIT_WIDTH == 16 */
+
+/* For UTF-32 we get here only for a malformed code unit, which should only
+occur if the sanity check has been turned off. Print it with \X instead of \x
+as an indication. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+fprintf(f, "\\X{%x}", c);
+return 0;
+#endif /* PCRE2_CODE_UNIT_WIDTH == 32 */
+#endif /* SUPPORT_UNICODE */
+}
+
+
+
+/*************************************************
+* Print string as a list of code units *
+*************************************************/
+
+/* These take no account of UTF as they always print each individual code unit.
+The string is zero-terminated for print_custring(); the length is given for
+print_custring_bylen().
+
+Arguments:
+ f file to write to
+ ptr point to the string
+ len length for print_custring_bylen()
+
+Returns: nothing
+*/
+
+static void
+print_custring(FILE *f, PCRE2_SPTR ptr)
+{
+while (*ptr != '\0')
+ {
+ uint32_t c = *ptr++;
+ if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x{%x}", c);
+ }
+}
+
+static void
+print_custring_bylen(FILE *f, PCRE2_SPTR ptr, PCRE2_UCHAR len)
+{
+for (; len > 0; len--)
+ {
+ uint32_t c = *ptr++;
+ if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x{%x}", c);
+ }
+}
+
+
+
+/*************************************************
+* Find Unicode property name *
+*************************************************/
+
+/* When there is no UTF/UCP support, the table of names does not exist. This
+function should not be called in such configurations, because a pattern that
+tries to use Unicode properties won't compile. Rather than put lots of #ifdefs
+into the main code, however, we just put one into this function. */
+
+static const char *
+get_ucpname(unsigned int ptype, unsigned int pvalue)
+{
+#ifdef SUPPORT_UNICODE
+int i;
+for (i = PRIV(utt_size) - 1; i >= 0; i--)
+ {
+ if (ptype == PRIV(utt)[i].type && pvalue == PRIV(utt)[i].value) break;
+ }
+return (i >= 0)? PRIV(utt_names) + PRIV(utt)[i].name_offset : "??";
+#else /* No UTF support */
+(void)ptype;
+(void)pvalue;
+return "??";
+#endif /* SUPPORT_UNICODE */
+}
+
+
+
+/*************************************************
+* Print Unicode property value *
+*************************************************/
+
+/* "Normal" properties can be printed from tables. The PT_CLIST property is a
+pseudo-property that contains a pointer to a list of case-equivalent
+characters.
+
+Arguments:
+ f file to write to
+ code pointer in the compiled code
+ before text to print before
+ after text to print after
+
+Returns: nothing
+*/
+
+static void
+print_prop(FILE *f, PCRE2_SPTR code, const char *before, const char *after)
+{
+if (code[1] != PT_CLIST)
+ {
+ fprintf(f, "%s%s %s%s", before, OP_names[*code], get_ucpname(code[1],
+ code[2]), after);
+ }
+else
+ {
+ const char *not = (*code == OP_PROP)? "" : "not ";
+ const uint32_t *p = PRIV(ucd_caseless_sets) + code[2];
+ fprintf (f, "%s%sclist", before, not);
+ while (*p < NOTACHAR) fprintf(f, " %04x", *p++);
+ fprintf(f, "%s", after);
+ }
+}
+
+
+
+/*************************************************
+* Print compiled pattern *
+*************************************************/
+
+/* The print_lengths flag controls whether offsets and lengths of items are
+printed. Lenths can be turned off from pcre2test so that automatic tests on
+bytecode can be written that do not depend on the value of LINK_SIZE.
+
+Arguments:
+ re a compiled pattern
+ f the file to write to
+ print_lengths show various lengths
+
+Returns: nothing
+*/
+
+static void
+pcre2_printint(pcre2_code *re, FILE *f, BOOL print_lengths)
+{
+PCRE2_SPTR codestart, nametable, code;
+uint32_t nesize = re->name_entry_size;
+BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
+
+nametable = (PCRE2_SPTR)((uint8_t *)re + sizeof(pcre2_real_code));
+code = codestart = nametable + re->name_count * re->name_entry_size;
+
+for(;;)
+ {
+ PCRE2_SPTR ccode;
+ uint32_t c;
+ int i;
+ const char *flag = " ";
+ unsigned int extra = 0;
+
+ if (print_lengths)
+ fprintf(f, "%3d ", (int)(code - codestart));
+ else
+ fprintf(f, " ");
+
+ switch(*code)
+ {
+/* ========================================================================== */
+ /* These cases are never obeyed. This is a fudge that causes a compile-
+ time error if the vectors OP_names or OP_lengths, which are indexed
+ by opcode, are not the correct length. It seems to be the only way to do
+ such a check at compile time, as the sizeof() operator does not work in
+ the C preprocessor. */
+
+ case OP_TABLE_LENGTH:
+ case OP_TABLE_LENGTH +
+ ((sizeof(OP_names)/sizeof(const char *) == OP_TABLE_LENGTH) &&
+ (sizeof(OP_lengths) == OP_TABLE_LENGTH)):
+ return;
+/* ========================================================================== */
+
+ case OP_END:
+ fprintf(f, " %s\n", OP_names[*code]);
+ fprintf(f, "------------------------------------------------------------------\n");
+ return;
+
+ case OP_CHAR:
+ fprintf(f, " ");
+ do
+ {
+ code++;
+ code += 1 + print_char(f, code, utf);
+ }
+ while (*code == OP_CHAR);
+ fprintf(f, "\n");
+ continue;
+
+ case OP_CHARI:
+ fprintf(f, " /i ");
+ do
+ {
+ code++;
+ code += 1 + print_char(f, code, utf);
+ }
+ while (*code == OP_CHARI);
+ fprintf(f, "\n");
+ continue;
+
+ case OP_CBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRA:
+ case OP_SCBRAPOS:
+ if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
+ else fprintf(f, " ");
+ fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
+ break;
+
+ case OP_BRA:
+ case OP_BRAPOS:
+ case OP_SBRA:
+ case OP_SBRAPOS:
+ case OP_KETRMAX:
+ case OP_KETRMIN:
+ case OP_KETRPOS:
+ case OP_ALT:
+ case OP_KET:
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ case OP_ONCE:
+ case OP_COND:
+ case OP_SCOND:
+ case OP_REVERSE:
+ if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
+ else fprintf(f, " ");
+ fprintf(f, "%s", OP_names[*code]);
+ break;
+
+ case OP_CLOSE:
+ fprintf(f, " %s %d", OP_names[*code], GET2(code, 1));
+ break;
+
+ case OP_CREF:
+ fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
+ break;
+
+ case OP_DNCREF:
+ {
+ PCRE2_SPTR entry = nametable + (GET2(code, 1) * nesize) + IMM2_SIZE;
+ fprintf(f, " %s Cond ref <", flag);
+ print_custring(f, entry);
+ fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE));
+ }
+ break;
+
+ case OP_RREF:
+ c = GET2(code, 1);
+ if (c == RREF_ANY)
+ fprintf(f, " Cond recurse any");
+ else
+ fprintf(f, " Cond recurse %d", c);
+ break;
+
+ case OP_DNRREF:
+ {
+ PCRE2_SPTR entry = nametable + (GET2(code, 1) * nesize) + IMM2_SIZE;
+ fprintf(f, " %s Cond recurse <", flag);
+ print_custring(f, entry);
+ fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE));
+ }
+ break;
+
+ case OP_FALSE:
+ fprintf(f, " Cond false");
+ break;
+
+ case OP_TRUE:
+ fprintf(f, " Cond true");
+ break;
+
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_POSSTARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSPLUSI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_POSQUERYI:
+ flag = "/i";
+ /* Fall through */
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_POSSTAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSPLUS:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_POSQUERY:
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEPOSPLUS:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSQUERY:
+ fprintf(f, " %s ", flag);
+
+ if (*code >= OP_TYPESTAR)
+ {
+ if (code[1] == OP_PROP || code[1] == OP_NOTPROP)
+ {
+ print_prop(f, code + 1, "", " ");
+ extra = 2;
+ }
+ else fprintf(f, "%s", OP_names[code[1]]);
+ }
+ else extra = print_char(f, code+1, utf);
+ fprintf(f, "%s", OP_names[*code]);
+ break;
+
+ case OP_EXACTI:
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_POSUPTOI:
+ flag = "/i";
+ /* Fall through */
+ case OP_EXACT:
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_POSUPTO:
+ fprintf(f, " %s ", flag);
+ extra = print_char(f, code + 1 + IMM2_SIZE, utf);
+ fprintf(f, "{");
+ if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,");
+ fprintf(f, "%d}", GET2(code,1));
+ if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?");
+ else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+");
+ break;
+
+ case OP_TYPEEXACT:
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEPOSUPTO:
+ if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
+ {
+ print_prop(f, code + IMM2_SIZE + 1, " ", " ");
+ extra = 2;
+ }
+ else fprintf(f, " %s", OP_names[code[1 + IMM2_SIZE]]);
+ fprintf(f, "{");
+ if (*code != OP_TYPEEXACT) fprintf(f, "0,");
+ fprintf(f, "%d}", GET2(code,1));
+ if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
+ else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
+ break;
+
+ case OP_NOTI:
+ flag = "/i";
+ /* Fall through */
+ case OP_NOT:
+ fprintf(f, " %s [^", flag);
+ extra = print_char(f, code + 1, utf);
+ fprintf(f, "]");
+ break;
+
+ case OP_NOTSTARI:
+ case OP_NOTMINSTARI:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUSI:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTQUERYI:
+ case OP_NOTMINQUERYI:
+ case OP_NOTPOSQUERYI:
+ flag = "/i";
+ /* Fall through */
+
+ case OP_NOTSTAR:
+ case OP_NOTMINSTAR:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPLUS:
+ case OP_NOTMINPLUS:
+ case OP_NOTPOSPLUS:
+ case OP_NOTQUERY:
+ case OP_NOTMINQUERY:
+ case OP_NOTPOSQUERY:
+ fprintf(f, " %s [^", flag);
+ extra = print_char(f, code + 1, utf);
+ fprintf(f, "]%s", OP_names[*code]);
+ break;
+
+ case OP_NOTEXACTI:
+ case OP_NOTUPTOI:
+ case OP_NOTMINUPTOI:
+ case OP_NOTPOSUPTOI:
+ flag = "/i";
+ /* Fall through */
+
+ case OP_NOTEXACT:
+ case OP_NOTUPTO:
+ case OP_NOTMINUPTO:
+ case OP_NOTPOSUPTO:
+ fprintf(f, " %s [^", flag);
+ extra = print_char(f, code + 1 + IMM2_SIZE, utf);
+ fprintf(f, "]{");
+ if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
+ fprintf(f, "%d}", GET2(code,1));
+ if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
+ else
+ if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
+ break;
+
+ case OP_RECURSE:
+ if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
+ else fprintf(f, " ");
+ fprintf(f, "%s", OP_names[*code]);
+ break;
+
+ case OP_REFI:
+ flag = "/i";
+ /* Fall through */
+ case OP_REF:
+ fprintf(f, " %s \\%d", flag, GET2(code,1));
+ ccode = code + OP_lengths[*code];
+ goto CLASS_REF_REPEAT;
+
+ case OP_DNREFI:
+ flag = "/i";
+ /* Fall through */
+ case OP_DNREF:
+ {
+ PCRE2_SPTR entry = nametable + (GET2(code, 1) * nesize) + IMM2_SIZE;
+ fprintf(f, " %s \\k<", flag);
+ print_custring(f, entry);
+ fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE));
+ }
+ ccode = code + OP_lengths[*code];
+ goto CLASS_REF_REPEAT;
+
+ case OP_CALLOUT:
+ fprintf(f, " %s %d %d %d", OP_names[*code], code[1 + 2*LINK_SIZE],
+ GET(code, 1), GET(code, 1 + LINK_SIZE));
+ break;
+
+ case OP_CALLOUT_STR:
+ c = code[1 + 4*LINK_SIZE];
+ fprintf(f, " %s %c", OP_names[*code], c);
+ extra = GET(code, 1 + 2*LINK_SIZE);
+ print_custring_bylen(f, code + 2 + 4*LINK_SIZE, extra - 3 - 4*LINK_SIZE);
+ for (i = 0; PRIV(callout_start_delims)[i] != 0; i++)
+ if (c == PRIV(callout_start_delims)[i])
+ {
+ c = PRIV(callout_end_delims)[i];
+ break;
+ }
+ fprintf(f, "%c %d %d %d", c, GET(code, 1 + 3*LINK_SIZE), GET(code, 1),
+ GET(code, 1 + LINK_SIZE));
+ break;
+
+ case OP_PROP:
+ case OP_NOTPROP:
+ print_prop(f, code, " ", "");
+ break;
+
+ /* OP_XCLASS cannot occur in 8-bit, non-UTF mode. However, there's no harm
+ in having this code always here, and it makes it less messy without all
+ those #ifdefs. */
+
+ case OP_CLASS:
+ case OP_NCLASS:
+ case OP_XCLASS:
+ {
+ unsigned int min, max;
+ BOOL printmap;
+ BOOL invertmap = FALSE;
+ uint8_t *map;
+ uint8_t inverted_map[32];
+
+ fprintf(f, " [");
+
+ if (*code == OP_XCLASS)
+ {
+ extra = GET(code, 1);
+ ccode = code + LINK_SIZE + 1;
+ printmap = (*ccode & XCL_MAP) != 0;
+ if ((*ccode & XCL_NOT) != 0)
+ {
+ invertmap = (*ccode & XCL_HASPROP) == 0;
+ fprintf(f, "^");
+ }
+ ccode++;
+ }
+ else
+ {
+ printmap = TRUE;
+ ccode = code + 1;
+ }
+
+ /* Print a bit map */
+
+ if (printmap)
+ {
+ map = (uint8_t *)ccode;
+ if (invertmap)
+ {
+ for (i = 0; i < 32; i++) inverted_map[i] = ~map[i];
+ map = inverted_map;
+ }
+
+ for (i = 0; i < 256; i++)
+ {
+ if ((map[i/8] & (1 << (i&7))) != 0)
+ {
+ int j;
+ for (j = i+1; j < 256; j++)
+ if ((map[j/8] & (1 << (j&7))) == 0) break;
+ if (i == '-' || i == ']') fprintf(f, "\\");
+ if (PRINTABLE(i)) fprintf(f, "%c", i);
+ else fprintf(f, "\\x%02x", i);
+ if (--j > i)
+ {
+ if (j != i + 1) fprintf(f, "-");
+ if (j == '-' || j == ']') fprintf(f, "\\");
+ if (PRINTABLE(j)) fprintf(f, "%c", j);
+ else fprintf(f, "\\x%02x", j);
+ }
+ i = j;
+ }
+ }
+ ccode += 32 / sizeof(PCRE2_UCHAR);
+ }
+
+ /* For an XCLASS there is always some additional data */
+
+ if (*code == OP_XCLASS)
+ {
+ PCRE2_UCHAR ch;
+ while ((ch = *ccode++) != XCL_END)
+ {
+ BOOL not = FALSE;
+ const char *notch = "";
+
+ switch(ch)
+ {
+ case XCL_NOTPROP:
+ not = TRUE;
+ notch = "^";
+ /* Fall through */
+
+ case XCL_PROP:
+ {
+ unsigned int ptype = *ccode++;
+ unsigned int pvalue = *ccode++;
+
+ switch(ptype)
+ {
+ case PT_PXGRAPH:
+ fprintf(f, "[:%sgraph:]", notch);
+ break;
+
+ case PT_PXPRINT:
+ fprintf(f, "[:%sprint:]", notch);
+ break;
+
+ case PT_PXPUNCT:
+ fprintf(f, "[:%spunct:]", notch);
+ break;
+
+ default:
+ fprintf(f, "\\%c{%s}", (not? 'P':'p'),
+ get_ucpname(ptype, pvalue));
+ break;
+ }
+ }
+ break;
+
+ default:
+ ccode += 1 + print_char(f, ccode, utf);
+ if (ch == XCL_RANGE)
+ {
+ fprintf(f, "-");
+ ccode += 1 + print_char(f, ccode, utf);
+ }
+ break;
+ }
+ }
+ }
+
+ /* Indicate a non-UTF class which was created by negation */
+
+ fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
+
+ /* Handle repeats after a class or a back reference */
+
+ CLASS_REF_REPEAT:
+ switch(*ccode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSPLUS:
+ case OP_CRPOSQUERY:
+ fprintf(f, "%s", OP_names[*ccode]);
+ extra += OP_lengths[*ccode];
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ min = GET2(ccode,1);
+ max = GET2(ccode,1 + IMM2_SIZE);
+ if (max == 0) fprintf(f, "{%u,}", min);
+ else fprintf(f, "{%u,%u}", min, max);
+ if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
+ else if (*ccode == OP_CRPOSRANGE) fprintf(f, "+");
+ extra += OP_lengths[*ccode];
+ break;
+
+ /* Do nothing if it's not a repeat; this code stops picky compilers
+ warning about the lack of a default code path. */
+
+ default:
+ break;
+ }
+ }
+ break;
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ fprintf(f, " %s ", OP_names[*code]);
+ print_custring_bylen(f, code + 2, code[1]);
+ extra += code[1];
+ break;
+
+ case OP_THEN:
+ fprintf(f, " %s", OP_names[*code]);
+ break;
+
+ case OP_CIRCM:
+ case OP_DOLLM:
+ flag = "/m";
+ /* Fall through */
+
+ /* Anything else is just an item with no data, but possibly a flag. */
+
+ default:
+ fprintf(f, " %s %s", flag, OP_names[*code]);
+ break;
+ }
+
+ code += OP_lengths[*code] + extra;
+ fprintf(f, "\n");
+ }
+}
+
+/* End of pcre2_printint.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_serialize.c b/test/monniaux/pcre2-10.32/pcre2_serialize.c
new file mode 100644
index 00000000..cec1a035
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_serialize.c
@@ -0,0 +1,286 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains functions for serializing and deserializing
+a sequence of compiled codes. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+#include "pcre2_internal.h"
+
+/* Magic number to provide a small check against being handed junk. */
+
+#define SERIALIZED_DATA_MAGIC 0x50523253u
+
+/* Deserialization is limited to the current PCRE version and
+character width. */
+
+#define SERIALIZED_DATA_VERSION \
+ ((PCRE2_MAJOR) | ((PCRE2_MINOR) << 16))
+
+#define SERIALIZED_DATA_CONFIG \
+ (sizeof(PCRE2_UCHAR) | ((sizeof(void*)) << 8) | ((sizeof(PCRE2_SIZE)) << 16))
+
+
+
+/*************************************************
+* Serialize compiled patterns *
+*************************************************/
+
+PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
+pcre2_serialize_encode(const pcre2_code **codes, int32_t number_of_codes,
+ uint8_t **serialized_bytes, PCRE2_SIZE *serialized_size,
+ pcre2_general_context *gcontext)
+{
+uint8_t *bytes;
+uint8_t *dst_bytes;
+int32_t i;
+PCRE2_SIZE total_size;
+const pcre2_real_code *re;
+const uint8_t *tables;
+pcre2_serialized_data *data;
+
+const pcre2_memctl *memctl = (gcontext != NULL) ?
+ &gcontext->memctl : &PRIV(default_compile_context).memctl;
+
+if (codes == NULL || serialized_bytes == NULL || serialized_size == NULL)
+ return PCRE2_ERROR_NULL;
+
+if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
+
+/* Compute total size. */
+total_size = sizeof(pcre2_serialized_data) + tables_length;
+tables = NULL;
+
+for (i = 0; i < number_of_codes; i++)
+ {
+ if (codes[i] == NULL) return PCRE2_ERROR_NULL;
+ re = (const pcre2_real_code *)(codes[i]);
+ if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
+ if (tables == NULL)
+ tables = re->tables;
+ else if (tables != re->tables)
+ return PCRE2_ERROR_MIXEDTABLES;
+ total_size += re->blocksize;
+ }
+
+/* Initialize the byte stream. */
+bytes = memctl->malloc(total_size + sizeof(pcre2_memctl), memctl->memory_data);
+if (bytes == NULL) return PCRE2_ERROR_NOMEMORY;
+
+/* The controller is stored as a hidden parameter. */
+memcpy(bytes, memctl, sizeof(pcre2_memctl));
+bytes += sizeof(pcre2_memctl);
+
+data = (pcre2_serialized_data *)bytes;
+data->magic = SERIALIZED_DATA_MAGIC;
+data->version = SERIALIZED_DATA_VERSION;
+data->config = SERIALIZED_DATA_CONFIG;
+data->number_of_codes = number_of_codes;
+
+/* Copy all compiled code data. */
+dst_bytes = bytes + sizeof(pcre2_serialized_data);
+memcpy(dst_bytes, tables, tables_length);
+dst_bytes += tables_length;
+
+for (i = 0; i < number_of_codes; i++)
+ {
+ re = (const pcre2_real_code *)(codes[i]);
+ (void)memcpy(dst_bytes, (char *)re, re->blocksize);
+
+ /* Certain fields in the compiled code block are re-set during
+ deserialization. In order to ensure that the serialized data stream is always
+ the same for the same pattern, set them to zero here. We can't assume the
+ copy of the pattern is correctly aligned for accessing the fields as part of
+ a structure. Note the use of sizeof(void *) in the second of these, to
+ specify the size of a pointer. If sizeof(uint8_t *) is used (tables is a
+ pointer to uint8_t), gcc gives a warning because the first argument is also a
+ pointer to uint8_t. Casting the first argument to (void *) can stop this, but
+ it didn't stop Coverity giving the same complaint. */
+
+ (void)memset(dst_bytes + offsetof(pcre2_real_code, memctl), 0,
+ sizeof(pcre2_memctl));
+ (void)memset(dst_bytes + offsetof(pcre2_real_code, tables), 0,
+ sizeof(void *));
+ (void)memset(dst_bytes + offsetof(pcre2_real_code, executable_jit), 0,
+ sizeof(void *));
+
+ dst_bytes += re->blocksize;
+ }
+
+*serialized_bytes = bytes;
+*serialized_size = total_size;
+return number_of_codes;
+}
+
+
+/*************************************************
+* Deserialize compiled patterns *
+*************************************************/
+
+PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
+pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,
+ const uint8_t *bytes, pcre2_general_context *gcontext)
+{
+const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
+const pcre2_memctl *memctl = (gcontext != NULL) ?
+ &gcontext->memctl : &PRIV(default_compile_context).memctl;
+
+const uint8_t *src_bytes;
+pcre2_real_code *dst_re;
+uint8_t *tables;
+int32_t i, j;
+
+/* Sanity checks. */
+
+if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;
+if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
+if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;
+if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
+if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
+if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
+
+if (number_of_codes > data->number_of_codes)
+ number_of_codes = data->number_of_codes;
+
+src_bytes = bytes + sizeof(pcre2_serialized_data);
+
+/* Decode tables. The reference count for the tables is stored immediately
+following them. */
+
+tables = memctl->malloc(tables_length + sizeof(PCRE2_SIZE), memctl->memory_data);
+if (tables == NULL) return PCRE2_ERROR_NOMEMORY;
+
+memcpy(tables, src_bytes, tables_length);
+*(PCRE2_SIZE *)(tables + tables_length) = number_of_codes;
+src_bytes += tables_length;
+
+/* Decode the byte stream. We must not try to read the size from the compiled
+code block in the stream, because it might be unaligned, which causes errors on
+hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type
+of the blocksize field is given its own name to ensure that it is the same here
+as in the block. */
+
+for (i = 0; i < number_of_codes; i++)
+ {
+ CODE_BLOCKSIZE_TYPE blocksize;
+ memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),
+ sizeof(CODE_BLOCKSIZE_TYPE));
+ if (blocksize <= sizeof(pcre2_real_code))
+ return PCRE2_ERROR_BADSERIALIZEDDATA;
+
+ /* The allocator provided by gcontext replaces the original one. */
+
+ dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,
+ (pcre2_memctl *)gcontext);
+ if (dst_re == NULL)
+ {
+ memctl->free(tables, memctl->memory_data);
+ for (j = 0; j < i; j++)
+ {
+ memctl->free(codes[j], memctl->memory_data);
+ codes[j] = NULL;
+ }
+ return PCRE2_ERROR_NOMEMORY;
+ }
+
+ /* The new allocator must be preserved. */
+
+ memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),
+ src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));
+ if (dst_re->magic_number != MAGIC_NUMBER ||
+ dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||
+ dst_re->name_count > MAX_NAME_COUNT)
+ {
+ memctl->free(dst_re, memctl->memory_data);
+ return PCRE2_ERROR_BADSERIALIZEDDATA;
+ }
+
+ /* At the moment only one table is supported. */
+
+ dst_re->tables = tables;
+ dst_re->executable_jit = NULL;
+ dst_re->flags |= PCRE2_DEREF_TABLES;
+
+ codes[i] = dst_re;
+ src_bytes += blocksize;
+ }
+
+return number_of_codes;
+}
+
+
+/*************************************************
+* Get the number of serialized patterns *
+*************************************************/
+
+PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
+pcre2_serialize_get_number_of_codes(const uint8_t *bytes)
+{
+const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
+
+if (data == NULL) return PCRE2_ERROR_NULL;
+if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
+if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
+if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
+
+return data->number_of_codes;
+}
+
+
+/*************************************************
+* Free the allocated stream *
+*************************************************/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_serialize_free(uint8_t *bytes)
+{
+if (bytes != NULL)
+ {
+ pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));
+ memctl->free(memctl, memctl->memory_data);
+ }
+}
+
+/* End of pcre2_serialize.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_string_utils.c b/test/monniaux/pcre2-10.32/pcre2_string_utils.c
new file mode 100644
index 00000000..d6be01ac
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_string_utils.c
@@ -0,0 +1,237 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains internal functions for comparing and finding the length
+of strings. These are used instead of strcmp() etc because the standard
+functions work only on 8-bit data. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+/*************************************************
+* Emulated memmove() for systems without it *
+*************************************************/
+
+/* This function can make use of bcopy() if it is available. Otherwise do it by
+steam, as there some non-Unix environments that lack both memmove() and
+bcopy(). */
+
+#if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
+void *
+PRIV(memmove)(void *d, const void *s, size_t n)
+{
+#ifdef HAVE_BCOPY
+bcopy(s, d, n);
+return d;
+#else
+size_t i;
+unsigned char *dest = (unsigned char *)d;
+const unsigned char *src = (const unsigned char *)s;
+if (dest > src)
+ {
+ dest += n;
+ src += n;
+ for (i = 0; i < n; ++i) *(--dest) = *(--src);
+ return (void *)dest;
+ }
+else
+ {
+ for (i = 0; i < n; ++i) *dest++ = *src++;
+ return (void *)(dest - n);
+ }
+#endif /* not HAVE_BCOPY */
+}
+#endif /* not VPCOMPAT && not HAVE_MEMMOVE */
+
+
+/*************************************************
+* Compare two zero-terminated PCRE2 strings *
+*************************************************/
+
+/*
+Arguments:
+ str1 first string
+ str2 second string
+
+Returns: 0, 1, or -1
+*/
+
+int
+PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)
+{
+PCRE2_UCHAR c1, c2;
+while (*str1 != '\0' || *str2 != '\0')
+ {
+ c1 = *str1++;
+ c2 = *str2++;
+ if (c1 != c2) return ((c1 > c2) << 1) - 1;
+ }
+return 0;
+}
+
+
+/*************************************************
+* Compare zero-terminated PCRE2 & 8-bit strings *
+*************************************************/
+
+/* As the 8-bit string is almost always a literal, its type is specified as
+const char *.
+
+Arguments:
+ str1 first string
+ str2 second string
+
+Returns: 0, 1, or -1
+*/
+
+int
+PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)
+{
+PCRE2_UCHAR c1, c2;
+while (*str1 != '\0' || *str2 != '\0')
+ {
+ c1 = *str1++;
+ c2 = *str2++;
+ if (c1 != c2) return ((c1 > c2) << 1) - 1;
+ }
+return 0;
+}
+
+
+/*************************************************
+* Compare two PCRE2 strings, given a length *
+*************************************************/
+
+/*
+Arguments:
+ str1 first string
+ str2 second string
+ len the length
+
+Returns: 0, 1, or -1
+*/
+
+int
+PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
+{
+PCRE2_UCHAR c1, c2;
+for (; len > 0; len--)
+ {
+ c1 = *str1++;
+ c2 = *str2++;
+ if (c1 != c2) return ((c1 > c2) << 1) - 1;
+ }
+return 0;
+}
+
+
+/*************************************************
+* Compare PCRE2 string to 8-bit string by length *
+*************************************************/
+
+/* As the 8-bit string is almost always a literal, its type is specified as
+const char *.
+
+Arguments:
+ str1 first string
+ str2 second string
+ len the length
+
+Returns: 0, 1, or -1
+*/
+
+int
+PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)
+{
+PCRE2_UCHAR c1, c2;
+for (; len > 0; len--)
+ {
+ c1 = *str1++;
+ c2 = *str2++;
+ if (c1 != c2) return ((c1 > c2) << 1) - 1;
+ }
+return 0;
+}
+
+
+/*************************************************
+* Find the length of a PCRE2 string *
+*************************************************/
+
+/*
+Argument: the string
+Returns: the length
+*/
+
+PCRE2_SIZE
+PRIV(strlen)(PCRE2_SPTR str)
+{
+PCRE2_SIZE c = 0;
+while (*str++ != 0) c++;
+return c;
+}
+
+
+/*************************************************
+* Copy 8-bit 0-terminated string to PCRE2 string *
+*************************************************/
+
+/* Arguments:
+ str1 buffer to receive the string
+ str2 8-bit string to be copied
+
+Returns: the number of code units used (excluding trailing zero)
+*/
+
+PCRE2_SIZE
+PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)
+{
+PCRE2_UCHAR *t = str1;
+while (*str2 != 0) *t++ = *str2++;
+*t = 0;
+return t - str1;
+}
+
+/* End of pcre2_string_utils.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_study.c b/test/monniaux/pcre2-10.32/pcre2_study.c
new file mode 100644
index 00000000..acbf98b4
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_study.c
@@ -0,0 +1,1635 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains functions for scanning a compiled pattern and
+collecting data (e.g. minimum matching length). */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+/* The maximum remembered capturing brackets minimum. */
+
+#define MAX_CACHE_BACKREF 128
+
+/* Set a bit in the starting code unit bit map. */
+
+#define SET_BIT(c) re->start_bitmap[(c)/8] |= (1 << ((c)&7))
+
+/* Returns from set_start_bits() */
+
+enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
+
+
+/*************************************************
+* Find the minimum subject length for a group *
+*************************************************/
+
+/* Scan a parenthesized group and compute the minimum length of subject that
+is needed to match it. This is a lower bound; it does not mean there is a
+string of that length that matches. In UTF mode, the result is in characters
+rather than code units. The field in a compiled pattern for storing the minimum
+length is 16-bits long (on the grounds that anything longer than that is
+pathological), so we give up when we reach that amount. This also means that
+integer overflow for really crazy patterns cannot happen.
+
+Backreference minimum lengths are cached to speed up multiple references. This
+function is called only when the highest back reference in the pattern is less
+than or equal to MAX_CACHE_BACKREF, which is one less than the size of the
+caching vector. The zeroth element contains the number of the highest set
+value.
+
+Arguments:
+ re compiled pattern block
+ code pointer to start of group (the bracket)
+ startcode pointer to start of the whole pattern's code
+ utf UTF flag
+ recurses chain of recurse_check to catch mutual recursion
+ countptr pointer to call count (to catch over complexity)
+ backref_cache vector for caching back references.
+
+Returns: the minimum length
+ -1 \C in UTF-8 mode
+ or (*ACCEPT)
+ or pattern too complicated
+ or back reference to duplicate name/number
+ -2 internal error (missing capturing bracket)
+ -3 internal error (opcode not listed)
+*/
+
+static int
+find_minlength(const pcre2_real_code *re, PCRE2_SPTR code,
+ PCRE2_SPTR startcode, BOOL utf, recurse_check *recurses, int *countptr,
+ int *backref_cache)
+{
+int length = -1;
+int prev_cap_recno = -1;
+int prev_cap_d = 0;
+int prev_recurse_recno = -1;
+int prev_recurse_d = 0;
+uint32_t once_fudge = 0;
+BOOL had_recurse = FALSE;
+BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
+recurse_check this_recurse;
+int branchlength = 0;
+PCRE2_UCHAR *cc = (PCRE2_UCHAR *)code + 1 + LINK_SIZE;
+
+/* If this is a "could be empty" group, its minimum length is 0. */
+
+if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
+
+/* Skip over capturing bracket number */
+
+if (*code == OP_CBRA || *code == OP_CBRAPOS) cc += IMM2_SIZE;
+
+/* A large and/or complex regex can take too long to process. */
+
+if ((*countptr)++ > 1000) return -1;
+
+/* Scan along the opcodes for this branch. If we get to the end of the branch,
+check the length against that of the other branches. If the accumulated length
+passes 16-bits, stop. */
+
+for (;;)
+ {
+ int d, min, recno;
+ PCRE2_UCHAR *cs, *ce;
+ PCRE2_UCHAR op = *cc;
+
+ if (branchlength >= UINT16_MAX) return UINT16_MAX;
+
+ switch (op)
+ {
+ case OP_COND:
+ case OP_SCOND:
+
+ /* If there is only one branch in a condition, the implied branch has zero
+ length, so we don't add anything. This covers the DEFINE "condition"
+ automatically. If there are two branches we can treat it the same as any
+ other non-capturing subpattern. */
+
+ cs = cc + GET(cc, 1);
+ if (*cs != OP_ALT)
+ {
+ cc = cs + 1 + LINK_SIZE;
+ break;
+ }
+ goto PROCESS_NON_CAPTURE;
+
+ case OP_BRA:
+ /* There's a special case of OP_BRA, when it is wrapped round a repeated
+ OP_RECURSE. We'd like to process the latter at this level so that
+ remembering the value works for repeated cases. So we do nothing, but
+ set a fudge value to skip over the OP_KET after the recurse. */
+
+ if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
+ {
+ once_fudge = 1 + LINK_SIZE;
+ cc += 1 + LINK_SIZE;
+ break;
+ }
+ /* Fall through */
+
+ case OP_ONCE:
+ case OP_SBRA:
+ case OP_BRAPOS:
+ case OP_SBRAPOS:
+ PROCESS_NON_CAPTURE:
+ d = find_minlength(re, cc, startcode, utf, recurses, countptr,
+ backref_cache);
+ if (d < 0) return d;
+ branchlength += d;
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
+ cc += 1 + LINK_SIZE;
+ break;
+
+ /* To save time for repeated capturing subpatterns, we remember the
+ length of the previous one. Unfortunately we can't do the same for
+ the unnumbered ones above. Nor can we do this if (?| is present in the
+ pattern because captures with the same number are not then identical. */
+
+ case OP_CBRA:
+ case OP_SCBRA:
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ recno = (int)GET2(cc, 1+LINK_SIZE);
+ if (dupcapused || recno != prev_cap_recno)
+ {
+ prev_cap_recno = recno;
+ prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
+ backref_cache);
+ if (prev_cap_d < 0) return prev_cap_d;
+ }
+ branchlength += prev_cap_d;
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
+ cc += 1 + LINK_SIZE;
+ break;
+
+ /* ACCEPT makes things far too complicated; we have to give up. */
+
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ return -1;
+
+ /* Reached end of a branch; if it's a ket it is the end of a nested
+ call. If it's ALT it is an alternation in a nested call. If it is END it's
+ the end of the outer call. All can be handled by the same code. If an
+ ACCEPT was previously encountered, use the length that was in force at that
+ time, and pass back the shortest ACCEPT length. */
+
+ case OP_ALT:
+ case OP_KET:
+ case OP_KETRMAX:
+ case OP_KETRMIN:
+ case OP_KETRPOS:
+ case OP_END:
+ if (length < 0 || (!had_recurse && branchlength < length))
+ length = branchlength;
+ if (op != OP_ALT) return length;
+ cc += 1 + LINK_SIZE;
+ branchlength = 0;
+ had_recurse = FALSE;
+ break;
+
+ /* Skip over assertive subpatterns */
+
+ case OP_ASSERT:
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
+ /* Fall through */
+
+ /* Skip over things that don't match chars */
+
+ case OP_REVERSE:
+ case OP_CREF:
+ case OP_DNCREF:
+ case OP_RREF:
+ case OP_DNRREF:
+ case OP_FALSE:
+ case OP_TRUE:
+ case OP_CALLOUT:
+ case OP_SOD:
+ case OP_SOM:
+ case OP_EOD:
+ case OP_EODN:
+ case OP_CIRC:
+ case OP_CIRCM:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_NOT_WORD_BOUNDARY:
+ case OP_WORD_BOUNDARY:
+ cc += PRIV(OP_lengths)[*cc];
+ break;
+
+ case OP_CALLOUT_STR:
+ cc += GET(cc, 1 + 2*LINK_SIZE);
+ break;
+
+ /* Skip over a subpattern that has a {0} or {0,x} quantifier */
+
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+ case OP_BRAPOSZERO:
+ case OP_SKIPZERO:
+ cc += PRIV(OP_lengths)[*cc];
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
+ cc += 1 + LINK_SIZE;
+ break;
+
+ /* Handle literal characters and + repetitions */
+
+ case OP_CHAR:
+ case OP_CHARI:
+ case OP_NOT:
+ case OP_NOTI:
+ case OP_PLUS:
+ case OP_PLUSI:
+ case OP_MINPLUS:
+ case OP_MINPLUSI:
+ case OP_POSPLUS:
+ case OP_POSPLUSI:
+ case OP_NOTPLUS:
+ case OP_NOTPLUSI:
+ case OP_NOTMINPLUS:
+ case OP_NOTMINPLUSI:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSPLUSI:
+ branchlength++;
+ cc += 2;
+#ifdef SUPPORT_UNICODE
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEPOSPLUS:
+ branchlength++;
+ cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
+ break;
+
+ /* Handle exact repetitions. The count is already in characters, but we
+ may need to skip over a multibyte character in UTF mode. */
+
+ case OP_EXACT:
+ case OP_EXACTI:
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ branchlength += GET2(cc,1);
+ cc += 2 + IMM2_SIZE;
+#ifdef SUPPORT_UNICODE
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ case OP_TYPEEXACT:
+ branchlength += GET2(cc,1);
+ cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
+ || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
+ break;
+
+ /* Handle single-char non-literal matchers */
+
+ case OP_PROP:
+ case OP_NOTPROP:
+ cc += 2;
+ /* Fall through */
+
+ case OP_NOT_DIGIT:
+ case OP_DIGIT:
+ case OP_NOT_WHITESPACE:
+ case OP_WHITESPACE:
+ case OP_NOT_WORDCHAR:
+ case OP_WORDCHAR:
+ case OP_ANY:
+ case OP_ALLANY:
+ case OP_EXTUNI:
+ case OP_HSPACE:
+ case OP_NOT_HSPACE:
+ case OP_VSPACE:
+ case OP_NOT_VSPACE:
+ branchlength++;
+ cc++;
+ break;
+
+ /* "Any newline" might match two characters, but it also might match just
+ one. */
+
+ case OP_ANYNL:
+ branchlength += 1;
+ cc++;
+ break;
+
+ /* The single-byte matcher means we can't proceed in UTF mode. (In
+ non-UTF mode \C will actually be turned into OP_ALLANY, so won't ever
+ appear, but leave the code, just in case.) */
+
+ case OP_ANYBYTE:
+#ifdef SUPPORT_UNICODE
+ if (utf) return -1;
+#endif
+ branchlength++;
+ cc++;
+ break;
+
+ /* For repeated character types, we have to test for \p and \P, which have
+ an extra two bytes of parameters. */
+
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEPOSQUERY:
+ if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
+ cc += PRIV(OP_lengths)[op];
+ break;
+
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEPOSUPTO:
+ if (cc[1 + IMM2_SIZE] == OP_PROP
+ || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
+ cc += PRIV(OP_lengths)[op];
+ break;
+
+ /* Check a class for variable quantification */
+
+ case OP_CLASS:
+ case OP_NCLASS:
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ /* The original code caused an unsigned overflow in 64 bit systems,
+ so now we use a conditional statement. */
+ if (op == OP_XCLASS)
+ cc += GET(cc, 1);
+ else
+ cc += PRIV(OP_lengths)[OP_CLASS];
+#else
+ cc += PRIV(OP_lengths)[OP_CLASS];
+#endif
+
+ switch (*cc)
+ {
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSPLUS:
+ branchlength++;
+ /* Fall through */
+
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSQUERY:
+ cc++;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ branchlength += GET2(cc,1);
+ cc += 1 + 2 * IMM2_SIZE;
+ break;
+
+ default:
+ branchlength++;
+ break;
+ }
+ break;
+
+ /* Backreferences and subroutine calls (OP_RECURSE) are treated in the same
+ way: we find the minimum length for the subpattern. A recursion
+ (backreference or subroutine) causes an a flag to be set that causes the
+ length of this branch to be ignored. The logic is that a recursion can only
+ make sense if there is another alternative that stops the recursing. That
+ will provide the minimum length (when no recursion happens).
+
+ If PCRE2_MATCH_UNSET_BACKREF is set, a backreference to an unset bracket
+ matches an empty string (by default it causes a matching failure), so in
+ that case we must set the minimum length to zero. */
+
+ /* Duplicate named pattern back reference. We cannot reliably find a length
+ for this if duplicate numbers are present in the pattern. */
+
+ case OP_DNREF:
+ case OP_DNREFI:
+ if (dupcapused) return -1;
+ if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
+ {
+ int count = GET2(cc, 1+IMM2_SIZE);
+ PCRE2_UCHAR *slot =
+ (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
+ GET2(cc, 1) * re->name_entry_size;
+
+ d = INT_MAX;
+
+ /* Scan all groups with the same name; find the shortest. */
+
+ while (count-- > 0)
+ {
+ int dd, i;
+ recno = GET2(slot, 0);
+
+ if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
+ dd = backref_cache[recno];
+ else
+ {
+ ce = cs = (PCRE2_UCHAR *)PRIV(find_bracket)(startcode, utf, recno);
+ if (cs == NULL) return -2;
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
+ if (cc > cs && cc < ce) /* Simple recursion */
+ {
+ dd = 0;
+ had_recurse = TRUE;
+ }
+ else
+ {
+ recurse_check *r = recurses;
+ for (r = recurses; r != NULL; r = r->prev)
+ if (r->group == cs) break;
+ if (r != NULL) /* Mutual recursion */
+ {
+ dd = 0;
+ had_recurse = TRUE;
+ }
+ else
+ {
+ this_recurse.prev = recurses;
+ this_recurse.group = cs;
+ dd = find_minlength(re, cs, startcode, utf, &this_recurse,
+ countptr, backref_cache);
+ if (dd < 0) return dd;
+ }
+ }
+
+ backref_cache[recno] = dd;
+ for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
+ backref_cache[0] = recno;
+ }
+
+ if (dd < d) d = dd;
+ if (d <= 0) break; /* No point looking at any more */
+ slot += re->name_entry_size;
+ }
+ }
+ else d = 0;
+ cc += 1 + 2*IMM2_SIZE;
+ goto REPEAT_BACK_REFERENCE;
+
+ /* Single back reference. We cannot find a length for this if duplicate
+ numbers are present in the pattern. */
+
+ case OP_REF:
+ case OP_REFI:
+ if (dupcapused) return -1;
+ recno = GET2(cc, 1);
+ if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
+ d = backref_cache[recno];
+ else
+ {
+ int i;
+ if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
+ {
+ ce = cs = (PCRE2_UCHAR *)PRIV(find_bracket)(startcode, utf, recno);
+ if (cs == NULL) return -2;
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
+ if (cc > cs && cc < ce) /* Simple recursion */
+ {
+ d = 0;
+ had_recurse = TRUE;
+ }
+ else
+ {
+ recurse_check *r = recurses;
+ for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
+ if (r != NULL) /* Mutual recursion */
+ {
+ d = 0;
+ had_recurse = TRUE;
+ }
+ else
+ {
+ this_recurse.prev = recurses;
+ this_recurse.group = cs;
+ d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
+ backref_cache);
+ if (d < 0) return d;
+ }
+ }
+ }
+ else d = 0;
+
+ backref_cache[recno] = d;
+ for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
+ backref_cache[0] = recno;
+ }
+
+ cc += 1 + IMM2_SIZE;
+
+ /* Handle repeated back references */
+
+ REPEAT_BACK_REFERENCE:
+ switch (*cc)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSQUERY:
+ min = 0;
+ cc++;
+ break;
+
+ case OP_CRPLUS:
+ case OP_CRMINPLUS:
+ case OP_CRPOSPLUS:
+ min = 1;
+ cc++;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ min = GET2(cc, 1);
+ cc += 1 + 2 * IMM2_SIZE;
+ break;
+
+ default:
+ min = 1;
+ break;
+ }
+
+ /* Take care not to overflow: (1) min and d are ints, so check that their
+ product is not greater than INT_MAX. (2) branchlength is limited to
+ UINT16_MAX (checked at the top of the loop). */
+
+ if ((d > 0 && (INT_MAX/d) < min) || UINT16_MAX - branchlength < min*d)
+ branchlength = UINT16_MAX;
+ else branchlength += min * d;
+ break;
+
+ /* Recursion always refers to the first occurrence of a subpattern with a
+ given number. Therefore, we can always make use of caching, even when the
+ pattern contains multiple subpatterns with the same number. */
+
+ case OP_RECURSE:
+ cs = ce = (PCRE2_UCHAR *)startcode + GET(cc, 1);
+ recno = GET2(cs, 1+LINK_SIZE);
+ if (recno == prev_recurse_recno)
+ {
+ branchlength += prev_recurse_d;
+ }
+ else
+ {
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
+ if (cc > cs && cc < ce) /* Simple recursion */
+ had_recurse = TRUE;
+ else
+ {
+ recurse_check *r = recurses;
+ for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
+ if (r != NULL) /* Mutual recursion */
+ had_recurse = TRUE;
+ else
+ {
+ this_recurse.prev = recurses;
+ this_recurse.group = cs;
+ prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
+ countptr, backref_cache);
+ if (prev_recurse_d < 0) return prev_recurse_d;
+ prev_recurse_recno = recno;
+ branchlength += prev_recurse_d;
+ }
+ }
+ }
+ cc += 1 + LINK_SIZE + once_fudge;
+ once_fudge = 0;
+ break;
+
+ /* Anything else does not or need not match a character. We can get the
+ item's length from the table, but for those that can match zero occurrences
+ of a character, we must take special action for UTF-8 characters. As it
+ happens, the "NOT" versions of these opcodes are used at present only for
+ ASCII characters, so they could be omitted from this list. However, in
+ future that may change, so we include them here so as not to leave a
+ gotcha for a future maintainer. */
+
+ case OP_UPTO:
+ case OP_UPTOI:
+ case OP_NOTUPTO:
+ case OP_NOTUPTOI:
+ case OP_MINUPTO:
+ case OP_MINUPTOI:
+ case OP_NOTMINUPTO:
+ case OP_NOTMINUPTOI:
+ case OP_POSUPTO:
+ case OP_POSUPTOI:
+ case OP_NOTPOSUPTO:
+ case OP_NOTPOSUPTOI:
+
+ case OP_STAR:
+ case OP_STARI:
+ case OP_NOTSTAR:
+ case OP_NOTSTARI:
+ case OP_MINSTAR:
+ case OP_MINSTARI:
+ case OP_NOTMINSTAR:
+ case OP_NOTMINSTARI:
+ case OP_POSSTAR:
+ case OP_POSSTARI:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSSTARI:
+
+ case OP_QUERY:
+ case OP_QUERYI:
+ case OP_NOTQUERY:
+ case OP_NOTQUERYI:
+ case OP_MINQUERY:
+ case OP_MINQUERYI:
+ case OP_NOTMINQUERY:
+ case OP_NOTMINQUERYI:
+ case OP_POSQUERY:
+ case OP_POSQUERYI:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSQUERYI:
+
+ cc += PRIV(OP_lengths)[op];
+#ifdef SUPPORT_UNICODE
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
+#endif
+ break;
+
+ /* Skip these, but we need to add in the name length. */
+
+ case OP_MARK:
+ case OP_COMMIT_ARG:
+ case OP_PRUNE_ARG:
+ case OP_SKIP_ARG:
+ case OP_THEN_ARG:
+ cc += PRIV(OP_lengths)[op] + cc[1];
+ break;
+
+ /* The remaining opcodes are just skipped over. */
+
+ case OP_CLOSE:
+ case OP_COMMIT:
+ case OP_FAIL:
+ case OP_PRUNE:
+ case OP_SET_SOM:
+ case OP_SKIP:
+ case OP_THEN:
+ cc += PRIV(OP_lengths)[op];
+ break;
+
+ /* This should not occur: we list all opcodes explicitly so that when
+ new ones get added they are properly considered. */
+
+ default:
+ return -3;
+ }
+ }
+/* Control never gets here */
+}
+
+
+
+/*************************************************
+* Set a bit and maybe its alternate case *
+*************************************************/
+
+/* Given a character, set its first code unit's bit in the table, and also the
+corresponding bit for the other version of a letter if we are caseless.
+
+Arguments:
+ re points to the regex block
+ p points to the first code unit of the character
+ caseless TRUE if caseless
+ utf TRUE for UTF mode
+
+Returns: pointer after the character
+*/
+
+static PCRE2_SPTR
+set_table_bit(pcre2_real_code *re, PCRE2_SPTR p, BOOL caseless, BOOL utf)
+{
+uint32_t c = *p++; /* First code unit */
+(void)utf; /* Stop compiler warning when UTF not supported */
+
+/* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for
+0xff. */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+if (c > 0xff) SET_BIT(0xff); else
+#endif
+
+SET_BIT(c);
+
+/* In UTF-8 or UTF-16 mode, pick up the remaining code units in order to find
+the end of the character, even when caseless. */
+
+#ifdef SUPPORT_UNICODE
+if (utf)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ if (c >= 0xc0) GETUTF8INC(c, p);
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
+#endif
+ }
+#endif /* SUPPORT_UNICODE */
+
+/* If caseless, handle the other case of the character. */
+
+if (caseless)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ PCRE2_UCHAR buff[6];
+ c = UCD_OTHERCASE(c);
+ (void)PRIV(ord2utf)(c, buff);
+ SET_BIT(buff[0]);
+#else /* 16-bit or 32-bit mode */
+ c = UCD_OTHERCASE(c);
+ if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
+#endif
+ }
+ else
+#endif /* SUPPORT_UNICODE */
+
+ /* Not UTF */
+
+ if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
+ }
+
+return p;
+}
+
+
+
+/*************************************************
+* Set bits for a positive character type *
+*************************************************/
+
+/* This function sets starting bits for a character type. In UTF-8 mode, we can
+only do a direct setting for bytes less than 128, as otherwise there can be
+confusion with bytes in the middle of UTF-8 characters. In a "traditional"
+environment, the tables will only recognize ASCII characters anyway, but in at
+least one Windows environment, some higher bytes bits were set in the tables.
+So we deal with that case by considering the UTF-8 encoding.
+
+Arguments:
+ re the regex block
+ cbit type the type of character wanted
+ table_limit 32 for non-UTF-8; 16 for UTF-8
+
+Returns: nothing
+*/
+
+static void
+set_type_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
+{
+uint32_t c;
+for (c = 0; c < table_limit; c++)
+ re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+if (table_limit == 32) return;
+for (c = 128; c < 256; c++)
+ {
+ if ((re->tables[cbits_offset + c/8] & (1 << (c&7))) != 0)
+ {
+ PCRE2_UCHAR buff[6];
+ (void)PRIV(ord2utf)(c, buff);
+ SET_BIT(buff[0]);
+ }
+ }
+#endif /* UTF-8 */
+}
+
+
+/*************************************************
+* Set bits for a negative character type *
+*************************************************/
+
+/* This function sets starting bits for a negative character type such as \D.
+In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
+otherwise there can be confusion with bytes in the middle of UTF-8 characters.
+Unlike in the positive case, where we can set appropriate starting bits for
+specific high-valued UTF-8 characters, in this case we have to set the bits for
+all high-valued characters. The lowest is 0xc2, but we overkill by starting at
+0xc0 (192) for simplicity.
+
+Arguments:
+ re the regex block
+ cbit type the type of character wanted
+ table_limit 32 for non-UTF-8; 16 for UTF-8
+
+Returns: nothing
+*/
+
+static void
+set_nottype_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
+{
+uint32_t c;
+for (c = 0; c < table_limit; c++)
+ re->start_bitmap[c] |= ~(re->tables[c+cbits_offset+cbit_type]);
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
+#endif
+}
+
+
+
+/*************************************************
+* Create bitmap of starting bytes *
+*************************************************/
+
+/* This function scans a compiled unanchored expression recursively and
+attempts to build a bitmap of the set of possible starting code units whose
+values are less than 256. In 16-bit and 32-bit mode, values above 255 all cause
+the 255 bit to be set. When calling set[_not]_type_bits() in UTF-8 (sic) mode
+we pass a value of 16 rather than 32 as the final argument. (See comments in
+those functions for the reason.)
+
+The SSB_CONTINUE return is useful for parenthesized groups in patterns such as
+(a*)b where the group provides some optional starting code units but scanning
+must continue at the outer level to find at least one mandatory code unit. At
+the outermost level, this function fails unless the result is SSB_DONE.
+
+Arguments:
+ re points to the compiled regex block
+ code points to an expression
+ utf TRUE if in UTF mode
+
+Returns: SSB_FAIL => Failed to find any starting code units
+ SSB_DONE => Found mandatory starting code units
+ SSB_CONTINUE => Found optional starting code units
+ SSB_UNKNOWN => Hit an unrecognized opcode
+*/
+
+static int
+set_start_bits(pcre2_real_code *re, PCRE2_SPTR code, BOOL utf)
+{
+uint32_t c;
+int yield = SSB_DONE;
+
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+int table_limit = utf? 16:32;
+#else
+int table_limit = 32;
+#endif
+
+do
+ {
+ BOOL try_next = TRUE;
+ PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
+
+ if (*code == OP_CBRA || *code == OP_SCBRA ||
+ *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
+
+ while (try_next) /* Loop for items in this branch */
+ {
+ int rc;
+ uint8_t *classmap = NULL;
+
+ switch(*tcode)
+ {
+ /* If we reach something we don't understand, it means a new opcode has
+ been created that hasn't been added to this function. Hopefully this
+ problem will be discovered during testing. */
+
+ default:
+ return SSB_UNKNOWN;
+
+ /* Fail for a valid opcode that implies no starting bits. */
+
+ case OP_ACCEPT:
+ case OP_ASSERT_ACCEPT:
+ case OP_ALLANY:
+ case OP_ANY:
+ case OP_ANYBYTE:
+ case OP_CIRCM:
+ case OP_CLOSE:
+ case OP_COMMIT:
+ case OP_COMMIT_ARG:
+ case OP_COND:
+ case OP_CREF:
+ case OP_FALSE:
+ case OP_TRUE:
+ case OP_DNCREF:
+ case OP_DNREF:
+ case OP_DNREFI:
+ case OP_DNRREF:
+ case OP_DOLL:
+ case OP_DOLLM:
+ case OP_END:
+ case OP_EOD:
+ case OP_EODN:
+ case OP_EXTUNI:
+ case OP_FAIL:
+ case OP_MARK:
+ case OP_NOT:
+ case OP_NOTEXACT:
+ case OP_NOTEXACTI:
+ case OP_NOTI:
+ case OP_NOTMINPLUS:
+ case OP_NOTMINPLUSI:
+ case OP_NOTMINQUERY:
+ case OP_NOTMINQUERYI:
+ case OP_NOTMINSTAR:
+ case OP_NOTMINSTARI:
+ case OP_NOTMINUPTO:
+ case OP_NOTMINUPTOI:
+ case OP_NOTPLUS:
+ case OP_NOTPLUSI:
+ case OP_NOTPOSPLUS:
+ case OP_NOTPOSPLUSI:
+ case OP_NOTPOSQUERY:
+ case OP_NOTPOSQUERYI:
+ case OP_NOTPOSSTAR:
+ case OP_NOTPOSSTARI:
+ case OP_NOTPOSUPTO:
+ case OP_NOTPOSUPTOI:
+ case OP_NOTPROP:
+ case OP_NOTQUERY:
+ case OP_NOTQUERYI:
+ case OP_NOTSTAR:
+ case OP_NOTSTARI:
+ case OP_NOTUPTO:
+ case OP_NOTUPTOI:
+ case OP_NOT_HSPACE:
+ case OP_NOT_VSPACE:
+ case OP_PRUNE:
+ case OP_PRUNE_ARG:
+ case OP_RECURSE:
+ case OP_REF:
+ case OP_REFI:
+ case OP_REVERSE:
+ case OP_RREF:
+ case OP_SCOND:
+ case OP_SET_SOM:
+ case OP_SKIP:
+ case OP_SKIP_ARG:
+ case OP_SOD:
+ case OP_SOM:
+ case OP_THEN:
+ case OP_THEN_ARG:
+ return SSB_FAIL;
+
+ /* OP_CIRC happens only at the start of an anchored branch (multiline ^
+ uses OP_CIRCM). Skip over it. */
+
+ case OP_CIRC:
+ tcode += PRIV(OP_lengths)[OP_CIRC];
+ break;
+
+ /* A "real" property test implies no starting bits, but the fake property
+ PT_CLIST identifies a list of characters. These lists are short, as they
+ are used for characters with more than one "other case", so there is no
+ point in recognizing them for OP_NOTPROP. */
+
+ case OP_PROP:
+ if (tcode[1] != PT_CLIST) return SSB_FAIL;
+ {
+ const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
+ while ((c = *p++) < NOTACHAR)
+ {
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (utf)
+ {
+ PCRE2_UCHAR buff[6];
+ (void)PRIV(ord2utf)(c, buff);
+ c = buff[0];
+ }
+#endif
+ if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
+ }
+ }
+ try_next = FALSE;
+ break;
+
+ /* We can ignore word boundary tests. */
+
+ case OP_WORD_BOUNDARY:
+ case OP_NOT_WORD_BOUNDARY:
+ tcode++;
+ break;
+
+ /* If we hit a bracket or a positive lookahead assertion, recurse to set
+ bits from within the subpattern. If it can't find anything, we have to
+ give up. If it finds some mandatory character(s), we are done for this
+ branch. Otherwise, carry on scanning after the subpattern. */
+
+ case OP_BRA:
+ case OP_SBRA:
+ case OP_CBRA:
+ case OP_SCBRA:
+ case OP_BRAPOS:
+ case OP_SBRAPOS:
+ case OP_CBRAPOS:
+ case OP_SCBRAPOS:
+ case OP_ONCE:
+ case OP_ASSERT:
+ rc = set_start_bits(re, tcode, utf);
+ if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
+ if (rc == SSB_DONE) try_next = FALSE; else
+ {
+ do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
+ tcode += 1 + LINK_SIZE;
+ }
+ break;
+
+ /* If we hit ALT or KET, it means we haven't found anything mandatory in
+ this branch, though we might have found something optional. For ALT, we
+ continue with the next alternative, but we have to arrange that the final
+ result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
+ return SSB_CONTINUE: if this is the top level, that indicates failure,
+ but after a nested subpattern, it causes scanning to continue. */
+
+ case OP_ALT:
+ yield = SSB_CONTINUE;
+ try_next = FALSE;
+ break;
+
+ case OP_KET:
+ case OP_KETRMAX:
+ case OP_KETRMIN:
+ case OP_KETRPOS:
+ return SSB_CONTINUE;
+
+ /* Skip over callout */
+
+ case OP_CALLOUT:
+ tcode += PRIV(OP_lengths)[OP_CALLOUT];
+ break;
+
+ case OP_CALLOUT_STR:
+ tcode += GET(tcode, 1 + 2*LINK_SIZE);
+ break;
+
+ /* Skip over lookbehind and negative lookahead assertions */
+
+ case OP_ASSERT_NOT:
+ case OP_ASSERTBACK:
+ case OP_ASSERTBACK_NOT:
+ do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
+ tcode += 1 + LINK_SIZE;
+ break;
+
+ /* BRAZERO does the bracket, but carries on. */
+
+ case OP_BRAZERO:
+ case OP_BRAMINZERO:
+ case OP_BRAPOSZERO:
+ rc = set_start_bits(re, ++tcode, utf);
+ if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
+ do tcode += GET(tcode,1); while (*tcode == OP_ALT);
+ tcode += 1 + LINK_SIZE;
+ break;
+
+ /* SKIPZERO skips the bracket. */
+
+ case OP_SKIPZERO:
+ tcode++;
+ do tcode += GET(tcode,1); while (*tcode == OP_ALT);
+ tcode += 1 + LINK_SIZE;
+ break;
+
+ /* Single-char * or ? sets the bit and tries the next item */
+
+ case OP_STAR:
+ case OP_MINSTAR:
+ case OP_POSSTAR:
+ case OP_QUERY:
+ case OP_MINQUERY:
+ case OP_POSQUERY:
+ tcode = set_table_bit(re, tcode + 1, FALSE, utf);
+ break;
+
+ case OP_STARI:
+ case OP_MINSTARI:
+ case OP_POSSTARI:
+ case OP_QUERYI:
+ case OP_MINQUERYI:
+ case OP_POSQUERYI:
+ tcode = set_table_bit(re, tcode + 1, TRUE, utf);
+ break;
+
+ /* Single-char upto sets the bit and tries the next */
+
+ case OP_UPTO:
+ case OP_MINUPTO:
+ case OP_POSUPTO:
+ tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf);
+ break;
+
+ case OP_UPTOI:
+ case OP_MINUPTOI:
+ case OP_POSUPTOI:
+ tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf);
+ break;
+
+ /* At least one single char sets the bit and stops */
+
+ case OP_EXACT:
+ tcode += IMM2_SIZE;
+ /* Fall through */
+ case OP_CHAR:
+ case OP_PLUS:
+ case OP_MINPLUS:
+ case OP_POSPLUS:
+ (void)set_table_bit(re, tcode + 1, FALSE, utf);
+ try_next = FALSE;
+ break;
+
+ case OP_EXACTI:
+ tcode += IMM2_SIZE;
+ /* Fall through */
+ case OP_CHARI:
+ case OP_PLUSI:
+ case OP_MINPLUSI:
+ case OP_POSPLUSI:
+ (void)set_table_bit(re, tcode + 1, TRUE, utf);
+ try_next = FALSE;
+ break;
+
+ /* Special spacing and line-terminating items. These recognize specific
+ lists of characters. The difference between VSPACE and ANYNL is that the
+ latter can match the two-character CRLF sequence, but that is not
+ relevant for finding the first character, so their code here is
+ identical. */
+
+ case OP_HSPACE:
+ SET_BIT(CHAR_HT);
+ SET_BIT(CHAR_SPACE);
+
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
+ the bits for 0xA0 and for code units >= 255, independently of UTF. */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ SET_BIT(0xA0);
+ SET_BIT(0xFF);
+#else
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
+ units of horizontal space characters. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ SET_BIT(0xC2); /* For U+00A0 */
+ SET_BIT(0xE1); /* For U+1680, U+180E */
+ SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
+ SET_BIT(0xE3); /* For U+3000 */
+ }
+ else
+#endif
+ /* For the 8-bit library not in UTF-8 mode, set the bit for 0xA0, unless
+ the code is EBCDIC. */
+ {
+#ifndef EBCDIC
+ SET_BIT(0xA0);
+#endif /* Not EBCDIC */
+ }
+#endif /* 8-bit support */
+
+ try_next = FALSE;
+ break;
+
+ case OP_ANYNL:
+ case OP_VSPACE:
+ SET_BIT(CHAR_LF);
+ SET_BIT(CHAR_VT);
+ SET_BIT(CHAR_FF);
+ SET_BIT(CHAR_CR);
+
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
+ the bits for NEL and for code units >= 255, independently of UTF. */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ SET_BIT(CHAR_NEL);
+ SET_BIT(0xFF);
+#else
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
+ units of vertical space characters. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ SET_BIT(0xC2); /* For U+0085 (NEL) */
+ SET_BIT(0xE2); /* For U+2028, U+2029 */
+ }
+ else
+#endif
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
+ {
+ SET_BIT(CHAR_NEL);
+ }
+#endif /* 8-bit support */
+
+ try_next = FALSE;
+ break;
+
+ /* Single character types set the bits and stop. Note that if PCRE2_UCP
+ is set, we do not see these opcodes because \d etc are converted to
+ properties. Therefore, these apply in the case when only characters less
+ than 256 are recognized to match the types. */
+
+ case OP_NOT_DIGIT:
+ set_nottype_bits(re, cbit_digit, table_limit);
+ try_next = FALSE;
+ break;
+
+ case OP_DIGIT:
+ set_type_bits(re, cbit_digit, table_limit);
+ try_next = FALSE;
+ break;
+
+ case OP_NOT_WHITESPACE:
+ set_nottype_bits(re, cbit_space, table_limit);
+ try_next = FALSE;
+ break;
+
+ case OP_WHITESPACE:
+ set_type_bits(re, cbit_space, table_limit);
+ try_next = FALSE;
+ break;
+
+ case OP_NOT_WORDCHAR:
+ set_nottype_bits(re, cbit_word, table_limit);
+ try_next = FALSE;
+ break;
+
+ case OP_WORDCHAR:
+ set_type_bits(re, cbit_word, table_limit);
+ try_next = FALSE;
+ break;
+
+ /* One or more character type fudges the pointer and restarts, knowing
+ it will hit a single character type and stop there. */
+
+ case OP_TYPEPLUS:
+ case OP_TYPEMINPLUS:
+ case OP_TYPEPOSPLUS:
+ tcode++;
+ break;
+
+ case OP_TYPEEXACT:
+ tcode += 1 + IMM2_SIZE;
+ break;
+
+ /* Zero or more repeats of character types set the bits and then
+ try again. */
+
+ case OP_TYPEUPTO:
+ case OP_TYPEMINUPTO:
+ case OP_TYPEPOSUPTO:
+ tcode += IMM2_SIZE; /* Fall through */
+
+ case OP_TYPESTAR:
+ case OP_TYPEMINSTAR:
+ case OP_TYPEPOSSTAR:
+ case OP_TYPEQUERY:
+ case OP_TYPEMINQUERY:
+ case OP_TYPEPOSQUERY:
+ switch(tcode[1])
+ {
+ default:
+ case OP_ANY:
+ case OP_ALLANY:
+ return SSB_FAIL;
+
+ case OP_HSPACE:
+ SET_BIT(CHAR_HT);
+ SET_BIT(CHAR_SPACE);
+
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
+ the bits for 0xA0 and for code units >= 255, independently of UTF. */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ SET_BIT(0xA0);
+ SET_BIT(0xFF);
+#else
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
+ units of horizontal space characters. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ SET_BIT(0xC2); /* For U+00A0 */
+ SET_BIT(0xE1); /* For U+1680, U+180E */
+ SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
+ SET_BIT(0xE3); /* For U+3000 */
+ }
+ else
+#endif
+ /* For the 8-bit library not in UTF-8 mode, set the bit for 0xA0, unless
+ the code is EBCDIC. */
+ {
+#ifndef EBCDIC
+ SET_BIT(0xA0);
+#endif /* Not EBCDIC */
+ }
+#endif /* 8-bit support */
+ break;
+
+ case OP_ANYNL:
+ case OP_VSPACE:
+ SET_BIT(CHAR_LF);
+ SET_BIT(CHAR_VT);
+ SET_BIT(CHAR_FF);
+ SET_BIT(CHAR_CR);
+
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
+ the bits for NEL and for code units >= 255, independently of UTF. */
+
+#if PCRE2_CODE_UNIT_WIDTH != 8
+ SET_BIT(CHAR_NEL);
+ SET_BIT(0xFF);
+#else
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
+ units of vertical space characters. */
+
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ SET_BIT(0xC2); /* For U+0085 (NEL) */
+ SET_BIT(0xE2); /* For U+2028, U+2029 */
+ }
+ else
+#endif
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
+ {
+ SET_BIT(CHAR_NEL);
+ }
+#endif /* 8-bit support */
+ break;
+
+ case OP_NOT_DIGIT:
+ set_nottype_bits(re, cbit_digit, table_limit);
+ break;
+
+ case OP_DIGIT:
+ set_type_bits(re, cbit_digit, table_limit);
+ break;
+
+ case OP_NOT_WHITESPACE:
+ set_nottype_bits(re, cbit_space, table_limit);
+ break;
+
+ case OP_WHITESPACE:
+ set_type_bits(re, cbit_space, table_limit);
+ break;
+
+ case OP_NOT_WORDCHAR:
+ set_nottype_bits(re, cbit_word, table_limit);
+ break;
+
+ case OP_WORDCHAR:
+ set_type_bits(re, cbit_word, table_limit);
+ break;
+ }
+
+ tcode += 2;
+ break;
+
+ /* Extended class: if there are any property checks, or if this is a
+ negative XCLASS without a map, give up. If there are no property checks,
+ there must be wide characters on the XCLASS list, because otherwise an
+ XCLASS would not have been created. This means that code points >= 255
+ are always potential starters. */
+
+#ifdef SUPPORT_WIDE_CHARS
+ case OP_XCLASS:
+ if ((tcode[1 + LINK_SIZE] & XCL_HASPROP) != 0 ||
+ (tcode[1 + LINK_SIZE] & (XCL_MAP|XCL_NOT)) == XCL_NOT)
+ return SSB_FAIL;
+
+ /* We have a positive XCLASS or a negative one without a map. Set up the
+ map pointer if there is one, and fall through. */
+
+ classmap = ((tcode[1 + LINK_SIZE] & XCL_MAP) == 0)? NULL :
+ (uint8_t *)(tcode + 1 + LINK_SIZE + 1);
+#endif
+ /* It seems that the fall through comment must be outside the #ifdef if
+ it is to avoid the gcc compiler warning. */
+
+ /* Fall through */
+
+ /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
+ in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
+ because it starts a character with a value > 255. In 8-bit non-UTF mode,
+ there is no difference between CLASS and NCLASS. In all other wide
+ character modes, set the 0xFF bit to indicate code units >= 255. */
+
+ case OP_NCLASS:
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (utf)
+ {
+ re->start_bitmap[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
+ memset(re->start_bitmap+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
+ }
+#elif PCRE2_CODE_UNIT_WIDTH != 8
+ SET_BIT(0xFF); /* For characters >= 255 */
+#endif
+ /* Fall through */
+
+ /* Enter here for a positive non-XCLASS. If we have fallen through from
+ an XCLASS, classmap will already be set; just advance the code pointer.
+ Otherwise, set up classmap for a a non-XCLASS and advance past it. */
+
+ case OP_CLASS:
+ if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
+ {
+ classmap = (uint8_t *)(++tcode);
+ tcode += 32 / sizeof(PCRE2_UCHAR);
+ }
+
+ /* When wide characters are supported, classmap may be NULL. In UTF-8
+ (sic) mode, the bits in a class bit map correspond to character values,
+ not to byte values. However, the bit map we are constructing is for byte
+ values. So we have to do a conversion for characters whose code point is
+ greater than 127. In fact, there are only two possible starting bytes for
+ characters in the range 128 - 255. */
+
+ if (classmap != NULL)
+ {
+#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
+ if (utf)
+ {
+ for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
+ for (c = 128; c < 256; c++)
+ {
+ if ((classmap[c/8] & (1 << (c&7))) != 0)
+ {
+ int d = (c >> 6) | 0xc0; /* Set bit for this starter */
+ re->start_bitmap[d/8] |= (1 << (d&7)); /* and then skip on to the */
+ c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
+ }
+ }
+ }
+ else
+#endif
+ /* In all modes except UTF-8, the two bit maps are compatible. */
+
+ {
+ for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
+ }
+ }
+
+ /* Act on what follows the class. For a zero minimum repeat, continue;
+ otherwise stop processing. */
+
+ switch (*tcode)
+ {
+ case OP_CRSTAR:
+ case OP_CRMINSTAR:
+ case OP_CRQUERY:
+ case OP_CRMINQUERY:
+ case OP_CRPOSSTAR:
+ case OP_CRPOSQUERY:
+ tcode++;
+ break;
+
+ case OP_CRRANGE:
+ case OP_CRMINRANGE:
+ case OP_CRPOSRANGE:
+ if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
+ else try_next = FALSE;
+ break;
+
+ default:
+ try_next = FALSE;
+ break;
+ }
+ break; /* End of class handling case */
+ } /* End of switch for opcodes */
+ } /* End of try_next loop */
+
+ code += GET(code, 1); /* Advance to next branch */
+ }
+while (*code == OP_ALT);
+
+return yield;
+}
+
+
+
+/*************************************************
+* Study a compiled expression *
+*************************************************/
+
+/* This function is handed a compiled expression that it must study to produce
+information that will speed up the matching.
+
+Argument: points to the compiled expression
+Returns: 0 normally; non-zero should never normally occur
+ 1 unknown opcode in set_start_bits
+ 2 missing capturing bracket
+ 3 unknown opcode in find_minlength
+*/
+
+int
+PRIV(study)(pcre2_real_code *re)
+{
+int min;
+int count = 0;
+PCRE2_UCHAR *code;
+BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
+
+/* Find start of compiled code */
+
+code = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
+ re->name_entry_size * re->name_count;
+
+/* For a pattern that has a first code unit, or a multiline pattern that
+matches only at "line start", there is no point in seeking a list of starting
+code units. */
+
+if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
+ {
+ int rc = set_start_bits(re, code, utf);
+ if (rc == SSB_UNKNOWN) return 1;
+ if (rc == SSB_DONE) re->flags |= PCRE2_FIRSTMAPSET;
+ }
+
+/* Find the minimum length of subject string. If the pattern can match an empty
+string, the minimum length is already known. If there are more back references
+than the size of the vector we are going to cache them in, do nothing. A
+pattern that complicated will probably take a long time to analyze and may in
+any case turn out to be too complicated. Note that back reference minima are
+held as 16-bit numbers. */
+
+if ((re->flags & PCRE2_MATCH_EMPTY) == 0 &&
+ re->top_backref <= MAX_CACHE_BACKREF)
+ {
+ int backref_cache[MAX_CACHE_BACKREF+1];
+ backref_cache[0] = 0; /* Highest one that is set */
+ min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
+ switch(min)
+ {
+ case -1: /* \C in UTF mode or (*ACCEPT) or over-complex regex */
+ break; /* Leave minlength unchanged (will be zero) */
+
+ case -2:
+ return 2; /* missing capturing bracket */
+
+ case -3:
+ return 3; /* unrecognized opcode */
+
+ default:
+ if (min > UINT16_MAX) min = UINT16_MAX;
+ re->minlength = min;
+ break;
+ }
+ }
+
+return 0;
+}
+
+/* End of pcre2_study.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_substitute.c b/test/monniaux/pcre2-10.32/pcre2_substitute.c
new file mode 100644
index 00000000..ab8d1090
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_substitute.c
@@ -0,0 +1,885 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+#define PTR_STACK_SIZE 20
+
+#define SUBSTITUTE_OPTIONS \
+ (PCRE2_SUBSTITUTE_EXTENDED|PCRE2_SUBSTITUTE_GLOBAL| \
+ PCRE2_SUBSTITUTE_OVERFLOW_LENGTH|PCRE2_SUBSTITUTE_UNKNOWN_UNSET| \
+ PCRE2_SUBSTITUTE_UNSET_EMPTY)
+
+
+
+/*************************************************
+* Find end of substitute text *
+*************************************************/
+
+/* In extended mode, we recognize ${name:+set text:unset text} and similar
+constructions. This requires the identification of unescaped : and }
+characters. This function scans for such. It must deal with nested ${
+constructions. The pointer to the text is updated, either to the required end
+character, or to where an error was detected.
+
+Arguments:
+ code points to the compiled expression (for options)
+ ptrptr points to the pointer to the start of the text (updated)
+ ptrend end of the whole string
+ last TRUE if the last expected string (only } recognized)
+
+Returns: 0 on success
+ negative error code on failure
+*/
+
+static int
+find_text_end(const pcre2_code *code, PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend,
+ BOOL last)
+{
+int rc = 0;
+uint32_t nestlevel = 0;
+BOOL literal = FALSE;
+PCRE2_SPTR ptr = *ptrptr;
+
+for (; ptr < ptrend; ptr++)
+ {
+ if (literal)
+ {
+ if (ptr[0] == CHAR_BACKSLASH && ptr < ptrend - 1 && ptr[1] == CHAR_E)
+ {
+ literal = FALSE;
+ ptr += 1;
+ }
+ }
+
+ else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
+ {
+ if (nestlevel == 0) goto EXIT;
+ nestlevel--;
+ }
+
+ else if (*ptr == CHAR_COLON && !last && nestlevel == 0) goto EXIT;
+
+ else if (*ptr == CHAR_DOLLAR_SIGN)
+ {
+ if (ptr < ptrend - 1 && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
+ {
+ nestlevel++;
+ ptr += 1;
+ }
+ }
+
+ else if (*ptr == CHAR_BACKSLASH)
+ {
+ int erc;
+ int errorcode;
+ uint32_t ch;
+
+ if (ptr < ptrend - 1) switch (ptr[1])
+ {
+ case CHAR_L:
+ case CHAR_l:
+ case CHAR_U:
+ case CHAR_u:
+ ptr += 1;
+ continue;
+ }
+
+ ptr += 1; /* Must point after \ */
+ erc = PRIV(check_escape)(&ptr, ptrend, &ch, &errorcode,
+ code->overall_options, FALSE, NULL);
+ ptr -= 1; /* Back to last code unit of escape */
+ if (errorcode != 0)
+ {
+ rc = errorcode;
+ goto EXIT;
+ }
+
+ switch(erc)
+ {
+ case 0: /* Data character */
+ case ESC_E: /* Isolated \E is ignored */
+ break;
+
+ case ESC_Q:
+ literal = TRUE;
+ break;
+
+ default:
+ rc = PCRE2_ERROR_BADREPESCAPE;
+ goto EXIT;
+ }
+ }
+ }
+
+rc = PCRE2_ERROR_REPMISSINGBRACE; /* Terminator not found */
+
+EXIT:
+*ptrptr = ptr;
+return rc;
+}
+
+
+
+/*************************************************
+* Match and substitute *
+*************************************************/
+
+/* This function applies a compiled re to a subject string and creates a new
+string with substitutions. The first 7 arguments are the same as for
+pcre2_match(). Either string length may be PCRE2_ZERO_TERMINATED.
+
+Arguments:
+ code points to the compiled expression
+ subject points to the subject string
+ length length of subject string (may contain binary zeros)
+ start_offset where to start in the subject string
+ options option bits
+ match_data points to a match_data block, or is NULL
+ context points a PCRE2 context
+ replacement points to the replacement string
+ rlength length of replacement string
+ buffer where to put the substituted string
+ blength points to length of buffer; updated to length of string
+
+Returns: >= 0 number of substitutions made
+ < 0 an error code
+ PCRE2_ERROR_BADREPLACEMENT means invalid use of $
+*/
+
+/* This macro checks for space in the buffer before copying into it. On
+overflow, either give an error immediately, or keep on, accumulating the
+length. */
+
+#define CHECKMEMCPY(from,length) \
+ if (!overflowed && lengthleft < length) \
+ { \
+ if ((suboptions & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH) == 0) goto NOROOM; \
+ overflowed = TRUE; \
+ extra_needed = length - lengthleft; \
+ } \
+ else if (overflowed) \
+ { \
+ extra_needed += length; \
+ } \
+ else \
+ { \
+ memcpy(buffer + buff_offset, from, CU2BYTES(length)); \
+ buff_offset += length; \
+ lengthleft -= length; \
+ }
+
+/* Here's the function */
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
+ PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
+ pcre2_match_context *mcontext, PCRE2_SPTR replacement, PCRE2_SIZE rlength,
+ PCRE2_UCHAR *buffer, PCRE2_SIZE *blength)
+{
+int rc;
+int subs;
+int forcecase = 0;
+int forcecasereset = 0;
+uint32_t ovector_count;
+uint32_t goptions = 0;
+uint32_t suboptions;
+BOOL match_data_created = FALSE;
+BOOL literal = FALSE;
+BOOL overflowed = FALSE;
+#ifdef SUPPORT_UNICODE
+BOOL utf = (code->overall_options & PCRE2_UTF) != 0;
+#endif
+PCRE2_UCHAR temp[6];
+PCRE2_SPTR ptr;
+PCRE2_SPTR repend;
+PCRE2_SIZE extra_needed = 0;
+PCRE2_SIZE buff_offset, buff_length, lengthleft, fraglength;
+PCRE2_SIZE *ovector;
+PCRE2_SIZE ovecsave[3];
+
+buff_offset = 0;
+lengthleft = buff_length = *blength;
+*blength = PCRE2_UNSET;
+ovecsave[0] = ovecsave[1] = ovecsave[2] = PCRE2_UNSET;
+
+/* Partial matching is not valid. */
+
+if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0)
+ return PCRE2_ERROR_BADOPTION;
+
+/* If no match data block is provided, create one. */
+
+if (match_data == NULL)
+ {
+ pcre2_general_context *gcontext = (mcontext == NULL)?
+ (pcre2_general_context *)code :
+ (pcre2_general_context *)mcontext;
+ match_data = pcre2_match_data_create_from_pattern(code, gcontext);
+ if (match_data == NULL) return PCRE2_ERROR_NOMEMORY;
+ match_data_created = TRUE;
+ }
+ovector = pcre2_get_ovector_pointer(match_data);
+ovector_count = pcre2_get_ovector_count(match_data);
+
+/* Find lengths of zero-terminated strings and the end of the replacement. */
+
+if (length == PCRE2_ZERO_TERMINATED) length = PRIV(strlen)(subject);
+if (rlength == PCRE2_ZERO_TERMINATED) rlength = PRIV(strlen)(replacement);
+repend = replacement + rlength;
+
+/* Check UTF replacement string if necessary. */
+
+#ifdef SUPPORT_UNICODE
+if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
+ {
+ rc = PRIV(valid_utf)(replacement, rlength, &(match_data->rightchar));
+ if (rc != 0)
+ {
+ match_data->leftchar = 0;
+ goto EXIT;
+ }
+ }
+#endif /* SUPPORT_UNICODE */
+
+/* Save the substitute options and remove them from the match options. */
+
+suboptions = options & SUBSTITUTE_OPTIONS;
+options &= ~SUBSTITUTE_OPTIONS;
+
+/* Copy up to the start offset */
+
+if (start_offset > length)
+ {
+ match_data->leftchar = 0;
+ rc = PCRE2_ERROR_BADOFFSET;
+ goto EXIT;
+ }
+CHECKMEMCPY(subject, start_offset);
+
+/* Loop for global substituting. */
+
+subs = 0;
+do
+ {
+ PCRE2_SPTR ptrstack[PTR_STACK_SIZE];
+ uint32_t ptrstackptr = 0;
+
+ rc = pcre2_match(code, subject, length, start_offset, options|goptions,
+ match_data, mcontext);
+
+#ifdef SUPPORT_UNICODE
+ if (utf) options |= PCRE2_NO_UTF_CHECK; /* Only need to check once */
+#endif
+
+ /* Any error other than no match returns the error code. No match when not
+ doing the special after-empty-match global rematch, or when at the end of the
+ subject, breaks the global loop. Otherwise, advance the starting point by one
+ character, copying it to the output, and try again. */
+
+ if (rc < 0)
+ {
+ PCRE2_SIZE save_start;
+
+ if (rc != PCRE2_ERROR_NOMATCH) goto EXIT;
+ if (goptions == 0 || start_offset >= length) break;
+
+ /* Advance by one code point. Then, if CRLF is a valid newline sequence and
+ we have advanced into the middle of it, advance one more code point. In
+ other words, do not start in the middle of CRLF, even if CR and LF on their
+ own are valid newlines. */
+
+ save_start = start_offset++;
+ if (subject[start_offset-1] == CHAR_CR &&
+ code->newline_convention != PCRE2_NEWLINE_CR &&
+ code->newline_convention != PCRE2_NEWLINE_LF &&
+ start_offset < length &&
+ subject[start_offset] == CHAR_LF)
+ start_offset++;
+
+ /* Otherwise, in UTF mode, advance past any secondary code points. */
+
+ else if ((code->overall_options & PCRE2_UTF) != 0)
+ {
+#if PCRE2_CODE_UNIT_WIDTH == 8
+ while (start_offset < length && (subject[start_offset] & 0xc0) == 0x80)
+ start_offset++;
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+ while (start_offset < length &&
+ (subject[start_offset] & 0xfc00) == 0xdc00)
+ start_offset++;
+#endif
+ }
+
+ /* Copy what we have advanced past, reset the special global options, and
+ continue to the next match. */
+
+ fraglength = start_offset - save_start;
+ CHECKMEMCPY(subject + save_start, fraglength);
+ goptions = 0;
+ continue;
+ }
+
+ /* Handle a successful match. Matches that use \K to end before they start
+ or start before the current point in the subject are not supported. */
+
+ if (ovector[1] < ovector[0] || ovector[0] < start_offset)
+ {
+ rc = PCRE2_ERROR_BADSUBSPATTERN;
+ goto EXIT;
+ }
+
+ /* Check for the same match as previous. This is legitimate after matching an
+ empty string that starts after the initial match offset. We have tried again
+ at the match point in case the pattern is one like /(?<=\G.)/ which can never
+ match at its starting point, so running the match achieves the bumpalong. If
+ we do get the same (null) match at the original match point, it isn't such a
+ pattern, so we now do the empty string magic. In all other cases, a repeat
+ match should never occur. */
+
+ if (ovecsave[0] == ovector[0] && ovecsave[1] == ovector[1])
+ {
+ if (ovector[0] == ovector[1] && ovecsave[2] != start_offset)
+ {
+ goptions = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
+ ovecsave[2] = start_offset;
+ continue; /* Back to the top of the loop */
+ }
+ rc = PCRE2_ERROR_INTERNAL_DUPMATCH;
+ goto EXIT;
+ }
+
+ /* Count substitutions with a paranoid check for integer overflow; surely no
+ real call to this function would ever hit this! */
+
+ if (subs == INT_MAX)
+ {
+ rc = PCRE2_ERROR_TOOMANYREPLACE;
+ goto EXIT;
+ }
+ subs++;
+
+ /* Copy the text leading up to the match. */
+
+ if (rc == 0) rc = ovector_count;
+ fraglength = ovector[0] - start_offset;
+ CHECKMEMCPY(subject + start_offset, fraglength);
+
+ /* Process the replacement string. Literal mode is set by \Q, but only in
+ extended mode when backslashes are being interpreted. In extended mode we
+ must handle nested substrings that are to be reprocessed. */
+
+ ptr = replacement;
+ for (;;)
+ {
+ uint32_t ch;
+ unsigned int chlen;
+
+ /* If at the end of a nested substring, pop the stack. */
+
+ if (ptr >= repend)
+ {
+ if (ptrstackptr <= 0) break; /* End of replacement string */
+ repend = ptrstack[--ptrstackptr];
+ ptr = ptrstack[--ptrstackptr];
+ continue;
+ }
+
+ /* Handle the next character */
+
+ if (literal)
+ {
+ if (ptr[0] == CHAR_BACKSLASH && ptr < repend - 1 && ptr[1] == CHAR_E)
+ {
+ literal = FALSE;
+ ptr += 2;
+ continue;
+ }
+ goto LOADLITERAL;
+ }
+
+ /* Not in literal mode. */
+
+ if (*ptr == CHAR_DOLLAR_SIGN)
+ {
+ int group, n;
+ uint32_t special = 0;
+ BOOL inparens;
+ BOOL star;
+ PCRE2_SIZE sublength;
+ PCRE2_SPTR text1_start = NULL;
+ PCRE2_SPTR text1_end = NULL;
+ PCRE2_SPTR text2_start = NULL;
+ PCRE2_SPTR text2_end = NULL;
+ PCRE2_UCHAR next;
+ PCRE2_UCHAR name[33];
+
+ if (++ptr >= repend) goto BAD;
+ if ((next = *ptr) == CHAR_DOLLAR_SIGN) goto LOADLITERAL;
+
+ group = -1;
+ n = 0;
+ inparens = FALSE;
+ star = FALSE;
+
+ if (next == CHAR_LEFT_CURLY_BRACKET)
+ {
+ if (++ptr >= repend) goto BAD;
+ next = *ptr;
+ inparens = TRUE;
+ }
+
+ if (next == CHAR_ASTERISK)
+ {
+ if (++ptr >= repend) goto BAD;
+ next = *ptr;
+ star = TRUE;
+ }
+
+ if (!star && next >= CHAR_0 && next <= CHAR_9)
+ {
+ group = next - CHAR_0;
+ while (++ptr < repend)
+ {
+ next = *ptr;
+ if (next < CHAR_0 || next > CHAR_9) break;
+ group = group * 10 + next - CHAR_0;
+
+ /* A check for a number greater than the hightest captured group
+ is sufficient here; no need for a separate overflow check. If unknown
+ groups are to be treated as unset, just skip over any remaining
+ digits and carry on. */
+
+ if (group > code->top_bracket)
+ {
+ if ((suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
+ {
+ while (++ptr < repend && *ptr >= CHAR_0 && *ptr <= CHAR_9);
+ break;
+ }
+ else
+ {
+ rc = PCRE2_ERROR_NOSUBSTRING;
+ goto PTREXIT;
+ }
+ }
+ }
+ }
+ else
+ {
+ const uint8_t *ctypes = code->tables + ctypes_offset;
+ while (MAX_255(next) && (ctypes[next] & ctype_word) != 0)
+ {
+ name[n++] = next;
+ if (n > 32) goto BAD;
+ if (++ptr >= repend) break;
+ next = *ptr;
+ }
+ if (n == 0) goto BAD;
+ name[n] = 0;
+ }
+
+ /* In extended mode we recognize ${name:+set text:unset text} and
+ ${name:-default text}. */
+
+ if (inparens)
+ {
+ if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
+ !star && ptr < repend - 2 && next == CHAR_COLON)
+ {
+ special = *(++ptr);
+ if (special != CHAR_PLUS && special != CHAR_MINUS)
+ {
+ rc = PCRE2_ERROR_BADSUBSTITUTION;
+ goto PTREXIT;
+ }
+
+ text1_start = ++ptr;
+ rc = find_text_end(code, &ptr, repend, special == CHAR_MINUS);
+ if (rc != 0) goto PTREXIT;
+ text1_end = ptr;
+
+ if (special == CHAR_PLUS && *ptr == CHAR_COLON)
+ {
+ text2_start = ++ptr;
+ rc = find_text_end(code, &ptr, repend, TRUE);
+ if (rc != 0) goto PTREXIT;
+ text2_end = ptr;
+ }
+ }
+
+ else
+ {
+ if (ptr >= repend || *ptr != CHAR_RIGHT_CURLY_BRACKET)
+ {
+ rc = PCRE2_ERROR_REPMISSINGBRACE;
+ goto PTREXIT;
+ }
+ }
+
+ ptr++;
+ }
+
+ /* Have found a syntactically correct group number or name, or *name.
+ Only *MARK is currently recognized. */
+
+ if (star)
+ {
+ if (PRIV(strcmp_c8)(name, STRING_MARK) == 0)
+ {
+ PCRE2_SPTR mark = pcre2_get_mark(match_data);
+ if (mark != NULL)
+ {
+ PCRE2_SPTR mark_start = mark;
+ while (*mark != 0) mark++;
+ fraglength = mark - mark_start;
+ CHECKMEMCPY(mark_start, fraglength);
+ }
+ }
+ else goto BAD;
+ }
+
+ /* Substitute the contents of a group. We don't use substring_copy
+ functions any more, in order to support case forcing. */
+
+ else
+ {
+ PCRE2_SPTR subptr, subptrend;
+
+ /* Find a number for a named group. In case there are duplicate names,
+ search for the first one that is set. If the name is not found when
+ PCRE2_SUBSTITUTE_UNKNOWN_EMPTY is set, set the group number to a
+ non-existent group. */
+
+ if (group < 0)
+ {
+ PCRE2_SPTR first, last, entry;
+ rc = pcre2_substring_nametable_scan(code, name, &first, &last);
+ if (rc == PCRE2_ERROR_NOSUBSTRING &&
+ (suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
+ {
+ group = code->top_bracket + 1;
+ }
+ else
+ {
+ if (rc < 0) goto PTREXIT;
+ for (entry = first; entry <= last; entry += rc)
+ {
+ uint32_t ng = GET2(entry, 0);
+ if (ng < ovector_count)
+ {
+ if (group < 0) group = ng; /* First in ovector */
+ if (ovector[ng*2] != PCRE2_UNSET)
+ {
+ group = ng; /* First that is set */
+ break;
+ }
+ }
+ }
+
+ /* If group is still negative, it means we did not find a group
+ that is in the ovector. Just set the first group. */
+
+ if (group < 0) group = GET2(first, 0);
+ }
+ }
+
+ /* We now have a group that is identified by number. Find the length of
+ the captured string. If a group in a non-special substitution is unset
+ when PCRE2_SUBSTITUTE_UNSET_EMPTY is set, substitute nothing. */
+
+ rc = pcre2_substring_length_bynumber(match_data, group, &sublength);
+ if (rc < 0)
+ {
+ if (rc == PCRE2_ERROR_NOSUBSTRING &&
+ (suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
+ {
+ rc = PCRE2_ERROR_UNSET;
+ }
+ if (rc != PCRE2_ERROR_UNSET) goto PTREXIT; /* Non-unset errors */
+ if (special == 0) /* Plain substitution */
+ {
+ if ((suboptions & PCRE2_SUBSTITUTE_UNSET_EMPTY) != 0) continue;
+ goto PTREXIT; /* Else error */
+ }
+ }
+
+ /* If special is '+' we have a 'set' and possibly an 'unset' text,
+ both of which are reprocessed when used. If special is '-' we have a
+ default text for when the group is unset; it must be reprocessed. */
+
+ if (special != 0)
+ {
+ if (special == CHAR_MINUS)
+ {
+ if (rc == 0) goto LITERAL_SUBSTITUTE;
+ text2_start = text1_start;
+ text2_end = text1_end;
+ }
+
+ if (ptrstackptr >= PTR_STACK_SIZE) goto BAD;
+ ptrstack[ptrstackptr++] = ptr;
+ ptrstack[ptrstackptr++] = repend;
+
+ if (rc == 0)
+ {
+ ptr = text1_start;
+ repend = text1_end;
+ }
+ else
+ {
+ ptr = text2_start;
+ repend = text2_end;
+ }
+ continue;
+ }
+
+ /* Otherwise we have a literal substitution of a group's contents. */
+
+ LITERAL_SUBSTITUTE:
+ subptr = subject + ovector[group*2];
+ subptrend = subject + ovector[group*2 + 1];
+
+ /* Substitute a literal string, possibly forcing alphabetic case. */
+
+ while (subptr < subptrend)
+ {
+ GETCHARINCTEST(ch, subptr);
+ if (forcecase != 0)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t type = UCD_CHARTYPE(ch);
+ if (PRIV(ucp_gentype)[type] == ucp_L &&
+ type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
+ ch = UCD_OTHERCASE(ch);
+ }
+ else
+#endif
+ {
+ if (((code->tables + cbits_offset +
+ ((forcecase > 0)? cbit_upper:cbit_lower)
+ )[ch/8] & (1 << (ch%8))) == 0)
+ ch = (code->tables + fcc_offset)[ch];
+ }
+ forcecase = forcecasereset;
+ }
+
+#ifdef SUPPORT_UNICODE
+ if (utf) chlen = PRIV(ord2utf)(ch, temp); else
+#endif
+ {
+ temp[0] = ch;
+ chlen = 1;
+ }
+ CHECKMEMCPY(temp, chlen);
+ }
+ }
+ }
+
+ /* Handle an escape sequence in extended mode. We can use check_escape()
+ to process \Q, \E, \c, \o, \x and \ followed by non-alphanumerics, but
+ the case-forcing escapes are not supported in pcre2_compile() so must be
+ recognized here. */
+
+ else if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
+ *ptr == CHAR_BACKSLASH)
+ {
+ int errorcode;
+
+ if (ptr < repend - 1) switch (ptr[1])
+ {
+ case CHAR_L:
+ forcecase = forcecasereset = -1;
+ ptr += 2;
+ continue;
+
+ case CHAR_l:
+ forcecase = -1;
+ forcecasereset = 0;
+ ptr += 2;
+ continue;
+
+ case CHAR_U:
+ forcecase = forcecasereset = 1;
+ ptr += 2;
+ continue;
+
+ case CHAR_u:
+ forcecase = 1;
+ forcecasereset = 0;
+ ptr += 2;
+ continue;
+
+ default:
+ break;
+ }
+
+ ptr++; /* Point after \ */
+ rc = PRIV(check_escape)(&ptr, repend, &ch, &errorcode,
+ code->overall_options, FALSE, NULL);
+ if (errorcode != 0) goto BADESCAPE;
+
+ switch(rc)
+ {
+ case ESC_E:
+ forcecase = forcecasereset = 0;
+ continue;
+
+ case ESC_Q:
+ literal = TRUE;
+ continue;
+
+ case 0: /* Data character */
+ goto LITERAL;
+
+ default:
+ goto BADESCAPE;
+ }
+ }
+
+ /* Handle a literal code unit */
+
+ else
+ {
+ LOADLITERAL:
+ GETCHARINCTEST(ch, ptr); /* Get character value, increment pointer */
+
+ LITERAL:
+ if (forcecase != 0)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ uint32_t type = UCD_CHARTYPE(ch);
+ if (PRIV(ucp_gentype)[type] == ucp_L &&
+ type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
+ ch = UCD_OTHERCASE(ch);
+ }
+ else
+#endif
+ {
+ if (((code->tables + cbits_offset +
+ ((forcecase > 0)? cbit_upper:cbit_lower)
+ )[ch/8] & (1 << (ch%8))) == 0)
+ ch = (code->tables + fcc_offset)[ch];
+ }
+ forcecase = forcecasereset;
+ }
+
+#ifdef SUPPORT_UNICODE
+ if (utf) chlen = PRIV(ord2utf)(ch, temp); else
+#endif
+ {
+ temp[0] = ch;
+ chlen = 1;
+ }
+ CHECKMEMCPY(temp, chlen);
+ } /* End handling a literal code unit */
+ } /* End of loop for scanning the replacement. */
+
+ /* The replacement has been copied to the output. Save the details of this
+ match. See above for how this data is used. If we matched an empty string, do
+ the magic for global matches. Finally, update the start offset to point to
+ the rest of the subject string. */
+
+ ovecsave[0] = ovector[0];
+ ovecsave[1] = ovector[1];
+ ovecsave[2] = start_offset;
+
+ goptions = (ovector[0] != ovector[1] || ovector[0] > start_offset)? 0 :
+ PCRE2_ANCHORED|PCRE2_NOTEMPTY_ATSTART;
+ start_offset = ovector[1];
+ } while ((suboptions & PCRE2_SUBSTITUTE_GLOBAL) != 0); /* Repeat "do" loop */
+
+/* Copy the rest of the subject. */
+
+fraglength = length - start_offset;
+CHECKMEMCPY(subject + start_offset, fraglength);
+temp[0] = 0;
+CHECKMEMCPY(temp , 1);
+
+/* If overflowed is set it means the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set,
+and matching has carried on after a full buffer, in order to compute the length
+needed. Otherwise, an overflow generates an immediate error return. */
+
+if (overflowed)
+ {
+ rc = PCRE2_ERROR_NOMEMORY;
+ *blength = buff_length + extra_needed;
+ }
+
+/* After a successful execution, return the number of substitutions and set the
+length of buffer used, excluding the trailing zero. */
+
+else
+ {
+ rc = subs;
+ *blength = buff_offset - 1;
+ }
+
+EXIT:
+if (match_data_created) pcre2_match_data_free(match_data);
+ else match_data->rc = rc;
+return rc;
+
+NOROOM:
+rc = PCRE2_ERROR_NOMEMORY;
+goto EXIT;
+
+BAD:
+rc = PCRE2_ERROR_BADREPLACEMENT;
+goto PTREXIT;
+
+BADESCAPE:
+rc = PCRE2_ERROR_BADREPESCAPE;
+
+PTREXIT:
+*blength = (PCRE2_SIZE)(ptr - replacement);
+goto EXIT;
+}
+
+/* End of pcre2_substitute.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_substring.c b/test/monniaux/pcre2-10.32/pcre2_substring.c
new file mode 100644
index 00000000..ddf5774e
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_substring.c
@@ -0,0 +1,547 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+
+
+/*************************************************
+* Copy named captured string to given buffer *
+*************************************************/
+
+/* This function copies a single captured substring into a given buffer,
+identifying it by name. If the regex permits duplicate names, the first
+substring that is set is chosen.
+
+Arguments:
+ match_data points to the match data
+ stringname the name of the required substring
+ buffer where to put the substring
+ sizeptr the size of the buffer, updated to the size of the substring
+
+Returns: if successful: zero
+ if not successful, a negative error code:
+ (1) an error from nametable_scan()
+ (2) an error from copy_bynumber()
+ (3) PCRE2_ERROR_UNAVAILABLE: no group is in ovector
+ (4) PCRE2_ERROR_UNSET: all named groups in ovector are unset
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_copy_byname(pcre2_match_data *match_data, PCRE2_SPTR stringname,
+ PCRE2_UCHAR *buffer, PCRE2_SIZE *sizeptr)
+{
+PCRE2_SPTR first, last, entry;
+int failrc, entrysize;
+if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
+ return PCRE2_ERROR_DFA_UFUNC;
+entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
+ &first, &last);
+if (entrysize < 0) return entrysize;
+failrc = PCRE2_ERROR_UNAVAILABLE;
+for (entry = first; entry <= last; entry += entrysize)
+ {
+ uint32_t n = GET2(entry, 0);
+ if (n < match_data->oveccount)
+ {
+ if (match_data->ovector[n*2] != PCRE2_UNSET)
+ return pcre2_substring_copy_bynumber(match_data, n, buffer, sizeptr);
+ failrc = PCRE2_ERROR_UNSET;
+ }
+ }
+return failrc;
+}
+
+
+
+/*************************************************
+* Copy numbered captured string to given buffer *
+*************************************************/
+
+/* This function copies a single captured substring into a given buffer,
+identifying it by number.
+
+Arguments:
+ match_data points to the match data
+ stringnumber the number of the required substring
+ buffer where to put the substring
+ sizeptr the size of the buffer, updated to the size of the substring
+
+Returns: if successful: 0
+ if not successful, a negative error code:
+ PCRE2_ERROR_NOMEMORY: buffer too small
+ PCRE2_ERROR_NOSUBSTRING: no such substring
+ PCRE2_ERROR_UNAVAILABLE: ovector too small
+ PCRE2_ERROR_UNSET: substring is not set
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_copy_bynumber(pcre2_match_data *match_data,
+ uint32_t stringnumber, PCRE2_UCHAR *buffer, PCRE2_SIZE *sizeptr)
+{
+int rc;
+PCRE2_SIZE size;
+rc = pcre2_substring_length_bynumber(match_data, stringnumber, &size);
+if (rc < 0) return rc;
+if (size + 1 > *sizeptr) return PCRE2_ERROR_NOMEMORY;
+memcpy(buffer, match_data->subject + match_data->ovector[stringnumber*2],
+ CU2BYTES(size));
+buffer[size] = 0;
+*sizeptr = size;
+return 0;
+}
+
+
+
+/*************************************************
+* Extract named captured string *
+*************************************************/
+
+/* This function copies a single captured substring, identified by name, into
+new memory. If the regex permits duplicate names, the first substring that is
+set is chosen.
+
+Arguments:
+ match_data pointer to match_data
+ stringname the name of the required substring
+ stringptr where to put the pointer to the new memory
+ sizeptr where to put the length of the substring
+
+Returns: if successful: zero
+ if not successful, a negative value:
+ (1) an error from nametable_scan()
+ (2) an error from get_bynumber()
+ (3) PCRE2_ERROR_UNAVAILABLE: no group is in ovector
+ (4) PCRE2_ERROR_UNSET: all named groups in ovector are unset
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_get_byname(pcre2_match_data *match_data,
+ PCRE2_SPTR stringname, PCRE2_UCHAR **stringptr, PCRE2_SIZE *sizeptr)
+{
+PCRE2_SPTR first, last, entry;
+int failrc, entrysize;
+if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
+ return PCRE2_ERROR_DFA_UFUNC;
+entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
+ &first, &last);
+if (entrysize < 0) return entrysize;
+failrc = PCRE2_ERROR_UNAVAILABLE;
+for (entry = first; entry <= last; entry += entrysize)
+ {
+ uint32_t n = GET2(entry, 0);
+ if (n < match_data->oveccount)
+ {
+ if (match_data->ovector[n*2] != PCRE2_UNSET)
+ return pcre2_substring_get_bynumber(match_data, n, stringptr, sizeptr);
+ failrc = PCRE2_ERROR_UNSET;
+ }
+ }
+return failrc;
+}
+
+
+
+/*************************************************
+* Extract captured string to new memory *
+*************************************************/
+
+/* This function copies a single captured substring into a piece of new
+memory.
+
+Arguments:
+ match_data points to match data
+ stringnumber the number of the required substring
+ stringptr where to put a pointer to the new memory
+ sizeptr where to put the size of the substring
+
+Returns: if successful: 0
+ if not successful, a negative error code:
+ PCRE2_ERROR_NOMEMORY: failed to get memory
+ PCRE2_ERROR_NOSUBSTRING: no such substring
+ PCRE2_ERROR_UNAVAILABLE: ovector too small
+ PCRE2_ERROR_UNSET: substring is not set
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_get_bynumber(pcre2_match_data *match_data,
+ uint32_t stringnumber, PCRE2_UCHAR **stringptr, PCRE2_SIZE *sizeptr)
+{
+int rc;
+PCRE2_SIZE size;
+PCRE2_UCHAR *yield;
+rc = pcre2_substring_length_bynumber(match_data, stringnumber, &size);
+if (rc < 0) return rc;
+yield = PRIV(memctl_malloc)(sizeof(pcre2_memctl) +
+ (size + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)match_data);
+if (yield == NULL) return PCRE2_ERROR_NOMEMORY;
+yield = (PCRE2_UCHAR *)(((char *)yield) + sizeof(pcre2_memctl));
+memcpy(yield, match_data->subject + match_data->ovector[stringnumber*2],
+ CU2BYTES(size));
+yield[size] = 0;
+*stringptr = yield;
+*sizeptr = size;
+return 0;
+}
+
+
+
+/*************************************************
+* Free memory obtained by get_substring *
+*************************************************/
+
+/*
+Argument: the result of a previous pcre2_substring_get_byxxx()
+Returns: nothing
+*/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_substring_free(PCRE2_UCHAR *string)
+{
+if (string != NULL)
+ {
+ pcre2_memctl *memctl = (pcre2_memctl *)((char *)string - sizeof(pcre2_memctl));
+ memctl->free(memctl, memctl->memory_data);
+ }
+}
+
+
+
+/*************************************************
+* Get length of a named substring *
+*************************************************/
+
+/* This function returns the length of a named captured substring. If the regex
+permits duplicate names, the first substring that is set is chosen.
+
+Arguments:
+ match_data pointer to match data
+ stringname the name of the required substring
+ sizeptr where to put the length
+
+Returns: 0 if successful, else a negative error number
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_length_byname(pcre2_match_data *match_data,
+ PCRE2_SPTR stringname, PCRE2_SIZE *sizeptr)
+{
+PCRE2_SPTR first, last, entry;
+int failrc, entrysize;
+if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
+ return PCRE2_ERROR_DFA_UFUNC;
+entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
+ &first, &last);
+if (entrysize < 0) return entrysize;
+failrc = PCRE2_ERROR_UNAVAILABLE;
+for (entry = first; entry <= last; entry += entrysize)
+ {
+ uint32_t n = GET2(entry, 0);
+ if (n < match_data->oveccount)
+ {
+ if (match_data->ovector[n*2] != PCRE2_UNSET)
+ return pcre2_substring_length_bynumber(match_data, n, sizeptr);
+ failrc = PCRE2_ERROR_UNSET;
+ }
+ }
+return failrc;
+}
+
+
+
+/*************************************************
+* Get length of a numbered substring *
+*************************************************/
+
+/* This function returns the length of a captured substring. If the start is
+beyond the end (which can happen when \K is used in an assertion), it sets the
+length to zero.
+
+Arguments:
+ match_data pointer to match data
+ stringnumber the number of the required substring
+ sizeptr where to put the length, if not NULL
+
+Returns: if successful: 0
+ if not successful, a negative error code:
+ PCRE2_ERROR_NOSUBSTRING: no such substring
+ PCRE2_ERROR_UNAVAILABLE: ovector is too small
+ PCRE2_ERROR_UNSET: substring is not set
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_length_bynumber(pcre2_match_data *match_data,
+ uint32_t stringnumber, PCRE2_SIZE *sizeptr)
+{
+PCRE2_SIZE left, right;
+int count = match_data->rc;
+if (count == PCRE2_ERROR_PARTIAL)
+ {
+ if (stringnumber > 0) return PCRE2_ERROR_PARTIAL;
+ count = 0;
+ }
+else if (count < 0) return count; /* Match failed */
+
+if (match_data->matchedby != PCRE2_MATCHEDBY_DFA_INTERPRETER)
+ {
+ if (stringnumber > match_data->code->top_bracket)
+ return PCRE2_ERROR_NOSUBSTRING;
+ if (stringnumber >= match_data->oveccount)
+ return PCRE2_ERROR_UNAVAILABLE;
+ if (match_data->ovector[stringnumber*2] == PCRE2_UNSET)
+ return PCRE2_ERROR_UNSET;
+ }
+else /* Matched using pcre2_dfa_match() */
+ {
+ if (stringnumber >= match_data->oveccount) return PCRE2_ERROR_UNAVAILABLE;
+ if (count != 0 && stringnumber >= (uint32_t)count) return PCRE2_ERROR_UNSET;
+ }
+
+left = match_data->ovector[stringnumber*2];
+right = match_data->ovector[stringnumber*2+1];
+if (sizeptr != NULL) *sizeptr = (left > right)? 0 : right - left;
+return 0;
+}
+
+
+
+/*************************************************
+* Extract all captured strings to new memory *
+*************************************************/
+
+/* This function gets one chunk of memory and builds a list of pointers and all
+the captured substrings in it. A NULL pointer is put on the end of the list.
+The substrings are zero-terminated, but also, if the final argument is
+non-NULL, a list of lengths is also returned. This allows binary data to be
+handled.
+
+Arguments:
+ match_data points to the match data
+ listptr set to point to the list of pointers
+ lengthsptr set to point to the list of lengths (may be NULL)
+
+Returns: if successful: 0
+ if not successful, a negative error code:
+ PCRE2_ERROR_NOMEMORY: failed to get memory,
+ or a match failure code
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_list_get(pcre2_match_data *match_data, PCRE2_UCHAR ***listptr,
+ PCRE2_SIZE **lengthsptr)
+{
+int i, count, count2;
+PCRE2_SIZE size;
+PCRE2_SIZE *lensp;
+pcre2_memctl *memp;
+PCRE2_UCHAR **listp;
+PCRE2_UCHAR *sp;
+PCRE2_SIZE *ovector;
+
+if ((count = match_data->rc) < 0) return count; /* Match failed */
+if (count == 0) count = match_data->oveccount; /* Ovector too small */
+
+count2 = 2*count;
+ovector = match_data->ovector;
+size = sizeof(pcre2_memctl) + sizeof(PCRE2_UCHAR *); /* For final NULL */
+if (lengthsptr != NULL) size += sizeof(PCRE2_SIZE) * count; /* For lengths */
+
+for (i = 0; i < count2; i += 2)
+ {
+ size += sizeof(PCRE2_UCHAR *) + CU2BYTES(1);
+ if (ovector[i+1] > ovector[i]) size += CU2BYTES(ovector[i+1] - ovector[i]);
+ }
+
+memp = PRIV(memctl_malloc)(size, (pcre2_memctl *)match_data);
+if (memp == NULL) return PCRE2_ERROR_NOMEMORY;
+
+*listptr = listp = (PCRE2_UCHAR **)((char *)memp + sizeof(pcre2_memctl));
+lensp = (PCRE2_SIZE *)((char *)listp + sizeof(PCRE2_UCHAR *) * (count + 1));
+
+if (lengthsptr == NULL)
+ {
+ sp = (PCRE2_UCHAR *)lensp;
+ lensp = NULL;
+ }
+else
+ {
+ *lengthsptr = lensp;
+ sp = (PCRE2_UCHAR *)((char *)lensp + sizeof(PCRE2_SIZE) * count);
+ }
+
+for (i = 0; i < count2; i += 2)
+ {
+ size = (ovector[i+1] > ovector[i])? (ovector[i+1] - ovector[i]) : 0;
+
+ /* Size == 0 includes the case when the capture is unset. Avoid adding
+ PCRE2_UNSET to match_data->subject because it overflows, even though with
+ zero size calling memcpy() is harmless. */
+
+ if (size != 0) memcpy(sp, match_data->subject + ovector[i], CU2BYTES(size));
+ *listp++ = sp;
+ if (lensp != NULL) *lensp++ = size;
+ sp += size;
+ *sp++ = 0;
+ }
+
+*listp = NULL;
+return 0;
+}
+
+
+
+/*************************************************
+* Free memory obtained by substring_list_get *
+*************************************************/
+
+/*
+Argument: the result of a previous pcre2_substring_list_get()
+Returns: nothing
+*/
+
+PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
+pcre2_substring_list_free(PCRE2_SPTR *list)
+{
+if (list != NULL)
+ {
+ pcre2_memctl *memctl = (pcre2_memctl *)((char *)list - sizeof(pcre2_memctl));
+ memctl->free(memctl, memctl->memory_data);
+ }
+}
+
+
+
+/*************************************************
+* Find (multiple) entries for named string *
+*************************************************/
+
+/* This function scans the nametable for a given name, using binary chop. It
+returns either two pointers to the entries in the table, or, if no pointers are
+given, the number of a unique group with the given name. If duplicate names are
+permitted, and the name is not unique, an error is generated.
+
+Arguments:
+ code the compiled regex
+ stringname the name whose entries required
+ firstptr where to put the pointer to the first entry
+ lastptr where to put the pointer to the last entry
+
+Returns: PCRE2_ERROR_NOSUBSTRING if the name is not found
+ otherwise, if firstptr and lastptr are NULL:
+ a group number for a unique substring
+ else PCRE2_ERROR_NOUNIQUESUBSTRING
+ otherwise:
+ the length of each entry, having set firstptr and lastptr
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_nametable_scan(const pcre2_code *code, PCRE2_SPTR stringname,
+ PCRE2_SPTR *firstptr, PCRE2_SPTR *lastptr)
+{
+uint16_t bot = 0;
+uint16_t top = code->name_count;
+uint16_t entrysize = code->name_entry_size;
+PCRE2_SPTR nametable = (PCRE2_SPTR)((char *)code + sizeof(pcre2_real_code));
+
+while (top > bot)
+ {
+ uint16_t mid = (top + bot) / 2;
+ PCRE2_SPTR entry = nametable + entrysize*mid;
+ int c = PRIV(strcmp)(stringname, entry + IMM2_SIZE);
+ if (c == 0)
+ {
+ PCRE2_SPTR first;
+ PCRE2_SPTR last;
+ PCRE2_SPTR lastentry;
+ lastentry = nametable + entrysize * (code->name_count - 1);
+ first = last = entry;
+ while (first > nametable)
+ {
+ if (PRIV(strcmp)(stringname, (first - entrysize + IMM2_SIZE)) != 0) break;
+ first -= entrysize;
+ }
+ while (last < lastentry)
+ {
+ if (PRIV(strcmp)(stringname, (last + entrysize + IMM2_SIZE)) != 0) break;
+ last += entrysize;
+ }
+ if (firstptr == NULL) return (first == last)?
+ (int)GET2(entry, 0) : PCRE2_ERROR_NOUNIQUESUBSTRING;
+ *firstptr = first;
+ *lastptr = last;
+ return entrysize;
+ }
+ if (c > 0) bot = mid + 1; else top = mid;
+ }
+
+return PCRE2_ERROR_NOSUBSTRING;
+}
+
+
+/*************************************************
+* Find number for named string *
+*************************************************/
+
+/* This function is a convenience wrapper for pcre2_substring_nametable_scan()
+when it is known that names are unique. If there are duplicate names, it is not
+defined which number is returned.
+
+Arguments:
+ code the compiled regex
+ stringname the name whose number is required
+
+Returns: the number of the named parenthesis, or a negative number
+ PCRE2_ERROR_NOSUBSTRING if not found
+ PCRE2_ERROR_NOUNIQUESUBSTRING if not unique
+*/
+
+PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
+pcre2_substring_number_from_name(const pcre2_code *code,
+ PCRE2_SPTR stringname)
+{
+return pcre2_substring_nametable_scan(code, stringname, NULL, NULL);
+}
+
+/* End of pcre2_substring.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_tables.c b/test/monniaux/pcre2-10.32/pcre2_tables.c
new file mode 100644
index 00000000..83d6f9de
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_tables.c
@@ -0,0 +1,827 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains some fixed tables that are used by more than one of the
+PCRE2 code modules. The tables are also #included by the pcre2test program,
+which uses macros to change their names from _pcre2_xxx to xxxx, thereby
+avoiding name clashes with the library. In this case, PCRE2_PCRE2TEST is
+defined. */
+
+#ifndef PCRE2_PCRE2TEST /* We're compiling the library */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "pcre2_internal.h"
+#endif /* PCRE2_PCRE2TEST */
+
+
+/* Table of sizes for the fixed-length opcodes. It's defined in a macro so that
+the definition is next to the definition of the opcodes in pcre2_internal.h.
+This is mode-dependent, so is skipped when this file is included by pcre2test. */
+
+#ifndef PCRE2_PCRE2TEST
+const uint8_t PRIV(OP_lengths)[] = { OP_LENGTHS };
+#endif
+
+/* Tables of horizontal and vertical whitespace characters, suitable for
+adding to classes. */
+
+const uint32_t PRIV(hspace_list)[] = { HSPACE_LIST };
+const uint32_t PRIV(vspace_list)[] = { VSPACE_LIST };
+
+/* These tables are the pairs of delimiters that are valid for callout string
+arguments. For each starting delimiter there must be a matching ending
+delimiter, which in fact is different only for bracket-like delimiters. */
+
+const uint32_t PRIV(callout_start_delims)[] = {
+ CHAR_GRAVE_ACCENT, CHAR_APOSTROPHE, CHAR_QUOTATION_MARK,
+ CHAR_CIRCUMFLEX_ACCENT, CHAR_PERCENT_SIGN, CHAR_NUMBER_SIGN,
+ CHAR_DOLLAR_SIGN, CHAR_LEFT_CURLY_BRACKET, 0 };
+
+const uint32_t PRIV(callout_end_delims[]) = {
+ CHAR_GRAVE_ACCENT, CHAR_APOSTROPHE, CHAR_QUOTATION_MARK,
+ CHAR_CIRCUMFLEX_ACCENT, CHAR_PERCENT_SIGN, CHAR_NUMBER_SIGN,
+ CHAR_DOLLAR_SIGN, CHAR_RIGHT_CURLY_BRACKET, 0 };
+
+
+/*************************************************
+* Tables for UTF-8 support *
+*************************************************/
+
+/* These tables are required by pcre2test in 16- or 32-bit mode, as well
+as for the library in 8-bit mode, because pcre2test uses UTF-8 internally for
+handling wide characters. */
+
+#if defined PCRE2_PCRE2TEST || \
+ (defined SUPPORT_UNICODE && \
+ defined PCRE2_CODE_UNIT_WIDTH && \
+ PCRE2_CODE_UNIT_WIDTH == 8)
+
+/* These are the breakpoints for different numbers of bytes in a UTF-8
+character. */
+
+const int PRIV(utf8_table1)[] =
+ { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff};
+
+const int PRIV(utf8_table1_size) = sizeof(PRIV(utf8_table1)) / sizeof(int);
+
+/* These are the indicator bits and the mask for the data bits to set in the
+first byte of a character, indexed by the number of additional bytes. */
+
+const int PRIV(utf8_table2)[] = { 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
+const int PRIV(utf8_table3)[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
+
+/* Table of the number of extra bytes, indexed by the first byte masked with
+0x3f. The highest number for a valid UTF-8 first byte is in fact 0x3d. */
+
+const uint8_t PRIV(utf8_table4)[] = {
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 };
+
+#endif /* UTF-8 support needed */
+
+
+#ifdef SUPPORT_UNICODE
+
+/* Table to translate from particular type value to the general value. */
+
+const uint32_t PRIV(ucp_gentype)[] = {
+ ucp_C, ucp_C, ucp_C, ucp_C, ucp_C, /* Cc, Cf, Cn, Co, Cs */
+ ucp_L, ucp_L, ucp_L, ucp_L, ucp_L, /* Ll, Lu, Lm, Lo, Lt */
+ ucp_M, ucp_M, ucp_M, /* Mc, Me, Mn */
+ ucp_N, ucp_N, ucp_N, /* Nd, Nl, No */
+ ucp_P, ucp_P, ucp_P, ucp_P, ucp_P, /* Pc, Pd, Pe, Pf, Pi */
+ ucp_P, ucp_P, /* Ps, Po */
+ ucp_S, ucp_S, ucp_S, ucp_S, /* Sc, Sk, Sm, So */
+ ucp_Z, ucp_Z, ucp_Z /* Zl, Zp, Zs */
+};
+
+/* This table encodes the rules for finding the end of an extended grapheme
+cluster. Every code point has a grapheme break property which is one of the
+ucp_gbXX values defined in pcre2_ucp.h. These changed between Unicode versions
+10 and 11. The 2-dimensional table is indexed by the properties of two adjacent
+code points. The left property selects a word from the table, and the right
+property selects a bit from that word like this:
+
+ PRIV(ucp_gbtable)[left-property] & (1 << right-property)
+
+The value is non-zero if a grapheme break is NOT permitted between the relevant
+two code points. The breaking rules are as follows:
+
+1. Break at the start and end of text (pretty obviously).
+
+2. Do not break between a CR and LF; otherwise, break before and after
+ controls.
+
+3. Do not break Hangul syllable sequences, the rules for which are:
+
+ L may be followed by L, V, LV or LVT
+ LV or V may be followed by V or T
+ LVT or T may be followed by T
+
+4. Do not break before extending characters or zero-width-joiner (ZWJ).
+
+The following rules are only for extended grapheme clusters (but that's what we
+are implementing).
+
+5. Do not break before SpacingMarks.
+
+6. Do not break after Prepend characters.
+
+7. Do not break within emoji modifier sequences or emoji zwj sequences. That
+ is, do not break between characters with the Extended_Pictographic property.
+ Extend and ZWJ characters are allowed between the characters; this cannot be
+ represented in this table, the code has to deal with it.
+
+8. Do not break within emoji flag sequences. That is, do not break between
+ regional indicator (RI) symbols if there are an odd number of RI characters
+ before the break point. This table encodes "join RI characters"; the code
+ has to deal with checking for previous adjoining RIs.
+
+9. Otherwise, break everywhere.
+*/
+
+#define ESZ (1<<ucp_gbExtend)|(1<<ucp_gbSpacingMark)|(1<<ucp_gbZWJ)
+
+const uint32_t PRIV(ucp_gbtable)[] = {
+ (1<<ucp_gbLF), /* 0 CR */
+ 0, /* 1 LF */
+ 0, /* 2 Control */
+ ESZ, /* 3 Extend */
+ ESZ|(1<<ucp_gbPrepend)| /* 4 Prepend */
+ (1<<ucp_gbL)|(1<<ucp_gbV)|(1<<ucp_gbT)|
+ (1<<ucp_gbLV)|(1<<ucp_gbLVT)|(1<<ucp_gbOther)|
+ (1<<ucp_gbRegionalIndicator),
+ ESZ, /* 5 SpacingMark */
+ ESZ|(1<<ucp_gbL)|(1<<ucp_gbV)|(1<<ucp_gbLV)| /* 6 L */
+ (1<<ucp_gbLVT),
+ ESZ|(1<<ucp_gbV)|(1<<ucp_gbT), /* 7 V */
+ ESZ|(1<<ucp_gbT), /* 8 T */
+ ESZ|(1<<ucp_gbV)|(1<<ucp_gbT), /* 9 LV */
+ ESZ|(1<<ucp_gbT), /* 10 LVT */
+ (1<<ucp_gbRegionalIndicator), /* 11 RegionalIndicator */
+ ESZ, /* 12 Other */
+ ESZ, /* 13 ZWJ */
+ ESZ|(1<<ucp_gbExtended_Pictographic) /* 14 Extended Pictographic */
+};
+
+#undef ESZ
+
+#ifdef SUPPORT_JIT
+/* This table reverses PRIV(ucp_gentype). We can save the cost
+of a memory load. */
+
+const int PRIV(ucp_typerange)[] = {
+ ucp_Cc, ucp_Cs,
+ ucp_Ll, ucp_Lu,
+ ucp_Mc, ucp_Mn,
+ ucp_Nd, ucp_No,
+ ucp_Pc, ucp_Ps,
+ ucp_Sc, ucp_So,
+ ucp_Zl, ucp_Zs,
+};
+#endif /* SUPPORT_JIT */
+
+/* The PRIV(utt)[] table below translates Unicode property names into type and
+code values. It is searched by binary chop, so must be in collating sequence of
+name. Originally, the table contained pointers to the name strings in the first
+field of each entry. However, that leads to a large number of relocations when
+a shared library is dynamically loaded. A significant reduction is made by
+putting all the names into a single, large string and then using offsets in the
+table itself. Maintenance is more error-prone, but frequent changes to this
+data are unlikely.
+
+July 2008: There is now a script called maint/GenerateUtt.py that can be used
+to generate this data automatically instead of maintaining it by hand.
+
+The script was updated in March 2009 to generate a new EBCDIC-compliant
+version. Like all other character and string literals that are compared against
+the regular expression pattern, we must use STR_ macros instead of literal
+strings to make sure that UTF-8 support works on EBCDIC platforms. */
+
+#define STRING_Adlam0 STR_A STR_d STR_l STR_a STR_m "\0"
+#define STRING_Ahom0 STR_A STR_h STR_o STR_m "\0"
+#define STRING_Anatolian_Hieroglyphs0 STR_A STR_n STR_a STR_t STR_o STR_l STR_i STR_a STR_n STR_UNDERSCORE STR_H STR_i STR_e STR_r STR_o STR_g STR_l STR_y STR_p STR_h STR_s "\0"
+#define STRING_Any0 STR_A STR_n STR_y "\0"
+#define STRING_Arabic0 STR_A STR_r STR_a STR_b STR_i STR_c "\0"
+#define STRING_Armenian0 STR_A STR_r STR_m STR_e STR_n STR_i STR_a STR_n "\0"
+#define STRING_Avestan0 STR_A STR_v STR_e STR_s STR_t STR_a STR_n "\0"
+#define STRING_Balinese0 STR_B STR_a STR_l STR_i STR_n STR_e STR_s STR_e "\0"
+#define STRING_Bamum0 STR_B STR_a STR_m STR_u STR_m "\0"
+#define STRING_Bassa_Vah0 STR_B STR_a STR_s STR_s STR_a STR_UNDERSCORE STR_V STR_a STR_h "\0"
+#define STRING_Batak0 STR_B STR_a STR_t STR_a STR_k "\0"
+#define STRING_Bengali0 STR_B STR_e STR_n STR_g STR_a STR_l STR_i "\0"
+#define STRING_Bhaiksuki0 STR_B STR_h STR_a STR_i STR_k STR_s STR_u STR_k STR_i "\0"
+#define STRING_Bopomofo0 STR_B STR_o STR_p STR_o STR_m STR_o STR_f STR_o "\0"
+#define STRING_Brahmi0 STR_B STR_r STR_a STR_h STR_m STR_i "\0"
+#define STRING_Braille0 STR_B STR_r STR_a STR_i STR_l STR_l STR_e "\0"
+#define STRING_Buginese0 STR_B STR_u STR_g STR_i STR_n STR_e STR_s STR_e "\0"
+#define STRING_Buhid0 STR_B STR_u STR_h STR_i STR_d "\0"
+#define STRING_C0 STR_C "\0"
+#define STRING_Canadian_Aboriginal0 STR_C STR_a STR_n STR_a STR_d STR_i STR_a STR_n STR_UNDERSCORE STR_A STR_b STR_o STR_r STR_i STR_g STR_i STR_n STR_a STR_l "\0"
+#define STRING_Carian0 STR_C STR_a STR_r STR_i STR_a STR_n "\0"
+#define STRING_Caucasian_Albanian0 STR_C STR_a STR_u STR_c STR_a STR_s STR_i STR_a STR_n STR_UNDERSCORE STR_A STR_l STR_b STR_a STR_n STR_i STR_a STR_n "\0"
+#define STRING_Cc0 STR_C STR_c "\0"
+#define STRING_Cf0 STR_C STR_f "\0"
+#define STRING_Chakma0 STR_C STR_h STR_a STR_k STR_m STR_a "\0"
+#define STRING_Cham0 STR_C STR_h STR_a STR_m "\0"
+#define STRING_Cherokee0 STR_C STR_h STR_e STR_r STR_o STR_k STR_e STR_e "\0"
+#define STRING_Cn0 STR_C STR_n "\0"
+#define STRING_Co0 STR_C STR_o "\0"
+#define STRING_Common0 STR_C STR_o STR_m STR_m STR_o STR_n "\0"
+#define STRING_Coptic0 STR_C STR_o STR_p STR_t STR_i STR_c "\0"
+#define STRING_Cs0 STR_C STR_s "\0"
+#define STRING_Cuneiform0 STR_C STR_u STR_n STR_e STR_i STR_f STR_o STR_r STR_m "\0"
+#define STRING_Cypriot0 STR_C STR_y STR_p STR_r STR_i STR_o STR_t "\0"
+#define STRING_Cyrillic0 STR_C STR_y STR_r STR_i STR_l STR_l STR_i STR_c "\0"
+#define STRING_Deseret0 STR_D STR_e STR_s STR_e STR_r STR_e STR_t "\0"
+#define STRING_Devanagari0 STR_D STR_e STR_v STR_a STR_n STR_a STR_g STR_a STR_r STR_i "\0"
+#define STRING_Dogra0 STR_D STR_o STR_g STR_r STR_a "\0"
+#define STRING_Duployan0 STR_D STR_u STR_p STR_l STR_o STR_y STR_a STR_n "\0"
+#define STRING_Egyptian_Hieroglyphs0 STR_E STR_g STR_y STR_p STR_t STR_i STR_a STR_n STR_UNDERSCORE STR_H STR_i STR_e STR_r STR_o STR_g STR_l STR_y STR_p STR_h STR_s "\0"
+#define STRING_Elbasan0 STR_E STR_l STR_b STR_a STR_s STR_a STR_n "\0"
+#define STRING_Ethiopic0 STR_E STR_t STR_h STR_i STR_o STR_p STR_i STR_c "\0"
+#define STRING_Georgian0 STR_G STR_e STR_o STR_r STR_g STR_i STR_a STR_n "\0"
+#define STRING_Glagolitic0 STR_G STR_l STR_a STR_g STR_o STR_l STR_i STR_t STR_i STR_c "\0"
+#define STRING_Gothic0 STR_G STR_o STR_t STR_h STR_i STR_c "\0"
+#define STRING_Grantha0 STR_G STR_r STR_a STR_n STR_t STR_h STR_a "\0"
+#define STRING_Greek0 STR_G STR_r STR_e STR_e STR_k "\0"
+#define STRING_Gujarati0 STR_G STR_u STR_j STR_a STR_r STR_a STR_t STR_i "\0"
+#define STRING_Gunjala_Gondi0 STR_G STR_u STR_n STR_j STR_a STR_l STR_a STR_UNDERSCORE STR_G STR_o STR_n STR_d STR_i "\0"
+#define STRING_Gurmukhi0 STR_G STR_u STR_r STR_m STR_u STR_k STR_h STR_i "\0"
+#define STRING_Han0 STR_H STR_a STR_n "\0"
+#define STRING_Hangul0 STR_H STR_a STR_n STR_g STR_u STR_l "\0"
+#define STRING_Hanifi_Rohingya0 STR_H STR_a STR_n STR_i STR_f STR_i STR_UNDERSCORE STR_R STR_o STR_h STR_i STR_n STR_g STR_y STR_a "\0"
+#define STRING_Hanunoo0 STR_H STR_a STR_n STR_u STR_n STR_o STR_o "\0"
+#define STRING_Hatran0 STR_H STR_a STR_t STR_r STR_a STR_n "\0"
+#define STRING_Hebrew0 STR_H STR_e STR_b STR_r STR_e STR_w "\0"
+#define STRING_Hiragana0 STR_H STR_i STR_r STR_a STR_g STR_a STR_n STR_a "\0"
+#define STRING_Imperial_Aramaic0 STR_I STR_m STR_p STR_e STR_r STR_i STR_a STR_l STR_UNDERSCORE STR_A STR_r STR_a STR_m STR_a STR_i STR_c "\0"
+#define STRING_Inherited0 STR_I STR_n STR_h STR_e STR_r STR_i STR_t STR_e STR_d "\0"
+#define STRING_Inscriptional_Pahlavi0 STR_I STR_n STR_s STR_c STR_r STR_i STR_p STR_t STR_i STR_o STR_n STR_a STR_l STR_UNDERSCORE STR_P STR_a STR_h STR_l STR_a STR_v STR_i "\0"
+#define STRING_Inscriptional_Parthian0 STR_I STR_n STR_s STR_c STR_r STR_i STR_p STR_t STR_i STR_o STR_n STR_a STR_l STR_UNDERSCORE STR_P STR_a STR_r STR_t STR_h STR_i STR_a STR_n "\0"
+#define STRING_Javanese0 STR_J STR_a STR_v STR_a STR_n STR_e STR_s STR_e "\0"
+#define STRING_Kaithi0 STR_K STR_a STR_i STR_t STR_h STR_i "\0"
+#define STRING_Kannada0 STR_K STR_a STR_n STR_n STR_a STR_d STR_a "\0"
+#define STRING_Katakana0 STR_K STR_a STR_t STR_a STR_k STR_a STR_n STR_a "\0"
+#define STRING_Kayah_Li0 STR_K STR_a STR_y STR_a STR_h STR_UNDERSCORE STR_L STR_i "\0"
+#define STRING_Kharoshthi0 STR_K STR_h STR_a STR_r STR_o STR_s STR_h STR_t STR_h STR_i "\0"
+#define STRING_Khmer0 STR_K STR_h STR_m STR_e STR_r "\0"
+#define STRING_Khojki0 STR_K STR_h STR_o STR_j STR_k STR_i "\0"
+#define STRING_Khudawadi0 STR_K STR_h STR_u STR_d STR_a STR_w STR_a STR_d STR_i "\0"
+#define STRING_L0 STR_L "\0"
+#define STRING_L_AMPERSAND0 STR_L STR_AMPERSAND "\0"
+#define STRING_Lao0 STR_L STR_a STR_o "\0"
+#define STRING_Latin0 STR_L STR_a STR_t STR_i STR_n "\0"
+#define STRING_Lepcha0 STR_L STR_e STR_p STR_c STR_h STR_a "\0"
+#define STRING_Limbu0 STR_L STR_i STR_m STR_b STR_u "\0"
+#define STRING_Linear_A0 STR_L STR_i STR_n STR_e STR_a STR_r STR_UNDERSCORE STR_A "\0"
+#define STRING_Linear_B0 STR_L STR_i STR_n STR_e STR_a STR_r STR_UNDERSCORE STR_B "\0"
+#define STRING_Lisu0 STR_L STR_i STR_s STR_u "\0"
+#define STRING_Ll0 STR_L STR_l "\0"
+#define STRING_Lm0 STR_L STR_m "\0"
+#define STRING_Lo0 STR_L STR_o "\0"
+#define STRING_Lt0 STR_L STR_t "\0"
+#define STRING_Lu0 STR_L STR_u "\0"
+#define STRING_Lycian0 STR_L STR_y STR_c STR_i STR_a STR_n "\0"
+#define STRING_Lydian0 STR_L STR_y STR_d STR_i STR_a STR_n "\0"
+#define STRING_M0 STR_M "\0"
+#define STRING_Mahajani0 STR_M STR_a STR_h STR_a STR_j STR_a STR_n STR_i "\0"
+#define STRING_Makasar0 STR_M STR_a STR_k STR_a STR_s STR_a STR_r "\0"
+#define STRING_Malayalam0 STR_M STR_a STR_l STR_a STR_y STR_a STR_l STR_a STR_m "\0"
+#define STRING_Mandaic0 STR_M STR_a STR_n STR_d STR_a STR_i STR_c "\0"
+#define STRING_Manichaean0 STR_M STR_a STR_n STR_i STR_c STR_h STR_a STR_e STR_a STR_n "\0"
+#define STRING_Marchen0 STR_M STR_a STR_r STR_c STR_h STR_e STR_n "\0"
+#define STRING_Masaram_Gondi0 STR_M STR_a STR_s STR_a STR_r STR_a STR_m STR_UNDERSCORE STR_G STR_o STR_n STR_d STR_i "\0"
+#define STRING_Mc0 STR_M STR_c "\0"
+#define STRING_Me0 STR_M STR_e "\0"
+#define STRING_Medefaidrin0 STR_M STR_e STR_d STR_e STR_f STR_a STR_i STR_d STR_r STR_i STR_n "\0"
+#define STRING_Meetei_Mayek0 STR_M STR_e STR_e STR_t STR_e STR_i STR_UNDERSCORE STR_M STR_a STR_y STR_e STR_k "\0"
+#define STRING_Mende_Kikakui0 STR_M STR_e STR_n STR_d STR_e STR_UNDERSCORE STR_K STR_i STR_k STR_a STR_k STR_u STR_i "\0"
+#define STRING_Meroitic_Cursive0 STR_M STR_e STR_r STR_o STR_i STR_t STR_i STR_c STR_UNDERSCORE STR_C STR_u STR_r STR_s STR_i STR_v STR_e "\0"
+#define STRING_Meroitic_Hieroglyphs0 STR_M STR_e STR_r STR_o STR_i STR_t STR_i STR_c STR_UNDERSCORE STR_H STR_i STR_e STR_r STR_o STR_g STR_l STR_y STR_p STR_h STR_s "\0"
+#define STRING_Miao0 STR_M STR_i STR_a STR_o "\0"
+#define STRING_Mn0 STR_M STR_n "\0"
+#define STRING_Modi0 STR_M STR_o STR_d STR_i "\0"
+#define STRING_Mongolian0 STR_M STR_o STR_n STR_g STR_o STR_l STR_i STR_a STR_n "\0"
+#define STRING_Mro0 STR_M STR_r STR_o "\0"
+#define STRING_Multani0 STR_M STR_u STR_l STR_t STR_a STR_n STR_i "\0"
+#define STRING_Myanmar0 STR_M STR_y STR_a STR_n STR_m STR_a STR_r "\0"
+#define STRING_N0 STR_N "\0"
+#define STRING_Nabataean0 STR_N STR_a STR_b STR_a STR_t STR_a STR_e STR_a STR_n "\0"
+#define STRING_Nd0 STR_N STR_d "\0"
+#define STRING_New_Tai_Lue0 STR_N STR_e STR_w STR_UNDERSCORE STR_T STR_a STR_i STR_UNDERSCORE STR_L STR_u STR_e "\0"
+#define STRING_Newa0 STR_N STR_e STR_w STR_a "\0"
+#define STRING_Nko0 STR_N STR_k STR_o "\0"
+#define STRING_Nl0 STR_N STR_l "\0"
+#define STRING_No0 STR_N STR_o "\0"
+#define STRING_Nushu0 STR_N STR_u STR_s STR_h STR_u "\0"
+#define STRING_Ogham0 STR_O STR_g STR_h STR_a STR_m "\0"
+#define STRING_Ol_Chiki0 STR_O STR_l STR_UNDERSCORE STR_C STR_h STR_i STR_k STR_i "\0"
+#define STRING_Old_Hungarian0 STR_O STR_l STR_d STR_UNDERSCORE STR_H STR_u STR_n STR_g STR_a STR_r STR_i STR_a STR_n "\0"
+#define STRING_Old_Italic0 STR_O STR_l STR_d STR_UNDERSCORE STR_I STR_t STR_a STR_l STR_i STR_c "\0"
+#define STRING_Old_North_Arabian0 STR_O STR_l STR_d STR_UNDERSCORE STR_N STR_o STR_r STR_t STR_h STR_UNDERSCORE STR_A STR_r STR_a STR_b STR_i STR_a STR_n "\0"
+#define STRING_Old_Permic0 STR_O STR_l STR_d STR_UNDERSCORE STR_P STR_e STR_r STR_m STR_i STR_c "\0"
+#define STRING_Old_Persian0 STR_O STR_l STR_d STR_UNDERSCORE STR_P STR_e STR_r STR_s STR_i STR_a STR_n "\0"
+#define STRING_Old_Sogdian0 STR_O STR_l STR_d STR_UNDERSCORE STR_S STR_o STR_g STR_d STR_i STR_a STR_n "\0"
+#define STRING_Old_South_Arabian0 STR_O STR_l STR_d STR_UNDERSCORE STR_S STR_o STR_u STR_t STR_h STR_UNDERSCORE STR_A STR_r STR_a STR_b STR_i STR_a STR_n "\0"
+#define STRING_Old_Turkic0 STR_O STR_l STR_d STR_UNDERSCORE STR_T STR_u STR_r STR_k STR_i STR_c "\0"
+#define STRING_Oriya0 STR_O STR_r STR_i STR_y STR_a "\0"
+#define STRING_Osage0 STR_O STR_s STR_a STR_g STR_e "\0"
+#define STRING_Osmanya0 STR_O STR_s STR_m STR_a STR_n STR_y STR_a "\0"
+#define STRING_P0 STR_P "\0"
+#define STRING_Pahawh_Hmong0 STR_P STR_a STR_h STR_a STR_w STR_h STR_UNDERSCORE STR_H STR_m STR_o STR_n STR_g "\0"
+#define STRING_Palmyrene0 STR_P STR_a STR_l STR_m STR_y STR_r STR_e STR_n STR_e "\0"
+#define STRING_Pau_Cin_Hau0 STR_P STR_a STR_u STR_UNDERSCORE STR_C STR_i STR_n STR_UNDERSCORE STR_H STR_a STR_u "\0"
+#define STRING_Pc0 STR_P STR_c "\0"
+#define STRING_Pd0 STR_P STR_d "\0"
+#define STRING_Pe0 STR_P STR_e "\0"
+#define STRING_Pf0 STR_P STR_f "\0"
+#define STRING_Phags_Pa0 STR_P STR_h STR_a STR_g STR_s STR_UNDERSCORE STR_P STR_a "\0"
+#define STRING_Phoenician0 STR_P STR_h STR_o STR_e STR_n STR_i STR_c STR_i STR_a STR_n "\0"
+#define STRING_Pi0 STR_P STR_i "\0"
+#define STRING_Po0 STR_P STR_o "\0"
+#define STRING_Ps0 STR_P STR_s "\0"
+#define STRING_Psalter_Pahlavi0 STR_P STR_s STR_a STR_l STR_t STR_e STR_r STR_UNDERSCORE STR_P STR_a STR_h STR_l STR_a STR_v STR_i "\0"
+#define STRING_Rejang0 STR_R STR_e STR_j STR_a STR_n STR_g "\0"
+#define STRING_Runic0 STR_R STR_u STR_n STR_i STR_c "\0"
+#define STRING_S0 STR_S "\0"
+#define STRING_Samaritan0 STR_S STR_a STR_m STR_a STR_r STR_i STR_t STR_a STR_n "\0"
+#define STRING_Saurashtra0 STR_S STR_a STR_u STR_r STR_a STR_s STR_h STR_t STR_r STR_a "\0"
+#define STRING_Sc0 STR_S STR_c "\0"
+#define STRING_Sharada0 STR_S STR_h STR_a STR_r STR_a STR_d STR_a "\0"
+#define STRING_Shavian0 STR_S STR_h STR_a STR_v STR_i STR_a STR_n "\0"
+#define STRING_Siddham0 STR_S STR_i STR_d STR_d STR_h STR_a STR_m "\0"
+#define STRING_SignWriting0 STR_S STR_i STR_g STR_n STR_W STR_r STR_i STR_t STR_i STR_n STR_g "\0"
+#define STRING_Sinhala0 STR_S STR_i STR_n STR_h STR_a STR_l STR_a "\0"
+#define STRING_Sk0 STR_S STR_k "\0"
+#define STRING_Sm0 STR_S STR_m "\0"
+#define STRING_So0 STR_S STR_o "\0"
+#define STRING_Sogdian0 STR_S STR_o STR_g STR_d STR_i STR_a STR_n "\0"
+#define STRING_Sora_Sompeng0 STR_S STR_o STR_r STR_a STR_UNDERSCORE STR_S STR_o STR_m STR_p STR_e STR_n STR_g "\0"
+#define STRING_Soyombo0 STR_S STR_o STR_y STR_o STR_m STR_b STR_o "\0"
+#define STRING_Sundanese0 STR_S STR_u STR_n STR_d STR_a STR_n STR_e STR_s STR_e "\0"
+#define STRING_Syloti_Nagri0 STR_S STR_y STR_l STR_o STR_t STR_i STR_UNDERSCORE STR_N STR_a STR_g STR_r STR_i "\0"
+#define STRING_Syriac0 STR_S STR_y STR_r STR_i STR_a STR_c "\0"
+#define STRING_Tagalog0 STR_T STR_a STR_g STR_a STR_l STR_o STR_g "\0"
+#define STRING_Tagbanwa0 STR_T STR_a STR_g STR_b STR_a STR_n STR_w STR_a "\0"
+#define STRING_Tai_Le0 STR_T STR_a STR_i STR_UNDERSCORE STR_L STR_e "\0"
+#define STRING_Tai_Tham0 STR_T STR_a STR_i STR_UNDERSCORE STR_T STR_h STR_a STR_m "\0"
+#define STRING_Tai_Viet0 STR_T STR_a STR_i STR_UNDERSCORE STR_V STR_i STR_e STR_t "\0"
+#define STRING_Takri0 STR_T STR_a STR_k STR_r STR_i "\0"
+#define STRING_Tamil0 STR_T STR_a STR_m STR_i STR_l "\0"
+#define STRING_Tangut0 STR_T STR_a STR_n STR_g STR_u STR_t "\0"
+#define STRING_Telugu0 STR_T STR_e STR_l STR_u STR_g STR_u "\0"
+#define STRING_Thaana0 STR_T STR_h STR_a STR_a STR_n STR_a "\0"
+#define STRING_Thai0 STR_T STR_h STR_a STR_i "\0"
+#define STRING_Tibetan0 STR_T STR_i STR_b STR_e STR_t STR_a STR_n "\0"
+#define STRING_Tifinagh0 STR_T STR_i STR_f STR_i STR_n STR_a STR_g STR_h "\0"
+#define STRING_Tirhuta0 STR_T STR_i STR_r STR_h STR_u STR_t STR_a "\0"
+#define STRING_Ugaritic0 STR_U STR_g STR_a STR_r STR_i STR_t STR_i STR_c "\0"
+#define STRING_Vai0 STR_V STR_a STR_i "\0"
+#define STRING_Warang_Citi0 STR_W STR_a STR_r STR_a STR_n STR_g STR_UNDERSCORE STR_C STR_i STR_t STR_i "\0"
+#define STRING_Xan0 STR_X STR_a STR_n "\0"
+#define STRING_Xps0 STR_X STR_p STR_s "\0"
+#define STRING_Xsp0 STR_X STR_s STR_p "\0"
+#define STRING_Xuc0 STR_X STR_u STR_c "\0"
+#define STRING_Xwd0 STR_X STR_w STR_d "\0"
+#define STRING_Yi0 STR_Y STR_i "\0"
+#define STRING_Z0 STR_Z "\0"
+#define STRING_Zanabazar_Square0 STR_Z STR_a STR_n STR_a STR_b STR_a STR_z STR_a STR_r STR_UNDERSCORE STR_S STR_q STR_u STR_a STR_r STR_e "\0"
+#define STRING_Zl0 STR_Z STR_l "\0"
+#define STRING_Zp0 STR_Z STR_p "\0"
+#define STRING_Zs0 STR_Z STR_s "\0"
+
+const char PRIV(utt_names)[] =
+ STRING_Adlam0
+ STRING_Ahom0
+ STRING_Anatolian_Hieroglyphs0
+ STRING_Any0
+ STRING_Arabic0
+ STRING_Armenian0
+ STRING_Avestan0
+ STRING_Balinese0
+ STRING_Bamum0
+ STRING_Bassa_Vah0
+ STRING_Batak0
+ STRING_Bengali0
+ STRING_Bhaiksuki0
+ STRING_Bopomofo0
+ STRING_Brahmi0
+ STRING_Braille0
+ STRING_Buginese0
+ STRING_Buhid0
+ STRING_C0
+ STRING_Canadian_Aboriginal0
+ STRING_Carian0
+ STRING_Caucasian_Albanian0
+ STRING_Cc0
+ STRING_Cf0
+ STRING_Chakma0
+ STRING_Cham0
+ STRING_Cherokee0
+ STRING_Cn0
+ STRING_Co0
+ STRING_Common0
+ STRING_Coptic0
+ STRING_Cs0
+ STRING_Cuneiform0
+ STRING_Cypriot0
+ STRING_Cyrillic0
+ STRING_Deseret0
+ STRING_Devanagari0
+ STRING_Dogra0
+ STRING_Duployan0
+ STRING_Egyptian_Hieroglyphs0
+ STRING_Elbasan0
+ STRING_Ethiopic0
+ STRING_Georgian0
+ STRING_Glagolitic0
+ STRING_Gothic0
+ STRING_Grantha0
+ STRING_Greek0
+ STRING_Gujarati0
+ STRING_Gunjala_Gondi0
+ STRING_Gurmukhi0
+ STRING_Han0
+ STRING_Hangul0
+ STRING_Hanifi_Rohingya0
+ STRING_Hanunoo0
+ STRING_Hatran0
+ STRING_Hebrew0
+ STRING_Hiragana0
+ STRING_Imperial_Aramaic0
+ STRING_Inherited0
+ STRING_Inscriptional_Pahlavi0
+ STRING_Inscriptional_Parthian0
+ STRING_Javanese0
+ STRING_Kaithi0
+ STRING_Kannada0
+ STRING_Katakana0
+ STRING_Kayah_Li0
+ STRING_Kharoshthi0
+ STRING_Khmer0
+ STRING_Khojki0
+ STRING_Khudawadi0
+ STRING_L0
+ STRING_L_AMPERSAND0
+ STRING_Lao0
+ STRING_Latin0
+ STRING_Lepcha0
+ STRING_Limbu0
+ STRING_Linear_A0
+ STRING_Linear_B0
+ STRING_Lisu0
+ STRING_Ll0
+ STRING_Lm0
+ STRING_Lo0
+ STRING_Lt0
+ STRING_Lu0
+ STRING_Lycian0
+ STRING_Lydian0
+ STRING_M0
+ STRING_Mahajani0
+ STRING_Makasar0
+ STRING_Malayalam0
+ STRING_Mandaic0
+ STRING_Manichaean0
+ STRING_Marchen0
+ STRING_Masaram_Gondi0
+ STRING_Mc0
+ STRING_Me0
+ STRING_Medefaidrin0
+ STRING_Meetei_Mayek0
+ STRING_Mende_Kikakui0
+ STRING_Meroitic_Cursive0
+ STRING_Meroitic_Hieroglyphs0
+ STRING_Miao0
+ STRING_Mn0
+ STRING_Modi0
+ STRING_Mongolian0
+ STRING_Mro0
+ STRING_Multani0
+ STRING_Myanmar0
+ STRING_N0
+ STRING_Nabataean0
+ STRING_Nd0
+ STRING_New_Tai_Lue0
+ STRING_Newa0
+ STRING_Nko0
+ STRING_Nl0
+ STRING_No0
+ STRING_Nushu0
+ STRING_Ogham0
+ STRING_Ol_Chiki0
+ STRING_Old_Hungarian0
+ STRING_Old_Italic0
+ STRING_Old_North_Arabian0
+ STRING_Old_Permic0
+ STRING_Old_Persian0
+ STRING_Old_Sogdian0
+ STRING_Old_South_Arabian0
+ STRING_Old_Turkic0
+ STRING_Oriya0
+ STRING_Osage0
+ STRING_Osmanya0
+ STRING_P0
+ STRING_Pahawh_Hmong0
+ STRING_Palmyrene0
+ STRING_Pau_Cin_Hau0
+ STRING_Pc0
+ STRING_Pd0
+ STRING_Pe0
+ STRING_Pf0
+ STRING_Phags_Pa0
+ STRING_Phoenician0
+ STRING_Pi0
+ STRING_Po0
+ STRING_Ps0
+ STRING_Psalter_Pahlavi0
+ STRING_Rejang0
+ STRING_Runic0
+ STRING_S0
+ STRING_Samaritan0
+ STRING_Saurashtra0
+ STRING_Sc0
+ STRING_Sharada0
+ STRING_Shavian0
+ STRING_Siddham0
+ STRING_SignWriting0
+ STRING_Sinhala0
+ STRING_Sk0
+ STRING_Sm0
+ STRING_So0
+ STRING_Sogdian0
+ STRING_Sora_Sompeng0
+ STRING_Soyombo0
+ STRING_Sundanese0
+ STRING_Syloti_Nagri0
+ STRING_Syriac0
+ STRING_Tagalog0
+ STRING_Tagbanwa0
+ STRING_Tai_Le0
+ STRING_Tai_Tham0
+ STRING_Tai_Viet0
+ STRING_Takri0
+ STRING_Tamil0
+ STRING_Tangut0
+ STRING_Telugu0
+ STRING_Thaana0
+ STRING_Thai0
+ STRING_Tibetan0
+ STRING_Tifinagh0
+ STRING_Tirhuta0
+ STRING_Ugaritic0
+ STRING_Vai0
+ STRING_Warang_Citi0
+ STRING_Xan0
+ STRING_Xps0
+ STRING_Xsp0
+ STRING_Xuc0
+ STRING_Xwd0
+ STRING_Yi0
+ STRING_Z0
+ STRING_Zanabazar_Square0
+ STRING_Zl0
+ STRING_Zp0
+ STRING_Zs0;
+
+const ucp_type_table PRIV(utt)[] = {
+ { 0, PT_SC, ucp_Adlam },
+ { 6, PT_SC, ucp_Ahom },
+ { 11, PT_SC, ucp_Anatolian_Hieroglyphs },
+ { 33, PT_ANY, 0 },
+ { 37, PT_SC, ucp_Arabic },
+ { 44, PT_SC, ucp_Armenian },
+ { 53, PT_SC, ucp_Avestan },
+ { 61, PT_SC, ucp_Balinese },
+ { 70, PT_SC, ucp_Bamum },
+ { 76, PT_SC, ucp_Bassa_Vah },
+ { 86, PT_SC, ucp_Batak },
+ { 92, PT_SC, ucp_Bengali },
+ { 100, PT_SC, ucp_Bhaiksuki },
+ { 110, PT_SC, ucp_Bopomofo },
+ { 119, PT_SC, ucp_Brahmi },
+ { 126, PT_SC, ucp_Braille },
+ { 134, PT_SC, ucp_Buginese },
+ { 143, PT_SC, ucp_Buhid },
+ { 149, PT_GC, ucp_C },
+ { 151, PT_SC, ucp_Canadian_Aboriginal },
+ { 171, PT_SC, ucp_Carian },
+ { 178, PT_SC, ucp_Caucasian_Albanian },
+ { 197, PT_PC, ucp_Cc },
+ { 200, PT_PC, ucp_Cf },
+ { 203, PT_SC, ucp_Chakma },
+ { 210, PT_SC, ucp_Cham },
+ { 215, PT_SC, ucp_Cherokee },
+ { 224, PT_PC, ucp_Cn },
+ { 227, PT_PC, ucp_Co },
+ { 230, PT_SC, ucp_Common },
+ { 237, PT_SC, ucp_Coptic },
+ { 244, PT_PC, ucp_Cs },
+ { 247, PT_SC, ucp_Cuneiform },
+ { 257, PT_SC, ucp_Cypriot },
+ { 265, PT_SC, ucp_Cyrillic },
+ { 274, PT_SC, ucp_Deseret },
+ { 282, PT_SC, ucp_Devanagari },
+ { 293, PT_SC, ucp_Dogra },
+ { 299, PT_SC, ucp_Duployan },
+ { 308, PT_SC, ucp_Egyptian_Hieroglyphs },
+ { 329, PT_SC, ucp_Elbasan },
+ { 337, PT_SC, ucp_Ethiopic },
+ { 346, PT_SC, ucp_Georgian },
+ { 355, PT_SC, ucp_Glagolitic },
+ { 366, PT_SC, ucp_Gothic },
+ { 373, PT_SC, ucp_Grantha },
+ { 381, PT_SC, ucp_Greek },
+ { 387, PT_SC, ucp_Gujarati },
+ { 396, PT_SC, ucp_Gunjala_Gondi },
+ { 410, PT_SC, ucp_Gurmukhi },
+ { 419, PT_SC, ucp_Han },
+ { 423, PT_SC, ucp_Hangul },
+ { 430, PT_SC, ucp_Hanifi_Rohingya },
+ { 446, PT_SC, ucp_Hanunoo },
+ { 454, PT_SC, ucp_Hatran },
+ { 461, PT_SC, ucp_Hebrew },
+ { 468, PT_SC, ucp_Hiragana },
+ { 477, PT_SC, ucp_Imperial_Aramaic },
+ { 494, PT_SC, ucp_Inherited },
+ { 504, PT_SC, ucp_Inscriptional_Pahlavi },
+ { 526, PT_SC, ucp_Inscriptional_Parthian },
+ { 549, PT_SC, ucp_Javanese },
+ { 558, PT_SC, ucp_Kaithi },
+ { 565, PT_SC, ucp_Kannada },
+ { 573, PT_SC, ucp_Katakana },
+ { 582, PT_SC, ucp_Kayah_Li },
+ { 591, PT_SC, ucp_Kharoshthi },
+ { 602, PT_SC, ucp_Khmer },
+ { 608, PT_SC, ucp_Khojki },
+ { 615, PT_SC, ucp_Khudawadi },
+ { 625, PT_GC, ucp_L },
+ { 627, PT_LAMP, 0 },
+ { 630, PT_SC, ucp_Lao },
+ { 634, PT_SC, ucp_Latin },
+ { 640, PT_SC, ucp_Lepcha },
+ { 647, PT_SC, ucp_Limbu },
+ { 653, PT_SC, ucp_Linear_A },
+ { 662, PT_SC, ucp_Linear_B },
+ { 671, PT_SC, ucp_Lisu },
+ { 676, PT_PC, ucp_Ll },
+ { 679, PT_PC, ucp_Lm },
+ { 682, PT_PC, ucp_Lo },
+ { 685, PT_PC, ucp_Lt },
+ { 688, PT_PC, ucp_Lu },
+ { 691, PT_SC, ucp_Lycian },
+ { 698, PT_SC, ucp_Lydian },
+ { 705, PT_GC, ucp_M },
+ { 707, PT_SC, ucp_Mahajani },
+ { 716, PT_SC, ucp_Makasar },
+ { 724, PT_SC, ucp_Malayalam },
+ { 734, PT_SC, ucp_Mandaic },
+ { 742, PT_SC, ucp_Manichaean },
+ { 753, PT_SC, ucp_Marchen },
+ { 761, PT_SC, ucp_Masaram_Gondi },
+ { 775, PT_PC, ucp_Mc },
+ { 778, PT_PC, ucp_Me },
+ { 781, PT_SC, ucp_Medefaidrin },
+ { 793, PT_SC, ucp_Meetei_Mayek },
+ { 806, PT_SC, ucp_Mende_Kikakui },
+ { 820, PT_SC, ucp_Meroitic_Cursive },
+ { 837, PT_SC, ucp_Meroitic_Hieroglyphs },
+ { 858, PT_SC, ucp_Miao },
+ { 863, PT_PC, ucp_Mn },
+ { 866, PT_SC, ucp_Modi },
+ { 871, PT_SC, ucp_Mongolian },
+ { 881, PT_SC, ucp_Mro },
+ { 885, PT_SC, ucp_Multani },
+ { 893, PT_SC, ucp_Myanmar },
+ { 901, PT_GC, ucp_N },
+ { 903, PT_SC, ucp_Nabataean },
+ { 913, PT_PC, ucp_Nd },
+ { 916, PT_SC, ucp_New_Tai_Lue },
+ { 928, PT_SC, ucp_Newa },
+ { 933, PT_SC, ucp_Nko },
+ { 937, PT_PC, ucp_Nl },
+ { 940, PT_PC, ucp_No },
+ { 943, PT_SC, ucp_Nushu },
+ { 949, PT_SC, ucp_Ogham },
+ { 955, PT_SC, ucp_Ol_Chiki },
+ { 964, PT_SC, ucp_Old_Hungarian },
+ { 978, PT_SC, ucp_Old_Italic },
+ { 989, PT_SC, ucp_Old_North_Arabian },
+ { 1007, PT_SC, ucp_Old_Permic },
+ { 1018, PT_SC, ucp_Old_Persian },
+ { 1030, PT_SC, ucp_Old_Sogdian },
+ { 1042, PT_SC, ucp_Old_South_Arabian },
+ { 1060, PT_SC, ucp_Old_Turkic },
+ { 1071, PT_SC, ucp_Oriya },
+ { 1077, PT_SC, ucp_Osage },
+ { 1083, PT_SC, ucp_Osmanya },
+ { 1091, PT_GC, ucp_P },
+ { 1093, PT_SC, ucp_Pahawh_Hmong },
+ { 1106, PT_SC, ucp_Palmyrene },
+ { 1116, PT_SC, ucp_Pau_Cin_Hau },
+ { 1128, PT_PC, ucp_Pc },
+ { 1131, PT_PC, ucp_Pd },
+ { 1134, PT_PC, ucp_Pe },
+ { 1137, PT_PC, ucp_Pf },
+ { 1140, PT_SC, ucp_Phags_Pa },
+ { 1149, PT_SC, ucp_Phoenician },
+ { 1160, PT_PC, ucp_Pi },
+ { 1163, PT_PC, ucp_Po },
+ { 1166, PT_PC, ucp_Ps },
+ { 1169, PT_SC, ucp_Psalter_Pahlavi },
+ { 1185, PT_SC, ucp_Rejang },
+ { 1192, PT_SC, ucp_Runic },
+ { 1198, PT_GC, ucp_S },
+ { 1200, PT_SC, ucp_Samaritan },
+ { 1210, PT_SC, ucp_Saurashtra },
+ { 1221, PT_PC, ucp_Sc },
+ { 1224, PT_SC, ucp_Sharada },
+ { 1232, PT_SC, ucp_Shavian },
+ { 1240, PT_SC, ucp_Siddham },
+ { 1248, PT_SC, ucp_SignWriting },
+ { 1260, PT_SC, ucp_Sinhala },
+ { 1268, PT_PC, ucp_Sk },
+ { 1271, PT_PC, ucp_Sm },
+ { 1274, PT_PC, ucp_So },
+ { 1277, PT_SC, ucp_Sogdian },
+ { 1285, PT_SC, ucp_Sora_Sompeng },
+ { 1298, PT_SC, ucp_Soyombo },
+ { 1306, PT_SC, ucp_Sundanese },
+ { 1316, PT_SC, ucp_Syloti_Nagri },
+ { 1329, PT_SC, ucp_Syriac },
+ { 1336, PT_SC, ucp_Tagalog },
+ { 1344, PT_SC, ucp_Tagbanwa },
+ { 1353, PT_SC, ucp_Tai_Le },
+ { 1360, PT_SC, ucp_Tai_Tham },
+ { 1369, PT_SC, ucp_Tai_Viet },
+ { 1378, PT_SC, ucp_Takri },
+ { 1384, PT_SC, ucp_Tamil },
+ { 1390, PT_SC, ucp_Tangut },
+ { 1397, PT_SC, ucp_Telugu },
+ { 1404, PT_SC, ucp_Thaana },
+ { 1411, PT_SC, ucp_Thai },
+ { 1416, PT_SC, ucp_Tibetan },
+ { 1424, PT_SC, ucp_Tifinagh },
+ { 1433, PT_SC, ucp_Tirhuta },
+ { 1441, PT_SC, ucp_Ugaritic },
+ { 1450, PT_SC, ucp_Vai },
+ { 1454, PT_SC, ucp_Warang_Citi },
+ { 1466, PT_ALNUM, 0 },
+ { 1470, PT_PXSPACE, 0 },
+ { 1474, PT_SPACE, 0 },
+ { 1478, PT_UCNC, 0 },
+ { 1482, PT_WORD, 0 },
+ { 1486, PT_SC, ucp_Yi },
+ { 1489, PT_GC, ucp_Z },
+ { 1491, PT_SC, ucp_Zanabazar_Square },
+ { 1508, PT_PC, ucp_Zl },
+ { 1511, PT_PC, ucp_Zp },
+ { 1514, PT_PC, ucp_Zs }
+};
+
+const size_t PRIV(utt_size) = sizeof(PRIV(utt)) / sizeof(ucp_type_table);
+
+#endif /* SUPPORT_UNICODE */
+
+/* End of pcre2_tables.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_ucd.c b/test/monniaux/pcre2-10.32/pcre2_ucd.c
new file mode 100644
index 00000000..275a4be2
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_ucd.c
@@ -0,0 +1,4189 @@
+/* This module is generated by the maint/MultiStage2.py script.
+Do not modify it by hand. Instead modify the script and run it
+to regenerate this code.
+
+As well as being part of the PCRE2 library, this module is #included
+by the pcre2test program, which redefines the PRIV macro to change
+table names from _pcre2_xxx to xxxx, thereby avoiding name clashes
+with the library. At present, just one of these tables is actually
+needed. */
+
+#ifndef PCRE2_PCRE2TEST
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "pcre2_internal.h"
+
+#endif /* PCRE2_PCRE2TEST */
+
+/* Unicode character database. */
+/* This file was autogenerated by the MultiStage2.py script. */
+/* Total size: 92592 bytes, block size: 128. */
+
+/* The tables herein are needed only when UCP support is built,
+and in PCRE2 that happens automatically with UTF support.
+This module should not be referenced otherwise, so
+it should not matter whether it is compiled or not. However
+a comment was received about space saving - maybe the guy linked
+all the modules rather than using a library - so we include a
+condition to cut out the tables when not needed. But don't leave
+a totally empty module because some compilers barf at that.
+Instead, just supply small dummy tables. */
+
+#ifndef SUPPORT_UNICODE
+const ucd_record PRIV(ucd_records)[] = {{0,0,0,0,0 }};
+const uint16_t PRIV(ucd_stage1)[] = {0};
+const uint16_t PRIV(ucd_stage2)[] = {0};
+const uint32_t PRIV(ucd_caseless_sets)[] = {0};
+#else
+
+const char *PRIV(unicode_version) = "11.0.0";
+
+/* If the 32-bit library is run in non-32-bit mode, character values
+greater than 0x10ffff may be encountered. For these we set up a
+special record. */
+
+#if PCRE2_CODE_UNIT_WIDTH == 32
+const ucd_record PRIV(dummy_ucd_record)[] = {{
+ ucp_Common, /* script */
+ ucp_Cn, /* type unassigned */
+ ucp_gbOther, /* grapheme break property */
+ 0, /* case set */
+ 0, /* other case */
+ }};
+#endif
+
+/* When recompiling tables with a new Unicode version, please check the
+types in this structure definition from pcre2_internal.h (the actual
+field names will be different):
+
+typedef struct {
+uint8_t property_0;
+uint8_t property_1;
+uint8_t property_2;
+uint8_t property_3;
+pcre_int32 property_4;
+} ucd_record;
+*/
+
+
+const uint32_t PRIV(ucd_caseless_sets)[] = {
+ NOTACHAR,
+ 0x0053, 0x0073, 0x017f, NOTACHAR,
+ 0x01c4, 0x01c5, 0x01c6, NOTACHAR,
+ 0x01c7, 0x01c8, 0x01c9, NOTACHAR,
+ 0x01ca, 0x01cb, 0x01cc, NOTACHAR,
+ 0x01f1, 0x01f2, 0x01f3, NOTACHAR,
+ 0x0345, 0x0399, 0x03b9, 0x1fbe, NOTACHAR,
+ 0x00b5, 0x039c, 0x03bc, NOTACHAR,
+ 0x03a3, 0x03c2, 0x03c3, NOTACHAR,
+ 0x0392, 0x03b2, 0x03d0, NOTACHAR,
+ 0x0398, 0x03b8, 0x03d1, 0x03f4, NOTACHAR,
+ 0x03a6, 0x03c6, 0x03d5, NOTACHAR,
+ 0x03a0, 0x03c0, 0x03d6, NOTACHAR,
+ 0x039a, 0x03ba, 0x03f0, NOTACHAR,
+ 0x03a1, 0x03c1, 0x03f1, NOTACHAR,
+ 0x0395, 0x03b5, 0x03f5, NOTACHAR,
+ 0x0412, 0x0432, 0x1c80, NOTACHAR,
+ 0x0414, 0x0434, 0x1c81, NOTACHAR,
+ 0x041e, 0x043e, 0x1c82, NOTACHAR,
+ 0x0421, 0x0441, 0x1c83, NOTACHAR,
+ 0x0422, 0x0442, 0x1c84, 0x1c85, NOTACHAR,
+ 0x042a, 0x044a, 0x1c86, NOTACHAR,
+ 0x0462, 0x0463, 0x1c87, NOTACHAR,
+ 0x1e60, 0x1e61, 0x1e9b, NOTACHAR,
+ 0x03a9, 0x03c9, 0x2126, NOTACHAR,
+ 0x004b, 0x006b, 0x212a, NOTACHAR,
+ 0x00c5, 0x00e5, 0x212b, NOTACHAR,
+ 0x1c88, 0xa64a, 0xa64b, NOTACHAR,
+};
+
+/* When #included in pcre2test, we don't need this large table. */
+
+#ifndef PCRE2_PCRE2TEST
+
+const ucd_record PRIV(ucd_records)[] = { /* 6832 bytes, record size 8 */
+ { 9, 0, 2, 0, 0, }, /* 0 */
+ { 9, 0, 1, 0, 0, }, /* 1 */
+ { 9, 0, 0, 0, 0, }, /* 2 */
+ { 9, 29, 12, 0, 0, }, /* 3 */
+ { 9, 21, 12, 0, 0, }, /* 4 */
+ { 9, 23, 12, 0, 0, }, /* 5 */
+ { 9, 22, 12, 0, 0, }, /* 6 */
+ { 9, 18, 12, 0, 0, }, /* 7 */
+ { 9, 25, 12, 0, 0, }, /* 8 */
+ { 9, 17, 12, 0, 0, }, /* 9 */
+ { 9, 13, 12, 0, 0, }, /* 10 */
+ { 33, 9, 12, 0, 32, }, /* 11 */
+ { 33, 9, 12, 100, 32, }, /* 12 */
+ { 33, 9, 12, 1, 32, }, /* 13 */
+ { 9, 24, 12, 0, 0, }, /* 14 */
+ { 9, 16, 12, 0, 0, }, /* 15 */
+ { 33, 5, 12, 0, -32, }, /* 16 */
+ { 33, 5, 12, 100, -32, }, /* 17 */
+ { 33, 5, 12, 1, -32, }, /* 18 */
+ { 9, 26, 12, 0, 0, }, /* 19 */
+ { 9, 26, 14, 0, 0, }, /* 20 */
+ { 33, 7, 12, 0, 0, }, /* 21 */
+ { 9, 20, 12, 0, 0, }, /* 22 */
+ { 9, 1, 2, 0, 0, }, /* 23 */
+ { 9, 15, 12, 0, 0, }, /* 24 */
+ { 9, 5, 12, 26, 775, }, /* 25 */
+ { 9, 19, 12, 0, 0, }, /* 26 */
+ { 33, 9, 12, 104, 32, }, /* 27 */
+ { 33, 5, 12, 0, 7615, }, /* 28 */
+ { 33, 5, 12, 104, -32, }, /* 29 */
+ { 33, 5, 12, 0, 121, }, /* 30 */
+ { 33, 9, 12, 0, 1, }, /* 31 */
+ { 33, 5, 12, 0, -1, }, /* 32 */
+ { 33, 9, 12, 0, 0, }, /* 33 */
+ { 33, 5, 12, 0, 0, }, /* 34 */
+ { 33, 9, 12, 0, -121, }, /* 35 */
+ { 33, 5, 12, 1, -268, }, /* 36 */
+ { 33, 5, 12, 0, 195, }, /* 37 */
+ { 33, 9, 12, 0, 210, }, /* 38 */
+ { 33, 9, 12, 0, 206, }, /* 39 */
+ { 33, 9, 12, 0, 205, }, /* 40 */
+ { 33, 9, 12, 0, 79, }, /* 41 */
+ { 33, 9, 12, 0, 202, }, /* 42 */
+ { 33, 9, 12, 0, 203, }, /* 43 */
+ { 33, 9, 12, 0, 207, }, /* 44 */
+ { 33, 5, 12, 0, 97, }, /* 45 */
+ { 33, 9, 12, 0, 211, }, /* 46 */
+ { 33, 9, 12, 0, 209, }, /* 47 */
+ { 33, 5, 12, 0, 163, }, /* 48 */
+ { 33, 9, 12, 0, 213, }, /* 49 */
+ { 33, 5, 12, 0, 130, }, /* 50 */
+ { 33, 9, 12, 0, 214, }, /* 51 */
+ { 33, 9, 12, 0, 218, }, /* 52 */
+ { 33, 9, 12, 0, 217, }, /* 53 */
+ { 33, 9, 12, 0, 219, }, /* 54 */
+ { 33, 5, 12, 0, 56, }, /* 55 */
+ { 33, 9, 12, 5, 2, }, /* 56 */
+ { 33, 8, 12, 5, 1, }, /* 57 */
+ { 33, 5, 12, 5, -2, }, /* 58 */
+ { 33, 9, 12, 9, 2, }, /* 59 */
+ { 33, 8, 12, 9, 1, }, /* 60 */
+ { 33, 5, 12, 9, -2, }, /* 61 */
+ { 33, 9, 12, 13, 2, }, /* 62 */
+ { 33, 8, 12, 13, 1, }, /* 63 */
+ { 33, 5, 12, 13, -2, }, /* 64 */
+ { 33, 5, 12, 0, -79, }, /* 65 */
+ { 33, 9, 12, 17, 2, }, /* 66 */
+ { 33, 8, 12, 17, 1, }, /* 67 */
+ { 33, 5, 12, 17, -2, }, /* 68 */
+ { 33, 9, 12, 0, -97, }, /* 69 */
+ { 33, 9, 12, 0, -56, }, /* 70 */
+ { 33, 9, 12, 0, -130, }, /* 71 */
+ { 33, 9, 12, 0, 10795, }, /* 72 */
+ { 33, 9, 12, 0, -163, }, /* 73 */
+ { 33, 9, 12, 0, 10792, }, /* 74 */
+ { 33, 5, 12, 0, 10815, }, /* 75 */
+ { 33, 9, 12, 0, -195, }, /* 76 */
+ { 33, 9, 12, 0, 69, }, /* 77 */
+ { 33, 9, 12, 0, 71, }, /* 78 */
+ { 33, 5, 12, 0, 10783, }, /* 79 */
+ { 33, 5, 12, 0, 10780, }, /* 80 */
+ { 33, 5, 12, 0, 10782, }, /* 81 */
+ { 33, 5, 12, 0, -210, }, /* 82 */
+ { 33, 5, 12, 0, -206, }, /* 83 */
+ { 33, 5, 12, 0, -205, }, /* 84 */
+ { 33, 5, 12, 0, -202, }, /* 85 */
+ { 33, 5, 12, 0, -203, }, /* 86 */
+ { 33, 5, 12, 0, 42319, }, /* 87 */
+ { 33, 5, 12, 0, 42315, }, /* 88 */
+ { 33, 5, 12, 0, -207, }, /* 89 */
+ { 33, 5, 12, 0, 42280, }, /* 90 */
+ { 33, 5, 12, 0, 42308, }, /* 91 */
+ { 33, 5, 12, 0, -209, }, /* 92 */
+ { 33, 5, 12, 0, -211, }, /* 93 */
+ { 33, 5, 12, 0, 10743, }, /* 94 */
+ { 33, 5, 12, 0, 42305, }, /* 95 */
+ { 33, 5, 12, 0, 10749, }, /* 96 */
+ { 33, 5, 12, 0, -213, }, /* 97 */
+ { 33, 5, 12, 0, -214, }, /* 98 */
+ { 33, 5, 12, 0, 10727, }, /* 99 */
+ { 33, 5, 12, 0, -218, }, /* 100 */
+ { 33, 5, 12, 0, 42282, }, /* 101 */
+ { 33, 5, 12, 0, -69, }, /* 102 */
+ { 33, 5, 12, 0, -217, }, /* 103 */
+ { 33, 5, 12, 0, -71, }, /* 104 */
+ { 33, 5, 12, 0, -219, }, /* 105 */
+ { 33, 5, 12, 0, 42261, }, /* 106 */
+ { 33, 5, 12, 0, 42258, }, /* 107 */
+ { 33, 6, 12, 0, 0, }, /* 108 */
+ { 9, 6, 12, 0, 0, }, /* 109 */
+ { 3, 24, 12, 0, 0, }, /* 110 */
+ { 27, 12, 3, 0, 0, }, /* 111 */
+ { 27, 12, 3, 21, 116, }, /* 112 */
+ { 19, 9, 12, 0, 1, }, /* 113 */
+ { 19, 5, 12, 0, -1, }, /* 114 */
+ { 19, 24, 12, 0, 0, }, /* 115 */
+ { 9, 2, 12, 0, 0, }, /* 116 */
+ { 19, 6, 12, 0, 0, }, /* 117 */
+ { 19, 5, 12, 0, 130, }, /* 118 */
+ { 19, 9, 12, 0, 116, }, /* 119 */
+ { 19, 9, 12, 0, 38, }, /* 120 */
+ { 19, 9, 12, 0, 37, }, /* 121 */
+ { 19, 9, 12, 0, 64, }, /* 122 */
+ { 19, 9, 12, 0, 63, }, /* 123 */
+ { 19, 5, 12, 0, 0, }, /* 124 */
+ { 19, 9, 12, 0, 32, }, /* 125 */
+ { 19, 9, 12, 34, 32, }, /* 126 */
+ { 19, 9, 12, 59, 32, }, /* 127 */
+ { 19, 9, 12, 38, 32, }, /* 128 */
+ { 19, 9, 12, 21, 32, }, /* 129 */
+ { 19, 9, 12, 51, 32, }, /* 130 */
+ { 19, 9, 12, 26, 32, }, /* 131 */
+ { 19, 9, 12, 47, 32, }, /* 132 */
+ { 19, 9, 12, 55, 32, }, /* 133 */
+ { 19, 9, 12, 30, 32, }, /* 134 */
+ { 19, 9, 12, 43, 32, }, /* 135 */
+ { 19, 9, 12, 96, 32, }, /* 136 */
+ { 19, 5, 12, 0, -38, }, /* 137 */
+ { 19, 5, 12, 0, -37, }, /* 138 */
+ { 19, 5, 12, 0, -32, }, /* 139 */
+ { 19, 5, 12, 34, -32, }, /* 140 */
+ { 19, 5, 12, 59, -32, }, /* 141 */
+ { 19, 5, 12, 38, -32, }, /* 142 */
+ { 19, 5, 12, 21, -116, }, /* 143 */
+ { 19, 5, 12, 51, -32, }, /* 144 */
+ { 19, 5, 12, 26, -775, }, /* 145 */
+ { 19, 5, 12, 47, -32, }, /* 146 */
+ { 19, 5, 12, 55, -32, }, /* 147 */
+ { 19, 5, 12, 30, 1, }, /* 148 */
+ { 19, 5, 12, 30, -32, }, /* 149 */
+ { 19, 5, 12, 43, -32, }, /* 150 */
+ { 19, 5, 12, 96, -32, }, /* 151 */
+ { 19, 5, 12, 0, -64, }, /* 152 */
+ { 19, 5, 12, 0, -63, }, /* 153 */
+ { 19, 9, 12, 0, 8, }, /* 154 */
+ { 19, 5, 12, 34, -30, }, /* 155 */
+ { 19, 5, 12, 38, -25, }, /* 156 */
+ { 19, 9, 12, 0, 0, }, /* 157 */
+ { 19, 5, 12, 43, -15, }, /* 158 */
+ { 19, 5, 12, 47, -22, }, /* 159 */
+ { 19, 5, 12, 0, -8, }, /* 160 */
+ { 10, 9, 12, 0, 1, }, /* 161 */
+ { 10, 5, 12, 0, -1, }, /* 162 */
+ { 19, 5, 12, 51, -54, }, /* 163 */
+ { 19, 5, 12, 55, -48, }, /* 164 */
+ { 19, 5, 12, 0, 7, }, /* 165 */
+ { 19, 5, 12, 0, -116, }, /* 166 */
+ { 19, 9, 12, 38, -60, }, /* 167 */
+ { 19, 5, 12, 59, -64, }, /* 168 */
+ { 19, 25, 12, 0, 0, }, /* 169 */
+ { 19, 9, 12, 0, -7, }, /* 170 */
+ { 19, 9, 12, 0, -130, }, /* 171 */
+ { 12, 9, 12, 0, 80, }, /* 172 */
+ { 12, 9, 12, 0, 32, }, /* 173 */
+ { 12, 9, 12, 63, 32, }, /* 174 */
+ { 12, 9, 12, 67, 32, }, /* 175 */
+ { 12, 9, 12, 71, 32, }, /* 176 */
+ { 12, 9, 12, 75, 32, }, /* 177 */
+ { 12, 9, 12, 79, 32, }, /* 178 */
+ { 12, 9, 12, 84, 32, }, /* 179 */
+ { 12, 5, 12, 0, -32, }, /* 180 */
+ { 12, 5, 12, 63, -32, }, /* 181 */
+ { 12, 5, 12, 67, -32, }, /* 182 */
+ { 12, 5, 12, 71, -32, }, /* 183 */
+ { 12, 5, 12, 75, -32, }, /* 184 */
+ { 12, 5, 12, 79, -32, }, /* 185 */
+ { 12, 5, 12, 84, -32, }, /* 186 */
+ { 12, 5, 12, 0, -80, }, /* 187 */
+ { 12, 9, 12, 0, 1, }, /* 188 */
+ { 12, 5, 12, 0, -1, }, /* 189 */
+ { 12, 9, 12, 88, 1, }, /* 190 */
+ { 12, 5, 12, 88, -1, }, /* 191 */
+ { 12, 26, 12, 0, 0, }, /* 192 */
+ { 12, 12, 3, 0, 0, }, /* 193 */
+ { 12, 11, 3, 0, 0, }, /* 194 */
+ { 12, 9, 12, 0, 15, }, /* 195 */
+ { 12, 5, 12, 0, -15, }, /* 196 */
+ { 1, 9, 12, 0, 48, }, /* 197 */
+ { 1, 6, 12, 0, 0, }, /* 198 */
+ { 1, 21, 12, 0, 0, }, /* 199 */
+ { 1, 5, 12, 0, 0, }, /* 200 */
+ { 1, 5, 12, 0, -48, }, /* 201 */
+ { 1, 17, 12, 0, 0, }, /* 202 */
+ { 1, 26, 12, 0, 0, }, /* 203 */
+ { 1, 23, 12, 0, 0, }, /* 204 */
+ { 25, 12, 3, 0, 0, }, /* 205 */
+ { 25, 17, 12, 0, 0, }, /* 206 */
+ { 25, 21, 12, 0, 0, }, /* 207 */
+ { 25, 7, 12, 0, 0, }, /* 208 */
+ { 0, 1, 4, 0, 0, }, /* 209 */
+ { 9, 1, 4, 0, 0, }, /* 210 */
+ { 0, 25, 12, 0, 0, }, /* 211 */
+ { 0, 21, 12, 0, 0, }, /* 212 */
+ { 0, 23, 12, 0, 0, }, /* 213 */
+ { 0, 26, 12, 0, 0, }, /* 214 */
+ { 0, 12, 3, 0, 0, }, /* 215 */
+ { 0, 1, 2, 0, 0, }, /* 216 */
+ { 0, 7, 12, 0, 0, }, /* 217 */
+ { 0, 13, 12, 0, 0, }, /* 218 */
+ { 0, 6, 12, 0, 0, }, /* 219 */
+ { 49, 21, 12, 0, 0, }, /* 220 */
+ { 49, 1, 4, 0, 0, }, /* 221 */
+ { 49, 7, 12, 0, 0, }, /* 222 */
+ { 49, 12, 3, 0, 0, }, /* 223 */
+ { 55, 7, 12, 0, 0, }, /* 224 */
+ { 55, 12, 3, 0, 0, }, /* 225 */
+ { 63, 13, 12, 0, 0, }, /* 226 */
+ { 63, 7, 12, 0, 0, }, /* 227 */
+ { 63, 12, 3, 0, 0, }, /* 228 */
+ { 63, 6, 12, 0, 0, }, /* 229 */
+ { 63, 26, 12, 0, 0, }, /* 230 */
+ { 63, 21, 12, 0, 0, }, /* 231 */
+ { 63, 23, 12, 0, 0, }, /* 232 */
+ { 89, 7, 12, 0, 0, }, /* 233 */
+ { 89, 12, 3, 0, 0, }, /* 234 */
+ { 89, 6, 12, 0, 0, }, /* 235 */
+ { 89, 21, 12, 0, 0, }, /* 236 */
+ { 94, 7, 12, 0, 0, }, /* 237 */
+ { 94, 12, 3, 0, 0, }, /* 238 */
+ { 94, 21, 12, 0, 0, }, /* 239 */
+ { 14, 12, 3, 0, 0, }, /* 240 */
+ { 14, 10, 5, 0, 0, }, /* 241 */
+ { 14, 7, 12, 0, 0, }, /* 242 */
+ { 14, 13, 12, 0, 0, }, /* 243 */
+ { 14, 21, 12, 0, 0, }, /* 244 */
+ { 14, 6, 12, 0, 0, }, /* 245 */
+ { 2, 7, 12, 0, 0, }, /* 246 */
+ { 2, 12, 3, 0, 0, }, /* 247 */
+ { 2, 10, 5, 0, 0, }, /* 248 */
+ { 2, 10, 3, 0, 0, }, /* 249 */
+ { 2, 13, 12, 0, 0, }, /* 250 */
+ { 2, 23, 12, 0, 0, }, /* 251 */
+ { 2, 15, 12, 0, 0, }, /* 252 */
+ { 2, 26, 12, 0, 0, }, /* 253 */
+ { 2, 21, 12, 0, 0, }, /* 254 */
+ { 21, 12, 3, 0, 0, }, /* 255 */
+ { 21, 10, 5, 0, 0, }, /* 256 */
+ { 21, 7, 12, 0, 0, }, /* 257 */
+ { 21, 13, 12, 0, 0, }, /* 258 */
+ { 21, 21, 12, 0, 0, }, /* 259 */
+ { 20, 12, 3, 0, 0, }, /* 260 */
+ { 20, 10, 5, 0, 0, }, /* 261 */
+ { 20, 7, 12, 0, 0, }, /* 262 */
+ { 20, 13, 12, 0, 0, }, /* 263 */
+ { 20, 21, 12, 0, 0, }, /* 264 */
+ { 20, 23, 12, 0, 0, }, /* 265 */
+ { 43, 12, 3, 0, 0, }, /* 266 */
+ { 43, 10, 5, 0, 0, }, /* 267 */
+ { 43, 7, 12, 0, 0, }, /* 268 */
+ { 43, 10, 3, 0, 0, }, /* 269 */
+ { 43, 13, 12, 0, 0, }, /* 270 */
+ { 43, 26, 12, 0, 0, }, /* 271 */
+ { 43, 15, 12, 0, 0, }, /* 272 */
+ { 53, 12, 3, 0, 0, }, /* 273 */
+ { 53, 7, 12, 0, 0, }, /* 274 */
+ { 53, 10, 3, 0, 0, }, /* 275 */
+ { 53, 10, 5, 0, 0, }, /* 276 */
+ { 53, 13, 12, 0, 0, }, /* 277 */
+ { 53, 15, 12, 0, 0, }, /* 278 */
+ { 53, 26, 12, 0, 0, }, /* 279 */
+ { 53, 23, 12, 0, 0, }, /* 280 */
+ { 54, 12, 3, 0, 0, }, /* 281 */
+ { 54, 10, 5, 0, 0, }, /* 282 */
+ { 54, 7, 12, 0, 0, }, /* 283 */
+ { 54, 13, 12, 0, 0, }, /* 284 */
+ { 54, 15, 12, 0, 0, }, /* 285 */
+ { 54, 26, 12, 0, 0, }, /* 286 */
+ { 28, 7, 12, 0, 0, }, /* 287 */
+ { 28, 12, 3, 0, 0, }, /* 288 */
+ { 28, 10, 5, 0, 0, }, /* 289 */
+ { 28, 21, 12, 0, 0, }, /* 290 */
+ { 28, 10, 3, 0, 0, }, /* 291 */
+ { 28, 13, 12, 0, 0, }, /* 292 */
+ { 36, 12, 3, 0, 0, }, /* 293 */
+ { 36, 10, 5, 0, 0, }, /* 294 */
+ { 36, 7, 12, 0, 0, }, /* 295 */
+ { 36, 10, 3, 0, 0, }, /* 296 */
+ { 36, 7, 4, 0, 0, }, /* 297 */
+ { 36, 26, 12, 0, 0, }, /* 298 */
+ { 36, 15, 12, 0, 0, }, /* 299 */
+ { 36, 13, 12, 0, 0, }, /* 300 */
+ { 47, 10, 5, 0, 0, }, /* 301 */
+ { 47, 7, 12, 0, 0, }, /* 302 */
+ { 47, 12, 3, 0, 0, }, /* 303 */
+ { 47, 10, 3, 0, 0, }, /* 304 */
+ { 47, 13, 12, 0, 0, }, /* 305 */
+ { 47, 21, 12, 0, 0, }, /* 306 */
+ { 56, 7, 12, 0, 0, }, /* 307 */
+ { 56, 12, 3, 0, 0, }, /* 308 */
+ { 56, 7, 5, 0, 0, }, /* 309 */
+ { 56, 6, 12, 0, 0, }, /* 310 */
+ { 56, 21, 12, 0, 0, }, /* 311 */
+ { 56, 13, 12, 0, 0, }, /* 312 */
+ { 32, 7, 12, 0, 0, }, /* 313 */
+ { 32, 12, 3, 0, 0, }, /* 314 */
+ { 32, 7, 5, 0, 0, }, /* 315 */
+ { 32, 6, 12, 0, 0, }, /* 316 */
+ { 32, 13, 12, 0, 0, }, /* 317 */
+ { 57, 7, 12, 0, 0, }, /* 318 */
+ { 57, 26, 12, 0, 0, }, /* 319 */
+ { 57, 21, 12, 0, 0, }, /* 320 */
+ { 57, 12, 3, 0, 0, }, /* 321 */
+ { 57, 13, 12, 0, 0, }, /* 322 */
+ { 57, 15, 12, 0, 0, }, /* 323 */
+ { 57, 22, 12, 0, 0, }, /* 324 */
+ { 57, 18, 12, 0, 0, }, /* 325 */
+ { 57, 10, 5, 0, 0, }, /* 326 */
+ { 38, 7, 12, 0, 0, }, /* 327 */
+ { 38, 10, 12, 0, 0, }, /* 328 */
+ { 38, 12, 3, 0, 0, }, /* 329 */
+ { 38, 10, 5, 0, 0, }, /* 330 */
+ { 38, 13, 12, 0, 0, }, /* 331 */
+ { 38, 21, 12, 0, 0, }, /* 332 */
+ { 38, 26, 12, 0, 0, }, /* 333 */
+ { 16, 9, 12, 0, 7264, }, /* 334 */
+ { 16, 5, 12, 0, 3008, }, /* 335 */
+ { 16, 6, 12, 0, 0, }, /* 336 */
+ { 23, 7, 6, 0, 0, }, /* 337 */
+ { 23, 7, 7, 0, 0, }, /* 338 */
+ { 23, 7, 8, 0, 0, }, /* 339 */
+ { 15, 7, 12, 0, 0, }, /* 340 */
+ { 15, 12, 3, 0, 0, }, /* 341 */
+ { 15, 21, 12, 0, 0, }, /* 342 */
+ { 15, 15, 12, 0, 0, }, /* 343 */
+ { 15, 26, 12, 0, 0, }, /* 344 */
+ { 8, 9, 12, 0, 38864, }, /* 345 */
+ { 8, 9, 12, 0, 8, }, /* 346 */
+ { 8, 5, 12, 0, -8, }, /* 347 */
+ { 7, 17, 12, 0, 0, }, /* 348 */
+ { 7, 7, 12, 0, 0, }, /* 349 */
+ { 7, 21, 12, 0, 0, }, /* 350 */
+ { 40, 29, 12, 0, 0, }, /* 351 */
+ { 40, 7, 12, 0, 0, }, /* 352 */
+ { 40, 22, 12, 0, 0, }, /* 353 */
+ { 40, 18, 12, 0, 0, }, /* 354 */
+ { 45, 7, 12, 0, 0, }, /* 355 */
+ { 45, 14, 12, 0, 0, }, /* 356 */
+ { 50, 7, 12, 0, 0, }, /* 357 */
+ { 50, 12, 3, 0, 0, }, /* 358 */
+ { 24, 7, 12, 0, 0, }, /* 359 */
+ { 24, 12, 3, 0, 0, }, /* 360 */
+ { 6, 7, 12, 0, 0, }, /* 361 */
+ { 6, 12, 3, 0, 0, }, /* 362 */
+ { 51, 7, 12, 0, 0, }, /* 363 */
+ { 51, 12, 3, 0, 0, }, /* 364 */
+ { 31, 7, 12, 0, 0, }, /* 365 */
+ { 31, 12, 3, 0, 0, }, /* 366 */
+ { 31, 10, 5, 0, 0, }, /* 367 */
+ { 31, 21, 12, 0, 0, }, /* 368 */
+ { 31, 6, 12, 0, 0, }, /* 369 */
+ { 31, 23, 12, 0, 0, }, /* 370 */
+ { 31, 13, 12, 0, 0, }, /* 371 */
+ { 31, 15, 12, 0, 0, }, /* 372 */
+ { 37, 21, 12, 0, 0, }, /* 373 */
+ { 37, 17, 12, 0, 0, }, /* 374 */
+ { 37, 12, 3, 0, 0, }, /* 375 */
+ { 37, 1, 2, 0, 0, }, /* 376 */
+ { 37, 13, 12, 0, 0, }, /* 377 */
+ { 37, 7, 12, 0, 0, }, /* 378 */
+ { 37, 6, 12, 0, 0, }, /* 379 */
+ { 34, 7, 12, 0, 0, }, /* 380 */
+ { 34, 12, 3, 0, 0, }, /* 381 */
+ { 34, 10, 5, 0, 0, }, /* 382 */
+ { 34, 26, 12, 0, 0, }, /* 383 */
+ { 34, 21, 12, 0, 0, }, /* 384 */
+ { 34, 13, 12, 0, 0, }, /* 385 */
+ { 52, 7, 12, 0, 0, }, /* 386 */
+ { 39, 7, 12, 0, 0, }, /* 387 */
+ { 39, 13, 12, 0, 0, }, /* 388 */
+ { 39, 15, 12, 0, 0, }, /* 389 */
+ { 39, 26, 12, 0, 0, }, /* 390 */
+ { 31, 26, 12, 0, 0, }, /* 391 */
+ { 5, 7, 12, 0, 0, }, /* 392 */
+ { 5, 12, 3, 0, 0, }, /* 393 */
+ { 5, 10, 5, 0, 0, }, /* 394 */
+ { 5, 21, 12, 0, 0, }, /* 395 */
+ { 90, 7, 12, 0, 0, }, /* 396 */
+ { 90, 10, 5, 0, 0, }, /* 397 */
+ { 90, 12, 3, 0, 0, }, /* 398 */
+ { 90, 10, 12, 0, 0, }, /* 399 */
+ { 90, 13, 12, 0, 0, }, /* 400 */
+ { 90, 21, 12, 0, 0, }, /* 401 */
+ { 90, 6, 12, 0, 0, }, /* 402 */
+ { 27, 11, 3, 0, 0, }, /* 403 */
+ { 61, 12, 3, 0, 0, }, /* 404 */
+ { 61, 10, 5, 0, 0, }, /* 405 */
+ { 61, 7, 12, 0, 0, }, /* 406 */
+ { 61, 13, 12, 0, 0, }, /* 407 */
+ { 61, 21, 12, 0, 0, }, /* 408 */
+ { 61, 26, 12, 0, 0, }, /* 409 */
+ { 75, 12, 3, 0, 0, }, /* 410 */
+ { 75, 10, 5, 0, 0, }, /* 411 */
+ { 75, 7, 12, 0, 0, }, /* 412 */
+ { 75, 13, 12, 0, 0, }, /* 413 */
+ { 92, 7, 12, 0, 0, }, /* 414 */
+ { 92, 12, 3, 0, 0, }, /* 415 */
+ { 92, 10, 5, 0, 0, }, /* 416 */
+ { 92, 21, 12, 0, 0, }, /* 417 */
+ { 69, 7, 12, 0, 0, }, /* 418 */
+ { 69, 10, 5, 0, 0, }, /* 419 */
+ { 69, 12, 3, 0, 0, }, /* 420 */
+ { 69, 21, 12, 0, 0, }, /* 421 */
+ { 69, 13, 12, 0, 0, }, /* 422 */
+ { 72, 13, 12, 0, 0, }, /* 423 */
+ { 72, 7, 12, 0, 0, }, /* 424 */
+ { 72, 6, 12, 0, 0, }, /* 425 */
+ { 72, 21, 12, 0, 0, }, /* 426 */
+ { 12, 5, 12, 63, -6222, }, /* 427 */
+ { 12, 5, 12, 67, -6221, }, /* 428 */
+ { 12, 5, 12, 71, -6212, }, /* 429 */
+ { 12, 5, 12, 75, -6210, }, /* 430 */
+ { 12, 5, 12, 79, -6210, }, /* 431 */
+ { 12, 5, 12, 79, -6211, }, /* 432 */
+ { 12, 5, 12, 84, -6204, }, /* 433 */
+ { 12, 5, 12, 88, -6180, }, /* 434 */
+ { 12, 5, 12, 108, 35267, }, /* 435 */
+ { 16, 9, 12, 0, -3008, }, /* 436 */
+ { 75, 21, 12, 0, 0, }, /* 437 */
+ { 9, 10, 5, 0, 0, }, /* 438 */
+ { 9, 7, 12, 0, 0, }, /* 439 */
+ { 12, 5, 12, 0, 0, }, /* 440 */
+ { 12, 6, 12, 0, 0, }, /* 441 */
+ { 33, 5, 12, 0, 35332, }, /* 442 */
+ { 33, 5, 12, 0, 3814, }, /* 443 */
+ { 33, 9, 12, 92, 1, }, /* 444 */
+ { 33, 5, 12, 92, -1, }, /* 445 */
+ { 33, 5, 12, 92, -58, }, /* 446 */
+ { 33, 9, 12, 0, -7615, }, /* 447 */
+ { 19, 5, 12, 0, 8, }, /* 448 */
+ { 19, 9, 12, 0, -8, }, /* 449 */
+ { 19, 5, 12, 0, 74, }, /* 450 */
+ { 19, 5, 12, 0, 86, }, /* 451 */
+ { 19, 5, 12, 0, 100, }, /* 452 */
+ { 19, 5, 12, 0, 128, }, /* 453 */
+ { 19, 5, 12, 0, 112, }, /* 454 */
+ { 19, 5, 12, 0, 126, }, /* 455 */
+ { 19, 8, 12, 0, -8, }, /* 456 */
+ { 19, 5, 12, 0, 9, }, /* 457 */
+ { 19, 9, 12, 0, -74, }, /* 458 */
+ { 19, 8, 12, 0, -9, }, /* 459 */
+ { 19, 5, 12, 21, -7173, }, /* 460 */
+ { 19, 9, 12, 0, -86, }, /* 461 */
+ { 19, 9, 12, 0, -100, }, /* 462 */
+ { 19, 9, 12, 0, -112, }, /* 463 */
+ { 19, 9, 12, 0, -128, }, /* 464 */
+ { 19, 9, 12, 0, -126, }, /* 465 */
+ { 27, 1, 3, 0, 0, }, /* 466 */
+ { 27, 1, 13, 0, 0, }, /* 467 */
+ { 9, 27, 2, 0, 0, }, /* 468 */
+ { 9, 28, 2, 0, 0, }, /* 469 */
+ { 9, 21, 14, 0, 0, }, /* 470 */
+ { 9, 2, 2, 0, 0, }, /* 471 */
+ { 9, 9, 12, 0, 0, }, /* 472 */
+ { 9, 5, 12, 0, 0, }, /* 473 */
+ { 19, 9, 12, 96, -7517, }, /* 474 */
+ { 33, 9, 12, 100, -8383, }, /* 475 */
+ { 33, 9, 12, 104, -8262, }, /* 476 */
+ { 33, 9, 12, 0, 28, }, /* 477 */
+ { 9, 5, 14, 0, 0, }, /* 478 */
+ { 33, 5, 12, 0, -28, }, /* 479 */
+ { 33, 14, 12, 0, 16, }, /* 480 */
+ { 33, 14, 12, 0, -16, }, /* 481 */
+ { 33, 14, 12, 0, 0, }, /* 482 */
+ { 9, 25, 14, 0, 0, }, /* 483 */
+ { 9, 26, 12, 0, 26, }, /* 484 */
+ { 9, 26, 14, 0, 26, }, /* 485 */
+ { 9, 26, 12, 0, -26, }, /* 486 */
+ { 4, 26, 12, 0, 0, }, /* 487 */
+ { 17, 9, 12, 0, 48, }, /* 488 */
+ { 17, 5, 12, 0, -48, }, /* 489 */
+ { 33, 9, 12, 0, -10743, }, /* 490 */
+ { 33, 9, 12, 0, -3814, }, /* 491 */
+ { 33, 9, 12, 0, -10727, }, /* 492 */
+ { 33, 5, 12, 0, -10795, }, /* 493 */
+ { 33, 5, 12, 0, -10792, }, /* 494 */
+ { 33, 9, 12, 0, -10780, }, /* 495 */
+ { 33, 9, 12, 0, -10749, }, /* 496 */
+ { 33, 9, 12, 0, -10783, }, /* 497 */
+ { 33, 9, 12, 0, -10782, }, /* 498 */
+ { 33, 9, 12, 0, -10815, }, /* 499 */
+ { 10, 5, 12, 0, 0, }, /* 500 */
+ { 10, 26, 12, 0, 0, }, /* 501 */
+ { 10, 12, 3, 0, 0, }, /* 502 */
+ { 10, 21, 12, 0, 0, }, /* 503 */
+ { 10, 15, 12, 0, 0, }, /* 504 */
+ { 16, 5, 12, 0, -7264, }, /* 505 */
+ { 58, 7, 12, 0, 0, }, /* 506 */
+ { 58, 6, 12, 0, 0, }, /* 507 */
+ { 58, 21, 12, 0, 0, }, /* 508 */
+ { 58, 12, 3, 0, 0, }, /* 509 */
+ { 22, 26, 12, 0, 0, }, /* 510 */
+ { 22, 6, 12, 0, 0, }, /* 511 */
+ { 22, 14, 12, 0, 0, }, /* 512 */
+ { 23, 10, 3, 0, 0, }, /* 513 */
+ { 9, 17, 14, 0, 0, }, /* 514 */
+ { 26, 7, 12, 0, 0, }, /* 515 */
+ { 26, 6, 12, 0, 0, }, /* 516 */
+ { 29, 7, 12, 0, 0, }, /* 517 */
+ { 29, 6, 12, 0, 0, }, /* 518 */
+ { 3, 7, 12, 0, 0, }, /* 519 */
+ { 23, 7, 12, 0, 0, }, /* 520 */
+ { 23, 26, 12, 0, 0, }, /* 521 */
+ { 29, 26, 12, 0, 0, }, /* 522 */
+ { 22, 7, 12, 0, 0, }, /* 523 */
+ { 60, 7, 12, 0, 0, }, /* 524 */
+ { 60, 6, 12, 0, 0, }, /* 525 */
+ { 60, 26, 12, 0, 0, }, /* 526 */
+ { 85, 7, 12, 0, 0, }, /* 527 */
+ { 85, 6, 12, 0, 0, }, /* 528 */
+ { 85, 21, 12, 0, 0, }, /* 529 */
+ { 76, 7, 12, 0, 0, }, /* 530 */
+ { 76, 6, 12, 0, 0, }, /* 531 */
+ { 76, 21, 12, 0, 0, }, /* 532 */
+ { 76, 13, 12, 0, 0, }, /* 533 */
+ { 12, 9, 12, 108, 1, }, /* 534 */
+ { 12, 5, 12, 108, -35267, }, /* 535 */
+ { 12, 7, 12, 0, 0, }, /* 536 */
+ { 12, 21, 12, 0, 0, }, /* 537 */
+ { 78, 7, 12, 0, 0, }, /* 538 */
+ { 78, 14, 12, 0, 0, }, /* 539 */
+ { 78, 12, 3, 0, 0, }, /* 540 */
+ { 78, 21, 12, 0, 0, }, /* 541 */
+ { 33, 9, 12, 0, -35332, }, /* 542 */
+ { 33, 9, 12, 0, -42280, }, /* 543 */
+ { 33, 9, 12, 0, -42308, }, /* 544 */
+ { 33, 9, 12, 0, -42319, }, /* 545 */
+ { 33, 9, 12, 0, -42315, }, /* 546 */
+ { 33, 9, 12, 0, -42305, }, /* 547 */
+ { 33, 9, 12, 0, -42258, }, /* 548 */
+ { 33, 9, 12, 0, -42282, }, /* 549 */
+ { 33, 9, 12, 0, -42261, }, /* 550 */
+ { 33, 9, 12, 0, 928, }, /* 551 */
+ { 48, 7, 12, 0, 0, }, /* 552 */
+ { 48, 12, 3, 0, 0, }, /* 553 */
+ { 48, 10, 5, 0, 0, }, /* 554 */
+ { 48, 26, 12, 0, 0, }, /* 555 */
+ { 64, 7, 12, 0, 0, }, /* 556 */
+ { 64, 21, 12, 0, 0, }, /* 557 */
+ { 74, 10, 5, 0, 0, }, /* 558 */
+ { 74, 7, 12, 0, 0, }, /* 559 */
+ { 74, 12, 3, 0, 0, }, /* 560 */
+ { 74, 21, 12, 0, 0, }, /* 561 */
+ { 74, 13, 12, 0, 0, }, /* 562 */
+ { 68, 13, 12, 0, 0, }, /* 563 */
+ { 68, 7, 12, 0, 0, }, /* 564 */
+ { 68, 12, 3, 0, 0, }, /* 565 */
+ { 68, 21, 12, 0, 0, }, /* 566 */
+ { 73, 7, 12, 0, 0, }, /* 567 */
+ { 73, 12, 3, 0, 0, }, /* 568 */
+ { 73, 10, 5, 0, 0, }, /* 569 */
+ { 73, 21, 12, 0, 0, }, /* 570 */
+ { 83, 12, 3, 0, 0, }, /* 571 */
+ { 83, 10, 5, 0, 0, }, /* 572 */
+ { 83, 7, 12, 0, 0, }, /* 573 */
+ { 83, 21, 12, 0, 0, }, /* 574 */
+ { 83, 13, 12, 0, 0, }, /* 575 */
+ { 38, 6, 12, 0, 0, }, /* 576 */
+ { 67, 7, 12, 0, 0, }, /* 577 */
+ { 67, 12, 3, 0, 0, }, /* 578 */
+ { 67, 10, 5, 0, 0, }, /* 579 */
+ { 67, 13, 12, 0, 0, }, /* 580 */
+ { 67, 21, 12, 0, 0, }, /* 581 */
+ { 91, 7, 12, 0, 0, }, /* 582 */
+ { 91, 12, 3, 0, 0, }, /* 583 */
+ { 91, 6, 12, 0, 0, }, /* 584 */
+ { 91, 21, 12, 0, 0, }, /* 585 */
+ { 86, 7, 12, 0, 0, }, /* 586 */
+ { 86, 10, 5, 0, 0, }, /* 587 */
+ { 86, 12, 3, 0, 0, }, /* 588 */
+ { 86, 21, 12, 0, 0, }, /* 589 */
+ { 86, 6, 12, 0, 0, }, /* 590 */
+ { 33, 5, 12, 0, -928, }, /* 591 */
+ { 8, 5, 12, 0, -38864, }, /* 592 */
+ { 86, 13, 12, 0, 0, }, /* 593 */
+ { 23, 7, 9, 0, 0, }, /* 594 */
+ { 23, 7, 10, 0, 0, }, /* 595 */
+ { 9, 4, 2, 0, 0, }, /* 596 */
+ { 9, 3, 12, 0, 0, }, /* 597 */
+ { 25, 25, 12, 0, 0, }, /* 598 */
+ { 0, 24, 12, 0, 0, }, /* 599 */
+ { 9, 6, 3, 0, 0, }, /* 600 */
+ { 35, 7, 12, 0, 0, }, /* 601 */
+ { 19, 14, 12, 0, 0, }, /* 602 */
+ { 19, 15, 12, 0, 0, }, /* 603 */
+ { 19, 26, 12, 0, 0, }, /* 604 */
+ { 70, 7, 12, 0, 0, }, /* 605 */
+ { 66, 7, 12, 0, 0, }, /* 606 */
+ { 41, 7, 12, 0, 0, }, /* 607 */
+ { 41, 15, 12, 0, 0, }, /* 608 */
+ { 18, 7, 12, 0, 0, }, /* 609 */
+ { 18, 14, 12, 0, 0, }, /* 610 */
+ { 117, 7, 12, 0, 0, }, /* 611 */
+ { 117, 12, 3, 0, 0, }, /* 612 */
+ { 59, 7, 12, 0, 0, }, /* 613 */
+ { 59, 21, 12, 0, 0, }, /* 614 */
+ { 42, 7, 12, 0, 0, }, /* 615 */
+ { 42, 21, 12, 0, 0, }, /* 616 */
+ { 42, 14, 12, 0, 0, }, /* 617 */
+ { 13, 9, 12, 0, 40, }, /* 618 */
+ { 13, 5, 12, 0, -40, }, /* 619 */
+ { 46, 7, 12, 0, 0, }, /* 620 */
+ { 44, 7, 12, 0, 0, }, /* 621 */
+ { 44, 13, 12, 0, 0, }, /* 622 */
+ { 135, 9, 12, 0, 40, }, /* 623 */
+ { 135, 5, 12, 0, -40, }, /* 624 */
+ { 105, 7, 12, 0, 0, }, /* 625 */
+ { 103, 7, 12, 0, 0, }, /* 626 */
+ { 103, 21, 12, 0, 0, }, /* 627 */
+ { 109, 7, 12, 0, 0, }, /* 628 */
+ { 11, 7, 12, 0, 0, }, /* 629 */
+ { 80, 7, 12, 0, 0, }, /* 630 */
+ { 80, 21, 12, 0, 0, }, /* 631 */
+ { 80, 15, 12, 0, 0, }, /* 632 */
+ { 119, 7, 12, 0, 0, }, /* 633 */
+ { 119, 26, 12, 0, 0, }, /* 634 */
+ { 119, 15, 12, 0, 0, }, /* 635 */
+ { 115, 7, 12, 0, 0, }, /* 636 */
+ { 115, 15, 12, 0, 0, }, /* 637 */
+ { 127, 7, 12, 0, 0, }, /* 638 */
+ { 127, 15, 12, 0, 0, }, /* 639 */
+ { 65, 7, 12, 0, 0, }, /* 640 */
+ { 65, 15, 12, 0, 0, }, /* 641 */
+ { 65, 21, 12, 0, 0, }, /* 642 */
+ { 71, 7, 12, 0, 0, }, /* 643 */
+ { 71, 21, 12, 0, 0, }, /* 644 */
+ { 97, 7, 12, 0, 0, }, /* 645 */
+ { 96, 7, 12, 0, 0, }, /* 646 */
+ { 96, 15, 12, 0, 0, }, /* 647 */
+ { 30, 7, 12, 0, 0, }, /* 648 */
+ { 30, 12, 3, 0, 0, }, /* 649 */
+ { 30, 15, 12, 0, 0, }, /* 650 */
+ { 30, 21, 12, 0, 0, }, /* 651 */
+ { 87, 7, 12, 0, 0, }, /* 652 */
+ { 87, 15, 12, 0, 0, }, /* 653 */
+ { 87, 21, 12, 0, 0, }, /* 654 */
+ { 116, 7, 12, 0, 0, }, /* 655 */
+ { 116, 15, 12, 0, 0, }, /* 656 */
+ { 111, 7, 12, 0, 0, }, /* 657 */
+ { 111, 26, 12, 0, 0, }, /* 658 */
+ { 111, 12, 3, 0, 0, }, /* 659 */
+ { 111, 15, 12, 0, 0, }, /* 660 */
+ { 111, 21, 12, 0, 0, }, /* 661 */
+ { 77, 7, 12, 0, 0, }, /* 662 */
+ { 77, 21, 12, 0, 0, }, /* 663 */
+ { 82, 7, 12, 0, 0, }, /* 664 */
+ { 82, 15, 12, 0, 0, }, /* 665 */
+ { 81, 7, 12, 0, 0, }, /* 666 */
+ { 81, 15, 12, 0, 0, }, /* 667 */
+ { 120, 7, 12, 0, 0, }, /* 668 */
+ { 120, 21, 12, 0, 0, }, /* 669 */
+ { 120, 15, 12, 0, 0, }, /* 670 */
+ { 88, 7, 12, 0, 0, }, /* 671 */
+ { 129, 9, 12, 0, 64, }, /* 672 */
+ { 129, 5, 12, 0, -64, }, /* 673 */
+ { 129, 15, 12, 0, 0, }, /* 674 */
+ { 143, 7, 12, 0, 0, }, /* 675 */
+ { 143, 12, 3, 0, 0, }, /* 676 */
+ { 143, 13, 12, 0, 0, }, /* 677 */
+ { 0, 15, 12, 0, 0, }, /* 678 */
+ { 146, 7, 12, 0, 0, }, /* 679 */
+ { 146, 15, 12, 0, 0, }, /* 680 */
+ { 147, 7, 12, 0, 0, }, /* 681 */
+ { 147, 12, 3, 0, 0, }, /* 682 */
+ { 147, 15, 12, 0, 0, }, /* 683 */
+ { 147, 21, 12, 0, 0, }, /* 684 */
+ { 93, 10, 5, 0, 0, }, /* 685 */
+ { 93, 12, 3, 0, 0, }, /* 686 */
+ { 93, 7, 12, 0, 0, }, /* 687 */
+ { 93, 21, 12, 0, 0, }, /* 688 */
+ { 93, 15, 12, 0, 0, }, /* 689 */
+ { 93, 13, 12, 0, 0, }, /* 690 */
+ { 84, 12, 3, 0, 0, }, /* 691 */
+ { 84, 10, 5, 0, 0, }, /* 692 */
+ { 84, 7, 12, 0, 0, }, /* 693 */
+ { 84, 21, 12, 0, 0, }, /* 694 */
+ { 84, 1, 4, 0, 0, }, /* 695 */
+ { 100, 7, 12, 0, 0, }, /* 696 */
+ { 100, 13, 12, 0, 0, }, /* 697 */
+ { 95, 12, 3, 0, 0, }, /* 698 */
+ { 95, 7, 12, 0, 0, }, /* 699 */
+ { 95, 10, 5, 0, 0, }, /* 700 */
+ { 95, 13, 12, 0, 0, }, /* 701 */
+ { 95, 21, 12, 0, 0, }, /* 702 */
+ { 110, 7, 12, 0, 0, }, /* 703 */
+ { 110, 12, 3, 0, 0, }, /* 704 */
+ { 110, 21, 12, 0, 0, }, /* 705 */
+ { 99, 12, 3, 0, 0, }, /* 706 */
+ { 99, 10, 5, 0, 0, }, /* 707 */
+ { 99, 7, 12, 0, 0, }, /* 708 */
+ { 99, 7, 4, 0, 0, }, /* 709 */
+ { 99, 21, 12, 0, 0, }, /* 710 */
+ { 99, 13, 12, 0, 0, }, /* 711 */
+ { 47, 15, 12, 0, 0, }, /* 712 */
+ { 107, 7, 12, 0, 0, }, /* 713 */
+ { 107, 10, 5, 0, 0, }, /* 714 */
+ { 107, 12, 3, 0, 0, }, /* 715 */
+ { 107, 21, 12, 0, 0, }, /* 716 */
+ { 128, 7, 12, 0, 0, }, /* 717 */
+ { 128, 21, 12, 0, 0, }, /* 718 */
+ { 108, 7, 12, 0, 0, }, /* 719 */
+ { 108, 12, 3, 0, 0, }, /* 720 */
+ { 108, 10, 5, 0, 0, }, /* 721 */
+ { 108, 13, 12, 0, 0, }, /* 722 */
+ { 106, 12, 3, 0, 0, }, /* 723 */
+ { 106, 10, 5, 0, 0, }, /* 724 */
+ { 106, 7, 12, 0, 0, }, /* 725 */
+ { 106, 10, 3, 0, 0, }, /* 726 */
+ { 134, 7, 12, 0, 0, }, /* 727 */
+ { 134, 10, 5, 0, 0, }, /* 728 */
+ { 134, 12, 3, 0, 0, }, /* 729 */
+ { 134, 21, 12, 0, 0, }, /* 730 */
+ { 134, 13, 12, 0, 0, }, /* 731 */
+ { 123, 7, 12, 0, 0, }, /* 732 */
+ { 123, 10, 3, 0, 0, }, /* 733 */
+ { 123, 10, 5, 0, 0, }, /* 734 */
+ { 123, 12, 3, 0, 0, }, /* 735 */
+ { 123, 21, 12, 0, 0, }, /* 736 */
+ { 123, 13, 12, 0, 0, }, /* 737 */
+ { 122, 7, 12, 0, 0, }, /* 738 */
+ { 122, 10, 3, 0, 0, }, /* 739 */
+ { 122, 10, 5, 0, 0, }, /* 740 */
+ { 122, 12, 3, 0, 0, }, /* 741 */
+ { 122, 21, 12, 0, 0, }, /* 742 */
+ { 113, 7, 12, 0, 0, }, /* 743 */
+ { 113, 10, 5, 0, 0, }, /* 744 */
+ { 113, 12, 3, 0, 0, }, /* 745 */
+ { 113, 21, 12, 0, 0, }, /* 746 */
+ { 113, 13, 12, 0, 0, }, /* 747 */
+ { 101, 7, 12, 0, 0, }, /* 748 */
+ { 101, 12, 3, 0, 0, }, /* 749 */
+ { 101, 10, 5, 0, 0, }, /* 750 */
+ { 101, 13, 12, 0, 0, }, /* 751 */
+ { 125, 7, 12, 0, 0, }, /* 752 */
+ { 125, 12, 3, 0, 0, }, /* 753 */
+ { 125, 10, 5, 0, 0, }, /* 754 */
+ { 125, 13, 12, 0, 0, }, /* 755 */
+ { 125, 15, 12, 0, 0, }, /* 756 */
+ { 125, 21, 12, 0, 0, }, /* 757 */
+ { 125, 26, 12, 0, 0, }, /* 758 */
+ { 141, 7, 12, 0, 0, }, /* 759 */
+ { 141, 10, 5, 0, 0, }, /* 760 */
+ { 141, 12, 3, 0, 0, }, /* 761 */
+ { 141, 21, 12, 0, 0, }, /* 762 */
+ { 124, 9, 12, 0, 32, }, /* 763 */
+ { 124, 5, 12, 0, -32, }, /* 764 */
+ { 124, 13, 12, 0, 0, }, /* 765 */
+ { 124, 15, 12, 0, 0, }, /* 766 */
+ { 124, 7, 12, 0, 0, }, /* 767 */
+ { 140, 7, 12, 0, 0, }, /* 768 */
+ { 140, 12, 3, 0, 0, }, /* 769 */
+ { 140, 10, 5, 0, 0, }, /* 770 */
+ { 140, 7, 4, 0, 0, }, /* 771 */
+ { 140, 21, 12, 0, 0, }, /* 772 */
+ { 139, 7, 12, 0, 0, }, /* 773 */
+ { 139, 12, 3, 0, 0, }, /* 774 */
+ { 139, 10, 5, 0, 0, }, /* 775 */
+ { 139, 7, 4, 0, 0, }, /* 776 */
+ { 139, 21, 12, 0, 0, }, /* 777 */
+ { 121, 7, 12, 0, 0, }, /* 778 */
+ { 132, 7, 12, 0, 0, }, /* 779 */
+ { 132, 10, 5, 0, 0, }, /* 780 */
+ { 132, 12, 3, 0, 0, }, /* 781 */
+ { 132, 21, 12, 0, 0, }, /* 782 */
+ { 132, 13, 12, 0, 0, }, /* 783 */
+ { 132, 15, 12, 0, 0, }, /* 784 */
+ { 133, 21, 12, 0, 0, }, /* 785 */
+ { 133, 7, 12, 0, 0, }, /* 786 */
+ { 133, 12, 3, 0, 0, }, /* 787 */
+ { 133, 10, 5, 0, 0, }, /* 788 */
+ { 137, 7, 12, 0, 0, }, /* 789 */
+ { 137, 12, 3, 0, 0, }, /* 790 */
+ { 137, 7, 4, 0, 0, }, /* 791 */
+ { 137, 13, 12, 0, 0, }, /* 792 */
+ { 142, 7, 12, 0, 0, }, /* 793 */
+ { 142, 10, 5, 0, 0, }, /* 794 */
+ { 142, 12, 3, 0, 0, }, /* 795 */
+ { 142, 13, 12, 0, 0, }, /* 796 */
+ { 144, 7, 12, 0, 0, }, /* 797 */
+ { 144, 12, 3, 0, 0, }, /* 798 */
+ { 144, 10, 5, 0, 0, }, /* 799 */
+ { 144, 21, 12, 0, 0, }, /* 800 */
+ { 62, 7, 12, 0, 0, }, /* 801 */
+ { 62, 14, 12, 0, 0, }, /* 802 */
+ { 62, 21, 12, 0, 0, }, /* 803 */
+ { 79, 7, 12, 0, 0, }, /* 804 */
+ { 126, 7, 12, 0, 0, }, /* 805 */
+ { 114, 7, 12, 0, 0, }, /* 806 */
+ { 114, 13, 12, 0, 0, }, /* 807 */
+ { 114, 21, 12, 0, 0, }, /* 808 */
+ { 102, 7, 12, 0, 0, }, /* 809 */
+ { 102, 12, 3, 0, 0, }, /* 810 */
+ { 102, 21, 12, 0, 0, }, /* 811 */
+ { 118, 7, 12, 0, 0, }, /* 812 */
+ { 118, 12, 3, 0, 0, }, /* 813 */
+ { 118, 21, 12, 0, 0, }, /* 814 */
+ { 118, 26, 12, 0, 0, }, /* 815 */
+ { 118, 6, 12, 0, 0, }, /* 816 */
+ { 118, 13, 12, 0, 0, }, /* 817 */
+ { 118, 15, 12, 0, 0, }, /* 818 */
+ { 145, 9, 12, 0, 32, }, /* 819 */
+ { 145, 5, 12, 0, -32, }, /* 820 */
+ { 145, 15, 12, 0, 0, }, /* 821 */
+ { 145, 21, 12, 0, 0, }, /* 822 */
+ { 98, 7, 12, 0, 0, }, /* 823 */
+ { 98, 10, 5, 0, 0, }, /* 824 */
+ { 98, 12, 3, 0, 0, }, /* 825 */
+ { 98, 6, 12, 0, 0, }, /* 826 */
+ { 136, 6, 12, 0, 0, }, /* 827 */
+ { 138, 6, 12, 0, 0, }, /* 828 */
+ { 136, 7, 12, 0, 0, }, /* 829 */
+ { 138, 7, 12, 0, 0, }, /* 830 */
+ { 104, 7, 12, 0, 0, }, /* 831 */
+ { 104, 26, 12, 0, 0, }, /* 832 */
+ { 104, 12, 3, 0, 0, }, /* 833 */
+ { 104, 21, 12, 0, 0, }, /* 834 */
+ { 9, 10, 3, 0, 0, }, /* 835 */
+ { 19, 12, 3, 0, 0, }, /* 836 */
+ { 130, 26, 12, 0, 0, }, /* 837 */
+ { 130, 12, 3, 0, 0, }, /* 838 */
+ { 130, 21, 12, 0, 0, }, /* 839 */
+ { 17, 12, 3, 0, 0, }, /* 840 */
+ { 112, 7, 12, 0, 0, }, /* 841 */
+ { 112, 15, 12, 0, 0, }, /* 842 */
+ { 112, 12, 3, 0, 0, }, /* 843 */
+ { 131, 9, 12, 0, 34, }, /* 844 */
+ { 131, 5, 12, 0, -34, }, /* 845 */
+ { 131, 12, 3, 0, 0, }, /* 846 */
+ { 131, 13, 12, 0, 0, }, /* 847 */
+ { 131, 21, 12, 0, 0, }, /* 848 */
+ { 9, 2, 14, 0, 0, }, /* 849 */
+ { 9, 26, 11, 0, 0, }, /* 850 */
+ { 26, 26, 12, 0, 0, }, /* 851 */
+ { 9, 24, 3, 0, 0, }, /* 852 */
+ { 9, 1, 3, 0, 0, }, /* 853 */
+};
+
+const uint16_t PRIV(ucd_stage1)[] = { /* 17408 bytes */
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* U+0000 */
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* U+0800 */
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 41, 41, 42, 43, 44, 45, /* U+1000 */
+ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, /* U+1800 */
+ 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, /* U+2000 */
+ 77, 77, 78, 79, 66, 66, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, /* U+2800 */
+ 90, 91, 92, 93, 94, 95, 96, 71, 97, 97, 97, 97, 97, 97, 97, 97, /* U+3000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+3800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+4000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 97, 97, 97, 97, /* U+4800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+5000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+5800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+6000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+6800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+7000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+7800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+8000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+8800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+9000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 99, /* U+9800 */
+100,101,101,101,101,101,101,101,101,102,103,103,104,105,106,107, /* U+A000 */
+108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,116, /* U+A800 */
+117,118,119,120,121,122,116,117,118,119,120,121,122,116,117,118, /* U+B000 */
+119,120,121,122,116,117,118,119,120,121,122,116,117,118,119,120, /* U+B800 */
+121,122,116,117,118,119,120,121,122,116,117,118,119,120,121,122, /* U+C000 */
+116,117,118,119,120,121,122,116,117,118,119,120,121,122,116,117, /* U+C800 */
+118,119,120,121,122,116,117,118,119,120,121,122,116,117,118,123, /* U+D000 */
+124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124, /* U+D800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+E000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+E800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F000 */
+125,125, 97, 97,126,127,128,129,130,130,131,132,133,134,135,136, /* U+F800 */
+137,138,139,140,141,142,143,144,145,146,147,141,148,148,149,141, /* U+10000 */
+150,151,152,153,154,155,156,157,158,159,160,141,161,141,162,141, /* U+10800 */
+163,164,165,166,167,168,169,141,170,171,141,172,173,174,175,141, /* U+11000 */
+176,177,141,141,178,179,141,141,180,181,182,183,141,184,141,141, /* U+11800 */
+185,185,185,185,185,185,185,186,187,185,188,141,141,141,141,141, /* U+12000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+12800 */
+189,189,189,189,189,189,189,189,190,141,141,141,141,141,141,141, /* U+13000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+13800 */
+141,141,141,141,141,141,141,141,191,191,191,191,192,141,141,141, /* U+14000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+14800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+15000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+15800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+16000 */
+193,193,193,193,194,195,196,197,141,141,141,141,198,199,200,201, /* U+16800 */
+202,202,202,202,202,202,202,202,202,202,202,202,202,202,202,202, /* U+17000 */
+202,202,202,202,202,202,202,202,202,202,202,202,202,202,202,202, /* U+17800 */
+202,202,202,202,202,202,202,202,202,202,202,202,202,202,202,203, /* U+18000 */
+202,202,202,202,202,204,141,141,141,141,141,141,141,141,141,141, /* U+18800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+19000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+19800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+1A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+1A800 */
+205,206,207,208,208,209,141,141,141,141,141,141,141,141,141,141, /* U+1B000 */
+141,141,141,141,141,141,141,141,210,211,141,141,141,141,141,141, /* U+1B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+1C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+1C800 */
+ 71,212,213,214,215,216,217,141,218,219,220,221,222,223,224,225, /* U+1D000 */
+226,226,226,226,227,228,141,141,141,141,141,141,141,141,141,141, /* U+1D800 */
+229,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+1E000 */
+230,231,232,141,141,141,141,141,233,234,141,141,235,236,141,141, /* U+1E800 */
+237,238,239,240,241,242,243,244,243,243,245,243,246,247,248,249, /* U+1F000 */
+250,251,252,253,254,242,242,242,242,242,242,242,242,242,242,255, /* U+1F800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+20000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+20800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+21000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+21800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+22000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+22800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+23000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+23800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+24000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+24800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+25000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+25800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+26000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+26800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+27000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+27800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+28000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+28800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+29000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+29800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,256, 97, 97, /* U+2A000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2A800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,257, 97, /* U+2B000 */
+258, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2B800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2C000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,259, 97, 97, /* U+2C800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2D000 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2D800 */
+ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, /* U+2E000 */
+ 97, 97, 97, 97, 97, 97, 97,260,141,141,141,141,141,141,141,141, /* U+2E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+2F000 */
+ 97, 97, 97, 97,261,141,141,141,141,141,141,141,141,141,141,141, /* U+2F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+30000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+30800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+31000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+31800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+32000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+32800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+33000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+33800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+34000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+34800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+35000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+35800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+36000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+36800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+37000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+37800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+38000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+38800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+39000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+39800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+3F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+40000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+40800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+41000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+41800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+42000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+42800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+43000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+43800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+44000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+44800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+45000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+45800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+46000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+46800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+47000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+47800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+48000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+48800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+49000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+49800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+4F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+50000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+50800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+51000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+51800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+52000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+52800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+53000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+53800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+54000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+54800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+55000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+55800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+56000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+56800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+57000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+57800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+58000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+58800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+59000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+59800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+5F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+60000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+60800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+61000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+61800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+62000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+62800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+63000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+63800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+64000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+64800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+65000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+65800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+66000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+66800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+67000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+67800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+68000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+68800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+69000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+69800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+6F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+70000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+70800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+71000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+71800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+72000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+72800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+73000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+73800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+74000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+74800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+75000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+75800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+76000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+76800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+77000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+77800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+78000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+78800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+79000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+79800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+7F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+80000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+80800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+81000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+81800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+82000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+82800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+83000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+83800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+84000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+84800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+85000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+85800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+86000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+86800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+87000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+87800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+88000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+88800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+89000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+89800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+8F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+90000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+90800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+91000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+91800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+92000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+92800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+93000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+93800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+94000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+94800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+95000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+95800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+96000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+96800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+97000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+97800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+98000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+98800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+99000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+99800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9A000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9A800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9B000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9B800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9C000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9C800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9D000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9D800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9E000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9E800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9F000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+9F800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A0000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A0800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A1000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A1800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A2000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A2800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A3000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A3800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A4000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A4800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A5000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A5800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A6000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A6800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A7000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A7800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A8000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A8800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A9000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+A9800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AA000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AA800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AB000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AB800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AC000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AC800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AD000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AD800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AE000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AE800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AF000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+AF800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B0000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B0800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B1000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B1800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B2000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B2800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B3000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B3800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B4000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B4800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B5000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B5800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B6000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B6800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B7000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B7800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B8000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B8800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B9000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+B9800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BA000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BA800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BB000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BB800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BC000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BC800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BD000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BD800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BE000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BE800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BF000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+BF800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C0000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C0800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C1000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C1800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C2000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C2800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C3000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C3800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C4000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C4800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C5000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C5800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C6000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C6800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C7000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C7800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C8000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C8800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C9000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+C9800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CA000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CA800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CB000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CB800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CC000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CC800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CD000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CD800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CE000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CE800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CF000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+CF800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D0000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D0800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D1000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D1800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D2000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D2800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D3000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D3800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D4000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D4800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D5000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D5800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D6000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D6800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D7000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D7800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D8000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D8800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D9000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+D9800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DA000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DA800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DB000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DB800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DC000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DC800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DD000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DD800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DE000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DE800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DF000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+DF800 */
+262,263,264,265,263,263,263,263,263,263,263,263,263,263,263,263, /* U+E0000 */
+263,263,263,263,263,263,263,263,263,263,263,263,263,263,263,263, /* U+E0800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E1000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E1800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E2000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E2800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E3000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E3800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E4000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E4800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E5000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E5800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E6000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E6800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E7000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E7800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E8000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E8800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E9000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+E9800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EA000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EA800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EB000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EB800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EC000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EC800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+ED000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+ED800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EE000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EE800 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EF000 */
+141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141, /* U+EF800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F0000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F0800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F1000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F1800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F2000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F2800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F3000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F3800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F4000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F4800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F5000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F5800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F6000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F6800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F7000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F7800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F8000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F8800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F9000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+F9800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FA000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FA800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FB000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FB800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FC000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FC800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FD000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FD800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FE000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FE800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+FF000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,266, /* U+FF800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+100000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+100800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+101000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+101800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+102000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+102800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+103000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+103800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+104000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+104800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+105000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+105800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+106000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+106800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+107000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+107800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+108000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+108800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+109000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+109800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10A000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10A800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10B000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10B800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10C000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10C800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10D000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10D800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10E000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10E800 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125, /* U+10F000 */
+125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,266, /* U+10F800 */
+};
+
+const uint16_t PRIV(ucd_stage2)[] = { /* 68352 bytes, block = 128 */
+/* block 0 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 3, 4, 4, 4, 5, 4, 4, 4, 6, 7, 4, 8, 4, 9, 4, 4,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 4, 4, 8, 8, 8, 4,
+ 4, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11,
+ 11, 11, 11, 13, 11, 11, 11, 11, 11, 11, 11, 6, 4, 7, 14, 15,
+ 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16, 16, 16, 16,
+ 16, 16, 16, 18, 16, 16, 16, 16, 16, 16, 16, 6, 8, 7, 8, 0,
+
+/* block 1 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 3, 4, 5, 5, 5, 5, 19, 4, 14, 20, 21, 22, 8, 23, 20, 14,
+ 19, 8, 24, 24, 14, 25, 4, 4, 14, 24, 21, 26, 24, 24, 24, 4,
+ 11, 11, 11, 11, 11, 27, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+ 11, 11, 11, 11, 11, 11, 11, 8, 11, 11, 11, 11, 11, 11, 11, 28,
+ 16, 16, 16, 16, 16, 29, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+ 16, 16, 16, 16, 16, 16, 16, 8, 16, 16, 16, 16, 16, 16, 16, 30,
+
+/* block 2 */
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 33, 34, 31, 32, 31, 32, 31, 32, 34, 31, 32, 31, 32, 31, 32, 31,
+ 32, 31, 32, 31, 32, 31, 32, 31, 32, 34, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 35, 31, 32, 31, 32, 31, 32, 36,
+
+/* block 3 */
+ 37, 38, 31, 32, 31, 32, 39, 31, 32, 40, 40, 31, 32, 34, 41, 42,
+ 43, 31, 32, 40, 44, 45, 46, 47, 31, 32, 48, 34, 46, 49, 50, 51,
+ 31, 32, 31, 32, 31, 32, 52, 31, 32, 52, 34, 34, 31, 32, 52, 31,
+ 32, 53, 53, 31, 32, 31, 32, 54, 31, 32, 34, 21, 31, 32, 34, 55,
+ 21, 21, 21, 21, 56, 57, 58, 59, 60, 61, 62, 63, 64, 31, 32, 31,
+ 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 65, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 34, 66, 67, 68, 31, 32, 69, 70, 31, 32, 31, 32, 31, 32, 31, 32,
+
+/* block 4 */
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 71, 34, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 34, 34, 34, 34, 34, 34, 72, 31, 32, 73, 74, 75,
+ 75, 31, 32, 76, 77, 78, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 79, 80, 81, 82, 83, 34, 84, 84, 34, 85, 34, 86, 87, 34, 34, 34,
+ 84, 88, 34, 89, 34, 90, 91, 34, 92, 93, 91, 94, 95, 34, 34, 93,
+ 34, 96, 97, 34, 34, 98, 34, 34, 34, 34, 34, 34, 34, 99, 34, 34,
+
+/* block 5 */
+100, 34, 34,100, 34, 34, 34,101,100,102,103,103,104, 34, 34, 34,
+ 34, 34,105, 34, 21, 34, 34, 34, 34, 34, 34, 34, 34,106,107, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+108,108,108,108,108,108,108,108,108,109,109,109,109,109,109,109,
+109,109, 14, 14, 14, 14,109,109,109,109,109,109,109,109,109,109,
+109,109, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+108,108,108,108,108, 14, 14, 14, 14, 14,110,110,109, 14,109, 14,
+ 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+
+/* block 6 */
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,112,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+113,114,113,114,109,115,113,114,116,116,117,118,118,118, 4,119,
+
+/* block 7 */
+116,116,116,116,115, 14,120, 4,121,121,121,116,122,116,123,123,
+124,125,126,125,125,127,125,125,128,129,130,125,131,125,125,125,
+132,133,116,134,125,125,135,125,125,136,125,125,137,138,138,138,
+124,139,140,139,139,141,139,139,142,143,144,139,145,139,139,139,
+146,147,148,149,139,139,150,139,139,151,139,139,152,153,153,154,
+155,156,157,157,157,158,159,160,113,114,113,114,113,114,113,114,
+113,114,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+163,164,165,166,167,168,169,113,114,170,113,114,124,171,171,171,
+
+/* block 8 */
+172,172,172,172,172,172,172,172,172,172,172,172,172,172,172,172,
+173,173,174,173,175,173,173,173,173,173,173,173,173,173,176,173,
+173,177,178,173,173,173,173,173,173,173,179,173,173,173,173,173,
+180,180,181,180,182,180,180,180,180,180,180,180,180,180,183,180,
+180,184,185,180,180,180,180,180,180,180,186,180,180,180,180,180,
+187,187,187,187,187,187,187,187,187,187,187,187,187,187,187,187,
+188,189,190,191,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+
+/* block 9 */
+188,189,192,193,193,111,111,193,194,194,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+195,188,189,188,189,188,189,188,189,188,189,188,189,188,189,196,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+
+/* block 10 */
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+116,197,197,197,197,197,197,197,197,197,197,197,197,197,197,197,
+197,197,197,197,197,197,197,197,197,197,197,197,197,197,197,197,
+197,197,197,197,197,197,197,116,116,198,199,199,199,199,199,199,
+200,201,201,201,201,201,201,201,201,201,201,201,201,201,201,201,
+201,201,201,201,201,201,201,201,201,201,201,201,201,201,201,201,
+
+/* block 11 */
+201,201,201,201,201,201,201,200,200, 4,202,116,116,203,203,204,
+116,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,
+205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,
+205,205,205,205,205,205,205,205,205,205,205,205,205,205,206,205,
+207,205,205,207,205,205,207,205,116,116,116,116,116,116,116,116,
+208,208,208,208,208,208,208,208,208,208,208,208,208,208,208,208,
+208,208,208,208,208,208,208,208,208,208,208,116,116,116,116,208,
+208,208,208,207,207,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 12 */
+209,209,209,209,209,210,211,211,211,212,212,213, 4,212,214,214,
+215,215,215,215,215,215,215,215,215,215,215, 4,216,116,212, 4,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+109,217,217,217,217,217,217,217,217,217,217,111,111,111,111,111,
+111,111,111,111,111,111,215,215,215,215,215,215,215,215,215,215,
+218,218,218,218,218,218,218,218,218,218,212,212,212,212,217,217,
+111,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 13 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,212,217,215,215,215,215,215,215,215,210,214,215,
+215,215,215,215,215,219,219,215,215,214,215,215,215,215,217,217,
+218,218,218,218,218,218,218,218,218,218,217,217,217,214,214,217,
+
+/* block 14 */
+220,220,220,220,220,220,220,220,220,220,220,220,220,220,116,221,
+222,223,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
+222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,222,
+223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,
+223,223,223,223,223,223,223,223,223,223,223,116,116,222,222,222,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 15 */
+224,224,224,224,224,224,224,224,224,224,224,224,224,224,224,224,
+224,224,224,224,224,224,224,224,224,224,224,224,224,224,224,224,
+224,224,224,224,224,224,225,225,225,225,225,225,225,225,225,225,
+225,224,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+226,226,226,226,226,226,226,226,226,226,227,227,227,227,227,227,
+227,227,227,227,227,227,227,227,227,227,227,227,227,227,227,227,
+227,227,227,227,227,227,227,227,227,227,227,228,228,228,228,228,
+228,228,228,228,229,229,230,231,231,231,229,116,116,228,232,232,
+
+/* block 16 */
+233,233,233,233,233,233,233,233,233,233,233,233,233,233,233,233,
+233,233,233,233,233,233,234,234,234,234,235,234,234,234,234,234,
+234,234,234,234,235,234,234,234,235,234,234,234,234,234,116,116,
+236,236,236,236,236,236,236,236,236,236,236,236,236,236,236,116,
+237,237,237,237,237,237,237,237,237,237,237,237,237,237,237,237,
+237,237,237,237,237,237,237,237,237,238,238,238,116,116,239,116,
+222,222,222,222,222,222,222,222,222,222,222,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 17 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,116,217,217,217,217,217,217,217,217,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,215,215,215,215,215,215,215,215,215,215,215,215,215,
+215,215,210,215,215,215,215,215,215,215,215,215,215,215,215,215,
+215,215,215,215,215,215,215,215,215,215,215,215,215,215,215,215,
+
+/* block 18 */
+240,240,240,241,242,242,242,242,242,242,242,242,242,242,242,242,
+242,242,242,242,242,242,242,242,242,242,242,242,242,242,242,242,
+242,242,242,242,242,242,242,242,242,242,242,242,242,242,242,242,
+242,242,242,242,242,242,242,242,242,242,240,241,240,242,241,241,
+241,240,240,240,240,240,240,240,240,241,241,241,241,240,241,241,
+242,111,111,240,240,240,240,240,242,242,242,242,242,242,242,242,
+242,242,240,240, 4, 4,243,243,243,243,243,243,243,243,243,243,
+244,245,242,242,242,242,242,242,242,242,242,242,242,242,242,242,
+
+/* block 19 */
+246,247,248,248,116,246,246,246,246,246,246,246,246,116,116,246,
+246,116,116,246,246,246,246,246,246,246,246,246,246,246,246,246,
+246,246,246,246,246,246,246,246,246,116,246,246,246,246,246,246,
+246,116,246,116,116,116,246,246,246,246,116,116,247,246,249,248,
+248,247,247,247,247,116,116,248,248,116,116,248,248,247,246,116,
+116,116,116,116,116,116,116,249,116,116,116,116,246,246,116,246,
+246,246,247,247,116,116,250,250,250,250,250,250,250,250,250,250,
+246,246,251,251,252,252,252,252,252,252,253,251,246,254,247,116,
+
+/* block 20 */
+116,255,255,256,116,257,257,257,257,257,257,116,116,116,116,257,
+257,116,116,257,257,257,257,257,257,257,257,257,257,257,257,257,
+257,257,257,257,257,257,257,257,257,116,257,257,257,257,257,257,
+257,116,257,257,116,257,257,116,257,257,116,116,255,116,256,256,
+256,255,255,116,116,116,116,255,255,116,116,255,255,255,116,116,
+116,255,116,116,116,116,116,116,116,257,257,257,257,116,257,116,
+116,116,116,116,116,116,258,258,258,258,258,258,258,258,258,258,
+255,255,257,257,257,255,259,116,116,116,116,116,116,116,116,116,
+
+/* block 21 */
+116,260,260,261,116,262,262,262,262,262,262,262,262,262,116,262,
+262,262,116,262,262,262,262,262,262,262,262,262,262,262,262,262,
+262,262,262,262,262,262,262,262,262,116,262,262,262,262,262,262,
+262,116,262,262,116,262,262,262,262,262,116,116,260,262,261,261,
+261,260,260,260,260,260,116,260,260,261,116,261,261,260,116,116,
+262,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+262,262,260,260,116,116,263,263,263,263,263,263,263,263,263,263,
+264,265,116,116,116,116,116,116,116,262,260,260,260,260,260,260,
+
+/* block 22 */
+116,266,267,267,116,268,268,268,268,268,268,268,268,116,116,268,
+268,116,116,268,268,268,268,268,268,268,268,268,268,268,268,268,
+268,268,268,268,268,268,268,268,268,116,268,268,268,268,268,268,
+268,116,268,268,116,268,268,268,268,268,116,116,266,268,269,266,
+267,266,266,266,266,116,116,267,267,116,116,267,267,266,116,116,
+116,116,116,116,116,116,266,269,116,116,116,116,268,268,116,268,
+268,268,266,266,116,116,270,270,270,270,270,270,270,270,270,270,
+271,268,272,272,272,272,272,272,116,116,116,116,116,116,116,116,
+
+/* block 23 */
+116,116,273,274,116,274,274,274,274,274,274,116,116,116,274,274,
+274,116,274,274,274,274,116,116,116,274,274,116,274,116,274,274,
+116,116,116,274,274,116,116,116,274,274,274,116,116,116,274,274,
+274,274,274,274,274,274,274,274,274,274,116,116,116,116,275,276,
+273,276,276,116,116,116,276,276,276,116,276,276,276,273,116,116,
+274,116,116,116,116,116,116,275,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,277,277,277,277,277,277,277,277,277,277,
+278,278,278,279,279,279,279,279,279,280,279,116,116,116,116,116,
+
+/* block 24 */
+281,282,282,282,281,283,283,283,283,283,283,283,283,116,283,283,
+283,116,283,283,283,283,283,283,283,283,283,283,283,283,283,283,
+283,283,283,283,283,283,283,283,283,116,283,283,283,283,283,283,
+283,283,283,283,283,283,283,283,283,283,116,116,116,283,281,281,
+281,282,282,282,282,116,281,281,281,116,281,281,281,281,116,116,
+116,116,116,116,116,281,281,116,283,283,283,116,116,116,116,116,
+283,283,281,281,116,116,284,284,284,284,284,284,284,284,284,284,
+116,116,116,116,116,116,116,116,285,285,285,285,285,285,285,286,
+
+/* block 25 */
+287,288,289,289,290,287,287,287,287,287,287,287,287,116,287,287,
+287,116,287,287,287,287,287,287,287,287,287,287,287,287,287,287,
+287,287,287,287,287,287,287,287,287,116,287,287,287,287,287,287,
+287,287,287,287,116,287,287,287,287,287,116,116,288,287,289,288,
+289,289,291,289,289,116,288,289,289,116,289,289,288,288,116,116,
+116,116,116,116,116,291,291,116,116,116,116,116,116,116,287,116,
+287,287,288,288,116,116,292,292,292,292,292,292,292,292,292,292,
+116,287,287,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 26 */
+293,293,294,294,116,295,295,295,295,295,295,295,295,116,295,295,
+295,116,295,295,295,295,295,295,295,295,295,295,295,295,295,295,
+295,295,295,295,295,295,295,295,295,295,295,295,295,295,295,295,
+295,295,295,295,295,295,295,295,295,295,295,293,293,295,296,294,
+294,293,293,293,293,116,294,294,294,116,294,294,294,293,297,298,
+116,116,116,116,295,295,295,296,299,299,299,299,299,299,299,295,
+295,295,293,293,116,116,300,300,300,300,300,300,300,300,300,300,
+299,299,299,299,299,299,299,299,299,298,295,295,295,295,295,295,
+
+/* block 27 */
+116,116,301,301,116,302,302,302,302,302,302,302,302,302,302,302,
+302,302,302,302,302,302,302,116,116,116,302,302,302,302,302,302,
+302,302,302,302,302,302,302,302,302,302,302,302,302,302,302,302,
+302,302,116,302,302,302,302,302,302,302,302,302,116,302,116,116,
+302,302,302,302,302,302,302,116,116,116,303,116,116,116,116,304,
+301,301,303,303,303,116,303,116,301,301,301,301,301,301,301,304,
+116,116,116,116,116,116,305,305,305,305,305,305,305,305,305,305,
+116,116,301,301,306,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 28 */
+116,307,307,307,307,307,307,307,307,307,307,307,307,307,307,307,
+307,307,307,307,307,307,307,307,307,307,307,307,307,307,307,307,
+307,307,307,307,307,307,307,307,307,307,307,307,307,307,307,307,
+307,308,307,309,308,308,308,308,308,308,308,116,116,116,116, 5,
+307,307,307,307,307,307,310,308,308,308,308,308,308,308,308,311,
+312,312,312,312,312,312,312,312,312,312,311,311,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 29 */
+116,313,313,116,313,116,116,313,313,116,313,116,116,313,116,116,
+116,116,116,116,313,313,313,313,116,313,313,313,313,313,313,313,
+116,313,313,313,116,313,116,313,116,116,313,313,116,313,313,313,
+313,314,313,315,314,314,314,314,314,314,116,314,314,313,116,116,
+313,313,313,313,313,116,316,116,314,314,314,314,314,314,116,116,
+317,317,317,317,317,317,317,317,317,317,116,116,313,313,313,313,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 30 */
+318,319,319,319,320,320,320,320,320,320,320,320,320,320,320,320,
+320,320,320,319,320,319,319,319,321,321,319,319,319,319,319,319,
+322,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,
+323,323,323,323,319,321,319,321,319,321,324,325,324,325,326,326,
+318,318,318,318,318,318,318,318,116,318,318,318,318,318,318,318,
+318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,
+318,318,318,318,318,318,318,318,318,318,318,318,318,116,116,116,
+116,321,321,321,321,321,321,321,321,321,321,321,321,321,321,326,
+
+/* block 31 */
+321,321,321,321,321,320,321,321,318,318,318,318,318,321,321,321,
+321,321,321,321,321,321,321,321,116,321,321,321,321,321,321,321,
+321,321,321,321,321,321,321,321,321,321,321,321,321,321,321,321,
+321,321,321,321,321,321,321,321,321,321,321,321,321,116,319,319,
+319,319,319,319,319,319,321,319,319,319,319,319,319,116,319,319,
+320,320,320,320,320, 19, 19, 19, 19,320,320,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 32 */
+327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,
+327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,
+327,327,327,327,327,327,327,327,327,327,327,328,328,329,329,329,
+329,330,329,329,329,329,329,329,328,329,329,330,330,329,329,327,
+331,331,331,331,331,331,331,331,331,331,332,332,332,332,332,332,
+327,327,327,327,327,327,330,330,329,329,327,327,327,327,329,329,
+329,327,328,328,328,327,327,328,328,328,328,328,328,328,327,327,
+327,329,329,329,329,327,327,327,327,327,327,327,327,327,327,327,
+
+/* block 33 */
+327,327,329,328,330,329,329,328,328,328,328,328,328,329,327,328,
+331,331,331,331,331,331,331,331,331,331,328,328,328,329,333,333,
+334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,
+334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,
+334,334,334,334,334,334,116,334,116,116,116,116,116,334,116,116,
+335,335,335,335,335,335,335,335,335,335,335,335,335,335,335,335,
+335,335,335,335,335,335,335,335,335,335,335,335,335,335,335,335,
+335,335,335,335,335,335,335,335,335,335,335, 4,336,335,335,335,
+
+/* block 34 */
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,
+338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,
+
+/* block 35 */
+338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,
+338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,
+338,338,338,338,338,338,338,338,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+
+/* block 36 */
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,116,340,340,340,340,116,116,
+340,340,340,340,340,340,340,116,340,116,340,340,340,340,116,116,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+
+/* block 37 */
+340,340,340,340,340,340,340,340,340,116,340,340,340,340,116,116,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,116,340,340,340,340,116,116,340,340,340,340,340,340,340,116,
+340,116,340,340,340,340,116,116,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+
+/* block 38 */
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,116,340,340,340,340,116,116,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,340,340,340,340,116,116,341,341,341,
+342,342,342,342,342,342,342,342,342,343,343,343,343,343,343,343,
+343,343,343,343,343,343,343,343,343,343,343,343,343,116,116,116,
+
+/* block 39 */
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+344,344,344,344,344,344,344,344,344,344,116,116,116,116,116,116,
+345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,
+345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,
+345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,
+345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,
+345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,
+346,346,346,346,346,346,116,116,347,347,347,347,347,347,116,116,
+
+/* block 40 */
+348,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+
+/* block 41 */
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+
+/* block 42 */
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,350,350,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+
+/* block 43 */
+351,352,352,352,352,352,352,352,352,352,352,352,352,352,352,352,
+352,352,352,352,352,352,352,352,352,352,352,353,354,116,116,116,
+355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,
+355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,
+355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,
+355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,355,
+355,355,355,355,355,355,355,355,355,355,355, 4, 4, 4,356,356,
+356,355,355,355,355,355,355,355,355,116,116,116,116,116,116,116,
+
+/* block 44 */
+357,357,357,357,357,357,357,357,357,357,357,357,357,116,357,357,
+357,357,358,358,358,116,116,116,116,116,116,116,116,116,116,116,
+359,359,359,359,359,359,359,359,359,359,359,359,359,359,359,359,
+359,359,360,360,360, 4, 4,116,116,116,116,116,116,116,116,116,
+361,361,361,361,361,361,361,361,361,361,361,361,361,361,361,361,
+361,361,362,362,116,116,116,116,116,116,116,116,116,116,116,116,
+363,363,363,363,363,363,363,363,363,363,363,363,363,116,363,363,
+363,116,364,364,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 45 */
+365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,
+365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,
+365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,365,
+365,365,365,365,366,366,367,366,366,366,366,366,366,366,367,367,
+367,367,367,367,367,367,366,367,367,366,366,366,366,366,366,366,
+366,366,366,366,368,368,368,369,368,368,368,370,365,366,116,116,
+371,371,371,371,371,371,371,371,371,371,116,116,116,116,116,116,
+372,372,372,372,372,372,372,372,372,372,116,116,116,116,116,116,
+
+/* block 46 */
+373,373, 4, 4,373, 4,374,373,373,373,373,375,375,375,376,116,
+377,377,377,377,377,377,377,377,377,377,116,116,116,116,116,116,
+378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,379,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,116,116,116,116,116,116,116,
+
+/* block 47 */
+378,378,378,378,378,375,375,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,
+378,378,378,378,378,378,378,378,378,375,378,116,116,116,116,116,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,349,
+349,349,349,349,349,349,116,116,116,116,116,116,116,116,116,116,
+
+/* block 48 */
+380,380,380,380,380,380,380,380,380,380,380,380,380,380,380,380,
+380,380,380,380,380,380,380,380,380,380,380,380,380,380,380,116,
+381,381,381,382,382,382,382,381,381,382,382,382,116,116,116,116,
+382,382,381,382,382,382,382,382,382,381,381,381,116,116,116,116,
+383,116,116,116,384,384,385,385,385,385,385,385,385,385,385,385,
+386,386,386,386,386,386,386,386,386,386,386,386,386,386,386,386,
+386,386,386,386,386,386,386,386,386,386,386,386,386,386,116,116,
+386,386,386,386,386,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 49 */
+387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,
+387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,
+387,387,387,387,387,387,387,387,387,387,387,387,116,116,116,116,
+387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,387,
+387,387,387,387,387,387,387,387,387,387,116,116,116,116,116,116,
+388,388,388,388,388,388,388,388,388,388,389,116,116,116,390,390,
+391,391,391,391,391,391,391,391,391,391,391,391,391,391,391,391,
+391,391,391,391,391,391,391,391,391,391,391,391,391,391,391,391,
+
+/* block 50 */
+392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,
+392,392,392,392,392,392,392,393,393,394,394,393,116,116,395,395,
+396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,
+396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,
+396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,396,
+396,396,396,396,396,397,398,397,398,398,398,398,398,398,398,116,
+398,399,398,399,399,398,398,398,398,398,398,398,398,397,397,397,
+397,397,397,398,398,398,398,398,398,398,398,398,398,116,116,398,
+
+/* block 51 */
+400,400,400,400,400,400,400,400,400,400,116,116,116,116,116,116,
+400,400,400,400,400,400,400,400,400,400,116,116,116,116,116,116,
+401,401,401,401,401,401,401,402,401,401,401,401,401,401,116,116,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,403,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 52 */
+404,404,404,404,405,406,406,406,406,406,406,406,406,406,406,406,
+406,406,406,406,406,406,406,406,406,406,406,406,406,406,406,406,
+406,406,406,406,406,406,406,406,406,406,406,406,406,406,406,406,
+406,406,406,406,404,405,404,404,404,404,404,405,404,405,405,405,
+405,405,404,405,405,406,406,406,406,406,406,406,116,116,116,116,
+407,407,407,407,407,407,407,407,407,407,408,408,408,408,408,408,
+408,409,409,409,409,409,409,409,409,409,409,404,404,404,404,404,
+404,404,404,404,409,409,409,409,409,409,409,409,409,116,116,116,
+
+/* block 53 */
+410,410,411,412,412,412,412,412,412,412,412,412,412,412,412,412,
+412,412,412,412,412,412,412,412,412,412,412,412,412,412,412,412,
+412,411,410,410,410,410,411,411,410,410,411,410,410,410,412,412,
+413,413,413,413,413,413,413,413,413,413,412,412,412,412,412,412,
+414,414,414,414,414,414,414,414,414,414,414,414,414,414,414,414,
+414,414,414,414,414,414,414,414,414,414,414,414,414,414,414,414,
+414,414,414,414,414,414,415,416,415,415,416,416,416,415,416,415,
+415,415,416,416,116,116,116,116,116,116,116,116,417,417,417,417,
+
+/* block 54 */
+418,418,418,418,418,418,418,418,418,418,418,418,418,418,418,418,
+418,418,418,418,418,418,418,418,418,418,418,418,418,418,418,418,
+418,418,418,418,419,419,419,419,419,419,419,419,420,420,420,420,
+420,420,420,420,419,419,420,420,116,116,116,421,421,421,421,421,
+422,422,422,422,422,422,422,422,422,422,116,116,116,418,418,418,
+423,423,423,423,423,423,423,423,423,423,424,424,424,424,424,424,
+424,424,424,424,424,424,424,424,424,424,424,424,424,424,424,424,
+424,424,424,424,424,424,424,424,425,425,425,425,425,425,426,426,
+
+/* block 55 */
+427,428,429,430,431,432,433,434,435,116,116,116,116,116,116,116,
+436,436,436,436,436,436,436,436,436,436,436,436,436,436,436,436,
+436,436,436,436,436,436,436,436,436,436,436,436,436,436,436,436,
+436,436,436,436,436,436,436,436,436,436,436,116,116,436,436,436,
+437,437,437,437,437,437,437,437,116,116,116,116,116,116,116,116,
+111,111,111, 4,111,111,111,111,111,111,111,111,111,111,111,111,
+111,438,111,111,111,111,111,111,111,439,439,439,439,111,439,439,
+439,439,438,438,111,439,439,438,111,111,116,116,116,116,116,116,
+
+/* block 56 */
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34,124,124,124,124,124,440,108,108,108,108,
+108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,
+108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,
+108,108,108,108,108,108,108,108,108,108,108,108,108,117,117,117,
+117,117,108,108,108,108,117,117,117,117,117, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34,441,442, 34, 34, 34,443, 34, 34,
+
+/* block 57 */
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,108,108,108,108,108,
+108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,
+108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,117,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,116,111,111,111,111,111,
+
+/* block 58 */
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+444,445, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+
+/* block 59 */
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 34, 34, 34, 34, 34,446, 34, 34,447, 34,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+
+/* block 60 */
+448,448,448,448,448,448,448,448,449,449,449,449,449,449,449,449,
+448,448,448,448,448,448,116,116,449,449,449,449,449,449,116,116,
+448,448,448,448,448,448,448,448,449,449,449,449,449,449,449,449,
+448,448,448,448,448,448,448,448,449,449,449,449,449,449,449,449,
+448,448,448,448,448,448,116,116,449,449,449,449,449,449,116,116,
+124,448,124,448,124,448,124,448,116,449,116,449,116,449,116,449,
+448,448,448,448,448,448,448,448,449,449,449,449,449,449,449,449,
+450,450,451,451,451,451,452,452,453,453,454,454,455,455,116,116,
+
+/* block 61 */
+448,448,448,448,448,448,448,448,456,456,456,456,456,456,456,456,
+448,448,448,448,448,448,448,448,456,456,456,456,456,456,456,456,
+448,448,448,448,448,448,448,448,456,456,456,456,456,456,456,456,
+448,448,124,457,124,116,124,124,449,449,458,458,459,115,460,115,
+115,115,124,457,124,116,124,124,461,461,461,461,459,115,115,115,
+448,448,124,124,116,116,124,124,449,449,462,462,116,115,115,115,
+448,448,124,124,124,165,124,124,449,449,463,463,170,115,115,115,
+116,116,124,457,124,116,124,124,464,464,465,465,459,115,115,116,
+
+/* block 62 */
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 23,466,467, 23, 23,
+ 9, 9, 9, 9, 9, 9, 4, 4, 22, 26, 6, 22, 22, 26, 6, 22,
+ 4, 4, 4, 4, 4, 4, 4, 4,468,469, 23, 23, 23, 23, 23, 3,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 22, 26, 4,470, 4, 4, 15,
+ 15, 4, 4, 4, 8, 6, 7, 4, 4,470, 4, 4, 4, 4, 4, 4,
+ 4, 4, 8, 4, 15, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3,
+ 23, 23, 23, 23, 23,471, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
+ 24,108,116,116, 24, 24, 24, 24, 24, 24, 8, 8, 8, 6, 7,108,
+
+/* block 63 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 8, 8, 8, 6, 7,116,
+108,108,108,108,108,108,108,108,108,108,108,108,108,116,116,116,
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+111,111,111,111,111,111,111,111,111,111,111,111,111,403,403,403,
+403,111,403,403,403,111,111,111,111,111,111,111,111,111,111,111,
+111,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 64 */
+ 19, 19,472, 19, 19, 19, 19,472, 19, 19,473,472,472,472,473,473,
+472,472,472,473, 19,472, 19, 19, 8,472,472,472,472,472, 19, 19,
+ 19, 19, 20, 19,472, 19,474, 19,472, 19,475,476,472,472, 19,473,
+472,472,477,472,473,439,439,439,439,478, 19, 19,473,473,472,472,
+ 8, 8, 8, 8, 8,472,473,473,473,473, 19, 8, 19, 19,479, 19,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+480,480,480,480,480,480,480,480,480,480,480,480,480,480,480,480,
+481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,
+
+/* block 65 */
+482,482,482, 31, 32,482,482,482,482, 24, 19, 19,116,116,116,116,
+ 8, 8, 8, 8,483, 20, 20, 20, 20, 20, 8, 8, 19, 19, 19, 19,
+ 8, 19, 19, 8, 19, 19, 8, 19, 19, 20, 20, 19, 19, 19, 8, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 8, 8,
+ 19, 19, 8, 19, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+
+/* block 66 */
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+
+/* block 67 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 6, 7, 6, 7, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 19, 19, 19, 19,
+ 8, 8, 19, 19, 19, 19, 19, 19, 20, 6, 7, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 8, 19, 19, 19,
+
+/* block 68 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 20, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 8, 8, 8, 8,
+ 8, 8, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 19, 19, 19, 19, 20, 20, 20, 19, 19, 19, 19, 19,
+
+/* block 69 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+
+/* block 70 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19,484,484,484,484,484,484,484,484,484,484,
+484,484,485,484,484,484,484,484,484,484,484,484,484,484,484,484,
+486,486,486,486,486,486,486,486,486,486,486,486,486,486,486,486,
+486,486,486,486,486,486,486,486,486,486, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+
+/* block 71 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 72 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 20, 8, 19, 19, 19, 19, 19, 19, 19, 19,
+ 20, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 8, 8, 8,483,483,483,483, 8,
+
+/* block 73 */
+ 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,483,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 74 */
+ 20, 20, 20, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 75 */
+ 20, 20, 20, 20, 20, 20, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 19, 20, 19, 20, 19, 19, 19, 19, 19, 19, 20, 19, 19,
+ 19, 20, 19, 19, 19, 19, 19, 19, 20, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 20, 19, 19, 20, 19, 19, 19, 19, 20, 19, 20, 19,
+ 19, 19, 19, 20, 20, 20, 19, 20, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 20, 20, 20, 20, 20, 6, 7, 6, 7, 6, 7, 6, 7,
+ 6, 7, 6, 7, 6, 7, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+
+/* block 76 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 19, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20,
+ 8, 8, 8, 8, 8, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+
+/* block 77 */
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,487,
+
+/* block 78 */
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8,483,483, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+
+/* block 79 */
+ 8, 8, 8, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6,
+ 7, 6, 7, 6, 7, 6, 7, 6, 7, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 6, 7, 6, 7, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 7, 8, 8,
+
+/* block 80 */
+ 19, 19, 19, 19, 19, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 19, 19, 8, 8, 8, 8, 8, 8, 19, 19, 19,
+ 20, 19, 19, 19, 19, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19,116,116, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 81 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19,116,116, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19,116, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,116,
+
+/* block 82 */
+488,488,488,488,488,488,488,488,488,488,488,488,488,488,488,488,
+488,488,488,488,488,488,488,488,488,488,488,488,488,488,488,488,
+488,488,488,488,488,488,488,488,488,488,488,488,488,488,488,116,
+489,489,489,489,489,489,489,489,489,489,489,489,489,489,489,489,
+489,489,489,489,489,489,489,489,489,489,489,489,489,489,489,489,
+489,489,489,489,489,489,489,489,489,489,489,489,489,489,489,116,
+ 31, 32,490,491,492,493,494, 31, 32, 31, 32, 31, 32,495,496,497,
+498, 34, 31, 32, 34, 31, 32, 34, 34, 34, 34, 34,108,108,499,499,
+
+/* block 83 */
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,161,162,161,162,161,162,161,162,161,162,161,162,
+161,162,161,162,500,501,501,501,501,501,501,161,162,161,162,502,
+502,502,161,162,116,116,116,116,116,503,503,503,503,504,503,503,
+
+/* block 84 */
+505,505,505,505,505,505,505,505,505,505,505,505,505,505,505,505,
+505,505,505,505,505,505,505,505,505,505,505,505,505,505,505,505,
+505,505,505,505,505,505,116,505,116,116,116,116,116,505,116,116,
+506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,
+506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,
+506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,506,
+506,506,506,506,506,506,506,506,116,116,116,116,116,116,116,507,
+508,116,116,116,116,116,116,116,116,116,116,116,116,116,116,509,
+
+/* block 85 */
+340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,340,
+340,340,340,340,340,340,340,116,116,116,116,116,116,116,116,116,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,116,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,116,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,116,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,116,
+193,193,193,193,193,193,193,193,193,193,193,193,193,193,193,193,
+193,193,193,193,193,193,193,193,193,193,193,193,193,193,193,193,
+
+/* block 86 */
+ 4, 4, 22, 26, 22, 26, 4, 4, 4, 22, 26, 4, 22, 26, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 9, 4, 4, 9, 4, 22, 26, 4, 4,
+ 22, 26, 6, 7, 6, 7, 6, 7, 6, 7, 4, 4, 4, 4, 4,109,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 9, 9, 4, 4, 4, 4,
+ 9, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 87 */
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,116,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 88 */
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+
+/* block 89 */
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,510,
+510,510,510,510,510,510,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,116,116,116,116,
+
+/* block 90 */
+ 3, 4, 4, 4, 19,511,439,512, 6, 7, 6, 7, 6, 7, 6, 7,
+ 6, 7, 19, 19, 6, 7, 6, 7, 6, 7, 6, 7, 9, 6, 7, 7,
+ 19,512,512,512,512,512,512,512,512,512,111,111,111,111,513,513,
+514,109,109,109,109,109, 19, 19,512,512,512,511,439,470, 19, 19,
+116,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+
+/* block 91 */
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,116,116,111,111, 14, 14,516,516,515,
+ 9,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517, 4,109,518,518,517,
+
+/* block 92 */
+116,116,116,116,116,519,519,519,519,519,519,519,519,519,519,519,
+519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,
+519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,
+116,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+
+/* block 93 */
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,116,
+ 19, 19, 24, 24, 24, 24, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,519,
+519,519,519,519,519,519,519,519,519,519,519,116,116,116,116,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19,116,116,116,116,116,116,116,116,116,116,116,116,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+
+/* block 94 */
+521,521,521,521,521,521,521,521,521,521,521,521,521,521,521,521,
+521,521,521,521,521,521,521,521,521,521,521,521,521,521,521,116,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 24, 24, 24, 24, 24, 24, 24, 24,
+ 19, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+521,521,521,521,521,521,521,521,521,521,521,521,521,521,521,521,
+521,521,521,521,521,521,521,521,521,521,521,521,521,521,521, 19,
+
+/* block 95 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 20, 19, 20, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,116,
+
+/* block 96 */
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,522,
+522,522,522,522,522,522,522,522, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 97 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+
+/* block 98 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,116,116,116,116,116,116,116,116,116,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 99 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 100 */
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,525,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+
+/* block 101 */
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,524,
+
+/* block 102 */
+524,524,524,524,524,524,524,524,524,524,524,524,524,116,116,116,
+526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,
+526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,
+526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,526,
+526,526,526,526,526,526,526,116,116,116,116,116,116,116,116,116,
+527,527,527,527,527,527,527,527,527,527,527,527,527,527,527,527,
+527,527,527,527,527,527,527,527,527,527,527,527,527,527,527,527,
+527,527,527,527,527,527,527,527,528,528,528,528,528,528,529,529,
+
+/* block 103 */
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+
+/* block 104 */
+530,530,530,530,530,530,530,530,530,530,530,530,531,532,532,532,
+530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,530,
+533,533,533,533,533,533,533,533,533,533,530,530,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+188,189,188,189,188,189,188,189,188,189,534,535,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,536,193,
+194,194,194,537,193,193,193,193,193,193,193,193,193,193,537,441,
+
+/* block 105 */
+188,189,188,189,188,189,188,189,188,189,188,189,188,189,188,189,
+188,189,188,189,188,189,188,189,188,189,188,189,441,441,193,193,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,539,539,539,539,539,539,539,539,539,539,
+540,540,541,541,541,541,541,541,116,116,116,116,116,116,116,116,
+
+/* block 106 */
+ 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+ 14, 14, 14, 14, 14, 14, 14,109,109,109,109,109,109,109,109,109,
+ 14, 14, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 34, 34, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+108, 34, 34, 34, 34, 34, 34, 34, 34, 31, 32, 31, 32,542, 31, 32,
+
+/* block 107 */
+ 31, 32, 31, 32, 31, 32, 31, 32,109, 14, 14, 31, 32,543, 34, 21,
+ 31, 32, 31, 32, 34, 34, 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,
+ 31, 32, 31, 32, 31, 32, 31, 32, 31, 32,544,545,546,547,544, 34,
+548,549,550,551, 31, 32, 31, 32, 31, 32,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116, 21,108,108, 34, 21, 21, 21, 21, 21,
+
+/* block 108 */
+552,552,553,552,552,552,553,552,552,552,552,553,552,552,552,552,
+552,552,552,552,552,552,552,552,552,552,552,552,552,552,552,552,
+552,552,552,554,554,553,553,554,555,555,555,555,116,116,116,116,
+ 24, 24, 24, 24, 24, 24, 19, 19, 5, 19,116,116,116,116,116,116,
+556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,
+556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,
+556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,556,
+556,556,556,556,557,557,557,557,116,116,116,116,116,116,116,116,
+
+/* block 109 */
+558,558,559,559,559,559,559,559,559,559,559,559,559,559,559,559,
+559,559,559,559,559,559,559,559,559,559,559,559,559,559,559,559,
+559,559,559,559,559,559,559,559,559,559,559,559,559,559,559,559,
+559,559,559,559,558,558,558,558,558,558,558,558,558,558,558,558,
+558,558,558,558,560,560,116,116,116,116,116,116,116,116,561,561,
+562,562,562,562,562,562,562,562,562,562,116,116,116,116,116,116,
+240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,
+240,240,242,242,242,242,242,242,244,244,244,242,244,242,242,240,
+
+/* block 110 */
+563,563,563,563,563,563,563,563,563,563,564,564,564,564,564,564,
+564,564,564,564,564,564,564,564,564,564,564,564,564,564,564,564,
+564,564,564,564,564,564,565,565,565,565,565,565,565,565, 4,566,
+567,567,567,567,567,567,567,567,567,567,567,567,567,567,567,567,
+567,567,567,567,567,567,567,568,568,568,568,568,568,568,568,568,
+568,568,569,569,116,116,116,116,116,116,116,116,116,116,116,570,
+337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,337,
+337,337,337,337,337,337,337,337,337,337,337,337,337,116,116,116,
+
+/* block 111 */
+571,571,571,572,573,573,573,573,573,573,573,573,573,573,573,573,
+573,573,573,573,573,573,573,573,573,573,573,573,573,573,573,573,
+573,573,573,573,573,573,573,573,573,573,573,573,573,573,573,573,
+573,573,573,571,572,572,571,571,571,571,572,572,571,572,572,572,
+572,574,574,574,574,574,574,574,574,574,574,574,574,574,116,109,
+575,575,575,575,575,575,575,575,575,575,116,116,116,116,574,574,
+327,327,327,327,327,329,576,327,327,327,327,327,327,327,327,327,
+331,331,331,331,331,331,331,331,331,331,327,327,327,327,327,116,
+
+/* block 112 */
+577,577,577,577,577,577,577,577,577,577,577,577,577,577,577,577,
+577,577,577,577,577,577,577,577,577,577,577,577,577,577,577,577,
+577,577,577,577,577,577,577,577,577,578,578,578,578,578,578,579,
+579,578,578,579,579,578,578,116,116,116,116,116,116,116,116,116,
+577,577,577,578,577,577,577,577,577,577,577,577,578,579,116,116,
+580,580,580,580,580,580,580,580,580,580,116,116,581,581,581,581,
+327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,327,
+576,327,327,327,327,327,327,333,333,333,327,328,329,328,327,327,
+
+/* block 113 */
+582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,
+582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,
+582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,582,
+583,582,583,583,583,582,582,583,583,582,582,582,582,582,583,583,
+582,583,582,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,582,582,584,585,585,
+586,586,586,586,586,586,586,586,586,586,586,587,588,588,587,587,
+589,589,586,590,590,587,588,116,116,116,116,116,116,116,116,116,
+
+/* block 114 */
+116,340,340,340,340,340,340,116,116,340,340,340,340,340,340,116,
+116,340,340,340,340,340,340,116,116,116,116,116,116,116,116,116,
+340,340,340,340,340,340,340,116,340,340,340,340,340,340,340,116,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34,591, 34, 34, 34, 34, 34, 34, 34, 14,108,108,108,108,
+ 34, 34, 34, 34, 34,124,116,116,116,116,116,116,116,116,116,116,
+592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,
+
+/* block 115 */
+592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,
+592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,
+592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,
+592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,592,
+586,586,586,586,586,586,586,586,586,586,586,586,586,586,586,586,
+586,586,586,586,586,586,586,586,586,586,586,586,586,586,586,586,
+586,586,586,587,587,588,587,587,588,587,587,589,587,588,116,116,
+593,593,593,593,593,593,593,593,593,593,116,116,116,116,116,116,
+
+/* block 116 */
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+
+/* block 117 */
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+
+/* block 118 */
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+
+/* block 119 */
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+
+/* block 120 */
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+
+/* block 121 */
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+
+/* block 122 */
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+594,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,594,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,594,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+
+/* block 123 */
+595,595,595,595,595,595,595,595,594,595,595,595,595,595,595,595,
+595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,595,
+595,595,595,595,116,116,116,116,116,116,116,116,116,116,116,116,
+338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,338,
+338,338,338,338,338,338,338,116,116,116,116,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,339,
+339,339,339,339,339,339,339,339,339,339,339,339,116,116,116,116,
+
+/* block 124 */
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,596,
+
+/* block 125 */
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+
+/* block 126 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,116,116,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+
+/* block 127 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 128 */
+ 34, 34, 34, 34, 34, 34, 34,116,116,116,116,116,116,116,116,116,
+116,116,116,200,200,200,200,200,116,116,116,116,116,208,205,208,
+208,208,208,208,208,208,208,208,208,598,208,208,208,208,208,208,
+208,208,208,208,208,208,208,116,208,208,208,208,208,116,208,116,
+208,208,116,208,208,116,208,208,208,208,208,208,208,208,208,208,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 129 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,599,599,599,599,599,599,599,599,599,599,599,599,599,599,
+599,599,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 130 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 131 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217, 7, 6,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+
+/* block 132 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+116,116,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+217,217,217,217,217,217,217,217,217,217,217,217,213,214,116,116,
+
+/* block 133 */
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+ 4, 4, 4, 4, 4, 4, 4, 6, 7, 4,116,116,116,116,116,116,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,193,193,
+ 4, 9, 9, 15, 15, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6,
+ 7, 6, 7, 6, 7, 4, 4, 6, 7, 4, 4, 4, 4, 15, 15, 15,
+ 4, 4, 4,116, 4, 4, 4, 4, 9, 6, 7, 6, 7, 6, 7, 4,
+ 4, 4, 8, 9, 8, 8, 8,116, 4, 5, 4, 4,116,116,116,116,
+217,217,217,217,217,116,217,217,217,217,217,217,217,217,217,217,
+
+/* block 134 */
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,116,116, 23,
+
+/* block 135 */
+116, 4, 4, 4, 5, 4, 4, 4, 6, 7, 4, 8, 4, 9, 4, 4,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 4, 4, 8, 8, 8, 4,
+ 4, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+ 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 6, 4, 7, 14, 15,
+ 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 6, 8, 7, 8, 6,
+ 7, 4, 6, 7, 4, 4,517,517,517,517,517,517,517,517,517,517,
+109,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+
+/* block 136 */
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,517,
+517,517,517,517,517,517,517,517,517,517,517,517,517,517,600,600,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,
+520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,116,
+116,116,520,520,520,520,520,520,116,116,520,520,520,520,520,520,
+116,116,520,520,520,520,520,520,116,116,520,520,520,116,116,116,
+ 5, 5, 8, 14, 19, 5, 5,116, 19, 8, 8, 8, 8, 19, 19,116,
+471,471,471,471,471,471,471,471,471, 23, 23, 23, 19, 19,116,116,
+
+/* block 137 */
+601,601,601,601,601,601,601,601,601,601,601,601,116,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,116,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,116,601,601,116,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,116,116,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 138 */
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,601,
+601,601,601,601,601,601,601,601,601,601,601,116,116,116,116,116,
+
+/* block 139 */
+ 4, 4, 4,116,116,116,116, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24,116,116,116, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,
+602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,
+602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,602,
+602,602,602,602,602,603,603,603,603,604,604,604,604,604,604,604,
+
+/* block 140 */
+604,604,604,604,604,604,604,604,604,604,603,603,604,604,604,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,116,116,116,116,
+604,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,111,116,116,
+
+/* block 141 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 142 */
+605,605,605,605,605,605,605,605,605,605,605,605,605,605,605,605,
+605,605,605,605,605,605,605,605,605,605,605,605,605,116,116,116,
+606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,
+606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,
+606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,606,
+606,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+111, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,116,116,116,116,
+
+/* block 143 */
+607,607,607,607,607,607,607,607,607,607,607,607,607,607,607,607,
+607,607,607,607,607,607,607,607,607,607,607,607,607,607,607,607,
+608,608,608,608,116,116,116,116,116,116,116,116,116,607,607,607,
+609,609,609,609,609,609,609,609,609,609,609,609,609,609,609,609,
+609,610,609,609,609,609,609,609,609,609,610,116,116,116,116,116,
+611,611,611,611,611,611,611,611,611,611,611,611,611,611,611,611,
+611,611,611,611,611,611,611,611,611,611,611,611,611,611,611,611,
+611,611,611,611,611,611,612,612,612,612,612,116,116,116,116,116,
+
+/* block 144 */
+613,613,613,613,613,613,613,613,613,613,613,613,613,613,613,613,
+613,613,613,613,613,613,613,613,613,613,613,613,613,613,116,614,
+615,615,615,615,615,615,615,615,615,615,615,615,615,615,615,615,
+615,615,615,615,615,615,615,615,615,615,615,615,615,615,615,615,
+615,615,615,615,116,116,116,116,615,615,615,615,615,615,615,615,
+616,617,617,617,617,617,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 145 */
+618,618,618,618,618,618,618,618,618,618,618,618,618,618,618,618,
+618,618,618,618,618,618,618,618,618,618,618,618,618,618,618,618,
+618,618,618,618,618,618,618,618,619,619,619,619,619,619,619,619,
+619,619,619,619,619,619,619,619,619,619,619,619,619,619,619,619,
+619,619,619,619,619,619,619,619,619,619,619,619,619,619,619,619,
+620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,
+620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,
+620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,620,
+
+/* block 146 */
+621,621,621,621,621,621,621,621,621,621,621,621,621,621,621,621,
+621,621,621,621,621,621,621,621,621,621,621,621,621,621,116,116,
+622,622,622,622,622,622,622,622,622,622,116,116,116,116,116,116,
+623,623,623,623,623,623,623,623,623,623,623,623,623,623,623,623,
+623,623,623,623,623,623,623,623,623,623,623,623,623,623,623,623,
+623,623,623,623,116,116,116,116,624,624,624,624,624,624,624,624,
+624,624,624,624,624,624,624,624,624,624,624,624,624,624,624,624,
+624,624,624,624,624,624,624,624,624,624,624,624,116,116,116,116,
+
+/* block 147 */
+625,625,625,625,625,625,625,625,625,625,625,625,625,625,625,625,
+625,625,625,625,625,625,625,625,625,625,625,625,625,625,625,625,
+625,625,625,625,625,625,625,625,116,116,116,116,116,116,116,116,
+626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,
+626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,
+626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,626,
+626,626,626,626,116,116,116,116,116,116,116,116,116,116,116,627,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 148 */
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+
+/* block 149 */
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,628,116,116,116,116,116,116,116,116,116,
+628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,628,
+628,628,628,628,628,628,116,116,116,116,116,116,116,116,116,116,
+628,628,628,628,628,628,628,628,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 150 */
+629,629,629,629,629,629,116,116,629,116,629,629,629,629,629,629,
+629,629,629,629,629,629,629,629,629,629,629,629,629,629,629,629,
+629,629,629,629,629,629,629,629,629,629,629,629,629,629,629,629,
+629,629,629,629,629,629,116,629,629,116,116,116,629,116,116,629,
+630,630,630,630,630,630,630,630,630,630,630,630,630,630,630,630,
+630,630,630,630,630,630,116,631,632,632,632,632,632,632,632,632,
+633,633,633,633,633,633,633,633,633,633,633,633,633,633,633,633,
+633,633,633,633,633,633,633,634,634,635,635,635,635,635,635,635,
+
+/* block 151 */
+636,636,636,636,636,636,636,636,636,636,636,636,636,636,636,636,
+636,636,636,636,636,636,636,636,636,636,636,636,636,636,636,116,
+116,116,116,116,116,116,116,637,637,637,637,637,637,637,637,637,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+638,638,638,638,638,638,638,638,638,638,638,638,638,638,638,638,
+638,638,638,116,638,638,116,116,116,116,116,639,639,639,639,639,
+
+/* block 152 */
+640,640,640,640,640,640,640,640,640,640,640,640,640,640,640,640,
+640,640,640,640,640,640,641,641,641,641,641,641,116,116,116,642,
+643,643,643,643,643,643,643,643,643,643,643,643,643,643,643,643,
+643,643,643,643,643,643,643,643,643,643,116,116,116,116,116,644,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 153 */
+645,645,645,645,645,645,645,645,645,645,645,645,645,645,645,645,
+645,645,645,645,645,645,645,645,645,645,645,645,645,645,645,645,
+646,646,646,646,646,646,646,646,646,646,646,646,646,646,646,646,
+646,646,646,646,646,646,646,646,116,116,116,116,647,647,646,646,
+647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,
+116,116,647,647,647,647,647,647,647,647,647,647,647,647,647,647,
+647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,
+647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,647,
+
+/* block 154 */
+648,649,649,649,116,649,649,116,116,116,116,116,649,649,649,649,
+648,648,648,648,116,648,648,648,116,648,648,648,648,648,648,648,
+648,648,648,648,648,648,648,648,648,648,648,648,648,648,648,648,
+648,648,648,648,648,648,116,116,649,649,649,116,116,116,116,649,
+650,650,650,650,650,650,650,650,650,116,116,116,116,116,116,116,
+651,651,651,651,651,651,651,651,651,116,116,116,116,116,116,116,
+652,652,652,652,652,652,652,652,652,652,652,652,652,652,652,652,
+652,652,652,652,652,652,652,652,652,652,652,652,652,653,653,654,
+
+/* block 155 */
+655,655,655,655,655,655,655,655,655,655,655,655,655,655,655,655,
+655,655,655,655,655,655,655,655,655,655,655,655,655,656,656,656,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+657,657,657,657,657,657,657,657,658,657,657,657,657,657,657,657,
+657,657,657,657,657,657,657,657,657,657,657,657,657,657,657,657,
+657,657,657,657,657,659,659,116,116,116,116,660,660,660,660,660,
+661,661,661,661,661,661,661,116,116,116,116,116,116,116,116,116,
+
+/* block 156 */
+662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,
+662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,
+662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,662,
+662,662,662,662,662,662,116,116,116,663,663,663,663,663,663,663,
+664,664,664,664,664,664,664,664,664,664,664,664,664,664,664,664,
+664,664,664,664,664,664,116,116,665,665,665,665,665,665,665,665,
+666,666,666,666,666,666,666,666,666,666,666,666,666,666,666,666,
+666,666,666,116,116,116,116,116,667,667,667,667,667,667,667,667,
+
+/* block 157 */
+668,668,668,668,668,668,668,668,668,668,668,668,668,668,668,668,
+668,668,116,116,116,116,116,116,116,669,669,669,669,116,116,116,
+116,116,116,116,116,116,116,116,116,670,670,670,670,670,670,670,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 158 */
+671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,
+671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,
+671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,
+671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,671,
+671,671,671,671,671,671,671,671,671,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 159 */
+672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,
+672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,
+672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,672,
+672,672,672,116,116,116,116,116,116,116,116,116,116,116,116,116,
+673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,
+673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,
+673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,673,
+673,673,673,116,116,116,116,116,116,116,674,674,674,674,674,674,
+
+/* block 160 */
+675,675,675,675,675,675,675,675,675,675,675,675,675,675,675,675,
+675,675,675,675,675,675,675,675,675,675,675,675,675,675,675,675,
+675,675,675,675,676,676,676,676,116,116,116,116,116,116,116,116,
+677,677,677,677,677,677,677,677,677,677,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 161 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+678,678,678,678,678,678,678,678,678,678,678,678,678,678,678,678,
+678,678,678,678,678,678,678,678,678,678,678,678,678,678,678,116,
+
+/* block 162 */
+679,679,679,679,679,679,679,679,679,679,679,679,679,679,679,679,
+679,679,679,679,679,679,679,679,679,679,679,679,679,680,680,680,
+680,680,680,680,680,680,680,679,116,116,116,116,116,116,116,116,
+681,681,681,681,681,681,681,681,681,681,681,681,681,681,681,681,
+681,681,681,681,681,681,682,682,682,682,682,682,682,682,682,682,
+682,683,683,683,683,684,684,684,684,684,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 163 */
+685,686,685,687,687,687,687,687,687,687,687,687,687,687,687,687,
+687,687,687,687,687,687,687,687,687,687,687,687,687,687,687,687,
+687,687,687,687,687,687,687,687,687,687,687,687,687,687,687,687,
+687,687,687,687,687,687,687,687,686,686,686,686,686,686,686,686,
+686,686,686,686,686,686,686,688,688,688,688,688,688,688,116,116,
+116,116,689,689,689,689,689,689,689,689,689,689,689,689,689,689,
+689,689,689,689,689,689,690,690,690,690,690,690,690,690,690,690,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,686,
+
+/* block 164 */
+691,691,692,693,693,693,693,693,693,693,693,693,693,693,693,693,
+693,693,693,693,693,693,693,693,693,693,693,693,693,693,693,693,
+693,693,693,693,693,693,693,693,693,693,693,693,693,693,693,693,
+692,692,692,691,691,691,691,692,692,691,691,694,694,695,694,694,
+694,694,116,116,116,116,116,116,116,116,116,116,116,695,116,116,
+696,696,696,696,696,696,696,696,696,696,696,696,696,696,696,696,
+696,696,696,696,696,696,696,696,696,116,116,116,116,116,116,116,
+697,697,697,697,697,697,697,697,697,697,116,116,116,116,116,116,
+
+/* block 165 */
+698,698,698,699,699,699,699,699,699,699,699,699,699,699,699,699,
+699,699,699,699,699,699,699,699,699,699,699,699,699,699,699,699,
+699,699,699,699,699,699,699,698,698,698,698,698,700,698,698,698,
+698,698,698,698,698,116,701,701,701,701,701,701,701,701,701,701,
+702,702,702,702,699,700,700,116,116,116,116,116,116,116,116,116,
+703,703,703,703,703,703,703,703,703,703,703,703,703,703,703,703,
+703,703,703,703,703,703,703,703,703,703,703,703,703,703,703,703,
+703,703,703,704,705,705,703,116,116,116,116,116,116,116,116,116,
+
+/* block 166 */
+706,706,707,708,708,708,708,708,708,708,708,708,708,708,708,708,
+708,708,708,708,708,708,708,708,708,708,708,708,708,708,708,708,
+708,708,708,708,708,708,708,708,708,708,708,708,708,708,708,708,
+708,708,708,707,707,707,706,706,706,706,706,706,706,706,706,707,
+707,708,709,709,708,710,710,710,710,706,706,706,706,710,116,116,
+711,711,711,711,711,711,711,711,711,711,708,710,708,710,710,710,
+116,712,712,712,712,712,712,712,712,712,712,712,712,712,712,712,
+712,712,712,712,712,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 167 */
+713,713,713,713,713,713,713,713,713,713,713,713,713,713,713,713,
+713,713,116,713,713,713,713,713,713,713,713,713,713,713,713,713,
+713,713,713,713,713,713,713,713,713,713,713,713,714,714,714,715,
+715,715,714,714,715,714,715,715,716,716,716,716,716,716,715,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 168 */
+717,717,717,717,717,717,717,116,717,116,717,717,717,717,116,717,
+717,717,717,717,717,717,717,717,717,717,717,717,717,717,116,717,
+717,717,717,717,717,717,717,717,717,718,116,116,116,116,116,116,
+719,719,719,719,719,719,719,719,719,719,719,719,719,719,719,719,
+719,719,719,719,719,719,719,719,719,719,719,719,719,719,719,719,
+719,719,719,719,719,719,719,719,719,719,719,719,719,719,719,720,
+721,721,721,720,720,720,720,720,720,720,720,116,116,116,116,116,
+722,722,722,722,722,722,722,722,722,722,116,116,116,116,116,116,
+
+/* block 169 */
+723,723,724,724,116,725,725,725,725,725,725,725,725,116,116,725,
+725,116,116,725,725,725,725,725,725,725,725,725,725,725,725,725,
+725,725,725,725,725,725,725,725,725,116,725,725,725,725,725,725,
+725,116,725,725,116,725,725,725,725,725,116,111,723,725,726,724,
+723,724,724,724,724,116,116,724,724,116,116,724,724,724,116,116,
+725,116,116,116,116,116,116,726,116,116,116,116,116,725,725,725,
+725,725,724,724,116,116,723,723,723,723,723,723,723,116,116,116,
+723,723,723,723,723,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 170 */
+727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,
+727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,
+727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,727,
+727,727,727,727,727,728,728,728,729,729,729,729,729,729,729,729,
+728,728,729,729,729,728,729,727,727,727,727,730,730,730,730,730,
+731,731,731,731,731,731,731,731,731,731,116,730,116,730,729,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 171 */
+732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,
+732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,
+732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,732,
+733,734,734,735,735,735,735,735,735,734,735,734,734,733,734,735,
+735,734,735,735,732,732,736,732,116,116,116,116,116,116,116,116,
+737,737,737,737,737,737,737,737,737,737,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 172 */
+738,738,738,738,738,738,738,738,738,738,738,738,738,738,738,738,
+738,738,738,738,738,738,738,738,738,738,738,738,738,738,738,738,
+738,738,738,738,738,738,738,738,738,738,738,738,738,738,738,739,
+740,740,741,741,741,741,116,116,740,740,740,740,741,741,740,741,
+741,742,742,742,742,742,742,742,742,742,742,742,742,742,742,742,
+742,742,742,742,742,742,742,742,738,738,738,738,741,741,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 173 */
+743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,
+743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,
+743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,743,
+744,744,744,745,745,745,745,745,745,745,745,744,744,745,744,745,
+745,746,746,746,743,116,116,116,116,116,116,116,116,116,116,116,
+747,747,747,747,747,747,747,747,747,747,116,116,116,116,116,116,
+373,373,373,373,373,373,373,373,373,373,373,373,373,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 174 */
+748,748,748,748,748,748,748,748,748,748,748,748,748,748,748,748,
+748,748,748,748,748,748,748,748,748,748,748,748,748,748,748,748,
+748,748,748,748,748,748,748,748,748,748,748,749,750,749,750,750,
+749,749,749,749,749,749,750,749,116,116,116,116,116,116,116,116,
+751,751,751,751,751,751,751,751,751,751,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 175 */
+752,752,752,752,752,752,752,752,752,752,752,752,752,752,752,752,
+752,752,752,752,752,752,752,752,752,752,752,116,116,753,753,753,
+754,754,753,753,753,753,754,753,753,753,753,753,116,116,116,116,
+755,755,755,755,755,755,755,755,755,755,756,756,757,757,757,758,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 176 */
+759,759,759,759,759,759,759,759,759,759,759,759,759,759,759,759,
+759,759,759,759,759,759,759,759,759,759,759,759,759,759,759,759,
+759,759,759,759,759,759,759,759,759,759,759,759,760,760,760,761,
+761,761,761,761,761,761,761,761,760,761,761,762,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 177 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
+763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
+764,764,764,764,764,764,764,764,764,764,764,764,764,764,764,764,
+764,764,764,764,764,764,764,764,764,764,764,764,764,764,764,764,
+765,765,765,765,765,765,765,765,765,765,766,766,766,766,766,766,
+766,766,766,116,116,116,116,116,116,116,116,116,116,116,116,767,
+
+/* block 178 */
+768,769,769,769,769,769,769,769,769,769,769,768,768,768,768,768,
+768,768,768,768,768,768,768,768,768,768,768,768,768,768,768,768,
+768,768,768,768,768,768,768,768,768,768,768,768,768,768,768,768,
+768,768,768,769,769,769,769,769,769,770,771,769,769,769,769,772,
+772,772,772,772,772,772,772,769,116,116,116,116,116,116,116,116,
+773,774,774,774,774,774,774,775,775,774,774,774,773,773,773,773,
+773,773,773,773,773,773,773,773,773,773,773,773,773,773,773,773,
+773,773,773,773,773,773,773,773,773,773,773,773,773,773,773,773,
+
+/* block 179 */
+773,773,773,773,116,116,776,776,776,776,774,774,774,774,774,774,
+774,774,774,774,774,774,774,775,774,774,777,777,777,773,777,777,
+777,777,777,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,
+778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,
+778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,778,
+778,778,778,778,778,778,778,778,778,116,116,116,116,116,116,116,
+
+/* block 180 */
+779,779,779,779,779,779,779,779,779,116,779,779,779,779,779,779,
+779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,
+779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,780,
+781,781,781,781,781,781,781,116,781,781,781,781,781,781,780,781,
+779,782,782,782,782,782,116,116,116,116,116,116,116,116,116,116,
+783,783,783,783,783,783,783,783,783,783,784,784,784,784,784,784,
+784,784,784,784,784,784,784,784,784,784,784,784,784,116,116,116,
+785,785,786,786,786,786,786,786,786,786,786,786,786,786,786,786,
+
+/* block 181 */
+786,786,786,786,786,786,786,786,786,786,786,786,786,786,786,786,
+116,116,787,787,787,787,787,787,787,787,787,787,787,787,787,787,
+787,787,787,787,787,787,787,787,116,788,787,787,787,787,787,787,
+787,788,787,787,788,787,787,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 182 */
+789,789,789,789,789,789,789,116,789,789,116,789,789,789,789,789,
+789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,
+789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,
+789,790,790,790,790,790,790,116,116,116,790,116,790,790,116,790,
+790,790,790,790,790,790,791,790,116,116,116,116,116,116,116,116,
+792,792,792,792,792,792,792,792,792,792,116,116,116,116,116,116,
+793,793,793,793,793,793,116,793,793,116,793,793,793,793,793,793,
+793,793,793,793,793,793,793,793,793,793,793,793,793,793,793,793,
+
+/* block 183 */
+793,793,793,793,793,793,793,793,793,793,794,794,794,794,794,116,
+795,795,116,794,794,795,794,795,793,116,116,116,116,116,116,116,
+796,796,796,796,796,796,796,796,796,796,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 184 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+797,797,797,797,797,797,797,797,797,797,797,797,797,797,797,797,
+797,797,797,798,798,799,799,800,800,116,116,116,116,116,116,116,
+
+/* block 185 */
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+
+/* block 186 */
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 187 */
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,
+802,802,802,802,802,802,802,802,802,802,802,802,802,802,802,116,
+803,803,803,803,803,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 188 */
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,801,
+801,801,801,801,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 189 */
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+
+/* block 190 */
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,
+804,804,804,804,804,804,804,804,804,804,804,804,804,804,804,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 191 */
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+
+/* block 192 */
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,805,
+805,805,805,805,805,805,805,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 193 */
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+
+/* block 194 */
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,538,
+538,538,538,538,538,538,538,538,538,116,116,116,116,116,116,116,
+806,806,806,806,806,806,806,806,806,806,806,806,806,806,806,806,
+806,806,806,806,806,806,806,806,806,806,806,806,806,806,806,116,
+807,807,807,807,807,807,807,807,807,807,116,116,116,116,808,808,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 195 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+809,809,809,809,809,809,809,809,809,809,809,809,809,809,809,809,
+809,809,809,809,809,809,809,809,809,809,809,809,809,809,116,116,
+810,810,810,810,810,811,116,116,116,116,116,116,116,116,116,116,
+
+/* block 196 */
+812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,
+812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,
+812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,
+813,813,813,813,813,813,813,814,814,814,814,814,815,815,815,815,
+816,816,816,816,814,815,116,116,116,116,116,116,116,116,116,116,
+817,817,817,817,817,817,817,817,817,817,116,818,818,818,818,818,
+818,818,116,812,812,812,812,812,812,812,812,812,812,812,812,812,
+812,812,812,812,812,812,812,812,116,116,116,116,116,812,812,812,
+
+/* block 197 */
+812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,812,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 198 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+819,819,819,819,819,819,819,819,819,819,819,819,819,819,819,819,
+819,819,819,819,819,819,819,819,819,819,819,819,819,819,819,819,
+820,820,820,820,820,820,820,820,820,820,820,820,820,820,820,820,
+820,820,820,820,820,820,820,820,820,820,820,820,820,820,820,820,
+
+/* block 199 */
+821,821,821,821,821,821,821,821,821,821,821,821,821,821,821,821,
+821,821,821,821,821,821,821,822,822,822,822,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 200 */
+823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,
+823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,
+823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,
+823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,823,
+823,823,823,823,823,116,116,116,116,116,116,116,116,116,116,116,
+823,824,824,824,824,824,824,824,824,824,824,824,824,824,824,824,
+824,824,824,824,824,824,824,824,824,824,824,824,824,824,824,824,
+824,824,824,824,824,824,824,824,824,824,824,824,824,824,824,116,
+
+/* block 201 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,825,
+825,825,825,826,826,826,826,826,826,826,826,826,826,826,826,826,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+827,828,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 202 */
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+
+/* block 203 */
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 204 */
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,829,
+829,829,829,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 205 */
+517,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+
+/* block 206 */
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+
+/* block 207 */
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,
+515,515,515,515,515,515,515,515,515,515,515,515,515,515,515,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+
+/* block 208 */
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+
+/* block 209 */
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,830,
+830,830,830,830,830,830,830,830,830,830,830,830,116,116,116,116,
+
+/* block 210 */
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,831,
+831,831,831,831,831,831,831,831,831,831,831,116,116,116,116,116,
+831,831,831,831,831,831,831,831,831,831,831,831,831,116,116,116,
+
+/* block 211 */
+831,831,831,831,831,831,831,831,831,116,116,116,116,116,116,116,
+831,831,831,831,831,831,831,831,831,831,116,116,832,833,833,834,
+ 23, 23, 23, 23,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 212 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19,116,116,116,116,116,116,116,116,116,116,
+
+/* block 213 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19,116,116, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19,835,438,111,111,111, 19, 19, 19,438,835,835,
+835,835,835, 23, 23, 23, 23, 23, 23, 23, 23,111,111,111,111,111,
+
+/* block 214 */
+111,111,111, 19, 19,111,111,111,111,111,111,111, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,111,111,111,111, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 215 */
+604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,
+604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,
+604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,
+604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,604,
+604,604,836,836,836,604,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 216 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 217 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19,116,116,116,116,116,116,116,116,116,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24,116,116,116,116,116,116,116,
+
+/* block 218 */
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,473,473,
+473,473,473,473,473,116,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+
+/* block 219 */
+472,472,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,472,116,472,472,
+116,116,472,116,116,472,472,116,116,472,472,472,472,116,472,472,
+472,472,472,472,472,472,473,473,473,473,116,473,116,473,473,473,
+473,473,473,473,116,473,473,473,473,473,473,473,473,473,473,473,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+
+/* block 220 */
+473,473,473,473,472,472,116,472,472,472,472,116,116,472,472,472,
+472,472,472,472,472,116,472,472,472,472,472,472,472,116,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,472,472,116,472,472,472,472,116,
+472,472,472,472,472,116,472,116,116,116,472,472,472,472,472,472,
+472,116,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+
+/* block 221 */
+472,472,472,472,472,472,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+
+/* block 222 */
+473,473,473,473,473,473,473,473,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+
+/* block 223 */
+472,472,472,472,472,472,472,472,472,472,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,116,116,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472, 8,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473, 8,473,473,473,473,
+473,473,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472, 8,473,473,473,473,
+
+/* block 224 */
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473, 8,473,473,473,473,473,473,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472, 8,473,473,473,473,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473, 8,
+473,473,473,473,473,473,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472, 8,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+
+/* block 225 */
+473,473,473,473,473,473,473,473,473, 8,473,473,473,473,473,473,
+472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,472,
+472,472,472,472,472,472,472,472,472, 8,473,473,473,473,473,473,
+473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,473,
+473,473,473, 8,473,473,473,473,473,473,472,473,116,116, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+
+/* block 226 */
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,837,
+
+/* block 227 */
+838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+838,838,838,838,838,838,838,837,837,837,837,838,838,838,838,838,
+838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+838,838,838,838,838,838,838,838,838,838,838,838,838,837,837,837,
+837,837,837,837,837,838,837,837,837,837,837,837,837,837,837,837,
+
+/* block 228 */
+837,837,837,837,838,837,837,839,839,839,839,839,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,838,838,838,838,838,
+116,838,838,838,838,838,838,838,838,838,838,838,838,838,838,838,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 229 */
+840,840,840,840,840,840,840,116,840,840,840,840,840,840,840,840,
+840,840,840,840,840,840,840,840,840,116,116,840,840,840,840,840,
+840,840,116,840,840,116,840,840,840,840,840,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 230 */
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+
+/* block 231 */
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,841,
+841,841,841,841,841,116,116,842,842,842,842,842,842,842,842,842,
+843,843,843,843,843,843,843,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 232 */
+844,844,844,844,844,844,844,844,844,844,844,844,844,844,844,844,
+844,844,844,844,844,844,844,844,844,844,844,844,844,844,844,844,
+844,844,845,845,845,845,845,845,845,845,845,845,845,845,845,845,
+845,845,845,845,845,845,845,845,845,845,845,845,845,845,845,845,
+845,845,845,845,846,846,846,846,846,846,846,116,116,116,116,116,
+847,847,847,847,847,847,847,847,847,847,116,116,116,116,848,848,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 233 */
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+
+/* block 234 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 19, 24, 24, 24,
+ 5, 24, 24, 24, 24,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 235 */
+217,217,217,217,116,217,217,217,217,217,217,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,217,
+116,217,217,116,217,116,116,217,116,217,217,217,217,217,217,217,
+217,217,217,116,217,217,217,217,116,217,116,217,116,116,116,116,
+116,116,217,116,116,116,116,217,116,217,116,217,116,217,217,217,
+116,217,217,116,217,116,116,217,116,217,116,217,116,217,116,217,
+116,217,217,116,217,116,116,217,217,217,217,116,217,217,217,217,
+217,217,217,116,217,217,217,217,116,217,217,217,217,116,217,116,
+
+/* block 236 */
+217,217,217,217,217,217,217,217,217,217,116,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,116,116,116,116,
+116,217,217,217,116,217,217,217,217,217,116,217,217,217,217,217,
+217,217,217,217,217,217,217,217,217,217,217,217,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+211,211,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 237 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 238 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20,849,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,
+849, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+849, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+849, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20,849,849,849,849,849,849,849,849,849,849,
+
+/* block 239 */
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,
+ 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20,
+
+/* block 240 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 19,
+ 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,850,850,850,850,850,850,850,850,850,850,
+850,850,850,850,850,850,850,850,850,850,850,850,850,850,850,850,
+
+/* block 241 */
+851, 20, 20,849,849,849,849,849,849,849,849,849,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20,
+ 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,849,849,849,
+ 20, 20,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 242 */
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 243 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 244 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,852,852,852,852,852,
+
+/* block 245 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 19,
+ 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 246 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 247 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,849,849,849,849,849,
+
+/* block 248 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 249 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 20, 20, 20, 20,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 250 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+
+/* block 251 */
+ 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,849,849,849,849,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 252 */
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20,849,
+ 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20,849,849, 20, 20, 20, 20,849,849,849, 20,849, 20, 20, 20, 20,
+
+/* block 253 */
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20,849,849,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,849,849,849,849,849,
+ 20, 20, 20,849,849,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+
+/* block 254 */
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+
+/* block 255 */
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,849,
+849,849,849,849,849,849,849,849,849,849,849,849,849,849,116,116,
+
+/* block 256 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 257 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,116,116,116,116,116,116,116,116,116,116,116,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+
+/* block 258 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,116,116,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+
+/* block 259 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+
+/* block 260 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 261 */
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,523,
+523,523,523,523,523,523,523,523,523,523,523,523,523,523,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
+
+/* block 262 */
+471, 23,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,853,
+
+/* block 263 */
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+
+/* block 264 */
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+
+/* block 265 */
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
+471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,471,
+
+/* block 266 */
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,597,
+597,597,597,597,597,597,597,597,597,597,597,597,597,597,116,116,
+
+};
+
+#if UCD_BLOCK_SIZE != 128
+#error Please correct UCD_BLOCK_SIZE in pcre2_internal.h
+#endif
+#endif /* SUPPORT_UNICODE */
+
+#endif /* PCRE2_PCRE2TEST */
diff --git a/test/monniaux/pcre2-10.32/pcre2_ucp.h b/test/monniaux/pcre2-10.32/pcre2_ucp.h
new file mode 100644
index 00000000..0c330edc
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_ucp.h
@@ -0,0 +1,288 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+#ifndef PCRE2_UCP_H_IDEMPOTENT_GUARD
+#define PCRE2_UCP_H_IDEMPOTENT_GUARD
+
+/* This file contains definitions of the property values that are returned by
+the UCD access macros. New values that are added for new releases of Unicode
+should always be at the end of each enum, for backwards compatibility.
+
+IMPORTANT: Note also that the specific numeric values of the enums have to be
+the same as the values that are generated by the maint/MultiStage2.py script,
+where the equivalent property descriptive names are listed in vectors.
+
+ALSO: The specific values of the first two enums are assumed for the table
+called catposstab in pcre2_compile.c. */
+
+/* These are the general character categories. */
+
+enum {
+ ucp_C, /* Other */
+ ucp_L, /* Letter */
+ ucp_M, /* Mark */
+ ucp_N, /* Number */
+ ucp_P, /* Punctuation */
+ ucp_S, /* Symbol */
+ ucp_Z /* Separator */
+};
+
+/* These are the particular character categories. */
+
+enum {
+ ucp_Cc, /* Control */
+ ucp_Cf, /* Format */
+ ucp_Cn, /* Unassigned */
+ ucp_Co, /* Private use */
+ ucp_Cs, /* Surrogate */
+ ucp_Ll, /* Lower case letter */
+ ucp_Lm, /* Modifier letter */
+ ucp_Lo, /* Other letter */
+ ucp_Lt, /* Title case letter */
+ ucp_Lu, /* Upper case letter */
+ ucp_Mc, /* Spacing mark */
+ ucp_Me, /* Enclosing mark */
+ ucp_Mn, /* Non-spacing mark */
+ ucp_Nd, /* Decimal number */
+ ucp_Nl, /* Letter number */
+ ucp_No, /* Other number */
+ ucp_Pc, /* Connector punctuation */
+ ucp_Pd, /* Dash punctuation */
+ ucp_Pe, /* Close punctuation */
+ ucp_Pf, /* Final punctuation */
+ ucp_Pi, /* Initial punctuation */
+ ucp_Po, /* Other punctuation */
+ ucp_Ps, /* Open punctuation */
+ ucp_Sc, /* Currency symbol */
+ ucp_Sk, /* Modifier symbol */
+ ucp_Sm, /* Mathematical symbol */
+ ucp_So, /* Other symbol */
+ ucp_Zl, /* Line separator */
+ ucp_Zp, /* Paragraph separator */
+ ucp_Zs /* Space separator */
+};
+
+/* These are grapheme break properties. The Extended Pictographic property
+comes from the emoji-data.txt file. */
+
+enum {
+ ucp_gbCR, /* 0 */
+ ucp_gbLF, /* 1 */
+ ucp_gbControl, /* 2 */
+ ucp_gbExtend, /* 3 */
+ ucp_gbPrepend, /* 4 */
+ ucp_gbSpacingMark, /* 5 */
+ ucp_gbL, /* 6 Hangul syllable type L */
+ ucp_gbV, /* 7 Hangul syllable type V */
+ ucp_gbT, /* 8 Hangul syllable type T */
+ ucp_gbLV, /* 9 Hangul syllable type LV */
+ ucp_gbLVT, /* 10 Hangul syllable type LVT */
+ ucp_gbRegionalIndicator, /* 11 */
+ ucp_gbOther, /* 12 */
+ ucp_gbZWJ, /* 13 */
+ ucp_gbExtended_Pictographic /* 14 */
+};
+
+/* These are the script identifications. */
+
+enum {
+ ucp_Arabic,
+ ucp_Armenian,
+ ucp_Bengali,
+ ucp_Bopomofo,
+ ucp_Braille,
+ ucp_Buginese,
+ ucp_Buhid,
+ ucp_Canadian_Aboriginal,
+ ucp_Cherokee,
+ ucp_Common,
+ ucp_Coptic,
+ ucp_Cypriot,
+ ucp_Cyrillic,
+ ucp_Deseret,
+ ucp_Devanagari,
+ ucp_Ethiopic,
+ ucp_Georgian,
+ ucp_Glagolitic,
+ ucp_Gothic,
+ ucp_Greek,
+ ucp_Gujarati,
+ ucp_Gurmukhi,
+ ucp_Han,
+ ucp_Hangul,
+ ucp_Hanunoo,
+ ucp_Hebrew,
+ ucp_Hiragana,
+ ucp_Inherited,
+ ucp_Kannada,
+ ucp_Katakana,
+ ucp_Kharoshthi,
+ ucp_Khmer,
+ ucp_Lao,
+ ucp_Latin,
+ ucp_Limbu,
+ ucp_Linear_B,
+ ucp_Malayalam,
+ ucp_Mongolian,
+ ucp_Myanmar,
+ ucp_New_Tai_Lue,
+ ucp_Ogham,
+ ucp_Old_Italic,
+ ucp_Old_Persian,
+ ucp_Oriya,
+ ucp_Osmanya,
+ ucp_Runic,
+ ucp_Shavian,
+ ucp_Sinhala,
+ ucp_Syloti_Nagri,
+ ucp_Syriac,
+ ucp_Tagalog,
+ ucp_Tagbanwa,
+ ucp_Tai_Le,
+ ucp_Tamil,
+ ucp_Telugu,
+ ucp_Thaana,
+ ucp_Thai,
+ ucp_Tibetan,
+ ucp_Tifinagh,
+ ucp_Ugaritic,
+ ucp_Yi,
+ /* New for Unicode 5.0 */
+ ucp_Balinese,
+ ucp_Cuneiform,
+ ucp_Nko,
+ ucp_Phags_Pa,
+ ucp_Phoenician,
+ /* New for Unicode 5.1 */
+ ucp_Carian,
+ ucp_Cham,
+ ucp_Kayah_Li,
+ ucp_Lepcha,
+ ucp_Lycian,
+ ucp_Lydian,
+ ucp_Ol_Chiki,
+ ucp_Rejang,
+ ucp_Saurashtra,
+ ucp_Sundanese,
+ ucp_Vai,
+ /* New for Unicode 5.2 */
+ ucp_Avestan,
+ ucp_Bamum,
+ ucp_Egyptian_Hieroglyphs,
+ ucp_Imperial_Aramaic,
+ ucp_Inscriptional_Pahlavi,
+ ucp_Inscriptional_Parthian,
+ ucp_Javanese,
+ ucp_Kaithi,
+ ucp_Lisu,
+ ucp_Meetei_Mayek,
+ ucp_Old_South_Arabian,
+ ucp_Old_Turkic,
+ ucp_Samaritan,
+ ucp_Tai_Tham,
+ ucp_Tai_Viet,
+ /* New for Unicode 6.0.0 */
+ ucp_Batak,
+ ucp_Brahmi,
+ ucp_Mandaic,
+ /* New for Unicode 6.1.0 */
+ ucp_Chakma,
+ ucp_Meroitic_Cursive,
+ ucp_Meroitic_Hieroglyphs,
+ ucp_Miao,
+ ucp_Sharada,
+ ucp_Sora_Sompeng,
+ ucp_Takri,
+ /* New for Unicode 7.0.0 */
+ ucp_Bassa_Vah,
+ ucp_Caucasian_Albanian,
+ ucp_Duployan,
+ ucp_Elbasan,
+ ucp_Grantha,
+ ucp_Khojki,
+ ucp_Khudawadi,
+ ucp_Linear_A,
+ ucp_Mahajani,
+ ucp_Manichaean,
+ ucp_Mende_Kikakui,
+ ucp_Modi,
+ ucp_Mro,
+ ucp_Nabataean,
+ ucp_Old_North_Arabian,
+ ucp_Old_Permic,
+ ucp_Pahawh_Hmong,
+ ucp_Palmyrene,
+ ucp_Psalter_Pahlavi,
+ ucp_Pau_Cin_Hau,
+ ucp_Siddham,
+ ucp_Tirhuta,
+ ucp_Warang_Citi,
+ /* New for Unicode 8.0.0 */
+ ucp_Ahom,
+ ucp_Anatolian_Hieroglyphs,
+ ucp_Hatran,
+ ucp_Multani,
+ ucp_Old_Hungarian,
+ ucp_SignWriting,
+ /* New for Unicode 10.0.0 (no update since 8.0.0) */
+ ucp_Adlam,
+ ucp_Bhaiksuki,
+ ucp_Marchen,
+ ucp_Newa,
+ ucp_Osage,
+ ucp_Tangut,
+ ucp_Masaram_Gondi,
+ ucp_Nushu,
+ ucp_Soyombo,
+ ucp_Zanabazar_Square,
+ /* New for Unicode 11.0.0 */
+ ucp_Dogra,
+ ucp_Gunjala_Gondi,
+ ucp_Hanifi_Rohingya,
+ ucp_Makasar,
+ ucp_Medefaidrin,
+ ucp_Old_Sogdian,
+ ucp_Sogdian
+};
+
+#endif /* PCRE2_UCP_H_IDEMPOTENT_GUARD */
+
+/* End of pcre2_ucp.h */
diff --git a/test/monniaux/pcre2-10.32/pcre2_valid_utf.c b/test/monniaux/pcre2-10.32/pcre2_valid_utf.c
new file mode 100644
index 00000000..96e8bff9
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_valid_utf.c
@@ -0,0 +1,398 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2017 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module contains an internal function for validating UTF character
+strings. This file is also #included by the pcre2test program, which uses
+macros to change names from _pcre2_xxx to xxxx, thereby avoiding name clashes
+with the library. In this case, PCRE2_PCRE2TEST is defined. */
+
+#ifndef PCRE2_PCRE2TEST /* We're compiling the library */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "pcre2_internal.h"
+#endif /* PCRE2_PCRE2TEST */
+
+
+#ifndef SUPPORT_UNICODE
+/*************************************************
+* Dummy function when Unicode is not supported *
+*************************************************/
+
+/* This function should never be called when Unicode is not supported. */
+
+int
+PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)
+{
+(void)string;
+(void)length;
+(void)erroroffset;
+return 0;
+}
+#else /* UTF is supported */
+
+
+
+/*************************************************
+* Validate a UTF string *
+*************************************************/
+
+/* This function is called (optionally) at the start of compile or match, to
+check that a supposed UTF string is actually valid. The early check means
+that subsequent code can assume it is dealing with a valid string. The check
+can be turned off for maximum performance, but the consequences of supplying an
+invalid string are then undefined.
+
+Arguments:
+ string points to the string
+ length length of string
+ errp pointer to an error position offset variable
+
+Returns: == 0 if the string is a valid UTF string
+ != 0 otherwise, setting the offset of the bad character
+*/
+
+int
+PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)
+{
+PCRE2_SPTR p;
+uint32_t c;
+
+/* ----------------- Check a UTF-8 string ----------------- */
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+
+/* Originally, this function checked according to RFC 2279, allowing for values
+in the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were
+in the canonical format. Once somebody had pointed out RFC 3629 to me (it
+obsoletes 2279), additional restrictions were applied. The values are now
+limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the
+subrange 0xd000 to 0xdfff is excluded. However, the format of 5-byte and 6-byte
+characters is still checked. Error returns are as follows:
+
+PCRE2_ERROR_UTF8_ERR1 Missing 1 byte at the end of the string
+PCRE2_ERROR_UTF8_ERR2 Missing 2 bytes at the end of the string
+PCRE2_ERROR_UTF8_ERR3 Missing 3 bytes at the end of the string
+PCRE2_ERROR_UTF8_ERR4 Missing 4 bytes at the end of the string
+PCRE2_ERROR_UTF8_ERR5 Missing 5 bytes at the end of the string
+PCRE2_ERROR_UTF8_ERR6 2nd-byte's two top bits are not 0x80
+PCRE2_ERROR_UTF8_ERR7 3rd-byte's two top bits are not 0x80
+PCRE2_ERROR_UTF8_ERR8 4th-byte's two top bits are not 0x80
+PCRE2_ERROR_UTF8_ERR9 5th-byte's two top bits are not 0x80
+PCRE2_ERROR_UTF8_ERR10 6th-byte's two top bits are not 0x80
+PCRE2_ERROR_UTF8_ERR11 5-byte character is not permitted by RFC 3629
+PCRE2_ERROR_UTF8_ERR12 6-byte character is not permitted by RFC 3629
+PCRE2_ERROR_UTF8_ERR13 4-byte character with value > 0x10ffff is not permitted
+PCRE2_ERROR_UTF8_ERR14 3-byte character with value 0xd800-0xdfff is not permitted
+PCRE2_ERROR_UTF8_ERR15 Overlong 2-byte sequence
+PCRE2_ERROR_UTF8_ERR16 Overlong 3-byte sequence
+PCRE2_ERROR_UTF8_ERR17 Overlong 4-byte sequence
+PCRE2_ERROR_UTF8_ERR18 Overlong 5-byte sequence (won't ever occur)
+PCRE2_ERROR_UTF8_ERR19 Overlong 6-byte sequence (won't ever occur)
+PCRE2_ERROR_UTF8_ERR20 Isolated 0x80 byte (not within UTF-8 character)
+PCRE2_ERROR_UTF8_ERR21 Byte with the illegal value 0xfe or 0xff
+*/
+
+for (p = string; length > 0; p++)
+ {
+ uint32_t ab, d;
+
+ c = *p;
+ length--;
+
+ if (c < 128) continue; /* ASCII character */
+
+ if (c < 0xc0) /* Isolated 10xx xxxx byte */
+ {
+ *erroroffset = (PCRE2_SIZE)(p - string);
+ return PCRE2_ERROR_UTF8_ERR20;
+ }
+
+ if (c >= 0xfe) /* Invalid 0xfe or 0xff bytes */
+ {
+ *erroroffset = (PCRE2_SIZE)(p - string);
+ return PCRE2_ERROR_UTF8_ERR21;
+ }
+
+ ab = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes (1-5) */
+ if (length < ab) /* Missing bytes */
+ {
+ *erroroffset = (PCRE2_SIZE)(p - string);
+ switch(ab - length)
+ {
+ case 1: return PCRE2_ERROR_UTF8_ERR1;
+ case 2: return PCRE2_ERROR_UTF8_ERR2;
+ case 3: return PCRE2_ERROR_UTF8_ERR3;
+ case 4: return PCRE2_ERROR_UTF8_ERR4;
+ case 5: return PCRE2_ERROR_UTF8_ERR5;
+ }
+ }
+ length -= ab; /* Length remaining */
+
+ /* Check top bits in the second byte */
+
+ if (((d = *(++p)) & 0xc0) != 0x80)
+ {
+ *erroroffset = (int)(p - string) - 1;
+ return PCRE2_ERROR_UTF8_ERR6;
+ }
+
+ /* For each length, check that the remaining bytes start with the 0x80 bit
+ set and not the 0x40 bit. Then check for an overlong sequence, and for the
+ excluded range 0xd800 to 0xdfff. */
+
+ switch (ab)
+ {
+ /* 2-byte character. No further bytes to check for 0x80. Check first byte
+ for for xx00 000x (overlong sequence). */
+
+ case 1: if ((c & 0x3e) == 0)
+ {
+ *erroroffset = (int)(p - string) - 1;
+ return PCRE2_ERROR_UTF8_ERR15;
+ }
+ break;
+
+ /* 3-byte character. Check third byte for 0x80. Then check first 2 bytes
+ for 1110 0000, xx0x xxxx (overlong sequence) or
+ 1110 1101, 1010 xxxx (0xd800 - 0xdfff) */
+
+ case 2:
+ if ((*(++p) & 0xc0) != 0x80) /* Third byte */
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR7;
+ }
+ if (c == 0xe0 && (d & 0x20) == 0)
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR16;
+ }
+ if (c == 0xed && d >= 0xa0)
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR14;
+ }
+ break;
+
+ /* 4-byte character. Check 3rd and 4th bytes for 0x80. Then check first 2
+ bytes for for 1111 0000, xx00 xxxx (overlong sequence), then check for a
+ character greater than 0x0010ffff (f4 8f bf bf) */
+
+ case 3:
+ if ((*(++p) & 0xc0) != 0x80) /* Third byte */
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR7;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
+ {
+ *erroroffset = (int)(p - string) - 3;
+ return PCRE2_ERROR_UTF8_ERR8;
+ }
+ if (c == 0xf0 && (d & 0x30) == 0)
+ {
+ *erroroffset = (int)(p - string) - 3;
+ return PCRE2_ERROR_UTF8_ERR17;
+ }
+ if (c > 0xf4 || (c == 0xf4 && d > 0x8f))
+ {
+ *erroroffset = (int)(p - string) - 3;
+ return PCRE2_ERROR_UTF8_ERR13;
+ }
+ break;
+
+ /* 5-byte and 6-byte characters are not allowed by RFC 3629, and will be
+ rejected by the length test below. However, we do the appropriate tests
+ here so that overlong sequences get diagnosed, and also in case there is
+ ever an option for handling these larger code points. */
+
+ /* 5-byte character. Check 3rd, 4th, and 5th bytes for 0x80. Then check for
+ 1111 1000, xx00 0xxx */
+
+ case 4:
+ if ((*(++p) & 0xc0) != 0x80) /* Third byte */
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR7;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
+ {
+ *erroroffset = (int)(p - string) - 3;
+ return PCRE2_ERROR_UTF8_ERR8;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */
+ {
+ *erroroffset = (int)(p - string) - 4;
+ return PCRE2_ERROR_UTF8_ERR9;
+ }
+ if (c == 0xf8 && (d & 0x38) == 0)
+ {
+ *erroroffset = (int)(p - string) - 4;
+ return PCRE2_ERROR_UTF8_ERR18;
+ }
+ break;
+
+ /* 6-byte character. Check 3rd-6th bytes for 0x80. Then check for
+ 1111 1100, xx00 00xx. */
+
+ case 5:
+ if ((*(++p) & 0xc0) != 0x80) /* Third byte */
+ {
+ *erroroffset = (int)(p - string) - 2;
+ return PCRE2_ERROR_UTF8_ERR7;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
+ {
+ *erroroffset = (int)(p - string) - 3;
+ return PCRE2_ERROR_UTF8_ERR8;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */
+ {
+ *erroroffset = (int)(p - string) - 4;
+ return PCRE2_ERROR_UTF8_ERR9;
+ }
+ if ((*(++p) & 0xc0) != 0x80) /* Sixth byte */
+ {
+ *erroroffset = (int)(p - string) - 5;
+ return PCRE2_ERROR_UTF8_ERR10;
+ }
+ if (c == 0xfc && (d & 0x3c) == 0)
+ {
+ *erroroffset = (int)(p - string) - 5;
+ return PCRE2_ERROR_UTF8_ERR19;
+ }
+ break;
+ }
+
+ /* Character is valid under RFC 2279, but 4-byte and 5-byte characters are
+ excluded by RFC 3629. The pointer p is currently at the last byte of the
+ character. */
+
+ if (ab > 3)
+ {
+ *erroroffset = (int)(p - string) - ab;
+ return (ab == 4)? PCRE2_ERROR_UTF8_ERR11 : PCRE2_ERROR_UTF8_ERR12;
+ }
+ }
+return 0;
+
+
+/* ----------------- Check a UTF-16 string ----------------- */
+
+#elif PCRE2_CODE_UNIT_WIDTH == 16
+
+/* There's not so much work, nor so many errors, for UTF-16.
+PCRE2_ERROR_UTF16_ERR1 Missing low surrogate at the end of the string
+PCRE2_ERROR_UTF16_ERR2 Invalid low surrogate
+PCRE2_ERROR_UTF16_ERR3 Isolated low surrogate
+*/
+
+for (p = string; length > 0; p++)
+ {
+ c = *p;
+ length--;
+
+ if ((c & 0xf800) != 0xd800)
+ {
+ /* Normal UTF-16 code point. Neither high nor low surrogate. */
+ }
+ else if ((c & 0x0400) == 0)
+ {
+ /* High surrogate. Must be a followed by a low surrogate. */
+ if (length == 0)
+ {
+ *erroroffset = p - string;
+ return PCRE2_ERROR_UTF16_ERR1;
+ }
+ p++;
+ length--;
+ if ((*p & 0xfc00) != 0xdc00)
+ {
+ *erroroffset = p - string;
+ return PCRE2_ERROR_UTF16_ERR2;
+ }
+ }
+ else
+ {
+ /* Isolated low surrogate. Always an error. */
+ *erroroffset = p - string;
+ return PCRE2_ERROR_UTF16_ERR3;
+ }
+ }
+return 0;
+
+
+
+/* ----------------- Check a UTF-32 string ----------------- */
+
+#else
+
+/* There is very little to do for a UTF-32 string.
+PCRE2_ERROR_UTF32_ERR1 Surrogate character
+PCRE2_ERROR_UTF32_ERR2 Character > 0x10ffff
+*/
+
+for (p = string; length > 0; length--, p++)
+ {
+ c = *p;
+ if ((c & 0xfffff800u) != 0xd800u)
+ {
+ /* Normal UTF-32 code point. Neither high nor low surrogate. */
+ if (c > 0x10ffffu)
+ {
+ *erroroffset = p - string;
+ return PCRE2_ERROR_UTF32_ERR2;
+ }
+ }
+ else
+ {
+ /* A surrogate */
+ *erroroffset = p - string;
+ return PCRE2_ERROR_UTF32_ERR1;
+ }
+ }
+return 0;
+#endif /* CODE_UNIT_WIDTH */
+}
+#endif /* SUPPORT_UNICODE */
+
+/* End of pcre2_valid_utf.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2_xclass.c b/test/monniaux/pcre2-10.32/pcre2_xclass.c
new file mode 100644
index 00000000..407d3f5b
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2_xclass.c
@@ -0,0 +1,271 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+/* This module contains an internal function that is used to match an extended
+class. It is used by pcre2_auto_possessify() and by both pcre2_match() and
+pcre2_def_match(). */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+#include "pcre2_internal.h"
+
+/*************************************************
+* Match character against an XCLASS *
+*************************************************/
+
+/* This function is called to match a character against an extended class that
+might contain codepoints above 255 and/or Unicode properties.
+
+Arguments:
+ c the character
+ data points to the flag code unit of the XCLASS data
+ utf TRUE if in UTF mode
+
+Returns: TRUE if character matches, else FALSE
+*/
+
+BOOL
+PRIV(xclass)(uint32_t c, PCRE2_SPTR data, BOOL utf)
+{
+PCRE2_UCHAR t;
+BOOL negated = (*data & XCL_NOT) != 0;
+
+#if PCRE2_CODE_UNIT_WIDTH == 8
+/* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
+utf = TRUE;
+#endif
+
+/* Code points < 256 are matched against a bitmap, if one is present. If not,
+we still carry on, because there may be ranges that start below 256 in the
+additional data. */
+
+if (c < 256)
+ {
+ if ((*data & XCL_HASPROP) == 0)
+ {
+ if ((*data & XCL_MAP) == 0) return negated;
+ return (((uint8_t *)(data + 1))[c/8] & (1 << (c&7))) != 0;
+ }
+ if ((*data & XCL_MAP) != 0 &&
+ (((uint8_t *)(data + 1))[c/8] & (1 << (c&7))) != 0)
+ return !negated; /* char found */
+ }
+
+/* First skip the bit map if present. Then match against the list of Unicode
+properties or large chars or ranges that end with a large char. We won't ever
+encounter XCL_PROP or XCL_NOTPROP when UTF support is not compiled. */
+
+if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(PCRE2_UCHAR);
+
+while ((t = *data++) != XCL_END)
+ {
+ uint32_t x, y;
+ if (t == XCL_SINGLE)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ GETCHARINC(x, data); /* macro generates multiple statements */
+ }
+ else
+#endif
+ x = *data++;
+ if (c == x) return !negated;
+ }
+ else if (t == XCL_RANGE)
+ {
+#ifdef SUPPORT_UNICODE
+ if (utf)
+ {
+ GETCHARINC(x, data); /* macro generates multiple statements */
+ GETCHARINC(y, data); /* macro generates multiple statements */
+ }
+ else
+#endif
+ {
+ x = *data++;
+ y = *data++;
+ }
+ if (c >= x && c <= y) return !negated;
+ }
+
+#ifdef SUPPORT_UNICODE
+ else /* XCL_PROP & XCL_NOTPROP */
+ {
+ const ucd_record *prop = GET_UCD(c);
+ BOOL isprop = t == XCL_PROP;
+
+ switch(*data)
+ {
+ case PT_ANY:
+ if (isprop) return !negated;
+ break;
+
+ case PT_LAMP:
+ if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
+ prop->chartype == ucp_Lt) == isprop) return !negated;
+ break;
+
+ case PT_GC:
+ if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)
+ return !negated;
+ break;
+
+ case PT_PC:
+ if ((data[1] == prop->chartype) == isprop) return !negated;
+ break;
+
+ case PT_SC:
+ if ((data[1] == prop->script) == isprop) return !negated;
+ break;
+
+ case PT_ALNUM:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop)
+ return !negated;
+ break;
+
+ /* Perl space used to exclude VT, but from Perl 5.18 it is included,
+ which means that Perl space and POSIX space are now identical. PCRE
+ was changed at release 8.34. */
+
+ case PT_SPACE: /* Perl space */
+ case PT_PXSPACE: /* POSIX space */
+ switch(c)
+ {
+ HSPACE_CASES:
+ VSPACE_CASES:
+ if (isprop) return !negated;
+ break;
+
+ default:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)
+ return !negated;
+ break;
+ }
+ break;
+
+ case PT_WORD:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
+ PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
+ == isprop)
+ return !negated;
+ break;
+
+ case PT_UCNC:
+ if (c < 0xa0)
+ {
+ if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
+ c == CHAR_GRAVE_ACCENT) == isprop)
+ return !negated;
+ }
+ else
+ {
+ if ((c < 0xd800 || c > 0xdfff) == isprop)
+ return !negated;
+ }
+ break;
+
+ /* The following three properties can occur only in an XCLASS, as there
+ is no \p or \P coding for them. */
+
+ /* Graphic character. Implement this as not Z (space or separator) and
+ not C (other), except for Cf (format) with a few exceptions. This seems
+ to be what Perl does. The exceptional characters are:
+
+ U+061C Arabic Letter Mark
+ U+180E Mongolian Vowel Separator
+ U+2066 - U+2069 Various "isolate"s
+ */
+
+ case PT_PXGRAPH:
+ if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z &&
+ (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
+ (prop->chartype == ucp_Cf &&
+ c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))
+ )) == isprop)
+ return !negated;
+ break;
+
+ /* Printable character: same as graphic, with the addition of Zs, i.e.
+ not Zl and not Zp, and U+180E. */
+
+ case PT_PXPRINT:
+ if ((prop->chartype != ucp_Zl &&
+ prop->chartype != ucp_Zp &&
+ (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
+ (prop->chartype == ucp_Cf &&
+ c != 0x061c && (c < 0x2066 || c > 0x2069))
+ )) == isprop)
+ return !negated;
+ break;
+
+ /* Punctuation: all Unicode punctuation, plus ASCII characters that
+ Unicode treats as symbols rather than punctuation, for Perl
+ compatibility (these are $+<=>^`|~). */
+
+ case PT_PXPUNCT:
+ if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P ||
+ (c < 128 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop)
+ return !negated;
+ break;
+
+ /* This should never occur, but compilers may mutter if there is no
+ default. */
+
+ default:
+ return FALSE;
+ }
+
+ data += 2;
+ }
+#else
+ (void)utf; /* Avoid compiler warning */
+#endif /* SUPPORT_UNICODE */
+ }
+
+return negated; /* char did not match */
+}
+
+/* End of pcre2_xclass.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2posix.c b/test/monniaux/pcre2-10.32/pcre2posix.c
new file mode 100644
index 00000000..7b9f4774
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2posix.c
@@ -0,0 +1,375 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This module is a wrapper that provides a POSIX API to the underlying PCRE2
+functions. */
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+/* Ensure that the PCRE2POSIX_EXP_xxx macros are set appropriately for
+compiling these functions. This must come before including pcre2posix.h, where
+they are set for an application (using these functions) if they have not
+previously been set. */
+
+#if defined(_WIN32) && !defined(PCRE2_STATIC)
+# define PCRE2POSIX_EXP_DECL extern __declspec(dllexport)
+# define PCRE2POSIX_EXP_DEFN __declspec(dllexport)
+#endif
+
+/* Older versions of MSVC lack snprintf(). This define allows for
+warning/error-free compilation and testing with MSVC compilers back to at least
+MSVC 10/2010. Except for VC6 (which is missing some fundamentals and fails). */
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+#define snprintf _snprintf
+#endif
+
+
+/* Compile-time error numbers start at this value. It should probably never be
+changed. This #define is a copy of the one in pcre2_internal.h. */
+
+#define COMPILE_ERROR_BASE 100
+
+
+/* Standard C headers */
+
+#include <ctype.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* PCRE2 headers */
+
+#include "pcre2.h"
+#include "pcre2posix.h"
+
+/* When compiling with the MSVC compiler, it is sometimes necessary to include
+a "calling convention" before exported function names. (This is secondhand
+information; I know nothing about MSVC myself). For example, something like
+
+ void __cdecl function(....)
+
+might be needed. In order to make this easy, all the exported functions have
+PCRE2_CALL_CONVENTION just before their names. It is rarely needed; if not
+set, we ensure here that it has no effect. */
+
+#ifndef PCRE2_CALL_CONVENTION
+#define PCRE2_CALL_CONVENTION
+#endif
+
+/* Table to translate PCRE2 compile time error codes into POSIX error codes.
+Only a few PCRE2 errors with a value greater than 23 turn into special POSIX
+codes: most go to REG_BADPAT. The second table lists, in pairs, those that
+don't. */
+
+static const int eint1[] = {
+ 0, /* No error */
+ REG_EESCAPE, /* \ at end of pattern */
+ REG_EESCAPE, /* \c at end of pattern */
+ REG_EESCAPE, /* unrecognized character follows \ */
+ REG_BADBR, /* numbers out of order in {} quantifier */
+ /* 5 */
+ REG_BADBR, /* number too big in {} quantifier */
+ REG_EBRACK, /* missing terminating ] for character class */
+ REG_ECTYPE, /* invalid escape sequence in character class */
+ REG_ERANGE, /* range out of order in character class */
+ REG_BADRPT, /* nothing to repeat */
+ /* 10 */
+ REG_ASSERT, /* internal error: unexpected repeat */
+ REG_BADPAT, /* unrecognized character after (? or (?- */
+ REG_BADPAT, /* POSIX named classes are supported only within a class */
+ REG_BADPAT, /* POSIX collating elements are not supported */
+ REG_EPAREN, /* missing ) */
+ /* 15 */
+ REG_ESUBREG, /* reference to non-existent subpattern */
+ REG_INVARG, /* pattern passed as NULL */
+ REG_INVARG, /* unknown compile-time option bit(s) */
+ REG_EPAREN, /* missing ) after (?# comment */
+ REG_ESIZE, /* parentheses nested too deeply */
+ /* 20 */
+ REG_ESIZE, /* regular expression too large */
+ REG_ESPACE, /* failed to get memory */
+ REG_EPAREN, /* unmatched closing parenthesis */
+ REG_ASSERT /* internal error: code overflow */
+ };
+
+static const int eint2[] = {
+ 30, REG_ECTYPE, /* unknown POSIX class name */
+ 32, REG_INVARG, /* this version of PCRE2 does not have Unicode support */
+ 37, REG_EESCAPE, /* PCRE2 does not support \L, \l, \N{name}, \U, or \u */
+ 56, REG_INVARG, /* internal error: unknown newline setting */
+ 92, REG_INVARG, /* invalid option bits with PCRE2_LITERAL */
+};
+
+/* Table of texts corresponding to POSIX error codes */
+
+static const char *const pstring[] = {
+ "", /* Dummy for value 0 */
+ "internal error", /* REG_ASSERT */
+ "invalid repeat counts in {}", /* BADBR */
+ "pattern error", /* BADPAT */
+ "? * + invalid", /* BADRPT */
+ "unbalanced {}", /* EBRACE */
+ "unbalanced []", /* EBRACK */
+ "collation error - not relevant", /* ECOLLATE */
+ "bad class", /* ECTYPE */
+ "bad escape sequence", /* EESCAPE */
+ "empty expression", /* EMPTY */
+ "unbalanced ()", /* EPAREN */
+ "bad range inside []", /* ERANGE */
+ "expression too big", /* ESIZE */
+ "failed to get memory", /* ESPACE */
+ "bad back reference", /* ESUBREG */
+ "bad argument", /* INVARG */
+ "match failed" /* NOMATCH */
+};
+
+
+
+
+/*************************************************
+* Translate error code to string *
+*************************************************/
+
+PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION
+regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
+{
+int used;
+const char *message;
+
+message = (errcode <= 0 || errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
+ "unknown error code" : pstring[errcode];
+
+if (preg != NULL && (int)preg->re_erroffset != -1)
+ {
+ used = snprintf(errbuf, errbuf_size, "%s at offset %-6d", message,
+ (int)preg->re_erroffset);
+ }
+else
+ {
+ used = snprintf(errbuf, errbuf_size, "%s", message);
+ }
+
+return used + 1;
+}
+
+
+
+
+/*************************************************
+* Free store held by a regex *
+*************************************************/
+
+PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION
+regfree(regex_t *preg)
+{
+pcre2_match_data_free(preg->re_match_data);
+pcre2_code_free(preg->re_pcre2_code);
+}
+
+
+
+
+/*************************************************
+* Compile a regular expression *
+*************************************************/
+
+/*
+Arguments:
+ preg points to a structure for recording the compiled expression
+ pattern the pattern to compile
+ cflags compilation flags
+
+Returns: 0 on success
+ various non-zero codes on failure
+*/
+
+PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
+regcomp(regex_t *preg, const char *pattern, int cflags)
+{
+PCRE2_SIZE erroffset;
+PCRE2_SIZE patlen;
+int errorcode;
+int options = 0;
+int re_nsub = 0;
+
+patlen = ((cflags & REG_PEND) != 0)? (PCRE2_SIZE)(preg->re_endp - pattern) :
+ PCRE2_ZERO_TERMINATED;
+
+if ((cflags & REG_ICASE) != 0) options |= PCRE2_CASELESS;
+if ((cflags & REG_NEWLINE) != 0) options |= PCRE2_MULTILINE;
+if ((cflags & REG_DOTALL) != 0) options |= PCRE2_DOTALL;
+if ((cflags & REG_NOSPEC) != 0) options |= PCRE2_LITERAL;
+if ((cflags & REG_UTF) != 0) options |= PCRE2_UTF;
+if ((cflags & REG_UCP) != 0) options |= PCRE2_UCP;
+if ((cflags & REG_UNGREEDY) != 0) options |= PCRE2_UNGREEDY;
+
+preg->re_cflags = cflags;
+preg->re_pcre2_code = pcre2_compile((PCRE2_SPTR)pattern, patlen, options,
+ &errorcode, &erroffset, NULL);
+preg->re_erroffset = erroffset;
+
+if (preg->re_pcre2_code == NULL)
+ {
+ unsigned int i;
+
+ /* A negative value is a UTF error; otherwise all error codes are greater
+ than COMPILE_ERROR_BASE, but check, just in case. */
+
+ if (errorcode < COMPILE_ERROR_BASE) return REG_BADPAT;
+ errorcode -= COMPILE_ERROR_BASE;
+
+ if (errorcode < (int)(sizeof(eint1)/sizeof(const int)))
+ return eint1[errorcode];
+ for (i = 0; i < sizeof(eint2)/sizeof(const int); i += 2)
+ if (errorcode == eint2[i]) return eint2[i+1];
+ return REG_BADPAT;
+ }
+
+(void)pcre2_pattern_info((const pcre2_code *)preg->re_pcre2_code,
+ PCRE2_INFO_CAPTURECOUNT, &re_nsub);
+preg->re_nsub = (size_t)re_nsub;
+preg->re_match_data = pcre2_match_data_create(re_nsub + 1, NULL);
+
+if (preg->re_match_data == NULL)
+ {
+ pcre2_code_free(preg->re_pcre2_code);
+ return REG_ESPACE;
+ }
+
+return 0;
+}
+
+
+
+/*************************************************
+* Match a regular expression *
+*************************************************/
+
+/* A suitable match_data block, large enough to hold all possible captures, was
+obtained when the pattern was compiled, to save having to allocate and free it
+for each match. If REG_NOSUB was specified at compile time, the nmatch and
+pmatch arguments are ignored, and the only result is yes/no/error. */
+
+PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
+regexec(const regex_t *preg, const char *string, size_t nmatch,
+ regmatch_t pmatch[], int eflags)
+{
+int rc, so, eo;
+int options = 0;
+pcre2_match_data *md = (pcre2_match_data *)preg->re_match_data;
+
+if ((eflags & REG_NOTBOL) != 0) options |= PCRE2_NOTBOL;
+if ((eflags & REG_NOTEOL) != 0) options |= PCRE2_NOTEOL;
+if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE2_NOTEMPTY;
+
+((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */
+
+/* When REG_NOSUB was specified, or if no vector has been passed in which to
+put captured strings, ensure that nmatch is zero. This will stop any attempt to
+write to pmatch. */
+
+if ((preg->re_cflags & REG_NOSUB) != 0 || pmatch == NULL) nmatch = 0;
+
+/* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings.
+The man page from OS X says "REG_STARTEND affects only the location of the
+string, not how it is matched". That is why the "so" value is used to bump the
+start location rather than being passed as a PCRE2 "starting offset". */
+
+if ((eflags & REG_STARTEND) != 0)
+ {
+ if (pmatch == NULL) return REG_INVARG;
+ so = pmatch[0].rm_so;
+ eo = pmatch[0].rm_eo;
+ }
+else
+ {
+ so = 0;
+ eo = (int)strlen(string);
+ }
+
+rc = pcre2_match((const pcre2_code *)preg->re_pcre2_code,
+ (PCRE2_SPTR)string + so, (eo - so), 0, options, md, NULL);
+
+/* Successful match */
+
+if (rc >= 0)
+ {
+ size_t i;
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md);
+ if ((size_t)rc > nmatch) rc = (int)nmatch;
+ for (i = 0; i < (size_t)rc; i++)
+ {
+ pmatch[i].rm_so = (ovector[i*2] == PCRE2_UNSET)? -1 :
+ (int)(ovector[i*2] + so);
+ pmatch[i].rm_eo = (ovector[i*2+1] == PCRE2_UNSET)? -1 :
+ (int)(ovector[i*2+1] + so);
+ }
+ for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
+ return 0;
+ }
+
+/* Unsuccessful match */
+
+if (rc <= PCRE2_ERROR_UTF8_ERR1 && rc >= PCRE2_ERROR_UTF8_ERR21)
+ return REG_INVARG;
+
+switch(rc)
+ {
+ default: return REG_ASSERT;
+ case PCRE2_ERROR_BADMODE: return REG_INVARG;
+ case PCRE2_ERROR_BADMAGIC: return REG_INVARG;
+ case PCRE2_ERROR_BADOPTION: return REG_INVARG;
+ case PCRE2_ERROR_BADUTFOFFSET: return REG_INVARG;
+ case PCRE2_ERROR_MATCHLIMIT: return REG_ESPACE;
+ case PCRE2_ERROR_NOMATCH: return REG_NOMATCH;
+ case PCRE2_ERROR_NOMEMORY: return REG_ESPACE;
+ case PCRE2_ERROR_NULL: return REG_INVARG;
+ }
+}
+
+/* End of pcre2posix.c */
diff --git a/test/monniaux/pcre2-10.32/pcre2posix.h b/test/monniaux/pcre2-10.32/pcre2posix.h
new file mode 100644
index 00000000..4ae1d3c2
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2posix.h
@@ -0,0 +1,151 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* PCRE2 is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language.
+
+ Written by Philip Hazel
+ Original API code Copyright (c) 1997-2012 University of Cambridge
+ New API code Copyright (c) 2016 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* Have to include stdlib.h in order to ensure that size_t is defined. */
+
+#include <stdlib.h>
+
+/* Allow for C++ users */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Options, mostly defined by POSIX, but with some extras. */
+
+#define REG_ICASE 0x0001 /* Maps to PCRE2_CASELESS */
+#define REG_NEWLINE 0x0002 /* Maps to PCRE2_MULTILINE */
+#define REG_NOTBOL 0x0004 /* Maps to PCRE2_NOTBOL */
+#define REG_NOTEOL 0x0008 /* Maps to PCRE2_NOTEOL */
+#define REG_DOTALL 0x0010 /* NOT defined by POSIX; maps to PCRE2_DOTALL */
+#define REG_NOSUB 0x0020 /* Do not report what was matched */
+#define REG_UTF 0x0040 /* NOT defined by POSIX; maps to PCRE2_UTF */
+#define REG_STARTEND 0x0080 /* BSD feature: pass subject string by so,eo */
+#define REG_NOTEMPTY 0x0100 /* NOT defined by POSIX; maps to PCRE2_NOTEMPTY */
+#define REG_UNGREEDY 0x0200 /* NOT defined by POSIX; maps to PCRE2_UNGREEDY */
+#define REG_UCP 0x0400 /* NOT defined by POSIX; maps to PCRE2_UCP */
+#define REG_PEND 0x0800 /* GNU feature: pass end pattern by re_endp */
+#define REG_NOSPEC 0x1000 /* Maps to PCRE2_LITERAL */
+
+/* This is not used by PCRE2, but by defining it we make it easier
+to slot PCRE2 into existing programs that make POSIX calls. */
+
+#define REG_EXTENDED 0
+
+/* Error values. Not all these are relevant or used by the wrapper. */
+
+enum {
+ REG_ASSERT = 1, /* internal error ? */
+ REG_BADBR, /* invalid repeat counts in {} */
+ REG_BADPAT, /* pattern error */
+ REG_BADRPT, /* ? * + invalid */
+ REG_EBRACE, /* unbalanced {} */
+ REG_EBRACK, /* unbalanced [] */
+ REG_ECOLLATE, /* collation error - not relevant */
+ REG_ECTYPE, /* bad class */
+ REG_EESCAPE, /* bad escape sequence */
+ REG_EMPTY, /* empty expression */
+ REG_EPAREN, /* unbalanced () */
+ REG_ERANGE, /* bad range inside [] */
+ REG_ESIZE, /* expression too big */
+ REG_ESPACE, /* failed to get memory */
+ REG_ESUBREG, /* bad back reference */
+ REG_INVARG, /* bad argument */
+ REG_NOMATCH /* match failed */
+};
+
+
+/* The structure representing a compiled regular expression. It is also used
+for passing the pattern end pointer when REG_PEND is set. */
+
+typedef struct {
+ void *re_pcre2_code;
+ void *re_match_data;
+ const char *re_endp;
+ size_t re_nsub;
+ size_t re_erroffset;
+ int re_cflags;
+} regex_t;
+
+/* The structure in which a captured offset is returned. */
+
+typedef int regoff_t;
+
+typedef struct {
+ regoff_t rm_so;
+ regoff_t rm_eo;
+} regmatch_t;
+
+/* When an application links to a PCRE2 DLL in Windows, the symbols that are
+imported have to be identified as such. When building PCRE2, the appropriate
+export settings are needed, and are set in pcre2posix.c before including this
+file. */
+
+#if defined(_WIN32) && !defined(PCRE2_STATIC) && !defined(PCRE2POSIX_EXP_DECL)
+# define PCRE2POSIX_EXP_DECL extern __declspec(dllimport)
+# define PCRE2POSIX_EXP_DEFN __declspec(dllimport)
+#endif
+
+/* By default, we use the standard "extern" declarations. */
+
+#ifndef PCRE2POSIX_EXP_DECL
+# ifdef __cplusplus
+# define PCRE2POSIX_EXP_DECL extern "C"
+# define PCRE2POSIX_EXP_DEFN extern "C"
+# else
+# define PCRE2POSIX_EXP_DECL extern
+# define PCRE2POSIX_EXP_DEFN extern
+# endif
+#endif
+
+/* The functions */
+
+PCRE2POSIX_EXP_DECL int regcomp(regex_t *, const char *, int);
+PCRE2POSIX_EXP_DECL int regexec(const regex_t *, const char *, size_t,
+ regmatch_t *, int);
+PCRE2POSIX_EXP_DECL size_t regerror(int, const regex_t *, char *, size_t);
+PCRE2POSIX_EXP_DECL void regfree(regex_t *);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+/* End of pcre2posix.h */
diff --git a/test/monniaux/pcre2-10.32/pcre2test.c b/test/monniaux/pcre2-10.32/pcre2test.c
new file mode 100644
index 00000000..a1fb64cb
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/pcre2test.c
@@ -0,0 +1,8800 @@
+/*************************************************
+* PCRE2 testing program *
+*************************************************/
+
+/* PCRE2 is a library of functions to support regular expressions whose syntax
+and semantics are as close as possible to those of the Perl 5 language. In 2014
+the API was completely revised and '2' was added to the name, because the old
+API, which had lasted for 16 years, could not accommodate new requirements. At
+the same time, this testing program was re-designed because its original
+hacked-up (non-) design had also run out of steam.
+
+ Written by Philip Hazel
+ Original code Copyright (c) 1997-2012 University of Cambridge
+ Rewritten code Copyright (c) 2016-2018 University of Cambridge
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the University of Cambridge nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+
+/* This program supports testing of the 8-bit, 16-bit, and 32-bit PCRE2
+libraries in a single program, though its input and output are always 8-bit.
+It is different from modules such as pcre2_compile.c in the library itself,
+which are compiled separately for each code unit width. If two widths are
+enabled, for example, pcre2_compile.c is compiled twice. In contrast,
+pcre2test.c is compiled only once, and linked with all the enabled libraries.
+Therefore, it must not make use of any of the macros from pcre2.h or
+pcre2_internal.h that depend on PCRE2_CODE_UNIT_WIDTH. It does, however, make
+use of SUPPORT_PCRE2_8, SUPPORT_PCRE2_16, and SUPPORT_PCRE2_32, to ensure that
+it references only the enabled library functions. */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <locale.h>
+#include <errno.h>
+
+/* DM */
+#include "../clock.h"
+int fileno(FILE *stream);
+
+#if defined NATIVE_ZOS
+#include "pcrzoscs.h"
+/* That header is not included in the main PCRE2 distribution because other
+apparatus is needed to compile pcre2test for z/OS. The header can be found in
+the special z/OS distribution, which is available from www.zaconsultants.net or
+from www.cbttape.org. */
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+/* Debugging code enabler */
+
+/* #define DEBUG_SHOW_MALLOC_ADDRESSES */
+
+/* Both libreadline and libedit are optionally supported. The user-supplied
+original patch uses readline/readline.h for libedit, but in at least one system
+it is installed as editline/readline.h, so the configuration code now looks for
+that first, falling back to readline/readline.h. */
+
+#if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT)
+#if defined(SUPPORT_LIBREADLINE)
+#include <readline/readline.h>
+#include <readline/history.h>
+#else
+#if defined(HAVE_EDITLINE_READLINE_H)
+#include <editline/readline.h>
+#else
+#include <readline/readline.h>
+#endif
+#endif
+#endif
+
+/* Put the test for interactive input into a macro so that it can be changed if
+required for different environments. */
+
+#define INTERACTIVE(f) isatty(fileno(f))
+
+
+/* ---------------------- System-specific definitions ---------------------- */
+
+/* A number of things vary for Windows builds. Originally, pcretest opened its
+input and output without "b"; then I was told that "b" was needed in some
+environments, so it was added for release 5.0 to both the input and output. (It
+makes no difference on Unix-like systems.) Later I was told that it is wrong
+for the input on Windows. I've now abstracted the modes into macros that are
+set here, to make it easier to fiddle with them, and removed "b" from the input
+mode under Windows. The BINARY versions are used when saving/restoring compiled
+patterns. */
+
+#if defined(_WIN32) || defined(WIN32)
+#include <io.h> /* For _setmode() */
+#include <fcntl.h> /* For _O_BINARY */
+#define INPUT_MODE "r"
+#define OUTPUT_MODE "wb"
+#define BINARY_INPUT_MODE "rb"
+#define BINARY_OUTPUT_MODE "wb"
+
+#ifndef isatty
+#define isatty _isatty /* This is what Windows calls them, I'm told, */
+#endif /* though in some environments they seem to */
+ /* be already defined, hence the #ifndefs. */
+#ifndef fileno
+#define fileno _fileno
+#endif
+
+/* A user sent this fix for Borland Builder 5 under Windows. */
+
+#ifdef __BORLANDC__
+#define _setmode(handle, mode) setmode(handle, mode)
+#endif
+
+/* Not Windows */
+
+#else
+#include <sys/time.h> /* These two includes are needed */
+#include <sys/resource.h> /* for setrlimit(). */
+#if defined NATIVE_ZOS /* z/OS uses non-binary I/O */
+#define INPUT_MODE "r"
+#define OUTPUT_MODE "w"
+#define BINARY_INPUT_MODE "rb"
+#define BINARY_OUTPUT_MODE "wb"
+#else
+#define INPUT_MODE "rb"
+#define OUTPUT_MODE "wb"
+#define BINARY_INPUT_MODE "rb"
+#define BINARY_OUTPUT_MODE "wb"
+#endif
+#endif
+
+#ifdef __VMS
+#include <ssdef.h>
+void vms_setsymbol( char *, char *, int );
+#endif
+
+/* VC and older compilers don't support %td or %zu. */
+
+#if defined(_MSC_VER) || !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
+#define PTR_FORM "lu"
+#define SIZ_FORM "lu"
+#define SIZ_CAST (unsigned long int)
+#else
+#define PTR_FORM "td"
+#define SIZ_FORM "zu"
+#define SIZ_CAST
+#endif
+
+/* ------------------End of system-specific definitions -------------------- */
+
+/* Glueing macros that are used in several places below. */
+
+#define glue(a,b) a##b
+#define G(a,b) glue(a,b)
+
+/* Miscellaneous parameters and manifests */
+
+#ifndef CLOCKS_PER_SEC
+#ifdef CLK_TCK
+#define CLOCKS_PER_SEC CLK_TCK
+#else
+#define CLOCKS_PER_SEC 100
+#endif
+#endif
+
+#define CFORE_UNSET UINT32_MAX /* Unset value for startend/cfail/cerror fields */
+#define CONVERT_UNSET UINT32_MAX /* Unset value for convert_type field */
+#define DFA_WS_DIMENSION 1000 /* Size of DFA workspace */
+#define DEFAULT_OVECCOUNT 15 /* Default ovector count */
+#define JUNK_OFFSET 0xdeadbeef /* For initializing ovector */
+#define LOCALESIZE 32 /* Size of locale name */
+#define LOOPREPEAT 500000 /* Default loop count for timing */
+#define MALLOCLISTSIZE 20 /* For remembering mallocs */
+#define PARENS_NEST_DEFAULT 220 /* Default parentheses nest limit */
+#define PATSTACKSIZE 20 /* Pattern stack for save/restore testing */
+#define REPLACE_MODSIZE 100 /* Field for reading 8-bit replacement */
+#define VERSION_SIZE 64 /* Size of buffer for the version strings */
+
+/* Make sure the buffer into which replacement strings are copied is big enough
+to hold them as 32-bit code units. */
+
+#define REPLACE_BUFFSIZE 1024 /* This is a byte value */
+
+/* Execution modes */
+
+#define PCRE8_MODE 8
+#define PCRE16_MODE 16
+#define PCRE32_MODE 32
+
+/* Processing returns */
+
+enum { PR_OK, PR_SKIP, PR_ABEND };
+
+/* The macro PRINTABLE determines whether to print an output character as-is or
+as a hex value when showing compiled patterns. is We use it in cases when the
+locale has not been explicitly changed, so as to get consistent output from
+systems that differ in their output from isprint() even in the "C" locale. */
+
+#ifdef EBCDIC
+#define PRINTABLE(c) ((c) >= 64 && (c) < 255)
+#else
+#define PRINTABLE(c) ((c) >= 32 && (c) < 127)
+#endif
+
+#define PRINTOK(c) ((use_tables != NULL && c < 256)? isprint(c) : PRINTABLE(c))
+
+/* We have to include some of the library source files because we need
+to use some of the macros, internal structure definitions, and other internal
+values - pcre2test has "inside information" compared to an application program
+that strictly follows the PCRE2 API.
+
+Before including pcre2_internal.h we define PRIV so that it does not get
+defined therein. This ensures that PRIV names in the included files do not
+clash with those in the libraries. Also, although pcre2_internal.h does itself
+include pcre2.h, we explicitly include it beforehand, along with pcre2posix.h,
+so that the PCRE2_EXP_xxx macros get set appropriately for an application, not
+for building the library. */
+
+#define PRIV(name) name
+#define PCRE2_CODE_UNIT_WIDTH 0
+#include "pcre2.h"
+#include "pcre2posix.h"
+#include "pcre2_internal.h"
+
+/* We need access to some of the data tables that PCRE2 uses. Defining
+PCRE2_PCRETEST makes some minor changes in the files. The previous definition
+of PRIV avoids name clashes. */
+
+#define PCRE2_PCRE2TEST
+#include "pcre2_tables.c"
+#include "pcre2_ucd.c"
+
+/* 32-bit integer values in the input are read by strtoul() or strtol(). The
+check needed for overflow depends on whether long ints are in fact longer than
+ints. They are defined not to be shorter. */
+
+#if ULONG_MAX > UINT32_MAX
+#define U32OVERFLOW(x) (x > UINT32_MAX)
+#else
+#define U32OVERFLOW(x) (x == UINT32_MAX)
+#endif
+
+#if LONG_MAX > INT32_MAX
+#define S32OVERFLOW(x) (x > INT32_MAX || x < INT32_MIN)
+#else
+#define S32OVERFLOW(x) (x == INT32_MAX || x == INT32_MIN)
+#endif
+
+/* When PCRE2_CODE_UNIT_WIDTH is zero, pcre2_internal.h does not include
+pcre2_intmodedep.h, which is where mode-dependent macros and structures are
+defined. We can now include it for each supported code unit width. Because
+PCRE2_CODE_UNIT_WIDTH was defined as zero before including pcre2.h, it will
+have left PCRE2_SUFFIX defined as a no-op. We must re-define it appropriately
+while including these files, and then restore it to a no-op. Because LINK_SIZE
+may be changed in 16-bit mode and forced to 1 in 32-bit mode, the order of
+these inclusions should not be changed. */
+
+#undef PCRE2_SUFFIX
+#undef PCRE2_CODE_UNIT_WIDTH
+
+#ifdef SUPPORT_PCRE2_8
+#define PCRE2_CODE_UNIT_WIDTH 8
+#define PCRE2_SUFFIX(a) G(a,8)
+#include "pcre2_intmodedep.h"
+#include "pcre2_printint.c"
+#undef PCRE2_CODE_UNIT_WIDTH
+#undef PCRE2_SUFFIX
+#endif /* SUPPORT_PCRE2_8 */
+
+#ifdef SUPPORT_PCRE2_16
+#define PCRE2_CODE_UNIT_WIDTH 16
+#define PCRE2_SUFFIX(a) G(a,16)
+#include "pcre2_intmodedep.h"
+#include "pcre2_printint.c"
+#undef PCRE2_CODE_UNIT_WIDTH
+#undef PCRE2_SUFFIX
+#endif /* SUPPORT_PCRE2_16 */
+
+#ifdef SUPPORT_PCRE2_32
+#define PCRE2_CODE_UNIT_WIDTH 32
+#define PCRE2_SUFFIX(a) G(a,32)
+#include "pcre2_intmodedep.h"
+#include "pcre2_printint.c"
+#undef PCRE2_CODE_UNIT_WIDTH
+#undef PCRE2_SUFFIX
+#endif /* SUPPORT_PCRE2_32 */
+
+#define PCRE2_SUFFIX(a) a
+
+/* We need to be able to check input text for UTF-8 validity, whatever code
+widths are actually available, because the input to pcre2test is always in
+8-bit code units. So we include the UTF validity checking function for 8-bit
+code units. */
+
+extern int valid_utf(PCRE2_SPTR8, PCRE2_SIZE, PCRE2_SIZE *);
+
+#define PCRE2_CODE_UNIT_WIDTH 8
+#undef PCRE2_SPTR
+#define PCRE2_SPTR PCRE2_SPTR8
+#include "pcre2_valid_utf.c"
+#undef PCRE2_CODE_UNIT_WIDTH
+#undef PCRE2_SPTR
+
+/* If we have 8-bit support, default to it; if there is also 16-or 32-bit
+support, it can be selected by a command-line option. If there is no 8-bit
+support, there must be 16-bit or 32-bit support, so default to one of them. The
+config function, JIT stack, contexts, and version string are the same in all
+modes, so use the form of the first that is available. */
+
+#if defined SUPPORT_PCRE2_8
+#define DEFAULT_TEST_MODE PCRE8_MODE
+#define VERSION_TYPE PCRE2_UCHAR8
+#define PCRE2_CONFIG pcre2_config_8
+#define PCRE2_JIT_STACK pcre2_jit_stack_8
+#define PCRE2_REAL_GENERAL_CONTEXT pcre2_real_general_context_8
+#define PCRE2_REAL_COMPILE_CONTEXT pcre2_real_compile_context_8
+#define PCRE2_REAL_CONVERT_CONTEXT pcre2_real_convert_context_8
+#define PCRE2_REAL_MATCH_CONTEXT pcre2_real_match_context_8
+
+#elif defined SUPPORT_PCRE2_16
+#define DEFAULT_TEST_MODE PCRE16_MODE
+#define VERSION_TYPE PCRE2_UCHAR16
+#define PCRE2_CONFIG pcre2_config_16
+#define PCRE2_JIT_STACK pcre2_jit_stack_16
+#define PCRE2_REAL_GENERAL_CONTEXT pcre2_real_general_context_16
+#define PCRE2_REAL_COMPILE_CONTEXT pcre2_real_compile_context_16
+#define PCRE2_REAL_CONVERT_CONTEXT pcre2_real_convert_context_16
+#define PCRE2_REAL_MATCH_CONTEXT pcre2_real_match_context_16
+
+#elif defined SUPPORT_PCRE2_32
+#define DEFAULT_TEST_MODE PCRE32_MODE
+#define VERSION_TYPE PCRE2_UCHAR32
+#define PCRE2_CONFIG pcre2_config_32
+#define PCRE2_JIT_STACK pcre2_jit_stack_32
+#define PCRE2_REAL_GENERAL_CONTEXT pcre2_real_general_context_32
+#define PCRE2_REAL_COMPILE_CONTEXT pcre2_real_compile_context_32
+#define PCRE2_REAL_CONVERT_CONTEXT pcre2_real_convert_context_32
+#define PCRE2_REAL_MATCH_CONTEXT pcre2_real_match_context_32
+#endif
+
+/* ------------- Structure and table for handling #-commands ------------- */
+
+typedef struct cmdstruct {
+ const char *name;
+ int value;
+} cmdstruct;
+
+enum { CMD_FORBID_UTF, CMD_LOAD, CMD_NEWLINE_DEFAULT, CMD_PATTERN,
+ CMD_PERLTEST, CMD_POP, CMD_POPCOPY, CMD_SAVE, CMD_SUBJECT, CMD_UNKNOWN };
+
+static cmdstruct cmdlist[] = {
+ { "forbid_utf", CMD_FORBID_UTF },
+ { "load", CMD_LOAD },
+ { "newline_default", CMD_NEWLINE_DEFAULT },
+ { "pattern", CMD_PATTERN },
+ { "perltest", CMD_PERLTEST },
+ { "pop", CMD_POP },
+ { "popcopy", CMD_POPCOPY },
+ { "save", CMD_SAVE },
+ { "subject", CMD_SUBJECT }};
+
+#define cmdlistcount (sizeof(cmdlist)/sizeof(cmdstruct))
+
+/* ------------- Structures and tables for handling modifiers -------------- */
+
+/* Table of names for newline types. Must be kept in step with the definitions
+of PCRE2_NEWLINE_xx in pcre2.h. */
+
+static const char *newlines[] = {
+ "DEFAULT", "CR", "LF", "CRLF", "ANY", "ANYCRLF", "NUL" };
+
+/* Structure and table for handling pattern conversion types. */
+
+typedef struct convertstruct {
+ const char *name;
+ uint32_t option;
+} convertstruct;
+
+static convertstruct convertlist[] = {
+ { "glob", PCRE2_CONVERT_GLOB },
+ { "glob_no_starstar", PCRE2_CONVERT_GLOB_NO_STARSTAR },
+ { "glob_no_wild_separator", PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR },
+ { "posix_basic", PCRE2_CONVERT_POSIX_BASIC },
+ { "posix_extended", PCRE2_CONVERT_POSIX_EXTENDED },
+ { "unset", CONVERT_UNSET }};
+
+#define convertlistcount (sizeof(convertlist)/sizeof(convertstruct))
+
+/* Modifier types and applicability */
+
+enum { MOD_CTC, /* Applies to a compile context */
+ MOD_CTM, /* Applies to a match context */
+ MOD_PAT, /* Applies to a pattern */
+ MOD_PATP, /* Ditto, OK for Perl test */
+ MOD_DAT, /* Applies to a data line */
+ MOD_PD, /* Applies to a pattern or a data line */
+ MOD_PDP, /* As MOD_PD, OK for Perl test */
+ MOD_PND, /* As MOD_PD, but not for a default pattern */
+ MOD_PNDP, /* As MOD_PND, OK for Perl test */
+ MOD_CHR, /* Is a single character */
+ MOD_CON, /* Is a "convert" type/options list */
+ MOD_CTL, /* Is a control bit */
+ MOD_BSR, /* Is a BSR value */
+ MOD_IN2, /* Is one or two unsigned integers */
+ MOD_INS, /* Is a signed integer */
+ MOD_INT, /* Is an unsigned integer */
+ MOD_IND, /* Is an unsigned integer, but no value => default */
+ MOD_NL, /* Is a newline value */
+ MOD_NN, /* Is a number or a name; more than one may occur */
+ MOD_OPT, /* Is an option bit */
+ MOD_SIZ, /* Is a PCRE2_SIZE value */
+ MOD_STR }; /* Is a string */
+
+/* Control bits. Some apply to compiling, some to matching, but some can be set
+either on a pattern or a data line, so they must all be distinct. There are now
+so many of them that they are split into two fields. */
+
+#define CTL_AFTERTEXT 0x00000001u
+#define CTL_ALLAFTERTEXT 0x00000002u
+#define CTL_ALLCAPTURES 0x00000004u
+#define CTL_ALLUSEDTEXT 0x00000008u
+#define CTL_ALTGLOBAL 0x00000010u
+#define CTL_BINCODE 0x00000020u
+#define CTL_CALLOUT_CAPTURE 0x00000040u
+#define CTL_CALLOUT_INFO 0x00000080u
+#define CTL_CALLOUT_NONE 0x00000100u
+#define CTL_DFA 0x00000200u
+#define CTL_EXPAND 0x00000400u
+#define CTL_FINDLIMITS 0x00000800u
+#define CTL_FRAMESIZE 0x00001000u
+#define CTL_FULLBINCODE 0x00002000u
+#define CTL_GETALL 0x00004000u
+#define CTL_GLOBAL 0x00008000u
+#define CTL_HEXPAT 0x00010000u /* Same word as USE_LENGTH */
+#define CTL_INFO 0x00020000u
+#define CTL_JITFAST 0x00040000u
+#define CTL_JITVERIFY 0x00080000u
+#define CTL_MARK 0x00100000u
+#define CTL_MEMORY 0x00200000u
+#define CTL_NULLCONTEXT 0x00400000u
+#define CTL_POSIX 0x00800000u
+#define CTL_POSIX_NOSUB 0x01000000u
+#define CTL_PUSH 0x02000000u /* These three must be */
+#define CTL_PUSHCOPY 0x04000000u /* all in the same */
+#define CTL_PUSHTABLESCOPY 0x08000000u /* word. */
+#define CTL_STARTCHAR 0x10000000u
+#define CTL_USE_LENGTH 0x20000000u /* Same word as HEXPAT */
+#define CTL_UTF8_INPUT 0x40000000u
+#define CTL_ZERO_TERMINATE 0x80000000u
+
+/* Combinations */
+
+#define CTL_DEBUG (CTL_FULLBINCODE|CTL_INFO) /* For setting */
+#define CTL_ANYINFO (CTL_DEBUG|CTL_BINCODE|CTL_CALLOUT_INFO)
+#define CTL_ANYGLOB (CTL_ALTGLOBAL|CTL_GLOBAL)
+
+/* Second control word */
+
+#define CTL2_SUBSTITUTE_EXTENDED 0x00000001u
+#define CTL2_SUBSTITUTE_OVERFLOW_LENGTH 0x00000002u
+#define CTL2_SUBSTITUTE_UNKNOWN_UNSET 0x00000004u
+#define CTL2_SUBSTITUTE_UNSET_EMPTY 0x00000008u
+#define CTL2_SUBJECT_LITERAL 0x00000010u
+#define CTL2_CALLOUT_NO_WHERE 0x00000020u
+#define CTL2_CALLOUT_EXTRA 0x00000040u
+
+#define CTL2_NL_SET 0x40000000u /* Informational */
+#define CTL2_BSR_SET 0x80000000u /* Informational */
+
+/* These are the matching controls that may be set either on a pattern or on a
+data line. They are copied from the pattern controls as initial settings for
+data line controls. Note that CTL_MEMORY is not included here, because it does
+different things in the two cases. */
+
+#define CTL_ALLPD (CTL_AFTERTEXT|\
+ CTL_ALLAFTERTEXT|\
+ CTL_ALLCAPTURES|\
+ CTL_ALLUSEDTEXT|\
+ CTL_ALTGLOBAL|\
+ CTL_GLOBAL|\
+ CTL_MARK|\
+ CTL_STARTCHAR|\
+ CTL_UTF8_INPUT)
+
+#define CTL2_ALLPD (CTL2_SUBSTITUTE_EXTENDED|\
+ CTL2_SUBSTITUTE_OVERFLOW_LENGTH|\
+ CTL2_SUBSTITUTE_UNKNOWN_UNSET|\
+ CTL2_SUBSTITUTE_UNSET_EMPTY)
+
+/* Structures for holding modifier information for patterns and subject strings
+(data). Fields containing modifiers that can be set either for a pattern or a
+subject must be at the start and in the same order in both cases so that the
+same offset in the big table below works for both. */
+
+typedef struct patctl { /* Structure for pattern modifiers. */
+ uint32_t options; /* Must be in same position as datctl */
+ uint32_t control; /* Must be in same position as datctl */
+ uint32_t control2; /* Must be in same position as datctl */
+ uint32_t jitstack; /* Must be in same position as datctl */
+ uint8_t replacement[REPLACE_MODSIZE]; /* So must this */
+ uint32_t jit;
+ uint32_t stackguard_test;
+ uint32_t tables_id;
+ uint32_t convert_type;
+ uint32_t convert_length;
+ uint32_t convert_glob_escape;
+ uint32_t convert_glob_separator;
+ uint32_t regerror_buffsize;
+ uint8_t locale[LOCALESIZE];
+} patctl;
+
+#define MAXCPYGET 10
+#define LENCPYGET 64
+
+typedef struct datctl { /* Structure for data line modifiers. */
+ uint32_t options; /* Must be in same position as patctl */
+ uint32_t control; /* Must be in same position as patctl */
+ uint32_t control2; /* Must be in same position as patctl */
+ uint32_t jitstack; /* Must be in same position as patctl */
+ uint8_t replacement[REPLACE_MODSIZE]; /* So must this */
+ uint32_t startend[2];
+ uint32_t cerror[2];
+ uint32_t cfail[2];
+ int32_t callout_data;
+ int32_t copy_numbers[MAXCPYGET];
+ int32_t get_numbers[MAXCPYGET];
+ uint32_t oveccount;
+ uint32_t offset;
+ uint8_t copy_names[LENCPYGET];
+ uint8_t get_names[LENCPYGET];
+} datctl;
+
+/* Ids for which context to modify. */
+
+enum { CTX_PAT, /* Active pattern context */
+ CTX_POPPAT, /* Ditto, for a popped pattern */
+ CTX_DEFPAT, /* Default pattern context */
+ CTX_DAT, /* Active data (match) context */
+ CTX_DEFDAT }; /* Default data (match) context */
+
+/* Macros to simplify the big table below. */
+
+#define CO(name) offsetof(PCRE2_REAL_COMPILE_CONTEXT, name)
+#define MO(name) offsetof(PCRE2_REAL_MATCH_CONTEXT, name)
+#define PO(name) offsetof(patctl, name)
+#define PD(name) PO(name)
+#define DO(name) offsetof(datctl, name)
+
+/* Table of all long-form modifiers. Must be in collating sequence of modifier
+name because it is searched by binary chop. */
+
+typedef struct modstruct {
+ const char *name;
+ uint16_t which;
+ uint16_t type;
+ uint32_t value;
+ PCRE2_SIZE offset;
+} modstruct;
+
+static modstruct modlist[] = {
+ { "aftertext", MOD_PNDP, MOD_CTL, CTL_AFTERTEXT, PO(control) },
+ { "allaftertext", MOD_PNDP, MOD_CTL, CTL_ALLAFTERTEXT, PO(control) },
+ { "allcaptures", MOD_PND, MOD_CTL, CTL_ALLCAPTURES, PO(control) },
+ { "allow_empty_class", MOD_PAT, MOD_OPT, PCRE2_ALLOW_EMPTY_CLASS, PO(options) },
+ { "allow_surrogate_escapes", MOD_CTC, MOD_OPT, PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES, CO(extra_options) },
+ { "allusedtext", MOD_PNDP, MOD_CTL, CTL_ALLUSEDTEXT, PO(control) },
+ { "alt_bsux", MOD_PAT, MOD_OPT, PCRE2_ALT_BSUX, PO(options) },
+ { "alt_circumflex", MOD_PAT, MOD_OPT, PCRE2_ALT_CIRCUMFLEX, PO(options) },
+ { "alt_verbnames", MOD_PAT, MOD_OPT, PCRE2_ALT_VERBNAMES, PO(options) },
+ { "altglobal", MOD_PND, MOD_CTL, CTL_ALTGLOBAL, PO(control) },
+ { "anchored", MOD_PD, MOD_OPT, PCRE2_ANCHORED, PD(options) },
+ { "auto_callout", MOD_PAT, MOD_OPT, PCRE2_AUTO_CALLOUT, PO(options) },
+ { "bad_escape_is_literal", MOD_CTC, MOD_OPT, PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL, CO(extra_options) },
+ { "bincode", MOD_PAT, MOD_CTL, CTL_BINCODE, PO(control) },
+ { "bsr", MOD_CTC, MOD_BSR, 0, CO(bsr_convention) },
+ { "callout_capture", MOD_DAT, MOD_CTL, CTL_CALLOUT_CAPTURE, DO(control) },
+ { "callout_data", MOD_DAT, MOD_INS, 0, DO(callout_data) },
+ { "callout_error", MOD_DAT, MOD_IN2, 0, DO(cerror) },
+ { "callout_extra", MOD_DAT, MOD_CTL, CTL2_CALLOUT_EXTRA, DO(control2) },
+ { "callout_fail", MOD_DAT, MOD_IN2, 0, DO(cfail) },
+ { "callout_info", MOD_PAT, MOD_CTL, CTL_CALLOUT_INFO, PO(control) },
+ { "callout_no_where", MOD_DAT, MOD_CTL, CTL2_CALLOUT_NO_WHERE, DO(control2) },
+ { "callout_none", MOD_DAT, MOD_CTL, CTL_CALLOUT_NONE, DO(control) },
+ { "caseless", MOD_PATP, MOD_OPT, PCRE2_CASELESS, PO(options) },
+ { "convert", MOD_PAT, MOD_CON, 0, PO(convert_type) },
+ { "convert_glob_escape", MOD_PAT, MOD_CHR, 0, PO(convert_glob_escape) },
+ { "convert_glob_separator", MOD_PAT, MOD_CHR, 0, PO(convert_glob_separator) },
+ { "convert_length", MOD_PAT, MOD_INT, 0, PO(convert_length) },
+ { "copy", MOD_DAT, MOD_NN, DO(copy_numbers), DO(copy_names) },
+ { "debug", MOD_PAT, MOD_CTL, CTL_DEBUG, PO(control) },
+ { "depth_limit", MOD_CTM, MOD_INT, 0, MO(depth_limit) },
+ { "dfa", MOD_DAT, MOD_CTL, CTL_DFA, DO(control) },
+ { "dfa_restart", MOD_DAT, MOD_OPT, PCRE2_DFA_RESTART, DO(options) },
+ { "dfa_shortest", MOD_DAT, MOD_OPT, PCRE2_DFA_SHORTEST, DO(options) },
+ { "dollar_endonly", MOD_PAT, MOD_OPT, PCRE2_DOLLAR_ENDONLY, PO(options) },
+ { "dotall", MOD_PATP, MOD_OPT, PCRE2_DOTALL, PO(options) },
+ { "dupnames", MOD_PATP, MOD_OPT, PCRE2_DUPNAMES, PO(options) },
+ { "endanchored", MOD_PD, MOD_OPT, PCRE2_ENDANCHORED, PD(options) },
+ { "expand", MOD_PAT, MOD_CTL, CTL_EXPAND, PO(control) },
+ { "extended", MOD_PATP, MOD_OPT, PCRE2_EXTENDED, PO(options) },
+ { "extended_more", MOD_PATP, MOD_OPT, PCRE2_EXTENDED_MORE, PO(options) },
+ { "find_limits", MOD_DAT, MOD_CTL, CTL_FINDLIMITS, DO(control) },
+ { "firstline", MOD_PAT, MOD_OPT, PCRE2_FIRSTLINE, PO(options) },
+ { "framesize", MOD_PAT, MOD_CTL, CTL_FRAMESIZE, PO(control) },
+ { "fullbincode", MOD_PAT, MOD_CTL, CTL_FULLBINCODE, PO(control) },
+ { "get", MOD_DAT, MOD_NN, DO(get_numbers), DO(get_names) },
+ { "getall", MOD_DAT, MOD_CTL, CTL_GETALL, DO(control) },
+ { "global", MOD_PNDP, MOD_CTL, CTL_GLOBAL, PO(control) },
+ { "heap_limit", MOD_CTM, MOD_INT, 0, MO(heap_limit) },
+ { "hex", MOD_PAT, MOD_CTL, CTL_HEXPAT, PO(control) },
+ { "info", MOD_PAT, MOD_CTL, CTL_INFO, PO(control) },
+ { "jit", MOD_PAT, MOD_IND, 7, PO(jit) },
+ { "jitfast", MOD_PAT, MOD_CTL, CTL_JITFAST, PO(control) },
+ { "jitstack", MOD_PNDP, MOD_INT, 0, PO(jitstack) },
+ { "jitverify", MOD_PAT, MOD_CTL, CTL_JITVERIFY, PO(control) },
+ { "literal", MOD_PAT, MOD_OPT, PCRE2_LITERAL, PO(options) },
+ { "locale", MOD_PAT, MOD_STR, LOCALESIZE, PO(locale) },
+ { "mark", MOD_PNDP, MOD_CTL, CTL_MARK, PO(control) },
+ { "match_limit", MOD_CTM, MOD_INT, 0, MO(match_limit) },
+ { "match_line", MOD_CTC, MOD_OPT, PCRE2_EXTRA_MATCH_LINE, CO(extra_options) },
+ { "match_unset_backref", MOD_PAT, MOD_OPT, PCRE2_MATCH_UNSET_BACKREF, PO(options) },
+ { "match_word", MOD_CTC, MOD_OPT, PCRE2_EXTRA_MATCH_WORD, CO(extra_options) },
+ { "max_pattern_length", MOD_CTC, MOD_SIZ, 0, CO(max_pattern_length) },
+ { "memory", MOD_PD, MOD_CTL, CTL_MEMORY, PD(control) },
+ { "multiline", MOD_PATP, MOD_OPT, PCRE2_MULTILINE, PO(options) },
+ { "never_backslash_c", MOD_PAT, MOD_OPT, PCRE2_NEVER_BACKSLASH_C, PO(options) },
+ { "never_ucp", MOD_PAT, MOD_OPT, PCRE2_NEVER_UCP, PO(options) },
+ { "never_utf", MOD_PAT, MOD_OPT, PCRE2_NEVER_UTF, PO(options) },
+ { "newline", MOD_CTC, MOD_NL, 0, CO(newline_convention) },
+ { "no_auto_capture", MOD_PAT, MOD_OPT, PCRE2_NO_AUTO_CAPTURE, PO(options) },
+ { "no_auto_possess", MOD_PATP, MOD_OPT, PCRE2_NO_AUTO_POSSESS, PO(options) },
+ { "no_dotstar_anchor", MOD_PAT, MOD_OPT, PCRE2_NO_DOTSTAR_ANCHOR, PO(options) },
+ { "no_jit", MOD_DAT, MOD_OPT, PCRE2_NO_JIT, DO(options) },
+ { "no_start_optimize", MOD_PATP, MOD_OPT, PCRE2_NO_START_OPTIMIZE, PO(options) },
+ { "no_utf_check", MOD_PD, MOD_OPT, PCRE2_NO_UTF_CHECK, PD(options) },
+ { "notbol", MOD_DAT, MOD_OPT, PCRE2_NOTBOL, DO(options) },
+ { "notempty", MOD_DAT, MOD_OPT, PCRE2_NOTEMPTY, DO(options) },
+ { "notempty_atstart", MOD_DAT, MOD_OPT, PCRE2_NOTEMPTY_ATSTART, DO(options) },
+ { "noteol", MOD_DAT, MOD_OPT, PCRE2_NOTEOL, DO(options) },
+ { "null_context", MOD_PD, MOD_CTL, CTL_NULLCONTEXT, PO(control) },
+ { "offset", MOD_DAT, MOD_INT, 0, DO(offset) },
+ { "offset_limit", MOD_CTM, MOD_SIZ, 0, MO(offset_limit)},
+ { "ovector", MOD_DAT, MOD_INT, 0, DO(oveccount) },
+ { "parens_nest_limit", MOD_CTC, MOD_INT, 0, CO(parens_nest_limit) },
+ { "partial_hard", MOD_DAT, MOD_OPT, PCRE2_PARTIAL_HARD, DO(options) },
+ { "partial_soft", MOD_DAT, MOD_OPT, PCRE2_PARTIAL_SOFT, DO(options) },
+ { "ph", MOD_DAT, MOD_OPT, PCRE2_PARTIAL_HARD, DO(options) },
+ { "posix", MOD_PAT, MOD_CTL, CTL_POSIX, PO(control) },
+ { "posix_nosub", MOD_PAT, MOD_CTL, CTL_POSIX|CTL_POSIX_NOSUB, PO(control) },
+ { "posix_startend", MOD_DAT, MOD_IN2, 0, DO(startend) },
+ { "ps", MOD_DAT, MOD_OPT, PCRE2_PARTIAL_SOFT, DO(options) },
+ { "push", MOD_PAT, MOD_CTL, CTL_PUSH, PO(control) },
+ { "pushcopy", MOD_PAT, MOD_CTL, CTL_PUSHCOPY, PO(control) },
+ { "pushtablescopy", MOD_PAT, MOD_CTL, CTL_PUSHTABLESCOPY, PO(control) },
+ { "recursion_limit", MOD_CTM, MOD_INT, 0, MO(depth_limit) }, /* Obsolete synonym */
+ { "regerror_buffsize", MOD_PAT, MOD_INT, 0, PO(regerror_buffsize) },
+ { "replace", MOD_PND, MOD_STR, REPLACE_MODSIZE, PO(replacement) },
+ { "stackguard", MOD_PAT, MOD_INT, 0, PO(stackguard_test) },
+ { "startchar", MOD_PND, MOD_CTL, CTL_STARTCHAR, PO(control) },
+ { "startoffset", MOD_DAT, MOD_INT, 0, DO(offset) },
+ { "subject_literal", MOD_PATP, MOD_CTL, CTL2_SUBJECT_LITERAL, PO(control2) },
+ { "substitute_extended", MOD_PND, MOD_CTL, CTL2_SUBSTITUTE_EXTENDED, PO(control2) },
+ { "substitute_overflow_length", MOD_PND, MOD_CTL, CTL2_SUBSTITUTE_OVERFLOW_LENGTH, PO(control2) },
+ { "substitute_unknown_unset", MOD_PND, MOD_CTL, CTL2_SUBSTITUTE_UNKNOWN_UNSET, PO(control2) },
+ { "substitute_unset_empty", MOD_PND, MOD_CTL, CTL2_SUBSTITUTE_UNSET_EMPTY, PO(control2) },
+ { "tables", MOD_PAT, MOD_INT, 0, PO(tables_id) },
+ { "ucp", MOD_PATP, MOD_OPT, PCRE2_UCP, PO(options) },
+ { "ungreedy", MOD_PAT, MOD_OPT, PCRE2_UNGREEDY, PO(options) },
+ { "use_length", MOD_PAT, MOD_CTL, CTL_USE_LENGTH, PO(control) },
+ { "use_offset_limit", MOD_PAT, MOD_OPT, PCRE2_USE_OFFSET_LIMIT, PO(options) },
+ { "utf", MOD_PATP, MOD_OPT, PCRE2_UTF, PO(options) },
+ { "utf8_input", MOD_PAT, MOD_CTL, CTL_UTF8_INPUT, PO(control) },
+ { "zero_terminate", MOD_DAT, MOD_CTL, CTL_ZERO_TERMINATE, DO(control) }
+};
+
+#define MODLISTCOUNT sizeof(modlist)/sizeof(modstruct)
+
+/* Controls and options that are supported for use with the POSIX interface. */
+
+#define POSIX_SUPPORTED_COMPILE_OPTIONS ( \
+ PCRE2_CASELESS|PCRE2_DOTALL|PCRE2_LITERAL|PCRE2_MULTILINE|PCRE2_UCP| \
+ PCRE2_UTF|PCRE2_UNGREEDY)
+
+#define POSIX_SUPPORTED_COMPILE_EXTRA_OPTIONS (0)
+
+#define POSIX_SUPPORTED_COMPILE_CONTROLS ( \
+ CTL_AFTERTEXT|CTL_ALLAFTERTEXT|CTL_EXPAND|CTL_HEXPAT|CTL_POSIX| \
+ CTL_POSIX_NOSUB|CTL_USE_LENGTH)
+
+#define POSIX_SUPPORTED_COMPILE_CONTROLS2 (0)
+
+#define POSIX_SUPPORTED_MATCH_OPTIONS ( \
+ PCRE2_NOTBOL|PCRE2_NOTEMPTY|PCRE2_NOTEOL)
+
+#define POSIX_SUPPORTED_MATCH_CONTROLS (CTL_AFTERTEXT|CTL_ALLAFTERTEXT)
+#define POSIX_SUPPORTED_MATCH_CONTROLS2 (0)
+
+/* Control bits that are not ignored with 'push'. */
+
+#define PUSH_SUPPORTED_COMPILE_CONTROLS ( \
+ CTL_BINCODE|CTL_CALLOUT_INFO|CTL_FULLBINCODE|CTL_HEXPAT|CTL_INFO| \
+ CTL_JITVERIFY|CTL_MEMORY|CTL_FRAMESIZE|CTL_PUSH|CTL_PUSHCOPY| \
+ CTL_PUSHTABLESCOPY|CTL_USE_LENGTH)
+
+#define PUSH_SUPPORTED_COMPILE_CONTROLS2 (CTL2_BSR_SET|CTL2_NL_SET)
+
+/* Controls that apply only at compile time with 'push'. */
+
+#define PUSH_COMPILE_ONLY_CONTROLS CTL_JITVERIFY
+#define PUSH_COMPILE_ONLY_CONTROLS2 (0)
+
+/* Controls that are forbidden with #pop or #popcopy. */
+
+#define NOTPOP_CONTROLS (CTL_HEXPAT|CTL_POSIX|CTL_POSIX_NOSUB|CTL_PUSH| \
+ CTL_PUSHCOPY|CTL_PUSHTABLESCOPY|CTL_USE_LENGTH)
+
+/* Pattern controls that are mutually exclusive. At present these are all in
+the first control word. Note that CTL_POSIX_NOSUB is always accompanied by
+CTL_POSIX, so it doesn't need its own entries. */
+
+static uint32_t exclusive_pat_controls[] = {
+ CTL_POSIX | CTL_PUSH,
+ CTL_POSIX | CTL_PUSHCOPY,
+ CTL_POSIX | CTL_PUSHTABLESCOPY,
+ CTL_PUSH | CTL_PUSHCOPY,
+ CTL_PUSH | CTL_PUSHTABLESCOPY,
+ CTL_PUSHCOPY | CTL_PUSHTABLESCOPY,
+ CTL_EXPAND | CTL_HEXPAT };
+
+/* Data controls that are mutually exclusive. At present these are all in the
+first control word. */
+
+static uint32_t exclusive_dat_controls[] = {
+ CTL_ALLUSEDTEXT | CTL_STARTCHAR,
+ CTL_FINDLIMITS | CTL_NULLCONTEXT };
+
+/* Table of single-character abbreviated modifiers. The index field is
+initialized to -1, but the first time the modifier is encountered, it is filled
+in with the index of the full entry in modlist, to save repeated searching when
+processing multiple test items. This short list is searched serially, so its
+order does not matter. */
+
+typedef struct c1modstruct {
+ const char *fullname;
+ uint32_t onechar;
+ int index;
+} c1modstruct;
+
+static c1modstruct c1modlist[] = {
+ { "bincode", 'B', -1 },
+ { "info", 'I', -1 },
+ { "global", 'g', -1 },
+ { "caseless", 'i', -1 },
+ { "multiline", 'm', -1 },
+ { "no_auto_capture", 'n', -1 },
+ { "dotall", 's', -1 },
+ { "extended", 'x', -1 }
+};
+
+#define C1MODLISTCOUNT sizeof(c1modlist)/sizeof(c1modstruct)
+
+/* Table of arguments for the -C command line option. Use macros to make the
+table itself easier to read. */
+
+#if defined SUPPORT_PCRE2_8
+#define SUPPORT_8 1
+#endif
+#if defined SUPPORT_PCRE2_16
+#define SUPPORT_16 1
+#endif
+#if defined SUPPORT_PCRE2_32
+#define SUPPORT_32 1
+#endif
+
+#ifndef SUPPORT_8
+#define SUPPORT_8 0
+#endif
+#ifndef SUPPORT_16
+#define SUPPORT_16 0
+#endif
+#ifndef SUPPORT_32
+#define SUPPORT_32 0
+#endif
+
+#ifdef EBCDIC
+#define SUPPORT_EBCDIC 1
+#define EBCDIC_NL CHAR_LF
+#else
+#define SUPPORT_EBCDIC 0
+#define EBCDIC_NL 0
+#endif
+
+#ifdef NEVER_BACKSLASH_C
+#define BACKSLASH_C 0
+#else
+#define BACKSLASH_C 1
+#endif
+
+typedef struct coptstruct {
+ const char *name;
+ uint32_t type;
+ uint32_t value;
+} coptstruct;
+
+enum { CONF_BSR,
+ CONF_FIX,
+ CONF_FIZ,
+ CONF_INT,
+ CONF_NL
+};
+
+static coptstruct coptlist[] = {
+ { "backslash-C", CONF_FIX, BACKSLASH_C },
+ { "bsr", CONF_BSR, PCRE2_CONFIG_BSR },
+ { "ebcdic", CONF_FIX, SUPPORT_EBCDIC },
+ { "ebcdic-nl", CONF_FIZ, EBCDIC_NL },
+ { "jit", CONF_INT, PCRE2_CONFIG_JIT },
+ { "linksize", CONF_INT, PCRE2_CONFIG_LINKSIZE },
+ { "newline", CONF_NL, PCRE2_CONFIG_NEWLINE },
+ { "pcre2-16", CONF_FIX, SUPPORT_16 },
+ { "pcre2-32", CONF_FIX, SUPPORT_32 },
+ { "pcre2-8", CONF_FIX, SUPPORT_8 },
+ { "unicode", CONF_INT, PCRE2_CONFIG_UNICODE }
+};
+
+#define COPTLISTCOUNT sizeof(coptlist)/sizeof(coptstruct)
+
+#undef SUPPORT_8
+#undef SUPPORT_16
+#undef SUPPORT_32
+#undef SUPPORT_EBCDIC
+
+
+/* ----------------------- Static variables ------------------------ */
+
+static FILE *infile;
+static FILE *outfile;
+
+static const void *last_callout_mark;
+static PCRE2_JIT_STACK *jit_stack = NULL;
+static size_t jit_stack_size = 0;
+
+static BOOL first_callout;
+static BOOL jit_was_used;
+static BOOL restrict_for_perl_test = FALSE;
+static BOOL show_memory = FALSE;
+
+static int code_unit_size; /* Bytes */
+static int jitrc; /* Return from JIT compile */
+static int test_mode = DEFAULT_TEST_MODE;
+static int timeit = 0;
+static int timeitm = 0;
+
+clock_t total_compile_time = 0;
+clock_t total_jit_compile_time = 0;
+clock_t total_match_time = 0;
+
+static uint32_t dfa_matched;
+static uint32_t forbid_utf = 0;
+static uint32_t maxlookbehind;
+static uint32_t max_oveccount;
+static uint32_t callout_count;
+
+static uint16_t local_newline_default = 0;
+
+static VERSION_TYPE jittarget[VERSION_SIZE];
+static VERSION_TYPE version[VERSION_SIZE];
+static VERSION_TYPE uversion[VERSION_SIZE];
+
+static patctl def_patctl;
+static patctl pat_patctl;
+static datctl def_datctl;
+static datctl dat_datctl;
+
+static void *patstack[PATSTACKSIZE];
+static int patstacknext = 0;
+
+static void *malloclist[MALLOCLISTSIZE];
+static PCRE2_SIZE malloclistlength[MALLOCLISTSIZE];
+static uint32_t malloclistptr = 0;
+
+#ifdef SUPPORT_PCRE2_8
+static regex_t preg = { NULL, NULL, 0, 0, 0, 0 };
+#endif
+
+static int *dfa_workspace = NULL;
+static const uint8_t *locale_tables = NULL;
+static const uint8_t *use_tables = NULL;
+static uint8_t locale_name[32];
+
+/* We need buffers for building 16/32-bit strings; 8-bit strings don't need
+rebuilding, but set up the same naming scheme for use in macros. The "buffer"
+buffer is where all input lines are read. Its size is the same as pbuffer8.
+Pattern lines are always copied to pbuffer8 for use in callouts, even if they
+are actually compiled from pbuffer16 or pbuffer32. */
+
+static size_t pbuffer8_size = 50000; /* Initial size, bytes */
+static uint8_t *pbuffer8 = NULL;
+static uint8_t *buffer = NULL;
+
+/* The dbuffer is where all processed data lines are put. In non-8-bit modes it
+is cast as needed. For long data lines it grows as necessary. */
+
+static size_t dbuffer_size = 1u << 14; /* Initial size, bytes */
+static uint8_t *dbuffer = NULL;
+
+
+/* ---------------- Mode-dependent variables -------------------*/
+
+#ifdef SUPPORT_PCRE2_8
+static pcre2_code_8 *compiled_code8;
+static pcre2_general_context_8 *general_context8, *general_context_copy8;
+static pcre2_compile_context_8 *pat_context8, *default_pat_context8;
+static pcre2_convert_context_8 *con_context8, *default_con_context8;
+static pcre2_match_context_8 *dat_context8, *default_dat_context8;
+static pcre2_match_data_8 *match_data8;
+#endif
+
+#ifdef SUPPORT_PCRE2_16
+static pcre2_code_16 *compiled_code16;
+static pcre2_general_context_16 *general_context16, *general_context_copy16;
+static pcre2_compile_context_16 *pat_context16, *default_pat_context16;
+static pcre2_convert_context_16 *con_context16, *default_con_context16;
+static pcre2_match_context_16 *dat_context16, *default_dat_context16;
+static pcre2_match_data_16 *match_data16;
+static PCRE2_SIZE pbuffer16_size = 0; /* Set only when needed */
+static uint16_t *pbuffer16 = NULL;
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+static pcre2_code_32 *compiled_code32;
+static pcre2_general_context_32 *general_context32, *general_context_copy32;
+static pcre2_compile_context_32 *pat_context32, *default_pat_context32;
+static pcre2_convert_context_32 *con_context32, *default_con_context32;
+static pcre2_match_context_32 *dat_context32, *default_dat_context32;
+static pcre2_match_data_32 *match_data32;
+static PCRE2_SIZE pbuffer32_size = 0; /* Set only when needed */
+static uint32_t *pbuffer32 = NULL;
+#endif
+
+
+/* ---------------- Macros that work in all modes ----------------- */
+
+#define CAST8VAR(x) CASTVAR(uint8_t *, x)
+#define SET(x,y) SETOP(x,y,=)
+#define SETPLUS(x,y) SETOP(x,y,+=)
+#define strlen8(x) strlen((char *)x)
+
+
+/* ---------------- Mode-dependent, runtime-testing macros ------------------*/
+
+/* Define macros for variables and functions that must be selected dynamically
+depending on the mode setting (8, 16, 32). These are dependent on which modes
+are supported. */
+
+#if (defined (SUPPORT_PCRE2_8) + defined (SUPPORT_PCRE2_16) + \
+ defined (SUPPORT_PCRE2_32)) >= 2
+
+/* ----- All three modes supported ----- */
+
+#if defined(SUPPORT_PCRE2_8) && defined(SUPPORT_PCRE2_16) && defined(SUPPORT_PCRE2_32)
+
+#define CASTFLD(t,a,b) ((test_mode == PCRE8_MODE)? (t)(G(a,8)->b) : \
+ (test_mode == PCRE16_MODE)? (t)(G(a,16)->b) : (t)(G(a,32)->b))
+
+#define CASTVAR(t,x) ( \
+ (test_mode == PCRE8_MODE)? (t)G(x,8) : \
+ (test_mode == PCRE16_MODE)? (t)G(x,16) : (t)G(x,32))
+
+#define CODE_UNIT(a,b) ( \
+ (test_mode == PCRE8_MODE)? (uint32_t)(((PCRE2_SPTR8)(a))[b]) : \
+ (test_mode == PCRE16_MODE)? (uint32_t)(((PCRE2_SPTR16)(a))[b]) : \
+ (uint32_t)(((PCRE2_SPTR32)(a))[b]))
+
+#define CONCTXCPY(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ memcpy(G(a,8),G(b,8),sizeof(pcre2_convert_context_8)); \
+ else if (test_mode == PCRE16_MODE) \
+ memcpy(G(a,16),G(b,16),sizeof(pcre2_convert_context_16)); \
+ else memcpy(G(a,32),G(b,32),sizeof(pcre2_convert_context_32))
+
+#define CONVERT_COPY(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ memcpy(G(a,8),(char *)b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ memcpy(G(a,16),(char *)b,(c)*2); \
+ else if (test_mode == PCRE32_MODE) \
+ memcpy(G(a,32),(char *)b,(c)*4)
+
+#define DATCTXCPY(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ memcpy(G(a,8),G(b,8),sizeof(pcre2_match_context_8)); \
+ else if (test_mode == PCRE16_MODE) \
+ memcpy(G(a,16),G(b,16),sizeof(pcre2_match_context_16)); \
+ else memcpy(G(a,32),G(b,32),sizeof(pcre2_match_context_32))
+
+#define FLD(a,b) ((test_mode == PCRE8_MODE)? G(a,8)->b : \
+ (test_mode == PCRE16_MODE)? G(a,16)->b : G(a,32)->b)
+
+#define PATCTXCPY(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ memcpy(G(a,8),G(b,8),sizeof(pcre2_compile_context_8)); \
+ else if (test_mode == PCRE16_MODE) \
+ memcpy(G(a,16),G(b,16),sizeof(pcre2_compile_context_16)); \
+ else memcpy(G(a,32),G(b,32),sizeof(pcre2_compile_context_32))
+
+#define PCHARS(lv, p, offset, len, utf, f) \
+ if (test_mode == PCRE32_MODE) \
+ lv = pchars32((PCRE2_SPTR32)(p)+offset, len, utf, f); \
+ else if (test_mode == PCRE16_MODE) \
+ lv = pchars16((PCRE2_SPTR16)(p)+offset, len, utf, f); \
+ else \
+ lv = pchars8((PCRE2_SPTR8)(p)+offset, len, utf, f)
+
+#define PCHARSV(p, offset, len, utf, f) \
+ if (test_mode == PCRE32_MODE) \
+ (void)pchars32((PCRE2_SPTR32)(p)+offset, len, utf, f); \
+ else if (test_mode == PCRE16_MODE) \
+ (void)pchars16((PCRE2_SPTR16)(p)+offset, len, utf, f); \
+ else \
+ (void)pchars8((PCRE2_SPTR8)(p)+offset, len, utf, f)
+
+#define PCRE2_CALLOUT_ENUMERATE(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_callout_enumerate_8(compiled_code8, \
+ (int (*)(struct pcre2_callout_enumerate_block_8 *, void *))b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_callout_enumerate_16(compiled_code16, \
+ (int(*)(struct pcre2_callout_enumerate_block_16 *, void *))b,c); \
+ else \
+ a = pcre2_callout_enumerate_32(compiled_code32, \
+ (int (*)(struct pcre2_callout_enumerate_block_32 *, void *))b,c)
+
+#define PCRE2_CODE_COPY_FROM_VOID(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ G(a,8) = pcre2_code_copy_8(b); \
+ else if (test_mode == PCRE16_MODE) \
+ G(a,16) = pcre2_code_copy_16(b); \
+ else \
+ G(a,32) = pcre2_code_copy_32(b)
+
+#define PCRE2_CODE_COPY_TO_VOID(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ a = (void *)pcre2_code_copy_8(G(b,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = (void *)pcre2_code_copy_16(G(b,16)); \
+ else \
+ a = (void *)pcre2_code_copy_32(G(b,32))
+
+#define PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ a = (void *)pcre2_code_copy_with_tables_8(G(b,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = (void *)pcre2_code_copy_with_tables_16(G(b,16)); \
+ else \
+ a = (void *)pcre2_code_copy_with_tables_32(G(b,32))
+
+#define PCRE2_COMPILE(a,b,c,d,e,f,g) \
+ if (test_mode == PCRE8_MODE) \
+ G(a,8) = pcre2_compile_8(G(b,8),c,d,e,f,g); \
+ else if (test_mode == PCRE16_MODE) \
+ G(a,16) = pcre2_compile_16(G(b,16),c,d,e,f,g); \
+ else \
+ G(a,32) = pcre2_compile_32(G(b,32),c,d,e,f,g)
+
+#define PCRE2_CONVERTED_PATTERN_FREE(a) \
+ if (test_mode == PCRE8_MODE) pcre2_converted_pattern_free_8((PCRE2_UCHAR8 *)a); \
+ else if (test_mode == PCRE16_MODE) pcre2_converted_pattern_free_16((PCRE2_UCHAR16 *)a); \
+ else pcre2_converted_pattern_free_32((PCRE2_UCHAR32 *)a)
+
+#define PCRE2_DFA_MATCH(a,b,c,d,e,f,g,h,i,j) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_dfa_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h,i,j); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_dfa_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h,i,j); \
+ else \
+ a = pcre2_dfa_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h,i,j)
+
+#define PCRE2_GET_ERROR_MESSAGE(r,a,b) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_get_error_message_8(a,G(b,8),G(G(b,8),_size)); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_get_error_message_16(a,G(b,16),G(G(b,16),_size/2)); \
+ else \
+ r = pcre2_get_error_message_32(a,G(b,32),G(G(b,32),_size/4))
+
+#define PCRE2_GET_OVECTOR_COUNT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_get_ovector_count_8(G(b,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_get_ovector_count_16(G(b,16)); \
+ else \
+ a = pcre2_get_ovector_count_32(G(b,32))
+
+#define PCRE2_GET_STARTCHAR(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_get_startchar_8(G(b,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_get_startchar_16(G(b,16)); \
+ else \
+ a = pcre2_get_startchar_32(G(b,32))
+
+#define PCRE2_JIT_COMPILE(r,a,b) \
+ if (test_mode == PCRE8_MODE) r = pcre2_jit_compile_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) r = pcre2_jit_compile_16(G(a,16),b); \
+ else r = pcre2_jit_compile_32(G(a,32),b)
+
+#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) \
+ if (test_mode == PCRE8_MODE) pcre2_jit_free_unused_memory_8(G(a,8)); \
+ else if (test_mode == PCRE16_MODE) pcre2_jit_free_unused_memory_16(G(a,16)); \
+ else pcre2_jit_free_unused_memory_32(G(a,32))
+
+#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_jit_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_jit_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h); \
+ else \
+ a = pcre2_jit_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h)
+
+#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_8(b,c,d); \
+ else if (test_mode == PCRE16_MODE) \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_16(b,c,d); \
+ else \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_32(b,c,d);
+
+#define PCRE2_JIT_STACK_ASSIGN(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_jit_stack_assign_8(G(a,8),(pcre2_jit_callback_8)b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_jit_stack_assign_16(G(a,16),(pcre2_jit_callback_16)b,c); \
+ else \
+ pcre2_jit_stack_assign_32(G(a,32),(pcre2_jit_callback_32)b,c);
+
+#define PCRE2_JIT_STACK_FREE(a) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_jit_stack_free_8((pcre2_jit_stack_8 *)a); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_jit_stack_free_16((pcre2_jit_stack_16 *)a); \
+ else \
+ pcre2_jit_stack_free_32((pcre2_jit_stack_32 *)a);
+
+#define PCRE2_MAKETABLES(a) \
+ if (test_mode == PCRE8_MODE) a = pcre2_maketables_8(NULL); \
+ else if (test_mode == PCRE16_MODE) a = pcre2_maketables_16(NULL); \
+ else a = pcre2_maketables_32(NULL)
+
+#define PCRE2_MATCH(a,b,c,d,e,f,g,h) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h); \
+ else \
+ a = pcre2_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h)
+
+#define PCRE2_MATCH_DATA_CREATE(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ G(a,8) = pcre2_match_data_create_8(b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ G(a,16) = pcre2_match_data_create_16(b,c); \
+ else \
+ G(a,32) = pcre2_match_data_create_32(b,c)
+
+#define PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ G(a,8) = pcre2_match_data_create_from_pattern_8(G(b,8),c); \
+ else if (test_mode == PCRE16_MODE) \
+ G(a,16) = pcre2_match_data_create_from_pattern_16(G(b,16),c); \
+ else \
+ G(a,32) = pcre2_match_data_create_from_pattern_32(G(b,32),c)
+
+#define PCRE2_MATCH_DATA_FREE(a) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_match_data_free_8(G(a,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_match_data_free_16(G(a,16)); \
+ else \
+ pcre2_match_data_free_32(G(a,32))
+
+#define PCRE2_PATTERN_CONVERT(a,b,c,d,e,f,g) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_pattern_convert_8(G(b,8),c,d,(PCRE2_UCHAR8 **)e,f,G(g,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_pattern_convert_16(G(b,16),c,d,(PCRE2_UCHAR16 **)e,f,G(g,16)); \
+ else \
+ a = pcre2_pattern_convert_32(G(b,32),c,d,(PCRE2_UCHAR32 **)e,f,G(g,32))
+
+#define PCRE2_PATTERN_INFO(a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_pattern_info_8(G(b,8),c,d); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_pattern_info_16(G(b,16),c,d); \
+ else \
+ a = pcre2_pattern_info_32(G(b,32),c,d)
+
+#define PCRE2_PRINTINT(a) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_printint_8(compiled_code8,outfile,a); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_printint_16(compiled_code16,outfile,a); \
+ else \
+ pcre2_printint_32(compiled_code32,outfile,a)
+
+#define PCRE2_SERIALIZE_DECODE(r,a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_serialize_decode_8((pcre2_code_8 **)a,b,c,G(d,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_serialize_decode_16((pcre2_code_16 **)a,b,c,G(d,16)); \
+ else \
+ r = pcre2_serialize_decode_32((pcre2_code_32 **)a,b,c,G(d,32))
+
+#define PCRE2_SERIALIZE_ENCODE(r,a,b,c,d,e) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_serialize_encode_8((const pcre2_code_8 **)a,b,c,d,G(e,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_serialize_encode_16((const pcre2_code_16 **)a,b,c,d,G(e,16)); \
+ else \
+ r = pcre2_serialize_encode_32((const pcre2_code_32 **)a,b,c,d,G(e,32))
+
+#define PCRE2_SERIALIZE_FREE(a) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_serialize_free_8(a); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_serialize_free_16(a); \
+ else \
+ pcre2_serialize_free_32(a)
+
+#define PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(r,a) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_serialize_get_number_of_codes_8(a); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_serialize_get_number_of_codes_16(a); \
+ else \
+ r = pcre2_serialize_get_number_of_codes_32(a); \
+
+#define PCRE2_SET_CALLOUT(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *, void *))b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *, void *))b,c); \
+ else \
+ pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *, void *))b,c);
+
+#define PCRE2_SET_CHARACTER_TABLES(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_character_tables_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_character_tables_16(G(a,16),b); \
+ else \
+ pcre2_set_character_tables_32(G(a,32),b)
+
+#define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_compile_recursion_guard_8(G(a,8),b,c); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_compile_recursion_guard_16(G(a,16),b,c); \
+ else \
+ pcre2_set_compile_recursion_guard_32(G(a,32),b,c)
+
+#define PCRE2_SET_DEPTH_LIMIT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_depth_limit_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_depth_limit_16(G(a,16),b); \
+ else \
+ pcre2_set_depth_limit_32(G(a,32),b)
+
+#define PCRE2_SET_GLOB_SEPARATOR(r,a,b) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_set_glob_separator_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_set_glob_separator_16(G(a,16),b); \
+ else \
+ r = pcre2_set_glob_separator_32(G(a,32),b)
+
+#define PCRE2_SET_GLOB_ESCAPE(r,a,b) \
+ if (test_mode == PCRE8_MODE) \
+ r = pcre2_set_glob_escape_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ r = pcre2_set_glob_escape_16(G(a,16),b); \
+ else \
+ r = pcre2_set_glob_escape_32(G(a,32),b)
+
+#define PCRE2_SET_HEAP_LIMIT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_heap_limit_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_heap_limit_16(G(a,16),b); \
+ else \
+ pcre2_set_heap_limit_32(G(a,32),b)
+
+#define PCRE2_SET_MATCH_LIMIT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_match_limit_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_match_limit_16(G(a,16),b); \
+ else \
+ pcre2_set_match_limit_32(G(a,32),b)
+
+#define PCRE2_SET_MAX_PATTERN_LENGTH(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_max_pattern_length_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_max_pattern_length_16(G(a,16),b); \
+ else \
+ pcre2_set_max_pattern_length_32(G(a,32),b)
+
+#define PCRE2_SET_OFFSET_LIMIT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_offset_limit_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_offset_limit_16(G(a,16),b); \
+ else \
+ pcre2_set_offset_limit_32(G(a,32),b)
+
+#define PCRE2_SET_PARENS_NEST_LIMIT(a,b) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_set_parens_nest_limit_8(G(a,8),b); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_set_parens_nest_limit_16(G(a,16),b); \
+ else \
+ pcre2_set_parens_nest_limit_32(G(a,32),b)
+
+#define PCRE2_SUBSTITUTE(a,b,c,d,e,f,g,h,i,j,k,l) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substitute_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),G(h,8), \
+ (PCRE2_SPTR8)i,j,(PCRE2_UCHAR8 *)k,l); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substitute_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),G(h,16), \
+ (PCRE2_SPTR16)i,j,(PCRE2_UCHAR16 *)k,l); \
+ else \
+ a = pcre2_substitute_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),G(h,32), \
+ (PCRE2_SPTR32)i,j,(PCRE2_UCHAR32 *)k,l)
+
+#define PCRE2_SUBSTRING_COPY_BYNAME(a,b,c,d,e) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_copy_byname_8(G(b,8),G(c,8),(PCRE2_UCHAR8 *)d,e); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_copy_byname_16(G(b,16),G(c,16),(PCRE2_UCHAR16 *)d,e); \
+ else \
+ a = pcre2_substring_copy_byname_32(G(b,32),G(c,32),(PCRE2_UCHAR32 *)d,e)
+
+#define PCRE2_SUBSTRING_COPY_BYNUMBER(a,b,c,d,e) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_copy_bynumber_8(G(b,8),c,(PCRE2_UCHAR8 *)d,e); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_copy_bynumber_16(G(b,16),c,(PCRE2_UCHAR16 *)d,e); \
+ else \
+ a = pcre2_substring_copy_bynumber_32(G(b,32),c,(PCRE2_UCHAR32 *)d,e)
+
+#define PCRE2_SUBSTRING_FREE(a) \
+ if (test_mode == PCRE8_MODE) pcre2_substring_free_8((PCRE2_UCHAR8 *)a); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_substring_free_16((PCRE2_UCHAR16 *)a); \
+ else pcre2_substring_free_32((PCRE2_UCHAR32 *)a)
+
+#define PCRE2_SUBSTRING_GET_BYNAME(a,b,c,d,e) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_get_byname_8(G(b,8),G(c,8),(PCRE2_UCHAR8 **)d,e); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_get_byname_16(G(b,16),G(c,16),(PCRE2_UCHAR16 **)d,e); \
+ else \
+ a = pcre2_substring_get_byname_32(G(b,32),G(c,32),(PCRE2_UCHAR32 **)d,e)
+
+#define PCRE2_SUBSTRING_GET_BYNUMBER(a,b,c,d,e) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_get_bynumber_8(G(b,8),c,(PCRE2_UCHAR8 **)d,e); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_get_bynumber_16(G(b,16),c,(PCRE2_UCHAR16 **)d,e); \
+ else \
+ a = pcre2_substring_get_bynumber_32(G(b,32),c,(PCRE2_UCHAR32 **)d,e)
+
+#define PCRE2_SUBSTRING_LENGTH_BYNAME(a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_length_byname_8(G(b,8),G(c,8),d); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_length_byname_16(G(b,16),G(c,16),d); \
+ else \
+ a = pcre2_substring_length_byname_32(G(b,32),G(c,32),d)
+
+#define PCRE2_SUBSTRING_LENGTH_BYNUMBER(a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_length_bynumber_8(G(b,8),c,d); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_length_bynumber_16(G(b,16),c,d); \
+ else \
+ a = pcre2_substring_length_bynumber_32(G(b,32),c,d)
+
+#define PCRE2_SUBSTRING_LIST_GET(a,b,c,d) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_list_get_8(G(b,8),(PCRE2_UCHAR8 ***)c,d); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_list_get_16(G(b,16),(PCRE2_UCHAR16 ***)c,d); \
+ else \
+ a = pcre2_substring_list_get_32(G(b,32),(PCRE2_UCHAR32 ***)c,d)
+
+#define PCRE2_SUBSTRING_LIST_FREE(a) \
+ if (test_mode == PCRE8_MODE) \
+ pcre2_substring_list_free_8((PCRE2_SPTR8 *)a); \
+ else if (test_mode == PCRE16_MODE) \
+ pcre2_substring_list_free_16((PCRE2_SPTR16 *)a); \
+ else \
+ pcre2_substring_list_free_32((PCRE2_SPTR32 *)a)
+
+#define PCRE2_SUBSTRING_NUMBER_FROM_NAME(a,b,c) \
+ if (test_mode == PCRE8_MODE) \
+ a = pcre2_substring_number_from_name_8(G(b,8),G(c,8)); \
+ else if (test_mode == PCRE16_MODE) \
+ a = pcre2_substring_number_from_name_16(G(b,16),G(c,16)); \
+ else \
+ a = pcre2_substring_number_from_name_32(G(b,32),G(c,32))
+
+#define PTR(x) ( \
+ (test_mode == PCRE8_MODE)? (void *)G(x,8) : \
+ (test_mode == PCRE16_MODE)? (void *)G(x,16) : \
+ (void *)G(x,32))
+
+#define SETFLD(x,y,z) \
+ if (test_mode == PCRE8_MODE) G(x,8)->y = z; \
+ else if (test_mode == PCRE16_MODE) G(x,16)->y = z; \
+ else G(x,32)->y = z
+
+#define SETFLDVEC(x,y,v,z) \
+ if (test_mode == PCRE8_MODE) G(x,8)->y[v] = z; \
+ else if (test_mode == PCRE16_MODE) G(x,16)->y[v] = z; \
+ else G(x,32)->y[v] = z
+
+#define SETOP(x,y,z) \
+ if (test_mode == PCRE8_MODE) G(x,8) z y; \
+ else if (test_mode == PCRE16_MODE) G(x,16) z y; \
+ else G(x,32) z y
+
+#define SETCASTPTR(x,y) \
+ if (test_mode == PCRE8_MODE) \
+ G(x,8) = (uint8_t *)(y); \
+ else if (test_mode == PCRE16_MODE) \
+ G(x,16) = (uint16_t *)(y); \
+ else \
+ G(x,32) = (uint32_t *)(y)
+
+#define STRLEN(p) ((test_mode == PCRE8_MODE)? ((int)strlen((char *)p)) : \
+ (test_mode == PCRE16_MODE)? ((int)strlen16((PCRE2_SPTR16)p)) : \
+ ((int)strlen32((PCRE2_SPTR32)p)))
+
+#define SUB1(a,b) \
+ if (test_mode == PCRE8_MODE) G(a,8)(G(b,8)); \
+ else if (test_mode == PCRE16_MODE) G(a,16)(G(b,16)); \
+ else G(a,32)(G(b,32))
+
+#define SUB2(a,b,c) \
+ if (test_mode == PCRE8_MODE) G(a,8)(G(b,8),G(c,8)); \
+ else if (test_mode == PCRE16_MODE) G(a,16)(G(b,16),G(c,16)); \
+ else G(a,32)(G(b,32),G(c,32))
+
+#define TEST(x,r,y) ( \
+ (test_mode == PCRE8_MODE && G(x,8) r (y)) || \
+ (test_mode == PCRE16_MODE && G(x,16) r (y)) || \
+ (test_mode == PCRE32_MODE && G(x,32) r (y)))
+
+#define TESTFLD(x,f,r,y) ( \
+ (test_mode == PCRE8_MODE && G(x,8)->f r (y)) || \
+ (test_mode == PCRE16_MODE && G(x,16)->f r (y)) || \
+ (test_mode == PCRE32_MODE && G(x,32)->f r (y)))
+
+
+/* ----- Two out of three modes are supported ----- */
+
+#else
+
+/* We can use some macro trickery to make a single set of definitions work in
+the three different cases. */
+
+/* ----- 32-bit and 16-bit but not 8-bit supported ----- */
+
+#if defined(SUPPORT_PCRE2_32) && defined(SUPPORT_PCRE2_16)
+#define BITONE 32
+#define BITTWO 16
+
+/* ----- 32-bit and 8-bit but not 16-bit supported ----- */
+
+#elif defined(SUPPORT_PCRE2_32) && defined(SUPPORT_PCRE2_8)
+#define BITONE 32
+#define BITTWO 8
+
+/* ----- 16-bit and 8-bit but not 32-bit supported ----- */
+
+#else
+#define BITONE 16
+#define BITTWO 8
+#endif
+
+
+/* ----- Common macros for two-mode cases ----- */
+
+#define BYTEONE (BITONE/8)
+#define BYTETWO (BITTWO/8)
+
+#define CASTFLD(t,a,b) \
+ ((test_mode == G(G(PCRE,BITONE),_MODE))? (t)(G(a,BITONE)->b) : \
+ (t)(G(a,BITTWO)->b))
+
+#define CASTVAR(t,x) ( \
+ (test_mode == G(G(PCRE,BITONE),_MODE))? \
+ (t)G(x,BITONE) : (t)G(x,BITTWO))
+
+#define CODE_UNIT(a,b) ( \
+ (test_mode == G(G(PCRE,BITONE),_MODE))? \
+ (uint32_t)(((G(PCRE2_SPTR,BITONE))(a))[b]) : \
+ (uint32_t)(((G(PCRE2_SPTR,BITTWO))(a))[b]))
+
+#define CONCTXCPY(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ memcpy(G(a,BITONE),G(b,BITONE),sizeof(G(pcre2_convert_context_,BITONE))); \
+ else \
+ memcpy(G(a,BITTWO),G(b,BITTWO),sizeof(G(pcre2_convert_context_,BITTWO)))
+
+#define CONVERT_COPY(a,b,c) \
+ (test_mode == G(G(PCRE,BITONE),_MODE))? \
+ memcpy(G(a,BITONE),(char *)b,(c)*BYTEONE) : \
+ memcpy(G(a,BITTWO),(char *)b,(c)*BYTETWO)
+
+#define DATCTXCPY(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ memcpy(G(a,BITONE),G(b,BITONE),sizeof(G(pcre2_match_context_,BITONE))); \
+ else \
+ memcpy(G(a,BITTWO),G(b,BITTWO),sizeof(G(pcre2_match_context_,BITTWO)))
+
+#define FLD(a,b) \
+ ((test_mode == G(G(PCRE,BITONE),_MODE))? G(a,BITONE)->b : G(a,BITTWO)->b)
+
+#define PATCTXCPY(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ memcpy(G(a,BITONE),G(b,BITONE),sizeof(G(pcre2_compile_context_,BITONE))); \
+ else \
+ memcpy(G(a,BITTWO),G(b,BITTWO),sizeof(G(pcre2_compile_context_,BITTWO)))
+
+#define PCHARS(lv, p, offset, len, utf, f) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ lv = G(pchars,BITONE)((G(PCRE2_SPTR,BITONE))(p)+offset, len, utf, f); \
+ else \
+ lv = G(pchars,BITTWO)((G(PCRE2_SPTR,BITTWO))(p)+offset, len, utf, f)
+
+#define PCHARSV(p, offset, len, utf, f) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ (void)G(pchars,BITONE)((G(PCRE2_SPTR,BITONE))(p)+offset, len, utf, f); \
+ else \
+ (void)G(pchars,BITTWO)((G(PCRE2_SPTR,BITTWO))(p)+offset, len, utf, f)
+
+#define PCRE2_CALLOUT_ENUMERATE(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_callout_enumerate,BITONE)(G(compiled_code,BITONE), \
+ (int (*)(struct G(pcre2_callout_enumerate_block_,BITONE) *, void *))b,c); \
+ else \
+ a = G(pcre2_callout_enumerate,BITTWO)(G(compiled_code,BITTWO), \
+ (int (*)(struct G(pcre2_callout_enumerate_block_,BITTWO) *, void *))b,c)
+
+#define PCRE2_CODE_COPY_FROM_VOID(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE) = G(pcre2_code_copy_,BITONE)(b); \
+ else \
+ G(a,BITTWO) = G(pcre2_code_copy_,BITTWO)(b)
+
+#define PCRE2_CODE_COPY_TO_VOID(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = (void *)G(pcre2_code_copy_,BITONE)(G(b,BITONE)); \
+ else \
+ a = (void *)G(pcre2_code_copy_,BITTWO)(G(b,BITTWO))
+
+#define PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = (void *)G(pcre2_code_copy_with_tables_,BITONE)(G(b,BITONE)); \
+ else \
+ a = (void *)G(pcre2_code_copy_with_tables_,BITTWO)(G(b,BITTWO))
+
+#define PCRE2_COMPILE(a,b,c,d,e,f,g) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE) = G(pcre2_compile_,BITONE)(G(b,BITONE),c,d,e,f,g); \
+ else \
+ G(a,BITTWO) = G(pcre2_compile_,BITTWO)(G(b,BITTWO),c,d,e,f,g)
+
+#define PCRE2_CONVERTED_PATTERN_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_converted_pattern_free_,BITONE)((G(PCRE2_UCHAR,BITONE) *)a); \
+ else \
+ G(pcre2_converted_pattern_free_,BITTWO)((G(PCRE2_UCHAR,BITTWO) *)a)
+
+#define PCRE2_DFA_MATCH(a,b,c,d,e,f,g,h,i,j) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_dfa_match_,BITONE)(G(b,BITONE),(G(PCRE2_SPTR,BITONE))c,d,e,f, \
+ G(g,BITONE),h,i,j); \
+ else \
+ a = G(pcre2_dfa_match_,BITTWO)(G(b,BITTWO),(G(PCRE2_SPTR,BITTWO))c,d,e,f, \
+ G(g,BITTWO),h,i,j)
+
+#define PCRE2_GET_ERROR_MESSAGE(r,a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_get_error_message_,BITONE)(a,G(b,BITONE),G(G(b,BITONE),_size/BYTEONE)); \
+ else \
+ r = G(pcre2_get_error_message_,BITTWO)(a,G(b,BITTWO),G(G(b,BITTWO),_size/BYTETWO))
+
+#define PCRE2_GET_OVECTOR_COUNT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_get_ovector_count_,BITONE)(G(b,BITONE)); \
+ else \
+ a = G(pcre2_get_ovector_count_,BITTWO)(G(b,BITTWO))
+
+#define PCRE2_GET_STARTCHAR(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_get_startchar_,BITONE)(G(b,BITONE)); \
+ else \
+ a = G(pcre2_get_startchar_,BITTWO)(G(b,BITTWO))
+
+#define PCRE2_JIT_COMPILE(r,a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_jit_compile_,BITONE)(G(a,BITONE),b); \
+ else \
+ r = G(pcre2_jit_compile_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_jit_free_unused_memory_,BITONE)(G(a,BITONE)); \
+ else \
+ G(pcre2_jit_free_unused_memory_,BITTWO)(G(a,BITTWO))
+
+#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_jit_match_,BITONE)(G(b,BITONE),(G(PCRE2_SPTR,BITONE))c,d,e,f, \
+ G(g,BITONE),h); \
+ else \
+ a = G(pcre2_jit_match_,BITTWO)(G(b,BITTWO),(G(PCRE2_SPTR,BITTWO))c,d,e,f, \
+ G(g,BITTWO),h)
+
+#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = (PCRE2_JIT_STACK *)G(pcre2_jit_stack_create_,BITONE)(b,c,d); \
+ else \
+ a = (PCRE2_JIT_STACK *)G(pcre2_jit_stack_create_,BITTWO)(b,c,d); \
+
+#define PCRE2_JIT_STACK_ASSIGN(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_jit_stack_assign_,BITONE)(G(a,BITONE),(G(pcre2_jit_callback_,BITONE))b,c); \
+ else \
+ G(pcre2_jit_stack_assign_,BITTWO)(G(a,BITTWO),(G(pcre2_jit_callback_,BITTWO))b,c);
+
+#define PCRE2_JIT_STACK_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_jit_stack_free_,BITONE)((G(pcre2_jit_stack_,BITONE) *)a); \
+ else \
+ G(pcre2_jit_stack_free_,BITTWO)((G(pcre2_jit_stack_,BITTWO) *)a);
+
+#define PCRE2_MAKETABLES(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_maketables_,BITONE)(NULL); \
+ else \
+ a = G(pcre2_maketables_,BITTWO)(NULL)
+
+#define PCRE2_MATCH(a,b,c,d,e,f,g,h) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_match_,BITONE)(G(b,BITONE),(G(PCRE2_SPTR,BITONE))c,d,e,f, \
+ G(g,BITONE),h); \
+ else \
+ a = G(pcre2_match_,BITTWO)(G(b,BITTWO),(G(PCRE2_SPTR,BITTWO))c,d,e,f, \
+ G(g,BITTWO),h)
+
+#define PCRE2_MATCH_DATA_CREATE(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE) = G(pcre2_match_data_create_,BITONE)(b,c); \
+ else \
+ G(a,BITTWO) = G(pcre2_match_data_create_,BITTWO)(b,c)
+
+#define PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE) = G(pcre2_match_data_create_from_pattern_,BITONE)(G(b,BITONE),c); \
+ else \
+ G(a,BITTWO) = G(pcre2_match_data_create_from_pattern_,BITTWO)(G(b,BITTWO),c)
+
+#define PCRE2_MATCH_DATA_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_match_data_free_,BITONE)(G(a,BITONE)); \
+ else \
+ G(pcre2_match_data_free_,BITTWO)(G(a,BITTWO))
+
+#define PCRE2_PATTERN_CONVERT(a,b,c,d,e,f,g) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_pattern_convert_,BITONE)(G(b,BITONE),c,d,(G(PCRE2_UCHAR,BITONE) **)e,f,G(g,BITONE)); \
+ else \
+ a = G(pcre2_pattern_convert_,BITTWO)(G(b,BITTWO),c,d,(G(PCRE2_UCHAR,BITTWO) **)e,f,G(g,BITTWO))
+
+#define PCRE2_PATTERN_INFO(a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_pattern_info_,BITONE)(G(b,BITONE),c,d); \
+ else \
+ a = G(pcre2_pattern_info_,BITTWO)(G(b,BITTWO),c,d)
+
+#define PCRE2_PRINTINT(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_printint_,BITONE)(G(compiled_code,BITONE),outfile,a); \
+ else \
+ G(pcre2_printint_,BITTWO)(G(compiled_code,BITTWO),outfile,a)
+
+#define PCRE2_SERIALIZE_DECODE(r,a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_serialize_decode_,BITONE)((G(pcre2_code_,BITONE) **)a,b,c,G(d,BITONE)); \
+ else \
+ r = G(pcre2_serialize_decode_,BITTWO)((G(pcre2_code_,BITTWO) **)a,b,c,G(d,BITTWO))
+
+#define PCRE2_SERIALIZE_ENCODE(r,a,b,c,d,e) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_serialize_encode_,BITONE)((G(const pcre2_code_,BITONE) **)a,b,c,d,G(e,BITONE)); \
+ else \
+ r = G(pcre2_serialize_encode_,BITTWO)((G(const pcre2_code_,BITTWO) **)a,b,c,d,G(e,BITTWO))
+
+#define PCRE2_SERIALIZE_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_serialize_free_,BITONE)(a); \
+ else \
+ G(pcre2_serialize_free_,BITTWO)(a)
+
+#define PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(r,a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_serialize_get_number_of_codes_,BITONE)(a); \
+ else \
+ r = G(pcre2_serialize_get_number_of_codes_,BITTWO)(a)
+
+#define PCRE2_SET_CALLOUT(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_callout_,BITONE)(G(a,BITONE), \
+ (int (*)(G(pcre2_callout_block_,BITONE) *, void *))b,c); \
+ else \
+ G(pcre2_set_callout_,BITTWO)(G(a,BITTWO), \
+ (int (*)(G(pcre2_callout_block_,BITTWO) *, void *))b,c);
+
+#define PCRE2_SET_CHARACTER_TABLES(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_character_tables_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_character_tables_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_compile_recursion_guard_,BITONE)(G(a,BITONE),b,c); \
+ else \
+ G(pcre2_set_compile_recursion_guard_,BITTWO)(G(a,BITTWO),b,c)
+
+#define PCRE2_SET_DEPTH_LIMIT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_depth_limit_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_depth_limit_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_GLOB_ESCAPE(r,a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_set_glob_escape_,BITONE)(G(a,BITONE),b); \
+ else \
+ r = G(pcre2_set_glob_escape_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_GLOB_SEPARATOR(r,a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ r = G(pcre2_set_glob_separator_,BITONE)(G(a,BITONE),b); \
+ else \
+ r = G(pcre2_set_glob_separator_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_HEAP_LIMIT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_heap_limit_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_heap_limit_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_MATCH_LIMIT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_match_limit_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_match_limit_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_MAX_PATTERN_LENGTH(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_max_pattern_length_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_max_pattern_length_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_OFFSET_LIMIT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_offset_limit_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_offset_limit_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SET_PARENS_NEST_LIMIT(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_set_parens_nest_limit_,BITONE)(G(a,BITONE),b); \
+ else \
+ G(pcre2_set_parens_nest_limit_,BITTWO)(G(a,BITTWO),b)
+
+#define PCRE2_SUBSTITUTE(a,b,c,d,e,f,g,h,i,j,k,l) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substitute_,BITONE)(G(b,BITONE),(G(PCRE2_SPTR,BITONE))c,d,e,f, \
+ G(g,BITONE),G(h,BITONE),(G(PCRE2_SPTR,BITONE))i,j, \
+ (G(PCRE2_UCHAR,BITONE) *)k,l); \
+ else \
+ a = G(pcre2_substitute_,BITTWO)(G(b,BITTWO),(G(PCRE2_SPTR,BITTWO))c,d,e,f, \
+ G(g,BITTWO),G(h,BITTWO),(G(PCRE2_SPTR,BITTWO))i,j, \
+ (G(PCRE2_UCHAR,BITTWO) *)k,l)
+
+#define PCRE2_SUBSTRING_COPY_BYNAME(a,b,c,d,e) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_copy_byname_,BITONE)(G(b,BITONE),G(c,BITONE),\
+ (G(PCRE2_UCHAR,BITONE) *)d,e); \
+ else \
+ a = G(pcre2_substring_copy_byname_,BITTWO)(G(b,BITTWO),G(c,BITTWO),\
+ (G(PCRE2_UCHAR,BITTWO) *)d,e)
+
+#define PCRE2_SUBSTRING_COPY_BYNUMBER(a,b,c,d,e) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_copy_bynumber_,BITONE)(G(b,BITONE),c,\
+ (G(PCRE2_UCHAR,BITONE) *)d,e); \
+ else \
+ a = G(pcre2_substring_copy_bynumber_,BITTWO)(G(b,BITTWO),c,\
+ (G(PCRE2_UCHAR,BITTWO) *)d,e)
+
+#define PCRE2_SUBSTRING_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_substring_free_,BITONE)((G(PCRE2_UCHAR,BITONE) *)a); \
+ else G(pcre2_substring_free_,BITTWO)((G(PCRE2_UCHAR,BITTWO) *)a)
+
+#define PCRE2_SUBSTRING_GET_BYNAME(a,b,c,d,e) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_get_byname_,BITONE)(G(b,BITONE),G(c,BITONE),\
+ (G(PCRE2_UCHAR,BITONE) **)d,e); \
+ else \
+ a = G(pcre2_substring_get_byname_,BITTWO)(G(b,BITTWO),G(c,BITTWO),\
+ (G(PCRE2_UCHAR,BITTWO) **)d,e)
+
+#define PCRE2_SUBSTRING_GET_BYNUMBER(a,b,c,d,e) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_get_bynumber_,BITONE)(G(b,BITONE),c,\
+ (G(PCRE2_UCHAR,BITONE) **)d,e); \
+ else \
+ a = G(pcre2_substring_get_bynumber_,BITTWO)(G(b,BITTWO),c,\
+ (G(PCRE2_UCHAR,BITTWO) **)d,e)
+
+#define PCRE2_SUBSTRING_LENGTH_BYNAME(a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_length_byname_,BITONE)(G(b,BITONE),G(c,BITONE),d); \
+ else \
+ a = G(pcre2_substring_length_byname_,BITTWO)(G(b,BITTWO),G(c,BITTWO),d)
+
+#define PCRE2_SUBSTRING_LENGTH_BYNUMBER(a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_length_bynumber_,BITONE)(G(b,BITONE),c,d); \
+ else \
+ a = G(pcre2_substring_length_bynumber_,BITTWO)(G(b,BITTWO),c,d)
+
+#define PCRE2_SUBSTRING_LIST_GET(a,b,c,d) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_list_get_,BITONE)(G(b,BITONE), \
+ (G(PCRE2_UCHAR,BITONE) ***)c,d); \
+ else \
+ a = G(pcre2_substring_list_get_,BITTWO)(G(b,BITTWO), \
+ (G(PCRE2_UCHAR,BITTWO) ***)c,d)
+
+#define PCRE2_SUBSTRING_LIST_FREE(a) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(pcre2_substring_list_free_,BITONE)((G(PCRE2_SPTR,BITONE) *)a); \
+ else \
+ G(pcre2_substring_list_free_,BITTWO)((G(PCRE2_SPTR,BITTWO) *)a)
+
+#define PCRE2_SUBSTRING_NUMBER_FROM_NAME(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ a = G(pcre2_substring_number_from_name_,BITONE)(G(b,BITONE),G(c,BITONE)); \
+ else \
+ a = G(pcre2_substring_number_from_name_,BITTWO)(G(b,BITTWO),G(c,BITTWO))
+
+#define PTR(x) ( \
+ (test_mode == G(G(PCRE,BITONE),_MODE))? (void *)G(x,BITONE) : \
+ (void *)G(x,BITTWO))
+
+#define SETFLD(x,y,z) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) G(x,BITONE)->y = z; \
+ else G(x,BITTWO)->y = z
+
+#define SETFLDVEC(x,y,v,z) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) G(x,BITONE)->y[v] = z; \
+ else G(x,BITTWO)->y[v] = z
+
+#define SETOP(x,y,z) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) G(x,BITONE) z y; \
+ else G(x,BITTWO) z y
+
+#define SETCASTPTR(x,y) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(x,BITONE) = (G(G(uint,BITONE),_t) *)(y); \
+ else \
+ G(x,BITTWO) = (G(G(uint,BITTWO),_t) *)(y)
+
+#define STRLEN(p) ((test_mode == G(G(PCRE,BITONE),_MODE))? \
+ G(strlen,BITONE)((G(PCRE2_SPTR,BITONE))p) : \
+ G(strlen,BITTWO)((G(PCRE2_SPTR,BITTWO))p))
+
+#define SUB1(a,b) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE)(G(b,BITONE)); \
+ else \
+ G(a,BITTWO)(G(b,BITTWO))
+
+#define SUB2(a,b,c) \
+ if (test_mode == G(G(PCRE,BITONE),_MODE)) \
+ G(a,BITONE))(G(b,BITONE),G(c,BITONE)); \
+ else \
+ G(a,BITTWO))(G(b,BITTWO),G(c,BITTWO))
+
+#define TEST(x,r,y) ( \
+ (test_mode == G(G(PCRE,BITONE),_MODE) && G(x,BITONE) r (y)) || \
+ (test_mode == G(G(PCRE,BITTWO),_MODE) && G(x,BITTWO) r (y)))
+
+#define TESTFLD(x,f,r,y) ( \
+ (test_mode == G(G(PCRE,BITONE),_MODE) && G(x,BITONE)->f r (y)) || \
+ (test_mode == G(G(PCRE,BITTWO),_MODE) && G(x,BITTWO)->f r (y)))
+
+
+#endif /* Two out of three modes */
+
+/* ----- End of cases where more than one mode is supported ----- */
+
+
+/* ----- Only 8-bit mode is supported ----- */
+
+#elif defined SUPPORT_PCRE2_8
+#define CASTFLD(t,a,b) (t)(G(a,8)->b)
+#define CASTVAR(t,x) (t)G(x,8)
+#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR8)(a))[b])
+#define CONCTXCPY(a,b) memcpy(G(a,8),G(b,8),sizeof(pcre2_convert_context_8))
+#define CONVERT_COPY(a,b,c) memcpy(G(a,8),(char *)b, c)
+#define DATCTXCPY(a,b) memcpy(G(a,8),G(b,8),sizeof(pcre2_match_context_8))
+#define FLD(a,b) G(a,8)->b
+#define PATCTXCPY(a,b) memcpy(G(a,8),G(b,8),sizeof(pcre2_compile_context_8))
+#define PCHARS(lv, p, offset, len, utf, f) \
+ lv = pchars8((PCRE2_SPTR8)(p)+offset, len, utf, f)
+#define PCHARSV(p, offset, len, utf, f) \
+ (void)pchars8((PCRE2_SPTR8)(p)+offset, len, utf, f)
+#define PCRE2_CALLOUT_ENUMERATE(a,b,c) \
+ a = pcre2_callout_enumerate_8(compiled_code8, \
+ (int (*)(struct pcre2_callout_enumerate_block_8 *, void *))b,c)
+#define PCRE2_CODE_COPY_FROM_VOID(a,b) G(a,8) = pcre2_code_copy_8(b)
+#define PCRE2_CODE_COPY_TO_VOID(a,b) a = (void *)pcre2_code_copy_8(G(b,8))
+#define PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(a,b) a = (void *)pcre2_code_copy_with_tables_8(G(b,8))
+#define PCRE2_COMPILE(a,b,c,d,e,f,g) \
+ G(a,8) = pcre2_compile_8(G(b,8),c,d,e,f,g)
+#define PCRE2_CONVERTED_PATTERN_FREE(a) \
+ pcre2_converted_pattern_free_8((PCRE2_UCHAR8 *)a)
+#define PCRE2_DFA_MATCH(a,b,c,d,e,f,g,h,i,j) \
+ a = pcre2_dfa_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h,i,j)
+#define PCRE2_GET_ERROR_MESSAGE(r,a,b) \
+ r = pcre2_get_error_message_8(a,G(b,8),G(G(b,8),_size))
+#define PCRE2_GET_OVECTOR_COUNT(a,b) a = pcre2_get_ovector_count_8(G(b,8))
+#define PCRE2_GET_STARTCHAR(a,b) a = pcre2_get_startchar_8(G(b,8))
+#define PCRE2_JIT_COMPILE(r,a,b) r = pcre2_jit_compile_8(G(a,8),b)
+#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) pcre2_jit_free_unused_memory_8(G(a,8))
+#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_jit_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h)
+#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_8(b,c,d);
+#define PCRE2_JIT_STACK_ASSIGN(a,b,c) \
+ pcre2_jit_stack_assign_8(G(a,8),(pcre2_jit_callback_8)b,c);
+#define PCRE2_JIT_STACK_FREE(a) pcre2_jit_stack_free_8((pcre2_jit_stack_8 *)a);
+#define PCRE2_MAKETABLES(a) a = pcre2_maketables_8(NULL)
+#define PCRE2_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_match_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),h)
+#define PCRE2_MATCH_DATA_CREATE(a,b,c) G(a,8) = pcre2_match_data_create_8(b,c)
+#define PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(a,b,c) \
+ G(a,8) = pcre2_match_data_create_from_pattern_8(G(b,8),c)
+#define PCRE2_MATCH_DATA_FREE(a) pcre2_match_data_free_8(G(a,8))
+#define PCRE2_PATTERN_CONVERT(a,b,c,d,e,f,g) a = pcre2_pattern_convert_8(G(b,8),c,d,(PCRE2_UCHAR8 **)e,f,G(g,8))
+#define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_8(G(b,8),c,d)
+#define PCRE2_PRINTINT(a) pcre2_printint_8(compiled_code8,outfile,a)
+#define PCRE2_SERIALIZE_DECODE(r,a,b,c,d) \
+ r = pcre2_serialize_decode_8((pcre2_code_8 **)a,b,c,G(d,8))
+#define PCRE2_SERIALIZE_ENCODE(r,a,b,c,d,e) \
+ r = pcre2_serialize_encode_8((const pcre2_code_8 **)a,b,c,d,G(e,8))
+#define PCRE2_SERIALIZE_FREE(a) pcre2_serialize_free_8(a)
+#define PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(r,a) \
+ r = pcre2_serialize_get_number_of_codes_8(a)
+#define PCRE2_SET_CALLOUT(a,b,c) \
+ pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *, void *))b,c)
+#define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_8(G(a,8),b)
+#define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b,c) \
+ pcre2_set_compile_recursion_guard_8(G(a,8),b,c)
+#define PCRE2_SET_DEPTH_LIMIT(a,b) pcre2_set_depth_limit_8(G(a,8),b)
+#define PCRE2_SET_GLOB_ESCAPE(r,a,b) r = pcre2_set_glob_escape_8(G(a,8),b)
+#define PCRE2_SET_GLOB_SEPARATOR(r,a,b) r = pcre2_set_glob_separator_8(G(a,8),b)
+#define PCRE2_SET_HEAP_LIMIT(a,b) pcre2_set_heap_limit_8(G(a,8),b)
+#define PCRE2_SET_MATCH_LIMIT(a,b) pcre2_set_match_limit_8(G(a,8),b)
+#define PCRE2_SET_MAX_PATTERN_LENGTH(a,b) pcre2_set_max_pattern_length_8(G(a,8),b)
+#define PCRE2_SET_OFFSET_LIMIT(a,b) pcre2_set_offset_limit_8(G(a,8),b)
+#define PCRE2_SET_PARENS_NEST_LIMIT(a,b) pcre2_set_parens_nest_limit_8(G(a,8),b)
+#define PCRE2_SUBSTITUTE(a,b,c,d,e,f,g,h,i,j,k,l) \
+ a = pcre2_substitute_8(G(b,8),(PCRE2_SPTR8)c,d,e,f,G(g,8),G(h,8), \
+ (PCRE2_SPTR8)i,j,(PCRE2_UCHAR8 *)k,l)
+#define PCRE2_SUBSTRING_COPY_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_copy_byname_8(G(b,8),G(c,8),(PCRE2_UCHAR8 *)d,e)
+#define PCRE2_SUBSTRING_COPY_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_copy_bynumber_8(G(b,8),c,(PCRE2_UCHAR8 *)d,e)
+#define PCRE2_SUBSTRING_FREE(a) pcre2_substring_free_8((PCRE2_UCHAR8 *)a)
+#define PCRE2_SUBSTRING_GET_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_get_byname_8(G(b,8),G(c,8),(PCRE2_UCHAR8 **)d,e)
+#define PCRE2_SUBSTRING_GET_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_get_bynumber_8(G(b,8),c,(PCRE2_UCHAR8 **)d,e)
+#define PCRE2_SUBSTRING_LENGTH_BYNAME(a,b,c,d) \
+ a = pcre2_substring_length_byname_8(G(b,8),G(c,8),d)
+#define PCRE2_SUBSTRING_LENGTH_BYNUMBER(a,b,c,d) \
+ a = pcre2_substring_length_bynumber_8(G(b,8),c,d)
+#define PCRE2_SUBSTRING_LIST_GET(a,b,c,d) \
+ a = pcre2_substring_list_get_8(G(b,8),(PCRE2_UCHAR8 ***)c,d)
+#define PCRE2_SUBSTRING_LIST_FREE(a) \
+ pcre2_substring_list_free_8((PCRE2_SPTR8 *)a)
+#define PCRE2_SUBSTRING_NUMBER_FROM_NAME(a,b,c) \
+ a = pcre2_substring_number_from_name_8(G(b,8),G(c,8));
+#define PTR(x) (void *)G(x,8)
+#define SETFLD(x,y,z) G(x,8)->y = z
+#define SETFLDVEC(x,y,v,z) G(x,8)->y[v] = z
+#define SETOP(x,y,z) G(x,8) z y
+#define SETCASTPTR(x,y) G(x,8) = (uint8_t *)(y)
+#define STRLEN(p) (int)strlen((char *)p)
+#define SUB1(a,b) G(a,8)(G(b,8))
+#define SUB2(a,b,c) G(a,8)(G(b,8),G(c,8))
+#define TEST(x,r,y) (G(x,8) r (y))
+#define TESTFLD(x,f,r,y) (G(x,8)->f r (y))
+
+
+/* ----- Only 16-bit mode is supported ----- */
+
+#elif defined SUPPORT_PCRE2_16
+#define CASTFLD(t,a,b) (t)(G(a,16)->b)
+#define CASTVAR(t,x) (t)G(x,16)
+#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR16)(a))[b])
+#define CONCTXCPY(a,b) memcpy(G(a,16),G(b,16),sizeof(pcre2_convert_context_16))
+#define CONVERT_COPY(a,b,c) memcpy(G(a,16),(char *)b, (c)*2)
+#define DATCTXCPY(a,b) memcpy(G(a,16),G(b,16),sizeof(pcre2_match_context_16))
+#define FLD(a,b) G(a,16)->b
+#define PATCTXCPY(a,b) memcpy(G(a,16),G(b,16),sizeof(pcre2_compile_context_16))
+#define PCHARS(lv, p, offset, len, utf, f) \
+ lv = pchars16((PCRE2_SPTR16)(p)+offset, len, utf, f)
+#define PCHARSV(p, offset, len, utf, f) \
+ (void)pchars16((PCRE2_SPTR16)(p)+offset, len, utf, f)
+#define PCRE2_CALLOUT_ENUMERATE(a,b,c) \
+ a = pcre2_callout_enumerate_16(compiled_code16, \
+ (int (*)(struct pcre2_callout_enumerate_block_16 *, void *))b,c)
+#define PCRE2_CODE_COPY_FROM_VOID(a,b) G(a,16) = pcre2_code_copy_16(b)
+#define PCRE2_CODE_COPY_TO_VOID(a,b) a = (void *)pcre2_code_copy_16(G(b,16))
+#define PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(a,b) a = (void *)pcre2_code_copy_with_tables_16(G(b,16))
+#define PCRE2_COMPILE(a,b,c,d,e,f,g) \
+ G(a,16) = pcre2_compile_16(G(b,16),c,d,e,f,g)
+#define PCRE2_CONVERTED_PATTERN_FREE(a) \
+ pcre2_converted_pattern_free_16((PCRE2_UCHAR16 *)a)
+#define PCRE2_DFA_MATCH(a,b,c,d,e,f,g,h,i,j) \
+ a = pcre2_dfa_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h,i,j)
+#define PCRE2_GET_ERROR_MESSAGE(r,a,b) \
+ r = pcre2_get_error_message_16(a,G(b,16),G(G(b,16),_size/2))
+#define PCRE2_GET_OVECTOR_COUNT(a,b) a = pcre2_get_ovector_count_16(G(b,16))
+#define PCRE2_GET_STARTCHAR(a,b) a = pcre2_get_startchar_16(G(b,16))
+#define PCRE2_JIT_COMPILE(r,a,b) r = pcre2_jit_compile_16(G(a,16),b)
+#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) pcre2_jit_free_unused_memory_16(G(a,16))
+#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_jit_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h)
+#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_16(b,c,d);
+#define PCRE2_JIT_STACK_ASSIGN(a,b,c) \
+ pcre2_jit_stack_assign_16(G(a,16),(pcre2_jit_callback_16)b,c);
+#define PCRE2_JIT_STACK_FREE(a) pcre2_jit_stack_free_16((pcre2_jit_stack_16 *)a);
+#define PCRE2_MAKETABLES(a) a = pcre2_maketables_16(NULL)
+#define PCRE2_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),h)
+#define PCRE2_MATCH_DATA_CREATE(a,b,c) G(a,16) = pcre2_match_data_create_16(b,c)
+#define PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(a,b,c) \
+ G(a,16) = pcre2_match_data_create_from_pattern_16(G(b,16),c)
+#define PCRE2_MATCH_DATA_FREE(a) pcre2_match_data_free_16(G(a,16))
+#define PCRE2_PATTERN_CONVERT(a,b,c,d,e,f,g) a = pcre2_pattern_convert_16(G(b,16),c,d,(PCRE2_UCHAR16 **)e,f,G(g,16))
+#define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_16(G(b,16),c,d)
+#define PCRE2_PRINTINT(a) pcre2_printint_16(compiled_code16,outfile,a)
+#define PCRE2_SERIALIZE_DECODE(r,a,b,c,d) \
+ r = pcre2_serialize_decode_16((pcre2_code_16 **)a,b,c,G(d,16))
+#define PCRE2_SERIALIZE_ENCODE(r,a,b,c,d,e) \
+ r = pcre2_serialize_encode_16((const pcre2_code_16 **)a,b,c,d,G(e,16))
+#define PCRE2_SERIALIZE_FREE(a) pcre2_serialize_free_16(a)
+#define PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(r,a) \
+ r = pcre2_serialize_get_number_of_codes_16(a)
+#define PCRE2_SET_CALLOUT(a,b,c) \
+ pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *, void *))b,c);
+#define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_16(G(a,16),b)
+#define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b,c) \
+ pcre2_set_compile_recursion_guard_16(G(a,16),b,c)
+#define PCRE2_SET_DEPTH_LIMIT(a,b) pcre2_set_depth_limit_16(G(a,16),b)
+#define PCRE2_SET_GLOB_ESCAPE(r,a,b) r = pcre2_set_glob_escape_16(G(a,16),b)
+#define PCRE2_SET_GLOB_SEPARATOR(r,a,b) r = pcre2_set_glob_separator_16(G(a,16),b)
+#define PCRE2_SET_HEAP_LIMIT(a,b) pcre2_set_heap_limit_16(G(a,16),b)
+#define PCRE2_SET_MATCH_LIMIT(a,b) pcre2_set_match_limit_16(G(a,16),b)
+#define PCRE2_SET_MAX_PATTERN_LENGTH(a,b) pcre2_set_max_pattern_length_16(G(a,16),b)
+#define PCRE2_SET_OFFSET_LIMIT(a,b) pcre2_set_offset_limit_16(G(a,16),b)
+#define PCRE2_SET_PARENS_NEST_LIMIT(a,b) pcre2_set_parens_nest_limit_16(G(a,16),b)
+#define PCRE2_SUBSTITUTE(a,b,c,d,e,f,g,h,i,j,k,l) \
+ a = pcre2_substitute_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),G(h,16), \
+ (PCRE2_SPTR16)i,j,(PCRE2_UCHAR16 *)k,l)
+#define PCRE2_SUBSTRING_COPY_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_copy_byname_16(G(b,16),G(c,16),(PCRE2_UCHAR16 *)d,e)
+#define PCRE2_SUBSTRING_COPY_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_copy_bynumber_16(G(b,16),c,(PCRE2_UCHAR16 *)d,e)
+#define PCRE2_SUBSTRING_FREE(a) pcre2_substring_free_16((PCRE2_UCHAR16 *)a)
+#define PCRE2_SUBSTRING_GET_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_get_byname_16(G(b,16),G(c,16),(PCRE2_UCHAR16 **)d,e)
+#define PCRE2_SUBSTRING_GET_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_get_bynumber_16(G(b,16),c,(PCRE2_UCHAR16 **)d,e)
+#define PCRE2_SUBSTRING_LENGTH_BYNAME(a,b,c,d) \
+ a = pcre2_substring_length_byname_16(G(b,16),G(c,16),d)
+#define PCRE2_SUBSTRING_LENGTH_BYNUMBER(a,b,c,d) \
+ a = pcre2_substring_length_bynumber_16(G(b,16),c,d)
+#define PCRE2_SUBSTRING_LIST_GET(a,b,c,d) \
+ a = pcre2_substring_list_get_16(G(b,16),(PCRE2_UCHAR16 ***)c,d)
+#define PCRE2_SUBSTRING_LIST_FREE(a) \
+ pcre2_substring_list_free_16((PCRE2_SPTR16 *)a)
+#define PCRE2_SUBSTRING_NUMBER_FROM_NAME(a,b,c) \
+ a = pcre2_substring_number_from_name_16(G(b,16),G(c,16));
+#define PTR(x) (void *)G(x,16)
+#define SETFLD(x,y,z) G(x,16)->y = z
+#define SETFLDVEC(x,y,v,z) G(x,16)->y[v] = z
+#define SETOP(x,y,z) G(x,16) z y
+#define SETCASTPTR(x,y) G(x,16) = (uint16_t *)(y)
+#define STRLEN(p) (int)strlen16((PCRE2_SPTR16)p)
+#define SUB1(a,b) G(a,16)(G(b,16))
+#define SUB2(a,b,c) G(a,16)(G(b,16),G(c,16))
+#define TEST(x,r,y) (G(x,16) r (y))
+#define TESTFLD(x,f,r,y) (G(x,16)->f r (y))
+
+
+/* ----- Only 32-bit mode is supported ----- */
+
+#elif defined SUPPORT_PCRE2_32
+#define CASTFLD(t,a,b) (t)(G(a,32)->b)
+#define CASTVAR(t,x) (t)G(x,32)
+#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR32)(a))[b])
+#define CONCTXCPY(a,b) memcpy(G(a,32),G(b,32),sizeof(pcre2_convert_context_32))
+#define CONVERT_COPY(a,b,c) memcpy(G(a,32),(char *)b, (c)*4)
+#define DATCTXCPY(a,b) memcpy(G(a,32),G(b,32),sizeof(pcre2_match_context_32))
+#define FLD(a,b) G(a,32)->b
+#define PATCTXCPY(a,b) memcpy(G(a,32),G(b,32),sizeof(pcre2_compile_context_32))
+#define PCHARS(lv, p, offset, len, utf, f) \
+ lv = pchars32((PCRE2_SPTR32)(p)+offset, len, utf, f)
+#define PCHARSV(p, offset, len, utf, f) \
+ (void)pchars32((PCRE2_SPTR32)(p)+offset, len, utf, f)
+#define PCRE2_CALLOUT_ENUMERATE(a,b,c) \
+ a = pcre2_callout_enumerate_32(compiled_code32, \
+ (int (*)(struct pcre2_callout_enumerate_block_32 *, void *))b,c)
+#define PCRE2_CODE_COPY_FROM_VOID(a,b) G(a,32) = pcre2_code_copy_32(b)
+#define PCRE2_CODE_COPY_TO_VOID(a,b) a = (void *)pcre2_code_copy_32(G(b,32))
+#define PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(a,b) a = (void *)pcre2_code_copy_with_tables_32(G(b,32))
+#define PCRE2_COMPILE(a,b,c,d,e,f,g) \
+ G(a,32) = pcre2_compile_32(G(b,32),c,d,e,f,g)
+#define PCRE2_CONVERTED_PATTERN_FREE(a) \
+ pcre2_converted_pattern_free_32((PCRE2_UCHAR32 *)a)
+#define PCRE2_DFA_MATCH(a,b,c,d,e,f,g,h,i,j) \
+ a = pcre2_dfa_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h,i,j)
+#define PCRE2_GET_ERROR_MESSAGE(r,a,b) \
+ r = pcre2_get_error_message_32(a,G(b,32),G(G(b,32),_size/4))
+#define PCRE2_GET_OVECTOR_COUNT(a,b) a = pcre2_get_ovector_count_32(G(b,32))
+#define PCRE2_GET_STARTCHAR(a,b) a = pcre2_get_startchar_32(G(b,32))
+#define PCRE2_JIT_COMPILE(r,a,b) r = pcre2_jit_compile_32(G(a,32),b)
+#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) pcre2_jit_free_unused_memory_32(G(a,32))
+#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_jit_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h)
+#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
+ a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_32(b,c,d);
+#define PCRE2_JIT_STACK_ASSIGN(a,b,c) \
+ pcre2_jit_stack_assign_32(G(a,32),(pcre2_jit_callback_32)b,c);
+#define PCRE2_JIT_STACK_FREE(a) pcre2_jit_stack_free_32((pcre2_jit_stack_32 *)a);
+#define PCRE2_MAKETABLES(a) a = pcre2_maketables_32(NULL)
+#define PCRE2_MATCH(a,b,c,d,e,f,g,h) \
+ a = pcre2_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),h)
+#define PCRE2_MATCH_DATA_CREATE(a,b,c) G(a,32) = pcre2_match_data_create_32(b,c)
+#define PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(a,b,c) \
+ G(a,32) = pcre2_match_data_create_from_pattern_32(G(b,32),c)
+#define PCRE2_MATCH_DATA_FREE(a) pcre2_match_data_free_32(G(a,32))
+#define PCRE2_PATTERN_CONVERT(a,b,c,d,e,f,g) a = pcre2_pattern_convert_32(G(b,32),c,d,(PCRE2_UCHAR32 **)e,f,G(g,32))
+#define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_32(G(b,32),c,d)
+#define PCRE2_PRINTINT(a) pcre2_printint_32(compiled_code32,outfile,a)
+#define PCRE2_SERIALIZE_DECODE(r,a,b,c,d) \
+ r = pcre2_serialize_decode_32((pcre2_code_32 **)a,b,c,G(d,32))
+#define PCRE2_SERIALIZE_ENCODE(r,a,b,c,d,e) \
+ r = pcre2_serialize_encode_32((const pcre2_code_32 **)a,b,c,d,G(e,32))
+#define PCRE2_SERIALIZE_FREE(a) pcre2_serialize_free_32(a)
+#define PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(r,a) \
+ r = pcre2_serialize_get_number_of_codes_32(a)
+#define PCRE2_SET_CALLOUT(a,b,c) \
+ pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *, void *))b,c);
+#define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_32(G(a,32),b)
+#define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b,c) \
+ pcre2_set_compile_recursion_guard_32(G(a,32),b,c)
+#define PCRE2_SET_DEPTH_LIMIT(a,b) pcre2_set_depth_limit_32(G(a,32),b)
+#define PCRE2_SET_GLOB_ESCAPE(r,a,b) r = pcre2_set_glob_escape_32(G(a,32),b)
+#define PCRE2_SET_GLOB_SEPARATOR(r,a,b) r = pcre2_set_glob_separator_32(G(a,32),b)
+#define PCRE2_SET_HEAP_LIMIT(a,b) pcre2_set_heap_limit_32(G(a,32),b)
+#define PCRE2_SET_MATCH_LIMIT(a,b) pcre2_set_match_limit_32(G(a,32),b)
+#define PCRE2_SET_MAX_PATTERN_LENGTH(a,b) pcre2_set_max_pattern_length_32(G(a,32),b)
+#define PCRE2_SET_OFFSET_LIMIT(a,b) pcre2_set_offset_limit_32(G(a,32),b)
+#define PCRE2_SET_PARENS_NEST_LIMIT(a,b) pcre2_set_parens_nest_limit_32(G(a,32),b)
+#define PCRE2_SUBSTITUTE(a,b,c,d,e,f,g,h,i,j,k,l) \
+ a = pcre2_substitute_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),G(h,32), \
+ (PCRE2_SPTR32)i,j,(PCRE2_UCHAR32 *)k,l)
+#define PCRE2_SUBSTRING_COPY_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_copy_byname_32(G(b,32),G(c,32),(PCRE2_UCHAR32 *)d,e)
+#define PCRE2_SUBSTRING_COPY_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_copy_bynumber_32(G(b,32),c,(PCRE2_UCHAR32 *)d,e);
+#define PCRE2_SUBSTRING_FREE(a) pcre2_substring_free_32((PCRE2_UCHAR32 *)a)
+#define PCRE2_SUBSTRING_GET_BYNAME(a,b,c,d,e) \
+ a = pcre2_substring_get_byname_32(G(b,32),G(c,32),(PCRE2_UCHAR32 **)d,e)
+#define PCRE2_SUBSTRING_GET_BYNUMBER(a,b,c,d,e) \
+ a = pcre2_substring_get_bynumber_32(G(b,32),c,(PCRE2_UCHAR32 **)d,e)
+#define PCRE2_SUBSTRING_LENGTH_BYNAME(a,b,c,d) \
+ a = pcre2_substring_length_byname_32(G(b,32),G(c,32),d)
+#define PCRE2_SUBSTRING_LENGTH_BYNUMBER(a,b,c,d) \
+ a = pcre2_substring_length_bynumber_32(G(b,32),c,d)
+#define PCRE2_SUBSTRING_LIST_GET(a,b,c,d) \
+ a = pcre2_substring_list_get_32(G(b,32),(PCRE2_UCHAR32 ***)c,d)
+#define PCRE2_SUBSTRING_LIST_FREE(a) \
+ pcre2_substring_list_free_32((PCRE2_SPTR32 *)a)
+#define PCRE2_SUBSTRING_NUMBER_FROM_NAME(a,b,c) \
+ a = pcre2_substring_number_from_name_32(G(b,32),G(c,32));
+#define PTR(x) (void *)G(x,32)
+#define SETFLD(x,y,z) G(x,32)->y = z
+#define SETFLDVEC(x,y,v,z) G(x,32)->y[v] = z
+#define SETOP(x,y,z) G(x,32) z y
+#define SETCASTPTR(x,y) G(x,32) = (uint32_t *)(y)
+#define STRLEN(p) (int)strlen32((PCRE2_SPTR32)p)
+#define SUB1(a,b) G(a,32)(G(b,32))
+#define SUB2(a,b,c) G(a,32)(G(b,32),G(c,32))
+#define TEST(x,r,y) (G(x,32) r (y))
+#define TESTFLD(x,f,r,y) (G(x,32)->f r (y))
+
+#endif
+
+/* ----- End of mode-specific function call macros ----- */
+
+
+
+
+/*************************************************
+* Alternate character tables *
+*************************************************/
+
+/* By default, the "tables" pointer in the compile context when calling
+pcre2_compile() is not set (= NULL), thereby using the default tables of the
+library. However, the tables modifier can be used to select alternate sets of
+tables, for different kinds of testing. Note that the locale modifier also
+adjusts the tables. */
+
+/* This is the set of tables distributed as default with PCRE2. It recognizes
+only ASCII characters. */
+
+static const uint8_t tables1[] = {
+
+/* This table is a lower casing table. */
+
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+ 64, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122, 91, 92, 93, 94, 95,
+ 96, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122,123,124,125,126,127,
+ 128,129,130,131,132,133,134,135,
+ 136,137,138,139,140,141,142,143,
+ 144,145,146,147,148,149,150,151,
+ 152,153,154,155,156,157,158,159,
+ 160,161,162,163,164,165,166,167,
+ 168,169,170,171,172,173,174,175,
+ 176,177,178,179,180,181,182,183,
+ 184,185,186,187,188,189,190,191,
+ 192,193,194,195,196,197,198,199,
+ 200,201,202,203,204,205,206,207,
+ 208,209,210,211,212,213,214,215,
+ 216,217,218,219,220,221,222,223,
+ 224,225,226,227,228,229,230,231,
+ 232,233,234,235,236,237,238,239,
+ 240,241,242,243,244,245,246,247,
+ 248,249,250,251,252,253,254,255,
+
+/* This table is a case flipping table. */
+
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+ 64, 97, 98, 99,100,101,102,103,
+ 104,105,106,107,108,109,110,111,
+ 112,113,114,115,116,117,118,119,
+ 120,121,122, 91, 92, 93, 94, 95,
+ 96, 65, 66, 67, 68, 69, 70, 71,
+ 72, 73, 74, 75, 76, 77, 78, 79,
+ 80, 81, 82, 83, 84, 85, 86, 87,
+ 88, 89, 90,123,124,125,126,127,
+ 128,129,130,131,132,133,134,135,
+ 136,137,138,139,140,141,142,143,
+ 144,145,146,147,148,149,150,151,
+ 152,153,154,155,156,157,158,159,
+ 160,161,162,163,164,165,166,167,
+ 168,169,170,171,172,173,174,175,
+ 176,177,178,179,180,181,182,183,
+ 184,185,186,187,188,189,190,191,
+ 192,193,194,195,196,197,198,199,
+ 200,201,202,203,204,205,206,207,
+ 208,209,210,211,212,213,214,215,
+ 216,217,218,219,220,221,222,223,
+ 224,225,226,227,228,229,230,231,
+ 232,233,234,235,236,237,238,239,
+ 240,241,242,243,244,245,246,247,
+ 248,249,250,251,252,253,254,255,
+
+/* This table contains bit maps for various character classes. Each map is 32
+bytes long and the bits run from the least significant end of each byte. The
+classes that have their own maps are: space, xdigit, digit, upper, lower, word,
+graph, print, punct, and cntrl. Other classes are built from combinations. */
+
+ 0x00,0x3e,0x00,0x00,0x01,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0x7e,0x00,0x00,0x00,0x7e,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0xfe,0xff,0xff,0x07,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x07,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03,
+ 0xfe,0xff,0xff,0x87,0xfe,0xff,0xff,0x07,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0xff,
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
+ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0x00,0x00,0x00,0x00,0xfe,0xff,0x00,0xfc,
+ 0x01,0x00,0x00,0xf8,0x01,0x00,0x00,0x78,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+ 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+
+/* This table identifies various classes of character by individual bits:
+ 0x01 white space character
+ 0x02 letter
+ 0x04 decimal digit
+ 0x08 hexadecimal digit
+ 0x10 alphanumeric or '_'
+ 0x80 regular expression metacharacter or binary zero
+*/
+
+ 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
+ 0x00,0x01,0x01,0x01,0x01,0x01,0x00,0x00, /* 8- 15 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
+ 0x01,0x00,0x00,0x00,0x80,0x00,0x00,0x00, /* - ' */
+ 0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x00, /* ( - / */
+ 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
+ 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x80, /* 8 - ? */
+ 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* @ - G */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* H - O */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* P - W */
+ 0x12,0x12,0x12,0x80,0x80,0x00,0x80,0x10, /* X - _ */
+ 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* ` - g */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* h - o */
+ 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* p - w */
+ 0x12,0x12,0x12,0x80,0x80,0x00,0x00,0x00, /* x -127 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
+
+/* This is a set of tables that came originally from a Windows user. It seems
+to be at least an approximation of ISO 8859. In particular, there are
+characters greater than 128 that are marked as spaces, letters, etc. */
+
+static const uint8_t tables2[] = {
+0,1,2,3,4,5,6,7,
+8,9,10,11,12,13,14,15,
+16,17,18,19,20,21,22,23,
+24,25,26,27,28,29,30,31,
+32,33,34,35,36,37,38,39,
+40,41,42,43,44,45,46,47,
+48,49,50,51,52,53,54,55,
+56,57,58,59,60,61,62,63,
+64,97,98,99,100,101,102,103,
+104,105,106,107,108,109,110,111,
+112,113,114,115,116,117,118,119,
+120,121,122,91,92,93,94,95,
+96,97,98,99,100,101,102,103,
+104,105,106,107,108,109,110,111,
+112,113,114,115,116,117,118,119,
+120,121,122,123,124,125,126,127,
+128,129,130,131,132,133,134,135,
+136,137,138,139,140,141,142,143,
+144,145,146,147,148,149,150,151,
+152,153,154,155,156,157,158,159,
+160,161,162,163,164,165,166,167,
+168,169,170,171,172,173,174,175,
+176,177,178,179,180,181,182,183,
+184,185,186,187,188,189,190,191,
+224,225,226,227,228,229,230,231,
+232,233,234,235,236,237,238,239,
+240,241,242,243,244,245,246,215,
+248,249,250,251,252,253,254,223,
+224,225,226,227,228,229,230,231,
+232,233,234,235,236,237,238,239,
+240,241,242,243,244,245,246,247,
+248,249,250,251,252,253,254,255,
+0,1,2,3,4,5,6,7,
+8,9,10,11,12,13,14,15,
+16,17,18,19,20,21,22,23,
+24,25,26,27,28,29,30,31,
+32,33,34,35,36,37,38,39,
+40,41,42,43,44,45,46,47,
+48,49,50,51,52,53,54,55,
+56,57,58,59,60,61,62,63,
+64,97,98,99,100,101,102,103,
+104,105,106,107,108,109,110,111,
+112,113,114,115,116,117,118,119,
+120,121,122,91,92,93,94,95,
+96,65,66,67,68,69,70,71,
+72,73,74,75,76,77,78,79,
+80,81,82,83,84,85,86,87,
+88,89,90,123,124,125,126,127,
+128,129,130,131,132,133,134,135,
+136,137,138,139,140,141,142,143,
+144,145,146,147,148,149,150,151,
+152,153,154,155,156,157,158,159,
+160,161,162,163,164,165,166,167,
+168,169,170,171,172,173,174,175,
+176,177,178,179,180,181,182,183,
+184,185,186,187,188,189,190,191,
+224,225,226,227,228,229,230,231,
+232,233,234,235,236,237,238,239,
+240,241,242,243,244,245,246,215,
+248,249,250,251,252,253,254,223,
+192,193,194,195,196,197,198,199,
+200,201,202,203,204,205,206,207,
+208,209,210,211,212,213,214,247,
+216,217,218,219,220,221,222,255,
+0,62,0,0,1,0,0,0,
+0,0,0,0,0,0,0,0,
+32,0,0,0,1,0,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,255,3,
+126,0,0,0,126,0,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,255,3,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,12,2,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,
+254,255,255,7,0,0,0,0,
+0,0,0,0,0,0,0,0,
+255,255,127,127,0,0,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,254,255,255,7,
+0,0,0,0,0,4,32,4,
+0,0,0,128,255,255,127,255,
+0,0,0,0,0,0,255,3,
+254,255,255,135,254,255,255,7,
+0,0,0,0,0,4,44,6,
+255,255,127,255,255,255,127,255,
+0,0,0,0,254,255,255,255,
+255,255,255,255,255,255,255,127,
+0,0,0,0,254,255,255,255,
+255,255,255,255,255,255,255,255,
+0,2,0,0,255,255,255,255,
+255,255,255,255,255,255,255,127,
+0,0,0,0,255,255,255,255,
+255,255,255,255,255,255,255,255,
+0,0,0,0,254,255,0,252,
+1,0,0,248,1,0,0,120,
+0,0,0,0,254,255,255,255,
+0,0,128,0,0,0,128,0,
+255,255,255,255,0,0,0,0,
+0,0,0,0,0,0,0,128,
+255,255,255,255,0,0,0,0,
+0,0,0,0,0,0,0,0,
+128,0,0,0,0,0,0,0,
+0,1,1,0,1,1,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,
+1,0,0,0,128,0,0,0,
+128,128,128,128,0,0,128,0,
+28,28,28,28,28,28,28,28,
+28,28,0,0,0,0,0,128,
+0,26,26,26,26,26,26,18,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,18,
+18,18,18,128,128,0,128,16,
+0,26,26,26,26,26,26,18,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,18,
+18,18,18,128,128,0,0,0,
+0,0,0,0,0,1,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,
+1,0,0,0,0,0,0,0,
+0,0,18,0,0,0,0,0,
+0,0,20,20,0,18,0,0,
+0,20,18,0,0,0,0,0,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,0,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,18,
+18,18,18,18,18,18,18,0,
+18,18,18,18,18,18,18,18
+};
+
+
+
+#if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
+/*************************************************
+* Emulated memmove() for systems without it *
+*************************************************/
+
+/* This function can make use of bcopy() if it is available. Otherwise do it by
+steam, as there are some non-Unix environments that lack both memmove() and
+bcopy(). */
+
+static void *
+emulated_memmove(void *d, const void *s, size_t n)
+{
+#ifdef HAVE_BCOPY
+bcopy(s, d, n);
+return d;
+#else
+size_t i;
+unsigned char *dest = (unsigned char *)d;
+const unsigned char *src = (const unsigned char *)s;
+if (dest > src)
+ {
+ dest += n;
+ src += n;
+ for (i = 0; i < n; ++i) *(--dest) = *(--src);
+ return (void *)dest;
+ }
+else
+ {
+ for (i = 0; i < n; ++i) *dest++ = *src++;
+ return (void *)(dest - n);
+ }
+#endif /* not HAVE_BCOPY */
+}
+#undef memmove
+#define memmove(d,s,n) emulated_memmove(d,s,n)
+#endif /* not VPCOMPAT && not HAVE_MEMMOVE */
+
+
+
+#ifndef HAVE_STRERROR
+/*************************************************
+* Provide strerror() for non-ANSI libraries *
+*************************************************/
+
+/* Some old-fashioned systems (e.g. SunOS4) didn't have strerror() in their
+libraries. They may no longer be around, but just in case, we can try to
+provide the same facility by this simple alternative function. */
+
+extern int sys_nerr;
+extern char *sys_errlist[];
+
+char *
+strerror(int n)
+{
+if (n < 0 || n >= sys_nerr) return "unknown error number";
+return sys_errlist[n];
+}
+#endif /* HAVE_STRERROR */
+
+
+
+/*************************************************
+* Local memory functions *
+*************************************************/
+
+/* Alternative memory functions, to test functionality. */
+
+static void *my_malloc(PCRE2_SIZE size, void *data)
+{
+void *block = malloc(size);
+(void)data;
+if (show_memory)
+ {
+ if (block == NULL)
+ {
+ fprintf(outfile, "** malloc() failed for %" SIZ_FORM "\n", SIZ_CAST size);
+ }
+ else
+ {
+ fprintf(outfile, "malloc %5" SIZ_FORM, SIZ_CAST size);
+#ifdef DEBUG_SHOW_MALLOC_ADDRESSES
+ fprintf(outfile, " %p", block); /* Not portable */
+#endif
+ if (malloclistptr < MALLOCLISTSIZE)
+ {
+ malloclist[malloclistptr] = block;
+ malloclistlength[malloclistptr++] = size;
+ }
+ else
+ fprintf(outfile, " (not remembered)");
+ fprintf(outfile, "\n");
+ }
+ }
+return block;
+}
+
+static void my_free(void *block, void *data)
+{
+(void)data;
+if (show_memory)
+ {
+ uint32_t i, j;
+ BOOL found = FALSE;
+
+ fprintf(outfile, "free");
+ for (i = 0; i < malloclistptr; i++)
+ {
+ if (block == malloclist[i])
+ {
+ fprintf(outfile, " %5" SIZ_FORM, SIZ_CAST malloclistlength[i]);
+ malloclistptr--;
+ for (j = i; j < malloclistptr; j++)
+ {
+ malloclist[j] = malloclist[j+1];
+ malloclistlength[j] = malloclistlength[j+1];
+ }
+ found = TRUE;
+ break;
+ }
+ }
+ if (!found) fprintf(outfile, " unremembered block");
+#ifdef DEBUG_SHOW_MALLOC_ADDRESSES
+ fprintf(outfile, " %p", block); /* Not portable */
+#endif
+ fprintf(outfile, "\n");
+ }
+free(block);
+}
+
+
+
+/*************************************************
+* Callback function for stack guard *
+*************************************************/
+
+/* This is set up to be called from pcre2_compile() when the stackguard=n
+modifier sets a value greater than zero. The test we do is whether the
+parenthesis nesting depth is greater than the value set by the modifier.
+
+Argument: the current parenthesis nesting depth
+Returns: non-zero to kill the compilation
+*/
+
+static int
+stack_guard(uint32_t depth, void *user_data)
+{
+(void)user_data;
+return depth > pat_patctl.stackguard_test;
+}
+
+
+/*************************************************
+* JIT memory callback *
+*************************************************/
+
+static PCRE2_JIT_STACK*
+jit_callback(void *arg)
+{
+jit_was_used = TRUE;
+return (PCRE2_JIT_STACK *)arg;
+}
+
+
+/*************************************************
+* Convert UTF-8 character to code point *
+*************************************************/
+
+/* This function reads one or more bytes that represent a UTF-8 character,
+and returns the codepoint of that character. Note that the function supports
+the original UTF-8 definition of RFC 2279, allowing for values in the range 0
+to 0x7fffffff, up to 6 bytes long. This makes it possible to generate
+codepoints greater than 0x10ffff which are useful for testing PCRE2's error
+checking, and also for generating 32-bit non-UTF data values above the UTF
+limit.
+
+Argument:
+ utf8bytes a pointer to the byte vector
+ vptr a pointer to an int to receive the value
+
+Returns: > 0 => the number of bytes consumed
+ -6 to 0 => malformed UTF-8 character at offset = (-return)
+*/
+
+static int
+utf82ord(PCRE2_SPTR8 utf8bytes, uint32_t *vptr)
+{
+uint32_t c = *utf8bytes++;
+uint32_t d = c;
+int i, j, s;
+
+for (i = -1; i < 6; i++) /* i is number of additional bytes */
+ {
+ if ((d & 0x80) == 0) break;
+ d <<= 1;
+ }
+
+if (i == -1) { *vptr = c; return 1; } /* ascii character */
+if (i == 0 || i == 6) return 0; /* invalid UTF-8 */
+
+/* i now has a value in the range 1-5 */
+
+s = 6*i;
+d = (c & utf8_table3[i]) << s;
+
+for (j = 0; j < i; j++)
+ {
+ c = *utf8bytes++;
+ if ((c & 0xc0) != 0x80) return -(j+1);
+ s -= 6;
+ d |= (c & 0x3f) << s;
+ }
+
+/* Check that encoding was the correct unique one */
+
+for (j = 0; j < utf8_table1_size; j++)
+ if (d <= (uint32_t)utf8_table1[j]) break;
+if (j != i) return -(i+1);
+
+/* Valid value */
+
+*vptr = d;
+return i+1;
+}
+
+
+
+/*************************************************
+* Print one character *
+*************************************************/
+
+/* Print a single character either literally, or as a hex escape, and count how
+many printed characters are used.
+
+Arguments:
+ c the character
+ utf TRUE in UTF mode
+ f the FILE to print to, or NULL just to count characters
+
+Returns: number of characters written
+*/
+
+static int
+pchar(uint32_t c, BOOL utf, FILE *f)
+{
+int n = 0;
+char tempbuffer[16];
+
+if (PRINTOK(c))
+ {
+ if (f != NULL) fprintf(f, "%c", c);
+ return 1;
+ }
+
+if (c < 0x100)
+ {
+ if (utf)
+ {
+ if (f != NULL) fprintf(f, "\\x{%02x}", c);
+ return 6;
+ }
+ else
+ {
+ if (f != NULL) fprintf(f, "\\x%02x", c);
+ return 4;
+ }
+ }
+
+if (f != NULL) n = fprintf(f, "\\x{%02x}", c);
+ else n = sprintf(tempbuffer, "\\x{%02x}", c);
+
+return n >= 0 ? n : 0;
+}
+
+
+
+#ifdef SUPPORT_PCRE2_16
+/*************************************************
+* Find length of 0-terminated 16-bit string *
+*************************************************/
+
+static size_t strlen16(PCRE2_SPTR16 p)
+{
+PCRE2_SPTR16 pp = p;
+while (*pp != 0) pp++;
+return (int)(pp - p);
+}
+#endif /* SUPPORT_PCRE2_16 */
+
+
+
+#ifdef SUPPORT_PCRE2_32
+/*************************************************
+* Find length of 0-terminated 32-bit string *
+*************************************************/
+
+static size_t strlen32(PCRE2_SPTR32 p)
+{
+PCRE2_SPTR32 pp = p;
+while (*pp != 0) pp++;
+return (int)(pp - p);
+}
+#endif /* SUPPORT_PCRE2_32 */
+
+
+#ifdef SUPPORT_PCRE2_8
+/*************************************************
+* Print 8-bit character string *
+*************************************************/
+
+/* Must handle UTF-8 strings in utf8 mode. Yields number of characters printed.
+For printing *MARK strings, a negative length is given. If handed a NULL file,
+just counts chars without printing (because pchar() does that). */
+
+static int pchars8(PCRE2_SPTR8 p, int length, BOOL utf, FILE *f)
+{
+uint32_t c = 0;
+int yield = 0;
+
+if (length < 0) length = p[-1];
+while (length-- > 0)
+ {
+ if (utf)
+ {
+ int rc = utf82ord(p, &c);
+ if (rc > 0 && rc <= length + 1) /* Mustn't run over the end */
+ {
+ length -= rc - 1;
+ p += rc;
+ yield += pchar(c, utf, f);
+ continue;
+ }
+ }
+ c = *p++;
+ yield += pchar(c, utf, f);
+ }
+
+return yield;
+}
+#endif
+
+
+#ifdef SUPPORT_PCRE2_16
+/*************************************************
+* Print 16-bit character string *
+*************************************************/
+
+/* Must handle UTF-16 strings in utf mode. Yields number of characters printed.
+For printing *MARK strings, a negative length is given. If handed a NULL file,
+just counts chars without printing. */
+
+static int pchars16(PCRE2_SPTR16 p, int length, BOOL utf, FILE *f)
+{
+int yield = 0;
+if (length < 0) length = p[-1];
+while (length-- > 0)
+ {
+ uint32_t c = *p++ & 0xffff;
+ if (utf && c >= 0xD800 && c < 0xDC00 && length > 0)
+ {
+ int d = *p & 0xffff;
+ if (d >= 0xDC00 && d <= 0xDFFF)
+ {
+ c = ((c & 0x3ff) << 10) + (d & 0x3ff) + 0x10000;
+ length--;
+ p++;
+ }
+ }
+ yield += pchar(c, utf, f);
+ }
+return yield;
+}
+#endif /* SUPPORT_PCRE2_16 */
+
+
+
+#ifdef SUPPORT_PCRE2_32
+/*************************************************
+* Print 32-bit character string *
+*************************************************/
+
+/* Must handle UTF-32 strings in utf mode. Yields number of characters printed.
+For printing *MARK strings, a negative length is given. If handed a NULL file,
+just counts chars without printing. */
+
+static int pchars32(PCRE2_SPTR32 p, int length, BOOL utf, FILE *f)
+{
+int yield = 0;
+(void)(utf); /* Avoid compiler warning */
+
+if (length < 0) length = p[-1];
+while (length-- > 0)
+ {
+ uint32_t c = *p++;
+ yield += pchar(c, utf, f);
+ }
+return yield;
+}
+#endif /* SUPPORT_PCRE2_32 */
+
+
+
+
+#ifdef SUPPORT_PCRE2_8
+/*************************************************
+* Convert character value to UTF-8 *
+*************************************************/
+
+/* This function takes an integer value in the range 0 - 0x7fffffff
+and encodes it as a UTF-8 character in 0 to 6 bytes.
+
+Arguments:
+ cvalue the character value
+ utf8bytes pointer to buffer for result - at least 6 bytes long
+
+Returns: number of characters placed in the buffer
+*/
+
+static int
+ord2utf8(uint32_t cvalue, uint8_t *utf8bytes)
+{
+int i, j;
+if (cvalue > 0x7fffffffu)
+ return -1;
+for (i = 0; i < utf8_table1_size; i++)
+ if (cvalue <= (uint32_t)utf8_table1[i]) break;
+utf8bytes += i;
+for (j = i; j > 0; j--)
+ {
+ *utf8bytes-- = 0x80 | (cvalue & 0x3f);
+ cvalue >>= 6;
+ }
+*utf8bytes = utf8_table2[i] | cvalue;
+return i + 1;
+}
+#endif /* SUPPORT_PCRE2_8 */
+
+
+
+#ifdef SUPPORT_PCRE2_16
+/*************************************************
+* Convert string to 16-bit *
+*************************************************/
+
+/* In UTF mode the input is always interpreted as a string of UTF-8 bytes using
+the original UTF-8 definition of RFC 2279, which allows for up to 6 bytes, and
+code values from 0 to 0x7fffffff. However, values greater than the later UTF
+limit of 0x10ffff cause an error. In non-UTF mode the input is interpreted as
+UTF-8 if the utf8_input modifier is set, but an error is generated for values
+greater than 0xffff.
+
+If all the input bytes are ASCII, the space needed for a 16-bit string is
+exactly double the 8-bit size. Otherwise, the size needed for a 16-bit string
+is no more than double, because up to 0xffff uses no more than 3 bytes in UTF-8
+but possibly 4 in UTF-16. Higher values use 4 bytes in UTF-8 and up to 4 bytes
+in UTF-16. The result is always left in pbuffer16. Impose a minimum size to
+save repeated re-sizing.
+
+Note that this function does not object to surrogate values. This is
+deliberate; it makes it possible to construct UTF-16 strings that are invalid,
+for the purpose of testing that they are correctly faulted.
+
+Arguments:
+ p points to a byte string
+ utf true in UTF mode
+ lenptr points to number of bytes in the string (excluding trailing zero)
+
+Returns: 0 on success, with the length updated to the number of 16-bit
+ data items used (excluding the trailing zero)
+ OR -1 if a UTF-8 string is malformed
+ OR -2 if a value > 0x10ffff is encountered in UTF mode
+ OR -3 if a value > 0xffff is encountered when not in UTF mode
+*/
+
+static PCRE2_SIZE
+to16(uint8_t *p, int utf, PCRE2_SIZE *lenptr)
+{
+uint16_t *pp;
+PCRE2_SIZE len = *lenptr;
+
+if (pbuffer16_size < 2*len + 2)
+ {
+ if (pbuffer16 != NULL) free(pbuffer16);
+ pbuffer16_size = 2*len + 2;
+ if (pbuffer16_size < 4096) pbuffer16_size = 4096;
+ pbuffer16 = (uint16_t *)malloc(pbuffer16_size);
+ if (pbuffer16 == NULL)
+ {
+ fprintf(stderr, "pcre2test: malloc(%" SIZ_FORM ") failed for pbuffer16\n",
+ SIZ_CAST pbuffer16_size);
+ exit(1);
+ }
+ }
+
+pp = pbuffer16;
+if (!utf && (pat_patctl.control & CTL_UTF8_INPUT) == 0)
+ {
+ for (; len > 0; len--) *pp++ = *p++;
+ }
+else while (len > 0)
+ {
+ uint32_t c;
+ int chlen = utf82ord(p, &c);
+ if (chlen <= 0) return -1;
+ if (!utf && c > 0xffff) return -3;
+ if (c > 0x10ffff) return -2;
+ p += chlen;
+ len -= chlen;
+ if (c < 0x10000) *pp++ = c; else
+ {
+ c -= 0x10000;
+ *pp++ = 0xD800 | (c >> 10);
+ *pp++ = 0xDC00 | (c & 0x3ff);
+ }
+ }
+
+*pp = 0;
+*lenptr = pp - pbuffer16;
+return 0;
+}
+#endif
+
+
+
+#ifdef SUPPORT_PCRE2_32
+/*************************************************
+* Convert string to 32-bit *
+*************************************************/
+
+/* In UTF mode the input is always interpreted as a string of UTF-8 bytes using
+the original UTF-8 definition of RFC 2279, which allows for up to 6 bytes, and
+code values from 0 to 0x7fffffff. However, values greater than the later UTF
+limit of 0x10ffff cause an error.
+
+In non-UTF mode the input is interpreted as UTF-8 if the utf8_input modifier
+is set, and no limit is imposed. There is special interpretation of the 0xff
+byte (which is illegal in UTF-8) in this case: it causes the top bit of the
+next character to be set. This provides a way of generating 32-bit characters
+greater than 0x7fffffff.
+
+If all the input bytes are ASCII, the space needed for a 32-bit string is
+exactly four times the 8-bit size. Otherwise, the size needed for a 32-bit
+string is no more than four times, because the number of characters must be
+less than the number of bytes. The result is always left in pbuffer32. Impose a
+minimum size to save repeated re-sizing.
+
+Note that this function does not object to surrogate values. This is
+deliberate; it makes it possible to construct UTF-32 strings that are invalid,
+for the purpose of testing that they are correctly faulted.
+
+Arguments:
+ p points to a byte string
+ utf true in UTF mode
+ lenptr points to number of bytes in the string (excluding trailing zero)
+
+Returns: 0 on success, with the length updated to the number of 32-bit
+ data items used (excluding the trailing zero)
+ OR -1 if a UTF-8 string is malformed
+ OR -2 if a value > 0x10ffff is encountered in UTF mode
+*/
+
+static PCRE2_SIZE
+to32(uint8_t *p, int utf, PCRE2_SIZE *lenptr)
+{
+uint32_t *pp;
+PCRE2_SIZE len = *lenptr;
+
+if (pbuffer32_size < 4*len + 4)
+ {
+ if (pbuffer32 != NULL) free(pbuffer32);
+ pbuffer32_size = 4*len + 4;
+ if (pbuffer32_size < 8192) pbuffer32_size = 8192;
+ pbuffer32 = (uint32_t *)malloc(pbuffer32_size);
+ if (pbuffer32 == NULL)
+ {
+ fprintf(stderr, "pcre2test: malloc(%" SIZ_FORM ") failed for pbuffer32\n",
+ SIZ_CAST pbuffer32_size);
+ exit(1);
+ }
+ }
+
+pp = pbuffer32;
+
+if (!utf && (pat_patctl.control & CTL_UTF8_INPUT) == 0)
+ {
+ for (; len > 0; len--) *pp++ = *p++;
+ }
+
+else while (len > 0)
+ {
+ int chlen;
+ uint32_t c;
+ uint32_t topbit = 0;
+ if (!utf && *p == 0xff && len > 1)
+ {
+ topbit = 0x80000000u;
+ p++;
+ len--;
+ }
+ chlen = utf82ord(p, &c);
+ if (chlen <= 0) return -1;
+ if (utf && c > 0x10ffff) return -2;
+ p += chlen;
+ len -= chlen;
+ *pp++ = c | topbit;
+ }
+
+*pp = 0;
+*lenptr = pp - pbuffer32;
+return 0;
+}
+#endif /* SUPPORT_PCRE2_32 */
+
+
+
+/*************************************************
+* Move back by so many characters *
+*************************************************/
+
+/* Given a code unit offset in a subject string, move backwards by a number of
+characters, and return the resulting offset.
+
+Arguments:
+ subject pointer to the string
+ offset start offset
+ count count to move back by
+ utf TRUE if in UTF mode
+
+Returns: a possibly changed offset
+*/
+
+static PCRE2_SIZE
+backchars(uint8_t *subject, PCRE2_SIZE offset, uint32_t count, BOOL utf)
+{
+if (!utf || test_mode == PCRE32_MODE)
+ return (count >= offset)? 0 : (offset - count);
+
+else if (test_mode == PCRE8_MODE)
+ {
+ PCRE2_SPTR8 pp = (PCRE2_SPTR8)subject + offset;
+ for (; count > 0 && pp > (PCRE2_SPTR8)subject; count--)
+ {
+ pp--;
+ while ((*pp & 0xc0) == 0x80) pp--;
+ }
+ return pp - (PCRE2_SPTR8)subject;
+ }
+
+else /* 16-bit mode */
+ {
+ PCRE2_SPTR16 pp = (PCRE2_SPTR16)subject + offset;
+ for (; count > 0 && pp > (PCRE2_SPTR16)subject; count--)
+ {
+ pp--;
+ if ((*pp & 0xfc00) == 0xdc00) pp--;
+ }
+ return pp - (PCRE2_SPTR16)subject;
+ }
+}
+
+
+
+/*************************************************
+* Expand input buffers *
+*************************************************/
+
+/* This function doubles the size of the input buffer and the buffer for
+keeping an 8-bit copy of patterns (pbuffer8), and copies the current buffers to
+the new ones.
+
+Arguments: none
+Returns: nothing (aborts if malloc() fails)
+*/
+
+static void
+expand_input_buffers(void)
+{
+int new_pbuffer8_size = 2*pbuffer8_size;
+uint8_t *new_buffer = (uint8_t *)malloc(new_pbuffer8_size);
+uint8_t *new_pbuffer8 = (uint8_t *)malloc(new_pbuffer8_size);
+
+if (new_buffer == NULL || new_pbuffer8 == NULL)
+ {
+ fprintf(stderr, "pcre2test: malloc(%d) failed\n", new_pbuffer8_size);
+ exit(1);
+ }
+
+memcpy(new_buffer, buffer, pbuffer8_size);
+memcpy(new_pbuffer8, pbuffer8, pbuffer8_size);
+
+pbuffer8_size = new_pbuffer8_size;
+
+free(buffer);
+free(pbuffer8);
+
+buffer = new_buffer;
+pbuffer8 = new_pbuffer8;
+}
+
+
+
+/*************************************************
+* Read or extend an input line *
+*************************************************/
+
+/* Input lines are read into buffer, but both patterns and data lines can be
+continued over multiple input lines. In addition, if the buffer fills up, we
+want to automatically expand it so as to be able to handle extremely large
+lines that are needed for certain stress tests, although this is less likely
+now that there are repetition features for both patterns and data. When the
+input buffer is expanded, the other two buffers must also be expanded likewise,
+and the contents of pbuffer, which are a copy of the input for callouts, must
+be preserved (for when expansion happens for a data line). This is not the most
+optimal way of handling this, but hey, this is just a test program!
+
+Arguments:
+ f the file to read
+ start where in buffer to start (this *must* be within buffer)
+ prompt for stdin or readline()
+
+Returns: pointer to the start of new data
+ could be a copy of start, or could be moved
+ NULL if no data read and EOF reached
+*/
+
+static uint8_t *
+extend_inputline(FILE *f, uint8_t *start, const char *prompt)
+{
+uint8_t *here = start;
+
+for (;;)
+ {
+ size_t rlen = (size_t)(pbuffer8_size - (here - buffer));
+
+ if (rlen > 1000)
+ {
+ size_t dlen;
+
+ /* If libreadline or libedit support is required, use readline() to read a
+ line if the input is a terminal. Note that readline() removes the trailing
+ newline, so we must put it back again, to be compatible with fgets(). */
+
+#if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT)
+ if (INTERACTIVE(f))
+ {
+ size_t len;
+ char *s = readline(prompt);
+ if (s == NULL) return (here == start)? NULL : start;
+ len = strlen(s);
+ if (len > 0) add_history(s);
+ if (len > rlen - 1) len = rlen - 1;
+ memcpy(here, s, len);
+ here[len] = '\n';
+ here[len+1] = 0;
+ free(s);
+ }
+ else
+#endif
+
+ /* Read the next line by normal means, prompting if the file is a tty. */
+
+ {
+ if (INTERACTIVE(f)) printf("%s", prompt);
+ if (fgets((char *)here, rlen, f) == NULL)
+ return (here == start)? NULL : start;
+ }
+
+ dlen = strlen((char *)here);
+ here += dlen;
+
+ /* Check for end of line reached. Take care not to read data from before
+ start (dlen will be zero for a file starting with a binary zero). */
+
+ if (here > start && here[-1] == '\n') return start;
+
+ /* If we have not read a newline when reading a file, we have either filled
+ the buffer or reached the end of the file. We can detect the former by
+ checking that the string fills the buffer, and the latter by feof(). If
+ neither of these is true, it means we read a binary zero which has caused
+ strlen() to give a short length. This is a hard error because pcre2test
+ expects to work with C strings. */
+
+ if (!INTERACTIVE(f) && dlen < rlen - 1 && !feof(f))
+ {
+ fprintf(outfile, "** Binary zero encountered in input\n");
+ fprintf(outfile, "** pcre2test run abandoned\n");
+ exit(1);
+ }
+ }
+
+ else
+ {
+ size_t start_offset = start - buffer;
+ size_t here_offset = here - buffer;
+ expand_input_buffers();
+ start = buffer + start_offset;
+ here = buffer + here_offset;
+ }
+ }
+
+/* Control never gets here */
+}
+
+
+
+/*************************************************
+* Case-independent strncmp() function *
+*************************************************/
+
+/*
+Arguments:
+ s first string
+ t second string
+ n number of characters to compare
+
+Returns: < 0, = 0, or > 0, according to the comparison
+*/
+
+static int
+strncmpic(const uint8_t *s, const uint8_t *t, int n)
+{
+while (n--)
+ {
+ int c = tolower(*s++) - tolower(*t++);
+ if (c != 0) return c;
+ }
+return 0;
+}
+
+
+
+/*************************************************
+* Scan the main modifier list *
+*************************************************/
+
+/* This function searches the modifier list for a long modifier name.
+
+Argument:
+ p start of the name
+ lenp length of the name
+
+Returns: an index in the modifier list, or -1 on failure
+*/
+
+static int
+scan_modifiers(const uint8_t *p, unsigned int len)
+{
+int bot = 0;
+int top = MODLISTCOUNT;
+
+while (top > bot)
+ {
+ int mid = (bot + top)/2;
+ unsigned int mlen = strlen(modlist[mid].name);
+ int c = strncmp((char *)p, modlist[mid].name, (len < mlen)? len : mlen);
+ if (c == 0)
+ {
+ if (len == mlen) return mid;
+ c = (int)len - (int)mlen;
+ }
+ if (c > 0) bot = mid + 1; else top = mid;
+ }
+
+return -1;
+
+}
+
+
+
+/*************************************************
+* Check a modifer and find its field *
+*************************************************/
+
+/* This function is called when a modifier has been identified. We check that
+it is allowed here and find the field that is to be changed.
+
+Arguments:
+ m the modifier list entry
+ ctx CTX_PAT => pattern context
+ CTX_POPPAT => pattern context for popped pattern
+ CTX_DEFPAT => default pattern context
+ CTX_DAT => data context
+ CTX_DEFDAT => default data context
+ pctl point to pattern control block
+ dctl point to data control block
+ c a single character or 0
+
+Returns: a field pointer or NULL
+*/
+
+static void *
+check_modifier(modstruct *m, int ctx, patctl *pctl, datctl *dctl, uint32_t c)
+{
+void *field = NULL;
+PCRE2_SIZE offset = m->offset;
+
+if (restrict_for_perl_test) switch(m->which)
+ {
+ case MOD_PNDP:
+ case MOD_PATP:
+ case MOD_PDP:
+ break;
+
+ default:
+ fprintf(outfile, "** '%s' is not allowed in a Perl-compatible test\n",
+ m->name);
+ return NULL;
+ }
+
+switch (m->which)
+ {
+ case MOD_CTC: /* Compile context modifier */
+ if (ctx == CTX_DEFPAT) field = PTR(default_pat_context);
+ else if (ctx == CTX_PAT) field = PTR(pat_context);
+ break;
+
+ case MOD_CTM: /* Match context modifier */
+ if (ctx == CTX_DEFDAT) field = PTR(default_dat_context);
+ else if (ctx == CTX_DAT) field = PTR(dat_context);
+ break;
+
+ case MOD_DAT: /* Data line modifier */
+ if (dctl != NULL) field = dctl;
+ break;
+
+ case MOD_PAT: /* Pattern modifier */
+ case MOD_PATP: /* Allowed for Perl test */
+ if (pctl != NULL) field = pctl;
+ break;
+
+ case MOD_PD: /* Pattern or data line modifier */
+ case MOD_PDP: /* Ditto, allowed for Perl test */
+ case MOD_PND: /* Ditto, but not default pattern */
+ case MOD_PNDP: /* Ditto, allowed for Perl test */
+ if (dctl != NULL) field = dctl;
+ else if (pctl != NULL && (m->which == MOD_PD || m->which == MOD_PDP ||
+ ctx != CTX_DEFPAT))
+ field = pctl;
+ break;
+ }
+
+if (field == NULL)
+ {
+ if (c == 0)
+ fprintf(outfile, "** '%s' is not valid here\n", m->name);
+ else
+ fprintf(outfile, "** /%c is not valid here\n", c);
+ return NULL;
+ }
+
+return (char *)field + offset;
+}
+
+
+
+/*************************************************
+* Decode a modifier list *
+*************************************************/
+
+/* A pointer to a control block is NULL when called in cases when that block is
+not relevant. They are never all relevant in one call. At least one of patctl
+and datctl is NULL. The second argument specifies which context to use for
+modifiers that apply to contexts.
+
+Arguments:
+ p point to modifier string
+ ctx CTX_PAT => pattern context
+ CTX_POPPAT => pattern context for popped pattern
+ CTX_DEFPAT => default pattern context
+ CTX_DAT => data context
+ CTX_DEFDAT => default data context
+ pctl point to pattern control block
+ dctl point to data control block
+
+Returns: TRUE if successful decode, FALSE otherwise
+*/
+
+static BOOL
+decode_modifiers(uint8_t *p, int ctx, patctl *pctl, datctl *dctl)
+{
+uint8_t *ep, *pp;
+long li;
+unsigned long uli;
+BOOL first = TRUE;
+
+for (;;)
+ {
+ void *field;
+ modstruct *m;
+ BOOL off = FALSE;
+ unsigned int i, len;
+ int index;
+ char *endptr;
+
+ /* Skip white space and commas. */
+
+ while (isspace(*p) || *p == ',') p++;
+ if (*p == 0) break;
+
+ /* Find the end of the item; lose trailing whitespace at end of line. */
+
+ for (ep = p; *ep != 0 && *ep != ','; ep++);
+ if (*ep == 0)
+ {
+ while (ep > p && isspace(ep[-1])) ep--;
+ *ep = 0;
+ }
+
+ /* Remember if the first character is '-'. */
+
+ if (*p == '-')
+ {
+ off = TRUE;
+ p++;
+ }
+
+ /* Find the length of a full-length modifier name, and scan for it. */
+
+ pp = p;
+ while (pp < ep && *pp != '=') pp++;
+ index = scan_modifiers(p, pp - p);
+
+ /* If the first modifier is unrecognized, try to interpret it as a sequence
+ of single-character abbreviated modifiers. None of these modifiers have any
+ associated data. They just set options or control bits. */
+
+ if (index < 0)
+ {
+ uint32_t cc;
+ uint8_t *mp = p;
+
+ if (!first)
+ {
+ fprintf(outfile, "** Unrecognized modifier '%.*s'\n", (int)(ep-p), p);
+ if (ep - p == 1)
+ fprintf(outfile, "** Single-character modifiers must come first\n");
+ return FALSE;
+ }
+
+ for (cc = *p; cc != ',' && cc != '\n' && cc != 0; cc = *(++p))
+ {
+ for (i = 0; i < C1MODLISTCOUNT; i++)
+ if (cc == c1modlist[i].onechar) break;
+
+ if (i >= C1MODLISTCOUNT)
+ {
+ fprintf(outfile, "** Unrecognized modifier '%c' in '%.*s'\n",
+ *p, (int)(ep-mp), mp);
+ return FALSE;
+ }
+
+ if (c1modlist[i].index >= 0)
+ {
+ index = c1modlist[i].index;
+ }
+
+ else
+ {
+ index = scan_modifiers((uint8_t *)(c1modlist[i].fullname),
+ strlen(c1modlist[i].fullname));
+ if (index < 0)
+ {
+ fprintf(outfile, "** Internal error: single-character equivalent "
+ "modifier '%s' not found\n", c1modlist[i].fullname);
+ return FALSE;
+ }
+ c1modlist[i].index = index; /* Cache for next time */
+ }
+
+ field = check_modifier(modlist + index, ctx, pctl, dctl, *p);
+ if (field == NULL) return FALSE;
+
+ /* /x is a special case; a second appearance changes PCRE2_EXTENDED to
+ PCRE2_EXTENDED_MORE. */
+
+ if (cc == 'x' && (*((uint32_t *)field) & PCRE2_EXTENDED) != 0)
+ {
+ *((uint32_t *)field) &= ~PCRE2_EXTENDED;
+ *((uint32_t *)field) |= PCRE2_EXTENDED_MORE;
+ }
+ else
+ *((uint32_t *)field) |= modlist[index].value;
+ }
+
+ continue; /* With tne next (fullname) modifier */
+ }
+
+ /* We have a match on a full-name modifier. Check for the existence of data
+ when needed. */
+
+ m = modlist + index; /* Save typing */
+ if (m->type != MOD_CTL && m->type != MOD_OPT &&
+ (m->type != MOD_IND || *pp == '='))
+ {
+ if (*pp++ != '=')
+ {
+ fprintf(outfile, "** '=' expected after '%s'\n", m->name);
+ return FALSE;
+ }
+ if (off)
+ {
+ fprintf(outfile, "** '-' is not valid for '%s'\n", m->name);
+ return FALSE;
+ }
+ }
+
+ /* These on/off types have no data. */
+
+ else if (*pp != ',' && *pp != '\n' && *pp != ' ' && *pp != 0)
+ {
+ fprintf(outfile, "** Unrecognized modifier '%.*s'\n", (int)(ep-p), p);
+ return FALSE;
+ }
+
+ /* Set the data length for those types that have data. Then find the field
+ that is to be set. If check_modifier() returns NULL, it has already output an
+ error message. */
+
+ len = ep - pp;
+ field = check_modifier(m, ctx, pctl, dctl, 0);
+ if (field == NULL) return FALSE;
+
+ /* Process according to data type. */
+
+ switch (m->type)
+ {
+ case MOD_CTL:
+ case MOD_OPT:
+ if (off) *((uint32_t *)field) &= ~m->value;
+ else *((uint32_t *)field) |= m->value;
+ break;
+
+ case MOD_BSR:
+ if (len == 7 && strncmpic(pp, (const uint8_t *)"default", 7) == 0)
+ {
+#ifdef BSR_ANYCRLF
+ *((uint16_t *)field) = PCRE2_BSR_ANYCRLF;
+#else
+ *((uint16_t *)field) = PCRE2_BSR_UNICODE;
+#endif
+ if (ctx == CTX_PAT || ctx == CTX_DEFPAT) pctl->control2 &= ~CTL2_BSR_SET;
+ else dctl->control2 &= ~CTL2_BSR_SET;
+ }
+ else
+ {
+ if (len == 7 && strncmpic(pp, (const uint8_t *)"anycrlf", 7) == 0)
+ *((uint16_t *)field) = PCRE2_BSR_ANYCRLF;
+ else if (len == 7 && strncmpic(pp, (const uint8_t *)"unicode", 7) == 0)
+ *((uint16_t *)field) = PCRE2_BSR_UNICODE;
+ else goto INVALID_VALUE;
+ if (ctx == CTX_PAT || ctx == CTX_DEFPAT) pctl->control2 |= CTL2_BSR_SET;
+ else dctl->control2 |= CTL2_BSR_SET;
+ }
+ pp = ep;
+ break;
+
+ case MOD_CHR: /* A single character */
+ *((uint32_t *)field) = *pp++;
+ break;
+
+ case MOD_CON: /* A convert type/options list */
+ for (;; pp++)
+ {
+ uint8_t *colon = (uint8_t *)strchr((const char *)pp, ':');
+ len = ((colon != NULL && colon < ep)? colon:ep) - pp;
+ for (i = 0; i < convertlistcount; i++)
+ {
+ if (strncmpic(pp, (const uint8_t *)convertlist[i].name, len) == 0)
+ {
+ if (*((uint32_t *)field) == CONVERT_UNSET)
+ *((uint32_t *)field) = convertlist[i].option;
+ else
+ *((uint32_t *)field) |= convertlist[i].option;
+ break;
+ }
+ }
+ if (i >= convertlistcount) goto INVALID_VALUE;
+ pp += len;
+ if (*pp != ':') break;
+ }
+ break;
+
+ case MOD_IN2: /* One or two unsigned integers */
+ if (!isdigit(*pp)) goto INVALID_VALUE;
+ uli = strtoul((const char *)pp, &endptr, 10);
+ if (U32OVERFLOW(uli)) goto INVALID_VALUE;
+ ((uint32_t *)field)[0] = (uint32_t)uli;
+ if (*endptr == ':')
+ {
+ uli = strtoul((const char *)endptr+1, &endptr, 10);
+ if (U32OVERFLOW(uli)) goto INVALID_VALUE;
+ ((uint32_t *)field)[1] = (uint32_t)uli;
+ }
+ else ((uint32_t *)field)[1] = 0;
+ pp = (uint8_t *)endptr;
+ break;
+
+ /* PCRE2_SIZE_MAX is usually SIZE_MAX, which may be greater, equal to, or
+ less than ULONG_MAX. So first test for overflowing the long int, and then
+ test for overflowing PCRE2_SIZE_MAX if it is smaller than ULONG_MAX. */
+
+ case MOD_SIZ: /* PCRE2_SIZE value */
+ if (!isdigit(*pp)) goto INVALID_VALUE;
+ uli = strtoul((const char *)pp, &endptr, 10);
+ if (uli == ULONG_MAX) goto INVALID_VALUE;
+#if ULONG_MAX > PCRE2_SIZE_MAX
+ if (uli > PCRE2_SIZE_MAX) goto INVALID_VALUE;
+#endif
+ *((PCRE2_SIZE *)field) = (PCRE2_SIZE)uli;
+ pp = (uint8_t *)endptr;
+ break;
+
+ case MOD_IND: /* Unsigned integer with default */
+ if (len == 0)
+ {
+ *((uint32_t *)field) = (uint32_t)(m->value);
+ break;
+ }
+ /* Fall through */
+
+ case MOD_INT: /* Unsigned integer */
+ if (!isdigit(*pp)) goto INVALID_VALUE;
+ uli = strtoul((const char *)pp, &endptr, 10);
+ if (U32OVERFLOW(uli)) goto INVALID_VALUE;
+ *((uint32_t *)field) = (uint32_t)uli;
+ pp = (uint8_t *)endptr;
+ break;
+
+ case MOD_INS: /* Signed integer */
+ if (!isdigit(*pp) && *pp != '-') goto INVALID_VALUE;
+ li = strtol((const char *)pp, &endptr, 10);
+ if (S32OVERFLOW(li)) goto INVALID_VALUE;
+ *((int32_t *)field) = (int32_t)li;
+ pp = (uint8_t *)endptr;
+ break;
+
+ case MOD_NL:
+ for (i = 0; i < sizeof(newlines)/sizeof(char *); i++)
+ if (len == strlen(newlines[i]) &&
+ strncmpic(pp, (const uint8_t *)newlines[i], len) == 0) break;
+ if (i >= sizeof(newlines)/sizeof(char *)) goto INVALID_VALUE;
+ if (i == 0)
+ {
+ *((uint16_t *)field) = NEWLINE_DEFAULT;
+ if (ctx == CTX_PAT || ctx == CTX_DEFPAT) pctl->control2 &= ~CTL2_NL_SET;
+ else dctl->control2 &= ~CTL2_NL_SET;
+ }
+ else
+ {
+ *((uint16_t *)field) = i;
+ if (ctx == CTX_PAT || ctx == CTX_DEFPAT) pctl->control2 |= CTL2_NL_SET;
+ else dctl->control2 |= CTL2_NL_SET;
+ }
+ pp = ep;
+ break;
+
+ case MOD_NN: /* Name or (signed) number; may be several */
+ if (isdigit(*pp) || *pp == '-')
+ {
+ int ct = MAXCPYGET - 1;
+ int32_t value;
+ li = strtol((const char *)pp, &endptr, 10);
+ if (S32OVERFLOW(li)) goto INVALID_VALUE;
+ value = (int32_t)li;
+ field = (char *)field - m->offset + m->value; /* Adjust field ptr */
+ if (value >= 0) /* Add new number */
+ {
+ while (*((int32_t *)field) >= 0 && ct-- > 0) /* Skip previous */
+ field = (char *)field + sizeof(int32_t);
+ if (ct <= 0)
+ {
+ fprintf(outfile, "** Too many numeric '%s' modifiers\n", m->name);
+ return FALSE;
+ }
+ }
+ *((int32_t *)field) = value;
+ if (ct > 0) ((int32_t *)field)[1] = -1;
+ pp = (uint8_t *)endptr;
+ }
+
+ /* Multiple strings are put end to end. */
+
+ else
+ {
+ char *nn = (char *)field;
+ if (len > 0) /* Add new name */
+ {
+ if (len > MAX_NAME_SIZE)
+ {
+ fprintf(outfile, "** Group name in '%s' is too long\n", m->name);
+ return FALSE;
+ }
+ while (*nn != 0) nn += strlen(nn) + 1;
+ if (nn + len + 2 - (char *)field > LENCPYGET)
+ {
+ fprintf(outfile, "** Too many characters in named '%s' modifiers\n",
+ m->name);
+ return FALSE;
+ }
+ memcpy(nn, pp, len);
+ }
+ nn[len] = 0 ;
+ nn[len+1] = 0;
+ pp = ep;
+ }
+ break;
+
+ case MOD_STR:
+ if (len + 1 > m->value)
+ {
+ fprintf(outfile, "** Overlong value for '%s' (max %d code units)\n",
+ m->name, m->value - 1);
+ return FALSE;
+ }
+ memcpy(field, pp, len);
+ ((uint8_t *)field)[len] = 0;
+ pp = ep;
+ break;
+ }
+
+ if (*pp != ',' && *pp != '\n' && *pp != ' ' && *pp != 0)
+ {
+ fprintf(outfile, "** Comma expected after modifier item '%s'\n", m->name);
+ return FALSE;
+ }
+
+ p = pp;
+ first = FALSE;
+
+ if (ctx == CTX_POPPAT &&
+ (pctl->options != 0 ||
+ pctl->tables_id != 0 ||
+ pctl->locale[0] != 0 ||
+ (pctl->control & NOTPOP_CONTROLS) != 0))
+ {
+ fprintf(outfile, "** '%s' is not valid here\n", m->name);
+ return FALSE;
+ }
+ }
+
+return TRUE;
+
+INVALID_VALUE:
+fprintf(outfile, "** Invalid value in '%.*s'\n", (int)(ep-p), p);
+return FALSE;
+}
+
+
+/*************************************************
+* Get info from a pattern *
+*************************************************/
+
+/* A wrapped call to pcre2_pattern_info(), applied to the current compiled
+pattern.
+
+Arguments:
+ what code for the required information
+ where where to put the answer
+ unsetok PCRE2_ERROR_UNSET is an "expected" result
+
+Returns: the return from pcre2_pattern_info()
+*/
+
+static int
+pattern_info(int what, void *where, BOOL unsetok)
+{
+int rc;
+PCRE2_PATTERN_INFO(rc, compiled_code, what, NULL); /* Exercise the code */
+PCRE2_PATTERN_INFO(rc, compiled_code, what, where);
+if (rc >= 0) return 0;
+if (rc != PCRE2_ERROR_UNSET || !unsetok)
+ {
+ fprintf(outfile, "Error %d from pcre2_pattern_info_%d(%d)\n", rc, test_mode,
+ what);
+ if (rc == PCRE2_ERROR_BADMODE)
+ fprintf(outfile, "Running in %d-bit mode but pattern was compiled in "
+ "%d-bit mode\n", test_mode,
+ 8 * (FLD(compiled_code, flags) & PCRE2_MODE_MASK));
+ }
+return rc;
+}
+
+
+
+#ifdef SUPPORT_PCRE2_8
+/*************************************************
+* Show something in a list *
+*************************************************/
+
+/* This function just helps to keep the code that uses it tidier. It's used for
+various lists of things where there needs to be introductory text before the
+first item. As these calls are all in the POSIX-support code, they happen only
+when 8-bit mode is supported. */
+
+static void
+prmsg(const char **msg, const char *s)
+{
+fprintf(outfile, "%s %s", *msg, s);
+*msg = "";
+}
+#endif /* SUPPORT_PCRE2_8 */
+
+
+
+/*************************************************
+* Show control bits *
+*************************************************/
+
+/* Called for mutually exclusive controls and for unsupported POSIX controls.
+Because the bits are unique, this can be used for both pattern and data control
+words.
+
+Arguments:
+ controls control bits
+ controls2 more control bits
+ before text to print before
+
+Returns: nothing
+*/
+
+static void
+show_controls(uint32_t controls, uint32_t controls2, const char *before)
+{
+fprintf(outfile, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+ before,
+ ((controls & CTL_AFTERTEXT) != 0)? " aftertext" : "",
+ ((controls & CTL_ALLAFTERTEXT) != 0)? " allaftertext" : "",
+ ((controls & CTL_ALLCAPTURES) != 0)? " allcaptures" : "",
+ ((controls & CTL_ALLUSEDTEXT) != 0)? " allusedtext" : "",
+ ((controls & CTL_ALTGLOBAL) != 0)? " altglobal" : "",
+ ((controls & CTL_BINCODE) != 0)? " bincode" : "",
+ ((controls2 & CTL2_BSR_SET) != 0)? " bsr" : "",
+ ((controls & CTL_CALLOUT_CAPTURE) != 0)? " callout_capture" : "",
+ ((controls2 & CTL2_CALLOUT_EXTRA) != 0)? " callout_extra" : "",
+ ((controls & CTL_CALLOUT_INFO) != 0)? " callout_info" : "",
+ ((controls & CTL_CALLOUT_NONE) != 0)? " callout_none" : "",
+ ((controls2 & CTL2_CALLOUT_NO_WHERE) != 0)? " callout_no_where" : "",
+ ((controls & CTL_DFA) != 0)? " dfa" : "",
+ ((controls & CTL_EXPAND) != 0)? " expand" : "",
+ ((controls & CTL_FINDLIMITS) != 0)? " find_limits" : "",
+ ((controls & CTL_FRAMESIZE) != 0)? " framesize" : "",
+ ((controls & CTL_FULLBINCODE) != 0)? " fullbincode" : "",
+ ((controls & CTL_GETALL) != 0)? " getall" : "",
+ ((controls & CTL_GLOBAL) != 0)? " global" : "",
+ ((controls & CTL_HEXPAT) != 0)? " hex" : "",
+ ((controls & CTL_INFO) != 0)? " info" : "",
+ ((controls & CTL_JITFAST) != 0)? " jitfast" : "",
+ ((controls & CTL_JITVERIFY) != 0)? " jitverify" : "",
+ ((controls & CTL_MARK) != 0)? " mark" : "",
+ ((controls & CTL_MEMORY) != 0)? " memory" : "",
+ ((controls2 & CTL2_NL_SET) != 0)? " newline" : "",
+ ((controls & CTL_NULLCONTEXT) != 0)? " null_context" : "",
+ ((controls & CTL_POSIX) != 0)? " posix" : "",
+ ((controls & CTL_POSIX_NOSUB) != 0)? " posix_nosub" : "",
+ ((controls & CTL_PUSH) != 0)? " push" : "",
+ ((controls & CTL_PUSHCOPY) != 0)? " pushcopy" : "",
+ ((controls & CTL_PUSHTABLESCOPY) != 0)? " pushtablescopy" : "",
+ ((controls & CTL_STARTCHAR) != 0)? " startchar" : "",
+ ((controls2 & CTL2_SUBSTITUTE_EXTENDED) != 0)? " substitute_extended" : "",
+ ((controls2 & CTL2_SUBSTITUTE_OVERFLOW_LENGTH) != 0)? " substitute_overflow_length" : "",
+ ((controls2 & CTL2_SUBSTITUTE_UNKNOWN_UNSET) != 0)? " substitute_unknown_unset" : "",
+ ((controls2 & CTL2_SUBSTITUTE_UNSET_EMPTY) != 0)? " substitute_unset_empty" : "",
+ ((controls & CTL_USE_LENGTH) != 0)? " use_length" : "",
+ ((controls & CTL_UTF8_INPUT) != 0)? " utf8_input" : "",
+ ((controls & CTL_ZERO_TERMINATE) != 0)? " zero_terminate" : "");
+}
+
+
+
+/*************************************************
+* Show compile options *
+*************************************************/
+
+/* Called from show_pattern_info() and for unsupported POSIX options.
+
+Arguments:
+ options an options word
+ before text to print before
+ after text to print after
+
+Returns: nothing
+*/
+
+static void
+show_compile_options(uint32_t options, const char *before, const char *after)
+{
+if (options == 0) fprintf(outfile, "%s <none>%s", before, after);
+else fprintf(outfile, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+ before,
+ ((options & PCRE2_ALT_BSUX) != 0)? " alt_bsux" : "",
+ ((options & PCRE2_ALT_CIRCUMFLEX) != 0)? " alt_circumflex" : "",
+ ((options & PCRE2_ALT_VERBNAMES) != 0)? " alt_verbnames" : "",
+ ((options & PCRE2_ALLOW_EMPTY_CLASS) != 0)? " allow_empty_class" : "",
+ ((options & PCRE2_ANCHORED) != 0)? " anchored" : "",
+ ((options & PCRE2_AUTO_CALLOUT) != 0)? " auto_callout" : "",
+ ((options & PCRE2_CASELESS) != 0)? " caseless" : "",
+ ((options & PCRE2_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",
+ ((options & PCRE2_DOTALL) != 0)? " dotall" : "",
+ ((options & PCRE2_DUPNAMES) != 0)? " dupnames" : "",
+ ((options & PCRE2_ENDANCHORED) != 0)? " endanchored" : "",
+ ((options & PCRE2_EXTENDED) != 0)? " extended" : "",
+ ((options & PCRE2_EXTENDED_MORE) != 0)? " extended_more" : "",
+ ((options & PCRE2_FIRSTLINE) != 0)? " firstline" : "",
+ ((options & PCRE2_LITERAL) != 0)? " literal" : "",
+ ((options & PCRE2_MATCH_UNSET_BACKREF) != 0)? " match_unset_backref" : "",
+ ((options & PCRE2_MULTILINE) != 0)? " multiline" : "",
+ ((options & PCRE2_NEVER_BACKSLASH_C) != 0)? " never_backslash_c" : "",
+ ((options & PCRE2_NEVER_UCP) != 0)? " never_ucp" : "",
+ ((options & PCRE2_NEVER_UTF) != 0)? " never_utf" : "",
+ ((options & PCRE2_NO_AUTO_CAPTURE) != 0)? " no_auto_capture" : "",
+ ((options & PCRE2_NO_AUTO_POSSESS) != 0)? " no_auto_possess" : "",
+ ((options & PCRE2_NO_DOTSTAR_ANCHOR) != 0)? " no_dotstar_anchor" : "",
+ ((options & PCRE2_NO_UTF_CHECK) != 0)? " no_utf_check" : "",
+ ((options & PCRE2_NO_START_OPTIMIZE) != 0)? " no_start_optimize" : "",
+ ((options & PCRE2_UCP) != 0)? " ucp" : "",
+ ((options & PCRE2_UNGREEDY) != 0)? " ungreedy" : "",
+ ((options & PCRE2_USE_OFFSET_LIMIT) != 0)? " use_offset_limit" : "",
+ ((options & PCRE2_UTF) != 0)? " utf" : "",
+ after);
+}
+
+
+/*************************************************
+* Show compile extra options *
+*************************************************/
+
+/* Called from show_pattern_info() and for unsupported POSIX options.
+
+Arguments:
+ options an options word
+ before text to print before
+ after text to print after
+
+Returns: nothing
+*/
+
+static void
+show_compile_extra_options(uint32_t options, const char *before,
+ const char *after)
+{
+if (options == 0) fprintf(outfile, "%s <none>%s", before, after);
+else fprintf(outfile, "%s%s%s%s%s%s",
+ before,
+ ((options & PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES) != 0)? " allow_surrogate_escapes" : "",
+ ((options & PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL) != 0)? " bad_escape_is_literal" : "",
+ ((options & PCRE2_EXTRA_MATCH_WORD) != 0)? " match_word" : "",
+ ((options & PCRE2_EXTRA_MATCH_LINE) != 0)? " match_line" : "",
+ after);
+}
+
+
+
+#ifdef SUPPORT_PCRE2_8
+/*************************************************
+* Show match options *
+*************************************************/
+
+/* Called for unsupported POSIX options. */
+
+static void
+show_match_options(uint32_t options)
+{
+fprintf(outfile, "%s%s%s%s%s%s%s%s%s%s%s",
+ ((options & PCRE2_ANCHORED) != 0)? " anchored" : "",
+ ((options & PCRE2_DFA_RESTART) != 0)? " dfa_restart" : "",
+ ((options & PCRE2_DFA_SHORTEST) != 0)? " dfa_shortest" : "",
+ ((options & PCRE2_ENDANCHORED) != 0)? " endanchored" : "",
+ ((options & PCRE2_NO_UTF_CHECK) != 0)? " no_utf_check" : "",
+ ((options & PCRE2_NOTBOL) != 0)? " notbol" : "",
+ ((options & PCRE2_NOTEMPTY) != 0)? " notempty" : "",
+ ((options & PCRE2_NOTEMPTY_ATSTART) != 0)? " notempty_atstart" : "",
+ ((options & PCRE2_NOTEOL) != 0)? " noteol" : "",
+ ((options & PCRE2_PARTIAL_HARD) != 0)? " partial_hard" : "",
+ ((options & PCRE2_PARTIAL_SOFT) != 0)? " partial_soft" : "");
+}
+#endif /* SUPPORT_PCRE2_8 */
+
+
+
+/*************************************************
+* Show memory usage info for a pattern *
+*************************************************/
+
+static void
+show_memory_info(void)
+{
+uint32_t name_count, name_entry_size;
+size_t size, cblock_size;
+
+/* One of the test_mode values will always be true, but to stop a compiler
+warning we must initialize cblock_size. */
+
+cblock_size = 0;
+#ifdef SUPPORT_PCRE2_8
+if (test_mode == PCRE8_MODE) cblock_size = sizeof(pcre2_real_code_8);
+#endif
+#ifdef SUPPORT_PCRE2_16
+if (test_mode == PCRE16_MODE) cblock_size = sizeof(pcre2_real_code_16);
+#endif
+#ifdef SUPPORT_PCRE2_32
+if (test_mode == PCRE32_MODE) cblock_size = sizeof(pcre2_real_code_32);
+#endif
+
+(void)pattern_info(PCRE2_INFO_SIZE, &size, FALSE);
+(void)pattern_info(PCRE2_INFO_NAMECOUNT, &name_count, FALSE);
+(void)pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &name_entry_size, FALSE);
+fprintf(outfile, "Memory allocation (code space): %d\n",
+ (int)(size - name_count*name_entry_size*code_unit_size - cblock_size));
+if (pat_patctl.jit != 0)
+ {
+ (void)pattern_info(PCRE2_INFO_JITSIZE, &size, FALSE);
+ fprintf(outfile, "Memory allocation (JIT code): %d\n", (int)size);
+ }
+}
+
+
+
+/*************************************************
+* Show frame size info for a pattern *
+*************************************************/
+
+static void
+show_framesize(void)
+{
+size_t frame_size;
+(void)pattern_info(PCRE2_INFO_FRAMESIZE, &frame_size, FALSE);
+fprintf(outfile, "Frame size for pcre2_match(): %d\n", (int)frame_size);
+}
+
+
+
+/*************************************************
+* Get and output an error message *
+*************************************************/
+
+static BOOL
+print_error_message(int errorcode, const char *before, const char *after)
+{
+int len;
+PCRE2_GET_ERROR_MESSAGE(len, errorcode, pbuffer);
+if (len < 0)
+ {
+ fprintf(outfile, "\n** pcre2test internal error: cannot interpret error "
+ "number\n** Unexpected return (%d) from pcre2_get_error_message()\n", len);
+ }
+else
+ {
+ fprintf(outfile, "%s", before);
+ PCHARSV(CASTVAR(void *, pbuffer), 0, len, FALSE, outfile);
+ fprintf(outfile, "%s", after);
+ }
+return len >= 0;
+}
+
+
+/*************************************************
+* Callback function for callout enumeration *
+*************************************************/
+
+/* The only differences in the callout emumeration block for different code
+unit widths are that the pointers to the subject, the most recent MARK, and a
+callout argument string point to strings of the appropriate width. Casts can be
+used to deal with this.
+
+Argument:
+ cb pointer to enumerate block
+ callout_data user data
+
+Returns: 0
+*/
+
+static int callout_callback(pcre2_callout_enumerate_block_8 *cb,
+ void *callout_data)
+{
+uint32_t i;
+BOOL utf = (FLD(compiled_code, overall_options) & PCRE2_UTF) != 0;
+
+(void)callout_data; /* Not currently displayed */
+
+fprintf(outfile, "Callout ");
+if (cb->callout_string != NULL)
+ {
+ uint32_t delimiter = CODE_UNIT(cb->callout_string, -1);
+ fprintf(outfile, "%c", delimiter);
+ PCHARSV(cb->callout_string, 0,
+ cb->callout_string_length, utf, outfile);
+ for (i = 0; callout_start_delims[i] != 0; i++)
+ if (delimiter == callout_start_delims[i])
+ {
+ delimiter = callout_end_delims[i];
+ break;
+ }
+ fprintf(outfile, "%c ", delimiter);
+ }
+else fprintf(outfile, "%d ", cb->callout_number);
+
+fprintf(outfile, "%.*s\n",
+ (int)((cb->next_item_length == 0)? 1 : cb->next_item_length),
+ pbuffer8 + cb->pattern_position);
+
+return 0;
+}
+
+
+
+/*************************************************
+* Show information about a pattern *
+*************************************************/
+
+/* This function is called after a pattern has been compiled if any of the
+information-requesting controls have been set.
+
+Arguments: none
+
+Returns: PR_OK continue processing next line
+ PR_SKIP skip to a blank line
+ PR_ABEND abort the pcre2test run
+*/
+
+static int
+show_pattern_info(void)
+{
+uint32_t compile_options, overall_options, extra_options;
+
+if ((pat_patctl.control & (CTL_BINCODE|CTL_FULLBINCODE)) != 0)
+ {
+ fprintf(outfile, "------------------------------------------------------------------\n");
+ PCRE2_PRINTINT((pat_patctl.control & CTL_FULLBINCODE) != 0);
+ }
+
+if ((pat_patctl.control & CTL_INFO) != 0)
+ {
+ int rc;
+ void *nametable;
+ uint8_t *start_bits;
+ BOOL heap_limit_set, match_limit_set, depth_limit_set;
+ uint32_t backrefmax, bsr_convention, capture_count, first_ctype, first_cunit,
+ hasbackslashc, hascrorlf, jchanged, last_ctype, last_cunit, match_empty,
+ depth_limit, heap_limit, match_limit, minlength, nameentrysize, namecount,
+ newline_convention;
+
+ /* Exercise the error route. */
+
+ PCRE2_PATTERN_INFO(rc, compiled_code, 999, NULL);
+ (void)rc;
+
+ /* These info requests may return PCRE2_ERROR_UNSET. */
+
+ switch(pattern_info(PCRE2_INFO_HEAPLIMIT, &heap_limit, TRUE))
+ {
+ case 0:
+ heap_limit_set = TRUE;
+ break;
+
+ case PCRE2_ERROR_UNSET:
+ heap_limit_set = FALSE;
+ break;
+
+ default:
+ return PR_ABEND;
+ }
+
+ switch(pattern_info(PCRE2_INFO_MATCHLIMIT, &match_limit, TRUE))
+ {
+ case 0:
+ match_limit_set = TRUE;
+ break;
+
+ case PCRE2_ERROR_UNSET:
+ match_limit_set = FALSE;
+ break;
+
+ default:
+ return PR_ABEND;
+ }
+
+ switch(pattern_info(PCRE2_INFO_DEPTHLIMIT, &depth_limit, TRUE))
+ {
+ case 0:
+ depth_limit_set = TRUE;
+ break;
+
+ case PCRE2_ERROR_UNSET:
+ depth_limit_set = FALSE;
+ break;
+
+ default:
+ return PR_ABEND;
+ }
+
+ /* These info requests should always succeed. */
+
+ if (pattern_info(PCRE2_INFO_BACKREFMAX, &backrefmax, FALSE) +
+ pattern_info(PCRE2_INFO_BSR, &bsr_convention, FALSE) +
+ pattern_info(PCRE2_INFO_CAPTURECOUNT, &capture_count, FALSE) +
+ pattern_info(PCRE2_INFO_FIRSTBITMAP, &start_bits, FALSE) +
+ pattern_info(PCRE2_INFO_FIRSTCODEUNIT, &first_cunit, FALSE) +
+ pattern_info(PCRE2_INFO_FIRSTCODETYPE, &first_ctype, FALSE) +
+ pattern_info(PCRE2_INFO_HASBACKSLASHC, &hasbackslashc, FALSE) +
+ pattern_info(PCRE2_INFO_HASCRORLF, &hascrorlf, FALSE) +
+ pattern_info(PCRE2_INFO_JCHANGED, &jchanged, FALSE) +
+ pattern_info(PCRE2_INFO_LASTCODEUNIT, &last_cunit, FALSE) +
+ pattern_info(PCRE2_INFO_LASTCODETYPE, &last_ctype, FALSE) +
+ pattern_info(PCRE2_INFO_MATCHEMPTY, &match_empty, FALSE) +
+ pattern_info(PCRE2_INFO_MINLENGTH, &minlength, FALSE) +
+ pattern_info(PCRE2_INFO_NAMECOUNT, &namecount, FALSE) +
+ pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &nameentrysize, FALSE) +
+ pattern_info(PCRE2_INFO_NAMETABLE, &nametable, FALSE) +
+ pattern_info(PCRE2_INFO_NEWLINE, &newline_convention, FALSE)
+ != 0)
+ return PR_ABEND;
+
+ fprintf(outfile, "Capturing subpattern count = %d\n", capture_count);
+
+ if (backrefmax > 0)
+ fprintf(outfile, "Max back reference = %d\n", backrefmax);
+
+ if (maxlookbehind > 0)
+ fprintf(outfile, "Max lookbehind = %d\n", maxlookbehind);
+
+ if (heap_limit_set)
+ fprintf(outfile, "Heap limit = %u\n", heap_limit);
+
+ if (match_limit_set)
+ fprintf(outfile, "Match limit = %u\n", match_limit);
+
+ if (depth_limit_set)
+ fprintf(outfile, "Depth limit = %u\n", depth_limit);
+
+ if (namecount > 0)
+ {
+ fprintf(outfile, "Named capturing subpatterns:\n");
+ for (; namecount > 0; namecount--)
+ {
+ int imm2_size = test_mode == PCRE8_MODE ? 2 : 1;
+ uint32_t length = (uint32_t)STRLEN(nametable + imm2_size);
+ fprintf(outfile, " ");
+ PCHARSV(nametable, imm2_size, length, FALSE, outfile);
+ while (length++ < nameentrysize - imm2_size) putc(' ', outfile);
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE)
+ fprintf(outfile, "%3d\n", (int)(((PCRE2_SPTR32)nametable)[0]));
+#endif
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)
+ fprintf(outfile, "%3d\n", (int)(((PCRE2_SPTR16)nametable)[0]));
+#endif
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE)
+ fprintf(outfile, "%3d\n", (int)(
+ ((((PCRE2_SPTR8)nametable)[0]) << 8) | ((PCRE2_SPTR8)nametable)[1]));
+#endif
+ nametable = (void*)((PCRE2_SPTR8)nametable + nameentrysize * code_unit_size);
+ }
+ }
+
+ if (hascrorlf) fprintf(outfile, "Contains explicit CR or LF match\n");
+ if (hasbackslashc) fprintf(outfile, "Contains \\C\n");
+ if (match_empty) fprintf(outfile, "May match empty string\n");
+
+ pattern_info(PCRE2_INFO_ARGOPTIONS, &compile_options, FALSE);
+ pattern_info(PCRE2_INFO_ALLOPTIONS, &overall_options, FALSE);
+ pattern_info(PCRE2_INFO_EXTRAOPTIONS, &extra_options, FALSE);
+
+ /* Remove UTF/UCP if they were there only because of forbid_utf. This saves
+ cluttering up the verification output of non-UTF test files. */
+
+ if ((pat_patctl.options & PCRE2_NEVER_UTF) == 0)
+ {
+ compile_options &= ~PCRE2_NEVER_UTF;
+ overall_options &= ~PCRE2_NEVER_UTF;
+ }
+
+ if ((pat_patctl.options & PCRE2_NEVER_UCP) == 0)
+ {
+ compile_options &= ~PCRE2_NEVER_UCP;
+ overall_options &= ~PCRE2_NEVER_UCP;
+ }
+
+ if ((compile_options|overall_options) != 0)
+ {
+ if (compile_options == overall_options)
+ show_compile_options(compile_options, "Options:", "\n");
+ else
+ {
+ show_compile_options(compile_options, "Compile options:", "\n");
+ show_compile_options(overall_options, "Overall options:", "\n");
+ }
+ }
+
+ if (extra_options != 0)
+ show_compile_extra_options(extra_options, "Extra options:", "\n");
+
+ if (jchanged) fprintf(outfile, "Duplicate name status changes\n");
+
+ if ((pat_patctl.control2 & CTL2_BSR_SET) != 0 ||
+ (FLD(compiled_code, flags) & PCRE2_BSR_SET) != 0)
+ fprintf(outfile, "\\R matches %s\n", (bsr_convention == PCRE2_BSR_UNICODE)?
+ "any Unicode newline" : "CR, LF, or CRLF");
+
+ if ((FLD(compiled_code, flags) & PCRE2_NL_SET) != 0)
+ {
+ switch (newline_convention)
+ {
+ case PCRE2_NEWLINE_CR:
+ fprintf(outfile, "Forced newline is CR\n");
+ break;
+
+ case PCRE2_NEWLINE_LF:
+ fprintf(outfile, "Forced newline is LF\n");
+ break;
+
+ case PCRE2_NEWLINE_CRLF:
+ fprintf(outfile, "Forced newline is CRLF\n");
+ break;
+
+ case PCRE2_NEWLINE_ANYCRLF:
+ fprintf(outfile, "Forced newline is CR, LF, or CRLF\n");
+ break;
+
+ case PCRE2_NEWLINE_ANY:
+ fprintf(outfile, "Forced newline is any Unicode newline\n");
+ break;
+
+ case PCRE2_NEWLINE_NUL:
+ fprintf(outfile, "Forced newline is NUL\n");
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ if (first_ctype == 2)
+ {
+ fprintf(outfile, "First code unit at start or follows newline\n");
+ }
+ else if (first_ctype == 1)
+ {
+ const char *caseless =
+ ((FLD(compiled_code, flags) & PCRE2_FIRSTCASELESS) == 0)?
+ "" : " (caseless)";
+ if (PRINTOK(first_cunit))
+ fprintf(outfile, "First code unit = \'%c\'%s\n", first_cunit, caseless);
+ else
+ {
+ fprintf(outfile, "First code unit = ");
+ pchar(first_cunit, FALSE, outfile);
+ fprintf(outfile, "%s\n", caseless);
+ }
+ }
+ else if (start_bits != NULL)
+ {
+ int i;
+ int c = 24;
+ fprintf(outfile, "Starting code units: ");
+ for (i = 0; i < 256; i++)
+ {
+ if ((start_bits[i/8] & (1<<(i&7))) != 0)
+ {
+ if (c > 75)
+ {
+ fprintf(outfile, "\n ");
+ c = 2;
+ }
+ if (PRINTOK(i) && i != ' ')
+ {
+ fprintf(outfile, "%c ", i);
+ c += 2;
+ }
+ else
+ {
+ fprintf(outfile, "\\x%02x ", i);
+ c += 5;
+ }
+ }
+ }
+ fprintf(outfile, "\n");
+ }
+
+ if (last_ctype != 0)
+ {
+ const char *caseless =
+ ((FLD(compiled_code, flags) & PCRE2_LASTCASELESS) == 0)?
+ "" : " (caseless)";
+ if (PRINTOK(last_cunit))
+ fprintf(outfile, "Last code unit = \'%c\'%s\n", last_cunit, caseless);
+ else
+ {
+ fprintf(outfile, "Last code unit = ");
+ pchar(last_cunit, FALSE, outfile);
+ fprintf(outfile, "%s\n", caseless);
+ }
+ }
+
+ fprintf(outfile, "Subject length lower bound = %d\n", minlength);
+
+ if (pat_patctl.jit != 0 && (pat_patctl.control & CTL_JITVERIFY) != 0)
+ {
+ if (FLD(compiled_code, executable_jit) != NULL)
+ fprintf(outfile, "JIT compilation was successful\n");
+ else
+ {
+#ifdef SUPPORT_JIT
+ fprintf(outfile, "JIT compilation was not successful");
+ if (jitrc != 0 && !print_error_message(jitrc, " (", ")"))
+ return PR_ABEND;
+ fprintf(outfile, "\n");
+#else
+ fprintf(outfile, "JIT support is not available in this version of PCRE2\n");
+#endif
+ }
+ }
+ }
+
+if ((pat_patctl.control & CTL_CALLOUT_INFO) != 0)
+ {
+ int errorcode;
+ PCRE2_CALLOUT_ENUMERATE(errorcode, callout_callback, 0);
+ if (errorcode != 0)
+ {
+ fprintf(outfile, "Callout enumerate failed: error %d: ", errorcode);
+ if (errorcode < 0 && !print_error_message(errorcode, "", "\n"))
+ return PR_ABEND;
+ return PR_SKIP;
+ }
+ }
+
+return PR_OK;
+}
+
+
+
+/*************************************************
+* Handle serialization error *
+*************************************************/
+
+/* Print an error message after a serialization failure.
+
+Arguments:
+ rc the error code
+ msg an initial message for what failed
+
+Returns: FALSE if print_error_message() fails
+*/
+
+static BOOL
+serial_error(int rc, const char *msg)
+{
+fprintf(outfile, "%s failed: error %d: ", msg, rc);
+return print_error_message(rc, "", "\n");
+}
+
+
+
+/*************************************************
+* Open file for save/load commands *
+*************************************************/
+
+/* This function decodes the file name and opens the file.
+
+Arguments:
+ buffptr point after the #command
+ mode open mode
+ fptr points to the FILE variable
+
+Returns: PR_OK or PR_ABEND
+*/
+
+static int
+open_file(uint8_t *buffptr, const char *mode, FILE **fptr)
+{
+char *endf;
+char *filename = (char *)buffptr;
+while (isspace(*filename)) filename++;
+endf = filename + strlen8(filename);
+while (endf > filename && isspace(endf[-1])) endf--;
+
+if (endf == filename)
+ {
+ fprintf(outfile, "** File name expected after #save\n");
+ return PR_ABEND;
+ }
+
+*endf = 0;
+*fptr = fopen((const char *)filename, mode);
+if (*fptr == NULL)
+ {
+ fprintf(outfile, "** Failed to open '%s': %s\n", filename, strerror(errno));
+ return PR_ABEND;
+ }
+
+return PR_OK;
+}
+
+
+
+/*************************************************
+* Process command line *
+*************************************************/
+
+/* This function is called for lines beginning with # and a character that is
+not ! or whitespace, when encountered between tests, which means that there is
+no compiled pattern (compiled_code is NULL). The line is in buffer.
+
+Arguments: none
+
+Returns: PR_OK continue processing next line
+ PR_SKIP skip to a blank line
+ PR_ABEND abort the pcre2test run
+*/
+
+static int
+process_command(void)
+{
+FILE *f;
+PCRE2_SIZE serial_size;
+size_t i;
+int rc, cmd, cmdlen, yield;
+uint16_t first_listed_newline;
+const char *cmdname;
+uint8_t *argptr, *serial;
+
+yield = PR_OK;
+cmd = CMD_UNKNOWN;
+cmdlen = 0;
+
+for (i = 0; i < cmdlistcount; i++)
+ {
+ cmdname = cmdlist[i].name;
+ cmdlen = strlen(cmdname);
+ if (strncmp((char *)(buffer+1), cmdname, cmdlen) == 0 &&
+ isspace(buffer[cmdlen+1]))
+ {
+ cmd = cmdlist[i].value;
+ break;
+ }
+ }
+
+argptr = buffer + cmdlen + 1;
+
+if (restrict_for_perl_test && cmd != CMD_PATTERN && cmd != CMD_SUBJECT)
+ {
+ fprintf(outfile, "** #%s is not allowed after #perltest\n", cmdname);
+ return PR_ABEND;
+ }
+
+switch(cmd)
+ {
+ case CMD_UNKNOWN:
+ fprintf(outfile, "** Unknown command: %s", buffer);
+ break;
+
+ case CMD_FORBID_UTF:
+ forbid_utf = PCRE2_NEVER_UTF|PCRE2_NEVER_UCP;
+ break;
+
+ case CMD_PERLTEST:
+ restrict_for_perl_test = TRUE;
+ break;
+
+ /* Set default pattern modifiers */
+
+ case CMD_PATTERN:
+ (void)decode_modifiers(argptr, CTX_DEFPAT, &def_patctl, NULL);
+ if (def_patctl.jit == 0 && (def_patctl.control & CTL_JITVERIFY) != 0)
+ def_patctl.jit = 7;
+ break;
+
+ /* Set default subject modifiers */
+
+ case CMD_SUBJECT:
+ (void)decode_modifiers(argptr, CTX_DEFDAT, NULL, &def_datctl);
+ break;
+
+ /* Check the default newline, and if not one of those listed, set up the
+ first one to be forced. An empty list unsets. */
+
+ case CMD_NEWLINE_DEFAULT:
+ local_newline_default = 0; /* Unset */
+ first_listed_newline = 0;
+ for (;;)
+ {
+ while (isspace(*argptr)) argptr++;
+ if (*argptr == 0) break;
+ for (i = 1; i < sizeof(newlines)/sizeof(char *); i++)
+ {
+ size_t nlen = strlen(newlines[i]);
+ if (strncmpic(argptr, (const uint8_t *)newlines[i], nlen) == 0 &&
+ isspace(argptr[nlen]))
+ {
+ if (i == NEWLINE_DEFAULT) return PR_OK; /* Default is valid */
+ if (first_listed_newline == 0) first_listed_newline = i;
+ }
+ }
+ while (*argptr != 0 && !isspace(*argptr)) argptr++;
+ }
+ local_newline_default = first_listed_newline;
+ break;
+
+ /* Pop or copy a compiled pattern off the stack. Modifiers that do not affect
+ the compiled pattern (e.g. to give information) are permitted. The default
+ pattern modifiers are ignored. */
+
+ case CMD_POP:
+ case CMD_POPCOPY:
+ if (patstacknext <= 0)
+ {
+ fprintf(outfile, "** Can't pop off an empty stack\n");
+ return PR_SKIP;
+ }
+ memset(&pat_patctl, 0, sizeof(patctl)); /* Completely unset */
+ if (!decode_modifiers(argptr, CTX_POPPAT, &pat_patctl, NULL))
+ return PR_SKIP;
+
+ if (cmd == CMD_POP)
+ {
+ SET(compiled_code, patstack[--patstacknext]);
+ }
+ else
+ {
+ PCRE2_CODE_COPY_FROM_VOID(compiled_code, patstack[patstacknext - 1]);
+ }
+
+ if (pat_patctl.jit != 0)
+ {
+ PCRE2_JIT_COMPILE(jitrc, compiled_code, pat_patctl.jit);
+ }
+ if ((pat_patctl.control & CTL_MEMORY) != 0) show_memory_info();
+ if ((pat_patctl.control & CTL_FRAMESIZE) != 0) show_framesize();
+ if ((pat_patctl.control & CTL_ANYINFO) != 0)
+ {
+ rc = show_pattern_info();
+ if (rc != PR_OK) return rc;
+ }
+ break;
+
+ /* Save the stack of compiled patterns to a file, then empty the stack. */
+
+ case CMD_SAVE:
+ if (patstacknext <= 0)
+ {
+ fprintf(outfile, "** No stacked patterns to save\n");
+ return PR_OK;
+ }
+
+ rc = open_file(argptr+1, BINARY_OUTPUT_MODE, &f);
+ if (rc != PR_OK) return rc;
+
+ PCRE2_SERIALIZE_ENCODE(rc, patstack, patstacknext, &serial, &serial_size,
+ general_context);
+ if (rc < 0)
+ {
+ fclose(f);
+ if (!serial_error(rc, "Serialization")) return PR_ABEND;
+ break;
+ }
+
+ /* Write the length at the start of the file to make it straightforward to
+ get the right memory when re-loading. This saves having to read the file size
+ in different operating systems. To allow for different endianness (even
+ though reloading with the opposite endianness does not work), write the
+ length byte-by-byte. */
+
+ for (i = 0; i < 4; i++) fputc((serial_size >> (i*8)) & 255, f);
+ if (fwrite(serial, 1, serial_size, f) != serial_size)
+ {
+ fprintf(outfile, "** Wrong return from fwrite()\n");
+ fclose(f);
+ return PR_ABEND;
+ }
+
+ fclose(f);
+ PCRE2_SERIALIZE_FREE(serial);
+ while(patstacknext > 0)
+ {
+ SET(compiled_code, patstack[--patstacknext]);
+ SUB1(pcre2_code_free, compiled_code);
+ }
+ SET(compiled_code, NULL);
+ break;
+
+ /* Load a set of compiled patterns from a file onto the stack */
+
+ case CMD_LOAD:
+ rc = open_file(argptr+1, BINARY_INPUT_MODE, &f);
+ if (rc != PR_OK) return rc;
+
+ serial_size = 0;
+ for (i = 0; i < 4; i++) serial_size |= fgetc(f) << (i*8);
+
+ serial = malloc(serial_size);
+ if (serial == NULL)
+ {
+ fprintf(outfile, "** Failed to get memory (size %" SIZ_FORM ") for #load\n",
+ SIZ_CAST serial_size);
+ fclose(f);
+ return PR_ABEND;
+ }
+
+ i = fread(serial, 1, serial_size, f);
+ fclose(f);
+
+ if (i != serial_size)
+ {
+ fprintf(outfile, "** Wrong return from fread()\n");
+ yield = PR_ABEND;
+ }
+ else
+ {
+ PCRE2_SERIALIZE_GET_NUMBER_OF_CODES(rc, serial);
+ if (rc < 0)
+ {
+ if (!serial_error(rc, "Get number of codes")) yield = PR_ABEND;
+ }
+ else
+ {
+ if (rc + patstacknext > PATSTACKSIZE)
+ {
+ fprintf(outfile, "** Not enough space on pattern stack for %d pattern%s\n",
+ rc, (rc == 1)? "" : "s");
+ rc = PATSTACKSIZE - patstacknext;
+ fprintf(outfile, "** Decoding %d pattern%s\n", rc,
+ (rc == 1)? "" : "s");
+ }
+ PCRE2_SERIALIZE_DECODE(rc, patstack + patstacknext, rc, serial,
+ general_context);
+ if (rc < 0)
+ {
+ if (!serial_error(rc, "Deserialization")) yield = PR_ABEND;
+ }
+ else patstacknext += rc;
+ }
+ }
+
+ free(serial);
+ break;
+ }
+
+return yield;
+}
+
+
+
+/*************************************************
+* Process pattern line *
+*************************************************/
+
+/* This function is called when the input buffer contains the start of a
+pattern. The first character is known to be a valid delimiter. The pattern is
+read, modifiers are interpreted, and a suitable local context is set up for
+this test. The pattern is then compiled.
+
+Arguments: none
+
+Returns: PR_OK continue processing next line
+ PR_SKIP skip to a blank line
+ PR_ABEND abort the pcre2test run
+*/
+
+static int
+process_pattern(void)
+{
+BOOL utf;
+uint32_t k;
+uint8_t *p = buffer;
+unsigned int delimiter = *p++;
+int errorcode;
+void *use_pat_context;
+uint32_t use_forbid_utf = forbid_utf;
+PCRE2_SIZE patlen;
+PCRE2_SIZE valgrind_access_length;
+PCRE2_SIZE erroroffset;
+
+/* Initialize the context and pattern/data controls for this test from the
+defaults. */
+
+PATCTXCPY(pat_context, default_pat_context);
+memcpy(&pat_patctl, &def_patctl, sizeof(patctl));
+
+/* Find the end of the pattern, reading more lines if necessary. */
+
+for(;;)
+ {
+ while (*p != 0)
+ {
+ if (*p == '\\' && p[1] != 0) p++;
+ else if (*p == delimiter) break;
+ p++;
+ }
+ if (*p != 0) break;
+ if ((p = extend_inputline(infile, p, " > ")) == NULL)
+ {
+ fprintf(outfile, "** Unexpected EOF\n");
+ return PR_ABEND;
+ }
+ if (!INTERACTIVE(infile)) fprintf(outfile, "%s", (char *)p);
+ }
+
+/* If the first character after the delimiter is backslash, make the pattern
+end with backslash. This is purely to provide a way of testing for the error
+message when a pattern ends with backslash. */
+
+if (p[1] == '\\') *p++ = '\\';
+
+/* Terminate the pattern at the delimiter, and compute the length. */
+
+*p++ = 0;
+patlen = p - buffer - 2;
+
+/* Look for modifiers and options after the final delimiter. */
+
+if (!decode_modifiers(p, CTX_PAT, &pat_patctl, NULL)) return PR_SKIP;
+utf = (pat_patctl.options & PCRE2_UTF) != 0;
+
+/* The utf8_input modifier is not allowed in 8-bit mode, and is mutually
+exclusive with the utf modifier. */
+
+if ((pat_patctl.control & CTL_UTF8_INPUT) != 0)
+ {
+ if (test_mode == PCRE8_MODE)
+ {
+ fprintf(outfile, "** The utf8_input modifier is not allowed in 8-bit mode\n");
+ return PR_SKIP;
+ }
+ if (utf)
+ {
+ fprintf(outfile, "** The utf and utf8_input modifiers are mutually exclusive\n");
+ return PR_SKIP;
+ }
+ }
+
+/* The convert and posix modifiers are mutually exclusive. */
+
+if (pat_patctl.convert_type != CONVERT_UNSET &&
+ (pat_patctl.control & CTL_POSIX) != 0)
+ {
+ fprintf(outfile, "** The convert and posix modifiers are mutually exclusive\n");
+ return PR_SKIP;
+ }
+
+/* Check for mutually exclusive control modifiers. At present, these are all in
+the first control word. */
+
+for (k = 0; k < sizeof(exclusive_pat_controls)/sizeof(uint32_t); k++)
+ {
+ uint32_t c = pat_patctl.control & exclusive_pat_controls[k];
+ if (c != 0 && c != (c & (~c+1)))
+ {
+ show_controls(c, 0, "** Not allowed together:");
+ fprintf(outfile, "\n");
+ return PR_SKIP;
+ }
+ }
+
+/* Assume full JIT compile for jitverify and/or jitfast if nothing else was
+specified. */
+
+if (pat_patctl.jit == 0 &&
+ (pat_patctl.control & (CTL_JITVERIFY|CTL_JITFAST)) != 0)
+ pat_patctl.jit = 7;
+
+/* Now copy the pattern to pbuffer8 for use in 8-bit testing and for reflecting
+in callouts. Convert from hex if requested (literal strings in quotes may be
+present within the hexadecimal pairs). The result must necessarily be fewer
+characters so will always fit in pbuffer8. */
+
+if ((pat_patctl.control & CTL_HEXPAT) != 0)
+ {
+ uint8_t *pp, *pt;
+ uint32_t c, d;
+
+ pt = pbuffer8;
+ for (pp = buffer + 1; *pp != 0; pp++)
+ {
+ if (isspace(*pp)) continue;
+ c = *pp++;
+
+ /* Handle a literal substring */
+
+ if (c == '\'' || c == '"')
+ {
+ uint8_t *pq = pp;
+ for (;; pp++)
+ {
+ d = *pp;
+ if (d == 0)
+ {
+ fprintf(outfile, "** Missing closing quote in hex pattern: "
+ "opening quote is at offset %" PTR_FORM ".\n", pq - buffer - 2);
+ return PR_SKIP;
+ }
+ if (d == c) break;
+ *pt++ = d;
+ }
+ }
+
+ /* Expect a hex pair */
+
+ else
+ {
+ if (!isxdigit(c))
+ {
+ fprintf(outfile, "** Unexpected non-hex-digit '%c' at offset %"
+ PTR_FORM " in hex pattern: quote missing?\n", c, pp - buffer - 2);
+ return PR_SKIP;
+ }
+ if (*pp == 0)
+ {
+ fprintf(outfile, "** Odd number of digits in hex pattern\n");
+ return PR_SKIP;
+ }
+ d = *pp;
+ if (!isxdigit(d))
+ {
+ fprintf(outfile, "** Unexpected non-hex-digit '%c' at offset %"
+ PTR_FORM " in hex pattern: quote missing?\n", d, pp - buffer - 1);
+ return PR_SKIP;
+ }
+ c = toupper(c);
+ d = toupper(d);
+ *pt++ = ((isdigit(c)? (c - '0') : (c - 'A' + 10)) << 4) +
+ (isdigit(d)? (d - '0') : (d - 'A' + 10));
+ }
+ }
+ *pt = 0;
+ patlen = pt - pbuffer8;
+ }
+
+/* If not a hex string, process for repetition expansion if requested. */
+
+else if ((pat_patctl.control & CTL_EXPAND) != 0)
+ {
+ uint8_t *pp, *pt;
+
+ pt = pbuffer8;
+ for (pp = buffer + 1; *pp != 0; pp++)
+ {
+ uint8_t *pc = pp;
+ uint32_t count = 1;
+ size_t length = 1;
+
+ /* Check for replication syntax; if not found, the defaults just set will
+ prevail and one character will be copied. */
+
+ if (pp[0] == '\\' && pp[1] == '[')
+ {
+ uint8_t *pe;
+ for (pe = pp + 2; *pe != 0; pe++)
+ {
+ if (pe[0] == ']' && pe[1] == '{')
+ {
+ uint32_t clen = pe - pc - 2;
+ uint32_t i = 0;
+ unsigned long uli;
+ char *endptr;
+
+ pe += 2;
+ uli = strtoul((const char *)pe, &endptr, 10);
+ if (U32OVERFLOW(uli))
+ {
+ fprintf(outfile, "** Pattern repeat count too large\n");
+ return PR_SKIP;
+ }
+
+ i = (uint32_t)uli;
+ pe = (uint8_t *)endptr;
+ if (*pe == '}')
+ {
+ if (i == 0)
+ {
+ fprintf(outfile, "** Zero repeat not allowed\n");
+ return PR_SKIP;
+ }
+ pc += 2;
+ count = i;
+ length = clen;
+ pp = pe;
+ break;
+ }
+ }
+ }
+ }
+
+ /* Add to output. If the buffer is too small expand it. The function for
+ expanding buffers always keeps buffer and pbuffer8 in step as far as their
+ size goes. */
+
+ while (pt + count * length > pbuffer8 + pbuffer8_size)
+ {
+ size_t pc_offset = pc - buffer;
+ size_t pp_offset = pp - buffer;
+ size_t pt_offset = pt - pbuffer8;
+ expand_input_buffers();
+ pc = buffer + pc_offset;
+ pp = buffer + pp_offset;
+ pt = pbuffer8 + pt_offset;
+ }
+
+ for (; count > 0; count--)
+ {
+ memcpy(pt, pc, length);
+ pt += length;
+ }
+ }
+
+ *pt = 0;
+ patlen = pt - pbuffer8;
+
+ if ((pat_patctl.control & CTL_INFO) != 0)
+ fprintf(outfile, "Expanded: %s\n", pbuffer8);
+ }
+
+/* Neither hex nor expanded, just copy the input verbatim. */
+
+else
+ {
+ strncpy((char *)pbuffer8, (char *)(buffer+1), patlen + 1);
+ }
+
+/* Sort out character tables */
+
+if (pat_patctl.locale[0] != 0)
+ {
+ if (pat_patctl.tables_id != 0)
+ {
+ fprintf(outfile, "** 'Locale' and 'tables' must not both be set\n");
+ return PR_SKIP;
+ }
+ if (setlocale(LC_CTYPE, (const char *)pat_patctl.locale) == NULL)
+ {
+ fprintf(outfile, "** Failed to set locale '%s'\n", pat_patctl.locale);
+ return PR_SKIP;
+ }
+ if (strcmp((const char *)pat_patctl.locale, (const char *)locale_name) != 0)
+ {
+ strcpy((char *)locale_name, (char *)pat_patctl.locale);
+ if (locale_tables != NULL) free((void *)locale_tables);
+ PCRE2_MAKETABLES(locale_tables);
+ }
+ use_tables = locale_tables;
+ }
+
+else switch (pat_patctl.tables_id)
+ {
+ case 0: use_tables = NULL; break;
+ case 1: use_tables = tables1; break;
+ case 2: use_tables = tables2; break;
+ default:
+ fprintf(outfile, "** 'Tables' must specify 0, 1, or 2.\n");
+ return PR_SKIP;
+ }
+
+PCRE2_SET_CHARACTER_TABLES(pat_context, use_tables);
+
+/* Set up for the stackguard test. */
+
+if (pat_patctl.stackguard_test != 0)
+ {
+ PCRE2_SET_COMPILE_RECURSION_GUARD(pat_context, stack_guard, NULL);
+ }
+
+/* Handle compiling via the POSIX interface, which doesn't support the
+timing, showing, or debugging options, nor the ability to pass over
+local character tables. Neither does it have 16-bit or 32-bit support. */
+
+if ((pat_patctl.control & CTL_POSIX) != 0)
+ {
+#ifdef SUPPORT_PCRE2_8
+ int rc;
+ int cflags = 0;
+ const char *msg = "** Ignored with POSIX interface:";
+#endif
+
+ if (test_mode != PCRE8_MODE)
+ {
+ fprintf(outfile, "** The POSIX interface is available only in 8-bit mode\n");
+ return PR_SKIP;
+ }
+
+#ifdef SUPPORT_PCRE2_8
+ /* Check for features that the POSIX interface does not support. */
+
+ if (pat_patctl.locale[0] != 0) prmsg(&msg, "locale");
+ if (pat_patctl.replacement[0] != 0) prmsg(&msg, "replace");
+ if (pat_patctl.tables_id != 0) prmsg(&msg, "tables");
+ if (pat_patctl.stackguard_test != 0) prmsg(&msg, "stackguard");
+ if (timeit > 0) prmsg(&msg, "timing");
+ if (pat_patctl.jit != 0) prmsg(&msg, "JIT");
+
+ if ((pat_patctl.options & ~POSIX_SUPPORTED_COMPILE_OPTIONS) != 0)
+ {
+ show_compile_options(
+ pat_patctl.options & ~POSIX_SUPPORTED_COMPILE_OPTIONS, msg, "");
+ msg = "";
+ }
+
+ if ((FLD(pat_context, extra_options) &
+ ~POSIX_SUPPORTED_COMPILE_EXTRA_OPTIONS) != 0)
+ {
+ show_compile_extra_options(
+ FLD(pat_context, extra_options) & ~POSIX_SUPPORTED_COMPILE_EXTRA_OPTIONS,
+ msg, "");
+ msg = "";
+ }
+
+ if ((pat_patctl.control & ~POSIX_SUPPORTED_COMPILE_CONTROLS) != 0 ||
+ (pat_patctl.control2 & ~POSIX_SUPPORTED_COMPILE_CONTROLS2) != 0)
+ {
+ show_controls(pat_patctl.control & ~POSIX_SUPPORTED_COMPILE_CONTROLS,
+ pat_patctl.control2 & ~POSIX_SUPPORTED_COMPILE_CONTROLS2, msg);
+ msg = "";
+ }
+
+ if (local_newline_default != 0) prmsg(&msg, "#newline_default");
+ if (FLD(pat_context, max_pattern_length) != PCRE2_UNSET)
+ prmsg(&msg, "max_pattern_length");
+ if (FLD(pat_context, parens_nest_limit) != PARENS_NEST_DEFAULT)
+ prmsg(&msg, "parens_nest_limit");
+
+ if (msg[0] == 0) fprintf(outfile, "\n");
+
+ /* Translate PCRE2 options to POSIX options and then compile. */
+
+ if (utf) cflags |= REG_UTF;
+ if ((pat_patctl.control & CTL_POSIX_NOSUB) != 0) cflags |= REG_NOSUB;
+ if ((pat_patctl.options & PCRE2_UCP) != 0) cflags |= REG_UCP;
+ if ((pat_patctl.options & PCRE2_CASELESS) != 0) cflags |= REG_ICASE;
+ if ((pat_patctl.options & PCRE2_LITERAL) != 0) cflags |= REG_NOSPEC;
+ if ((pat_patctl.options & PCRE2_MULTILINE) != 0) cflags |= REG_NEWLINE;
+ if ((pat_patctl.options & PCRE2_DOTALL) != 0) cflags |= REG_DOTALL;
+ if ((pat_patctl.options & PCRE2_UNGREEDY) != 0) cflags |= REG_UNGREEDY;
+
+ if ((pat_patctl.control & (CTL_HEXPAT|CTL_USE_LENGTH)) != 0)
+ {
+ preg.re_endp = (char *)pbuffer8 + patlen;
+ cflags |= REG_PEND;
+ }
+
+ rc = regcomp(&preg, (char *)pbuffer8, cflags);
+
+ /* Compiling failed */
+
+ if (rc != 0)
+ {
+ size_t bsize, usize;
+ int psize;
+
+ preg.re_pcre2_code = NULL; /* In case something was left in there */
+ preg.re_match_data = NULL;
+
+ bsize = (pat_patctl.regerror_buffsize != 0)?
+ pat_patctl.regerror_buffsize : pbuffer8_size;
+ if (bsize + 8 < pbuffer8_size)
+ memcpy(pbuffer8 + bsize, "DEADBEEF", 8);
+ usize = regerror(rc, &preg, (char *)pbuffer8, bsize);
+
+ /* Inside regerror(), snprintf() is used. If the buffer is too small, some
+ versions of snprintf() put a zero byte at the end, but others do not.
+ Therefore, we print a maximum of one less than the size of the buffer. */
+
+ psize = (int)bsize - 1;
+ fprintf(outfile, "Failed: POSIX code %d: %.*s\n", rc, psize, pbuffer8);
+ if (usize > bsize)
+ {
+ fprintf(outfile, "** regerror() message truncated\n");
+ if (memcmp(pbuffer8 + bsize, "DEADBEEF", 8) != 0)
+ fprintf(outfile, "** regerror() buffer overflow\n");
+ }
+ return PR_SKIP;
+ }
+
+ /* Compiling succeeded. Check that the values in the preg block are sensible.
+ It can happen that pcre2test is accidentally linked with a different POSIX
+ library which succeeds, but of course puts different things into preg. In
+ this situation, calling regfree() may cause a segfault (or invalid free() in
+ valgrind), so ensure that preg.re_pcre2_code is NULL, which suppresses the
+ calling of regfree() on exit. */
+
+ if (preg.re_pcre2_code == NULL ||
+ ((pcre2_real_code_8 *)preg.re_pcre2_code)->magic_number != MAGIC_NUMBER ||
+ ((pcre2_real_code_8 *)preg.re_pcre2_code)->top_bracket != preg.re_nsub ||
+ preg.re_match_data == NULL ||
+ preg.re_cflags != cflags)
+ {
+ fprintf(outfile,
+ "** The regcomp() function returned zero (success), but the values set\n"
+ "** in the preg block are not valid for PCRE2. Check that pcre2test is\n"
+ "** linked with PCRE2's pcre2posix module (-lpcre2-posix) and not with\n"
+ "** some other POSIX regex library.\n**\n");
+ preg.re_pcre2_code = NULL;
+ return PR_ABEND;
+ }
+
+ return PR_OK;
+#endif /* SUPPORT_PCRE2_8 */
+ }
+
+/* Handle compiling via the native interface. Controls that act later are
+ignored with "push". Replacements are locked out. */
+
+if ((pat_patctl.control & (CTL_PUSH|CTL_PUSHCOPY|CTL_PUSHTABLESCOPY)) != 0)
+ {
+ if (pat_patctl.replacement[0] != 0)
+ {
+ fprintf(outfile, "** Replacement text is not supported with 'push'.\n");
+ return PR_OK;
+ }
+ if ((pat_patctl.control & ~PUSH_SUPPORTED_COMPILE_CONTROLS) != 0 ||
+ (pat_patctl.control2 & ~PUSH_SUPPORTED_COMPILE_CONTROLS2) != 0)
+ {
+ show_controls(pat_patctl.control & ~PUSH_SUPPORTED_COMPILE_CONTROLS,
+ pat_patctl.control2 & ~PUSH_SUPPORTED_COMPILE_CONTROLS2,
+ "** Ignored when compiled pattern is stacked with 'push':");
+ fprintf(outfile, "\n");
+ }
+ if ((pat_patctl.control & PUSH_COMPILE_ONLY_CONTROLS) != 0 ||
+ (pat_patctl.control2 & PUSH_COMPILE_ONLY_CONTROLS2) != 0)
+ {
+ show_controls(pat_patctl.control & PUSH_COMPILE_ONLY_CONTROLS,
+ pat_patctl.control2 & PUSH_COMPILE_ONLY_CONTROLS2,
+ "** Applies only to compile when pattern is stacked with 'push':");
+ fprintf(outfile, "\n");
+ }
+ }
+
+/* Convert the input in non-8-bit modes. */
+
+errorcode = 0;
+
+#ifdef SUPPORT_PCRE2_16
+if (test_mode == PCRE16_MODE) errorcode = to16(pbuffer8, utf, &patlen);
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+if (test_mode == PCRE32_MODE) errorcode = to32(pbuffer8, utf, &patlen);
+#endif
+
+switch(errorcode)
+ {
+ case -1:
+ fprintf(outfile, "** Failed: invalid UTF-8 string cannot be "
+ "converted to %d-bit string\n", (test_mode == PCRE16_MODE)? 16:32);
+ return PR_SKIP;
+
+ case -2:
+ fprintf(outfile, "** Failed: character value greater than 0x10ffff "
+ "cannot be converted to UTF\n");
+ return PR_SKIP;
+
+ case -3:
+ fprintf(outfile, "** Failed: character value greater than 0xffff "
+ "cannot be converted to 16-bit in non-UTF mode\n");
+ return PR_SKIP;
+
+ default:
+ break;
+ }
+
+/* The pattern is now in pbuffer[8|16|32], with the length in code units in
+patlen. If it is to be converted, copy the result back afterwards so that it
+ends up back in the usual place. */
+
+if (pat_patctl.convert_type != CONVERT_UNSET)
+ {
+ int rc;
+ int convert_return = PR_OK;
+ uint32_t convert_options = pat_patctl.convert_type;
+ void *converted_pattern;
+ PCRE2_SIZE converted_length;
+
+ if (pat_patctl.convert_length != 0)
+ {
+ converted_length = pat_patctl.convert_length;
+ converted_pattern = malloc(converted_length * code_unit_size);
+ if (converted_pattern == NULL)
+ {
+ fprintf(outfile, "** Failed: malloc failed for converted pattern\n");
+ return PR_SKIP;
+ }
+ }
+ else converted_pattern = NULL; /* Let the library allocate */
+
+ if (utf) convert_options |= PCRE2_CONVERT_UTF;
+ if ((pat_patctl.options & PCRE2_NO_UTF_CHECK) != 0)
+ convert_options |= PCRE2_CONVERT_NO_UTF_CHECK;
+
+ CONCTXCPY(con_context, default_con_context);
+
+ if (pat_patctl.convert_glob_escape != 0)
+ {
+ uint32_t escape = (pat_patctl.convert_glob_escape == '0')? 0 :
+ pat_patctl.convert_glob_escape;
+ PCRE2_SET_GLOB_ESCAPE(rc, con_context, escape);
+ if (rc != 0)
+ {
+ fprintf(outfile, "** Invalid glob escape '%c'\n",
+ pat_patctl.convert_glob_escape);
+ convert_return = PR_SKIP;
+ goto CONVERT_FINISH;
+ }
+ }
+
+ if (pat_patctl.convert_glob_separator != 0)
+ {
+ PCRE2_SET_GLOB_SEPARATOR(rc, con_context, pat_patctl.convert_glob_separator);
+ if (rc != 0)
+ {
+ fprintf(outfile, "** Invalid glob separator '%c'\n",
+ pat_patctl.convert_glob_separator);
+ convert_return = PR_SKIP;
+ goto CONVERT_FINISH;
+ }
+ }
+
+ PCRE2_PATTERN_CONVERT(rc, pbuffer, patlen, convert_options,
+ &converted_pattern, &converted_length, con_context);
+
+ if (rc != 0)
+ {
+ fprintf(outfile, "** Pattern conversion error at offset %" SIZ_FORM ": ",
+ SIZ_CAST converted_length);
+ convert_return = print_error_message(rc, "", "\n")? PR_SKIP:PR_ABEND;
+ }
+
+ /* Output the converted pattern, then copy it. */
+
+ else
+ {
+ PCHARSV(converted_pattern, 0, converted_length, utf, outfile);
+ fprintf(outfile, "\n");
+ patlen = converted_length;
+ CONVERT_COPY(pbuffer, converted_pattern, converted_length + 1);
+ }
+
+ /* Free the converted pattern. */
+
+ CONVERT_FINISH:
+ if (pat_patctl.convert_length != 0)
+ free(converted_pattern);
+ else
+ PCRE2_CONVERTED_PATTERN_FREE(converted_pattern);
+
+ /* Return if conversion was unsuccessful. */
+
+ if (convert_return != PR_OK) return convert_return;
+ }
+
+/* By default we pass a zero-terminated pattern, but a length is passed if
+"use_length" was specified or this is a hex pattern (which might contain binary
+zeros). When valgrind is supported, arrange for the unused part of the buffer
+to be marked as no access. */
+
+valgrind_access_length = patlen;
+if ((pat_patctl.control & (CTL_HEXPAT|CTL_USE_LENGTH)) == 0)
+ {
+ patlen = PCRE2_ZERO_TERMINATED;
+ valgrind_access_length += 1; /* For the terminating zero */
+ }
+
+#ifdef SUPPORT_VALGRIND
+#ifdef SUPPORT_PCRE2_8
+if (test_mode == PCRE8_MODE && pbuffer8 != NULL)
+ {
+ VALGRIND_MAKE_MEM_NOACCESS(pbuffer8 + valgrind_access_length,
+ pbuffer8_size - valgrind_access_length);
+ }
+#endif
+#ifdef SUPPORT_PCRE2_16
+if (test_mode == PCRE16_MODE && pbuffer16 != NULL)
+ {
+ VALGRIND_MAKE_MEM_NOACCESS(pbuffer16 + valgrind_access_length,
+ pbuffer16_size - valgrind_access_length*sizeof(uint16_t));
+ }
+#endif
+#ifdef SUPPORT_PCRE2_32
+if (test_mode == PCRE32_MODE && pbuffer32 != NULL)
+ {
+ VALGRIND_MAKE_MEM_NOACCESS(pbuffer32 + valgrind_access_length,
+ pbuffer32_size - valgrind_access_length*sizeof(uint32_t));
+ }
+#endif
+#else /* Valgrind not supported */
+(void)valgrind_access_length; /* Avoid compiler warning */
+#endif
+
+/* If #newline_default has been used and the library was not compiled with an
+appropriate default newline setting, local_newline_default will be non-zero. We
+use this if there is no explicit newline modifier. */
+
+if ((pat_patctl.control2 & CTL2_NL_SET) == 0 && local_newline_default != 0)
+ {
+ SETFLD(pat_context, newline_convention, local_newline_default);
+ }
+
+/* The null_context modifier is used to test calling pcre2_compile() with a
+NULL context. */
+
+use_pat_context = ((pat_patctl.control & CTL_NULLCONTEXT) != 0)?
+ NULL : PTR(pat_context);
+
+/* If PCRE2_LITERAL is set, set use_forbid_utf zero because PCRE2_NEVER_UTF
+and PCRE2_NEVER_UCP are invalid with it. */
+
+if ((pat_patctl.options & PCRE2_LITERAL) != 0) use_forbid_utf = 0;
+
+/* Compile many times when timing. */
+
+if (timeit > 0)
+ {
+ int i;
+ clock_t time_taken = 0;
+ for (i = 0; i < timeit; i++)
+ {
+ clock_t start_time = clock();
+ PCRE2_COMPILE(compiled_code, pbuffer, patlen,
+ pat_patctl.options|use_forbid_utf, &errorcode, &erroroffset,
+ use_pat_context);
+ time_taken += clock() - start_time;
+ if (TEST(compiled_code, !=, NULL))
+ { SUB1(pcre2_code_free, compiled_code); }
+ }
+ total_compile_time += time_taken;
+ fprintf(outfile, "Compile time %.4f milliseconds\n",
+ (((double)time_taken * 1000.0) / (double)timeit) /
+ (double)CLOCKS_PER_SEC);
+ }
+
+/* A final compile that is used "for real". */
+
+PCRE2_COMPILE(compiled_code, pbuffer, patlen, pat_patctl.options|use_forbid_utf,
+ &errorcode, &erroroffset, use_pat_context);
+
+/* Call the JIT compiler if requested. When timing, we must free and recompile
+the pattern each time because that is the only way to free the JIT compiled
+code. We know that compilation will always succeed. */
+
+if (TEST(compiled_code, !=, NULL) && pat_patctl.jit != 0)
+ {
+ if (timeit > 0)
+ {
+ int i;
+ clock_t time_taken = 0;
+ for (i = 0; i < timeit; i++)
+ {
+ clock_t start_time;
+ SUB1(pcre2_code_free, compiled_code);
+ PCRE2_COMPILE(compiled_code, pbuffer, patlen,
+ pat_patctl.options|use_forbid_utf, &errorcode, &erroroffset,
+ use_pat_context);
+ start_time = clock();
+ PCRE2_JIT_COMPILE(jitrc,compiled_code, pat_patctl.jit);
+ time_taken += clock() - start_time;
+ }
+ total_jit_compile_time += time_taken;
+ fprintf(outfile, "JIT compile %.4f milliseconds\n",
+ (((double)time_taken * 1000.0) / (double)timeit) /
+ (double)CLOCKS_PER_SEC);
+ }
+ else
+ {
+ PCRE2_JIT_COMPILE(jitrc, compiled_code, pat_patctl.jit);
+ }
+ }
+
+/* If valgrind is supported, mark the pbuffer as accessible again. The 16-bit
+and 32-bit buffers can be marked completely undefined, but we must leave the
+pattern in the 8-bit buffer defined because it may be read from a callout
+during matching. */
+
+#ifdef SUPPORT_VALGRIND
+#ifdef SUPPORT_PCRE2_8
+if (test_mode == PCRE8_MODE)
+ {
+ VALGRIND_MAKE_MEM_UNDEFINED(pbuffer8 + valgrind_access_length,
+ pbuffer8_size - valgrind_access_length);
+ }
+#endif
+#ifdef SUPPORT_PCRE2_16
+if (test_mode == PCRE16_MODE)
+ {
+ VALGRIND_MAKE_MEM_UNDEFINED(pbuffer16, pbuffer16_size);
+ }
+#endif
+#ifdef SUPPORT_PCRE2_32
+if (test_mode == PCRE32_MODE)
+ {
+ VALGRIND_MAKE_MEM_UNDEFINED(pbuffer32, pbuffer32_size);
+ }
+#endif
+#endif
+
+/* Compilation failed; go back for another re, skipping to blank line
+if non-interactive. */
+
+if (TEST(compiled_code, ==, NULL))
+ {
+ fprintf(outfile, "Failed: error %d at offset %d: ", errorcode,
+ (int)erroroffset);
+ if (!print_error_message(errorcode, "", "\n")) return PR_ABEND;
+ return PR_SKIP;
+ }
+
+/* If forbid_utf is non-zero, we are running a non-UTF test. UTF and UCP are
+locked out at compile time, but we must also check for occurrences of \P, \p,
+and \X, which are only supported when Unicode is supported. */
+
+if (forbid_utf != 0)
+ {
+ if ((FLD(compiled_code, flags) & PCRE2_HASBKPORX) != 0)
+ {
+ fprintf(outfile, "** \\P, \\p, and \\X are not allowed after the "
+ "#forbid_utf command\n");
+ return PR_SKIP;
+ }
+ }
+
+/* Remember the maximum lookbehind, for partial matching. */
+
+if (pattern_info(PCRE2_INFO_MAXLOOKBEHIND, &maxlookbehind, FALSE) != 0)
+ return PR_ABEND;
+
+/* If an explicit newline modifier was given, set the information flag in the
+pattern so that it is preserved over push/pop. */
+
+if ((pat_patctl.control2 & CTL2_NL_SET) != 0)
+ {
+ SETFLD(compiled_code, flags, FLD(compiled_code, flags) | PCRE2_NL_SET);
+ }
+
+/* Output code size and other information if requested. */
+
+if ((pat_patctl.control & CTL_MEMORY) != 0) show_memory_info();
+if ((pat_patctl.control & CTL_FRAMESIZE) != 0) show_framesize();
+if ((pat_patctl.control & CTL_ANYINFO) != 0)
+ {
+ int rc = show_pattern_info();
+ if (rc != PR_OK) return rc;
+ }
+
+/* The "push" control requests that the compiled pattern be remembered on a
+stack. This is mainly for testing the serialization functionality. */
+
+if ((pat_patctl.control & CTL_PUSH) != 0)
+ {
+ if (patstacknext >= PATSTACKSIZE)
+ {
+ fprintf(outfile, "** Too many pushed patterns (max %d)\n", PATSTACKSIZE);
+ return PR_ABEND;
+ }
+ patstack[patstacknext++] = PTR(compiled_code);
+ SET(compiled_code, NULL);
+ }
+
+/* The "pushcopy" and "pushtablescopy" controls are similar, but push a
+copy of the pattern, the latter with a copy of its character tables. This tests
+the pcre2_code_copy() and pcre2_code_copy_with_tables() functions. */
+
+if ((pat_patctl.control & (CTL_PUSHCOPY|CTL_PUSHTABLESCOPY)) != 0)
+ {
+ if (patstacknext >= PATSTACKSIZE)
+ {
+ fprintf(outfile, "** Too many pushed patterns (max %d)\n", PATSTACKSIZE);
+ return PR_ABEND;
+ }
+ if ((pat_patctl.control & CTL_PUSHCOPY) != 0)
+ {
+ PCRE2_CODE_COPY_TO_VOID(patstack[patstacknext++], compiled_code);
+ }
+ else
+ {
+ PCRE2_CODE_COPY_WITH_TABLES_TO_VOID(patstack[patstacknext++],
+ compiled_code); }
+ }
+
+return PR_OK;
+}
+
+
+
+/*************************************************
+* Check heap, match or depth limit *
+*************************************************/
+
+/* This is used for DFA, normal, and JIT fast matching. For DFA matching it
+should only be called with the third argument set to PCRE2_ERROR_DEPTHLIMIT.
+
+Arguments:
+ pp the subject string
+ ulen length of subject or PCRE2_ZERO_TERMINATED
+ errnumber defines which limit to test
+ msg string to include in final message
+
+Returns: the return from the final match function call
+*/
+
+static int
+check_match_limit(uint8_t *pp, PCRE2_SIZE ulen, int errnumber, const char *msg)
+{
+int capcount;
+uint32_t min = 0;
+uint32_t mid = 64;
+uint32_t max = UINT32_MAX;
+
+PCRE2_SET_MATCH_LIMIT(dat_context, max);
+PCRE2_SET_DEPTH_LIMIT(dat_context, max);
+PCRE2_SET_HEAP_LIMIT(dat_context, max);
+
+for (;;)
+ {
+ uint32_t stack_start = 0;
+
+ if (errnumber == PCRE2_ERROR_HEAPLIMIT)
+ {
+ PCRE2_SET_HEAP_LIMIT(dat_context, mid);
+ }
+ else if (errnumber == PCRE2_ERROR_MATCHLIMIT)
+ {
+ PCRE2_SET_MATCH_LIMIT(dat_context, mid);
+ }
+ else
+ {
+ PCRE2_SET_DEPTH_LIMIT(dat_context, mid);
+ }
+
+ if ((dat_datctl.control & CTL_DFA) != 0)
+ {
+ stack_start = DFA_START_RWS_SIZE/1024;
+ if (dfa_workspace == NULL)
+ dfa_workspace = (int *)malloc(DFA_WS_DIMENSION*sizeof(int));
+ if (dfa_matched++ == 0)
+ dfa_workspace[0] = -1; /* To catch bad restart */
+ PCRE2_DFA_MATCH(capcount, compiled_code, pp, ulen, dat_datctl.offset,
+ dat_datctl.options, match_data,
+ PTR(dat_context), dfa_workspace, DFA_WS_DIMENSION);
+ }
+
+ else if ((pat_patctl.control & CTL_JITFAST) != 0)
+ PCRE2_JIT_MATCH(capcount, compiled_code, pp, ulen, dat_datctl.offset,
+ dat_datctl.options, match_data, PTR(dat_context));
+
+ else
+ {
+ stack_start = START_FRAMES_SIZE/1024;
+ PCRE2_MATCH(capcount, compiled_code, pp, ulen, dat_datctl.offset,
+ dat_datctl.options, match_data, PTR(dat_context));
+ }
+
+ if (capcount == errnumber)
+ {
+ if ((mid & 0x80000000u) != 0)
+ {
+ fprintf(outfile, "Can't find minimum %s limit: check pattern for "
+ "restriction\n", msg);
+ break;
+ }
+
+ min = mid;
+ mid = (mid == max - 1)? max : (max != UINT32_MAX)? (min + max)/2 : mid*2;
+ }
+ else if (capcount >= 0 ||
+ capcount == PCRE2_ERROR_NOMATCH ||
+ capcount == PCRE2_ERROR_PARTIAL)
+ {
+ /* If we've not hit the error with a heap limit less than the size of the
+ initial stack frame vector (for pcre2_match()) or the initial stack
+ workspace vector (for pcre2_dfa_match()), the heap is not being used, so
+ the minimum limit is zero; there's no need to go on. The other limits are
+ always greater than zero. */
+
+ if (errnumber == PCRE2_ERROR_HEAPLIMIT && mid < stack_start)
+ {
+ fprintf(outfile, "Minimum %s limit = 0\n", msg);
+ break;
+ }
+ if (mid == min + 1)
+ {
+ fprintf(outfile, "Minimum %s limit = %d\n", msg, mid);
+ break;
+ }
+ max = mid;
+ mid = (min + max)/2;
+ }
+ else break; /* Some other error */
+ }
+
+return capcount;
+}
+
+
+
+/*************************************************
+* Callout function *
+*************************************************/
+
+/* Called from a PCRE2 library as a result of the (?C) item. We print out where
+we are in the match (unless suppressed). Yield zero unless more callouts than
+the fail count, or the callout data is not zero. The only differences in the
+callout block for different code unit widths are that the pointers to the
+subject, the most recent MARK, and a callout argument string point to strings
+of the appropriate width. Casts can be used to deal with this.
+
+Argument: a pointer to a callout block
+Return:
+*/
+
+static int
+callout_function(pcre2_callout_block_8 *cb, void *callout_data_ptr)
+{
+FILE *f, *fdefault;
+uint32_t i, pre_start, post_start, subject_length;
+PCRE2_SIZE current_position;
+BOOL utf = (FLD(compiled_code, overall_options) & PCRE2_UTF) != 0;
+BOOL callout_capture = (dat_datctl.control & CTL_CALLOUT_CAPTURE) != 0;
+BOOL callout_where = (dat_datctl.control2 & CTL2_CALLOUT_NO_WHERE) == 0;
+
+/* The FILE f is used for echoing the subject string if it is non-NULL. This
+happens only once in simple cases, but we want to repeat after any additional
+output caused by CALLOUT_EXTRA. */
+
+fdefault = (!first_callout && !callout_capture && cb->callout_string == NULL)?
+ NULL : outfile;
+
+if ((dat_datctl.control2 & CTL2_CALLOUT_EXTRA) != 0)
+ {
+ f = outfile;
+ switch (cb->callout_flags)
+ {
+ case PCRE2_CALLOUT_BACKTRACK:
+ fprintf(f, "Backtrack\n");
+ break;
+
+ case PCRE2_CALLOUT_STARTMATCH|PCRE2_CALLOUT_BACKTRACK:
+ fprintf(f, "Backtrack\nNo other matching paths\n");
+ /* Fall through */
+
+ case PCRE2_CALLOUT_STARTMATCH:
+ fprintf(f, "New match attempt\n");
+ break;
+
+ default:
+ f = fdefault;
+ break;
+ }
+ }
+else f = fdefault;
+
+/* For a callout with a string argument, show the string first because there
+isn't a tidy way to fit it in the rest of the data. */
+
+if (cb->callout_string != NULL)
+ {
+ uint32_t delimiter = CODE_UNIT(cb->callout_string, -1);
+ fprintf(outfile, "Callout (%" SIZ_FORM "): %c",
+ SIZ_CAST cb->callout_string_offset, delimiter);
+ PCHARSV(cb->callout_string, 0,
+ cb->callout_string_length, utf, outfile);
+ for (i = 0; callout_start_delims[i] != 0; i++)
+ if (delimiter == callout_start_delims[i])
+ {
+ delimiter = callout_end_delims[i];
+ break;
+ }
+ fprintf(outfile, "%c", delimiter);
+ if (!callout_capture) fprintf(outfile, "\n");
+ }
+
+/* Show captured strings if required */
+
+if (callout_capture)
+ {
+ if (cb->callout_string == NULL)
+ fprintf(outfile, "Callout %d:", cb->callout_number);
+ fprintf(outfile, " last capture = %d\n", cb->capture_last);
+ for (i = 2; i < cb->capture_top * 2; i += 2)
+ {
+ fprintf(outfile, "%2d: ", i/2);
+ if (cb->offset_vector[i] == PCRE2_UNSET)
+ fprintf(outfile, "<unset>");
+ else
+ {
+ PCHARSV(cb->subject, cb->offset_vector[i],
+ cb->offset_vector[i+1] - cb->offset_vector[i], utf, f);
+ }
+ fprintf(outfile, "\n");
+ }
+ }
+
+/* Unless suppressed, re-print the subject in canonical form (with escapes for
+non-printing characters), the first time, or if giving full details. On
+subsequent calls in the same match, we use PCHARS() just to find the printed
+lengths of the substrings. */
+
+if (callout_where)
+ {
+ if (f != NULL) fprintf(f, "--->");
+
+ /* The subject before the match start. */
+
+ PCHARS(pre_start, cb->subject, 0, cb->start_match, utf, f);
+
+ /* If a lookbehind is involved, the current position may be earlier than the
+ match start. If so, use the match start instead. */
+
+ current_position = (cb->current_position >= cb->start_match)?
+ cb->current_position : cb->start_match;
+
+ /* The subject between the match start and the current position. */
+
+ PCHARS(post_start, cb->subject, cb->start_match,
+ current_position - cb->start_match, utf, f);
+
+ /* Print from the current position to the end. */
+
+ PCHARSV(cb->subject, current_position, cb->subject_length - current_position,
+ utf, f);
+
+ /* Calculate the total subject printed length (no print). */
+
+ PCHARS(subject_length, cb->subject, 0, cb->subject_length, utf, NULL);
+
+ if (f != NULL) fprintf(f, "\n");
+
+ /* For automatic callouts, show the pattern offset. Otherwise, for a
+ numerical callout whose number has not already been shown with captured
+ strings, show the number here. A callout with a string argument has been
+ displayed above. */
+
+ if (cb->callout_number == 255)
+ {
+ fprintf(outfile, "%+3d ", (int)cb->pattern_position);
+ if (cb->pattern_position > 99) fprintf(outfile, "\n ");
+ }
+ else
+ {
+ if (callout_capture || cb->callout_string != NULL) fprintf(outfile, " ");
+ else fprintf(outfile, "%3d ", cb->callout_number);
+ }
+
+ /* Now show position indicators */
+
+ for (i = 0; i < pre_start; i++) fprintf(outfile, " ");
+ fprintf(outfile, "^");
+
+ if (post_start > 0)
+ {
+ for (i = 0; i < post_start - 1; i++) fprintf(outfile, " ");
+ fprintf(outfile, "^");
+ }
+
+ for (i = 0; i < subject_length - pre_start - post_start + 4; i++)
+ fprintf(outfile, " ");
+
+ if (cb->next_item_length != 0)
+ fprintf(outfile, "%.*s", (int)(cb->next_item_length),
+ pbuffer8 + cb->pattern_position);
+ else
+ fprintf(outfile, "End of pattern");
+
+ fprintf(outfile, "\n");
+ }
+
+first_callout = FALSE;
+
+/* Show any mark info */
+
+if (cb->mark != last_callout_mark)
+ {
+ if (cb->mark == NULL)
+ fprintf(outfile, "Latest Mark: <unset>\n");
+ else
+ {
+ fprintf(outfile, "Latest Mark: ");
+ PCHARSV(cb->mark, 0, -1, utf, outfile);
+ putc('\n', outfile);
+ }
+ last_callout_mark = cb->mark;
+ }
+
+/* Show callout data */
+
+if (callout_data_ptr != NULL)
+ {
+ int callout_data = *((int32_t *)callout_data_ptr);
+ if (callout_data != 0)
+ {
+ fprintf(outfile, "Callout data = %d\n", callout_data);
+ return callout_data;
+ }
+ }
+
+/* Keep count and give the appropriate return code */
+
+callout_count++;
+
+if (cb->callout_number == dat_datctl.cerror[0] &&
+ callout_count >= dat_datctl.cerror[1])
+ return PCRE2_ERROR_CALLOUT;
+
+if (cb->callout_number == dat_datctl.cfail[0] &&
+ callout_count >= dat_datctl.cfail[1])
+ return 1;
+
+return 0;
+}
+
+
+
+/*************************************************
+* Handle *MARK and copy/get tests *
+*************************************************/
+
+/* This function is called after complete and partial matches. It runs the
+tests for substring extraction.
+
+Arguments:
+ utf TRUE for utf
+ capcount return from pcre2_match()
+
+Returns: FALSE if print_error_message() fails
+*/
+
+static BOOL
+copy_and_get(BOOL utf, int capcount)
+{
+int i;
+uint8_t *nptr;
+
+/* Test copy strings by number */
+
+for (i = 0; i < MAXCPYGET && dat_datctl.copy_numbers[i] >= 0; i++)
+ {
+ int rc;
+ PCRE2_SIZE length, length2;
+ uint32_t copybuffer[256];
+ uint32_t n = (uint32_t)(dat_datctl.copy_numbers[i]);
+ length = sizeof(copybuffer)/code_unit_size;
+ PCRE2_SUBSTRING_COPY_BYNUMBER(rc, match_data, n, copybuffer, &length);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Copy substring %d failed (%d): ", n, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else
+ {
+ PCRE2_SUBSTRING_LENGTH_BYNUMBER(rc, match_data, n, &length2);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Get substring %d length failed (%d): ", n, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else if (length2 != length)
+ {
+ fprintf(outfile, "Mismatched substring lengths: %"
+ SIZ_FORM " %" SIZ_FORM "\n", SIZ_CAST length, SIZ_CAST length2);
+ }
+ fprintf(outfile, "%2dC ", n);
+ PCHARSV(copybuffer, 0, length, utf, outfile);
+ fprintf(outfile, " (%" SIZ_FORM ")\n", SIZ_CAST length);
+ }
+ }
+
+/* Test copy strings by name */
+
+nptr = dat_datctl.copy_names;
+for (;;)
+ {
+ int rc;
+ int groupnumber;
+ PCRE2_SIZE length, length2;
+ uint32_t copybuffer[256];
+ int namelen = strlen((const char *)nptr);
+#if defined SUPPORT_PCRE2_16 || defined SUPPORT_PCRE2_32
+ PCRE2_SIZE cnl = namelen;
+#endif
+ if (namelen == 0) break;
+
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE) strcpy((char *)pbuffer8, (char *)nptr);
+#endif
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)(void)to16(nptr, utf, &cnl);
+#endif
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE)(void)to32(nptr, utf, &cnl);
+#endif
+
+ PCRE2_SUBSTRING_NUMBER_FROM_NAME(groupnumber, compiled_code, pbuffer);
+ if (groupnumber < 0 && groupnumber != PCRE2_ERROR_NOUNIQUESUBSTRING)
+ fprintf(outfile, "Number not found for group '%s'\n", nptr);
+
+ length = sizeof(copybuffer)/code_unit_size;
+ PCRE2_SUBSTRING_COPY_BYNAME(rc, match_data, pbuffer, copybuffer, &length);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Copy substring '%s' failed (%d): ", nptr, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else
+ {
+ PCRE2_SUBSTRING_LENGTH_BYNAME(rc, match_data, pbuffer, &length2);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Get substring '%s' length failed (%d): ", nptr, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else if (length2 != length)
+ {
+ fprintf(outfile, "Mismatched substring lengths: %"
+ SIZ_FORM " %" SIZ_FORM "\n", SIZ_CAST length, SIZ_CAST length2);
+ }
+ fprintf(outfile, " C ");
+ PCHARSV(copybuffer, 0, length, utf, outfile);
+ fprintf(outfile, " (%" SIZ_FORM ") %s", SIZ_CAST length, nptr);
+ if (groupnumber >= 0) fprintf(outfile, " (group %d)\n", groupnumber);
+ else fprintf(outfile, " (non-unique)\n");
+ }
+ nptr += namelen + 1;
+ }
+
+/* Test get strings by number */
+
+for (i = 0; i < MAXCPYGET && dat_datctl.get_numbers[i] >= 0; i++)
+ {
+ int rc;
+ PCRE2_SIZE length;
+ void *gotbuffer;
+ uint32_t n = (uint32_t)(dat_datctl.get_numbers[i]);
+ PCRE2_SUBSTRING_GET_BYNUMBER(rc, match_data, n, &gotbuffer, &length);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Get substring %d failed (%d): ", n, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else
+ {
+ fprintf(outfile, "%2dG ", n);
+ PCHARSV(gotbuffer, 0, length, utf, outfile);
+ fprintf(outfile, " (%" SIZ_FORM ")\n", SIZ_CAST length);
+ PCRE2_SUBSTRING_FREE(gotbuffer);
+ }
+ }
+
+/* Test get strings by name */
+
+nptr = dat_datctl.get_names;
+for (;;)
+ {
+ PCRE2_SIZE length;
+ void *gotbuffer;
+ int rc;
+ int groupnumber;
+ int namelen = strlen((const char *)nptr);
+#if defined SUPPORT_PCRE2_16 || defined SUPPORT_PCRE2_32
+ PCRE2_SIZE cnl = namelen;
+#endif
+ if (namelen == 0) break;
+
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE) strcpy((char *)pbuffer8, (char *)nptr);
+#endif
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)(void)to16(nptr, utf, &cnl);
+#endif
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE)(void)to32(nptr, utf, &cnl);
+#endif
+
+ PCRE2_SUBSTRING_NUMBER_FROM_NAME(groupnumber, compiled_code, pbuffer);
+ if (groupnumber < 0 && groupnumber != PCRE2_ERROR_NOUNIQUESUBSTRING)
+ fprintf(outfile, "Number not found for group '%s'\n", nptr);
+
+ PCRE2_SUBSTRING_GET_BYNAME(rc, match_data, pbuffer, &gotbuffer, &length);
+ if (rc < 0)
+ {
+ fprintf(outfile, "Get substring '%s' failed (%d): ", nptr, rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else
+ {
+ fprintf(outfile, " G ");
+ PCHARSV(gotbuffer, 0, length, utf, outfile);
+ fprintf(outfile, " (%" SIZ_FORM ") %s", SIZ_CAST length, nptr);
+ if (groupnumber >= 0) fprintf(outfile, " (group %d)\n", groupnumber);
+ else fprintf(outfile, " (non-unique)\n");
+ PCRE2_SUBSTRING_FREE(gotbuffer);
+ }
+ nptr += namelen + 1;
+ }
+
+/* Test getting the complete list of captured strings. */
+
+if ((dat_datctl.control & CTL_GETALL) != 0)
+ {
+ int rc;
+ void **stringlist;
+ PCRE2_SIZE *lengths;
+ PCRE2_SUBSTRING_LIST_GET(rc, match_data, &stringlist, &lengths);
+ if (rc < 0)
+ {
+ fprintf(outfile, "get substring list failed (%d): ", rc);
+ if (!print_error_message(rc, "", "\n")) return FALSE;
+ }
+ else
+ {
+ for (i = 0; i < capcount; i++)
+ {
+ fprintf(outfile, "%2dL ", i);
+ PCHARSV(stringlist[i], 0, lengths[i], utf, outfile);
+ putc('\n', outfile);
+ }
+ if (stringlist[i] != NULL)
+ fprintf(outfile, "string list not terminated by NULL\n");
+ PCRE2_SUBSTRING_LIST_FREE(stringlist);
+ }
+ }
+
+return TRUE;
+}
+
+
+
+/*************************************************
+* Process a data line *
+*************************************************/
+
+/* The line is in buffer; it will not be empty.
+
+Arguments: none
+
+Returns: PR_OK continue processing next line
+ PR_SKIP skip to a blank line
+ PR_ABEND abort the pcre2test run
+*/
+
+static int
+process_data(void)
+{
+PCRE2_SIZE len, ulen, arg_ulen;
+uint32_t gmatched;
+uint32_t c, k;
+uint32_t g_notempty = 0;
+uint8_t *p, *pp, *start_rep;
+size_t needlen;
+void *use_dat_context;
+BOOL utf;
+BOOL subject_literal;
+PCRE2_SIZE ovecsave[3];
+
+#ifdef SUPPORT_PCRE2_8
+uint8_t *q8 = NULL;
+#endif
+#ifdef SUPPORT_PCRE2_16
+uint16_t *q16 = NULL;
+#endif
+#ifdef SUPPORT_PCRE2_32
+uint32_t *q32 = NULL;
+#endif
+
+subject_literal = (pat_patctl.control2 & CTL2_SUBJECT_LITERAL) != 0;
+
+/* Copy the default context and data control blocks to the active ones. Then
+copy from the pattern the controls that can be set in either the pattern or the
+data. This allows them to be overridden in the data line. We do not do this for
+options because those that are common apply separately to compiling and
+matching. */
+
+DATCTXCPY(dat_context, default_dat_context);
+memcpy(&dat_datctl, &def_datctl, sizeof(datctl));
+dat_datctl.control |= (pat_patctl.control & CTL_ALLPD);
+dat_datctl.control2 |= (pat_patctl.control2 & CTL2_ALLPD);
+strcpy((char *)dat_datctl.replacement, (char *)pat_patctl.replacement);
+if (dat_datctl.jitstack == 0) dat_datctl.jitstack = pat_patctl.jitstack;
+
+/* Initialize for scanning the data line. */
+
+#ifdef SUPPORT_PCRE2_8
+utf = ((((pat_patctl.control & CTL_POSIX) != 0)?
+ ((pcre2_real_code_8 *)preg.re_pcre2_code)->overall_options :
+ FLD(compiled_code, overall_options)) & PCRE2_UTF) != 0;
+#else
+utf = (FLD(compiled_code, overall_options) & PCRE2_UTF) != 0;
+#endif
+
+start_rep = NULL;
+len = strlen((const char *)buffer);
+while (len > 0 && isspace(buffer[len-1])) len--;
+buffer[len] = 0;
+p = buffer;
+while (isspace(*p)) p++;
+
+/* Check that the data is well-formed UTF-8 if we're in UTF mode. To create
+invalid input to pcre2_match(), you must use \x?? or \x{} sequences. */
+
+if (utf)
+ {
+ uint8_t *q;
+ uint32_t cc;
+ int n = 1;
+ for (q = p; n > 0 && *q; q += n) n = utf82ord(q, &cc);
+ if (n <= 0)
+ {
+ fprintf(outfile, "** Failed: invalid UTF-8 string cannot be used as input "
+ "in UTF mode\n");
+ return PR_OK;
+ }
+ }
+
+#ifdef SUPPORT_VALGRIND
+/* Mark the dbuffer as addressable but undefined again. */
+if (dbuffer != NULL)
+ {
+ VALGRIND_MAKE_MEM_UNDEFINED(dbuffer, dbuffer_size);
+ }
+#endif
+
+/* Allocate a buffer to hold the data line; len+1 is an upper bound on
+the number of code units that will be needed (though the buffer may have to be
+extended if replication is involved). */
+
+needlen = (size_t)((len+1) * code_unit_size);
+if (dbuffer == NULL || needlen >= dbuffer_size)
+ {
+ while (needlen >= dbuffer_size) dbuffer_size *= 2;
+ dbuffer = (uint8_t *)realloc(dbuffer, dbuffer_size);
+ if (dbuffer == NULL)
+ {
+ fprintf(stderr, "pcre2test: realloc(%d) failed\n", (int)dbuffer_size);
+ exit(1);
+ }
+ }
+SETCASTPTR(q, dbuffer); /* Sets q8, q16, or q32, as appropriate. */
+
+/* Scan the data line, interpreting data escapes, and put the result into a
+buffer of the appropriate width. In UTF mode, input is always UTF-8; otherwise,
+in 16- and 32-bit modes, it can be forced to UTF-8 by the utf8_input modifier.
+*/
+
+while ((c = *p++) != 0)
+ {
+ int32_t i = 0;
+ size_t replen;
+
+ /* ] may mark the end of a replicated sequence */
+
+ if (c == ']' && start_rep != NULL)
+ {
+ long li;
+ char *endptr;
+ size_t qoffset = CAST8VAR(q) - dbuffer;
+ size_t rep_offset = start_rep - dbuffer;
+
+ if (*p++ != '{')
+ {
+ fprintf(outfile, "** Expected '{' after \\[....]\n");
+ return PR_OK;
+ }
+
+ li = strtol((const char *)p, &endptr, 10);
+ if (S32OVERFLOW(li))
+ {
+ fprintf(outfile, "** Repeat count too large\n");
+ return PR_OK;
+ }
+
+ p = (uint8_t *)endptr;
+ if (*p++ != '}')
+ {
+ fprintf(outfile, "** Expected '}' after \\[...]{...\n");
+ return PR_OK;
+ }
+
+ i = (int32_t)li;
+ if (i-- == 0)
+ {
+ fprintf(outfile, "** Zero repeat not allowed\n");
+ return PR_OK;
+ }
+
+ replen = CAST8VAR(q) - start_rep;
+ needlen += replen * i;
+
+ if (needlen >= dbuffer_size)
+ {
+ while (needlen >= dbuffer_size) dbuffer_size *= 2;
+ dbuffer = (uint8_t *)realloc(dbuffer, dbuffer_size);
+ if (dbuffer == NULL)
+ {
+ fprintf(stderr, "pcre2test: realloc(%d) failed\n", (int)dbuffer_size);
+ exit(1);
+ }
+ SETCASTPTR(q, dbuffer + qoffset);
+ start_rep = dbuffer + rep_offset;
+ }
+
+ while (i-- > 0)
+ {
+ memcpy(CAST8VAR(q), start_rep, replen);
+ SETPLUS(q, replen/code_unit_size);
+ }
+
+ start_rep = NULL;
+ continue;
+ }
+
+ /* Handle a non-escaped character. In non-UTF 32-bit mode with utf8_input
+ set, do the fudge for setting the top bit. */
+
+ if (c != '\\' || subject_literal)
+ {
+ uint32_t topbit = 0;
+ if (test_mode == PCRE32_MODE && c == 0xff && *p != 0)
+ {
+ topbit = 0x80000000;
+ c = *p++;
+ }
+ if ((utf || (pat_patctl.control & CTL_UTF8_INPUT) != 0) &&
+ HASUTF8EXTRALEN(c)) { GETUTF8INC(c, p); }
+ c |= topbit;
+ }
+
+ /* Handle backslash escapes */
+
+ else switch ((c = *p++))
+ {
+ case '\\': break;
+ case 'a': c = CHAR_BEL; break;
+ case 'b': c = '\b'; break;
+ case 'e': c = CHAR_ESC; break;
+ case 'f': c = '\f'; break;
+ case 'n': c = '\n'; break;
+ case 'r': c = '\r'; break;
+ case 't': c = '\t'; break;
+ case 'v': c = '\v'; break;
+
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ c -= '0';
+ while (i++ < 2 && isdigit(*p) && *p != '8' && *p != '9')
+ c = c * 8 + *p++ - '0';
+ break;
+
+ case 'o':
+ if (*p == '{')
+ {
+ uint8_t *pt = p;
+ c = 0;
+ for (pt++; isdigit(*pt) && *pt != '8' && *pt != '9'; pt++)
+ {
+ if (++i == 12)
+ fprintf(outfile, "** Too many octal digits in \\o{...} item; "
+ "using only the first twelve.\n");
+ else c = c * 8 + *pt - '0';
+ }
+ if (*pt == '}') p = pt + 1;
+ else fprintf(outfile, "** Missing } after \\o{ (assumed)\n");
+ }
+ break;
+
+ case 'x':
+ if (*p == '{')
+ {
+ uint8_t *pt = p;
+ c = 0;
+
+ /* We used to have "while (isxdigit(*(++pt)))" here, but it fails
+ when isxdigit() is a macro that refers to its argument more than
+ once. This is banned by the C Standard, but apparently happens in at
+ least one MacOS environment. */
+
+ for (pt++; isxdigit(*pt); pt++)
+ {
+ if (++i == 9)
+ fprintf(outfile, "** Too many hex digits in \\x{...} item; "
+ "using only the first eight.\n");
+ else c = c * 16 + tolower(*pt) - ((isdigit(*pt))? '0' : 'a' - 10);
+ }
+ if (*pt == '}')
+ {
+ p = pt + 1;
+ break;
+ }
+ /* Not correct form for \x{...}; fall through */
+ }
+
+ /* \x without {} always defines just one byte in 8-bit mode. This
+ allows UTF-8 characters to be constructed byte by byte, and also allows
+ invalid UTF-8 sequences to be made. Just copy the byte in UTF-8 mode.
+ Otherwise, pass it down as data. */
+
+ c = 0;
+ while (i++ < 2 && isxdigit(*p))
+ {
+ c = c * 16 + tolower(*p) - ((isdigit(*p))? '0' : 'a' - 10);
+ p++;
+ }
+#if defined SUPPORT_PCRE2_8
+ if (utf && (test_mode == PCRE8_MODE))
+ {
+ *q8++ = c;
+ continue;
+ }
+#endif
+ break;
+
+ case 0: /* \ followed by EOF allows for an empty line */
+ p--;
+ continue;
+
+ case '=': /* \= terminates the data, starts modifiers */
+ goto ENDSTRING;
+
+ case '[': /* \[ introduces a replicated character sequence */
+ if (start_rep != NULL)
+ {
+ fprintf(outfile, "** Nested replication is not supported\n");
+ return PR_OK;
+ }
+ start_rep = CAST8VAR(q);
+ continue;
+
+ default:
+ if (isalnum(c))
+ {
+ fprintf(outfile, "** Unrecognized escape sequence \"\\%c\"\n", c);
+ return PR_OK;
+ }
+ }
+
+ /* We now have a character value in c that may be greater than 255.
+ In 8-bit mode we convert to UTF-8 if we are in UTF mode. Values greater
+ than 127 in UTF mode must have come from \x{...} or octal constructs
+ because values from \x.. get this far only in non-UTF mode. */
+
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE)
+ {
+ if (utf)
+ {
+ if (c > 0x7fffffff)
+ {
+ fprintf(outfile, "** Character \\x{%x} is greater than 0x7fffffff "
+ "and so cannot be converted to UTF-8\n", c);
+ return PR_OK;
+ }
+ q8 += ord2utf8(c, q8);
+ }
+ else
+ {
+ if (c > 0xffu)
+ {
+ fprintf(outfile, "** Character \\x{%x} is greater than 255 "
+ "and UTF-8 mode is not enabled.\n", c);
+ fprintf(outfile, "** Truncation will probably give the wrong "
+ "result.\n");
+ }
+ *q8++ = c;
+ }
+ }
+#endif
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)
+ {
+ if (utf)
+ {
+ if (c > 0x10ffffu)
+ {
+ fprintf(outfile, "** Failed: character \\x{%x} is greater than "
+ "0x10ffff and so cannot be converted to UTF-16\n", c);
+ return PR_OK;
+ }
+ else if (c >= 0x10000u)
+ {
+ c-= 0x10000u;
+ *q16++ = 0xD800 | (c >> 10);
+ *q16++ = 0xDC00 | (c & 0x3ff);
+ }
+ else
+ *q16++ = c;
+ }
+ else
+ {
+ if (c > 0xffffu)
+ {
+ fprintf(outfile, "** Character \\x{%x} is greater than 0xffff "
+ "and UTF-16 mode is not enabled.\n", c);
+ fprintf(outfile, "** Truncation will probably give the wrong "
+ "result.\n");
+ }
+
+ *q16++ = c;
+ }
+ }
+#endif
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE)
+ {
+ *q32++ = c;
+ }
+#endif
+ }
+
+ENDSTRING:
+SET(*q, 0);
+len = CASTVAR(uint8_t *, q) - dbuffer; /* Length in bytes */
+ulen = len/code_unit_size; /* Length in code units */
+arg_ulen = ulen; /* Value to use in match arg */
+
+/* If the string was terminated by \= we must now interpret modifiers. */
+
+if (p[-1] != 0 && !decode_modifiers(p, CTX_DAT, NULL, &dat_datctl))
+ return PR_OK;
+
+/* Check for mutually exclusive modifiers. At present, these are all in the
+first control word. */
+
+for (k = 0; k < sizeof(exclusive_dat_controls)/sizeof(uint32_t); k++)
+ {
+ c = dat_datctl.control & exclusive_dat_controls[k];
+ if (c != 0 && c != (c & (~c+1)))
+ {
+ show_controls(c, 0, "** Not allowed together:");
+ fprintf(outfile, "\n");
+ return PR_OK;
+ }
+ }
+
+if (pat_patctl.replacement[0] != 0 &&
+ (dat_datctl.control & CTL_NULLCONTEXT) != 0)
+ {
+ fprintf(outfile, "** Replacement text is not supported with null_context.\n");
+ return PR_OK;
+ }
+
+/* We now have the subject in dbuffer, with len containing the byte length, and
+ulen containing the code unit length, with a copy in arg_ulen for use in match
+function arguments (this gets changed to PCRE2_ZERO_TERMINATED when the
+zero_terminate modifier is present).
+
+Move the data to the end of the buffer so that a read over the end can be
+caught by valgrind or other means. If we have explicit valgrind support, mark
+the unused start of the buffer unaddressable. If we are using the POSIX
+interface, or testing zero-termination, we must include the terminating zero in
+the usable data. */
+
+c = code_unit_size * (((pat_patctl.control & CTL_POSIX) +
+ (dat_datctl.control & CTL_ZERO_TERMINATE) != 0)? 1:0);
+pp = memmove(dbuffer + dbuffer_size - len - c, dbuffer, len + c);
+#ifdef SUPPORT_VALGRIND
+ VALGRIND_MAKE_MEM_NOACCESS(dbuffer, dbuffer_size - (len + c));
+#endif
+
+/* Now pp points to the subject string. POSIX matching is only possible in
+8-bit mode, and it does not support timing or other fancy features. Some were
+checked at compile time, but we need to check the match-time settings here. */
+
+#ifdef SUPPORT_PCRE2_8
+if ((pat_patctl.control & CTL_POSIX) != 0)
+ {
+ int rc;
+ int eflags = 0;
+ regmatch_t *pmatch = NULL;
+ const char *msg = "** Ignored with POSIX interface:";
+
+ if (dat_datctl.cerror[0] != CFORE_UNSET || dat_datctl.cerror[1] != CFORE_UNSET)
+ prmsg(&msg, "callout_error");
+ if (dat_datctl.cfail[0] != CFORE_UNSET || dat_datctl.cfail[1] != CFORE_UNSET)
+ prmsg(&msg, "callout_fail");
+ if (dat_datctl.copy_numbers[0] >= 0 || dat_datctl.copy_names[0] != 0)
+ prmsg(&msg, "copy");
+ if (dat_datctl.get_numbers[0] >= 0 || dat_datctl.get_names[0] != 0)
+ prmsg(&msg, "get");
+ if (dat_datctl.jitstack != 0) prmsg(&msg, "jitstack");
+ if (dat_datctl.offset != 0) prmsg(&msg, "offset");
+
+ if ((dat_datctl.options & ~POSIX_SUPPORTED_MATCH_OPTIONS) != 0)
+ {
+ fprintf(outfile, "%s", msg);
+ show_match_options(dat_datctl.options & ~POSIX_SUPPORTED_MATCH_OPTIONS);
+ msg = "";
+ }
+ if ((dat_datctl.control & ~POSIX_SUPPORTED_MATCH_CONTROLS) != 0 ||
+ (dat_datctl.control2 & ~POSIX_SUPPORTED_MATCH_CONTROLS2) != 0)
+ {
+ show_controls(dat_datctl.control & ~POSIX_SUPPORTED_MATCH_CONTROLS,
+ dat_datctl.control2 & ~POSIX_SUPPORTED_MATCH_CONTROLS2, msg);
+ msg = "";
+ }
+
+ if (msg[0] == 0) fprintf(outfile, "\n");
+
+ if (dat_datctl.oveccount > 0)
+ {
+ pmatch = (regmatch_t *)malloc(sizeof(regmatch_t) * dat_datctl.oveccount);
+ if (pmatch == NULL)
+ {
+ fprintf(outfile, "** Failed to get memory for recording matching "
+ "information (size set = %du)\n", dat_datctl.oveccount);
+ return PR_OK;
+ }
+ }
+
+ if (dat_datctl.startend[0] != CFORE_UNSET)
+ {
+ pmatch[0].rm_so = dat_datctl.startend[0];
+ pmatch[0].rm_eo = (dat_datctl.startend[1] != 0)?
+ dat_datctl.startend[1] : len;
+ eflags |= REG_STARTEND;
+ }
+
+ if ((dat_datctl.options & PCRE2_NOTBOL) != 0) eflags |= REG_NOTBOL;
+ if ((dat_datctl.options & PCRE2_NOTEOL) != 0) eflags |= REG_NOTEOL;
+ if ((dat_datctl.options & PCRE2_NOTEMPTY) != 0) eflags |= REG_NOTEMPTY;
+
+ rc = regexec(&preg, (const char *)pp, dat_datctl.oveccount, pmatch, eflags);
+ if (rc != 0)
+ {
+ (void)regerror(rc, &preg, (char *)pbuffer8, pbuffer8_size);
+ fprintf(outfile, "No match: POSIX code %d: %s\n", rc, pbuffer8);
+ }
+ else if ((pat_patctl.control & CTL_POSIX_NOSUB) != 0)
+ fprintf(outfile, "Matched with REG_NOSUB\n");
+ else if (dat_datctl.oveccount == 0)
+ fprintf(outfile, "Matched without capture\n");
+ else
+ {
+ size_t i, j;
+ size_t last_printed = (size_t)dat_datctl.oveccount;
+ for (i = 0; i < (size_t)dat_datctl.oveccount; i++)
+ {
+ if (pmatch[i].rm_so >= 0)
+ {
+ PCRE2_SIZE start = pmatch[i].rm_so;
+ PCRE2_SIZE end = pmatch[i].rm_eo;
+ for (j = last_printed + 1; j < i; j++)
+ fprintf(outfile, "%2d: <unset>\n", (int)j);
+ last_printed = i;
+ if (start > end)
+ {
+ start = pmatch[i].rm_eo;
+ end = pmatch[i].rm_so;
+ fprintf(outfile, "Start of matched string is beyond its end - "
+ "displaying from end to start.\n");
+ }
+ fprintf(outfile, "%2d: ", (int)i);
+ PCHARSV(pp, start, end - start, utf, outfile);
+ fprintf(outfile, "\n");
+
+ if ((i == 0 && (dat_datctl.control & CTL_AFTERTEXT) != 0) ||
+ (dat_datctl.control & CTL_ALLAFTERTEXT) != 0)
+ {
+ fprintf(outfile, "%2d+ ", (int)i);
+ /* Note: don't use the start/end variables here because we want to
+ show the text from what is reported as the end. */
+ PCHARSV(pp, pmatch[i].rm_eo, len - pmatch[i].rm_eo, utf, outfile);
+ fprintf(outfile, "\n"); }
+ }
+ }
+ }
+ free(pmatch);
+ return PR_OK;
+ }
+#endif /* SUPPORT_PCRE2_8 */
+
+ /* Handle matching via the native interface. Check for consistency of
+modifiers. */
+
+if (dat_datctl.startend[0] != CFORE_UNSET)
+ fprintf(outfile, "** \\=posix_startend ignored for non-POSIX matching\n");
+
+/* ALLUSEDTEXT is not supported with JIT, but JIT is not used with DFA
+matching, even if the JIT compiler was used. */
+
+if ((dat_datctl.control & (CTL_ALLUSEDTEXT|CTL_DFA)) == CTL_ALLUSEDTEXT &&
+ FLD(compiled_code, executable_jit) != NULL)
+ {
+ fprintf(outfile, "** Showing all consulted text is not supported by JIT: ignored\n");
+ dat_datctl.control &= ~CTL_ALLUSEDTEXT;
+ }
+
+/* Handle passing the subject as zero-terminated. */
+
+if ((dat_datctl.control & CTL_ZERO_TERMINATE) != 0)
+ arg_ulen = PCRE2_ZERO_TERMINATED;
+
+/* The nullcontext modifier is used to test calling pcre2_[jit_]match() with a
+NULL context. */
+
+use_dat_context = ((dat_datctl.control & CTL_NULLCONTEXT) != 0)?
+ NULL : PTR(dat_context);
+
+/* Enable display of malloc/free if wanted. We can do this only if either the
+pattern or the subject is processed with a context. */
+
+show_memory = (dat_datctl.control & CTL_MEMORY) != 0;
+
+if (show_memory &&
+ (pat_patctl.control & dat_datctl.control & CTL_NULLCONTEXT) != 0)
+ fprintf(outfile, "** \\=memory requires either a pattern or a subject "
+ "context: ignored\n");
+
+/* Create and assign a JIT stack if requested. */
+
+if (dat_datctl.jitstack != 0)
+ {
+ if (dat_datctl.jitstack != jit_stack_size)
+ {
+ PCRE2_JIT_STACK_FREE(jit_stack);
+ PCRE2_JIT_STACK_CREATE(jit_stack, 1, dat_datctl.jitstack * 1024, NULL);
+ jit_stack_size = dat_datctl.jitstack;
+ }
+ PCRE2_JIT_STACK_ASSIGN(dat_context, jit_callback, jit_stack);
+ }
+
+/* Or de-assign */
+
+else if (jit_stack != NULL)
+ {
+ PCRE2_JIT_STACK_ASSIGN(dat_context, NULL, NULL);
+ PCRE2_JIT_STACK_FREE(jit_stack);
+ jit_stack = NULL;
+ jit_stack_size = 0;
+ }
+
+/* When no JIT stack is assigned, we must ensure that there is a JIT callback
+if we want to verify that JIT was actually used. */
+
+if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_stack == NULL)
+ {
+ PCRE2_JIT_STACK_ASSIGN(dat_context, jit_callback, NULL);
+ }
+
+/* Adjust match_data according to size of offsets required. A size of zero
+causes a new match data block to be obtained that exactly fits the pattern. */
+
+if (dat_datctl.oveccount == 0)
+ {
+ PCRE2_MATCH_DATA_FREE(match_data);
+ PCRE2_MATCH_DATA_CREATE_FROM_PATTERN(match_data, compiled_code, NULL);
+ PCRE2_GET_OVECTOR_COUNT(max_oveccount, match_data);
+ }
+else if (dat_datctl.oveccount <= max_oveccount)
+ {
+ SETFLD(match_data, oveccount, dat_datctl.oveccount);
+ }
+else
+ {
+ max_oveccount = dat_datctl.oveccount;
+ PCRE2_MATCH_DATA_FREE(match_data);
+ PCRE2_MATCH_DATA_CREATE(match_data, max_oveccount, NULL);
+ }
+
+if (CASTVAR(void *, match_data) == NULL)
+ {
+ fprintf(outfile, "** Failed to get memory for recording matching "
+ "information (size requested: %d)\n", dat_datctl.oveccount);
+ max_oveccount = 0;
+ return PR_OK;
+ }
+
+/* Replacement processing is ignored for DFA matching. */
+
+if (dat_datctl.replacement[0] != 0 && (dat_datctl.control & CTL_DFA) != 0)
+ {
+ fprintf(outfile, "** Ignored for DFA matching: replace\n");
+ dat_datctl.replacement[0] = 0;
+ }
+
+/* If a replacement string is provided, call pcre2_substitute() instead of one
+of the matching functions. First we have to convert the replacement string to
+the appropriate width. */
+
+if (dat_datctl.replacement[0] != 0)
+ {
+ int rc;
+ uint8_t *pr;
+ uint8_t rbuffer[REPLACE_BUFFSIZE];
+ uint8_t nbuffer[REPLACE_BUFFSIZE];
+ uint32_t xoptions;
+ PCRE2_SIZE rlen, nsize, erroroffset;
+ BOOL badutf = FALSE;
+
+#ifdef SUPPORT_PCRE2_8
+ uint8_t *r8 = NULL;
+#endif
+#ifdef SUPPORT_PCRE2_16
+ uint16_t *r16 = NULL;
+#endif
+#ifdef SUPPORT_PCRE2_32
+ uint32_t *r32 = NULL;
+#endif
+
+ if (timeitm)
+ fprintf(outfile, "** Timing is not supported with replace: ignored\n");
+
+ if ((dat_datctl.control & CTL_ALTGLOBAL) != 0)
+ fprintf(outfile, "** Altglobal is not supported with replace: ignored\n");
+
+ xoptions = (((dat_datctl.control & CTL_GLOBAL) == 0)? 0 :
+ PCRE2_SUBSTITUTE_GLOBAL) |
+ (((dat_datctl.control2 & CTL2_SUBSTITUTE_EXTENDED) == 0)? 0 :
+ PCRE2_SUBSTITUTE_EXTENDED) |
+ (((dat_datctl.control2 & CTL2_SUBSTITUTE_OVERFLOW_LENGTH) == 0)? 0 :
+ PCRE2_SUBSTITUTE_OVERFLOW_LENGTH) |
+ (((dat_datctl.control2 & CTL2_SUBSTITUTE_UNKNOWN_UNSET) == 0)? 0 :
+ PCRE2_SUBSTITUTE_UNKNOWN_UNSET) |
+ (((dat_datctl.control2 & CTL2_SUBSTITUTE_UNSET_EMPTY) == 0)? 0 :
+ PCRE2_SUBSTITUTE_UNSET_EMPTY);
+
+ SETCASTPTR(r, rbuffer); /* Sets r8, r16, or r32, as appropriate. */
+ pr = dat_datctl.replacement;
+
+ /* If the replacement starts with '[<number>]' we interpret that as length
+ value for the replacement buffer. */
+
+ nsize = REPLACE_BUFFSIZE/code_unit_size;
+ if (*pr == '[')
+ {
+ PCRE2_SIZE n = 0;
+ while ((c = *(++pr)) >= CHAR_0 && c <= CHAR_9) n = n * 10 + c - CHAR_0;
+ if (*pr++ != ']')
+ {
+ fprintf(outfile, "Bad buffer size in replacement string\n");
+ return PR_OK;
+ }
+ if (n > nsize)
+ {
+ fprintf(outfile, "Replacement buffer setting (%" SIZ_FORM ") is too "
+ "large (max %" SIZ_FORM ")\n", SIZ_CAST n, SIZ_CAST nsize);
+ return PR_OK;
+ }
+ nsize = n;
+ }
+
+ /* Now copy the replacement string to a buffer of the appropriate width. No
+ escape processing is done for replacements. In UTF mode, check for an invalid
+ UTF-8 input string, and if it is invalid, just copy its code units without
+ UTF interpretation. This provides a means of checking that an invalid string
+ is detected. Otherwise, UTF-8 can be used to include wide characters in a
+ replacement. */
+
+ if (utf) badutf = valid_utf(pr, strlen((const char *)pr), &erroroffset);
+
+ /* Not UTF or invalid UTF-8: just copy the code units. */
+
+ if (!utf || badutf)
+ {
+ while ((c = *pr++) != 0)
+ {
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE) *r8++ = c;
+#endif
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE) *r16++ = c;
+#endif
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE) *r32++ = c;
+#endif
+ }
+ }
+
+ /* Valid UTF-8 replacement string */
+
+ else while ((c = *pr++) != 0)
+ {
+ if (HASUTF8EXTRALEN(c)) { GETUTF8INC(c, pr); }
+
+#ifdef SUPPORT_PCRE2_8
+ if (test_mode == PCRE8_MODE) r8 += ord2utf8(c, r8);
+#endif
+
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)
+ {
+ if (c >= 0x10000u)
+ {
+ c-= 0x10000u;
+ *r16++ = 0xD800 | (c >> 10);
+ *r16++ = 0xDC00 | (c & 0x3ff);
+ }
+ else *r16++ = c;
+ }
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE) *r32++ = c;
+#endif
+ }
+
+ SET(*r, 0);
+ if ((dat_datctl.control & CTL_ZERO_TERMINATE) != 0)
+ rlen = PCRE2_ZERO_TERMINATED;
+ else
+ rlen = (CASTVAR(uint8_t *, r) - rbuffer)/code_unit_size;
+ PCRE2_SUBSTITUTE(rc, compiled_code, pp, arg_ulen, dat_datctl.offset,
+ dat_datctl.options|xoptions, match_data, dat_context,
+ rbuffer, rlen, nbuffer, &nsize);
+
+ if (rc < 0)
+ {
+ fprintf(outfile, "Failed: error %d", rc);
+ if (rc != PCRE2_ERROR_NOMEMORY && nsize != PCRE2_UNSET)
+ fprintf(outfile, " at offset %ld in replacement", (long int)nsize);
+ fprintf(outfile, ": ");
+ if (!print_error_message(rc, "", "")) return PR_ABEND;
+ if (rc == PCRE2_ERROR_NOMEMORY &&
+ (xoptions & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH) != 0)
+ fprintf(outfile, ": %ld code units are needed", (long int)nsize);
+ }
+ else
+ {
+ fprintf(outfile, "%2d: ", rc);
+ PCHARSV(nbuffer, 0, nsize, utf, outfile);
+ }
+
+ fprintf(outfile, "\n");
+ show_memory = FALSE;
+ return PR_OK;
+ } /* End of substitution handling */
+
+/* When a replacement string is not provided, run a loop for global matching
+with one of the basic matching functions. For altglobal (or first time round
+the loop), set an "unset" value for the previous match info. */
+
+ovecsave[0] = ovecsave[1] = ovecsave[2] = PCRE2_UNSET;
+
+for (gmatched = 0;; gmatched++)
+ {
+ PCRE2_SIZE j;
+ int capcount;
+ PCRE2_SIZE *ovector;
+
+ ovector = FLD(match_data, ovector);
+
+ /* Fill the ovector with junk to detect elements that do not get set
+ when they should be. */
+
+ for (j = 0; j < 2*dat_datctl.oveccount; j++) ovector[j] = JUNK_OFFSET;
+
+ /* When matching is via pcre2_match(), we will detect the use of JIT via the
+ stack callback function. */
+
+ jit_was_used = (pat_patctl.control & CTL_JITFAST) != 0;
+
+ /* Do timing if required. */
+
+ if (timeitm > 0)
+ {
+ int i;
+ clock_t start_time, time_taken;
+
+ if ((dat_datctl.control & CTL_DFA) != 0)
+ {
+ if ((dat_datctl.options & PCRE2_DFA_RESTART) != 0)
+ {
+ fprintf(outfile, "Timing DFA restarts is not supported\n");
+ return PR_OK;
+ }
+ if (dfa_workspace == NULL)
+ dfa_workspace = (int *)malloc(DFA_WS_DIMENSION*sizeof(int));
+ start_time = clock();
+ for (i = 0; i < timeitm; i++)
+ {
+ PCRE2_DFA_MATCH(capcount, compiled_code, pp, arg_ulen,
+ dat_datctl.offset, dat_datctl.options | g_notempty, match_data,
+ use_dat_context, dfa_workspace, DFA_WS_DIMENSION);
+ }
+ }
+
+ else if ((pat_patctl.control & CTL_JITFAST) != 0)
+ {
+ start_time = clock();
+ for (i = 0; i < timeitm; i++)
+ {
+ PCRE2_JIT_MATCH(capcount, compiled_code, pp, arg_ulen,
+ dat_datctl.offset, dat_datctl.options | g_notempty, match_data,
+ use_dat_context);
+ }
+ }
+
+ else
+ {
+ start_time = clock();
+ for (i = 0; i < timeitm; i++)
+ {
+ PCRE2_MATCH(capcount, compiled_code, pp, arg_ulen,
+ dat_datctl.offset, dat_datctl.options | g_notempty, match_data,
+ use_dat_context);
+ }
+ }
+ total_match_time += (time_taken = clock() - start_time);
+ fprintf(outfile, "Match time %.4f milliseconds\n",
+ (((double)time_taken * 1000.0) / (double)timeitm) /
+ (double)CLOCKS_PER_SEC);
+ }
+
+ /* Find the heap, match and depth limits if requested. The depth and heap
+ limits are not relevant for JIT. The return from check_match_limit() is the
+ return from the final call to pcre2_match() or pcre2_dfa_match(). */
+
+ if ((dat_datctl.control & CTL_FINDLIMITS) != 0)
+ {
+ capcount = 0; /* This stops compiler warnings */
+
+ if (FLD(compiled_code, executable_jit) == NULL ||
+ (dat_datctl.options & PCRE2_NO_JIT) != 0)
+ {
+ (void)check_match_limit(pp, arg_ulen, PCRE2_ERROR_HEAPLIMIT, "heap");
+ }
+
+ capcount = check_match_limit(pp, arg_ulen, PCRE2_ERROR_MATCHLIMIT,
+ "match");
+
+ if (FLD(compiled_code, executable_jit) == NULL ||
+ (dat_datctl.options & PCRE2_NO_JIT) != 0 ||
+ (dat_datctl.control & CTL_DFA) != 0)
+ {
+ capcount = check_match_limit(pp, arg_ulen, PCRE2_ERROR_DEPTHLIMIT,
+ "depth");
+ }
+
+ if (capcount == 0)
+ {
+ fprintf(outfile, "Matched, but offsets vector is too small to show all matches\n");
+ capcount = dat_datctl.oveccount;
+ }
+ }
+
+ /* Otherwise just run a single match, setting up a callout if required (the
+ default). There is a copy of the pattern in pbuffer8 for use by callouts. */
+
+ else
+ {
+ if ((dat_datctl.control & CTL_CALLOUT_NONE) == 0)
+ {
+ PCRE2_SET_CALLOUT(dat_context, callout_function,
+ (void *)(&dat_datctl.callout_data));
+ first_callout = TRUE;
+ last_callout_mark = NULL;
+ callout_count = 0;
+ }
+ else
+ {
+ PCRE2_SET_CALLOUT(dat_context, NULL, NULL); /* No callout */
+ }
+
+ /* Run a single DFA or NFA match. */
+
+ if ((dat_datctl.control & CTL_DFA) != 0)
+ {
+ if (dfa_workspace == NULL)
+ dfa_workspace = (int *)malloc(DFA_WS_DIMENSION*sizeof(int));
+ if (dfa_matched++ == 0)
+ dfa_workspace[0] = -1; /* To catch bad restart */
+ PCRE2_DFA_MATCH(capcount, compiled_code, pp, arg_ulen,
+ dat_datctl.offset, dat_datctl.options | g_notempty, match_data,
+ use_dat_context, dfa_workspace, DFA_WS_DIMENSION);
+ if (capcount == 0)
+ {
+ fprintf(outfile, "Matched, but offsets vector is too small to show all matches\n");
+ capcount = dat_datctl.oveccount;
+ }
+ }
+ else
+ {
+ if ((pat_patctl.control & CTL_JITFAST) != 0)
+ PCRE2_JIT_MATCH(capcount, compiled_code, pp, arg_ulen, dat_datctl.offset,
+ dat_datctl.options | g_notempty, match_data, use_dat_context);
+ else
+ PCRE2_MATCH(capcount, compiled_code, pp, arg_ulen, dat_datctl.offset,
+ dat_datctl.options | g_notempty, match_data, use_dat_context);
+ if (capcount == 0)
+ {
+ fprintf(outfile, "Matched, but too many substrings\n");
+ capcount = dat_datctl.oveccount;
+ }
+ }
+ }
+
+ /* The result of the match is now in capcount. First handle a successful
+ match. */
+
+ if (capcount >= 0)
+ {
+ int i;
+ uint32_t oveccount;
+
+ /* This is a check against a lunatic return value. */
+
+ PCRE2_GET_OVECTOR_COUNT(oveccount, match_data);
+ if (capcount > (int)oveccount)
+ {
+ fprintf(outfile,
+ "** PCRE2 error: returned count %d is too big for ovector count %d\n",
+ capcount, oveccount);
+ capcount = oveccount;
+ if ((dat_datctl.control & CTL_ANYGLOB) != 0)
+ {
+ fprintf(outfile, "** Global loop abandoned\n");
+ dat_datctl.control &= ~CTL_ANYGLOB; /* Break g/G loop */
+ }
+ }
+
+ /* If this is not the first time round a global loop, check that the
+ returned string has changed. If it has not, check for an empty string match
+ at different starting offset from the previous match. This is a failed test
+ retry for null-matching patterns that don't match at their starting offset,
+ for example /(?<=\G.)/. A repeated match at the same point is not such a
+ pattern, and must be discarded, and we then proceed to seek a non-null
+ match at the current point. For any other repeated match, there is a bug
+ somewhere and we must break the loop because it will go on for ever. We
+ know that there are always at least two elements in the ovector. */
+
+ if (gmatched > 0 && ovecsave[0] == ovector[0] && ovecsave[1] == ovector[1])
+ {
+ if (ovector[0] == ovector[1] && ovecsave[2] != dat_datctl.offset)
+ {
+ g_notempty = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
+ ovecsave[2] = dat_datctl.offset;
+ continue; /* Back to the top of the loop */
+ }
+ fprintf(outfile,
+ "** PCRE2 error: global repeat returned the same string as previous\n");
+ fprintf(outfile, "** Global loop abandoned\n");
+ dat_datctl.control &= ~CTL_ANYGLOB; /* Break g/G loop */
+ }
+
+ /* "allcaptures" requests showing of all captures in the pattern, to check
+ unset ones at the end. It may be set on the pattern or the data. Implement
+ by setting capcount to the maximum. This is not relevant for DFA matching,
+ so ignore it. */
+
+ if ((dat_datctl.control & CTL_ALLCAPTURES) != 0)
+ {
+ uint32_t maxcapcount;
+ if ((dat_datctl.control & CTL_DFA) != 0)
+ {
+ fprintf(outfile, "** Ignored after DFA matching: allcaptures\n");
+ }
+ else
+ {
+ if (pattern_info(PCRE2_INFO_CAPTURECOUNT, &maxcapcount, FALSE) < 0)
+ return PR_SKIP;
+ capcount = maxcapcount + 1; /* Allow for full match */
+ if (capcount > (int)oveccount) capcount = oveccount;
+ }
+ }
+
+ /* Output the captured substrings. Note that, for the matched string,
+ the use of \K in an assertion can make the start later than the end. */
+
+ for (i = 0; i < 2*capcount; i += 2)
+ {
+ PCRE2_SIZE lleft, lmiddle, lright;
+ PCRE2_SIZE start = ovector[i];
+ PCRE2_SIZE end = ovector[i+1];
+
+ if (start > end)
+ {
+ start = ovector[i+1];
+ end = ovector[i];
+ fprintf(outfile, "Start of matched string is beyond its end - "
+ "displaying from end to start.\n");
+ }
+
+ fprintf(outfile, "%2d: ", i/2);
+
+ /* Check for an unset group */
+
+ if (start == PCRE2_UNSET)
+ {
+ fprintf(outfile, "<unset>\n");
+ continue;
+ }
+
+ /* Check for silly offsets, in particular, values that have not been
+ set when they should have been. */
+
+ if (start > ulen || end > ulen)
+ {
+ fprintf(outfile, "ERROR: bad value(s) for offset(s): 0x%lx 0x%lx\n",
+ (unsigned long int)start, (unsigned long int)end);
+ continue;
+ }
+
+ /* When JIT is not being used, ALLUSEDTEXT may be set. (It if is set with
+ JIT, it is disabled above, with a comment.) When the match is done by the
+ interpreter, leftchar and rightchar are available, and if ALLUSEDTEXT is
+ set, and if the leftmost consulted character is before the start of the
+ match or the rightmost consulted character is past the end of the match,
+ we want to show all consulted characters for the main matched string, and
+ indicate which were lookarounds. */
+
+ if (i == 0)
+ {
+ BOOL showallused;
+ PCRE2_SIZE leftchar, rightchar;
+
+ if ((dat_datctl.control & CTL_ALLUSEDTEXT) != 0)
+ {
+ leftchar = FLD(match_data, leftchar);
+ rightchar = FLD(match_data, rightchar);
+ showallused = i == 0 && (leftchar < start || rightchar > end);
+ }
+ else showallused = FALSE;
+
+ if (showallused)
+ {
+ PCHARS(lleft, pp, leftchar, start - leftchar, utf, outfile);
+ PCHARS(lmiddle, pp, start, end - start, utf, outfile);
+ PCHARS(lright, pp, end, rightchar - end, utf, outfile);
+ if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_was_used)
+ fprintf(outfile, " (JIT)");
+ fprintf(outfile, "\n ");
+ for (j = 0; j < lleft; j++) fprintf(outfile, "<");
+ for (j = 0; j < lmiddle; j++) fprintf(outfile, " ");
+ for (j = 0; j < lright; j++) fprintf(outfile, ">");
+ }
+
+ /* When a pattern contains \K, the start of match position may be
+ different to the start of the matched string. When this is the case,
+ show it when requested. */
+
+ else if ((dat_datctl.control & CTL_STARTCHAR) != 0)
+ {
+ PCRE2_SIZE startchar;
+ PCRE2_GET_STARTCHAR(startchar, match_data);
+ PCHARS(lleft, pp, startchar, start - startchar, utf, outfile);
+ PCHARSV(pp, start, end - start, utf, outfile);
+ if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_was_used)
+ fprintf(outfile, " (JIT)");
+ if (startchar != start)
+ {
+ fprintf(outfile, "\n ");
+ for (j = 0; j < lleft; j++) fprintf(outfile, "^");
+ }
+ }
+
+ /* Otherwise, just show the matched string. */
+
+ else
+ {
+ PCHARSV(pp, start, end - start, utf, outfile);
+ if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_was_used)
+ fprintf(outfile, " (JIT)");
+ }
+ }
+
+ /* Not the main matched string. Just show it unadorned. */
+
+ else
+ {
+ PCHARSV(pp, start, end - start, utf, outfile);
+ }
+
+ fprintf(outfile, "\n");
+
+ /* Note: don't use the start/end variables here because we want to
+ show the text from what is reported as the end. */
+
+ if ((dat_datctl.control & CTL_ALLAFTERTEXT) != 0 ||
+ (i == 0 && (dat_datctl.control & CTL_AFTERTEXT) != 0))
+ {
+ fprintf(outfile, "%2d+ ", i/2);
+ PCHARSV(pp, ovector[i+1], ulen - ovector[i+1], utf, outfile);
+ fprintf(outfile, "\n");
+ }
+ }
+
+ /* Output (*MARK) data if requested */
+
+ if ((dat_datctl.control & CTL_MARK) != 0 &&
+ TESTFLD(match_data, mark, !=, NULL))
+ {
+ fprintf(outfile, "MK: ");
+ PCHARSV(CASTFLD(void *, match_data, mark), 0, -1, utf, outfile);
+ fprintf(outfile, "\n");
+ }
+
+ /* Process copy/get strings */
+
+ if (!copy_and_get(utf, capcount)) return PR_ABEND;
+
+ } /* End of handling a successful match */
+
+ /* There was a partial match. The value of ovector[0] is the bumpalong point,
+ that is, startchar, not any \K point that might have been passed. */
+
+ else if (capcount == PCRE2_ERROR_PARTIAL)
+ {
+ PCRE2_SIZE poffset;
+ int backlength;
+ int rubriclength = 0;
+
+ fprintf(outfile, "Partial match");
+ if ((dat_datctl.control & CTL_MARK) != 0 &&
+ TESTFLD(match_data, mark, !=, NULL))
+ {
+ fprintf(outfile, ", mark=");
+ PCHARS(rubriclength, CASTFLD(void *, match_data, mark), 0, -1, utf,
+ outfile);
+ rubriclength += 7;
+ }
+ fprintf(outfile, ": ");
+ rubriclength += 15;
+
+ poffset = backchars(pp, ovector[0], maxlookbehind, utf);
+ PCHARS(backlength, pp, poffset, ovector[0] - poffset, utf, outfile);
+ PCHARSV(pp, ovector[0], ulen - ovector[0], utf, outfile);
+
+ if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_was_used)
+ fprintf(outfile, " (JIT)");
+ fprintf(outfile, "\n");
+
+ if (backlength != 0)
+ {
+ int i;
+ for (i = 0; i < rubriclength; i++) fprintf(outfile, " ");
+ for (i = 0; i < backlength; i++) fprintf(outfile, "<");
+ fprintf(outfile, "\n");
+ }
+
+ /* Process copy/get strings */
+
+ if (!copy_and_get(utf, 1)) return PR_ABEND;
+
+ break; /* Out of the /g loop */
+ } /* End of handling partial match */
+
+ /* Failed to match. If this is a /g or /G loop, we might previously have
+ set g_notempty (to PCRE2_NOTEMPTY_ATSTART|PCRE2_ANCHORED) after a null match.
+ If that is the case, this is not necessarily the end. We want to advance the
+ start offset, and continue. We won't be at the end of the string - that was
+ checked before setting g_notempty. We achieve the effect by pretending that a
+ single character was matched.
+
+ Complication arises in the case when the newline convention is "any", "crlf",
+ or "anycrlf". If the previous match was at the end of a line terminated by
+ CRLF, an advance of one character just passes the CR, whereas we should
+ prefer the longer newline sequence, as does the code in pcre2_match().
+
+ Otherwise, in the case of UTF-8 or UTF-16 matching, the advance must be one
+ character, not one byte. */
+
+ else if (g_notempty != 0) /* There was a previous null match */
+ {
+ uint16_t nl = FLD(compiled_code, newline_convention);
+ PCRE2_SIZE start_offset = dat_datctl.offset; /* Where the match was */
+ PCRE2_SIZE end_offset = start_offset + 1;
+
+ if ((nl == PCRE2_NEWLINE_CRLF || nl == PCRE2_NEWLINE_ANY ||
+ nl == PCRE2_NEWLINE_ANYCRLF) &&
+ start_offset < ulen - 1 &&
+ CODE_UNIT(pp, start_offset) == '\r' &&
+ CODE_UNIT(pp, end_offset) == '\n')
+ end_offset++;
+
+ else if (utf && test_mode != PCRE32_MODE)
+ {
+ if (test_mode == PCRE8_MODE)
+ {
+ for (; end_offset < ulen; end_offset++)
+ if ((((PCRE2_SPTR8)pp)[end_offset] & 0xc0) != 0x80) break;
+ }
+ else /* 16-bit mode */
+ {
+ for (; end_offset < ulen; end_offset++)
+ if ((((PCRE2_SPTR16)pp)[end_offset] & 0xfc00) != 0xdc00) break;
+ }
+ }
+
+ SETFLDVEC(match_data, ovector, 0, start_offset);
+ SETFLDVEC(match_data, ovector, 1, end_offset);
+ } /* End of handling null match in a global loop */
+
+ /* A "normal" match failure. There will be a negative error number in
+ capcount. */
+
+ else
+ {
+ switch(capcount)
+ {
+ case PCRE2_ERROR_NOMATCH:
+ if (gmatched == 0)
+ {
+ fprintf(outfile, "No match");
+ if ((dat_datctl.control & CTL_MARK) != 0 &&
+ TESTFLD(match_data, mark, !=, NULL))
+ {
+ fprintf(outfile, ", mark = ");
+ PCHARSV(CASTFLD(void *, match_data, mark), 0, -1, utf, outfile);
+ }
+ if ((pat_patctl.control & CTL_JITVERIFY) != 0 && jit_was_used)
+ fprintf(outfile, " (JIT)");
+ fprintf(outfile, "\n");
+ }
+ break;
+
+ case PCRE2_ERROR_BADUTFOFFSET:
+ fprintf(outfile, "Error %d (bad UTF-%d offset)\n", capcount, test_mode);
+ break;
+
+ default:
+ fprintf(outfile, "Failed: error %d: ", capcount);
+ if (!print_error_message(capcount, "", "")) return PR_ABEND;
+ if (capcount <= PCRE2_ERROR_UTF8_ERR1 &&
+ capcount >= PCRE2_ERROR_UTF32_ERR2)
+ {
+ PCRE2_SIZE startchar;
+ PCRE2_GET_STARTCHAR(startchar, match_data);
+ fprintf(outfile, " at offset %" SIZ_FORM, SIZ_CAST startchar);
+ }
+ fprintf(outfile, "\n");
+ break;
+ }
+
+ break; /* Out of the /g loop */
+ } /* End of failed match handling */
+
+ /* Control reaches here in two circumstances: (a) after a match, and (b)
+ after a non-match that immediately followed a match on an empty string when
+ doing a global search. Such a match is done with PCRE2_NOTEMPTY_ATSTART and
+ PCRE2_ANCHORED set in g_notempty. The code above turns it into a fake match
+ of one character. So effectively we get here only after a match. If we
+ are not doing a global search, we are done. */
+
+ if ((dat_datctl.control & CTL_ANYGLOB) == 0) break; else
+ {
+ PCRE2_SIZE match_offset = FLD(match_data, ovector)[0];
+ PCRE2_SIZE end_offset = FLD(match_data, ovector)[1];
+
+ /* We must now set up for the next iteration of a global search. If we have
+ matched an empty string, first check to see if we are at the end of the
+ subject. If so, the loop is over. Otherwise, mimic what Perl's /g option
+ does. Set PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED and try the match again
+ at the same point. If this fails it will be picked up above, where a fake
+ match is set up so that at this point we advance to the next character.
+
+ However, in order to cope with patterns that never match at their starting
+ offset (e.g. /(?<=\G.)/) we don't do this when the match offset is greater
+ than the starting offset. This means there will be a retry with the
+ starting offset at the match offset. If this returns the same match again,
+ it is picked up above and ignored, and the special action is then taken. */
+
+ if (match_offset == end_offset)
+ {
+ if (end_offset == ulen) break; /* End of subject */
+ if (match_offset <= dat_datctl.offset)
+ g_notempty = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
+ }
+
+ /* However, even after matching a non-empty string, there is still one
+ tricky case. If a pattern contains \K within a lookbehind assertion at the
+ start, the end of the matched string can be at the offset where the match
+ started. In the case of a normal /g iteration without special action, this
+ leads to a loop that keeps on returning the same substring. The loop would
+ be caught above, but we really want to move on to the next match. */
+
+ else
+ {
+ g_notempty = 0; /* Set for a "normal" repeat */
+ if ((dat_datctl.control & CTL_GLOBAL) != 0)
+ {
+ PCRE2_SIZE startchar;
+ PCRE2_GET_STARTCHAR(startchar, match_data);
+ if (end_offset <= startchar)
+ {
+ if (startchar >= ulen) break; /* End of subject */
+ end_offset = startchar + 1;
+ if (utf && test_mode != PCRE32_MODE)
+ {
+ if (test_mode == PCRE8_MODE)
+ {
+ for (; end_offset < ulen; end_offset++)
+ if ((((PCRE2_SPTR8)pp)[end_offset] & 0xc0) != 0x80) break;
+ }
+ else /* 16-bit mode */
+ {
+ for (; end_offset < ulen; end_offset++)
+ if ((((PCRE2_SPTR16)pp)[end_offset] & 0xfc00) != 0xdc00) break;
+ }
+ }
+ }
+ }
+ }
+
+ /* For a normal global (/g) iteration, save the current ovector[0,1] and
+ the starting offset so that we can check that they do change each time.
+ Otherwise a matching bug that returns the same string causes an infinite
+ loop. It has happened! Then update the start offset, leaving other
+ parameters alone. */
+
+ if ((dat_datctl.control & CTL_GLOBAL) != 0)
+ {
+ ovecsave[0] = ovector[0];
+ ovecsave[1] = ovector[1];
+ ovecsave[2] = dat_datctl.offset;
+ dat_datctl.offset = end_offset;
+ }
+
+ /* For altglobal, just update the pointer and length. */
+
+ else
+ {
+ pp += end_offset * code_unit_size;
+ len -= end_offset * code_unit_size;
+ ulen -= end_offset;
+ if (arg_ulen != PCRE2_ZERO_TERMINATED) arg_ulen -= end_offset;
+ }
+ }
+ } /* End of global loop */
+
+show_memory = FALSE;
+return PR_OK;
+}
+
+
+
+
+/*************************************************
+* Print PCRE2 version *
+*************************************************/
+
+static void
+print_version(FILE *f)
+{
+VERSION_TYPE *vp;
+fprintf(f, "PCRE2 version ");
+for (vp = version; *vp != 0; vp++) fprintf(f, "%c", *vp);
+fprintf(f, "\n");
+}
+
+
+
+/*************************************************
+* Print Unicode version *
+*************************************************/
+
+static void
+print_unicode_version(FILE *f)
+{
+VERSION_TYPE *vp;
+fprintf(f, "Unicode version ");
+for (vp = uversion; *vp != 0; vp++) fprintf(f, "%c", *vp);
+}
+
+
+
+/*************************************************
+* Print JIT target *
+*************************************************/
+
+static void
+print_jit_target(FILE *f)
+{
+VERSION_TYPE *vp;
+for (vp = jittarget; *vp != 0; vp++) fprintf(f, "%c", *vp);
+}
+
+
+
+/*************************************************
+* Print newline configuration *
+*************************************************/
+
+/* Output is always to stdout.
+
+Arguments:
+ rc the return code from PCRE2_CONFIG_NEWLINE
+ isc TRUE if called from "-C newline"
+Returns: nothing
+*/
+
+static void
+print_newline_config(uint32_t optval, BOOL isc)
+{
+if (!isc) printf(" Newline sequence is ");
+if (optval < sizeof(newlines)/sizeof(char *))
+ printf("%s\n", newlines[optval]);
+else
+ printf("a non-standard value: %d\n", optval);
+}
+
+
+
+/*************************************************
+* Usage function *
+*************************************************/
+
+static void
+usage(void)
+{
+printf("Usage: pcre2test [options] [<input file> [<output file>]]\n\n");
+printf("Input and output default to stdin and stdout.\n");
+#if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT)
+printf("If input is a terminal, readline() is used to read from it.\n");
+#else
+printf("This version of pcre2test is not linked with readline().\n");
+#endif
+printf("\nOptions:\n");
+#ifdef SUPPORT_PCRE2_8
+printf(" -8 use the 8-bit library\n");
+#endif
+#ifdef SUPPORT_PCRE2_16
+printf(" -16 use the 16-bit library\n");
+#endif
+#ifdef SUPPORT_PCRE2_32
+printf(" -32 use the 32-bit library\n");
+#endif
+printf(" -ac set default pattern modifier PCRE2_AUTO_CALLOUT\n");
+printf(" -AC as -ac, but also set subject 'callout_extra' modifier\n");
+printf(" -b set default pattern modifier 'fullbincode'\n");
+printf(" -C show PCRE2 compile-time options and exit\n");
+printf(" -C arg show a specific compile-time option and exit with its\n");
+printf(" value if numeric (else 0). The arg can be:\n");
+printf(" backslash-C use of \\C is enabled [0, 1]\n");
+printf(" bsr \\R type [ANYCRLF, ANY]\n");
+printf(" ebcdic compiled for EBCDIC character code [0,1]\n");
+printf(" ebcdic-nl NL code if compiled for EBCDIC\n");
+printf(" jit just-in-time compiler supported [0, 1]\n");
+printf(" linksize internal link size [2, 3, 4]\n");
+printf(" newline newline type [CR, LF, CRLF, ANYCRLF, ANY, NUL]\n");
+printf(" pcre2-8 8 bit library support enabled [0, 1]\n");
+printf(" pcre2-16 16 bit library support enabled [0, 1]\n");
+printf(" pcre2-32 32 bit library support enabled [0, 1]\n");
+printf(" unicode Unicode and UTF support enabled [0, 1]\n");
+printf(" -d set default pattern modifier 'debug'\n");
+printf(" -dfa set default subject modifier 'dfa'\n");
+printf(" -error <n,m,..> show messages for error numbers, then exit\n");
+printf(" -help show usage information\n");
+printf(" -i set default pattern modifier 'info'\n");
+printf(" -jit set default pattern modifier 'jit'\n");
+printf(" -jitverify set default pattern modifier 'jitverify'\n");
+printf(" -LM list pattern and subject modifiers, then exit\n");
+printf(" -q quiet: do not output PCRE2 version number at start\n");
+printf(" -pattern <s> set default pattern modifier fields\n");
+printf(" -subject <s> set default subject modifier fields\n");
+printf(" -S <n> set stack size to <n> mebibytes\n");
+printf(" -t [<n>] time compilation and execution, repeating <n> times\n");
+printf(" -tm [<n>] time execution (matching) only, repeating <n> times\n");
+printf(" -T same as -t, but show total times at the end\n");
+printf(" -TM same as -tm, but show total time at the end\n");
+printf(" -version show PCRE2 version and exit\n");
+}
+
+
+
+/*************************************************
+* Handle -C option *
+*************************************************/
+
+/* This option outputs configuration options and sets an appropriate return
+code when asked for a single option. The code is abstracted into a separate
+function because of its size. Use whichever pcre2_config() function is
+available.
+
+Argument: an option name or NULL
+Returns: the return code
+*/
+
+static int
+c_option(const char *arg)
+{
+uint32_t optval;
+unsigned int i = COPTLISTCOUNT;
+int yield = 0;
+
+if (arg != NULL && arg[0] != CHAR_MINUS)
+ {
+ for (i = 0; i < COPTLISTCOUNT; i++)
+ if (strcmp(arg, coptlist[i].name) == 0) break;
+
+ if (i >= COPTLISTCOUNT)
+ {
+ fprintf(stderr, "** Unknown -C option '%s'\n", arg);
+ return 0;
+ }
+
+ switch (coptlist[i].type)
+ {
+ case CONF_BSR:
+ (void)PCRE2_CONFIG(coptlist[i].value, &optval);
+ printf("%s\n", (optval == PCRE2_BSR_ANYCRLF)? "ANYCRLF" : "ANY");
+ break;
+
+ case CONF_FIX:
+ yield = coptlist[i].value;
+ printf("%d\n", yield);
+ break;
+
+ case CONF_FIZ:
+ optval = coptlist[i].value;
+ printf("%d\n", optval);
+ break;
+
+ case CONF_INT:
+ (void)PCRE2_CONFIG(coptlist[i].value, &yield);
+ printf("%d\n", yield);
+ break;
+
+ case CONF_NL:
+ (void)PCRE2_CONFIG(coptlist[i].value, &optval);
+ print_newline_config(optval, TRUE);
+ break;
+ }
+
+/* For VMS, return the value by setting a symbol, for certain values only. */
+
+#ifdef __VMS
+ if (copytlist[i].type == CONF_FIX || coptlist[i].type == CONF_INT)
+ {
+ char ucname[16];
+ strcpy(ucname, coptlist[i].name);
+ for (i = 0; ucname[i] != 0; i++) ucname[i] = toupper[ucname[i]];
+ vms_setsymbol(ucname, 0, optval);
+ }
+#endif
+
+ return yield;
+ }
+
+/* No argument for -C: output all configuration information. */
+
+print_version(stdout);
+printf("Compiled with\n");
+
+#ifdef EBCDIC
+printf(" EBCDIC code support: LF is 0x%02x\n", CHAR_LF);
+#if defined NATIVE_ZOS
+printf(" EBCDIC code page %s or similar\n", pcrz_cpversion());
+#endif
+#endif
+
+(void)PCRE2_CONFIG(PCRE2_CONFIG_COMPILED_WIDTHS, &optval);
+if (optval & 1) printf(" 8-bit support\n");
+if (optval & 2) printf(" 16-bit support\n");
+if (optval & 4) printf(" 32-bit support\n");
+
+#ifdef SUPPORT_VALGRIND
+printf(" Valgrind support\n");
+#endif
+
+(void)PCRE2_CONFIG(PCRE2_CONFIG_UNICODE, &optval);
+if (optval != 0)
+ {
+ printf(" UTF and UCP support (");
+ print_unicode_version(stdout);
+ printf(")\n");
+ }
+else printf(" No Unicode support\n");
+
+(void)PCRE2_CONFIG(PCRE2_CONFIG_JIT, &optval);
+if (optval != 0)
+ {
+ printf(" Just-in-time compiler support: ");
+ print_jit_target(stdout);
+ printf("\n");
+ }
+else
+ {
+ printf(" No just-in-time compiler support\n");
+ }
+
+(void)PCRE2_CONFIG(PCRE2_CONFIG_NEWLINE, &optval);
+print_newline_config(optval, FALSE);
+(void)PCRE2_CONFIG(PCRE2_CONFIG_BSR, &optval);
+printf(" \\R matches %s\n",
+ (optval == PCRE2_BSR_ANYCRLF)? "CR, LF, or CRLF only" :
+ "all Unicode newlines");
+(void)PCRE2_CONFIG(PCRE2_CONFIG_NEVER_BACKSLASH_C, &optval);
+printf(" \\C is %ssupported\n", optval? "not ":"");
+(void)PCRE2_CONFIG(PCRE2_CONFIG_LINKSIZE, &optval);
+printf(" Internal link size = %d\n", optval);
+(void)PCRE2_CONFIG(PCRE2_CONFIG_PARENSLIMIT, &optval);
+printf(" Parentheses nest limit = %d\n", optval);
+(void)PCRE2_CONFIG(PCRE2_CONFIG_HEAPLIMIT, &optval);
+printf(" Default heap limit = %d\n", optval);
+(void)PCRE2_CONFIG(PCRE2_CONFIG_MATCHLIMIT, &optval);
+printf(" Default match limit = %d\n", optval);
+(void)PCRE2_CONFIG(PCRE2_CONFIG_DEPTHLIMIT, &optval);
+printf(" Default depth limit = %d\n", optval);
+return 0;
+}
+
+
+
+/*************************************************
+* Display one modifier *
+*************************************************/
+
+static void
+display_one_modifier(modstruct *m, BOOL for_pattern)
+{
+uint32_t c = (!for_pattern && (m->which == MOD_PND || m->which == MOD_PNDP))?
+ '*' : ' ';
+printf("%c%s", c, m->name);
+}
+
+
+
+/*************************************************
+* Display pattern or subject modifiers *
+*************************************************/
+
+/* In order to print in two columns, first scan without printing to get a list
+of the modifiers that are required.
+
+Arguments:
+ for_pattern TRUE for pattern modifiers, FALSE for subject modifiers
+ title string to be used in title
+
+Returns: nothing
+*/
+
+static void
+display_selected_modifiers(BOOL for_pattern, const char *title)
+{
+uint32_t i, j;
+uint32_t n = 0;
+uint32_t list[MODLISTCOUNT];
+
+for (i = 0; i < MODLISTCOUNT; i++)
+ {
+ BOOL is_pattern = TRUE;
+ modstruct *m = modlist + i;
+
+ switch (m->which)
+ {
+ case MOD_CTC: /* Compile context */
+ case MOD_PAT: /* Pattern */
+ case MOD_PATP: /* Pattern, OK for Perl-compatible test */
+ break;
+
+ /* The MOD_PND and MOD_PNDP modifiers are precisely those that affect
+ subjects, but can be given with a pattern. We list them as subject
+ modifiers, but marked with an asterisk.*/
+
+ case MOD_CTM: /* Match context */
+ case MOD_DAT: /* Subject line */
+ case MOD_PND: /* As PD, but not default pattern */
+ case MOD_PNDP: /* As PND, OK for Perl-compatible test */
+ is_pattern = FALSE;
+ break;
+
+ default: printf("** Unknown type for modifier '%s'\n", m->name);
+ /* Fall through */
+ case MOD_PD: /* Pattern or subject */
+ case MOD_PDP: /* As PD, OK for Perl-compatible test */
+ is_pattern = for_pattern;
+ break;
+ }
+
+ if (for_pattern == is_pattern) list[n++] = i;
+ }
+
+/* Now print from the list in two columns. */
+
+printf("-------------- %s MODIFIERS --------------\n", title);
+
+for (i = 0, j = (n+1)/2; i < (n+1)/2; i++, j++)
+ {
+ modstruct *m = modlist + list[i];
+ display_one_modifier(m, for_pattern);
+ if (j < n)
+ {
+ uint32_t k = 27 - strlen(m->name);
+ while (k-- > 0) printf(" ");
+ display_one_modifier(modlist + list[j], for_pattern);
+ }
+ printf("\n");
+ }
+}
+
+
+
+/*************************************************
+* Display the list of modifiers *
+*************************************************/
+
+static void
+display_modifiers(void)
+{
+printf(
+ "An asterisk on a subject modifier means that it may be given on a pattern\n"
+ "line, in order to apply to all subjects matched by that pattern. Modifiers\n"
+ "that are listed for both patterns and subjects have different effects in\n"
+ "each case.\n\n");
+display_selected_modifiers(TRUE, "PATTERN");
+printf("\n");
+display_selected_modifiers(FALSE, "SUBJECT");
+}
+
+
+
+/*************************************************
+* Main Program *
+*************************************************/
+
+int
+main(int argc, char **argv)
+{
+uint32_t temp;
+uint32_t yield = 0;
+uint32_t op = 1;
+BOOL notdone = TRUE;
+BOOL quiet = FALSE;
+BOOL showtotaltimes = FALSE;
+BOOL skipping = FALSE;
+char *arg_subject = NULL;
+char *arg_pattern = NULL;
+char *arg_error = NULL;
+
+/* DM */
+ clock_prepare();
+ clock_start();
+
+/* The offsets to the options and control bits fields of the pattern and data
+control blocks must be the same so that common options and controls such as
+"anchored" or "memory" can work for either of them from a single table entry.
+We cannot test this till runtime because "offsetof" does not work in the
+preprocessor. */
+
+if (PO(options) != DO(options) || PO(control) != DO(control) ||
+ PO(control2) != DO(control2))
+ {
+ fprintf(stderr, "** Coding error: "
+ "options and control offsets for pattern and data must be the same.\n");
+ return 1;
+ }
+
+/* Get the PCRE2 and Unicode version number and JIT target information, at the
+same time checking that a request for the length gives the same answer. Also
+check lengths for non-string items. */
+
+if (PCRE2_CONFIG(PCRE2_CONFIG_VERSION, NULL) !=
+ PCRE2_CONFIG(PCRE2_CONFIG_VERSION, version) ||
+
+ PCRE2_CONFIG(PCRE2_CONFIG_UNICODE_VERSION, NULL) !=
+ PCRE2_CONFIG(PCRE2_CONFIG_UNICODE_VERSION, uversion) ||
+
+ PCRE2_CONFIG(PCRE2_CONFIG_JITTARGET, NULL) !=
+ PCRE2_CONFIG(PCRE2_CONFIG_JITTARGET, jittarget) ||
+
+ PCRE2_CONFIG(PCRE2_CONFIG_UNICODE, NULL) != sizeof(uint32_t) ||
+ PCRE2_CONFIG(PCRE2_CONFIG_MATCHLIMIT, NULL) != sizeof(uint32_t))
+ {
+ fprintf(stderr, "** Error in pcre2_config(): bad length\n");
+ return 1;
+ }
+
+/* Check that bad options are diagnosed. */
+
+if (PCRE2_CONFIG(999, NULL) != PCRE2_ERROR_BADOPTION ||
+ PCRE2_CONFIG(999, &temp) != PCRE2_ERROR_BADOPTION)
+ {
+ fprintf(stderr, "** Error in pcre2_config(): bad option not diagnosed\n");
+ return 1;
+ }
+
+/* This configuration option is now obsolete, but running a quick check ensures
+that its code is covered. */
+
+(void)PCRE2_CONFIG(PCRE2_CONFIG_STACKRECURSE, &temp);
+
+/* Get buffers from malloc() so that valgrind will check their misuse when
+debugging. They grow automatically when very long lines are read. The 16-
+and 32-bit buffers (pbuffer16, pbuffer32) are obtained only if needed. */
+
+buffer = (uint8_t *)malloc(pbuffer8_size);
+pbuffer8 = (uint8_t *)malloc(pbuffer8_size);
+
+/* The following _setmode() stuff is some Windows magic that tells its runtime
+library to translate CRLF into a single LF character. At least, that's what
+I've been told: never having used Windows I take this all on trust. Originally
+it set 0x8000, but then I was advised that _O_BINARY was better. */
+
+#if defined(_WIN32) || defined(WIN32)
+_setmode( _fileno( stdout ), _O_BINARY );
+#endif
+
+/* Initialization that does not depend on the running mode. */
+
+locale_name[0] = 0;
+
+memset(&def_patctl, 0, sizeof(patctl));
+def_patctl.convert_type = CONVERT_UNSET;
+
+memset(&def_datctl, 0, sizeof(datctl));
+def_datctl.oveccount = DEFAULT_OVECCOUNT;
+def_datctl.copy_numbers[0] = -1;
+def_datctl.get_numbers[0] = -1;
+def_datctl.startend[0] = def_datctl.startend[1] = CFORE_UNSET;
+def_datctl.cerror[0] = def_datctl.cerror[1] = CFORE_UNSET;
+def_datctl.cfail[0] = def_datctl.cfail[1] = CFORE_UNSET;
+
+/* Scan command line options. */
+
+while (argc > 1 && argv[op][0] == '-' && argv[op][1] != 0)
+ {
+ char *endptr;
+ char *arg = argv[op];
+ unsigned long uli;
+
+ /* List modifiers and exit. */
+
+ if (strcmp(arg, "-LM") == 0)
+ {
+ display_modifiers();
+ goto EXIT;
+ }
+
+ /* Display and/or set return code for configuration options. */
+
+ if (strcmp(arg, "-C") == 0)
+ {
+ yield = c_option(argv[op + 1]);
+ goto EXIT;
+ }
+
+ /* Select operating mode. Ensure that pcre2_config() is called in 16-bit
+ and 32-bit modes because that won't happen naturally when 8-bit is also
+ configured. Also call some other functions that are not otherwise used. This
+ means that a coverage report won't claim there are uncalled functions. */
+
+ if (strcmp(arg, "-8") == 0)
+ {
+#ifdef SUPPORT_PCRE2_8
+ test_mode = PCRE8_MODE;
+ (void)pcre2_set_bsr_8(pat_context8, 999);
+ (void)pcre2_set_newline_8(pat_context8, 999);
+#else
+ fprintf(stderr,
+ "** This version of PCRE2 was built without 8-bit support\n");
+ exit(1);
+#endif
+ }
+
+ else if (strcmp(arg, "-16") == 0)
+ {
+#ifdef SUPPORT_PCRE2_16
+ test_mode = PCRE16_MODE;
+ (void)pcre2_config_16(PCRE2_CONFIG_VERSION, NULL);
+ (void)pcre2_set_bsr_16(pat_context16, 999);
+ (void)pcre2_set_newline_16(pat_context16, 999);
+#else
+ fprintf(stderr,
+ "** This version of PCRE2 was built without 16-bit support\n");
+ exit(1);
+#endif
+ }
+
+ else if (strcmp(arg, "-32") == 0)
+ {
+#ifdef SUPPORT_PCRE2_32
+ test_mode = PCRE32_MODE;
+ (void)pcre2_config_32(PCRE2_CONFIG_VERSION, NULL);
+ (void)pcre2_set_bsr_32(pat_context32, 999);
+ (void)pcre2_set_newline_32(pat_context32, 999);
+#else
+ fprintf(stderr,
+ "** This version of PCRE2 was built without 32-bit support\n");
+ exit(1);
+#endif
+ }
+
+ /* Set quiet (no version verification) */
+
+ else if (strcmp(arg, "-q") == 0) quiet = TRUE;
+
+ /* Set system stack size */
+
+ else if (strcmp(arg, "-S") == 0 && argc > 2 &&
+ ((uli = strtoul(argv[op+1], &endptr, 10)), *endptr == 0))
+ {
+#if defined(_WIN32) || defined(WIN32) || defined(__minix) || defined(NATIVE_ZOS) || defined(__VMS) || defined(__K1__)
+ fprintf(stderr, "pcre2test: -S is not supported on this OS\n");
+ exit(1);
+#else
+ int rc;
+ uint32_t stack_size;
+ struct rlimit rlim;
+ if (U32OVERFLOW(uli))
+ {
+ fprintf(stderr, "** Argument for -S is too big\n");
+ exit(1);
+ }
+ stack_size = (uint32_t)uli;
+ getrlimit(RLIMIT_STACK, &rlim);
+ rlim.rlim_cur = stack_size * 1024 * 1024;
+ if (rlim.rlim_cur > rlim.rlim_max)
+ {
+ fprintf(stderr,
+ "pcre2test: requested stack size %luMiB is greater than hard limit "
+ "%luMiB\n", (unsigned long int)stack_size,
+ (unsigned long int)(rlim.rlim_max));
+ exit(1);
+ }
+ rc = setrlimit(RLIMIT_STACK, &rlim);
+ if (rc != 0)
+ {
+ fprintf(stderr, "pcre2test: setting stack size %luMiB failed: %s\n",
+ (unsigned long int)stack_size, strerror(errno));
+ exit(1);
+ }
+ op++;
+ argc--;
+#endif
+ }
+
+ /* Set some common pattern and subject controls */
+
+ else if (strcmp(arg, "-AC") == 0)
+ {
+ def_patctl.options |= PCRE2_AUTO_CALLOUT;
+ def_datctl.control2 |= CTL2_CALLOUT_EXTRA;
+ }
+ else if (strcmp(arg, "-ac") == 0) def_patctl.options |= PCRE2_AUTO_CALLOUT;
+ else if (strcmp(arg, "-b") == 0) def_patctl.control |= CTL_FULLBINCODE;
+ else if (strcmp(arg, "-d") == 0) def_patctl.control |= CTL_DEBUG;
+ else if (strcmp(arg, "-dfa") == 0) def_datctl.control |= CTL_DFA;
+ else if (strcmp(arg, "-i") == 0) def_patctl.control |= CTL_INFO;
+ else if (strcmp(arg, "-jit") == 0 || strcmp(arg, "-jitverify") == 0)
+ {
+ if (arg[4] != 0) def_patctl.control |= CTL_JITVERIFY;
+ def_patctl.jit = 7; /* full & partial */
+#ifndef SUPPORT_JIT
+ fprintf(stderr, "** Warning: JIT support is not available: "
+ "-jit[verify] calls functions that do nothing.\n");
+#endif
+ }
+
+ /* Set timing parameters */
+
+ else if (strcmp(arg, "-t") == 0 || strcmp(arg, "-tm") == 0 ||
+ strcmp(arg, "-T") == 0 || strcmp(arg, "-TM") == 0)
+ {
+ int both = arg[2] == 0;
+ showtotaltimes = arg[1] == 'T';
+ if (argc > 2 && (uli = strtoul(argv[op+1], &endptr, 10), *endptr == 0))
+ {
+ if (U32OVERFLOW(uli))
+ {
+ fprintf(stderr, "** Argument for %s is too big\n", arg);
+ exit(1);
+ }
+ timeitm = (int)uli;
+ op++;
+ argc--;
+ }
+ else timeitm = LOOPREPEAT;
+ if (both) timeit = timeitm;
+ }
+
+ /* Give help */
+
+ else if (strcmp(arg, "-help") == 0 ||
+ strcmp(arg, "--help") == 0)
+ {
+ usage();
+ goto EXIT;
+ }
+
+ /* Show version */
+
+ else if (strcmp(arg, "-version") == 0 ||
+ strcmp(arg, "--version") == 0)
+ {
+ print_version(stdout);
+ goto EXIT;
+ }
+
+ /* The following options save their data for processing once we know what
+ the running mode is. */
+
+ else if (strcmp(arg, "-error") == 0)
+ {
+ arg_error = argv[op+1];
+ goto CHECK_VALUE_EXISTS;
+ }
+
+ else if (strcmp(arg, "-subject") == 0)
+ {
+ arg_subject = argv[op+1];
+ goto CHECK_VALUE_EXISTS;
+ }
+
+ else if (strcmp(arg, "-pattern") == 0)
+ {
+ arg_pattern = argv[op+1];
+ CHECK_VALUE_EXISTS:
+ if (argc <= 2)
+ {
+ fprintf(stderr, "** Missing value for %s\n", arg);
+ yield = 1;
+ goto EXIT;
+ }
+ op++;
+ argc--;
+ }
+
+ /* Unrecognized option */
+
+ else
+ {
+ fprintf(stderr, "** Unknown or malformed option '%s'\n", arg);
+ usage();
+ yield = 1;
+ goto EXIT;
+ }
+ op++;
+ argc--;
+ }
+
+/* If -error was present, get the error numbers, show the messages, and exit.
+We wait to do this until we know which mode we are in. */
+
+if (arg_error != NULL)
+ {
+ int len;
+ int errcode;
+ char *endptr;
+
+/* Ensure the relevant non-8-bit buffer is available. Ensure that it is at
+least 128 code units, because it is used for retrieving error messages. */
+
+#ifdef SUPPORT_PCRE2_16
+ if (test_mode == PCRE16_MODE)
+ {
+ pbuffer16_size = 256;
+ pbuffer16 = (uint16_t *)malloc(pbuffer16_size);
+ if (pbuffer16 == NULL)
+ {
+ fprintf(stderr, "pcre2test: malloc(%" SIZ_FORM ") failed for pbuffer16\n",
+ SIZ_CAST pbuffer16_size);
+ yield = 1;
+ goto EXIT;
+ }
+ }
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+ if (test_mode == PCRE32_MODE)
+ {
+ pbuffer32_size = 512;
+ pbuffer32 = (uint32_t *)malloc(pbuffer32_size);
+ if (pbuffer32 == NULL)
+ {
+ fprintf(stderr, "pcre2test: malloc(%" SIZ_FORM ") failed for pbuffer32\n",
+ SIZ_CAST pbuffer32_size);
+ yield = 1;
+ goto EXIT;
+ }
+ }
+#endif
+
+ /* Loop along a list of error numbers. */
+
+ for (;;)
+ {
+ errcode = strtol(arg_error, &endptr, 10);
+ if (*endptr != 0 && *endptr != CHAR_COMMA)
+ {
+ fprintf(stderr, "** '%s' is not a valid error number list\n", arg_error);
+ yield = 1;
+ goto EXIT;
+ }
+ printf("Error %d: ", errcode);
+ PCRE2_GET_ERROR_MESSAGE(len, errcode, pbuffer);
+ if (len < 0)
+ {
+ switch (len)
+ {
+ case PCRE2_ERROR_BADDATA:
+ printf("PCRE2_ERROR_BADDATA (unknown error number)");
+ break;
+
+ case PCRE2_ERROR_NOMEMORY:
+ printf("PCRE2_ERROR_NOMEMORY (buffer too small)");
+ break;
+
+ default:
+ printf("Unexpected return (%d) from pcre2_get_error_message()", len);
+ break;
+ }
+ }
+ else
+ {
+ PCHARSV(CASTVAR(void *, pbuffer), 0, len, FALSE, stdout);
+ }
+ printf("\n");
+ if (*endptr == 0) goto EXIT;
+ arg_error = endptr + 1;
+ }
+ /* Control never reaches here */
+ } /* End of -error handling */
+
+/* Initialize things that cannot be done until we know which test mode we are
+running in. Exercise the general context copying function, which is not
+otherwise used. */
+
+code_unit_size = test_mode/8;
+max_oveccount = DEFAULT_OVECCOUNT;
+
+/* Use macros to save a lot of duplication. */
+
+#define CREATECONTEXTS \
+ G(general_context,BITS) = G(pcre2_general_context_create_,BITS)(&my_malloc, &my_free, NULL); \
+ G(general_context_copy,BITS) = G(pcre2_general_context_copy_,BITS)(G(general_context,BITS)); \
+ G(default_pat_context,BITS) = G(pcre2_compile_context_create_,BITS)(G(general_context,BITS)); \
+ G(pat_context,BITS) = G(pcre2_compile_context_copy_,BITS)(G(default_pat_context,BITS)); \
+ G(default_dat_context,BITS) = G(pcre2_match_context_create_,BITS)(G(general_context,BITS)); \
+ G(dat_context,BITS) = G(pcre2_match_context_copy_,BITS)(G(default_dat_context,BITS)); \
+ G(default_con_context,BITS) = G(pcre2_convert_context_create_,BITS)(G(general_context,BITS)); \
+ G(con_context,BITS) = G(pcre2_convert_context_copy_,BITS)(G(default_con_context,BITS)); \
+ G(match_data,BITS) = G(pcre2_match_data_create_,BITS)(max_oveccount, G(general_context,BITS))
+
+#define CONTEXTTESTS \
+ (void)G(pcre2_set_compile_extra_options_,BITS)(G(pat_context,BITS), 0); \
+ (void)G(pcre2_set_max_pattern_length_,BITS)(G(pat_context,BITS), 0); \
+ (void)G(pcre2_set_offset_limit_,BITS)(G(dat_context,BITS), 0); \
+ (void)G(pcre2_set_recursion_memory_management_,BITS)(G(dat_context,BITS), my_malloc, my_free, NULL)
+
+/* Call the appropriate functions for the current mode, and exercise some
+functions that are not otherwise called. */
+
+#ifdef SUPPORT_PCRE2_8
+#undef BITS
+#define BITS 8
+if (test_mode == PCRE8_MODE)
+ {
+ CREATECONTEXTS;
+ CONTEXTTESTS;
+ }
+#endif
+
+#ifdef SUPPORT_PCRE2_16
+#undef BITS
+#define BITS 16
+if (test_mode == PCRE16_MODE)
+ {
+ CREATECONTEXTS;
+ CONTEXTTESTS;
+ }
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+#undef BITS
+#define BITS 32
+if (test_mode == PCRE32_MODE)
+ {
+ CREATECONTEXTS;
+ CONTEXTTESTS;
+ }
+#endif
+
+/* Set a default parentheses nest limit that is large enough to run the
+standard tests (this also exercises the function). */
+
+PCRE2_SET_PARENS_NEST_LIMIT(default_pat_context, PARENS_NEST_DEFAULT);
+
+/* Handle command line modifier settings, sending any error messages to
+stderr. We need to know the mode before modifying the context, and it is tidier
+to do them all in the same way. */
+
+outfile = stderr;
+if ((arg_pattern != NULL &&
+ !decode_modifiers((uint8_t *)arg_pattern, CTX_DEFPAT, &def_patctl, NULL)) ||
+ (arg_subject != NULL &&
+ !decode_modifiers((uint8_t *)arg_subject, CTX_DEFDAT, NULL, &def_datctl)))
+ {
+ yield = 1;
+ goto EXIT;
+ }
+
+/* Sort out the input and output files, defaulting to stdin/stdout. */
+
+infile = stdin;
+outfile = stdout;
+
+if (argc > 1 && strcmp(argv[op], "-") != 0)
+ {
+ infile = fopen(argv[op], INPUT_MODE);
+ if (infile == NULL)
+ {
+ printf("** Failed to open '%s': %s\n", argv[op], strerror(errno));
+ yield = 1;
+ goto EXIT;
+ }
+ }
+
+#if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT)
+if (INTERACTIVE(infile)) using_history();
+#endif
+
+if (argc > 2)
+ {
+ outfile = fopen(argv[op+1], OUTPUT_MODE);
+ if (outfile == NULL)
+ {
+ printf("** Failed to open '%s': %s\n", argv[op+1], strerror(errno));
+ yield = 1;
+ goto EXIT;
+ }
+ }
+
+/* Output a heading line unless quiet, then process input lines. */
+
+if (!quiet) print_version(outfile);
+
+SET(compiled_code, NULL);
+
+#ifdef SUPPORT_PCRE2_8
+preg.re_pcre2_code = NULL;
+preg.re_match_data = NULL;
+#endif
+
+while (notdone)
+ {
+ uint8_t *p;
+ int rc = PR_OK;
+ BOOL expectdata = TEST(compiled_code, !=, NULL);
+#ifdef SUPPORT_PCRE2_8
+ expectdata |= preg.re_pcre2_code != NULL;
+#endif
+
+ if (extend_inputline(infile, buffer, expectdata? "data> " : " re> ") == NULL)
+ break;
+ if (!INTERACTIVE(infile)) fprintf(outfile, "%s", (char *)buffer);
+ fflush(outfile);
+ p = buffer;
+
+ /* If we have a pattern set up for testing, or we are skipping after a
+ compile failure, a blank line terminates this test. */
+
+ if (expectdata || skipping)
+ {
+ while (isspace(*p)) p++;
+ if (*p == 0)
+ {
+#ifdef SUPPORT_PCRE2_8
+ if (preg.re_pcre2_code != NULL)
+ {
+ regfree(&preg);
+ preg.re_pcre2_code = NULL;
+ preg.re_match_data = NULL;
+ }
+#endif /* SUPPORT_PCRE2_8 */
+ if (TEST(compiled_code, !=, NULL))
+ {
+ SUB1(pcre2_code_free, compiled_code);
+ SET(compiled_code, NULL);
+ }
+ skipping = FALSE;
+ setlocale(LC_CTYPE, "C");
+ }
+
+ /* Otherwise, if we are not skipping, and the line is not a data comment
+ line starting with "\=", process a data line. */
+
+ else if (!skipping && !(p[0] == '\\' && p[1] == '=' && isspace(p[2])))
+ {
+ rc = process_data();
+ }
+ }
+
+ /* We do not have a pattern set up for testing. Lines starting with # are
+ either comments or special commands. Blank lines are ignored. Otherwise, the
+ line must start with a valid delimiter. It is then processed as a pattern
+ line. A copy of the pattern is left in pbuffer8 for use by callouts. Under
+ valgrind, make the unused part of the buffer undefined, to catch overruns. */
+
+ else if (*p == '#')
+ {
+ if (isspace(p[1]) || p[1] == '!' || p[1] == 0) continue;
+ rc = process_command();
+ }
+
+ else if (strchr("/!\"'`%&-=_:;,@~", *p) != NULL)
+ {
+ rc = process_pattern();
+ dfa_matched = 0;
+ }
+
+ else
+ {
+ while (isspace(*p)) p++;
+ if (*p != 0)
+ {
+ fprintf(outfile, "** Invalid pattern delimiter '%c' (x%x).\n", *buffer,
+ *buffer);
+ rc = PR_SKIP;
+ }
+ }
+
+ if (rc == PR_SKIP && !INTERACTIVE(infile)) skipping = TRUE;
+ else if (rc == PR_ABEND)
+ {
+ fprintf(outfile, "** pcre2test run abandoned\n");
+ yield = 1;
+ goto EXIT;
+ }
+ }
+
+/* Finish off a normal run. */
+
+if (INTERACTIVE(infile)) fprintf(outfile, "\n");
+
+if (showtotaltimes)
+ {
+ const char *pad = "";
+ fprintf(outfile, "--------------------------------------\n");
+ if (timeit > 0)
+ {
+ fprintf(outfile, "Total compile time %.4f milliseconds\n",
+ (((double)total_compile_time * 1000.0) / (double)timeit) /
+ (double)CLOCKS_PER_SEC);
+ if (total_jit_compile_time > 0)
+ fprintf(outfile, "Total JIT compile %.4f milliseconds\n",
+ (((double)total_jit_compile_time * 1000.0) / (double)timeit) /
+ (double)CLOCKS_PER_SEC);
+ pad = " ";
+ }
+ fprintf(outfile, "Total match time %s%.4f milliseconds\n", pad,
+ (((double)total_match_time * 1000.0) / (double)timeitm) /
+ (double)CLOCKS_PER_SEC);
+ }
+
+
+EXIT:
+
+#if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT)
+if (infile != NULL && INTERACTIVE(infile)) clear_history();
+#endif
+
+if (infile != NULL && infile != stdin) fclose(infile);
+if (outfile != NULL && outfile != stdout) fclose(outfile);
+
+free(buffer);
+free(dbuffer);
+free(pbuffer8);
+free(dfa_workspace);
+free((void *)locale_tables);
+PCRE2_MATCH_DATA_FREE(match_data);
+SUB1(pcre2_code_free, compiled_code);
+
+while(patstacknext-- > 0)
+ {
+ SET(compiled_code, patstack[patstacknext]);
+ SUB1(pcre2_code_free, compiled_code);
+ }
+
+PCRE2_JIT_FREE_UNUSED_MEMORY(general_context);
+if (jit_stack != NULL)
+ {
+ PCRE2_JIT_STACK_FREE(jit_stack);
+ }
+
+#define FREECONTEXTS \
+ G(pcre2_general_context_free_,BITS)(G(general_context,BITS)); \
+ G(pcre2_general_context_free_,BITS)(G(general_context_copy,BITS)); \
+ G(pcre2_compile_context_free_,BITS)(G(pat_context,BITS)); \
+ G(pcre2_compile_context_free_,BITS)(G(default_pat_context,BITS)); \
+ G(pcre2_match_context_free_,BITS)(G(dat_context,BITS)); \
+ G(pcre2_match_context_free_,BITS)(G(default_dat_context,BITS)); \
+ G(pcre2_convert_context_free_,BITS)(G(default_con_context,BITS)); \
+ G(pcre2_convert_context_free_,BITS)(G(con_context,BITS));
+
+#ifdef SUPPORT_PCRE2_8
+#undef BITS
+#define BITS 8
+if (preg.re_pcre2_code != NULL) regfree(&preg);
+FREECONTEXTS;
+#endif
+
+#ifdef SUPPORT_PCRE2_16
+#undef BITS
+#define BITS 16
+free(pbuffer16);
+FREECONTEXTS;
+#endif
+
+#ifdef SUPPORT_PCRE2_32
+#undef BITS
+#define BITS 32
+free(pbuffer32);
+FREECONTEXTS;
+#endif
+
+#if defined(__VMS)
+ yield = SS$_NORMAL; /* Return values via DCL symbols */
+#endif
+
+ clock_stop();
+ printerr_total_clock();
+
+return yield;
+}
+
+/* End of pcre2test.c */
diff --git a/test/monniaux/pcre2-10.32/testdata/testinput6 b/test/monniaux/pcre2-10.32/testdata/testinput6
new file mode 100644
index 00000000..c1a08348
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/testdata/testinput6
@@ -0,0 +1,4958 @@
+# This set of tests check the DFA matching functionality of pcre2_dfa_match(),
+# excluding UTF and Unicode property support. All matches are done using DFA,
+# forced by setting a default subject modifier at the start.
+
+#forbid_utf
+#subject dfa
+#newline_default lf anycrlf any
+
+/abc/
+ abc
+
+/ab*c/
+ abc
+ abbbbc
+ ac
+
+/ab+c/
+ abc
+ abbbbbbc
+\= Expect no match
+ ac
+ ab
+
+/a*/no_auto_possess
+ a
+ aaaaaaaaaaaaaaaaa
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\=ovector=10
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\=dfa_shortest
+
+/(a|abcd|african)/
+ a
+ abcd
+ african
+
+/^abc/
+ abcdef
+\= Expect no match
+ xyzabc
+ xyz\nabc
+
+/^abc/m
+ abcdef
+ xyz\nabc
+\= Expect no match
+ xyzabc
+
+/\Aabc/
+ abcdef
+\= Expect no match
+ xyzabc
+ xyz\nabc
+
+/\Aabc/m
+ abcdef
+\= Expect no match
+ xyzabc
+ xyz\nabc
+
+/\Gabc/
+ abcdef
+ xyzabc\=offset=3
+\= Expect no match
+ xyzabc
+ xyzabc\=offset=2
+
+/x\dy\Dz/
+ x9yzz
+ x0y+z
+\= Expect no match
+ xyz
+ xxy0z
+
+/x\sy\Sz/
+ x yzz
+ x y+z
+\= Expect no match
+ xyz
+ xxyyz
+
+/x\wy\Wz/
+ xxy+z
+\= Expect no match
+ xxy0z
+ x+y+z
+
+/x.y/
+ x+y
+ x-y
+\= Expect no match
+ x\ny
+
+/x.y/s
+ x+y
+ x-y
+ x\ny
+
+/(a.b(?s)c.d|x.y)p.q/
+ a+bc+dp+q
+ a+bc\ndp+q
+ x\nyp+q
+\= Expect no match
+ a\nbc\ndp+q
+ a+bc\ndp\nq
+ x\nyp\nq
+
+/a\d\z/
+ ba0
+\= Expect no match
+ ba0\n
+ ba0\ncd
+
+/a\d\z/m
+ ba0
+\= Expect no match
+ ba0\n
+ ba0\ncd
+
+/a\d\Z/
+ ba0
+ ba0\n
+\= Expect no match
+ ba0\ncd
+
+/a\d\Z/m
+ ba0
+ ba0\n
+\= Expect no match
+ ba0\ncd
+
+/a\d$/
+ ba0
+ ba0\n
+\= Expect no match
+ ba0\ncd
+
+/a\d$/m
+ ba0
+ ba0\n
+ ba0\ncd
+
+/abc/i
+ abc
+ aBc
+ ABC
+
+/[^a]/
+ abcd
+
+/ab?\w/
+ abz
+ abbz
+ azz
+
+/x{0,3}yz/
+ ayzq
+ axyzq
+ axxyz
+ axxxyzq
+ axxxxyzq
+\= Expect no match
+ ax
+ axx
+
+/x{3}yz/
+ axxxyzq
+ axxxxyzq
+\= Expect no match
+ ax
+ axx
+ ayzq
+ axyzq
+ axxyz
+
+/x{2,3}yz/
+ axxyz
+ axxxyzq
+ axxxxyzq
+\= Expect no match
+ ax
+ axx
+ ayzq
+ axyzq
+
+/[^a]+/no_auto_possess
+ bac
+ bcdefax
+\= Expect no match
+ aaaaa
+
+/[^a]*/no_auto_possess
+ bac
+ bcdefax
+ aaaaa
+
+/[^a]{3,5}/no_auto_possess
+ xyz
+ awxyza
+ abcdefa
+ abcdefghijk
+\= Expect no match
+ axya
+ axa
+ aaaaa
+
+/\d*/
+ 1234b567
+ xyz
+
+/\D*/
+ a1234b567
+ xyz
+
+/\d+/
+ ab1234c56
+\= Expect no match
+ xyz
+
+/\D+/
+ ab123c56
+\= Expect no match
+ 789
+
+/\d?A/
+ 045ABC
+ ABC
+\= Expect no match
+ XYZ
+
+/\D?A/
+ ABC
+ BAC
+ 9ABC
+
+/a+/
+ aaaa
+
+/^.*xyz/
+ xyz
+ ggggggggxyz
+
+/^.+xyz/
+ abcdxyz
+ axyz
+\= Expect no match
+ xyz
+
+/^.?xyz/
+ xyz
+ cxyz
+
+/^\d{2,3}X/
+ 12X
+ 123X
+\= Expect no match
+ X
+ 1X
+ 1234X
+
+/^[abcd]\d/
+ a45
+ b93
+ c99z
+ d04
+\= Expect no match
+ e45
+ abcd
+ abcd1234
+ 1234
+
+/^[abcd]*\d/
+ a45
+ b93
+ c99z
+ d04
+ abcd1234
+ 1234
+\= Expect no match
+ e45
+ abcd
+
+/^[abcd]+\d/
+ a45
+ b93
+ c99z
+ d04
+ abcd1234
+\= Expect no match
+ 1234
+ e45
+ abcd
+
+/^a+X/
+ aX
+ aaX
+
+/^[abcd]?\d/
+ a45
+ b93
+ c99z
+ d04
+ 1234
+\= Expect no match
+ abcd1234
+ e45
+
+/^[abcd]{2,3}\d/
+ ab45
+ bcd93
+\= Expect no match
+ 1234
+ a36
+ abcd1234
+ ee45
+
+/^(abc)*\d/
+ abc45
+ abcabcabc45
+ 42xyz
+
+/^(abc)+\d/
+ abc45
+ abcabcabc45
+\= Expect no match
+ 42xyz
+
+/^(abc)?\d/
+ abc45
+ 42xyz
+\= Expect no match
+ abcabcabc45
+
+/^(abc){2,3}\d/
+ abcabc45
+ abcabcabc45
+\= Expect no match
+ abcabcabcabc45
+ abc45
+ 42xyz
+
+/1(abc|xyz)2(?1)3/
+ 1abc2abc3456
+ 1abc2xyz3456
+
+/^(a*\w|ab)=(a*\w|ab)/
+ ab=ab
+
+/^(a*\w|ab)=(?1)/
+ ab=ab
+
+/^([^()]|\((?1)*\))*$/
+ abc
+ a(b)c
+ a(b(c))d
+\= Expect no match)
+ a(b(c)d
+
+/^>abc>([^()]|\((?1)*\))*<xyz<$/
+ >abc>123<xyz<
+ >abc>1(2)3<xyz<
+ >abc>(1(2)3)<xyz<
+
+/^(?>a*)\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9876
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/< (?: (?(R) \d++ | [^<>]*+) | (?R)) * >/x
+ <>
+ <abcd>
+ <abc <123> hij>
+ <abc <def> hij>
+ <abc<>def>
+ <abc<>
+\= Expect no match
+ <abc
+
+/^(?(?=abc)\w{3}:|\d\d)$/
+ abc:
+ 12
+\= Expect no match
+ 123
+ xyz
+
+/^(?(?!abc)\d\d|\w{3}:)$/
+ abc:
+ 12
+\= Expect no match
+ 123
+ xyz
+
+/^(?=abc)\w{5}:$/
+ abcde:
+\= Expect no match
+ abc..
+ 123
+ vwxyz
+
+/^(?!abc)\d\d$/
+ 12
+\= Expect no match
+ abcde:
+ abc..
+ 123
+ vwxyz
+
+/(?<=abc|xy)123/
+ abc12345
+ wxy123z
+\= Expect no match
+ 123abc
+
+/(?<!abc|xy)123/
+ 123abc
+ mno123456
+\= Expect no match
+ abc12345
+ wxy123z
+
+/abc(?C1)xyz/
+ abcxyz
+ 123abcxyz999
+
+/(ab|cd){3,4}/auto_callout
+ ababab
+ abcdabcd
+ abcdcdcdcdcd
+
+/^abc/
+ abcdef
+\= Expect no match
+ abcdef\=notbol
+
+/^(a*|xyz)/
+ bcd
+ aaabcd
+ xyz
+ xyz\=notempty
+\= Expect no match
+ bcd\=notempty
+
+/xyz$/
+ xyz
+ xyz\n
+\= Expect no match
+ xyz\=noteol
+ xyz\n\=noteol
+
+/xyz$/m
+ xyz
+ xyz\n
+ abcxyz\npqr
+ abcxyz\npqr\=noteol
+ xyz\n\=noteol
+\= Expect no match
+ xyz\=noteol
+
+/\Gabc/
+ abcdef
+ defabcxyz\=offset=3
+\= Expect no match
+ defabcxyz
+
+/^abcdef/
+ ab\=ps
+ abcde\=ps
+ abcdef\=ps
+\= Expect no match
+ abx\=ps
+
+/^a{2,4}\d+z/
+ a\=ps
+ aa\=ps
+ aa2\=ps
+ aaa\=ps
+ aaa23\=ps
+ aaaa12345\=ps
+ aa0z\=ps
+ aaaa4444444444444z\=ps
+\= Expect no match
+ az\=ps
+ aaaaa\=ps
+ a56\=ps
+
+/^abcdef/
+ abc\=ps
+ def\=dfa_restart
+
+/(?<=foo)bar/
+ foob\=ps,offset=2
+ foobar...\=ps,dfa_restart,offset=4
+ foobar\=offset=2
+\= Expect no match
+ xyzfo\=ps
+ obar\=dfa_restart
+
+/(ab*(cd|ef))+X/
+ lkjhlkjhlkjhlkjhabbbbbbcdaefabbbbbbbefa\=ps,notbol,noteol
+ cdabbbbbbbb\=ps,notbol,dfa_restart,noteol
+ efabbbbbbbbbbbbbbbb\=ps,notbol,dfa_restart,noteol
+ bbbbbbbbbbbbcdXyasdfadf\=ps,notbol,dfa_restart,noteol
+\= Expect no match
+ adfadadaklhlkalkajhlkjahdfasdfasdfladsfjkj\=ps,noteol
+
+/the quick brown fox/
+ the quick brown fox
+ What do you know about the quick brown fox?
+\= Expect no match
+ The quick brown FOX
+ What do you know about THE QUICK BROWN FOX?
+
+/The quick brown fox/i
+ the quick brown fox
+ The quick brown FOX
+ What do you know about the quick brown fox?
+ What do you know about THE QUICK BROWN FOX?
+
+/abcd\t\n\r\f\a\e\071\x3b\$\\\?caxyz/
+ abcd\t\n\r\f\a\e9;\$\\?caxyz
+
+/a*abc?xyz+pqr{3}ab{2,}xy{4,5}pq{0,6}AB{0,}zz/
+ abxyzpqrrrabbxyyyypqAzz
+ abxyzpqrrrabbxyyyypqAzz
+ aabxyzpqrrrabbxyyyypqAzz
+ aaabxyzpqrrrabbxyyyypqAzz
+ aaaabxyzpqrrrabbxyyyypqAzz
+ abcxyzpqrrrabbxyyyypqAzz
+ aabcxyzpqrrrabbxyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypAzz
+ aaabcxyzpqrrrabbxyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqqqAzz
+ aaaabcxyzpqrrrabbxyyyypqAzz
+ abxyzzpqrrrabbxyyyypqAzz
+ aabxyzzzpqrrrabbxyyyypqAzz
+ aaabxyzzzzpqrrrabbxyyyypqAzz
+ aaaabxyzzzzpqrrrabbxyyyypqAzz
+ abcxyzzpqrrrabbxyyyypqAzz
+ aabcxyzzzpqrrrabbxyyyypqAzz
+ aaabcxyzzzzpqrrrabbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypABzz
+ aaabcxyzpqrrrabbxyyyypABBzz
+ >>>aaabxyzpqrrrabbxyyyypqAzz
+ >aaaabxyzpqrrrabbxyyyypqAzz
+ >>>>abcxyzpqrrrabbxyyyypqAzz
+\= Expect no match
+ abxyzpqrrabbxyyyypqAzz
+ abxyzpqrrrrabbxyyyypqAzz
+ abxyzpqrrrabxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyypqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqqqqAzz
+
+/^(abc){1,2}zz/
+ abczz
+ abcabczz
+\= Expect no match
+ zz
+ abcabcabczz
+ >>abczz
+
+/^(b+?|a){1,2}?c/
+ bc
+ bbc
+ bbbc
+ bac
+ bbac
+ aac
+ abbbbbbbbbbbc
+ bbbbbbbbbbbac
+\= Expect no match
+ aaac
+ abbbbbbbbbbbac
+
+/^(b+|a){1,2}c/
+ bc
+ bbc
+ bbbc
+ bac
+ bbac
+ aac
+ abbbbbbbbbbbc
+ bbbbbbbbbbbac
+\= Expect no match
+ aaac
+ abbbbbbbbbbbac
+
+/^(b+|a){1,2}?bc/
+ bbc
+
+/^(b*|ba){1,2}?bc/
+ babc
+ bbabc
+ bababc
+\= Expect no match
+ bababbc
+ babababc
+
+/^(ba|b*){1,2}?bc/
+ babc
+ bbabc
+ bababc
+\= Expect no match
+ bababbc
+ babababc
+
+/^\ca\cA\c[\c{\c:/
+ \x01\x01\e;z
+
+/^[ab\]cde]/
+ athing
+ bthing
+ ]thing
+ cthing
+ dthing
+ ething
+\= Expect no match
+ fthing
+ [thing
+ \\thing
+
+/^[]cde]/
+ ]thing
+ cthing
+ dthing
+ ething
+\= Expect no match
+ athing
+ fthing
+
+/^[^ab\]cde]/
+ fthing
+ [thing
+ \\thing
+\= Expect no match
+ athing
+ bthing
+ ]thing
+ cthing
+ dthing
+ ething
+
+/^[^]cde]/
+ athing
+ fthing
+\= Expect no match
+ ]thing
+ cthing
+ dthing
+ ething
+
+/^\/
+
+
+/^ÿ/
+ ÿ
+
+/^[0-9]+$/
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 100
+\= Expect no match
+ abc
+
+/^.*nter/
+ enter
+ inter
+ uponter
+
+/^xxx[0-9]+$/
+ xxx0
+ xxx1234
+\= Expect no match
+ xxx
+
+/^.+[0-9][0-9][0-9]$/
+ x123
+ xx123
+ 123456
+ x1234
+\= Expect no match
+ 123
+
+/^.+?[0-9][0-9][0-9]$/
+ x123
+ xx123
+ 123456
+ x1234
+\= Expect no match
+ 123
+
+/^([^!]+)!(.+)=apquxz\.ixr\.zzz\.ac\.uk$/
+ abc!pqr=apquxz.ixr.zzz.ac.uk
+\= Expect no match
+ !pqr=apquxz.ixr.zzz.ac.uk
+ abc!=apquxz.ixr.zzz.ac.uk
+ abc!pqr=apquxz:ixr.zzz.ac.uk
+ abc!pqr=apquxz.ixr.zzz.ac.ukk
+
+/:/
+ Well, we need a colon: somewhere
+\= Expect no match
+ No match without a colon
+
+/([\da-f:]+)$/i
+ 0abc
+ abc
+ fed
+ E
+ ::
+ 5f03:12C0::932e
+ fed def
+ Any old stuff
+\= Expect no match
+ 0zzz
+ gzzz
+ fed\x20
+ Any old rubbish
+
+/^.*\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
+ .1.2.3
+ A.12.123.0
+\= Expect no match
+ .1.2.3333
+ 1.2.3
+ 1234.2.3
+
+/^(\d+)\s+IN\s+SOA\s+(\S+)\s+(\S+)\s*\(\s*$/
+ 1 IN SOA non-sp1 non-sp2(
+ 1 IN SOA non-sp1 non-sp2 (
+\= Expect no match
+ 1IN SOA non-sp1 non-sp2(
+
+/^[a-zA-Z\d][a-zA-Z\d\-]*(\.[a-zA-Z\d][a-zA-z\d\-]*)*\.$/
+ a.
+ Z.
+ 2.
+ ab-c.pq-r.
+ sxk.zzz.ac.uk.
+ x-.y-.
+\= Expect no match
+ -abc.peq.
+
+/^\*\.[a-z]([a-z\-\d]*[a-z\d]+)?(\.[a-z]([a-z\-\d]*[a-z\d]+)?)*$/
+ *.a
+ *.b0-a
+ *.c3-b.c
+ *.c-a.b-c
+\= Expect no match
+ *.0
+ *.a-
+ *.a-b.c-
+ *.c-a.0-c
+
+/^(?=ab(de))(abd)(e)/
+ abde
+
+/^(?!(ab)de|x)(abd)(f)/
+ abdf
+
+/^(?=(ab(cd)))(ab)/
+ abcd
+
+/^[\da-f](\.[\da-f])*$/i
+ a.b.c.d
+ A.B.C.D
+ a.b.c.1.2.3.C
+
+/^\".*\"\s*(;.*)?$/
+ \"1234\"
+ \"abcd\" ;
+ \"\" ; rhubarb
+\= Expect no match
+ \"1234\" : things
+
+/^$/
+ \
+
+/ ^ a (?# begins with a) b\sc (?# then b c) $ (?# then end)/x
+ ab c
+\= Expect no match
+ abc
+ ab cde
+
+/(?x) ^ a (?# begins with a) b\sc (?# then b c) $ (?# then end)/
+ ab c
+\= Expect no match
+ abc
+ ab cde
+
+/^ a\ b[c ]d $/x
+ a bcd
+ a b d
+\= Expect no match
+ abcd
+ ab d
+
+/^(a(b(c)))(d(e(f)))(h(i(j)))(k(l(m)))$/
+ abcdefhijklm
+
+/^(?:a(b(c)))(?:d(e(f)))(?:h(i(j)))(?:k(l(m)))$/
+ abcdefhijklm
+
+/^[\w][\W][\s][\S][\d][\D][\b][\n][\c]][\022]/
+ a+ Z0+\x08\n\x1d\x12
+
+/^[.^$|()*+?{,}]+/
+ .^\$(*+)|{?,?}
+
+/^a*\w/
+ z
+ az
+ aaaz
+ a
+ aa
+ aaaa
+ a+
+ aa+
+
+/^a*?\w/
+ z
+ az
+ aaaz
+ a
+ aa
+ aaaa
+ a+
+ aa+
+
+/^a+\w/
+ az
+ aaaz
+ aa
+ aaaa
+ aa+
+
+/^a+?\w/
+ az
+ aaaz
+ aa
+ aaaa
+ aa+
+
+/^\d{8}\w{2,}/
+ 1234567890
+ 12345678ab
+ 12345678__
+\= Expect no match
+ 1234567
+
+/^[aeiou\d]{4,5}$/
+ uoie
+ 1234
+ 12345
+ aaaaa
+\= Expect no match
+ 123456
+
+/^[aeiou\d]{4,5}?/
+ uoie
+ 1234
+ 12345
+ aaaaa
+ 123456
+
+/^From +([^ ]+) +[a-zA-Z][a-zA-Z][a-zA-Z] +[a-zA-Z][a-zA-Z][a-zA-Z] +[0-9]?[0-9] +[0-9][0-9]:[0-9][0-9]/
+ From abcd Mon Sep 01 12:33:02 1997
+
+/^From\s+\S+\s+([a-zA-Z]{3}\s+){2}\d{1,2}\s+\d\d:\d\d/
+ From abcd Mon Sep 01 12:33:02 1997
+ From abcd Mon Sep 1 12:33:02 1997
+\= Expect no match
+ From abcd Sep 01 12:33:02 1997
+
+/^12.34/s
+ 12\n34
+ 12\r34
+
+/\w+(?=\t)/
+ the quick brown\t fox
+
+/foo(?!bar)(.*)/
+ foobar is foolish see?
+
+/(?:(?!foo)...|^.{0,2})bar(.*)/
+ foobar crowbar etc
+ barrel
+ 2barrel
+ A barrel
+
+/^(\D*)(?=\d)(?!123)/
+ abc456
+\= Expect no match
+ abc123
+
+/^1234(?# test newlines
+ inside)/
+ 1234
+
+/^1234 #comment in extended re
+ /x
+ 1234
+
+/#rhubarb
+ abcd/x
+ abcd
+
+/^abcd#rhubarb/x
+ abcd
+
+/(?!^)abc/
+ the abc
+\= Expect no match
+ abc
+
+/(?=^)abc/
+ abc
+\= Expect no match
+ the abc
+
+/^[ab]{1,3}(ab*|b)/no_auto_possess
+ aabbbbb
+
+/^[ab]{1,3}?(ab*|b)/no_auto_possess
+ aabbbbb
+
+/^[ab]{1,3}?(ab*?|b)/no_auto_possess
+ aabbbbb
+
+/^[ab]{1,3}(ab*?|b)/no_auto_possess
+ aabbbbb
+
+/ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # optional leading comment
+(?: (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # initial word
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) )* # further okay, if led by a period
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+# address
+| # or
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # one word, optionally followed by....
+(?:
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] | # atom and space parts, or...
+\(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) | # comments, or...
+
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+# quoted strings
+)*
+< (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # leading <
+(?: @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* , (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+)* # further okay, if led by comma
+: # closing colon
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* )? # optional route
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # initial word
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) )* # further okay, if led by a period
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+# address spec
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* > # trailing >
+# name and address
+) (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # optional trailing comment
+/x
+ Alan Other <user\@dom.ain>
+ <user\@dom.ain>
+ user\@dom.ain
+ \"A. Other\" <user.1234\@dom.ain> (a comment)
+ A. Other <user.1234\@dom.ain> (a comment)
+ \"/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/\"\@x400-re.lay
+ A missing angle <user\@some.where
+\= Expect no match
+ The quick brown fox
+
+/[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional leading comment
+(?:
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# additional words
+)*
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+# address
+| # or
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+# leading word
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] * # "normal" atoms and or spaces
+(?:
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+|
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+) # "special" comment or quoted string
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] * # more "normal"
+)*
+<
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# <
+(?:
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+(?: ,
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+)* # additional domains
+:
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)? # optional route
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# additional words
+)*
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+# address spec
+> # >
+# name and address
+)
+/x
+ Alan Other <user\@dom.ain>
+ <user\@dom.ain>
+ user\@dom.ain
+ \"A. Other\" <user.1234\@dom.ain> (a comment)
+ A. Other <user.1234\@dom.ain> (a comment)
+ \"/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/\"\@x400-re.lay
+ A missing angle <user\@some.where
+\= Expect no match
+ The quick brown fox
+
+/abc\0def\00pqr\000xyz\0000AB/
+ abc\0def\00pqr\000xyz\0000AB
+ abc456 abc\0def\00pqr\000xyz\0000ABCDE
+
+/abc\x0def\x00pqr\x000xyz\x0000AB/
+ abc\x0def\x00pqr\x000xyz\x0000AB
+ abc456 abc\x0def\x00pqr\x000xyz\x0000ABCDE
+
+/^[\000-\037]/
+ \0A
+ \01B
+ \037C
+
+/\0*/
+ \0\0\0\0
+
+/A\x0{2,3}Z/
+ The A\x0\x0Z
+ An A\0\x0\0Z
+\= Expect no match
+ A\0Z
+ A\0\x0\0\x0Z
+
+/^\s/
+ \040abc
+ \x0cabc
+ \nabc
+ \rabc
+ \tabc
+\= Expect no match
+ abc
+
+/^a b
+ c/x
+ abc
+
+/ab{1,3}bc/
+ abbbbc
+ abbbc
+ abbc
+\= Expect no match
+ abc
+ abbbbbc
+
+/([^.]*)\.([^:]*):[T ]+(.*)/
+ track1.title:TBlah blah blah
+
+/([^.]*)\.([^:]*):[T ]+(.*)/i
+ track1.title:TBlah blah blah
+
+/([^.]*)\.([^:]*):[t ]+(.*)/i
+ track1.title:TBlah blah blah
+
+/^[W-c]+$/
+ WXY_^abc
+\= Expect no match
+ wxy
+
+/^[W-c]+$/i
+ WXY_^abc
+ wxy_^ABC
+
+/^[\x3f-\x5F]+$/i
+ WXY_^abc
+ wxy_^ABC
+
+/^abc$/m
+ abc
+ qqq\nabc
+ abc\nzzz
+ qqq\nabc\nzzz
+
+/^abc$/
+ abc
+\= Expect no match
+ qqq\nabc
+ abc\nzzz
+ qqq\nabc\nzzz
+
+/\Aabc\Z/m
+ abc
+ abc\n
+\= Expect no match
+ qqq\nabc
+ abc\nzzz
+ qqq\nabc\nzzz
+
+/\A(.)*\Z/s
+ abc\ndef
+
+/\A(.)*\Z/m
+\= Expect no match
+ abc\ndef
+
+/(?:b)|(?::+)/
+ b::c
+ c::b
+
+/[-az]+/
+ az-
+\= Expect no match
+ b
+
+/[az-]+/
+ za-
+\= Expect no match
+ b
+
+/[a\-z]+/
+ a-z
+\= Expect no match
+ b
+
+/[a-z]+/
+ abcdxyz
+
+/[\d-]+/
+ 12-34
+\= Expect no match
+ aaa
+
+/\x5c/
+ \\
+
+/\x20Z/
+ the Zoo
+\= Expect no match
+ Zulu
+
+/ab{3cd/
+ ab{3cd
+
+/ab{3,cd/
+ ab{3,cd
+
+/ab{3,4a}cd/
+ ab{3,4a}cd
+
+/{4,5a}bc/
+ {4,5a}bc
+
+/^a.b/newline=lf
+ a\rb
+\= Expect no match
+ a\nb
+
+/abc$/
+ abc
+ abc\n
+\= Expect no match
+ abc\ndef
+
+/(abc)\123/
+ abc\x53
+
+/(abc)\223/
+ abc\x93
+
+/(abc)\323/
+ abc\xd3
+
+/(abc)\100/
+ abc\x40
+ abc\100
+
+/(abc)\1000/
+ abc\x400
+ abc\x40\x30
+ abc\1000
+ abc\100\x30
+ abc\100\060
+ abc\100\60
+
+/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)\12\123/
+ abcdefghijk\12S
+
+/a{0}bc/
+ bc
+
+/(a|(bc)){0,0}?xyz/
+ xyz
+
+/abc[\10]de/
+ abc\010de
+
+/abc[\1]de/
+ abc\1de
+
+/(abc)[\1]de/
+ abc\1de
+
+/(?s)a.b/
+ a\nb
+
+/^([^a])([^\b])([^c]*)([^d]{3,4})/
+ baNOTccccd
+ baNOTcccd
+ baNOTccd
+ bacccd
+\= Expect no match
+ anything
+ b\bc
+ baccd
+
+/[^a]/
+ Abc
+
+/[^a]/i
+ Abc
+
+/[^a]+/
+ AAAaAbc
+
+/[^a]+/i
+ AAAaAbc
+
+/[^a]+/
+ bbb\nccc
+
+/[^k]$/
+ abc
+\= Expect no match
+ abk
+
+/[^k]{2,3}$/
+ abc
+ kbc
+ kabc
+\= Expect no match
+ abk
+ akb
+ akk
+
+/^\d{8,}\@.+[^k]$/
+ 12345678\@a.b.c.d
+ 123456789\@x.y.z
+\= Expect no match
+ 12345678\@x.y.uk
+ 1234567\@a.b.c.d
+
+/[^a]/
+ aaaabcd
+ aaAabcd
+
+/[^a]/i
+ aaaabcd
+ aaAabcd
+
+/[^az]/
+ aaaabcd
+ aaAabcd
+
+/[^az]/i
+ aaaabcd
+ aaAabcd
+
+/\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377/
+ \000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377
+
+/P[^*]TAIRE[^*]{1,6}?LL/
+ xxxxxxxxxxxPSTAIREISLLxxxxxxxxx
+
+/P[^*]TAIRE[^*]{1,}?LL/
+ xxxxxxxxxxxPSTAIREISLLxxxxxxxxx
+
+/(\.\d\d[1-9]?)\d+/
+ 1.230003938
+ 1.875000282
+ 1.235
+
+/(\.\d\d((?=0)|\d(?=\d)))/
+ 1.230003938
+ 1.875000282
+\= Expect no match
+ 1.235
+
+/a(?)b/
+ ab
+
+/\b(foo)\s+(\w+)/i
+ Food is on the foo table
+
+/foo(.*)bar/
+ The food is under the bar in the barn.
+
+/foo(.*?)bar/
+ The food is under the bar in the barn.
+
+/(.*)(\d*)/no_auto_possess
+ I have 2 numbers: 53147
+
+/(.*)(\d+)/
+ I have 2 numbers: 53147
+
+/(.*?)(\d*)/no_auto_possess
+ I have 2 numbers: 53147
+
+/(.*?)(\d+)/
+ I have 2 numbers: 53147
+
+/(.*)(\d+)$/
+ I have 2 numbers: 53147
+
+/(.*?)(\d+)$/
+ I have 2 numbers: 53147
+
+/(.*)\b(\d+)$/
+ I have 2 numbers: 53147
+
+/(.*\D)(\d+)$/
+ I have 2 numbers: 53147
+
+/^\D*(?!123)/
+ ABC123
+
+/^(\D*)(?=\d)(?!123)/
+ ABC445
+\= Expect no match
+ ABC123
+
+/^[W-]46]/
+ W46]789
+ -46]789
+\= Expect no match
+ Wall
+ Zebra
+ 42
+ [abcd]
+ ]abcd[
+
+/^[W-\]46]/
+ W46]789
+ Wall
+ Zebra
+ Xylophone
+ 42
+ [abcd]
+ ]abcd[
+ \\backslash
+\= Expect no match
+ -46]789
+ well
+
+/\d\d\/\d\d\/\d\d\d\d/
+ 01/01/2000
+
+/word (?:[a-zA-Z0-9]+ ){0,10}otherword/
+ word cat dog elephant mussel cow horse canary baboon snake shark otherword
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark
+
+/word (?:[a-zA-Z0-9]+ ){0,300}otherword/
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark the quick brown fox and the lazy dog and several other words getting close to thirty by now I hope
+
+/^(a){0,0}/
+ bcd
+ abc
+ aab
+
+/^(a){0,1}/
+ bcd
+ abc
+ aab
+
+/^(a){0,2}/
+ bcd
+ abc
+ aab
+
+/^(a){0,3}/
+ bcd
+ abc
+ aab
+ aaa
+
+/^(a){0,}/
+ bcd
+ abc
+ aab
+ aaa
+ aaaaaaaa
+
+/^(a){1,1}/
+ abc
+ aab
+\= Expect no match
+ bcd
+
+/^(a){1,2}/
+ abc
+ aab
+\= Expect no match
+ bcd
+
+/^(a){1,3}/
+ abc
+ aab
+ aaa
+\= Expect no match
+ bcd
+
+/^(a){1,}/
+ abc
+ aab
+ aaa
+ aaaaaaaa
+\= Expect no match
+ bcd
+
+/.*\.gif/
+ borfle\nbib.gif\nno
+
+/.{0,}\.gif/
+ borfle\nbib.gif\nno
+
+/.*\.gif/m
+ borfle\nbib.gif\nno
+
+/.*\.gif/s
+ borfle\nbib.gif\nno
+
+/.*\.gif/ms
+ borfle\nbib.gif\nno
+
+/.*$/
+ borfle\nbib.gif\nno
+
+/.*$/m
+ borfle\nbib.gif\nno
+
+/.*$/s
+ borfle\nbib.gif\nno
+
+/.*$/ms
+ borfle\nbib.gif\nno
+
+/.*$/
+ borfle\nbib.gif\nno\n
+
+/.*$/m
+ borfle\nbib.gif\nno\n
+
+/.*$/s
+ borfle\nbib.gif\nno\n
+
+/.*$/ms
+ borfle\nbib.gif\nno\n
+
+/(.*X|^B)/
+ abcde\n1234Xyz
+ BarFoo
+\= Expect no match
+ abcde\nBar
+
+/(.*X|^B)/m
+ abcde\n1234Xyz
+ BarFoo
+ abcde\nBar
+
+/(.*X|^B)/s
+ abcde\n1234Xyz
+ BarFoo
+\= Expect no match
+ abcde\nBar
+
+/(.*X|^B)/ms
+ abcde\n1234Xyz
+ BarFoo
+ abcde\nBar
+
+/(?s)(.*X|^B)/
+ abcde\n1234Xyz
+ BarFoo
+\= Expect no match
+ abcde\nBar
+
+/(?s:.*X|^B)/
+ abcde\n1234Xyz
+ BarFoo
+\= Expect no match
+ abcde\nBar
+
+/^.*B/
+\= Expect no match
+ abc\nB
+
+/(?s)^.*B/
+ abc\nB
+
+/(?m)^.*B/
+ abc\nB
+
+/(?ms)^.*B/
+ abc\nB
+
+/(?ms)^B/
+ abc\nB
+
+/(?s)B$/
+ B\n
+
+/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
+ 123456654321
+
+/^\d\d\d\d\d\d\d\d\d\d\d\d/
+ 123456654321
+
+/^[\d][\d][\d][\d][\d][\d][\d][\d][\d][\d][\d][\d]/
+ 123456654321
+
+/^[abc]{12}/
+ abcabcabcabc
+
+/^[a-c]{12}/
+ abcabcabcabc
+
+/^(a|b|c){12}/
+ abcabcabcabc
+
+/^[abcdefghijklmnopqrstuvwxy0123456789]/
+ n
+\= Expect no match
+ z
+
+/abcde{0,0}/
+ abcd
+\= Expect no match
+ abce
+
+/ab[cd]{0,0}e/
+ abe
+\= Expect no match
+ abcde
+
+/ab(c){0,0}d/
+ abd
+\= Expect no match
+ abcd
+
+/a(b*)/
+ a
+ ab
+ abbbb
+\= Expect no match
+ bbbbb
+
+/ab\d{0}e/
+ abe
+\= Expect no match
+ ab1e
+
+/"([^\\"]+|\\.)*"/
+ the \"quick\" brown fox
+ \"the \\\"quick\\\" brown fox\"
+
+/.*?/g,aftertext
+ abc
+
+/\b/g,aftertext
+ abc
+
+/\b/g,aftertext
+ abc
+
+//g
+ abc
+
+/<tr([\w\W\s\d][^<>]{0,})><TD([\w\W\s\d][^<>]{0,})>([\d]{0,}\.)(.*)((<BR>([\w\W\s\d][^<>]{0,})|[\s]{0,}))<\/a><\/TD><TD([\w\W\s\d][^<>]{0,})>([\w\W\s\d][^<>]{0,})<\/TD><TD([\w\W\s\d][^<>]{0,})>([\w\W\s\d][^<>]{0,})<\/TD><\/TR>/is
+ <TR BGCOLOR='#DBE9E9'><TD align=left valign=top>43.<a href='joblist.cfm?JobID=94 6735&Keyword='>Word Processor<BR>(N-1286)</a></TD><TD align=left valign=top>Lega lstaff.com</TD><TD align=left valign=top>CA - Statewide</TD></TR>
+
+/a[^a]b/
+ acb
+ a\nb
+
+/a.b/
+ acb
+\= Expect no match
+ a\nb
+
+/a[^a]b/s
+ acb
+ a\nb
+
+/a.b/s
+ acb
+ a\nb
+
+/^(b+?|a){1,2}?c/
+ bac
+ bbac
+ bbbac
+ bbbbac
+ bbbbbac
+
+/^(b+|a){1,2}?c/
+ bac
+ bbac
+ bbbac
+ bbbbac
+ bbbbbac
+
+/(?!\A)x/m
+ a\bx\n
+\= Expect no match
+ x\nb\n
+
+/\x0{ab}/
+ \0{ab}
+
+/(A|B)*?CD/
+ CD
+
+/(A|B)*CD/
+ CD
+
+/(?<!bar)foo/
+ foo
+ catfood
+ arfootle
+ rfoosh
+\= Expect no match
+ barfoo
+ towbarfoo
+
+/\w{3}(?<!bar)foo/
+ catfood
+\= Expect no match
+ foo
+ barfoo
+ towbarfoo
+
+/(?<=(foo)a)bar/
+ fooabar
+\= Expect no match
+ bar
+ foobbar
+
+/\Aabc\z/m
+ abc
+\= Expect no match
+ abc\n
+ qqq\nabc
+ abc\nzzz
+ qqq\nabc\nzzz
+
+"(?>.*/)foo"
+\= Expect no match
+ /this/is/a/very/long/line/in/deed/with/very/many/slashes/in/it/you/see/
+
+"(?>.*/)foo"
+ /this/is/a/very/long/line/in/deed/with/very/many/slashes/in/and/foo
+
+/(?>(\.\d\d[1-9]?))\d+/
+ 1.230003938
+ 1.875000282
+\= Expect no match
+ 1.235
+
+/^((?>\w+)|(?>\s+))*$/
+ now is the time for all good men to come to the aid of the party
+\= Expect no match
+ this is not a line with only words and spaces!
+
+/(\d+)(\w)/
+ 12345a
+ 12345+
+
+/((?>\d+))(\w)/
+ 12345a
+\= Expect no match
+ 12345+
+
+/(?>a+)b/
+ aaab
+
+/((?>a+)b)/
+ aaab
+
+/(?>(a+))b/
+ aaab
+
+/(?>b)+/
+ aaabbbccc
+
+/(?>a+|b+|c+)*c/
+ aaabbbbccccd
+
+/(a+|b+|c+)*c/
+ aaabbbbccccd
+
+/((?>[^()]+)|\([^()]*\))+/
+ ((abc(ade)ufh()()x
+
+/\(((?>[^()]+)|\([^()]+\))+\)/
+ (abc)
+ (abc(def)xyz)
+\= Expect no match
+ ((()aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/a(?-i)b/i
+ ab
+ Ab
+\= Expect no match
+ aB
+ AB
+
+/(a (?x)b c)d e/
+ a bcd e
+\= Expect no match
+ a b cd e
+ abcd e
+ a bcde
+
+/(a b(?x)c d (?-x)e f)/
+ a bcde f
+\= Expect no match
+ abcdef
+
+/(a(?i)b)c/
+ abc
+ aBc
+\= Expect no match
+ abC
+ aBC
+ Abc
+ ABc
+ ABC
+ AbC
+
+/a(?i:b)c/
+ abc
+ aBc
+\= Expect no match
+ ABC
+ abC
+ aBC
+
+/a(?i:b)*c/
+ aBc
+ aBBc
+\= Expect no match
+ aBC
+ aBBC
+
+/a(?=b(?i)c)\w\wd/
+ abcd
+ abCd
+\= Expect no match
+ aBCd
+ abcD
+
+/(?s-i:more.*than).*million/i
+ more than million
+ more than MILLION
+ more \n than Million
+\= Expect no match
+ MORE THAN MILLION
+ more \n than \n million
+
+/(?:(?s-i)more.*than).*million/i
+ more than million
+ more than MILLION
+ more \n than Million
+\= Expect no match
+ MORE THAN MILLION
+ more \n than \n million
+
+/(?>a(?i)b+)+c/
+ abc
+ aBbc
+ aBBc
+\= Expect no match
+ Abc
+ abAb
+ abbC
+
+/(?=a(?i)b)\w\wc/
+ abc
+ aBc
+\= Expect no match
+ Ab
+ abC
+ aBC
+
+/(?<=a(?i)b)(\w\w)c/
+ abxxc
+ aBxxc
+\= Expect no match
+ Abxxc
+ ABxxc
+ abxxC
+
+/^(?(?=abc)\w{3}:|\d\d)$/
+ abc:
+ 12
+\= Expect no match
+ 123
+ xyz
+
+/^(?(?!abc)\d\d|\w{3}:)$/
+ abc:
+ 12
+\= Expect no match
+ 123
+ xyz
+
+/(?(?<=foo)bar|cat)/
+ foobar
+ cat
+ fcat
+ focat
+\= Expect no match
+ foocat
+
+/(?(?<!foo)cat|bar)/
+ foobar
+ cat
+ fcat
+ focat
+\= Expect no match
+ foocat
+
+/(?>a*)*/
+ a
+ aa
+ aaaa
+
+/(abc|)+/
+ abc
+ abcabc
+ abcabcabc
+ xyz
+
+/([a]*)*/
+ a
+ aaaaa
+
+/([ab]*)*/
+ a
+ b
+ ababab
+ aaaabcde
+ bbbb
+
+/([^a]*)*/
+ b
+ bbbb
+ aaa
+
+/([^ab]*)*/
+ cccc
+ abab
+
+/([a]*?)*/
+ a
+ aaaa
+
+/([ab]*?)*/
+ a
+ b
+ abab
+ baba
+
+/([^a]*?)*/
+ b
+ bbbb
+ aaa
+
+/([^ab]*?)*/
+ c
+ cccc
+ baba
+
+/(?>a*)*/
+ a
+ aaabcde
+
+/((?>a*))*/
+ aaaaa
+ aabbaa
+
+/((?>a*?))*/
+ aaaaa
+ aabbaa
+
+/(?(?=[^a-z]+[a-z]) \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) /x
+ 12-sep-98
+ 12-09-98
+\= Expect no match
+ sep-12-98
+
+/(?i:saturday|sunday)/
+ saturday
+ sunday
+ Saturday
+ Sunday
+ SATURDAY
+ SUNDAY
+ SunDay
+
+/(a(?i)bc|BB)x/
+ abcx
+ aBCx
+ bbx
+ BBx
+\= Expect no match
+ abcX
+ aBCX
+ bbX
+ BBX
+
+/^([ab](?i)[cd]|[ef])/
+ ac
+ aC
+ bD
+ elephant
+ Europe
+ frog
+ France
+\= Expect no match
+ Africa
+
+/^(ab|a(?i)[b-c](?m-i)d|x(?i)y|z)/
+ ab
+ aBd
+ xy
+ xY
+ zebra
+ Zambesi
+\= Expect no match
+ aCD
+ XY
+
+/(?<=foo\n)^bar/m
+ foo\nbar
+\= Expect no match
+ bar
+ baz\nbar
+
+/(?<=(?<!foo)bar)baz/
+ barbaz
+ barbarbaz
+ koobarbaz
+\= Expect no match
+ baz
+ foobarbaz
+
+# The following tests are taken from the Perl 5.005 test suite; some of them
+# are compatible with 5.004, but I'd rather not have to sort them out.
+
+/abc/
+ abc
+ xabcy
+ ababc
+\= Expect no match
+ xbc
+ axc
+ abx
+
+/ab*c/
+ abc
+
+/ab*bc/
+ abc
+ abbc
+ abbbbc
+
+/.{1}/
+ abbbbc
+
+/.{3,4}/
+ abbbbc
+
+/ab{0,}bc/
+ abbbbc
+
+/ab+bc/
+ abbc
+\= Expect no match
+ abc
+ abq
+
+/ab+bc/
+ abbbbc
+
+/ab{1,}bc/
+ abbbbc
+
+/ab{1,3}bc/
+ abbbbc
+
+/ab{3,4}bc/
+ abbbbc
+
+/ab{4,5}bc/
+\= Expect no match
+ abq
+ abbbbc
+
+/ab?bc/
+ abbc
+ abc
+
+/ab{0,1}bc/
+ abc
+
+/ab?bc/
+
+/ab?c/
+ abc
+
+/ab{0,1}c/
+ abc
+
+/^abc$/
+ abc
+\= Expect no match
+ abbbbc
+ abcc
+
+/^abc/
+ abcc
+
+/^abc$/
+
+/abc$/
+ aabc
+ aabc
+\= Expect no match
+ aabcd
+
+/^/
+ abc
+
+/$/
+ abc
+
+/a.c/
+ abc
+ axc
+
+/a.*c/
+ axyzc
+
+/a[bc]d/
+ abd
+\= Expect no match
+ axyzd
+ abc
+
+/a[b-d]e/
+ ace
+
+/a[b-d]/
+ aac
+
+/a[-b]/
+ a-
+
+/a[b-]/
+ a-
+
+/a]/
+ a]
+
+/a[]]b/
+ a]b
+
+/a[^bc]d/
+ aed
+\= Expect no match
+ abd
+ abd
+
+/a[^-b]c/
+ adc
+
+/a[^]b]c/
+ adc
+ a-c
+\= Expect no match
+ a]c
+
+/\ba\b/
+ a-
+ -a
+ -a-
+
+/\by\b/
+\= Expect no match
+ xy
+ yz
+ xyz
+
+/\Ba\B/
+\= Expect no match
+ a-
+ -a
+ -a-
+
+/\By\b/
+ xy
+
+/\by\B/
+ yz
+
+/\By\B/
+ xyz
+
+/\w/
+ a
+
+/\W/
+ -
+\= Expect no match
+ a
+
+/a\sb/
+ a b
+
+/a\Sb/
+ a-b
+\= Expect no match
+ a b
+
+/\d/
+ 1
+
+/\D/
+ -
+\= Expect no match
+ 1
+
+/[\w]/
+ a
+
+/[\W]/
+ -
+\= Expect no match
+ a
+
+/a[\s]b/
+ a b
+
+/a[\S]b/
+ a-b
+\= Expect no match
+ a b
+
+/[\d]/
+ 1
+
+/[\D]/
+ -
+\= Expect no match
+ 1
+
+/ab|cd/
+ abc
+ abcd
+
+/()ef/
+ def
+
+/$b/
+
+/a\(b/
+ a(b
+
+/a\(*b/
+ ab
+ a((b
+
+/a\\b/
+ a\\b
+\= Expect no match
+ a\b
+
+/((a))/
+ abc
+
+/(a)b(c)/
+ abc
+
+/a+b+c/
+ aabbabc
+
+/a{1,}b{1,}c/
+ aabbabc
+
+/a.+?c/
+ abcabc
+
+/(a+|b)*/
+ ab
+
+/(a+|b){0,}/
+ ab
+
+/(a+|b)+/
+ ab
+
+/(a+|b){1,}/
+ ab
+
+/(a+|b)?/
+ ab
+
+/(a+|b){0,1}/
+ ab
+
+/[^ab]*/
+ cde
+
+/abc/
+\= Expect no match
+ b
+
+/a*/
+
+/([abc])*d/
+ abbbcd
+
+/([abc])*bcd/
+ abcd
+
+/a|b|c|d|e/
+ e
+
+/(a|b|c|d|e)f/
+ ef
+
+/abcd*efg/
+ abcdefg
+
+/ab*/
+ xabyabbbz
+ xayabbbz
+
+/(ab|cd)e/
+ abcde
+
+/[abhgefdc]ij/
+ hij
+
+/^(ab|cd)e/
+
+/(abc|)ef/
+ abcdef
+
+/(a|b)c*d/
+ abcd
+
+/(ab|ab*)bc/
+ abc
+
+/a([bc]*)c*/
+ abc
+
+/a([bc]*)(c*d)/
+ abcd
+
+/a([bc]+)(c*d)/
+ abcd
+
+/a([bc]*)(c+d)/
+ abcd
+
+/a[bcd]*dcdcde/
+ adcdcde
+
+/a[bcd]+dcdcde/
+\= Expect no match
+ abcde
+ adcdcde
+
+/(ab|a)b*c/
+ abc
+
+/((a)(b)c)(d)/
+ abcd
+
+/[a-zA-Z_][a-zA-Z0-9_]*/
+ alpha
+
+/^a(bc+|b[eh])g|.h$/
+ abh
+
+/(bc+d$|ef*g.|h?i(j|k))/
+ effgz
+ ij
+ reffgz
+\= Expect no match
+ effg
+ bcdd
+
+/((((((((((a))))))))))/
+ a
+
+/(((((((((a)))))))))/
+ a
+
+/multiple words of text/
+\= Expect no match
+ aa
+ uh-uh
+
+/multiple words/
+ multiple words, yeah
+
+/(.*)c(.*)/
+ abcde
+
+/\((.*), (.*)\)/
+ (a, b)
+
+/[k]/
+
+/abcd/
+ abcd
+
+/a(bc)d/
+ abcd
+
+/a[-]?c/
+ ac
+
+/abc/i
+ ABC
+ XABCY
+ ABABC
+\= Expect no match
+ aaxabxbaxbbx
+ XBC
+ AXC
+ ABX
+
+/ab*c/i
+ ABC
+
+/ab*bc/i
+ ABC
+ ABBC
+
+/ab*?bc/i
+ ABBBBC
+
+/ab{0,}?bc/i
+ ABBBBC
+
+/ab+?bc/i
+ ABBC
+
+/ab+bc/i
+\= Expect no match
+ ABC
+ ABQ
+
+/ab{1,}bc/i
+
+/ab+bc/i
+ ABBBBC
+
+/ab{1,}?bc/i
+ ABBBBC
+
+/ab{1,3}?bc/i
+ ABBBBC
+
+/ab{3,4}?bc/i
+ ABBBBC
+
+/ab{4,5}?bc/i
+\= Expect no match
+ ABQ
+ ABBBBC
+
+/ab??bc/i
+ ABBC
+ ABC
+
+/ab{0,1}?bc/i
+ ABC
+
+/ab??bc/i
+
+/ab??c/i
+ ABC
+
+/ab{0,1}?c/i
+ ABC
+
+/^abc$/i
+ ABC
+\= Expect no match
+ ABBBBC
+ ABCC
+
+/^abc/i
+ ABCC
+
+/^abc$/i
+
+/abc$/i
+ AABC
+
+/^/i
+ ABC
+
+/$/i
+ ABC
+
+/a.c/i
+ ABC
+ AXC
+
+/a.*?c/i
+ AXYZC
+
+/a.*c/i
+ AABC
+\= Expect no match
+ AXYZD
+
+/a[bc]d/i
+ ABD
+
+/a[b-d]e/i
+ ACE
+\= Expect no match
+ ABC
+ ABD
+
+/a[b-d]/i
+ AAC
+
+/a[-b]/i
+ A-
+
+/a[b-]/i
+ A-
+
+/a]/i
+ A]
+
+/a[]]b/i
+ A]B
+
+/a[^bc]d/i
+ AED
+
+/a[^-b]c/i
+ ADC
+\= Expect no match
+ ABD
+ A-C
+
+/a[^]b]c/i
+ ADC
+
+/ab|cd/i
+ ABC
+ ABCD
+
+/()ef/i
+ DEF
+
+/$b/i
+\= Expect no match
+ A]C
+ B
+
+/a\(b/i
+ A(B
+
+/a\(*b/i
+ AB
+ A((B
+
+/a\\b/i
+\= Expect no match
+ A\=notbol
+
+/((a))/i
+ ABC
+
+/(a)b(c)/i
+ ABC
+
+/a+b+c/i
+ AABBABC
+
+/a{1,}b{1,}c/i
+ AABBABC
+
+/a.+?c/i
+ ABCABC
+
+/a.*?c/i
+ ABCABC
+
+/a.{0,5}?c/i
+ ABCABC
+
+/(a+|b)*/i
+ AB
+
+/(a+|b){0,}/i
+ AB
+
+/(a+|b)+/i
+ AB
+
+/(a+|b){1,}/i
+ AB
+
+/(a+|b)?/i
+ AB
+
+/(a+|b){0,1}/i
+ AB
+
+/(a+|b){0,1}?/i
+ AB
+
+/[^ab]*/i
+ CDE
+
+/abc/i
+
+/a*/i
+
+/([abc])*d/i
+ ABBBCD
+
+/([abc])*bcd/i
+ ABCD
+
+/a|b|c|d|e/i
+ E
+
+/(a|b|c|d|e)f/i
+ EF
+
+/abcd*efg/i
+ ABCDEFG
+
+/ab*/i
+ XABYABBBZ
+ XAYABBBZ
+
+/(ab|cd)e/i
+ ABCDE
+
+/[abhgefdc]ij/i
+ HIJ
+
+/^(ab|cd)e/i
+\= Expect no match
+ ABCDE
+
+/(abc|)ef/i
+ ABCDEF
+
+/(a|b)c*d/i
+ ABCD
+
+/(ab|ab*)bc/i
+ ABC
+
+/a([bc]*)c*/i
+ ABC
+
+/a([bc]*)(c*d)/i
+ ABCD
+
+/a([bc]+)(c*d)/i
+ ABCD
+
+/a([bc]*)(c+d)/i
+ ABCD
+
+/a[bcd]*dcdcde/i
+ ADCDCDE
+
+/a[bcd]+dcdcde/i
+
+/(ab|a)b*c/i
+ ABC
+
+/((a)(b)c)(d)/i
+ ABCD
+
+/[a-zA-Z_][a-zA-Z0-9_]*/i
+ ALPHA
+
+/^a(bc+|b[eh])g|.h$/i
+ ABH
+
+/(bc+d$|ef*g.|h?i(j|k))/i
+ EFFGZ
+ IJ
+ REFFGZ
+\= Expect no match
+ ADCDCDE
+ EFFG
+ BCDD
+
+/((((((((((a))))))))))/i
+ A
+
+/(((((((((a)))))))))/i
+ A
+
+/(?:(?:(?:(?:(?:(?:(?:(?:(?:(a))))))))))/i
+ A
+
+/(?:(?:(?:(?:(?:(?:(?:(?:(?:(a|b|c))))))))))/i
+ C
+
+/multiple words of text/i
+\= Expect no match
+ AA
+ UH-UH
+
+/multiple words/i
+ MULTIPLE WORDS, YEAH
+
+/(.*)c(.*)/i
+ ABCDE
+
+/\((.*), (.*)\)/i
+ (A, B)
+
+/[k]/i
+
+/abcd/i
+ ABCD
+
+/a(bc)d/i
+ ABCD
+
+/a[-]?c/i
+ AC
+
+/a(?!b)./
+ abad
+
+/a(?=d)./
+ abad
+
+/a(?=c|d)./
+ abad
+
+/a(?:b|c|d)(.)/
+ ace
+
+/a(?:b|c|d)*(.)/
+ ace
+
+/a(?:b|c|d)+?(.)/
+ ace
+ acdbcdbe
+
+/a(?:b|c|d)+(.)/
+ acdbcdbe
+
+/a(?:b|c|d){2}(.)/
+ acdbcdbe
+
+/a(?:b|c|d){4,5}(.)/
+ acdbcdbe
+
+/a(?:b|c|d){4,5}?(.)/
+ acdbcdbe
+
+/((foo)|(bar))*/
+ foobar
+
+/a(?:b|c|d){6,7}(.)/
+ acdbcdbe
+
+/a(?:b|c|d){6,7}?(.)/
+ acdbcdbe
+
+/a(?:b|c|d){5,6}(.)/
+ acdbcdbe
+
+/a(?:b|c|d){5,6}?(.)/
+ acdbcdbe
+
+/a(?:b|c|d){5,7}(.)/
+ acdbcdbe
+
+/a(?:b|c|d){5,7}?(.)/
+ acdbcdbe
+
+/a(?:b|(c|e){1,2}?|d)+?(.)/
+ ace
+
+/^(.+)?B/
+ AB
+
+/^([^a-z])|(\^)$/
+ .
+
+/^[<>]&/
+ <&OUT
+
+/(?:(f)(o)(o)|(b)(a)(r))*/
+ foobar
+
+/(?<=a)b/
+ ab
+\= Expect no match
+ cb
+ b
+
+/(?<!c)b/
+ ab
+ b
+ b
+
+/(?:..)*a/
+ aba
+
+/(?:..)*?a/
+ aba
+
+/^(){3,5}/
+ abc
+
+/^(a+)*ax/
+ aax
+
+/^((a|b)+)*ax/
+ aax
+
+/^((a|bc)+)*ax/
+ aax
+
+/(a|x)*ab/
+ cab
+
+/(a)*ab/
+ cab
+
+/(?:(?i)a)b/
+ ab
+
+/((?i)a)b/
+ ab
+
+/(?:(?i)a)b/
+ Ab
+
+/((?i)a)b/
+ Ab
+
+/(?:(?i)a)b/
+\= Expect no match
+ cb
+ aB
+
+/((?i)a)b/
+
+/(?i:a)b/
+ ab
+
+/((?i:a))b/
+ ab
+
+/(?i:a)b/
+ Ab
+
+/((?i:a))b/
+ Ab
+
+/(?i:a)b/
+\= Expect no match
+ aB
+ aB
+
+/((?i:a))b/
+
+/(?:(?-i)a)b/i
+ ab
+
+/((?-i)a)b/i
+ ab
+
+/(?:(?-i)a)b/i
+ aB
+
+/((?-i)a)b/i
+ aB
+
+/(?:(?-i)a)b/i
+ aB
+\= Expect no match
+ Ab
+
+/((?-i)a)b/i
+
+/(?:(?-i)a)b/i
+ aB
+
+/((?-i)a)b/i
+ aB
+
+/(?:(?-i)a)b/i
+\= Expect no match
+ Ab
+ AB
+
+/((?-i)a)b/i
+
+/(?-i:a)b/i
+ ab
+
+/((?-i:a))b/i
+ ab
+
+/(?-i:a)b/i
+ aB
+
+/((?-i:a))b/i
+ aB
+
+/(?-i:a)b/i
+\= Expect no match
+ AB
+ Ab
+
+/((?-i:a))b/i
+
+/(?-i:a)b/i
+ aB
+
+/((?-i:a))b/i
+ aB
+
+/(?-i:a)b/i
+\= Expect no match
+ Ab
+ AB
+
+/((?-i:a))b/i
+
+/((?-i:a.))b/i
+\= Expect no match
+ AB
+ a\nB
+
+/((?s-i:a.))b/i
+ a\nB
+
+/(?:c|d)(?:)(?:a(?:)(?:b)(?:b(?:))(?:b(?:)(?:b)))/
+ cabbbb
+
+/(?:c|d)(?:)(?:aaaaaaaa(?:)(?:bbbbbbbb)(?:bbbbbbbb(?:))(?:bbbbbbbb(?:)(?:bbbbbbbb)))/
+ caaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+
+/foo\w*\d{4}baz/
+ foobar1234baz
+
+/x(~~)*(?:(?:F)?)?/
+ x~~
+
+/^a(?#xxx){3}c/
+ aaac
+
+/^a (?#xxx) (?#yyy) {3}c/x
+ aaac
+
+/(?<![cd])b/
+\= Expect no match
+ B\nB
+ dbcb
+
+/(?<![cd])[ab]/
+ dbaacb
+
+/(?<!(c|d))b/
+
+/(?<!(c|d))[ab]/
+ dbaacb
+
+/(?<!cd)[ab]/
+ cdaccb
+
+/^(?:a?b?)*$/
+\= Expect no match
+ dbcb
+ a--
+
+/((?s)^a(.))((?m)^b$)/
+ a\nb\nc\n
+
+/((?m)^b$)/
+ a\nb\nc\n
+
+/(?m)^b/
+ a\nb\n
+
+/(?m)^(b)/
+ a\nb\n
+
+/((?m)^b)/
+ a\nb\n
+
+/\n((?m)^b)/
+ a\nb\n
+
+/((?s).)c(?!.)/
+ a\nb\nc\n
+ a\nb\nc\n
+
+/((?s)b.)c(?!.)/
+ a\nb\nc\n
+ a\nb\nc\n
+
+/^b/
+
+/()^b/
+\= Expect no match
+ a\nb\nc\n
+ a\nb\nc\n
+
+/((?m)^b)/
+ a\nb\nc\n
+
+/(?(?!a)a|b)/
+
+/(?(?!a)b|a)/
+ a
+
+/(?(?=a)b|a)/
+\= Expect no match
+ a
+ a
+
+/(?(?=a)a|b)/
+ a
+
+/(\w+:)+/
+ one:
+
+/$(?<=^(a))/
+ a
+
+/([\w:]+::)?(\w+)$/
+ abcd
+ xy:z:::abcd
+
+/^[^bcd]*(c+)/
+ aexycd
+
+/(a*)b+/
+ caab
+
+/([\w:]+::)?(\w+)$/
+ abcd
+ xy:z:::abcd
+\= Expect no match
+ abcd:
+ abcd:
+
+/^[^bcd]*(c+)/
+ aexycd
+
+/(>a+)ab/
+
+/(?>a+)b/
+ aaab
+
+/([[:]+)/
+ a:[b]:
+
+/([[=]+)/
+ a=[b]=
+
+/([[.]+)/
+ a.[b].
+
+/((?>a+)b)/
+ aaab
+
+/(?>(a+))b/
+ aaab
+
+/((?>[^()]+)|\([^()]*\))+/
+ ((abc(ade)ufh()()x
+
+/a\Z/
+\= Expect no match
+ aaab
+ a\nb\n
+
+/b\Z/
+ a\nb\n
+
+/b\z/
+
+/b\Z/
+ a\nb
+
+/b\z/
+ a\nb
+
+/(?>.*)(?<=(abcd|wxyz))/
+ alphabetabcd
+ endingwxyz
+\= Expect no match
+ a rather long string that doesn't end with one of them
+
+/word (?>(?:(?!otherword)[a-zA-Z0-9]+ ){0,30})otherword/
+ word cat dog elephant mussel cow horse canary baboon snake shark otherword
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark
+
+/word (?>[a-zA-Z0-9]+ ){0,30}otherword/
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark the quick brown fox and the lazy dog and several other words getting close to thirty by now I hope
+
+/(?<=\d{3}(?!999))foo/
+ 999foo
+ 123999foo
+\= Expect no match
+ 123abcfoo
+
+/(?<=(?!...999)\d{3})foo/
+ 999foo
+ 123999foo
+\= Expect no match
+ 123abcfoo
+
+/(?<=\d{3}(?!999)...)foo/
+ 123abcfoo
+ 123456foo
+\= Expect no match
+ 123999foo
+
+/(?<=\d{3}...)(?<!999)foo/
+ 123abcfoo
+ 123456foo
+\= Expect no match
+ 123999foo
+
+/((Z)+|A)*/
+ ZABCDEFG
+
+/(Z()|A)*/
+ ZABCDEFG
+
+/(Z(())|A)*/
+ ZABCDEFG
+
+/((?>Z)+|A)*/
+ ZABCDEFG
+
+/((?>)+|A)*/
+ ZABCDEFG
+
+/a*/g
+ abbab
+
+/[[:space:]]+/
+ > \x09\x0a\x0c\x0d\x0b<
+
+/[[:blank:]]+/
+ > \x09\x0a\x0c\x0d\x0b<
+
+/[\s]+/
+ > \x09\x0a\x0c\x0d\x0b<
+
+/\s+/
+ > \x09\x0a\x0c\x0d\x0b<
+
+/a b/x
+ ab
+
+/(?!\A)x/m
+ a\nxb\n
+
+/(?!^)x/m
+\= Expect no match
+ a\nxb\n
+
+/abc\Qabc\Eabc/
+ abcabcabc
+
+/abc\Q(*+|\Eabc/
+ abc(*+|abc
+
+/ abc\Q abc\Eabc/x
+ abc abcabc
+\= Expect no match
+ abcabcabc
+
+/abc#comment
+ \Q#not comment
+ literal\E/x
+ abc#not comment\n literal
+
+/abc#comment
+ \Q#not comment
+ literal/x
+ abc#not comment\n literal
+
+/abc#comment
+ \Q#not comment
+ literal\E #more comment
+ /x
+ abc#not comment\n literal
+
+/abc#comment
+ \Q#not comment
+ literal\E #more comment/x
+ abc#not comment\n literal
+
+/\Qabc\$xyz\E/
+ abc\\\$xyz
+
+/\Qabc\E\$\Qxyz\E/
+ abc\$xyz
+
+/\Gabc/
+ abc
+\= Expect no match
+ xyzabc
+
+/\Gabc./g
+ abc1abc2xyzabc3
+
+/abc./g
+ abc1abc2xyzabc3
+
+/a(?x: b c )d/
+ XabcdY
+\= Expect no match
+ Xa b c d Y
+
+/((?x)x y z | a b c)/
+ XabcY
+ AxyzB
+
+/(?i)AB(?-i)C/
+ XabCY
+\= Expect no match
+ XabcY
+
+/((?i)AB(?-i)C|D)E/
+ abCE
+ DE
+\= Expect no match
+ abcE
+ abCe
+ dE
+ De
+
+/[z\Qa-d]\E]/
+ z
+ a
+ -
+ d
+ ]
+\= Expect no match
+ b
+
+/(a+)*b/
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/(?i)reg(?:ul(?:[aä]|ae)r|ex)/
+ REGular
+ regulaer
+ Regex
+ regulär
+
+/Åæåä[à-ÿÀ-ß]+/
+ Åæåäà
+ Åæåäÿ
+ ÅæåäÀ
+ Åæåäß
+
+/(?<=Z)X./
+ \x84XAZXB
+
+/^(?(2)a|(1)(2))+$/
+ 123a
+
+/(?<=a|bbbb)c/
+ ac
+ bbbbc
+
+/line\nbreak/
+ this is a line\nbreak
+ line one\nthis is a line\nbreak in the second line
+
+/line\nbreak/firstline
+ this is a line\nbreak
+\= Expect no match
+ line one\nthis is a line\nbreak in the second line
+
+/line\nbreak/m,firstline
+ this is a line\nbreak
+\= Expect no match
+ line one\nthis is a line\nbreak in the second line
+
+/1234/
+ 123\=ps
+\= Expect no match
+ a4\=ps,dfa_restart
+
+/1234/
+ 123\=ps
+ 4\=ps,dfa_restart
+
+/^/gm
+ a\nb\nc\n
+ \
+
+/(?<=C\n)^/gm
+ A\nC\nC\n
+
+/(?s)A?B/
+ AB
+ aB
+
+/(?s)A*B/
+ AB
+ aB
+
+/(?m)A?B/
+ AB
+ aB
+
+/(?m)A*B/
+ AB
+ aB
+
+/Content-Type\x3A[^\r\n]{6,}/
+ Content-Type:xxxxxyyy
+
+/Content-Type\x3A[^\r\n]{6,}z/
+ Content-Type:xxxxxyyyz
+
+/Content-Type\x3A[^a]{6,}/
+ Content-Type:xxxyyy
+
+/Content-Type\x3A[^a]{6,}z/
+ Content-Type:xxxyyyz
+
+/^abc/Im,newline=lf
+ xyz\nabc
+ xyz\r\nabc
+\= Expect no match
+ xyz\rabc
+ xyzabc\r
+ xyzabc\rpqr
+ xyzabc\r\n
+ xyzabc\r\npqr
+
+/^abc/Im,newline=crlf
+ xyz\r\nabclf>
+\= Expect no match
+ xyz\nabclf
+ xyz\rabclf
+
+/^abc/Im,newline=cr
+ xyz\rabc
+\= Expect no match
+ xyz\nabc
+ xyz\r\nabc
+
+/.*/I,newline=lf
+ abc\ndef
+ abc\rdef
+ abc\r\ndef
+
+/.*/I,newline=cr
+ abc\ndef
+ abc\rdef
+ abc\r\ndef
+
+/.*/I,newline=crlf
+ abc\ndef
+ abc\rdef
+ abc\r\ndef
+
+/\w+(.)(.)?def/Is
+ abc\ndef
+ abc\rdef
+ abc\r\ndef
+
+/\w+(.)(.)?def/s
+ abc\ndef
+ abc\rdef
+ abc\r\ndef
+
+/^\w+=.*(\\\n.*)*/
+ abc=xyz\\\npqr
+
+/^(a()*)*/
+ aaaa
+
+/^(?:a(?:(?:))*)*/
+ aaaa
+
+/^(a()+)+/
+ aaaa
+
+/^(?:a(?:(?:))+)+/
+ aaaa
+
+/(a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/(?>a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/(?:a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+/^a.b/newline=lf
+ a\rb
+\= Expect no match
+ a\nb
+
+/^a.b/newline=cr
+ a\nb
+\= Expect no match
+ a\rb
+
+/^a.b/newline=anycrlf
+ a\x85b
+\= Expect no match
+ a\rb
+
+/^a.b/newline=any
+\= Expect no match
+ a\nb
+ a\rb
+ a\x85b
+
+/^abc./gmx,newline=any
+ abc1 \x0aabc2 \x0babc3xx \x0cabc4 \x0dabc5xx \x0d\x0aabc6 \x85abc7 JUNK
+
+/abc.$/gmx,newline=any
+ abc1\x0a abc2\x0b abc3\x0c abc4\x0d abc5\x0d\x0a abc6\x85 abc9
+
+/^a\Rb/bsr=unicode
+ a\nb
+ a\rb
+ a\r\nb
+ a\x0bb
+ a\x0cb
+ a\x85b
+\= Expect no match
+ a\n\rb
+
+/^a\R*b/bsr=unicode
+ ab
+ a\nb
+ a\rb
+ a\r\nb
+ a\x0bb
+ a\x0cb
+ a\x85b
+ a\n\rb
+ a\n\r\x85\x0cb
+
+/^a\R+b/bsr=unicode
+ a\nb
+ a\rb
+ a\r\nb
+ a\x0bb
+ a\x0cb
+ a\x85b
+ a\n\rb
+ a\n\r\x85\x0cb
+\= Expect no match
+ ab
+
+/^a\R{1,3}b/bsr=unicode
+ a\nb
+ a\n\rb
+ a\n\r\x85b
+ a\r\n\r\nb
+ a\r\n\r\n\r\nb
+ a\n\r\n\rb
+ a\n\n\r\nb
+\= Expect no match
+ a\n\n\n\rb
+ a\r
+
+/.+foo/
+ afoo
+\= Expect no match
+ \r\nfoo
+ \nfoo
+
+/.+foo/newline=crlf
+ afoo
+ \nfoo
+\= Expect no match
+ \r\nfoo
+
+/.+foo/newline=any
+ afoo
+\= Expect no match
+ \nfoo
+ \r\nfoo
+
+/.+foo/s
+ afoo
+ \r\nfoo
+ \nfoo
+
+/^$/gm,newline=any
+ abc\r\rxyz
+ abc\n\rxyz
+\= Expect no match
+ abc\r\nxyz
+
+/^X/m
+ XABC
+\= Expect no match
+ XABC\=notbol
+
+/(?m)^$/g,newline=any,aftertext
+ abc\r\n\r\n
+
+/(?m)^$|^\r\n/g,newline=any,aftertext
+ abc\r\n\r\n
+
+/(?m)$/g,newline=any,aftertext
+ abc\r\n\r\n
+
+/(?|(abc)|(xyz))/
+ >abc<
+ >xyz<
+
+/(x)(?|(abc)|(xyz))(x)/
+ xabcx
+ xxyzx
+
+/(x)(?|(abc)(pqr)|(xyz))(x)/
+ xabcpqrx
+ xxyzx
+
+/(?|(abc)|(xyz))(?1)/
+ abcabc
+ xyzabc
+\= Expect no match
+ xyzxyz
+
+/\H\h\V\v/
+ X X\x0a
+ X\x09X\x0b
+\= Expect no match
+ \xa0 X\x0a
+
+/\H*\h+\V?\v{3,4}/
+ \x09\x20\xa0X\x0a\x0b\x0c\x0d\x0a
+ \x09\x20\xa0\x0a\x0b\x0c\x0d\x0a
+ \x09\x20\xa0\x0a\x0b\x0c
+\= Expect no match
+ \x09\x20\xa0\x0a\x0b
+
+/\H{3,4}/
+ XY ABCDE
+ XY PQR ST
+
+/.\h{3,4}./
+ XY AB PQRS
+
+/\h*X\h?\H+Y\H?Z/
+ >XNNNYZ
+ > X NYQZ
+\= Expect no match
+ >XYZ
+ > X NY Z
+
+/\v*X\v?Y\v+Z\V*\x0a\V+\x0b\V{2,3}\x0c/
+ >XY\x0aZ\x0aA\x0bNN\x0c
+ >\x0a\x0dX\x0aY\x0a\x0bZZZ\x0aAAA\x0bNNN\x0c
+
+/.+A/newline=crlf
+\= Expect no match
+ \r\nA
+
+/\nA/newline=crlf
+ \r\nA
+
+/[\r\n]A/newline=crlf
+ \r\nA
+
+/(\r|\n)A/newline=crlf
+ \r\nA
+
+/a\Rb/I,bsr=anycrlf
+ a\rb
+ a\nb
+ a\r\nb
+\= Expect no match
+ a\x85b
+ a\x0bb
+
+/a\Rb/I,bsr=unicode
+ a\rb
+ a\nb
+ a\r\nb
+ a\x85b
+ a\x0bb
+
+/a\R?b/I,bsr=anycrlf
+ a\rb
+ a\nb
+ a\r\nb
+\= Expect no match
+ a\x85b
+ a\x0bb
+
+/a\R?b/I,bsr=unicode
+ a\rb
+ a\nb
+ a\r\nb
+ a\x85b
+ a\x0bb
+
+/a\R{2,4}b/I,bsr=anycrlf
+ a\r\n\nb
+ a\n\r\rb
+ a\r\n\r\n\r\n\r\nb
+\= Expect no match
+ a\x0b\x0bb
+ a\x85\x85b
+
+/a\R{2,4}b/I,bsr=unicode
+ a\r\rb
+ a\n\n\nb
+ a\r\n\n\r\rb
+ a\x85\x85b
+ a\x0b\x0bb
+\= Expect no match
+ a\r\r\r\r\rb
+
+/a(?!)|\wbc/
+ abc
+
+/a[]b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+
+/a[]+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+
+/a[]*+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+
+/a[^]b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+ aXb
+ a\nb
+\= Expect no match
+ ab
+
+/a[^]+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+ aXb
+ a\nX\nXb
+\= Expect no match
+ ab
+
+/X$/dollar_endonly
+ X
+\= Expect no match
+ X\n
+
+/X$/
+ X
+ X\n
+
+/xyz/auto_callout
+ xyz
+ abcxyz
+\= Expect no match
+ abc
+ abcxypqr
+
+/xyz/auto_callout,no_start_optimize
+ abcxyz
+\= Expect no match
+ abc
+ abcxypqr
+
+/(*NO_START_OPT)xyz/auto_callout
+ abcxyz
+
+/(?C)ab/
+ ab
+ ab\=callout_none
+
+/ab/auto_callout
+ ab
+ ab\=callout_none
+
+/^"((?(?=[a])[^"])|b)*"$/auto_callout
+ "ab"
+ "ab"\=callout_none
+
+/\d+X|9+Y/
+ ++++123999\=ps
+ ++++123999Y\=ps
+
+/Z(*F)/
+\= Expect no match
+ Z\=ps
+ ZA\=ps
+
+/Z(?!)/
+\= Expect no match
+ Z\=ps
+ ZA\=ps
+
+/dog(sbody)?/
+ dogs\=ps
+ dogs\=ph
+
+/dog(sbody)??/
+ dogs\=ps
+ dogs\=ph
+
+/dog|dogsbody/
+ dogs\=ps
+ dogs\=ph
+
+/dogsbody|dog/
+ dogs\=ps
+ dogs\=ph
+
+/Z(*F)Q|ZXY/
+ Z\=ps
+\= Expect no match
+ ZA\=ps
+ X\=ps
+
+/\bthe cat\b/
+ the cat\=ps
+ the cat\=ph
+
+/dog(sbody)?/
+ dogs\=ps
+ body\=dfa_restart
+
+/dog(sbody)?/
+ dogs\=ph
+ body\=dfa_restart
+
+/abc/
+ abc\=ps
+ abc\=ph
+
+/abc\K123/
+ xyzabc123pqr
+
+/(?<=abc)123/
+ xyzabc123pqr
+ xyzabc12\=ps
+ xyzabc12\=ph
+
+/\babc\b/
+ +++abc+++
+ +++ab\=ps
+ +++ab\=ph
+
+/(?=C)/g,aftertext
+ ABCDECBA
+
+/(abc|def|xyz)/I
+ terhjk;abcdaadsfe
+ the quick xyz brown fox
+\= Expect no match
+ thejk;adlfj aenjl;fda asdfasd ehj;kjxyasiupd
+
+/(abc|def|xyz)/I,no_start_optimize
+ terhjk;abcdaadsfe
+ the quick xyz brown fox
+\= Expect no match
+ thejk;adlfj aenjl;fda asdfasd ehj;kjxyasiupd
+
+/abcd*/aftertext
+ xxxxabcd\=ps
+ xxxxabcd\=ph
+ dddxxx\=dfa_restart
+ xxxxabcd\=ph
+ xxx\=dfa_restart
+
+/abcd*/i
+ xxxxabcd\=ps
+ xxxxabcd\=ph
+ XXXXABCD\=ps
+ XXXXABCD\=ph
+
+/abc\d*/
+ xxxxabc1\=ps
+ xxxxabc1\=ph
+
+/abc[de]*/
+ xxxxabcde\=ps
+ xxxxabcde\=ph
+
+/(?:(?1)|B)(A(*F)|C)/
+ ABCD
+ CCD
+\= Expect no match
+ CAD
+
+/^(?:(?1)|B)(A(*F)|C)/
+ CCD
+ BCD
+\= Expect no match
+ ABCD
+ CAD
+ BAD
+
+/^(?!a(*SKIP)b)/
+ ac
+
+/^(?=a(*SKIP)b|ac)/
+ ac
+
+/^(?=a(*THEN)b|ac)/
+ ac
+
+/^(?=a(*PRUNE)b)/
+ ab
+
+/^(?(?!a(*SKIP)b))/
+ ac
+
+/(?<=abc)def/
+ abc\=ph
+
+/abc$/
+ abc
+ abc\=ps
+ abc\=ph
+
+/abc$/m
+ abc
+ abc\n
+ abc\=ph
+ abc\n\=ph
+ abc\=ps
+ abc\n\=ps
+
+/abc\z/
+ abc
+ abc\=ps
+ abc\=ph
+
+/abc\Z/
+ abc
+ abc\=ps
+ abc\=ph
+
+/abc\b/
+ abc
+ abc\=ps
+ abc\=ph
+
+/abc\B/
+ abc\=ps
+ abc\=ph
+\= Expect no match
+ abc
+
+/.+/
+ abc\=offset=0
+ abc\=offset=1
+ abc\=offset=2
+\= Bad offsets
+ abc\=offset=4
+ abc\=offset=-4
+\= Expect no match
+ abc\=offset=3
+
+/^(?:a)++\w/
+ aaaab
+\= Expect no match
+ aaaa
+ bbb
+
+/^(?:aa|(?:a)++\w)/
+ aaaab
+ aaaa
+\= Expect no match
+ bbb
+
+/^(?:a)*+\w/
+ aaaab
+ bbb
+\= Expect no match
+ aaaa
+
+/^(a)++\w/
+ aaaab
+\= Expect no match
+ aaaa
+ bbb
+
+/^(a|)++\w/
+ aaaab
+\= Expect no match
+ aaaa
+ bbb
+
+/(?=abc){3}abc/aftertext
+ abcabcabc
+\= Expect no match
+ xyz
+
+/(?=abc)+abc/aftertext
+ abcabcabc
+\= Expect no match
+ xyz
+
+/(?=abc)++abc/aftertext
+ abcabcabc
+\= Expect no match
+ xyz
+
+/(?=abc){0}xyz/
+ xyz
+
+/(?=abc){1}xyz/
+\= Expect no match
+ xyz
+
+/(?=(a))?./
+ ab
+ bc
+
+/(?=(a))??./
+ ab
+ bc
+
+/^(?=(a)){0}b(?1)/
+ backgammon
+
+/^(?=(?1))?[az]([abc])d/
+ abd
+ zcdxx
+
+/^(?!a){0}\w+/
+ aaaaa
+
+/(?<=(abc))?xyz/
+ abcxyz
+ pqrxyz
+
+/((?2))((?1))/
+ abc
+
+/(?(R)a+|(?R)b)/
+ aaaabcde
+
+/(?(R)a+|((?R))b)/
+ aaaabcde
+
+/((?(R)a+|(?1)b))/
+ aaaabcde
+
+/((?(R2)a+|(?1)b))()/
+ aaaabcde
+
+/(?(R)a*(?1)|((?R))b)/
+ aaaabcde
+
+/(a+)/no_auto_possess
+ aaaa\=ovector=3
+ aaaa\=ovector=4
+
+/^\R/
+ \r\=ps
+ \r\=ph
+
+/^\R{2,3}x/
+ \r\=ps
+ \r\=ph
+ \r\r\=ps
+ \r\r\=ph
+ \r\r\r\=ps
+ \r\r\r\=ph
+ \r\rx
+ \r\r\rx
+
+/^\R{2,3}?x/
+ \r\=ps
+ \r\=ph
+ \r\r\=ps
+ \r\r\=ph
+ \r\r\r\=ps
+ \r\r\r\=ph
+ \r\rx
+ \r\r\rx
+
+/^\R?x/
+ \r\=ps
+ \r\=ph
+ x
+ \rx
+
+/^\R+x/
+ \r\=ps
+ \r\=ph
+ \r\n\=ps
+ \r\n\=ph
+ \rx
+
+/^a$/newline=crlf
+ a\r\=ps
+ a\r\=ph
+
+/^a$/m,newline=crlf
+ a\r\=ps
+ a\r\=ph
+
+/^(a$|a\r)/newline=crlf
+ a\r\=ps
+ a\r\=ph
+
+/^(a$|a\r)/m,newline=crlf
+ a\r\=ps
+ a\r\=ph
+
+/./newline=crlf
+ \r\=ps
+ \r\=ph
+
+/.{2,3}/newline=crlf
+ \r\=ps
+ \r\=ph
+ \r\r\=ps
+ \r\r\=ph
+ \r\r\r\=ps
+ \r\r\r\=ph
+
+/.{2,3}?/newline=crlf
+ \r\=ps
+ \r\=ph
+ \r\r\=ps
+ \r\r\=ph
+ \r\r\r\=ps
+ \r\r\r\=ph
+
+# Test simple validity check for restarts
+
+/abcdef/
+ abc\=dfa_restart
+
+/<H((?(?!<H|F>)(.)|(?R))++)*F>/
+ text <H more text <H texting more hexA0-"\xA0" hex above 7F-"\xBC" F> text xxxxx <H text F> text F> text2 <H text sample F> more text.
+
+/^(?>.{4})abc|^\w\w.xabcd/
+ xxxxabcd
+ xx\xa0xabcd
+
+/^(.{4}){2}+abc|^\w\w.x\w\w\w\wabcd/
+ xxxxxxxxabcd
+ xx\xa0xxxxxabcd
+
+/abcd/
+ abcd\=ovector=0
+
+# These tests show up auto-possessification
+
+/[ab]*/
+ aaaa
+
+/[ab]*?/
+ aaaa
+
+/[ab]?/
+ aaaa
+
+/[ab]??/
+ aaaa
+
+/[ab]+/
+ aaaa
+
+/[ab]+?/
+ aaaa
+
+/[ab]{2,3}/
+ aaaa
+
+/[ab]{2,3}?/
+ aaaa
+
+/[ab]{2,}/
+ aaaa
+
+/[ab]{2,}?/
+ aaaa
+
+'\A(?:[^\"]++|\"(?:[^\"]*+|\"\")*+\")++'
+ NON QUOTED \"QUOT\"\"ED\" AFTER \"NOT MATCHED
+
+'\A(?:[^\"]++|\"(?:[^\"]++|\"\")*+\")++'
+ NON QUOTED \"QUOT\"\"ED\" AFTER \"NOT MATCHED
+
+/abc(?=xyz)/allusedtext
+ abcxyzpqr
+ abcxyzpqr\=aftertext
+
+/(?<=pqr)abc(?=xyz)/allusedtext
+ xyzpqrabcxyzpqr
+ xyzpqrabcxyzpqr\=aftertext
+
+/a\b/
+ a.\=allusedtext
+ a\=allusedtext
+
+/abc(?=abcde)(?=ab)/allusedtext
+ abcabcdefg
+
+/a*?b*?/
+ ab
+
+/(*NOTEMPTY)a*?b*?/
+ ab
+ ba
+ cb
+
+/(*NOTEMPTY_ATSTART)a*?b*?/aftertext
+ ab
+ cdab
+
+/(a)(b)|(c)/
+ XcX\=ovector=2,get=1,get=2,get=3,get=4,getall
+
+/(?<A>aa)/
+ aa\=get=A
+ aa\=copy=A
+
+/a+/no_auto_possess
+ a\=ovector=2,get=1,get=2,getall
+ aaa\=ovector=2,get=1,get=2,getall
+
+/a(b)c(d)/
+ abc\=ph,copy=0,copy=1,getall
+
+/ab(?C" any text with spaces ")cde/B
+ abcde
+ 12abcde
+
+/^a(b)c(?C1)def/
+ abcdef
+
+/^a(b)c(?C"AB")def/
+ abcdef
+
+/^a(b)c(?C1)def/
+ abcdef\=callout_capture
+
+/^a(b)c(?C{AB})def/B
+ abcdef\=callout_capture
+
+/^(?(?C25)(?=abc)abcd|xyz)/B
+ abcdefg
+ xyz123
+
+/^(?(?C$abc$)(?=abc)abcd|xyz)/B
+ abcdefg
+ xyz123
+
+/^ab(?C'first')cd(?C"second")ef/
+ abcdefg
+
+/(?:a(?C`code`)){3}X/
+ aaaXY
+
+# Binary zero in callout string
+/"a(?C'x" 00 "z')b"/hex
+ abcdefgh
+
+/(?(?!)a|b)/
+ bbb
+\= Expect no match
+ aaa
+
+/^/gm
+ \n\n\n
+
+/^/gm,alt_circumflex
+ \n\n\n
+
+/abc/use_offset_limit
+ 1234abcde\=offset_limit=100
+ 1234abcde\=offset_limit=9
+ 1234abcde\=offset_limit=4
+ 1234abcde\=offset_limit=4,offset=4
+\= Expect no match
+ 1234abcde\=offset_limit=4,offset=5
+ 1234abcde\=offset_limit=3
+
+/(?<=abc)/use_offset_limit
+ 1234abc\=offset_limit=7
+\= Expect no match
+ 1234abc\=offset_limit=6
+
+/abcd/null_context
+ abcd\=null_context
+
+/()()a+/no_auto_possess
+ aaa\=allcaptures
+ a\=allcaptures
+
+#/(*LIMIT_DEPTH=100)^((.)(?1)|.)$/
+#\= Expect depth limit exceeded
+# a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+
+/(*LIMIT_HEAP=0)^((.)(?1)|.)$/
+\= Expect heap limit exceeded
+ a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+
+#/(*LIMIT_HEAP=50000)^((.)(?1)|.)$/
+#\= Expect success
+# a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+
+/(02-)?[0-9]{3}-[0-9]{3}/
+ 02-123-123
+
+/^(a(?2))(b)(?1)/
+ abbab\=find_limits
+
+/abc/endanchored
+ xyzabc
+\= Expect no match
+ xyzabcdef
+\= Expect error
+ xyzabc\=ph
+
+/abc/
+ xyzabc\=endanchored
+\= Expect no match
+ xyzabcdef\=endanchored
+\= Expect error
+ xyzabc\=ps,endanchored
+
+/abc|bcd/endanchored
+ xyzabcd
+\= Expect no match
+ xyzabcdef
+
+/(*NUL)^.*/
+ a\nb\x00ccc
+
+/(*NUL)^.*/s
+ a\nb\x00ccc
+
+/^x/m,newline=nul
+ ab\x00xy
+
+/'#comment' 0d 0a 00 '^x\' 0a 'y'/x,newline=nul,hex
+ x\nyz
+
+/(*NUL)^X\NY/
+ X\nY
+ X\rY
+\= Expect no match
+ X\x00Y
+
+/(?<=abc|)/
+ abcde\=aftertext
+
+/(?<=|abc)/
+ abcde\=aftertext
+
+/(?<=abc|)/endanchored
+ abcde\=aftertext
+
+/(?<=|abc)/endanchored
+ abcde\=aftertext
+
+#/(*LIMIT_MATCH=100).*(?![|H]?.*(?![|H]?););.*(?![|H]?.*(?![|H]?););\x00\x00\x00\x00\x00\x00\x00(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?![|);)?.*(![|H]?);)?.*(?![|H]?);)?.*(?![|H]?);)?.*(?![|H]););![|H]?););[|H]?);|H]?);)\x00\x00\x00 \x00\x00\x00H]?););?![|H]?);)?.*(?![|H]?););[||H]?);)?.*(?![|H]?););[|H]?);(?![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););;[\x00\x00\x00\x00\x00\x00\x00![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););/no_dotstar_anchor
+#\= Expect limit exceeded
+#.*(?![|H]?.*(?![|H]?););.*(?![|H]?.*(?![|H]?););\x00\x00\x00\x00\x00\x00\x00(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?![|);)?.*(![|H]?);)?.*(?![|H]?);)?.*(?![|H]?);)?.*(?![|H]););![|H]?););[|H]?);|H]?);)\x00\x00\x00 \x00\x00\x00H]?););?![|H]?);)?.*(?![|H]?););[||H]?);)?.*(?![|H]?););[|H]?);(?![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););;[\x00\x00\x00\x00\x00\x00\x00![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););
+
+/\n/firstline
+ xyz\nabc
+
+/\nabc/firstline
+ xyz\nabc
+
+/\x{0a}abc/firstline,newline=crlf
+\= Expect no match
+ xyz\r\nabc
+
+/[abc]/firstline
+\= Expect no match
+ \na
+
+# End of testinput6
diff --git a/test/monniaux/pcre2-10.32/testdata/testoutput6 b/test/monniaux/pcre2-10.32/testdata/testoutput6
new file mode 100644
index 00000000..caec833f
--- /dev/null
+++ b/test/monniaux/pcre2-10.32/testdata/testoutput6
@@ -0,0 +1,7786 @@
+# This set of tests check the DFA matching functionality of pcre2_dfa_match(),
+# excluding UTF and Unicode property support. All matches are done using DFA,
+# forced by setting a default subject modifier at the start.
+
+#forbid_utf
+#subject dfa
+#newline_default lf anycrlf any
+
+/abc/
+ abc
+ 0: abc
+
+/ab*c/
+ abc
+ 0: abc
+ abbbbc
+ 0: abbbbc
+ ac
+ 0: ac
+
+/ab+c/
+ abc
+ 0: abc
+ abbbbbbc
+ 0: abbbbbbc
+\= Expect no match
+ ac
+No match
+ ab
+No match
+
+/a*/no_auto_possess
+ a
+ 0: a
+ 1:
+ aaaaaaaaaaaaaaaaa
+Matched, but offsets vector is too small to show all matches
+ 0: aaaaaaaaaaaaaaaaa
+ 1: aaaaaaaaaaaaaaaa
+ 2: aaaaaaaaaaaaaaa
+ 3: aaaaaaaaaaaaaa
+ 4: aaaaaaaaaaaaa
+ 5: aaaaaaaaaaaa
+ 6: aaaaaaaaaaa
+ 7: aaaaaaaaaa
+ 8: aaaaaaaaa
+ 9: aaaaaaaa
+10: aaaaaaa
+11: aaaaaa
+12: aaaaa
+13: aaaa
+14: aaa
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\=ovector=10
+Matched, but offsets vector is too small to show all matches
+ 0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ 1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ 2: aaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ 3: aaaaaaaaaaaaaaaaaaaaaaaaaaa
+ 4: aaaaaaaaaaaaaaaaaaaaaaaaaa
+ 5: aaaaaaaaaaaaaaaaaaaaaaaaa
+ 6: aaaaaaaaaaaaaaaaaaaaaaaa
+ 7: aaaaaaaaaaaaaaaaaaaaaaa
+ 8: aaaaaaaaaaaaaaaaaaaaaa
+ 9: aaaaaaaaaaaaaaaaaaaaa
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\=dfa_shortest
+ 0:
+
+/(a|abcd|african)/
+ a
+ 0: a
+ abcd
+ 0: abcd
+ 1: a
+ african
+ 0: african
+ 1: a
+
+/^abc/
+ abcdef
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+ xyz\nabc
+No match
+
+/^abc/m
+ abcdef
+ 0: abc
+ xyz\nabc
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+
+/\Aabc/
+ abcdef
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+ xyz\nabc
+No match
+
+/\Aabc/m
+ abcdef
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+ xyz\nabc
+No match
+
+/\Gabc/
+ abcdef
+ 0: abc
+ xyzabc\=offset=3
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+ xyzabc\=offset=2
+No match
+
+/x\dy\Dz/
+ x9yzz
+ 0: x9yzz
+ x0y+z
+ 0: x0y+z
+\= Expect no match
+ xyz
+No match
+ xxy0z
+No match
+
+/x\sy\Sz/
+ x yzz
+ 0: x yzz
+ x y+z
+ 0: x y+z
+\= Expect no match
+ xyz
+No match
+ xxyyz
+No match
+
+/x\wy\Wz/
+ xxy+z
+ 0: xxy+z
+\= Expect no match
+ xxy0z
+No match
+ x+y+z
+No match
+
+/x.y/
+ x+y
+ 0: x+y
+ x-y
+ 0: x-y
+\= Expect no match
+ x\ny
+No match
+
+/x.y/s
+ x+y
+ 0: x+y
+ x-y
+ 0: x-y
+ x\ny
+ 0: x\x0ay
+
+/(a.b(?s)c.d|x.y)p.q/
+ a+bc+dp+q
+ 0: a+bc+dp+q
+ a+bc\ndp+q
+ 0: a+bc\x0adp+q
+ x\nyp+q
+ 0: x\x0ayp+q
+\= Expect no match
+ a\nbc\ndp+q
+No match
+ a+bc\ndp\nq
+No match
+ x\nyp\nq
+No match
+
+/a\d\z/
+ ba0
+ 0: a0
+\= Expect no match
+ ba0\n
+No match
+ ba0\ncd
+No match
+
+/a\d\z/m
+ ba0
+ 0: a0
+\= Expect no match
+ ba0\n
+No match
+ ba0\ncd
+No match
+
+/a\d\Z/
+ ba0
+ 0: a0
+ ba0\n
+ 0: a0
+\= Expect no match
+ ba0\ncd
+No match
+
+/a\d\Z/m
+ ba0
+ 0: a0
+ ba0\n
+ 0: a0
+\= Expect no match
+ ba0\ncd
+No match
+
+/a\d$/
+ ba0
+ 0: a0
+ ba0\n
+ 0: a0
+\= Expect no match
+ ba0\ncd
+No match
+
+/a\d$/m
+ ba0
+ 0: a0
+ ba0\n
+ 0: a0
+ ba0\ncd
+ 0: a0
+
+/abc/i
+ abc
+ 0: abc
+ aBc
+ 0: aBc
+ ABC
+ 0: ABC
+
+/[^a]/
+ abcd
+ 0: b
+
+/ab?\w/
+ abz
+ 0: abz
+ 1: ab
+ abbz
+ 0: abb
+ 1: ab
+ azz
+ 0: az
+
+/x{0,3}yz/
+ ayzq
+ 0: yz
+ axyzq
+ 0: xyz
+ axxyz
+ 0: xxyz
+ axxxyzq
+ 0: xxxyz
+ axxxxyzq
+ 0: xxxyz
+\= Expect no match
+ ax
+No match
+ axx
+No match
+
+/x{3}yz/
+ axxxyzq
+ 0: xxxyz
+ axxxxyzq
+ 0: xxxyz
+\= Expect no match
+ ax
+No match
+ axx
+No match
+ ayzq
+No match
+ axyzq
+No match
+ axxyz
+No match
+
+/x{2,3}yz/
+ axxyz
+ 0: xxyz
+ axxxyzq
+ 0: xxxyz
+ axxxxyzq
+ 0: xxxyz
+\= Expect no match
+ ax
+No match
+ axx
+No match
+ ayzq
+No match
+ axyzq
+No match
+
+/[^a]+/no_auto_possess
+ bac
+ 0: b
+ bcdefax
+ 0: bcdef
+ 1: bcde
+ 2: bcd
+ 3: bc
+ 4: b
+\= Expect no match
+ aaaaa
+No match
+
+/[^a]*/no_auto_possess
+ bac
+ 0: b
+ 1:
+ bcdefax
+ 0: bcdef
+ 1: bcde
+ 2: bcd
+ 3: bc
+ 4: b
+ 5:
+ aaaaa
+ 0:
+
+/[^a]{3,5}/no_auto_possess
+ xyz
+ 0: xyz
+ awxyza
+ 0: wxyz
+ 1: wxy
+ abcdefa
+ 0: bcdef
+ 1: bcde
+ 2: bcd
+ abcdefghijk
+ 0: bcdef
+ 1: bcde
+ 2: bcd
+\= Expect no match
+ axya
+No match
+ axa
+No match
+ aaaaa
+No match
+
+/\d*/
+ 1234b567
+ 0: 1234
+ xyz
+ 0:
+
+/\D*/
+ a1234b567
+ 0: a
+ xyz
+ 0: xyz
+
+/\d+/
+ ab1234c56
+ 0: 1234
+\= Expect no match
+ xyz
+No match
+
+/\D+/
+ ab123c56
+ 0: ab
+\= Expect no match
+ 789
+No match
+
+/\d?A/
+ 045ABC
+ 0: 5A
+ ABC
+ 0: A
+\= Expect no match
+ XYZ
+No match
+
+/\D?A/
+ ABC
+ 0: A
+ BAC
+ 0: BA
+ 9ABC
+ 0: A
+
+/a+/
+ aaaa
+ 0: aaaa
+
+/^.*xyz/
+ xyz
+ 0: xyz
+ ggggggggxyz
+ 0: ggggggggxyz
+
+/^.+xyz/
+ abcdxyz
+ 0: abcdxyz
+ axyz
+ 0: axyz
+\= Expect no match
+ xyz
+No match
+
+/^.?xyz/
+ xyz
+ 0: xyz
+ cxyz
+ 0: cxyz
+
+/^\d{2,3}X/
+ 12X
+ 0: 12X
+ 123X
+ 0: 123X
+\= Expect no match
+ X
+No match
+ 1X
+No match
+ 1234X
+No match
+
+/^[abcd]\d/
+ a45
+ 0: a4
+ b93
+ 0: b9
+ c99z
+ 0: c9
+ d04
+ 0: d0
+\= Expect no match
+ e45
+No match
+ abcd
+No match
+ abcd1234
+No match
+ 1234
+No match
+
+/^[abcd]*\d/
+ a45
+ 0: a4
+ b93
+ 0: b9
+ c99z
+ 0: c9
+ d04
+ 0: d0
+ abcd1234
+ 0: abcd1
+ 1234
+ 0: 1
+\= Expect no match
+ e45
+No match
+ abcd
+No match
+
+/^[abcd]+\d/
+ a45
+ 0: a4
+ b93
+ 0: b9
+ c99z
+ 0: c9
+ d04
+ 0: d0
+ abcd1234
+ 0: abcd1
+\= Expect no match
+ 1234
+No match
+ e45
+No match
+ abcd
+No match
+
+/^a+X/
+ aX
+ 0: aX
+ aaX
+ 0: aaX
+
+/^[abcd]?\d/
+ a45
+ 0: a4
+ b93
+ 0: b9
+ c99z
+ 0: c9
+ d04
+ 0: d0
+ 1234
+ 0: 1
+\= Expect no match
+ abcd1234
+No match
+ e45
+No match
+
+/^[abcd]{2,3}\d/
+ ab45
+ 0: ab4
+ bcd93
+ 0: bcd9
+\= Expect no match
+ 1234
+No match
+ a36
+No match
+ abcd1234
+No match
+ ee45
+No match
+
+/^(abc)*\d/
+ abc45
+ 0: abc4
+ abcabcabc45
+ 0: abcabcabc4
+ 42xyz
+ 0: 4
+
+/^(abc)+\d/
+ abc45
+ 0: abc4
+ abcabcabc45
+ 0: abcabcabc4
+\= Expect no match
+ 42xyz
+No match
+
+/^(abc)?\d/
+ abc45
+ 0: abc4
+ 42xyz
+ 0: 4
+\= Expect no match
+ abcabcabc45
+No match
+
+/^(abc){2,3}\d/
+ abcabc45
+ 0: abcabc4
+ abcabcabc45
+ 0: abcabcabc4
+\= Expect no match
+ abcabcabcabc45
+No match
+ abc45
+No match
+ 42xyz
+No match
+
+/1(abc|xyz)2(?1)3/
+ 1abc2abc3456
+ 0: 1abc2abc3
+ 1abc2xyz3456
+ 0: 1abc2xyz3
+
+/^(a*\w|ab)=(a*\w|ab)/
+ ab=ab
+ 0: ab=ab
+ 1: ab=a
+
+/^(a*\w|ab)=(?1)/
+ ab=ab
+ 0: ab=ab
+ 1: ab=a
+
+/^([^()]|\((?1)*\))*$/
+ abc
+ 0: abc
+ a(b)c
+ 0: a(b)c
+ a(b(c))d
+ 0: a(b(c))d
+\= Expect no match)
+ a(b(c)d
+No match
+
+/^>abc>([^()]|\((?1)*\))*<xyz<$/
+ >abc>123<xyz<
+ 0: >abc>123<xyz<
+ >abc>1(2)3<xyz<
+ 0: >abc>1(2)3<xyz<
+ >abc>(1(2)3)<xyz<
+ 0: >abc>(1(2)3)<xyz<
+
+/^(?>a*)\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9876
+ 0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/< (?: (?(R) \d++ | [^<>]*+) | (?R)) * >/x
+ <>
+ 0: <>
+ <abcd>
+ 0: <abcd>
+ <abc <123> hij>
+ 0: <abc <123> hij>
+ <abc <def> hij>
+ 0: <def>
+ <abc<>def>
+ 0: <abc<>def>
+ <abc<>
+ 0: <>
+\= Expect no match
+ <abc
+No match
+
+/^(?(?=abc)\w{3}:|\d\d)$/
+ abc:
+ 0: abc:
+ 12
+ 0: 12
+\= Expect no match
+ 123
+No match
+ xyz
+No match
+
+/^(?(?!abc)\d\d|\w{3}:)$/
+ abc:
+ 0: abc:
+ 12
+ 0: 12
+\= Expect no match
+ 123
+No match
+ xyz
+No match
+
+/^(?=abc)\w{5}:$/
+ abcde:
+ 0: abcde:
+\= Expect no match
+ abc..
+No match
+ 123
+No match
+ vwxyz
+No match
+
+/^(?!abc)\d\d$/
+ 12
+ 0: 12
+\= Expect no match
+ abcde:
+No match
+ abc..
+No match
+ 123
+No match
+ vwxyz
+No match
+
+/(?<=abc|xy)123/
+ abc12345
+ 0: 123
+ wxy123z
+ 0: 123
+\= Expect no match
+ 123abc
+No match
+
+/(?<!abc|xy)123/
+ 123abc
+ 0: 123
+ mno123456
+ 0: 123
+\= Expect no match
+ abc12345
+No match
+ wxy123z
+No match
+
+/abc(?C1)xyz/
+ abcxyz
+--->abcxyz
+ 1 ^ ^ x
+ 0: abcxyz
+ 123abcxyz999
+--->123abcxyz999
+ 1 ^ ^ x
+ 0: abcxyz
+
+/(ab|cd){3,4}/auto_callout
+ ababab
+--->ababab
+ +0 ^ (
+ +1 ^ a
+ +4 ^ c
+ +2 ^^ b
+ +3 ^ ^ |
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +2 ^ ^ b
+ +3 ^ ^ |
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +2 ^ ^ b
+ +3 ^ ^ |
++12 ^ ^ End of pattern
+ +1 ^ ^ a
+ +4 ^ ^ c
+ 0: ababab
+ abcdabcd
+--->abcdabcd
+ +0 ^ (
+ +1 ^ a
+ +4 ^ c
+ +2 ^^ b
+ +3 ^ ^ |
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +5 ^ ^ d
+ +6 ^ ^ ){3,4}
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +2 ^ ^ b
+ +3 ^ ^ |
++12 ^ ^ End of pattern
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +5 ^ ^ d
+ +6 ^ ^ ){3,4}
++12 ^ ^ End of pattern
+ 0: abcdabcd
+ 1: abcdab
+ abcdcdcdcdcd
+--->abcdcdcdcdcd
+ +0 ^ (
+ +1 ^ a
+ +4 ^ c
+ +2 ^^ b
+ +3 ^ ^ |
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +5 ^ ^ d
+ +6 ^ ^ ){3,4}
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +5 ^ ^ d
+ +6 ^ ^ ){3,4}
++12 ^ ^ End of pattern
+ +1 ^ ^ a
+ +4 ^ ^ c
+ +5 ^ ^ d
+ +6 ^ ^ ){3,4}
++12 ^ ^ End of pattern
+ 0: abcdcdcd
+ 1: abcdcd
+
+/^abc/
+ abcdef
+ 0: abc
+\= Expect no match
+ abcdef\=notbol
+No match
+
+/^(a*|xyz)/
+ bcd
+ 0:
+ aaabcd
+ 0: aaa
+ xyz
+ 0: xyz
+ 1:
+ xyz\=notempty
+ 0: xyz
+\= Expect no match
+ bcd\=notempty
+No match
+
+/xyz$/
+ xyz
+ 0: xyz
+ xyz\n
+ 0: xyz
+\= Expect no match
+ xyz\=noteol
+No match
+ xyz\n\=noteol
+No match
+
+/xyz$/m
+ xyz
+ 0: xyz
+ xyz\n
+ 0: xyz
+ abcxyz\npqr
+ 0: xyz
+ abcxyz\npqr\=noteol
+ 0: xyz
+ xyz\n\=noteol
+ 0: xyz
+\= Expect no match
+ xyz\=noteol
+No match
+
+/\Gabc/
+ abcdef
+ 0: abc
+ defabcxyz\=offset=3
+ 0: abc
+\= Expect no match
+ defabcxyz
+No match
+
+/^abcdef/
+ ab\=ps
+Partial match: ab
+ abcde\=ps
+Partial match: abcde
+ abcdef\=ps
+ 0: abcdef
+\= Expect no match
+ abx\=ps
+No match
+
+/^a{2,4}\d+z/
+ a\=ps
+Partial match: a
+ aa\=ps
+Partial match: aa
+ aa2\=ps
+Partial match: aa2
+ aaa\=ps
+Partial match: aaa
+ aaa23\=ps
+Partial match: aaa23
+ aaaa12345\=ps
+Partial match: aaaa12345
+ aa0z\=ps
+ 0: aa0z
+ aaaa4444444444444z\=ps
+ 0: aaaa4444444444444z
+\= Expect no match
+ az\=ps
+No match
+ aaaaa\=ps
+No match
+ a56\=ps
+No match
+
+/^abcdef/
+ abc\=ps
+Partial match: abc
+ def\=dfa_restart
+ 0: def
+
+/(?<=foo)bar/
+ foob\=ps,offset=2
+Partial match: foob
+ <<<
+ foobar...\=ps,dfa_restart,offset=4
+ 0: ar
+ foobar\=offset=2
+ 0: bar
+\= Expect no match
+ xyzfo\=ps
+No match
+ obar\=dfa_restart
+No match
+
+/(ab*(cd|ef))+X/
+ lkjhlkjhlkjhlkjhabbbbbbcdaefabbbbbbbefa\=ps,notbol,noteol
+Partial match: abbbbbbcdaefabbbbbbbefa
+ cdabbbbbbbb\=ps,notbol,dfa_restart,noteol
+Partial match: cdabbbbbbbb
+ efabbbbbbbbbbbbbbbb\=ps,notbol,dfa_restart,noteol
+Partial match: efabbbbbbbbbbbbbbbb
+ bbbbbbbbbbbbcdXyasdfadf\=ps,notbol,dfa_restart,noteol
+ 0: bbbbbbbbbbbbcdX
+\= Expect no match
+ adfadadaklhlkalkajhlkjahdfasdfasdfladsfjkj\=ps,noteol
+No match
+
+/the quick brown fox/
+ the quick brown fox
+ 0: the quick brown fox
+ What do you know about the quick brown fox?
+ 0: the quick brown fox
+\= Expect no match
+ The quick brown FOX
+No match
+ What do you know about THE QUICK BROWN FOX?
+No match
+
+/The quick brown fox/i
+ the quick brown fox
+ 0: the quick brown fox
+ The quick brown FOX
+ 0: The quick brown FOX
+ What do you know about the quick brown fox?
+ 0: the quick brown fox
+ What do you know about THE QUICK BROWN FOX?
+ 0: THE QUICK BROWN FOX
+
+/abcd\t\n\r\f\a\e\071\x3b\$\\\?caxyz/
+ abcd\t\n\r\f\a\e9;\$\\?caxyz
+ 0: abcd\x09\x0a\x0d\x0c\x07\x1b9;$\?caxyz
+
+/a*abc?xyz+pqr{3}ab{2,}xy{4,5}pq{0,6}AB{0,}zz/
+ abxyzpqrrrabbxyyyypqAzz
+ 0: abxyzpqrrrabbxyyyypqAzz
+ abxyzpqrrrabbxyyyypqAzz
+ 0: abxyzpqrrrabbxyyyypqAzz
+ aabxyzpqrrrabbxyyyypqAzz
+ 0: aabxyzpqrrrabbxyyyypqAzz
+ aaabxyzpqrrrabbxyyyypqAzz
+ 0: aaabxyzpqrrrabbxyyyypqAzz
+ aaaabxyzpqrrrabbxyyyypqAzz
+ 0: aaaabxyzpqrrrabbxyyyypqAzz
+ abcxyzpqrrrabbxyyyypqAzz
+ 0: abcxyzpqrrrabbxyyyypqAzz
+ aabcxyzpqrrrabbxyyyypqAzz
+ 0: aabcxyzpqrrrabbxyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypAzz
+ 0: aaabcxyzpqrrrabbxyyyypAzz
+ aaabcxyzpqrrrabbxyyyypqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypqqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqqqqqAzz
+ aaabcxyzpqrrrabbxyyyypqqqqqqAzz
+ 0: aaabcxyzpqrrrabbxyyyypqqqqqqAzz
+ aaaabcxyzpqrrrabbxyyyypqAzz
+ 0: aaaabcxyzpqrrrabbxyyyypqAzz
+ abxyzzpqrrrabbxyyyypqAzz
+ 0: abxyzzpqrrrabbxyyyypqAzz
+ aabxyzzzpqrrrabbxyyyypqAzz
+ 0: aabxyzzzpqrrrabbxyyyypqAzz
+ aaabxyzzzzpqrrrabbxyyyypqAzz
+ 0: aaabxyzzzzpqrrrabbxyyyypqAzz
+ aaaabxyzzzzpqrrrabbxyyyypqAzz
+ 0: aaaabxyzzzzpqrrrabbxyyyypqAzz
+ abcxyzzpqrrrabbxyyyypqAzz
+ 0: abcxyzzpqrrrabbxyyyypqAzz
+ aabcxyzzzpqrrrabbxyyyypqAzz
+ 0: aabcxyzzzpqrrrabbxyyyypqAzz
+ aaabcxyzzzzpqrrrabbxyyyypqAzz
+ 0: aaabcxyzzzzpqrrrabbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbxyyyypqAzz
+ 0: aaaabcxyzzzzpqrrrabbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyyypqAzz
+ 0: aaaabcxyzzzzpqrrrabbbxyyyypqAzz
+ aaaabcxyzzzzpqrrrabbbxyyyyypqAzz
+ 0: aaaabcxyzzzzpqrrrabbbxyyyyypqAzz
+ aaabcxyzpqrrrabbxyyyypABzz
+ 0: aaabcxyzpqrrrabbxyyyypABzz
+ aaabcxyzpqrrrabbxyyyypABBzz
+ 0: aaabcxyzpqrrrabbxyyyypABBzz
+ >>>aaabxyzpqrrrabbxyyyypqAzz
+ 0: aaabxyzpqrrrabbxyyyypqAzz
+ >aaaabxyzpqrrrabbxyyyypqAzz
+ 0: aaaabxyzpqrrrabbxyyyypqAzz
+ >>>>abcxyzpqrrrabbxyyyypqAzz
+ 0: abcxyzpqrrrabbxyyyypqAzz
+\= Expect no match
+ abxyzpqrrabbxyyyypqAzz
+No match
+ abxyzpqrrrrabbxyyyypqAzz
+No match
+ abxyzpqrrrabxyyyypqAzz
+No match
+ aaaabcxyzzzzpqrrrabbbxyyyyyypqAzz
+No match
+ aaaabcxyzzzzpqrrrabbbxyyypqAzz
+No match
+ aaabcxyzpqrrrabbxyyyypqqqqqqqAzz
+No match
+
+/^(abc){1,2}zz/
+ abczz
+ 0: abczz
+ abcabczz
+ 0: abcabczz
+\= Expect no match
+ zz
+No match
+ abcabcabczz
+No match
+ >>abczz
+No match
+
+/^(b+?|a){1,2}?c/
+ bc
+ 0: bc
+ bbc
+ 0: bbc
+ bbbc
+ 0: bbbc
+ bac
+ 0: bac
+ bbac
+ 0: bbac
+ aac
+ 0: aac
+ abbbbbbbbbbbc
+ 0: abbbbbbbbbbbc
+ bbbbbbbbbbbac
+ 0: bbbbbbbbbbbac
+\= Expect no match
+ aaac
+No match
+ abbbbbbbbbbbac
+No match
+
+/^(b+|a){1,2}c/
+ bc
+ 0: bc
+ bbc
+ 0: bbc
+ bbbc
+ 0: bbbc
+ bac
+ 0: bac
+ bbac
+ 0: bbac
+ aac
+ 0: aac
+ abbbbbbbbbbbc
+ 0: abbbbbbbbbbbc
+ bbbbbbbbbbbac
+ 0: bbbbbbbbbbbac
+\= Expect no match
+ aaac
+No match
+ abbbbbbbbbbbac
+No match
+
+/^(b+|a){1,2}?bc/
+ bbc
+ 0: bbc
+
+/^(b*|ba){1,2}?bc/
+ babc
+ 0: babc
+ bbabc
+ 0: bbabc
+ bababc
+ 0: bababc
+\= Expect no match
+ bababbc
+No match
+ babababc
+No match
+
+/^(ba|b*){1,2}?bc/
+ babc
+ 0: babc
+ bbabc
+ 0: bbabc
+ bababc
+ 0: bababc
+\= Expect no match
+ bababbc
+No match
+ babababc
+No match
+
+/^\ca\cA\c[\c{\c:/
+ \x01\x01\e;z
+ 0: \x01\x01\x1b;z
+
+/^[ab\]cde]/
+ athing
+ 0: a
+ bthing
+ 0: b
+ ]thing
+ 0: ]
+ cthing
+ 0: c
+ dthing
+ 0: d
+ ething
+ 0: e
+\= Expect no match
+ fthing
+No match
+ [thing
+No match
+ \\thing
+No match
+
+/^[]cde]/
+ ]thing
+ 0: ]
+ cthing
+ 0: c
+ dthing
+ 0: d
+ ething
+ 0: e
+\= Expect no match
+ athing
+No match
+ fthing
+No match
+
+/^[^ab\]cde]/
+ fthing
+ 0: f
+ [thing
+ 0: [
+ \\thing
+ 0: \
+\= Expect no match
+ athing
+No match
+ bthing
+No match
+ ]thing
+No match
+ cthing
+No match
+ dthing
+No match
+ ething
+No match
+
+/^[^]cde]/
+ athing
+ 0: a
+ fthing
+ 0: f
+\= Expect no match
+ ]thing
+No match
+ cthing
+No match
+ dthing
+No match
+ ething
+No match
+
+/^\/
+
+ 0: \x81
+
+/^ÿ/
+ ÿ
+ 0: \xff
+
+/^[0-9]+$/
+ 0
+ 0: 0
+ 1
+ 0: 1
+ 2
+ 0: 2
+ 3
+ 0: 3
+ 4
+ 0: 4
+ 5
+ 0: 5
+ 6
+ 0: 6
+ 7
+ 0: 7
+ 8
+ 0: 8
+ 9
+ 0: 9
+ 10
+ 0: 10
+ 100
+ 0: 100
+\= Expect no match
+ abc
+No match
+
+/^.*nter/
+ enter
+ 0: enter
+ inter
+ 0: inter
+ uponter
+ 0: uponter
+
+/^xxx[0-9]+$/
+ xxx0
+ 0: xxx0
+ xxx1234
+ 0: xxx1234
+\= Expect no match
+ xxx
+No match
+
+/^.+[0-9][0-9][0-9]$/
+ x123
+ 0: x123
+ xx123
+ 0: xx123
+ 123456
+ 0: 123456
+ x1234
+ 0: x1234
+\= Expect no match
+ 123
+No match
+
+/^.+?[0-9][0-9][0-9]$/
+ x123
+ 0: x123
+ xx123
+ 0: xx123
+ 123456
+ 0: 123456
+ x1234
+ 0: x1234
+\= Expect no match
+ 123
+No match
+
+/^([^!]+)!(.+)=apquxz\.ixr\.zzz\.ac\.uk$/
+ abc!pqr=apquxz.ixr.zzz.ac.uk
+ 0: abc!pqr=apquxz.ixr.zzz.ac.uk
+\= Expect no match
+ !pqr=apquxz.ixr.zzz.ac.uk
+No match
+ abc!=apquxz.ixr.zzz.ac.uk
+No match
+ abc!pqr=apquxz:ixr.zzz.ac.uk
+No match
+ abc!pqr=apquxz.ixr.zzz.ac.ukk
+No match
+
+/:/
+ Well, we need a colon: somewhere
+ 0: :
+\= Expect no match
+ No match without a colon
+No match
+
+/([\da-f:]+)$/i
+ 0abc
+ 0: 0abc
+ abc
+ 0: abc
+ fed
+ 0: fed
+ E
+ 0: E
+ ::
+ 0: ::
+ 5f03:12C0::932e
+ 0: 5f03:12C0::932e
+ fed def
+ 0: def
+ Any old stuff
+ 0: ff
+\= Expect no match
+ 0zzz
+No match
+ gzzz
+No match
+ fed\x20
+No match
+ Any old rubbish
+No match
+
+/^.*\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
+ .1.2.3
+ 0: .1.2.3
+ A.12.123.0
+ 0: A.12.123.0
+\= Expect no match
+ .1.2.3333
+No match
+ 1.2.3
+No match
+ 1234.2.3
+No match
+
+/^(\d+)\s+IN\s+SOA\s+(\S+)\s+(\S+)\s*\(\s*$/
+ 1 IN SOA non-sp1 non-sp2(
+ 0: 1 IN SOA non-sp1 non-sp2(
+ 1 IN SOA non-sp1 non-sp2 (
+ 0: 1 IN SOA non-sp1 non-sp2 (
+\= Expect no match
+ 1IN SOA non-sp1 non-sp2(
+No match
+
+/^[a-zA-Z\d][a-zA-Z\d\-]*(\.[a-zA-Z\d][a-zA-z\d\-]*)*\.$/
+ a.
+ 0: a.
+ Z.
+ 0: Z.
+ 2.
+ 0: 2.
+ ab-c.pq-r.
+ 0: ab-c.pq-r.
+ sxk.zzz.ac.uk.
+ 0: sxk.zzz.ac.uk.
+ x-.y-.
+ 0: x-.y-.
+\= Expect no match
+ -abc.peq.
+No match
+
+/^\*\.[a-z]([a-z\-\d]*[a-z\d]+)?(\.[a-z]([a-z\-\d]*[a-z\d]+)?)*$/
+ *.a
+ 0: *.a
+ *.b0-a
+ 0: *.b0-a
+ *.c3-b.c
+ 0: *.c3-b.c
+ *.c-a.b-c
+ 0: *.c-a.b-c
+\= Expect no match
+ *.0
+No match
+ *.a-
+No match
+ *.a-b.c-
+No match
+ *.c-a.0-c
+No match
+
+/^(?=ab(de))(abd)(e)/
+ abde
+ 0: abde
+
+/^(?!(ab)de|x)(abd)(f)/
+ abdf
+ 0: abdf
+
+/^(?=(ab(cd)))(ab)/
+ abcd
+ 0: ab
+
+/^[\da-f](\.[\da-f])*$/i
+ a.b.c.d
+ 0: a.b.c.d
+ A.B.C.D
+ 0: A.B.C.D
+ a.b.c.1.2.3.C
+ 0: a.b.c.1.2.3.C
+
+/^\".*\"\s*(;.*)?$/
+ \"1234\"
+ 0: "1234"
+ \"abcd\" ;
+ 0: "abcd" ;
+ \"\" ; rhubarb
+ 0: "" ; rhubarb
+\= Expect no match
+ \"1234\" : things
+No match
+
+/^$/
+ \
+ 0:
+
+/ ^ a (?# begins with a) b\sc (?# then b c) $ (?# then end)/x
+ ab c
+ 0: ab c
+\= Expect no match
+ abc
+No match
+ ab cde
+No match
+
+/(?x) ^ a (?# begins with a) b\sc (?# then b c) $ (?# then end)/
+ ab c
+ 0: ab c
+\= Expect no match
+ abc
+No match
+ ab cde
+No match
+
+/^ a\ b[c ]d $/x
+ a bcd
+ 0: a bcd
+ a b d
+ 0: a b d
+\= Expect no match
+ abcd
+No match
+ ab d
+No match
+
+/^(a(b(c)))(d(e(f)))(h(i(j)))(k(l(m)))$/
+ abcdefhijklm
+ 0: abcdefhijklm
+
+/^(?:a(b(c)))(?:d(e(f)))(?:h(i(j)))(?:k(l(m)))$/
+ abcdefhijklm
+ 0: abcdefhijklm
+
+/^[\w][\W][\s][\S][\d][\D][\b][\n][\c]][\022]/
+ a+ Z0+\x08\n\x1d\x12
+ 0: a+ Z0+\x08\x0a\x1d\x12
+
+/^[.^$|()*+?{,}]+/
+ .^\$(*+)|{?,?}
+ 0: .^$(*+)|{?,?}
+
+/^a*\w/
+ z
+ 0: z
+ az
+ 0: az
+ 1: a
+ aaaz
+ 0: aaaz
+ 1: aaa
+ 2: aa
+ 3: a
+ a
+ 0: a
+ aa
+ 0: aa
+ 1: a
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ a+
+ 0: a
+ aa+
+ 0: aa
+ 1: a
+
+/^a*?\w/
+ z
+ 0: z
+ az
+ 0: az
+ 1: a
+ aaaz
+ 0: aaaz
+ 1: aaa
+ 2: aa
+ 3: a
+ a
+ 0: a
+ aa
+ 0: aa
+ 1: a
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ a+
+ 0: a
+ aa+
+ 0: aa
+ 1: a
+
+/^a+\w/
+ az
+ 0: az
+ aaaz
+ 0: aaaz
+ 1: aaa
+ 2: aa
+ aa
+ 0: aa
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ aa+
+ 0: aa
+
+/^a+?\w/
+ az
+ 0: az
+ aaaz
+ 0: aaaz
+ 1: aaa
+ 2: aa
+ aa
+ 0: aa
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ aa+
+ 0: aa
+
+/^\d{8}\w{2,}/
+ 1234567890
+ 0: 1234567890
+ 12345678ab
+ 0: 12345678ab
+ 12345678__
+ 0: 12345678__
+\= Expect no match
+ 1234567
+No match
+
+/^[aeiou\d]{4,5}$/
+ uoie
+ 0: uoie
+ 1234
+ 0: 1234
+ 12345
+ 0: 12345
+ aaaaa
+ 0: aaaaa
+\= Expect no match
+ 123456
+No match
+
+/^[aeiou\d]{4,5}?/
+ uoie
+ 0: uoie
+ 1234
+ 0: 1234
+ 12345
+ 0: 12345
+ 1: 1234
+ aaaaa
+ 0: aaaaa
+ 1: aaaa
+ 123456
+ 0: 12345
+ 1: 1234
+
+/^From +([^ ]+) +[a-zA-Z][a-zA-Z][a-zA-Z] +[a-zA-Z][a-zA-Z][a-zA-Z] +[0-9]?[0-9] +[0-9][0-9]:[0-9][0-9]/
+ From abcd Mon Sep 01 12:33:02 1997
+ 0: From abcd Mon Sep 01 12:33
+
+/^From\s+\S+\s+([a-zA-Z]{3}\s+){2}\d{1,2}\s+\d\d:\d\d/
+ From abcd Mon Sep 01 12:33:02 1997
+ 0: From abcd Mon Sep 01 12:33
+ From abcd Mon Sep 1 12:33:02 1997
+ 0: From abcd Mon Sep 1 12:33
+\= Expect no match
+ From abcd Sep 01 12:33:02 1997
+No match
+
+/^12.34/s
+ 12\n34
+ 0: 12\x0a34
+ 12\r34
+ 0: 12\x0d34
+
+/\w+(?=\t)/
+ the quick brown\t fox
+ 0: brown
+
+/foo(?!bar)(.*)/
+ foobar is foolish see?
+ 0: foolish see?
+
+/(?:(?!foo)...|^.{0,2})bar(.*)/
+ foobar crowbar etc
+ 0: rowbar etc
+ barrel
+ 0: barrel
+ 2barrel
+ 0: 2barrel
+ A barrel
+ 0: A barrel
+
+/^(\D*)(?=\d)(?!123)/
+ abc456
+ 0: abc
+\= Expect no match
+ abc123
+No match
+
+/^1234(?# test newlines
+ inside)/
+ 1234
+ 0: 1234
+
+/^1234 #comment in extended re
+ /x
+ 1234
+ 0: 1234
+
+/#rhubarb
+ abcd/x
+ abcd
+ 0: abcd
+
+/^abcd#rhubarb/x
+ abcd
+ 0: abcd
+
+/(?!^)abc/
+ the abc
+ 0: abc
+\= Expect no match
+ abc
+No match
+
+/(?=^)abc/
+ abc
+ 0: abc
+\= Expect no match
+ the abc
+No match
+
+/^[ab]{1,3}(ab*|b)/no_auto_possess
+ aabbbbb
+ 0: aabbbbb
+ 1: aabbbb
+ 2: aabbb
+ 3: aabb
+ 4: aab
+ 5: aa
+
+/^[ab]{1,3}?(ab*|b)/no_auto_possess
+ aabbbbb
+ 0: aabbbbb
+ 1: aabbbb
+ 2: aabbb
+ 3: aabb
+ 4: aab
+ 5: aa
+
+/^[ab]{1,3}?(ab*?|b)/no_auto_possess
+ aabbbbb
+ 0: aabbbbb
+ 1: aabbbb
+ 2: aabbb
+ 3: aabb
+ 4: aab
+ 5: aa
+
+/^[ab]{1,3}(ab*?|b)/no_auto_possess
+ aabbbbb
+ 0: aabbbbb
+ 1: aabbbb
+ 2: aabbb
+ 3: aabb
+ 4: aab
+ 5: aa
+
+/ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # optional leading comment
+(?: (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # initial word
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) )* # further okay, if led by a period
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+# address
+| # or
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # one word, optionally followed by....
+(?:
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] | # atom and space parts, or...
+\(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) | # comments, or...
+
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+# quoted strings
+)*
+< (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # leading <
+(?: @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* , (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+)* # further okay, if led by comma
+: # closing colon
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* )? # optional route
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) # initial word
+(?: (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+" (?: # opening quote...
+[^\\\x80-\xff\n\015"] # Anything except backslash and quote
+| # or
+\\ [^\x80-\xff] # Escaped something (something != CR)
+)* " # closing quote
+) )* # further okay, if led by a period
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* @ (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # initial subdomain
+(?: #
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* \. # if led by a period...
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* (?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+| \[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+) # ...further okay
+)*
+# address spec
+(?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* > # trailing >
+# name and address
+) (?: [\040\t] | \(
+(?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] | \( (?: [^\\\x80-\xff\n\015()] | \\ [^\x80-\xff] )* \) )*
+\) )* # optional trailing comment
+/x
+ Alan Other <user\@dom.ain>
+ 0: Alan Other <user@dom.ain>
+ <user\@dom.ain>
+ 0: user@dom.ain
+ 1: user@dom
+ user\@dom.ain
+ 0: user@dom.ain
+ 1: user@dom
+ \"A. Other\" <user.1234\@dom.ain> (a comment)
+ 0: "A. Other" <user.1234@dom.ain> (a comment)
+ 1: "A. Other" <user.1234@dom.ain>
+ 2: "A. Other" <user.1234@dom.ain>
+ A. Other <user.1234\@dom.ain> (a comment)
+ 0: Other <user.1234@dom.ain> (a comment)
+ 1: Other <user.1234@dom.ain>
+ 2: Other <user.1234@dom.ain>
+ \"/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/\"\@x400-re.lay
+ 0: "/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/"@x400-re.lay
+ 1: "/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/"@x400-re
+ A missing angle <user\@some.where
+ 0: user@some.where
+ 1: user@some
+\= Expect no match
+ The quick brown fox
+No match
+
+/[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional leading comment
+(?:
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# additional words
+)*
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+# address
+| # or
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+# leading word
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] * # "normal" atoms and or spaces
+(?:
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+|
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+) # "special" comment or quoted string
+[^()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037] * # more "normal"
+)*
+<
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# <
+(?:
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+(?: ,
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+)* # additional domains
+:
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)? # optional route
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+# Atom
+| # or
+" # "
+[^\\\x80-\xff\n\015"] * # normal
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015"] * )* # ( special normal* )*
+" # "
+# Quoted string
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# additional words
+)*
+@
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+(?:
+\.
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+(?:
+[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+ # some number of atom characters...
+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]) # ..not followed by something that could be part of an atom
+|
+\[ # [
+(?: [^\\\x80-\xff\n\015\[\]] | \\ [^\x80-\xff] )* # stuff
+\] # ]
+)
+[\040\t]* # Nab whitespace.
+(?:
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: # (
+(?: \\ [^\x80-\xff] |
+\( # (
+[^\\\x80-\xff\n\015()] * # normal*
+(?: \\ [^\x80-\xff] [^\\\x80-\xff\n\015()] * )* # (special normal*)*
+\) # )
+) # special
+[^\\\x80-\xff\n\015()] * # normal*
+)* # )*
+\) # )
+[\040\t]* )* # If comment found, allow more spaces.
+# optional trailing comments
+)*
+# address spec
+> # >
+# name and address
+)
+/x
+ Alan Other <user\@dom.ain>
+ 0: Alan Other <user@dom.ain>
+ <user\@dom.ain>
+ 0: user@dom.ain
+ 1: user@dom
+ user\@dom.ain
+ 0: user@dom.ain
+ 1: user@dom
+ \"A. Other\" <user.1234\@dom.ain> (a comment)
+ 0: "A. Other" <user.1234@dom.ain>
+ A. Other <user.1234\@dom.ain> (a comment)
+ 0: Other <user.1234@dom.ain>
+ \"/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/\"\@x400-re.lay
+ 0: "/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/"@x400-re.lay
+ 1: "/s=user/ou=host/o=place/prmd=uu.yy/admd= /c=gb/"@x400-re
+ A missing angle <user\@some.where
+ 0: user@some.where
+ 1: user@some
+\= Expect no match
+ The quick brown fox
+No match
+
+/abc\0def\00pqr\000xyz\0000AB/
+ abc\0def\00pqr\000xyz\0000AB
+ 0: abc\x00def\x00pqr\x00xyz\x000AB
+ abc456 abc\0def\00pqr\000xyz\0000ABCDE
+ 0: abc\x00def\x00pqr\x00xyz\x000AB
+
+/abc\x0def\x00pqr\x000xyz\x0000AB/
+ abc\x0def\x00pqr\x000xyz\x0000AB
+ 0: abc\x0def\x00pqr\x000xyz\x0000AB
+ abc456 abc\x0def\x00pqr\x000xyz\x0000ABCDE
+ 0: abc\x0def\x00pqr\x000xyz\x0000AB
+
+/^[\000-\037]/
+ \0A
+ 0: \x00
+ \01B
+ 0: \x01
+ \037C
+ 0: \x1f
+
+/\0*/
+ \0\0\0\0
+ 0: \x00\x00\x00\x00
+
+/A\x0{2,3}Z/
+ The A\x0\x0Z
+ 0: A\x00\x00Z
+ An A\0\x0\0Z
+ 0: A\x00\x00\x00Z
+\= Expect no match
+ A\0Z
+No match
+ A\0\x0\0\x0Z
+No match
+
+/^\s/
+ \040abc
+ 0:
+ \x0cabc
+ 0: \x0c
+ \nabc
+ 0: \x0a
+ \rabc
+ 0: \x0d
+ \tabc
+ 0: \x09
+\= Expect no match
+ abc
+No match
+
+/^a b
+ c/x
+ abc
+ 0: abc
+
+/ab{1,3}bc/
+ abbbbc
+ 0: abbbbc
+ abbbc
+ 0: abbbc
+ abbc
+ 0: abbc
+\= Expect no match
+ abc
+No match
+ abbbbbc
+No match
+
+/([^.]*)\.([^:]*):[T ]+(.*)/
+ track1.title:TBlah blah blah
+ 0: track1.title:TBlah blah blah
+
+/([^.]*)\.([^:]*):[T ]+(.*)/i
+ track1.title:TBlah blah blah
+ 0: track1.title:TBlah blah blah
+
+/([^.]*)\.([^:]*):[t ]+(.*)/i
+ track1.title:TBlah blah blah
+ 0: track1.title:TBlah blah blah
+
+/^[W-c]+$/
+ WXY_^abc
+ 0: WXY_^abc
+\= Expect no match
+ wxy
+No match
+
+/^[W-c]+$/i
+ WXY_^abc
+ 0: WXY_^abc
+ wxy_^ABC
+ 0: wxy_^ABC
+
+/^[\x3f-\x5F]+$/i
+ WXY_^abc
+ 0: WXY_^abc
+ wxy_^ABC
+ 0: wxy_^ABC
+
+/^abc$/m
+ abc
+ 0: abc
+ qqq\nabc
+ 0: abc
+ abc\nzzz
+ 0: abc
+ qqq\nabc\nzzz
+ 0: abc
+
+/^abc$/
+ abc
+ 0: abc
+\= Expect no match
+ qqq\nabc
+No match
+ abc\nzzz
+No match
+ qqq\nabc\nzzz
+No match
+
+/\Aabc\Z/m
+ abc
+ 0: abc
+ abc\n
+ 0: abc
+\= Expect no match
+ qqq\nabc
+No match
+ abc\nzzz
+No match
+ qqq\nabc\nzzz
+No match
+
+/\A(.)*\Z/s
+ abc\ndef
+ 0: abc\x0adef
+
+/\A(.)*\Z/m
+\= Expect no match
+ abc\ndef
+No match
+
+/(?:b)|(?::+)/
+ b::c
+ 0: b
+ c::b
+ 0: ::
+
+/[-az]+/
+ az-
+ 0: az-
+\= Expect no match
+ b
+No match
+
+/[az-]+/
+ za-
+ 0: za-
+\= Expect no match
+ b
+No match
+
+/[a\-z]+/
+ a-z
+ 0: a-z
+\= Expect no match
+ b
+No match
+
+/[a-z]+/
+ abcdxyz
+ 0: abcdxyz
+
+/[\d-]+/
+ 12-34
+ 0: 12-34
+\= Expect no match
+ aaa
+No match
+
+/\x5c/
+ \\
+ 0: \
+
+/\x20Z/
+ the Zoo
+ 0: Z
+\= Expect no match
+ Zulu
+No match
+
+/ab{3cd/
+ ab{3cd
+ 0: ab{3cd
+
+/ab{3,cd/
+ ab{3,cd
+ 0: ab{3,cd
+
+/ab{3,4a}cd/
+ ab{3,4a}cd
+ 0: ab{3,4a}cd
+
+/{4,5a}bc/
+ {4,5a}bc
+ 0: {4,5a}bc
+
+/^a.b/newline=lf
+ a\rb
+ 0: a\x0db
+\= Expect no match
+ a\nb
+No match
+
+/abc$/
+ abc
+ 0: abc
+ abc\n
+ 0: abc
+\= Expect no match
+ abc\ndef
+No match
+
+/(abc)\123/
+ abc\x53
+ 0: abcS
+
+/(abc)\223/
+ abc\x93
+ 0: abc\x93
+
+/(abc)\323/
+ abc\xd3
+ 0: abc\xd3
+
+/(abc)\100/
+ abc\x40
+ 0: abc@
+ abc\100
+ 0: abc@
+
+/(abc)\1000/
+ abc\x400
+ 0: abc@0
+ abc\x40\x30
+ 0: abc@0
+ abc\1000
+ 0: abc@0
+ abc\100\x30
+ 0: abc@0
+ abc\100\060
+ 0: abc@0
+ abc\100\60
+ 0: abc@0
+
+/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)\12\123/
+ abcdefghijk\12S
+ 0: abcdefghijk\x0aS
+
+/a{0}bc/
+ bc
+ 0: bc
+
+/(a|(bc)){0,0}?xyz/
+ xyz
+ 0: xyz
+
+/abc[\10]de/
+ abc\010de
+ 0: abc\x08de
+
+/abc[\1]de/
+ abc\1de
+ 0: abc\x01de
+
+/(abc)[\1]de/
+ abc\1de
+ 0: abc\x01de
+
+/(?s)a.b/
+ a\nb
+ 0: a\x0ab
+
+/^([^a])([^\b])([^c]*)([^d]{3,4})/
+ baNOTccccd
+ 0: baNOTcccc
+ 1: baNOTccc
+ 2: baNOTcc
+ 3: baNOTc
+ baNOTcccd
+ 0: baNOTccc
+ 1: baNOTcc
+ 2: baNOTc
+ baNOTccd
+ 0: baNOTcc
+ 1: baNOTc
+ bacccd
+ 0: baccc
+\= Expect no match
+ anything
+No match
+ b\bc
+No match
+ baccd
+No match
+
+/[^a]/
+ Abc
+ 0: A
+
+/[^a]/i
+ Abc
+ 0: b
+
+/[^a]+/
+ AAAaAbc
+ 0: AAA
+
+/[^a]+/i
+ AAAaAbc
+ 0: bc
+
+/[^a]+/
+ bbb\nccc
+ 0: bbb\x0accc
+
+/[^k]$/
+ abc
+ 0: c
+\= Expect no match
+ abk
+No match
+
+/[^k]{2,3}$/
+ abc
+ 0: abc
+ kbc
+ 0: bc
+ kabc
+ 0: abc
+\= Expect no match
+ abk
+No match
+ akb
+No match
+ akk
+No match
+
+/^\d{8,}\@.+[^k]$/
+ 12345678\@a.b.c.d
+ 0: 12345678@a.b.c.d
+ 123456789\@x.y.z
+ 0: 123456789@x.y.z
+\= Expect no match
+ 12345678\@x.y.uk
+No match
+ 1234567\@a.b.c.d
+No match
+
+/[^a]/
+ aaaabcd
+ 0: b
+ aaAabcd
+ 0: A
+
+/[^a]/i
+ aaaabcd
+ 0: b
+ aaAabcd
+ 0: b
+
+/[^az]/
+ aaaabcd
+ 0: b
+ aaAabcd
+ 0: A
+
+/[^az]/i
+ aaaabcd
+ 0: b
+ aaAabcd
+ 0: b
+
+/\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377/
+ \000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377
+ 0: \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
+
+/P[^*]TAIRE[^*]{1,6}?LL/
+ xxxxxxxxxxxPSTAIREISLLxxxxxxxxx
+ 0: PSTAIREISLL
+
+/P[^*]TAIRE[^*]{1,}?LL/
+ xxxxxxxxxxxPSTAIREISLLxxxxxxxxx
+ 0: PSTAIREISLL
+
+/(\.\d\d[1-9]?)\d+/
+ 1.230003938
+ 0: .230003938
+ 1.875000282
+ 0: .875000282
+ 1.235
+ 0: .235
+
+/(\.\d\d((?=0)|\d(?=\d)))/
+ 1.230003938
+ 0: .230
+ 1: .23
+ 1.875000282
+ 0: .875
+\= Expect no match
+ 1.235
+No match
+
+/a(?)b/
+ ab
+ 0: ab
+
+/\b(foo)\s+(\w+)/i
+ Food is on the foo table
+ 0: foo table
+
+/foo(.*)bar/
+ The food is under the bar in the barn.
+ 0: food is under the bar in the bar
+ 1: food is under the bar
+
+/foo(.*?)bar/
+ The food is under the bar in the barn.
+ 0: food is under the bar in the bar
+ 1: food is under the bar
+
+/(.*)(\d*)/no_auto_possess
+ I have 2 numbers: 53147
+Matched, but offsets vector is too small to show all matches
+ 0: I have 2 numbers: 53147
+ 1: I have 2 numbers: 5314
+ 2: I have 2 numbers: 531
+ 3: I have 2 numbers: 53
+ 4: I have 2 numbers: 5
+ 5: I have 2 numbers:
+ 6: I have 2 numbers:
+ 7: I have 2 numbers
+ 8: I have 2 number
+ 9: I have 2 numbe
+10: I have 2 numb
+11: I have 2 num
+12: I have 2 nu
+13: I have 2 n
+14: I have 2
+
+/(.*)(\d+)/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+ 1: I have 2
+
+/(.*?)(\d*)/no_auto_possess
+ I have 2 numbers: 53147
+Matched, but offsets vector is too small to show all matches
+ 0: I have 2 numbers: 53147
+ 1: I have 2 numbers: 5314
+ 2: I have 2 numbers: 531
+ 3: I have 2 numbers: 53
+ 4: I have 2 numbers: 5
+ 5: I have 2 numbers:
+ 6: I have 2 numbers:
+ 7: I have 2 numbers
+ 8: I have 2 number
+ 9: I have 2 numbe
+10: I have 2 numb
+11: I have 2 num
+12: I have 2 nu
+13: I have 2 n
+14: I have 2
+
+/(.*?)(\d+)/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+ 1: I have 2
+
+/(.*)(\d+)$/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+
+/(.*?)(\d+)$/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+
+/(.*)\b(\d+)$/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+
+/(.*\D)(\d+)$/
+ I have 2 numbers: 53147
+ 0: I have 2 numbers: 53147
+
+/^\D*(?!123)/
+ ABC123
+ 0: AB
+ 1: A
+ 2:
+
+/^(\D*)(?=\d)(?!123)/
+ ABC445
+ 0: ABC
+\= Expect no match
+ ABC123
+No match
+
+/^[W-]46]/
+ W46]789
+ 0: W46]
+ -46]789
+ 0: -46]
+\= Expect no match
+ Wall
+No match
+ Zebra
+No match
+ 42
+No match
+ [abcd]
+No match
+ ]abcd[
+No match
+
+/^[W-\]46]/
+ W46]789
+ 0: W
+ Wall
+ 0: W
+ Zebra
+ 0: Z
+ Xylophone
+ 0: X
+ 42
+ 0: 4
+ [abcd]
+ 0: [
+ ]abcd[
+ 0: ]
+ \\backslash
+ 0: \
+\= Expect no match
+ -46]789
+No match
+ well
+No match
+
+/\d\d\/\d\d\/\d\d\d\d/
+ 01/01/2000
+ 0: 01/01/2000
+
+/word (?:[a-zA-Z0-9]+ ){0,10}otherword/
+ word cat dog elephant mussel cow horse canary baboon snake shark otherword
+ 0: word cat dog elephant mussel cow horse canary baboon snake shark otherword
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark
+No match
+
+/word (?:[a-zA-Z0-9]+ ){0,300}otherword/
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark the quick brown fox and the lazy dog and several other words getting close to thirty by now I hope
+No match
+
+/^(a){0,0}/
+ bcd
+ 0:
+ abc
+ 0:
+ aab
+ 0:
+
+/^(a){0,1}/
+ bcd
+ 0:
+ abc
+ 0: a
+ 1:
+ aab
+ 0: a
+ 1:
+
+/^(a){0,2}/
+ bcd
+ 0:
+ abc
+ 0: a
+ 1:
+ aab
+ 0: aa
+ 1: a
+ 2:
+
+/^(a){0,3}/
+ bcd
+ 0:
+ abc
+ 0: a
+ 1:
+ aab
+ 0: aa
+ 1: a
+ 2:
+ aaa
+ 0: aaa
+ 1: aa
+ 2: a
+ 3:
+
+/^(a){0,}/
+ bcd
+ 0:
+ abc
+ 0: a
+ 1:
+ aab
+ 0: aa
+ 1: a
+ 2:
+ aaa
+ 0: aaa
+ 1: aa
+ 2: a
+ 3:
+ aaaaaaaa
+ 0: aaaaaaaa
+ 1: aaaaaaa
+ 2: aaaaaa
+ 3: aaaaa
+ 4: aaaa
+ 5: aaa
+ 6: aa
+ 7: a
+ 8:
+
+/^(a){1,1}/
+ abc
+ 0: a
+ aab
+ 0: a
+\= Expect no match
+ bcd
+No match
+
+/^(a){1,2}/
+ abc
+ 0: a
+ aab
+ 0: aa
+ 1: a
+\= Expect no match
+ bcd
+No match
+
+/^(a){1,3}/
+ abc
+ 0: a
+ aab
+ 0: aa
+ 1: a
+ aaa
+ 0: aaa
+ 1: aa
+ 2: a
+\= Expect no match
+ bcd
+No match
+
+/^(a){1,}/
+ abc
+ 0: a
+ aab
+ 0: aa
+ 1: a
+ aaa
+ 0: aaa
+ 1: aa
+ 2: a
+ aaaaaaaa
+ 0: aaaaaaaa
+ 1: aaaaaaa
+ 2: aaaaaa
+ 3: aaaaa
+ 4: aaaa
+ 5: aaa
+ 6: aa
+ 7: a
+\= Expect no match
+ bcd
+No match
+
+/.*\.gif/
+ borfle\nbib.gif\nno
+ 0: bib.gif
+
+/.{0,}\.gif/
+ borfle\nbib.gif\nno
+ 0: bib.gif
+
+/.*\.gif/m
+ borfle\nbib.gif\nno
+ 0: bib.gif
+
+/.*\.gif/s
+ borfle\nbib.gif\nno
+ 0: borfle\x0abib.gif
+
+/.*\.gif/ms
+ borfle\nbib.gif\nno
+ 0: borfle\x0abib.gif
+
+/.*$/
+ borfle\nbib.gif\nno
+ 0: no
+
+/.*$/m
+ borfle\nbib.gif\nno
+ 0: borfle
+
+/.*$/s
+ borfle\nbib.gif\nno
+ 0: borfle\x0abib.gif\x0ano
+
+/.*$/ms
+ borfle\nbib.gif\nno
+ 0: borfle\x0abib.gif\x0ano
+ 1: borfle\x0abib.gif
+ 2: borfle
+
+/.*$/
+ borfle\nbib.gif\nno\n
+ 0: no
+
+/.*$/m
+ borfle\nbib.gif\nno\n
+ 0: borfle
+
+/.*$/s
+ borfle\nbib.gif\nno\n
+ 0: borfle\x0abib.gif\x0ano\x0a
+ 1: borfle\x0abib.gif\x0ano
+
+/.*$/ms
+ borfle\nbib.gif\nno\n
+ 0: borfle\x0abib.gif\x0ano\x0a
+ 1: borfle\x0abib.gif\x0ano
+ 2: borfle\x0abib.gif
+ 3: borfle
+
+/(.*X|^B)/
+ abcde\n1234Xyz
+ 0: 1234X
+ BarFoo
+ 0: B
+\= Expect no match
+ abcde\nBar
+No match
+
+/(.*X|^B)/m
+ abcde\n1234Xyz
+ 0: 1234X
+ BarFoo
+ 0: B
+ abcde\nBar
+ 0: B
+
+/(.*X|^B)/s
+ abcde\n1234Xyz
+ 0: abcde\x0a1234X
+ BarFoo
+ 0: B
+\= Expect no match
+ abcde\nBar
+No match
+
+/(.*X|^B)/ms
+ abcde\n1234Xyz
+ 0: abcde\x0a1234X
+ BarFoo
+ 0: B
+ abcde\nBar
+ 0: B
+
+/(?s)(.*X|^B)/
+ abcde\n1234Xyz
+ 0: abcde\x0a1234X
+ BarFoo
+ 0: B
+\= Expect no match
+ abcde\nBar
+No match
+
+/(?s:.*X|^B)/
+ abcde\n1234Xyz
+ 0: abcde\x0a1234X
+ BarFoo
+ 0: B
+\= Expect no match
+ abcde\nBar
+No match
+
+/^.*B/
+\= Expect no match
+ abc\nB
+No match
+
+/(?s)^.*B/
+ abc\nB
+ 0: abc\x0aB
+
+/(?m)^.*B/
+ abc\nB
+ 0: B
+
+/(?ms)^.*B/
+ abc\nB
+ 0: abc\x0aB
+
+/(?ms)^B/
+ abc\nB
+ 0: B
+
+/(?s)B$/
+ B\n
+ 0: B
+
+/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
+ 123456654321
+ 0: 123456654321
+
+/^\d\d\d\d\d\d\d\d\d\d\d\d/
+ 123456654321
+ 0: 123456654321
+
+/^[\d][\d][\d][\d][\d][\d][\d][\d][\d][\d][\d][\d]/
+ 123456654321
+ 0: 123456654321
+
+/^[abc]{12}/
+ abcabcabcabc
+ 0: abcabcabcabc
+
+/^[a-c]{12}/
+ abcabcabcabc
+ 0: abcabcabcabc
+
+/^(a|b|c){12}/
+ abcabcabcabc
+ 0: abcabcabcabc
+
+/^[abcdefghijklmnopqrstuvwxy0123456789]/
+ n
+ 0: n
+\= Expect no match
+ z
+No match
+
+/abcde{0,0}/
+ abcd
+ 0: abcd
+\= Expect no match
+ abce
+No match
+
+/ab[cd]{0,0}e/
+ abe
+ 0: abe
+\= Expect no match
+ abcde
+No match
+
+/ab(c){0,0}d/
+ abd
+ 0: abd
+\= Expect no match
+ abcd
+No match
+
+/a(b*)/
+ a
+ 0: a
+ ab
+ 0: ab
+ abbbb
+ 0: abbbb
+\= Expect no match
+ bbbbb
+No match
+
+/ab\d{0}e/
+ abe
+ 0: abe
+\= Expect no match
+ ab1e
+No match
+
+/"([^\\"]+|\\.)*"/
+ the \"quick\" brown fox
+ 0: "quick"
+ \"the \\\"quick\\\" brown fox\"
+ 0: "the \"quick\" brown fox"
+
+/.*?/g,aftertext
+ abc
+ 0: abc
+ 0+
+ 1: ab
+ 2: a
+ 3:
+ 0:
+ 0+
+
+/\b/g,aftertext
+ abc
+ 0:
+ 0+ abc
+ 0:
+ 0+
+
+/\b/g,aftertext
+ abc
+ 0:
+ 0+ abc
+ 0:
+ 0+
+
+//g
+ abc
+ 0:
+ 0:
+ 0:
+ 0:
+
+/<tr([\w\W\s\d][^<>]{0,})><TD([\w\W\s\d][^<>]{0,})>([\d]{0,}\.)(.*)((<BR>([\w\W\s\d][^<>]{0,})|[\s]{0,}))<\/a><\/TD><TD([\w\W\s\d][^<>]{0,})>([\w\W\s\d][^<>]{0,})<\/TD><TD([\w\W\s\d][^<>]{0,})>([\w\W\s\d][^<>]{0,})<\/TD><\/TR>/is
+ <TR BGCOLOR='#DBE9E9'><TD align=left valign=top>43.<a href='joblist.cfm?JobID=94 6735&Keyword='>Word Processor<BR>(N-1286)</a></TD><TD align=left valign=top>Lega lstaff.com</TD><TD align=left valign=top>CA - Statewide</TD></TR>
+ 0: <TR BGCOLOR='#DBE9E9'><TD align=left valign=top>43.<a href='joblist.cfm?JobID=94 6735&Keyword='>Word Processor<BR>(N-1286)</a></TD><TD align=left valign=top>Lega lstaff.com</TD><TD align=left valign=top>CA - Statewide</TD></TR>
+
+/a[^a]b/
+ acb
+ 0: acb
+ a\nb
+ 0: a\x0ab
+
+/a.b/
+ acb
+ 0: acb
+\= Expect no match
+ a\nb
+No match
+
+/a[^a]b/s
+ acb
+ 0: acb
+ a\nb
+ 0: a\x0ab
+
+/a.b/s
+ acb
+ 0: acb
+ a\nb
+ 0: a\x0ab
+
+/^(b+?|a){1,2}?c/
+ bac
+ 0: bac
+ bbac
+ 0: bbac
+ bbbac
+ 0: bbbac
+ bbbbac
+ 0: bbbbac
+ bbbbbac
+ 0: bbbbbac
+
+/^(b+|a){1,2}?c/
+ bac
+ 0: bac
+ bbac
+ 0: bbac
+ bbbac
+ 0: bbbac
+ bbbbac
+ 0: bbbbac
+ bbbbbac
+ 0: bbbbbac
+
+/(?!\A)x/m
+ a\bx\n
+ 0: x
+\= Expect no match
+ x\nb\n
+No match
+
+/\x0{ab}/
+ \0{ab}
+ 0: \x00{ab}
+
+/(A|B)*?CD/
+ CD
+ 0: CD
+
+/(A|B)*CD/
+ CD
+ 0: CD
+
+/(?<!bar)foo/
+ foo
+ 0: foo
+ catfood
+ 0: foo
+ arfootle
+ 0: foo
+ rfoosh
+ 0: foo
+\= Expect no match
+ barfoo
+No match
+ towbarfoo
+No match
+
+/\w{3}(?<!bar)foo/
+ catfood
+ 0: catfoo
+\= Expect no match
+ foo
+No match
+ barfoo
+No match
+ towbarfoo
+No match
+
+/(?<=(foo)a)bar/
+ fooabar
+ 0: bar
+\= Expect no match
+ bar
+No match
+ foobbar
+No match
+
+/\Aabc\z/m
+ abc
+ 0: abc
+\= Expect no match
+ abc\n
+No match
+ qqq\nabc
+No match
+ abc\nzzz
+No match
+ qqq\nabc\nzzz
+No match
+
+"(?>.*/)foo"
+\= Expect no match
+ /this/is/a/very/long/line/in/deed/with/very/many/slashes/in/it/you/see/
+No match
+
+"(?>.*/)foo"
+ /this/is/a/very/long/line/in/deed/with/very/many/slashes/in/and/foo
+ 0: /this/is/a/very/long/line/in/deed/with/very/many/slashes/in/and/foo
+
+/(?>(\.\d\d[1-9]?))\d+/
+ 1.230003938
+ 0: .230003938
+ 1.875000282
+ 0: .875000282
+\= Expect no match
+ 1.235
+No match
+
+/^((?>\w+)|(?>\s+))*$/
+ now is the time for all good men to come to the aid of the party
+ 0: now is the time for all good men to come to the aid of the party
+\= Expect no match
+ this is not a line with only words and spaces!
+No match
+
+/(\d+)(\w)/
+ 12345a
+ 0: 12345a
+ 1: 12345
+ 2: 1234
+ 3: 123
+ 4: 12
+ 12345+
+ 0: 12345
+ 1: 1234
+ 2: 123
+ 3: 12
+
+/((?>\d+))(\w)/
+ 12345a
+ 0: 12345a
+\= Expect no match
+ 12345+
+No match
+
+/(?>a+)b/
+ aaab
+ 0: aaab
+
+/((?>a+)b)/
+ aaab
+ 0: aaab
+
+/(?>(a+))b/
+ aaab
+ 0: aaab
+
+/(?>b)+/
+ aaabbbccc
+ 0: bbb
+ 1: bb
+ 2: b
+
+/(?>a+|b+|c+)*c/
+ aaabbbbccccd
+ 0: aaabbbbcccc
+ 1: aaabbbbc
+
+/(a+|b+|c+)*c/
+ aaabbbbccccd
+ 0: aaabbbbcccc
+ 1: aaabbbbccc
+ 2: aaabbbbcc
+ 3: aaabbbbc
+
+/((?>[^()]+)|\([^()]*\))+/
+ ((abc(ade)ufh()()x
+ 0: abc(ade)ufh()()x
+ 1: abc(ade)ufh()()
+ 2: abc(ade)ufh()
+ 3: abc(ade)ufh
+ 4: abc(ade)
+ 5: abc
+
+/\(((?>[^()]+)|\([^()]+\))+\)/
+ (abc)
+ 0: (abc)
+ (abc(def)xyz)
+ 0: (abc(def)xyz)
+\= Expect no match
+ ((()aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/a(?-i)b/i
+ ab
+ 0: ab
+ Ab
+ 0: Ab
+\= Expect no match
+ aB
+No match
+ AB
+No match
+
+/(a (?x)b c)d e/
+ a bcd e
+ 0: a bcd e
+\= Expect no match
+ a b cd e
+No match
+ abcd e
+No match
+ a bcde
+No match
+
+/(a b(?x)c d (?-x)e f)/
+ a bcde f
+ 0: a bcde f
+\= Expect no match
+ abcdef
+No match
+
+/(a(?i)b)c/
+ abc
+ 0: abc
+ aBc
+ 0: aBc
+\= Expect no match
+ abC
+No match
+ aBC
+No match
+ Abc
+No match
+ ABc
+No match
+ ABC
+No match
+ AbC
+No match
+
+/a(?i:b)c/
+ abc
+ 0: abc
+ aBc
+ 0: aBc
+\= Expect no match
+ ABC
+No match
+ abC
+No match
+ aBC
+No match
+
+/a(?i:b)*c/
+ aBc
+ 0: aBc
+ aBBc
+ 0: aBBc
+\= Expect no match
+ aBC
+No match
+ aBBC
+No match
+
+/a(?=b(?i)c)\w\wd/
+ abcd
+ 0: abcd
+ abCd
+ 0: abCd
+\= Expect no match
+ aBCd
+No match
+ abcD
+No match
+
+/(?s-i:more.*than).*million/i
+ more than million
+ 0: more than million
+ more than MILLION
+ 0: more than MILLION
+ more \n than Million
+ 0: more \x0a than Million
+\= Expect no match
+ MORE THAN MILLION
+No match
+ more \n than \n million
+No match
+
+/(?:(?s-i)more.*than).*million/i
+ more than million
+ 0: more than million
+ more than MILLION
+ 0: more than MILLION
+ more \n than Million
+ 0: more \x0a than Million
+\= Expect no match
+ MORE THAN MILLION
+No match
+ more \n than \n million
+No match
+
+/(?>a(?i)b+)+c/
+ abc
+ 0: abc
+ aBbc
+ 0: aBbc
+ aBBc
+ 0: aBBc
+\= Expect no match
+ Abc
+No match
+ abAb
+No match
+ abbC
+No match
+
+/(?=a(?i)b)\w\wc/
+ abc
+ 0: abc
+ aBc
+ 0: aBc
+\= Expect no match
+ Ab
+No match
+ abC
+No match
+ aBC
+No match
+
+/(?<=a(?i)b)(\w\w)c/
+ abxxc
+ 0: xxc
+ aBxxc
+ 0: xxc
+\= Expect no match
+ Abxxc
+No match
+ ABxxc
+No match
+ abxxC
+No match
+
+/^(?(?=abc)\w{3}:|\d\d)$/
+ abc:
+ 0: abc:
+ 12
+ 0: 12
+\= Expect no match
+ 123
+No match
+ xyz
+No match
+
+/^(?(?!abc)\d\d|\w{3}:)$/
+ abc:
+ 0: abc:
+ 12
+ 0: 12
+\= Expect no match
+ 123
+No match
+ xyz
+No match
+
+/(?(?<=foo)bar|cat)/
+ foobar
+ 0: bar
+ cat
+ 0: cat
+ fcat
+ 0: cat
+ focat
+ 0: cat
+\= Expect no match
+ foocat
+No match
+
+/(?(?<!foo)cat|bar)/
+ foobar
+ 0: bar
+ cat
+ 0: cat
+ fcat
+ 0: cat
+ focat
+ 0: cat
+\= Expect no match
+ foocat
+No match
+
+/(?>a*)*/
+ a
+ 0: a
+ 1:
+ aa
+ 0: aa
+ 1:
+ aaaa
+ 0: aaaa
+ 1:
+
+/(abc|)+/
+ abc
+ 0: abc
+ 1:
+ abcabc
+ 0: abcabc
+ 1: abc
+ 2:
+ abcabcabc
+ 0: abcabcabc
+ 1: abcabc
+ 2: abc
+ 3:
+ xyz
+ 0:
+
+/([a]*)*/
+ a
+ 0: a
+ 1:
+ aaaaa
+ 0: aaaaa
+ 1: aaaa
+ 2: aaa
+ 3: aa
+ 4: a
+ 5:
+
+/([ab]*)*/
+ a
+ 0: a
+ 1:
+ b
+ 0: b
+ 1:
+ ababab
+ 0: ababab
+ 1: ababa
+ 2: abab
+ 3: aba
+ 4: ab
+ 5: a
+ 6:
+ aaaabcde
+ 0: aaaab
+ 1: aaaa
+ 2: aaa
+ 3: aa
+ 4: a
+ 5:
+ bbbb
+ 0: bbbb
+ 1: bbb
+ 2: bb
+ 3: b
+ 4:
+
+/([^a]*)*/
+ b
+ 0: b
+ 1:
+ bbbb
+ 0: bbbb
+ 1: bbb
+ 2: bb
+ 3: b
+ 4:
+ aaa
+ 0:
+
+/([^ab]*)*/
+ cccc
+ 0: cccc
+ 1: ccc
+ 2: cc
+ 3: c
+ 4:
+ abab
+ 0:
+
+/([a]*?)*/
+ a
+ 0: a
+ 1:
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ 4:
+
+/([ab]*?)*/
+ a
+ 0: a
+ 1:
+ b
+ 0: b
+ 1:
+ abab
+ 0: abab
+ 1: aba
+ 2: ab
+ 3: a
+ 4:
+ baba
+ 0: baba
+ 1: bab
+ 2: ba
+ 3: b
+ 4:
+
+/([^a]*?)*/
+ b
+ 0: b
+ 1:
+ bbbb
+ 0: bbbb
+ 1: bbb
+ 2: bb
+ 3: b
+ 4:
+ aaa
+ 0:
+
+/([^ab]*?)*/
+ c
+ 0: c
+ 1:
+ cccc
+ 0: cccc
+ 1: ccc
+ 2: cc
+ 3: c
+ 4:
+ baba
+ 0:
+
+/(?>a*)*/
+ a
+ 0: a
+ 1:
+ aaabcde
+ 0: aaa
+ 1:
+
+/((?>a*))*/
+ aaaaa
+ 0: aaaaa
+ 1:
+ aabbaa
+ 0: aa
+ 1:
+
+/((?>a*?))*/
+ aaaaa
+ 0: aaaaa
+ 1:
+ aabbaa
+ 0: aa
+ 1:
+
+/(?(?=[^a-z]+[a-z]) \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) /x
+ 12-sep-98
+ 0: 12-sep-98
+ 12-09-98
+ 0: 12-09-98
+\= Expect no match
+ sep-12-98
+No match
+
+/(?i:saturday|sunday)/
+ saturday
+ 0: saturday
+ sunday
+ 0: sunday
+ Saturday
+ 0: Saturday
+ Sunday
+ 0: Sunday
+ SATURDAY
+ 0: SATURDAY
+ SUNDAY
+ 0: SUNDAY
+ SunDay
+ 0: SunDay
+
+/(a(?i)bc|BB)x/
+ abcx
+ 0: abcx
+ aBCx
+ 0: aBCx
+ bbx
+ 0: bbx
+ BBx
+ 0: BBx
+\= Expect no match
+ abcX
+No match
+ aBCX
+No match
+ bbX
+No match
+ BBX
+No match
+
+/^([ab](?i)[cd]|[ef])/
+ ac
+ 0: ac
+ aC
+ 0: aC
+ bD
+ 0: bD
+ elephant
+ 0: e
+ Europe
+ 0: E
+ frog
+ 0: f
+ France
+ 0: F
+\= Expect no match
+ Africa
+No match
+
+/^(ab|a(?i)[b-c](?m-i)d|x(?i)y|z)/
+ ab
+ 0: ab
+ aBd
+ 0: aBd
+ xy
+ 0: xy
+ xY
+ 0: xY
+ zebra
+ 0: z
+ Zambesi
+ 0: Z
+\= Expect no match
+ aCD
+No match
+ XY
+No match
+
+/(?<=foo\n)^bar/m
+ foo\nbar
+ 0: bar
+\= Expect no match
+ bar
+No match
+ baz\nbar
+No match
+
+/(?<=(?<!foo)bar)baz/
+ barbaz
+ 0: baz
+ barbarbaz
+ 0: baz
+ koobarbaz
+ 0: baz
+\= Expect no match
+ baz
+No match
+ foobarbaz
+No match
+
+# The following tests are taken from the Perl 5.005 test suite; some of them
+# are compatible with 5.004, but I'd rather not have to sort them out.
+
+/abc/
+ abc
+ 0: abc
+ xabcy
+ 0: abc
+ ababc
+ 0: abc
+\= Expect no match
+ xbc
+No match
+ axc
+No match
+ abx
+No match
+
+/ab*c/
+ abc
+ 0: abc
+
+/ab*bc/
+ abc
+ 0: abc
+ abbc
+ 0: abbc
+ abbbbc
+ 0: abbbbc
+
+/.{1}/
+ abbbbc
+ 0: a
+
+/.{3,4}/
+ abbbbc
+ 0: abbb
+
+/ab{0,}bc/
+ abbbbc
+ 0: abbbbc
+
+/ab+bc/
+ abbc
+ 0: abbc
+\= Expect no match
+ abc
+No match
+ abq
+No match
+
+/ab+bc/
+ abbbbc
+ 0: abbbbc
+
+/ab{1,}bc/
+ abbbbc
+ 0: abbbbc
+
+/ab{1,3}bc/
+ abbbbc
+ 0: abbbbc
+
+/ab{3,4}bc/
+ abbbbc
+ 0: abbbbc
+
+/ab{4,5}bc/
+\= Expect no match
+ abq
+No match
+ abbbbc
+No match
+
+/ab?bc/
+ abbc
+ 0: abbc
+ abc
+ 0: abc
+
+/ab{0,1}bc/
+ abc
+ 0: abc
+
+/ab?bc/
+
+/ab?c/
+ abc
+ 0: abc
+
+/ab{0,1}c/
+ abc
+ 0: abc
+
+/^abc$/
+ abc
+ 0: abc
+\= Expect no match
+ abbbbc
+No match
+ abcc
+No match
+
+/^abc/
+ abcc
+ 0: abc
+
+/^abc$/
+
+/abc$/
+ aabc
+ 0: abc
+ aabc
+ 0: abc
+\= Expect no match
+ aabcd
+No match
+
+/^/
+ abc
+ 0:
+
+/$/
+ abc
+ 0:
+
+/a.c/
+ abc
+ 0: abc
+ axc
+ 0: axc
+
+/a.*c/
+ axyzc
+ 0: axyzc
+
+/a[bc]d/
+ abd
+ 0: abd
+\= Expect no match
+ axyzd
+No match
+ abc
+No match
+
+/a[b-d]e/
+ ace
+ 0: ace
+
+/a[b-d]/
+ aac
+ 0: ac
+
+/a[-b]/
+ a-
+ 0: a-
+
+/a[b-]/
+ a-
+ 0: a-
+
+/a]/
+ a]
+ 0: a]
+
+/a[]]b/
+ a]b
+ 0: a]b
+
+/a[^bc]d/
+ aed
+ 0: aed
+\= Expect no match
+ abd
+No match
+ abd
+No match
+
+/a[^-b]c/
+ adc
+ 0: adc
+
+/a[^]b]c/
+ adc
+ 0: adc
+ a-c
+ 0: a-c
+\= Expect no match
+ a]c
+No match
+
+/\ba\b/
+ a-
+ 0: a
+ -a
+ 0: a
+ -a-
+ 0: a
+
+/\by\b/
+\= Expect no match
+ xy
+No match
+ yz
+No match
+ xyz
+No match
+
+/\Ba\B/
+\= Expect no match
+ a-
+No match
+ -a
+No match
+ -a-
+No match
+
+/\By\b/
+ xy
+ 0: y
+
+/\by\B/
+ yz
+ 0: y
+
+/\By\B/
+ xyz
+ 0: y
+
+/\w/
+ a
+ 0: a
+
+/\W/
+ -
+ 0: -
+\= Expect no match
+ a
+No match
+
+/a\sb/
+ a b
+ 0: a b
+
+/a\Sb/
+ a-b
+ 0: a-b
+\= Expect no match
+ a b
+No match
+
+/\d/
+ 1
+ 0: 1
+
+/\D/
+ -
+ 0: -
+\= Expect no match
+ 1
+No match
+
+/[\w]/
+ a
+ 0: a
+
+/[\W]/
+ -
+ 0: -
+\= Expect no match
+ a
+No match
+
+/a[\s]b/
+ a b
+ 0: a b
+
+/a[\S]b/
+ a-b
+ 0: a-b
+\= Expect no match
+ a b
+No match
+
+/[\d]/
+ 1
+ 0: 1
+
+/[\D]/
+ -
+ 0: -
+\= Expect no match
+ 1
+No match
+
+/ab|cd/
+ abc
+ 0: ab
+ abcd
+ 0: ab
+
+/()ef/
+ def
+ 0: ef
+
+/$b/
+
+/a\(b/
+ a(b
+ 0: a(b
+
+/a\(*b/
+ ab
+ 0: ab
+ a((b
+ 0: a((b
+
+/a\\b/
+ a\\b
+ 0: a\b
+\= Expect no match
+ a\b
+No match
+
+/((a))/
+ abc
+ 0: a
+
+/(a)b(c)/
+ abc
+ 0: abc
+
+/a+b+c/
+ aabbabc
+ 0: abc
+
+/a{1,}b{1,}c/
+ aabbabc
+ 0: abc
+
+/a.+?c/
+ abcabc
+ 0: abcabc
+ 1: abc
+
+/(a+|b)*/
+ ab
+ 0: ab
+ 1: a
+ 2:
+
+/(a+|b){0,}/
+ ab
+ 0: ab
+ 1: a
+ 2:
+
+/(a+|b)+/
+ ab
+ 0: ab
+ 1: a
+
+/(a+|b){1,}/
+ ab
+ 0: ab
+ 1: a
+
+/(a+|b)?/
+ ab
+ 0: a
+ 1:
+
+/(a+|b){0,1}/
+ ab
+ 0: a
+ 1:
+
+/[^ab]*/
+ cde
+ 0: cde
+
+/abc/
+\= Expect no match
+ b
+No match
+
+/a*/
+
+/([abc])*d/
+ abbbcd
+ 0: abbbcd
+
+/([abc])*bcd/
+ abcd
+ 0: abcd
+
+/a|b|c|d|e/
+ e
+ 0: e
+
+/(a|b|c|d|e)f/
+ ef
+ 0: ef
+
+/abcd*efg/
+ abcdefg
+ 0: abcdefg
+
+/ab*/
+ xabyabbbz
+ 0: ab
+ xayabbbz
+ 0: a
+
+/(ab|cd)e/
+ abcde
+ 0: cde
+
+/[abhgefdc]ij/
+ hij
+ 0: hij
+
+/^(ab|cd)e/
+
+/(abc|)ef/
+ abcdef
+ 0: ef
+
+/(a|b)c*d/
+ abcd
+ 0: bcd
+
+/(ab|ab*)bc/
+ abc
+ 0: abc
+
+/a([bc]*)c*/
+ abc
+ 0: abc
+ 1: a
+
+/a([bc]*)(c*d)/
+ abcd
+ 0: abcd
+
+/a([bc]+)(c*d)/
+ abcd
+ 0: abcd
+
+/a([bc]*)(c+d)/
+ abcd
+ 0: abcd
+
+/a[bcd]*dcdcde/
+ adcdcde
+ 0: adcdcde
+
+/a[bcd]+dcdcde/
+\= Expect no match
+ abcde
+No match
+ adcdcde
+No match
+
+/(ab|a)b*c/
+ abc
+ 0: abc
+
+/((a)(b)c)(d)/
+ abcd
+ 0: abcd
+
+/[a-zA-Z_][a-zA-Z0-9_]*/
+ alpha
+ 0: alpha
+
+/^a(bc+|b[eh])g|.h$/
+ abh
+ 0: bh
+
+/(bc+d$|ef*g.|h?i(j|k))/
+ effgz
+ 0: effgz
+ ij
+ 0: ij
+ reffgz
+ 0: effgz
+\= Expect no match
+ effg
+No match
+ bcdd
+No match
+
+/((((((((((a))))))))))/
+ a
+ 0: a
+
+/(((((((((a)))))))))/
+ a
+ 0: a
+
+/multiple words of text/
+\= Expect no match
+ aa
+No match
+ uh-uh
+No match
+
+/multiple words/
+ multiple words, yeah
+ 0: multiple words
+
+/(.*)c(.*)/
+ abcde
+ 0: abcde
+
+/\((.*), (.*)\)/
+ (a, b)
+ 0: (a, b)
+
+/[k]/
+
+/abcd/
+ abcd
+ 0: abcd
+
+/a(bc)d/
+ abcd
+ 0: abcd
+
+/a[-]?c/
+ ac
+ 0: ac
+
+/abc/i
+ ABC
+ 0: ABC
+ XABCY
+ 0: ABC
+ ABABC
+ 0: ABC
+\= Expect no match
+ aaxabxbaxbbx
+No match
+ XBC
+No match
+ AXC
+No match
+ ABX
+No match
+
+/ab*c/i
+ ABC
+ 0: ABC
+
+/ab*bc/i
+ ABC
+ 0: ABC
+ ABBC
+ 0: ABBC
+
+/ab*?bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab{0,}?bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab+?bc/i
+ ABBC
+ 0: ABBC
+
+/ab+bc/i
+\= Expect no match
+ ABC
+No match
+ ABQ
+No match
+
+/ab{1,}bc/i
+
+/ab+bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab{1,}?bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab{1,3}?bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab{3,4}?bc/i
+ ABBBBC
+ 0: ABBBBC
+
+/ab{4,5}?bc/i
+\= Expect no match
+ ABQ
+No match
+ ABBBBC
+No match
+
+/ab??bc/i
+ ABBC
+ 0: ABBC
+ ABC
+ 0: ABC
+
+/ab{0,1}?bc/i
+ ABC
+ 0: ABC
+
+/ab??bc/i
+
+/ab??c/i
+ ABC
+ 0: ABC
+
+/ab{0,1}?c/i
+ ABC
+ 0: ABC
+
+/^abc$/i
+ ABC
+ 0: ABC
+\= Expect no match
+ ABBBBC
+No match
+ ABCC
+No match
+
+/^abc/i
+ ABCC
+ 0: ABC
+
+/^abc$/i
+
+/abc$/i
+ AABC
+ 0: ABC
+
+/^/i
+ ABC
+ 0:
+
+/$/i
+ ABC
+ 0:
+
+/a.c/i
+ ABC
+ 0: ABC
+ AXC
+ 0: AXC
+
+/a.*?c/i
+ AXYZC
+ 0: AXYZC
+
+/a.*c/i
+ AABC
+ 0: AABC
+\= Expect no match
+ AXYZD
+No match
+
+/a[bc]d/i
+ ABD
+ 0: ABD
+
+/a[b-d]e/i
+ ACE
+ 0: ACE
+\= Expect no match
+ ABC
+No match
+ ABD
+No match
+
+/a[b-d]/i
+ AAC
+ 0: AC
+
+/a[-b]/i
+ A-
+ 0: A-
+
+/a[b-]/i
+ A-
+ 0: A-
+
+/a]/i
+ A]
+ 0: A]
+
+/a[]]b/i
+ A]B
+ 0: A]B
+
+/a[^bc]d/i
+ AED
+ 0: AED
+
+/a[^-b]c/i
+ ADC
+ 0: ADC
+\= Expect no match
+ ABD
+No match
+ A-C
+No match
+
+/a[^]b]c/i
+ ADC
+ 0: ADC
+
+/ab|cd/i
+ ABC
+ 0: AB
+ ABCD
+ 0: AB
+
+/()ef/i
+ DEF
+ 0: EF
+
+/$b/i
+\= Expect no match
+ A]C
+No match
+ B
+No match
+
+/a\(b/i
+ A(B
+ 0: A(B
+
+/a\(*b/i
+ AB
+ 0: AB
+ A((B
+ 0: A((B
+
+/a\\b/i
+\= Expect no match
+ A\=notbol
+No match
+
+/((a))/i
+ ABC
+ 0: A
+
+/(a)b(c)/i
+ ABC
+ 0: ABC
+
+/a+b+c/i
+ AABBABC
+ 0: ABC
+
+/a{1,}b{1,}c/i
+ AABBABC
+ 0: ABC
+
+/a.+?c/i
+ ABCABC
+ 0: ABCABC
+ 1: ABC
+
+/a.*?c/i
+ ABCABC
+ 0: ABCABC
+ 1: ABC
+
+/a.{0,5}?c/i
+ ABCABC
+ 0: ABCABC
+ 1: ABC
+
+/(a+|b)*/i
+ AB
+ 0: AB
+ 1: A
+ 2:
+
+/(a+|b){0,}/i
+ AB
+ 0: AB
+ 1: A
+ 2:
+
+/(a+|b)+/i
+ AB
+ 0: AB
+ 1: A
+
+/(a+|b){1,}/i
+ AB
+ 0: AB
+ 1: A
+
+/(a+|b)?/i
+ AB
+ 0: A
+ 1:
+
+/(a+|b){0,1}/i
+ AB
+ 0: A
+ 1:
+
+/(a+|b){0,1}?/i
+ AB
+ 0: A
+ 1:
+
+/[^ab]*/i
+ CDE
+ 0: CDE
+
+/abc/i
+
+/a*/i
+
+/([abc])*d/i
+ ABBBCD
+ 0: ABBBCD
+
+/([abc])*bcd/i
+ ABCD
+ 0: ABCD
+
+/a|b|c|d|e/i
+ E
+ 0: E
+
+/(a|b|c|d|e)f/i
+ EF
+ 0: EF
+
+/abcd*efg/i
+ ABCDEFG
+ 0: ABCDEFG
+
+/ab*/i
+ XABYABBBZ
+ 0: AB
+ XAYABBBZ
+ 0: A
+
+/(ab|cd)e/i
+ ABCDE
+ 0: CDE
+
+/[abhgefdc]ij/i
+ HIJ
+ 0: HIJ
+
+/^(ab|cd)e/i
+\= Expect no match
+ ABCDE
+No match
+
+/(abc|)ef/i
+ ABCDEF
+ 0: EF
+
+/(a|b)c*d/i
+ ABCD
+ 0: BCD
+
+/(ab|ab*)bc/i
+ ABC
+ 0: ABC
+
+/a([bc]*)c*/i
+ ABC
+ 0: ABC
+ 1: A
+
+/a([bc]*)(c*d)/i
+ ABCD
+ 0: ABCD
+
+/a([bc]+)(c*d)/i
+ ABCD
+ 0: ABCD
+
+/a([bc]*)(c+d)/i
+ ABCD
+ 0: ABCD
+
+/a[bcd]*dcdcde/i
+ ADCDCDE
+ 0: ADCDCDE
+
+/a[bcd]+dcdcde/i
+
+/(ab|a)b*c/i
+ ABC
+ 0: ABC
+
+/((a)(b)c)(d)/i
+ ABCD
+ 0: ABCD
+
+/[a-zA-Z_][a-zA-Z0-9_]*/i
+ ALPHA
+ 0: ALPHA
+
+/^a(bc+|b[eh])g|.h$/i
+ ABH
+ 0: BH
+
+/(bc+d$|ef*g.|h?i(j|k))/i
+ EFFGZ
+ 0: EFFGZ
+ IJ
+ 0: IJ
+ REFFGZ
+ 0: EFFGZ
+\= Expect no match
+ ADCDCDE
+No match
+ EFFG
+No match
+ BCDD
+No match
+
+/((((((((((a))))))))))/i
+ A
+ 0: A
+
+/(((((((((a)))))))))/i
+ A
+ 0: A
+
+/(?:(?:(?:(?:(?:(?:(?:(?:(?:(a))))))))))/i
+ A
+ 0: A
+
+/(?:(?:(?:(?:(?:(?:(?:(?:(?:(a|b|c))))))))))/i
+ C
+ 0: C
+
+/multiple words of text/i
+\= Expect no match
+ AA
+No match
+ UH-UH
+No match
+
+/multiple words/i
+ MULTIPLE WORDS, YEAH
+ 0: MULTIPLE WORDS
+
+/(.*)c(.*)/i
+ ABCDE
+ 0: ABCDE
+
+/\((.*), (.*)\)/i
+ (A, B)
+ 0: (A, B)
+
+/[k]/i
+
+/abcd/i
+ ABCD
+ 0: ABCD
+
+/a(bc)d/i
+ ABCD
+ 0: ABCD
+
+/a[-]?c/i
+ AC
+ 0: AC
+
+/a(?!b)./
+ abad
+ 0: ad
+
+/a(?=d)./
+ abad
+ 0: ad
+
+/a(?=c|d)./
+ abad
+ 0: ad
+
+/a(?:b|c|d)(.)/
+ ace
+ 0: ace
+
+/a(?:b|c|d)*(.)/
+ ace
+ 0: ace
+ 1: ac
+
+/a(?:b|c|d)+?(.)/
+ ace
+ 0: ace
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+ 2: acdbcd
+ 3: acdbc
+ 4: acdb
+ 5: acd
+
+/a(?:b|c|d)+(.)/
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+ 2: acdbcd
+ 3: acdbc
+ 4: acdb
+ 5: acd
+
+/a(?:b|c|d){2}(.)/
+ acdbcdbe
+ 0: acdb
+
+/a(?:b|c|d){4,5}(.)/
+ acdbcdbe
+ 0: acdbcdb
+ 1: acdbcd
+
+/a(?:b|c|d){4,5}?(.)/
+ acdbcdbe
+ 0: acdbcdb
+ 1: acdbcd
+
+/((foo)|(bar))*/
+ foobar
+ 0: foobar
+ 1: foo
+ 2:
+
+/a(?:b|c|d){6,7}(.)/
+ acdbcdbe
+ 0: acdbcdbe
+
+/a(?:b|c|d){6,7}?(.)/
+ acdbcdbe
+ 0: acdbcdbe
+
+/a(?:b|c|d){5,6}(.)/
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+
+/a(?:b|c|d){5,6}?(.)/
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+
+/a(?:b|c|d){5,7}(.)/
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+
+/a(?:b|c|d){5,7}?(.)/
+ acdbcdbe
+ 0: acdbcdbe
+ 1: acdbcdb
+
+/a(?:b|(c|e){1,2}?|d)+?(.)/
+ ace
+ 0: ace
+
+/^(.+)?B/
+ AB
+ 0: AB
+
+/^([^a-z])|(\^)$/
+ .
+ 0: .
+
+/^[<>]&/
+ <&OUT
+ 0: <&
+
+/(?:(f)(o)(o)|(b)(a)(r))*/
+ foobar
+ 0: foobar
+ 1: foo
+ 2:
+
+/(?<=a)b/
+ ab
+ 0: b
+\= Expect no match
+ cb
+No match
+ b
+No match
+
+/(?<!c)b/
+ ab
+ 0: b
+ b
+ 0: b
+ b
+ 0: b
+
+/(?:..)*a/
+ aba
+ 0: aba
+ 1: a
+
+/(?:..)*?a/
+ aba
+ 0: aba
+ 1: a
+
+/^(){3,5}/
+ abc
+ 0:
+
+/^(a+)*ax/
+ aax
+ 0: aax
+
+/^((a|b)+)*ax/
+ aax
+ 0: aax
+
+/^((a|bc)+)*ax/
+ aax
+ 0: aax
+
+/(a|x)*ab/
+ cab
+ 0: ab
+
+/(a)*ab/
+ cab
+ 0: ab
+
+/(?:(?i)a)b/
+ ab
+ 0: ab
+
+/((?i)a)b/
+ ab
+ 0: ab
+
+/(?:(?i)a)b/
+ Ab
+ 0: Ab
+
+/((?i)a)b/
+ Ab
+ 0: Ab
+
+/(?:(?i)a)b/
+\= Expect no match
+ cb
+No match
+ aB
+No match
+
+/((?i)a)b/
+
+/(?i:a)b/
+ ab
+ 0: ab
+
+/((?i:a))b/
+ ab
+ 0: ab
+
+/(?i:a)b/
+ Ab
+ 0: Ab
+
+/((?i:a))b/
+ Ab
+ 0: Ab
+
+/(?i:a)b/
+\= Expect no match
+ aB
+No match
+ aB
+No match
+
+/((?i:a))b/
+
+/(?:(?-i)a)b/i
+ ab
+ 0: ab
+
+/((?-i)a)b/i
+ ab
+ 0: ab
+
+/(?:(?-i)a)b/i
+ aB
+ 0: aB
+
+/((?-i)a)b/i
+ aB
+ 0: aB
+
+/(?:(?-i)a)b/i
+ aB
+ 0: aB
+\= Expect no match
+ Ab
+No match
+
+/((?-i)a)b/i
+
+/(?:(?-i)a)b/i
+ aB
+ 0: aB
+
+/((?-i)a)b/i
+ aB
+ 0: aB
+
+/(?:(?-i)a)b/i
+\= Expect no match
+ Ab
+No match
+ AB
+No match
+
+/((?-i)a)b/i
+
+/(?-i:a)b/i
+ ab
+ 0: ab
+
+/((?-i:a))b/i
+ ab
+ 0: ab
+
+/(?-i:a)b/i
+ aB
+ 0: aB
+
+/((?-i:a))b/i
+ aB
+ 0: aB
+
+/(?-i:a)b/i
+\= Expect no match
+ AB
+No match
+ Ab
+No match
+
+/((?-i:a))b/i
+
+/(?-i:a)b/i
+ aB
+ 0: aB
+
+/((?-i:a))b/i
+ aB
+ 0: aB
+
+/(?-i:a)b/i
+\= Expect no match
+ Ab
+No match
+ AB
+No match
+
+/((?-i:a))b/i
+
+/((?-i:a.))b/i
+\= Expect no match
+ AB
+No match
+ a\nB
+No match
+
+/((?s-i:a.))b/i
+ a\nB
+ 0: a\x0aB
+
+/(?:c|d)(?:)(?:a(?:)(?:b)(?:b(?:))(?:b(?:)(?:b)))/
+ cabbbb
+ 0: cabbbb
+
+/(?:c|d)(?:)(?:aaaaaaaa(?:)(?:bbbbbbbb)(?:bbbbbbbb(?:))(?:bbbbbbbb(?:)(?:bbbbbbbb)))/
+ caaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+ 0: caaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+
+/foo\w*\d{4}baz/
+ foobar1234baz
+ 0: foobar1234baz
+
+/x(~~)*(?:(?:F)?)?/
+ x~~
+ 0: x~~
+ 1: x
+
+/^a(?#xxx){3}c/
+ aaac
+ 0: aaac
+
+/^a (?#xxx) (?#yyy) {3}c/x
+ aaac
+ 0: aaac
+
+/(?<![cd])b/
+\= Expect no match
+ B\nB
+No match
+ dbcb
+No match
+
+/(?<![cd])[ab]/
+ dbaacb
+ 0: a
+
+/(?<!(c|d))b/
+
+/(?<!(c|d))[ab]/
+ dbaacb
+ 0: a
+
+/(?<!cd)[ab]/
+ cdaccb
+ 0: b
+
+/^(?:a?b?)*$/
+\= Expect no match
+ dbcb
+No match
+ a--
+No match
+
+/((?s)^a(.))((?m)^b$)/
+ a\nb\nc\n
+ 0: a\x0ab
+
+/((?m)^b$)/
+ a\nb\nc\n
+ 0: b
+
+/(?m)^b/
+ a\nb\n
+ 0: b
+
+/(?m)^(b)/
+ a\nb\n
+ 0: b
+
+/((?m)^b)/
+ a\nb\n
+ 0: b
+
+/\n((?m)^b)/
+ a\nb\n
+ 0: \x0ab
+
+/((?s).)c(?!.)/
+ a\nb\nc\n
+ 0: \x0ac
+ a\nb\nc\n
+ 0: \x0ac
+
+/((?s)b.)c(?!.)/
+ a\nb\nc\n
+ 0: b\x0ac
+ a\nb\nc\n
+ 0: b\x0ac
+
+/^b/
+
+/()^b/
+\= Expect no match
+ a\nb\nc\n
+No match
+ a\nb\nc\n
+No match
+
+/((?m)^b)/
+ a\nb\nc\n
+ 0: b
+
+/(?(?!a)a|b)/
+
+/(?(?!a)b|a)/
+ a
+ 0: a
+
+/(?(?=a)b|a)/
+\= Expect no match
+ a
+No match
+ a
+No match
+
+/(?(?=a)a|b)/
+ a
+ 0: a
+
+/(\w+:)+/
+ one:
+ 0: one:
+
+/$(?<=^(a))/
+ a
+ 0:
+
+/([\w:]+::)?(\w+)$/
+ abcd
+ 0: abcd
+ xy:z:::abcd
+ 0: xy:z:::abcd
+
+/^[^bcd]*(c+)/
+ aexycd
+ 0: aexyc
+
+/(a*)b+/
+ caab
+ 0: aab
+
+/([\w:]+::)?(\w+)$/
+ abcd
+ 0: abcd
+ xy:z:::abcd
+ 0: xy:z:::abcd
+\= Expect no match
+ abcd:
+No match
+ abcd:
+No match
+
+/^[^bcd]*(c+)/
+ aexycd
+ 0: aexyc
+
+/(>a+)ab/
+
+/(?>a+)b/
+ aaab
+ 0: aaab
+
+/([[:]+)/
+ a:[b]:
+ 0: :[
+
+/([[=]+)/
+ a=[b]=
+ 0: =[
+
+/([[.]+)/
+ a.[b].
+ 0: .[
+
+/((?>a+)b)/
+ aaab
+ 0: aaab
+
+/(?>(a+))b/
+ aaab
+ 0: aaab
+
+/((?>[^()]+)|\([^()]*\))+/
+ ((abc(ade)ufh()()x
+ 0: abc(ade)ufh()()x
+ 1: abc(ade)ufh()()
+ 2: abc(ade)ufh()
+ 3: abc(ade)ufh
+ 4: abc(ade)
+ 5: abc
+
+/a\Z/
+\= Expect no match
+ aaab
+No match
+ a\nb\n
+No match
+
+/b\Z/
+ a\nb\n
+ 0: b
+
+/b\z/
+
+/b\Z/
+ a\nb
+ 0: b
+
+/b\z/
+ a\nb
+ 0: b
+
+/(?>.*)(?<=(abcd|wxyz))/
+ alphabetabcd
+ 0: alphabetabcd
+ endingwxyz
+ 0: endingwxyz
+\= Expect no match
+ a rather long string that doesn't end with one of them
+No match
+
+/word (?>(?:(?!otherword)[a-zA-Z0-9]+ ){0,30})otherword/
+ word cat dog elephant mussel cow horse canary baboon snake shark otherword
+ 0: word cat dog elephant mussel cow horse canary baboon snake shark otherword
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark
+No match
+
+/word (?>[a-zA-Z0-9]+ ){0,30}otherword/
+\= Expect no match
+ word cat dog elephant mussel cow horse canary baboon snake shark the quick brown fox and the lazy dog and several other words getting close to thirty by now I hope
+No match
+
+/(?<=\d{3}(?!999))foo/
+ 999foo
+ 0: foo
+ 123999foo
+ 0: foo
+\= Expect no match
+ 123abcfoo
+No match
+
+/(?<=(?!...999)\d{3})foo/
+ 999foo
+ 0: foo
+ 123999foo
+ 0: foo
+\= Expect no match
+ 123abcfoo
+No match
+
+/(?<=\d{3}(?!999)...)foo/
+ 123abcfoo
+ 0: foo
+ 123456foo
+ 0: foo
+\= Expect no match
+ 123999foo
+No match
+
+/(?<=\d{3}...)(?<!999)foo/
+ 123abcfoo
+ 0: foo
+ 123456foo
+ 0: foo
+\= Expect no match
+ 123999foo
+No match
+
+/((Z)+|A)*/
+ ZABCDEFG
+ 0: ZA
+ 1: Z
+ 2:
+
+/(Z()|A)*/
+ ZABCDEFG
+ 0: ZA
+ 1: Z
+ 2:
+
+/(Z(())|A)*/
+ ZABCDEFG
+ 0: ZA
+ 1: Z
+ 2:
+
+/((?>Z)+|A)*/
+ ZABCDEFG
+ 0: ZA
+ 1: Z
+ 2:
+
+/((?>)+|A)*/
+ ZABCDEFG
+ 0:
+
+/a*/g
+ abbab
+ 0: a
+ 0:
+ 0:
+ 0: a
+ 0:
+ 0:
+
+/[[:space:]]+/
+ > \x09\x0a\x0c\x0d\x0b<
+ 0: \x09\x0a\x0c\x0d\x0b
+
+/[[:blank:]]+/
+ > \x09\x0a\x0c\x0d\x0b<
+ 0: \x09
+
+/[\s]+/
+ > \x09\x0a\x0c\x0d\x0b<
+ 0: \x09\x0a\x0c\x0d\x0b
+
+/\s+/
+ > \x09\x0a\x0c\x0d\x0b<
+ 0: \x09\x0a\x0c\x0d\x0b
+
+/a b/x
+ ab
+ 0: ab
+
+/(?!\A)x/m
+ a\nxb\n
+ 0: x
+
+/(?!^)x/m
+\= Expect no match
+ a\nxb\n
+No match
+
+/abc\Qabc\Eabc/
+ abcabcabc
+ 0: abcabcabc
+
+/abc\Q(*+|\Eabc/
+ abc(*+|abc
+ 0: abc(*+|abc
+
+/ abc\Q abc\Eabc/x
+ abc abcabc
+ 0: abc abcabc
+\= Expect no match
+ abcabcabc
+No match
+
+/abc#comment
+ \Q#not comment
+ literal\E/x
+ abc#not comment\n literal
+ 0: abc#not comment\x0a literal
+
+/abc#comment
+ \Q#not comment
+ literal/x
+ abc#not comment\n literal
+ 0: abc#not comment\x0a literal
+
+/abc#comment
+ \Q#not comment
+ literal\E #more comment
+ /x
+ abc#not comment\n literal
+ 0: abc#not comment\x0a literal
+
+/abc#comment
+ \Q#not comment
+ literal\E #more comment/x
+ abc#not comment\n literal
+ 0: abc#not comment\x0a literal
+
+/\Qabc\$xyz\E/
+ abc\\\$xyz
+ 0: abc\$xyz
+
+/\Qabc\E\$\Qxyz\E/
+ abc\$xyz
+ 0: abc$xyz
+
+/\Gabc/
+ abc
+ 0: abc
+\= Expect no match
+ xyzabc
+No match
+
+/\Gabc./g
+ abc1abc2xyzabc3
+ 0: abc1
+ 0: abc2
+
+/abc./g
+ abc1abc2xyzabc3
+ 0: abc1
+ 0: abc2
+ 0: abc3
+
+/a(?x: b c )d/
+ XabcdY
+ 0: abcd
+\= Expect no match
+ Xa b c d Y
+No match
+
+/((?x)x y z | a b c)/
+ XabcY
+ 0: abc
+ AxyzB
+ 0: xyz
+
+/(?i)AB(?-i)C/
+ XabCY
+ 0: abC
+\= Expect no match
+ XabcY
+No match
+
+/((?i)AB(?-i)C|D)E/
+ abCE
+ 0: abCE
+ DE
+ 0: DE
+\= Expect no match
+ abcE
+No match
+ abCe
+No match
+ dE
+No match
+ De
+No match
+
+/[z\Qa-d]\E]/
+ z
+ 0: z
+ a
+ 0: a
+ -
+ 0: -
+ d
+ 0: d
+ ]
+ 0: ]
+\= Expect no match
+ b
+No match
+
+/(a+)*b/
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/(?i)reg(?:ul(?:[aä]|ae)r|ex)/
+ REGular
+ 0: REGular
+ regulaer
+ 0: regulaer
+ Regex
+ 0: Regex
+ regulär
+ 0: regul\xe4r
+
+/Åæåä[à-ÿÀ-ß]+/
+ Åæåäà
+ 0: \xc5\xe6\xe5\xe4\xe0
+ Åæåäÿ
+ 0: \xc5\xe6\xe5\xe4\xff
+ ÅæåäÀ
+ 0: \xc5\xe6\xe5\xe4\xc0
+ Åæåäß
+ 0: \xc5\xe6\xe5\xe4\xdf
+
+/(?<=Z)X./
+ \x84XAZXB
+ 0: XB
+
+/^(?(2)a|(1)(2))+$/
+ 123a
+Failed: error -40: backreference condition or recursion test is not supported for DFA matching
+
+/(?<=a|bbbb)c/
+ ac
+ 0: c
+ bbbbc
+ 0: c
+
+/line\nbreak/
+ this is a line\nbreak
+ 0: line\x0abreak
+ line one\nthis is a line\nbreak in the second line
+ 0: line\x0abreak
+
+/line\nbreak/firstline
+ this is a line\nbreak
+ 0: line\x0abreak
+\= Expect no match
+ line one\nthis is a line\nbreak in the second line
+No match
+
+/line\nbreak/m,firstline
+ this is a line\nbreak
+ 0: line\x0abreak
+\= Expect no match
+ line one\nthis is a line\nbreak in the second line
+No match
+
+/1234/
+ 123\=ps
+Partial match: 123
+\= Expect no match
+ a4\=ps,dfa_restart
+No match
+
+/1234/
+ 123\=ps
+Partial match: 123
+ 4\=ps,dfa_restart
+ 0: 4
+
+/^/gm
+ a\nb\nc\n
+ 0:
+ 0:
+ 0:
+ \
+ 0:
+
+/(?<=C\n)^/gm
+ A\nC\nC\n
+ 0:
+
+/(?s)A?B/
+ AB
+ 0: AB
+ aB
+ 0: B
+
+/(?s)A*B/
+ AB
+ 0: AB
+ aB
+ 0: B
+
+/(?m)A?B/
+ AB
+ 0: AB
+ aB
+ 0: B
+
+/(?m)A*B/
+ AB
+ 0: AB
+ aB
+ 0: B
+
+/Content-Type\x3A[^\r\n]{6,}/
+ Content-Type:xxxxxyyy
+ 0: Content-Type:xxxxxyyy
+
+/Content-Type\x3A[^\r\n]{6,}z/
+ Content-Type:xxxxxyyyz
+ 0: Content-Type:xxxxxyyyz
+
+/Content-Type\x3A[^a]{6,}/
+ Content-Type:xxxyyy
+ 0: Content-Type:xxxyyy
+
+/Content-Type\x3A[^a]{6,}z/
+ Content-Type:xxxyyyz
+ 0: Content-Type:xxxyyyz
+
+/^abc/Im,newline=lf
+Capturing subpattern count = 0
+Options: multiline
+Forced newline is LF
+First code unit at start or follows newline
+Last code unit = 'c'
+Subject length lower bound = 3
+ xyz\nabc
+ 0: abc
+ xyz\r\nabc
+ 0: abc
+\= Expect no match
+ xyz\rabc
+No match
+ xyzabc\r
+No match
+ xyzabc\rpqr
+No match
+ xyzabc\r\n
+No match
+ xyzabc\r\npqr
+No match
+
+/^abc/Im,newline=crlf
+Capturing subpattern count = 0
+Options: multiline
+Forced newline is CRLF
+First code unit at start or follows newline
+Last code unit = 'c'
+Subject length lower bound = 3
+ xyz\r\nabclf>
+ 0: abc
+\= Expect no match
+ xyz\nabclf
+No match
+ xyz\rabclf
+No match
+
+/^abc/Im,newline=cr
+Capturing subpattern count = 0
+Options: multiline
+Forced newline is CR
+First code unit at start or follows newline
+Last code unit = 'c'
+Subject length lower bound = 3
+ xyz\rabc
+ 0: abc
+\= Expect no match
+ xyz\nabc
+No match
+ xyz\r\nabc
+No match
+
+/.*/I,newline=lf
+Capturing subpattern count = 0
+May match empty string
+Forced newline is LF
+First code unit at start or follows newline
+Subject length lower bound = 0
+ abc\ndef
+ 0: abc
+ abc\rdef
+ 0: abc\x0ddef
+ abc\r\ndef
+ 0: abc\x0d
+
+/.*/I,newline=cr
+Capturing subpattern count = 0
+May match empty string
+Forced newline is CR
+First code unit at start or follows newline
+Subject length lower bound = 0
+ abc\ndef
+ 0: abc\x0adef
+ abc\rdef
+ 0: abc
+ abc\r\ndef
+ 0: abc
+
+/.*/I,newline=crlf
+Capturing subpattern count = 0
+May match empty string
+Forced newline is CRLF
+First code unit at start or follows newline
+Subject length lower bound = 0
+ abc\ndef
+ 0: abc\x0adef
+ abc\rdef
+ 0: abc\x0ddef
+ abc\r\ndef
+ 0: abc
+
+/\w+(.)(.)?def/Is
+Capturing subpattern count = 2
+Options: dotall
+Starting code units: 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P
+ Q R S T U V W X Y Z _ a b c d e f g h i j k l m n o p q r s t u v w x y z
+Last code unit = 'f'
+Subject length lower bound = 5
+ abc\ndef
+ 0: abc\x0adef
+ abc\rdef
+ 0: abc\x0ddef
+ abc\r\ndef
+ 0: abc\x0d\x0adef
+
+/\w+(.)(.)?def/s
+ abc\ndef
+ 0: abc\x0adef
+ abc\rdef
+ 0: abc\x0ddef
+ abc\r\ndef
+ 0: abc\x0d\x0adef
+
+/^\w+=.*(\\\n.*)*/
+ abc=xyz\\\npqr
+ 0: abc=xyz\\x0apqr
+ 1: abc=xyz\\x0apq
+ 2: abc=xyz\\x0ap
+ 3: abc=xyz\\x0a
+ 4: abc=xyz\
+ 5: abc=xyz
+ 6: abc=xy
+ 7: abc=x
+ 8: abc=
+
+/^(a()*)*/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ 4:
+
+/^(?:a(?:(?:))*)*/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ 4:
+
+/^(a()+)+/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+
+/^(?:a(?:(?:))+)+/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+
+/(a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+ 0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/(?>a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+ 0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/(?:a|)*\d/
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+ 0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4
+\= Expect no match
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+No match
+
+/^a.b/newline=lf
+ a\rb
+ 0: a\x0db
+\= Expect no match
+ a\nb
+No match
+
+/^a.b/newline=cr
+ a\nb
+ 0: a\x0ab
+\= Expect no match
+ a\rb
+No match
+
+/^a.b/newline=anycrlf
+ a\x85b
+ 0: a\x85b
+\= Expect no match
+ a\rb
+No match
+
+/^a.b/newline=any
+\= Expect no match
+ a\nb
+No match
+ a\rb
+No match
+ a\x85b
+No match
+
+/^abc./gmx,newline=any
+ abc1 \x0aabc2 \x0babc3xx \x0cabc4 \x0dabc5xx \x0d\x0aabc6 \x85abc7 JUNK
+ 0: abc1
+ 0: abc2
+ 0: abc3
+ 0: abc4
+ 0: abc5
+ 0: abc6
+ 0: abc7
+
+/abc.$/gmx,newline=any
+ abc1\x0a abc2\x0b abc3\x0c abc4\x0d abc5\x0d\x0a abc6\x85 abc9
+ 0: abc1
+ 0: abc2
+ 0: abc3
+ 0: abc4
+ 0: abc5
+ 0: abc6
+ 0: abc9
+
+/^a\Rb/bsr=unicode
+ a\nb
+ 0: a\x0ab
+ a\rb
+ 0: a\x0db
+ a\r\nb
+ 0: a\x0d\x0ab
+ a\x0bb
+ 0: a\x0bb
+ a\x0cb
+ 0: a\x0cb
+ a\x85b
+ 0: a\x85b
+\= Expect no match
+ a\n\rb
+No match
+
+/^a\R*b/bsr=unicode
+ ab
+ 0: ab
+ a\nb
+ 0: a\x0ab
+ a\rb
+ 0: a\x0db
+ a\r\nb
+ 0: a\x0d\x0ab
+ a\x0bb
+ 0: a\x0bb
+ a\x0cb
+ 0: a\x0cb
+ a\x85b
+ 0: a\x85b
+ a\n\rb
+ 0: a\x0a\x0db
+ a\n\r\x85\x0cb
+ 0: a\x0a\x0d\x85\x0cb
+
+/^a\R+b/bsr=unicode
+ a\nb
+ 0: a\x0ab
+ a\rb
+ 0: a\x0db
+ a\r\nb
+ 0: a\x0d\x0ab
+ a\x0bb
+ 0: a\x0bb
+ a\x0cb
+ 0: a\x0cb
+ a\x85b
+ 0: a\x85b
+ a\n\rb
+ 0: a\x0a\x0db
+ a\n\r\x85\x0cb
+ 0: a\x0a\x0d\x85\x0cb
+\= Expect no match
+ ab
+No match
+
+/^a\R{1,3}b/bsr=unicode
+ a\nb
+ 0: a\x0ab
+ a\n\rb
+ 0: a\x0a\x0db
+ a\n\r\x85b
+ 0: a\x0a\x0d\x85b
+ a\r\n\r\nb
+ 0: a\x0d\x0a\x0d\x0ab
+ a\r\n\r\n\r\nb
+ 0: a\x0d\x0a\x0d\x0a\x0d\x0ab
+ a\n\r\n\rb
+ 0: a\x0a\x0d\x0a\x0db
+ a\n\n\r\nb
+ 0: a\x0a\x0a\x0d\x0ab
+\= Expect no match
+ a\n\n\n\rb
+No match
+ a\r
+No match
+
+/.+foo/
+ afoo
+ 0: afoo
+\= Expect no match
+ \r\nfoo
+No match
+ \nfoo
+No match
+
+/.+foo/newline=crlf
+ afoo
+ 0: afoo
+ \nfoo
+ 0: \x0afoo
+\= Expect no match
+ \r\nfoo
+No match
+
+/.+foo/newline=any
+ afoo
+ 0: afoo
+\= Expect no match
+ \nfoo
+No match
+ \r\nfoo
+No match
+
+/.+foo/s
+ afoo
+ 0: afoo
+ \r\nfoo
+ 0: \x0d\x0afoo
+ \nfoo
+ 0: \x0afoo
+
+/^$/gm,newline=any
+ abc\r\rxyz
+ 0:
+ abc\n\rxyz
+ 0:
+\= Expect no match
+ abc\r\nxyz
+No match
+
+/^X/m
+ XABC
+ 0: X
+\= Expect no match
+ XABC\=notbol
+No match
+
+/(?m)^$/g,newline=any,aftertext
+ abc\r\n\r\n
+ 0:
+ 0+ \x0d\x0a
+
+/(?m)^$|^\r\n/g,newline=any,aftertext
+ abc\r\n\r\n
+ 0: \x0d\x0a
+ 0+
+ 1:
+
+/(?m)$/g,newline=any,aftertext
+ abc\r\n\r\n
+ 0:
+ 0+ \x0d\x0a\x0d\x0a
+ 0:
+ 0+ \x0d\x0a
+ 0:
+ 0+
+
+/(?|(abc)|(xyz))/
+ >abc<
+ 0: abc
+ >xyz<
+ 0: xyz
+
+/(x)(?|(abc)|(xyz))(x)/
+ xabcx
+ 0: xabcx
+ xxyzx
+ 0: xxyzx
+
+/(x)(?|(abc)(pqr)|(xyz))(x)/
+ xabcpqrx
+ 0: xabcpqrx
+ xxyzx
+ 0: xxyzx
+
+/(?|(abc)|(xyz))(?1)/
+ abcabc
+ 0: abcabc
+ xyzabc
+ 0: xyzabc
+\= Expect no match
+ xyzxyz
+No match
+
+/\H\h\V\v/
+ X X\x0a
+ 0: X X\x0a
+ X\x09X\x0b
+ 0: X\x09X\x0b
+\= Expect no match
+ \xa0 X\x0a
+No match
+
+/\H*\h+\V?\v{3,4}/
+ \x09\x20\xa0X\x0a\x0b\x0c\x0d\x0a
+ 0: \x09 \xa0X\x0a\x0b\x0c\x0d
+ \x09\x20\xa0\x0a\x0b\x0c\x0d\x0a
+ 0: \x09 \xa0\x0a\x0b\x0c\x0d
+ \x09\x20\xa0\x0a\x0b\x0c
+ 0: \x09 \xa0\x0a\x0b\x0c
+\= Expect no match
+ \x09\x20\xa0\x0a\x0b
+No match
+
+/\H{3,4}/
+ XY ABCDE
+ 0: ABCD
+ XY PQR ST
+ 0: PQR
+
+/.\h{3,4}./
+ XY AB PQRS
+ 0: B P
+ 1: B
+
+/\h*X\h?\H+Y\H?Z/
+ >XNNNYZ
+ 0: XNNNYZ
+ > X NYQZ
+ 0: X NYQZ
+\= Expect no match
+ >XYZ
+No match
+ > X NY Z
+No match
+
+/\v*X\v?Y\v+Z\V*\x0a\V+\x0b\V{2,3}\x0c/
+ >XY\x0aZ\x0aA\x0bNN\x0c
+ 0: XY\x0aZ\x0aA\x0bNN\x0c
+ >\x0a\x0dX\x0aY\x0a\x0bZZZ\x0aAAA\x0bNNN\x0c
+ 0: \x0a\x0dX\x0aY\x0a\x0bZZZ\x0aAAA\x0bNNN\x0c
+
+/.+A/newline=crlf
+\= Expect no match
+ \r\nA
+No match
+
+/\nA/newline=crlf
+ \r\nA
+ 0: \x0aA
+
+/[\r\n]A/newline=crlf
+ \r\nA
+ 0: \x0aA
+
+/(\r|\n)A/newline=crlf
+ \r\nA
+ 0: \x0aA
+
+/a\Rb/I,bsr=anycrlf
+Capturing subpattern count = 0
+\R matches CR, LF, or CRLF
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 3
+ a\rb
+ 0: a\x0db
+ a\nb
+ 0: a\x0ab
+ a\r\nb
+ 0: a\x0d\x0ab
+\= Expect no match
+ a\x85b
+No match
+ a\x0bb
+No match
+
+/a\Rb/I,bsr=unicode
+Capturing subpattern count = 0
+\R matches any Unicode newline
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 3
+ a\rb
+ 0: a\x0db
+ a\nb
+ 0: a\x0ab
+ a\r\nb
+ 0: a\x0d\x0ab
+ a\x85b
+ 0: a\x85b
+ a\x0bb
+ 0: a\x0bb
+
+/a\R?b/I,bsr=anycrlf
+Capturing subpattern count = 0
+\R matches CR, LF, or CRLF
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 2
+ a\rb
+ 0: a\x0db
+ a\nb
+ 0: a\x0ab
+ a\r\nb
+ 0: a\x0d\x0ab
+\= Expect no match
+ a\x85b
+No match
+ a\x0bb
+No match
+
+/a\R?b/I,bsr=unicode
+Capturing subpattern count = 0
+\R matches any Unicode newline
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 2
+ a\rb
+ 0: a\x0db
+ a\nb
+ 0: a\x0ab
+ a\r\nb
+ 0: a\x0d\x0ab
+ a\x85b
+ 0: a\x85b
+ a\x0bb
+ 0: a\x0bb
+
+/a\R{2,4}b/I,bsr=anycrlf
+Capturing subpattern count = 0
+\R matches CR, LF, or CRLF
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 4
+ a\r\n\nb
+ 0: a\x0d\x0a\x0ab
+ a\n\r\rb
+ 0: a\x0a\x0d\x0db
+ a\r\n\r\n\r\n\r\nb
+ 0: a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0ab
+\= Expect no match
+ a\x0b\x0bb
+No match
+ a\x85\x85b
+No match
+
+/a\R{2,4}b/I,bsr=unicode
+Capturing subpattern count = 0
+\R matches any Unicode newline
+First code unit = 'a'
+Last code unit = 'b'
+Subject length lower bound = 4
+ a\r\rb
+ 0: a\x0d\x0db
+ a\n\n\nb
+ 0: a\x0a\x0a\x0ab
+ a\r\n\n\r\rb
+ 0: a\x0d\x0a\x0a\x0d\x0db
+ a\x85\x85b
+ 0: a\x85\x85b
+ a\x0b\x0bb
+ 0: a\x0b\x0bb
+\= Expect no match
+ a\r\r\r\r\rb
+No match
+
+/a(?!)|\wbc/
+ abc
+ 0: abc
+
+/a[]b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+No match
+
+/a[]+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+No match
+
+/a[]*+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+\= Expect no match
+ ab
+No match
+
+/a[^]b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+ aXb
+ 0: aXb
+ a\nb
+ 0: a\x0ab
+\= Expect no match
+ ab
+No match
+
+/a[^]+b/alt_bsux,allow_empty_class,match_unset_backref,dupnames
+ aXb
+ 0: aXb
+ a\nX\nXb
+ 0: a\x0aX\x0aXb
+\= Expect no match
+ ab
+No match
+
+/X$/dollar_endonly
+ X
+ 0: X
+\= Expect no match
+ X\n
+No match
+
+/X$/
+ X
+ 0: X
+ X\n
+ 0: X
+
+/xyz/auto_callout
+ xyz
+--->xyz
+ +0 ^ x
+ +1 ^^ y
+ +2 ^ ^ z
+ +3 ^ ^ End of pattern
+ 0: xyz
+ abcxyz
+--->abcxyz
+ +0 ^ x
+ +1 ^^ y
+ +2 ^ ^ z
+ +3 ^ ^ End of pattern
+ 0: xyz
+\= Expect no match
+ abc
+No match
+ abcxypqr
+No match
+
+/xyz/auto_callout,no_start_optimize
+ abcxyz
+--->abcxyz
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +1 ^^ y
+ +2 ^ ^ z
+ +3 ^ ^ End of pattern
+ 0: xyz
+\= Expect no match
+ abc
+--->abc
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+No match
+ abcxypqr
+--->abcxypqr
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +1 ^^ y
+ +2 ^ ^ z
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+ +0 ^ x
+No match
+
+/(*NO_START_OPT)xyz/auto_callout
+ abcxyz
+--->abcxyz
++15 ^ x
++15 ^ x
++15 ^ x
++15 ^ x
++16 ^^ y
++17 ^ ^ z
++18 ^ ^ End of pattern
+ 0: xyz
+
+/(?C)ab/
+ ab
+--->ab
+ 0 ^ a
+ 0: ab
+ ab\=callout_none
+ 0: ab
+
+/ab/auto_callout
+ ab
+--->ab
+ +0 ^ a
+ +1 ^^ b
+ +2 ^ ^ End of pattern
+ 0: ab
+ ab\=callout_none
+ 0: ab
+
+/^"((?(?=[a])[^"])|b)*"$/auto_callout
+ "ab"
+--->"ab"
+ +0 ^ ^
+ +1 ^ "
+ +2 ^^ (
++21 ^^ "
+ +3 ^^ (?
++18 ^^ b
+ +5 ^^ (?=
+ +8 ^ [a]
++11 ^^ )
++12 ^^ [^"]
++16 ^ ^ )
++17 ^ ^ |
++21 ^ ^ "
+ +3 ^ ^ (?
++18 ^ ^ b
+ +5 ^ ^ (?=
+ +8 ^ [a]
++19 ^ ^ )*
++21 ^ ^ "
+ +3 ^ ^ (?
++18 ^ ^ b
+ +5 ^ ^ (?=
+ +8 ^ [a]
++17 ^ ^ |
++22 ^ ^ $
++23 ^ ^ End of pattern
+ 0: "ab"
+ "ab"\=callout_none
+ 0: "ab"
+
+/\d+X|9+Y/
+ ++++123999\=ps
+Partial match: 123999
+ ++++123999Y\=ps
+ 0: 999Y
+
+/Z(*F)/
+\= Expect no match
+ Z\=ps
+No match
+ ZA\=ps
+No match
+
+/Z(?!)/
+\= Expect no match
+ Z\=ps
+No match
+ ZA\=ps
+No match
+
+/dog(sbody)?/
+ dogs\=ps
+ 0: dog
+ dogs\=ph
+Partial match: dogs
+
+/dog(sbody)??/
+ dogs\=ps
+ 0: dog
+ dogs\=ph
+Partial match: dogs
+
+/dog|dogsbody/
+ dogs\=ps
+ 0: dog
+ dogs\=ph
+Partial match: dogs
+
+/dogsbody|dog/
+ dogs\=ps
+ 0: dog
+ dogs\=ph
+Partial match: dogs
+
+/Z(*F)Q|ZXY/
+ Z\=ps
+Partial match: Z
+\= Expect no match
+ ZA\=ps
+No match
+ X\=ps
+No match
+
+/\bthe cat\b/
+ the cat\=ps
+ 0: the cat
+ the cat\=ph
+Partial match: the cat
+
+/dog(sbody)?/
+ dogs\=ps
+ 0: dog
+ body\=dfa_restart
+ 0: body
+
+/dog(sbody)?/
+ dogs\=ph
+Partial match: dogs
+ body\=dfa_restart
+ 0: body
+
+/abc/
+ abc\=ps
+ 0: abc
+ abc\=ph
+ 0: abc
+
+/abc\K123/
+ xyzabc123pqr
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/(?<=abc)123/
+ xyzabc123pqr
+ 0: 123
+ xyzabc12\=ps
+Partial match: abc12
+ <<<
+ xyzabc12\=ph
+Partial match: abc12
+ <<<
+
+/\babc\b/
+ +++abc+++
+ 0: abc
+ +++ab\=ps
+Partial match: +ab
+ <
+ +++ab\=ph
+Partial match: +ab
+ <
+
+/(?=C)/g,aftertext
+ ABCDECBA
+ 0:
+ 0+ CDECBA
+ 0:
+ 0+ CBA
+
+/(abc|def|xyz)/I
+Capturing subpattern count = 1
+Starting code units: a d x
+Subject length lower bound = 3
+ terhjk;abcdaadsfe
+ 0: abc
+ the quick xyz brown fox
+ 0: xyz
+\= Expect no match
+ thejk;adlfj aenjl;fda asdfasd ehj;kjxyasiupd
+No match
+
+/(abc|def|xyz)/I,no_start_optimize
+Capturing subpattern count = 1
+Options: no_start_optimize
+Subject length lower bound = 0
+ terhjk;abcdaadsfe
+ 0: abc
+ the quick xyz brown fox
+ 0: xyz
+\= Expect no match
+ thejk;adlfj aenjl;fda asdfasd ehj;kjxyasiupd
+No match
+
+/abcd*/aftertext
+ xxxxabcd\=ps
+ 0: abcd
+ 0+
+ xxxxabcd\=ph
+Partial match: abcd
+ dddxxx\=dfa_restart
+ 0: ddd
+ 0+ xxx
+ xxxxabcd\=ph
+Partial match: abcd
+ xxx\=dfa_restart
+ 0:
+ 0+ xxx
+
+/abcd*/i
+ xxxxabcd\=ps
+ 0: abcd
+ xxxxabcd\=ph
+Partial match: abcd
+ XXXXABCD\=ps
+ 0: ABCD
+ XXXXABCD\=ph
+Partial match: ABCD
+
+/abc\d*/
+ xxxxabc1\=ps
+ 0: abc1
+ xxxxabc1\=ph
+Partial match: abc1
+
+/abc[de]*/
+ xxxxabcde\=ps
+ 0: abcde
+ xxxxabcde\=ph
+Partial match: abcde
+
+/(?:(?1)|B)(A(*F)|C)/
+ ABCD
+ 0: BC
+ CCD
+ 0: CC
+\= Expect no match
+ CAD
+No match
+
+/^(?:(?1)|B)(A(*F)|C)/
+ CCD
+ 0: CC
+ BCD
+ 0: BC
+\= Expect no match
+ ABCD
+No match
+ CAD
+No match
+ BAD
+No match
+
+/^(?!a(*SKIP)b)/
+ ac
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/^(?=a(*SKIP)b|ac)/
+ ac
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/^(?=a(*THEN)b|ac)/
+ ac
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/^(?=a(*PRUNE)b)/
+ ab
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/^(?(?!a(*SKIP)b))/
+ ac
+Failed: error -42: pattern contains an item that is not supported for DFA matching
+
+/(?<=abc)def/
+ abc\=ph
+Partial match: abc
+ <<<
+
+/abc$/
+ abc
+ 0: abc
+ abc\=ps
+ 0: abc
+ abc\=ph
+Partial match: abc
+
+/abc$/m
+ abc
+ 0: abc
+ abc\n
+ 0: abc
+ abc\=ph
+Partial match: abc
+ abc\n\=ph
+ 0: abc
+ abc\=ps
+ 0: abc
+ abc\n\=ps
+ 0: abc
+
+/abc\z/
+ abc
+ 0: abc
+ abc\=ps
+ 0: abc
+ abc\=ph
+Partial match: abc
+
+/abc\Z/
+ abc
+ 0: abc
+ abc\=ps
+ 0: abc
+ abc\=ph
+Partial match: abc
+
+/abc\b/
+ abc
+ 0: abc
+ abc\=ps
+ 0: abc
+ abc\=ph
+Partial match: abc
+
+/abc\B/
+ abc\=ps
+Partial match: abc
+ abc\=ph
+Partial match: abc
+\= Expect no match
+ abc
+No match
+
+/.+/
+ abc\=offset=0
+ 0: abc
+ abc\=offset=1
+ 0: bc
+ abc\=offset=2
+ 0: c
+\= Bad offsets
+ abc\=offset=4
+Failed: error -33: bad offset value
+ abc\=offset=-4
+** Invalid value in 'offset=-4'
+\= Expect no match
+ abc\=offset=3
+No match
+
+/^(?:a)++\w/
+ aaaab
+ 0: aaaab
+\= Expect no match
+ aaaa
+No match
+ bbb
+No match
+
+/^(?:aa|(?:a)++\w)/
+ aaaab
+ 0: aaaab
+ 1: aa
+ aaaa
+ 0: aa
+\= Expect no match
+ bbb
+No match
+
+/^(?:a)*+\w/
+ aaaab
+ 0: aaaab
+ bbb
+ 0: b
+\= Expect no match
+ aaaa
+No match
+
+/^(a)++\w/
+ aaaab
+ 0: aaaab
+\= Expect no match
+ aaaa
+No match
+ bbb
+No match
+
+/^(a|)++\w/
+ aaaab
+ 0: aaaab
+\= Expect no match
+ aaaa
+No match
+ bbb
+No match
+
+/(?=abc){3}abc/aftertext
+ abcabcabc
+ 0: abc
+ 0+ abcabc
+\= Expect no match
+ xyz
+No match
+
+/(?=abc)+abc/aftertext
+ abcabcabc
+ 0: abc
+ 0+ abcabc
+\= Expect no match
+ xyz
+No match
+
+/(?=abc)++abc/aftertext
+ abcabcabc
+ 0: abc
+ 0+ abcabc
+\= Expect no match
+ xyz
+No match
+
+/(?=abc){0}xyz/
+ xyz
+ 0: xyz
+
+/(?=abc){1}xyz/
+\= Expect no match
+ xyz
+No match
+
+/(?=(a))?./
+ ab
+ 0: a
+ bc
+ 0: b
+
+/(?=(a))??./
+ ab
+ 0: a
+ bc
+ 0: b
+
+/^(?=(a)){0}b(?1)/
+ backgammon
+ 0: ba
+
+/^(?=(?1))?[az]([abc])d/
+ abd
+ 0: abd
+ zcdxx
+ 0: zcd
+
+/^(?!a){0}\w+/
+ aaaaa
+ 0: aaaaa
+
+/(?<=(abc))?xyz/
+ abcxyz
+ 0: xyz
+ pqrxyz
+ 0: xyz
+
+/((?2))((?1))/
+ abc
+Failed: error -52: nested recursion at the same subject position
+
+/(?(R)a+|(?R)b)/
+ aaaabcde
+ 0: aaaab
+
+/(?(R)a+|((?R))b)/
+ aaaabcde
+ 0: aaaab
+
+/((?(R)a+|(?1)b))/
+ aaaabcde
+ 0: aaaab
+
+/((?(R2)a+|(?1)b))()/
+ aaaabcde
+Failed: error -40: backreference condition or recursion test is not supported for DFA matching
+
+/(?(R)a*(?1)|((?R))b)/
+ aaaabcde
+Failed: error -52: nested recursion at the same subject position
+
+/(a+)/no_auto_possess
+ aaaa\=ovector=3
+Matched, but offsets vector is too small to show all matches
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ aaaa\=ovector=4
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+
+/^\R/
+ \r\=ps
+ 0: \x0d
+ \r\=ph
+Partial match: \x0d
+
+/^\R{2,3}x/
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ \r\r\=ps
+Partial match: \x0d\x0d
+ \r\r\=ph
+Partial match: \x0d\x0d
+ \r\r\r\=ps
+Partial match: \x0d\x0d\x0d
+ \r\r\r\=ph
+Partial match: \x0d\x0d\x0d
+ \r\rx
+ 0: \x0d\x0dx
+ \r\r\rx
+ 0: \x0d\x0d\x0dx
+
+/^\R{2,3}?x/
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ \r\r\=ps
+Partial match: \x0d\x0d
+ \r\r\=ph
+Partial match: \x0d\x0d
+ \r\r\r\=ps
+Partial match: \x0d\x0d\x0d
+ \r\r\r\=ph
+Partial match: \x0d\x0d\x0d
+ \r\rx
+ 0: \x0d\x0dx
+ \r\r\rx
+ 0: \x0d\x0d\x0dx
+
+/^\R?x/
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ x
+ 0: x
+ \rx
+ 0: \x0dx
+
+/^\R+x/
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ \r\n\=ps
+Partial match: \x0d\x0a
+ \r\n\=ph
+Partial match: \x0d\x0a
+ \rx
+ 0: \x0dx
+
+/^a$/newline=crlf
+ a\r\=ps
+Partial match: a\x0d
+ a\r\=ph
+Partial match: a\x0d
+
+/^a$/m,newline=crlf
+ a\r\=ps
+Partial match: a\x0d
+ a\r\=ph
+Partial match: a\x0d
+
+/^(a$|a\r)/newline=crlf
+ a\r\=ps
+ 0: a\x0d
+ a\r\=ph
+Partial match: a\x0d
+
+/^(a$|a\r)/m,newline=crlf
+ a\r\=ps
+ 0: a\x0d
+ a\r\=ph
+Partial match: a\x0d
+
+/./newline=crlf
+ \r\=ps
+ 0: \x0d
+ \r\=ph
+Partial match: \x0d
+
+/.{2,3}/newline=crlf
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ \r\r\=ps
+ 0: \x0d\x0d
+ \r\r\=ph
+Partial match: \x0d\x0d
+ \r\r\r\=ps
+ 0: \x0d\x0d\x0d
+ \r\r\r\=ph
+Partial match: \x0d\x0d\x0d
+
+/.{2,3}?/newline=crlf
+ \r\=ps
+Partial match: \x0d
+ \r\=ph
+Partial match: \x0d
+ \r\r\=ps
+ 0: \x0d\x0d
+ \r\r\=ph
+Partial match: \x0d\x0d
+ \r\r\r\=ps
+ 0: \x0d\x0d\x0d
+ 1: \x0d\x0d
+ \r\r\r\=ph
+Partial match: \x0d\x0d\x0d
+
+# Test simple validity check for restarts
+
+/abcdef/
+ abc\=dfa_restart
+Failed: error -38: invalid data in workspace for DFA restart
+
+/<H((?(?!<H|F>)(.)|(?R))++)*F>/
+ text <H more text <H texting more hexA0-"\xA0" hex above 7F-"\xBC" F> text xxxxx <H text F> text F> text2 <H text sample F> more text.
+ 0: <H more text <H texting more hexA0-"\xa0" hex above 7F-"\xbc" F> text xxxxx <H text F> text F>
+
+/^(?>.{4})abc|^\w\w.xabcd/
+ xxxxabcd
+ 0: xxxxabcd
+ 1: xxxxabc
+ xx\xa0xabcd
+ 0: xx\xa0xabcd
+ 1: xx\xa0xabc
+
+/^(.{4}){2}+abc|^\w\w.x\w\w\w\wabcd/
+ xxxxxxxxabcd
+ 0: xxxxxxxxabcd
+ 1: xxxxxxxxabc
+ xx\xa0xxxxxabcd
+ 0: xx\xa0xxxxxabcd
+ 1: xx\xa0xxxxxabc
+
+/abcd/
+ abcd\=ovector=0
+ 0: abcd
+
+# These tests show up auto-possessification
+
+/[ab]*/
+ aaaa
+ 0: aaaa
+
+/[ab]*?/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+ 4:
+
+/[ab]?/
+ aaaa
+ 0: a
+
+/[ab]??/
+ aaaa
+ 0: a
+ 1:
+
+/[ab]+/
+ aaaa
+ 0: aaaa
+
+/[ab]+?/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+ 3: a
+
+/[ab]{2,3}/
+ aaaa
+ 0: aaa
+
+/[ab]{2,3}?/
+ aaaa
+ 0: aaa
+ 1: aa
+
+/[ab]{2,}/
+ aaaa
+ 0: aaaa
+
+/[ab]{2,}?/
+ aaaa
+ 0: aaaa
+ 1: aaa
+ 2: aa
+
+'\A(?:[^\"]++|\"(?:[^\"]*+|\"\")*+\")++'
+ NON QUOTED \"QUOT\"\"ED\" AFTER \"NOT MATCHED
+ 0: NON QUOTED "QUOT""ED" AFTER
+
+'\A(?:[^\"]++|\"(?:[^\"]++|\"\")*+\")++'
+ NON QUOTED \"QUOT\"\"ED\" AFTER \"NOT MATCHED
+ 0: NON QUOTED "QUOT""ED" AFTER
+
+/abc(?=xyz)/allusedtext
+ abcxyzpqr
+ 0: abcxyz
+ >>>
+ abcxyzpqr\=aftertext
+ 0: abcxyz
+ >>>
+ 0+ xyzpqr
+
+/(?<=pqr)abc(?=xyz)/allusedtext
+ xyzpqrabcxyzpqr
+ 0: pqrabcxyz
+ <<< >>>
+ xyzpqrabcxyzpqr\=aftertext
+ 0: pqrabcxyz
+ <<< >>>
+ 0+ xyzpqr
+
+/a\b/
+ a.\=allusedtext
+ 0: a.
+ >
+ a\=allusedtext
+ 0: a
+
+/abc(?=abcde)(?=ab)/allusedtext
+ abcabcdefg
+ 0: abcabcde
+ >>>>>
+
+/a*?b*?/
+ ab
+ 0: ab
+ 1: a
+ 2:
+
+/(*NOTEMPTY)a*?b*?/
+ ab
+ 0: ab
+ 1: a
+ ba
+ 0: b
+ cb
+ 0: b
+
+/(*NOTEMPTY_ATSTART)a*?b*?/aftertext
+ ab
+ 0: ab
+ 0+
+ 1: a
+ cdab
+ 0:
+ 0+ dab
+
+/(a)(b)|(c)/
+ XcX\=ovector=2,get=1,get=2,get=3,get=4,getall
+ 0: c
+Get substring 1 failed (-55): requested value is not set
+Get substring 2 failed (-54): requested value is not available
+Get substring 3 failed (-54): requested value is not available
+Get substring 4 failed (-54): requested value is not available
+ 0L c
+
+/(?<A>aa)/
+ aa\=get=A
+ 0: aa
+Get substring 'A' failed (-41): function is not supported for DFA matching
+ aa\=copy=A
+ 0: aa
+Copy substring 'A' failed (-41): function is not supported for DFA matching
+
+/a+/no_auto_possess
+ a\=ovector=2,get=1,get=2,getall
+ 0: a
+Get substring 1 failed (-55): requested value is not set
+Get substring 2 failed (-54): requested value is not available
+ 0L a
+ aaa\=ovector=2,get=1,get=2,getall
+Matched, but offsets vector is too small to show all matches
+ 0: aaa
+ 1: aa
+ 1G aa (2)
+Get substring 2 failed (-54): requested value is not available
+ 0L aaa
+ 1L aa
+
+/a(b)c(d)/
+ abc\=ph,copy=0,copy=1,getall
+Partial match: abc
+ 0C abc (3)
+Copy substring 1 failed (-2): partial match
+get substring list failed (-2): partial match
+
+/ab(?C" any text with spaces ")cde/B
+------------------------------------------------------------------
+ Bra
+ ab
+ CalloutStr " any text with spaces " 6 30 1
+ cde
+ Ket
+ End
+------------------------------------------------------------------
+ abcde
+Callout (6): " any text with spaces "
+--->abcde
+ ^ ^ c
+ 0: abcde
+ 12abcde
+Callout (6): " any text with spaces "
+--->12abcde
+ ^ ^ c
+ 0: abcde
+
+/^a(b)c(?C1)def/
+ abcdef
+--->abcdef
+ 1 ^ ^ d
+ 0: abcdef
+
+/^a(b)c(?C"AB")def/
+ abcdef
+Callout (10): "AB"
+--->abcdef
+ ^ ^ d
+ 0: abcdef
+
+/^a(b)c(?C1)def/
+ abcdef\=callout_capture
+Callout 1: last capture = 0
+--->abcdef
+ ^ ^ d
+ 0: abcdef
+
+/^a(b)c(?C{AB})def/B
+------------------------------------------------------------------
+ Bra
+ ^
+ a
+ CBra 1
+ b
+ Ket
+ c
+ CalloutStr {AB} 10 14 1
+ def
+ Ket
+ End
+------------------------------------------------------------------
+ abcdef\=callout_capture
+Callout (10): {AB} last capture = 0
+--->abcdef
+ ^ ^ d
+ 0: abcdef
+
+/^(?(?C25)(?=abc)abcd|xyz)/B
+------------------------------------------------------------------
+ Bra
+ ^
+ Cond
+ Callout 25 9 3
+ Assert
+ abc
+ Ket
+ abcd
+ Alt
+ xyz
+ Ket
+ Ket
+ End
+------------------------------------------------------------------
+ abcdefg
+--->abcdefg
+ 25 ^ (?=
+ 0: abcd
+ xyz123
+--->xyz123
+ 25 ^ (?=
+ 0: xyz
+
+/^(?(?C$abc$)(?=abc)abcd|xyz)/B
+------------------------------------------------------------------
+ Bra
+ ^
+ Cond
+ CalloutStr $abc$ 7 12 3
+ Assert
+ abc
+ Ket
+ abcd
+ Alt
+ xyz
+ Ket
+ Ket
+ End
+------------------------------------------------------------------
+ abcdefg
+Callout (7): $abc$
+--->abcdefg
+ ^ (?=
+ 0: abcd
+ xyz123
+Callout (7): $abc$
+--->xyz123
+ ^ (?=
+ 0: xyz
+
+/^ab(?C'first')cd(?C"second")ef/
+ abcdefg
+Callout (7): 'first'
+--->abcdefg
+ ^ ^ c
+Callout (20): "second"
+--->abcdefg
+ ^ ^ e
+ 0: abcdef
+
+/(?:a(?C`code`)){3}X/
+ aaaXY
+Callout (8): `code`
+--->aaaXY
+ ^^ ){3}
+Callout (8): `code`
+--->aaaXY
+ ^ ^ ){3}
+Callout (8): `code`
+--->aaaXY
+ ^ ^ ){3}
+ 0: aaaX
+
+# Binary zero in callout string
+/"a(?C'x" 00 "z')b"/hex
+ abcdefgh
+Callout (5): 'x\x00z'
+--->abcdefgh
+ ^^ b
+ 0: ab
+
+/(?(?!)a|b)/
+ bbb
+ 0: b
+\= Expect no match
+ aaa
+No match
+
+/^/gm
+ \n\n\n
+ 0:
+ 0:
+ 0:
+
+/^/gm,alt_circumflex
+ \n\n\n
+ 0:
+ 0:
+ 0:
+ 0:
+
+/abc/use_offset_limit
+ 1234abcde\=offset_limit=100
+ 0: abc
+ 1234abcde\=offset_limit=9
+ 0: abc
+ 1234abcde\=offset_limit=4
+ 0: abc
+ 1234abcde\=offset_limit=4,offset=4
+ 0: abc
+\= Expect no match
+ 1234abcde\=offset_limit=4,offset=5
+No match
+ 1234abcde\=offset_limit=3
+No match
+
+/(?<=abc)/use_offset_limit
+ 1234abc\=offset_limit=7
+ 0:
+\= Expect no match
+ 1234abc\=offset_limit=6
+No match
+
+/abcd/null_context
+ abcd\=null_context
+ 0: abcd
+
+/()()a+/no_auto_possess
+ aaa\=allcaptures
+** Ignored after DFA matching: allcaptures
+ 0: aaa
+ 1: aa
+ 2: a
+ a\=allcaptures
+** Ignored after DFA matching: allcaptures
+ 0: a
+
+/(*LIMIT_DEPTH=100)^((.)(?1)|.)$/
+\= Expect depth limit exceeded
+ a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+Failed: error -53: matching depth limit exceeded
+
+/(*LIMIT_HEAP=0)^((.)(?1)|.)$/
+\= Expect heap limit exceeded
+ a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+Failed: error -63: heap limit exceeded
+
+/(*LIMIT_HEAP=50000)^((.)(?1)|.)$/
+\= Expect success
+ a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+ 0: a[00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]([00]
+
+/(02-)?[0-9]{3}-[0-9]{3}/
+ 02-123-123
+ 0: 02-123-123
+
+/^(a(?2))(b)(?1)/
+ abbab\=find_limits
+Minimum heap limit = 0
+Minimum match limit = 4
+Minimum depth limit = 2
+ 0: abbab
+
+/abc/endanchored
+ xyzabc
+ 0: abc
+\= Expect no match
+ xyzabcdef
+No match
+\= Expect error
+ xyzabc\=ph
+Failed: error -34: bad option value
+
+/abc/
+ xyzabc\=endanchored
+ 0: abc
+\= Expect no match
+ xyzabcdef\=endanchored
+No match
+\= Expect error
+ xyzabc\=ps,endanchored
+Failed: error -34: bad option value
+
+/abc|bcd/endanchored
+ xyzabcd
+ 0: bcd
+\= Expect no match
+ xyzabcdef
+No match
+
+/(*NUL)^.*/
+ a\nb\x00ccc
+ 0: a\x0ab
+
+/(*NUL)^.*/s
+ a\nb\x00ccc
+ 0: a\x0ab\x00ccc
+
+/^x/m,newline=nul
+ ab\x00xy
+ 0: x
+
+/'#comment' 0d 0a 00 '^x\' 0a 'y'/x,newline=nul,hex
+ x\nyz
+ 0: x\x0ay
+
+/(*NUL)^X\NY/
+ X\nY
+ 0: X\x0aY
+ X\rY
+ 0: X\x0dY
+\= Expect no match
+ X\x00Y
+No match
+
+/(?<=abc|)/
+ abcde\=aftertext
+ 0:
+ 0+ abcde
+
+/(?<=|abc)/
+ abcde\=aftertext
+ 0:
+ 0+ abcde
+
+/(?<=abc|)/endanchored
+ abcde\=aftertext
+ 0:
+ 0+
+
+/(?<=|abc)/endanchored
+ abcde\=aftertext
+ 0:
+ 0+
+
+/(*LIMIT_MATCH=100).*(?![|H]?.*(?![|H]?););.*(?![|H]?.*(?![|H]?););\x00\x00\x00\x00\x00\x00\x00(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?![|);)?.*(![|H]?);)?.*(?![|H]?);)?.*(?![|H]?);)?.*(?![|H]););![|H]?););[|H]?);|H]?);)\x00\x00\x00 \x00\x00\x00H]?););?![|H]?);)?.*(?![|H]?););[||H]?);)?.*(?![|H]?););[|H]?);(?![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););;[\x00\x00\x00\x00\x00\x00\x00![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););/no_dotstar_anchor
+\= Expect limit exceeded
+.*(?![|H]?.*(?![|H]?););.*(?![|H]?.*(?![|H]?););\x00\x00\x00\x00\x00\x00\x00(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?!(?![|);)?.*(![|H]?);)?.*(?![|H]?);)?.*(?![|H]?);)?.*(?![|H]););![|H]?););[|H]?);|H]?);)\x00\x00\x00 \x00\x00\x00H]?););?![|H]?);)?.*(?![|H]?););[||H]?);)?.*(?![|H]?););[|H]?);(?![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););;[\x00\x00\x00\x00\x00\x00\x00![|H]?););![|H]?););[|H]?);|H]?);)?.*(?![|H]?););
+Failed: error -47: match limit exceeded
+
+/\n/firstline
+ xyz\nabc
+ 0: \x0a
+
+/\nabc/firstline
+ xyz\nabc
+ 0: \x0aabc
+
+/\x{0a}abc/firstline,newline=crlf
+\= Expect no match
+ xyz\r\nabc
+No match
+
+/[abc]/firstline
+\= Expect no match
+ \na
+No match
+
+# End of testinput6
diff --git a/test/monniaux/picosat-965/LICENSE b/test/monniaux/picosat-965/LICENSE
new file mode 100644
index 00000000..96739de2
--- /dev/null
+++ b/test/monniaux/picosat-965/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 - 2014, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+
diff --git a/test/monniaux/picosat-965/Makefile b/test/monniaux/picosat-965/Makefile
new file mode 100644
index 00000000..4d6eee20
--- /dev/null
+++ b/test/monniaux/picosat-965/Makefile
@@ -0,0 +1,11 @@
+EXECUTE_ARGS=sudoku.sat
+ALL_CFLAGS = -DNALARM -DNZIP -DNGETRUSAGE -DNDEBUG
+ALL_CCOMPFLAGS += -fbitfields # -fno-if-conversion
+TARGET=picosat
+ALL_CFILES=picosat.c version.c app.c main.c
+
+include ../rules.mk
+
+# FIXME - what were these for?
+#KVX_CFLAGS += $(EMBEDDED_CFLAGS)
+#KVX_CCOMPFLAGS += $(EMBEDDED_CFLAGS)
diff --git a/test/monniaux/picosat-965/NEWS b/test/monniaux/picosat-965/NEWS
new file mode 100644
index 00000000..7ad60b53
--- /dev/null
+++ b/test/monniaux/picosat-965/NEWS
@@ -0,0 +1,162 @@
+news for release 965 since 959
+------------------------------
+
+* ADC code works again (spotted by Himanshu Jain)
+* include into R projects (with Christoph Muessel) (--rcode)
+* fixed 'undefined' + 'ptrdiff_' issues (thanks to Christoph Muessel)
+* added 'picosat_set_interrupt' and '-a <alarm>' command line option
+* fixed various issues pointed out by Stefan Hengelein:
+ - fixed incremental usage of 'picosat_adjust'
+ - added CPP fixes (STATS, NO_BINARY_CLAUSE versus TRACE mix-ups)
+ - removed redundant explicit set to zero on reset
+* fixed various usage bugs with 'picomus' (thanks to Stefan Hengelein)
+* removed '-fno-strict-aliasing' (thanks to Jerry James)
+
+news for release 959 since 953
+------------------------------
+
+* fixed header comments
+
+* fixed minor compilation issues
+
+* fixed unitialized memory access problem for 'picosat_deref_partial'
+ and another issue with partial models
+
+* added 'picosat_add_arg' and 'picosat_add_lits'
+
+* '--plain' and 'picosat_set_plain' to disable failed literal probing
+
+* new '#define PICOSAT_REENTRANT_API' in 'picosat.h'
+
+* added manager so no more global variables
+ (allows multiple instances, requires manager object)
+
+news for release 951 since 941
+------------------------------
+
+* cleaned up code (based on comments by Donald Knuth)
+
+* lreduce=O(conflicts^.5)
+
+* added 'picosat_visits' and 'picosat_decisions'
+
+* added '--partial' command line option
+
+* added 'picosat_deref_partial' and 'picosat_save_original_clauses'
+
+* added 'picomcs' as example for MSS computation
+
+news for release 941 since 936
+------------------------------
+
+* added 'picogcnf'
+
+* added All-SAT mode ('--all' command line option)
+
+* statistics include time spent in failed literal preprocessing (probing)
+
+* 'picosat_failed_context' for 'push & pop'
+ (and tested failed assumptions for 'push & pop')
+
+* 'picosat_simplify' for forced garbage collection
+
+* undefined NFL, defined NADC (= failed literals on, ADC's off)
+
+* 'picosat_push' and 'picosat_pop' (beta version)
+
+* fixed some issues related to binary clause handling and
+ generating list of failed assumptions
+
+news for release 936 since 935
+------------------------------
+
+* simple minimal unsatisfiable core (MUS) extractor 'picomus'
+ (example for using 'picosat_mus_assumptions' and 'picosat_coreclause')
+
+* added 'picosat_mus_assumptions'
+
+* added 'picosat_{set_}propagations'
+
+* new 'int' return value for 'picosat_enable_trace_generation' to
+ check for trace code being compiled
+
+news for release 935 since 926
+------------------------------
+
+* added 'picosat_failed_assumptions' (plural)
+
+* new '-A <failedlits>' command line option
+
+* fixed failed assumption issues
+
+* added 'picosat_remove_learned'
+
+* added 'picosat_reset_{phases,scores}'
+
+* added 'picosat_set_less_important_lit'
+
+* added 'picosat_res'
+
+news for release 926 since 846
+------------------------------
+
+* random initial phase (API of 'picosat_set_default_phase' changed)
+
+* fixed accumulative failed assumption (multiple times)
+
+* fixed missing original clause in core generation with assumptions
+
+* fixed debugging code for memory allocation
+
+* shared library in addition to static library
+
+* removed potential UNKNOWN result without decision limit
+
+* added picosat_set_more_important_lit
+
+* added picosat_coreclause
+
+* propagation of binary clauses until completion
+
+* fixed API usage 'assume;sat;sat'
+
+* literals move to front (LMTF) during traversal of visited clauses
+
+* switched from inner/outer to Luby style restart scheduling
+
+* less agressive reduce schedule
+
+* replaced watched literals with head and tail pointers
+
+* add 'picosat_failed_assumption', which allows to avoid tracing and core
+ generation, if one is only interested in assumptions in the core
+
+* fixed a BUG in the generic iterator code of clauses
+ (should rarely happen unless you use a very sophisticated malloc lib)
+
+news for release 846 since 632
+------------------------------
+
+* cleaned up assumption handling (actually removed buggy optimization)
+
+* incremental core generation
+
+* experimental 'all different constraint' handling as in our FMCAD'08 paper
+
+* new API calls:
+
+ - picosat_add_ado_lit (add all different object literal)
+ - picosat_deref_top_level (deref top level assignment)
+ - picosat_changed (check whether extension was possible)
+ - picosat_measure_all_calls (per default do not measure adding time)
+ - picosat_set_prefix (set prefix for messages)
+
+* 64 bit port (and compilation options)
+
+* optional NVSIDS visualization code
+
+* resource controlled failed literal implementation
+
+* disconnect long clauses satisfied at lower decision level
+
+* controlling restarts
diff --git a/test/monniaux/picosat-965/README b/test/monniaux/picosat-965/README
new file mode 100644
index 00000000..05b84396
--- /dev/null
+++ b/test/monniaux/picosat-965/README
@@ -0,0 +1,5 @@
+These are the sources of the PicoSAT solver.
+The preprocessor is not included.
+To compile run './configure.sh && make'.
+The API is document in 'picosat.h'.
+See also 'NEWS' and 'LICENSE'.
diff --git a/test/monniaux/picosat-965/VERSION b/test/monniaux/picosat-965/VERSION
new file mode 100644
index 00000000..aa5bea4e
--- /dev/null
+++ b/test/monniaux/picosat-965/VERSION
@@ -0,0 +1 @@
+965
diff --git a/test/monniaux/picosat-965/app.c b/test/monniaux/picosat-965/app.c
new file mode 100644
index 00000000..64ebdbd0
--- /dev/null
+++ b/test/monniaux/picosat-965/app.c
@@ -0,0 +1,1214 @@
+#include "picosat.h"
+
+#include <assert.h>
+#include <ctype.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define GUNZIP "gunzip -c %s"
+#define BUNZIP2 "bzcat %s"
+#define GZIP "gzip -c -f > %s"
+
+#ifndef NZIP
+FILE * popen (const char *, const char*);
+int pclose (FILE *);
+#endif
+
+static PicoSAT * picosat;
+
+static int lineno;
+static FILE *input;
+static int inputid;
+static FILE *output;
+static int verbose;
+static int sargc;
+static char ** sargv;
+static char buffer[100];
+static char *bhead = buffer;
+static const char *eob = buffer + 80;
+static FILE * incremental_rup_file;
+static signed char * sol;
+
+extern void picosat_enter (PicoSAT *);
+extern void picosat_leave (PicoSAT *);
+
+static int
+next (void)
+{
+ int res = getc (input);
+ if (res == '\n')
+ lineno++;
+
+ return res;
+}
+
+static const char *
+parse (PicoSAT * picosat, int force)
+{
+ int ch, sign, lit, vars, clauses;
+
+ lineno = 1;
+ /* DM inputid = fileno (input); */
+
+SKIP_COMMENTS:
+ ch = next ();
+ if (ch == 'c')
+ {
+ while ((ch = next ()) != EOF && ch != '\n')
+ ;
+ goto SKIP_COMMENTS;
+ }
+
+ if (isspace (ch))
+ goto SKIP_COMMENTS;
+
+ if (ch != 'p')
+INVALID_HEADER:
+ return "missing or invalid 'p cnf <variables> <clauses>' header";
+
+ if (!isspace (next ()))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (ch != 'c' || next () != 'n' || next () != 'f' || !isspace (next ()))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (!isdigit (ch))
+ goto INVALID_HEADER;
+
+ vars = ch - '0';
+ while (isdigit (ch = next ()))
+ vars = 10 * vars + (ch - '0');
+
+ if (!isspace (ch))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (!isdigit (ch))
+ goto INVALID_HEADER;
+
+ clauses = ch - '0';
+ while (isdigit (ch = next ()))
+ clauses = 10 * clauses + (ch - '0');
+
+ if (!isspace (ch) && ch != '\n' )
+ goto INVALID_HEADER;
+
+ if (verbose)
+ {
+ fprintf (output, "c parsed header 'p cnf %d %d'\n", vars, clauses);
+ fflush (output);
+ }
+
+ picosat_adjust (picosat, vars);
+
+ if (incremental_rup_file)
+ picosat_set_incremental_rup_file (picosat, incremental_rup_file, vars, clauses);
+
+ lit = 0;
+READ_LITERAL:
+ ch = next ();
+
+ if (ch == 'c')
+ {
+ while ((ch = next ()) != EOF && ch != '\n')
+ ;
+ goto READ_LITERAL;
+ }
+
+ if (ch == EOF)
+ {
+ if (lit)
+ return "trailing 0 missing";
+
+ if (clauses && !force)
+ return "clause missing";
+
+ return 0;
+ }
+
+ if (isspace (ch))
+ goto READ_LITERAL;
+
+ sign = 1;
+ if (ch == '-')
+ {
+ sign = -1;
+ ch = next ();
+ }
+
+ if (!isdigit (ch))
+ return "expected number";
+
+ lit = ch - '0';
+ while (isdigit (ch = next ()))
+ lit = 10 * lit + (ch - '0');
+
+ if (!clauses && !force)
+ return "too many clauses";
+
+ if (lit)
+ {
+ if (lit > vars && !force)
+ return "maximal variable index exceeded";
+
+ lit *= sign;
+ }
+ else
+ clauses--;
+
+ picosat_add (picosat, lit);
+
+ goto READ_LITERAL;
+}
+
+static void
+bflush (void)
+{
+ *bhead = 0;
+ fputs (buffer, output);
+ fputc ('\n', output);
+ bhead = buffer;
+}
+
+static void
+printi (int i)
+{
+ char *next;
+ int l;
+
+REENTER:
+ if (bhead == buffer)
+ *bhead++ = 'v';
+
+ l = sprintf (bhead, " %d", i);
+ next = bhead + l;
+
+ if (next >= eob)
+ {
+ bflush ();
+ goto REENTER;
+ }
+ else
+ bhead = next;
+}
+
+static void
+printa (PicoSAT * picosat, int partial)
+{
+ int max_idx = picosat_variables (picosat), i, lit, val;
+
+ assert (bhead == buffer);
+
+ for (i = 1; i <= max_idx; i++)
+ {
+ if (partial)
+ {
+ val = picosat_deref_partial (picosat, i);
+ if (!val)
+ continue;
+ }
+ else
+ val = picosat_deref (picosat, i);
+ lit = (val > 0) ? i : -i;
+ printi (lit);
+ }
+
+ printi (0);
+ if (bhead > buffer)
+ bflush ();
+}
+
+static void
+blocksol (PicoSAT * picosat)
+{
+ int max_idx = picosat_variables (picosat), i;
+
+ if (!sol)
+ {
+ sol = malloc (max_idx + 1);
+ memset (sol, 0, max_idx + 1);
+ }
+
+ for (i = 1; i <= max_idx; i++)
+ sol[i] = (picosat_deref (picosat, i) > 0) ? 1 : -1;
+
+ for (i = 1; i <= max_idx; i++)
+ picosat_add (picosat, (sol[i] < 0) ? i : -i);
+
+ picosat_add (picosat, 0);
+}
+
+static int
+has_suffix (const char *str, const char *suffix)
+{
+ const char *tmp = strstr (str, suffix);
+ if (!tmp)
+ return 0;
+
+ return str + strlen (str) - strlen (suffix) == tmp;
+}
+
+static void
+write_core_variables (PicoSAT * picosat, FILE * file)
+{
+ int i, max_idx = picosat_variables (picosat), count = 0;
+ for (i = 1; i <= max_idx; i++)
+ if (picosat_corelit (picosat, i))
+ {
+ fprintf (file, "%d\n", i);
+ count++;
+ }
+
+ if (verbose)
+ fprintf (output, "c found and wrote %d core variables\n", count);
+}
+
+static int
+next_assumption (int start)
+{
+ char * arg, c;
+ int res;
+ res = start + 1;
+ while (res < sargc)
+ {
+ arg = sargv[res++];
+ if (!strcmp (arg, "-a"))
+ {
+ assert (res < sargc);
+ break;
+ }
+
+ if (arg[0] == '-') {
+ c = arg[1];
+ if (c == 'l' || c == 'i' || c == 's' || c == 'o' || c == 't' ||
+ c == 'T' || c == 'r' || c == 'R' || c == 'c' || c == 'V' ||
+ c == 'U' || c == 'A') res++;
+ }
+ }
+ if (res >= sargc) res = 0;
+ return res;
+}
+
+static void
+write_failed_assumptions (PicoSAT * picosat, FILE * file)
+{
+ int i, lit, count = 0;
+#ifndef NDEBUG
+ int max_idx = picosat_variables (picosat);
+#endif
+ i = 0;
+ while ((i = next_assumption (i))) {
+ lit = atoi (sargv[i]);
+ if (!picosat_failed_assumption (picosat, lit)) continue;
+ fprintf (file, "%d\n", lit);
+ count++;
+ }
+ if (verbose)
+ fprintf (output, "c found and wrote %d failed assumptions\n", count);
+#ifndef NDEBUG
+ for (i = 1; i <= max_idx; i++)
+ if (picosat_failed_assumption (picosat, i))
+ count--;
+#endif
+ assert (!count);
+}
+
+static void
+write_to_file (PicoSAT * picosat,
+ const char *name,
+ const char *type,
+ void (*writer) (PicoSAT *, FILE *))
+{
+ int pclose_file, zipped = has_suffix (name, ".gz");
+ FILE *file;
+ char *cmd;
+
+ if (zipped)
+ {
+#ifdef NZIP
+ file = NULL;
+#else
+ cmd = malloc (strlen (GZIP) + strlen (name));
+ sprintf (cmd, GZIP, name);
+ file = popen (cmd, "w");
+ free (cmd);
+ pclose_file = 1;
+#endif
+ }
+ else
+ {
+ file = fopen (name, "w");
+ pclose_file = 0;
+ }
+
+ if (file)
+ {
+ if (verbose)
+ fprintf (output,
+ "c\nc writing %s%s to '%s'\n",
+ zipped ? "gzipped " : "", type, name);
+
+ writer (picosat, file);
+
+#ifndef NZIP
+ if (pclose_file)
+ pclose (file);
+ else
+#endif
+ fclose (file);
+ }
+ else
+ fprintf (output, "*** picosat: can not write to '%s'\n", name);
+}
+
+static int catched;
+
+static void (*sig_int_handler);
+static void (*sig_segv_handler);
+static void (*sig_abrt_handler);
+static void (*sig_term_handler);
+#ifndef NALLSIGNALS
+static void (*sig_kill_handler);
+static void (*sig_xcpu_handler);
+static void (*sig_xfsz_handler);
+#endif
+
+static void
+resetsighandlers (void)
+{
+ (void) signal (SIGINT, sig_int_handler);
+ (void) signal (SIGSEGV, sig_segv_handler);
+ (void) signal (SIGABRT, sig_abrt_handler);
+ (void) signal (SIGTERM, sig_term_handler);
+#ifndef NALLSIGNALS
+ (void) signal (SIGKILL, sig_kill_handler);
+ (void) signal (SIGXCPU, sig_xcpu_handler);
+ (void) signal (SIGXFSZ, sig_xfsz_handler);
+#endif
+}
+
+static int time_limit_in_seconds;
+static void (*sig_alarm_handler);
+static int ought_to_be_interrupted, interrupt_notified;
+
+static void
+alarm_triggered (int sig)
+{
+ (void) sig;
+ assert (sig == SIGALRM);
+ assert (time_limit_in_seconds);
+ assert (!ought_to_be_interrupted);
+ ought_to_be_interrupted = 1;
+ assert (!interrupt_notified);
+}
+
+static int
+interrupt_call_back (void * dummy)
+{
+ (void) dummy;
+ if (!ought_to_be_interrupted)
+ return 0;
+ if (!interrupt_notified)
+ {
+ if (verbose)
+ {
+ picosat_message (picosat, 1, "");
+ picosat_message (picosat, 1,
+ "*** TIME LIMIT OF %d SECONDS REACHED ***",
+ time_limit_in_seconds);
+ picosat_message (picosat, 1, "");
+ }
+ interrupt_notified = 1;
+ }
+ return 1;
+}
+
+static void
+setalarm ()
+{
+#ifndef NALARM
+ assert (time_limit_in_seconds > 0);
+ sig_alarm_handler = signal (SIGALRM, alarm_triggered);
+ alarm (time_limit_in_seconds);
+ assert (picosat);
+ picosat_set_interrupt (picosat, 0, interrupt_call_back);
+#endif
+}
+
+static void
+resetalarm ()
+{
+ assert (time_limit_in_seconds > 0);
+ (void) signal (SIGALRM, sig_term_handler);
+}
+
+static void
+message (int sig)
+{
+ picosat_message (picosat, 1, "");
+ picosat_message (picosat, 1, "*** CAUGHT SIGNAL %d ***", sig);
+ picosat_message (picosat, 1, "");
+}
+
+static void
+catch (int sig)
+{
+ if (!catched)
+ {
+ message (sig);
+ catched = 1;
+ picosat_stats (picosat);
+ message (sig);
+ }
+
+ resetsighandlers ();
+ raise (sig);
+}
+
+static void
+setsighandlers (void)
+{
+ sig_int_handler = signal (SIGINT, catch);
+ sig_segv_handler = signal (SIGSEGV, catch);
+ sig_abrt_handler = signal (SIGABRT, catch);
+ sig_term_handler = signal (SIGTERM, catch);
+#ifndef NALLSIGNALS
+ sig_kill_handler = signal (SIGKILL, catch);
+ sig_xcpu_handler = signal (SIGXCPU, catch);
+ sig_xfsz_handler = signal (SIGXFSZ, catch);
+#endif
+}
+
+#define USAGE \
+"usage: picosat [ <option> ... ] [ <input> ]\n" \
+"\n" \
+"where <option> is one of the following\n" \
+"\n" \
+" -h print this command line option summary and exit\n" \
+" --version print version and exit\n" \
+" --config print build configuration and exit\n" \
+"\n" \
+" -v enable verbose output\n" \
+" -f ignore invalid header\n" \
+" -n do not print satisfying assignment\n" \
+" -p print formula in DIMACS format and exit\n" \
+" --plain disable preprocessing (failed literal probing)\n" \
+" -a <lit> start with an assumption\n" \
+" -l <limit> set decision limit (no limit per default)\n" \
+" -L <limit> set time limit in seconds (no limit per default)\n" \
+" -P <limit> set propagation limit (no limit per default)\n" \
+" -i [0-3] [0-3]=[FALSE,TRUE,JWH,RAND] initial phase (default 2=JWH)\n" \
+" -s <seed> set random number generator seed (default 0)\n" \
+" -o <output> set output file (<stdout> per default)\n" \
+" -t <trace> generate compact proof trace file\n" \
+" -T <trace> generate extended proof trace file\n" \
+" -r <trace> generate reverse unit propagation proof file\n" \
+" -R <trace> generate reverse unit propagation proof file incrementally\n" \
+" -c <core> generate clausal core file in DIMACS format\n" \
+" -V <core> generate file listing core variables\n" \
+" -U <core> generate file listing used variables\n" \
+" -A <core> generate file listing failed assumptions\n" \
+"\n" \
+" --all enumerate all solutions\n" \
+" --partial generate and print only partial assignment\n" \
+"\n" \
+"and <input> is an optional input file in DIMACS format.\n"
+
+int
+picosat_main (int argc, char **argv)
+{
+ int res, done, err, print_satisfying_assignment, force, print_formula;
+ const char *compact_trace_name, *extended_trace_name, * rup_trace_name;
+ int assumption, assumptions, defaultphase, allsat, partial, plain;
+ const char * clausal_core_name, * variable_core_name;
+ const char *input_name, *output_name;
+ const char * failed_assumptions_name;
+ int close_input, pclose_input;
+ long long propagation_limit;
+ int i, decision_limit;
+ double start_time;
+ long long sols;
+ unsigned seed;
+ FILE *file;
+ int trace;
+
+ start_time = picosat_time_stamp ();
+
+ sargc = argc;
+ sargv = argv;
+
+ clausal_core_name = 0;
+ variable_core_name = 0;
+ failed_assumptions_name = 0;
+ output_name = 0;
+ compact_trace_name = 0;
+ extended_trace_name = 0;
+ rup_trace_name = 0;
+ incremental_rup_file = 0;
+ close_input = 0;
+ pclose_input = 0;
+ input_name = "<stdin>";
+ input = stdin;
+ output = stdout;
+ verbose = 0;
+ done = err = 0;
+ decision_limit = -1;
+ propagation_limit = -1;
+ defaultphase = 2;
+ assumptions = 0;
+ force = 0;
+ allsat = 0;
+ partial = 0;
+ trace = 0;
+ plain = 0;
+ seed = 0;
+ sols= 0;
+
+ picosat = 0;
+
+ print_satisfying_assignment = 1;
+ print_formula = 0;
+
+ for (i = 1; !done && !err && i < argc; i++)
+ {
+ if (!strcmp (argv[i], "-h"))
+ {
+ fputs (USAGE, output);
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "--version"))
+ {
+ fprintf (output, "%s\n", picosat_version ());
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "--config"))
+ {
+ fprintf (output, "%s\n", picosat_config ());
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "-v"))
+ {
+ verbose++;
+ }
+ else if (!strcmp (argv[i], "--plain"))
+ {
+ plain = 1;
+ }
+ else if (!strcmp (argv[i], "-f"))
+ {
+ force = 1;
+ }
+ else if (!strcmp (argv[i], "-n"))
+ {
+ print_satisfying_assignment = 0;
+ }
+ else if (!strcmp (argv[i], "--partial"))
+ {
+ partial = 1;
+ }
+ else if (!strcmp (argv[i], "-p"))
+ {
+ print_formula = 1;
+ }
+ else if (!strcmp (argv[i], "-l"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-l' missing\n");
+ err = 1;
+ }
+ else
+ decision_limit = atoi (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-L"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-L' missing\n");
+ err = 1;
+ }
+ else
+ {
+ time_limit_in_seconds = atoi (argv[i]);
+ if (time_limit_in_seconds <= 0)
+ {
+ fprintf (output, "*** picosat: invalid '-L' argument\n");
+ err = 1;
+ }
+ }
+ }
+ else if (!strcmp (argv[i], "-P"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-P' missing\n");
+ err = 1;
+ }
+ else
+ propagation_limit = atol /* DM */ (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-i"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-i' missing\n");
+ err = 1;
+ }
+ else if (!argv[i][1] && ('0' <= argv[i][0] && argv[i][0] <= '3'))
+ {
+ defaultphase = argv[i][0] - '0';
+ }
+ else
+ {
+ fprintf (output, "*** picosat: invalid argument to '-i'\n");
+ err = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-a"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-a' missing\n");
+ err = 1;
+ }
+ else if (!atoi (argv[i]))
+ {
+ fprintf (output, "*** picosat: argument to '-a' zero\n");
+ err = 1;
+ }
+ else
+ {
+ /* Handle assumptions further down
+ */
+ assumptions++;
+ }
+ }
+ else if (!strcmp (argv[i], "--all"))
+ {
+ allsat = 1;
+ }
+ else if (!strcmp (argv[i], "-s"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-s' missing\n");
+ err = 1;
+ }
+ else
+ seed = atoi (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-o"))
+ {
+ if (output_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple output files '%s' and '%s'\n",
+ output_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-o' missing\n");
+ err = 1;
+ }
+ else if (!(file = fopen (argv[i], "w")))
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not write output file '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ output_name = argv[i];
+ output = file;
+ }
+ }
+ else if (!strcmp (argv[i], "-t"))
+ {
+ if (compact_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple compact trace files '%s' and '%s'\n",
+ compact_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-t' missing\n");
+ err = 1;
+ }
+ else
+ {
+ compact_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-T"))
+ {
+ if (extended_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple extended trace files '%s' and '%s'\n",
+ extended_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-T' missing\n");
+ err = 1;
+ }
+ else
+ {
+ extended_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-r"))
+ {
+ if (rup_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple RUP trace files '%s' and '%s'\n",
+ rup_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-r' missing\n");
+ err = 1;
+ }
+ else
+ {
+ rup_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-R"))
+ {
+ if (rup_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple RUP trace files '%s' and '%s'\n",
+ rup_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-R' missing\n");
+ err = 1;
+ }
+ else if (!(file = fopen (argv[i], "w")))
+ {
+ fprintf (output,
+ "*** picosat: can not write to '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ rup_trace_name = argv[i];
+ incremental_rup_file = file;
+ }
+ }
+ else if (!strcmp (argv[i], "-c"))
+ {
+ if (clausal_core_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple clausal core files '%s' and '%s'\n",
+ clausal_core_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-c' missing\n");
+ err = 1;
+ }
+ else
+ {
+ clausal_core_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-V"))
+ {
+ if (variable_core_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple variable core files '%s' and '%s'\n",
+ variable_core_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-V' missing\n");
+ err = 1;
+ }
+ else
+ {
+ variable_core_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-A"))
+ {
+ if (failed_assumptions_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple failed assumptions files '%s' and '%s'\n",
+ failed_assumptions_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-A' missing\n");
+ err = 1;
+ }
+ else
+ failed_assumptions_name = argv[i];
+ }
+ else if (argv[i][0] == '-')
+ {
+ fprintf (output,
+ "*** picosat: "
+ "unknown command line option '%s' (try '-h')\n", argv[i]);
+ err = 1;
+ }
+ else if (close_input || pclose_input)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple input files '%s' and '%s'\n",
+ input_name, argv[i]);
+ err = 1;
+ }
+ else if (has_suffix (argv[i], ".gz"))
+ {
+#ifdef NZIP
+ file=NULL;
+ err=1;
+#else
+ char *cmd = malloc (strlen (GUNZIP) + strlen (argv[i]));
+ sprintf (cmd, GUNZIP, argv[i]);
+ if ((file = popen (cmd, "r")))
+ {
+ input_name = argv[i];
+ pclose_input = 1;
+ input = file;
+ }
+ else
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not read compressed input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ free (cmd);
+#endif
+ }
+ else if (has_suffix (argv[i], ".bz2"))
+ {
+#ifdef NZIP
+ file=NULL;
+ err=1;
+#else
+ char *cmd = malloc (strlen (BUNZIP2) + strlen (argv[i]));
+ sprintf (cmd, BUNZIP2, argv[i]);
+ if ((file = popen (cmd, "r")))
+ {
+ input_name = argv[i];
+ pclose_input = 1;
+ input = file;
+ }
+ else
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not read compressed input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ free (cmd);
+#endif
+ }
+ else if (!(file = fopen (argv[i], "r"))) /* TODO .gz ? */
+ {
+ fprintf (output,
+ "*** picosat: can not read input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ input_name = argv[i];
+ close_input = 1;
+ input = file;
+ }
+ }
+
+ if (allsat && partial)
+ {
+ fprintf (output,
+ "*** picosat: can not combine '--all' and '--partial'");
+ err = 1;
+ }
+
+ res = PICOSAT_UNKNOWN;
+
+ if (!done && !err)
+ {
+ const char *err_msg;
+
+ if (verbose)
+ {
+ fprintf (output,
+ "c PicoSAT SAT Solver Version %s\n",
+ picosat_version ());
+
+ fprintf (output, "c %s\n", picosat_copyright ());
+ fprintf (output, "c %s\n", picosat_config ());
+ }
+
+ picosat = picosat_init ();
+
+ if (verbose)
+ setsighandlers ();
+
+ if (time_limit_in_seconds)
+ setalarm ();
+
+ picosat_enter (picosat);
+
+ if (output_name)
+ picosat_set_output (picosat, output);
+
+ picosat_set_verbosity (picosat, verbose);
+ picosat_set_plain (picosat, plain);
+
+ if (verbose) fputs ("c\n", output);
+
+ if (trace)
+ {
+ if (verbose)
+ fprintf (output, "c tracing proof\n");
+ picosat_enable_trace_generation (picosat);
+ }
+
+ if (defaultphase)
+ {
+ if (verbose)
+ fprintf (output, "c using %d as default phase\n", defaultphase);
+ picosat_set_global_default_phase (picosat, defaultphase);
+ }
+
+ if (propagation_limit >= 0)
+ {
+ if (verbose)
+ fprintf (output, "c propagation limit of %lld propagations\n",
+ propagation_limit);
+ picosat_set_propagation_limit (picosat,
+ (unsigned long long) propagation_limit);
+ }
+
+ if (partial)
+ {
+ if (verbose)
+ fprintf (output,
+ "c saving original clauses for partial assignment\n");
+
+ picosat_save_original_clauses (picosat);
+ }
+
+ if (verbose)
+ fprintf (output, "c\nc parsing %s\n", input_name);
+
+ if (verbose)
+ fflush (output);
+
+ if ((err_msg = parse (picosat, force)))
+ {
+ fprintf (output, "%s:%d: %s\n", input_name, lineno, err_msg);
+ err = 1;
+ }
+ else
+ {
+NEXT_SOLUTION:
+ if (assumptions)
+ {
+ i = 0;
+ while ((i = next_assumption (i)))
+ {
+ assert (i < argc);
+ assumption = atoi (argv[i]);
+ assert (assumption);
+
+ picosat_assume (picosat, assumption);
+
+ if (verbose)
+ fprintf (output, "c assumption %d\n", assumption);
+ }
+ }
+
+ if (print_formula)
+ {
+ picosat_print (picosat, output);
+ }
+ else
+ {
+ if (verbose)
+ fprintf (output,
+ "c initialized %u variables\n"
+ "c found %u non trivial clauses\n",
+ picosat_variables (picosat),
+ picosat_added_original_clauses (picosat));
+
+ picosat_set_seed (picosat, seed);
+ if (verbose)
+ fprintf (output,
+ "c\nc random number generator seed %u\n",
+ seed);
+
+ res = picosat_sat (picosat, decision_limit);
+
+ if (res == PICOSAT_UNSATISFIABLE)
+ {
+
+ if (allsat)
+ fprintf (output, "s SOLUTIONS %lld\n", sols);
+ else
+ fputs ("s UNSATISFIABLE\n", output);
+
+ fflush (output);
+
+ if (compact_trace_name)
+ write_to_file (picosat,
+ compact_trace_name,
+ "compact trace",
+ picosat_write_compact_trace);
+
+ if (extended_trace_name)
+ write_to_file (picosat,
+ extended_trace_name,
+ "extended trace",
+ picosat_write_extended_trace);
+
+ if (!incremental_rup_file && rup_trace_name)
+ write_to_file (picosat,
+ rup_trace_name,
+ "rup trace",
+ picosat_write_rup_trace);
+
+ if (clausal_core_name)
+ write_to_file (picosat,
+ clausal_core_name,
+ "clausal core",
+ picosat_write_clausal_core);
+
+ if (variable_core_name)
+ write_to_file (picosat,
+ variable_core_name,
+ "variable core",
+ write_core_variables);
+
+ if (failed_assumptions_name)
+ write_to_file (picosat,
+ failed_assumptions_name,
+ "failed assumptions",
+ write_failed_assumptions);
+ }
+ else if (res == PICOSAT_SATISFIABLE)
+ {
+ if (allsat)
+ {
+ sols++;
+ if (verbose)
+ fprintf (output, "c\nc solution %lld\nc\n", sols);
+ }
+
+ if (!allsat || print_satisfying_assignment)
+ fputs ("s SATISFIABLE\n", output);
+
+ if (!allsat || verbose || print_satisfying_assignment)
+ fflush (output);
+
+ if (print_satisfying_assignment)
+ printa (picosat, partial);
+
+ if (allsat)
+ {
+ blocksol (picosat);
+ goto NEXT_SOLUTION;
+ }
+ }
+ else
+ {
+ fputs ("s UNKNOWN\n", output);
+
+ if (allsat && verbose)
+ fprintf (output,
+ "c\nc limit reached after %lld solutions\n",
+ sols);
+ fflush (output);
+ }
+ }
+ }
+
+ if (!err && verbose)
+ {
+ fputs ("c\n", output);
+ picosat_stats (picosat);
+ fprintf (output,
+ "c %.1f seconds total run time\n",
+ picosat_time_stamp () - start_time);
+ }
+
+ if (sol)
+ {
+ free (sol);
+ sol = 0;
+ }
+
+ picosat_leave (picosat);
+
+ if (time_limit_in_seconds)
+ resetalarm ();
+
+ if (verbose)
+ resetsighandlers ();
+
+ picosat_reset (picosat);
+ }
+
+ if (incremental_rup_file)
+ fclose (incremental_rup_file);
+
+ if (close_input)
+ fclose (input);
+
+#ifndef NZIP
+ if (pclose_input)
+ pclose (input);
+#endif
+
+ if (output_name)
+ fclose (output);
+
+ return res;
+}
diff --git a/test/monniaux/picosat-965/config.h b/test/monniaux/picosat-965/config.h
new file mode 100644
index 00000000..36ffc6b6
--- /dev/null
+++ b/test/monniaux/picosat-965/config.h
@@ -0,0 +1,3 @@
+#define PICOSAT_CC "../../../ccomp"
+#define PICOSAT_CFLAGS "-fall -Wall -fno-unprototyped -O3 -DNALARM -DNZIP -DNGETRUSAGE"
+#define PICOSAT_VERSION "965"
diff --git a/test/monniaux/picosat-965/configure.sh b/test/monniaux/picosat-965/configure.sh
new file mode 100755
index 00000000..a82cfe87
--- /dev/null
+++ b/test/monniaux/picosat-965/configure.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+
+satcompetition=no
+
+log=no
+debug=no
+stats=undefined
+trace=undefined
+static=yes
+shared=no
+thirtytwobit=no
+static=no
+rcode=no
+
+while [ $# -gt 0 ]
+do
+ case $1 in
+ -g|--debug) debug=yes;;
+ -O|--optimize) debug=no;;
+ -l|--log) log=yes;;
+ -s|--stats) stats=yes;;
+ -t|--trace) trace=yes;;
+ --no-stats) stats=no;;
+ --no-trace) trace=no;;
+ --no-rcode) rcode=no;;
+ --rcode) rcode=yes;;
+ -32|--32|-m32) thirtytwobit=yes;;
+ -static|--static) static=yes;;
+ -shared|--shared) shared=yes;;
+ *) cat <<EOF
+usage: ./configure.sh [<option> ...]
+
+where <option> is one of the following:
+
+ -g|--debug include debugging code and symbols
+ -O|--optimize optimized compilation (default)
+ -l|--log add low level logging code (default with '-g')
+ -s|--stats more expensive statististcs (default with '-g')
+ -t|--trace trace generation (more memory, default with '-g')
+ --no-stats disable expensive stats
+ --no-trace enable trace generation
+ -32|--32|-m32 compile for 32 bit machine even on 64 bit host
+ -rcode|--no-rcode enable/disable compatibility for used in R exension
+ -static|--static produce static binary
+ -shared|--shared produce shared library
+EOF
+exit 1
+;;
+ esac
+shift
+done
+
+echo "version ... `cat VERSION`"
+
+if [ $satcompetition = yes ]
+then
+ debug=no
+ stats=no
+ trace=no
+ thirtytwobit=yes
+ static=yes
+ shared=no
+fi
+
+echo "debug ... $debug"
+echo "log ... $log"
+
+[ $stats = undefined ] && stats=$debug
+echo "stats ... $stats"
+
+[ $trace = undefined ] && trace=$debug
+echo "trace ... $trace"
+
+echo "static ... $static"
+
+echo "shared ... $shared"
+
+[ "X$CC" = X ] && CC=gcc
+
+if [ X"$CFLAGS" = X ]
+then
+ case X"$CC" in
+ *wine*|*mingw*) CFLAGS="-DNGETRUSAGE -DNALLSIGNALS";;
+ *);;
+ esac
+ [ $log = yes ] && CFLAGS="$CFLAGS -DLOGGING"
+ [ $stats = yes ] && CFLAGS="$CFLAGS -DSTATS"
+ [ $trace = yes ] && CFLAGS="$CFLAGS -DTRACE"
+ [ $static = yes ] && CFLAGS="$CFLAGS -static"
+ [ $rcode = yes ] && CFLAGS="$CFLAGS -DRCODE"
+ case X"$CC" in
+ X*gcc*)
+ CFLAGS="$CFLAGS -Wall -Wextra"
+ [ $thirtytwobit = yes ] && CFLAGS="$CFLAGS -m32"
+ if [ $debug = yes ]
+ then
+ CFLAGS="$CFLAGS -g3 -ggdb"
+ else
+ CFLAGS="$CFLAGS -DNDEBUG -O3"
+ fi
+ ;;
+ *)
+ if [ $debug = yes ]
+ then
+ CFLAGS="$CFLAGS -g"
+ else
+ CFLAGS="$CFLAGS -O"
+ fi
+ ;;
+ esac
+fi
+
+if [ $rcode = yes ]
+then
+ for rdoth in /usr/share/R/include/R.h $RINC undefined
+ do
+ [ -f $rdoth ] && break
+ done
+ if [ $rdoth = undefined ]
+ then
+ echo "R.h ... not found (add '-I' manually or 'RHEADER=... ./configure')"
+ else
+ RINC="-I`dirname $rdoth`"
+ CFLAGS="$CFLAGS $RINC"
+ echo "R.h ... added '$RINC' include directive"
+ fi
+ TARGETS="libpicosat.a"
+else
+ TARGETS="picosat picomcs picomus picogcnf libpicosat.a"
+fi
+
+if [ $shared = yes ]
+then
+ TARGETS="$TARGETS libpicosat.so"
+ CFLAGS="$CFLAGS -fPIC"
+fi
+echo "targets ... $TARGETS"
+
+echo "cc ... $CC"
+
+echo "cflags ... $CFLAGS"
+
+printf "makefile ..."
+rm -f makefile
+sed \
+ -e "s,@CC@,$CC," \
+ -e "s,@CFLAGS@,$CFLAGS," \
+ -e "s,@TARGETS@,$TARGETS," \
+makefile.in > makefile
+echo " done"
diff --git a/test/monniaux/picosat-965/main.c b/test/monniaux/picosat-965/main.c
new file mode 100644
index 00000000..13d7b0e5
--- /dev/null
+++ b/test/monniaux/picosat-965/main.c
@@ -0,0 +1,25 @@
+#define VERIMAG_MEASUREMENTS
+#ifdef VERIMAG_MEASUREMENTS
+#include "../clock.h"
+#endif
+
+int picosat_main (int, char **);
+
+int
+main (int argc, char **argv)
+{
+
+#ifdef VERIMAG_MEASUREMENTS
+ clock_prepare();
+ clock_start();
+#endif
+
+ int ret= picosat_main (argc, argv);
+
+#ifdef VERIMAG_MEASUREMENTS
+ clock_stop();
+ print_total_clock();
+#endif
+
+ return ret;
+}
diff --git a/test/monniaux/picosat-965/mkconfig.sh b/test/monniaux/picosat-965/mkconfig.sh
new file mode 100755
index 00000000..621210b9
--- /dev/null
+++ b/test/monniaux/picosat-965/mkconfig.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+die () {
+ echo "*** mkconfig.sh: $*" 1>&2
+ exit 1
+}
+
+[ -f makefile ] || die "can not find 'makefile'"
+
+sed \
+ -e '/^C[A-Z]*=/!d' \
+ -e 's,^,#define PICOSAT_,' \
+ -e 's,= *, ",' \
+ -e 's,$,",' \
+ makefile
+
+id=""
+if [ -d .git -a -f .git/HEAD ]
+then
+ head="`awk 'NF == 1' .git/HEAD`"
+ if [ x"$head" = x ]
+ then
+ head="`awk '{print $2}' .git/HEAD`"
+ if [ ! x"$head" = x -a -f ".git/$head" ]
+ then
+ id=" `cat .git/$head`"
+ fi
+ else
+ id=" $head"
+ fi
+fi
+
+echo "#define PICOSAT_VERSION \"`cat VERSION`$id\""
+
+exit 0
diff --git a/test/monniaux/picosat-965/onefile/picosat.c b/test/monniaux/picosat-965/onefile/picosat.c
new file mode 100644
index 00000000..e1c18438
--- /dev/null
+++ b/test/monniaux/picosat-965/onefile/picosat.c
@@ -0,0 +1,25 @@
+typedef struct b b;
+b *a;
+struct b {
+ int c;
+ int d, **clshead;
+ int **ahead;
+ unsigned h;
+} i;
+b *j();
+int k();
+int main() {
+ a = j();
+ k(a);
+}
+#define e(f) f - g->c
+static void m(b *g, int *l) {
+ if (g)
+ *g->ahead = l;
+}
+b *j() { return &i; }
+int k(b *g) {
+ if (g->d)
+ m(g, e(g->clshead[-1]));
+ return g->h;
+}
diff --git a/test/monniaux/picosat-965/onefile/testcmp.sh b/test/monniaux/picosat-965/onefile/testcmp.sh
new file mode 100755
index 00000000..2228c675
--- /dev/null
+++ b/test/monniaux/picosat-965/onefile/testcmp.sh
@@ -0,0 +1,146 @@
+DEFINES="-DNALARM -DNZIP -DNGETRUSAGE -DNDEBUG"
+COMPCERT=/local/monniaux/Kalray/mppa-RTLpathSE-verif-hash-junk
+DATA=$COMPCERT/test/monniaux/picosat-965/tiny.dat
+CCOMP="$COMPCERT/ccomp -fbitfields -fduplicate 2 -fall-loads-nontrap $DEFINES"
+GCC="kvx-cos-gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC0="gcc -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC1="gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC2="gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror -fsanitize=undefined -fsanitize=address $DEFINES"
+HOSTCC3="gcc -O3 -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC4="clang -Wimplicit -Wuninitialized -Werror $DEFINES"
+HOSTCC5="clang -Wimplicit -Wuninitialized -Werror -fsanitize=undefined -fsanitize=address $DEFINES"
+CFILES="picosat.c"
+SIMU="kvx-cluster --timeout=100000 -- "
+
+if ! $HOSTCC0 $CFILES -o picosat.cc0.host ;
+then exit 30 ;
+fi
+
+if ! $HOSTCC1 $CFILES -o picosat.cc1.host ;
+then exit 31 ;
+fi
+
+if ! $HOSTCC2 $CFILES -o picosat.cc2.host ;
+then exit 32 ;
+fi
+
+if ! $HOSTCC3 $CFILES -o picosat.cc3.host ;
+then exit 33 ;
+fi
+
+if ! $HOSTCC4 $CFILES -o picosat.cc4.host ;
+then exit 34 ;
+fi
+
+if ! $HOSTCC5 $CFILES -o picosat.cc5.host ;
+then exit 35 ;
+fi
+
+timeout 1 ./picosat.cc0.host $DATA 2>&1 > picosat.cc0.out
+if [ $? -ge 100 ];
+then exit 40 ;
+fi
+
+timeout 1 ./picosat.cc1.host $DATA 2>&1 > picosat.cc1.out
+if [ $? -ge 100 ];
+then exit 41 ;
+fi
+
+timeout 1 valgrind --log-file=picosat.cc0.valgrind.log ./picosat.cc0.host $DATA 2>&1 > picosat.cc0.valgrind.out
+if [ $? -ge 100 ];
+then exit 50 ;
+fi
+
+timeout 1 valgrind --log-file=picosat.cc1.valgrind.log ./picosat.cc1.host $DATA 2>&1 > picosat.cc1.valgrind.out
+if [ $? -ge 100 ];
+then exit 51 ;
+fi
+
+timeout 1 ./picosat.cc2.host $DATA 2>&1 > picosat.cc2.out
+if [ $? -ge 100 ];
+then exit 42 ;
+fi
+
+timeout 1 ./picosat.cc3.host $DATA 2>&1 > picosat.cc3.out
+if [ $? -ge 100 ];
+then exit 43 ;
+fi
+
+timeout 1 ./picosat.cc4.host $DATA 2>&1 > picosat.cc4.out
+if [ $? -ge 100 ];
+then exit 44 ;
+fi
+
+timeout 1 ./picosat.cc5.host $DATA 2>&1 > picosat.cc5.out
+if [ $? -ge 100 ];
+then exit 45 ;
+fi
+
+if ! cmp picosat.cc0.out picosat.cc1.out ;
+then exit 60 ;
+fi
+
+if ! cmp picosat.cc0.out picosat.cc0.valgrind.out ;
+then exit 70 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc1.valgrind.out ;
+then exit 61 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc2.out ;
+then exit 62 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc3.out ;
+then exit 63 ;
+fi
+
+if ! $GCC $CFILES -o picosat.gcc.target ;
+then exit 1 ;
+fi
+
+if ! $CCOMP $CFILES -o picosat.ccomp.target ;
+then exit 2 ;
+fi
+
+if ! $CCOMP -fprepass -fprepass= list $CFILES -o picosat.prepass.target ;
+then exit 3 ;
+fi
+
+$SIMU ./picosat.gcc.target $DATA 2>&1 > picosat.gcc.out
+if [ $? -ge 100 ];
+then exit 4 ;
+fi
+
+if ! cmp picosat.gcc.out picosat.cc1.out ;
+then exit 13 ;
+fi
+
+if grep timeout picosat.gcc.out ;
+then exit 8 ;
+fi
+
+$SIMU ./picosat.ccomp.target $DATA 2>&1 > picosat.ccomp.out
+if [ $? -ge 100 ];
+then exit 5 ;
+fi
+
+if grep timeout picosat.ccomp.out ;
+then exit 9 ;
+fi
+
+if ! cmp picosat.gcc.out picosat.ccomp.out ;
+then exit 6 ;
+fi
+
+$SIMU ./picosat.prepass.target $DATA 2>&1 > picosat.prepass.out
+if [ $? -ge 100 ];
+then exit 0 ;
+fi
+
+if cmp picosat.gcc.out picosat.prepass.out ;
+then exit 7 ;
+fi
+
+exit 0
diff --git a/test/monniaux/picosat-965/picogcnf.c b/test/monniaux/picosat-965/picogcnf.c
new file mode 100644
index 00000000..4d91bfae
--- /dev/null
+++ b/test/monniaux/picosat-965/picogcnf.c
@@ -0,0 +1,165 @@
+/****************************************************************************
+Copyright (c) 2011-2012, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include "picosat.h"
+
+#include <stdio.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <limits.h>
+
+#if 1
+#define LOG(ARGS...) do { } while (0)
+#else
+#define LOG(ARGS...) do { printf (##ARGS); } while (0)
+#endif
+
+static int reductions, ngroups;
+
+static PicoSAT * ps;
+
+static void die (const char * fmt, ...) {
+ va_list ap;
+ fprintf (stderr, "*** picogcnf: ");
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stderr);
+ exit (1);
+}
+
+static void msg (const char * fmt, ...) {
+ va_list ap;
+ printf ("c [picogcnf] %.2f seconds: ", picosat_time_stamp ());
+ va_start (ap, fmt);
+ vprintf (fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static double percent (double a, double b) { return b?100.0*a/b:0.0; }
+
+static void callback (void * dummy, const int * mus) {
+ int remaining;
+ const int * p;
+ (void) dummy;
+ remaining = 0;
+ for (p = mus; *p; p++) remaining++;
+ assert (remaining <= ngroups);
+ msg ("<%d> reduction to %d out of %d (%.0f%%)",
+ ++reductions, remaining, ngroups, percent (remaining, ngroups));
+}
+
+int main (int argc, char ** argv) {
+ int ch, nvars, sclauses, nclauses, sign, lit, group, res;
+ const int * mus, * p;
+ FILE * file;
+ if (argc != 2) die ("usage: picogcnf <gcnf-file>");
+ if (!(file = fopen (argv[1], "r"))) die ("can not read '%s'", argv[1]);
+ ps = picosat_init ();
+HEADER:
+ ch = getc (file);
+ if (ch == 'c') {
+ while ((ch = getc (file)) != '\n')
+ if (ch == EOF) die ("unexpected EOF");
+ goto HEADER;
+ }
+ if (ch != 'p' ||
+ getc (file) != ' ' ||
+ fscanf (file, "gcnf %d %d %d", &nvars, &sclauses, &ngroups) != 3)
+ die ("invalid header");
+ nclauses = lit = 0;
+ group = INT_MAX;
+ LOG ("p gcnf %d %d %d\n", nvars, sclauses, ngroups);
+LIT:
+ ch = getc (file);
+ if (ch == EOF) {
+ if (lit) die ("zero missing");
+ if (nclauses < sclauses) die ("clauses missing");
+ goto DONE;
+ }
+ if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') goto LIT;
+ if (lit) {
+ if (ch == '-') {
+ sign = -1;
+ ch = getc (file);
+ } else sign = 1;
+ lit = ch - '0';
+ while (isdigit (ch = getc (file)))
+ lit = 10 * lit + ch - '0';
+ if (lit > nvars) die ("maximum variable exceeded");
+ lit *= sign;
+ if (lit) {
+ LOG ("%d ", lit);
+ } else {
+ LOG ("0\n");
+ group = INT_MAX;
+ nclauses++;
+ }
+ picosat_add (ps, lit);
+ } else if (ch == '{') {
+ if (nclauses == sclauses) die ("too many clauses");
+ if (group < INT_MAX) die ("multiple groups per clause");
+GROUP:
+ ch = getc (file);
+ if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') goto GROUP;
+ if (!isdigit (ch)) die ("group does not start with digit");
+ group = ch - '0';
+ while (isdigit (ch = getc (file)))
+ group = 10 * group + (ch - '0');
+ if (group > ngroups) die ("maximal group exceeded");
+ while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
+ ch = getc (file);
+ if (ch != '}') die ("expected '}'");
+ LOG ("{%d} ", group);
+ if (group) picosat_add (ps, -(nvars + group));
+ lit = INT_MAX;
+ } else die ("expected '{'");
+ goto LIT;
+DONE:
+ fclose (file);
+ for (lit = nvars + 1; lit <= nvars + ngroups; lit++) picosat_assume (ps, lit);
+ res = picosat_sat (ps, -1);
+ msg ("first call to SAT solver returned");
+ if (res == 10) printf ("s SATISFIABLE\n");
+ else if (res == 20) printf ("s UNSATISFIABLE\n");
+ else printf ("s UNKNOWN\n");
+ fflush (stdout);
+ if (res == 20) {
+ mus = picosat_mus_assumptions (ps, 0, callback, 1);
+ assert (mus);
+ printf ("v");
+ for (p = mus; (lit = *p); p++) {
+ assert (nvars + 1 <= lit && lit <= nvars + ngroups);
+ printf (" %d", lit - nvars);
+ }
+ printf (" 0\n");
+ fflush (stdout);
+ }
+ msg ("max memory %.1f MB",
+ picosat_max_bytes_allocated (ps) / (double)(1<<20));
+ picosat_reset (ps);
+ msg ("%d reductions", reductions);
+ return res;
+}
diff --git a/test/monniaux/picosat-965/picomcs.c b/test/monniaux/picosat-965/picomcs.c
new file mode 100644
index 00000000..3acf7bd7
--- /dev/null
+++ b/test/monniaux/picosat-965/picomcs.c
@@ -0,0 +1,334 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "picosat.h"
+
+typedef struct Clause { int cid, * lits; struct Clause * next; } Clause;
+typedef struct MCS { int mid, * clauses; struct MCS * next; } MCS;
+
+static int nvars;
+static char * marked;
+
+static Clause * first_clause, * last_clause;
+static int nclauses, first_cid, last_cid;
+
+static MCS * first_mcs, * last_mcs;
+static int nmcs;
+
+static int * stk, szstk, nstk;
+
+static int verbose, join, noprint;
+
+static int lineno = 1, close_input;
+static const char * input_name;
+static FILE * input;
+
+static PicoSAT * ps;
+
+static void release_clauses (void) {
+ Clause * p, * next;
+ for (p = first_clause; p; p = next) {
+ next = p->next;
+ free (p->lits);
+ free (p);
+ }
+}
+
+static void release_mss (void) {
+ MCS * p, * next;
+ for (p = first_mcs; p; p = next) {
+ next = p->next;
+ free (p->clauses);
+ free (p);
+ }
+}
+
+static void release (void) {
+ release_clauses ();
+ release_mss ();
+ free (marked);
+ free (stk);
+}
+
+static void push_stack (int n) {
+ if (nstk == szstk)
+ stk = realloc (stk, (szstk = szstk ? 2*szstk : 1) * sizeof *stk);
+ stk[nstk++] = n;
+}
+
+static void push_clause (void) {
+ Clause * clause;
+ size_t bytes;
+ clause = malloc (sizeof *clause);
+ clause->cid = ++nclauses;
+ clause->next = 0;
+ push_stack (0);
+ bytes = nstk * sizeof *clause->lits;
+ clause->lits = malloc (bytes);
+ memcpy (clause->lits, stk, bytes);
+ if (last_clause) last_clause->next = clause;
+ else first_clause = clause;
+ last_clause = clause;
+ nstk = 0;
+}
+
+static void push_mcs (void) {
+ MCS * mcs;
+ size_t bytes;
+ mcs = malloc (sizeof *mcs);
+ mcs->mid = ++nmcs;
+ mcs->next = 0;
+ push_stack (0);
+ bytes = nstk * sizeof *mcs->clauses;
+ mcs->clauses = malloc (bytes);
+ memcpy (mcs->clauses, stk, bytes);
+ if (last_mcs) last_mcs->next = mcs;
+ else first_mcs = mcs;
+ last_mcs = mcs;
+ nstk = 0;
+}
+
+static int nextch (void) {
+ int res = getc (input);
+ if (res == '\n') lineno++;
+ return res;
+}
+
+static void msg (int level, const char * fmt, ...) {
+ va_list ap;
+ if (level > verbose) return;
+ printf ("c [picomcs] ");
+ va_start (ap, fmt);
+ vprintf (fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static const char * parse (void) {
+ int ch, expclauses, lit, sign;
+ size_t bytes;
+ msg (1, "parsing %s", input_name);
+COMMENTS:
+ ch = nextch ();
+ if (ch == 'c') {
+ while ((ch = nextch ()) != '\n')
+ if (ch == EOF) return "out of file in comment";
+ goto COMMENTS;
+ }
+ if (ch != 'p')
+INVALID_HEADER:
+ return "invalid header";
+ ungetc (ch, input);
+ if (fscanf (input, "p cnf %d %d", &nvars, &expclauses) != 2)
+ goto INVALID_HEADER;
+ msg (1, "found 'p cnf %d %d' header", nvars, expclauses);
+ bytes = (1 + nvars + expclauses) * sizeof *marked;
+ marked = malloc (bytes);
+ memset (marked, 0, bytes);
+LIT:
+ ch = nextch ();
+ if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r') goto LIT;
+ if (ch == EOF) {
+ assert (nclauses <= expclauses);
+ if (nclauses < expclauses) return "clauses missing";
+ return 0;
+ }
+ if (ch == '-') {
+ ch = nextch ();
+ if (!isdigit (ch)) return "expected digit after '-'";
+ if (ch == '0') return "expected positive digit after '-'";
+ sign = -1;
+ } else if (!isdigit (ch)) return "expected '-' or digit";
+ else sign = 1;
+ lit = ch - '0';
+ while (isdigit (ch = nextch ()))
+ lit = 10*lit + (ch - '0');
+ if (lit) {
+ if (lit > nvars) return "maximum variable index exceeded";
+ if (nclauses == expclauses) return "too many clauses";
+ push_stack (sign * lit);
+ } else {
+ assert (nclauses < expclauses);
+ push_clause ();
+ }
+ goto LIT;
+}
+
+#ifndef NDEBUG
+static void dump_clause (Clause * c) {
+ int * p, lit;
+ for (p = c->lits; (lit = *p); p++)
+ printf ("%d ", lit);
+ printf ("0\n");
+}
+
+void dump (void) {
+ Clause * p;
+ printf ("p cnf %d %d\n", nvars, nclauses);
+ for (p = first_clause; p; p = p->next)
+ dump_clause (p);
+}
+#endif
+
+static int clause2selvar (Clause * c) {
+ int res = c->cid + nvars;
+ assert (first_cid <= res && res <= last_cid);
+ return res;
+}
+
+static void encode_clause (Clause * c) {
+ int * p, lit;
+ if (verbose >= 2) {
+ printf ("c [picomcs] encode clause %d :", c->cid);
+ printf (" %d", -clause2selvar (c));
+ for (p = c->lits; (lit = *p); p++) printf (" %d", lit);
+ fputc ('\n', stdout), fflush (stdout);
+ }
+ picosat_add (ps, -clause2selvar (c));
+ for (p = c->lits; (lit = *p); p++) picosat_add (ps, lit);
+ picosat_add (ps, 0);
+}
+
+static void encode (void) {
+ Clause * p;
+ first_cid = nvars + 1;
+ last_cid = nvars + nclauses;
+ msg (2, "selector variables range %d to %d", first_cid, last_cid);
+ for (p = first_clause; p; p = p->next)
+ encode_clause (p);
+ msg (1, "encoded %d clauses", nclauses);
+}
+
+static void camcs (void) {
+ int cid, i;
+ const int * mcs, * p;
+ msg (1, "starting to compute all minimal correcting sets");
+ while ((mcs = picosat_next_minimal_correcting_subset_of_assumptions (ps))) {
+ for (p = mcs; (cid = *p); p++)
+ push_stack (cid);
+ if (verbose >= 2) {
+ printf ("c [picomcs] mcs %d :", nmcs);
+ for (i = 0; i < nstk; i++) printf (" %d", stk[i] - nvars);
+ fputc ('\n', stdout);
+ fflush (stdout);
+ } else if (verbose && isatty (1)) {
+ printf ("\rc [picomcs] mcs %d", nmcs);
+ fflush (stdout);
+ }
+ push_mcs ();
+ }
+ if (verbose && isatty (1)) fputc ('\r', stdout);
+ msg (1, "found %d minimal correcting sets", nmcs);
+}
+
+static void cumcscb (void * state, int nmcs, int nhumus) {
+ int * ptr = state;
+ *ptr = nmcs;
+ ptr[0] = nmcs, ptr[1] = nhumus;
+ if (!verbose || (!isatty (1) && verbose == 1)) return;
+ if (verbose == 1) fputc ('\r', stdout);
+ printf ("c [picomcs] mcs %d humus %d", nmcs, nhumus);
+ if (verbose >= 2) fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static void cumcs (void) {
+ int stats[2], count, cid;
+ const int * humus, * p;
+ stats[0] = stats[1] = 0;
+ humus = picosat_humus (ps, cumcscb, stats);
+ if (isatty (1) && verbose == 1) fputc ('\n', stdout);
+ count = 0;
+ for (p = humus; (cid = *p); p++) {
+ if (marked[cid]) continue;
+ marked[cid] = 1;
+ count++;
+ }
+ assert (count == stats[1]);
+ msg (1,
+ "computed union of minimal correcting sets of size %d with %d mcs",
+ stats[1], stats[0]);
+}
+
+static void
+print_umcs (void) {
+ int cid;
+ printf ("v");
+ for (cid = first_cid; cid <= last_cid; cid++)
+ if (marked[cid])
+ printf (" %d", cid - nvars);
+ printf (" 0\n");
+}
+
+static void
+print_mcs (MCS * mcs)
+{
+ const int * p;
+ int cid;
+ printf ("v");
+ for (p = mcs->clauses; (cid = *p); p++)
+ printf (" %d", cid - nvars);
+ printf (" 0\n");
+}
+
+static void
+print_all_mcs (void)
+{
+ MCS * p;
+ for (p = first_mcs; p; p = p->next)
+ print_mcs (p);
+}
+
+int main (int argc, char ** argv) {
+ const char * perr;
+ int i, res;
+ for (i = 1; i < argc; i++) {
+ if (!strcmp (argv[i], "-h")) {
+ printf ("usage: picomcs [-h][-v][-j][-n][<input>]\n");
+ exit (0);
+ }
+ else if (!strcmp (argv[i], "-v")) verbose++;
+ else if (!strcmp (argv[i], "-j")) join = 1;
+ else if (!strcmp (argv[i], "-n")) noprint = 1;
+ else if (argv[i][0] == '-') {
+ fprintf (stderr, "*** picomcs: invalid option '%s'\n", argv[i]);
+ exit (1);
+ } else if (input_name) {
+ fprintf (stderr, "*** picomcs: two input files specified\n");
+ exit (1);
+ } else if (!(input = fopen ((input_name = argv[i]), "r"))) {
+ fprintf (stderr, "*** picomcs: can not read '%s'\n", argv[i]);
+ exit (1);
+ } else close_input = 1;
+ }
+ if (!input_name) input_name = "<stdin>", input = stdin;
+ if ((perr = parse ())) {
+ fprintf (stderr, "%s:%d: parse error: %s\n", input_name, lineno, perr);
+ exit (1);
+ }
+ if (close_input) fclose (input);
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ encode ();
+ for (i = first_cid; i <= last_cid; i++)
+ picosat_set_default_phase_lit (ps, i, 1);
+ for (i = first_cid; i <= last_cid; i++) picosat_assume (ps, i);
+ res = picosat_sat (ps, -1);
+ if (res == 10) printf ("s SATISFIABLE\n");
+ else printf ("s UNSATISFIABLE\n");
+ fflush (stdout);
+ if (join) cumcs (); else camcs ();
+ if (verbose) picosat_stats (ps);
+ picosat_reset (ps);
+ if (!noprint) {
+ if (join) print_umcs (); else print_all_mcs ();
+ }
+ release ();
+ return res;
+}
diff --git a/test/monniaux/picosat-965/picomus.c b/test/monniaux/picosat-965/picomus.c
new file mode 100644
index 00000000..0f559ded
--- /dev/null
+++ b/test/monniaux/picosat-965/picomus.c
@@ -0,0 +1,413 @@
+/****************************************************************************
+Copyright (c) 2011-2014, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include "picosat.h"
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+
+#define MAXNONREDROUNDS 3
+#define MINCOREROUNDS 5
+#define MAXCOREROUNDS 100
+
+typedef struct Cls { int lit, red, * lits; } Cls;
+
+static int verbose, nowitness;
+static int fclose_input, pclose_input, close_output;
+static FILE * input_file, * output_file;
+static const char * input_name, * output_name;
+static int lineno = 1;
+static int nvars, nclauses;
+static Cls * clauses;
+static int * lits, nlits, szlits;
+static double start;
+static int reductions;
+
+static PicoSAT * ps;
+
+static int next (void) {
+ int res = fgetc (input_file);
+ if (res == '\n') lineno++;
+ return res;
+}
+
+static void msg (int level, const char * fmt, ...) {
+ va_list ap;
+ if (verbose < level) return;
+ fputs ("c [picomus] ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static void warn (const char * fmt, ...) {
+ va_list ap;
+ if (verbose < 0) return;
+ fputs ("c [picomus] WARNING: ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static const char * parse (void) {
+ int ch, n, lit, sign, i;
+ Cls * c;
+HEADER:
+ ch = next ();
+ if (ch == 'c') {
+ while ((ch = next ()) != '\n')
+ if (ch == EOF) return "EOF after 'c'";
+ goto HEADER;
+ }
+ if (ch == '\r') goto HEADER;
+ if (ch != 'p') return "expected 'c' or 'p'";
+ if (fscanf (input_file, " cnf %d %d", &nvars, &nclauses) != 2)
+ return "invalid header";
+ msg (1, "p cnf %d %d", nvars, nclauses);
+ clauses = calloc (nclauses, sizeof *clauses);
+ lit = n = 0;
+LIT:
+ ch = next ();
+ if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r') goto LIT;
+ if (ch == 'c') {
+ while ((ch = next ()) != '\n')
+ if (ch == EOF) return "EOF after 'c'";
+ goto LIT;
+ }
+ if (ch == EOF) {
+ if (lit) return "zero missing";
+ if (n < nclauses) return "clauses missing";
+ return 0;
+ }
+ if (n == nclauses) return "too many clauses";
+ if (ch == '-') {
+ sign = -1;
+ ch = next ();
+ if (!isdigit (ch)) return "expected digit after '-'";
+ } else sign = 1;
+ if (!isdigit (ch)) return "expected digit";
+ lit = ch - '0';
+ while (isdigit (ch = next ()))
+ lit = 10 * lit + (ch - '0');
+ if (lit > nvars) return "maximum variable index exceeded";
+ if (lit) {
+ lit *= sign;
+ if (nlits == szlits) {
+ szlits = szlits ? 2 * szlits : 1;
+ lits = realloc (lits, szlits * sizeof *lits);
+ }
+ lits[nlits++] = lit;
+ } else {
+ c = clauses + n++;
+ c->lits = malloc ((nlits + 1) * sizeof *c->lits);
+ for (i = 0; i < nlits; i++)
+ c->lits[i] = lits[i];
+ c->lits[i] = 0;
+ nlits = 0;
+ }
+ goto LIT;
+}
+
+static void die (const char * fmt, ...) {
+ va_list ap;
+ fputs ("*** picomus: ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+ exit (1);
+}
+
+static double percent (double a, double b) { return b?100.0*a/b:0.0; }
+
+static void callback (void * dummy, const int * mus) {
+ int remaining;
+ const int * p;
+ (void) dummy;
+ if (verbose <= 0) return;
+ remaining = 0;
+ for (p = mus; *p; p++) remaining++;
+ assert (remaining <= nclauses);
+ reductions++;
+ msg (1, "reduction %d to %d = %.0f%% out of %d after %.1f sec",
+ reductions,
+ remaining, percent (remaining, nclauses), nclauses,
+ picosat_time_stamp () - start);
+}
+
+static const char * USAGE =
+"picomus [-h][-v][-q] [ <input> [ <output> ] ]\n"
+"\n"
+" -h print this command line option summary\n"
+" -v increase verbosity level (default 0 = no messages)\n"
+" -q be quiet (no warnings nor messages)\n"
+"\n"
+"This tool is a SAT solver that uses the PicoSAT library to\n"
+"generate a 'minimal unsatisfiable core' also known as 'minimal\n"
+"unsatisfiable set' (MUS) of a CNF in DIMACS format.\n"
+"\n"
+"Both file arguments can be \"-\" and then denote <stdin> and\n"
+"<stdout> respectively. If no input file is given <stdin> is used.\n"
+"If no output file is specified the MUS is computed and only printed\n"
+"to <stdout> in the format of the SAT competition 2011 MUS track.\n"
+"\n"
+"Note, that the 's ...' lines and in case the instance is satisfiable\n"
+"also the 'v ...' lines for the satisfying assignment are always\n"
+"printed to <stdout> (or not printed at all with '-q').\n"
+"\n"
+"If '-n' is specified satisfying assignment and MUS printing\n"
+"on <stdout> (using the 'v ...' format) is suppressed.\n"
+"The 's ...' line is still printed unless '-q' is specified.\n"
+"If <output> is specified an MUS is written to this file,\n"
+"even if '-n' or '-q' is used.\n"
+"\n"
+#ifndef TRACE
+"WARNING: PicosSAT is compiled without trace support.\n"
+"\n"
+"This typically slows down this MUS extractor, since\n"
+"it only relies on clause selector variables and\n"
+"can not make use of core extraction. To enable\n"
+"trace generation use './configure.sh --trace' or\n"
+"'./configure.sh -O --trace' when building PicoSAT.\n"
+#else
+"Since trace generation code is included, this binary\n"
+"uses also core extraction in addition to clause selector\n"
+"variables.\n"
+#endif
+;
+
+int main (int argc, char ** argv) {
+ int i, * p, n, oldn, red, nonred, res, round, printed, len;
+ const char * err;
+ const int * q;
+ char * cmd;
+ Cls * c;
+#ifndef NDEBUG
+ int tmp;
+#endif
+ start = picosat_time_stamp ();
+ for (i = 1; i < argc; i++) {
+ if (!strcmp (argv[i], "-h")) {
+ fputs (USAGE, stdout);
+ exit (0);
+ } else if (!strcmp (argv[i], "-v")) {
+ if (verbose < 0) die ("'-v' option after '-q'");
+ verbose++;
+ } else if (!strcmp (argv[i], "-q")) {
+ if (verbose < 0) die ("two '-q' options");
+ if (verbose > 0) die ("'-q' option after '-v'");
+ verbose = -1;
+ } else if (!strcmp (argv[i], "-n")) nowitness = 1;
+ else if (argv[i][0] == '-' && argv[i][1])
+ die ("invalid command line option '%s'", argv[i]);
+ else if (output_name) die ("too many arguments");
+ else if (!input_name) input_name = argv[i];
+ else output_name = argv[i];
+ }
+ if (!output_name) warn ("no output file given");
+ if (input_name && strcmp (input_name, "-")) {
+ len = strlen (input_name);
+ if (len >= 3 && !strcmp (input_name + len - 3, ".gz")) {
+#ifdef NZIP
+ input_file=NULL;
+#else
+ cmd = malloc (len + 20);
+ sprintf (cmd, "gunzip -c %s 2>/dev/null", input_name);
+ input_file = popen (cmd, "r");
+ pclose_input = 1;
+ free (cmd);
+#endif
+ } else input_file = fopen (input_name, "r"), fclose_input = 1;
+ if (!input_file) die ("can not read '%s'", input_name);
+ } else input_file = stdin, input_name = "-";
+ if ((err = parse ())) {
+ fprintf (stdout, "%s:%d: %s\n", input_name, lineno, err);
+ fflush (stdout);
+ exit (1);
+ }
+ if (fclose_input) fclose (input_file);
+#ifndef NZIP
+ if (pclose_input) pclose (input_file);
+#endif
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ picosat_set_output (ps, stdout);
+ if (verbose > 1) picosat_set_verbosity (ps, verbose - 1);
+ printed = 0;
+ if (!picosat_enable_trace_generation (ps))
+ warn ("PicoSAT compiled without trace generation"),
+ warn ("core extraction disabled");
+ else {
+ n = nclauses;
+ nonred = 0;
+ for (round = 1; round <= MAXCOREROUNDS; round++) {
+ if (verbose > 1)
+ msg (1, "starting core extraction round %d", round);
+ picosat_set_seed (ps, round);
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) {
+ picosat_add (ps, 1);
+ picosat_add (ps, -1);
+ } else {
+ for (p = c->lits; *p; p++)
+ picosat_add (ps, *p);
+ }
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ }
+ res = picosat_sat (ps, -1);
+ if (res == 10) { assert (round == 1); goto SAT; }
+ assert (res == 20);
+ if (!printed) {
+ assert (round == 1);
+ printed = 1;
+ if (verbose >= 0)
+ printf ("s UNSATISFIABLE\n"),
+ fflush (stdout);
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) { assert (!picosat_coreclause (ps, i)); continue; }
+ if (picosat_coreclause (ps, i)) continue;
+ c->red = 1;
+ }
+ oldn = n;
+ n = 0;
+ for (i = 0; i < nclauses; i++) if (!clauses[i].red) n++;
+ msg (1, "extracted core %d of size %d = %0.f%% out of %d after %.1f sec",
+ round, n, percent (n, nclauses), nclauses,
+ picosat_time_stamp () - start);
+ assert (oldn >= n);
+ picosat_reset (ps);
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ picosat_set_output (ps, stdout);
+ if (round >= MINCOREROUNDS) {
+ red = oldn - n;
+ if (red < 10 && (100*red + 99)/oldn < 2) {
+ nonred++;
+ if (nonred > MAXNONREDROUNDS) break;
+ }
+ }
+ if (round < MAXCOREROUNDS) picosat_enable_trace_generation (ps);
+ }
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) {
+ picosat_add (ps, 1);
+ picosat_add (ps, -1);
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ continue;
+ }
+ c->lit = nvars + i + 1;
+ picosat_add (ps, -c->lit);
+ for (p = c->lits; *p; p++)
+ (void) picosat_add (ps, *p);
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) continue;
+ picosat_assume (ps, c->lit);
+ }
+ res = picosat_sat (ps, -1);
+ if (res == 20) {
+ if (!printed && verbose >= 0)
+ printf ("s UNSATISFIABLE\n"), fflush (stdout);
+ for (i = 0; i < nclauses; i++) clauses[i].red = 1;
+ q = picosat_mus_assumptions (ps, 0, callback, 1);
+ while ((i = *q++)) {
+ i -= nvars + 1;
+ assert (0 <= i && i < nclauses);
+ clauses[i].red = 0;
+ }
+ } else {
+SAT:
+ assert (res == 10);
+ if (!printed && verbose >= 0)
+ printf ("s SATISFIABLE\n"), fflush (stdout);
+ if (!nowitness && verbose >= 0) {
+ for (i = 1; i <= nvars; i++)
+ printf ("v %d\n", ((picosat_deref (ps, i) < 0) ? -1 : 1) * i);
+ printf ("v 0\n");
+ }
+ }
+ if (verbose > 0) picosat_stats (ps);
+ picosat_reset (ps);
+ n = 0;
+ for (i = 0; i < nclauses; i++) if (!clauses[i].red) n++;
+ red = nclauses - n;
+ msg (1, "found %d redundant clauses %.0f%%", red, percent (red, nclauses));
+ if (res == 20)
+ msg (0, "computed MUS of size %d out of %d (%.0f%%)",
+ n, nclauses, percent (n, nclauses));
+ if (output_name && strcmp (output_name, "-")) {
+ output_file = fopen (output_name, "w");
+ if (!output_file) die ("can not write '%s'", output_name);
+ close_output = 1;
+ } else if (output_name && !strcmp (output_name, "-")) output_file = stdout;
+ if (output_file) {
+ fprintf (output_file, "p cnf %d %d\n", nvars, n);
+ for (i = 0; i < nclauses; i++)
+ if (!clauses[i].red) {
+ for (p = clauses[i].lits; *p; p++) fprintf (output_file, "%d ", *p);
+ fprintf (output_file, "0\n");
+ }
+ if (close_output) fclose (output_file);
+ }
+ if (res == 20) {
+ if (!nowitness && verbose >= 0) {
+ for (i = 0; i < nclauses; i++)
+ if (!clauses[i].red) printf ("v %d\n", i+1);
+ printf ("v 0\n");
+ }
+ }
+ msg (1, "%s %d irredundant clauses %.0f%%",
+ output_file ? "printed" : "computed", n, percent (n, nclauses));
+ for (i = 0; i < nclauses; i++) free (clauses[i].lits);
+ free (clauses);
+ free (lits);
+ msg (1, "%d reductions in %.1f seconds",
+ reductions, picosat_time_stamp () - start);
+ return res;
+}
diff --git a/test/monniaux/picosat-965/picosat.c b/test/monniaux/picosat-965/picosat.c
new file mode 100644
index 00000000..21442f44
--- /dev/null
+++ b/test/monniaux/picosat-965/picosat.c
@@ -0,0 +1,8508 @@
+/****************************************************************************
+Copyright (c) 2006 - 2015, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#include "picosat.h"
+
+#define INLINE inline
+
+/* By default code for 'all different constraints' is disabled, since 'NADC'
+ * is defined.
+ */
+#define NADC
+
+/* By default we enable failed literals, since 'NFL' is undefined.
+ *
+#define NFL
+ */
+
+/* By default we 'detach satisfied (large) clauses', e.g. NDSC undefined.
+ *
+#define NDSC
+ */
+
+/* Do not use luby restart schedule instead of inner/outer.
+ *
+#define NLUBY
+ */
+
+/* Enabling this define, will use gnuplot to visualize how the scores evolve.
+ *
+#define VISCORES
+ */
+
+#ifdef VISCORES
+// #define WRITEGIF /* ... to generate a video */
+#endif
+
+#ifdef VISCORES
+#ifndef WRITEGIF
+#include <unistd.h> /* for 'usleep' */
+#endif
+#endif
+
+#ifdef RCODE
+#include <R.h>
+#endif
+
+#define MINRESTART 100 /* minimum restart interval */
+#define MAXRESTART 1000000 /* maximum restart interval */
+#define RDECIDE 1000 /* interval of random decisions */
+#define FRESTART 110 /* restart increase factor in percent */
+#define FREDUCE 110 /* reduce increase factor in percent */
+#define FREDADJ 121 /* reduce increase adjustment factor */
+#define MAXCILS 10 /* maximal number of unrecycled internals */
+#define FFLIPPED 10000 /* flipped reduce factor */
+#define FFLIPPEDPREC 10000000/* flipped reduce factor precision */
+#define INTERRUPTLIM 1024 /* check interrupt after that many decisions */
+
+#ifndef TRACE
+#define NO_BINARY_CLAUSES /* store binary clauses more compactly */
+#endif
+
+/* For debugging purposes you may want to define 'LOGGING', which actually
+ * can be enforced by using './configure.sh --log'.
+ */
+#ifdef LOGGING
+#define LOG(code) do { code; } while (0)
+#else
+#define LOG(code) do { } while (0)
+#endif
+#define NOLOG(code) do { } while (0) /* log exception */
+#define ONLYLOG(code) do { code; } while (0) /* force logging */
+
+#define FALSE ((Val)-1)
+#define UNDEF ((Val)0)
+#define TRUE ((Val)1)
+
+#define COMPACT_TRACECHECK_TRACE_FMT 0
+#define EXTENDED_TRACECHECK_TRACE_FMT 1
+#define RUP_TRACE_FMT 2
+
+#define NEWN(p,n) do { (p) = new (ps, sizeof (*(p)) * (n)); } while (0)
+#define CLRN(p,n) do { memset ((p), 0, sizeof (*(p)) * (n)); } while (0)
+#define CLR(p) CLRN(p,1)
+#define DELETEN(p,n) \
+ do { delete (ps, p, sizeof (*(p)) * (n)); (p) = 0; } while (0)
+
+#define RESIZEN(p,old_num,new_num) \
+ do { \
+ size_t old_size = sizeof (*(p)) * (old_num); \
+ size_t new_size = sizeof (*(p)) * (new_num); \
+ (p) = resize (ps, (p), old_size, new_size) ; \
+ } while (0)
+
+#define ENLARGE(start,head,end) \
+ do { \
+ unsigned old_num = (ptrdiff_t)((end) - (start)); \
+ size_t new_num = old_num ? (2 * old_num) : 1; \
+ unsigned count = (head) - (start); \
+ assert ((start) <= (end)); \
+ RESIZEN((start),old_num,new_num); \
+ (head) = (start) + count; \
+ (end) = (start) + new_num; \
+ } while (0)
+
+#define NOTLIT(l) (ps->lits + (1 ^ ((l) - ps->lits)))
+
+#define LIT2IDX(l) ((ptrdiff_t)((l) - ps->lits) / 2)
+#define LIT2IMPLS(l) (ps->impls + (ptrdiff_t)((l) - ps->lits))
+#define LIT2INT(l) ((int)(LIT2SGN(l) * LIT2IDX(l)))
+#define LIT2SGN(l) (((ptrdiff_t)((l) - ps->lits) & 1) ? -1 : 1)
+#define LIT2VAR(l) (ps->vars + LIT2IDX(l))
+#define LIT2HTPS(l) (ps->htps + (ptrdiff_t)((l) - ps->lits))
+#define LIT2JWH(l) (ps->jwh + ((l) - ps->lits))
+
+#ifndef NDSC
+#define LIT2DHTPS(l) (ps->dhtps + (ptrdiff_t)((l) - ps->lits))
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+typedef uintptr_t Wrd;
+#define ISLITREASON(C) (1&(Wrd)C)
+#define LIT2REASON(L) \
+ (assert (L->val==TRUE), ((Cls*)(1 + (2*(L - ps->lits)))))
+#define REASON2LIT(C) ((Lit*)(ps->lits + ((Wrd)C)/2))
+#endif
+
+#define ENDOFCLS(c) ((void*)((Lit**)(c)->lits + (c)->size))
+
+#define SOC ((ps->oclauses == ps->ohead) ? ps->lclauses : ps->oclauses)
+#define EOC (ps->lhead)
+#define NXC(p) (((p) + 1 == ps->ohead) ? ps->lclauses : (p) + 1)
+
+#define OIDX2IDX(idx) (2 * ((idx) + 1))
+#define LIDX2IDX(idx) (2 * (idx) + 1)
+
+#define ISLIDX(idx) ((idx)&1)
+
+#define IDX2OIDX(idx) (assert(!ISLIDX(idx)), (idx)/2 - 1)
+#define IDX2LIDX(idx) (assert(ISLIDX(idx)), (idx)/2)
+
+#define EXPORTIDX(idx) \
+ ((ISLIDX(idx) ? (IDX2LIDX (idx) + (ps->ohead - ps->oclauses)) : IDX2OIDX(idx)) + 1)
+
+#define IDX2CLS(i) \
+ (assert(i), (ISLIDX(i) ? ps->lclauses : ps->oclauses)[(i)/2 - !ISLIDX(i)])
+
+#define IDX2ZHN(i) (assert(i), (ISLIDX(i) ? ps->zhains[(i)/2] : 0))
+
+#define CLS2TRD(c) (((Trd*)(c)) - 1)
+#define CLS2IDX(c) ((((Trd*)(c)) - 1)->idx)
+
+#define CLS2ACT(c) \
+ ((Act*)((assert((c)->learned)),assert((c)->size>2),ENDOFCLS(c)))
+
+#define VAR2LIT(v) (ps->lits + 2 * ((v) - ps->vars))
+#define VAR2RNK(v) (ps->rnks + ((v) - ps->vars))
+
+#define RNK2LIT(r) (ps->lits + 2 * ((r) - ps->rnks))
+#define RNK2VAR(r) (ps->vars + ((r) - ps->rnks))
+
+#define BLK_FILL_BYTES 8
+#define SIZE_OF_BLK (sizeof (Blk) - BLK_FILL_BYTES)
+
+#define PTR2BLK(void_ptr) \
+ ((void_ptr) ? (Blk*)(((char*)(void_ptr)) - SIZE_OF_BLK) : 0)
+
+#define AVERAGE(a,b) ((b) ? (((double)a) / (double)(b)) : 0.0)
+#define PERCENT(a,b) (100.0 * AVERAGE(a,b))
+
+#ifndef RCODE
+#define ABORT(msg) \
+ do { \
+ fputs ("*** picosat: " msg "\n", stderr); \
+ abort (); \
+ } while (0)
+#else
+#define ABORT(msg) \
+ do { \
+ Rf_error (msg); \
+ } while (0)
+#endif
+
+#define ABORTIF(cond,msg) \
+ do { \
+ if (!(cond)) break; \
+ ABORT (msg); \
+ } while (0)
+
+#define ZEROFLT (0x00000000u)
+#define EPSFLT (0x00000001u)
+#define INFFLT (0xffffffffu)
+
+#define FLTCARRY (1u << 25)
+#define FLTMSB (1u << 24)
+#define FLTMAXMANTISSA (FLTMSB - 1)
+
+#define FLTMANTISSA(d) ((d) & FLTMAXMANTISSA)
+#define FLTEXPONENT(d) ((int)((d) >> 24) - 128)
+
+#define FLTMINEXPONENT (-128)
+#define FLTMAXEXPONENT (127)
+
+#define CMPSWAPFLT(a,b) \
+ do { \
+ Flt tmp; \
+ if (((a) < (b))) \
+ { \
+ tmp = (a); \
+ (a) = (b); \
+ (b) = tmp; \
+ } \
+ } while (0)
+
+#define UNPACKFLT(u,m,e) \
+ do { \
+ (m) = FLTMANTISSA(u); \
+ (e) = FLTEXPONENT(u); \
+ (m) |= FLTMSB; \
+ } while (0)
+
+#define INSERTION_SORT_LIMIT 10
+
+#define SORTING_SWAP(T,p,q) \
+do { \
+ T tmp = *(q); \
+ *(q) = *(p); \
+ *(p) = tmp; \
+} while (0)
+
+#define SORTING_CMP_SWAP(T,cmp,p,q) \
+do { \
+ if ((cmp) (ps, *(p), *(q)) > 0) \
+ SORTING_SWAP (T, p, q); \
+} while(0)
+
+#define QUICKSORT_PARTITION(T,cmp,a,l,r) \
+do { \
+ T pivot; \
+ int j; \
+ i = (l) - 1; /* result in 'i' */ \
+ j = (r); \
+ pivot = (a)[j]; \
+ for (;;) \
+ { \
+ while ((cmp) (ps, (a)[++i], pivot) < 0) \
+ ; \
+ while ((cmp) (ps, pivot, (a)[--j]) < 0) \
+ if (j == (l)) \
+ break; \
+ if (i >= j) \
+ break; \
+ SORTING_SWAP (T, (a) + i, (a) + j); \
+ } \
+ SORTING_SWAP (T, (a) + i, (a) + (r)); \
+} while(0)
+
+#define QUICKSORT(T,cmp,a,n) \
+do { \
+ int l = 0, r = (n) - 1, m, ll, rr, i; \
+ assert (ps->ihead == ps->indices); \
+ if (r - l <= INSERTION_SORT_LIMIT) \
+ break; \
+ for (;;) \
+ { \
+ m = (l + r) / 2; \
+ SORTING_SWAP (T, (a) + m, (a) + r - 1); \
+ SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r - 1); \
+ SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r); \
+ SORTING_CMP_SWAP (T, cmp, (a) + r - 1, (a) + r); \
+ QUICKSORT_PARTITION (T, cmp, (a), l + 1, r - 1); \
+ if (i - l < r - i) \
+ { \
+ ll = i + 1; \
+ rr = r; \
+ r = i - 1; \
+ } \
+ else \
+ { \
+ ll = l; \
+ rr = i - 1; \
+ l = i + 1; \
+ } \
+ if (r - l > INSERTION_SORT_LIMIT) \
+ { \
+ assert (rr - ll > INSERTION_SORT_LIMIT); \
+ if (ps->ihead == ps->eoi) \
+ ENLARGE (ps->indices, ps->ihead, ps->eoi); \
+ *ps->ihead++ = ll; \
+ if (ps->ihead == ps->eoi) \
+ ENLARGE (ps->indices, ps->ihead, ps->eoi); \
+ *ps->ihead++ = rr; \
+ } \
+ else if (rr - ll > INSERTION_SORT_LIMIT) \
+ { \
+ l = ll; \
+ r = rr; \
+ } \
+ else if (ps->ihead > ps->indices) \
+ { \
+ r = *--ps->ihead; \
+ l = *--ps->ihead; \
+ } \
+ else \
+ break; \
+ } \
+} while (0)
+
+#define INSERTION_SORT(T,cmp,a,n) \
+do { \
+ T pivot; \
+ int l = 0, r = (n) - 1, i, j; \
+ for (i = r; i > l; i--) \
+ SORTING_CMP_SWAP (T, cmp, (a) + i - 1, (a) + i); \
+ for (i = l + 2; i <= r; i++) \
+ { \
+ j = i; \
+ pivot = (a)[i]; \
+ while ((cmp) (ps, pivot, (a)[j - 1]) < 0) \
+ { \
+ (a)[j] = (a)[j - 1]; \
+ j--; \
+ } \
+ (a)[j] = pivot; \
+ } \
+} while (0)
+
+#ifdef NDEBUG
+#define CHECK_SORTED(cmp,a,n) do { } while(0)
+#else
+#define CHECK_SORTED(cmp,a,n) \
+do { \
+ int i; \
+ for (i = 0; i < (n) - 1; i++) \
+ assert ((cmp) (ps, (a)[i], (a)[i + 1]) <= 0); \
+} while(0)
+#endif
+
+#define SORT(T,cmp,a,n) \
+do { \
+ T * aa = (a); \
+ int nn = (n); \
+ QUICKSORT (T, cmp, aa, nn); \
+ INSERTION_SORT (T, cmp, aa, nn); \
+ assert (ps->ihead == ps->indices); \
+ CHECK_SORTED (cmp, aa, nn); \
+} while (0)
+
+#define WRDSZ (sizeof (long) * 8)
+
+#ifdef RCODE
+#define fprintf(...) do { } while (0)
+#define vfprintf(...) do { } while (0)
+#define fputs(...) do { } while (0)
+#define fputc(...) do { } while (0)
+#endif
+
+typedef unsigned Flt; /* 32 bit deterministic soft float */
+typedef Flt Act; /* clause and variable activity */
+typedef struct Blk Blk; /* allocated memory block */
+typedef struct Cls Cls; /* clause */
+typedef struct Lit Lit; /* literal */
+typedef struct Rnk Rnk; /* variable to score mapping */
+typedef signed char Val; /* TRUE, UNDEF, FALSE */
+typedef struct Var Var; /* variable */
+#ifdef TRACE
+typedef struct Trd Trd; /* trace data for clauses */
+typedef struct Zhn Zhn; /* compressed chain (=zain) data */
+typedef unsigned char Znt; /* compressed antecedent data */
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+typedef struct Ltk Ltk;
+
+struct Ltk
+{
+ Lit ** start;
+ unsigned count : WRDSZ == 32 ? 27 : 32;
+ unsigned ldsize : WRDSZ == 32 ? 5 : 32;
+};
+#endif
+
+struct Lit
+{
+ Val val;
+};
+
+struct Var
+{
+ unsigned mark : 1; /*bit 1*/
+ unsigned resolved : 1; /*bit 2*/
+ unsigned phase : 1; /*bit 3*/
+ unsigned assigned : 1; /*bit 4*/
+ unsigned used : 1; /*bit 5*/
+ unsigned failed : 1; /*bit 6*/
+ unsigned internal : 1; /*bit 7*/
+ unsigned usedefphase : 1; /*bit 8*/
+ unsigned defphase : 1; /*bit 9*/
+ unsigned msspos : 1; /*bit 10*/
+ unsigned mssneg : 1; /*bit 11*/
+ unsigned humuspos : 1; /*bit 12*/
+ unsigned humusneg : 1; /*bit 13*/
+ unsigned partial : 1; /*bit 14*/
+#ifdef TRACE
+ unsigned core : 1; /*bit 15*/
+#endif
+ unsigned level;
+ Cls *reason;
+#ifndef NADC
+ Lit ** inado;
+ Lit ** ado;
+ Lit *** adotabpos;
+#endif
+};
+
+struct Rnk
+{
+ Act score;
+ unsigned pos : 30; /* 0 iff not on heap */
+ unsigned moreimportant : 1;
+ unsigned lessimportant : 1;
+};
+
+struct Cls
+{
+ unsigned size;
+
+ unsigned collect:1; /* bit 1 */
+ unsigned learned:1; /* bit 2 */
+ unsigned locked:1; /* bit 3 */
+ unsigned used:1; /* bit 4 */
+#ifndef NDEBUG
+ unsigned connected:1; /* bit 5 */
+#endif
+#ifdef TRACE
+ unsigned collected:1; /* bit 6 */
+ unsigned core:1; /* bit 7 */
+#endif
+
+#define LDMAXGLUE 25 /* 32 - 7 */
+#define MAXGLUE ((1<<LDMAXGLUE)-1)
+
+ unsigned glue:LDMAXGLUE;
+
+ Cls *next[2];
+ Lit *lits[2];
+};
+
+#ifdef TRACE
+struct Zhn
+{
+ unsigned ref:31;
+ unsigned core:1;
+ Znt * liz;
+ Znt znt[0];
+};
+
+struct Trd
+{
+ unsigned idx;
+ Cls cls[0];
+};
+#endif
+
+struct Blk
+{
+#ifndef NDEBUG
+ union
+ {
+ size_t size; /* this is what we really use */
+ void *as_two_ptrs[2]; /* 2 * sizeof (void*) alignment of data */
+ }
+ header;
+#endif
+ char data[BLK_FILL_BYTES];
+};
+
+enum State
+{
+ RESET = 0,
+ READY = 1,
+ SAT = 2,
+ UNSAT = 3,
+ UNKNOWN = 4,
+};
+
+enum Phase
+{
+ POSPHASE,
+ NEGPHASE,
+ JWLPHASE,
+ RNDPHASE,
+};
+
+struct PicoSAT
+{
+ enum State state;
+ enum Phase defaultphase;
+ int last_sat_call_result;
+
+ FILE *out;
+ char * prefix;
+ int verbosity;
+ int plain;
+ unsigned LEVEL;
+ unsigned max_var;
+ unsigned size_vars;
+
+ Lit *lits;
+ Var *vars;
+ Rnk *rnks;
+ Flt *jwh;
+ Cls **htps;
+#ifndef NDSC
+ Cls **dhtps;
+#endif
+#ifdef NO_BINARY_CLAUSES
+ Ltk *impls;
+ Cls impl, cimpl;
+ int implvalid, cimplvalid;
+#else
+ Cls **impls;
+#endif
+ Lit **trail, **thead, **eot, **ttail, ** ttail2;
+#ifndef NADC
+ Lit **ttailado;
+#endif
+ unsigned adecidelevel;
+ Lit **als, **alshead, **alstail, **eoals;
+ Lit **CLS, **clshead, **eocls;
+ int *rils, *rilshead, *eorils;
+ int *cils, *cilshead, *eocils;
+ int *fals, *falshead, *eofals;
+ int *mass, szmass;
+ int *mssass, szmssass;
+ int *mcsass, nmcsass, szmcsass;
+ int *humus, szhumus;
+ Lit *failed_assumption;
+ int extracted_all_failed_assumptions;
+ Rnk **heap, **hhead, **eoh;
+ Cls **oclauses, **ohead, **eoo; /* original clauses */
+ Cls **lclauses, **lhead, ** EOL; /* learned clauses */
+ int * soclauses, * sohead, * eoso; /* saved original clauses */
+ int saveorig;
+ int partial;
+#ifdef TRACE
+ int trace;
+ Zhn **zhains, **zhead, **eoz;
+ int ocore;
+#endif
+ FILE * rup;
+ int rupstarted;
+ int rupvariables;
+ int rupclauses;
+ Cls *mtcls;
+ Cls *conflict;
+ Lit **added, **ahead, **eoa;
+ Var **marked, **mhead, **eom;
+ Var **dfs, **dhead, **eod;
+ Cls **resolved, **rhead, **eor;
+ unsigned char *levels, *levelshead, *eolevels;
+ unsigned *dused, *dusedhead, *eodused;
+ unsigned char *buffer, *bhead, *eob;
+ Act vinc, lscore, ilvinc, ifvinc;
+#ifdef VISCORES
+ Act fvinc, nvinc;
+#endif
+ Act cinc, lcinc, ilcinc, fcinc;
+ unsigned srng;
+ size_t current_bytes;
+ size_t max_bytes;
+ size_t recycled;
+ double seconds, flseconds;
+ double entered;
+ unsigned nentered;
+ int measurealltimeinlib;
+ char *rline[2];
+ int szrline, RCOUNT;
+ double levelsum;
+ unsigned iterations;
+ int reports;
+ int lastrheader;
+ unsigned calls;
+ unsigned decisions;
+ unsigned restarts;
+ unsigned simps;
+ unsigned fsimplify;
+ unsigned isimplify;
+ unsigned reductions;
+ unsigned lreduce;
+ unsigned lreduceadjustcnt;
+ unsigned lreduceadjustinc;
+ unsigned lastreduceconflicts;
+ unsigned llocked; /* locked large learned clauses */
+ unsigned lrestart;
+#ifdef NLUBY
+ unsigned drestart;
+ unsigned ddrestart;
+#else
+ unsigned lubycnt;
+ unsigned lubymaxdelta;
+ int waslubymaxdelta;
+#endif
+ unsigned long long lsimplify;
+ unsigned long long propagations;
+ unsigned long long lpropagations;
+ unsigned fixed; /* top level assignments */
+#ifndef NFL
+ unsigned failedlits;
+ unsigned ifailedlits;
+ unsigned efailedlits;
+ unsigned flcalls;
+#ifdef STATS
+ unsigned flrounds;
+ unsigned long long flprops;
+ unsigned long long floopsed, fltried, flskipped;
+#endif
+ unsigned long long fllimit;
+ int simplifying;
+ Lit ** saved;
+ unsigned saved_size;
+#endif
+ unsigned conflicts;
+ unsigned contexts;
+ unsigned internals;
+ unsigned noclauses; /* current number large original clauses */
+ unsigned nlclauses; /* current number large learned clauses */
+ unsigned olits; /* current literals in large original clauses */
+ unsigned llits; /* current literals in large learned clauses */
+ unsigned oadded; /* added original clauses */
+ unsigned ladded; /* added learned clauses */
+ unsigned loadded; /* added original large clauses */
+ unsigned lladded; /* added learned large clauses */
+ unsigned addedclauses; /* oadded + ladded */
+ unsigned vused; /* used variables */
+ unsigned llitsadded; /* added learned literals */
+ unsigned long long visits;
+#ifdef STATS
+ unsigned loused; /* used large original clauses */
+ unsigned llused; /* used large learned clauses */
+ unsigned long long bvisits;
+ unsigned long long tvisits;
+ unsigned long long lvisits;
+ unsigned long long othertrue;
+ unsigned long long othertrue2;
+ unsigned long long othertruel;
+ unsigned long long othertrue2u;
+ unsigned long long othertruelu;
+ unsigned long long ltraversals;
+ unsigned long long traversals;
+#ifdef TRACE
+ unsigned long long antecedents;
+#endif
+ unsigned uips;
+ unsigned znts;
+ unsigned assumptions;
+ unsigned rdecisions;
+ unsigned sdecisions;
+ size_t srecycled;
+ size_t rrecycled;
+ unsigned long long derefs;
+#endif
+ unsigned minimizedllits;
+ unsigned nonminimizedllits;
+#ifndef NADC
+ Lit *** ados, *** hados, *** eados;
+ Lit *** adotab;
+ unsigned nadotab;
+ unsigned szadotab;
+ Cls * adoconflict;
+ unsigned adoconflicts;
+ unsigned adoconflictlimit;
+ int addingtoado;
+ int adodisabled;
+#endif
+ unsigned long long flips;
+#ifdef STATS
+ unsigned long long FORCED;
+ unsigned long long assignments;
+ unsigned inclreduces;
+ unsigned staticphasedecisions;
+ unsigned skippedrestarts;
+#endif
+ int * indices, * ihead, *eoi;
+ unsigned sdflips;
+
+ unsigned long long saved_flips;
+ unsigned saved_max_var;
+ unsigned min_flipped;
+
+ void * emgr;
+ picosat_malloc enew;
+ picosat_realloc eresize;
+ picosat_free edelete;
+
+ struct {
+ void * state;
+ int (*function) (void *);
+ } interrupt;
+
+#ifdef VISCORES
+ FILE * fviscores;
+#endif
+};
+
+typedef PicoSAT PS;
+
+static INLINE Flt
+packflt (unsigned m, int e)
+{
+ Flt res;
+ assert (m < FLTMSB);
+ assert (FLTMINEXPONENT <= e);
+ assert (e <= FLTMAXEXPONENT);
+ res = m | ((unsigned)(e + 128) << 24);
+ return res;
+}
+
+static Flt
+base2flt (unsigned m, int e)
+{
+ if (!m)
+ return ZEROFLT;
+
+ if (m < FLTMSB)
+ {
+ do
+ {
+ if (e <= FLTMINEXPONENT)
+ return EPSFLT;
+
+ e--;
+ m <<= 1;
+
+ }
+ while (m < FLTMSB);
+ }
+ else
+ {
+ while (m >= FLTCARRY)
+ {
+ if (e >= FLTMAXEXPONENT)
+ return INFFLT;
+
+ e++;
+ m >>= 1;
+ }
+ }
+
+ m &= ~FLTMSB;
+ return packflt (m, e);
+}
+
+static Flt
+addflt (Flt a, Flt b)
+{
+ unsigned ma, mb, delta;
+ int ea, eb;
+
+ CMPSWAPFLT (a, b);
+ if (!b)
+ return a;
+
+ UNPACKFLT (a, ma, ea);
+ UNPACKFLT (b, mb, eb);
+
+ assert (ea >= eb);
+ delta = ea - eb;
+ if (delta < 32) mb >>= delta; else mb = 0;
+ if (!mb)
+ return a;
+
+ ma += mb;
+ if (ma & FLTCARRY)
+ {
+ if (ea == FLTMAXEXPONENT)
+ return INFFLT;
+
+ ea++;
+ ma >>= 1;
+ }
+
+ assert (ma < FLTCARRY);
+ ma &= FLTMAXMANTISSA;
+
+ return packflt (ma, ea);
+}
+
+static Flt
+mulflt (Flt a, Flt b)
+{
+ unsigned ma, mb;
+ unsigned long long accu;
+ int ea, eb;
+
+ CMPSWAPFLT (a, b);
+ if (!b)
+ return ZEROFLT;
+
+ UNPACKFLT (a, ma, ea);
+ UNPACKFLT (b, mb, eb);
+
+ ea += eb;
+ ea += 24;
+ if (ea > FLTMAXEXPONENT)
+ return INFFLT;
+
+ if (ea < FLTMINEXPONENT)
+ return EPSFLT;
+
+ accu = ma;
+ accu *= mb;
+ accu >>= 24;
+
+ if (accu >= FLTCARRY)
+ {
+ if (ea == FLTMAXEXPONENT)
+ return INFFLT;
+
+ ea++;
+ accu >>= 1;
+
+ if (accu >= FLTCARRY)
+ return INFFLT;
+ }
+
+ assert (accu < FLTCARRY);
+ assert (accu & FLTMSB);
+
+ ma = accu;
+ ma &= ~FLTMSB;
+
+ return packflt (ma, ea);
+}
+
+static Flt
+ascii2flt (const char *str)
+{
+ Flt ten = base2flt (10, 0);
+ Flt onetenth = base2flt (26843546, -28);
+ Flt res = ZEROFLT, tmp, base;
+ const char *p = str;
+ int ch;
+
+ ch = *p++;
+
+ if (ch != '.')
+ {
+ if (!isdigit (ch))
+ return INFFLT; /* better abort ? */
+
+ res = base2flt (ch - '0', 0);
+
+ while ((ch = *p++))
+ {
+ if (ch == '.')
+ break;
+
+ if (!isdigit (ch))
+ return INFFLT; /* better abort? */
+
+ res = mulflt (res, ten);
+ tmp = base2flt (ch - '0', 0);
+ res = addflt (res, tmp);
+ }
+ }
+
+ if (ch == '.')
+ {
+ ch = *p++;
+ if (!isdigit (ch))
+ return INFFLT; /* better abort ? */
+
+ base = onetenth;
+ tmp = mulflt (base2flt (ch - '0', 0), base);
+ res = addflt (res, tmp);
+
+ while ((ch = *p++))
+ {
+ if (!isdigit (ch))
+ return INFFLT; /* better abort? */
+
+ base = mulflt (base, onetenth);
+ tmp = mulflt (base2flt (ch - '0', 0), base);
+ res = addflt (res, tmp);
+ }
+ }
+
+ return res;
+}
+
+#if defined(VISCORES)
+
+static double
+flt2double (Flt f)
+{
+ double res;
+ unsigned m;
+ int e, i;
+
+ UNPACKFLT (f, m, e);
+ res = m;
+
+ if (e < 0)
+ {
+ for (i = e; i < 0; i++)
+ res *= 0.5;
+ }
+ else
+ {
+ for (i = 0; i < e; i++)
+ res *= 2.0;
+ }
+
+ return res;
+}
+
+#endif
+
+static INLINE int
+log2flt (Flt a)
+{
+ return FLTEXPONENT (a) + 24;
+}
+
+static INLINE int
+cmpflt (Flt a, Flt b)
+{
+ if (a < b)
+ return -1;
+
+ if (a > b)
+ return 1;
+
+ return 0;
+}
+
+static void *
+new (PS * ps, size_t size)
+{
+ size_t bytes;
+ Blk *b;
+
+ if (!size)
+ return 0;
+
+ bytes = size + SIZE_OF_BLK;
+
+ if (ps->enew)
+ b = ps->enew (ps->emgr, bytes);
+ else
+ b = malloc (bytes);
+
+ ABORTIF (!b, "out of memory in 'new'");
+#ifndef NDEBUG
+ b->header.size = size;
+#endif
+ ps->current_bytes += size;
+ if (ps->current_bytes > ps->max_bytes)
+ ps->max_bytes = ps->current_bytes;
+ return b->data;
+}
+
+static void
+delete (PS * ps, void *void_ptr, size_t size)
+{
+ size_t bytes;
+ Blk *b;
+
+ if (!void_ptr)
+ {
+ assert (!size);
+ return;
+ }
+
+ assert (size);
+ b = PTR2BLK (void_ptr);
+
+ assert (size <= ps->current_bytes);
+ ps->current_bytes -= size;
+
+ assert (b->header.size == size);
+
+ bytes = size + SIZE_OF_BLK;
+ if (ps->edelete)
+ ps->edelete (ps->emgr, b, bytes);
+ else
+ free (b);
+}
+
+static void *
+resize (PS * ps, void *void_ptr, size_t old_size, size_t new_size)
+{
+ size_t old_bytes, new_bytes;
+ Blk *b;
+
+ b = PTR2BLK (void_ptr);
+
+ assert (old_size <= ps->current_bytes);
+ ps->current_bytes -= old_size;
+
+ if ((old_bytes = old_size))
+ {
+ assert (old_size && b && b->header.size == old_size);
+ old_bytes += SIZE_OF_BLK;
+ }
+ else
+ assert (!b);
+
+ if ((new_bytes = new_size))
+ new_bytes += SIZE_OF_BLK;
+
+ if (ps->eresize)
+ b = ps->eresize (ps->emgr, b, old_bytes, new_bytes);
+ else
+ b = realloc (b, new_bytes);
+
+ if (!new_size)
+ {
+ assert (!b);
+ return 0;
+ }
+
+ ABORTIF (!b, "out of memory in 'resize'");
+#ifndef NDEBUG
+ b->header.size = new_size;
+#endif
+
+ ps->current_bytes += new_size;
+ if (ps->current_bytes > ps->max_bytes)
+ ps->max_bytes = ps->current_bytes;
+
+ return b->data;
+}
+
+static INLINE unsigned
+int2unsigned (int l)
+{
+ return (l < 0) ? 1 + 2 * -l : 2 * l;
+}
+
+static INLINE Lit *
+int2lit (PS * ps, int l)
+{
+ return ps->lits + int2unsigned (l);
+}
+
+static INLINE Lit **
+end_of_lits (Cls * c)
+{
+ return (Lit**)c->lits + c->size;
+}
+
+#if !defined(NDEBUG) || defined(LOGGING)
+
+static void
+dumplits (PS * ps, Lit ** l, Lit ** end)
+{
+ int first;
+ Lit ** p;
+
+ if (l == end)
+ {
+ /* empty clause */
+ }
+ else if (l + 1 == end)
+ {
+ fprintf (ps->out, "%d ", LIT2INT (l[0]));
+ }
+ else
+ {
+ assert (l + 2 <= end);
+ first = (abs (LIT2INT (l[0])) > abs (LIT2INT (l[1])));
+ fprintf (ps->out, "%d ", LIT2INT (l[first]));
+ fprintf (ps->out, "%d ", LIT2INT (l[!first]));
+ for (p = l + 2; p < end; p++)
+ fprintf (ps->out, "%d ", LIT2INT (*p));
+ }
+
+ fputc ('0', ps->out);
+}
+
+static void
+dumpcls (PS * ps, Cls * c)
+{
+ Lit **end;
+
+ if (c)
+ {
+ end = end_of_lits (c);
+ dumplits (ps, c->lits, end);
+#ifdef TRACE
+ if (ps->trace)
+ fprintf (ps->out, " clause(%u)", CLS2IDX (c));
+#endif
+ }
+ else
+ fputs ("DECISION", ps->out);
+}
+
+static void
+dumpclsnl (PS * ps, Cls * c)
+{
+ dumpcls (ps, c);
+ fputc ('\n', ps->out);
+}
+
+void
+dumpcnf (PS * ps)
+{
+ Cls **p, *c;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ dumpclsnl (ps, *p);
+ }
+}
+
+#endif
+
+static INLINE void
+delete_prefix (PS * ps)
+{
+ if (!ps->prefix)
+ return;
+
+ delete (ps, ps->prefix, strlen (ps->prefix) + 1);
+ ps->prefix = 0;
+}
+
+static void
+new_prefix (PS * ps, const char * str)
+{
+ delete_prefix (ps);
+ assert (str);
+ ps->prefix = new (ps, strlen (str) + 1);
+ strcpy (ps->prefix, str);
+}
+
+static PS *
+init (void * pmgr,
+ picosat_malloc pnew, picosat_realloc presize, picosat_free pdelete)
+{
+ PS * ps;
+
+#if 0
+ int count = 3 - !pnew - !presize - !pdelete;
+
+ ABORTIF (count && !pnew, "API usage: missing 'picosat_set_new'");
+ ABORTIF (count && !presize, "API usage: missing 'picosat_set_resize'");
+ ABORTIF (count && !pdelete, "API usage: missing 'picosat_set_delete'");
+#endif
+
+ ps = pnew ? pnew (pmgr, sizeof *ps) : malloc (sizeof *ps);
+ ABORTIF (!ps, "failed to allocate memory for PicoSAT manager");
+ memset (ps, 0, sizeof *ps);
+
+ ps->emgr = pmgr;
+ ps->enew = pnew;
+ ps->eresize = presize;
+ ps->edelete = pdelete;
+
+ ps->size_vars = 1;
+ ps->state = RESET;
+ ps->defaultphase = JWLPHASE;
+#ifdef TRACE
+ ps->ocore = -1;
+#endif
+ ps->lastrheader = -2;
+#ifndef NADC
+ ps->adoconflictlimit = UINT_MAX;
+#endif
+ ps->min_flipped = UINT_MAX;
+
+ NEWN (ps->lits, 2 * ps->size_vars);
+ NEWN (ps->jwh, 2 * ps->size_vars);
+ NEWN (ps->htps, 2 * ps->size_vars);
+#ifndef NDSC
+ NEWN (ps->dhtps, 2 * ps->size_vars);
+#endif
+ NEWN (ps->impls, 2 * ps->size_vars);
+ NEWN (ps->vars, ps->size_vars);
+ NEWN (ps->rnks, ps->size_vars);
+
+ /* because '0' pos denotes not on heap
+ */
+ ENLARGE (ps->heap, ps->hhead, ps->eoh);
+ ps->hhead = ps->heap + 1;
+
+ ps->vinc = base2flt (1, 0); /* initial var activity */
+ ps->ifvinc = ascii2flt ("1.05"); /* var score rescore factor */
+#ifdef VISCORES
+ ps->fvinc = ascii2flt ("0.9523809"); /* 1/f = 1/1.05 */
+ ps->nvinc = ascii2flt ("0.0476191"); /* 1 - 1/f = 1 - 1/1.05 */
+#endif
+ ps->lscore = base2flt (1, 90); /* var activity rescore limit */
+ ps->ilvinc = base2flt (1, -90); /* inverse of 'lscore' */
+
+ ps->cinc = base2flt (1, 0); /* initial clause activity */
+ ps->fcinc = ascii2flt ("1.001"); /* cls activity rescore factor */
+ ps->lcinc = base2flt (1, 90); /* cls activity rescore limit */
+ ps->ilcinc = base2flt (1, -90); /* inverse of 'ilcinc' */
+
+ ps->lreduceadjustcnt = ps->lreduceadjustinc = 100;
+ ps->lpropagations = ~0ull;
+
+#ifndef RCODE
+ ps->out = stdout;
+#else
+ ps->out = 0;
+#endif
+ new_prefix (ps, "c ");
+ ps->verbosity = 0;
+ ps->plain = 0;
+
+#ifdef NO_BINARY_CLAUSES
+ memset (&ps->impl, 0, sizeof (ps->impl));
+ ps->impl.size = 2;
+
+ memset (&ps->cimpl, 0, sizeof (ps->impl));
+ ps->cimpl.size = 2;
+#endif
+
+#ifdef VISCORES
+ ps->fviscores = popen (
+ "/usr/bin/gnuplot -background black"
+ " -xrm 'gnuplot*textColor:white'"
+ " -xrm 'gnuplot*borderColor:white'"
+ " -xrm 'gnuplot*axisColor:white'"
+ , "w");
+ fprintf (ps->fviscores, "unset key\n");
+ // fprintf (ps->fviscores, "set log y\n");
+ fflush (ps->fviscores);
+ system ("rm -rf /tmp/picosat-viscores");
+ system ("mkdir /tmp/picosat-viscores");
+ system ("mkdir /tmp/picosat-viscores/data");
+#ifdef WRITEGIF
+ system ("mkdir /tmp/picosat-viscores/gif");
+ fprintf (ps->fviscores,
+ "set terminal gif giant animate opt size 1024,768 x000000 xffffff"
+ "\n");
+
+ fprintf (ps->fviscores,
+ "set output \"/tmp/picosat-viscores/gif/animated.gif\"\n");
+#endif
+#endif
+ ps->defaultphase = JWLPHASE;
+ ps->state = READY;
+ ps->last_sat_call_result = 0;
+
+ return ps;
+}
+
+static size_t
+bytes_clause (PS * ps, unsigned size, unsigned learned)
+{
+ size_t res;
+
+ res = sizeof (Cls);
+ res += size * sizeof (Lit *);
+ res -= 2 * sizeof (Lit *);
+
+ if (learned && size > 2)
+ res += sizeof (Act); /* add activity */
+
+#ifdef TRACE
+ if (ps->trace)
+ res += sizeof (Trd); /* add trace data */
+#else
+ (void) ps;
+#endif
+
+ return res;
+}
+
+static Cls *
+new_clause (PS * ps, unsigned size, unsigned learned)
+{
+ size_t bytes;
+ void * tmp;
+#ifdef TRACE
+ Trd *trd;
+#endif
+ Cls *res;
+
+ bytes = bytes_clause (ps, size, learned);
+ tmp = new (ps, bytes);
+
+#ifdef TRACE
+ if (ps->trace)
+ {
+ trd = tmp;
+
+ if (learned)
+ trd->idx = LIDX2IDX (ps->lhead - ps->lclauses);
+ else
+ trd->idx = OIDX2IDX (ps->ohead - ps->oclauses);
+
+ res = trd->cls;
+ }
+ else
+#endif
+ res = tmp;
+
+ res->size = size;
+ res->learned = learned;
+
+ res->collect = 0;
+#ifndef NDEBUG
+ res->connected = 0;
+#endif
+ res->locked = 0;
+ res->used = 0;
+#ifdef TRACE
+ res->core = 0;
+ res->collected = 0;
+#endif
+
+ if (learned && size > 2)
+ {
+ Act * p = CLS2ACT (res);
+ *p = ps->cinc;
+ }
+
+ return res;
+}
+
+static void
+delete_clause (PS * ps, Cls * c)
+{
+ size_t bytes;
+#ifdef TRACE
+ Trd *trd;
+#endif
+
+ bytes = bytes_clause (ps, c->size, c->learned);
+
+#ifdef TRACE
+ if (ps->trace)
+ {
+ trd = CLS2TRD (c);
+ delete (ps, trd, bytes);
+ }
+ else
+#endif
+ delete (ps, c, bytes);
+}
+
+static void
+delete_clauses (PS * ps)
+{
+ Cls **p;
+ for (p = SOC; p != EOC; p = NXC (p))
+ if (*p)
+ delete_clause (ps, *p);
+
+ DELETEN (ps->oclauses, ps->eoo - ps->oclauses);
+ DELETEN (ps->lclauses, ps->EOL - ps->lclauses);
+
+ ps->ohead = ps->eoo = ps->lhead = ps->EOL = 0;
+}
+
+#ifdef TRACE
+
+static void
+delete_zhain (PS * ps, Zhn * zhain)
+{
+ const Znt *p, *znt;
+
+ assert (zhain);
+
+ znt = zhain->znt;
+ for (p = znt; *p; p++)
+ ;
+
+ delete (ps, zhain, sizeof (Zhn) + (p - znt) + 1);
+}
+
+static void
+delete_zhains (PS * ps)
+{
+ Zhn **p, *z;
+ for (p = ps->zhains; p < ps->zhead; p++)
+ if ((z = *p))
+ delete_zhain (ps, z);
+
+ DELETEN (ps->zhains, ps->eoz - ps->zhains);
+ ps->eoz = ps->zhead = 0;
+}
+
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+static void
+lrelease (PS * ps, Ltk * stk)
+{
+ if (stk->start)
+ DELETEN (stk->start, (1 << (stk->ldsize)));
+ memset (stk, 0, sizeof (*stk));
+}
+#endif
+
+#ifndef NADC
+
+static INLINE unsigned
+llength (Lit ** a)
+{
+ Lit ** p;
+ for (p = a; *p; p++)
+ ;
+ return p - a;
+}
+
+static INLINE void
+resetadoconflict (PS * ps)
+{
+ assert (ps->adoconflict);
+ delete_clause (ps, ps->adoconflict);
+ ps->adoconflict = 0;
+}
+
+static INLINE void
+reset_ados (PS * ps)
+{
+ Lit *** p;
+
+ for (p = ps->ados; p < ps->hados; p++)
+ DELETEN (*p, llength (*p) + 1);
+
+ DELETEN (ps->ados, ps->eados - ps->ados);
+ ps->hados = ps->eados = 0;
+
+ DELETEN (ps->adotab, ps->szadotab);
+ ps->szadotab = ps->nadotab = 0;
+
+ if (ps->adoconflict)
+ resetadoconflict (ps);
+
+ ps->adoconflicts = 0;
+ ps->adoconflictlimit = UINT_MAX;
+ ps->adodisabled = 0;
+}
+
+#endif
+
+static void
+reset (PS * ps)
+{
+ ABORTIF (!ps ||
+ ps->state == RESET, "API usage: reset without initialization");
+
+ delete_clauses (ps);
+#ifdef TRACE
+ delete_zhains (ps);
+#endif
+#ifdef NO_BINARY_CLAUSES
+ {
+ unsigned i;
+ for (i = 2; i <= 2 * ps->max_var + 1; i++)
+ lrelease (ps, ps->impls + i);
+ }
+#endif
+#ifndef NADC
+ reset_ados (ps);
+#endif
+#ifndef NFL
+ DELETEN (ps->saved, ps->saved_size);
+#endif
+ DELETEN (ps->htps, 2 * ps->size_vars);
+#ifndef NDSC
+ DELETEN (ps->dhtps, 2 * ps->size_vars);
+#endif
+ DELETEN (ps->impls, 2 * ps->size_vars);
+ DELETEN (ps->lits, 2 * ps->size_vars);
+ DELETEN (ps->jwh, 2 * ps->size_vars);
+ DELETEN (ps->vars, ps->size_vars);
+ DELETEN (ps->rnks, ps->size_vars);
+ DELETEN (ps->trail, ps->eot - ps->trail);
+ DELETEN (ps->heap, ps->eoh - ps->heap);
+ DELETEN (ps->als, ps->eoals - ps->als);
+ DELETEN (ps->CLS, ps->eocls - ps->CLS);
+ DELETEN (ps->rils, ps->eorils - ps->rils);
+ DELETEN (ps->cils, ps->eocils - ps->cils);
+ DELETEN (ps->fals, ps->eofals - ps->fals);
+ DELETEN (ps->mass, ps->szmass);
+ DELETEN (ps->mssass, ps->szmssass);
+ DELETEN (ps->mcsass, ps->szmcsass);
+ DELETEN (ps->humus, ps->szhumus);
+ DELETEN (ps->added, ps->eoa - ps->added);
+ DELETEN (ps->marked, ps->eom - ps->marked);
+ DELETEN (ps->dfs, ps->eod - ps->dfs);
+ DELETEN (ps->resolved, ps->eor - ps->resolved);
+ DELETEN (ps->levels, ps->eolevels - ps->levels);
+ DELETEN (ps->dused, ps->eodused - ps->dused);
+ DELETEN (ps->buffer, ps->eob - ps->buffer);
+ DELETEN (ps->indices, ps->eoi - ps->indices);
+ DELETEN (ps->soclauses, ps->eoso - ps->soclauses);
+ delete_prefix (ps);
+ delete (ps, ps->rline[0], ps->szrline);
+ delete (ps, ps->rline[1], ps->szrline);
+ assert (getenv ("LEAK") || !ps->current_bytes); /* found leak if failing */
+#ifdef VISCORES
+ pclose (ps->fviscores);
+#endif
+ if (ps->edelete)
+ ps->edelete (ps->emgr, ps, sizeof *ps);
+ else
+ free (ps);
+}
+
+inline static void
+tpush (PS * ps, Lit * lit)
+{
+ assert (ps->lits < lit && lit <= ps->lits + 2* ps->max_var + 1);
+ if (ps->thead == ps->eot)
+ {
+ unsigned ttail2count = ps->ttail2 - ps->trail;
+ unsigned ttailcount = ps->ttail - ps->trail;
+#ifndef NADC
+ unsigned ttailadocount = ps->ttailado - ps->trail;
+#endif
+ ENLARGE (ps->trail, ps->thead, ps->eot);
+ ps->ttail = ps->trail + ttailcount;
+ ps->ttail2 = ps->trail + ttail2count;
+#ifndef NADC
+ ps->ttailado = ps->trail + ttailadocount;
+#endif
+ }
+
+ *ps->thead++ = lit;
+}
+
+static INLINE void
+assign_reason (PS * ps, Var * v, Cls * reason)
+{
+#if defined(NO_BINARY_CLAUSES) && !defined(NDEBUG)
+ assert (reason != &ps->impl);
+#else
+ (void) ps;
+#endif
+ v->reason = reason;
+}
+
+static void
+assign_phase (PS * ps, Lit * lit)
+{
+ unsigned new_phase, idx;
+ Var * v = LIT2VAR (lit);
+
+#ifndef NFL
+ /* In 'simplifying' mode we only need to keep 'min_flipped' up to date if
+ * we force assignments on the top level. The other assignments will be
+ * undone and thus we can keep the old saved value of the phase.
+ */
+ if (!ps->LEVEL || !ps->simplifying)
+#endif
+ {
+ new_phase = (LIT2SGN (lit) > 0);
+
+ if (v->assigned)
+ {
+ ps->sdflips -= ps->sdflips/FFLIPPED;
+
+ if (new_phase != v->phase)
+ {
+ assert (FFLIPPEDPREC >= FFLIPPED);
+ ps->sdflips += FFLIPPEDPREC / FFLIPPED;
+ ps->flips++;
+
+ idx = LIT2IDX (lit);
+ if (idx < ps->min_flipped)
+ ps->min_flipped = idx;
+
+ NOLOG (fprintf (ps->out,
+ "%sflipped %d\n",
+ ps->prefix, LIT2INT (lit)));
+ }
+ }
+
+ v->phase = new_phase;
+ v->assigned = 1;
+ }
+
+ lit->val = TRUE;
+ NOTLIT (lit)->val = FALSE;
+}
+
+inline static void
+assign (PS * ps, Lit * lit, Cls * reason)
+{
+ Var * v = LIT2VAR (lit);
+ assert (lit->val == UNDEF);
+#ifdef STATS
+ ps->assignments++;
+#endif
+ v->level = ps->LEVEL;
+ assign_phase (ps, lit);
+ assign_reason (ps, v, reason);
+ tpush (ps, lit);
+}
+
+inline static int
+cmp_added (PS * ps, Lit * k, Lit * l)
+{
+ Val a = k->val, b = l->val;
+ Var *u, *v;
+ int res;
+
+ if (a == UNDEF && b != UNDEF)
+ return -1;
+
+ if (a != UNDEF && b == UNDEF)
+ return 1;
+
+ u = LIT2VAR (k);
+ v = LIT2VAR (l);
+
+ if (a != UNDEF)
+ {
+ assert (b != UNDEF);
+ res = v->level - u->level;
+ if (res)
+ return res; /* larger level first */
+ }
+
+ res = cmpflt (VAR2RNK (u)->score, VAR2RNK (v)->score);
+ if (res)
+ return res; /* smaller activity first */
+
+ return u - v; /* smaller index first */
+}
+
+static INLINE void
+sorttwolits (Lit ** v)
+{
+ Lit * a = v[0], * b = v[1];
+
+ assert (a != b);
+
+ if (a < b)
+ return;
+
+ v[0] = b;
+ v[1] = a;
+}
+
+inline static void
+sortlits (PS * ps, Lit ** v, unsigned size)
+{
+ if (size == 2)
+ sorttwolits (v); /* same order with and with out 'NO_BINARY_CLAUSES' */
+ else
+ SORT (Lit *, cmp_added, v, size);
+}
+
+#ifdef NO_BINARY_CLAUSES
+static INLINE Cls *
+setimpl (PS * ps, Lit * a, Lit * b)
+{
+ assert (!ps->implvalid);
+ assert (ps->impl.size == 2);
+
+ ps->impl.lits[0] = a;
+ ps->impl.lits[1] = b;
+
+ sorttwolits (ps->impl.lits);
+ ps->implvalid = 1;
+
+ return &ps->impl;
+}
+
+static INLINE void
+resetimpl (PS * ps)
+{
+ ps->implvalid = 0;
+}
+
+static Cls *
+setcimpl (PS * ps, Lit * a, Lit * b)
+{
+ assert (!ps->cimplvalid);
+ assert (ps->cimpl.size == 2);
+
+ ps->cimpl.lits[0] = a;
+ ps->cimpl.lits[1] = b;
+
+ sorttwolits (ps->cimpl.lits);
+ ps->cimplvalid = 1;
+
+ return &ps->cimpl;
+}
+
+static INLINE void
+resetcimpl (PS * ps)
+{
+ assert (ps->cimplvalid);
+ ps->cimplvalid = 0;
+}
+
+#endif
+
+static INLINE int
+cmp_ptr (PS * ps, void *l, void *k)
+{
+ (void) ps;
+ return ((char*)l) - (char*)k; /* arbitrarily already reverse */
+}
+
+static int
+cmp_rnk (Rnk * r, Rnk * s)
+{
+ if (!r->moreimportant && s->moreimportant)
+ return -1;
+
+ if (r->moreimportant && !s->moreimportant)
+ return 1;
+
+ if (!r->lessimportant && s->lessimportant)
+ return 1;
+
+ if (r->lessimportant && !s->lessimportant)
+ return -1;
+
+ if (r->score < s->score)
+ return -1;
+
+ if (r->score > s->score)
+ return 1;
+
+ return -cmp_ptr (0, r, s);
+}
+
+static void
+hup (PS * ps, Rnk * v)
+{
+ int upos, vpos;
+ Rnk *u;
+
+#ifndef NFL
+ assert (!ps->simplifying);
+#endif
+
+ vpos = v->pos;
+
+ assert (0 < vpos);
+ assert (vpos < ps->hhead - ps->heap);
+ assert (ps->heap[vpos] == v);
+
+ while (vpos > 1)
+ {
+ upos = vpos / 2;
+
+ u = ps->heap[upos];
+
+ if (cmp_rnk (u, v) > 0)
+ break;
+
+ ps->heap[vpos] = u;
+ u->pos = vpos;
+
+ vpos = upos;
+ }
+
+ ps->heap[vpos] = v;
+ v->pos = vpos;
+}
+
+static Cls *add_simplified_clause (PS *, int);
+
+inline static void
+add_antecedent (PS * ps, Cls * c)
+{
+ assert (c);
+
+#ifdef NO_BINARY_CLAUSES
+ if (ISLITREASON (c))
+ return;
+
+ if (c == &ps->impl)
+ return;
+#elif defined(STATS) && defined(TRACE)
+ ps->antecedents++;
+#endif
+ if (ps->rhead == ps->eor)
+ ENLARGE (ps->resolved, ps->rhead, ps->eor);
+
+ assert (ps->rhead < ps->eor);
+ *ps->rhead++ = c;
+}
+
+#ifdef TRACE
+
+#ifdef NO_BINARY_CLAUSES
+#error "can not combine TRACE and NO_BINARY_CLAUSES"
+#endif
+
+#endif /* TRACE */
+
+static INLINE void
+add_lit (PS * ps, Lit * lit)
+{
+ assert (lit);
+
+ if (ps->ahead == ps->eoa)
+ ENLARGE (ps->added, ps->ahead, ps->eoa);
+
+ *ps->ahead++ = lit;
+}
+
+static INLINE void
+push_var_as_marked (PS * ps, Var * v)
+{
+ if (ps->mhead == ps->eom)
+ ENLARGE (ps->marked, ps->mhead, ps->eom);
+
+ *ps->mhead++ = v;
+}
+
+static INLINE void
+mark_var (PS * ps, Var * v)
+{
+ assert (!v->mark);
+ v->mark = 1;
+ push_var_as_marked (ps, v);
+}
+
+#ifdef NO_BINARY_CLAUSES
+
+static Cls *
+impl2reason (PS * ps, Lit * lit)
+{
+ Lit * other;
+ Cls * res;
+ other = ps->impl.lits[0];
+ if (lit == other)
+ other = ps->impl.lits[1];
+ assert (other->val == FALSE);
+ res = LIT2REASON (NOTLIT (other));
+ resetimpl (ps);
+ return res;
+}
+
+#endif
+
+/* Whenever we have a top level derived unit we really should derive a unit
+ * clause otherwise the resolutions in 'add_simplified_clause' become
+ * incorrect.
+ */
+static Cls *
+resolve_top_level_unit (PS * ps, Lit * lit, Cls * reason)
+{
+ unsigned count_resolved;
+ Lit **p, **eol, *other;
+ Var *u, *v;
+
+ assert (ps->rhead == ps->resolved);
+ assert (ps->ahead == ps->added);
+
+ add_lit (ps, lit);
+ add_antecedent (ps, reason);
+ count_resolved = 1;
+ v = LIT2VAR (lit);
+
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p < eol; p++)
+ {
+ other = *p;
+ u = LIT2VAR (other);
+ if (u == v)
+ continue;
+
+ add_antecedent (ps, u->reason);
+ count_resolved++;
+ }
+
+ /* Some of the literals could be assumptions. If at least one
+ * variable is not an assumption, we should resolve.
+ */
+ if (count_resolved >= 2)
+ {
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+ reason = add_simplified_clause (ps, 1);
+#ifdef NO_BINARY_CLAUSES
+ if (reason->size == 2)
+ {
+ assert (reason == &ps->impl);
+ reason = impl2reason (ps, lit);
+ }
+#endif
+ assign_reason (ps, v, reason);
+ }
+ else
+ {
+ ps->ahead = ps->added;
+ ps->rhead = ps->resolved;
+ }
+
+ return reason;
+}
+
+static void
+fixvar (PS * ps, Var * v)
+{
+ Rnk * r;
+
+ assert (VAR2LIT (v) != UNDEF);
+ assert (!v->level);
+
+ ps->fixed++;
+
+ r = VAR2RNK (v);
+ r->score = INFFLT;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return;
+#endif
+
+ if (!r->pos)
+ return;
+
+ hup (ps, r);
+}
+
+static INLINE void
+use_var (PS * ps, Var * v)
+{
+ if (v->used)
+ return;
+
+ v->used = 1;
+ ps->vused++;
+}
+
+static void
+assign_forced (PS * ps, Lit * lit, Cls * reason)
+{
+ Var *v;
+
+ assert (reason);
+ assert (lit->val == UNDEF);
+
+#ifdef STATS
+ ps->FORCED++;
+#endif
+ assign (ps, lit, reason);
+
+#ifdef NO_BINARY_CLAUSES
+ assert (reason != &ps->impl);
+ if (ISLITREASON (reason))
+ {
+ reason = setimpl (ps, lit, NOTLIT (REASON2LIT (reason)));
+ assert (reason);
+ }
+#endif
+ LOG ( fprintf (ps->out,
+ "%sassign %d at level %d by ",
+ ps->prefix, LIT2INT (lit), ps->LEVEL);
+ dumpclsnl (ps, reason));
+
+ v = LIT2VAR (lit);
+ if (!ps->LEVEL)
+ use_var (ps, v);
+
+ if (!ps->LEVEL && reason->size > 1)
+ {
+ reason = resolve_top_level_unit (ps, lit, reason);
+ assert (reason);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (ISLITREASON (reason) || reason == &ps->impl)
+ {
+ /* DO NOTHING */
+ }
+ else
+#endif
+ {
+ assert (!reason->locked);
+ reason->locked = 1;
+ if (reason->learned && reason->size > 2)
+ ps->llocked++;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+
+ if (!ps->LEVEL)
+ fixvar (ps, v);
+}
+
+#ifdef NO_BINARY_CLAUSES
+
+static void
+lpush (PS * ps, Lit * lit, Cls * c)
+{
+ int pos = (c->lits[0] == lit);
+ Ltk * s = LIT2IMPLS (lit);
+ unsigned oldsize, newsize;
+
+ assert (c->size == 2);
+
+ if (!s->start)
+ {
+ assert (!s->count);
+ assert (!s->ldsize);
+ NEWN (s->start, 1);
+ }
+ else
+ {
+ oldsize = (1 << (s->ldsize));
+ assert (s->count <= oldsize);
+ if (s->count == oldsize)
+ {
+ newsize = 2 * oldsize;
+ RESIZEN (s->start, oldsize, newsize);
+ s->ldsize++;
+ }
+ }
+
+ s->start[s->count++] = c->lits[pos];
+}
+
+#endif
+
+static void
+connect_head_tail (PS * ps, Lit * lit, Cls * c)
+{
+ Cls ** s;
+ assert (c->size >= 1);
+ if (c->size == 2)
+ {
+#ifdef NO_BINARY_CLAUSES
+ lpush (ps, lit, c);
+ return;
+#else
+ s = LIT2IMPLS (lit);
+#endif
+ }
+ else
+ s = LIT2HTPS (lit);
+
+ if (c->lits[0] != lit)
+ {
+ assert (c->size >= 2);
+ assert (c->lits[1] == lit);
+ c->next[1] = *s;
+ }
+ else
+ c->next[0] = *s;
+
+ *s = c;
+}
+
+#ifdef TRACE
+static void
+zpush (PS * ps, Zhn * zhain)
+{
+ assert (ps->trace);
+
+ if (ps->zhead == ps->eoz)
+ ENLARGE (ps->zhains, ps->zhead, ps->eoz);
+
+ *ps->zhead++ = zhain;
+}
+
+static INLINE int
+cmp_resolved (PS * ps, Cls * c, Cls * d)
+{
+#ifndef NDEBUG
+ assert (ps->trace);
+#else
+ (void) ps;
+#endif
+ return CLS2IDX (c) - CLS2IDX (d);
+}
+
+static INLINE void
+bpushc (PS * ps, unsigned char ch)
+{
+ if (ps->bhead == ps->eob)
+ ENLARGE (ps->buffer, ps->bhead, ps->eob);
+
+ *ps->bhead++ = ch;
+}
+
+static INLINE void
+bpushu (PS * ps, unsigned u)
+{
+ while (u & ~0x7f)
+ {
+ bpushc (ps, u | 0x80);
+ u >>= 7;
+ }
+
+ bpushc (ps, u);
+}
+
+static INLINE void
+bpushd (PS * ps, unsigned prev, unsigned this)
+{
+ unsigned delta;
+ assert (prev < this);
+ delta = this - prev;
+ bpushu (ps, delta);
+}
+
+static void
+add_zhain (PS * ps)
+{
+ unsigned prev, this, count, rcount;
+ Cls **p, *c;
+ Zhn *res;
+
+ assert (ps->trace);
+ assert (ps->bhead == ps->buffer);
+ assert (ps->rhead > ps->resolved);
+
+ rcount = ps->rhead - ps->resolved;
+ SORT (Cls *, cmp_resolved, ps->resolved, rcount);
+
+ prev = 0;
+ for (p = ps->resolved; p < ps->rhead; p++)
+ {
+ c = *p;
+ this = CLS2TRD (c)->idx;
+ bpushd (ps, prev, this);
+ prev = this;
+ }
+ bpushc (ps, 0);
+
+ count = ps->bhead - ps->buffer;
+
+ res = new (ps, sizeof (Zhn) + count);
+ res->core = 0;
+ res->ref = 0;
+ memcpy (res->znt, ps->buffer, count);
+
+ ps->bhead = ps->buffer;
+#ifdef STATS
+ ps->znts += count - 1;
+#endif
+ zpush (ps, res);
+}
+
+#endif
+
+static void
+add_resolved (PS * ps, int learned)
+{
+#if defined(STATS) || defined(TRACE)
+ Cls **p, *c;
+
+ for (p = ps->resolved; p < ps->rhead; p++)
+ {
+ c = *p;
+ if (c->used)
+ continue;
+
+ c->used = 1;
+
+ if (c->size <= 2)
+ continue;
+
+#ifdef STATS
+ if (c->learned)
+ ps->llused++;
+ else
+ ps->loused++;
+#endif
+ }
+#endif
+
+#ifdef TRACE
+ if (learned && ps->trace)
+ add_zhain (ps);
+#else
+ (void) learned;
+#endif
+ ps->rhead = ps->resolved;
+}
+
+static void
+incjwh (PS * ps, Cls * c)
+{
+ Lit **p, *lit, ** eol;
+ Flt * f, inc, sum;
+ unsigned size = 0;
+ Var * v;
+ Val val;
+
+ eol = end_of_lits (c);
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ val = lit->val;
+
+ if (val && ps->LEVEL > 0)
+ {
+ v = LIT2VAR (lit);
+ if (v->level > 0)
+ val = UNDEF;
+ }
+
+ if (val == TRUE)
+ return;
+
+ if (val != FALSE)
+ size++;
+ }
+
+ inc = base2flt (1, -size);
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ f = LIT2JWH (lit);
+ sum = addflt (*f, inc);
+ *f = sum;
+ }
+}
+
+static void
+write_rup_header (PS * ps, FILE * file)
+{
+ char line[80];
+ int i;
+
+ sprintf (line, "%%RUPD32 %u %u", ps->rupvariables, ps->rupclauses);
+
+ fputs (line, file);
+ for (i = 255 - strlen (line); i >= 0; i--)
+ fputc (' ', file);
+
+ fputc ('\n', file);
+ fflush (file);
+}
+
+static Cls *
+add_simplified_clause (PS * ps, int learned)
+{
+ unsigned num_true, num_undef, num_false, size, count_resolved;
+ Lit **p, **q, *lit, ** end;
+ unsigned litlevel, glue;
+ Cls *res, * reason;
+ int reentered;
+ Val val;
+ Var *v;
+#if !defined(NDEBUG) && defined(TRACE)
+ unsigned idx;
+#endif
+
+ reentered = 0;
+
+REENTER:
+
+ size = ps->ahead - ps->added;
+
+ add_resolved (ps, learned);
+
+ if (learned)
+ {
+ ps->ladded++;
+ ps->llitsadded += size;
+ if (size > 2)
+ {
+ ps->lladded++;
+ ps->nlclauses++;
+ ps->llits += size;
+ }
+ }
+ else
+ {
+ ps->oadded++;
+ if (size > 2)
+ {
+ ps->loadded++;
+ ps->noclauses++;
+ ps->olits += size;
+ }
+ }
+
+ ps->addedclauses++;
+ assert (ps->addedclauses == ps->ladded + ps->oadded);
+
+#ifdef NO_BINARY_CLAUSES
+ if (size == 2)
+ res = setimpl (ps, ps->added[0], ps->added[1]);
+ else
+#endif
+ {
+ sortlits (ps, ps->added, size);
+
+ if (learned)
+ {
+ if (ps->lhead == ps->EOL)
+ {
+ ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
+
+ /* A very difficult to find bug, which only occurs if the
+ * learned clauses stack is immediately allocated before the
+ * original clauses stack without padding. In this case, we
+ * have 'SOC == EOC', which terminates all loops using the
+ * idiom 'for (p = SOC; p != EOC; p = NXC(p))' immediately.
+ * Unfortunately this occurred in 'fix_clause_lits' after
+ * using a recent version of the memory allocator of 'Google'
+ * perftools in the context of one large benchmark for
+ * our SMT solver 'Boolector'.
+ */
+ if (ps->EOL == ps->oclauses)
+ ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
+ }
+
+#if !defined(NDEBUG) && defined(TRACE)
+ idx = LIDX2IDX (ps->lhead - ps->lclauses);
+#endif
+ }
+ else
+ {
+ if (ps->ohead == ps->eoo)
+ {
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
+ if (ps->EOL == ps->oclauses)
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo); /* ditto */
+ }
+
+#if !defined(NDEBUG) && defined(TRACE)
+ idx = OIDX2IDX (ps->ohead - ps->oclauses);
+#endif
+ }
+
+ assert (ps->EOL != ps->oclauses); /* ditto */
+
+ res = new_clause (ps, size, learned);
+
+ glue = 0;
+
+ if (learned)
+ {
+ assert (ps->dusedhead == ps->dused);
+
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ lit = *p;
+ if (lit->val)
+ {
+ litlevel = LIT2VAR (lit)->level;
+ assert (litlevel <= ps->LEVEL);
+ while (ps->levels + litlevel >= ps->levelshead)
+ {
+ if (ps->levelshead >= ps->eolevels)
+ ENLARGE (ps->levels, ps->levelshead, ps->eolevels);
+ assert (ps->levelshead < ps->eolevels);
+ *ps->levelshead++ = 0;
+ }
+ if (!ps->levels[litlevel])
+ {
+ if (ps->dusedhead >= ps->eodused)
+ ENLARGE (ps->dused, ps->dusedhead, ps->eodused);
+ assert (ps->dusedhead < ps->eodused);
+ *ps->dusedhead++ = litlevel;
+ ps->levels[litlevel] = 1;
+ glue++;
+ }
+ }
+ else
+ glue++;
+ }
+
+ while (ps->dusedhead > ps->dused)
+ {
+ litlevel = *--ps->dusedhead;
+ assert (ps->levels + litlevel < ps->levelshead);
+ assert (ps->levels[litlevel]);
+ ps->levels[litlevel] = 0;
+ }
+ }
+
+ assert (glue <= MAXGLUE);
+ res->glue = glue;
+
+#if !defined(NDEBUG) && defined(TRACE)
+ if (ps->trace)
+ assert (CLS2IDX (res) == idx);
+#endif
+ if (learned)
+ *ps->lhead++ = res;
+ else
+ *ps->ohead++ = res;
+
+#if !defined(NDEBUG) && defined(TRACE)
+ if (ps->trace && learned)
+ assert (ps->zhead - ps->zhains == ps->lhead - ps->lclauses);
+#endif
+ assert (ps->lhead != ps->oclauses); /* ditto */
+ }
+
+ if (learned && ps->rup)
+ {
+ if (!ps->rupstarted)
+ {
+ write_rup_header (ps, ps->rup);
+ ps->rupstarted = 1;
+ }
+ }
+
+ num_true = num_undef = num_false = 0;
+
+ q = res->lits;
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ lit = *p;
+ *q++ = lit;
+
+ if (learned && ps->rup)
+ fprintf (ps->rup, "%d ", LIT2INT (lit));
+
+ val = lit->val;
+
+ num_true += (val == TRUE);
+ num_undef += (val == UNDEF);
+ num_false += (val == FALSE);
+ }
+ assert (num_false + num_true + num_undef == size);
+
+ if (learned && ps->rup)
+ fputs ("0\n", ps->rup);
+
+ ps->ahead = ps->added; /* reset */
+
+ if (!reentered) // TODO merge
+ if (size > 0)
+ {
+ assert (size <= 2 || !reentered); // TODO remove
+ connect_head_tail (ps, res->lits[0], res);
+ if (size > 1)
+ connect_head_tail (ps, res->lits[1], res);
+ }
+
+ if (size == 0)
+ {
+ if (!ps->mtcls)
+ ps->mtcls = res;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (size != 2)
+#endif
+#ifndef NDEBUG
+ res->connected = 1;
+#endif
+
+ LOG ( fprintf (ps->out, "%s%s ", ps->prefix, learned ? "learned" : "original");
+ dumpclsnl (ps, res));
+
+ /* Shrink clause by resolving it against top level assignments.
+ */
+ if (!ps->LEVEL && num_false > 0)
+ {
+ assert (ps->ahead == ps->added);
+ assert (ps->rhead == ps->resolved);
+
+ count_resolved = 1;
+ add_antecedent (ps, res);
+
+ end = end_of_lits (res);
+ for (p = res->lits; p < end; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (lit);
+ use_var (ps, v);
+
+ if (lit->val == FALSE)
+ {
+ add_antecedent (ps, v->reason);
+ count_resolved++;
+ }
+ else
+ add_lit (ps, lit);
+ }
+
+ assert (count_resolved >= 2);
+
+ learned = 1;
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ resetimpl (ps);
+#endif
+ reentered = 1;
+ goto REENTER; /* and return simplified clause */
+ }
+
+ if (!num_true && num_undef == 1) /* unit clause */
+ {
+ lit = 0;
+ for (p = res->lits; p < res->lits + size; p++)
+ {
+ if ((*p)->val == UNDEF)
+ lit = *p;
+
+ v = LIT2VAR (*p);
+ use_var (ps, v);
+ }
+ assert (lit);
+
+ reason = res;
+#ifdef NO_BINARY_CLAUSES
+ if (size == 2)
+ {
+ Lit * other = res->lits[0];
+ if (other == lit)
+ other = res->lits[1];
+
+ assert (other->val == FALSE);
+ reason = LIT2REASON (NOTLIT (other));
+ }
+#endif
+ assign_forced (ps, lit, reason);
+ num_true++;
+ }
+
+ if (num_false == size && !ps->conflict)
+ {
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ ps->conflict = setcimpl (ps, res->lits[0], res->lits[1]);
+ else
+#endif
+ ps->conflict = res;
+ }
+
+ if (!learned && !num_true && num_undef)
+ incjwh (ps, res);
+
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ resetimpl (ps);
+#endif
+ return res;
+}
+
+static int
+trivial_clause (PS * ps)
+{
+ Lit **p, **q, *prev;
+ Var *v;
+
+ SORT (Lit *, cmp_ptr, ps->added, ps->ahead - ps->added);
+
+ prev = 0;
+ q = ps->added;
+ for (p = q; p < ps->ahead; p++)
+ {
+ Lit *this = *p;
+
+ v = LIT2VAR (this);
+
+ if (prev == this) /* skip repeated literals */
+ continue;
+
+ /* Top level satisfied ?
+ */
+ if (this->val == TRUE && !v->level)
+ return 1;
+
+ if (prev == NOTLIT (this))/* found pair of dual literals */
+ return 1;
+
+ *q++ = prev = this;
+ }
+
+ ps->ahead = q; /* shrink */
+
+ return 0;
+}
+
+static void
+simplify_and_add_original_clause (PS * ps)
+{
+#ifdef NO_BINARY_CLAUSES
+ Cls * c;
+#endif
+ if (trivial_clause (ps))
+ {
+ ps->ahead = ps->added;
+
+ if (ps->ohead == ps->eoo)
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
+
+ *ps->ohead++ = 0;
+
+ ps->addedclauses++;
+ ps->oadded++;
+ }
+ else
+ {
+ if (ps->CLS != ps->clshead)
+ add_lit (ps, NOTLIT (ps->clshead[-1]));
+
+#ifdef NO_BINARY_CLAUSES
+ c =
+#endif
+ add_simplified_clause (ps, 0);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl) assert (!ps->implvalid);
+#endif
+ }
+}
+
+#ifndef NADC
+
+static void
+add_ado (PS * ps)
+{
+ unsigned len = ps->ahead - ps->added;
+ Lit ** ado, ** p, ** q, *lit;
+ Var * v, * u;
+
+#ifdef TRACE
+ assert (!ps->trace);
+#endif
+
+ ABORTIF (ps->ados < ps->hados && llength (ps->ados[0]) != len,
+ "internal: non matching all different constraint object lengths");
+
+ if (ps->hados == ps->eados)
+ ENLARGE (ps->ados, ps->hados, ps->eados);
+
+ NEWN (ado, len + 1);
+ *ps->hados++ = ado;
+
+ p = ps->added;
+ q = ado;
+ u = 0;
+ while (p < ps->ahead)
+ {
+ lit = *p++;
+ v = LIT2VAR (lit);
+ ABORTIF (v->inado,
+ "internal: variable in multiple all different objects");
+ v->inado = ado;
+ if (!u && !lit->val)
+ u = v;
+ *q++ = lit;
+ }
+
+ assert (q == ado + len);
+ *q++ = 0;
+
+ /* TODO simply do a conflict test as in propado */
+
+ ABORTIF (!u,
+ "internal: "
+ "adding fully instantiated all different object not implemented yet");
+
+ assert (u);
+ assert (u->inado == ado);
+ assert (!u->ado);
+ u->ado = ado;
+
+ ps->ahead = ps->added;
+}
+
+#endif
+
+static void
+hdown (PS * ps, Rnk * r)
+{
+ unsigned end, rpos, cpos, opos;
+ Rnk *child, *other;
+
+ assert (r->pos > 0);
+ assert (ps->heap[r->pos] == r);
+
+ end = ps->hhead - ps->heap;
+ rpos = r->pos;
+
+ for (;;)
+ {
+ cpos = 2 * rpos;
+ if (cpos >= end)
+ break;
+
+ opos = cpos + 1;
+ child = ps->heap[cpos];
+
+ if (cmp_rnk (r, child) < 0)
+ {
+ if (opos < end)
+ {
+ other = ps->heap[opos];
+
+ if (cmp_rnk (child, other) < 0)
+ {
+ child = other;
+ cpos = opos;
+ }
+ }
+ }
+ else if (opos < end)
+ {
+ child = ps->heap[opos];
+
+ if (cmp_rnk (r, child) >= 0)
+ break;
+
+ cpos = opos;
+ }
+ else
+ break;
+
+ ps->heap[rpos] = child;
+ child->pos = rpos;
+ rpos = cpos;
+ }
+
+ r->pos = rpos;
+ ps->heap[rpos] = r;
+}
+
+static Rnk *
+htop (PS * ps)
+{
+ assert (ps->hhead > ps->heap + 1);
+ return ps->heap[1];
+}
+
+static Rnk *
+hpop (PS * ps)
+{
+ Rnk *res, *last;
+ unsigned end;
+
+ assert (ps->hhead > ps->heap + 1);
+
+ res = ps->heap[1];
+ res->pos = 0;
+
+ end = --ps->hhead - ps->heap;
+ if (end == 1)
+ return res;
+
+ last = ps->heap[end];
+
+ ps->heap[last->pos = 1] = last;
+ hdown (ps, last);
+
+ return res;
+}
+
+inline static void
+hpush (PS * ps, Rnk * r)
+{
+ assert (!r->pos);
+
+ if (ps->hhead == ps->eoh)
+ ENLARGE (ps->heap, ps->hhead, ps->eoh);
+
+ r->pos = ps->hhead++ - ps->heap;
+ ps->heap[r->pos] = r;
+ hup (ps, r);
+}
+
+static INLINE void
+fix_trail_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->trail; p < ps->thead; p++)
+ *p += delta;
+}
+
+#ifdef NO_BINARY_CLAUSES
+static void
+fix_impl_lits (PS * ps, long delta)
+{
+ Ltk * s;
+ Lit ** p;
+
+ for (s = ps->impls + 2; s <= ps->impls + 2 * ps->max_var + 1; s++)
+ for (p = s->start; p < s->start + s->count; p++)
+ *p += delta;
+}
+#endif
+
+static void
+fix_clause_lits (PS * ps, long delta)
+{
+ Cls **p, *clause;
+ Lit **q, *lit, **eol;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ clause = *p;
+ if (!clause)
+ continue;
+
+ q = clause->lits;
+ eol = end_of_lits (clause);
+ while (q < eol)
+ {
+ assert (q - clause->lits <= (int) clause->size);
+ lit = *q;
+ lit += delta;
+ *q++ = lit;
+ }
+ }
+}
+
+static INLINE void
+fix_added_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->added; p < ps->ahead; p++)
+ *p += delta;
+}
+
+static INLINE void
+fix_assumed_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->als; p < ps->alshead; p++)
+ *p += delta;
+}
+
+static INLINE void
+fix_cls_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->CLS; p < ps->clshead; p++)
+ *p += delta;
+}
+
+static INLINE void
+fix_heap_rnks (PS * ps, long delta)
+{
+ Rnk **p;
+
+ for (p = ps->heap + 1; p < ps->hhead; p++)
+ *p += delta;
+}
+
+#ifndef NADC
+
+static INLINE void
+fix_ado (long delta, Lit ** ado)
+{
+ Lit ** p;
+ for (p = ado; *p; p++)
+ *p += delta;
+}
+
+static INLINE void
+fix_ados (PS * ps, long delta)
+{
+ Lit *** p;
+
+ for (p = ps->ados; p < ps->hados; p++)
+ fix_ado (delta, *p);
+}
+
+#endif
+
+static void
+enlarge (PS * ps, unsigned new_size_vars)
+{
+ long rnks_delta, lits_delta;
+ Lit *old_lits = ps->lits;
+ Rnk *old_rnks = ps->rnks;
+
+ RESIZEN (ps->lits, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->jwh, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->htps, 2 * ps->size_vars, 2 * new_size_vars);
+#ifndef NDSC
+ RESIZEN (ps->dhtps, 2 * ps->size_vars, 2 * new_size_vars);
+#endif
+ RESIZEN (ps->impls, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->vars, ps->size_vars, new_size_vars);
+ RESIZEN (ps->rnks, ps->size_vars, new_size_vars);
+
+ if ((lits_delta = ps->lits - old_lits))
+ {
+ fix_trail_lits (ps, lits_delta);
+ fix_clause_lits (ps, lits_delta);
+ fix_added_lits (ps, lits_delta);
+ fix_assumed_lits (ps, lits_delta);
+ fix_cls_lits (ps, lits_delta);
+#ifdef NO_BINARY_CLAUSES
+ fix_impl_lits (ps, lits_delta);
+#endif
+#ifndef NADC
+ fix_ados (ps, lits_delta);
+#endif
+ }
+
+ if ((rnks_delta = ps->rnks - old_rnks))
+ {
+ fix_heap_rnks (ps, rnks_delta);
+ }
+
+ assert (ps->mhead == ps->marked);
+
+ ps->size_vars = new_size_vars;
+}
+
+static void
+unassign (PS * ps, Lit * lit)
+{
+ Cls *reason;
+ Var *v;
+ Rnk *r;
+
+ assert (lit->val == TRUE);
+
+ LOG ( fprintf (ps->out, "%sunassign %d\n", ps->prefix, LIT2INT (lit)));
+
+ v = LIT2VAR (lit);
+ reason = v->reason;
+
+#ifdef NO_BINARY_CLAUSES
+ assert (reason != &ps->impl);
+ if (ISLITREASON (reason))
+ {
+ /* DO NOTHING */
+ }
+ else
+#endif
+ if (reason)
+ {
+ assert (reason->locked);
+ reason->locked = 0;
+ if (reason->learned && reason->size > 2)
+ {
+ assert (ps->llocked > 0);
+ ps->llocked--;
+ }
+ }
+
+ lit->val = UNDEF;
+ NOTLIT (lit)->val = UNDEF;
+
+ r = VAR2RNK (v);
+ if (!r->pos)
+ hpush (ps, r);
+
+#ifndef NDSC
+ {
+ Cls * p, * next, ** q;
+
+ q = LIT2DHTPS (lit);
+ p = *q;
+ *q = 0;
+
+ while (p)
+ {
+ Lit * other = p->lits[0];
+
+ if (other == lit)
+ {
+ other = p->lits[1];
+ q = p->next + 1;
+ }
+ else
+ {
+ assert (p->lits[1] == lit);
+ q = p->next;
+ }
+
+ next = *q;
+ *q = *LIT2HTPS (other);
+ *LIT2HTPS (other) = p;
+ p = next;
+ }
+ }
+#endif
+
+#ifndef NADC
+ if (v->adotabpos)
+ {
+ assert (ps->nadotab);
+ assert (*v->adotabpos == v->ado);
+
+ *v->adotabpos = 0;
+ v->adotabpos = 0;
+
+ ps->nadotab--;
+ }
+#endif
+}
+
+static Cls *
+var2reason (PS * ps, Var * var)
+{
+ Cls * res = var->reason;
+#ifdef NO_BINARY_CLAUSES
+ Lit * this, * other;
+ if (ISLITREASON (res))
+ {
+ this = VAR2LIT (var);
+ if (this->val == FALSE)
+ this = NOTLIT (this);
+
+ other = REASON2LIT (res);
+ assert (other->val == TRUE);
+ assert (this->val == TRUE);
+ res = setimpl (ps, NOTLIT (other), this);
+ }
+#else
+ (void) ps;
+#endif
+ return res;
+}
+
+static INLINE void
+mark_clause_to_be_collected (Cls * c)
+{
+ assert (!c->collect);
+ c->collect = 1;
+}
+
+static void
+undo (PS * ps, unsigned new_level)
+{
+ Lit *lit;
+ Var *v;
+
+ while (ps->thead > ps->trail)
+ {
+ lit = *--ps->thead;
+ v = LIT2VAR (lit);
+ if (v->level == new_level)
+ {
+ ps->thead++; /* fix pre decrement */
+ break;
+ }
+
+ unassign (ps, lit);
+ }
+
+ ps->LEVEL = new_level;
+ ps->ttail = ps->thead;
+ ps->ttail2 = ps->thead;
+#ifndef NADC
+ ps->ttailado = ps->thead;
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+#endif
+#ifndef NADC
+ if (ps->conflict && ps->conflict == ps->adoconflict)
+ resetadoconflict (ps);
+#endif
+ ps->conflict = ps->mtcls;
+ if (ps->LEVEL < ps->adecidelevel)
+ {
+ assert (ps->als < ps->alshead);
+ ps->adecidelevel = 0;
+ ps->alstail = ps->als;
+ }
+ LOG ( fprintf (ps->out, "%sback to level %u\n", ps->prefix, ps->LEVEL));
+}
+
+#ifndef NDEBUG
+
+static int
+clause_satisfied (Cls * c)
+{
+ Lit **p, **eol, *lit;
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == TRUE)
+ return 1;
+ }
+
+ return 0;
+}
+
+static void
+original_clauses_satisfied (PS * ps)
+{
+ Cls **p, *c;
+
+ for (p = ps->oclauses; p < ps->ohead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (c->learned)
+ continue;
+
+ assert (clause_satisfied (c));
+ }
+}
+
+static void
+assumptions_satisfied (PS * ps)
+{
+ Lit *lit, ** p;
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ assert (lit->val == TRUE);
+ }
+}
+
+#endif
+
+static void
+sflush (PS * ps)
+{
+#ifdef HAS_FLOAT
+ double now = picosat_time_stamp ();
+ double delta = now - ps->entered;
+ delta = (delta < 0) ? 0 : delta;
+ ps->seconds += delta;
+ ps->entered = now;
+#endif
+}
+
+static double
+mb (PS * ps)
+{
+ return ps->current_bytes / (double) (1 << 20);
+}
+
+static INLINE double
+avglevel (PS * ps)
+{
+ return ps->decisions ? ps->levelsum / ps->decisions : 0.0;
+}
+
+static void
+rheader (PS * ps)
+{
+ assert (ps->lastrheader <= ps->reports);
+
+ if (ps->lastrheader == ps->reports)
+ return;
+
+ ps->lastrheader = ps->reports;
+
+ fprintf (ps->out, "%s\n", ps->prefix);
+ fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[0]);
+ fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[1]);
+ fprintf (ps->out, "%s\n", ps->prefix);
+}
+
+static unsigned
+dynamic_flips_per_assignment_per_mille (PS * ps)
+{
+ assert (FFLIPPEDPREC >= 1000);
+ return ps->sdflips / (FFLIPPEDPREC / 1000);
+}
+
+#ifdef NLUBY
+
+static int
+high_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 200;
+}
+
+static int
+very_high_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 250;
+}
+
+#else
+
+static int
+medium_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 230;
+}
+
+#endif
+
+static void
+relemdata (PS * ps)
+{
+ char *p;
+ int x;
+
+ if (ps->reports < 0)
+ {
+ /* strip trailing white space
+ */
+ for (x = 0; x <= 1; x++)
+ {
+ p = ps->rline[x] + strlen (ps->rline[x]);
+ while (p-- > ps->rline[x])
+ {
+ if (*p != ' ')
+ break;
+
+ *p = 0;
+ }
+ }
+
+ rheader (ps);
+ }
+ else
+ fputc ('\n', ps->out);
+
+ ps->RCOUNT = 0;
+}
+
+static void
+relemhead (PS * ps, const char * name, int fp, double val)
+{
+ int x, y, len, size;
+ const char *fmt;
+ unsigned tmp, e;
+
+ if (ps->reports < 0)
+ {
+ x = ps->RCOUNT & 1;
+ y = (ps->RCOUNT / 2) * 12 + x * 6;
+
+ if (ps->RCOUNT == 1)
+ sprintf (ps->rline[1], "%6s", "");
+
+ len = strlen (name);
+ while (ps->szrline <= len + y + 1)
+ {
+ size = ps->szrline ? 2 * ps->szrline : 128;
+ ps->rline[0] = resize (ps, ps->rline[0], ps->szrline, size);
+ ps->rline[1] = resize (ps, ps->rline[1], ps->szrline, size);
+ ps->szrline = size;
+ }
+
+ fmt = (len <= 6) ? "%6s%10s" : "%-10s%4s";
+ sprintf (ps->rline[x] + y, fmt, name, "");
+ }
+ else if (val < 0)
+ {
+ assert (fp);
+
+ if (val > -100 && (tmp = val * 10.0 - 0.5) > -1000.0)
+ {
+ fprintf (ps->out, "-%4.1f ", -tmp / 10.0);
+ }
+ else
+ {
+ tmp = -val / 10.0 + 0.5;
+ e = 1;
+ while (tmp >= 100)
+ {
+ tmp /= 10;
+ e++;
+ }
+
+ fprintf (ps->out, "-%2ue%u ", tmp, e);
+ }
+ }
+ else
+ {
+ if (fp && val < 1000 && (tmp = val * 10.0 + 0.5) < 10000)
+ {
+ fprintf (ps->out, "%5.1f ", tmp / 10.0);
+ }
+ else if (!fp && (tmp = val) < 100000)
+ {
+ fprintf (ps->out, "%5u ", tmp);
+ }
+ else
+ {
+ tmp = val / 10.0 + 0.5;
+ e = 1;
+
+ while (tmp >= 1000)
+ {
+ tmp /= 10;
+ e++;
+ }
+
+ fprintf (ps->out, "%3ue%u ", tmp, e);
+ }
+ }
+
+ ps->RCOUNT++;
+}
+
+inline static void
+relem (PS * ps, const char *name, int fp, double val)
+{
+ if (name)
+ relemhead (ps, name, fp, val);
+ else
+ relemdata (ps);
+}
+
+static unsigned
+reduce_limit_on_lclauses (PS * ps)
+{
+ unsigned res = ps->lreduce;
+ res += ps->llocked;
+ return res;
+}
+
+static void
+report (PS * ps, int replevel, char type)
+{
+ int rounds;
+
+#ifdef RCODE
+ (void) type;
+#endif
+
+ if (ps->verbosity < replevel)
+ return;
+
+ sflush (ps);
+
+ if (!ps->reports)
+ ps->reports = -1;
+
+ for (rounds = (ps->reports < 0) ? 2 : 1; rounds; rounds--)
+ {
+ if (ps->reports >= 0)
+ fprintf (ps->out, "%s%c ", ps->prefix, type);
+#ifdef DMONNIAUX_DISABLE
+
+ relem (ps, "seconds", 1, ps->seconds);
+ relem (ps, "level", 1, avglevel (ps));
+ assert (ps->fixed <= ps->max_var);
+ relem (ps, "variables", 0, ps->max_var - ps->fixed);
+ relem (ps, "used", 1, PERCENT (ps->vused, ps->max_var));
+ relem (ps, "original", 0, ps->noclauses);
+ relem (ps, "conflicts", 0, ps->conflicts);
+ // relem (ps, "decisions", 0, ps->decisions);
+ // relem (ps, "conf/dec", 1, PERCENT(ps->conflicts,ps->decisions));
+ // relem (ps, "limit", 0, reduce_limit_on_lclauses (ps));
+ relem (ps, "learned", 0, ps->nlclauses);
+ // relem (ps, "limit", 1, PERCENT (ps->nlclauses, reduce_limit_on_lclauses (ps)));
+ relem (ps, "limit", 0, ps->lreduce);
+#ifdef STATS
+ relem (ps, "learning", 1, PERCENT (ps->llused, ps->lladded));
+#endif
+ relem (ps, "agility", 1, dynamic_flips_per_assignment_per_mille (ps) / 10.0);
+ // relem (ps, "original", 0, ps->noclauses);
+ relem (ps, "MB", 1, mb (ps));
+ // relem (ps, "lladded", 0, ps->lladded);
+ // relem (ps, "llused", 0, ps->llused);
+
+ relem (ps, 0, 0, 0);
+#endif
+
+ ps->reports++;
+ }
+
+ /* Adapt this to the number of rows in your terminal.
+ */
+ #define ROWS 25
+
+ if (INT_MOD(ps->reports, (ROWS - 3)) == (ROWS - 4))
+ rheader (ps);
+
+ fflush (ps->out);
+}
+
+static int
+bcp_queue_is_empty (PS * ps)
+{
+ if (ps->ttail != ps->thead)
+ return 0;
+
+ if (ps->ttail2 != ps->thead)
+ return 0;
+
+#ifndef NADC
+ if (ps->ttailado != ps->thead)
+ return 0;
+#endif
+
+ return 1;
+}
+
+static int
+satisfied (PS * ps)
+{
+ assert (!ps->mtcls);
+ assert (!ps->failed_assumption);
+ if (ps->alstail < ps->alshead)
+ return 0;
+ assert (!ps->conflict);
+ assert (bcp_queue_is_empty (ps));
+ return ps->thead == ps->trail + ps->max_var; /* all assigned */
+}
+
+static void
+vrescore (PS * ps)
+{
+ Rnk *p, *eor = ps->rnks + ps->max_var;
+ for (p = ps->rnks + 1; p <= eor; p++)
+ if (p->score != INFFLT)
+ p->score = mulflt (p->score, ps->ilvinc);
+ ps->vinc = mulflt (ps->vinc, ps->ilvinc);;
+#ifdef VISCORES
+ ps->nvinc = mulflt (ps->nvinc, ps->lscore);;
+#endif
+}
+
+static void
+inc_score (PS * ps, Var * v)
+{
+ Flt score;
+ Rnk *r;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return;
+#endif
+
+ if (!v->level)
+ return;
+
+ if (v->internal)
+ return;
+
+ r = VAR2RNK (v);
+ score = r->score;
+
+ assert (score != INFFLT);
+
+ score = addflt (score, ps->vinc);
+ assert (score < INFFLT);
+ r->score = score;
+ if (r->pos > 0)
+ hup (ps, r);
+
+ if (score > ps->lscore)
+ vrescore (ps);
+}
+
+static void
+inc_activity (PS * ps, Cls * c)
+{
+ Act *p;
+
+ if (!c->learned)
+ return;
+
+ if (c->size <= 2)
+ return;
+
+ p = CLS2ACT (c);
+ *p = addflt (*p, ps->cinc);
+}
+
+static INLINE unsigned
+hashlevel (unsigned l)
+{
+ return 1u << (l & 31);
+}
+
+static INLINE void
+push (PS * ps, Var * v)
+{
+ if (ps->dhead == ps->eod)
+ ENLARGE (ps->dfs, ps->dhead, ps->eod);
+
+ *ps->dhead++ = v;
+}
+
+static INLINE Var *
+pop (PS * ps)
+{
+ assert (ps->dfs < ps->dhead);
+ return *--ps->dhead;
+}
+
+static void
+analyze (PS * ps)
+{
+ unsigned open, minlevel, siglevels, l, old, i, orig;
+ Lit *this, *other, **p, **q, **eol;
+ Var *v, *u, **m, *start, *uip;
+ Cls *c;
+
+ assert (ps->conflict);
+
+ assert (ps->ahead == ps->added);
+ assert (ps->mhead == ps->marked);
+ assert (ps->rhead == ps->resolved);
+
+ /* First, search for First UIP variable and mark all resolved variables.
+ * At the same time determine the minimum decision level involved.
+ * Increase activities of resolved variables.
+ */
+ q = ps->thead;
+ open = 0;
+ minlevel = ps->LEVEL;
+ siglevels = 0;
+ uip = 0;
+
+ c = ps->conflict;
+
+ for (;;)
+ {
+ add_antecedent (ps, c);
+ inc_activity (ps, c);
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ other = *p;
+
+ if (other->val == TRUE)
+ continue;
+
+ assert (other->val == FALSE);
+
+ u = LIT2VAR (other);
+ if (u->mark)
+ continue;
+
+ u->mark = 1;
+ inc_score (ps, u);
+ use_var (ps, u);
+
+ if (u->level == ps->LEVEL)
+ {
+ open++;
+ }
+ else
+ {
+ push_var_as_marked (ps, u);
+
+ if (u->level)
+ {
+ /* The statistics counter 'nonminimizedllits' sums up the
+ * number of literals that would be added if only the
+ * 'first UIP' scheme for learned clauses would be used
+ * and no clause minimization.
+ */
+ ps->nonminimizedllits++;
+
+ if (u->level < minlevel)
+ minlevel = u->level;
+
+ siglevels |= hashlevel (u->level);
+ }
+ else
+ {
+ assert (!u->level);
+ assert (u->reason);
+ }
+ }
+ }
+
+ do
+ {
+ if (q == ps->trail)
+ {
+ uip = 0;
+ goto DONE_FIRST_UIP;
+ }
+
+ this = *--q;
+ uip = LIT2VAR (this);
+ }
+ while (!uip->mark);
+
+ uip->mark = 0;
+
+ c = var2reason (ps, uip);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ open--;
+ if ((!open && ps->LEVEL) || !c)
+ break;
+
+ assert (c);
+ }
+
+DONE_FIRST_UIP:
+
+ if (uip)
+ {
+ assert (ps->LEVEL);
+ this = VAR2LIT (uip);
+ this += (this->val == TRUE);
+ ps->nonminimizedllits++;
+ ps->minimizedllits++;
+ add_lit (ps, this);
+#ifdef STATS
+ if (uip->reason)
+ ps->uips++;
+#endif
+ }
+ else
+ assert (!ps->LEVEL);
+
+ /* Second, try to mark more intermediate variables, with the goal to
+ * minimize the conflict clause. This is a DFS from already marked
+ * variables backward through the implication graph. It tries to reach
+ * other marked variables. If the search reaches an unmarked decision
+ * variable or a variable assigned below the minimum level of variables in
+ * the first uip learned clause or a level on which no variable has been
+ * marked, then the variable from which the DFS is started is not
+ * redundant. Otherwise the start variable is redundant and will
+ * eventually be removed from the learned clause in step 4. We initially
+ * implemented BFS, but then profiling revelead that this step is a bottle
+ * neck for certain incremental applications. After switching to DFS this
+ * hot spot went away.
+ */
+ orig = ps->mhead - ps->marked;
+ for (i = 0; i < orig; i++)
+ {
+ start = ps->marked[i];
+
+ assert (start->mark);
+ assert (start != uip);
+ assert (start->level < ps->LEVEL);
+
+ if (!start->reason)
+ continue;
+
+ old = ps->mhead - ps->marked;
+ assert (ps->dhead == ps->dfs);
+ push (ps, start);
+
+ while (ps->dhead > ps->dfs)
+ {
+ u = pop (ps);
+ assert (u->mark);
+
+ c = var2reason (ps, u);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ if (!c ||
+ ((l = u->level) &&
+ (l < minlevel || ((hashlevel (l) & ~siglevels)))))
+ {
+ while (ps->mhead > ps->marked + old) /* reset all marked */
+ (*--ps->mhead)->mark = 0;
+
+ ps->dhead = ps->dfs; /* and DFS stack */
+ break;
+ }
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ v = LIT2VAR (*p);
+ if (v->mark)
+ continue;
+
+ mark_var (ps, v);
+ push (ps, v);
+ }
+ }
+ }
+
+ for (m = ps->marked; m < ps->mhead; m++)
+ {
+ v = *m;
+
+ assert (v->mark);
+ assert (!v->resolved);
+
+ use_var (ps, v);
+
+ c = var2reason (ps, v);
+ if (!c)
+ continue;
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ other = *p;
+
+ u = LIT2VAR (other);
+ if (!u->level)
+ continue;
+
+ if (!u->mark) /* 'MARKTEST' */
+ break;
+ }
+
+ if (p != eol)
+ continue;
+
+ add_antecedent (ps, c);
+ v->resolved = 1;
+ }
+
+ for (m = ps->marked; m < ps->mhead; m++)
+ {
+ v = *m;
+
+ assert (v->mark);
+ v->mark = 0;
+
+ if (v->resolved)
+ {
+ v->resolved = 0;
+ continue;
+ }
+
+ this = VAR2LIT (v);
+ if (this->val == TRUE)
+ this++; /* actually NOTLIT */
+
+ add_lit (ps, this);
+ ps->minimizedllits++;
+ }
+
+ assert (ps->ahead <= ps->eoa);
+ assert (ps->rhead <= ps->eor);
+
+ ps->mhead = ps->marked;
+}
+
+static void
+fanalyze (PS * ps)
+{
+ Lit ** eol, ** p, * lit;
+ Cls * c, * reason;
+ Var * v, * u;
+ int next;
+
+#ifndef RCODE
+ double start = picosat_time_stamp ();
+#endif
+
+ assert (ps->failed_assumption);
+ assert (ps->failed_assumption->val == FALSE);
+
+ v = LIT2VAR (ps->failed_assumption);
+ reason = var2reason (ps, v);
+ if (!reason) return;
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p != eol; p++)
+ {
+ lit = *p;
+ u = LIT2VAR (lit);
+ if (u == v) continue;
+ if (u->reason) break;
+ }
+ if (p == eol) return;
+
+ assert (ps->ahead == ps->added);
+ assert (ps->mhead == ps->marked);
+ assert (ps->rhead == ps->resolved);
+
+ next = 0;
+ mark_var (ps, v);
+ add_lit (ps, NOTLIT (ps->failed_assumption));
+
+ do
+ {
+ v = ps->marked[next++];
+ use_var (ps, v);
+ if (v->reason)
+ {
+ reason = var2reason (ps, v);
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+ add_antecedent (ps, reason);
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p != eol; p++)
+ {
+ lit = *p;
+ u = LIT2VAR (lit);
+ if (u == v) continue;
+ if (u->mark) continue;
+ mark_var (ps, u);
+ }
+ }
+ else
+ {
+ lit = VAR2LIT (v);
+ if (lit->val == TRUE) lit = NOTLIT (lit);
+ add_lit (ps, lit);
+ }
+ }
+ while (ps->marked + next < ps->mhead);
+
+ c = add_simplified_clause (ps, 1);
+ v = LIT2VAR (ps->failed_assumption);
+ reason = v->reason;
+#ifdef NO_BINARY_CLAUSES
+ if (!ISLITREASON (reason))
+#endif
+ {
+ assert (reason->locked);
+ reason->locked = 0;
+ if (reason->learned && reason->size > 2)
+ {
+ assert (ps->llocked > 0);
+ ps->llocked--;
+ }
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ {
+ c = impl2reason (ps, NOTLIT (ps->failed_assumption));
+ }
+ else
+#endif
+ {
+ assert (c->learned);
+ assert (!c->locked);
+ c->locked = 1;
+ if (c->size > 2)
+ {
+ ps->llocked++;
+ assert (ps->llocked > 0);
+ }
+ }
+
+ v->reason = c;
+
+ while (ps->mhead > ps->marked)
+ (*--ps->mhead)->mark = 0;
+
+ if (ps->verbosity)
+ fprintf (ps->out, "%sfanalyze took %.1f seconds\n",
+ ps->prefix, picosat_time_stamp () - start);
+}
+
+/* Propagate assignment of 'this' to 'FALSE' by visiting all binary clauses in
+ * which 'this' occurs.
+ */
+inline static void
+prop2 (PS * ps, Lit * this)
+{
+#ifdef NO_BINARY_CLAUSES
+ Lit ** l, ** start;
+ Ltk * lstk;
+#else
+ Cls * c, ** p;
+ Cls * next;
+#endif
+ Lit * other;
+ Val tmp;
+
+ assert (this->val == FALSE);
+
+#ifdef NO_BINARY_CLAUSES
+ lstk = LIT2IMPLS (this);
+ start = lstk->start;
+ l = start + lstk->count;
+ while (l != start)
+ {
+ /* The counter 'visits' is the number of clauses that are
+ * visited during propagations of assignments.
+ */
+ ps->visits++;
+#ifdef STATS
+ ps->bvisits++;
+#endif
+ other = *--l;
+ tmp = other->val;
+
+ if (tmp == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertrue2++;
+ if (LIT2VAR (other)->level < ps->LEVEL)
+ ps->othertrue2u++;
+#endif
+ continue;
+ }
+
+ if (tmp != FALSE)
+ {
+ assign_forced (ps, other, LIT2REASON (NOTLIT(this)));
+ continue;
+ }
+
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+ ps->conflict = setcimpl (ps, this, other);
+ }
+#else
+ /* Traverse all binary clauses with 'this'. Head/Tail pointers for binary
+ * clauses do not have to be modified here.
+ */
+ p = LIT2IMPLS (this);
+ for (c = *p; c; c = next)
+ {
+ ps->visits++;
+#ifdef STATS
+ ps->bvisits++;
+#endif
+ assert (!c->collect);
+#ifdef TRACE
+ assert (!c->collected);
+#endif
+ assert (c->size == 2);
+
+ other = c->lits[0];
+ if (other == this)
+ {
+ next = c->next[0];
+ other = c->lits[1];
+ }
+ else
+ next = c->next[1];
+
+ tmp = other->val;
+
+ if (tmp == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertrue2++;
+ if (LIT2VAR (other)->level < ps->LEVEL)
+ ps->othertrue2u++;
+#endif
+ continue;
+ }
+
+ if (tmp == FALSE)
+ ps->conflict = c;
+ else
+ assign_forced (ps, other, c); /* unit clause */
+ }
+#endif /* !defined(NO_BINARY_CLAUSES) */
+}
+
+#ifndef NDSC
+static int
+should_disconnect_head_tail (PS * ps, Lit * lit)
+{
+ unsigned litlevel;
+ Var * v;
+
+ assert (lit->val == TRUE);
+
+ v = LIT2VAR (lit);
+ litlevel = v->level;
+
+ if (!litlevel)
+ return 1;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return 0;
+#endif
+
+ return litlevel < ps->LEVEL;
+}
+#endif
+
+inline static void
+propl (PS * ps, Lit * this)
+{
+ Lit **l, *other, *prev, *new_lit, **eol;
+ Cls *next, **htp_ptr, **new_htp_ptr;
+ Cls *c;
+#ifdef STATS
+ unsigned size;
+#endif
+
+ htp_ptr = LIT2HTPS (this);
+ assert (this->val == FALSE);
+
+ /* Traverse all non binary clauses with 'this'. Head/Tail pointers are
+ * updated as well.
+ */
+ for (c = *htp_ptr; c; c = next)
+ {
+ ps->visits++;
+#ifdef STATS
+ size = c->size;
+ assert (size >= 3);
+ ps->traversals++; /* other is dereferenced at least */
+
+ if (size == 3)
+ ps->tvisits++;
+ else if (size >= 4)
+ {
+ ps->lvisits++;
+ ps->ltraversals++;
+ }
+#endif
+#ifdef TRACE
+ assert (!c->collected);
+#endif
+ assert (c->size > 0);
+
+ other = c->lits[0];
+ if (other != this)
+ {
+ assert (c->size != 1);
+ c->lits[0] = this;
+ c->lits[1] = other;
+ next = c->next[1];
+ c->next[1] = c->next[0];
+ c->next[0] = next;
+ }
+ else if (c->size == 1) /* With assumptions we need to
+ * traverse unit clauses as well.
+ */
+ {
+ assert (!ps->conflict);
+ ps->conflict = c;
+ break;
+ }
+ else
+ {
+ assert (other == this && c->size > 1);
+ other = c->lits[1];
+ next = c->next[0];
+ }
+ assert (other == c->lits[1]);
+ assert (this == c->lits[0]);
+ assert (next == c->next[0]);
+ assert (!c->collect);
+
+ if (other->val == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertruel++;
+#endif
+#ifndef NDSC
+ if (should_disconnect_head_tail (ps, other))
+ {
+ new_htp_ptr = LIT2DHTPS (other);
+ c->next[0] = *new_htp_ptr;
+ *new_htp_ptr = c;
+#ifdef STATS
+ ps->othertruelu++;
+#endif
+ *htp_ptr = next;
+ continue;
+ }
+#endif
+ htp_ptr = c->next;
+ continue;
+ }
+
+ l = c->lits + 1;
+ eol = (Lit**) c->lits + c->size;
+ prev = this;
+
+ while (++l != eol)
+ {
+#ifdef STATS
+ if (size >= 3)
+ {
+ ps->traversals++;
+ if (size > 3)
+ ps->ltraversals++;
+ }
+#endif
+ new_lit = *l;
+ *l = prev;
+ prev = new_lit;
+ if (new_lit->val != FALSE) break;
+ }
+
+ if (l == eol)
+ {
+ while (l > c->lits + 2)
+ {
+ new_lit = *--l;
+ *l = prev;
+ prev = new_lit;
+ }
+ assert (c->lits[0] == this);
+
+ assert (other == c->lits[1]);
+ if (other->val == FALSE) /* found conflict */
+ {
+ assert (!ps->conflict);
+ ps->conflict = c;
+ return;
+ }
+
+ assign_forced (ps, other, c); /* unit clause */
+ htp_ptr = c->next;
+ }
+ else
+ {
+ assert (new_lit->val == TRUE || new_lit->val == UNDEF);
+ c->lits[0] = new_lit;
+ // *l = this;
+ new_htp_ptr = LIT2HTPS (new_lit);
+ c->next[0] = *new_htp_ptr;
+ *new_htp_ptr = c;
+ *htp_ptr = next;
+ }
+ }
+}
+
+#ifndef NADC
+
+static unsigned primes[] = { 996293, 330643, 753947, 500873 };
+
+#define PRIMES ((sizeof primes)/sizeof *primes)
+
+static unsigned
+hash_ado (PS * ps, Lit ** ado, unsigned salt)
+{
+ unsigned i, res, tmp;
+ Lit ** p, * lit;
+
+ assert (salt < PRIMES);
+
+ i = salt;
+ res = 0;
+
+ for (p = ado; (lit = *p); p++)
+ {
+ assert (lit->val);
+
+ tmp = res >> 31;
+ res <<= 1;
+
+ if (lit->val > 0)
+ res |= 1;
+
+ assert (i < PRIMES);
+ res *= primes[i++];
+ if (i == PRIMES)
+ i = 0;
+
+ res += tmp;
+ }
+
+ return res & (ps->szadotab - 1);
+}
+
+static unsigned
+cmp_ado (Lit ** a, Lit ** b)
+{
+ Lit ** p, ** q, * l, * k;
+ int res;
+
+ for (p = a, q = b; (l = *p); p++, q++)
+ {
+ k = *q;
+ assert (k);
+ if ((res = (l->val - k->val)))
+ return res;
+ }
+
+ assert (!*q);
+
+ return 0;
+}
+
+static Lit ***
+find_ado (PS * ps, Lit ** ado)
+{
+ Lit *** res, ** other;
+ unsigned pos, delta;
+
+ pos = hash_ado (ps, ado, 0);
+ assert (pos < ps->szadotab);
+ res = ps->adotab + pos;
+
+ other = *res;
+ if (!other || !cmp_ado (other, ado))
+ return res;
+
+ delta = hash_ado (ps, ado, 1);
+ if (!(delta & 1))
+ delta++;
+
+ assert (delta & 1);
+ assert (delta < ps->szadotab);
+
+ for (;;)
+ {
+ pos += delta;
+ if (pos >= ps->szadotab)
+ pos -= ps->szadotab;
+
+ assert (pos < ps->szadotab);
+ res = ps->adotab + pos;
+ other = *res;
+ if (!other || !cmp_ado (other, ado))
+ return res;
+ }
+}
+
+static void
+enlarge_adotab (PS * ps)
+{
+ /* TODO make this generic */
+
+ ABORTIF (ps->szadotab,
+ "internal: all different objects table needs larger initial size");
+ assert (!ps->nadotab);
+ ps->szadotab = 10000;
+ NEWN (ps->adotab, ps->szadotab);
+ CLRN (ps->adotab, ps->szadotab);
+}
+
+static int
+propado (PS * ps, Var * v)
+{
+ Lit ** p, ** q, *** adotabpos, **ado, * lit;
+ Var * u;
+
+ if (ps->LEVEL && ps->adodisabled)
+ return 1;
+
+ assert (!ps->conflict);
+ assert (!ps->adoconflict);
+ assert (VAR2LIT (v)->val != UNDEF);
+ assert (!v->adotabpos);
+
+ if (!v->ado)
+ return 1;
+
+ assert (v->inado);
+
+ for (p = v->ado; (lit = *p); p++)
+ if (lit->val == UNDEF)
+ {
+ u = LIT2VAR (lit);
+ assert (!u->ado);
+ u->ado = v->ado;
+ v->ado = 0;
+
+ return 1;
+ }
+
+ if (4 * ps->nadotab >= 3 * ps->szadotab) /* at least 75% filled */
+ enlarge_adotab (ps);
+
+ adotabpos = find_ado (ps, v->ado);
+ ado = *adotabpos;
+
+ if (!ado)
+ {
+ ps->nadotab++;
+ v->adotabpos = adotabpos;
+ *adotabpos = v->ado;
+ return 1;
+ }
+
+ assert (ado != v->ado);
+
+ ps->adoconflict = new_clause (ps, 2 * llength (ado), 1);
+ q = ps->adoconflict->lits;
+
+ for (p = ado; (lit = *p); p++)
+ *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
+
+ for (p = v->ado; (lit = *p); p++)
+ *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
+
+ assert (q == ENDOFCLS (ps->adoconflict));
+ ps->conflict = ps->adoconflict;
+ ps->adoconflicts++;
+ return 0;
+}
+
+#endif
+
+static void
+bcp (PS * ps)
+{
+ int props = 0;
+ assert (!ps->conflict);
+
+ if (ps->mtcls)
+ return;
+
+ for (;;)
+ {
+ if (ps->ttail2 < ps->thead) /* prioritize implications */
+ {
+ props++;
+ prop2 (ps, NOTLIT (*ps->ttail2++));
+ }
+ else if (ps->ttail < ps->thead) /* unit clauses or clauses with length > 2 */
+ {
+ if (ps->conflict) break;
+ propl (ps, NOTLIT (*ps->ttail++));
+ if (ps->conflict) break;
+ }
+#ifndef NADC
+ else if (ps->ttailado < ps->thead)
+ {
+ if (ps->conflict) break;
+ propado (ps, LIT2VAR (*ps->ttailado++));
+ if (ps->conflict) break;
+ }
+#endif
+ else
+ break; /* all assignments propagated, so break */
+ }
+
+ ps->propagations += props;
+}
+
+static unsigned
+drive (PS * ps)
+{
+ unsigned res, vlevel;
+ Lit **p;
+ Var *v;
+
+ res = 0;
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ v = LIT2VAR (*p);
+ vlevel = v->level;
+ assert (vlevel <= ps->LEVEL);
+ if (vlevel < ps->LEVEL && vlevel > res)
+ res = vlevel;
+ }
+
+ return res;
+}
+
+#ifdef VISCORES
+
+static void
+viscores (PS * ps)
+{
+ Rnk *p, *eor = ps->rnks + ps->max_var;
+ char name[100], cmd[200];
+ FILE * data;
+ Flt s;
+ int i;
+
+ for (p = ps->rnks + 1; p <= ps->eor; p++)
+ {
+ s = p->score;
+ if (s == INFFLT)
+ continue;
+ s = mulflt (s, ps->nvinc);
+ assert (flt2double (s) <= 1.0);
+ }
+
+ sprintf (name, "/tmp/picosat-viscores/data/%08u", ps->conflicts);
+ sprintf (cmd, "sort -n|nl>%s", name);
+
+ data = popen (cmd, "w");
+ for (p = ps->rnks + 1; p <= ps->eor; p++)
+ {
+ s = p->score;
+ if (s == INFFLT)
+ continue;
+ s = mulflt (s, ps->nvinc);
+ fprintf (data, "%lf %d\n", 100.0 * flt2double (s), (int)(p - ps->rnks));
+ }
+ fflush (data);
+ pclose (data);
+
+ for (i = 0; i < 8; i++)
+ {
+ sprintf (cmd, "awk '$3%%8==%d' %s>%s.%d", i, name, name, i);
+ system (cmd);
+ }
+
+ fprintf (ps->fviscores, "set title \"%u\"\n", ps->conflicts);
+ fprintf (ps->fviscores, "plot [0:%u] 0, 100 * (1 - 1/1.1), 100", ps->max_var);
+
+ for (i = 0; i < 8; i++)
+ fprintf (ps->fviscores,
+ ", \"%s.%d\" using 1:2:3 with labels tc lt %d",
+ name, i, i + 1);
+
+ fputc ('\n', ps->fviscores);
+ fflush (ps->fviscores);
+#ifndef WRITEGIF
+ usleep (50000); /* refresh rate of 20 Hz */
+#endif
+}
+
+#endif
+
+static void
+crescore (PS * ps)
+{
+ Cls **p, *c;
+ Act *a;
+ Flt factor;
+ int l = log2flt (ps->cinc);
+ assert (l > 0);
+ factor = base2flt (1, -l);
+
+ for (p = ps->lclauses; p != ps->lhead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+ assert (c->learned);
+
+ if (c->size <= 2)
+ continue;
+
+ a = CLS2ACT (c);
+ *a = mulflt (*a, factor);
+ }
+
+ ps->cinc = mulflt (ps->cinc, factor);
+}
+
+static void
+inc_vinc (PS * ps)
+{
+#ifdef VISCORES
+ ps->nvinc = mulflt (ps->nvinc, ps->fvinc);
+#endif
+ ps->vinc = mulflt (ps->vinc, ps->ifvinc);
+}
+
+inline static void
+inc_max_var (PS * ps)
+{
+ Lit *lit;
+ Rnk *r;
+ Var *v;
+
+ assert (ps->max_var < ps->size_vars);
+
+ if (ps->max_var + 1 == ps->size_vars)
+ enlarge (ps, ps->size_vars + 2*(ps->size_vars + 3) / 4); /* +25% */
+
+ ps->max_var++; /* new index of variable */
+ assert (ps->max_var); /* no unsigned overflow */
+
+ assert (ps->max_var < ps->size_vars);
+
+ lit = ps->lits + 2 * ps->max_var;
+ lit[0].val = lit[1].val = UNDEF;
+
+ memset (ps->htps + 2 * ps->max_var, 0, 2 * sizeof *ps->htps);
+#ifndef NDSC
+ memset (ps->dhtps + 2 * ps->max_var, 0, 2 * sizeof *ps->dhtps);
+#endif
+ memset (ps->impls + 2 * ps->max_var, 0, 2 * sizeof *ps->impls);
+ memset (ps->jwh + 2 * ps->max_var, 0, 2 * sizeof *ps->jwh);
+
+ v = ps->vars + ps->max_var; /* initialize variable components */
+ CLR (v);
+
+ r = ps->rnks + ps->max_var; /* initialize rank */
+ CLR (r);
+
+ hpush (ps, r);
+}
+
+static void
+force (PS * ps, Cls * c)
+{
+ Lit ** p, ** eol, * lit, * forced;
+ Cls * reason;
+
+ forced = 0;
+ reason = c;
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == UNDEF)
+ {
+ assert (!forced);
+ forced = lit;
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ reason = LIT2REASON (NOTLIT (p[p == c->lits ? 1 : -1]));
+#endif
+ }
+ else
+ assert (lit->val == FALSE);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ if (!forced)
+ return;
+
+ assign_forced (ps, forced, reason);
+}
+
+static INLINE void
+inc_lreduce (PS * ps)
+{
+#ifdef STATS
+ ps->inclreduces++;
+#endif
+ ps->lreduce *= FREDUCE;
+ ps->lreduce /= 100;
+ report (ps, 1, '+');
+}
+
+static void
+backtrack (PS * ps)
+{
+ unsigned new_level;
+ Cls * c;
+
+ ps->conflicts++;
+ LOG ( fprintf (ps->out, "%sconflict ", ps->prefix); dumpclsnl (ps, ps->conflict));
+
+ analyze (ps);
+ new_level = drive (ps);
+ // TODO: why not? assert (new_level != 1 || (ps->ahead - ps->added) == 2);
+ c = add_simplified_clause (ps, 1);
+ undo (ps, new_level);
+ force (ps, c);
+
+ if (
+#ifndef NFL
+ !ps->simplifying &&
+#endif
+ !--ps->lreduceadjustcnt)
+ {
+ /* With FREDUCE==110 and FREDADJ=121 we stir 'lreduce' to be
+ * proportional to 'sqrt(conflicts)'. In earlier version we actually
+ * used 'FREDADJ=150', which results in 'lreduce' to approximate
+ * 'conflicts^(log(1.1)/log(1.5))' which is close to the fourth root
+ * of 'conflicts', since log(1.1)/log(1.5)=0.235 (as observed by
+ * Donald Knuth). The square root is the same we get by a Glucose
+ * style increase, which simply adds a constant at every reduction.
+ * This would be way simpler to implement but for now we keep the more
+ * complicated code using the adjust increments and counters.
+ */
+ ps->lreduceadjustinc *= FREDADJ; ps->lreduceadjustinc /= 100; ps->lreduceadjustcnt
+ = ps->lreduceadjustinc;
+ inc_lreduce (ps);
+ }
+
+ if (ps->verbosity >= 4 && !(INT_MOD(ps->conflicts, 1000)))
+ report (ps, 4, 'C');
+}
+
+static void
+inc_cinc (PS * ps)
+{
+ ps->cinc = mulflt (ps->cinc, ps->fcinc);
+ if (ps->lcinc < ps->cinc)
+ crescore (ps);
+}
+
+static void
+incincs (PS * ps)
+{
+ inc_vinc (ps);
+ inc_cinc (ps);
+#ifdef VISCORES
+ viscores (ps);
+#endif
+}
+
+static void
+disconnect_clause (PS * ps, Cls * c)
+{
+ assert (c->connected);
+
+ if (c->size > 2)
+ {
+ if (c->learned)
+ {
+ assert (ps->nlclauses > 0);
+ ps->nlclauses--;
+
+ assert (ps->llits >= c->size);
+ ps->llits -= c->size;
+ }
+ else
+ {
+ assert (ps->noclauses > 0);
+ ps->noclauses--;
+
+ assert (ps->olits >= c->size);
+ ps->olits -= c->size;
+ }
+ }
+
+#ifndef NDEBUG
+ c->connected = 0;
+#endif
+}
+
+static int
+clause_is_toplevel_satisfied (PS * ps, Cls * c)
+{
+ Lit *lit, **p, **eol = end_of_lits (c);
+ Var *v;
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == TRUE)
+ {
+ v = LIT2VAR (lit);
+ if (!v->level)
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int
+collect_clause (PS * ps, Cls * c)
+{
+ assert (c->collect);
+ c->collect = 0;
+
+#ifdef TRACE
+ assert (!c->collected);
+ c->collected = 1;
+#endif
+ disconnect_clause (ps, c);
+
+#ifdef TRACE
+ if (ps->trace && (!c->learned || c->used))
+ return 0;
+#endif
+ delete_clause (ps, c);
+
+ return 1;
+}
+
+static size_t
+collect_clauses (PS * ps)
+{
+ Cls *c, **p, **q, * next;
+ Lit * lit, * eol;
+ size_t res;
+ int i;
+
+ res = ps->current_bytes;
+
+ eol = ps->lits + 2 * ps->max_var + 1;
+ for (lit = ps->lits + 2; lit <= eol; lit++)
+ {
+ for (i = 0; i <= 1; i++)
+ {
+ if (i)
+ {
+#ifdef NO_BINARY_CLAUSES
+ Ltk * lstk = LIT2IMPLS (lit);
+ Lit ** r, ** s;
+ r = lstk->start;
+ if (lit->val != TRUE || LIT2VAR (lit)->level)
+ for (s = r; s < lstk->start + lstk->count; s++)
+ {
+ Lit * other = *s;
+ Var *v = LIT2VAR (other);
+ if (v->level ||
+ other->val != TRUE)
+ *r++ = other;
+ }
+ lstk->count = r - lstk->start;
+ continue;
+#else
+ p = LIT2IMPLS (lit);
+#endif
+ }
+ else
+ p = LIT2HTPS (lit);
+
+ for (c = *p; c; c = next)
+ {
+ q = c->next;
+ if (c->lits[0] != lit)
+ q++;
+
+ next = *q;
+ if (c->collect)
+ *p = next;
+ else
+ p = q;
+ }
+ }
+ }
+
+#ifndef NDSC
+ for (lit = ps->lits + 2; lit <= eol; lit++)
+ {
+ p = LIT2DHTPS (lit);
+ while ((c = *p))
+ {
+ Lit * other = c->lits[0];
+ if (other == lit)
+ {
+ q = c->next + 1;
+ }
+ else
+ {
+ assert (c->lits[1] == lit);
+ q = c->next;
+ }
+
+ if (c->collect)
+ *p = *q;
+ else
+ p = q;
+ }
+ }
+#endif
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (!c->collect)
+ continue;
+
+ if (collect_clause (ps, c))
+ *p = 0;
+ }
+
+#ifdef TRACE
+ if (!ps->trace)
+#endif
+ {
+ q = ps->oclauses;
+ for (p = q; p < ps->ohead; p++)
+ if ((c = *p))
+ *q++ = c;
+ ps->ohead = q;
+
+ q = ps->lclauses;
+ for (p = q; p < ps->lhead; p++)
+ if ((c = *p))
+ *q++ = c;
+ ps->lhead = q;
+ }
+
+ assert (ps->current_bytes <= res);
+ res -= ps->current_bytes;
+ ps->recycled += res;
+
+ LOG ( fprintf (ps->out, "%scollected %ld bytes\n", ps->prefix, (long)res));
+
+ return res;
+}
+
+static INLINE int
+need_to_reduce (PS * ps)
+{
+ return ps->nlclauses >= reduce_limit_on_lclauses (ps);
+}
+
+#ifdef NLUBY
+
+static void
+inc_drestart (PS * ps)
+{
+ ps->drestart *= FRESTART;
+ ps->drestart /= 100;
+
+ if (ps->drestart >= MAXRESTART)
+ ps->drestart = MAXRESTART;
+}
+
+static void
+inc_ddrestart (PS * ps)
+{
+ ps->ddrestart *= FRESTART;
+ ps->ddrestart /= 100;
+
+ if (ps->ddrestart >= MAXRESTART)
+ ps->ddrestart = MAXRESTART;
+}
+
+#else
+
+static unsigned
+luby (unsigned i)
+{
+ unsigned k;
+ for (k = 1; k < 32; k++)
+ if (i == (1u << k) - 1)
+ return 1u << (k - 1);
+
+ for (k = 1;; k++)
+ if ((1u << (k - 1)) <= i && i < (1u << k) - 1)
+ return luby (i - (1u << (k-1)) + 1);
+}
+
+#endif
+
+#ifndef NLUBY
+static void
+inc_lrestart (PS * ps, int skip)
+{
+ unsigned delta;
+
+ delta = 100 * luby (++ps->lubycnt);
+ ps->lrestart = ps->conflicts + delta;
+
+ if (ps->waslubymaxdelta)
+ report (ps, 1, skip ? 'N' : 'R');
+ else
+ report (ps, 2, skip ? 'n' : 'r');
+
+ if (delta > ps->lubymaxdelta)
+ {
+ ps->lubymaxdelta = delta;
+ ps->waslubymaxdelta = 1;
+ }
+ else
+ ps->waslubymaxdelta = 0;
+}
+#endif
+
+static void
+init_restart (PS * ps)
+{
+#ifdef NLUBY
+ /* TODO: why is it better in incremental usage to have smaller initial
+ * outer restart interval?
+ */
+ ps->ddrestart = ps->calls > 1 ? MINRESTART : 1000;
+ ps->drestart = MINRESTART;
+ ps->lrestart = ps->conflicts + ps->drestart;
+#else
+ ps->lubycnt = 0;
+ ps->lubymaxdelta = 0;
+ ps->waslubymaxdelta = 0;
+ inc_lrestart (ps, 0);
+#endif
+}
+
+static void
+restart (PS * ps)
+{
+ int skip;
+#ifdef NLUBY
+ char kind;
+ int outer;
+
+ inc_drestart (ps);
+ outer = (ps->drestart >= ps->ddrestart);
+
+ if (outer)
+ skip = very_high_agility (ps);
+ else
+ skip = high_agility (ps);
+#else
+ skip = medium_agility (ps);
+#endif
+
+#ifdef STATS
+ if (skip)
+ ps->skippedrestarts++;
+#endif
+
+ assert (ps->conflicts >= ps->lrestart);
+
+ if (!skip)
+ {
+ ps->restarts++;
+ assert (ps->LEVEL > 1);
+ LOG ( fprintf (ps->out, "%srestart %u\n", ps->prefix, ps->restarts));
+ undo (ps, 0);
+ }
+
+#ifdef NLUBY
+ if (outer)
+ {
+ kind = skip ? 'N' : 'R';
+ inc_ddrestart (ps);
+ ps->drestart = MINRESTART;
+ }
+ else if (skip)
+ {
+ kind = 'n';
+ }
+ else
+ {
+ kind = 'r';
+ }
+
+ assert (ps->drestart <= MAXRESTART);
+ ps->lrestart = ps->conflicts + ps->drestart;
+ assert (ps->lrestart > ps->conflicts);
+
+ report (outer ? 1 : 2, kind);
+#else
+ inc_lrestart (ps, skip);
+#endif
+}
+
+inline static void
+assign_decision (PS * ps, Lit * lit)
+{
+ assert (!ps->conflict);
+
+ ps->LEVEL++;
+
+ LOG ( fprintf (ps->out, "%snew level %u\n", ps->prefix, ps->LEVEL));
+ LOG ( fprintf (ps->out,
+ "%sassign %d at level %d <= DECISION\n",
+ ps->prefix, LIT2INT (lit), ps->LEVEL));
+
+ assign (ps, lit, 0);
+}
+
+#ifndef NFL
+
+static INLINE int
+lit_has_binary_clauses (PS * ps, Lit * lit)
+{
+#ifdef NO_BINARY_CLAUSES
+ Ltk* lstk = LIT2IMPLS (lit);
+ return lstk->count != 0;
+#else
+ return *LIT2IMPLS (lit) != 0;
+#endif
+}
+
+static void
+flbcp (PS * ps)
+{
+#ifdef STATS
+ unsigned long long propagaions_before_bcp = ps->propagations;
+#endif
+ bcp (ps);
+#ifdef STATS
+ ps->flprops += ps->propagations - propagaions_before_bcp;
+#endif
+}
+
+inline static INLINE int
+cmp_inverse_rnk (PS * ps, Rnk * a, Rnk * b)
+{
+ (void) ps;
+ return -cmp_rnk (a, b);
+}
+
+inline static Flt
+rnk2jwh (PS * ps, Rnk * r)
+{
+ Flt res, sum, pjwh, njwh;
+ Lit * plit, * nlit;
+
+ plit = RNK2LIT (r);
+ nlit = plit + 1;
+
+ pjwh = *LIT2JWH (plit);
+ njwh = *LIT2JWH (nlit);
+
+ res = mulflt (pjwh, njwh);
+
+ sum = addflt (pjwh, njwh);
+ sum = mulflt (sum, base2flt (1, -10));
+ res = addflt (res, sum);
+
+ return res;
+}
+
+static int
+cmp_inverse_jwh_rnk (PS * ps, Rnk * r, Rnk * s)
+{
+ Flt a = rnk2jwh (ps, r);
+ Flt b = rnk2jwh (ps, s);
+ int res = cmpflt (a, b);
+
+ if (res)
+ return -res;
+
+ return cmp_inverse_rnk (ps, r, s);
+}
+
+static void
+faillits (PS * ps)
+{
+ unsigned i, j, old_trail_count, common, saved_count;
+ unsigned new_saved_size, oldladded = ps->ladded;
+ unsigned long long limit, delta;
+ Lit * lit, * other, * pivot;
+ Rnk * r, ** p, ** q;
+ int new_trail_count;
+ double started;
+
+ if (ps->plain)
+ return;
+
+ if (ps->heap + 1 >= ps->hhead)
+ return;
+
+ if (ps->propagations < ps->fllimit)
+ return;
+
+ sflush (ps);
+ started = ps->seconds;
+
+ ps->flcalls++;
+#ifdef STATSA
+ ps->flrounds++;
+#endif
+ delta = ps->propagations/10;
+ if (delta >= 100*1000*1000) delta = 100*1000*1000;
+ else if (delta <= 100*1000) delta = 100*1000;
+
+ limit = ps->propagations + delta;
+ ps->fllimit = ps->propagations;
+
+ assert (!ps->LEVEL);
+ assert (ps->simplifying);
+
+ if (ps->flcalls <= 1)
+ SORT (Rnk *, cmp_inverse_jwh_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+ else
+ SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+
+ i = 1; /* NOTE: heap starts at position '1' */
+
+ while (ps->propagations < limit)
+ {
+ if (ps->heap + i == ps->hhead)
+ {
+ if (ps->ladded == oldladded)
+ break;
+
+ i = 1;
+#ifdef STATS
+ ps->flrounds++;
+#endif
+ oldladded = ps->ladded;
+ }
+
+ assert (ps->heap + i < ps->hhead);
+
+ r = ps->heap[i++];
+ lit = RNK2LIT (r);
+
+ if (lit->val)
+ continue;
+
+ if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
+ {
+#ifdef STATS
+ ps->flskipped++;
+#endif
+ continue;
+ }
+
+#ifdef STATS
+ ps->fltried++;
+#endif
+ LOG ( fprintf (ps->out, "%strying %d as failed literal\n",
+ ps->prefix, LIT2INT (lit)));
+
+ assign_decision (ps, lit);
+ old_trail_count = ps->thead - ps->trail;
+ flbcp (ps);
+
+ if (ps->conflict)
+ {
+EXPLICITLY_FAILED_LITERAL:
+ LOG ( fprintf (ps->out, "%sfound explicitly failed literal %d\n",
+ ps->prefix, LIT2INT (lit)));
+
+ ps->failedlits++;
+ ps->efailedlits++;
+
+ backtrack (ps);
+ flbcp (ps);
+
+ if (!ps->conflict)
+ continue;
+
+CONTRADICTION:
+ assert (!ps->LEVEL);
+ backtrack (ps);
+ assert (ps->mtcls);
+
+ goto RETURN;
+ }
+
+ if (ps->propagations >= limit)
+ {
+ undo (ps, 0);
+ break;
+ }
+
+ lit = NOTLIT (lit);
+
+ if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
+ {
+#ifdef STATS
+ ps->flskipped++;
+#endif
+ undo (ps, 0);
+ continue;
+ }
+
+#ifdef STATS
+ ps->fltried++;
+#endif
+ LOG ( fprintf (ps->out, "%strying %d as failed literals\n",
+ ps->prefix, LIT2INT (lit)));
+
+ new_trail_count = ps->thead - ps->trail;
+ saved_count = new_trail_count - old_trail_count;
+
+ if (saved_count > ps->saved_size)
+ {
+ new_saved_size = ps->saved_size ? 2 * ps->saved_size : 1;
+ while (saved_count > new_saved_size)
+ new_saved_size *= 2;
+
+ RESIZEN (ps->saved, ps->saved_size, new_saved_size);
+ ps->saved_size = new_saved_size;
+ }
+
+ for (j = 0; j < saved_count; j++)
+ ps->saved[j] = ps->trail[old_trail_count + j];
+
+ undo (ps, 0);
+
+ assign_decision (ps, lit);
+ flbcp (ps);
+
+ if (ps->conflict)
+ goto EXPLICITLY_FAILED_LITERAL;
+
+ pivot = (ps->thead - ps->trail <= new_trail_count) ? lit : NOTLIT (lit);
+
+ common = 0;
+ for (j = 0; j < saved_count; j++)
+ if ((other = ps->saved[j])->val == TRUE)
+ ps->saved[common++] = other;
+
+ undo (ps, 0);
+
+ LOG (if (common)
+ fprintf (ps->out,
+ "%sfound %d literals implied by %d and %d\n",
+ ps->prefix, common,
+ LIT2INT (NOTLIT (lit)), LIT2INT (lit)));
+
+#if 1 // set to zero to disable 'lifting'
+ for (j = 0;
+ j < common
+ /* TODO: For some Velev benchmarks, extracting the common implicit
+ * failed literals took quite some time. This needs to be fixed by
+ * a dedicated analyzer. Up to then we bound the number of
+ * propagations in this loop as well.
+ */
+ && ps->propagations < limit + delta
+ ; j++)
+ {
+ other = ps->saved[j];
+
+ if (other->val == TRUE)
+ continue;
+
+ assert (!other->val);
+
+ LOG ( fprintf (ps->out,
+ "%sforcing %d as forced implicitly failed literal\n",
+ ps->prefix, LIT2INT (other)));
+
+ assert (pivot != NOTLIT (other));
+ assert (pivot != other);
+
+ assign_decision (ps, NOTLIT (other));
+ flbcp (ps);
+
+ assert (ps->LEVEL == 1);
+
+ if (ps->conflict)
+ {
+ backtrack (ps);
+ assert (!ps->LEVEL);
+ }
+ else
+ {
+ assign_decision (ps, pivot);
+ flbcp (ps);
+
+ backtrack (ps);
+
+ if (ps->LEVEL)
+ {
+ assert (ps->LEVEL == 1);
+
+ flbcp (ps);
+
+ if (ps->conflict)
+ {
+ backtrack (ps);
+ assert (!ps->LEVEL);
+ }
+ else
+ {
+ assign_decision (ps, NOTLIT (pivot));
+ flbcp (ps);
+ backtrack (ps);
+
+ if (ps->LEVEL)
+ {
+ assert (ps->LEVEL == 1);
+ flbcp (ps);
+
+ if (!ps->conflict)
+ {
+#ifdef STATS
+ ps->floopsed++;
+#endif
+ undo (ps, 0);
+ continue;
+ }
+
+ backtrack (ps);
+ }
+
+ assert (!ps->LEVEL);
+ }
+
+ assert (!ps->LEVEL);
+ }
+ }
+ assert (!ps->LEVEL);
+ flbcp (ps);
+
+ ps->failedlits++;
+ ps->ifailedlits++;
+
+ if (ps->conflict)
+ goto CONTRADICTION;
+ }
+#endif
+ }
+
+ ps->fllimit += 9 * (ps->propagations - ps->fllimit); /* 10% for failed literals */
+
+RETURN:
+
+ /* First flush top level assigned literals. Those are prohibited from
+ * being pushed up the heap during 'faillits' since 'simplifying' is set.
+ */
+ assert (ps->heap < ps->hhead);
+ for (p = q = ps->heap + 1; p < ps->hhead; p++)
+ {
+ r = *p;
+ lit = RNK2LIT (r);
+ if (lit->val)
+ r->pos = 0;
+ else
+ *q++ = r;
+ }
+
+ /* Then resort with respect to EVSIDS score and fix positions.
+ */
+ SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+ for (p = ps->heap + 1; p < ps->hhead; p++)
+ (*p)->pos = p - ps->heap;
+
+ sflush (ps);
+ ps->flseconds += ps->seconds - started;
+}
+
+#endif
+
+static void
+simplify (PS * ps, int forced)
+{
+ Lit * lit, * notlit, ** t;
+ unsigned collect, delta;
+#ifdef STATS
+ size_t bytes_collected;
+#endif
+ int * q, ilit;
+ Cls **p, *c;
+ Var * v;
+
+#ifndef NDEDBUG
+ (void) forced;
+#endif
+
+ assert (!ps->mtcls);
+ assert (!satisfied (ps));
+ assert (forced || ps->lsimplify <= ps->propagations);
+ assert (forced || ps->fsimplify <= ps->fixed);
+
+ if (ps->LEVEL)
+ undo (ps, 0);
+#ifndef NFL
+ ps->simplifying = 1;
+ faillits (ps);
+ ps->simplifying = 0;
+
+ if (ps->mtcls)
+ return;
+#endif
+
+ if (ps->cils != ps->cilshead)
+ {
+ assert (ps->ttail == ps->thead);
+ assert (ps->ttail2 == ps->thead);
+ ps->ttail = ps->trail;
+ for (t = ps->trail; t < ps->thead; t++)
+ {
+ lit = *t;
+ v = LIT2VAR (lit);
+ if (v->internal)
+ {
+ assert (LIT2INT (lit) < 0);
+ assert (lit->val == TRUE);
+ unassign (ps, lit);
+ }
+ else
+ *ps->ttail++ = lit;
+ }
+ ps->ttail2 = ps->thead = ps->ttail;
+
+ for (q = ps->cils; q != ps->cilshead; q++)
+ {
+ ilit = *q;
+ assert (0 < ilit && ilit <= (int) ps->max_var);
+ v = ps->vars + ilit;
+ assert (v->internal);
+ v->level = 0;
+ v->reason = 0;
+ lit = int2lit (ps, -ilit);
+ assert (lit->val == UNDEF);
+ lit->val = TRUE;
+ notlit = NOTLIT (lit);
+ assert (notlit->val == UNDEF);
+ notlit->val = FALSE;
+ }
+ }
+
+ collect = 0;
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ if (c->locked)
+ continue;
+
+ assert (!c->collect);
+ if (clause_is_toplevel_satisfied (ps, c))
+ {
+ mark_clause_to_be_collected (c);
+ collect++;
+ }
+ }
+
+ LOG ( fprintf (ps->out, "%scollecting %d clauses\n", ps->prefix, collect));
+#ifdef STATS
+ bytes_collected =
+#endif
+ collect_clauses (ps);
+#ifdef STATS
+ ps->srecycled += bytes_collected;
+#endif
+
+ if (ps->cils != ps->cilshead)
+ {
+ for (q = ps->cils; q != ps->cilshead; q++)
+ {
+ ilit = *q;
+ assert (0 < ilit && ilit <= (int) ps->max_var);
+ assert (ps->vars[ilit].internal);
+ if (ps->rilshead == ps->eorils)
+ ENLARGE (ps->rils, ps->rilshead, ps->eorils);
+ *ps->rilshead++ = ilit;
+ lit = int2lit (ps, -ilit);
+ assert (lit->val == TRUE);
+ lit->val = UNDEF;
+ notlit = NOTLIT (lit);
+ assert (notlit->val == FALSE);
+ notlit->val = UNDEF;
+ }
+ ps->cilshead = ps->cils;
+ }
+
+ delta = 10 * (ps->olits + ps->llits) + 100000;
+ if (delta > 2000000)
+ delta = 2000000;
+ ps->lsimplify = ps->propagations + delta;
+ ps->fsimplify = ps->fixed;
+ ps->simps++;
+ report (ps, 1, 's');
+}
+
+static void
+iteration (PS * ps)
+{
+ assert (!ps->LEVEL);
+ assert (bcp_queue_is_empty (ps));
+ assert (ps->isimplify < ps->fixed);
+
+ ps->iterations++;
+ report (ps, 2, 'i');
+#ifdef NLUBY
+ ps->drestart = MINRESTART;
+ ps->lrestart = ps->conflicts + ps->drestart;
+#else
+ init_restart (ps);
+#endif
+ ps->isimplify = ps->fixed;
+}
+
+static int
+cmp_glue_activity_size (PS * ps, Cls * c, Cls * d)
+{
+ Act a, b, * p, * q;
+
+ (void) ps;
+
+ assert (c->learned);
+ assert (d->learned);
+
+ if (c->glue < d->glue) // smaller glue preferred
+ return 1;
+
+ if (c->glue > d->glue)
+ return -1;
+
+ p = CLS2ACT (c);
+ q = CLS2ACT (d);
+ a = *p;
+ b = *q;
+
+ if (a < b) // then higher activity
+ return -1;
+
+ if (b < a)
+ return 1;
+
+ if (c->size < d->size) // then smaller size
+ return 1;
+
+ if (c->size > d->size)
+ return -1;
+
+ return 0;
+}
+
+static void
+reduce (PS * ps, unsigned percentage)
+{
+ unsigned redcount, lcollect, collect, target;
+#ifdef STATS
+ size_t bytes_collected;
+#endif
+ Cls **p, *c;
+
+ assert (ps->rhead == ps->resolved);
+
+ ps->lastreduceconflicts = ps->conflicts;
+
+ assert (percentage <= 100);
+ LOG ( fprintf (ps->out,
+ "%sreducing %u%% learned clauses\n",
+ ps->prefix, percentage));
+
+ while (ps->nlclauses - ps->llocked > (unsigned)(ps->eor - ps->resolved))
+ ENLARGE (ps->resolved, ps->rhead, ps->eor);
+
+ collect = 0;
+ lcollect = 0;
+
+ for (p = ((ps->fsimplify < ps->fixed) ? SOC : ps->lclauses); p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ if (c->locked)
+ continue;
+
+ assert (!c->collect);
+ if (ps->fsimplify < ps->fixed && clause_is_toplevel_satisfied (ps, c))
+ {
+ mark_clause_to_be_collected (c);
+ collect++;
+
+ if (c->learned && c->size > 2)
+ lcollect++;
+
+ continue;
+ }
+
+ if (!c->learned)
+ continue;
+
+ if (c->size <= 2)
+ continue;
+
+ assert (ps->rhead < ps->eor);
+ *ps->rhead++ = c;
+ }
+ assert (ps->rhead <= ps->eor);
+
+ ps->fsimplify = ps->fixed;
+
+ redcount = ps->rhead - ps->resolved;
+ SORT (Cls *, cmp_glue_activity_size, ps->resolved, redcount);
+
+ assert (ps->nlclauses >= lcollect);
+ target = ps->nlclauses - lcollect + 1;
+
+ target = (percentage * target + 99) / 100;
+
+ if (target >= redcount)
+ target = redcount;
+
+ ps->rhead = ps->resolved + target;
+ while (ps->rhead > ps->resolved)
+ {
+ c = *--ps->rhead;
+ mark_clause_to_be_collected (c);
+
+ collect++;
+ if (c->learned && c->size > 2) /* just for consistency */
+ lcollect++;
+ }
+
+ if (collect)
+ {
+ ps->reductions++;
+#ifdef STATS
+ bytes_collected =
+#endif
+ collect_clauses (ps);
+#ifdef STATS
+ ps->rrecycled += bytes_collected;
+#endif
+ report (ps, 2, '-');
+ }
+
+ if (!lcollect)
+ inc_lreduce (ps); /* avoid dead lock */
+
+ assert (ps->rhead == ps->resolved);
+}
+
+static void
+init_reduce (PS * ps)
+{
+ // lreduce = loadded / 2;
+ ps->lreduce = 1000;
+
+ if (ps->lreduce < 100)
+ ps->lreduce = 100;
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%s\n%sinitial reduction limit %u clauses\n%s\n",
+ ps->prefix, ps->prefix, ps->lreduce, ps->prefix);
+}
+
+static INLINE unsigned
+rng (PS * ps)
+{
+ unsigned res = ps->srng;
+ ps->srng *= 1664525u;
+ ps->srng += 1013904223u;
+ NOLOG ( fprintf (ps->out, "%srng () = %u\n", ps->prefix, res));
+ return res;
+}
+
+static unsigned
+rrng (PS * ps, unsigned low, unsigned high)
+{
+ unsigned long long tmp;
+ unsigned res, elements;
+ assert (low <= high);
+ elements = high - low + 1;
+ tmp = rng (ps);
+ tmp *= elements;
+ tmp >>= 32;
+ tmp += low;
+ res = tmp;
+ NOLOG ( fprintf (ps->out, "%srrng (ps, %u, %u) = %u\n", ps->prefix, low, high, res));
+ assert (low <= res);
+ assert (res <= high);
+ return res;
+}
+
+static Lit *
+decide_phase (PS * ps, Lit * lit)
+{
+ Lit * not_lit = NOTLIT (lit);
+ Var *v = LIT2VAR (lit);
+
+ assert (LIT2SGN (lit) > 0);
+ if (v->usedefphase)
+ {
+ if (v->defphase)
+ {
+ /* assign to TRUE */
+ }
+ else
+ {
+ /* assign to FALSE */
+ lit = not_lit;
+ }
+ }
+ else if (!v->assigned)
+ {
+#ifdef STATS
+ ps->staticphasedecisions++;
+#endif
+ if (ps->defaultphase == POSPHASE)
+ {
+ /* assign to TRUE */
+ }
+ else if (ps->defaultphase == NEGPHASE)
+ {
+ /* assign to FALSE */
+ lit = not_lit;
+ }
+ else if (ps->defaultphase == RNDPHASE)
+ {
+ /* randomly assign default phase */
+ if (rrng (ps, 1, 2) != 2)
+ lit = not_lit;
+ }
+ else if (*LIT2JWH(lit) <= *LIT2JWH (not_lit))
+ {
+ /* assign to FALSE (Jeroslow-Wang says there are more short
+ * clauses with negative occurence of this variable, so satisfy
+ * those, to minimize BCP)
+ */
+ lit = not_lit;
+ }
+ else
+ {
+ /* assign to TRUE (... but strictly more positive occurrences) */
+ }
+ }
+ else
+ {
+ /* repeat last phase: phase saving heuristic */
+
+ if (v->phase)
+ {
+ /* assign to TRUE (last phase was TRUE as well) */
+ }
+ else
+ {
+ /* assign to FALSE (last phase was FALSE as well) */
+ lit = not_lit;
+ }
+ }
+
+ return lit;
+}
+
+static unsigned
+gcd (unsigned a, unsigned b)
+{
+ unsigned tmp;
+
+ assert (a);
+ assert (b);
+
+ if (a < b)
+ {
+ tmp = a;
+ a = b;
+ b = tmp;
+ }
+
+ while (b)
+ {
+ assert (a >= b);
+ tmp = b;
+ b = INT_MOD(a, b);
+ a = tmp;
+ }
+
+ return a;
+}
+
+static Lit *
+rdecide (PS * ps)
+{
+ unsigned idx, delta, spread;
+ Lit * res;
+
+ spread = RDECIDE;
+ if (rrng (ps, 1, spread) != 2)
+ return 0;
+
+ assert (1 <= ps->max_var);
+ idx = rrng (ps, 1, ps->max_var);
+ res = int2lit (ps, idx);
+
+ if (res->val != UNDEF)
+ {
+ delta = rrng (ps, 1, ps->max_var);
+ while (gcd (delta, ps->max_var) != 1)
+ delta--;
+
+ assert (1 <= delta);
+ assert (delta <= ps->max_var);
+
+ do {
+ idx += delta;
+ if (idx > ps->max_var)
+ idx -= ps->max_var;
+ res = int2lit (ps, idx);
+ } while (res->val != UNDEF);
+ }
+
+#ifdef STATS
+ ps->rdecisions++;
+#endif
+ res = decide_phase (ps, res);
+ LOG ( fprintf (ps->out, "%srdecide %d\n", ps->prefix, LIT2INT (res)));
+
+ return res;
+}
+
+static Lit *
+sdecide (PS * ps)
+{
+ Lit *res;
+ Rnk *r;
+
+ for (;;)
+ {
+ r = htop (ps);
+ res = RNK2LIT (r);
+ if (res->val == UNDEF) break;
+ (void) hpop (ps);
+ NOLOG ( fprintf (ps->out,
+ "%shpop %u %u %u\n",
+ ps->prefix, r - ps->rnks,
+ FLTMANTISSA(r->score),
+ FLTEXPONENT(r->score)));
+ }
+
+#ifdef STATS
+ ps->sdecisions++;
+#endif
+ res = decide_phase (ps, res);
+
+ LOG ( fprintf (ps->out, "%ssdecide %d\n", ps->prefix, LIT2INT (res)));
+
+ return res;
+}
+
+static Lit *
+adecide (PS * ps)
+{
+ Lit *lit;
+ Var * v;
+
+ assert (ps->als < ps->alshead);
+ assert (!ps->failed_assumption);
+
+ while (ps->alstail < ps->alshead)
+ {
+ lit = *ps->alstail++;
+
+ if (lit->val == FALSE)
+ {
+ ps->failed_assumption = lit;
+ v = LIT2VAR (lit);
+
+ use_var (ps, v);
+
+ LOG ( fprintf (ps->out, "%sfirst failed assumption %d\n",
+ ps->prefix, LIT2INT (ps->failed_assumption)));
+ fanalyze (ps);
+ return 0;
+ }
+
+ if (lit->val == TRUE)
+ {
+ v = LIT2VAR (lit);
+ if (v->level > ps->adecidelevel)
+ ps->adecidelevel = v->level;
+ continue;
+ }
+
+#ifdef STATS
+ ps->assumptions++;
+#endif
+ LOG ( fprintf (ps->out, "%sadecide %d\n", ps->prefix, LIT2INT (lit)));
+ ps->adecidelevel = ps->LEVEL + 1;
+
+ return lit;
+ }
+
+ return 0;
+}
+
+static void
+decide (PS * ps)
+{
+ Lit * lit;
+
+ assert (!satisfied (ps));
+ assert (!ps->conflict);
+
+ if (ps->alstail < ps->alshead && (lit = adecide (ps)))
+ ;
+ else if (ps->failed_assumption)
+ return;
+ else if (satisfied (ps))
+ return;
+ else if (!(lit = rdecide (ps)))
+ lit = sdecide (ps);
+
+ assert (lit);
+ assign_decision (ps, lit);
+
+ ps->levelsum += ps->LEVEL;
+ ps->decisions++;
+}
+
+static int
+sat (PS * ps, int l)
+{
+ int count = 0, backtracked;
+
+ if (!ps->conflict)
+ bcp (ps);
+
+ if (ps->conflict)
+ backtrack (ps);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ goto SATISFIED;
+
+ if (ps->lsimplify <= ps->propagations)
+ simplify (ps, 0);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ goto SATISFIED;
+
+ init_restart (ps);
+
+ if (!ps->lreduce)
+ init_reduce (ps);
+
+ ps->isimplify = ps->fixed;
+ backtracked = 0;
+
+ for (;;)
+ {
+ if (!ps->conflict)
+ bcp (ps);
+
+ if (ps->conflict)
+ {
+ incincs (ps);
+ backtrack (ps);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+ backtracked = 1;
+ continue;
+ }
+
+ if (satisfied (ps))
+ {
+SATISFIED:
+#ifndef NDEBUG
+ original_clauses_satisfied (ps);
+ assumptions_satisfied (ps);
+#endif
+ return PICOSAT_SATISFIABLE;
+ }
+
+ if (backtracked)
+ {
+ backtracked = 0;
+ if (!ps->LEVEL && ps->isimplify < ps->fixed)
+ iteration (ps);
+ }
+
+ if (l >= 0 && count >= l) /* decision limit reached ? */
+ return PICOSAT_UNKNOWN;
+
+ if (ps->interrupt.function && /* external interrupt */
+ count > 0 && !(INT_MOD(count, INTERRUPTLIM)) &&
+ ps->interrupt.function (ps->interrupt.state))
+ return PICOSAT_UNKNOWN;
+
+ if (ps->propagations >= ps->lpropagations)/* propagation limit reached ? */
+ return PICOSAT_UNKNOWN;
+
+#ifndef NADC
+ if (!ps->adodisabled && ps->adoconflicts >= ps->adoconflictlimit)
+ {
+ assert (bcp_queue_is_empty (ps));
+ return PICOSAT_UNKNOWN;
+ }
+#endif
+
+ if (ps->fsimplify < ps->fixed && ps->lsimplify <= ps->propagations)
+ {
+ simplify (ps, 0);
+ if (!bcp_queue_is_empty (ps))
+ continue;
+#ifndef NFL
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ return PICOSAT_SATISFIABLE;
+
+ assert (!ps->LEVEL);
+#endif
+ }
+
+ if (need_to_reduce (ps))
+ reduce (ps, 50);
+
+ if (ps->conflicts >= ps->lrestart && ps->LEVEL > 2)
+ restart (ps);
+
+ decide (ps);
+ if (ps->failed_assumption)
+ return PICOSAT_UNSATISFIABLE;
+ count++;
+ }
+}
+
+static void
+rebias (PS * ps)
+{
+ Cls ** p, * c;
+ Var * v;
+
+ for (v = ps->vars + 1; v <= ps->vars + ps->max_var; v++)
+ v->assigned = 0;
+
+ memset (ps->jwh, 0, 2 * (ps->max_var + 1) * sizeof *ps->jwh);
+
+ for (p = ps->oclauses; p < ps->ohead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (c->learned)
+ continue;
+
+ incjwh (ps, c);
+ }
+}
+
+#ifdef TRACE
+
+static unsigned
+core (PS * ps)
+{
+ unsigned idx, prev, this, delta, i, lcore, vcore;
+ unsigned *stack, *shead, *eos;
+ Lit **q, **eol, *lit;
+ Cls *c, *reason;
+ Znt *p, byte;
+ Zhn *zhain;
+ Var *v;
+
+ assert (ps->trace);
+
+ assert (ps->mtcls || ps->failed_assumption);
+ if (ps->ocore >= 0)
+ return ps->ocore;
+
+ lcore = ps->ocore = vcore = 0;
+
+ stack = shead = eos = 0;
+ ENLARGE (stack, shead, eos);
+
+ if (ps->mtcls)
+ {
+ idx = CLS2IDX (ps->mtcls);
+ *shead++ = idx;
+ }
+ else
+ {
+ assert (ps->failed_assumption);
+ v = LIT2VAR (ps->failed_assumption);
+ reason = v->reason;
+ assert (reason);
+ idx = CLS2IDX (reason);
+ *shead++ = idx;
+ }
+
+ while (shead > stack)
+ {
+ idx = *--shead;
+ zhain = IDX2ZHN (idx);
+
+ if (zhain)
+ {
+ if (zhain->core)
+ continue;
+
+ zhain->core = 1;
+ lcore++;
+
+ c = IDX2CLS (idx);
+ if (c)
+ {
+ assert (!c->core);
+ c->core = 1;
+ }
+
+ i = 0;
+ delta = 0;
+ prev = 0;
+ for (p = zhain->znt; (byte = *p); p++, i += 7)
+ {
+ delta |= (byte & 0x7f) << i;
+ if (byte & 0x80)
+ continue;
+
+ this = prev + delta;
+ assert (prev < this); /* no overflow */
+
+ if (shead == eos)
+ ENLARGE (stack, shead, eos);
+ *shead++ = this;
+
+ prev = this;
+ delta = 0;
+ i = -7;
+ }
+ }
+ else
+ {
+ c = IDX2CLS (idx);
+
+ assert (c);
+ assert (!c->learned);
+
+ if (c->core)
+ continue;
+
+ c->core = 1;
+ ps->ocore++;
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ {
+ lit = *q;
+ v = LIT2VAR (lit);
+ if (v->core)
+ continue;
+
+ v->core = 1;
+ vcore++;
+
+ if (!ps->failed_assumption) continue;
+ if (lit != ps->failed_assumption) continue;
+
+ reason = v->reason;
+ if (!reason) continue;
+ if (reason->core) continue;
+
+ idx = CLS2IDX (reason);
+ if (shead == eos)
+ ENLARGE (stack, shead, eos);
+ *shead++ = idx;
+ }
+ }
+ }
+
+ DELETEN (stack, eos - stack);
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%s%u core variables out of %u (%.1f%%)\n"
+ "%s%u core original clauses out of %u (%.1f%%)\n"
+ "%s%u core learned clauses out of %u (%.1f%%)\n",
+ ps->prefix, vcore, ps->max_var, PERCENT (vcore, ps->max_var),
+ ps->prefix, ps->ocore, ps->oadded, PERCENT (ps->ocore, ps->oadded),
+ ps->prefix, lcore, ps->ladded, PERCENT (lcore, ps->ladded));
+
+ return ps->ocore;
+}
+
+static void
+trace_lits (PS * ps, Cls * c, FILE * file)
+{
+ Lit **p, **eol = end_of_lits (c);
+
+ assert (c);
+ assert (c->core);
+
+ for (p = c->lits; p < eol; p++)
+ fprintf (file, "%d ", LIT2INT (*p));
+
+ fputc ('0', file);
+}
+
+static void
+write_idx (PS * ps, unsigned idx, FILE * file)
+{
+ fprintf (file, "%ld", EXPORTIDX (idx));
+}
+
+static void
+trace_clause (PS * ps, unsigned idx, Cls * c, FILE * file, int fmt)
+{
+ assert (c);
+ assert (c->core);
+ assert (fmt == RUP_TRACE_FMT || !c->learned);
+ assert (CLS2IDX (c) == idx);
+
+ if (fmt != RUP_TRACE_FMT)
+ {
+ write_idx (ps, idx, file);
+ fputc (' ', file);
+ }
+
+ trace_lits (ps, c, file);
+
+ if (fmt != RUP_TRACE_FMT)
+ fputs (" 0", file);
+
+ fputc ('\n', file);
+}
+
+static void
+trace_zhain (PS * ps, unsigned idx, Zhn * zhain, FILE * file, int fmt)
+{
+ unsigned prev, this, delta, i;
+ Znt *p, byte;
+ Cls * c;
+
+ assert (zhain);
+ assert (zhain->core);
+
+ write_idx (ps, idx, file);
+ fputc (' ', file);
+
+ if (fmt == EXTENDED_TRACECHECK_TRACE_FMT)
+ {
+ c = IDX2CLS (idx);
+ assert (c);
+ trace_lits (ps, c, file);
+ }
+ else
+ {
+ assert (fmt == COMPACT_TRACECHECK_TRACE_FMT);
+ putc ('*', file);
+ }
+
+ i = 0;
+ delta = 0;
+ prev = 0;
+
+ for (p = zhain->znt; (byte = *p); p++, i += 7)
+ {
+ delta |= (byte & 0x7f) << i;
+ if (byte & 0x80)
+ continue;
+
+ this = prev + delta;
+
+ putc (' ', file);
+ write_idx (ps, this, file);
+
+ prev = this;
+ delta = 0;
+ i = -7;
+ }
+
+ fputs (" 0\n", file);
+}
+
+static void
+write_core (PS * ps, FILE * file)
+{
+ Lit **q, **eol;
+ Cls **p, *c;
+
+ fprintf (file, "p cnf %u %u\n", ps->max_var, core (ps));
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c || c->learned || !c->core)
+ continue;
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ fprintf (file, "%d ", LIT2INT (*q));
+
+ fputs ("0\n", file);
+ }
+}
+
+#endif
+
+static void
+write_trace (PS * ps, FILE * file, int fmt)
+{
+#ifdef TRACE
+ Cls *c, ** p;
+ Zhn *zhain;
+ unsigned i;
+
+ core (ps);
+
+ if (fmt == RUP_TRACE_FMT)
+ {
+ ps->rupvariables = picosat_variables (ps),
+ ps->rupclauses = picosat_added_original_clauses (ps);
+ write_rup_header (ps, file);
+ }
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (ps->oclauses <= p && p < ps->eoo)
+ {
+ i = OIDX2IDX (p - ps->oclauses);
+ assert (!c || CLS2IDX (c) == i);
+ }
+ else
+ {
+ assert (ps->lclauses <= p && p < ps->EOL);
+ i = LIDX2IDX (p - ps->lclauses);
+ }
+
+ zhain = IDX2ZHN (i);
+
+ if (zhain)
+ {
+ if (zhain->core)
+ {
+ if (fmt == RUP_TRACE_FMT)
+ trace_clause (ps,i, c, file, fmt);
+ else
+ trace_zhain (ps, i, zhain, file, fmt);
+ }
+ }
+ else if (c)
+ {
+ if (fmt != RUP_TRACE_FMT && c)
+ {
+ if (c->core)
+ trace_clause (ps, i, c, file, fmt);
+ }
+ }
+ }
+#else
+ (void) file;
+ (void) fmt;
+ (void) ps;
+#endif
+}
+
+static void
+write_core_wrapper (PS * ps, FILE * file, int fmt)
+{
+ (void) fmt;
+#ifdef TRACE
+ write_core (ps, file);
+#else
+ (void) ps;
+ (void) file;
+#endif
+}
+
+static Lit *
+import_lit (PS * ps, int lit, int nointernal)
+{
+ Lit * res;
+ Var * v;
+
+ ABORTIF (lit == INT_MIN, "API usage: INT_MIN literal");
+ ABORTIF (abs (lit) > (int) ps->max_var && ps->CLS != ps->clshead,
+ "API usage: new variable index after 'picosat_push'");
+
+ if (abs (lit) <= (int) ps->max_var)
+ {
+ res = int2lit (ps, lit);
+ v = LIT2VAR (res);
+ if (nointernal && v->internal)
+ ABORT ("API usage: trying to import invalid literal");
+ else if (!nointernal && !v->internal)
+ ABORT ("API usage: trying to import invalid context");
+ }
+ else
+ {
+ while (abs (lit) > (int) ps->max_var)
+ inc_max_var (ps);
+ res = int2lit (ps, lit);
+ }
+
+ return res;
+}
+
+#ifdef TRACE
+static void
+reset_core (PS * ps)
+{
+ Cls ** p, * c;
+ Zhn ** q, * z;
+ unsigned i;
+
+ for (i = 1; i <= ps->max_var; i++)
+ ps->vars[i].core = 0;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ if ((c = *p))
+ c->core = 0;
+
+ for (q = ps->zhains; q != ps->zhead; q++)
+ if ((z = *q))
+ z->core = 0;
+
+ ps->ocore = -1;
+}
+#endif
+
+static void
+reset_assumptions (PS * ps)
+{
+ Lit ** p;
+
+ ps->failed_assumption = 0;
+
+ if (ps->extracted_all_failed_assumptions)
+ {
+ for (p = ps->als; p < ps->alshead; p++)
+ LIT2VAR (*p)->failed = 0;
+
+ ps->extracted_all_failed_assumptions = 0;
+ }
+
+ ps->alstail = ps->alshead = ps->als;
+ ps->adecidelevel = 0;
+}
+
+static INLINE void
+check_ready (PS * ps)
+{
+ ABORTIF (!ps || ps->state == RESET, "API usage: uninitialized");
+}
+
+static INLINE void
+check_sat_state (PS * ps)
+{
+ ABORTIF (ps->state != SAT, "API usage: expected to be in SAT state");
+}
+
+static INLINE void
+check_unsat_state (PS * ps)
+{
+ ABORTIF (ps->state != UNSAT, "API usage: expected to be in UNSAT state");
+}
+
+static INLINE void
+check_sat_or_unsat_or_unknown_state (PS * ps)
+{
+ ABORTIF (ps->state != SAT && ps->state != UNSAT && ps->state != UNKNOWN,
+ "API usage: expected to be in SAT, UNSAT, or UNKNOWN state");
+}
+
+static void
+reset_partial (PS * ps)
+{
+ unsigned idx;
+ if (!ps->partial)
+ return;
+ for (idx = 1; idx <= ps->max_var; idx++)
+ ps->vars[idx].partial = 0;
+ ps->partial = 0;
+}
+
+static void
+reset_incremental_usage (PS * ps)
+{
+ unsigned num_non_false;
+ Lit * lit, ** q;
+
+ check_sat_or_unsat_or_unknown_state (ps);
+
+ LOG ( fprintf (ps->out, "%sRESET incremental usage\n", ps->prefix));
+
+ if (ps->LEVEL)
+ undo (ps, 0);
+
+ reset_assumptions (ps);
+
+ if (ps->conflict)
+ {
+ num_non_false = 0;
+ for (q = ps->conflict->lits; q < end_of_lits (ps->conflict); q++)
+ {
+ lit = *q;
+ if (lit->val != FALSE)
+ num_non_false++;
+ }
+
+ // assert (num_non_false >= 2); // TODO: why this assertion?
+#ifdef NO_BINARY_CLAUSES
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+#endif
+#ifndef NADC
+ if (ps->conflict == ps->adoconflict)
+ resetadoconflict (ps);
+#endif
+ ps->conflict = 0;
+ }
+
+#ifdef TRACE
+ reset_core (ps);
+#endif
+
+ reset_partial (ps);
+
+ ps->saved_flips = ps->flips;
+ ps->min_flipped = UINT_MAX;
+ ps->saved_max_var = ps->max_var;
+
+ ps->state = READY;
+}
+
+static void
+enter (PS * ps)
+{
+ if (ps->nentered++)
+ return;
+
+ check_ready (ps);
+ ps->entered = picosat_time_stamp ();
+}
+
+static INLINE void
+leave (PS * ps)
+{
+ assert (ps->nentered);
+ if (--ps->nentered)
+ return;
+
+ sflush (ps);
+}
+
+static void
+check_trace_support_and_execute (PS * ps,
+ FILE * file,
+ void (*f)(PS*,FILE*,int), int fmt)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+#ifdef TRACE
+ ABORTIF (!ps->trace, "API usage: tracing disabled");
+ enter (ps);
+ f (ps, file, fmt);
+ leave (ps);
+#else
+ (void) file;
+ (void) fmt;
+ (void) f;
+ ABORT ("compiled without trace support");
+#endif
+}
+
+static void
+extract_all_failed_assumptions (PS * ps)
+{
+ Lit ** p, ** eol;
+ Var * v, * u;
+ int pos;
+ Cls * c;
+
+ assert (!ps->extracted_all_failed_assumptions);
+
+ assert (ps->failed_assumption);
+ assert (ps->mhead == ps->marked);
+
+ if (ps->marked == ps->eom)
+ ENLARGE (ps->marked, ps->mhead, ps->eom);
+
+ v = LIT2VAR (ps->failed_assumption);
+ mark_var (ps, v);
+ pos = 0;
+
+ while (pos < ps->mhead - ps->marked)
+ {
+ v = ps->marked[pos++];
+ assert (v->mark);
+ c = var2reason (ps, v);
+ if (!c)
+ continue;
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ u = LIT2VAR (*p);
+ if (!u->mark)
+ mark_var (ps, u);
+ }
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ }
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ u = LIT2VAR (*p);
+ if (!u->mark) continue;
+ u->failed = 1;
+ LOG ( fprintf (ps->out,
+ "%sfailed assumption %d\n",
+ ps->prefix, LIT2INT (*p)));
+ }
+
+ while (ps->mhead > ps->marked)
+ (*--ps->mhead)->mark = 0;
+
+ ps->extracted_all_failed_assumptions = 1;
+}
+
+const char *
+picosat_copyright (void)
+{
+ return "Copyright (c) 2006 - 2014 Armin Biere JKU Linz";
+}
+
+PicoSAT *
+picosat_init (void)
+{
+ return init (0, 0, 0, 0);
+}
+
+PicoSAT *
+picosat_minit (void * pmgr,
+ picosat_malloc pnew,
+ picosat_realloc presize,
+ picosat_free pfree)
+{
+ ABORTIF (!pnew, "API usage: zero 'picosat_malloc' argument");
+ ABORTIF (!presize, "API usage: zero 'picosat_realloc' argument");
+ ABORTIF (!pfree, "API usage: zero 'picosat_free' argument");
+ return init (pmgr, pnew, presize, pfree);
+}
+
+
+void
+picosat_adjust (PS * ps, int new_max_var)
+{
+ unsigned new_size_vars;
+
+ ABORTIF (abs (new_max_var) > (int) ps->max_var && ps->CLS != ps->clshead,
+ "API usage: adjusting variable index after 'picosat_push'");
+ enter (ps);
+
+ new_max_var = abs (new_max_var);
+ new_size_vars = new_max_var + 1;
+
+ if (ps->size_vars < new_size_vars)
+ enlarge (ps, new_size_vars);
+
+ while (ps->max_var < (unsigned) new_max_var)
+ inc_max_var (ps);
+
+ leave (ps);
+}
+
+int
+picosat_inc_max_var (PS * ps)
+{
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ inc_max_var (ps);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return ps->max_var;
+}
+
+int
+picosat_context (PS * ps)
+{
+ return ps->clshead == ps->CLS ? 0 : LIT2INT (ps->clshead[-1]);
+}
+
+int
+picosat_push (PS * ps)
+{
+ int res;
+ Lit *lit;
+ Var * v;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ if (ps->rils != ps->rilshead)
+ {
+ res = *--ps->rilshead;
+ assert (ps->vars[res].internal);
+ }
+ else
+ {
+ inc_max_var (ps);
+ res = ps->max_var;
+ v = ps->vars + res;
+ assert (!v->internal);
+ v->internal = 1;
+ ps->internals++;
+ LOG ( fprintf (ps->out, "%snew internal variable index %d\n", ps->prefix, res));
+ }
+
+ lit = int2lit (ps, res);
+
+ if (ps->clshead == ps->eocls)
+ ENLARGE (ps->CLS, ps->clshead, ps->eocls);
+ *ps->clshead++ = lit;
+
+ ps->contexts++;
+
+ LOG ( fprintf (ps->out, "%snew context %d at depth %ld after push\n",
+ ps->prefix, res, (long)(ps->clshead - ps->CLS)));
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+int
+picosat_pop (PS * ps)
+{
+ Lit * lit;
+ int res;
+ ABORTIF (ps->CLS == ps->clshead, "API usage: too many 'picosat_pop'");
+ ABORTIF (ps->added != ps->ahead, "API usage: incomplete clause");
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assert (ps->CLS < ps->clshead);
+ lit = *--ps->clshead;
+ LOG ( fprintf (ps->out, "%sclosing context %d at depth %ld after pop\n",
+ ps->prefix, LIT2INT (lit), (long)(ps->clshead - ps->CLS) + 1));
+
+ if (ps->cilshead == ps->eocils)
+ ENLARGE (ps->cils, ps->cilshead, ps->eocils);
+ *ps->cilshead++ = LIT2INT (lit);
+
+ if (ps->cilshead - ps->cils > MAXCILS) {
+ LOG ( fprintf (ps->out,
+ "%srecycling %ld interals with forced simplification\n",
+ ps->prefix, (long)(ps->cilshead - ps->cils)));
+ simplify (ps, 1);
+ }
+
+ res = picosat_context (ps);
+ if (res)
+ LOG ( fprintf (ps->out, "%snew context %d at depth %ld after pop\n",
+ ps->prefix, res, (long)(ps->clshead - ps->CLS)));
+ else
+ LOG ( fprintf (ps->out, "%souter most context reached after pop\n", ps->prefix));
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+void
+picosat_set_verbosity (PS * ps, int new_verbosity_level)
+{
+ check_ready (ps);
+ ps->verbosity = new_verbosity_level;
+}
+
+void
+picosat_set_plain (PS * ps, int new_plain_value)
+{
+ check_ready (ps);
+ ps->plain = new_plain_value;
+}
+
+int
+picosat_enable_trace_generation (PS * ps)
+{
+ int res = 0;
+ check_ready (ps);
+#ifdef TRACE
+ ABORTIF (ps->addedclauses,
+ "API usage: trace generation enabled after adding clauses");
+ res = ps->trace = 1;
+#endif
+ return res;
+}
+
+void
+picosat_set_incremental_rup_file (PS * ps, FILE * rup_file, int m, int n)
+{
+ check_ready (ps);
+ assert (!ps->rupstarted);
+ ps->rup = rup_file;
+ ps->rupvariables = m;
+ ps->rupclauses = n;
+}
+
+void
+picosat_set_output (PS * ps, FILE * output_file)
+{
+ check_ready (ps);
+ ps->out = output_file;
+}
+
+void
+picosat_measure_all_calls (PS * ps)
+{
+ check_ready (ps);
+ ps->measurealltimeinlib = 1;
+}
+
+void
+picosat_set_prefix (PS * ps, const char * str)
+{
+ check_ready (ps);
+ new_prefix (ps, str);
+}
+
+void
+picosat_set_seed (PS * ps, unsigned s)
+{
+ check_ready (ps);
+ ps->srng = s;
+}
+
+void
+picosat_reset (PS * ps)
+{
+ check_ready (ps);
+ reset (ps);
+}
+
+int
+picosat_add (PS * ps, int int_lit)
+{
+ int res = ps->oadded;
+ Lit *lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ ABORTIF (ps->rup && ps->rupstarted && ps->oadded >= (unsigned)ps->rupclauses,
+ "API usage: adding too many clauses after RUP header written");
+#ifndef NADC
+ ABORTIF (ps->addingtoado,
+ "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
+#endif
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ if (ps->saveorig)
+ {
+ if (ps->sohead == ps->eoso)
+ ENLARGE (ps->soclauses, ps->sohead, ps->eoso);
+
+ *ps->sohead++ = int_lit;
+ }
+
+ if (int_lit)
+ {
+ lit = import_lit (ps, int_lit, 1);
+ add_lit (ps, lit);
+ }
+ else
+ simplify_and_add_original_clause (ps);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+int
+picosat_add_arg (PS * ps, ...)
+{
+ int lit;
+ va_list ap;
+ va_start (ap, ps);
+ while ((lit = va_arg (ap, int)))
+ (void) picosat_add (ps, lit);
+ va_end (ap);
+ return picosat_add (ps, 0);
+}
+
+int
+picosat_add_lits (PS * ps, int * lits)
+{
+ const int * p;
+ int lit;
+ for (p = lits; (lit = *p); p++)
+ (void) picosat_add (ps, lit);
+ return picosat_add (ps, 0);
+}
+
+void
+picosat_add_ado_lit (PS * ps, int external_lit)
+{
+#ifndef NADC
+ Lit * internal_lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ ABORTIF (!ps->addingtoado && ps->ahead > ps->added,
+ "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
+
+ if (external_lit)
+ {
+ ps->addingtoado = 1;
+ internal_lit = import_lit (ps, external_lit, 1);
+ add_lit (ps, internal_lit);
+ }
+ else
+ {
+ ps->addingtoado = 0;
+ add_ado (ps);
+ }
+ if (ps->measurealltimeinlib)
+ leave (ps);
+#else
+ (void) ps;
+ (void) external_lit;
+ ABORT ("compiled without all different constraint support");
+#endif
+}
+
+static void
+assume (PS * ps, Lit * lit)
+{
+ if (ps->alshead == ps->eoals)
+ {
+ assert (ps->alstail == ps->als);
+ ENLARGE (ps->als, ps->alshead, ps->eoals);
+ ps->alstail = ps->als;
+ }
+
+ *ps->alshead++ = lit;
+ LOG ( fprintf (ps->out, "%sassumption %d\n", ps->prefix, LIT2INT (lit)));
+}
+
+static void
+assume_contexts (PS * ps)
+{
+ Lit ** p;
+ if (ps->als != ps->alshead)
+ return;
+ for (p = ps->CLS; p != ps->clshead; p++)
+ assume (ps, *p);
+}
+
+#ifndef RCODE
+static const char * enumstr (int i) {
+ int last = INT_MOD(i, 10);
+ if (last == 1) return "st";
+ if (last == 2) return "nd";
+ if (last == 3) return "rd";
+ return "th";
+}
+#endif
+
+static int
+tderef (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+
+ assert (abs (int_lit) <= (int) ps->max_var);
+
+ lit = int2lit (ps, int_lit);
+
+ v = LIT2VAR (lit);
+ if (v->level > 0)
+ return 0;
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+static int
+pderef (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+
+ assert (abs (int_lit) <= (int) ps->max_var);
+
+ v = ps->vars + abs (int_lit);
+ if (!v->partial)
+ return 0;
+
+ lit = int2lit (ps, int_lit);
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+static void
+minautarky (PS * ps)
+{
+ unsigned * occs, maxoccs, tmpoccs, npartial;
+ int * p, * c, lit, best, val;
+#ifdef LOGGING
+ int tl;
+#endif
+
+ assert (!ps->partial);
+
+ npartial = 0;
+
+ NEWN (occs, 2*ps->max_var + 1);
+ CLRN (occs, 2*ps->max_var + 1);
+ occs += ps->max_var;
+ for (p = ps->soclauses; p < ps->sohead; p++)
+ occs[*p]++;
+ assert (occs[0] == ps->oadded);
+
+ for (c = ps->soclauses; c < ps->sohead; c = p + 1)
+ {
+#ifdef LOGGING
+ tl = 0;
+#endif
+ best = 0;
+ maxoccs = 0;
+ for (p = c; (lit = *p); p++)
+ {
+ val = tderef (ps, lit);
+ if (val < 0)
+ continue;
+ if (val > 0)
+ {
+#ifdef LOGGING
+ tl = 1;
+#endif
+ best = lit;
+ maxoccs = occs[lit];
+ }
+
+ val = pderef (ps, lit);
+ if (val > 0)
+ break;
+ if (val < 0)
+ continue;
+ val = int2lit (ps, lit)->val;
+ assert (val);
+ if (val < 0)
+ continue;
+ tmpoccs = occs[lit];
+ if (best && tmpoccs <= maxoccs)
+ continue;
+ best = lit;
+ maxoccs = tmpoccs;
+ }
+ if (!lit)
+ {
+ assert (best);
+ LOG ( fprintf (ps->out, "%sautark %d with %d occs%s\n",
+ ps->prefix, best, maxoccs, tl ? " (top)" : ""));
+ ps->vars[abs (best)].partial = 1;
+ npartial++;
+ }
+ for (p = c; (lit = *p); p++)
+ {
+ assert (occs[lit] > 0);
+ occs[lit]--;
+ }
+ }
+ occs -= ps->max_var;
+ DELETEN (occs, 2*ps->max_var + 1);
+ ps->partial = 1;
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sautarky of size %u out of %u satisfying all clauses (%.1f%%)\n",
+ ps->prefix, npartial, ps->max_var, PERCENT (npartial, ps->max_var));
+}
+
+void
+picosat_assume (PS * ps, int int_lit)
+{
+ Lit *lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assume_contexts (ps);
+ lit = import_lit (ps, int_lit, 1);
+ assume (ps, lit);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+}
+
+int
+picosat_sat (PS * ps, int l)
+{
+ int res;
+ char ch;
+
+ enter (ps);
+
+ ps->calls++;
+ LOG ( fprintf (ps->out, "%sSTART call %u\n", ps->prefix, ps->calls));
+
+ if (ps->added < ps->ahead)
+ {
+#ifndef NADC
+ if (ps->addingtoado)
+ ABORT ("API usage: incomplete all different constraint");
+ else
+#endif
+ ABORT ("API usage: incomplete clause");
+ }
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assume_contexts (ps);
+
+ res = sat (ps, l);
+
+ assert (ps->state == READY);
+
+ switch (res)
+ {
+ case PICOSAT_UNSATISFIABLE:
+ ch = '0';
+ ps->state = UNSAT;
+ break;
+ case PICOSAT_SATISFIABLE:
+ ch = '1';
+ ps->state = SAT;
+ break;
+ default:
+ ch = '?';
+ ps->state = UNKNOWN;
+ break;
+ }
+
+ if (ps->verbosity)
+ {
+ report (ps, 1, ch);
+ rheader (ps);
+ }
+
+ leave (ps);
+ LOG ( fprintf (ps->out, "%sEND call %u result %d\n", ps->prefix, ps->calls, res));
+
+ ps->last_sat_call_result = res;
+
+ return res;
+}
+
+int
+picosat_res (PS * ps)
+{
+ return ps->last_sat_call_result;
+}
+
+int
+picosat_deref (PS * ps, int int_lit)
+{
+ Lit *lit;
+
+ check_ready (ps);
+ check_sat_state (ps);
+ ABORTIF (!int_lit, "API usage: can not deref zero literal");
+ ABORTIF (ps->mtcls, "API usage: deref after empty clause generated");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+
+ lit = int2lit (ps, int_lit);
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+int
+picosat_deref_toplevel (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ ABORTIF (!int_lit, "API usage: can not deref zero literal");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+
+ return tderef (ps, int_lit);
+}
+
+int
+picosat_inconsistent (PS * ps)
+{
+ check_ready (ps);
+ return ps->mtcls != 0;
+}
+
+int
+picosat_corelit (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+ ABORTIF (!int_lit, "API usage: zero literal can not be in core");
+
+ assert (ps->mtcls || ps->failed_assumption);
+
+#ifdef TRACE
+ {
+ int res = 0;
+ ABORTIF (!ps->trace, "tracing disabled");
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ core (ps);
+ if (abs (int_lit) <= (int) ps->max_var)
+ res = ps->vars[abs (int_lit)].core;
+ assert (!res || ps->failed_assumption || ps->vars[abs (int_lit)].used);
+ if (ps->measurealltimeinlib)
+ leave (ps);
+ return res;
+ }
+#else
+ ABORT ("compiled without trace support");
+ return 0;
+#endif
+}
+
+int
+picosat_coreclause (PS * ps, int ocls)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+
+ ABORTIF (ocls < 0, "API usage: negative original clause index");
+ ABORTIF (ocls >= (int)ps->oadded, "API usage: original clause index exceeded");
+
+ assert (ps->mtcls || ps->failed_assumption);
+
+#ifdef TRACE
+ {
+ Cls ** clsptr, * c;
+ int res = 0;
+
+ ABORTIF (!ps->trace, "tracing disabled");
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ core (ps);
+ clsptr = ps->oclauses + ocls;
+ assert (clsptr < ps->ohead);
+ c = *clsptr;
+ if (c)
+ res = c->core;
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+ }
+#else
+ ABORT ("compiled without trace support");
+ return 0;
+#endif
+}
+
+int
+picosat_failed_assumption (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ ABORTIF (!int_lit, "API usage: zero literal as assumption");
+ check_ready (ps);
+ check_unsat_state (ps);
+ if (ps->mtcls)
+ return 0;
+ assert (ps->failed_assumption);
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ return v->failed;
+}
+
+int
+picosat_failed_context (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ ABORTIF (!int_lit, "API usage: zero literal as context");
+ ABORTIF (abs (int_lit) > (int) ps->max_var, "API usage: invalid context");
+ check_ready (ps);
+ check_unsat_state (ps);
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+ lit = import_lit (ps, int_lit, 0);
+ v = LIT2VAR (lit);
+ return v->failed;
+}
+
+const int *
+picosat_failed_assumptions (PS * ps)
+{
+ Lit ** p, * lit;
+ Var * v;
+ int ilit;
+
+ ps->falshead = ps->fals;
+ check_ready (ps);
+ check_unsat_state (ps);
+ if (!ps->mtcls)
+ {
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (*p);
+ if (!v->failed)
+ continue;
+ ilit = LIT2INT (lit);
+ if (ps->falshead == ps->eofals)
+ ENLARGE (ps->fals, ps->falshead, ps->eofals);
+ *ps->falshead++ = ilit;
+ }
+ }
+ if (ps->falshead == ps->eofals)
+ ENLARGE (ps->fals, ps->falshead, ps->eofals);
+ *ps->falshead++ = 0;
+ return ps->fals;
+}
+
+const int *
+picosat_mus_assumptions (PS * ps, void * s, void (*cb)(void*,const int*), int fix)
+{
+ int i, j, ilit, len, nwork, * work, res;
+ signed char * redundant;
+ Lit ** p, * lit;
+ int failed;
+ Var * v;
+#ifndef NDEBUG
+ int oldlen;
+#endif
+#ifndef RCODE
+ int norig = ps->alshead - ps->als;
+#endif
+
+ check_ready (ps);
+ check_unsat_state (ps);
+ len = 0;
+ if (!ps->mtcls)
+ {
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+
+ for (p = ps->als; p < ps->alshead; p++)
+ if (LIT2VAR (*p)->failed)
+ len++;
+ }
+
+ if (ps->mass)
+ DELETEN (ps->mass, ps->szmass);
+ ps->szmass = len + 1;
+ NEWN (ps->mass, ps->szmass);
+
+ i = 0;
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (lit);
+ if (!v->failed)
+ continue;
+ ilit = LIT2INT (lit);
+ assert (i < len);
+ ps->mass[i++] = ilit;
+ }
+ assert (i == len);
+ ps->mass[i] = 0;
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sinitial set of failed assumptions of size %d out of %d (%.0f%%)\n",
+ ps->prefix, len, norig, PERCENT (len, norig));
+ if (cb)
+ cb (s, ps->mass);
+
+ nwork = len;
+ NEWN (work, nwork);
+ for (i = 0; i < len; i++)
+ work[i] = ps->mass[i];
+
+ NEWN (redundant, nwork);
+ CLRN (redundant, nwork);
+
+ for (i = 0; i < nwork; i++)
+ {
+ if (redundant[i])
+ continue;
+
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%strying to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+ for (j = 0; j < nwork; j++)
+ {
+ if (i == j) continue;
+ if (j < i && fix) continue;
+ if (redundant[j]) continue;
+ picosat_assume (ps, work[j]);
+ }
+
+ res = picosat_sat (ps, -1);
+ if (res == 10)
+ {
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%sfailed to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+
+ if (fix)
+ {
+ picosat_add (ps, work[i]);
+ picosat_add (ps, 0);
+ }
+ }
+ else
+ {
+ assert (res == 20);
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%ssuceeded to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+ redundant[i] = 1;
+ for (j = 0; j < nwork; j++)
+ {
+ failed = picosat_failed_assumption (ps, work[j]);
+ if (j <= i)
+ {
+ assert ((j < i && fix) || redundant[j] == !failed);
+ continue;
+ }
+
+ if (!failed)
+ {
+ redundant[j] = -1;
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%salso suceeded to drop %d%s assumption %d\n",
+ ps->prefix, j, enumstr (j), work[j]);
+ }
+ }
+
+#ifndef NDEBUG
+ oldlen = len;
+#endif
+ len = 0;
+ for (j = 0; j < nwork; j++)
+ if (!redundant[j])
+ ps->mass[len++] = work[j];
+ ps->mass[len] = 0;
+ assert (len < oldlen);
+
+ if (fix)
+ {
+ picosat_add (ps, -work[i]);
+ picosat_add (ps, 0);
+ }
+
+#ifndef NDEBUG
+ for (j = 0; j <= i; j++)
+ assert (redundant[j] >= 0);
+#endif
+ for (j = i + 1; j < nwork; j++)
+ {
+ if (redundant[j] >= 0)
+ continue;
+
+ if (fix)
+ {
+ picosat_add (ps, -work[j]);
+ picosat_add (ps, 0);
+ }
+
+ redundant[j] = 1;
+ }
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sreduced set of failed assumptions of size %d out of %d (%.0f%%)\n",
+ ps->prefix, len, norig, PERCENT (len, norig));
+ if (cb)
+ cb (s, ps->mass);
+ }
+ }
+
+ DELETEN (work, nwork);
+ DELETEN (redundant, nwork);
+
+ if (ps->verbosity)
+ {
+ fprintf (ps->out, "%sreinitializing unsat state\n", ps->prefix);
+ fflush (ps->out);
+ }
+
+ for (i = 0; i < len; i++)
+ picosat_assume (ps, ps->mass[i]);
+
+#ifndef NDEBUG
+ res =
+#endif
+ picosat_sat (ps, -1);
+ assert (res == 20);
+
+ if (!ps->mtcls)
+ {
+ assert (!ps->extracted_all_failed_assumptions);
+ extract_all_failed_assumptions (ps);
+ }
+
+ return ps->mass;
+}
+
+static const int *
+mss (PS * ps, int * a, int size)
+{
+ int i, j, k, res;
+
+ assert (!ps->mtcls);
+
+ if (ps->szmssass)
+ DELETEN (ps->mssass, ps->szmssass);
+
+ ps->szmssass = 0;
+ ps->mssass = 0;
+
+ ps->szmssass = size + 1;
+ NEWN (ps->mssass, ps->szmssass);
+
+ LOG ( fprintf (ps->out, "%ssearch MSS over %d assumptions\n", ps->prefix, size));
+
+ k = 0;
+ for (i = k; i < size; i++)
+ {
+ for (j = 0; j < k; j++)
+ picosat_assume (ps, ps->mssass[j]);
+
+ LOG ( fprintf (ps->out,
+ "%strying to add assumption %d to MSS : %d\n",
+ ps->prefix, i, a[i]));
+
+ picosat_assume (ps, a[i]);
+
+ res = picosat_sat (ps, -1);
+ if (res == 10)
+ {
+ LOG ( fprintf (ps->out,
+ "%sadding assumption %d to MSS : %d\n", ps->prefix, i, a[i]));
+
+ ps->mssass[k++] = a[i];
+
+ for (j = i + 1; j < size; j++)
+ {
+ if (picosat_deref (ps, a[j]) <= 0)
+ continue;
+
+ LOG ( fprintf (ps->out,
+ "%salso adding assumption %d to MSS : %d\n",
+ ps->prefix, j, a[j]));
+
+ ps->mssass[k++] = a[j];
+
+ if (++i != j)
+ {
+ int tmp = a[i];
+ a[i] = a[j];
+ a[j] = tmp;
+ }
+ }
+ }
+ else
+ {
+ assert (res == 20);
+
+ LOG ( fprintf (ps->out,
+ "%signoring assumption %d in MSS : %d\n", ps->prefix, i, a[i]));
+ }
+ }
+ ps->mssass[k] = 0;
+ LOG ( fprintf (ps->out, "%sfound MSS of size %d\n", ps->prefix, k));
+
+ return ps->mssass;
+}
+
+static void
+reassume (PS * ps, const int * a, int size)
+{
+ int i;
+ LOG ( fprintf (ps->out, "%sreassuming all assumptions\n", ps->prefix));
+ for (i = 0; i < size; i++)
+ picosat_assume (ps, a[i]);
+}
+
+const int *
+picosat_maximal_satisfiable_subset_of_assumptions (PS * ps)
+{
+ const int * res;
+ int i, *a, size;
+
+ ABORTIF (ps->mtcls,
+ "API usage: CNF inconsistent (use 'picosat_inconsistent')");
+
+ enter (ps);
+
+ size = ps->alshead - ps->als;
+ NEWN (a, size);
+
+ for (i = 0; i < size; i++)
+ a[i] = LIT2INT (ps->als[i]);
+
+ res = mss (ps, a, size);
+ reassume (ps, a, size);
+
+ DELETEN (a, size);
+
+ leave (ps);
+
+ return res;
+}
+
+static void
+check_mss_flags_clean (PS * ps)
+{
+#ifndef NDEBUG
+ unsigned i;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ assert (!ps->vars[i].msspos);
+ assert (!ps->vars[i].mssneg);
+ }
+#else
+ (void) ps;
+#endif
+}
+
+static void
+push_mcsass (PS * ps, int lit)
+{
+ if (ps->nmcsass == ps->szmcsass)
+ {
+ ps->szmcsass = ps->szmcsass ? 2*ps->szmcsass : 1;
+ RESIZEN (ps->mcsass, ps->nmcsass, ps->szmcsass);
+ }
+
+ ps->mcsass[ps->nmcsass++] = lit;
+}
+
+static const int *
+next_mss (PS * ps, int mcs)
+{
+ int i, *a, size, mssize, mcsize, lit, inmss;
+ const int * res, * p;
+ Var * v;
+
+ if (ps->mtcls) return 0;
+
+ check_mss_flags_clean (ps);
+
+ if (mcs && ps->mcsass)
+ {
+ DELETEN (ps->mcsass, ps->szmcsass);
+ ps->nmcsass = ps->szmcsass = 0;
+ ps->mcsass = 0;
+ }
+
+ size = ps->alshead - ps->als;
+ NEWN (a, size);
+
+ for (i = 0; i < size; i++)
+ a[i] = LIT2INT (ps->als[i]);
+
+ (void) picosat_sat (ps, -1);
+
+ //TODO short cut for 'picosat_res () == 10'?
+
+ if (ps->mtcls)
+ {
+ assert (picosat_res (ps) == 20);
+ res = 0;
+ goto DONE;
+ }
+
+ res = mss (ps, a, size);
+
+ if (ps->mtcls)
+ {
+ res = 0;
+ goto DONE;
+ }
+
+ for (p = res; (lit = *p); p++)
+ {
+ v = ps->vars + abs (lit);
+ if (lit < 0)
+ {
+ assert (!v->msspos);
+ v->mssneg = 1;
+ }
+ else
+ {
+ assert (!v->mssneg);
+ v->msspos = 1;
+ }
+ }
+
+ mssize = p - res;
+ mcsize = INT_MIN;
+
+ for (i = 0; i < size; i++)
+ {
+ lit = a[i];
+ v = ps->vars + abs (lit);
+ if (lit > 0 && v->msspos)
+ inmss = 1;
+ else if (lit < 0 && v->mssneg)
+ inmss = 1;
+ else
+ inmss = 0;
+
+ if (mssize < mcsize)
+ {
+ if (inmss)
+ picosat_add (ps, -lit);
+ }
+ else
+ {
+ if (!inmss)
+ picosat_add (ps, lit);
+ }
+
+ if (!inmss && mcs)
+ push_mcsass (ps, lit);
+ }
+ picosat_add (ps, 0);
+ if (mcs)
+ push_mcsass (ps, 0);
+
+ for (i = 0; i < size; i++)
+ {
+ lit = a[i];
+ v = ps->vars + abs (lit);
+ v->msspos = 0;
+ v->mssneg = 0;
+ }
+
+DONE:
+
+ reassume (ps, a, size);
+ DELETEN (a, size);
+
+ return res;
+}
+
+const int *
+picosat_next_maximal_satisfiable_subset_of_assumptions (PS * ps)
+{
+ const int * res;
+ enter (ps);
+ res = next_mss (ps, 0);
+ leave (ps);
+ return res;
+}
+
+const int *
+picosat_next_minimal_correcting_subset_of_assumptions (PS * ps)
+{
+ const int * res, * tmp;
+ enter (ps);
+ tmp = next_mss (ps, 1);
+ res = tmp ? ps->mcsass : 0;
+ leave (ps);
+ return res;
+}
+
+const int *
+picosat_humus (PS * ps,
+ void (*callback)(void*state,int nmcs,int nhumus),
+ void * state)
+{
+ int lit, nmcs, j, nhumus;
+ const int * mcs, * p;
+ unsigned i;
+ Var * v;
+ enter (ps);
+#ifndef NDEBUG
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ assert (!v->humuspos);
+ assert (!v->humusneg);
+ }
+#endif
+ nhumus = nmcs = 0;
+ while ((mcs = picosat_next_minimal_correcting_subset_of_assumptions (ps)))
+ {
+ for (p = mcs; (lit = *p); p++)
+ {
+ v = ps->vars + abs (lit);
+ if (lit < 0)
+ {
+ if (!v->humusneg)
+ {
+ v->humusneg = 1;
+ nhumus++;
+ }
+ }
+ else
+ {
+ if (!v->humuspos)
+ {
+ v->humuspos = 1;
+ nhumus++;
+ }
+ }
+ }
+ nmcs++;
+ LOG ( fprintf (ps->out,
+ "%smcs %d of size %d humus %d\n",
+ ps->prefix, nmcs, (int)(p - mcs), nhumus));
+ if (callback)
+ callback (state, nmcs, nhumus);
+ }
+ assert (!ps->szhumus);
+ ps->szhumus = 1;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ if (v->humuspos)
+ ps->szhumus++;
+ if (v->humusneg)
+ ps->szhumus++;
+ }
+ assert (nhumus + 1 == ps->szhumus);
+ NEWN (ps->humus, ps->szhumus);
+ j = 0;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ if (v->humuspos)
+ {
+ assert (j < nhumus);
+ ps->humus[j++] = (int) i;
+ }
+ if (v->humusneg)
+ {
+ assert (j < nhumus);
+ assert (i < INT_MAX);
+ ps->humus[j++] = - (int) i;
+ }
+ }
+ assert (j == nhumus);
+ assert (j < ps->szhumus);
+ ps->humus[j] = 0;
+ leave (ps);
+ return ps->humus;
+}
+
+int
+picosat_usedlit (PS * ps, int int_lit)
+{
+ int res;
+ check_ready (ps);
+ check_sat_or_unsat_or_unknown_state (ps);
+ ABORTIF (!int_lit, "API usage: zero literal can not be used");
+ int_lit = abs (int_lit);
+ res = (int_lit <= (int) ps->max_var) ? ps->vars[int_lit].used : 0;
+ return res;
+}
+
+void
+picosat_write_clausal_core (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_core_wrapper, 0);
+}
+
+void
+picosat_write_compact_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace,
+ COMPACT_TRACECHECK_TRACE_FMT);
+}
+
+void
+picosat_write_extended_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace,
+ EXTENDED_TRACECHECK_TRACE_FMT);
+}
+
+void
+picosat_write_rup_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace, RUP_TRACE_FMT);
+}
+
+size_t
+picosat_max_bytes_allocated (PS * ps)
+{
+ check_ready (ps);
+ return ps->max_bytes;
+}
+
+void
+picosat_set_propagation_limit (PS * ps, unsigned long long l)
+{
+ ps->lpropagations = l;
+}
+
+unsigned long long
+picosat_propagations (PS * ps)
+{
+ return ps->propagations;
+}
+
+unsigned long long
+picosat_visits (PS * ps)
+{
+ return ps->visits;
+}
+
+unsigned long long
+picosat_decisions (PS * ps)
+{
+ return ps->decisions;
+}
+
+int
+picosat_variables (PS * ps)
+{
+ check_ready (ps);
+ return (int) ps->max_var;
+}
+
+int
+picosat_added_original_clauses (PS * ps)
+{
+ check_ready (ps);
+ return (int) ps->oadded;
+}
+
+void
+picosat_stats (PS * ps)
+{
+#ifndef RCODE
+ unsigned redlits;
+#endif
+#ifdef STATS
+ check_ready (ps);
+ assert (ps->sdecisions + ps->rdecisions + ps->assumptions == ps->decisions);
+#endif
+ if (ps->calls > 1)
+ fprintf (ps->out, "%s%u calls\n", ps->prefix, ps->calls);
+ if (ps->contexts)
+ {
+ fprintf (ps->out, "%s%u contexts", ps->prefix, ps->contexts);
+#ifdef STATS
+ fprintf (ps->out, " %u internal variables", ps->internals);
+#endif
+ fprintf (ps->out, "\n");
+ }
+ fprintf (ps->out, "%s%u iterations\n", ps->prefix, ps->iterations);
+ fprintf (ps->out, "%s%u restarts", ps->prefix, ps->restarts);
+#ifdef STATS
+ fprintf (ps->out, " (%u skipped)", ps->skippedrestarts);
+#endif
+ fputc ('\n', ps->out);
+#ifndef NFL
+ fprintf (ps->out, "%s%u failed literals", ps->prefix, ps->failedlits);
+#ifdef STATS
+ fprintf (ps->out,
+ ", %u calls, %u rounds, %llu propagations",
+ ps->flcalls, ps->flrounds, ps->flprops);
+#endif
+ fputc ('\n', ps->out);
+#ifdef STATS
+ fprintf (ps->out,
+ "%sfl: %u = %.1f%% implicit, %llu oopsed, %llu tried, %llu skipped\n",
+ ps->prefix,
+ ps->ifailedlits, PERCENT (ps->ifailedlits, ps->failedlits),
+ ps->floopsed, ps->fltried, ps->flskipped);
+#endif
+#endif
+ fprintf (ps->out, "%s%u conflicts", ps->prefix, ps->conflicts);
+#ifdef STATS
+ fprintf (ps->out, " (%u uips = %.1f%%)\n", ps->uips, PERCENT(ps->uips,ps->conflicts));
+#else
+ fputc ('\n', ps->out);
+#endif
+#ifndef NADC
+ fprintf (ps->out, "%s%u adc conflicts\n", ps->prefix, ps->adoconflicts);
+#endif
+#ifdef STATS
+ fprintf (ps->out, "%s%llu dereferenced literals\n", ps->prefix, ps->derefs);
+#endif
+ fprintf (ps->out, "%s%u decisions", ps->prefix, ps->decisions);
+#ifdef STATS
+ fprintf (ps->out, " (%u random = %.2f%%",
+ ps->rdecisions, PERCENT (ps->rdecisions, ps->decisions));
+ fprintf (ps->out, ", %u assumptions", ps->assumptions);
+ fputc (')', ps->out);
+#endif
+ fputc ('\n', ps->out);
+#ifdef STATS
+ fprintf (ps->out,
+ "%s%u static phase decisions (%.1f%% of all variables)\n",
+ ps->prefix,
+ ps->staticphasedecisions, PERCENT (ps->staticphasedecisions, ps->max_var));
+#endif
+ fprintf (ps->out, "%s%u fixed variables\n", ps->prefix, ps->fixed);
+ assert (ps->nonminimizedllits >= ps->minimizedllits);
+#ifndef RCODE
+ redlits = ps->nonminimizedllits - ps->minimizedllits;
+#endif
+ fprintf (ps->out, "%s%u learned literals\n", ps->prefix, ps->llitsadded);
+ fprintf (ps->out, "%s%.1f%% deleted literals\n",
+ ps->prefix, PERCENT (redlits, ps->nonminimizedllits));
+
+#ifdef STATS
+#ifdef TRACE
+ fprintf (ps->out,
+ "%s%llu antecedents (%.1f antecedents per clause",
+ ps->prefix, ps->antecedents, AVERAGE (ps->antecedents, ps->conflicts));
+ if (ps->trace)
+ fprintf (ps->out, ", %.1f bytes/antecedent)", AVERAGE (ps->znts, ps->antecedents));
+ fputs (")\n", ps->out);
+#endif
+
+ fprintf (ps->out, "%s%llu propagations (%.1f propagations per decision)\n",
+ ps->prefix, ps->propagations, AVERAGE (ps->propagations, ps->decisions));
+ fprintf (ps->out, "%s%llu visits (%.1f per propagation)\n",
+ ps->prefix, ps->visits, AVERAGE (ps->visits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu binary clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->bvisits,
+ PERCENT (ps->bvisits, ps->visits),
+ AVERAGE (ps->bvisits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu ternary clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->tvisits,
+ PERCENT (ps->tvisits, ps->visits),
+ AVERAGE (ps->tvisits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu large clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->lvisits,
+ PERCENT (ps->lvisits, ps->visits),
+ AVERAGE (ps->lvisits, ps->propagations));
+ fprintf (ps->out, "%s%llu other true (%.1f%% of visited clauses)\n",
+ ps->prefix, ps->othertrue, PERCENT (ps->othertrue, ps->visits));
+ fprintf (ps->out,
+ "%s%llu other true in binary clauses (%.1f%%)"
+ ", %llu upper (%.1f%%)\n",
+ ps->prefix, ps->othertrue2, PERCENT (ps->othertrue2, ps->othertrue),
+ ps->othertrue2u, PERCENT (ps->othertrue2u, ps->othertrue2));
+ fprintf (ps->out,
+ "%s%llu other true in large clauses (%.1f%%)"
+ ", %llu upper (%.1f%%)\n",
+ ps->prefix, ps->othertruel, PERCENT (ps->othertruel, ps->othertrue),
+ ps->othertruelu, PERCENT (ps->othertruelu, ps->othertruel));
+ fprintf (ps->out, "%s%llu ternary and large traversals (%.1f per visit)\n",
+ ps->prefix, ps->traversals, AVERAGE (ps->traversals, ps->visits));
+ fprintf (ps->out, "%s%llu large traversals (%.1f per large visit)\n",
+ ps->prefix, ps->ltraversals, AVERAGE (ps->ltraversals, ps->lvisits));
+ fprintf (ps->out, "%s%llu assignments\n", ps->prefix, ps->assignments);
+#else
+ fprintf (ps->out, "%s%llu propagations\n", ps->prefix, picosat_propagations (ps));
+ fprintf (ps->out, "%s%llu visits\n", ps->prefix, picosat_visits (ps));
+#endif
+ fprintf (ps->out, "%s%.1f%% variables used\n", ps->prefix, PERCENT (ps->vused, ps->max_var));
+
+ sflush (ps);
+ fprintf (ps->out, "%s%.1f seconds in library\n", ps->prefix, ps->seconds);
+ fprintf (ps->out, "%s%.1f megaprops/second\n",
+ ps->prefix, AVERAGE (ps->propagations / 1e6f, ps->seconds));
+ fprintf (ps->out, "%s%.1f megavisits/second\n",
+ ps->prefix, AVERAGE (ps->visits / 1e6f, ps->seconds));
+ fprintf (ps->out, "%sprobing %.1f seconds %.0f%%\n",
+ ps->prefix, ps->flseconds, PERCENT (ps->flseconds, ps->seconds));
+#ifdef STATS
+ fprintf (ps->out,
+ "%srecycled %.1f MB in %u reductions\n",
+ ps->prefix, ps->rrecycled / (double) (1 << 20), ps->reductions);
+ fprintf (ps->out,
+ "%srecycled %.1f MB in %u simplifications\n",
+ ps->prefix, ps->srecycled / (double) (1 << 20), ps->simps);
+#else
+ fprintf (ps->out, "%s%u simplifications\n", ps->prefix, ps->simps);
+ fprintf (ps->out, "%s%u reductions\n", ps->prefix, ps->reductions);
+ fprintf (ps->out, "%s%.1f MB recycled\n", ps->prefix, ps->recycled / (double) (1 << 20));
+#endif
+ fprintf (ps->out, "%s%.1f MB maximally allocated\n",
+ ps->prefix, picosat_max_bytes_allocated (ps) / (double) (1 << 20));
+}
+
+#ifndef NGETRUSAGE
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/unistd.h>
+#endif
+
+double
+picosat_time_stamp (void)
+{
+ double res = -1;
+#ifndef NGETRUSAGE
+ struct rusage u;
+ res = 0;
+ if (!getrusage (RUSAGE_SELF, &u))
+ {
+ res += u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec;
+ res += u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
+ }
+#endif
+ return res;
+}
+
+double
+picosat_seconds (PS * ps)
+{
+ check_ready (ps);
+ return ps->seconds;
+}
+
+void
+picosat_print (PS * ps, FILE * file)
+{
+#ifdef NO_BINARY_CLAUSES
+ Lit * lit, *other, * last;
+ Ltk * stack;
+#endif
+ Lit **q, **eol;
+ Cls **p, *c;
+ unsigned n;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ n = 0;
+ n += ps->alshead - ps->als;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+ n++;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ last = int2lit (ps, -ps->max_var);
+ for (lit = int2lit (ps, 1); lit <= last; lit++)
+ {
+ stack = LIT2IMPLS (lit);
+ eol = stack->start + stack->count;
+ for (q = stack->start; q < eol; q++)
+ if (*q >= lit)
+ n++;
+ }
+#endif
+
+ fprintf (file, "p cnf %d %u\n", ps->max_var, n);
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ fprintf (file, "%d ", LIT2INT (*q));
+
+ fputs ("0\n", file);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ last = int2lit (ps, -ps->max_var);
+ for (lit = int2lit (ps, 1); lit <= last; lit++)
+ {
+ stack = LIT2IMPLS (lit);
+ eol = stack->start + stack->count;
+ for (q = stack->start; q < eol; q++)
+ if ((other = *q) >= lit)
+ fprintf (file, "%d %d 0\n", LIT2INT (lit), LIT2INT (other));
+ }
+#endif
+
+ {
+ Lit **r;
+ for (r = ps->als; r < ps->alshead; r++)
+ fprintf (file, "%d 0\n", LIT2INT (*r));
+ }
+
+ fflush (file);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+}
+
+void
+picosat_enter (PS * ps)
+{
+ enter (ps);
+}
+
+void
+picosat_leave (PS * ps)
+{
+ leave (ps);
+}
+
+void
+picosat_message (PS * ps, int vlevel, const char * fmt, ...)
+{
+ va_list ap;
+
+ if (vlevel > ps->verbosity)
+ return;
+
+ fputs (ps->prefix, ps->out);
+ va_start (ap, fmt);
+ vfprintf (ps->out, fmt, ap);
+ va_end (ap);
+ fputc ('\n', ps->out);
+}
+
+int
+picosat_changed (PS * ps)
+{
+ int res;
+
+ check_ready (ps);
+ check_sat_state (ps);
+
+ res = (ps->min_flipped <= ps->saved_max_var);
+ assert (!res || ps->saved_flips != ps->flips);
+
+ return res;
+}
+
+void
+picosat_reset_phases (PS * ps)
+{
+ rebias (ps);
+}
+
+void
+picosat_reset_scores (PS * ps)
+{
+ Rnk * r;
+ ps->hhead = ps->heap + 1;
+ for (r = ps->rnks + 1; r <= ps->rnks + ps->max_var; r++)
+ {
+ CLR (r);
+ hpush (ps, r);
+ }
+}
+
+void
+picosat_remove_learned (PS * ps, unsigned percentage)
+{
+ enter (ps);
+ reset_incremental_usage (ps);
+ reduce (ps, percentage);
+ leave (ps);
+}
+
+void
+picosat_set_global_default_phase (PS * ps, int phase)
+{
+ check_ready (ps);
+ ABORTIF (phase < 0, "API usage: 'picosat_set_global_default_phase' "
+ "with negative argument");
+ ABORTIF (phase > 3, "API usage: 'picosat_set_global_default_phase' "
+ "with argument > 3");
+ ps->defaultphase = phase;
+}
+
+void
+picosat_set_default_phase_lit (PS * ps, int int_lit, int phase)
+{
+ unsigned newphase;
+ Lit * lit;
+ Var * v;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+
+ if (phase)
+ {
+ newphase = (int_lit < 0) == (phase < 0);
+ v->defphase = v->phase = newphase;
+ v->usedefphase = v->assigned = 1;
+ }
+ else
+ {
+ v->usedefphase = v->assigned = 0;
+ }
+}
+
+void
+picosat_set_more_important_lit (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ Rnk * r;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ r = VAR2RNK (v);
+
+ ABORTIF (r->lessimportant, "can not mark variable more and less important");
+
+ if (r->moreimportant)
+ return;
+
+ r->moreimportant = 1;
+
+ if (r->pos)
+ hup (ps, r);
+}
+
+void
+picosat_set_less_important_lit (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ Rnk * r;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ r = VAR2RNK (v);
+
+ ABORTIF (r->moreimportant, "can not mark variable more and less important");
+
+ if (r->lessimportant)
+ return;
+
+ r->lessimportant = 1;
+
+ if (r->pos)
+ hdown (ps, r);
+}
+
+#ifndef NADC
+
+unsigned
+picosat_ado_conflicts (PS * ps)
+{
+ check_ready (ps);
+ return ps->adoconflicts;
+}
+
+void
+picosat_disable_ado (PS * ps)
+{
+ check_ready (ps);
+ assert (!ps->adodisabled);
+ ps->adodisabled = 1;
+}
+
+void
+picosat_enable_ado (PS * ps)
+{
+ check_ready (ps);
+ assert (ps->adodisabled);
+ ps->adodisabled = 0;
+}
+
+void
+picosat_set_ado_conflict_limit (PS * ps, unsigned newadoconflictlimit)
+{
+ check_ready (ps);
+ ps->adoconflictlimit = newadoconflictlimit;
+}
+
+#endif
+
+void
+picosat_simplify (PS * ps)
+{
+ enter (ps);
+ reset_incremental_usage (ps);
+ simplify (ps, 1);
+ leave (ps);
+}
+
+int
+picosat_haveados (void)
+{
+#ifndef NADC
+ return 1;
+#else
+ return 0;
+#endif
+}
+
+void
+picosat_save_original_clauses (PS * ps)
+{
+ if (ps->saveorig) return;
+ ABORTIF (ps->oadded, "API usage: 'picosat_save_original_clauses' too late");
+ ps->saveorig = 1;
+}
+
+void picosat_set_interrupt (PicoSAT * ps,
+ void * external_state,
+ int (*interrupted)(void * external_state))
+{
+ ps->interrupt.state = external_state;
+ ps->interrupt.function = interrupted;
+}
+
+int
+picosat_deref_partial (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ check_sat_state (ps);
+ ABORTIF (!int_lit, "API usage: can not partial deref zero literal");
+ ABORTIF (ps->mtcls, "API usage: deref partial after empty clause generated");
+ ABORTIF (!ps->saveorig, "API usage: 'picosat_save_original_clauses' missing");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+
+ if (!ps->partial)
+ minautarky (ps);
+
+ return pderef (ps, int_lit);
+}
diff --git a/test/monniaux/picosat-965/picosat.h b/test/monniaux/picosat-965/picosat.h
new file mode 100644
index 00000000..e735aec9
--- /dev/null
+++ b/test/monniaux/picosat-965/picosat.h
@@ -0,0 +1,660 @@
+/****************************************************************************
+Copyright (c) 2006 - 2015, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#ifndef picosat_h_INCLUDED
+#define picosat_h_INCLUDED
+
+#define INT_MOD(x, y) ((x) % (y))
+
+/*------------------------------------------------------------------------*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stddef.h>
+
+/*------------------------------------------------------------------------*/
+/* The following macros allows for users to distiguish between different
+ * versions of the API. The first 'PICOSAT_REENTRANT_API' is defined for
+ * the new reentrant API which allows to generate multiple instances of
+ * PicoSAT in one process. The second 'PICOSAT_API_VERSION' defines the
+ * (smallest) version of PicoSAT to which this API conforms.
+ */
+#define PICOSAT_REENTRANT_API
+#define PICOSAT_API_VERSION 953 /* API version */
+
+/*------------------------------------------------------------------------*/
+/* These are the return values for 'picosat_sat' as for instance
+ * standardized by the output format of the SAT competition.
+ */
+#define PICOSAT_UNKNOWN 0
+#define PICOSAT_SATISFIABLE 10
+#define PICOSAT_UNSATISFIABLE 20
+
+/*------------------------------------------------------------------------*/
+
+typedef struct PicoSAT PicoSAT;
+
+/*------------------------------------------------------------------------*/
+
+const char *picosat_version (void);
+const char *picosat_config (void);
+const char *picosat_copyright (void);
+
+/*------------------------------------------------------------------------*/
+/* You can make PicoSAT use an external memory manager instead of the one
+ * provided by LIBC. But then you need to call these three function before
+ * 'picosat_init'. The memory manager functions here all have an additional
+ * first argument which is a pointer to the memory manager, but otherwise
+ * are supposed to work as their LIBC counter parts 'malloc', 'realloc' and
+ * 'free'. As exception the 'resize' and 'delete' function have as third
+ * argument the number of bytes of the block given as second argument.
+ */
+
+typedef void * (*picosat_malloc)(void *, size_t);
+typedef void * (*picosat_realloc)(void*, void *, size_t, size_t);
+typedef void (*picosat_free)(void*, void*, size_t);
+
+/*------------------------------------------------------------------------*/
+
+PicoSAT * picosat_init (void); /* constructor */
+
+PicoSAT * picosat_minit (void * state,
+ picosat_malloc,
+ picosat_realloc,
+ picosat_free);
+
+void picosat_reset (PicoSAT *); /* destructor */
+
+/*------------------------------------------------------------------------*/
+/* The following five functions are essentially parameters to 'init', and
+ * thus should be called right after 'picosat_init' before doing anything
+ * else. You should not call any of them after adding a literal.
+ */
+
+/* Set output file, default is 'stdout'.
+ */
+void picosat_set_output (PicoSAT *, FILE *);
+
+/* Measure all time spent in all calls in the solver. By default only the
+ * time spent in 'picosat_sat' is measured. Enabling this function might
+ * for instance triple the time needed to add large CNFs, since every call
+ * to 'picosat_add' will trigger a call to 'getrusage'.
+ */
+void picosat_measure_all_calls (PicoSAT *);
+
+/* Set the prefix used for printing verbose messages and statistics.
+ * Default is "c ".
+ */
+void picosat_set_prefix (PicoSAT *, const char *);
+
+/* Set verbosity level. A verbosity level of 1 and above prints more and
+ * more detailed progress reports on the output file, set by
+ * 'picosat_set_output'. Verbose messages are prefixed with the string set
+ * by 'picosat_set_prefix'.
+ */
+void picosat_set_verbosity (PicoSAT *, int new_verbosity_level);
+
+/* Disable/Enable all pre-processing, currently only failed literal probing.
+ *
+ * new_plain_value != 0 only 'plain' solving, so no preprocessing
+ * new_plain_value == 0 allow preprocessing
+ */
+void picosat_set_plain (PicoSAT *, int new_plain_value);
+
+/* Set default initial phase:
+ *
+ * 0 = false
+ * 1 = true
+ * 2 = Jeroslow-Wang (default)
+ * 3 = random initial phase
+ *
+ * After a variable has been assigned the first time, it will always
+ * be assigned the previous value if it is picked as decision variable.
+ * The initial assignment can be chosen with this function.
+ */
+void picosat_set_global_default_phase (PicoSAT *, int);
+
+/* Set next/initial phase of a particular variable if picked as decision
+ * variable. Second argument 'phase' has the following meaning:
+ *
+ * negative = next value if picked as decision variable is false
+ *
+ * positive = next value if picked as decision variable is true
+ *
+ * 0 = use global default phase as next value and
+ * assume 'lit' was never assigned
+ *
+ * Again if 'lit' is assigned afterwards through a forced assignment,
+ * then this forced assignment is the next phase if this variable is
+ * used as decision variable.
+ */
+void picosat_set_default_phase_lit (PicoSAT *, int lit, int phase);
+
+/* You can reset all phases by the following function.
+ */
+void picosat_reset_phases (PicoSAT *);
+
+/* Scores can be erased as well. Note, however, that even after erasing
+ * scores and phases, learned clauses are kept. In addition head tail
+ * pointers for literals are not moved either. So expect a difference
+ * between calling the solver in incremental mode or with a fresh copy of
+ * the CNF.
+ */
+void picosat_reset_scores (PicoSAT *);
+
+/* Reset assignment if in SAT state and then remove the given percentage of
+ * less active (large) learned clauses. If you specify 100% all large
+ * learned clauses are removed.
+ */
+void picosat_remove_learned (PicoSAT *, unsigned percentage);
+
+/* Set some variables to be more important than others. These variables are
+ * always used as decisions before other variables are used. Dually there
+ * is a set of variables that is used last. The default is
+ * to mark all variables as being indifferent only.
+ */
+void picosat_set_more_important_lit (PicoSAT *, int lit);
+void picosat_set_less_important_lit (PicoSAT *, int lit);
+
+/* Allows to print to internal 'out' file from client.
+ */
+void picosat_message (PicoSAT *, int verbosity_level, const char * fmt, ...);
+
+/* Set a seed for the random number generator. The random number generator
+ * is currently just used for generating random decisions. In our
+ * experiments having random decisions did not really help on industrial
+ * examples, but was rather helpful to randomize the solver in order to
+ * do proper benchmarking of different internal parameter sets.
+ */
+void picosat_set_seed (PicoSAT *, unsigned random_number_generator_seed);
+
+/* If you ever want to extract cores or proof traces with the current
+ * instance of PicoSAT initialized with 'picosat_init', then make sure to
+ * call 'picosat_enable_trace_generation' right after 'picosat_init'. This
+ * is not necessary if you only use 'picosat_set_incremental_rup_file'.
+ *
+ * NOTE, trace generation code is not necessarily included, e.g. if you
+ * configure PicoSAT with full optimzation as './configure.sh -O' or with
+
+ * you do not get any results by trying to generate traces.
+ *
+ * The return value is non-zero if code for generating traces is included
+ * and it is zero if traces can not be generated.
+ */
+int picosat_enable_trace_generation (PicoSAT *);
+
+/* You can dump proof traces in RUP format incrementally even without
+ * keeping the proof trace in memory. The advantage is a reduction of
+ * memory usage, but the dumped clauses do not necessarily belong to the
+ * clausal core. Beside the file the additional parameters denotes the
+ * maximal number of variables and the number of original clauses.
+ */
+void picosat_set_incremental_rup_file (PicoSAT *, FILE * file, int m, int n);
+
+/* Save original clauses for 'picosat_deref_partial'. See comments to that
+ * function further down.
+ */
+void picosat_save_original_clauses (PicoSAT *);
+
+/* Add a call back which is checked regularly to notify the SAT solver
+ * to terminate earlier. This is useful for setting external time limits
+ * or terminate early in say a portfolio style parallel SAT solver.
+ */
+void picosat_set_interrupt (PicoSAT *,
+ void * external_state,
+ int (*interrupted)(void * external_state));
+
+/*------------------------------------------------------------------------*/
+/* This function returns the next available unused variable index and
+ * allocates a variable for it even though this variable does not occur as
+ * assumption, nor in a clause or any other constraints. In future calls to
+ * 'picosat_sat', 'picosat_deref' and particularly for 'picosat_changed',
+ * this variable is treated as if it had been used.
+ */
+int picosat_inc_max_var (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* Push and pop semantics for PicoSAT. 'picosat_push' opens up a new
+ * context. All clauses added in this context are attached to it and
+ * discarded when the context is closed with 'picosat_pop'. It is also
+ * possible to nest contexts.
+ *
+ * The current implementation uses a new internal variable for each context.
+ * However, the indices for these internal variables are shared with
+ * ordinary external variables. This means that after any call to
+ * 'picosat_push', new variable indices should be obtained with
+ * 'picosat_inc_max_var' and not just by incrementing the largest variable
+ * index used so far.
+ *
+ * The return value is the index of the literal that assumes this context.
+ * This literal can only be used for 'picosat_failed_context' otherwise
+ * it will lead to an API usage error.
+ */
+int picosat_push (PicoSAT *);
+
+/* This is as 'picosat_failed_assumption', but only for internal variables
+ * generated by 'picosat_push'.
+ */
+int picosat_failed_context (PicoSAT *, int lit);
+
+/* Returns the literal that assumes the current context or zero if the
+ * outer context has been reached.
+ */
+int picosat_context (PicoSAT *);
+
+/* Closes the current context and recycles the literal generated for
+ * assuming this context. The return value is the literal for the new
+ * outer context or zero if the outer most context has been reached.
+ */
+int picosat_pop (PicoSAT *);
+
+/* Force immmediate removal of all satisfied clauses and clauses that are
+ * added or generated in closed contexts. This function is called
+ * internally if enough units are learned or after a certain number of
+ * contexts have been closed. This number is fixed at compile time
+ * and defined as MAXCILS in 'picosat.c'.
+ *
+ * Note that learned clauses which only involve outer contexts are kept.
+ */
+void picosat_simplify (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* If you know a good estimate on how many variables you are going to use
+ * then calling this function before adding literals will result in less
+ * resizing of the variable table. But this is just a minor optimization.
+ * Beside exactly allocating enough variables it has the same effect as
+ * calling 'picosat_inc_max_var'.
+ */
+void picosat_adjust (PicoSAT *, int max_idx);
+
+/*------------------------------------------------------------------------*/
+/* Statistics.
+ */
+int picosat_variables (PicoSAT *); /* p cnf <m> n */
+int picosat_added_original_clauses (PicoSAT *); /* p cnf m <n> */
+size_t picosat_max_bytes_allocated (PicoSAT *);
+double picosat_time_stamp (void); /* ... in process */
+void picosat_stats (PicoSAT *); /* > output file */
+unsigned long long picosat_propagations (PicoSAT *); /* #propagations */
+unsigned long long picosat_decisions (PicoSAT *); /* #decisions */
+unsigned long long picosat_visits (PicoSAT *); /* #visits */
+
+/* The time spent in calls to the library or in 'picosat_sat' respectively.
+ * The former is returned if, right after initialization
+ * 'picosat_measure_all_calls' is called.
+ */
+double picosat_seconds (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* Add a literal of the next clause. A zero terminates the clause. The
+ * solver is incremental. Adding a new literal will reset the previous
+ * assignment. The return value is the original clause index to which
+ * this literal respectively the trailing zero belong starting at 0.
+ */
+int picosat_add (PicoSAT *, int lit);
+
+/* As the previous function, but allows to add a full clause at once with an
+ * at compiled time known size. The list of argument literals has to be
+ * terminated with a zero literal. Literals beyond the first zero literal
+ * are discarded.
+ */
+int picosat_add_arg (PicoSAT *, ...);
+
+/* As the previous function but with an at compile time unknown size.
+ */
+int picosat_add_lits (PicoSAT *, int * lits);
+
+/* Print the CNF to the given file in DIMACS format.
+ */
+void picosat_print (PicoSAT *, FILE *);
+
+/* You can add arbitrary many assumptions before the next 'picosat_sat'
+ * call. This is similar to the using assumptions in MiniSAT, except that
+ * for PicoSAT you do not have to collect all your assumptions in a vector
+ * yourself. In PicoSAT you can add one after the other, to be used in the
+ * next call to 'picosat_sat'.
+ *
+ * These assumptions can be interpreted as adding unit clauses with those
+ * assumptions as literals. However these assumption clauses are only valid
+ * for exactly the next call to 'picosat_sat', and will be removed
+ * afterwards, e.g. in following future calls to 'picosat_sat' after the
+ * next 'picosat_sat' call, unless they are assumed again trough
+ * 'picosat_assume'.
+ *
+ * More precisely, assumptions actually remain valid even after the next
+ * call to 'picosat_sat' has returned. Valid means they remain 'assumed'
+ * internally until a call to 'picosat_add', 'picosat_assume', or a second
+ * 'picosat_sat', following the first 'picosat_sat'. The reason for keeping
+ * them valid is to allow 'picosat_failed_assumption' to return correct
+ * values.
+ *
+ * Example:
+ *
+ * picosat_assume (1); // assume unit clause '1 0'
+ * picosat_assume (-2); // additionally assume clause '-2 0'
+ * res = picosat_sat (1000); // assumes 1 and -2 to hold
+ * // 1000 decisions max.
+ *
+ * if (res == PICOSAT_UNSATISFIABLE)
+ * {
+ * if (picosat_failed_assumption (1))
+ * // unit clause '1 0' was necessary to derive UNSAT
+ *
+ * if (picosat_failed_assumption (-2))
+ * // unit clause '-2 0' was necessary to derive UNSAT
+ *
+ * // at least one but also both could be necessary
+ *
+ * picosat_assume (17); // previous assumptions are removed
+ * // now assume unit clause '17 0' for
+ * // the next call to 'picosat_sat'
+ *
+ * // adding a new clause, actually the first literal of
+ * // a clause would also make the assumptions used in the previous
+ * // call to 'picosat_sat' invalid.
+ *
+ * // The first two assumptions above are not assumed anymore. Only
+ * // the assumptions, since the last call to 'picosat_sat' returned
+ * // are assumed, e.g. the unit clause '17 0'.
+ *
+ * res = picosat_sat (-1);
+ * }
+ * else if (res == PICOSAT_SATISFIABLE)
+ * {
+ * // now the assignment is valid and we can call 'picosat_deref'
+ *
+ * assert (picosat_deref (1) == 1));
+ * assert (picosat_deref (-2) == 1));
+ *
+ * val = picosat_deref (15);
+ *
+ * // previous two assumptions are still valid
+ *
+ * // would become invalid if 'picosat_add' or 'picosat_assume' is
+ * // called here, but we immediately call 'picosat_sat'. Now when
+ * // entering 'picosat_sat' the solver knows that the previous call
+ * // returned SAT and it can safely reset the previous assumptions
+ *
+ * res = picosat_sat (-1);
+ * }
+ * else
+ * {
+ * assert (res == PICOSAT_UNKNOWN);
+ *
+ * // assumptions valid, but assignment invalid
+ * // except for top level assigned literals which
+ * // necessarily need to have this value if the formula is SAT
+ *
+ * // as above the solver nows that the previous call returned UNKWOWN
+ * // and will before doing anything else reset assumptions
+ *
+ * picosat_sat (-1);
+ * }
+ */
+void picosat_assume (PicoSAT *, int lit);
+
+/*------------------------------------------------------------------------*/
+/* This is an experimental feature for handling 'all different constraints'
+ * (ADC). Currently only one global ADC can be handled. The bit-width of
+ * all the bit-vectors entered in this ADC (stored in 'all different
+ * objects' or ADOs) has to be identical.
+ *
+ * TODO: also handle top level assigned literals here.
+ */
+void picosat_add_ado_lit (PicoSAT *, int);
+
+/*------------------------------------------------------------------------*/
+/* Call the main SAT routine. A negative decision limit sets no limit on
+ * the number of decisions. The return values are as above, e.g.
+ * 'PICOSAT_UNSATISFIABLE', 'PICOSAT_SATISFIABLE', or 'PICOSAT_UNKNOWN'.
+ */
+int picosat_sat (PicoSAT *, int decision_limit);
+
+/* As alternative to a decision limit you can use the number of propagations
+ * as limit. This is more linearly related to execution time. This has to
+ * be called after 'picosat_init' and before 'picosat_sat'.
+ */
+void picosat_set_propagation_limit (PicoSAT *, unsigned long long limit);
+
+/* Return last result of calling 'picosat_sat' or '0' if not called.
+ */
+int picosat_res (PicoSAT *);
+
+/* After 'picosat_sat' was called and returned 'PICOSAT_SATISFIABLE', then
+ * the satisfying assignment can be obtained by 'dereferencing' literals.
+ * The value of the literal is return as '1' for 'true', '-1' for 'false'
+ * and '0' for an unknown value.
+ */
+int picosat_deref (PicoSAT *, int lit);
+
+/* Same as before but just returns true resp. false if the literals is
+ * forced to this assignment at the top level. This function does not
+ * require that 'picosat_sat' was called and also does not internally reset
+ * incremental usage.
+ */
+int picosat_deref_toplevel (PicoSAT *, int lit);
+
+/* After 'picosat_sat' was called and returned 'PICOSAT_SATISFIABLE' a
+ * partial satisfying assignment can be obtained as well. It satisfies all
+ * original clauses. The value of the literal is return as '1' for 'true',
+ * '-1' for 'false' and '0' for an unknown value. In order to make this
+ * work all original clauses have to be saved internally, which has to be
+ * enabled by 'picosat_save_original_clauses' right after initialization.
+ */
+int picosat_deref_partial (PicoSAT *, int lit);
+
+/* Returns non zero if the CNF is unsatisfiable because an empty clause was
+ * added or derived.
+ */
+int picosat_inconsistent (PicoSAT *);
+
+/* Returns non zero if the literal is a failed assumption, which is defined
+ * as an assumption used to derive unsatisfiability. This is as accurate as
+ * generating core literals, but still of course is an overapproximation of
+ * the set of assumptions really necessary. The technique does not need
+ * clausal core generation nor tracing to be enabled and thus can be much
+ * more effective. The function can only be called as long the current
+ * assumptions are valid. See 'picosat_assume' for more details.
+ */
+int picosat_failed_assumption (PicoSAT *, int lit);
+
+/* Returns a zero terminated list of failed assumption in the last call to
+ * 'picosat_sat'. The pointer is valid until the next call to
+ * 'picosat_sat' or 'picosat_failed_assumptions'. It only makes sense if the
+ * last call to 'picosat_sat' returned 'PICOSAT_UNSATISFIABLE'.
+ */
+const int * picosat_failed_assumptions (PicoSAT *);
+
+/* Returns a zero terminated minimized list of failed assumption for the last
+ * call to 'picosat_sat'. The pointer is valid until the next call to this
+ * function or 'picosat_sat' or 'picosat_mus_assumptions'. It only makes sense
+ * if the last call to 'picosat_sat' returned 'PICOSAT_UNSATISFIABLE'.
+ *
+ * The call back function is called for all successful simplification
+ * attempts. The first argument of the call back function is the state
+ * given as first argument to 'picosat_mus_assumptions'. The second
+ * argument to the call back function is the new reduced list of failed
+ * assumptions.
+ *
+ * This function will call 'picosat_assume' and 'picosat_sat' internally but
+ * before returning reestablish a proper UNSAT state, e.g.
+ * 'picosat_failed_assumption' will work afterwards as expected.
+ *
+ * The last argument if non zero fixes assumptions. In particular, if an
+ * assumption can not be removed it is permanently assigned true, otherwise
+ * if it turns out to be redundant it is permanently assumed to be false.
+ */
+const int * picosat_mus_assumptions (PicoSAT *, void *,
+ void(*)(void*,const int*),int);
+
+/* Compute one maximal subset of satisfiable assumptions. You need to set
+ * the assumptions, call 'picosat_sat' and check for 'picosat_inconsistent',
+ * before calling this function. The result is a zero terminated array of
+ * assumptions that consistently can be asserted at the same time. Before
+ * returing the library 'reassumes' all assumptions.
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This can speed up the computation.
+ */
+const int * picosat_maximal_satisfiable_subset_of_assumptions (PicoSAT *);
+
+/* This function assumes that you have set up all assumptions with
+ * 'picosat_assume'. Then it calls 'picosat_sat' internally unless the
+ * formula is already inconsistent without assumptions, i.e. it contains
+ * the empty clause. After that it extracts a maximal satisfiable subset of
+ * assumptions.
+ *
+ * The result is a zero terminated maximal subset of consistent assumptions
+ * or a zero pointer if the formula contains the empty clause and thus no
+ * more maximal consistent subsets of assumptions can be extracted. In the
+ * first case, before returning, a blocking clause is added, that rules out
+ * the result for the next call.
+ *
+ * NOTE: adding the blocking clause changes the CNF.
+ *
+ * So the following idiom
+ *
+ * const int * mss;
+ * picosat_assume (a1);
+ * picosat_assume (a2);
+ * picosat_assume (a3);
+ * picosat_assume (a4);
+ * while ((mss = picosat_next_maximal_satisfiable_subset_of_assumptions ()))
+ * process_mss (mss);
+ *
+ * can be used to iterate over all maximal consistent subsets of
+ * the set of assumptions {a1,a2,a3,a4}.
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This might speed up the computation.
+ */
+const int *
+picosat_next_maximal_satisfiable_subset_of_assumptions (PicoSAT *);
+
+/* Similarly we can iterate over all minimal correcting assumption sets.
+ * See the CAMUS literature [M. Liffiton, K. Sakallah JAR 2008].
+ *
+ * The result contains each assumed literal only once, even if it
+ * was assumed multiple times (in contrast to the maximal consistent
+ * subset functions above).
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This might speed up the computation.
+ */
+const int *
+picosat_next_minimal_correcting_subset_of_assumptions (PicoSAT *);
+
+/* Compute the union of all minmal correcting sets, which is called
+ * the 'high level union of all minimal unsatisfiable subset sets'
+ * or 'HUMUS' in our papers.
+ *
+ * It uses 'picosat_next_minimal_correcting_subset_of_assumptions' and
+ * the same notes and advices apply. In particular, this implies that
+ * after calling the function once, the current CNF becomes inconsistent,
+ * and PicoSAT has to be reset. So even this function internally uses
+ * PicoSAT incrementally, it can not be used incrementally itself at this
+ * point.
+ *
+ * The 'callback' can be used for progress logging and is called after
+ * each extracted minimal correcting set if non zero. The 'nhumus'
+ * parameter of 'callback' denotes the number of assumptions found to be
+ * part of the HUMUS sofar.
+ */
+const int *
+picosat_humus (PicoSAT *,
+ void (*callback)(void * state, int nmcs, int nhumus),
+ void * state);
+
+/*------------------------------------------------------------------------*/
+/* Assume that a previous call to 'picosat_sat' in incremental usage,
+ * returned 'SATISFIABLE'. Then a couple of clauses and optionally new
+ * variables were added (a new variable is a variable that has an index
+ * larger then the maximum variable added so far). The next call to
+ * 'picosat_sat' also returns 'SATISFIABLE'. If this function
+ * 'picosat_changed' returns '0', then the assignment to the old variables
+ * is guaranteed to not have changed. Otherwise it might have changed.
+ *
+ * The return value to this function is only valid until new clauses are
+ * added through 'picosat_add', an assumption is made through
+ * 'picosat_assume', or again 'picosat_sat' is called. This is the same
+ * assumption as for 'picosat_deref'.
+ *
+ * TODO currently this function might also return a non zero value even if
+ * the old assignment did not change, because it only checks whether the
+ * assignment of at least one old variable was flipped at least once during
+ * the search. In principle it should be possible to be exact in the other
+ * direction as well by using a counter of variables that have an odd number
+ * of flips. But this is not implemented yet.
+ */
+int picosat_changed (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* The following six functions internally extract the variable and clausal
+ * core and thus require trace generation to be enabled with
+ * 'picosat_enable_trace_generation' right after calling 'picosat_init'.
+ *
+ * TODO: using these functions in incremental mode with failed assumptions
+ * has only been tested for 'picosat_corelit' thoroughly. The others
+ * probably only work in non-incremental mode or without using
+ * 'picosat_assume'.
+ */
+
+/* This function determines whether the i'th added original clause is in the
+ * core. The 'i' is the return value of 'picosat_add', which starts at zero
+ * and is incremented by one after a original clause is added (that is after
+ * 'picosat_add (0)'). For the index 'i' the following has to hold:
+ *
+ * 0 <= i < picosat_added_original_clauses ()
+ */
+int picosat_coreclause (PicoSAT *, int i);
+
+/* This function gives access to the variable core, which is made up of the
+ * variables that were resolved in deriving the empty clause.
+ */
+int picosat_corelit (PicoSAT *, int lit);
+
+/* Write the clauses that were used in deriving the empty clause to a file
+ * in DIMACS format.
+ */
+void picosat_write_clausal_core (PicoSAT *, FILE * core_file);
+
+/* Write a proof trace in TraceCheck format to a file.
+ */
+void picosat_write_compact_trace (PicoSAT *, FILE * trace_file);
+void picosat_write_extended_trace (PicoSAT *, FILE * trace_file);
+
+/* Write a RUP trace to a file. This trace file contains only the learned
+ * core clauses while this is not necessarily the case for the RUP file
+ * obtained with 'picosat_set_incremental_rup_file'.
+ */
+void picosat_write_rup_trace (PicoSAT *, FILE * trace_file);
+
+/*------------------------------------------------------------------------*/
+/* Keeping the proof trace around is not necessary if an over-approximation
+ * of the core is enough. A literal is 'used' if it was involved in a
+ * resolution to derive a learned clause. The core literals are necessarily
+ * a subset of the 'used' literals.
+ */
+
+int picosat_usedlit (PicoSAT *, int lit);
+/*------------------------------------------------------------------------*/
+#endif
diff --git a/test/monniaux/picosat-965/small.dat b/test/monniaux/picosat-965/small.dat
new file mode 100644
index 00000000..accb9054
--- /dev/null
+++ b/test/monniaux/picosat-965/small.dat
@@ -0,0 +1,2 @@
+p cnf 1 1
+1 0
diff --git a/test/monniaux/picosat-965/sudoku.sat b/test/monniaux/picosat-965/sudoku.sat
new file mode 100644
index 00000000..23effaed
--- /dev/null
+++ b/test/monniaux/picosat-965/sudoku.sat
@@ -0,0 +1,20519 @@
+c sudoku
+p cnf 729 20517
+-1 -2 0
+-1 -3 0
+-1 -4 0
+-1 -5 0
+-1 -6 0
+-1 -7 0
+-1 -8 0
+-1 -9 0
+-2 -1 0
+-2 -3 0
+-2 -4 0
+-2 -5 0
+-2 -6 0
+-2 -7 0
+-2 -8 0
+-2 -9 0
+-3 -1 0
+-3 -2 0
+-3 -4 0
+-3 -5 0
+-3 -6 0
+-3 -7 0
+-3 -8 0
+-3 -9 0
+-4 -1 0
+-4 -2 0
+-4 -3 0
+-4 -5 0
+-4 -6 0
+-4 -7 0
+-4 -8 0
+-4 -9 0
+-5 -1 0
+-5 -2 0
+-5 -3 0
+-5 -4 0
+-5 -6 0
+-5 -7 0
+-5 -8 0
+-5 -9 0
+-6 -1 0
+-6 -2 0
+-6 -3 0
+-6 -4 0
+-6 -5 0
+-6 -7 0
+-6 -8 0
+-6 -9 0
+-7 -1 0
+-7 -2 0
+-7 -3 0
+-7 -4 0
+-7 -5 0
+-7 -6 0
+-7 -8 0
+-7 -9 0
+-8 -1 0
+-8 -2 0
+-8 -3 0
+-8 -4 0
+-8 -5 0
+-8 -6 0
+-8 -7 0
+-8 -9 0
+-9 -1 0
+-9 -2 0
+-9 -3 0
+-9 -4 0
+-9 -5 0
+-9 -6 0
+-9 -7 0
+-9 -8 0
+1 2 3 4 5 6 7 8 9 0
+-10 -11 0
+-10 -12 0
+-10 -13 0
+-10 -14 0
+-10 -15 0
+-10 -16 0
+-10 -17 0
+-10 -18 0
+-11 -10 0
+-11 -12 0
+-11 -13 0
+-11 -14 0
+-11 -15 0
+-11 -16 0
+-11 -17 0
+-11 -18 0
+-12 -10 0
+-12 -11 0
+-12 -13 0
+-12 -14 0
+-12 -15 0
+-12 -16 0
+-12 -17 0
+-12 -18 0
+-13 -10 0
+-13 -11 0
+-13 -12 0
+-13 -14 0
+-13 -15 0
+-13 -16 0
+-13 -17 0
+-13 -18 0
+-14 -10 0
+-14 -11 0
+-14 -12 0
+-14 -13 0
+-14 -15 0
+-14 -16 0
+-14 -17 0
+-14 -18 0
+-15 -10 0
+-15 -11 0
+-15 -12 0
+-15 -13 0
+-15 -14 0
+-15 -16 0
+-15 -17 0
+-15 -18 0
+-16 -10 0
+-16 -11 0
+-16 -12 0
+-16 -13 0
+-16 -14 0
+-16 -15 0
+-16 -17 0
+-16 -18 0
+-17 -10 0
+-17 -11 0
+-17 -12 0
+-17 -13 0
+-17 -14 0
+-17 -15 0
+-17 -16 0
+-17 -18 0
+-18 -10 0
+-18 -11 0
+-18 -12 0
+-18 -13 0
+-18 -14 0
+-18 -15 0
+-18 -16 0
+-18 -17 0
+10 11 12 13 14 15 16 17 18 0
+-19 -20 0
+-19 -21 0
+-19 -22 0
+-19 -23 0
+-19 -24 0
+-19 -25 0
+-19 -26 0
+-19 -27 0
+-20 -19 0
+-20 -21 0
+-20 -22 0
+-20 -23 0
+-20 -24 0
+-20 -25 0
+-20 -26 0
+-20 -27 0
+-21 -19 0
+-21 -20 0
+-21 -22 0
+-21 -23 0
+-21 -24 0
+-21 -25 0
+-21 -26 0
+-21 -27 0
+-22 -19 0
+-22 -20 0
+-22 -21 0
+-22 -23 0
+-22 -24 0
+-22 -25 0
+-22 -26 0
+-22 -27 0
+-23 -19 0
+-23 -20 0
+-23 -21 0
+-23 -22 0
+-23 -24 0
+-23 -25 0
+-23 -26 0
+-23 -27 0
+-24 -19 0
+-24 -20 0
+-24 -21 0
+-24 -22 0
+-24 -23 0
+-24 -25 0
+-24 -26 0
+-24 -27 0
+-25 -19 0
+-25 -20 0
+-25 -21 0
+-25 -22 0
+-25 -23 0
+-25 -24 0
+-25 -26 0
+-25 -27 0
+-26 -19 0
+-26 -20 0
+-26 -21 0
+-26 -22 0
+-26 -23 0
+-26 -24 0
+-26 -25 0
+-26 -27 0
+-27 -19 0
+-27 -20 0
+-27 -21 0
+-27 -22 0
+-27 -23 0
+-27 -24 0
+-27 -25 0
+-27 -26 0
+19 20 21 22 23 24 25 26 27 0
+-28 -29 0
+-28 -30 0
+-28 -31 0
+-28 -32 0
+-28 -33 0
+-28 -34 0
+-28 -35 0
+-28 -36 0
+-29 -28 0
+-29 -30 0
+-29 -31 0
+-29 -32 0
+-29 -33 0
+-29 -34 0
+-29 -35 0
+-29 -36 0
+-30 -28 0
+-30 -29 0
+-30 -31 0
+-30 -32 0
+-30 -33 0
+-30 -34 0
+-30 -35 0
+-30 -36 0
+-31 -28 0
+-31 -29 0
+-31 -30 0
+-31 -32 0
+-31 -33 0
+-31 -34 0
+-31 -35 0
+-31 -36 0
+-32 -28 0
+-32 -29 0
+-32 -30 0
+-32 -31 0
+-32 -33 0
+-32 -34 0
+-32 -35 0
+-32 -36 0
+-33 -28 0
+-33 -29 0
+-33 -30 0
+-33 -31 0
+-33 -32 0
+-33 -34 0
+-33 -35 0
+-33 -36 0
+-34 -28 0
+-34 -29 0
+-34 -30 0
+-34 -31 0
+-34 -32 0
+-34 -33 0
+-34 -35 0
+-34 -36 0
+-35 -28 0
+-35 -29 0
+-35 -30 0
+-35 -31 0
+-35 -32 0
+-35 -33 0
+-35 -34 0
+-35 -36 0
+-36 -28 0
+-36 -29 0
+-36 -30 0
+-36 -31 0
+-36 -32 0
+-36 -33 0
+-36 -34 0
+-36 -35 0
+28 29 30 31 32 33 34 35 36 0
+-37 -38 0
+-37 -39 0
+-37 -40 0
+-37 -41 0
+-37 -42 0
+-37 -43 0
+-37 -44 0
+-37 -45 0
+-38 -37 0
+-38 -39 0
+-38 -40 0
+-38 -41 0
+-38 -42 0
+-38 -43 0
+-38 -44 0
+-38 -45 0
+-39 -37 0
+-39 -38 0
+-39 -40 0
+-39 -41 0
+-39 -42 0
+-39 -43 0
+-39 -44 0
+-39 -45 0
+-40 -37 0
+-40 -38 0
+-40 -39 0
+-40 -41 0
+-40 -42 0
+-40 -43 0
+-40 -44 0
+-40 -45 0
+-41 -37 0
+-41 -38 0
+-41 -39 0
+-41 -40 0
+-41 -42 0
+-41 -43 0
+-41 -44 0
+-41 -45 0
+-42 -37 0
+-42 -38 0
+-42 -39 0
+-42 -40 0
+-42 -41 0
+-42 -43 0
+-42 -44 0
+-42 -45 0
+-43 -37 0
+-43 -38 0
+-43 -39 0
+-43 -40 0
+-43 -41 0
+-43 -42 0
+-43 -44 0
+-43 -45 0
+-44 -37 0
+-44 -38 0
+-44 -39 0
+-44 -40 0
+-44 -41 0
+-44 -42 0
+-44 -43 0
+-44 -45 0
+-45 -37 0
+-45 -38 0
+-45 -39 0
+-45 -40 0
+-45 -41 0
+-45 -42 0
+-45 -43 0
+-45 -44 0
+37 38 39 40 41 42 43 44 45 0
+-46 -47 0
+-46 -48 0
+-46 -49 0
+-46 -50 0
+-46 -51 0
+-46 -52 0
+-46 -53 0
+-46 -54 0
+-47 -46 0
+-47 -48 0
+-47 -49 0
+-47 -50 0
+-47 -51 0
+-47 -52 0
+-47 -53 0
+-47 -54 0
+-48 -46 0
+-48 -47 0
+-48 -49 0
+-48 -50 0
+-48 -51 0
+-48 -52 0
+-48 -53 0
+-48 -54 0
+-49 -46 0
+-49 -47 0
+-49 -48 0
+-49 -50 0
+-49 -51 0
+-49 -52 0
+-49 -53 0
+-49 -54 0
+-50 -46 0
+-50 -47 0
+-50 -48 0
+-50 -49 0
+-50 -51 0
+-50 -52 0
+-50 -53 0
+-50 -54 0
+-51 -46 0
+-51 -47 0
+-51 -48 0
+-51 -49 0
+-51 -50 0
+-51 -52 0
+-51 -53 0
+-51 -54 0
+-52 -46 0
+-52 -47 0
+-52 -48 0
+-52 -49 0
+-52 -50 0
+-52 -51 0
+-52 -53 0
+-52 -54 0
+-53 -46 0
+-53 -47 0
+-53 -48 0
+-53 -49 0
+-53 -50 0
+-53 -51 0
+-53 -52 0
+-53 -54 0
+-54 -46 0
+-54 -47 0
+-54 -48 0
+-54 -49 0
+-54 -50 0
+-54 -51 0
+-54 -52 0
+-54 -53 0
+46 47 48 49 50 51 52 53 54 0
+-55 -56 0
+-55 -57 0
+-55 -58 0
+-55 -59 0
+-55 -60 0
+-55 -61 0
+-55 -62 0
+-55 -63 0
+-56 -55 0
+-56 -57 0
+-56 -58 0
+-56 -59 0
+-56 -60 0
+-56 -61 0
+-56 -62 0
+-56 -63 0
+-57 -55 0
+-57 -56 0
+-57 -58 0
+-57 -59 0
+-57 -60 0
+-57 -61 0
+-57 -62 0
+-57 -63 0
+-58 -55 0
+-58 -56 0
+-58 -57 0
+-58 -59 0
+-58 -60 0
+-58 -61 0
+-58 -62 0
+-58 -63 0
+-59 -55 0
+-59 -56 0
+-59 -57 0
+-59 -58 0
+-59 -60 0
+-59 -61 0
+-59 -62 0
+-59 -63 0
+-60 -55 0
+-60 -56 0
+-60 -57 0
+-60 -58 0
+-60 -59 0
+-60 -61 0
+-60 -62 0
+-60 -63 0
+-61 -55 0
+-61 -56 0
+-61 -57 0
+-61 -58 0
+-61 -59 0
+-61 -60 0
+-61 -62 0
+-61 -63 0
+-62 -55 0
+-62 -56 0
+-62 -57 0
+-62 -58 0
+-62 -59 0
+-62 -60 0
+-62 -61 0
+-62 -63 0
+-63 -55 0
+-63 -56 0
+-63 -57 0
+-63 -58 0
+-63 -59 0
+-63 -60 0
+-63 -61 0
+-63 -62 0
+55 56 57 58 59 60 61 62 63 0
+-64 -65 0
+-64 -66 0
+-64 -67 0
+-64 -68 0
+-64 -69 0
+-64 -70 0
+-64 -71 0
+-64 -72 0
+-65 -64 0
+-65 -66 0
+-65 -67 0
+-65 -68 0
+-65 -69 0
+-65 -70 0
+-65 -71 0
+-65 -72 0
+-66 -64 0
+-66 -65 0
+-66 -67 0
+-66 -68 0
+-66 -69 0
+-66 -70 0
+-66 -71 0
+-66 -72 0
+-67 -64 0
+-67 -65 0
+-67 -66 0
+-67 -68 0
+-67 -69 0
+-67 -70 0
+-67 -71 0
+-67 -72 0
+-68 -64 0
+-68 -65 0
+-68 -66 0
+-68 -67 0
+-68 -69 0
+-68 -70 0
+-68 -71 0
+-68 -72 0
+-69 -64 0
+-69 -65 0
+-69 -66 0
+-69 -67 0
+-69 -68 0
+-69 -70 0
+-69 -71 0
+-69 -72 0
+-70 -64 0
+-70 -65 0
+-70 -66 0
+-70 -67 0
+-70 -68 0
+-70 -69 0
+-70 -71 0
+-70 -72 0
+-71 -64 0
+-71 -65 0
+-71 -66 0
+-71 -67 0
+-71 -68 0
+-71 -69 0
+-71 -70 0
+-71 -72 0
+-72 -64 0
+-72 -65 0
+-72 -66 0
+-72 -67 0
+-72 -68 0
+-72 -69 0
+-72 -70 0
+-72 -71 0
+64 65 66 67 68 69 70 71 72 0
+-73 -74 0
+-73 -75 0
+-73 -76 0
+-73 -77 0
+-73 -78 0
+-73 -79 0
+-73 -80 0
+-73 -81 0
+-74 -73 0
+-74 -75 0
+-74 -76 0
+-74 -77 0
+-74 -78 0
+-74 -79 0
+-74 -80 0
+-74 -81 0
+-75 -73 0
+-75 -74 0
+-75 -76 0
+-75 -77 0
+-75 -78 0
+-75 -79 0
+-75 -80 0
+-75 -81 0
+-76 -73 0
+-76 -74 0
+-76 -75 0
+-76 -77 0
+-76 -78 0
+-76 -79 0
+-76 -80 0
+-76 -81 0
+-77 -73 0
+-77 -74 0
+-77 -75 0
+-77 -76 0
+-77 -78 0
+-77 -79 0
+-77 -80 0
+-77 -81 0
+-78 -73 0
+-78 -74 0
+-78 -75 0
+-78 -76 0
+-78 -77 0
+-78 -79 0
+-78 -80 0
+-78 -81 0
+-79 -73 0
+-79 -74 0
+-79 -75 0
+-79 -76 0
+-79 -77 0
+-79 -78 0
+-79 -80 0
+-79 -81 0
+-80 -73 0
+-80 -74 0
+-80 -75 0
+-80 -76 0
+-80 -77 0
+-80 -78 0
+-80 -79 0
+-80 -81 0
+-81 -73 0
+-81 -74 0
+-81 -75 0
+-81 -76 0
+-81 -77 0
+-81 -78 0
+-81 -79 0
+-81 -80 0
+73 74 75 76 77 78 79 80 81 0
+-82 -83 0
+-82 -84 0
+-82 -85 0
+-82 -86 0
+-82 -87 0
+-82 -88 0
+-82 -89 0
+-82 -90 0
+-83 -82 0
+-83 -84 0
+-83 -85 0
+-83 -86 0
+-83 -87 0
+-83 -88 0
+-83 -89 0
+-83 -90 0
+-84 -82 0
+-84 -83 0
+-84 -85 0
+-84 -86 0
+-84 -87 0
+-84 -88 0
+-84 -89 0
+-84 -90 0
+-85 -82 0
+-85 -83 0
+-85 -84 0
+-85 -86 0
+-85 -87 0
+-85 -88 0
+-85 -89 0
+-85 -90 0
+-86 -82 0
+-86 -83 0
+-86 -84 0
+-86 -85 0
+-86 -87 0
+-86 -88 0
+-86 -89 0
+-86 -90 0
+-87 -82 0
+-87 -83 0
+-87 -84 0
+-87 -85 0
+-87 -86 0
+-87 -88 0
+-87 -89 0
+-87 -90 0
+-88 -82 0
+-88 -83 0
+-88 -84 0
+-88 -85 0
+-88 -86 0
+-88 -87 0
+-88 -89 0
+-88 -90 0
+-89 -82 0
+-89 -83 0
+-89 -84 0
+-89 -85 0
+-89 -86 0
+-89 -87 0
+-89 -88 0
+-89 -90 0
+-90 -82 0
+-90 -83 0
+-90 -84 0
+-90 -85 0
+-90 -86 0
+-90 -87 0
+-90 -88 0
+-90 -89 0
+82 83 84 85 86 87 88 89 90 0
+-91 -92 0
+-91 -93 0
+-91 -94 0
+-91 -95 0
+-91 -96 0
+-91 -97 0
+-91 -98 0
+-91 -99 0
+-92 -91 0
+-92 -93 0
+-92 -94 0
+-92 -95 0
+-92 -96 0
+-92 -97 0
+-92 -98 0
+-92 -99 0
+-93 -91 0
+-93 -92 0
+-93 -94 0
+-93 -95 0
+-93 -96 0
+-93 -97 0
+-93 -98 0
+-93 -99 0
+-94 -91 0
+-94 -92 0
+-94 -93 0
+-94 -95 0
+-94 -96 0
+-94 -97 0
+-94 -98 0
+-94 -99 0
+-95 -91 0
+-95 -92 0
+-95 -93 0
+-95 -94 0
+-95 -96 0
+-95 -97 0
+-95 -98 0
+-95 -99 0
+-96 -91 0
+-96 -92 0
+-96 -93 0
+-96 -94 0
+-96 -95 0
+-96 -97 0
+-96 -98 0
+-96 -99 0
+-97 -91 0
+-97 -92 0
+-97 -93 0
+-97 -94 0
+-97 -95 0
+-97 -96 0
+-97 -98 0
+-97 -99 0
+-98 -91 0
+-98 -92 0
+-98 -93 0
+-98 -94 0
+-98 -95 0
+-98 -96 0
+-98 -97 0
+-98 -99 0
+-99 -91 0
+-99 -92 0
+-99 -93 0
+-99 -94 0
+-99 -95 0
+-99 -96 0
+-99 -97 0
+-99 -98 0
+91 92 93 94 95 96 97 98 99 0
+-100 -101 0
+-100 -102 0
+-100 -103 0
+-100 -104 0
+-100 -105 0
+-100 -106 0
+-100 -107 0
+-100 -108 0
+-101 -100 0
+-101 -102 0
+-101 -103 0
+-101 -104 0
+-101 -105 0
+-101 -106 0
+-101 -107 0
+-101 -108 0
+-102 -100 0
+-102 -101 0
+-102 -103 0
+-102 -104 0
+-102 -105 0
+-102 -106 0
+-102 -107 0
+-102 -108 0
+-103 -100 0
+-103 -101 0
+-103 -102 0
+-103 -104 0
+-103 -105 0
+-103 -106 0
+-103 -107 0
+-103 -108 0
+-104 -100 0
+-104 -101 0
+-104 -102 0
+-104 -103 0
+-104 -105 0
+-104 -106 0
+-104 -107 0
+-104 -108 0
+-105 -100 0
+-105 -101 0
+-105 -102 0
+-105 -103 0
+-105 -104 0
+-105 -106 0
+-105 -107 0
+-105 -108 0
+-106 -100 0
+-106 -101 0
+-106 -102 0
+-106 -103 0
+-106 -104 0
+-106 -105 0
+-106 -107 0
+-106 -108 0
+-107 -100 0
+-107 -101 0
+-107 -102 0
+-107 -103 0
+-107 -104 0
+-107 -105 0
+-107 -106 0
+-107 -108 0
+-108 -100 0
+-108 -101 0
+-108 -102 0
+-108 -103 0
+-108 -104 0
+-108 -105 0
+-108 -106 0
+-108 -107 0
+100 101 102 103 104 105 106 107 108 0
+-109 -110 0
+-109 -111 0
+-109 -112 0
+-109 -113 0
+-109 -114 0
+-109 -115 0
+-109 -116 0
+-109 -117 0
+-110 -109 0
+-110 -111 0
+-110 -112 0
+-110 -113 0
+-110 -114 0
+-110 -115 0
+-110 -116 0
+-110 -117 0
+-111 -109 0
+-111 -110 0
+-111 -112 0
+-111 -113 0
+-111 -114 0
+-111 -115 0
+-111 -116 0
+-111 -117 0
+-112 -109 0
+-112 -110 0
+-112 -111 0
+-112 -113 0
+-112 -114 0
+-112 -115 0
+-112 -116 0
+-112 -117 0
+-113 -109 0
+-113 -110 0
+-113 -111 0
+-113 -112 0
+-113 -114 0
+-113 -115 0
+-113 -116 0
+-113 -117 0
+-114 -109 0
+-114 -110 0
+-114 -111 0
+-114 -112 0
+-114 -113 0
+-114 -115 0
+-114 -116 0
+-114 -117 0
+-115 -109 0
+-115 -110 0
+-115 -111 0
+-115 -112 0
+-115 -113 0
+-115 -114 0
+-115 -116 0
+-115 -117 0
+-116 -109 0
+-116 -110 0
+-116 -111 0
+-116 -112 0
+-116 -113 0
+-116 -114 0
+-116 -115 0
+-116 -117 0
+-117 -109 0
+-117 -110 0
+-117 -111 0
+-117 -112 0
+-117 -113 0
+-117 -114 0
+-117 -115 0
+-117 -116 0
+109 110 111 112 113 114 115 116 117 0
+-118 -119 0
+-118 -120 0
+-118 -121 0
+-118 -122 0
+-118 -123 0
+-118 -124 0
+-118 -125 0
+-118 -126 0
+-119 -118 0
+-119 -120 0
+-119 -121 0
+-119 -122 0
+-119 -123 0
+-119 -124 0
+-119 -125 0
+-119 -126 0
+-120 -118 0
+-120 -119 0
+-120 -121 0
+-120 -122 0
+-120 -123 0
+-120 -124 0
+-120 -125 0
+-120 -126 0
+-121 -118 0
+-121 -119 0
+-121 -120 0
+-121 -122 0
+-121 -123 0
+-121 -124 0
+-121 -125 0
+-121 -126 0
+-122 -118 0
+-122 -119 0
+-122 -120 0
+-122 -121 0
+-122 -123 0
+-122 -124 0
+-122 -125 0
+-122 -126 0
+-123 -118 0
+-123 -119 0
+-123 -120 0
+-123 -121 0
+-123 -122 0
+-123 -124 0
+-123 -125 0
+-123 -126 0
+-124 -118 0
+-124 -119 0
+-124 -120 0
+-124 -121 0
+-124 -122 0
+-124 -123 0
+-124 -125 0
+-124 -126 0
+-125 -118 0
+-125 -119 0
+-125 -120 0
+-125 -121 0
+-125 -122 0
+-125 -123 0
+-125 -124 0
+-125 -126 0
+-126 -118 0
+-126 -119 0
+-126 -120 0
+-126 -121 0
+-126 -122 0
+-126 -123 0
+-126 -124 0
+-126 -125 0
+118 119 120 121 122 123 124 125 126 0
+-127 -128 0
+-127 -129 0
+-127 -130 0
+-127 -131 0
+-127 -132 0
+-127 -133 0
+-127 -134 0
+-127 -135 0
+-128 -127 0
+-128 -129 0
+-128 -130 0
+-128 -131 0
+-128 -132 0
+-128 -133 0
+-128 -134 0
+-128 -135 0
+-129 -127 0
+-129 -128 0
+-129 -130 0
+-129 -131 0
+-129 -132 0
+-129 -133 0
+-129 -134 0
+-129 -135 0
+-130 -127 0
+-130 -128 0
+-130 -129 0
+-130 -131 0
+-130 -132 0
+-130 -133 0
+-130 -134 0
+-130 -135 0
+-131 -127 0
+-131 -128 0
+-131 -129 0
+-131 -130 0
+-131 -132 0
+-131 -133 0
+-131 -134 0
+-131 -135 0
+-132 -127 0
+-132 -128 0
+-132 -129 0
+-132 -130 0
+-132 -131 0
+-132 -133 0
+-132 -134 0
+-132 -135 0
+-133 -127 0
+-133 -128 0
+-133 -129 0
+-133 -130 0
+-133 -131 0
+-133 -132 0
+-133 -134 0
+-133 -135 0
+-134 -127 0
+-134 -128 0
+-134 -129 0
+-134 -130 0
+-134 -131 0
+-134 -132 0
+-134 -133 0
+-134 -135 0
+-135 -127 0
+-135 -128 0
+-135 -129 0
+-135 -130 0
+-135 -131 0
+-135 -132 0
+-135 -133 0
+-135 -134 0
+127 128 129 130 131 132 133 134 135 0
+-136 -137 0
+-136 -138 0
+-136 -139 0
+-136 -140 0
+-136 -141 0
+-136 -142 0
+-136 -143 0
+-136 -144 0
+-137 -136 0
+-137 -138 0
+-137 -139 0
+-137 -140 0
+-137 -141 0
+-137 -142 0
+-137 -143 0
+-137 -144 0
+-138 -136 0
+-138 -137 0
+-138 -139 0
+-138 -140 0
+-138 -141 0
+-138 -142 0
+-138 -143 0
+-138 -144 0
+-139 -136 0
+-139 -137 0
+-139 -138 0
+-139 -140 0
+-139 -141 0
+-139 -142 0
+-139 -143 0
+-139 -144 0
+-140 -136 0
+-140 -137 0
+-140 -138 0
+-140 -139 0
+-140 -141 0
+-140 -142 0
+-140 -143 0
+-140 -144 0
+-141 -136 0
+-141 -137 0
+-141 -138 0
+-141 -139 0
+-141 -140 0
+-141 -142 0
+-141 -143 0
+-141 -144 0
+-142 -136 0
+-142 -137 0
+-142 -138 0
+-142 -139 0
+-142 -140 0
+-142 -141 0
+-142 -143 0
+-142 -144 0
+-143 -136 0
+-143 -137 0
+-143 -138 0
+-143 -139 0
+-143 -140 0
+-143 -141 0
+-143 -142 0
+-143 -144 0
+-144 -136 0
+-144 -137 0
+-144 -138 0
+-144 -139 0
+-144 -140 0
+-144 -141 0
+-144 -142 0
+-144 -143 0
+136 137 138 139 140 141 142 143 144 0
+-145 -146 0
+-145 -147 0
+-145 -148 0
+-145 -149 0
+-145 -150 0
+-145 -151 0
+-145 -152 0
+-145 -153 0
+-146 -145 0
+-146 -147 0
+-146 -148 0
+-146 -149 0
+-146 -150 0
+-146 -151 0
+-146 -152 0
+-146 -153 0
+-147 -145 0
+-147 -146 0
+-147 -148 0
+-147 -149 0
+-147 -150 0
+-147 -151 0
+-147 -152 0
+-147 -153 0
+-148 -145 0
+-148 -146 0
+-148 -147 0
+-148 -149 0
+-148 -150 0
+-148 -151 0
+-148 -152 0
+-148 -153 0
+-149 -145 0
+-149 -146 0
+-149 -147 0
+-149 -148 0
+-149 -150 0
+-149 -151 0
+-149 -152 0
+-149 -153 0
+-150 -145 0
+-150 -146 0
+-150 -147 0
+-150 -148 0
+-150 -149 0
+-150 -151 0
+-150 -152 0
+-150 -153 0
+-151 -145 0
+-151 -146 0
+-151 -147 0
+-151 -148 0
+-151 -149 0
+-151 -150 0
+-151 -152 0
+-151 -153 0
+-152 -145 0
+-152 -146 0
+-152 -147 0
+-152 -148 0
+-152 -149 0
+-152 -150 0
+-152 -151 0
+-152 -153 0
+-153 -145 0
+-153 -146 0
+-153 -147 0
+-153 -148 0
+-153 -149 0
+-153 -150 0
+-153 -151 0
+-153 -152 0
+145 146 147 148 149 150 151 152 153 0
+-154 -155 0
+-154 -156 0
+-154 -157 0
+-154 -158 0
+-154 -159 0
+-154 -160 0
+-154 -161 0
+-154 -162 0
+-155 -154 0
+-155 -156 0
+-155 -157 0
+-155 -158 0
+-155 -159 0
+-155 -160 0
+-155 -161 0
+-155 -162 0
+-156 -154 0
+-156 -155 0
+-156 -157 0
+-156 -158 0
+-156 -159 0
+-156 -160 0
+-156 -161 0
+-156 -162 0
+-157 -154 0
+-157 -155 0
+-157 -156 0
+-157 -158 0
+-157 -159 0
+-157 -160 0
+-157 -161 0
+-157 -162 0
+-158 -154 0
+-158 -155 0
+-158 -156 0
+-158 -157 0
+-158 -159 0
+-158 -160 0
+-158 -161 0
+-158 -162 0
+-159 -154 0
+-159 -155 0
+-159 -156 0
+-159 -157 0
+-159 -158 0
+-159 -160 0
+-159 -161 0
+-159 -162 0
+-160 -154 0
+-160 -155 0
+-160 -156 0
+-160 -157 0
+-160 -158 0
+-160 -159 0
+-160 -161 0
+-160 -162 0
+-161 -154 0
+-161 -155 0
+-161 -156 0
+-161 -157 0
+-161 -158 0
+-161 -159 0
+-161 -160 0
+-161 -162 0
+-162 -154 0
+-162 -155 0
+-162 -156 0
+-162 -157 0
+-162 -158 0
+-162 -159 0
+-162 -160 0
+-162 -161 0
+154 155 156 157 158 159 160 161 162 0
+-163 -164 0
+-163 -165 0
+-163 -166 0
+-163 -167 0
+-163 -168 0
+-163 -169 0
+-163 -170 0
+-163 -171 0
+-164 -163 0
+-164 -165 0
+-164 -166 0
+-164 -167 0
+-164 -168 0
+-164 -169 0
+-164 -170 0
+-164 -171 0
+-165 -163 0
+-165 -164 0
+-165 -166 0
+-165 -167 0
+-165 -168 0
+-165 -169 0
+-165 -170 0
+-165 -171 0
+-166 -163 0
+-166 -164 0
+-166 -165 0
+-166 -167 0
+-166 -168 0
+-166 -169 0
+-166 -170 0
+-166 -171 0
+-167 -163 0
+-167 -164 0
+-167 -165 0
+-167 -166 0
+-167 -168 0
+-167 -169 0
+-167 -170 0
+-167 -171 0
+-168 -163 0
+-168 -164 0
+-168 -165 0
+-168 -166 0
+-168 -167 0
+-168 -169 0
+-168 -170 0
+-168 -171 0
+-169 -163 0
+-169 -164 0
+-169 -165 0
+-169 -166 0
+-169 -167 0
+-169 -168 0
+-169 -170 0
+-169 -171 0
+-170 -163 0
+-170 -164 0
+-170 -165 0
+-170 -166 0
+-170 -167 0
+-170 -168 0
+-170 -169 0
+-170 -171 0
+-171 -163 0
+-171 -164 0
+-171 -165 0
+-171 -166 0
+-171 -167 0
+-171 -168 0
+-171 -169 0
+-171 -170 0
+163 164 165 166 167 168 169 170 171 0
+-172 -173 0
+-172 -174 0
+-172 -175 0
+-172 -176 0
+-172 -177 0
+-172 -178 0
+-172 -179 0
+-172 -180 0
+-173 -172 0
+-173 -174 0
+-173 -175 0
+-173 -176 0
+-173 -177 0
+-173 -178 0
+-173 -179 0
+-173 -180 0
+-174 -172 0
+-174 -173 0
+-174 -175 0
+-174 -176 0
+-174 -177 0
+-174 -178 0
+-174 -179 0
+-174 -180 0
+-175 -172 0
+-175 -173 0
+-175 -174 0
+-175 -176 0
+-175 -177 0
+-175 -178 0
+-175 -179 0
+-175 -180 0
+-176 -172 0
+-176 -173 0
+-176 -174 0
+-176 -175 0
+-176 -177 0
+-176 -178 0
+-176 -179 0
+-176 -180 0
+-177 -172 0
+-177 -173 0
+-177 -174 0
+-177 -175 0
+-177 -176 0
+-177 -178 0
+-177 -179 0
+-177 -180 0
+-178 -172 0
+-178 -173 0
+-178 -174 0
+-178 -175 0
+-178 -176 0
+-178 -177 0
+-178 -179 0
+-178 -180 0
+-179 -172 0
+-179 -173 0
+-179 -174 0
+-179 -175 0
+-179 -176 0
+-179 -177 0
+-179 -178 0
+-179 -180 0
+-180 -172 0
+-180 -173 0
+-180 -174 0
+-180 -175 0
+-180 -176 0
+-180 -177 0
+-180 -178 0
+-180 -179 0
+172 173 174 175 176 177 178 179 180 0
+-181 -182 0
+-181 -183 0
+-181 -184 0
+-181 -185 0
+-181 -186 0
+-181 -187 0
+-181 -188 0
+-181 -189 0
+-182 -181 0
+-182 -183 0
+-182 -184 0
+-182 -185 0
+-182 -186 0
+-182 -187 0
+-182 -188 0
+-182 -189 0
+-183 -181 0
+-183 -182 0
+-183 -184 0
+-183 -185 0
+-183 -186 0
+-183 -187 0
+-183 -188 0
+-183 -189 0
+-184 -181 0
+-184 -182 0
+-184 -183 0
+-184 -185 0
+-184 -186 0
+-184 -187 0
+-184 -188 0
+-184 -189 0
+-185 -181 0
+-185 -182 0
+-185 -183 0
+-185 -184 0
+-185 -186 0
+-185 -187 0
+-185 -188 0
+-185 -189 0
+-186 -181 0
+-186 -182 0
+-186 -183 0
+-186 -184 0
+-186 -185 0
+-186 -187 0
+-186 -188 0
+-186 -189 0
+-187 -181 0
+-187 -182 0
+-187 -183 0
+-187 -184 0
+-187 -185 0
+-187 -186 0
+-187 -188 0
+-187 -189 0
+-188 -181 0
+-188 -182 0
+-188 -183 0
+-188 -184 0
+-188 -185 0
+-188 -186 0
+-188 -187 0
+-188 -189 0
+-189 -181 0
+-189 -182 0
+-189 -183 0
+-189 -184 0
+-189 -185 0
+-189 -186 0
+-189 -187 0
+-189 -188 0
+181 182 183 184 185 186 187 188 189 0
+-190 -191 0
+-190 -192 0
+-190 -193 0
+-190 -194 0
+-190 -195 0
+-190 -196 0
+-190 -197 0
+-190 -198 0
+-191 -190 0
+-191 -192 0
+-191 -193 0
+-191 -194 0
+-191 -195 0
+-191 -196 0
+-191 -197 0
+-191 -198 0
+-192 -190 0
+-192 -191 0
+-192 -193 0
+-192 -194 0
+-192 -195 0
+-192 -196 0
+-192 -197 0
+-192 -198 0
+-193 -190 0
+-193 -191 0
+-193 -192 0
+-193 -194 0
+-193 -195 0
+-193 -196 0
+-193 -197 0
+-193 -198 0
+-194 -190 0
+-194 -191 0
+-194 -192 0
+-194 -193 0
+-194 -195 0
+-194 -196 0
+-194 -197 0
+-194 -198 0
+-195 -190 0
+-195 -191 0
+-195 -192 0
+-195 -193 0
+-195 -194 0
+-195 -196 0
+-195 -197 0
+-195 -198 0
+-196 -190 0
+-196 -191 0
+-196 -192 0
+-196 -193 0
+-196 -194 0
+-196 -195 0
+-196 -197 0
+-196 -198 0
+-197 -190 0
+-197 -191 0
+-197 -192 0
+-197 -193 0
+-197 -194 0
+-197 -195 0
+-197 -196 0
+-197 -198 0
+-198 -190 0
+-198 -191 0
+-198 -192 0
+-198 -193 0
+-198 -194 0
+-198 -195 0
+-198 -196 0
+-198 -197 0
+190 191 192 193 194 195 196 197 198 0
+-199 -200 0
+-199 -201 0
+-199 -202 0
+-199 -203 0
+-199 -204 0
+-199 -205 0
+-199 -206 0
+-199 -207 0
+-200 -199 0
+-200 -201 0
+-200 -202 0
+-200 -203 0
+-200 -204 0
+-200 -205 0
+-200 -206 0
+-200 -207 0
+-201 -199 0
+-201 -200 0
+-201 -202 0
+-201 -203 0
+-201 -204 0
+-201 -205 0
+-201 -206 0
+-201 -207 0
+-202 -199 0
+-202 -200 0
+-202 -201 0
+-202 -203 0
+-202 -204 0
+-202 -205 0
+-202 -206 0
+-202 -207 0
+-203 -199 0
+-203 -200 0
+-203 -201 0
+-203 -202 0
+-203 -204 0
+-203 -205 0
+-203 -206 0
+-203 -207 0
+-204 -199 0
+-204 -200 0
+-204 -201 0
+-204 -202 0
+-204 -203 0
+-204 -205 0
+-204 -206 0
+-204 -207 0
+-205 -199 0
+-205 -200 0
+-205 -201 0
+-205 -202 0
+-205 -203 0
+-205 -204 0
+-205 -206 0
+-205 -207 0
+-206 -199 0
+-206 -200 0
+-206 -201 0
+-206 -202 0
+-206 -203 0
+-206 -204 0
+-206 -205 0
+-206 -207 0
+-207 -199 0
+-207 -200 0
+-207 -201 0
+-207 -202 0
+-207 -203 0
+-207 -204 0
+-207 -205 0
+-207 -206 0
+199 200 201 202 203 204 205 206 207 0
+-208 -209 0
+-208 -210 0
+-208 -211 0
+-208 -212 0
+-208 -213 0
+-208 -214 0
+-208 -215 0
+-208 -216 0
+-209 -208 0
+-209 -210 0
+-209 -211 0
+-209 -212 0
+-209 -213 0
+-209 -214 0
+-209 -215 0
+-209 -216 0
+-210 -208 0
+-210 -209 0
+-210 -211 0
+-210 -212 0
+-210 -213 0
+-210 -214 0
+-210 -215 0
+-210 -216 0
+-211 -208 0
+-211 -209 0
+-211 -210 0
+-211 -212 0
+-211 -213 0
+-211 -214 0
+-211 -215 0
+-211 -216 0
+-212 -208 0
+-212 -209 0
+-212 -210 0
+-212 -211 0
+-212 -213 0
+-212 -214 0
+-212 -215 0
+-212 -216 0
+-213 -208 0
+-213 -209 0
+-213 -210 0
+-213 -211 0
+-213 -212 0
+-213 -214 0
+-213 -215 0
+-213 -216 0
+-214 -208 0
+-214 -209 0
+-214 -210 0
+-214 -211 0
+-214 -212 0
+-214 -213 0
+-214 -215 0
+-214 -216 0
+-215 -208 0
+-215 -209 0
+-215 -210 0
+-215 -211 0
+-215 -212 0
+-215 -213 0
+-215 -214 0
+-215 -216 0
+-216 -208 0
+-216 -209 0
+-216 -210 0
+-216 -211 0
+-216 -212 0
+-216 -213 0
+-216 -214 0
+-216 -215 0
+208 209 210 211 212 213 214 215 216 0
+-217 -218 0
+-217 -219 0
+-217 -220 0
+-217 -221 0
+-217 -222 0
+-217 -223 0
+-217 -224 0
+-217 -225 0
+-218 -217 0
+-218 -219 0
+-218 -220 0
+-218 -221 0
+-218 -222 0
+-218 -223 0
+-218 -224 0
+-218 -225 0
+-219 -217 0
+-219 -218 0
+-219 -220 0
+-219 -221 0
+-219 -222 0
+-219 -223 0
+-219 -224 0
+-219 -225 0
+-220 -217 0
+-220 -218 0
+-220 -219 0
+-220 -221 0
+-220 -222 0
+-220 -223 0
+-220 -224 0
+-220 -225 0
+-221 -217 0
+-221 -218 0
+-221 -219 0
+-221 -220 0
+-221 -222 0
+-221 -223 0
+-221 -224 0
+-221 -225 0
+-222 -217 0
+-222 -218 0
+-222 -219 0
+-222 -220 0
+-222 -221 0
+-222 -223 0
+-222 -224 0
+-222 -225 0
+-223 -217 0
+-223 -218 0
+-223 -219 0
+-223 -220 0
+-223 -221 0
+-223 -222 0
+-223 -224 0
+-223 -225 0
+-224 -217 0
+-224 -218 0
+-224 -219 0
+-224 -220 0
+-224 -221 0
+-224 -222 0
+-224 -223 0
+-224 -225 0
+-225 -217 0
+-225 -218 0
+-225 -219 0
+-225 -220 0
+-225 -221 0
+-225 -222 0
+-225 -223 0
+-225 -224 0
+217 218 219 220 221 222 223 224 225 0
+-226 -227 0
+-226 -228 0
+-226 -229 0
+-226 -230 0
+-226 -231 0
+-226 -232 0
+-226 -233 0
+-226 -234 0
+-227 -226 0
+-227 -228 0
+-227 -229 0
+-227 -230 0
+-227 -231 0
+-227 -232 0
+-227 -233 0
+-227 -234 0
+-228 -226 0
+-228 -227 0
+-228 -229 0
+-228 -230 0
+-228 -231 0
+-228 -232 0
+-228 -233 0
+-228 -234 0
+-229 -226 0
+-229 -227 0
+-229 -228 0
+-229 -230 0
+-229 -231 0
+-229 -232 0
+-229 -233 0
+-229 -234 0
+-230 -226 0
+-230 -227 0
+-230 -228 0
+-230 -229 0
+-230 -231 0
+-230 -232 0
+-230 -233 0
+-230 -234 0
+-231 -226 0
+-231 -227 0
+-231 -228 0
+-231 -229 0
+-231 -230 0
+-231 -232 0
+-231 -233 0
+-231 -234 0
+-232 -226 0
+-232 -227 0
+-232 -228 0
+-232 -229 0
+-232 -230 0
+-232 -231 0
+-232 -233 0
+-232 -234 0
+-233 -226 0
+-233 -227 0
+-233 -228 0
+-233 -229 0
+-233 -230 0
+-233 -231 0
+-233 -232 0
+-233 -234 0
+-234 -226 0
+-234 -227 0
+-234 -228 0
+-234 -229 0
+-234 -230 0
+-234 -231 0
+-234 -232 0
+-234 -233 0
+226 227 228 229 230 231 232 233 234 0
+-235 -236 0
+-235 -237 0
+-235 -238 0
+-235 -239 0
+-235 -240 0
+-235 -241 0
+-235 -242 0
+-235 -243 0
+-236 -235 0
+-236 -237 0
+-236 -238 0
+-236 -239 0
+-236 -240 0
+-236 -241 0
+-236 -242 0
+-236 -243 0
+-237 -235 0
+-237 -236 0
+-237 -238 0
+-237 -239 0
+-237 -240 0
+-237 -241 0
+-237 -242 0
+-237 -243 0
+-238 -235 0
+-238 -236 0
+-238 -237 0
+-238 -239 0
+-238 -240 0
+-238 -241 0
+-238 -242 0
+-238 -243 0
+-239 -235 0
+-239 -236 0
+-239 -237 0
+-239 -238 0
+-239 -240 0
+-239 -241 0
+-239 -242 0
+-239 -243 0
+-240 -235 0
+-240 -236 0
+-240 -237 0
+-240 -238 0
+-240 -239 0
+-240 -241 0
+-240 -242 0
+-240 -243 0
+-241 -235 0
+-241 -236 0
+-241 -237 0
+-241 -238 0
+-241 -239 0
+-241 -240 0
+-241 -242 0
+-241 -243 0
+-242 -235 0
+-242 -236 0
+-242 -237 0
+-242 -238 0
+-242 -239 0
+-242 -240 0
+-242 -241 0
+-242 -243 0
+-243 -235 0
+-243 -236 0
+-243 -237 0
+-243 -238 0
+-243 -239 0
+-243 -240 0
+-243 -241 0
+-243 -242 0
+235 236 237 238 239 240 241 242 243 0
+-244 -245 0
+-244 -246 0
+-244 -247 0
+-244 -248 0
+-244 -249 0
+-244 -250 0
+-244 -251 0
+-244 -252 0
+-245 -244 0
+-245 -246 0
+-245 -247 0
+-245 -248 0
+-245 -249 0
+-245 -250 0
+-245 -251 0
+-245 -252 0
+-246 -244 0
+-246 -245 0
+-246 -247 0
+-246 -248 0
+-246 -249 0
+-246 -250 0
+-246 -251 0
+-246 -252 0
+-247 -244 0
+-247 -245 0
+-247 -246 0
+-247 -248 0
+-247 -249 0
+-247 -250 0
+-247 -251 0
+-247 -252 0
+-248 -244 0
+-248 -245 0
+-248 -246 0
+-248 -247 0
+-248 -249 0
+-248 -250 0
+-248 -251 0
+-248 -252 0
+-249 -244 0
+-249 -245 0
+-249 -246 0
+-249 -247 0
+-249 -248 0
+-249 -250 0
+-249 -251 0
+-249 -252 0
+-250 -244 0
+-250 -245 0
+-250 -246 0
+-250 -247 0
+-250 -248 0
+-250 -249 0
+-250 -251 0
+-250 -252 0
+-251 -244 0
+-251 -245 0
+-251 -246 0
+-251 -247 0
+-251 -248 0
+-251 -249 0
+-251 -250 0
+-251 -252 0
+-252 -244 0
+-252 -245 0
+-252 -246 0
+-252 -247 0
+-252 -248 0
+-252 -249 0
+-252 -250 0
+-252 -251 0
+244 245 246 247 248 249 250 251 252 0
+-253 -254 0
+-253 -255 0
+-253 -256 0
+-253 -257 0
+-253 -258 0
+-253 -259 0
+-253 -260 0
+-253 -261 0
+-254 -253 0
+-254 -255 0
+-254 -256 0
+-254 -257 0
+-254 -258 0
+-254 -259 0
+-254 -260 0
+-254 -261 0
+-255 -253 0
+-255 -254 0
+-255 -256 0
+-255 -257 0
+-255 -258 0
+-255 -259 0
+-255 -260 0
+-255 -261 0
+-256 -253 0
+-256 -254 0
+-256 -255 0
+-256 -257 0
+-256 -258 0
+-256 -259 0
+-256 -260 0
+-256 -261 0
+-257 -253 0
+-257 -254 0
+-257 -255 0
+-257 -256 0
+-257 -258 0
+-257 -259 0
+-257 -260 0
+-257 -261 0
+-258 -253 0
+-258 -254 0
+-258 -255 0
+-258 -256 0
+-258 -257 0
+-258 -259 0
+-258 -260 0
+-258 -261 0
+-259 -253 0
+-259 -254 0
+-259 -255 0
+-259 -256 0
+-259 -257 0
+-259 -258 0
+-259 -260 0
+-259 -261 0
+-260 -253 0
+-260 -254 0
+-260 -255 0
+-260 -256 0
+-260 -257 0
+-260 -258 0
+-260 -259 0
+-260 -261 0
+-261 -253 0
+-261 -254 0
+-261 -255 0
+-261 -256 0
+-261 -257 0
+-261 -258 0
+-261 -259 0
+-261 -260 0
+253 254 255 256 257 258 259 260 261 0
+-262 -263 0
+-262 -264 0
+-262 -265 0
+-262 -266 0
+-262 -267 0
+-262 -268 0
+-262 -269 0
+-262 -270 0
+-263 -262 0
+-263 -264 0
+-263 -265 0
+-263 -266 0
+-263 -267 0
+-263 -268 0
+-263 -269 0
+-263 -270 0
+-264 -262 0
+-264 -263 0
+-264 -265 0
+-264 -266 0
+-264 -267 0
+-264 -268 0
+-264 -269 0
+-264 -270 0
+-265 -262 0
+-265 -263 0
+-265 -264 0
+-265 -266 0
+-265 -267 0
+-265 -268 0
+-265 -269 0
+-265 -270 0
+-266 -262 0
+-266 -263 0
+-266 -264 0
+-266 -265 0
+-266 -267 0
+-266 -268 0
+-266 -269 0
+-266 -270 0
+-267 -262 0
+-267 -263 0
+-267 -264 0
+-267 -265 0
+-267 -266 0
+-267 -268 0
+-267 -269 0
+-267 -270 0
+-268 -262 0
+-268 -263 0
+-268 -264 0
+-268 -265 0
+-268 -266 0
+-268 -267 0
+-268 -269 0
+-268 -270 0
+-269 -262 0
+-269 -263 0
+-269 -264 0
+-269 -265 0
+-269 -266 0
+-269 -267 0
+-269 -268 0
+-269 -270 0
+-270 -262 0
+-270 -263 0
+-270 -264 0
+-270 -265 0
+-270 -266 0
+-270 -267 0
+-270 -268 0
+-270 -269 0
+262 263 264 265 266 267 268 269 270 0
+-271 -272 0
+-271 -273 0
+-271 -274 0
+-271 -275 0
+-271 -276 0
+-271 -277 0
+-271 -278 0
+-271 -279 0
+-272 -271 0
+-272 -273 0
+-272 -274 0
+-272 -275 0
+-272 -276 0
+-272 -277 0
+-272 -278 0
+-272 -279 0
+-273 -271 0
+-273 -272 0
+-273 -274 0
+-273 -275 0
+-273 -276 0
+-273 -277 0
+-273 -278 0
+-273 -279 0
+-274 -271 0
+-274 -272 0
+-274 -273 0
+-274 -275 0
+-274 -276 0
+-274 -277 0
+-274 -278 0
+-274 -279 0
+-275 -271 0
+-275 -272 0
+-275 -273 0
+-275 -274 0
+-275 -276 0
+-275 -277 0
+-275 -278 0
+-275 -279 0
+-276 -271 0
+-276 -272 0
+-276 -273 0
+-276 -274 0
+-276 -275 0
+-276 -277 0
+-276 -278 0
+-276 -279 0
+-277 -271 0
+-277 -272 0
+-277 -273 0
+-277 -274 0
+-277 -275 0
+-277 -276 0
+-277 -278 0
+-277 -279 0
+-278 -271 0
+-278 -272 0
+-278 -273 0
+-278 -274 0
+-278 -275 0
+-278 -276 0
+-278 -277 0
+-278 -279 0
+-279 -271 0
+-279 -272 0
+-279 -273 0
+-279 -274 0
+-279 -275 0
+-279 -276 0
+-279 -277 0
+-279 -278 0
+271 272 273 274 275 276 277 278 279 0
+-280 -281 0
+-280 -282 0
+-280 -283 0
+-280 -284 0
+-280 -285 0
+-280 -286 0
+-280 -287 0
+-280 -288 0
+-281 -280 0
+-281 -282 0
+-281 -283 0
+-281 -284 0
+-281 -285 0
+-281 -286 0
+-281 -287 0
+-281 -288 0
+-282 -280 0
+-282 -281 0
+-282 -283 0
+-282 -284 0
+-282 -285 0
+-282 -286 0
+-282 -287 0
+-282 -288 0
+-283 -280 0
+-283 -281 0
+-283 -282 0
+-283 -284 0
+-283 -285 0
+-283 -286 0
+-283 -287 0
+-283 -288 0
+-284 -280 0
+-284 -281 0
+-284 -282 0
+-284 -283 0
+-284 -285 0
+-284 -286 0
+-284 -287 0
+-284 -288 0
+-285 -280 0
+-285 -281 0
+-285 -282 0
+-285 -283 0
+-285 -284 0
+-285 -286 0
+-285 -287 0
+-285 -288 0
+-286 -280 0
+-286 -281 0
+-286 -282 0
+-286 -283 0
+-286 -284 0
+-286 -285 0
+-286 -287 0
+-286 -288 0
+-287 -280 0
+-287 -281 0
+-287 -282 0
+-287 -283 0
+-287 -284 0
+-287 -285 0
+-287 -286 0
+-287 -288 0
+-288 -280 0
+-288 -281 0
+-288 -282 0
+-288 -283 0
+-288 -284 0
+-288 -285 0
+-288 -286 0
+-288 -287 0
+280 281 282 283 284 285 286 287 288 0
+-289 -290 0
+-289 -291 0
+-289 -292 0
+-289 -293 0
+-289 -294 0
+-289 -295 0
+-289 -296 0
+-289 -297 0
+-290 -289 0
+-290 -291 0
+-290 -292 0
+-290 -293 0
+-290 -294 0
+-290 -295 0
+-290 -296 0
+-290 -297 0
+-291 -289 0
+-291 -290 0
+-291 -292 0
+-291 -293 0
+-291 -294 0
+-291 -295 0
+-291 -296 0
+-291 -297 0
+-292 -289 0
+-292 -290 0
+-292 -291 0
+-292 -293 0
+-292 -294 0
+-292 -295 0
+-292 -296 0
+-292 -297 0
+-293 -289 0
+-293 -290 0
+-293 -291 0
+-293 -292 0
+-293 -294 0
+-293 -295 0
+-293 -296 0
+-293 -297 0
+-294 -289 0
+-294 -290 0
+-294 -291 0
+-294 -292 0
+-294 -293 0
+-294 -295 0
+-294 -296 0
+-294 -297 0
+-295 -289 0
+-295 -290 0
+-295 -291 0
+-295 -292 0
+-295 -293 0
+-295 -294 0
+-295 -296 0
+-295 -297 0
+-296 -289 0
+-296 -290 0
+-296 -291 0
+-296 -292 0
+-296 -293 0
+-296 -294 0
+-296 -295 0
+-296 -297 0
+-297 -289 0
+-297 -290 0
+-297 -291 0
+-297 -292 0
+-297 -293 0
+-297 -294 0
+-297 -295 0
+-297 -296 0
+289 290 291 292 293 294 295 296 297 0
+-298 -299 0
+-298 -300 0
+-298 -301 0
+-298 -302 0
+-298 -303 0
+-298 -304 0
+-298 -305 0
+-298 -306 0
+-299 -298 0
+-299 -300 0
+-299 -301 0
+-299 -302 0
+-299 -303 0
+-299 -304 0
+-299 -305 0
+-299 -306 0
+-300 -298 0
+-300 -299 0
+-300 -301 0
+-300 -302 0
+-300 -303 0
+-300 -304 0
+-300 -305 0
+-300 -306 0
+-301 -298 0
+-301 -299 0
+-301 -300 0
+-301 -302 0
+-301 -303 0
+-301 -304 0
+-301 -305 0
+-301 -306 0
+-302 -298 0
+-302 -299 0
+-302 -300 0
+-302 -301 0
+-302 -303 0
+-302 -304 0
+-302 -305 0
+-302 -306 0
+-303 -298 0
+-303 -299 0
+-303 -300 0
+-303 -301 0
+-303 -302 0
+-303 -304 0
+-303 -305 0
+-303 -306 0
+-304 -298 0
+-304 -299 0
+-304 -300 0
+-304 -301 0
+-304 -302 0
+-304 -303 0
+-304 -305 0
+-304 -306 0
+-305 -298 0
+-305 -299 0
+-305 -300 0
+-305 -301 0
+-305 -302 0
+-305 -303 0
+-305 -304 0
+-305 -306 0
+-306 -298 0
+-306 -299 0
+-306 -300 0
+-306 -301 0
+-306 -302 0
+-306 -303 0
+-306 -304 0
+-306 -305 0
+298 299 300 301 302 303 304 305 306 0
+-307 -308 0
+-307 -309 0
+-307 -310 0
+-307 -311 0
+-307 -312 0
+-307 -313 0
+-307 -314 0
+-307 -315 0
+-308 -307 0
+-308 -309 0
+-308 -310 0
+-308 -311 0
+-308 -312 0
+-308 -313 0
+-308 -314 0
+-308 -315 0
+-309 -307 0
+-309 -308 0
+-309 -310 0
+-309 -311 0
+-309 -312 0
+-309 -313 0
+-309 -314 0
+-309 -315 0
+-310 -307 0
+-310 -308 0
+-310 -309 0
+-310 -311 0
+-310 -312 0
+-310 -313 0
+-310 -314 0
+-310 -315 0
+-311 -307 0
+-311 -308 0
+-311 -309 0
+-311 -310 0
+-311 -312 0
+-311 -313 0
+-311 -314 0
+-311 -315 0
+-312 -307 0
+-312 -308 0
+-312 -309 0
+-312 -310 0
+-312 -311 0
+-312 -313 0
+-312 -314 0
+-312 -315 0
+-313 -307 0
+-313 -308 0
+-313 -309 0
+-313 -310 0
+-313 -311 0
+-313 -312 0
+-313 -314 0
+-313 -315 0
+-314 -307 0
+-314 -308 0
+-314 -309 0
+-314 -310 0
+-314 -311 0
+-314 -312 0
+-314 -313 0
+-314 -315 0
+-315 -307 0
+-315 -308 0
+-315 -309 0
+-315 -310 0
+-315 -311 0
+-315 -312 0
+-315 -313 0
+-315 -314 0
+307 308 309 310 311 312 313 314 315 0
+-316 -317 0
+-316 -318 0
+-316 -319 0
+-316 -320 0
+-316 -321 0
+-316 -322 0
+-316 -323 0
+-316 -324 0
+-317 -316 0
+-317 -318 0
+-317 -319 0
+-317 -320 0
+-317 -321 0
+-317 -322 0
+-317 -323 0
+-317 -324 0
+-318 -316 0
+-318 -317 0
+-318 -319 0
+-318 -320 0
+-318 -321 0
+-318 -322 0
+-318 -323 0
+-318 -324 0
+-319 -316 0
+-319 -317 0
+-319 -318 0
+-319 -320 0
+-319 -321 0
+-319 -322 0
+-319 -323 0
+-319 -324 0
+-320 -316 0
+-320 -317 0
+-320 -318 0
+-320 -319 0
+-320 -321 0
+-320 -322 0
+-320 -323 0
+-320 -324 0
+-321 -316 0
+-321 -317 0
+-321 -318 0
+-321 -319 0
+-321 -320 0
+-321 -322 0
+-321 -323 0
+-321 -324 0
+-322 -316 0
+-322 -317 0
+-322 -318 0
+-322 -319 0
+-322 -320 0
+-322 -321 0
+-322 -323 0
+-322 -324 0
+-323 -316 0
+-323 -317 0
+-323 -318 0
+-323 -319 0
+-323 -320 0
+-323 -321 0
+-323 -322 0
+-323 -324 0
+-324 -316 0
+-324 -317 0
+-324 -318 0
+-324 -319 0
+-324 -320 0
+-324 -321 0
+-324 -322 0
+-324 -323 0
+316 317 318 319 320 321 322 323 324 0
+-325 -326 0
+-325 -327 0
+-325 -328 0
+-325 -329 0
+-325 -330 0
+-325 -331 0
+-325 -332 0
+-325 -333 0
+-326 -325 0
+-326 -327 0
+-326 -328 0
+-326 -329 0
+-326 -330 0
+-326 -331 0
+-326 -332 0
+-326 -333 0
+-327 -325 0
+-327 -326 0
+-327 -328 0
+-327 -329 0
+-327 -330 0
+-327 -331 0
+-327 -332 0
+-327 -333 0
+-328 -325 0
+-328 -326 0
+-328 -327 0
+-328 -329 0
+-328 -330 0
+-328 -331 0
+-328 -332 0
+-328 -333 0
+-329 -325 0
+-329 -326 0
+-329 -327 0
+-329 -328 0
+-329 -330 0
+-329 -331 0
+-329 -332 0
+-329 -333 0
+-330 -325 0
+-330 -326 0
+-330 -327 0
+-330 -328 0
+-330 -329 0
+-330 -331 0
+-330 -332 0
+-330 -333 0
+-331 -325 0
+-331 -326 0
+-331 -327 0
+-331 -328 0
+-331 -329 0
+-331 -330 0
+-331 -332 0
+-331 -333 0
+-332 -325 0
+-332 -326 0
+-332 -327 0
+-332 -328 0
+-332 -329 0
+-332 -330 0
+-332 -331 0
+-332 -333 0
+-333 -325 0
+-333 -326 0
+-333 -327 0
+-333 -328 0
+-333 -329 0
+-333 -330 0
+-333 -331 0
+-333 -332 0
+325 326 327 328 329 330 331 332 333 0
+-334 -335 0
+-334 -336 0
+-334 -337 0
+-334 -338 0
+-334 -339 0
+-334 -340 0
+-334 -341 0
+-334 -342 0
+-335 -334 0
+-335 -336 0
+-335 -337 0
+-335 -338 0
+-335 -339 0
+-335 -340 0
+-335 -341 0
+-335 -342 0
+-336 -334 0
+-336 -335 0
+-336 -337 0
+-336 -338 0
+-336 -339 0
+-336 -340 0
+-336 -341 0
+-336 -342 0
+-337 -334 0
+-337 -335 0
+-337 -336 0
+-337 -338 0
+-337 -339 0
+-337 -340 0
+-337 -341 0
+-337 -342 0
+-338 -334 0
+-338 -335 0
+-338 -336 0
+-338 -337 0
+-338 -339 0
+-338 -340 0
+-338 -341 0
+-338 -342 0
+-339 -334 0
+-339 -335 0
+-339 -336 0
+-339 -337 0
+-339 -338 0
+-339 -340 0
+-339 -341 0
+-339 -342 0
+-340 -334 0
+-340 -335 0
+-340 -336 0
+-340 -337 0
+-340 -338 0
+-340 -339 0
+-340 -341 0
+-340 -342 0
+-341 -334 0
+-341 -335 0
+-341 -336 0
+-341 -337 0
+-341 -338 0
+-341 -339 0
+-341 -340 0
+-341 -342 0
+-342 -334 0
+-342 -335 0
+-342 -336 0
+-342 -337 0
+-342 -338 0
+-342 -339 0
+-342 -340 0
+-342 -341 0
+334 335 336 337 338 339 340 341 342 0
+-343 -344 0
+-343 -345 0
+-343 -346 0
+-343 -347 0
+-343 -348 0
+-343 -349 0
+-343 -350 0
+-343 -351 0
+-344 -343 0
+-344 -345 0
+-344 -346 0
+-344 -347 0
+-344 -348 0
+-344 -349 0
+-344 -350 0
+-344 -351 0
+-345 -343 0
+-345 -344 0
+-345 -346 0
+-345 -347 0
+-345 -348 0
+-345 -349 0
+-345 -350 0
+-345 -351 0
+-346 -343 0
+-346 -344 0
+-346 -345 0
+-346 -347 0
+-346 -348 0
+-346 -349 0
+-346 -350 0
+-346 -351 0
+-347 -343 0
+-347 -344 0
+-347 -345 0
+-347 -346 0
+-347 -348 0
+-347 -349 0
+-347 -350 0
+-347 -351 0
+-348 -343 0
+-348 -344 0
+-348 -345 0
+-348 -346 0
+-348 -347 0
+-348 -349 0
+-348 -350 0
+-348 -351 0
+-349 -343 0
+-349 -344 0
+-349 -345 0
+-349 -346 0
+-349 -347 0
+-349 -348 0
+-349 -350 0
+-349 -351 0
+-350 -343 0
+-350 -344 0
+-350 -345 0
+-350 -346 0
+-350 -347 0
+-350 -348 0
+-350 -349 0
+-350 -351 0
+-351 -343 0
+-351 -344 0
+-351 -345 0
+-351 -346 0
+-351 -347 0
+-351 -348 0
+-351 -349 0
+-351 -350 0
+343 344 345 346 347 348 349 350 351 0
+-352 -353 0
+-352 -354 0
+-352 -355 0
+-352 -356 0
+-352 -357 0
+-352 -358 0
+-352 -359 0
+-352 -360 0
+-353 -352 0
+-353 -354 0
+-353 -355 0
+-353 -356 0
+-353 -357 0
+-353 -358 0
+-353 -359 0
+-353 -360 0
+-354 -352 0
+-354 -353 0
+-354 -355 0
+-354 -356 0
+-354 -357 0
+-354 -358 0
+-354 -359 0
+-354 -360 0
+-355 -352 0
+-355 -353 0
+-355 -354 0
+-355 -356 0
+-355 -357 0
+-355 -358 0
+-355 -359 0
+-355 -360 0
+-356 -352 0
+-356 -353 0
+-356 -354 0
+-356 -355 0
+-356 -357 0
+-356 -358 0
+-356 -359 0
+-356 -360 0
+-357 -352 0
+-357 -353 0
+-357 -354 0
+-357 -355 0
+-357 -356 0
+-357 -358 0
+-357 -359 0
+-357 -360 0
+-358 -352 0
+-358 -353 0
+-358 -354 0
+-358 -355 0
+-358 -356 0
+-358 -357 0
+-358 -359 0
+-358 -360 0
+-359 -352 0
+-359 -353 0
+-359 -354 0
+-359 -355 0
+-359 -356 0
+-359 -357 0
+-359 -358 0
+-359 -360 0
+-360 -352 0
+-360 -353 0
+-360 -354 0
+-360 -355 0
+-360 -356 0
+-360 -357 0
+-360 -358 0
+-360 -359 0
+352 353 354 355 356 357 358 359 360 0
+-361 -362 0
+-361 -363 0
+-361 -364 0
+-361 -365 0
+-361 -366 0
+-361 -367 0
+-361 -368 0
+-361 -369 0
+-362 -361 0
+-362 -363 0
+-362 -364 0
+-362 -365 0
+-362 -366 0
+-362 -367 0
+-362 -368 0
+-362 -369 0
+-363 -361 0
+-363 -362 0
+-363 -364 0
+-363 -365 0
+-363 -366 0
+-363 -367 0
+-363 -368 0
+-363 -369 0
+-364 -361 0
+-364 -362 0
+-364 -363 0
+-364 -365 0
+-364 -366 0
+-364 -367 0
+-364 -368 0
+-364 -369 0
+-365 -361 0
+-365 -362 0
+-365 -363 0
+-365 -364 0
+-365 -366 0
+-365 -367 0
+-365 -368 0
+-365 -369 0
+-366 -361 0
+-366 -362 0
+-366 -363 0
+-366 -364 0
+-366 -365 0
+-366 -367 0
+-366 -368 0
+-366 -369 0
+-367 -361 0
+-367 -362 0
+-367 -363 0
+-367 -364 0
+-367 -365 0
+-367 -366 0
+-367 -368 0
+-367 -369 0
+-368 -361 0
+-368 -362 0
+-368 -363 0
+-368 -364 0
+-368 -365 0
+-368 -366 0
+-368 -367 0
+-368 -369 0
+-369 -361 0
+-369 -362 0
+-369 -363 0
+-369 -364 0
+-369 -365 0
+-369 -366 0
+-369 -367 0
+-369 -368 0
+361 362 363 364 365 366 367 368 369 0
+-370 -371 0
+-370 -372 0
+-370 -373 0
+-370 -374 0
+-370 -375 0
+-370 -376 0
+-370 -377 0
+-370 -378 0
+-371 -370 0
+-371 -372 0
+-371 -373 0
+-371 -374 0
+-371 -375 0
+-371 -376 0
+-371 -377 0
+-371 -378 0
+-372 -370 0
+-372 -371 0
+-372 -373 0
+-372 -374 0
+-372 -375 0
+-372 -376 0
+-372 -377 0
+-372 -378 0
+-373 -370 0
+-373 -371 0
+-373 -372 0
+-373 -374 0
+-373 -375 0
+-373 -376 0
+-373 -377 0
+-373 -378 0
+-374 -370 0
+-374 -371 0
+-374 -372 0
+-374 -373 0
+-374 -375 0
+-374 -376 0
+-374 -377 0
+-374 -378 0
+-375 -370 0
+-375 -371 0
+-375 -372 0
+-375 -373 0
+-375 -374 0
+-375 -376 0
+-375 -377 0
+-375 -378 0
+-376 -370 0
+-376 -371 0
+-376 -372 0
+-376 -373 0
+-376 -374 0
+-376 -375 0
+-376 -377 0
+-376 -378 0
+-377 -370 0
+-377 -371 0
+-377 -372 0
+-377 -373 0
+-377 -374 0
+-377 -375 0
+-377 -376 0
+-377 -378 0
+-378 -370 0
+-378 -371 0
+-378 -372 0
+-378 -373 0
+-378 -374 0
+-378 -375 0
+-378 -376 0
+-378 -377 0
+370 371 372 373 374 375 376 377 378 0
+-379 -380 0
+-379 -381 0
+-379 -382 0
+-379 -383 0
+-379 -384 0
+-379 -385 0
+-379 -386 0
+-379 -387 0
+-380 -379 0
+-380 -381 0
+-380 -382 0
+-380 -383 0
+-380 -384 0
+-380 -385 0
+-380 -386 0
+-380 -387 0
+-381 -379 0
+-381 -380 0
+-381 -382 0
+-381 -383 0
+-381 -384 0
+-381 -385 0
+-381 -386 0
+-381 -387 0
+-382 -379 0
+-382 -380 0
+-382 -381 0
+-382 -383 0
+-382 -384 0
+-382 -385 0
+-382 -386 0
+-382 -387 0
+-383 -379 0
+-383 -380 0
+-383 -381 0
+-383 -382 0
+-383 -384 0
+-383 -385 0
+-383 -386 0
+-383 -387 0
+-384 -379 0
+-384 -380 0
+-384 -381 0
+-384 -382 0
+-384 -383 0
+-384 -385 0
+-384 -386 0
+-384 -387 0
+-385 -379 0
+-385 -380 0
+-385 -381 0
+-385 -382 0
+-385 -383 0
+-385 -384 0
+-385 -386 0
+-385 -387 0
+-386 -379 0
+-386 -380 0
+-386 -381 0
+-386 -382 0
+-386 -383 0
+-386 -384 0
+-386 -385 0
+-386 -387 0
+-387 -379 0
+-387 -380 0
+-387 -381 0
+-387 -382 0
+-387 -383 0
+-387 -384 0
+-387 -385 0
+-387 -386 0
+379 380 381 382 383 384 385 386 387 0
+-388 -389 0
+-388 -390 0
+-388 -391 0
+-388 -392 0
+-388 -393 0
+-388 -394 0
+-388 -395 0
+-388 -396 0
+-389 -388 0
+-389 -390 0
+-389 -391 0
+-389 -392 0
+-389 -393 0
+-389 -394 0
+-389 -395 0
+-389 -396 0
+-390 -388 0
+-390 -389 0
+-390 -391 0
+-390 -392 0
+-390 -393 0
+-390 -394 0
+-390 -395 0
+-390 -396 0
+-391 -388 0
+-391 -389 0
+-391 -390 0
+-391 -392 0
+-391 -393 0
+-391 -394 0
+-391 -395 0
+-391 -396 0
+-392 -388 0
+-392 -389 0
+-392 -390 0
+-392 -391 0
+-392 -393 0
+-392 -394 0
+-392 -395 0
+-392 -396 0
+-393 -388 0
+-393 -389 0
+-393 -390 0
+-393 -391 0
+-393 -392 0
+-393 -394 0
+-393 -395 0
+-393 -396 0
+-394 -388 0
+-394 -389 0
+-394 -390 0
+-394 -391 0
+-394 -392 0
+-394 -393 0
+-394 -395 0
+-394 -396 0
+-395 -388 0
+-395 -389 0
+-395 -390 0
+-395 -391 0
+-395 -392 0
+-395 -393 0
+-395 -394 0
+-395 -396 0
+-396 -388 0
+-396 -389 0
+-396 -390 0
+-396 -391 0
+-396 -392 0
+-396 -393 0
+-396 -394 0
+-396 -395 0
+388 389 390 391 392 393 394 395 396 0
+-397 -398 0
+-397 -399 0
+-397 -400 0
+-397 -401 0
+-397 -402 0
+-397 -403 0
+-397 -404 0
+-397 -405 0
+-398 -397 0
+-398 -399 0
+-398 -400 0
+-398 -401 0
+-398 -402 0
+-398 -403 0
+-398 -404 0
+-398 -405 0
+-399 -397 0
+-399 -398 0
+-399 -400 0
+-399 -401 0
+-399 -402 0
+-399 -403 0
+-399 -404 0
+-399 -405 0
+-400 -397 0
+-400 -398 0
+-400 -399 0
+-400 -401 0
+-400 -402 0
+-400 -403 0
+-400 -404 0
+-400 -405 0
+-401 -397 0
+-401 -398 0
+-401 -399 0
+-401 -400 0
+-401 -402 0
+-401 -403 0
+-401 -404 0
+-401 -405 0
+-402 -397 0
+-402 -398 0
+-402 -399 0
+-402 -400 0
+-402 -401 0
+-402 -403 0
+-402 -404 0
+-402 -405 0
+-403 -397 0
+-403 -398 0
+-403 -399 0
+-403 -400 0
+-403 -401 0
+-403 -402 0
+-403 -404 0
+-403 -405 0
+-404 -397 0
+-404 -398 0
+-404 -399 0
+-404 -400 0
+-404 -401 0
+-404 -402 0
+-404 -403 0
+-404 -405 0
+-405 -397 0
+-405 -398 0
+-405 -399 0
+-405 -400 0
+-405 -401 0
+-405 -402 0
+-405 -403 0
+-405 -404 0
+397 398 399 400 401 402 403 404 405 0
+-406 -407 0
+-406 -408 0
+-406 -409 0
+-406 -410 0
+-406 -411 0
+-406 -412 0
+-406 -413 0
+-406 -414 0
+-407 -406 0
+-407 -408 0
+-407 -409 0
+-407 -410 0
+-407 -411 0
+-407 -412 0
+-407 -413 0
+-407 -414 0
+-408 -406 0
+-408 -407 0
+-408 -409 0
+-408 -410 0
+-408 -411 0
+-408 -412 0
+-408 -413 0
+-408 -414 0
+-409 -406 0
+-409 -407 0
+-409 -408 0
+-409 -410 0
+-409 -411 0
+-409 -412 0
+-409 -413 0
+-409 -414 0
+-410 -406 0
+-410 -407 0
+-410 -408 0
+-410 -409 0
+-410 -411 0
+-410 -412 0
+-410 -413 0
+-410 -414 0
+-411 -406 0
+-411 -407 0
+-411 -408 0
+-411 -409 0
+-411 -410 0
+-411 -412 0
+-411 -413 0
+-411 -414 0
+-412 -406 0
+-412 -407 0
+-412 -408 0
+-412 -409 0
+-412 -410 0
+-412 -411 0
+-412 -413 0
+-412 -414 0
+-413 -406 0
+-413 -407 0
+-413 -408 0
+-413 -409 0
+-413 -410 0
+-413 -411 0
+-413 -412 0
+-413 -414 0
+-414 -406 0
+-414 -407 0
+-414 -408 0
+-414 -409 0
+-414 -410 0
+-414 -411 0
+-414 -412 0
+-414 -413 0
+406 407 408 409 410 411 412 413 414 0
+-415 -416 0
+-415 -417 0
+-415 -418 0
+-415 -419 0
+-415 -420 0
+-415 -421 0
+-415 -422 0
+-415 -423 0
+-416 -415 0
+-416 -417 0
+-416 -418 0
+-416 -419 0
+-416 -420 0
+-416 -421 0
+-416 -422 0
+-416 -423 0
+-417 -415 0
+-417 -416 0
+-417 -418 0
+-417 -419 0
+-417 -420 0
+-417 -421 0
+-417 -422 0
+-417 -423 0
+-418 -415 0
+-418 -416 0
+-418 -417 0
+-418 -419 0
+-418 -420 0
+-418 -421 0
+-418 -422 0
+-418 -423 0
+-419 -415 0
+-419 -416 0
+-419 -417 0
+-419 -418 0
+-419 -420 0
+-419 -421 0
+-419 -422 0
+-419 -423 0
+-420 -415 0
+-420 -416 0
+-420 -417 0
+-420 -418 0
+-420 -419 0
+-420 -421 0
+-420 -422 0
+-420 -423 0
+-421 -415 0
+-421 -416 0
+-421 -417 0
+-421 -418 0
+-421 -419 0
+-421 -420 0
+-421 -422 0
+-421 -423 0
+-422 -415 0
+-422 -416 0
+-422 -417 0
+-422 -418 0
+-422 -419 0
+-422 -420 0
+-422 -421 0
+-422 -423 0
+-423 -415 0
+-423 -416 0
+-423 -417 0
+-423 -418 0
+-423 -419 0
+-423 -420 0
+-423 -421 0
+-423 -422 0
+415 416 417 418 419 420 421 422 423 0
+-424 -425 0
+-424 -426 0
+-424 -427 0
+-424 -428 0
+-424 -429 0
+-424 -430 0
+-424 -431 0
+-424 -432 0
+-425 -424 0
+-425 -426 0
+-425 -427 0
+-425 -428 0
+-425 -429 0
+-425 -430 0
+-425 -431 0
+-425 -432 0
+-426 -424 0
+-426 -425 0
+-426 -427 0
+-426 -428 0
+-426 -429 0
+-426 -430 0
+-426 -431 0
+-426 -432 0
+-427 -424 0
+-427 -425 0
+-427 -426 0
+-427 -428 0
+-427 -429 0
+-427 -430 0
+-427 -431 0
+-427 -432 0
+-428 -424 0
+-428 -425 0
+-428 -426 0
+-428 -427 0
+-428 -429 0
+-428 -430 0
+-428 -431 0
+-428 -432 0
+-429 -424 0
+-429 -425 0
+-429 -426 0
+-429 -427 0
+-429 -428 0
+-429 -430 0
+-429 -431 0
+-429 -432 0
+-430 -424 0
+-430 -425 0
+-430 -426 0
+-430 -427 0
+-430 -428 0
+-430 -429 0
+-430 -431 0
+-430 -432 0
+-431 -424 0
+-431 -425 0
+-431 -426 0
+-431 -427 0
+-431 -428 0
+-431 -429 0
+-431 -430 0
+-431 -432 0
+-432 -424 0
+-432 -425 0
+-432 -426 0
+-432 -427 0
+-432 -428 0
+-432 -429 0
+-432 -430 0
+-432 -431 0
+424 425 426 427 428 429 430 431 432 0
+-433 -434 0
+-433 -435 0
+-433 -436 0
+-433 -437 0
+-433 -438 0
+-433 -439 0
+-433 -440 0
+-433 -441 0
+-434 -433 0
+-434 -435 0
+-434 -436 0
+-434 -437 0
+-434 -438 0
+-434 -439 0
+-434 -440 0
+-434 -441 0
+-435 -433 0
+-435 -434 0
+-435 -436 0
+-435 -437 0
+-435 -438 0
+-435 -439 0
+-435 -440 0
+-435 -441 0
+-436 -433 0
+-436 -434 0
+-436 -435 0
+-436 -437 0
+-436 -438 0
+-436 -439 0
+-436 -440 0
+-436 -441 0
+-437 -433 0
+-437 -434 0
+-437 -435 0
+-437 -436 0
+-437 -438 0
+-437 -439 0
+-437 -440 0
+-437 -441 0
+-438 -433 0
+-438 -434 0
+-438 -435 0
+-438 -436 0
+-438 -437 0
+-438 -439 0
+-438 -440 0
+-438 -441 0
+-439 -433 0
+-439 -434 0
+-439 -435 0
+-439 -436 0
+-439 -437 0
+-439 -438 0
+-439 -440 0
+-439 -441 0
+-440 -433 0
+-440 -434 0
+-440 -435 0
+-440 -436 0
+-440 -437 0
+-440 -438 0
+-440 -439 0
+-440 -441 0
+-441 -433 0
+-441 -434 0
+-441 -435 0
+-441 -436 0
+-441 -437 0
+-441 -438 0
+-441 -439 0
+-441 -440 0
+433 434 435 436 437 438 439 440 441 0
+-442 -443 0
+-442 -444 0
+-442 -445 0
+-442 -446 0
+-442 -447 0
+-442 -448 0
+-442 -449 0
+-442 -450 0
+-443 -442 0
+-443 -444 0
+-443 -445 0
+-443 -446 0
+-443 -447 0
+-443 -448 0
+-443 -449 0
+-443 -450 0
+-444 -442 0
+-444 -443 0
+-444 -445 0
+-444 -446 0
+-444 -447 0
+-444 -448 0
+-444 -449 0
+-444 -450 0
+-445 -442 0
+-445 -443 0
+-445 -444 0
+-445 -446 0
+-445 -447 0
+-445 -448 0
+-445 -449 0
+-445 -450 0
+-446 -442 0
+-446 -443 0
+-446 -444 0
+-446 -445 0
+-446 -447 0
+-446 -448 0
+-446 -449 0
+-446 -450 0
+-447 -442 0
+-447 -443 0
+-447 -444 0
+-447 -445 0
+-447 -446 0
+-447 -448 0
+-447 -449 0
+-447 -450 0
+-448 -442 0
+-448 -443 0
+-448 -444 0
+-448 -445 0
+-448 -446 0
+-448 -447 0
+-448 -449 0
+-448 -450 0
+-449 -442 0
+-449 -443 0
+-449 -444 0
+-449 -445 0
+-449 -446 0
+-449 -447 0
+-449 -448 0
+-449 -450 0
+-450 -442 0
+-450 -443 0
+-450 -444 0
+-450 -445 0
+-450 -446 0
+-450 -447 0
+-450 -448 0
+-450 -449 0
+442 443 444 445 446 447 448 449 450 0
+-451 -452 0
+-451 -453 0
+-451 -454 0
+-451 -455 0
+-451 -456 0
+-451 -457 0
+-451 -458 0
+-451 -459 0
+-452 -451 0
+-452 -453 0
+-452 -454 0
+-452 -455 0
+-452 -456 0
+-452 -457 0
+-452 -458 0
+-452 -459 0
+-453 -451 0
+-453 -452 0
+-453 -454 0
+-453 -455 0
+-453 -456 0
+-453 -457 0
+-453 -458 0
+-453 -459 0
+-454 -451 0
+-454 -452 0
+-454 -453 0
+-454 -455 0
+-454 -456 0
+-454 -457 0
+-454 -458 0
+-454 -459 0
+-455 -451 0
+-455 -452 0
+-455 -453 0
+-455 -454 0
+-455 -456 0
+-455 -457 0
+-455 -458 0
+-455 -459 0
+-456 -451 0
+-456 -452 0
+-456 -453 0
+-456 -454 0
+-456 -455 0
+-456 -457 0
+-456 -458 0
+-456 -459 0
+-457 -451 0
+-457 -452 0
+-457 -453 0
+-457 -454 0
+-457 -455 0
+-457 -456 0
+-457 -458 0
+-457 -459 0
+-458 -451 0
+-458 -452 0
+-458 -453 0
+-458 -454 0
+-458 -455 0
+-458 -456 0
+-458 -457 0
+-458 -459 0
+-459 -451 0
+-459 -452 0
+-459 -453 0
+-459 -454 0
+-459 -455 0
+-459 -456 0
+-459 -457 0
+-459 -458 0
+451 452 453 454 455 456 457 458 459 0
+-460 -461 0
+-460 -462 0
+-460 -463 0
+-460 -464 0
+-460 -465 0
+-460 -466 0
+-460 -467 0
+-460 -468 0
+-461 -460 0
+-461 -462 0
+-461 -463 0
+-461 -464 0
+-461 -465 0
+-461 -466 0
+-461 -467 0
+-461 -468 0
+-462 -460 0
+-462 -461 0
+-462 -463 0
+-462 -464 0
+-462 -465 0
+-462 -466 0
+-462 -467 0
+-462 -468 0
+-463 -460 0
+-463 -461 0
+-463 -462 0
+-463 -464 0
+-463 -465 0
+-463 -466 0
+-463 -467 0
+-463 -468 0
+-464 -460 0
+-464 -461 0
+-464 -462 0
+-464 -463 0
+-464 -465 0
+-464 -466 0
+-464 -467 0
+-464 -468 0
+-465 -460 0
+-465 -461 0
+-465 -462 0
+-465 -463 0
+-465 -464 0
+-465 -466 0
+-465 -467 0
+-465 -468 0
+-466 -460 0
+-466 -461 0
+-466 -462 0
+-466 -463 0
+-466 -464 0
+-466 -465 0
+-466 -467 0
+-466 -468 0
+-467 -460 0
+-467 -461 0
+-467 -462 0
+-467 -463 0
+-467 -464 0
+-467 -465 0
+-467 -466 0
+-467 -468 0
+-468 -460 0
+-468 -461 0
+-468 -462 0
+-468 -463 0
+-468 -464 0
+-468 -465 0
+-468 -466 0
+-468 -467 0
+460 461 462 463 464 465 466 467 468 0
+-469 -470 0
+-469 -471 0
+-469 -472 0
+-469 -473 0
+-469 -474 0
+-469 -475 0
+-469 -476 0
+-469 -477 0
+-470 -469 0
+-470 -471 0
+-470 -472 0
+-470 -473 0
+-470 -474 0
+-470 -475 0
+-470 -476 0
+-470 -477 0
+-471 -469 0
+-471 -470 0
+-471 -472 0
+-471 -473 0
+-471 -474 0
+-471 -475 0
+-471 -476 0
+-471 -477 0
+-472 -469 0
+-472 -470 0
+-472 -471 0
+-472 -473 0
+-472 -474 0
+-472 -475 0
+-472 -476 0
+-472 -477 0
+-473 -469 0
+-473 -470 0
+-473 -471 0
+-473 -472 0
+-473 -474 0
+-473 -475 0
+-473 -476 0
+-473 -477 0
+-474 -469 0
+-474 -470 0
+-474 -471 0
+-474 -472 0
+-474 -473 0
+-474 -475 0
+-474 -476 0
+-474 -477 0
+-475 -469 0
+-475 -470 0
+-475 -471 0
+-475 -472 0
+-475 -473 0
+-475 -474 0
+-475 -476 0
+-475 -477 0
+-476 -469 0
+-476 -470 0
+-476 -471 0
+-476 -472 0
+-476 -473 0
+-476 -474 0
+-476 -475 0
+-476 -477 0
+-477 -469 0
+-477 -470 0
+-477 -471 0
+-477 -472 0
+-477 -473 0
+-477 -474 0
+-477 -475 0
+-477 -476 0
+469 470 471 472 473 474 475 476 477 0
+-478 -479 0
+-478 -480 0
+-478 -481 0
+-478 -482 0
+-478 -483 0
+-478 -484 0
+-478 -485 0
+-478 -486 0
+-479 -478 0
+-479 -480 0
+-479 -481 0
+-479 -482 0
+-479 -483 0
+-479 -484 0
+-479 -485 0
+-479 -486 0
+-480 -478 0
+-480 -479 0
+-480 -481 0
+-480 -482 0
+-480 -483 0
+-480 -484 0
+-480 -485 0
+-480 -486 0
+-481 -478 0
+-481 -479 0
+-481 -480 0
+-481 -482 0
+-481 -483 0
+-481 -484 0
+-481 -485 0
+-481 -486 0
+-482 -478 0
+-482 -479 0
+-482 -480 0
+-482 -481 0
+-482 -483 0
+-482 -484 0
+-482 -485 0
+-482 -486 0
+-483 -478 0
+-483 -479 0
+-483 -480 0
+-483 -481 0
+-483 -482 0
+-483 -484 0
+-483 -485 0
+-483 -486 0
+-484 -478 0
+-484 -479 0
+-484 -480 0
+-484 -481 0
+-484 -482 0
+-484 -483 0
+-484 -485 0
+-484 -486 0
+-485 -478 0
+-485 -479 0
+-485 -480 0
+-485 -481 0
+-485 -482 0
+-485 -483 0
+-485 -484 0
+-485 -486 0
+-486 -478 0
+-486 -479 0
+-486 -480 0
+-486 -481 0
+-486 -482 0
+-486 -483 0
+-486 -484 0
+-486 -485 0
+478 479 480 481 482 483 484 485 486 0
+-487 -488 0
+-487 -489 0
+-487 -490 0
+-487 -491 0
+-487 -492 0
+-487 -493 0
+-487 -494 0
+-487 -495 0
+-488 -487 0
+-488 -489 0
+-488 -490 0
+-488 -491 0
+-488 -492 0
+-488 -493 0
+-488 -494 0
+-488 -495 0
+-489 -487 0
+-489 -488 0
+-489 -490 0
+-489 -491 0
+-489 -492 0
+-489 -493 0
+-489 -494 0
+-489 -495 0
+-490 -487 0
+-490 -488 0
+-490 -489 0
+-490 -491 0
+-490 -492 0
+-490 -493 0
+-490 -494 0
+-490 -495 0
+-491 -487 0
+-491 -488 0
+-491 -489 0
+-491 -490 0
+-491 -492 0
+-491 -493 0
+-491 -494 0
+-491 -495 0
+-492 -487 0
+-492 -488 0
+-492 -489 0
+-492 -490 0
+-492 -491 0
+-492 -493 0
+-492 -494 0
+-492 -495 0
+-493 -487 0
+-493 -488 0
+-493 -489 0
+-493 -490 0
+-493 -491 0
+-493 -492 0
+-493 -494 0
+-493 -495 0
+-494 -487 0
+-494 -488 0
+-494 -489 0
+-494 -490 0
+-494 -491 0
+-494 -492 0
+-494 -493 0
+-494 -495 0
+-495 -487 0
+-495 -488 0
+-495 -489 0
+-495 -490 0
+-495 -491 0
+-495 -492 0
+-495 -493 0
+-495 -494 0
+487 488 489 490 491 492 493 494 495 0
+-496 -497 0
+-496 -498 0
+-496 -499 0
+-496 -500 0
+-496 -501 0
+-496 -502 0
+-496 -503 0
+-496 -504 0
+-497 -496 0
+-497 -498 0
+-497 -499 0
+-497 -500 0
+-497 -501 0
+-497 -502 0
+-497 -503 0
+-497 -504 0
+-498 -496 0
+-498 -497 0
+-498 -499 0
+-498 -500 0
+-498 -501 0
+-498 -502 0
+-498 -503 0
+-498 -504 0
+-499 -496 0
+-499 -497 0
+-499 -498 0
+-499 -500 0
+-499 -501 0
+-499 -502 0
+-499 -503 0
+-499 -504 0
+-500 -496 0
+-500 -497 0
+-500 -498 0
+-500 -499 0
+-500 -501 0
+-500 -502 0
+-500 -503 0
+-500 -504 0
+-501 -496 0
+-501 -497 0
+-501 -498 0
+-501 -499 0
+-501 -500 0
+-501 -502 0
+-501 -503 0
+-501 -504 0
+-502 -496 0
+-502 -497 0
+-502 -498 0
+-502 -499 0
+-502 -500 0
+-502 -501 0
+-502 -503 0
+-502 -504 0
+-503 -496 0
+-503 -497 0
+-503 -498 0
+-503 -499 0
+-503 -500 0
+-503 -501 0
+-503 -502 0
+-503 -504 0
+-504 -496 0
+-504 -497 0
+-504 -498 0
+-504 -499 0
+-504 -500 0
+-504 -501 0
+-504 -502 0
+-504 -503 0
+496 497 498 499 500 501 502 503 504 0
+-505 -506 0
+-505 -507 0
+-505 -508 0
+-505 -509 0
+-505 -510 0
+-505 -511 0
+-505 -512 0
+-505 -513 0
+-506 -505 0
+-506 -507 0
+-506 -508 0
+-506 -509 0
+-506 -510 0
+-506 -511 0
+-506 -512 0
+-506 -513 0
+-507 -505 0
+-507 -506 0
+-507 -508 0
+-507 -509 0
+-507 -510 0
+-507 -511 0
+-507 -512 0
+-507 -513 0
+-508 -505 0
+-508 -506 0
+-508 -507 0
+-508 -509 0
+-508 -510 0
+-508 -511 0
+-508 -512 0
+-508 -513 0
+-509 -505 0
+-509 -506 0
+-509 -507 0
+-509 -508 0
+-509 -510 0
+-509 -511 0
+-509 -512 0
+-509 -513 0
+-510 -505 0
+-510 -506 0
+-510 -507 0
+-510 -508 0
+-510 -509 0
+-510 -511 0
+-510 -512 0
+-510 -513 0
+-511 -505 0
+-511 -506 0
+-511 -507 0
+-511 -508 0
+-511 -509 0
+-511 -510 0
+-511 -512 0
+-511 -513 0
+-512 -505 0
+-512 -506 0
+-512 -507 0
+-512 -508 0
+-512 -509 0
+-512 -510 0
+-512 -511 0
+-512 -513 0
+-513 -505 0
+-513 -506 0
+-513 -507 0
+-513 -508 0
+-513 -509 0
+-513 -510 0
+-513 -511 0
+-513 -512 0
+505 506 507 508 509 510 511 512 513 0
+-514 -515 0
+-514 -516 0
+-514 -517 0
+-514 -518 0
+-514 -519 0
+-514 -520 0
+-514 -521 0
+-514 -522 0
+-515 -514 0
+-515 -516 0
+-515 -517 0
+-515 -518 0
+-515 -519 0
+-515 -520 0
+-515 -521 0
+-515 -522 0
+-516 -514 0
+-516 -515 0
+-516 -517 0
+-516 -518 0
+-516 -519 0
+-516 -520 0
+-516 -521 0
+-516 -522 0
+-517 -514 0
+-517 -515 0
+-517 -516 0
+-517 -518 0
+-517 -519 0
+-517 -520 0
+-517 -521 0
+-517 -522 0
+-518 -514 0
+-518 -515 0
+-518 -516 0
+-518 -517 0
+-518 -519 0
+-518 -520 0
+-518 -521 0
+-518 -522 0
+-519 -514 0
+-519 -515 0
+-519 -516 0
+-519 -517 0
+-519 -518 0
+-519 -520 0
+-519 -521 0
+-519 -522 0
+-520 -514 0
+-520 -515 0
+-520 -516 0
+-520 -517 0
+-520 -518 0
+-520 -519 0
+-520 -521 0
+-520 -522 0
+-521 -514 0
+-521 -515 0
+-521 -516 0
+-521 -517 0
+-521 -518 0
+-521 -519 0
+-521 -520 0
+-521 -522 0
+-522 -514 0
+-522 -515 0
+-522 -516 0
+-522 -517 0
+-522 -518 0
+-522 -519 0
+-522 -520 0
+-522 -521 0
+514 515 516 517 518 519 520 521 522 0
+-523 -524 0
+-523 -525 0
+-523 -526 0
+-523 -527 0
+-523 -528 0
+-523 -529 0
+-523 -530 0
+-523 -531 0
+-524 -523 0
+-524 -525 0
+-524 -526 0
+-524 -527 0
+-524 -528 0
+-524 -529 0
+-524 -530 0
+-524 -531 0
+-525 -523 0
+-525 -524 0
+-525 -526 0
+-525 -527 0
+-525 -528 0
+-525 -529 0
+-525 -530 0
+-525 -531 0
+-526 -523 0
+-526 -524 0
+-526 -525 0
+-526 -527 0
+-526 -528 0
+-526 -529 0
+-526 -530 0
+-526 -531 0
+-527 -523 0
+-527 -524 0
+-527 -525 0
+-527 -526 0
+-527 -528 0
+-527 -529 0
+-527 -530 0
+-527 -531 0
+-528 -523 0
+-528 -524 0
+-528 -525 0
+-528 -526 0
+-528 -527 0
+-528 -529 0
+-528 -530 0
+-528 -531 0
+-529 -523 0
+-529 -524 0
+-529 -525 0
+-529 -526 0
+-529 -527 0
+-529 -528 0
+-529 -530 0
+-529 -531 0
+-530 -523 0
+-530 -524 0
+-530 -525 0
+-530 -526 0
+-530 -527 0
+-530 -528 0
+-530 -529 0
+-530 -531 0
+-531 -523 0
+-531 -524 0
+-531 -525 0
+-531 -526 0
+-531 -527 0
+-531 -528 0
+-531 -529 0
+-531 -530 0
+523 524 525 526 527 528 529 530 531 0
+-532 -533 0
+-532 -534 0
+-532 -535 0
+-532 -536 0
+-532 -537 0
+-532 -538 0
+-532 -539 0
+-532 -540 0
+-533 -532 0
+-533 -534 0
+-533 -535 0
+-533 -536 0
+-533 -537 0
+-533 -538 0
+-533 -539 0
+-533 -540 0
+-534 -532 0
+-534 -533 0
+-534 -535 0
+-534 -536 0
+-534 -537 0
+-534 -538 0
+-534 -539 0
+-534 -540 0
+-535 -532 0
+-535 -533 0
+-535 -534 0
+-535 -536 0
+-535 -537 0
+-535 -538 0
+-535 -539 0
+-535 -540 0
+-536 -532 0
+-536 -533 0
+-536 -534 0
+-536 -535 0
+-536 -537 0
+-536 -538 0
+-536 -539 0
+-536 -540 0
+-537 -532 0
+-537 -533 0
+-537 -534 0
+-537 -535 0
+-537 -536 0
+-537 -538 0
+-537 -539 0
+-537 -540 0
+-538 -532 0
+-538 -533 0
+-538 -534 0
+-538 -535 0
+-538 -536 0
+-538 -537 0
+-538 -539 0
+-538 -540 0
+-539 -532 0
+-539 -533 0
+-539 -534 0
+-539 -535 0
+-539 -536 0
+-539 -537 0
+-539 -538 0
+-539 -540 0
+-540 -532 0
+-540 -533 0
+-540 -534 0
+-540 -535 0
+-540 -536 0
+-540 -537 0
+-540 -538 0
+-540 -539 0
+532 533 534 535 536 537 538 539 540 0
+-541 -542 0
+-541 -543 0
+-541 -544 0
+-541 -545 0
+-541 -546 0
+-541 -547 0
+-541 -548 0
+-541 -549 0
+-542 -541 0
+-542 -543 0
+-542 -544 0
+-542 -545 0
+-542 -546 0
+-542 -547 0
+-542 -548 0
+-542 -549 0
+-543 -541 0
+-543 -542 0
+-543 -544 0
+-543 -545 0
+-543 -546 0
+-543 -547 0
+-543 -548 0
+-543 -549 0
+-544 -541 0
+-544 -542 0
+-544 -543 0
+-544 -545 0
+-544 -546 0
+-544 -547 0
+-544 -548 0
+-544 -549 0
+-545 -541 0
+-545 -542 0
+-545 -543 0
+-545 -544 0
+-545 -546 0
+-545 -547 0
+-545 -548 0
+-545 -549 0
+-546 -541 0
+-546 -542 0
+-546 -543 0
+-546 -544 0
+-546 -545 0
+-546 -547 0
+-546 -548 0
+-546 -549 0
+-547 -541 0
+-547 -542 0
+-547 -543 0
+-547 -544 0
+-547 -545 0
+-547 -546 0
+-547 -548 0
+-547 -549 0
+-548 -541 0
+-548 -542 0
+-548 -543 0
+-548 -544 0
+-548 -545 0
+-548 -546 0
+-548 -547 0
+-548 -549 0
+-549 -541 0
+-549 -542 0
+-549 -543 0
+-549 -544 0
+-549 -545 0
+-549 -546 0
+-549 -547 0
+-549 -548 0
+541 542 543 544 545 546 547 548 549 0
+-550 -551 0
+-550 -552 0
+-550 -553 0
+-550 -554 0
+-550 -555 0
+-550 -556 0
+-550 -557 0
+-550 -558 0
+-551 -550 0
+-551 -552 0
+-551 -553 0
+-551 -554 0
+-551 -555 0
+-551 -556 0
+-551 -557 0
+-551 -558 0
+-552 -550 0
+-552 -551 0
+-552 -553 0
+-552 -554 0
+-552 -555 0
+-552 -556 0
+-552 -557 0
+-552 -558 0
+-553 -550 0
+-553 -551 0
+-553 -552 0
+-553 -554 0
+-553 -555 0
+-553 -556 0
+-553 -557 0
+-553 -558 0
+-554 -550 0
+-554 -551 0
+-554 -552 0
+-554 -553 0
+-554 -555 0
+-554 -556 0
+-554 -557 0
+-554 -558 0
+-555 -550 0
+-555 -551 0
+-555 -552 0
+-555 -553 0
+-555 -554 0
+-555 -556 0
+-555 -557 0
+-555 -558 0
+-556 -550 0
+-556 -551 0
+-556 -552 0
+-556 -553 0
+-556 -554 0
+-556 -555 0
+-556 -557 0
+-556 -558 0
+-557 -550 0
+-557 -551 0
+-557 -552 0
+-557 -553 0
+-557 -554 0
+-557 -555 0
+-557 -556 0
+-557 -558 0
+-558 -550 0
+-558 -551 0
+-558 -552 0
+-558 -553 0
+-558 -554 0
+-558 -555 0
+-558 -556 0
+-558 -557 0
+550 551 552 553 554 555 556 557 558 0
+-559 -560 0
+-559 -561 0
+-559 -562 0
+-559 -563 0
+-559 -564 0
+-559 -565 0
+-559 -566 0
+-559 -567 0
+-560 -559 0
+-560 -561 0
+-560 -562 0
+-560 -563 0
+-560 -564 0
+-560 -565 0
+-560 -566 0
+-560 -567 0
+-561 -559 0
+-561 -560 0
+-561 -562 0
+-561 -563 0
+-561 -564 0
+-561 -565 0
+-561 -566 0
+-561 -567 0
+-562 -559 0
+-562 -560 0
+-562 -561 0
+-562 -563 0
+-562 -564 0
+-562 -565 0
+-562 -566 0
+-562 -567 0
+-563 -559 0
+-563 -560 0
+-563 -561 0
+-563 -562 0
+-563 -564 0
+-563 -565 0
+-563 -566 0
+-563 -567 0
+-564 -559 0
+-564 -560 0
+-564 -561 0
+-564 -562 0
+-564 -563 0
+-564 -565 0
+-564 -566 0
+-564 -567 0
+-565 -559 0
+-565 -560 0
+-565 -561 0
+-565 -562 0
+-565 -563 0
+-565 -564 0
+-565 -566 0
+-565 -567 0
+-566 -559 0
+-566 -560 0
+-566 -561 0
+-566 -562 0
+-566 -563 0
+-566 -564 0
+-566 -565 0
+-566 -567 0
+-567 -559 0
+-567 -560 0
+-567 -561 0
+-567 -562 0
+-567 -563 0
+-567 -564 0
+-567 -565 0
+-567 -566 0
+559 560 561 562 563 564 565 566 567 0
+-568 -569 0
+-568 -570 0
+-568 -571 0
+-568 -572 0
+-568 -573 0
+-568 -574 0
+-568 -575 0
+-568 -576 0
+-569 -568 0
+-569 -570 0
+-569 -571 0
+-569 -572 0
+-569 -573 0
+-569 -574 0
+-569 -575 0
+-569 -576 0
+-570 -568 0
+-570 -569 0
+-570 -571 0
+-570 -572 0
+-570 -573 0
+-570 -574 0
+-570 -575 0
+-570 -576 0
+-571 -568 0
+-571 -569 0
+-571 -570 0
+-571 -572 0
+-571 -573 0
+-571 -574 0
+-571 -575 0
+-571 -576 0
+-572 -568 0
+-572 -569 0
+-572 -570 0
+-572 -571 0
+-572 -573 0
+-572 -574 0
+-572 -575 0
+-572 -576 0
+-573 -568 0
+-573 -569 0
+-573 -570 0
+-573 -571 0
+-573 -572 0
+-573 -574 0
+-573 -575 0
+-573 -576 0
+-574 -568 0
+-574 -569 0
+-574 -570 0
+-574 -571 0
+-574 -572 0
+-574 -573 0
+-574 -575 0
+-574 -576 0
+-575 -568 0
+-575 -569 0
+-575 -570 0
+-575 -571 0
+-575 -572 0
+-575 -573 0
+-575 -574 0
+-575 -576 0
+-576 -568 0
+-576 -569 0
+-576 -570 0
+-576 -571 0
+-576 -572 0
+-576 -573 0
+-576 -574 0
+-576 -575 0
+568 569 570 571 572 573 574 575 576 0
+-577 -578 0
+-577 -579 0
+-577 -580 0
+-577 -581 0
+-577 -582 0
+-577 -583 0
+-577 -584 0
+-577 -585 0
+-578 -577 0
+-578 -579 0
+-578 -580 0
+-578 -581 0
+-578 -582 0
+-578 -583 0
+-578 -584 0
+-578 -585 0
+-579 -577 0
+-579 -578 0
+-579 -580 0
+-579 -581 0
+-579 -582 0
+-579 -583 0
+-579 -584 0
+-579 -585 0
+-580 -577 0
+-580 -578 0
+-580 -579 0
+-580 -581 0
+-580 -582 0
+-580 -583 0
+-580 -584 0
+-580 -585 0
+-581 -577 0
+-581 -578 0
+-581 -579 0
+-581 -580 0
+-581 -582 0
+-581 -583 0
+-581 -584 0
+-581 -585 0
+-582 -577 0
+-582 -578 0
+-582 -579 0
+-582 -580 0
+-582 -581 0
+-582 -583 0
+-582 -584 0
+-582 -585 0
+-583 -577 0
+-583 -578 0
+-583 -579 0
+-583 -580 0
+-583 -581 0
+-583 -582 0
+-583 -584 0
+-583 -585 0
+-584 -577 0
+-584 -578 0
+-584 -579 0
+-584 -580 0
+-584 -581 0
+-584 -582 0
+-584 -583 0
+-584 -585 0
+-585 -577 0
+-585 -578 0
+-585 -579 0
+-585 -580 0
+-585 -581 0
+-585 -582 0
+-585 -583 0
+-585 -584 0
+577 578 579 580 581 582 583 584 585 0
+-586 -587 0
+-586 -588 0
+-586 -589 0
+-586 -590 0
+-586 -591 0
+-586 -592 0
+-586 -593 0
+-586 -594 0
+-587 -586 0
+-587 -588 0
+-587 -589 0
+-587 -590 0
+-587 -591 0
+-587 -592 0
+-587 -593 0
+-587 -594 0
+-588 -586 0
+-588 -587 0
+-588 -589 0
+-588 -590 0
+-588 -591 0
+-588 -592 0
+-588 -593 0
+-588 -594 0
+-589 -586 0
+-589 -587 0
+-589 -588 0
+-589 -590 0
+-589 -591 0
+-589 -592 0
+-589 -593 0
+-589 -594 0
+-590 -586 0
+-590 -587 0
+-590 -588 0
+-590 -589 0
+-590 -591 0
+-590 -592 0
+-590 -593 0
+-590 -594 0
+-591 -586 0
+-591 -587 0
+-591 -588 0
+-591 -589 0
+-591 -590 0
+-591 -592 0
+-591 -593 0
+-591 -594 0
+-592 -586 0
+-592 -587 0
+-592 -588 0
+-592 -589 0
+-592 -590 0
+-592 -591 0
+-592 -593 0
+-592 -594 0
+-593 -586 0
+-593 -587 0
+-593 -588 0
+-593 -589 0
+-593 -590 0
+-593 -591 0
+-593 -592 0
+-593 -594 0
+-594 -586 0
+-594 -587 0
+-594 -588 0
+-594 -589 0
+-594 -590 0
+-594 -591 0
+-594 -592 0
+-594 -593 0
+586 587 588 589 590 591 592 593 594 0
+-595 -596 0
+-595 -597 0
+-595 -598 0
+-595 -599 0
+-595 -600 0
+-595 -601 0
+-595 -602 0
+-595 -603 0
+-596 -595 0
+-596 -597 0
+-596 -598 0
+-596 -599 0
+-596 -600 0
+-596 -601 0
+-596 -602 0
+-596 -603 0
+-597 -595 0
+-597 -596 0
+-597 -598 0
+-597 -599 0
+-597 -600 0
+-597 -601 0
+-597 -602 0
+-597 -603 0
+-598 -595 0
+-598 -596 0
+-598 -597 0
+-598 -599 0
+-598 -600 0
+-598 -601 0
+-598 -602 0
+-598 -603 0
+-599 -595 0
+-599 -596 0
+-599 -597 0
+-599 -598 0
+-599 -600 0
+-599 -601 0
+-599 -602 0
+-599 -603 0
+-600 -595 0
+-600 -596 0
+-600 -597 0
+-600 -598 0
+-600 -599 0
+-600 -601 0
+-600 -602 0
+-600 -603 0
+-601 -595 0
+-601 -596 0
+-601 -597 0
+-601 -598 0
+-601 -599 0
+-601 -600 0
+-601 -602 0
+-601 -603 0
+-602 -595 0
+-602 -596 0
+-602 -597 0
+-602 -598 0
+-602 -599 0
+-602 -600 0
+-602 -601 0
+-602 -603 0
+-603 -595 0
+-603 -596 0
+-603 -597 0
+-603 -598 0
+-603 -599 0
+-603 -600 0
+-603 -601 0
+-603 -602 0
+595 596 597 598 599 600 601 602 603 0
+-604 -605 0
+-604 -606 0
+-604 -607 0
+-604 -608 0
+-604 -609 0
+-604 -610 0
+-604 -611 0
+-604 -612 0
+-605 -604 0
+-605 -606 0
+-605 -607 0
+-605 -608 0
+-605 -609 0
+-605 -610 0
+-605 -611 0
+-605 -612 0
+-606 -604 0
+-606 -605 0
+-606 -607 0
+-606 -608 0
+-606 -609 0
+-606 -610 0
+-606 -611 0
+-606 -612 0
+-607 -604 0
+-607 -605 0
+-607 -606 0
+-607 -608 0
+-607 -609 0
+-607 -610 0
+-607 -611 0
+-607 -612 0
+-608 -604 0
+-608 -605 0
+-608 -606 0
+-608 -607 0
+-608 -609 0
+-608 -610 0
+-608 -611 0
+-608 -612 0
+-609 -604 0
+-609 -605 0
+-609 -606 0
+-609 -607 0
+-609 -608 0
+-609 -610 0
+-609 -611 0
+-609 -612 0
+-610 -604 0
+-610 -605 0
+-610 -606 0
+-610 -607 0
+-610 -608 0
+-610 -609 0
+-610 -611 0
+-610 -612 0
+-611 -604 0
+-611 -605 0
+-611 -606 0
+-611 -607 0
+-611 -608 0
+-611 -609 0
+-611 -610 0
+-611 -612 0
+-612 -604 0
+-612 -605 0
+-612 -606 0
+-612 -607 0
+-612 -608 0
+-612 -609 0
+-612 -610 0
+-612 -611 0
+604 605 606 607 608 609 610 611 612 0
+-613 -614 0
+-613 -615 0
+-613 -616 0
+-613 -617 0
+-613 -618 0
+-613 -619 0
+-613 -620 0
+-613 -621 0
+-614 -613 0
+-614 -615 0
+-614 -616 0
+-614 -617 0
+-614 -618 0
+-614 -619 0
+-614 -620 0
+-614 -621 0
+-615 -613 0
+-615 -614 0
+-615 -616 0
+-615 -617 0
+-615 -618 0
+-615 -619 0
+-615 -620 0
+-615 -621 0
+-616 -613 0
+-616 -614 0
+-616 -615 0
+-616 -617 0
+-616 -618 0
+-616 -619 0
+-616 -620 0
+-616 -621 0
+-617 -613 0
+-617 -614 0
+-617 -615 0
+-617 -616 0
+-617 -618 0
+-617 -619 0
+-617 -620 0
+-617 -621 0
+-618 -613 0
+-618 -614 0
+-618 -615 0
+-618 -616 0
+-618 -617 0
+-618 -619 0
+-618 -620 0
+-618 -621 0
+-619 -613 0
+-619 -614 0
+-619 -615 0
+-619 -616 0
+-619 -617 0
+-619 -618 0
+-619 -620 0
+-619 -621 0
+-620 -613 0
+-620 -614 0
+-620 -615 0
+-620 -616 0
+-620 -617 0
+-620 -618 0
+-620 -619 0
+-620 -621 0
+-621 -613 0
+-621 -614 0
+-621 -615 0
+-621 -616 0
+-621 -617 0
+-621 -618 0
+-621 -619 0
+-621 -620 0
+613 614 615 616 617 618 619 620 621 0
+-622 -623 0
+-622 -624 0
+-622 -625 0
+-622 -626 0
+-622 -627 0
+-622 -628 0
+-622 -629 0
+-622 -630 0
+-623 -622 0
+-623 -624 0
+-623 -625 0
+-623 -626 0
+-623 -627 0
+-623 -628 0
+-623 -629 0
+-623 -630 0
+-624 -622 0
+-624 -623 0
+-624 -625 0
+-624 -626 0
+-624 -627 0
+-624 -628 0
+-624 -629 0
+-624 -630 0
+-625 -622 0
+-625 -623 0
+-625 -624 0
+-625 -626 0
+-625 -627 0
+-625 -628 0
+-625 -629 0
+-625 -630 0
+-626 -622 0
+-626 -623 0
+-626 -624 0
+-626 -625 0
+-626 -627 0
+-626 -628 0
+-626 -629 0
+-626 -630 0
+-627 -622 0
+-627 -623 0
+-627 -624 0
+-627 -625 0
+-627 -626 0
+-627 -628 0
+-627 -629 0
+-627 -630 0
+-628 -622 0
+-628 -623 0
+-628 -624 0
+-628 -625 0
+-628 -626 0
+-628 -627 0
+-628 -629 0
+-628 -630 0
+-629 -622 0
+-629 -623 0
+-629 -624 0
+-629 -625 0
+-629 -626 0
+-629 -627 0
+-629 -628 0
+-629 -630 0
+-630 -622 0
+-630 -623 0
+-630 -624 0
+-630 -625 0
+-630 -626 0
+-630 -627 0
+-630 -628 0
+-630 -629 0
+622 623 624 625 626 627 628 629 630 0
+-631 -632 0
+-631 -633 0
+-631 -634 0
+-631 -635 0
+-631 -636 0
+-631 -637 0
+-631 -638 0
+-631 -639 0
+-632 -631 0
+-632 -633 0
+-632 -634 0
+-632 -635 0
+-632 -636 0
+-632 -637 0
+-632 -638 0
+-632 -639 0
+-633 -631 0
+-633 -632 0
+-633 -634 0
+-633 -635 0
+-633 -636 0
+-633 -637 0
+-633 -638 0
+-633 -639 0
+-634 -631 0
+-634 -632 0
+-634 -633 0
+-634 -635 0
+-634 -636 0
+-634 -637 0
+-634 -638 0
+-634 -639 0
+-635 -631 0
+-635 -632 0
+-635 -633 0
+-635 -634 0
+-635 -636 0
+-635 -637 0
+-635 -638 0
+-635 -639 0
+-636 -631 0
+-636 -632 0
+-636 -633 0
+-636 -634 0
+-636 -635 0
+-636 -637 0
+-636 -638 0
+-636 -639 0
+-637 -631 0
+-637 -632 0
+-637 -633 0
+-637 -634 0
+-637 -635 0
+-637 -636 0
+-637 -638 0
+-637 -639 0
+-638 -631 0
+-638 -632 0
+-638 -633 0
+-638 -634 0
+-638 -635 0
+-638 -636 0
+-638 -637 0
+-638 -639 0
+-639 -631 0
+-639 -632 0
+-639 -633 0
+-639 -634 0
+-639 -635 0
+-639 -636 0
+-639 -637 0
+-639 -638 0
+631 632 633 634 635 636 637 638 639 0
+-640 -641 0
+-640 -642 0
+-640 -643 0
+-640 -644 0
+-640 -645 0
+-640 -646 0
+-640 -647 0
+-640 -648 0
+-641 -640 0
+-641 -642 0
+-641 -643 0
+-641 -644 0
+-641 -645 0
+-641 -646 0
+-641 -647 0
+-641 -648 0
+-642 -640 0
+-642 -641 0
+-642 -643 0
+-642 -644 0
+-642 -645 0
+-642 -646 0
+-642 -647 0
+-642 -648 0
+-643 -640 0
+-643 -641 0
+-643 -642 0
+-643 -644 0
+-643 -645 0
+-643 -646 0
+-643 -647 0
+-643 -648 0
+-644 -640 0
+-644 -641 0
+-644 -642 0
+-644 -643 0
+-644 -645 0
+-644 -646 0
+-644 -647 0
+-644 -648 0
+-645 -640 0
+-645 -641 0
+-645 -642 0
+-645 -643 0
+-645 -644 0
+-645 -646 0
+-645 -647 0
+-645 -648 0
+-646 -640 0
+-646 -641 0
+-646 -642 0
+-646 -643 0
+-646 -644 0
+-646 -645 0
+-646 -647 0
+-646 -648 0
+-647 -640 0
+-647 -641 0
+-647 -642 0
+-647 -643 0
+-647 -644 0
+-647 -645 0
+-647 -646 0
+-647 -648 0
+-648 -640 0
+-648 -641 0
+-648 -642 0
+-648 -643 0
+-648 -644 0
+-648 -645 0
+-648 -646 0
+-648 -647 0
+640 641 642 643 644 645 646 647 648 0
+-649 -650 0
+-649 -651 0
+-649 -652 0
+-649 -653 0
+-649 -654 0
+-649 -655 0
+-649 -656 0
+-649 -657 0
+-650 -649 0
+-650 -651 0
+-650 -652 0
+-650 -653 0
+-650 -654 0
+-650 -655 0
+-650 -656 0
+-650 -657 0
+-651 -649 0
+-651 -650 0
+-651 -652 0
+-651 -653 0
+-651 -654 0
+-651 -655 0
+-651 -656 0
+-651 -657 0
+-652 -649 0
+-652 -650 0
+-652 -651 0
+-652 -653 0
+-652 -654 0
+-652 -655 0
+-652 -656 0
+-652 -657 0
+-653 -649 0
+-653 -650 0
+-653 -651 0
+-653 -652 0
+-653 -654 0
+-653 -655 0
+-653 -656 0
+-653 -657 0
+-654 -649 0
+-654 -650 0
+-654 -651 0
+-654 -652 0
+-654 -653 0
+-654 -655 0
+-654 -656 0
+-654 -657 0
+-655 -649 0
+-655 -650 0
+-655 -651 0
+-655 -652 0
+-655 -653 0
+-655 -654 0
+-655 -656 0
+-655 -657 0
+-656 -649 0
+-656 -650 0
+-656 -651 0
+-656 -652 0
+-656 -653 0
+-656 -654 0
+-656 -655 0
+-656 -657 0
+-657 -649 0
+-657 -650 0
+-657 -651 0
+-657 -652 0
+-657 -653 0
+-657 -654 0
+-657 -655 0
+-657 -656 0
+649 650 651 652 653 654 655 656 657 0
+-658 -659 0
+-658 -660 0
+-658 -661 0
+-658 -662 0
+-658 -663 0
+-658 -664 0
+-658 -665 0
+-658 -666 0
+-659 -658 0
+-659 -660 0
+-659 -661 0
+-659 -662 0
+-659 -663 0
+-659 -664 0
+-659 -665 0
+-659 -666 0
+-660 -658 0
+-660 -659 0
+-660 -661 0
+-660 -662 0
+-660 -663 0
+-660 -664 0
+-660 -665 0
+-660 -666 0
+-661 -658 0
+-661 -659 0
+-661 -660 0
+-661 -662 0
+-661 -663 0
+-661 -664 0
+-661 -665 0
+-661 -666 0
+-662 -658 0
+-662 -659 0
+-662 -660 0
+-662 -661 0
+-662 -663 0
+-662 -664 0
+-662 -665 0
+-662 -666 0
+-663 -658 0
+-663 -659 0
+-663 -660 0
+-663 -661 0
+-663 -662 0
+-663 -664 0
+-663 -665 0
+-663 -666 0
+-664 -658 0
+-664 -659 0
+-664 -660 0
+-664 -661 0
+-664 -662 0
+-664 -663 0
+-664 -665 0
+-664 -666 0
+-665 -658 0
+-665 -659 0
+-665 -660 0
+-665 -661 0
+-665 -662 0
+-665 -663 0
+-665 -664 0
+-665 -666 0
+-666 -658 0
+-666 -659 0
+-666 -660 0
+-666 -661 0
+-666 -662 0
+-666 -663 0
+-666 -664 0
+-666 -665 0
+658 659 660 661 662 663 664 665 666 0
+-667 -668 0
+-667 -669 0
+-667 -670 0
+-667 -671 0
+-667 -672 0
+-667 -673 0
+-667 -674 0
+-667 -675 0
+-668 -667 0
+-668 -669 0
+-668 -670 0
+-668 -671 0
+-668 -672 0
+-668 -673 0
+-668 -674 0
+-668 -675 0
+-669 -667 0
+-669 -668 0
+-669 -670 0
+-669 -671 0
+-669 -672 0
+-669 -673 0
+-669 -674 0
+-669 -675 0
+-670 -667 0
+-670 -668 0
+-670 -669 0
+-670 -671 0
+-670 -672 0
+-670 -673 0
+-670 -674 0
+-670 -675 0
+-671 -667 0
+-671 -668 0
+-671 -669 0
+-671 -670 0
+-671 -672 0
+-671 -673 0
+-671 -674 0
+-671 -675 0
+-672 -667 0
+-672 -668 0
+-672 -669 0
+-672 -670 0
+-672 -671 0
+-672 -673 0
+-672 -674 0
+-672 -675 0
+-673 -667 0
+-673 -668 0
+-673 -669 0
+-673 -670 0
+-673 -671 0
+-673 -672 0
+-673 -674 0
+-673 -675 0
+-674 -667 0
+-674 -668 0
+-674 -669 0
+-674 -670 0
+-674 -671 0
+-674 -672 0
+-674 -673 0
+-674 -675 0
+-675 -667 0
+-675 -668 0
+-675 -669 0
+-675 -670 0
+-675 -671 0
+-675 -672 0
+-675 -673 0
+-675 -674 0
+667 668 669 670 671 672 673 674 675 0
+-676 -677 0
+-676 -678 0
+-676 -679 0
+-676 -680 0
+-676 -681 0
+-676 -682 0
+-676 -683 0
+-676 -684 0
+-677 -676 0
+-677 -678 0
+-677 -679 0
+-677 -680 0
+-677 -681 0
+-677 -682 0
+-677 -683 0
+-677 -684 0
+-678 -676 0
+-678 -677 0
+-678 -679 0
+-678 -680 0
+-678 -681 0
+-678 -682 0
+-678 -683 0
+-678 -684 0
+-679 -676 0
+-679 -677 0
+-679 -678 0
+-679 -680 0
+-679 -681 0
+-679 -682 0
+-679 -683 0
+-679 -684 0
+-680 -676 0
+-680 -677 0
+-680 -678 0
+-680 -679 0
+-680 -681 0
+-680 -682 0
+-680 -683 0
+-680 -684 0
+-681 -676 0
+-681 -677 0
+-681 -678 0
+-681 -679 0
+-681 -680 0
+-681 -682 0
+-681 -683 0
+-681 -684 0
+-682 -676 0
+-682 -677 0
+-682 -678 0
+-682 -679 0
+-682 -680 0
+-682 -681 0
+-682 -683 0
+-682 -684 0
+-683 -676 0
+-683 -677 0
+-683 -678 0
+-683 -679 0
+-683 -680 0
+-683 -681 0
+-683 -682 0
+-683 -684 0
+-684 -676 0
+-684 -677 0
+-684 -678 0
+-684 -679 0
+-684 -680 0
+-684 -681 0
+-684 -682 0
+-684 -683 0
+676 677 678 679 680 681 682 683 684 0
+-685 -686 0
+-685 -687 0
+-685 -688 0
+-685 -689 0
+-685 -690 0
+-685 -691 0
+-685 -692 0
+-685 -693 0
+-686 -685 0
+-686 -687 0
+-686 -688 0
+-686 -689 0
+-686 -690 0
+-686 -691 0
+-686 -692 0
+-686 -693 0
+-687 -685 0
+-687 -686 0
+-687 -688 0
+-687 -689 0
+-687 -690 0
+-687 -691 0
+-687 -692 0
+-687 -693 0
+-688 -685 0
+-688 -686 0
+-688 -687 0
+-688 -689 0
+-688 -690 0
+-688 -691 0
+-688 -692 0
+-688 -693 0
+-689 -685 0
+-689 -686 0
+-689 -687 0
+-689 -688 0
+-689 -690 0
+-689 -691 0
+-689 -692 0
+-689 -693 0
+-690 -685 0
+-690 -686 0
+-690 -687 0
+-690 -688 0
+-690 -689 0
+-690 -691 0
+-690 -692 0
+-690 -693 0
+-691 -685 0
+-691 -686 0
+-691 -687 0
+-691 -688 0
+-691 -689 0
+-691 -690 0
+-691 -692 0
+-691 -693 0
+-692 -685 0
+-692 -686 0
+-692 -687 0
+-692 -688 0
+-692 -689 0
+-692 -690 0
+-692 -691 0
+-692 -693 0
+-693 -685 0
+-693 -686 0
+-693 -687 0
+-693 -688 0
+-693 -689 0
+-693 -690 0
+-693 -691 0
+-693 -692 0
+685 686 687 688 689 690 691 692 693 0
+-694 -695 0
+-694 -696 0
+-694 -697 0
+-694 -698 0
+-694 -699 0
+-694 -700 0
+-694 -701 0
+-694 -702 0
+-695 -694 0
+-695 -696 0
+-695 -697 0
+-695 -698 0
+-695 -699 0
+-695 -700 0
+-695 -701 0
+-695 -702 0
+-696 -694 0
+-696 -695 0
+-696 -697 0
+-696 -698 0
+-696 -699 0
+-696 -700 0
+-696 -701 0
+-696 -702 0
+-697 -694 0
+-697 -695 0
+-697 -696 0
+-697 -698 0
+-697 -699 0
+-697 -700 0
+-697 -701 0
+-697 -702 0
+-698 -694 0
+-698 -695 0
+-698 -696 0
+-698 -697 0
+-698 -699 0
+-698 -700 0
+-698 -701 0
+-698 -702 0
+-699 -694 0
+-699 -695 0
+-699 -696 0
+-699 -697 0
+-699 -698 0
+-699 -700 0
+-699 -701 0
+-699 -702 0
+-700 -694 0
+-700 -695 0
+-700 -696 0
+-700 -697 0
+-700 -698 0
+-700 -699 0
+-700 -701 0
+-700 -702 0
+-701 -694 0
+-701 -695 0
+-701 -696 0
+-701 -697 0
+-701 -698 0
+-701 -699 0
+-701 -700 0
+-701 -702 0
+-702 -694 0
+-702 -695 0
+-702 -696 0
+-702 -697 0
+-702 -698 0
+-702 -699 0
+-702 -700 0
+-702 -701 0
+694 695 696 697 698 699 700 701 702 0
+-703 -704 0
+-703 -705 0
+-703 -706 0
+-703 -707 0
+-703 -708 0
+-703 -709 0
+-703 -710 0
+-703 -711 0
+-704 -703 0
+-704 -705 0
+-704 -706 0
+-704 -707 0
+-704 -708 0
+-704 -709 0
+-704 -710 0
+-704 -711 0
+-705 -703 0
+-705 -704 0
+-705 -706 0
+-705 -707 0
+-705 -708 0
+-705 -709 0
+-705 -710 0
+-705 -711 0
+-706 -703 0
+-706 -704 0
+-706 -705 0
+-706 -707 0
+-706 -708 0
+-706 -709 0
+-706 -710 0
+-706 -711 0
+-707 -703 0
+-707 -704 0
+-707 -705 0
+-707 -706 0
+-707 -708 0
+-707 -709 0
+-707 -710 0
+-707 -711 0
+-708 -703 0
+-708 -704 0
+-708 -705 0
+-708 -706 0
+-708 -707 0
+-708 -709 0
+-708 -710 0
+-708 -711 0
+-709 -703 0
+-709 -704 0
+-709 -705 0
+-709 -706 0
+-709 -707 0
+-709 -708 0
+-709 -710 0
+-709 -711 0
+-710 -703 0
+-710 -704 0
+-710 -705 0
+-710 -706 0
+-710 -707 0
+-710 -708 0
+-710 -709 0
+-710 -711 0
+-711 -703 0
+-711 -704 0
+-711 -705 0
+-711 -706 0
+-711 -707 0
+-711 -708 0
+-711 -709 0
+-711 -710 0
+703 704 705 706 707 708 709 710 711 0
+-712 -713 0
+-712 -714 0
+-712 -715 0
+-712 -716 0
+-712 -717 0
+-712 -718 0
+-712 -719 0
+-712 -720 0
+-713 -712 0
+-713 -714 0
+-713 -715 0
+-713 -716 0
+-713 -717 0
+-713 -718 0
+-713 -719 0
+-713 -720 0
+-714 -712 0
+-714 -713 0
+-714 -715 0
+-714 -716 0
+-714 -717 0
+-714 -718 0
+-714 -719 0
+-714 -720 0
+-715 -712 0
+-715 -713 0
+-715 -714 0
+-715 -716 0
+-715 -717 0
+-715 -718 0
+-715 -719 0
+-715 -720 0
+-716 -712 0
+-716 -713 0
+-716 -714 0
+-716 -715 0
+-716 -717 0
+-716 -718 0
+-716 -719 0
+-716 -720 0
+-717 -712 0
+-717 -713 0
+-717 -714 0
+-717 -715 0
+-717 -716 0
+-717 -718 0
+-717 -719 0
+-717 -720 0
+-718 -712 0
+-718 -713 0
+-718 -714 0
+-718 -715 0
+-718 -716 0
+-718 -717 0
+-718 -719 0
+-718 -720 0
+-719 -712 0
+-719 -713 0
+-719 -714 0
+-719 -715 0
+-719 -716 0
+-719 -717 0
+-719 -718 0
+-719 -720 0
+-720 -712 0
+-720 -713 0
+-720 -714 0
+-720 -715 0
+-720 -716 0
+-720 -717 0
+-720 -718 0
+-720 -719 0
+712 713 714 715 716 717 718 719 720 0
+-721 -722 0
+-721 -723 0
+-721 -724 0
+-721 -725 0
+-721 -726 0
+-721 -727 0
+-721 -728 0
+-721 -729 0
+-722 -721 0
+-722 -723 0
+-722 -724 0
+-722 -725 0
+-722 -726 0
+-722 -727 0
+-722 -728 0
+-722 -729 0
+-723 -721 0
+-723 -722 0
+-723 -724 0
+-723 -725 0
+-723 -726 0
+-723 -727 0
+-723 -728 0
+-723 -729 0
+-724 -721 0
+-724 -722 0
+-724 -723 0
+-724 -725 0
+-724 -726 0
+-724 -727 0
+-724 -728 0
+-724 -729 0
+-725 -721 0
+-725 -722 0
+-725 -723 0
+-725 -724 0
+-725 -726 0
+-725 -727 0
+-725 -728 0
+-725 -729 0
+-726 -721 0
+-726 -722 0
+-726 -723 0
+-726 -724 0
+-726 -725 0
+-726 -727 0
+-726 -728 0
+-726 -729 0
+-727 -721 0
+-727 -722 0
+-727 -723 0
+-727 -724 0
+-727 -725 0
+-727 -726 0
+-727 -728 0
+-727 -729 0
+-728 -721 0
+-728 -722 0
+-728 -723 0
+-728 -724 0
+-728 -725 0
+-728 -726 0
+-728 -727 0
+-728 -729 0
+-729 -721 0
+-729 -722 0
+-729 -723 0
+-729 -724 0
+-729 -725 0
+-729 -726 0
+-729 -727 0
+-729 -728 0
+721 722 723 724 725 726 727 728 729 0
+2 0
+18 0
+30 0
+53 0
+105 0
+112 0
+149 0
+196 0
+234 0
+262 0
+323 0
+331 0
+369 0
+381 0
+408 0
+482 0
+533 0
+564 0
+584 0
+604 0
+626 0
+653 0
+659 0
+692 0
+-1 -10 0
+-2 -11 0
+-3 -12 0
+-4 -13 0
+-5 -14 0
+-6 -15 0
+-7 -16 0
+-8 -17 0
+-9 -18 0
+-1 -19 0
+-2 -20 0
+-3 -21 0
+-4 -22 0
+-5 -23 0
+-6 -24 0
+-7 -25 0
+-8 -26 0
+-9 -27 0
+-1 -28 0
+-2 -29 0
+-3 -30 0
+-4 -31 0
+-5 -32 0
+-6 -33 0
+-7 -34 0
+-8 -35 0
+-9 -36 0
+-1 -37 0
+-2 -38 0
+-3 -39 0
+-4 -40 0
+-5 -41 0
+-6 -42 0
+-7 -43 0
+-8 -44 0
+-9 -45 0
+-1 -46 0
+-2 -47 0
+-3 -48 0
+-4 -49 0
+-5 -50 0
+-6 -51 0
+-7 -52 0
+-8 -53 0
+-9 -54 0
+-1 -55 0
+-2 -56 0
+-3 -57 0
+-4 -58 0
+-5 -59 0
+-6 -60 0
+-7 -61 0
+-8 -62 0
+-9 -63 0
+-1 -64 0
+-2 -65 0
+-3 -66 0
+-4 -67 0
+-5 -68 0
+-6 -69 0
+-7 -70 0
+-8 -71 0
+-9 -72 0
+-1 -73 0
+-2 -74 0
+-3 -75 0
+-4 -76 0
+-5 -77 0
+-6 -78 0
+-7 -79 0
+-8 -80 0
+-9 -81 0
+-10 -1 0
+-11 -2 0
+-12 -3 0
+-13 -4 0
+-14 -5 0
+-15 -6 0
+-16 -7 0
+-17 -8 0
+-18 -9 0
+-10 -19 0
+-11 -20 0
+-12 -21 0
+-13 -22 0
+-14 -23 0
+-15 -24 0
+-16 -25 0
+-17 -26 0
+-18 -27 0
+-10 -28 0
+-11 -29 0
+-12 -30 0
+-13 -31 0
+-14 -32 0
+-15 -33 0
+-16 -34 0
+-17 -35 0
+-18 -36 0
+-10 -37 0
+-11 -38 0
+-12 -39 0
+-13 -40 0
+-14 -41 0
+-15 -42 0
+-16 -43 0
+-17 -44 0
+-18 -45 0
+-10 -46 0
+-11 -47 0
+-12 -48 0
+-13 -49 0
+-14 -50 0
+-15 -51 0
+-16 -52 0
+-17 -53 0
+-18 -54 0
+-10 -55 0
+-11 -56 0
+-12 -57 0
+-13 -58 0
+-14 -59 0
+-15 -60 0
+-16 -61 0
+-17 -62 0
+-18 -63 0
+-10 -64 0
+-11 -65 0
+-12 -66 0
+-13 -67 0
+-14 -68 0
+-15 -69 0
+-16 -70 0
+-17 -71 0
+-18 -72 0
+-10 -73 0
+-11 -74 0
+-12 -75 0
+-13 -76 0
+-14 -77 0
+-15 -78 0
+-16 -79 0
+-17 -80 0
+-18 -81 0
+-19 -1 0
+-20 -2 0
+-21 -3 0
+-22 -4 0
+-23 -5 0
+-24 -6 0
+-25 -7 0
+-26 -8 0
+-27 -9 0
+-19 -10 0
+-20 -11 0
+-21 -12 0
+-22 -13 0
+-23 -14 0
+-24 -15 0
+-25 -16 0
+-26 -17 0
+-27 -18 0
+-19 -28 0
+-20 -29 0
+-21 -30 0
+-22 -31 0
+-23 -32 0
+-24 -33 0
+-25 -34 0
+-26 -35 0
+-27 -36 0
+-19 -37 0
+-20 -38 0
+-21 -39 0
+-22 -40 0
+-23 -41 0
+-24 -42 0
+-25 -43 0
+-26 -44 0
+-27 -45 0
+-19 -46 0
+-20 -47 0
+-21 -48 0
+-22 -49 0
+-23 -50 0
+-24 -51 0
+-25 -52 0
+-26 -53 0
+-27 -54 0
+-19 -55 0
+-20 -56 0
+-21 -57 0
+-22 -58 0
+-23 -59 0
+-24 -60 0
+-25 -61 0
+-26 -62 0
+-27 -63 0
+-19 -64 0
+-20 -65 0
+-21 -66 0
+-22 -67 0
+-23 -68 0
+-24 -69 0
+-25 -70 0
+-26 -71 0
+-27 -72 0
+-19 -73 0
+-20 -74 0
+-21 -75 0
+-22 -76 0
+-23 -77 0
+-24 -78 0
+-25 -79 0
+-26 -80 0
+-27 -81 0
+-28 -1 0
+-29 -2 0
+-30 -3 0
+-31 -4 0
+-32 -5 0
+-33 -6 0
+-34 -7 0
+-35 -8 0
+-36 -9 0
+-28 -10 0
+-29 -11 0
+-30 -12 0
+-31 -13 0
+-32 -14 0
+-33 -15 0
+-34 -16 0
+-35 -17 0
+-36 -18 0
+-28 -19 0
+-29 -20 0
+-30 -21 0
+-31 -22 0
+-32 -23 0
+-33 -24 0
+-34 -25 0
+-35 -26 0
+-36 -27 0
+-28 -37 0
+-29 -38 0
+-30 -39 0
+-31 -40 0
+-32 -41 0
+-33 -42 0
+-34 -43 0
+-35 -44 0
+-36 -45 0
+-28 -46 0
+-29 -47 0
+-30 -48 0
+-31 -49 0
+-32 -50 0
+-33 -51 0
+-34 -52 0
+-35 -53 0
+-36 -54 0
+-28 -55 0
+-29 -56 0
+-30 -57 0
+-31 -58 0
+-32 -59 0
+-33 -60 0
+-34 -61 0
+-35 -62 0
+-36 -63 0
+-28 -64 0
+-29 -65 0
+-30 -66 0
+-31 -67 0
+-32 -68 0
+-33 -69 0
+-34 -70 0
+-35 -71 0
+-36 -72 0
+-28 -73 0
+-29 -74 0
+-30 -75 0
+-31 -76 0
+-32 -77 0
+-33 -78 0
+-34 -79 0
+-35 -80 0
+-36 -81 0
+-37 -1 0
+-38 -2 0
+-39 -3 0
+-40 -4 0
+-41 -5 0
+-42 -6 0
+-43 -7 0
+-44 -8 0
+-45 -9 0
+-37 -10 0
+-38 -11 0
+-39 -12 0
+-40 -13 0
+-41 -14 0
+-42 -15 0
+-43 -16 0
+-44 -17 0
+-45 -18 0
+-37 -19 0
+-38 -20 0
+-39 -21 0
+-40 -22 0
+-41 -23 0
+-42 -24 0
+-43 -25 0
+-44 -26 0
+-45 -27 0
+-37 -28 0
+-38 -29 0
+-39 -30 0
+-40 -31 0
+-41 -32 0
+-42 -33 0
+-43 -34 0
+-44 -35 0
+-45 -36 0
+-37 -46 0
+-38 -47 0
+-39 -48 0
+-40 -49 0
+-41 -50 0
+-42 -51 0
+-43 -52 0
+-44 -53 0
+-45 -54 0
+-37 -55 0
+-38 -56 0
+-39 -57 0
+-40 -58 0
+-41 -59 0
+-42 -60 0
+-43 -61 0
+-44 -62 0
+-45 -63 0
+-37 -64 0
+-38 -65 0
+-39 -66 0
+-40 -67 0
+-41 -68 0
+-42 -69 0
+-43 -70 0
+-44 -71 0
+-45 -72 0
+-37 -73 0
+-38 -74 0
+-39 -75 0
+-40 -76 0
+-41 -77 0
+-42 -78 0
+-43 -79 0
+-44 -80 0
+-45 -81 0
+-46 -1 0
+-47 -2 0
+-48 -3 0
+-49 -4 0
+-50 -5 0
+-51 -6 0
+-52 -7 0
+-53 -8 0
+-54 -9 0
+-46 -10 0
+-47 -11 0
+-48 -12 0
+-49 -13 0
+-50 -14 0
+-51 -15 0
+-52 -16 0
+-53 -17 0
+-54 -18 0
+-46 -19 0
+-47 -20 0
+-48 -21 0
+-49 -22 0
+-50 -23 0
+-51 -24 0
+-52 -25 0
+-53 -26 0
+-54 -27 0
+-46 -28 0
+-47 -29 0
+-48 -30 0
+-49 -31 0
+-50 -32 0
+-51 -33 0
+-52 -34 0
+-53 -35 0
+-54 -36 0
+-46 -37 0
+-47 -38 0
+-48 -39 0
+-49 -40 0
+-50 -41 0
+-51 -42 0
+-52 -43 0
+-53 -44 0
+-54 -45 0
+-46 -55 0
+-47 -56 0
+-48 -57 0
+-49 -58 0
+-50 -59 0
+-51 -60 0
+-52 -61 0
+-53 -62 0
+-54 -63 0
+-46 -64 0
+-47 -65 0
+-48 -66 0
+-49 -67 0
+-50 -68 0
+-51 -69 0
+-52 -70 0
+-53 -71 0
+-54 -72 0
+-46 -73 0
+-47 -74 0
+-48 -75 0
+-49 -76 0
+-50 -77 0
+-51 -78 0
+-52 -79 0
+-53 -80 0
+-54 -81 0
+-55 -1 0
+-56 -2 0
+-57 -3 0
+-58 -4 0
+-59 -5 0
+-60 -6 0
+-61 -7 0
+-62 -8 0
+-63 -9 0
+-55 -10 0
+-56 -11 0
+-57 -12 0
+-58 -13 0
+-59 -14 0
+-60 -15 0
+-61 -16 0
+-62 -17 0
+-63 -18 0
+-55 -19 0
+-56 -20 0
+-57 -21 0
+-58 -22 0
+-59 -23 0
+-60 -24 0
+-61 -25 0
+-62 -26 0
+-63 -27 0
+-55 -28 0
+-56 -29 0
+-57 -30 0
+-58 -31 0
+-59 -32 0
+-60 -33 0
+-61 -34 0
+-62 -35 0
+-63 -36 0
+-55 -37 0
+-56 -38 0
+-57 -39 0
+-58 -40 0
+-59 -41 0
+-60 -42 0
+-61 -43 0
+-62 -44 0
+-63 -45 0
+-55 -46 0
+-56 -47 0
+-57 -48 0
+-58 -49 0
+-59 -50 0
+-60 -51 0
+-61 -52 0
+-62 -53 0
+-63 -54 0
+-55 -64 0
+-56 -65 0
+-57 -66 0
+-58 -67 0
+-59 -68 0
+-60 -69 0
+-61 -70 0
+-62 -71 0
+-63 -72 0
+-55 -73 0
+-56 -74 0
+-57 -75 0
+-58 -76 0
+-59 -77 0
+-60 -78 0
+-61 -79 0
+-62 -80 0
+-63 -81 0
+-64 -1 0
+-65 -2 0
+-66 -3 0
+-67 -4 0
+-68 -5 0
+-69 -6 0
+-70 -7 0
+-71 -8 0
+-72 -9 0
+-64 -10 0
+-65 -11 0
+-66 -12 0
+-67 -13 0
+-68 -14 0
+-69 -15 0
+-70 -16 0
+-71 -17 0
+-72 -18 0
+-64 -19 0
+-65 -20 0
+-66 -21 0
+-67 -22 0
+-68 -23 0
+-69 -24 0
+-70 -25 0
+-71 -26 0
+-72 -27 0
+-64 -28 0
+-65 -29 0
+-66 -30 0
+-67 -31 0
+-68 -32 0
+-69 -33 0
+-70 -34 0
+-71 -35 0
+-72 -36 0
+-64 -37 0
+-65 -38 0
+-66 -39 0
+-67 -40 0
+-68 -41 0
+-69 -42 0
+-70 -43 0
+-71 -44 0
+-72 -45 0
+-64 -46 0
+-65 -47 0
+-66 -48 0
+-67 -49 0
+-68 -50 0
+-69 -51 0
+-70 -52 0
+-71 -53 0
+-72 -54 0
+-64 -55 0
+-65 -56 0
+-66 -57 0
+-67 -58 0
+-68 -59 0
+-69 -60 0
+-70 -61 0
+-71 -62 0
+-72 -63 0
+-64 -73 0
+-65 -74 0
+-66 -75 0
+-67 -76 0
+-68 -77 0
+-69 -78 0
+-70 -79 0
+-71 -80 0
+-72 -81 0
+-73 -1 0
+-74 -2 0
+-75 -3 0
+-76 -4 0
+-77 -5 0
+-78 -6 0
+-79 -7 0
+-80 -8 0
+-81 -9 0
+-73 -10 0
+-74 -11 0
+-75 -12 0
+-76 -13 0
+-77 -14 0
+-78 -15 0
+-79 -16 0
+-80 -17 0
+-81 -18 0
+-73 -19 0
+-74 -20 0
+-75 -21 0
+-76 -22 0
+-77 -23 0
+-78 -24 0
+-79 -25 0
+-80 -26 0
+-81 -27 0
+-73 -28 0
+-74 -29 0
+-75 -30 0
+-76 -31 0
+-77 -32 0
+-78 -33 0
+-79 -34 0
+-80 -35 0
+-81 -36 0
+-73 -37 0
+-74 -38 0
+-75 -39 0
+-76 -40 0
+-77 -41 0
+-78 -42 0
+-79 -43 0
+-80 -44 0
+-81 -45 0
+-73 -46 0
+-74 -47 0
+-75 -48 0
+-76 -49 0
+-77 -50 0
+-78 -51 0
+-79 -52 0
+-80 -53 0
+-81 -54 0
+-73 -55 0
+-74 -56 0
+-75 -57 0
+-76 -58 0
+-77 -59 0
+-78 -60 0
+-79 -61 0
+-80 -62 0
+-81 -63 0
+-73 -64 0
+-74 -65 0
+-75 -66 0
+-76 -67 0
+-77 -68 0
+-78 -69 0
+-79 -70 0
+-80 -71 0
+-81 -72 0
+-82 -91 0
+-83 -92 0
+-84 -93 0
+-85 -94 0
+-86 -95 0
+-87 -96 0
+-88 -97 0
+-89 -98 0
+-90 -99 0
+-82 -100 0
+-83 -101 0
+-84 -102 0
+-85 -103 0
+-86 -104 0
+-87 -105 0
+-88 -106 0
+-89 -107 0
+-90 -108 0
+-82 -109 0
+-83 -110 0
+-84 -111 0
+-85 -112 0
+-86 -113 0
+-87 -114 0
+-88 -115 0
+-89 -116 0
+-90 -117 0
+-82 -118 0
+-83 -119 0
+-84 -120 0
+-85 -121 0
+-86 -122 0
+-87 -123 0
+-88 -124 0
+-89 -125 0
+-90 -126 0
+-82 -127 0
+-83 -128 0
+-84 -129 0
+-85 -130 0
+-86 -131 0
+-87 -132 0
+-88 -133 0
+-89 -134 0
+-90 -135 0
+-82 -136 0
+-83 -137 0
+-84 -138 0
+-85 -139 0
+-86 -140 0
+-87 -141 0
+-88 -142 0
+-89 -143 0
+-90 -144 0
+-82 -145 0
+-83 -146 0
+-84 -147 0
+-85 -148 0
+-86 -149 0
+-87 -150 0
+-88 -151 0
+-89 -152 0
+-90 -153 0
+-82 -154 0
+-83 -155 0
+-84 -156 0
+-85 -157 0
+-86 -158 0
+-87 -159 0
+-88 -160 0
+-89 -161 0
+-90 -162 0
+-91 -82 0
+-92 -83 0
+-93 -84 0
+-94 -85 0
+-95 -86 0
+-96 -87 0
+-97 -88 0
+-98 -89 0
+-99 -90 0
+-91 -100 0
+-92 -101 0
+-93 -102 0
+-94 -103 0
+-95 -104 0
+-96 -105 0
+-97 -106 0
+-98 -107 0
+-99 -108 0
+-91 -109 0
+-92 -110 0
+-93 -111 0
+-94 -112 0
+-95 -113 0
+-96 -114 0
+-97 -115 0
+-98 -116 0
+-99 -117 0
+-91 -118 0
+-92 -119 0
+-93 -120 0
+-94 -121 0
+-95 -122 0
+-96 -123 0
+-97 -124 0
+-98 -125 0
+-99 -126 0
+-91 -127 0
+-92 -128 0
+-93 -129 0
+-94 -130 0
+-95 -131 0
+-96 -132 0
+-97 -133 0
+-98 -134 0
+-99 -135 0
+-91 -136 0
+-92 -137 0
+-93 -138 0
+-94 -139 0
+-95 -140 0
+-96 -141 0
+-97 -142 0
+-98 -143 0
+-99 -144 0
+-91 -145 0
+-92 -146 0
+-93 -147 0
+-94 -148 0
+-95 -149 0
+-96 -150 0
+-97 -151 0
+-98 -152 0
+-99 -153 0
+-91 -154 0
+-92 -155 0
+-93 -156 0
+-94 -157 0
+-95 -158 0
+-96 -159 0
+-97 -160 0
+-98 -161 0
+-99 -162 0
+-100 -82 0
+-101 -83 0
+-102 -84 0
+-103 -85 0
+-104 -86 0
+-105 -87 0
+-106 -88 0
+-107 -89 0
+-108 -90 0
+-100 -91 0
+-101 -92 0
+-102 -93 0
+-103 -94 0
+-104 -95 0
+-105 -96 0
+-106 -97 0
+-107 -98 0
+-108 -99 0
+-100 -109 0
+-101 -110 0
+-102 -111 0
+-103 -112 0
+-104 -113 0
+-105 -114 0
+-106 -115 0
+-107 -116 0
+-108 -117 0
+-100 -118 0
+-101 -119 0
+-102 -120 0
+-103 -121 0
+-104 -122 0
+-105 -123 0
+-106 -124 0
+-107 -125 0
+-108 -126 0
+-100 -127 0
+-101 -128 0
+-102 -129 0
+-103 -130 0
+-104 -131 0
+-105 -132 0
+-106 -133 0
+-107 -134 0
+-108 -135 0
+-100 -136 0
+-101 -137 0
+-102 -138 0
+-103 -139 0
+-104 -140 0
+-105 -141 0
+-106 -142 0
+-107 -143 0
+-108 -144 0
+-100 -145 0
+-101 -146 0
+-102 -147 0
+-103 -148 0
+-104 -149 0
+-105 -150 0
+-106 -151 0
+-107 -152 0
+-108 -153 0
+-100 -154 0
+-101 -155 0
+-102 -156 0
+-103 -157 0
+-104 -158 0
+-105 -159 0
+-106 -160 0
+-107 -161 0
+-108 -162 0
+-109 -82 0
+-110 -83 0
+-111 -84 0
+-112 -85 0
+-113 -86 0
+-114 -87 0
+-115 -88 0
+-116 -89 0
+-117 -90 0
+-109 -91 0
+-110 -92 0
+-111 -93 0
+-112 -94 0
+-113 -95 0
+-114 -96 0
+-115 -97 0
+-116 -98 0
+-117 -99 0
+-109 -100 0
+-110 -101 0
+-111 -102 0
+-112 -103 0
+-113 -104 0
+-114 -105 0
+-115 -106 0
+-116 -107 0
+-117 -108 0
+-109 -118 0
+-110 -119 0
+-111 -120 0
+-112 -121 0
+-113 -122 0
+-114 -123 0
+-115 -124 0
+-116 -125 0
+-117 -126 0
+-109 -127 0
+-110 -128 0
+-111 -129 0
+-112 -130 0
+-113 -131 0
+-114 -132 0
+-115 -133 0
+-116 -134 0
+-117 -135 0
+-109 -136 0
+-110 -137 0
+-111 -138 0
+-112 -139 0
+-113 -140 0
+-114 -141 0
+-115 -142 0
+-116 -143 0
+-117 -144 0
+-109 -145 0
+-110 -146 0
+-111 -147 0
+-112 -148 0
+-113 -149 0
+-114 -150 0
+-115 -151 0
+-116 -152 0
+-117 -153 0
+-109 -154 0
+-110 -155 0
+-111 -156 0
+-112 -157 0
+-113 -158 0
+-114 -159 0
+-115 -160 0
+-116 -161 0
+-117 -162 0
+-118 -82 0
+-119 -83 0
+-120 -84 0
+-121 -85 0
+-122 -86 0
+-123 -87 0
+-124 -88 0
+-125 -89 0
+-126 -90 0
+-118 -91 0
+-119 -92 0
+-120 -93 0
+-121 -94 0
+-122 -95 0
+-123 -96 0
+-124 -97 0
+-125 -98 0
+-126 -99 0
+-118 -100 0
+-119 -101 0
+-120 -102 0
+-121 -103 0
+-122 -104 0
+-123 -105 0
+-124 -106 0
+-125 -107 0
+-126 -108 0
+-118 -109 0
+-119 -110 0
+-120 -111 0
+-121 -112 0
+-122 -113 0
+-123 -114 0
+-124 -115 0
+-125 -116 0
+-126 -117 0
+-118 -127 0
+-119 -128 0
+-120 -129 0
+-121 -130 0
+-122 -131 0
+-123 -132 0
+-124 -133 0
+-125 -134 0
+-126 -135 0
+-118 -136 0
+-119 -137 0
+-120 -138 0
+-121 -139 0
+-122 -140 0
+-123 -141 0
+-124 -142 0
+-125 -143 0
+-126 -144 0
+-118 -145 0
+-119 -146 0
+-120 -147 0
+-121 -148 0
+-122 -149 0
+-123 -150 0
+-124 -151 0
+-125 -152 0
+-126 -153 0
+-118 -154 0
+-119 -155 0
+-120 -156 0
+-121 -157 0
+-122 -158 0
+-123 -159 0
+-124 -160 0
+-125 -161 0
+-126 -162 0
+-127 -82 0
+-128 -83 0
+-129 -84 0
+-130 -85 0
+-131 -86 0
+-132 -87 0
+-133 -88 0
+-134 -89 0
+-135 -90 0
+-127 -91 0
+-128 -92 0
+-129 -93 0
+-130 -94 0
+-131 -95 0
+-132 -96 0
+-133 -97 0
+-134 -98 0
+-135 -99 0
+-127 -100 0
+-128 -101 0
+-129 -102 0
+-130 -103 0
+-131 -104 0
+-132 -105 0
+-133 -106 0
+-134 -107 0
+-135 -108 0
+-127 -109 0
+-128 -110 0
+-129 -111 0
+-130 -112 0
+-131 -113 0
+-132 -114 0
+-133 -115 0
+-134 -116 0
+-135 -117 0
+-127 -118 0
+-128 -119 0
+-129 -120 0
+-130 -121 0
+-131 -122 0
+-132 -123 0
+-133 -124 0
+-134 -125 0
+-135 -126 0
+-127 -136 0
+-128 -137 0
+-129 -138 0
+-130 -139 0
+-131 -140 0
+-132 -141 0
+-133 -142 0
+-134 -143 0
+-135 -144 0
+-127 -145 0
+-128 -146 0
+-129 -147 0
+-130 -148 0
+-131 -149 0
+-132 -150 0
+-133 -151 0
+-134 -152 0
+-135 -153 0
+-127 -154 0
+-128 -155 0
+-129 -156 0
+-130 -157 0
+-131 -158 0
+-132 -159 0
+-133 -160 0
+-134 -161 0
+-135 -162 0
+-136 -82 0
+-137 -83 0
+-138 -84 0
+-139 -85 0
+-140 -86 0
+-141 -87 0
+-142 -88 0
+-143 -89 0
+-144 -90 0
+-136 -91 0
+-137 -92 0
+-138 -93 0
+-139 -94 0
+-140 -95 0
+-141 -96 0
+-142 -97 0
+-143 -98 0
+-144 -99 0
+-136 -100 0
+-137 -101 0
+-138 -102 0
+-139 -103 0
+-140 -104 0
+-141 -105 0
+-142 -106 0
+-143 -107 0
+-144 -108 0
+-136 -109 0
+-137 -110 0
+-138 -111 0
+-139 -112 0
+-140 -113 0
+-141 -114 0
+-142 -115 0
+-143 -116 0
+-144 -117 0
+-136 -118 0
+-137 -119 0
+-138 -120 0
+-139 -121 0
+-140 -122 0
+-141 -123 0
+-142 -124 0
+-143 -125 0
+-144 -126 0
+-136 -127 0
+-137 -128 0
+-138 -129 0
+-139 -130 0
+-140 -131 0
+-141 -132 0
+-142 -133 0
+-143 -134 0
+-144 -135 0
+-136 -145 0
+-137 -146 0
+-138 -147 0
+-139 -148 0
+-140 -149 0
+-141 -150 0
+-142 -151 0
+-143 -152 0
+-144 -153 0
+-136 -154 0
+-137 -155 0
+-138 -156 0
+-139 -157 0
+-140 -158 0
+-141 -159 0
+-142 -160 0
+-143 -161 0
+-144 -162 0
+-145 -82 0
+-146 -83 0
+-147 -84 0
+-148 -85 0
+-149 -86 0
+-150 -87 0
+-151 -88 0
+-152 -89 0
+-153 -90 0
+-145 -91 0
+-146 -92 0
+-147 -93 0
+-148 -94 0
+-149 -95 0
+-150 -96 0
+-151 -97 0
+-152 -98 0
+-153 -99 0
+-145 -100 0
+-146 -101 0
+-147 -102 0
+-148 -103 0
+-149 -104 0
+-150 -105 0
+-151 -106 0
+-152 -107 0
+-153 -108 0
+-145 -109 0
+-146 -110 0
+-147 -111 0
+-148 -112 0
+-149 -113 0
+-150 -114 0
+-151 -115 0
+-152 -116 0
+-153 -117 0
+-145 -118 0
+-146 -119 0
+-147 -120 0
+-148 -121 0
+-149 -122 0
+-150 -123 0
+-151 -124 0
+-152 -125 0
+-153 -126 0
+-145 -127 0
+-146 -128 0
+-147 -129 0
+-148 -130 0
+-149 -131 0
+-150 -132 0
+-151 -133 0
+-152 -134 0
+-153 -135 0
+-145 -136 0
+-146 -137 0
+-147 -138 0
+-148 -139 0
+-149 -140 0
+-150 -141 0
+-151 -142 0
+-152 -143 0
+-153 -144 0
+-145 -154 0
+-146 -155 0
+-147 -156 0
+-148 -157 0
+-149 -158 0
+-150 -159 0
+-151 -160 0
+-152 -161 0
+-153 -162 0
+-154 -82 0
+-155 -83 0
+-156 -84 0
+-157 -85 0
+-158 -86 0
+-159 -87 0
+-160 -88 0
+-161 -89 0
+-162 -90 0
+-154 -91 0
+-155 -92 0
+-156 -93 0
+-157 -94 0
+-158 -95 0
+-159 -96 0
+-160 -97 0
+-161 -98 0
+-162 -99 0
+-154 -100 0
+-155 -101 0
+-156 -102 0
+-157 -103 0
+-158 -104 0
+-159 -105 0
+-160 -106 0
+-161 -107 0
+-162 -108 0
+-154 -109 0
+-155 -110 0
+-156 -111 0
+-157 -112 0
+-158 -113 0
+-159 -114 0
+-160 -115 0
+-161 -116 0
+-162 -117 0
+-154 -118 0
+-155 -119 0
+-156 -120 0
+-157 -121 0
+-158 -122 0
+-159 -123 0
+-160 -124 0
+-161 -125 0
+-162 -126 0
+-154 -127 0
+-155 -128 0
+-156 -129 0
+-157 -130 0
+-158 -131 0
+-159 -132 0
+-160 -133 0
+-161 -134 0
+-162 -135 0
+-154 -136 0
+-155 -137 0
+-156 -138 0
+-157 -139 0
+-158 -140 0
+-159 -141 0
+-160 -142 0
+-161 -143 0
+-162 -144 0
+-154 -145 0
+-155 -146 0
+-156 -147 0
+-157 -148 0
+-158 -149 0
+-159 -150 0
+-160 -151 0
+-161 -152 0
+-162 -153 0
+-163 -172 0
+-164 -173 0
+-165 -174 0
+-166 -175 0
+-167 -176 0
+-168 -177 0
+-169 -178 0
+-170 -179 0
+-171 -180 0
+-163 -181 0
+-164 -182 0
+-165 -183 0
+-166 -184 0
+-167 -185 0
+-168 -186 0
+-169 -187 0
+-170 -188 0
+-171 -189 0
+-163 -190 0
+-164 -191 0
+-165 -192 0
+-166 -193 0
+-167 -194 0
+-168 -195 0
+-169 -196 0
+-170 -197 0
+-171 -198 0
+-163 -199 0
+-164 -200 0
+-165 -201 0
+-166 -202 0
+-167 -203 0
+-168 -204 0
+-169 -205 0
+-170 -206 0
+-171 -207 0
+-163 -208 0
+-164 -209 0
+-165 -210 0
+-166 -211 0
+-167 -212 0
+-168 -213 0
+-169 -214 0
+-170 -215 0
+-171 -216 0
+-163 -217 0
+-164 -218 0
+-165 -219 0
+-166 -220 0
+-167 -221 0
+-168 -222 0
+-169 -223 0
+-170 -224 0
+-171 -225 0
+-163 -226 0
+-164 -227 0
+-165 -228 0
+-166 -229 0
+-167 -230 0
+-168 -231 0
+-169 -232 0
+-170 -233 0
+-171 -234 0
+-163 -235 0
+-164 -236 0
+-165 -237 0
+-166 -238 0
+-167 -239 0
+-168 -240 0
+-169 -241 0
+-170 -242 0
+-171 -243 0
+-172 -163 0
+-173 -164 0
+-174 -165 0
+-175 -166 0
+-176 -167 0
+-177 -168 0
+-178 -169 0
+-179 -170 0
+-180 -171 0
+-172 -181 0
+-173 -182 0
+-174 -183 0
+-175 -184 0
+-176 -185 0
+-177 -186 0
+-178 -187 0
+-179 -188 0
+-180 -189 0
+-172 -190 0
+-173 -191 0
+-174 -192 0
+-175 -193 0
+-176 -194 0
+-177 -195 0
+-178 -196 0
+-179 -197 0
+-180 -198 0
+-172 -199 0
+-173 -200 0
+-174 -201 0
+-175 -202 0
+-176 -203 0
+-177 -204 0
+-178 -205 0
+-179 -206 0
+-180 -207 0
+-172 -208 0
+-173 -209 0
+-174 -210 0
+-175 -211 0
+-176 -212 0
+-177 -213 0
+-178 -214 0
+-179 -215 0
+-180 -216 0
+-172 -217 0
+-173 -218 0
+-174 -219 0
+-175 -220 0
+-176 -221 0
+-177 -222 0
+-178 -223 0
+-179 -224 0
+-180 -225 0
+-172 -226 0
+-173 -227 0
+-174 -228 0
+-175 -229 0
+-176 -230 0
+-177 -231 0
+-178 -232 0
+-179 -233 0
+-180 -234 0
+-172 -235 0
+-173 -236 0
+-174 -237 0
+-175 -238 0
+-176 -239 0
+-177 -240 0
+-178 -241 0
+-179 -242 0
+-180 -243 0
+-181 -163 0
+-182 -164 0
+-183 -165 0
+-184 -166 0
+-185 -167 0
+-186 -168 0
+-187 -169 0
+-188 -170 0
+-189 -171 0
+-181 -172 0
+-182 -173 0
+-183 -174 0
+-184 -175 0
+-185 -176 0
+-186 -177 0
+-187 -178 0
+-188 -179 0
+-189 -180 0
+-181 -190 0
+-182 -191 0
+-183 -192 0
+-184 -193 0
+-185 -194 0
+-186 -195 0
+-187 -196 0
+-188 -197 0
+-189 -198 0
+-181 -199 0
+-182 -200 0
+-183 -201 0
+-184 -202 0
+-185 -203 0
+-186 -204 0
+-187 -205 0
+-188 -206 0
+-189 -207 0
+-181 -208 0
+-182 -209 0
+-183 -210 0
+-184 -211 0
+-185 -212 0
+-186 -213 0
+-187 -214 0
+-188 -215 0
+-189 -216 0
+-181 -217 0
+-182 -218 0
+-183 -219 0
+-184 -220 0
+-185 -221 0
+-186 -222 0
+-187 -223 0
+-188 -224 0
+-189 -225 0
+-181 -226 0
+-182 -227 0
+-183 -228 0
+-184 -229 0
+-185 -230 0
+-186 -231 0
+-187 -232 0
+-188 -233 0
+-189 -234 0
+-181 -235 0
+-182 -236 0
+-183 -237 0
+-184 -238 0
+-185 -239 0
+-186 -240 0
+-187 -241 0
+-188 -242 0
+-189 -243 0
+-190 -163 0
+-191 -164 0
+-192 -165 0
+-193 -166 0
+-194 -167 0
+-195 -168 0
+-196 -169 0
+-197 -170 0
+-198 -171 0
+-190 -172 0
+-191 -173 0
+-192 -174 0
+-193 -175 0
+-194 -176 0
+-195 -177 0
+-196 -178 0
+-197 -179 0
+-198 -180 0
+-190 -181 0
+-191 -182 0
+-192 -183 0
+-193 -184 0
+-194 -185 0
+-195 -186 0
+-196 -187 0
+-197 -188 0
+-198 -189 0
+-190 -199 0
+-191 -200 0
+-192 -201 0
+-193 -202 0
+-194 -203 0
+-195 -204 0
+-196 -205 0
+-197 -206 0
+-198 -207 0
+-190 -208 0
+-191 -209 0
+-192 -210 0
+-193 -211 0
+-194 -212 0
+-195 -213 0
+-196 -214 0
+-197 -215 0
+-198 -216 0
+-190 -217 0
+-191 -218 0
+-192 -219 0
+-193 -220 0
+-194 -221 0
+-195 -222 0
+-196 -223 0
+-197 -224 0
+-198 -225 0
+-190 -226 0
+-191 -227 0
+-192 -228 0
+-193 -229 0
+-194 -230 0
+-195 -231 0
+-196 -232 0
+-197 -233 0
+-198 -234 0
+-190 -235 0
+-191 -236 0
+-192 -237 0
+-193 -238 0
+-194 -239 0
+-195 -240 0
+-196 -241 0
+-197 -242 0
+-198 -243 0
+-199 -163 0
+-200 -164 0
+-201 -165 0
+-202 -166 0
+-203 -167 0
+-204 -168 0
+-205 -169 0
+-206 -170 0
+-207 -171 0
+-199 -172 0
+-200 -173 0
+-201 -174 0
+-202 -175 0
+-203 -176 0
+-204 -177 0
+-205 -178 0
+-206 -179 0
+-207 -180 0
+-199 -181 0
+-200 -182 0
+-201 -183 0
+-202 -184 0
+-203 -185 0
+-204 -186 0
+-205 -187 0
+-206 -188 0
+-207 -189 0
+-199 -190 0
+-200 -191 0
+-201 -192 0
+-202 -193 0
+-203 -194 0
+-204 -195 0
+-205 -196 0
+-206 -197 0
+-207 -198 0
+-199 -208 0
+-200 -209 0
+-201 -210 0
+-202 -211 0
+-203 -212 0
+-204 -213 0
+-205 -214 0
+-206 -215 0
+-207 -216 0
+-199 -217 0
+-200 -218 0
+-201 -219 0
+-202 -220 0
+-203 -221 0
+-204 -222 0
+-205 -223 0
+-206 -224 0
+-207 -225 0
+-199 -226 0
+-200 -227 0
+-201 -228 0
+-202 -229 0
+-203 -230 0
+-204 -231 0
+-205 -232 0
+-206 -233 0
+-207 -234 0
+-199 -235 0
+-200 -236 0
+-201 -237 0
+-202 -238 0
+-203 -239 0
+-204 -240 0
+-205 -241 0
+-206 -242 0
+-207 -243 0
+-208 -163 0
+-209 -164 0
+-210 -165 0
+-211 -166 0
+-212 -167 0
+-213 -168 0
+-214 -169 0
+-215 -170 0
+-216 -171 0
+-208 -172 0
+-209 -173 0
+-210 -174 0
+-211 -175 0
+-212 -176 0
+-213 -177 0
+-214 -178 0
+-215 -179 0
+-216 -180 0
+-208 -181 0
+-209 -182 0
+-210 -183 0
+-211 -184 0
+-212 -185 0
+-213 -186 0
+-214 -187 0
+-215 -188 0
+-216 -189 0
+-208 -190 0
+-209 -191 0
+-210 -192 0
+-211 -193 0
+-212 -194 0
+-213 -195 0
+-214 -196 0
+-215 -197 0
+-216 -198 0
+-208 -199 0
+-209 -200 0
+-210 -201 0
+-211 -202 0
+-212 -203 0
+-213 -204 0
+-214 -205 0
+-215 -206 0
+-216 -207 0
+-208 -217 0
+-209 -218 0
+-210 -219 0
+-211 -220 0
+-212 -221 0
+-213 -222 0
+-214 -223 0
+-215 -224 0
+-216 -225 0
+-208 -226 0
+-209 -227 0
+-210 -228 0
+-211 -229 0
+-212 -230 0
+-213 -231 0
+-214 -232 0
+-215 -233 0
+-216 -234 0
+-208 -235 0
+-209 -236 0
+-210 -237 0
+-211 -238 0
+-212 -239 0
+-213 -240 0
+-214 -241 0
+-215 -242 0
+-216 -243 0
+-217 -163 0
+-218 -164 0
+-219 -165 0
+-220 -166 0
+-221 -167 0
+-222 -168 0
+-223 -169 0
+-224 -170 0
+-225 -171 0
+-217 -172 0
+-218 -173 0
+-219 -174 0
+-220 -175 0
+-221 -176 0
+-222 -177 0
+-223 -178 0
+-224 -179 0
+-225 -180 0
+-217 -181 0
+-218 -182 0
+-219 -183 0
+-220 -184 0
+-221 -185 0
+-222 -186 0
+-223 -187 0
+-224 -188 0
+-225 -189 0
+-217 -190 0
+-218 -191 0
+-219 -192 0
+-220 -193 0
+-221 -194 0
+-222 -195 0
+-223 -196 0
+-224 -197 0
+-225 -198 0
+-217 -199 0
+-218 -200 0
+-219 -201 0
+-220 -202 0
+-221 -203 0
+-222 -204 0
+-223 -205 0
+-224 -206 0
+-225 -207 0
+-217 -208 0
+-218 -209 0
+-219 -210 0
+-220 -211 0
+-221 -212 0
+-222 -213 0
+-223 -214 0
+-224 -215 0
+-225 -216 0
+-217 -226 0
+-218 -227 0
+-219 -228 0
+-220 -229 0
+-221 -230 0
+-222 -231 0
+-223 -232 0
+-224 -233 0
+-225 -234 0
+-217 -235 0
+-218 -236 0
+-219 -237 0
+-220 -238 0
+-221 -239 0
+-222 -240 0
+-223 -241 0
+-224 -242 0
+-225 -243 0
+-226 -163 0
+-227 -164 0
+-228 -165 0
+-229 -166 0
+-230 -167 0
+-231 -168 0
+-232 -169 0
+-233 -170 0
+-234 -171 0
+-226 -172 0
+-227 -173 0
+-228 -174 0
+-229 -175 0
+-230 -176 0
+-231 -177 0
+-232 -178 0
+-233 -179 0
+-234 -180 0
+-226 -181 0
+-227 -182 0
+-228 -183 0
+-229 -184 0
+-230 -185 0
+-231 -186 0
+-232 -187 0
+-233 -188 0
+-234 -189 0
+-226 -190 0
+-227 -191 0
+-228 -192 0
+-229 -193 0
+-230 -194 0
+-231 -195 0
+-232 -196 0
+-233 -197 0
+-234 -198 0
+-226 -199 0
+-227 -200 0
+-228 -201 0
+-229 -202 0
+-230 -203 0
+-231 -204 0
+-232 -205 0
+-233 -206 0
+-234 -207 0
+-226 -208 0
+-227 -209 0
+-228 -210 0
+-229 -211 0
+-230 -212 0
+-231 -213 0
+-232 -214 0
+-233 -215 0
+-234 -216 0
+-226 -217 0
+-227 -218 0
+-228 -219 0
+-229 -220 0
+-230 -221 0
+-231 -222 0
+-232 -223 0
+-233 -224 0
+-234 -225 0
+-226 -235 0
+-227 -236 0
+-228 -237 0
+-229 -238 0
+-230 -239 0
+-231 -240 0
+-232 -241 0
+-233 -242 0
+-234 -243 0
+-235 -163 0
+-236 -164 0
+-237 -165 0
+-238 -166 0
+-239 -167 0
+-240 -168 0
+-241 -169 0
+-242 -170 0
+-243 -171 0
+-235 -172 0
+-236 -173 0
+-237 -174 0
+-238 -175 0
+-239 -176 0
+-240 -177 0
+-241 -178 0
+-242 -179 0
+-243 -180 0
+-235 -181 0
+-236 -182 0
+-237 -183 0
+-238 -184 0
+-239 -185 0
+-240 -186 0
+-241 -187 0
+-242 -188 0
+-243 -189 0
+-235 -190 0
+-236 -191 0
+-237 -192 0
+-238 -193 0
+-239 -194 0
+-240 -195 0
+-241 -196 0
+-242 -197 0
+-243 -198 0
+-235 -199 0
+-236 -200 0
+-237 -201 0
+-238 -202 0
+-239 -203 0
+-240 -204 0
+-241 -205 0
+-242 -206 0
+-243 -207 0
+-235 -208 0
+-236 -209 0
+-237 -210 0
+-238 -211 0
+-239 -212 0
+-240 -213 0
+-241 -214 0
+-242 -215 0
+-243 -216 0
+-235 -217 0
+-236 -218 0
+-237 -219 0
+-238 -220 0
+-239 -221 0
+-240 -222 0
+-241 -223 0
+-242 -224 0
+-243 -225 0
+-235 -226 0
+-236 -227 0
+-237 -228 0
+-238 -229 0
+-239 -230 0
+-240 -231 0
+-241 -232 0
+-242 -233 0
+-243 -234 0
+-244 -253 0
+-245 -254 0
+-246 -255 0
+-247 -256 0
+-248 -257 0
+-249 -258 0
+-250 -259 0
+-251 -260 0
+-252 -261 0
+-244 -262 0
+-245 -263 0
+-246 -264 0
+-247 -265 0
+-248 -266 0
+-249 -267 0
+-250 -268 0
+-251 -269 0
+-252 -270 0
+-244 -271 0
+-245 -272 0
+-246 -273 0
+-247 -274 0
+-248 -275 0
+-249 -276 0
+-250 -277 0
+-251 -278 0
+-252 -279 0
+-244 -280 0
+-245 -281 0
+-246 -282 0
+-247 -283 0
+-248 -284 0
+-249 -285 0
+-250 -286 0
+-251 -287 0
+-252 -288 0
+-244 -289 0
+-245 -290 0
+-246 -291 0
+-247 -292 0
+-248 -293 0
+-249 -294 0
+-250 -295 0
+-251 -296 0
+-252 -297 0
+-244 -298 0
+-245 -299 0
+-246 -300 0
+-247 -301 0
+-248 -302 0
+-249 -303 0
+-250 -304 0
+-251 -305 0
+-252 -306 0
+-244 -307 0
+-245 -308 0
+-246 -309 0
+-247 -310 0
+-248 -311 0
+-249 -312 0
+-250 -313 0
+-251 -314 0
+-252 -315 0
+-244 -316 0
+-245 -317 0
+-246 -318 0
+-247 -319 0
+-248 -320 0
+-249 -321 0
+-250 -322 0
+-251 -323 0
+-252 -324 0
+-253 -244 0
+-254 -245 0
+-255 -246 0
+-256 -247 0
+-257 -248 0
+-258 -249 0
+-259 -250 0
+-260 -251 0
+-261 -252 0
+-253 -262 0
+-254 -263 0
+-255 -264 0
+-256 -265 0
+-257 -266 0
+-258 -267 0
+-259 -268 0
+-260 -269 0
+-261 -270 0
+-253 -271 0
+-254 -272 0
+-255 -273 0
+-256 -274 0
+-257 -275 0
+-258 -276 0
+-259 -277 0
+-260 -278 0
+-261 -279 0
+-253 -280 0
+-254 -281 0
+-255 -282 0
+-256 -283 0
+-257 -284 0
+-258 -285 0
+-259 -286 0
+-260 -287 0
+-261 -288 0
+-253 -289 0
+-254 -290 0
+-255 -291 0
+-256 -292 0
+-257 -293 0
+-258 -294 0
+-259 -295 0
+-260 -296 0
+-261 -297 0
+-253 -298 0
+-254 -299 0
+-255 -300 0
+-256 -301 0
+-257 -302 0
+-258 -303 0
+-259 -304 0
+-260 -305 0
+-261 -306 0
+-253 -307 0
+-254 -308 0
+-255 -309 0
+-256 -310 0
+-257 -311 0
+-258 -312 0
+-259 -313 0
+-260 -314 0
+-261 -315 0
+-253 -316 0
+-254 -317 0
+-255 -318 0
+-256 -319 0
+-257 -320 0
+-258 -321 0
+-259 -322 0
+-260 -323 0
+-261 -324 0
+-262 -244 0
+-263 -245 0
+-264 -246 0
+-265 -247 0
+-266 -248 0
+-267 -249 0
+-268 -250 0
+-269 -251 0
+-270 -252 0
+-262 -253 0
+-263 -254 0
+-264 -255 0
+-265 -256 0
+-266 -257 0
+-267 -258 0
+-268 -259 0
+-269 -260 0
+-270 -261 0
+-262 -271 0
+-263 -272 0
+-264 -273 0
+-265 -274 0
+-266 -275 0
+-267 -276 0
+-268 -277 0
+-269 -278 0
+-270 -279 0
+-262 -280 0
+-263 -281 0
+-264 -282 0
+-265 -283 0
+-266 -284 0
+-267 -285 0
+-268 -286 0
+-269 -287 0
+-270 -288 0
+-262 -289 0
+-263 -290 0
+-264 -291 0
+-265 -292 0
+-266 -293 0
+-267 -294 0
+-268 -295 0
+-269 -296 0
+-270 -297 0
+-262 -298 0
+-263 -299 0
+-264 -300 0
+-265 -301 0
+-266 -302 0
+-267 -303 0
+-268 -304 0
+-269 -305 0
+-270 -306 0
+-262 -307 0
+-263 -308 0
+-264 -309 0
+-265 -310 0
+-266 -311 0
+-267 -312 0
+-268 -313 0
+-269 -314 0
+-270 -315 0
+-262 -316 0
+-263 -317 0
+-264 -318 0
+-265 -319 0
+-266 -320 0
+-267 -321 0
+-268 -322 0
+-269 -323 0
+-270 -324 0
+-271 -244 0
+-272 -245 0
+-273 -246 0
+-274 -247 0
+-275 -248 0
+-276 -249 0
+-277 -250 0
+-278 -251 0
+-279 -252 0
+-271 -253 0
+-272 -254 0
+-273 -255 0
+-274 -256 0
+-275 -257 0
+-276 -258 0
+-277 -259 0
+-278 -260 0
+-279 -261 0
+-271 -262 0
+-272 -263 0
+-273 -264 0
+-274 -265 0
+-275 -266 0
+-276 -267 0
+-277 -268 0
+-278 -269 0
+-279 -270 0
+-271 -280 0
+-272 -281 0
+-273 -282 0
+-274 -283 0
+-275 -284 0
+-276 -285 0
+-277 -286 0
+-278 -287 0
+-279 -288 0
+-271 -289 0
+-272 -290 0
+-273 -291 0
+-274 -292 0
+-275 -293 0
+-276 -294 0
+-277 -295 0
+-278 -296 0
+-279 -297 0
+-271 -298 0
+-272 -299 0
+-273 -300 0
+-274 -301 0
+-275 -302 0
+-276 -303 0
+-277 -304 0
+-278 -305 0
+-279 -306 0
+-271 -307 0
+-272 -308 0
+-273 -309 0
+-274 -310 0
+-275 -311 0
+-276 -312 0
+-277 -313 0
+-278 -314 0
+-279 -315 0
+-271 -316 0
+-272 -317 0
+-273 -318 0
+-274 -319 0
+-275 -320 0
+-276 -321 0
+-277 -322 0
+-278 -323 0
+-279 -324 0
+-280 -244 0
+-281 -245 0
+-282 -246 0
+-283 -247 0
+-284 -248 0
+-285 -249 0
+-286 -250 0
+-287 -251 0
+-288 -252 0
+-280 -253 0
+-281 -254 0
+-282 -255 0
+-283 -256 0
+-284 -257 0
+-285 -258 0
+-286 -259 0
+-287 -260 0
+-288 -261 0
+-280 -262 0
+-281 -263 0
+-282 -264 0
+-283 -265 0
+-284 -266 0
+-285 -267 0
+-286 -268 0
+-287 -269 0
+-288 -270 0
+-280 -271 0
+-281 -272 0
+-282 -273 0
+-283 -274 0
+-284 -275 0
+-285 -276 0
+-286 -277 0
+-287 -278 0
+-288 -279 0
+-280 -289 0
+-281 -290 0
+-282 -291 0
+-283 -292 0
+-284 -293 0
+-285 -294 0
+-286 -295 0
+-287 -296 0
+-288 -297 0
+-280 -298 0
+-281 -299 0
+-282 -300 0
+-283 -301 0
+-284 -302 0
+-285 -303 0
+-286 -304 0
+-287 -305 0
+-288 -306 0
+-280 -307 0
+-281 -308 0
+-282 -309 0
+-283 -310 0
+-284 -311 0
+-285 -312 0
+-286 -313 0
+-287 -314 0
+-288 -315 0
+-280 -316 0
+-281 -317 0
+-282 -318 0
+-283 -319 0
+-284 -320 0
+-285 -321 0
+-286 -322 0
+-287 -323 0
+-288 -324 0
+-289 -244 0
+-290 -245 0
+-291 -246 0
+-292 -247 0
+-293 -248 0
+-294 -249 0
+-295 -250 0
+-296 -251 0
+-297 -252 0
+-289 -253 0
+-290 -254 0
+-291 -255 0
+-292 -256 0
+-293 -257 0
+-294 -258 0
+-295 -259 0
+-296 -260 0
+-297 -261 0
+-289 -262 0
+-290 -263 0
+-291 -264 0
+-292 -265 0
+-293 -266 0
+-294 -267 0
+-295 -268 0
+-296 -269 0
+-297 -270 0
+-289 -271 0
+-290 -272 0
+-291 -273 0
+-292 -274 0
+-293 -275 0
+-294 -276 0
+-295 -277 0
+-296 -278 0
+-297 -279 0
+-289 -280 0
+-290 -281 0
+-291 -282 0
+-292 -283 0
+-293 -284 0
+-294 -285 0
+-295 -286 0
+-296 -287 0
+-297 -288 0
+-289 -298 0
+-290 -299 0
+-291 -300 0
+-292 -301 0
+-293 -302 0
+-294 -303 0
+-295 -304 0
+-296 -305 0
+-297 -306 0
+-289 -307 0
+-290 -308 0
+-291 -309 0
+-292 -310 0
+-293 -311 0
+-294 -312 0
+-295 -313 0
+-296 -314 0
+-297 -315 0
+-289 -316 0
+-290 -317 0
+-291 -318 0
+-292 -319 0
+-293 -320 0
+-294 -321 0
+-295 -322 0
+-296 -323 0
+-297 -324 0
+-298 -244 0
+-299 -245 0
+-300 -246 0
+-301 -247 0
+-302 -248 0
+-303 -249 0
+-304 -250 0
+-305 -251 0
+-306 -252 0
+-298 -253 0
+-299 -254 0
+-300 -255 0
+-301 -256 0
+-302 -257 0
+-303 -258 0
+-304 -259 0
+-305 -260 0
+-306 -261 0
+-298 -262 0
+-299 -263 0
+-300 -264 0
+-301 -265 0
+-302 -266 0
+-303 -267 0
+-304 -268 0
+-305 -269 0
+-306 -270 0
+-298 -271 0
+-299 -272 0
+-300 -273 0
+-301 -274 0
+-302 -275 0
+-303 -276 0
+-304 -277 0
+-305 -278 0
+-306 -279 0
+-298 -280 0
+-299 -281 0
+-300 -282 0
+-301 -283 0
+-302 -284 0
+-303 -285 0
+-304 -286 0
+-305 -287 0
+-306 -288 0
+-298 -289 0
+-299 -290 0
+-300 -291 0
+-301 -292 0
+-302 -293 0
+-303 -294 0
+-304 -295 0
+-305 -296 0
+-306 -297 0
+-298 -307 0
+-299 -308 0
+-300 -309 0
+-301 -310 0
+-302 -311 0
+-303 -312 0
+-304 -313 0
+-305 -314 0
+-306 -315 0
+-298 -316 0
+-299 -317 0
+-300 -318 0
+-301 -319 0
+-302 -320 0
+-303 -321 0
+-304 -322 0
+-305 -323 0
+-306 -324 0
+-307 -244 0
+-308 -245 0
+-309 -246 0
+-310 -247 0
+-311 -248 0
+-312 -249 0
+-313 -250 0
+-314 -251 0
+-315 -252 0
+-307 -253 0
+-308 -254 0
+-309 -255 0
+-310 -256 0
+-311 -257 0
+-312 -258 0
+-313 -259 0
+-314 -260 0
+-315 -261 0
+-307 -262 0
+-308 -263 0
+-309 -264 0
+-310 -265 0
+-311 -266 0
+-312 -267 0
+-313 -268 0
+-314 -269 0
+-315 -270 0
+-307 -271 0
+-308 -272 0
+-309 -273 0
+-310 -274 0
+-311 -275 0
+-312 -276 0
+-313 -277 0
+-314 -278 0
+-315 -279 0
+-307 -280 0
+-308 -281 0
+-309 -282 0
+-310 -283 0
+-311 -284 0
+-312 -285 0
+-313 -286 0
+-314 -287 0
+-315 -288 0
+-307 -289 0
+-308 -290 0
+-309 -291 0
+-310 -292 0
+-311 -293 0
+-312 -294 0
+-313 -295 0
+-314 -296 0
+-315 -297 0
+-307 -298 0
+-308 -299 0
+-309 -300 0
+-310 -301 0
+-311 -302 0
+-312 -303 0
+-313 -304 0
+-314 -305 0
+-315 -306 0
+-307 -316 0
+-308 -317 0
+-309 -318 0
+-310 -319 0
+-311 -320 0
+-312 -321 0
+-313 -322 0
+-314 -323 0
+-315 -324 0
+-316 -244 0
+-317 -245 0
+-318 -246 0
+-319 -247 0
+-320 -248 0
+-321 -249 0
+-322 -250 0
+-323 -251 0
+-324 -252 0
+-316 -253 0
+-317 -254 0
+-318 -255 0
+-319 -256 0
+-320 -257 0
+-321 -258 0
+-322 -259 0
+-323 -260 0
+-324 -261 0
+-316 -262 0
+-317 -263 0
+-318 -264 0
+-319 -265 0
+-320 -266 0
+-321 -267 0
+-322 -268 0
+-323 -269 0
+-324 -270 0
+-316 -271 0
+-317 -272 0
+-318 -273 0
+-319 -274 0
+-320 -275 0
+-321 -276 0
+-322 -277 0
+-323 -278 0
+-324 -279 0
+-316 -280 0
+-317 -281 0
+-318 -282 0
+-319 -283 0
+-320 -284 0
+-321 -285 0
+-322 -286 0
+-323 -287 0
+-324 -288 0
+-316 -289 0
+-317 -290 0
+-318 -291 0
+-319 -292 0
+-320 -293 0
+-321 -294 0
+-322 -295 0
+-323 -296 0
+-324 -297 0
+-316 -298 0
+-317 -299 0
+-318 -300 0
+-319 -301 0
+-320 -302 0
+-321 -303 0
+-322 -304 0
+-323 -305 0
+-324 -306 0
+-316 -307 0
+-317 -308 0
+-318 -309 0
+-319 -310 0
+-320 -311 0
+-321 -312 0
+-322 -313 0
+-323 -314 0
+-324 -315 0
+-325 -334 0
+-326 -335 0
+-327 -336 0
+-328 -337 0
+-329 -338 0
+-330 -339 0
+-331 -340 0
+-332 -341 0
+-333 -342 0
+-325 -343 0
+-326 -344 0
+-327 -345 0
+-328 -346 0
+-329 -347 0
+-330 -348 0
+-331 -349 0
+-332 -350 0
+-333 -351 0
+-325 -352 0
+-326 -353 0
+-327 -354 0
+-328 -355 0
+-329 -356 0
+-330 -357 0
+-331 -358 0
+-332 -359 0
+-333 -360 0
+-325 -361 0
+-326 -362 0
+-327 -363 0
+-328 -364 0
+-329 -365 0
+-330 -366 0
+-331 -367 0
+-332 -368 0
+-333 -369 0
+-325 -370 0
+-326 -371 0
+-327 -372 0
+-328 -373 0
+-329 -374 0
+-330 -375 0
+-331 -376 0
+-332 -377 0
+-333 -378 0
+-325 -379 0
+-326 -380 0
+-327 -381 0
+-328 -382 0
+-329 -383 0
+-330 -384 0
+-331 -385 0
+-332 -386 0
+-333 -387 0
+-325 -388 0
+-326 -389 0
+-327 -390 0
+-328 -391 0
+-329 -392 0
+-330 -393 0
+-331 -394 0
+-332 -395 0
+-333 -396 0
+-325 -397 0
+-326 -398 0
+-327 -399 0
+-328 -400 0
+-329 -401 0
+-330 -402 0
+-331 -403 0
+-332 -404 0
+-333 -405 0
+-334 -325 0
+-335 -326 0
+-336 -327 0
+-337 -328 0
+-338 -329 0
+-339 -330 0
+-340 -331 0
+-341 -332 0
+-342 -333 0
+-334 -343 0
+-335 -344 0
+-336 -345 0
+-337 -346 0
+-338 -347 0
+-339 -348 0
+-340 -349 0
+-341 -350 0
+-342 -351 0
+-334 -352 0
+-335 -353 0
+-336 -354 0
+-337 -355 0
+-338 -356 0
+-339 -357 0
+-340 -358 0
+-341 -359 0
+-342 -360 0
+-334 -361 0
+-335 -362 0
+-336 -363 0
+-337 -364 0
+-338 -365 0
+-339 -366 0
+-340 -367 0
+-341 -368 0
+-342 -369 0
+-334 -370 0
+-335 -371 0
+-336 -372 0
+-337 -373 0
+-338 -374 0
+-339 -375 0
+-340 -376 0
+-341 -377 0
+-342 -378 0
+-334 -379 0
+-335 -380 0
+-336 -381 0
+-337 -382 0
+-338 -383 0
+-339 -384 0
+-340 -385 0
+-341 -386 0
+-342 -387 0
+-334 -388 0
+-335 -389 0
+-336 -390 0
+-337 -391 0
+-338 -392 0
+-339 -393 0
+-340 -394 0
+-341 -395 0
+-342 -396 0
+-334 -397 0
+-335 -398 0
+-336 -399 0
+-337 -400 0
+-338 -401 0
+-339 -402 0
+-340 -403 0
+-341 -404 0
+-342 -405 0
+-343 -325 0
+-344 -326 0
+-345 -327 0
+-346 -328 0
+-347 -329 0
+-348 -330 0
+-349 -331 0
+-350 -332 0
+-351 -333 0
+-343 -334 0
+-344 -335 0
+-345 -336 0
+-346 -337 0
+-347 -338 0
+-348 -339 0
+-349 -340 0
+-350 -341 0
+-351 -342 0
+-343 -352 0
+-344 -353 0
+-345 -354 0
+-346 -355 0
+-347 -356 0
+-348 -357 0
+-349 -358 0
+-350 -359 0
+-351 -360 0
+-343 -361 0
+-344 -362 0
+-345 -363 0
+-346 -364 0
+-347 -365 0
+-348 -366 0
+-349 -367 0
+-350 -368 0
+-351 -369 0
+-343 -370 0
+-344 -371 0
+-345 -372 0
+-346 -373 0
+-347 -374 0
+-348 -375 0
+-349 -376 0
+-350 -377 0
+-351 -378 0
+-343 -379 0
+-344 -380 0
+-345 -381 0
+-346 -382 0
+-347 -383 0
+-348 -384 0
+-349 -385 0
+-350 -386 0
+-351 -387 0
+-343 -388 0
+-344 -389 0
+-345 -390 0
+-346 -391 0
+-347 -392 0
+-348 -393 0
+-349 -394 0
+-350 -395 0
+-351 -396 0
+-343 -397 0
+-344 -398 0
+-345 -399 0
+-346 -400 0
+-347 -401 0
+-348 -402 0
+-349 -403 0
+-350 -404 0
+-351 -405 0
+-352 -325 0
+-353 -326 0
+-354 -327 0
+-355 -328 0
+-356 -329 0
+-357 -330 0
+-358 -331 0
+-359 -332 0
+-360 -333 0
+-352 -334 0
+-353 -335 0
+-354 -336 0
+-355 -337 0
+-356 -338 0
+-357 -339 0
+-358 -340 0
+-359 -341 0
+-360 -342 0
+-352 -343 0
+-353 -344 0
+-354 -345 0
+-355 -346 0
+-356 -347 0
+-357 -348 0
+-358 -349 0
+-359 -350 0
+-360 -351 0
+-352 -361 0
+-353 -362 0
+-354 -363 0
+-355 -364 0
+-356 -365 0
+-357 -366 0
+-358 -367 0
+-359 -368 0
+-360 -369 0
+-352 -370 0
+-353 -371 0
+-354 -372 0
+-355 -373 0
+-356 -374 0
+-357 -375 0
+-358 -376 0
+-359 -377 0
+-360 -378 0
+-352 -379 0
+-353 -380 0
+-354 -381 0
+-355 -382 0
+-356 -383 0
+-357 -384 0
+-358 -385 0
+-359 -386 0
+-360 -387 0
+-352 -388 0
+-353 -389 0
+-354 -390 0
+-355 -391 0
+-356 -392 0
+-357 -393 0
+-358 -394 0
+-359 -395 0
+-360 -396 0
+-352 -397 0
+-353 -398 0
+-354 -399 0
+-355 -400 0
+-356 -401 0
+-357 -402 0
+-358 -403 0
+-359 -404 0
+-360 -405 0
+-361 -325 0
+-362 -326 0
+-363 -327 0
+-364 -328 0
+-365 -329 0
+-366 -330 0
+-367 -331 0
+-368 -332 0
+-369 -333 0
+-361 -334 0
+-362 -335 0
+-363 -336 0
+-364 -337 0
+-365 -338 0
+-366 -339 0
+-367 -340 0
+-368 -341 0
+-369 -342 0
+-361 -343 0
+-362 -344 0
+-363 -345 0
+-364 -346 0
+-365 -347 0
+-366 -348 0
+-367 -349 0
+-368 -350 0
+-369 -351 0
+-361 -352 0
+-362 -353 0
+-363 -354 0
+-364 -355 0
+-365 -356 0
+-366 -357 0
+-367 -358 0
+-368 -359 0
+-369 -360 0
+-361 -370 0
+-362 -371 0
+-363 -372 0
+-364 -373 0
+-365 -374 0
+-366 -375 0
+-367 -376 0
+-368 -377 0
+-369 -378 0
+-361 -379 0
+-362 -380 0
+-363 -381 0
+-364 -382 0
+-365 -383 0
+-366 -384 0
+-367 -385 0
+-368 -386 0
+-369 -387 0
+-361 -388 0
+-362 -389 0
+-363 -390 0
+-364 -391 0
+-365 -392 0
+-366 -393 0
+-367 -394 0
+-368 -395 0
+-369 -396 0
+-361 -397 0
+-362 -398 0
+-363 -399 0
+-364 -400 0
+-365 -401 0
+-366 -402 0
+-367 -403 0
+-368 -404 0
+-369 -405 0
+-370 -325 0
+-371 -326 0
+-372 -327 0
+-373 -328 0
+-374 -329 0
+-375 -330 0
+-376 -331 0
+-377 -332 0
+-378 -333 0
+-370 -334 0
+-371 -335 0
+-372 -336 0
+-373 -337 0
+-374 -338 0
+-375 -339 0
+-376 -340 0
+-377 -341 0
+-378 -342 0
+-370 -343 0
+-371 -344 0
+-372 -345 0
+-373 -346 0
+-374 -347 0
+-375 -348 0
+-376 -349 0
+-377 -350 0
+-378 -351 0
+-370 -352 0
+-371 -353 0
+-372 -354 0
+-373 -355 0
+-374 -356 0
+-375 -357 0
+-376 -358 0
+-377 -359 0
+-378 -360 0
+-370 -361 0
+-371 -362 0
+-372 -363 0
+-373 -364 0
+-374 -365 0
+-375 -366 0
+-376 -367 0
+-377 -368 0
+-378 -369 0
+-370 -379 0
+-371 -380 0
+-372 -381 0
+-373 -382 0
+-374 -383 0
+-375 -384 0
+-376 -385 0
+-377 -386 0
+-378 -387 0
+-370 -388 0
+-371 -389 0
+-372 -390 0
+-373 -391 0
+-374 -392 0
+-375 -393 0
+-376 -394 0
+-377 -395 0
+-378 -396 0
+-370 -397 0
+-371 -398 0
+-372 -399 0
+-373 -400 0
+-374 -401 0
+-375 -402 0
+-376 -403 0
+-377 -404 0
+-378 -405 0
+-379 -325 0
+-380 -326 0
+-381 -327 0
+-382 -328 0
+-383 -329 0
+-384 -330 0
+-385 -331 0
+-386 -332 0
+-387 -333 0
+-379 -334 0
+-380 -335 0
+-381 -336 0
+-382 -337 0
+-383 -338 0
+-384 -339 0
+-385 -340 0
+-386 -341 0
+-387 -342 0
+-379 -343 0
+-380 -344 0
+-381 -345 0
+-382 -346 0
+-383 -347 0
+-384 -348 0
+-385 -349 0
+-386 -350 0
+-387 -351 0
+-379 -352 0
+-380 -353 0
+-381 -354 0
+-382 -355 0
+-383 -356 0
+-384 -357 0
+-385 -358 0
+-386 -359 0
+-387 -360 0
+-379 -361 0
+-380 -362 0
+-381 -363 0
+-382 -364 0
+-383 -365 0
+-384 -366 0
+-385 -367 0
+-386 -368 0
+-387 -369 0
+-379 -370 0
+-380 -371 0
+-381 -372 0
+-382 -373 0
+-383 -374 0
+-384 -375 0
+-385 -376 0
+-386 -377 0
+-387 -378 0
+-379 -388 0
+-380 -389 0
+-381 -390 0
+-382 -391 0
+-383 -392 0
+-384 -393 0
+-385 -394 0
+-386 -395 0
+-387 -396 0
+-379 -397 0
+-380 -398 0
+-381 -399 0
+-382 -400 0
+-383 -401 0
+-384 -402 0
+-385 -403 0
+-386 -404 0
+-387 -405 0
+-388 -325 0
+-389 -326 0
+-390 -327 0
+-391 -328 0
+-392 -329 0
+-393 -330 0
+-394 -331 0
+-395 -332 0
+-396 -333 0
+-388 -334 0
+-389 -335 0
+-390 -336 0
+-391 -337 0
+-392 -338 0
+-393 -339 0
+-394 -340 0
+-395 -341 0
+-396 -342 0
+-388 -343 0
+-389 -344 0
+-390 -345 0
+-391 -346 0
+-392 -347 0
+-393 -348 0
+-394 -349 0
+-395 -350 0
+-396 -351 0
+-388 -352 0
+-389 -353 0
+-390 -354 0
+-391 -355 0
+-392 -356 0
+-393 -357 0
+-394 -358 0
+-395 -359 0
+-396 -360 0
+-388 -361 0
+-389 -362 0
+-390 -363 0
+-391 -364 0
+-392 -365 0
+-393 -366 0
+-394 -367 0
+-395 -368 0
+-396 -369 0
+-388 -370 0
+-389 -371 0
+-390 -372 0
+-391 -373 0
+-392 -374 0
+-393 -375 0
+-394 -376 0
+-395 -377 0
+-396 -378 0
+-388 -379 0
+-389 -380 0
+-390 -381 0
+-391 -382 0
+-392 -383 0
+-393 -384 0
+-394 -385 0
+-395 -386 0
+-396 -387 0
+-388 -397 0
+-389 -398 0
+-390 -399 0
+-391 -400 0
+-392 -401 0
+-393 -402 0
+-394 -403 0
+-395 -404 0
+-396 -405 0
+-397 -325 0
+-398 -326 0
+-399 -327 0
+-400 -328 0
+-401 -329 0
+-402 -330 0
+-403 -331 0
+-404 -332 0
+-405 -333 0
+-397 -334 0
+-398 -335 0
+-399 -336 0
+-400 -337 0
+-401 -338 0
+-402 -339 0
+-403 -340 0
+-404 -341 0
+-405 -342 0
+-397 -343 0
+-398 -344 0
+-399 -345 0
+-400 -346 0
+-401 -347 0
+-402 -348 0
+-403 -349 0
+-404 -350 0
+-405 -351 0
+-397 -352 0
+-398 -353 0
+-399 -354 0
+-400 -355 0
+-401 -356 0
+-402 -357 0
+-403 -358 0
+-404 -359 0
+-405 -360 0
+-397 -361 0
+-398 -362 0
+-399 -363 0
+-400 -364 0
+-401 -365 0
+-402 -366 0
+-403 -367 0
+-404 -368 0
+-405 -369 0
+-397 -370 0
+-398 -371 0
+-399 -372 0
+-400 -373 0
+-401 -374 0
+-402 -375 0
+-403 -376 0
+-404 -377 0
+-405 -378 0
+-397 -379 0
+-398 -380 0
+-399 -381 0
+-400 -382 0
+-401 -383 0
+-402 -384 0
+-403 -385 0
+-404 -386 0
+-405 -387 0
+-397 -388 0
+-398 -389 0
+-399 -390 0
+-400 -391 0
+-401 -392 0
+-402 -393 0
+-403 -394 0
+-404 -395 0
+-405 -396 0
+-406 -415 0
+-407 -416 0
+-408 -417 0
+-409 -418 0
+-410 -419 0
+-411 -420 0
+-412 -421 0
+-413 -422 0
+-414 -423 0
+-406 -424 0
+-407 -425 0
+-408 -426 0
+-409 -427 0
+-410 -428 0
+-411 -429 0
+-412 -430 0
+-413 -431 0
+-414 -432 0
+-406 -433 0
+-407 -434 0
+-408 -435 0
+-409 -436 0
+-410 -437 0
+-411 -438 0
+-412 -439 0
+-413 -440 0
+-414 -441 0
+-406 -442 0
+-407 -443 0
+-408 -444 0
+-409 -445 0
+-410 -446 0
+-411 -447 0
+-412 -448 0
+-413 -449 0
+-414 -450 0
+-406 -451 0
+-407 -452 0
+-408 -453 0
+-409 -454 0
+-410 -455 0
+-411 -456 0
+-412 -457 0
+-413 -458 0
+-414 -459 0
+-406 -460 0
+-407 -461 0
+-408 -462 0
+-409 -463 0
+-410 -464 0
+-411 -465 0
+-412 -466 0
+-413 -467 0
+-414 -468 0
+-406 -469 0
+-407 -470 0
+-408 -471 0
+-409 -472 0
+-410 -473 0
+-411 -474 0
+-412 -475 0
+-413 -476 0
+-414 -477 0
+-406 -478 0
+-407 -479 0
+-408 -480 0
+-409 -481 0
+-410 -482 0
+-411 -483 0
+-412 -484 0
+-413 -485 0
+-414 -486 0
+-415 -406 0
+-416 -407 0
+-417 -408 0
+-418 -409 0
+-419 -410 0
+-420 -411 0
+-421 -412 0
+-422 -413 0
+-423 -414 0
+-415 -424 0
+-416 -425 0
+-417 -426 0
+-418 -427 0
+-419 -428 0
+-420 -429 0
+-421 -430 0
+-422 -431 0
+-423 -432 0
+-415 -433 0
+-416 -434 0
+-417 -435 0
+-418 -436 0
+-419 -437 0
+-420 -438 0
+-421 -439 0
+-422 -440 0
+-423 -441 0
+-415 -442 0
+-416 -443 0
+-417 -444 0
+-418 -445 0
+-419 -446 0
+-420 -447 0
+-421 -448 0
+-422 -449 0
+-423 -450 0
+-415 -451 0
+-416 -452 0
+-417 -453 0
+-418 -454 0
+-419 -455 0
+-420 -456 0
+-421 -457 0
+-422 -458 0
+-423 -459 0
+-415 -460 0
+-416 -461 0
+-417 -462 0
+-418 -463 0
+-419 -464 0
+-420 -465 0
+-421 -466 0
+-422 -467 0
+-423 -468 0
+-415 -469 0
+-416 -470 0
+-417 -471 0
+-418 -472 0
+-419 -473 0
+-420 -474 0
+-421 -475 0
+-422 -476 0
+-423 -477 0
+-415 -478 0
+-416 -479 0
+-417 -480 0
+-418 -481 0
+-419 -482 0
+-420 -483 0
+-421 -484 0
+-422 -485 0
+-423 -486 0
+-424 -406 0
+-425 -407 0
+-426 -408 0
+-427 -409 0
+-428 -410 0
+-429 -411 0
+-430 -412 0
+-431 -413 0
+-432 -414 0
+-424 -415 0
+-425 -416 0
+-426 -417 0
+-427 -418 0
+-428 -419 0
+-429 -420 0
+-430 -421 0
+-431 -422 0
+-432 -423 0
+-424 -433 0
+-425 -434 0
+-426 -435 0
+-427 -436 0
+-428 -437 0
+-429 -438 0
+-430 -439 0
+-431 -440 0
+-432 -441 0
+-424 -442 0
+-425 -443 0
+-426 -444 0
+-427 -445 0
+-428 -446 0
+-429 -447 0
+-430 -448 0
+-431 -449 0
+-432 -450 0
+-424 -451 0
+-425 -452 0
+-426 -453 0
+-427 -454 0
+-428 -455 0
+-429 -456 0
+-430 -457 0
+-431 -458 0
+-432 -459 0
+-424 -460 0
+-425 -461 0
+-426 -462 0
+-427 -463 0
+-428 -464 0
+-429 -465 0
+-430 -466 0
+-431 -467 0
+-432 -468 0
+-424 -469 0
+-425 -470 0
+-426 -471 0
+-427 -472 0
+-428 -473 0
+-429 -474 0
+-430 -475 0
+-431 -476 0
+-432 -477 0
+-424 -478 0
+-425 -479 0
+-426 -480 0
+-427 -481 0
+-428 -482 0
+-429 -483 0
+-430 -484 0
+-431 -485 0
+-432 -486 0
+-433 -406 0
+-434 -407 0
+-435 -408 0
+-436 -409 0
+-437 -410 0
+-438 -411 0
+-439 -412 0
+-440 -413 0
+-441 -414 0
+-433 -415 0
+-434 -416 0
+-435 -417 0
+-436 -418 0
+-437 -419 0
+-438 -420 0
+-439 -421 0
+-440 -422 0
+-441 -423 0
+-433 -424 0
+-434 -425 0
+-435 -426 0
+-436 -427 0
+-437 -428 0
+-438 -429 0
+-439 -430 0
+-440 -431 0
+-441 -432 0
+-433 -442 0
+-434 -443 0
+-435 -444 0
+-436 -445 0
+-437 -446 0
+-438 -447 0
+-439 -448 0
+-440 -449 0
+-441 -450 0
+-433 -451 0
+-434 -452 0
+-435 -453 0
+-436 -454 0
+-437 -455 0
+-438 -456 0
+-439 -457 0
+-440 -458 0
+-441 -459 0
+-433 -460 0
+-434 -461 0
+-435 -462 0
+-436 -463 0
+-437 -464 0
+-438 -465 0
+-439 -466 0
+-440 -467 0
+-441 -468 0
+-433 -469 0
+-434 -470 0
+-435 -471 0
+-436 -472 0
+-437 -473 0
+-438 -474 0
+-439 -475 0
+-440 -476 0
+-441 -477 0
+-433 -478 0
+-434 -479 0
+-435 -480 0
+-436 -481 0
+-437 -482 0
+-438 -483 0
+-439 -484 0
+-440 -485 0
+-441 -486 0
+-442 -406 0
+-443 -407 0
+-444 -408 0
+-445 -409 0
+-446 -410 0
+-447 -411 0
+-448 -412 0
+-449 -413 0
+-450 -414 0
+-442 -415 0
+-443 -416 0
+-444 -417 0
+-445 -418 0
+-446 -419 0
+-447 -420 0
+-448 -421 0
+-449 -422 0
+-450 -423 0
+-442 -424 0
+-443 -425 0
+-444 -426 0
+-445 -427 0
+-446 -428 0
+-447 -429 0
+-448 -430 0
+-449 -431 0
+-450 -432 0
+-442 -433 0
+-443 -434 0
+-444 -435 0
+-445 -436 0
+-446 -437 0
+-447 -438 0
+-448 -439 0
+-449 -440 0
+-450 -441 0
+-442 -451 0
+-443 -452 0
+-444 -453 0
+-445 -454 0
+-446 -455 0
+-447 -456 0
+-448 -457 0
+-449 -458 0
+-450 -459 0
+-442 -460 0
+-443 -461 0
+-444 -462 0
+-445 -463 0
+-446 -464 0
+-447 -465 0
+-448 -466 0
+-449 -467 0
+-450 -468 0
+-442 -469 0
+-443 -470 0
+-444 -471 0
+-445 -472 0
+-446 -473 0
+-447 -474 0
+-448 -475 0
+-449 -476 0
+-450 -477 0
+-442 -478 0
+-443 -479 0
+-444 -480 0
+-445 -481 0
+-446 -482 0
+-447 -483 0
+-448 -484 0
+-449 -485 0
+-450 -486 0
+-451 -406 0
+-452 -407 0
+-453 -408 0
+-454 -409 0
+-455 -410 0
+-456 -411 0
+-457 -412 0
+-458 -413 0
+-459 -414 0
+-451 -415 0
+-452 -416 0
+-453 -417 0
+-454 -418 0
+-455 -419 0
+-456 -420 0
+-457 -421 0
+-458 -422 0
+-459 -423 0
+-451 -424 0
+-452 -425 0
+-453 -426 0
+-454 -427 0
+-455 -428 0
+-456 -429 0
+-457 -430 0
+-458 -431 0
+-459 -432 0
+-451 -433 0
+-452 -434 0
+-453 -435 0
+-454 -436 0
+-455 -437 0
+-456 -438 0
+-457 -439 0
+-458 -440 0
+-459 -441 0
+-451 -442 0
+-452 -443 0
+-453 -444 0
+-454 -445 0
+-455 -446 0
+-456 -447 0
+-457 -448 0
+-458 -449 0
+-459 -450 0
+-451 -460 0
+-452 -461 0
+-453 -462 0
+-454 -463 0
+-455 -464 0
+-456 -465 0
+-457 -466 0
+-458 -467 0
+-459 -468 0
+-451 -469 0
+-452 -470 0
+-453 -471 0
+-454 -472 0
+-455 -473 0
+-456 -474 0
+-457 -475 0
+-458 -476 0
+-459 -477 0
+-451 -478 0
+-452 -479 0
+-453 -480 0
+-454 -481 0
+-455 -482 0
+-456 -483 0
+-457 -484 0
+-458 -485 0
+-459 -486 0
+-460 -406 0
+-461 -407 0
+-462 -408 0
+-463 -409 0
+-464 -410 0
+-465 -411 0
+-466 -412 0
+-467 -413 0
+-468 -414 0
+-460 -415 0
+-461 -416 0
+-462 -417 0
+-463 -418 0
+-464 -419 0
+-465 -420 0
+-466 -421 0
+-467 -422 0
+-468 -423 0
+-460 -424 0
+-461 -425 0
+-462 -426 0
+-463 -427 0
+-464 -428 0
+-465 -429 0
+-466 -430 0
+-467 -431 0
+-468 -432 0
+-460 -433 0
+-461 -434 0
+-462 -435 0
+-463 -436 0
+-464 -437 0
+-465 -438 0
+-466 -439 0
+-467 -440 0
+-468 -441 0
+-460 -442 0
+-461 -443 0
+-462 -444 0
+-463 -445 0
+-464 -446 0
+-465 -447 0
+-466 -448 0
+-467 -449 0
+-468 -450 0
+-460 -451 0
+-461 -452 0
+-462 -453 0
+-463 -454 0
+-464 -455 0
+-465 -456 0
+-466 -457 0
+-467 -458 0
+-468 -459 0
+-460 -469 0
+-461 -470 0
+-462 -471 0
+-463 -472 0
+-464 -473 0
+-465 -474 0
+-466 -475 0
+-467 -476 0
+-468 -477 0
+-460 -478 0
+-461 -479 0
+-462 -480 0
+-463 -481 0
+-464 -482 0
+-465 -483 0
+-466 -484 0
+-467 -485 0
+-468 -486 0
+-469 -406 0
+-470 -407 0
+-471 -408 0
+-472 -409 0
+-473 -410 0
+-474 -411 0
+-475 -412 0
+-476 -413 0
+-477 -414 0
+-469 -415 0
+-470 -416 0
+-471 -417 0
+-472 -418 0
+-473 -419 0
+-474 -420 0
+-475 -421 0
+-476 -422 0
+-477 -423 0
+-469 -424 0
+-470 -425 0
+-471 -426 0
+-472 -427 0
+-473 -428 0
+-474 -429 0
+-475 -430 0
+-476 -431 0
+-477 -432 0
+-469 -433 0
+-470 -434 0
+-471 -435 0
+-472 -436 0
+-473 -437 0
+-474 -438 0
+-475 -439 0
+-476 -440 0
+-477 -441 0
+-469 -442 0
+-470 -443 0
+-471 -444 0
+-472 -445 0
+-473 -446 0
+-474 -447 0
+-475 -448 0
+-476 -449 0
+-477 -450 0
+-469 -451 0
+-470 -452 0
+-471 -453 0
+-472 -454 0
+-473 -455 0
+-474 -456 0
+-475 -457 0
+-476 -458 0
+-477 -459 0
+-469 -460 0
+-470 -461 0
+-471 -462 0
+-472 -463 0
+-473 -464 0
+-474 -465 0
+-475 -466 0
+-476 -467 0
+-477 -468 0
+-469 -478 0
+-470 -479 0
+-471 -480 0
+-472 -481 0
+-473 -482 0
+-474 -483 0
+-475 -484 0
+-476 -485 0
+-477 -486 0
+-478 -406 0
+-479 -407 0
+-480 -408 0
+-481 -409 0
+-482 -410 0
+-483 -411 0
+-484 -412 0
+-485 -413 0
+-486 -414 0
+-478 -415 0
+-479 -416 0
+-480 -417 0
+-481 -418 0
+-482 -419 0
+-483 -420 0
+-484 -421 0
+-485 -422 0
+-486 -423 0
+-478 -424 0
+-479 -425 0
+-480 -426 0
+-481 -427 0
+-482 -428 0
+-483 -429 0
+-484 -430 0
+-485 -431 0
+-486 -432 0
+-478 -433 0
+-479 -434 0
+-480 -435 0
+-481 -436 0
+-482 -437 0
+-483 -438 0
+-484 -439 0
+-485 -440 0
+-486 -441 0
+-478 -442 0
+-479 -443 0
+-480 -444 0
+-481 -445 0
+-482 -446 0
+-483 -447 0
+-484 -448 0
+-485 -449 0
+-486 -450 0
+-478 -451 0
+-479 -452 0
+-480 -453 0
+-481 -454 0
+-482 -455 0
+-483 -456 0
+-484 -457 0
+-485 -458 0
+-486 -459 0
+-478 -460 0
+-479 -461 0
+-480 -462 0
+-481 -463 0
+-482 -464 0
+-483 -465 0
+-484 -466 0
+-485 -467 0
+-486 -468 0
+-478 -469 0
+-479 -470 0
+-480 -471 0
+-481 -472 0
+-482 -473 0
+-483 -474 0
+-484 -475 0
+-485 -476 0
+-486 -477 0
+-487 -496 0
+-488 -497 0
+-489 -498 0
+-490 -499 0
+-491 -500 0
+-492 -501 0
+-493 -502 0
+-494 -503 0
+-495 -504 0
+-487 -505 0
+-488 -506 0
+-489 -507 0
+-490 -508 0
+-491 -509 0
+-492 -510 0
+-493 -511 0
+-494 -512 0
+-495 -513 0
+-487 -514 0
+-488 -515 0
+-489 -516 0
+-490 -517 0
+-491 -518 0
+-492 -519 0
+-493 -520 0
+-494 -521 0
+-495 -522 0
+-487 -523 0
+-488 -524 0
+-489 -525 0
+-490 -526 0
+-491 -527 0
+-492 -528 0
+-493 -529 0
+-494 -530 0
+-495 -531 0
+-487 -532 0
+-488 -533 0
+-489 -534 0
+-490 -535 0
+-491 -536 0
+-492 -537 0
+-493 -538 0
+-494 -539 0
+-495 -540 0
+-487 -541 0
+-488 -542 0
+-489 -543 0
+-490 -544 0
+-491 -545 0
+-492 -546 0
+-493 -547 0
+-494 -548 0
+-495 -549 0
+-487 -550 0
+-488 -551 0
+-489 -552 0
+-490 -553 0
+-491 -554 0
+-492 -555 0
+-493 -556 0
+-494 -557 0
+-495 -558 0
+-487 -559 0
+-488 -560 0
+-489 -561 0
+-490 -562 0
+-491 -563 0
+-492 -564 0
+-493 -565 0
+-494 -566 0
+-495 -567 0
+-496 -487 0
+-497 -488 0
+-498 -489 0
+-499 -490 0
+-500 -491 0
+-501 -492 0
+-502 -493 0
+-503 -494 0
+-504 -495 0
+-496 -505 0
+-497 -506 0
+-498 -507 0
+-499 -508 0
+-500 -509 0
+-501 -510 0
+-502 -511 0
+-503 -512 0
+-504 -513 0
+-496 -514 0
+-497 -515 0
+-498 -516 0
+-499 -517 0
+-500 -518 0
+-501 -519 0
+-502 -520 0
+-503 -521 0
+-504 -522 0
+-496 -523 0
+-497 -524 0
+-498 -525 0
+-499 -526 0
+-500 -527 0
+-501 -528 0
+-502 -529 0
+-503 -530 0
+-504 -531 0
+-496 -532 0
+-497 -533 0
+-498 -534 0
+-499 -535 0
+-500 -536 0
+-501 -537 0
+-502 -538 0
+-503 -539 0
+-504 -540 0
+-496 -541 0
+-497 -542 0
+-498 -543 0
+-499 -544 0
+-500 -545 0
+-501 -546 0
+-502 -547 0
+-503 -548 0
+-504 -549 0
+-496 -550 0
+-497 -551 0
+-498 -552 0
+-499 -553 0
+-500 -554 0
+-501 -555 0
+-502 -556 0
+-503 -557 0
+-504 -558 0
+-496 -559 0
+-497 -560 0
+-498 -561 0
+-499 -562 0
+-500 -563 0
+-501 -564 0
+-502 -565 0
+-503 -566 0
+-504 -567 0
+-505 -487 0
+-506 -488 0
+-507 -489 0
+-508 -490 0
+-509 -491 0
+-510 -492 0
+-511 -493 0
+-512 -494 0
+-513 -495 0
+-505 -496 0
+-506 -497 0
+-507 -498 0
+-508 -499 0
+-509 -500 0
+-510 -501 0
+-511 -502 0
+-512 -503 0
+-513 -504 0
+-505 -514 0
+-506 -515 0
+-507 -516 0
+-508 -517 0
+-509 -518 0
+-510 -519 0
+-511 -520 0
+-512 -521 0
+-513 -522 0
+-505 -523 0
+-506 -524 0
+-507 -525 0
+-508 -526 0
+-509 -527 0
+-510 -528 0
+-511 -529 0
+-512 -530 0
+-513 -531 0
+-505 -532 0
+-506 -533 0
+-507 -534 0
+-508 -535 0
+-509 -536 0
+-510 -537 0
+-511 -538 0
+-512 -539 0
+-513 -540 0
+-505 -541 0
+-506 -542 0
+-507 -543 0
+-508 -544 0
+-509 -545 0
+-510 -546 0
+-511 -547 0
+-512 -548 0
+-513 -549 0
+-505 -550 0
+-506 -551 0
+-507 -552 0
+-508 -553 0
+-509 -554 0
+-510 -555 0
+-511 -556 0
+-512 -557 0
+-513 -558 0
+-505 -559 0
+-506 -560 0
+-507 -561 0
+-508 -562 0
+-509 -563 0
+-510 -564 0
+-511 -565 0
+-512 -566 0
+-513 -567 0
+-514 -487 0
+-515 -488 0
+-516 -489 0
+-517 -490 0
+-518 -491 0
+-519 -492 0
+-520 -493 0
+-521 -494 0
+-522 -495 0
+-514 -496 0
+-515 -497 0
+-516 -498 0
+-517 -499 0
+-518 -500 0
+-519 -501 0
+-520 -502 0
+-521 -503 0
+-522 -504 0
+-514 -505 0
+-515 -506 0
+-516 -507 0
+-517 -508 0
+-518 -509 0
+-519 -510 0
+-520 -511 0
+-521 -512 0
+-522 -513 0
+-514 -523 0
+-515 -524 0
+-516 -525 0
+-517 -526 0
+-518 -527 0
+-519 -528 0
+-520 -529 0
+-521 -530 0
+-522 -531 0
+-514 -532 0
+-515 -533 0
+-516 -534 0
+-517 -535 0
+-518 -536 0
+-519 -537 0
+-520 -538 0
+-521 -539 0
+-522 -540 0
+-514 -541 0
+-515 -542 0
+-516 -543 0
+-517 -544 0
+-518 -545 0
+-519 -546 0
+-520 -547 0
+-521 -548 0
+-522 -549 0
+-514 -550 0
+-515 -551 0
+-516 -552 0
+-517 -553 0
+-518 -554 0
+-519 -555 0
+-520 -556 0
+-521 -557 0
+-522 -558 0
+-514 -559 0
+-515 -560 0
+-516 -561 0
+-517 -562 0
+-518 -563 0
+-519 -564 0
+-520 -565 0
+-521 -566 0
+-522 -567 0
+-523 -487 0
+-524 -488 0
+-525 -489 0
+-526 -490 0
+-527 -491 0
+-528 -492 0
+-529 -493 0
+-530 -494 0
+-531 -495 0
+-523 -496 0
+-524 -497 0
+-525 -498 0
+-526 -499 0
+-527 -500 0
+-528 -501 0
+-529 -502 0
+-530 -503 0
+-531 -504 0
+-523 -505 0
+-524 -506 0
+-525 -507 0
+-526 -508 0
+-527 -509 0
+-528 -510 0
+-529 -511 0
+-530 -512 0
+-531 -513 0
+-523 -514 0
+-524 -515 0
+-525 -516 0
+-526 -517 0
+-527 -518 0
+-528 -519 0
+-529 -520 0
+-530 -521 0
+-531 -522 0
+-523 -532 0
+-524 -533 0
+-525 -534 0
+-526 -535 0
+-527 -536 0
+-528 -537 0
+-529 -538 0
+-530 -539 0
+-531 -540 0
+-523 -541 0
+-524 -542 0
+-525 -543 0
+-526 -544 0
+-527 -545 0
+-528 -546 0
+-529 -547 0
+-530 -548 0
+-531 -549 0
+-523 -550 0
+-524 -551 0
+-525 -552 0
+-526 -553 0
+-527 -554 0
+-528 -555 0
+-529 -556 0
+-530 -557 0
+-531 -558 0
+-523 -559 0
+-524 -560 0
+-525 -561 0
+-526 -562 0
+-527 -563 0
+-528 -564 0
+-529 -565 0
+-530 -566 0
+-531 -567 0
+-532 -487 0
+-533 -488 0
+-534 -489 0
+-535 -490 0
+-536 -491 0
+-537 -492 0
+-538 -493 0
+-539 -494 0
+-540 -495 0
+-532 -496 0
+-533 -497 0
+-534 -498 0
+-535 -499 0
+-536 -500 0
+-537 -501 0
+-538 -502 0
+-539 -503 0
+-540 -504 0
+-532 -505 0
+-533 -506 0
+-534 -507 0
+-535 -508 0
+-536 -509 0
+-537 -510 0
+-538 -511 0
+-539 -512 0
+-540 -513 0
+-532 -514 0
+-533 -515 0
+-534 -516 0
+-535 -517 0
+-536 -518 0
+-537 -519 0
+-538 -520 0
+-539 -521 0
+-540 -522 0
+-532 -523 0
+-533 -524 0
+-534 -525 0
+-535 -526 0
+-536 -527 0
+-537 -528 0
+-538 -529 0
+-539 -530 0
+-540 -531 0
+-532 -541 0
+-533 -542 0
+-534 -543 0
+-535 -544 0
+-536 -545 0
+-537 -546 0
+-538 -547 0
+-539 -548 0
+-540 -549 0
+-532 -550 0
+-533 -551 0
+-534 -552 0
+-535 -553 0
+-536 -554 0
+-537 -555 0
+-538 -556 0
+-539 -557 0
+-540 -558 0
+-532 -559 0
+-533 -560 0
+-534 -561 0
+-535 -562 0
+-536 -563 0
+-537 -564 0
+-538 -565 0
+-539 -566 0
+-540 -567 0
+-541 -487 0
+-542 -488 0
+-543 -489 0
+-544 -490 0
+-545 -491 0
+-546 -492 0
+-547 -493 0
+-548 -494 0
+-549 -495 0
+-541 -496 0
+-542 -497 0
+-543 -498 0
+-544 -499 0
+-545 -500 0
+-546 -501 0
+-547 -502 0
+-548 -503 0
+-549 -504 0
+-541 -505 0
+-542 -506 0
+-543 -507 0
+-544 -508 0
+-545 -509 0
+-546 -510 0
+-547 -511 0
+-548 -512 0
+-549 -513 0
+-541 -514 0
+-542 -515 0
+-543 -516 0
+-544 -517 0
+-545 -518 0
+-546 -519 0
+-547 -520 0
+-548 -521 0
+-549 -522 0
+-541 -523 0
+-542 -524 0
+-543 -525 0
+-544 -526 0
+-545 -527 0
+-546 -528 0
+-547 -529 0
+-548 -530 0
+-549 -531 0
+-541 -532 0
+-542 -533 0
+-543 -534 0
+-544 -535 0
+-545 -536 0
+-546 -537 0
+-547 -538 0
+-548 -539 0
+-549 -540 0
+-541 -550 0
+-542 -551 0
+-543 -552 0
+-544 -553 0
+-545 -554 0
+-546 -555 0
+-547 -556 0
+-548 -557 0
+-549 -558 0
+-541 -559 0
+-542 -560 0
+-543 -561 0
+-544 -562 0
+-545 -563 0
+-546 -564 0
+-547 -565 0
+-548 -566 0
+-549 -567 0
+-550 -487 0
+-551 -488 0
+-552 -489 0
+-553 -490 0
+-554 -491 0
+-555 -492 0
+-556 -493 0
+-557 -494 0
+-558 -495 0
+-550 -496 0
+-551 -497 0
+-552 -498 0
+-553 -499 0
+-554 -500 0
+-555 -501 0
+-556 -502 0
+-557 -503 0
+-558 -504 0
+-550 -505 0
+-551 -506 0
+-552 -507 0
+-553 -508 0
+-554 -509 0
+-555 -510 0
+-556 -511 0
+-557 -512 0
+-558 -513 0
+-550 -514 0
+-551 -515 0
+-552 -516 0
+-553 -517 0
+-554 -518 0
+-555 -519 0
+-556 -520 0
+-557 -521 0
+-558 -522 0
+-550 -523 0
+-551 -524 0
+-552 -525 0
+-553 -526 0
+-554 -527 0
+-555 -528 0
+-556 -529 0
+-557 -530 0
+-558 -531 0
+-550 -532 0
+-551 -533 0
+-552 -534 0
+-553 -535 0
+-554 -536 0
+-555 -537 0
+-556 -538 0
+-557 -539 0
+-558 -540 0
+-550 -541 0
+-551 -542 0
+-552 -543 0
+-553 -544 0
+-554 -545 0
+-555 -546 0
+-556 -547 0
+-557 -548 0
+-558 -549 0
+-550 -559 0
+-551 -560 0
+-552 -561 0
+-553 -562 0
+-554 -563 0
+-555 -564 0
+-556 -565 0
+-557 -566 0
+-558 -567 0
+-559 -487 0
+-560 -488 0
+-561 -489 0
+-562 -490 0
+-563 -491 0
+-564 -492 0
+-565 -493 0
+-566 -494 0
+-567 -495 0
+-559 -496 0
+-560 -497 0
+-561 -498 0
+-562 -499 0
+-563 -500 0
+-564 -501 0
+-565 -502 0
+-566 -503 0
+-567 -504 0
+-559 -505 0
+-560 -506 0
+-561 -507 0
+-562 -508 0
+-563 -509 0
+-564 -510 0
+-565 -511 0
+-566 -512 0
+-567 -513 0
+-559 -514 0
+-560 -515 0
+-561 -516 0
+-562 -517 0
+-563 -518 0
+-564 -519 0
+-565 -520 0
+-566 -521 0
+-567 -522 0
+-559 -523 0
+-560 -524 0
+-561 -525 0
+-562 -526 0
+-563 -527 0
+-564 -528 0
+-565 -529 0
+-566 -530 0
+-567 -531 0
+-559 -532 0
+-560 -533 0
+-561 -534 0
+-562 -535 0
+-563 -536 0
+-564 -537 0
+-565 -538 0
+-566 -539 0
+-567 -540 0
+-559 -541 0
+-560 -542 0
+-561 -543 0
+-562 -544 0
+-563 -545 0
+-564 -546 0
+-565 -547 0
+-566 -548 0
+-567 -549 0
+-559 -550 0
+-560 -551 0
+-561 -552 0
+-562 -553 0
+-563 -554 0
+-564 -555 0
+-565 -556 0
+-566 -557 0
+-567 -558 0
+-568 -577 0
+-569 -578 0
+-570 -579 0
+-571 -580 0
+-572 -581 0
+-573 -582 0
+-574 -583 0
+-575 -584 0
+-576 -585 0
+-568 -586 0
+-569 -587 0
+-570 -588 0
+-571 -589 0
+-572 -590 0
+-573 -591 0
+-574 -592 0
+-575 -593 0
+-576 -594 0
+-568 -595 0
+-569 -596 0
+-570 -597 0
+-571 -598 0
+-572 -599 0
+-573 -600 0
+-574 -601 0
+-575 -602 0
+-576 -603 0
+-568 -604 0
+-569 -605 0
+-570 -606 0
+-571 -607 0
+-572 -608 0
+-573 -609 0
+-574 -610 0
+-575 -611 0
+-576 -612 0
+-568 -613 0
+-569 -614 0
+-570 -615 0
+-571 -616 0
+-572 -617 0
+-573 -618 0
+-574 -619 0
+-575 -620 0
+-576 -621 0
+-568 -622 0
+-569 -623 0
+-570 -624 0
+-571 -625 0
+-572 -626 0
+-573 -627 0
+-574 -628 0
+-575 -629 0
+-576 -630 0
+-568 -631 0
+-569 -632 0
+-570 -633 0
+-571 -634 0
+-572 -635 0
+-573 -636 0
+-574 -637 0
+-575 -638 0
+-576 -639 0
+-568 -640 0
+-569 -641 0
+-570 -642 0
+-571 -643 0
+-572 -644 0
+-573 -645 0
+-574 -646 0
+-575 -647 0
+-576 -648 0
+-577 -568 0
+-578 -569 0
+-579 -570 0
+-580 -571 0
+-581 -572 0
+-582 -573 0
+-583 -574 0
+-584 -575 0
+-585 -576 0
+-577 -586 0
+-578 -587 0
+-579 -588 0
+-580 -589 0
+-581 -590 0
+-582 -591 0
+-583 -592 0
+-584 -593 0
+-585 -594 0
+-577 -595 0
+-578 -596 0
+-579 -597 0
+-580 -598 0
+-581 -599 0
+-582 -600 0
+-583 -601 0
+-584 -602 0
+-585 -603 0
+-577 -604 0
+-578 -605 0
+-579 -606 0
+-580 -607 0
+-581 -608 0
+-582 -609 0
+-583 -610 0
+-584 -611 0
+-585 -612 0
+-577 -613 0
+-578 -614 0
+-579 -615 0
+-580 -616 0
+-581 -617 0
+-582 -618 0
+-583 -619 0
+-584 -620 0
+-585 -621 0
+-577 -622 0
+-578 -623 0
+-579 -624 0
+-580 -625 0
+-581 -626 0
+-582 -627 0
+-583 -628 0
+-584 -629 0
+-585 -630 0
+-577 -631 0
+-578 -632 0
+-579 -633 0
+-580 -634 0
+-581 -635 0
+-582 -636 0
+-583 -637 0
+-584 -638 0
+-585 -639 0
+-577 -640 0
+-578 -641 0
+-579 -642 0
+-580 -643 0
+-581 -644 0
+-582 -645 0
+-583 -646 0
+-584 -647 0
+-585 -648 0
+-586 -568 0
+-587 -569 0
+-588 -570 0
+-589 -571 0
+-590 -572 0
+-591 -573 0
+-592 -574 0
+-593 -575 0
+-594 -576 0
+-586 -577 0
+-587 -578 0
+-588 -579 0
+-589 -580 0
+-590 -581 0
+-591 -582 0
+-592 -583 0
+-593 -584 0
+-594 -585 0
+-586 -595 0
+-587 -596 0
+-588 -597 0
+-589 -598 0
+-590 -599 0
+-591 -600 0
+-592 -601 0
+-593 -602 0
+-594 -603 0
+-586 -604 0
+-587 -605 0
+-588 -606 0
+-589 -607 0
+-590 -608 0
+-591 -609 0
+-592 -610 0
+-593 -611 0
+-594 -612 0
+-586 -613 0
+-587 -614 0
+-588 -615 0
+-589 -616 0
+-590 -617 0
+-591 -618 0
+-592 -619 0
+-593 -620 0
+-594 -621 0
+-586 -622 0
+-587 -623 0
+-588 -624 0
+-589 -625 0
+-590 -626 0
+-591 -627 0
+-592 -628 0
+-593 -629 0
+-594 -630 0
+-586 -631 0
+-587 -632 0
+-588 -633 0
+-589 -634 0
+-590 -635 0
+-591 -636 0
+-592 -637 0
+-593 -638 0
+-594 -639 0
+-586 -640 0
+-587 -641 0
+-588 -642 0
+-589 -643 0
+-590 -644 0
+-591 -645 0
+-592 -646 0
+-593 -647 0
+-594 -648 0
+-595 -568 0
+-596 -569 0
+-597 -570 0
+-598 -571 0
+-599 -572 0
+-600 -573 0
+-601 -574 0
+-602 -575 0
+-603 -576 0
+-595 -577 0
+-596 -578 0
+-597 -579 0
+-598 -580 0
+-599 -581 0
+-600 -582 0
+-601 -583 0
+-602 -584 0
+-603 -585 0
+-595 -586 0
+-596 -587 0
+-597 -588 0
+-598 -589 0
+-599 -590 0
+-600 -591 0
+-601 -592 0
+-602 -593 0
+-603 -594 0
+-595 -604 0
+-596 -605 0
+-597 -606 0
+-598 -607 0
+-599 -608 0
+-600 -609 0
+-601 -610 0
+-602 -611 0
+-603 -612 0
+-595 -613 0
+-596 -614 0
+-597 -615 0
+-598 -616 0
+-599 -617 0
+-600 -618 0
+-601 -619 0
+-602 -620 0
+-603 -621 0
+-595 -622 0
+-596 -623 0
+-597 -624 0
+-598 -625 0
+-599 -626 0
+-600 -627 0
+-601 -628 0
+-602 -629 0
+-603 -630 0
+-595 -631 0
+-596 -632 0
+-597 -633 0
+-598 -634 0
+-599 -635 0
+-600 -636 0
+-601 -637 0
+-602 -638 0
+-603 -639 0
+-595 -640 0
+-596 -641 0
+-597 -642 0
+-598 -643 0
+-599 -644 0
+-600 -645 0
+-601 -646 0
+-602 -647 0
+-603 -648 0
+-604 -568 0
+-605 -569 0
+-606 -570 0
+-607 -571 0
+-608 -572 0
+-609 -573 0
+-610 -574 0
+-611 -575 0
+-612 -576 0
+-604 -577 0
+-605 -578 0
+-606 -579 0
+-607 -580 0
+-608 -581 0
+-609 -582 0
+-610 -583 0
+-611 -584 0
+-612 -585 0
+-604 -586 0
+-605 -587 0
+-606 -588 0
+-607 -589 0
+-608 -590 0
+-609 -591 0
+-610 -592 0
+-611 -593 0
+-612 -594 0
+-604 -595 0
+-605 -596 0
+-606 -597 0
+-607 -598 0
+-608 -599 0
+-609 -600 0
+-610 -601 0
+-611 -602 0
+-612 -603 0
+-604 -613 0
+-605 -614 0
+-606 -615 0
+-607 -616 0
+-608 -617 0
+-609 -618 0
+-610 -619 0
+-611 -620 0
+-612 -621 0
+-604 -622 0
+-605 -623 0
+-606 -624 0
+-607 -625 0
+-608 -626 0
+-609 -627 0
+-610 -628 0
+-611 -629 0
+-612 -630 0
+-604 -631 0
+-605 -632 0
+-606 -633 0
+-607 -634 0
+-608 -635 0
+-609 -636 0
+-610 -637 0
+-611 -638 0
+-612 -639 0
+-604 -640 0
+-605 -641 0
+-606 -642 0
+-607 -643 0
+-608 -644 0
+-609 -645 0
+-610 -646 0
+-611 -647 0
+-612 -648 0
+-613 -568 0
+-614 -569 0
+-615 -570 0
+-616 -571 0
+-617 -572 0
+-618 -573 0
+-619 -574 0
+-620 -575 0
+-621 -576 0
+-613 -577 0
+-614 -578 0
+-615 -579 0
+-616 -580 0
+-617 -581 0
+-618 -582 0
+-619 -583 0
+-620 -584 0
+-621 -585 0
+-613 -586 0
+-614 -587 0
+-615 -588 0
+-616 -589 0
+-617 -590 0
+-618 -591 0
+-619 -592 0
+-620 -593 0
+-621 -594 0
+-613 -595 0
+-614 -596 0
+-615 -597 0
+-616 -598 0
+-617 -599 0
+-618 -600 0
+-619 -601 0
+-620 -602 0
+-621 -603 0
+-613 -604 0
+-614 -605 0
+-615 -606 0
+-616 -607 0
+-617 -608 0
+-618 -609 0
+-619 -610 0
+-620 -611 0
+-621 -612 0
+-613 -622 0
+-614 -623 0
+-615 -624 0
+-616 -625 0
+-617 -626 0
+-618 -627 0
+-619 -628 0
+-620 -629 0
+-621 -630 0
+-613 -631 0
+-614 -632 0
+-615 -633 0
+-616 -634 0
+-617 -635 0
+-618 -636 0
+-619 -637 0
+-620 -638 0
+-621 -639 0
+-613 -640 0
+-614 -641 0
+-615 -642 0
+-616 -643 0
+-617 -644 0
+-618 -645 0
+-619 -646 0
+-620 -647 0
+-621 -648 0
+-622 -568 0
+-623 -569 0
+-624 -570 0
+-625 -571 0
+-626 -572 0
+-627 -573 0
+-628 -574 0
+-629 -575 0
+-630 -576 0
+-622 -577 0
+-623 -578 0
+-624 -579 0
+-625 -580 0
+-626 -581 0
+-627 -582 0
+-628 -583 0
+-629 -584 0
+-630 -585 0
+-622 -586 0
+-623 -587 0
+-624 -588 0
+-625 -589 0
+-626 -590 0
+-627 -591 0
+-628 -592 0
+-629 -593 0
+-630 -594 0
+-622 -595 0
+-623 -596 0
+-624 -597 0
+-625 -598 0
+-626 -599 0
+-627 -600 0
+-628 -601 0
+-629 -602 0
+-630 -603 0
+-622 -604 0
+-623 -605 0
+-624 -606 0
+-625 -607 0
+-626 -608 0
+-627 -609 0
+-628 -610 0
+-629 -611 0
+-630 -612 0
+-622 -613 0
+-623 -614 0
+-624 -615 0
+-625 -616 0
+-626 -617 0
+-627 -618 0
+-628 -619 0
+-629 -620 0
+-630 -621 0
+-622 -631 0
+-623 -632 0
+-624 -633 0
+-625 -634 0
+-626 -635 0
+-627 -636 0
+-628 -637 0
+-629 -638 0
+-630 -639 0
+-622 -640 0
+-623 -641 0
+-624 -642 0
+-625 -643 0
+-626 -644 0
+-627 -645 0
+-628 -646 0
+-629 -647 0
+-630 -648 0
+-631 -568 0
+-632 -569 0
+-633 -570 0
+-634 -571 0
+-635 -572 0
+-636 -573 0
+-637 -574 0
+-638 -575 0
+-639 -576 0
+-631 -577 0
+-632 -578 0
+-633 -579 0
+-634 -580 0
+-635 -581 0
+-636 -582 0
+-637 -583 0
+-638 -584 0
+-639 -585 0
+-631 -586 0
+-632 -587 0
+-633 -588 0
+-634 -589 0
+-635 -590 0
+-636 -591 0
+-637 -592 0
+-638 -593 0
+-639 -594 0
+-631 -595 0
+-632 -596 0
+-633 -597 0
+-634 -598 0
+-635 -599 0
+-636 -600 0
+-637 -601 0
+-638 -602 0
+-639 -603 0
+-631 -604 0
+-632 -605 0
+-633 -606 0
+-634 -607 0
+-635 -608 0
+-636 -609 0
+-637 -610 0
+-638 -611 0
+-639 -612 0
+-631 -613 0
+-632 -614 0
+-633 -615 0
+-634 -616 0
+-635 -617 0
+-636 -618 0
+-637 -619 0
+-638 -620 0
+-639 -621 0
+-631 -622 0
+-632 -623 0
+-633 -624 0
+-634 -625 0
+-635 -626 0
+-636 -627 0
+-637 -628 0
+-638 -629 0
+-639 -630 0
+-631 -640 0
+-632 -641 0
+-633 -642 0
+-634 -643 0
+-635 -644 0
+-636 -645 0
+-637 -646 0
+-638 -647 0
+-639 -648 0
+-640 -568 0
+-641 -569 0
+-642 -570 0
+-643 -571 0
+-644 -572 0
+-645 -573 0
+-646 -574 0
+-647 -575 0
+-648 -576 0
+-640 -577 0
+-641 -578 0
+-642 -579 0
+-643 -580 0
+-644 -581 0
+-645 -582 0
+-646 -583 0
+-647 -584 0
+-648 -585 0
+-640 -586 0
+-641 -587 0
+-642 -588 0
+-643 -589 0
+-644 -590 0
+-645 -591 0
+-646 -592 0
+-647 -593 0
+-648 -594 0
+-640 -595 0
+-641 -596 0
+-642 -597 0
+-643 -598 0
+-644 -599 0
+-645 -600 0
+-646 -601 0
+-647 -602 0
+-648 -603 0
+-640 -604 0
+-641 -605 0
+-642 -606 0
+-643 -607 0
+-644 -608 0
+-645 -609 0
+-646 -610 0
+-647 -611 0
+-648 -612 0
+-640 -613 0
+-641 -614 0
+-642 -615 0
+-643 -616 0
+-644 -617 0
+-645 -618 0
+-646 -619 0
+-647 -620 0
+-648 -621 0
+-640 -622 0
+-641 -623 0
+-642 -624 0
+-643 -625 0
+-644 -626 0
+-645 -627 0
+-646 -628 0
+-647 -629 0
+-648 -630 0
+-640 -631 0
+-641 -632 0
+-642 -633 0
+-643 -634 0
+-644 -635 0
+-645 -636 0
+-646 -637 0
+-647 -638 0
+-648 -639 0
+-649 -658 0
+-650 -659 0
+-651 -660 0
+-652 -661 0
+-653 -662 0
+-654 -663 0
+-655 -664 0
+-656 -665 0
+-657 -666 0
+-649 -667 0
+-650 -668 0
+-651 -669 0
+-652 -670 0
+-653 -671 0
+-654 -672 0
+-655 -673 0
+-656 -674 0
+-657 -675 0
+-649 -676 0
+-650 -677 0
+-651 -678 0
+-652 -679 0
+-653 -680 0
+-654 -681 0
+-655 -682 0
+-656 -683 0
+-657 -684 0
+-649 -685 0
+-650 -686 0
+-651 -687 0
+-652 -688 0
+-653 -689 0
+-654 -690 0
+-655 -691 0
+-656 -692 0
+-657 -693 0
+-649 -694 0
+-650 -695 0
+-651 -696 0
+-652 -697 0
+-653 -698 0
+-654 -699 0
+-655 -700 0
+-656 -701 0
+-657 -702 0
+-649 -703 0
+-650 -704 0
+-651 -705 0
+-652 -706 0
+-653 -707 0
+-654 -708 0
+-655 -709 0
+-656 -710 0
+-657 -711 0
+-649 -712 0
+-650 -713 0
+-651 -714 0
+-652 -715 0
+-653 -716 0
+-654 -717 0
+-655 -718 0
+-656 -719 0
+-657 -720 0
+-649 -721 0
+-650 -722 0
+-651 -723 0
+-652 -724 0
+-653 -725 0
+-654 -726 0
+-655 -727 0
+-656 -728 0
+-657 -729 0
+-658 -649 0
+-659 -650 0
+-660 -651 0
+-661 -652 0
+-662 -653 0
+-663 -654 0
+-664 -655 0
+-665 -656 0
+-666 -657 0
+-658 -667 0
+-659 -668 0
+-660 -669 0
+-661 -670 0
+-662 -671 0
+-663 -672 0
+-664 -673 0
+-665 -674 0
+-666 -675 0
+-658 -676 0
+-659 -677 0
+-660 -678 0
+-661 -679 0
+-662 -680 0
+-663 -681 0
+-664 -682 0
+-665 -683 0
+-666 -684 0
+-658 -685 0
+-659 -686 0
+-660 -687 0
+-661 -688 0
+-662 -689 0
+-663 -690 0
+-664 -691 0
+-665 -692 0
+-666 -693 0
+-658 -694 0
+-659 -695 0
+-660 -696 0
+-661 -697 0
+-662 -698 0
+-663 -699 0
+-664 -700 0
+-665 -701 0
+-666 -702 0
+-658 -703 0
+-659 -704 0
+-660 -705 0
+-661 -706 0
+-662 -707 0
+-663 -708 0
+-664 -709 0
+-665 -710 0
+-666 -711 0
+-658 -712 0
+-659 -713 0
+-660 -714 0
+-661 -715 0
+-662 -716 0
+-663 -717 0
+-664 -718 0
+-665 -719 0
+-666 -720 0
+-658 -721 0
+-659 -722 0
+-660 -723 0
+-661 -724 0
+-662 -725 0
+-663 -726 0
+-664 -727 0
+-665 -728 0
+-666 -729 0
+-667 -649 0
+-668 -650 0
+-669 -651 0
+-670 -652 0
+-671 -653 0
+-672 -654 0
+-673 -655 0
+-674 -656 0
+-675 -657 0
+-667 -658 0
+-668 -659 0
+-669 -660 0
+-670 -661 0
+-671 -662 0
+-672 -663 0
+-673 -664 0
+-674 -665 0
+-675 -666 0
+-667 -676 0
+-668 -677 0
+-669 -678 0
+-670 -679 0
+-671 -680 0
+-672 -681 0
+-673 -682 0
+-674 -683 0
+-675 -684 0
+-667 -685 0
+-668 -686 0
+-669 -687 0
+-670 -688 0
+-671 -689 0
+-672 -690 0
+-673 -691 0
+-674 -692 0
+-675 -693 0
+-667 -694 0
+-668 -695 0
+-669 -696 0
+-670 -697 0
+-671 -698 0
+-672 -699 0
+-673 -700 0
+-674 -701 0
+-675 -702 0
+-667 -703 0
+-668 -704 0
+-669 -705 0
+-670 -706 0
+-671 -707 0
+-672 -708 0
+-673 -709 0
+-674 -710 0
+-675 -711 0
+-667 -712 0
+-668 -713 0
+-669 -714 0
+-670 -715 0
+-671 -716 0
+-672 -717 0
+-673 -718 0
+-674 -719 0
+-675 -720 0
+-667 -721 0
+-668 -722 0
+-669 -723 0
+-670 -724 0
+-671 -725 0
+-672 -726 0
+-673 -727 0
+-674 -728 0
+-675 -729 0
+-676 -649 0
+-677 -650 0
+-678 -651 0
+-679 -652 0
+-680 -653 0
+-681 -654 0
+-682 -655 0
+-683 -656 0
+-684 -657 0
+-676 -658 0
+-677 -659 0
+-678 -660 0
+-679 -661 0
+-680 -662 0
+-681 -663 0
+-682 -664 0
+-683 -665 0
+-684 -666 0
+-676 -667 0
+-677 -668 0
+-678 -669 0
+-679 -670 0
+-680 -671 0
+-681 -672 0
+-682 -673 0
+-683 -674 0
+-684 -675 0
+-676 -685 0
+-677 -686 0
+-678 -687 0
+-679 -688 0
+-680 -689 0
+-681 -690 0
+-682 -691 0
+-683 -692 0
+-684 -693 0
+-676 -694 0
+-677 -695 0
+-678 -696 0
+-679 -697 0
+-680 -698 0
+-681 -699 0
+-682 -700 0
+-683 -701 0
+-684 -702 0
+-676 -703 0
+-677 -704 0
+-678 -705 0
+-679 -706 0
+-680 -707 0
+-681 -708 0
+-682 -709 0
+-683 -710 0
+-684 -711 0
+-676 -712 0
+-677 -713 0
+-678 -714 0
+-679 -715 0
+-680 -716 0
+-681 -717 0
+-682 -718 0
+-683 -719 0
+-684 -720 0
+-676 -721 0
+-677 -722 0
+-678 -723 0
+-679 -724 0
+-680 -725 0
+-681 -726 0
+-682 -727 0
+-683 -728 0
+-684 -729 0
+-685 -649 0
+-686 -650 0
+-687 -651 0
+-688 -652 0
+-689 -653 0
+-690 -654 0
+-691 -655 0
+-692 -656 0
+-693 -657 0
+-685 -658 0
+-686 -659 0
+-687 -660 0
+-688 -661 0
+-689 -662 0
+-690 -663 0
+-691 -664 0
+-692 -665 0
+-693 -666 0
+-685 -667 0
+-686 -668 0
+-687 -669 0
+-688 -670 0
+-689 -671 0
+-690 -672 0
+-691 -673 0
+-692 -674 0
+-693 -675 0
+-685 -676 0
+-686 -677 0
+-687 -678 0
+-688 -679 0
+-689 -680 0
+-690 -681 0
+-691 -682 0
+-692 -683 0
+-693 -684 0
+-685 -694 0
+-686 -695 0
+-687 -696 0
+-688 -697 0
+-689 -698 0
+-690 -699 0
+-691 -700 0
+-692 -701 0
+-693 -702 0
+-685 -703 0
+-686 -704 0
+-687 -705 0
+-688 -706 0
+-689 -707 0
+-690 -708 0
+-691 -709 0
+-692 -710 0
+-693 -711 0
+-685 -712 0
+-686 -713 0
+-687 -714 0
+-688 -715 0
+-689 -716 0
+-690 -717 0
+-691 -718 0
+-692 -719 0
+-693 -720 0
+-685 -721 0
+-686 -722 0
+-687 -723 0
+-688 -724 0
+-689 -725 0
+-690 -726 0
+-691 -727 0
+-692 -728 0
+-693 -729 0
+-694 -649 0
+-695 -650 0
+-696 -651 0
+-697 -652 0
+-698 -653 0
+-699 -654 0
+-700 -655 0
+-701 -656 0
+-702 -657 0
+-694 -658 0
+-695 -659 0
+-696 -660 0
+-697 -661 0
+-698 -662 0
+-699 -663 0
+-700 -664 0
+-701 -665 0
+-702 -666 0
+-694 -667 0
+-695 -668 0
+-696 -669 0
+-697 -670 0
+-698 -671 0
+-699 -672 0
+-700 -673 0
+-701 -674 0
+-702 -675 0
+-694 -676 0
+-695 -677 0
+-696 -678 0
+-697 -679 0
+-698 -680 0
+-699 -681 0
+-700 -682 0
+-701 -683 0
+-702 -684 0
+-694 -685 0
+-695 -686 0
+-696 -687 0
+-697 -688 0
+-698 -689 0
+-699 -690 0
+-700 -691 0
+-701 -692 0
+-702 -693 0
+-694 -703 0
+-695 -704 0
+-696 -705 0
+-697 -706 0
+-698 -707 0
+-699 -708 0
+-700 -709 0
+-701 -710 0
+-702 -711 0
+-694 -712 0
+-695 -713 0
+-696 -714 0
+-697 -715 0
+-698 -716 0
+-699 -717 0
+-700 -718 0
+-701 -719 0
+-702 -720 0
+-694 -721 0
+-695 -722 0
+-696 -723 0
+-697 -724 0
+-698 -725 0
+-699 -726 0
+-700 -727 0
+-701 -728 0
+-702 -729 0
+-703 -649 0
+-704 -650 0
+-705 -651 0
+-706 -652 0
+-707 -653 0
+-708 -654 0
+-709 -655 0
+-710 -656 0
+-711 -657 0
+-703 -658 0
+-704 -659 0
+-705 -660 0
+-706 -661 0
+-707 -662 0
+-708 -663 0
+-709 -664 0
+-710 -665 0
+-711 -666 0
+-703 -667 0
+-704 -668 0
+-705 -669 0
+-706 -670 0
+-707 -671 0
+-708 -672 0
+-709 -673 0
+-710 -674 0
+-711 -675 0
+-703 -676 0
+-704 -677 0
+-705 -678 0
+-706 -679 0
+-707 -680 0
+-708 -681 0
+-709 -682 0
+-710 -683 0
+-711 -684 0
+-703 -685 0
+-704 -686 0
+-705 -687 0
+-706 -688 0
+-707 -689 0
+-708 -690 0
+-709 -691 0
+-710 -692 0
+-711 -693 0
+-703 -694 0
+-704 -695 0
+-705 -696 0
+-706 -697 0
+-707 -698 0
+-708 -699 0
+-709 -700 0
+-710 -701 0
+-711 -702 0
+-703 -712 0
+-704 -713 0
+-705 -714 0
+-706 -715 0
+-707 -716 0
+-708 -717 0
+-709 -718 0
+-710 -719 0
+-711 -720 0
+-703 -721 0
+-704 -722 0
+-705 -723 0
+-706 -724 0
+-707 -725 0
+-708 -726 0
+-709 -727 0
+-710 -728 0
+-711 -729 0
+-712 -649 0
+-713 -650 0
+-714 -651 0
+-715 -652 0
+-716 -653 0
+-717 -654 0
+-718 -655 0
+-719 -656 0
+-720 -657 0
+-712 -658 0
+-713 -659 0
+-714 -660 0
+-715 -661 0
+-716 -662 0
+-717 -663 0
+-718 -664 0
+-719 -665 0
+-720 -666 0
+-712 -667 0
+-713 -668 0
+-714 -669 0
+-715 -670 0
+-716 -671 0
+-717 -672 0
+-718 -673 0
+-719 -674 0
+-720 -675 0
+-712 -676 0
+-713 -677 0
+-714 -678 0
+-715 -679 0
+-716 -680 0
+-717 -681 0
+-718 -682 0
+-719 -683 0
+-720 -684 0
+-712 -685 0
+-713 -686 0
+-714 -687 0
+-715 -688 0
+-716 -689 0
+-717 -690 0
+-718 -691 0
+-719 -692 0
+-720 -693 0
+-712 -694 0
+-713 -695 0
+-714 -696 0
+-715 -697 0
+-716 -698 0
+-717 -699 0
+-718 -700 0
+-719 -701 0
+-720 -702 0
+-712 -703 0
+-713 -704 0
+-714 -705 0
+-715 -706 0
+-716 -707 0
+-717 -708 0
+-718 -709 0
+-719 -710 0
+-720 -711 0
+-712 -721 0
+-713 -722 0
+-714 -723 0
+-715 -724 0
+-716 -725 0
+-717 -726 0
+-718 -727 0
+-719 -728 0
+-720 -729 0
+-721 -649 0
+-722 -650 0
+-723 -651 0
+-724 -652 0
+-725 -653 0
+-726 -654 0
+-727 -655 0
+-728 -656 0
+-729 -657 0
+-721 -658 0
+-722 -659 0
+-723 -660 0
+-724 -661 0
+-725 -662 0
+-726 -663 0
+-727 -664 0
+-728 -665 0
+-729 -666 0
+-721 -667 0
+-722 -668 0
+-723 -669 0
+-724 -670 0
+-725 -671 0
+-726 -672 0
+-727 -673 0
+-728 -674 0
+-729 -675 0
+-721 -676 0
+-722 -677 0
+-723 -678 0
+-724 -679 0
+-725 -680 0
+-726 -681 0
+-727 -682 0
+-728 -683 0
+-729 -684 0
+-721 -685 0
+-722 -686 0
+-723 -687 0
+-724 -688 0
+-725 -689 0
+-726 -690 0
+-727 -691 0
+-728 -692 0
+-729 -693 0
+-721 -694 0
+-722 -695 0
+-723 -696 0
+-724 -697 0
+-725 -698 0
+-726 -699 0
+-727 -700 0
+-728 -701 0
+-729 -702 0
+-721 -703 0
+-722 -704 0
+-723 -705 0
+-724 -706 0
+-725 -707 0
+-726 -708 0
+-727 -709 0
+-728 -710 0
+-729 -711 0
+-721 -712 0
+-722 -713 0
+-723 -714 0
+-724 -715 0
+-725 -716 0
+-726 -717 0
+-727 -718 0
+-728 -719 0
+-729 -720 0
+-1 -82 0
+-2 -83 0
+-3 -84 0
+-4 -85 0
+-5 -86 0
+-6 -87 0
+-7 -88 0
+-8 -89 0
+-9 -90 0
+-10 -91 0
+-11 -92 0
+-12 -93 0
+-13 -94 0
+-14 -95 0
+-15 -96 0
+-16 -97 0
+-17 -98 0
+-18 -99 0
+-19 -100 0
+-20 -101 0
+-21 -102 0
+-22 -103 0
+-23 -104 0
+-24 -105 0
+-25 -106 0
+-26 -107 0
+-27 -108 0
+-28 -109 0
+-29 -110 0
+-30 -111 0
+-31 -112 0
+-32 -113 0
+-33 -114 0
+-34 -115 0
+-35 -116 0
+-36 -117 0
+-37 -118 0
+-38 -119 0
+-39 -120 0
+-40 -121 0
+-41 -122 0
+-42 -123 0
+-43 -124 0
+-44 -125 0
+-45 -126 0
+-46 -127 0
+-47 -128 0
+-48 -129 0
+-49 -130 0
+-50 -131 0
+-51 -132 0
+-52 -133 0
+-53 -134 0
+-54 -135 0
+-55 -136 0
+-56 -137 0
+-57 -138 0
+-58 -139 0
+-59 -140 0
+-60 -141 0
+-61 -142 0
+-62 -143 0
+-63 -144 0
+-64 -145 0
+-65 -146 0
+-66 -147 0
+-67 -148 0
+-68 -149 0
+-69 -150 0
+-70 -151 0
+-71 -152 0
+-72 -153 0
+-73 -154 0
+-74 -155 0
+-75 -156 0
+-76 -157 0
+-77 -158 0
+-78 -159 0
+-79 -160 0
+-80 -161 0
+-81 -162 0
+-1 -163 0
+-2 -164 0
+-3 -165 0
+-4 -166 0
+-5 -167 0
+-6 -168 0
+-7 -169 0
+-8 -170 0
+-9 -171 0
+-10 -172 0
+-11 -173 0
+-12 -174 0
+-13 -175 0
+-14 -176 0
+-15 -177 0
+-16 -178 0
+-17 -179 0
+-18 -180 0
+-19 -181 0
+-20 -182 0
+-21 -183 0
+-22 -184 0
+-23 -185 0
+-24 -186 0
+-25 -187 0
+-26 -188 0
+-27 -189 0
+-28 -190 0
+-29 -191 0
+-30 -192 0
+-31 -193 0
+-32 -194 0
+-33 -195 0
+-34 -196 0
+-35 -197 0
+-36 -198 0
+-37 -199 0
+-38 -200 0
+-39 -201 0
+-40 -202 0
+-41 -203 0
+-42 -204 0
+-43 -205 0
+-44 -206 0
+-45 -207 0
+-46 -208 0
+-47 -209 0
+-48 -210 0
+-49 -211 0
+-50 -212 0
+-51 -213 0
+-52 -214 0
+-53 -215 0
+-54 -216 0
+-55 -217 0
+-56 -218 0
+-57 -219 0
+-58 -220 0
+-59 -221 0
+-60 -222 0
+-61 -223 0
+-62 -224 0
+-63 -225 0
+-64 -226 0
+-65 -227 0
+-66 -228 0
+-67 -229 0
+-68 -230 0
+-69 -231 0
+-70 -232 0
+-71 -233 0
+-72 -234 0
+-73 -235 0
+-74 -236 0
+-75 -237 0
+-76 -238 0
+-77 -239 0
+-78 -240 0
+-79 -241 0
+-80 -242 0
+-81 -243 0
+-1 -244 0
+-2 -245 0
+-3 -246 0
+-4 -247 0
+-5 -248 0
+-6 -249 0
+-7 -250 0
+-8 -251 0
+-9 -252 0
+-10 -253 0
+-11 -254 0
+-12 -255 0
+-13 -256 0
+-14 -257 0
+-15 -258 0
+-16 -259 0
+-17 -260 0
+-18 -261 0
+-19 -262 0
+-20 -263 0
+-21 -264 0
+-22 -265 0
+-23 -266 0
+-24 -267 0
+-25 -268 0
+-26 -269 0
+-27 -270 0
+-28 -271 0
+-29 -272 0
+-30 -273 0
+-31 -274 0
+-32 -275 0
+-33 -276 0
+-34 -277 0
+-35 -278 0
+-36 -279 0
+-37 -280 0
+-38 -281 0
+-39 -282 0
+-40 -283 0
+-41 -284 0
+-42 -285 0
+-43 -286 0
+-44 -287 0
+-45 -288 0
+-46 -289 0
+-47 -290 0
+-48 -291 0
+-49 -292 0
+-50 -293 0
+-51 -294 0
+-52 -295 0
+-53 -296 0
+-54 -297 0
+-55 -298 0
+-56 -299 0
+-57 -300 0
+-58 -301 0
+-59 -302 0
+-60 -303 0
+-61 -304 0
+-62 -305 0
+-63 -306 0
+-64 -307 0
+-65 -308 0
+-66 -309 0
+-67 -310 0
+-68 -311 0
+-69 -312 0
+-70 -313 0
+-71 -314 0
+-72 -315 0
+-73 -316 0
+-74 -317 0
+-75 -318 0
+-76 -319 0
+-77 -320 0
+-78 -321 0
+-79 -322 0
+-80 -323 0
+-81 -324 0
+-1 -325 0
+-2 -326 0
+-3 -327 0
+-4 -328 0
+-5 -329 0
+-6 -330 0
+-7 -331 0
+-8 -332 0
+-9 -333 0
+-10 -334 0
+-11 -335 0
+-12 -336 0
+-13 -337 0
+-14 -338 0
+-15 -339 0
+-16 -340 0
+-17 -341 0
+-18 -342 0
+-19 -343 0
+-20 -344 0
+-21 -345 0
+-22 -346 0
+-23 -347 0
+-24 -348 0
+-25 -349 0
+-26 -350 0
+-27 -351 0
+-28 -352 0
+-29 -353 0
+-30 -354 0
+-31 -355 0
+-32 -356 0
+-33 -357 0
+-34 -358 0
+-35 -359 0
+-36 -360 0
+-37 -361 0
+-38 -362 0
+-39 -363 0
+-40 -364 0
+-41 -365 0
+-42 -366 0
+-43 -367 0
+-44 -368 0
+-45 -369 0
+-46 -370 0
+-47 -371 0
+-48 -372 0
+-49 -373 0
+-50 -374 0
+-51 -375 0
+-52 -376 0
+-53 -377 0
+-54 -378 0
+-55 -379 0
+-56 -380 0
+-57 -381 0
+-58 -382 0
+-59 -383 0
+-60 -384 0
+-61 -385 0
+-62 -386 0
+-63 -387 0
+-64 -388 0
+-65 -389 0
+-66 -390 0
+-67 -391 0
+-68 -392 0
+-69 -393 0
+-70 -394 0
+-71 -395 0
+-72 -396 0
+-73 -397 0
+-74 -398 0
+-75 -399 0
+-76 -400 0
+-77 -401 0
+-78 -402 0
+-79 -403 0
+-80 -404 0
+-81 -405 0
+-1 -406 0
+-2 -407 0
+-3 -408 0
+-4 -409 0
+-5 -410 0
+-6 -411 0
+-7 -412 0
+-8 -413 0
+-9 -414 0
+-10 -415 0
+-11 -416 0
+-12 -417 0
+-13 -418 0
+-14 -419 0
+-15 -420 0
+-16 -421 0
+-17 -422 0
+-18 -423 0
+-19 -424 0
+-20 -425 0
+-21 -426 0
+-22 -427 0
+-23 -428 0
+-24 -429 0
+-25 -430 0
+-26 -431 0
+-27 -432 0
+-28 -433 0
+-29 -434 0
+-30 -435 0
+-31 -436 0
+-32 -437 0
+-33 -438 0
+-34 -439 0
+-35 -440 0
+-36 -441 0
+-37 -442 0
+-38 -443 0
+-39 -444 0
+-40 -445 0
+-41 -446 0
+-42 -447 0
+-43 -448 0
+-44 -449 0
+-45 -450 0
+-46 -451 0
+-47 -452 0
+-48 -453 0
+-49 -454 0
+-50 -455 0
+-51 -456 0
+-52 -457 0
+-53 -458 0
+-54 -459 0
+-55 -460 0
+-56 -461 0
+-57 -462 0
+-58 -463 0
+-59 -464 0
+-60 -465 0
+-61 -466 0
+-62 -467 0
+-63 -468 0
+-64 -469 0
+-65 -470 0
+-66 -471 0
+-67 -472 0
+-68 -473 0
+-69 -474 0
+-70 -475 0
+-71 -476 0
+-72 -477 0
+-73 -478 0
+-74 -479 0
+-75 -480 0
+-76 -481 0
+-77 -482 0
+-78 -483 0
+-79 -484 0
+-80 -485 0
+-81 -486 0
+-1 -487 0
+-2 -488 0
+-3 -489 0
+-4 -490 0
+-5 -491 0
+-6 -492 0
+-7 -493 0
+-8 -494 0
+-9 -495 0
+-10 -496 0
+-11 -497 0
+-12 -498 0
+-13 -499 0
+-14 -500 0
+-15 -501 0
+-16 -502 0
+-17 -503 0
+-18 -504 0
+-19 -505 0
+-20 -506 0
+-21 -507 0
+-22 -508 0
+-23 -509 0
+-24 -510 0
+-25 -511 0
+-26 -512 0
+-27 -513 0
+-28 -514 0
+-29 -515 0
+-30 -516 0
+-31 -517 0
+-32 -518 0
+-33 -519 0
+-34 -520 0
+-35 -521 0
+-36 -522 0
+-37 -523 0
+-38 -524 0
+-39 -525 0
+-40 -526 0
+-41 -527 0
+-42 -528 0
+-43 -529 0
+-44 -530 0
+-45 -531 0
+-46 -532 0
+-47 -533 0
+-48 -534 0
+-49 -535 0
+-50 -536 0
+-51 -537 0
+-52 -538 0
+-53 -539 0
+-54 -540 0
+-55 -541 0
+-56 -542 0
+-57 -543 0
+-58 -544 0
+-59 -545 0
+-60 -546 0
+-61 -547 0
+-62 -548 0
+-63 -549 0
+-64 -550 0
+-65 -551 0
+-66 -552 0
+-67 -553 0
+-68 -554 0
+-69 -555 0
+-70 -556 0
+-71 -557 0
+-72 -558 0
+-73 -559 0
+-74 -560 0
+-75 -561 0
+-76 -562 0
+-77 -563 0
+-78 -564 0
+-79 -565 0
+-80 -566 0
+-81 -567 0
+-1 -568 0
+-2 -569 0
+-3 -570 0
+-4 -571 0
+-5 -572 0
+-6 -573 0
+-7 -574 0
+-8 -575 0
+-9 -576 0
+-10 -577 0
+-11 -578 0
+-12 -579 0
+-13 -580 0
+-14 -581 0
+-15 -582 0
+-16 -583 0
+-17 -584 0
+-18 -585 0
+-19 -586 0
+-20 -587 0
+-21 -588 0
+-22 -589 0
+-23 -590 0
+-24 -591 0
+-25 -592 0
+-26 -593 0
+-27 -594 0
+-28 -595 0
+-29 -596 0
+-30 -597 0
+-31 -598 0
+-32 -599 0
+-33 -600 0
+-34 -601 0
+-35 -602 0
+-36 -603 0
+-37 -604 0
+-38 -605 0
+-39 -606 0
+-40 -607 0
+-41 -608 0
+-42 -609 0
+-43 -610 0
+-44 -611 0
+-45 -612 0
+-46 -613 0
+-47 -614 0
+-48 -615 0
+-49 -616 0
+-50 -617 0
+-51 -618 0
+-52 -619 0
+-53 -620 0
+-54 -621 0
+-55 -622 0
+-56 -623 0
+-57 -624 0
+-58 -625 0
+-59 -626 0
+-60 -627 0
+-61 -628 0
+-62 -629 0
+-63 -630 0
+-64 -631 0
+-65 -632 0
+-66 -633 0
+-67 -634 0
+-68 -635 0
+-69 -636 0
+-70 -637 0
+-71 -638 0
+-72 -639 0
+-73 -640 0
+-74 -641 0
+-75 -642 0
+-76 -643 0
+-77 -644 0
+-78 -645 0
+-79 -646 0
+-80 -647 0
+-81 -648 0
+-1 -649 0
+-2 -650 0
+-3 -651 0
+-4 -652 0
+-5 -653 0
+-6 -654 0
+-7 -655 0
+-8 -656 0
+-9 -657 0
+-10 -658 0
+-11 -659 0
+-12 -660 0
+-13 -661 0
+-14 -662 0
+-15 -663 0
+-16 -664 0
+-17 -665 0
+-18 -666 0
+-19 -667 0
+-20 -668 0
+-21 -669 0
+-22 -670 0
+-23 -671 0
+-24 -672 0
+-25 -673 0
+-26 -674 0
+-27 -675 0
+-28 -676 0
+-29 -677 0
+-30 -678 0
+-31 -679 0
+-32 -680 0
+-33 -681 0
+-34 -682 0
+-35 -683 0
+-36 -684 0
+-37 -685 0
+-38 -686 0
+-39 -687 0
+-40 -688 0
+-41 -689 0
+-42 -690 0
+-43 -691 0
+-44 -692 0
+-45 -693 0
+-46 -694 0
+-47 -695 0
+-48 -696 0
+-49 -697 0
+-50 -698 0
+-51 -699 0
+-52 -700 0
+-53 -701 0
+-54 -702 0
+-55 -703 0
+-56 -704 0
+-57 -705 0
+-58 -706 0
+-59 -707 0
+-60 -708 0
+-61 -709 0
+-62 -710 0
+-63 -711 0
+-64 -712 0
+-65 -713 0
+-66 -714 0
+-67 -715 0
+-68 -716 0
+-69 -717 0
+-70 -718 0
+-71 -719 0
+-72 -720 0
+-73 -721 0
+-74 -722 0
+-75 -723 0
+-76 -724 0
+-77 -725 0
+-78 -726 0
+-79 -727 0
+-80 -728 0
+-81 -729 0
+-82 -1 0
+-83 -2 0
+-84 -3 0
+-85 -4 0
+-86 -5 0
+-87 -6 0
+-88 -7 0
+-89 -8 0
+-90 -9 0
+-91 -10 0
+-92 -11 0
+-93 -12 0
+-94 -13 0
+-95 -14 0
+-96 -15 0
+-97 -16 0
+-98 -17 0
+-99 -18 0
+-100 -19 0
+-101 -20 0
+-102 -21 0
+-103 -22 0
+-104 -23 0
+-105 -24 0
+-106 -25 0
+-107 -26 0
+-108 -27 0
+-109 -28 0
+-110 -29 0
+-111 -30 0
+-112 -31 0
+-113 -32 0
+-114 -33 0
+-115 -34 0
+-116 -35 0
+-117 -36 0
+-118 -37 0
+-119 -38 0
+-120 -39 0
+-121 -40 0
+-122 -41 0
+-123 -42 0
+-124 -43 0
+-125 -44 0
+-126 -45 0
+-127 -46 0
+-128 -47 0
+-129 -48 0
+-130 -49 0
+-131 -50 0
+-132 -51 0
+-133 -52 0
+-134 -53 0
+-135 -54 0
+-136 -55 0
+-137 -56 0
+-138 -57 0
+-139 -58 0
+-140 -59 0
+-141 -60 0
+-142 -61 0
+-143 -62 0
+-144 -63 0
+-145 -64 0
+-146 -65 0
+-147 -66 0
+-148 -67 0
+-149 -68 0
+-150 -69 0
+-151 -70 0
+-152 -71 0
+-153 -72 0
+-154 -73 0
+-155 -74 0
+-156 -75 0
+-157 -76 0
+-158 -77 0
+-159 -78 0
+-160 -79 0
+-161 -80 0
+-162 -81 0
+-82 -163 0
+-83 -164 0
+-84 -165 0
+-85 -166 0
+-86 -167 0
+-87 -168 0
+-88 -169 0
+-89 -170 0
+-90 -171 0
+-91 -172 0
+-92 -173 0
+-93 -174 0
+-94 -175 0
+-95 -176 0
+-96 -177 0
+-97 -178 0
+-98 -179 0
+-99 -180 0
+-100 -181 0
+-101 -182 0
+-102 -183 0
+-103 -184 0
+-104 -185 0
+-105 -186 0
+-106 -187 0
+-107 -188 0
+-108 -189 0
+-109 -190 0
+-110 -191 0
+-111 -192 0
+-112 -193 0
+-113 -194 0
+-114 -195 0
+-115 -196 0
+-116 -197 0
+-117 -198 0
+-118 -199 0
+-119 -200 0
+-120 -201 0
+-121 -202 0
+-122 -203 0
+-123 -204 0
+-124 -205 0
+-125 -206 0
+-126 -207 0
+-127 -208 0
+-128 -209 0
+-129 -210 0
+-130 -211 0
+-131 -212 0
+-132 -213 0
+-133 -214 0
+-134 -215 0
+-135 -216 0
+-136 -217 0
+-137 -218 0
+-138 -219 0
+-139 -220 0
+-140 -221 0
+-141 -222 0
+-142 -223 0
+-143 -224 0
+-144 -225 0
+-145 -226 0
+-146 -227 0
+-147 -228 0
+-148 -229 0
+-149 -230 0
+-150 -231 0
+-151 -232 0
+-152 -233 0
+-153 -234 0
+-154 -235 0
+-155 -236 0
+-156 -237 0
+-157 -238 0
+-158 -239 0
+-159 -240 0
+-160 -241 0
+-161 -242 0
+-162 -243 0
+-82 -244 0
+-83 -245 0
+-84 -246 0
+-85 -247 0
+-86 -248 0
+-87 -249 0
+-88 -250 0
+-89 -251 0
+-90 -252 0
+-91 -253 0
+-92 -254 0
+-93 -255 0
+-94 -256 0
+-95 -257 0
+-96 -258 0
+-97 -259 0
+-98 -260 0
+-99 -261 0
+-100 -262 0
+-101 -263 0
+-102 -264 0
+-103 -265 0
+-104 -266 0
+-105 -267 0
+-106 -268 0
+-107 -269 0
+-108 -270 0
+-109 -271 0
+-110 -272 0
+-111 -273 0
+-112 -274 0
+-113 -275 0
+-114 -276 0
+-115 -277 0
+-116 -278 0
+-117 -279 0
+-118 -280 0
+-119 -281 0
+-120 -282 0
+-121 -283 0
+-122 -284 0
+-123 -285 0
+-124 -286 0
+-125 -287 0
+-126 -288 0
+-127 -289 0
+-128 -290 0
+-129 -291 0
+-130 -292 0
+-131 -293 0
+-132 -294 0
+-133 -295 0
+-134 -296 0
+-135 -297 0
+-136 -298 0
+-137 -299 0
+-138 -300 0
+-139 -301 0
+-140 -302 0
+-141 -303 0
+-142 -304 0
+-143 -305 0
+-144 -306 0
+-145 -307 0
+-146 -308 0
+-147 -309 0
+-148 -310 0
+-149 -311 0
+-150 -312 0
+-151 -313 0
+-152 -314 0
+-153 -315 0
+-154 -316 0
+-155 -317 0
+-156 -318 0
+-157 -319 0
+-158 -320 0
+-159 -321 0
+-160 -322 0
+-161 -323 0
+-162 -324 0
+-82 -325 0
+-83 -326 0
+-84 -327 0
+-85 -328 0
+-86 -329 0
+-87 -330 0
+-88 -331 0
+-89 -332 0
+-90 -333 0
+-91 -334 0
+-92 -335 0
+-93 -336 0
+-94 -337 0
+-95 -338 0
+-96 -339 0
+-97 -340 0
+-98 -341 0
+-99 -342 0
+-100 -343 0
+-101 -344 0
+-102 -345 0
+-103 -346 0
+-104 -347 0
+-105 -348 0
+-106 -349 0
+-107 -350 0
+-108 -351 0
+-109 -352 0
+-110 -353 0
+-111 -354 0
+-112 -355 0
+-113 -356 0
+-114 -357 0
+-115 -358 0
+-116 -359 0
+-117 -360 0
+-118 -361 0
+-119 -362 0
+-120 -363 0
+-121 -364 0
+-122 -365 0
+-123 -366 0
+-124 -367 0
+-125 -368 0
+-126 -369 0
+-127 -370 0
+-128 -371 0
+-129 -372 0
+-130 -373 0
+-131 -374 0
+-132 -375 0
+-133 -376 0
+-134 -377 0
+-135 -378 0
+-136 -379 0
+-137 -380 0
+-138 -381 0
+-139 -382 0
+-140 -383 0
+-141 -384 0
+-142 -385 0
+-143 -386 0
+-144 -387 0
+-145 -388 0
+-146 -389 0
+-147 -390 0
+-148 -391 0
+-149 -392 0
+-150 -393 0
+-151 -394 0
+-152 -395 0
+-153 -396 0
+-154 -397 0
+-155 -398 0
+-156 -399 0
+-157 -400 0
+-158 -401 0
+-159 -402 0
+-160 -403 0
+-161 -404 0
+-162 -405 0
+-82 -406 0
+-83 -407 0
+-84 -408 0
+-85 -409 0
+-86 -410 0
+-87 -411 0
+-88 -412 0
+-89 -413 0
+-90 -414 0
+-91 -415 0
+-92 -416 0
+-93 -417 0
+-94 -418 0
+-95 -419 0
+-96 -420 0
+-97 -421 0
+-98 -422 0
+-99 -423 0
+-100 -424 0
+-101 -425 0
+-102 -426 0
+-103 -427 0
+-104 -428 0
+-105 -429 0
+-106 -430 0
+-107 -431 0
+-108 -432 0
+-109 -433 0
+-110 -434 0
+-111 -435 0
+-112 -436 0
+-113 -437 0
+-114 -438 0
+-115 -439 0
+-116 -440 0
+-117 -441 0
+-118 -442 0
+-119 -443 0
+-120 -444 0
+-121 -445 0
+-122 -446 0
+-123 -447 0
+-124 -448 0
+-125 -449 0
+-126 -450 0
+-127 -451 0
+-128 -452 0
+-129 -453 0
+-130 -454 0
+-131 -455 0
+-132 -456 0
+-133 -457 0
+-134 -458 0
+-135 -459 0
+-136 -460 0
+-137 -461 0
+-138 -462 0
+-139 -463 0
+-140 -464 0
+-141 -465 0
+-142 -466 0
+-143 -467 0
+-144 -468 0
+-145 -469 0
+-146 -470 0
+-147 -471 0
+-148 -472 0
+-149 -473 0
+-150 -474 0
+-151 -475 0
+-152 -476 0
+-153 -477 0
+-154 -478 0
+-155 -479 0
+-156 -480 0
+-157 -481 0
+-158 -482 0
+-159 -483 0
+-160 -484 0
+-161 -485 0
+-162 -486 0
+-82 -487 0
+-83 -488 0
+-84 -489 0
+-85 -490 0
+-86 -491 0
+-87 -492 0
+-88 -493 0
+-89 -494 0
+-90 -495 0
+-91 -496 0
+-92 -497 0
+-93 -498 0
+-94 -499 0
+-95 -500 0
+-96 -501 0
+-97 -502 0
+-98 -503 0
+-99 -504 0
+-100 -505 0
+-101 -506 0
+-102 -507 0
+-103 -508 0
+-104 -509 0
+-105 -510 0
+-106 -511 0
+-107 -512 0
+-108 -513 0
+-109 -514 0
+-110 -515 0
+-111 -516 0
+-112 -517 0
+-113 -518 0
+-114 -519 0
+-115 -520 0
+-116 -521 0
+-117 -522 0
+-118 -523 0
+-119 -524 0
+-120 -525 0
+-121 -526 0
+-122 -527 0
+-123 -528 0
+-124 -529 0
+-125 -530 0
+-126 -531 0
+-127 -532 0
+-128 -533 0
+-129 -534 0
+-130 -535 0
+-131 -536 0
+-132 -537 0
+-133 -538 0
+-134 -539 0
+-135 -540 0
+-136 -541 0
+-137 -542 0
+-138 -543 0
+-139 -544 0
+-140 -545 0
+-141 -546 0
+-142 -547 0
+-143 -548 0
+-144 -549 0
+-145 -550 0
+-146 -551 0
+-147 -552 0
+-148 -553 0
+-149 -554 0
+-150 -555 0
+-151 -556 0
+-152 -557 0
+-153 -558 0
+-154 -559 0
+-155 -560 0
+-156 -561 0
+-157 -562 0
+-158 -563 0
+-159 -564 0
+-160 -565 0
+-161 -566 0
+-162 -567 0
+-82 -568 0
+-83 -569 0
+-84 -570 0
+-85 -571 0
+-86 -572 0
+-87 -573 0
+-88 -574 0
+-89 -575 0
+-90 -576 0
+-91 -577 0
+-92 -578 0
+-93 -579 0
+-94 -580 0
+-95 -581 0
+-96 -582 0
+-97 -583 0
+-98 -584 0
+-99 -585 0
+-100 -586 0
+-101 -587 0
+-102 -588 0
+-103 -589 0
+-104 -590 0
+-105 -591 0
+-106 -592 0
+-107 -593 0
+-108 -594 0
+-109 -595 0
+-110 -596 0
+-111 -597 0
+-112 -598 0
+-113 -599 0
+-114 -600 0
+-115 -601 0
+-116 -602 0
+-117 -603 0
+-118 -604 0
+-119 -605 0
+-120 -606 0
+-121 -607 0
+-122 -608 0
+-123 -609 0
+-124 -610 0
+-125 -611 0
+-126 -612 0
+-127 -613 0
+-128 -614 0
+-129 -615 0
+-130 -616 0
+-131 -617 0
+-132 -618 0
+-133 -619 0
+-134 -620 0
+-135 -621 0
+-136 -622 0
+-137 -623 0
+-138 -624 0
+-139 -625 0
+-140 -626 0
+-141 -627 0
+-142 -628 0
+-143 -629 0
+-144 -630 0
+-145 -631 0
+-146 -632 0
+-147 -633 0
+-148 -634 0
+-149 -635 0
+-150 -636 0
+-151 -637 0
+-152 -638 0
+-153 -639 0
+-154 -640 0
+-155 -641 0
+-156 -642 0
+-157 -643 0
+-158 -644 0
+-159 -645 0
+-160 -646 0
+-161 -647 0
+-162 -648 0
+-82 -649 0
+-83 -650 0
+-84 -651 0
+-85 -652 0
+-86 -653 0
+-87 -654 0
+-88 -655 0
+-89 -656 0
+-90 -657 0
+-91 -658 0
+-92 -659 0
+-93 -660 0
+-94 -661 0
+-95 -662 0
+-96 -663 0
+-97 -664 0
+-98 -665 0
+-99 -666 0
+-100 -667 0
+-101 -668 0
+-102 -669 0
+-103 -670 0
+-104 -671 0
+-105 -672 0
+-106 -673 0
+-107 -674 0
+-108 -675 0
+-109 -676 0
+-110 -677 0
+-111 -678 0
+-112 -679 0
+-113 -680 0
+-114 -681 0
+-115 -682 0
+-116 -683 0
+-117 -684 0
+-118 -685 0
+-119 -686 0
+-120 -687 0
+-121 -688 0
+-122 -689 0
+-123 -690 0
+-124 -691 0
+-125 -692 0
+-126 -693 0
+-127 -694 0
+-128 -695 0
+-129 -696 0
+-130 -697 0
+-131 -698 0
+-132 -699 0
+-133 -700 0
+-134 -701 0
+-135 -702 0
+-136 -703 0
+-137 -704 0
+-138 -705 0
+-139 -706 0
+-140 -707 0
+-141 -708 0
+-142 -709 0
+-143 -710 0
+-144 -711 0
+-145 -712 0
+-146 -713 0
+-147 -714 0
+-148 -715 0
+-149 -716 0
+-150 -717 0
+-151 -718 0
+-152 -719 0
+-153 -720 0
+-154 -721 0
+-155 -722 0
+-156 -723 0
+-157 -724 0
+-158 -725 0
+-159 -726 0
+-160 -727 0
+-161 -728 0
+-162 -729 0
+-163 -1 0
+-164 -2 0
+-165 -3 0
+-166 -4 0
+-167 -5 0
+-168 -6 0
+-169 -7 0
+-170 -8 0
+-171 -9 0
+-172 -10 0
+-173 -11 0
+-174 -12 0
+-175 -13 0
+-176 -14 0
+-177 -15 0
+-178 -16 0
+-179 -17 0
+-180 -18 0
+-181 -19 0
+-182 -20 0
+-183 -21 0
+-184 -22 0
+-185 -23 0
+-186 -24 0
+-187 -25 0
+-188 -26 0
+-189 -27 0
+-190 -28 0
+-191 -29 0
+-192 -30 0
+-193 -31 0
+-194 -32 0
+-195 -33 0
+-196 -34 0
+-197 -35 0
+-198 -36 0
+-199 -37 0
+-200 -38 0
+-201 -39 0
+-202 -40 0
+-203 -41 0
+-204 -42 0
+-205 -43 0
+-206 -44 0
+-207 -45 0
+-208 -46 0
+-209 -47 0
+-210 -48 0
+-211 -49 0
+-212 -50 0
+-213 -51 0
+-214 -52 0
+-215 -53 0
+-216 -54 0
+-217 -55 0
+-218 -56 0
+-219 -57 0
+-220 -58 0
+-221 -59 0
+-222 -60 0
+-223 -61 0
+-224 -62 0
+-225 -63 0
+-226 -64 0
+-227 -65 0
+-228 -66 0
+-229 -67 0
+-230 -68 0
+-231 -69 0
+-232 -70 0
+-233 -71 0
+-234 -72 0
+-235 -73 0
+-236 -74 0
+-237 -75 0
+-238 -76 0
+-239 -77 0
+-240 -78 0
+-241 -79 0
+-242 -80 0
+-243 -81 0
+-163 -82 0
+-164 -83 0
+-165 -84 0
+-166 -85 0
+-167 -86 0
+-168 -87 0
+-169 -88 0
+-170 -89 0
+-171 -90 0
+-172 -91 0
+-173 -92 0
+-174 -93 0
+-175 -94 0
+-176 -95 0
+-177 -96 0
+-178 -97 0
+-179 -98 0
+-180 -99 0
+-181 -100 0
+-182 -101 0
+-183 -102 0
+-184 -103 0
+-185 -104 0
+-186 -105 0
+-187 -106 0
+-188 -107 0
+-189 -108 0
+-190 -109 0
+-191 -110 0
+-192 -111 0
+-193 -112 0
+-194 -113 0
+-195 -114 0
+-196 -115 0
+-197 -116 0
+-198 -117 0
+-199 -118 0
+-200 -119 0
+-201 -120 0
+-202 -121 0
+-203 -122 0
+-204 -123 0
+-205 -124 0
+-206 -125 0
+-207 -126 0
+-208 -127 0
+-209 -128 0
+-210 -129 0
+-211 -130 0
+-212 -131 0
+-213 -132 0
+-214 -133 0
+-215 -134 0
+-216 -135 0
+-217 -136 0
+-218 -137 0
+-219 -138 0
+-220 -139 0
+-221 -140 0
+-222 -141 0
+-223 -142 0
+-224 -143 0
+-225 -144 0
+-226 -145 0
+-227 -146 0
+-228 -147 0
+-229 -148 0
+-230 -149 0
+-231 -150 0
+-232 -151 0
+-233 -152 0
+-234 -153 0
+-235 -154 0
+-236 -155 0
+-237 -156 0
+-238 -157 0
+-239 -158 0
+-240 -159 0
+-241 -160 0
+-242 -161 0
+-243 -162 0
+-163 -244 0
+-164 -245 0
+-165 -246 0
+-166 -247 0
+-167 -248 0
+-168 -249 0
+-169 -250 0
+-170 -251 0
+-171 -252 0
+-172 -253 0
+-173 -254 0
+-174 -255 0
+-175 -256 0
+-176 -257 0
+-177 -258 0
+-178 -259 0
+-179 -260 0
+-180 -261 0
+-181 -262 0
+-182 -263 0
+-183 -264 0
+-184 -265 0
+-185 -266 0
+-186 -267 0
+-187 -268 0
+-188 -269 0
+-189 -270 0
+-190 -271 0
+-191 -272 0
+-192 -273 0
+-193 -274 0
+-194 -275 0
+-195 -276 0
+-196 -277 0
+-197 -278 0
+-198 -279 0
+-199 -280 0
+-200 -281 0
+-201 -282 0
+-202 -283 0
+-203 -284 0
+-204 -285 0
+-205 -286 0
+-206 -287 0
+-207 -288 0
+-208 -289 0
+-209 -290 0
+-210 -291 0
+-211 -292 0
+-212 -293 0
+-213 -294 0
+-214 -295 0
+-215 -296 0
+-216 -297 0
+-217 -298 0
+-218 -299 0
+-219 -300 0
+-220 -301 0
+-221 -302 0
+-222 -303 0
+-223 -304 0
+-224 -305 0
+-225 -306 0
+-226 -307 0
+-227 -308 0
+-228 -309 0
+-229 -310 0
+-230 -311 0
+-231 -312 0
+-232 -313 0
+-233 -314 0
+-234 -315 0
+-235 -316 0
+-236 -317 0
+-237 -318 0
+-238 -319 0
+-239 -320 0
+-240 -321 0
+-241 -322 0
+-242 -323 0
+-243 -324 0
+-163 -325 0
+-164 -326 0
+-165 -327 0
+-166 -328 0
+-167 -329 0
+-168 -330 0
+-169 -331 0
+-170 -332 0
+-171 -333 0
+-172 -334 0
+-173 -335 0
+-174 -336 0
+-175 -337 0
+-176 -338 0
+-177 -339 0
+-178 -340 0
+-179 -341 0
+-180 -342 0
+-181 -343 0
+-182 -344 0
+-183 -345 0
+-184 -346 0
+-185 -347 0
+-186 -348 0
+-187 -349 0
+-188 -350 0
+-189 -351 0
+-190 -352 0
+-191 -353 0
+-192 -354 0
+-193 -355 0
+-194 -356 0
+-195 -357 0
+-196 -358 0
+-197 -359 0
+-198 -360 0
+-199 -361 0
+-200 -362 0
+-201 -363 0
+-202 -364 0
+-203 -365 0
+-204 -366 0
+-205 -367 0
+-206 -368 0
+-207 -369 0
+-208 -370 0
+-209 -371 0
+-210 -372 0
+-211 -373 0
+-212 -374 0
+-213 -375 0
+-214 -376 0
+-215 -377 0
+-216 -378 0
+-217 -379 0
+-218 -380 0
+-219 -381 0
+-220 -382 0
+-221 -383 0
+-222 -384 0
+-223 -385 0
+-224 -386 0
+-225 -387 0
+-226 -388 0
+-227 -389 0
+-228 -390 0
+-229 -391 0
+-230 -392 0
+-231 -393 0
+-232 -394 0
+-233 -395 0
+-234 -396 0
+-235 -397 0
+-236 -398 0
+-237 -399 0
+-238 -400 0
+-239 -401 0
+-240 -402 0
+-241 -403 0
+-242 -404 0
+-243 -405 0
+-163 -406 0
+-164 -407 0
+-165 -408 0
+-166 -409 0
+-167 -410 0
+-168 -411 0
+-169 -412 0
+-170 -413 0
+-171 -414 0
+-172 -415 0
+-173 -416 0
+-174 -417 0
+-175 -418 0
+-176 -419 0
+-177 -420 0
+-178 -421 0
+-179 -422 0
+-180 -423 0
+-181 -424 0
+-182 -425 0
+-183 -426 0
+-184 -427 0
+-185 -428 0
+-186 -429 0
+-187 -430 0
+-188 -431 0
+-189 -432 0
+-190 -433 0
+-191 -434 0
+-192 -435 0
+-193 -436 0
+-194 -437 0
+-195 -438 0
+-196 -439 0
+-197 -440 0
+-198 -441 0
+-199 -442 0
+-200 -443 0
+-201 -444 0
+-202 -445 0
+-203 -446 0
+-204 -447 0
+-205 -448 0
+-206 -449 0
+-207 -450 0
+-208 -451 0
+-209 -452 0
+-210 -453 0
+-211 -454 0
+-212 -455 0
+-213 -456 0
+-214 -457 0
+-215 -458 0
+-216 -459 0
+-217 -460 0
+-218 -461 0
+-219 -462 0
+-220 -463 0
+-221 -464 0
+-222 -465 0
+-223 -466 0
+-224 -467 0
+-225 -468 0
+-226 -469 0
+-227 -470 0
+-228 -471 0
+-229 -472 0
+-230 -473 0
+-231 -474 0
+-232 -475 0
+-233 -476 0
+-234 -477 0
+-235 -478 0
+-236 -479 0
+-237 -480 0
+-238 -481 0
+-239 -482 0
+-240 -483 0
+-241 -484 0
+-242 -485 0
+-243 -486 0
+-163 -487 0
+-164 -488 0
+-165 -489 0
+-166 -490 0
+-167 -491 0
+-168 -492 0
+-169 -493 0
+-170 -494 0
+-171 -495 0
+-172 -496 0
+-173 -497 0
+-174 -498 0
+-175 -499 0
+-176 -500 0
+-177 -501 0
+-178 -502 0
+-179 -503 0
+-180 -504 0
+-181 -505 0
+-182 -506 0
+-183 -507 0
+-184 -508 0
+-185 -509 0
+-186 -510 0
+-187 -511 0
+-188 -512 0
+-189 -513 0
+-190 -514 0
+-191 -515 0
+-192 -516 0
+-193 -517 0
+-194 -518 0
+-195 -519 0
+-196 -520 0
+-197 -521 0
+-198 -522 0
+-199 -523 0
+-200 -524 0
+-201 -525 0
+-202 -526 0
+-203 -527 0
+-204 -528 0
+-205 -529 0
+-206 -530 0
+-207 -531 0
+-208 -532 0
+-209 -533 0
+-210 -534 0
+-211 -535 0
+-212 -536 0
+-213 -537 0
+-214 -538 0
+-215 -539 0
+-216 -540 0
+-217 -541 0
+-218 -542 0
+-219 -543 0
+-220 -544 0
+-221 -545 0
+-222 -546 0
+-223 -547 0
+-224 -548 0
+-225 -549 0
+-226 -550 0
+-227 -551 0
+-228 -552 0
+-229 -553 0
+-230 -554 0
+-231 -555 0
+-232 -556 0
+-233 -557 0
+-234 -558 0
+-235 -559 0
+-236 -560 0
+-237 -561 0
+-238 -562 0
+-239 -563 0
+-240 -564 0
+-241 -565 0
+-242 -566 0
+-243 -567 0
+-163 -568 0
+-164 -569 0
+-165 -570 0
+-166 -571 0
+-167 -572 0
+-168 -573 0
+-169 -574 0
+-170 -575 0
+-171 -576 0
+-172 -577 0
+-173 -578 0
+-174 -579 0
+-175 -580 0
+-176 -581 0
+-177 -582 0
+-178 -583 0
+-179 -584 0
+-180 -585 0
+-181 -586 0
+-182 -587 0
+-183 -588 0
+-184 -589 0
+-185 -590 0
+-186 -591 0
+-187 -592 0
+-188 -593 0
+-189 -594 0
+-190 -595 0
+-191 -596 0
+-192 -597 0
+-193 -598 0
+-194 -599 0
+-195 -600 0
+-196 -601 0
+-197 -602 0
+-198 -603 0
+-199 -604 0
+-200 -605 0
+-201 -606 0
+-202 -607 0
+-203 -608 0
+-204 -609 0
+-205 -610 0
+-206 -611 0
+-207 -612 0
+-208 -613 0
+-209 -614 0
+-210 -615 0
+-211 -616 0
+-212 -617 0
+-213 -618 0
+-214 -619 0
+-215 -620 0
+-216 -621 0
+-217 -622 0
+-218 -623 0
+-219 -624 0
+-220 -625 0
+-221 -626 0
+-222 -627 0
+-223 -628 0
+-224 -629 0
+-225 -630 0
+-226 -631 0
+-227 -632 0
+-228 -633 0
+-229 -634 0
+-230 -635 0
+-231 -636 0
+-232 -637 0
+-233 -638 0
+-234 -639 0
+-235 -640 0
+-236 -641 0
+-237 -642 0
+-238 -643 0
+-239 -644 0
+-240 -645 0
+-241 -646 0
+-242 -647 0
+-243 -648 0
+-163 -649 0
+-164 -650 0
+-165 -651 0
+-166 -652 0
+-167 -653 0
+-168 -654 0
+-169 -655 0
+-170 -656 0
+-171 -657 0
+-172 -658 0
+-173 -659 0
+-174 -660 0
+-175 -661 0
+-176 -662 0
+-177 -663 0
+-178 -664 0
+-179 -665 0
+-180 -666 0
+-181 -667 0
+-182 -668 0
+-183 -669 0
+-184 -670 0
+-185 -671 0
+-186 -672 0
+-187 -673 0
+-188 -674 0
+-189 -675 0
+-190 -676 0
+-191 -677 0
+-192 -678 0
+-193 -679 0
+-194 -680 0
+-195 -681 0
+-196 -682 0
+-197 -683 0
+-198 -684 0
+-199 -685 0
+-200 -686 0
+-201 -687 0
+-202 -688 0
+-203 -689 0
+-204 -690 0
+-205 -691 0
+-206 -692 0
+-207 -693 0
+-208 -694 0
+-209 -695 0
+-210 -696 0
+-211 -697 0
+-212 -698 0
+-213 -699 0
+-214 -700 0
+-215 -701 0
+-216 -702 0
+-217 -703 0
+-218 -704 0
+-219 -705 0
+-220 -706 0
+-221 -707 0
+-222 -708 0
+-223 -709 0
+-224 -710 0
+-225 -711 0
+-226 -712 0
+-227 -713 0
+-228 -714 0
+-229 -715 0
+-230 -716 0
+-231 -717 0
+-232 -718 0
+-233 -719 0
+-234 -720 0
+-235 -721 0
+-236 -722 0
+-237 -723 0
+-238 -724 0
+-239 -725 0
+-240 -726 0
+-241 -727 0
+-242 -728 0
+-243 -729 0
+-244 -1 0
+-245 -2 0
+-246 -3 0
+-247 -4 0
+-248 -5 0
+-249 -6 0
+-250 -7 0
+-251 -8 0
+-252 -9 0
+-253 -10 0
+-254 -11 0
+-255 -12 0
+-256 -13 0
+-257 -14 0
+-258 -15 0
+-259 -16 0
+-260 -17 0
+-261 -18 0
+-262 -19 0
+-263 -20 0
+-264 -21 0
+-265 -22 0
+-266 -23 0
+-267 -24 0
+-268 -25 0
+-269 -26 0
+-270 -27 0
+-271 -28 0
+-272 -29 0
+-273 -30 0
+-274 -31 0
+-275 -32 0
+-276 -33 0
+-277 -34 0
+-278 -35 0
+-279 -36 0
+-280 -37 0
+-281 -38 0
+-282 -39 0
+-283 -40 0
+-284 -41 0
+-285 -42 0
+-286 -43 0
+-287 -44 0
+-288 -45 0
+-289 -46 0
+-290 -47 0
+-291 -48 0
+-292 -49 0
+-293 -50 0
+-294 -51 0
+-295 -52 0
+-296 -53 0
+-297 -54 0
+-298 -55 0
+-299 -56 0
+-300 -57 0
+-301 -58 0
+-302 -59 0
+-303 -60 0
+-304 -61 0
+-305 -62 0
+-306 -63 0
+-307 -64 0
+-308 -65 0
+-309 -66 0
+-310 -67 0
+-311 -68 0
+-312 -69 0
+-313 -70 0
+-314 -71 0
+-315 -72 0
+-316 -73 0
+-317 -74 0
+-318 -75 0
+-319 -76 0
+-320 -77 0
+-321 -78 0
+-322 -79 0
+-323 -80 0
+-324 -81 0
+-244 -82 0
+-245 -83 0
+-246 -84 0
+-247 -85 0
+-248 -86 0
+-249 -87 0
+-250 -88 0
+-251 -89 0
+-252 -90 0
+-253 -91 0
+-254 -92 0
+-255 -93 0
+-256 -94 0
+-257 -95 0
+-258 -96 0
+-259 -97 0
+-260 -98 0
+-261 -99 0
+-262 -100 0
+-263 -101 0
+-264 -102 0
+-265 -103 0
+-266 -104 0
+-267 -105 0
+-268 -106 0
+-269 -107 0
+-270 -108 0
+-271 -109 0
+-272 -110 0
+-273 -111 0
+-274 -112 0
+-275 -113 0
+-276 -114 0
+-277 -115 0
+-278 -116 0
+-279 -117 0
+-280 -118 0
+-281 -119 0
+-282 -120 0
+-283 -121 0
+-284 -122 0
+-285 -123 0
+-286 -124 0
+-287 -125 0
+-288 -126 0
+-289 -127 0
+-290 -128 0
+-291 -129 0
+-292 -130 0
+-293 -131 0
+-294 -132 0
+-295 -133 0
+-296 -134 0
+-297 -135 0
+-298 -136 0
+-299 -137 0
+-300 -138 0
+-301 -139 0
+-302 -140 0
+-303 -141 0
+-304 -142 0
+-305 -143 0
+-306 -144 0
+-307 -145 0
+-308 -146 0
+-309 -147 0
+-310 -148 0
+-311 -149 0
+-312 -150 0
+-313 -151 0
+-314 -152 0
+-315 -153 0
+-316 -154 0
+-317 -155 0
+-318 -156 0
+-319 -157 0
+-320 -158 0
+-321 -159 0
+-322 -160 0
+-323 -161 0
+-324 -162 0
+-244 -163 0
+-245 -164 0
+-246 -165 0
+-247 -166 0
+-248 -167 0
+-249 -168 0
+-250 -169 0
+-251 -170 0
+-252 -171 0
+-253 -172 0
+-254 -173 0
+-255 -174 0
+-256 -175 0
+-257 -176 0
+-258 -177 0
+-259 -178 0
+-260 -179 0
+-261 -180 0
+-262 -181 0
+-263 -182 0
+-264 -183 0
+-265 -184 0
+-266 -185 0
+-267 -186 0
+-268 -187 0
+-269 -188 0
+-270 -189 0
+-271 -190 0
+-272 -191 0
+-273 -192 0
+-274 -193 0
+-275 -194 0
+-276 -195 0
+-277 -196 0
+-278 -197 0
+-279 -198 0
+-280 -199 0
+-281 -200 0
+-282 -201 0
+-283 -202 0
+-284 -203 0
+-285 -204 0
+-286 -205 0
+-287 -206 0
+-288 -207 0
+-289 -208 0
+-290 -209 0
+-291 -210 0
+-292 -211 0
+-293 -212 0
+-294 -213 0
+-295 -214 0
+-296 -215 0
+-297 -216 0
+-298 -217 0
+-299 -218 0
+-300 -219 0
+-301 -220 0
+-302 -221 0
+-303 -222 0
+-304 -223 0
+-305 -224 0
+-306 -225 0
+-307 -226 0
+-308 -227 0
+-309 -228 0
+-310 -229 0
+-311 -230 0
+-312 -231 0
+-313 -232 0
+-314 -233 0
+-315 -234 0
+-316 -235 0
+-317 -236 0
+-318 -237 0
+-319 -238 0
+-320 -239 0
+-321 -240 0
+-322 -241 0
+-323 -242 0
+-324 -243 0
+-244 -325 0
+-245 -326 0
+-246 -327 0
+-247 -328 0
+-248 -329 0
+-249 -330 0
+-250 -331 0
+-251 -332 0
+-252 -333 0
+-253 -334 0
+-254 -335 0
+-255 -336 0
+-256 -337 0
+-257 -338 0
+-258 -339 0
+-259 -340 0
+-260 -341 0
+-261 -342 0
+-262 -343 0
+-263 -344 0
+-264 -345 0
+-265 -346 0
+-266 -347 0
+-267 -348 0
+-268 -349 0
+-269 -350 0
+-270 -351 0
+-271 -352 0
+-272 -353 0
+-273 -354 0
+-274 -355 0
+-275 -356 0
+-276 -357 0
+-277 -358 0
+-278 -359 0
+-279 -360 0
+-280 -361 0
+-281 -362 0
+-282 -363 0
+-283 -364 0
+-284 -365 0
+-285 -366 0
+-286 -367 0
+-287 -368 0
+-288 -369 0
+-289 -370 0
+-290 -371 0
+-291 -372 0
+-292 -373 0
+-293 -374 0
+-294 -375 0
+-295 -376 0
+-296 -377 0
+-297 -378 0
+-298 -379 0
+-299 -380 0
+-300 -381 0
+-301 -382 0
+-302 -383 0
+-303 -384 0
+-304 -385 0
+-305 -386 0
+-306 -387 0
+-307 -388 0
+-308 -389 0
+-309 -390 0
+-310 -391 0
+-311 -392 0
+-312 -393 0
+-313 -394 0
+-314 -395 0
+-315 -396 0
+-316 -397 0
+-317 -398 0
+-318 -399 0
+-319 -400 0
+-320 -401 0
+-321 -402 0
+-322 -403 0
+-323 -404 0
+-324 -405 0
+-244 -406 0
+-245 -407 0
+-246 -408 0
+-247 -409 0
+-248 -410 0
+-249 -411 0
+-250 -412 0
+-251 -413 0
+-252 -414 0
+-253 -415 0
+-254 -416 0
+-255 -417 0
+-256 -418 0
+-257 -419 0
+-258 -420 0
+-259 -421 0
+-260 -422 0
+-261 -423 0
+-262 -424 0
+-263 -425 0
+-264 -426 0
+-265 -427 0
+-266 -428 0
+-267 -429 0
+-268 -430 0
+-269 -431 0
+-270 -432 0
+-271 -433 0
+-272 -434 0
+-273 -435 0
+-274 -436 0
+-275 -437 0
+-276 -438 0
+-277 -439 0
+-278 -440 0
+-279 -441 0
+-280 -442 0
+-281 -443 0
+-282 -444 0
+-283 -445 0
+-284 -446 0
+-285 -447 0
+-286 -448 0
+-287 -449 0
+-288 -450 0
+-289 -451 0
+-290 -452 0
+-291 -453 0
+-292 -454 0
+-293 -455 0
+-294 -456 0
+-295 -457 0
+-296 -458 0
+-297 -459 0
+-298 -460 0
+-299 -461 0
+-300 -462 0
+-301 -463 0
+-302 -464 0
+-303 -465 0
+-304 -466 0
+-305 -467 0
+-306 -468 0
+-307 -469 0
+-308 -470 0
+-309 -471 0
+-310 -472 0
+-311 -473 0
+-312 -474 0
+-313 -475 0
+-314 -476 0
+-315 -477 0
+-316 -478 0
+-317 -479 0
+-318 -480 0
+-319 -481 0
+-320 -482 0
+-321 -483 0
+-322 -484 0
+-323 -485 0
+-324 -486 0
+-244 -487 0
+-245 -488 0
+-246 -489 0
+-247 -490 0
+-248 -491 0
+-249 -492 0
+-250 -493 0
+-251 -494 0
+-252 -495 0
+-253 -496 0
+-254 -497 0
+-255 -498 0
+-256 -499 0
+-257 -500 0
+-258 -501 0
+-259 -502 0
+-260 -503 0
+-261 -504 0
+-262 -505 0
+-263 -506 0
+-264 -507 0
+-265 -508 0
+-266 -509 0
+-267 -510 0
+-268 -511 0
+-269 -512 0
+-270 -513 0
+-271 -514 0
+-272 -515 0
+-273 -516 0
+-274 -517 0
+-275 -518 0
+-276 -519 0
+-277 -520 0
+-278 -521 0
+-279 -522 0
+-280 -523 0
+-281 -524 0
+-282 -525 0
+-283 -526 0
+-284 -527 0
+-285 -528 0
+-286 -529 0
+-287 -530 0
+-288 -531 0
+-289 -532 0
+-290 -533 0
+-291 -534 0
+-292 -535 0
+-293 -536 0
+-294 -537 0
+-295 -538 0
+-296 -539 0
+-297 -540 0
+-298 -541 0
+-299 -542 0
+-300 -543 0
+-301 -544 0
+-302 -545 0
+-303 -546 0
+-304 -547 0
+-305 -548 0
+-306 -549 0
+-307 -550 0
+-308 -551 0
+-309 -552 0
+-310 -553 0
+-311 -554 0
+-312 -555 0
+-313 -556 0
+-314 -557 0
+-315 -558 0
+-316 -559 0
+-317 -560 0
+-318 -561 0
+-319 -562 0
+-320 -563 0
+-321 -564 0
+-322 -565 0
+-323 -566 0
+-324 -567 0
+-244 -568 0
+-245 -569 0
+-246 -570 0
+-247 -571 0
+-248 -572 0
+-249 -573 0
+-250 -574 0
+-251 -575 0
+-252 -576 0
+-253 -577 0
+-254 -578 0
+-255 -579 0
+-256 -580 0
+-257 -581 0
+-258 -582 0
+-259 -583 0
+-260 -584 0
+-261 -585 0
+-262 -586 0
+-263 -587 0
+-264 -588 0
+-265 -589 0
+-266 -590 0
+-267 -591 0
+-268 -592 0
+-269 -593 0
+-270 -594 0
+-271 -595 0
+-272 -596 0
+-273 -597 0
+-274 -598 0
+-275 -599 0
+-276 -600 0
+-277 -601 0
+-278 -602 0
+-279 -603 0
+-280 -604 0
+-281 -605 0
+-282 -606 0
+-283 -607 0
+-284 -608 0
+-285 -609 0
+-286 -610 0
+-287 -611 0
+-288 -612 0
+-289 -613 0
+-290 -614 0
+-291 -615 0
+-292 -616 0
+-293 -617 0
+-294 -618 0
+-295 -619 0
+-296 -620 0
+-297 -621 0
+-298 -622 0
+-299 -623 0
+-300 -624 0
+-301 -625 0
+-302 -626 0
+-303 -627 0
+-304 -628 0
+-305 -629 0
+-306 -630 0
+-307 -631 0
+-308 -632 0
+-309 -633 0
+-310 -634 0
+-311 -635 0
+-312 -636 0
+-313 -637 0
+-314 -638 0
+-315 -639 0
+-316 -640 0
+-317 -641 0
+-318 -642 0
+-319 -643 0
+-320 -644 0
+-321 -645 0
+-322 -646 0
+-323 -647 0
+-324 -648 0
+-244 -649 0
+-245 -650 0
+-246 -651 0
+-247 -652 0
+-248 -653 0
+-249 -654 0
+-250 -655 0
+-251 -656 0
+-252 -657 0
+-253 -658 0
+-254 -659 0
+-255 -660 0
+-256 -661 0
+-257 -662 0
+-258 -663 0
+-259 -664 0
+-260 -665 0
+-261 -666 0
+-262 -667 0
+-263 -668 0
+-264 -669 0
+-265 -670 0
+-266 -671 0
+-267 -672 0
+-268 -673 0
+-269 -674 0
+-270 -675 0
+-271 -676 0
+-272 -677 0
+-273 -678 0
+-274 -679 0
+-275 -680 0
+-276 -681 0
+-277 -682 0
+-278 -683 0
+-279 -684 0
+-280 -685 0
+-281 -686 0
+-282 -687 0
+-283 -688 0
+-284 -689 0
+-285 -690 0
+-286 -691 0
+-287 -692 0
+-288 -693 0
+-289 -694 0
+-290 -695 0
+-291 -696 0
+-292 -697 0
+-293 -698 0
+-294 -699 0
+-295 -700 0
+-296 -701 0
+-297 -702 0
+-298 -703 0
+-299 -704 0
+-300 -705 0
+-301 -706 0
+-302 -707 0
+-303 -708 0
+-304 -709 0
+-305 -710 0
+-306 -711 0
+-307 -712 0
+-308 -713 0
+-309 -714 0
+-310 -715 0
+-311 -716 0
+-312 -717 0
+-313 -718 0
+-314 -719 0
+-315 -720 0
+-316 -721 0
+-317 -722 0
+-318 -723 0
+-319 -724 0
+-320 -725 0
+-321 -726 0
+-322 -727 0
+-323 -728 0
+-324 -729 0
+-325 -1 0
+-326 -2 0
+-327 -3 0
+-328 -4 0
+-329 -5 0
+-330 -6 0
+-331 -7 0
+-332 -8 0
+-333 -9 0
+-334 -10 0
+-335 -11 0
+-336 -12 0
+-337 -13 0
+-338 -14 0
+-339 -15 0
+-340 -16 0
+-341 -17 0
+-342 -18 0
+-343 -19 0
+-344 -20 0
+-345 -21 0
+-346 -22 0
+-347 -23 0
+-348 -24 0
+-349 -25 0
+-350 -26 0
+-351 -27 0
+-352 -28 0
+-353 -29 0
+-354 -30 0
+-355 -31 0
+-356 -32 0
+-357 -33 0
+-358 -34 0
+-359 -35 0
+-360 -36 0
+-361 -37 0
+-362 -38 0
+-363 -39 0
+-364 -40 0
+-365 -41 0
+-366 -42 0
+-367 -43 0
+-368 -44 0
+-369 -45 0
+-370 -46 0
+-371 -47 0
+-372 -48 0
+-373 -49 0
+-374 -50 0
+-375 -51 0
+-376 -52 0
+-377 -53 0
+-378 -54 0
+-379 -55 0
+-380 -56 0
+-381 -57 0
+-382 -58 0
+-383 -59 0
+-384 -60 0
+-385 -61 0
+-386 -62 0
+-387 -63 0
+-388 -64 0
+-389 -65 0
+-390 -66 0
+-391 -67 0
+-392 -68 0
+-393 -69 0
+-394 -70 0
+-395 -71 0
+-396 -72 0
+-397 -73 0
+-398 -74 0
+-399 -75 0
+-400 -76 0
+-401 -77 0
+-402 -78 0
+-403 -79 0
+-404 -80 0
+-405 -81 0
+-325 -82 0
+-326 -83 0
+-327 -84 0
+-328 -85 0
+-329 -86 0
+-330 -87 0
+-331 -88 0
+-332 -89 0
+-333 -90 0
+-334 -91 0
+-335 -92 0
+-336 -93 0
+-337 -94 0
+-338 -95 0
+-339 -96 0
+-340 -97 0
+-341 -98 0
+-342 -99 0
+-343 -100 0
+-344 -101 0
+-345 -102 0
+-346 -103 0
+-347 -104 0
+-348 -105 0
+-349 -106 0
+-350 -107 0
+-351 -108 0
+-352 -109 0
+-353 -110 0
+-354 -111 0
+-355 -112 0
+-356 -113 0
+-357 -114 0
+-358 -115 0
+-359 -116 0
+-360 -117 0
+-361 -118 0
+-362 -119 0
+-363 -120 0
+-364 -121 0
+-365 -122 0
+-366 -123 0
+-367 -124 0
+-368 -125 0
+-369 -126 0
+-370 -127 0
+-371 -128 0
+-372 -129 0
+-373 -130 0
+-374 -131 0
+-375 -132 0
+-376 -133 0
+-377 -134 0
+-378 -135 0
+-379 -136 0
+-380 -137 0
+-381 -138 0
+-382 -139 0
+-383 -140 0
+-384 -141 0
+-385 -142 0
+-386 -143 0
+-387 -144 0
+-388 -145 0
+-389 -146 0
+-390 -147 0
+-391 -148 0
+-392 -149 0
+-393 -150 0
+-394 -151 0
+-395 -152 0
+-396 -153 0
+-397 -154 0
+-398 -155 0
+-399 -156 0
+-400 -157 0
+-401 -158 0
+-402 -159 0
+-403 -160 0
+-404 -161 0
+-405 -162 0
+-325 -163 0
+-326 -164 0
+-327 -165 0
+-328 -166 0
+-329 -167 0
+-330 -168 0
+-331 -169 0
+-332 -170 0
+-333 -171 0
+-334 -172 0
+-335 -173 0
+-336 -174 0
+-337 -175 0
+-338 -176 0
+-339 -177 0
+-340 -178 0
+-341 -179 0
+-342 -180 0
+-343 -181 0
+-344 -182 0
+-345 -183 0
+-346 -184 0
+-347 -185 0
+-348 -186 0
+-349 -187 0
+-350 -188 0
+-351 -189 0
+-352 -190 0
+-353 -191 0
+-354 -192 0
+-355 -193 0
+-356 -194 0
+-357 -195 0
+-358 -196 0
+-359 -197 0
+-360 -198 0
+-361 -199 0
+-362 -200 0
+-363 -201 0
+-364 -202 0
+-365 -203 0
+-366 -204 0
+-367 -205 0
+-368 -206 0
+-369 -207 0
+-370 -208 0
+-371 -209 0
+-372 -210 0
+-373 -211 0
+-374 -212 0
+-375 -213 0
+-376 -214 0
+-377 -215 0
+-378 -216 0
+-379 -217 0
+-380 -218 0
+-381 -219 0
+-382 -220 0
+-383 -221 0
+-384 -222 0
+-385 -223 0
+-386 -224 0
+-387 -225 0
+-388 -226 0
+-389 -227 0
+-390 -228 0
+-391 -229 0
+-392 -230 0
+-393 -231 0
+-394 -232 0
+-395 -233 0
+-396 -234 0
+-397 -235 0
+-398 -236 0
+-399 -237 0
+-400 -238 0
+-401 -239 0
+-402 -240 0
+-403 -241 0
+-404 -242 0
+-405 -243 0
+-325 -244 0
+-326 -245 0
+-327 -246 0
+-328 -247 0
+-329 -248 0
+-330 -249 0
+-331 -250 0
+-332 -251 0
+-333 -252 0
+-334 -253 0
+-335 -254 0
+-336 -255 0
+-337 -256 0
+-338 -257 0
+-339 -258 0
+-340 -259 0
+-341 -260 0
+-342 -261 0
+-343 -262 0
+-344 -263 0
+-345 -264 0
+-346 -265 0
+-347 -266 0
+-348 -267 0
+-349 -268 0
+-350 -269 0
+-351 -270 0
+-352 -271 0
+-353 -272 0
+-354 -273 0
+-355 -274 0
+-356 -275 0
+-357 -276 0
+-358 -277 0
+-359 -278 0
+-360 -279 0
+-361 -280 0
+-362 -281 0
+-363 -282 0
+-364 -283 0
+-365 -284 0
+-366 -285 0
+-367 -286 0
+-368 -287 0
+-369 -288 0
+-370 -289 0
+-371 -290 0
+-372 -291 0
+-373 -292 0
+-374 -293 0
+-375 -294 0
+-376 -295 0
+-377 -296 0
+-378 -297 0
+-379 -298 0
+-380 -299 0
+-381 -300 0
+-382 -301 0
+-383 -302 0
+-384 -303 0
+-385 -304 0
+-386 -305 0
+-387 -306 0
+-388 -307 0
+-389 -308 0
+-390 -309 0
+-391 -310 0
+-392 -311 0
+-393 -312 0
+-394 -313 0
+-395 -314 0
+-396 -315 0
+-397 -316 0
+-398 -317 0
+-399 -318 0
+-400 -319 0
+-401 -320 0
+-402 -321 0
+-403 -322 0
+-404 -323 0
+-405 -324 0
+-325 -406 0
+-326 -407 0
+-327 -408 0
+-328 -409 0
+-329 -410 0
+-330 -411 0
+-331 -412 0
+-332 -413 0
+-333 -414 0
+-334 -415 0
+-335 -416 0
+-336 -417 0
+-337 -418 0
+-338 -419 0
+-339 -420 0
+-340 -421 0
+-341 -422 0
+-342 -423 0
+-343 -424 0
+-344 -425 0
+-345 -426 0
+-346 -427 0
+-347 -428 0
+-348 -429 0
+-349 -430 0
+-350 -431 0
+-351 -432 0
+-352 -433 0
+-353 -434 0
+-354 -435 0
+-355 -436 0
+-356 -437 0
+-357 -438 0
+-358 -439 0
+-359 -440 0
+-360 -441 0
+-361 -442 0
+-362 -443 0
+-363 -444 0
+-364 -445 0
+-365 -446 0
+-366 -447 0
+-367 -448 0
+-368 -449 0
+-369 -450 0
+-370 -451 0
+-371 -452 0
+-372 -453 0
+-373 -454 0
+-374 -455 0
+-375 -456 0
+-376 -457 0
+-377 -458 0
+-378 -459 0
+-379 -460 0
+-380 -461 0
+-381 -462 0
+-382 -463 0
+-383 -464 0
+-384 -465 0
+-385 -466 0
+-386 -467 0
+-387 -468 0
+-388 -469 0
+-389 -470 0
+-390 -471 0
+-391 -472 0
+-392 -473 0
+-393 -474 0
+-394 -475 0
+-395 -476 0
+-396 -477 0
+-397 -478 0
+-398 -479 0
+-399 -480 0
+-400 -481 0
+-401 -482 0
+-402 -483 0
+-403 -484 0
+-404 -485 0
+-405 -486 0
+-325 -487 0
+-326 -488 0
+-327 -489 0
+-328 -490 0
+-329 -491 0
+-330 -492 0
+-331 -493 0
+-332 -494 0
+-333 -495 0
+-334 -496 0
+-335 -497 0
+-336 -498 0
+-337 -499 0
+-338 -500 0
+-339 -501 0
+-340 -502 0
+-341 -503 0
+-342 -504 0
+-343 -505 0
+-344 -506 0
+-345 -507 0
+-346 -508 0
+-347 -509 0
+-348 -510 0
+-349 -511 0
+-350 -512 0
+-351 -513 0
+-352 -514 0
+-353 -515 0
+-354 -516 0
+-355 -517 0
+-356 -518 0
+-357 -519 0
+-358 -520 0
+-359 -521 0
+-360 -522 0
+-361 -523 0
+-362 -524 0
+-363 -525 0
+-364 -526 0
+-365 -527 0
+-366 -528 0
+-367 -529 0
+-368 -530 0
+-369 -531 0
+-370 -532 0
+-371 -533 0
+-372 -534 0
+-373 -535 0
+-374 -536 0
+-375 -537 0
+-376 -538 0
+-377 -539 0
+-378 -540 0
+-379 -541 0
+-380 -542 0
+-381 -543 0
+-382 -544 0
+-383 -545 0
+-384 -546 0
+-385 -547 0
+-386 -548 0
+-387 -549 0
+-388 -550 0
+-389 -551 0
+-390 -552 0
+-391 -553 0
+-392 -554 0
+-393 -555 0
+-394 -556 0
+-395 -557 0
+-396 -558 0
+-397 -559 0
+-398 -560 0
+-399 -561 0
+-400 -562 0
+-401 -563 0
+-402 -564 0
+-403 -565 0
+-404 -566 0
+-405 -567 0
+-325 -568 0
+-326 -569 0
+-327 -570 0
+-328 -571 0
+-329 -572 0
+-330 -573 0
+-331 -574 0
+-332 -575 0
+-333 -576 0
+-334 -577 0
+-335 -578 0
+-336 -579 0
+-337 -580 0
+-338 -581 0
+-339 -582 0
+-340 -583 0
+-341 -584 0
+-342 -585 0
+-343 -586 0
+-344 -587 0
+-345 -588 0
+-346 -589 0
+-347 -590 0
+-348 -591 0
+-349 -592 0
+-350 -593 0
+-351 -594 0
+-352 -595 0
+-353 -596 0
+-354 -597 0
+-355 -598 0
+-356 -599 0
+-357 -600 0
+-358 -601 0
+-359 -602 0
+-360 -603 0
+-361 -604 0
+-362 -605 0
+-363 -606 0
+-364 -607 0
+-365 -608 0
+-366 -609 0
+-367 -610 0
+-368 -611 0
+-369 -612 0
+-370 -613 0
+-371 -614 0
+-372 -615 0
+-373 -616 0
+-374 -617 0
+-375 -618 0
+-376 -619 0
+-377 -620 0
+-378 -621 0
+-379 -622 0
+-380 -623 0
+-381 -624 0
+-382 -625 0
+-383 -626 0
+-384 -627 0
+-385 -628 0
+-386 -629 0
+-387 -630 0
+-388 -631 0
+-389 -632 0
+-390 -633 0
+-391 -634 0
+-392 -635 0
+-393 -636 0
+-394 -637 0
+-395 -638 0
+-396 -639 0
+-397 -640 0
+-398 -641 0
+-399 -642 0
+-400 -643 0
+-401 -644 0
+-402 -645 0
+-403 -646 0
+-404 -647 0
+-405 -648 0
+-325 -649 0
+-326 -650 0
+-327 -651 0
+-328 -652 0
+-329 -653 0
+-330 -654 0
+-331 -655 0
+-332 -656 0
+-333 -657 0
+-334 -658 0
+-335 -659 0
+-336 -660 0
+-337 -661 0
+-338 -662 0
+-339 -663 0
+-340 -664 0
+-341 -665 0
+-342 -666 0
+-343 -667 0
+-344 -668 0
+-345 -669 0
+-346 -670 0
+-347 -671 0
+-348 -672 0
+-349 -673 0
+-350 -674 0
+-351 -675 0
+-352 -676 0
+-353 -677 0
+-354 -678 0
+-355 -679 0
+-356 -680 0
+-357 -681 0
+-358 -682 0
+-359 -683 0
+-360 -684 0
+-361 -685 0
+-362 -686 0
+-363 -687 0
+-364 -688 0
+-365 -689 0
+-366 -690 0
+-367 -691 0
+-368 -692 0
+-369 -693 0
+-370 -694 0
+-371 -695 0
+-372 -696 0
+-373 -697 0
+-374 -698 0
+-375 -699 0
+-376 -700 0
+-377 -701 0
+-378 -702 0
+-379 -703 0
+-380 -704 0
+-381 -705 0
+-382 -706 0
+-383 -707 0
+-384 -708 0
+-385 -709 0
+-386 -710 0
+-387 -711 0
+-388 -712 0
+-389 -713 0
+-390 -714 0
+-391 -715 0
+-392 -716 0
+-393 -717 0
+-394 -718 0
+-395 -719 0
+-396 -720 0
+-397 -721 0
+-398 -722 0
+-399 -723 0
+-400 -724 0
+-401 -725 0
+-402 -726 0
+-403 -727 0
+-404 -728 0
+-405 -729 0
+-406 -1 0
+-407 -2 0
+-408 -3 0
+-409 -4 0
+-410 -5 0
+-411 -6 0
+-412 -7 0
+-413 -8 0
+-414 -9 0
+-415 -10 0
+-416 -11 0
+-417 -12 0
+-418 -13 0
+-419 -14 0
+-420 -15 0
+-421 -16 0
+-422 -17 0
+-423 -18 0
+-424 -19 0
+-425 -20 0
+-426 -21 0
+-427 -22 0
+-428 -23 0
+-429 -24 0
+-430 -25 0
+-431 -26 0
+-432 -27 0
+-433 -28 0
+-434 -29 0
+-435 -30 0
+-436 -31 0
+-437 -32 0
+-438 -33 0
+-439 -34 0
+-440 -35 0
+-441 -36 0
+-442 -37 0
+-443 -38 0
+-444 -39 0
+-445 -40 0
+-446 -41 0
+-447 -42 0
+-448 -43 0
+-449 -44 0
+-450 -45 0
+-451 -46 0
+-452 -47 0
+-453 -48 0
+-454 -49 0
+-455 -50 0
+-456 -51 0
+-457 -52 0
+-458 -53 0
+-459 -54 0
+-460 -55 0
+-461 -56 0
+-462 -57 0
+-463 -58 0
+-464 -59 0
+-465 -60 0
+-466 -61 0
+-467 -62 0
+-468 -63 0
+-469 -64 0
+-470 -65 0
+-471 -66 0
+-472 -67 0
+-473 -68 0
+-474 -69 0
+-475 -70 0
+-476 -71 0
+-477 -72 0
+-478 -73 0
+-479 -74 0
+-480 -75 0
+-481 -76 0
+-482 -77 0
+-483 -78 0
+-484 -79 0
+-485 -80 0
+-486 -81 0
+-406 -82 0
+-407 -83 0
+-408 -84 0
+-409 -85 0
+-410 -86 0
+-411 -87 0
+-412 -88 0
+-413 -89 0
+-414 -90 0
+-415 -91 0
+-416 -92 0
+-417 -93 0
+-418 -94 0
+-419 -95 0
+-420 -96 0
+-421 -97 0
+-422 -98 0
+-423 -99 0
+-424 -100 0
+-425 -101 0
+-426 -102 0
+-427 -103 0
+-428 -104 0
+-429 -105 0
+-430 -106 0
+-431 -107 0
+-432 -108 0
+-433 -109 0
+-434 -110 0
+-435 -111 0
+-436 -112 0
+-437 -113 0
+-438 -114 0
+-439 -115 0
+-440 -116 0
+-441 -117 0
+-442 -118 0
+-443 -119 0
+-444 -120 0
+-445 -121 0
+-446 -122 0
+-447 -123 0
+-448 -124 0
+-449 -125 0
+-450 -126 0
+-451 -127 0
+-452 -128 0
+-453 -129 0
+-454 -130 0
+-455 -131 0
+-456 -132 0
+-457 -133 0
+-458 -134 0
+-459 -135 0
+-460 -136 0
+-461 -137 0
+-462 -138 0
+-463 -139 0
+-464 -140 0
+-465 -141 0
+-466 -142 0
+-467 -143 0
+-468 -144 0
+-469 -145 0
+-470 -146 0
+-471 -147 0
+-472 -148 0
+-473 -149 0
+-474 -150 0
+-475 -151 0
+-476 -152 0
+-477 -153 0
+-478 -154 0
+-479 -155 0
+-480 -156 0
+-481 -157 0
+-482 -158 0
+-483 -159 0
+-484 -160 0
+-485 -161 0
+-486 -162 0
+-406 -163 0
+-407 -164 0
+-408 -165 0
+-409 -166 0
+-410 -167 0
+-411 -168 0
+-412 -169 0
+-413 -170 0
+-414 -171 0
+-415 -172 0
+-416 -173 0
+-417 -174 0
+-418 -175 0
+-419 -176 0
+-420 -177 0
+-421 -178 0
+-422 -179 0
+-423 -180 0
+-424 -181 0
+-425 -182 0
+-426 -183 0
+-427 -184 0
+-428 -185 0
+-429 -186 0
+-430 -187 0
+-431 -188 0
+-432 -189 0
+-433 -190 0
+-434 -191 0
+-435 -192 0
+-436 -193 0
+-437 -194 0
+-438 -195 0
+-439 -196 0
+-440 -197 0
+-441 -198 0
+-442 -199 0
+-443 -200 0
+-444 -201 0
+-445 -202 0
+-446 -203 0
+-447 -204 0
+-448 -205 0
+-449 -206 0
+-450 -207 0
+-451 -208 0
+-452 -209 0
+-453 -210 0
+-454 -211 0
+-455 -212 0
+-456 -213 0
+-457 -214 0
+-458 -215 0
+-459 -216 0
+-460 -217 0
+-461 -218 0
+-462 -219 0
+-463 -220 0
+-464 -221 0
+-465 -222 0
+-466 -223 0
+-467 -224 0
+-468 -225 0
+-469 -226 0
+-470 -227 0
+-471 -228 0
+-472 -229 0
+-473 -230 0
+-474 -231 0
+-475 -232 0
+-476 -233 0
+-477 -234 0
+-478 -235 0
+-479 -236 0
+-480 -237 0
+-481 -238 0
+-482 -239 0
+-483 -240 0
+-484 -241 0
+-485 -242 0
+-486 -243 0
+-406 -244 0
+-407 -245 0
+-408 -246 0
+-409 -247 0
+-410 -248 0
+-411 -249 0
+-412 -250 0
+-413 -251 0
+-414 -252 0
+-415 -253 0
+-416 -254 0
+-417 -255 0
+-418 -256 0
+-419 -257 0
+-420 -258 0
+-421 -259 0
+-422 -260 0
+-423 -261 0
+-424 -262 0
+-425 -263 0
+-426 -264 0
+-427 -265 0
+-428 -266 0
+-429 -267 0
+-430 -268 0
+-431 -269 0
+-432 -270 0
+-433 -271 0
+-434 -272 0
+-435 -273 0
+-436 -274 0
+-437 -275 0
+-438 -276 0
+-439 -277 0
+-440 -278 0
+-441 -279 0
+-442 -280 0
+-443 -281 0
+-444 -282 0
+-445 -283 0
+-446 -284 0
+-447 -285 0
+-448 -286 0
+-449 -287 0
+-450 -288 0
+-451 -289 0
+-452 -290 0
+-453 -291 0
+-454 -292 0
+-455 -293 0
+-456 -294 0
+-457 -295 0
+-458 -296 0
+-459 -297 0
+-460 -298 0
+-461 -299 0
+-462 -300 0
+-463 -301 0
+-464 -302 0
+-465 -303 0
+-466 -304 0
+-467 -305 0
+-468 -306 0
+-469 -307 0
+-470 -308 0
+-471 -309 0
+-472 -310 0
+-473 -311 0
+-474 -312 0
+-475 -313 0
+-476 -314 0
+-477 -315 0
+-478 -316 0
+-479 -317 0
+-480 -318 0
+-481 -319 0
+-482 -320 0
+-483 -321 0
+-484 -322 0
+-485 -323 0
+-486 -324 0
+-406 -325 0
+-407 -326 0
+-408 -327 0
+-409 -328 0
+-410 -329 0
+-411 -330 0
+-412 -331 0
+-413 -332 0
+-414 -333 0
+-415 -334 0
+-416 -335 0
+-417 -336 0
+-418 -337 0
+-419 -338 0
+-420 -339 0
+-421 -340 0
+-422 -341 0
+-423 -342 0
+-424 -343 0
+-425 -344 0
+-426 -345 0
+-427 -346 0
+-428 -347 0
+-429 -348 0
+-430 -349 0
+-431 -350 0
+-432 -351 0
+-433 -352 0
+-434 -353 0
+-435 -354 0
+-436 -355 0
+-437 -356 0
+-438 -357 0
+-439 -358 0
+-440 -359 0
+-441 -360 0
+-442 -361 0
+-443 -362 0
+-444 -363 0
+-445 -364 0
+-446 -365 0
+-447 -366 0
+-448 -367 0
+-449 -368 0
+-450 -369 0
+-451 -370 0
+-452 -371 0
+-453 -372 0
+-454 -373 0
+-455 -374 0
+-456 -375 0
+-457 -376 0
+-458 -377 0
+-459 -378 0
+-460 -379 0
+-461 -380 0
+-462 -381 0
+-463 -382 0
+-464 -383 0
+-465 -384 0
+-466 -385 0
+-467 -386 0
+-468 -387 0
+-469 -388 0
+-470 -389 0
+-471 -390 0
+-472 -391 0
+-473 -392 0
+-474 -393 0
+-475 -394 0
+-476 -395 0
+-477 -396 0
+-478 -397 0
+-479 -398 0
+-480 -399 0
+-481 -400 0
+-482 -401 0
+-483 -402 0
+-484 -403 0
+-485 -404 0
+-486 -405 0
+-406 -487 0
+-407 -488 0
+-408 -489 0
+-409 -490 0
+-410 -491 0
+-411 -492 0
+-412 -493 0
+-413 -494 0
+-414 -495 0
+-415 -496 0
+-416 -497 0
+-417 -498 0
+-418 -499 0
+-419 -500 0
+-420 -501 0
+-421 -502 0
+-422 -503 0
+-423 -504 0
+-424 -505 0
+-425 -506 0
+-426 -507 0
+-427 -508 0
+-428 -509 0
+-429 -510 0
+-430 -511 0
+-431 -512 0
+-432 -513 0
+-433 -514 0
+-434 -515 0
+-435 -516 0
+-436 -517 0
+-437 -518 0
+-438 -519 0
+-439 -520 0
+-440 -521 0
+-441 -522 0
+-442 -523 0
+-443 -524 0
+-444 -525 0
+-445 -526 0
+-446 -527 0
+-447 -528 0
+-448 -529 0
+-449 -530 0
+-450 -531 0
+-451 -532 0
+-452 -533 0
+-453 -534 0
+-454 -535 0
+-455 -536 0
+-456 -537 0
+-457 -538 0
+-458 -539 0
+-459 -540 0
+-460 -541 0
+-461 -542 0
+-462 -543 0
+-463 -544 0
+-464 -545 0
+-465 -546 0
+-466 -547 0
+-467 -548 0
+-468 -549 0
+-469 -550 0
+-470 -551 0
+-471 -552 0
+-472 -553 0
+-473 -554 0
+-474 -555 0
+-475 -556 0
+-476 -557 0
+-477 -558 0
+-478 -559 0
+-479 -560 0
+-480 -561 0
+-481 -562 0
+-482 -563 0
+-483 -564 0
+-484 -565 0
+-485 -566 0
+-486 -567 0
+-406 -568 0
+-407 -569 0
+-408 -570 0
+-409 -571 0
+-410 -572 0
+-411 -573 0
+-412 -574 0
+-413 -575 0
+-414 -576 0
+-415 -577 0
+-416 -578 0
+-417 -579 0
+-418 -580 0
+-419 -581 0
+-420 -582 0
+-421 -583 0
+-422 -584 0
+-423 -585 0
+-424 -586 0
+-425 -587 0
+-426 -588 0
+-427 -589 0
+-428 -590 0
+-429 -591 0
+-430 -592 0
+-431 -593 0
+-432 -594 0
+-433 -595 0
+-434 -596 0
+-435 -597 0
+-436 -598 0
+-437 -599 0
+-438 -600 0
+-439 -601 0
+-440 -602 0
+-441 -603 0
+-442 -604 0
+-443 -605 0
+-444 -606 0
+-445 -607 0
+-446 -608 0
+-447 -609 0
+-448 -610 0
+-449 -611 0
+-450 -612 0
+-451 -613 0
+-452 -614 0
+-453 -615 0
+-454 -616 0
+-455 -617 0
+-456 -618 0
+-457 -619 0
+-458 -620 0
+-459 -621 0
+-460 -622 0
+-461 -623 0
+-462 -624 0
+-463 -625 0
+-464 -626 0
+-465 -627 0
+-466 -628 0
+-467 -629 0
+-468 -630 0
+-469 -631 0
+-470 -632 0
+-471 -633 0
+-472 -634 0
+-473 -635 0
+-474 -636 0
+-475 -637 0
+-476 -638 0
+-477 -639 0
+-478 -640 0
+-479 -641 0
+-480 -642 0
+-481 -643 0
+-482 -644 0
+-483 -645 0
+-484 -646 0
+-485 -647 0
+-486 -648 0
+-406 -649 0
+-407 -650 0
+-408 -651 0
+-409 -652 0
+-410 -653 0
+-411 -654 0
+-412 -655 0
+-413 -656 0
+-414 -657 0
+-415 -658 0
+-416 -659 0
+-417 -660 0
+-418 -661 0
+-419 -662 0
+-420 -663 0
+-421 -664 0
+-422 -665 0
+-423 -666 0
+-424 -667 0
+-425 -668 0
+-426 -669 0
+-427 -670 0
+-428 -671 0
+-429 -672 0
+-430 -673 0
+-431 -674 0
+-432 -675 0
+-433 -676 0
+-434 -677 0
+-435 -678 0
+-436 -679 0
+-437 -680 0
+-438 -681 0
+-439 -682 0
+-440 -683 0
+-441 -684 0
+-442 -685 0
+-443 -686 0
+-444 -687 0
+-445 -688 0
+-446 -689 0
+-447 -690 0
+-448 -691 0
+-449 -692 0
+-450 -693 0
+-451 -694 0
+-452 -695 0
+-453 -696 0
+-454 -697 0
+-455 -698 0
+-456 -699 0
+-457 -700 0
+-458 -701 0
+-459 -702 0
+-460 -703 0
+-461 -704 0
+-462 -705 0
+-463 -706 0
+-464 -707 0
+-465 -708 0
+-466 -709 0
+-467 -710 0
+-468 -711 0
+-469 -712 0
+-470 -713 0
+-471 -714 0
+-472 -715 0
+-473 -716 0
+-474 -717 0
+-475 -718 0
+-476 -719 0
+-477 -720 0
+-478 -721 0
+-479 -722 0
+-480 -723 0
+-481 -724 0
+-482 -725 0
+-483 -726 0
+-484 -727 0
+-485 -728 0
+-486 -729 0
+-487 -1 0
+-488 -2 0
+-489 -3 0
+-490 -4 0
+-491 -5 0
+-492 -6 0
+-493 -7 0
+-494 -8 0
+-495 -9 0
+-496 -10 0
+-497 -11 0
+-498 -12 0
+-499 -13 0
+-500 -14 0
+-501 -15 0
+-502 -16 0
+-503 -17 0
+-504 -18 0
+-505 -19 0
+-506 -20 0
+-507 -21 0
+-508 -22 0
+-509 -23 0
+-510 -24 0
+-511 -25 0
+-512 -26 0
+-513 -27 0
+-514 -28 0
+-515 -29 0
+-516 -30 0
+-517 -31 0
+-518 -32 0
+-519 -33 0
+-520 -34 0
+-521 -35 0
+-522 -36 0
+-523 -37 0
+-524 -38 0
+-525 -39 0
+-526 -40 0
+-527 -41 0
+-528 -42 0
+-529 -43 0
+-530 -44 0
+-531 -45 0
+-532 -46 0
+-533 -47 0
+-534 -48 0
+-535 -49 0
+-536 -50 0
+-537 -51 0
+-538 -52 0
+-539 -53 0
+-540 -54 0
+-541 -55 0
+-542 -56 0
+-543 -57 0
+-544 -58 0
+-545 -59 0
+-546 -60 0
+-547 -61 0
+-548 -62 0
+-549 -63 0
+-550 -64 0
+-551 -65 0
+-552 -66 0
+-553 -67 0
+-554 -68 0
+-555 -69 0
+-556 -70 0
+-557 -71 0
+-558 -72 0
+-559 -73 0
+-560 -74 0
+-561 -75 0
+-562 -76 0
+-563 -77 0
+-564 -78 0
+-565 -79 0
+-566 -80 0
+-567 -81 0
+-487 -82 0
+-488 -83 0
+-489 -84 0
+-490 -85 0
+-491 -86 0
+-492 -87 0
+-493 -88 0
+-494 -89 0
+-495 -90 0
+-496 -91 0
+-497 -92 0
+-498 -93 0
+-499 -94 0
+-500 -95 0
+-501 -96 0
+-502 -97 0
+-503 -98 0
+-504 -99 0
+-505 -100 0
+-506 -101 0
+-507 -102 0
+-508 -103 0
+-509 -104 0
+-510 -105 0
+-511 -106 0
+-512 -107 0
+-513 -108 0
+-514 -109 0
+-515 -110 0
+-516 -111 0
+-517 -112 0
+-518 -113 0
+-519 -114 0
+-520 -115 0
+-521 -116 0
+-522 -117 0
+-523 -118 0
+-524 -119 0
+-525 -120 0
+-526 -121 0
+-527 -122 0
+-528 -123 0
+-529 -124 0
+-530 -125 0
+-531 -126 0
+-532 -127 0
+-533 -128 0
+-534 -129 0
+-535 -130 0
+-536 -131 0
+-537 -132 0
+-538 -133 0
+-539 -134 0
+-540 -135 0
+-541 -136 0
+-542 -137 0
+-543 -138 0
+-544 -139 0
+-545 -140 0
+-546 -141 0
+-547 -142 0
+-548 -143 0
+-549 -144 0
+-550 -145 0
+-551 -146 0
+-552 -147 0
+-553 -148 0
+-554 -149 0
+-555 -150 0
+-556 -151 0
+-557 -152 0
+-558 -153 0
+-559 -154 0
+-560 -155 0
+-561 -156 0
+-562 -157 0
+-563 -158 0
+-564 -159 0
+-565 -160 0
+-566 -161 0
+-567 -162 0
+-487 -163 0
+-488 -164 0
+-489 -165 0
+-490 -166 0
+-491 -167 0
+-492 -168 0
+-493 -169 0
+-494 -170 0
+-495 -171 0
+-496 -172 0
+-497 -173 0
+-498 -174 0
+-499 -175 0
+-500 -176 0
+-501 -177 0
+-502 -178 0
+-503 -179 0
+-504 -180 0
+-505 -181 0
+-506 -182 0
+-507 -183 0
+-508 -184 0
+-509 -185 0
+-510 -186 0
+-511 -187 0
+-512 -188 0
+-513 -189 0
+-514 -190 0
+-515 -191 0
+-516 -192 0
+-517 -193 0
+-518 -194 0
+-519 -195 0
+-520 -196 0
+-521 -197 0
+-522 -198 0
+-523 -199 0
+-524 -200 0
+-525 -201 0
+-526 -202 0
+-527 -203 0
+-528 -204 0
+-529 -205 0
+-530 -206 0
+-531 -207 0
+-532 -208 0
+-533 -209 0
+-534 -210 0
+-535 -211 0
+-536 -212 0
+-537 -213 0
+-538 -214 0
+-539 -215 0
+-540 -216 0
+-541 -217 0
+-542 -218 0
+-543 -219 0
+-544 -220 0
+-545 -221 0
+-546 -222 0
+-547 -223 0
+-548 -224 0
+-549 -225 0
+-550 -226 0
+-551 -227 0
+-552 -228 0
+-553 -229 0
+-554 -230 0
+-555 -231 0
+-556 -232 0
+-557 -233 0
+-558 -234 0
+-559 -235 0
+-560 -236 0
+-561 -237 0
+-562 -238 0
+-563 -239 0
+-564 -240 0
+-565 -241 0
+-566 -242 0
+-567 -243 0
+-487 -244 0
+-488 -245 0
+-489 -246 0
+-490 -247 0
+-491 -248 0
+-492 -249 0
+-493 -250 0
+-494 -251 0
+-495 -252 0
+-496 -253 0
+-497 -254 0
+-498 -255 0
+-499 -256 0
+-500 -257 0
+-501 -258 0
+-502 -259 0
+-503 -260 0
+-504 -261 0
+-505 -262 0
+-506 -263 0
+-507 -264 0
+-508 -265 0
+-509 -266 0
+-510 -267 0
+-511 -268 0
+-512 -269 0
+-513 -270 0
+-514 -271 0
+-515 -272 0
+-516 -273 0
+-517 -274 0
+-518 -275 0
+-519 -276 0
+-520 -277 0
+-521 -278 0
+-522 -279 0
+-523 -280 0
+-524 -281 0
+-525 -282 0
+-526 -283 0
+-527 -284 0
+-528 -285 0
+-529 -286 0
+-530 -287 0
+-531 -288 0
+-532 -289 0
+-533 -290 0
+-534 -291 0
+-535 -292 0
+-536 -293 0
+-537 -294 0
+-538 -295 0
+-539 -296 0
+-540 -297 0
+-541 -298 0
+-542 -299 0
+-543 -300 0
+-544 -301 0
+-545 -302 0
+-546 -303 0
+-547 -304 0
+-548 -305 0
+-549 -306 0
+-550 -307 0
+-551 -308 0
+-552 -309 0
+-553 -310 0
+-554 -311 0
+-555 -312 0
+-556 -313 0
+-557 -314 0
+-558 -315 0
+-559 -316 0
+-560 -317 0
+-561 -318 0
+-562 -319 0
+-563 -320 0
+-564 -321 0
+-565 -322 0
+-566 -323 0
+-567 -324 0
+-487 -325 0
+-488 -326 0
+-489 -327 0
+-490 -328 0
+-491 -329 0
+-492 -330 0
+-493 -331 0
+-494 -332 0
+-495 -333 0
+-496 -334 0
+-497 -335 0
+-498 -336 0
+-499 -337 0
+-500 -338 0
+-501 -339 0
+-502 -340 0
+-503 -341 0
+-504 -342 0
+-505 -343 0
+-506 -344 0
+-507 -345 0
+-508 -346 0
+-509 -347 0
+-510 -348 0
+-511 -349 0
+-512 -350 0
+-513 -351 0
+-514 -352 0
+-515 -353 0
+-516 -354 0
+-517 -355 0
+-518 -356 0
+-519 -357 0
+-520 -358 0
+-521 -359 0
+-522 -360 0
+-523 -361 0
+-524 -362 0
+-525 -363 0
+-526 -364 0
+-527 -365 0
+-528 -366 0
+-529 -367 0
+-530 -368 0
+-531 -369 0
+-532 -370 0
+-533 -371 0
+-534 -372 0
+-535 -373 0
+-536 -374 0
+-537 -375 0
+-538 -376 0
+-539 -377 0
+-540 -378 0
+-541 -379 0
+-542 -380 0
+-543 -381 0
+-544 -382 0
+-545 -383 0
+-546 -384 0
+-547 -385 0
+-548 -386 0
+-549 -387 0
+-550 -388 0
+-551 -389 0
+-552 -390 0
+-553 -391 0
+-554 -392 0
+-555 -393 0
+-556 -394 0
+-557 -395 0
+-558 -396 0
+-559 -397 0
+-560 -398 0
+-561 -399 0
+-562 -400 0
+-563 -401 0
+-564 -402 0
+-565 -403 0
+-566 -404 0
+-567 -405 0
+-487 -406 0
+-488 -407 0
+-489 -408 0
+-490 -409 0
+-491 -410 0
+-492 -411 0
+-493 -412 0
+-494 -413 0
+-495 -414 0
+-496 -415 0
+-497 -416 0
+-498 -417 0
+-499 -418 0
+-500 -419 0
+-501 -420 0
+-502 -421 0
+-503 -422 0
+-504 -423 0
+-505 -424 0
+-506 -425 0
+-507 -426 0
+-508 -427 0
+-509 -428 0
+-510 -429 0
+-511 -430 0
+-512 -431 0
+-513 -432 0
+-514 -433 0
+-515 -434 0
+-516 -435 0
+-517 -436 0
+-518 -437 0
+-519 -438 0
+-520 -439 0
+-521 -440 0
+-522 -441 0
+-523 -442 0
+-524 -443 0
+-525 -444 0
+-526 -445 0
+-527 -446 0
+-528 -447 0
+-529 -448 0
+-530 -449 0
+-531 -450 0
+-532 -451 0
+-533 -452 0
+-534 -453 0
+-535 -454 0
+-536 -455 0
+-537 -456 0
+-538 -457 0
+-539 -458 0
+-540 -459 0
+-541 -460 0
+-542 -461 0
+-543 -462 0
+-544 -463 0
+-545 -464 0
+-546 -465 0
+-547 -466 0
+-548 -467 0
+-549 -468 0
+-550 -469 0
+-551 -470 0
+-552 -471 0
+-553 -472 0
+-554 -473 0
+-555 -474 0
+-556 -475 0
+-557 -476 0
+-558 -477 0
+-559 -478 0
+-560 -479 0
+-561 -480 0
+-562 -481 0
+-563 -482 0
+-564 -483 0
+-565 -484 0
+-566 -485 0
+-567 -486 0
+-487 -568 0
+-488 -569 0
+-489 -570 0
+-490 -571 0
+-491 -572 0
+-492 -573 0
+-493 -574 0
+-494 -575 0
+-495 -576 0
+-496 -577 0
+-497 -578 0
+-498 -579 0
+-499 -580 0
+-500 -581 0
+-501 -582 0
+-502 -583 0
+-503 -584 0
+-504 -585 0
+-505 -586 0
+-506 -587 0
+-507 -588 0
+-508 -589 0
+-509 -590 0
+-510 -591 0
+-511 -592 0
+-512 -593 0
+-513 -594 0
+-514 -595 0
+-515 -596 0
+-516 -597 0
+-517 -598 0
+-518 -599 0
+-519 -600 0
+-520 -601 0
+-521 -602 0
+-522 -603 0
+-523 -604 0
+-524 -605 0
+-525 -606 0
+-526 -607 0
+-527 -608 0
+-528 -609 0
+-529 -610 0
+-530 -611 0
+-531 -612 0
+-532 -613 0
+-533 -614 0
+-534 -615 0
+-535 -616 0
+-536 -617 0
+-537 -618 0
+-538 -619 0
+-539 -620 0
+-540 -621 0
+-541 -622 0
+-542 -623 0
+-543 -624 0
+-544 -625 0
+-545 -626 0
+-546 -627 0
+-547 -628 0
+-548 -629 0
+-549 -630 0
+-550 -631 0
+-551 -632 0
+-552 -633 0
+-553 -634 0
+-554 -635 0
+-555 -636 0
+-556 -637 0
+-557 -638 0
+-558 -639 0
+-559 -640 0
+-560 -641 0
+-561 -642 0
+-562 -643 0
+-563 -644 0
+-564 -645 0
+-565 -646 0
+-566 -647 0
+-567 -648 0
+-487 -649 0
+-488 -650 0
+-489 -651 0
+-490 -652 0
+-491 -653 0
+-492 -654 0
+-493 -655 0
+-494 -656 0
+-495 -657 0
+-496 -658 0
+-497 -659 0
+-498 -660 0
+-499 -661 0
+-500 -662 0
+-501 -663 0
+-502 -664 0
+-503 -665 0
+-504 -666 0
+-505 -667 0
+-506 -668 0
+-507 -669 0
+-508 -670 0
+-509 -671 0
+-510 -672 0
+-511 -673 0
+-512 -674 0
+-513 -675 0
+-514 -676 0
+-515 -677 0
+-516 -678 0
+-517 -679 0
+-518 -680 0
+-519 -681 0
+-520 -682 0
+-521 -683 0
+-522 -684 0
+-523 -685 0
+-524 -686 0
+-525 -687 0
+-526 -688 0
+-527 -689 0
+-528 -690 0
+-529 -691 0
+-530 -692 0
+-531 -693 0
+-532 -694 0
+-533 -695 0
+-534 -696 0
+-535 -697 0
+-536 -698 0
+-537 -699 0
+-538 -700 0
+-539 -701 0
+-540 -702 0
+-541 -703 0
+-542 -704 0
+-543 -705 0
+-544 -706 0
+-545 -707 0
+-546 -708 0
+-547 -709 0
+-548 -710 0
+-549 -711 0
+-550 -712 0
+-551 -713 0
+-552 -714 0
+-553 -715 0
+-554 -716 0
+-555 -717 0
+-556 -718 0
+-557 -719 0
+-558 -720 0
+-559 -721 0
+-560 -722 0
+-561 -723 0
+-562 -724 0
+-563 -725 0
+-564 -726 0
+-565 -727 0
+-566 -728 0
+-567 -729 0
+-568 -1 0
+-569 -2 0
+-570 -3 0
+-571 -4 0
+-572 -5 0
+-573 -6 0
+-574 -7 0
+-575 -8 0
+-576 -9 0
+-577 -10 0
+-578 -11 0
+-579 -12 0
+-580 -13 0
+-581 -14 0
+-582 -15 0
+-583 -16 0
+-584 -17 0
+-585 -18 0
+-586 -19 0
+-587 -20 0
+-588 -21 0
+-589 -22 0
+-590 -23 0
+-591 -24 0
+-592 -25 0
+-593 -26 0
+-594 -27 0
+-595 -28 0
+-596 -29 0
+-597 -30 0
+-598 -31 0
+-599 -32 0
+-600 -33 0
+-601 -34 0
+-602 -35 0
+-603 -36 0
+-604 -37 0
+-605 -38 0
+-606 -39 0
+-607 -40 0
+-608 -41 0
+-609 -42 0
+-610 -43 0
+-611 -44 0
+-612 -45 0
+-613 -46 0
+-614 -47 0
+-615 -48 0
+-616 -49 0
+-617 -50 0
+-618 -51 0
+-619 -52 0
+-620 -53 0
+-621 -54 0
+-622 -55 0
+-623 -56 0
+-624 -57 0
+-625 -58 0
+-626 -59 0
+-627 -60 0
+-628 -61 0
+-629 -62 0
+-630 -63 0
+-631 -64 0
+-632 -65 0
+-633 -66 0
+-634 -67 0
+-635 -68 0
+-636 -69 0
+-637 -70 0
+-638 -71 0
+-639 -72 0
+-640 -73 0
+-641 -74 0
+-642 -75 0
+-643 -76 0
+-644 -77 0
+-645 -78 0
+-646 -79 0
+-647 -80 0
+-648 -81 0
+-568 -82 0
+-569 -83 0
+-570 -84 0
+-571 -85 0
+-572 -86 0
+-573 -87 0
+-574 -88 0
+-575 -89 0
+-576 -90 0
+-577 -91 0
+-578 -92 0
+-579 -93 0
+-580 -94 0
+-581 -95 0
+-582 -96 0
+-583 -97 0
+-584 -98 0
+-585 -99 0
+-586 -100 0
+-587 -101 0
+-588 -102 0
+-589 -103 0
+-590 -104 0
+-591 -105 0
+-592 -106 0
+-593 -107 0
+-594 -108 0
+-595 -109 0
+-596 -110 0
+-597 -111 0
+-598 -112 0
+-599 -113 0
+-600 -114 0
+-601 -115 0
+-602 -116 0
+-603 -117 0
+-604 -118 0
+-605 -119 0
+-606 -120 0
+-607 -121 0
+-608 -122 0
+-609 -123 0
+-610 -124 0
+-611 -125 0
+-612 -126 0
+-613 -127 0
+-614 -128 0
+-615 -129 0
+-616 -130 0
+-617 -131 0
+-618 -132 0
+-619 -133 0
+-620 -134 0
+-621 -135 0
+-622 -136 0
+-623 -137 0
+-624 -138 0
+-625 -139 0
+-626 -140 0
+-627 -141 0
+-628 -142 0
+-629 -143 0
+-630 -144 0
+-631 -145 0
+-632 -146 0
+-633 -147 0
+-634 -148 0
+-635 -149 0
+-636 -150 0
+-637 -151 0
+-638 -152 0
+-639 -153 0
+-640 -154 0
+-641 -155 0
+-642 -156 0
+-643 -157 0
+-644 -158 0
+-645 -159 0
+-646 -160 0
+-647 -161 0
+-648 -162 0
+-568 -163 0
+-569 -164 0
+-570 -165 0
+-571 -166 0
+-572 -167 0
+-573 -168 0
+-574 -169 0
+-575 -170 0
+-576 -171 0
+-577 -172 0
+-578 -173 0
+-579 -174 0
+-580 -175 0
+-581 -176 0
+-582 -177 0
+-583 -178 0
+-584 -179 0
+-585 -180 0
+-586 -181 0
+-587 -182 0
+-588 -183 0
+-589 -184 0
+-590 -185 0
+-591 -186 0
+-592 -187 0
+-593 -188 0
+-594 -189 0
+-595 -190 0
+-596 -191 0
+-597 -192 0
+-598 -193 0
+-599 -194 0
+-600 -195 0
+-601 -196 0
+-602 -197 0
+-603 -198 0
+-604 -199 0
+-605 -200 0
+-606 -201 0
+-607 -202 0
+-608 -203 0
+-609 -204 0
+-610 -205 0
+-611 -206 0
+-612 -207 0
+-613 -208 0
+-614 -209 0
+-615 -210 0
+-616 -211 0
+-617 -212 0
+-618 -213 0
+-619 -214 0
+-620 -215 0
+-621 -216 0
+-622 -217 0
+-623 -218 0
+-624 -219 0
+-625 -220 0
+-626 -221 0
+-627 -222 0
+-628 -223 0
+-629 -224 0
+-630 -225 0
+-631 -226 0
+-632 -227 0
+-633 -228 0
+-634 -229 0
+-635 -230 0
+-636 -231 0
+-637 -232 0
+-638 -233 0
+-639 -234 0
+-640 -235 0
+-641 -236 0
+-642 -237 0
+-643 -238 0
+-644 -239 0
+-645 -240 0
+-646 -241 0
+-647 -242 0
+-648 -243 0
+-568 -244 0
+-569 -245 0
+-570 -246 0
+-571 -247 0
+-572 -248 0
+-573 -249 0
+-574 -250 0
+-575 -251 0
+-576 -252 0
+-577 -253 0
+-578 -254 0
+-579 -255 0
+-580 -256 0
+-581 -257 0
+-582 -258 0
+-583 -259 0
+-584 -260 0
+-585 -261 0
+-586 -262 0
+-587 -263 0
+-588 -264 0
+-589 -265 0
+-590 -266 0
+-591 -267 0
+-592 -268 0
+-593 -269 0
+-594 -270 0
+-595 -271 0
+-596 -272 0
+-597 -273 0
+-598 -274 0
+-599 -275 0
+-600 -276 0
+-601 -277 0
+-602 -278 0
+-603 -279 0
+-604 -280 0
+-605 -281 0
+-606 -282 0
+-607 -283 0
+-608 -284 0
+-609 -285 0
+-610 -286 0
+-611 -287 0
+-612 -288 0
+-613 -289 0
+-614 -290 0
+-615 -291 0
+-616 -292 0
+-617 -293 0
+-618 -294 0
+-619 -295 0
+-620 -296 0
+-621 -297 0
+-622 -298 0
+-623 -299 0
+-624 -300 0
+-625 -301 0
+-626 -302 0
+-627 -303 0
+-628 -304 0
+-629 -305 0
+-630 -306 0
+-631 -307 0
+-632 -308 0
+-633 -309 0
+-634 -310 0
+-635 -311 0
+-636 -312 0
+-637 -313 0
+-638 -314 0
+-639 -315 0
+-640 -316 0
+-641 -317 0
+-642 -318 0
+-643 -319 0
+-644 -320 0
+-645 -321 0
+-646 -322 0
+-647 -323 0
+-648 -324 0
+-568 -325 0
+-569 -326 0
+-570 -327 0
+-571 -328 0
+-572 -329 0
+-573 -330 0
+-574 -331 0
+-575 -332 0
+-576 -333 0
+-577 -334 0
+-578 -335 0
+-579 -336 0
+-580 -337 0
+-581 -338 0
+-582 -339 0
+-583 -340 0
+-584 -341 0
+-585 -342 0
+-586 -343 0
+-587 -344 0
+-588 -345 0
+-589 -346 0
+-590 -347 0
+-591 -348 0
+-592 -349 0
+-593 -350 0
+-594 -351 0
+-595 -352 0
+-596 -353 0
+-597 -354 0
+-598 -355 0
+-599 -356 0
+-600 -357 0
+-601 -358 0
+-602 -359 0
+-603 -360 0
+-604 -361 0
+-605 -362 0
+-606 -363 0
+-607 -364 0
+-608 -365 0
+-609 -366 0
+-610 -367 0
+-611 -368 0
+-612 -369 0
+-613 -370 0
+-614 -371 0
+-615 -372 0
+-616 -373 0
+-617 -374 0
+-618 -375 0
+-619 -376 0
+-620 -377 0
+-621 -378 0
+-622 -379 0
+-623 -380 0
+-624 -381 0
+-625 -382 0
+-626 -383 0
+-627 -384 0
+-628 -385 0
+-629 -386 0
+-630 -387 0
+-631 -388 0
+-632 -389 0
+-633 -390 0
+-634 -391 0
+-635 -392 0
+-636 -393 0
+-637 -394 0
+-638 -395 0
+-639 -396 0
+-640 -397 0
+-641 -398 0
+-642 -399 0
+-643 -400 0
+-644 -401 0
+-645 -402 0
+-646 -403 0
+-647 -404 0
+-648 -405 0
+-568 -406 0
+-569 -407 0
+-570 -408 0
+-571 -409 0
+-572 -410 0
+-573 -411 0
+-574 -412 0
+-575 -413 0
+-576 -414 0
+-577 -415 0
+-578 -416 0
+-579 -417 0
+-580 -418 0
+-581 -419 0
+-582 -420 0
+-583 -421 0
+-584 -422 0
+-585 -423 0
+-586 -424 0
+-587 -425 0
+-588 -426 0
+-589 -427 0
+-590 -428 0
+-591 -429 0
+-592 -430 0
+-593 -431 0
+-594 -432 0
+-595 -433 0
+-596 -434 0
+-597 -435 0
+-598 -436 0
+-599 -437 0
+-600 -438 0
+-601 -439 0
+-602 -440 0
+-603 -441 0
+-604 -442 0
+-605 -443 0
+-606 -444 0
+-607 -445 0
+-608 -446 0
+-609 -447 0
+-610 -448 0
+-611 -449 0
+-612 -450 0
+-613 -451 0
+-614 -452 0
+-615 -453 0
+-616 -454 0
+-617 -455 0
+-618 -456 0
+-619 -457 0
+-620 -458 0
+-621 -459 0
+-622 -460 0
+-623 -461 0
+-624 -462 0
+-625 -463 0
+-626 -464 0
+-627 -465 0
+-628 -466 0
+-629 -467 0
+-630 -468 0
+-631 -469 0
+-632 -470 0
+-633 -471 0
+-634 -472 0
+-635 -473 0
+-636 -474 0
+-637 -475 0
+-638 -476 0
+-639 -477 0
+-640 -478 0
+-641 -479 0
+-642 -480 0
+-643 -481 0
+-644 -482 0
+-645 -483 0
+-646 -484 0
+-647 -485 0
+-648 -486 0
+-568 -487 0
+-569 -488 0
+-570 -489 0
+-571 -490 0
+-572 -491 0
+-573 -492 0
+-574 -493 0
+-575 -494 0
+-576 -495 0
+-577 -496 0
+-578 -497 0
+-579 -498 0
+-580 -499 0
+-581 -500 0
+-582 -501 0
+-583 -502 0
+-584 -503 0
+-585 -504 0
+-586 -505 0
+-587 -506 0
+-588 -507 0
+-589 -508 0
+-590 -509 0
+-591 -510 0
+-592 -511 0
+-593 -512 0
+-594 -513 0
+-595 -514 0
+-596 -515 0
+-597 -516 0
+-598 -517 0
+-599 -518 0
+-600 -519 0
+-601 -520 0
+-602 -521 0
+-603 -522 0
+-604 -523 0
+-605 -524 0
+-606 -525 0
+-607 -526 0
+-608 -527 0
+-609 -528 0
+-610 -529 0
+-611 -530 0
+-612 -531 0
+-613 -532 0
+-614 -533 0
+-615 -534 0
+-616 -535 0
+-617 -536 0
+-618 -537 0
+-619 -538 0
+-620 -539 0
+-621 -540 0
+-622 -541 0
+-623 -542 0
+-624 -543 0
+-625 -544 0
+-626 -545 0
+-627 -546 0
+-628 -547 0
+-629 -548 0
+-630 -549 0
+-631 -550 0
+-632 -551 0
+-633 -552 0
+-634 -553 0
+-635 -554 0
+-636 -555 0
+-637 -556 0
+-638 -557 0
+-639 -558 0
+-640 -559 0
+-641 -560 0
+-642 -561 0
+-643 -562 0
+-644 -563 0
+-645 -564 0
+-646 -565 0
+-647 -566 0
+-648 -567 0
+-568 -649 0
+-569 -650 0
+-570 -651 0
+-571 -652 0
+-572 -653 0
+-573 -654 0
+-574 -655 0
+-575 -656 0
+-576 -657 0
+-577 -658 0
+-578 -659 0
+-579 -660 0
+-580 -661 0
+-581 -662 0
+-582 -663 0
+-583 -664 0
+-584 -665 0
+-585 -666 0
+-586 -667 0
+-587 -668 0
+-588 -669 0
+-589 -670 0
+-590 -671 0
+-591 -672 0
+-592 -673 0
+-593 -674 0
+-594 -675 0
+-595 -676 0
+-596 -677 0
+-597 -678 0
+-598 -679 0
+-599 -680 0
+-600 -681 0
+-601 -682 0
+-602 -683 0
+-603 -684 0
+-604 -685 0
+-605 -686 0
+-606 -687 0
+-607 -688 0
+-608 -689 0
+-609 -690 0
+-610 -691 0
+-611 -692 0
+-612 -693 0
+-613 -694 0
+-614 -695 0
+-615 -696 0
+-616 -697 0
+-617 -698 0
+-618 -699 0
+-619 -700 0
+-620 -701 0
+-621 -702 0
+-622 -703 0
+-623 -704 0
+-624 -705 0
+-625 -706 0
+-626 -707 0
+-627 -708 0
+-628 -709 0
+-629 -710 0
+-630 -711 0
+-631 -712 0
+-632 -713 0
+-633 -714 0
+-634 -715 0
+-635 -716 0
+-636 -717 0
+-637 -718 0
+-638 -719 0
+-639 -720 0
+-640 -721 0
+-641 -722 0
+-642 -723 0
+-643 -724 0
+-644 -725 0
+-645 -726 0
+-646 -727 0
+-647 -728 0
+-648 -729 0
+-649 -1 0
+-650 -2 0
+-651 -3 0
+-652 -4 0
+-653 -5 0
+-654 -6 0
+-655 -7 0
+-656 -8 0
+-657 -9 0
+-658 -10 0
+-659 -11 0
+-660 -12 0
+-661 -13 0
+-662 -14 0
+-663 -15 0
+-664 -16 0
+-665 -17 0
+-666 -18 0
+-667 -19 0
+-668 -20 0
+-669 -21 0
+-670 -22 0
+-671 -23 0
+-672 -24 0
+-673 -25 0
+-674 -26 0
+-675 -27 0
+-676 -28 0
+-677 -29 0
+-678 -30 0
+-679 -31 0
+-680 -32 0
+-681 -33 0
+-682 -34 0
+-683 -35 0
+-684 -36 0
+-685 -37 0
+-686 -38 0
+-687 -39 0
+-688 -40 0
+-689 -41 0
+-690 -42 0
+-691 -43 0
+-692 -44 0
+-693 -45 0
+-694 -46 0
+-695 -47 0
+-696 -48 0
+-697 -49 0
+-698 -50 0
+-699 -51 0
+-700 -52 0
+-701 -53 0
+-702 -54 0
+-703 -55 0
+-704 -56 0
+-705 -57 0
+-706 -58 0
+-707 -59 0
+-708 -60 0
+-709 -61 0
+-710 -62 0
+-711 -63 0
+-712 -64 0
+-713 -65 0
+-714 -66 0
+-715 -67 0
+-716 -68 0
+-717 -69 0
+-718 -70 0
+-719 -71 0
+-720 -72 0
+-721 -73 0
+-722 -74 0
+-723 -75 0
+-724 -76 0
+-725 -77 0
+-726 -78 0
+-727 -79 0
+-728 -80 0
+-729 -81 0
+-649 -82 0
+-650 -83 0
+-651 -84 0
+-652 -85 0
+-653 -86 0
+-654 -87 0
+-655 -88 0
+-656 -89 0
+-657 -90 0
+-658 -91 0
+-659 -92 0
+-660 -93 0
+-661 -94 0
+-662 -95 0
+-663 -96 0
+-664 -97 0
+-665 -98 0
+-666 -99 0
+-667 -100 0
+-668 -101 0
+-669 -102 0
+-670 -103 0
+-671 -104 0
+-672 -105 0
+-673 -106 0
+-674 -107 0
+-675 -108 0
+-676 -109 0
+-677 -110 0
+-678 -111 0
+-679 -112 0
+-680 -113 0
+-681 -114 0
+-682 -115 0
+-683 -116 0
+-684 -117 0
+-685 -118 0
+-686 -119 0
+-687 -120 0
+-688 -121 0
+-689 -122 0
+-690 -123 0
+-691 -124 0
+-692 -125 0
+-693 -126 0
+-694 -127 0
+-695 -128 0
+-696 -129 0
+-697 -130 0
+-698 -131 0
+-699 -132 0
+-700 -133 0
+-701 -134 0
+-702 -135 0
+-703 -136 0
+-704 -137 0
+-705 -138 0
+-706 -139 0
+-707 -140 0
+-708 -141 0
+-709 -142 0
+-710 -143 0
+-711 -144 0
+-712 -145 0
+-713 -146 0
+-714 -147 0
+-715 -148 0
+-716 -149 0
+-717 -150 0
+-718 -151 0
+-719 -152 0
+-720 -153 0
+-721 -154 0
+-722 -155 0
+-723 -156 0
+-724 -157 0
+-725 -158 0
+-726 -159 0
+-727 -160 0
+-728 -161 0
+-729 -162 0
+-649 -163 0
+-650 -164 0
+-651 -165 0
+-652 -166 0
+-653 -167 0
+-654 -168 0
+-655 -169 0
+-656 -170 0
+-657 -171 0
+-658 -172 0
+-659 -173 0
+-660 -174 0
+-661 -175 0
+-662 -176 0
+-663 -177 0
+-664 -178 0
+-665 -179 0
+-666 -180 0
+-667 -181 0
+-668 -182 0
+-669 -183 0
+-670 -184 0
+-671 -185 0
+-672 -186 0
+-673 -187 0
+-674 -188 0
+-675 -189 0
+-676 -190 0
+-677 -191 0
+-678 -192 0
+-679 -193 0
+-680 -194 0
+-681 -195 0
+-682 -196 0
+-683 -197 0
+-684 -198 0
+-685 -199 0
+-686 -200 0
+-687 -201 0
+-688 -202 0
+-689 -203 0
+-690 -204 0
+-691 -205 0
+-692 -206 0
+-693 -207 0
+-694 -208 0
+-695 -209 0
+-696 -210 0
+-697 -211 0
+-698 -212 0
+-699 -213 0
+-700 -214 0
+-701 -215 0
+-702 -216 0
+-703 -217 0
+-704 -218 0
+-705 -219 0
+-706 -220 0
+-707 -221 0
+-708 -222 0
+-709 -223 0
+-710 -224 0
+-711 -225 0
+-712 -226 0
+-713 -227 0
+-714 -228 0
+-715 -229 0
+-716 -230 0
+-717 -231 0
+-718 -232 0
+-719 -233 0
+-720 -234 0
+-721 -235 0
+-722 -236 0
+-723 -237 0
+-724 -238 0
+-725 -239 0
+-726 -240 0
+-727 -241 0
+-728 -242 0
+-729 -243 0
+-649 -244 0
+-650 -245 0
+-651 -246 0
+-652 -247 0
+-653 -248 0
+-654 -249 0
+-655 -250 0
+-656 -251 0
+-657 -252 0
+-658 -253 0
+-659 -254 0
+-660 -255 0
+-661 -256 0
+-662 -257 0
+-663 -258 0
+-664 -259 0
+-665 -260 0
+-666 -261 0
+-667 -262 0
+-668 -263 0
+-669 -264 0
+-670 -265 0
+-671 -266 0
+-672 -267 0
+-673 -268 0
+-674 -269 0
+-675 -270 0
+-676 -271 0
+-677 -272 0
+-678 -273 0
+-679 -274 0
+-680 -275 0
+-681 -276 0
+-682 -277 0
+-683 -278 0
+-684 -279 0
+-685 -280 0
+-686 -281 0
+-687 -282 0
+-688 -283 0
+-689 -284 0
+-690 -285 0
+-691 -286 0
+-692 -287 0
+-693 -288 0
+-694 -289 0
+-695 -290 0
+-696 -291 0
+-697 -292 0
+-698 -293 0
+-699 -294 0
+-700 -295 0
+-701 -296 0
+-702 -297 0
+-703 -298 0
+-704 -299 0
+-705 -300 0
+-706 -301 0
+-707 -302 0
+-708 -303 0
+-709 -304 0
+-710 -305 0
+-711 -306 0
+-712 -307 0
+-713 -308 0
+-714 -309 0
+-715 -310 0
+-716 -311 0
+-717 -312 0
+-718 -313 0
+-719 -314 0
+-720 -315 0
+-721 -316 0
+-722 -317 0
+-723 -318 0
+-724 -319 0
+-725 -320 0
+-726 -321 0
+-727 -322 0
+-728 -323 0
+-729 -324 0
+-649 -325 0
+-650 -326 0
+-651 -327 0
+-652 -328 0
+-653 -329 0
+-654 -330 0
+-655 -331 0
+-656 -332 0
+-657 -333 0
+-658 -334 0
+-659 -335 0
+-660 -336 0
+-661 -337 0
+-662 -338 0
+-663 -339 0
+-664 -340 0
+-665 -341 0
+-666 -342 0
+-667 -343 0
+-668 -344 0
+-669 -345 0
+-670 -346 0
+-671 -347 0
+-672 -348 0
+-673 -349 0
+-674 -350 0
+-675 -351 0
+-676 -352 0
+-677 -353 0
+-678 -354 0
+-679 -355 0
+-680 -356 0
+-681 -357 0
+-682 -358 0
+-683 -359 0
+-684 -360 0
+-685 -361 0
+-686 -362 0
+-687 -363 0
+-688 -364 0
+-689 -365 0
+-690 -366 0
+-691 -367 0
+-692 -368 0
+-693 -369 0
+-694 -370 0
+-695 -371 0
+-696 -372 0
+-697 -373 0
+-698 -374 0
+-699 -375 0
+-700 -376 0
+-701 -377 0
+-702 -378 0
+-703 -379 0
+-704 -380 0
+-705 -381 0
+-706 -382 0
+-707 -383 0
+-708 -384 0
+-709 -385 0
+-710 -386 0
+-711 -387 0
+-712 -388 0
+-713 -389 0
+-714 -390 0
+-715 -391 0
+-716 -392 0
+-717 -393 0
+-718 -394 0
+-719 -395 0
+-720 -396 0
+-721 -397 0
+-722 -398 0
+-723 -399 0
+-724 -400 0
+-725 -401 0
+-726 -402 0
+-727 -403 0
+-728 -404 0
+-729 -405 0
+-649 -406 0
+-650 -407 0
+-651 -408 0
+-652 -409 0
+-653 -410 0
+-654 -411 0
+-655 -412 0
+-656 -413 0
+-657 -414 0
+-658 -415 0
+-659 -416 0
+-660 -417 0
+-661 -418 0
+-662 -419 0
+-663 -420 0
+-664 -421 0
+-665 -422 0
+-666 -423 0
+-667 -424 0
+-668 -425 0
+-669 -426 0
+-670 -427 0
+-671 -428 0
+-672 -429 0
+-673 -430 0
+-674 -431 0
+-675 -432 0
+-676 -433 0
+-677 -434 0
+-678 -435 0
+-679 -436 0
+-680 -437 0
+-681 -438 0
+-682 -439 0
+-683 -440 0
+-684 -441 0
+-685 -442 0
+-686 -443 0
+-687 -444 0
+-688 -445 0
+-689 -446 0
+-690 -447 0
+-691 -448 0
+-692 -449 0
+-693 -450 0
+-694 -451 0
+-695 -452 0
+-696 -453 0
+-697 -454 0
+-698 -455 0
+-699 -456 0
+-700 -457 0
+-701 -458 0
+-702 -459 0
+-703 -460 0
+-704 -461 0
+-705 -462 0
+-706 -463 0
+-707 -464 0
+-708 -465 0
+-709 -466 0
+-710 -467 0
+-711 -468 0
+-712 -469 0
+-713 -470 0
+-714 -471 0
+-715 -472 0
+-716 -473 0
+-717 -474 0
+-718 -475 0
+-719 -476 0
+-720 -477 0
+-721 -478 0
+-722 -479 0
+-723 -480 0
+-724 -481 0
+-725 -482 0
+-726 -483 0
+-727 -484 0
+-728 -485 0
+-729 -486 0
+-649 -487 0
+-650 -488 0
+-651 -489 0
+-652 -490 0
+-653 -491 0
+-654 -492 0
+-655 -493 0
+-656 -494 0
+-657 -495 0
+-658 -496 0
+-659 -497 0
+-660 -498 0
+-661 -499 0
+-662 -500 0
+-663 -501 0
+-664 -502 0
+-665 -503 0
+-666 -504 0
+-667 -505 0
+-668 -506 0
+-669 -507 0
+-670 -508 0
+-671 -509 0
+-672 -510 0
+-673 -511 0
+-674 -512 0
+-675 -513 0
+-676 -514 0
+-677 -515 0
+-678 -516 0
+-679 -517 0
+-680 -518 0
+-681 -519 0
+-682 -520 0
+-683 -521 0
+-684 -522 0
+-685 -523 0
+-686 -524 0
+-687 -525 0
+-688 -526 0
+-689 -527 0
+-690 -528 0
+-691 -529 0
+-692 -530 0
+-693 -531 0
+-694 -532 0
+-695 -533 0
+-696 -534 0
+-697 -535 0
+-698 -536 0
+-699 -537 0
+-700 -538 0
+-701 -539 0
+-702 -540 0
+-703 -541 0
+-704 -542 0
+-705 -543 0
+-706 -544 0
+-707 -545 0
+-708 -546 0
+-709 -547 0
+-710 -548 0
+-711 -549 0
+-712 -550 0
+-713 -551 0
+-714 -552 0
+-715 -553 0
+-716 -554 0
+-717 -555 0
+-718 -556 0
+-719 -557 0
+-720 -558 0
+-721 -559 0
+-722 -560 0
+-723 -561 0
+-724 -562 0
+-725 -563 0
+-726 -564 0
+-727 -565 0
+-728 -566 0
+-729 -567 0
+-649 -568 0
+-650 -569 0
+-651 -570 0
+-652 -571 0
+-653 -572 0
+-654 -573 0
+-655 -574 0
+-656 -575 0
+-657 -576 0
+-658 -577 0
+-659 -578 0
+-660 -579 0
+-661 -580 0
+-662 -581 0
+-663 -582 0
+-664 -583 0
+-665 -584 0
+-666 -585 0
+-667 -586 0
+-668 -587 0
+-669 -588 0
+-670 -589 0
+-671 -590 0
+-672 -591 0
+-673 -592 0
+-674 -593 0
+-675 -594 0
+-676 -595 0
+-677 -596 0
+-678 -597 0
+-679 -598 0
+-680 -599 0
+-681 -600 0
+-682 -601 0
+-683 -602 0
+-684 -603 0
+-685 -604 0
+-686 -605 0
+-687 -606 0
+-688 -607 0
+-689 -608 0
+-690 -609 0
+-691 -610 0
+-692 -611 0
+-693 -612 0
+-694 -613 0
+-695 -614 0
+-696 -615 0
+-697 -616 0
+-698 -617 0
+-699 -618 0
+-700 -619 0
+-701 -620 0
+-702 -621 0
+-703 -622 0
+-704 -623 0
+-705 -624 0
+-706 -625 0
+-707 -626 0
+-708 -627 0
+-709 -628 0
+-710 -629 0
+-711 -630 0
+-712 -631 0
+-713 -632 0
+-714 -633 0
+-715 -634 0
+-716 -635 0
+-717 -636 0
+-718 -637 0
+-719 -638 0
+-720 -639 0
+-721 -640 0
+-722 -641 0
+-723 -642 0
+-724 -643 0
+-725 -644 0
+-726 -645 0
+-727 -646 0
+-728 -647 0
+-729 -648 0
+-1 -91 0
+-2 -92 0
+-3 -93 0
+-4 -94 0
+-5 -95 0
+-6 -96 0
+-7 -97 0
+-8 -98 0
+-9 -99 0
+-1 -100 0
+-2 -101 0
+-3 -102 0
+-4 -103 0
+-5 -104 0
+-6 -105 0
+-7 -106 0
+-8 -107 0
+-9 -108 0
+-1 -172 0
+-2 -173 0
+-3 -174 0
+-4 -175 0
+-5 -176 0
+-6 -177 0
+-7 -178 0
+-8 -179 0
+-9 -180 0
+-1 -181 0
+-2 -182 0
+-3 -183 0
+-4 -184 0
+-5 -185 0
+-6 -186 0
+-7 -187 0
+-8 -188 0
+-9 -189 0
+-10 -82 0
+-11 -83 0
+-12 -84 0
+-13 -85 0
+-14 -86 0
+-15 -87 0
+-16 -88 0
+-17 -89 0
+-18 -90 0
+-10 -100 0
+-11 -101 0
+-12 -102 0
+-13 -103 0
+-14 -104 0
+-15 -105 0
+-16 -106 0
+-17 -107 0
+-18 -108 0
+-10 -163 0
+-11 -164 0
+-12 -165 0
+-13 -166 0
+-14 -167 0
+-15 -168 0
+-16 -169 0
+-17 -170 0
+-18 -171 0
+-10 -181 0
+-11 -182 0
+-12 -183 0
+-13 -184 0
+-14 -185 0
+-15 -186 0
+-16 -187 0
+-17 -188 0
+-18 -189 0
+-19 -82 0
+-20 -83 0
+-21 -84 0
+-22 -85 0
+-23 -86 0
+-24 -87 0
+-25 -88 0
+-26 -89 0
+-27 -90 0
+-19 -91 0
+-20 -92 0
+-21 -93 0
+-22 -94 0
+-23 -95 0
+-24 -96 0
+-25 -97 0
+-26 -98 0
+-27 -99 0
+-19 -163 0
+-20 -164 0
+-21 -165 0
+-22 -166 0
+-23 -167 0
+-24 -168 0
+-25 -169 0
+-26 -170 0
+-27 -171 0
+-19 -172 0
+-20 -173 0
+-21 -174 0
+-22 -175 0
+-23 -176 0
+-24 -177 0
+-25 -178 0
+-26 -179 0
+-27 -180 0
+-82 -10 0
+-83 -11 0
+-84 -12 0
+-85 -13 0
+-86 -14 0
+-87 -15 0
+-88 -16 0
+-89 -17 0
+-90 -18 0
+-82 -19 0
+-83 -20 0
+-84 -21 0
+-85 -22 0
+-86 -23 0
+-87 -24 0
+-88 -25 0
+-89 -26 0
+-90 -27 0
+-82 -172 0
+-83 -173 0
+-84 -174 0
+-85 -175 0
+-86 -176 0
+-87 -177 0
+-88 -178 0
+-89 -179 0
+-90 -180 0
+-82 -181 0
+-83 -182 0
+-84 -183 0
+-85 -184 0
+-86 -185 0
+-87 -186 0
+-88 -187 0
+-89 -188 0
+-90 -189 0
+-91 -1 0
+-92 -2 0
+-93 -3 0
+-94 -4 0
+-95 -5 0
+-96 -6 0
+-97 -7 0
+-98 -8 0
+-99 -9 0
+-91 -19 0
+-92 -20 0
+-93 -21 0
+-94 -22 0
+-95 -23 0
+-96 -24 0
+-97 -25 0
+-98 -26 0
+-99 -27 0
+-91 -163 0
+-92 -164 0
+-93 -165 0
+-94 -166 0
+-95 -167 0
+-96 -168 0
+-97 -169 0
+-98 -170 0
+-99 -171 0
+-91 -181 0
+-92 -182 0
+-93 -183 0
+-94 -184 0
+-95 -185 0
+-96 -186 0
+-97 -187 0
+-98 -188 0
+-99 -189 0
+-100 -1 0
+-101 -2 0
+-102 -3 0
+-103 -4 0
+-104 -5 0
+-105 -6 0
+-106 -7 0
+-107 -8 0
+-108 -9 0
+-100 -10 0
+-101 -11 0
+-102 -12 0
+-103 -13 0
+-104 -14 0
+-105 -15 0
+-106 -16 0
+-107 -17 0
+-108 -18 0
+-100 -163 0
+-101 -164 0
+-102 -165 0
+-103 -166 0
+-104 -167 0
+-105 -168 0
+-106 -169 0
+-107 -170 0
+-108 -171 0
+-100 -172 0
+-101 -173 0
+-102 -174 0
+-103 -175 0
+-104 -176 0
+-105 -177 0
+-106 -178 0
+-107 -179 0
+-108 -180 0
+-163 -10 0
+-164 -11 0
+-165 -12 0
+-166 -13 0
+-167 -14 0
+-168 -15 0
+-169 -16 0
+-170 -17 0
+-171 -18 0
+-163 -19 0
+-164 -20 0
+-165 -21 0
+-166 -22 0
+-167 -23 0
+-168 -24 0
+-169 -25 0
+-170 -26 0
+-171 -27 0
+-163 -91 0
+-164 -92 0
+-165 -93 0
+-166 -94 0
+-167 -95 0
+-168 -96 0
+-169 -97 0
+-170 -98 0
+-171 -99 0
+-163 -100 0
+-164 -101 0
+-165 -102 0
+-166 -103 0
+-167 -104 0
+-168 -105 0
+-169 -106 0
+-170 -107 0
+-171 -108 0
+-172 -1 0
+-173 -2 0
+-174 -3 0
+-175 -4 0
+-176 -5 0
+-177 -6 0
+-178 -7 0
+-179 -8 0
+-180 -9 0
+-172 -19 0
+-173 -20 0
+-174 -21 0
+-175 -22 0
+-176 -23 0
+-177 -24 0
+-178 -25 0
+-179 -26 0
+-180 -27 0
+-172 -82 0
+-173 -83 0
+-174 -84 0
+-175 -85 0
+-176 -86 0
+-177 -87 0
+-178 -88 0
+-179 -89 0
+-180 -90 0
+-172 -100 0
+-173 -101 0
+-174 -102 0
+-175 -103 0
+-176 -104 0
+-177 -105 0
+-178 -106 0
+-179 -107 0
+-180 -108 0
+-181 -1 0
+-182 -2 0
+-183 -3 0
+-184 -4 0
+-185 -5 0
+-186 -6 0
+-187 -7 0
+-188 -8 0
+-189 -9 0
+-181 -10 0
+-182 -11 0
+-183 -12 0
+-184 -13 0
+-185 -14 0
+-186 -15 0
+-187 -16 0
+-188 -17 0
+-189 -18 0
+-181 -82 0
+-182 -83 0
+-183 -84 0
+-184 -85 0
+-185 -86 0
+-186 -87 0
+-187 -88 0
+-188 -89 0
+-189 -90 0
+-181 -91 0
+-182 -92 0
+-183 -93 0
+-184 -94 0
+-185 -95 0
+-186 -96 0
+-187 -97 0
+-188 -98 0
+-189 -99 0
+-28 -118 0
+-29 -119 0
+-30 -120 0
+-31 -121 0
+-32 -122 0
+-33 -123 0
+-34 -124 0
+-35 -125 0
+-36 -126 0
+-28 -127 0
+-29 -128 0
+-30 -129 0
+-31 -130 0
+-32 -131 0
+-33 -132 0
+-34 -133 0
+-35 -134 0
+-36 -135 0
+-28 -199 0
+-29 -200 0
+-30 -201 0
+-31 -202 0
+-32 -203 0
+-33 -204 0
+-34 -205 0
+-35 -206 0
+-36 -207 0
+-28 -208 0
+-29 -209 0
+-30 -210 0
+-31 -211 0
+-32 -212 0
+-33 -213 0
+-34 -214 0
+-35 -215 0
+-36 -216 0
+-37 -109 0
+-38 -110 0
+-39 -111 0
+-40 -112 0
+-41 -113 0
+-42 -114 0
+-43 -115 0
+-44 -116 0
+-45 -117 0
+-37 -127 0
+-38 -128 0
+-39 -129 0
+-40 -130 0
+-41 -131 0
+-42 -132 0
+-43 -133 0
+-44 -134 0
+-45 -135 0
+-37 -190 0
+-38 -191 0
+-39 -192 0
+-40 -193 0
+-41 -194 0
+-42 -195 0
+-43 -196 0
+-44 -197 0
+-45 -198 0
+-37 -208 0
+-38 -209 0
+-39 -210 0
+-40 -211 0
+-41 -212 0
+-42 -213 0
+-43 -214 0
+-44 -215 0
+-45 -216 0
+-46 -109 0
+-47 -110 0
+-48 -111 0
+-49 -112 0
+-50 -113 0
+-51 -114 0
+-52 -115 0
+-53 -116 0
+-54 -117 0
+-46 -118 0
+-47 -119 0
+-48 -120 0
+-49 -121 0
+-50 -122 0
+-51 -123 0
+-52 -124 0
+-53 -125 0
+-54 -126 0
+-46 -190 0
+-47 -191 0
+-48 -192 0
+-49 -193 0
+-50 -194 0
+-51 -195 0
+-52 -196 0
+-53 -197 0
+-54 -198 0
+-46 -199 0
+-47 -200 0
+-48 -201 0
+-49 -202 0
+-50 -203 0
+-51 -204 0
+-52 -205 0
+-53 -206 0
+-54 -207 0
+-109 -37 0
+-110 -38 0
+-111 -39 0
+-112 -40 0
+-113 -41 0
+-114 -42 0
+-115 -43 0
+-116 -44 0
+-117 -45 0
+-109 -46 0
+-110 -47 0
+-111 -48 0
+-112 -49 0
+-113 -50 0
+-114 -51 0
+-115 -52 0
+-116 -53 0
+-117 -54 0
+-109 -199 0
+-110 -200 0
+-111 -201 0
+-112 -202 0
+-113 -203 0
+-114 -204 0
+-115 -205 0
+-116 -206 0
+-117 -207 0
+-109 -208 0
+-110 -209 0
+-111 -210 0
+-112 -211 0
+-113 -212 0
+-114 -213 0
+-115 -214 0
+-116 -215 0
+-117 -216 0
+-118 -28 0
+-119 -29 0
+-120 -30 0
+-121 -31 0
+-122 -32 0
+-123 -33 0
+-124 -34 0
+-125 -35 0
+-126 -36 0
+-118 -46 0
+-119 -47 0
+-120 -48 0
+-121 -49 0
+-122 -50 0
+-123 -51 0
+-124 -52 0
+-125 -53 0
+-126 -54 0
+-118 -190 0
+-119 -191 0
+-120 -192 0
+-121 -193 0
+-122 -194 0
+-123 -195 0
+-124 -196 0
+-125 -197 0
+-126 -198 0
+-118 -208 0
+-119 -209 0
+-120 -210 0
+-121 -211 0
+-122 -212 0
+-123 -213 0
+-124 -214 0
+-125 -215 0
+-126 -216 0
+-127 -28 0
+-128 -29 0
+-129 -30 0
+-130 -31 0
+-131 -32 0
+-132 -33 0
+-133 -34 0
+-134 -35 0
+-135 -36 0
+-127 -37 0
+-128 -38 0
+-129 -39 0
+-130 -40 0
+-131 -41 0
+-132 -42 0
+-133 -43 0
+-134 -44 0
+-135 -45 0
+-127 -190 0
+-128 -191 0
+-129 -192 0
+-130 -193 0
+-131 -194 0
+-132 -195 0
+-133 -196 0
+-134 -197 0
+-135 -198 0
+-127 -199 0
+-128 -200 0
+-129 -201 0
+-130 -202 0
+-131 -203 0
+-132 -204 0
+-133 -205 0
+-134 -206 0
+-135 -207 0
+-190 -37 0
+-191 -38 0
+-192 -39 0
+-193 -40 0
+-194 -41 0
+-195 -42 0
+-196 -43 0
+-197 -44 0
+-198 -45 0
+-190 -46 0
+-191 -47 0
+-192 -48 0
+-193 -49 0
+-194 -50 0
+-195 -51 0
+-196 -52 0
+-197 -53 0
+-198 -54 0
+-190 -118 0
+-191 -119 0
+-192 -120 0
+-193 -121 0
+-194 -122 0
+-195 -123 0
+-196 -124 0
+-197 -125 0
+-198 -126 0
+-190 -127 0
+-191 -128 0
+-192 -129 0
+-193 -130 0
+-194 -131 0
+-195 -132 0
+-196 -133 0
+-197 -134 0
+-198 -135 0
+-199 -28 0
+-200 -29 0
+-201 -30 0
+-202 -31 0
+-203 -32 0
+-204 -33 0
+-205 -34 0
+-206 -35 0
+-207 -36 0
+-199 -46 0
+-200 -47 0
+-201 -48 0
+-202 -49 0
+-203 -50 0
+-204 -51 0
+-205 -52 0
+-206 -53 0
+-207 -54 0
+-199 -109 0
+-200 -110 0
+-201 -111 0
+-202 -112 0
+-203 -113 0
+-204 -114 0
+-205 -115 0
+-206 -116 0
+-207 -117 0
+-199 -127 0
+-200 -128 0
+-201 -129 0
+-202 -130 0
+-203 -131 0
+-204 -132 0
+-205 -133 0
+-206 -134 0
+-207 -135 0
+-208 -28 0
+-209 -29 0
+-210 -30 0
+-211 -31 0
+-212 -32 0
+-213 -33 0
+-214 -34 0
+-215 -35 0
+-216 -36 0
+-208 -37 0
+-209 -38 0
+-210 -39 0
+-211 -40 0
+-212 -41 0
+-213 -42 0
+-214 -43 0
+-215 -44 0
+-216 -45 0
+-208 -109 0
+-209 -110 0
+-210 -111 0
+-211 -112 0
+-212 -113 0
+-213 -114 0
+-214 -115 0
+-215 -116 0
+-216 -117 0
+-208 -118 0
+-209 -119 0
+-210 -120 0
+-211 -121 0
+-212 -122 0
+-213 -123 0
+-214 -124 0
+-215 -125 0
+-216 -126 0
+-55 -145 0
+-56 -146 0
+-57 -147 0
+-58 -148 0
+-59 -149 0
+-60 -150 0
+-61 -151 0
+-62 -152 0
+-63 -153 0
+-55 -154 0
+-56 -155 0
+-57 -156 0
+-58 -157 0
+-59 -158 0
+-60 -159 0
+-61 -160 0
+-62 -161 0
+-63 -162 0
+-55 -226 0
+-56 -227 0
+-57 -228 0
+-58 -229 0
+-59 -230 0
+-60 -231 0
+-61 -232 0
+-62 -233 0
+-63 -234 0
+-55 -235 0
+-56 -236 0
+-57 -237 0
+-58 -238 0
+-59 -239 0
+-60 -240 0
+-61 -241 0
+-62 -242 0
+-63 -243 0
+-64 -136 0
+-65 -137 0
+-66 -138 0
+-67 -139 0
+-68 -140 0
+-69 -141 0
+-70 -142 0
+-71 -143 0
+-72 -144 0
+-64 -154 0
+-65 -155 0
+-66 -156 0
+-67 -157 0
+-68 -158 0
+-69 -159 0
+-70 -160 0
+-71 -161 0
+-72 -162 0
+-64 -217 0
+-65 -218 0
+-66 -219 0
+-67 -220 0
+-68 -221 0
+-69 -222 0
+-70 -223 0
+-71 -224 0
+-72 -225 0
+-64 -235 0
+-65 -236 0
+-66 -237 0
+-67 -238 0
+-68 -239 0
+-69 -240 0
+-70 -241 0
+-71 -242 0
+-72 -243 0
+-73 -136 0
+-74 -137 0
+-75 -138 0
+-76 -139 0
+-77 -140 0
+-78 -141 0
+-79 -142 0
+-80 -143 0
+-81 -144 0
+-73 -145 0
+-74 -146 0
+-75 -147 0
+-76 -148 0
+-77 -149 0
+-78 -150 0
+-79 -151 0
+-80 -152 0
+-81 -153 0
+-73 -217 0
+-74 -218 0
+-75 -219 0
+-76 -220 0
+-77 -221 0
+-78 -222 0
+-79 -223 0
+-80 -224 0
+-81 -225 0
+-73 -226 0
+-74 -227 0
+-75 -228 0
+-76 -229 0
+-77 -230 0
+-78 -231 0
+-79 -232 0
+-80 -233 0
+-81 -234 0
+-136 -64 0
+-137 -65 0
+-138 -66 0
+-139 -67 0
+-140 -68 0
+-141 -69 0
+-142 -70 0
+-143 -71 0
+-144 -72 0
+-136 -73 0
+-137 -74 0
+-138 -75 0
+-139 -76 0
+-140 -77 0
+-141 -78 0
+-142 -79 0
+-143 -80 0
+-144 -81 0
+-136 -226 0
+-137 -227 0
+-138 -228 0
+-139 -229 0
+-140 -230 0
+-141 -231 0
+-142 -232 0
+-143 -233 0
+-144 -234 0
+-136 -235 0
+-137 -236 0
+-138 -237 0
+-139 -238 0
+-140 -239 0
+-141 -240 0
+-142 -241 0
+-143 -242 0
+-144 -243 0
+-145 -55 0
+-146 -56 0
+-147 -57 0
+-148 -58 0
+-149 -59 0
+-150 -60 0
+-151 -61 0
+-152 -62 0
+-153 -63 0
+-145 -73 0
+-146 -74 0
+-147 -75 0
+-148 -76 0
+-149 -77 0
+-150 -78 0
+-151 -79 0
+-152 -80 0
+-153 -81 0
+-145 -217 0
+-146 -218 0
+-147 -219 0
+-148 -220 0
+-149 -221 0
+-150 -222 0
+-151 -223 0
+-152 -224 0
+-153 -225 0
+-145 -235 0
+-146 -236 0
+-147 -237 0
+-148 -238 0
+-149 -239 0
+-150 -240 0
+-151 -241 0
+-152 -242 0
+-153 -243 0
+-154 -55 0
+-155 -56 0
+-156 -57 0
+-157 -58 0
+-158 -59 0
+-159 -60 0
+-160 -61 0
+-161 -62 0
+-162 -63 0
+-154 -64 0
+-155 -65 0
+-156 -66 0
+-157 -67 0
+-158 -68 0
+-159 -69 0
+-160 -70 0
+-161 -71 0
+-162 -72 0
+-154 -217 0
+-155 -218 0
+-156 -219 0
+-157 -220 0
+-158 -221 0
+-159 -222 0
+-160 -223 0
+-161 -224 0
+-162 -225 0
+-154 -226 0
+-155 -227 0
+-156 -228 0
+-157 -229 0
+-158 -230 0
+-159 -231 0
+-160 -232 0
+-161 -233 0
+-162 -234 0
+-217 -64 0
+-218 -65 0
+-219 -66 0
+-220 -67 0
+-221 -68 0
+-222 -69 0
+-223 -70 0
+-224 -71 0
+-225 -72 0
+-217 -73 0
+-218 -74 0
+-219 -75 0
+-220 -76 0
+-221 -77 0
+-222 -78 0
+-223 -79 0
+-224 -80 0
+-225 -81 0
+-217 -145 0
+-218 -146 0
+-219 -147 0
+-220 -148 0
+-221 -149 0
+-222 -150 0
+-223 -151 0
+-224 -152 0
+-225 -153 0
+-217 -154 0
+-218 -155 0
+-219 -156 0
+-220 -157 0
+-221 -158 0
+-222 -159 0
+-223 -160 0
+-224 -161 0
+-225 -162 0
+-226 -55 0
+-227 -56 0
+-228 -57 0
+-229 -58 0
+-230 -59 0
+-231 -60 0
+-232 -61 0
+-233 -62 0
+-234 -63 0
+-226 -73 0
+-227 -74 0
+-228 -75 0
+-229 -76 0
+-230 -77 0
+-231 -78 0
+-232 -79 0
+-233 -80 0
+-234 -81 0
+-226 -136 0
+-227 -137 0
+-228 -138 0
+-229 -139 0
+-230 -140 0
+-231 -141 0
+-232 -142 0
+-233 -143 0
+-234 -144 0
+-226 -154 0
+-227 -155 0
+-228 -156 0
+-229 -157 0
+-230 -158 0
+-231 -159 0
+-232 -160 0
+-233 -161 0
+-234 -162 0
+-235 -55 0
+-236 -56 0
+-237 -57 0
+-238 -58 0
+-239 -59 0
+-240 -60 0
+-241 -61 0
+-242 -62 0
+-243 -63 0
+-235 -64 0
+-236 -65 0
+-237 -66 0
+-238 -67 0
+-239 -68 0
+-240 -69 0
+-241 -70 0
+-242 -71 0
+-243 -72 0
+-235 -136 0
+-236 -137 0
+-237 -138 0
+-238 -139 0
+-239 -140 0
+-240 -141 0
+-241 -142 0
+-242 -143 0
+-243 -144 0
+-235 -145 0
+-236 -146 0
+-237 -147 0
+-238 -148 0
+-239 -149 0
+-240 -150 0
+-241 -151 0
+-242 -152 0
+-243 -153 0
+-244 -334 0
+-245 -335 0
+-246 -336 0
+-247 -337 0
+-248 -338 0
+-249 -339 0
+-250 -340 0
+-251 -341 0
+-252 -342 0
+-244 -343 0
+-245 -344 0
+-246 -345 0
+-247 -346 0
+-248 -347 0
+-249 -348 0
+-250 -349 0
+-251 -350 0
+-252 -351 0
+-244 -415 0
+-245 -416 0
+-246 -417 0
+-247 -418 0
+-248 -419 0
+-249 -420 0
+-250 -421 0
+-251 -422 0
+-252 -423 0
+-244 -424 0
+-245 -425 0
+-246 -426 0
+-247 -427 0
+-248 -428 0
+-249 -429 0
+-250 -430 0
+-251 -431 0
+-252 -432 0
+-253 -325 0
+-254 -326 0
+-255 -327 0
+-256 -328 0
+-257 -329 0
+-258 -330 0
+-259 -331 0
+-260 -332 0
+-261 -333 0
+-253 -343 0
+-254 -344 0
+-255 -345 0
+-256 -346 0
+-257 -347 0
+-258 -348 0
+-259 -349 0
+-260 -350 0
+-261 -351 0
+-253 -406 0
+-254 -407 0
+-255 -408 0
+-256 -409 0
+-257 -410 0
+-258 -411 0
+-259 -412 0
+-260 -413 0
+-261 -414 0
+-253 -424 0
+-254 -425 0
+-255 -426 0
+-256 -427 0
+-257 -428 0
+-258 -429 0
+-259 -430 0
+-260 -431 0
+-261 -432 0
+-262 -325 0
+-263 -326 0
+-264 -327 0
+-265 -328 0
+-266 -329 0
+-267 -330 0
+-268 -331 0
+-269 -332 0
+-270 -333 0
+-262 -334 0
+-263 -335 0
+-264 -336 0
+-265 -337 0
+-266 -338 0
+-267 -339 0
+-268 -340 0
+-269 -341 0
+-270 -342 0
+-262 -406 0
+-263 -407 0
+-264 -408 0
+-265 -409 0
+-266 -410 0
+-267 -411 0
+-268 -412 0
+-269 -413 0
+-270 -414 0
+-262 -415 0
+-263 -416 0
+-264 -417 0
+-265 -418 0
+-266 -419 0
+-267 -420 0
+-268 -421 0
+-269 -422 0
+-270 -423 0
+-325 -253 0
+-326 -254 0
+-327 -255 0
+-328 -256 0
+-329 -257 0
+-330 -258 0
+-331 -259 0
+-332 -260 0
+-333 -261 0
+-325 -262 0
+-326 -263 0
+-327 -264 0
+-328 -265 0
+-329 -266 0
+-330 -267 0
+-331 -268 0
+-332 -269 0
+-333 -270 0
+-325 -415 0
+-326 -416 0
+-327 -417 0
+-328 -418 0
+-329 -419 0
+-330 -420 0
+-331 -421 0
+-332 -422 0
+-333 -423 0
+-325 -424 0
+-326 -425 0
+-327 -426 0
+-328 -427 0
+-329 -428 0
+-330 -429 0
+-331 -430 0
+-332 -431 0
+-333 -432 0
+-334 -244 0
+-335 -245 0
+-336 -246 0
+-337 -247 0
+-338 -248 0
+-339 -249 0
+-340 -250 0
+-341 -251 0
+-342 -252 0
+-334 -262 0
+-335 -263 0
+-336 -264 0
+-337 -265 0
+-338 -266 0
+-339 -267 0
+-340 -268 0
+-341 -269 0
+-342 -270 0
+-334 -406 0
+-335 -407 0
+-336 -408 0
+-337 -409 0
+-338 -410 0
+-339 -411 0
+-340 -412 0
+-341 -413 0
+-342 -414 0
+-334 -424 0
+-335 -425 0
+-336 -426 0
+-337 -427 0
+-338 -428 0
+-339 -429 0
+-340 -430 0
+-341 -431 0
+-342 -432 0
+-343 -244 0
+-344 -245 0
+-345 -246 0
+-346 -247 0
+-347 -248 0
+-348 -249 0
+-349 -250 0
+-350 -251 0
+-351 -252 0
+-343 -253 0
+-344 -254 0
+-345 -255 0
+-346 -256 0
+-347 -257 0
+-348 -258 0
+-349 -259 0
+-350 -260 0
+-351 -261 0
+-343 -406 0
+-344 -407 0
+-345 -408 0
+-346 -409 0
+-347 -410 0
+-348 -411 0
+-349 -412 0
+-350 -413 0
+-351 -414 0
+-343 -415 0
+-344 -416 0
+-345 -417 0
+-346 -418 0
+-347 -419 0
+-348 -420 0
+-349 -421 0
+-350 -422 0
+-351 -423 0
+-406 -253 0
+-407 -254 0
+-408 -255 0
+-409 -256 0
+-410 -257 0
+-411 -258 0
+-412 -259 0
+-413 -260 0
+-414 -261 0
+-406 -262 0
+-407 -263 0
+-408 -264 0
+-409 -265 0
+-410 -266 0
+-411 -267 0
+-412 -268 0
+-413 -269 0
+-414 -270 0
+-406 -334 0
+-407 -335 0
+-408 -336 0
+-409 -337 0
+-410 -338 0
+-411 -339 0
+-412 -340 0
+-413 -341 0
+-414 -342 0
+-406 -343 0
+-407 -344 0
+-408 -345 0
+-409 -346 0
+-410 -347 0
+-411 -348 0
+-412 -349 0
+-413 -350 0
+-414 -351 0
+-415 -244 0
+-416 -245 0
+-417 -246 0
+-418 -247 0
+-419 -248 0
+-420 -249 0
+-421 -250 0
+-422 -251 0
+-423 -252 0
+-415 -262 0
+-416 -263 0
+-417 -264 0
+-418 -265 0
+-419 -266 0
+-420 -267 0
+-421 -268 0
+-422 -269 0
+-423 -270 0
+-415 -325 0
+-416 -326 0
+-417 -327 0
+-418 -328 0
+-419 -329 0
+-420 -330 0
+-421 -331 0
+-422 -332 0
+-423 -333 0
+-415 -343 0
+-416 -344 0
+-417 -345 0
+-418 -346 0
+-419 -347 0
+-420 -348 0
+-421 -349 0
+-422 -350 0
+-423 -351 0
+-424 -244 0
+-425 -245 0
+-426 -246 0
+-427 -247 0
+-428 -248 0
+-429 -249 0
+-430 -250 0
+-431 -251 0
+-432 -252 0
+-424 -253 0
+-425 -254 0
+-426 -255 0
+-427 -256 0
+-428 -257 0
+-429 -258 0
+-430 -259 0
+-431 -260 0
+-432 -261 0
+-424 -325 0
+-425 -326 0
+-426 -327 0
+-427 -328 0
+-428 -329 0
+-429 -330 0
+-430 -331 0
+-431 -332 0
+-432 -333 0
+-424 -334 0
+-425 -335 0
+-426 -336 0
+-427 -337 0
+-428 -338 0
+-429 -339 0
+-430 -340 0
+-431 -341 0
+-432 -342 0
+-271 -361 0
+-272 -362 0
+-273 -363 0
+-274 -364 0
+-275 -365 0
+-276 -366 0
+-277 -367 0
+-278 -368 0
+-279 -369 0
+-271 -370 0
+-272 -371 0
+-273 -372 0
+-274 -373 0
+-275 -374 0
+-276 -375 0
+-277 -376 0
+-278 -377 0
+-279 -378 0
+-271 -442 0
+-272 -443 0
+-273 -444 0
+-274 -445 0
+-275 -446 0
+-276 -447 0
+-277 -448 0
+-278 -449 0
+-279 -450 0
+-271 -451 0
+-272 -452 0
+-273 -453 0
+-274 -454 0
+-275 -455 0
+-276 -456 0
+-277 -457 0
+-278 -458 0
+-279 -459 0
+-280 -352 0
+-281 -353 0
+-282 -354 0
+-283 -355 0
+-284 -356 0
+-285 -357 0
+-286 -358 0
+-287 -359 0
+-288 -360 0
+-280 -370 0
+-281 -371 0
+-282 -372 0
+-283 -373 0
+-284 -374 0
+-285 -375 0
+-286 -376 0
+-287 -377 0
+-288 -378 0
+-280 -433 0
+-281 -434 0
+-282 -435 0
+-283 -436 0
+-284 -437 0
+-285 -438 0
+-286 -439 0
+-287 -440 0
+-288 -441 0
+-280 -451 0
+-281 -452 0
+-282 -453 0
+-283 -454 0
+-284 -455 0
+-285 -456 0
+-286 -457 0
+-287 -458 0
+-288 -459 0
+-289 -352 0
+-290 -353 0
+-291 -354 0
+-292 -355 0
+-293 -356 0
+-294 -357 0
+-295 -358 0
+-296 -359 0
+-297 -360 0
+-289 -361 0
+-290 -362 0
+-291 -363 0
+-292 -364 0
+-293 -365 0
+-294 -366 0
+-295 -367 0
+-296 -368 0
+-297 -369 0
+-289 -433 0
+-290 -434 0
+-291 -435 0
+-292 -436 0
+-293 -437 0
+-294 -438 0
+-295 -439 0
+-296 -440 0
+-297 -441 0
+-289 -442 0
+-290 -443 0
+-291 -444 0
+-292 -445 0
+-293 -446 0
+-294 -447 0
+-295 -448 0
+-296 -449 0
+-297 -450 0
+-352 -280 0
+-353 -281 0
+-354 -282 0
+-355 -283 0
+-356 -284 0
+-357 -285 0
+-358 -286 0
+-359 -287 0
+-360 -288 0
+-352 -289 0
+-353 -290 0
+-354 -291 0
+-355 -292 0
+-356 -293 0
+-357 -294 0
+-358 -295 0
+-359 -296 0
+-360 -297 0
+-352 -442 0
+-353 -443 0
+-354 -444 0
+-355 -445 0
+-356 -446 0
+-357 -447 0
+-358 -448 0
+-359 -449 0
+-360 -450 0
+-352 -451 0
+-353 -452 0
+-354 -453 0
+-355 -454 0
+-356 -455 0
+-357 -456 0
+-358 -457 0
+-359 -458 0
+-360 -459 0
+-361 -271 0
+-362 -272 0
+-363 -273 0
+-364 -274 0
+-365 -275 0
+-366 -276 0
+-367 -277 0
+-368 -278 0
+-369 -279 0
+-361 -289 0
+-362 -290 0
+-363 -291 0
+-364 -292 0
+-365 -293 0
+-366 -294 0
+-367 -295 0
+-368 -296 0
+-369 -297 0
+-361 -433 0
+-362 -434 0
+-363 -435 0
+-364 -436 0
+-365 -437 0
+-366 -438 0
+-367 -439 0
+-368 -440 0
+-369 -441 0
+-361 -451 0
+-362 -452 0
+-363 -453 0
+-364 -454 0
+-365 -455 0
+-366 -456 0
+-367 -457 0
+-368 -458 0
+-369 -459 0
+-370 -271 0
+-371 -272 0
+-372 -273 0
+-373 -274 0
+-374 -275 0
+-375 -276 0
+-376 -277 0
+-377 -278 0
+-378 -279 0
+-370 -280 0
+-371 -281 0
+-372 -282 0
+-373 -283 0
+-374 -284 0
+-375 -285 0
+-376 -286 0
+-377 -287 0
+-378 -288 0
+-370 -433 0
+-371 -434 0
+-372 -435 0
+-373 -436 0
+-374 -437 0
+-375 -438 0
+-376 -439 0
+-377 -440 0
+-378 -441 0
+-370 -442 0
+-371 -443 0
+-372 -444 0
+-373 -445 0
+-374 -446 0
+-375 -447 0
+-376 -448 0
+-377 -449 0
+-378 -450 0
+-433 -280 0
+-434 -281 0
+-435 -282 0
+-436 -283 0
+-437 -284 0
+-438 -285 0
+-439 -286 0
+-440 -287 0
+-441 -288 0
+-433 -289 0
+-434 -290 0
+-435 -291 0
+-436 -292 0
+-437 -293 0
+-438 -294 0
+-439 -295 0
+-440 -296 0
+-441 -297 0
+-433 -361 0
+-434 -362 0
+-435 -363 0
+-436 -364 0
+-437 -365 0
+-438 -366 0
+-439 -367 0
+-440 -368 0
+-441 -369 0
+-433 -370 0
+-434 -371 0
+-435 -372 0
+-436 -373 0
+-437 -374 0
+-438 -375 0
+-439 -376 0
+-440 -377 0
+-441 -378 0
+-442 -271 0
+-443 -272 0
+-444 -273 0
+-445 -274 0
+-446 -275 0
+-447 -276 0
+-448 -277 0
+-449 -278 0
+-450 -279 0
+-442 -289 0
+-443 -290 0
+-444 -291 0
+-445 -292 0
+-446 -293 0
+-447 -294 0
+-448 -295 0
+-449 -296 0
+-450 -297 0
+-442 -352 0
+-443 -353 0
+-444 -354 0
+-445 -355 0
+-446 -356 0
+-447 -357 0
+-448 -358 0
+-449 -359 0
+-450 -360 0
+-442 -370 0
+-443 -371 0
+-444 -372 0
+-445 -373 0
+-446 -374 0
+-447 -375 0
+-448 -376 0
+-449 -377 0
+-450 -378 0
+-451 -271 0
+-452 -272 0
+-453 -273 0
+-454 -274 0
+-455 -275 0
+-456 -276 0
+-457 -277 0
+-458 -278 0
+-459 -279 0
+-451 -280 0
+-452 -281 0
+-453 -282 0
+-454 -283 0
+-455 -284 0
+-456 -285 0
+-457 -286 0
+-458 -287 0
+-459 -288 0
+-451 -352 0
+-452 -353 0
+-453 -354 0
+-454 -355 0
+-455 -356 0
+-456 -357 0
+-457 -358 0
+-458 -359 0
+-459 -360 0
+-451 -361 0
+-452 -362 0
+-453 -363 0
+-454 -364 0
+-455 -365 0
+-456 -366 0
+-457 -367 0
+-458 -368 0
+-459 -369 0
+-298 -388 0
+-299 -389 0
+-300 -390 0
+-301 -391 0
+-302 -392 0
+-303 -393 0
+-304 -394 0
+-305 -395 0
+-306 -396 0
+-298 -397 0
+-299 -398 0
+-300 -399 0
+-301 -400 0
+-302 -401 0
+-303 -402 0
+-304 -403 0
+-305 -404 0
+-306 -405 0
+-298 -469 0
+-299 -470 0
+-300 -471 0
+-301 -472 0
+-302 -473 0
+-303 -474 0
+-304 -475 0
+-305 -476 0
+-306 -477 0
+-298 -478 0
+-299 -479 0
+-300 -480 0
+-301 -481 0
+-302 -482 0
+-303 -483 0
+-304 -484 0
+-305 -485 0
+-306 -486 0
+-307 -379 0
+-308 -380 0
+-309 -381 0
+-310 -382 0
+-311 -383 0
+-312 -384 0
+-313 -385 0
+-314 -386 0
+-315 -387 0
+-307 -397 0
+-308 -398 0
+-309 -399 0
+-310 -400 0
+-311 -401 0
+-312 -402 0
+-313 -403 0
+-314 -404 0
+-315 -405 0
+-307 -460 0
+-308 -461 0
+-309 -462 0
+-310 -463 0
+-311 -464 0
+-312 -465 0
+-313 -466 0
+-314 -467 0
+-315 -468 0
+-307 -478 0
+-308 -479 0
+-309 -480 0
+-310 -481 0
+-311 -482 0
+-312 -483 0
+-313 -484 0
+-314 -485 0
+-315 -486 0
+-316 -379 0
+-317 -380 0
+-318 -381 0
+-319 -382 0
+-320 -383 0
+-321 -384 0
+-322 -385 0
+-323 -386 0
+-324 -387 0
+-316 -388 0
+-317 -389 0
+-318 -390 0
+-319 -391 0
+-320 -392 0
+-321 -393 0
+-322 -394 0
+-323 -395 0
+-324 -396 0
+-316 -460 0
+-317 -461 0
+-318 -462 0
+-319 -463 0
+-320 -464 0
+-321 -465 0
+-322 -466 0
+-323 -467 0
+-324 -468 0
+-316 -469 0
+-317 -470 0
+-318 -471 0
+-319 -472 0
+-320 -473 0
+-321 -474 0
+-322 -475 0
+-323 -476 0
+-324 -477 0
+-379 -307 0
+-380 -308 0
+-381 -309 0
+-382 -310 0
+-383 -311 0
+-384 -312 0
+-385 -313 0
+-386 -314 0
+-387 -315 0
+-379 -316 0
+-380 -317 0
+-381 -318 0
+-382 -319 0
+-383 -320 0
+-384 -321 0
+-385 -322 0
+-386 -323 0
+-387 -324 0
+-379 -469 0
+-380 -470 0
+-381 -471 0
+-382 -472 0
+-383 -473 0
+-384 -474 0
+-385 -475 0
+-386 -476 0
+-387 -477 0
+-379 -478 0
+-380 -479 0
+-381 -480 0
+-382 -481 0
+-383 -482 0
+-384 -483 0
+-385 -484 0
+-386 -485 0
+-387 -486 0
+-388 -298 0
+-389 -299 0
+-390 -300 0
+-391 -301 0
+-392 -302 0
+-393 -303 0
+-394 -304 0
+-395 -305 0
+-396 -306 0
+-388 -316 0
+-389 -317 0
+-390 -318 0
+-391 -319 0
+-392 -320 0
+-393 -321 0
+-394 -322 0
+-395 -323 0
+-396 -324 0
+-388 -460 0
+-389 -461 0
+-390 -462 0
+-391 -463 0
+-392 -464 0
+-393 -465 0
+-394 -466 0
+-395 -467 0
+-396 -468 0
+-388 -478 0
+-389 -479 0
+-390 -480 0
+-391 -481 0
+-392 -482 0
+-393 -483 0
+-394 -484 0
+-395 -485 0
+-396 -486 0
+-397 -298 0
+-398 -299 0
+-399 -300 0
+-400 -301 0
+-401 -302 0
+-402 -303 0
+-403 -304 0
+-404 -305 0
+-405 -306 0
+-397 -307 0
+-398 -308 0
+-399 -309 0
+-400 -310 0
+-401 -311 0
+-402 -312 0
+-403 -313 0
+-404 -314 0
+-405 -315 0
+-397 -460 0
+-398 -461 0
+-399 -462 0
+-400 -463 0
+-401 -464 0
+-402 -465 0
+-403 -466 0
+-404 -467 0
+-405 -468 0
+-397 -469 0
+-398 -470 0
+-399 -471 0
+-400 -472 0
+-401 -473 0
+-402 -474 0
+-403 -475 0
+-404 -476 0
+-405 -477 0
+-460 -307 0
+-461 -308 0
+-462 -309 0
+-463 -310 0
+-464 -311 0
+-465 -312 0
+-466 -313 0
+-467 -314 0
+-468 -315 0
+-460 -316 0
+-461 -317 0
+-462 -318 0
+-463 -319 0
+-464 -320 0
+-465 -321 0
+-466 -322 0
+-467 -323 0
+-468 -324 0
+-460 -388 0
+-461 -389 0
+-462 -390 0
+-463 -391 0
+-464 -392 0
+-465 -393 0
+-466 -394 0
+-467 -395 0
+-468 -396 0
+-460 -397 0
+-461 -398 0
+-462 -399 0
+-463 -400 0
+-464 -401 0
+-465 -402 0
+-466 -403 0
+-467 -404 0
+-468 -405 0
+-469 -298 0
+-470 -299 0
+-471 -300 0
+-472 -301 0
+-473 -302 0
+-474 -303 0
+-475 -304 0
+-476 -305 0
+-477 -306 0
+-469 -316 0
+-470 -317 0
+-471 -318 0
+-472 -319 0
+-473 -320 0
+-474 -321 0
+-475 -322 0
+-476 -323 0
+-477 -324 0
+-469 -379 0
+-470 -380 0
+-471 -381 0
+-472 -382 0
+-473 -383 0
+-474 -384 0
+-475 -385 0
+-476 -386 0
+-477 -387 0
+-469 -397 0
+-470 -398 0
+-471 -399 0
+-472 -400 0
+-473 -401 0
+-474 -402 0
+-475 -403 0
+-476 -404 0
+-477 -405 0
+-478 -298 0
+-479 -299 0
+-480 -300 0
+-481 -301 0
+-482 -302 0
+-483 -303 0
+-484 -304 0
+-485 -305 0
+-486 -306 0
+-478 -307 0
+-479 -308 0
+-480 -309 0
+-481 -310 0
+-482 -311 0
+-483 -312 0
+-484 -313 0
+-485 -314 0
+-486 -315 0
+-478 -379 0
+-479 -380 0
+-480 -381 0
+-481 -382 0
+-482 -383 0
+-483 -384 0
+-484 -385 0
+-485 -386 0
+-486 -387 0
+-478 -388 0
+-479 -389 0
+-480 -390 0
+-481 -391 0
+-482 -392 0
+-483 -393 0
+-484 -394 0
+-485 -395 0
+-486 -396 0
+-487 -577 0
+-488 -578 0
+-489 -579 0
+-490 -580 0
+-491 -581 0
+-492 -582 0
+-493 -583 0
+-494 -584 0
+-495 -585 0
+-487 -586 0
+-488 -587 0
+-489 -588 0
+-490 -589 0
+-491 -590 0
+-492 -591 0
+-493 -592 0
+-494 -593 0
+-495 -594 0
+-487 -658 0
+-488 -659 0
+-489 -660 0
+-490 -661 0
+-491 -662 0
+-492 -663 0
+-493 -664 0
+-494 -665 0
+-495 -666 0
+-487 -667 0
+-488 -668 0
+-489 -669 0
+-490 -670 0
+-491 -671 0
+-492 -672 0
+-493 -673 0
+-494 -674 0
+-495 -675 0
+-496 -568 0
+-497 -569 0
+-498 -570 0
+-499 -571 0
+-500 -572 0
+-501 -573 0
+-502 -574 0
+-503 -575 0
+-504 -576 0
+-496 -586 0
+-497 -587 0
+-498 -588 0
+-499 -589 0
+-500 -590 0
+-501 -591 0
+-502 -592 0
+-503 -593 0
+-504 -594 0
+-496 -649 0
+-497 -650 0
+-498 -651 0
+-499 -652 0
+-500 -653 0
+-501 -654 0
+-502 -655 0
+-503 -656 0
+-504 -657 0
+-496 -667 0
+-497 -668 0
+-498 -669 0
+-499 -670 0
+-500 -671 0
+-501 -672 0
+-502 -673 0
+-503 -674 0
+-504 -675 0
+-505 -568 0
+-506 -569 0
+-507 -570 0
+-508 -571 0
+-509 -572 0
+-510 -573 0
+-511 -574 0
+-512 -575 0
+-513 -576 0
+-505 -577 0
+-506 -578 0
+-507 -579 0
+-508 -580 0
+-509 -581 0
+-510 -582 0
+-511 -583 0
+-512 -584 0
+-513 -585 0
+-505 -649 0
+-506 -650 0
+-507 -651 0
+-508 -652 0
+-509 -653 0
+-510 -654 0
+-511 -655 0
+-512 -656 0
+-513 -657 0
+-505 -658 0
+-506 -659 0
+-507 -660 0
+-508 -661 0
+-509 -662 0
+-510 -663 0
+-511 -664 0
+-512 -665 0
+-513 -666 0
+-568 -496 0
+-569 -497 0
+-570 -498 0
+-571 -499 0
+-572 -500 0
+-573 -501 0
+-574 -502 0
+-575 -503 0
+-576 -504 0
+-568 -505 0
+-569 -506 0
+-570 -507 0
+-571 -508 0
+-572 -509 0
+-573 -510 0
+-574 -511 0
+-575 -512 0
+-576 -513 0
+-568 -658 0
+-569 -659 0
+-570 -660 0
+-571 -661 0
+-572 -662 0
+-573 -663 0
+-574 -664 0
+-575 -665 0
+-576 -666 0
+-568 -667 0
+-569 -668 0
+-570 -669 0
+-571 -670 0
+-572 -671 0
+-573 -672 0
+-574 -673 0
+-575 -674 0
+-576 -675 0
+-577 -487 0
+-578 -488 0
+-579 -489 0
+-580 -490 0
+-581 -491 0
+-582 -492 0
+-583 -493 0
+-584 -494 0
+-585 -495 0
+-577 -505 0
+-578 -506 0
+-579 -507 0
+-580 -508 0
+-581 -509 0
+-582 -510 0
+-583 -511 0
+-584 -512 0
+-585 -513 0
+-577 -649 0
+-578 -650 0
+-579 -651 0
+-580 -652 0
+-581 -653 0
+-582 -654 0
+-583 -655 0
+-584 -656 0
+-585 -657 0
+-577 -667 0
+-578 -668 0
+-579 -669 0
+-580 -670 0
+-581 -671 0
+-582 -672 0
+-583 -673 0
+-584 -674 0
+-585 -675 0
+-586 -487 0
+-587 -488 0
+-588 -489 0
+-589 -490 0
+-590 -491 0
+-591 -492 0
+-592 -493 0
+-593 -494 0
+-594 -495 0
+-586 -496 0
+-587 -497 0
+-588 -498 0
+-589 -499 0
+-590 -500 0
+-591 -501 0
+-592 -502 0
+-593 -503 0
+-594 -504 0
+-586 -649 0
+-587 -650 0
+-588 -651 0
+-589 -652 0
+-590 -653 0
+-591 -654 0
+-592 -655 0
+-593 -656 0
+-594 -657 0
+-586 -658 0
+-587 -659 0
+-588 -660 0
+-589 -661 0
+-590 -662 0
+-591 -663 0
+-592 -664 0
+-593 -665 0
+-594 -666 0
+-649 -496 0
+-650 -497 0
+-651 -498 0
+-652 -499 0
+-653 -500 0
+-654 -501 0
+-655 -502 0
+-656 -503 0
+-657 -504 0
+-649 -505 0
+-650 -506 0
+-651 -507 0
+-652 -508 0
+-653 -509 0
+-654 -510 0
+-655 -511 0
+-656 -512 0
+-657 -513 0
+-649 -577 0
+-650 -578 0
+-651 -579 0
+-652 -580 0
+-653 -581 0
+-654 -582 0
+-655 -583 0
+-656 -584 0
+-657 -585 0
+-649 -586 0
+-650 -587 0
+-651 -588 0
+-652 -589 0
+-653 -590 0
+-654 -591 0
+-655 -592 0
+-656 -593 0
+-657 -594 0
+-658 -487 0
+-659 -488 0
+-660 -489 0
+-661 -490 0
+-662 -491 0
+-663 -492 0
+-664 -493 0
+-665 -494 0
+-666 -495 0
+-658 -505 0
+-659 -506 0
+-660 -507 0
+-661 -508 0
+-662 -509 0
+-663 -510 0
+-664 -511 0
+-665 -512 0
+-666 -513 0
+-658 -568 0
+-659 -569 0
+-660 -570 0
+-661 -571 0
+-662 -572 0
+-663 -573 0
+-664 -574 0
+-665 -575 0
+-666 -576 0
+-658 -586 0
+-659 -587 0
+-660 -588 0
+-661 -589 0
+-662 -590 0
+-663 -591 0
+-664 -592 0
+-665 -593 0
+-666 -594 0
+-667 -487 0
+-668 -488 0
+-669 -489 0
+-670 -490 0
+-671 -491 0
+-672 -492 0
+-673 -493 0
+-674 -494 0
+-675 -495 0
+-667 -496 0
+-668 -497 0
+-669 -498 0
+-670 -499 0
+-671 -500 0
+-672 -501 0
+-673 -502 0
+-674 -503 0
+-675 -504 0
+-667 -568 0
+-668 -569 0
+-669 -570 0
+-670 -571 0
+-671 -572 0
+-672 -573 0
+-673 -574 0
+-674 -575 0
+-675 -576 0
+-667 -577 0
+-668 -578 0
+-669 -579 0
+-670 -580 0
+-671 -581 0
+-672 -582 0
+-673 -583 0
+-674 -584 0
+-675 -585 0
+-514 -604 0
+-515 -605 0
+-516 -606 0
+-517 -607 0
+-518 -608 0
+-519 -609 0
+-520 -610 0
+-521 -611 0
+-522 -612 0
+-514 -613 0
+-515 -614 0
+-516 -615 0
+-517 -616 0
+-518 -617 0
+-519 -618 0
+-520 -619 0
+-521 -620 0
+-522 -621 0
+-514 -685 0
+-515 -686 0
+-516 -687 0
+-517 -688 0
+-518 -689 0
+-519 -690 0
+-520 -691 0
+-521 -692 0
+-522 -693 0
+-514 -694 0
+-515 -695 0
+-516 -696 0
+-517 -697 0
+-518 -698 0
+-519 -699 0
+-520 -700 0
+-521 -701 0
+-522 -702 0
+-523 -595 0
+-524 -596 0
+-525 -597 0
+-526 -598 0
+-527 -599 0
+-528 -600 0
+-529 -601 0
+-530 -602 0
+-531 -603 0
+-523 -613 0
+-524 -614 0
+-525 -615 0
+-526 -616 0
+-527 -617 0
+-528 -618 0
+-529 -619 0
+-530 -620 0
+-531 -621 0
+-523 -676 0
+-524 -677 0
+-525 -678 0
+-526 -679 0
+-527 -680 0
+-528 -681 0
+-529 -682 0
+-530 -683 0
+-531 -684 0
+-523 -694 0
+-524 -695 0
+-525 -696 0
+-526 -697 0
+-527 -698 0
+-528 -699 0
+-529 -700 0
+-530 -701 0
+-531 -702 0
+-532 -595 0
+-533 -596 0
+-534 -597 0
+-535 -598 0
+-536 -599 0
+-537 -600 0
+-538 -601 0
+-539 -602 0
+-540 -603 0
+-532 -604 0
+-533 -605 0
+-534 -606 0
+-535 -607 0
+-536 -608 0
+-537 -609 0
+-538 -610 0
+-539 -611 0
+-540 -612 0
+-532 -676 0
+-533 -677 0
+-534 -678 0
+-535 -679 0
+-536 -680 0
+-537 -681 0
+-538 -682 0
+-539 -683 0
+-540 -684 0
+-532 -685 0
+-533 -686 0
+-534 -687 0
+-535 -688 0
+-536 -689 0
+-537 -690 0
+-538 -691 0
+-539 -692 0
+-540 -693 0
+-595 -523 0
+-596 -524 0
+-597 -525 0
+-598 -526 0
+-599 -527 0
+-600 -528 0
+-601 -529 0
+-602 -530 0
+-603 -531 0
+-595 -532 0
+-596 -533 0
+-597 -534 0
+-598 -535 0
+-599 -536 0
+-600 -537 0
+-601 -538 0
+-602 -539 0
+-603 -540 0
+-595 -685 0
+-596 -686 0
+-597 -687 0
+-598 -688 0
+-599 -689 0
+-600 -690 0
+-601 -691 0
+-602 -692 0
+-603 -693 0
+-595 -694 0
+-596 -695 0
+-597 -696 0
+-598 -697 0
+-599 -698 0
+-600 -699 0
+-601 -700 0
+-602 -701 0
+-603 -702 0
+-604 -514 0
+-605 -515 0
+-606 -516 0
+-607 -517 0
+-608 -518 0
+-609 -519 0
+-610 -520 0
+-611 -521 0
+-612 -522 0
+-604 -532 0
+-605 -533 0
+-606 -534 0
+-607 -535 0
+-608 -536 0
+-609 -537 0
+-610 -538 0
+-611 -539 0
+-612 -540 0
+-604 -676 0
+-605 -677 0
+-606 -678 0
+-607 -679 0
+-608 -680 0
+-609 -681 0
+-610 -682 0
+-611 -683 0
+-612 -684 0
+-604 -694 0
+-605 -695 0
+-606 -696 0
+-607 -697 0
+-608 -698 0
+-609 -699 0
+-610 -700 0
+-611 -701 0
+-612 -702 0
+-613 -514 0
+-614 -515 0
+-615 -516 0
+-616 -517 0
+-617 -518 0
+-618 -519 0
+-619 -520 0
+-620 -521 0
+-621 -522 0
+-613 -523 0
+-614 -524 0
+-615 -525 0
+-616 -526 0
+-617 -527 0
+-618 -528 0
+-619 -529 0
+-620 -530 0
+-621 -531 0
+-613 -676 0
+-614 -677 0
+-615 -678 0
+-616 -679 0
+-617 -680 0
+-618 -681 0
+-619 -682 0
+-620 -683 0
+-621 -684 0
+-613 -685 0
+-614 -686 0
+-615 -687 0
+-616 -688 0
+-617 -689 0
+-618 -690 0
+-619 -691 0
+-620 -692 0
+-621 -693 0
+-676 -523 0
+-677 -524 0
+-678 -525 0
+-679 -526 0
+-680 -527 0
+-681 -528 0
+-682 -529 0
+-683 -530 0
+-684 -531 0
+-676 -532 0
+-677 -533 0
+-678 -534 0
+-679 -535 0
+-680 -536 0
+-681 -537 0
+-682 -538 0
+-683 -539 0
+-684 -540 0
+-676 -604 0
+-677 -605 0
+-678 -606 0
+-679 -607 0
+-680 -608 0
+-681 -609 0
+-682 -610 0
+-683 -611 0
+-684 -612 0
+-676 -613 0
+-677 -614 0
+-678 -615 0
+-679 -616 0
+-680 -617 0
+-681 -618 0
+-682 -619 0
+-683 -620 0
+-684 -621 0
+-685 -514 0
+-686 -515 0
+-687 -516 0
+-688 -517 0
+-689 -518 0
+-690 -519 0
+-691 -520 0
+-692 -521 0
+-693 -522 0
+-685 -532 0
+-686 -533 0
+-687 -534 0
+-688 -535 0
+-689 -536 0
+-690 -537 0
+-691 -538 0
+-692 -539 0
+-693 -540 0
+-685 -595 0
+-686 -596 0
+-687 -597 0
+-688 -598 0
+-689 -599 0
+-690 -600 0
+-691 -601 0
+-692 -602 0
+-693 -603 0
+-685 -613 0
+-686 -614 0
+-687 -615 0
+-688 -616 0
+-689 -617 0
+-690 -618 0
+-691 -619 0
+-692 -620 0
+-693 -621 0
+-694 -514 0
+-695 -515 0
+-696 -516 0
+-697 -517 0
+-698 -518 0
+-699 -519 0
+-700 -520 0
+-701 -521 0
+-702 -522 0
+-694 -523 0
+-695 -524 0
+-696 -525 0
+-697 -526 0
+-698 -527 0
+-699 -528 0
+-700 -529 0
+-701 -530 0
+-702 -531 0
+-694 -595 0
+-695 -596 0
+-696 -597 0
+-697 -598 0
+-698 -599 0
+-699 -600 0
+-700 -601 0
+-701 -602 0
+-702 -603 0
+-694 -604 0
+-695 -605 0
+-696 -606 0
+-697 -607 0
+-698 -608 0
+-699 -609 0
+-700 -610 0
+-701 -611 0
+-702 -612 0
+-541 -631 0
+-542 -632 0
+-543 -633 0
+-544 -634 0
+-545 -635 0
+-546 -636 0
+-547 -637 0
+-548 -638 0
+-549 -639 0
+-541 -640 0
+-542 -641 0
+-543 -642 0
+-544 -643 0
+-545 -644 0
+-546 -645 0
+-547 -646 0
+-548 -647 0
+-549 -648 0
+-541 -712 0
+-542 -713 0
+-543 -714 0
+-544 -715 0
+-545 -716 0
+-546 -717 0
+-547 -718 0
+-548 -719 0
+-549 -720 0
+-541 -721 0
+-542 -722 0
+-543 -723 0
+-544 -724 0
+-545 -725 0
+-546 -726 0
+-547 -727 0
+-548 -728 0
+-549 -729 0
+-550 -622 0
+-551 -623 0
+-552 -624 0
+-553 -625 0
+-554 -626 0
+-555 -627 0
+-556 -628 0
+-557 -629 0
+-558 -630 0
+-550 -640 0
+-551 -641 0
+-552 -642 0
+-553 -643 0
+-554 -644 0
+-555 -645 0
+-556 -646 0
+-557 -647 0
+-558 -648 0
+-550 -703 0
+-551 -704 0
+-552 -705 0
+-553 -706 0
+-554 -707 0
+-555 -708 0
+-556 -709 0
+-557 -710 0
+-558 -711 0
+-550 -721 0
+-551 -722 0
+-552 -723 0
+-553 -724 0
+-554 -725 0
+-555 -726 0
+-556 -727 0
+-557 -728 0
+-558 -729 0
+-559 -622 0
+-560 -623 0
+-561 -624 0
+-562 -625 0
+-563 -626 0
+-564 -627 0
+-565 -628 0
+-566 -629 0
+-567 -630 0
+-559 -631 0
+-560 -632 0
+-561 -633 0
+-562 -634 0
+-563 -635 0
+-564 -636 0
+-565 -637 0
+-566 -638 0
+-567 -639 0
+-559 -703 0
+-560 -704 0
+-561 -705 0
+-562 -706 0
+-563 -707 0
+-564 -708 0
+-565 -709 0
+-566 -710 0
+-567 -711 0
+-559 -712 0
+-560 -713 0
+-561 -714 0
+-562 -715 0
+-563 -716 0
+-564 -717 0
+-565 -718 0
+-566 -719 0
+-567 -720 0
+-622 -550 0
+-623 -551 0
+-624 -552 0
+-625 -553 0
+-626 -554 0
+-627 -555 0
+-628 -556 0
+-629 -557 0
+-630 -558 0
+-622 -559 0
+-623 -560 0
+-624 -561 0
+-625 -562 0
+-626 -563 0
+-627 -564 0
+-628 -565 0
+-629 -566 0
+-630 -567 0
+-622 -712 0
+-623 -713 0
+-624 -714 0
+-625 -715 0
+-626 -716 0
+-627 -717 0
+-628 -718 0
+-629 -719 0
+-630 -720 0
+-622 -721 0
+-623 -722 0
+-624 -723 0
+-625 -724 0
+-626 -725 0
+-627 -726 0
+-628 -727 0
+-629 -728 0
+-630 -729 0
+-631 -541 0
+-632 -542 0
+-633 -543 0
+-634 -544 0
+-635 -545 0
+-636 -546 0
+-637 -547 0
+-638 -548 0
+-639 -549 0
+-631 -559 0
+-632 -560 0
+-633 -561 0
+-634 -562 0
+-635 -563 0
+-636 -564 0
+-637 -565 0
+-638 -566 0
+-639 -567 0
+-631 -703 0
+-632 -704 0
+-633 -705 0
+-634 -706 0
+-635 -707 0
+-636 -708 0
+-637 -709 0
+-638 -710 0
+-639 -711 0
+-631 -721 0
+-632 -722 0
+-633 -723 0
+-634 -724 0
+-635 -725 0
+-636 -726 0
+-637 -727 0
+-638 -728 0
+-639 -729 0
+-640 -541 0
+-641 -542 0
+-642 -543 0
+-643 -544 0
+-644 -545 0
+-645 -546 0
+-646 -547 0
+-647 -548 0
+-648 -549 0
+-640 -550 0
+-641 -551 0
+-642 -552 0
+-643 -553 0
+-644 -554 0
+-645 -555 0
+-646 -556 0
+-647 -557 0
+-648 -558 0
+-640 -703 0
+-641 -704 0
+-642 -705 0
+-643 -706 0
+-644 -707 0
+-645 -708 0
+-646 -709 0
+-647 -710 0
+-648 -711 0
+-640 -712 0
+-641 -713 0
+-642 -714 0
+-643 -715 0
+-644 -716 0
+-645 -717 0
+-646 -718 0
+-647 -719 0
+-648 -720 0
+-703 -550 0
+-704 -551 0
+-705 -552 0
+-706 -553 0
+-707 -554 0
+-708 -555 0
+-709 -556 0
+-710 -557 0
+-711 -558 0
+-703 -559 0
+-704 -560 0
+-705 -561 0
+-706 -562 0
+-707 -563 0
+-708 -564 0
+-709 -565 0
+-710 -566 0
+-711 -567 0
+-703 -631 0
+-704 -632 0
+-705 -633 0
+-706 -634 0
+-707 -635 0
+-708 -636 0
+-709 -637 0
+-710 -638 0
+-711 -639 0
+-703 -640 0
+-704 -641 0
+-705 -642 0
+-706 -643 0
+-707 -644 0
+-708 -645 0
+-709 -646 0
+-710 -647 0
+-711 -648 0
+-712 -541 0
+-713 -542 0
+-714 -543 0
+-715 -544 0
+-716 -545 0
+-717 -546 0
+-718 -547 0
+-719 -548 0
+-720 -549 0
+-712 -559 0
+-713 -560 0
+-714 -561 0
+-715 -562 0
+-716 -563 0
+-717 -564 0
+-718 -565 0
+-719 -566 0
+-720 -567 0
+-712 -622 0
+-713 -623 0
+-714 -624 0
+-715 -625 0
+-716 -626 0
+-717 -627 0
+-718 -628 0
+-719 -629 0
+-720 -630 0
+-712 -640 0
+-713 -641 0
+-714 -642 0
+-715 -643 0
+-716 -644 0
+-717 -645 0
+-718 -646 0
+-719 -647 0
+-720 -648 0
+-721 -541 0
+-722 -542 0
+-723 -543 0
+-724 -544 0
+-725 -545 0
+-726 -546 0
+-727 -547 0
+-728 -548 0
+-729 -549 0
+-721 -550 0
+-722 -551 0
+-723 -552 0
+-724 -553 0
+-725 -554 0
+-726 -555 0
+-727 -556 0
+-728 -557 0
+-729 -558 0
+-721 -622 0
+-722 -623 0
+-723 -624 0
+-724 -625 0
+-725 -626 0
+-726 -627 0
+-727 -628 0
+-728 -629 0
+-729 -630 0
+-721 -631 0
+-722 -632 0
+-723 -633 0
+-724 -634 0
+-725 -635 0
+-726 -636 0
+-727 -637 0
+-728 -638 0
+-729 -639 0
diff --git a/test/monniaux/picosat-965/tiny.dat b/test/monniaux/picosat-965/tiny.dat
new file mode 100644
index 00000000..1d89b303
--- /dev/null
+++ b/test/monniaux/picosat-965/tiny.dat
@@ -0,0 +1,2 @@
+p cnf 0 1
+0
diff --git a/test/monniaux/picosat-965/version.c b/test/monniaux/picosat-965/version.c
new file mode 100644
index 00000000..71c322bd
--- /dev/null
+++ b/test/monniaux/picosat-965/version.c
@@ -0,0 +1,14 @@
+#include "config.h"
+
+const char *
+picosat_version (void)
+{
+ return PICOSAT_VERSION;
+}
+
+const char *
+picosat_config (void)
+{
+ return PICOSAT_CC " " PICOSAT_CFLAGS;
+}
+
diff --git a/test/monniaux/predicated/predicated_run.c b/test/monniaux/predicated/predicated_run.c
new file mode 100644
index 00000000..2a15318b
--- /dev/null
+++ b/test/monniaux/predicated/predicated_run.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+extern void predicated_write(int flag, long *buf, long data);
+extern long predicated_read(long defval, int flag, long *buf);
+
+int main() {
+ long buf[2] = {42, 69};
+ printf("%ld\n", buf[1]);
+ predicated_write(0, buf, 33);
+ printf("%ld\n", buf[1]);
+ predicated_write(1, buf, 45);
+ printf("%ld\n", buf[1]);
+ printf("%ld\n", predicated_read(1515, 0, buf));
+ printf("%ld\n", predicated_read(1789, 1, buf));
+ return 0;
+}
diff --git a/test/monniaux/profiling/compcert_profiling.dat b/test/monniaux/profiling/compcert_profiling.dat
new file mode 100644
index 00000000..bd2f90da
--- /dev/null
+++ b/test/monniaux/profiling/compcert_profiling.dat
Binary files differ
diff --git a/test/monniaux/profiling/profiling_call.c b/test/monniaux/profiling/profiling_call.c
new file mode 100644
index 00000000..ce20241d
--- /dev/null
+++ b/test/monniaux/profiling/profiling_call.c
@@ -0,0 +1,27 @@
+/*
+For knowing how to write assembly profiling stubs.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <errno.h>
+
+typedef uint8_t md5_hash[16];
+typedef uint64_t condition_counters[2];
+
+void _compcert_write_profiling_table(unsigned int nr_items,
+ md5_hash id_table[],
+ condition_counters counter_table[]);
+
+static md5_hash id_table[42] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}};
+static condition_counters counter_table[42];
+
+void write_profile(void) {
+ _compcert_write_profiling_table(42, id_table, counter_table);
+}
+
+static _Atomic uint64_t counter;
+
+void incr_counter(void) {
+ counter++;
+}
diff --git a/test/monniaux/profiling/test_profiling b/test/monniaux/profiling/test_profiling
new file mode 100755
index 00000000..33e22d11
--- /dev/null
+++ b/test/monniaux/profiling/test_profiling
Binary files differ
diff --git a/test/monniaux/profiling/test_profiling.c b/test/monniaux/profiling/test_profiling.c
new file mode 100644
index 00000000..013b1d68
--- /dev/null
+++ b/test/monniaux/profiling/test_profiling.c
@@ -0,0 +1,15 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+ if (argc < 2) return 1;
+ int i = atoi(argv[1]);
+ if (i > 0) {
+ printf("positive\n");
+ } else if (i==0) {
+ printf("zero\n");
+ } else {
+ printf("negative\n");
+ }
+ return 0;
+}
diff --git a/test/monniaux/quest/Makefile b/test/monniaux/quest/Makefile
new file mode 100644
index 00000000..ef0b7db8
--- /dev/null
+++ b/test/monniaux/quest/Makefile
@@ -0,0 +1,24 @@
+# install Quest using: opam install quest
+MAX=300
+
+include ../rules.mk
+
+QUEST=quest
+KVX_CCOMPFLAGS += -fstruct-passing -fbitfields
+
+PREFIX=ran%06.f
+TARGETS_C=$(shell seq --format $(PREFIX).c 0 $(MAX))
+TARGETS_OUT=$(shell seq --format $(PREFIX).ccomp.kvx.out 0 $(MAX))
+
+all: $(TARGETS_C) $(TARGETS_OUT)
+
+ran%.c :
+ $(QUEST) -seed $* -test ansi > $@
+
+%.ccomp.kvx : %.ccomp.kvx.s
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+clean:
+ -rm -f $(TARGETS_C) $(TARGETS_OUT)
+
+.PHONY: all clean
diff --git a/test/monniaux/quicksort/Makefile b/test/monniaux/quicksort/Makefile
new file mode 100644
index 00000000..719cd755
--- /dev/null
+++ b/test/monniaux/quicksort/Makefile
@@ -0,0 +1,3 @@
+TARGET=quicksort
+
+include ../rules.mk
diff --git a/test/monniaux/quicksort/quicksort.c b/test/monniaux/quicksort/quicksort.c
new file mode 100644
index 00000000..4b93ae7b
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort.c
@@ -0,0 +1,42 @@
+#include "quicksort.h"
+
+/* Rosetta Code */
+void quicksort(data *A, int len) {
+ if (len < 2) return;
+
+ data pivot = A[len / 2];
+
+ int i, j;
+ for (i = 0, j = len - 1; ; i++, j--) {
+ while (A[i] < pivot) i++;
+ while (A[j] > pivot) j--;
+
+ if (i >= j) break;
+
+ data temp = A[i];
+ A[i] = A[j];
+ A[j] = temp;
+ }
+
+ quicksort(A, i);
+ quicksort(A + i, len - i);
+}
+
+data data_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next;
+}
+
+void data_vec_random(data *a, unsigned len) {
+ for(unsigned i=0; i<len; i++) {
+ a[i] = data_random();
+ }
+}
+
+bool data_vec_is_sorted(const data *a, unsigned len) {
+ for(unsigned i=0; i<len-1; i++) {
+ if (a[i] > a[i+1]) return false;
+ }
+ return true;
+}
diff --git a/test/monniaux/quicksort/quicksort.ccomp.k1c.s_modified5 b/test/monniaux/quicksort/quicksort.ccomp.k1c.s_modified5
new file mode 100644
index 00000000..d1379555
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort.ccomp.k1c.s_modified5
@@ -0,0 +1,285 @@
+# File generated by CompCert 3.4
+# Command line: -Wall -O3 -S quicksort.c -o quicksort.ccomp.kvx.s
+ .text
+ .balign 2
+ .globl quicksort
+quicksort:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+ make $r32, 2
+;;
+ compw.lt $r32 = $r18, $r32
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+;;
+ cb.wnez $r32?.L100
+;;
+ sraw $r32 = $r18, 31
+ make $r20, 0
+ addw $r38 = $r18, 4294967295
+;;
+ srlw $r32 = $r32, 31
+;;
+ addw $r32 = $r18, $r32
+;;
+ sraw $r41 = $r32, 1
+;;
+ sxwd $r5 = $r41
+;;
+ ld.xs $r15 = $r5[$r19]
+;;
+.L101:
+ sxwd $r6 = $r20
+;;
+ ld.xs $r34 = $r6[$r19]
+;;
+ compd.geu $r32 = $r34, $r15
+;;
+ cb.wnez $r32?.L102
+;;
+ addw $r20 = $r20, 1
+ goto .L101
+;;
+.L102:
+ sxwd $r17 = $r38
+;;
+ ld.xs $r2 = $r17[$r19]
+;;
+ compd.leu $r32 = $r2, $r15
+;;
+ cb.wnez $r32?.L103
+;;
+ addw $r38 = $r38, 4294967295
+ goto .L102
+;;
+.L103:
+ compw.ge $r32 = $r20, $r38
+;;
+ cb.wnez $r32?.L104
+;;
+ sxwd $r35 = $r20
+ addw $r20 = $r20, 1
+ addw $r38 = $r38, 4294967295
+;;
+ ld.xs $r9 = $r35[$r19]
+;;
+ sd.xs $r35[$r19] = $r2
+;;
+ sd.xs $r17[$r19] = $r9
+ goto .L101
+;;
+.L104:
+ addd $r1 = $r20, 0
+ addd $r0 = $r19, 0
+ call quicksort
+;;
+ sxwd $r11 = $r20
+ sbfw $r1 = $r20, $r18
+ ld $r16 = 8[$r12]
+;;
+ slld $r33 = $r11, 3
+ ld $r18 = 16[$r12]
+;;
+ addd $r0 = $r19, $r33
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ goto quicksort
+;;
+.L100:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type quicksort, @function
+ .size quicksort, . - quicksort
+ .data
+ .balign 8
+next:
+ .long 0x4f091c37, 0x0
+ .type next, @object
+ .size next, . - next
+ .text
+ .balign 2
+ .globl data_random
+data_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r32 = next
+ make $r0, 1103515249
+;;
+ ld $r1 = 0[$r32]
+ make $r32 = next
+;;
+ muld $r63 = $r1, $r0
+;;
+ addd $r0 = $r63, 12345
+;;
+ sd 0[$r32] = $r0
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_random, @function
+ .size data_random, . - data_random
+ .text
+ .balign 2
+ .globl data_vec_random
+data_vec_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+ make $r20, 0
+;;
+.L105:
+ compw.geu $r32 = $r20, $r18
+;;
+ cb.wnez $r32?.L106
+;;
+ call data_random
+;;
+ addd $r1 = $r20, 0
+ addw $r20 = $r20, 1
+;;
+ zxwd $r1 = $r1
+;;
+ slld $r2 = $r1, 3
+;;
+ addd $r3 = $r19, $r2
+;;
+ sd 0[$r3] = $r0
+ goto .L105
+;;
+.L106:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type data_vec_random, @function
+ .size data_vec_random, . - data_vec_random
+ .text
+ .balign 2
+ .globl data_vec_is_sorted
+data_vec_is_sorted:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r2, 0
+;;
+.L107:
+ addw $r6 = $r1, 4294967295
+;;
+ compw.geu $r32 = $r2, $r6
+;;
+ cb.wnez $r32?.L108
+;;
+ addd $r3 = $r2, 0
+ addw $r2 = $r2, 1
+;;
+ zxwd $r3 = $r3
+;;
+ slld $r5 = $r3, 3
+ addd $r3 = $r2, 0
+;;
+ addd $r10 = $r0, $r5
+ zxwd $r3 = $r3
+;;
+ slld $r8 = $r3, 3
+;;
+ addd $r3 = $r0, $r8
+;;
+ ld $r4 = 0[$r10]
+;;
+ ld $r9 = 0[$r3]
+;;
+ compd.leu $r32 = $r4, $r9
+;;
+ cb.wnez $r32?.L107
+;;
+ make $r0, 0
+ goto .L109
+;;
+.L108:
+ make $r0, 1
+;;
+.L109:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_vec_is_sorted, @function
+ .size data_vec_is_sorted, . - data_vec_is_sorted
diff --git a/test/monniaux/quicksort/quicksort.ccomp.k1c.s_orig b/test/monniaux/quicksort/quicksort.ccomp.k1c.s_orig
new file mode 100644
index 00000000..b83b9a64
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort.ccomp.k1c.s_orig
@@ -0,0 +1,301 @@
+# File generated by CompCert 3.4
+# Command line: -Wall -O3 -S quicksort.c -o quicksort.ccomp.kvx.s
+ .text
+ .balign 2
+ .globl quicksort
+quicksort:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+ make $r32, 2
+;;
+ compw.lt $r32 = $r18, $r32
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+;;
+ cb.wnez $r32?.L100
+;;
+ sraw $r32 = $r18, 31
+ make $r20, 0
+ addw $r38 = $r18, 4294967295
+;;
+ srlw $r32 = $r32, 31
+;;
+ addw $r32 = $r18, $r32
+;;
+ sraw $r41 = $r32, 1
+;;
+ sxwd $r5 = $r41
+;;
+ slld $r7 = $r5, 3
+;;
+ addd $r37 = $r19, $r7
+;;
+ ld $r15 = 0[$r37]
+;;
+.L101:
+ sxwd $r6 = $r20
+;;
+ slld $r40 = $r6, 3
+;;
+ addd $r4 = $r19, $r40
+;;
+ ld $r34 = 0[$r4]
+;;
+ compd.geu $r32 = $r34, $r15
+;;
+ cb.wnez $r32?.L102
+;;
+ addw $r20 = $r20, 1
+ goto .L101
+;;
+.L102:
+ sxwd $r17 = $r38
+;;
+ slld $r39 = $r17, 3
+;;
+ addd $r0 = $r19, $r39
+;;
+ ld $r2 = 0[$r0]
+;;
+ compd.leu $r32 = $r2, $r15
+;;
+ cb.wnez $r32?.L103
+;;
+ addw $r38 = $r38, 4294967295
+ goto .L102
+;;
+.L103:
+ compw.ge $r32 = $r20, $r38
+;;
+ cb.wnez $r32?.L104
+;;
+ sxwd $r35 = $r20
+ addw $r20 = $r20, 1
+ addw $r38 = $r38, 4294967295
+;;
+ slld $r10 = $r35, 3
+;;
+ addd $r1 = $r19, $r10
+;;
+ ld $r9 = 0[$r1]
+;;
+ sd 0[$r1] = $r2
+;;
+ sd 0[$r0] = $r9
+ goto .L101
+;;
+.L104:
+ addd $r1 = $r20, 0
+ addd $r0 = $r19, 0
+ call quicksort
+;;
+ sxwd $r11 = $r20
+ sbfw $r1 = $r20, $r18
+ ld $r16 = 8[$r12]
+;;
+ slld $r33 = $r11, 3
+ ld $r18 = 16[$r12]
+;;
+ addd $r0 = $r19, $r33
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ goto quicksort
+;;
+.L100:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type quicksort, @function
+ .size quicksort, . - quicksort
+ .data
+ .balign 8
+next:
+ .long 0x4f091c37, 0x0
+ .type next, @object
+ .size next, . - next
+ .text
+ .balign 2
+ .globl data_random
+data_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r32 = next
+ make $r0, 1103515249
+;;
+ ld $r1 = 0[$r32]
+ make $r32 = next
+;;
+ muld $r63 = $r1, $r0
+;;
+ addd $r0 = $r63, 12345
+;;
+ sd 0[$r32] = $r0
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_random, @function
+ .size data_random, . - data_random
+ .text
+ .balign 2
+ .globl data_vec_random
+data_vec_random:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -48
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ sd 16[$r12] = $r18
+ addd $r18 = $r1, 0
+;;
+ sd 24[$r12] = $r19
+ addd $r19 = $r0, 0
+;;
+ sd 32[$r12] = $r20
+ make $r20, 0
+;;
+.L105:
+ compw.geu $r32 = $r20, $r18
+;;
+ cb.wnez $r32?.L106
+;;
+ call data_random
+;;
+ addd $r1 = $r20, 0
+ addw $r20 = $r20, 1
+;;
+ zxwd $r1 = $r1
+;;
+ slld $r2 = $r1, 3
+;;
+ addd $r3 = $r19, $r2
+;;
+ sd 0[$r3] = $r0
+ goto .L105
+;;
+.L106:
+ ld $r16 = 8[$r12]
+;;
+ ld $r18 = 16[$r12]
+;;
+ ld $r19 = 24[$r12]
+;;
+ ld $r20 = 32[$r12]
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 48
+;;
+ ret
+;;
+ .type data_vec_random, @function
+ .size data_vec_random, . - data_vec_random
+ .text
+ .balign 2
+ .globl data_vec_is_sorted
+data_vec_is_sorted:
+ addd $r14 = $r12, 0
+ addd $r12 = $r12, -16
+;;
+ sd 0[$r12] = $r14
+;;
+;;
+ get $r16 = $ra
+;;
+ sd 8[$r12] = $r16
+;;
+ make $r2, 0
+;;
+.L107:
+ addw $r6 = $r1, 4294967295
+;;
+ compw.geu $r32 = $r2, $r6
+;;
+ cb.wnez $r32?.L108
+;;
+ addd $r3 = $r2, 0
+ addw $r2 = $r2, 1
+;;
+ zxwd $r3 = $r3
+;;
+ slld $r5 = $r3, 3
+ addd $r3 = $r2, 0
+;;
+ addd $r10 = $r0, $r5
+ zxwd $r3 = $r3
+;;
+ slld $r8 = $r3, 3
+;;
+ addd $r3 = $r0, $r8
+;;
+ ld $r4 = 0[$r10]
+;;
+ ld $r9 = 0[$r3]
+;;
+ compd.leu $r32 = $r4, $r9
+;;
+ cb.wnez $r32?.L107
+;;
+ make $r0, 0
+ goto .L109
+;;
+.L108:
+ make $r0, 1
+;;
+.L109:
+;;
+ ld $r16 = 8[$r12]
+;;
+ set $ra = $r16
+;;
+ addd $r12 = $r12, 16
+;;
+ ret
+;;
+ .type data_vec_is_sorted, @function
+ .size data_vec_is_sorted, . - data_vec_is_sorted
diff --git a/test/monniaux/quicksort/quicksort.h b/test/monniaux/quicksort/quicksort.h
new file mode 100644
index 00000000..cc73e7c3
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort.h
@@ -0,0 +1,7 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef uint64_t data;
+void quicksort(data *A, int len);
+void data_vec_random(data *a, unsigned len);
+bool data_vec_is_sorted(const data *a, unsigned len);
diff --git a/test/monniaux/quicksort/quicksort_run.c b/test/monniaux/quicksort/quicksort_run.c
new file mode 100644
index 00000000..3c640b24
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort_run.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "quicksort.h"
+#include "../cycles.h"
+
+int main (void) {
+ cycle_count_config();
+ unsigned len=30000;
+ data *vec = malloc(sizeof(data) * len);
+ data_vec_random(vec, len);
+ cycle_t quicksort_time = get_cycle();
+ quicksort(vec, len);
+ quicksort_time = get_cycle() - quicksort_time;
+ printf("sorted=%s\n"
+ "time cycles:%" PRcycle "\n",
+ data_vec_is_sorted(vec, len)?"true":"false",
+ quicksort_time);
+ free(vec);
+ return 0;
+}
+
diff --git a/test/monniaux/reduced_picosat/reduced_picosat.c b/test/monniaux/reduced_picosat/reduced_picosat.c
new file mode 100644
index 00000000..eb9fdaf8
--- /dev/null
+++ b/test/monniaux/reduced_picosat/reduced_picosat.c
@@ -0,0 +1,23 @@
+typedef struct b b;
+b *a;
+struct b {
+ int c;
+ int d, **clshead;
+ int **ahead;
+ unsigned h;
+} glob;
+int k();
+int main() {
+ a = &glob;
+ k(a);
+}
+#define e(f) f - g->c
+static void m(b *g, int *l) {
+ if (g)
+ *g->ahead = l;
+}
+int k(b *g) {
+ if (g->d)
+ m(g, e(g->clshead[-1]));
+ return g->h;
+}
diff --git a/test/monniaux/reduced_picosat/test_a.s b/test/monniaux/reduced_picosat/test_a.s
new file mode 100644
index 00000000..c14cc8f9
--- /dev/null
+++ b/test/monniaux/reduced_picosat/test_a.s
@@ -0,0 +1,10 @@
+ .text
+ .global dummyload
+ .type dummyload, @function
+dummyload:
+ make $r0 = 0
+ ;;
+ ld.s $r0 = -8[$r0]
+ ret
+ ;;
+ .size dummyload, .-dummyload
diff --git a/test/monniaux/reduced_picosat/test_b.c b/test/monniaux/reduced_picosat/test_b.c
new file mode 100644
index 00000000..a0fe625b
--- /dev/null
+++ b/test/monniaux/reduced_picosat/test_b.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+extern uint64_t dummyload(void);
+
+int main() {
+ printf("%" PRIu64 "\n", dummyload());
+}
diff --git a/test/monniaux/reduced_picosat/testcmp.sh b/test/monniaux/reduced_picosat/testcmp.sh
new file mode 100755
index 00000000..8dc93de9
--- /dev/null
+++ b/test/monniaux/reduced_picosat/testcmp.sh
@@ -0,0 +1,146 @@
+DEFINES="-DNALARM -DNZIP -DNGETRUSAGE -DNDEBUG"
+COMPCERT=/home/monniaux/work/Kalray/mppa-RTLpathSE-verif-hash-junk
+DATA=$COMPCERT/test/monniaux/picosat-965/tiny.dat
+CCOMP="$COMPCERT/ccomp -fbitfields -fduplicate 2 -fall-loads-nontrap $DEFINES"
+GCC="kvx-cos-gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC0="gcc -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC1="gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC2="gcc -O -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror -fsanitize=undefined -fsanitize=address $DEFINES"
+HOSTCC3="gcc -O3 -Wimplicit -Wuninitialized -Wmaybe-uninitialized -Werror $DEFINES"
+HOSTCC4="/usr/bin/clang -Wimplicit -Wuninitialized -Werror $DEFINES"
+HOSTCC5="/usr/bin/clang -Wimplicit -Wuninitialized -Werror -fsanitize=undefined -fsanitize=address $DEFINES"
+CFILES="reduced_picosat.c"
+SIMU="kvx-cluster --timeout=10000000 -- "
+
+if ! $HOSTCC0 $CFILES -o picosat.cc0.host ;
+then exit 30 ;
+fi
+
+if ! $HOSTCC1 $CFILES -o picosat.cc1.host ;
+then exit 31 ;
+fi
+
+if ! $HOSTCC2 $CFILES -o picosat.cc2.host ;
+then exit 32 ;
+fi
+
+if ! $HOSTCC3 $CFILES -o picosat.cc3.host ;
+then exit 33 ;
+fi
+
+if ! $HOSTCC4 $CFILES -o picosat.cc4.host ;
+then exit 34 ;
+fi
+
+if ! $HOSTCC5 $CFILES -o picosat.cc5.host ;
+then exit 35 ;
+fi
+
+timeout 1 ./picosat.cc0.host $DATA 2>&1 > picosat.cc0.out
+if [ $? -ge 100 ];
+then exit 40 ;
+fi
+
+timeout 1 ./picosat.cc1.host $DATA 2>&1 > picosat.cc1.out
+if [ $? -ge 100 ];
+then exit 41 ;
+fi
+
+timeout 1 valgrind --log-file=picosat.cc0.valgrind.log ./picosat.cc0.host $DATA 2>&1 > picosat.cc0.valgrind.out
+if [ $? -ge 100 ];
+then exit 50 ;
+fi
+
+timeout 1 valgrind --log-file=picosat.cc1.valgrind.log ./picosat.cc1.host $DATA 2>&1 > picosat.cc1.valgrind.out
+if [ $? -ge 100 ];
+then exit 51 ;
+fi
+
+timeout 1 ./picosat.cc2.host $DATA 2>&1 > picosat.cc2.out
+if [ $? -ge 100 ];
+then exit 42 ;
+fi
+
+timeout 1 ./picosat.cc3.host $DATA 2>&1 > picosat.cc3.out
+if [ $? -ge 100 ];
+then exit 43 ;
+fi
+
+timeout 1 ./picosat.cc4.host $DATA 2>&1 > picosat.cc4.out
+if [ $? -ge 100 ];
+then exit 44 ;
+fi
+
+timeout 1 ./picosat.cc5.host $DATA 2>&1 > picosat.cc5.out
+if [ $? -ge 100 ];
+then exit 45 ;
+fi
+
+if ! cmp picosat.cc0.out picosat.cc1.out ;
+then exit 60 ;
+fi
+
+if ! cmp picosat.cc0.out picosat.cc0.valgrind.out ;
+then exit 70 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc1.valgrind.out ;
+then exit 61 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc2.out ;
+then exit 62 ;
+fi
+
+if ! cmp picosat.cc1.out picosat.cc3.out ;
+then exit 63 ;
+fi
+
+if ! $GCC $CFILES -o picosat.gcc.target ;
+then exit 1 ;
+fi
+
+if ! $CCOMP $CFILES -o picosat.ccomp.target ;
+then exit 2 ;
+fi
+
+if ! $CCOMP -fprepass -fprepass= list $CFILES -o picosat.prepass.target ;
+then exit 3 ;
+fi
+
+$SIMU ./picosat.gcc.target $DATA 2>&1 > picosat.gcc.out
+if [ $? -ge 100 ];
+then exit 4 ;
+fi
+
+if ! cmp picosat.gcc.out picosat.cc1.out ;
+then exit 13 ;
+fi
+
+if grep timeout picosat.gcc.out ;
+then exit 8 ;
+fi
+
+$SIMU ./picosat.ccomp.target $DATA 2>&1 > picosat.ccomp.out
+if [ $? -ge 100 ];
+then exit 5 ;
+fi
+
+if grep timeout picosat.ccomp.out ;
+then exit 9 ;
+fi
+
+if ! cmp picosat.gcc.out picosat.ccomp.out ;
+then exit 6 ;
+fi
+
+$SIMU ./picosat.prepass.target $DATA 2>&1 > picosat.prepass.out
+if [ $? -ge 100 ];
+then exit 0 ;
+fi
+
+if cmp picosat.gcc.out picosat.prepass.out ;
+then exit 7 ;
+fi
+
+exit 0
diff --git a/test/monniaux/regalloc/bigspill.c b/test/monniaux/regalloc/bigspill.c
new file mode 100644
index 00000000..6191e018
--- /dev/null
+++ b/test/monniaux/regalloc/bigspill.c
@@ -0,0 +1,21 @@
+extern void callee(void);
+
+void bigspill(int *t) {
+ int t0 = t[0];
+ int t1 = t[1];
+ int t2 = t[2];
+ int t3 = t[3];
+ int t4 = t[4];
+ int t5 = t[5];
+ int t6 = t[6];
+ int t7 = t[7];
+ callee();
+ t[0] = t0;
+ t[1] = t1;
+ t[2] = t2;
+ t[3] = t3;
+ t[4] = t4;
+ t[5] = t5;
+ t[6] = t6;
+ t[7] = t7;
+}
diff --git a/test/monniaux/regalloc/invalid_register_allocation.c b/test/monniaux/regalloc/invalid_register_allocation.c
new file mode 100644
index 00000000..93da30eb
--- /dev/null
+++ b/test/monniaux/regalloc/invalid_register_allocation.c
@@ -0,0 +1,12 @@
+/* simplified version of c-strtod.i */
+
+extern double getdouble(void);
+extern void intptr(void);
+
+double bidule (void)
+{
+ double r;
+ r = getdouble();
+ intptr();
+ return r;
+}
diff --git a/test/monniaux/regalloc/invalid_register_allocation2.c b/test/monniaux/regalloc/invalid_register_allocation2.c
new file mode 100644
index 00000000..88a8e184
--- /dev/null
+++ b/test/monniaux/regalloc/invalid_register_allocation2.c
@@ -0,0 +1,7 @@
+extern void toto(int, double);
+
+void
+report (int x)
+{
+ toto (1, x/ 10.0);
+}
diff --git a/test/monniaux/regalloc/spill_queue_issues.c b/test/monniaux/regalloc/spill_queue_issues.c
new file mode 100644
index 00000000..8e2019a8
--- /dev/null
+++ b/test/monniaux/regalloc/spill_queue_issues.c
@@ -0,0 +1,5 @@
+int g(double);
+ ;
+void meow (int x) {
+ g(100.0 * x);
+}
diff --git a/test/monniaux/rotate/rotate.c b/test/monniaux/rotate/rotate.c
new file mode 100644
index 00000000..96f20c49
--- /dev/null
+++ b/test/monniaux/rotate/rotate.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+unsigned rotate(unsigned x) {
+ return (x << 4) | (x >> (32-4));
+}
+
+int main() {
+ unsigned x = 0x12345678U;
+ printf("%X %X\n", x, rotate(x));
+}
diff --git a/test/monniaux/rules.mk b/test/monniaux/rules.mk
new file mode 100644
index 00000000..cab957c0
--- /dev/null
+++ b/test/monniaux/rules.mk
@@ -0,0 +1,162 @@
+# This Makefile does not depend on ../rules.mk
+SHELL=bash
+
+# You can modify ALL_CFILES to include the files that should be linked
+ALL_CFILES?=$(wildcard *.c)
+
+# Name of the target
+TARGET?=toto
+
+# Arguments of execution
+EXECUTE_ARGS?=
+
+# Name of the clock object
+CLOCK=../clock
+
+# Maximum amount of time measures (see cycles.h)
+MAX_MEASURES=10
+MEASURES?=time
+
+# Flags common to both compilers, then to gcc, then to ccomp
+ALL_CFLAGS+=-Wall -D__KVX_COS__ -DMAX_MEASURES=$(MAX_MEASURES)
+#ALL_CFLAGS+=-g
+ALL_GCCFLAGS+=$(ALL_CFLAGS) -std=c99 -Wextra -Werror=implicit
+ALL_CCOMPFLAGS+=$(ALL_CFLAGS) # -fprofile-use= ../compcert_profiling.dat
+
+# The compilers
+KVX_CC?=kvx-elf-gcc
+KVX_CCOMP?=ccomp
+
+# Command to execute
+#EXECUTE_CYCLES?=timeout --signal=SIGTERM 3m kvx-cluster --syscall=libstd_scalls.so --cycle-based --
+EXECUTE_CYCLES?=kvx-cluster --enable-cache --syscall=libstd_scalls.so --cycle-based --
+
+# You can define up to GCC4FLAGS and CCOMP4FLAGS
+GCC0FLAGS?=$(ALL_GCCFLAGS) -O0
+GCC1FLAGS?=$(ALL_GCCFLAGS) -O1
+GCC2FLAGS?=$(ALL_GCCFLAGS) -O2
+GCC3FLAGS?=$(ALL_GCCFLAGS) -O3
+GCC4FLAGS?=
+CCOMP0FLAGS?=$(ALL_CCOMPFLAGS) -O2
+CCOMP1FLAGS?=$(ALL_CCOMPFLAGS) -O2 -fprepass= list
+CCOMP2FLAGS?=$(ALL_CCOMPFLAGS) -O2 -funrollsingle 30
+CCOMP3FLAGS?=$(ALL_CCOMPFLAGS) -O2 -fprepass= list -funrollsingle 30
+CCOMP4FLAGS?=$(ALL_CCOMPFLAGS) -O2 -fprepass= zigzag
+
+# Prefix names
+GCC0PREFIX?=.gcc.o0
+GCC1PREFIX?=.gcc.o1
+GCC2PREFIX?=.gcc.o2
+GCC3PREFIX?=.gcc.o3
+GCC4PREFIX?=
+CCOMP0PREFIX?=.ccomp
+CCOMP1PREFIX?=.ccomp.prepass_list
+CCOMP2PREFIX?=.ccomp.unrollsingle_30
+CCOMP3PREFIX?=.ccomp.prepass_list-unrollsingle_30
+CCOMP4PREFIX?=.ccomp.prepass_zigzag
+
+# List of outfiles, updated by gen_rules
+OUTFILES:=
+BINFILES:=
+
+# First line of the CSV file, completed later
+FIRSTLINE:=benches
+
+firstrule: all
+
+# $1: compiler
+# $2: compilation flags
+# $3: extension prefix
+define gen_rules
+
+.SECONDARY:
+asm/%$(3).s: %.c
+ @mkdir -p $$(@D)
+ $(1) $(2) -S $$< -o $$@
+
+.SECONDARY:
+bin/$(TARGET)$(3).bin: $(addprefix obj/,$(ALL_CFILES:.c=$(3).o)) $(CLOCK).gcc.kvx.o
+ @mkdir -p $$(@D)
+ $(1) $$+ -lm -o $$@
+
+BINFILES:=$(BINFILES) bin/$(TARGET)$(3).bin
+OUTFILES:=$(OUTFILES) out/$(TARGET)$(3).out
+FIRSTLINE:=$(FIRSTLINE), $(3)
+
+endef
+
+# Clock generation
+$(CLOCK).gcc.kvx.o: $(CLOCK).c
+ $(KVX_CC) $(ALL_GCCFLAGS) -O3 $< -c -o $@
+
+# Generic rules
+obj/%.o: asm/%.s
+ @mkdir -p $(@D)
+ $(KVX_CC) $< -c -o $@
+
+out/%.out: bin/%.bin
+ @mkdir -p $(@D)
+ @rm -f $@
+ $(EXECUTE_CYCLES) $< $(subst __BASE__,$(patsubst %.out,%,$@),$(EXECUTE_ARGS)) | tee -a $@
+
+##
+# Generating the rules for all the compiler/flags..
+##
+
+ifneq ($(GCC0FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC0FLAGS),$(GCC0PREFIX)))
+endif
+ifneq ($(GCC1FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC1FLAGS),$(GCC1PREFIX)))
+endif
+ifneq ($(GCC2FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC2FLAGS),$(GCC2PREFIX)))
+endif
+ifneq ($(GCC3FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC3FLAGS),$(GCC3PREFIX)))
+endif
+ifneq ($(GCC4FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC4FLAGS),$(GCC4PREFIX)))
+endif
+
+ifneq ($(CCOMP0FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP0FLAGS),$(CCOMP0PREFIX)))
+endif
+ifneq ($(CCOMP1FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP1FLAGS),$(CCOMP1PREFIX)))
+endif
+ifneq ($(CCOMP2FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP2FLAGS),$(CCOMP2PREFIX)))
+endif
+ifneq ($(CCOMP3FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP3FLAGS),$(CCOMP3PREFIX)))
+endif
+ifneq ($(CCOMP4FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP4FLAGS),$(CCOMP4PREFIX)))
+endif
+
+measures.csv: $(OUTFILES)
+ @echo $(FIRSTLINE) > $@
+ @for i in "$(MEASURES)"; do\
+ first=$$(grep "$$i cycles" $(firstword $(OUTFILES)));\
+ if test ! -z "$$first"; then\
+ if [ "$$i" != "time" ]; then\
+ line="$(TARGET) $$i";\
+ else\
+ line="$(TARGET)";\
+ fi;\
+ $(foreach outfile,$(OUTFILES),line="$$line, $$(grep "$$i cycles" $(outfile) | cut -d':' -f2)"; ):;\
+ echo "$$line" >> $@;\
+ fi;\
+ done;\
+ echo "$@ created!"
+
+.PHONY: all clean run
+all: $(BINFILES)
+
+run: measures.csv
+
+clean:
+ rm -f *.o *.s *.bin *.out
+ rm -rf asm/ bin/ obj/ out/
+
diff --git a/test/monniaux/run_benches.sh b/test/monniaux/run_benches.sh
new file mode 100755
index 00000000..2b2e28d6
--- /dev/null
+++ b/test/monniaux/run_benches.sh
@@ -0,0 +1,27 @@
+
+source benches.sh
+
+cores=$(grep -c ^processor /proc/cpuinfo)
+processes=$((cores/4))
+
+rm -f commands.txt
+for bench in $benches; do
+ echo "(cd $bench && echo \"Running $bench..\" &&\
+ make -j4 run > /dev/null && echo \"$bench DONE\")" >> commands.txt
+done
+
+cat commands.txt | xargs -n1 -I{} -P$processes bash -c '{}'
+
+##
+# Gather all the CSV files
+##
+
+benches_csv=""
+for bench in $benches; do
+ if [ -f $bench/measures.csv ]; then
+ benches_csv="$benches_csv $bench/measures.csv"
+ fi
+done
+
+nawk 'FNR==1 && NR!=1{next;}{print}' $benches_csv > $1
+echo "$1 done"
diff --git a/test/monniaux/sandbox/Makefile b/test/monniaux/sandbox/Makefile
new file mode 100644
index 00000000..c4a81f1c
--- /dev/null
+++ b/test/monniaux/sandbox/Makefile
@@ -0,0 +1,148 @@
+# This Makefile does not depend on ../rules.mk
+SHELL=bash
+
+# You can modify ALL_CFILES to include the files that should be linked
+ALL_CFILES=$(wildcard *.c)
+
+# Name of the target
+TARGET=toto
+
+# Name of the clock object
+CLOCK=../clock.gcc.kvx.o
+
+# Maximum amount of time measures (see cycles.h)
+MAX_MEASURES=10
+
+# Flags common to both compilers, then to gcc, then to ccomp
+ALL_CFLAGS=-Wall -D__KVX_COS__ -DMAX_MEASURES=$(MAX_MEASURES)
+#ALL_CFLAGS+=-g
+ALL_GCCFLAGS=$(ALL_CFLAGS) -std=c99 -Wextra -Werror=implicit
+ALL_CCOMPFLAGS=$(ALL_CFLAGS)
+
+# The compilers
+KVX_CC=k1-cos-gcc
+KVX_CCOMP=ccomp
+
+# Command to execute
+EXECUTE_CYCLES=k1-cluster --syscall=libstd_scalls.so --cycle-based --
+
+# You can define up to GCC4FLAGS and CCOMP4FLAGS
+GCC0FLAGS=
+GCC1FLAGS=$(ALL_GCCFLAGS) -O1
+GCC2FLAGS=$(ALL_GCCFLAGS) -O2
+GCC3FLAGS=$(ALL_GCCFLAGS) -O3
+GCC4FLAGS=
+CCOMP0FLAGS=
+CCOMP1FLAGS=$(ALL_CCOMPFLAGS) -fno-postpass
+CCOMP2FLAGS=$(ALL_CCOMPFLAGS)
+CCOMP3FLAGS=
+CCOMP4FLAGS=
+
+# Prefix names
+GCC0PREFIX=
+GCC1PREFIX=.gcc.o1
+GCC2PREFIX=.gcc.o2
+GCC3PREFIX=.gcc.o3
+GCC4PREFIX=
+CCOMP0PREFIX=.ccomp.o0
+CCOMP1PREFIX=.ccomp.o1
+CCOMP2PREFIX=.ccomp.o2
+CCOMP3PREFIX=
+CCOMP4PREFIX=
+
+# List of outfiles, updated by gen_rules
+OUTFILES:=
+BINFILES:=
+
+# First line of the CSV file
+FIRSTLINE:=benches
+
+firstrule: all
+
+# $1: compiler
+# $2: compilation flags
+# $3: extension prefix
+define gen_rules
+
+.SECONDARY:
+asm/%$(3).s: %.c
+ @mkdir -p $$(@D)
+ $(1) $(2) -S $$< -o $$@
+
+.SECONDARY:
+bin/$(TARGET)$(3).bin: $(addprefix obj/,$(ALL_CFILES:.c=$(3).o)) $(CLOCK)
+ @mkdir -p $$(@D)
+ $(KVX_CC) $$+ -lm -o $$@
+
+BINFILES:=$(BINFILES) bin/$(TARGET)$(3).bin
+OUTFILES:=$(OUTFILES) out/$(TARGET)$(3).out
+FIRSTLINE:=$(FIRSTLINE), $(3)
+
+endef
+
+# Generic rules
+obj/%.o: asm/%.s
+ @mkdir -p $(@D)
+ $(KVX_CC) $< -c -o $@
+
+out/%.out: bin/%.bin
+ @mkdir -p $(@D)
+ $(EXECUTE_CYCLES) $< | tee $@
+
+##
+# Generating the rules for all the compiler/flags..
+##
+
+ifneq ($(GCC0FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC0FLAGS),$(GCC0PREFIX)))
+endif
+ifneq ($(GCC1FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC1FLAGS),$(GCC1PREFIX)))
+endif
+ifneq ($(GCC2FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC2FLAGS),$(GCC2PREFIX)))
+endif
+ifneq ($(GCC3FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC3FLAGS),$(GCC3PREFIX)))
+endif
+ifneq ($(GCC4FLAGS),)
+$(eval $(call gen_rules,$(KVX_CC),$(GCC4FLAGS),$(GCC4PREFIX)))
+endif
+
+ifneq ($(CCOMP0FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP0FLAGS),$(CCOMP0PREFIX)))
+endif
+ifneq ($(CCOMP1FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP1FLAGS),$(CCOMP1PREFIX)))
+endif
+ifneq ($(CCOMP2FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP2FLAGS),$(CCOMP2PREFIX)))
+endif
+ifneq ($(CCOMP3FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP3FLAGS),$(CCOMP3PREFIX)))
+endif
+ifneq ($(CCOMP4FLAGS),)
+$(eval $(call gen_rules,$(KVX_CCOMP),$(CCOMP4FLAGS),$(CCOMP4PREFIX)))
+endif
+
+measures.csv: $(OUTFILES)
+ @echo $(FIRSTLINE) > $@
+ @for ((i=0;i<=$(MAX_MEASURES);i++)) do\
+ first=$$(grep "($$i) cycles" $(firstword $(OUTFILES)));\
+ if test ! -z "$$first"; then\
+ line="$(TARGET) $$i";\
+ $(foreach outfile,$(OUTFILES),line="$$line, $$(grep "($$i) cycles" $(outfile) | cut -d':' -f2)"; ):;\
+ echo "$$line" >> $@;\
+ fi;\
+ done;\
+ echo "$@ created!"
+
+.PHONY: all clean run
+all: $(BINFILES)
+
+run: measures.csv
+
+clean:
+ rm -f *.o *.s *.bin *.out
+ rm -f asm/*.s bin/*.bin obj/*.o out/*.out
+
diff --git a/test/monniaux/sandbox/example.c b/test/monniaux/sandbox/example.c
new file mode 100644
index 00000000..2b8fc8c5
--- /dev/null
+++ b/test/monniaux/sandbox/example.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include "f.h"
+#include "../cycles.h"
+
+#if 0
+int main(void){
+ cycle_count_config();
+
+ int i;
+ int S = 0;
+
+ TIMEINIT
+ for (i = 0; i < 1000; i++){
+ S += f(i, i*2);
+ }
+ TIMESTOP(0)
+
+ printf("Final value: %d\n", S);
+ TIMESTOP(1)
+
+ TIMEPRINT(1)
+ return 0;
+}
+#endif
+
+#if 0
+int main(void){
+ cycle_count_config();
+
+ TIMEINIT
+ int a = 42;
+ int b = 21;
+ int c = 42+b;
+ TIMESTOP(0)
+ int d = a + b;
+ int e = a + b + c + d;
+ TIMESTOP(1)
+
+ TIMEPRINT(1)
+ return e;
+}
+#endif
diff --git a/test/monniaux/sandbox/f.c b/test/monniaux/sandbox/f.c
new file mode 100644
index 00000000..7705c4f7
--- /dev/null
+++ b/test/monniaux/sandbox/f.c
@@ -0,0 +1,3 @@
+int f(int x, int y){
+ return x+y;
+}
diff --git a/test/monniaux/sandbox/f.h b/test/monniaux/sandbox/f.h
new file mode 100644
index 00000000..6ecab534
--- /dev/null
+++ b/test/monniaux/sandbox/f.h
@@ -0,0 +1 @@
+int f(int, int);
diff --git a/test/monniaux/sandbox/sha-256.c b/test/monniaux/sandbox/sha-256.c
new file mode 100644
index 00000000..9a9e7802
--- /dev/null
+++ b/test/monniaux/sandbox/sha-256.c
@@ -0,0 +1,387 @@
+#include <stdint.h>
+#include <string.h>
+#if 0 /* __COMPCERT__ */
+#define my_memcpy(dst, src, size) __builtin_memcpy_aligned(dst, src, size, 1)
+#else
+#define my_memcpy(dst, src, size) memcpy(dst, src, size)
+#endif
+
+#include "../cycles.h"
+
+#include "sha-256.h"
+
+#define USE_ORIGINAL 1
+#define AUTOINCREMENT 1
+
+#define CHUNK_SIZE 64
+#define TOTAL_LEN_LEN 8
+
+/*
+ * ABOUT bool: this file does not use bool in order to be as pre-C99 compatible as possible.
+ */
+
+/*
+ * Comments from pseudo-code at https://en.wikipedia.org/wiki/SHA-2 are reproduced here.
+ * When useful for clarification, portions of the pseudo-code are reproduced here too.
+ */
+
+/*
+ * Initialize array of round constants:
+ * (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
+ */
+static const uint32_t k[] = {
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
+
+struct buffer_state {
+ const uint8_t * p;
+ size_t len;
+ size_t total_len;
+ int single_one_delivered; /* bool */
+ int total_len_delivered; /* bool */
+};
+
+static inline uint32_t right_rot(uint32_t value, unsigned int count)
+{
+ /*
+ * Defined behaviour in standard C for all count where 0 < count < 32,
+ * which is what we need here.
+ */
+ return value >> count | value << (32 - count);
+}
+
+/* BEGIN DM */
+#define DEF_ROT(n) \
+static inline uint32_t right_rot##n(uint32_t value) \
+{ \
+ return value >> n | value << (32 - n); \
+}
+DEF_ROT(2)
+DEF_ROT(6)
+DEF_ROT(7)
+DEF_ROT(11)
+DEF_ROT(13)
+DEF_ROT(17)
+DEF_ROT(18)
+DEF_ROT(19)
+DEF_ROT(22)
+DEF_ROT(25)
+/* END DM */
+
+static void init_buf_state(struct buffer_state * state, const void * input, size_t len)
+{
+ state->p = input;
+ state->len = len;
+ state->total_len = len;
+ state->single_one_delivered = 0;
+ state->total_len_delivered = 0;
+}
+
+/* Return value: bool */
+static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
+{
+ size_t space_in_chunk;
+
+ if (state->total_len_delivered) {
+ return 0;
+ }
+
+ if (state->len >= CHUNK_SIZE) {
+ my_memcpy(chunk, state->p, CHUNK_SIZE);
+ state->p += CHUNK_SIZE;
+ state->len -= CHUNK_SIZE;
+ return 1;
+ }
+
+ memcpy(chunk, state->p, state->len);
+ chunk += state->len;
+ space_in_chunk = CHUNK_SIZE - state->len;
+ state->p += state->len;
+ state->len = 0;
+
+ /* If we are here, space_in_chunk is one at minimum. */
+ if (!state->single_one_delivered) {
+ *chunk++ = 0x80;
+ space_in_chunk -= 1;
+ state->single_one_delivered = 1;
+ }
+
+ /*
+ * Now:
+ * - either there is enough space left for the total length, and we can conclude,
+ * - or there is too little space left, and we have to pad the rest of this chunk with zeroes.
+ * In the latter case, we will conclude at the next invokation of this function.
+ */
+ if (space_in_chunk >= TOTAL_LEN_LEN) {
+ const size_t left = space_in_chunk - TOTAL_LEN_LEN;
+ size_t len = state->total_len;
+ int i;
+ memset(chunk, 0x00, left);
+ chunk += left;
+
+ /* Storing of len * 8 as a big endian 64-bit without overflow. */
+ chunk[7] = (uint8_t) (len << 3);
+ len >>= 5;
+ for (i = 6; i >= 0; i--) {
+ chunk[i] = (uint8_t) len;
+ len >>= 8;
+ }
+ state->total_len_delivered = 1;
+ } else {
+ memset(chunk, 0x00, space_in_chunk);
+ }
+
+ return 1;
+}
+
+/*
+ * Limitations:
+ * - Since input is a pointer in RAM, the data to hash should be in RAM, which could be a problem
+ * for large data sizes.
+ * - SHA algorithms theoretically operate on bit strings. However, this implementation has no support
+ * for bit string lengths that are not multiples of eight, and it really operates on arrays of bytes.
+ * In particular, the len parameter is a number of bytes.
+ */
+
+#if USE_ORIGINAL
+void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
+{
+ TIMEINIT(3)
+ /*
+ * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
+ * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
+ * Note 3: The compression function uses 8 working variables, a through h
+ * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
+ * and when parsing message block data from bytes to words, for example,
+ * the first word of the input message "abc" after padding is 0x61626380
+ */
+
+ /*
+ * Initialize hash values:
+ * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
+ */
+ uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
+ int i, j;
+
+ /* 512-bit chunks is what we will operate on. */
+ uint8_t chunk[64];
+
+ struct buffer_state state;
+
+ init_buf_state(&state, input, len);
+ TIMEINIT(0)
+ while (calc_chunk(chunk, &state)) {
+ uint32_t ah[8];
+
+ /*
+ * create a 64-entry message schedule array w[0..63] of 32-bit words
+ * (The initial values in w[0..63] don't matter, so many implementations zero them here)
+ * copy chunk into first 16 words w[0..15] of the message schedule array
+ */
+ uint32_t w[64];
+ const uint8_t *p = chunk;
+
+ memset(w, 0x00, sizeof w);
+ for (i = 0; i < 16; i++) {
+ w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
+ (uint32_t) p[2] << 8 | (uint32_t) p[3];
+ p += 4;
+ }
+
+ /* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
+ for (i = 16; i < 64; i++) {
+ const uint32_t s0 = right_rot7(w[i - 15]) ^ right_rot18(w[i - 15]) ^ (w[i - 15] >> 3);
+ const uint32_t s1 = right_rot17(w[i - 2]) ^ right_rot19(w[i - 2]) ^ (w[i - 2] >> 10);
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
+ }
+
+ /* Initialize working variables to current hash value: */
+ for (i = 0; i < 8; i++)
+ ah[i] = h[i];
+
+ /* Compression function main loop: */
+ for (i = 0; i < 64; i++) {
+ //TIMEINIT(4)
+ const uint32_t s1 = right_rot6(ah[4]) ^ right_rot11(ah[4]) ^ right_rot25(ah[4]);
+ const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]);
+ const uint32_t temp1 = ah[7] + s1 + ch + k[i] + w[i];
+ const uint32_t s0 = right_rot2(ah[0]) ^ right_rot13(ah[0]) ^ right_rot22(ah[0]);
+ const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]);
+ const uint32_t temp2 = s0 + maj;
+ //TIMESTOP(4) TIMEINIT(5)
+ ah[7] = ah[6];
+ ah[6] = ah[5];
+ ah[5] = ah[4];
+ ah[4] = ah[3] + temp1;
+ ah[3] = ah[2];
+ ah[2] = ah[1];
+ ah[1] = ah[0];
+ ah[0] = temp1 + temp2;
+ //TIMESTOP(5)
+ }
+
+ /* Add the compressed chunk to the current hash value: */
+ for (i = 0; i < 8; i++)
+ h[i] += ah[i];
+ TIMESTOP(0)
+ }
+
+ TIMEINIT(2)
+ /* Produce the final hash value (big-endian): */
+ for (i = 0, j = 0; i < 8; i++)
+ {
+ hash[j++] = (uint8_t) (h[i] >> 24);
+ hash[j++] = (uint8_t) (h[i] >> 16);
+ hash[j++] = (uint8_t) (h[i] >> 8);
+ hash[j++] = (uint8_t) h[i];
+ TIMESTOP(2)
+ }
+ TIMESTOP(3)
+}
+#else
+/* Modified by D. Monniaux */
+void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
+{
+ /*
+ * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
+ * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
+ * Note 3: The compression function uses 8 working variables, a through h
+ * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
+ * and when parsing message block data from bytes to words, for example,
+ * the first word of the input message "abc" after padding is 0x61626380
+ */
+
+ /*
+ * Initialize hash values:
+ * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
+ */
+ uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
+ uint32_t h0 = h[0];
+ uint32_t h1 = h[1];
+ uint32_t h2 = h[2];
+ uint32_t h3 = h[3];
+ uint32_t h4 = h[4];
+ uint32_t h5 = h[5];
+ uint32_t h6 = h[6];
+ uint32_t h7 = h[7];
+ int i, j;
+
+ /* 512-bit chunks is what we will operate on. */
+ uint8_t chunk[64];
+
+ struct buffer_state state;
+
+ init_buf_state(&state, input, len);
+
+ while (calc_chunk(chunk, &state)) {
+ uint32_t ah0, ah1, ah2, ah3, ah4, ah5, ah6, ah7;
+
+ /*
+ * create a 64-entry message schedule array w[0..63] of 32-bit words
+ * (The initial values in w[0..63] don't matter, so many implementations zero them here)
+ * copy chunk into first 16 words w[0..15] of the message schedule array
+ */
+ uint32_t w[64];
+ const uint8_t *p = chunk;
+
+ memset(w, 0x00, sizeof w);
+#ifndef SKIP_SLOW_PARTS
+ for (i = 0; i < 16; i++) {
+ w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
+ (uint32_t) p[2] << 8 | (uint32_t) p[3];
+ p += 4;
+ }
+
+ /* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
+ /* DM this is a SLOW part with ccomp; awkward address computations. */
+ for (i = 16; i < 64; i++) {
+ const uint32_t s0 = right_rot7(w[i - 15]) ^ right_rot18(w[i - 15]) ^ (w[i - 15] >> 3);
+ const uint32_t s1 = right_rot17(w[i - 2]) ^ right_rot19(w[i - 2]) ^ (w[i - 2] >> 10);
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
+ }
+#endif
+ /* Initialize working variables to current hash value: */
+ ah0 = h0;
+ ah1 = h1;
+ ah2 = h2;
+ ah3 = h3;
+ ah4 = h4;
+ ah5 = h5;
+ ah6 = h6;
+ ah7 = h7;
+
+ /* Compression function main loop: */
+#if AUTOINCREMENT
+ const uint32_t *ki=k, *wi=w;
+#define KI *ki
+#define WI *wi
+#define STEP i++; ki++; wi++;
+#else
+#define KI k[i]
+#define WI w[i]
+#define STEP i++;
+#endif
+ for (i = 0; i < 64; ) {
+#define CHUNK \
+ { \
+ const uint32_t s1 = right_rot6(ah4) ^ right_rot11(ah4) ^ right_rot25(ah4); \
+ const uint32_t ch = (ah4 & ah5) ^ (~ah4 & ah6); \
+ const uint32_t temp1 = ah7 + s1 + ch + KI + WI; \
+ const uint32_t s0 = right_rot2(ah0) ^ right_rot13(ah0) ^ right_rot22(ah0); \
+ const uint32_t maj = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2); \
+ const uint32_t temp2 = s0 + maj; \
+ \
+ ah7 = ah6; \
+ ah6 = ah5; \
+ ah5 = ah4; \
+ ah4 = ah3 + temp1; \
+ ah3 = ah2; \
+ ah2 = ah1; \
+ ah1 = ah0; \
+ ah0 = temp1 + temp2; \
+ STEP \
+ }
+ CHUNK
+ CHUNK
+ }
+
+ /* Add the compressed chunk to the current hash value: */
+ h0 += ah0;
+ h1 += ah1;
+ h2 += ah2;
+ h3 += ah3;
+ h4 += ah4;
+ h5 += ah5;
+ h6 += ah6;
+ h7 += ah7;
+ }
+ h[0]=h0;
+ h[1]=h1;
+ h[2]=h2;
+ h[3]=h3;
+ h[4]=h4;
+ h[5]=h5;
+ h[6]=h6;
+ h[7]=h7;
+
+ /* Produce the final hash value (big-endian): */
+ for (i = 0, j = 0; i < 8; i++)
+ {
+ hash[j++] = (uint8_t) (h[i] >> 24);
+ hash[j++] = (uint8_t) (h[i] >> 16);
+ hash[j++] = (uint8_t) (h[i] >> 8);
+ hash[j++] = (uint8_t) h[i];
+ }
+}
+#endif
+
+void print_all(void){
+ TIMEPRINT(5)
+}
diff --git a/test/monniaux/sandbox/sha-256.h b/test/monniaux/sandbox/sha-256.h
new file mode 100644
index 00000000..0753ea9e
--- /dev/null
+++ b/test/monniaux/sandbox/sha-256.h
@@ -0,0 +1,2 @@
+void calc_sha_256(uint8_t hash[32], const void *input, size_t len);
+void print_all(void);
diff --git a/test/monniaux/sandbox/sha-256_run.c b/test/monniaux/sandbox/sha-256_run.c
new file mode 100644
index 00000000..a1631bc6
--- /dev/null
+++ b/test/monniaux/sandbox/sha-256_run.c
@@ -0,0 +1,286 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "../cycles.h"
+#include "sha-256.h"
+
+struct string_vector {
+ const char *input;
+ const char *output;
+};
+
+static const struct string_vector STRING_VECTORS[] = {
+ {
+ "",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ },
+ {
+ "abc",
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
+ "a8ae6e6ee929abea3afcfc5258c8ccd6f85273e0d4626d26c7279f3250f77c8e"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde",
+ "057ee79ece0b9a849552ab8d3c335fe9a5f1c46ef5f1d9b190c295728628299c"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
+ "2a6ad82f3620d3ebe9d678c812ae12312699d673240d5be8fac0910a70000d93"
+ },
+ {
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
+ },
+ {
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
+ "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
+ }
+};
+
+#define LARGE_MESSAGES 1
+#define LARGER_MESSAGES 0
+
+static uint8_t data1[] = { 0xbd };
+static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 };
+static uint8_t data7[1000];
+static uint8_t data8[1000];
+static uint8_t data9[1005];
+#if LARGE_MESSAGES
+#define SIZEOF_DATA11 536870912
+#define SIZEOF_DATA12 1090519040
+#define SIZEOF_DATA13 1610612798
+static uint8_t * data11;
+static uint8_t * data12;
+static uint8_t * data13;
+#endif
+
+struct vector {
+ const uint8_t *input;
+ size_t input_len;
+ const char *output;
+};
+
+static struct vector vectors[] = {
+ {
+ data1,
+ sizeof data1,
+ "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b"
+ },
+ {
+ data2,
+ sizeof data2,
+ "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504"
+ },
+ {
+ data7,
+ 55,
+ "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7"
+ },
+ {
+ data7,
+ 56,
+ "d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb"
+ },
+ {
+ data7,
+ 57,
+ "65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785"
+ },
+ {
+ data7,
+ 64,
+ "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
+ },
+ {
+ data7,
+ sizeof data7,
+ "541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53"
+ },
+ {
+ data8,
+ sizeof data8,
+ "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4"
+ },
+ {
+ data9,
+ sizeof data9,
+ "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0"
+ },
+#if LARGE_MESSAGES
+ {
+ NULL,
+ /* too big
+ 1000000,
+ "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025"
+ */
+ 50000,
+ "5b4b67b5d68e02c992760de07640472efe53a7f7553865f83262d0a74efc3e5d"
+ },
+#if LARGER_MESSAGES
+ {
+ NULL,
+ SIZEOF_DATA11,
+ "15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd"
+ },
+ {
+ NULL,
+ SIZEOF_DATA12,
+ "461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53"
+ },
+ {
+ NULL,
+ SIZEOF_DATA13,
+ "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea"
+ }
+#endif
+#endif
+};
+
+#if LARGE_MESSAGES
+static void *my_malloc(size_t size) {
+ void *p=malloc(size);
+ if (p==0) {
+ fprintf(stderr, "malloc(%zu) failed\n", size);
+ abort();
+ }
+ return p;
+}
+#endif
+
+static void construct_binary_messages(void)
+{
+ memset(data7, 0x00, sizeof data7);
+ memset(data8, 0x41, sizeof data8);
+ memset(data9, 0x55, sizeof data9);
+#if LARGE_MESSAGES
+#if LARGER_MESSAGES
+ /*
+ * Heap allocation as a workaround for some linkers not liking
+ * large BSS segments.
+ */
+ data11 = my_malloc(SIZEOF_DATA11);
+ data12 = my_malloc(SIZEOF_DATA12);
+ data13 = my_malloc(SIZEOF_DATA13);
+ memset(data11, 0x5a, SIZEOF_DATA11);
+ memset(data12, 0x00, SIZEOF_DATA12);
+ memset(data13, 0x42, SIZEOF_DATA13);
+ vectors[9].input = data12;
+ vectors[10].input = data11;
+ vectors[11].input = data12;
+ vectors[12].input = data13;
+#else
+ vectors[9].input = data12 = my_malloc(vectors[9].input_len);
+ memset(data12, 0x00, vectors[9].input_len);
+#endif
+#endif
+}
+
+static void destruct_binary_messages(void)
+{
+#if LARGE_MESSAGES
+#if LARGER_MESSAGES
+ free(data11);
+ free(data12);
+ free(data13);
+#else
+ free(data12);
+#endif
+#endif
+}
+
+static void hash_to_string(char string[65], const uint8_t hash[32])
+{
+ size_t i;
+ for (i = 0; i < 32; i++) {
+ string += sprintf(string, "%02x", hash[i]);
+ }
+}
+
+static cycle_t cycle_total, cycle_start_time;
+
+static void cycle_count_start(void) {
+ cycle_start_time=get_cycle();
+}
+
+static void cycle_count_end(void) {
+ cycle_total += get_cycle()-cycle_start_time;
+}
+
+static int string_test(const char input[], const char output[])
+{
+ uint8_t hash[32];
+ char hash_string[65];
+
+ cycle_count_start();
+ calc_sha_256(hash, input, strlen(input));
+ cycle_count_end();
+
+ hash_to_string(hash_string, hash);
+ printf("input: %s\n", input);
+ printf("hash : %s\n", hash_string);
+ if (strcmp(output, hash_string)) {
+ printf("FAILURE!\n\n");
+ return 1;
+ } else {
+ printf("SUCCESS!\n\n");
+ return 0;
+ }
+}
+
+/*
+ * Limitation:
+ * - The variable input_len will be truncated to its LONG_BIT least
+ * significant bits in the print output. This will never be a problem
+ * for values that in practice are less than 2^32 - 1. Rationale: ANSI
+ * C-compatibility and keeping it simple.
+ */
+static int test(const uint8_t * input, size_t input_len, const char output[])
+{
+ uint8_t hash[32];
+ char hash_string[65];
+
+ cycle_count_start();
+ calc_sha_256(hash, input, input_len);
+ cycle_count_end();
+
+ hash_to_string(hash_string, hash);
+ printf("input starts with 0x%02x, length %lu\n", *input, (unsigned long) input_len);
+ printf("hash : %s\n", hash_string);
+ if (strcmp(output, hash_string)) {
+ printf("FAILURE!\n\n");
+ return 1;
+ } else {
+ printf("SUCCESS!\n\n");
+ return 0;
+ }
+}
+
+int main(void)
+{
+ cycle_count_config();
+ size_t i;
+ for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) {
+ const struct string_vector *vector = &STRING_VECTORS[i];
+ if (string_test(vector->input, vector->output))
+ {} /* DM return 1; */
+ }
+ construct_binary_messages();
+ for (i = 0; i < (sizeof vectors / sizeof (struct vector)); i++) {
+ const struct vector *vector = &vectors[i];
+ if (test(vector->input, vector->input_len, vector->output))
+ { /* DM
+ destruct_binary_messages();
+ return 1; */
+ }
+ }
+ destruct_binary_messages();
+ printf("total cycles : %" PRIu64 "\n", cycle_total);
+ print_all();
+ return 0;
+}
diff --git a/test/monniaux/scheduling/mal_schedule.c b/test/monniaux/scheduling/mal_schedule.c
new file mode 100644
index 00000000..a6ba967f
--- /dev/null
+++ b/test/monniaux/scheduling/mal_schedule.c
@@ -0,0 +1,14 @@
+#include <stdint.h>
+int16_t meuh;
+extern int uv_encode(double, double, int);
+void f(int *ab, int e) {
+ uint32_t *ao = (uint32_t *)ab;
+ int16_t *aq = &meuh;
+ while (e) {
+ int ar, as;
+ ar = 1. / 2147483647;
+ as = uv_encode(5, *aq, *ab);
+ if (as)
+ *ao++ = ar;
+ }
+}
diff --git a/test/monniaux/send_through/Makefile b/test/monniaux/send_through/Makefile
new file mode 100644
index 00000000..b2ab4e8e
--- /dev/null
+++ b/test/monniaux/send_through/Makefile
@@ -0,0 +1,10 @@
+send_through: send_through_gcc.kvx.o send_through_ccomp.kvx.o
+ ../../../ccomp -Wall $+ -o $@ -lm
+
+send_through_gcc.kvx.o send_through_ccomp.kvx.o: send_through.h
+
+send_through_gcc.kvx.o : send_through_gcc.c
+ k1-cos-gcc -Wall -Wextra -std=c99 -Werror=implicit -c $< -o $@
+
+send_through_ccomp.kvx.o : send_through_ccomp.c
+ ../../../ccomp -Wall -fnone -fvararg-calls -c $< -o $@
diff --git a/test/monniaux/send_through/send_through.h b/test/monniaux/send_through/send_through.h
new file mode 100644
index 00000000..03714fec
--- /dev/null
+++ b/test/monniaux/send_through/send_through.h
@@ -0,0 +1,6 @@
+typedef double op_int_double(int, double);
+typedef float op_int_float(int, float);
+
+double send_through_double(op_int_double f, int x, int y, double z);
+float send_through_float(op_int_float f, int x, int y, float z);
+void print_from_ccomp(double x);
diff --git a/test/monniaux/send_through/send_through_ccomp.c b/test/monniaux/send_through/send_through_ccomp.c
new file mode 100644
index 00000000..a683dd82
--- /dev/null
+++ b/test/monniaux/send_through/send_through_ccomp.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <math.h>
+#include "send_through.h"
+
+double send_through_double(op_int_double f, int x, int y, double z) {
+ double w= f(x, f(y, z));
+ int mu = 1;
+ return w;
+}
+
+float send_through_float(op_int_float f, int x, int y, float z) {
+ float w= f(x, f(y, z));
+ int mu = 1;
+ return w;
+}
+
+void print_from_ccomp(double x) {
+ printf("x=%e x=%f x=%g x=%.03e x=%.03f x=%.03g x[rounded]=%ld\n",
+ x, x, x, x, x, x, lrint(x));
+}
diff --git a/test/monniaux/send_through/send_through_gcc.c b/test/monniaux/send_through/send_through_gcc.c
new file mode 100644
index 00000000..83541cee
--- /dev/null
+++ b/test/monniaux/send_through/send_through_gcc.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include "send_through.h"
+
+double sum_int_double(int x, double y) {
+ return x + y;
+}
+
+float sum_int_float(int x, float y) {
+ return x + y;
+}
+
+int main() {
+ double x = send_through_double(sum_int_double, 2, 3, 4.5);
+ float y = send_through_float(sum_int_float, 2, 3, 4.5f);
+ printf("x[gcc] = %f\n", x);
+ printf("y[gcc] = %f\n", y);
+ print_from_ccomp(x);
+}
diff --git a/test/monniaux/sha-2/LICENSE b/test/monniaux/sha-2/LICENSE
new file mode 100644
index 00000000..cf1ab25d
--- /dev/null
+++ b/test/monniaux/sha-2/LICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org>
diff --git a/test/monniaux/sha-2/Makefile b/test/monniaux/sha-2/Makefile
new file mode 100644
index 00000000..a043d89f
--- /dev/null
+++ b/test/monniaux/sha-2/Makefile
@@ -0,0 +1,3 @@
+TARGET=sha-256
+
+include ../rules.mk
diff --git a/test/monniaux/sha-2/README.md b/test/monniaux/sha-2/README.md
new file mode 100644
index 00000000..bd975955
--- /dev/null
+++ b/test/monniaux/sha-2/README.md
@@ -0,0 +1,99 @@
+# sha-2 [![Build Status](https://travis-ci.org/amosnier/sha-2.svg?branch=master)](https://travis-ci.org/amosnier/sha-2)
+
+https://github.com/amosnier/sha-2
+
+## Contents
+
+SHA-2 algorithm implementations.
+
+At the moment, only SHA-256 is implemented.
+
+## Design criteria
+
+- Easy to test, include in any project, compile and link.
+
+- ANSI C with as little specific C99 as possible (e.g. extended
+ integer types are used, but not bool).
+
+- Portable. Makes no assumptions on the target system's endianess or
+ word size.
+
+- The SHA-256 implementation is a straightforward implementation of
+ the algorithm specified on
+ [Wikipedia](https://en.wikipedia.org/wiki/SHA-2).
+
+## Notes
+
+The Makefile is as minimal as possible. No effort was put into making
+it general. Its purpose is mainly to ease testing for the developer's
+host machine. The actual implementation is however extremely easy to
+include in any project, may it use GNU make or any other build tool.
+
+## Code review
+
+This code has been reviewed at [Stack Exchange CODE
+REVIEW](https://codereview.stackexchange.com/questions/182812/self-contained-sha-256-implementation-in-c),
+and the implementation has been improved accordingly.
+
+## Testing
+
+Testing is continuously performed on Travis CI (see above).
+
+Apart from that, the implementation has been successfully tested on an x86-64 machine
+under Linux as well as on a 16-bit DSP. On the x86-64 machine, all the
+available NIST test vectors where successfully tested ([SHA-256
+examples](https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA256.pdf)
+and [SHA-2 Additional
+examples](https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA2_Additional.pdf),
+plus a few others).
+
+In particular:
+
+```
+Input Message: "abc"
+Message Digest is BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD
+```
+
+```
+Input Message: "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+Message Digest is 248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1
+```
+
+```
+SHA-256 Test Data
+#1) 1 byte 0xbd
+68325720 aabd7c82 f30f554b 313d0570 c95accbb 7dc4b5aa e11204c0 8ffe732b
+#2) 4 bytes 0xc98c8e55
+7abc22c0 ae5af26c e93dbb94 433a0e0b 2e119d01 4f8e7f65 bd56c61c cccd9504
+#3) 55 bytes of zeros
+02779466 cdec1638 11d07881 5c633f21 90141308 1449002f 24aa3e80 f0b88ef7
+#4) 56 bytes of zeros
+d4817aa5 497628e7 c77e6b60 6107042b bba31308 88c5f47a 375e6179 be789fbb
+#5) 57 bytes of zeros
+65a16cb7 861335d5 ace3c607 18b5052e 44660726 da4cd13b b745381b 235a1785
+#6) 64 bytes of zeros
+f5a5fd42 d16a2030 2798ef6e d309979b 43003d23 20d9f0e8 ea9831a9 2759fb4b
+#7) 1000 bytes of zeros
+541b3e9d aa09b20b f85fa273 e5cbd3e8 0185aa4e c298e765 db87742b 70138a53
+#8) 1000 bytes of 0x41 ‘A’
+c2e68682 3489ced2 017f6059 b8b23931 8b6364f6 dcd835d0 a519105a 1eadd6e4
+#9) 1005 bytes of 0x55 ‘U’
+f4d62dde c0f3dd90 ea1380fa 16a5ff8d c4c54b21 740650f2 4afc4120 903552b0
+#10) 1000000 bytes of zeros
+d29751f2 649b32ff 572b5e0a 9f541ea6 60a50f94 ff0beedf b0b692b9 24cc8025
+#11) 0x20000000 (536870912) bytes of 0x5a ‘Z’
+15a1868c 12cc5395 1e182344 277447cd 0979536b adcc512a d24c67e9 b2d4f3dd
+#12) 0x41000000 (1090519040) bytes of zeros
+461c19a9 3bd4344f 9215f5ec 64357090 342bc66b 15a14831 7d276e31 cbc20b53
+#13) 0x6000003e (1610612798) bytes of 0x42 ‘B’
+c23ce8a7 895f4b21 ec0daf37 920ac0a2 62a22004 5a03eb2d fed48ef9 b05aabea
+```
+
+## License
+
+This repository is made available in the public domain. See [LICENSE
+FILE](LICENSE).
+
+## Reference implementation
+
+I had missed that when I made this implementation but [RFC 6234, chapter 8](https://tools.ietf.org/html/rfc6234#section-8) actually includes a reference implementation in C that is (at least in ambition) broader in scope than this one. I have however neither compiled nor tested it.
diff --git a/test/monniaux/sha-2/sha-256.c b/test/monniaux/sha-2/sha-256.c
new file mode 100644
index 00000000..63ac09b7
--- /dev/null
+++ b/test/monniaux/sha-2/sha-256.c
@@ -0,0 +1,374 @@
+#include <stdint.h>
+#include <string.h>
+#if 0 /* __COMPCERT__ */
+#define my_memcpy(dst, src, size) __builtin_memcpy_aligned(dst, src, size, 1)
+#else
+#define my_memcpy(dst, src, size) memcpy(dst, src, size)
+#endif
+
+#include "sha-256.h"
+
+#define USE_ORIGINAL 1
+#define AUTOINCREMENT 1
+
+#define CHUNK_SIZE 64
+#define TOTAL_LEN_LEN 8
+
+/*
+ * ABOUT bool: this file does not use bool in order to be as pre-C99 compatible as possible.
+ */
+
+/*
+ * Comments from pseudo-code at https://en.wikipedia.org/wiki/SHA-2 are reproduced here.
+ * When useful for clarification, portions of the pseudo-code are reproduced here too.
+ */
+
+/*
+ * Initialize array of round constants:
+ * (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
+ */
+static const uint32_t k[] = {
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
+
+struct buffer_state {
+ const uint8_t * p;
+ size_t len;
+ size_t total_len;
+ int single_one_delivered; /* bool */
+ int total_len_delivered; /* bool */
+};
+
+static inline uint32_t right_rot(uint32_t value, unsigned int count)
+{
+ /*
+ * Defined behaviour in standard C for all count where 0 < count < 32,
+ * which is what we need here.
+ */
+ return value >> count | value << (32 - count);
+}
+
+/* BEGIN DM */
+#define DEF_ROT(n) \
+static inline uint32_t right_rot##n(uint32_t value) \
+{ \
+ return value >> n | value << (32 - n); \
+}
+DEF_ROT(2)
+DEF_ROT(6)
+DEF_ROT(7)
+DEF_ROT(11)
+DEF_ROT(13)
+DEF_ROT(17)
+DEF_ROT(18)
+DEF_ROT(19)
+DEF_ROT(22)
+DEF_ROT(25)
+/* END DM */
+
+static void init_buf_state(struct buffer_state * state, const void * input, size_t len)
+{
+ state->p = input;
+ state->len = len;
+ state->total_len = len;
+ state->single_one_delivered = 0;
+ state->total_len_delivered = 0;
+}
+
+/* Return value: bool */
+static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
+{
+ size_t space_in_chunk;
+
+ if (state->total_len_delivered) {
+ return 0;
+ }
+
+ if (state->len >= CHUNK_SIZE) {
+ my_memcpy(chunk, state->p, CHUNK_SIZE);
+ state->p += CHUNK_SIZE;
+ state->len -= CHUNK_SIZE;
+ return 1;
+ }
+
+ memcpy(chunk, state->p, state->len);
+ chunk += state->len;
+ space_in_chunk = CHUNK_SIZE - state->len;
+ state->p += state->len;
+ state->len = 0;
+
+ /* If we are here, space_in_chunk is one at minimum. */
+ if (!state->single_one_delivered) {
+ *chunk++ = 0x80;
+ space_in_chunk -= 1;
+ state->single_one_delivered = 1;
+ }
+
+ /*
+ * Now:
+ * - either there is enough space left for the total length, and we can conclude,
+ * - or there is too little space left, and we have to pad the rest of this chunk with zeroes.
+ * In the latter case, we will conclude at the next invokation of this function.
+ */
+ if (space_in_chunk >= TOTAL_LEN_LEN) {
+ const size_t left = space_in_chunk - TOTAL_LEN_LEN;
+ size_t len = state->total_len;
+ int i;
+ memset(chunk, 0x00, left);
+ chunk += left;
+
+ /* Storing of len * 8 as a big endian 64-bit without overflow. */
+ chunk[7] = (uint8_t) (len << 3);
+ len >>= 5;
+ for (i = 6; i >= 0; i--) {
+ chunk[i] = (uint8_t) len;
+ len >>= 8;
+ }
+ state->total_len_delivered = 1;
+ } else {
+ memset(chunk, 0x00, space_in_chunk);
+ }
+
+ return 1;
+}
+
+/*
+ * Limitations:
+ * - Since input is a pointer in RAM, the data to hash should be in RAM, which could be a problem
+ * for large data sizes.
+ * - SHA algorithms theoretically operate on bit strings. However, this implementation has no support
+ * for bit string lengths that are not multiples of eight, and it really operates on arrays of bytes.
+ * In particular, the len parameter is a number of bytes.
+ */
+
+#if USE_ORIGINAL
+void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
+{
+ /*
+ * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
+ * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
+ * Note 3: The compression function uses 8 working variables, a through h
+ * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
+ * and when parsing message block data from bytes to words, for example,
+ * the first word of the input message "abc" after padding is 0x61626380
+ */
+
+ /*
+ * Initialize hash values:
+ * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
+ */
+ uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
+ int i, j;
+
+ /* 512-bit chunks is what we will operate on. */
+ uint8_t chunk[64];
+
+ struct buffer_state state;
+
+ init_buf_state(&state, input, len);
+
+ while (calc_chunk(chunk, &state)) {
+ uint32_t ah[8];
+
+ /*
+ * create a 64-entry message schedule array w[0..63] of 32-bit words
+ * (The initial values in w[0..63] don't matter, so many implementations zero them here)
+ * copy chunk into first 16 words w[0..15] of the message schedule array
+ */
+ uint32_t w[64];
+ const uint8_t *p = chunk;
+
+ memset(w, 0x00, sizeof w);
+ for (i = 0; i < 16; i++) {
+ w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
+ (uint32_t) p[2] << 8 | (uint32_t) p[3];
+ p += 4;
+ }
+
+ /* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
+ for (i = 16; i < 64; i++) {
+ const uint32_t s0 = right_rot7(w[i - 15]) ^ right_rot18(w[i - 15]) ^ (w[i - 15] >> 3);
+ const uint32_t s1 = right_rot17(w[i - 2]) ^ right_rot19(w[i - 2]) ^ (w[i - 2] >> 10);
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
+ }
+
+ /* Initialize working variables to current hash value: */
+ for (i = 0; i < 8; i++)
+ ah[i] = h[i];
+
+ /* Compression function main loop: */
+ for (i = 0; i < 64; i++) {
+ const uint32_t s1 = right_rot6(ah[4]) ^ right_rot11(ah[4]) ^ right_rot25(ah[4]);
+ const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]);
+ const uint32_t temp1 = ah[7] + s1 + ch + k[i] + w[i];
+ const uint32_t s0 = right_rot2(ah[0]) ^ right_rot13(ah[0]) ^ right_rot22(ah[0]);
+ const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]);
+ const uint32_t temp2 = s0 + maj;
+
+ ah[7] = ah[6];
+ ah[6] = ah[5];
+ ah[5] = ah[4];
+ ah[4] = ah[3] + temp1;
+ ah[3] = ah[2];
+ ah[2] = ah[1];
+ ah[1] = ah[0];
+ ah[0] = temp1 + temp2;
+ }
+
+ /* Add the compressed chunk to the current hash value: */
+ for (i = 0; i < 8; i++)
+ h[i] += ah[i];
+ }
+
+ /* Produce the final hash value (big-endian): */
+ for (i = 0, j = 0; i < 8; i++)
+ {
+ hash[j++] = (uint8_t) (h[i] >> 24);
+ hash[j++] = (uint8_t) (h[i] >> 16);
+ hash[j++] = (uint8_t) (h[i] >> 8);
+ hash[j++] = (uint8_t) h[i];
+ }
+}
+#else
+/* Modified by D. Monniaux */
+void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
+{
+ /*
+ * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
+ * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
+ * Note 3: The compression function uses 8 working variables, a through h
+ * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
+ * and when parsing message block data from bytes to words, for example,
+ * the first word of the input message "abc" after padding is 0x61626380
+ */
+
+ /*
+ * Initialize hash values:
+ * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
+ */
+ uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
+ uint32_t h0 = h[0];
+ uint32_t h1 = h[1];
+ uint32_t h2 = h[2];
+ uint32_t h3 = h[3];
+ uint32_t h4 = h[4];
+ uint32_t h5 = h[5];
+ uint32_t h6 = h[6];
+ uint32_t h7 = h[7];
+ int i, j;
+
+ /* 512-bit chunks is what we will operate on. */
+ uint8_t chunk[64];
+
+ struct buffer_state state;
+
+ init_buf_state(&state, input, len);
+
+ while (calc_chunk(chunk, &state)) {
+ uint32_t ah0, ah1, ah2, ah3, ah4, ah5, ah6, ah7;
+
+ /*
+ * create a 64-entry message schedule array w[0..63] of 32-bit words
+ * (The initial values in w[0..63] don't matter, so many implementations zero them here)
+ * copy chunk into first 16 words w[0..15] of the message schedule array
+ */
+ uint32_t w[64];
+ const uint8_t *p = chunk;
+
+ memset(w, 0x00, sizeof w);
+#ifndef SKIP_SLOW_PARTS
+ for (i = 0; i < 16; i++) {
+ w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
+ (uint32_t) p[2] << 8 | (uint32_t) p[3];
+ p += 4;
+ }
+
+ /* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
+ /* DM this is a SLOW part with ccomp; awkward address computations. */
+ for (i = 16; i < 64; i++) {
+ const uint32_t s0 = right_rot7(w[i - 15]) ^ right_rot18(w[i - 15]) ^ (w[i - 15] >> 3);
+ const uint32_t s1 = right_rot17(w[i - 2]) ^ right_rot19(w[i - 2]) ^ (w[i - 2] >> 10);
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
+ }
+#endif
+ /* Initialize working variables to current hash value: */
+ ah0 = h0;
+ ah1 = h1;
+ ah2 = h2;
+ ah3 = h3;
+ ah4 = h4;
+ ah5 = h5;
+ ah6 = h6;
+ ah7 = h7;
+
+ /* Compression function main loop: */
+#if AUTOINCREMENT
+ const uint32_t *ki=k, *wi=w;
+#define KI *ki
+#define WI *wi
+#define STEP i++; ki++; wi++;
+#else
+#define KI k[i]
+#define WI w[i]
+#define STEP i++;
+#endif
+ for (i = 0; i < 64; ) {
+#define CHUNK \
+ { \
+ const uint32_t s1 = right_rot6(ah4) ^ right_rot11(ah4) ^ right_rot25(ah4); \
+ const uint32_t ch = (ah4 & ah5) ^ (~ah4 & ah6); \
+ const uint32_t temp1 = ah7 + s1 + ch + KI + WI; \
+ const uint32_t s0 = right_rot2(ah0) ^ right_rot13(ah0) ^ right_rot22(ah0); \
+ const uint32_t maj = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2); \
+ const uint32_t temp2 = s0 + maj; \
+ \
+ ah7 = ah6; \
+ ah6 = ah5; \
+ ah5 = ah4; \
+ ah4 = ah3 + temp1; \
+ ah3 = ah2; \
+ ah2 = ah1; \
+ ah1 = ah0; \
+ ah0 = temp1 + temp2; \
+ STEP \
+ }
+ CHUNK
+ CHUNK
+ }
+
+ /* Add the compressed chunk to the current hash value: */
+ h0 += ah0;
+ h1 += ah1;
+ h2 += ah2;
+ h3 += ah3;
+ h4 += ah4;
+ h5 += ah5;
+ h6 += ah6;
+ h7 += ah7;
+ }
+ h[0]=h0;
+ h[1]=h1;
+ h[2]=h2;
+ h[3]=h3;
+ h[4]=h4;
+ h[5]=h5;
+ h[6]=h6;
+ h[7]=h7;
+
+ /* Produce the final hash value (big-endian): */
+ for (i = 0, j = 0; i < 8; i++)
+ {
+ hash[j++] = (uint8_t) (h[i] >> 24);
+ hash[j++] = (uint8_t) (h[i] >> 16);
+ hash[j++] = (uint8_t) (h[i] >> 8);
+ hash[j++] = (uint8_t) h[i];
+ }
+}
+#endif
diff --git a/test/monniaux/sha-2/sha-256.h b/test/monniaux/sha-2/sha-256.h
new file mode 100644
index 00000000..47f06ebf
--- /dev/null
+++ b/test/monniaux/sha-2/sha-256.h
@@ -0,0 +1 @@
+void calc_sha_256(uint8_t hash[32], const void *input, size_t len);
diff --git a/test/monniaux/sha-2/sha-256_run.c b/test/monniaux/sha-2/sha-256_run.c
new file mode 100644
index 00000000..1b6f7372
--- /dev/null
+++ b/test/monniaux/sha-2/sha-256_run.c
@@ -0,0 +1,285 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "../cycles.h"
+#include "sha-256.h"
+
+struct string_vector {
+ const char *input;
+ const char *output;
+};
+
+static const struct string_vector STRING_VECTORS[] = {
+ {
+ "",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ },
+ {
+ "abc",
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
+ "a8ae6e6ee929abea3afcfc5258c8ccd6f85273e0d4626d26c7279f3250f77c8e"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde",
+ "057ee79ece0b9a849552ab8d3c335fe9a5f1c46ef5f1d9b190c295728628299c"
+ },
+ {
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
+ "2a6ad82f3620d3ebe9d678c812ae12312699d673240d5be8fac0910a70000d93"
+ },
+ {
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
+ },
+ {
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
+ "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
+ }
+};
+
+#define LARGE_MESSAGES 1
+#define LARGER_MESSAGES 0
+
+static uint8_t data1[] = { 0xbd };
+static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 };
+static uint8_t data7[1000];
+static uint8_t data8[1000];
+static uint8_t data9[1005];
+#if LARGE_MESSAGES
+#define SIZEOF_DATA11 536870912
+#define SIZEOF_DATA12 1090519040
+#define SIZEOF_DATA13 1610612798
+static uint8_t * data11;
+static uint8_t * data12;
+static uint8_t * data13;
+#endif
+
+struct vector {
+ const uint8_t *input;
+ size_t input_len;
+ const char *output;
+};
+
+static struct vector vectors[] = {
+ {
+ data1,
+ sizeof data1,
+ "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b"
+ },
+ {
+ data2,
+ sizeof data2,
+ "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504"
+ },
+ {
+ data7,
+ 55,
+ "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7"
+ },
+ {
+ data7,
+ 56,
+ "d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb"
+ },
+ {
+ data7,
+ 57,
+ "65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785"
+ },
+ {
+ data7,
+ 64,
+ "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
+ },
+ {
+ data7,
+ sizeof data7,
+ "541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53"
+ },
+ {
+ data8,
+ sizeof data8,
+ "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4"
+ },
+ {
+ data9,
+ sizeof data9,
+ "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0"
+ },
+#if LARGE_MESSAGES
+ {
+ NULL,
+ /* too big
+ 1000000,
+ "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025"
+ */
+ 50000,
+ "5b4b67b5d68e02c992760de07640472efe53a7f7553865f83262d0a74efc3e5d"
+ },
+#if LARGER_MESSAGES
+ {
+ NULL,
+ SIZEOF_DATA11,
+ "15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd"
+ },
+ {
+ NULL,
+ SIZEOF_DATA12,
+ "461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53"
+ },
+ {
+ NULL,
+ SIZEOF_DATA13,
+ "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea"
+ }
+#endif
+#endif
+};
+
+#if LARGE_MESSAGES
+static void *my_malloc(size_t size) {
+ void *p=malloc(size);
+ if (p==0) {
+ fprintf(stderr, "malloc(%zu) failed\n", size);
+ abort();
+ }
+ return p;
+}
+#endif
+
+static void construct_binary_messages(void)
+{
+ memset(data7, 0x00, sizeof data7);
+ memset(data8, 0x41, sizeof data8);
+ memset(data9, 0x55, sizeof data9);
+#if LARGE_MESSAGES
+#if LARGER_MESSAGES
+ /*
+ * Heap allocation as a workaround for some linkers not liking
+ * large BSS segments.
+ */
+ data11 = my_malloc(SIZEOF_DATA11);
+ data12 = my_malloc(SIZEOF_DATA12);
+ data13 = my_malloc(SIZEOF_DATA13);
+ memset(data11, 0x5a, SIZEOF_DATA11);
+ memset(data12, 0x00, SIZEOF_DATA12);
+ memset(data13, 0x42, SIZEOF_DATA13);
+ vectors[9].input = data12;
+ vectors[10].input = data11;
+ vectors[11].input = data12;
+ vectors[12].input = data13;
+#else
+ vectors[9].input = data12 = my_malloc(vectors[9].input_len);
+ memset(data12, 0x00, vectors[9].input_len);
+#endif
+#endif
+}
+
+static void destruct_binary_messages(void)
+{
+#if LARGE_MESSAGES
+#if LARGER_MESSAGES
+ free(data11);
+ free(data12);
+ free(data13);
+#else
+ free(data12);
+#endif
+#endif
+}
+
+static void hash_to_string(char string[65], const uint8_t hash[32])
+{
+ size_t i;
+ for (i = 0; i < 32; i++) {
+ string += sprintf(string, "%02x", hash[i]);
+ }
+}
+
+static cycle_t cycle_total, cycle_start_time;
+
+static void cycle_count_start(void) {
+ cycle_start_time=get_cycle();
+}
+
+static void cycle_count_end(void) {
+ cycle_total += get_cycle()-cycle_start_time;
+}
+
+static int string_test(const char input[], const char output[])
+{
+ uint8_t hash[32];
+ char hash_string[65];
+
+ cycle_count_start();
+ calc_sha_256(hash, input, strlen(input));
+ cycle_count_end();
+
+ hash_to_string(hash_string, hash);
+ printf("input: %s\n", input);
+ printf("hash : %s\n", hash_string);
+ if (strcmp(output, hash_string)) {
+ printf("FAILURE!\n\n");
+ return 1;
+ } else {
+ printf("SUCCESS!\n\n");
+ return 0;
+ }
+}
+
+/*
+ * Limitation:
+ * - The variable input_len will be truncated to its LONG_BIT least
+ * significant bits in the print output. This will never be a problem
+ * for values that in practice are less than 2^32 - 1. Rationale: ANSI
+ * C-compatibility and keeping it simple.
+ */
+static int test(const uint8_t * input, size_t input_len, const char output[])
+{
+ uint8_t hash[32];
+ char hash_string[65];
+
+ cycle_count_start();
+ calc_sha_256(hash, input, input_len);
+ cycle_count_end();
+
+ hash_to_string(hash_string, hash);
+ printf("input starts with 0x%02x, length %lu\n", *input, (unsigned long) input_len);
+ printf("hash : %s\n", hash_string);
+ if (strcmp(output, hash_string)) {
+ printf("FAILURE!\n\n");
+ return 1;
+ } else {
+ printf("SUCCESS!\n\n");
+ return 0;
+ }
+}
+
+int main(void)
+{
+ cycle_count_config();
+ size_t i;
+ for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) {
+ const struct string_vector *vector = &STRING_VECTORS[i];
+ if (string_test(vector->input, vector->output))
+ {} /* DM return 1; */
+ }
+ construct_binary_messages();
+ for (i = 0; i < (sizeof vectors / sizeof (struct vector)); i++) {
+ const struct vector *vector = &vectors[i];
+ if (test(vector->input, vector->input_len, vector->output))
+ { /* DM
+ destruct_binary_messages();
+ return 1; */
+ }
+ }
+ destruct_binary_messages();
+ printf("time cycles: %" PRIu64 "\n", cycle_total);
+ return 0;
+}
diff --git a/test/monniaux/sizeof/sizeof.c b/test/monniaux/sizeof/sizeof.c
new file mode 100644
index 00000000..9f283376
--- /dev/null
+++ b/test/monniaux/sizeof/sizeof.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+#define PRINT_SIZE(t) printf("%s\t%zd\n", #t, sizeof(t))
+#define PRINT_SIZES_INTN(n) \
+ PRINT_SIZE(int##n##_t); \
+ PRINT_SIZE(uint##n##_t); \
+ PRINT_SIZE(int_least##n##_t); \
+ PRINT_SIZE(uint_least##n##_t); \
+ PRINT_SIZE(int_fast##n##_t); \
+ PRINT_SIZE(uint_fast##n##_t)
+
+typedef void fun(void);
+
+int main() {
+ PRINT_SIZE(bool);
+ PRINT_SIZE(char);
+ PRINT_SIZE(short);
+ PRINT_SIZE(int);
+ PRINT_SIZE(long);
+ PRINT_SIZE(long long);
+
+ PRINT_SIZE(void*);
+ PRINT_SIZE(fun*);
+
+ PRINT_SIZE(float);
+ PRINT_SIZE(double);
+ PRINT_SIZE(long double);
+
+ PRINT_SIZE(ptrdiff_t);
+ PRINT_SIZE(size_t);
+ PRINT_SIZE(wchar_t);
+ PRINT_SIZE(intptr_t);
+ PRINT_SIZE(uintptr_t);
+ PRINT_SIZE(intmax_t);
+ PRINT_SIZE(uintmax_t);
+
+ PRINT_SIZES_INTN(8);
+ PRINT_SIZES_INTN(16);
+ PRINT_SIZES_INTN(32);
+ PRINT_SIZES_INTN(64);
+
+ return 0;
+}
diff --git a/test/monniaux/slow_globals/slow_globals.c b/test/monniaux/slow_globals/slow_globals.c
new file mode 100644
index 00000000..636f5861
--- /dev/null
+++ b/test/monniaux/slow_globals/slow_globals.c
@@ -0,0 +1,15 @@
+#include "../clock.h"
+
+extern void nothing(void);
+int variable;
+
+int main() {
+ clock_prepare();
+ clock_start();
+ for(int i=0; i<1000; i++) {
+ variable++;
+ nothing();
+ }
+ clock_stop();
+ print_total_clock();
+}
diff --git a/test/monniaux/slow_globals/slow_globals2.c b/test/monniaux/slow_globals/slow_globals2.c
new file mode 100644
index 00000000..b68a6ebf
--- /dev/null
+++ b/test/monniaux/slow_globals/slow_globals2.c
@@ -0,0 +1,2 @@
+void nothing(void) {
+}
diff --git a/test/monniaux/tacle-bench-lift/Makefile b/test/monniaux/tacle-bench-lift/Makefile
new file mode 100644
index 00000000..2e1db080
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/Makefile
@@ -0,0 +1,4 @@
+ALL_CFLAGS:=-include kill_pragma.h
+TARGET=lift
+
+include ../rules.mk
diff --git a/test/monniaux/tacle-bench-lift/README.md b/test/monniaux/tacle-bench-lift/README.md
new file mode 100644
index 00000000..dc2b1967
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/README.md
@@ -0,0 +1 @@
+From tacle-bench/bench/app/lift \ No newline at end of file
diff --git a/test/monniaux/tacle-bench-lift/kill_pragma.h b/test/monniaux/tacle-bench-lift/kill_pragma.h
new file mode 100644
index 00000000..a2fde5a9
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/kill_pragma.h
@@ -0,0 +1 @@
+#define _Pragma(x)
diff --git a/test/monniaux/tacle-bench-lift/lift.c b/test/monniaux/tacle-bench-lift/lift.c
new file mode 100644
index 00000000..5a810244
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/lift.c
@@ -0,0 +1,145 @@
+/* DM */
+#include "../clock.h"
+#include <stdio.h>
+
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 2.0
+
+ Name: lift
+
+ Author: Martin Schoeberl, Benedikt Huber
+
+ Function: Lift Controler
+
+ Source: C-Port from http://www.jopdesign.com/doc/jembench.pdf
+
+ Original name: run_lift.c
+
+ Changes: no major functional changes
+
+ License: GPL version 3 or later
+
+*/
+
+
+/*
+ Include section
+*/
+
+#include "liftlibio.h"
+#include "liftlibcontrol.h"
+
+
+/*
+ Forward declaration of functions
+*/
+
+void lift_controller();
+void lift_init();
+void lift_main();
+int lift_return();
+
+
+/*
+ Declaration of global variables
+*/
+
+int lift_checksum;/* Checksum */
+
+
+/*
+ Initialization- and return-value-related functions
+*/
+
+void lift_init()
+{
+ unsigned int i;
+ unsigned char *p;
+ volatile char bitmask = 0;
+
+ /*
+ Apply volatile XOR-bitmask to entire input array.
+ */
+ p = ( unsigned char * ) &lift_ctrl_io_in[ 0 ];
+ _Pragma( "loopbound min 40 max 40" )
+ for ( i = 0; i < sizeof( lift_ctrl_io_in ); ++i, ++p )
+ *p ^= bitmask;
+
+ p = ( unsigned char * ) &lift_ctrl_io_out[ 0 ];
+ _Pragma( "loopbound min 16 max 16" )
+ for ( i = 0; i < sizeof( lift_ctrl_io_out ); ++i, ++p )
+ *p ^= bitmask;
+
+ p = ( unsigned char * ) &lift_ctrl_io_analog[ 0 ];
+ _Pragma( "loopbound min 16 max 16" )
+ for ( i = 0; i < sizeof( lift_ctrl_io_analog ); ++i, ++p )
+ *p ^= bitmask;
+
+ p = ( unsigned char * ) &lift_ctrl_io_led[ 0 ];
+ _Pragma( "loopbound min 64 max 64" )
+ for ( i = 0; i < sizeof( lift_ctrl_io_led ); ++i, ++p )
+ *p ^= bitmask;
+
+ lift_checksum = 0;
+ lift_ctrl_init();
+}
+
+
+int lift_return()
+{
+ return ( lift_checksum - 4005888 != 0 );
+}
+
+
+/*
+ Algorithm core functions
+*/
+
+void lift_controller()
+{
+ lift_ctrl_get_vals();
+ lift_ctrl_loop();
+ lift_ctrl_set_vals();
+}
+
+
+/*
+ Main functions
+*/
+
+void _Pragma( "entrypoint" ) lift_main()
+{
+ int i = 0;
+ _Pragma( "loopbound min 1001 max 1001" )
+ while ( 1 ) {
+ /* zero input stimulus */
+ lift_simio_in = 0;
+ lift_simio_adc1 = 0;
+ lift_simio_adc2 = 0;
+ lift_simio_adc3 = 0;
+ /* run lift_controller */
+ lift_controller();
+ if ( i++ >= 1000 )
+ break;
+ }
+}
+
+
+int main( void )
+{
+ clock_prepare();
+ clock_start();
+
+ lift_init();
+ lift_main();
+
+ clock_stop();
+
+ int ret = lift_return();
+
+ printf("check: %s\n", ret ? "FAIL" : "passed");
+
+ print_total_clock();
+}
diff --git a/test/monniaux/tacle-bench-lift/liftlibcontrol.c b/test/monniaux/tacle-bench-lift/liftlibcontrol.c
new file mode 100644
index 00000000..5e491276
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/liftlibcontrol.c
@@ -0,0 +1,277 @@
+#include "liftlibio.h"
+#include "liftlibcontrol.h"
+
+/* Global variables */
+int lift_levelPos[16];
+int lift_one_level;
+
+/**
+ Is the counter valid for level positioning?
+*/
+int lift_cntValid;
+
+/**
+ Position absolute or relative<.
+*/
+int lift_cnt;
+
+/**
+ Last stoped level (1..13) if position is absolute else 0.
+*/
+int lift_level;
+
+/**
+ load position in level, 0 means we don't know
+*/
+int lift_loadLevel;
+
+/**
+ we're going TOP or BOTTOM, but stop at load position.
+*/
+int lift_loadPending;
+
+/**
+ we're waiting for the load sensor to go.
+*/
+int lift_loadSensor;
+
+/**
+ cmd keeps the value of the command until the command is finished.
+ It is only updated by the switches if it's current value is CMD_NONE.
+*/
+int lift_cmd;
+
+int lift_timMotor;
+
+int lift_timImp;
+
+/**
+ Remember last direction for impuls count after motor off;
+*/
+int lift_directionUp;
+
+/**
+ Last value of impuls sensor.
+*/
+int lift_lastImp;
+
+int lift_dbgCnt;
+
+/**
+ stop value for the counter.
+*/
+int lift_endCnt;
+
+
+void lift_ctrl_init()
+{
+ int i;
+ lift_checksum = 0;
+
+ lift_io_init();
+ lift_cntValid = 0;
+ lift_cnt = 0;
+ lift_cmd = lift_CMD_NONE;
+ lift_timMotor = 0;
+ lift_timImp = 0;
+ lift_directionUp = 1;
+ lift_lastImp = 0;
+ lift_loadLevel = 0;
+ lift_loadPending = 0;
+ lift_loadSensor = 0;
+ i = 0;
+ lift_levelPos[i++] = 0;
+ lift_levelPos[i++] = 58;
+ lift_levelPos[i++] = 115;
+ lift_levelPos[i++] = 173;
+ lift_levelPos[i++] = 230;
+ lift_levelPos[i++] = 288;
+ lift_levelPos[i++] = 346;
+ lift_levelPos[i++] = 403;
+ lift_levelPos[i++] = 461;
+ lift_levelPos[i++] = 518;
+ lift_levelPos[i++] = 576;
+ lift_levelPos[i++] = 634;
+ lift_levelPos[i++] = 691;
+ lift_levelPos[i++] = 749;
+ lift_levelPos[i++] = 806;
+ lift_levelPos[i++] = 864;
+ lift_one_level = lift_levelPos[1];
+}
+
+void lift_ctrl_loop()
+{
+ if ( lift_cmd == lift_CMD_NONE )
+ lift_check_cmd();
+ else {
+ lift_do_impulse( lift_ctrl_io_in[lift_SENS_IMPULS],
+ lift_ctrl_io_out[lift_MOTOR_ON],
+ lift_ctrl_io_in[lift_SENS_BOTTOM] );
+ lift_do_cmd();
+ }
+ lift_check_level();
+ lift_ctrl_io_led[13] = ( lift_dbgCnt & 0x80 ) != 0;
+ ++lift_dbgCnt;
+}
+
+
+void lift_check_level()
+{
+ int i;
+ int middle = lift_one_level >> 2;
+ if ( lift_cntValid ) {
+ _Pragma( "loopbound min 14 max 14" )
+ for ( lift_level = 1; lift_level < 14; ++lift_level ) {
+ if ( lift_cnt < lift_levelPos[lift_level] - middle )
+ break;
+ }
+ } else
+ lift_level = 0;
+ _Pragma( "loopbound min 14 max 14" )
+ for ( i = 0; i < 14; ++i )
+ lift_ctrl_io_led[i] = ( i == lift_level - 1 );
+}
+
+
+void lift_check_cmd()
+{
+ if ( lift_loadPending ) {
+ if ( lift_ctrl_io_in[lift_SENS_BOTTOM] )
+ lift_cmd = lift_CMD_TOP;
+ } else
+ if ( lift_ctrl_io_in[lift_GO_UP] ) {
+ if ( !lift_ctrl_io_in[lift_SENS_TOP] && lift_level != 14 )
+ lift_cmd = lift_CMD_UP;
+ } else
+ if ( lift_ctrl_io_in[lift_GO_DOWN] ) {
+ if ( !lift_ctrl_io_in[lift_SENS_BOTTOM] && lift_level != 1 )
+ lift_cmd = lift_CMD_DOWN;
+ } else
+ if ( lift_ctrl_io_in[lift_GO_LOAD] ) {
+ if ( lift_loadLevel != 0 && lift_level < lift_loadLevel )
+ lift_cmd = lift_CMD_TOP;
+ else
+ lift_cmd = lift_CMD_BOTTOM;
+ lift_loadPending = 1;
+ lift_loadSensor = 0;
+ } else
+ if ( lift_ctrl_io_in[lift_GO_TOP] ) {
+ if ( !lift_ctrl_io_in[lift_SENS_TOP] )
+ lift_cmd = lift_CMD_TOP;
+ } else
+ if ( lift_ctrl_io_in[lift_GO_BOTTOM] ) {
+ if ( !lift_ctrl_io_in[lift_SENS_BOTTOM] )
+ lift_cmd = lift_CMD_BOTTOM;
+ }
+ if ( lift_cmd != lift_CMD_NONE )
+ lift_timMotor = 50;
+}
+
+
+void lift_do_impulse( int val, int motor, int reset )
+{
+ if ( val && !lift_lastImp ) {
+ if ( motor || lift_timImp > 0 ) {
+ if ( lift_directionUp )
+ ++lift_cnt;
+ else
+ --lift_cnt;
+ }
+ }
+ if ( reset ) {
+ lift_cnt = 0;
+ lift_cntValid = 1;
+ }
+ lift_lastImp = val;
+ if ( lift_timImp > 0 ) {
+ --lift_timImp;
+ if ( lift_timImp == 0 && lift_cmd != lift_CMD_NONE )
+ lift_cmd = lift_CMD_NONE;
+ }
+}
+
+
+void lift_do_cmd()
+{
+ int run = 0;
+ if ( lift_timMotor > 0 )
+ lift_wait_for_motor_start();
+ else {
+ run = lift_check_run();
+ if ( lift_ctrl_io_out[lift_MOTOR_ON] && !run ) {
+ /* motor stopped: */
+ lift_cmd = 99;
+ lift_timImp = 50;
+ }
+ lift_ctrl_io_out[lift_MOTOR_ON] = run;
+ }
+}
+
+
+void lift_wait_for_motor_start()
+{
+ int newLevel = 0;
+ --lift_timMotor;
+ lift_directionUp = ( lift_cmd == lift_CMD_UP || lift_cmd == lift_CMD_TOP );
+ lift_ctrl_io_out[lift_MOTOR_UP] = lift_directionUp;
+ if ( !lift_cntValid ) {
+ lift_cnt = 0; /* use relative counter */
+ if ( lift_cmd == lift_CMD_UP )
+ lift_endCnt = lift_one_level;
+ else
+ lift_endCnt = -lift_one_level;
+ } else {
+ lift_endCnt = lift_cnt;
+ newLevel = -99;
+ if ( lift_cmd == lift_CMD_UP )
+ newLevel = lift_level + 1;
+ else
+ if ( lift_cmd == lift_CMD_DOWN )
+ newLevel = lift_level - 1;
+ --newLevel; /* lift_level is one based */
+ if ( newLevel >= 0 && newLevel < 14 )
+ lift_endCnt = lift_levelPos[newLevel];
+ }
+}
+
+
+int lift_check_run()
+{
+ if ( lift_cmd == lift_CMD_UP ) {
+ if ( lift_cnt < lift_endCnt - 1 && !lift_ctrl_io_in[lift_SENS_TOP] )
+ return 1;
+ } else
+ if ( lift_cmd == lift_CMD_DOWN ) {
+ if ( lift_cnt > lift_endCnt + 1 && !lift_ctrl_io_in[lift_SENS_BOTTOM] )
+ return 1;
+ } else
+ if ( lift_cmd == lift_CMD_TOP ) {
+ if ( lift_loadPending && lift_ctrl_io_in[lift_SENS_LOAD] ) {
+ /* we are at lift_load position */
+ lift_loadLevel = lift_level;
+ lift_loadPending = 0;
+ return 0;
+ }
+ if ( !lift_ctrl_io_in[lift_SENS_TOP] )
+ return 1;
+ /* safe fallback if lift_load sensor does not work */
+ lift_loadPending = 0;
+ } else
+ if ( lift_cmd == lift_CMD_BOTTOM ) {
+ if ( lift_loadPending ) {
+ if ( lift_loadSensor ) {
+ if ( !lift_ctrl_io_in[lift_SENS_LOAD] ) {
+ lift_loadSensor = 0;
+ /* we are at lift_load position */
+ lift_loadPending = 0;
+ lift_loadLevel = lift_level;
+ return 0;
+ }
+ }
+ lift_loadSensor = lift_ctrl_io_in[lift_SENS_LOAD];
+ }
+ if ( !lift_ctrl_io_in[lift_SENS_BOTTOM] )
+ return 1;
+ }
+ return 0;
+}
diff --git a/test/monniaux/tacle-bench-lift/liftlibcontrol.h b/test/monniaux/tacle-bench-lift/liftlibcontrol.h
new file mode 100644
index 00000000..2c6669e8
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/liftlibcontrol.h
@@ -0,0 +1,64 @@
+#ifndef LIFTLIBCONTROL_H
+#define LIFTLIBCONTROL_H
+
+enum lift_Direction {
+ lift_GO_LOAD = 8,
+ lift_GO_TOP = 6,
+ lift_GO_BOTTOM = 7,
+ lift_GO_UP = 4,
+ lift_GO_DOWN = 5
+};
+
+enum lift_Sensor {
+ lift_SENS_IMPULS = 0,
+ lift_SENS_TOP = 1,
+ lift_SENS_BOTTOM = 2,
+ lift_SENS_LOAD = 3
+};
+
+enum lift_Motor {
+ lift_MOTOR_ON = 0,
+ lift_MOTOR_UP = 1
+};
+
+enum lift_Command {
+ lift_CMD_NONE = 0,
+ lift_CMD_TOP = 1,
+ lift_CMD_BOTTOM = 2,
+ lift_CMD_UP = 3,
+ lift_CMD_DOWN = 4
+};
+
+/* Global variables */
+extern int lift_levelPos[16];
+extern int lift_one_level;
+extern int lift_cntValid;
+extern int lift_cnt;
+extern int lift_level;
+extern int lift_loadLevel;
+extern int lift_loadPending;
+extern int lift_loadSensor;
+extern int lift_cmd;
+extern int lift_timMotor;
+extern int lift_timImp;
+extern int lift_directionUp;
+extern int lift_lastImp;
+extern int lift_dbgCnt;
+extern int lift_endCnt;
+
+/* Checksum */
+extern int lift_checksum;
+
+/* prototypes */
+void lift_ctrl_init(void); /* DM prototype */
+void lift_ctrl_loop(void); /* DM prototype */
+
+/* internal prototypes */
+int lift_check_run(void);
+void lift_wait_for_motor_start(void);
+void lift_do_cmd(void);
+void lift_do_impulse( int val, int motor, int reset );
+void lift_check_cmd(void);
+void lift_check_level(void);
+
+#endif
diff --git a/test/monniaux/tacle-bench-lift/liftlibio.c b/test/monniaux/tacle-bench-lift/liftlibio.c
new file mode 100644
index 00000000..f61df2d0
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/liftlibio.c
@@ -0,0 +1,65 @@
+#include "liftlibio.h"
+
+/* Global variables */
+int lift_ctrl_io_in[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+int lift_ctrl_io_out[4] = {0, 0, 0, 0};
+int lift_ctrl_io_analog[4] = {0, 0, 0, 0};
+int lift_ctrl_io_led[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+int lift_ctrl_dly1;
+int lift_ctrl_dly2;
+
+/* Simulated hardware */
+volatile int lift_simio_in;
+volatile int lift_simio_out;
+volatile int lift_simio_led;
+volatile int lift_simio_adc1;
+volatile int lift_simio_adc2;
+volatile int lift_simio_adc3;
+
+void lift_io_init()
+{
+ lift_ctrl_dly1 = 0;
+ lift_ctrl_dly2 = 0;
+}
+
+
+void lift_ctrl_set_vals()
+{
+ int val = 0, i;
+ _Pragma( "loopbound min 4 max 4" )
+ for ( i = 4 - 1; i >= 0; --i ) {
+ val <<= 1;
+ val |= lift_ctrl_io_out[i] ? 1 : 0;
+ }
+ lift_simio_out = val;
+ _Pragma( "loopbound min 16 max 16" )
+ for ( i = 14 - 1; i >= 0; --i ) {
+ val <<= 1;
+ val |= lift_ctrl_io_led[i] ? 1 : 0;
+ }
+ lift_simio_led = val;
+ lift_checksum += val;
+}
+
+
+void lift_ctrl_get_vals()
+{
+ int i;
+ unsigned short int in0 = lift_simio_in;
+ unsigned short int in1 = lift_ctrl_dly1;
+ unsigned short int in2 = lift_ctrl_dly2;
+ lift_ctrl_dly2 = lift_ctrl_dly1;
+ lift_ctrl_dly1 = in0;
+ /* majority voting for input values
+ delays input value change by one period */
+ _Pragma( "loopbound min 10 max 10" )
+ for ( i = 0; i < 10; ++i ) {
+ lift_ctrl_io_in[i] = ( ( in0 & 1 ) + ( in1 & 1 ) + ( in2 & 1 ) ) > 1;
+ in0 >>= 1;
+ in1 >>= 1;
+ in2 >>= 1;
+ }
+ lift_ctrl_io_analog[0] = lift_simio_adc1;
+ lift_ctrl_io_analog[1] = lift_simio_adc2;
+ lift_ctrl_io_analog[2] = lift_simio_adc3;
+}
diff --git a/test/monniaux/tacle-bench-lift/liftlibio.h b/test/monniaux/tacle-bench-lift/liftlibio.h
new file mode 100644
index 00000000..1a67cfb5
--- /dev/null
+++ b/test/monniaux/tacle-bench-lift/liftlibio.h
@@ -0,0 +1,27 @@
+#ifndef LIFTLIBIO_H
+#define LIFTLIBIO_H
+
+/* Global variables */
+extern int lift_ctrl_io_in[10];
+extern int lift_ctrl_io_out[4];
+extern int lift_ctrl_io_analog[4];
+extern int lift_ctrl_io_led[16];
+extern int lift_ctrl_dly1, lift_ctrl_dly2;
+
+/* Simulated hardware */
+extern volatile int lift_simio_in;
+extern volatile int lift_simio_out;
+extern volatile int lift_simio_led;
+extern volatile int lift_simio_adc1;
+extern volatile int lift_simio_adc2;
+extern volatile int lift_simio_adc3;
+
+/* Checksum */
+extern int lift_checksum;
+
+/* prototypes */
+void lift_io_init( void );
+void lift_ctrl_get_vals( void );
+void lift_ctrl_set_vals( void );
+
+#endif
diff --git a/test/monniaux/tacle-bench-powerwindow/Makefile b/test/monniaux/tacle-bench-powerwindow/Makefile
new file mode 100644
index 00000000..3417cd4c
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/Makefile
@@ -0,0 +1,4 @@
+ALL_CFLAGS:=-include kill_pragma.h
+TARGET=powerwindow
+
+include ../rules.mk
diff --git a/test/monniaux/tacle-bench-powerwindow/changeLog b/test/monniaux/tacle-bench-powerwindow/changeLog
new file mode 100644
index 00000000..e8989ddc
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/changeLog
@@ -0,0 +1,42 @@
+Adjusted the original files to tacle benchmark requirements
+
+Created the input serials in void powerwindow_input_initialize(void) to cover the real life scenarios
+
+23-03-2016:
+Deleted folder powerwindow_SourceFiles moved all the contained c files to PowerWindow folder
+
+Created headerfile powerwindow.h
+
+Added
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
+to header file powerwindow.h
+
+Deleted #include <stdio.h> in all files replaced with headerfile powerwindow.h
+
+Deleted function powerwindow_GetErrorStatus(void) and the related code
+
+Commented
+ long tic, toc, tictoc;
+ tic = powerwindow_get_cycles();
+ toc = powerwindow_get_cycles();
+ tictoc = toc-tic;
+ printf("%ld\n",tictoc );
+
+in the int main(void). Uncomment can print the execution time of the powerwindow benchmark.
+
+Adjusted the description of all the files
+
+21-04-2016
+
+Adjusted the author and license
+
+18-04-2017:
+Annotated powerwindow_main as entry-point for timing analysis
+
+10-07-2017
+- Fix changelog order so dates are correctly sorted.
+- Fix input array length to avoid possible buffer overflow.
diff --git a/test/monniaux/tacle-bench-powerwindow/kill_pragma.h b/test/monniaux/tacle-bench-powerwindow/kill_pragma.h
new file mode 100644
index 00000000..a2fde5a9
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/kill_pragma.h
@@ -0,0 +1 @@
+#define _Pragma(x)
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow.c b/test/monniaux/tacle-bench-powerwindow/powerwindow.c
new file mode 100644
index 00000000..6fb3a348
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow.c
@@ -0,0 +1,627 @@
+/* DM */
+#include "../clock.h"
+#include <stdio.h>
+
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: powerwindow implement the powerwindow that can be seen in cars nowadays.
+ The window can be controlled by either driver or passenger. When an object is
+ detected between the window frame and the glass during the raising of the glass,
+ the glass will lower down for some distance. This benchmark contains 4 tasks which includes the
+ driver side powerwindow, front passenger side powerwindow, back-left passenger side powerwindow,
+ back-right passenger side powerwindow. These 4 tasks can be easily adjusted to execute in
+ sequential order parallel on single or muti core.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+#include "powerwindow_HeaderFiles/powerwindow.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h"
+#include "powerwindow_HeaderFiles/powerwindow_debounce.h"
+#include "powerwindow_HeaderFiles/powerwindow_controlexclusion.h" /* Control Model's header file */
+#include "powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#include "powerwindow_HeaderFiles/powerwindow_powerwindow_control.h" /* PW passenger control Model's header file */
+#include "powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+/*
+ Forward declaration of functions
+*/
+
+
+void powerwindow_Booleaninputarray_initialize(powerwindow_boolean_T*, powerwindow_boolean_T*);
+void powerwindow_Uint8inputarray_initialize(powerwindow_uint8_T*, powerwindow_uint8_T*);
+void powerwindow_init();
+void powerwindow_main();
+int powerwindow_return();
+int main(void);
+
+
+
+
+//DRV
+void powerwindow_init_DRV(int);
+void powerwindow_input_initialize_DRV(void);
+void powerwindow_initialize_DRV(void);
+void powerwindow_return_DRV(void);
+void powerwindow_DRV_main(void);
+
+// PSG_Front
+void powerwindow_init_PSG_Front(int);
+void powerwindow_input_initialize_PSG_Front(void);
+void powerwindow_initialize_PSG_Front(void);
+void powerwindow_return_PSG_Front(void);
+void powerwindow_PSG_Front_main(void);
+
+// PSG_BackL
+void powerwindow_init_PSG_BackL(int);
+void powerwindow_input_initialize_PSG_BackL(void);
+void powerwindow_initialize_PSG_BackL(void);
+void powerwindow_return_PSG_BackL(void);
+void powerwindow_PSG_BackL_main(void);
+
+// PSG_BackR
+void powerwindow_init_PSG_BackR(int);
+void powerwindow_input_initialize_PSG_BackR(void);
+void powerwindow_initialize_PSG_BackR(void);
+void powerwindow_return_PSG_BackR(void);
+void powerwindow_PSG_BackR_main(void);
+
+
+
+/*
+ Declaration of global variables
+*/
+
+/* External inputs (root inport signals with auto storage) */
+
+extern powerwindow_ExternalInputs_powerwindow_PW_C powerwindow_PW_Control_DRV_U;
+extern powerwindow_ExternalOutputs_powerwindow_PW_ powerwindow_PW_Control_DRV_Y;
+extern powerwindow_ExternalInputs_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_U;
+extern powerwindow_ExternalInputs_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_U;
+extern powerwindow_ExternalInputs_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_U;
+
+
+
+powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Up_Input_DRV[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Down_Input_DRV[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Up_Input_Front[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Down_Input_Front[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Up_Input_BackL[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Down_Input_BackL[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Up_Input_BackR[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Down_Input_BackR[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Input_DRV[powerwindow_input_length];
+powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Input_DRV[powerwindow_input_length];
+
+
+extern powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Up_Input_DRV_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Down_Input_DRV_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Up_Input_Front_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Down_Input_Front_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Up_Input_BackL_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Down_Input_BackL_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Up_Input_BackR_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Down_Input_BackR_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_DRV_Array[powerwindow_input_length];
+extern powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_DRV_Array[powerwindow_input_length];
+
+powerwindow_boolean_T powerwindow_controlexclusion_U_Up_DRV_Input_Front[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+powerwindow_boolean_T powerwindow_controlexclusion_U_Down_DRV_Input_Front[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Up_Input_Front[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Down_Input_Front[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Input_Front[powerwindow_input_length];
+powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Input_Front[powerwindow_input_length];
+
+extern powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Up_Front_Array[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+extern powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Down_Front_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Front_Array[powerwindow_input_length];
+extern powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Front_Array[powerwindow_input_length];
+
+powerwindow_boolean_T powerwindow_controlexclusion_U_Up_DRV_Input_BackL[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+powerwindow_boolean_T powerwindow_controlexclusion_U_Down_DRV_Input_BackL[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Up_Input_BackL[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Down_Input_BackL[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackL[powerwindow_input_length];
+powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Input_BackL[powerwindow_input_length];
+
+extern powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Up_BackL_Array[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+extern powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Down_BackL_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_BackL_Array[powerwindow_input_length];
+extern powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_BackL_Array[powerwindow_input_length];
+
+powerwindow_boolean_T powerwindow_controlexclusion_U_Up_DRV_Input_BackR[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+powerwindow_boolean_T powerwindow_controlexclusion_U_Down_DRV_Input_BackR[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Up_Input_BackR[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Down_Input_BackR[powerwindow_input_length];
+powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackR[powerwindow_input_length];
+powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Input_BackR[powerwindow_input_length];
+
+extern powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Up_BackR_Array[powerwindow_input_length]; /* Here applied a push-down button, the signal is high when the button is not pressed. */
+extern powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Down_BackR_Array[powerwindow_input_length];
+extern powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_BackR_Array[powerwindow_input_length];
+extern powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_BackR_Array[powerwindow_input_length];
+
+int powerwindow_main_inputcyclecounter;
+
+/*
+ Initialization- and return-value-related functions
+*/
+void powerwindow_init_DRV(int i)
+{
+ powerwindow_PW_Control_DRV_U.In1 = powerwindow_powerwindow_control_U_endofdetectionrange_Input_DRV[i]; /* The when the window reaches the end of the range, the endofdetectionrange changes to 0. */
+ powerwindow_PW_Control_DRV_U.In3 = powerwindow_powerwindow_control_U_currentsense_Input_DRV[i]; /* When the currentsense is higher than 92 (based on experiments), one object is stuck between the window and the frame. Pinch is set to True.*/
+
+ powerwindow_PW_Control_DRV_U.In2 = powerwindow_debounce_Driver_DRV_U_Up_Input_DRV[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_DRV_U.In4 = powerwindow_debounce_Driver_DRV_U_Down_Input_DRV[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+
+ powerwindow_PW_Control_DRV_U.In5 = powerwindow_debounce_Driver_Front_U_Up_Input_Front[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_DRV_U.In6 = powerwindow_debounce_Driver_Front_U_Down_Input_Front[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+
+ powerwindow_PW_Control_DRV_U.In9 = powerwindow_debounce_Driver_BackL_U_Up_Input_BackL[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_DRV_U.In10 = powerwindow_debounce_Driver_BackL_U_Down_Input_BackL[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+
+ powerwindow_PW_Control_DRV_U.In7 = powerwindow_debounce_Driver_BackR_U_Up_Input_BackR[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_DRV_U.In8 = powerwindow_debounce_Driver_BackR_U_Down_Input_BackR[i]; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+
+}
+
+void powerwindow_init_PSG_Front(int i)
+{
+
+
+ powerwindow_PW_Control_PSG_Front_U.Up_DRV = powerwindow_PW_Control_DRV_Y.Out6; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_PSG_Front_U.Down_DRV = powerwindow_PW_Control_DRV_Y.Out7; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_Front_U.Up_PSG_Front = powerwindow_debounce_passenger_Front_U_Up_Input_Front[i];
+ powerwindow_PW_Control_PSG_Front_U.Down_PSG_Front = powerwindow_debounce_passenger_Front_U_Down_Input_Front[i]; /* '<Root>/Down'. Here applied a push-down button, the signal is high when the button is not pressed. Change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_Front_U.endofdetectionrange = powerwindow_powerwindow_control_U_endofdetectionrange_Input_Front[i]; /* The when the window reaches the end of the range, the endofdetectionrange changes to 0. */
+
+ powerwindow_PW_Control_PSG_Front_U.currentsense = powerwindow_powerwindow_control_U_currentsense_Input_Front[i]; /* When the currentsense is higher than 92 (based on experiments), one object is stuck between the window and the frame. Pinch is set to True.*/
+
+}
+
+void powerwindow_init_PSG_BackL(int i)
+{
+
+
+ powerwindow_PW_Control_PSG_BackL_U.Up_DRV = powerwindow_PW_Control_DRV_Y.Out10; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_PSG_BackL_U.Down_DRV = powerwindow_PW_Control_DRV_Y.Out11; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_BackL_U.Up_PSG_BackL = powerwindow_debounce_passenger_BackL_U_Up_Input_BackL[i];
+ powerwindow_PW_Control_PSG_BackL_U.Down_PSG_BackL = powerwindow_debounce_passenger_BackL_U_Down_Input_BackL[i]; /* '<Root>/Down'. Here applied a push-down button, the signal is high when the button is not pressed. Change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_BackL_U.endofdetectionrange = powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackL[i]; /* The when the window reaches the end of the range, the endofdetectionrange changes to 0. */
+
+ powerwindow_PW_Control_PSG_BackL_U.currentsense = powerwindow_powerwindow_control_U_currentsense_Input_BackL[i]; /* When the currentsense is higher than 92 (based on experiments), one object is stuck between the window and the frame. Pinch is set to True.*/
+
+}
+
+void powerwindow_init_PSG_BackR(int i)
+{
+
+
+ powerwindow_PW_Control_PSG_BackR_U.Up_DRV = powerwindow_PW_Control_DRV_Y.Out8; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lift the window. */
+ powerwindow_PW_Control_PSG_BackR_U.Down_DRV = powerwindow_PW_Control_DRV_Y.Out9; /* The debounced control signal from the driver. 1 when the button is not pressed, change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_BackR_U.Up_PSG_BackR = powerwindow_debounce_passenger_BackR_U_Up_Input_BackR[i];
+ powerwindow_PW_Control_PSG_BackR_U.Down_PSG_BackR = powerwindow_debounce_passenger_BackR_U_Down_Input_BackR[i]; /* '<Root>/Down'. Here applied a push-down button, the signal is high when the button is not pressed. Change to 0 to lower the window. */
+
+ powerwindow_PW_Control_PSG_BackR_U.endofdetectionrange = powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackR[i]; /* The when the window reaches the end of the range, the endofdetectionrange changes to 0. */
+
+ powerwindow_PW_Control_PSG_BackR_U.currentsense = powerwindow_powerwindow_control_U_currentsense_Input_BackR[i]; /* When the currentsense is higher than 92 (based on experiments), one object is stuck between the window and the frame. Pinch is set to True.*/
+
+}
+
+void powerwindow_input_initialize_DRV(void)
+{
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_DRV_U_Up_Input_DRV,powerwindow_debounce_Driver_DRV_U_Up_Input_DRV_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_DRV_U_Down_Input_DRV,powerwindow_debounce_Driver_DRV_U_Down_Input_DRV_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_Front_U_Up_Input_Front,powerwindow_debounce_Driver_Front_U_Up_Input_Front_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_Front_U_Down_Input_Front,powerwindow_debounce_Driver_Front_U_Down_Input_Front_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_BackL_U_Up_Input_BackL,powerwindow_debounce_Driver_BackL_U_Up_Input_BackL_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_BackL_U_Down_Input_BackL,powerwindow_debounce_Driver_BackL_U_Down_Input_BackL_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_BackL_U_Down_Input_BackL,powerwindow_debounce_Driver_BackR_U_Up_Input_BackR_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_Driver_BackR_U_Down_Input_BackR,powerwindow_debounce_Driver_BackR_U_Down_Input_BackR_Array);
+ powerwindow_Booleaninputarray_initialize(powerwindow_powerwindow_control_U_endofdetectionrange_Input_DRV,powerwindow_powerwindow_control_U_endofdetectionrange_DRV_Array);
+ powerwindow_Uint8inputarray_initialize(powerwindow_powerwindow_control_U_currentsense_DRV_Array,powerwindow_powerwindow_control_U_currentsense_DRV_Array);
+
+}
+
+
+
+void powerwindow_input_initialize_PSG_Front(void)
+{
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_Front_U_Up_Input_Front, powerwindow_debounce_passenger_Front_U_Up_Front_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_Front_U_Down_Input_Front,powerwindow_debounce_passenger_Front_U_Down_Front_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_powerwindow_control_U_endofdetectionrange_Input_Front, powerwindow_powerwindow_control_U_endofdetectionrange_Front_Array);
+
+ powerwindow_Uint8inputarray_initialize(powerwindow_powerwindow_control_U_currentsense_Input_Front,powerwindow_powerwindow_control_U_currentsense_Front_Array);
+
+}
+
+
+void powerwindow_input_initialize_PSG_BackL(void)
+{
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_BackL_U_Up_Input_BackL, powerwindow_debounce_passenger_BackL_U_Up_BackL_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_BackL_U_Down_Input_BackL,powerwindow_debounce_passenger_BackL_U_Down_BackL_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackL, powerwindow_powerwindow_control_U_endofdetectionrange_BackL_Array);
+
+ powerwindow_Uint8inputarray_initialize(powerwindow_powerwindow_control_U_currentsense_Input_BackL,powerwindow_powerwindow_control_U_currentsense_BackL_Array);
+
+}
+
+void powerwindow_input_initialize_PSG_BackR(void)
+{
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_BackR_U_Up_Input_BackR, powerwindow_debounce_passenger_BackR_U_Up_BackR_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_debounce_passenger_BackR_U_Down_Input_BackR,powerwindow_debounce_passenger_BackR_U_Down_BackR_Array);
+
+ powerwindow_Booleaninputarray_initialize(powerwindow_powerwindow_control_U_endofdetectionrange_Input_BackR, powerwindow_powerwindow_control_U_endofdetectionrange_BackR_Array);
+
+ powerwindow_Uint8inputarray_initialize(powerwindow_powerwindow_control_U_currentsense_Input_BackR,powerwindow_powerwindow_control_U_currentsense_BackR_Array);
+
+}
+
+void powerwindow_Booleaninputarray_initialize(powerwindow_boolean_T* arrayA, powerwindow_boolean_T* arrayB)
+{
+
+ register int i;
+// _Pragma( "loopbound min powerwindow_input_length max powerwindow_input_length" )
+ for ( i = 0; i < powerwindow_input_length; i++ )
+ arrayA[i] = arrayB[i];
+}
+
+void powerwindow_Uint8inputarray_initialize(powerwindow_uint8_T* arrayA, powerwindow_uint8_T* arrayB)
+{
+
+ register int i;
+// _Pragma( "loopbound min powerwindow_input_length max powerwindow_input_length" )
+ for ( i = 0; i < powerwindow_input_length; i++ )
+ arrayA[i] = arrayB[i];
+}
+
+void powerwindow_initialize_DRV(void)
+{
+ /* Initialize model */
+ powerwindow_PW_Control_DRV_initialize();
+
+}
+
+void powerwindow_initialize_PSG_Front(void)
+{
+ /* Initialize model */
+ powerwindow_PW_Control_PSG_Front_initialize();
+
+}
+
+void powerwindow_initialize_PSG_BackL(void)
+{
+ /* Initialize model */
+ powerwindow_PW_Control_PSG_BackL_initialize();
+
+}
+
+void powerwindow_initialize_PSG_BackR(void)
+{
+ /* Initialize model */
+ powerwindow_PW_Control_PSG_BackR_initialize();
+
+}
+
+
+void powerwindow_return_DRV(void)
+{
+ /* Terminate model */
+ powerwindow_PW_Control_DRV_terminate();
+
+}
+
+void powerwindow_return_PSG_Front(void)
+{
+ /* Terminate model */
+ powerwindow_PW_Control_PSG_Front_terminate();
+
+}
+
+void powerwindow_return_PSG_BackL(void)
+{
+ /* Terminate model */
+ powerwindow_PW_Control_PSG_BackL_terminate();
+
+}
+
+void powerwindow_return_PSG_BackR(void)
+{
+ /* Terminate model */
+ powerwindow_PW_Control_PSG_BackR_terminate();
+
+}
+
+/*
+ Main functions
+*/
+
+/*
+ * Associating powerwindow_main with a real-time clock or interrupt service routine
+ * is what makes the generated code "real-time". The function powerwindow_main is
+ * always associated with the base rate of the model. Subrates are managed
+ * by the base rate from inside the generated code. Enabling/disabling
+ * interrupts and floating point context switches are target specific. This
+ * example code indicates where these should take place relative to executing
+ * the generated code step function. Overrun behavior should be tailored to
+ * your application needs. This example simply sets an error status in the
+ * real-time model and returns from powerwindow_main.
+ */
+
+
+void powerwindow_DRV_main(void)
+{
+
+ static powerwindow_boolean_T OverrunFlag = 0;
+
+ /* Disable interrupts here */
+
+ /* Check for overrun */
+ if (OverrunFlag) {
+ powerwindow_PW_DRV_rtmSetErrorStatus(powerwindow_PW_Control_DRV_M, "Overrun"); //////////
+
+ return;
+ }
+
+
+ OverrunFlag = true;
+
+ /* Save FPU context here (if necessary) */
+ /* Re-enable timer or interrupt here */
+ /* Set model inputs here */
+
+
+ powerwindow_PW_Control_DRV_main();
+
+ /* Get model outputs here */
+
+
+ /* Indicate task complete */
+ OverrunFlag = false;
+
+ /* Disable interrupts here */
+ /* Restore FPU context here (if necessary) */
+ /* Enable interrupts here */
+
+}
+
+/*
+ * The example "main" function illustrates what is required by your
+ * application code to initialize, execute, and terminate the generated code.
+ * Attaching powerwindow_main to a real-time clock is target specific. This example
+ * illustates how you do this relative to initializing the model.
+ */
+
+void powerwindow_PSG_Front_main(void)
+{
+
+ static powerwindow_boolean_T OverrunFlag = 0;
+
+ /* Disable interrupts here */
+
+ /* Check for overrun */
+ if (OverrunFlag) {
+ powerwindow_PW_PSG_Front_rtmSetErrorStatus(powerwindow_PW_Control_PSG_Front_M, "Overrun");
+
+ return;
+ }
+
+
+ OverrunFlag = true;
+
+ /* Save FPU context here (if necessary) */
+ /* Re-enable timer or interrupt here */
+ /* Set model inputs here */
+
+
+ powerwindow_PW_Control_PSG_Front_main();
+
+ /* Get model outputs here */
+
+
+ /* Indicate task complete */
+ OverrunFlag = false;
+
+ /* Disable interrupts here */
+ /* Restore FPU context here (if necessary) */
+ /* Enable interrupts here */
+
+}
+
+void powerwindow_PSG_BackL_main(void)
+{
+
+ static powerwindow_boolean_T OverrunFlag = 0;
+
+ /* Disable interrupts here */
+
+ /* Check for overrun */
+ if (OverrunFlag) {
+ powerwindow_PW_PSG_BackL_rtmSetErrorStatus(powerwindow_PW_Control_PSG_BackL_M, "Overrun");
+
+ return;
+ }
+
+
+ OverrunFlag = true;
+
+ /* Save FPU context here (if necessary) */
+ /* Re-enable timer or interrupt here */
+ /* Set model inputs here */
+
+
+ powerwindow_PW_Control_PSG_BackL_main();
+
+ /* Get model outputs here */
+
+
+ /* Indicate task complete */
+ OverrunFlag = false;
+
+ /* Disable interrupts here */
+ /* Restore FPU context here (if necessary) */
+ /* Enable interrupts here */
+
+}
+
+void powerwindow_PSG_BackR_main(void)
+{
+
+ static powerwindow_boolean_T OverrunFlag = 0;
+
+ /* Disable interrupts here */
+
+ /* Check for overrun */
+ if (OverrunFlag) {
+ powerwindow_PW_PSG_BackR_rtmSetErrorStatus(powerwindow_PW_Control_PSG_BackR_M, "Overrun");
+
+ return;
+ }
+
+
+ OverrunFlag = true;
+
+ /* Save FPU context here (if necessary) */
+ /* Re-enable timer or interrupt here */
+ /* Set model inputs here */
+
+
+ powerwindow_PW_Control_PSG_BackR_main();
+
+ /* Get model outputs here */
+
+
+ /* Indicate task complete */
+ OverrunFlag = false;
+
+ /* Disable interrupts here */
+ /* Restore FPU context here (if necessary) */
+ /* Enable interrupts here */
+
+}
+
+void powerwindow_init(void)
+{
+ powerwindow_initialize_DRV();
+ powerwindow_initialize_PSG_Front();
+ powerwindow_initialize_PSG_BackL();
+ powerwindow_initialize_PSG_BackR();
+ powerwindow_main_inputcyclecounter=0;
+
+}
+
+void _Pragma( "entrypoint" ) powerwindow_main(void)
+{
+ /* Attach powerwindow_main to a timer or interrupt service routine with
+ * period 0.005 seconds (the model's base sample time) here. The
+ * call syntax for powerwindow_main is
+ *
+ * powerwindow_main();
+ */
+ //Task 1: Driver side window
+
+
+ powerwindow_input_initialize_DRV();
+ powerwindow_input_initialize_PSG_Front();
+ powerwindow_input_initialize_PSG_BackL();
+ powerwindow_input_initialize_PSG_BackR();
+
+ while(powerwindow_main_inputcyclecounter<powerwindow_input_length)
+ {
+
+ powerwindow_init_DRV(powerwindow_main_inputcyclecounter);
+ powerwindow_DRV_main();
+
+
+ //Task 2: Front passenger side window
+
+
+ powerwindow_init_PSG_Front(powerwindow_main_inputcyclecounter);
+ powerwindow_PSG_Front_main();
+
+
+ //Task 3: Back left passenger side window
+
+
+ powerwindow_init_PSG_BackL(powerwindow_main_inputcyclecounter);
+ powerwindow_PSG_BackL_main();
+
+
+ //Task 4: Back right passenger side window
+
+
+ powerwindow_init_PSG_BackR(powerwindow_main_inputcyclecounter);
+ powerwindow_PSG_BackR_main();
+
+
+ powerwindow_main_inputcyclecounter++;
+ }
+
+
+}
+
+int powerwindow_return(void)
+{
+ powerwindow_return_DRV();
+ powerwindow_return_PSG_Front();
+ powerwindow_return_PSG_BackL();
+ powerwindow_return_PSG_BackR();
+
+ return 0;
+}
+
+
+int main(void){
+ clock_prepare();
+ clock_start();
+
+ powerwindow_init();
+ powerwindow_main();
+
+ clock_stop();
+
+ int ret = powerwindow_return();
+
+ printf("check: %s\n", ret ? "FAIL" : "passed");
+ print_total_clock();
+
+ return ret;
+}
+
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
+
+
+
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow.h
new file mode 100644
index 00000000..357f91ee
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow.h
@@ -0,0 +1,22 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef NULL
+#define NULL ((viod *) 0)
+#endif
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h
new file mode 100644
index 00000000..3ad4ff6e
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h
@@ -0,0 +1,139 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_DRV.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_powerwindow_PW_Control_DRV_h_
+#define powerwindow_RTW_HEADER_powerwindow_PW_Control_DRV_h_
+#ifndef powerwindow_PW_Control_DRV_COMMON_INCLUDES_
+# define powerwindow_PW_Control_DRV_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#endif /* powerwindow_PW_Control_DRV_COMMON_INCLUDES_ */
+
+#include "../powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Child system includes */
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control.h"
+#include "../powerwindow_HeaderFiles/powerwindow_debounce.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_PW_DRV_rtmGetErrorStatus
+# define powerwindow_PW_DRV_rtmGetErrorStatus(rtm) ((rtm)->errorStatus)
+#endif
+
+#ifndef powerwindow_PW_DRV_rtmSetErrorStatus
+# define powerwindow_PW_DRV_rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+
+#ifndef powerwindow_PW_DRV_rtmGetErrorStatusPointer
+# define powerwindow_PW_DRV_rtmGetErrorStatusPointer(rtm) ((const powerwindow_char_T **)(&((rtm)->errorStatus)))
+#endif
+
+/* Block states (auto storage) for system '<Root>' */
+typedef struct {
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_DRV_DWORK1;/* '<S2>/Debounce_Up_DRV' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_DRV_DWORK1;/* '<S2>/Debounce_Down_DRV' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_PSG_BackL_DWORK1;/* '<S2>/Debounce_Up_PSG_BackL' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_PSG_BackL_DWORK1;/* '<S2>/Debounce_Down_PSG_BackL' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_PSG_Front_DWORK1;/* '<S2>/Debounce_Up_PSG_Front' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_PSG_Front_DWORK1;/* '<S2>/Debounce_Down_PSG_Front' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_PSG_BackR_DWORK1;/* '<S2>/Debounce_Up_PSG_BackR' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_PSG_BackR_DWORK1;/* '<S2>/Debounce_Down_PSG_BackR' */
+ powerwindow_rtMdlrefDWork_PowerWindow_contr PW_DRV_DWORK1;/* '<S1>/PW_DRV' */
+} powerwindow_D_Work_powerwindow_PW_Control_D;
+
+/* External inputs (root inport signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T In1; /* '<Root>/In1' */
+ powerwindow_boolean_T In2; /* '<Root>/In2' */
+ powerwindow_uint8_T In3; /* '<Root>/In3' */
+ powerwindow_boolean_T In4; /* '<Root>/In4' */
+ powerwindow_boolean_T In5; /* '<Root>/In5' */
+ powerwindow_boolean_T In6; /* '<Root>/In6' */
+ powerwindow_boolean_T In7; /* '<Root>/In7' */
+ powerwindow_boolean_T In8; /* '<Root>/In8' */
+ powerwindow_boolean_T In9; /* '<Root>/In9' */
+ powerwindow_boolean_T In10; /* '<Root>/In10' */
+} powerwindow_ExternalInputs_powerwindow_PW_C;
+
+/* External outputs (root outports fed by signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T Out1; /* '<Root>/Out1' */
+ powerwindow_boolean_T Out2; /* '<Root>/Out2' */
+ powerwindow_boolean_T Out3; /* '<Root>/Out3' */
+ powerwindow_boolean_T Out4; /* '<Root>/Out4' */
+ powerwindow_boolean_T Out5; /* '<Root>/Out5' */
+ powerwindow_boolean_T Out6; /* '<Root>/Out6' */
+ powerwindow_boolean_T Out7; /* '<Root>/Out7' */
+ powerwindow_boolean_T Out8; /* '<Root>/Out8' */
+ powerwindow_boolean_T Out9; /* '<Root>/Out9' */
+ powerwindow_boolean_T Out10; /* '<Root>/Out10' */
+ powerwindow_boolean_T Out11; /* '<Root>/Out11' */
+} powerwindow_ExternalOutputs_powerwindow_PW_;
+
+struct powerwindow_tag_RTM_PW_Control_DRV {
+ const powerwindow_char_T * volatile errorStatus;
+};
+
+/* Block states (auto storage) */
+extern powerwindow_D_Work_powerwindow_PW_Control_D powerwindow_PW_Control_DR_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+extern powerwindow_ExternalInputs_powerwindow_PW_C powerwindow_PW_Control_DRV_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+extern powerwindow_ExternalOutputs_powerwindow_PW_ powerwindow_PW_Control_DRV_Y;
+
+/* Model entry point functions */
+extern void powerwindow_PW_Control_DRV_initialize(void);
+extern void powerwindow_PW_Control_DRV_main(void);
+extern void powerwindow_PW_Control_DRV_terminate(void);
+
+/* Real-time Model object */
+extern powerwindow_RT_MODEL_PW_Control_DRV *const powerwindow_PW_Control_DRV_M;
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Note that this particular code originates from a subsystem build,
+ * and has its own system numbers different from the parent model.
+ * Refer to the system hierarchy for this subsystem below, and use the
+ * MATLAB hilite_system command to trace the generated code back
+ * to the parent model. For example,
+ *
+ * hilite_system('PowerWindow/powerwindow_PW_Control_DRV') - opens subsystem PowerWindow/powerwindow_PW_Control_DRV
+ * hilite_system('PowerWindow/powerwindow_PW_Control_DRV/Kp') - opens and selects block Kp
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'PowerWindow'
+ * '<S1>' : 'PowerWindow/powerwindow_PW_Control_DRV'
+ * '<S2>' : 'PowerWindow/powerwindow_PW_Control_DRV/Debounce_DRV'
+ */
+#endif /* RTW_HEADER_powerwindow_PW_Control_DRV_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h
new file mode 100644
index 00000000..07dc468b
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_DRV_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_powerwindow_PW_Control_DRV_private_h_
+#define powerwindow_RTW_HEADER_powerwindow_PW_Control_DRV_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#endif /* RTW_HEADER_powerwindow_PW_Control_DRV_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h
new file mode 100644
index 00000000..3fa897ad
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h
@@ -0,0 +1,32 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_DRV_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_DRV_types_h_
+#define powerwindow_RTW_HEADER_PW_Control_DRV_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_PW_Control_DRV powerwindow_RT_MODEL_PW_Control_DRV;
+
+#endif /* RTW_HEADER_PW_Control_DRV_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h
new file mode 100644
index 00000000..663edec5
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h
@@ -0,0 +1,126 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackL.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackL_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackL_h_
+#ifndef powerwindow_PW_Control_PSG_BackL_COMMON_INCLUDES_
+# define powerwindow_PW_Control_PSG_BackL_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_rtw_solver.h"
+#endif /* powerwindow_PW_Control_PSG_BackL_COMMON_INCLUDES_*/
+
+#include "../powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Child system includes */
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control.h"
+#include "../powerwindow_HeaderFiles/powerwindow_debounce.h"
+#include "../powerwindow_HeaderFiles/powerwindow_controlexclusion.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_PW_PSG_BackL_BackL_rtmGetErrorStatus
+# define powerwindow_PW_PSG_BackL_rtmGetErrorStatus(rtm) ((rtm)->errorStatus)
+#endif
+
+#ifndef powerwindow_PW_PSG_BackL_rtmSetErrorStatus
+# define powerwindow_PW_PSG_BackL_rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+
+#ifndef powerwindow_PW_PSG_BackL_rtmGetErrorStatusPointer
+# define powerwindow_PW_PSG_BackL_rtmGetErrorStatusPointer(rtm) ((const powerwindow_char_T **)(&((rtm)->errorStatus)))
+#endif
+
+/* Block states (auto storage) for system '<Root>' */
+typedef struct {
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_DWORK1;/* '<S2>/Debounce_Up' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_DWORK1;/* '<S2>/Debounce_Down' */
+ powerwindow_rtMdlrefDWork_PowerWindow_contr PW_PSG_BackL_DWORK1;/* '<S1>/PW_PSG_BackL' */
+} powerwindow_D_Work_PW_Control_PSG_BackL;
+
+/* External inputs (root inport signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T Up_DRV; /* '<Root>/Up_DRV' */
+ powerwindow_boolean_T Down_DRV; /* '<Root>/Down_DRV' */
+ powerwindow_boolean_T endofdetectionrange; /* '<Root>/end of detection range' */
+ powerwindow_uint8_T currentsense; /* '<Root>/current sense' */
+ powerwindow_boolean_T Up_PSG_BackL; /* '<Root>/Up_PSG_BackL' */
+ powerwindow_boolean_T Down_PSG_BackL; /* '<Root>/Down_PSG_BackL' */
+} powerwindow_ExternalInputs_PW_Control_PSG_BackL;
+
+/* External outputs (root outports fed by signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T window_up; /* '<Root>/window_up' */
+ powerwindow_boolean_T window_down; /* '<Root>/window_down' */
+ powerwindow_boolean_T overcurrent; /* '<Root>/overcurrent' */
+ powerwindow_boolean_T pinch; /* '<Root>/pinch' */
+ powerwindow_boolean_T wake; /* '<Root>/wake' */
+} powerwindow_ExternalOutputs_PW_Control_PSG_BackL;
+
+/* Real-time Model Data Structure */
+struct powerwindow_tag_RTM_PW_Control_PSG_BackL {
+ const powerwindow_char_T * volatile errorStatus;
+};
+
+/* Block states (auto storage) */
+extern powerwindow_D_Work_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+extern powerwindow_ExternalInputs_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+extern powerwindow_ExternalOutputs_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_Y;
+
+/* Model entry point functions */
+extern void powerwindow_PW_Control_PSG_BackL_initialize(void);
+extern void powerwindow_PW_Control_PSG_BackL_main(void);
+extern void powerwindow_PW_Control_PSG_BackL_terminate(void);
+
+/* Real-time Model object */
+extern powerwindow_RT_MODEL_PW_Control_PSG_BackL *const powerwindow_PW_Control_PSG_BackL_M;
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Note that this particular code originates from a subsystem build,
+ * and has its own system numbers different from the parent model.
+ * Refer to the system hierarchy for this subsystem below, and use the
+ * MATLAB hilite_system command to trace the generated code back
+ * to the parent model. For example,
+ *
+ * hilite_system('PowerWindow/PW_Control_PSG_BackL') - opens subsystem PowerWindow/PW_Control_PSG_BackL
+ * hilite_system('PowerWindow/PW_Control_PSG_BackL/Kp') - opens and selects block Kp
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'PowerWindow'
+ * '<S1>' : 'PowerWindow/PW_Control_PSG_BackL'
+ * '<S2>' : 'PowerWindow/PW_Control_PSG_BackL/Debounce_PSG_BackL'
+ */
+#endif /* RTW_HEADER_PW_Control_PSG_BackL_h_*/
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h
new file mode 100644
index 00000000..7a5037d6
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackL_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackL_private_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackL_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#endif /* RTW_HEADER_PW_Control_PSG_BackL_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h
new file mode 100644
index 00000000..661f6264
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h
@@ -0,0 +1,32 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackL_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackL_types_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackL_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_PW_Control_PSG_BackL powerwindow_RT_MODEL_PW_Control_PSG_BackL;
+
+#endif /* RTW_HEADER_PW_Control_PSG_BackL_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h
new file mode 100644
index 00000000..383e2855
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h
@@ -0,0 +1,126 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackR.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackR_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackR_h_
+#ifndef powerwindow_PW_Control_PSG_BackR_COMMON_INCLUDES_
+# define powerwindow_PW_Control_PSG_BackR_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_rtw_solver.h"
+#endif /* powerwindow_PW_Control_PSG_BackR_COMMON_INCLUDES_*/
+
+#include "../powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Child system includes */
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control.h"
+#include "../powerwindow_HeaderFiles/powerwindow_debounce.h"
+#include "../powerwindow_HeaderFiles/powerwindow_controlexclusion.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_PW_PSG_BackR_rtmGetErrorStatus
+# define powerwindow_PW_PSG_BackR_rtmGetErrorStatus(rtm) ((rtm)->errorStatus)
+#endif
+
+#ifndef powerwindow_PW_PSG_BackR_rtmSetErrorStatus
+# define powerwindow_PW_PSG_BackR_rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+
+#ifndef powerwindow_PW_PSG_BackR_rtmGetErrorStatusPointer
+# define powerwindow_PW_PSG_BackR_rtmGetErrorStatusPointer(rtm) ((const powerwindow_char_T **)(&((rtm)->errorStatus)))
+#endif
+
+/* Block states (auto storage) for system '<Root>' */
+typedef struct {
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_DWORK1;/* '<S2>/Debounce_Up' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_DWORK1;/* '<S2>/Debounce_Down' */
+ powerwindow_rtMdlrefDWork_PowerWindow_contr PW_PSG_BackR_DWORK1;/* '<S1>/PW_PSG_BackR' */
+} powerwindow_D_Work_PW_Control_PSG_BackR;
+
+/* External inputs (root inport signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T Up_DRV; /* '<Root>/Up_DRV' */
+ powerwindow_boolean_T Down_DRV; /* '<Root>/Down_DRV' */
+ powerwindow_boolean_T endofdetectionrange; /* '<Root>/end of detection range' */
+ powerwindow_uint8_T currentsense; /* '<Root>/current sense' */
+ powerwindow_boolean_T Up_PSG_BackR; /* '<Root>/Up_PSG_BackR' */
+ powerwindow_boolean_T Down_PSG_BackR; /* '<Root>/Down_PSG_BackR' */
+} powerwindow_ExternalInputs_PW_Control_PSG_BackR;
+
+/* External outputs (root outports fed by signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T window_up; /* '<Root>/window_up' */
+ powerwindow_boolean_T window_down; /* '<Root>/window_down' */
+ powerwindow_boolean_T overcurrent; /* '<Root>/overcurrent' */
+ powerwindow_boolean_T pinch; /* '<Root>/pinch' */
+ powerwindow_boolean_T wake; /* '<Root>/wake' */
+} powerwindow_ExternalOutputs_PW_Control_PSG_BackR;
+
+/* Real-time Model Data Structure */
+struct powerwindow_tag_RTM_PW_Control_PSG_BackR {
+ const powerwindow_char_T * volatile errorStatus;
+};
+
+/* Block states (auto storage) */
+extern powerwindow_D_Work_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+extern powerwindow_ExternalInputs_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+extern powerwindow_ExternalOutputs_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_Y;
+
+/* Model entry point functions */
+extern void powerwindow_PW_Control_PSG_BackR_initialize(void);
+extern void powerwindow_PW_Control_PSG_BackR_main(void);
+extern void powerwindow_PW_Control_PSG_BackR_terminate(void);
+
+/* Real-time Model object */
+extern powerwindow_RT_MODEL_PW_Control_PSG_BackR *const powerwindow_PW_Control_PSG_BackR_M;
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Note that this particular code originates from a subsystem build,
+ * and has its own system numbers different from the parent model.
+ * Refer to the system hierarchy for this subsystem below, and use the
+ * MATLAB hilite_system command to trace the generated code back
+ * to the parent model. For example,
+ *
+ * hilite_system('PowerWindow/PW_Control_PSG_BackR') - opens subsystem PowerWindow/PW_Control_PSG_BackR
+ * hilite_system('PowerWindow/PW_Control_PSG_BackR/Kp') - opens and selects block Kp
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'PowerWindow'
+ * '<S1>' : 'PowerWindow/PW_Control_PSG_BackR'
+ * '<S2>' : 'PowerWindow/PW_Control_PSG_BackR/Debounce_PSG'
+ */
+#endif /* RTW_HEADER_PW_Control_PSG_h_*/
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h
new file mode 100644
index 00000000..3dc4a3f6
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackR_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackR_private_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackR_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#endif /* RTW_HEADER_PW_Control_PSG_BackR_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h
new file mode 100644
index 00000000..fc22a6a4
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h
@@ -0,0 +1,31 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackR_tyoes.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_BackR_types_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_BackR_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_PW_Control_PSG_BackR powerwindow_RT_MODEL_PW_Control_PSG_BackR;
+
+#endif /* RTW_HEADER_PW_Control_PSG_BackR_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h
new file mode 100644
index 00000000..9bcdfe8f
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h
@@ -0,0 +1,126 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_Front.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_Front_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_Front_h_
+#ifndef powerwindow_PW_Control_PSG_Front_COMMON_INCLUDES_
+# define powerwindow_PW_Control_PSG_Front_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_rtw_solver.h"
+#endif /* powerwindow_PW_Control_PSG_Front_COMMON_INCLUDES_*/
+
+#include "../powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Child system includes */
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control.h"
+#include "../powerwindow_HeaderFiles/powerwindow_debounce.h"
+#include "../powerwindow_HeaderFiles/powerwindow_controlexclusion.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_PW_PSG_Front_rtmGetErrorStatus
+# define powerwindow_PW_PSG_Front_rtmGetErrorStatus(rtm) ((rtm)->errorStatus)
+#endif
+
+#ifndef powerwindow_PW_PSG_Front_rtmSetErrorStatus
+# define powerwindow_PW_PSG_Front_rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+
+#ifndef powerwindow_PW_PSG_Front_rtmGetErrorStatusPointer
+# define powerwindow_PW_PSG_Front_rtmGetErrorStatusPointer(rtm) ((const powerwindow_char_T **)(&((rtm)->errorStatus)))
+#endif
+
+/* Block states (auto storage) for system '<Root>' */
+typedef struct {
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Up_DWORK1;/* '<S2>/Debounce_Up' */
+ powerwindow_rtMdlrefDWork_debounce_T Debounce_Down_DWORK1;/* '<S2>/Debounce_Down' */
+ powerwindow_rtMdlrefDWork_PowerWindow_contr PW_PSG_Front_Front_DWORK1;/* '<S1>/PW_PSG_Front_Front' */
+} powerwindow_D_Work_PW_Control_PSG_Front;
+
+/* External inputs (root inport signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T Up_DRV; /* '<Root>/Up_DRV' */
+ powerwindow_boolean_T Down_DRV; /* '<Root>/Down_DRV' */
+ powerwindow_boolean_T endofdetectionrange; /* '<Root>/end of detection range' */
+ powerwindow_uint8_T currentsense; /* '<Root>/current sense' */
+ powerwindow_boolean_T Up_PSG_Front; /* '<Root>/Up_PSG_Front' */
+ powerwindow_boolean_T Down_PSG_Front; /* '<Root>/Down_PSG_Front' */
+} powerwindow_ExternalInputs_PW_Control_PSG_Front;
+
+/* External outputs (root outports fed by signals with auto storage) */
+typedef struct {
+ powerwindow_boolean_T window_up; /* '<Root>/window_up' */
+ powerwindow_boolean_T window_down; /* '<Root>/window_down' */
+ powerwindow_boolean_T overcurrent; /* '<Root>/overcurrent' */
+ powerwindow_boolean_T pinch; /* '<Root>/pinch' */
+ powerwindow_boolean_T wake; /* '<Root>/wake' */
+} powerwindow_ExternalOutputs_PW_Control_PSG_Front;
+
+/* Real-time Model Data Structure */
+struct powerwindow_tag_RTM_PW_Control_PSG_Front {
+ const powerwindow_char_T * volatile errorStatus;
+};
+
+/* Block states (auto storage) */
+extern powerwindow_D_Work_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+extern powerwindow_ExternalInputs_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+extern powerwindow_ExternalOutputs_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_Y;
+
+/* Model entry point functions */
+extern void powerwindow_PW_Control_PSG_Front_initialize(void);
+extern void powerwindow_PW_Control_PSG_Front_main(void);
+extern void powerwindow_PW_Control_PSG_Front_terminate(void);
+
+/* Real-time Model object */
+extern powerwindow_RT_MODEL_PW_Control_PSG_Front *const powerwindow_PW_Control_PSG_Front_M;
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Note that this particular code originates from a subsystem build,
+ * and has its own system numbers different from the parent model.
+ * Refer to the system hierarchy for this subsystem below, and use the
+ * MATLAB hilite_system command to trace the generated code back
+ * to the parent model. For example,
+ *
+ * hilite_system('PowerWindow/PW_Control_PSG_Front') - opens subsystem PowerWindow/PW_Control_PSG_Front
+ * hilite_system('PowerWindow/PW_Control_PSG_Front/Kp') - opens and selects block Kp
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'PowerWindow'
+ * '<S1>' : 'PowerWindow/PW_Control_PSG_Front'
+ * '<S2>' : 'PowerWindow/PW_Control_PSG_Front/Debounce_PSG_Front_Front'
+ */
+#endif /* RTW_HEADER_PW_Control_PSG_Front_h_*/
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_private.h
new file mode 100644
index 00000000..0a84437e
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_private.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_Front_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_Front_private_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_Front_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#endif /* RTW_HEADER_PW_Control_PSG_Front_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h
new file mode 100644
index 00000000..38eb15ae
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h
@@ -0,0 +1,33 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_Front_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+
+#ifndef powerwindow_RTW_HEADER_PW_Control_PSG_Front_types_h_
+#define powerwindow_RTW_HEADER_PW_Control_PSG_Front_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_PW_Control_PSG_Front powerwindow_RT_MODEL_PW_Control_PSG_Front;
+
+#endif /* RTW_HEADER_PW_Control_PSG_Front_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion.h
new file mode 100644
index 00000000..d59b8d99
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion.h
@@ -0,0 +1,72 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_controlexclusion.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_controlexclusion.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_controlexclusion_h_
+#define powerwindow_RTW_HEADER_controlexclusion_h_
+#ifndef powerwindow_controlexclusion_COMMON_INCLUDES_
+#define powerwindow_controlexclusion_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+
+#include "../powerwindow_HeaderFiles/powerwindow_rtw_solver.h"
+#endif /* powerwindow_controlexclusion_COMMON_INCLUDES_ */
+
+#include "../powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* user code (top of header file) */
+
+/* Model reference registration function */
+
+extern void powerwindow_controlexclusion_initialize(void);
+extern void powerwindow_controlexclusion_terminate(void);
+extern void powerwindow_controlexclusion_main(const powerwindow_boolean_T *rtu_Up_DRV, const powerwindow_boolean_T
+ *rtu_Down_DRV, const powerwindow_boolean_T *rtu_Up_PSG, const powerwindow_boolean_T *rtu_Down_PSG,
+ powerwindow_boolean_T *rty_Up, powerwindow_boolean_T *rty_Down);
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Use the MATLAB hilite_system command to trace the generated code back
+ * to the model. For example,
+ *
+ * hilite_system('<S3>') - opens system 3
+ * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'ControlExclusion'
+ * '<S1>' : 'ControlExclusion/Control_Clock_TicToc'
+ * '<S2>' : 'ControlExclusion/Control_Clock_TicToc/ControlEx_PSG'
+ * '<S3>' : 'ControlExclusion/Control_Clock_TicToc/Tic'
+ * '<S4>' : 'powerwindow_controlexclusion_main/Control_Clock_TicToc/Toc'
+ * '<S5>' : 'ControlExclusion/Control_Clock_TicToc/Tic/Tic_T'
+ * '<S6>' : 'ControlExclusion/Control_Clock_TicToc/Toc/Toc_T'
+ */
+#endif /* RTW_HEADER_ControlExclusion_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h
new file mode 100644
index 00000000..403fd687
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_controlexclusion_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_controlexclusion_private_h_
+#define powerwindow_RTW_HEADER_controlexclusion_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+#endif /* RTW_HEADER_controlexclusion_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h
new file mode 100644
index 00000000..3e389bff
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h
@@ -0,0 +1,30 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_controlexclusion_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_controlexclusion_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_controlexclusion_types_h_
+#define powerwindow_RTW_HEADER_controlexclusion_types_h_
+
+/* Forward declaration for rtModel */
+#endif /* powerwindow_RTW_HEADER_controlexclusion_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce.h
new file mode 100644
index 00000000..d17f4678
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce.h
@@ -0,0 +1,96 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_debounce.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_debounce.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_debounce_h_
+#define powerwindow_RTW_HEADER_debounce_h_
+#ifndef powerwindow_debounce_COMMON_INCLUDES_
+#define powerwindow_debounce_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#endif /* debounce_COMMON_INCLUDES_ */
+
+#include "../powerwindow_HeaderFiles/powerwindow_debounce_types.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Block signals for model 'powerwindow_debounce_main' */
+typedef struct {
+ powerwindow_boolean_T Q; /* '<Root>/Chart' */
+} powerwindow_rtB_debounce_T;
+
+/* Block states (auto storage) for model 'powerwindow_debounce_main' */
+typedef struct {
+ powerwindow_int32_T clockTickCounter; /* '<Root>/period of 10ms' */
+ powerwindow_uint8_T is_active_c3_debounce; /* '<Root>/Chart' */
+ powerwindow_uint8_T is_c3_debounce; /* '<Root>/Chart' */
+ powerwindow_uint8_T is_debounce; /* '<Root>/Chart' */
+ powerwindow_uint8_T temporalCounter_i1; /* '<Root>/Chart' */
+} powerwindow_rtDW_debounce_T;
+
+/* Zero-crossing (trigger) state for model 'powerwindow_debounce_main' */
+typedef struct {
+ ZCSigState Chart_Trig_ZCE; /* '<Root>/Chart' */
+} powerwindow_rtZCE_debounce_T;
+
+/* Real-time Model Data Structure */
+struct powerwindow_tag_RTM_debounce_T {
+ const powerwindow_char_T **errorStatus;
+};
+
+typedef struct {
+ powerwindow_rtB_debounce_T rtb;
+ powerwindow_rtDW_debounce_T rtdw;
+ powerwindow_RT_MODEL_debounce_T rtm;
+ powerwindow_rtZCE_debounce_T rtzce;
+} powerwindow_rtMdlrefDWork_debounce_T;
+
+/* Model reference registration function */
+extern void powerwindow_debounce_initialize(const powerwindow_char_T **rt_errorStatus,
+ powerwindow_RT_MODEL_debounce_T *const debounce_M, powerwindow_rtB_debounce_T *localB, powerwindow_rtDW_debounce_T
+ *localDW, powerwindow_rtZCE_debounce_T *localZCE);
+extern void powerwindow_debounce_Init(powerwindow_rtB_debounce_T *localB, powerwindow_rtDW_debounce_T *localDW);
+extern void powerwindow_debounce_Start(powerwindow_rtDW_debounce_T *localDW);
+extern void powerwindow_debounce_main(const powerwindow_boolean_T *rtu_Switch, powerwindow_boolean_T
+ *rty_debounced_Switch, powerwindow_rtB_debounce_T *localB,
+ powerwindow_rtDW_debounce_T *localDW, powerwindow_rtZCE_debounce_T *localZCE);
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Use the MATLAB hilite_system command to trace the generated code back
+ * to the model. For example,
+ *
+ * hilite_system('<S3>') - opens system 3
+ * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'powerwindow_debounce_main'
+ * '<S1>' : 'powerwindow_debounce_main/Chart'
+ */
+#endif /* RTW_HEADER_debounce_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_private.h
new file mode 100644
index 00000000..069d44d9
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_private.h
@@ -0,0 +1,46 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_debounce_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_debounce_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+#ifndef powerwindow_RTW_HEADER_debounce_private_h_
+#define powerwindow_RTW_HEADER_debounce_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_rtmGetErrorStatus
+# define powerwindow_rtmGetErrorStatus(rtm) (*((rtm)->errorStatus))
+#endif
+
+#ifndef powerwindow_rtmSetErrorStatus
+# define powerwindow_rtmSetErrorStatus(rtm, val) (*((rtm)->errorStatus) = (val))
+#endif
+
+#ifndef powerwindow_rtmGetErrorStatusPointer
+# define powerwindow_rtmGetErrorStatusPointer(rtm) (rtm)->errorStatus
+#endif
+
+#ifndef powerwindow_rtmSetErrorStatusPointer
+# define powerwindow_rtmSetErrorStatusPointer(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+#endif /* RTW_HEADER_debounce_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_types.h
new file mode 100644
index 00000000..e3d3549e
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_debounce_types.h
@@ -0,0 +1,31 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_debounce_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_debounce_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+#ifndef powerwindow_RTW_HEADER_debounce_types_h_
+#define powerwindow_RTW_HEADER_debounce_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_debounce_T powerwindow_RT_MODEL_debounce_T;
+
+#endif /* RTW_HEADER_debounce_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_model_reference_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_model_reference_types.h
new file mode 100644
index 00000000..c2dda9e1
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_model_reference_types.h
@@ -0,0 +1,51 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_model_reference_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_model_reference_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow___MODEL_REFERENCE_TYPES_H__
+#define powerwindow___MODEL_REFERENCE_TYPES_H__
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#ifndef powerwindow___MODEL_REFERENCE_TYPES__
+#define powerwindow___MODEL_REFERENCE_TYPES__
+
+/*===========================================================================*
+ * Model reference type definitions *
+ *===========================================================================*/
+/*
+ * This structure is used by model reference to
+ * communicate timing information through the hierarchy.
+ */
+typedef struct powerwindow__rtTimingBridge_tag powerwindow_rtTimingBridge;
+struct powerwindow__rtTimingBridge_tag {
+ powerwindow_uint32_T nTasks;
+ powerwindow_uint32_T** clockTick;
+ powerwindow_uint32_T** clockTickH;
+ powerwindow_uint32_T* taskCounter;
+ powerwindow_real_T** taskTime;
+ powerwindow_boolean_T** rateTransition;
+ powerwindow_boolean_T *firstInitCond;
+};
+
+#endif /* __MODEL_REFERENCE_TYPES__ */
+#endif /* __MODEL_REFERENCE_TYPES_H__ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control.h
new file mode 100644
index 00000000..1c0f950f
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control.h
@@ -0,0 +1,135 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_powerwindow_control.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow_RTW_HEADER_PowerWindow_control_h_
+#define powerwindow_RTW_HEADER_PowerWindow_control_h_
+#ifndef powerwindow_powerWindow_control_COMMON_INCLUDES_
+# define powerwindow_powerWindow_control_COMMON_INCLUDES_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+
+#include "../powerwindow_HeaderFiles/powerwindow_rtw_solver.h"
+#endif /* PowerWindow_control_COMMON_INCLUDES_ */
+
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h"
+#include "../powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h"
+
+/* Shared type includes */
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* user code (top of header file) */
+
+/* Block signals for model 'PowerWindow_control' */
+typedef struct {
+ powerwindow_uint8_T RateTransition1; /* '<S2>/Rate Transition1' */
+ powerwindow_boolean_T LogicalOperator; /* '<S2>/Logical Operator' */
+ powerwindow_boolean_T map[3]; /* '<S2>/map' */
+} powerwindow_rtB_PowerWindow_control;
+
+/* Block states (auto storage) for model 'PowerWindow_control' */
+typedef struct {
+ powerwindow_int32_T clockTickCounter; /* '<S2>/period of 50ms' */
+ powerwindow_uint8_T is_active_c2_PowerWindow_contro;/* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_c2_PowerWindow_control; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_Up; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_Down; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_Logic; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_active_Logic; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_Sensing; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T is_active_Sensing; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T temporalCounter_i1; /* '<S2>/stateflow control model' */
+ powerwindow_uint8_T temporalCounter_i2; /* '<S2>/stateflow control model' */
+} powerwindow_rtDW_PowerWindow_control;
+
+/* Zero-crossing (trigger) state for model 'PowerWindow_control' */
+typedef struct {
+ ZCSigState stateflowcontrolmodel_Trig_ZCE;/* '<S2>/stateflow control model' */
+} powerwindow_rtZCE_PowerWindow_control;
+
+/* Real-time Model Data Structure */
+struct powerwindow_tag_RTM_PowerWindow_control{
+ const powerwindow_char_T **errorStatus;
+};
+
+typedef struct {
+ powerwindow_rtB_PowerWindow_control rtb;
+ powerwindow_rtDW_PowerWindow_control rtdw;
+ powerwindow_RT_MODEL_PowerWindow_control rtm;
+ powerwindow_rtZCE_PowerWindow_control rtzce;
+} powerwindow_rtMdlrefDWork_PowerWindow_contr;
+
+
+
+/* Model reference registration function */
+extern void powerwindow_powerwindow_control_initialize(
+ const powerwindow_char_T **rt_errorStatus,
+ powerwindow_RT_MODEL_PowerWindow_control * const PowerWindow_control_M,
+ powerwindow_rtB_PowerWindow_control *localB,
+ powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE);
+extern void powerwindow_powerwindow_control_Init(
+ powerwindow_boolean_T *rty_window_up,
+ powerwindow_boolean_T *rty_window_down,
+ powerwindow_boolean_T *rty_overcurrent,
+ powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T * rty_wake,
+ powerwindow_rtDW_PowerWindow_control *localDW);
+extern void powerwindow_powerwindow_control_Start(
+ powerwindow_rtDW_PowerWindow_control *localDW);
+extern void powerwindow_powerwindow_control_main(
+ const powerwindow_boolean_T *rtu_up,
+ const powerwindow_boolean_T *rtu_down,
+ const powerwindow_boolean_T *rtu_endofdetectionrange,
+ const powerwindow_uint8_T *rtu_currentsense,
+ powerwindow_boolean_T *rty_window_up,
+ powerwindow_boolean_T *rty_window_down,
+ powerwindow_boolean_T *rty_overcurrent,
+ powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T *rty_wake,
+ powerwindow_rtB_PowerWindow_control *localB,
+ powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE);
+
+/*-
+ * The generated code includes comments that allow you to trace directly
+ * back to the appropriate location in the model. The basic format
+ * is <system>/block_name, where system is the system number (uniquely
+ * assigned by Simulink) and block_name is the name of the block.
+ *
+ * Use the MATLAB hilite_system command to trace the generated code back
+ * to the model. For example,
+ *
+ * hilite_system('<S3>') - opens system 3
+ * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
+ *
+ * Here is the system hierarchy for this model
+ *
+ * '<Root>' : 'PowerWindow_control'
+ * '<S1>' : 'PowerWindow_control/PW_PSG'
+ * '<S2>' : 'PowerWindow_control/PW_PSG/PW_PSG'
+ * '<S3>' : 'PowerWindow_control/PW_PSG/Tic'
+ * '<S4>' : 'PowerWindow_control/PW_PSG/Toc'
+ * '<S5>' : 'PowerWindow_control/PW_PSG/PW_PSG/stateflow control model'
+ * '<S6>' : 'PowerWindow_control/PW_PSG/Tic/Tic_T'
+ * '<S7>' : 'PowerWindow_control/PW_PSG/Toc/Toc_T'
+ */
+#endif /* powerwindow_RTW_HEADER_PowerWindow_control_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h
new file mode 100644
index 00000000..47334915
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h
@@ -0,0 +1,52 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_powerwindow_control_private.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+#ifndef powerwindow_RTW_HEADER_PowerWindow_control_private_h_
+#define powerwindow_RTW_HEADER_PowerWindow_control_private_h_
+#include "../powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+#include "../powerwindow_HeaderFiles/powerwindow_model_reference_types.h"
+
+/* Macros for accessing real-time model data structure */
+#ifndef powerwindow_powerwindow_control_rtmGetErrorStatus
+# define powerwindow_powerwindow_control_rtmGetErrorStatus(rtm) (*((rtm)->errorStatus))
+#endif
+
+#ifndef powerwindow_powerwindow_control_rtmSetErrorStatus
+# define powerwindow_powerwindow_control_rtmSetErrorStatus(rtm, val) (*((rtm)->errorStatus) = (val))
+#endif
+
+#ifndef powerwindow_powerwindow_control_rtmGetErrorStatusPointer
+# define powerwindow_powerwindow_control_rtmGetErrorStatusPointer(rtm) (rtm)->errorStatus
+#endif
+
+#ifndef powerwindow_powerwindow_control_rtmSetErrorStatusPointer
+# define powerwindow_powerwindow_control_rtmSetErrorStatusPointer(rtm, val) ((rtm)->errorStatus = (val))
+#endif
+
+extern const powerwindow_boolean_T powerwindow_rtCP_pooled_6bUUQf1tASYw[12];
+
+#define rtCP_map_table powerwindow_rtCP_pooled_6bUUQf1tASYw /* Computed Parameter: map_table
+ * Referenced by: '<S2>/map'
+ */
+#endif /* RTW_HEADER_PowerWindow_control_private_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h
new file mode 100644
index 00000000..0f96b568
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h
@@ -0,0 +1,33 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_powerwindow_control_types.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_powerwindow_control_types.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+
+#ifndef powerwindow_RTW_HEADER_powerwindow_control_types_h_
+#define powerwindow_RTW_HEADER_powerwindow_control_types_h_
+
+/* Forward declaration for rtModel */
+typedef struct powerwindow_tag_RTM_PowerWindow_control powerwindow_RT_MODEL_PowerWindow_control;
+
+#endif /* RTW_HEADER_powerwindow_control_types_h_ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_continuous.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_continuous.h
new file mode 100644
index 00000000..5cab83fd
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_continuous.h
@@ -0,0 +1,136 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_rtw_continuous.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_rtw_continuous.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow___RTW_CONTINUOUS_H__
+#define powerwindow___RTW_CONTINUOUS_H__
+
+//#include "../powerwindow_HeaderFiles/powerwindow_tmwtypes.h"
+
+/* For models registering MassMatrix */
+typedef enum {
+ SS_MATRIX_NONE,
+ SS_MATRIX_CONSTANT,
+ SS_MATRIX_TIMEDEP,
+ SS_MATRIX_STATEDEP
+} powerwindow_ssMatrixType;
+
+typedef enum {
+ SOLVER_MODE_AUTO, /* only occurs in
+ mdlInitializeSizes/mdlInitializeSampleTimes */
+ SOLVER_MODE_SINGLETASKING,
+ SOLVER_MODE_MULTITASKING
+} powerwindow_SolverMode;
+
+typedef enum {
+ MINOR_TIME_STEP,
+ MAJOR_TIME_STEP
+} powerwindow_SimTimeStep;
+
+/* =============================================================================
+ * Model methods object
+ * =============================================================================
+ */
+typedef void (*powerwindow_rtMdlInitializeSizesFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlInitializeSampleTimesFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlStartFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlOutputsFcn)(void *rtModel, powerwindow_int_T tid);
+typedef void (*powerwindow_rtMdlUpdateFcn)(void *rtModel, powerwindow_int_T tid);
+typedef void (*powerwindow_rtMdlDerivativesFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlProjectionFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlMassMatrixFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlForcingFunctionFcn)(void *rtModel);
+typedef void (*powerwindow_rtMdlTerminateFcn)(void *rtModel);
+#ifdef RT_MALLOC
+typedef real_T (*rtMdlDiscreteEventsFcn)(void *pModel,
+ powerwindow_int_T rtmNumSampTimes,
+ void *rtmTimingData,
+ powerwindow_int_T *rtmSampleHitPtr,
+ powerwindow_int_T *rtmPerTaskSampleHits);
+#endif
+
+typedef struct powerwindow__RTWRTModelMethodsInfo_tag {
+ void *rtModelPtr;
+ powerwindow_rtMdlInitializeSizesFcn rtmInitSizesFcn;
+ powerwindow_rtMdlInitializeSampleTimesFcn rtmInitSampTimesFcn;
+ powerwindow_rtMdlStartFcn rtmStartFcn;
+ powerwindow_rtMdlOutputsFcn rtmOutputsFcn;
+ powerwindow_rtMdlUpdateFcn rtmUpdateFcn;
+ powerwindow_rtMdlDerivativesFcn rtmDervisFcn;
+ powerwindow_rtMdlProjectionFcn rtmProjectionFcn;
+ powerwindow_rtMdlMassMatrixFcn rtmMassMatrixFcn;
+ powerwindow_rtMdlForcingFunctionFcn rtmForcingFunctionFcn;
+ powerwindow_rtMdlTerminateFcn rtmTerminateFcn;
+#ifdef RT_MALLOC
+ rtMdlDiscreteEventsFcn rtmDiscreteEventsFcn;
+#endif
+} powerwindow_RTWRTModelMethodsInfo;
+
+#define rtmiSetRTModelPtr(M,rtmp) ((M).rtModelPtr = (rtmp))
+#define rtmiGetRTModelPtr(M) (M).rtModelPtr
+
+#define rtmiSetInitSizesFcn(M,fp) \
+ ((M).rtmInitSizesFcn = ((powerwindow_rtMdlInitializeSizesFcn)(fp)))
+#define rtmiSetInitSampTimesFcn(M,fp) \
+ ((M).rtmInitSampTimesFcn = ((powerwindow_rtMdlInitializeSampleTimesFcn)(fp)))
+#define rtmiSetStartFcn(M,fp) \
+ ((M).rtmStartFcn = ((powerwindow_rtMdlStartFcn)(fp)))
+#define rtmiSetOutputsFcn(M,fp) \
+ ((M).rtmOutputsFcn = ((powerwindow_rtMdlOutputsFcn)(fp)))
+#define rtmiSetUpdateFcn(M,fp) \
+ ((M).rtmUpdateFcn = ((powerwindow_rtMdlUpdateFcn)(fp)))
+#define rtmiSetDervisFcn(M,fp) \
+ ((M).rtmDervisFcn = ((powerwindow_rtMdlDerivativesFcn)(fp)))
+#define rtmiSetProjectionFcn(M,fp) \
+ ((M).rtmProjectionFcn = ((powerwindow_rtMdlProjectionFcn)(fp)))
+#define rtmiSetMassMatrixFcn(M,fp) \
+ ((M).rtmMassMatrixFcn = ((powerwindow_rtMdlMassMatrixFcn)(fp)))
+#define rtmiSetForcingFunctionFcn(M,fp) \
+ ((M).rtmForcingFunctionFcn = ((powerwindow_rtMdlForcingFunctionFcn)(fp)))
+#define rtmiSetTerminateFcn(M,fp) \
+ ((M).rtmTerminateFcn = ((powerwindow_rtMdlTerminateFcn)(fp)))
+#ifdef RT_MALLOC
+#define rtmiSetDiscreteEventsFcn(M,fp) \
+ ((M).rtmDiscreteEventsFcn = ((rtMdlDiscreteEventsFcn)(fp)))
+#endif
+
+#define rtmiInitializeSizes(M) \
+ ((*(M).rtmInitSizesFcn)((M).rtModelPtr))
+#define rtmiInitializeSampleTimes(M) \
+ ((*(M).rtmInitSampTimesFcn)((M).rtModelPtr))
+#define rtmiStart(M) \
+ ((*(M).rtmStartFcn)((M).rtModelPtr))
+#define rtmiOutputs(M, tid) \
+ ((*(M).rtmOutputsFcn)((M).rtModelPtr,tid))
+#define rtmiUpdate(M, tid) \
+ ((*(M).rtmUpdateFcn)((M).rtModelPtr,tid))
+#define rtmiDerivatives(M) \
+ ((*(M).rtmDervisFcn)((M).rtModelPtr))
+#define rtmiProjection(M) \
+ ((*(M).rtmProjectionFcn)((M).rtModelPtr))
+#define rtmiMassMatrix(M) \
+ ((*(M).rtmMassMatrixFcn)((M).rtModelPtr))
+#define rtmiForcingFunction(M) \
+ ((*(M).rtmForcingFunctionFcn)((M).rtModelPtr))
+#define rtmiTerminate(M) \
+ ((*(M).rtmTerminateFcn)((M).rtModelPtr))
+#ifdef RT_MALLOC
+#define rtmiDiscreteEvents(M,x1,x2,x3,x4) \
+ ((*(M).rtmDiscreteEventsFcn)((M).rtModelPtr,(x1),(x2),(x3),(x4)))
+#endif
+#endif /* __RTW_CONTINUOUS_H__ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_solver.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_solver.h
new file mode 100644
index 00000000..2e0895ea
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtw_solver.h
@@ -0,0 +1,243 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_rtw_solver.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_rtw_solver.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#ifndef powerwindow___RTW_SOLVER_H__
+#define powerwindow___RTW_SOLVER_H__
+
+/* =============================================================================
+ * Solver object
+ * =============================================================================
+ */
+#ifndef NO_FLOATS /* ERT integer-only */
+/*
+ * Enum for solver tolerance
+ */
+typedef enum {
+ SL_SOLVER_TOLERANCE_AUTO = 0, /* Set Automatically by Solver */
+ SL_SOLVER_TOLERANCE_LOCAL = 1, /* Set Locally, e.g., by Blocks */
+ SL_SOLVER_TOLERANCE_GLOBAL = 2, /* Set Globally, e.g., by Block Diagram */
+ SL_SOLVER_TOLERANCE_UNDEFINED = 255 /* Signal uninitialized */
+} powerwindow_SL_SolverToleranceControlFlag_T;
+
+
+/*
+ * Enum for jacobian method control
+ */
+typedef enum {
+ SL_JM_BD_AUTO = 0,
+ SL_JM_BD_SPARSE_PERTURBATION,
+ SL_JM_BD_FULL_PERTURBATION,
+ SL_JM_BD_SPARSE_ANALYTICAL,
+ SL_JM_BD_FULL_ANALYTICAL
+} powerwindow_slJmBdControl;
+
+
+typedef struct _ssSolverInfo_tag {
+ void *rtModelPtr;
+
+
+ const char *solverName;
+ powerwindow_boolean_T isVariableStepSolver;
+ powerwindow_boolean_T solverNeedsReset;
+
+ powerwindow_time_T solverStopTime;
+ powerwindow_time_T *stepSizePtr;
+ powerwindow_time_T minStepSize;
+ powerwindow_time_T maxStepSize;
+ powerwindow_time_T fixedStepSize;
+
+ powerwindow_int_T solverShapePreserveControl;
+ powerwindow_int_T solverMaxConsecutiveMinStep;
+ powerwindow_int_T maxNumMinSteps;
+ powerwindow_int_T solverMaxOrder;
+ powerwindow_real_T solverConsecutiveZCsStepRelTol;
+ powerwindow_int_T solverMaxConsecutiveZCs;
+
+ powerwindow_int_T solverExtrapolationOrder;
+ powerwindow_int_T solverNumberNewtonIterations;
+
+ powerwindow_int_T solverRefineFactor;
+ powerwindow_real_T solverRelTol;
+ powerwindow_real_T unused_real_T_1;
+
+ powerwindow_real_T **dXPtr;
+ powerwindow_time_T **tPtr;
+
+ powerwindow_int_T *numContStatesPtr;
+ powerwindow_real_T **contStatesPtr;
+
+ powerwindow_real_T* zcSignalVector;
+ powerwindow_uint8_T* zcEventsVector;
+ powerwindow_uint8_T* zcSignalAttrib;
+ powerwindow_int_T zcSignalVectorLength;
+ powerwindow_uint8_T* reserved;
+
+ powerwindow_boolean_T foundContZcEvents;
+ powerwindow_boolean_T isAtLeftPostOfContZcEvent;
+ powerwindow_boolean_T isAtRightPostOfContZcEvent;
+ powerwindow_boolean_T adaptiveZcDetection;
+
+ powerwindow_int_T numZcSignals;
+
+ powerwindow_boolean_T stateProjection;
+ powerwindow_boolean_T robustResetMethod; /* user's preference */
+ powerwindow_boolean_T updateJacobianAtReset; /* S-Fcn request (sticky) */
+ powerwindow_boolean_T consistencyChecking;
+
+ powerwindow_int_T massMatrixNzMax;
+ powerwindow_int_T* massMatrixIr;
+ powerwindow_int_T* massMatrixJc;
+ powerwindow_real_T* massMatrixPr;
+
+ const powerwindow_char_T **errStatusPtr;
+
+ powerwindow_real_T zcThreshold;
+ powerwindow_int_T zeroCrossAlgorithm;
+ powerwindow_int_T consecutiveZCsError;
+
+ powerwindow_boolean_T isComputingJacobian;
+ powerwindow_slJmBdControl solverJacobianMethodControl;
+ powerwindow_int_T ignoredZcDiagnostic;
+ powerwindow_int_T maskedZcDiagnostic;
+ powerwindow_boolean_T isOutputMethodComputed;
+} ssSolverInfo;
+
+/* Support old name RTWSolverInfo */
+typedef ssSolverInfo RTWSolverInfo;
+
+#define rtsiSetRTModelPtr(S,rtmp) ((S)->rtModelPtr = (rtmp))
+#define rtsiGetRTModelPtr(S) (S)->rtModelPtr
+
+#define rtsiSetSimTimeStepPtr(S,stp) ((S)->simTimeStepPtr = (stp))
+#define rtsiGetSimTimeStepPtr(S) ((S)->simTimeStepPtr)
+#define rtsiGetSimTimeStep(S) *((S)->simTimeStepPtr)
+#define rtsiSetSimTimeStep(S,st) (*((S)->simTimeStepPtr) = (st))
+
+#define rtsiSetSolverData(S,sd) ((S)->solverData = (sd))
+#define rtsiGetSolverData(S) (S)->solverData
+
+#define rtsiSetSolverName(S,sn) ((S)->solverName = (sn))
+#define rtsiGetSolverName(S) (S)->solverName
+
+#define rtsiSetVariableStepSolver(S,vs) ((S)->isVariableStepSolver = (vs))
+#define rtsiIsVariableStepSolver(S) (S)->isVariableStepSolver
+
+#define rtsiSetSolverNeedsReset(S,sn) ((S)->solverNeedsReset = (sn))
+#define rtsiGetSolverNeedsReset(S) (S)->solverNeedsReset
+
+#define rtsiSetBlkStateChange(S,sn) ((S)->blkStateChange = (sn))
+#define rtsiGetBlkStateChange(S) (S)->blkStateChange
+
+#define rtsiSetSolverMode(S,sm) ((S)->solverMode = (sm))
+#define rtsiGetSolverMode(S) (S)->solverMode
+
+#define rtsiSetSolverStopTime(S,st) ((S)->solverStopTime = (st))
+#define rtsiGetSolverStopTime(S) (S)->solverStopTime
+
+#define rtsiSetStepSizePtr(S,ssp) ((S)->stepSizePtr = (ssp))
+#define rtsiSetStepSize(S,ss) (*((S)->stepSizePtr) = (ss))
+#define rtsiGetStepSize(S) *((S)->stepSizePtr)
+
+#define rtsiSetMinStepSize(S,ss) (((S)->minStepSize = (ss)))
+#define rtsiGetMinStepSize(S) (S)->minStepSize
+
+#define rtsiSetMaxStepSize(S,ss) ((S)->maxStepSize = (ss))
+#define rtsiGetMaxStepSize(S) (S)->maxStepSize
+
+#define rtsiSetFixedStepSize(S,ss) ((S)->fixedStepSize = (ss))
+#define rtsiGetFixedStepSize(S) (S)->fixedStepSize
+
+#define rtsiSetMaxNumMinSteps(S,mns) ((S)->maxNumMinSteps = (mns))
+#define rtsiGetMaxNumMinSteps(S) (S)->maxNumMinSteps
+
+#define rtsiSetSolverMaxOrder(S,smo) ((S)->solverMaxOrder = (smo))
+#define rtsiGetSolverMaxOrder(S) (S)->solverMaxOrder
+
+#define rtsiSetSolverJacobianMethodControl(S,smcm) (ssGetSolverInfo(S)->solverJacobianMethodControl = (smcm))
+#define rtsiGetSolverJacobianMethodControl(S) ssGetSolverInfo(S)->solverJacobianMethodControl
+
+#define rtsiSetSolverShapePreserveControl(S,smcm) (ssGetSolverInfo(S)->solverShapePreserveControl = (smcm))
+#define rtsiGetSolverShapePreserveControl(S) ssGetSolverInfo(S)->solverShapePreserveControl
+
+#define rtsiSetSolverConsecutiveZCsStepRelTol(S,scr) (ssGetSolverInfo(S)->solverConsecutiveZCsStepRelTol = (scr))
+#define rtsiGetSolverConsecutiveZCsStepRelTol(S) ssGetSolverInfo(S)->solverConsecutiveZCsStepRelTol
+
+#define rtsiSetSolverMaxConsecutiveZCs(S,smcz) (ssGetSolverInfo(S)->solverMaxConsecutiveZCs = (smcz))
+#define rtsiGetSolverMaxConsecutiveZCs(S) ssGetSolverInfo(S)->solverMaxConsecutiveZCs
+
+#define rtsiSetSolverMaxConsecutiveMinStep(S,smcm) (ssGetSolverInfo(S)->solverMaxConsecutiveMinStep = (smcm))
+#define rtsiGetSolverMaxConsecutiveMinStep(S) ssGetSolverInfo(S)->solverMaxConsecutiveMinStep
+
+#define rtsiSetSolverExtrapolationOrder(S,seo) ((S)->solverExtrapolationOrder = (seo))
+#define rtsiGetSolverExtrapolationOrder(S) (S)->solverExtrapolationOrder
+
+#define rtsiSetSolverNumberNewtonIterations(S,nni) ((S)->solverNumberNewtonIterations = (nni))
+#define rtsiGetSolverNumberNewtonIterations(S) (S)->solverNumberNewtonIterations
+
+#define rtsiSetSolverRefineFactor(S,smo) ((S)->solverRefineFactor = (smo))
+#define rtsiGetSolverRefineFactor(S) (S)->solverRefineFactor
+
+#define rtsiSetSolverRelTol(S,smo) ((S)->solverRelTol = (smo))
+#define rtsiGetSolverRelTol(S) (S)->solverRelTol
+
+#define rtsiSetSolverMassMatrixType(S,type) ((S)->massMatrixType = (type))
+#define rtsiGetSolverMassMatrixType(S) (S)->massMatrixType
+
+#define rtsiSetSolverMassMatrixNzMax(S,nzMax) ((S)->massMatrixNzMax = (nzMax))
+#define rtsiGetSolverMassMatrixNzMax(S) (S)->massMatrixNzMax
+
+#define rtsiSetSolverMassMatrixIr(S,ir) ((S)->massMatrixIr = (ir))
+#define rtsiGetSolverMassMatrixIr(S) (S)->massMatrixIr
+
+#define rtsiSetSolverMassMatrixJc(S,jc) ((S)->massMatrixJc = (jc))
+#define rtsiGetSolverMassMatrixJc(S) (S)->massMatrixJc
+
+#define rtsiSetSolverMassMatrixPr(S,pr) ((S)->massMatrixPr = (pr))
+#define rtsiGetSolverMassMatrixPr(S) (S)->massMatrixPr
+
+#define rtsiSetdXPtr(S,dxp) ((S)->dXPtr = (dxp))
+#define rtsiSetdX(S,dx) (*((S)->dXPtr) = (dx))
+#define rtsiGetdX(S) *((S)->dXPtr)
+
+#define rtsiSetTPtr(S,tp) ((S)->tPtr = (tp))
+#define rtsiSetT(S,t) ((*((S)->tPtr))[0] = (t))
+#define rtsiGetT(S) (*((S)->tPtr))[0]
+
+#define rtsiSetContStatesPtr(S,cp) ((S)->contStatesPtr = (cp))
+#define rtsiGetContStates(S) *((S)->contStatesPtr)
+
+#define rtsiSetNumContStatesPtr(S,cp) ((S)->numContStatesPtr = (cp))
+#define rtsiGetNumContStates(S) *((S)->numContStatesPtr)
+
+#define rtsiSetErrorStatusPtr(S,esp) ((S)->errStatusPtr = (esp))
+#define rtsiSetErrorStatus(S,es) (*((S)->errStatusPtr) = (es))
+#define rtsiGetErrorStatus(S) *((S)->errStatusPtr)
+
+#define rtsiSetModelMethodsPtr(S,mmp) ((S)->modelMethodsPtr = (mmp))
+#define rtsiGetModelMethodsPtr(S) (S)->modelMethodsPtr
+
+#define rtsiSetSolverComputingJacobian(S,val) ((S)->isComputingJacobian = (val))
+#define rtsiIsSolverComputingJacobian(S) (S)->isComputingJacobian
+
+#define rtsiSetSolverOutputComputed(S,val) ((S)->isOutputMethodComputed = (val))
+#define rtsiIsSolverOutputComputed(S) (S)->isOutputMethodComputed
+
+#endif /* !NO_FLOATS */
+
+#endif /* powerwindow___RTW_SOLVER_H__ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtwtypes.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtwtypes.h
new file mode 100644
index 00000000..c6dfca07
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_rtwtypes.h
@@ -0,0 +1,194 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_rtwtypes.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_rtwtypes.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+#ifndef powerwindow__RTWTYPES_H__
+#define powerwindow__RTWTYPES_H__
+
+/* Logical type definitions */
+#if (!defined(__cplusplus))
+# ifndef false
+# define false (0U)
+# endif
+
+# ifndef true
+# define true (1U)
+# endif
+#endif
+
+#define powerwindow__TMWTYPES__
+
+/*=======================================================================*
+ * Target hardware information
+ *
+ * Number of bits: char: 8 short: 16 int: 16
+ * long: 32
+ * native word size: 8
+ * Byte ordering: LittleEndian
+ * Signed integer division rounds to: Zero
+ * Shift right on a signed integer as arithmetic shift: on
+ *=======================================================================*/
+
+/*=======================================================================*
+ * Fixed width word size data types: *
+ * powerwindow_int8_T, powerwindow_int16_T, powerwindow_int32_T - signed 8, 16, or 32 bit integers *
+ * powerwindow_uint8_T, powerwindow_uint16_T, powerwindow_uint32_T - unsigned 8, 16, or 32 bit integers *
+ * powerwindow_real32_T, powerwindow_real64_T - 32 and 64 bit floating point numbers *
+ *=======================================================================*/
+typedef signed char powerwindow_int8_T;
+typedef unsigned char powerwindow_uint8_T;
+typedef int powerwindow_int16_T;
+typedef unsigned int powerwindow_uint16_T;
+typedef long powerwindow_int32_T;
+typedef unsigned long powerwindow_uint32_T;
+typedef float powerwindow_real32_T;
+typedef double powerwindow_real64_T;
+
+/*===========================================================================*
+ * Generic type definitions: powerwindow_real_T, powerwindow_time_T, powerwindow_boolean_T, powerwindow_int_T, powerwindow_uint_T, *
+ * powerwindow_ulong_T, powerwindow_char_T and powerwindow_byte_T. *
+ *===========================================================================*/
+typedef double powerwindow_real_T;
+typedef double powerwindow_time_T;
+typedef unsigned char powerwindow_boolean_T;
+typedef int powerwindow_int_T;
+typedef unsigned int powerwindow_uint_T;
+typedef unsigned long powerwindow_ulong_T;
+typedef char powerwindow_char_T;
+typedef unsigned char powerwindow_uchar_T;
+typedef powerwindow_char_T powerwindow_byte_T;
+
+/*===========================================================================*
+ * Complex number type definitions *
+ *===========================================================================*/
+#define powerwindow_CREAL_T
+
+typedef struct {
+ powerwindow_real32_T re;
+ powerwindow_real32_T im;
+} powerwindow_creal32_T;
+
+typedef struct {
+ powerwindow_real64_T re;
+ powerwindow_real64_T im;
+} powerwindow_creal64_T;
+
+typedef struct {
+ powerwindow_real_T re;
+ powerwindow_real_T im;
+} powerwindow_creal_T;
+
+#define powerwindow_CINT8_T
+
+typedef struct {
+ powerwindow_int8_T re;
+ powerwindow_int8_T im;
+} powerwindow_cint8_T;
+
+#define powerwindow_CUINT8_T
+
+typedef struct {
+ powerwindow_uint8_T re;
+ powerwindow_uint8_T im;
+} powerwindow_cuint8_T;
+
+#define powerwindow_CINT16_T
+
+typedef struct {
+ powerwindow_int16_T re;
+ powerwindow_int16_T im;
+} powerwindow_cint16_T;
+
+#define powerwindow_CUINT16_T
+
+typedef struct {
+ powerwindow_uint16_T re;
+ powerwindow_uint16_T im;
+} powerwindow_cuint16_T;
+
+#define powerwindow_CINT32_T
+
+typedef struct {
+ powerwindow_int32_T re;
+ powerwindow_int32_T im;
+} powerwindow_cint32_T;
+
+#define powerwindow_CUINT32_T
+
+typedef struct {
+ powerwindow_uint32_T re;
+ powerwindow_uint32_T im;
+} powerwindow_cuint32_T;
+
+/*=======================================================================*
+ * Min and Max: *
+ * powerwindow_int8_T, powerwindow_int16_T, powerwindow_int32_T - signed 8, 16, or 32 bit integers *
+ * powerwindow_uint8_T, powerwindow_uint16_T, powerwindow_uint32_T - unsigned 8, 16, or 32 bit integers *
+ *=======================================================================*/
+#define powerwindow_MAX_int8_T ((powerwindow_int8_T)(127))
+#define powerwindow_MIN_int8_T ((powerwindow_int8_T)(-128))
+#define powerwindow_MAX_uint8_T ((powerwindow_uint8_T)(255U))
+#define powerwindow_MIN_uint8_T ((powerwindow_uint8_T)(0U))
+#define powerwindow_MAX_int16_T ((powerwindow_int16_T)(32767))
+#define powerwindow_MIN_int16_T ((powerwindow_int16_T)(-32768))
+#define powerwindow_MAX_uint16_T ((powerwindow_uint16_T)(65535U))
+#define powerwindow_MIN_uint16_T ((powerwindow_uint16_T)(0U))
+#define powerwindow_MAX_int32_T ((powerwindow_int32_T)(2147483647))
+#define powerwindow_MIN_int32_T ((powerwindow_int32_T)(-2147483647-1))
+#define powerwindow_MAX_uint32_T ((powerwindow_uint32_T)(0xFFFFFFFFU))
+#define powerwindow_MIN_uint32_T ((powerwindow_uint32_T)(0U))
+
+/* Block D-Work pointer type */
+typedef void * powerwindow_pointer_T;
+
+#define powerwindow_input_length 977
+
+/* Simulink specific types */
+#ifndef powerwindow___ZERO_CROSSING_TYPES__
+#define powerwindow___ZERO_CROSSING_TYPES__
+
+/* Trigger directions: falling, either, and rising */
+typedef enum {
+ FALLING_ZERO_CROSSING = -1,
+ ANY_ZERO_CROSSING = 0,
+ RISING_ZERO_CROSSING = 1
+} powerwindow_ZCDirection;
+
+/* Previous state of a trigger signal */
+typedef powerwindow_uint8_T ZCSigState;
+
+/* Initial value of a trigger zero crossing signal */
+#define powerwindow_UNINITIALIZED_ZCSIG 0x03U
+#define powerwindow_NEG_ZCSIG 0x02U
+#define powerwindow_POS_ZCSIG 0x01U
+#define powerwindow_ZERO_ZCSIG 0x00U
+
+/* Current state of a trigger signal */
+typedef enum {
+ FALLING_ZCEVENT = -1,
+ NO_ZCEVENT = 0,
+ RISING_ZCEVENT = 1
+} powerwindow_ZCEventType;
+
+#endif /* powerwindow___ZERO_CROSSING_TYPES__ */
+#endif /* powerwindow__RTWTYPES_H__ */
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_tmwtypes.h b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_tmwtypes.h
new file mode 100644
index 00000000..7b1b7f34
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_HeaderFiles/powerwindow_tmwtypes.h
@@ -0,0 +1,821 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_tmwtypes.h
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: headerfile
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_HeaderFiles/powerwindow_tmwtypes.h
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+#if defined(_MSC_VER)
+# pragma once
+#endif
+#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))
+# pragma once
+#endif
+
+#ifndef powerwindow_tmwtypes_h
+#define powerwindow_tmwtypes_h
+
+#ifndef powerwindow__TMWTYPES__
+#define powerwindow__TMWTYPES__
+/*
+ * File : tmwtypes.h
+ * Abstract:
+ * Data types for use with MATLAB/SIMULINK and the Real-Time Workshop.
+ *
+ * When compiling stand-alone model code, data types can be overridden
+ * via compiler switches.
+ *
+ * Define NO_FLOATS to eliminate reference to powerwindow_real_T, etc.
+ */
+
+
+#define LOGICAL_IS_A_TYPE
+#define SPARSE_GENERALIZATION
+
+#ifdef NO_FLOATS
+# define double double_not_allowed
+# define float float_not_allowed
+#endif /*NO_FLOATS*/
+
+
+/*
+ * The following data types cannot be overridden when building MEX files.
+ */
+#ifdef MATLAB_MEX_FILE
+# undef CHARACTER_T
+# undef INTEGER_T
+# undef BOOLEAN_T
+# undef REAL_T
+# undef TIME_T
+#endif
+
+/*
+ * The powerwindow_uchar_T, powerwindow_ushort_T and powerwindow_ulong_T types are needed for compilers which do
+ * not allow defines to be specified, at the command line, with spaces in them.
+ */
+
+typedef unsigned char powerwindow_uchar_T;
+typedef unsigned short powerwindow_ushort_T;
+typedef unsigned long powerwindow_ulong_T;
+
+#if (defined(_MSC_VER) && _MSC_VER >= 1500) \
+ || defined(__x86_64__) || defined(__LP64__) \
+ || defined(__LCC64__)
+
+typedef unsigned long long powerwindow_ulonglong_T;
+#endif
+
+
+
+/*=======================================================================*
+ * Fixed width word size data types: *
+ * powerwindow_int8_T, powerwindow_int16_T, powerwindow_int32_T - signed 8, 16, or 32 bit integers *
+ * powerwindow_uint8_T, powerwindow_uint16_T, powerwindow_uint32_T - unsigned 8, 16, or 32 bit integers *
+ * powerwindow_real32_T, powerwindow_real64_T - 32 and 64 bit floating point numbers *
+ *=======================================================================*/
+
+/* When used with Real Time Workshop generated code, this
+ * header file can be used with a variety of compilers.
+ *
+ * The compiler could be for an 8 bit embedded processor that
+ * only had 8 bits per integer and 16 bits per long.
+ * In that example, a 32 bit integer size is not even available.
+ * This header file should be robust to that.
+ *
+ * For the case of an 8 bit processor, the preprocessor
+ * may be limited to 16 bit math like its target. That limitation
+ * would mean that 32 bit comparisons can't be done accurately.
+ * To increase robustness to this, comparisons are done against
+ * smaller values first. An inaccurate 32 bit comparison isn't
+ * attempted if the 16 bit comparison has already succeeded.
+ *
+ * Limitations on preprocessor math can also be stricter than
+ * for the target. There are known cases where a compiler
+ * targeting processors with 64 bit longs can't do accurate
+ * preprocessor comparisons on more than 32 bits.
+ */
+
+/* Determine the number of bits for int, long, short, and char.
+ * If one fails to be determined, set the number of bits to -1
+ */
+
+#ifndef TMW_BITS_PER_INT
+# if INT_MAX == 0x7FL
+# define TMW_BITS_PER_INT 8
+# elif INT_MAX == 0x7FFFL
+# define TMW_BITS_PER_INT 16
+# elif INT_MAX == 0x7FFFFFFFL
+# define TMW_BITS_PER_INT 32
+# else
+# define TMW_BITS_PER_INT -1
+# endif
+#endif
+
+#ifndef TMW_BITS_PER_LONG
+# if LONG_MAX == 0x7FL
+# define TMW_BITS_PER_LONG 8
+# elif LONG_MAX == 0x7FFFL
+# define TMW_BITS_PER_LONG 16
+# elif LONG_MAX == 0x7FFFFFFFL
+# define TMW_BITS_PER_LONG 32
+# else
+# define TMW_BITS_PER_LONG -1
+# endif
+#endif
+
+#ifndef TMW_BITS_PER_SHRT
+# if SHRT_MAX == 0x7FL
+# define TMW_BITS_PER_SHRT 8
+# elif SHRT_MAX == 0x7FFFL
+# define TMW_BITS_PER_SHRT 16
+# elif SHRT_MAX == 0x7FFFFFFFL
+# define TMW_BITS_PER_SHRT 32
+# else
+# define TMW_BITS_PER_SHRT -1
+# endif
+#endif
+
+#ifndef TMW_BITS_PER_SCHAR
+# if SCHAR_MAX == 0x7FL
+# define TMW_BITS_PER_SCHAR 8
+# elif SCHAR_MAX == 0x7FFFL
+# define TMW_BITS_PER_SCHAR 16
+# elif SCHAR_MAX == 0x7FFFFFFFL
+# define TMW_BITS_PER_SCHAR 32
+# else
+# define TMW_BITS_PER_SCHAR -1
+# endif
+#endif
+
+#ifndef TMW_CHAR_SIGNED
+# if SCHAR_MAX == CHAR_MAX
+# define TMW_CHAR_SIGNED 1
+# else
+# define TMW_CHAR_SIGNED 0
+# endif
+#endif
+
+/* It is common for one or more of the integer types
+ * to be the same size. For example, on many embedded
+ * processors, both shorts and ints are 16 bits. On
+ * processors used for workstations, it is quite common
+ * for both int and long to be 32 bits.
+ * When there is more than one choice for typdef'ing
+ * a portable type like powerwindow_int16_T or powerwindow_uint32_T, in
+ * concept, it should not matter which choice is made.
+ * However, some style guides and some code checking
+ * tools do identify and complain about seemingly
+ * irrelevant differences. For example, a code
+ * checking tool may complain about an implicit
+ * conversion from int to short even though both
+ * are 16 bits. To reduce these types of
+ * complaints, it is best to make int the
+ * preferred choice when more than one is available.
+ */
+
+#ifndef INT8_T
+# if TMW_BITS_PER_INT == 8
+# define INT8_T int
+# elif TMW_BITS_PER_LONG == 8
+# define INT8_T long
+# elif TMW_BITS_PER_SCHAR == 8
+# define INT8_T signed char
+# elif TMW_BITS_PER_SHRT == 8
+# define INT8_T short
+# endif
+#endif
+#ifdef INT8_T
+typedef INT8_T powerwindow_int8_T;
+#endif
+
+
+
+#ifndef UINT8_T
+# if TMW_BITS_PER_INT == 8
+# define UINT8_T unsigned int
+# elif TMW_BITS_PER_LONG == 8
+# define UINT8_T unsigned long
+# elif TMW_BITS_PER_SCHAR == 8
+# define UINT8_T unsigned char
+# elif TMW_BITS_PER_SHRT == 8
+# define UINT8_T unsigned short
+# endif
+#endif
+#ifdef UINT8_T
+typedef UINT8_T powerwindow_uint8_T;
+#endif
+
+
+#ifndef INT16_T
+# if TMW_BITS_PER_INT == 16
+# define INT16_T int
+# elif TMW_BITS_PER_LONG == 16
+# define INT16_T long
+# elif TMW_BITS_PER_SCHAR == 16
+# define INT16_T signed char
+# elif TMW_BITS_PER_SHRT == 16
+# define INT16_T short
+# endif
+#endif
+#ifdef INT16_T
+typedef INT16_T powerwindow_int16_T;
+#endif
+
+
+#ifndef UINT16_T
+# if TMW_BITS_PER_INT == 16
+# define UINT16_T unsigned int
+# elif TMW_BITS_PER_LONG == 16
+# define UINT16_T unsigned long
+# elif TMW_BITS_PER_SCHAR == 16
+# define UINT16_T unsigned char
+# elif TMW_BITS_PER_SHRT == 16
+# define UINT16_T unsigned short
+# endif
+#endif
+#ifdef UINT16_T
+typedef UINT16_T powerwindow_uint16_T;
+#endif
+
+
+#ifndef INT32_T
+# if TMW_BITS_PER_INT == 32
+# define INT32_T int
+# elif TMW_BITS_PER_LONG == 32
+# define INT32_T long
+# elif TMW_BITS_PER_SCHAR == 32
+# define INT32_T signed char
+# elif TMW_BITS_PER_SHRT == 32
+# define INT32_T short
+# endif
+#endif
+#ifdef INT32_T
+typedef INT32_T powerwindow_int32_T;
+#endif
+
+
+#ifndef UINT32_T
+# if TMW_BITS_PER_INT == 32
+# define UINT32_T unsigned int
+# elif TMW_BITS_PER_LONG == 32
+# define UINT32_T unsigned long
+# elif TMW_BITS_PER_SCHAR == 32
+# define UINT32_T unsigned char
+# elif TMW_BITS_PER_SHRT == 32
+# define UINT32_T unsigned short
+# endif
+#endif
+#ifdef UINT32_T
+typedef UINT32_T powerwindow_uint32_T;
+#endif
+
+/* The following is used to emulate smaller integer types when only
+ * larger types are available. For example, compilers for TI C3x/C4x DSPs
+ * define char and short to be 32 bits, so 8 and 16 bits are not directly
+ * available. This target is commonly used with RTW rapid prototyping.
+ * Other DSPs define char to be 16 bits, so 8 bits is not directly
+ * available.
+ */
+#ifndef INT8_T
+# ifdef INT16_T
+# define INT8_T INT16_T
+typedef INT8_T powerwindow_int8_T;
+# else
+# ifdef INT32_T
+# define INT8_T INT32_T
+typedef INT8_T powerwindow_int8_T;
+# endif
+# endif
+#endif
+
+#ifndef UINT8_T
+# ifdef UINT16_T
+# define UINT8_T UINT16_T
+typedef UINT8_T powerwindow_uint8_T;
+# else
+# ifdef UINT32_T
+# define UINT8_T UINT32_T
+typedef UINT8_T powerwindow_uint8_T;
+# endif
+# endif
+#endif
+
+#ifndef INT16_T
+# ifdef INT32_T
+# define INT16_T INT32_T
+typedef INT16_T powerwindow_int16_T;
+# endif
+#endif
+
+#ifndef UINT16_T
+# ifdef UINT32_T
+# define UINT16_T UINT32_T
+typedef UINT16_T powerwindow_uint16_T;
+# endif
+#endif
+
+
+#ifndef NO_FLOATS
+
+#ifndef REAL32_T
+# ifndef __MWERKS__
+# if FLT_MANT_DIG >= 23
+# define REAL32_T float
+# endif
+# else
+# define REAL32_T float
+# endif
+#endif
+#ifdef REAL32_T
+typedef REAL32_T powerwindow_real32_T;
+#endif
+
+
+#ifndef REAL64_T
+# ifndef __MWERKS__
+# if DBL_MANT_DIG >= 52
+# define REAL64_T double
+# endif
+# else
+# define REAL64_T double
+# endif
+#endif
+#ifdef REAL64_T
+typedef REAL64_T powerwindow_real64_T;
+#endif
+
+#endif /* NO_FLOATS*/
+
+/*=======================================================================*
+ * Fixed width word size data types: *
+ * powerwindow_int64_T - signed 64 bit integers *
+ * powerwindow_uint64_T - unsigned 64 bit integers *
+ *=======================================================================*/
+
+
+
+#ifndef INT64_T
+# if defined(__APPLE__)
+# define INT64_T long long
+# define FMT64 "ll"
+# if defined(__LP64__) && !defined(INT_TYPE_64_IS_LONG)
+# define INT_TYPE_64_IS_LONG
+# endif
+# elif defined(__x86_64__) || defined(__LP64__)
+# define INT64_T long
+# define FMT64 "l"
+# if !defined(INT_TYPE_64_IS_LONG)
+# define INT_TYPE_64_IS_LONG
+# endif
+# elif defined(_MSC_VER) || (defined(__BORLANDC__) && __BORLANDC__ >= 0x530) \
+ || (defined(__WATCOMC__) && __WATCOMC__ >= 1100)
+# define INT64_T __int64
+# define FMT64 "I64"
+# elif defined(__GNUC__) || defined(TMW_ENABLE_INT64) \
+ || defined(__LCC64__)
+# define INT64_T long long
+# define FMT64 "ll"
+# endif
+#endif
+
+
+
+#if defined(INT64_T)
+# if defined(__GNUC__) && \
+ ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >=9)))
+__extension__
+# endif
+typedef INT64_T powerwindow_int64_T;
+#endif
+
+
+
+#ifndef UINT64_T
+# if defined(__APPLE__)
+# define UINT64_T unsigned long long
+# define FMT64 "ll"
+# if defined(__LP64__) && !defined(INT_TYPE_64_IS_LONG)
+# define INT_TYPE_64_IS_LONG
+# endif
+# elif defined(__x86_64__) || defined(__LP64__)
+# define UINT64_T unsigned long
+# define FMT64 "l"
+# if !defined(INT_TYPE_64_IS_LONG)
+# define INT_TYPE_64_IS_LONG
+# endif
+# elif defined(_MSC_VER) || (defined(__BORLANDC__) && __BORLANDC__ >= 0x530) \
+ || (defined(__WATCOMC__) && __WATCOMC__ >= 1100)
+# define UINT64_T unsigned __int64
+# define FMT64 "I64"
+# elif defined(__GNUC__) || defined(TMW_ENABLE_INT64) \
+ || defined(__LCC64__)
+# define UINT64_T unsigned long long
+# define FMT64 "ll"
+# endif
+#endif
+
+#if defined(_WIN64) || (defined(__APPLE__) && defined(__LP64__)) \
+ || defined(__x86_64__) \
+ || defined(__LP64__)
+# define INT_TYPE_64_IS_SUPPORTED
+#endif
+
+#if defined(UINT64_T)
+# if defined(__GNUC__) && \
+ ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >=9)))
+__extension__
+# endif
+typedef UINT64_T powerwindow_uint64_T;
+#endif
+
+/*===========================================================================*
+ * Format string modifiers for using powerwindow_size_t variables in printf statements. *
+ *===========================================================================*/
+
+#ifndef FMT_SIZE_T
+# if defined( __GNUC__ ) || defined(_STDC_C99)
+# define FMT_SIZE_T "z"
+# elif defined (__WATCOMC__)
+# define FMT_SIZE_T "l"
+# elif defined (_WIN32 )
+# define FMT_SIZE_T "I"
+# else
+# define FMT_SIZE_T "l"
+# endif
+#endif
+
+#ifndef FMT_PTRDIFF_T
+# if defined(__APPLE__)
+# define FMT_PTRDIFF_T "l"
+# elif defined( __GNUC__ ) || defined(_STDC_C99)
+# define FMT_PTRDIFF_T "t"
+# elif defined (__WATCOMC__)
+# define FMT_PTRDIFF_T "l"
+# elif defined (_WIN32 )
+# define FMT_PTRDIFF_T "I"
+# else
+# define FMT_PTRDIFF_T "l"
+# endif
+#endif
+
+/*===========================================================================*
+ * General or logical data types where the word size is not guaranteed. *
+ * powerwindow_real_T - possible settings include powerwindow_real32_T or powerwindow_real64_T *
+ * powerwindow_time_T - possible settings include powerwindow_real64_T or powerwindow_uint32_T *
+ * powerwindow_boolean_T *
+ * powerwindow_char_T *
+ * powerwindow_int_T *
+ * powerwindow_uint_T *
+ * powerwindow_byte_T *
+ *===========================================================================*/
+
+#ifndef NO_FLOATS
+
+#ifndef REAL_T
+# ifdef REAL64_T
+# define REAL_T powerwindow_real64_T
+# else
+# ifdef REAL32_T
+# define REAL_T powerwindow_real32_T
+# endif
+# endif
+#endif
+#ifdef REAL_T
+typedef REAL_T powerwindow_real_T;
+#endif
+
+#ifndef TIME_T
+# ifdef REAL_T
+# define TIME_T powerwindow_real_T
+# endif
+#endif
+#ifdef TIME_T
+typedef TIME_T powerwindow_time_T;
+#endif
+
+#endif /* NO_FLOATS */
+
+#ifndef BOOLEAN_T
+# if defined(UINT8_T)
+# define BOOLEAN_T UINT8_T
+# else
+# define BOOLEAN_T unsigned int
+# endif
+#endif
+typedef BOOLEAN_T powerwindow_boolean_T;
+
+
+#ifndef CHARACTER_T
+# define CHARACTER_T char
+#endif
+typedef CHARACTER_T powerwindow_char_T;
+
+
+#ifndef INTEGER_T
+# define INTEGER_T int
+#endif
+typedef INTEGER_T powerwindow_int_T;
+
+
+#ifndef UINTEGER_T
+# define UINTEGER_T unsigned
+#endif
+typedef UINTEGER_T powerwindow_uint_T;
+
+
+#ifndef BYTE_T
+# define BYTE_T unsigned char
+#endif
+typedef BYTE_T powerwindow_byte_T;
+
+
+/*===========================================================================*
+ * Define Complex Structures *
+ *===========================================================================*/
+#ifndef NO_FLOATS
+
+#ifndef CREAL32_T
+# ifdef REAL32_T
+typedef struct {
+ powerwindow_real32_T re, im;
+} powerwindow_creal32_T;
+# define CREAL32_T creal32_T
+# endif
+#endif
+
+#ifndef CREAL64_T
+# ifdef REAL64_T
+typedef struct {
+ powerwindow_real64_T re, im;
+} powerwindow_creal64_T;
+# define CREAL64_T powerwindow_creal64_T
+# endif
+#endif
+
+#ifndef CREAL_T
+# ifdef REAL_T
+typedef struct {
+ powerwindow_real_T re, im;
+} powerwindow_creal_T;
+# define CREAL_T powerwindow_creal_T
+# endif
+#endif
+
+#endif /* NO_FLOATS */
+
+#ifndef CINT8_T
+# ifdef INT8_T
+typedef struct {
+ powerwindow_int8_T re, im;
+} powerwindow_cint8_T;
+# define CINT8_T powerwindow_cint8_T
+# endif
+#endif
+
+#ifndef CUINT8_T
+# ifdef UINT8_T
+typedef struct {
+ powerwindow_uint8_T re, im;
+} powerwindow_cuint8_T;
+# define CUINT8_T powerwindow_cuint8_T
+# endif
+#endif
+
+#ifndef CINT16_T
+# ifdef INT16_T
+typedef struct {
+ powerwindow_int16_T re, im;
+} powerwindow_cint16_T;
+# define CINT16_T powerwindow_cint16_T
+# endif
+#endif
+
+#ifndef CUINT16_T
+# ifdef UINT16_T
+typedef struct {
+ powerwindow_uint16_T re, im;
+} powerwindow_cuint16_T;
+# define CUINT16_T powerwindow_cuint16_T
+# endif
+#endif
+
+#ifndef CINT32_T
+# ifdef INT32_T
+typedef struct {
+ powerwindow_int32_T re, im;
+} powerwindow_cint32_T;
+# define CINT32_T powerwindow_cint32_T
+# endif
+#endif
+
+#ifndef CUINT32_T
+# ifdef UINT32_T
+typedef struct {
+ powerwindow_uint32_T re, im;
+} powerwindow_cuint32_T;
+# define CUINT32_T powerwindow_cuint32_T
+# endif
+#endif
+
+#ifndef CINT64_T
+# ifdef INT64_T
+typedef struct {
+ powerwindow_int64_T re, im;
+} powerwindow_cint64_T;
+# define CINT64_T powerwindow_cint64_T
+# endif
+#endif
+
+#ifndef CUINT64_T
+# ifdef UINT64_T
+typedef struct {
+ powerwindow_uint64_T re, im;
+} powerwindow_cuint64_T;
+# define CUINT64_T powerwindow_cuint64_T
+# endif
+#endif
+
+/*=======================================================================*
+ * Min and Max: *
+ * powerwindow_int8_T, powerwindow_int16_T, powerwindow_int32_T - signed 8, 16, or 32 bit integers *
+ * powerwindow_uint8_T, powerwindow_uint16_T, powerwindow_uint32_T - unsigned 8, 16, or 32 bit integers *
+ *=======================================================================*/
+
+#define powerwindow_MAX_int8_T ((powerwindow_int8_T)(127)) /* 127 */
+#define powerwindow_MIN_int8_T ((powerwindow_int8_T)(-128)) /* -128 */
+#define powerwindow_MAX_uint8_T ((powerwindow_uint8_T)(255)) /* 255 */
+#define powerwindow_MIN_uint8_T ((powerwindow_uint8_T)(0))
+
+#define powerwindow_MAX_int16_T ((powerwindow_int16_T)(32767)) /* 32767 */
+#define powerwindow_MIN_int16_T ((powerwindow_int16_T)(-32768)) /* -32768 */
+#define powerwindow_MAX_uint16_T ((powerwindow_uint16_T)(65535)) /* 65535 */
+#define powerwindow_MIN_uint16_T ((powerwindow_uint16_T)(0))
+
+#define powerwindow_MAX_int32_T ((powerwindow_int32_T)(2147483647)) /* 2147483647 */
+#define powerwindow_MIN_int32_T ((powerwindow_int32_T)(-2147483647-1)) /* -2147483648 */
+#define powerwindow_MAX_uint32_T ((powerwindow_uint32_T)(0xFFFFFFFFU)) /* 4294967295 */
+#define powerwindow_MIN_uint32_T ((powerwindow_uint32_T)(0))
+
+#if defined(_MSC_VER) || (defined(__BORLANDC__) && __BORLANDC__ >= 0x530) \
+ || (defined(__WATCOMC__) && __WATCOMC__ >= 1100) \
+ || defined(__LCC64__)
+# ifdef INT64_T
+# define powerwindow_MAX_int64_T ((powerwindow_int64_T)(9223372036854775807LL))
+# define powerwindow_MIN_int64_T ((powerwindow_int64_T)(-9223372036854775807LL-1LL))
+# endif
+# ifdef UINT64_T
+# define powerwindow_MAX_uint64_T ((powerwindow_uint64_T)(0xFFFFFFFFFFFFFFFFULL))
+# define powerwindow_MIN_uint64_T ((powerwindow_uint64_T)(0))
+# endif
+#else
+# ifdef INT64_T
+# ifdef INT_TYPE_64_IS_LONG
+# define powerwindow_MAX_int64_T ((powerwindow_int64_T)(9223372036854775807L))
+# define powerwindow_MIN_int64_T ((powerwindow_int64_T)(-9223372036854775807L-1L))
+# else
+# define powerwindow_MAX_int64_T ((powerwindow_int64_T)(9223372036854775807LL))
+# define powerwindow_MIN_int64_T ((powerwindow_int64_T)(-9223372036854775807LL-1LL))
+# endif
+# endif
+# ifdef UINT64_T
+# ifdef INT_TYPE_64_IS_LONG
+# define powerwindow_MAX_uint64_T ((powerwindow_uint64_T)(0xFFFFFFFFFFFFFFFFUL))
+# define powerwindow_MIN_uint64_T ((powerwindow_uint64_T)(0))
+# else
+# define powerwindow_MAX_uint64_T ((powerwindow_uint64_T)(0xFFFFFFFFFFFFFFFFULL))
+# define powerwindow_MIN_uint64_T ((powerwindow_uint64_T)(0))
+# endif
+# endif
+#endif
+
+#ifdef _MSC_VER
+/* Conversion from unsigned __int64 to double is not implemented in windows
+ * and results in a compile error, thus the value must first be cast to
+ * signed __int64, and then to double.
+ *
+ * If the 64 bit int value is greater than 2^63-1, which is the signed int64 max,
+ * the macro below provides a workaround for casting a uint64 value to a double
+ * in windows.
+ */
+# define uint64_to_double(u) ( ((u) > _I64_MAX) ? \
+ (double)(__int64)((u) - _I64_MAX - 1) + (double)_I64_MAX + 1: \
+ (double)(__int64)(u) )
+
+/* The following inline function should only be used in the macro double_to_uint64,
+ * as it only handles the specfic range of double between 2^63 and 2^64-1 */
+__forceinline
+powerwindow_uint64_T double_to_uint64_helper(double d) {
+ union double_to_uint64_union_type {
+ double dd;
+ powerwindow_uint64_T i64;
+ } di;
+ di.dd = d;
+ return (((di.i64 & 0x000fffffffffffff) | 0x0010000000000000) << 11);
+}
+
+/* The largest double value that can be cast to uint64 in windows is the
+ * signed int64 max, which is 2^63-1. The macro below provides
+ * a workaround for casting large double values to uint64 in windows.
+ */
+/* The magic number 18446744073709551616.0 is 2^64 */
+/* The magic number 9223372036854775808.0 is 2^63 */
+# define double_to_uint64(d) ( ((d) >= 18446744073709551616.0) ? \
+ 0xffffffffffffffffULL : \
+ ((d) < 0.0) ? 0ULL : \
+ ((d) >= 9223372036854775808.0) ? \
+ double_to_uint64_helper(d) : \
+ (unsigned __int64)(d) )
+#else
+# define uint64_to_double(u) ((double)(u))
+# if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__TICCSC__)
+/* double_to_uint64 defined only for MSVC and UNIX */
+# else
+# define double_to_uint64(d) ( ((d) > 0xffffffffffffffffULL) ? \
+ (unsigned long long) 0xffffffffffffffffULL : \
+ ((d) < 0) ? (unsigned long long) 0 : (unsigned long long)(d) )
+# endif
+#endif
+
+#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
+
+#ifndef _bool_T
+#define _bool_T
+
+typedef powerwindow_boolean_T bool;
+
+#ifndef false
+#define false (0)
+#endif
+#ifndef true
+#define true (1)
+#endif
+
+#endif /* _bool_T */
+
+#endif /* !__cplusplus */
+
+/*
+ * This software assumes that the code is being compiled on a target using a
+ * 2's complement representation for signed integer values.
+ */
+#if ((SCHAR_MIN + 1) != -SCHAR_MAX)
+#error "This code must be compiled using a 2's complement representation for signed integer values"
+#endif
+
+/*
+ * Maximum length of a MATLAB identifier (function/variable/model)
+ * including the null-termination character.
+ */
+#define TMW_NAME_LENGTH_MAX 64
+
+
+#ifdef MX_COMPAT_32
+typedef int powerwindow_mwSize;
+typedef int powerwindow_mwIndex;
+typedef int powerwindow_mwSignedIndex;
+#else
+typedef size_t powerwindow_mwSize; /* unsigned pointer-width integer */
+typedef size_t powerwindow_mwIndex; /* unsigned pointer-width integer */
+typedef ptrdiff_t powerwindow_mwSignedIndex; /* a signed pointer-width integer */
+#endif
+
+#if (defined(_LP64) || defined(_WIN64)) && !defined(MX_COMPAT_32)
+/* Currently 2^48 based on hardware limitations */
+# define powerwindow_MWSIZE_MAX 281474976710655UL
+# define powerwindow_MWINDEX_MAX 281474976710655UL
+# define powerwindow_MWSINDEX_MAX 281474976710655L
+# define powerwindow_MWSINDEX_MIN -281474976710655L
+#else
+# define powerwindow_MWSIZE_MAX 2147483647UL
+# define powerwindow_MWINDEX_MAX 2147483647UL
+# define powerwindow_MWSINDEX_MAX 2147483647L
+# define powerwindow_MWSINDEX_MIN -2147483647L
+#endif
+#define powerwindow_MWSIZE_MIN 0UL
+#define powerwindow_MWINDEX_MIN 0UL
+
+/** UTF-16 character type */
+
+#if (defined(__cplusplus) && (__cplusplus >= 201103L)) || (defined(_HAS_CHAR16_T_LANGUAGE_SUPPORT) && _HAS_CHAR16_T_LANGUAGE_SUPPORT)
+typedef char16_t powerwindow_CHAR16_T;
+#elif defined(_MSC_VER)
+typedef wchar_t powerwindow_CHAR16_T;
+#else
+typedef UINT16_T powerwindow_CHAR16_T;
+#endif
+
+#endif /* powerwindow__TMWTYPES__ */
+
+#endif /* powerwindow_tmwtypes_h */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_DRV.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_DRV.c
new file mode 100644
index 00000000..37398528
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_DRV.c
@@ -0,0 +1,291 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_DRV.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: PW_Control_DRV realizes the functionality of driver side powerwindow. It connects the 3 smaller modules together.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow//powerwindow_powerwindow_control.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_DRV.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_DRV_private.h"
+
+
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_PW_Control_DRV_initialize(void);
+void powerwindow_PW_Control_DRV_terminate(void);
+void powerwindow_PW_Control_DRV_main(void);
+
+
+/* Block states (auto storage) */
+powerwindow_D_Work_powerwindow_PW_Control_D powerwindow_PW_Control_DR_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+powerwindow_ExternalInputs_powerwindow_PW_C powerwindow_PW_Control_DRV_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+powerwindow_ExternalOutputs_powerwindow_PW_ powerwindow_PW_Control_DRV_Y;
+
+/* Real-time model */
+powerwindow_RT_MODEL_PW_Control_DRV powerwindow_PW_Control_DRV_M_;
+powerwindow_RT_MODEL_PW_Control_DRV *const powerwindow_PW_Control_DRV_M = &powerwindow_PW_Control_DRV_M_;
+
+
+
+/* Model step function */
+void powerwindow_PW_Control_DRV_main(void)
+{
+ /* local block i/o variables */
+ powerwindow_boolean_T rtb_Debounce_Up_DRV;
+ powerwindow_boolean_T rtb_Debounce_Down_DRV;
+
+ /* ModelReference: '<S2>/Debounce_Up_DRV' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In2, &rtb_Debounce_Up_DRV,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Down_DRV' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In4, &rtb_Debounce_Down_DRV,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Up_PSG_BackL' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In9,
+ &powerwindow_PW_Control_DRV_Y.Out10,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Down_PSG_BackL' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In10,
+ &powerwindow_PW_Control_DRV_Y.Out11,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Up_PSG_Front' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In5, &powerwindow_PW_Control_DRV_Y.Out6,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Down_PSG_Front' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In6, &powerwindow_PW_Control_DRV_Y.Out7,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Up_PSG_BackR' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In7, &powerwindow_PW_Control_DRV_Y.Out8,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/Debounce_Down_PSG_BackR' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_DRV_U.In8, &powerwindow_PW_Control_DRV_Y.Out9,
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtzce));
+
+ /* ModelReference: '<S1>/PW_DRV' */
+ powerwindow_powerwindow_control_main(&rtb_Debounce_Up_DRV, &rtb_Debounce_Down_DRV,
+ &powerwindow_PW_Control_DRV_U.In1,
+ &powerwindow_PW_Control_DRV_U.In3,
+ &powerwindow_PW_Control_DRV_Y.Out1,
+ &powerwindow_PW_Control_DRV_Y.Out2,
+ &powerwindow_PW_Control_DRV_Y.Out3,
+ &powerwindow_PW_Control_DRV_Y.Out4,
+ &powerwindow_PW_Control_DRV_Y.Out5,
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtzce));
+}
+
+
+/* Model initialize function */
+void powerwindow_PW_Control_DRV_initialize(void)
+{
+ /* Registration code */
+
+ /* states (dwork) */
+ (void) memset((void *)&powerwindow_PW_Control_DR_DWork, 0,
+ sizeof(powerwindow_D_Work_powerwindow_PW_Control_D));
+
+ /* external inputs */
+ (void) memset((void *)&powerwindow_PW_Control_DRV_U, 0,
+ sizeof(powerwindow_ExternalInputs_powerwindow_PW_C));
+
+ /* external outputs */
+ (void) memset((void *)&powerwindow_PW_Control_DRV_Y, 0,
+ sizeof(powerwindow_ExternalOutputs_powerwindow_PW_));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Down_DRV' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Down_PSG_BackL' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Down_PSG_BackR' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Down_PSG_Front' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Up_DRV' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Up_PSG_BackL' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Up_PSG_BackR' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/Debounce_Up_PSG_Front' */
+ powerwindow_debounce_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/PW_DRV' */
+ powerwindow_powerwindow_control_initialize(powerwindow_PW_DRV_rtmGetErrorStatusPointer(powerwindow_PW_Control_DRV_M),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtm),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtdw),
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtzce));
+
+ /* Start for ModelReference: '<S2>/Debounce_Up_DRV' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Down_DRV' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Up_PSG_BackL' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Down_PSG_BackL' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Up_PSG_Front' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Down_PSG_Front' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Up_PSG_BackR' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/Debounce_Down_PSG_BackR' */
+ powerwindow_debounce_Start
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S1>/PW_DRV' */
+ powerwindow_powerwindow_control_Start(&(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Up_DRV' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_DRV_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Down_DRV' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_DRV_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Up_PSG_BackL' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackL_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Down_PSG_BackL' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackL_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Up_PSG_Front' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_Front_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Down_PSG_Front' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_Front_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Up_PSG_BackR' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Up_PSG_BackR_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S2>/Debounce_Down_PSG_BackR' */
+ powerwindow_debounce_Init
+ (&(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_DR_DWork.Debounce_Down_PSG_BackR_DWORK1.rtdw));
+
+ /* SystemInitialize for ModelReference: '<S1>/PW_DRV' */
+ powerwindow_powerwindow_control_Init(&powerwindow_PW_Control_DRV_Y.Out1,
+ &powerwindow_PW_Control_DRV_Y.Out2, &powerwindow_PW_Control_DRV_Y.Out3,
+ &powerwindow_PW_Control_DRV_Y.Out4, &powerwindow_PW_Control_DRV_Y.Out5,
+ &(powerwindow_PW_Control_DR_DWork.PW_DRV_DWORK1.rtdw));
+}
+
+/* Model terminate function */
+void powerwindow_PW_Control_DRV_terminate(void)
+{
+ /* (no terminate code required) */
+}
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackL.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackL.c
new file mode 100644
index 00000000..e2f9be2f
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackL.c
@@ -0,0 +1,163 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackL.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: PW_Control_DRV realizes the functionality of back-left passenger side powerwindow. It connects the 3 smaller modules together.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow//powerwindow_powerwindow_control.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackL_private.h"
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_PW_Control_PSG_BackL_initialize(void);
+void powerwindow_PW_Control_PSG_BackL_terminate(void);
+void powerwindow_PW_Control_PSG_BackL_main(void);
+
+
+/* Block states (auto storage) */
+powerwindow_D_Work_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+powerwindow_ExternalInputs_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+powerwindow_ExternalOutputs_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_Y;
+
+/* Real-time model */
+powerwindow_RT_MODEL_PW_Control_PSG_BackL powerwindow_PW_Control_PSG_BackL_M_;
+powerwindow_RT_MODEL_PW_Control_PSG_BackL *const powerwindow_PW_Control_PSG_BackL_M = &powerwindow_PW_Control_PSG_BackL_M_;
+
+/* Model step function */
+void powerwindow_PW_Control_PSG_BackL_main(void)
+{
+ /* local block i/o variables */
+ powerwindow_boolean_T rtb_debounce_Up;
+ powerwindow_boolean_T rtb_debounce_Down;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_BackL_o1;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_BackL_o2;
+
+ /* ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_BackL_U.Up_PSG_BackL, &rtb_debounce_Up,
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_BackL_U.Down_PSG_BackL, &rtb_debounce_Down,
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* ModelReference: '<S1>/ControlEx_PSG_BackL' */
+ powerwindow_controlexclusion_main(&powerwindow_PW_Control_PSG_BackL_U.Up_DRV, &powerwindow_PW_Control_PSG_BackL_U.Down_DRV,
+ &rtb_debounce_Up, &rtb_debounce_Down,
+ &powerwindow_rtb_ControlEx_PSG_BackL_o1, &powerwindow_rtb_ControlEx_PSG_BackL_o2);
+
+ /* ModelReference: '<S1>/PW_PSG_BackL' */
+ powerwindow_powerwindow_control_main(&powerwindow_rtb_ControlEx_PSG_BackL_o1, &powerwindow_rtb_ControlEx_PSG_BackL_o2,
+ &powerwindow_PW_Control_PSG_BackL_U.endofdetectionrange,
+ &powerwindow_PW_Control_PSG_BackL_U.currentsense,
+ &powerwindow_PW_Control_PSG_BackL_Y.window_up, &powerwindow_PW_Control_PSG_BackL_Y.window_down,
+ &powerwindow_PW_Control_PSG_BackL_Y.overcurrent, &powerwindow_PW_Control_PSG_BackL_Y.pinch,
+ &powerwindow_PW_Control_PSG_BackL_Y.wake,
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtzce));
+}
+
+/* Model initialize function */
+void powerwindow_PW_Control_PSG_BackL_initialize(void)
+{
+ /* Registration code */
+
+ /* initialize error status */
+ powerwindow_PW_PSG_BackL_rtmSetErrorStatus(powerwindow_PW_Control_PSG_BackL_M, (NULL));
+
+ /* states (dwork) */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackL_DWork, 0,
+ sizeof(powerwindow_D_Work_PW_Control_PSG_BackL));
+
+ /* external inputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackL_U, 0,
+ sizeof(powerwindow_ExternalInputs_PW_Control_PSG_BackL));
+
+ /* external outputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackL_Y, 0,
+ sizeof(powerwindow_ExternalOutputs_PW_Control_PSG_BackL));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/ControlEx_PSG_BackL' */
+ powerwindow_controlexclusion_initialize();
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Down' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_BackL_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackL_M),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Up' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_BackL_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackL_M),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/PW_PSG_BackL' */
+ powerwindow_powerwindow_control_initialize(powerwindow_PW_PSG_BackL_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackL_M),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtzce));
+
+ /* Start for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S1>/PW_PSG_BackL' */
+ powerwindow_powerwindow_control_Start(&(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackL_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S1>/PW_PSG_BackL' */
+ powerwindow_powerwindow_control_Init(&powerwindow_PW_Control_PSG_BackL_Y.window_up,
+ &powerwindow_PW_Control_PSG_BackL_Y.window_down, &powerwindow_PW_Control_PSG_BackL_Y.overcurrent,
+ &powerwindow_PW_Control_PSG_BackL_Y.pinch, &powerwindow_PW_Control_PSG_BackL_Y.wake,
+ &(powerwindow_PW_Control_PSG_BackL_DWork.PW_PSG_BackL_DWORK1.rtdw));
+}
+
+/* Model terminate function */
+void powerwindow_PW_Control_PSG_BackL_terminate(void)
+{
+ /* (no terminate code required) */
+}
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackR.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackR.c
new file mode 100644
index 00000000..64adede6
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_BackR.c
@@ -0,0 +1,163 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_BackR.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: PW_Control_DRV realizes the functionality of back-right passenger side powerwindow. It connects the 3 smaller modules together.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow//powerwindow_PW_Control_PSG_BackR.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_BackR_private.h"
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_PW_Control_PSG_BackR_initialize(void);
+void powerwindow_PW_Control_PSG_BackR_terminate(void);
+void powerwindow_PW_Control_PSG_BackR_main(void);
+
+
+/* Block states (auto storage) */
+powerwindow_D_Work_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+powerwindow_ExternalInputs_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+powerwindow_ExternalOutputs_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_Y;
+
+/* Real-time model */
+powerwindow_RT_MODEL_PW_Control_PSG_BackR powerwindow_PW_Control_PSG_BackR_M_;
+powerwindow_RT_MODEL_PW_Control_PSG_BackR *const powerwindow_PW_Control_PSG_BackR_M = &powerwindow_PW_Control_PSG_BackR_M_;
+
+/* Model step function */
+void powerwindow_PW_Control_PSG_BackR_main(void)
+{
+ /* local block i/o variables */
+ powerwindow_boolean_T rtb_debounce_Up;
+ powerwindow_boolean_T rtb_debounce_Down;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_BackR_o1;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_BackR_o2;
+
+ /* ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_BackR_U.Up_PSG_BackR, &rtb_debounce_Up,
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_BackR_U.Down_PSG_BackR, &rtb_debounce_Down,
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* ModelReference: '<S1>/ControlEx_PSG_BackR' */
+ powerwindow_controlexclusion_main(&powerwindow_PW_Control_PSG_BackR_U.Up_DRV, &powerwindow_PW_Control_PSG_BackR_U.Down_DRV,
+ &rtb_debounce_Up, &rtb_debounce_Down,
+ &powerwindow_rtb_ControlEx_PSG_BackR_o1, &powerwindow_rtb_ControlEx_PSG_BackR_o2);
+
+ /* ModelReference: '<S1>/PW_PSG_BackR' */
+ powerwindow_powerwindow_control_main(&powerwindow_rtb_ControlEx_PSG_BackR_o1, &powerwindow_rtb_ControlEx_PSG_BackR_o2,
+ &powerwindow_PW_Control_PSG_BackR_U.endofdetectionrange,
+ &powerwindow_PW_Control_PSG_BackR_U.currentsense,
+ &powerwindow_PW_Control_PSG_BackR_Y.window_up, &powerwindow_PW_Control_PSG_BackR_Y.window_down,
+ &powerwindow_PW_Control_PSG_BackR_Y.overcurrent, &powerwindow_PW_Control_PSG_BackR_Y.pinch,
+ &powerwindow_PW_Control_PSG_BackR_Y.wake,
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtzce));
+}
+
+/* Model initialize function */
+void powerwindow_PW_Control_PSG_BackR_initialize(void)
+{
+ /* Registration code */
+
+ /* initialize error status */
+ powerwindow_PW_PSG_BackR_rtmSetErrorStatus(powerwindow_PW_Control_PSG_BackR_M, (NULL));
+
+ /* states (dwork) */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackR_DWork, 0,
+ sizeof(powerwindow_D_Work_PW_Control_PSG_BackR));
+
+ /* external inputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackR_U, 0,
+ sizeof(powerwindow_ExternalInputs_PW_Control_PSG_BackR));
+
+ /* external outputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_BackR_Y, 0,
+ sizeof(powerwindow_ExternalOutputs_PW_Control_PSG_BackR));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/ControlEx_PSG_BackR' */
+ powerwindow_controlexclusion_initialize();
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Down' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_BackR_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackR_M),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Up' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_BackR_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackR_M),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/PW_PSG_BackR' */
+ powerwindow_powerwindow_control_initialize(powerwindow_PW_PSG_BackR_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_BackR_M),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtzce));
+
+ /* Start for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S1>/PW_PSG_BackR' */
+ powerwindow_powerwindow_control_Start(&(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_BackR_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S1>/PW_PSG_BackR' */
+ powerwindow_powerwindow_control_Init(&powerwindow_PW_Control_PSG_BackR_Y.window_up,
+ &powerwindow_PW_Control_PSG_BackR_Y.window_down, &powerwindow_PW_Control_PSG_BackR_Y.overcurrent,
+ &powerwindow_PW_Control_PSG_BackR_Y.pinch, &powerwindow_PW_Control_PSG_BackR_Y.wake,
+ &(powerwindow_PW_Control_PSG_BackR_DWork.PW_PSG_BackR_DWORK1.rtdw));
+}
+
+/* Model terminate function */
+void powerwindow_PW_Control_PSG_BackR_terminate(void)
+{
+ /* (no terminate code required) */
+}
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_Front.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_Front.c
new file mode 100644
index 00000000..517c0140
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_PW_Control_PSG_Front.c
@@ -0,0 +1,163 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_PW_Control_PSG_Front.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: PW_Control_DRV realizes the functionality of front passenger side powerwindow. It connects the 3 smaller modules together.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow//powerwindow_powerwindow_control.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front.h"
+#include "powerwindow_HeaderFiles/powerwindow_PW_Control_PSG_Front_private.h"
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_PW_Control_PSG_Front_initialize(void);
+void powerwindow_PW_Control_PSG_Front_terminate(void);
+void powerwindow_PW_Control_PSG_Front_main(void);
+
+
+/* Block states (auto storage) */
+powerwindow_D_Work_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_DWork;
+
+/* External inputs (root inport signals with auto storage) */
+powerwindow_ExternalInputs_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_U;
+
+/* External outputs (root outports fed by signals with auto storage) */
+powerwindow_ExternalOutputs_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_Y;
+
+/* Real-time model */
+powerwindow_RT_MODEL_PW_Control_PSG_Front powerwindow_PW_Control_PSG_Front_M_;
+powerwindow_RT_MODEL_PW_Control_PSG_Front *const powerwindow_PW_Control_PSG_Front_M = &powerwindow_PW_Control_PSG_Front_M_;
+
+/* Model step function */
+void powerwindow_PW_Control_PSG_Front_main(void)
+{
+ /* local block i/o variables */
+ powerwindow_boolean_T rtb_debounce_Up;
+ powerwindow_boolean_T rtb_debounce_Down;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_Front_Front_o1;
+ powerwindow_boolean_T powerwindow_rtb_ControlEx_PSG_Front_Front_o2;
+
+ /* ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_Front_U.Up_PSG_Front, &rtb_debounce_Up,
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_main(&powerwindow_PW_Control_PSG_Front_U.Down_PSG_Front, &rtb_debounce_Down,
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* ModelReference: '<S1>/ControlEx_PSG_Front_Front' */
+ powerwindow_controlexclusion_main(&powerwindow_PW_Control_PSG_Front_U.Up_DRV, &powerwindow_PW_Control_PSG_Front_U.Down_DRV,
+ &rtb_debounce_Up, &rtb_debounce_Down,
+ &powerwindow_rtb_ControlEx_PSG_Front_Front_o1, &powerwindow_rtb_ControlEx_PSG_Front_Front_o2);
+
+ /* ModelReference: '<S1>/PW_PSG_Front_Front' */
+ powerwindow_powerwindow_control_main(&powerwindow_rtb_ControlEx_PSG_Front_Front_o1, &powerwindow_rtb_ControlEx_PSG_Front_Front_o2,
+ &powerwindow_PW_Control_PSG_Front_U.endofdetectionrange,
+ &powerwindow_PW_Control_PSG_Front_U.currentsense,
+ &powerwindow_PW_Control_PSG_Front_Y.window_up, &powerwindow_PW_Control_PSG_Front_Y.window_down,
+ &powerwindow_PW_Control_PSG_Front_Y.overcurrent, &powerwindow_PW_Control_PSG_Front_Y.pinch,
+ &powerwindow_PW_Control_PSG_Front_Y.wake,
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtzce));
+}
+
+/* Model initialize function */
+void powerwindow_PW_Control_PSG_Front_initialize(void)
+{
+ /* Registration code */
+
+ /* initialize error status */
+ powerwindow_PW_PSG_Front_rtmSetErrorStatus(powerwindow_PW_Control_PSG_Front_M, (NULL));
+
+ /* states (dwork) */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_Front_DWork, 0,
+ sizeof(powerwindow_D_Work_PW_Control_PSG_Front));
+
+ /* external inputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_Front_U, 0,
+ sizeof(powerwindow_ExternalInputs_PW_Control_PSG_Front));
+
+ /* external outputs */
+ (void) memset((void *)&powerwindow_PW_Control_PSG_Front_Y, 0,
+ sizeof(powerwindow_ExternalOutputs_PW_Control_PSG_Front));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/ControlEx_PSG_Front_Front' */
+ powerwindow_controlexclusion_initialize();
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Down' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_Front_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_Front_M),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S2>/debounce_Up' */
+ powerwindow_debounce_initialize(powerwindow_PW_PSG_Front_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_Front_M),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtzce));
+
+ /* Model Initialize fcn for ModelReference Block: '<S1>/PW_PSG_Front_Front' */
+ powerwindow_powerwindow_control_initialize(powerwindow_PW_PSG_Front_rtmGetErrorStatusPointer(powerwindow_PW_Control_PSG_Front_M),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtm),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtdw),
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtzce));
+
+ /* Start for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Start(&(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* Start for ModelReference: '<S1>/PW_PSG_Front_Front' */
+ powerwindow_powerwindow_control_Start(&(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Up' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Up_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S2>/debounce_Down' */
+ powerwindow_debounce_Init(&(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtb),
+ &(powerwindow_PW_Control_PSG_Front_DWork.Debounce_Down_DWORK1.rtdw));
+
+ /* InitializeConditions for ModelReference: '<S1>/PW_PSG_Front_Front' */
+ powerwindow_powerwindow_control_Init(&powerwindow_PW_Control_PSG_Front_Y.window_up,
+ &powerwindow_PW_Control_PSG_Front_Y.window_down, &powerwindow_PW_Control_PSG_Front_Y.overcurrent,
+ &powerwindow_PW_Control_PSG_Front_Y.pinch, &powerwindow_PW_Control_PSG_Front_Y.wake,
+ &(powerwindow_PW_Control_PSG_Front_DWork.PW_PSG_Front_Front_DWORK1.rtdw));
+}
+
+/* Model terminate function */
+void powerwindow_PW_Control_PSG_Front_terminate(void)
+{
+ /* (no terminate code required) */
+}
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_const_params.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_const_params.c
new file mode 100644
index 00000000..74777528
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_const_params.c
@@ -0,0 +1,24 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_const_params.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: the lookup table for stateflow chart in powerwindow_powerwindow_control
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_const_params.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+#include "powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+
+extern const powerwindow_boolean_T powerwindow_rtCP_pooled_6bUUQf1tASYw[12];
+const powerwindow_boolean_T powerwindow_rtCP_pooled_6bUUQf1tASYw[12] = { 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,
+ 0 } ;
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_controlexclusion.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_controlexclusion.c
new file mode 100644
index 00000000..e199f0e4
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_controlexclusion.c
@@ -0,0 +1,85 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_controlexclusion
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: powerwindow_controlexclusion is one functionality of the power window benchmark.
+ It takes the input signal from the driver and the passenger to determine the final control signal.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_controlexclusion.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+
+
+#include "powerwindow_HeaderFiles/powerwindow_controlexclusion.h"
+#include "powerwindow_HeaderFiles/powerwindow_controlexclusion_private.h"
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_controlexclusion_initialize(void);
+void powerwindow_controlexclusion_terminate(void);
+void powerwindow_controlexclusion_main(const powerwindow_boolean_T *rtu_Up_DRV, const powerwindow_boolean_T *rtu_Down_DRV,
+ const powerwindow_boolean_T *rtu_Up_PSG, const powerwindow_boolean_T *rtu_Down_PSG,
+ powerwindow_boolean_T *rty_Up, powerwindow_boolean_T *rty_Down);
+
+/* Model initialize function */
+void powerwindow_controlexclusion_initialize(void)
+{
+ /* (no initialization code required) */
+}
+
+/* Model terminate function */
+void powerwindow_controlexclusion_terminate(void)
+{
+ /* (no terminate code required) */
+}
+
+
+/*
+ Algorithm core functions
+ */
+
+/* Output and update for referenced model: 'ControlExclusion' */
+void powerwindow_controlexclusion_main(const powerwindow_boolean_T *rtu_Up_DRV, const powerwindow_boolean_T *rtu_Down_DRV,
+ const powerwindow_boolean_T *rtu_Up_PSG, const powerwindow_boolean_T *rtu_Down_PSG,
+ powerwindow_boolean_T *rty_Up, powerwindow_boolean_T *rty_Down)
+{
+ /* Logic: '<S2>/Logical Operator11' incorporates:
+ * Logic: '<S2>/Logical Operator2'
+ * Logic: '<S2>/Logical Operator3'
+ * Logic: '<S2>/Logical Operator5'
+ * Logic: '<S2>/Logical Operator6'
+ * Logic: '<S2>/Logical Operator7'
+ */
+ *rty_Up = !(((!*rtu_Up_DRV) && (*rtu_Down_DRV)) || ((*rtu_Down_DRV) &&
+ (!*rtu_Up_PSG) && (*rtu_Down_PSG)));
+
+ /* Logic: '<S2>/Logical Operator12' incorporates:
+ * Logic: '<S2>/Logical Operator1'
+ * Logic: '<S2>/Logical Operator10'
+ * Logic: '<S2>/Logical Operator4'
+ * Logic: '<S2>/Logical Operator8'
+ * Logic: '<S2>/Logical Operator9'
+ */
+ *rty_Down = !(((*rtu_Up_DRV) && (!*rtu_Down_DRV)) || ((*rtu_Up_DRV) &&
+ (*rtu_Up_PSG) && (!*rtu_Down_PSG)));
+}
+
+
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_debounce.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_debounce.c
new file mode 100644
index 00000000..df0b91d2
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_debounce.c
@@ -0,0 +1,210 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_debounce.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: powerwindow_debounce_main is used to powerwindow_debounce_main the push-down button of the power window.
+ In order to input a manual switch signal into a digital circuit,
+ debouncing is necessary so that a single press does not appear like multiple presses.
+ Without debouncing, pressing the button once may cause unpredictable results.
+ powerwindow_debounce_main.c defines all the functions that will be used in debounce_PSG_Front which is a part of the Power window.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow/powerwindow_debounce.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+ */
+
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_debounce.h"
+#include "powerwindow_HeaderFiles/powerwindow_debounce_private.h"
+
+/* Named constants for Chart: '<Root>/Chart' */
+#define debounce_IN_debounce ((powerwindow_uint8_T)1U)
+#define debounce_IN_NO_ACTIVE_CHILD ((powerwindow_uint8_T)0U)
+#define debounce_IN_Off ((powerwindow_uint8_T)2U)
+#define debounce_IN_Off_h ((powerwindow_uint8_T)1U)
+#define debounce_IN_On ((powerwindow_uint8_T)3U)
+#define debounce_IN_On_b ((powerwindow_uint8_T)2U)
+
+/*
+ Forward declaration of functions
+*/
+
+void powerwindow_debounce_Init(powerwindow_rtB_debounce_T *, powerwindow_rtDW_debounce_T *);
+void powerwindow_debounce_Start(powerwindow_rtDW_debounce_T *);
+void powerwindow_debounce_initialize(const powerwindow_char_T **, powerwindow_RT_MODEL_debounce_T * const,
+ powerwindow_rtB_debounce_T *, powerwindow_rtDW_debounce_T *, powerwindow_rtZCE_debounce_T *);
+void powerwindow_debounce_main(const powerwindow_boolean_T *, powerwindow_boolean_T *, powerwindow_rtB_debounce_T *,
+ powerwindow_rtDW_debounce_T *, powerwindow_rtZCE_debounce_T *);
+
+/*
+ Initialization- and return-value-related functions
+ */
+
+/* Initial conditions for referenced model: 'powerwindow_debounce_main' */
+void powerwindow_debounce_Init(powerwindow_rtB_debounce_T *localB, powerwindow_rtDW_debounce_T *localDW)
+{
+ /* InitializeConditions for Chart: '<Root>/Chart' */
+ localDW->is_debounce = debounce_IN_NO_ACTIVE_CHILD;
+ localDW->temporalCounter_i1 = 0U;
+ localDW->is_active_c3_debounce = 0U;
+ localDW->is_c3_debounce = debounce_IN_NO_ACTIVE_CHILD;
+ localB->Q = false;
+}
+
+/* Start for referenced model: 'powerwindow_debounce_main' */
+void powerwindow_debounce_Start(powerwindow_rtDW_debounce_T *localDW)
+{
+ /* Start for DiscretePulseGenerator: '<Root>/period of 10ms' */
+ localDW->clockTickCounter = 0L;
+}
+
+/* Model initialize function */
+void powerwindow_debounce_initialize(const powerwindow_char_T **rt_errorStatus, powerwindow_RT_MODEL_debounce_T *
+ const debounce_M, powerwindow_rtB_debounce_T *localB, powerwindow_rtDW_debounce_T *localDW,
+ powerwindow_rtZCE_debounce_T *localZCE)
+{
+ /* Registration code */
+
+ /* initialize error status */
+ powerwindow_rtmSetErrorStatusPointer(debounce_M, rt_errorStatus);
+
+ /* block I/O */
+ (void) memset(((void *) localB), 0,
+ sizeof(powerwindow_rtB_debounce_T));
+
+ /* states (dwork) */
+ (void) memset((void *)localDW, 0,
+ sizeof(powerwindow_rtDW_debounce_T));
+ localZCE->Chart_Trig_ZCE = powerwindow_POS_ZCSIG;
+}
+
+/*
+ Algorithm core functions
+ */
+
+/* Output and update for referenced model: 'powerwindow_debounce_main' */
+void powerwindow_debounce_main(const powerwindow_boolean_T *rtu_Switch, powerwindow_boolean_T *rty_debounced_Switch,
+ powerwindow_rtB_debounce_T *localB, powerwindow_rtDW_debounce_T *localDW, powerwindow_rtZCE_debounce_T
+ *localZCE)
+{
+ powerwindow_int16_T rtb_periodof10ms;
+
+ /* DiscretePulseGenerator: '<Root>/period of 10ms' */
+ rtb_periodof10ms = (localDW->clockTickCounter < 1L) &&
+ (localDW->clockTickCounter >= 0L) ? 1 : 0;
+ if (localDW->clockTickCounter >= 1L) {
+ localDW->clockTickCounter = 0L;
+ } else {
+ localDW->clockTickCounter++;
+ }
+
+ /* End of DiscretePulseGenerator: '<Root>/period of 10ms' */
+
+ /* Chart: '<Root>/Chart' incorporates:
+ * TriggerPort: '<S1>/ticks'
+ */
+ /* DataTypeConversion: '<Root>/Data Type Conversion' */
+ if ((rtb_periodof10ms != 0) && (localZCE->Chart_Trig_ZCE != powerwindow_POS_ZCSIG)) {
+ /* Gateway: Chart */
+ if (localDW->temporalCounter_i1 < 7U) {
+ localDW->temporalCounter_i1++;
+ }
+
+ /* Event: '<S1>:13' */
+ /* During: Chart */
+ if (localDW->is_active_c3_debounce == 0U) {
+ /* Entry: Chart */
+ localDW->is_active_c3_debounce = 1U;
+
+ /* Entry Internal: Chart */
+ /* Transition: '<S1>:9' */
+ localDW->is_c3_debounce = debounce_IN_Off;
+
+ /* Entry 'Off': '<S1>:1' */
+ localB->Q = true;
+ } else {
+ switch (localDW->is_c3_debounce) {
+ case debounce_IN_debounce:
+ /* During 'powerwindow_debounce_main': '<S1>:6' */
+ if (localDW->is_debounce == debounce_IN_Off_h) {
+ /* During 'Off': '<S1>:8' */
+ if ((powerwindow_int16_T)*rtu_Switch < 1) {
+ /* Transition: '<S1>:12' */
+ localDW->is_debounce = debounce_IN_On_b;
+ localDW->temporalCounter_i1 = 0U;
+ } else {
+ if (localDW->temporalCounter_i1 >= 3) {
+ /* Transition: '<S1>:16' */
+ localDW->is_debounce = debounce_IN_NO_ACTIVE_CHILD;
+ localDW->is_c3_debounce = debounce_IN_Off;
+
+ /* Entry 'Off': '<S1>:1' */
+ localB->Q = true;
+ }
+ }
+ } else {
+ /* During 'On': '<S1>:7' */
+ if ((powerwindow_int16_T)*rtu_Switch > 0) {
+ /* Transition: '<S1>:11' */
+ localDW->is_debounce = debounce_IN_Off_h;
+ localDW->temporalCounter_i1 = 0U;
+ } else {
+ if (localDW->temporalCounter_i1 >= 3) {
+ /* Transition: '<S1>:14' */
+ localDW->is_debounce = debounce_IN_NO_ACTIVE_CHILD;
+ localDW->is_c3_debounce = debounce_IN_On;
+
+ /* Entry 'On': '<S1>:5' */
+ localB->Q = false;
+ }
+ }
+ }
+ break;
+
+ case debounce_IN_Off:
+ /* During 'Off': '<S1>:1' */
+ if ((powerwindow_int16_T)*rtu_Switch < 1) {
+ /* Transition: '<S1>:10' */
+ localDW->is_c3_debounce = debounce_IN_debounce;
+ localDW->is_debounce = debounce_IN_On_b;
+ localDW->temporalCounter_i1 = 0U;
+ }
+ break;
+
+ default:
+ /* During 'On': '<S1>:5' */
+ if ((powerwindow_int16_T)*rtu_Switch > 0) {
+ /* Transition: '<S1>:15' */
+ localDW->is_c3_debounce = debounce_IN_debounce;
+ localDW->is_debounce = debounce_IN_Off_h;
+ localDW->temporalCounter_i1 = 0U;
+ }
+ break;
+ }
+ }
+ }
+
+ localZCE->Chart_Trig_ZCE = (powerwindow_uint8_T)(rtb_periodof10ms != 0 ? (powerwindow_int16_T)
+ powerwindow_POS_ZCSIG : (powerwindow_int16_T)powerwindow_ZERO_ZCSIG);
+
+ /* End of DataTypeConversion: '<Root>/Data Type Conversion' */
+
+ /* DataTypeConversion: '<Root>/Data Type Conversion2' */
+ *rty_debounced_Switch = localB->Q;
+}
+
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_inputs.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_inputs.c
new file mode 100644
index 00000000..fa408c19
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_inputs.c
@@ -0,0 +1,46 @@
+ #include "powerwindow_HeaderFiles/powerwindow_rtwtypes.h"
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Up_Input_DRV_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1};
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_DRV_U_Down_Input_DRV_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1};
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Up_Input_Front_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_Front_U_Down_Input_Front_Array[powerwindow_input_length] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1};
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Up_Input_BackL_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_BackL_U_Down_Input_BackL_Array[powerwindow_input_length] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Up_Input_BackR_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_boolean_T powerwindow_debounce_Driver_BackR_U_Down_Input_BackR_Array[powerwindow_input_length] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_DRV_Array[powerwindow_input_length] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_DRV_Array[powerwindow_input_length] = {92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 94, 92, 92, 92, 92, 92, 92, 92, 92, 92, 20, 73, 20, 73, 92, 94, 29, 72, 88, 72, 22, 93, 89, 89, 72, 93, 70, 30, 17, 72, 92, 121, 69, 24, 8, 93, 84, 94, 14, 72, 68, 84, 92, 84, 8, 92, 85, 29, 85, 72, 92, 92, 29, 93, 29, 92, 92, 72, 92, 92, 92, 92, 92, 124, 124, 92, 92, 93, 105, 72, 69, 21, 30, 88, 120, 93, 116, 16, 94, 0, 84, 116, 94, 65, 64, 94, 86, 73, 74, 72, 21, 85, 116, 92, 0, 92, 116, 88, 80, 8, 92, 84, 117, 4, 8, 29, 76, 1, 85, 72, 92, 84, 124, 84, 64, 93, 29, 93, 81, 188, 124, 124, 124, 93, 0, 29, 28, 24, 69, 8, 92, 92, 92, 116, 116, 120, 116, 93, 94, 93, 94, 124, 122, 124, 100, 116, 116, 116, 116, 116, 124, 116, 93, 93, 93, 10, 101, 94, 102, 98, 97, 1, 97, 97, 97, 117, 94, 93, 94, 93, 94, 2, 2, 93, 8, 93, 92, 88, 80, 81, 80, 100, 80, 112, 112, 117, 96, 96, 80, 93, 92, 124, 89, 84, 112, 112, 117, 118, 16, 124, 94, 94, 94, 124, 93, 8, 94, 14, 72, 104, 28, 68, 0, 72, 188, 93, 120, 93, 88, 10, 28, 20, 20, 88, 120, 88, 176, 93, 120, 88, 93, 120, 29, 120, 93, 92, 93, 93, 117, 120, 120, 93, 120, 89, 114, 197, 189, 93, 121, 112, 124, 93, 93, 197, 197, 94, 189, 197, 5, 133, 157, 197, 112, 65, 121, 25, 186, 93, 120, 122, 94, 149, 200, 149, 149, 157, 150, 145, 156, 149, 148, 146, 150, 148, 150, 130, 150, 150, 150, 150, 146, 150, 150, 149, 149, 134, 149, 130, 129, 148, 149, 148, 150, 128, 197, 132, 148, 140, 132, 8, 20, 84, 88, 76, 64, 20, 20, 68, 68, 28, 28, 28, 29, 28, 28, 8, 28, 20, 28, 28, 28, 28, 28, 13, 13, 22, 13, 70, 12, 76, 24, 24, 24, 72, 24, 28, 13, 13, 24, 24, 24, 14, 14, 13, 14, 14, 78, 13, 14, 14, 14, 13, 13, 30, 5, 8, 4, 20, 20, 4, 4, 4, 68, 28, 4, 8, 4, 12, 4, 4, 4, 68, 68, 92, 4, 68, 4, 4, 4, 28, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 76, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 12, 12, 12, 12, 12, 12, 14, 12, 72, 14, 13, 12, 14, 14, 13, 14, 72, 13, 13, 72, 93, 13, 88, 94, 72, 94, 93, 94, 94, 94, 72, 94, 93, 94, 93, 88, 88, 72, 88, 92, 12, 88, 4, 84, 84, 84, 84, 84, 84, 84, 84, 84, 68, 4, 68, 24, 24, 24, 12, 24, 88, 28, 88, 12, 72, 12, 12, 88, 12, 12, 12, 12, 12, 12, 12, 72, 12, 12, 8, 20, 20, 133, 133, 157, 157, 93, 137, 157, 93, 148, 0, 148, 144, 152, 156, 140, 137, 128, 158, 144, 129, 144, 128, 130, 129, 132, 156, 156, 72, 120, 93, 93, 93, 86, 137, 178, 113, 93, 92, 85, 5, 188, 188, 6, 188, 17, 21, 197, 22, 194, 196, 188, 10, 100, 192, 138, 154, 137, 92, 137, 200, 200, 200, 6, 133, 128, 136, 200, 0, 192, 188, 153, 140, 72, 200, 200, 92, 200, 124, 196, 124, 116, 116, 116, 124, 124, 121, 124, 124, 200, 124, 92, 185, 5, 117, 124, 120, 82, 88, 112, 188, 193, 198, 72, 20, 4, 120, 97, 97, 121, 112, 93, 121, 113, 80, 80, 93, 88, 89, 93, 88, 92, 88, 104, 89, 89, 89, 92, 88, 121, 121, 122, 88, 94, 84, 84, 124, 116, 94, 104, 124, 94, 108, 94, 88, 88, 84, 116, 80, 94, 88, 88, 92, 25, 88, 120, 104, 108, 1, 94, 96, 96, 98, 82, 104, 20, 92, 98, 14, 94, 6, 101, 109, 22, 120, 105, 93, 120, 81, 120, 2, 1, 94, 18, 18, 120, 94, 66, 2, 104, 68, 94, 73, 82, 101, 121, 93, 113, 22, 93, 120, 94, 93, 5, 13, 122, 89, 90, 5, 101, 93, 106, 94, 73, 21, 72, 89, 121, 73, 92, 93, 84, 117, 0, 21, 85, 52, 4, 6, 198, 0, 185, 192, 29, 194, 189, 36, 36, 93, 185, 52, 0, 4, 13, 188, 9, 28, 89, 86, 185, 113, 186, 186, 14, 185, 188, 186, 188, 88, 189, 188, 116, 124, 68, 188, 188, 188, 198, 84, 52, 188, 197, 185, 20, 190, 5, 6, 190, 28, 128, 189, 189, 189, 93, 189, 14, 94, 189, 68, 190, 190, 190, 157, 84, 141, 197, 189, 197, 93, 189, 37, 190, 190, 22, 190, 190, 190, 190, 86, 190, 190, 190, 189, 94, 190, 190, 190, 190, 86, 198, 190, 189, 200, 94, 94, 6, 190, 5, 86, 2, 190, 190, 33, 0, 28, 68, 16, 80, 144, 144, 49, 52, 116, 76, 84, 49, 196, 197, 93, 17, 73, 137, 185, 93, 185, 188, 188, 185, 13, 185, 186, 186, 186, 14, 188, 186, 186, 188, 6, 186, 188, 188, 188, 84, 188, 188, 188, 188, 84, 188, 188, 188, 188, 69, 188, 189, 188, 188, 84, 188, 189, 189, 189, 24, 189, 189, 189, 189, 86, 52, 189, 93, 149, 84, 189, 93, 141, 189, 84, 189, 189, 190, 190, 86, 190, 198, 142, 190, 86, 190, 142, 190, 190, 76, 118, 142, 94, 94, 86, 198, 134, 198, 198, 85, 6, 6, 46, 38, 85, 190, 190, 186, 190, 64, 69, 69, 77, 86, 88, 28, 93, 84, 116, 0, 0 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Up_Front_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_Front_U_Down_Front_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_Front_Array[powerwindow_input_length] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_Front_Array[powerwindow_input_length] = {92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 94, 92, 92, 92, 92, 92, 92, 92, 92, 92, 20, 73, 20, 73, 92, 94, 29, 72, 88, 72, 22, 93, 89, 89, 72, 93, 70, 30, 17, 72, 92, 121, 69, 24, 8, 93, 84, 94, 14, 72, 68, 84, 92, 84, 8, 92, 85, 29, 85, 72, 92, 92, 29, 93, 29, 92, 92, 72, 92, 92, 92, 92, 92, 124, 124, 92, 92, 93, 105, 72, 69, 21, 30, 88, 120, 93, 116, 16, 94, 0, 84, 116, 94, 65, 64, 94, 86, 73, 74, 72, 21, 85, 116, 92, 0, 92, 116, 88, 80, 8, 92, 84, 117, 4, 8, 29, 76, 1, 85, 72, 92, 84, 124, 84, 64, 93, 29, 93, 81, 188, 124, 124, 124, 93, 0, 29, 28, 24, 69, 8, 92, 92, 92, 116, 116, 120, 116, 93, 94, 93, 94, 124, 122, 124, 100, 116, 116, 116, 116, 116, 124, 116, 93, 93, 93, 10, 101, 94, 102, 98, 97, 1, 97, 97, 97, 117, 94, 93, 94, 93, 94, 2, 2, 93, 8, 93, 92, 88, 80, 81, 80, 100, 80, 112, 112, 117, 96, 96, 80, 93, 92, 124, 89, 84, 112, 112, 117, 118, 16, 124, 94, 94, 94, 124, 93, 8, 94, 14, 72, 104, 28, 68, 0, 72, 188, 93, 120, 93, 88, 10, 28, 20, 20, 88, 120, 88, 176, 93, 120, 88, 93, 120, 29, 120, 93, 92, 93, 93, 117, 120, 120, 93, 120, 89, 114, 197, 189, 93, 121, 112, 124, 93, 93, 197, 197, 94, 189, 197, 5, 133, 157, 197, 112, 65, 121, 25, 186, 93, 120, 122, 94, 149, 200, 149, 149, 157, 150, 145, 156, 149, 148, 146, 150, 148, 150, 130, 150, 150, 150, 150, 146, 150, 150, 149, 149, 134, 149, 130, 129, 148, 149, 148, 150, 128, 197, 132, 148, 140, 132, 8, 20, 84, 88, 76, 64, 20, 20, 68, 68, 28, 28, 28, 29, 28, 28, 8, 28, 20, 28, 28, 28, 28, 28, 13, 13, 22, 13, 70, 12, 76, 24, 24, 24, 72, 24, 28, 13, 13, 24, 24, 24, 14, 14, 13, 14, 14, 78, 13, 14, 14, 14, 13, 13, 30, 5, 8, 4, 20, 20, 4, 4, 4, 68, 28, 4, 8, 4, 12, 4, 4, 4, 68, 68, 92, 4, 68, 4, 4, 4, 28, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 76, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 12, 12, 12, 12, 12, 12, 14, 12, 72, 14, 13, 12, 14, 14, 13, 14, 72, 13, 13, 72, 93, 13, 88, 94, 72, 94, 93, 94, 94, 94, 72, 94, 93, 94, 93, 88, 88, 72, 88, 92, 12, 88, 4, 84, 84, 84, 84, 84, 84, 84, 84, 84, 68, 4, 68, 24, 24, 24, 12, 24, 88, 28, 88, 12, 72, 12, 12, 88, 12, 12, 12, 12, 12, 12, 12, 72, 12, 12, 8, 20, 20, 133, 133, 157, 157, 93, 137, 157, 93, 148, 0, 148, 144, 152, 156, 140, 137, 128, 158, 144, 129, 144, 128, 130, 129, 132, 156, 156, 72, 120, 93, 93, 93, 86, 137, 178, 113, 93, 92, 85, 5, 188, 188, 6, 188, 17, 21, 197, 22, 194, 196, 188, 10, 100, 192, 138, 154, 137, 92, 137, 200, 200, 200, 6, 133, 128, 136, 200, 0, 192, 188, 153, 140, 72, 200, 200, 92, 200, 124, 196, 124, 116, 116, 116, 124, 124, 121, 124, 124, 200, 124, 92, 185, 5, 117, 124, 120, 82, 88, 112, 188, 193, 198, 72, 20, 4, 120, 97, 97, 121, 112, 93, 121, 113, 80, 80, 93, 88, 89, 93, 88, 92, 88, 104, 89, 89, 89, 92, 88, 121, 121, 122, 88, 94, 84, 84, 124, 116, 94, 104, 124, 94, 108, 94, 88, 88, 84, 116, 80, 94, 88, 88, 92, 25, 88, 120, 104, 108, 1, 94, 96, 96, 98, 82, 104, 20, 92, 98, 14, 94, 6, 101, 109, 22, 120, 105, 93, 120, 81, 120, 2, 1, 94, 18, 18, 120, 94, 66, 2, 104, 68, 94, 73, 82, 101, 121, 93, 113, 22, 93, 120, 94, 93, 5, 13, 122, 89, 90, 5, 101, 93, 106, 94, 73, 21, 72, 89, 121, 73, 92, 93, 84, 117, 0, 21, 85, 52, 4, 6, 198, 0, 185, 192, 29, 194, 189, 36, 36, 93, 185, 52, 0, 4, 13, 188, 9, 28, 89, 86, 185, 113, 186, 186, 14, 185, 188, 186, 188, 88, 189, 188, 116, 124, 68, 188, 188, 188, 198, 84, 52, 188, 197, 185, 20, 190, 5, 6, 190, 28, 128, 189, 189, 189, 93, 189, 14, 94, 189, 68, 190, 190, 190, 157, 84, 141, 197, 189, 197, 93, 189, 37, 190, 190, 22, 190, 190, 190, 190, 86, 190, 190, 190, 189, 94, 190, 190, 190, 190, 86, 198, 190, 189, 200, 94, 94, 6, 190, 5, 86, 2, 190, 190, 33, 0, 28, 68, 16, 80, 144, 144, 49, 52, 116, 76, 84, 49, 196, 197, 93, 17, 73, 137, 185, 93, 185, 188, 188, 185, 13, 185, 186, 186, 186, 14, 188, 186, 186, 188, 6, 186, 188, 188, 188, 84, 188, 188, 188, 188, 84, 188, 188, 188, 188, 69, 188, 189, 188, 188, 84, 188, 189, 189, 189, 24, 189, 189, 189, 189, 86, 52, 189, 93, 149, 84, 189, 93, 141, 189, 84, 189, 189, 190, 190, 86, 190, 198, 142, 190, 86, 190, 142, 190, 190, 76, 118, 142, 94, 94, 86, 198, 134, 198, 198, 85, 6, 6, 46, 38, 85, 190, 190, 186, 190, 64, 69, 69, 77, 86, 88, 28, 93, 84, 116, 0, 0 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Up_BackL_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_BackL_U_Down_BackL_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_BackL_Array[powerwindow_input_length] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_BackL_Array[powerwindow_input_length] = {92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 94, 92, 92, 92, 92, 92, 92, 92, 92, 92, 20, 73, 20, 73, 92, 94, 29, 72, 88, 72, 22, 93, 89, 89, 72, 93, 70, 30, 17, 72, 92, 121, 69, 24, 8, 93, 84, 94, 14, 72, 68, 84, 92, 84, 8, 92, 85, 29, 85, 72, 92, 92, 29, 93, 29, 92, 92, 72, 92, 92, 92, 92, 92, 124, 124, 92, 92, 93, 105, 72, 69, 21, 30, 88, 120, 93, 116, 16, 94, 0, 84, 116, 94, 65, 64, 94, 86, 73, 74, 72, 21, 85, 116, 92, 0, 92, 116, 88, 80, 8, 92, 84, 117, 4, 8, 29, 76, 1, 85, 72, 92, 84, 124, 84, 64, 93, 29, 93, 81, 188, 124, 124, 124, 93, 0, 29, 28, 24, 69, 8, 92, 92, 92, 116, 116, 120, 116, 93, 94, 93, 94, 124, 122, 124, 100, 116, 116, 116, 116, 116, 124, 116, 93, 93, 93, 10, 101, 94, 102, 98, 97, 1, 97, 97, 97, 117, 94, 93, 94, 93, 94, 2, 2, 93, 8, 93, 92, 88, 80, 81, 80, 100, 80, 112, 112, 117, 96, 96, 80, 93, 92, 124, 89, 84, 112, 112, 117, 118, 16, 124, 94, 94, 94, 124, 93, 8, 94, 14, 72, 104, 28, 68, 0, 72, 188, 93, 120, 93, 88, 10, 28, 20, 20, 88, 120, 88, 176, 93, 120, 88, 93, 120, 29, 120, 93, 92, 93, 93, 117, 120, 120, 93, 120, 89, 114, 197, 189, 93, 121, 112, 124, 93, 93, 197, 197, 94, 189, 197, 5, 133, 157, 197, 112, 65, 121, 25, 186, 93, 120, 122, 94, 149, 200, 149, 149, 157, 150, 145, 156, 149, 148, 146, 150, 148, 150, 130, 150, 150, 150, 150, 146, 150, 150, 149, 149, 134, 149, 130, 129, 148, 149, 148, 150, 128, 197, 132, 148, 140, 132, 8, 20, 84, 88, 76, 64, 20, 20, 68, 68, 28, 28, 28, 29, 28, 28, 8, 28, 20, 28, 28, 28, 28, 28, 13, 13, 22, 13, 70, 12, 76, 24, 24, 24, 72, 24, 28, 13, 13, 24, 24, 24, 14, 14, 13, 14, 14, 78, 13, 14, 14, 14, 13, 13, 30, 5, 8, 4, 20, 20, 4, 4, 4, 68, 28, 4, 8, 4, 12, 4, 4, 4, 68, 68, 92, 4, 68, 4, 4, 4, 28, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 76, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 12, 12, 12, 12, 12, 12, 14, 12, 72, 14, 13, 12, 14, 14, 13, 14, 72, 13, 13, 72, 93, 13, 88, 94, 72, 94, 93, 94, 94, 94, 72, 94, 93, 94, 93, 88, 88, 72, 88, 92, 12, 88, 4, 84, 84, 84, 84, 84, 84, 84, 84, 84, 68, 4, 68, 24, 24, 24, 12, 24, 88, 28, 88, 12, 72, 12, 12, 88, 12, 12, 12, 12, 12, 12, 12, 72, 12, 12, 8, 20, 20, 133, 133, 157, 157, 93, 137, 157, 93, 148, 0, 148, 144, 152, 156, 140, 137, 128, 158, 144, 129, 144, 128, 130, 129, 132, 156, 156, 72, 120, 93, 93, 93, 86, 137, 178, 113, 93, 92, 85, 5, 188, 188, 6, 188, 17, 21, 197, 22, 194, 196, 188, 10, 100, 192, 138, 154, 137, 92, 137, 200, 200, 200, 6, 133, 128, 136, 200, 0, 192, 188, 153, 140, 72, 200, 200, 92, 200, 124, 196, 124, 116, 116, 116, 124, 124, 121, 124, 124, 200, 124, 92, 185, 5, 117, 124, 120, 82, 88, 112, 188, 193, 198, 72, 20, 4, 120, 97, 97, 121, 112, 93, 121, 113, 80, 80, 93, 88, 89, 93, 88, 92, 88, 104, 89, 89, 89, 92, 88, 121, 121, 122, 88, 94, 84, 84, 124, 116, 94, 104, 124, 94, 108, 94, 88, 88, 84, 116, 80, 94, 88, 88, 92, 25, 88, 120, 104, 108, 1, 94, 96, 96, 98, 82, 104, 20, 92, 98, 14, 94, 6, 101, 109, 22, 120, 105, 93, 120, 81, 120, 2, 1, 94, 18, 18, 120, 94, 66, 2, 104, 68, 94, 73, 82, 101, 121, 93, 113, 22, 93, 120, 94, 93, 5, 13, 122, 89, 90, 5, 101, 93, 106, 94, 73, 21, 72, 89, 121, 73, 92, 93, 84, 117, 0, 21, 85, 52, 4, 6, 198, 0, 185, 192, 29, 194, 189, 36, 36, 93, 185, 52, 0, 4, 13, 188, 9, 28, 89, 86, 185, 113, 186, 186, 14, 185, 188, 186, 188, 88, 189, 188, 116, 124, 68, 188, 188, 188, 198, 84, 52, 188, 197, 185, 20, 190, 5, 6, 190, 28, 128, 189, 189, 189, 93, 189, 14, 94, 189, 68, 190, 190, 190, 157, 84, 141, 197, 189, 197, 93, 189, 37, 190, 190, 22, 190, 190, 190, 190, 86, 190, 190, 190, 189, 94, 190, 190, 190, 190, 86, 198, 190, 189, 200, 94, 94, 6, 190, 5, 86, 2, 190, 190, 33, 0, 28, 68, 16, 80, 144, 144, 49, 52, 116, 76, 84, 49, 196, 197, 93, 17, 73, 137, 185, 93, 185, 188, 188, 185, 13, 185, 186, 186, 186, 14, 188, 186, 186, 188, 6, 186, 188, 188, 188, 84, 188, 188, 188, 188, 84, 188, 188, 188, 188, 69, 188, 189, 188, 188, 84, 188, 189, 189, 189, 24, 189, 189, 189, 189, 86, 52, 189, 93, 149, 84, 189, 93, 141, 189, 84, 189, 189, 190, 190, 86, 190, 198, 142, 190, 86, 190, 142, 190, 190, 76, 118, 142, 94, 94, 86, 198, 134, 198, 198, 85, 6, 6, 46, 38, 85, 190, 190, 186, 190, 64, 69, 69, 77, 86, 88, 28, 93, 84, 116, 0, 0 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Up_BackR_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_debounce_passenger_BackR_U_Down_BackR_Array[powerwindow_input_length] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1 };
+
+ powerwindow_boolean_T powerwindow_powerwindow_control_U_endofdetectionrange_BackR_Array[powerwindow_input_length] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
+
+ powerwindow_uint8_T powerwindow_powerwindow_control_U_currentsense_BackR_Array[powerwindow_input_length] = {92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 94, 92, 92, 92, 92, 92, 92, 92, 92, 92, 20, 73, 20, 73, 92, 94, 29, 72, 88, 72, 22, 93, 89, 89, 72, 93, 70, 30, 17, 72, 92, 121, 69, 24, 8, 93, 84, 94, 14, 72, 68, 84, 92, 84, 8, 92, 85, 29, 85, 72, 92, 92, 29, 93, 29, 92, 92, 72, 92, 92, 92, 92, 92, 124, 124, 92, 92, 93, 105, 72, 69, 21, 30, 88, 120, 93, 116, 16, 94, 0, 84, 116, 94, 65, 64, 94, 86, 73, 74, 72, 21, 85, 116, 92, 0, 92, 116, 88, 80, 8, 92, 84, 117, 4, 8, 29, 76, 1, 85, 72, 92, 84, 124, 84, 64, 93, 29, 93, 81, 188, 124, 124, 124, 93, 0, 29, 28, 24, 69, 8, 92, 92, 92, 116, 116, 120, 116, 93, 94, 93, 94, 124, 122, 124, 100, 116, 116, 116, 116, 116, 124, 116, 93, 93, 93, 10, 101, 94, 102, 98, 97, 1, 97, 97, 97, 117, 94, 93, 94, 93, 94, 2, 2, 93, 8, 93, 92, 88, 80, 81, 80, 100, 80, 112, 112, 117, 96, 96, 80, 93, 92, 124, 89, 84, 112, 112, 117, 118, 16, 124, 94, 94, 94, 124, 93, 8, 94, 14, 72, 104, 28, 68, 0, 72, 188, 93, 120, 93, 88, 10, 28, 20, 20, 88, 120, 88, 176, 93, 120, 88, 93, 120, 29, 120, 93, 92, 93, 93, 117, 120, 120, 93, 120, 89, 114, 197, 189, 93, 121, 112, 124, 93, 93, 197, 197, 94, 189, 197, 5, 133, 157, 197, 112, 65, 121, 25, 186, 93, 120, 122, 94, 149, 200, 149, 149, 157, 150, 145, 156, 149, 148, 146, 150, 148, 150, 130, 150, 150, 150, 150, 146, 150, 150, 149, 149, 134, 149, 130, 129, 148, 149, 148, 150, 128, 197, 132, 148, 140, 132, 8, 20, 84, 88, 76, 64, 20, 20, 68, 68, 28, 28, 28, 29, 28, 28, 8, 28, 20, 28, 28, 28, 28, 28, 13, 13, 22, 13, 70, 12, 76, 24, 24, 24, 72, 24, 28, 13, 13, 24, 24, 24, 14, 14, 13, 14, 14, 78, 13, 14, 14, 14, 13, 13, 30, 5, 8, 4, 20, 20, 4, 4, 4, 68, 28, 4, 8, 4, 12, 4, 4, 4, 68, 68, 92, 4, 68, 4, 4, 4, 28, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 76, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 8, 12, 12, 12, 12, 12, 12, 12, 14, 12, 72, 14, 13, 12, 14, 14, 13, 14, 72, 13, 13, 72, 93, 13, 88, 94, 72, 94, 93, 94, 94, 94, 72, 94, 93, 94, 93, 88, 88, 72, 88, 92, 12, 88, 4, 84, 84, 84, 84, 84, 84, 84, 84, 84, 68, 4, 68, 24, 24, 24, 12, 24, 88, 28, 88, 12, 72, 12, 12, 88, 12, 12, 12, 12, 12, 12, 12, 72, 12, 12, 8, 20, 20, 133, 133, 157, 157, 93, 137, 157, 93, 148, 0, 148, 144, 152, 156, 140, 137, 128, 158, 144, 129, 144, 128, 130, 129, 132, 156, 156, 72, 120, 93, 93, 93, 86, 137, 178, 113, 93, 92, 85, 5, 188, 188, 6, 188, 17, 21, 197, 22, 194, 196, 188, 10, 100, 192, 138, 154, 137, 92, 137, 200, 200, 200, 6, 133, 128, 136, 200, 0, 192, 188, 153, 140, 72, 200, 200, 92, 200, 124, 196, 124, 116, 116, 116, 124, 124, 121, 124, 124, 200, 124, 92, 185, 5, 117, 124, 120, 82, 88, 112, 188, 193, 198, 72, 20, 4, 120, 97, 97, 121, 112, 93, 121, 113, 80, 80, 93, 88, 89, 93, 88, 92, 88, 104, 89, 89, 89, 92, 88, 121, 121, 122, 88, 94, 84, 84, 124, 116, 94, 104, 124, 94, 108, 94, 88, 88, 84, 116, 80, 94, 88, 88, 92, 25, 88, 120, 104, 108, 1, 94, 96, 96, 98, 82, 104, 20, 92, 98, 14, 94, 6, 101, 109, 22, 120, 105, 93, 120, 81, 120, 2, 1, 94, 18, 18, 120, 94, 66, 2, 104, 68, 94, 73, 82, 101, 121, 93, 113, 22, 93, 120, 94, 93, 5, 13, 122, 89, 90, 5, 101, 93, 106, 94, 73, 21, 72, 89, 121, 73, 92, 93, 84, 117, 0, 21, 85, 52, 4, 6, 198, 0, 185, 192, 29, 194, 189, 36, 36, 93, 185, 52, 0, 4, 13, 188, 9, 28, 89, 86, 185, 113, 186, 186, 14, 185, 188, 186, 188, 88, 189, 188, 116, 124, 68, 188, 188, 188, 198, 84, 52, 188, 197, 185, 20, 190, 5, 6, 190, 28, 128, 189, 189, 189, 93, 189, 14, 94, 189, 68, 190, 190, 190, 157, 84, 141, 197, 189, 197, 93, 189, 37, 190, 190, 22, 190, 190, 190, 190, 86, 190, 190, 190, 189, 94, 190, 190, 190, 190, 86, 198, 190, 189, 200, 94, 94, 6, 190, 5, 86, 2, 190, 190, 33, 0, 28, 68, 16, 80, 144, 144, 49, 52, 116, 76, 84, 49, 196, 197, 93, 17, 73, 137, 185, 93, 185, 188, 188, 185, 13, 185, 186, 186, 186, 14, 188, 186, 186, 188, 6, 186, 188, 188, 188, 84, 188, 188, 188, 188, 84, 188, 188, 188, 188, 69, 188, 189, 188, 188, 84, 188, 189, 189, 189, 24, 189, 189, 189, 189, 86, 52, 189, 93, 149, 84, 189, 93, 141, 189, 84, 189, 189, 190, 190, 86, 190, 198, 142, 190, 86, 190, 142, 190, 190, 76, 118, 142, 94, 94, 86, 198, 134, 198, 198, 85, 6, 6, 46, 38, 85, 190, 190, 186, 190, 64, 69, 69, 77, 86, 88, 28, 93, 84, 116, 0, 0 };
+
diff --git a/test/monniaux/tacle-bench-powerwindow/powerwindow_powerwindow_control.c b/test/monniaux/tacle-bench-powerwindow/powerwindow_powerwindow_control.c
new file mode 100644
index 00000000..8bce1869
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/powerwindow_powerwindow_control.c
@@ -0,0 +1,523 @@
+/*
+
+ This program is part of the TACLeBench benchmark suite.
+ Version V 1.x
+
+ Name: powerwindow_powerwindow_control.c
+
+ Author: CoSys-Lab, University of Antwerp
+
+ Function: powerwindow_control is the main functionality of the power window benchmark.
+ It contains 3 states: System, EndReached and Pinch, which are used to controll the
+ position of the glass, if the window is fully closed and sensing pinch force to realize
+ the powerwindow function.
+
+ Source: https://github.com/tacle/tacle-bench/blob/master/bench/app/PowerWindow//powerwindow_powerwindow_control.c
+
+ Changes: a brief summary of major functional changes and formatting)
+
+ License: GNU General Public License
+
+*/
+
+#include "wcclib.h"
+#include "powerwindow_HeaderFiles/powerwindow_powerwindow_control.h"
+#include "powerwindow_HeaderFiles/powerwindow_powerwindow_control_private.h"
+
+/* Named constants for Chart: '<S2>/stateflow control model' */
+#define powerwindow_powerwindow__IN_NO_ACTIVE_CHILD ((powerwindow_uint8_T)0U)
+#define powerwindow_powerwindow_contr_IN_EndReached ((powerwindow_uint8_T)1U)
+#define powerwindow_powerwindow_contr_IN_SensePinch ((powerwindow_uint8_T)2U)
+#define powerwindow_powerwindow_control_IN_AutoDown ((powerwindow_uint8_T)1U)
+#define powerwindow_powerwindow_control_IN_AutoUp ((powerwindow_uint8_T)1U)
+#define powerwindow_powerwindow_control_IN_Down ((powerwindow_uint8_T)2U)
+#define powerwindow_powerwindow_control_IN_Down_d ((powerwindow_uint8_T)1U)
+#define powerwindow_powerwindow_control_IN_InitDown ((powerwindow_uint8_T)3U)
+#define powerwindow_powerwindow_control_IN_InitUp ((powerwindow_uint8_T)2U)
+#define powerwindow_powerwindow_control_IN_Neutral ((powerwindow_uint8_T)2U)
+#define powerwindow_powerwindow_control_IN_Pinch ((powerwindow_uint8_T)2U)
+#define powerwindow_powerwindow_control_IN_SenseEnd ((powerwindow_uint8_T)1U)
+#define powerwindow_powerwindow_control_IN_Start ((powerwindow_uint8_T)3U)
+#define powerwindow_powerwindow_control_IN_System ((powerwindow_uint8_T)3U)
+#define powerwindow_powerwindow_control_IN_Up ((powerwindow_uint8_T)3U)
+
+/* Forward declaration for local functions */
+void powerwindow_powerwindow_control_Start(powerwindow_rtDW_PowerWindow_control *localDW);
+
+void powerwindow_powerwindow_control_Init(powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T
+ *rty_window_down, powerwindow_boolean_T *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T *
+ rty_wake, powerwindow_rtDW_PowerWindow_control *localDW);
+
+void powerwindow_powerwindow_control_Start(powerwindow_rtDW_PowerWindow_control *localDW);
+
+void powerwindow_powerwindow_control_initialize(const powerwindow_char_T **rt_errorStatus,
+ powerwindow_RT_MODEL_PowerWindow_control *const PowerWindow_control_M,
+ powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE);
+
+void powerwindow_powerwindow_control_main(const powerwindow_boolean_T *rtu_up, const powerwindow_boolean_T *rtu_down,
+ const powerwindow_boolean_T *rtu_endofdetectionrange, const powerwindow_uint8_T *rtu_currentsense,
+ powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T *rty_window_down, powerwindow_boolean_T
+ *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T *rty_wake,
+ powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE);
+
+void powerwindow_powerwindow_con_broadcast_ticks(powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T *
+ rty_window_down, powerwindow_boolean_T *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T
+ *rty_wake, powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW);
+
+/* Function for Chart: '<S2>/stateflow control model' */
+void powerwindow_powerwindow_con_broadcast_ticks(powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T *
+ rty_window_down, powerwindow_boolean_T *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T
+ *rty_wake, powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW)
+{
+ /* Event: '<S3>:30' */
+ /* During: PW_PSG/PWExternalClock/stateflow control model */
+ if (localDW->is_active_c2_PowerWindow_contro == 0U) {
+ /* Entry: PW_PSG/PWExternalClock/stateflow control model */
+ localDW->is_active_c2_PowerWindow_contro = 1U;
+
+ /* Entry Internal: PW_PSG/PWExternalClock/stateflow control model */
+ /* Transition: '<S3>:102' */
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow_control_IN_System;
+
+ /* Entry Internal 'System': '<S3>:94' */
+ localDW->is_active_Logic = 1U;
+
+ /* Entry Internal 'Logic': '<S3>:95' */
+ /* Transition: '<S3>:82' */
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Neutral;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Neutral': '<S3>:16' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_wake = false;
+ localDW->is_active_Sensing = 1U;
+
+ /* Entry Internal 'Sensing': '<S3>:96' */
+ /* Transition: '<S3>:153' */
+ localDW->is_Sensing = powerwindow_powerwindow_control_IN_Start;
+ localDW->temporalCounter_i2 = 0U;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Start': '<S3>:170' */
+ *rty_overcurrent = false;
+ *rty_pinch = false;
+ } else {
+ switch (localDW->is_c2_PowerWindow_control) {
+ case powerwindow_powerwindow_contr_IN_EndReached:
+ /* During 'EndReached': '<S3>:97' */
+ if (localDW->temporalCounter_i1 >= 10) {
+ /* Transition: '<S3>:101' */
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow_control_IN_System;
+
+ /* Entry Internal 'System': '<S3>:94' */
+ localDW->is_active_Logic = 1U;
+
+ /* Entry Internal 'Logic': '<S3>:95' */
+ /* Transition: '<S3>:82' */
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Neutral;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Neutral': '<S3>:16' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_wake = false;
+ localDW->is_active_Sensing = 1U;
+
+ /* Entry Internal 'Sensing': '<S3>:96' */
+ /* Transition: '<S3>:153' */
+ localDW->is_Sensing = powerwindow_powerwindow_control_IN_Start;
+ localDW->temporalCounter_i2 = 0U;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Start': '<S3>:170' */
+ *rty_overcurrent = false;
+ *rty_pinch = false;
+ }
+ break;
+
+ case powerwindow_powerwindow_control_IN_Pinch:
+ /* During 'Pinch': '<S3>:152' */
+ if (localDW->temporalCounter_i1 >= 40) {
+ /* Transition: '<S3>:157' */
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow_control_IN_System;
+
+ /* Entry Internal 'System': '<S3>:94' */
+ localDW->is_active_Logic = 1U;
+
+ /* Entry Internal 'Logic': '<S3>:95' */
+ /* Transition: '<S3>:82' */
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Neutral;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Neutral': '<S3>:16' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_wake = false;
+ localDW->is_active_Sensing = 1U;
+
+ /* Entry Internal 'Sensing': '<S3>:96' */
+ /* Transition: '<S3>:153' */
+ localDW->is_Sensing = powerwindow_powerwindow_control_IN_Start;
+ localDW->temporalCounter_i2 = 0U;
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* Entry 'Start': '<S3>:170' */
+ *rty_overcurrent = false;
+ *rty_pinch = false;
+ }
+ break;
+
+ default:
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* During 'System': '<S3>:94' */
+ if (*rty_pinch == 1) {
+ /* Transition: '<S3>:155' */
+ /* Exit Internal 'System': '<S3>:94' */
+ /* Exit Internal 'Sensing': '<S3>:96' */
+ localDW->is_Sensing = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_active_Sensing = 0U;
+
+ /* Exit Internal 'Logic': '<S3>:95' */
+ /* Exit Internal 'Down': '<S3>:18' */
+ localDW->is_Down = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+
+ /* Exit Internal 'Up': '<S3>:17' */
+ localDW->is_Up = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_active_Logic = 0U;
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow_control_IN_Pinch;
+ localDW->temporalCounter_i1 = 0U;
+
+ /* Entry 'Pinch': '<S3>:152' */
+ *rty_window_up = false;
+ *rty_window_down = true;
+ } else if (*rty_overcurrent == 1) {
+ /* Transition: '<S3>:100' */
+ /* Exit Internal 'System': '<S3>:94' */
+ /* Exit Internal 'Sensing': '<S3>:96' */
+ localDW->is_Sensing = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_active_Sensing = 0U;
+
+ /* Exit Internal 'Logic': '<S3>:95' */
+ /* Exit Internal 'Down': '<S3>:18' */
+ localDW->is_Down = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+
+ /* Exit Internal 'Up': '<S3>:17' */
+ localDW->is_Up = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_active_Logic = 0U;
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow_contr_IN_EndReached;
+ localDW->temporalCounter_i1 = 0U;
+
+ /* Entry 'EndReached': '<S3>:97' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ } else {
+ /* During 'Logic': '<S3>:95' */
+ switch (localDW->is_Logic) {
+ case powerwindow_powerwindow_control_IN_Down_d:
+ /* During 'Down': '<S3>:18' */
+ if (localB->map[1]) {
+ /* Transition: '<S3>:169' */
+ /* Exit Internal 'Down': '<S3>:18' */
+ localDW->is_Down = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Up;
+
+ /* Entry 'Up': '<S3>:17' */
+ *rty_window_up = true;
+ *rty_window_down = false;
+ *rty_wake = true;
+ localDW->is_Up = powerwindow_powerwindow_control_IN_Up;
+ } else {
+ switch (localDW->is_Down) {
+ case powerwindow_powerwindow_control_IN_AutoDown:
+ /* During 'AutoDown': '<S3>:111' */
+ break;
+
+ case powerwindow_powerwindow_control_IN_Down:
+ /* During 'Down': '<S3>:110' */
+ if (localB->map[0]) {
+ /* Transition: '<S3>:26' */
+ localDW->is_Down = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Neutral;
+
+ /* Entry 'Neutral': '<S3>:16' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_wake = false;
+ }
+ break;
+
+ default:
+ /* During 'InitDown': '<S3>:109' */
+ if (localDW->temporalCounter_i1 >= 20) {
+ /* Transition: '<S3>:119' */
+ if (localB->map[0]) {
+ /* Transition: '<S3>:120' */
+ localDW->is_Down = powerwindow_powerwindow_control_IN_AutoDown;
+ } else {
+ if (localB->map[2]) {
+ /* Transition: '<S3>:121' */
+ localDW->is_Down = powerwindow_powerwindow_control_IN_Down;
+ }
+ }
+ }
+ break;
+ }
+ }
+ break;
+
+ case powerwindow_powerwindow_control_IN_Neutral:
+ /* During 'Neutral': '<S3>:16' */
+ if (localB->map[1]) {
+ /* Transition: '<S3>:24' */
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Up;
+
+ /* Entry 'Up': '<S3>:17' */
+ *rty_window_up = true;
+ *rty_window_down = false;
+ *rty_wake = true;
+ localDW->is_Up = powerwindow_powerwindow_control_IN_InitUp;
+ localDW->temporalCounter_i1 = 0U;
+ } else {
+ if (localB->map[2]) {
+ /* Transition: '<S3>:25' */
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Down_d;
+
+ /* Entry 'Down': '<S3>:18' */
+ *rty_window_up = false;
+ *rty_window_down = true;
+ *rty_wake = true;
+ localDW->is_Down = powerwindow_powerwindow_control_IN_InitDown;
+ localDW->temporalCounter_i1 = 0U;
+ }
+ }
+ break;
+
+ default:
+ /* During 'Up': '<S3>:17' */
+ if (localB->map[2]) {
+ /* Transition: '<S3>:166' */
+ /* Exit Internal 'Up': '<S3>:17' */
+ localDW->is_Up = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Down_d;
+
+ /* Entry 'Down': '<S3>:18' */
+ *rty_window_up = false;
+ *rty_window_down = true;
+ *rty_wake = true;
+ localDW->is_Down = powerwindow_powerwindow_control_IN_Down;
+ } else {
+ switch (localDW->is_Up) {
+ case powerwindow_powerwindow_control_IN_AutoUp:
+ /* During 'AutoUp': '<S3>:108' */
+ break;
+
+ case powerwindow_powerwindow_control_IN_InitUp:
+ /* During 'InitUp': '<S3>:106' */
+ if (localDW->temporalCounter_i1 >= 20) {
+ /* Transition: '<S3>:115' */
+ if (localB->map[0]) {
+ /* Transition: '<S3>:118' */
+ localDW->is_Up = powerwindow_powerwindow_control_IN_AutoUp;
+ } else {
+ if (localB->map[1]) {
+ /* Transition: '<S3>:117' */
+ localDW->is_Up = powerwindow_powerwindow_control_IN_Up;
+ }
+ }
+ }
+ break;
+
+ default:
+ /* During 'Up': '<S3>:107' */
+ if (localB->map[0]) {
+ /* Transition: '<S3>:23' */
+ localDW->is_Up = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Logic = powerwindow_powerwindow_control_IN_Neutral;
+
+ /* Entry 'Neutral': '<S3>:16' */
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_wake = false;
+ }
+ break;
+ }
+ }
+ break;
+ }
+
+ /* During 'Sensing': '<S3>:96' */
+ switch (localDW->is_Sensing) {
+ case powerwindow_powerwindow_control_IN_SenseEnd:
+ /* During 'SenseEnd': '<S3>:147' */
+ if ((localB->LogicalOperator == 0) && (*rty_window_up == 1)) {
+ /* Transition: '<S3>:173' */
+ localDW->is_Sensing = powerwindow_powerwindow_control_IN_Start;
+ localDW->temporalCounter_i2 = 0U;
+
+ /* Entry 'Start': '<S3>:170' */
+ *rty_overcurrent = false;
+ *rty_pinch = false;
+ } else {
+ *rty_overcurrent = (localB->RateTransition1 > 184);
+ }
+ break;
+
+ case powerwindow_powerwindow_contr_IN_SensePinch:
+ /* During 'SensePinch': '<S3>:148' */
+ if ((localB->LogicalOperator == 1) || (*rty_window_down == 1)) {
+ /* Transition: '<S3>:150' */
+ localDW->is_Sensing = powerwindow_powerwindow_control_IN_SenseEnd;
+ } else {
+ *rty_pinch = (localB->RateTransition1 > 92);
+ }
+ break;
+
+ default:
+ /* During 'Start': '<S3>:170' */
+ if (localDW->temporalCounter_i2 >= 6) {
+ /* Transition: '<S3>:171' */
+ localDW->is_Sensing = powerwindow_powerwindow_contr_IN_SensePinch;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ }
+}
+
+/* Initial conditions for referenced model: 'powerwindow_powerwindow_control' */
+void powerwindow_powerwindow_control_Init(powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T
+ *rty_window_down, powerwindow_boolean_T *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T *
+ rty_wake, powerwindow_rtDW_PowerWindow_control *localDW)
+{
+ /* InitializeConditions for Chart: '<S2>/stateflow control model' */
+ localDW->is_active_Logic = 0U;
+ localDW->is_Logic = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Down = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->is_Up = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->temporalCounter_i1 = 0U;
+ localDW->is_active_Sensing = 0U;
+ localDW->is_Sensing = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ localDW->temporalCounter_i2 = 0U;
+ localDW->is_active_c2_PowerWindow_contro = 0U;
+ localDW->is_c2_PowerWindow_control = powerwindow_powerwindow__IN_NO_ACTIVE_CHILD;
+ *rty_window_up = false;
+ *rty_window_down = false;
+ *rty_overcurrent = false;
+ *rty_pinch = false;
+ *rty_wake = false;
+}
+
+/* Start for referenced model: 'powerwindow_powerwindow_control' */
+void powerwindow_powerwindow_control_Start(powerwindow_rtDW_PowerWindow_control *localDW)
+{
+ /* Start for DiscretePulseGenerator: '<S2>/period of 50ms' */
+ localDW->clockTickCounter = 0L;
+}
+
+/* Output and update for referenced model: 'powerwindow_powerwindow_control' */
+void powerwindow_powerwindow_control_main(const powerwindow_boolean_T *rtu_up, const powerwindow_boolean_T *rtu_down,
+ const powerwindow_boolean_T *rtu_endofdetectionrange, const powerwindow_uint8_T *rtu_currentsense,
+ powerwindow_boolean_T *rty_window_up, powerwindow_boolean_T *rty_window_down, powerwindow_boolean_T
+ *rty_overcurrent, powerwindow_boolean_T *rty_pinch, powerwindow_boolean_T *rty_wake,
+ powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE)
+{
+ powerwindow_int16_T rowIdx;
+ powerwindow_int16_T rtb_periodof50ms;
+
+ /* DiscretePulseGenerator: '<S2>/period of 50ms' */
+ rtb_periodof50ms = (localDW->clockTickCounter < 5L) &&
+ (localDW->clockTickCounter >= 0L) ? 1 : 0;
+ if (localDW->clockTickCounter >= 9L) {
+ localDW->clockTickCounter = 0L;
+ } else {
+ localDW->clockTickCounter++;
+ }
+
+ /* End of DiscretePulseGenerator: '<S2>/period of 50ms' */
+
+ /* Logic: '<S2>/Logical Operator' */
+ localB->LogicalOperator = !*rtu_endofdetectionrange;
+
+ /* RateTransition: '<S2>/Rate Transition1' */
+ localB->RateTransition1 = *rtu_currentsense;
+
+ /* CombinatorialLogic: '<S2>/map' */
+ rowIdx = (powerwindow_int16_T)(((powerwindow_uint16_T)*rtu_up << 1) + *rtu_down);
+ localB->map[0U] = rtCP_map_table[(powerwindow_uint16_T)rowIdx];
+ localB->map[1U] = rtCP_map_table[rowIdx + 4U];
+ localB->map[2U] = rtCP_map_table[rowIdx + 8U];
+
+ /* Chart: '<S2>/stateflow control model' incorporates:
+ * TriggerPort: '<S3>/ticks'
+ */
+ /* DataTypeConversion: '<S2>/Data Type Conversion' */
+ if (((rtb_periodof50ms != 0) != (localZCE->stateflowcontrolmodel_Trig_ZCE ==
+ powerwindow_POS_ZCSIG)) && (localZCE->stateflowcontrolmodel_Trig_ZCE !=
+ powerwindow_UNINITIALIZED_ZCSIG)) {
+ /* Gateway: PW_PSG/PWExternalClock/stateflow control model */
+ if (localDW->temporalCounter_i1 < 63U) {
+ localDW->temporalCounter_i1++;
+ }
+
+ if (localDW->temporalCounter_i2 < 7U) {
+ localDW->temporalCounter_i2++;
+ }
+
+ powerwindow_powerwindow_con_broadcast_ticks(rty_window_up, rty_window_down,
+ rty_overcurrent, rty_pinch, rty_wake, localB, localDW);
+ }
+
+ localZCE->stateflowcontrolmodel_Trig_ZCE = (powerwindow_uint8_T)(rtb_periodof50ms != 0 ?
+ (powerwindow_int16_T)powerwindow_POS_ZCSIG : (powerwindow_int16_T)powerwindow_ZERO_ZCSIG);
+
+ /* End of DataTypeConversion: '<S2>/Data Type Conversion' */
+}
+
+/* Model initialize function */
+void powerwindow_powerwindow_control_initialize(const powerwindow_char_T **rt_errorStatus,
+ powerwindow_RT_MODEL_PowerWindow_control *const PowerWindow_control_M,
+ powerwindow_rtB_PowerWindow_control *localB, powerwindow_rtDW_PowerWindow_control *localDW,
+ powerwindow_rtZCE_PowerWindow_control *localZCE)
+{
+ /* Registration code */
+
+ /* initialize error status */
+ powerwindow_powerwindow_control_rtmSetErrorStatusPointer(PowerWindow_control_M, rt_errorStatus);
+
+ /* block I/O */
+ (void) memset(((void *) localB), 0,
+ sizeof(powerwindow_rtB_PowerWindow_control));
+
+ /* states (dwork) */
+ (void) memset((void *)localDW, 0,
+ sizeof(powerwindow_rtDW_PowerWindow_control));
+ localZCE->stateflowcontrolmodel_Trig_ZCE = powerwindow_UNINITIALIZED_ZCSIG;
+}
+
+/*
+ * File trailer for generated code.
+ *
+ * [EOF]
+ */
diff --git a/test/monniaux/tacle-bench-powerwindow/wcclib.c b/test/monniaux/tacle-bench-powerwindow/wcclib.c
new file mode 100644
index 00000000..20d58fdc
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/wcclib.c
@@ -0,0 +1,12 @@
+#include "wcclib.h"
+
+
+void *memset( void *s, int c, size_t n )
+{
+ unsigned char *p = s;
+
+ _Pragma( "loopbound min 1 max 368" )
+ while ( n-- )
+ *p++ = (unsigned char) c;
+ return( s );
+}
diff --git a/test/monniaux/tacle-bench-powerwindow/wcclib.h b/test/monniaux/tacle-bench-powerwindow/wcclib.h
new file mode 100644
index 00000000..5a5dc36f
--- /dev/null
+++ b/test/monniaux/tacle-bench-powerwindow/wcclib.h
@@ -0,0 +1,14 @@
+#ifndef _WCCLIB
+#define _WCCLIB
+
+#define size_t unsigned long
+#define int32_t int
+#define uint32_t unsigned int
+#define u_int16_t unsigned short
+#define u_int32_t unsigned int
+
+#define NULL ( (void *) 0)
+
+void *memset( void *s, int c, size_t n );
+
+#endif // _WCCLIB
diff --git a/test/monniaux/ternary/make.proto b/test/monniaux/ternary/make.proto
new file mode 100644
index 00000000..1568ed07
--- /dev/null
+++ b/test/monniaux/ternary/make.proto
@@ -0,0 +1,2 @@
+target: ternary
+measures: [cycles]
diff --git a/test/monniaux/ternary/ternary.c b/test/monniaux/ternary/ternary.c
new file mode 100644
index 00000000..e8813a5c
--- /dev/null
+++ b/test/monniaux/ternary/ternary.c
@@ -0,0 +1,24 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "../clock.h"
+#include "../ternary.h"
+
+typedef uint32_t data;
+
+data silly_computation(void) {
+ data x = 1;
+ for(int i=0; i<10000; i++) {
+ x = x * TERNARY32(x & 0x100, 45561U, 337777U);
+ }
+ return x;
+}
+
+int main() {
+ clock_prepare();
+ clock_start();
+ data result = silly_computation();
+ clock_stop();
+ printf("result=%" PRIu32 "\ncycles:%" PRIu64 "\n", result, get_total_clock());
+ return 0;
+}
diff --git a/test/monniaux/ternary_builtin/ternary_builtin.c b/test/monniaux/ternary_builtin/ternary_builtin.c
new file mode 100644
index 00000000..218c5b28
--- /dev/null
+++ b/test/monniaux/ternary_builtin/ternary_builtin.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "../ternary.h"
+
+int main(int argc, char **argv) {
+ int i=0;
+ if (argc >= 2) i=atoi(argv[1]);
+ printf("%d\n",ternary_uint32(i, 42, 69));
+ return 0;
+}
diff --git a/test/monniaux/thread_local/thread_local.c b/test/monniaux/thread_local/thread_local.c
new file mode 100644
index 00000000..7a50db0a
--- /dev/null
+++ b/test/monniaux/thread_local/thread_local.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+_Thread_local int toto;
+_Thread_local int toto2 = 45;
+
+int foobar(void) {
+ return toto;
+}
+
+int main() {
+ printf("%d %d\n", toto, toto2);
+ return 0;
+}
diff --git a/test/monniaux/thread_local/thread_local2.c b/test/monniaux/thread_local/thread_local2.c
new file mode 100644
index 00000000..ba244ac6
--- /dev/null
+++ b/test/monniaux/thread_local/thread_local2.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <pthread.h>
+
+_Thread_local int toto;
+_Thread_local int toto2 = 45;
+
+void* poulet(void * dummy) {
+ printf("%p %p\n", &toto, &toto2);
+ return NULL;
+}
+
+int main() {
+ pthread_t thr;
+ poulet(NULL);
+ pthread_create(&thr, NULL, poulet, NULL);
+ pthread_join(thr, NULL);
+ return 0;
+}
diff --git a/test/monniaux/tiff-4.0.10/Makefile b/test/monniaux/tiff-4.0.10/Makefile
new file mode 100644
index 00000000..ac1aa276
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/Makefile
@@ -0,0 +1,7 @@
+TARGET=ppm2tiff
+ALL_CFLAGS=-lm
+ALL_CCOMPFLAGS = -flongdouble
+EXECUTE_ARGS= -c g3 __BASE__.g3.tif < example_bw.pbm
+
+include ../rules.mk
+
diff --git a/test/monniaux/tiff-4.0.10/example_bw.pbm b/test/monniaux/tiff-4.0.10/example_bw.pbm
new file mode 100644
index 00000000..971a82bb
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/example_bw.pbm
Binary files differ
diff --git a/test/monniaux/tiff-4.0.10/example_bw.pbm.bz2 b/test/monniaux/tiff-4.0.10/example_bw.pbm.bz2
new file mode 100644
index 00000000..ebf0ede0
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/example_bw.pbm.bz2
Binary files differ
diff --git a/test/monniaux/tiff-4.0.10/make.proto b/test/monniaux/tiff-4.0.10/make.proto
new file mode 100644
index 00000000..3a072b4c
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/make.proto
@@ -0,0 +1,4 @@
+sources: "$(wildcard *.c)"
+target: ppm2tiff
+measures: [cycles]
+name: ppm2tiff
diff --git a/test/monniaux/tiff-4.0.10/ppm2tiff.c b/test/monniaux/tiff-4.0.10/ppm2tiff.c
new file mode 100644
index 00000000..4fcee53e
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/ppm2tiff.c
@@ -0,0 +1,411 @@
+/*
+ * Copyright (c) 1991-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+// VERIMAG
+#define _POSIX_C_SOURCE 2
+#define VERIMAG 1
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+#include "tif_config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+
+#ifdef HAVE_IO_H
+# include <io.h>
+#endif
+
+#ifdef NEED_LIBPORT
+# include "libport.h"
+#endif
+
+#include "tiffio.h"
+
+#ifndef HAVE_GETOPT
+extern int getopt(int, char**, char*);
+#endif
+
+#define streq(a,b) (strcmp(a,b) == 0)
+#define strneq(a,b,n) (strncmp(a,b,n) == 0)
+
+static uint16 compression = COMPRESSION_PACKBITS;
+static uint16 predictor = 0;
+static int quality = 75; /* JPEG quality */
+static int jpegcolormode = JPEGCOLORMODE_RGB;
+static uint32 g3opts;
+
+static void usage(void);
+static int processCompressOptions(char*);
+
+static void
+BadPPM(char* file)
+{
+ fprintf(stderr, "%s: Not a PPM file.\n", file);
+ exit(-2);
+}
+
+
+#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
+#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
+
+static tmsize_t
+multiply_ms(tmsize_t m1, tmsize_t m2)
+{
+ if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
+ return 0;
+ return m1 * m2;
+}
+
+int
+main(int argc, char* argv[])
+{
+ uint16 photometric = 0;
+ uint32 rowsperstrip = (uint32) -1;
+ double resolution = -1;
+ unsigned char *buf = NULL;
+ tmsize_t linebytes = 0;
+ uint16 spp = 1;
+ uint16 bpp = 8;
+ TIFF *out;
+ FILE *in;
+ unsigned int w, h, prec, row;
+ char *infile;
+ int c;
+#if !HAVE_DECL_OPTARG
+ extern int optind;
+ extern char* optarg;
+#endif
+ tmsize_t scanline_size;
+
+ if (argc < 2) {
+ fprintf(stderr, "%s: Too few arguments\n", argv[0]);
+ usage();
+ }
+ while ((c = getopt(argc, argv, "c:r:R:")) != -1)
+ switch (c) {
+ case 'c': /* compression scheme */
+ if (!processCompressOptions(optarg))
+ usage();
+ break;
+ case 'r': /* rows/strip */
+ rowsperstrip = atoi(optarg);
+ break;
+ case 'R': /* resolution */
+ resolution = atof(optarg);
+ break;
+ case '?':
+ usage();
+ /*NOTREACHED*/
+ }
+
+ if (optind + 2 < argc) {
+ fprintf(stderr, "%s: Too many arguments\n", argv[0]);
+ usage();
+ }
+
+ /*
+ * If only one file is specified, read input from
+ * stdin; otherwise usage is: ppm2tiff input output.
+ */
+ if (argc - optind > 1) {
+ infile = argv[optind++];
+ in = fopen(infile, "rb");
+ if (in == NULL) {
+ fprintf(stderr, "%s: Can not open.\n", infile);
+ return (-1);
+ }
+ } else {
+ infile = "<stdin>";
+ in = stdin;
+#if defined(HAVE_SETMODE) && defined(O_BINARY)
+ setmode(fileno(stdin), O_BINARY);
+#endif
+ }
+
+ if (fgetc(in) != 'P')
+ BadPPM(infile);
+ switch (fgetc(in)) {
+ case '4': /* it's a PBM file */
+ bpp = 1;
+ spp = 1;
+ photometric = PHOTOMETRIC_MINISWHITE;
+ break;
+ case '5': /* it's a PGM file */
+ bpp = 8;
+ spp = 1;
+ photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+ case '6': /* it's a PPM file */
+ bpp = 8;
+ spp = 3;
+ photometric = PHOTOMETRIC_RGB;
+ if (compression == COMPRESSION_JPEG &&
+ jpegcolormode == JPEGCOLORMODE_RGB)
+ photometric = PHOTOMETRIC_YCBCR;
+ break;
+ default:
+ BadPPM(infile);
+ }
+
+ /* Parse header */
+ while(1) {
+ if (feof(in))
+ BadPPM(infile);
+ c = fgetc(in);
+ /* Skip whitespaces (blanks, TABs, CRs, LFs) */
+ if (strchr(" \t\r\n", c))
+ continue;
+
+ /* Check for comment line */
+ if (c == '#') {
+ do {
+ c = fgetc(in);
+ } while(!(strchr("\r\n", c) || feof(in)));
+ continue;
+ }
+
+ ungetc(c, in);
+ break;
+ }
+ switch (bpp) {
+ case 1:
+ if (fscanf(in, " %u %u", &w, &h) != 2)
+ BadPPM(infile);
+ if (fgetc(in) != '\n')
+ BadPPM(infile);
+ break;
+ case 8:
+ if (fscanf(in, " %u %u %u", &w, &h, &prec) != 3)
+ BadPPM(infile);
+ if (fgetc(in) != '\n' || prec != 255)
+ BadPPM(infile);
+ break;
+ }
+ out = TIFFOpen(argv[optind], "w");
+ if (out == NULL)
+ return (-4);
+ TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w);
+ TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h);
+ TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+ TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
+ TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
+ TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+ TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
+ TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
+ switch (compression) {
+ case COMPRESSION_JPEG:
+ TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
+ TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
+ break;
+ case COMPRESSION_LZW:
+ case COMPRESSION_DEFLATE:
+ if (predictor != 0)
+ TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
+ break;
+ case COMPRESSION_CCITTFAX3:
+ TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);
+ break;
+ }
+ switch (bpp) {
+ case 1:
+ /* if round-up overflows, result will be zero, OK */
+ linebytes = (multiply_ms(spp, w) + (8 - 1)) / 8;
+ if (rowsperstrip == (uint32) -1) {
+ TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h);
+ } else {
+ TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
+ TIFFDefaultStripSize(out, rowsperstrip));
+ }
+ break;
+ case 8:
+ linebytes = multiply_ms(spp, w);
+ TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
+ TIFFDefaultStripSize(out, rowsperstrip));
+ break;
+ }
+ if (linebytes == 0) {
+ fprintf(stderr, "%s: scanline size overflow\n", infile);
+ (void) TIFFClose(out);
+ exit(-2);
+ }
+ scanline_size = TIFFScanlineSize(out);
+ if (scanline_size == 0) {
+ /* overflow - TIFFScanlineSize already printed a message */
+ (void) TIFFClose(out);
+ exit(-2);
+ }
+ if (scanline_size < linebytes)
+ buf = (unsigned char *)_TIFFmalloc(linebytes);
+ else
+ buf = (unsigned char *)_TIFFmalloc(scanline_size);
+ if (buf == NULL) {
+ fprintf(stderr, "%s: Not enough memory\n", infile);
+ (void) TIFFClose(out);
+ exit(-2);
+ }
+ if (resolution > 0) {
+ TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
+ TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
+ TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
+ }
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ for (row = 0; row < h; row++) {
+ if (fread(buf, linebytes, 1, in) != 1) {
+ fprintf(stderr, "%s: scanline %lu: Read error.\n",
+ infile, (unsigned long) row);
+ break;
+ }
+ if (TIFFWriteScanline(out, buf, row, 0) < 0)
+ break;
+ }
+#ifdef VERIMAG
+ clock_stop();
+ print_total_clock();
+#endif
+ (void) TIFFClose(out);
+ if (buf)
+ _TIFFfree(buf);
+ return (0);
+}
+
+static void
+processG3Options(char* cp)
+{
+ g3opts = 0;
+ if( (cp = strchr(cp, ':')) ) {
+ do {
+ cp++;
+ if (strneq(cp, "1d", 2))
+ g3opts &= ~GROUP3OPT_2DENCODING;
+ else if (strneq(cp, "2d", 2))
+ g3opts |= GROUP3OPT_2DENCODING;
+ else if (strneq(cp, "fill", 4))
+ g3opts |= GROUP3OPT_FILLBITS;
+ else
+ usage();
+ } while( (cp = strchr(cp, ':')) );
+ }
+}
+
+static int
+processCompressOptions(char* opt)
+{
+ if (streq(opt, "none"))
+ compression = COMPRESSION_NONE;
+ else if (streq(opt, "packbits"))
+ compression = COMPRESSION_PACKBITS;
+ else if (strneq(opt, "jpeg", 4)) {
+ char* cp = strchr(opt, ':');
+
+ compression = COMPRESSION_JPEG;
+ while (cp)
+ {
+ if (isdigit((int)cp[1]))
+ quality = atoi(cp+1);
+ else if (cp[1] == 'r' )
+ jpegcolormode = JPEGCOLORMODE_RAW;
+ else
+ usage();
+
+ cp = strchr(cp+1,':');
+ }
+ } else if (strneq(opt, "g3", 2)) {
+ processG3Options(opt);
+ compression = COMPRESSION_CCITTFAX3;
+ } else if (streq(opt, "g4")) {
+ compression = COMPRESSION_CCITTFAX4;
+ } else if (strneq(opt, "lzw", 3)) {
+ char* cp = strchr(opt, ':');
+ if (cp)
+ predictor = atoi(cp+1);
+ compression = COMPRESSION_LZW;
+ } else if (strneq(opt, "zip", 3)) {
+ char* cp = strchr(opt, ':');
+ if (cp)
+ predictor = atoi(cp+1);
+ compression = COMPRESSION_DEFLATE;
+ } else
+ return (0);
+ return (1);
+}
+
+char* stuff[] = {
+"usage: ppm2tiff [options] input.ppm output.tif",
+"where options are:",
+" -r # make each strip have no more than # rows",
+" -R # set x&y resolution (dpi)",
+"",
+" -c jpeg[:opts] compress output with JPEG encoding",
+" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
+" -c zip[:opts] compress output with deflate encoding",
+" -c packbits compress output with packbits encoding (the default)",
+" -c g3[:opts] compress output with CCITT Group 3 encoding",
+" -c g4 compress output with CCITT Group 4 encoding",
+" -c none use no compression algorithm on output",
+"",
+"JPEG options:",
+" # set compression quality level (0-100, default 75)",
+" r output color image as RGB rather than YCbCr",
+"LZW and deflate options:",
+" # set predictor value",
+"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
+NULL
+};
+
+static void
+usage(void)
+{
+ char buf[BUFSIZ];
+ int i;
+
+ setbuf(stderr, buf);
+ fprintf(stderr, "%s\n\n", TIFFGetVersion());
+ for (i = 0; stuff[i] != NULL; i++)
+ fprintf(stderr, "%s\n", stuff[i]);
+ exit(-1);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/t4.h b/test/monniaux/tiff-4.0.10/t4.h
new file mode 100644
index 00000000..fb0951a1
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/t4.h
@@ -0,0 +1,290 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _T4_
+#define _T4_
+/*
+ * CCITT T.4 1D Huffman runlength codes and
+ * related definitions. Given the small sizes
+ * of these tables it does not seem
+ * worthwhile to make code & length 8 bits.
+ */
+typedef struct tableentry {
+ unsigned short length; /* bit length of g3 code */
+ unsigned short code; /* g3 code */
+ short runlen; /* run length in bits */
+} tableentry;
+
+#define EOL 0x001 /* EOL code value - 0000 0000 0000 1 */
+
+/* status values returned instead of a run length */
+#define G3CODE_EOL -1 /* NB: ACT_EOL - ACT_WRUNT */
+#define G3CODE_INVALID -2 /* NB: ACT_INVALID - ACT_WRUNT */
+#define G3CODE_EOF -3 /* end of input data */
+#define G3CODE_INCOMP -4 /* incomplete run code */
+
+/*
+ * Note that these tables are ordered such that the
+ * index into the table is known to be either the
+ * run length, or (run length / 64) + a fixed offset.
+ *
+ * NB: The G3CODE_INVALID entries are only used
+ * during state generation (see mkg3states.c).
+ */
+#ifdef G3CODES
+const tableentry TIFFFaxWhiteCodes[] = {
+ { 8, 0x35, 0 }, /* 0011 0101 */
+ { 6, 0x7, 1 }, /* 0001 11 */
+ { 4, 0x7, 2 }, /* 0111 */
+ { 4, 0x8, 3 }, /* 1000 */
+ { 4, 0xB, 4 }, /* 1011 */
+ { 4, 0xC, 5 }, /* 1100 */
+ { 4, 0xE, 6 }, /* 1110 */
+ { 4, 0xF, 7 }, /* 1111 */
+ { 5, 0x13, 8 }, /* 1001 1 */
+ { 5, 0x14, 9 }, /* 1010 0 */
+ { 5, 0x7, 10 }, /* 0011 1 */
+ { 5, 0x8, 11 }, /* 0100 0 */
+ { 6, 0x8, 12 }, /* 0010 00 */
+ { 6, 0x3, 13 }, /* 0000 11 */
+ { 6, 0x34, 14 }, /* 1101 00 */
+ { 6, 0x35, 15 }, /* 1101 01 */
+ { 6, 0x2A, 16 }, /* 1010 10 */
+ { 6, 0x2B, 17 }, /* 1010 11 */
+ { 7, 0x27, 18 }, /* 0100 111 */
+ { 7, 0xC, 19 }, /* 0001 100 */
+ { 7, 0x8, 20 }, /* 0001 000 */
+ { 7, 0x17, 21 }, /* 0010 111 */
+ { 7, 0x3, 22 }, /* 0000 011 */
+ { 7, 0x4, 23 }, /* 0000 100 */
+ { 7, 0x28, 24 }, /* 0101 000 */
+ { 7, 0x2B, 25 }, /* 0101 011 */
+ { 7, 0x13, 26 }, /* 0010 011 */
+ { 7, 0x24, 27 }, /* 0100 100 */
+ { 7, 0x18, 28 }, /* 0011 000 */
+ { 8, 0x2, 29 }, /* 0000 0010 */
+ { 8, 0x3, 30 }, /* 0000 0011 */
+ { 8, 0x1A, 31 }, /* 0001 1010 */
+ { 8, 0x1B, 32 }, /* 0001 1011 */
+ { 8, 0x12, 33 }, /* 0001 0010 */
+ { 8, 0x13, 34 }, /* 0001 0011 */
+ { 8, 0x14, 35 }, /* 0001 0100 */
+ { 8, 0x15, 36 }, /* 0001 0101 */
+ { 8, 0x16, 37 }, /* 0001 0110 */
+ { 8, 0x17, 38 }, /* 0001 0111 */
+ { 8, 0x28, 39 }, /* 0010 1000 */
+ { 8, 0x29, 40 }, /* 0010 1001 */
+ { 8, 0x2A, 41 }, /* 0010 1010 */
+ { 8, 0x2B, 42 }, /* 0010 1011 */
+ { 8, 0x2C, 43 }, /* 0010 1100 */
+ { 8, 0x2D, 44 }, /* 0010 1101 */
+ { 8, 0x4, 45 }, /* 0000 0100 */
+ { 8, 0x5, 46 }, /* 0000 0101 */
+ { 8, 0xA, 47 }, /* 0000 1010 */
+ { 8, 0xB, 48 }, /* 0000 1011 */
+ { 8, 0x52, 49 }, /* 0101 0010 */
+ { 8, 0x53, 50 }, /* 0101 0011 */
+ { 8, 0x54, 51 }, /* 0101 0100 */
+ { 8, 0x55, 52 }, /* 0101 0101 */
+ { 8, 0x24, 53 }, /* 0010 0100 */
+ { 8, 0x25, 54 }, /* 0010 0101 */
+ { 8, 0x58, 55 }, /* 0101 1000 */
+ { 8, 0x59, 56 }, /* 0101 1001 */
+ { 8, 0x5A, 57 }, /* 0101 1010 */
+ { 8, 0x5B, 58 }, /* 0101 1011 */
+ { 8, 0x4A, 59 }, /* 0100 1010 */
+ { 8, 0x4B, 60 }, /* 0100 1011 */
+ { 8, 0x32, 61 }, /* 0011 0010 */
+ { 8, 0x33, 62 }, /* 0011 0011 */
+ { 8, 0x34, 63 }, /* 0011 0100 */
+ { 5, 0x1B, 64 }, /* 1101 1 */
+ { 5, 0x12, 128 }, /* 1001 0 */
+ { 6, 0x17, 192 }, /* 0101 11 */
+ { 7, 0x37, 256 }, /* 0110 111 */
+ { 8, 0x36, 320 }, /* 0011 0110 */
+ { 8, 0x37, 384 }, /* 0011 0111 */
+ { 8, 0x64, 448 }, /* 0110 0100 */
+ { 8, 0x65, 512 }, /* 0110 0101 */
+ { 8, 0x68, 576 }, /* 0110 1000 */
+ { 8, 0x67, 640 }, /* 0110 0111 */
+ { 9, 0xCC, 704 }, /* 0110 0110 0 */
+ { 9, 0xCD, 768 }, /* 0110 0110 1 */
+ { 9, 0xD2, 832 }, /* 0110 1001 0 */
+ { 9, 0xD3, 896 }, /* 0110 1001 1 */
+ { 9, 0xD4, 960 }, /* 0110 1010 0 */
+ { 9, 0xD5, 1024 }, /* 0110 1010 1 */
+ { 9, 0xD6, 1088 }, /* 0110 1011 0 */
+ { 9, 0xD7, 1152 }, /* 0110 1011 1 */
+ { 9, 0xD8, 1216 }, /* 0110 1100 0 */
+ { 9, 0xD9, 1280 }, /* 0110 1100 1 */
+ { 9, 0xDA, 1344 }, /* 0110 1101 0 */
+ { 9, 0xDB, 1408 }, /* 0110 1101 1 */
+ { 9, 0x98, 1472 }, /* 0100 1100 0 */
+ { 9, 0x99, 1536 }, /* 0100 1100 1 */
+ { 9, 0x9A, 1600 }, /* 0100 1101 0 */
+ { 6, 0x18, 1664 }, /* 0110 00 */
+ { 9, 0x9B, 1728 }, /* 0100 1101 1 */
+ { 11, 0x8, 1792 }, /* 0000 0001 000 */
+ { 11, 0xC, 1856 }, /* 0000 0001 100 */
+ { 11, 0xD, 1920 }, /* 0000 0001 101 */
+ { 12, 0x12, 1984 }, /* 0000 0001 0010 */
+ { 12, 0x13, 2048 }, /* 0000 0001 0011 */
+ { 12, 0x14, 2112 }, /* 0000 0001 0100 */
+ { 12, 0x15, 2176 }, /* 0000 0001 0101 */
+ { 12, 0x16, 2240 }, /* 0000 0001 0110 */
+ { 12, 0x17, 2304 }, /* 0000 0001 0111 */
+ { 12, 0x1C, 2368 }, /* 0000 0001 1100 */
+ { 12, 0x1D, 2432 }, /* 0000 0001 1101 */
+ { 12, 0x1E, 2496 }, /* 0000 0001 1110 */
+ { 12, 0x1F, 2560 }, /* 0000 0001 1111 */
+ { 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */
+ { 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */
+ { 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */
+ { 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */
+ { 12, 0x0, G3CODE_INVALID }, /* 0000 0000 0000 */
+};
+
+const tableentry TIFFFaxBlackCodes[] = {
+ { 10, 0x37, 0 }, /* 0000 1101 11 */
+ { 3, 0x2, 1 }, /* 010 */
+ { 2, 0x3, 2 }, /* 11 */
+ { 2, 0x2, 3 }, /* 10 */
+ { 3, 0x3, 4 }, /* 011 */
+ { 4, 0x3, 5 }, /* 0011 */
+ { 4, 0x2, 6 }, /* 0010 */
+ { 5, 0x3, 7 }, /* 0001 1 */
+ { 6, 0x5, 8 }, /* 0001 01 */
+ { 6, 0x4, 9 }, /* 0001 00 */
+ { 7, 0x4, 10 }, /* 0000 100 */
+ { 7, 0x5, 11 }, /* 0000 101 */
+ { 7, 0x7, 12 }, /* 0000 111 */
+ { 8, 0x4, 13 }, /* 0000 0100 */
+ { 8, 0x7, 14 }, /* 0000 0111 */
+ { 9, 0x18, 15 }, /* 0000 1100 0 */
+ { 10, 0x17, 16 }, /* 0000 0101 11 */
+ { 10, 0x18, 17 }, /* 0000 0110 00 */
+ { 10, 0x8, 18 }, /* 0000 0010 00 */
+ { 11, 0x67, 19 }, /* 0000 1100 111 */
+ { 11, 0x68, 20 }, /* 0000 1101 000 */
+ { 11, 0x6C, 21 }, /* 0000 1101 100 */
+ { 11, 0x37, 22 }, /* 0000 0110 111 */
+ { 11, 0x28, 23 }, /* 0000 0101 000 */
+ { 11, 0x17, 24 }, /* 0000 0010 111 */
+ { 11, 0x18, 25 }, /* 0000 0011 000 */
+ { 12, 0xCA, 26 }, /* 0000 1100 1010 */
+ { 12, 0xCB, 27 }, /* 0000 1100 1011 */
+ { 12, 0xCC, 28 }, /* 0000 1100 1100 */
+ { 12, 0xCD, 29 }, /* 0000 1100 1101 */
+ { 12, 0x68, 30 }, /* 0000 0110 1000 */
+ { 12, 0x69, 31 }, /* 0000 0110 1001 */
+ { 12, 0x6A, 32 }, /* 0000 0110 1010 */
+ { 12, 0x6B, 33 }, /* 0000 0110 1011 */
+ { 12, 0xD2, 34 }, /* 0000 1101 0010 */
+ { 12, 0xD3, 35 }, /* 0000 1101 0011 */
+ { 12, 0xD4, 36 }, /* 0000 1101 0100 */
+ { 12, 0xD5, 37 }, /* 0000 1101 0101 */
+ { 12, 0xD6, 38 }, /* 0000 1101 0110 */
+ { 12, 0xD7, 39 }, /* 0000 1101 0111 */
+ { 12, 0x6C, 40 }, /* 0000 0110 1100 */
+ { 12, 0x6D, 41 }, /* 0000 0110 1101 */
+ { 12, 0xDA, 42 }, /* 0000 1101 1010 */
+ { 12, 0xDB, 43 }, /* 0000 1101 1011 */
+ { 12, 0x54, 44 }, /* 0000 0101 0100 */
+ { 12, 0x55, 45 }, /* 0000 0101 0101 */
+ { 12, 0x56, 46 }, /* 0000 0101 0110 */
+ { 12, 0x57, 47 }, /* 0000 0101 0111 */
+ { 12, 0x64, 48 }, /* 0000 0110 0100 */
+ { 12, 0x65, 49 }, /* 0000 0110 0101 */
+ { 12, 0x52, 50 }, /* 0000 0101 0010 */
+ { 12, 0x53, 51 }, /* 0000 0101 0011 */
+ { 12, 0x24, 52 }, /* 0000 0010 0100 */
+ { 12, 0x37, 53 }, /* 0000 0011 0111 */
+ { 12, 0x38, 54 }, /* 0000 0011 1000 */
+ { 12, 0x27, 55 }, /* 0000 0010 0111 */
+ { 12, 0x28, 56 }, /* 0000 0010 1000 */
+ { 12, 0x58, 57 }, /* 0000 0101 1000 */
+ { 12, 0x59, 58 }, /* 0000 0101 1001 */
+ { 12, 0x2B, 59 }, /* 0000 0010 1011 */
+ { 12, 0x2C, 60 }, /* 0000 0010 1100 */
+ { 12, 0x5A, 61 }, /* 0000 0101 1010 */
+ { 12, 0x66, 62 }, /* 0000 0110 0110 */
+ { 12, 0x67, 63 }, /* 0000 0110 0111 */
+ { 10, 0xF, 64 }, /* 0000 0011 11 */
+ { 12, 0xC8, 128 }, /* 0000 1100 1000 */
+ { 12, 0xC9, 192 }, /* 0000 1100 1001 */
+ { 12, 0x5B, 256 }, /* 0000 0101 1011 */
+ { 12, 0x33, 320 }, /* 0000 0011 0011 */
+ { 12, 0x34, 384 }, /* 0000 0011 0100 */
+ { 12, 0x35, 448 }, /* 0000 0011 0101 */
+ { 13, 0x6C, 512 }, /* 0000 0011 0110 0 */
+ { 13, 0x6D, 576 }, /* 0000 0011 0110 1 */
+ { 13, 0x4A, 640 }, /* 0000 0010 0101 0 */
+ { 13, 0x4B, 704 }, /* 0000 0010 0101 1 */
+ { 13, 0x4C, 768 }, /* 0000 0010 0110 0 */
+ { 13, 0x4D, 832 }, /* 0000 0010 0110 1 */
+ { 13, 0x72, 896 }, /* 0000 0011 1001 0 */
+ { 13, 0x73, 960 }, /* 0000 0011 1001 1 */
+ { 13, 0x74, 1024 }, /* 0000 0011 1010 0 */
+ { 13, 0x75, 1088 }, /* 0000 0011 1010 1 */
+ { 13, 0x76, 1152 }, /* 0000 0011 1011 0 */
+ { 13, 0x77, 1216 }, /* 0000 0011 1011 1 */
+ { 13, 0x52, 1280 }, /* 0000 0010 1001 0 */
+ { 13, 0x53, 1344 }, /* 0000 0010 1001 1 */
+ { 13, 0x54, 1408 }, /* 0000 0010 1010 0 */
+ { 13, 0x55, 1472 }, /* 0000 0010 1010 1 */
+ { 13, 0x5A, 1536 }, /* 0000 0010 1101 0 */
+ { 13, 0x5B, 1600 }, /* 0000 0010 1101 1 */
+ { 13, 0x64, 1664 }, /* 0000 0011 0010 0 */
+ { 13, 0x65, 1728 }, /* 0000 0011 0010 1 */
+ { 11, 0x8, 1792 }, /* 0000 0001 000 */
+ { 11, 0xC, 1856 }, /* 0000 0001 100 */
+ { 11, 0xD, 1920 }, /* 0000 0001 101 */
+ { 12, 0x12, 1984 }, /* 0000 0001 0010 */
+ { 12, 0x13, 2048 }, /* 0000 0001 0011 */
+ { 12, 0x14, 2112 }, /* 0000 0001 0100 */
+ { 12, 0x15, 2176 }, /* 0000 0001 0101 */
+ { 12, 0x16, 2240 }, /* 0000 0001 0110 */
+ { 12, 0x17, 2304 }, /* 0000 0001 0111 */
+ { 12, 0x1C, 2368 }, /* 0000 0001 1100 */
+ { 12, 0x1D, 2432 }, /* 0000 0001 1101 */
+ { 12, 0x1E, 2496 }, /* 0000 0001 1110 */
+ { 12, 0x1F, 2560 }, /* 0000 0001 1111 */
+ { 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */
+ { 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */
+ { 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */
+ { 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */
+ { 12, 0x0, G3CODE_INVALID }, /* 0000 0000 0000 */
+};
+#else
+extern const tableentry TIFFFaxWhiteCodes[];
+extern const tableentry TIFFFaxBlackCodes[];
+#endif
+#endif /* _T4_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_aux.c b/test/monniaux/tiff-4.0.10/tif_aux.c
new file mode 100644
index 00000000..4ece162f
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_aux.c
@@ -0,0 +1,374 @@
+/*
+ * Copyright (c) 1991-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Auxiliary Support Routines.
+ */
+#include "tiffiop.h"
+#include "tif_predict.h"
+#include <math.h>
+
+uint32
+_TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
+{
+ uint32 bytes = first * second;
+
+ if (second && bytes / second != first) {
+ TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
+ bytes = 0;
+ }
+
+ return bytes;
+}
+
+uint64
+_TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
+{
+ uint64 bytes = first * second;
+
+ if (second && bytes / second != first) {
+ TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
+ bytes = 0;
+ }
+
+ return bytes;
+}
+
+void*
+_TIFFCheckRealloc(TIFF* tif, void* buffer,
+ tmsize_t nmemb, tmsize_t elem_size, const char* what)
+{
+ void* cp = NULL;
+ tmsize_t bytes = nmemb * elem_size;
+
+ /*
+ * XXX: Check for integer overflow.
+ */
+ if (nmemb && elem_size && bytes / elem_size == nmemb)
+ cp = _TIFFrealloc(buffer, bytes);
+
+ if (cp == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Failed to allocate memory for %s "
+ "(%ld elements of %ld bytes each)",
+ what,(long) nmemb, (long) elem_size);
+ }
+
+ return cp;
+}
+
+void*
+_TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
+{
+ return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
+}
+
+static int
+TIFFDefaultTransferFunction(TIFFDirectory* td)
+{
+ uint16 **tf = td->td_transferfunction;
+ tmsize_t i, n, nbytes;
+
+ tf[0] = tf[1] = tf[2] = 0;
+ if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
+ return 0;
+
+ n = ((tmsize_t)1)<<td->td_bitspersample;
+ nbytes = n * sizeof (uint16);
+ tf[0] = (uint16 *)_TIFFmalloc(nbytes);
+ if (tf[0] == NULL)
+ return 0;
+ tf[0][0] = 0;
+ for (i = 1; i < n; i++) {
+ double t = (double)i/((double) n-1.);
+ tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
+ }
+
+ if (td->td_samplesperpixel - td->td_extrasamples > 1) {
+ tf[1] = (uint16 *)_TIFFmalloc(nbytes);
+ if(tf[1] == NULL)
+ goto bad;
+ _TIFFmemcpy(tf[1], tf[0], nbytes);
+ tf[2] = (uint16 *)_TIFFmalloc(nbytes);
+ if (tf[2] == NULL)
+ goto bad;
+ _TIFFmemcpy(tf[2], tf[0], nbytes);
+ }
+ return 1;
+
+bad:
+ if (tf[0])
+ _TIFFfree(tf[0]);
+ if (tf[1])
+ _TIFFfree(tf[1]);
+ if (tf[2])
+ _TIFFfree(tf[2]);
+ tf[0] = tf[1] = tf[2] = 0;
+ return 0;
+}
+
+static int
+TIFFDefaultRefBlackWhite(TIFFDirectory* td)
+{
+ int i;
+
+ td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
+ if (td->td_refblackwhite == NULL)
+ return 0;
+ if (td->td_photometric == PHOTOMETRIC_YCBCR) {
+ /*
+ * YCbCr (Class Y) images must have the ReferenceBlackWhite
+ * tag set. Fix the broken images, which lacks that tag.
+ */
+ td->td_refblackwhite[0] = 0.0F;
+ td->td_refblackwhite[1] = td->td_refblackwhite[3] =
+ td->td_refblackwhite[5] = 255.0F;
+ td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
+ } else {
+ /*
+ * Assume RGB (Class R)
+ */
+ for (i = 0; i < 3; i++) {
+ td->td_refblackwhite[2*i+0] = 0;
+ td->td_refblackwhite[2*i+1] =
+ (float)((1L<<td->td_bitspersample)-1L);
+ }
+ }
+ return 1;
+}
+
+/*
+ * Like TIFFGetField, but return any default
+ * value if the tag is not present in the directory.
+ *
+ * NB: We use the value in the directory, rather than
+ * explicit values so that defaults exist only one
+ * place in the library -- in TIFFDefaultDirectory.
+ */
+int
+TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (TIFFVGetField(tif, tag, ap))
+ return (1);
+ switch (tag) {
+ case TIFFTAG_SUBFILETYPE:
+ *va_arg(ap, uint32 *) = td->td_subfiletype;
+ return (1);
+ case TIFFTAG_BITSPERSAMPLE:
+ *va_arg(ap, uint16 *) = td->td_bitspersample;
+ return (1);
+ case TIFFTAG_THRESHHOLDING:
+ *va_arg(ap, uint16 *) = td->td_threshholding;
+ return (1);
+ case TIFFTAG_FILLORDER:
+ *va_arg(ap, uint16 *) = td->td_fillorder;
+ return (1);
+ case TIFFTAG_ORIENTATION:
+ *va_arg(ap, uint16 *) = td->td_orientation;
+ return (1);
+ case TIFFTAG_SAMPLESPERPIXEL:
+ *va_arg(ap, uint16 *) = td->td_samplesperpixel;
+ return (1);
+ case TIFFTAG_ROWSPERSTRIP:
+ *va_arg(ap, uint32 *) = td->td_rowsperstrip;
+ return (1);
+ case TIFFTAG_MINSAMPLEVALUE:
+ *va_arg(ap, uint16 *) = td->td_minsamplevalue;
+ return (1);
+ case TIFFTAG_MAXSAMPLEVALUE:
+ *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
+ return (1);
+ case TIFFTAG_PLANARCONFIG:
+ *va_arg(ap, uint16 *) = td->td_planarconfig;
+ return (1);
+ case TIFFTAG_RESOLUTIONUNIT:
+ *va_arg(ap, uint16 *) = td->td_resolutionunit;
+ return (1);
+ case TIFFTAG_PREDICTOR:
+ {
+ TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
+ if( sp == NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Cannot get \"Predictor\" tag as plugin is not configured");
+ *va_arg(ap, uint16*) = 0;
+ return 0;
+ }
+ *va_arg(ap, uint16*) = (uint16) sp->predictor;
+ return 1;
+ }
+ case TIFFTAG_DOTRANGE:
+ *va_arg(ap, uint16 *) = 0;
+ *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
+ return (1);
+ case TIFFTAG_INKSET:
+ *va_arg(ap, uint16 *) = INKSET_CMYK;
+ return 1;
+ case TIFFTAG_NUMBEROFINKS:
+ *va_arg(ap, uint16 *) = 4;
+ return (1);
+ case TIFFTAG_EXTRASAMPLES:
+ *va_arg(ap, uint16 *) = td->td_extrasamples;
+ *va_arg(ap, uint16 **) = td->td_sampleinfo;
+ return (1);
+ case TIFFTAG_MATTEING:
+ *va_arg(ap, uint16 *) =
+ (td->td_extrasamples == 1 &&
+ td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
+ return (1);
+ case TIFFTAG_TILEDEPTH:
+ *va_arg(ap, uint32 *) = td->td_tiledepth;
+ return (1);
+ case TIFFTAG_DATATYPE:
+ *va_arg(ap, uint16 *) = td->td_sampleformat-1;
+ return (1);
+ case TIFFTAG_SAMPLEFORMAT:
+ *va_arg(ap, uint16 *) = td->td_sampleformat;
+ return(1);
+ case TIFFTAG_IMAGEDEPTH:
+ *va_arg(ap, uint32 *) = td->td_imagedepth;
+ return (1);
+ case TIFFTAG_YCBCRCOEFFICIENTS:
+ {
+ /* defaults are from CCIR Recommendation 601-1 */
+ static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
+ *va_arg(ap, float **) = ycbcrcoeffs;
+ return 1;
+ }
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
+ *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
+ return (1);
+ case TIFFTAG_YCBCRPOSITIONING:
+ *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
+ return (1);
+ case TIFFTAG_WHITEPOINT:
+ {
+ static float whitepoint[2];
+
+ /* TIFF 6.0 specification tells that it is no default
+ value for the WhitePoint, but AdobePhotoshop TIFF
+ Technical Note tells that it should be CIE D50. */
+ whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
+ whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
+ *va_arg(ap, float **) = whitepoint;
+ return 1;
+ }
+ case TIFFTAG_TRANSFERFUNCTION:
+ if (!td->td_transferfunction[0] &&
+ !TIFFDefaultTransferFunction(td)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
+ return (0);
+ }
+ *va_arg(ap, uint16 **) = td->td_transferfunction[0];
+ if (td->td_samplesperpixel - td->td_extrasamples > 1) {
+ *va_arg(ap, uint16 **) = td->td_transferfunction[1];
+ *va_arg(ap, uint16 **) = td->td_transferfunction[2];
+ }
+ return (1);
+ case TIFFTAG_REFERENCEBLACKWHITE:
+ if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
+ return (0);
+ *va_arg(ap, float **) = td->td_refblackwhite;
+ return (1);
+ }
+ return 0;
+}
+
+/*
+ * Like TIFFGetField, but return any default
+ * value if the tag is not present in the directory.
+ */
+int
+TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
+{
+ int ok;
+ va_list ap;
+
+ va_start(ap, tag);
+ ok = TIFFVGetFieldDefaulted(tif, tag, ap);
+ va_end(ap);
+ return (ok);
+}
+
+struct _Int64Parts {
+ int32 low, high;
+};
+
+typedef union {
+ struct _Int64Parts part;
+ int64 value;
+} _Int64;
+
+float
+_TIFFUInt64ToFloat(uint64 ui64)
+{
+ _Int64 i;
+
+ i.value = ui64;
+ if (i.part.high >= 0) {
+ return (float)i.value;
+ } else {
+ long double df;
+ df = (long double)i.value;
+ df += 18446744073709551616.0; /* adding 2**64 */
+ return (float)df;
+ }
+}
+
+double
+_TIFFUInt64ToDouble(uint64 ui64)
+{
+ _Int64 i;
+
+ i.value = ui64;
+ if (i.part.high >= 0) {
+ return (double)i.value;
+ } else {
+ long double df;
+ df = (long double)i.value;
+ df += 18446744073709551616.0; /* adding 2**64 */
+ return (double)df;
+ }
+}
+
+int _TIFFSeekOK(TIFF* tif, toff_t off)
+{
+ /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
+ /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
+ return off <= (~(uint64)0)/2 && TIFFSeekFile(tif,off,SEEK_SET)==off;
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_close.c b/test/monniaux/tiff-4.0.10/tif_close.c
new file mode 100644
index 00000000..e4228df9
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_close.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ */
+#include "tiffiop.h"
+#include <string.h>
+
+/************************************************************************/
+/* TIFFCleanup() */
+/************************************************************************/
+
+/**
+ * Auxiliary function to free the TIFF structure. Given structure will be
+ * completely freed, so you should save opened file handle and pointer
+ * to the close procedure in external variables before calling
+ * _TIFFCleanup(), if you will need these ones to close the file.
+ *
+ * @param tif A TIFF pointer.
+ */
+
+void
+TIFFCleanup(TIFF* tif)
+{
+ /*
+ * Flush buffered data and directory (if dirty).
+ */
+ if (tif->tif_mode != O_RDONLY)
+ TIFFFlush(tif);
+ (*tif->tif_cleanup)(tif);
+ TIFFFreeDirectory(tif);
+
+ if (tif->tif_dirlist)
+ _TIFFfree(tif->tif_dirlist);
+
+ /*
+ * Clean up client info links.
+ */
+ while( tif->tif_clientinfo )
+ {
+ TIFFClientInfoLink *psLink = tif->tif_clientinfo;
+
+ tif->tif_clientinfo = psLink->next;
+ _TIFFfree( psLink->name );
+ _TIFFfree( psLink );
+ }
+
+ if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
+ _TIFFfree(tif->tif_rawdata);
+ if (isMapped(tif))
+ TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
+
+ /*
+ * Clean up custom fields.
+ */
+ if (tif->tif_fields && tif->tif_nfields > 0) {
+ uint32 i;
+
+ for (i = 0; i < tif->tif_nfields; i++) {
+ TIFFField *fld = tif->tif_fields[i];
+ if (fld->field_bit == FIELD_CUSTOM &&
+ strncmp("Tag ", fld->field_name, 4) == 0) {
+ _TIFFfree(fld->field_name);
+ _TIFFfree(fld);
+ }
+ }
+
+ _TIFFfree(tif->tif_fields);
+ }
+
+ if (tif->tif_nfieldscompat > 0) {
+ uint32 i;
+
+ for (i = 0; i < tif->tif_nfieldscompat; i++) {
+ if (tif->tif_fieldscompat[i].allocated_size)
+ _TIFFfree(tif->tif_fieldscompat[i].fields);
+ }
+ _TIFFfree(tif->tif_fieldscompat);
+ }
+
+ _TIFFfree(tif);
+}
+
+/************************************************************************/
+/* TIFFClose() */
+/************************************************************************/
+
+/**
+ * Close a previously opened TIFF file.
+ *
+ * TIFFClose closes a file that was previously opened with TIFFOpen().
+ * Any buffered data are flushed to the file, including the contents of
+ * the current directory (if modified); and all resources are reclaimed.
+ *
+ * @param tif A TIFF pointer.
+ */
+
+void
+TIFFClose(TIFF* tif)
+{
+ TIFFCloseProc closeproc = tif->tif_closeproc;
+ thandle_t fd = tif->tif_clientdata;
+
+ TIFFCleanup(tif);
+ (void) (*closeproc)(fd);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_codec.c b/test/monniaux/tiff-4.0.10/tif_codec.c
new file mode 100644
index 00000000..b6c04f01
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_codec.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library
+ *
+ * Builtin Compression Scheme Configuration Support.
+ */
+#include "tiffiop.h"
+
+static int NotConfigured(TIFF*, int);
+
+#ifndef LZW_SUPPORT
+#define TIFFInitLZW NotConfigured
+#endif
+#ifndef PACKBITS_SUPPORT
+#define TIFFInitPackBits NotConfigured
+#endif
+#ifndef THUNDER_SUPPORT
+#define TIFFInitThunderScan NotConfigured
+#endif
+#ifndef NEXT_SUPPORT
+#define TIFFInitNeXT NotConfigured
+#endif
+#ifndef JPEG_SUPPORT
+#define TIFFInitJPEG NotConfigured
+#endif
+#ifndef OJPEG_SUPPORT
+#define TIFFInitOJPEG NotConfigured
+#endif
+#ifndef CCITT_SUPPORT
+#define TIFFInitCCITTRLE NotConfigured
+#define TIFFInitCCITTRLEW NotConfigured
+#define TIFFInitCCITTFax3 NotConfigured
+#define TIFFInitCCITTFax4 NotConfigured
+#endif
+#ifndef JBIG_SUPPORT
+#define TIFFInitJBIG NotConfigured
+#endif
+#ifndef ZIP_SUPPORT
+#define TIFFInitZIP NotConfigured
+#endif
+#ifndef PIXARLOG_SUPPORT
+#define TIFFInitPixarLog NotConfigured
+#endif
+#ifndef LOGLUV_SUPPORT
+#define TIFFInitSGILog NotConfigured
+#endif
+#ifndef LZMA_SUPPORT
+#define TIFFInitLZMA NotConfigured
+#endif
+#ifndef ZSTD_SUPPORT
+#define TIFFInitZSTD NotConfigured
+#endif
+#ifndef WEBP_SUPPORT
+#define TIFFInitWebP NotConfigured
+#endif
+
+/*
+ * Compression schemes statically built into the library.
+ */
+#ifdef VMS
+const TIFFCodec _TIFFBuiltinCODECS[] = {
+#else
+TIFFCodec _TIFFBuiltinCODECS[] = {
+#endif
+ { "None", COMPRESSION_NONE, TIFFInitDumpMode },
+ { "LZW", COMPRESSION_LZW, TIFFInitLZW },
+ { "PackBits", COMPRESSION_PACKBITS, TIFFInitPackBits },
+ { "ThunderScan", COMPRESSION_THUNDERSCAN,TIFFInitThunderScan },
+ { "NeXT", COMPRESSION_NEXT, TIFFInitNeXT },
+ { "JPEG", COMPRESSION_JPEG, TIFFInitJPEG },
+ { "Old-style JPEG", COMPRESSION_OJPEG, TIFFInitOJPEG },
+ { "CCITT RLE", COMPRESSION_CCITTRLE, TIFFInitCCITTRLE },
+ { "CCITT RLE/W", COMPRESSION_CCITTRLEW, TIFFInitCCITTRLEW },
+ { "CCITT Group 3", COMPRESSION_CCITTFAX3, TIFFInitCCITTFax3 },
+ { "CCITT Group 4", COMPRESSION_CCITTFAX4, TIFFInitCCITTFax4 },
+ { "ISO JBIG", COMPRESSION_JBIG, TIFFInitJBIG },
+ { "Deflate", COMPRESSION_DEFLATE, TIFFInitZIP },
+ { "AdobeDeflate", COMPRESSION_ADOBE_DEFLATE , TIFFInitZIP },
+ { "PixarLog", COMPRESSION_PIXARLOG, TIFFInitPixarLog },
+ { "SGILog", COMPRESSION_SGILOG, TIFFInitSGILog },
+ { "SGILog24", COMPRESSION_SGILOG24, TIFFInitSGILog },
+ { "LZMA", COMPRESSION_LZMA, TIFFInitLZMA },
+ { "ZSTD", COMPRESSION_ZSTD, TIFFInitZSTD },
+ { "WEBP", COMPRESSION_WEBP, TIFFInitWebP },
+ { NULL, 0, NULL }
+};
+
+static int
+_notConfigured(TIFF* tif)
+{
+ const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
+ char compression_code[20];
+
+ sprintf(compression_code, "%d",tif->tif_dir.td_compression );
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%s compression support is not configured",
+ c ? c->name : compression_code );
+ return (0);
+}
+
+static int
+NotConfigured(TIFF* tif, int scheme)
+{
+ (void) scheme;
+
+ tif->tif_fixuptags = _notConfigured;
+ tif->tif_decodestatus = FALSE;
+ tif->tif_setupdecode = _notConfigured;
+ tif->tif_encodestatus = FALSE;
+ tif->tif_setupencode = _notConfigured;
+ return (1);
+}
+
+/************************************************************************/
+/* TIFFIsCODECConfigured() */
+/************************************************************************/
+
+/**
+ * Check whether we have working codec for the specific coding scheme.
+ *
+ * @return returns 1 if the codec is configured and working. Otherwise
+ * 0 will be returned.
+ */
+
+int
+TIFFIsCODECConfigured(uint16 scheme)
+{
+ const TIFFCodec* codec = TIFFFindCODEC(scheme);
+
+ if(codec == NULL) {
+ return 0;
+ }
+ if(codec->init == NULL) {
+ return 0;
+ }
+ if(codec->init != NotConfigured){
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_color.c b/test/monniaux/tiff-4.0.10/tif_color.c
new file mode 100644
index 00000000..8fae40ea
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_color.c
@@ -0,0 +1,307 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * CIE L*a*b* to CIE XYZ and CIE XYZ to RGB conversion routines are taken
+ * from the VIPS library (http://www.vips.ecs.soton.ac.uk) with
+ * the permission of John Cupitt, the VIPS author.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Color space conversion routines.
+ */
+
+#include "tiffiop.h"
+#include <math.h>
+
+/*
+ * Convert color value from the CIE L*a*b* 1976 space to CIE XYZ.
+ */
+void
+TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32 l, int32 a, int32 b,
+ float *X, float *Y, float *Z)
+{
+ float L = (float)l * 100.0F / 255.0F;
+ float cby, tmp;
+
+ if( L < 8.856F ) {
+ *Y = (L * cielab->Y0) / 903.292F;
+ cby = 7.787F * (*Y / cielab->Y0) + 16.0F / 116.0F;
+ } else {
+ cby = (L + 16.0F) / 116.0F;
+ *Y = cielab->Y0 * cby * cby * cby;
+ }
+
+ tmp = (float)a / 500.0F + cby;
+ if( tmp < 0.2069F )
+ *X = cielab->X0 * (tmp - 0.13793F) / 7.787F;
+ else
+ *X = cielab->X0 * tmp * tmp * tmp;
+
+ tmp = cby - (float)b / 200.0F;
+ if( tmp < 0.2069F )
+ *Z = cielab->Z0 * (tmp - 0.13793F) / 7.787F;
+ else
+ *Z = cielab->Z0 * tmp * tmp * tmp;
+}
+
+#define RINT(R) ((uint32)((R)>0?((R)+0.5):((R)-0.5)))
+/*
+ * Convert color value from the XYZ space to RGB.
+ */
+void
+TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z,
+ uint32 *r, uint32 *g, uint32 *b)
+{
+ int i;
+ float Yr, Yg, Yb;
+ float *matrix = &cielab->display.d_mat[0][0];
+
+ /* Multiply through the matrix to get luminosity values. */
+ Yr = matrix[0] * X + matrix[1] * Y + matrix[2] * Z;
+ Yg = matrix[3] * X + matrix[4] * Y + matrix[5] * Z;
+ Yb = matrix[6] * X + matrix[7] * Y + matrix[8] * Z;
+
+ /* Clip input */
+ Yr = TIFFmax(Yr, cielab->display.d_Y0R);
+ Yg = TIFFmax(Yg, cielab->display.d_Y0G);
+ Yb = TIFFmax(Yb, cielab->display.d_Y0B);
+
+ /* Avoid overflow in case of wrong input values */
+ Yr = TIFFmin(Yr, cielab->display.d_YCR);
+ Yg = TIFFmin(Yg, cielab->display.d_YCG);
+ Yb = TIFFmin(Yb, cielab->display.d_YCB);
+
+ /* Turn luminosity to colour value. */
+ i = (int)((Yr - cielab->display.d_Y0R) / cielab->rstep);
+ i = TIFFmin(cielab->range, i);
+ *r = RINT(cielab->Yr2r[i]);
+
+ i = (int)((Yg - cielab->display.d_Y0G) / cielab->gstep);
+ i = TIFFmin(cielab->range, i);
+ *g = RINT(cielab->Yg2g[i]);
+
+ i = (int)((Yb - cielab->display.d_Y0B) / cielab->bstep);
+ i = TIFFmin(cielab->range, i);
+ *b = RINT(cielab->Yb2b[i]);
+
+ /* Clip output. */
+ *r = TIFFmin(*r, cielab->display.d_Vrwr);
+ *g = TIFFmin(*g, cielab->display.d_Vrwg);
+ *b = TIFFmin(*b, cielab->display.d_Vrwb);
+}
+#undef RINT
+
+/*
+ * Allocate conversion state structures and make look_up tables for
+ * the Yr,Yb,Yg <=> r,g,b conversions.
+ */
+int
+TIFFCIELabToRGBInit(TIFFCIELabToRGB* cielab,
+ const TIFFDisplay *display, float *refWhite)
+{
+ int i;
+ double dfGamma;
+
+ cielab->range = CIELABTORGB_TABLE_RANGE;
+
+ _TIFFmemcpy(&cielab->display, display, sizeof(TIFFDisplay));
+
+ /* Red */
+ dfGamma = 1.0 / cielab->display.d_gammaR ;
+ cielab->rstep =
+ (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
+ for(i = 0; i <= cielab->range; i++) {
+ cielab->Yr2r[i] = cielab->display.d_Vrwr
+ * ((float)pow((double)i / cielab->range, dfGamma));
+ }
+
+ /* Green */
+ dfGamma = 1.0 / cielab->display.d_gammaG ;
+ cielab->gstep =
+ (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
+ for(i = 0; i <= cielab->range; i++) {
+ cielab->Yg2g[i] = cielab->display.d_Vrwg
+ * ((float)pow((double)i / cielab->range, dfGamma));
+ }
+
+ /* Blue */
+ dfGamma = 1.0 / cielab->display.d_gammaB ;
+ cielab->bstep =
+ (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
+ for(i = 0; i <= cielab->range; i++) {
+ cielab->Yb2b[i] = cielab->display.d_Vrwb
+ * ((float)pow((double)i / cielab->range, dfGamma));
+ }
+
+ /* Init reference white point */
+ cielab->X0 = refWhite[0];
+ cielab->Y0 = refWhite[1];
+ cielab->Z0 = refWhite[2];
+
+ return 0;
+}
+
+/*
+ * Convert color value from the YCbCr space to RGB.
+ * The colorspace conversion algorithm comes from the IJG v5a code;
+ * see below for more information on how it works.
+ */
+#define SHIFT 16
+#define FIX(x) ((int32)((x) * (1L<<SHIFT) + 0.5))
+#define ONE_HALF ((int32)(1<<(SHIFT-1)))
+#define Code2V(c, RB, RW, CR) ((((c)-(int32)(RB))*(float)(CR))/(float)(((RW)-(RB)!=0) ? ((RW)-(RB)) : 1))
+#define CLAMP(f,min,max) ((f)<(min)?(min):(f)>(max)?(max):(f))
+#define HICLAMP(f,max) ((f)>(max)?(max):(f))
+
+void
+TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32 Y, int32 Cb, int32 Cr,
+ uint32 *r, uint32 *g, uint32 *b)
+{
+ int32 i;
+
+ /* XXX: Only 8-bit YCbCr input supported for now */
+ Y = HICLAMP(Y, 255);
+ Cb = CLAMP(Cb, 0, 255);
+ Cr = CLAMP(Cr, 0, 255);
+
+ i = ycbcr->Y_tab[Y] + ycbcr->Cr_r_tab[Cr];
+ *r = CLAMP(i, 0, 255);
+ i = ycbcr->Y_tab[Y]
+ + (int)((ycbcr->Cb_g_tab[Cb] + ycbcr->Cr_g_tab[Cr]) >> SHIFT);
+ *g = CLAMP(i, 0, 255);
+ i = ycbcr->Y_tab[Y] + ycbcr->Cb_b_tab[Cb];
+ *b = CLAMP(i, 0, 255);
+}
+
+/* Clamp function for sanitization purposes. Normally clamping should not */
+/* occur for well behaved chroma and refBlackWhite coefficients */
+static float CLAMPw(float v, float vmin, float vmax)
+{
+ if( v < vmin )
+ {
+ /* printf("%f clamped to %f\n", v, vmin); */
+ return vmin;
+ }
+ if( v > vmax )
+ {
+ /* printf("%f clamped to %f\n", v, vmax); */
+ return vmax;
+ }
+ return v;
+}
+
+/*
+ * Initialize the YCbCr->RGB conversion tables. The conversion
+ * is done according to the 6.0 spec:
+ *
+ * R = Y + Cr*(2 - 2*LumaRed)
+ * B = Y + Cb*(2 - 2*LumaBlue)
+ * G = Y
+ * - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
+ * - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
+ *
+ * To avoid floating point arithmetic the fractional constants that
+ * come out of the equations are represented as fixed point values
+ * in the range 0...2^16. We also eliminate multiplications by
+ * pre-calculating possible values indexed by Cb and Cr (this code
+ * assumes conversion is being done for 8-bit samples).
+ */
+int
+TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, float *luma, float *refBlackWhite)
+{
+ TIFFRGBValue* clamptab;
+ int i;
+
+#define LumaRed luma[0]
+#define LumaGreen luma[1]
+#define LumaBlue luma[2]
+
+ clamptab = (TIFFRGBValue*)(
+ (uint8*) ycbcr+TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long)));
+ _TIFFmemset(clamptab, 0, 256); /* v < 0 => 0 */
+ ycbcr->clamptab = (clamptab += 256);
+ for (i = 0; i < 256; i++)
+ clamptab[i] = (TIFFRGBValue) i;
+ _TIFFmemset(clamptab+256, 255, 2*256); /* v > 255 => 255 */
+ ycbcr->Cr_r_tab = (int*) (clamptab + 3*256);
+ ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256;
+ ycbcr->Cr_g_tab = (int32*) (ycbcr->Cb_b_tab + 256);
+ ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256;
+ ycbcr->Y_tab = ycbcr->Cb_g_tab + 256;
+
+ { float f1 = 2-2*LumaRed; int32 D1 = FIX(CLAMP(f1,0.0F,2.0F));
+ float f2 = LumaRed*f1/LumaGreen; int32 D2 = -FIX(CLAMP(f2,0.0F,2.0F));
+ float f3 = 2-2*LumaBlue; int32 D3 = FIX(CLAMP(f3,0.0F,2.0F));
+ float f4 = LumaBlue*f3/LumaGreen; int32 D4 = -FIX(CLAMP(f4,0.0F,2.0F));
+ int x;
+
+#undef LumaBlue
+#undef LumaGreen
+#undef LumaRed
+
+ /*
+ * i is the actual input pixel value in the range 0..255
+ * Cb and Cr values are in the range -128..127 (actually
+ * they are in a range defined by the ReferenceBlackWhite
+ * tag) so there is some range shifting to do here when
+ * constructing tables indexed by the raw pixel data.
+ */
+ for (i = 0, x = -128; i < 256; i++, x++) {
+ int32 Cr = (int32)CLAMPw(Code2V(x, refBlackWhite[4] - 128.0F,
+ refBlackWhite[5] - 128.0F, 127),
+ -128.0F * 32, 128.0F * 32);
+ int32 Cb = (int32)CLAMPw(Code2V(x, refBlackWhite[2] - 128.0F,
+ refBlackWhite[3] - 128.0F, 127),
+ -128.0F * 32, 128.0F * 32);
+
+ ycbcr->Cr_r_tab[i] = (int32)((D1*Cr + ONE_HALF)>>SHIFT);
+ ycbcr->Cb_b_tab[i] = (int32)((D3*Cb + ONE_HALF)>>SHIFT);
+ ycbcr->Cr_g_tab[i] = D2*Cr;
+ ycbcr->Cb_g_tab[i] = D4*Cb + ONE_HALF;
+ ycbcr->Y_tab[i] =
+ (int32)CLAMPw(Code2V(x + 128, refBlackWhite[0], refBlackWhite[1], 255),
+ -128.0F * 32, 128.0F * 32);
+ }
+ }
+
+ return 0;
+}
+#undef HICLAMP
+#undef CLAMP
+#undef Code2V
+#undef SHIFT
+#undef ONE_HALF
+#undef FIX
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_compress.c b/test/monniaux/tiff-4.0.10/tif_compress.c
new file mode 100644
index 00000000..8130ef08
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_compress.c
@@ -0,0 +1,302 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library
+ *
+ * Compression Scheme Configuration Support.
+ */
+#include "tiffiop.h"
+
+static int
+TIFFNoEncode(TIFF* tif, const char* method)
+{
+ const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
+
+ if (c) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%s %s encoding is not implemented",
+ c->name, method);
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Compression scheme %u %s encoding is not implemented",
+ tif->tif_dir.td_compression, method);
+ }
+ return (-1);
+}
+
+int
+_TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoEncode(tif, "scanline"));
+}
+
+int
+_TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoEncode(tif, "strip"));
+}
+
+int
+_TIFFNoTileEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoEncode(tif, "tile"));
+}
+
+static int
+TIFFNoDecode(TIFF* tif, const char* method)
+{
+ const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
+
+ if (c)
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%s %s decoding is not implemented",
+ c->name, method);
+ else
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Compression scheme %u %s decoding is not implemented",
+ tif->tif_dir.td_compression, method);
+ return (0);
+}
+
+static int
+_TIFFNoFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+int
+_TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoDecode(tif, "scanline"));
+}
+
+int
+_TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoDecode(tif, "strip"));
+}
+
+int
+_TIFFNoTileDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) pp; (void) cc; (void) s;
+ return (TIFFNoDecode(tif, "tile"));
+}
+
+int
+_TIFFNoSeek(TIFF* tif, uint32 off)
+{
+ (void) off;
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Compression algorithm does not support random access");
+ return (0);
+}
+
+int
+_TIFFNoPreCode(TIFF* tif, uint16 s)
+{
+ (void) tif; (void) s;
+ return (1);
+}
+
+static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
+static void _TIFFvoid(TIFF* tif) { (void) tif; }
+
+void
+_TIFFSetDefaultCompressionState(TIFF* tif)
+{
+ tif->tif_fixuptags = _TIFFNoFixupTags;
+ tif->tif_decodestatus = TRUE;
+ tif->tif_setupdecode = _TIFFtrue;
+ tif->tif_predecode = _TIFFNoPreCode;
+ tif->tif_decoderow = _TIFFNoRowDecode;
+ tif->tif_decodestrip = _TIFFNoStripDecode;
+ tif->tif_decodetile = _TIFFNoTileDecode;
+ tif->tif_encodestatus = TRUE;
+ tif->tif_setupencode = _TIFFtrue;
+ tif->tif_preencode = _TIFFNoPreCode;
+ tif->tif_postencode = _TIFFtrue;
+ tif->tif_encoderow = _TIFFNoRowEncode;
+ tif->tif_encodestrip = _TIFFNoStripEncode;
+ tif->tif_encodetile = _TIFFNoTileEncode;
+ tif->tif_close = _TIFFvoid;
+ tif->tif_seek = _TIFFNoSeek;
+ tif->tif_cleanup = _TIFFvoid;
+ tif->tif_defstripsize = _TIFFDefaultStripSize;
+ tif->tif_deftilesize = _TIFFDefaultTileSize;
+ tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
+}
+
+int
+TIFFSetCompressionScheme(TIFF* tif, int scheme)
+{
+ const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
+
+ _TIFFSetDefaultCompressionState(tif);
+ /*
+ * Don't treat an unknown compression scheme as an error.
+ * This permits applications to open files with data that
+ * the library does not have builtin support for, but which
+ * may still be meaningful.
+ */
+ return (c ? (*c->init)(tif, scheme) : 1);
+}
+
+/*
+ * Other compression schemes may be registered. Registered
+ * schemes can also override the builtin versions provided
+ * by this library.
+ */
+typedef struct _codec {
+ struct _codec* next;
+ TIFFCodec* info;
+} codec_t;
+static codec_t* registeredCODECS = NULL;
+
+const TIFFCodec*
+TIFFFindCODEC(uint16 scheme)
+{
+ const TIFFCodec* c;
+ codec_t* cd;
+
+ for (cd = registeredCODECS; cd; cd = cd->next)
+ if (cd->info->scheme == scheme)
+ return ((const TIFFCodec*) cd->info);
+ for (c = _TIFFBuiltinCODECS; c->name; c++)
+ if (c->scheme == scheme)
+ return (c);
+ return ((const TIFFCodec*) 0);
+}
+
+TIFFCodec*
+TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
+{
+ codec_t* cd = (codec_t*)
+ _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
+
+ if (cd != NULL) {
+ cd->info = (TIFFCodec*) ((uint8*) cd + sizeof (codec_t));
+ cd->info->name = (char*)
+ ((uint8*) cd->info + sizeof (TIFFCodec));
+ strcpy(cd->info->name, name);
+ cd->info->scheme = scheme;
+ cd->info->init = init;
+ cd->next = registeredCODECS;
+ registeredCODECS = cd;
+ } else {
+ TIFFErrorExt(0, "TIFFRegisterCODEC",
+ "No space to register compression scheme %s", name);
+ return NULL;
+ }
+ return (cd->info);
+}
+
+void
+TIFFUnRegisterCODEC(TIFFCodec* c)
+{
+ codec_t* cd;
+ codec_t** pcd;
+
+ for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
+ if (cd->info == c) {
+ *pcd = cd->next;
+ _TIFFfree(cd);
+ return;
+ }
+ TIFFErrorExt(0, "TIFFUnRegisterCODEC",
+ "Cannot remove compression scheme %s; not registered", c->name);
+}
+
+/************************************************************************/
+/* TIFFGetConfisuredCODECs() */
+/************************************************************************/
+
+/**
+ * Get list of configured codecs, both built-in and registered by user.
+ * Caller is responsible to free this structure.
+ *
+ * @return returns array of TIFFCodec records (the last record should be NULL)
+ * or NULL if function failed.
+ */
+
+TIFFCodec*
+TIFFGetConfiguredCODECs()
+{
+ int i = 1;
+ codec_t *cd;
+ const TIFFCodec* c;
+ TIFFCodec* codecs = NULL;
+ TIFFCodec* new_codecs;
+
+ for (cd = registeredCODECS; cd; cd = cd->next) {
+ new_codecs = (TIFFCodec *)
+ _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+ if (!new_codecs) {
+ _TIFFfree (codecs);
+ return NULL;
+ }
+ codecs = new_codecs;
+ _TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
+ i++;
+ }
+ for (c = _TIFFBuiltinCODECS; c->name; c++) {
+ if (TIFFIsCODECConfigured(c->scheme)) {
+ new_codecs = (TIFFCodec *)
+ _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+ if (!new_codecs) {
+ _TIFFfree (codecs);
+ return NULL;
+ }
+ codecs = new_codecs;
+ _TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
+ i++;
+ }
+ }
+
+ new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+ if (!new_codecs) {
+ _TIFFfree (codecs);
+ return NULL;
+ }
+ codecs = new_codecs;
+ _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
+
+ return codecs;
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_config.h b/test/monniaux/tiff-4.0.10/tif_config.h
new file mode 100644
index 00000000..ade947e6
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_config.h
@@ -0,0 +1,369 @@
+/* libtiff/tif_config.h. Generated from tif_config.h.in by configure. */
+/* libtiff/tif_config.h.in. Generated from configure.ac by autoheader. */
+
+/* Define if building universal (internal helper macro) */
+/* #undef AC_APPLE_UNIVERSAL_BUILD */
+
+/* Support CCITT Group 3 & 4 algorithms */
+#define CCITT_SUPPORT 1
+
+/* Pick up YCbCr subsampling info from the JPEG data stream to support files
+ lacking the tag (default enabled). */
+#define CHECK_JPEG_YCBCR_SUBSAMPLING 1
+
+/* enable partial strip reading for large strips (experimental) */
+/* #undef CHUNKY_STRIP_READ_SUPPORT */
+
+/* Support C++ stream API (requires C++ compiler) */
+/* #undef CXX_SUPPORT */
+
+/* Treat extra sample as alpha (default enabled). The RGBA interface will
+ treat a fourth sample with no EXTRASAMPLE_ value as being ASSOCALPHA. Many
+ packages produce RGBA files but don't mark the alpha properly. */
+#define DEFAULT_EXTRASAMPLE_AS_ALPHA 1
+
+/* enable deferred strip/tile offset/size loading (experimental) */
+/* #undef DEFER_STRILE_LOAD */
+
+/* Define to 1 if you have the <assert.h> header file. */
+#define HAVE_ASSERT_H 1
+
+/* Define to 1 if you have the declaration of `optarg', and to 0 if you don't.
+ */
+#define HAVE_DECL_OPTARG 1
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+/* #undef HAVE_DLFCN_H */
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#define HAVE_FCNTL_H 1
+
+/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
+/* #undef HAVE_FSEEKO */
+
+/* Define to 1 if you have the `getopt' function. */
+#define HAVE_GETOPT 1
+
+/* Define to 1 if you have the <GLUT/glut.h> header file. */
+/* #undef HAVE_GLUT_GLUT_H */
+
+/* Define to 1 if you have the <GL/glut.h> header file. */
+/* #undef HAVE_GL_GLUT_H */
+
+/* Define to 1 if you have the <GL/glu.h> header file. */
+/* #undef HAVE_GL_GLU_H */
+
+/* Define to 1 if you have the <GL/gl.h> header file. */
+/* #undef HAVE_GL_GL_H */
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* Define to 1 if you have the <io.h> header file. */
+/* #undef HAVE_IO_H */
+
+/* Define to 1 if you have the `jbg_newlen' function. */
+/* #undef HAVE_JBG_NEWLEN */
+
+/* Define to 1 if you have the `lfind' function. */
+/* #undef HAVE_LFIND */
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `mmap' function. */
+/* #undef HAVE_MMAP */
+
+/* Define to 1 if you have the <OpenGL/glu.h> header file. */
+/* #undef HAVE_OPENGL_GLU_H */
+
+/* Define to 1 if you have the <OpenGL/gl.h> header file. */
+/* #undef HAVE_OPENGL_GL_H */
+
+/* Define if you have POSIX threads libraries and header files. */
+/* #undef HAVE_PTHREAD */
+
+/* Define to 1 if you have the <search.h> header file. */
+#define HAVE_SEARCH_H 1
+
+/* Define to 1 if you have the `setmode' function. */
+/* #undef HAVE_SETMODE */
+
+/* Define to 1 if you have the `snprintf' function. */
+#define HAVE_SNPRINTF 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the `strcasecmp' function. */
+#define HAVE_STRCASECMP 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the `strtol' function. */
+#define HAVE_STRTOL 1
+
+/* Define to 1 if you have the `strtoll' function. */
+#define HAVE_STRTOLL 1
+
+/* Define to 1 if you have the `strtoul' function. */
+#define HAVE_STRTOUL 1
+
+/* Define to 1 if you have the `strtoull' function. */
+#define HAVE_STRTOULL 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Use nonstandard varargs form for the GLU tesselator callback */
+/* #undef HAVE_VARARGS_GLU_TESSCB */
+
+/* Define to 1 if you have the <windows.h> header file. */
+/* #undef HAVE_WINDOWS_H */
+
+/* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
+ (Intel) */
+#define HOST_BIGENDIAN 0
+
+/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
+#define HOST_FILLORDER FILLORDER_MSB2LSB
+
+/* Support ISO JBIG compression (requires JBIG-KIT library) */
+/* #undef JBIG_SUPPORT */
+
+/* 8/12 bit libjpeg dual mode enabled */
+/* #undef JPEG_DUAL_MODE_8_12 */
+
+/* Support JPEG compression (requires IJG JPEG library) */
+/* #undef JPEG_SUPPORT */
+
+/* 12bit libjpeg primary include file with path */
+/* #undef LIBJPEG_12_PATH */
+
+/* Support LogLuv high dynamic range encoding */
+#define LOGLUV_SUPPORT 1
+
+/* Define to the sub-directory where libtool stores uninstalled libraries. */
+#define LT_OBJDIR ".libs/"
+
+/* Support LZMA2 compression */
+/* #undef LZMA_SUPPORT */
+
+/* Support LZW algorithm */
+#define LZW_SUPPORT 1
+
+/* Support Microsoft Document Imaging format */
+#define MDI_SUPPORT 1
+
+/* Support NeXT 2-bit RLE algorithm */
+#define NEXT_SUPPORT 1
+
+/* Support Old JPEG compresson (read-only) */
+/* #undef OJPEG_SUPPORT */
+
+/* Name of package */
+#define PACKAGE "tiff"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "tiff@lists.maptools.org"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "LibTIFF Software"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "LibTIFF Software 4.0.10"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "tiff"
+
+/* Define to the home page for this package. */
+#define PACKAGE_URL ""
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "4.0.10"
+
+/* Support Macintosh PackBits algorithm */
+#define PACKBITS_SUPPORT 1
+
+/* Support Pixar log-format algorithm (requires Zlib) */
+/* #undef PIXARLOG_SUPPORT */
+
+/* Define to necessary symbol if this constant uses a non-standard name on
+ your system. */
+/* #undef PTHREAD_CREATE_JOINABLE */
+
+/* The size of `signed int', as computed by sizeof. */
+#define SIZEOF_SIGNED_INT 4
+
+/* The size of `signed long', as computed by sizeof. */
+#define SIZEOF_SIGNED_LONG 8
+
+/* The size of `signed long long', as computed by sizeof. */
+#define SIZEOF_SIGNED_LONG_LONG 8
+
+/* The size of `size_t', as computed by sizeof. */
+#define SIZEOF_SIZE_T 8
+
+/* The size of `unsigned char *', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_CHAR_P 8
+
+/* The size of `unsigned int', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_INT 4
+
+/* The size of `unsigned long', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_LONG 8
+
+/* The size of `unsigned long long', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_LONG_LONG 8
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Support strip chopping (whether or not to convert single-strip uncompressed
+ images to mutiple strips of specified size to reduce memory usage) */
+#define STRIPCHOP_DEFAULT TIFF_STRIPCHOP
+
+/* Default size of the strip in bytes (when strip chopping enabled) */
+#define STRIP_SIZE_DEFAULT 8192
+
+/* Enable SubIFD tag (330) support */
+#define SUBIFD_SUPPORT 1
+
+/* Support ThunderScan 4-bit RLE algorithm */
+#define THUNDER_SUPPORT 1
+
+/* Signed 16-bit type */
+#define TIFF_INT16_T signed short
+
+/* Signed 32-bit type formatter */
+#define TIFF_INT32_FORMAT "%d"
+
+/* Signed 32-bit type */
+#define TIFF_INT32_T signed int
+
+/* Signed 64-bit type formatter */
+#define TIFF_INT64_FORMAT "%ld"
+
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed long
+
+/* Signed 8-bit type */
+#define TIFF_INT8_T signed char
+
+/* Pointer difference type formatter */
+#define TIFF_PTRDIFF_FORMAT "%ld"
+
+/* Pointer difference type */
+#define TIFF_PTRDIFF_T ptrdiff_t
+
+/* Size type formatter */
+#define TIFF_SIZE_FORMAT "%lu"
+
+/* Unsigned size type */
+#define TIFF_SIZE_T unsigned long
+
+/* Signed size type formatter */
+#define TIFF_SSIZE_FORMAT "%ld"
+
+/* Signed size type */
+#define TIFF_SSIZE_T signed long
+
+/* Unsigned 16-bit type */
+#define TIFF_UINT16_T unsigned short
+
+/* Unsigned 32-bit type formatter */
+#define TIFF_UINT32_FORMAT "%u"
+
+/* Unsigned 32-bit type */
+#define TIFF_UINT32_T unsigned int
+
+/* Unsigned 64-bit type formatter */
+#define TIFF_UINT64_FORMAT "%lu"
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned long
+
+/* Unsigned 8-bit type */
+#define TIFF_UINT8_T unsigned char
+
+/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#define TIME_WITH_SYS_TIME 1
+
+/* Define to 1 if your <sys/time.h> declares `struct tm'. */
+/* #undef TM_IN_SYS_TIME */
+
+/* define to use win32 IO system */
+/* #undef USE_WIN32_FILEIO */
+
+/* Version number of package */
+#define VERSION "4.0.10"
+
+/* Support webp compression */
+/* #undef WEBP_SUPPORT */
+
+/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
+ significant byte first (like Motorola and SPARC, unlike Intel). */
+#if defined AC_APPLE_UNIVERSAL_BUILD
+# if defined __BIG_ENDIAN__
+# define WORDS_BIGENDIAN 1
+# endif
+#else
+# ifndef WORDS_BIGENDIAN
+/* # undef WORDS_BIGENDIAN */
+# endif
+#endif
+
+/* Define to 1 if the X Window System is missing or not being used. */
+#define X_DISPLAY_MISSING 1
+
+/* Support Deflate compression */
+/* #undef ZIP_SUPPORT */
+
+/* Support zstd compression */
+/* #undef ZSTD_SUPPORT */
+
+/* Enable large inode numbers on Mac OS X 10.5. */
+#ifndef _DARWIN_USE_64_BIT_INODE
+# define _DARWIN_USE_64_BIT_INODE 1
+#endif
+
+/* Number of bits in a file offset, on hosts where this is settable. */
+/* #undef _FILE_OFFSET_BITS */
+
+/* Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */
+/* #undef _LARGEFILE_SOURCE */
+
+/* Define for large files, on AIX-style hosts. */
+/* #undef _LARGE_FILES */
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+/* #undef inline */
+#endif
+
+/* Define to `long int' if <sys/types.h> does not define. */
+/* #undef off_t */
+
+/* Define to `unsigned int' if <sys/types.h> does not define. */
+/* #undef size_t */
diff --git a/test/monniaux/tiff-4.0.10/tif_config.vc.h b/test/monniaux/tiff-4.0.10/tif_config.vc.h
new file mode 100644
index 00000000..5cebfa02
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_config.vc.h
@@ -0,0 +1,137 @@
+#ifndef _TIF_CONFIG_H_
+#define _TIF_CONFIG_H_
+
+/* Define to 1 if you have the <assert.h> header file. */
+#define HAVE_ASSERT_H 1
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#define HAVE_FCNTL_H 1
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Define to 1 if you have the `jbg_newlen' function. */
+#define HAVE_JBG_NEWLEN 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <io.h> header file. */
+#define HAVE_IO_H 1
+
+/* Define to 1 if you have the <search.h> header file. */
+#define HAVE_SEARCH_H 1
+
+/* Define to 1 if you have the `setmode' function. */
+#define HAVE_SETMODE 1
+
+/* Define to 1 if you have the declaration of `optarg', and to 0 if you don't. */
+#define HAVE_DECL_OPTARG 0
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* The size of a `long', as computed by sizeof. */
+#define SIZEOF_LONG 4
+
+/* Signed 64-bit type formatter */
+#define TIFF_INT64_FORMAT "%I64d"
+
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed __int64
+
+/* Unsigned 64-bit type formatter */
+#define TIFF_UINT64_FORMAT "%I64u"
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned __int64
+
+#if _WIN64
+/*
+ Windows 64-bit build
+*/
+
+/* Pointer difference type */
+# define TIFF_PTRDIFF_T TIFF_INT64_T
+
+/* The size of `size_t', as computed by sizeof. */
+# define SIZEOF_SIZE_T 8
+
+/* Size type formatter */
+# define TIFF_SIZE_FORMAT TIFF_INT64_FORMAT
+
+/* Unsigned size type */
+# define TIFF_SIZE_T TIFF_UINT64_T
+
+/* Signed size type formatter */
+# define TIFF_SSIZE_FORMAT TIFF_INT64_FORMAT
+
+/* Signed size type */
+# define TIFF_SSIZE_T TIFF_INT64_T
+
+#else
+/*
+ Windows 32-bit build
+*/
+
+/* Pointer difference type */
+# define TIFF_PTRDIFF_T signed int
+
+/* The size of `size_t', as computed by sizeof. */
+# define SIZEOF_SIZE_T 4
+
+/* Size type formatter */
+# define TIFF_SIZE_FORMAT "%u"
+
+/* Size type formatter */
+# define TIFF_SIZE_FORMAT "%u"
+
+/* Unsigned size type */
+# define TIFF_SIZE_T unsigned int
+
+/* Signed size type formatter */
+# define TIFF_SSIZE_FORMAT "%d"
+
+/* Signed size type */
+# define TIFF_SSIZE_T signed int
+
+#endif
+
+/* Set the native cpu bit order */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
+
+/* Visual Studio 2015 / VC 14 / MSVC 19.00 finally has snprintf() */
+#if defined(_MSC_VER) && _MSC_VER < 1900
+#define snprintf _snprintf
+#else
+#define HAVE_SNPRINTF 1
+#endif
+
+/* Define to 1 if your processor stores words with the most significant byte
+ first (like Motorola and SPARC, unlike Intel and VAX). */
+/* #undef WORDS_BIGENDIAN */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+# ifndef inline
+# define inline __inline
+# endif
+#endif
+
+#define lfind _lfind
+
+#pragma warning(disable : 4996) /* function deprecation warnings */
+
+#endif /* _TIF_CONFIG_H_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_config.wince.h b/test/monniaux/tiff-4.0.10/tif_config.wince.h
new file mode 100644
index 00000000..e85e2e62
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_config.wince.h
@@ -0,0 +1,69 @@
+/*
+ * TIFF library configuration header for Windows CE platform.
+ */
+#ifndef _WIN32_WCE
+# error This version of tif_config.h header is dedicated for Windows CE platform!
+#endif
+
+/* Define to 1 if you have the <assert.h> header file. */
+#define HAVE_ASSERT_H 1
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#define HAVE_FCNTL_H 1
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Define to 1 if you have the `jbg_newlen' function. */
+#define HAVE_JBG_NEWLEN 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#undef HAVE_SYS_TYPES_H
+
+/* Define to 1 if you have the <io.h> header file. */
+#define HAVE_IO_H 1
+
+/* Define to 1 if you have the <search.h> header file. */
+#define HAVE_SEARCH_H 1
+
+/* Define to 1 if you have the `setmode' function. */
+#define HAVE_SETMODE 1
+
+/* Define to 1 if you have the `bsearch' function. */
+#define HAVE_BSEARCH 1
+#define bsearch wceex_bsearch
+
+/* Define to 1 if you have the `lfind' function. */
+#define HAVE_LFIND 1
+#define lfind wceex_lfind
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* Set the native cpu bit order */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
+
+/* Define to 1 if your processor stores words with the most significant byte
+ first (like Motorola and SPARC, unlike Intel and VAX). */
+/* #undef WORDS_BIGENDIAN */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+# ifndef inline
+# define inline __inline
+# endif
+#endif
+
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dir.c b/test/monniaux/tiff-4.0.10/tif_dir.c
new file mode 100644
index 00000000..6f0b4879
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dir.c
@@ -0,0 +1,1768 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Directory Tag Get & Set Routines.
+ * (and also some miscellaneous stuff)
+ */
+#include "tiffiop.h"
+#include <float.h>
+
+/*
+ * These are used in the backwards compatibility code...
+ */
+#define DATATYPE_VOID 0 /* !untyped data */
+#define DATATYPE_INT 1 /* !signed integer data */
+#define DATATYPE_UINT 2 /* !unsigned integer data */
+#define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
+
+static void
+setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
+{
+ if (*vpp) {
+ _TIFFfree(*vpp);
+ *vpp = 0;
+ }
+ if (vp) {
+ tmsize_t bytes = (tmsize_t)(nmemb * elem_size);
+ if (elem_size && bytes / elem_size == nmemb)
+ *vpp = (void*) _TIFFmalloc(bytes);
+ if (*vpp)
+ _TIFFmemcpy(*vpp, vp, bytes);
+ }
+}
+void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
+ { setByteArray(vpp, vp, n, 1); }
+void _TIFFsetString(char** cpp, char* cp)
+ { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
+static void _TIFFsetNString(char** cpp, char* cp, uint32 n)
+ { setByteArray((void**) cpp, (void*) cp, n, 1); }
+void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
+ { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
+void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
+ { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
+static void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
+ { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
+void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
+ { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
+void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
+ { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
+
+static void
+setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
+{
+ if (*vpp)
+ _TIFFfree(*vpp);
+ *vpp = _TIFFmalloc(nmemb*sizeof(double));
+ if (*vpp)
+ {
+ while (nmemb--)
+ ((double*)*vpp)[nmemb] = value;
+ }
+}
+
+/*
+ * Install extra samples information.
+ */
+static int
+setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
+{
+/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
+#define EXTRASAMPLE_COREL_UNASSALPHA 999
+
+ uint16* va;
+ uint32 i;
+
+ *v = (uint16) va_arg(ap, uint16_vap);
+ if ((uint16) *v > td->td_samplesperpixel)
+ return 0;
+ va = va_arg(ap, uint16*);
+ if (*v > 0 && va == NULL) /* typically missing param */
+ return 0;
+ for (i = 0; i < *v; i++) {
+ if (va[i] > EXTRASAMPLE_UNASSALPHA) {
+ /*
+ * XXX: Corel Draw is known to produce incorrect
+ * ExtraSamples tags which must be patched here if we
+ * want to be able to open some of the damaged TIFF
+ * files:
+ */
+ if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
+ va[i] = EXTRASAMPLE_UNASSALPHA;
+ else
+ return 0;
+ }
+ }
+ td->td_extrasamples = (uint16) *v;
+ _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
+ return 1;
+
+#undef EXTRASAMPLE_COREL_UNASSALPHA
+}
+
+/*
+ * Confirm we have "samplesperpixel" ink names separated by \0. Returns
+ * zero if the ink names are not as expected.
+ */
+static uint32
+checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
+{
+ TIFFDirectory* td = &tif->tif_dir;
+ uint16 i = td->td_samplesperpixel;
+
+ if (slen > 0) {
+ const char* ep = s+slen;
+ const char* cp = s;
+ for (; i > 0; i--) {
+ for (; cp < ep && *cp != '\0'; cp++) {}
+ if (cp >= ep)
+ goto bad;
+ cp++; /* skip \0 */
+ }
+ return ((uint32)(cp-s));
+ }
+bad:
+ TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
+ "%s: Invalid InkNames value; expecting %d names, found %d",
+ tif->tif_name,
+ td->td_samplesperpixel,
+ td->td_samplesperpixel-i);
+ return (0);
+}
+
+static float TIFFClampDoubleToFloat( double val )
+{
+ if( val > FLT_MAX )
+ return FLT_MAX;
+ if( val < -FLT_MAX )
+ return -FLT_MAX;
+ return (float)val;
+}
+
+static int
+_TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "_TIFFVSetField";
+
+ TIFFDirectory* td = &tif->tif_dir;
+ int status = 1;
+ uint32 v32, i, v;
+ double dblval;
+ char* s;
+ const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
+ uint32 standard_tag = tag;
+ if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
+ return 0;
+ /*
+ * We want to force the custom code to be used for custom
+ * fields even if the tag happens to match a well known
+ * one - important for reinterpreted handling of standard
+ * tag values in custom directories (i.e. EXIF)
+ */
+ if (fip->field_bit == FIELD_CUSTOM) {
+ standard_tag = 0;
+ }
+
+ switch (standard_tag) {
+ case TIFFTAG_SUBFILETYPE:
+ td->td_subfiletype = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_IMAGEWIDTH:
+ td->td_imagewidth = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_IMAGELENGTH:
+ td->td_imagelength = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_BITSPERSAMPLE:
+ td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
+ /*
+ * If the data require post-decoding processing to byte-swap
+ * samples, set it up here. Note that since tags are required
+ * to be ordered, compression code can override this behaviour
+ * in the setup method if it wants to roll the post decoding
+ * work in with its normal work.
+ */
+ if (tif->tif_flags & TIFF_SWAB) {
+ if (td->td_bitspersample == 8)
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ else if (td->td_bitspersample == 16)
+ tif->tif_postdecode = _TIFFSwab16BitData;
+ else if (td->td_bitspersample == 24)
+ tif->tif_postdecode = _TIFFSwab24BitData;
+ else if (td->td_bitspersample == 32)
+ tif->tif_postdecode = _TIFFSwab32BitData;
+ else if (td->td_bitspersample == 64)
+ tif->tif_postdecode = _TIFFSwab64BitData;
+ else if (td->td_bitspersample == 128) /* two 64's */
+ tif->tif_postdecode = _TIFFSwab64BitData;
+ }
+ break;
+ case TIFFTAG_COMPRESSION:
+ v = (uint16) va_arg(ap, uint16_vap);
+ /*
+ * If we're changing the compression scheme, the notify the
+ * previous module so that it can cleanup any state it's
+ * setup.
+ */
+ if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
+ if ((uint32)td->td_compression == v)
+ break;
+ (*tif->tif_cleanup)(tif);
+ tif->tif_flags &= ~TIFF_CODERSETUP;
+ }
+ /*
+ * Setup new compression routine state.
+ */
+ if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
+ td->td_compression = (uint16) v;
+ else
+ status = 0;
+ break;
+ case TIFFTAG_PHOTOMETRIC:
+ td->td_photometric = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_THRESHHOLDING:
+ td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_FILLORDER:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
+ goto badvalue;
+ td->td_fillorder = (uint16) v;
+ break;
+ case TIFFTAG_ORIENTATION:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
+ goto badvalue;
+ else
+ td->td_orientation = (uint16) v;
+ break;
+ case TIFFTAG_SAMPLESPERPIXEL:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v == 0)
+ goto badvalue;
+ if( v != td->td_samplesperpixel )
+ {
+ /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
+ if( td->td_sminsamplevalue != NULL )
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "SamplesPerPixel tag value is changing, "
+ "but SMinSampleValue tag was read with a different value. Cancelling it");
+ TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
+ _TIFFfree(td->td_sminsamplevalue);
+ td->td_sminsamplevalue = NULL;
+ }
+ if( td->td_smaxsamplevalue != NULL )
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "SamplesPerPixel tag value is changing, "
+ "but SMaxSampleValue tag was read with a different value. Cancelling it");
+ TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
+ _TIFFfree(td->td_smaxsamplevalue);
+ td->td_smaxsamplevalue = NULL;
+ }
+ }
+ td->td_samplesperpixel = (uint16) v;
+ break;
+ case TIFFTAG_ROWSPERSTRIP:
+ v32 = (uint32) va_arg(ap, uint32);
+ if (v32 == 0)
+ goto badvalue32;
+ td->td_rowsperstrip = v32;
+ if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
+ td->td_tilelength = v32;
+ td->td_tilewidth = td->td_imagewidth;
+ }
+ break;
+ case TIFFTAG_MINSAMPLEVALUE:
+ td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_MAXSAMPLEVALUE:
+ td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_SMINSAMPLEVALUE:
+ if (tif->tif_flags & TIFF_PERSAMPLE)
+ _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
+ else
+ setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
+ break;
+ case TIFFTAG_SMAXSAMPLEVALUE:
+ if (tif->tif_flags & TIFF_PERSAMPLE)
+ _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
+ else
+ setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
+ break;
+ case TIFFTAG_XRESOLUTION:
+ dblval = va_arg(ap, double);
+ if( dblval < 0 )
+ goto badvaluedouble;
+ td->td_xresolution = TIFFClampDoubleToFloat( dblval );
+ break;
+ case TIFFTAG_YRESOLUTION:
+ dblval = va_arg(ap, double);
+ if( dblval < 0 )
+ goto badvaluedouble;
+ td->td_yresolution = TIFFClampDoubleToFloat( dblval );
+ break;
+ case TIFFTAG_PLANARCONFIG:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
+ goto badvalue;
+ td->td_planarconfig = (uint16) v;
+ break;
+ case TIFFTAG_XPOSITION:
+ td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
+ break;
+ case TIFFTAG_YPOSITION:
+ td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
+ break;
+ case TIFFTAG_RESOLUTIONUNIT:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
+ goto badvalue;
+ td->td_resolutionunit = (uint16) v;
+ break;
+ case TIFFTAG_PAGENUMBER:
+ td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
+ td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_HALFTONEHINTS:
+ td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
+ td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_COLORMAP:
+ v32 = (uint32)(1L<<td->td_bitspersample);
+ _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
+ _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
+ _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
+ break;
+ case TIFFTAG_EXTRASAMPLES:
+ if (!setExtraSamples(td, ap, &v))
+ goto badvalue;
+ break;
+ case TIFFTAG_MATTEING:
+ td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
+ if (td->td_extrasamples) {
+ uint16 sv = EXTRASAMPLE_ASSOCALPHA;
+ _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
+ }
+ break;
+ case TIFFTAG_TILEWIDTH:
+ v32 = (uint32) va_arg(ap, uint32);
+ if (v32 % 16) {
+ if (tif->tif_mode != O_RDONLY)
+ goto badvalue32;
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "Nonstandard tile width %d, convert file", v32);
+ }
+ td->td_tilewidth = v32;
+ tif->tif_flags |= TIFF_ISTILED;
+ break;
+ case TIFFTAG_TILELENGTH:
+ v32 = (uint32) va_arg(ap, uint32);
+ if (v32 % 16) {
+ if (tif->tif_mode != O_RDONLY)
+ goto badvalue32;
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "Nonstandard tile length %d, convert file", v32);
+ }
+ td->td_tilelength = v32;
+ tif->tif_flags |= TIFF_ISTILED;
+ break;
+ case TIFFTAG_TILEDEPTH:
+ v32 = (uint32) va_arg(ap, uint32);
+ if (v32 == 0)
+ goto badvalue32;
+ td->td_tiledepth = v32;
+ break;
+ case TIFFTAG_DATATYPE:
+ v = (uint16) va_arg(ap, uint16_vap);
+ switch (v) {
+ case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
+ case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
+ case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
+ case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
+ default: goto badvalue;
+ }
+ td->td_sampleformat = (uint16) v;
+ break;
+ case TIFFTAG_SAMPLEFORMAT:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
+ goto badvalue;
+ td->td_sampleformat = (uint16) v;
+
+ /* Try to fix up the SWAB function for complex data. */
+ if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
+ && td->td_bitspersample == 32
+ && tif->tif_postdecode == _TIFFSwab32BitData )
+ tif->tif_postdecode = _TIFFSwab16BitData;
+ else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
+ || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
+ && td->td_bitspersample == 64
+ && tif->tif_postdecode == _TIFFSwab64BitData )
+ tif->tif_postdecode = _TIFFSwab32BitData;
+ break;
+ case TIFFTAG_IMAGEDEPTH:
+ td->td_imagedepth = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_SUBIFD:
+ if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
+ td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
+ _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
+ (uint32) td->td_nsubifd);
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Sorry, cannot nest SubIFDs",
+ tif->tif_name);
+ status = 0;
+ }
+ break;
+ case TIFFTAG_YCBCRPOSITIONING:
+ td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
+ td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_TRANSFERFUNCTION:
+ v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
+ for (i = 0; i < v; i++)
+ _TIFFsetShortArray(&td->td_transferfunction[i],
+ va_arg(ap, uint16*), 1U<<td->td_bitspersample);
+ break;
+ case TIFFTAG_REFERENCEBLACKWHITE:
+ /* XXX should check for null range */
+ _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
+ break;
+ case TIFFTAG_INKNAMES:
+ v = (uint16) va_arg(ap, uint16_vap);
+ s = va_arg(ap, char*);
+ v = checkInkNamesString(tif, v, s);
+ status = v > 0;
+ if( v > 0 ) {
+ _TIFFsetNString(&td->td_inknames, s, v);
+ td->td_inknameslen = v;
+ }
+ break;
+ case TIFFTAG_PERSAMPLE:
+ v = (uint16) va_arg(ap, uint16_vap);
+ if( v == PERSAMPLE_MULTI )
+ tif->tif_flags |= TIFF_PERSAMPLE;
+ else
+ tif->tif_flags &= ~TIFF_PERSAMPLE;
+ break;
+ default: {
+ TIFFTagValue *tv;
+ int tv_size, iCustom;
+
+ /*
+ * This can happen if multiple images are open with different
+ * codecs which have private tags. The global tag information
+ * table may then have tags that are valid for one file but not
+ * the other. If the client tries to set a tag that is not valid
+ * for the image's codec then we'll arrive here. This
+ * happens, for example, when tiffcp is used to convert between
+ * compression schemes and codec-specific tags are blindly copied.
+ */
+ if(fip->field_bit != FIELD_CUSTOM) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Invalid %stag \"%s\" (not supported by codec)",
+ tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
+ fip->field_name);
+ status = 0;
+ break;
+ }
+
+ /*
+ * Find the existing entry for this custom value.
+ */
+ tv = NULL;
+ for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
+ if (td->td_customValues[iCustom].info->field_tag == tag) {
+ tv = td->td_customValues + iCustom;
+ if (tv->value != NULL) {
+ _TIFFfree(tv->value);
+ tv->value = NULL;
+ }
+ break;
+ }
+ }
+
+ /*
+ * Grow the custom list if the entry was not found.
+ */
+ if(tv == NULL) {
+ TIFFTagValue *new_customValues;
+
+ td->td_customValueCount++;
+ new_customValues = (TIFFTagValue *)
+ _TIFFrealloc(td->td_customValues,
+ sizeof(TIFFTagValue) * td->td_customValueCount);
+ if (!new_customValues) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Failed to allocate space for list of custom values",
+ tif->tif_name);
+ status = 0;
+ goto end;
+ }
+
+ td->td_customValues = new_customValues;
+
+ tv = td->td_customValues + (td->td_customValueCount - 1);
+ tv->info = fip;
+ tv->value = NULL;
+ tv->count = 0;
+ }
+
+ /*
+ * Set custom value ... save a copy of the custom tag value.
+ */
+ tv_size = _TIFFDataSize(fip->field_type);
+ if (tv_size == 0) {
+ status = 0;
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad field type %d for \"%s\"",
+ tif->tif_name, fip->field_type,
+ fip->field_name);
+ goto end;
+ }
+
+ if (fip->field_type == TIFF_ASCII)
+ {
+ uint32 ma;
+ char* mb;
+ if (fip->field_passcount)
+ {
+ assert(fip->field_writecount==TIFF_VARIABLE2);
+ ma=(uint32)va_arg(ap,uint32);
+ mb=(char*)va_arg(ap,char*);
+ }
+ else
+ {
+ mb=(char*)va_arg(ap,char*);
+ ma=(uint32)(strlen(mb)+1);
+ }
+ tv->count=ma;
+ setByteArray(&tv->value,mb,ma,1);
+ }
+ else
+ {
+ if (fip->field_passcount) {
+ if (fip->field_writecount == TIFF_VARIABLE2)
+ tv->count = (uint32) va_arg(ap, uint32);
+ else
+ tv->count = (int) va_arg(ap, int);
+ } else if (fip->field_writecount == TIFF_VARIABLE
+ || fip->field_writecount == TIFF_VARIABLE2)
+ tv->count = 1;
+ else if (fip->field_writecount == TIFF_SPP)
+ tv->count = td->td_samplesperpixel;
+ else
+ tv->count = fip->field_writecount;
+
+ if (tv->count == 0) {
+ status = 0;
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Null count for \"%s\" (type "
+ "%d, writecount %d, passcount %d)",
+ tif->tif_name,
+ fip->field_name,
+ fip->field_type,
+ fip->field_writecount,
+ fip->field_passcount);
+ goto end;
+ }
+
+ tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
+ "custom tag binary object");
+ if (!tv->value) {
+ status = 0;
+ goto end;
+ }
+
+ if (fip->field_tag == TIFFTAG_DOTRANGE
+ && strcmp(fip->field_name,"DotRange") == 0) {
+ /* TODO: This is an evil exception and should not have been
+ handled this way ... likely best if we move it into
+ the directory structure with an explicit field in
+ libtiff 4.1 and assign it a FIELD_ value */
+ uint16 v2[2];
+ v2[0] = (uint16)va_arg(ap, int);
+ v2[1] = (uint16)va_arg(ap, int);
+ _TIFFmemcpy(tv->value, &v2, 4);
+ }
+
+ else if (fip->field_passcount
+ || fip->field_writecount == TIFF_VARIABLE
+ || fip->field_writecount == TIFF_VARIABLE2
+ || fip->field_writecount == TIFF_SPP
+ || tv->count > 1) {
+ _TIFFmemcpy(tv->value, va_arg(ap, void *),
+ tv->count * tv_size);
+ } else {
+ char *val = (char *)tv->value;
+ assert( tv->count == 1 );
+
+ switch (fip->field_type) {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ {
+ uint8 v2 = (uint8)va_arg(ap, int);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8 v2 = (int8)va_arg(ap, int);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16 v2 = (uint16)va_arg(ap, int);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16 v2 = (int16)va_arg(ap, int);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_LONG:
+ case TIFF_IFD:
+ {
+ uint32 v2 = va_arg(ap, uint32);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32 v2 = va_arg(ap, int32);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_LONG8:
+ case TIFF_IFD8:
+ {
+ uint64 v2 = va_arg(ap, uint64);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64 v2 = va_arg(ap, int64);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ {
+ float v2 = TIFFClampDoubleToFloat(va_arg(ap, double));
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ case TIFF_DOUBLE:
+ {
+ double v2 = va_arg(ap, double);
+ _TIFFmemcpy(val, &v2, tv_size);
+ }
+ break;
+ default:
+ _TIFFmemset(val, 0, tv_size);
+ status = 0;
+ break;
+ }
+ }
+ }
+ }
+ }
+ if (status) {
+ const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
+ if (fip2)
+ TIFFSetFieldBit(tif, fip2->field_bit);
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+ }
+
+end:
+ va_end(ap);
+ return (status);
+badvalue:
+ {
+ const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad value %u for \"%s\" tag",
+ tif->tif_name, v,
+ fip2 ? fip2->field_name : "Unknown");
+ va_end(ap);
+ }
+ return (0);
+badvalue32:
+ {
+ const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad value %u for \"%s\" tag",
+ tif->tif_name, v32,
+ fip2 ? fip2->field_name : "Unknown");
+ va_end(ap);
+ }
+ return (0);
+badvaluedouble:
+ {
+ const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad value %f for \"%s\" tag",
+ tif->tif_name, dblval,
+ fip2 ? fip2->field_name : "Unknown");
+ va_end(ap);
+ }
+ return (0);
+}
+
+/*
+ * Return 1/0 according to whether or not
+ * it is permissible to set the tag's value.
+ * Note that we allow ImageLength to be changed
+ * so that we can append and extend to images.
+ * Any other tag may not be altered once writing
+ * has commenced, unless its value has no effect
+ * on the format of the data that is written.
+ */
+static int
+OkToChangeTag(TIFF* tif, uint32 tag)
+{
+ const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
+ if (!fip) { /* unknown tag */
+ TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
+ tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
+ return (0);
+ }
+ if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
+ !fip->field_oktochange) {
+ /*
+ * Consult info table to see if tag can be changed
+ * after we've started writing. We only allow changes
+ * to those tags that don't/shouldn't affect the
+ * compression and/or format of the data.
+ */
+ TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
+ "%s: Cannot modify tag \"%s\" while writing",
+ tif->tif_name, fip->field_name);
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * Record the value of a field in the
+ * internal directory structure. The
+ * field will be written to the file
+ * when/if the directory structure is
+ * updated.
+ */
+int
+TIFFSetField(TIFF* tif, uint32 tag, ...)
+{
+ va_list ap;
+ int status;
+
+ va_start(ap, tag);
+ status = TIFFVSetField(tif, tag, ap);
+ va_end(ap);
+ return (status);
+}
+
+/*
+ * Clear the contents of the field in the internal structure.
+ */
+int
+TIFFUnsetField(TIFF* tif, uint32 tag)
+{
+ const TIFFField *fip = TIFFFieldWithTag(tif, tag);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ if( !fip )
+ return 0;
+
+ if( fip->field_bit != FIELD_CUSTOM )
+ TIFFClrFieldBit(tif, fip->field_bit);
+ else
+ {
+ TIFFTagValue *tv = NULL;
+ int i;
+
+ for (i = 0; i < td->td_customValueCount; i++) {
+
+ tv = td->td_customValues + i;
+ if( tv->info->field_tag == tag )
+ break;
+ }
+
+ if( i < td->td_customValueCount )
+ {
+ _TIFFfree(tv->value);
+ for( ; i < td->td_customValueCount-1; i++) {
+ td->td_customValues[i] = td->td_customValues[i+1];
+ }
+ td->td_customValueCount--;
+ }
+ }
+
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+
+ return (1);
+}
+
+/*
+ * Like TIFFSetField, but taking a varargs
+ * parameter list. This routine is useful
+ * for building higher-level interfaces on
+ * top of the library.
+ */
+int
+TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ return OkToChangeTag(tif, tag) ?
+ (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
+}
+
+static int
+_TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ TIFFDirectory* td = &tif->tif_dir;
+ int ret_val = 1;
+ uint32 standard_tag = tag;
+ const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
+ if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
+ return 0;
+
+ /*
+ * We want to force the custom code to be used for custom
+ * fields even if the tag happens to match a well known
+ * one - important for reinterpreted handling of standard
+ * tag values in custom directories (i.e. EXIF)
+ */
+ if (fip->field_bit == FIELD_CUSTOM) {
+ standard_tag = 0;
+ }
+
+ if( standard_tag == TIFFTAG_NUMBEROFINKS )
+ {
+ int i;
+ for (i = 0; i < td->td_customValueCount; i++) {
+ uint16 val;
+ TIFFTagValue *tv = td->td_customValues + i;
+ if (tv->info->field_tag != standard_tag)
+ continue;
+ if( tv->value == NULL )
+ return 0;
+ val = *(uint16 *)tv->value;
+ /* Truncate to SamplesPerPixel, since the */
+ /* setting code for INKNAMES assume that there are SamplesPerPixel */
+ /* inknames. */
+ /* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
+ if( val > td->td_samplesperpixel )
+ {
+ TIFFWarningExt(tif->tif_clientdata,"_TIFFVGetField",
+ "Truncating NumberOfInks from %u to %u",
+ val, td->td_samplesperpixel);
+ val = td->td_samplesperpixel;
+ }
+ *va_arg(ap, uint16*) = val;
+ return 1;
+ }
+ return 0;
+ }
+
+ switch (standard_tag) {
+ case TIFFTAG_SUBFILETYPE:
+ *va_arg(ap, uint32*) = td->td_subfiletype;
+ break;
+ case TIFFTAG_IMAGEWIDTH:
+ *va_arg(ap, uint32*) = td->td_imagewidth;
+ break;
+ case TIFFTAG_IMAGELENGTH:
+ *va_arg(ap, uint32*) = td->td_imagelength;
+ break;
+ case TIFFTAG_BITSPERSAMPLE:
+ *va_arg(ap, uint16*) = td->td_bitspersample;
+ break;
+ case TIFFTAG_COMPRESSION:
+ *va_arg(ap, uint16*) = td->td_compression;
+ break;
+ case TIFFTAG_PHOTOMETRIC:
+ *va_arg(ap, uint16*) = td->td_photometric;
+ break;
+ case TIFFTAG_THRESHHOLDING:
+ *va_arg(ap, uint16*) = td->td_threshholding;
+ break;
+ case TIFFTAG_FILLORDER:
+ *va_arg(ap, uint16*) = td->td_fillorder;
+ break;
+ case TIFFTAG_ORIENTATION:
+ *va_arg(ap, uint16*) = td->td_orientation;
+ break;
+ case TIFFTAG_SAMPLESPERPIXEL:
+ *va_arg(ap, uint16*) = td->td_samplesperpixel;
+ break;
+ case TIFFTAG_ROWSPERSTRIP:
+ *va_arg(ap, uint32*) = td->td_rowsperstrip;
+ break;
+ case TIFFTAG_MINSAMPLEVALUE:
+ *va_arg(ap, uint16*) = td->td_minsamplevalue;
+ break;
+ case TIFFTAG_MAXSAMPLEVALUE:
+ *va_arg(ap, uint16*) = td->td_maxsamplevalue;
+ break;
+ case TIFFTAG_SMINSAMPLEVALUE:
+ if (tif->tif_flags & TIFF_PERSAMPLE)
+ *va_arg(ap, double**) = td->td_sminsamplevalue;
+ else
+ {
+ /* libtiff historically treats this as a single value. */
+ uint16 i;
+ double v = td->td_sminsamplevalue[0];
+ for (i=1; i < td->td_samplesperpixel; ++i)
+ if( td->td_sminsamplevalue[i] < v )
+ v = td->td_sminsamplevalue[i];
+ *va_arg(ap, double*) = v;
+ }
+ break;
+ case TIFFTAG_SMAXSAMPLEVALUE:
+ if (tif->tif_flags & TIFF_PERSAMPLE)
+ *va_arg(ap, double**) = td->td_smaxsamplevalue;
+ else
+ {
+ /* libtiff historically treats this as a single value. */
+ uint16 i;
+ double v = td->td_smaxsamplevalue[0];
+ for (i=1; i < td->td_samplesperpixel; ++i)
+ if( td->td_smaxsamplevalue[i] > v )
+ v = td->td_smaxsamplevalue[i];
+ *va_arg(ap, double*) = v;
+ }
+ break;
+ case TIFFTAG_XRESOLUTION:
+ *va_arg(ap, float*) = td->td_xresolution;
+ break;
+ case TIFFTAG_YRESOLUTION:
+ *va_arg(ap, float*) = td->td_yresolution;
+ break;
+ case TIFFTAG_PLANARCONFIG:
+ *va_arg(ap, uint16*) = td->td_planarconfig;
+ break;
+ case TIFFTAG_XPOSITION:
+ *va_arg(ap, float*) = td->td_xposition;
+ break;
+ case TIFFTAG_YPOSITION:
+ *va_arg(ap, float*) = td->td_yposition;
+ break;
+ case TIFFTAG_RESOLUTIONUNIT:
+ *va_arg(ap, uint16*) = td->td_resolutionunit;
+ break;
+ case TIFFTAG_PAGENUMBER:
+ *va_arg(ap, uint16*) = td->td_pagenumber[0];
+ *va_arg(ap, uint16*) = td->td_pagenumber[1];
+ break;
+ case TIFFTAG_HALFTONEHINTS:
+ *va_arg(ap, uint16*) = td->td_halftonehints[0];
+ *va_arg(ap, uint16*) = td->td_halftonehints[1];
+ break;
+ case TIFFTAG_COLORMAP:
+ *va_arg(ap, uint16**) = td->td_colormap[0];
+ *va_arg(ap, uint16**) = td->td_colormap[1];
+ *va_arg(ap, uint16**) = td->td_colormap[2];
+ break;
+ case TIFFTAG_STRIPOFFSETS:
+ case TIFFTAG_TILEOFFSETS:
+ _TIFFFillStriles( tif );
+ *va_arg(ap, uint64**) = td->td_stripoffset;
+ break;
+ case TIFFTAG_STRIPBYTECOUNTS:
+ case TIFFTAG_TILEBYTECOUNTS:
+ _TIFFFillStriles( tif );
+ *va_arg(ap, uint64**) = td->td_stripbytecount;
+ break;
+ case TIFFTAG_MATTEING:
+ *va_arg(ap, uint16*) =
+ (td->td_extrasamples == 1 &&
+ td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
+ break;
+ case TIFFTAG_EXTRASAMPLES:
+ *va_arg(ap, uint16*) = td->td_extrasamples;
+ *va_arg(ap, uint16**) = td->td_sampleinfo;
+ break;
+ case TIFFTAG_TILEWIDTH:
+ *va_arg(ap, uint32*) = td->td_tilewidth;
+ break;
+ case TIFFTAG_TILELENGTH:
+ *va_arg(ap, uint32*) = td->td_tilelength;
+ break;
+ case TIFFTAG_TILEDEPTH:
+ *va_arg(ap, uint32*) = td->td_tiledepth;
+ break;
+ case TIFFTAG_DATATYPE:
+ switch (td->td_sampleformat) {
+ case SAMPLEFORMAT_UINT:
+ *va_arg(ap, uint16*) = DATATYPE_UINT;
+ break;
+ case SAMPLEFORMAT_INT:
+ *va_arg(ap, uint16*) = DATATYPE_INT;
+ break;
+ case SAMPLEFORMAT_IEEEFP:
+ *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
+ break;
+ case SAMPLEFORMAT_VOID:
+ *va_arg(ap, uint16*) = DATATYPE_VOID;
+ break;
+ }
+ break;
+ case TIFFTAG_SAMPLEFORMAT:
+ *va_arg(ap, uint16*) = td->td_sampleformat;
+ break;
+ case TIFFTAG_IMAGEDEPTH:
+ *va_arg(ap, uint32*) = td->td_imagedepth;
+ break;
+ case TIFFTAG_SUBIFD:
+ *va_arg(ap, uint16*) = td->td_nsubifd;
+ *va_arg(ap, uint64**) = td->td_subifd;
+ break;
+ case TIFFTAG_YCBCRPOSITIONING:
+ *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
+ *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
+ break;
+ case TIFFTAG_TRANSFERFUNCTION:
+ *va_arg(ap, uint16**) = td->td_transferfunction[0];
+ if (td->td_samplesperpixel - td->td_extrasamples > 1) {
+ *va_arg(ap, uint16**) = td->td_transferfunction[1];
+ *va_arg(ap, uint16**) = td->td_transferfunction[2];
+ } else {
+ *va_arg(ap, uint16**) = NULL;
+ *va_arg(ap, uint16**) = NULL;
+ }
+ break;
+ case TIFFTAG_REFERENCEBLACKWHITE:
+ *va_arg(ap, float**) = td->td_refblackwhite;
+ break;
+ case TIFFTAG_INKNAMES:
+ *va_arg(ap, char**) = td->td_inknames;
+ break;
+ default:
+ {
+ int i;
+
+ /*
+ * This can happen if multiple images are open
+ * with different codecs which have private
+ * tags. The global tag information table may
+ * then have tags that are valid for one file
+ * but not the other. If the client tries to
+ * get a tag that is not valid for the image's
+ * codec then we'll arrive here.
+ */
+ if( fip->field_bit != FIELD_CUSTOM )
+ {
+ TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
+ "%s: Invalid %stag \"%s\" "
+ "(not supported by codec)",
+ tif->tif_name,
+ isPseudoTag(tag) ? "pseudo-" : "",
+ fip->field_name);
+ ret_val = 0;
+ break;
+ }
+
+ /*
+ * Do we have a custom value?
+ */
+ ret_val = 0;
+ for (i = 0; i < td->td_customValueCount; i++) {
+ TIFFTagValue *tv = td->td_customValues + i;
+
+ if (tv->info->field_tag != tag)
+ continue;
+
+ if (fip->field_passcount) {
+ if (fip->field_readcount == TIFF_VARIABLE2)
+ *va_arg(ap, uint32*) = (uint32)tv->count;
+ else /* Assume TIFF_VARIABLE */
+ *va_arg(ap, uint16*) = (uint16)tv->count;
+ *va_arg(ap, void **) = tv->value;
+ ret_val = 1;
+ } else if (fip->field_tag == TIFFTAG_DOTRANGE
+ && strcmp(fip->field_name,"DotRange") == 0) {
+ /* TODO: This is an evil exception and should not have been
+ handled this way ... likely best if we move it into
+ the directory structure with an explicit field in
+ libtiff 4.1 and assign it a FIELD_ value */
+ *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
+ *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
+ ret_val = 1;
+ } else {
+ if (fip->field_type == TIFF_ASCII
+ || fip->field_readcount == TIFF_VARIABLE
+ || fip->field_readcount == TIFF_VARIABLE2
+ || fip->field_readcount == TIFF_SPP
+ || tv->count > 1) {
+ *va_arg(ap, void **) = tv->value;
+ ret_val = 1;
+ } else {
+ char *val = (char *)tv->value;
+ assert( tv->count == 1 );
+ switch (fip->field_type) {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ *va_arg(ap, uint8*) =
+ *(uint8 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_SBYTE:
+ *va_arg(ap, int8*) =
+ *(int8 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_SHORT:
+ *va_arg(ap, uint16*) =
+ *(uint16 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_SSHORT:
+ *va_arg(ap, int16*) =
+ *(int16 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_LONG:
+ case TIFF_IFD:
+ *va_arg(ap, uint32*) =
+ *(uint32 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_SLONG:
+ *va_arg(ap, int32*) =
+ *(int32 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_LONG8:
+ case TIFF_IFD8:
+ *va_arg(ap, uint64*) =
+ *(uint64 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_SLONG8:
+ *va_arg(ap, int64*) =
+ *(int64 *)val;
+ ret_val = 1;
+ break;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ *va_arg(ap, float*) =
+ *(float *)val;
+ ret_val = 1;
+ break;
+ case TIFF_DOUBLE:
+ *va_arg(ap, double*) =
+ *(double *)val;
+ ret_val = 1;
+ break;
+ default:
+ ret_val = 0;
+ break;
+ }
+ }
+ }
+ break;
+ }
+ }
+ }
+ return(ret_val);
+}
+
+/*
+ * Return the value of a field in the
+ * internal directory structure.
+ */
+int
+TIFFGetField(TIFF* tif, uint32 tag, ...)
+{
+ int status;
+ va_list ap;
+
+ va_start(ap, tag);
+ status = TIFFVGetField(tif, tag, ap);
+ va_end(ap);
+ return (status);
+}
+
+/*
+ * Like TIFFGetField, but taking a varargs
+ * parameter list. This routine is useful
+ * for building higher-level interfaces on
+ * top of the library.
+ */
+int
+TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
+ return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
+ (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
+}
+
+#define CleanupField(member) { \
+ if (td->member) { \
+ _TIFFfree(td->member); \
+ td->member = 0; \
+ } \
+}
+
+/*
+ * Release storage associated with a directory.
+ */
+void
+TIFFFreeDirectory(TIFF* tif)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ int i;
+
+ _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
+ CleanupField(td_sminsamplevalue);
+ CleanupField(td_smaxsamplevalue);
+ CleanupField(td_colormap[0]);
+ CleanupField(td_colormap[1]);
+ CleanupField(td_colormap[2]);
+ CleanupField(td_sampleinfo);
+ CleanupField(td_subifd);
+ CleanupField(td_inknames);
+ CleanupField(td_refblackwhite);
+ CleanupField(td_transferfunction[0]);
+ CleanupField(td_transferfunction[1]);
+ CleanupField(td_transferfunction[2]);
+ CleanupField(td_stripoffset);
+ CleanupField(td_stripbytecount);
+ TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
+ TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
+
+ /* Cleanup custom tag values */
+ for( i = 0; i < td->td_customValueCount; i++ ) {
+ if (td->td_customValues[i].value)
+ _TIFFfree(td->td_customValues[i].value);
+ }
+
+ td->td_customValueCount = 0;
+ CleanupField(td_customValues);
+
+#if defined(DEFER_STRILE_LOAD)
+ _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
+ _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
+#endif
+}
+#undef CleanupField
+
+/*
+ * Client Tag extension support (from Niles Ritter).
+ */
+static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
+
+TIFFExtendProc
+TIFFSetTagExtender(TIFFExtendProc extender)
+{
+ TIFFExtendProc prev = _TIFFextender;
+ _TIFFextender = extender;
+ return (prev);
+}
+
+/*
+ * Setup for a new directory. Should we automatically call
+ * TIFFWriteDirectory() if the current one is dirty?
+ *
+ * The newly created directory will not exist on the file till
+ * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
+ */
+int
+TIFFCreateDirectory(TIFF* tif)
+{
+ TIFFDefaultDirectory(tif);
+ tif->tif_diroff = 0;
+ tif->tif_nextdiroff = 0;
+ tif->tif_curoff = 0;
+ tif->tif_row = (uint32) -1;
+ tif->tif_curstrip = (uint32) -1;
+
+ return 0;
+}
+
+int
+TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
+{
+ TIFFDefaultDirectory(tif);
+
+ /*
+ * Reset the field definitions to match the application provided list.
+ * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
+ * based on it's assumption this is an image directory.
+ */
+ _TIFFSetupFields(tif, infoarray);
+
+ tif->tif_diroff = 0;
+ tif->tif_nextdiroff = 0;
+ tif->tif_curoff = 0;
+ tif->tif_row = (uint32) -1;
+ tif->tif_curstrip = (uint32) -1;
+
+ return 0;
+}
+
+int
+TIFFCreateEXIFDirectory(TIFF* tif)
+{
+ const TIFFFieldArray* exifFieldArray;
+ exifFieldArray = _TIFFGetExifFields();
+ return TIFFCreateCustomDirectory(tif, exifFieldArray);
+}
+
+/*
+ * Setup a default directory structure.
+ */
+int
+TIFFDefaultDirectory(TIFF* tif)
+{
+ register TIFFDirectory* td = &tif->tif_dir;
+ const TIFFFieldArray* tiffFieldArray;
+
+ tiffFieldArray = _TIFFGetFields();
+ _TIFFSetupFields(tif, tiffFieldArray);
+
+ _TIFFmemset(td, 0, sizeof (*td));
+ td->td_fillorder = FILLORDER_MSB2LSB;
+ td->td_bitspersample = 1;
+ td->td_threshholding = THRESHHOLD_BILEVEL;
+ td->td_orientation = ORIENTATION_TOPLEFT;
+ td->td_samplesperpixel = 1;
+ td->td_rowsperstrip = (uint32) -1;
+ td->td_tilewidth = 0;
+ td->td_tilelength = 0;
+ td->td_tiledepth = 1;
+ td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
+ td->td_resolutionunit = RESUNIT_INCH;
+ td->td_sampleformat = SAMPLEFORMAT_UINT;
+ td->td_imagedepth = 1;
+ td->td_ycbcrsubsampling[0] = 2;
+ td->td_ycbcrsubsampling[1] = 2;
+ td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ tif->tif_foundfield = NULL;
+ tif->tif_tagmethods.vsetfield = _TIFFVSetField;
+ tif->tif_tagmethods.vgetfield = _TIFFVGetField;
+ tif->tif_tagmethods.printdir = NULL;
+ /*
+ * Give client code a chance to install their own
+ * tag extensions & methods, prior to compression overloads,
+ * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
+ */
+ if (tif->tif_nfieldscompat > 0) {
+ uint32 i;
+
+ for (i = 0; i < tif->tif_nfieldscompat; i++) {
+ if (tif->tif_fieldscompat[i].allocated_size)
+ _TIFFfree(tif->tif_fieldscompat[i].fields);
+ }
+ _TIFFfree(tif->tif_fieldscompat);
+ tif->tif_nfieldscompat = 0;
+ tif->tif_fieldscompat = NULL;
+ }
+ if (_TIFFextender)
+ (*_TIFFextender)(tif);
+ (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
+ /*
+ * NB: The directory is marked dirty as a result of setting
+ * up the default compression scheme. However, this really
+ * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
+ * if the user does something. We could just do the setup
+ * by hand, but it seems better to use the normal mechanism
+ * (i.e. TIFFSetField).
+ */
+ tif->tif_flags &= ~TIFF_DIRTYDIRECT;
+
+ /*
+ * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
+ * we clear the ISTILED flag when setting up a new directory.
+ * Should we also be clearing stuff like INSUBIFD?
+ */
+ tif->tif_flags &= ~TIFF_ISTILED;
+
+ return (1);
+}
+
+static int
+TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
+{
+ static const char module[] = "TIFFAdvanceDirectory";
+ if (isMapped(tif))
+ {
+ uint64 poff=*nextdir;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ tmsize_t poffa,poffb,poffc,poffd;
+ uint16 dircount;
+ uint32 nextdir32;
+ poffa=(tmsize_t)poff;
+ poffb=poffa+sizeof(uint16);
+ if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
+ *nextdir=0;
+ return(0);
+ }
+ _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ poffc=poffb+dircount*12;
+ poffd=poffc+sizeof(uint32);
+ if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
+ return(0);
+ }
+ if (off!=NULL)
+ *off=(uint64)poffc;
+ _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&nextdir32);
+ *nextdir=nextdir32;
+ }
+ else
+ {
+ tmsize_t poffa,poffb,poffc,poffd;
+ uint64 dircount64;
+ uint16 dircount16;
+ poffa=(tmsize_t)poff;
+ poffb=poffa+sizeof(uint64);
+ if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
+ return(0);
+ }
+ _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>0xFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
+ return(0);
+ }
+ dircount16=(uint16)dircount64;
+ poffc=poffb+dircount16*20;
+ poffd=poffc+sizeof(uint64);
+ if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
+ return(0);
+ }
+ if (off!=NULL)
+ *off=(uint64)poffc;
+ _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(nextdir);
+ }
+ return(1);
+ }
+ else
+ {
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint16 dircount;
+ uint32 nextdir32;
+ if (!SeekOK(tif, *nextdir) ||
+ !ReadOK(tif, &dircount, sizeof (uint16))) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ if (off != NULL)
+ *off = TIFFSeekFile(tif,
+ dircount*12, SEEK_CUR);
+ else
+ (void) TIFFSeekFile(tif,
+ dircount*12, SEEK_CUR);
+ if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&nextdir32);
+ *nextdir=nextdir32;
+ }
+ else
+ {
+ uint64 dircount64;
+ uint16 dircount16;
+ if (!SeekOK(tif, *nextdir) ||
+ !ReadOK(tif, &dircount64, sizeof (uint64))) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>0xFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
+ return(0);
+ }
+ dircount16 = (uint16)dircount64;
+ if (off != NULL)
+ *off = TIFFSeekFile(tif,
+ dircount16*20, SEEK_CUR);
+ else
+ (void) TIFFSeekFile(tif,
+ dircount16*20, SEEK_CUR);
+ if (!ReadOK(tif, nextdir, sizeof (uint64))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Error fetching directory link",
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(nextdir);
+ }
+ return (1);
+ }
+}
+
+/*
+ * Count the number of directories in a file.
+ */
+uint16
+TIFFNumberOfDirectories(TIFF* tif)
+{
+ static const char module[] = "TIFFNumberOfDirectories";
+ uint64 nextdir;
+ uint16 n;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ nextdir = tif->tif_header.classic.tiff_diroff;
+ else
+ nextdir = tif->tif_header.big.tiff_diroff;
+ n = 0;
+ while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
+ {
+ if (n != 65535) {
+ ++n;
+ }
+ else
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Directory count exceeded 65535 limit,"
+ " giving up on counting.");
+ return (65535);
+ }
+ }
+ return (n);
+}
+
+/*
+ * Set the n-th directory as the current directory.
+ * NB: Directories are numbered starting at 0.
+ */
+int
+TIFFSetDirectory(TIFF* tif, uint16 dirn)
+{
+ uint64 nextdir;
+ uint16 n;
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ nextdir = tif->tif_header.classic.tiff_diroff;
+ else
+ nextdir = tif->tif_header.big.tiff_diroff;
+ for (n = dirn; n > 0 && nextdir != 0; n--)
+ if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
+ return (0);
+ tif->tif_nextdiroff = nextdir;
+ /*
+ * Set curdir to the actual directory index. The
+ * -1 is because TIFFReadDirectory will increment
+ * tif_curdir after successfully reading the directory.
+ */
+ tif->tif_curdir = (dirn - n) - 1;
+ /*
+ * Reset tif_dirnumber counter and start new list of seen directories.
+ * We need this to prevent IFD loops.
+ */
+ tif->tif_dirnumber = 0;
+ return (TIFFReadDirectory(tif));
+}
+
+/*
+ * Set the current directory to be the directory
+ * located at the specified file offset. This interface
+ * is used mainly to access directories linked with
+ * the SubIFD tag (e.g. thumbnail images).
+ */
+int
+TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
+{
+ tif->tif_nextdiroff = diroff;
+ /*
+ * Reset tif_dirnumber counter and start new list of seen directories.
+ * We need this to prevent IFD loops.
+ */
+ tif->tif_dirnumber = 0;
+ return (TIFFReadDirectory(tif));
+}
+
+/*
+ * Return file offset of the current directory.
+ */
+uint64
+TIFFCurrentDirOffset(TIFF* tif)
+{
+ return (tif->tif_diroff);
+}
+
+/*
+ * Return an indication of whether or not we are
+ * at the last directory in the file.
+ */
+int
+TIFFLastDirectory(TIFF* tif)
+{
+ return (tif->tif_nextdiroff == 0);
+}
+
+/*
+ * Unlink the specified directory from the directory chain.
+ */
+int
+TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
+{
+ static const char module[] = "TIFFUnlinkDirectory";
+ uint64 nextdir;
+ uint64 off;
+ uint16 n;
+
+ if (tif->tif_mode == O_RDONLY) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not unlink directory in read-only file");
+ return (0);
+ }
+ /*
+ * Go to the directory before the one we want
+ * to unlink and nab the offset of the link
+ * field we'll need to patch.
+ */
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ nextdir = tif->tif_header.classic.tiff_diroff;
+ off = 4;
+ }
+ else
+ {
+ nextdir = tif->tif_header.big.tiff_diroff;
+ off = 8;
+ }
+ for (n = dirn-1; n > 0; n--) {
+ if (nextdir == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
+ return (0);
+ }
+ if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
+ return (0);
+ }
+ /*
+ * Advance to the directory to be unlinked and fetch
+ * the offset of the directory that follows.
+ */
+ if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
+ return (0);
+ /*
+ * Go back and patch the link field of the preceding
+ * directory to point to the offset of the directory
+ * that follows.
+ */
+ (void) TIFFSeekFile(tif, off, SEEK_SET);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 nextdir32;
+ nextdir32=(uint32)nextdir;
+ assert((uint64)nextdir32==nextdir);
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&nextdir32);
+ if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
+ return (0);
+ }
+ }
+ else
+ {
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&nextdir);
+ if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
+ return (0);
+ }
+ }
+ /*
+ * Leave directory state setup safely. We don't have
+ * facilities for doing inserting and removing directories,
+ * so it's safest to just invalidate everything. This
+ * means that the caller can only append to the directory
+ * chain.
+ */
+ (*tif->tif_cleanup)(tif);
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawcc = 0;
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = 0;
+ }
+ tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
+ TIFFFreeDirectory(tif);
+ TIFFDefaultDirectory(tif);
+ tif->tif_diroff = 0; /* force link on next write */
+ tif->tif_nextdiroff = 0; /* next write must be at end */
+ tif->tif_curoff = 0;
+ tif->tif_row = (uint32) -1;
+ tif->tif_curstrip = (uint32) -1;
+ return (1);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dir.h b/test/monniaux/tiff-4.0.10/tif_dir.h
new file mode 100644
index 00000000..b2f5e694
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dir.h
@@ -0,0 +1,311 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _TIFFDIR_
+#define _TIFFDIR_
+
+#include "tiff.h"
+#include "tiffio.h"
+
+/*
+ * ``Library-private'' Directory-related Definitions.
+ */
+
+typedef struct {
+ const TIFFField *info;
+ int count;
+ void *value;
+} TIFFTagValue;
+
+/*
+ * TIFF Image File Directories are comprised of a table of field
+ * descriptors of the form shown below. The table is sorted in
+ * ascending order by tag. The values associated with each entry are
+ * disjoint and may appear anywhere in the file (so long as they are
+ * placed on a word boundary).
+ *
+ * If the value is 4 bytes or less, in ClassicTIFF, or 8 bytes or less in
+ * BigTIFF, then it is placed in the offset field to save space. If so,
+ * it is left-justified in the offset field.
+ */
+typedef struct {
+ uint16 tdir_tag; /* see below */
+ uint16 tdir_type; /* data type; see below */
+ uint64 tdir_count; /* number of items; length in spec */
+ union {
+ uint16 toff_short;
+ uint32 toff_long;
+ uint64 toff_long8;
+ } tdir_offset; /* either offset or the data itself if fits */
+} TIFFDirEntry;
+
+/*
+ * Internal format of a TIFF directory entry.
+ */
+typedef struct {
+#define FIELD_SETLONGS 4
+ /* bit vector of fields that are set */
+ unsigned long td_fieldsset[FIELD_SETLONGS];
+
+ uint32 td_imagewidth, td_imagelength, td_imagedepth;
+ uint32 td_tilewidth, td_tilelength, td_tiledepth;
+ uint32 td_subfiletype;
+ uint16 td_bitspersample;
+ uint16 td_sampleformat;
+ uint16 td_compression;
+ uint16 td_photometric;
+ uint16 td_threshholding;
+ uint16 td_fillorder;
+ uint16 td_orientation;
+ uint16 td_samplesperpixel;
+ uint32 td_rowsperstrip;
+ uint16 td_minsamplevalue, td_maxsamplevalue;
+ double* td_sminsamplevalue;
+ double* td_smaxsamplevalue;
+ float td_xresolution, td_yresolution;
+ uint16 td_resolutionunit;
+ uint16 td_planarconfig;
+ float td_xposition, td_yposition;
+ uint16 td_pagenumber[2];
+ uint16* td_colormap[3];
+ uint16 td_halftonehints[2];
+ uint16 td_extrasamples;
+ uint16* td_sampleinfo;
+ /* even though the name is misleading, td_stripsperimage is the number
+ * of striles (=strips or tiles) per plane, and td_nstrips the total
+ * number of striles */
+ uint32 td_stripsperimage;
+ uint32 td_nstrips; /* size of offset & bytecount arrays */
+ uint64* td_stripoffset;
+ uint64* td_stripbytecount;
+ int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
+#if defined(DEFER_STRILE_LOAD)
+ TIFFDirEntry td_stripoffset_entry; /* for deferred loading */
+ TIFFDirEntry td_stripbytecount_entry; /* for deferred loading */
+#endif
+ uint16 td_nsubifd;
+ uint64* td_subifd;
+ /* YCbCr parameters */
+ uint16 td_ycbcrsubsampling[2];
+ uint16 td_ycbcrpositioning;
+ /* Colorimetry parameters */
+ uint16* td_transferfunction[3];
+ float* td_refblackwhite;
+ /* CMYK parameters */
+ int td_inknameslen;
+ char* td_inknames;
+
+ int td_customValueCount;
+ TIFFTagValue *td_customValues;
+} TIFFDirectory;
+
+/*
+ * Field flags used to indicate fields that have been set in a directory, and
+ * to reference fields when manipulating a directory.
+ */
+
+/*
+ * FIELD_IGNORE is used to signify tags that are to be processed but otherwise
+ * ignored. This permits antiquated tags to be quietly read and discarded.
+ * Note that a bit *is* allocated for ignored tags; this is understood by the
+ * directory reading logic which uses this fact to avoid special-case handling
+ */
+#define FIELD_IGNORE 0
+
+/* multi-item fields */
+#define FIELD_IMAGEDIMENSIONS 1
+#define FIELD_TILEDIMENSIONS 2
+#define FIELD_RESOLUTION 3
+#define FIELD_POSITION 4
+
+/* single-item fields */
+#define FIELD_SUBFILETYPE 5
+#define FIELD_BITSPERSAMPLE 6
+#define FIELD_COMPRESSION 7
+#define FIELD_PHOTOMETRIC 8
+#define FIELD_THRESHHOLDING 9
+#define FIELD_FILLORDER 10
+#define FIELD_ORIENTATION 15
+#define FIELD_SAMPLESPERPIXEL 16
+#define FIELD_ROWSPERSTRIP 17
+#define FIELD_MINSAMPLEVALUE 18
+#define FIELD_MAXSAMPLEVALUE 19
+#define FIELD_PLANARCONFIG 20
+#define FIELD_RESOLUTIONUNIT 22
+#define FIELD_PAGENUMBER 23
+#define FIELD_STRIPBYTECOUNTS 24
+#define FIELD_STRIPOFFSETS 25
+#define FIELD_COLORMAP 26
+#define FIELD_EXTRASAMPLES 31
+#define FIELD_SAMPLEFORMAT 32
+#define FIELD_SMINSAMPLEVALUE 33
+#define FIELD_SMAXSAMPLEVALUE 34
+#define FIELD_IMAGEDEPTH 35
+#define FIELD_TILEDEPTH 36
+#define FIELD_HALFTONEHINTS 37
+#define FIELD_YCBCRSUBSAMPLING 39
+#define FIELD_YCBCRPOSITIONING 40
+#define FIELD_REFBLACKWHITE 41
+#define FIELD_TRANSFERFUNCTION 44
+#define FIELD_INKNAMES 46
+#define FIELD_SUBIFD 49
+/* FIELD_CUSTOM (see tiffio.h) 65 */
+/* end of support for well-known tags; codec-private tags follow */
+#define FIELD_CODEC 66 /* base of codec-private tags */
+
+
+/*
+ * Pseudo-tags don't normally need field bits since they are not written to an
+ * output file (by definition). The library also has express logic to always
+ * query a codec for a pseudo-tag so allocating a field bit for one is a
+ * waste. If codec wants to promote the notion of a pseudo-tag being ``set''
+ * or ``unset'' then it can do using internal state flags without polluting
+ * the field bit space defined for real tags.
+ */
+#define FIELD_PSEUDO 0
+
+#define FIELD_LAST (32*FIELD_SETLONGS-1)
+
+#define BITn(n) (((unsigned long)1L)<<((n)&0x1f))
+#define BITFIELDn(tif, n) ((tif)->tif_dir.td_fieldsset[(n)/32])
+#define TIFFFieldSet(tif, field) (BITFIELDn(tif, field) & BITn(field))
+#define TIFFSetFieldBit(tif, field) (BITFIELDn(tif, field) |= BITn(field))
+#define TIFFClrFieldBit(tif, field) (BITFIELDn(tif, field) &= ~BITn(field))
+
+#define FieldSet(fields, f) (fields[(f)/32] & BITn(f))
+#define ResetFieldBit(fields, f) (fields[(f)/32] &= ~BITn(f))
+
+typedef enum {
+ TIFF_SETGET_UNDEFINED = 0,
+ TIFF_SETGET_ASCII = 1,
+ TIFF_SETGET_UINT8 = 2,
+ TIFF_SETGET_SINT8 = 3,
+ TIFF_SETGET_UINT16 = 4,
+ TIFF_SETGET_SINT16 = 5,
+ TIFF_SETGET_UINT32 = 6,
+ TIFF_SETGET_SINT32 = 7,
+ TIFF_SETGET_UINT64 = 8,
+ TIFF_SETGET_SINT64 = 9,
+ TIFF_SETGET_FLOAT = 10,
+ TIFF_SETGET_DOUBLE = 11,
+ TIFF_SETGET_IFD8 = 12,
+ TIFF_SETGET_INT = 13,
+ TIFF_SETGET_UINT16_PAIR = 14,
+ TIFF_SETGET_C0_ASCII = 15,
+ TIFF_SETGET_C0_UINT8 = 16,
+ TIFF_SETGET_C0_SINT8 = 17,
+ TIFF_SETGET_C0_UINT16 = 18,
+ TIFF_SETGET_C0_SINT16 = 19,
+ TIFF_SETGET_C0_UINT32 = 20,
+ TIFF_SETGET_C0_SINT32 = 21,
+ TIFF_SETGET_C0_UINT64 = 22,
+ TIFF_SETGET_C0_SINT64 = 23,
+ TIFF_SETGET_C0_FLOAT = 24,
+ TIFF_SETGET_C0_DOUBLE = 25,
+ TIFF_SETGET_C0_IFD8 = 26,
+ TIFF_SETGET_C16_ASCII = 27,
+ TIFF_SETGET_C16_UINT8 = 28,
+ TIFF_SETGET_C16_SINT8 = 29,
+ TIFF_SETGET_C16_UINT16 = 30,
+ TIFF_SETGET_C16_SINT16 = 31,
+ TIFF_SETGET_C16_UINT32 = 32,
+ TIFF_SETGET_C16_SINT32 = 33,
+ TIFF_SETGET_C16_UINT64 = 34,
+ TIFF_SETGET_C16_SINT64 = 35,
+ TIFF_SETGET_C16_FLOAT = 36,
+ TIFF_SETGET_C16_DOUBLE = 37,
+ TIFF_SETGET_C16_IFD8 = 38,
+ TIFF_SETGET_C32_ASCII = 39,
+ TIFF_SETGET_C32_UINT8 = 40,
+ TIFF_SETGET_C32_SINT8 = 41,
+ TIFF_SETGET_C32_UINT16 = 42,
+ TIFF_SETGET_C32_SINT16 = 43,
+ TIFF_SETGET_C32_UINT32 = 44,
+ TIFF_SETGET_C32_SINT32 = 45,
+ TIFF_SETGET_C32_UINT64 = 46,
+ TIFF_SETGET_C32_SINT64 = 47,
+ TIFF_SETGET_C32_FLOAT = 48,
+ TIFF_SETGET_C32_DOUBLE = 49,
+ TIFF_SETGET_C32_IFD8 = 50,
+ TIFF_SETGET_OTHER = 51
+} TIFFSetGetFieldType;
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+extern const TIFFFieldArray* _TIFFGetFields(void);
+extern const TIFFFieldArray* _TIFFGetExifFields(void);
+extern void _TIFFSetupFields(TIFF* tif, const TIFFFieldArray* infoarray);
+extern void _TIFFPrintFieldInfo(TIFF*, FILE*);
+
+extern int _TIFFFillStriles(TIFF*);
+
+typedef enum {
+ tfiatImage,
+ tfiatExif,
+ tfiatOther
+} TIFFFieldArrayType;
+
+struct _TIFFFieldArray {
+ TIFFFieldArrayType type; /* array type, will be used to determine if IFD is image and such */
+ uint32 allocated_size; /* 0 if array is constant, other if modified by future definition extension support */
+ uint32 count; /* number of elements in fields array */
+ TIFFField* fields; /* actual field info */
+};
+
+struct _TIFFField {
+ uint32 field_tag; /* field's tag */
+ short field_readcount; /* read count/TIFF_VARIABLE/TIFF_SPP */
+ short field_writecount; /* write count/TIFF_VARIABLE */
+ TIFFDataType field_type; /* type of associated data */
+ uint32 reserved; /* reserved for future extension */
+ TIFFSetGetFieldType set_field_type; /* type to be passed to TIFFSetField */
+ TIFFSetGetFieldType get_field_type; /* type to be passed to TIFFGetField */
+ unsigned short field_bit; /* bit in fieldsset bit vector */
+ unsigned char field_oktochange; /* if true, can change while writing */
+ unsigned char field_passcount; /* if true, pass dir count on set */
+ char* field_name; /* ASCII name */
+ TIFFFieldArray* field_subfields; /* if field points to child ifds, child ifd field definition array */
+};
+
+extern int _TIFFMergeFields(TIFF*, const TIFFField[], uint32);
+extern const TIFFField* _TIFFFindOrRegisterField(TIFF *, uint32, TIFFDataType);
+extern TIFFField* _TIFFCreateAnonField(TIFF *, uint32, TIFFDataType);
+extern int _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag);
+
+#if defined(__cplusplus)
+}
+#endif
+#endif /* _TIFFDIR_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dirinfo.c b/test/monniaux/tiff-4.0.10/tif_dirinfo.c
new file mode 100644
index 00000000..e1f6b23e
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dirinfo.c
@@ -0,0 +1,1081 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Core Directory Tag Support.
+ */
+#include "tiffiop.h"
+#include <stdlib.h>
+
+/*
+ * NOTE: THIS ARRAY IS ASSUMED TO BE SORTED BY TAG.
+ *
+ * NOTE: The second field (field_readcount) and third field (field_writecount)
+ * sometimes use the values TIFF_VARIABLE (-1), TIFF_VARIABLE2 (-3)
+ * and TIFF_SPP (-2). The macros should be used but would throw off
+ * the formatting of the code, so please interpret the -1, -2 and -3
+ * values accordingly.
+ */
+
+/* const object should be initialized */
+#ifdef _MSC_VER
+#pragma warning( push )
+#pragma warning( disable : 4132 )
+#endif
+static const TIFFFieldArray tiffFieldArray;
+static const TIFFFieldArray exifFieldArray;
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif
+
+static const TIFFField
+tiffFields[] = {
+ { TIFFTAG_SUBFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_SUBFILETYPE, 1, 0, "SubfileType", NULL },
+ { TIFFTAG_OSUBFILETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_SUBFILETYPE, 1, 0, "OldSubfileType", NULL },
+ { TIFFTAG_IMAGEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDIMENSIONS, 0, 0, "ImageWidth", NULL },
+ { TIFFTAG_IMAGELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDIMENSIONS, 1, 0, "ImageLength", NULL },
+ { TIFFTAG_BITSPERSAMPLE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_BITSPERSAMPLE, 0, 0, "BitsPerSample", NULL },
+ { TIFFTAG_COMPRESSION, -1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_COMPRESSION, 0, 0, "Compression", NULL },
+ { TIFFTAG_PHOTOMETRIC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_PHOTOMETRIC, 0, 0, "PhotometricInterpretation", NULL },
+ { TIFFTAG_THRESHHOLDING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_THRESHHOLDING, 1, 0, "Threshholding", NULL },
+ { TIFFTAG_CELLWIDTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "CellWidth", NULL },
+ { TIFFTAG_CELLLENGTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "CellLength", NULL },
+ { TIFFTAG_FILLORDER, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_FILLORDER, 0, 0, "FillOrder", NULL },
+ { TIFFTAG_DOCUMENTNAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DocumentName", NULL },
+ { TIFFTAG_IMAGEDESCRIPTION, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageDescription", NULL },
+ { TIFFTAG_MAKE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Make", NULL },
+ { TIFFTAG_MODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Model", NULL },
+ { TIFFTAG_STRIPOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "StripOffsets", NULL },
+ { TIFFTAG_ORIENTATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_ORIENTATION, 0, 0, "Orientation", NULL },
+ { TIFFTAG_SAMPLESPERPIXEL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLESPERPIXEL, 0, 0, "SamplesPerPixel", NULL },
+ { TIFFTAG_ROWSPERSTRIP, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_ROWSPERSTRIP, 0, 0, "RowsPerStrip", NULL },
+ { TIFFTAG_STRIPBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "StripByteCounts", NULL },
+ { TIFFTAG_MINSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_MINSAMPLEVALUE, 1, 0, "MinSampleValue", NULL },
+ { TIFFTAG_MAXSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_MAXSAMPLEVALUE, 1, 0, "MaxSampleValue", NULL },
+ { TIFFTAG_XRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTION, 1, 0, "XResolution", NULL },
+ { TIFFTAG_YRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTION, 1, 0, "YResolution", NULL },
+ { TIFFTAG_PLANARCONFIG, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_PLANARCONFIG, 0, 0, "PlanarConfiguration", NULL },
+ { TIFFTAG_PAGENAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PageName", NULL },
+ { TIFFTAG_XPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_POSITION, 1, 0, "XPosition", NULL },
+ { TIFFTAG_YPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_POSITION, 1, 0, "YPosition", NULL },
+ { TIFFTAG_FREEOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeOffsets", NULL },
+ { TIFFTAG_FREEBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeByteCounts", NULL },
+ { TIFFTAG_GRAYRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseUnit", NULL },
+ { TIFFTAG_GRAYRESPONSECURVE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseCurve", NULL },
+ { TIFFTAG_RESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTIONUNIT, 1, 0, "ResolutionUnit", NULL },
+ { TIFFTAG_PAGENUMBER, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_PAGENUMBER, 1, 0, "PageNumber", NULL },
+ { TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "ColorResponseUnit", NULL },
+ { TIFFTAG_TRANSFERFUNCTION, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_TRANSFERFUNCTION, 1, 0, "TransferFunction", NULL },
+ { TIFFTAG_SOFTWARE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Software", NULL },
+ { TIFFTAG_DATETIME, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTime", NULL },
+ { TIFFTAG_ARTIST, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Artist", NULL },
+ { TIFFTAG_HOSTCOMPUTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "HostComputer", NULL },
+ { TIFFTAG_WHITEPOINT, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "WhitePoint", NULL },
+ { TIFFTAG_PRIMARYCHROMATICITIES, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PrimaryChromaticities", NULL },
+ { TIFFTAG_COLORMAP, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_COLORMAP, 1, 0, "ColorMap", NULL },
+ { TIFFTAG_HALFTONEHINTS, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_HALFTONEHINTS, 1, 0, "HalftoneHints", NULL },
+ { TIFFTAG_TILEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDIMENSIONS, 0, 0, "TileWidth", NULL },
+ { TIFFTAG_TILELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDIMENSIONS, 0, 0, "TileLength", NULL },
+ { TIFFTAG_TILEOFFSETS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "TileOffsets", NULL },
+ { TIFFTAG_TILEBYTECOUNTS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "TileByteCounts", NULL },
+ { TIFFTAG_SUBIFD, -1, -1, TIFF_IFD8, 0, TIFF_SETGET_C16_IFD8, TIFF_SETGET_UNDEFINED, FIELD_SUBIFD, 1, 1, "SubIFD", (TIFFFieldArray*) &tiffFieldArray },
+ { TIFFTAG_INKSET, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "InkSet", NULL },
+ { TIFFTAG_INKNAMES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_C16_ASCII, TIFF_SETGET_UNDEFINED, FIELD_INKNAMES, 1, 1, "InkNames", NULL },
+ { TIFFTAG_NUMBEROFINKS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "NumberOfInks", NULL },
+ { TIFFTAG_DOTRANGE, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DotRange", NULL },
+ { TIFFTAG_TARGETPRINTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TargetPrinter", NULL },
+ { TIFFTAG_EXTRASAMPLES, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_EXTRASAMPLES, 0, 1, "ExtraSamples", NULL },
+ { TIFFTAG_SAMPLEFORMAT, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLEFORMAT, 0, 0, "SampleFormat", NULL },
+ { TIFFTAG_SMINSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_SMINSAMPLEVALUE, 1, 0, "SMinSampleValue", NULL },
+ { TIFFTAG_SMAXSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_SMAXSAMPLEVALUE, 1, 0, "SMaxSampleValue", NULL },
+ { TIFFTAG_CLIPPATH, -1, -3, TIFF_BYTE, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ClipPath", NULL },
+ { TIFFTAG_XCLIPPATHUNITS, 1, 1, TIFF_SLONG, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "XClipPathUnits", NULL },
+ { TIFFTAG_XCLIPPATHUNITS, 1, 1, TIFF_SBYTE, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "XClipPathUnits", NULL },
+ { TIFFTAG_YCLIPPATHUNITS, 1, 1, TIFF_SLONG, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "YClipPathUnits", NULL },
+ { TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "YCbCrCoefficients", NULL },
+ { TIFFTAG_YCBCRSUBSAMPLING, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_YCBCRSUBSAMPLING, 0, 0, "YCbCrSubsampling", NULL },
+ { TIFFTAG_YCBCRPOSITIONING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_YCBCRPOSITIONING, 0, 0, "YCbCrPositioning", NULL },
+ { TIFFTAG_REFERENCEBLACKWHITE, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_REFBLACKWHITE, 1, 0, "ReferenceBlackWhite", NULL },
+ { TIFFTAG_XMLPACKET, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "XMLPacket", NULL },
+ /* begin SGI tags */
+ { TIFFTAG_MATTEING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_EXTRASAMPLES, 0, 0, "Matteing", NULL },
+ { TIFFTAG_DATATYPE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLEFORMAT, 0, 0, "DataType", NULL },
+ { TIFFTAG_IMAGEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDEPTH, 0, 0, "ImageDepth", NULL },
+ { TIFFTAG_TILEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDEPTH, 0, 0, "TileDepth", NULL },
+ /* end SGI tags */
+ /* begin Pixar tags */
+ { TIFFTAG_PIXAR_IMAGEFULLWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageFullWidth", NULL },
+ { TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageFullLength", NULL },
+ { TIFFTAG_PIXAR_TEXTUREFORMAT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TextureFormat", NULL },
+ { TIFFTAG_PIXAR_WRAPMODES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TextureWrapModes", NULL },
+ { TIFFTAG_PIXAR_FOVCOT, 1, 1, TIFF_FLOAT, 0, TIFF_SETGET_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FieldOfViewCotangent", NULL },
+ { TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MatrixWorldToScreen", NULL },
+ { TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MatrixWorldToCamera", NULL },
+ { TIFFTAG_CFAREPEATPATTERNDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CFARepeatPatternDim", NULL },
+ { TIFFTAG_CFAPATTERN, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CFAPattern" , NULL},
+ { TIFFTAG_COPYRIGHT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Copyright", NULL },
+ /* end Pixar tags */
+ { TIFFTAG_RICHTIFFIPTC, -3, -3, TIFF_LONG, 0, TIFF_SETGET_C32_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "RichTIFFIPTC", NULL },
+ { TIFFTAG_PHOTOSHOP, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "Photoshop", NULL },
+ { TIFFTAG_EXIFIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "EXIFIFDOffset", (TIFFFieldArray*) &exifFieldArray },
+ { TIFFTAG_ICCPROFILE, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ICC Profile", NULL },
+ { TIFFTAG_GPSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "GPSIFDOffset", NULL },
+ { TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvParams", NULL },
+ { TIFFTAG_FAXSUBADDRESS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxSubAddress", NULL },
+ { TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvTime", NULL },
+ { TIFFTAG_FAXDCS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxDcs", NULL },
+ { TIFFTAG_STONITS, 1, 1, TIFF_DOUBLE, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "StoNits", NULL },
+ { TIFFTAG_INTEROPERABILITYIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "InteroperabilityIFDOffset", NULL },
+ /* begin DNG tags */
+ { TIFFTAG_DNGVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DNGVersion", NULL },
+ { TIFFTAG_DNGBACKWARDVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DNGBackwardVersion", NULL },
+ { TIFFTAG_UNIQUECAMERAMODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "UniqueCameraModel", NULL },
+ { TIFFTAG_LOCALIZEDCAMERAMODEL, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "LocalizedCameraModel", NULL },
+ { TIFFTAG_CFAPLANECOLOR, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CFAPlaneColor", NULL },
+ { TIFFTAG_CFALAYOUT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CFALayout", NULL },
+ { TIFFTAG_LINEARIZATIONTABLE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "LinearizationTable", NULL },
+ { TIFFTAG_BLACKLEVELREPEATDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BlackLevelRepeatDim", NULL },
+ { TIFFTAG_BLACKLEVEL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevel", NULL },
+ { TIFFTAG_BLACKLEVELDELTAH, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevelDeltaH", NULL },
+ { TIFFTAG_BLACKLEVELDELTAV, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevelDeltaV", NULL },
+ { TIFFTAG_WHITELEVEL, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "WhiteLevel", NULL },
+ { TIFFTAG_DEFAULTSCALE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultScale", NULL },
+ { TIFFTAG_BESTQUALITYSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BestQualityScale", NULL },
+ { TIFFTAG_DEFAULTCROPORIGIN, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultCropOrigin", NULL },
+ { TIFFTAG_DEFAULTCROPSIZE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultCropSize", NULL },
+ { TIFFTAG_COLORMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ColorMatrix1", NULL },
+ { TIFFTAG_COLORMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ColorMatrix2", NULL },
+ { TIFFTAG_CAMERACALIBRATION1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CameraCalibration1", NULL },
+ { TIFFTAG_CAMERACALIBRATION2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CameraCalibration2", NULL },
+ { TIFFTAG_REDUCTIONMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ReductionMatrix1", NULL },
+ { TIFFTAG_REDUCTIONMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ReductionMatrix2", NULL },
+ { TIFFTAG_ANALOGBALANCE, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AnalogBalance", NULL },
+ { TIFFTAG_ASSHOTNEUTRAL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotNeutral", NULL },
+ { TIFFTAG_ASSHOTWHITEXY, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "AsShotWhiteXY", NULL },
+ { TIFFTAG_BASELINEEXPOSURE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineExposure", NULL },
+ { TIFFTAG_BASELINENOISE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineNoise", NULL },
+ { TIFFTAG_BASELINESHARPNESS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineSharpness", NULL },
+ { TIFFTAG_BAYERGREENSPLIT, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BayerGreenSplit", NULL },
+ { TIFFTAG_LINEARRESPONSELIMIT, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "LinearResponseLimit", NULL },
+ { TIFFTAG_CAMERASERIALNUMBER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CameraSerialNumber", NULL },
+ { TIFFTAG_LENSINFO, 4, 4, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "LensInfo", NULL },
+ { TIFFTAG_CHROMABLURRADIUS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ChromaBlurRadius", NULL },
+ { TIFFTAG_ANTIALIASSTRENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "AntiAliasStrength", NULL },
+ { TIFFTAG_SHADOWSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ShadowScale", NULL },
+ { TIFFTAG_DNGPRIVATEDATA, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "DNGPrivateData", NULL },
+ { TIFFTAG_MAKERNOTESAFETY, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "MakerNoteSafety", NULL },
+ { TIFFTAG_CALIBRATIONILLUMINANT1, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CalibrationIlluminant1", NULL },
+ { TIFFTAG_CALIBRATIONILLUMINANT2, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CalibrationIlluminant2", NULL },
+ { TIFFTAG_RAWDATAUNIQUEID, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "RawDataUniqueID", NULL },
+ { TIFFTAG_ORIGINALRAWFILENAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "OriginalRawFileName", NULL },
+ { TIFFTAG_ORIGINALRAWFILEDATA, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "OriginalRawFileData", NULL },
+ { TIFFTAG_ACTIVEAREA, 4, 4, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ActiveArea", NULL },
+ { TIFFTAG_MASKEDAREAS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "MaskedAreas", NULL },
+ { TIFFTAG_ASSHOTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotICCProfile", NULL },
+ { TIFFTAG_ASSHOTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotPreProfileMatrix", NULL },
+ { TIFFTAG_CURRENTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CurrentICCProfile", NULL },
+ { TIFFTAG_CURRENTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CurrentPreProfileMatrix", NULL },
+ { TIFFTAG_PERSAMPLE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "PerSample", NULL},
+ /* end DNG tags */
+ /* begin TIFF/FX tags */
+ { TIFFTAG_INDEXED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "Indexed", NULL },
+ { TIFFTAG_GLOBALPARAMETERSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "GlobalParametersIFD", NULL },
+ { TIFFTAG_PROFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ProfileType", NULL },
+ { TIFFTAG_FAXPROFILE, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "FaxProfile", NULL },
+ { TIFFTAG_CODINGMETHODS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CodingMethods", NULL },
+ { TIFFTAG_VERSIONYEAR, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "VersionYear", NULL },
+ { TIFFTAG_MODENUMBER, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ModeNumber", NULL },
+ { TIFFTAG_DECODE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "Decode", NULL },
+ { TIFFTAG_IMAGEBASECOLOR, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ImageBaseColor", NULL },
+ { TIFFTAG_T82OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "T82Options", NULL },
+ { TIFFTAG_STRIPROWCOUNTS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "StripRowCounts", NULL },
+ { TIFFTAG_IMAGELAYER, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ImageLayer", NULL },
+ /* end TIFF/FX tags */
+ /* begin pseudo tags */
+};
+
+static const TIFFField
+exifFields[] = {
+ { EXIFTAG_EXPOSURETIME, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureTime", NULL },
+ { EXIFTAG_FNUMBER, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FNumber", NULL },
+ { EXIFTAG_EXPOSUREPROGRAM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureProgram", NULL },
+ { EXIFTAG_SPECTRALSENSITIVITY, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SpectralSensitivity", NULL },
+ { EXIFTAG_ISOSPEEDRATINGS, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "ISOSpeedRatings", NULL },
+ { EXIFTAG_OECF, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "OptoelectricConversionFactor", NULL },
+ { EXIFTAG_EXIFVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExifVersion", NULL },
+ { EXIFTAG_DATETIMEORIGINAL, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTimeOriginal", NULL },
+ { EXIFTAG_DATETIMEDIGITIZED, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTimeDigitized", NULL },
+ { EXIFTAG_COMPONENTSCONFIGURATION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ComponentsConfiguration", NULL },
+ { EXIFTAG_COMPRESSEDBITSPERPIXEL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CompressedBitsPerPixel", NULL },
+ { EXIFTAG_SHUTTERSPEEDVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ShutterSpeedValue", NULL },
+ { EXIFTAG_APERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ApertureValue", NULL },
+ { EXIFTAG_BRIGHTNESSVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "BrightnessValue", NULL },
+ { EXIFTAG_EXPOSUREBIASVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureBiasValue", NULL },
+ { EXIFTAG_MAXAPERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MaxApertureValue", NULL },
+ { EXIFTAG_SUBJECTDISTANCE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectDistance", NULL },
+ { EXIFTAG_METERINGMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MeteringMode", NULL },
+ { EXIFTAG_LIGHTSOURCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "LightSource", NULL },
+ { EXIFTAG_FLASH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Flash", NULL },
+ { EXIFTAG_FOCALLENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalLength", NULL },
+ { EXIFTAG_SUBJECTAREA, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "SubjectArea", NULL },
+ { EXIFTAG_MAKERNOTE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "MakerNote", NULL },
+ { EXIFTAG_USERCOMMENT, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "UserComment", NULL },
+ { EXIFTAG_SUBSECTIME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTime", NULL },
+ { EXIFTAG_SUBSECTIMEORIGINAL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTimeOriginal", NULL },
+ { EXIFTAG_SUBSECTIMEDIGITIZED, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTimeDigitized", NULL },
+ { EXIFTAG_FLASHPIXVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FlashpixVersion", NULL },
+ { EXIFTAG_COLORSPACE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ColorSpace", NULL },
+ { EXIFTAG_PIXELXDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PixelXDimension", NULL },
+ { EXIFTAG_PIXELYDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PixelYDimension", NULL },
+ { EXIFTAG_RELATEDSOUNDFILE, 13, 13, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "RelatedSoundFile", NULL },
+ { EXIFTAG_FLASHENERGY, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FlashEnergy", NULL },
+ { EXIFTAG_SPATIALFREQUENCYRESPONSE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "SpatialFrequencyResponse", NULL },
+ { EXIFTAG_FOCALPLANEXRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneXResolution", NULL },
+ { EXIFTAG_FOCALPLANEYRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneYResolution", NULL },
+ { EXIFTAG_FOCALPLANERESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneResolutionUnit", NULL },
+ { EXIFTAG_SUBJECTLOCATION, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectLocation", NULL },
+ { EXIFTAG_EXPOSUREINDEX, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureIndex", NULL },
+ { EXIFTAG_SENSINGMETHOD, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SensingMethod", NULL },
+ { EXIFTAG_FILESOURCE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FileSource", NULL },
+ { EXIFTAG_SCENETYPE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SceneType", NULL },
+ { EXIFTAG_CFAPATTERN, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "CFAPattern", NULL },
+ { EXIFTAG_CUSTOMRENDERED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CustomRendered", NULL },
+ { EXIFTAG_EXPOSUREMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureMode", NULL },
+ { EXIFTAG_WHITEBALANCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "WhiteBalance", NULL },
+ { EXIFTAG_DIGITALZOOMRATIO, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DigitalZoomRatio", NULL },
+ { EXIFTAG_FOCALLENGTHIN35MMFILM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalLengthIn35mmFilm", NULL },
+ { EXIFTAG_SCENECAPTURETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SceneCaptureType", NULL },
+ { EXIFTAG_GAINCONTROL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "GainControl", NULL },
+ { EXIFTAG_CONTRAST, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Contrast", NULL },
+ { EXIFTAG_SATURATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Saturation", NULL },
+ { EXIFTAG_SHARPNESS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Sharpness", NULL },
+ { EXIFTAG_DEVICESETTINGDESCRIPTION, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "DeviceSettingDescription", NULL },
+ { EXIFTAG_SUBJECTDISTANCERANGE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectDistanceRange", NULL },
+ { EXIFTAG_IMAGEUNIQUEID, 33, 33, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageUniqueID", NULL }
+};
+
+static const TIFFFieldArray
+tiffFieldArray = { tfiatImage, 0, TIFFArrayCount(tiffFields), (TIFFField*) tiffFields };
+static const TIFFFieldArray
+exifFieldArray = { tfiatExif, 0, TIFFArrayCount(exifFields), (TIFFField*) exifFields };
+
+/*
+ * We have our own local lfind() equivalent to avoid subtle differences
+ * in types passed to lfind() on different systems.
+ */
+
+static void *
+td_lfind(const void *key, const void *base, size_t *nmemb, size_t size,
+ int(*compar)(const void *, const void *))
+{
+ char *element, *end;
+
+ end = (char *)base + *nmemb * size;
+ for (element = (char *)base; element < end; element += size)
+ if (!compar(key, element)) /* key found */
+ return element;
+
+ return NULL;
+}
+
+const TIFFFieldArray*
+_TIFFGetFields(void)
+{
+ return(&tiffFieldArray);
+}
+
+const TIFFFieldArray*
+_TIFFGetExifFields(void)
+{
+ return(&exifFieldArray);
+}
+
+void
+_TIFFSetupFields(TIFF* tif, const TIFFFieldArray* fieldarray)
+{
+ if (tif->tif_fields && tif->tif_nfields > 0) {
+ uint32 i;
+
+ for (i = 0; i < tif->tif_nfields; i++) {
+ TIFFField *fld = tif->tif_fields[i];
+ if (fld->field_bit == FIELD_CUSTOM &&
+ strncmp("Tag ", fld->field_name, 4) == 0) {
+ _TIFFfree(fld->field_name);
+ _TIFFfree(fld);
+ }
+ }
+
+ _TIFFfree(tif->tif_fields);
+ tif->tif_fields = NULL;
+ tif->tif_nfields = 0;
+ }
+ if (!_TIFFMergeFields(tif, fieldarray->fields, fieldarray->count)) {
+ TIFFErrorExt(tif->tif_clientdata, "_TIFFSetupFields",
+ "Setting up field info failed");
+ }
+}
+
+static int
+tagCompare(const void* a, const void* b)
+{
+ const TIFFField* ta = *(const TIFFField**) a;
+ const TIFFField* tb = *(const TIFFField**) b;
+ /* NB: be careful of return values for 16-bit platforms */
+ if (ta->field_tag != tb->field_tag)
+ return (int)ta->field_tag - (int)tb->field_tag;
+ else
+ return (ta->field_type == TIFF_ANY) ?
+ 0 : ((int)tb->field_type - (int)ta->field_type);
+}
+
+static int
+tagNameCompare(const void* a, const void* b)
+{
+ const TIFFField* ta = *(const TIFFField**) a;
+ const TIFFField* tb = *(const TIFFField**) b;
+ int ret = strcmp(ta->field_name, tb->field_name);
+
+ if (ret)
+ return ret;
+ else
+ return (ta->field_type == TIFF_ANY) ?
+ 0 : ((int)tb->field_type - (int)ta->field_type);
+}
+
+int
+_TIFFMergeFields(TIFF* tif, const TIFFField info[], uint32 n)
+{
+ static const char module[] = "_TIFFMergeFields";
+ static const char reason[] = "for fields array";
+ /* TIFFField** tp; */
+ uint32 i;
+
+ tif->tif_foundfield = NULL;
+
+ if (tif->tif_fields && tif->tif_nfields > 0) {
+ tif->tif_fields = (TIFFField**)
+ _TIFFCheckRealloc(tif, tif->tif_fields,
+ (tif->tif_nfields + n),
+ sizeof(TIFFField *), reason);
+ } else {
+ tif->tif_fields = (TIFFField **)
+ _TIFFCheckMalloc(tif, n, sizeof(TIFFField *),
+ reason);
+ }
+ if (!tif->tif_fields) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Failed to allocate fields array");
+ return 0;
+ }
+
+ /* tp = tif->tif_fields + tif->tif_nfields; */
+ for (i = 0; i < n; i++) {
+ const TIFFField *fip =
+ TIFFFindField(tif, info[i].field_tag, TIFF_ANY);
+
+ /* only add definitions that aren't already present */
+ if (!fip) {
+ tif->tif_fields[tif->tif_nfields] = (TIFFField *) (info+i);
+ tif->tif_nfields++;
+ }
+ }
+
+ /* Sort the field info by tag number */
+ qsort(tif->tif_fields, tif->tif_nfields,
+ sizeof(TIFFField *), tagCompare);
+
+ return n;
+}
+
+void
+_TIFFPrintFieldInfo(TIFF* tif, FILE* fd)
+{
+ uint32 i;
+
+ fprintf(fd, "%s: \n", tif->tif_name);
+ for (i = 0; i < tif->tif_nfields; i++) {
+ const TIFFField* fip = tif->tif_fields[i];
+ fprintf(fd, "field[%2d] %5lu, %2d, %2d, %d, %2d, %5s, %5s, %s\n"
+ , (int)i
+ , (unsigned long) fip->field_tag
+ , fip->field_readcount, fip->field_writecount
+ , fip->field_type
+ , fip->field_bit
+ , fip->field_oktochange ? "TRUE" : "FALSE"
+ , fip->field_passcount ? "TRUE" : "FALSE"
+ , fip->field_name
+ );
+ }
+}
+
+/*
+ * Return size of TIFFDataType in bytes
+ */
+int
+TIFFDataWidth(TIFFDataType type)
+{
+ switch(type)
+ {
+ case 0: /* nothing */
+ case TIFF_BYTE:
+ case TIFF_ASCII:
+ case TIFF_SBYTE:
+ case TIFF_UNDEFINED:
+ return 1;
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ return 2;
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_FLOAT:
+ case TIFF_IFD:
+ return 4;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_DOUBLE:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ case TIFF_IFD8:
+ return 8;
+ default:
+ return 0; /* will return 0 for unknown types */
+ }
+}
+
+/*
+ * Return size of TIFFDataType in bytes.
+ *
+ * XXX: We need a separate function to determine the space needed
+ * to store the value. For TIFF_RATIONAL values TIFFDataWidth() returns 8,
+ * but we use 4-byte float to represent rationals.
+ */
+int
+_TIFFDataSize(TIFFDataType type)
+{
+ switch (type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_ASCII:
+ case TIFF_UNDEFINED:
+ return 1;
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ return 2;
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_FLOAT:
+ case TIFF_IFD:
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ return 4;
+ case TIFF_DOUBLE:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ case TIFF_IFD8:
+ return 8;
+ default:
+ return 0;
+ }
+}
+
+const TIFFField*
+TIFFFindField(TIFF* tif, uint32 tag, TIFFDataType dt)
+{
+ TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, 0, NULL, NULL};
+ TIFFField* pkey = &key;
+ const TIFFField **ret;
+ if (tif->tif_foundfield && tif->tif_foundfield->field_tag == tag &&
+ (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
+ return tif->tif_foundfield;
+
+ /* If we are invoked with no field information, then just return. */
+ if (!tif->tif_fields)
+ return NULL;
+
+ /* NB: use sorted search (e.g. binary search) */
+
+ key.field_tag = tag;
+ key.field_type = dt;
+
+ ret = (const TIFFField **) bsearch(&pkey, tif->tif_fields,
+ tif->tif_nfields,
+ sizeof(TIFFField *), tagCompare);
+ return tif->tif_foundfield = (ret ? *ret : NULL);
+}
+
+static const TIFFField*
+_TIFFFindFieldByName(TIFF* tif, const char *field_name, TIFFDataType dt)
+{
+ TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, 0, NULL, NULL};
+ TIFFField* pkey = &key;
+ const TIFFField **ret;
+ if (tif->tif_foundfield
+ && streq(tif->tif_foundfield->field_name, field_name)
+ && (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
+ return (tif->tif_foundfield);
+
+ /* If we are invoked with no field information, then just return. */
+ if (!tif->tif_fields)
+ return NULL;
+
+ /* NB: use linear search since list is sorted by key#, not name */
+
+ key.field_name = (char *)field_name;
+ key.field_type = dt;
+
+ ret = (const TIFFField **)
+ td_lfind(&pkey, tif->tif_fields, &tif->tif_nfields,
+ sizeof(TIFFField *), tagNameCompare);
+
+ return tif->tif_foundfield = (ret ? *ret : NULL);
+}
+
+const TIFFField*
+TIFFFieldWithTag(TIFF* tif, uint32 tag)
+{
+ const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
+ if (!fip) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithTag",
+ "Internal error, unknown tag 0x%x",
+ (unsigned int) tag);
+ }
+ return (fip);
+}
+
+const TIFFField*
+TIFFFieldWithName(TIFF* tif, const char *field_name)
+{
+ const TIFFField* fip =
+ _TIFFFindFieldByName(tif, field_name, TIFF_ANY);
+ if (!fip) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithName",
+ "Internal error, unknown tag %s", field_name);
+ }
+ return (fip);
+}
+
+uint32
+TIFFFieldTag(const TIFFField* fip)
+{
+ return fip->field_tag;
+}
+
+const char *
+TIFFFieldName(const TIFFField* fip)
+{
+ return fip->field_name;
+}
+
+TIFFDataType
+TIFFFieldDataType(const TIFFField* fip)
+{
+ return fip->field_type;
+}
+
+int
+TIFFFieldPassCount(const TIFFField* fip)
+{
+ return fip->field_passcount;
+}
+
+int
+TIFFFieldReadCount(const TIFFField* fip)
+{
+ return fip->field_readcount;
+}
+
+int
+TIFFFieldWriteCount(const TIFFField* fip)
+{
+ return fip->field_writecount;
+}
+
+const TIFFField*
+_TIFFFindOrRegisterField(TIFF *tif, uint32 tag, TIFFDataType dt)
+
+{
+ const TIFFField *fld;
+
+ fld = TIFFFindField(tif, tag, dt);
+ if (fld == NULL) {
+ fld = _TIFFCreateAnonField(tif, tag, dt);
+ if (!_TIFFMergeFields(tif, fld, 1))
+ return NULL;
+ }
+
+ return fld;
+}
+
+TIFFField*
+_TIFFCreateAnonField(TIFF *tif, uint32 tag, TIFFDataType field_type)
+{
+ TIFFField *fld;
+ (void) tif;
+
+ fld = (TIFFField *) _TIFFmalloc(sizeof (TIFFField));
+ if (fld == NULL)
+ return NULL;
+ _TIFFmemset(fld, 0, sizeof(TIFFField));
+
+ fld->field_tag = tag;
+ fld->field_readcount = TIFF_VARIABLE2;
+ fld->field_writecount = TIFF_VARIABLE2;
+ fld->field_type = field_type;
+ fld->reserved = 0;
+ switch (field_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ fld->set_field_type = TIFF_SETGET_C32_UINT8;
+ fld->get_field_type = TIFF_SETGET_C32_UINT8;
+ break;
+ case TIFF_ASCII:
+ fld->set_field_type = TIFF_SETGET_C32_ASCII;
+ fld->get_field_type = TIFF_SETGET_C32_ASCII;
+ break;
+ case TIFF_SHORT:
+ fld->set_field_type = TIFF_SETGET_C32_UINT16;
+ fld->get_field_type = TIFF_SETGET_C32_UINT16;
+ break;
+ case TIFF_LONG:
+ fld->set_field_type = TIFF_SETGET_C32_UINT32;
+ fld->get_field_type = TIFF_SETGET_C32_UINT32;
+ break;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ fld->set_field_type = TIFF_SETGET_C32_FLOAT;
+ fld->get_field_type = TIFF_SETGET_C32_FLOAT;
+ break;
+ case TIFF_SBYTE:
+ fld->set_field_type = TIFF_SETGET_C32_SINT8;
+ fld->get_field_type = TIFF_SETGET_C32_SINT8;
+ break;
+ case TIFF_SSHORT:
+ fld->set_field_type = TIFF_SETGET_C32_SINT16;
+ fld->get_field_type = TIFF_SETGET_C32_SINT16;
+ break;
+ case TIFF_SLONG:
+ fld->set_field_type = TIFF_SETGET_C32_SINT32;
+ fld->get_field_type = TIFF_SETGET_C32_SINT32;
+ break;
+ case TIFF_DOUBLE:
+ fld->set_field_type = TIFF_SETGET_C32_DOUBLE;
+ fld->get_field_type = TIFF_SETGET_C32_DOUBLE;
+ break;
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ fld->set_field_type = TIFF_SETGET_C32_IFD8;
+ fld->get_field_type = TIFF_SETGET_C32_IFD8;
+ break;
+ case TIFF_LONG8:
+ fld->set_field_type = TIFF_SETGET_C32_UINT64;
+ fld->get_field_type = TIFF_SETGET_C32_UINT64;
+ break;
+ case TIFF_SLONG8:
+ fld->set_field_type = TIFF_SETGET_C32_SINT64;
+ fld->get_field_type = TIFF_SETGET_C32_SINT64;
+ break;
+ default:
+ fld->set_field_type = TIFF_SETGET_UNDEFINED;
+ fld->get_field_type = TIFF_SETGET_UNDEFINED;
+ break;
+ }
+ fld->field_bit = FIELD_CUSTOM;
+ fld->field_oktochange = TRUE;
+ fld->field_passcount = TRUE;
+ fld->field_name = (char *) _TIFFmalloc(32);
+ if (fld->field_name == NULL) {
+ _TIFFfree(fld);
+ return NULL;
+ }
+ fld->field_subfields = NULL;
+
+ /*
+ * note that this name is a special sign to TIFFClose() and
+ * _TIFFSetupFields() to free the field
+ */
+ (void) snprintf(fld->field_name, 32, "Tag %d", (int) tag);
+
+ return fld;
+}
+
+/****************************************************************************
+ * O B S O L E T E D I N T E R F A C E S
+ *
+ * Don't use this stuff in your applications, it may be removed in the future
+ * libtiff versions.
+ ****************************************************************************/
+
+static TIFFSetGetFieldType
+_TIFFSetGetType(TIFFDataType type, short count, unsigned char passcount)
+{
+ if (type == TIFF_ASCII && count == TIFF_VARIABLE && passcount == 0)
+ return TIFF_SETGET_ASCII;
+
+ else if (count == 1 && passcount == 0) {
+ switch (type)
+ {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ return TIFF_SETGET_UINT8;
+ case TIFF_ASCII:
+ return TIFF_SETGET_ASCII;
+ case TIFF_SHORT:
+ return TIFF_SETGET_UINT16;
+ case TIFF_LONG:
+ return TIFF_SETGET_UINT32;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ return TIFF_SETGET_FLOAT;
+ case TIFF_SBYTE:
+ return TIFF_SETGET_SINT8;
+ case TIFF_SSHORT:
+ return TIFF_SETGET_SINT16;
+ case TIFF_SLONG:
+ return TIFF_SETGET_SINT32;
+ case TIFF_DOUBLE:
+ return TIFF_SETGET_DOUBLE;
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ return TIFF_SETGET_IFD8;
+ case TIFF_LONG8:
+ return TIFF_SETGET_UINT64;
+ case TIFF_SLONG8:
+ return TIFF_SETGET_SINT64;
+ default:
+ return TIFF_SETGET_UNDEFINED;
+ }
+ }
+
+ else if (count >= 1 && passcount == 0) {
+ switch (type)
+ {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ return TIFF_SETGET_C0_UINT8;
+ case TIFF_ASCII:
+ return TIFF_SETGET_C0_ASCII;
+ case TIFF_SHORT:
+ return TIFF_SETGET_C0_UINT16;
+ case TIFF_LONG:
+ return TIFF_SETGET_C0_UINT32;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ return TIFF_SETGET_C0_FLOAT;
+ case TIFF_SBYTE:
+ return TIFF_SETGET_C0_SINT8;
+ case TIFF_SSHORT:
+ return TIFF_SETGET_C0_SINT16;
+ case TIFF_SLONG:
+ return TIFF_SETGET_C0_SINT32;
+ case TIFF_DOUBLE:
+ return TIFF_SETGET_C0_DOUBLE;
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ return TIFF_SETGET_C0_IFD8;
+ case TIFF_LONG8:
+ return TIFF_SETGET_C0_UINT64;
+ case TIFF_SLONG8:
+ return TIFF_SETGET_C0_SINT64;
+ default:
+ return TIFF_SETGET_UNDEFINED;
+ }
+ }
+
+ else if (count == TIFF_VARIABLE && passcount == 1) {
+ switch (type)
+ {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ return TIFF_SETGET_C16_UINT8;
+ case TIFF_ASCII:
+ return TIFF_SETGET_C16_ASCII;
+ case TIFF_SHORT:
+ return TIFF_SETGET_C16_UINT16;
+ case TIFF_LONG:
+ return TIFF_SETGET_C16_UINT32;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ return TIFF_SETGET_C16_FLOAT;
+ case TIFF_SBYTE:
+ return TIFF_SETGET_C16_SINT8;
+ case TIFF_SSHORT:
+ return TIFF_SETGET_C16_SINT16;
+ case TIFF_SLONG:
+ return TIFF_SETGET_C16_SINT32;
+ case TIFF_DOUBLE:
+ return TIFF_SETGET_C16_DOUBLE;
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ return TIFF_SETGET_C16_IFD8;
+ case TIFF_LONG8:
+ return TIFF_SETGET_C16_UINT64;
+ case TIFF_SLONG8:
+ return TIFF_SETGET_C16_SINT64;
+ default:
+ return TIFF_SETGET_UNDEFINED;
+ }
+ }
+
+ else if (count == TIFF_VARIABLE2 && passcount == 1) {
+ switch (type)
+ {
+ case TIFF_BYTE:
+ case TIFF_UNDEFINED:
+ return TIFF_SETGET_C32_UINT8;
+ case TIFF_ASCII:
+ return TIFF_SETGET_C32_ASCII;
+ case TIFF_SHORT:
+ return TIFF_SETGET_C32_UINT16;
+ case TIFF_LONG:
+ return TIFF_SETGET_C32_UINT32;
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ return TIFF_SETGET_C32_FLOAT;
+ case TIFF_SBYTE:
+ return TIFF_SETGET_C32_SINT8;
+ case TIFF_SSHORT:
+ return TIFF_SETGET_C32_SINT16;
+ case TIFF_SLONG:
+ return TIFF_SETGET_C32_SINT32;
+ case TIFF_DOUBLE:
+ return TIFF_SETGET_C32_DOUBLE;
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ return TIFF_SETGET_C32_IFD8;
+ case TIFF_LONG8:
+ return TIFF_SETGET_C32_UINT64;
+ case TIFF_SLONG8:
+ return TIFF_SETGET_C32_SINT64;
+ default:
+ return TIFF_SETGET_UNDEFINED;
+ }
+ }
+
+ return TIFF_SETGET_UNDEFINED;
+}
+
+int
+TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], uint32 n)
+{
+ static const char module[] = "TIFFMergeFieldInfo";
+ static const char reason[] = "for fields array";
+ TIFFField *tp;
+ size_t nfields;
+ uint32 i;
+
+ if (tif->tif_nfieldscompat > 0) {
+ tif->tif_fieldscompat = (TIFFFieldArray *)
+ _TIFFCheckRealloc(tif, tif->tif_fieldscompat,
+ tif->tif_nfieldscompat + 1,
+ sizeof(TIFFFieldArray), reason);
+ } else {
+ tif->tif_fieldscompat = (TIFFFieldArray *)
+ _TIFFCheckMalloc(tif, 1, sizeof(TIFFFieldArray),
+ reason);
+ }
+ if (!tif->tif_fieldscompat) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Failed to allocate fields array");
+ return -1;
+ }
+ nfields = tif->tif_nfieldscompat++;
+
+ tif->tif_fieldscompat[nfields].type = tfiatOther;
+ tif->tif_fieldscompat[nfields].allocated_size = n;
+ tif->tif_fieldscompat[nfields].count = n;
+ tif->tif_fieldscompat[nfields].fields =
+ (TIFFField *)_TIFFCheckMalloc(tif, n, sizeof(TIFFField),
+ reason);
+ if (!tif->tif_fieldscompat[nfields].fields) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Failed to allocate fields array");
+ return -1;
+ }
+
+ tp = tif->tif_fieldscompat[nfields].fields;
+ for (i = 0; i < n; i++) {
+ tp->field_tag = info[i].field_tag;
+ tp->field_readcount = info[i].field_readcount;
+ tp->field_writecount = info[i].field_writecount;
+ tp->field_type = info[i].field_type;
+ tp->reserved = 0;
+ tp->set_field_type =
+ _TIFFSetGetType(info[i].field_type,
+ info[i].field_readcount,
+ info[i].field_passcount);
+ tp->get_field_type =
+ _TIFFSetGetType(info[i].field_type,
+ info[i].field_readcount,
+ info[i].field_passcount);
+ tp->field_bit = info[i].field_bit;
+ tp->field_oktochange = info[i].field_oktochange;
+ tp->field_passcount = info[i].field_passcount;
+ tp->field_name = info[i].field_name;
+ tp->field_subfields = NULL;
+ tp++;
+ }
+
+ if (!_TIFFMergeFields(tif, tif->tif_fieldscompat[nfields].fields, n)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Setting up field info failed");
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+_TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
+{
+ /* Filter out non-codec specific tags */
+ switch (tag) {
+ /* Shared tags */
+ case TIFFTAG_PREDICTOR:
+ /* JPEG tags */
+ case TIFFTAG_JPEGTABLES:
+ /* OJPEG tags */
+ case TIFFTAG_JPEGIFOFFSET:
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ case TIFFTAG_JPEGQTABLES:
+ case TIFFTAG_JPEGDCTABLES:
+ case TIFFTAG_JPEGACTABLES:
+ case TIFFTAG_JPEGPROC:
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ /* CCITT* */
+ case TIFFTAG_BADFAXLINES:
+ case TIFFTAG_CLEANFAXDATA:
+ case TIFFTAG_CONSECUTIVEBADFAXLINES:
+ case TIFFTAG_GROUP3OPTIONS:
+ case TIFFTAG_GROUP4OPTIONS:
+ /* LERC */
+ case TIFFTAG_LERC_PARAMETERS:
+ break;
+ default:
+ return 1;
+ }
+ /* Check if codec specific tags are allowed for the current
+ * compression scheme (codec) */
+ switch (tif->tif_dir.td_compression) {
+ case COMPRESSION_LZW:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ case COMPRESSION_PACKBITS:
+ /* No codec-specific tags */
+ break;
+ case COMPRESSION_THUNDERSCAN:
+ /* No codec-specific tags */
+ break;
+ case COMPRESSION_NEXT:
+ /* No codec-specific tags */
+ break;
+ case COMPRESSION_JPEG:
+ if (tag == TIFFTAG_JPEGTABLES)
+ return 1;
+ break;
+ case COMPRESSION_OJPEG:
+ switch (tag) {
+ case TIFFTAG_JPEGIFOFFSET:
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ case TIFFTAG_JPEGQTABLES:
+ case TIFFTAG_JPEGDCTABLES:
+ case TIFFTAG_JPEGACTABLES:
+ case TIFFTAG_JPEGPROC:
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ return 1;
+ }
+ break;
+ case COMPRESSION_CCITTRLE:
+ case COMPRESSION_CCITTRLEW:
+ case COMPRESSION_CCITTFAX3:
+ case COMPRESSION_CCITTFAX4:
+ switch (tag) {
+ case TIFFTAG_BADFAXLINES:
+ case TIFFTAG_CLEANFAXDATA:
+ case TIFFTAG_CONSECUTIVEBADFAXLINES:
+ return 1;
+ case TIFFTAG_GROUP3OPTIONS:
+ if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
+ return 1;
+ break;
+ case TIFFTAG_GROUP4OPTIONS:
+ if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
+ return 1;
+ break;
+ }
+ break;
+ case COMPRESSION_JBIG:
+ /* No codec-specific tags */
+ break;
+ case COMPRESSION_DEFLATE:
+ case COMPRESSION_ADOBE_DEFLATE:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ case COMPRESSION_PIXARLOG:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ case COMPRESSION_SGILOG:
+ case COMPRESSION_SGILOG24:
+ /* No codec-specific tags */
+ break;
+ case COMPRESSION_LZMA:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ case COMPRESSION_ZSTD:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ case COMPRESSION_LERC:
+ if (tag == TIFFTAG_LERC_PARAMETERS)
+ return 1;
+ break;
+ case COMPRESSION_WEBP:
+ if (tag == TIFFTAG_PREDICTOR)
+ return 1;
+ break;
+ }
+ return 0;
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dirread.c b/test/monniaux/tiff-4.0.10/tif_dirread.c
new file mode 100644
index 00000000..e80a3b13
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dirread.c
@@ -0,0 +1,5874 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Directory Read Support Routines.
+ */
+
+/* Suggested pending improvements:
+ * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
+ * eliminating current use of the IGNORE value, and therefore eliminating
+ * current irrational behaviour on tags with tag id code 0
+ * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
+ * the pointer to the appropriate TIFFField structure early on in
+ * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
+ */
+
+#include "tiffiop.h"
+#include <float.h>
+#include <stdlib.h>
+
+#define IGNORE 0 /* tag placeholder used below */
+#define FAILED_FII ((uint32) -1)
+
+#ifdef HAVE_IEEEFP
+# define TIFFCvtIEEEFloatToNative(tif, n, fp)
+# define TIFFCvtIEEEDoubleToNative(tif, n, dp)
+#else
+extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
+extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
+#endif
+
+enum TIFFReadDirEntryErr {
+ TIFFReadDirEntryErrOk = 0,
+ TIFFReadDirEntryErrCount = 1,
+ TIFFReadDirEntryErrType = 2,
+ TIFFReadDirEntryErrIo = 3,
+ TIFFReadDirEntryErrRange = 4,
+ TIFFReadDirEntryErrPsdif = 5,
+ TIFFReadDirEntryErrSizesan = 6,
+ TIFFReadDirEntryErrAlloc = 7,
+};
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
+#if 0
+static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
+#endif
+
+static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
+static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
+static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
+static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
+static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
+static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
+static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
+static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
+
+static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
+static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
+static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
+
+static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
+static void MissingRequired(TIFF*, const char*);
+static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
+static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
+static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
+static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
+static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
+static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
+static void ChopUpSingleUncompressedStrip(TIFF*);
+static uint64 TIFFReadUInt64(const uint8 *value);
+static int _TIFFGetMaxColorChannels(uint16 photometric);
+
+static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
+
+typedef union _UInt64Aligned_t
+{
+ double d;
+ uint64 l;
+ uint32 i[2];
+ uint16 s[4];
+ uint8 c[8];
+} UInt64Aligned_t;
+
+/*
+ Unaligned safe copy of a uint64 value from an octet array.
+*/
+static uint64 TIFFReadUInt64(const uint8 *value)
+{
+ UInt64Aligned_t result;
+
+ result.c[0]=value[0];
+ result.c[1]=value[1];
+ result.c[2]=value[2];
+ result.c[3]=value[3];
+ result.c[4]=value[4];
+ result.c[5]=value[5];
+ result.c[6]=value[6];
+ result.c[7]=value[7];
+
+ return result.l;
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ TIFFReadDirEntryCheckedByte(tif,direntry,value);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeByteSbyte(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ {
+ uint16 m;
+ TIFFReadDirEntryCheckedShort(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeByteShort(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeByteSshort(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeByteLong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeByteSlong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ {
+ uint64 m;
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeByteLong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeByteSlong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint8)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8 m;
+ TIFFReadDirEntryCheckedByte(tif,direntry,&m);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeShortSbyte(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ TIFFReadDirEntryCheckedShort(tif,direntry,value);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeShortSshort(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeShortLong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeShortSlong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ {
+ uint64 m;
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeShortLong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeShortSlong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint16)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8 m;
+ TIFFReadDirEntryCheckedByte(tif,direntry,&m);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLongSbyte(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ {
+ uint16 m;
+ TIFFReadDirEntryCheckedShort(tif,direntry,&m);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLongSshort(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ TIFFReadDirEntryCheckedLong(tif,direntry,value);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLongSlong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ {
+ uint64 m;
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeLongLong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeLongSlong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint32)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8 m;
+ TIFFReadDirEntryCheckedByte(tif,direntry,&m);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ {
+ uint16 m;
+ TIFFReadDirEntryCheckedShort(tif,direntry,&m);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ err=TIFFReadDirEntryCheckRangeLong8Slong(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
+ return(err);
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8 m;
+ TIFFReadDirEntryCheckedByte(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ {
+ uint16 m;
+ TIFFReadDirEntryCheckedShort(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ {
+ uint64 m;
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+#if defined(__WIN32__) && (_MSC_VER < 1500)
+ /*
+ * XXX: MSVC 6.0 does not support conversion
+ * of 64-bit integers into floating point
+ * values.
+ */
+ *value = _TIFFUInt64ToFloat(m);
+#else
+ *value=(float)m;
+#endif
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_RATIONAL:
+ {
+ double m;
+ err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SRATIONAL:
+ {
+ double m;
+ err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_FLOAT:
+ TIFFReadDirEntryCheckedFloat(tif,direntry,value);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_DOUBLE:
+ {
+ double m;
+ err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ if ((m > FLT_MAX) || (m < FLT_MIN))
+ return(TIFFReadDirEntryErrRange);
+ *value=(float)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8 m;
+ TIFFReadDirEntryCheckedByte(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ {
+ int8 m;
+ TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SHORT:
+ {
+ uint16 m;
+ TIFFReadDirEntryCheckedShort(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ {
+ int16 m;
+ TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ {
+ int32 m;
+ TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ {
+ uint64 m;
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+#if defined(__WIN32__) && (_MSC_VER < 1500)
+ /*
+ * XXX: MSVC 6.0 does not support conversion
+ * of 64-bit integers into floating point
+ * values.
+ */
+ *value = _TIFFUInt64ToDouble(m);
+#else
+ *value = (double)m;
+#endif
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ {
+ int64 m;
+ err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_RATIONAL:
+ err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
+ return(err);
+ case TIFF_SRATIONAL:
+ err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
+ return(err);
+ case TIFF_FLOAT:
+ {
+ float m;
+ TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
+ *value=(double)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_DOUBLE:
+ err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
+ return(err);
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
+{
+ enum TIFFReadDirEntryErr err;
+ if (direntry->tdir_count!=1)
+ return(TIFFReadDirEntryErrCount);
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG:
+ case TIFF_IFD:
+ {
+ uint32 m;
+ TIFFReadDirEntryCheckedLong(tif,direntry,&m);
+ *value=(uint64)m;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_LONG8:
+ case TIFF_IFD8:
+ err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
+ return(err);
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+}
+
+
+#define INITIAL_THRESHOLD (1024 * 1024)
+#define THRESHOLD_MULTIPLIER 10
+#define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
+ TIFF* tif, uint64 offset, tmsize_t size, void** pdest)
+{
+#if SIZEOF_SIZE_T == 8
+ tmsize_t threshold = INITIAL_THRESHOLD;
+#endif
+ tmsize_t already_read = 0;
+
+ assert( !isMapped(tif) );
+
+ if (!SeekOK(tif,offset))
+ return(TIFFReadDirEntryErrIo);
+
+ /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
+ /* so as to avoid allocating too much memory in case the file is too */
+ /* short. We could ask for the file size, but this might be */
+ /* expensive with some I/O layers (think of reading a gzipped file) */
+ /* Restrict to 64 bit processes, so as to avoid reallocs() */
+ /* on 32 bit processes where virtual memory is scarce. */
+ while( already_read < size )
+ {
+ void* new_dest;
+ tmsize_t bytes_read;
+ tmsize_t to_read = size - already_read;
+#if SIZEOF_SIZE_T == 8
+ if( to_read >= threshold && threshold < MAX_THRESHOLD )
+ {
+ to_read = threshold;
+ threshold *= THRESHOLD_MULTIPLIER;
+ }
+#endif
+
+ new_dest = (uint8*) _TIFFrealloc(
+ *pdest, already_read + to_read);
+ if( new_dest == NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Failed to allocate memory for %s "
+ "(%ld elements of %ld bytes each)",
+ "TIFFReadDirEntryArray",
+ (long) 1, (long) (already_read + to_read));
+ return TIFFReadDirEntryErrAlloc;
+ }
+ *pdest = new_dest;
+
+ bytes_read = TIFFReadFile(tif,
+ (char*)*pdest + already_read, to_read);
+ already_read += bytes_read;
+ if (bytes_read != to_read) {
+ return TIFFReadDirEntryErrIo;
+ }
+ }
+ return TIFFReadDirEntryErrOk;
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(
+ TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize,
+ void** value, uint64 maxcount)
+{
+ int typesize;
+ uint32 datasize;
+ void* data;
+ uint64 target_count64;
+ typesize=TIFFDataWidth(direntry->tdir_type);
+
+ target_count64 = (direntry->tdir_count > maxcount) ?
+ maxcount : direntry->tdir_count;
+
+ if ((target_count64==0)||(typesize==0))
+ {
+ *value=0;
+ return(TIFFReadDirEntryErrOk);
+ }
+ (void) desttypesize;
+
+ /*
+ * As a sanity check, make sure we have no more than a 2GB tag array
+ * in either the current data type or the dest data type. This also
+ * avoids problems with overflow of tmsize_t on 32bit systems.
+ */
+ if ((uint64)(2147483647/typesize)<target_count64)
+ return(TIFFReadDirEntryErrSizesan);
+ if ((uint64)(2147483647/desttypesize)<target_count64)
+ return(TIFFReadDirEntryErrSizesan);
+
+ *count=(uint32)target_count64;
+ datasize=(*count)*typesize;
+ assert((tmsize_t)datasize>0);
+
+ if( isMapped(tif) && datasize > (uint32)tif->tif_size )
+ return TIFFReadDirEntryErrIo;
+
+ if( !isMapped(tif) &&
+ (((tif->tif_flags&TIFF_BIGTIFF) && datasize > 8) ||
+ (!(tif->tif_flags&TIFF_BIGTIFF) && datasize > 4)) )
+ {
+ data = NULL;
+ }
+ else
+ {
+ data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
+ if (data==0)
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (datasize<=4)
+ _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
+ else
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ if( isMapped(tif) )
+ err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
+ else
+ err=TIFFReadDirEntryDataAndRealloc(tif,(uint64)offset,(tmsize_t)datasize,&data);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ }
+ }
+ else
+ {
+ if (datasize<=8)
+ _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
+ else
+ {
+ enum TIFFReadDirEntryErr err;
+ uint64 offset = direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(&offset);
+ if( isMapped(tif) )
+ err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
+ else
+ err=TIFFReadDirEntryDataAndRealloc(tif,(uint64)offset,(tmsize_t)datasize,&data);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ }
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
+{
+ return TIFFReadDirEntryArrayWithLimit(tif, direntry, count,
+ desttypesize, value, ~((uint64)0));
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ uint8* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_ASCII:
+ case TIFF_UNDEFINED:
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_ASCII:
+ case TIFF_UNDEFINED:
+ case TIFF_BYTE:
+ *value=(uint8*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SBYTE:
+ {
+ int8* m;
+ uint32 n;
+ m=(int8*)origdata;
+ for (n=0; n<count; n++)
+ {
+ err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(uint8*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ }
+ data=(uint8*)_TIFFmalloc(count);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ err=TIFFReadDirEntryCheckRangeByteShort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ err=TIFFReadDirEntryCheckRangeByteLong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ uint8* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint8)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ int8* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_UNDEFINED:
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_UNDEFINED:
+ case TIFF_BYTE:
+ {
+ uint8* m;
+ uint32 n;
+ m=(uint8*)origdata;
+ for (n=0; n<count; n++)
+ {
+ err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(int8*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SBYTE:
+ *value=(int8*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(int8*)_TIFFmalloc(count);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ int8* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ int8* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ int8* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ int8* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ int8* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ int8* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int8)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ uint16* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_SHORT:
+ *value=(uint16*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfShort(*value,count);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SSHORT:
+ {
+ int16* m;
+ uint32 n;
+ m=(int16*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)m);
+ err=TIFFReadDirEntryCheckRangeShortSshort(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(uint16*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ }
+ data=(uint16*)_TIFFmalloc(count*2);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(uint16)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ err=TIFFReadDirEntryCheckRangeShortLong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ uint16* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint16)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ int16* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_SHORT:
+ {
+ uint16* m;
+ uint32 n;
+ m=(uint16*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(m);
+ err=TIFFReadDirEntryCheckRangeSshortShort(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(int16*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SSHORT:
+ *value=(int16*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfShort((uint16*)(*value),count);
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(int16*)_TIFFmalloc(count*2);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ int16* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int16)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ int16* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int16)(*ma++);
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ int16* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ int16* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ int16* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int16)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ int16* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int16)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ uint32* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG:
+ *value=(uint32*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(*value,count);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SLONG:
+ {
+ int32* m;
+ uint32 n;
+ m=(int32*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)m);
+ err=TIFFReadDirEntryCheckRangeLongSlong(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(uint32*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ }
+ data=(uint32*)_TIFFmalloc(count*4);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(uint32)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(uint32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ uint32* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint32)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ int32* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG:
+ {
+ uint32* m;
+ uint32 n;
+ m=(uint32*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)m);
+ err=TIFFReadDirEntryCheckRangeSlongLong(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(int32*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG:
+ *value=(int32*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong((uint32*)(*value),count);
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(int32*)_TIFFmalloc(count*4);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ int32* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int32)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ int32* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int32)(*ma++);
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ int32* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(int32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ int32* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ *mb++=(int32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ int32* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+ err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int32)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ int32* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(int32)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
+ TIFF* tif, TIFFDirEntry* direntry, uint64** value, uint64 maxcount)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ uint64* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArrayWithLimit(tif,direntry,&count,8,&origdata,maxcount);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG8:
+ *value=(uint64*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8(*value,count);
+ return(TIFFReadDirEntryErrOk);
+ case TIFF_SLONG8:
+ {
+ int64* m;
+ uint32 n;
+ m=(int64*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)m);
+ err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(uint64*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ }
+ data=(uint64*)_TIFFmalloc(count*8);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(uint64)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
+ if (err!=TIFFReadDirEntryErrOk)
+ break;
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(data);
+ return(err);
+ }
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
+{
+ return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value, ~((uint64)0));
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ int64* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG8:
+ {
+ uint64* m;
+ uint32 n;
+ m=(uint64*)origdata;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(m);
+ err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ _TIFFfree(origdata);
+ return(err);
+ }
+ m++;
+ }
+ *value=(int64*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ case TIFF_SLONG8:
+ *value=(int64*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8((uint64*)(*value),count);
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(int64*)_TIFFmalloc(count*8);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ int64* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int64)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ int64* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(int64)(*ma++);
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ int64* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(int64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ int64* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ *mb++=(int64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ int64* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ *mb++=(int64)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ int64* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ *mb++=(int64)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ float* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ case TIFF_DOUBLE:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_FLOAT:
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong((uint32*)origdata,count);
+ TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
+ *value=(float*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(float*)_TIFFmalloc(count*sizeof(float));
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ float* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(float)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ float* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(float)(*ma++);
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ float* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(float)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ float* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ *mb++=(float)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ float* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ *mb++=(float)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ float* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ *mb++=(float)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ float* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+#if defined(__WIN32__) && (_MSC_VER < 1500)
+ /*
+ * XXX: MSVC 6.0 does not support
+ * conversion of 64-bit integers into
+ * floating point values.
+ */
+ *mb++ = _TIFFUInt64ToFloat(*ma++);
+#else
+ *mb++ = (float)(*ma++);
+#endif
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ float* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ *mb++=(float)(*ma++);
+ }
+ }
+ break;
+ case TIFF_RATIONAL:
+ {
+ uint32* ma;
+ uint32 maa;
+ uint32 mab;
+ float* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ maa=*ma++;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ mab=*ma++;
+ if (mab==0)
+ *mb++=0.0;
+ else
+ *mb++=(float)maa/(float)mab;
+ }
+ }
+ break;
+ case TIFF_SRATIONAL:
+ {
+ uint32* ma;
+ int32 maa;
+ uint32 mab;
+ float* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ maa=*(int32*)ma;
+ ma++;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ mab=*ma++;
+ if (mab==0)
+ *mb++=0.0;
+ else
+ *mb++=(float)maa/(float)mab;
+ }
+ }
+ break;
+ case TIFF_DOUBLE:
+ {
+ double* ma;
+ float* mb;
+ uint32 n;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8((uint64*)origdata,count);
+ TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
+ ma=(double*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ double val = *ma++;
+ if( val > FLT_MAX )
+ val = FLT_MAX;
+ else if( val < -FLT_MAX )
+ val = -FLT_MAX;
+ *mb++=(float)val;
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ double* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ case TIFF_SBYTE:
+ case TIFF_SHORT:
+ case TIFF_SSHORT:
+ case TIFF_LONG:
+ case TIFF_SLONG:
+ case TIFF_LONG8:
+ case TIFF_SLONG8:
+ case TIFF_RATIONAL:
+ case TIFF_SRATIONAL:
+ case TIFF_FLOAT:
+ case TIFF_DOUBLE:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_DOUBLE:
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8((uint64*)origdata,count);
+ TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
+ *value=(double*)origdata;
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(double*)_TIFFmalloc(count*sizeof(double));
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_BYTE:
+ {
+ uint8* ma;
+ double* mb;
+ uint32 n;
+ ma=(uint8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(double)(*ma++);
+ }
+ break;
+ case TIFF_SBYTE:
+ {
+ int8* ma;
+ double* mb;
+ uint32 n;
+ ma=(int8*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(double)(*ma++);
+ }
+ break;
+ case TIFF_SHORT:
+ {
+ uint16* ma;
+ double* mb;
+ uint32 n;
+ ma=(uint16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(ma);
+ *mb++=(double)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SSHORT:
+ {
+ int16* ma;
+ double* mb;
+ uint32 n;
+ ma=(int16*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ *mb++=(double)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG:
+ {
+ uint32* ma;
+ double* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ *mb++=(double)(*ma++);
+ }
+ }
+ break;
+ case TIFF_SLONG:
+ {
+ int32* ma;
+ double* mb;
+ uint32 n;
+ ma=(int32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ *mb++=(double)(*ma++);
+ }
+ }
+ break;
+ case TIFF_LONG8:
+ {
+ uint64* ma;
+ double* mb;
+ uint32 n;
+ ma=(uint64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(ma);
+#if defined(__WIN32__) && (_MSC_VER < 1500)
+ /*
+ * XXX: MSVC 6.0 does not support
+ * conversion of 64-bit integers into
+ * floating point values.
+ */
+ *mb++ = _TIFFUInt64ToDouble(*ma++);
+#else
+ *mb++ = (double)(*ma++);
+#endif
+ }
+ }
+ break;
+ case TIFF_SLONG8:
+ {
+ int64* ma;
+ double* mb;
+ uint32 n;
+ ma=(int64*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ *mb++=(double)(*ma++);
+ }
+ }
+ break;
+ case TIFF_RATIONAL:
+ {
+ uint32* ma;
+ uint32 maa;
+ uint32 mab;
+ double* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ maa=*ma++;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ mab=*ma++;
+ if (mab==0)
+ *mb++=0.0;
+ else
+ *mb++=(double)maa/(double)mab;
+ }
+ }
+ break;
+ case TIFF_SRATIONAL:
+ {
+ uint32* ma;
+ int32 maa;
+ uint32 mab;
+ double* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ maa=*(int32*)ma;
+ ma++;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ mab=*ma++;
+ if (mab==0)
+ *mb++=0.0;
+ else
+ *mb++=(double)maa/(double)mab;
+ }
+ }
+ break;
+ case TIFF_FLOAT:
+ {
+ float* ma;
+ double* mb;
+ uint32 n;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong((uint32*)origdata,count);
+ TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
+ ma=(float*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ *mb++=(double)(*ma++);
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint32 count;
+ void* origdata;
+ uint64* data;
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG:
+ case TIFF_LONG8:
+ case TIFF_IFD:
+ case TIFF_IFD8:
+ break;
+ default:
+ return(TIFFReadDirEntryErrType);
+ }
+ err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
+ if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
+ {
+ *value=0;
+ return(err);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG8:
+ case TIFF_IFD8:
+ *value=(uint64*)origdata;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8(*value,count);
+ return(TIFFReadDirEntryErrOk);
+ }
+ data=(uint64*)_TIFFmalloc(count*8);
+ if (data==0)
+ {
+ _TIFFfree(origdata);
+ return(TIFFReadDirEntryErrAlloc);
+ }
+ switch (direntry->tdir_type)
+ {
+ case TIFF_LONG:
+ case TIFF_IFD:
+ {
+ uint32* ma;
+ uint64* mb;
+ uint32 n;
+ ma=(uint32*)origdata;
+ mb=data;
+ for (n=0; n<count; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(ma);
+ *mb++=(uint64)(*ma++);
+ }
+ }
+ break;
+ }
+ _TIFFfree(origdata);
+ *value=data;
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
+{
+ enum TIFFReadDirEntryErr err;
+ uint16* m;
+ uint16* na;
+ uint16 nb;
+ if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
+ return(TIFFReadDirEntryErrCount);
+ err=TIFFReadDirEntryShortArray(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk || m == NULL)
+ return(err);
+ na=m;
+ nb=tif->tif_dir.td_samplesperpixel;
+ *value=*na++;
+ nb--;
+ while (nb>0)
+ {
+ if (*na++!=*value)
+ {
+ err=TIFFReadDirEntryErrPsdif;
+ break;
+ }
+ nb--;
+ }
+ _TIFFfree(m);
+ return(err);
+}
+
+#if 0
+static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
+{
+ enum TIFFReadDirEntryErr err;
+ double* m;
+ double* na;
+ uint16 nb;
+ if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
+ return(TIFFReadDirEntryErrCount);
+ err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ na=m;
+ nb=tif->tif_dir.td_samplesperpixel;
+ *value=*na++;
+ nb--;
+ while (nb>0)
+ {
+ if (*na++!=*value)
+ {
+ err=TIFFReadDirEntryErrPsdif;
+ break;
+ }
+ nb--;
+ }
+ _TIFFfree(m);
+ return(err);
+}
+#endif
+
+static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
+{
+ (void) tif;
+ *value=*(uint8*)(&direntry->tdir_offset);
+}
+
+static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
+{
+ (void) tif;
+ *value=*(int8*)(&direntry->tdir_offset);
+}
+
+static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
+{
+ *value = direntry->tdir_offset.toff_short;
+ /* *value=*(uint16*)(&direntry->tdir_offset); */
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(value);
+}
+
+static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
+{
+ *value=*(int16*)(&direntry->tdir_offset);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)value);
+}
+
+static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
+{
+ *value=*(uint32*)(&direntry->tdir_offset);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(value);
+}
+
+static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
+{
+ *value=*(int32*)(&direntry->tdir_offset);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)value);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
+{
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,value);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ }
+ else
+ *value = direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(value);
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
+{
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,value);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ }
+ else
+ *value=*(int64*)(&direntry->tdir_offset);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)value);
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
+{
+ UInt64Aligned_t m;
+
+ assert(sizeof(double)==8);
+ assert(sizeof(uint64)==8);
+ assert(sizeof(uint32)==4);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,m.i);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ }
+ else
+ m.l = direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m.i,2);
+ /* Not completely sure what we should do when m.i[1]==0, but some */
+ /* sanitizers do not like division by 0.0: */
+ /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
+ if (m.i[0]==0 || m.i[1]==0)
+ *value=0.0;
+ else
+ *value=(double)m.i[0]/(double)m.i[1];
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
+{
+ UInt64Aligned_t m;
+ assert(sizeof(double)==8);
+ assert(sizeof(uint64)==8);
+ assert(sizeof(int32)==4);
+ assert(sizeof(uint32)==4);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,m.i);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ }
+ else
+ m.l=direntry->tdir_offset.toff_long8;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m.i,2);
+ /* Not completely sure what we should do when m.i[1]==0, but some */
+ /* sanitizers do not like division by 0.0: */
+ /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
+ if ((int32)m.i[0]==0 || m.i[1]==0)
+ *value=0.0;
+ else
+ *value=(double)((int32)m.i[0])/(double)m.i[1];
+ return(TIFFReadDirEntryErrOk);
+}
+
+static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
+{
+ union
+ {
+ float f;
+ uint32 i;
+ } float_union;
+ assert(sizeof(float)==4);
+ assert(sizeof(uint32)==4);
+ assert(sizeof(float_union)==4);
+ float_union.i=*(uint32*)(&direntry->tdir_offset);
+ *value=float_union.f;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)value);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
+{
+ assert(sizeof(double)==8);
+ assert(sizeof(uint64)==8);
+ assert(sizeof(UInt64Aligned_t)==8);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 offset = direntry->tdir_offset.toff_long;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,value);
+ if (err!=TIFFReadDirEntryErrOk)
+ return(err);
+ }
+ else
+ {
+ UInt64Aligned_t uint64_union;
+ uint64_union.l=direntry->tdir_offset.toff_long8;
+ *value=uint64_union.d;
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)value);
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
+{
+ if (value>0xFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
+{
+ if ((value<0)||(value>0xFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
+{
+ if (value>0xFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
+{
+ if ((value<0)||(value>0xFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
+{
+ if (value>0xFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
+{
+ if ((value<0)||(value>0xFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
+{
+ if (value>0x7F)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
+{
+ if (value>0x7F)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
+{
+ if ((value<-0x80)||(value>0x7F))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
+{
+ if (value>0x7F)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
+{
+ if ((value<-0x80)||(value>0x7F))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
+{
+ if (value>0x7F)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
+{
+ if ((value<-0x80)||(value>0x7F))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
+{
+ if (value>0xFFFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
+{
+ if ((value<0)||(value>0xFFFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
+{
+ if (value>0xFFFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
+{
+ if ((value<0)||(value>0xFFFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
+{
+ if (value>0x7FFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
+{
+ if (value>0x7FFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
+{
+ if ((value<-0x8000)||(value>0x7FFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
+{
+ if (value>0x7FFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
+{
+ if ((value<-0x8000)||(value>0x7FFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
+{
+ if (value<0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+/*
+ * Largest 32-bit unsigned integer value.
+ */
+#define TIFF_UINT32_MAX 0xFFFFFFFFU
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
+{
+ if (value > TIFF_UINT32_MAX)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
+{
+ if ((value < 0) || (value > (int64) TIFF_UINT32_MAX))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+#undef TIFF_UINT32_MAX
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
+{
+ if (value > 0x7FFFFFFFUL)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+/* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
+{
+ if (value > 0x7FFFFFFF)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+/* Check that the 8-byte signed value can fit in a 4-byte signed range */
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
+{
+ if ((value < 0-((int64) 0x7FFFFFFF+1)) || (value > 0x7FFFFFFF))
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
+{
+ if (value < 0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
+{
+ if (value < 0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
+{
+ if (value < 0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
+{
+ if (value < 0)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+/*
+ * Largest 64-bit signed integer value.
+ */
+#define TIFF_INT64_MAX ((int64)(((uint64) ~0) >> 1))
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
+{
+ if (value > TIFF_INT64_MAX)
+ return(TIFFReadDirEntryErrRange);
+ else
+ return(TIFFReadDirEntryErrOk);
+}
+
+#undef TIFF_INT64_MAX
+
+static enum TIFFReadDirEntryErr
+TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
+{
+ assert(size>0);
+ if (!isMapped(tif)) {
+ if (!SeekOK(tif,offset))
+ return(TIFFReadDirEntryErrIo);
+ if (!ReadOK(tif,dest,size))
+ return(TIFFReadDirEntryErrIo);
+ } else {
+ size_t ma,mb;
+ ma=(size_t)offset;
+ mb=ma+size;
+ if (((uint64)ma!=offset)
+ || (mb < ma)
+ || (mb - ma != (size_t) size)
+ || (mb < (size_t)size)
+ || (mb > (size_t)tif->tif_size)
+ )
+ return(TIFFReadDirEntryErrIo);
+ _TIFFmemcpy(dest,tif->tif_base+ma,size);
+ }
+ return(TIFFReadDirEntryErrOk);
+}
+
+static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
+{
+ if (!recover) {
+ switch (err) {
+ case TIFFReadDirEntryErrCount:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Incorrect count for \"%s\"",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrType:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Incompatible type for \"%s\"",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrIo:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "IO error during reading of \"%s\"",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrRange:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Incorrect value for \"%s\"",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrPsdif:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot handle different values per sample for \"%s\"",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrSizesan:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on size of \"%s\" value failed",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrAlloc:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Out of memory reading of \"%s\"",
+ tagname);
+ break;
+ default:
+ assert(0); /* we should never get here */
+ break;
+ }
+ } else {
+ switch (err) {
+ case TIFFReadDirEntryErrCount:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Incorrect count for \"%s\"; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrType:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Incompatible type for \"%s\"; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrIo:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "IO error during reading of \"%s\"; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrRange:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Incorrect value for \"%s\"; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrPsdif:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Cannot handle different values per sample for \"%s\"; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrSizesan:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Sanity check on size of \"%s\" value failed; tag ignored",
+ tagname);
+ break;
+ case TIFFReadDirEntryErrAlloc:
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Out of memory reading of \"%s\"; tag ignored",
+ tagname);
+ break;
+ default:
+ assert(0); /* we should never get here */
+ break;
+ }
+ }
+}
+
+/*
+ * Return the maximum number of color channels specified for a given photometric
+ * type. 0 is returned if photometric type isn't supported or no default value
+ * is defined by the specification.
+ */
+static int _TIFFGetMaxColorChannels( uint16 photometric )
+{
+ switch (photometric) {
+ case PHOTOMETRIC_PALETTE:
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ return 1;
+ case PHOTOMETRIC_YCBCR:
+ case PHOTOMETRIC_RGB:
+ case PHOTOMETRIC_CIELAB:
+ case PHOTOMETRIC_LOGLUV:
+ case PHOTOMETRIC_ITULAB:
+ case PHOTOMETRIC_ICCLAB:
+ return 3;
+ case PHOTOMETRIC_SEPARATED:
+ case PHOTOMETRIC_MASK:
+ return 4;
+ case PHOTOMETRIC_LOGL:
+ case PHOTOMETRIC_CFA:
+ default:
+ return 0;
+ }
+}
+
+/*
+ * Read the next TIFF directory from a file and convert it to the internal
+ * format. We read directories sequentially.
+ */
+int
+TIFFReadDirectory(TIFF* tif)
+{
+ static const char module[] = "TIFFReadDirectory";
+ TIFFDirEntry* dir;
+ uint16 dircount;
+ TIFFDirEntry* dp;
+ uint16 di;
+ const TIFFField* fip;
+ uint32 fii=FAILED_FII;
+ toff_t nextdiroff;
+ int bitspersample_read = FALSE;
+ int color_channels;
+
+ tif->tif_diroff=tif->tif_nextdiroff;
+ if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
+ return 0; /* last offset or bad offset (IFD looping) */
+ (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
+ tif->tif_curdir++;
+ nextdiroff = tif->tif_nextdiroff;
+ dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
+ if (!dircount)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
+ return 0;
+ }
+ TIFFReadDirectoryCheckOrder(tif,dir,dircount);
+
+ /*
+ * Mark duplicates of any tag to be ignored (bugzilla 1994)
+ * to avoid certain pathological problems.
+ */
+ {
+ TIFFDirEntry* ma;
+ uint16 mb;
+ for (ma=dir, mb=0; mb<dircount; ma++, mb++)
+ {
+ TIFFDirEntry* na;
+ uint16 nb;
+ for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
+ {
+ if (ma->tdir_tag==na->tdir_tag)
+ na->tdir_tag=IGNORE;
+ }
+ }
+ }
+
+ tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
+ tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
+ /* free any old stuff and reinit */
+ TIFFFreeDirectory(tif);
+ TIFFDefaultDirectory(tif);
+ /*
+ * Electronic Arts writes gray-scale TIFF files
+ * without a PlanarConfiguration directory entry.
+ * Thus we setup a default value here, even though
+ * the TIFF spec says there is no default value.
+ */
+ TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
+ /*
+ * Setup default value and then make a pass over
+ * the fields to check type and tag information,
+ * and to extract info required to size data
+ * structures. A second pass is made afterwards
+ * to read in everything not taken in the first pass.
+ * But we must process the Compression tag first
+ * in order to merge in codec-private tag definitions (otherwise
+ * we may get complaints about unknown tags). However, the
+ * Compression tag may be dependent on the SamplesPerPixel
+ * tag value because older TIFF specs permitted Compression
+ * to be written as a SamplesPerPixel-count tag entry.
+ * Thus if we don't first figure out the correct SamplesPerPixel
+ * tag value then we may end up ignoring the Compression tag
+ * value because it has an incorrect count value (if the
+ * true value of SamplesPerPixel is not 1).
+ */
+ dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
+ if (dp)
+ {
+ if (!TIFFFetchNormalTag(tif,dp,0))
+ goto bad;
+ dp->tdir_tag=IGNORE;
+ }
+ dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
+ if (dp)
+ {
+ /*
+ * The 5.0 spec says the Compression tag has one value, while
+ * earlier specs say it has one value per sample. Because of
+ * this, we accept the tag if one value is supplied with either
+ * count.
+ */
+ uint16 value;
+ enum TIFFReadDirEntryErr err;
+ err=TIFFReadDirEntryShort(tif,dp,&value);
+ if (err==TIFFReadDirEntryErrCount)
+ err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
+ goto bad;
+ }
+ if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
+ goto bad;
+ dp->tdir_tag=IGNORE;
+ }
+ else
+ {
+ if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
+ goto bad;
+ }
+ /*
+ * First real pass over the directory.
+ */
+ for (di=0, dp=dir; di<dircount; di++, dp++)
+ {
+ if (dp->tdir_tag!=IGNORE)
+ {
+ TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
+ if (fii == FAILED_FII)
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Unknown field with tag %d (0x%x) encountered",
+ dp->tdir_tag,dp->tdir_tag);
+ /* the following knowingly leaks the
+ anonymous field structure */
+ if (!_TIFFMergeFields(tif,
+ _TIFFCreateAnonField(tif,
+ dp->tdir_tag,
+ (TIFFDataType) dp->tdir_type),
+ 1)) {
+ TIFFWarningExt(tif->tif_clientdata,
+ module,
+ "Registering anonymous field with tag %d (0x%x) failed",
+ dp->tdir_tag,
+ dp->tdir_tag);
+ dp->tdir_tag=IGNORE;
+ } else {
+ TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
+ assert(fii != FAILED_FII);
+ }
+ }
+ }
+ if (dp->tdir_tag!=IGNORE)
+ {
+ fip=tif->tif_fields[fii];
+ if (fip->field_bit==FIELD_IGNORE)
+ dp->tdir_tag=IGNORE;
+ else
+ {
+ switch (dp->tdir_tag)
+ {
+ case TIFFTAG_STRIPOFFSETS:
+ case TIFFTAG_STRIPBYTECOUNTS:
+ case TIFFTAG_TILEOFFSETS:
+ case TIFFTAG_TILEBYTECOUNTS:
+ TIFFSetFieldBit(tif,fip->field_bit);
+ break;
+ case TIFFTAG_IMAGEWIDTH:
+ case TIFFTAG_IMAGELENGTH:
+ case TIFFTAG_IMAGEDEPTH:
+ case TIFFTAG_TILELENGTH:
+ case TIFFTAG_TILEWIDTH:
+ case TIFFTAG_TILEDEPTH:
+ case TIFFTAG_PLANARCONFIG:
+ case TIFFTAG_ROWSPERSTRIP:
+ case TIFFTAG_EXTRASAMPLES:
+ if (!TIFFFetchNormalTag(tif,dp,0))
+ goto bad;
+ dp->tdir_tag=IGNORE;
+ break;
+ default:
+ if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )
+ dp->tdir_tag=IGNORE;
+ break;
+ }
+ }
+ }
+ }
+ /*
+ * XXX: OJPEG hack.
+ * If a) compression is OJPEG, b) planarconfig tag says it's separate,
+ * c) strip offsets/bytecounts tag are both present and
+ * d) both contain exactly one value, then we consistently find
+ * that the buggy implementation of the buggy compression scheme
+ * matches contig planarconfig best. So we 'fix-up' the tag here
+ */
+ if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
+ (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
+ {
+ if (!_TIFFFillStriles(tif))
+ goto bad;
+ dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
+ if ((dp!=0)&&(dp->tdir_count==1))
+ {
+ dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
+ TIFFTAG_STRIPBYTECOUNTS);
+ if ((dp!=0)&&(dp->tdir_count==1))
+ {
+ tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Planarconfig tag value assumed incorrect, "
+ "assuming data is contig instead of chunky");
+ }
+ }
+ }
+ /*
+ * Allocate directory structure and setup defaults.
+ */
+ if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
+ {
+ MissingRequired(tif,"ImageLength");
+ goto bad;
+ }
+ /*
+ * Setup appropriate structures (by strip or by tile)
+ */
+ if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
+ tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
+ tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
+ tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
+ tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
+ tif->tif_flags &= ~TIFF_ISTILED;
+ } else {
+ tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
+ tif->tif_flags |= TIFF_ISTILED;
+ }
+ if (!tif->tif_dir.td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot handle zero number of %s",
+ isTiled(tif) ? "tiles" : "strips");
+ goto bad;
+ }
+ tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
+ if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
+ tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
+ if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
+#ifdef OJPEG_SUPPORT
+ if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
+ (isTiled(tif)==0) &&
+ (tif->tif_dir.td_nstrips==1)) {
+ /*
+ * XXX: OJPEG hack.
+ * If a) compression is OJPEG, b) it's not a tiled TIFF,
+ * and c) the number of strips is 1,
+ * then we tolerate the absence of stripoffsets tag,
+ * because, presumably, all required data is in the
+ * JpegInterchangeFormat stream.
+ */
+ TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
+ } else
+#endif
+ {
+ MissingRequired(tif,
+ isTiled(tif) ? "TileOffsets" : "StripOffsets");
+ goto bad;
+ }
+ }
+ /*
+ * Second pass: extract other information.
+ */
+ for (di=0, dp=dir; di<dircount; di++, dp++)
+ {
+ switch (dp->tdir_tag)
+ {
+ case IGNORE:
+ break;
+ case TIFFTAG_MINSAMPLEVALUE:
+ case TIFFTAG_MAXSAMPLEVALUE:
+ case TIFFTAG_BITSPERSAMPLE:
+ case TIFFTAG_DATATYPE:
+ case TIFFTAG_SAMPLEFORMAT:
+ /*
+ * The MinSampleValue, MaxSampleValue, BitsPerSample
+ * DataType and SampleFormat tags are supposed to be
+ * written as one value/sample, but some vendors
+ * incorrectly write one value only -- so we accept
+ * that as well (yuck). Other vendors write correct
+ * value for NumberOfSamples, but incorrect one for
+ * BitsPerSample and friends, and we will read this
+ * too.
+ */
+ {
+ uint16 value;
+ enum TIFFReadDirEntryErr err;
+ err=TIFFReadDirEntryShort(tif,dp,&value);
+ if (err==TIFFReadDirEntryErrCount)
+ err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ fip = TIFFFieldWithTag(tif,dp->tdir_tag);
+ TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
+ goto bad;
+ }
+ if (!TIFFSetField(tif,dp->tdir_tag,value))
+ goto bad;
+ if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )
+ bitspersample_read = TRUE;
+ }
+ break;
+ case TIFFTAG_SMINSAMPLEVALUE:
+ case TIFFTAG_SMAXSAMPLEVALUE:
+ {
+
+ double *data = NULL;
+ enum TIFFReadDirEntryErr err;
+ uint32 saved_flags;
+ int m;
+ if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
+ err = TIFFReadDirEntryErrCount;
+ else
+ err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ fip = TIFFFieldWithTag(tif,dp->tdir_tag);
+ TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
+ goto bad;
+ }
+ saved_flags = tif->tif_flags;
+ tif->tif_flags |= TIFF_PERSAMPLE;
+ m = TIFFSetField(tif,dp->tdir_tag,data);
+ tif->tif_flags = saved_flags;
+ _TIFFfree(data);
+ if (!m)
+ goto bad;
+ }
+ break;
+ case TIFFTAG_STRIPOFFSETS:
+ case TIFFTAG_TILEOFFSETS:
+#if defined(DEFER_STRILE_LOAD)
+ _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
+ dp, sizeof(TIFFDirEntry) );
+#else
+ if( tif->tif_dir.td_stripoffset != NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "tif->tif_dir.td_stripoffset is "
+ "already allocated. Likely duplicated "
+ "StripOffsets/TileOffsets tag");
+ goto bad;
+ }
+ if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
+ goto bad;
+#endif
+ break;
+ case TIFFTAG_STRIPBYTECOUNTS:
+ case TIFFTAG_TILEBYTECOUNTS:
+#if defined(DEFER_STRILE_LOAD)
+ _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
+ dp, sizeof(TIFFDirEntry) );
+#else
+ if( tif->tif_dir.td_stripbytecount != NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "tif->tif_dir.td_stripbytecount is "
+ "already allocated. Likely duplicated "
+ "StripByteCounts/TileByteCounts tag");
+ goto bad;
+ }
+ if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
+ goto bad;
+#endif
+ break;
+ case TIFFTAG_COLORMAP:
+ case TIFFTAG_TRANSFERFUNCTION:
+ {
+ enum TIFFReadDirEntryErr err;
+ uint32 countpersample;
+ uint32 countrequired;
+ uint32 incrementpersample;
+ uint16* value=NULL;
+ /* It would be dangerous to instantiate those tag values */
+ /* since if td_bitspersample has not yet been read (due to */
+ /* unordered tags), it could be read afterwards with a */
+ /* values greater than the default one (1), which may cause */
+ /* crashes in user code */
+ if( !bitspersample_read )
+ {
+ fip = TIFFFieldWithTag(tif,dp->tdir_tag);
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Ignoring %s since BitsPerSample tag not found",
+ fip ? fip->field_name : "unknown tagname");
+ continue;
+ }
+ /* ColorMap or TransferFunction for high bit */
+ /* depths do not make much sense and could be */
+ /* used as a denial of service vector */
+ if (tif->tif_dir.td_bitspersample > 24)
+ {
+ fip = TIFFFieldWithTag(tif,dp->tdir_tag);
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Ignoring %s because BitsPerSample=%d>24",
+ fip ? fip->field_name : "unknown tagname",
+ tif->tif_dir.td_bitspersample);
+ continue;
+ }
+ countpersample=(1U<<tif->tif_dir.td_bitspersample);
+ if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
+ {
+ countrequired=countpersample;
+ incrementpersample=0;
+ }
+ else
+ {
+ countrequired=3*countpersample;
+ incrementpersample=countpersample;
+ }
+ if (dp->tdir_count!=(uint64)countrequired)
+ err=TIFFReadDirEntryErrCount;
+ else
+ err=TIFFReadDirEntryShortArray(tif,dp,&value);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ fip = TIFFFieldWithTag(tif,dp->tdir_tag);
+ TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
+ }
+ else
+ {
+ TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
+ _TIFFfree(value);
+ }
+ }
+ break;
+/* BEGIN REV 4.0 COMPATIBILITY */
+ case TIFFTAG_OSUBFILETYPE:
+ {
+ uint16 valueo;
+ uint32 value;
+ if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
+ {
+ switch (valueo)
+ {
+ case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
+ case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
+ default: value=0; break;
+ }
+ if (value!=0)
+ TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
+ }
+ }
+ break;
+/* END REV 4.0 COMPATIBILITY */
+ default:
+ (void) TIFFFetchNormalTag(tif, dp, TRUE);
+ break;
+ }
+ }
+ /*
+ * OJPEG hack:
+ * - If a) compression is OJPEG, and b) photometric tag is missing,
+ * then we consistently find that photometric should be YCbCr
+ * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
+ * then we consistently find that the buggy implementation of the
+ * buggy compression scheme matches photometric YCbCr instead.
+ * - If a) compression is OJPEG, and b) bitspersample tag is missing,
+ * then we consistently find bitspersample should be 8.
+ * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+ * and c) photometric is RGB or YCbCr, then we consistently find
+ * samplesperpixel should be 3
+ * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+ * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
+ * find samplesperpixel should be 3
+ */
+ if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
+ {
+ if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Photometric tag is missing, assuming data is YCbCr");
+ if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
+ goto bad;
+ }
+ else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
+ {
+ tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Photometric tag value assumed incorrect, "
+ "assuming data is YCbCr instead of RGB");
+ }
+ if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "BitsPerSample tag is missing, assuming 8 bits per sample");
+ if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
+ goto bad;
+ }
+ if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
+ {
+ if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "SamplesPerPixel tag is missing, "
+ "assuming correct SamplesPerPixel value is 3");
+ if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
+ goto bad;
+ }
+ if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "SamplesPerPixel tag is missing, "
+ "applying correct SamplesPerPixel value of 3");
+ if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
+ goto bad;
+ }
+ else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
+ || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
+ {
+ /*
+ * SamplesPerPixel tag is missing, but is not required
+ * by spec. Assume correct SamplesPerPixel value of 1.
+ */
+ if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
+ goto bad;
+ }
+ }
+ }
+
+ /*
+ * Make sure all non-color channels are extrasamples.
+ * If it's not the case, define them as such.
+ */
+ color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
+ if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
+ uint16 old_extrasamples;
+ uint16 *new_sampleinfo;
+
+ TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
+ "color channels and ExtraSamples doesn't match SamplesPerPixel. "
+ "Defining non-color channels as ExtraSamples.");
+
+ old_extrasamples = tif->tif_dir.td_extrasamples;
+ tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);
+
+ // sampleinfo should contain information relative to these new extra samples
+ new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
+ if (!new_sampleinfo) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
+ "temporary new sampleinfo array (%d 16 bit elements)",
+ tif->tif_dir.td_extrasamples);
+ goto bad;
+ }
+
+ memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));
+ _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
+ _TIFFfree(new_sampleinfo);
+ }
+
+ /*
+ * Verify Palette image has a Colormap.
+ */
+ if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
+ !TIFFFieldSet(tif, FIELD_COLORMAP)) {
+ if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
+ tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
+ else if (tif->tif_dir.td_bitspersample>=8)
+ tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
+ else {
+ MissingRequired(tif, "Colormap");
+ goto bad;
+ }
+ }
+ /*
+ * OJPEG hack:
+ * We do no further messing with strip/tile offsets/bytecounts in OJPEG
+ * TIFFs
+ */
+ if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
+ {
+ /*
+ * Attempt to deal with a missing StripByteCounts tag.
+ */
+ if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
+ /*
+ * Some manufacturers violate the spec by not giving
+ * the size of the strips. In this case, assume there
+ * is one uncompressed strip of data.
+ */
+ if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
+ tif->tif_dir.td_nstrips > 1) ||
+ (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
+ tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
+ MissingRequired(tif, "StripByteCounts");
+ goto bad;
+ }
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "TIFF directory is missing required "
+ "\"StripByteCounts\" field, calculating from imagelength");
+ if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
+ /*
+ * Assume we have wrong StripByteCount value (in case
+ * of single strip) in following cases:
+ * - it is equal to zero along with StripOffset;
+ * - it is larger than file itself (in case of uncompressed
+ * image);
+ * - it is smaller than the size of the bytes per row
+ * multiplied on the number of rows. The last case should
+ * not be checked in the case of writing new image,
+ * because we may do not know the exact strip size
+ * until the whole image will be written and directory
+ * dumped out.
+ */
+ #define BYTECOUNTLOOKSBAD \
+ ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
+ (tif->tif_dir.td_compression == COMPRESSION_NONE && \
+ (tif->tif_dir.td_stripoffset[0] <= TIFFGetFileSize(tif) && \
+ tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0])) || \
+ (tif->tif_mode == O_RDONLY && \
+ tif->tif_dir.td_compression == COMPRESSION_NONE && \
+ tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
+
+ } else if (tif->tif_dir.td_nstrips == 1
+ && !(tif->tif_flags&TIFF_ISTILED)
+ && _TIFFFillStriles(tif)
+ && tif->tif_dir.td_stripoffset[0] != 0
+ && BYTECOUNTLOOKSBAD) {
+ /*
+ * XXX: Plexus (and others) sometimes give a value of
+ * zero for a tag when they don't know what the
+ * correct value is! Try and handle the simple case
+ * of estimating the size of a one strip image.
+ */
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
+ if(EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
+
+#if !defined(DEFER_STRILE_LOAD)
+ } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
+ && tif->tif_dir.td_nstrips > 2
+ && tif->tif_dir.td_compression == COMPRESSION_NONE
+ && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
+ && tif->tif_dir.td_stripbytecount[0] != 0
+ && tif->tif_dir.td_stripbytecount[1] != 0 ) {
+ /*
+ * XXX: Some vendors fill StripByteCount array with
+ * absolutely wrong values (it can be equal to
+ * StripOffset array, for example). Catch this case
+ * here.
+ *
+ * We avoid this check if deferring strile loading
+ * as it would always force us to load the strip/tile
+ * information.
+ */
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
+ if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
+#endif /* !defined(DEFER_STRILE_LOAD) */
+ }
+ }
+ if (dir)
+ {
+ _TIFFfree(dir);
+ dir=NULL;
+ }
+ if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
+ {
+ if (tif->tif_dir.td_bitspersample>=16)
+ tif->tif_dir.td_maxsamplevalue=0xFFFF;
+ else
+ tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
+ }
+ /*
+ * XXX: We can optimize checking for the strip bounds using the sorted
+ * bytecounts array. See also comments for TIFFAppendToStrip()
+ * function in tif_write.c.
+ */
+#if !defined(DEFER_STRILE_LOAD)
+ if (tif->tif_dir.td_nstrips > 1) {
+ uint32 strip;
+
+ tif->tif_dir.td_stripbytecountsorted = 1;
+ for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
+ if (tif->tif_dir.td_stripoffset[strip - 1] >
+ tif->tif_dir.td_stripoffset[strip]) {
+ tif->tif_dir.td_stripbytecountsorted = 0;
+ break;
+ }
+ }
+ }
+#endif /* !defined(DEFER_STRILE_LOAD) */
+
+ /*
+ * An opportunity for compression mode dependent tag fixup
+ */
+ (*tif->tif_fixuptags)(tif);
+
+ /*
+ * Some manufacturers make life difficult by writing
+ * large amounts of uncompressed data as a single strip.
+ * This is contrary to the recommendations of the spec.
+ * The following makes an attempt at breaking such images
+ * into strips closer to the recommended 8k bytes. A
+ * side effect, however, is that the RowsPerStrip tag
+ * value may be changed.
+ */
+ if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
+ (tif->tif_dir.td_nstrips==1)&&
+ (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
+ ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
+ {
+ if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
+ return 0;
+ ChopUpSingleUncompressedStrip(tif);
+ }
+
+ /*
+ * Clear the dirty directory flag.
+ */
+ tif->tif_flags &= ~TIFF_DIRTYDIRECT;
+ tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+
+ /*
+ * Reinitialize i/o since we are starting on a new directory.
+ */
+ tif->tif_row = (uint32) -1;
+ tif->tif_curstrip = (uint32) -1;
+ tif->tif_col = (uint32) -1;
+ tif->tif_curtile = (uint32) -1;
+ tif->tif_tilesize = (tmsize_t) -1;
+
+ tif->tif_scanlinesize = TIFFScanlineSize(tif);
+ if (!tif->tif_scanlinesize) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot handle zero scanline size");
+ return (0);
+ }
+
+ if (isTiled(tif)) {
+ tif->tif_tilesize = TIFFTileSize(tif);
+ if (!tif->tif_tilesize) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot handle zero tile size");
+ return (0);
+ }
+ } else {
+ if (!TIFFStripSize(tif)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot handle zero strip size");
+ return (0);
+ }
+ }
+ return (1);
+bad:
+ if (dir)
+ _TIFFfree(dir);
+ return (0);
+}
+
+static void
+TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
+{
+ static const char module[] = "TIFFReadDirectoryCheckOrder";
+ uint16 m;
+ uint16 n;
+ TIFFDirEntry* o;
+ m=0;
+ for (n=0, o=dir; n<dircount; n++, o++)
+ {
+ if (o->tdir_tag<m)
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Invalid TIFF directory; tags are not sorted in ascending order");
+ break;
+ }
+ m=o->tdir_tag+1;
+ }
+}
+
+static TIFFDirEntry*
+TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
+{
+ TIFFDirEntry* m;
+ uint16 n;
+ (void) tif;
+ for (m=dir, n=0; n<dircount; m++, n++)
+ {
+ if (m->tdir_tag==tagid)
+ return(m);
+ }
+ return(0);
+}
+
+static void
+TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
+{
+ int32 ma,mb,mc;
+ ma=-1;
+ mc=(int32)tif->tif_nfields;
+ while (1)
+ {
+ if (ma+1==mc)
+ {
+ *fii = FAILED_FII;
+ return;
+ }
+ mb=(ma+mc)/2;
+ if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
+ break;
+ if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
+ ma=mb;
+ else
+ mc=mb;
+ }
+ while (1)
+ {
+ if (mb==0)
+ break;
+ if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
+ break;
+ mb--;
+ }
+ *fii=mb;
+}
+
+/*
+ * Read custom directory from the arbitrary offset.
+ * The code is very similar to TIFFReadDirectory().
+ */
+int
+TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
+ const TIFFFieldArray* infoarray)
+{
+ static const char module[] = "TIFFReadCustomDirectory";
+ TIFFDirEntry* dir;
+ uint16 dircount;
+ TIFFDirEntry* dp;
+ uint16 di;
+ const TIFFField* fip;
+ uint32 fii;
+ _TIFFSetupFields(tif, infoarray);
+ dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
+ if (!dircount)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
+ return 0;
+ }
+ TIFFFreeDirectory(tif);
+ _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
+ TIFFReadDirectoryCheckOrder(tif,dir,dircount);
+ for (di=0, dp=dir; di<dircount; di++, dp++)
+ {
+ TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
+ if (fii == FAILED_FII)
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Unknown field with tag %d (0x%x) encountered",
+ dp->tdir_tag, dp->tdir_tag);
+ if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
+ dp->tdir_tag,
+ (TIFFDataType) dp->tdir_type),
+ 1)) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Registering anonymous field with tag %d (0x%x) failed",
+ dp->tdir_tag, dp->tdir_tag);
+ dp->tdir_tag=IGNORE;
+ } else {
+ TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
+ assert( fii != FAILED_FII );
+ }
+ }
+ if (dp->tdir_tag!=IGNORE)
+ {
+ fip=tif->tif_fields[fii];
+ if (fip->field_bit==FIELD_IGNORE)
+ dp->tdir_tag=IGNORE;
+ else
+ {
+ /* check data type */
+ while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
+ {
+ fii++;
+ if ((fii==tif->tif_nfields)||
+ (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
+ {
+ fii=0xFFFF;
+ break;
+ }
+ fip=tif->tif_fields[fii];
+ }
+ if (fii==0xFFFF)
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Wrong data type %d for \"%s\"; tag ignored",
+ dp->tdir_type,fip->field_name);
+ dp->tdir_tag=IGNORE;
+ }
+ else
+ {
+ /* check count if known in advance */
+ if ((fip->field_readcount!=TIFF_VARIABLE)&&
+ (fip->field_readcount!=TIFF_VARIABLE2))
+ {
+ uint32 expected;
+ if (fip->field_readcount==TIFF_SPP)
+ expected=(uint32)tif->tif_dir.td_samplesperpixel;
+ else
+ expected=(uint32)fip->field_readcount;
+ if (!CheckDirCount(tif,dp,expected))
+ dp->tdir_tag=IGNORE;
+ }
+ }
+ }
+ switch (dp->tdir_tag)
+ {
+ case IGNORE:
+ break;
+ case EXIFTAG_SUBJECTDISTANCE:
+ (void) TIFFFetchSubjectDistance(tif,dp);
+ break;
+ default:
+ (void) TIFFFetchNormalTag(tif, dp, TRUE);
+ break;
+ }
+ }
+ }
+ if (dir)
+ _TIFFfree(dir);
+ return 1;
+}
+
+/*
+ * EXIF is important special case of custom IFD, so we have a special
+ * function to read it.
+ */
+int
+TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
+{
+ const TIFFFieldArray* exifFieldArray;
+ exifFieldArray = _TIFFGetExifFields();
+ return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
+}
+
+static int
+EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
+{
+ static const char module[] = "EstimateStripByteCounts";
+
+ TIFFDirEntry *dp;
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 strip;
+
+ /* Do not try to load stripbytecount as we will compute it */
+ if( !_TIFFFillStrilesInternal( tif, 0 ) )
+ return -1;
+
+ if (td->td_stripbytecount)
+ _TIFFfree(td->td_stripbytecount);
+ td->td_stripbytecount = (uint64*)
+ _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
+ "for \"StripByteCounts\" array");
+ if( td->td_stripbytecount == NULL )
+ return -1;
+
+ if (td->td_compression != COMPRESSION_NONE) {
+ uint64 space;
+ uint64 filesize;
+ uint16 n;
+ filesize = TIFFGetFileSize(tif);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
+ else
+ space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
+ /* calculate amount of space used by indirect values */
+ for (dp = dir, n = dircount; n > 0; n--, dp++)
+ {
+ uint32 typewidth;
+ uint64 datasize;
+ typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
+ if (typewidth == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot determine size of unknown tag type %d",
+ dp->tdir_type);
+ return -1;
+ }
+ datasize=(uint64)typewidth*dp->tdir_count;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (datasize<=4)
+ datasize=0;
+ }
+ else
+ {
+ if (datasize<=8)
+ datasize=0;
+ }
+ space+=datasize;
+ }
+ if( filesize < space )
+ /* we should perhaps return in error ? */
+ space = filesize;
+ else
+ space = filesize - space;
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+ space /= td->td_samplesperpixel;
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = space;
+ /*
+ * This gross hack handles the case were the offset to
+ * the last strip is past the place where we think the strip
+ * should begin. Since a strip of data must be contiguous,
+ * it's safe to assume that we've overestimated the amount
+ * of data in the strip and trim this number back accordingly.
+ */
+ strip--;
+ if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
+ td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
+ } else if (isTiled(tif)) {
+ uint64 bytespertile = TIFFTileSize64(tif);
+
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = bytespertile;
+ } else {
+ uint64 rowbytes = TIFFScanlineSize64(tif);
+ uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
+ }
+ TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
+ if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
+ td->td_rowsperstrip = td->td_imagelength;
+ return 1;
+}
+
+static void
+MissingRequired(TIFF* tif, const char* tagname)
+{
+ static const char module[] = "MissingRequired";
+
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "TIFF directory is missing required \"%s\" field",
+ tagname);
+}
+
+/*
+ * Check the directory offset against the list of already seen directory
+ * offsets. This is a trick to prevent IFD looping. The one can create TIFF
+ * file with looped directory pointers. We will maintain a list of already
+ * seen directories and check every IFD offset against that list.
+ */
+static int
+TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
+{
+ uint16 n;
+
+ if (diroff == 0) /* no more directories */
+ return 0;
+ if (tif->tif_dirnumber == 65535) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFCheckDirOffset",
+ "Cannot handle more than 65535 TIFF directories");
+ return 0;
+ }
+
+ for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
+ if (tif->tif_dirlist[n] == diroff)
+ return 0;
+ }
+
+ tif->tif_dirnumber++;
+
+ if (tif->tif_dirlist == NULL || tif->tif_dirnumber > tif->tif_dirlistsize) {
+ uint64* new_dirlist;
+
+ /*
+ * XXX: Reduce memory allocation granularity of the dirlist
+ * array.
+ */
+ new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
+ tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
+ if (!new_dirlist)
+ return 0;
+ if( tif->tif_dirnumber >= 32768 )
+ tif->tif_dirlistsize = 65535;
+ else
+ tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
+ tif->tif_dirlist = new_dirlist;
+ }
+
+ tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
+
+ return 1;
+}
+
+/*
+ * Check the count field of a directory entry against a known value. The
+ * caller is expected to skip/ignore the tag if there is a mismatch.
+ */
+static int
+CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
+{
+ if ((uint64)count > dir->tdir_count) {
+ const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
+ fip ? fip->field_name : "unknown tagname",
+ dir->tdir_count, count);
+ return (0);
+ } else if ((uint64)count < dir->tdir_count) {
+ const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
+ fip ? fip->field_name : "unknown tagname",
+ dir->tdir_count, count);
+ dir->tdir_count = count;
+ return (1);
+ }
+ return (1);
+}
+
+/*
+ * Read IFD structure from the specified offset. If the pointer to
+ * nextdiroff variable has been specified, read it too. Function returns a
+ * number of fields in the directory or 0 if failed.
+ */
+static uint16
+TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
+ uint64 *nextdiroff)
+{
+ static const char module[] = "TIFFFetchDirectory";
+
+ void* origdir;
+ uint16 dircount16;
+ uint32 dirsize;
+ TIFFDirEntry* dir;
+ uint8* ma;
+ TIFFDirEntry* mb;
+ uint16 n;
+
+ assert(pdir);
+
+ tif->tif_diroff = diroff;
+ if (nextdiroff)
+ *nextdiroff = 0;
+ if (!isMapped(tif)) {
+ if (!SeekOK(tif, tif->tif_diroff)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Seek error accessing TIFF directory",
+ tif->tif_name);
+ return 0;
+ }
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount16);
+ if (dircount16>4096)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on directory count failed, this is probably not a valid IFD offset");
+ return 0;
+ }
+ dirsize = 12;
+ } else {
+ uint64 dircount64;
+ if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>4096)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on directory count failed, this is probably not a valid IFD offset");
+ return 0;
+ }
+ dircount16 = (uint16)dircount64;
+ dirsize = 20;
+ }
+ origdir = _TIFFCheckMalloc(tif, dircount16,
+ dirsize, "to read TIFF directory");
+ if (origdir == NULL)
+ return 0;
+ if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%.100s: Can not read TIFF directory",
+ tif->tif_name);
+ _TIFFfree(origdir);
+ return 0;
+ }
+ /*
+ * Read offset to next directory for sequential scans if
+ * needed.
+ */
+ if (nextdiroff)
+ {
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 nextdiroff32;
+ if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
+ nextdiroff32 = 0;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&nextdiroff32);
+ *nextdiroff=nextdiroff32;
+ } else {
+ if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
+ *nextdiroff = 0;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(nextdiroff);
+ }
+ }
+ } else {
+ tmsize_t m;
+ tmsize_t off = (tmsize_t) tif->tif_diroff;
+ if ((uint64)off!=tif->tif_diroff)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
+ return(0);
+ }
+
+ /*
+ * Check for integer overflow when validating the dir_off,
+ * otherwise a very high offset may cause an OOB read and
+ * crash the client. Make two comparisons instead of
+ *
+ * off + sizeof(uint16) > tif->tif_size
+ *
+ * to avoid overflow.
+ */
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ m=off+sizeof(uint16);
+ if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not read TIFF directory count");
+ return 0;
+ } else {
+ _TIFFmemcpy(&dircount16, tif->tif_base + off,
+ sizeof(uint16));
+ }
+ off += sizeof (uint16);
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount16);
+ if (dircount16>4096)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on directory count failed, this is probably not a valid IFD offset");
+ return 0;
+ }
+ dirsize = 12;
+ }
+ else
+ {
+ uint64 dircount64;
+ m=off+sizeof(uint64);
+ if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not read TIFF directory count");
+ return 0;
+ } else {
+ _TIFFmemcpy(&dircount64, tif->tif_base + off,
+ sizeof(uint64));
+ }
+ off += sizeof (uint64);
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>4096)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on directory count failed, this is probably not a valid IFD offset");
+ return 0;
+ }
+ dircount16 = (uint16)dircount64;
+ dirsize = 20;
+ }
+ if (dircount16 == 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on directory count failed, zero tag directories not supported");
+ return 0;
+ }
+ origdir = _TIFFCheckMalloc(tif, dircount16,
+ dirsize,
+ "to read TIFF directory");
+ if (origdir == NULL)
+ return 0;
+ m=off+dircount16*dirsize;
+ if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not read TIFF directory");
+ _TIFFfree(origdir);
+ return 0;
+ } else {
+ _TIFFmemcpy(origdir, tif->tif_base + off,
+ dircount16 * dirsize);
+ }
+ if (nextdiroff) {
+ off += dircount16 * dirsize;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 nextdiroff32;
+ m=off+sizeof(uint32);
+ if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
+ nextdiroff32 = 0;
+ else
+ _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
+ sizeof (uint32));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&nextdiroff32);
+ *nextdiroff = nextdiroff32;
+ }
+ else
+ {
+ m=off+sizeof(uint64);
+ if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
+ *nextdiroff = 0;
+ else
+ _TIFFmemcpy(nextdiroff, tif->tif_base + off,
+ sizeof (uint64));
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(nextdiroff);
+ }
+ }
+ }
+ dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
+ sizeof(TIFFDirEntry),
+ "to read TIFF directory");
+ if (dir==0)
+ {
+ _TIFFfree(origdir);
+ return 0;
+ }
+ ma=(uint8*)origdir;
+ mb=dir;
+ for (n=0; n<dircount16; n++)
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ mb->tdir_tag=*(uint16*)ma;
+ ma+=sizeof(uint16);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)ma);
+ mb->tdir_type=*(uint16*)ma;
+ ma+=sizeof(uint16);
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)ma);
+ mb->tdir_count=(uint64)(*(uint32*)ma);
+ ma+=sizeof(uint32);
+ *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
+ ma+=sizeof(uint32);
+ }
+ else
+ {
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)ma);
+ mb->tdir_count=TIFFReadUInt64(ma);
+ ma+=sizeof(uint64);
+ mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
+ ma+=sizeof(uint64);
+ }
+ mb++;
+ }
+ _TIFFfree(origdir);
+ *pdir = dir;
+ return dircount16;
+}
+
+/*
+ * Fetch a tag that is not handled by special case code.
+ */
+static int
+TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
+{
+ static const char module[] = "TIFFFetchNormalTag";
+ enum TIFFReadDirEntryErr err;
+ uint32 fii;
+ const TIFFField* fip = NULL;
+ TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
+ if( fii == FAILED_FII )
+ {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
+ "No definition found for tag %d",
+ dp->tdir_tag);
+ return 0;
+ }
+ fip=tif->tif_fields[fii];
+ assert(fip != NULL); /* should not happen */
+ assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
+ assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
+ err=TIFFReadDirEntryErrOk;
+ switch (fip->set_field_type)
+ {
+ case TIFF_SETGET_UNDEFINED:
+ break;
+ case TIFF_SETGET_ASCII:
+ {
+ uint8* data;
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ uint32 mb = 0;
+ int n;
+ if (data != NULL)
+ {
+ uint8* ma = data;
+ while (mb<(uint32)dp->tdir_count)
+ {
+ if (*ma==0)
+ break;
+ ma++;
+ mb++;
+ }
+ }
+ if (mb+1<(uint32)dp->tdir_count)
+ TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
+ else if (mb+1>(uint32)dp->tdir_count)
+ {
+ uint8* o;
+ TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
+ if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
+ o=NULL;
+ else
+ o=_TIFFmalloc((uint32)dp->tdir_count+1);
+ if (o==NULL)
+ {
+ if (data!=NULL)
+ _TIFFfree(data);
+ return(0);
+ }
+ _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
+ o[(uint32)dp->tdir_count]=0;
+ if (data!=0)
+ _TIFFfree(data);
+ data=o;
+ }
+ n=TIFFSetField(tif,dp->tdir_tag,data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!n)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_UINT8:
+ {
+ uint8 data=0;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryByte(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_UINT16:
+ {
+ uint16 data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryShort(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_UINT32:
+ {
+ uint32 data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryLong(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_UINT64:
+ {
+ uint64 data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryLong8(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_FLOAT:
+ {
+ float data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryFloat(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_DOUBLE:
+ {
+ double data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryDouble(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_IFD8:
+ {
+ uint64 data;
+ assert(fip->field_readcount==1);
+ assert(fip->field_passcount==0);
+ err=TIFFReadDirEntryIfd8(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ if (!TIFFSetField(tif,dp->tdir_tag,data))
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_UINT16_PAIR:
+ {
+ uint16* data;
+ assert(fip->field_readcount==2);
+ assert(fip->field_passcount==0);
+ if (dp->tdir_count!=2) {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "incorrect count for field \"%s\", expected 2, got %d",
+ fip->field_name,(int)dp->tdir_count);
+ return(0);
+ }
+ err=TIFFReadDirEntryShortArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C0_UINT8:
+ {
+ uint8* data;
+ assert(fip->field_readcount>=1);
+ assert(fip->field_passcount==0);
+ if (dp->tdir_count!=(uint64)fip->field_readcount) {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "incorrect count for field \"%s\", expected %d, got %d",
+ fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
+ return 0;
+ }
+ else
+ {
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C0_UINT16:
+ {
+ uint16* data;
+ assert(fip->field_readcount>=1);
+ assert(fip->field_passcount==0);
+ if (dp->tdir_count!=(uint64)fip->field_readcount)
+ /* corrupt file */;
+ else
+ {
+ err=TIFFReadDirEntryShortArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C0_UINT32:
+ {
+ uint32* data;
+ assert(fip->field_readcount>=1);
+ assert(fip->field_passcount==0);
+ if (dp->tdir_count!=(uint64)fip->field_readcount)
+ /* corrupt file */;
+ else
+ {
+ err=TIFFReadDirEntryLongArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C0_FLOAT:
+ {
+ float* data;
+ assert(fip->field_readcount>=1);
+ assert(fip->field_passcount==0);
+ if (dp->tdir_count!=(uint64)fip->field_readcount)
+ /* corrupt file */;
+ else
+ {
+ err=TIFFReadDirEntryFloatArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_ASCII:
+ {
+ uint8* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
+ data[dp->tdir_count-1] = '\0';
+ }
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_UINT8:
+ {
+ uint8* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_UINT16:
+ {
+ uint16* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryShortArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_UINT32:
+ {
+ uint32* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryLongArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_UINT64:
+ {
+ uint64* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryLong8Array(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_FLOAT:
+ {
+ float* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryFloatArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_DOUBLE:
+ {
+ double* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C16_IFD8:
+ {
+ uint64* data;
+ assert(fip->field_readcount==TIFF_VARIABLE);
+ assert(fip->field_passcount==1);
+ if (dp->tdir_count>0xFFFF)
+ err=TIFFReadDirEntryErrCount;
+ else
+ {
+ err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_ASCII:
+ {
+ uint8* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
+ data[dp->tdir_count-1] = '\0';
+ }
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_UINT8:
+ {
+ uint8* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryByteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_SINT8:
+ {
+ int8* data = NULL;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_UINT16:
+ {
+ uint16* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryShortArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_SINT16:
+ {
+ int16* data = NULL;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntrySshortArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_UINT32:
+ {
+ uint32* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryLongArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_SINT32:
+ {
+ int32* data = NULL;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntrySlongArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_UINT64:
+ {
+ uint64* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryLong8Array(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_SINT64:
+ {
+ int64* data = NULL;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_FLOAT:
+ {
+ float* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryFloatArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_DOUBLE:
+ {
+ double* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ case TIFF_SETGET_C32_IFD8:
+ {
+ uint64* data;
+ assert(fip->field_readcount==TIFF_VARIABLE2);
+ assert(fip->field_passcount==1);
+ err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ int m;
+ m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
+ if (data!=0)
+ _TIFFfree(data);
+ if (!m)
+ return(0);
+ }
+ }
+ break;
+ default:
+ assert(0); /* we should never get here */
+ break;
+ }
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ TIFFReadDirEntryOutputErr(tif,err,module,fip->field_name,recover);
+ return(0);
+ }
+ return(1);
+}
+
+/*
+ * Fetch a set of offsets or lengths.
+ * While this routine says "strips", in fact it's also used for tiles.
+ */
+static int
+TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
+{
+ static const char module[] = "TIFFFetchStripThing";
+ enum TIFFReadDirEntryErr err;
+ uint64* data;
+ err=TIFFReadDirEntryLong8ArrayWithLimit(tif,dir,&data,nstrips);
+ if (err!=TIFFReadDirEntryErrOk)
+ {
+ const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
+ TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
+ return(0);
+ }
+ if (dir->tdir_count<(uint64)nstrips)
+ {
+ uint64* resizeddata;
+ const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
+ const char* pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
+ uint32 max_nstrips = 1000000;
+ if( pszMax )
+ max_nstrips = (uint32) atoi(pszMax);
+ TIFFReadDirEntryOutputErr(tif,TIFFReadDirEntryErrCount,
+ module,
+ fip ? fip->field_name : "unknown tagname",
+ ( nstrips <= max_nstrips ) );
+
+ if( nstrips > max_nstrips )
+ {
+ _TIFFfree(data);
+ return(0);
+ }
+
+ resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
+ if (resizeddata==0) {
+ _TIFFfree(data);
+ return(0);
+ }
+ _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
+ _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
+ _TIFFfree(data);
+ data=resizeddata;
+ }
+ *lpp=data;
+ return(1);
+}
+
+/*
+ * Fetch and set the SubjectDistance EXIF tag.
+ */
+static int
+TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
+{
+ static const char module[] = "TIFFFetchSubjectDistance";
+ enum TIFFReadDirEntryErr err;
+ UInt64Aligned_t m;
+ m.l=0;
+ assert(sizeof(double)==8);
+ assert(sizeof(uint64)==8);
+ assert(sizeof(uint32)==4);
+ if (dir->tdir_count!=1)
+ err=TIFFReadDirEntryErrCount;
+ else if (dir->tdir_type!=TIFF_RATIONAL)
+ err=TIFFReadDirEntryErrType;
+ else
+ {
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 offset;
+ offset=*(uint32*)(&dir->tdir_offset);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&offset);
+ err=TIFFReadDirEntryData(tif,offset,8,m.i);
+ }
+ else
+ {
+ m.l=dir->tdir_offset.toff_long8;
+ err=TIFFReadDirEntryErrOk;
+ }
+ }
+ if (err==TIFFReadDirEntryErrOk)
+ {
+ double n;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m.i,2);
+ if (m.i[0]==0)
+ n=0.0;
+ else if (m.i[0]==0xFFFFFFFF)
+ /*
+ * XXX: Numerator 0xFFFFFFFF means that we have infinite
+ * distance. Indicate that with a negative floating point
+ * SubjectDistance value.
+ */
+ n=-1.0;
+ else
+ n=(double)m.i[0]/(double)m.i[1];
+ return(TIFFSetField(tif,dir->tdir_tag,n));
+ }
+ else
+ {
+ TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
+ return(0);
+ }
+}
+
+/*
+ * Replace a single strip (tile) of uncompressed data by multiple strips
+ * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
+ * dealing with large images or for dealing with machines with a limited
+ * amount memory.
+ */
+static void
+ChopUpSingleUncompressedStrip(TIFF* tif)
+{
+ register TIFFDirectory *td = &tif->tif_dir;
+ uint64 bytecount;
+ uint64 offset;
+ uint32 rowblock;
+ uint64 rowblockbytes;
+ uint64 stripbytes;
+ uint32 strip;
+ uint32 nstrips;
+ uint32 rowsperstrip;
+ uint64* newcounts;
+ uint64* newoffsets;
+
+ bytecount = td->td_stripbytecount[0];
+ /* On a newly created file, just re-opened to be filled, we */
+ /* don't want strip chop to trigger as it is going to cause issues */
+ /* later ( StripOffsets and StripByteCounts improperly filled) . */
+ if( bytecount == 0 && tif->tif_mode != O_RDONLY )
+ return;
+ offset = td->td_stripoffset[0];
+ assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
+ if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
+ (!isUpSampled(tif)))
+ rowblock = td->td_ycbcrsubsampling[1];
+ else
+ rowblock = 1;
+ rowblockbytes = TIFFVTileSize64(tif, rowblock);
+ /*
+ * Make the rows hold at least one scanline, but fill specified amount
+ * of data if possible.
+ */
+ if (rowblockbytes > STRIP_SIZE_DEFAULT) {
+ stripbytes = rowblockbytes;
+ rowsperstrip = rowblock;
+ } else if (rowblockbytes > 0 ) {
+ uint32 rowblocksperstrip;
+ rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
+ rowsperstrip = rowblocksperstrip * rowblock;
+ stripbytes = rowblocksperstrip * rowblockbytes;
+ }
+ else
+ return;
+
+ /*
+ * never increase the number of rows per strip
+ */
+ if (rowsperstrip >= td->td_rowsperstrip)
+ return;
+ nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
+ if( nstrips == 0 )
+ return;
+
+ /* If we are going to allocate a lot of memory, make sure that the */
+ /* file is as big as needed */
+ if( tif->tif_mode == O_RDONLY &&
+ nstrips > 1000000 &&
+ (offset >= TIFFGetFileSize(tif) ||
+ stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)) )
+ {
+ return;
+ }
+
+ newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
+ "for chopped \"StripByteCounts\" array");
+ newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
+ "for chopped \"StripOffsets\" array");
+ if (newcounts == NULL || newoffsets == NULL) {
+ /*
+ * Unable to allocate new strip information, give up and use
+ * the original one strip information.
+ */
+ if (newcounts != NULL)
+ _TIFFfree(newcounts);
+ if (newoffsets != NULL)
+ _TIFFfree(newoffsets);
+ return;
+ }
+ /*
+ * Fill the strip information arrays with new bytecounts and offsets
+ * that reflect the broken-up format.
+ */
+ for (strip = 0; strip < nstrips; strip++) {
+ if (stripbytes > bytecount)
+ stripbytes = bytecount;
+ newcounts[strip] = stripbytes;
+ newoffsets[strip] = stripbytes ? offset : 0;
+ offset += stripbytes;
+ bytecount -= stripbytes;
+ }
+ /*
+ * Replace old single strip info with multi-strip info.
+ */
+ td->td_stripsperimage = td->td_nstrips = nstrips;
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
+
+ _TIFFfree(td->td_stripbytecount);
+ _TIFFfree(td->td_stripoffset);
+ td->td_stripbytecount = newcounts;
+ td->td_stripoffset = newoffsets;
+ td->td_stripbytecountsorted = 1;
+}
+
+int _TIFFFillStriles( TIFF *tif )
+{
+ return _TIFFFillStrilesInternal( tif, 1 );
+}
+
+static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )
+{
+#if defined(DEFER_STRILE_LOAD)
+ register TIFFDirectory *td = &tif->tif_dir;
+ int return_value = 1;
+
+ if( td->td_stripoffset != NULL )
+ return 1;
+
+ if( td->td_stripoffset_entry.tdir_count == 0 )
+ return 0;
+
+ if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
+ td->td_nstrips,&td->td_stripoffset))
+ {
+ return_value = 0;
+ }
+
+ if (loadStripByteCount &&
+ !TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
+ td->td_nstrips,&td->td_stripbytecount))
+ {
+ return_value = 0;
+ }
+
+ _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
+ _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
+
+ if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
+ uint32 strip;
+
+ tif->tif_dir.td_stripbytecountsorted = 1;
+ for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
+ if (tif->tif_dir.td_stripoffset[strip - 1] >
+ tif->tif_dir.td_stripoffset[strip]) {
+ tif->tif_dir.td_stripbytecountsorted = 0;
+ break;
+ }
+ }
+ }
+
+ return return_value;
+#else /* !defined(DEFER_STRILE_LOAD) */
+ (void) tif;
+ (void) loadStripByteCount;
+ return 1;
+#endif
+}
+
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dirwrite.c b/test/monniaux/tiff-4.0.10/tif_dirwrite.c
new file mode 100644
index 00000000..c15a28db
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dirwrite.c
@@ -0,0 +1,3025 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Directory Write Support Routines.
+ */
+#include "tiffiop.h"
+#include <float.h>
+
+#ifdef HAVE_IEEEFP
+#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
+#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
+#else
+extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
+extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
+#endif
+
+static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
+
+static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
+#if 0
+static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+#endif
+
+static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
+static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
+#endif
+static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
+#if 0
+static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
+#endif
+#ifdef notdef
+static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
+#endif
+static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
+#if 0
+static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
+#endif
+static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
+static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
+static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
+#endif
+static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
+#if 0
+static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
+#endif
+static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
+static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
+#if 0
+static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
+#endif
+#ifdef notdef
+static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
+#endif
+static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
+#if 0
+static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
+#endif
+#ifdef notdef
+static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
+#endif
+static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
+#endif
+static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
+static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
+#endif
+static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+#if 0
+static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
+#endif
+#ifdef notdef
+static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+#endif
+static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
+#if 0
+static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+#endif
+static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+#endif
+static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
+static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+#endif
+static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
+static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
+static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
+
+static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
+static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
+static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
+static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
+static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
+static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
+#endif
+static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
+static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
+#endif
+static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
+#ifdef notdef
+static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
+#endif
+static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
+static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
+static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
+
+static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
+
+static int TIFFLinkDirectory(TIFF*);
+
+/*
+ * Write the contents of the current directory
+ * to the specified file. This routine doesn't
+ * handle overwriting a directory with auxiliary
+ * storage that's been changed.
+ */
+int
+TIFFWriteDirectory(TIFF* tif)
+{
+ return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
+}
+
+/*
+ * Similar to TIFFWriteDirectory(), writes the directory out
+ * but leaves all data structures in memory so that it can be
+ * written again. This will make a partially written TIFF file
+ * readable before it is successfully completed/closed.
+ */
+int
+TIFFCheckpointDirectory(TIFF* tif)
+{
+ int rc;
+ /* Setup the strips arrays, if they haven't already been. */
+ if (tif->tif_dir.td_stripoffset == NULL)
+ (void) TIFFSetupStrips(tif);
+ rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
+ (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
+ return rc;
+}
+
+int
+TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
+{
+ return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
+}
+
+/*
+ * Similar to TIFFWriteDirectory(), but if the directory has already
+ * been written once, it is relocated to the end of the file, in case it
+ * has changed in size. Note that this will result in the loss of the
+ * previously used directory space.
+ */
+int
+TIFFRewriteDirectory( TIFF *tif )
+{
+ static const char module[] = "TIFFRewriteDirectory";
+
+ /* We don't need to do anything special if it hasn't been written. */
+ if( tif->tif_diroff == 0 )
+ return TIFFWriteDirectory( tif );
+
+ /*
+ * Find and zero the pointer to this directory, so that TIFFLinkDirectory
+ * will cause it to be added after this directories current pre-link.
+ */
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
+ {
+ tif->tif_header.classic.tiff_diroff = 0;
+ tif->tif_diroff = 0;
+
+ TIFFSeekFile(tif,4,SEEK_SET);
+ if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error updating TIFF header");
+ return (0);
+ }
+ }
+ else
+ {
+ uint32 nextdir;
+ nextdir = tif->tif_header.classic.tiff_diroff;
+ while(1) {
+ uint16 dircount;
+ uint32 nextnextdir;
+
+ if (!SeekOK(tif, nextdir) ||
+ !ReadOK(tif, &dircount, 2)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ (void) TIFFSeekFile(tif,
+ nextdir+2+dircount*12, SEEK_SET);
+ if (!ReadOK(tif, &nextnextdir, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&nextnextdir);
+ if (nextnextdir==tif->tif_diroff)
+ {
+ uint32 m;
+ m=0;
+ (void) TIFFSeekFile(tif,
+ nextdir+2+dircount*12, SEEK_SET);
+ if (!WriteOK(tif, &m, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+ tif->tif_diroff=0;
+ break;
+ }
+ nextdir=nextnextdir;
+ }
+ }
+ }
+ else
+ {
+ if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
+ {
+ tif->tif_header.big.tiff_diroff = 0;
+ tif->tif_diroff = 0;
+
+ TIFFSeekFile(tif,8,SEEK_SET);
+ if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error updating TIFF header");
+ return (0);
+ }
+ }
+ else
+ {
+ uint64 nextdir;
+ nextdir = tif->tif_header.big.tiff_diroff;
+ while(1) {
+ uint64 dircount64;
+ uint16 dircount;
+ uint64 nextnextdir;
+
+ if (!SeekOK(tif, nextdir) ||
+ !ReadOK(tif, &dircount64, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>0xFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on tag count failed, likely corrupt TIFF");
+ return (0);
+ }
+ dircount=(uint16)dircount64;
+ (void) TIFFSeekFile(tif,
+ nextdir+8+dircount*20, SEEK_SET);
+ if (!ReadOK(tif, &nextnextdir, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&nextnextdir);
+ if (nextnextdir==tif->tif_diroff)
+ {
+ uint64 m;
+ m=0;
+ (void) TIFFSeekFile(tif,
+ nextdir+8+dircount*20, SEEK_SET);
+ if (!WriteOK(tif, &m, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+ tif->tif_diroff=0;
+ break;
+ }
+ nextdir=nextnextdir;
+ }
+ }
+ }
+
+ /*
+ * Now use TIFFWriteDirectory() normally.
+ */
+
+ return TIFFWriteDirectory( tif );
+}
+
+static int
+TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
+{
+ static const char module[] = "TIFFWriteDirectorySec";
+ uint32 ndir;
+ TIFFDirEntry* dir;
+ uint32 dirsize;
+ void* dirmem;
+ uint32 m;
+ if (tif->tif_mode == O_RDONLY)
+ return (1);
+
+ _TIFFFillStriles( tif );
+
+ /*
+ * Clear write state so that subsequent images with
+ * different characteristics get the right buffers
+ * setup for them.
+ */
+ if (imagedone)
+ {
+ if (tif->tif_flags & TIFF_POSTENCODE)
+ {
+ tif->tif_flags &= ~TIFF_POSTENCODE;
+ if (!(*tif->tif_postencode)(tif))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Error post-encoding before directory write");
+ return (0);
+ }
+ }
+ (*tif->tif_close)(tif); /* shutdown encoder */
+ /*
+ * Flush any data that might have been written
+ * by the compression close+cleanup routines. But
+ * be careful not to write stuff if we didn't add data
+ * in the previous steps as the "rawcc" data may well be
+ * a previously read tile/strip in mixed read/write mode.
+ */
+ if (tif->tif_rawcc > 0
+ && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
+ {
+ if( !TIFFFlushData1(tif) )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error flushing data before directory write");
+ return (0);
+ }
+ }
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
+ {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawcc = 0;
+ tif->tif_rawdatasize = 0;
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = 0;
+ }
+ tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
+ }
+ dir=NULL;
+ dirmem=NULL;
+ dirsize=0;
+ while (1)
+ {
+ ndir=0;
+ if (isimage)
+ {
+ if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
+ {
+ if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
+ goto bad;
+ if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
+ {
+ if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
+ goto bad;
+ if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_RESOLUTION))
+ {
+ if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
+ goto bad;
+ if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_POSITION))
+ {
+ if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
+ goto bad;
+ if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
+ {
+ if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
+ {
+ if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_COMPRESSION))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_FILLORDER))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_ORIENTATION))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
+ {
+ if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
+ {
+ if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
+ {
+ if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
+ {
+ if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
+ {
+ if (!isTiled(tif))
+ {
+ if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
+ goto bad;
+ }
+ else
+ {
+ if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
+ goto bad;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
+ {
+ if (!isTiled(tif))
+ {
+ /* td_stripoffset might be NULL in an odd OJPEG case. See
+ * tif_dirread.c around line 3634.
+ * XXX: OJPEG hack.
+ * If a) compression is OJPEG, b) it's not a tiled TIFF,
+ * and c) the number of strips is 1,
+ * then we tolerate the absence of stripoffsets tag,
+ * because, presumably, all required data is in the
+ * JpegInterchangeFormat stream.
+ * We can get here when using tiffset on such a file.
+ * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
+ */
+ if (tif->tif_dir.td_stripoffset != NULL &&
+ !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
+ goto bad;
+ }
+ else
+ {
+ if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
+ goto bad;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_COLORMAP))
+ {
+ if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
+ {
+ if (tif->tif_dir.td_extrasamples)
+ {
+ uint16 na;
+ uint16* nb;
+ TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
+ if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
+ goto bad;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
+ {
+ if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
+ {
+ if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
+ {
+ if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
+ {
+ if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
+ {
+ if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
+ {
+ if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
+ {
+ if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
+ {
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
+ {
+ if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
+ {
+ if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_INKNAMES))
+ {
+ if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
+ goto bad;
+ }
+ if (TIFFFieldSet(tif,FIELD_SUBIFD))
+ {
+ if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
+ goto bad;
+ }
+ {
+ uint32 n;
+ for (n=0; n<tif->tif_nfields; n++) {
+ const TIFFField* o;
+ o = tif->tif_fields[n];
+ if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
+ {
+ switch (o->get_field_type)
+ {
+ case TIFF_SETGET_ASCII:
+ {
+ uint32 pa;
+ char* pb;
+ assert(o->field_type==TIFF_ASCII);
+ assert(o->field_readcount==TIFF_VARIABLE);
+ assert(o->field_passcount==0);
+ TIFFGetField(tif,o->field_tag,&pb);
+ pa=(uint32)(strlen(pb));
+ if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
+ goto bad;
+ }
+ break;
+ case TIFF_SETGET_UINT16:
+ {
+ uint16 p;
+ assert(o->field_type==TIFF_SHORT);
+ assert(o->field_readcount==1);
+ assert(o->field_passcount==0);
+ TIFFGetField(tif,o->field_tag,&p);
+ if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))
+ goto bad;
+ }
+ break;
+ case TIFF_SETGET_UINT32:
+ {
+ uint32 p;
+ assert(o->field_type==TIFF_LONG);
+ assert(o->field_readcount==1);
+ assert(o->field_passcount==0);
+ TIFFGetField(tif,o->field_tag,&p);
+ if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))
+ goto bad;
+ }
+ break;
+ case TIFF_SETGET_C32_UINT8:
+ {
+ uint32 pa;
+ void* pb;
+ assert(o->field_type==TIFF_UNDEFINED);
+ assert(o->field_readcount==TIFF_VARIABLE2);
+ assert(o->field_passcount==1);
+ TIFFGetField(tif,o->field_tag,&pa,&pb);
+ if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
+ goto bad;
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Cannot write tag %d (%s)",
+ TIFFFieldTag(o),
+ o->field_name ? o->field_name : "unknown");
+ goto bad;
+ }
+ }
+ }
+ }
+ }
+ for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
+ {
+ uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;
+ uint32 count = tif->tif_dir.td_customValues[m].count;
+ switch (tif->tif_dir.td_customValues[m].info->field_type)
+ {
+ case TIFF_ASCII:
+ if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_UNDEFINED:
+ if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_BYTE:
+ if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SBYTE:
+ if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SHORT:
+ if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SSHORT:
+ if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_LONG:
+ if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SLONG:
+ if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_LONG8:
+ if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SLONG8:
+ if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_RATIONAL:
+ if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_SRATIONAL:
+ if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_FLOAT:
+ if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_DOUBLE:
+ if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_IFD:
+ if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ case TIFF_IFD8:
+ if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
+ goto bad;
+ break;
+ default:
+ assert(0); /* we should never get here */
+ break;
+ }
+ }
+ if (dir!=NULL)
+ break;
+ dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
+ if (dir==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ goto bad;
+ }
+ if (isimage)
+ {
+ if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
+ goto bad;
+ }
+ else
+ tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
+ if (pdiroff!=NULL)
+ *pdiroff=tif->tif_diroff;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ dirsize=2+ndir*12+4;
+ else
+ dirsize=8+ndir*20+8;
+ tif->tif_dataoff=tif->tif_diroff+dirsize;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ tif->tif_dataoff=(uint32)tif->tif_dataoff;
+ if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
+ goto bad;
+ }
+ if (tif->tif_dataoff&1)
+ tif->tif_dataoff++;
+ if (isimage)
+ tif->tif_curdir++;
+ }
+ if (isimage)
+ {
+ if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
+ {
+ uint32 na;
+ TIFFDirEntry* nb;
+ for (na=0, nb=dir; ; na++, nb++)
+ {
+ if( na == ndir )
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Cannot find SubIFD tag");
+ goto bad;
+ }
+ if (nb->tdir_tag==TIFFTAG_SUBIFD)
+ break;
+ }
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
+ else
+ tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
+ }
+ }
+ dirmem=_TIFFmalloc(dirsize);
+ if (dirmem==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ goto bad;
+ }
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint8* n;
+ uint32 nTmp;
+ TIFFDirEntry* o;
+ n=dirmem;
+ *(uint16*)n=(uint16)ndir;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)n);
+ n+=2;
+ o=dir;
+ for (m=0; m<ndir; m++)
+ {
+ *(uint16*)n=o->tdir_tag;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)n);
+ n+=2;
+ *(uint16*)n=o->tdir_type;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)n);
+ n+=2;
+ nTmp = (uint32)o->tdir_count;
+ _TIFFmemcpy(n,&nTmp,4);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)n);
+ n+=4;
+ /* This is correct. The data has been */
+ /* swabbed previously in TIFFWriteDirectoryTagData */
+ _TIFFmemcpy(n,&o->tdir_offset,4);
+ n+=4;
+ o++;
+ }
+ nTmp = (uint32)tif->tif_nextdiroff;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&nTmp);
+ _TIFFmemcpy(n,&nTmp,4);
+ }
+ else
+ {
+ uint8* n;
+ TIFFDirEntry* o;
+ n=dirmem;
+ *(uint64*)n=ndir;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)n);
+ n+=8;
+ o=dir;
+ for (m=0; m<ndir; m++)
+ {
+ *(uint16*)n=o->tdir_tag;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)n);
+ n+=2;
+ *(uint16*)n=o->tdir_type;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)n);
+ n+=2;
+ _TIFFmemcpy(n,&o->tdir_count,8);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)n);
+ n+=8;
+ _TIFFmemcpy(n,&o->tdir_offset,8);
+ n+=8;
+ o++;
+ }
+ _TIFFmemcpy(n,&tif->tif_nextdiroff,8);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)n);
+ }
+ _TIFFfree(dir);
+ dir=NULL;
+ if (!SeekOK(tif,tif->tif_diroff))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
+ goto bad;
+ }
+ if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
+ goto bad;
+ }
+ _TIFFfree(dirmem);
+ if (imagedone)
+ {
+ TIFFFreeDirectory(tif);
+ tif->tif_flags &= ~TIFF_DIRTYDIRECT;
+ tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+ (*tif->tif_cleanup)(tif);
+ /*
+ * Reset directory-related state for subsequent
+ * directories.
+ */
+ TIFFCreateDirectory(tif);
+ }
+ return(1);
+bad:
+ if (dir!=NULL)
+ _TIFFfree(dir);
+ if (dirmem!=NULL)
+ _TIFFfree(dirmem);
+ return(0);
+}
+
+static float TIFFClampDoubleToFloat( double val )
+{
+ if( val > FLT_MAX )
+ return FLT_MAX;
+ if( val < -FLT_MAX )
+ return -FLT_MAX;
+ return (float)val;
+}
+
+static int8 TIFFClampDoubleToInt8( double val )
+{
+ if( val > 127 )
+ return 127;
+ if( val < -128 || val != val )
+ return -128;
+ return (int8)val;
+}
+
+static int16 TIFFClampDoubleToInt16( double val )
+{
+ if( val > 32767 )
+ return 32767;
+ if( val < -32768 || val != val )
+ return -32768;
+ return (int16)val;
+}
+
+static int32 TIFFClampDoubleToInt32( double val )
+{
+ if( val > 0x7FFFFFFF )
+ return 0x7FFFFFFF;
+ if( val < -0x7FFFFFFF-1 || val != val )
+ return -0x7FFFFFFF-1;
+ return (int32)val;
+}
+
+static uint8 TIFFClampDoubleToUInt8( double val )
+{
+ if( val < 0 )
+ return 0;
+ if( val > 255 || val != val )
+ return 255;
+ return (uint8)val;
+}
+
+static uint16 TIFFClampDoubleToUInt16( double val )
+{
+ if( val < 0 )
+ return 0;
+ if( val > 65535 || val != val )
+ return 65535;
+ return (uint16)val;
+}
+
+static uint32 TIFFClampDoubleToUInt32( double val )
+{
+ if( val < 0 )
+ return 0;
+ if( val > 0xFFFFFFFFU || val != val )
+ return 0xFFFFFFFFU;
+ return (uint32)val;
+}
+
+static int
+TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
+ void* conv;
+ uint32 i;
+ int ok;
+ conv = _TIFFmalloc(count*sizeof(double));
+ if (conv == NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
+ return (0);
+ }
+
+ switch (tif->tif_dir.td_sampleformat)
+ {
+ case SAMPLEFORMAT_IEEEFP:
+ if (tif->tif_dir.td_bitspersample<=32)
+ {
+ for (i = 0; i < count; ++i)
+ ((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
+ ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
+ }
+ else
+ {
+ ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
+ }
+ break;
+ case SAMPLEFORMAT_INT:
+ if (tif->tif_dir.td_bitspersample<=8)
+ {
+ for (i = 0; i < count; ++i)
+ ((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
+ ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
+ }
+ else if (tif->tif_dir.td_bitspersample<=16)
+ {
+ for (i = 0; i < count; ++i)
+ ((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
+ ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
+ }
+ else
+ {
+ for (i = 0; i < count; ++i)
+ ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
+ ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
+ }
+ break;
+ case SAMPLEFORMAT_UINT:
+ if (tif->tif_dir.td_bitspersample<=8)
+ {
+ for (i = 0; i < count; ++i)
+ ((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
+ ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
+ }
+ else if (tif->tif_dir.td_bitspersample<=16)
+ {
+ for (i = 0; i < count; ++i)
+ ((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
+ ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
+ }
+ else
+ {
+ for (i = 0; i < count; ++i)
+ ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
+ ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
+ }
+ break;
+ default:
+ ok = 0;
+ }
+
+ _TIFFfree(conv);
+ return (ok);
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ switch (tif->tif_dir.td_sampleformat)
+ {
+ case SAMPLEFORMAT_IEEEFP:
+ if (tif->tif_dir.td_bitspersample<=32)
+ return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
+ else
+ return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
+ case SAMPLEFORMAT_INT:
+ if (tif->tif_dir.td_bitspersample<=8)
+ return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
+ else if (tif->tif_dir.td_bitspersample<=16)
+ return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
+ else
+ return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
+ case SAMPLEFORMAT_UINT:
+ if (tif->tif_dir.td_bitspersample<=8)
+ return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
+ else if (tif->tif_dir.td_bitspersample<=16)
+ return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));
+ else
+ return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
+ default:
+ return(1);
+ }
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
+ uint8* m;
+ uint8* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
+ int8* m;
+ int8* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
+}
+
+static int
+TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
+ uint16* m;
+ uint16* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
+ int16* m;
+ int16* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
+}
+
+static int
+TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
+ uint32* m;
+ uint32* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int
+TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
+ int32* m;
+ int32* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
+}
+
+static int
+TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
+}
+
+#ifdef notdef
+static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
+ float* m;
+ float* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+#ifdef notdef
+static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
+}
+#endif
+
+static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
+}
+
+#if 0
+static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
+ double* m;
+ double* na;
+ uint16 nb;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
+ *na=value;
+ o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
+ _TIFFfree(m);
+ return(o);
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
+{
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ if (value<=0xFFFF)
+ return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16)value));
+ else
+ return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
+}
+
+/************************************************************************/
+/* TIFFWriteDirectoryTagLongLong8Array() */
+/* */
+/* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
+/* Classic TIFF with some checking. */
+/************************************************************************/
+
+static int
+TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
+ uint64* ma;
+ uint32 mb;
+ uint32* p;
+ uint32* q;
+ int o;
+
+ /* is this just a counting pass? */
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+
+ /* We always write LONG8 for BigTIFF, no checking needed. */
+ if( tif->tif_flags&TIFF_BIGTIFF )
+ return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
+ tag,count,value);
+
+ /*
+ ** For classic tiff we want to verify everything is in range for LONG
+ ** and convert to long format.
+ */
+
+ p = _TIFFmalloc(count*sizeof(uint32));
+ if (p==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+
+ for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
+ {
+ if (*ma>0xFFFFFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
+ _TIFFfree(p);
+ return(0);
+ }
+ *q= (uint32)(*ma);
+ }
+
+ o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
+ _TIFFfree(p);
+
+ return(o);
+}
+
+/************************************************************************/
+/* TIFFWriteDirectoryTagIfdIfd8Array() */
+/* */
+/* Write either IFD8 or IFD array depending on file type. */
+/************************************************************************/
+
+static int
+TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
+ uint64* ma;
+ uint32 mb;
+ uint32* p;
+ uint32* q;
+ int o;
+
+ /* is this just a counting pass? */
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+
+ /* We always write IFD8 for BigTIFF, no checking needed. */
+ if( tif->tif_flags&TIFF_BIGTIFF )
+ return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
+ tag,count,value);
+
+ /*
+ ** For classic tiff we want to verify everything is in range for IFD
+ ** and convert to long format.
+ */
+
+ p = _TIFFmalloc(count*sizeof(uint32));
+ if (p==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+
+ for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
+ {
+ if (*ma>0xFFFFFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
+ _TIFFfree(p);
+ return(0);
+ }
+ *q= (uint32)(*ma);
+ }
+
+ o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
+ _TIFFfree(p);
+
+ return(o);
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
+ uint64* ma;
+ uint32 mb;
+ uint8 n;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ n=0;
+ for (ma=value, mb=0; mb<count; ma++, mb++)
+ {
+ if ((n==0)&&(*ma>0xFFFF))
+ n=1;
+ if ((n==1)&&(*ma>0xFFFFFFFF))
+ {
+ n=2;
+ break;
+ }
+ }
+ if (n==0)
+ {
+ uint16* p;
+ uint16* q;
+ p=_TIFFmalloc(count*sizeof(uint16));
+ if (p==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
+ *q=(uint16)(*ma);
+ o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
+ _TIFFfree(p);
+ }
+ else if (n==1)
+ {
+ uint32* p;
+ uint32* q;
+ p=_TIFFmalloc(count*sizeof(uint32));
+ if (p==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
+ *q=(uint32)(*ma);
+ o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
+ _TIFFfree(p);
+ }
+ else
+ {
+ assert(n==2);
+ o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
+ }
+ return(o);
+}
+#endif
+static int
+TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
+{
+ static const char module[] = "TIFFWriteDirectoryTagColormap";
+ uint32 m;
+ uint16* n;
+ int o;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=(1<<tif->tif_dir.td_bitspersample);
+ n=_TIFFmalloc(3*m*sizeof(uint16));
+ if (n==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ _TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
+ _TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
+ _TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
+ o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
+ _TIFFfree(n);
+ return(o);
+}
+
+static int
+TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
+{
+ static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
+ uint32 m;
+ uint16 n;
+ uint16* o;
+ int p;
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=(1<<tif->tif_dir.td_bitspersample);
+ n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
+ /*
+ * Check if the table can be written as a single column,
+ * or if it must be written as 3 columns. Note that we
+ * write a 3-column tag if there are 2 samples/pixel and
+ * a single column of data won't suffice--hmm.
+ */
+ if (n>3)
+ n=3;
+ if (n==3)
+ {
+ if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16)))
+ n=2;
+ }
+ if (n==2)
+ {
+ if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16)))
+ n=1;
+ }
+ if (n==0)
+ n=1;
+ o=_TIFFmalloc(n*m*sizeof(uint16));
+ if (o==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ _TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
+ if (n>1)
+ _TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
+ if (n>2)
+ _TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
+ p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
+ _TIFFfree(o);
+ return(p);
+}
+
+static int
+TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
+{
+ static const char module[] = "TIFFWriteDirectoryTagSubifd";
+ uint64 m;
+ int n;
+ if (tif->tif_dir.td_nsubifd==0)
+ return(1);
+ if (dir==NULL)
+ {
+ (*ndir)++;
+ return(1);
+ }
+ m=tif->tif_dataoff;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32* o;
+ uint64* pa;
+ uint32* pb;
+ uint16 p;
+ o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
+ if (o==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ pa=tif->tif_dir.td_subifd;
+ pb=o;
+ for (p=0; p < tif->tif_dir.td_nsubifd; p++)
+ {
+ assert(pa != 0);
+
+ /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
+ if( *pa > 0xFFFFFFFFUL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Illegal value for SubIFD tag");
+ _TIFFfree(o);
+ return(0);
+ }
+ *pb++=(uint32)(*pa++);
+ }
+ n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
+ _TIFFfree(o);
+ }
+ else
+ n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
+ if (!n)
+ return(0);
+ /*
+ * Total hack: if this directory includes a SubIFD
+ * tag then force the next <n> directories to be
+ * written as ``sub directories'' of this one. This
+ * is used to write things like thumbnails and
+ * image masks that one wants to keep out of the
+ * normal directory linkage access mechanism.
+ */
+ tif->tif_flags|=TIFF_INSUBIFD;
+ tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
+ if (tif->tif_dir.td_nsubifd==1)
+ tif->tif_subifdoff=0;
+ else
+ tif->tif_subifdoff=m;
+ return(1);
+}
+
+static int
+TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
+{
+ assert(sizeof(char)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
+{
+ assert(sizeof(uint8)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
+{
+ assert(sizeof(uint8)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
+{
+ assert(sizeof(uint8)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
+{
+ assert(sizeof(int8)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
+{
+ assert(sizeof(int8)==1);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
+{
+ uint16 m;
+ assert(sizeof(uint16)==2);
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort(&m);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
+{
+ assert(count<0x80000000);
+ assert(sizeof(uint16)==2);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfShort(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
+{
+ int16 m;
+ assert(sizeof(int16)==2);
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort((uint16*)(&m));
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
+{
+ assert(count<0x80000000);
+ assert(sizeof(int16)==2);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfShort((uint16*)value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
+{
+ uint32 m;
+ assert(sizeof(uint32)==4);
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&m);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
+{
+ assert(count<0x40000000);
+ assert(sizeof(uint32)==4);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
+{
+ int32 m;
+ assert(sizeof(int32)==4);
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong((uint32*)(&m));
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
+{
+ assert(count<0x40000000);
+ assert(sizeof(int32)==4);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong((uint32*)value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
+{
+ uint64 m;
+ assert(sizeof(uint64)==8);
+ if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
+ TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
+ return(0);
+ }
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(&m);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ assert(count<0x20000000);
+ assert(sizeof(uint64)==8);
+ if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
+ TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
+ return(0);
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
+{
+ int64 m;
+ assert(sizeof(int64)==8);
+ if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
+ TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
+ return(0);
+ }
+ m=value;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8((uint64*)(&m));
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
+{
+ assert(count<0x20000000);
+ assert(sizeof(int64)==8);
+ if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
+ TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
+ return(0);
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8((uint64*)value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
+ uint32 m[2];
+ assert(sizeof(uint32)==4);
+ if( value < 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
+ return 0;
+ }
+ else if( value != value )
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
+ return 0;
+ }
+ else if (value==0.0)
+ {
+ m[0]=0;
+ m[1]=1;
+ }
+ else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
+ {
+ m[0]=(uint32)value;
+ m[1]=1;
+ }
+ else if (value<1.0)
+ {
+ m[0]=(uint32)(value*0xFFFFFFFF);
+ m[1]=0xFFFFFFFF;
+ }
+ else
+ {
+ m[0]=0xFFFFFFFF;
+ m[1]=(uint32)(0xFFFFFFFF/value);
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ {
+ TIFFSwabLong(&m[0]);
+ TIFFSwabLong(&m[1]);
+ }
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
+ uint32* m;
+ float* na;
+ uint32* nb;
+ uint32 nc;
+ int o;
+ assert(sizeof(uint32)==4);
+ m=_TIFFmalloc(count*2*sizeof(uint32));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
+ {
+ if (*na<=0.0 || *na != *na)
+ {
+ nb[0]=0;
+ nb[1]=1;
+ }
+ else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
+ *na==(float)(uint32)(*na))
+ {
+ nb[0]=(uint32)(*na);
+ nb[1]=1;
+ }
+ else if (*na<1.0)
+ {
+ nb[0]=(uint32)((double)(*na)*0xFFFFFFFF);
+ nb[1]=0xFFFFFFFF;
+ }
+ else
+ {
+ nb[0]=0xFFFFFFFF;
+ nb[1]=(uint32)((double)0xFFFFFFFF/(*na));
+ }
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(m,count*2);
+ o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
+ _TIFFfree(m);
+ return(o);
+}
+
+static int
+TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
+ int32* m;
+ float* na;
+ int32* nb;
+ uint32 nc;
+ int o;
+ assert(sizeof(int32)==4);
+ m=_TIFFmalloc(count*2*sizeof(int32));
+ if (m==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
+ {
+ if (*na<0.0)
+ {
+ if (*na==(int32)(*na))
+ {
+ nb[0]=(int32)(*na);
+ nb[1]=1;
+ }
+ else if (*na>-1.0)
+ {
+ nb[0]=-(int32)((double)(-*na)*0x7FFFFFFF);
+ nb[1]=0x7FFFFFFF;
+ }
+ else
+ {
+ nb[0]=-0x7FFFFFFF;
+ nb[1]=(int32)((double)0x7FFFFFFF/(-*na));
+ }
+ }
+ else
+ {
+ if (*na==(int32)(*na))
+ {
+ nb[0]=(int32)(*na);
+ nb[1]=1;
+ }
+ else if (*na<1.0)
+ {
+ nb[0]=(int32)((double)(*na)*0x7FFFFFFF);
+ nb[1]=0x7FFFFFFF;
+ }
+ else
+ {
+ nb[0]=0x7FFFFFFF;
+ nb[1]=(int32)((double)0x7FFFFFFF/(*na));
+ }
+ }
+ }
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong((uint32*)m,count*2);
+ o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
+ _TIFFfree(m);
+ return(o);
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
+{
+ float m;
+ assert(sizeof(float)==4);
+ m=value;
+ TIFFCvtNativeToIEEEFloat(tif,1,&m);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabFloat(&m);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
+{
+ assert(count<0x40000000);
+ assert(sizeof(float)==4);
+ TIFFCvtNativeToIEEEFloat(tif,count,&value);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfFloat(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
+}
+
+#ifdef notdef
+static int
+TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
+{
+ double m;
+ assert(sizeof(double)==8);
+ m=value;
+ TIFFCvtNativeToIEEEDouble(tif,1,&m);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabDouble(&m);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
+}
+#endif
+
+static int
+TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
+{
+ assert(count<0x20000000);
+ assert(sizeof(double)==8);
+ TIFFCvtNativeToIEEEDouble(tif,count,&value);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfDouble(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
+{
+ assert(count<0x40000000);
+ assert(sizeof(uint32)==4);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
+}
+
+static int
+TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
+{
+ assert(count<0x20000000);
+ assert(sizeof(uint64)==8);
+ assert(tif->tif_flags&TIFF_BIGTIFF);
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabArrayOfLong8(value,count);
+ return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
+}
+
+static int
+TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
+{
+ static const char module[] = "TIFFWriteDirectoryTagData";
+ uint32 m;
+ m=0;
+ while (m<(*ndir))
+ {
+ assert(dir[m].tdir_tag!=tag);
+ if (dir[m].tdir_tag>tag)
+ break;
+ m++;
+ }
+ if (m<(*ndir))
+ {
+ uint32 n;
+ for (n=*ndir; n>m; n--)
+ dir[n]=dir[n-1];
+ }
+ dir[m].tdir_tag=tag;
+ dir[m].tdir_type=datatype;
+ dir[m].tdir_count=count;
+ dir[m].tdir_offset.toff_long8 = 0;
+ if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
+ _TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
+ else
+ {
+ uint64 na,nb;
+ na=tif->tif_dataoff;
+ nb=na+datalength;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ nb=(uint32)nb;
+ if ((nb<na)||(nb<datalength))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
+ return(0);
+ }
+ if (!SeekOK(tif,na))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
+ return(0);
+ }
+ assert(datalength<0x80000000UL);
+ if (!WriteOK(tif,data,(tmsize_t)datalength))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
+ return(0);
+ }
+ tif->tif_dataoff=nb;
+ if (tif->tif_dataoff&1)
+ tif->tif_dataoff++;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 o;
+ o=(uint32)na;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong(&o);
+ _TIFFmemcpy(&dir[m].tdir_offset,&o,4);
+ }
+ else
+ {
+ dir[m].tdir_offset.toff_long8 = na;
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
+ }
+ }
+ (*ndir)++;
+ return(1);
+}
+
+/*
+ * Link the current directory into the directory chain for the file.
+ */
+static int
+TIFFLinkDirectory(TIFF* tif)
+{
+ static const char module[] = "TIFFLinkDirectory";
+
+ tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
+
+ /*
+ * Handle SubIFDs
+ */
+ if (tif->tif_flags & TIFF_INSUBIFD)
+ {
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 m;
+ m = (uint32)tif->tif_diroff;
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&m);
+ (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
+ if (!WriteOK(tif, &m, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing SubIFD directory link");
+ return (0);
+ }
+ /*
+ * Advance to the next SubIFD or, if this is
+ * the last one configured, revert back to the
+ * normal directory linkage.
+ */
+ if (--tif->tif_nsubifd)
+ tif->tif_subifdoff += 4;
+ else
+ tif->tif_flags &= ~TIFF_INSUBIFD;
+ return (1);
+ }
+ else
+ {
+ uint64 m;
+ m = tif->tif_diroff;
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&m);
+ (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
+ if (!WriteOK(tif, &m, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing SubIFD directory link");
+ return (0);
+ }
+ /*
+ * Advance to the next SubIFD or, if this is
+ * the last one configured, revert back to the
+ * normal directory linkage.
+ */
+ if (--tif->tif_nsubifd)
+ tif->tif_subifdoff += 8;
+ else
+ tif->tif_flags &= ~TIFF_INSUBIFD;
+ return (1);
+ }
+ }
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 m;
+ uint32 nextdir;
+ m = (uint32)(tif->tif_diroff);
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&m);
+ if (tif->tif_header.classic.tiff_diroff == 0) {
+ /*
+ * First directory, overwrite offset in header.
+ */
+ tif->tif_header.classic.tiff_diroff = (uint32) tif->tif_diroff;
+ (void) TIFFSeekFile(tif,4, SEEK_SET);
+ if (!WriteOK(tif, &m, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing TIFF header");
+ return (0);
+ }
+ return (1);
+ }
+ /*
+ * Not the first directory, search to the last and append.
+ */
+ nextdir = tif->tif_header.classic.tiff_diroff;
+ while(1) {
+ uint16 dircount;
+ uint32 nextnextdir;
+
+ if (!SeekOK(tif, nextdir) ||
+ !ReadOK(tif, &dircount, 2)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ (void) TIFFSeekFile(tif,
+ nextdir+2+dircount*12, SEEK_SET);
+ if (!ReadOK(tif, &nextnextdir, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&nextnextdir);
+ if (nextnextdir==0)
+ {
+ (void) TIFFSeekFile(tif,
+ nextdir+2+dircount*12, SEEK_SET);
+ if (!WriteOK(tif, &m, 4)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+ break;
+ }
+ nextdir=nextnextdir;
+ }
+ }
+ else
+ {
+ uint64 m;
+ uint64 nextdir;
+ m = tif->tif_diroff;
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&m);
+ if (tif->tif_header.big.tiff_diroff == 0) {
+ /*
+ * First directory, overwrite offset in header.
+ */
+ tif->tif_header.big.tiff_diroff = tif->tif_diroff;
+ (void) TIFFSeekFile(tif,8, SEEK_SET);
+ if (!WriteOK(tif, &m, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing TIFF header");
+ return (0);
+ }
+ return (1);
+ }
+ /*
+ * Not the first directory, search to the last and append.
+ */
+ nextdir = tif->tif_header.big.tiff_diroff;
+ while(1) {
+ uint64 dircount64;
+ uint16 dircount;
+ uint64 nextnextdir;
+
+ if (!SeekOK(tif, nextdir) ||
+ !ReadOK(tif, &dircount64, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ if (dircount64>0xFFFF)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sanity check on tag count failed, likely corrupt TIFF");
+ return (0);
+ }
+ dircount=(uint16)dircount64;
+ (void) TIFFSeekFile(tif,
+ nextdir+8+dircount*20, SEEK_SET);
+ if (!ReadOK(tif, &nextnextdir, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&nextnextdir);
+ if (nextnextdir==0)
+ {
+ (void) TIFFSeekFile(tif,
+ nextdir+8+dircount*20, SEEK_SET);
+ if (!WriteOK(tif, &m, 8)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+ break;
+ }
+ nextdir=nextnextdir;
+ }
+ }
+ return (1);
+}
+
+/************************************************************************/
+/* TIFFRewriteField() */
+/* */
+/* Rewrite a field in the directory on disk without regard to */
+/* updating the TIFF directory structure in memory. Currently */
+/* only supported for field that already exist in the on-disk */
+/* directory. Mainly used for updating stripoffset / */
+/* stripbytecount values after the directory is already on */
+/* disk. */
+/* */
+/* Returns zero on failure, and one on success. */
+/************************************************************************/
+
+int
+_TIFFRewriteField(TIFF* tif, uint16 tag, TIFFDataType in_datatype,
+ tmsize_t count, void* data)
+{
+ static const char module[] = "TIFFResetField";
+ /* const TIFFField* fip = NULL; */
+ uint16 dircount;
+ tmsize_t dirsize;
+ uint8 direntry_raw[20];
+ uint16 entry_tag = 0;
+ uint16 entry_type = 0;
+ uint64 entry_count = 0;
+ uint64 entry_offset = 0;
+ int value_in_entry = 0;
+ uint64 read_offset;
+ uint8 *buf_to_write = NULL;
+ TIFFDataType datatype;
+
+/* -------------------------------------------------------------------- */
+/* Find field definition. */
+/* -------------------------------------------------------------------- */
+ /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
+
+/* -------------------------------------------------------------------- */
+/* Do some checking this is a straight forward case. */
+/* -------------------------------------------------------------------- */
+ if( isMapped(tif) )
+ {
+ TIFFErrorExt( tif->tif_clientdata, module,
+ "Memory mapped files not currently supported for this operation." );
+ return 0;
+ }
+
+ if( tif->tif_diroff == 0 )
+ {
+ TIFFErrorExt( tif->tif_clientdata, module,
+ "Attempt to reset field on directory not already on disk." );
+ return 0;
+ }
+
+/* -------------------------------------------------------------------- */
+/* Read the directory entry count. */
+/* -------------------------------------------------------------------- */
+ if (!SeekOK(tif, tif->tif_diroff)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Seek error accessing TIFF directory",
+ tif->tif_name);
+ return 0;
+ }
+
+ read_offset = tif->tif_diroff;
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if (!ReadOK(tif, &dircount, sizeof (uint16))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ dirsize = 12;
+ read_offset += 2;
+ } else {
+ uint64 dircount64;
+ if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong8(&dircount64);
+ dircount = (uint16)dircount64;
+ dirsize = 20;
+ read_offset += 8;
+ }
+
+/* -------------------------------------------------------------------- */
+/* Read through directory to find target tag. */
+/* -------------------------------------------------------------------- */
+ while( dircount > 0 )
+ {
+ if (!ReadOK(tif, direntry_raw, dirsize)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory entry.",
+ tif->tif_name);
+ return 0;
+ }
+
+ memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort( &entry_tag );
+
+ if( entry_tag == tag )
+ break;
+
+ read_offset += dirsize;
+ }
+
+ if( entry_tag != tag )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Could not find tag %d.",
+ tif->tif_name, tag );
+ return 0;
+ }
+
+/* -------------------------------------------------------------------- */
+/* Extract the type, count and offset for this entry. */
+/* -------------------------------------------------------------------- */
+ memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort( &entry_type );
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 value;
+
+ memcpy( &value, direntry_raw + 4, sizeof(uint32) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong( &value );
+ entry_count = value;
+
+ memcpy( &value, direntry_raw + 8, sizeof(uint32) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong( &value );
+ entry_offset = value;
+ }
+ else
+ {
+ memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8( &entry_count );
+
+ memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8( &entry_offset );
+ }
+
+/* -------------------------------------------------------------------- */
+/* What data type do we want to write this as? */
+/* -------------------------------------------------------------------- */
+ if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
+ {
+ if( in_datatype == TIFF_LONG8 )
+ datatype = TIFF_LONG;
+ else if( in_datatype == TIFF_SLONG8 )
+ datatype = TIFF_SLONG;
+ else if( in_datatype == TIFF_IFD8 )
+ datatype = TIFF_IFD;
+ else
+ datatype = in_datatype;
+ }
+ else
+ datatype = in_datatype;
+
+/* -------------------------------------------------------------------- */
+/* Prepare buffer of actual data to write. This includes */
+/* swabbing as needed. */
+/* -------------------------------------------------------------------- */
+ buf_to_write =
+ (uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
+ "for field buffer.");
+ if (!buf_to_write)
+ return 0;
+
+ if( datatype == in_datatype )
+ memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
+ else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
+ {
+ tmsize_t i;
+
+ for( i = 0; i < count; i++ )
+ {
+ ((int32 *) buf_to_write)[i] =
+ (int32) ((int64 *) data)[i];
+ if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
+ {
+ _TIFFfree( buf_to_write );
+ TIFFErrorExt( tif->tif_clientdata, module,
+ "Value exceeds 32bit range of output type." );
+ return 0;
+ }
+ }
+ }
+ else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
+ || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
+ {
+ tmsize_t i;
+
+ for( i = 0; i < count; i++ )
+ {
+ ((uint32 *) buf_to_write)[i] =
+ (uint32) ((uint64 *) data)[i];
+ if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
+ {
+ _TIFFfree( buf_to_write );
+ TIFFErrorExt( tif->tif_clientdata, module,
+ "Value exceeds 32bit range of output type." );
+ return 0;
+ }
+ }
+ }
+
+ if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
+ {
+ if( TIFFDataWidth(datatype) == 2 )
+ TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
+ else if( TIFFDataWidth(datatype) == 4 )
+ TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
+ else if( TIFFDataWidth(datatype) == 8 )
+ TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
+ }
+
+/* -------------------------------------------------------------------- */
+/* Is this a value that fits into the directory entry? */
+/* -------------------------------------------------------------------- */
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ if( TIFFDataWidth(datatype) * count <= 4 )
+ {
+ entry_offset = read_offset + 8;
+ value_in_entry = 1;
+ }
+ }
+ else
+ {
+ if( TIFFDataWidth(datatype) * count <= 8 )
+ {
+ entry_offset = read_offset + 12;
+ value_in_entry = 1;
+ }
+ }
+
+/* -------------------------------------------------------------------- */
+/* If the tag type, and count match, then we just write it out */
+/* over the old values without altering the directory entry at */
+/* all. */
+/* -------------------------------------------------------------------- */
+ if( entry_count == (uint64)count && entry_type == (uint16) datatype )
+ {
+ if (!SeekOK(tif, entry_offset)) {
+ _TIFFfree( buf_to_write );
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Seek error accessing TIFF directory",
+ tif->tif_name);
+ return 0;
+ }
+ if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
+ _TIFFfree( buf_to_write );
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+
+ _TIFFfree( buf_to_write );
+ return 1;
+ }
+
+/* -------------------------------------------------------------------- */
+/* Otherwise, we write the new tag data at the end of the file. */
+/* -------------------------------------------------------------------- */
+ if( !value_in_entry )
+ {
+ entry_offset = TIFFSeekFile(tif,0,SEEK_END);
+
+ if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
+ _TIFFfree( buf_to_write );
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
+ return (0);
+ }
+ }
+ else
+ {
+ memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
+ }
+
+ _TIFFfree( buf_to_write );
+ buf_to_write = 0;
+
+/* -------------------------------------------------------------------- */
+/* Adjust the directory entry. */
+/* -------------------------------------------------------------------- */
+ entry_type = datatype;
+ memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
+
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ uint32 value;
+
+ value = (uint32) entry_count;
+ memcpy( direntry_raw + 4, &value, sizeof(uint32) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
+
+ value = (uint32) entry_offset;
+ memcpy( direntry_raw + 8, &value, sizeof(uint32) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
+ }
+ else
+ {
+ memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
+
+ memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
+ if (tif->tif_flags&TIFF_SWAB)
+ TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
+ }
+
+/* -------------------------------------------------------------------- */
+/* Write the directory entry out to disk. */
+/* -------------------------------------------------------------------- */
+ if (!SeekOK(tif, read_offset )) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Seek error accessing TIFF directory",
+ tif->tif_name);
+ return 0;
+ }
+
+ if (!WriteOK(tif, direntry_raw,dirsize))
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not write TIFF directory entry.",
+ tif->tif_name);
+ return 0;
+ }
+
+ return 1;
+}
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_dumpmode.c b/test/monniaux/tiff-4.0.10/tif_dumpmode.c
new file mode 100644
index 00000000..4a0b07f5
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_dumpmode.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * "Null" Compression Algorithm Support.
+ */
+#include "tiffiop.h"
+
+static int
+DumpFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+/*
+ * Encode a hunk of pixels.
+ */
+static int
+DumpModeEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
+{
+ (void) s;
+ while (cc > 0) {
+ tmsize_t n;
+
+ n = cc;
+ if (tif->tif_rawcc + n > tif->tif_rawdatasize)
+ n = tif->tif_rawdatasize - tif->tif_rawcc;
+
+ assert( n > 0 );
+
+ /*
+ * Avoid copy if client has setup raw
+ * data buffer to avoid extra copy.
+ */
+ if (tif->tif_rawcp != pp)
+ _TIFFmemcpy(tif->tif_rawcp, pp, n);
+ tif->tif_rawcp += n;
+ tif->tif_rawcc += n;
+ pp += n;
+ cc -= n;
+ if (tif->tif_rawcc >= tif->tif_rawdatasize &&
+ !TIFFFlushData1(tif))
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * Decode a hunk of pixels.
+ */
+static int
+DumpModeDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "DumpModeDecode";
+ (void) s;
+ if (tif->tif_rawcc < cc) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+"Not enough data for scanline %lu, expected a request for at most %I64d bytes, got a request for %I64d bytes",
+ (unsigned long) tif->tif_row,
+ (signed __int64) tif->tif_rawcc,
+ (signed __int64) cc);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+"Not enough data for scanline %lu, expected a request for at most %lld bytes, got a request for %lld bytes",
+ (unsigned long) tif->tif_row,
+ (signed long long) tif->tif_rawcc,
+ (signed long long) cc);
+#endif
+ return (0);
+ }
+ /*
+ * Avoid copy if client has setup raw
+ * data buffer to avoid extra copy.
+ */
+ if (tif->tif_rawcp != buf)
+ _TIFFmemcpy(buf, tif->tif_rawcp, cc);
+ tif->tif_rawcp += cc;
+ tif->tif_rawcc -= cc;
+ return (1);
+}
+
+/*
+ * Seek forwards nrows in the current strip.
+ */
+static int
+DumpModeSeek(TIFF* tif, uint32 nrows)
+{
+ tif->tif_rawcp += nrows * tif->tif_scanlinesize;
+ tif->tif_rawcc -= nrows * tif->tif_scanlinesize;
+ return (1);
+}
+
+/*
+ * Initialize dump mode.
+ */
+int
+TIFFInitDumpMode(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ tif->tif_fixuptags = DumpFixupTags;
+ tif->tif_decoderow = DumpModeDecode;
+ tif->tif_decodestrip = DumpModeDecode;
+ tif->tif_decodetile = DumpModeDecode;
+ tif->tif_encoderow = DumpModeEncode;
+ tif->tif_encodestrip = DumpModeEncode;
+ tif->tif_encodetile = DumpModeEncode;
+ tif->tif_seek = DumpModeSeek;
+ return (1);
+}
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_error.c b/test/monniaux/tiff-4.0.10/tif_error.c
new file mode 100644
index 00000000..651168f7
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_error.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ */
+#include "tiffiop.h"
+
+TIFFErrorHandlerExt _TIFFerrorHandlerExt = NULL;
+
+TIFFErrorHandler
+TIFFSetErrorHandler(TIFFErrorHandler handler)
+{
+ TIFFErrorHandler prev = _TIFFerrorHandler;
+ _TIFFerrorHandler = handler;
+ return (prev);
+}
+
+TIFFErrorHandlerExt
+TIFFSetErrorHandlerExt(TIFFErrorHandlerExt handler)
+{
+ TIFFErrorHandlerExt prev = _TIFFerrorHandlerExt;
+ _TIFFerrorHandlerExt = handler;
+ return (prev);
+}
+
+void
+TIFFError(const char* module, const char* fmt, ...)
+{
+ va_list ap;
+ if (_TIFFerrorHandler) {
+ va_start(ap, fmt);
+ (*_TIFFerrorHandler)(module, fmt, ap);
+ va_end(ap);
+ }
+ if (_TIFFerrorHandlerExt) {
+ va_start(ap, fmt);
+ (*_TIFFerrorHandlerExt)(0, module, fmt, ap);
+ va_end(ap);
+ }
+}
+
+void
+TIFFErrorExt(thandle_t fd, const char* module, const char* fmt, ...)
+{
+ va_list ap;
+ if (_TIFFerrorHandler) {
+ va_start(ap, fmt);
+ (*_TIFFerrorHandler)(module, fmt, ap);
+ va_end(ap);
+ }
+ if (_TIFFerrorHandlerExt) {
+ va_start(ap, fmt);
+ (*_TIFFerrorHandlerExt)(fd, module, fmt, ap);
+ va_end(ap);
+ }
+}
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_extension.c b/test/monniaux/tiff-4.0.10/tif_extension.c
new file mode 100644
index 00000000..87d3cfcb
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_extension.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Various routines support external extension of the tag set, and other
+ * application extension capabilities.
+ */
+
+#include "tiffiop.h"
+
+int TIFFGetTagListCount( TIFF *tif )
+
+{
+ TIFFDirectory* td = &tif->tif_dir;
+
+ return td->td_customValueCount;
+}
+
+uint32 TIFFGetTagListEntry( TIFF *tif, int tag_index )
+
+{
+ TIFFDirectory* td = &tif->tif_dir;
+
+ if( tag_index < 0 || tag_index >= td->td_customValueCount )
+ return (uint32)(-1);
+ else
+ return td->td_customValues[tag_index].info->field_tag;
+}
+
+/*
+** This provides read/write access to the TIFFTagMethods within the TIFF
+** structure to application code without giving access to the private
+** TIFF structure.
+*/
+TIFFTagMethods *TIFFAccessTagMethods( TIFF *tif )
+
+{
+ return &(tif->tif_tagmethods);
+}
+
+void *TIFFGetClientInfo( TIFF *tif, const char *name )
+
+{
+ TIFFClientInfoLink *psLink = tif->tif_clientinfo;
+
+ while( psLink != NULL && strcmp(psLink->name,name) != 0 )
+ psLink = psLink->next;
+
+ if( psLink != NULL )
+ return psLink->data;
+ else
+ return NULL;
+}
+
+void TIFFSetClientInfo( TIFF *tif, void *data, const char *name )
+
+{
+ TIFFClientInfoLink *psLink = tif->tif_clientinfo;
+
+ /*
+ ** Do we have an existing link with this name? If so, just
+ ** set it.
+ */
+ while( psLink != NULL && strcmp(psLink->name,name) != 0 )
+ psLink = psLink->next;
+
+ if( psLink != NULL )
+ {
+ psLink->data = data;
+ return;
+ }
+
+ /*
+ ** Create a new link.
+ */
+
+ psLink = (TIFFClientInfoLink *) _TIFFmalloc(sizeof(TIFFClientInfoLink));
+ assert (psLink != NULL);
+ psLink->next = tif->tif_clientinfo;
+ psLink->name = (char *) _TIFFmalloc((tmsize_t)(strlen(name)+1));
+ assert (psLink->name != NULL);
+ strcpy(psLink->name, name);
+ psLink->data = data;
+
+ tif->tif_clientinfo = psLink;
+}
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_fax3.c b/test/monniaux/tiff-4.0.10/tif_fax3.c
new file mode 100644
index 00000000..d11c9684
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_fax3.c
@@ -0,0 +1,1646 @@
+/*
+ * Copyright (c) 1990-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef CCITT_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
+ *
+ * This file contains support for decoding and encoding TIFF
+ * compression algorithms 2, 3, 4, and 32771.
+ *
+ * Decoder support is derived, with permission, from the code
+ * in Frank Cringle's viewfax program;
+ * Copyright (C) 1990, 1995 Frank D. Cringle.
+ */
+#include "tif_fax3.h"
+#define G3CODES
+#include "t4.h"
+#include <stdio.h>
+
+/*
+ * Compression+decompression state blocks are
+ * derived from this ``base state'' block.
+ */
+typedef struct {
+ int rw_mode; /* O_RDONLY for decode, else encode */
+ int mode; /* operating mode */
+ tmsize_t rowbytes; /* bytes in a decoded scanline */
+ uint32 rowpixels; /* pixels in a scanline */
+
+ uint16 cleanfaxdata; /* CleanFaxData tag */
+ uint32 badfaxrun; /* BadFaxRun tag */
+ uint32 badfaxlines; /* BadFaxLines tag */
+ uint32 groupoptions; /* Group 3/4 options tag */
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+ TIFFPrintMethod printdir; /* super-class method */
+} Fax3BaseState;
+#define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
+
+typedef enum { G3_1D, G3_2D } Ttag;
+typedef struct {
+ Fax3BaseState b;
+
+ /* Decoder state info */
+ const unsigned char* bitmap; /* bit reversal table */
+ uint32 data; /* current i/o byte/word */
+ int bit; /* current i/o bit in byte */
+ int EOLcnt; /* count of EOL codes recognized */
+ TIFFFaxFillFunc fill; /* fill routine */
+ uint32* runs; /* b&w runs for current/previous row */
+ uint32* refruns; /* runs for reference line */
+ uint32* curruns; /* runs for current line */
+
+ /* Encoder state info */
+ Ttag tag; /* encoding state */
+ unsigned char* refline; /* reference line for 2d decoding */
+ int k; /* #rows left that can be 2d encoded */
+ int maxk; /* max #rows that can be 2d encoded */
+
+ int line;
+} Fax3CodecState;
+#define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
+#define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
+
+#define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
+#define isAligned(p,t) ((((size_t)(p)) & (sizeof (t)-1)) == 0)
+
+/*
+ * Group 3 and Group 4 Decoding.
+ */
+
+/*
+ * These macros glue the TIFF library state to
+ * the state expected by Frank's decoder.
+ */
+#define DECLARE_STATE(tif, sp, mod) \
+ static const char module[] = mod; \
+ Fax3CodecState* sp = DecoderState(tif); \
+ int a0; /* reference element */ \
+ int lastx = sp->b.rowpixels; /* last element in row */ \
+ uint32 BitAcc; /* bit accumulator */ \
+ int BitsAvail; /* # valid bits in BitAcc */ \
+ int RunLength; /* length of current run */ \
+ unsigned char* cp; /* next byte of input data */ \
+ unsigned char* ep; /* end of input data */ \
+ uint32* pa; /* place to stuff next run */ \
+ uint32* thisrun; /* current row's run array */ \
+ int EOLcnt; /* # EOL codes recognized */ \
+ const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \
+ const TIFFFaxTabEnt* TabEnt
+#define DECLARE_STATE_2D(tif, sp, mod) \
+ DECLARE_STATE(tif, sp, mod); \
+ int b1; /* next change on prev line */ \
+ uint32* pb /* next run in reference line */\
+/*
+ * Load any state that may be changed during decoding.
+ */
+#define CACHE_STATE(tif, sp) do { \
+ BitAcc = sp->data; \
+ BitsAvail = sp->bit; \
+ EOLcnt = sp->EOLcnt; \
+ cp = (unsigned char*) tif->tif_rawcp; \
+ ep = cp + tif->tif_rawcc; \
+} while (0)
+/*
+ * Save state possibly changed during decoding.
+ */
+#define UNCACHE_STATE(tif, sp) do { \
+ sp->bit = BitsAvail; \
+ sp->data = BitAcc; \
+ sp->EOLcnt = EOLcnt; \
+ tif->tif_rawcc -= (tmsize_t)((uint8*) cp - tif->tif_rawcp); \
+ tif->tif_rawcp = (uint8*) cp; \
+} while (0)
+
+/*
+ * Setup state for decoding a strip.
+ */
+static int
+Fax3PreDecode(TIFF* tif, uint16 s)
+{
+ Fax3CodecState* sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ sp->bit = 0; /* force initial read */
+ sp->data = 0;
+ sp->EOLcnt = 0; /* force initial scan for EOL */
+ /*
+ * Decoder assumes lsb-to-msb bit order. Note that we select
+ * this here rather than in Fax3SetupState so that viewers can
+ * hold the image open, fiddle with the FillOrder tag value,
+ * and then re-decode the image. Otherwise they'd need to close
+ * and open the image to get the state reset.
+ */
+ sp->bitmap =
+ TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);
+ if (sp->refruns) { /* init reference line to white */
+ sp->refruns[0] = (uint32) sp->b.rowpixels;
+ sp->refruns[1] = 0;
+ }
+ sp->line = 0;
+ return (1);
+}
+
+/*
+ * Routine for handling various errors/conditions.
+ * Note how they are "glued into the decoder" by
+ * overriding the definitions used by the decoder.
+ */
+
+static void
+Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0)
+{
+ TIFFErrorExt(tif->tif_clientdata, module, "Bad code word at line %u of %s %u (x %u)",
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
+}
+#define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
+
+static void
+Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0)
+{
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Uncompressed data (not supported) at line %u of %s %u (x %u)",
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
+}
+#define extension(a0) Fax3Extension(module, tif, sp->line, a0)
+
+static void
+Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx)
+{
+ TIFFWarningExt(tif->tif_clientdata, module, "%s at line %u of %s %u (got %u, expected %u)",
+ a0 < lastx ? "Premature EOL" : "Line length mismatch",
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0, lastx);
+}
+#define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
+
+static void
+Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0)
+{
+ TIFFWarningExt(tif->tif_clientdata, module, "Premature EOF at line %u of %s %u (x %u)",
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
+}
+#define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
+
+#define Nop
+
+/*
+ * Decode the requested amount of G3 1D-encoded data.
+ */
+static int
+Fax3Decode1D(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ DECLARE_STATE(tif, sp, "Fax3Decode1D");
+ (void) s;
+ if (occ % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (-1);
+ }
+ CACHE_STATE(tif, sp);
+ thisrun = sp->curruns;
+ while (occ > 0) {
+ a0 = 0;
+ RunLength = 0;
+ pa = thisrun;
+#ifdef FAX3_DEBUG
+ printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
+ printf("-------------------- %d\n", tif->tif_row);
+ fflush(stdout);
+#endif
+ SYNC_EOL(EOF1D);
+ EXPAND1D(EOF1Da);
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ buf += sp->b.rowbytes;
+ occ -= sp->b.rowbytes;
+ sp->line++;
+ continue;
+ EOF1D: /* premature EOF */
+ CLEANUP_RUNS();
+ EOF1Da: /* premature EOF */
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ UNCACHE_STATE(tif, sp);
+ return (-1);
+ }
+ UNCACHE_STATE(tif, sp);
+ return (1);
+}
+
+#define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
+/*
+ * Decode the requested amount of G3 2D-encoded data.
+ */
+static int
+Fax3Decode2D(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
+ int is1D; /* current line is 1d/2d-encoded */
+ (void) s;
+ if (occ % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (-1);
+ }
+ CACHE_STATE(tif, sp);
+ while (occ > 0) {
+ a0 = 0;
+ RunLength = 0;
+ pa = thisrun = sp->curruns;
+#ifdef FAX3_DEBUG
+ printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
+ BitAcc, BitsAvail, EOLcnt);
+#endif
+ SYNC_EOL(EOF2D);
+ NeedBits8(1, EOF2D);
+ is1D = GetBits(1); /* 1D/2D-encoding tag bit */
+ ClrBits(1);
+#ifdef FAX3_DEBUG
+ printf(" %s\n-------------------- %d\n",
+ is1D ? "1D" : "2D", tif->tif_row);
+ fflush(stdout);
+#endif
+ pb = sp->refruns;
+ b1 = *pb++;
+ if (is1D)
+ EXPAND1D(EOF2Da);
+ else
+ EXPAND2D(EOF2Da);
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ SETVALUE(0); /* imaginary change for reference */
+ SWAP(uint32*, sp->curruns, sp->refruns);
+ buf += sp->b.rowbytes;
+ occ -= sp->b.rowbytes;
+ sp->line++;
+ continue;
+ EOF2D: /* premature EOF */
+ CLEANUP_RUNS();
+ EOF2Da: /* premature EOF */
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ UNCACHE_STATE(tif, sp);
+ return (-1);
+ }
+ UNCACHE_STATE(tif, sp);
+ return (1);
+}
+#undef SWAP
+
+/*
+ * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
+ * For machines with 64-bit longs this is <16 bytes; otherwise
+ * this is <8 bytes. We optimize the code here to reflect the
+ * machine characteristics.
+ */
+#if SIZEOF_UNSIGNED_LONG == 8
+# define FILL(n, cp) \
+ switch (n) { \
+ case 15:(cp)[14] = 0xff; /*-fallthrough*/ \
+ case 14:(cp)[13] = 0xff; /*-fallthrough*/ \
+ case 13:(cp)[12] = 0xff; /*-fallthrough*/ \
+ case 12:(cp)[11] = 0xff; /*-fallthrough*/ \
+ case 11:(cp)[10] = 0xff; /*-fallthrough*/ \
+ case 10: (cp)[9] = 0xff; /*-fallthrough*/ \
+ case 9: (cp)[8] = 0xff; /*-fallthrough*/ \
+ case 8: (cp)[7] = 0xff; /*-fallthrough*/ \
+ case 7: (cp)[6] = 0xff; /*-fallthrough*/ \
+ case 6: (cp)[5] = 0xff; /*-fallthrough*/ \
+ case 5: (cp)[4] = 0xff; /*-fallthrough*/ \
+ case 4: (cp)[3] = 0xff; /*-fallthrough*/ \
+ case 3: (cp)[2] = 0xff; /*-fallthrough*/ \
+ case 2: (cp)[1] = 0xff; /*-fallthrough*/ \
+ case 1: (cp)[0] = 0xff; (cp) += (n); /*-fallthrough*/ \
+ case 0: ; \
+ }
+# define ZERO(n, cp) \
+ switch (n) { \
+ case 15:(cp)[14] = 0; /*-fallthrough*/ \
+ case 14:(cp)[13] = 0; /*-fallthrough*/ \
+ case 13:(cp)[12] = 0; /*-fallthrough*/ \
+ case 12:(cp)[11] = 0; /*-fallthrough*/ \
+ case 11:(cp)[10] = 0; /*-fallthrough*/ \
+ case 10: (cp)[9] = 0; /*-fallthrough*/ \
+ case 9: (cp)[8] = 0; /*-fallthrough*/ \
+ case 8: (cp)[7] = 0; /*-fallthrough*/ \
+ case 7: (cp)[6] = 0; /*-fallthrough*/ \
+ case 6: (cp)[5] = 0; /*-fallthrough*/ \
+ case 5: (cp)[4] = 0; /*-fallthrough*/ \
+ case 4: (cp)[3] = 0; /*-fallthrough*/ \
+ case 3: (cp)[2] = 0; /*-fallthrough*/ \
+ case 2: (cp)[1] = 0; /*-fallthrough*/ \
+ case 1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
+ case 0: ; \
+ }
+#else
+# define FILL(n, cp) \
+ switch (n) { \
+ case 7: (cp)[6] = 0xff; /*-fallthrough*/ \
+ case 6: (cp)[5] = 0xff; /*-fallthrough*/ \
+ case 5: (cp)[4] = 0xff; /*-fallthrough*/ \
+ case 4: (cp)[3] = 0xff; /*-fallthrough*/ \
+ case 3: (cp)[2] = 0xff; /*-fallthrough*/ \
+ case 2: (cp)[1] = 0xff; /*-fallthrough*/ \
+ case 1: (cp)[0] = 0xff; (cp) += (n); /*-fallthrough*/ \
+ case 0: ; \
+ }
+# define ZERO(n, cp) \
+ switch (n) { \
+ case 7: (cp)[6] = 0; /*-fallthrough*/ \
+ case 6: (cp)[5] = 0; /*-fallthrough*/ \
+ case 5: (cp)[4] = 0; /*-fallthrough*/ \
+ case 4: (cp)[3] = 0; /*-fallthrough*/ \
+ case 3: (cp)[2] = 0; /*-fallthrough*/ \
+ case 2: (cp)[1] = 0; /*-fallthrough*/ \
+ case 1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
+ case 0: ; \
+ }
+#endif
+
+/*
+ * Bit-fill a row according to the white/black
+ * runs generated during G3/G4 decoding.
+ */
+void
+_TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
+{
+ static const unsigned char _fillmasks[] =
+ { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
+ unsigned char* cp;
+ uint32 x, bx, run;
+ int32 n, nw;
+ long* lp;
+
+ if ((erun-runs)&1)
+ *erun++ = 0;
+ x = 0;
+ for (; runs < erun; runs += 2) {
+ run = runs[0];
+ if (x+run > lastx || run > lastx )
+ run = runs[0] = (uint32) (lastx - x);
+ if (run) {
+ cp = buf + (x>>3);
+ bx = x&7;
+ if (run > 8-bx) {
+ if (bx) { /* align to byte boundary */
+ *cp++ &= 0xff << (8-bx);
+ run -= 8-bx;
+ }
+ if( (n = run >> 3) != 0 ) { /* multiple bytes to fill */
+ if ((n/sizeof (long)) > 1) {
+ /*
+ * Align to longword boundary and fill.
+ */
+ for (; n && !isAligned(cp, long); n--)
+ *cp++ = 0x00;
+ lp = (long*) cp;
+ nw = (int32)(n / sizeof (long));
+ n -= nw * sizeof (long);
+ do {
+ *lp++ = 0L;
+ } while (--nw);
+ cp = (unsigned char*) lp;
+ }
+ ZERO(n, cp);
+ run &= 7;
+ }
+ if (run)
+ cp[0] &= 0xff >> run;
+ } else
+ cp[0] &= ~(_fillmasks[run]>>bx);
+ x += runs[0];
+ }
+ run = runs[1];
+ if (x+run > lastx || run > lastx )
+ run = runs[1] = lastx - x;
+ if (run) {
+ cp = buf + (x>>3);
+ bx = x&7;
+ if (run > 8-bx) {
+ if (bx) { /* align to byte boundary */
+ *cp++ |= 0xff >> bx;
+ run -= 8-bx;
+ }
+ if( (n = run>>3) != 0 ) { /* multiple bytes to fill */
+ if ((n/sizeof (long)) > 1) {
+ /*
+ * Align to longword boundary and fill.
+ */
+ for (; n && !isAligned(cp, long); n--)
+ *cp++ = 0xff;
+ lp = (long*) cp;
+ nw = (int32)(n / sizeof (long));
+ n -= nw * sizeof (long);
+ do {
+ *lp++ = -1L;
+ } while (--nw);
+ cp = (unsigned char*) lp;
+ }
+ FILL(n, cp);
+ run &= 7;
+ }
+ /* Explicit 0xff masking to make icc -check=conversions happy */
+ if (run)
+ cp[0] = (unsigned char)((cp[0] | (0xff00 >> run))&0xff);
+ } else
+ cp[0] |= _fillmasks[run]>>bx;
+ x += runs[1];
+ }
+ }
+ assert(x == lastx);
+}
+#undef ZERO
+#undef FILL
+
+static int
+Fax3FixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+/*
+ * Setup G3/G4-related compression/decompression state
+ * before data is processed. This routine is called once
+ * per image -- it sets up different state based on whether
+ * or not decoding or encoding is being done and whether
+ * 1D- or 2D-encoded data is involved.
+ */
+static int
+Fax3SetupState(TIFF* tif)
+{
+ static const char module[] = "Fax3SetupState";
+ TIFFDirectory* td = &tif->tif_dir;
+ Fax3BaseState* sp = Fax3State(tif);
+ int needsRefLine;
+ Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
+ tmsize_t rowbytes;
+ uint32 rowpixels, nruns;
+
+ if (td->td_bitspersample != 1) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Bits/sample must be 1 for Group 3/4 encoding/decoding");
+ return (0);
+ }
+ /*
+ * Calculate the scanline/tile widths.
+ */
+ if (isTiled(tif)) {
+ rowbytes = TIFFTileRowSize(tif);
+ rowpixels = td->td_tilewidth;
+ } else {
+ rowbytes = TIFFScanlineSize(tif);
+ rowpixels = td->td_imagewidth;
+ }
+ sp->rowbytes = rowbytes;
+ sp->rowpixels = rowpixels;
+ /*
+ * Allocate any additional space required for decoding/encoding.
+ */
+ needsRefLine = (
+ (sp->groupoptions & GROUP3OPT_2DENCODING) ||
+ td->td_compression == COMPRESSION_CCITTFAX4
+ );
+
+ /*
+ Assure that allocation computations do not overflow.
+
+ TIFFroundup and TIFFSafeMultiply return zero on integer overflow
+ */
+ dsp->runs=(uint32*) NULL;
+ nruns = TIFFroundup_32(rowpixels,32);
+ if (needsRefLine) {
+ nruns = TIFFSafeMultiply(uint32,nruns,2);
+ }
+ if ((nruns == 0) || (TIFFSafeMultiply(uint32,nruns,2) == 0)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Row pixels integer overflow (rowpixels %u)",
+ rowpixels);
+ return (0);
+ }
+ dsp->runs = (uint32*) _TIFFCheckMalloc(tif,
+ TIFFSafeMultiply(uint32,nruns,2),
+ sizeof (uint32),
+ "for Group 3/4 run arrays");
+ if (dsp->runs == NULL)
+ return (0);
+ memset( dsp->runs, 0, TIFFSafeMultiply(uint32,nruns,2)*sizeof(uint32));
+ dsp->curruns = dsp->runs;
+ if (needsRefLine)
+ dsp->refruns = dsp->runs + nruns;
+ else
+ dsp->refruns = NULL;
+ if (td->td_compression == COMPRESSION_CCITTFAX3
+ && is2DEncoding(dsp)) { /* NB: default is 1D routine */
+ tif->tif_decoderow = Fax3Decode2D;
+ tif->tif_decodestrip = Fax3Decode2D;
+ tif->tif_decodetile = Fax3Decode2D;
+ }
+
+ if (needsRefLine) { /* 2d encoding */
+ Fax3CodecState* esp = EncoderState(tif);
+ /*
+ * 2d encoding requires a scanline
+ * buffer for the ``reference line''; the
+ * scanline against which delta encoding
+ * is referenced. The reference line must
+ * be initialized to be ``white'' (done elsewhere).
+ */
+ esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
+ if (esp->refline == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for Group 3/4 reference line");
+ return (0);
+ }
+ } else /* 1d encoding */
+ EncoderState(tif)->refline = NULL;
+
+ return (1);
+}
+
+/*
+ * CCITT Group 3 FAX Encoding.
+ */
+
+#define Fax3FlushBits(tif, sp) { \
+ if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
+ (void) TIFFFlushData1(tif); \
+ *(tif)->tif_rawcp++ = (uint8) (sp)->data; \
+ (tif)->tif_rawcc++; \
+ (sp)->data = 0, (sp)->bit = 8; \
+}
+#define _FlushBits(tif) { \
+ if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
+ (void) TIFFFlushData1(tif); \
+ *(tif)->tif_rawcp++ = (uint8) data; \
+ (tif)->tif_rawcc++; \
+ data = 0, bit = 8; \
+}
+static const int _msbmask[9] =
+ { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
+#define _PutBits(tif, bits, length) { \
+ while (length > bit) { \
+ data |= bits >> (length - bit); \
+ length -= bit; \
+ _FlushBits(tif); \
+ } \
+ assert( length < 9 ); \
+ data |= (bits & _msbmask[length]) << (bit - length); \
+ bit -= length; \
+ if (bit == 0) \
+ _FlushBits(tif); \
+}
+
+/*
+ * Write a variable-length bit-value to
+ * the output stream. Values are
+ * assumed to be at most 16 bits.
+ */
+static void
+Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+ unsigned int bit = sp->bit;
+ int data = sp->data;
+
+ _PutBits(tif, bits, length);
+
+ sp->data = data;
+ sp->bit = bit;
+}
+
+/*
+ * Write a code to the output stream.
+ */
+#define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
+
+#ifdef FAX3_DEBUG
+#define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
+#define DEBUG_PRINT(what,len) { \
+ int t; \
+ printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
+ for (t = length-1; t >= 0; t--) \
+ putchar(code & (1<<t) ? '1' : '0'); \
+ putchar('\n'); \
+}
+#endif
+
+/*
+ * Write the sequence of codes that describes
+ * the specified span of zero's or one's. The
+ * appropriate table that holds the make-up and
+ * terminating codes is supplied.
+ */
+static void
+putspan(TIFF* tif, int32 span, const tableentry* tab)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+ unsigned int bit = sp->bit;
+ int data = sp->data;
+ unsigned int code, length;
+
+ while (span >= 2624) {
+ const tableentry* te = &tab[63 + (2560>>6)];
+ code = te->code;
+ length = te->length;
+#ifdef FAX3_DEBUG
+ DEBUG_PRINT("MakeUp", te->runlen);
+#endif
+ _PutBits(tif, code, length);
+ span -= te->runlen;
+ }
+ if (span >= 64) {
+ const tableentry* te = &tab[63 + (span>>6)];
+ assert(te->runlen == 64*(span>>6));
+ code = te->code;
+ length = te->length;
+#ifdef FAX3_DEBUG
+ DEBUG_PRINT("MakeUp", te->runlen);
+#endif
+ _PutBits(tif, code, length);
+ span -= te->runlen;
+ }
+ code = tab[span].code;
+ length = tab[span].length;
+#ifdef FAX3_DEBUG
+ DEBUG_PRINT(" Term", tab[span].runlen);
+#endif
+ _PutBits(tif, code, length);
+
+ sp->data = data;
+ sp->bit = bit;
+}
+
+/*
+ * Write an EOL code to the output stream. The zero-fill
+ * logic for byte-aligning encoded scanlines is handled
+ * here. We also handle writing the tag bit for the next
+ * scanline when doing 2d encoding.
+ */
+static void
+Fax3PutEOL(TIFF* tif)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+ unsigned int bit = sp->bit;
+ int data = sp->data;
+ unsigned int code, length, tparm;
+
+ if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
+ /*
+ * Force bit alignment so EOL will terminate on
+ * a byte boundary. That is, force the bit alignment
+ * to 16-12 = 4 before putting out the EOL code.
+ */
+ int align = 8 - 4;
+ if (align != sp->bit) {
+ if (align > sp->bit)
+ align = sp->bit + (8 - align);
+ else
+ align = sp->bit - align;
+ tparm=align;
+ _PutBits(tif, 0, tparm);
+ }
+ }
+ code = EOL;
+ length = 12;
+ if (is2DEncoding(sp)) {
+ code = (code<<1) | (sp->tag == G3_1D);
+ length++;
+ }
+ _PutBits(tif, code, length);
+
+ sp->data = data;
+ sp->bit = bit;
+}
+
+/*
+ * Reset encoding state at the start of a strip.
+ */
+static int
+Fax3PreEncode(TIFF* tif, uint16 s)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ sp->bit = 8;
+ sp->data = 0;
+ sp->tag = G3_1D;
+ /*
+ * This is necessary for Group 4; otherwise it isn't
+ * needed because the first scanline of each strip ends
+ * up being copied into the refline.
+ */
+ if (sp->refline)
+ _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
+ if (is2DEncoding(sp)) {
+ float res = tif->tif_dir.td_yresolution;
+ /*
+ * The CCITT spec says that when doing 2d encoding, you
+ * should only do it on K consecutive scanlines, where K
+ * depends on the resolution of the image being encoded
+ * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
+ * code initializes td_yresolution to 0, this code will
+ * select a K of 2 unless the YResolution tag is set
+ * appropriately. (Note also that we fudge a little here
+ * and use 150 lpi to avoid problems with units conversion.)
+ */
+ if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
+ res *= 2.54f; /* convert to inches */
+ sp->maxk = (res > 150 ? 4 : 2);
+ sp->k = sp->maxk-1;
+ } else
+ sp->k = sp->maxk = 0;
+ sp->line = 0;
+ return (1);
+}
+
+static const unsigned char zeroruns[256] = {
+ 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
+};
+static const unsigned char oneruns[256] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
+};
+
+/*
+ * On certain systems it pays to inline
+ * the routines that find pixel spans.
+ */
+#ifdef VAXC
+static int32 find0span(unsigned char*, int32, int32);
+static int32 find1span(unsigned char*, int32, int32);
+#pragma inline(find0span,find1span)
+#endif
+
+/*
+ * Find a span of ones or zeros using the supplied
+ * table. The ``base'' of the bit string is supplied
+ * along with the start+end bit indices.
+ */
+inline static int32
+find0span(unsigned char* bp, int32 bs, int32 be)
+{
+ int32 bits = be - bs;
+ int32 n, span;
+
+ bp += bs>>3;
+ /*
+ * Check partial byte on lhs.
+ */
+ if (bits > 0 && (n = (bs & 7)) != 0) {
+ span = zeroruns[(*bp << n) & 0xff];
+ if (span > 8-n) /* table value too generous */
+ span = 8-n;
+ if (span > bits) /* constrain span to bit range */
+ span = bits;
+ if (n+span < 8) /* doesn't extend to edge of byte */
+ return (span);
+ bits -= span;
+ bp++;
+ } else
+ span = 0;
+ if (bits >= (int32)(2 * 8 * sizeof(long))) {
+ long* lp;
+ /*
+ * Align to longword boundary and check longwords.
+ */
+ while (!isAligned(bp, long)) {
+ if (*bp != 0x00)
+ return (span + zeroruns[*bp]);
+ span += 8;
+ bits -= 8;
+ bp++;
+ }
+ lp = (long*) bp;
+ while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) {
+ span += 8*sizeof (long);
+ bits -= 8*sizeof (long);
+ lp++;
+ }
+ bp = (unsigned char*) lp;
+ }
+ /*
+ * Scan full bytes for all 0's.
+ */
+ while (bits >= 8) {
+ if (*bp != 0x00) /* end of run */
+ return (span + zeroruns[*bp]);
+ span += 8;
+ bits -= 8;
+ bp++;
+ }
+ /*
+ * Check partial byte on rhs.
+ */
+ if (bits > 0) {
+ n = zeroruns[*bp];
+ span += (n > bits ? bits : n);
+ }
+ return (span);
+}
+
+inline static int32
+find1span(unsigned char* bp, int32 bs, int32 be)
+{
+ int32 bits = be - bs;
+ int32 n, span;
+
+ bp += bs>>3;
+ /*
+ * Check partial byte on lhs.
+ */
+ if (bits > 0 && (n = (bs & 7)) != 0) {
+ span = oneruns[(*bp << n) & 0xff];
+ if (span > 8-n) /* table value too generous */
+ span = 8-n;
+ if (span > bits) /* constrain span to bit range */
+ span = bits;
+ if (n+span < 8) /* doesn't extend to edge of byte */
+ return (span);
+ bits -= span;
+ bp++;
+ } else
+ span = 0;
+ if (bits >= (int32)(2 * 8 * sizeof(long))) {
+ long* lp;
+ /*
+ * Align to longword boundary and check longwords.
+ */
+ while (!isAligned(bp, long)) {
+ if (*bp != 0xff)
+ return (span + oneruns[*bp]);
+ span += 8;
+ bits -= 8;
+ bp++;
+ }
+ lp = (long*) bp;
+ while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) {
+ span += 8*sizeof (long);
+ bits -= 8*sizeof (long);
+ lp++;
+ }
+ bp = (unsigned char*) lp;
+ }
+ /*
+ * Scan full bytes for all 1's.
+ */
+ while (bits >= 8) {
+ if (*bp != 0xff) /* end of run */
+ return (span + oneruns[*bp]);
+ span += 8;
+ bits -= 8;
+ bp++;
+ }
+ /*
+ * Check partial byte on rhs.
+ */
+ if (bits > 0) {
+ n = oneruns[*bp];
+ span += (n > bits ? bits : n);
+ }
+ return (span);
+}
+
+/*
+ * Return the offset of the next bit in the range
+ * [bs..be] that is different from the specified
+ * color. The end, be, is returned if no such bit
+ * exists.
+ */
+#define finddiff(_cp, _bs, _be, _color) \
+ (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
+/*
+ * Like finddiff, but also check the starting bit
+ * against the end in case start > end.
+ */
+#define finddiff2(_cp, _bs, _be, _color) \
+ (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
+
+/*
+ * 1d-encode a row of pixels. The encoding is
+ * a sequence of all-white or all-black spans
+ * of pixels encoded with Huffman codes.
+ */
+static int
+Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+ int32 span;
+ uint32 bs = 0;
+
+ for (;;) {
+ span = find0span(bp, bs, bits); /* white span */
+ putspan(tif, span, TIFFFaxWhiteCodes);
+ bs += span;
+ if (bs >= bits)
+ break;
+ span = find1span(bp, bs, bits); /* black span */
+ putspan(tif, span, TIFFFaxBlackCodes);
+ bs += span;
+ if (bs >= bits)
+ break;
+ }
+ if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
+ if (sp->bit != 8) /* byte-align */
+ Fax3FlushBits(tif, sp);
+ if ((sp->b.mode&FAXMODE_WORDALIGN) &&
+ !isAligned(tif->tif_rawcp, uint16))
+ Fax3FlushBits(tif, sp);
+ }
+ return (1);
+}
+
+static const tableentry horizcode =
+ { 3, 0x1, 0 }; /* 001 */
+static const tableentry passcode =
+ { 4, 0x1, 0 }; /* 0001 */
+static const tableentry vcodes[7] = {
+ { 7, 0x03, 0 }, /* 0000 011 */
+ { 6, 0x03, 0 }, /* 0000 11 */
+ { 3, 0x03, 0 }, /* 011 */
+ { 1, 0x1, 0 }, /* 1 */
+ { 3, 0x2, 0 }, /* 010 */
+ { 6, 0x02, 0 }, /* 0000 10 */
+ { 7, 0x02, 0 } /* 0000 010 */
+};
+
+/*
+ * 2d-encode a row of pixels. Consult the CCITT
+ * documentation for the algorithm.
+ */
+static int
+Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits)
+{
+#define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
+ uint32 a0 = 0;
+ uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
+ uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
+ uint32 a2, b2;
+
+ for (;;) {
+ b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
+ if (b2 >= a1) {
+ /* Naive computation triggers -fsanitize=undefined,unsigned-integer-overflow */
+ /* although it is correct unless the difference between both is < 31 bit */
+ /* int32 d = b1 - a1; */
+ int32 d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32)(b1 - a1):
+ (b1 < a1 && a1 - b1 <= 3U) ? -(int32)(a1 - b1) : 0x7FFFFFFF;
+ if (!(-3 <= d && d <= 3)) { /* horizontal mode */
+ a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
+ putcode(tif, &horizcode);
+ if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
+ putspan(tif, a1-a0, TIFFFaxWhiteCodes);
+ putspan(tif, a2-a1, TIFFFaxBlackCodes);
+ } else {
+ putspan(tif, a1-a0, TIFFFaxBlackCodes);
+ putspan(tif, a2-a1, TIFFFaxWhiteCodes);
+ }
+ a0 = a2;
+ } else { /* vertical mode */
+ putcode(tif, &vcodes[d+3]);
+ a0 = a1;
+ }
+ } else { /* pass mode */
+ putcode(tif, &passcode);
+ a0 = b2;
+ }
+ if (a0 >= bits)
+ break;
+ a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
+ b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
+ b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
+ }
+ return (1);
+#undef PIXEL
+}
+
+/*
+ * Encode a buffer of pixels.
+ */
+static int
+Fax3Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "Fax3Encode";
+ Fax3CodecState* sp = EncoderState(tif);
+ (void) s;
+ if (cc % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
+ return (0);
+ }
+ while (cc > 0) {
+ if ((sp->b.mode & FAXMODE_NOEOL) == 0)
+ Fax3PutEOL(tif);
+ if (is2DEncoding(sp)) {
+ if (sp->tag == G3_1D) {
+ if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
+ return (0);
+ sp->tag = G3_2D;
+ } else {
+ if (!Fax3Encode2DRow(tif, bp, sp->refline,
+ sp->b.rowpixels))
+ return (0);
+ sp->k--;
+ }
+ if (sp->k == 0) {
+ sp->tag = G3_1D;
+ sp->k = sp->maxk-1;
+ } else
+ _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
+ } else {
+ if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
+ return (0);
+ }
+ bp += sp->b.rowbytes;
+ cc -= sp->b.rowbytes;
+ }
+ return (1);
+}
+
+static int
+Fax3PostEncode(TIFF* tif)
+{
+ Fax3CodecState* sp = EncoderState(tif);
+
+ if (sp->bit != 8)
+ Fax3FlushBits(tif, sp);
+ return (1);
+}
+
+static void
+Fax3Close(TIFF* tif)
+{
+ if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp) {
+ Fax3CodecState* sp = EncoderState(tif);
+ unsigned int code = EOL;
+ unsigned int length = 12;
+ int i;
+
+ if (is2DEncoding(sp)) {
+ code = (code<<1) | (sp->tag == G3_1D);
+ length++;
+ }
+ for (i = 0; i < 6; i++)
+ Fax3PutBits(tif, code, length);
+ Fax3FlushBits(tif, sp);
+ }
+}
+
+static void
+Fax3Cleanup(TIFF* tif)
+{
+ Fax3CodecState* sp = DecoderState(tif);
+
+ assert(sp != 0);
+
+ tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
+ tif->tif_tagmethods.printdir = sp->b.printdir;
+
+ if (sp->runs)
+ _TIFFfree(sp->runs);
+ if (sp->refline)
+ _TIFFfree(sp->refline);
+
+ _TIFFfree(tif->tif_data);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+#define FIELD_BADFAXLINES (FIELD_CODEC+0)
+#define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
+#define FIELD_BADFAXRUN (FIELD_CODEC+2)
+
+#define FIELD_OPTIONS (FIELD_CODEC+7)
+
+static const TIFFField faxFields[] = {
+ { TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL },
+ { TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL },
+ { TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL },
+ { TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL },
+ { TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL }};
+static const TIFFField fax3Fields[] = {
+ { TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL },
+};
+static const TIFFField fax4Fields[] = {
+ { TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL },
+};
+
+static int
+Fax3VSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ Fax3BaseState* sp = Fax3State(tif);
+ const TIFFField* fip;
+
+ assert(sp != 0);
+ assert(sp->vsetparent != 0);
+
+ switch (tag) {
+ case TIFFTAG_FAXMODE:
+ sp->mode = (int) va_arg(ap, int);
+ return 1; /* NB: pseudo tag */
+ case TIFFTAG_FAXFILLFUNC:
+ DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
+ return 1; /* NB: pseudo tag */
+ case TIFFTAG_GROUP3OPTIONS:
+ /* XXX: avoid reading options if compression mismatches. */
+ if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
+ sp->groupoptions = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_GROUP4OPTIONS:
+ /* XXX: avoid reading options if compression mismatches. */
+ if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
+ sp->groupoptions = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_BADFAXLINES:
+ sp->badfaxlines = (uint32) va_arg(ap, uint32);
+ break;
+ case TIFFTAG_CLEANFAXDATA:
+ sp->cleanfaxdata = (uint16) va_arg(ap, uint16_vap);
+ break;
+ case TIFFTAG_CONSECUTIVEBADFAXLINES:
+ sp->badfaxrun = (uint32) va_arg(ap, uint32);
+ break;
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+
+ if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
+ TIFFSetFieldBit(tif, fip->field_bit);
+ else
+ return 0;
+
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+ return 1;
+}
+
+static int
+Fax3VGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ Fax3BaseState* sp = Fax3State(tif);
+
+ assert(sp != 0);
+
+ switch (tag) {
+ case TIFFTAG_FAXMODE:
+ *va_arg(ap, int*) = sp->mode;
+ break;
+ case TIFFTAG_FAXFILLFUNC:
+ *va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
+ break;
+ case TIFFTAG_GROUP3OPTIONS:
+ case TIFFTAG_GROUP4OPTIONS:
+ *va_arg(ap, uint32*) = sp->groupoptions;
+ break;
+ case TIFFTAG_BADFAXLINES:
+ *va_arg(ap, uint32*) = sp->badfaxlines;
+ break;
+ case TIFFTAG_CLEANFAXDATA:
+ *va_arg(ap, uint16*) = sp->cleanfaxdata;
+ break;
+ case TIFFTAG_CONSECUTIVEBADFAXLINES:
+ *va_arg(ap, uint32*) = sp->badfaxrun;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return (1);
+}
+
+static void
+Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
+{
+ Fax3BaseState* sp = Fax3State(tif);
+
+ assert(sp != 0);
+
+ (void) flags;
+ if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
+ const char* sep = " ";
+ if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
+ fprintf(fd, " Group 4 Options:");
+ if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
+ fprintf(fd, "%suncompressed data", sep);
+ } else {
+
+ fprintf(fd, " Group 3 Options:");
+ if (sp->groupoptions & GROUP3OPT_2DENCODING) {
+ fprintf(fd, "%s2-d encoding", sep);
+ sep = "+";
+ }
+ if (sp->groupoptions & GROUP3OPT_FILLBITS) {
+ fprintf(fd, "%sEOL padding", sep);
+ sep = "+";
+ }
+ if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
+ fprintf(fd, "%suncompressed data", sep);
+ }
+ fprintf(fd, " (%lu = 0x%lx)\n",
+ (unsigned long) sp->groupoptions,
+ (unsigned long) sp->groupoptions);
+ }
+ if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
+ fprintf(fd, " Fax Data:");
+ switch (sp->cleanfaxdata) {
+ case CLEANFAXDATA_CLEAN:
+ fprintf(fd, " clean");
+ break;
+ case CLEANFAXDATA_REGENERATED:
+ fprintf(fd, " receiver regenerated");
+ break;
+ case CLEANFAXDATA_UNCLEAN:
+ fprintf(fd, " uncorrected errors");
+ break;
+ }
+ fprintf(fd, " (%u = 0x%x)\n",
+ sp->cleanfaxdata, sp->cleanfaxdata);
+ }
+ if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
+ fprintf(fd, " Bad Fax Lines: %lu\n",
+ (unsigned long) sp->badfaxlines);
+ if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
+ fprintf(fd, " Consecutive Bad Fax Lines: %lu\n",
+ (unsigned long) sp->badfaxrun);
+ if (sp->printdir)
+ (*sp->printdir)(tif, fd, flags);
+}
+
+static int
+InitCCITTFax3(TIFF* tif)
+{
+ static const char module[] = "InitCCITTFax3";
+ Fax3BaseState* sp;
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) {
+ TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
+ "Merging common CCITT Fax codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*)
+ _TIFFmalloc(sizeof (Fax3CodecState));
+
+ if (tif->tif_data == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for state block");
+ return (0);
+ }
+ _TIFFmemset(tif->tif_data, 0, sizeof (Fax3CodecState));
+
+ sp = Fax3State(tif);
+ sp->rw_mode = tif->tif_mode;
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
+ sp->printdir = tif->tif_tagmethods.printdir;
+ tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */
+ sp->groupoptions = 0;
+
+ if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
+ tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
+ DecoderState(tif)->runs = NULL;
+ TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
+ EncoderState(tif)->refline = NULL;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = Fax3FixupTags;
+ tif->tif_setupdecode = Fax3SetupState;
+ tif->tif_predecode = Fax3PreDecode;
+ tif->tif_decoderow = Fax3Decode1D;
+ tif->tif_decodestrip = Fax3Decode1D;
+ tif->tif_decodetile = Fax3Decode1D;
+ tif->tif_setupencode = Fax3SetupState;
+ tif->tif_preencode = Fax3PreEncode;
+ tif->tif_postencode = Fax3PostEncode;
+ tif->tif_encoderow = Fax3Encode;
+ tif->tif_encodestrip = Fax3Encode;
+ tif->tif_encodetile = Fax3Encode;
+ tif->tif_close = Fax3Close;
+ tif->tif_cleanup = Fax3Cleanup;
+
+ return (1);
+}
+
+int
+TIFFInitCCITTFax3(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ if (InitCCITTFax3(tif)) {
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, fax3Fields,
+ TIFFArrayCount(fax3Fields))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
+ "Merging CCITT Fax 3 codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * The default format is Class/F-style w/o RTC.
+ */
+ return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
+ } else
+ return 01;
+}
+
+/*
+ * CCITT Group 4 (T.6) Facsimile-compatible
+ * Compression Scheme Support.
+ */
+
+#define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
+/*
+ * Decode the requested amount of G4-encoded data.
+ */
+static int
+Fax4Decode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ DECLARE_STATE_2D(tif, sp, "Fax4Decode");
+ (void) s;
+ if (occ % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (-1);
+ }
+ CACHE_STATE(tif, sp);
+ while (occ > 0) {
+ a0 = 0;
+ RunLength = 0;
+ pa = thisrun = sp->curruns;
+ pb = sp->refruns;
+ b1 = *pb++;
+#ifdef FAX3_DEBUG
+ printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
+ printf("-------------------- %d\n", tif->tif_row);
+ fflush(stdout);
+#endif
+ EXPAND2D(EOFG4);
+ if (EOLcnt)
+ goto EOFG4;
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ SETVALUE(0); /* imaginary change for reference */
+ SWAP(uint32*, sp->curruns, sp->refruns);
+ buf += sp->b.rowbytes;
+ occ -= sp->b.rowbytes;
+ sp->line++;
+ continue;
+ EOFG4:
+ NeedBits16( 13, BADG4 );
+ BADG4:
+#ifdef FAX3_DEBUG
+ if( GetBits(13) != 0x1001 )
+ fputs( "Bad EOFB\n", stderr );
+#endif
+ ClrBits( 13 );
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ UNCACHE_STATE(tif, sp);
+ return ( sp->line ? 1 : -1); /* don't error on badly-terminated strips */
+ }
+ UNCACHE_STATE(tif, sp);
+ return (1);
+}
+#undef SWAP
+
+/*
+ * Encode the requested amount of data.
+ */
+static int
+Fax4Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "Fax4Encode";
+ Fax3CodecState *sp = EncoderState(tif);
+ (void) s;
+ if (cc % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
+ return (0);
+ }
+ while (cc > 0) {
+ if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
+ return (0);
+ _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
+ bp += sp->b.rowbytes;
+ cc -= sp->b.rowbytes;
+ }
+ return (1);
+}
+
+static int
+Fax4PostEncode(TIFF* tif)
+{
+ Fax3CodecState *sp = EncoderState(tif);
+
+ /* terminate strip w/ EOFB */
+ Fax3PutBits(tif, EOL, 12);
+ Fax3PutBits(tif, EOL, 12);
+ if (sp->bit != 8)
+ Fax3FlushBits(tif, sp);
+ return (1);
+}
+
+int
+TIFFInitCCITTFax4(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ if (InitCCITTFax3(tif)) { /* reuse G3 support */
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, fax4Fields,
+ TIFFArrayCount(fax4Fields))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
+ "Merging CCITT Fax 4 codec-specific tags failed");
+ return 0;
+ }
+
+ tif->tif_decoderow = Fax4Decode;
+ tif->tif_decodestrip = Fax4Decode;
+ tif->tif_decodetile = Fax4Decode;
+ tif->tif_encoderow = Fax4Encode;
+ tif->tif_encodestrip = Fax4Encode;
+ tif->tif_encodetile = Fax4Encode;
+ tif->tif_postencode = Fax4PostEncode;
+ /*
+ * Suppress RTC at the end of each strip.
+ */
+ return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
+ } else
+ return (0);
+}
+
+/*
+ * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
+ * (Compression algorithms 2 and 32771)
+ */
+
+/*
+ * Decode the requested amount of RLE-encoded data.
+ */
+static int
+Fax3DecodeRLE(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
+ int mode = sp->b.mode;
+ (void) s;
+ if (occ % sp->b.rowbytes)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (-1);
+ }
+ CACHE_STATE(tif, sp);
+ thisrun = sp->curruns;
+ while (occ > 0) {
+ a0 = 0;
+ RunLength = 0;
+ pa = thisrun;
+#ifdef FAX3_DEBUG
+ printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
+ printf("-------------------- %d\n", tif->tif_row);
+ fflush(stdout);
+#endif
+ EXPAND1D(EOFRLE);
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ /*
+ * Cleanup at the end of the row.
+ */
+ if (mode & FAXMODE_BYTEALIGN) {
+ int n = BitsAvail - (BitsAvail &~ 7);
+ ClrBits(n);
+ } else if (mode & FAXMODE_WORDALIGN) {
+ int n = BitsAvail - (BitsAvail &~ 15);
+ ClrBits(n);
+ if (BitsAvail == 0 && !isAligned(cp, uint16))
+ cp++;
+ }
+ buf += sp->b.rowbytes;
+ occ -= sp->b.rowbytes;
+ sp->line++;
+ continue;
+ EOFRLE: /* premature EOF */
+ (*sp->fill)(buf, thisrun, pa, lastx);
+ UNCACHE_STATE(tif, sp);
+ return (-1);
+ }
+ UNCACHE_STATE(tif, sp);
+ return (1);
+}
+
+int
+TIFFInitCCITTRLE(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ if (InitCCITTFax3(tif)) { /* reuse G3 support */
+ tif->tif_decoderow = Fax3DecodeRLE;
+ tif->tif_decodestrip = Fax3DecodeRLE;
+ tif->tif_decodetile = Fax3DecodeRLE;
+ /*
+ * Suppress RTC+EOLs when encoding and byte-align data.
+ */
+ return TIFFSetField(tif, TIFFTAG_FAXMODE,
+ FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
+ } else
+ return (0);
+}
+
+int
+TIFFInitCCITTRLEW(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ if (InitCCITTFax3(tif)) { /* reuse G3 support */
+ tif->tif_decoderow = Fax3DecodeRLE;
+ tif->tif_decodestrip = Fax3DecodeRLE;
+ tif->tif_decodetile = Fax3DecodeRLE;
+ /*
+ * Suppress RTC+EOLs when encoding and word-align data.
+ */
+ return TIFFSetField(tif, TIFFTAG_FAXMODE,
+ FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
+ } else
+ return (0);
+}
+#endif /* CCITT_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_fax3.h b/test/monniaux/tiff-4.0.10/tif_fax3.h
new file mode 100644
index 00000000..abadcd97
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_fax3.h
@@ -0,0 +1,538 @@
+/*
+ * Copyright (c) 1990-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _FAX3_
+#define _FAX3_
+/*
+ * TIFF Library.
+ *
+ * CCITT Group 3 (T.4) and Group 4 (T.6) Decompression Support.
+ *
+ * Decoder support is derived, with permission, from the code
+ * in Frank Cringle's viewfax program;
+ * Copyright (C) 1990, 1995 Frank D. Cringle.
+ */
+#include "tiff.h"
+
+/*
+ * To override the default routine used to image decoded
+ * spans one can use the pseudo tag TIFFTAG_FAXFILLFUNC.
+ * The routine must have the type signature given below;
+ * for example:
+ *
+ * fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
+ *
+ * where buf is place to set the bits, runs is the array of b&w run
+ * lengths (white then black), erun is the last run in the array, and
+ * lastx is the width of the row in pixels. Fill routines can assume
+ * the run array has room for at least lastx runs and can overwrite
+ * data in the run array as needed (e.g. to append zero runs to bring
+ * the count up to a nice multiple).
+ */
+typedef void (*TIFFFaxFillFunc)(unsigned char*, uint32*, uint32*, uint32);
+
+/*
+ * The default run filler; made external for other decoders.
+ */
+#if defined(__cplusplus)
+extern "C" {
+#endif
+extern void _TIFFFax3fillruns(unsigned char*, uint32*, uint32*, uint32);
+#if defined(__cplusplus)
+}
+#endif
+
+
+/* finite state machine codes */
+#define S_Null 0
+#define S_Pass 1
+#define S_Horiz 2
+#define S_V0 3
+#define S_VR 4
+#define S_VL 5
+#define S_Ext 6
+#define S_TermW 7
+#define S_TermB 8
+#define S_MakeUpW 9
+#define S_MakeUpB 10
+#define S_MakeUp 11
+#define S_EOL 12
+
+/* WARNING: do not change the layout of this structure as the HylaFAX software */
+/* really depends on it. See http://bugzilla.maptools.org/show_bug.cgi?id=2636 */
+typedef struct { /* state table entry */
+ unsigned char State; /* see above */
+ unsigned char Width; /* width of code in bits */
+ uint32 Param; /* unsigned 32-bit run length in bits (holds on 16 bit actually, but cannot be changed. See above warning) */
+} TIFFFaxTabEnt;
+
+extern const TIFFFaxTabEnt TIFFFaxMainTable[];
+extern const TIFFFaxTabEnt TIFFFaxWhiteTable[];
+extern const TIFFFaxTabEnt TIFFFaxBlackTable[];
+
+/*
+ * The following macros define the majority of the G3/G4 decoder
+ * algorithm using the state tables defined elsewhere. To build
+ * a decoder you need some setup code and some glue code. Note
+ * that you may also need/want to change the way the NeedBits*
+ * macros get input data if, for example, you know the data to be
+ * decoded is properly aligned and oriented (doing so before running
+ * the decoder can be a big performance win).
+ *
+ * Consult the decoder in the TIFF library for an idea of what you
+ * need to define and setup to make use of these definitions.
+ *
+ * NB: to enable a debugging version of these macros define FAX3_DEBUG
+ * before including this file. Trace output goes to stdout.
+ */
+
+#ifndef EndOfData
+#define EndOfData() (cp >= ep)
+#endif
+/*
+ * Need <=8 or <=16 bits of input data. Unlike viewfax we
+ * cannot use/assume a word-aligned, properly bit swizzled
+ * input data set because data may come from an arbitrarily
+ * aligned, read-only source such as a memory-mapped file.
+ * Note also that the viewfax decoder does not check for
+ * running off the end of the input data buffer. This is
+ * possible for G3-encoded data because it prescans the input
+ * data to count EOL markers, but can cause problems for G4
+ * data. In any event, we don't prescan and must watch for
+ * running out of data since we can't permit the library to
+ * scan past the end of the input data buffer.
+ *
+ * Finally, note that we must handle remaindered data at the end
+ * of a strip specially. The coder asks for a fixed number of
+ * bits when scanning for the next code. This may be more bits
+ * than are actually present in the data stream. If we appear
+ * to run out of data but still have some number of valid bits
+ * remaining then we makeup the requested amount with zeros and
+ * return successfully. If the returned data is incorrect then
+ * we should be called again and get a premature EOF error;
+ * otherwise we should get the right answer.
+ */
+#ifndef NeedBits8
+#define NeedBits8(n,eoflab) do { \
+ if (BitsAvail < (n)) { \
+ if (EndOfData()) { \
+ if (BitsAvail == 0) /* no valid bits */ \
+ goto eoflab; \
+ BitsAvail = (n); /* pad with zeros */ \
+ } else { \
+ BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
+ BitsAvail += 8; \
+ } \
+ } \
+} while (0)
+#endif
+#ifndef NeedBits16
+#define NeedBits16(n,eoflab) do { \
+ if (BitsAvail < (n)) { \
+ if (EndOfData()) { \
+ if (BitsAvail == 0) /* no valid bits */ \
+ goto eoflab; \
+ BitsAvail = (n); /* pad with zeros */ \
+ } else { \
+ BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
+ if ((BitsAvail += 8) < (n)) { \
+ if (EndOfData()) { \
+ /* NB: we know BitsAvail is non-zero here */ \
+ BitsAvail = (n); /* pad with zeros */ \
+ } else { \
+ BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
+ BitsAvail += 8; \
+ } \
+ } \
+ } \
+ } \
+} while (0)
+#endif
+#define GetBits(n) (BitAcc & ((1<<(n))-1))
+#define ClrBits(n) do { \
+ BitsAvail -= (n); \
+ BitAcc >>= (n); \
+} while (0)
+
+#ifdef FAX3_DEBUG
+static const char* StateNames[] = {
+ "Null ",
+ "Pass ",
+ "Horiz ",
+ "V0 ",
+ "VR ",
+ "VL ",
+ "Ext ",
+ "TermW ",
+ "TermB ",
+ "MakeUpW",
+ "MakeUpB",
+ "MakeUp ",
+ "EOL ",
+};
+#define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0')
+#define LOOKUP8(wid,tab,eoflab) do { \
+ int t; \
+ NeedBits8(wid,eoflab); \
+ TabEnt = tab + GetBits(wid); \
+ printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
+ StateNames[TabEnt->State], TabEnt->Param); \
+ for (t = 0; t < TabEnt->Width; t++) \
+ DEBUG_SHOW; \
+ putchar('\n'); \
+ fflush(stdout); \
+ ClrBits(TabEnt->Width); \
+} while (0)
+#define LOOKUP16(wid,tab,eoflab) do { \
+ int t; \
+ NeedBits16(wid,eoflab); \
+ TabEnt = tab + GetBits(wid); \
+ printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
+ StateNames[TabEnt->State], TabEnt->Param); \
+ for (t = 0; t < TabEnt->Width; t++) \
+ DEBUG_SHOW; \
+ putchar('\n'); \
+ fflush(stdout); \
+ ClrBits(TabEnt->Width); \
+} while (0)
+
+#define SETVALUE(x) do { \
+ *pa++ = RunLength + (x); \
+ printf("SETVALUE: %d\t%d\n", RunLength + (x), a0); \
+ a0 += x; \
+ RunLength = 0; \
+} while (0)
+#else
+#define LOOKUP8(wid,tab,eoflab) do { \
+ NeedBits8(wid,eoflab); \
+ TabEnt = tab + GetBits(wid); \
+ ClrBits(TabEnt->Width); \
+} while (0)
+#define LOOKUP16(wid,tab,eoflab) do { \
+ NeedBits16(wid,eoflab); \
+ TabEnt = tab + GetBits(wid); \
+ ClrBits(TabEnt->Width); \
+} while (0)
+
+/*
+ * Append a run to the run length array for the
+ * current row and reset decoding state.
+ */
+#define SETVALUE(x) do { \
+ *pa++ = RunLength + (x); \
+ a0 += (x); \
+ RunLength = 0; \
+} while (0)
+#endif
+
+/*
+ * Synchronize input decoding at the start of each
+ * row by scanning for an EOL (if appropriate) and
+ * skipping any trash data that might be present
+ * after a decoding error. Note that the decoding
+ * done elsewhere that recognizes an EOL only consumes
+ * 11 consecutive zero bits. This means that if EOLcnt
+ * is non-zero then we still need to scan for the final flag
+ * bit that is part of the EOL code.
+ */
+#define SYNC_EOL(eoflab) do { \
+ if (EOLcnt == 0) { \
+ for (;;) { \
+ NeedBits16(11,eoflab); \
+ if (GetBits(11) == 0) \
+ break; \
+ ClrBits(1); \
+ } \
+ } \
+ for (;;) { \
+ NeedBits8(8,eoflab); \
+ if (GetBits(8)) \
+ break; \
+ ClrBits(8); \
+ } \
+ while (GetBits(1) == 0) \
+ ClrBits(1); \
+ ClrBits(1); /* EOL bit */ \
+ EOLcnt = 0; /* reset EOL counter/flag */ \
+} while (0)
+
+/*
+ * Cleanup the array of runs after decoding a row.
+ * We adjust final runs to insure the user buffer is not
+ * overwritten and/or undecoded area is white filled.
+ */
+#define CLEANUP_RUNS() do { \
+ if (RunLength) \
+ SETVALUE(0); \
+ if (a0 != lastx) { \
+ badlength(a0, lastx); \
+ while (a0 > lastx && pa > thisrun) \
+ a0 -= *--pa; \
+ if (a0 < lastx) { \
+ if (a0 < 0) \
+ a0 = 0; \
+ if ((pa-thisrun)&1) \
+ SETVALUE(0); \
+ SETVALUE(lastx - a0); \
+ } else if (a0 > lastx) { \
+ SETVALUE(lastx); \
+ SETVALUE(0); \
+ } \
+ } \
+} while (0)
+
+/*
+ * Decode a line of 1D-encoded data.
+ *
+ * The line expanders are written as macros so that they can be reused
+ * but still have direct access to the local variables of the "calling"
+ * function.
+ *
+ * Note that unlike the original version we have to explicitly test for
+ * a0 >= lastx after each black/white run is decoded. This is because
+ * the original code depended on the input data being zero-padded to
+ * insure the decoder recognized an EOL before running out of data.
+ */
+#define EXPAND1D(eoflab) do { \
+ for (;;) { \
+ for (;;) { \
+ LOOKUP16(12, TIFFFaxWhiteTable, eof1d); \
+ switch (TabEnt->State) { \
+ case S_EOL: \
+ EOLcnt = 1; \
+ goto done1d; \
+ case S_TermW: \
+ SETVALUE(TabEnt->Param); \
+ goto doneWhite1d; \
+ case S_MakeUpW: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ unexpected("WhiteTable", a0); \
+ goto done1d; \
+ } \
+ } \
+ doneWhite1d: \
+ if (a0 >= lastx) \
+ goto done1d; \
+ for (;;) { \
+ LOOKUP16(13, TIFFFaxBlackTable, eof1d); \
+ switch (TabEnt->State) { \
+ case S_EOL: \
+ EOLcnt = 1; \
+ goto done1d; \
+ case S_TermB: \
+ SETVALUE(TabEnt->Param); \
+ goto doneBlack1d; \
+ case S_MakeUpB: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ unexpected("BlackTable", a0); \
+ goto done1d; \
+ } \
+ } \
+ doneBlack1d: \
+ if (a0 >= lastx) \
+ goto done1d; \
+ if( *(pa-1) == 0 && *(pa-2) == 0 ) \
+ pa -= 2; \
+ } \
+eof1d: \
+ prematureEOF(a0); \
+ CLEANUP_RUNS(); \
+ goto eoflab; \
+done1d: \
+ CLEANUP_RUNS(); \
+} while (0)
+
+/*
+ * Update the value of b1 using the array
+ * of runs for the reference line.
+ */
+#define CHECK_b1 do { \
+ if (pa != thisrun) while (b1 <= a0 && b1 < lastx) { \
+ b1 += pb[0] + pb[1]; \
+ pb += 2; \
+ } \
+} while (0)
+
+/*
+ * Expand a row of 2D-encoded data.
+ */
+#define EXPAND2D(eoflab) do { \
+ while (a0 < lastx) { \
+ LOOKUP8(7, TIFFFaxMainTable, eof2d); \
+ switch (TabEnt->State) { \
+ case S_Pass: \
+ CHECK_b1; \
+ b1 += *pb++; \
+ RunLength += b1 - a0; \
+ a0 = b1; \
+ b1 += *pb++; \
+ break; \
+ case S_Horiz: \
+ if ((pa-thisrun)&1) { \
+ for (;;) { /* black first */ \
+ LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
+ switch (TabEnt->State) { \
+ case S_TermB: \
+ SETVALUE(TabEnt->Param); \
+ goto doneWhite2da; \
+ case S_MakeUpB: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ goto badBlack2d; \
+ } \
+ } \
+ doneWhite2da:; \
+ for (;;) { /* then white */ \
+ LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
+ switch (TabEnt->State) { \
+ case S_TermW: \
+ SETVALUE(TabEnt->Param); \
+ goto doneBlack2da; \
+ case S_MakeUpW: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ goto badWhite2d; \
+ } \
+ } \
+ doneBlack2da:; \
+ } else { \
+ for (;;) { /* white first */ \
+ LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
+ switch (TabEnt->State) { \
+ case S_TermW: \
+ SETVALUE(TabEnt->Param); \
+ goto doneWhite2db; \
+ case S_MakeUpW: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ goto badWhite2d; \
+ } \
+ } \
+ doneWhite2db:; \
+ for (;;) { /* then black */ \
+ LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
+ switch (TabEnt->State) { \
+ case S_TermB: \
+ SETVALUE(TabEnt->Param); \
+ goto doneBlack2db; \
+ case S_MakeUpB: \
+ case S_MakeUp: \
+ a0 += TabEnt->Param; \
+ RunLength += TabEnt->Param; \
+ break; \
+ default: \
+ goto badBlack2d; \
+ } \
+ } \
+ doneBlack2db:; \
+ } \
+ CHECK_b1; \
+ break; \
+ case S_V0: \
+ CHECK_b1; \
+ SETVALUE(b1 - a0); \
+ b1 += *pb++; \
+ break; \
+ case S_VR: \
+ CHECK_b1; \
+ SETVALUE(b1 - a0 + TabEnt->Param); \
+ b1 += *pb++; \
+ break; \
+ case S_VL: \
+ CHECK_b1; \
+ if (b1 <= (int) (a0 + TabEnt->Param)) { \
+ if (b1 < (int) (a0 + TabEnt->Param) || pa != thisrun) { \
+ unexpected("VL", a0); \
+ goto eol2d; \
+ } \
+ } \
+ SETVALUE(b1 - a0 - TabEnt->Param); \
+ b1 -= *--pb; \
+ break; \
+ case S_Ext: \
+ *pa++ = lastx - a0; \
+ extension(a0); \
+ goto eol2d; \
+ case S_EOL: \
+ *pa++ = lastx - a0; \
+ NeedBits8(4,eof2d); \
+ if (GetBits(4)) \
+ unexpected("EOL", a0); \
+ ClrBits(4); \
+ EOLcnt = 1; \
+ goto eol2d; \
+ default: \
+ badMain2d: \
+ unexpected("MainTable", a0); \
+ goto eol2d; \
+ badBlack2d: \
+ unexpected("BlackTable", a0); \
+ goto eol2d; \
+ badWhite2d: \
+ unexpected("WhiteTable", a0); \
+ goto eol2d; \
+ eof2d: \
+ prematureEOF(a0); \
+ CLEANUP_RUNS(); \
+ goto eoflab; \
+ } \
+ } \
+ if (RunLength) { \
+ if (RunLength + a0 < lastx) { \
+ /* expect a final V0 */ \
+ NeedBits8(1,eof2d); \
+ if (!GetBits(1)) \
+ goto badMain2d; \
+ ClrBits(1); \
+ } \
+ SETVALUE(0); \
+ } \
+eol2d: \
+ CLEANUP_RUNS(); \
+} while (0)
+#endif /* _FAX3_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_fax3sm.c b/test/monniaux/tiff-4.0.10/tif_fax3sm.c
new file mode 100644
index 00000000..822191ec
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_fax3sm.c
@@ -0,0 +1,1260 @@
+/* WARNING, this file was automatically generated by the
+ mkg3states program */
+#include "tiff.h"
+#include "tif_fax3.h"
+ const TIFFFaxTabEnt TIFFFaxMainTable[128] = {
+{12,7,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},
+{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{5,6,2},{3,1,0},{5,3,1},{3,1,0},
+{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},
+{4,3,1},{3,1,0},{5,7,3},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},
+{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{4,6,2},{3,1,0},
+{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},
+{2,3,0},{3,1,0},{4,3,1},{3,1,0},{6,7,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},
+{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},
+{5,6,2},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},
+{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{4,7,3},{3,1,0},{5,3,1},{3,1,0},
+{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},
+{4,3,1},{3,1,0},{4,6,2},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},
+{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0}
+};
+ const TIFFFaxTabEnt TIFFFaxWhiteTable[4096] = {
+{12,11,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},
+{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},
+{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},
+{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1792},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},
+{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},
+{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},
+{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},
+{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},
+{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},
+{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},
+{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},
+{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},
+{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},
+{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},
+{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1856},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},
+{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},
+{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},
+{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},
+{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},
+{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},
+{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},
+{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},
+{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},
+{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{11,12,2112},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},
+{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},
+{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},
+{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},
+{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},
+{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},
+{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},
+{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2368},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},
+{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},
+{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},
+{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},
+{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},
+{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},
+{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},
+{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},
+{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},
+{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},
+{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},
+{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},
+{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{11,12,1984},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},
+{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},
+{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},
+{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},
+{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},
+{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},
+{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},
+{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1920},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},
+{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},
+{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},
+{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},
+{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},
+{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},
+{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},
+{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},
+{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},
+{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},
+{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},
+{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2240},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},
+{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},
+{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},
+{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},
+{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},
+{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},
+{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},
+{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},
+{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},
+{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{11,12,2496},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},
+{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},
+{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},
+{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{12,11,0},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},
+{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},
+{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},
+{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},
+{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1792},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},
+{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},
+{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},
+{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},
+{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},
+{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},
+{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},
+{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},
+{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},
+{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},
+{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},
+{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},
+{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{11,11,1856},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},
+{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},
+{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},
+{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},
+{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},
+{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},
+{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},
+{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2176},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},
+{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},
+{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},
+{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},
+{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},
+{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},
+{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},
+{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},
+{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},
+{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},
+{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},
+{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2432},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},
+{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},
+{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},
+{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},
+{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},
+{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},
+{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},
+{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},
+{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},
+{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{11,12,2048},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},
+{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},
+{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},
+{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},
+{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},
+{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},
+{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},
+{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1920},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},
+{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},
+{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},
+{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},
+{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},
+{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},
+{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},
+{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},
+{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},
+{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},
+{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},
+{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},
+{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},
+{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},
+{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},
+{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{11,12,2304},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},
+{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},
+{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},
+{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},
+{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},
+{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},
+{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},
+{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},
+{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},
+{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},
+{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},
+{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},
+{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},
+{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},
+{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},
+{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},
+{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},
+{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},
+{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2560},{7,4,3},
+{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},
+{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},
+{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},
+{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},
+{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},
+{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},
+{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},
+{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},
+{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},
+{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},
+{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},
+{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},
+{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}
+};
+ const TIFFFaxTabEnt TIFFFaxBlackTable[8192] = {
+{12,11,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,23},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,11,25},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,128},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,56},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,30},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,57},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,11,21},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,54},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,52},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,48},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,12,2112},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,44},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,36},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,384},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,28},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,60},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,40},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2368},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,12,1984},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,50},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,34},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1664},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,26},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1408},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,32},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,61},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,42},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,13,1024},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,13,768},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,62},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2240},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,46},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,38},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,512},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,19},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,12,2496},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,25},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,12,192},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1280},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,31},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,58},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,21},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,896},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,640},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,49},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2176},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,45},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,37},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,12,448},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,29},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,13,1536},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,41},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2432},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,12,2048},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,51},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,35},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,320},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,27},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,59},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,33},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,256},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,43},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,13,1152},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,55},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,63},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,12,2304},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,47},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,39},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,53},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2560},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,25},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,12,128},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,56},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,30},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,57},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,21},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,54},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,52},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,48},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2112},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,44},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,36},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,12,384},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,28},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,60},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,40},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,12,2368},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,1984},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,50},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,34},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,13,1728},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,26},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,13,1472},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,32},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,61},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,42},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1088},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,832},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,62},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,12,2240},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,46},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,38},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,576},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2496},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,25},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,192},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1344},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,31},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1856},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,58},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,21},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{10,13,960},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,13,704},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,49},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2176},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,45},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,37},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,448},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,29},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1600},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,41},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{11,12,2432},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2048},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,51},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,35},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{10,12,320},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,27},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,59},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,33},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,256},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,43},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1216},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,55},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,63},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2304},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,12,47},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,39},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,12,53},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2560},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},
+{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},
+{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},
+{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},
+{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},
+{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},
+{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},
+{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
+{8,3,4},{8,2,2}
+};
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_flush.c b/test/monniaux/tiff-4.0.10/tif_flush.c
new file mode 100644
index 00000000..881fac51
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_flush.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ */
+#include "tiffiop.h"
+
+int
+TIFFFlush(TIFF* tif)
+{
+ if( tif->tif_mode == O_RDONLY )
+ return 1;
+
+ if (!TIFFFlushData(tif))
+ return (0);
+
+ /* In update (r+) mode we try to detect the case where
+ only the strip/tile map has been altered, and we try to
+ rewrite only that portion of the directory without
+ making any other changes */
+
+ if( (tif->tif_flags & TIFF_DIRTYSTRIP)
+ && !(tif->tif_flags & TIFF_DIRTYDIRECT)
+ && tif->tif_mode == O_RDWR )
+ {
+ uint64 *offsets=NULL, *sizes=NULL;
+
+ if( TIFFIsTiled(tif) )
+ {
+ if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )
+ && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )
+ && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,
+ tif->tif_dir.td_nstrips, offsets )
+ && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,
+ tif->tif_dir.td_nstrips, sizes ) )
+ {
+ tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+ tif->tif_flags &= ~TIFF_BEENWRITING;
+ return 1;
+ }
+ }
+ else
+ {
+ if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )
+ && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )
+ && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,
+ tif->tif_dir.td_nstrips, offsets )
+ && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,
+ tif->tif_dir.td_nstrips, sizes ) )
+ {
+ tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+ tif->tif_flags &= ~TIFF_BEENWRITING;
+ return 1;
+ }
+ }
+ }
+
+ if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))
+ && !TIFFRewriteDirectory(tif))
+ return (0);
+
+ return (1);
+}
+
+/*
+ * Flush buffered data to the file.
+ *
+ * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
+ * is not set, so that TIFFFlush() will proceed to write out the directory.
+ * The documentation says returning 1 is an error indicator, but not having
+ * been writing isn't exactly a an error. Hopefully this doesn't cause
+ * problems for other people.
+ */
+int
+TIFFFlushData(TIFF* tif)
+{
+ if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
+ return (1);
+ if (tif->tif_flags & TIFF_POSTENCODE) {
+ tif->tif_flags &= ~TIFF_POSTENCODE;
+ if (!(*tif->tif_postencode)(tif))
+ return (0);
+ }
+ return (TIFFFlushData1(tif));
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_getimage.c b/test/monniaux/tiff-4.0.10/tif_getimage.c
new file mode 100644
index 00000000..6a9d5a7c
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_getimage.c
@@ -0,0 +1,3046 @@
+/*
+ * Copyright (c) 1991-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library
+ *
+ * Read and return a packed RGBA image.
+ */
+#include "tiffiop.h"
+#include <stdio.h>
+
+static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int PickContigCase(TIFFRGBAImage*);
+static int PickSeparateCase(TIFFRGBAImage*);
+
+static int BuildMapUaToAa(TIFFRGBAImage* img);
+static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
+
+static const char photoTag[] = "PhotometricInterpretation";
+
+/*
+ * Helper constants used in Orientation tag handling
+ */
+#define FLIP_VERTICALLY 0x01
+#define FLIP_HORIZONTALLY 0x02
+
+/*
+ * Color conversion constants. We will define display types here.
+ */
+
+static const TIFFDisplay display_sRGB = {
+ { /* XYZ -> luminance matrix */
+ { 3.2410F, -1.5374F, -0.4986F },
+ { -0.9692F, 1.8760F, 0.0416F },
+ { 0.0556F, -0.2040F, 1.0570F }
+ },
+ 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
+ 255, 255, 255, /* Pixel values for ref. white */
+ 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
+ 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
+};
+
+/*
+ * Check the image to see if TIFFReadRGBAImage can deal with it.
+ * 1/0 is returned according to whether or not the image can
+ * be handled. If 0 is returned, emsg contains the reason
+ * why it is being rejected.
+ */
+int
+TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
+{
+ TIFFDirectory* td = &tif->tif_dir;
+ uint16 photometric;
+ int colorchannels;
+
+ if (!tif->tif_decodestatus) {
+ sprintf(emsg, "Sorry, requested compression method is not configured");
+ return (0);
+ }
+ switch (td->td_bitspersample) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ case 16:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+ td->td_bitspersample);
+ return (0);
+ }
+ if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP) {
+ sprintf(emsg, "Sorry, can not handle images with IEEE floating-point samples");
+ return (0);
+ }
+ colorchannels = td->td_samplesperpixel - td->td_extrasamples;
+ if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
+ switch (colorchannels) {
+ case 1:
+ photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+ case 3:
+ photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ sprintf(emsg, "Missing needed %s tag", photoTag);
+ return (0);
+ }
+ }
+ switch (photometric) {
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ case PHOTOMETRIC_PALETTE:
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG
+ && td->td_samplesperpixel != 1
+ && td->td_bitspersample < 8 ) {
+ sprintf(emsg,
+ "Sorry, can not handle contiguous data with %s=%d, "
+ "and %s=%d and Bits/Sample=%d",
+ photoTag, photometric,
+ "Samples/pixel", td->td_samplesperpixel,
+ td->td_bitspersample);
+ return (0);
+ }
+ /*
+ * We should likely validate that any extra samples are either
+ * to be ignored, or are alpha, and if alpha we should try to use
+ * them. But for now we won't bother with this.
+ */
+ break;
+ case PHOTOMETRIC_YCBCR:
+ /*
+ * TODO: if at all meaningful and useful, make more complete
+ * support check here, or better still, refactor to let supporting
+ * code decide whether there is support and what meaningful
+ * error to return
+ */
+ break;
+ case PHOTOMETRIC_RGB:
+ if (colorchannels < 3) {
+ sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+ "Color channels", colorchannels);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ {
+ uint16 inkset;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+ if (inkset != INKSET_CMYK) {
+ sprintf(emsg,
+ "Sorry, can not handle separated image with %s=%d",
+ "InkSet", inkset);
+ return 0;
+ }
+ if (td->td_samplesperpixel < 4) {
+ sprintf(emsg,
+ "Sorry, can not handle separated image with %s=%d",
+ "Samples/pixel", td->td_samplesperpixel);
+ return 0;
+ }
+ break;
+ }
+ case PHOTOMETRIC_LOGL:
+ if (td->td_compression != COMPRESSION_SGILOG) {
+ sprintf(emsg, "Sorry, LogL data must have %s=%d",
+ "Compression", COMPRESSION_SGILOG);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_LOGLUV:
+ if (td->td_compression != COMPRESSION_SGILOG &&
+ td->td_compression != COMPRESSION_SGILOG24) {
+ sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+ "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+ return (0);
+ }
+ if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
+ sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+ "Planarconfiguration", td->td_planarconfig);
+ return (0);
+ }
+ if ( td->td_samplesperpixel != 3 || colorchannels != 3 ) {
+ sprintf(emsg,
+ "Sorry, can not handle image with %s=%d, %s=%d",
+ "Samples/pixel", td->td_samplesperpixel,
+ "colorchannels", colorchannels);
+ return 0;
+ }
+ break;
+ case PHOTOMETRIC_CIELAB:
+ if ( td->td_samplesperpixel != 3 || colorchannels != 3 || td->td_bitspersample != 8 ) {
+ sprintf(emsg,
+ "Sorry, can not handle image with %s=%d, %s=%d and %s=%d",
+ "Samples/pixel", td->td_samplesperpixel,
+ "colorchannels", colorchannels,
+ "Bits/sample", td->td_bitspersample);
+ return 0;
+ }
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle image with %s=%d",
+ photoTag, photometric);
+ return (0);
+ }
+ return (1);
+}
+
+void
+TIFFRGBAImageEnd(TIFFRGBAImage* img)
+{
+ if (img->Map) {
+ _TIFFfree(img->Map);
+ img->Map = NULL;
+ }
+ if (img->BWmap) {
+ _TIFFfree(img->BWmap);
+ img->BWmap = NULL;
+ }
+ if (img->PALmap) {
+ _TIFFfree(img->PALmap);
+ img->PALmap = NULL;
+ }
+ if (img->ycbcr) {
+ _TIFFfree(img->ycbcr);
+ img->ycbcr = NULL;
+ }
+ if (img->cielab) {
+ _TIFFfree(img->cielab);
+ img->cielab = NULL;
+ }
+ if (img->UaToAa) {
+ _TIFFfree(img->UaToAa);
+ img->UaToAa = NULL;
+ }
+ if (img->Bitdepth16To8) {
+ _TIFFfree(img->Bitdepth16To8);
+ img->Bitdepth16To8 = NULL;
+ }
+
+ if( img->redcmap ) {
+ _TIFFfree( img->redcmap );
+ _TIFFfree( img->greencmap );
+ _TIFFfree( img->bluecmap );
+ img->redcmap = img->greencmap = img->bluecmap = NULL;
+ }
+}
+
+static int
+isCCITTCompression(TIFF* tif)
+{
+ uint16 compress;
+ TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
+ return (compress == COMPRESSION_CCITTFAX3 ||
+ compress == COMPRESSION_CCITTFAX4 ||
+ compress == COMPRESSION_CCITTRLE ||
+ compress == COMPRESSION_CCITTRLEW);
+}
+
+int
+TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
+{
+ uint16* sampleinfo;
+ uint16 extrasamples;
+ uint16 planarconfig;
+ uint16 compress;
+ int colorchannels;
+ uint16 *red_orig, *green_orig, *blue_orig;
+ int n_color;
+
+ if( !TIFFRGBAImageOK(tif, emsg) )
+ return 0;
+
+ /* Initialize to normal values */
+ img->row_offset = 0;
+ img->col_offset = 0;
+ img->redcmap = NULL;
+ img->greencmap = NULL;
+ img->bluecmap = NULL;
+ img->Map = NULL;
+ img->BWmap = NULL;
+ img->PALmap = NULL;
+ img->ycbcr = NULL;
+ img->cielab = NULL;
+ img->UaToAa = NULL;
+ img->Bitdepth16To8 = NULL;
+ img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
+
+ img->tif = tif;
+ img->stoponerr = stop;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
+ switch (img->bitspersample) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ case 16:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+ img->bitspersample);
+ goto fail_return;
+ }
+ img->alpha = 0;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
+ &extrasamples, &sampleinfo);
+ if (extrasamples >= 1)
+ {
+ switch (sampleinfo[0]) {
+ case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
+ if (img->samplesperpixel > 3) /* correct info about alpha channel */
+ img->alpha = EXTRASAMPLE_ASSOCALPHA;
+ break;
+ case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
+ case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
+ img->alpha = sampleinfo[0];
+ break;
+ }
+ }
+
+#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
+ if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
+ img->photometric = PHOTOMETRIC_MINISWHITE;
+
+ if( extrasamples == 0
+ && img->samplesperpixel == 4
+ && img->photometric == PHOTOMETRIC_RGB )
+ {
+ img->alpha = EXTRASAMPLE_ASSOCALPHA;
+ extrasamples = 1;
+ }
+#endif
+
+ colorchannels = img->samplesperpixel - extrasamples;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
+ if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
+ switch (colorchannels) {
+ case 1:
+ if (isCCITTCompression(tif))
+ img->photometric = PHOTOMETRIC_MINISWHITE;
+ else
+ img->photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+ case 3:
+ img->photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ sprintf(emsg, "Missing needed %s tag", photoTag);
+ goto fail_return;
+ }
+ }
+ switch (img->photometric) {
+ case PHOTOMETRIC_PALETTE:
+ if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
+ &red_orig, &green_orig, &blue_orig)) {
+ sprintf(emsg, "Missing required \"Colormap\" tag");
+ goto fail_return;
+ }
+
+ /* copy the colormaps so we can modify them */
+ n_color = (1U << img->bitspersample);
+ img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
+ sprintf(emsg, "Out of memory for colormap copy");
+ goto fail_return;
+ }
+
+ _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
+ _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
+ _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
+
+ /* fall through... */
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ if (planarconfig == PLANARCONFIG_CONTIG
+ && img->samplesperpixel != 1
+ && img->bitspersample < 8 ) {
+ sprintf(emsg,
+ "Sorry, can not handle contiguous data with %s=%d, "
+ "and %s=%d and Bits/Sample=%d",
+ photoTag, img->photometric,
+ "Samples/pixel", img->samplesperpixel,
+ img->bitspersample);
+ goto fail_return;
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ /* It would probably be nice to have a reality check here. */
+ if (planarconfig == PLANARCONFIG_CONTIG)
+ /* can rely on libjpeg to convert to RGB */
+ /* XXX should restore current state on exit */
+ switch (compress) {
+ case COMPRESSION_JPEG:
+ /*
+ * TODO: when complete tests verify complete desubsampling
+ * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
+ * favor of tif_getimage.c native handling
+ */
+ TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
+ img->photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ /* do nothing */;
+ break;
+ }
+ /*
+ * TODO: if at all meaningful and useful, make more complete
+ * support check here, or better still, refactor to let supporting
+ * code decide whether there is support and what meaningful
+ * error to return
+ */
+ break;
+ case PHOTOMETRIC_RGB:
+ if (colorchannels < 3) {
+ sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+ "Color channels", colorchannels);
+ goto fail_return;
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ {
+ uint16 inkset;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+ if (inkset != INKSET_CMYK) {
+ sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+ "InkSet", inkset);
+ goto fail_return;
+ }
+ if (img->samplesperpixel < 4) {
+ sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+ "Samples/pixel", img->samplesperpixel);
+ goto fail_return;
+ }
+ }
+ break;
+ case PHOTOMETRIC_LOGL:
+ if (compress != COMPRESSION_SGILOG) {
+ sprintf(emsg, "Sorry, LogL data must have %s=%d",
+ "Compression", COMPRESSION_SGILOG);
+ goto fail_return;
+ }
+ TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+ img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
+ img->bitspersample = 8;
+ break;
+ case PHOTOMETRIC_LOGLUV:
+ if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
+ sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+ "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+ goto fail_return;
+ }
+ if (planarconfig != PLANARCONFIG_CONTIG) {
+ sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+ "Planarconfiguration", planarconfig);
+ return (0);
+ }
+ TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+ img->photometric = PHOTOMETRIC_RGB; /* little white lie */
+ img->bitspersample = 8;
+ break;
+ case PHOTOMETRIC_CIELAB:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle image with %s=%d",
+ photoTag, img->photometric);
+ goto fail_return;
+ }
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
+ img->isContig =
+ !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
+ if (img->isContig) {
+ if (!PickContigCase(img)) {
+ sprintf(emsg, "Sorry, can not handle image");
+ goto fail_return;
+ }
+ } else {
+ if (!PickSeparateCase(img)) {
+ sprintf(emsg, "Sorry, can not handle image");
+ goto fail_return;
+ }
+ }
+ return 1;
+
+ fail_return:
+ TIFFRGBAImageEnd( img );
+ return 0;
+}
+
+int
+TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+{
+ if (img->get == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
+ return (0);
+ }
+ if (img->put.any == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
+ "No \"put\" routine setupl; probably can not handle image format");
+ return (0);
+ }
+ return (*img->get)(img, raster, w, h);
+}
+
+/*
+ * Read the specified image into an ABGR-format rastertaking in account
+ * specified orientation.
+ */
+int
+TIFFReadRGBAImageOriented(TIFF* tif,
+ uint32 rwidth, uint32 rheight, uint32* raster,
+ int orientation, int stop)
+{
+ char emsg[1024] = "";
+ TIFFRGBAImage img;
+ int ok;
+
+ if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
+ img.req_orientation = (uint16)orientation;
+ /* XXX verify rwidth and rheight against width and height */
+ ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
+ rwidth, img.height);
+ TIFFRGBAImageEnd(&img);
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
+ ok = 0;
+ }
+ return (ok);
+}
+
+/*
+ * Read the specified image into an ABGR-format raster. Use bottom left
+ * origin for raster by default.
+ */
+int
+TIFFReadRGBAImage(TIFF* tif,
+ uint32 rwidth, uint32 rheight, uint32* raster, int stop)
+{
+ return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
+ ORIENTATION_BOTLEFT, stop);
+}
+
+static int
+setorientation(TIFFRGBAImage* img)
+{
+ switch (img->orientation) {
+ case ORIENTATION_TOPLEFT:
+ case ORIENTATION_LEFTTOP:
+ if (img->req_orientation == ORIENTATION_TOPRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTTOP)
+ return FLIP_HORIZONTALLY;
+ else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTBOT)
+ return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_BOTLEFT ||
+ img->req_orientation == ORIENTATION_LEFTBOT)
+ return FLIP_VERTICALLY;
+ else
+ return 0;
+ case ORIENTATION_TOPRIGHT:
+ case ORIENTATION_RIGHTTOP:
+ if (img->req_orientation == ORIENTATION_TOPLEFT ||
+ img->req_orientation == ORIENTATION_LEFTTOP)
+ return FLIP_HORIZONTALLY;
+ else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTBOT)
+ return FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_BOTLEFT ||
+ img->req_orientation == ORIENTATION_LEFTBOT)
+ return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
+ else
+ return 0;
+ case ORIENTATION_BOTRIGHT:
+ case ORIENTATION_RIGHTBOT:
+ if (img->req_orientation == ORIENTATION_TOPLEFT ||
+ img->req_orientation == ORIENTATION_LEFTTOP)
+ return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTTOP)
+ return FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_BOTLEFT ||
+ img->req_orientation == ORIENTATION_LEFTBOT)
+ return FLIP_HORIZONTALLY;
+ else
+ return 0;
+ case ORIENTATION_BOTLEFT:
+ case ORIENTATION_LEFTBOT:
+ if (img->req_orientation == ORIENTATION_TOPLEFT ||
+ img->req_orientation == ORIENTATION_LEFTTOP)
+ return FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTTOP)
+ return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
+ else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
+ img->req_orientation == ORIENTATION_RIGHTBOT)
+ return FLIP_HORIZONTALLY;
+ else
+ return 0;
+ default: /* NOTREACHED */
+ return 0;
+ }
+}
+
+/*
+ * Get an tile-organized image that has
+ * PlanarConfiguration contiguous if SamplesPerPixel > 1
+ * or
+ * SamplesPerPixel == 1
+ */
+static int
+gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+{
+ TIFF* tif = img->tif;
+ tileContigRoutine put = img->put.contig;
+ uint32 col, row, y, rowstoread;
+ tmsize_t pos;
+ uint32 tw, th;
+ unsigned char* buf = NULL;
+ int32 fromskew, toskew;
+ uint32 nrow;
+ int ret = 1, flip;
+ uint32 this_tw, tocol;
+ int32 this_toskew, leftmost_toskew;
+ int32 leftmost_fromskew;
+ uint32 leftmost_tw;
+ tmsize_t bufsize;
+
+ bufsize = TIFFTileSize(tif);
+ if (bufsize == 0) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
+ return (0);
+ }
+
+ TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
+ TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(tw + w);
+ }
+ else {
+ y = 0;
+ toskew = -(int32)(tw - w);
+ }
+
+ /*
+ * Leftmost tile is clipped on left side if col_offset > 0.
+ */
+ leftmost_fromskew = img->col_offset % tw;
+ leftmost_tw = tw - leftmost_fromskew;
+ leftmost_toskew = toskew + leftmost_fromskew;
+ for (row = 0; ret != 0 && row < h; row += nrow)
+ {
+ rowstoread = th - (row + img->row_offset) % th;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ fromskew = leftmost_fromskew;
+ this_tw = leftmost_tw;
+ this_toskew = leftmost_toskew;
+ tocol = 0;
+ col = img->col_offset;
+ while (tocol < w)
+ {
+ if (_TIFFReadTileAndAllocBuffer(tif, (void**) &buf, bufsize, col,
+ row+img->row_offset, 0, 0)==(tmsize_t)(-1) &&
+ (buf == NULL || img->stoponerr))
+ {
+ ret = 0;
+ break;
+ }
+ pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
+ ((tmsize_t) fromskew * img->samplesperpixel);
+ if (tocol + this_tw > w)
+ {
+ /*
+ * Rightmost tile is clipped on right side.
+ */
+ fromskew = tw - (w - tocol);
+ this_tw = tw - fromskew;
+ this_toskew = toskew + fromskew;
+ }
+ (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, buf + pos);
+ tocol += this_tw;
+ col += this_tw;
+ /*
+ * After the leftmost tile, tiles are no longer clipped on left side.
+ */
+ fromskew = 0;
+ this_tw = tw;
+ this_toskew = toskew;
+ }
+
+ y += ((flip & FLIP_VERTICALLY) ? -(int32) nrow : (int32) nrow);
+ }
+ _TIFFfree(buf);
+
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
+
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
+
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++;
+ right--;
+ }
+ }
+ }
+
+ return (ret);
+}
+
+/*
+ * Get an tile-organized image that has
+ * SamplesPerPixel > 1
+ * PlanarConfiguration separated
+ * We assume that all such images are RGB.
+ */
+static int
+gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+{
+ TIFF* tif = img->tif;
+ tileSeparateRoutine put = img->put.separate;
+ uint32 col, row, y, rowstoread;
+ tmsize_t pos;
+ uint32 tw, th;
+ unsigned char* buf = NULL;
+ unsigned char* p0 = NULL;
+ unsigned char* p1 = NULL;
+ unsigned char* p2 = NULL;
+ unsigned char* pa = NULL;
+ tmsize_t tilesize;
+ tmsize_t bufsize;
+ int32 fromskew, toskew;
+ int alpha = img->alpha;
+ uint32 nrow;
+ int ret = 1, flip;
+ uint16 colorchannels;
+ uint32 this_tw, tocol;
+ int32 this_toskew, leftmost_toskew;
+ int32 leftmost_fromskew;
+ uint32 leftmost_tw;
+
+ tilesize = TIFFTileSize(tif);
+ bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
+ if (bufsize == 0) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
+ return (0);
+ }
+
+ TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
+ TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(tw + w);
+ }
+ else {
+ y = 0;
+ toskew = -(int32)(tw - w);
+ }
+
+ switch( img->photometric )
+ {
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ case PHOTOMETRIC_PALETTE:
+ colorchannels = 1;
+ break;
+
+ default:
+ colorchannels = 3;
+ break;
+ }
+
+ /*
+ * Leftmost tile is clipped on left side if col_offset > 0.
+ */
+ leftmost_fromskew = img->col_offset % tw;
+ leftmost_tw = tw - leftmost_fromskew;
+ leftmost_toskew = toskew + leftmost_fromskew;
+ for (row = 0; ret != 0 && row < h; row += nrow)
+ {
+ rowstoread = th - (row + img->row_offset) % th;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ fromskew = leftmost_fromskew;
+ this_tw = leftmost_tw;
+ this_toskew = leftmost_toskew;
+ tocol = 0;
+ col = img->col_offset;
+ while (tocol < w)
+ {
+ if( buf == NULL )
+ {
+ if (_TIFFReadTileAndAllocBuffer(
+ tif, (void**) &buf, bufsize, col,
+ row+img->row_offset,0,0)==(tmsize_t)(-1)
+ && (buf == NULL || img->stoponerr))
+ {
+ ret = 0;
+ break;
+ }
+ p0 = buf;
+ if( colorchannels == 1 )
+ {
+ p2 = p1 = p0;
+ pa = (alpha?(p0+3*tilesize):NULL);
+ }
+ else
+ {
+ p1 = p0 + tilesize;
+ p2 = p1 + tilesize;
+ pa = (alpha?(p2+tilesize):NULL);
+ }
+ }
+ else if (TIFFReadTile(tif, p0, col,
+ row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (colorchannels > 1
+ && TIFFReadTile(tif, p1, col,
+ row+img->row_offset,0,1) == (tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (colorchannels > 1
+ && TIFFReadTile(tif, p2, col,
+ row+img->row_offset,0,2) == (tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (alpha
+ && TIFFReadTile(tif,pa,col,
+ row+img->row_offset,0,colorchannels) == (tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+
+ pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
+ ((tmsize_t) fromskew * img->samplesperpixel);
+ if (tocol + this_tw > w)
+ {
+ /*
+ * Rightmost tile is clipped on right side.
+ */
+ fromskew = tw - (w - tocol);
+ this_tw = tw - fromskew;
+ this_toskew = toskew + fromskew;
+ }
+ (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, \
+ p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
+ tocol += this_tw;
+ col += this_tw;
+ /*
+ * After the leftmost tile, tiles are no longer clipped on left side.
+ */
+ fromskew = 0;
+ this_tw = tw;
+ this_toskew = toskew;
+ }
+
+ y += ((flip & FLIP_VERTICALLY) ?-(int32) nrow : (int32) nrow);
+ }
+
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
+
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
+
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++;
+ right--;
+ }
+ }
+ }
+
+ _TIFFfree(buf);
+ return (ret);
+}
+
+/*
+ * Get a strip-organized image that has
+ * PlanarConfiguration contiguous if SamplesPerPixel > 1
+ * or
+ * SamplesPerPixel == 1
+ */
+static int
+gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+{
+ TIFF* tif = img->tif;
+ tileContigRoutine put = img->put.contig;
+ uint32 row, y, nrow, nrowsub, rowstoread;
+ tmsize_t pos;
+ unsigned char* buf = NULL;
+ uint32 rowsperstrip;
+ uint16 subsamplinghor,subsamplingver;
+ uint32 imagewidth = img->width;
+ tmsize_t scanline;
+ int32 fromskew, toskew;
+ int ret = 1, flip;
+ tmsize_t maxstripsize;
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
+ if( subsamplingver == 0 ) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Invalid vertical YCbCr subsampling");
+ return (0);
+ }
+
+ maxstripsize = TIFFStripSize(tif);
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(w + w);
+ } else {
+ y = 0;
+ toskew = -(int32)(w - w);
+ }
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+
+ scanline = TIFFScanlineSize(tif);
+ fromskew = (w < imagewidth ? imagewidth - w : 0);
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ nrowsub = nrow;
+ if ((nrowsub%subsamplingver)!=0)
+ nrowsub+=subsamplingver-nrowsub%subsamplingver;
+ if (_TIFFReadEncodedStripAndAllocBuffer(tif,
+ TIFFComputeStrip(tif,row+img->row_offset, 0),
+ (void**)(&buf),
+ maxstripsize,
+ ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
+ && (buf == NULL || img->stoponerr))
+ {
+ ret = 0;
+ break;
+ }
+
+ pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
+ ((tmsize_t) img->col_offset * img->samplesperpixel);
+ (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
+ y += ((flip & FLIP_VERTICALLY) ? -(int32) nrow : (int32) nrow);
+ }
+
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
+
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
+
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++;
+ right--;
+ }
+ }
+ }
+
+ _TIFFfree(buf);
+ return (ret);
+}
+
+/*
+ * Get a strip-organized image with
+ * SamplesPerPixel > 1
+ * PlanarConfiguration separated
+ * We assume that all such images are RGB.
+ */
+static int
+gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
+{
+ TIFF* tif = img->tif;
+ tileSeparateRoutine put = img->put.separate;
+ unsigned char *buf = NULL;
+ unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
+ uint32 row, y, nrow, rowstoread;
+ tmsize_t pos;
+ tmsize_t scanline;
+ uint32 rowsperstrip, offset_row;
+ uint32 imagewidth = img->width;
+ tmsize_t stripsize;
+ tmsize_t bufsize;
+ int32 fromskew, toskew;
+ int alpha = img->alpha;
+ int ret = 1, flip;
+ uint16 colorchannels;
+
+ stripsize = TIFFStripSize(tif);
+ bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
+ if (bufsize == 0) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
+ return (0);
+ }
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(w + w);
+ }
+ else {
+ y = 0;
+ toskew = -(int32)(w - w);
+ }
+
+ switch( img->photometric )
+ {
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ case PHOTOMETRIC_PALETTE:
+ colorchannels = 1;
+ break;
+
+ default:
+ colorchannels = 3;
+ break;
+ }
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+ scanline = TIFFScanlineSize(tif);
+ fromskew = (w < imagewidth ? imagewidth - w : 0);
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ offset_row = row + img->row_offset;
+ if( buf == NULL )
+ {
+ if (_TIFFReadEncodedStripAndAllocBuffer(
+ tif, TIFFComputeStrip(tif, offset_row, 0),
+ (void**) &buf, bufsize,
+ ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
+ && (buf == NULL || img->stoponerr))
+ {
+ ret = 0;
+ break;
+ }
+ p0 = buf;
+ if( colorchannels == 1 )
+ {
+ p2 = p1 = p0;
+ pa = (alpha?(p0+3*stripsize):NULL);
+ }
+ else
+ {
+ p1 = p0 + stripsize;
+ p2 = p1 + stripsize;
+ pa = (alpha?(p2+stripsize):NULL);
+ }
+ }
+ else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
+ p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (colorchannels > 1
+ && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
+ p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (colorchannels > 1
+ && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
+ p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (alpha)
+ {
+ if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
+ pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ }
+
+ pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
+ ((tmsize_t) img->col_offset * img->samplesperpixel);
+ (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
+ p2 + pos, (alpha?(pa+pos):NULL));
+ y += ((flip & FLIP_VERTICALLY) ? -(int32) nrow : (int32) nrow);
+ }
+
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
+
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
+
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++;
+ right--;
+ }
+ }
+ }
+
+ _TIFFfree(buf);
+ return (ret);
+}
+
+/*
+ * The following routines move decoded data returned
+ * from the TIFF library into rasters filled with packed
+ * ABGR pixels (i.e. suitable for passing to lrecwrite.)
+ *
+ * The routines have been created according to the most
+ * important cases and optimized. PickContigCase and
+ * PickSeparateCase analyze the parameters and select
+ * the appropriate "get" and "put" routine to use.
+ */
+#define REPEAT8(op) REPEAT4(op); REPEAT4(op)
+#define REPEAT4(op) REPEAT2(op); REPEAT2(op)
+#define REPEAT2(op) op; op
+#define CASE8(x,op) \
+ switch (x) { \
+ case 7: op; /*-fallthrough*/ \
+ case 6: op; /*-fallthrough*/ \
+ case 5: op; /*-fallthrough*/ \
+ case 4: op; /*-fallthrough*/ \
+ case 3: op; /*-fallthrough*/ \
+ case 2: op; /*-fallthrough*/ \
+ case 1: op; \
+ }
+#define CASE4(x,op) switch (x) { case 3: op; /*-fallthrough*/ case 2: op; /*-fallthrough*/ case 1: op; }
+#define NOP
+
+#define UNROLL8(w, op1, op2) { \
+ uint32 _x; \
+ for (_x = w; _x >= 8; _x -= 8) { \
+ op1; \
+ REPEAT8(op2); \
+ } \
+ if (_x > 0) { \
+ op1; \
+ CASE8(_x,op2); \
+ } \
+}
+#define UNROLL4(w, op1, op2) { \
+ uint32 _x; \
+ for (_x = w; _x >= 4; _x -= 4) { \
+ op1; \
+ REPEAT4(op2); \
+ } \
+ if (_x > 0) { \
+ op1; \
+ CASE4(_x,op2); \
+ } \
+}
+#define UNROLL2(w, op1, op2) { \
+ uint32 _x; \
+ for (_x = w; _x >= 2; _x -= 2) { \
+ op1; \
+ REPEAT2(op2); \
+ } \
+ if (_x) { \
+ op1; \
+ op2; \
+ } \
+}
+
+#define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
+#define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
+
+#define A1 (((uint32)0xffL)<<24)
+#define PACK(r,g,b) \
+ ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
+#define PACK4(r,g,b,a) \
+ ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
+#define W2B(v) (((v)>>8)&0xff)
+/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
+#define PACKW(r,g,b) \
+ ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
+#define PACKW4(r,g,b,a) \
+ ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
+
+#define DECLAREContigPutFunc(name) \
+static void name(\
+ TIFFRGBAImage* img, \
+ uint32* cp, \
+ uint32 x, uint32 y, \
+ uint32 w, uint32 h, \
+ int32 fromskew, int32 toskew, \
+ unsigned char* pp \
+)
+
+/*
+ * 8-bit palette => colormap/RGB
+ */
+DECLAREContigPutFunc(put8bitcmaptile)
+{
+ uint32** PALmap = img->PALmap;
+ int samplesperpixel = img->samplesperpixel;
+
+ (void) y;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x)
+ {
+ *cp++ = PALmap[*pp][0];
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 4-bit palette => colormap/RGB
+ */
+DECLAREContigPutFunc(put4bitcmaptile)
+{
+ uint32** PALmap = img->PALmap;
+
+ (void) x; (void) y;
+ fromskew /= 2;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 2-bit palette => colormap/RGB
+ */
+DECLAREContigPutFunc(put2bitcmaptile)
+{
+ uint32** PALmap = img->PALmap;
+
+ (void) x; (void) y;
+ fromskew /= 4;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 1-bit palette => colormap/RGB
+ */
+DECLAREContigPutFunc(put1bitcmaptile)
+{
+ uint32** PALmap = img->PALmap;
+
+ (void) x; (void) y;
+ fromskew /= 8;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit greyscale => colormap/RGB
+ */
+DECLAREContigPutFunc(putgreytile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint32** BWmap = img->BWmap;
+
+ (void) y;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x)
+ {
+ *cp++ = BWmap[*pp][0];
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit greyscale with associated alpha => colormap/RGBA
+ */
+DECLAREContigPutFunc(putagreytile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint32** BWmap = img->BWmap;
+
+ (void) y;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x)
+ {
+ *cp++ = BWmap[*pp][0] & ((uint32)*(pp+1) << 24 | ~A1);
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 16-bit greyscale => colormap/RGB
+ */
+DECLAREContigPutFunc(put16bitbwtile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint32** BWmap = img->BWmap;
+
+ (void) y;
+ for( ; h > 0; --h) {
+ uint16 *wp = (uint16 *) pp;
+
+ for (x = w; x > 0; --x)
+ {
+ /* use high order byte of 16bit value */
+
+ *cp++ = BWmap[*wp >> 8][0];
+ pp += 2 * samplesperpixel;
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 1-bit bilevel => colormap/RGB
+ */
+DECLAREContigPutFunc(put1bitbwtile)
+{
+ uint32** BWmap = img->BWmap;
+
+ (void) x; (void) y;
+ fromskew /= 8;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 2-bit greyscale => colormap/RGB
+ */
+DECLAREContigPutFunc(put2bitbwtile)
+{
+ uint32** BWmap = img->BWmap;
+
+ (void) x; (void) y;
+ fromskew /= 4;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 4-bit greyscale => colormap/RGB
+ */
+DECLAREContigPutFunc(put4bitbwtile)
+{
+ uint32** BWmap = img->BWmap;
+
+ (void) x; (void) y;
+ fromskew /= 2;
+ for( ; h > 0; --h) {
+ uint32* bw;
+ UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit packed samples, no Map => RGB
+ */
+DECLAREContigPutFunc(putRGBcontig8bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+
+ (void) x; (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ UNROLL8(w, NOP,
+ *cp++ = PACK(pp[0], pp[1], pp[2]);
+ pp += samplesperpixel);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit packed samples => RGBA w/ associated alpha
+ * (known to have Map == NULL)
+ */
+DECLAREContigPutFunc(putRGBAAcontig8bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+
+ (void) x; (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ UNROLL8(w, NOP,
+ *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
+ pp += samplesperpixel);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit packed samples => RGBA w/ unassociated alpha
+ * (known to have Map == NULL)
+ */
+DECLAREContigPutFunc(putRGBUAcontig8bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ uint32 r, g, b, a;
+ uint8* m;
+ for (x = w; x > 0; --x) {
+ a = pp[3];
+ m = img->UaToAa+((size_t) a<<8);
+ r = m[pp[0]];
+ g = m[pp[1]];
+ b = m[pp[2]];
+ *cp++ = PACK4(r,g,b,a);
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 16-bit packed samples => RGB
+ */
+DECLAREContigPutFunc(putRGBcontig16bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x) {
+ *cp++ = PACK(img->Bitdepth16To8[wp[0]],
+ img->Bitdepth16To8[wp[1]],
+ img->Bitdepth16To8[wp[2]]);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
+ }
+}
+
+/*
+ * 16-bit packed samples => RGBA w/ associated alpha
+ * (known to have Map == NULL)
+ */
+DECLAREContigPutFunc(putRGBAAcontig16bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x) {
+ *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
+ img->Bitdepth16To8[wp[1]],
+ img->Bitdepth16To8[wp[2]],
+ img->Bitdepth16To8[wp[3]]);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
+ }
+}
+
+/*
+ * 16-bit packed samples => RGBA w/ unassociated alpha
+ * (known to have Map == NULL)
+ */
+DECLAREContigPutFunc(putRGBUAcontig16bittile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ uint32 r,g,b,a;
+ uint8* m;
+ for (x = w; x > 0; --x) {
+ a = img->Bitdepth16To8[wp[3]];
+ m = img->UaToAa+((size_t) a<<8);
+ r = m[img->Bitdepth16To8[wp[0]]];
+ g = m[img->Bitdepth16To8[wp[1]]];
+ b = m[img->Bitdepth16To8[wp[2]]];
+ *cp++ = PACK4(r,g,b,a);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
+ }
+}
+
+/*
+ * 8-bit packed CMYK samples w/o Map => RGB
+ *
+ * NB: The conversion of CMYK->RGB is *very* crude.
+ */
+DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ uint16 r, g, b, k;
+
+ (void) x; (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ UNROLL8(w, NOP,
+ k = 255 - pp[3];
+ r = (k*(255-pp[0]))/255;
+ g = (k*(255-pp[1]))/255;
+ b = (k*(255-pp[2]))/255;
+ *cp++ = PACK(r, g, b);
+ pp += samplesperpixel);
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * 8-bit packed CMYK samples w/Map => RGB
+ *
+ * NB: The conversion of CMYK->RGB is *very* crude.
+ */
+DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
+{
+ int samplesperpixel = img->samplesperpixel;
+ TIFFRGBValue* Map = img->Map;
+ uint16 r, g, b, k;
+
+ (void) y;
+ fromskew *= samplesperpixel;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x) {
+ k = 255 - pp[3];
+ r = (k*(255-pp[0]))/255;
+ g = (k*(255-pp[1]))/255;
+ b = (k*(255-pp[2]))/255;
+ *cp++ = PACK(Map[r], Map[g], Map[b]);
+ pp += samplesperpixel;
+ }
+ pp += fromskew;
+ cp += toskew;
+ }
+}
+
+#define DECLARESepPutFunc(name) \
+static void name(\
+ TIFFRGBAImage* img,\
+ uint32* cp,\
+ uint32 x, uint32 y, \
+ uint32 w, uint32 h,\
+ int32 fromskew, int32 toskew,\
+ unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
+)
+
+/*
+ * 8-bit unpacked samples => RGB
+ */
+DECLARESepPutFunc(putRGBseparate8bittile)
+{
+ (void) img; (void) x; (void) y; (void) a;
+ for( ; h > 0; --h) {
+ UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
+ SKEW(r, g, b, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 8-bit unpacked samples => RGBA w/ associated alpha
+ */
+DECLARESepPutFunc(putRGBAAseparate8bittile)
+{
+ (void) img; (void) x; (void) y;
+ for( ; h > 0; --h) {
+ UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
+ SKEW4(r, g, b, a, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 8-bit unpacked CMYK samples => RGBA
+ */
+DECLARESepPutFunc(putCMYKseparate8bittile)
+{
+ (void) img; (void) y;
+ for( ; h > 0; --h) {
+ uint32 rv, gv, bv, kv;
+ for (x = w; x > 0; --x) {
+ kv = 255 - *a++;
+ rv = (kv*(255-*r++))/255;
+ gv = (kv*(255-*g++))/255;
+ bv = (kv*(255-*b++))/255;
+ *cp++ = PACK4(rv,gv,bv,255);
+ }
+ SKEW4(r, g, b, a, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 8-bit unpacked samples => RGBA w/ unassociated alpha
+ */
+DECLARESepPutFunc(putRGBUAseparate8bittile)
+{
+ (void) img; (void) y;
+ for( ; h > 0; --h) {
+ uint32 rv, gv, bv, av;
+ uint8* m;
+ for (x = w; x > 0; --x) {
+ av = *a++;
+ m = img->UaToAa+((size_t) av<<8);
+ rv = m[*r++];
+ gv = m[*g++];
+ bv = m[*b++];
+ *cp++ = PACK4(rv,gv,bv,av);
+ }
+ SKEW4(r, g, b, a, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 16-bit unpacked samples => RGB
+ */
+DECLARESepPutFunc(putRGBseparate16bittile)
+{
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ (void) img; (void) y; (void) a;
+ for( ; h > 0; --h) {
+ for (x = 0; x < w; x++)
+ *cp++ = PACK(img->Bitdepth16To8[*wr++],
+ img->Bitdepth16To8[*wg++],
+ img->Bitdepth16To8[*wb++]);
+ SKEW(wr, wg, wb, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 16-bit unpacked samples => RGBA w/ associated alpha
+ */
+DECLARESepPutFunc(putRGBAAseparate16bittile)
+{
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ uint16 *wa = (uint16*) a;
+ (void) img; (void) y;
+ for( ; h > 0; --h) {
+ for (x = 0; x < w; x++)
+ *cp++ = PACK4(img->Bitdepth16To8[*wr++],
+ img->Bitdepth16To8[*wg++],
+ img->Bitdepth16To8[*wb++],
+ img->Bitdepth16To8[*wa++]);
+ SKEW4(wr, wg, wb, wa, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 16-bit unpacked samples => RGBA w/ unassociated alpha
+ */
+DECLARESepPutFunc(putRGBUAseparate16bittile)
+{
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ uint16 *wa = (uint16*) a;
+ (void) img; (void) y;
+ for( ; h > 0; --h) {
+ uint32 r2,g2,b2,a2;
+ uint8* m;
+ for (x = w; x > 0; --x) {
+ a2 = img->Bitdepth16To8[*wa++];
+ m = img->UaToAa+((size_t) a2<<8);
+ r2 = m[img->Bitdepth16To8[*wr++]];
+ g2 = m[img->Bitdepth16To8[*wg++]];
+ b2 = m[img->Bitdepth16To8[*wb++]];
+ *cp++ = PACK4(r2,g2,b2,a2);
+ }
+ SKEW4(wr, wg, wb, wa, fromskew);
+ cp += toskew;
+ }
+}
+
+/*
+ * 8-bit packed CIE L*a*b 1976 samples => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitCIELab)
+{
+ float X, Y, Z;
+ uint32 r, g, b;
+ (void) y;
+ fromskew *= 3;
+ for( ; h > 0; --h) {
+ for (x = w; x > 0; --x) {
+ TIFFCIELabToXYZ(img->cielab,
+ (unsigned char)pp[0],
+ (signed char)pp[1],
+ (signed char)pp[2],
+ &X, &Y, &Z);
+ TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
+ *cp++ = PACK(r, g, b);
+ pp += 3;
+ }
+ cp += toskew;
+ pp += fromskew;
+ }
+}
+
+/*
+ * YCbCr -> RGB conversion and packing routines.
+ */
+
+#define YCbCrtoRGB(dst, Y) { \
+ uint32 r, g, b; \
+ TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
+ dst = PACK(r, g, b); \
+}
+
+/*
+ * 8-bit packed YCbCr samples => RGB
+ * This function is generic for different sampling sizes,
+ * and can handle blocks sizes that aren't multiples of the
+ * sampling size. However, it is substantially less optimized
+ * than the specific sampling cases. It is used as a fallback
+ * for difficult blocks.
+ */
+#ifdef notdef
+static void putcontig8bitYCbCrGenericTile(
+ TIFFRGBAImage* img,
+ uint32* cp,
+ uint32 x, uint32 y,
+ uint32 w, uint32 h,
+ int32 fromskew, int32 toskew,
+ unsigned char* pp,
+ int h_group,
+ int v_group )
+
+{
+ uint32* cp1 = cp+w+toskew;
+ uint32* cp2 = cp1+w+toskew;
+ uint32* cp3 = cp2+w+toskew;
+ int32 incr = 3*w+4*toskew;
+ int32 Cb, Cr;
+ int group_size = v_group * h_group + 2;
+
+ (void) y;
+ fromskew = (fromskew * group_size) / h_group;
+
+ for( yy = 0; yy < h; yy++ )
+ {
+ unsigned char *pp_line;
+ int y_line_group = yy / v_group;
+ int y_remainder = yy - y_line_group * v_group;
+
+ pp_line = pp + v_line_group *
+
+
+ for( xx = 0; xx < w; xx++ )
+ {
+ Cb = pp
+ }
+ }
+ for (; h >= 4; h -= 4) {
+ x = w>>2;
+ do {
+ Cb = pp[16];
+ Cr = pp[17];
+
+ YCbCrtoRGB(cp [0], pp[ 0]);
+ YCbCrtoRGB(cp [1], pp[ 1]);
+ YCbCrtoRGB(cp [2], pp[ 2]);
+ YCbCrtoRGB(cp [3], pp[ 3]);
+ YCbCrtoRGB(cp1[0], pp[ 4]);
+ YCbCrtoRGB(cp1[1], pp[ 5]);
+ YCbCrtoRGB(cp1[2], pp[ 6]);
+ YCbCrtoRGB(cp1[3], pp[ 7]);
+ YCbCrtoRGB(cp2[0], pp[ 8]);
+ YCbCrtoRGB(cp2[1], pp[ 9]);
+ YCbCrtoRGB(cp2[2], pp[10]);
+ YCbCrtoRGB(cp2[3], pp[11]);
+ YCbCrtoRGB(cp3[0], pp[12]);
+ YCbCrtoRGB(cp3[1], pp[13]);
+ YCbCrtoRGB(cp3[2], pp[14]);
+ YCbCrtoRGB(cp3[3], pp[15]);
+
+ cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
+ pp += 18;
+ } while (--x);
+ cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
+ pp += fromskew;
+ }
+}
+#endif
+
+/*
+ * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
+{
+ uint32* cp1 = cp+w+toskew;
+ uint32* cp2 = cp1+w+toskew;
+ uint32* cp3 = cp2+w+toskew;
+ int32 incr = 3*w+4*toskew;
+
+ (void) y;
+ /* adjust fromskew */
+ fromskew = (fromskew / 4) * (4*2+2);
+ if ((h & 3) == 0 && (w & 3) == 0) {
+ for (; h >= 4; h -= 4) {
+ x = w>>2;
+ do {
+ int32 Cb = pp[16];
+ int32 Cr = pp[17];
+
+ YCbCrtoRGB(cp [0], pp[ 0]);
+ YCbCrtoRGB(cp [1], pp[ 1]);
+ YCbCrtoRGB(cp [2], pp[ 2]);
+ YCbCrtoRGB(cp [3], pp[ 3]);
+ YCbCrtoRGB(cp1[0], pp[ 4]);
+ YCbCrtoRGB(cp1[1], pp[ 5]);
+ YCbCrtoRGB(cp1[2], pp[ 6]);
+ YCbCrtoRGB(cp1[3], pp[ 7]);
+ YCbCrtoRGB(cp2[0], pp[ 8]);
+ YCbCrtoRGB(cp2[1], pp[ 9]);
+ YCbCrtoRGB(cp2[2], pp[10]);
+ YCbCrtoRGB(cp2[3], pp[11]);
+ YCbCrtoRGB(cp3[0], pp[12]);
+ YCbCrtoRGB(cp3[1], pp[13]);
+ YCbCrtoRGB(cp3[2], pp[14]);
+ YCbCrtoRGB(cp3[3], pp[15]);
+
+ cp += 4;
+ cp1 += 4;
+ cp2 += 4;
+ cp3 += 4;
+ pp += 18;
+ } while (--x);
+ cp += incr;
+ cp1 += incr;
+ cp2 += incr;
+ cp3 += incr;
+ pp += fromskew;
+ }
+ } else {
+ while (h > 0) {
+ for (x = w; x > 0;) {
+ int32 Cb = pp[16];
+ int32 Cr = pp[17];
+ switch (x) {
+ default:
+ switch (h) {
+ default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
+ case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
+ case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 3:
+ switch (h) {
+ default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
+ case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
+ case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 2:
+ switch (h) {
+ default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
+ case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
+ case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 1:
+ switch (h) {
+ default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
+ case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
+ case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ }
+ if (x < 4) {
+ cp += x; cp1 += x; cp2 += x; cp3 += x;
+ x = 0;
+ }
+ else {
+ cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
+ x -= 4;
+ }
+ pp += 18;
+ }
+ if (h <= 4)
+ break;
+ h -= 4;
+ cp += incr;
+ cp1 += incr;
+ cp2 += incr;
+ cp3 += incr;
+ pp += fromskew;
+ }
+ }
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
+{
+ uint32* cp1 = cp+w+toskew;
+ int32 incr = 2*toskew+w;
+
+ (void) y;
+ fromskew = (fromskew / 4) * (4*2+2);
+ if ((w & 3) == 0 && (h & 1) == 0) {
+ for (; h >= 2; h -= 2) {
+ x = w>>2;
+ do {
+ int32 Cb = pp[8];
+ int32 Cr = pp[9];
+
+ YCbCrtoRGB(cp [0], pp[0]);
+ YCbCrtoRGB(cp [1], pp[1]);
+ YCbCrtoRGB(cp [2], pp[2]);
+ YCbCrtoRGB(cp [3], pp[3]);
+ YCbCrtoRGB(cp1[0], pp[4]);
+ YCbCrtoRGB(cp1[1], pp[5]);
+ YCbCrtoRGB(cp1[2], pp[6]);
+ YCbCrtoRGB(cp1[3], pp[7]);
+
+ cp += 4;
+ cp1 += 4;
+ pp += 10;
+ } while (--x);
+ cp += incr;
+ cp1 += incr;
+ pp += fromskew;
+ }
+ } else {
+ while (h > 0) {
+ for (x = w; x > 0;) {
+ int32 Cb = pp[8];
+ int32 Cr = pp[9];
+ switch (x) {
+ default:
+ switch (h) {
+ default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 3:
+ switch (h) {
+ default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 2:
+ switch (h) {
+ default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ case 1:
+ switch (h) {
+ default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
+ case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
+ } /* FALLTHROUGH */
+ }
+ if (x < 4) {
+ cp += x; cp1 += x;
+ x = 0;
+ }
+ else {
+ cp += 4; cp1 += 4;
+ x -= 4;
+ }
+ pp += 10;
+ }
+ if (h <= 2)
+ break;
+ h -= 2;
+ cp += incr;
+ cp1 += incr;
+ pp += fromskew;
+ }
+ }
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
+{
+ (void) y;
+ fromskew = (fromskew / 4) * (4*1+2);
+ do {
+ x = w>>2;
+ while(x>0) {
+ int32 Cb = pp[4];
+ int32 Cr = pp[5];
+
+ YCbCrtoRGB(cp [0], pp[0]);
+ YCbCrtoRGB(cp [1], pp[1]);
+ YCbCrtoRGB(cp [2], pp[2]);
+ YCbCrtoRGB(cp [3], pp[3]);
+
+ cp += 4;
+ pp += 6;
+ x--;
+ }
+
+ if( (w&3) != 0 )
+ {
+ int32 Cb = pp[4];
+ int32 Cr = pp[5];
+
+ switch( (w&3) ) {
+ case 3: YCbCrtoRGB(cp [2], pp[2]); /*-fallthrough*/
+ case 2: YCbCrtoRGB(cp [1], pp[1]); /*-fallthrough*/
+ case 1: YCbCrtoRGB(cp [0], pp[0]); /*-fallthrough*/
+ case 0: break;
+ }
+
+ cp += (w&3);
+ pp += 6;
+ }
+
+ cp += toskew;
+ pp += fromskew;
+ } while (--h);
+
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
+{
+ uint32* cp2;
+ int32 incr = 2*toskew+w;
+ (void) y;
+ fromskew = (fromskew / 2) * (2*2+2);
+ cp2 = cp+w+toskew;
+ while (h>=2) {
+ x = w;
+ while (x>=2) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
+ YCbCrtoRGB(cp2[0], pp[2]);
+ YCbCrtoRGB(cp2[1], pp[3]);
+ cp += 2;
+ cp2 += 2;
+ pp += 6;
+ x -= 2;
+ }
+ if (x==1) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp2[0], pp[2]);
+ cp ++ ;
+ cp2 ++ ;
+ pp += 6;
+ }
+ cp += incr;
+ cp2 += incr;
+ pp += fromskew;
+ h-=2;
+ }
+ if (h==1) {
+ x = w;
+ while (x>=2) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
+ cp += 2;
+ cp2 += 2;
+ pp += 6;
+ x -= 2;
+ }
+ if (x==1) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ }
+ }
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
+{
+ (void) y;
+ fromskew = (fromskew / 2) * (2*1+2);
+ do {
+ x = w>>1;
+ while(x>0) {
+ int32 Cb = pp[2];
+ int32 Cr = pp[3];
+
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
+
+ cp += 2;
+ pp += 4;
+ x --;
+ }
+
+ if( (w&1) != 0 )
+ {
+ int32 Cb = pp[2];
+ int32 Cr = pp[3];
+
+ YCbCrtoRGB(cp[0], pp[0]);
+
+ cp += 1;
+ pp += 4;
+ }
+
+ cp += toskew;
+ pp += fromskew;
+ } while (--h);
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
+{
+ uint32* cp2;
+ int32 incr = 2*toskew+w;
+ (void) y;
+ fromskew = (fromskew / 1) * (1 * 2 + 2);
+ cp2 = cp+w+toskew;
+ while (h>=2) {
+ x = w;
+ do {
+ uint32 Cb = pp[2];
+ uint32 Cr = pp[3];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp2[0], pp[1]);
+ cp ++;
+ cp2 ++;
+ pp += 4;
+ } while (--x);
+ cp += incr;
+ cp2 += incr;
+ pp += fromskew;
+ h-=2;
+ }
+ if (h==1) {
+ x = w;
+ do {
+ uint32 Cb = pp[2];
+ uint32 Cr = pp[3];
+ YCbCrtoRGB(cp[0], pp[0]);
+ cp ++;
+ pp += 4;
+ } while (--x);
+ }
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ no subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
+{
+ (void) y;
+ fromskew = (fromskew / 1) * (1 * 1 + 2);
+ do {
+ x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
+ do {
+ int32 Cb = pp[1];
+ int32 Cr = pp[2];
+
+ YCbCrtoRGB(*cp++, pp[0]);
+
+ pp += 3;
+ } while (--x);
+ cp += toskew;
+ pp += fromskew;
+ } while (--h);
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ no subsampling => RGB
+ */
+DECLARESepPutFunc(putseparate8bitYCbCr11tile)
+{
+ (void) y;
+ (void) a;
+ /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
+ for( ; h > 0; --h) {
+ x = w;
+ do {
+ uint32 dr, dg, db;
+ TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
+ *cp++ = PACK(dr,dg,db);
+ } while (--x);
+ SKEW(r, g, b, fromskew);
+ cp += toskew;
+ }
+}
+#undef YCbCrtoRGB
+
+static int isInRefBlackWhiteRange(float f)
+{
+ return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
+}
+
+static int
+initYCbCrConversion(TIFFRGBAImage* img)
+{
+ static const char module[] = "initYCbCrConversion";
+
+ float *luma, *refBlackWhite;
+
+ if (img->ycbcr == NULL) {
+ img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
+ TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))
+ + 4*256*sizeof (TIFFRGBValue)
+ + 2*256*sizeof (int)
+ + 3*256*sizeof (int32)
+ );
+ if (img->ycbcr == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "No space for YCbCr->RGB conversion state");
+ return (0);
+ }
+ }
+
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
+ &refBlackWhite);
+
+ /* Do some validation to avoid later issues. Detect NaN for now */
+ /* and also if lumaGreen is zero since we divide by it later */
+ if( luma[0] != luma[0] ||
+ luma[1] != luma[1] ||
+ luma[1] == 0.0 ||
+ luma[2] != luma[2] )
+ {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "Invalid values for YCbCrCoefficients tag");
+ return (0);
+ }
+
+ if( !isInRefBlackWhiteRange(refBlackWhite[0]) ||
+ !isInRefBlackWhiteRange(refBlackWhite[1]) ||
+ !isInRefBlackWhiteRange(refBlackWhite[2]) ||
+ !isInRefBlackWhiteRange(refBlackWhite[3]) ||
+ !isInRefBlackWhiteRange(refBlackWhite[4]) ||
+ !isInRefBlackWhiteRange(refBlackWhite[5]) )
+ {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "Invalid values for ReferenceBlackWhite tag");
+ return (0);
+ }
+
+ if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
+ return(0);
+ return (1);
+}
+
+static tileContigRoutine
+initCIELabConversion(TIFFRGBAImage* img)
+{
+ static const char module[] = "initCIELabConversion";
+
+ float *whitePoint;
+ float refWhite[3];
+
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
+ if (whitePoint[1] == 0.0f ) {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "Invalid value for WhitePoint tag.");
+ return NULL;
+ }
+
+ if (!img->cielab) {
+ img->cielab = (TIFFCIELabToRGB *)
+ _TIFFmalloc(sizeof(TIFFCIELabToRGB));
+ if (!img->cielab) {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "No space for CIE L*a*b*->RGB conversion state.");
+ return NULL;
+ }
+ }
+
+ refWhite[1] = 100.0F;
+ refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
+ refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
+ / whitePoint[1] * refWhite[1];
+ if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
+ TIFFErrorExt(img->tif->tif_clientdata, module,
+ "Failed to initialize CIE L*a*b*->RGB conversion state.");
+ _TIFFfree(img->cielab);
+ return NULL;
+ }
+
+ return putcontig8bitCIELab;
+}
+
+/*
+ * Greyscale images with less than 8 bits/sample are handled
+ * with a table to avoid lots of shifts and masks. The table
+ * is setup so that put*bwtile (below) can retrieve 8/bitspersample
+ * pixel values simply by indexing into the table with one
+ * number.
+ */
+static int
+makebwmap(TIFFRGBAImage* img)
+{
+ TIFFRGBValue* Map = img->Map;
+ int bitspersample = img->bitspersample;
+ int nsamples = 8 / bitspersample;
+ int i;
+ uint32* p;
+
+ if( nsamples == 0 )
+ nsamples = 1;
+
+ img->BWmap = (uint32**) _TIFFmalloc(
+ 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
+ if (img->BWmap == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
+ return (0);
+ }
+ p = (uint32*)(img->BWmap + 256);
+ for (i = 0; i < 256; i++) {
+ TIFFRGBValue c;
+ img->BWmap[i] = p;
+ switch (bitspersample) {
+#define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
+ case 1:
+ GREY(i>>7);
+ GREY((i>>6)&1);
+ GREY((i>>5)&1);
+ GREY((i>>4)&1);
+ GREY((i>>3)&1);
+ GREY((i>>2)&1);
+ GREY((i>>1)&1);
+ GREY(i&1);
+ break;
+ case 2:
+ GREY(i>>6);
+ GREY((i>>4)&3);
+ GREY((i>>2)&3);
+ GREY(i&3);
+ break;
+ case 4:
+ GREY(i>>4);
+ GREY(i&0xf);
+ break;
+ case 8:
+ case 16:
+ GREY(i);
+ break;
+ }
+#undef GREY
+ }
+ return (1);
+}
+
+/*
+ * Construct a mapping table to convert from the range
+ * of the data samples to [0,255] --for display. This
+ * process also handles inverting B&W images when needed.
+ */
+static int
+setupMap(TIFFRGBAImage* img)
+{
+ int32 x, range;
+
+ range = (int32)((1L<<img->bitspersample)-1);
+
+ /* treat 16 bit the same as eight bit */
+ if( img->bitspersample == 16 )
+ range = (int32) 255;
+
+ img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
+ if (img->Map == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
+ "No space for photometric conversion table");
+ return (0);
+ }
+ if (img->photometric == PHOTOMETRIC_MINISWHITE) {
+ for (x = 0; x <= range; x++)
+ img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
+ } else {
+ for (x = 0; x <= range; x++)
+ img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
+ }
+ if (img->bitspersample <= 16 &&
+ (img->photometric == PHOTOMETRIC_MINISBLACK ||
+ img->photometric == PHOTOMETRIC_MINISWHITE)) {
+ /*
+ * Use photometric mapping table to construct
+ * unpacking tables for samples <= 8 bits.
+ */
+ if (!makebwmap(img))
+ return (0);
+ /* no longer need Map, free it */
+ _TIFFfree(img->Map);
+ img->Map = NULL;
+ }
+ return (1);
+}
+
+static int
+checkcmap(TIFFRGBAImage* img)
+{
+ uint16* r = img->redcmap;
+ uint16* g = img->greencmap;
+ uint16* b = img->bluecmap;
+ long n = 1L<<img->bitspersample;
+
+ while (n-- > 0)
+ if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
+ return (16);
+ return (8);
+}
+
+static void
+cvtcmap(TIFFRGBAImage* img)
+{
+ uint16* r = img->redcmap;
+ uint16* g = img->greencmap;
+ uint16* b = img->bluecmap;
+ long i;
+
+ for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
+#define CVT(x) ((uint16)((x)>>8))
+ r[i] = CVT(r[i]);
+ g[i] = CVT(g[i]);
+ b[i] = CVT(b[i]);
+#undef CVT
+ }
+}
+
+/*
+ * Palette images with <= 8 bits/sample are handled
+ * with a table to avoid lots of shifts and masks. The table
+ * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
+ * pixel values simply by indexing into the table with one
+ * number.
+ */
+static int
+makecmap(TIFFRGBAImage* img)
+{
+ int bitspersample = img->bitspersample;
+ int nsamples = 8 / bitspersample;
+ uint16* r = img->redcmap;
+ uint16* g = img->greencmap;
+ uint16* b = img->bluecmap;
+ uint32 *p;
+ int i;
+
+ img->PALmap = (uint32**) _TIFFmalloc(
+ 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
+ if (img->PALmap == NULL) {
+ TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
+ return (0);
+ }
+ p = (uint32*)(img->PALmap + 256);
+ for (i = 0; i < 256; i++) {
+ TIFFRGBValue c;
+ img->PALmap[i] = p;
+#define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
+ switch (bitspersample) {
+ case 1:
+ CMAP(i>>7);
+ CMAP((i>>6)&1);
+ CMAP((i>>5)&1);
+ CMAP((i>>4)&1);
+ CMAP((i>>3)&1);
+ CMAP((i>>2)&1);
+ CMAP((i>>1)&1);
+ CMAP(i&1);
+ break;
+ case 2:
+ CMAP(i>>6);
+ CMAP((i>>4)&3);
+ CMAP((i>>2)&3);
+ CMAP(i&3);
+ break;
+ case 4:
+ CMAP(i>>4);
+ CMAP(i&0xf);
+ break;
+ case 8:
+ CMAP(i);
+ break;
+ }
+#undef CMAP
+ }
+ return (1);
+}
+
+/*
+ * Construct any mapping table used
+ * by the associated put routine.
+ */
+static int
+buildMap(TIFFRGBAImage* img)
+{
+ switch (img->photometric) {
+ case PHOTOMETRIC_RGB:
+ case PHOTOMETRIC_YCBCR:
+ case PHOTOMETRIC_SEPARATED:
+ if (img->bitspersample == 8)
+ break;
+ /* fall through... */
+ case PHOTOMETRIC_MINISBLACK:
+ case PHOTOMETRIC_MINISWHITE:
+ if (!setupMap(img))
+ return (0);
+ break;
+ case PHOTOMETRIC_PALETTE:
+ /*
+ * Convert 16-bit colormap to 8-bit (unless it looks
+ * like an old-style 8-bit colormap).
+ */
+ if (checkcmap(img) == 16)
+ cvtcmap(img);
+ else
+ TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
+ /*
+ * Use mapping table and colormap to construct
+ * unpacking tables for samples < 8 bits.
+ */
+ if (img->bitspersample <= 8 && !makecmap(img))
+ return (0);
+ break;
+ }
+ return (1);
+}
+
+/*
+ * Select the appropriate conversion routine for packed data.
+ */
+static int
+PickContigCase(TIFFRGBAImage* img)
+{
+ img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
+ img->put.contig = NULL;
+ switch (img->photometric) {
+ case PHOTOMETRIC_RGB:
+ switch (img->bitspersample) {
+ case 8:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
+ img->samplesperpixel >= 4)
+ img->put.contig = putRGBAAcontig8bittile;
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
+ img->samplesperpixel >= 4)
+ {
+ if (BuildMapUaToAa(img))
+ img->put.contig = putRGBUAcontig8bittile;
+ }
+ else if( img->samplesperpixel >= 3 )
+ img->put.contig = putRGBcontig8bittile;
+ break;
+ case 16:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
+ img->samplesperpixel >=4 )
+ {
+ if (BuildMapBitdepth16To8(img))
+ img->put.contig = putRGBAAcontig16bittile;
+ }
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
+ img->samplesperpixel >=4 )
+ {
+ if (BuildMapBitdepth16To8(img) &&
+ BuildMapUaToAa(img))
+ img->put.contig = putRGBUAcontig16bittile;
+ }
+ else if( img->samplesperpixel >=3 )
+ {
+ if (BuildMapBitdepth16To8(img))
+ img->put.contig = putRGBcontig16bittile;
+ }
+ break;
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ if (img->samplesperpixel >=4 && buildMap(img)) {
+ if (img->bitspersample == 8) {
+ if (!img->Map)
+ img->put.contig = putRGBcontig8bitCMYKtile;
+ else
+ img->put.contig = putRGBcontig8bitCMYKMaptile;
+ }
+ }
+ break;
+ case PHOTOMETRIC_PALETTE:
+ if (buildMap(img)) {
+ switch (img->bitspersample) {
+ case 8:
+ img->put.contig = put8bitcmaptile;
+ break;
+ case 4:
+ img->put.contig = put4bitcmaptile;
+ break;
+ case 2:
+ img->put.contig = put2bitcmaptile;
+ break;
+ case 1:
+ img->put.contig = put1bitcmaptile;
+ break;
+ }
+ }
+ break;
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ if (buildMap(img)) {
+ switch (img->bitspersample) {
+ case 16:
+ img->put.contig = put16bitbwtile;
+ break;
+ case 8:
+ if (img->alpha && img->samplesperpixel == 2)
+ img->put.contig = putagreytile;
+ else
+ img->put.contig = putgreytile;
+ break;
+ case 4:
+ img->put.contig = put4bitbwtile;
+ break;
+ case 2:
+ img->put.contig = put2bitbwtile;
+ break;
+ case 1:
+ img->put.contig = put1bitbwtile;
+ break;
+ }
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ if ((img->bitspersample==8) && (img->samplesperpixel==3))
+ {
+ if (initYCbCrConversion(img)!=0)
+ {
+ /*
+ * The 6.0 spec says that subsampling must be
+ * one of 1, 2, or 4, and that vertical subsampling
+ * must always be <= horizontal subsampling; so
+ * there are only a few possibilities and we just
+ * enumerate the cases.
+ * Joris: added support for the [1,2] case, nonetheless, to accommodate
+ * some OJPEG files
+ */
+ uint16 SubsamplingHor;
+ uint16 SubsamplingVer;
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
+ switch ((SubsamplingHor<<4)|SubsamplingVer) {
+ case 0x44:
+ img->put.contig = putcontig8bitYCbCr44tile;
+ break;
+ case 0x42:
+ img->put.contig = putcontig8bitYCbCr42tile;
+ break;
+ case 0x41:
+ img->put.contig = putcontig8bitYCbCr41tile;
+ break;
+ case 0x22:
+ img->put.contig = putcontig8bitYCbCr22tile;
+ break;
+ case 0x21:
+ img->put.contig = putcontig8bitYCbCr21tile;
+ break;
+ case 0x12:
+ img->put.contig = putcontig8bitYCbCr12tile;
+ break;
+ case 0x11:
+ img->put.contig = putcontig8bitYCbCr11tile;
+ break;
+ }
+ }
+ }
+ break;
+ case PHOTOMETRIC_CIELAB:
+ if (img->samplesperpixel == 3 && buildMap(img)) {
+ if (img->bitspersample == 8)
+ img->put.contig = initCIELabConversion(img);
+ break;
+ }
+ }
+ return ((img->get!=NULL) && (img->put.contig!=NULL));
+}
+
+/*
+ * Select the appropriate conversion routine for unpacked data.
+ *
+ * NB: we assume that unpacked single channel data is directed
+ * to the "packed routines.
+ */
+static int
+PickSeparateCase(TIFFRGBAImage* img)
+{
+ img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
+ img->put.separate = NULL;
+ switch (img->photometric) {
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ /* greyscale images processed pretty much as RGB by gtTileSeparate */
+ case PHOTOMETRIC_RGB:
+ switch (img->bitspersample) {
+ case 8:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ img->put.separate = putRGBAAseparate8bittile;
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ if (BuildMapUaToAa(img))
+ img->put.separate = putRGBUAseparate8bittile;
+ }
+ else
+ img->put.separate = putRGBseparate8bittile;
+ break;
+ case 16:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ {
+ if (BuildMapBitdepth16To8(img))
+ img->put.separate = putRGBAAseparate16bittile;
+ }
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ if (BuildMapBitdepth16To8(img) &&
+ BuildMapUaToAa(img))
+ img->put.separate = putRGBUAseparate16bittile;
+ }
+ else
+ {
+ if (BuildMapBitdepth16To8(img))
+ img->put.separate = putRGBseparate16bittile;
+ }
+ break;
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ if (img->bitspersample == 8 && img->samplesperpixel == 4)
+ {
+ img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
+ img->put.separate = putCMYKseparate8bittile;
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ if ((img->bitspersample==8) && (img->samplesperpixel==3))
+ {
+ if (initYCbCrConversion(img)!=0)
+ {
+ uint16 hs, vs;
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
+ switch ((hs<<4)|vs) {
+ case 0x11:
+ img->put.separate = putseparate8bitYCbCr11tile;
+ break;
+ /* TODO: add other cases here */
+ }
+ }
+ }
+ break;
+ }
+ return ((img->get!=NULL) && (img->put.separate!=NULL));
+}
+
+static int
+BuildMapUaToAa(TIFFRGBAImage* img)
+{
+ static const char module[]="BuildMapUaToAa";
+ uint8* m;
+ uint16 na,nv;
+ assert(img->UaToAa==NULL);
+ img->UaToAa=_TIFFmalloc(65536);
+ if (img->UaToAa==NULL)
+ {
+ TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ m=img->UaToAa;
+ for (na=0; na<256; na++)
+ {
+ for (nv=0; nv<256; nv++)
+ *m++=(uint8)((nv*na+127)/255);
+ }
+ return(1);
+}
+
+static int
+BuildMapBitdepth16To8(TIFFRGBAImage* img)
+{
+ static const char module[]="BuildMapBitdepth16To8";
+ uint8* m;
+ uint32 n;
+ assert(img->Bitdepth16To8==NULL);
+ img->Bitdepth16To8=_TIFFmalloc(65536);
+ if (img->Bitdepth16To8==NULL)
+ {
+ TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ m=img->Bitdepth16To8;
+ for (n=0; n<65536; n++)
+ *m++=(uint8)((n+128)/257);
+ return(1);
+}
+
+
+/*
+ * Read a whole strip off data from the file, and convert to RGBA form.
+ * If this is the last strip, then it will only contain the portion of
+ * the strip that is actually within the image space. The result is
+ * organized in bottom to top form.
+ */
+
+
+int
+TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
+
+{
+ return TIFFReadRGBAStripExt(tif, row, raster, 0 );
+}
+
+int
+TIFFReadRGBAStripExt(TIFF* tif, uint32 row, uint32 * raster, int stop_on_error)
+
+{
+ char emsg[1024] = "";
+ TIFFRGBAImage img;
+ int ok;
+ uint32 rowsperstrip, rows_to_read;
+
+ if( TIFFIsTiled( tif ) )
+ {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+ "Can't use TIFFReadRGBAStrip() with tiled file.");
+ return (0);
+ }
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+ if( (row % rowsperstrip) != 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+ "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
+ return (0);
+ }
+
+ if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) {
+
+ img.row_offset = row;
+ img.col_offset = 0;
+
+ if( row + rowsperstrip > img.height )
+ rows_to_read = img.height - row;
+ else
+ rows_to_read = rowsperstrip;
+
+ ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
+
+ TIFFRGBAImageEnd(&img);
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
+ ok = 0;
+ }
+
+ return (ok);
+}
+
+/*
+ * Read a whole tile off data from the file, and convert to RGBA form.
+ * The returned RGBA data is organized from bottom to top of tile,
+ * and may include zeroed areas if the tile extends off the image.
+ */
+
+int
+TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
+
+{
+ return TIFFReadRGBATileExt(tif, col, row, raster, 0 );
+}
+
+
+int
+TIFFReadRGBATileExt(TIFF* tif, uint32 col, uint32 row, uint32 * raster, int stop_on_error )
+{
+ char emsg[1024] = "";
+ TIFFRGBAImage img;
+ int ok;
+ uint32 tile_xsize, tile_ysize;
+ uint32 read_xsize, read_ysize;
+ uint32 i_row;
+
+ /*
+ * Verify that our request is legal - on a tile file, and on a
+ * tile boundary.
+ */
+
+ if( !TIFFIsTiled( tif ) )
+ {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+ "Can't use TIFFReadRGBATile() with stripped file.");
+ return (0);
+ }
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
+ if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+ "Row/col passed to TIFFReadRGBATile() must be top"
+ "left corner of a tile.");
+ return (0);
+ }
+
+ /*
+ * Setup the RGBA reader.
+ */
+
+ if (!TIFFRGBAImageOK(tif, emsg)
+ || !TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
+ return( 0 );
+ }
+
+ /*
+ * The TIFFRGBAImageGet() function doesn't allow us to get off the
+ * edge of the image, even to fill an otherwise valid tile. So we
+ * figure out how much we can read, and fix up the tile buffer to
+ * a full tile configuration afterwards.
+ */
+
+ if( row + tile_ysize > img.height )
+ read_ysize = img.height - row;
+ else
+ read_ysize = tile_ysize;
+
+ if( col + tile_xsize > img.width )
+ read_xsize = img.width - col;
+ else
+ read_xsize = tile_xsize;
+
+ /*
+ * Read the chunk of imagery.
+ */
+
+ img.row_offset = row;
+ img.col_offset = col;
+
+ ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
+
+ TIFFRGBAImageEnd(&img);
+
+ /*
+ * If our read was incomplete we will need to fix up the tile by
+ * shifting the data around as if a full tile of data is being returned.
+ *
+ * This is all the more complicated because the image is organized in
+ * bottom to top format.
+ */
+
+ if( read_xsize == tile_xsize && read_ysize == tile_ysize )
+ return( ok );
+
+ for( i_row = 0; i_row < read_ysize; i_row++ ) {
+ memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
+ raster + (read_ysize - i_row - 1) * read_xsize,
+ read_xsize * sizeof(uint32) );
+ _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
+ 0, sizeof(uint32) * (tile_xsize - read_xsize) );
+ }
+
+ for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
+ _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
+ 0, sizeof(uint32) * tile_xsize );
+ }
+
+ return (ok);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_jbig.c b/test/monniaux/tiff-4.0.10/tif_jbig.c
new file mode 100644
index 00000000..7ffe8851
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_jbig.c
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * JBIG Compression Algorithm Support.
+ * Contributed by Lee Howard <faxguy@deanox.com>
+ *
+ */
+
+#include "tiffiop.h"
+
+#ifdef JBIG_SUPPORT
+#include "jbig.h"
+
+static int JBIGSetupDecode(TIFF* tif)
+{
+ if (TIFFNumberOfStrips(tif) != 1)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
+{
+ struct jbg_dec_state decoder;
+ int decodeStatus = 0;
+ unsigned char* pImage = NULL;
+ unsigned long decodedSize;
+ (void) s;
+
+ if (isFillOrder(tif, tif->tif_dir.td_fillorder))
+ {
+ TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
+ }
+
+ jbg_dec_init(&decoder);
+
+#if defined(HAVE_JBG_NEWLEN)
+ jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
+ /*
+ * I do not check the return status of jbg_newlen because even if this
+ * function fails it does not necessarily mean that decoding the image
+ * will fail. It is generally only needed for received fax images
+ * that do not contain the actual length of the image in the BIE
+ * header. I do not log when an error occurs because that will cause
+ * problems when converting JBIG encoded TIFF's to
+ * PostScript. As long as the actual image length is contained in the
+ * BIE header jbg_dec_in should succeed.
+ */
+#endif /* HAVE_JBG_NEWLEN */
+
+ decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawcp,
+ (size_t)tif->tif_rawcc, NULL);
+ if (JBG_EOK != decodeStatus)
+ {
+ /*
+ * XXX: JBG_EN constant was defined in pre-2.0 releases of the
+ * JBIG-KIT. Since the 2.0 the error reporting functions were
+ * changed. We will handle both cases here.
+ */
+ TIFFErrorExt(tif->tif_clientdata,
+ "JBIG", "Error (%d) decoding: %s",
+ decodeStatus,
+#if defined(JBG_EN)
+ jbg_strerror(decodeStatus, JBG_EN)
+#else
+ jbg_strerror(decodeStatus)
+#endif
+ );
+ jbg_dec_free(&decoder);
+ return 0;
+ }
+
+ decodedSize = jbg_dec_getsize(&decoder);
+ if( (tmsize_t)decodedSize < size )
+ {
+ TIFFWarningExt(tif->tif_clientdata, "JBIG",
+ "Only decoded %lu bytes, whereas %lu requested",
+ decodedSize, (unsigned long)size);
+ }
+ else if( (tmsize_t)decodedSize > size )
+ {
+ TIFFErrorExt(tif->tif_clientdata, "JBIG",
+ "Decoded %lu bytes, whereas %lu were requested",
+ decodedSize, (unsigned long)size);
+ jbg_dec_free(&decoder);
+ return 0;
+ }
+ pImage = jbg_dec_getimage(&decoder, 0);
+ _TIFFmemcpy(buffer, pImage, decodedSize);
+ jbg_dec_free(&decoder);
+
+ tif->tif_rawcp += tif->tif_rawcc;
+ tif->tif_rawcc = 0;
+
+ return 1;
+}
+
+static int JBIGSetupEncode(TIFF* tif)
+{
+ if (TIFFNumberOfStrips(tif) != 1)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
+{
+ (void) s;
+ while (cc > 0)
+ {
+ tmsize_t n = (tmsize_t)cc;
+
+ if (tif->tif_rawcc + n > tif->tif_rawdatasize)
+ {
+ n = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+
+ assert(n > 0);
+ _TIFFmemcpy(tif->tif_rawcp, pp, n);
+ tif->tif_rawcp += n;
+ tif->tif_rawcc += n;
+ pp += n;
+ cc -= (size_t)n;
+ if (tif->tif_rawcc >= tif->tif_rawdatasize &&
+ !TIFFFlushData1(tif))
+ {
+ return (-1);
+ }
+ }
+
+ return (1);
+}
+
+static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
+{
+ TIFF* tif = (TIFF*)userData;
+
+ if (isFillOrder(tif, tif->tif_dir.td_fillorder))
+ {
+ TIFFReverseBits(buffer, (tmsize_t)len);
+ }
+
+ JBIGCopyEncodedData(tif, buffer, len, 0);
+}
+
+static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
+{
+ TIFFDirectory* dir = &tif->tif_dir;
+ struct jbg_enc_state encoder;
+
+ (void) size, (void) s;
+
+ jbg_enc_init(&encoder,
+ dir->td_imagewidth,
+ dir->td_imagelength,
+ 1,
+ &buffer,
+ JBIGOutputBie,
+ tif);
+ /*
+ * jbg_enc_out does the "real" encoding. As data is encoded,
+ * JBIGOutputBie is called, which writes the data to the directory.
+ */
+ jbg_enc_out(&encoder);
+ jbg_enc_free(&encoder);
+
+ return 1;
+}
+
+int TIFFInitJBIG(TIFF* tif, int scheme)
+{
+ assert(scheme == COMPRESSION_JBIG);
+
+ /*
+ * These flags are set so the JBIG Codec can control when to reverse
+ * bits and when not to and to allow the jbig decoder and bit reverser
+ * to write to memory when necessary.
+ */
+ tif->tif_flags |= TIFF_NOBITREV;
+ tif->tif_flags &= ~TIFF_MAPPED;
+
+ /* Setup the function pointers for encode, decode, and cleanup. */
+ tif->tif_setupdecode = JBIGSetupDecode;
+ tif->tif_decodestrip = JBIGDecode;
+
+ tif->tif_setupencode = JBIGSetupEncode;
+ tif->tif_encodestrip = JBIGEncode;
+
+ return 1;
+}
+
+#endif /* JBIG_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_jpeg.c b/test/monniaux/tiff-4.0.10/tif_jpeg.c
new file mode 100644
index 00000000..f2ddc331
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_jpeg.c
@@ -0,0 +1,2599 @@
+/*
+ * Copyright (c) 1994-1997 Sam Leffler
+ * Copyright (c) 1994-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#define WIN32_LEAN_AND_MEAN
+#define VC_EXTRALEAN
+
+#include "tiffiop.h"
+#include <stdlib.h>
+
+#ifdef JPEG_SUPPORT
+
+/*
+ * TIFF Library
+ *
+ * JPEG Compression support per TIFF Technical Note #2
+ * (*not* per the original TIFF 6.0 spec).
+ *
+ * This file is simply an interface to the libjpeg library written by
+ * the Independent JPEG Group. You need release 5 or later of the IJG
+ * code, which you can find on the Internet at ftp.uu.net:/graphics/jpeg/.
+ *
+ * Contributed by Tom Lane <tgl@sss.pgh.pa.us>.
+ */
+#include <setjmp.h>
+
+int TIFFFillStrip(TIFF* tif, uint32 strip);
+int TIFFFillTile(TIFF* tif, uint32 tile);
+int TIFFReInitJPEG_12( TIFF *tif, int scheme, int is_encode );
+int TIFFJPEGIsFullStripRequired_12(TIFF* tif);
+
+/* We undefine FAR to avoid conflict with JPEG definition */
+
+#ifdef FAR
+#undef FAR
+#endif
+
+/*
+ Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
+ not defined. Unfortunately, the MinGW and Borland compilers include
+ a typedef for INT32, which causes a conflict. MSVC does not include
+ a conflicting typedef given the headers which are included.
+*/
+#if defined(__BORLANDC__) || defined(__MINGW32__)
+# define XMD_H 1
+#endif
+
+/*
+ The windows RPCNDR.H file defines boolean, but defines it with the
+ unsigned char size. You should compile JPEG library using appropriate
+ definitions in jconfig.h header, but many users compile library in wrong
+ way. That causes errors of the following type:
+
+ "JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
+ caller expects 464"
+
+ For such users we will fix the problem here. See install.doc file from
+ the JPEG library distribution for details.
+*/
+
+/* Define "boolean" as unsigned char, not int, per Windows custom. */
+#if defined(__WIN32__) && !defined(__MINGW32__)
+# ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+ typedef unsigned char boolean;
+# endif
+# define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+#endif
+
+#include "jpeglib.h"
+#include "jerror.h"
+
+/*
+ * Do we want to do special processing suitable for when JSAMPLE is a
+ * 16bit value?
+ */
+
+#if defined(JPEG_LIB_MK1)
+# define JPEG_LIB_MK1_OR_12BIT 1
+#elif BITS_IN_JSAMPLE == 12
+# define JPEG_LIB_MK1_OR_12BIT 1
+#endif
+
+/*
+ * We are using width_in_blocks which is supposed to be private to
+ * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has
+ * renamed this member to width_in_data_units. Since the header has
+ * also renamed a define, use that unique define name in order to
+ * detect the problem header and adjust to suit.
+ */
+#if defined(D_MAX_DATA_UNITS_IN_MCU)
+#define width_in_blocks width_in_data_units
+#endif
+
+/*
+ * On some machines it may be worthwhile to use _setjmp or sigsetjmp
+ * in place of plain setjmp. These macros will make it easier.
+ */
+#define SETJMP(jbuf) setjmp(jbuf)
+#define LONGJMP(jbuf,code) longjmp(jbuf,code)
+#define JMP_BUF jmp_buf
+
+typedef struct jpeg_destination_mgr jpeg_destination_mgr;
+typedef struct jpeg_source_mgr jpeg_source_mgr;
+typedef struct jpeg_error_mgr jpeg_error_mgr;
+
+/*
+ * State block for each open TIFF file using
+ * libjpeg to do JPEG compression/decompression.
+ *
+ * libjpeg's visible state is either a jpeg_compress_struct
+ * or jpeg_decompress_struct depending on which way we
+ * are going. comm can be used to refer to the fields
+ * which are common to both.
+ *
+ * NB: cinfo is required to be the first member of JPEGState,
+ * so we can safely cast JPEGState* -> jpeg_xxx_struct*
+ * and vice versa!
+ */
+typedef struct {
+ union {
+ struct jpeg_compress_struct c;
+ struct jpeg_decompress_struct d;
+ struct jpeg_common_struct comm;
+ } cinfo; /* NB: must be first */
+ int cinfo_initialized;
+
+ jpeg_error_mgr err; /* libjpeg error manager */
+ JMP_BUF exit_jmpbuf; /* for catching libjpeg failures */
+
+ struct jpeg_progress_mgr progress;
+ /*
+ * The following two members could be a union, but
+ * they're small enough that it's not worth the effort.
+ */
+ jpeg_destination_mgr dest; /* data dest for compression */
+ jpeg_source_mgr src; /* data source for decompression */
+ /* private state */
+ TIFF* tif; /* back link needed by some code */
+ uint16 photometric; /* copy of PhotometricInterpretation */
+ uint16 h_sampling; /* luminance sampling factors */
+ uint16 v_sampling;
+ tmsize_t bytesperline; /* decompressed bytes per scanline */
+ /* pointers to intermediate buffers when processing downsampled data */
+ JSAMPARRAY ds_buffer[MAX_COMPONENTS];
+ int scancount; /* number of "scanlines" accumulated */
+ int samplesperclump;
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+ TIFFPrintMethod printdir; /* super-class method */
+ TIFFStripMethod defsparent; /* super-class method */
+ TIFFTileMethod deftparent; /* super-class method */
+ /* pseudo-tag fields */
+ void* jpegtables; /* JPEGTables tag value, or NULL */
+ uint32 jpegtables_length; /* number of bytes in same */
+ int jpegquality; /* Compression quality level */
+ int jpegcolormode; /* Auto RGB<=>YCbCr convert? */
+ int jpegtablesmode; /* What to put in JPEGTables */
+
+ int ycbcrsampling_fetched;
+ int max_allowed_scan_number;
+} JPEGState;
+
+#define JState(tif) ((JPEGState*)(tif)->tif_data)
+
+static int JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int JPEGEncodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int JPEGInitializeLibJPEG(TIFF * tif, int decode );
+static int DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+
+#define FIELD_JPEGTABLES (FIELD_CODEC+0)
+
+static const TIFFField jpegFields[] = {
+ { TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_C32_UINT8, FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL },
+ { TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
+ { TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL },
+ { TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL }
+};
+
+/*
+ * libjpeg interface layer.
+ *
+ * We use setjmp/longjmp to return control to libtiff
+ * when a fatal error is encountered within the JPEG
+ * library. We also direct libjpeg error and warning
+ * messages through the appropriate libtiff handlers.
+ */
+
+/*
+ * Error handling routines (these replace corresponding
+ * IJG routines from jerror.c). These are used for both
+ * compression and decompression.
+ */
+static void
+TIFFjpeg_error_exit(j_common_ptr cinfo)
+{
+ JPEGState *sp = (JPEGState *) cinfo; /* NB: cinfo assumed first */
+ char buffer[JMSG_LENGTH_MAX];
+
+ (*cinfo->err->format_message) (cinfo, buffer);
+ TIFFErrorExt(sp->tif->tif_clientdata, "JPEGLib", "%s", buffer); /* display the error message */
+ jpeg_abort(cinfo); /* clean up libjpeg state */
+ LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
+}
+
+/*
+ * This routine is invoked only for warning messages,
+ * since error_exit does its own thing and trace_level
+ * is never set > 0.
+ */
+static void
+TIFFjpeg_output_message(j_common_ptr cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+
+ (*cinfo->err->format_message) (cinfo, buffer);
+ TIFFWarningExt(((JPEGState *) cinfo)->tif->tif_clientdata, "JPEGLib", "%s", buffer);
+}
+
+/* Avoid the risk of denial-of-service on crafted JPEGs with an insane */
+/* number of scans. */
+/* See http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf */
+static void
+TIFFjpeg_progress_monitor(j_common_ptr cinfo)
+{
+ JPEGState *sp = (JPEGState *) cinfo; /* NB: cinfo assumed first */
+ if (cinfo->is_decompressor)
+ {
+ const int scan_no =
+ ((j_decompress_ptr)cinfo)->input_scan_number;
+ if (scan_no >= sp->max_allowed_scan_number)
+ {
+ TIFFErrorExt(((JPEGState *) cinfo)->tif->tif_clientdata,
+ "TIFFjpeg_progress_monitor",
+ "Scan number %d exceeds maximum scans (%d). This limit "
+ "can be raised through the LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
+ "environment variable.",
+ scan_no, sp->max_allowed_scan_number);
+
+ jpeg_abort(cinfo); /* clean up libjpeg state */
+ LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
+ }
+ }
+}
+
+
+/*
+ * Interface routines. This layer of routines exists
+ * primarily to limit side-effects from using setjmp.
+ * Also, normal/error returns are converted into return
+ * values per libtiff practice.
+ */
+#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
+#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op),1))
+
+static int
+TIFFjpeg_create_compress(JPEGState* sp)
+{
+ /* initialize JPEG error handling */
+ sp->cinfo.c.err = jpeg_std_error(&sp->err);
+ sp->err.error_exit = TIFFjpeg_error_exit;
+ sp->err.output_message = TIFFjpeg_output_message;
+
+ /* set client_data to avoid UMR warning from tools like Purify */
+ sp->cinfo.c.client_data = NULL;
+
+ return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
+}
+
+static int
+TIFFjpeg_create_decompress(JPEGState* sp)
+{
+ /* initialize JPEG error handling */
+ sp->cinfo.d.err = jpeg_std_error(&sp->err);
+ sp->err.error_exit = TIFFjpeg_error_exit;
+ sp->err.output_message = TIFFjpeg_output_message;
+
+ /* set client_data to avoid UMR warning from tools like Purify */
+ sp->cinfo.d.client_data = NULL;
+
+ return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
+}
+
+static int
+TIFFjpeg_set_defaults(JPEGState* sp)
+{
+ return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
+}
+
+static int
+TIFFjpeg_set_colorspace(JPEGState* sp, J_COLOR_SPACE colorspace)
+{
+ return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
+}
+
+static int
+TIFFjpeg_set_quality(JPEGState* sp, int quality, boolean force_baseline)
+{
+ return CALLVJPEG(sp,
+ jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
+}
+
+static int
+TIFFjpeg_suppress_tables(JPEGState* sp, boolean suppress)
+{
+ return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
+}
+
+static int
+TIFFjpeg_start_compress(JPEGState* sp, boolean write_all_tables)
+{
+ return CALLVJPEG(sp,
+ jpeg_start_compress(&sp->cinfo.c, write_all_tables));
+}
+
+static int
+TIFFjpeg_write_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int num_lines)
+{
+ return CALLJPEG(sp, -1, (int) jpeg_write_scanlines(&sp->cinfo.c,
+ scanlines, (JDIMENSION) num_lines));
+}
+
+static int
+TIFFjpeg_write_raw_data(JPEGState* sp, JSAMPIMAGE data, int num_lines)
+{
+ return CALLJPEG(sp, -1, (int) jpeg_write_raw_data(&sp->cinfo.c,
+ data, (JDIMENSION) num_lines));
+}
+
+static int
+TIFFjpeg_finish_compress(JPEGState* sp)
+{
+ return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
+}
+
+static int
+TIFFjpeg_write_tables(JPEGState* sp)
+{
+ return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
+}
+
+static int
+TIFFjpeg_read_header(JPEGState* sp, boolean require_image)
+{
+ return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
+}
+
+static int
+TIFFjpeg_has_multiple_scans(JPEGState* sp)
+{
+ return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
+}
+
+static int
+TIFFjpeg_start_decompress(JPEGState* sp)
+{
+ const char* sz_max_allowed_scan_number;
+ /* progress monitor */
+ sp->cinfo.d.progress = &sp->progress;
+ sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
+ sp->max_allowed_scan_number = 100;
+ sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
+ if( sz_max_allowed_scan_number )
+ sp->max_allowed_scan_number = atoi(sz_max_allowed_scan_number);
+
+ return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
+}
+
+static int
+TIFFjpeg_read_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int max_lines)
+{
+ return CALLJPEG(sp, -1, (int) jpeg_read_scanlines(&sp->cinfo.d,
+ scanlines, (JDIMENSION) max_lines));
+}
+
+static int
+TIFFjpeg_read_raw_data(JPEGState* sp, JSAMPIMAGE data, int max_lines)
+{
+ return CALLJPEG(sp, -1, (int) jpeg_read_raw_data(&sp->cinfo.d,
+ data, (JDIMENSION) max_lines));
+}
+
+static int
+TIFFjpeg_finish_decompress(JPEGState* sp)
+{
+ return CALLJPEG(sp, -1, (int) jpeg_finish_decompress(&sp->cinfo.d));
+}
+
+static int
+TIFFjpeg_abort(JPEGState* sp)
+{
+ return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
+}
+
+static int
+TIFFjpeg_destroy(JPEGState* sp)
+{
+ return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
+}
+
+static JSAMPARRAY
+TIFFjpeg_alloc_sarray(JPEGState* sp, int pool_id,
+ JDIMENSION samplesperrow, JDIMENSION numrows)
+{
+ return CALLJPEG(sp, (JSAMPARRAY) NULL,
+ (*sp->cinfo.comm.mem->alloc_sarray)
+ (&sp->cinfo.comm, pool_id, samplesperrow, numrows));
+}
+
+/*
+ * JPEG library destination data manager.
+ * These routines direct compressed data from libjpeg into the
+ * libtiff output buffer.
+ */
+
+static void
+std_init_destination(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+ TIFF* tif = sp->tif;
+
+ sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata;
+ sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize;
+}
+
+static boolean
+std_empty_output_buffer(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+ TIFF* tif = sp->tif;
+
+ /* the entire buffer has been filled */
+ tif->tif_rawcc = tif->tif_rawdatasize;
+
+#ifdef IPPJ_HUFF
+ /*
+ * The Intel IPP performance library does not necessarily fill up
+ * the whole output buffer on each pass, so only dump out the parts
+ * that have been filled.
+ * http://trac.osgeo.org/gdal/wiki/JpegIPP
+ */
+ if ( sp->dest.free_in_buffer >= 0 ) {
+ tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer;
+ }
+#endif
+
+ TIFFFlushData1(tif);
+ sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata;
+ sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize;
+
+ return (TRUE);
+}
+
+static void
+std_term_destination(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+ TIFF* tif = sp->tif;
+
+ tif->tif_rawcp = (uint8*) sp->dest.next_output_byte;
+ tif->tif_rawcc =
+ tif->tif_rawdatasize - (tmsize_t) sp->dest.free_in_buffer;
+ /* NB: libtiff does the final buffer flush */
+}
+
+static void
+TIFFjpeg_data_dest(JPEGState* sp, TIFF* tif)
+{
+ (void) tif;
+ sp->cinfo.c.dest = &sp->dest;
+ sp->dest.init_destination = std_init_destination;
+ sp->dest.empty_output_buffer = std_empty_output_buffer;
+ sp->dest.term_destination = std_term_destination;
+}
+
+/*
+ * Alternate destination manager for outputting to JPEGTables field.
+ */
+
+static void
+tables_init_destination(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+
+ /* while building, jpegtables_length is allocated buffer size */
+ sp->dest.next_output_byte = (JOCTET*) sp->jpegtables;
+ sp->dest.free_in_buffer = (size_t) sp->jpegtables_length;
+}
+
+static boolean
+tables_empty_output_buffer(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+ void* newbuf;
+
+ /* the entire buffer has been filled; enlarge it by 1000 bytes */
+ newbuf = _TIFFrealloc((void*) sp->jpegtables,
+ (tmsize_t) (sp->jpegtables_length + 1000));
+ if (newbuf == NULL)
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
+ sp->dest.next_output_byte = (JOCTET*) newbuf + sp->jpegtables_length;
+ sp->dest.free_in_buffer = (size_t) 1000;
+ sp->jpegtables = newbuf;
+ sp->jpegtables_length += 1000;
+ return (TRUE);
+}
+
+static void
+tables_term_destination(j_compress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+
+ /* set tables length to number of bytes actually emitted */
+ sp->jpegtables_length -= (uint32) sp->dest.free_in_buffer;
+}
+
+static int
+TIFFjpeg_tables_dest(JPEGState* sp, TIFF* tif)
+{
+ (void) tif;
+ /*
+ * Allocate a working buffer for building tables.
+ * Initial size is 1000 bytes, which is usually adequate.
+ */
+ if (sp->jpegtables)
+ _TIFFfree(sp->jpegtables);
+ sp->jpegtables_length = 1000;
+ sp->jpegtables = (void*) _TIFFmalloc((tmsize_t) sp->jpegtables_length);
+ if (sp->jpegtables == NULL) {
+ sp->jpegtables_length = 0;
+ TIFFErrorExt(sp->tif->tif_clientdata, "TIFFjpeg_tables_dest", "No space for JPEGTables");
+ return (0);
+ }
+ sp->cinfo.c.dest = &sp->dest;
+ sp->dest.init_destination = tables_init_destination;
+ sp->dest.empty_output_buffer = tables_empty_output_buffer;
+ sp->dest.term_destination = tables_term_destination;
+ return (1);
+}
+
+/*
+ * JPEG library source data manager.
+ * These routines supply compressed data to libjpeg.
+ */
+
+static void
+std_init_source(j_decompress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+ TIFF* tif = sp->tif;
+
+ sp->src.next_input_byte = (const JOCTET*) tif->tif_rawdata;
+ sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
+}
+
+static boolean
+std_fill_input_buffer(j_decompress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState* ) cinfo;
+ static const JOCTET dummy_EOI[2] = { 0xFF, JPEG_EOI };
+
+#ifdef IPPJ_HUFF
+ /*
+ * The Intel IPP performance library does not necessarily read the whole
+ * input buffer in one pass, so it is possible to get here with data
+ * yet to read.
+ *
+ * We just return without doing anything, until the entire buffer has
+ * been read.
+ * http://trac.osgeo.org/gdal/wiki/JpegIPP
+ */
+ if( sp->src.bytes_in_buffer > 0 ) {
+ return (TRUE);
+ }
+#endif
+
+ /*
+ * Normally the whole strip/tile is read and so we don't need to do
+ * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
+ * all the data, but the rawdata is refreshed between scanlines and
+ * we push this into the io machinery in JPEGDecode().
+ * http://trac.osgeo.org/gdal/ticket/3894
+ */
+
+ WARNMS(cinfo, JWRN_JPEG_EOF);
+ /* insert a fake EOI marker */
+ sp->src.next_input_byte = dummy_EOI;
+ sp->src.bytes_in_buffer = 2;
+ return (TRUE);
+}
+
+static void
+std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+
+ if (num_bytes > 0) {
+ if ((size_t)num_bytes > sp->src.bytes_in_buffer) {
+ /* oops, buffer overrun */
+ (void) std_fill_input_buffer(cinfo);
+ } else {
+ sp->src.next_input_byte += (size_t) num_bytes;
+ sp->src.bytes_in_buffer -= (size_t) num_bytes;
+ }
+ }
+}
+
+static void
+std_term_source(j_decompress_ptr cinfo)
+{
+ /* No work necessary here */
+ (void) cinfo;
+}
+
+static void
+TIFFjpeg_data_src(JPEGState* sp)
+{
+ sp->cinfo.d.src = &sp->src;
+ sp->src.init_source = std_init_source;
+ sp->src.fill_input_buffer = std_fill_input_buffer;
+ sp->src.skip_input_data = std_skip_input_data;
+ sp->src.resync_to_restart = jpeg_resync_to_restart;
+ sp->src.term_source = std_term_source;
+ sp->src.bytes_in_buffer = 0; /* for safety */
+ sp->src.next_input_byte = NULL;
+}
+
+/*
+ * Alternate source manager for reading from JPEGTables.
+ * We can share all the code except for the init routine.
+ */
+
+static void
+tables_init_source(j_decompress_ptr cinfo)
+{
+ JPEGState* sp = (JPEGState*) cinfo;
+
+ sp->src.next_input_byte = (const JOCTET*) sp->jpegtables;
+ sp->src.bytes_in_buffer = (size_t) sp->jpegtables_length;
+}
+
+static void
+TIFFjpeg_tables_src(JPEGState* sp)
+{
+ TIFFjpeg_data_src(sp);
+ sp->src.init_source = tables_init_source;
+}
+
+/*
+ * Allocate downsampled-data buffers needed for downsampled I/O.
+ * We use values computed in jpeg_start_compress or jpeg_start_decompress.
+ * We use libjpeg's allocator so that buffers will be released automatically
+ * when done with strip/tile.
+ * This is also a handy place to compute samplesperclump, bytesperline.
+ */
+static int
+alloc_downsampled_buffers(TIFF* tif, jpeg_component_info* comp_info,
+ int num_components)
+{
+ JPEGState* sp = JState(tif);
+ int ci;
+ jpeg_component_info* compptr;
+ JSAMPARRAY buf;
+ int samples_per_clump = 0;
+
+ for (ci = 0, compptr = comp_info; ci < num_components;
+ ci++, compptr++) {
+ samples_per_clump += compptr->h_samp_factor *
+ compptr->v_samp_factor;
+ buf = TIFFjpeg_alloc_sarray(sp, JPOOL_IMAGE,
+ compptr->width_in_blocks * DCTSIZE,
+ (JDIMENSION) (compptr->v_samp_factor*DCTSIZE));
+ if (buf == NULL)
+ return (0);
+ sp->ds_buffer[ci] = buf;
+ }
+ sp->samplesperclump = samples_per_clump;
+ return (1);
+}
+
+
+/*
+ * JPEG Decoding.
+ */
+
+#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
+
+#define JPEG_MARKER_SOF0 0xC0
+#define JPEG_MARKER_SOF1 0xC1
+#define JPEG_MARKER_SOF2 0xC2
+#define JPEG_MARKER_SOF9 0xC9
+#define JPEG_MARKER_SOF10 0xCA
+#define JPEG_MARKER_DHT 0xC4
+#define JPEG_MARKER_SOI 0xD8
+#define JPEG_MARKER_SOS 0xDA
+#define JPEG_MARKER_DQT 0xDB
+#define JPEG_MARKER_DRI 0xDD
+#define JPEG_MARKER_APP0 0xE0
+#define JPEG_MARKER_COM 0xFE
+struct JPEGFixupTagsSubsamplingData
+{
+ TIFF* tif;
+ void* buffer;
+ uint32 buffersize;
+ uint8* buffercurrentbyte;
+ uint32 bufferbytesleft;
+ uint64 fileoffset;
+ uint64 filebytesleft;
+ uint8 filepositioned;
+};
+static void JPEGFixupTagsSubsampling(TIFF* tif);
+static int JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data);
+static int JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result);
+static int JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result);
+static void JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength);
+
+#endif
+
+static int
+JPEGFixupTags(TIFF* tif)
+{
+#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
+ JPEGState* sp = JState(tif);
+ if ((tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)&&
+ (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
+ (tif->tif_dir.td_samplesperpixel==3) &&
+ !sp->ycbcrsampling_fetched)
+ JPEGFixupTagsSubsampling(tif);
+#endif
+
+ return(1);
+}
+
+#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
+
+static void
+JPEGFixupTagsSubsampling(TIFF* tif)
+{
+ /*
+ * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
+ * the TIFF tags, but still use non-default (2,2) values within the jpeg
+ * data stream itself. In order for TIFF applications to work properly
+ * - for instance to get the strip buffer size right - it is imperative
+ * that the subsampling be available before we start reading the image
+ * data normally. This function will attempt to analyze the first strip in
+ * order to get the sampling values from the jpeg data stream.
+ *
+ * Note that JPEGPreDeocode() will produce a fairly loud warning when the
+ * discovered sampling does not match the default sampling (2,2) or whatever
+ * was actually in the tiff tags.
+ *
+ * See the bug in bugzilla for details:
+ *
+ * http://bugzilla.remotesensing.org/show_bug.cgi?id=168
+ *
+ * Frank Warmerdam, July 2002
+ * Joris Van Damme, May 2007
+ */
+ static const char module[] = "JPEGFixupTagsSubsampling";
+ struct JPEGFixupTagsSubsamplingData m;
+
+ _TIFFFillStriles( tif );
+
+ if( tif->tif_dir.td_stripbytecount == NULL
+ || tif->tif_dir.td_stripoffset == NULL
+ || tif->tif_dir.td_stripbytecount[0] == 0 )
+ {
+ /* Do not even try to check if the first strip/tile does not
+ yet exist, as occurs when GDAL has created a new NULL file
+ for instance. */
+ return;
+ }
+
+ m.tif=tif;
+ m.buffersize=2048;
+ m.buffer=_TIFFmalloc(m.buffersize);
+ if (m.buffer==NULL)
+ {
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Unable to allocate memory for auto-correcting of subsampling values; auto-correcting skipped");
+ return;
+ }
+ m.buffercurrentbyte=NULL;
+ m.bufferbytesleft=0;
+ m.fileoffset=tif->tif_dir.td_stripoffset[0];
+ m.filepositioned=0;
+ m.filebytesleft=tif->tif_dir.td_stripbytecount[0];
+ if (!JPEGFixupTagsSubsamplingSec(&m))
+ TIFFWarningExt(tif->tif_clientdata,module,
+ "Unable to auto-correct subsampling values, likely corrupt JPEG compressed data in first strip/tile; auto-correcting skipped");
+ _TIFFfree(m.buffer);
+}
+
+static int
+JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data)
+{
+ static const char module[] = "JPEGFixupTagsSubsamplingSec";
+ uint8 m;
+ while (1)
+ {
+ while (1)
+ {
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
+ return(0);
+ if (m==255)
+ break;
+ }
+ while (1)
+ {
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
+ return(0);
+ if (m!=255)
+ break;
+ }
+ switch (m)
+ {
+ case JPEG_MARKER_SOI:
+ /* this type of marker has no data and should be skipped */
+ break;
+ case JPEG_MARKER_COM:
+ case JPEG_MARKER_APP0:
+ case JPEG_MARKER_APP0+1:
+ case JPEG_MARKER_APP0+2:
+ case JPEG_MARKER_APP0+3:
+ case JPEG_MARKER_APP0+4:
+ case JPEG_MARKER_APP0+5:
+ case JPEG_MARKER_APP0+6:
+ case JPEG_MARKER_APP0+7:
+ case JPEG_MARKER_APP0+8:
+ case JPEG_MARKER_APP0+9:
+ case JPEG_MARKER_APP0+10:
+ case JPEG_MARKER_APP0+11:
+ case JPEG_MARKER_APP0+12:
+ case JPEG_MARKER_APP0+13:
+ case JPEG_MARKER_APP0+14:
+ case JPEG_MARKER_APP0+15:
+ case JPEG_MARKER_DQT:
+ case JPEG_MARKER_SOS:
+ case JPEG_MARKER_DHT:
+ case JPEG_MARKER_DRI:
+ /* this type of marker has data, but it has no use to us and should be skipped */
+ {
+ uint16 n;
+ if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
+ return(0);
+ if (n<2)
+ return(0);
+ n-=2;
+ if (n>0)
+ JPEGFixupTagsSubsamplingSkip(data,n);
+ }
+ break;
+ case JPEG_MARKER_SOF0: /* Baseline sequential Huffman */
+ case JPEG_MARKER_SOF1: /* Extended sequential Huffman */
+ case JPEG_MARKER_SOF2: /* Progressive Huffman: normally not allowed by TechNote, but that doesn't hurt supporting it */
+ case JPEG_MARKER_SOF9: /* Extended sequential arithmetic */
+ case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not allowed by TechNote, but that doesn't hurt supporting it */
+ /* this marker contains the subsampling factors we're scanning for */
+ {
+ uint16 n;
+ uint16 o;
+ uint8 p;
+ uint8 ph,pv;
+ if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
+ return(0);
+ if (n!=8+data->tif->tif_dir.td_samplesperpixel*3)
+ return(0);
+ JPEGFixupTagsSubsamplingSkip(data,7);
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
+ return(0);
+ ph=(p>>4);
+ pv=(p&15);
+ JPEGFixupTagsSubsamplingSkip(data,1);
+ for (o=1; o<data->tif->tif_dir.td_samplesperpixel; o++)
+ {
+ JPEGFixupTagsSubsamplingSkip(data,1);
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
+ return(0);
+ if (p!=0x11)
+ {
+ TIFFWarningExt(data->tif->tif_clientdata,module,
+ "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
+ return(1);
+ }
+ JPEGFixupTagsSubsamplingSkip(data,1);
+ }
+ if (((ph!=1)&&(ph!=2)&&(ph!=4))||((pv!=1)&&(pv!=2)&&(pv!=4)))
+ {
+ TIFFWarningExt(data->tif->tif_clientdata,module,
+ "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
+ return(1);
+ }
+ if ((ph!=data->tif->tif_dir.td_ycbcrsubsampling[0])||(pv!=data->tif->tif_dir.td_ycbcrsubsampling[1]))
+ {
+ TIFFWarningExt(data->tif->tif_clientdata,module,
+ "Auto-corrected former TIFF subsampling values [%d,%d] to match subsampling values inside JPEG compressed data [%d,%d]",
+ (int)data->tif->tif_dir.td_ycbcrsubsampling[0],
+ (int)data->tif->tif_dir.td_ycbcrsubsampling[1],
+ (int)ph,(int)pv);
+ data->tif->tif_dir.td_ycbcrsubsampling[0]=ph;
+ data->tif->tif_dir.td_ycbcrsubsampling[1]=pv;
+ }
+ }
+ return(1);
+ default:
+ return(0);
+ }
+ }
+}
+
+static int
+JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result)
+{
+ if (data->bufferbytesleft==0)
+ {
+ uint32 m;
+ if (data->filebytesleft==0)
+ return(0);
+ if (!data->filepositioned)
+ {
+ TIFFSeekFile(data->tif,data->fileoffset,SEEK_SET);
+ data->filepositioned=1;
+ }
+ m=data->buffersize;
+ if ((uint64)m>data->filebytesleft)
+ m=(uint32)data->filebytesleft;
+ assert(m<0x80000000UL);
+ if (TIFFReadFile(data->tif,data->buffer,(tmsize_t)m)!=(tmsize_t)m)
+ return(0);
+ data->buffercurrentbyte=data->buffer;
+ data->bufferbytesleft=m;
+ data->fileoffset+=m;
+ data->filebytesleft-=m;
+ }
+ *result=*data->buffercurrentbyte;
+ data->buffercurrentbyte++;
+ data->bufferbytesleft--;
+ return(1);
+}
+
+static int
+JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result)
+{
+ uint8 ma;
+ uint8 mb;
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&ma))
+ return(0);
+ if (!JPEGFixupTagsSubsamplingReadByte(data,&mb))
+ return(0);
+ *result=(ma<<8)|mb;
+ return(1);
+}
+
+static void
+JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)
+{
+ if ((uint32)skiplength<=data->bufferbytesleft)
+ {
+ data->buffercurrentbyte+=skiplength;
+ data->bufferbytesleft-=skiplength;
+ }
+ else
+ {
+ uint16 m;
+ m=(uint16)(skiplength-data->bufferbytesleft);
+ if (m<=data->filebytesleft)
+ {
+ data->bufferbytesleft=0;
+ data->fileoffset+=m;
+ data->filebytesleft-=m;
+ data->filepositioned=0;
+ }
+ else
+ {
+ data->bufferbytesleft=0;
+ data->filebytesleft=0;
+ }
+ }
+}
+
+#endif
+
+
+static int
+JPEGSetupDecode(TIFF* tif)
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+
+#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
+ if( tif->tif_dir.td_bitspersample == 12 )
+ return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 0 );
+#endif
+
+ JPEGInitializeLibJPEG( tif, TRUE );
+
+ assert(sp != NULL);
+ assert(sp->cinfo.comm.is_decompressor);
+
+ /* Read JPEGTables if it is present */
+ if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) {
+ TIFFjpeg_tables_src(sp);
+ if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) {
+ TIFFErrorExt(tif->tif_clientdata, "JPEGSetupDecode", "Bogus JPEGTables field");
+ return (0);
+ }
+ }
+
+ /* Grab parameters that are same for all strips/tiles */
+ sp->photometric = td->td_photometric;
+ switch (sp->photometric) {
+ case PHOTOMETRIC_YCBCR:
+ sp->h_sampling = td->td_ycbcrsubsampling[0];
+ sp->v_sampling = td->td_ycbcrsubsampling[1];
+ break;
+ default:
+ /* TIFF 6.0 forbids subsampling of all other color spaces */
+ sp->h_sampling = 1;
+ sp->v_sampling = 1;
+ break;
+ }
+
+ /* Set up for reading normal data */
+ TIFFjpeg_data_src(sp);
+ tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
+ return (1);
+}
+
+/* Returns 1 if the full strip should be read, even when doing scanline per */
+/* scanline decoding. This happens when the JPEG stream uses multiple scans. */
+/* Currently only called in CHUNKY_STRIP_READ_SUPPORT mode through */
+/* scanline interface. */
+/* Only reads tif->tif_dir.td_bitspersample, tif->tif_rawdata and */
+/* tif->tif_rawcc members. */
+/* Can be called independently of the usual setup/predecode/decode states */
+int TIFFJPEGIsFullStripRequired(TIFF* tif)
+{
+ int ret;
+ JPEGState state;
+
+#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFJPEGIsFullStripRequired)
+ if( tif->tif_dir.td_bitspersample == 12 )
+ return TIFFJPEGIsFullStripRequired_12( tif );
+#endif
+
+ memset(&state, 0, sizeof(JPEGState));
+ state.tif = tif;
+
+ TIFFjpeg_create_decompress(&state);
+
+ TIFFjpeg_data_src(&state);
+
+ if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
+ {
+ TIFFjpeg_destroy(&state);
+ return (0);
+ }
+ ret = TIFFjpeg_has_multiple_scans(&state);
+
+ TIFFjpeg_destroy(&state);
+
+ return ret;
+}
+
+/*
+ * Set up for decoding a strip or tile.
+ */
+/*ARGSUSED*/ static int
+JPEGPreDecode(TIFF* tif, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+ static const char module[] = "JPEGPreDecode";
+ uint32 segment_width, segment_height;
+ int downsampled_output;
+ int ci;
+
+ assert(sp != NULL);
+
+ if (sp->cinfo.comm.is_decompressor == 0)
+ {
+ tif->tif_setupdecode( tif );
+ }
+
+ assert(sp->cinfo.comm.is_decompressor);
+ /*
+ * Reset decoder state from any previous strip/tile,
+ * in case application didn't read the whole strip.
+ */
+ if (!TIFFjpeg_abort(sp))
+ return (0);
+ /*
+ * Read the header for this strip/tile.
+ */
+
+ if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
+ return (0);
+
+ tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
+ tif->tif_rawcc = sp->src.bytes_in_buffer;
+
+ /*
+ * Check image parameters and set decompression parameters.
+ */
+ if (isTiled(tif)) {
+ segment_width = td->td_tilewidth;
+ segment_height = td->td_tilelength;
+ sp->bytesperline = TIFFTileRowSize(tif);
+ } else {
+ segment_width = td->td_imagewidth;
+ segment_height = td->td_imagelength - tif->tif_row;
+ if (segment_height > td->td_rowsperstrip)
+ segment_height = td->td_rowsperstrip;
+ sp->bytesperline = TIFFScanlineSize(tif);
+ }
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
+ /*
+ * For PC 2, scale down the expected strip/tile size
+ * to match a downsampled component
+ */
+ segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
+ segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
+ }
+ if (sp->cinfo.d.image_width < segment_width ||
+ sp->cinfo.d.image_height < segment_height) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Improper JPEG strip/tile size, "
+ "expected %dx%d, got %dx%d",
+ segment_width, segment_height,
+ sp->cinfo.d.image_width,
+ sp->cinfo.d.image_height);
+ }
+ if( sp->cinfo.d.image_width == segment_width &&
+ sp->cinfo.d.image_height > segment_height &&
+ tif->tif_row + segment_height == td->td_imagelength &&
+ !isTiled(tif) ) {
+ /* Some files have a last strip, that should be truncated, */
+ /* but their JPEG codestream has still the maximum strip */
+ /* height. Warn about this as this is non compliant, but */
+ /* we can safely recover from that. */
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "JPEG strip size exceeds expected dimensions,"
+ " expected %dx%d, got %dx%d",
+ segment_width, segment_height,
+ sp->cinfo.d.image_width, sp->cinfo.d.image_height);
+ }
+ else if (sp->cinfo.d.image_width > segment_width ||
+ sp->cinfo.d.image_height > segment_height) {
+ /*
+ * This case could be dangerous, if the strip or tile size has
+ * been reported as less than the amount of data jpeg will
+ * return, some potential security issues arise. Catch this
+ * case and error out.
+ */
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "JPEG strip/tile size exceeds expected dimensions,"
+ " expected %dx%d, got %dx%d",
+ segment_width, segment_height,
+ sp->cinfo.d.image_width, sp->cinfo.d.image_height);
+ return (0);
+ }
+ if (sp->cinfo.d.num_components !=
+ (td->td_planarconfig == PLANARCONFIG_CONTIG ?
+ td->td_samplesperpixel : 1)) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG component count");
+ return (0);
+ }
+#ifdef JPEG_LIB_MK1
+ if (12 != td->td_bitspersample && 8 != td->td_bitspersample) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
+ return (0);
+ }
+ sp->cinfo.d.data_precision = td->td_bitspersample;
+ sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
+#else
+ if (sp->cinfo.d.data_precision != td->td_bitspersample) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
+ return (0);
+ }
+#endif
+
+ /* In some cases, libjpeg needs to allocate a lot of memory */
+ /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf */
+ if( TIFFjpeg_has_multiple_scans(sp) )
+ {
+ /* In this case libjpeg will need to allocate memory or backing */
+ /* store for all coefficients */
+ /* See call to jinit_d_coef_controller() from master_selection() */
+ /* in libjpeg */
+ toff_t nRequiredMemory = (toff_t)sp->cinfo.d.image_width *
+ sp->cinfo.d.image_height *
+ sp->cinfo.d.num_components *
+ ((td->td_bitspersample+7)/8);
+ /* BLOCK_SMOOTHING_SUPPORTED is generally defined, so we need */
+ /* to replicate the logic of jinit_d_coef_controller() */
+ if( sp->cinfo.d.progressive_mode )
+ nRequiredMemory *= 3;
+
+#ifndef TIFF_LIBJPEG_LARGEST_MEM_ALLOC
+#define TIFF_LIBJPEG_LARGEST_MEM_ALLOC (100 * 1024 * 1024)
+#endif
+
+ if( nRequiredMemory > TIFF_LIBJPEG_LARGEST_MEM_ALLOC &&
+ getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Reading this strip would require libjpeg to allocate "
+ "at least %u bytes. "
+ "This is disabled since above the %u threshold. "
+ "You may override this restriction by defining the "
+ "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
+ "or recompile libtiff by defining the "
+ "TIFF_LIBJPEG_LARGEST_MEM_ALLOC macro to a value greater "
+ "than %u",
+ (unsigned)nRequiredMemory,
+ (unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC,
+ (unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC);
+ return (0);
+ }
+ }
+
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ /* Component 0 should have expected sampling factors */
+ if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
+ sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Improper JPEG sampling factors %d,%d\n"
+ "Apparently should be %d,%d.",
+ sp->cinfo.d.comp_info[0].h_samp_factor,
+ sp->cinfo.d.comp_info[0].v_samp_factor,
+ sp->h_sampling, sp->v_sampling);
+ return (0);
+ }
+ /* Rest should have sampling factors 1,1 */
+ for (ci = 1; ci < sp->cinfo.d.num_components; ci++) {
+ if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
+ sp->cinfo.d.comp_info[ci].v_samp_factor != 1) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
+ return (0);
+ }
+ }
+ } else {
+ /* PC 2's single component should have sampling factors 1,1 */
+ if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
+ sp->cinfo.d.comp_info[0].v_samp_factor != 1) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
+ return (0);
+ }
+ }
+ downsampled_output = FALSE;
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
+ sp->photometric == PHOTOMETRIC_YCBCR &&
+ sp->jpegcolormode == JPEGCOLORMODE_RGB) {
+ /* Convert YCbCr to RGB */
+ sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
+ sp->cinfo.d.out_color_space = JCS_RGB;
+ } else {
+ /* Suppress colorspace handling */
+ sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
+ sp->cinfo.d.out_color_space = JCS_UNKNOWN;
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
+ (sp->h_sampling != 1 || sp->v_sampling != 1))
+ downsampled_output = TRUE;
+ /* XXX what about up-sampling? */
+ }
+ if (downsampled_output) {
+ /* Need to use raw-data interface to libjpeg */
+ sp->cinfo.d.raw_data_out = TRUE;
+#if JPEG_LIB_VERSION >= 70
+ sp->cinfo.d.do_fancy_upsampling = FALSE;
+#endif /* JPEG_LIB_VERSION >= 70 */
+ tif->tif_decoderow = DecodeRowError;
+ tif->tif_decodestrip = JPEGDecodeRaw;
+ tif->tif_decodetile = JPEGDecodeRaw;
+ } else {
+ /* Use normal interface to libjpeg */
+ sp->cinfo.d.raw_data_out = FALSE;
+ tif->tif_decoderow = JPEGDecode;
+ tif->tif_decodestrip = JPEGDecode;
+ tif->tif_decodetile = JPEGDecode;
+ }
+ /* Start JPEG decompressor */
+ if (!TIFFjpeg_start_decompress(sp))
+ return (0);
+ /* Allocate downsampled-data buffers if needed */
+ if (downsampled_output) {
+ if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
+ sp->cinfo.d.num_components))
+ return (0);
+ sp->scancount = DCTSIZE; /* mark buffer empty */
+ }
+ return (1);
+}
+
+/*
+ * Decode a chunk of pixels.
+ * "Standard" case: returned data is not downsampled.
+ */
+#if !JPEG_LIB_MK1_OR_12BIT
+static int
+JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ tmsize_t nrows;
+ (void) s;
+
+ /*
+ ** Update available information, buffer may have been refilled
+ ** between decode requests
+ */
+ sp->src.next_input_byte = (const JOCTET*) tif->tif_rawcp;
+ sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
+
+ if( sp->bytesperline == 0 )
+ return 0;
+
+ nrows = cc / sp->bytesperline;
+ if (cc % sp->bytesperline)
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "fractional scanline not read");
+
+ if( nrows > (tmsize_t) sp->cinfo.d.image_height )
+ nrows = sp->cinfo.d.image_height;
+
+ /* data is expected to be read in multiples of a scanline */
+ if (nrows)
+ {
+ do
+ {
+ /*
+ * In the libjpeg6b-9a 8bit case. We read directly into
+ * the TIFF buffer.
+ */
+ JSAMPROW bufptr = (JSAMPROW)buf;
+
+ if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
+ return (0);
+
+ ++tif->tif_row;
+ buf += sp->bytesperline;
+ cc -= sp->bytesperline;
+ } while (--nrows > 0);
+ }
+
+ /* Update information on consumed data */
+ tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
+ tif->tif_rawcc = sp->src.bytes_in_buffer;
+
+ /* Close down the decompressor if we've finished the strip or tile. */
+ return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
+ || TIFFjpeg_finish_decompress(sp);
+}
+#endif /* !JPEG_LIB_MK1_OR_12BIT */
+
+#if JPEG_LIB_MK1_OR_12BIT
+/*ARGSUSED*/ static int
+JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ tmsize_t nrows;
+ (void) s;
+
+ /*
+ ** Update available information, buffer may have been refilled
+ ** between decode requests
+ */
+ sp->src.next_input_byte = (const JOCTET*) tif->tif_rawcp;
+ sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
+
+ if( sp->bytesperline == 0 )
+ return 0;
+
+ nrows = cc / sp->bytesperline;
+ if (cc % sp->bytesperline)
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "fractional scanline not read");
+
+ if( nrows > (tmsize_t) sp->cinfo.d.image_height )
+ nrows = sp->cinfo.d.image_height;
+
+ /* data is expected to be read in multiples of a scanline */
+ if (nrows)
+ {
+ JSAMPROW line_work_buf = NULL;
+
+ /*
+ * For 6B, only use temporary buffer for 12 bit imagery.
+ * For Mk1 always use it.
+ */
+ if( sp->cinfo.d.data_precision == 12 )
+ {
+ line_work_buf = (JSAMPROW)
+ _TIFFmalloc(sizeof(short) * sp->cinfo.d.output_width
+ * sp->cinfo.d.num_components );
+ }
+
+ do
+ {
+ if( line_work_buf != NULL )
+ {
+ /*
+ * In the MK1 case, we always read into a 16bit
+ * buffer, and then pack down to 12bit or 8bit.
+ * In 6B case we only read into 16 bit buffer
+ * for 12bit data, which we need to repack.
+ */
+ if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
+ return (0);
+
+ if( sp->cinfo.d.data_precision == 12 )
+ {
+ int value_pairs = (sp->cinfo.d.output_width
+ * sp->cinfo.d.num_components) / 2;
+ int iPair;
+
+ for( iPair = 0; iPair < value_pairs; iPair++ )
+ {
+ unsigned char *out_ptr =
+ ((unsigned char *) buf) + iPair * 3;
+ JSAMPLE *in_ptr = line_work_buf + iPair * 2;
+
+ out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
+ out_ptr[1] = (unsigned char)(((in_ptr[0] & 0xf) << 4)
+ | ((in_ptr[1] & 0xf00) >> 8));
+ out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
+ }
+ }
+ else if( sp->cinfo.d.data_precision == 8 )
+ {
+ int value_count = (sp->cinfo.d.output_width
+ * sp->cinfo.d.num_components);
+ int iValue;
+
+ for( iValue = 0; iValue < value_count; iValue++ )
+ {
+ ((unsigned char *) buf)[iValue] =
+ line_work_buf[iValue] & 0xff;
+ }
+ }
+ }
+
+ ++tif->tif_row;
+ buf += sp->bytesperline;
+ cc -= sp->bytesperline;
+ } while (--nrows > 0);
+
+ if( line_work_buf != NULL )
+ _TIFFfree( line_work_buf );
+ }
+
+ /* Update information on consumed data */
+ tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
+ tif->tif_rawcc = sp->src.bytes_in_buffer;
+
+ /* Close down the decompressor if we've finished the strip or tile. */
+ return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
+ || TIFFjpeg_finish_decompress(sp);
+}
+#endif /* JPEG_LIB_MK1_OR_12BIT */
+
+/*ARGSUSED*/ static int
+DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+
+{
+ (void) buf;
+ (void) cc;
+ (void) s;
+
+ TIFFErrorExt(tif->tif_clientdata, "TIFFReadScanline",
+ "scanline oriented access is not supported for downsampled JPEG compressed images, consider enabling TIFF_JPEGCOLORMODE as JPEGCOLORMODE_RGB." );
+ return 0;
+}
+
+/*
+ * Decode a chunk of pixels.
+ * Returned data is downsampled per sampling factors.
+ */
+/*ARGSUSED*/ static int
+JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ tmsize_t nrows;
+ TIFFDirectory *td = &tif->tif_dir;
+ (void) s;
+
+ nrows = sp->cinfo.d.image_height;
+ /* For last strip, limit number of rows to its truncated height */
+ /* even if the codestream height is larger (which is not compliant, */
+ /* but that we tolerate) */
+ if( (uint32)nrows > td->td_imagelength - tif->tif_row && !isTiled(tif) )
+ nrows = td->td_imagelength - tif->tif_row;
+
+ /* data is expected to be read in multiples of a scanline */
+ if ( nrows != 0 ) {
+
+ /* Cb,Cr both have sampling factors 1, so this is correct */
+ JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
+ int samples_per_clump = sp->samplesperclump;
+
+#if defined(JPEG_LIB_MK1_OR_12BIT)
+ unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
+ sp->cinfo.d.output_width *
+ sp->cinfo.d.num_components);
+ if(tmpbuf==NULL) {
+ TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
+ "Out of memory");
+ return 0;
+ }
+#endif
+
+ do {
+ jpeg_component_info *compptr;
+ int ci, clumpoffset;
+
+ if( cc < sp->bytesperline ) {
+ TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
+ "application buffer not large enough for all data.");
+ return 0;
+ }
+
+ /* Reload downsampled-data buffer if needed */
+ if (sp->scancount >= DCTSIZE) {
+ int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
+ if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
+ return (0);
+ sp->scancount = 0;
+ }
+ /*
+ * Fastest way to unseparate data is to make one pass
+ * over the scanline for each row of each component.
+ */
+ clumpoffset = 0; /* first sample in clump */
+ for (ci = 0, compptr = sp->cinfo.d.comp_info;
+ ci < sp->cinfo.d.num_components;
+ ci++, compptr++) {
+ int hsamp = compptr->h_samp_factor;
+ int vsamp = compptr->v_samp_factor;
+ int ypos;
+
+ for (ypos = 0; ypos < vsamp; ypos++) {
+ JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
+ JDIMENSION nclump;
+#if defined(JPEG_LIB_MK1_OR_12BIT)
+ JSAMPLE *outptr = (JSAMPLE*)tmpbuf + clumpoffset;
+#else
+ JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
+ if (cc < (tmsize_t) (clumpoffset + samples_per_clump*(clumps_per_line-1) + hsamp)) {
+ TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
+ "application buffer not large enough for all data, possible subsampling issue");
+ return 0;
+ }
+#endif
+
+ if (hsamp == 1) {
+ /* fast path for at least Cb and Cr */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ outptr[0] = *inptr++;
+ outptr += samples_per_clump;
+ }
+ } else {
+ int xpos;
+
+ /* general case */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ for (xpos = 0; xpos < hsamp; xpos++)
+ outptr[xpos] = *inptr++;
+ outptr += samples_per_clump;
+ }
+ }
+ clumpoffset += hsamp;
+ }
+ }
+
+#if defined(JPEG_LIB_MK1_OR_12BIT)
+ {
+ if (sp->cinfo.d.data_precision == 8)
+ {
+ int i=0;
+ int len = sp->cinfo.d.output_width * sp->cinfo.d.num_components;
+ for (i=0; i<len; i++)
+ {
+ ((unsigned char*)buf)[i] = tmpbuf[i] & 0xff;
+ }
+ }
+ else
+ { /* 12-bit */
+ int value_pairs = (sp->cinfo.d.output_width
+ * sp->cinfo.d.num_components) / 2;
+ int iPair;
+ for( iPair = 0; iPair < value_pairs; iPair++ )
+ {
+ unsigned char *out_ptr = ((unsigned char *) buf) + iPair * 3;
+ JSAMPLE *in_ptr = (JSAMPLE *) (tmpbuf + iPair * 2);
+ out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
+ out_ptr[1] = (unsigned char)(((in_ptr[0] & 0xf) << 4)
+ | ((in_ptr[1] & 0xf00) >> 8));
+ out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
+ }
+ }
+ }
+#endif
+
+ sp->scancount ++;
+ tif->tif_row += sp->v_sampling;
+
+ buf += sp->bytesperline;
+ cc -= sp->bytesperline;
+
+ nrows -= sp->v_sampling;
+ } while (nrows > 0);
+
+#if defined(JPEG_LIB_MK1_OR_12BIT)
+ _TIFFfree(tmpbuf);
+#endif
+
+ }
+
+ /* Close down the decompressor if done. */
+ return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
+ || TIFFjpeg_finish_decompress(sp);
+}
+
+
+/*
+ * JPEG Encoding.
+ */
+
+static void
+unsuppress_quant_table (JPEGState* sp, int tblno)
+{
+ JQUANT_TBL* qtbl;
+
+ if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
+ qtbl->sent_table = FALSE;
+}
+
+static void
+suppress_quant_table (JPEGState* sp, int tblno)
+{
+ JQUANT_TBL* qtbl;
+
+ if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
+ qtbl->sent_table = TRUE;
+}
+
+static void
+unsuppress_huff_table (JPEGState* sp, int tblno)
+{
+ JHUFF_TBL* htbl;
+
+ if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
+ htbl->sent_table = FALSE;
+ if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
+ htbl->sent_table = FALSE;
+}
+
+static void
+suppress_huff_table (JPEGState* sp, int tblno)
+{
+ JHUFF_TBL* htbl;
+
+ if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
+ htbl->sent_table = TRUE;
+ if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
+ htbl->sent_table = TRUE;
+}
+
+static int
+prepare_JPEGTables(TIFF* tif)
+{
+ JPEGState* sp = JState(tif);
+
+ /* Initialize quant tables for current quality setting */
+ if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
+ return (0);
+ /* Mark only the tables we want for output */
+ /* NB: chrominance tables are currently used only with YCbCr */
+ if (!TIFFjpeg_suppress_tables(sp, TRUE))
+ return (0);
+ if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) {
+ unsuppress_quant_table(sp, 0);
+ if (sp->photometric == PHOTOMETRIC_YCBCR)
+ unsuppress_quant_table(sp, 1);
+ }
+ if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) {
+ unsuppress_huff_table(sp, 0);
+ if (sp->photometric == PHOTOMETRIC_YCBCR)
+ unsuppress_huff_table(sp, 1);
+ }
+ /* Direct libjpeg output into jpegtables */
+ if (!TIFFjpeg_tables_dest(sp, tif))
+ return (0);
+ /* Emit tables-only datastream */
+ if (!TIFFjpeg_write_tables(sp))
+ return (0);
+
+ return (1);
+}
+
+static int
+JPEGSetupEncode(TIFF* tif)
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+ static const char module[] = "JPEGSetupEncode";
+
+#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
+ if( tif->tif_dir.td_bitspersample == 12 )
+ return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 1 );
+#endif
+
+ JPEGInitializeLibJPEG( tif, FALSE );
+
+ assert(sp != NULL);
+ assert(!sp->cinfo.comm.is_decompressor);
+
+ sp->photometric = td->td_photometric;
+
+ /*
+ * Initialize all JPEG parameters to default values.
+ * Note that jpeg_set_defaults needs legal values for
+ * in_color_space and input_components.
+ */
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ sp->cinfo.c.input_components = td->td_samplesperpixel;
+ if (sp->photometric == PHOTOMETRIC_YCBCR) {
+ if (sp->jpegcolormode == JPEGCOLORMODE_RGB) {
+ sp->cinfo.c.in_color_space = JCS_RGB;
+ } else {
+ sp->cinfo.c.in_color_space = JCS_YCbCr;
+ }
+ } else {
+ if ((td->td_photometric == PHOTOMETRIC_MINISWHITE || td->td_photometric == PHOTOMETRIC_MINISBLACK) && td->td_samplesperpixel == 1)
+ sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
+ else if (td->td_photometric == PHOTOMETRIC_RGB && td->td_samplesperpixel == 3)
+ sp->cinfo.c.in_color_space = JCS_RGB;
+ else if (td->td_photometric == PHOTOMETRIC_SEPARATED && td->td_samplesperpixel == 4)
+ sp->cinfo.c.in_color_space = JCS_CMYK;
+ else
+ sp->cinfo.c.in_color_space = JCS_UNKNOWN;
+ }
+ } else {
+ sp->cinfo.c.input_components = 1;
+ sp->cinfo.c.in_color_space = JCS_UNKNOWN;
+ }
+ if (!TIFFjpeg_set_defaults(sp))
+ return (0);
+ /* Set per-file parameters */
+ switch (sp->photometric) {
+ case PHOTOMETRIC_YCBCR:
+ sp->h_sampling = td->td_ycbcrsubsampling[0];
+ sp->v_sampling = td->td_ycbcrsubsampling[1];
+ if( sp->h_sampling == 0 || sp->v_sampling == 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalig horizontal/vertical sampling value");
+ return (0);
+ }
+ if( td->td_bitspersample > 16 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "BitsPerSample %d not allowed for JPEG",
+ td->td_bitspersample);
+ return (0);
+ }
+
+ /*
+ * A ReferenceBlackWhite field *must* be present since the
+ * default value is inappropriate for YCbCr. Fill in the
+ * proper value if application didn't set it.
+ */
+ {
+ float *ref;
+ if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
+ &ref)) {
+ float refbw[6];
+ long top = 1L << td->td_bitspersample;
+ refbw[0] = 0;
+ refbw[1] = (float)(top-1L);
+ refbw[2] = (float)(top>>1);
+ refbw[3] = refbw[1];
+ refbw[4] = refbw[2];
+ refbw[5] = refbw[1];
+ TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
+ refbw);
+ }
+ }
+ break;
+ case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
+ case PHOTOMETRIC_MASK:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "PhotometricInterpretation %d not allowed for JPEG",
+ (int) sp->photometric);
+ return (0);
+ default:
+ /* TIFF 6.0 forbids subsampling of all other color spaces */
+ sp->h_sampling = 1;
+ sp->v_sampling = 1;
+ break;
+ }
+
+ /* Verify miscellaneous parameters */
+
+ /*
+ * This would need work if libtiff ever supports different
+ * depths for different components, or if libjpeg ever supports
+ * run-time selection of depth. Neither is imminent.
+ */
+#ifdef JPEG_LIB_MK1
+ /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
+ if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
+#else
+ if (td->td_bitspersample != BITS_IN_JSAMPLE )
+#endif
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "BitsPerSample %d not allowed for JPEG",
+ (int) td->td_bitspersample);
+ return (0);
+ }
+ sp->cinfo.c.data_precision = td->td_bitspersample;
+#ifdef JPEG_LIB_MK1
+ sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
+#endif
+ if (isTiled(tif)) {
+ if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "JPEG tile height must be multiple of %d",
+ sp->v_sampling * DCTSIZE);
+ return (0);
+ }
+ if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "JPEG tile width must be multiple of %d",
+ sp->h_sampling * DCTSIZE);
+ return (0);
+ }
+ } else {
+ if (td->td_rowsperstrip < td->td_imagelength &&
+ (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "RowsPerStrip must be multiple of %d for JPEG",
+ sp->v_sampling * DCTSIZE);
+ return (0);
+ }
+ }
+
+ /* Create a JPEGTables field if appropriate */
+ if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) {
+ if( sp->jpegtables == NULL
+ || memcmp(sp->jpegtables,"\0\0\0\0\0\0\0\0\0",8) == 0 )
+ {
+ if (!prepare_JPEGTables(tif))
+ return (0);
+ /* Mark the field present */
+ /* Can't use TIFFSetField since BEENWRITING is already set! */
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+ TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
+ }
+ } else {
+ /* We do not support application-supplied JPEGTables, */
+ /* so mark the field not present */
+ TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
+ }
+
+ /* Direct libjpeg output to libtiff's output buffer */
+ TIFFjpeg_data_dest(sp, tif);
+
+ return (1);
+}
+
+/*
+ * Set encoding state at the start of a strip or tile.
+ */
+static int
+JPEGPreEncode(TIFF* tif, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+ static const char module[] = "JPEGPreEncode";
+ uint32 segment_width, segment_height;
+ int downsampled_input;
+
+ assert(sp != NULL);
+
+ if (sp->cinfo.comm.is_decompressor == 1)
+ {
+ tif->tif_setupencode( tif );
+ }
+
+ assert(!sp->cinfo.comm.is_decompressor);
+ /*
+ * Set encoding parameters for this strip/tile.
+ */
+ if (isTiled(tif)) {
+ segment_width = td->td_tilewidth;
+ segment_height = td->td_tilelength;
+ sp->bytesperline = TIFFTileRowSize(tif);
+ } else {
+ segment_width = td->td_imagewidth;
+ segment_height = td->td_imagelength - tif->tif_row;
+ if (segment_height > td->td_rowsperstrip)
+ segment_height = td->td_rowsperstrip;
+ sp->bytesperline = TIFFScanlineSize(tif);
+ }
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
+ /* for PC 2, scale down the strip/tile size
+ * to match a downsampled component
+ */
+ segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
+ segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
+ }
+ if (segment_width > 65535 || segment_height > 65535) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Strip/tile too large for JPEG");
+ return (0);
+ }
+ sp->cinfo.c.image_width = segment_width;
+ sp->cinfo.c.image_height = segment_height;
+ downsampled_input = FALSE;
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ sp->cinfo.c.input_components = td->td_samplesperpixel;
+ if (sp->photometric == PHOTOMETRIC_YCBCR) {
+ if (sp->jpegcolormode != JPEGCOLORMODE_RGB) {
+ if (sp->h_sampling != 1 || sp->v_sampling != 1)
+ downsampled_input = TRUE;
+ }
+ if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
+ return (0);
+ /*
+ * Set Y sampling factors;
+ * we assume jpeg_set_colorspace() set the rest to 1
+ */
+ sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
+ sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
+ } else {
+ if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
+ return (0);
+ /* jpeg_set_colorspace set all sampling factors to 1 */
+ }
+ } else {
+ if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
+ return (0);
+ sp->cinfo.c.comp_info[0].component_id = s;
+ /* jpeg_set_colorspace() set sampling factors to 1 */
+ if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) {
+ sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
+ sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
+ sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
+ }
+ }
+ /* ensure libjpeg won't write any extraneous markers */
+ sp->cinfo.c.write_JFIF_header = FALSE;
+ sp->cinfo.c.write_Adobe_marker = FALSE;
+ /* set up table handling correctly */
+ /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged */
+ /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT */
+ /* mode, so we must manually suppress them. However TIFFjpeg_set_quality() */
+ /* should really be called when dealing with files with directories with */
+ /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
+ if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
+ return (0);
+ if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) {
+ suppress_quant_table(sp, 0);
+ suppress_quant_table(sp, 1);
+ }
+ else {
+ unsuppress_quant_table(sp, 0);
+ unsuppress_quant_table(sp, 1);
+ }
+ if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF)
+ {
+ /* Explicit suppression is only needed if we did not go through the */
+ /* prepare_JPEGTables() code path, which may be the case if updating */
+ /* an existing file */
+ suppress_huff_table(sp, 0);
+ suppress_huff_table(sp, 1);
+ sp->cinfo.c.optimize_coding = FALSE;
+ }
+ else
+ sp->cinfo.c.optimize_coding = TRUE;
+ if (downsampled_input) {
+ /* Need to use raw-data interface to libjpeg */
+ sp->cinfo.c.raw_data_in = TRUE;
+ tif->tif_encoderow = JPEGEncodeRaw;
+ tif->tif_encodestrip = JPEGEncodeRaw;
+ tif->tif_encodetile = JPEGEncodeRaw;
+ } else {
+ /* Use normal interface to libjpeg */
+ sp->cinfo.c.raw_data_in = FALSE;
+ tif->tif_encoderow = JPEGEncode;
+ tif->tif_encodestrip = JPEGEncode;
+ tif->tif_encodetile = JPEGEncode;
+ }
+ /* Start JPEG compressor */
+ if (!TIFFjpeg_start_compress(sp, FALSE))
+ return (0);
+ /* Allocate downsampled-data buffers if needed */
+ if (downsampled_input) {
+ if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
+ sp->cinfo.c.num_components))
+ return (0);
+ }
+ sp->scancount = 0;
+
+ return (1);
+}
+
+/*
+ * Encode a chunk of pixels.
+ * "Standard" case: incoming data is not downsampled.
+ */
+static int
+JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ tmsize_t nrows;
+ JSAMPROW bufptr[1];
+ short *line16 = NULL;
+ int line16_count = 0;
+
+ (void) s;
+ assert(sp != NULL);
+ /* data is expected to be supplied in multiples of a scanline */
+ nrows = cc / sp->bytesperline;
+ if (cc % sp->bytesperline)
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "fractional scanline discarded");
+
+ /* The last strip will be limited to image size */
+ if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
+ nrows = tif->tif_dir.td_imagelength - tif->tif_row;
+
+ if( sp->cinfo.c.data_precision == 12 )
+ {
+ line16_count = (int)((sp->bytesperline * 2) / 3);
+ line16 = (short *) _TIFFmalloc(sizeof(short) * line16_count);
+ if (!line16)
+ {
+ TIFFErrorExt(tif->tif_clientdata,
+ "JPEGEncode",
+ "Failed to allocate memory");
+
+ return 0;
+ }
+ }
+
+ while (nrows-- > 0) {
+
+ if( sp->cinfo.c.data_precision == 12 )
+ {
+
+ int value_pairs = line16_count / 2;
+ int iPair;
+
+ bufptr[0] = (JSAMPROW) line16;
+
+ for( iPair = 0; iPair < value_pairs; iPair++ )
+ {
+ unsigned char *in_ptr =
+ ((unsigned char *) buf) + iPair * 3;
+ JSAMPLE *out_ptr = (JSAMPLE *) (line16 + iPair * 2);
+
+ out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
+ out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
+ }
+ }
+ else
+ {
+ bufptr[0] = (JSAMPROW) buf;
+ }
+ if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
+ return (0);
+ if (nrows > 0)
+ tif->tif_row++;
+ buf += sp->bytesperline;
+ }
+
+ if( sp->cinfo.c.data_precision == 12 )
+ {
+ _TIFFfree( line16 );
+ }
+
+ return (1);
+}
+
+/*
+ * Encode a chunk of pixels.
+ * Incoming data is expected to be downsampled per sampling factors.
+ */
+static int
+JPEGEncodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ JPEGState *sp = JState(tif);
+ JSAMPLE* inptr;
+ JSAMPLE* outptr;
+ tmsize_t nrows;
+ JDIMENSION clumps_per_line, nclump;
+ int clumpoffset, ci, xpos, ypos;
+ jpeg_component_info* compptr;
+ int samples_per_clump = sp->samplesperclump;
+ tmsize_t bytesperclumpline;
+
+ (void) s;
+ assert(sp != NULL);
+ /* data is expected to be supplied in multiples of a clumpline */
+ /* a clumpline is equivalent to v_sampling desubsampled scanlines */
+ /* TODO: the following calculation of bytesperclumpline, should substitute calculation of sp->bytesperline, except that it is per v_sampling lines */
+ bytesperclumpline = (((sp->cinfo.c.image_width+sp->h_sampling-1)/sp->h_sampling)
+ *(sp->h_sampling*sp->v_sampling+2)*sp->cinfo.c.data_precision+7)
+ /8;
+
+ nrows = ( cc / bytesperclumpline ) * sp->v_sampling;
+ if (cc % bytesperclumpline)
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
+
+ /* Cb,Cr both have sampling factors 1, so this is correct */
+ clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
+
+ while (nrows > 0) {
+ /*
+ * Fastest way to separate the data is to make one pass
+ * over the scanline for each row of each component.
+ */
+ clumpoffset = 0; /* first sample in clump */
+ for (ci = 0, compptr = sp->cinfo.c.comp_info;
+ ci < sp->cinfo.c.num_components;
+ ci++, compptr++) {
+ int hsamp = compptr->h_samp_factor;
+ int vsamp = compptr->v_samp_factor;
+ int padding = (int) (compptr->width_in_blocks * DCTSIZE -
+ clumps_per_line * hsamp);
+ for (ypos = 0; ypos < vsamp; ypos++) {
+ inptr = ((JSAMPLE*) buf) + clumpoffset;
+ outptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
+ if (hsamp == 1) {
+ /* fast path for at least Cb and Cr */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ *outptr++ = inptr[0];
+ inptr += samples_per_clump;
+ }
+ } else {
+ /* general case */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ for (xpos = 0; xpos < hsamp; xpos++)
+ *outptr++ = inptr[xpos];
+ inptr += samples_per_clump;
+ }
+ }
+ /* pad each scanline as needed */
+ for (xpos = 0; xpos < padding; xpos++) {
+ *outptr = outptr[-1];
+ outptr++;
+ }
+ clumpoffset += hsamp;
+ }
+ }
+ sp->scancount++;
+ if (sp->scancount >= DCTSIZE) {
+ int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
+ if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
+ return (0);
+ sp->scancount = 0;
+ }
+ tif->tif_row += sp->v_sampling;
+ buf += bytesperclumpline;
+ nrows -= sp->v_sampling;
+ }
+ return (1);
+}
+
+/*
+ * Finish up at the end of a strip or tile.
+ */
+static int
+JPEGPostEncode(TIFF* tif)
+{
+ JPEGState *sp = JState(tif);
+
+ if (sp->scancount > 0) {
+ /*
+ * Need to emit a partial bufferload of downsampled data.
+ * Pad the data vertically.
+ */
+ int ci, ypos, n;
+ jpeg_component_info* compptr;
+
+ for (ci = 0, compptr = sp->cinfo.c.comp_info;
+ ci < sp->cinfo.c.num_components;
+ ci++, compptr++) {
+ int vsamp = compptr->v_samp_factor;
+ tmsize_t row_width = compptr->width_in_blocks * DCTSIZE
+ * sizeof(JSAMPLE);
+ for (ypos = sp->scancount * vsamp;
+ ypos < DCTSIZE * vsamp; ypos++) {
+ _TIFFmemcpy((void*)sp->ds_buffer[ci][ypos],
+ (void*)sp->ds_buffer[ci][ypos-1],
+ row_width);
+
+ }
+ }
+ n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
+ if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
+ return (0);
+ }
+
+ return (TIFFjpeg_finish_compress(JState(tif)));
+}
+
+static void
+JPEGCleanup(TIFF* tif)
+{
+ JPEGState *sp = JState(tif);
+
+ assert(sp != 0);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+ tif->tif_tagmethods.printdir = sp->printdir;
+ if( sp->cinfo_initialized )
+ TIFFjpeg_destroy(sp); /* release libjpeg resources */
+ if (sp->jpegtables) /* tag value */
+ _TIFFfree(sp->jpegtables);
+ _TIFFfree(tif->tif_data); /* release local state */
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static void
+JPEGResetUpsampled( TIFF* tif )
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ /*
+ * Mark whether returned data is up-sampled or not so TIFFStripSize
+ * and TIFFTileSize return values that reflect the true amount of
+ * data.
+ */
+ tif->tif_flags &= ~TIFF_UPSAMPLED;
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ if (td->td_photometric == PHOTOMETRIC_YCBCR &&
+ sp->jpegcolormode == JPEGCOLORMODE_RGB) {
+ tif->tif_flags |= TIFF_UPSAMPLED;
+ } else {
+#ifdef notdef
+ if (td->td_ycbcrsubsampling[0] != 1 ||
+ td->td_ycbcrsubsampling[1] != 1)
+ ; /* XXX what about up-sampling? */
+#endif
+ }
+ }
+
+ /*
+ * Must recalculate cached tile size in case sampling state changed.
+ * Should we really be doing this now if image size isn't set?
+ */
+ if( tif->tif_tilesize > 0 )
+ tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
+ if( tif->tif_scanlinesize > 0 )
+ tif->tif_scanlinesize = TIFFScanlineSize(tif);
+}
+
+static int
+JPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ JPEGState* sp = JState(tif);
+ const TIFFField* fip;
+ uint32 v32;
+
+ assert(sp != NULL);
+
+ switch (tag) {
+ case TIFFTAG_JPEGTABLES:
+ v32 = (uint32) va_arg(ap, uint32);
+ if (v32 == 0) {
+ /* XXX */
+ return (0);
+ }
+ _TIFFsetByteArray(&sp->jpegtables, va_arg(ap, void*), v32);
+ sp->jpegtables_length = v32;
+ TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
+ break;
+ case TIFFTAG_JPEGQUALITY:
+ sp->jpegquality = (int) va_arg(ap, int);
+ return (1); /* pseudo tag */
+ case TIFFTAG_JPEGCOLORMODE:
+ sp->jpegcolormode = (int) va_arg(ap, int);
+ JPEGResetUpsampled( tif );
+ return (1); /* pseudo tag */
+ case TIFFTAG_PHOTOMETRIC:
+ {
+ int ret_value = (*sp->vsetparent)(tif, tag, ap);
+ JPEGResetUpsampled( tif );
+ return ret_value;
+ }
+ case TIFFTAG_JPEGTABLESMODE:
+ sp->jpegtablesmode = (int) va_arg(ap, int);
+ return (1); /* pseudo tag */
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ /* mark the fact that we have a real ycbcrsubsampling! */
+ sp->ycbcrsampling_fetched = 1;
+ /* should we be recomputing upsampling info here? */
+ return (*sp->vsetparent)(tif, tag, ap);
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+
+ if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) {
+ TIFFSetFieldBit(tif, fip->field_bit);
+ } else {
+ return (0);
+ }
+
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+ return (1);
+}
+
+static int
+JPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ JPEGState* sp = JState(tif);
+
+ assert(sp != NULL);
+
+ switch (tag) {
+ case TIFFTAG_JPEGTABLES:
+ *va_arg(ap, uint32*) = sp->jpegtables_length;
+ *va_arg(ap, void**) = sp->jpegtables;
+ break;
+ case TIFFTAG_JPEGQUALITY:
+ *va_arg(ap, int*) = sp->jpegquality;
+ break;
+ case TIFFTAG_JPEGCOLORMODE:
+ *va_arg(ap, int*) = sp->jpegcolormode;
+ break;
+ case TIFFTAG_JPEGTABLESMODE:
+ *va_arg(ap, int*) = sp->jpegtablesmode;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return (1);
+}
+
+static void
+JPEGPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+ JPEGState* sp = JState(tif);
+
+ assert(sp != NULL);
+ (void) flags;
+
+ if( sp != NULL ) {
+ if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
+ fprintf(fd, " JPEG Tables: (%lu bytes)\n",
+ (unsigned long) sp->jpegtables_length);
+ if (sp->printdir)
+ (*sp->printdir)(tif, fd, flags);
+ }
+}
+
+static uint32
+JPEGDefaultStripSize(TIFF* tif, uint32 s)
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+
+ s = (*sp->defsparent)(tif, s);
+ if (s < td->td_imagelength)
+ s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
+ return (s);
+}
+
+static void
+JPEGDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory *td = &tif->tif_dir;
+
+ (*sp->deftparent)(tif, tw, th);
+ *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE);
+ *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE);
+}
+
+/*
+ * The JPEG library initialized used to be done in TIFFInitJPEG(), but
+ * now that we allow a TIFF file to be opened in update mode it is necessary
+ * to have some way of deciding whether compression or decompression is
+ * desired other than looking at tif->tif_mode. We accomplish this by
+ * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry.
+ * If so, we assume decompression is desired.
+ *
+ * This is tricky, because TIFFInitJPEG() is called while the directory is
+ * being read, and generally speaking the BYTECOUNTS tag won't have been read
+ * at that point. So we try to defer jpeg library initialization till we
+ * do have that tag ... basically any access that might require the compressor
+ * or decompressor that occurs after the reading of the directory.
+ *
+ * In an ideal world compressors or decompressors would be setup
+ * at the point where a single tile or strip was accessed (for read or write)
+ * so that stuff like update of missing tiles, or replacement of tiles could
+ * be done. However, we aren't trying to crack that nut just yet ...
+ *
+ * NFW, Feb 3rd, 2003.
+ */
+
+static int JPEGInitializeLibJPEG( TIFF * tif, int decompress )
+{
+ JPEGState* sp = JState(tif);
+
+ if(sp->cinfo_initialized)
+ {
+ if( !decompress && sp->cinfo.comm.is_decompressor )
+ TIFFjpeg_destroy( sp );
+ else if( decompress && !sp->cinfo.comm.is_decompressor )
+ TIFFjpeg_destroy( sp );
+ else
+ return 1;
+
+ sp->cinfo_initialized = 0;
+ }
+
+ /*
+ * Initialize libjpeg.
+ */
+ if ( decompress ) {
+ if (!TIFFjpeg_create_decompress(sp))
+ return (0);
+ } else {
+ if (!TIFFjpeg_create_compress(sp))
+ return (0);
+#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
+#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
+#endif
+ /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */
+ /* store implementation, so better not set max_memory_to_use ourselves. */
+ /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */
+ if( sp->cinfo.c.mem->max_memory_to_use > 0 )
+ {
+ /* This is to address bug related in ticket GDAL #1795. */
+ if (getenv("JPEGMEM") == NULL)
+ {
+ /* Increase the max memory usable. This helps when creating files */
+ /* with "big" tile, without using libjpeg temporary files. */
+ /* For example a 512x512 tile with 3 bands */
+ /* requires 1.5 MB which is above libjpeg 1MB default */
+ if( sp->cinfo.c.mem->max_memory_to_use < TIFF_JPEG_MAX_MEMORY_TO_USE )
+ sp->cinfo.c.mem->max_memory_to_use = TIFF_JPEG_MAX_MEMORY_TO_USE;
+ }
+ }
+ }
+
+ sp->cinfo_initialized = TRUE;
+
+ return 1;
+}
+
+int
+TIFFInitJPEG(TIFF* tif, int scheme)
+{
+ JPEGState* sp;
+
+ assert(scheme == COMPRESSION_JPEG);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields))) {
+ TIFFErrorExt(tif->tif_clientdata,
+ "TIFFInitJPEG",
+ "Merging JPEG codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof (JPEGState));
+
+ if (tif->tif_data == NULL) {
+ TIFFErrorExt(tif->tif_clientdata,
+ "TIFFInitJPEG", "No space for JPEG state block");
+ return 0;
+ }
+ _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
+
+ sp = JState(tif);
+ sp->tif = tif; /* back link */
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
+ sp->printdir = tif->tif_tagmethods.printdir;
+ tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->jpegtables = NULL;
+ sp->jpegtables_length = 0;
+ sp->jpegquality = 75; /* Default IJG quality */
+ sp->jpegcolormode = JPEGCOLORMODE_RAW;
+ sp->jpegtablesmode = JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
+ sp->ycbcrsampling_fetched = 0;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = JPEGFixupTags;
+ tif->tif_setupdecode = JPEGSetupDecode;
+ tif->tif_predecode = JPEGPreDecode;
+ tif->tif_decoderow = JPEGDecode;
+ tif->tif_decodestrip = JPEGDecode;
+ tif->tif_decodetile = JPEGDecode;
+ tif->tif_setupencode = JPEGSetupEncode;
+ tif->tif_preencode = JPEGPreEncode;
+ tif->tif_postencode = JPEGPostEncode;
+ tif->tif_encoderow = JPEGEncode;
+ tif->tif_encodestrip = JPEGEncode;
+ tif->tif_encodetile = JPEGEncode;
+ tif->tif_cleanup = JPEGCleanup;
+ sp->defsparent = tif->tif_defstripsize;
+ tif->tif_defstripsize = JPEGDefaultStripSize;
+ sp->deftparent = tif->tif_deftilesize;
+ tif->tif_deftilesize = JPEGDefaultTileSize;
+ tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
+
+ sp->cinfo_initialized = FALSE;
+
+ /*
+ ** Create a JPEGTables field if no directory has yet been created.
+ ** We do this just to ensure that sufficient space is reserved for
+ ** the JPEGTables field. It will be properly created the right
+ ** size later.
+ */
+ if( tif->tif_diroff == 0 )
+ {
+#define SIZE_OF_JPEGTABLES 2000
+/*
+The following line assumes incorrectly that all JPEG-in-TIFF files will have
+a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags to be written
+when the JPEG data is placed with TIFFWriteRawStrip. The field bit should be
+set, anyway, later when actual JPEGTABLES header is generated, so removing it
+here hopefully is harmless.
+ TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
+*/
+ sp->jpegtables_length = SIZE_OF_JPEGTABLES;
+ sp->jpegtables = (void *) _TIFFmalloc(sp->jpegtables_length);
+ if (sp->jpegtables)
+ {
+ _TIFFmemset(sp->jpegtables, 0, SIZE_OF_JPEGTABLES);
+ }
+ else
+ {
+ TIFFErrorExt(tif->tif_clientdata,
+ "TIFFInitJPEG",
+ "Failed to allocate memory for JPEG tables");
+ return 0;
+ }
+#undef SIZE_OF_JPEGTABLES
+ }
+
+ return 1;
+}
+#endif /* JPEG_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_jpeg_12.c b/test/monniaux/tiff-4.0.10/tif_jpeg_12.c
new file mode 100644
index 00000000..b458c258
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_jpeg_12.c
@@ -0,0 +1,69 @@
+
+#include "tiffiop.h"
+
+#if defined(JPEG_DUAL_MODE_8_12)
+
+# define TIFFInitJPEG TIFFInitJPEG_12
+# define TIFFJPEGIsFullStripRequired TIFFJPEGIsFullStripRequired_12
+
+int
+TIFFInitJPEG_12(TIFF* tif, int scheme);
+
+# include LIBJPEG_12_PATH
+
+# include "tif_jpeg.c"
+
+int TIFFReInitJPEG_12( TIFF *tif, int scheme, int is_encode )
+
+{
+ JPEGState* sp;
+
+ assert(scheme == COMPRESSION_JPEG);
+
+ sp = JState(tif);
+ sp->tif = tif; /* back link */
+
+ /*
+ * Override parent get/set field methods.
+ */
+ tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
+ tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
+ tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = JPEGFixupTags;
+ tif->tif_setupdecode = JPEGSetupDecode;
+ tif->tif_predecode = JPEGPreDecode;
+ tif->tif_decoderow = JPEGDecode;
+ tif->tif_decodestrip = JPEGDecode;
+ tif->tif_decodetile = JPEGDecode;
+ tif->tif_setupencode = JPEGSetupEncode;
+ tif->tif_preencode = JPEGPreEncode;
+ tif->tif_postencode = JPEGPostEncode;
+ tif->tif_encoderow = JPEGEncode;
+ tif->tif_encodestrip = JPEGEncode;
+ tif->tif_encodetile = JPEGEncode;
+ tif->tif_cleanup = JPEGCleanup;
+ tif->tif_defstripsize = JPEGDefaultStripSize;
+ tif->tif_deftilesize = JPEGDefaultTileSize;
+ tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
+
+ sp->cinfo_initialized = FALSE;
+
+ if( is_encode )
+ return JPEGSetupEncode(tif);
+ else
+ return JPEGSetupDecode(tif);
+}
+
+#endif /* defined(JPEG_DUAL_MODE_8_12) */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_luv.c b/test/monniaux/tiff-4.0.10/tif_luv.c
new file mode 100644
index 00000000..aa35ea07
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_luv.c
@@ -0,0 +1,1765 @@
+/*
+ * Copyright (c) 1997 Greg Ward Larson
+ * Copyright (c) 1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
+ * advertising or publicity relating to the software without the specific,
+ * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef LOGLUV_SUPPORT
+
+/*
+ * TIFF Library.
+ * LogLuv compression support for high dynamic range images.
+ *
+ * Contributed by Greg Larson.
+ *
+ * LogLuv image support uses the TIFF library to store 16 or 10-bit
+ * log luminance values with 8 bits each of u and v or a 14-bit index.
+ *
+ * The codec can take as input and produce as output 32-bit IEEE float values
+ * as well as 16-bit integer values. A 16-bit luminance is interpreted
+ * as a sign bit followed by a 15-bit integer that is converted
+ * to and from a linear magnitude using the transformation:
+ *
+ * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
+ *
+ * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
+ *
+ * The actual conversion to world luminance units in candelas per sq. meter
+ * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
+ * This value is usually set such that a reasonable exposure comes from
+ * clamping decoded luminances above 1 to 1 in the displayed image.
+ *
+ * The 16-bit values for u and v may be converted to real values by dividing
+ * each by 32768. (This allows for negative values, which aren't useful as
+ * far as we know, but are left in case of future improvements in human
+ * color vision.)
+ *
+ * Conversion from (u,v), which is actually the CIE (u',v') system for
+ * you color scientists, is accomplished by the following transformation:
+ *
+ * u = 4*x / (-2*x + 12*y + 3)
+ * v = 9*y / (-2*x + 12*y + 3)
+ *
+ * x = 9*u / (6*u - 16*v + 12)
+ * y = 4*v / (6*u - 16*v + 12)
+ *
+ * This process is greatly simplified by passing 32-bit IEEE floats
+ * for each of three CIE XYZ coordinates. The codec then takes care
+ * of conversion to and from LogLuv, though the application is still
+ * responsible for interpreting the TIFFTAG_STONITS calibration factor.
+ *
+ * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
+ * point of (x,y)=(1/3,1/3). However, most color systems assume some other
+ * white point, such as D65, and an absolute color conversion to XYZ then
+ * to another color space with a different white point may introduce an
+ * unwanted color cast to the image. It is often desirable, therefore, to
+ * perform a white point conversion that maps the input white to [1 1 1]
+ * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
+ * tag value. A decoder that demands absolute color calibration may use
+ * this white point tag to get back the original colors, but usually it
+ * will be ignored and the new white point will be used instead that
+ * matches the output color space.
+ *
+ * Pixel information is compressed into one of two basic encodings, depending
+ * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
+ * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
+ * stored as:
+ *
+ * 1 15
+ * |-+---------------|
+ *
+ * COMPRESSION_SGILOG color data is stored as:
+ *
+ * 1 15 8 8
+ * |-+---------------|--------+--------|
+ * S Le ue ve
+ *
+ * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
+ *
+ * 10 14
+ * |----------|--------------|
+ * Le' Ce
+ *
+ * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
+ * encoded as an index for optimal color resolution. The 10 log bits are
+ * defined by the following conversions:
+ *
+ * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
+ *
+ * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
+ *
+ * The 10 bits of the smaller format may be converted into the 15 bits of
+ * the larger format by multiplying by 4 and adding 13314. Obviously,
+ * a smaller range of magnitudes is covered (about 5 orders of magnitude
+ * instead of 38), and the lack of a sign bit means that negative luminances
+ * are not allowed. (Well, they aren't allowed in the real world, either,
+ * but they are useful for certain types of image processing.)
+ *
+ * The desired user format is controlled by the setting the internal
+ * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
+ * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
+ * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
+ * Raw data i/o is also possible using:
+ * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
+ * In addition, the following decoding is provided for ease of display:
+ * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
+ *
+ * For grayscale images, we provide the following data formats:
+ * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
+ * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
+ * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
+ *
+ * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
+ * scheme by separating the logL, u and v bytes for each row and applying
+ * a PackBits type of compression. Since the 24-bit encoding is not
+ * adaptive, the 32-bit color format takes less space in many cases.
+ *
+ * Further control is provided over the conversion from higher-resolution
+ * formats to final encoded values through the pseudo tag
+ * TIFFTAG_SGILOGENCODE:
+ * SGILOGENCODE_NODITHER = do not dither encoded values
+ * SGILOGENCODE_RANDITHER = apply random dithering during encoding
+ *
+ * The default value of this tag is SGILOGENCODE_NODITHER for
+ * COMPRESSION_SGILOG to maximize run-length encoding and
+ * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
+ * quantization errors into noise.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+/*
+ * State block for each open TIFF
+ * file using LogLuv compression/decompression.
+ */
+typedef struct logLuvState LogLuvState;
+
+struct logLuvState {
+ int encoder_state; /* 1 if encoder correctly initialized */
+ int user_datafmt; /* user data format */
+ int encode_meth; /* encoding method */
+ int pixel_size; /* bytes per pixel */
+
+ uint8* tbuf; /* translation buffer */
+ tmsize_t tbuflen; /* buffer length */
+ void (*tfunc)(LogLuvState*, uint8*, tmsize_t);
+
+ TIFFVSetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+};
+
+#define DecoderState(tif) ((LogLuvState*) (tif)->tif_data)
+#define EncoderState(tif) ((LogLuvState*) (tif)->tif_data)
+
+#define SGILOGDATAFMT_UNKNOWN -1
+
+#define MINRUN 4 /* minimum run length */
+
+/*
+ * Decode a string of 16-bit gray pixels.
+ */
+static int
+LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "LogL16Decode";
+ LogLuvState* sp = DecoderState(tif);
+ int shft;
+ tmsize_t i;
+ tmsize_t npixels;
+ unsigned char* bp;
+ int16* tp;
+ int16 b;
+ tmsize_t cc;
+ int rc;
+
+ assert(s == 0);
+ assert(sp != NULL);
+
+ npixels = occ / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
+ tp = (int16*) op;
+ else {
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ tp = (int16*) sp->tbuf;
+ }
+ _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
+
+ bp = (unsigned char*) tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ /* get each byte string */
+ for (shft = 8; shft >= 0; shft -=8) {
+ for (i = 0; i < npixels && cc > 0; ) {
+ if (*bp >= 128) { /* run */
+ if( cc < 2 )
+ break;
+ rc = *bp++ + (2-128);
+ b = (int16)(*bp++ << shft);
+ cc -= 2;
+ while (rc-- && i < npixels)
+ tp[i++] |= b;
+ } else { /* non-run */
+ rc = *bp++; /* nul is noop */
+ while (--cc && rc-- && i < npixels)
+ tp[i++] |= (int16)*bp++ << shft;
+ }
+ }
+ if (i != npixels) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %I64d pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) (npixels - i));
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %llu pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) (npixels - i));
+#endif
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ return (0);
+ }
+ }
+ (*sp->tfunc)(sp, op, npixels);
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ return (1);
+}
+
+/*
+ * Decode a string of 24-bit pixels.
+ */
+static int
+LogLuvDecode24(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "LogLuvDecode24";
+ LogLuvState* sp = DecoderState(tif);
+ tmsize_t cc;
+ tmsize_t i;
+ tmsize_t npixels;
+ unsigned char* bp;
+ uint32* tp;
+
+ assert(s == 0);
+ assert(sp != NULL);
+
+ npixels = occ / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_RAW)
+ tp = (uint32 *)op;
+ else {
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ tp = (uint32 *) sp->tbuf;
+ }
+ /* copy to array of uint32 */
+ bp = (unsigned char*) tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ for (i = 0; i < npixels && cc >= 3; i++) {
+ tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
+ bp += 3;
+ cc -= 3;
+ }
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ if (i != npixels) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %I64d pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) (npixels - i));
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %llu pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) (npixels - i));
+#endif
+ return (0);
+ }
+ (*sp->tfunc)(sp, op, npixels);
+ return (1);
+}
+
+/*
+ * Decode a string of 32-bit pixels.
+ */
+static int
+LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "LogLuvDecode32";
+ LogLuvState* sp;
+ int shft;
+ tmsize_t i;
+ tmsize_t npixels;
+ unsigned char* bp;
+ uint32* tp;
+ uint32 b;
+ tmsize_t cc;
+ int rc;
+
+ assert(s == 0);
+ sp = DecoderState(tif);
+ assert(sp != NULL);
+
+ npixels = occ / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_RAW)
+ tp = (uint32*) op;
+ else {
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ tp = (uint32*) sp->tbuf;
+ }
+ _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
+
+ bp = (unsigned char*) tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ /* get each byte string */
+ for (shft = 24; shft >= 0; shft -=8) {
+ for (i = 0; i < npixels && cc > 0; ) {
+ if (*bp >= 128) { /* run */
+ if( cc < 2 )
+ break;
+ rc = *bp++ + (2-128);
+ b = (uint32)*bp++ << shft;
+ cc -= 2;
+ while (rc-- && i < npixels)
+ tp[i++] |= b;
+ } else { /* non-run */
+ rc = *bp++; /* nul is noop */
+ while (--cc && rc-- && i < npixels)
+ tp[i++] |= (uint32)*bp++ << shft;
+ }
+ }
+ if (i != npixels) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %I64d pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) (npixels - i));
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at row %lu (short %llu pixels)",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) (npixels - i));
+#endif
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ return (0);
+ }
+ }
+ (*sp->tfunc)(sp, op, npixels);
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ return (1);
+}
+
+/*
+ * Decode a strip of pixels. We break it into rows to
+ * maintain synchrony with the encode algorithm, which
+ * is row by row.
+ */
+static int
+LogLuvDecodeStrip(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ tmsize_t rowlen = TIFFScanlineSize(tif);
+
+ if (rowlen == 0)
+ return 0;
+
+ assert(cc%rowlen == 0);
+ while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) {
+ bp += rowlen;
+ cc -= rowlen;
+ }
+ return (cc == 0);
+}
+
+/*
+ * Decode a tile of pixels. We break it into rows to
+ * maintain synchrony with the encode algorithm, which
+ * is row by row.
+ */
+static int
+LogLuvDecodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ tmsize_t rowlen = TIFFTileRowSize(tif);
+
+ if (rowlen == 0)
+ return 0;
+
+ assert(cc%rowlen == 0);
+ while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) {
+ bp += rowlen;
+ cc -= rowlen;
+ }
+ return (cc == 0);
+}
+
+/*
+ * Encode a row of 16-bit pixels.
+ */
+static int
+LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "LogL16Encode";
+ LogLuvState* sp = EncoderState(tif);
+ int shft;
+ tmsize_t i;
+ tmsize_t j;
+ tmsize_t npixels;
+ uint8* op;
+ int16* tp;
+ int16 b;
+ tmsize_t occ;
+ int rc=0, mask;
+ tmsize_t beg;
+
+ assert(s == 0);
+ assert(sp != NULL);
+ npixels = cc / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
+ tp = (int16*) bp;
+ else {
+ tp = (int16*) sp->tbuf;
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ (*sp->tfunc)(sp, bp, npixels);
+ }
+ /* compress each byte string */
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ for (shft = 8; shft >= 0; shft -=8) {
+ for (i = 0; i < npixels; i += rc) {
+ if (occ < 4) {
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+ mask = 0xff << shft; /* find next run */
+ for (beg = i; beg < npixels; beg += rc) {
+ b = (int16) (tp[beg] & mask);
+ rc = 1;
+ while (rc < 127+2 && beg+rc < npixels &&
+ (tp[beg+rc] & mask) == b)
+ rc++;
+ if (rc >= MINRUN)
+ break; /* long enough */
+ }
+ if (beg-i > 1 && beg-i < MINRUN) {
+ b = (int16) (tp[i] & mask);/*check short run */
+ j = i+1;
+ while ((tp[j++] & mask) == b)
+ if (j == beg) {
+ *op++ = (uint8)(128-2+j-i);
+ *op++ = (uint8)(b >> shft);
+ occ -= 2;
+ i = beg;
+ break;
+ }
+ }
+ while (i < beg) { /* write out non-run */
+ if ((j = beg-i) > 127) j = 127;
+ if (occ < j+3) {
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+ *op++ = (uint8) j; occ--;
+ while (j--) {
+ *op++ = (uint8) (tp[i++] >> shft & 0xff);
+ occ--;
+ }
+ }
+ if (rc >= MINRUN) { /* write out run */
+ *op++ = (uint8) (128-2+rc);
+ *op++ = (uint8) (tp[beg] >> shft & 0xff);
+ occ -= 2;
+ } else
+ rc = 0;
+ }
+ }
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+
+ return (1);
+}
+
+/*
+ * Encode a row of 24-bit pixels.
+ */
+static int
+LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "LogLuvEncode24";
+ LogLuvState* sp = EncoderState(tif);
+ tmsize_t i;
+ tmsize_t npixels;
+ tmsize_t occ;
+ uint8* op;
+ uint32* tp;
+
+ assert(s == 0);
+ assert(sp != NULL);
+ npixels = cc / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_RAW)
+ tp = (uint32*) bp;
+ else {
+ tp = (uint32*) sp->tbuf;
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ (*sp->tfunc)(sp, bp, npixels);
+ }
+ /* write out encoded pixels */
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ for (i = npixels; i--; ) {
+ if (occ < 3) {
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+ *op++ = (uint8)(*tp >> 16);
+ *op++ = (uint8)(*tp >> 8 & 0xff);
+ *op++ = (uint8)(*tp++ & 0xff);
+ occ -= 3;
+ }
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+
+ return (1);
+}
+
+/*
+ * Encode a row of 32-bit pixels.
+ */
+static int
+LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "LogLuvEncode32";
+ LogLuvState* sp = EncoderState(tif);
+ int shft;
+ tmsize_t i;
+ tmsize_t j;
+ tmsize_t npixels;
+ uint8* op;
+ uint32* tp;
+ uint32 b;
+ tmsize_t occ;
+ int rc=0, mask;
+ tmsize_t beg;
+
+ assert(s == 0);
+ assert(sp != NULL);
+
+ npixels = cc / sp->pixel_size;
+
+ if (sp->user_datafmt == SGILOGDATAFMT_RAW)
+ tp = (uint32*) bp;
+ else {
+ tp = (uint32*) sp->tbuf;
+ if(sp->tbuflen < npixels) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Translation buffer too short");
+ return (0);
+ }
+ (*sp->tfunc)(sp, bp, npixels);
+ }
+ /* compress each byte string */
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ for (shft = 24; shft >= 0; shft -=8) {
+ for (i = 0; i < npixels; i += rc) {
+ if (occ < 4) {
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+ mask = 0xff << shft; /* find next run */
+ for (beg = i; beg < npixels; beg += rc) {
+ b = tp[beg] & mask;
+ rc = 1;
+ while (rc < 127+2 && beg+rc < npixels &&
+ (tp[beg+rc] & mask) == b)
+ rc++;
+ if (rc >= MINRUN)
+ break; /* long enough */
+ }
+ if (beg-i > 1 && beg-i < MINRUN) {
+ b = tp[i] & mask; /* check short run */
+ j = i+1;
+ while ((tp[j++] & mask) == b)
+ if (j == beg) {
+ *op++ = (uint8)(128-2+j-i);
+ *op++ = (uint8)(b >> shft);
+ occ -= 2;
+ i = beg;
+ break;
+ }
+ }
+ while (i < beg) { /* write out non-run */
+ if ((j = beg-i) > 127) j = 127;
+ if (occ < j+3) {
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ occ = tif->tif_rawdatasize - tif->tif_rawcc;
+ }
+ *op++ = (uint8) j; occ--;
+ while (j--) {
+ *op++ = (uint8)(tp[i++] >> shft & 0xff);
+ occ--;
+ }
+ }
+ if (rc >= MINRUN) { /* write out run */
+ *op++ = (uint8) (128-2+rc);
+ *op++ = (uint8)(tp[beg] >> shft & 0xff);
+ occ -= 2;
+ } else
+ rc = 0;
+ }
+ }
+ tif->tif_rawcp = op;
+ tif->tif_rawcc = tif->tif_rawdatasize - occ;
+
+ return (1);
+}
+
+/*
+ * Encode a strip of pixels. We break it into rows to
+ * avoid encoding runs across row boundaries.
+ */
+static int
+LogLuvEncodeStrip(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ tmsize_t rowlen = TIFFScanlineSize(tif);
+
+ if (rowlen == 0)
+ return 0;
+
+ assert(cc%rowlen == 0);
+ while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) {
+ bp += rowlen;
+ cc -= rowlen;
+ }
+ return (cc == 0);
+}
+
+/*
+ * Encode a tile of pixels. We break it into rows to
+ * avoid encoding runs across row boundaries.
+ */
+static int
+LogLuvEncodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ tmsize_t rowlen = TIFFTileRowSize(tif);
+
+ if (rowlen == 0)
+ return 0;
+
+ assert(cc%rowlen == 0);
+ while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) {
+ bp += rowlen;
+ cc -= rowlen;
+ }
+ return (cc == 0);
+}
+
+/*
+ * Encode/Decode functions for converting to and from user formats.
+ */
+
+#include "uvcode.h"
+
+#ifndef UVSCALE
+#define U_NEU 0.210526316
+#define V_NEU 0.473684211
+#define UVSCALE 410.
+#endif
+
+#ifndef M_LN2
+#define M_LN2 0.69314718055994530942
+#endif
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
+#undef log2 /* Conflict with C'99 function */
+#define log2(x) ((1./M_LN2)*log(x))
+#undef exp2 /* Conflict with C'99 function */
+#define exp2(x) exp(M_LN2*(x))
+
+#define itrunc(x,m) ((m)==SGILOGENCODE_NODITHER ? \
+ (int)(x) : \
+ (int)((x) + rand()*(1./RAND_MAX) - .5))
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+double
+LogL16toY(int p16) /* compute luminance from 16-bit LogL */
+{
+ int Le = p16 & 0x7fff;
+ double Y;
+
+ if (!Le)
+ return (0.);
+ Y = exp(M_LN2/256.*(Le+.5) - M_LN2*64.);
+ return (!(p16 & 0x8000) ? Y : -Y);
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+int
+LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */
+{
+ if (Y >= 1.8371976e19)
+ return (0x7fff);
+ if (Y <= -1.8371976e19)
+ return (0xffff);
+ if (Y > 5.4136769e-20)
+ return itrunc(256.*(log2(Y) + 64.), em);
+ if (Y < -5.4136769e-20)
+ return (~0x7fff | itrunc(256.*(log2(-Y) + 64.), em));
+ return (0);
+}
+
+static void
+L16toY(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ int16* l16 = (int16*) sp->tbuf;
+ float* yp = (float*) op;
+
+ while (n-- > 0)
+ *yp++ = (float)LogL16toY(*l16++);
+}
+
+static void
+L16toGry(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ int16* l16 = (int16*) sp->tbuf;
+ uint8* gp = (uint8*) op;
+
+ while (n-- > 0) {
+ double Y = LogL16toY(*l16++);
+ *gp++ = (uint8) ((Y <= 0.) ? 0 : (Y >= 1.) ? 255 : (int)(256.*sqrt(Y)));
+ }
+}
+
+static void
+L16fromY(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ int16* l16 = (int16*) sp->tbuf;
+ float* yp = (float*) op;
+
+ while (n-- > 0)
+ *l16++ = (int16) (LogL16fromY(*yp++, sp->encode_meth));
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+void
+XYZtoRGB24(float xyz[3], uint8 rgb[3])
+{
+ double r, g, b;
+ /* assume CCIR-709 primaries */
+ r = 2.690*xyz[0] + -1.276*xyz[1] + -0.414*xyz[2];
+ g = -1.022*xyz[0] + 1.978*xyz[1] + 0.044*xyz[2];
+ b = 0.061*xyz[0] + -0.224*xyz[1] + 1.163*xyz[2];
+ /* assume 2.0 gamma for speed */
+ /* could use integer sqrt approx., but this is probably faster */
+ rgb[0] = (uint8)((r<=0.) ? 0 : (r >= 1.) ? 255 : (int)(256.*sqrt(r)));
+ rgb[1] = (uint8)((g<=0.) ? 0 : (g >= 1.) ? 255 : (int)(256.*sqrt(g)));
+ rgb[2] = (uint8)((b<=0.) ? 0 : (b >= 1.) ? 255 : (int)(256.*sqrt(b)));
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+double
+LogL10toY(int p10) /* compute luminance from 10-bit LogL */
+{
+ if (p10 == 0)
+ return (0.);
+ return (exp(M_LN2/64.*(p10+.5) - M_LN2*12.));
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+int
+LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */
+{
+ if (Y >= 15.742)
+ return (0x3ff);
+ else if (Y <= .00024283)
+ return (0);
+ else
+ return itrunc(64.*(log2(Y) + 12.), em);
+}
+
+#define NANGLES 100
+#define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \
+ * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES )
+
+static int
+oog_encode(double u, double v) /* encode out-of-gamut chroma */
+{
+ static int oog_table[NANGLES];
+ static int initialized = 0;
+ register int i;
+
+ if (!initialized) { /* set up perimeter table */
+ double eps[NANGLES], ua, va, ang, epsa;
+ int ui, vi, ustep;
+ for (i = NANGLES; i--; )
+ eps[i] = 2.;
+ for (vi = UV_NVS; vi--; ) {
+ va = UV_VSTART + (vi+.5)*UV_SQSIZ;
+ ustep = uv_row[vi].nus-1;
+ if (vi == UV_NVS-1 || vi == 0 || ustep <= 0)
+ ustep = 1;
+ for (ui = uv_row[vi].nus-1; ui >= 0; ui -= ustep) {
+ ua = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
+ ang = uv2ang(ua, va);
+ i = (int) ang;
+ epsa = fabs(ang - (i+.5));
+ if (epsa < eps[i]) {
+ oog_table[i] = uv_row[vi].ncum + ui;
+ eps[i] = epsa;
+ }
+ }
+ }
+ for (i = NANGLES; i--; ) /* fill any holes */
+ if (eps[i] > 1.5) {
+ int i1, i2;
+ for (i1 = 1; i1 < NANGLES/2; i1++)
+ if (eps[(i+i1)%NANGLES] < 1.5)
+ break;
+ for (i2 = 1; i2 < NANGLES/2; i2++)
+ if (eps[(i+NANGLES-i2)%NANGLES] < 1.5)
+ break;
+ if (i1 < i2)
+ oog_table[i] =
+ oog_table[(i+i1)%NANGLES];
+ else
+ oog_table[i] =
+ oog_table[(i+NANGLES-i2)%NANGLES];
+ }
+ initialized = 1;
+ }
+ i = (int) uv2ang(u, v); /* look up hue angle */
+ return (oog_table[i]);
+}
+
+#undef uv2ang
+#undef NANGLES
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+int
+uv_encode(double u, double v, int em) /* encode (u',v') coordinates */
+{
+ register int vi, ui;
+
+ if (v < UV_VSTART)
+ return oog_encode(u, v);
+ vi = itrunc((v - UV_VSTART)*(1./UV_SQSIZ), em);
+ if (vi >= UV_NVS)
+ return oog_encode(u, v);
+ if (u < uv_row[vi].ustart)
+ return oog_encode(u, v);
+ ui = itrunc((u - uv_row[vi].ustart)*(1./UV_SQSIZ), em);
+ if (ui >= uv_row[vi].nus)
+ return oog_encode(u, v);
+
+ return (uv_row[vi].ncum + ui);
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+int
+uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
+{
+ int upper, lower;
+ register int ui, vi;
+
+ if (c < 0 || c >= UV_NDIVS)
+ return (-1);
+ lower = 0; /* binary search */
+ upper = UV_NVS;
+ while (upper - lower > 1) {
+ vi = (lower + upper) >> 1;
+ ui = c - uv_row[vi].ncum;
+ if (ui > 0)
+ lower = vi;
+ else if (ui < 0)
+ upper = vi;
+ else {
+ lower = vi;
+ break;
+ }
+ }
+ vi = lower;
+ ui = c - uv_row[vi].ncum;
+ *up = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
+ *vp = UV_VSTART + (vi+.5)*UV_SQSIZ;
+ return (0);
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+void
+LogLuv24toXYZ(uint32 p, float XYZ[3])
+{
+ int Ce;
+ double L, u, v, s, x, y;
+ /* decode luminance */
+ L = LogL10toY(p>>14 & 0x3ff);
+ if (L <= 0.) {
+ XYZ[0] = XYZ[1] = XYZ[2] = 0.;
+ return;
+ }
+ /* decode color */
+ Ce = p & 0x3fff;
+ if (uv_decode(&u, &v, Ce) < 0) {
+ u = U_NEU; v = V_NEU;
+ }
+ s = 1./(6.*u - 16.*v + 12.);
+ x = 9.*u * s;
+ y = 4.*v * s;
+ /* convert to XYZ */
+ XYZ[0] = (float)(x/y * L);
+ XYZ[1] = (float)L;
+ XYZ[2] = (float)((1.-x-y)/y * L);
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+uint32
+LogLuv24fromXYZ(float XYZ[3], int em)
+{
+ int Le, Ce;
+ double u, v, s;
+ /* encode luminance */
+ Le = LogL10fromY(XYZ[1], em);
+ /* encode color */
+ s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
+ if (!Le || s <= 0.) {
+ u = U_NEU;
+ v = V_NEU;
+ } else {
+ u = 4.*XYZ[0] / s;
+ v = 9.*XYZ[1] / s;
+ }
+ Ce = uv_encode(u, v, em);
+ if (Ce < 0) /* never happens */
+ Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
+ /* combine encodings */
+ return (Le << 14 | Ce);
+}
+
+static void
+Luv24toXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ float* xyz = (float*) op;
+
+ while (n-- > 0) {
+ LogLuv24toXYZ(*luv, xyz);
+ xyz += 3;
+ luv++;
+ }
+}
+
+static void
+Luv24toLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ int16* luv3 = (int16*) op;
+
+ while (n-- > 0) {
+ double u, v;
+
+ *luv3++ = (int16)((*luv >> 12 & 0xffd) + 13314);
+ if (uv_decode(&u, &v, *luv&0x3fff) < 0) {
+ u = U_NEU;
+ v = V_NEU;
+ }
+ *luv3++ = (int16)(u * (1L<<15));
+ *luv3++ = (int16)(v * (1L<<15));
+ luv++;
+ }
+}
+
+static void
+Luv24toRGB(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ uint8* rgb = (uint8*) op;
+
+ while (n-- > 0) {
+ float xyz[3];
+
+ LogLuv24toXYZ(*luv++, xyz);
+ XYZtoRGB24(xyz, rgb);
+ rgb += 3;
+ }
+}
+
+static void
+Luv24fromXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ float* xyz = (float*) op;
+
+ while (n-- > 0) {
+ *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
+ xyz += 3;
+ }
+}
+
+static void
+Luv24fromLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ int16* luv3 = (int16*) op;
+
+ while (n-- > 0) {
+ int Le, Ce;
+
+ if (luv3[0] <= 0)
+ Le = 0;
+ else if (luv3[0] >= (1<<12)+3314)
+ Le = (1<<10) - 1;
+ else if (sp->encode_meth == SGILOGENCODE_NODITHER)
+ Le = (luv3[0]-3314) >> 2;
+ else
+ Le = itrunc(.25*(luv3[0]-3314.), sp->encode_meth);
+
+ Ce = uv_encode((luv3[1]+.5)/(1<<15), (luv3[2]+.5)/(1<<15),
+ sp->encode_meth);
+ if (Ce < 0) /* never happens */
+ Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
+ *luv++ = (uint32)Le << 14 | Ce;
+ luv3 += 3;
+ }
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+void
+LogLuv32toXYZ(uint32 p, float XYZ[3])
+{
+ double L, u, v, s, x, y;
+ /* decode luminance */
+ L = LogL16toY((int)p >> 16);
+ if (L <= 0.) {
+ XYZ[0] = XYZ[1] = XYZ[2] = 0.;
+ return;
+ }
+ /* decode color */
+ u = 1./UVSCALE * ((p>>8 & 0xff) + .5);
+ v = 1./UVSCALE * ((p & 0xff) + .5);
+ s = 1./(6.*u - 16.*v + 12.);
+ x = 9.*u * s;
+ y = 4.*v * s;
+ /* convert to XYZ */
+ XYZ[0] = (float)(x/y * L);
+ XYZ[1] = (float)L;
+ XYZ[2] = (float)((1.-x-y)/y * L);
+}
+
+#if !LOGLUV_PUBLIC
+static
+#endif
+uint32
+LogLuv32fromXYZ(float XYZ[3], int em)
+{
+ unsigned int Le, ue, ve;
+ double u, v, s;
+ /* encode luminance */
+ Le = (unsigned int)LogL16fromY(XYZ[1], em);
+ /* encode color */
+ s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
+ if (!Le || s <= 0.) {
+ u = U_NEU;
+ v = V_NEU;
+ } else {
+ u = 4.*XYZ[0] / s;
+ v = 9.*XYZ[1] / s;
+ }
+ if (u <= 0.) ue = 0;
+ else ue = itrunc(UVSCALE*u, em);
+ if (ue > 255) ue = 255;
+ if (v <= 0.) ve = 0;
+ else ve = itrunc(UVSCALE*v, em);
+ if (ve > 255) ve = 255;
+ /* combine encodings */
+ return (Le << 16 | ue << 8 | ve);
+}
+
+static void
+Luv32toXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ float* xyz = (float*) op;
+
+ while (n-- > 0) {
+ LogLuv32toXYZ(*luv++, xyz);
+ xyz += 3;
+ }
+}
+
+static void
+Luv32toLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ int16* luv3 = (int16*) op;
+
+ while (n-- > 0) {
+ double u, v;
+
+ *luv3++ = (int16)(*luv >> 16);
+ u = 1./UVSCALE * ((*luv>>8 & 0xff) + .5);
+ v = 1./UVSCALE * ((*luv & 0xff) + .5);
+ *luv3++ = (int16)(u * (1L<<15));
+ *luv3++ = (int16)(v * (1L<<15));
+ luv++;
+ }
+}
+
+static void
+Luv32toRGB(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ uint8* rgb = (uint8*) op;
+
+ while (n-- > 0) {
+ float xyz[3];
+
+ LogLuv32toXYZ(*luv++, xyz);
+ XYZtoRGB24(xyz, rgb);
+ rgb += 3;
+ }
+}
+
+static void
+Luv32fromXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ float* xyz = (float*) op;
+
+ while (n-- > 0) {
+ *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
+ xyz += 3;
+ }
+}
+
+static void
+Luv32fromLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ uint32* luv = (uint32*) sp->tbuf;
+ int16* luv3 = (int16*) op;
+
+ if (sp->encode_meth == SGILOGENCODE_NODITHER) {
+ while (n-- > 0) {
+ *luv++ = (uint32)luv3[0] << 16 |
+ (luv3[1]*(uint32)(UVSCALE+.5) >> 7 & 0xff00) |
+ (luv3[2]*(uint32)(UVSCALE+.5) >> 15 & 0xff);
+ luv3 += 3;
+ }
+ return;
+ }
+ while (n-- > 0) {
+ *luv++ = (uint32)luv3[0] << 16 |
+ (itrunc(luv3[1]*(UVSCALE/(1<<15)), sp->encode_meth) << 8 & 0xff00) |
+ (itrunc(luv3[2]*(UVSCALE/(1<<15)), sp->encode_meth) & 0xff);
+ luv3 += 3;
+ }
+}
+
+static void
+_logLuvNop(LogLuvState* sp, uint8* op, tmsize_t n)
+{
+ (void) sp; (void) op; (void) n;
+}
+
+static int
+LogL16GuessDataFmt(TIFFDirectory *td)
+{
+#define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f))
+ switch (PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) {
+ case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
+ return (SGILOGDATAFMT_FLOAT);
+ case PACK(1, 16, SAMPLEFORMAT_VOID):
+ case PACK(1, 16, SAMPLEFORMAT_INT):
+ case PACK(1, 16, SAMPLEFORMAT_UINT):
+ return (SGILOGDATAFMT_16BIT);
+ case PACK(1, 8, SAMPLEFORMAT_VOID):
+ case PACK(1, 8, SAMPLEFORMAT_UINT):
+ return (SGILOGDATAFMT_8BIT);
+ }
+#undef PACK
+ return (SGILOGDATAFMT_UNKNOWN);
+}
+
+
+#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
+#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
+
+static tmsize_t
+multiply_ms(tmsize_t m1, tmsize_t m2)
+{
+ if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
+ return 0;
+ return m1 * m2;
+}
+
+static int
+LogL16InitState(TIFF* tif)
+{
+ static const char module[] = "LogL16InitState";
+ TIFFDirectory *td = &tif->tif_dir;
+ LogLuvState* sp = DecoderState(tif);
+
+ assert(sp != NULL);
+ assert(td->td_photometric == PHOTOMETRIC_LOGL);
+
+ if( td->td_samplesperpixel != 1 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Sorry, can not handle LogL image with %s=%d",
+ "Samples/pixel", td->td_samplesperpixel);
+ return 0;
+ }
+
+ /* for some reason, we can't do this in TIFFInitLogL16 */
+ if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
+ sp->user_datafmt = LogL16GuessDataFmt(td);
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->pixel_size = sizeof (float);
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->pixel_size = sizeof (int16);
+ break;
+ case SGILOGDATAFMT_8BIT:
+ sp->pixel_size = sizeof (uint8);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No support for converting user data format to LogL");
+ return (0);
+ }
+ if( isTiled(tif) )
+ sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
+ else if( td->td_rowsperstrip < td->td_imagelength )
+ sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
+ else
+ sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
+ if (multiply_ms(sp->tbuflen, sizeof (int16)) == 0 ||
+ (sp->tbuf = (uint8*) _TIFFmalloc(sp->tbuflen * sizeof (int16))) == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer");
+ return (0);
+ }
+ return (1);
+}
+
+static int
+LogLuvGuessDataFmt(TIFFDirectory *td)
+{
+ int guess;
+
+ /*
+ * If the user didn't tell us their datafmt,
+ * take our best guess from the bitspersample.
+ */
+#define PACK(a,b) (((a)<<3)|(b))
+ switch (PACK(td->td_bitspersample, td->td_sampleformat)) {
+ case PACK(32, SAMPLEFORMAT_IEEEFP):
+ guess = SGILOGDATAFMT_FLOAT;
+ break;
+ case PACK(32, SAMPLEFORMAT_VOID):
+ case PACK(32, SAMPLEFORMAT_UINT):
+ case PACK(32, SAMPLEFORMAT_INT):
+ guess = SGILOGDATAFMT_RAW;
+ break;
+ case PACK(16, SAMPLEFORMAT_VOID):
+ case PACK(16, SAMPLEFORMAT_INT):
+ case PACK(16, SAMPLEFORMAT_UINT):
+ guess = SGILOGDATAFMT_16BIT;
+ break;
+ case PACK( 8, SAMPLEFORMAT_VOID):
+ case PACK( 8, SAMPLEFORMAT_UINT):
+ guess = SGILOGDATAFMT_8BIT;
+ break;
+ default:
+ guess = SGILOGDATAFMT_UNKNOWN;
+ break;
+#undef PACK
+ }
+ /*
+ * Double-check samples per pixel.
+ */
+ switch (td->td_samplesperpixel) {
+ case 1:
+ if (guess != SGILOGDATAFMT_RAW)
+ guess = SGILOGDATAFMT_UNKNOWN;
+ break;
+ case 3:
+ if (guess == SGILOGDATAFMT_RAW)
+ guess = SGILOGDATAFMT_UNKNOWN;
+ break;
+ default:
+ guess = SGILOGDATAFMT_UNKNOWN;
+ break;
+ }
+ return (guess);
+}
+
+static int
+LogLuvInitState(TIFF* tif)
+{
+ static const char module[] = "LogLuvInitState";
+ TIFFDirectory* td = &tif->tif_dir;
+ LogLuvState* sp = DecoderState(tif);
+
+ assert(sp != NULL);
+ assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
+
+ /* for some reason, we can't do this in TIFFInitLogLuv */
+ if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "SGILog compression cannot handle non-contiguous data");
+ return (0);
+ }
+ if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
+ sp->user_datafmt = LogLuvGuessDataFmt(td);
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->pixel_size = 3*sizeof (float);
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->pixel_size = 3*sizeof (int16);
+ break;
+ case SGILOGDATAFMT_RAW:
+ sp->pixel_size = sizeof (uint32);
+ break;
+ case SGILOGDATAFMT_8BIT:
+ sp->pixel_size = 3*sizeof (uint8);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No support for converting user data format to LogLuv");
+ return (0);
+ }
+ if( isTiled(tif) )
+ sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
+ else if( td->td_rowsperstrip < td->td_imagelength )
+ sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
+ else
+ sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
+ if (multiply_ms(sp->tbuflen, sizeof (uint32)) == 0 ||
+ (sp->tbuf = (uint8*) _TIFFmalloc(sp->tbuflen * sizeof (uint32))) == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer");
+ return (0);
+ }
+ return (1);
+}
+
+static int
+LogLuvFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+static int
+LogLuvSetupDecode(TIFF* tif)
+{
+ static const char module[] = "LogLuvSetupDecode";
+ LogLuvState* sp = DecoderState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ switch (td->td_photometric) {
+ case PHOTOMETRIC_LOGLUV:
+ if (!LogLuvInitState(tif))
+ break;
+ if (td->td_compression == COMPRESSION_SGILOG24) {
+ tif->tif_decoderow = LogLuvDecode24;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = Luv24toXYZ;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->tfunc = Luv24toLuv48;
+ break;
+ case SGILOGDATAFMT_8BIT:
+ sp->tfunc = Luv24toRGB;
+ break;
+ }
+ } else {
+ tif->tif_decoderow = LogLuvDecode32;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = Luv32toXYZ;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->tfunc = Luv32toLuv48;
+ break;
+ case SGILOGDATAFMT_8BIT:
+ sp->tfunc = Luv32toRGB;
+ break;
+ }
+ }
+ return (1);
+ case PHOTOMETRIC_LOGL:
+ if (!LogL16InitState(tif))
+ break;
+ tif->tif_decoderow = LogL16Decode;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = L16toY;
+ break;
+ case SGILOGDATAFMT_8BIT:
+ sp->tfunc = L16toGry;
+ break;
+ }
+ return (1);
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Inappropriate photometric interpretation %d for SGILog compression; %s",
+ td->td_photometric, "must be either LogLUV or LogL");
+ break;
+ }
+ return (0);
+}
+
+static int
+LogLuvSetupEncode(TIFF* tif)
+{
+ static const char module[] = "LogLuvSetupEncode";
+ LogLuvState* sp = EncoderState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ switch (td->td_photometric) {
+ case PHOTOMETRIC_LOGLUV:
+ if (!LogLuvInitState(tif))
+ break;
+ if (td->td_compression == COMPRESSION_SGILOG24) {
+ tif->tif_encoderow = LogLuvEncode24;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = Luv24fromXYZ;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->tfunc = Luv24fromLuv48;
+ break;
+ case SGILOGDATAFMT_RAW:
+ break;
+ default:
+ goto notsupported;
+ }
+ } else {
+ tif->tif_encoderow = LogLuvEncode32;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = Luv32fromXYZ;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ sp->tfunc = Luv32fromLuv48;
+ break;
+ case SGILOGDATAFMT_RAW:
+ break;
+ default:
+ goto notsupported;
+ }
+ }
+ break;
+ case PHOTOMETRIC_LOGL:
+ if (!LogL16InitState(tif))
+ break;
+ tif->tif_encoderow = LogL16Encode;
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ sp->tfunc = L16fromY;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ break;
+ default:
+ goto notsupported;
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Inappropriate photometric interpretation %d for SGILog compression; %s",
+ td->td_photometric, "must be either LogLUV or LogL");
+ break;
+ }
+ sp->encoder_state = 1;
+ return (1);
+notsupported:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "SGILog compression supported only for %s, or raw data",
+ td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
+ return (0);
+}
+
+static void
+LogLuvClose(TIFF* tif)
+{
+ LogLuvState* sp = (LogLuvState*) tif->tif_data;
+ TIFFDirectory *td = &tif->tif_dir;
+
+ assert(sp != 0);
+ /*
+ * For consistency, we always want to write out the same
+ * bitspersample and sampleformat for our TIFF file,
+ * regardless of the data format being used by the application.
+ * Since this routine is called after tags have been set but
+ * before they have been recorded in the file, we reset them here.
+ * Note: this is really a nasty approach. See PixarLogClose
+ */
+ if( sp->encoder_state )
+ {
+ /* See PixarLogClose. Might avoid issues with tags whose size depends
+ * on those below, but not completely sure this is enough. */
+ td->td_samplesperpixel =
+ (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
+ td->td_bitspersample = 16;
+ td->td_sampleformat = SAMPLEFORMAT_INT;
+ }
+}
+
+static void
+LogLuvCleanup(TIFF* tif)
+{
+ LogLuvState* sp = (LogLuvState *)tif->tif_data;
+
+ assert(sp != 0);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->tbuf)
+ _TIFFfree(sp->tbuf);
+ _TIFFfree(sp);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+LogLuvVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "LogLuvVSetField";
+ LogLuvState* sp = DecoderState(tif);
+ int bps, fmt;
+
+ switch (tag) {
+ case TIFFTAG_SGILOGDATAFMT:
+ sp->user_datafmt = (int) va_arg(ap, int);
+ /*
+ * Tweak the TIFF header so that the rest of libtiff knows what
+ * size of data will be passed between app and library, and
+ * assume that the app knows what it is doing and is not
+ * confused by these header manipulations...
+ */
+ switch (sp->user_datafmt) {
+ case SGILOGDATAFMT_FLOAT:
+ bps = 32;
+ fmt = SAMPLEFORMAT_IEEEFP;
+ break;
+ case SGILOGDATAFMT_16BIT:
+ bps = 16;
+ fmt = SAMPLEFORMAT_INT;
+ break;
+ case SGILOGDATAFMT_RAW:
+ bps = 32;
+ fmt = SAMPLEFORMAT_UINT;
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
+ break;
+ case SGILOGDATAFMT_8BIT:
+ bps = 8;
+ fmt = SAMPLEFORMAT_UINT;
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Unknown data format %d for LogLuv compression",
+ sp->user_datafmt);
+ return (0);
+ }
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
+ /*
+ * Must recalculate sizes should bits/sample change.
+ */
+ tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t) -1;
+ tif->tif_scanlinesize = TIFFScanlineSize(tif);
+ return (1);
+ case TIFFTAG_SGILOGENCODE:
+ sp->encode_meth = (int) va_arg(ap, int);
+ if (sp->encode_meth != SGILOGENCODE_NODITHER &&
+ sp->encode_meth != SGILOGENCODE_RANDITHER) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Unknown encoding %d for LogLuv compression",
+ sp->encode_meth);
+ return (0);
+ }
+ return (1);
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+}
+
+static int
+LogLuvVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ LogLuvState *sp = (LogLuvState *)tif->tif_data;
+
+ switch (tag) {
+ case TIFFTAG_SGILOGDATAFMT:
+ *va_arg(ap, int*) = sp->user_datafmt;
+ return (1);
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+}
+
+static const TIFFField LogLuvFields[] = {
+ { TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL},
+ { TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL}
+};
+
+int
+TIFFInitSGILog(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitSGILog";
+ LogLuvState* sp;
+
+ assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, LogLuvFields,
+ TIFFArrayCount(LogLuvFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging SGILog codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LogLuvState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = (LogLuvState*) tif->tif_data;
+ _TIFFmemset((void*)sp, 0, sizeof (*sp));
+ sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
+ sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ?
+ SGILOGENCODE_RANDITHER : SGILOGENCODE_NODITHER;
+ sp->tfunc = _logLuvNop;
+
+ /*
+ * Install codec methods.
+ * NB: tif_decoderow & tif_encoderow are filled
+ * in at setup time.
+ */
+ tif->tif_fixuptags = LogLuvFixupTags;
+ tif->tif_setupdecode = LogLuvSetupDecode;
+ tif->tif_decodestrip = LogLuvDecodeStrip;
+ tif->tif_decodetile = LogLuvDecodeTile;
+ tif->tif_setupencode = LogLuvSetupEncode;
+ tif->tif_encodestrip = LogLuvEncodeStrip;
+ tif->tif_encodetile = LogLuvEncodeTile;
+ tif->tif_close = LogLuvClose;
+ tif->tif_cleanup = LogLuvCleanup;
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
+
+ return (1);
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: No space for LogLuv state block", tif->tif_name);
+ return (0);
+}
+#endif /* LOGLUV_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_lzma.c b/test/monniaux/tiff-4.0.10/tif_lzma.c
new file mode 100644
index 00000000..3f6096b6
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_lzma.c
@@ -0,0 +1,500 @@
+/*
+ * Copyright (c) 2010, Andrey Kiselev <dron@ak4719.spb.edu>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef LZMA_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * LZMA2 Compression Support
+ *
+ * You need an LZMA2 SDK to link with. See http://tukaani.org/xz/ for details.
+ *
+ * The codec is derived from ZLIB codec (tif_zip.c).
+ */
+
+#include "tif_predict.h"
+#include "lzma.h"
+
+#include <stdio.h>
+
+/*
+ * State block for each open TIFF file using LZMA2 compression/decompression.
+ */
+typedef struct {
+ TIFFPredictorState predict;
+ lzma_stream stream;
+ lzma_filter filters[LZMA_FILTERS_MAX + 1];
+ lzma_options_delta opt_delta; /* delta filter options */
+ lzma_options_lzma opt_lzma; /* LZMA2 filter options */
+ int preset; /* compression level */
+ lzma_check check; /* type of the integrity check */
+ int state; /* state flags */
+#define LSTATE_INIT_DECODE 0x01
+#define LSTATE_INIT_ENCODE 0x02
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+} LZMAState;
+
+#define LState(tif) ((LZMAState*) (tif)->tif_data)
+#define DecoderState(tif) LState(tif)
+#define EncoderState(tif) LState(tif)
+
+static int LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static const char *
+LZMAStrerror(lzma_ret ret)
+{
+ switch (ret) {
+ case LZMA_OK:
+ return "operation completed successfully";
+ case LZMA_STREAM_END:
+ return "end of stream was reached";
+ case LZMA_NO_CHECK:
+ return "input stream has no integrity check";
+ case LZMA_UNSUPPORTED_CHECK:
+ return "cannot calculate the integrity check";
+ case LZMA_GET_CHECK:
+ return "integrity check type is now available";
+ case LZMA_MEM_ERROR:
+ return "cannot allocate memory";
+ case LZMA_MEMLIMIT_ERROR:
+ return "memory usage limit was reached";
+ case LZMA_FORMAT_ERROR:
+ return "file format not recognized";
+ case LZMA_OPTIONS_ERROR:
+ return "invalid or unsupported options";
+ case LZMA_DATA_ERROR:
+ return "data is corrupt";
+ case LZMA_BUF_ERROR:
+ return "no progress is possible (stream is truncated or corrupt)";
+ case LZMA_PROG_ERROR:
+ return "programming error";
+ default:
+ return "unidentified liblzma error";
+ }
+}
+
+static int
+LZMAFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return 1;
+}
+
+static int
+LZMASetupDecode(TIFF* tif)
+{
+ LZMAState* sp = DecoderState(tif);
+
+ assert(sp != NULL);
+
+ /* if we were last encoding, terminate this mode */
+ if (sp->state & LSTATE_INIT_ENCODE) {
+ lzma_end(&sp->stream);
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_DECODE;
+ return 1;
+}
+
+/*
+ * Setup state for decoding a strip.
+ */
+static int
+LZMAPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "LZMAPreDecode";
+ LZMAState* sp = DecoderState(tif);
+ lzma_ret ret;
+
+ (void) s;
+ assert(sp != NULL);
+
+ if( (sp->state & LSTATE_INIT_DECODE) == 0 )
+ tif->tif_setupdecode(tif);
+
+ sp->stream.next_in = tif->tif_rawdata;
+ sp->stream.avail_in = (size_t) tif->tif_rawcc;
+ if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Liblzma cannot deal with buffers this size");
+ return 0;
+ }
+
+ /*
+ * Disable memory limit when decoding. UINT64_MAX is a flag to disable
+ * the limit, we are passing (uint64_t)-1 which should be the same.
+ */
+ ret = lzma_stream_decoder(&sp->stream, (uint64_t)-1, 0);
+ if (ret != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error initializing the stream decoder, %s",
+ LZMAStrerror(ret));
+ return 0;
+ }
+ return 1;
+}
+
+static int
+LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "LZMADecode";
+ LZMAState* sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_DECODE);
+
+ sp->stream.next_in = tif->tif_rawcp;
+ sp->stream.avail_in = (size_t) tif->tif_rawcc;
+
+ sp->stream.next_out = op;
+ sp->stream.avail_out = (size_t) occ;
+ if ((tmsize_t)sp->stream.avail_out != occ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Liblzma cannot deal with buffers this size");
+ return 0;
+ }
+
+ do {
+ /*
+ * Save the current stream state to properly recover from the
+ * decoding errors later.
+ */
+ const uint8_t *next_in = sp->stream.next_in;
+ size_t avail_in = sp->stream.avail_in;
+
+ lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
+ if (ret == LZMA_STREAM_END)
+ break;
+ if (ret == LZMA_MEMLIMIT_ERROR) {
+ lzma_ret r = lzma_stream_decoder(&sp->stream,
+ lzma_memusage(&sp->stream), 0);
+ if (r != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error initializing the stream decoder, %s",
+ LZMAStrerror(r));
+ break;
+ }
+ sp->stream.next_in = next_in;
+ sp->stream.avail_in = avail_in;
+ continue;
+ }
+ if (ret != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Decoding error at scanline %lu, %s",
+ (unsigned long) tif->tif_row, LZMAStrerror(ret));
+ break;
+ }
+ } while (sp->stream.avail_out > 0);
+ if (sp->stream.avail_out != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %lu (short %lu bytes)",
+ (unsigned long) tif->tif_row, (unsigned long) sp->stream.avail_out);
+ return 0;
+ }
+
+ tif->tif_rawcp = (uint8 *)sp->stream.next_in; /* cast away const */
+ tif->tif_rawcc = sp->stream.avail_in;
+
+ return 1;
+}
+
+static int
+LZMASetupEncode(TIFF* tif)
+{
+ LZMAState* sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ if (sp->state & LSTATE_INIT_DECODE) {
+ lzma_end(&sp->stream);
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_ENCODE;
+ return 1;
+}
+
+/*
+ * Reset encoding state at the start of a strip.
+ */
+static int
+LZMAPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "LZMAPreEncode";
+ LZMAState *sp = EncoderState(tif);
+ lzma_ret ret;
+
+ (void) s;
+ assert(sp != NULL);
+ if( sp->state != LSTATE_INIT_ENCODE )
+ tif->tif_setupencode(tif);
+
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (size_t)tif->tif_rawdatasize;
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Liblzma cannot deal with buffers this size");
+ return 0;
+ }
+ ret = lzma_stream_encoder(&sp->stream, sp->filters, sp->check);
+ if (ret != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in lzma_stream_encoder(): %s", LZMAStrerror(ret));
+ return 0;
+ }
+ return 1;
+}
+
+/*
+ * Encode a chunk of pixels.
+ */
+static int
+LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "LZMAEncode";
+ LZMAState *sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_ENCODE);
+
+ (void) s;
+ sp->stream.next_in = bp;
+ sp->stream.avail_in = (size_t) cc;
+ if ((tmsize_t)sp->stream.avail_in != cc) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Liblzma cannot deal with buffers this size");
+ return 0;
+ }
+ do {
+ lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
+ if (ret != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Encoding error at scanline %lu, %s",
+ (unsigned long) tif->tif_row, LZMAStrerror(ret));
+ return 0;
+ }
+ if (sp->stream.avail_out == 0) {
+ tif->tif_rawcc = tif->tif_rawdatasize;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (size_t)tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in LZMAPreEncode */
+ }
+ } while (sp->stream.avail_in > 0);
+ return 1;
+}
+
+/*
+ * Finish off an encoded strip by flushing the last
+ * string and tacking on an End Of Information code.
+ */
+static int
+LZMAPostEncode(TIFF* tif)
+{
+ static const char module[] = "LZMAPostEncode";
+ LZMAState *sp = EncoderState(tif);
+ lzma_ret ret;
+
+ sp->stream.avail_in = 0;
+ do {
+ ret = lzma_code(&sp->stream, LZMA_FINISH);
+ switch (ret) {
+ case LZMA_STREAM_END:
+ case LZMA_OK:
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
+ tif->tif_rawcc =
+ tif->tif_rawdatasize - sp->stream.avail_out;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (size_t)tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module, "Liblzma error: %s",
+ LZMAStrerror(ret));
+ return 0;
+ }
+ } while (ret != LZMA_STREAM_END);
+ return 1;
+}
+
+static void
+LZMACleanup(TIFF* tif)
+{
+ LZMAState* sp = LState(tif);
+
+ assert(sp != 0);
+
+ (void)TIFFPredictorCleanup(tif);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->state) {
+ lzma_end(&sp->stream);
+ sp->state = 0;
+ }
+ _TIFFfree(sp);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+LZMAVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "LZMAVSetField";
+ LZMAState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_LZMAPRESET:
+ sp->preset = (int) va_arg(ap, int);
+ lzma_lzma_preset(&sp->opt_lzma, sp->preset);
+ if (sp->state & LSTATE_INIT_ENCODE) {
+ lzma_ret ret = lzma_stream_encoder(&sp->stream,
+ sp->filters,
+ sp->check);
+ if (ret != LZMA_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Liblzma error: %s",
+ LZMAStrerror(ret));
+ }
+ }
+ return 1;
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+ /*NOTREACHED*/
+}
+
+static int
+LZMAVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ LZMAState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_LZMAPRESET:
+ *va_arg(ap, int*) = sp->preset;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return 1;
+}
+
+static const TIFFField lzmaFields[] = {
+ { TIFFTAG_LZMAPRESET, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED,
+ FIELD_PSEUDO, TRUE, FALSE, "LZMA2 Compression Preset", NULL },
+};
+
+int
+TIFFInitLZMA(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitLZMA";
+ LZMAState* sp;
+ lzma_stream tmp_stream = LZMA_STREAM_INIT;
+
+ assert( scheme == COMPRESSION_LZMA );
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, lzmaFields, TIFFArrayCount(lzmaFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging LZMA2 codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZMAState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = LState(tif);
+ memcpy(&sp->stream, &tmp_stream, sizeof(lzma_stream));
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = LZMAVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = LZMAVSetField; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->preset = LZMA_PRESET_DEFAULT; /* default comp. level */
+ sp->check = LZMA_CHECK_NONE;
+ sp->state = 0;
+
+ /* Data filters. So far we are using delta and LZMA2 filters only. */
+ sp->opt_delta.type = LZMA_DELTA_TYPE_BYTE;
+ /*
+ * The sample size in bytes seems to be reasonable distance for delta
+ * filter.
+ */
+ sp->opt_delta.dist = (tif->tif_dir.td_bitspersample % 8) ?
+ 1 : tif->tif_dir.td_bitspersample / 8;
+ sp->filters[0].id = LZMA_FILTER_DELTA;
+ sp->filters[0].options = &sp->opt_delta;
+
+ lzma_lzma_preset(&sp->opt_lzma, sp->preset);
+ sp->filters[1].id = LZMA_FILTER_LZMA2;
+ sp->filters[1].options = &sp->opt_lzma;
+
+ sp->filters[2].id = LZMA_VLI_UNKNOWN;
+ sp->filters[2].options = NULL;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = LZMAFixupTags;
+ tif->tif_setupdecode = LZMASetupDecode;
+ tif->tif_predecode = LZMAPreDecode;
+ tif->tif_decoderow = LZMADecode;
+ tif->tif_decodestrip = LZMADecode;
+ tif->tif_decodetile = LZMADecode;
+ tif->tif_setupencode = LZMASetupEncode;
+ tif->tif_preencode = LZMAPreEncode;
+ tif->tif_postencode = LZMAPostEncode;
+ tif->tif_encoderow = LZMAEncode;
+ tif->tif_encodestrip = LZMAEncode;
+ tif->tif_encodetile = LZMAEncode;
+ tif->tif_cleanup = LZMACleanup;
+ /*
+ * Setup predictor setup.
+ */
+ (void) TIFFPredictorInit(tif);
+ return 1;
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for LZMA2 state block");
+ return 0;
+}
+#endif /* LZMA_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
diff --git a/test/monniaux/tiff-4.0.10/tif_lzw.c b/test/monniaux/tiff-4.0.10/tif_lzw.c
new file mode 100644
index 00000000..ac685dd7
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_lzw.c
@@ -0,0 +1,1230 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef LZW_SUPPORT
+/*
+ * TIFF Library.
+ * Rev 5.0 Lempel-Ziv & Welch Compression Support
+ *
+ * This code is derived from the compress program whose code is
+ * derived from software contributed to Berkeley by James A. Woods,
+ * derived from original work by Spencer Thomas and Joseph Orost.
+ *
+ * The original Berkeley copyright notice appears below in its entirety.
+ */
+#include "tif_predict.h"
+
+#include <stdio.h>
+
+/*
+ * NB: The 5.0 spec describes a different algorithm than Aldus
+ * implements. Specifically, Aldus does code length transitions
+ * one code earlier than should be done (for real LZW).
+ * Earlier versions of this library implemented the correct
+ * LZW algorithm, but emitted codes in a bit order opposite
+ * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
+ * we interpret MSB-LSB ordered codes to be images written w/
+ * old versions of this library, but otherwise adhere to the
+ * Aldus "off by one" algorithm.
+ *
+ * Future revisions to the TIFF spec are expected to "clarify this issue".
+ */
+#define LZW_COMPAT /* include backwards compatibility code */
+/*
+ * Each strip of data is supposed to be terminated by a CODE_EOI.
+ * If the following #define is included, the decoder will also
+ * check for end-of-strip w/o seeing this code. This makes the
+ * library more robust, but also slower.
+ */
+#define LZW_CHECKEOS /* include checks for strips w/o EOI code */
+
+#define MAXCODE(n) ((1L<<(n))-1)
+/*
+ * The TIFF spec specifies that encoded bit
+ * strings range from 9 to 12 bits.
+ */
+#define BITS_MIN 9 /* start with 9 bits */
+#define BITS_MAX 12 /* max of 12 bit strings */
+/* predefined codes */
+#define CODE_CLEAR 256 /* code to clear string table */
+#define CODE_EOI 257 /* end-of-information code */
+#define CODE_FIRST 258 /* first free code entry */
+#define CODE_MAX MAXCODE(BITS_MAX)
+#define HSIZE 9001L /* 91% occupancy */
+#define HSHIFT (13-8)
+#ifdef LZW_COMPAT
+/* NB: +1024 is for compatibility with old files */
+#define CSIZE (MAXCODE(BITS_MAX)+1024L)
+#else
+#define CSIZE (MAXCODE(BITS_MAX)+1L)
+#endif
+
+/*
+ * State block for each open TIFF file using LZW
+ * compression/decompression. Note that the predictor
+ * state block must be first in this data structure.
+ */
+typedef struct {
+ TIFFPredictorState predict; /* predictor super class */
+
+ unsigned short nbits; /* # of bits/code */
+ unsigned short maxcode; /* maximum code for lzw_nbits */
+ unsigned short free_ent; /* next free entry in hash table */
+ unsigned long nextdata; /* next bits of i/o */
+ long nextbits; /* # of valid bits in lzw_nextdata */
+
+ int rw_mode; /* preserve rw_mode from init */
+} LZWBaseState;
+
+#define lzw_nbits base.nbits
+#define lzw_maxcode base.maxcode
+#define lzw_free_ent base.free_ent
+#define lzw_nextdata base.nextdata
+#define lzw_nextbits base.nextbits
+
+/*
+ * Encoding-specific state.
+ */
+typedef uint16 hcode_t; /* codes fit in 16 bits */
+typedef struct {
+ long hash;
+ hcode_t code;
+} hash_t;
+
+/*
+ * Decoding-specific state.
+ */
+typedef struct code_ent {
+ struct code_ent *next;
+ unsigned short length; /* string len, including this token */
+ unsigned char value; /* data value */
+ unsigned char firstchar; /* first token of string */
+} code_t;
+
+typedef int (*decodeFunc)(TIFF*, uint8*, tmsize_t, uint16);
+
+typedef struct {
+ LZWBaseState base;
+
+ /* Decoding specific data */
+ long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
+ long dec_restart; /* restart count */
+#ifdef LZW_CHECKEOS
+ uint64 dec_bitsleft; /* available bits in raw data */
+ tmsize_t old_tif_rawcc; /* value of tif_rawcc at the end of the previous TIFLZWDecode() call */
+#endif
+ decodeFunc dec_decode; /* regular or backwards compatible */
+ code_t* dec_codep; /* current recognized code */
+ code_t* dec_oldcodep; /* previously recognized code */
+ code_t* dec_free_entp; /* next free entry */
+ code_t* dec_maxcodep; /* max available entry */
+ code_t* dec_codetab; /* kept separate for small machines */
+
+ /* Encoding specific data */
+ int enc_oldcode; /* last code encountered */
+ long enc_checkpoint; /* point at which to clear table */
+#define CHECK_GAP 10000 /* enc_ratio check interval */
+ long enc_ratio; /* current compression ratio */
+ long enc_incount; /* (input) data bytes encoded */
+ long enc_outcount; /* encoded (output) bytes */
+ uint8* enc_rawlimit; /* bound on tif_rawdata buffer */
+ hash_t* enc_hashtab; /* kept separate for small machines */
+} LZWCodecState;
+
+#define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
+#define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
+#define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
+
+static int LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+#ifdef LZW_COMPAT
+static int LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+#endif
+static void cl_hash(LZWCodecState*);
+
+/*
+ * LZW Decoder.
+ */
+
+#ifdef LZW_CHECKEOS
+/*
+ * This check shouldn't be necessary because each
+ * strip is suppose to be terminated with CODE_EOI.
+ */
+#define NextCode(_tif, _sp, _bp, _code, _get) { \
+ if ((_sp)->dec_bitsleft < (uint64)nbits) { \
+ TIFFWarningExt(_tif->tif_clientdata, module, \
+ "LZWDecode: Strip %d not terminated with EOI code", \
+ _tif->tif_curstrip); \
+ _code = CODE_EOI; \
+ } else { \
+ _get(_sp,_bp,_code); \
+ (_sp)->dec_bitsleft -= nbits; \
+ } \
+}
+#else
+#define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
+#endif
+
+static int
+LZWFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+static int
+LZWSetupDecode(TIFF* tif)
+{
+ static const char module[] = "LZWSetupDecode";
+ LZWCodecState* sp = DecoderState(tif);
+ int code;
+
+ if( sp == NULL )
+ {
+ /*
+ * Allocate state block so tag methods have storage to record
+ * values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZWCodecState));
+ if (tif->tif_data == NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
+ return (0);
+ }
+
+ DecoderState(tif)->dec_codetab = NULL;
+ DecoderState(tif)->dec_decode = NULL;
+
+ /*
+ * Setup predictor setup.
+ */
+ (void) TIFFPredictorInit(tif);
+
+ sp = DecoderState(tif);
+ }
+
+ assert(sp != NULL);
+
+ if (sp->dec_codetab == NULL) {
+ sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
+ if (sp->dec_codetab == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for LZW code table");
+ return (0);
+ }
+ /*
+ * Pre-load the table.
+ */
+ code = 255;
+ do {
+ sp->dec_codetab[code].value = (unsigned char)code;
+ sp->dec_codetab[code].firstchar = (unsigned char)code;
+ sp->dec_codetab[code].length = 1;
+ sp->dec_codetab[code].next = NULL;
+ } while (code--);
+ /*
+ * Zero-out the unused entries
+ */
+ _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
+ (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
+ }
+ return (1);
+}
+
+/*
+ * Setup state for decoding a strip.
+ */
+static int
+LZWPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "LZWPreDecode";
+ LZWCodecState *sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ if( sp->dec_codetab == NULL )
+ {
+ tif->tif_setupdecode( tif );
+ if( sp->dec_codetab == NULL )
+ return (0);
+ }
+
+ /*
+ * Check for old bit-reversed codes.
+ */
+ if (tif->tif_rawcc >= 2 &&
+ tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
+#ifdef LZW_COMPAT
+ if (!sp->dec_decode) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Old-style LZW codes, convert file");
+ /*
+ * Override default decoding methods with
+ * ones that deal with the old coding.
+ * Otherwise the predictor versions set
+ * above will call the compatibility routines
+ * through the dec_decode method.
+ */
+ tif->tif_decoderow = LZWDecodeCompat;
+ tif->tif_decodestrip = LZWDecodeCompat;
+ tif->tif_decodetile = LZWDecodeCompat;
+ /*
+ * If doing horizontal differencing, must
+ * re-setup the predictor logic since we
+ * switched the basic decoder methods...
+ */
+ (*tif->tif_setupdecode)(tif);
+ sp->dec_decode = LZWDecodeCompat;
+ }
+ sp->lzw_maxcode = MAXCODE(BITS_MIN);
+#else /* !LZW_COMPAT */
+ if (!sp->dec_decode) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Old-style LZW codes not supported");
+ sp->dec_decode = LZWDecode;
+ }
+ return (0);
+#endif/* !LZW_COMPAT */
+ } else {
+ sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
+ sp->dec_decode = LZWDecode;
+ }
+ sp->lzw_nbits = BITS_MIN;
+ sp->lzw_nextbits = 0;
+ sp->lzw_nextdata = 0;
+
+ sp->dec_restart = 0;
+ sp->dec_nbitsmask = MAXCODE(BITS_MIN);
+#ifdef LZW_CHECKEOS
+ sp->dec_bitsleft = 0;
+ sp->old_tif_rawcc = 0;
+#endif
+ sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
+ /*
+ * Zero entries that are not yet filled in. We do
+ * this to guard against bogus input data that causes
+ * us to index into undefined entries. If you can
+ * come up with a way to safely bounds-check input codes
+ * while decoding then you can remove this operation.
+ */
+ _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
+ sp->dec_oldcodep = &sp->dec_codetab[-1];
+ sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
+ return (1);
+}
+
+/*
+ * Decode a "hunk of data".
+ */
+#define GetNextCode(sp, bp, code) { \
+ nextdata = (nextdata<<8) | *(bp)++; \
+ nextbits += 8; \
+ if (nextbits < nbits) { \
+ nextdata = (nextdata<<8) | *(bp)++; \
+ nextbits += 8; \
+ } \
+ code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
+ nextbits -= nbits; \
+}
+
+static void
+codeLoop(TIFF* tif, const char* module)
+{
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Bogus encoding, loop in the code table; scanline %d",
+ tif->tif_row);
+}
+
+static int
+LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
+{
+ static const char module[] = "LZWDecode";
+ LZWCodecState *sp = DecoderState(tif);
+ char *op = (char*) op0;
+ long occ = (long) occ0;
+ char *tp;
+ unsigned char *bp;
+ hcode_t code;
+ int len;
+ long nbits, nextbits, nbitsmask;
+ unsigned long nextdata;
+ code_t *codep, *free_entp, *maxcodep, *oldcodep;
+
+ (void) s;
+ assert(sp != NULL);
+ assert(sp->dec_codetab != NULL);
+
+ /*
+ Fail if value does not fit in long.
+ */
+ if ((tmsize_t) occ != occ0)
+ return (0);
+ /*
+ * Restart interrupted output operation.
+ */
+ if (sp->dec_restart) {
+ long residue;
+
+ codep = sp->dec_codep;
+ residue = codep->length - sp->dec_restart;
+ if (residue > occ) {
+ /*
+ * Residue from previous decode is sufficient
+ * to satisfy decode request. Skip to the
+ * start of the decoded string, place decoded
+ * values in the output buffer, and return.
+ */
+ sp->dec_restart += occ;
+ do {
+ codep = codep->next;
+ } while (--residue > occ && codep);
+ if (codep) {
+ tp = op + occ;
+ do {
+ *--tp = codep->value;
+ codep = codep->next;
+ } while (--occ && codep);
+ }
+ return (1);
+ }
+ /*
+ * Residue satisfies only part of the decode request.
+ */
+ op += residue;
+ occ -= residue;
+ tp = op;
+ do {
+ int t;
+ --tp;
+ t = codep->value;
+ codep = codep->next;
+ *tp = (char)t;
+ } while (--residue && codep);
+ sp->dec_restart = 0;
+ }
+
+ bp = (unsigned char *)tif->tif_rawcp;
+#ifdef LZW_CHECKEOS
+ sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
+#endif
+ nbits = sp->lzw_nbits;
+ nextdata = sp->lzw_nextdata;
+ nextbits = sp->lzw_nextbits;
+ nbitsmask = sp->dec_nbitsmask;
+ oldcodep = sp->dec_oldcodep;
+ free_entp = sp->dec_free_entp;
+ maxcodep = sp->dec_maxcodep;
+
+ while (occ > 0) {
+ NextCode(tif, sp, bp, code, GetNextCode);
+ if (code == CODE_EOI)
+ break;
+ if (code == CODE_CLEAR) {
+ do {
+ free_entp = sp->dec_codetab + CODE_FIRST;
+ _TIFFmemset(free_entp, 0,
+ (CSIZE - CODE_FIRST) * sizeof (code_t));
+ nbits = BITS_MIN;
+ nbitsmask = MAXCODE(BITS_MIN);
+ maxcodep = sp->dec_codetab + nbitsmask-1;
+ NextCode(tif, sp, bp, code, GetNextCode);
+ } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
+ if (code == CODE_EOI)
+ break;
+ if (code > CODE_CLEAR) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "LZWDecode: Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+ *op++ = (char)code;
+ occ--;
+ oldcodep = sp->dec_codetab + code;
+ continue;
+ }
+ codep = sp->dec_codetab + code;
+
+ /*
+ * Add the new entry to the code table.
+ */
+ if (free_entp < &sp->dec_codetab[0] ||
+ free_entp >= &sp->dec_codetab[CSIZE]) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+
+ free_entp->next = oldcodep;
+ if (free_entp->next < &sp->dec_codetab[0] ||
+ free_entp->next >= &sp->dec_codetab[CSIZE]) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+ free_entp->firstchar = free_entp->next->firstchar;
+ free_entp->length = free_entp->next->length+1;
+ free_entp->value = (codep < free_entp) ?
+ codep->firstchar : free_entp->firstchar;
+ if (++free_entp > maxcodep) {
+ if (++nbits > BITS_MAX) /* should not happen */
+ nbits = BITS_MAX;
+ nbitsmask = MAXCODE(nbits);
+ maxcodep = sp->dec_codetab + nbitsmask-1;
+ }
+ oldcodep = codep;
+ if (code >= 256) {
+ /*
+ * Code maps to a string, copy string
+ * value to output (written in reverse).
+ */
+ if(codep->length == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Wrong length of decoded string: "
+ "data probably corrupted at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+ if (codep->length > occ) {
+ /*
+ * String is too long for decode buffer,
+ * locate portion that will fit, copy to
+ * the decode buffer, and setup restart
+ * logic for the next decoding call.
+ */
+ sp->dec_codep = codep;
+ do {
+ codep = codep->next;
+ } while (codep && codep->length > occ);
+ if (codep) {
+ sp->dec_restart = (long)occ;
+ tp = op + occ;
+ do {
+ *--tp = codep->value;
+ codep = codep->next;
+ } while (--occ && codep);
+ if (codep)
+ codeLoop(tif, module);
+ }
+ break;
+ }
+ len = codep->length;
+ tp = op + len;
+ do {
+ int t;
+ --tp;
+ t = codep->value;
+ codep = codep->next;
+ *tp = (char)t;
+ } while (codep && tp > op);
+ if (codep) {
+ codeLoop(tif, module);
+ break;
+ }
+ assert(occ >= len);
+ op += len;
+ occ -= len;
+ } else {
+ *op++ = (char)code;
+ occ--;
+ }
+ }
+
+ tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
+ tif->tif_rawcp = (uint8*) bp;
+#ifdef LZW_CHECKEOS
+ sp->old_tif_rawcc = tif->tif_rawcc;
+#endif
+ sp->lzw_nbits = (unsigned short) nbits;
+ sp->lzw_nextdata = nextdata;
+ sp->lzw_nextbits = nextbits;
+ sp->dec_nbitsmask = nbitsmask;
+ sp->dec_oldcodep = oldcodep;
+ sp->dec_free_entp = free_entp;
+ sp->dec_maxcodep = maxcodep;
+
+ if (occ > 0) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %d (short %I64d bytes)",
+ tif->tif_row, (unsigned __int64) occ);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %d (short %llu bytes)",
+ tif->tif_row, (unsigned long long) occ);
+#endif
+ return (0);
+ }
+ return (1);
+}
+
+#ifdef LZW_COMPAT
+/*
+ * Decode a "hunk of data" for old images.
+ */
+#define GetNextCodeCompat(sp, bp, code) { \
+ nextdata |= (unsigned long) *(bp)++ << nextbits; \
+ nextbits += 8; \
+ if (nextbits < nbits) { \
+ nextdata |= (unsigned long) *(bp)++ << nextbits;\
+ nextbits += 8; \
+ } \
+ code = (hcode_t)(nextdata & nbitsmask); \
+ nextdata >>= nbits; \
+ nextbits -= nbits; \
+}
+
+static int
+LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
+{
+ static const char module[] = "LZWDecodeCompat";
+ LZWCodecState *sp = DecoderState(tif);
+ char *op = (char*) op0;
+ long occ = (long) occ0;
+ char *tp;
+ unsigned char *bp;
+ int code, nbits;
+ int len;
+ long nextbits, nextdata, nbitsmask;
+ code_t *codep, *free_entp, *maxcodep, *oldcodep;
+
+ (void) s;
+ assert(sp != NULL);
+
+ /*
+ Fail if value does not fit in long.
+ */
+ if ((tmsize_t) occ != occ0)
+ return (0);
+
+ /*
+ * Restart interrupted output operation.
+ */
+ if (sp->dec_restart) {
+ long residue;
+
+ codep = sp->dec_codep;
+ residue = codep->length - sp->dec_restart;
+ if (residue > occ) {
+ /*
+ * Residue from previous decode is sufficient
+ * to satisfy decode request. Skip to the
+ * start of the decoded string, place decoded
+ * values in the output buffer, and return.
+ */
+ sp->dec_restart += occ;
+ do {
+ codep = codep->next;
+ } while (--residue > occ);
+ tp = op + occ;
+ do {
+ *--tp = codep->value;
+ codep = codep->next;
+ } while (--occ);
+ return (1);
+ }
+ /*
+ * Residue satisfies only part of the decode request.
+ */
+ op += residue;
+ occ -= residue;
+ tp = op;
+ do {
+ *--tp = codep->value;
+ codep = codep->next;
+ } while (--residue);
+ sp->dec_restart = 0;
+ }
+
+ bp = (unsigned char *)tif->tif_rawcp;
+#ifdef LZW_CHECKEOS
+ sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
+#endif
+ nbits = sp->lzw_nbits;
+ nextdata = sp->lzw_nextdata;
+ nextbits = sp->lzw_nextbits;
+ nbitsmask = sp->dec_nbitsmask;
+ oldcodep = sp->dec_oldcodep;
+ free_entp = sp->dec_free_entp;
+ maxcodep = sp->dec_maxcodep;
+
+ while (occ > 0) {
+ NextCode(tif, sp, bp, code, GetNextCodeCompat);
+ if (code == CODE_EOI)
+ break;
+ if (code == CODE_CLEAR) {
+ do {
+ free_entp = sp->dec_codetab + CODE_FIRST;
+ _TIFFmemset(free_entp, 0,
+ (CSIZE - CODE_FIRST) * sizeof (code_t));
+ nbits = BITS_MIN;
+ nbitsmask = MAXCODE(BITS_MIN);
+ maxcodep = sp->dec_codetab + nbitsmask;
+ NextCode(tif, sp, bp, code, GetNextCodeCompat);
+ } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
+ if (code == CODE_EOI)
+ break;
+ if (code > CODE_CLEAR) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "LZWDecode: Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+ *op++ = (char)code;
+ occ--;
+ oldcodep = sp->dec_codetab + code;
+ continue;
+ }
+ codep = sp->dec_codetab + code;
+
+ /*
+ * Add the new entry to the code table.
+ */
+ if (free_entp < &sp->dec_codetab[0] ||
+ free_entp >= &sp->dec_codetab[CSIZE]) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Corrupted LZW table at scanline %d", tif->tif_row);
+ return (0);
+ }
+
+ free_entp->next = oldcodep;
+ if (free_entp->next < &sp->dec_codetab[0] ||
+ free_entp->next >= &sp->dec_codetab[CSIZE]) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Corrupted LZW table at scanline %d", tif->tif_row);
+ return (0);
+ }
+ free_entp->firstchar = free_entp->next->firstchar;
+ free_entp->length = free_entp->next->length+1;
+ free_entp->value = (codep < free_entp) ?
+ codep->firstchar : free_entp->firstchar;
+ if (++free_entp > maxcodep) {
+ if (++nbits > BITS_MAX) /* should not happen */
+ nbits = BITS_MAX;
+ nbitsmask = MAXCODE(nbits);
+ maxcodep = sp->dec_codetab + nbitsmask;
+ }
+ oldcodep = codep;
+ if (code >= 256) {
+ /*
+ * Code maps to a string, copy string
+ * value to output (written in reverse).
+ */
+ if(codep->length == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Wrong length of decoded "
+ "string: data probably corrupted at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
+ if (codep->length > occ) {
+ /*
+ * String is too long for decode buffer,
+ * locate portion that will fit, copy to
+ * the decode buffer, and setup restart
+ * logic for the next decoding call.
+ */
+ sp->dec_codep = codep;
+ do {
+ codep = codep->next;
+ } while (codep->length > occ);
+ sp->dec_restart = occ;
+ tp = op + occ;
+ do {
+ *--tp = codep->value;
+ codep = codep->next;
+ } while (--occ);
+ break;
+ }
+ len = codep->length;
+ tp = op + len;
+ do {
+ int t;
+ --tp;
+ t = codep->value;
+ codep = codep->next;
+ *tp = (char)t;
+ } while (codep && tp > op);
+ assert(occ >= len);
+ op += len;
+ occ -= len;
+ } else {
+ *op++ = (char)code;
+ occ--;
+ }
+ }
+
+ tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
+ tif->tif_rawcp = (uint8*) bp;
+#ifdef LZW_CHECKEOS
+ sp->old_tif_rawcc = tif->tif_rawcc;
+#endif
+ sp->lzw_nbits = (unsigned short)nbits;
+ sp->lzw_nextdata = nextdata;
+ sp->lzw_nextbits = nextbits;
+ sp->dec_nbitsmask = nbitsmask;
+ sp->dec_oldcodep = oldcodep;
+ sp->dec_free_entp = free_entp;
+ sp->dec_maxcodep = maxcodep;
+
+ if (occ > 0) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %d (short %I64d bytes)",
+ tif->tif_row, (unsigned __int64) occ);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %d (short %llu bytes)",
+ tif->tif_row, (unsigned long long) occ);
+#endif
+ return (0);
+ }
+ return (1);
+}
+#endif /* LZW_COMPAT */
+
+/*
+ * LZW Encoding.
+ */
+
+static int
+LZWSetupEncode(TIFF* tif)
+{
+ static const char module[] = "LZWSetupEncode";
+ LZWCodecState* sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
+ if (sp->enc_hashtab == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for LZW hash table");
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * Reset encoding state at the start of a strip.
+ */
+static int
+LZWPreEncode(TIFF* tif, uint16 s)
+{
+ LZWCodecState *sp = EncoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+
+ if( sp->enc_hashtab == NULL )
+ {
+ tif->tif_setupencode( tif );
+ }
+
+ sp->lzw_nbits = BITS_MIN;
+ sp->lzw_maxcode = MAXCODE(BITS_MIN);
+ sp->lzw_free_ent = CODE_FIRST;
+ sp->lzw_nextbits = 0;
+ sp->lzw_nextdata = 0;
+ sp->enc_checkpoint = CHECK_GAP;
+ sp->enc_ratio = 0;
+ sp->enc_incount = 0;
+ sp->enc_outcount = 0;
+ /*
+ * The 4 here insures there is space for 2 max-sized
+ * codes in LZWEncode and LZWPostDecode.
+ */
+ sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
+ cl_hash(sp); /* clear hash table */
+ sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
+ return (1);
+}
+
+#define CALCRATIO(sp, rat) { \
+ if (incount > 0x007fffff) { /* NB: shift will overflow */\
+ rat = outcount >> 8; \
+ rat = (rat == 0 ? 0x7fffffff : incount/rat); \
+ } else \
+ rat = (incount<<8) / outcount; \
+}
+
+/* Explicit 0xff masking to make icc -check=conversions happy */
+#define PutNextCode(op, c) { \
+ nextdata = (nextdata << nbits) | c; \
+ nextbits += nbits; \
+ *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
+ nextbits -= 8; \
+ if (nextbits >= 8) { \
+ *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
+ nextbits -= 8; \
+ } \
+ outcount += nbits; \
+}
+
+/*
+ * Encode a chunk of pixels.
+ *
+ * Uses an open addressing double hashing (no chaining) on the
+ * prefix code/next character combination. We do a variant of
+ * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
+ * relatively-prime secondary probe. Here, the modular division
+ * first probe is gives way to a faster exclusive-or manipulation.
+ * Also do block compression with an adaptive reset, whereby the
+ * code table is cleared when the compression ratio decreases,
+ * but after the table fills. The variable-length output codes
+ * are re-sized at this point, and a CODE_CLEAR is generated
+ * for the decoder.
+ */
+static int
+LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ register LZWCodecState *sp = EncoderState(tif);
+ register long fcode;
+ register hash_t *hp;
+ register int h, c;
+ hcode_t ent;
+ long disp;
+ long incount, outcount, checkpoint;
+ unsigned long nextdata;
+ long nextbits;
+ int free_ent, maxcode, nbits;
+ uint8* op;
+ uint8* limit;
+
+ (void) s;
+ if (sp == NULL)
+ return (0);
+
+ assert(sp->enc_hashtab != NULL);
+
+ /*
+ * Load local state.
+ */
+ incount = sp->enc_incount;
+ outcount = sp->enc_outcount;
+ checkpoint = sp->enc_checkpoint;
+ nextdata = sp->lzw_nextdata;
+ nextbits = sp->lzw_nextbits;
+ free_ent = sp->lzw_free_ent;
+ maxcode = sp->lzw_maxcode;
+ nbits = sp->lzw_nbits;
+ op = tif->tif_rawcp;
+ limit = sp->enc_rawlimit;
+ ent = (hcode_t)sp->enc_oldcode;
+
+ if (ent == (hcode_t) -1 && cc > 0) {
+ /*
+ * NB: This is safe because it can only happen
+ * at the start of a strip where we know there
+ * is space in the data buffer.
+ */
+ PutNextCode(op, CODE_CLEAR);
+ ent = *bp++; cc--; incount++;
+ }
+ while (cc > 0) {
+ c = *bp++; cc--; incount++;
+ fcode = ((long)c << BITS_MAX) + ent;
+ h = (c << HSHIFT) ^ ent; /* xor hashing */
+#ifdef _WINDOWS
+ /*
+ * Check hash index for an overflow.
+ */
+ if (h >= HSIZE)
+ h -= HSIZE;
+#endif
+ hp = &sp->enc_hashtab[h];
+ if (hp->hash == fcode) {
+ ent = hp->code;
+ continue;
+ }
+ if (hp->hash >= 0) {
+ /*
+ * Primary hash failed, check secondary hash.
+ */
+ disp = HSIZE - h;
+ if (h == 0)
+ disp = 1;
+ do {
+ /*
+ * Avoid pointer arithmetic because of
+ * wraparound problems with segments.
+ */
+ if ((h -= disp) < 0)
+ h += HSIZE;
+ hp = &sp->enc_hashtab[h];
+ if (hp->hash == fcode) {
+ ent = hp->code;
+ goto hit;
+ }
+ } while (hp->hash >= 0);
+ }
+ /*
+ * New entry, emit code and add to table.
+ */
+ /*
+ * Verify there is space in the buffer for the code
+ * and any potential Clear code that might be emitted
+ * below. The value of limit is setup so that there
+ * are at least 4 bytes free--room for 2 codes.
+ */
+ if (op > limit) {
+ tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
+ if( !TIFFFlushData1(tif) )
+ return 0;
+ op = tif->tif_rawdata;
+ }
+ PutNextCode(op, ent);
+ ent = (hcode_t)c;
+ hp->code = (hcode_t)(free_ent++);
+ hp->hash = fcode;
+ if (free_ent == CODE_MAX-1) {
+ /* table is full, emit clear code and reset */
+ cl_hash(sp);
+ sp->enc_ratio = 0;
+ incount = 0;
+ outcount = 0;
+ free_ent = CODE_FIRST;
+ PutNextCode(op, CODE_CLEAR);
+ nbits = BITS_MIN;
+ maxcode = MAXCODE(BITS_MIN);
+ } else {
+ /*
+ * If the next entry is going to be too big for
+ * the code size, then increase it, if possible.
+ */
+ if (free_ent > maxcode) {
+ nbits++;
+ assert(nbits <= BITS_MAX);
+ maxcode = (int) MAXCODE(nbits);
+ } else if (incount >= checkpoint) {
+ long rat;
+ /*
+ * Check compression ratio and, if things seem
+ * to be slipping, clear the hash table and
+ * reset state. The compression ratio is a
+ * 24+8-bit fractional number.
+ */
+ checkpoint = incount+CHECK_GAP;
+ CALCRATIO(sp, rat);
+ if (rat <= sp->enc_ratio) {
+ cl_hash(sp);
+ sp->enc_ratio = 0;
+ incount = 0;
+ outcount = 0;
+ free_ent = CODE_FIRST;
+ PutNextCode(op, CODE_CLEAR);
+ nbits = BITS_MIN;
+ maxcode = MAXCODE(BITS_MIN);
+ } else
+ sp->enc_ratio = rat;
+ }
+ }
+ hit:
+ ;
+ }
+
+ /*
+ * Restore global state.
+ */
+ sp->enc_incount = incount;
+ sp->enc_outcount = outcount;
+ sp->enc_checkpoint = checkpoint;
+ sp->enc_oldcode = ent;
+ sp->lzw_nextdata = nextdata;
+ sp->lzw_nextbits = nextbits;
+ sp->lzw_free_ent = (unsigned short)free_ent;
+ sp->lzw_maxcode = (unsigned short)maxcode;
+ sp->lzw_nbits = (unsigned short)nbits;
+ tif->tif_rawcp = op;
+ return (1);
+}
+
+/*
+ * Finish off an encoded strip by flushing the last
+ * string and tacking on an End Of Information code.
+ */
+static int
+LZWPostEncode(TIFF* tif)
+{
+ register LZWCodecState *sp = EncoderState(tif);
+ uint8* op = tif->tif_rawcp;
+ long nextbits = sp->lzw_nextbits;
+ unsigned long nextdata = sp->lzw_nextdata;
+ long outcount = sp->enc_outcount;
+ int nbits = sp->lzw_nbits;
+
+ if (op > sp->enc_rawlimit) {
+ tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
+ if( !TIFFFlushData1(tif) )
+ return 0;
+ op = tif->tif_rawdata;
+ }
+ if (sp->enc_oldcode != (hcode_t) -1) {
+ int free_ent = sp->lzw_free_ent;
+
+ PutNextCode(op, sp->enc_oldcode);
+ sp->enc_oldcode = (hcode_t) -1;
+ free_ent ++;
+
+ if (free_ent == CODE_MAX-1) {
+ /* table is full, emit clear code and reset */
+ outcount = 0;
+ PutNextCode(op, CODE_CLEAR);
+ nbits = BITS_MIN;
+ } else {
+ /*
+ * If the next entry is going to be too big for
+ * the code size, then increase it, if possible.
+ */
+ if (free_ent > sp->lzw_maxcode) {
+ nbits++;
+ assert(nbits <= BITS_MAX);
+ }
+ }
+ }
+ PutNextCode(op, CODE_EOI);
+ /* Explicit 0xff masking to make icc -check=conversions happy */
+ if (nextbits > 0)
+ *op++ = (unsigned char)((nextdata << (8-nextbits))&0xff);
+ tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
+ return (1);
+}
+
+/*
+ * Reset encoding hash table.
+ */
+static void
+cl_hash(LZWCodecState* sp)
+{
+ register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
+ register long i = HSIZE-8;
+
+ do {
+ i -= 8;
+ hp[-7].hash = -1;
+ hp[-6].hash = -1;
+ hp[-5].hash = -1;
+ hp[-4].hash = -1;
+ hp[-3].hash = -1;
+ hp[-2].hash = -1;
+ hp[-1].hash = -1;
+ hp[ 0].hash = -1;
+ hp -= 8;
+ } while (i >= 0);
+ for (i += 8; i > 0; i--, hp--)
+ hp->hash = -1;
+}
+
+static void
+LZWCleanup(TIFF* tif)
+{
+ (void)TIFFPredictorCleanup(tif);
+
+ assert(tif->tif_data != 0);
+
+ if (DecoderState(tif)->dec_codetab)
+ _TIFFfree(DecoderState(tif)->dec_codetab);
+
+ if (EncoderState(tif)->enc_hashtab)
+ _TIFFfree(EncoderState(tif)->enc_hashtab);
+
+ _TIFFfree(tif->tif_data);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+int
+TIFFInitLZW(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitLZW";
+ assert(scheme == COMPRESSION_LZW);
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LZWCodecState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ DecoderState(tif)->dec_codetab = NULL;
+ DecoderState(tif)->dec_decode = NULL;
+ EncoderState(tif)->enc_hashtab = NULL;
+ LZWState(tif)->rw_mode = tif->tif_mode;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = LZWFixupTags;
+ tif->tif_setupdecode = LZWSetupDecode;
+ tif->tif_predecode = LZWPreDecode;
+ tif->tif_decoderow = LZWDecode;
+ tif->tif_decodestrip = LZWDecode;
+ tif->tif_decodetile = LZWDecode;
+ tif->tif_setupencode = LZWSetupEncode;
+ tif->tif_preencode = LZWPreEncode;
+ tif->tif_postencode = LZWPostEncode;
+ tif->tif_encoderow = LZWEncode;
+ tif->tif_encodestrip = LZWEncode;
+ tif->tif_encodetile = LZWEncode;
+ tif->tif_cleanup = LZWCleanup;
+ /*
+ * Setup predictor setup.
+ */
+ (void) TIFFPredictorInit(tif);
+ return (1);
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for LZW state block");
+ return (0);
+}
+
+/*
+ * Copyright (c) 1985, 1986 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * James A. Woods, derived from original work by Spencer Thomas
+ * and Joseph Orost.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley. The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+#endif /* LZW_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_next.c b/test/monniaux/tiff-4.0.10/tif_next.c
new file mode 100644
index 00000000..0ba61aed
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_next.c
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef NEXT_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * NeXT 2-bit Grey Scale Compression Algorithm Support
+ */
+
+#define SETPIXEL(op, v) { \
+ switch (npixels++ & 3) { \
+ case 0: op[0] = (unsigned char) ((v) << 6); break; \
+ case 1: op[0] |= (v) << 4; break; \
+ case 2: op[0] |= (v) << 2; break; \
+ case 3: *op++ |= (v); op_offset++; break; \
+ } \
+}
+
+#define LITERALROW 0x00
+#define LITERALSPAN 0x40
+#define WHITE ((1<<2)-1)
+
+static int
+NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "NeXTDecode";
+ unsigned char *bp, *op;
+ tmsize_t cc;
+ uint8* row;
+ tmsize_t scanline, n;
+
+ (void) s;
+ /*
+ * Each scanline is assumed to start off as all
+ * white (we assume a PhotometricInterpretation
+ * of ``min-is-black'').
+ */
+ for (op = (unsigned char*) buf, cc = occ; cc-- > 0;)
+ *op++ = 0xff;
+
+ bp = (unsigned char *)tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ scanline = tif->tif_scanlinesize;
+ if (occ % scanline)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (0);
+ }
+ for (row = buf; cc > 0 && occ > 0; occ -= scanline, row += scanline) {
+ n = *bp++;
+ cc--;
+ switch (n) {
+ case LITERALROW:
+ /*
+ * The entire scanline is given as literal values.
+ */
+ if (cc < scanline)
+ goto bad;
+ _TIFFmemcpy(row, bp, scanline);
+ bp += scanline;
+ cc -= scanline;
+ break;
+ case LITERALSPAN: {
+ tmsize_t off;
+ /*
+ * The scanline has a literal span that begins at some
+ * offset.
+ */
+ if( cc < 4 )
+ goto bad;
+ off = (bp[0] * 256) + bp[1];
+ n = (bp[2] * 256) + bp[3];
+ if (cc < 4+n || off+n > scanline)
+ goto bad;
+ _TIFFmemcpy(row+off, bp+4, n);
+ bp += 4+n;
+ cc -= 4+n;
+ break;
+ }
+ default: {
+ uint32 npixels = 0, grey;
+ tmsize_t op_offset = 0;
+ uint32 imagewidth = tif->tif_dir.td_imagewidth;
+ if( isTiled(tif) )
+ imagewidth = tif->tif_dir.td_tilewidth;
+
+ /*
+ * The scanline is composed of a sequence of constant
+ * color ``runs''. We shift into ``run mode'' and
+ * interpret bytes as codes of the form
+ * <color><npixels> until we've filled the scanline.
+ */
+ op = row;
+ for (;;) {
+ grey = (uint32)((n>>6) & 0x3);
+ n &= 0x3f;
+ /*
+ * Ensure the run does not exceed the scanline
+ * bounds, potentially resulting in a security
+ * issue.
+ */
+ while (n-- > 0 && npixels < imagewidth && op_offset < scanline)
+ SETPIXEL(op, grey);
+ if (npixels >= imagewidth)
+ break;
+ if (op_offset >= scanline ) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Invalid data for scanline %ld",
+ (long) tif->tif_row);
+ return (0);
+ }
+ if (cc == 0)
+ goto bad;
+ n = *bp++;
+ cc--;
+ }
+ break;
+ }
+ }
+ }
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ return (1);
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",
+ (long) tif->tif_row);
+ return (0);
+}
+
+static int
+NeXTPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "NeXTPreDecode";
+ TIFFDirectory *td = &tif->tif_dir;
+ (void)s;
+
+ if( td->td_bitspersample != 2 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Unsupported BitsPerSample = %d",
+ td->td_bitspersample);
+ return (0);
+ }
+ return (1);
+}
+
+int
+TIFFInitNeXT(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ tif->tif_predecode = NeXTPreDecode;
+ tif->tif_decoderow = NeXTDecode;
+ tif->tif_decodestrip = NeXTDecode;
+ tif->tif_decodetile = NeXTDecode;
+ return (1);
+}
+#endif /* NEXT_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_ojpeg.c b/test/monniaux/tiff-4.0.10/tif_ojpeg.c
new file mode 100644
index 00000000..27385d8c
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_ojpeg.c
@@ -0,0 +1,2561 @@
+/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
+ specification is now totally obsolete and deprecated for new applications and
+ images. This file was was created solely in order to read unconverted images
+ still present on some users' computer systems. It will never be extended
+ to write such files. Writing new-style JPEG compressed TIFFs is implemented
+ in tif_jpeg.c.
+
+ The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
+ testfiles, and anticipate as much as possible all other... But still, it may
+ fail on some. If you encounter problems, please report them on the TIFF
+ mailing list and/or to Joris Van Damme <info@awaresystems.be>.
+
+ Please read the file called "TIFF Technical Note #2" if you need to be
+ convinced this compression scheme is bad and breaks TIFF. That document
+ is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
+ and from AWare Systems' TIFF section
+ <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
+ in Adobe's specification supplements, marked "draft" up to this day, but
+ supported by the TIFF community.
+
+ This file interfaces with Release 6B of the JPEG Library written by the
+ Independent JPEG Group. Previous versions of this file required a hack inside
+ the LibJpeg library. This version no longer requires that. Remember to
+ remove the hack if you update from the old version.
+
+ Copyright (c) Joris Van Damme <info@awaresystems.be>
+ Copyright (c) AWare Systems <http://www.awaresystems.be/>
+
+ The licence agreement for this file is the same as the rest of the LibTiff
+ library.
+
+ IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
+ ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ OF THIS SOFTWARE.
+
+ Joris Van Damme and/or AWare Systems may be available for custom
+ development. If you like what you see, and need anything similar or related,
+ contact <info@awaresystems.be>.
+*/
+
+/* What is what, and what is not?
+
+ This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
+ stream, if any, followed by the strile data, if any. This stream is read in
+ OJPEGReadByte and related functions.
+
+ It analyzes the start of this stream, until it encounters non-marker data, i.e.
+ compressed image data. Some of the header markers it sees have no actual content,
+ like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
+ other markers do have content, and the valuable bits and pieces of information
+ in these markers are saved, checking all to verify that the stream is more or
+ less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
+ functions.
+
+ Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
+ up on if we've seen no SOF marker when we're at the start of the compressed image
+ data. In this case, the tables are read from JpegXxxTables tags, and the other
+ bits and pieces of information is initialized to its most basic value. This is
+ implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
+
+ When this is complete, a good and valid JPEG header can be assembled, and this is
+ passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
+ the compressed image data, can be passed through unchanged. This is done in
+ OJPEGWriteStream functions.
+
+ LibTiff rightly expects to know the subsampling values before decompression. Just like
+ in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
+ tag is notoriously unreliable. To correct these tag values with the ones inside
+ the JPEG stream, the first part of the input stream is pre-scanned in
+ OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
+ or errors, up to the point where either these values are read, or it's clear they
+ aren't there. This means that some of the data is read twice, but we feel speed
+ in correcting these values is important enough to warrant this sacrifice. Although
+ there is currently no define or other configuration mechanism to disable this behaviour,
+ the actual header scanning is build to robustly respond with error report if it
+ should encounter an uncorrected mismatch of subsampling values. See
+ OJPEGReadHeaderInfoSecStreamSof.
+
+ The restart interval and restart markers are the most tricky part... The restart
+ interval can be specified in a tag. It can also be set inside the input JPEG stream.
+ It can be used inside the input JPEG stream. If reading from strile data, we've
+ consistently discovered the need to insert restart markers in between the different
+ striles, as is also probably the most likely interpretation of the original TIFF 6.0
+ specification. With all this setting of interval, and actual use of markers that is not
+ predictable at the time of valid JPEG header assembly, the restart thing may turn
+ out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
+ succeed in reading back what they write, which may be the reason why we've been able
+ to discover ways that seem to work.
+
+ Some special provision is made for planarconfig separate OJPEG files. These seem
+ to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
+ and plane. This may or may not be a valid JPEG configuration, we don't know and don't
+ care. We want LibTiff to be able to access the planes individually, without huge
+ buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
+ case, that allow us to pass a single plane such that LibJpeg sees a valid
+ single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
+ planes, is done inside OJPEGReadSecondarySos.
+
+ The benefit of the scheme is... that it works, basically. We know of no other that
+ does. It works without checking software tag, or otherwise going about things in an
+ OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
+ with and without JpegInterchangeFormat, with and without striles, with part of
+ the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
+ and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
+ of the data.
+
+ Another nice side-effect is that a complete JPEG single valid stream is build if
+ planarconfig is not separate (vast majority). We may one day use that to build
+ converters to JPEG, and/or to new-style JPEG compression inside TIFF.
+
+ A disadvantage is the lack of random access to the individual striles. This is the
+ reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
+ Applications would do well accessing all striles in order, as this will result in
+ a single sequential scan of the input stream, and no restarting of LibJpeg decoding
+ session.
+*/
+
+#define WIN32_LEAN_AND_MEAN
+#define VC_EXTRALEAN
+
+#include "tiffiop.h"
+#ifdef OJPEG_SUPPORT
+
+/* Configuration defines here are:
+ * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
+ * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
+ * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
+ * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
+ * to this unit, and can be defined elsewhere to use stuff other then longjump.
+ * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
+ * here, internally, with normal longjump.
+ * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
+ * conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
+ * in place of plain setjmp. These macros will make it easier. It is useless
+ * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
+ * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
+ * instant processing, optimal streaming and optimal use of processor cache, but also big
+ * enough so as to not result in significant call overhead. It should be at least a few
+ * bytes to accommodate some structures (this is verified in asserts), but it would not be
+ * sensible to make it this small anyway, and it should be at most 64K since it is indexed
+ * with uint16. We recommend 2K.
+ * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
+ * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
+ */
+
+/* define LIBJPEG_ENCAP_EXTERNAL */
+#define SETJMP(jbuf) setjmp(jbuf)
+#define LONGJMP(jbuf,code) longjmp(jbuf,code)
+#define JMP_BUF jmp_buf
+#define OJPEG_BUFFER 2048
+/* define EGYPTIANWALK */
+
+#define JPEG_MARKER_SOF0 0xC0
+#define JPEG_MARKER_SOF1 0xC1
+#define JPEG_MARKER_SOF3 0xC3
+#define JPEG_MARKER_DHT 0xC4
+#define JPEG_MARKER_RST0 0XD0
+#define JPEG_MARKER_SOI 0xD8
+#define JPEG_MARKER_EOI 0xD9
+#define JPEG_MARKER_SOS 0xDA
+#define JPEG_MARKER_DQT 0xDB
+#define JPEG_MARKER_DRI 0xDD
+#define JPEG_MARKER_APP0 0xE0
+#define JPEG_MARKER_COM 0xFE
+
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
+#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
+#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
+#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
+#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
+#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
+
+static const TIFFField ojpegFields[] = {
+ {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat",NULL},
+ {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength",NULL},
+ {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables",NULL},
+ {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables",NULL},
+ {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables",NULL},
+ {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc",NULL},
+ {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval",NULL},
+};
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+#include <setjmp.h>
+#endif
+
+/* We undefine FAR to avoid conflict with JPEG definition */
+
+#ifdef FAR
+#undef FAR
+#endif
+
+/*
+ Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
+ not defined. Unfortunately, the MinGW and Borland compilers include
+ a typedef for INT32, which causes a conflict. MSVC does not include
+ a conflicting typedef given the headers which are included.
+*/
+#if defined(__BORLANDC__) || defined(__MINGW32__)
+# define XMD_H 1
+#endif
+
+/* Define "boolean" as unsigned char, not int, per Windows custom. */
+#if defined(__WIN32__) && !defined(__MINGW32__)
+# ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+ typedef unsigned char boolean;
+# endif
+# define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+#endif
+
+#include "jpeglib.h"
+#include "jerror.h"
+
+typedef struct jpeg_error_mgr jpeg_error_mgr;
+typedef struct jpeg_common_struct jpeg_common_struct;
+typedef struct jpeg_decompress_struct jpeg_decompress_struct;
+typedef struct jpeg_source_mgr jpeg_source_mgr;
+
+typedef enum {
+ osibsNotSetYet,
+ osibsJpegInterchangeFormat,
+ osibsStrile,
+ osibsEof
+} OJPEGStateInBufferSource;
+
+typedef enum {
+ ososSoi,
+ ososQTable0,ososQTable1,ososQTable2,ososQTable3,
+ ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
+ ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
+ ososDri,
+ ososSof,
+ ososSos,
+ ososCompressed,
+ ososRst,
+ ososEoi
+} OJPEGStateOutState;
+
+typedef struct {
+ TIFF* tif;
+ int decoder_ok;
+ #ifndef LIBJPEG_ENCAP_EXTERNAL
+ JMP_BUF exit_jmpbuf;
+ #endif
+ TIFFVGetMethod vgetparent;
+ TIFFVSetMethod vsetparent;
+ TIFFPrintMethod printdir;
+ uint64 file_size;
+ uint32 image_width;
+ uint32 image_length;
+ uint32 strile_width;
+ uint32 strile_length;
+ uint32 strile_length_total;
+ uint8 samples_per_pixel;
+ uint8 plane_sample_offset;
+ uint8 samples_per_pixel_per_plane;
+ uint64 jpeg_interchange_format;
+ uint64 jpeg_interchange_format_length;
+ uint8 jpeg_proc;
+ uint8 subsamplingcorrect;
+ uint8 subsamplingcorrect_done;
+ uint8 subsampling_tag;
+ uint8 subsampling_hor;
+ uint8 subsampling_ver;
+ uint8 subsampling_force_desubsampling_inside_decompression;
+ uint8 qtable_offset_count;
+ uint8 dctable_offset_count;
+ uint8 actable_offset_count;
+ uint64 qtable_offset[3];
+ uint64 dctable_offset[3];
+ uint64 actable_offset[3];
+ uint8* qtable[4];
+ uint8* dctable[4];
+ uint8* actable[4];
+ uint16 restart_interval;
+ uint8 restart_index;
+ uint8 sof_log;
+ uint8 sof_marker_id;
+ uint32 sof_x;
+ uint32 sof_y;
+ uint8 sof_c[3];
+ uint8 sof_hv[3];
+ uint8 sof_tq[3];
+ uint8 sos_cs[3];
+ uint8 sos_tda[3];
+ struct {
+ uint8 log;
+ OJPEGStateInBufferSource in_buffer_source;
+ uint32 in_buffer_next_strile;
+ uint64 in_buffer_file_pos;
+ uint64 in_buffer_file_togo;
+ } sos_end[3];
+ uint8 readheader_done;
+ uint8 writeheader_done;
+ uint16 write_cursample;
+ uint32 write_curstrile;
+ uint8 libjpeg_session_active;
+ uint8 libjpeg_jpeg_query_style;
+ jpeg_error_mgr libjpeg_jpeg_error_mgr;
+ jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
+ jpeg_source_mgr libjpeg_jpeg_source_mgr;
+ uint8 subsampling_convert_log;
+ uint32 subsampling_convert_ylinelen;
+ uint32 subsampling_convert_ylines;
+ uint32 subsampling_convert_clinelen;
+ uint32 subsampling_convert_clines;
+ uint32 subsampling_convert_ybuflen;
+ uint32 subsampling_convert_cbuflen;
+ uint32 subsampling_convert_ycbcrbuflen;
+ uint8* subsampling_convert_ycbcrbuf;
+ uint8* subsampling_convert_ybuf;
+ uint8* subsampling_convert_cbbuf;
+ uint8* subsampling_convert_crbuf;
+ uint32 subsampling_convert_ycbcrimagelen;
+ uint8** subsampling_convert_ycbcrimage;
+ uint32 subsampling_convert_clinelenout;
+ uint32 subsampling_convert_state;
+ uint32 bytes_per_line; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
+ uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows */
+ OJPEGStateInBufferSource in_buffer_source;
+ uint32 in_buffer_next_strile;
+ uint32 in_buffer_strile_count;
+ uint64 in_buffer_file_pos;
+ uint8 in_buffer_file_pos_log;
+ uint64 in_buffer_file_togo;
+ uint16 in_buffer_togo;
+ uint8* in_buffer_cur;
+ uint8 in_buffer[OJPEG_BUFFER];
+ OJPEGStateOutState out_state;
+ uint8 out_buffer[OJPEG_BUFFER];
+ uint8* skip_buffer;
+} OJPEGState;
+
+static int OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap);
+static int OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap);
+static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
+
+static int OJPEGFixupTags(TIFF* tif);
+static int OJPEGSetupDecode(TIFF* tif);
+static int OJPEGPreDecode(TIFF* tif, uint16 s);
+static int OJPEGPreDecodeSkipRaw(TIFF* tif);
+static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
+static int OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc);
+static int OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc);
+static void OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
+static int OJPEGSetupEncode(TIFF* tif);
+static int OJPEGPreEncode(TIFF* tif, uint16 s);
+static int OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
+static int OJPEGPostEncode(TIFF* tif);
+static void OJPEGCleanup(TIFF* tif);
+
+static void OJPEGSubsamplingCorrect(TIFF* tif);
+static int OJPEGReadHeaderInfo(TIFF* tif);
+static int OJPEGReadSecondarySos(TIFF* tif, uint16 s);
+static int OJPEGWriteHeaderInfo(TIFF* tif);
+static void OJPEGLibjpegSessionAbort(TIFF* tif);
+
+static int OJPEGReadHeaderInfoSec(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
+static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
+
+static int OJPEGReadBufferFill(OJPEGState* sp);
+static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
+static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
+static void OJPEGReadByteAdvance(OJPEGState* sp);
+static int OJPEGReadWord(OJPEGState* sp, uint16* word);
+static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
+static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
+
+static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
+static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
+
+#ifdef LIBJPEG_ENCAP_EXTERNAL
+extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+extern void jpeg_encap_unwind(TIFF* tif);
+#else
+static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
+static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+static void jpeg_encap_unwind(TIFF* tif);
+#endif
+
+static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
+static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
+static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
+static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
+
+int
+TIFFInitOJPEG(TIFF* tif, int scheme)
+{
+ static const char module[]="TIFFInitOJPEG";
+ OJPEGState* sp;
+
+ assert(scheme==COMPRESSION_OJPEG);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging Old JPEG codec-specific tags failed");
+ return 0;
+ }
+
+ /* state block */
+ sp=_TIFFmalloc(sizeof(OJPEGState));
+ if (sp==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
+ return(0);
+ }
+ _TIFFmemset(sp,0,sizeof(OJPEGState));
+ sp->tif=tif;
+ sp->jpeg_proc=1;
+ sp->subsampling_hor=2;
+ sp->subsampling_ver=2;
+ TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
+ /* tif codec methods */
+ tif->tif_fixuptags=OJPEGFixupTags;
+ tif->tif_setupdecode=OJPEGSetupDecode;
+ tif->tif_predecode=OJPEGPreDecode;
+ tif->tif_postdecode=OJPEGPostDecode;
+ tif->tif_decoderow=OJPEGDecode;
+ tif->tif_decodestrip=OJPEGDecode;
+ tif->tif_decodetile=OJPEGDecode;
+ tif->tif_setupencode=OJPEGSetupEncode;
+ tif->tif_preencode=OJPEGPreEncode;
+ tif->tif_postencode=OJPEGPostEncode;
+ tif->tif_encoderow=OJPEGEncode;
+ tif->tif_encodestrip=OJPEGEncode;
+ tif->tif_encodetile=OJPEGEncode;
+ tif->tif_cleanup=OJPEGCleanup;
+ tif->tif_data=(uint8*)sp;
+ /* tif tag methods */
+ sp->vgetparent=tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield=OJPEGVGetField;
+ sp->vsetparent=tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield=OJPEGVSetField;
+ sp->printdir=tif->tif_tagmethods.printdir;
+ tif->tif_tagmethods.printdir=OJPEGPrintDir;
+ /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
+ Some others do, but have totally meaningless or corrupt values
+ in these tags. In these cases, the JpegInterchangeFormat stream is
+ reliable. In any case, this decoder reads the compressed data itself,
+ from the most reliable locations, and we need to notify encapsulating
+ LibTiff not to read raw strips or tiles for us. */
+ tif->tif_flags|=TIFF_NOREADRAW;
+ return(1);
+}
+
+static int
+OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ switch(tag)
+ {
+ case TIFFTAG_JPEGIFOFFSET:
+ *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format;
+ break;
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format_length;
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ if (sp->subsamplingcorrect_done==0)
+ OJPEGSubsamplingCorrect(tif);
+ *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
+ *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
+ break;
+ case TIFFTAG_JPEGQTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->qtable_offset;
+ break;
+ case TIFFTAG_JPEGDCTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->dctable_offset;
+ break;
+ case TIFFTAG_JPEGACTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->actable_offset;
+ break;
+ case TIFFTAG_JPEGPROC:
+ *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
+ break;
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ *va_arg(ap,uint16*)=sp->restart_interval;
+ break;
+ default:
+ return (*sp->vgetparent)(tif,tag,ap);
+ }
+ return (1);
+}
+
+static int
+OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[]="OJPEGVSetField";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 ma;
+ uint64* mb;
+ uint32 n;
+ const TIFFField* fip;
+
+ switch(tag)
+ {
+ case TIFFTAG_JPEGIFOFFSET:
+ sp->jpeg_interchange_format=(uint64)va_arg(ap,uint64);
+ break;
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ sp->jpeg_interchange_format_length=(uint64)va_arg(ap,uint64);
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ sp->subsampling_tag=1;
+ sp->subsampling_hor=(uint8)va_arg(ap,uint16_vap);
+ sp->subsampling_ver=(uint8)va_arg(ap,uint16_vap);
+ tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
+ tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
+ break;
+ case TIFFTAG_JPEGQTABLES:
+ ma=(uint32)va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
+ return(0);
+ }
+ sp->qtable_offset_count=(uint8)ma;
+ mb=(uint64*)va_arg(ap,uint64*);
+ for (n=0; n<ma; n++)
+ sp->qtable_offset[n]=mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGDCTABLES:
+ ma=(uint32)va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
+ return(0);
+ }
+ sp->dctable_offset_count=(uint8)ma;
+ mb=(uint64*)va_arg(ap,uint64*);
+ for (n=0; n<ma; n++)
+ sp->dctable_offset[n]=mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGACTABLES:
+ ma=(uint32)va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
+ return(0);
+ }
+ sp->actable_offset_count=(uint8)ma;
+ mb=(uint64*)va_arg(ap,uint64*);
+ for (n=0; n<ma; n++)
+ sp->actable_offset[n]=mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGPROC:
+ sp->jpeg_proc=(uint8)va_arg(ap,uint16_vap);
+ break;
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ sp->restart_interval=(uint16)va_arg(ap,uint16_vap);
+ break;
+ default:
+ return (*sp->vsetparent)(tif,tag,ap);
+ }
+ fip = TIFFFieldWithTag(tif,tag);
+ if( fip == NULL ) /* shouldn't happen */
+ return(0);
+ TIFFSetFieldBit(tif,fip->field_bit);
+ tif->tif_flags|=TIFF_DIRTYDIRECT;
+ return(1);
+}
+
+static void
+OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ (void)flags;
+ assert(sp!=NULL);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
+ fprintf(fd," JpegInterchangeFormat: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
+ fprintf(fd," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format_length);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
+ {
+ fprintf(fd," JpegQTables:");
+ for (m=0; m<sp->qtable_offset_count; m++)
+ fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->qtable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
+ {
+ fprintf(fd," JpegDcTables:");
+ for (m=0; m<sp->dctable_offset_count; m++)
+ fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->dctable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
+ {
+ fprintf(fd," JpegAcTables:");
+ for (m=0; m<sp->actable_offset_count; m++)
+ fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->actable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
+ fprintf(fd," JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
+ fprintf(fd," JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
+ if (sp->printdir)
+ (*sp->printdir)(tif, fd, flags);
+}
+
+static int
+OJPEGFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return(1);
+}
+
+static int
+OJPEGSetupDecode(TIFF* tif)
+{
+ static const char module[]="OJPEGSetupDecode";
+ TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
+ return(1);
+}
+
+static int
+OJPEGPreDecode(TIFF* tif, uint16 s)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 m;
+ if (sp->subsamplingcorrect_done==0)
+ OJPEGSubsamplingCorrect(tif);
+ if (sp->readheader_done==0)
+ {
+ if (OJPEGReadHeaderInfo(tif)==0)
+ return(0);
+ }
+ if (sp->sos_end[s].log==0)
+ {
+ if (OJPEGReadSecondarySos(tif,s)==0)
+ return(0);
+ }
+ if isTiled(tif)
+ m=tif->tif_curtile;
+ else
+ m=tif->tif_curstrip;
+ if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
+ {
+ if (sp->libjpeg_session_active!=0)
+ OJPEGLibjpegSessionAbort(tif);
+ sp->writeheader_done=0;
+ }
+ if (sp->writeheader_done==0)
+ {
+ sp->plane_sample_offset=(uint8)s;
+ sp->write_cursample=s;
+ sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
+ if ((sp->in_buffer_file_pos_log==0) ||
+ (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
+ {
+ sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
+ sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
+ sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
+ sp->in_buffer_file_pos_log=0;
+ sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
+ sp->in_buffer_togo=0;
+ sp->in_buffer_cur=0;
+ }
+ if (OJPEGWriteHeaderInfo(tif)==0)
+ return(0);
+ }
+ while (sp->write_curstrile<m)
+ {
+ if (sp->libjpeg_jpeg_query_style==0)
+ {
+ if (OJPEGPreDecodeSkipRaw(tif)==0)
+ return(0);
+ }
+ else
+ {
+ if (OJPEGPreDecodeSkipScanlines(tif)==0)
+ return(0);
+ }
+ sp->write_curstrile++;
+ }
+ sp->decoder_ok = 1;
+ return(1);
+}
+
+static int
+OJPEGPreDecodeSkipRaw(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 m;
+ m=sp->lines_per_strile;
+ if (sp->subsampling_convert_state!=0)
+ {
+ if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
+ {
+ sp->subsampling_convert_state+=m;
+ if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+ sp->subsampling_convert_state=0;
+ return(1);
+ }
+ m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
+ sp->subsampling_convert_state=0;
+ }
+ while (m>=sp->subsampling_convert_clines)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ m-=sp->subsampling_convert_clines;
+ }
+ if (m>0)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ sp->subsampling_convert_state=m;
+ }
+ return(1);
+}
+
+static int
+OJPEGPreDecodeSkipScanlines(TIFF* tif)
+{
+ static const char module[]="OJPEGPreDecodeSkipScanlines";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 m;
+ if (sp->skip_buffer==NULL)
+ {
+ sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
+ if (sp->skip_buffer==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ }
+ for (m=0; m<sp->lines_per_strile; m++)
+ {
+ if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
+ return(0);
+ }
+ return(1);
+}
+
+static int
+OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ static const char module[]="OJPEGDecode";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ (void)s;
+ if( !sp->decoder_ok )
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Cannot decode: decoder not correctly initialized");
+ return 0;
+ }
+ if (sp->libjpeg_jpeg_query_style==0)
+ {
+ if (OJPEGDecodeRaw(tif,buf,cc)==0)
+ return(0);
+ }
+ else
+ {
+ if (OJPEGDecodeScanlines(tif,buf,cc)==0)
+ return(0);
+ }
+ return(1);
+}
+
+static int
+OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ static const char module[]="OJPEGDecodeRaw";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8* m;
+ tmsize_t n;
+ uint8* oy;
+ uint8* ocb;
+ uint8* ocr;
+ uint8* p;
+ uint32 q;
+ uint8* r;
+ uint8 sx,sy;
+ if (cc%sp->bytes_per_line!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+ return(0);
+ }
+ assert(cc>0);
+ m=buf;
+ n=cc;
+ do
+ {
+ if (sp->subsampling_convert_state==0)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ }
+ oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
+ ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+ ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+ p=m;
+ for (q=0; q<sp->subsampling_convert_clinelenout; q++)
+ {
+ r=oy;
+ for (sy=0; sy<sp->subsampling_ver; sy++)
+ {
+ for (sx=0; sx<sp->subsampling_hor; sx++)
+ *p++=*r++;
+ r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
+ }
+ oy+=sp->subsampling_hor;
+ *p++=*ocb++;
+ *p++=*ocr++;
+ }
+ sp->subsampling_convert_state++;
+ if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+ sp->subsampling_convert_state=0;
+ m+=sp->bytes_per_line;
+ n-=sp->bytes_per_line;
+ } while(n>0);
+ return(1);
+}
+
+static int
+OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ static const char module[]="OJPEGDecodeScanlines";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8* m;
+ tmsize_t n;
+ if (cc%sp->bytes_per_line!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+ return(0);
+ }
+ assert(cc>0);
+ m=buf;
+ n=cc;
+ do
+ {
+ if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
+ return(0);
+ m+=sp->bytes_per_line;
+ n-=sp->bytes_per_line;
+ } while(n>0);
+ return(1);
+}
+
+static void
+OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ (void)buf;
+ (void)cc;
+ sp->write_curstrile++;
+ if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
+ {
+ assert(sp->libjpeg_session_active!=0);
+ OJPEGLibjpegSessionAbort(tif);
+ sp->writeheader_done=0;
+ }
+}
+
+static int
+OJPEGSetupEncode(TIFF* tif)
+{
+ static const char module[]="OJPEGSetupEncode";
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
+
+static int
+OJPEGPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[]="OJPEGPreEncode";
+ (void)s;
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
+
+static int
+OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ static const char module[]="OJPEGEncode";
+ (void)buf;
+ (void)cc;
+ (void)s;
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
+
+static int
+OJPEGPostEncode(TIFF* tif)
+{
+ static const char module[]="OJPEGPostEncode";
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
+
+static void
+OJPEGCleanup(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp!=0)
+ {
+ tif->tif_tagmethods.vgetfield=sp->vgetparent;
+ tif->tif_tagmethods.vsetfield=sp->vsetparent;
+ tif->tif_tagmethods.printdir=sp->printdir;
+ if (sp->qtable[0]!=0)
+ _TIFFfree(sp->qtable[0]);
+ if (sp->qtable[1]!=0)
+ _TIFFfree(sp->qtable[1]);
+ if (sp->qtable[2]!=0)
+ _TIFFfree(sp->qtable[2]);
+ if (sp->qtable[3]!=0)
+ _TIFFfree(sp->qtable[3]);
+ if (sp->dctable[0]!=0)
+ _TIFFfree(sp->dctable[0]);
+ if (sp->dctable[1]!=0)
+ _TIFFfree(sp->dctable[1]);
+ if (sp->dctable[2]!=0)
+ _TIFFfree(sp->dctable[2]);
+ if (sp->dctable[3]!=0)
+ _TIFFfree(sp->dctable[3]);
+ if (sp->actable[0]!=0)
+ _TIFFfree(sp->actable[0]);
+ if (sp->actable[1]!=0)
+ _TIFFfree(sp->actable[1]);
+ if (sp->actable[2]!=0)
+ _TIFFfree(sp->actable[2]);
+ if (sp->actable[3]!=0)
+ _TIFFfree(sp->actable[3]);
+ if (sp->libjpeg_session_active!=0)
+ OJPEGLibjpegSessionAbort(tif);
+ if (sp->subsampling_convert_ycbcrbuf!=0)
+ _TIFFfree(sp->subsampling_convert_ycbcrbuf);
+ if (sp->subsampling_convert_ycbcrimage!=0)
+ _TIFFfree(sp->subsampling_convert_ycbcrimage);
+ if (sp->skip_buffer!=0)
+ _TIFFfree(sp->skip_buffer);
+ _TIFFfree(sp);
+ tif->tif_data=NULL;
+ _TIFFSetDefaultCompressionState(tif);
+ }
+}
+
+static void
+OJPEGSubsamplingCorrect(TIFF* tif)
+{
+ static const char module[]="OJPEGSubsamplingCorrect";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 mh;
+ uint8 mv;
+ _TIFFFillStriles( tif );
+
+ assert(sp->subsamplingcorrect_done==0);
+ if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
+ (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
+ {
+ if (sp->subsampling_tag!=0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ sp->subsampling_force_desubsampling_inside_decompression=0;
+ }
+ else
+ {
+ sp->subsamplingcorrect_done=1;
+ mh=sp->subsampling_hor;
+ mv=sp->subsampling_ver;
+ sp->subsamplingcorrect=1;
+ OJPEGReadHeaderInfoSec(tif);
+ if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+ {
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ }
+ sp->subsamplingcorrect=0;
+ if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
+ {
+ if (sp->subsampling_tag==0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
+ else
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
+ }
+ if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+ {
+ if (sp->subsampling_tag==0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
+ else
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
+ }
+ if (sp->subsampling_force_desubsampling_inside_decompression==0)
+ {
+ if (sp->subsampling_hor<sp->subsampling_ver)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
+ }
+ }
+ sp->subsamplingcorrect_done=1;
+}
+
+static int
+OJPEGReadHeaderInfo(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfo";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(sp->readheader_done==0);
+ sp->image_width=tif->tif_dir.td_imagewidth;
+ sp->image_length=tif->tif_dir.td_imagelength;
+ if isTiled(tif)
+ {
+ sp->strile_width=tif->tif_dir.td_tilewidth;
+ sp->strile_length=tif->tif_dir.td_tilelength;
+ sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
+ }
+ else
+ {
+ sp->strile_width=sp->image_width;
+ sp->strile_length=tif->tif_dir.td_rowsperstrip;
+ sp->strile_length_total=sp->image_length;
+ }
+ if (tif->tif_dir.td_samplesperpixel==1)
+ {
+ sp->samples_per_pixel=1;
+ sp->plane_sample_offset=0;
+ sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ }
+ else
+ {
+ if (tif->tif_dir.td_samplesperpixel!=3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
+ return(0);
+ }
+ sp->samples_per_pixel=3;
+ sp->plane_sample_offset=0;
+ if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
+ sp->samples_per_pixel_per_plane=3;
+ else
+ sp->samples_per_pixel_per_plane=1;
+ }
+ if (sp->strile_length<sp->image_length)
+ {
+ if (sp->strile_length%(sp->subsampling_ver*8)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
+ return(0);
+ }
+ sp->restart_interval=(uint16)(((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8)));
+ }
+ if (OJPEGReadHeaderInfoSec(tif)==0)
+ return(0);
+ sp->sos_end[0].log=1;
+ sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
+ sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
+ sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+ sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+ sp->readheader_done=1;
+ return(1);
+}
+
+static int
+OJPEGReadSecondarySos(TIFF* tif, uint16 s)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(s>0);
+ assert(s<3);
+ assert(sp->sos_end[0].log!=0);
+ assert(sp->sos_end[s].log==0);
+ sp->plane_sample_offset=(uint8)(s-1);
+ while(sp->sos_end[sp->plane_sample_offset].log==0)
+ sp->plane_sample_offset--;
+ sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
+ sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
+ sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
+ sp->in_buffer_file_pos_log=0;
+ sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
+ sp->in_buffer_togo=0;
+ sp->in_buffer_cur=0;
+ while(sp->plane_sample_offset<s)
+ {
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ if (m==255)
+ {
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ if (m!=255)
+ break;
+ } while(1);
+ if (m==JPEG_MARKER_SOS)
+ break;
+ }
+ } while(1);
+ sp->plane_sample_offset++;
+ if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+ return(0);
+ sp->sos_end[sp->plane_sample_offset].log=1;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+ }
+ return(1);
+}
+
+static int
+OJPEGWriteHeaderInfo(TIFF* tif)
+{
+ static const char module[]="OJPEGWriteHeaderInfo";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8** m;
+ uint32 n;
+ /* if a previous attempt failed, don't try again */
+ if (sp->libjpeg_session_active != 0)
+ return 0;
+ sp->out_state=ososSoi;
+ sp->restart_index=0;
+ jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
+ sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
+ sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
+ sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
+ sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
+ if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+ return(0);
+ sp->libjpeg_session_active=1;
+ sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
+ sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
+ sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
+ sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
+ sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
+ sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
+ sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
+ if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
+ return(0);
+ if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
+ {
+ sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
+#if JPEG_LIB_VERSION >= 70
+ sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
+#endif
+ sp->libjpeg_jpeg_query_style=0;
+ if (sp->subsampling_convert_log==0)
+ {
+ assert(sp->subsampling_convert_ycbcrbuf==0);
+ assert(sp->subsampling_convert_ycbcrimage==0);
+ sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
+ sp->subsampling_convert_ylines=sp->subsampling_ver*8;
+ sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
+ sp->subsampling_convert_clines=8;
+ sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
+ sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
+ sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
+ sp->subsampling_convert_ycbcrbuf=_TIFFmalloc(sp->subsampling_convert_ycbcrbuflen);
+ if (sp->subsampling_convert_ycbcrbuf==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
+ sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
+ sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
+ sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
+ sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
+ if (sp->subsampling_convert_ycbcrimage==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ m=sp->subsampling_convert_ycbcrimage;
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
+ for (n=0; n<sp->subsampling_convert_ylines; n++)
+ *m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
+ for (n=0; n<sp->subsampling_convert_clines; n++)
+ *m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
+ for (n=0; n<sp->subsampling_convert_clines; n++)
+ *m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
+ sp->subsampling_convert_clinelenout=((sp->strile_width+sp->subsampling_hor-1)/sp->subsampling_hor);
+ sp->subsampling_convert_state=0;
+ sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
+ sp->lines_per_strile=((sp->strile_length+sp->subsampling_ver-1)/sp->subsampling_ver);
+ sp->subsampling_convert_log=1;
+ }
+ }
+ else
+ {
+ sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
+ sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
+ sp->libjpeg_jpeg_query_style=1;
+ sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
+ sp->lines_per_strile=sp->strile_length;
+ }
+ if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+ return(0);
+ sp->writeheader_done=1;
+ return(1);
+}
+
+static void
+OJPEGLibjpegSessionAbort(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(sp->libjpeg_session_active!=0);
+ jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
+ sp->libjpeg_session_active=0;
+}
+
+static int
+OJPEGReadHeaderInfoSec(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSec";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint16 n;
+ uint8 o;
+ if (sp->file_size==0)
+ sp->file_size=TIFFGetFileSize(tif);
+ if (sp->jpeg_interchange_format!=0)
+ {
+ if (sp->jpeg_interchange_format>=sp->file_size)
+ {
+ sp->jpeg_interchange_format=0;
+ sp->jpeg_interchange_format_length=0;
+ }
+ else
+ {
+ if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
+ sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
+ }
+ }
+ sp->in_buffer_source=osibsNotSetYet;
+ sp->in_buffer_next_strile=0;
+ sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
+ sp->in_buffer_file_togo=0;
+ sp->in_buffer_togo=0;
+ do
+ {
+ if (OJPEGReadBytePeek(sp,&m)==0)
+ return(0);
+ if (m!=255)
+ break;
+ OJPEGReadByteAdvance(sp);
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ } while(m==255);
+ switch(m)
+ {
+ case JPEG_MARKER_SOI:
+ /* this type of marker has no data, and should be skipped */
+ break;
+ case JPEG_MARKER_COM:
+ case JPEG_MARKER_APP0:
+ case JPEG_MARKER_APP0+1:
+ case JPEG_MARKER_APP0+2:
+ case JPEG_MARKER_APP0+3:
+ case JPEG_MARKER_APP0+4:
+ case JPEG_MARKER_APP0+5:
+ case JPEG_MARKER_APP0+6:
+ case JPEG_MARKER_APP0+7:
+ case JPEG_MARKER_APP0+8:
+ case JPEG_MARKER_APP0+9:
+ case JPEG_MARKER_APP0+10:
+ case JPEG_MARKER_APP0+11:
+ case JPEG_MARKER_APP0+12:
+ case JPEG_MARKER_APP0+13:
+ case JPEG_MARKER_APP0+14:
+ case JPEG_MARKER_APP0+15:
+ /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
+ if (OJPEGReadWord(sp,&n)==0)
+ return(0);
+ if (n<2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+ return(0);
+ }
+ if (n>2)
+ OJPEGReadSkip(sp,n-2);
+ break;
+ case JPEG_MARKER_DRI:
+ if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_DQT:
+ if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_DHT:
+ if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_SOF0:
+ case JPEG_MARKER_SOF1:
+ case JPEG_MARKER_SOF3:
+ if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
+ return(0);
+ if (sp->subsamplingcorrect!=0)
+ return(1);
+ break;
+ case JPEG_MARKER_SOS:
+ if (sp->subsamplingcorrect!=0)
+ return(1);
+ assert(sp->plane_sample_offset==0);
+ if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+ return(0);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
+ return(0);
+ }
+ } while(m!=JPEG_MARKER_SOS);
+ if (sp->subsamplingcorrect)
+ return(1);
+ if (sp->sof_log==0)
+ {
+ if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
+ return(0);
+ sp->sof_marker_id=JPEG_MARKER_SOF0;
+ for (o=0; o<sp->samples_per_pixel; o++)
+ sp->sof_c[o]=o;
+ sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
+ for (o=1; o<sp->samples_per_pixel; o++)
+ sp->sof_hv[o]=17;
+ sp->sof_x=sp->strile_width;
+ sp->sof_y=sp->strile_length_total;
+ sp->sof_log=1;
+ if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
+ return(0);
+ if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
+ return(0);
+ for (o=1; o<sp->samples_per_pixel; o++)
+ sp->sos_cs[o]=o;
+ }
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
+{
+ /* This could easily cause trouble in some cases... but no such cases have
+ occurred so far */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m!=4)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
+ return(0);
+ }
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ sp->restart_interval=m;
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
+{
+ /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint32 na;
+ uint8* nb;
+ uint8 o;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<=2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect!=0)
+ OJPEGReadSkip(sp,m-2);
+ else
+ {
+ m-=2;
+ do
+ {
+ if (m<65)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ return(0);
+ }
+ na=sizeof(uint32)+69;
+ nb=_TIFFmalloc(na);
+ if (nb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)nb=na;
+ nb[sizeof(uint32)]=255;
+ nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+ nb[sizeof(uint32)+2]=0;
+ nb[sizeof(uint32)+3]=67;
+ if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0) {
+ _TIFFfree(nb);
+ return(0);
+ }
+ o=nb[sizeof(uint32)+4]&15;
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ _TIFFfree(nb);
+ return(0);
+ }
+ if (sp->qtable[o]!=0)
+ _TIFFfree(sp->qtable[o]);
+ sp->qtable[o]=nb;
+ m-=65;
+ } while(m>0);
+ }
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
+{
+ /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+ /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint32 na;
+ uint8* nb;
+ uint8 o;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<=2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect!=0)
+ {
+ OJPEGReadSkip(sp,m-2);
+ }
+ else
+ {
+ na=sizeof(uint32)+2+m;
+ nb=_TIFFmalloc(na);
+ if (nb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)nb=na;
+ nb[sizeof(uint32)]=255;
+ nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ nb[sizeof(uint32)+2]=(m>>8);
+ nb[sizeof(uint32)+3]=(m&255);
+ if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0) {
+ _TIFFfree(nb);
+ return(0);
+ }
+ o=nb[sizeof(uint32)+4];
+ if ((o&240)==0)
+ {
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ _TIFFfree(nb);
+ return(0);
+ }
+ if (sp->dctable[o]!=0)
+ _TIFFfree(sp->dctable[o]);
+ sp->dctable[o]=nb;
+ }
+ else
+ {
+ if ((o&240)!=16)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ _TIFFfree(nb);
+ return(0);
+ }
+ o&=15;
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ _TIFFfree(nb);
+ return(0);
+ }
+ if (sp->actable[o]!=0)
+ _TIFFfree(sp->actable[o]);
+ sp->actable[o]=nb;
+ }
+ }
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
+{
+ /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint16 n;
+ uint8 o;
+ uint16 p;
+ uint16 q;
+ if (sp->sof_log!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect==0)
+ sp->sof_marker_id=marker_id;
+ /* Lf: data length */
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<11)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ m-=8;
+ if (m%3!=0)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ n=m/3;
+ if (sp->subsamplingcorrect==0)
+ {
+ if (n!=sp->samples_per_pixel)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
+ return(0);
+ }
+ }
+ /* P: Sample precision */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (o!=8)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
+ return(0);
+ }
+ /* Y: Number of lines, X: Number of samples per line */
+ if (sp->subsamplingcorrect)
+ OJPEGReadSkip(sp,4);
+ else
+ {
+ /* Y: Number of lines */
+ if (OJPEGReadWord(sp,&p)==0)
+ return(0);
+ if (((uint32)p<sp->image_length) && ((uint32)p<sp->strile_length_total))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
+ return(0);
+ }
+ sp->sof_y=p;
+ /* X: Number of samples per line */
+ if (OJPEGReadWord(sp,&p)==0)
+ return(0);
+ if (((uint32)p<sp->image_width) && ((uint32)p<sp->strile_width))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
+ return(0);
+ }
+ if ((uint32)p>sp->strile_width)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data image width exceeds expected image width");
+ return(0);
+ }
+ sp->sof_x=p;
+ }
+ /* Nf: Number of image components in frame */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (o!=n)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ /* per component stuff */
+ /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
+ for (q=0; q<n; q++)
+ {
+ /* C: Component identifier */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect==0)
+ sp->sof_c[q]=o;
+ /* H: Horizontal sampling factor, and V: Vertical sampling factor */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect!=0)
+ {
+ if (q==0)
+ {
+ sp->subsampling_hor=(o>>4);
+ sp->subsampling_ver=(o&15);
+ if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
+ ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
+ sp->subsampling_force_desubsampling_inside_decompression=1;
+ }
+ else
+ {
+ if (o!=17)
+ sp->subsampling_force_desubsampling_inside_decompression=1;
+ }
+ }
+ else
+ {
+ sp->sof_hv[q]=o;
+ if (sp->subsampling_force_desubsampling_inside_decompression==0)
+ {
+ if (q==0)
+ {
+ if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+ return(0);
+ }
+ }
+ else
+ {
+ if (o!=17)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+ return(0);
+ }
+ }
+ }
+ }
+ /* Tq: Quantization table destination selector */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect==0)
+ sp->sof_tq[q]=o;
+ }
+ if (sp->subsamplingcorrect==0)
+ sp->sof_log=1;
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
+{
+ /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint8 n;
+ uint8 o;
+ assert(sp->subsamplingcorrect==0);
+ if (sp->sof_log==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Ls */
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m!=6+sp->samples_per_pixel_per_plane*2)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Ns */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ if (n!=sp->samples_per_pixel_per_plane)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Cs, Td, and Ta */
+ for (o=0; o<sp->samples_per_pixel_per_plane; o++)
+ {
+ /* Cs */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ sp->sos_cs[sp->plane_sample_offset+o]=n;
+ /* Td and Ta */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ sp->sos_tda[sp->plane_sample_offset+o]=n;
+ }
+ /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
+ OJPEGReadSkip(sp,3);
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint32 oa;
+ uint8* ob;
+ uint32 p;
+ if (sp->qtable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->qtable_offset[m]==sp->qtable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
+ return(0);
+ }
+ }
+ oa=sizeof(uint32)+69;
+ ob=_TIFFmalloc(oa);
+ if (ob==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)ob=oa;
+ ob[sizeof(uint32)]=255;
+ ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+ ob[sizeof(uint32)+2]=0;
+ ob[sizeof(uint32)+3]=67;
+ ob[sizeof(uint32)+4]=m;
+ TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
+ p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
+ if (p!=64)
+ {
+ _TIFFfree(ob);
+ return(0);
+ }
+ if (sp->qtable[m]!=0)
+ _TIFFfree(sp->qtable[m]);
+ sp->qtable[m]=ob;
+ sp->sof_tq[m]=m;
+ }
+ else
+ sp->sof_tq[m]=sp->sof_tq[m-1];
+ }
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint8 o[16];
+ uint32 p;
+ uint32 q;
+ uint32 ra;
+ uint8* rb;
+ if (sp->dctable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->dctable_offset[m]==sp->dctable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
+ return(0);
+ }
+ }
+ TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
+ p=(uint32)TIFFReadFile(tif,o,16);
+ if (p!=16)
+ return(0);
+ q=0;
+ for (n=0; n<16; n++)
+ q+=o[n];
+ ra=sizeof(uint32)+21+q;
+ rb=_TIFFmalloc(ra);
+ if (rb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)rb=ra;
+ rb[sizeof(uint32)]=255;
+ rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
+ rb[sizeof(uint32)+3]=((19+q)&255);
+ rb[sizeof(uint32)+4]=m;
+ for (n=0; n<16; n++)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
+ {
+ _TIFFfree(rb);
+ return(0);
+ }
+ if (sp->dctable[m]!=0)
+ _TIFFfree(sp->dctable[m]);
+ sp->dctable[m]=rb;
+ sp->sos_tda[m]=(m<<4);
+ }
+ else
+ sp->sos_tda[m]=sp->sos_tda[m-1];
+ }
+ return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint8 o[16];
+ uint32 p;
+ uint32 q;
+ uint32 ra;
+ uint8* rb;
+ if (sp->actable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->actable_offset[m]==sp->actable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
+ return(0);
+ }
+ }
+ TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
+ p=(uint32)TIFFReadFile(tif,o,16);
+ if (p!=16)
+ return(0);
+ q=0;
+ for (n=0; n<16; n++)
+ q+=o[n];
+ ra=sizeof(uint32)+21+q;
+ rb=_TIFFmalloc(ra);
+ if (rb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)rb=ra;
+ rb[sizeof(uint32)]=255;
+ rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
+ rb[sizeof(uint32)+3]=((19+q)&255);
+ rb[sizeof(uint32)+4]=(16|m);
+ for (n=0; n<16; n++)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
+ {
+ _TIFFfree(rb);
+ return(0);
+ }
+ if (sp->actable[m]!=0)
+ _TIFFfree(sp->actable[m]);
+ sp->actable[m]=rb;
+ sp->sos_tda[m]=(sp->sos_tda[m]|m);
+ }
+ else
+ sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
+ }
+ return(1);
+}
+
+static int
+OJPEGReadBufferFill(OJPEGState* sp)
+{
+ uint16 m;
+ tmsize_t n;
+ /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
+ * in any other case, seek or read errors should be passed through */
+ do
+ {
+ if (sp->in_buffer_file_togo!=0)
+ {
+ if (sp->in_buffer_file_pos_log==0)
+ {
+ TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
+ sp->in_buffer_file_pos_log=1;
+ }
+ m=OJPEG_BUFFER;
+ if ((uint64)m>sp->in_buffer_file_togo)
+ m=(uint16)sp->in_buffer_file_togo;
+ n=TIFFReadFile(sp->tif,sp->in_buffer,(tmsize_t)m);
+ if (n==0)
+ return(0);
+ assert(n>0);
+ assert(n<=OJPEG_BUFFER);
+ assert(n<65536);
+ assert((uint64)n<=sp->in_buffer_file_togo);
+ m=(uint16)n;
+ sp->in_buffer_togo=m;
+ sp->in_buffer_cur=sp->in_buffer;
+ sp->in_buffer_file_togo-=m;
+ sp->in_buffer_file_pos+=m;
+ break;
+ }
+ sp->in_buffer_file_pos_log=0;
+ switch(sp->in_buffer_source)
+ {
+ case osibsNotSetYet:
+ if (sp->jpeg_interchange_format!=0)
+ {
+ sp->in_buffer_file_pos=sp->jpeg_interchange_format;
+ sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
+ }
+ sp->in_buffer_source=osibsJpegInterchangeFormat;
+ break;
+ case osibsJpegInterchangeFormat:
+ sp->in_buffer_source=osibsStrile;
+ break;
+ case osibsStrile:
+ if (!_TIFFFillStriles( sp->tif )
+ || sp->tif->tif_dir.td_stripoffset == NULL
+ || sp->tif->tif_dir.td_stripbytecount == NULL)
+ return 0;
+
+ if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)
+ sp->in_buffer_source=osibsEof;
+ else
+ {
+ sp->in_buffer_file_pos=sp->tif->tif_dir.td_stripoffset[sp->in_buffer_next_strile];
+ if (sp->in_buffer_file_pos!=0)
+ {
+ if (sp->in_buffer_file_pos>=sp->file_size)
+ sp->in_buffer_file_pos=0;
+ else if (sp->tif->tif_dir.td_stripbytecount==NULL)
+ sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
+ else
+ {
+ if (sp->tif->tif_dir.td_stripbytecount == 0) {
+ TIFFErrorExt(sp->tif->tif_clientdata,sp->tif->tif_name,"Strip byte counts are missing");
+ return(0);
+ }
+ sp->in_buffer_file_togo=sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];
+ if (sp->in_buffer_file_togo==0)
+ sp->in_buffer_file_pos=0;
+ else if (sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
+ sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
+ }
+ }
+ sp->in_buffer_next_strile++;
+ }
+ break;
+ default:
+ return(0);
+ }
+ } while (1);
+ return(1);
+}
+
+static int
+OJPEGReadByte(OJPEGState* sp, uint8* byte)
+{
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *byte=*(sp->in_buffer_cur);
+ sp->in_buffer_cur++;
+ sp->in_buffer_togo--;
+ return(1);
+}
+
+static int
+OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
+{
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *byte=*(sp->in_buffer_cur);
+ return(1);
+}
+
+static void
+OJPEGReadByteAdvance(OJPEGState* sp)
+{
+ assert(sp->in_buffer_togo>0);
+ sp->in_buffer_cur++;
+ sp->in_buffer_togo--;
+}
+
+static int
+OJPEGReadWord(OJPEGState* sp, uint16* word)
+{
+ uint8 m;
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ *word=(m<<8);
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ *word|=m;
+ return(1);
+}
+
+static int
+OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
+{
+ uint16 mlen;
+ uint8* mmem;
+ uint16 n;
+ assert(len>0);
+ mlen=len;
+ mmem=mem;
+ do
+ {
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ n=mlen;
+ if (n>sp->in_buffer_togo)
+ n=sp->in_buffer_togo;
+ _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
+ sp->in_buffer_cur+=n;
+ sp->in_buffer_togo-=n;
+ mlen-=n;
+ mmem+=n;
+ } while(mlen>0);
+ return(1);
+}
+
+static void
+OJPEGReadSkip(OJPEGState* sp, uint16 len)
+{
+ uint16 m;
+ uint16 n;
+ m=len;
+ n=m;
+ if (n>sp->in_buffer_togo)
+ n=sp->in_buffer_togo;
+ sp->in_buffer_cur+=n;
+ sp->in_buffer_togo-=n;
+ m-=n;
+ if (m>0)
+ {
+ assert(sp->in_buffer_togo==0);
+ n=m;
+ if ((uint64)n>sp->in_buffer_file_togo)
+ n=(uint16)sp->in_buffer_file_togo;
+ sp->in_buffer_file_pos+=n;
+ sp->in_buffer_file_togo-=n;
+ sp->in_buffer_file_pos_log=0;
+ /* we don't skip past jpeginterchangeformat/strile block...
+ * if that is asked from us, we're dealing with totally bazurk
+ * data anyway, and we've not seen this happening on any
+ * testfile, so we might as well likely cause some other
+ * meaningless error to be passed at some later time
+ */
+ }
+}
+
+static int
+OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ *len=0;
+ do
+ {
+ assert(sp->out_state<=ososEoi);
+ switch(sp->out_state)
+ {
+ case ososSoi:
+ OJPEGWriteStreamSoi(tif,mem,len);
+ break;
+ case ososQTable0:
+ OJPEGWriteStreamQTable(tif,0,mem,len);
+ break;
+ case ososQTable1:
+ OJPEGWriteStreamQTable(tif,1,mem,len);
+ break;
+ case ososQTable2:
+ OJPEGWriteStreamQTable(tif,2,mem,len);
+ break;
+ case ososQTable3:
+ OJPEGWriteStreamQTable(tif,3,mem,len);
+ break;
+ case ososDcTable0:
+ OJPEGWriteStreamDcTable(tif,0,mem,len);
+ break;
+ case ososDcTable1:
+ OJPEGWriteStreamDcTable(tif,1,mem,len);
+ break;
+ case ososDcTable2:
+ OJPEGWriteStreamDcTable(tif,2,mem,len);
+ break;
+ case ososDcTable3:
+ OJPEGWriteStreamDcTable(tif,3,mem,len);
+ break;
+ case ososAcTable0:
+ OJPEGWriteStreamAcTable(tif,0,mem,len);
+ break;
+ case ososAcTable1:
+ OJPEGWriteStreamAcTable(tif,1,mem,len);
+ break;
+ case ososAcTable2:
+ OJPEGWriteStreamAcTable(tif,2,mem,len);
+ break;
+ case ososAcTable3:
+ OJPEGWriteStreamAcTable(tif,3,mem,len);
+ break;
+ case ososDri:
+ OJPEGWriteStreamDri(tif,mem,len);
+ break;
+ case ososSof:
+ OJPEGWriteStreamSof(tif,mem,len);
+ break;
+ case ososSos:
+ OJPEGWriteStreamSos(tif,mem,len);
+ break;
+ case ososCompressed:
+ if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
+ return(0);
+ break;
+ case ososRst:
+ OJPEGWriteStreamRst(tif,mem,len);
+ break;
+ case ososEoi:
+ OJPEGWriteStreamEoi(tif,mem,len);
+ break;
+ }
+ } while (*len==0);
+ return(1);
+}
+
+static void
+OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_SOI;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->qtable[table_index]!=0)
+ {
+ *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->dctable[table_index]!=0)
+ {
+ *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->actable[table_index]!=0)
+ {
+ *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=6);
+ if (sp->restart_interval!=0)
+ {
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_DRI;
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=4;
+ sp->out_buffer[4]=(sp->restart_interval>>8);
+ sp->out_buffer[5]=(sp->restart_interval&255);
+ *len=6;
+ *mem=(void*)sp->out_buffer;
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
+ assert(255>=8+sp->samples_per_pixel_per_plane*3);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=sp->sof_marker_id;
+ /* Lf */
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
+ /* P */
+ sp->out_buffer[4]=8;
+ /* Y */
+ sp->out_buffer[5]=(uint8)(sp->sof_y>>8);
+ sp->out_buffer[6]=(sp->sof_y&255);
+ /* X */
+ sp->out_buffer[7]=(uint8)(sp->sof_x>>8);
+ sp->out_buffer[8]=(sp->sof_x&255);
+ /* Nf */
+ sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
+ for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+ {
+ /* C */
+ sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
+ /* H and V */
+ sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
+ /* Tq */
+ sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
+ }
+ *len=10+sp->samples_per_pixel_per_plane*3;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
+ assert(255>=6+sp->samples_per_pixel_per_plane*2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_SOS;
+ /* Ls */
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
+ /* Ns */
+ sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
+ for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+ {
+ /* Cs */
+ sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
+ /* Td and Ta */
+ sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
+ }
+ /* Ss */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
+ /* Se */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
+ /* Ah and Al */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
+ *len=8+sp->samples_per_pixel_per_plane*2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static int
+OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *len=sp->in_buffer_togo;
+ *mem=(void*)sp->in_buffer_cur;
+ sp->in_buffer_togo=0;
+ if (sp->in_buffer_file_togo==0)
+ {
+ switch(sp->in_buffer_source)
+ {
+ case osibsStrile:
+ if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)
+ sp->out_state=ososRst;
+ else
+ sp->out_state=ososEoi;
+ break;
+ case osibsEof:
+ sp->out_state=ososEoi;
+ break;
+ default:
+ break;
+ }
+ }
+ return(1);
+}
+
+static void
+OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
+ sp->restart_index++;
+ if (sp->restart_index==8)
+ sp->restart_index=0;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state=ososCompressed;
+}
+
+static void
+OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_EOI;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+}
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+ if( SETJMP(sp->exit_jmpbuf) )
+ return 0;
+ else {
+ jpeg_create_decompress(cinfo);
+ return 1;
+ }
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
+{
+ if( SETJMP(sp->exit_jmpbuf) )
+ return 0;
+ else {
+ jpeg_read_header(cinfo,require_image);
+ return 1;
+ }
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+ if( SETJMP(sp->exit_jmpbuf) )
+ return 0;
+ else {
+ jpeg_start_decompress(cinfo);
+ return 1;
+ }
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
+{
+ if( SETJMP(sp->exit_jmpbuf) )
+ return 0;
+ else {
+ jpeg_read_scanlines(cinfo,scanlines,max_lines);
+ return 1;
+ }
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
+{
+ if( SETJMP(sp->exit_jmpbuf) )
+ return 0;
+ else {
+ jpeg_read_raw_data(cinfo,data,max_lines);
+ return 1;
+ }
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static void
+jpeg_encap_unwind(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ LONGJMP(sp->exit_jmpbuf,1);
+}
+#endif
+
+static void
+OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+ (*cinfo->err->format_message)(cinfo,buffer);
+ TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
+}
+
+static void
+OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+ (*cinfo->err->format_message)(cinfo,buffer);
+ TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
+ jpeg_encap_unwind((TIFF*)(cinfo->client_data));
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
+{
+ (void)cinfo;
+}
+
+static boolean
+OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ void* mem=0;
+ uint32 len=0U;
+ if (OJPEGWriteStream(tif,&mem,&len)==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
+ jpeg_encap_unwind(tif);
+ }
+ sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
+ sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
+ return(1);
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ (void)num_bytes;
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+ jpeg_encap_unwind(tif);
+}
+
+#ifdef _MSC_VER
+#pragma warning( push )
+#pragma warning( disable : 4702 ) /* unreachable code */
+#endif
+static boolean
+OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ (void)desired;
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+ jpeg_encap_unwind(tif);
+ return(0);
+}
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif
+
+static void
+OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
+{
+ (void)cinfo;
+}
+
+#endif
+
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_open.c b/test/monniaux/tiff-4.0.10/tif_open.c
new file mode 100644
index 00000000..c574c452
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_open.c
@@ -0,0 +1,723 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ */
+#include "tiffiop.h"
+
+/*
+ * Dummy functions to fill the omitted client procedures.
+ */
+static int
+_tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
+{
+ (void) fd; (void) pbase; (void) psize;
+ return (0);
+}
+
+static void
+_tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
+{
+ (void) fd; (void) base; (void) size;
+}
+
+int
+_TIFFgetMode(const char* mode, const char* module)
+{
+ int m = -1;
+
+ switch (mode[0]) {
+ case 'r':
+ m = O_RDONLY;
+ if (mode[1] == '+')
+ m = O_RDWR;
+ break;
+ case 'w':
+ case 'a':
+ m = O_RDWR|O_CREAT;
+ if (mode[0] == 'w')
+ m |= O_TRUNC;
+ break;
+ default:
+ TIFFErrorExt(0, module, "\"%s\": Bad mode", mode);
+ break;
+ }
+ return (m);
+}
+
+TIFF*
+TIFFClientOpen(
+ const char* name, const char* mode,
+ thandle_t clientdata,
+ TIFFReadWriteProc readproc,
+ TIFFReadWriteProc writeproc,
+ TIFFSeekProc seekproc,
+ TIFFCloseProc closeproc,
+ TIFFSizeProc sizeproc,
+ TIFFMapFileProc mapproc,
+ TIFFUnmapFileProc unmapproc
+)
+{
+ static const char module[] = "TIFFClientOpen";
+ TIFF *tif;
+ int m;
+ const char* cp;
+
+ /* The following are configuration checks. They should be redundant, but should not
+ * compile to any actual code in an optimised release build anyway. If any of them
+ * fail, (makefile-based or other) configuration is not correct */
+ assert(sizeof(uint8)==1);
+ assert(sizeof(int8)==1);
+ assert(sizeof(uint16)==2);
+ assert(sizeof(int16)==2);
+ assert(sizeof(uint32)==4);
+ assert(sizeof(int32)==4);
+ assert(sizeof(uint64)==8);
+ assert(sizeof(int64)==8);
+ assert(sizeof(tmsize_t)==sizeof(void*));
+ {
+ union{
+ uint8 a8[2];
+ uint16 a16;
+ } n;
+ n.a8[0]=1;
+ n.a8[1]=0;
+ #ifdef WORDS_BIGENDIAN
+ assert(n.a16==256);
+ #else
+ assert(n.a16==1);
+ #endif
+ }
+
+ m = _TIFFgetMode(mode, module);
+ if (m == -1)
+ goto bad2;
+ tif = (TIFF *)_TIFFmalloc((tmsize_t)(sizeof (TIFF) + strlen(name) + 1));
+ if (tif == NULL) {
+ TIFFErrorExt(clientdata, module, "%s: Out of memory (TIFF structure)", name);
+ goto bad2;
+ }
+ _TIFFmemset(tif, 0, sizeof (*tif));
+ tif->tif_name = (char *)tif + sizeof (TIFF);
+ strcpy(tif->tif_name, name);
+ tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
+ tif->tif_curdir = (uint16) -1; /* non-existent directory */
+ tif->tif_curoff = 0;
+ tif->tif_curstrip = (uint32) -1; /* invalid strip */
+ tif->tif_row = (uint32) -1; /* read/write pre-increment */
+ tif->tif_clientdata = clientdata;
+ if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {
+ TIFFErrorExt(clientdata, module,
+ "One of the client procedures is NULL pointer.");
+ goto bad2;
+ }
+ tif->tif_readproc = readproc;
+ tif->tif_writeproc = writeproc;
+ tif->tif_seekproc = seekproc;
+ tif->tif_closeproc = closeproc;
+ tif->tif_sizeproc = sizeproc;
+ if (mapproc)
+ tif->tif_mapproc = mapproc;
+ else
+ tif->tif_mapproc = _tiffDummyMapProc;
+ if (unmapproc)
+ tif->tif_unmapproc = unmapproc;
+ else
+ tif->tif_unmapproc = _tiffDummyUnmapProc;
+ _TIFFSetDefaultCompressionState(tif); /* setup default state */
+ /*
+ * Default is to return data MSB2LSB and enable the
+ * use of memory-mapped files and strip chopping when
+ * a file is opened read-only.
+ */
+ tif->tif_flags = FILLORDER_MSB2LSB;
+ if (m == O_RDONLY )
+ tif->tif_flags |= TIFF_MAPPED;
+
+ #ifdef STRIPCHOP_DEFAULT
+ if (m == O_RDONLY || m == O_RDWR)
+ tif->tif_flags |= STRIPCHOP_DEFAULT;
+ #endif
+
+ /*
+ * Process library-specific flags in the open mode string.
+ * The following flags may be used to control intrinsic library
+ * behaviour that may or may not be desirable (usually for
+ * compatibility with some application that claims to support
+ * TIFF but only supports some brain dead idea of what the
+ * vendor thinks TIFF is):
+ *
+ * 'l' use little-endian byte order for creating a file
+ * 'b' use big-endian byte order for creating a file
+ * 'L' read/write information using LSB2MSB bit order
+ * 'B' read/write information using MSB2LSB bit order
+ * 'H' read/write information using host bit order
+ * 'M' enable use of memory-mapped files when supported
+ * 'm' disable use of memory-mapped files
+ * 'C' enable strip chopping support when reading
+ * 'c' disable strip chopping support
+ * 'h' read TIFF header only, do not load the first IFD
+ * '4' ClassicTIFF for creating a file (default)
+ * '8' BigTIFF for creating a file
+ *
+ * The use of the 'l' and 'b' flags is strongly discouraged.
+ * These flags are provided solely because numerous vendors,
+ * typically on the PC, do not correctly support TIFF; they
+ * only support the Intel little-endian byte order. This
+ * support is not configured by default because it supports
+ * the violation of the TIFF spec that says that readers *MUST*
+ * support both byte orders. It is strongly recommended that
+ * you not use this feature except to deal with busted apps
+ * that write invalid TIFF. And even in those cases you should
+ * bang on the vendors to fix their software.
+ *
+ * The 'L', 'B', and 'H' flags are intended for applications
+ * that can optimize operations on data by using a particular
+ * bit order. By default the library returns data in MSB2LSB
+ * bit order for compatibility with older versions of this
+ * library. Returning data in the bit order of the native CPU
+ * makes the most sense but also requires applications to check
+ * the value of the FillOrder tag; something they probably do
+ * not do right now.
+ *
+ * The 'M' and 'm' flags are provided because some virtual memory
+ * systems exhibit poor behaviour when large images are mapped.
+ * These options permit clients to control the use of memory-mapped
+ * files on a per-file basis.
+ *
+ * The 'C' and 'c' flags are provided because the library support
+ * for chopping up large strips into multiple smaller strips is not
+ * application-transparent and as such can cause problems. The 'c'
+ * option permits applications that only want to look at the tags,
+ * for example, to get the unadulterated TIFF tag information.
+ */
+ for (cp = mode; *cp; cp++)
+ switch (*cp) {
+ case 'b':
+ #ifndef WORDS_BIGENDIAN
+ if (m&O_CREAT)
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ break;
+ case 'l':
+ #ifdef WORDS_BIGENDIAN
+ if ((m&O_CREAT))
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ break;
+ case 'B':
+ tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
+ FILLORDER_MSB2LSB;
+ break;
+ case 'L':
+ tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
+ FILLORDER_LSB2MSB;
+ break;
+ case 'H':
+ tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
+ HOST_FILLORDER;
+ break;
+ case 'M':
+ if (m == O_RDONLY)
+ tif->tif_flags |= TIFF_MAPPED;
+ break;
+ case 'm':
+ if (m == O_RDONLY)
+ tif->tif_flags &= ~TIFF_MAPPED;
+ break;
+ case 'C':
+ if (m == O_RDONLY)
+ tif->tif_flags |= TIFF_STRIPCHOP;
+ break;
+ case 'c':
+ if (m == O_RDONLY)
+ tif->tif_flags &= ~TIFF_STRIPCHOP;
+ break;
+ case 'h':
+ tif->tif_flags |= TIFF_HEADERONLY;
+ break;
+ case '8':
+ if (m&O_CREAT)
+ tif->tif_flags |= TIFF_BIGTIFF;
+ break;
+ }
+ /*
+ * Read in TIFF header.
+ */
+ if ((m & O_TRUNC) ||
+ !ReadOK(tif, &tif->tif_header, sizeof (TIFFHeaderClassic))) {
+ if (tif->tif_mode == O_RDONLY) {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Cannot read TIFF header");
+ goto bad;
+ }
+ /*
+ * Setup header and write.
+ */
+ #ifdef WORDS_BIGENDIAN
+ tif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)
+ ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
+ #else
+ tif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)
+ ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
+ #endif
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ {
+ tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
+ tif->tif_header.classic.tiff_diroff = 0;
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&tif->tif_header.common.tiff_version);
+ tif->tif_header_size = sizeof(TIFFHeaderClassic);
+ }
+ else
+ {
+ tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
+ tif->tif_header.big.tiff_offsetsize = 8;
+ tif->tif_header.big.tiff_unused = 0;
+ tif->tif_header.big.tiff_diroff = 0;
+ if (tif->tif_flags & TIFF_SWAB)
+ {
+ TIFFSwabShort(&tif->tif_header.common.tiff_version);
+ TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
+ }
+ tif->tif_header_size = sizeof (TIFFHeaderBig);
+ }
+ /*
+ * The doc for "fopen" for some STD_C_LIBs says that if you
+ * open a file for modify ("+"), then you must fseek (or
+ * fflush?) between any freads and fwrites. This is not
+ * necessary on most systems, but has been shown to be needed
+ * on Solaris.
+ */
+ TIFFSeekFile( tif, 0, SEEK_SET );
+ if (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size))) {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Error writing TIFF header");
+ goto bad;
+ }
+ /*
+ * Setup the byte order handling.
+ */
+ if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {
+ #ifndef WORDS_BIGENDIAN
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ } else {
+ #ifdef WORDS_BIGENDIAN
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ }
+ /*
+ * Setup default directory.
+ */
+ if (!TIFFDefaultDirectory(tif))
+ goto bad;
+ tif->tif_diroff = 0;
+ tif->tif_dirlist = NULL;
+ tif->tif_dirlistsize = 0;
+ tif->tif_dirnumber = 0;
+ return (tif);
+ }
+ /*
+ * Setup the byte order handling.
+ */
+ if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
+ tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
+ #if MDI_SUPPORT
+ &&
+ #if HOST_BIGENDIAN
+ tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
+ #else
+ tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
+ #endif
+ ) {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF or MDI file, bad magic number %d (0x%x)",
+ #else
+ ) {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF file, bad magic number %d (0x%x)",
+ #endif
+ tif->tif_header.common.tiff_magic,
+ tif->tif_header.common.tiff_magic);
+ goto bad;
+ }
+ if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {
+ #ifndef WORDS_BIGENDIAN
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ } else {
+ #ifdef WORDS_BIGENDIAN
+ tif->tif_flags |= TIFF_SWAB;
+ #endif
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&tif->tif_header.common.tiff_version);
+ if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC)&&
+ (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF file, bad version number %d (0x%x)",
+ tif->tif_header.common.tiff_version,
+ tif->tif_header.common.tiff_version);
+ goto bad;
+ }
+ if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
+ {
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
+ tif->tif_header_size = sizeof(TIFFHeaderClassic);
+ }
+ else
+ {
+ if (!ReadOK(tif, ((uint8*)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), (sizeof(TIFFHeaderBig)-sizeof(TIFFHeaderClassic))))
+ {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Cannot read TIFF header");
+ goto bad;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ {
+ TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
+ TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
+ }
+ if (tif->tif_header.big.tiff_offsetsize != 8)
+ {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",
+ tif->tif_header.big.tiff_offsetsize,
+ tif->tif_header.big.tiff_offsetsize);
+ goto bad;
+ }
+ if (tif->tif_header.big.tiff_unused != 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF file, bad BigTIFF unused %d (0x%x)",
+ tif->tif_header.big.tiff_unused,
+ tif->tif_header.big.tiff_unused);
+ goto bad;
+ }
+ tif->tif_header_size = sizeof(TIFFHeaderBig);
+ tif->tif_flags |= TIFF_BIGTIFF;
+ }
+ tif->tif_flags |= TIFF_MYBUFFER;
+ tif->tif_rawcp = tif->tif_rawdata = 0;
+ tif->tif_rawdatasize = 0;
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = 0;
+
+ switch (mode[0]) {
+ case 'r':
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
+ else
+ tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
+ /*
+ * Try to use a memory-mapped file if the client
+ * has not explicitly suppressed usage with the
+ * 'm' flag in the open mode (see above).
+ */
+ if (tif->tif_flags & TIFF_MAPPED)
+ {
+ toff_t n;
+ if (TIFFMapFileContents(tif,(void**)(&tif->tif_base),&n))
+ {
+ tif->tif_size=(tmsize_t)n;
+ assert((toff_t)tif->tif_size==n);
+ }
+ else
+ tif->tif_flags &= ~TIFF_MAPPED;
+ }
+ /*
+ * Sometimes we do not want to read the first directory (for example,
+ * it may be broken) and want to proceed to other directories. I this
+ * case we use the TIFF_HEADERONLY flag to open file and return
+ * immediately after reading TIFF header.
+ */
+ if (tif->tif_flags & TIFF_HEADERONLY)
+ return (tif);
+
+ /*
+ * Setup initial directory.
+ */
+ if (TIFFReadDirectory(tif)) {
+ tif->tif_rawcc = (tmsize_t)-1;
+ tif->tif_flags |= TIFF_BUFFERSETUP;
+ return (tif);
+ }
+ break;
+ case 'a':
+ /*
+ * New directories are automatically append
+ * to the end of the directory chain when they
+ * are written out (see TIFFWriteDirectory).
+ */
+ if (!TIFFDefaultDirectory(tif))
+ goto bad;
+ return (tif);
+ }
+bad:
+ tif->tif_mode = O_RDONLY; /* XXX avoid flush */
+ TIFFCleanup(tif);
+bad2:
+ return ((TIFF*)0);
+}
+
+/*
+ * Query functions to access private data.
+ */
+
+/*
+ * Return open file's name.
+ */
+const char *
+TIFFFileName(TIFF* tif)
+{
+ return (tif->tif_name);
+}
+
+/*
+ * Set the file name.
+ */
+const char *
+TIFFSetFileName(TIFF* tif, const char *name)
+{
+ const char* old_name = tif->tif_name;
+ tif->tif_name = (char *)name;
+ return (old_name);
+}
+
+/*
+ * Return open file's I/O descriptor.
+ */
+int
+TIFFFileno(TIFF* tif)
+{
+ return (tif->tif_fd);
+}
+
+/*
+ * Set open file's I/O descriptor, and return previous value.
+ */
+int
+TIFFSetFileno(TIFF* tif, int fd)
+{
+ int old_fd = tif->tif_fd;
+ tif->tif_fd = fd;
+ return old_fd;
+}
+
+/*
+ * Return open file's clientdata.
+ */
+thandle_t
+TIFFClientdata(TIFF* tif)
+{
+ return (tif->tif_clientdata);
+}
+
+/*
+ * Set open file's clientdata, and return previous value.
+ */
+thandle_t
+TIFFSetClientdata(TIFF* tif, thandle_t newvalue)
+{
+ thandle_t m = tif->tif_clientdata;
+ tif->tif_clientdata = newvalue;
+ return m;
+}
+
+/*
+ * Return read/write mode.
+ */
+int
+TIFFGetMode(TIFF* tif)
+{
+ return (tif->tif_mode);
+}
+
+/*
+ * Return read/write mode.
+ */
+int
+TIFFSetMode(TIFF* tif, int mode)
+{
+ int old_mode = tif->tif_mode;
+ tif->tif_mode = mode;
+ return (old_mode);
+}
+
+/*
+ * Return nonzero if file is organized in
+ * tiles; zero if organized as strips.
+ */
+int
+TIFFIsTiled(TIFF* tif)
+{
+ return (isTiled(tif));
+}
+
+/*
+ * Return current row being read/written.
+ */
+uint32
+TIFFCurrentRow(TIFF* tif)
+{
+ return (tif->tif_row);
+}
+
+/*
+ * Return index of the current directory.
+ */
+uint16
+TIFFCurrentDirectory(TIFF* tif)
+{
+ return (tif->tif_curdir);
+}
+
+/*
+ * Return current strip.
+ */
+uint32
+TIFFCurrentStrip(TIFF* tif)
+{
+ return (tif->tif_curstrip);
+}
+
+/*
+ * Return current tile.
+ */
+uint32
+TIFFCurrentTile(TIFF* tif)
+{
+ return (tif->tif_curtile);
+}
+
+/*
+ * Return nonzero if the file has byte-swapped data.
+ */
+int
+TIFFIsByteSwapped(TIFF* tif)
+{
+ return ((tif->tif_flags & TIFF_SWAB) != 0);
+}
+
+/*
+ * Return nonzero if the data is returned up-sampled.
+ */
+int
+TIFFIsUpSampled(TIFF* tif)
+{
+ return (isUpSampled(tif));
+}
+
+/*
+ * Return nonzero if the data is returned in MSB-to-LSB bit order.
+ */
+int
+TIFFIsMSB2LSB(TIFF* tif)
+{
+ return (isFillOrder(tif, FILLORDER_MSB2LSB));
+}
+
+/*
+ * Return nonzero if given file was written in big-endian order.
+ */
+int
+TIFFIsBigEndian(TIFF* tif)
+{
+ return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
+}
+
+/*
+ * Return pointer to file read method.
+ */
+TIFFReadWriteProc
+TIFFGetReadProc(TIFF* tif)
+{
+ return (tif->tif_readproc);
+}
+
+/*
+ * Return pointer to file write method.
+ */
+TIFFReadWriteProc
+TIFFGetWriteProc(TIFF* tif)
+{
+ return (tif->tif_writeproc);
+}
+
+/*
+ * Return pointer to file seek method.
+ */
+TIFFSeekProc
+TIFFGetSeekProc(TIFF* tif)
+{
+ return (tif->tif_seekproc);
+}
+
+/*
+ * Return pointer to file close method.
+ */
+TIFFCloseProc
+TIFFGetCloseProc(TIFF* tif)
+{
+ return (tif->tif_closeproc);
+}
+
+/*
+ * Return pointer to file size requesting method.
+ */
+TIFFSizeProc
+TIFFGetSizeProc(TIFF* tif)
+{
+ return (tif->tif_sizeproc);
+}
+
+/*
+ * Return pointer to memory mapping method.
+ */
+TIFFMapFileProc
+TIFFGetMapFileProc(TIFF* tif)
+{
+ return (tif->tif_mapproc);
+}
+
+/*
+ * Return pointer to memory unmapping method.
+ */
+TIFFUnmapFileProc
+TIFFGetUnmapFileProc(TIFF* tif)
+{
+ return (tif->tif_unmapproc);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_packbits.c b/test/monniaux/tiff-4.0.10/tif_packbits.c
new file mode 100644
index 00000000..a8f29e87
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_packbits.c
@@ -0,0 +1,309 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef PACKBITS_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * PackBits Compression Algorithm Support
+ */
+#include <stdio.h>
+
+static int
+PackBitsPreEncode(TIFF* tif, uint16 s)
+{
+ (void) s;
+
+ tif->tif_data = (uint8*)_TIFFmalloc(sizeof(tmsize_t));
+ if (tif->tif_data == NULL)
+ return (0);
+ /*
+ * Calculate the scanline/tile-width size in bytes.
+ */
+ if (isTiled(tif))
+ *(tmsize_t*)tif->tif_data = TIFFTileRowSize(tif);
+ else
+ *(tmsize_t*)tif->tif_data = TIFFScanlineSize(tif);
+ return (1);
+}
+
+static int
+PackBitsPostEncode(TIFF* tif)
+{
+ if (tif->tif_data)
+ _TIFFfree(tif->tif_data);
+ return (1);
+}
+
+/*
+ * Encode a run of pixels.
+ */
+static int
+PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
+{
+ unsigned char* bp = (unsigned char*) buf;
+ uint8* op;
+ uint8* ep;
+ uint8* lastliteral;
+ long n, slop;
+ int b;
+ enum { BASE, LITERAL, RUN, LITERAL_RUN } state;
+
+ (void) s;
+ op = tif->tif_rawcp;
+ ep = tif->tif_rawdata + tif->tif_rawdatasize;
+ state = BASE;
+ lastliteral = 0;
+ while (cc > 0) {
+ /*
+ * Find the longest string of identical bytes.
+ */
+ b = *bp++;
+ cc--;
+ n = 1;
+ for (; cc > 0 && b == *bp; cc--, bp++)
+ n++;
+ again:
+ if (op + 2 >= ep) { /* insure space for new data */
+ /*
+ * Be careful about writing the last
+ * literal. Must write up to that point
+ * and then copy the remainder to the
+ * front of the buffer.
+ */
+ if (state == LITERAL || state == LITERAL_RUN) {
+ slop = (long)(op - lastliteral);
+ tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ while (slop-- > 0)
+ *op++ = *lastliteral++;
+ lastliteral = tif->tif_rawcp;
+ } else {
+ tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
+ if (!TIFFFlushData1(tif))
+ return (0);
+ op = tif->tif_rawcp;
+ }
+ }
+ switch (state) {
+ case BASE: /* initial state, set run/literal */
+ if (n > 1) {
+ state = RUN;
+ if (n > 128) {
+ *op++ = (uint8) -127;
+ *op++ = (uint8) b;
+ n -= 128;
+ goto again;
+ }
+ *op++ = (uint8)(-(n-1));
+ *op++ = (uint8) b;
+ } else {
+ lastliteral = op;
+ *op++ = 0;
+ *op++ = (uint8) b;
+ state = LITERAL;
+ }
+ break;
+ case LITERAL: /* last object was literal string */
+ if (n > 1) {
+ state = LITERAL_RUN;
+ if (n > 128) {
+ *op++ = (uint8) -127;
+ *op++ = (uint8) b;
+ n -= 128;
+ goto again;
+ }
+ *op++ = (uint8)(-(n-1)); /* encode run */
+ *op++ = (uint8) b;
+ } else { /* extend literal */
+ if (++(*lastliteral) == 127)
+ state = BASE;
+ *op++ = (uint8) b;
+ }
+ break;
+ case RUN: /* last object was run */
+ if (n > 1) {
+ if (n > 128) {
+ *op++ = (uint8) -127;
+ *op++ = (uint8) b;
+ n -= 128;
+ goto again;
+ }
+ *op++ = (uint8)(-(n-1));
+ *op++ = (uint8) b;
+ } else {
+ lastliteral = op;
+ *op++ = 0;
+ *op++ = (uint8) b;
+ state = LITERAL;
+ }
+ break;
+ case LITERAL_RUN: /* literal followed by a run */
+ /*
+ * Check to see if previous run should
+ * be converted to a literal, in which
+ * case we convert literal-run-literal
+ * to a single literal.
+ */
+ if (n == 1 && op[-2] == (uint8) -1 &&
+ *lastliteral < 126) {
+ state = (((*lastliteral) += 2) == 127 ?
+ BASE : LITERAL);
+ op[-2] = op[-1]; /* replicate */
+ } else
+ state = RUN;
+ goto again;
+ }
+ }
+ tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
+ tif->tif_rawcp = op;
+ return (1);
+}
+
+/*
+ * Encode a rectangular chunk of pixels. We break it up
+ * into row-sized pieces to insure that encoded runs do
+ * not span rows. Otherwise, there can be problems with
+ * the decoder if data is read, for example, by scanlines
+ * when it was encoded by strips.
+ */
+static int
+PackBitsEncodeChunk(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ tmsize_t rowsize = *(tmsize_t*)tif->tif_data;
+
+ while (cc > 0) {
+ tmsize_t chunk = rowsize;
+
+ if( cc < chunk )
+ chunk = cc;
+
+ if (PackBitsEncode(tif, bp, chunk, s) < 0)
+ return (-1);
+ bp += chunk;
+ cc -= chunk;
+ }
+ return (1);
+}
+
+static int
+PackBitsDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "PackBitsDecode";
+ char *bp;
+ tmsize_t cc;
+ long n;
+ int b;
+
+ (void) s;
+ bp = (char*) tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ while (cc > 0 && occ > 0) {
+ n = (long) *bp++;
+ cc--;
+ /*
+ * Watch out for compilers that
+ * don't sign extend chars...
+ */
+ if (n >= 128)
+ n -= 256;
+ if (n < 0) { /* replicate next byte -n+1 times */
+ if (n == -128) /* nop */
+ continue;
+ n = -n + 1;
+ if( occ < (tmsize_t)n )
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Discarding %lu bytes to avoid buffer overrun",
+ (unsigned long) ((tmsize_t)n - occ));
+ n = (long)occ;
+ }
+ if( cc == 0 )
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Terminating PackBitsDecode due to lack of data.");
+ break;
+ }
+ occ -= n;
+ b = *bp++;
+ cc--;
+ while (n-- > 0)
+ *op++ = (uint8) b;
+ } else { /* copy next n+1 bytes literally */
+ if (occ < (tmsize_t)(n + 1))
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Discarding %lu bytes to avoid buffer overrun",
+ (unsigned long) ((tmsize_t)n - occ + 1));
+ n = (long)occ - 1;
+ }
+ if (cc < (tmsize_t) (n+1))
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Terminating PackBitsDecode due to lack of data.");
+ break;
+ }
+ _TIFFmemcpy(op, bp, ++n);
+ op += n; occ -= n;
+ bp += n; cc -= n;
+ }
+ }
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ if (occ > 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data for scanline %lu",
+ (unsigned long) tif->tif_row);
+ return (0);
+ }
+ return (1);
+}
+
+int
+TIFFInitPackBits(TIFF* tif, int scheme)
+{
+ (void) scheme;
+ tif->tif_decoderow = PackBitsDecode;
+ tif->tif_decodestrip = PackBitsDecode;
+ tif->tif_decodetile = PackBitsDecode;
+ tif->tif_preencode = PackBitsPreEncode;
+ tif->tif_postencode = PackBitsPostEncode;
+ tif->tif_encoderow = PackBitsEncode;
+ tif->tif_encodestrip = PackBitsEncodeChunk;
+ tif->tif_encodetile = PackBitsEncodeChunk;
+ return (1);
+}
+#endif /* PACKBITS_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_pixarlog.c b/test/monniaux/tiff-4.0.10/tif_pixarlog.c
new file mode 100644
index 00000000..7438d692
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_pixarlog.c
@@ -0,0 +1,1483 @@
+/*
+ * Copyright (c) 1996-1997 Sam Leffler
+ * Copyright (c) 1996 Pixar
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Pixar, Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef PIXARLOG_SUPPORT
+
+/*
+ * TIFF Library.
+ * PixarLog Compression Support
+ *
+ * Contributed by Dan McCoy.
+ *
+ * PixarLog film support uses the TIFF library to store companded
+ * 11 bit values into a tiff file, which are compressed using the
+ * zip compressor.
+ *
+ * The codec can take as input and produce as output 32-bit IEEE float values
+ * as well as 16-bit or 8-bit unsigned integer values.
+ *
+ * On writing any of the above are converted into the internal
+ * 11-bit log format. In the case of 8 and 16 bit values, the
+ * input is assumed to be unsigned linear color values that represent
+ * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
+ * be the normal linear color range, in addition over 1 values are
+ * accepted up to a value of about 25.0 to encode "hot" highlights and such.
+ * The encoding is lossless for 8-bit values, slightly lossy for the
+ * other bit depths. The actual color precision should be better
+ * than the human eye can perceive with extra room to allow for
+ * error introduced by further image computation. As with any quantized
+ * color format, it is possible to perform image calculations which
+ * expose the quantization error. This format should certainly be less
+ * susceptible to such errors than standard 8-bit encodings, but more
+ * susceptible than straight 16-bit or 32-bit encodings.
+ *
+ * On reading the internal format is converted to the desired output format.
+ * The program can request which format it desires by setting the internal
+ * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
+ * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
+ * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
+ * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
+ *
+ * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
+ * values with the difference that if there are exactly three or four channels
+ * (rgb or rgba) it swaps the channel order (bgr or abgr).
+ *
+ * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
+ * packed in 16-bit values. However no tools are supplied for interpreting
+ * these values.
+ *
+ * "hot" (over 1.0) areas written in floating point get clamped to
+ * 1.0 in the integer data types.
+ *
+ * When the file is closed after writing, the bit depth and sample format
+ * are set always to appear as if 8-bit data has been written into it.
+ * That way a naive program unaware of the particulars of the encoding
+ * gets the format it is most likely able to handle.
+ *
+ * The codec does it's own horizontal differencing step on the coded
+ * values so the libraries predictor stuff should be turned off.
+ * The codec also handle byte swapping the encoded values as necessary
+ * since the library does not have the information necessary
+ * to know the bit depth of the raw unencoded buffer.
+ *
+ * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
+ * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
+ * as noted in http://trac.osgeo.org/gdal/ticket/3894. FrankW - Jan'11
+ */
+
+#include "tif_predict.h"
+#include "zlib.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+/* Tables for converting to/from 11 bit coded values */
+
+#define TSIZE 2048 /* decode table size (11-bit tokens) */
+#define TSIZEP1 2049 /* Plus one for slop */
+#define ONE 1250 /* token value of 1.0 exactly */
+#define RATIO 1.004 /* nominal ratio for log part */
+
+#define CODE_MASK 0x7ff /* 11 bits. */
+
+static float Fltsize;
+static float LogK1, LogK2;
+
+#define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
+
+static void
+horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
+ float *ToLinearF)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+ register float t0, t1, t2, t3;
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ t0 = ToLinearF[cr = (wp[0] & mask)];
+ t1 = ToLinearF[cg = (wp[1] & mask)];
+ t2 = ToLinearF[cb = (wp[2] & mask)];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ n -= 3;
+ while (n > 0) {
+ wp += 3;
+ op += 3;
+ n -= 3;
+ t0 = ToLinearF[(cr += wp[0]) & mask];
+ t1 = ToLinearF[(cg += wp[1]) & mask];
+ t2 = ToLinearF[(cb += wp[2]) & mask];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ }
+ } else if (stride == 4) {
+ t0 = ToLinearF[cr = (wp[0] & mask)];
+ t1 = ToLinearF[cg = (wp[1] & mask)];
+ t2 = ToLinearF[cb = (wp[2] & mask)];
+ t3 = ToLinearF[ca = (wp[3] & mask)];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ n -= 4;
+ while (n > 0) {
+ wp += 4;
+ op += 4;
+ n -= 4;
+ t0 = ToLinearF[(cr += wp[0]) & mask];
+ t1 = ToLinearF[(cg += wp[1]) & mask];
+ t2 = ToLinearF[(cb += wp[2]) & mask];
+ t3 = ToLinearF[(ca += wp[3]) & mask];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ }
+ } else {
+ REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+static void
+horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
+ float *ToLinearF)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+ register float t0, t1, t2, t3;
+
+#define SCALE12 2048.0F
+#define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
+ t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
+ t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
+ op[0] = CLAMP12(t0);
+ op[1] = CLAMP12(t1);
+ op[2] = CLAMP12(t2);
+ n -= 3;
+ while (n > 0) {
+ wp += 3;
+ op += 3;
+ n -= 3;
+ t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
+ t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
+ t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
+ op[0] = CLAMP12(t0);
+ op[1] = CLAMP12(t1);
+ op[2] = CLAMP12(t2);
+ }
+ } else if (stride == 4) {
+ t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
+ t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
+ t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
+ t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
+ op[0] = CLAMP12(t0);
+ op[1] = CLAMP12(t1);
+ op[2] = CLAMP12(t2);
+ op[3] = CLAMP12(t3);
+ n -= 4;
+ while (n > 0) {
+ wp += 4;
+ op += 4;
+ n -= 4;
+ t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
+ t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
+ t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
+ t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
+ op[0] = CLAMP12(t0);
+ op[1] = CLAMP12(t1);
+ op[2] = CLAMP12(t2);
+ op[3] = CLAMP12(t3);
+ }
+ } else {
+ REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
+ *op = CLAMP12(t0); wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
+ *op = CLAMP12(t0); wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+static void
+horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
+ uint16 *ToLinear16)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ op[0] = ToLinear16[cr = (wp[0] & mask)];
+ op[1] = ToLinear16[cg = (wp[1] & mask)];
+ op[2] = ToLinear16[cb = (wp[2] & mask)];
+ n -= 3;
+ while (n > 0) {
+ wp += 3;
+ op += 3;
+ n -= 3;
+ op[0] = ToLinear16[(cr += wp[0]) & mask];
+ op[1] = ToLinear16[(cg += wp[1]) & mask];
+ op[2] = ToLinear16[(cb += wp[2]) & mask];
+ }
+ } else if (stride == 4) {
+ op[0] = ToLinear16[cr = (wp[0] & mask)];
+ op[1] = ToLinear16[cg = (wp[1] & mask)];
+ op[2] = ToLinear16[cb = (wp[2] & mask)];
+ op[3] = ToLinear16[ca = (wp[3] & mask)];
+ n -= 4;
+ while (n > 0) {
+ wp += 4;
+ op += 4;
+ n -= 4;
+ op[0] = ToLinear16[(cr += wp[0]) & mask];
+ op[1] = ToLinear16[(cg += wp[1]) & mask];
+ op[2] = ToLinear16[(cb += wp[2]) & mask];
+ op[3] = ToLinear16[(ca += wp[3]) & mask];
+ }
+ } else {
+ REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+/*
+ * Returns the log encoded 11-bit values with the horizontal
+ * differencing undone.
+ */
+static void
+horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ op[0] = wp[0]; op[1] = wp[1]; op[2] = wp[2];
+ cr = wp[0]; cg = wp[1]; cb = wp[2];
+ n -= 3;
+ while (n > 0) {
+ wp += 3;
+ op += 3;
+ n -= 3;
+ op[0] = (uint16)((cr += wp[0]) & mask);
+ op[1] = (uint16)((cg += wp[1]) & mask);
+ op[2] = (uint16)((cb += wp[2]) & mask);
+ }
+ } else if (stride == 4) {
+ op[0] = wp[0]; op[1] = wp[1];
+ op[2] = wp[2]; op[3] = wp[3];
+ cr = wp[0]; cg = wp[1]; cb = wp[2]; ca = wp[3];
+ n -= 4;
+ while (n > 0) {
+ wp += 4;
+ op += 4;
+ n -= 4;
+ op[0] = (uint16)((cr += wp[0]) & mask);
+ op[1] = (uint16)((cg += wp[1]) & mask);
+ op[2] = (uint16)((cb += wp[2]) & mask);
+ op[3] = (uint16)((ca += wp[3]) & mask);
+ }
+ } else {
+ REPEAT(stride, *op = *wp&mask; wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; *op = *wp&mask; wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+static void
+horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
+ unsigned char *ToLinear8)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ op[0] = ToLinear8[cr = (wp[0] & mask)];
+ op[1] = ToLinear8[cg = (wp[1] & mask)];
+ op[2] = ToLinear8[cb = (wp[2] & mask)];
+ n -= 3;
+ while (n > 0) {
+ n -= 3;
+ wp += 3;
+ op += 3;
+ op[0] = ToLinear8[(cr += wp[0]) & mask];
+ op[1] = ToLinear8[(cg += wp[1]) & mask];
+ op[2] = ToLinear8[(cb += wp[2]) & mask];
+ }
+ } else if (stride == 4) {
+ op[0] = ToLinear8[cr = (wp[0] & mask)];
+ op[1] = ToLinear8[cg = (wp[1] & mask)];
+ op[2] = ToLinear8[cb = (wp[2] & mask)];
+ op[3] = ToLinear8[ca = (wp[3] & mask)];
+ n -= 4;
+ while (n > 0) {
+ n -= 4;
+ wp += 4;
+ op += 4;
+ op[0] = ToLinear8[(cr += wp[0]) & mask];
+ op[1] = ToLinear8[(cg += wp[1]) & mask];
+ op[2] = ToLinear8[(cb += wp[2]) & mask];
+ op[3] = ToLinear8[(ca += wp[3]) & mask];
+ }
+ } else {
+ REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+
+static void
+horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
+ unsigned char *ToLinear8)
+{
+ register unsigned int cr, cg, cb, ca, mask;
+ register unsigned char t0, t1, t2, t3;
+
+ if (n >= stride) {
+ mask = CODE_MASK;
+ if (stride == 3) {
+ op[0] = 0;
+ t1 = ToLinear8[cb = (wp[2] & mask)];
+ t2 = ToLinear8[cg = (wp[1] & mask)];
+ t3 = ToLinear8[cr = (wp[0] & mask)];
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ n -= 3;
+ while (n > 0) {
+ n -= 3;
+ wp += 3;
+ op += 4;
+ op[0] = 0;
+ t1 = ToLinear8[(cb += wp[2]) & mask];
+ t2 = ToLinear8[(cg += wp[1]) & mask];
+ t3 = ToLinear8[(cr += wp[0]) & mask];
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ }
+ } else if (stride == 4) {
+ t0 = ToLinear8[ca = (wp[3] & mask)];
+ t1 = ToLinear8[cb = (wp[2] & mask)];
+ t2 = ToLinear8[cg = (wp[1] & mask)];
+ t3 = ToLinear8[cr = (wp[0] & mask)];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ n -= 4;
+ while (n > 0) {
+ n -= 4;
+ wp += 4;
+ op += 4;
+ t0 = ToLinear8[(ca += wp[3]) & mask];
+ t1 = ToLinear8[(cb += wp[2]) & mask];
+ t2 = ToLinear8[(cg += wp[1]) & mask];
+ t3 = ToLinear8[(cr += wp[0]) & mask];
+ op[0] = t0;
+ op[1] = t1;
+ op[2] = t2;
+ op[3] = t3;
+ }
+ } else {
+ REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+/*
+ * State block for each open TIFF
+ * file using PixarLog compression/decompression.
+ */
+typedef struct {
+ TIFFPredictorState predict;
+ z_stream stream;
+ tmsize_t tbuf_size; /* only set/used on reading for now */
+ uint16 *tbuf;
+ uint16 stride;
+ int state;
+ int user_datafmt;
+ int quality;
+#define PLSTATE_INIT 1
+
+ TIFFVSetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+
+ float *ToLinearF;
+ uint16 *ToLinear16;
+ unsigned char *ToLinear8;
+ uint16 *FromLT2;
+ uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
+ uint16 *From8;
+
+} PixarLogState;
+
+static int
+PixarLogMakeTables(PixarLogState *sp)
+{
+
+/*
+ * We make several tables here to convert between various external
+ * representations (float, 16-bit, and 8-bit) and the internal
+ * 11-bit companded representation. The 11-bit representation has two
+ * distinct regions. A linear bottom end up through .018316 in steps
+ * of about .000073, and a region of constant ratio up to about 25.
+ * These floating point numbers are stored in the main table ToLinearF.
+ * All other tables are derived from this one. The tables (and the
+ * ratios) are continuous at the internal seam.
+ */
+
+ int nlin, lt2size;
+ int i, j;
+ double b, c, linstep, v;
+ float *ToLinearF;
+ uint16 *ToLinear16;
+ unsigned char *ToLinear8;
+ uint16 *FromLT2;
+ uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
+ uint16 *From8;
+
+ c = log(RATIO);
+ nlin = (int)(1./c); /* nlin must be an integer */
+ c = 1./nlin;
+ b = exp(-c*ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
+ linstep = b*c*exp(1.);
+
+ LogK1 = (float)(1./c); /* if (v >= 2) token = k1*log(v*k2) */
+ LogK2 = (float)(1./b);
+ lt2size = (int)(2./linstep) + 1;
+ FromLT2 = (uint16 *)_TIFFmalloc(lt2size*sizeof(uint16));
+ From14 = (uint16 *)_TIFFmalloc(16384*sizeof(uint16));
+ From8 = (uint16 *)_TIFFmalloc(256*sizeof(uint16));
+ ToLinearF = (float *)_TIFFmalloc(TSIZEP1 * sizeof(float));
+ ToLinear16 = (uint16 *)_TIFFmalloc(TSIZEP1 * sizeof(uint16));
+ ToLinear8 = (unsigned char *)_TIFFmalloc(TSIZEP1 * sizeof(unsigned char));
+ if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
+ ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
+ if (FromLT2) _TIFFfree(FromLT2);
+ if (From14) _TIFFfree(From14);
+ if (From8) _TIFFfree(From8);
+ if (ToLinearF) _TIFFfree(ToLinearF);
+ if (ToLinear16) _TIFFfree(ToLinear16);
+ if (ToLinear8) _TIFFfree(ToLinear8);
+ sp->FromLT2 = NULL;
+ sp->From14 = NULL;
+ sp->From8 = NULL;
+ sp->ToLinearF = NULL;
+ sp->ToLinear16 = NULL;
+ sp->ToLinear8 = NULL;
+ return 0;
+ }
+
+ j = 0;
+
+ for (i = 0; i < nlin; i++) {
+ v = i * linstep;
+ ToLinearF[j++] = (float)v;
+ }
+
+ for (i = nlin; i < TSIZE; i++)
+ ToLinearF[j++] = (float)(b*exp(c*i));
+
+ ToLinearF[2048] = ToLinearF[2047];
+
+ for (i = 0; i < TSIZEP1; i++) {
+ v = ToLinearF[i]*65535.0 + 0.5;
+ ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16)v;
+ v = ToLinearF[i]*255.0 + 0.5;
+ ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
+ }
+
+ j = 0;
+ for (i = 0; i < lt2size; i++) {
+ if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
+ j++;
+ FromLT2[i] = (uint16)j;
+ }
+
+ /*
+ * Since we lose info anyway on 16-bit data, we set up a 14-bit
+ * table and shift 16-bit values down two bits on input.
+ * saves a little table space.
+ */
+ j = 0;
+ for (i = 0; i < 16384; i++) {
+ while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
+ j++;
+ From14[i] = (uint16)j;
+ }
+
+ j = 0;
+ for (i = 0; i < 256; i++) {
+ while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
+ j++;
+ From8[i] = (uint16)j;
+ }
+
+ Fltsize = (float)(lt2size/2);
+
+ sp->ToLinearF = ToLinearF;
+ sp->ToLinear16 = ToLinear16;
+ sp->ToLinear8 = ToLinear8;
+ sp->FromLT2 = FromLT2;
+ sp->From14 = From14;
+ sp->From8 = From8;
+
+ return 1;
+}
+
+#define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
+#define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
+
+static int PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+#define PIXARLOGDATAFMT_UNKNOWN -1
+
+static int
+PixarLogGuessDataFmt(TIFFDirectory *td)
+{
+ int guess = PIXARLOGDATAFMT_UNKNOWN;
+ int format = td->td_sampleformat;
+
+ /* If the user didn't tell us his datafmt,
+ * take our best guess from the bitspersample.
+ */
+ switch (td->td_bitspersample) {
+ case 32:
+ if (format == SAMPLEFORMAT_IEEEFP)
+ guess = PIXARLOGDATAFMT_FLOAT;
+ break;
+ case 16:
+ if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
+ guess = PIXARLOGDATAFMT_16BIT;
+ break;
+ case 12:
+ if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
+ guess = PIXARLOGDATAFMT_12BITPICIO;
+ break;
+ case 11:
+ if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
+ guess = PIXARLOGDATAFMT_11BITLOG;
+ break;
+ case 8:
+ if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
+ guess = PIXARLOGDATAFMT_8BIT;
+ break;
+ }
+
+ return guess;
+}
+
+#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
+#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
+
+static tmsize_t
+multiply_ms(tmsize_t m1, tmsize_t m2)
+{
+ if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
+ return 0;
+ return m1 * m2;
+}
+
+static tmsize_t
+add_ms(tmsize_t m1, tmsize_t m2)
+{
+ /* if either input is zero, assume overflow already occurred */
+ if (m1 == 0 || m2 == 0)
+ return 0;
+ else if (m1 > TIFF_TMSIZE_T_MAX - m2)
+ return 0;
+
+ return m1 + m2;
+}
+
+static int
+PixarLogFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+static int
+PixarLogSetupDecode(TIFF* tif)
+{
+ static const char module[] = "PixarLogSetupDecode";
+ TIFFDirectory *td = &tif->tif_dir;
+ PixarLogState* sp = DecoderState(tif);
+ tmsize_t tbuf_size;
+ uint32 strip_height;
+
+ assert(sp != NULL);
+
+ /* This function can possibly be called several times by */
+ /* PredictorSetupDecode() if this function succeeds but */
+ /* PredictorSetup() fails */
+ if( (sp->state & PLSTATE_INIT) != 0 )
+ return 1;
+
+ strip_height = td->td_rowsperstrip;
+ if( strip_height > td->td_imagelength )
+ strip_height = td->td_imagelength;
+
+ /* Make sure no byte swapping happens on the data
+ * after decompression. */
+ tif->tif_postdecode = _TIFFNoPostDecode;
+
+ /* for some reason, we can't do this in TIFFInitPixarLog */
+
+ sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
+ td->td_samplesperpixel : 1);
+ tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
+ strip_height), sizeof(uint16));
+ /* add one more stride in case input ends mid-stride */
+ tbuf_size = add_ms(tbuf_size, sizeof(uint16) * sp->stride);
+ if (tbuf_size == 0)
+ return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
+ sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
+ if (sp->tbuf == NULL)
+ return (0);
+ sp->tbuf_size = tbuf_size;
+ if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
+ sp->user_datafmt = PixarLogGuessDataFmt(td);
+ if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
+ _TIFFfree(sp->tbuf);
+ sp->tbuf = NULL;
+ sp->tbuf_size = 0;
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
+ td->td_bitspersample);
+ return (0);
+ }
+
+ if (inflateInit(&sp->stream) != Z_OK) {
+ _TIFFfree(sp->tbuf);
+ sp->tbuf = NULL;
+ sp->tbuf_size = 0;
+ TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ } else {
+ sp->state |= PLSTATE_INIT;
+ return (1);
+ }
+}
+
+/*
+ * Setup state for decoding a strip.
+ */
+static int
+PixarLogPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "PixarLogPreDecode";
+ PixarLogState* sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ sp->stream.next_in = tif->tif_rawdata;
+ assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_in = (uInt) tif->tif_rawcc;
+ if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ return (inflateReset(&sp->stream) == Z_OK);
+}
+
+static int
+PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "PixarLogDecode";
+ TIFFDirectory *td = &tif->tif_dir;
+ PixarLogState* sp = DecoderState(tif);
+ tmsize_t i;
+ tmsize_t nsamples;
+ int llen;
+ uint16 *up;
+
+ switch (sp->user_datafmt) {
+ case PIXARLOGDATAFMT_FLOAT:
+ nsamples = occ / sizeof(float); /* XXX float == 32 bits */
+ break;
+ case PIXARLOGDATAFMT_16BIT:
+ case PIXARLOGDATAFMT_12BITPICIO:
+ case PIXARLOGDATAFMT_11BITLOG:
+ nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
+ break;
+ case PIXARLOGDATAFMT_8BIT:
+ case PIXARLOGDATAFMT_8BITABGR:
+ nsamples = occ;
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%d bit input not supported in PixarLog",
+ td->td_bitspersample);
+ return 0;
+ }
+
+ llen = sp->stride * td->td_imagewidth;
+
+ (void) s;
+ assert(sp != NULL);
+
+ sp->stream.next_in = tif->tif_rawcp;
+ sp->stream.avail_in = (uInt) tif->tif_rawcc;
+
+ sp->stream.next_out = (unsigned char *) sp->tbuf;
+ assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
+ if (sp->stream.avail_out != nsamples * sizeof(uint16))
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ /* Check that we will not fill more than what was allocated */
+ if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "sp->stream.avail_out > sp->tbuf_size");
+ return (0);
+ }
+ do {
+ int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
+ if (state == Z_STREAM_END) {
+ break; /* XXX */
+ }
+ if (state == Z_DATA_ERROR) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Decoding error at scanline %lu, %s",
+ (unsigned long) tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)");
+ if (inflateSync(&sp->stream) != Z_OK)
+ return (0);
+ continue;
+ }
+ if (state != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+ sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ }
+ } while (sp->stream.avail_out > 0);
+
+ /* hopefully, we got all the bytes we needed */
+ if (sp->stream.avail_out != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
+ (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
+ return (0);
+ }
+
+ tif->tif_rawcp = sp->stream.next_in;
+ tif->tif_rawcc = sp->stream.avail_in;
+
+ up = sp->tbuf;
+ /* Swap bytes in the data if from a different endian machine. */
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabArrayOfShort(up, nsamples);
+
+ /*
+ * if llen is not an exact multiple of nsamples, the decode operation
+ * may overflow the output buffer, so truncate it enough to prevent
+ * that but still salvage as much data as possible.
+ */
+ if (nsamples % llen) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "stride %lu is not a multiple of sample count, "
+ "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
+ nsamples -= nsamples % llen;
+ }
+
+ for (i = 0; i < nsamples; i += llen, up += llen) {
+ switch (sp->user_datafmt) {
+ case PIXARLOGDATAFMT_FLOAT:
+ horizontalAccumulateF(up, llen, sp->stride,
+ (float *)op, sp->ToLinearF);
+ op += llen * sizeof(float);
+ break;
+ case PIXARLOGDATAFMT_16BIT:
+ horizontalAccumulate16(up, llen, sp->stride,
+ (uint16 *)op, sp->ToLinear16);
+ op += llen * sizeof(uint16);
+ break;
+ case PIXARLOGDATAFMT_12BITPICIO:
+ horizontalAccumulate12(up, llen, sp->stride,
+ (int16 *)op, sp->ToLinearF);
+ op += llen * sizeof(int16);
+ break;
+ case PIXARLOGDATAFMT_11BITLOG:
+ horizontalAccumulate11(up, llen, sp->stride,
+ (uint16 *)op);
+ op += llen * sizeof(uint16);
+ break;
+ case PIXARLOGDATAFMT_8BIT:
+ horizontalAccumulate8(up, llen, sp->stride,
+ (unsigned char *)op, sp->ToLinear8);
+ op += llen * sizeof(unsigned char);
+ break;
+ case PIXARLOGDATAFMT_8BITABGR:
+ horizontalAccumulate8abgr(up, llen, sp->stride,
+ (unsigned char *)op, sp->ToLinear8);
+ op += llen * sizeof(unsigned char);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Unsupported bits/sample: %d",
+ td->td_bitspersample);
+ return (0);
+ }
+ }
+
+ return (1);
+}
+
+static int
+PixarLogSetupEncode(TIFF* tif)
+{
+ static const char module[] = "PixarLogSetupEncode";
+ TIFFDirectory *td = &tif->tif_dir;
+ PixarLogState* sp = EncoderState(tif);
+ tmsize_t tbuf_size;
+
+ assert(sp != NULL);
+
+ /* for some reason, we can't do this in TIFFInitPixarLog */
+
+ sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
+ td->td_samplesperpixel : 1);
+ tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
+ td->td_rowsperstrip), sizeof(uint16));
+ if (tbuf_size == 0)
+ return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
+ sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
+ if (sp->tbuf == NULL)
+ return (0);
+ if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
+ sp->user_datafmt = PixarLogGuessDataFmt(td);
+ if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
+ TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
+ return (0);
+ }
+
+ if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ } else {
+ sp->state |= PLSTATE_INIT;
+ return (1);
+ }
+}
+
+/*
+ * Reset encoding state at the start of a strip.
+ */
+static int
+PixarLogPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "PixarLogPreEncode";
+ PixarLogState *sp = EncoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ sp->stream.next_out = tif->tif_rawdata;
+ assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ return (deflateReset(&sp->stream) == Z_OK);
+}
+
+static void
+horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
+{
+ int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
+ float fltsize = Fltsize;
+
+#define CLAMP(v) ( (v<(float)0.) ? 0 \
+ : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
+ : (v>(float)24.2) ? 2047 \
+ : LogK1*log(v*LogK2) + 0.5 )
+
+ mask = CODE_MASK;
+ if (n >= stride) {
+ if (stride == 3) {
+ r2 = wp[0] = (uint16) CLAMP(ip[0]);
+ g2 = wp[1] = (uint16) CLAMP(ip[1]);
+ b2 = wp[2] = (uint16) CLAMP(ip[2]);
+ n -= 3;
+ while (n > 0) {
+ n -= 3;
+ wp += 3;
+ ip += 3;
+ r1 = (int32) CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = (int32) CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = (int32) CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
+ }
+ } else if (stride == 4) {
+ r2 = wp[0] = (uint16) CLAMP(ip[0]);
+ g2 = wp[1] = (uint16) CLAMP(ip[1]);
+ b2 = wp[2] = (uint16) CLAMP(ip[2]);
+ a2 = wp[3] = (uint16) CLAMP(ip[3]);
+ n -= 4;
+ while (n > 0) {
+ n -= 4;
+ wp += 4;
+ ip += 4;
+ r1 = (int32) CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = (int32) CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = (int32) CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
+ a1 = (int32) CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
+ }
+ } else {
+ REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp++; ip++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[0] = (uint16)(((int32)CLAMP(ip[0])-(int32)CLAMP(ip[-stride])) & mask);
+ wp++; ip++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+static void
+horizontalDifference16(unsigned short *ip, int n, int stride,
+ unsigned short *wp, uint16 *From14)
+{
+ register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
+
+/* assumption is unsigned pixel values */
+#undef CLAMP
+#define CLAMP(v) From14[(v) >> 2]
+
+ mask = CODE_MASK;
+ if (n >= stride) {
+ if (stride == 3) {
+ r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
+ b2 = wp[2] = CLAMP(ip[2]);
+ n -= 3;
+ while (n > 0) {
+ n -= 3;
+ wp += 3;
+ ip += 3;
+ r1 = CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
+ }
+ } else if (stride == 4) {
+ r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
+ b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
+ n -= 4;
+ while (n > 0) {
+ n -= 4;
+ wp += 4;
+ ip += 4;
+ r1 = CLAMP(ip[0]); wp[0] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
+ a1 = CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
+ }
+ } else {
+ REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
+ wp++; ip++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+
+static void
+horizontalDifference8(unsigned char *ip, int n, int stride,
+ unsigned short *wp, uint16 *From8)
+{
+ register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
+
+#undef CLAMP
+#define CLAMP(v) (From8[(v)])
+
+ mask = CODE_MASK;
+ if (n >= stride) {
+ if (stride == 3) {
+ r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
+ b2 = wp[2] = CLAMP(ip[2]);
+ n -= 3;
+ while (n > 0) {
+ n -= 3;
+ r1 = CLAMP(ip[3]); wp[3] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = CLAMP(ip[4]); wp[4] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = CLAMP(ip[5]); wp[5] = (uint16)((b1-b2) & mask); b2 = b1;
+ wp += 3;
+ ip += 3;
+ }
+ } else if (stride == 4) {
+ r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
+ b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
+ n -= 4;
+ while (n > 0) {
+ n -= 4;
+ r1 = CLAMP(ip[4]); wp[4] = (uint16)((r1-r2) & mask); r2 = r1;
+ g1 = CLAMP(ip[5]); wp[5] = (uint16)((g1-g2) & mask); g2 = g1;
+ b1 = CLAMP(ip[6]); wp[6] = (uint16)((b1-b2) & mask); b2 = b1;
+ a1 = CLAMP(ip[7]); wp[7] = (uint16)((a1-a2) & mask); a2 = a1;
+ wp += 4;
+ ip += 4;
+ }
+ } else {
+ REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
+ n -= stride;
+ while (n > 0) {
+ REPEAT(stride,
+ wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
+ wp++; ip++)
+ n -= stride;
+ }
+ }
+ }
+}
+
+/*
+ * Encode a chunk of pixels.
+ */
+static int
+PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "PixarLogEncode";
+ TIFFDirectory *td = &tif->tif_dir;
+ PixarLogState *sp = EncoderState(tif);
+ tmsize_t i;
+ tmsize_t n;
+ int llen;
+ unsigned short * up;
+
+ (void) s;
+
+ switch (sp->user_datafmt) {
+ case PIXARLOGDATAFMT_FLOAT:
+ n = cc / sizeof(float); /* XXX float == 32 bits */
+ break;
+ case PIXARLOGDATAFMT_16BIT:
+ case PIXARLOGDATAFMT_12BITPICIO:
+ case PIXARLOGDATAFMT_11BITLOG:
+ n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
+ break;
+ case PIXARLOGDATAFMT_8BIT:
+ case PIXARLOGDATAFMT_8BITABGR:
+ n = cc;
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%d bit input not supported in PixarLog",
+ td->td_bitspersample);
+ return 0;
+ }
+
+ llen = sp->stride * td->td_imagewidth;
+ /* Check against the number of elements (of size uint16) of sp->tbuf */
+ if( n > (tmsize_t)(td->td_rowsperstrip * llen) )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Too many input bytes provided");
+ return 0;
+ }
+
+ for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
+ switch (sp->user_datafmt) {
+ case PIXARLOGDATAFMT_FLOAT:
+ horizontalDifferenceF((float *)bp, llen,
+ sp->stride, up, sp->FromLT2);
+ bp += llen * sizeof(float);
+ break;
+ case PIXARLOGDATAFMT_16BIT:
+ horizontalDifference16((uint16 *)bp, llen,
+ sp->stride, up, sp->From14);
+ bp += llen * sizeof(uint16);
+ break;
+ case PIXARLOGDATAFMT_8BIT:
+ horizontalDifference8((unsigned char *)bp, llen,
+ sp->stride, up, sp->From8);
+ bp += llen * sizeof(unsigned char);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%d bit input not supported in PixarLog",
+ td->td_bitspersample);
+ return 0;
+ }
+ }
+
+ sp->stream.next_in = (unsigned char *) sp->tbuf;
+ assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_in = (uInt) (n * sizeof(uint16));
+ if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+
+ do {
+ if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
+ sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ }
+ if (sp->stream.avail_out == 0) {
+ tif->tif_rawcc = tif->tif_rawdatasize;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
+ }
+ } while (sp->stream.avail_in > 0);
+ return (1);
+}
+
+/*
+ * Finish off an encoded strip by flushing the last
+ * string and tacking on an End Of Information code.
+ */
+
+static int
+PixarLogPostEncode(TIFF* tif)
+{
+ static const char module[] = "PixarLogPostEncode";
+ PixarLogState *sp = EncoderState(tif);
+ int state;
+
+ sp->stream.avail_in = 0;
+
+ do {
+ state = deflate(&sp->stream, Z_FINISH);
+ switch (state) {
+ case Z_STREAM_END:
+ case Z_OK:
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
+ tif->tif_rawcc =
+ tif->tif_rawdatasize - sp->stream.avail_out;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+ sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ }
+ } while (state != Z_STREAM_END);
+ return (1);
+}
+
+static void
+PixarLogClose(TIFF* tif)
+{
+ PixarLogState* sp = (PixarLogState*) tif->tif_data;
+ TIFFDirectory *td = &tif->tif_dir;
+
+ assert(sp != 0);
+ /* In a really sneaky (and really incorrect, and untruthful, and
+ * troublesome, and error-prone) maneuver that completely goes against
+ * the spirit of TIFF, and breaks TIFF, on close, we covertly
+ * modify both bitspersample and sampleformat in the directory to
+ * indicate 8-bit linear. This way, the decode "just works" even for
+ * readers that don't know about PixarLog, or how to set
+ * the PIXARLOGDATFMT pseudo-tag.
+ */
+
+ if (sp->state&PLSTATE_INIT) {
+ /* We test the state to avoid an issue such as in
+ * http://bugzilla.maptools.org/show_bug.cgi?id=2604
+ * What appends in that case is that the bitspersample is 1 and
+ * a TransferFunction is set. The size of the TransferFunction
+ * depends on 1<<bitspersample. So if we increase it, an access
+ * out of the buffer will happen at directory flushing.
+ * Another option would be to clear those targs.
+ */
+ td->td_bitspersample = 8;
+ td->td_sampleformat = SAMPLEFORMAT_UINT;
+ }
+}
+
+static void
+PixarLogCleanup(TIFF* tif)
+{
+ PixarLogState* sp = (PixarLogState*) tif->tif_data;
+
+ assert(sp != 0);
+
+ (void)TIFFPredictorCleanup(tif);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->FromLT2) _TIFFfree(sp->FromLT2);
+ if (sp->From14) _TIFFfree(sp->From14);
+ if (sp->From8) _TIFFfree(sp->From8);
+ if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
+ if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
+ if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
+ if (sp->state&PLSTATE_INIT) {
+ if (tif->tif_mode == O_RDONLY)
+ inflateEnd(&sp->stream);
+ else
+ deflateEnd(&sp->stream);
+ }
+ if (sp->tbuf)
+ _TIFFfree(sp->tbuf);
+ _TIFFfree(sp);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "PixarLogVSetField";
+ PixarLogState *sp = (PixarLogState *)tif->tif_data;
+ int result;
+
+ switch (tag) {
+ case TIFFTAG_PIXARLOGQUALITY:
+ sp->quality = (int) va_arg(ap, int);
+ if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
+ if (deflateParams(&sp->stream,
+ sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+ sp->stream.msg ? sp->stream.msg : "(null)");
+ return (0);
+ }
+ }
+ return (1);
+ case TIFFTAG_PIXARLOGDATAFMT:
+ sp->user_datafmt = (int) va_arg(ap, int);
+ /* Tweak the TIFF header so that the rest of libtiff knows what
+ * size of data will be passed between app and library, and
+ * assume that the app knows what it is doing and is not
+ * confused by these header manipulations...
+ */
+ switch (sp->user_datafmt) {
+ case PIXARLOGDATAFMT_8BIT:
+ case PIXARLOGDATAFMT_8BITABGR:
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
+ break;
+ case PIXARLOGDATAFMT_11BITLOG:
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
+ break;
+ case PIXARLOGDATAFMT_12BITPICIO:
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
+ break;
+ case PIXARLOGDATAFMT_16BIT:
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
+ break;
+ case PIXARLOGDATAFMT_FLOAT:
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
+ TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
+ break;
+ }
+ /*
+ * Must recalculate sizes should bits/sample change.
+ */
+ tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
+ tif->tif_scanlinesize = TIFFScanlineSize(tif);
+ result = 1; /* NB: pseudo tag */
+ break;
+ default:
+ result = (*sp->vsetparent)(tif, tag, ap);
+ }
+ return (result);
+}
+
+static int
+PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ PixarLogState *sp = (PixarLogState *)tif->tif_data;
+
+ switch (tag) {
+ case TIFFTAG_PIXARLOGQUALITY:
+ *va_arg(ap, int*) = sp->quality;
+ break;
+ case TIFFTAG_PIXARLOGDATAFMT:
+ *va_arg(ap, int*) = sp->user_datafmt;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return (1);
+}
+
+static const TIFFField pixarlogFields[] = {
+ {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
+ {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
+};
+
+int
+TIFFInitPixarLog(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitPixarLog";
+
+ PixarLogState* sp;
+
+ assert(scheme == COMPRESSION_PIXARLOG);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, pixarlogFields,
+ TIFFArrayCount(pixarlogFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging PixarLog codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = (PixarLogState*) tif->tif_data;
+ _TIFFmemset(sp, 0, sizeof (*sp));
+ sp->stream.data_type = Z_BINARY;
+ sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = PixarLogFixupTags;
+ tif->tif_setupdecode = PixarLogSetupDecode;
+ tif->tif_predecode = PixarLogPreDecode;
+ tif->tif_decoderow = PixarLogDecode;
+ tif->tif_decodestrip = PixarLogDecode;
+ tif->tif_decodetile = PixarLogDecode;
+ tif->tif_setupencode = PixarLogSetupEncode;
+ tif->tif_preencode = PixarLogPreEncode;
+ tif->tif_postencode = PixarLogPostEncode;
+ tif->tif_encoderow = PixarLogEncode;
+ tif->tif_encodestrip = PixarLogEncode;
+ tif->tif_encodetile = PixarLogEncode;
+ tif->tif_close = PixarLogClose;
+ tif->tif_cleanup = PixarLogCleanup;
+
+ /* Override SetField so we can handle our private pseudo-tag */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
+ sp->state = 0;
+
+ /* we don't wish to use the predictor,
+ * the default is none, which predictor value 1
+ */
+ (void) TIFFPredictorInit(tif);
+
+ /*
+ * build the companding tables
+ */
+ PixarLogMakeTables(sp);
+
+ return (1);
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for PixarLog state block");
+ return (0);
+}
+#endif /* PIXARLOG_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_predict.c b/test/monniaux/tiff-4.0.10/tif_predict.c
new file mode 100644
index 00000000..b775663a
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_predict.c
@@ -0,0 +1,879 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Predictor Tag Support (used by multiple codecs).
+ */
+#include "tiffiop.h"
+#include "tif_predict.h"
+
+#define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
+
+static int horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
+
+static int
+PredictorSetup(TIFF* tif)
+{
+ static const char module[] = "PredictorSetup";
+
+ TIFFPredictorState* sp = PredictorState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ switch (sp->predictor) /* no differencing */
+ {
+ case PREDICTOR_NONE:
+ return 1;
+ case PREDICTOR_HORIZONTAL:
+ if (td->td_bitspersample != 8
+ && td->td_bitspersample != 16
+ && td->td_bitspersample != 32) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
+ td->td_bitspersample);
+ return 0;
+ }
+ break;
+ case PREDICTOR_FLOATINGPOINT:
+ if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Floating point \"Predictor\" not supported with %d data format",
+ td->td_sampleformat);
+ return 0;
+ }
+ if (td->td_bitspersample != 16
+ && td->td_bitspersample != 24
+ && td->td_bitspersample != 32
+ && td->td_bitspersample != 64) { /* Should 64 be allowed? */
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Floating point \"Predictor\" not supported with %d-bit samples",
+ td->td_bitspersample);
+ return 0;
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "\"Predictor\" value %d not supported",
+ sp->predictor);
+ return 0;
+ }
+ sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
+ td->td_samplesperpixel : 1);
+ /*
+ * Calculate the scanline/tile-width size in bytes.
+ */
+ if (isTiled(tif))
+ sp->rowsize = TIFFTileRowSize(tif);
+ else
+ sp->rowsize = TIFFScanlineSize(tif);
+ if (sp->rowsize == 0)
+ return 0;
+
+ return 1;
+}
+
+static int
+PredictorSetupDecode(TIFF* tif)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ /* Note: when PredictorSetup() fails, the effets of setupdecode() */
+ /* will not be "cancelled" so setupdecode() might be robust to */
+ /* be called several times. */
+ if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
+ return 0;
+
+ if (sp->predictor == 2) {
+ switch (td->td_bitspersample) {
+ case 8: sp->decodepfunc = horAcc8; break;
+ case 16: sp->decodepfunc = horAcc16; break;
+ case 32: sp->decodepfunc = horAcc32; break;
+ }
+ /*
+ * Override default decoding method with one that does the
+ * predictor stuff.
+ */
+ if( tif->tif_decoderow != PredictorDecodeRow )
+ {
+ sp->decoderow = tif->tif_decoderow;
+ tif->tif_decoderow = PredictorDecodeRow;
+ sp->decodestrip = tif->tif_decodestrip;
+ tif->tif_decodestrip = PredictorDecodeTile;
+ sp->decodetile = tif->tif_decodetile;
+ tif->tif_decodetile = PredictorDecodeTile;
+ }
+
+ /*
+ * If the data is horizontally differenced 16-bit data that
+ * requires byte-swapping, then it must be byte swapped before
+ * the accumulation step. We do this with a special-purpose
+ * routine and override the normal post decoding logic that
+ * the library setup when the directory was read.
+ */
+ if (tif->tif_flags & TIFF_SWAB) {
+ if (sp->decodepfunc == horAcc16) {
+ sp->decodepfunc = swabHorAcc16;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ } else if (sp->decodepfunc == horAcc32) {
+ sp->decodepfunc = swabHorAcc32;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ }
+ }
+ }
+
+ else if (sp->predictor == 3) {
+ sp->decodepfunc = fpAcc;
+ /*
+ * Override default decoding method with one that does the
+ * predictor stuff.
+ */
+ if( tif->tif_decoderow != PredictorDecodeRow )
+ {
+ sp->decoderow = tif->tif_decoderow;
+ tif->tif_decoderow = PredictorDecodeRow;
+ sp->decodestrip = tif->tif_decodestrip;
+ tif->tif_decodestrip = PredictorDecodeTile;
+ sp->decodetile = tif->tif_decodetile;
+ tif->tif_decodetile = PredictorDecodeTile;
+ }
+ /*
+ * The data should not be swapped outside of the floating
+ * point predictor, the accumulation routine should return
+ * byres in the native order.
+ */
+ if (tif->tif_flags & TIFF_SWAB) {
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ }
+ /*
+ * Allocate buffer to keep the decoded bytes before
+ * rearranging in the right order
+ */
+ }
+
+ return 1;
+}
+
+static int
+PredictorSetupEncode(TIFF* tif)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
+ return 0;
+
+ if (sp->predictor == 2) {
+ switch (td->td_bitspersample) {
+ case 8: sp->encodepfunc = horDiff8; break;
+ case 16: sp->encodepfunc = horDiff16; break;
+ case 32: sp->encodepfunc = horDiff32; break;
+ }
+ /*
+ * Override default encoding method with one that does the
+ * predictor stuff.
+ */
+ if( tif->tif_encoderow != PredictorEncodeRow )
+ {
+ sp->encoderow = tif->tif_encoderow;
+ tif->tif_encoderow = PredictorEncodeRow;
+ sp->encodestrip = tif->tif_encodestrip;
+ tif->tif_encodestrip = PredictorEncodeTile;
+ sp->encodetile = tif->tif_encodetile;
+ tif->tif_encodetile = PredictorEncodeTile;
+ }
+
+ /*
+ * If the data is horizontally differenced 16-bit data that
+ * requires byte-swapping, then it must be byte swapped after
+ * the differentiation step. We do this with a special-purpose
+ * routine and override the normal post decoding logic that
+ * the library setup when the directory was read.
+ */
+ if (tif->tif_flags & TIFF_SWAB) {
+ if (sp->encodepfunc == horDiff16) {
+ sp->encodepfunc = swabHorDiff16;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ } else if (sp->encodepfunc == horDiff32) {
+ sp->encodepfunc = swabHorDiff32;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ }
+ }
+ }
+
+ else if (sp->predictor == 3) {
+ sp->encodepfunc = fpDiff;
+ /*
+ * Override default encoding method with one that does the
+ * predictor stuff.
+ */
+ if( tif->tif_encoderow != PredictorEncodeRow )
+ {
+ sp->encoderow = tif->tif_encoderow;
+ tif->tif_encoderow = PredictorEncodeRow;
+ sp->encodestrip = tif->tif_encodestrip;
+ tif->tif_encodestrip = PredictorEncodeTile;
+ sp->encodetile = tif->tif_encodetile;
+ tif->tif_encodetile = PredictorEncodeTile;
+ }
+ }
+
+ return 1;
+}
+
+#define REPEAT4(n, op) \
+ switch (n) { \
+ default: { \
+ tmsize_t i; for (i = n-4; i > 0; i--) { op; } } /*-fallthrough*/ \
+ case 4: op; /*-fallthrough*/ \
+ case 3: op; /*-fallthrough*/ \
+ case 2: op; /*-fallthrough*/ \
+ case 1: op; /*-fallthrough*/ \
+ case 0: ; \
+ }
+
+/* Remarks related to C standard compliance in all below functions : */
+/* - to avoid any undefined behaviour, we only operate on unsigned types */
+/* since the behaviour of "overflows" is defined (wrap over) */
+/* - when storing into the byte stream, we explicitly mask with 0xff so */
+/* as to make icc -check=conversions happy (not necessary by the standard) */
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ tmsize_t stride = PredictorState(tif)->stride;
+
+ unsigned char* cp = (unsigned char*) cp0;
+ if((cc%stride)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horAcc8",
+ "%s", "(cc%stride)!=0");
+ return 0;
+ }
+
+ if (cc > stride) {
+ /*
+ * Pipeline the most common cases.
+ */
+ if (stride == 3) {
+ unsigned int cr = cp[0];
+ unsigned int cg = cp[1];
+ unsigned int cb = cp[2];
+ cc -= 3;
+ cp += 3;
+ while (cc>0) {
+ cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
+ cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
+ cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
+ cc -= 3;
+ cp += 3;
+ }
+ } else if (stride == 4) {
+ unsigned int cr = cp[0];
+ unsigned int cg = cp[1];
+ unsigned int cb = cp[2];
+ unsigned int ca = cp[3];
+ cc -= 4;
+ cp += 4;
+ while (cc>0) {
+ cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
+ cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
+ cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
+ cp[3] = (unsigned char) ((ca += cp[3]) & 0xff);
+ cc -= 4;
+ cp += 4;
+ }
+ } else {
+ cc -= stride;
+ do {
+ REPEAT4(stride, cp[stride] =
+ (unsigned char) ((cp[stride] + *cp) & 0xff); cp++)
+ cc -= stride;
+ } while (cc>0);
+ }
+ }
+ return 1;
+}
+
+static int
+swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ uint16* wp = (uint16*) cp0;
+ tmsize_t wc = cc / 2;
+
+ TIFFSwabArrayOfShort(wp, wc);
+ return horAcc16(tif, cp0, cc);
+}
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ tmsize_t stride = PredictorState(tif)->stride;
+ uint16* wp = (uint16*) cp0;
+ tmsize_t wc = cc / 2;
+
+ if((cc%(2*stride))!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horAcc16",
+ "%s", "cc%(2*stride))!=0");
+ return 0;
+ }
+
+ if (wc > stride) {
+ wc -= stride;
+ do {
+ REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] + (unsigned int)wp[0]) & 0xffff); wp++)
+ wc -= stride;
+ } while (wc > 0);
+ }
+ return 1;
+}
+
+static int
+swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ uint32* wp = (uint32*) cp0;
+ tmsize_t wc = cc / 4;
+
+ TIFFSwabArrayOfLong(wp, wc);
+ return horAcc32(tif, cp0, cc);
+}
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ tmsize_t stride = PredictorState(tif)->stride;
+ uint32* wp = (uint32*) cp0;
+ tmsize_t wc = cc / 4;
+
+ if((cc%(4*stride))!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horAcc32",
+ "%s", "cc%(4*stride))!=0");
+ return 0;
+ }
+
+ if (wc > stride) {
+ wc -= stride;
+ do {
+ REPEAT4(stride, wp[stride] += wp[0]; wp++)
+ wc -= stride;
+ } while (wc > 0);
+ }
+ return 1;
+}
+
+/*
+ * Floating point predictor accumulation routine.
+ */
+static int
+fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ tmsize_t stride = PredictorState(tif)->stride;
+ uint32 bps = tif->tif_dir.td_bitspersample / 8;
+ tmsize_t wc = cc / bps;
+ tmsize_t count = cc;
+ uint8 *cp = (uint8 *) cp0;
+ uint8 *tmp;
+
+ if(cc%(bps*stride)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "fpAcc",
+ "%s", "cc%(bps*stride))!=0");
+ return 0;
+ }
+
+ tmp = (uint8 *)_TIFFmalloc(cc);
+ if (!tmp)
+ return 0;
+
+ while (count > stride) {
+ REPEAT4(stride, cp[stride] =
+ (unsigned char) ((cp[stride] + cp[0]) & 0xff); cp++)
+ count -= stride;
+ }
+
+ _TIFFmemcpy(tmp, cp0, cc);
+ cp = (uint8 *) cp0;
+ for (count = 0; count < wc; count++) {
+ uint32 byte;
+ for (byte = 0; byte < bps; byte++) {
+ #if WORDS_BIGENDIAN
+ cp[bps * count + byte] = tmp[byte * wc + count];
+ #else
+ cp[bps * count + byte] =
+ tmp[(bps - byte - 1) * wc + count];
+ #endif
+ }
+ }
+ _TIFFfree(tmp);
+ return 1;
+}
+
+/*
+ * Decode a scanline and apply the predictor routine.
+ */
+static int
+PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
+{
+ TIFFPredictorState *sp = PredictorState(tif);
+
+ assert(sp != NULL);
+ assert(sp->decoderow != NULL);
+ assert(sp->decodepfunc != NULL);
+
+ if ((*sp->decoderow)(tif, op0, occ0, s)) {
+ return (*sp->decodepfunc)(tif, op0, occ0);
+ } else
+ return 0;
+}
+
+/*
+ * Decode a tile/strip and apply the predictor routine.
+ * Note that horizontal differencing must be done on a
+ * row-by-row basis. The width of a "row" has already
+ * been calculated at pre-decode time according to the
+ * strip/tile dimensions.
+ */
+static int
+PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
+{
+ TIFFPredictorState *sp = PredictorState(tif);
+
+ assert(sp != NULL);
+ assert(sp->decodetile != NULL);
+
+ if ((*sp->decodetile)(tif, op0, occ0, s)) {
+ tmsize_t rowsize = sp->rowsize;
+ assert(rowsize > 0);
+ if((occ0%rowsize) !=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "PredictorDecodeTile",
+ "%s", "occ0%rowsize != 0");
+ return 0;
+ }
+ assert(sp->decodepfunc != NULL);
+ while (occ0 > 0) {
+ if( !(*sp->decodepfunc)(tif, op0, rowsize) )
+ return 0;
+ occ0 -= rowsize;
+ op0 += rowsize;
+ }
+ return 1;
+ } else
+ return 0;
+}
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ tmsize_t stride = sp->stride;
+ unsigned char* cp = (unsigned char*) cp0;
+
+ if((cc%stride)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horDiff8",
+ "%s", "(cc%stride)!=0");
+ return 0;
+ }
+
+ if (cc > stride) {
+ cc -= stride;
+ /*
+ * Pipeline the most common cases.
+ */
+ if (stride == 3) {
+ unsigned int r1, g1, b1;
+ unsigned int r2 = cp[0];
+ unsigned int g2 = cp[1];
+ unsigned int b2 = cp[2];
+ do {
+ r1 = cp[3]; cp[3] = (unsigned char)((r1-r2)&0xff); r2 = r1;
+ g1 = cp[4]; cp[4] = (unsigned char)((g1-g2)&0xff); g2 = g1;
+ b1 = cp[5]; cp[5] = (unsigned char)((b1-b2)&0xff); b2 = b1;
+ cp += 3;
+ } while ((cc -= 3) > 0);
+ } else if (stride == 4) {
+ unsigned int r1, g1, b1, a1;
+ unsigned int r2 = cp[0];
+ unsigned int g2 = cp[1];
+ unsigned int b2 = cp[2];
+ unsigned int a2 = cp[3];
+ do {
+ r1 = cp[4]; cp[4] = (unsigned char)((r1-r2)&0xff); r2 = r1;
+ g1 = cp[5]; cp[5] = (unsigned char)((g1-g2)&0xff); g2 = g1;
+ b1 = cp[6]; cp[6] = (unsigned char)((b1-b2)&0xff); b2 = b1;
+ a1 = cp[7]; cp[7] = (unsigned char)((a1-a2)&0xff); a2 = a1;
+ cp += 4;
+ } while ((cc -= 4) > 0);
+ } else {
+ cp += cc - 1;
+ do {
+ REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
+ } while ((cc -= stride) > 0);
+ }
+ }
+ return 1;
+}
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ tmsize_t stride = sp->stride;
+ uint16 *wp = (uint16*) cp0;
+ tmsize_t wc = cc/2;
+
+ if((cc%(2*stride))!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horDiff8",
+ "%s", "(cc%(2*stride))!=0");
+ return 0;
+ }
+
+ if (wc > stride) {
+ wc -= stride;
+ wp += wc - 1;
+ do {
+ REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] - (unsigned int)wp[0]) & 0xffff); wp--)
+ wc -= stride;
+ } while (wc > 0);
+ }
+ return 1;
+}
+
+static int
+swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ uint16* wp = (uint16*) cp0;
+ tmsize_t wc = cc / 2;
+
+ if( !horDiff16(tif, cp0, cc) )
+ return 0;
+
+ TIFFSwabArrayOfShort(wp, wc);
+ return 1;
+}
+
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ tmsize_t stride = sp->stride;
+ uint32 *wp = (uint32*) cp0;
+ tmsize_t wc = cc/4;
+
+ if((cc%(4*stride))!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "horDiff32",
+ "%s", "(cc%(4*stride))!=0");
+ return 0;
+ }
+
+ if (wc > stride) {
+ wc -= stride;
+ wp += wc - 1;
+ do {
+ REPEAT4(stride, wp[stride] -= wp[0]; wp--)
+ wc -= stride;
+ } while (wc > 0);
+ }
+ return 1;
+}
+
+static int
+swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ uint32* wp = (uint32*) cp0;
+ tmsize_t wc = cc / 4;
+
+ if( !horDiff32(tif, cp0, cc) )
+ return 0;
+
+ TIFFSwabArrayOfLong(wp, wc);
+ return 1;
+}
+
+/*
+ * Floating point predictor differencing routine.
+ */
+TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+static int
+fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+ tmsize_t stride = PredictorState(tif)->stride;
+ uint32 bps = tif->tif_dir.td_bitspersample / 8;
+ tmsize_t wc = cc / bps;
+ tmsize_t count;
+ uint8 *cp = (uint8 *) cp0;
+ uint8 *tmp;
+
+ if((cc%(bps*stride))!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "fpDiff",
+ "%s", "(cc%(bps*stride))!=0");
+ return 0;
+ }
+
+ tmp = (uint8 *)_TIFFmalloc(cc);
+ if (!tmp)
+ return 0;
+
+ _TIFFmemcpy(tmp, cp0, cc);
+ for (count = 0; count < wc; count++) {
+ uint32 byte;
+ for (byte = 0; byte < bps; byte++) {
+ #if WORDS_BIGENDIAN
+ cp[byte * wc + count] = tmp[bps * count + byte];
+ #else
+ cp[(bps - byte - 1) * wc + count] =
+ tmp[bps * count + byte];
+ #endif
+ }
+ }
+ _TIFFfree(tmp);
+
+ cp = (uint8 *) cp0;
+ cp += cc - stride - 1;
+ for (count = cc; count > stride; count -= stride)
+ REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
+ return 1;
+}
+
+static int
+PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ TIFFPredictorState *sp = PredictorState(tif);
+
+ assert(sp != NULL);
+ assert(sp->encodepfunc != NULL);
+ assert(sp->encoderow != NULL);
+
+ /* XXX horizontal differencing alters user's data XXX */
+ if( !(*sp->encodepfunc)(tif, bp, cc) )
+ return 0;
+ return (*sp->encoderow)(tif, bp, cc, s);
+}
+
+static int
+PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
+{
+ static const char module[] = "PredictorEncodeTile";
+ TIFFPredictorState *sp = PredictorState(tif);
+ uint8 *working_copy;
+ tmsize_t cc = cc0, rowsize;
+ unsigned char* bp;
+ int result_code;
+
+ assert(sp != NULL);
+ assert(sp->encodepfunc != NULL);
+ assert(sp->encodetile != NULL);
+
+ /*
+ * Do predictor manipulation in a working buffer to avoid altering
+ * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
+ */
+ working_copy = (uint8*) _TIFFmalloc(cc0);
+ if( working_copy == NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.",
+ cc0 );
+ return 0;
+ }
+ memcpy( working_copy, bp0, cc0 );
+ bp = working_copy;
+
+ rowsize = sp->rowsize;
+ assert(rowsize > 0);
+ if((cc0%rowsize)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "PredictorEncodeTile",
+ "%s", "(cc0%rowsize)!=0");
+ _TIFFfree( working_copy );
+ return 0;
+ }
+ while (cc > 0) {
+ (*sp->encodepfunc)(tif, bp, rowsize);
+ cc -= rowsize;
+ bp += rowsize;
+ }
+ result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
+
+ _TIFFfree( working_copy );
+
+ return result_code;
+}
+
+#define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
+
+static const TIFFField predictFields[] = {
+ { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
+};
+
+static int
+PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ TIFFPredictorState *sp = PredictorState(tif);
+
+ assert(sp != NULL);
+ assert(sp->vsetparent != NULL);
+
+ switch (tag) {
+ case TIFFTAG_PREDICTOR:
+ sp->predictor = (uint16) va_arg(ap, uint16_vap);
+ TIFFSetFieldBit(tif, FIELD_PREDICTOR);
+ break;
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+ return 1;
+}
+
+static int
+PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ TIFFPredictorState *sp = PredictorState(tif);
+
+ assert(sp != NULL);
+ assert(sp->vgetparent != NULL);
+
+ switch (tag) {
+ case TIFFTAG_PREDICTOR:
+ *va_arg(ap, uint16*) = (uint16)sp->predictor;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return 1;
+}
+
+static void
+PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+
+ (void) flags;
+ if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
+ fprintf(fd, " Predictor: ");
+ switch (sp->predictor) {
+ case 1: fprintf(fd, "none "); break;
+ case 2: fprintf(fd, "horizontal differencing "); break;
+ case 3: fprintf(fd, "floating point predictor "); break;
+ }
+ fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
+ }
+ if (sp->printdir)
+ (*sp->printdir)(tif, fd, flags);
+}
+
+int
+TIFFPredictorInit(TIFF* tif)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+
+ assert(sp != 0);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, predictFields,
+ TIFFArrayCount(predictFields))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
+ "Merging Predictor codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield =
+ PredictorVGetField;/* hook for predictor tag */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield =
+ PredictorVSetField;/* hook for predictor tag */
+ sp->printdir = tif->tif_tagmethods.printdir;
+ tif->tif_tagmethods.printdir =
+ PredictorPrintDir; /* hook for predictor tag */
+
+ sp->setupdecode = tif->tif_setupdecode;
+ tif->tif_setupdecode = PredictorSetupDecode;
+ sp->setupencode = tif->tif_setupencode;
+ tif->tif_setupencode = PredictorSetupEncode;
+
+ sp->predictor = 1; /* default value */
+ sp->encodepfunc = NULL; /* no predictor routine */
+ sp->decodepfunc = NULL; /* no predictor routine */
+ return 1;
+}
+
+int
+TIFFPredictorCleanup(TIFF* tif)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+
+ assert(sp != 0);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+ tif->tif_tagmethods.printdir = sp->printdir;
+ tif->tif_setupdecode = sp->setupdecode;
+ tif->tif_setupencode = sp->setupencode;
+
+ return 1;
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_predict.h b/test/monniaux/tiff-4.0.10/tif_predict.h
new file mode 100644
index 00000000..a326b9b8
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_predict.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 1995-1997 Sam Leffler
+ * Copyright (c) 1995-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _TIFFPREDICT_
+#define _TIFFPREDICT_
+
+#include "tiffio.h"
+#include "tiffiop.h"
+
+/*
+ * ``Library-private'' Support for the Predictor Tag
+ */
+
+typedef int (*TIFFEncodeDecodeMethod)(TIFF* tif, uint8* buf, tmsize_t size);
+
+/*
+ * Codecs that want to support the Predictor tag must place
+ * this structure first in their private state block so that
+ * the predictor code can cast tif_data to find its state.
+ */
+typedef struct {
+ int predictor; /* predictor tag value */
+ tmsize_t stride; /* sample stride over data */
+ tmsize_t rowsize; /* tile/strip row size */
+
+ TIFFCodeMethod encoderow; /* parent codec encode/decode row */
+ TIFFCodeMethod encodestrip; /* parent codec encode/decode strip */
+ TIFFCodeMethod encodetile; /* parent codec encode/decode tile */
+ TIFFEncodeDecodeMethod encodepfunc; /* horizontal differencer */
+
+ TIFFCodeMethod decoderow; /* parent codec encode/decode row */
+ TIFFCodeMethod decodestrip; /* parent codec encode/decode strip */
+ TIFFCodeMethod decodetile; /* parent codec encode/decode tile */
+ TIFFEncodeDecodeMethod decodepfunc; /* horizontal accumulator */
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+ TIFFPrintMethod printdir; /* super-class method */
+ TIFFBoolMethod setupdecode; /* super-class method */
+ TIFFBoolMethod setupencode; /* super-class method */
+} TIFFPredictorState;
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+extern int TIFFPredictorInit(TIFF*);
+extern int TIFFPredictorCleanup(TIFF*);
+#if defined(__cplusplus)
+}
+#endif
+#endif /* _TIFFPREDICT_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_print.c b/test/monniaux/tiff-4.0.10/tif_print.c
new file mode 100644
index 00000000..1d86adbf
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_print.c
@@ -0,0 +1,720 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Directory Printing Support
+ */
+#include "tiffiop.h"
+#include <stdio.h>
+
+#include <ctype.h>
+
+static void
+_TIFFprintAsciiBounded(FILE* fd, const char* cp, size_t max_chars);
+
+static const char * const photoNames[] = {
+ "min-is-white", /* PHOTOMETRIC_MINISWHITE */
+ "min-is-black", /* PHOTOMETRIC_MINISBLACK */
+ "RGB color", /* PHOTOMETRIC_RGB */
+ "palette color (RGB from colormap)", /* PHOTOMETRIC_PALETTE */
+ "transparency mask", /* PHOTOMETRIC_MASK */
+ "separated", /* PHOTOMETRIC_SEPARATED */
+ "YCbCr", /* PHOTOMETRIC_YCBCR */
+ "7 (0x7)",
+ "CIE L*a*b*", /* PHOTOMETRIC_CIELAB */
+ "ICC L*a*b*", /* PHOTOMETRIC_ICCLAB */
+ "ITU L*a*b*" /* PHOTOMETRIC_ITULAB */
+};
+#define NPHOTONAMES (sizeof (photoNames) / sizeof (photoNames[0]))
+
+static const char * const orientNames[] = {
+ "0 (0x0)",
+ "row 0 top, col 0 lhs", /* ORIENTATION_TOPLEFT */
+ "row 0 top, col 0 rhs", /* ORIENTATION_TOPRIGHT */
+ "row 0 bottom, col 0 rhs", /* ORIENTATION_BOTRIGHT */
+ "row 0 bottom, col 0 lhs", /* ORIENTATION_BOTLEFT */
+ "row 0 lhs, col 0 top", /* ORIENTATION_LEFTTOP */
+ "row 0 rhs, col 0 top", /* ORIENTATION_RIGHTTOP */
+ "row 0 rhs, col 0 bottom", /* ORIENTATION_RIGHTBOT */
+ "row 0 lhs, col 0 bottom", /* ORIENTATION_LEFTBOT */
+};
+#define NORIENTNAMES (sizeof (orientNames) / sizeof (orientNames[0]))
+
+static void
+_TIFFPrintField(FILE* fd, const TIFFField *fip,
+ uint32 value_count, void *raw_data)
+{
+ uint32 j;
+
+ fprintf(fd, " %s: ", fip->field_name);
+
+ for(j = 0; j < value_count; j++) {
+ if(fip->field_type == TIFF_BYTE)
+ fprintf(fd, "%u", ((uint8 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_UNDEFINED)
+ fprintf(fd, "0x%x",
+ (unsigned int) ((unsigned char *) raw_data)[j]);
+ else if(fip->field_type == TIFF_SBYTE)
+ fprintf(fd, "%d", ((int8 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_SHORT)
+ fprintf(fd, "%u", ((uint16 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_SSHORT)
+ fprintf(fd, "%d", ((int16 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_LONG)
+ fprintf(fd, "%lu",
+ (unsigned long)((uint32 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_SLONG)
+ fprintf(fd, "%ld", (long)((int32 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_IFD)
+ fprintf(fd, "0x%lx",
+ (unsigned long)((uint32 *) raw_data)[j]);
+ else if(fip->field_type == TIFF_RATIONAL
+ || fip->field_type == TIFF_SRATIONAL
+ || fip->field_type == TIFF_FLOAT)
+ fprintf(fd, "%f", ((float *) raw_data)[j]);
+ else if(fip->field_type == TIFF_LONG8)
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, "%I64u",
+ (unsigned __int64)((uint64 *) raw_data)[j]);
+#else
+ fprintf(fd, "%llu",
+ (unsigned long long)((uint64 *) raw_data)[j]);
+#endif
+ else if(fip->field_type == TIFF_SLONG8)
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, "%I64d", (__int64)((int64 *) raw_data)[j]);
+#else
+ fprintf(fd, "%lld", (long long)((int64 *) raw_data)[j]);
+#endif
+ else if(fip->field_type == TIFF_IFD8)
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, "0x%I64x",
+ (unsigned __int64)((uint64 *) raw_data)[j]);
+#else
+ fprintf(fd, "0x%llx",
+ (unsigned long long)((uint64 *) raw_data)[j]);
+#endif
+ else if(fip->field_type == TIFF_FLOAT)
+ fprintf(fd, "%f", ((float *)raw_data)[j]);
+ else if(fip->field_type == TIFF_DOUBLE)
+ fprintf(fd, "%f", ((double *) raw_data)[j]);
+ else if(fip->field_type == TIFF_ASCII) {
+ fprintf(fd, "%s", (char *) raw_data);
+ break;
+ }
+ else {
+ fprintf(fd, "<unsupported data type in TIFFPrint>");
+ break;
+ }
+
+ if(j < value_count - 1)
+ fprintf(fd, ",");
+ }
+
+ fprintf(fd, "\n");
+}
+
+static int
+_TIFFPrettyPrintField(TIFF* tif, const TIFFField *fip, FILE* fd, uint32 tag,
+ uint32 value_count, void *raw_data)
+{
+ (void) tif;
+
+ /* do not try to pretty print auto-defined fields */
+ if (strncmp(fip->field_name,"Tag ", 4) == 0) {
+ return 0;
+ }
+
+ switch (tag)
+ {
+ case TIFFTAG_INKSET:
+ if (value_count == 2 && fip->field_type == TIFF_SHORT) {
+ fprintf(fd, " Ink Set: ");
+ switch (*((uint16*)raw_data)) {
+ case INKSET_CMYK:
+ fprintf(fd, "CMYK\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ *((uint16*)raw_data),
+ *((uint16*)raw_data));
+ break;
+ }
+ return 1;
+ }
+ return 0;
+
+ case TIFFTAG_DOTRANGE:
+ if (value_count == 2 && fip->field_type == TIFF_SHORT) {
+ fprintf(fd, " Dot Range: %u-%u\n",
+ ((uint16*)raw_data)[0], ((uint16*)raw_data)[1]);
+ return 1;
+ }
+ return 0;
+
+ case TIFFTAG_WHITEPOINT:
+ if (value_count == 2 && fip->field_type == TIFF_RATIONAL) {
+ fprintf(fd, " White Point: %g-%g\n",
+ ((float *)raw_data)[0], ((float *)raw_data)[1]);
+ return 1;
+ }
+ return 0;
+
+ case TIFFTAG_XMLPACKET:
+ {
+ uint32 i;
+
+ fprintf(fd, " XMLPacket (XMP Metadata):\n" );
+ for(i = 0; i < value_count; i++)
+ fputc(((char *)raw_data)[i], fd);
+ fprintf( fd, "\n" );
+ return 1;
+ }
+ case TIFFTAG_RICHTIFFIPTC:
+ /*
+ * XXX: for some weird reason RichTIFFIPTC tag
+ * defined as array of LONG values.
+ */
+ fprintf(fd,
+ " RichTIFFIPTC Data: <present>, %lu bytes\n",
+ (unsigned long) value_count * 4);
+ return 1;
+
+ case TIFFTAG_PHOTOSHOP:
+ fprintf(fd, " Photoshop Data: <present>, %lu bytes\n",
+ (unsigned long) value_count);
+ return 1;
+
+ case TIFFTAG_ICCPROFILE:
+ fprintf(fd, " ICC Profile: <present>, %lu bytes\n",
+ (unsigned long) value_count);
+ return 1;
+
+ case TIFFTAG_STONITS:
+ if (value_count == 1 && fip->field_type == TIFF_DOUBLE) {
+ fprintf(fd,
+ " Sample to Nits conversion factor: %.4e\n",
+ *((double*)raw_data));
+ return 1;
+ }
+ return 0;
+ }
+
+ return 0;
+}
+
+/*
+ * Print the contents of the current directory
+ * to the specified stdio file stream.
+ */
+void
+TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ char *sep;
+ long l, n;
+
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, "TIFF Directory at offset 0x%I64x (%I64u)\n",
+ (unsigned __int64) tif->tif_diroff,
+ (unsigned __int64) tif->tif_diroff);
+#else
+ fprintf(fd, "TIFF Directory at offset 0x%llx (%llu)\n",
+ (unsigned long long) tif->tif_diroff,
+ (unsigned long long) tif->tif_diroff);
+#endif
+ if (TIFFFieldSet(tif,FIELD_SUBFILETYPE)) {
+ fprintf(fd, " Subfile Type:");
+ sep = " ";
+ if (td->td_subfiletype & FILETYPE_REDUCEDIMAGE) {
+ fprintf(fd, "%sreduced-resolution image", sep);
+ sep = "/";
+ }
+ if (td->td_subfiletype & FILETYPE_PAGE) {
+ fprintf(fd, "%smulti-page document", sep);
+ sep = "/";
+ }
+ if (td->td_subfiletype & FILETYPE_MASK)
+ fprintf(fd, "%stransparency mask", sep);
+ fprintf(fd, " (%lu = 0x%lx)\n",
+ (unsigned long) td->td_subfiletype, (long) td->td_subfiletype);
+ }
+ if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS)) {
+ fprintf(fd, " Image Width: %lu Image Length: %lu",
+ (unsigned long) td->td_imagewidth, (unsigned long) td->td_imagelength);
+ if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
+ fprintf(fd, " Image Depth: %lu",
+ (unsigned long) td->td_imagedepth);
+ fprintf(fd, "\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS)) {
+ fprintf(fd, " Tile Width: %lu Tile Length: %lu",
+ (unsigned long) td->td_tilewidth, (unsigned long) td->td_tilelength);
+ if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
+ fprintf(fd, " Tile Depth: %lu",
+ (unsigned long) td->td_tiledepth);
+ fprintf(fd, "\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_RESOLUTION)) {
+ fprintf(fd, " Resolution: %g, %g",
+ td->td_xresolution, td->td_yresolution);
+ if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT)) {
+ switch (td->td_resolutionunit) {
+ case RESUNIT_NONE:
+ fprintf(fd, " (unitless)");
+ break;
+ case RESUNIT_INCH:
+ fprintf(fd, " pixels/inch");
+ break;
+ case RESUNIT_CENTIMETER:
+ fprintf(fd, " pixels/cm");
+ break;
+ default:
+ fprintf(fd, " (unit %u = 0x%x)",
+ td->td_resolutionunit,
+ td->td_resolutionunit);
+ break;
+ }
+ }
+ fprintf(fd, "\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_POSITION))
+ fprintf(fd, " Position: %g, %g\n",
+ td->td_xposition, td->td_yposition);
+ if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
+ fprintf(fd, " Bits/Sample: %u\n", td->td_bitspersample);
+ if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT)) {
+ fprintf(fd, " Sample Format: ");
+ switch (td->td_sampleformat) {
+ case SAMPLEFORMAT_VOID:
+ fprintf(fd, "void\n");
+ break;
+ case SAMPLEFORMAT_INT:
+ fprintf(fd, "signed integer\n");
+ break;
+ case SAMPLEFORMAT_UINT:
+ fprintf(fd, "unsigned integer\n");
+ break;
+ case SAMPLEFORMAT_IEEEFP:
+ fprintf(fd, "IEEE floating point\n");
+ break;
+ case SAMPLEFORMAT_COMPLEXINT:
+ fprintf(fd, "complex signed integer\n");
+ break;
+ case SAMPLEFORMAT_COMPLEXIEEEFP:
+ fprintf(fd, "complex IEEE floating point\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_sampleformat, td->td_sampleformat);
+ break;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_COMPRESSION)) {
+ const TIFFCodec* c = TIFFFindCODEC(td->td_compression);
+ fprintf(fd, " Compression Scheme: ");
+ if (c)
+ fprintf(fd, "%s\n", c->name);
+ else
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_compression, td->td_compression);
+ }
+ if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC)) {
+ fprintf(fd, " Photometric Interpretation: ");
+ if (td->td_photometric < NPHOTONAMES)
+ fprintf(fd, "%s\n", photoNames[td->td_photometric]);
+ else {
+ switch (td->td_photometric) {
+ case PHOTOMETRIC_LOGL:
+ fprintf(fd, "CIE Log2(L)\n");
+ break;
+ case PHOTOMETRIC_LOGLUV:
+ fprintf(fd, "CIE Log2(L) (u',v')\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_photometric, td->td_photometric);
+ break;
+ }
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES) && td->td_extrasamples) {
+ uint16 i;
+ fprintf(fd, " Extra Samples: %u<", td->td_extrasamples);
+ sep = "";
+ for (i = 0; i < td->td_extrasamples; i++) {
+ switch (td->td_sampleinfo[i]) {
+ case EXTRASAMPLE_UNSPECIFIED:
+ fprintf(fd, "%sunspecified", sep);
+ break;
+ case EXTRASAMPLE_ASSOCALPHA:
+ fprintf(fd, "%sassoc-alpha", sep);
+ break;
+ case EXTRASAMPLE_UNASSALPHA:
+ fprintf(fd, "%sunassoc-alpha", sep);
+ break;
+ default:
+ fprintf(fd, "%s%u (0x%x)", sep,
+ td->td_sampleinfo[i], td->td_sampleinfo[i]);
+ break;
+ }
+ sep = ", ";
+ }
+ fprintf(fd, ">\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_INKNAMES)) {
+ char* cp;
+ uint16 i;
+ fprintf(fd, " Ink Names: ");
+ i = td->td_samplesperpixel;
+ sep = "";
+ for (cp = td->td_inknames;
+ i > 0 && cp < td->td_inknames + td->td_inknameslen;
+ cp = strchr(cp,'\0')+1, i--) {
+ size_t max_chars =
+ td->td_inknameslen - (cp - td->td_inknames);
+ fputs(sep, fd);
+ _TIFFprintAsciiBounded(fd, cp, max_chars);
+ sep = ", ";
+ }
+ fputs("\n", fd);
+ }
+ if (TIFFFieldSet(tif,FIELD_THRESHHOLDING)) {
+ fprintf(fd, " Thresholding: ");
+ switch (td->td_threshholding) {
+ case THRESHHOLD_BILEVEL:
+ fprintf(fd, "bilevel art scan\n");
+ break;
+ case THRESHHOLD_HALFTONE:
+ fprintf(fd, "halftone or dithered scan\n");
+ break;
+ case THRESHHOLD_ERRORDIFFUSE:
+ fprintf(fd, "error diffused\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_threshholding, td->td_threshholding);
+ break;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_FILLORDER)) {
+ fprintf(fd, " FillOrder: ");
+ switch (td->td_fillorder) {
+ case FILLORDER_MSB2LSB:
+ fprintf(fd, "msb-to-lsb\n");
+ break;
+ case FILLORDER_LSB2MSB:
+ fprintf(fd, "lsb-to-msb\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_fillorder, td->td_fillorder);
+ break;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
+ {
+ fprintf(fd, " YCbCr Subsampling: %u, %u\n",
+ td->td_ycbcrsubsampling[0], td->td_ycbcrsubsampling[1] );
+ }
+ if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING)) {
+ fprintf(fd, " YCbCr Positioning: ");
+ switch (td->td_ycbcrpositioning) {
+ case YCBCRPOSITION_CENTERED:
+ fprintf(fd, "centered\n");
+ break;
+ case YCBCRPOSITION_COSITED:
+ fprintf(fd, "cosited\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_ycbcrpositioning, td->td_ycbcrpositioning);
+ break;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
+ fprintf(fd, " Halftone Hints: light %u dark %u\n",
+ td->td_halftonehints[0], td->td_halftonehints[1]);
+ if (TIFFFieldSet(tif,FIELD_ORIENTATION)) {
+ fprintf(fd, " Orientation: ");
+ if (td->td_orientation < NORIENTNAMES)
+ fprintf(fd, "%s\n", orientNames[td->td_orientation]);
+ else
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_orientation, td->td_orientation);
+ }
+ if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
+ fprintf(fd, " Samples/Pixel: %u\n", td->td_samplesperpixel);
+ if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP)) {
+ fprintf(fd, " Rows/Strip: ");
+ if (td->td_rowsperstrip == (uint32) -1)
+ fprintf(fd, "(infinite)\n");
+ else
+ fprintf(fd, "%lu\n", (unsigned long) td->td_rowsperstrip);
+ }
+ if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
+ fprintf(fd, " Min Sample Value: %u\n", td->td_minsamplevalue);
+ if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
+ fprintf(fd, " Max Sample Value: %u\n", td->td_maxsamplevalue);
+ if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE)) {
+ int i;
+ int count = (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
+ fprintf(fd, " SMin Sample Value:");
+ for (i = 0; i < count; ++i)
+ fprintf(fd, " %g", td->td_sminsamplevalue[i]);
+ fprintf(fd, "\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE)) {
+ int i;
+ int count = (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
+ fprintf(fd, " SMax Sample Value:");
+ for (i = 0; i < count; ++i)
+ fprintf(fd, " %g", td->td_smaxsamplevalue[i]);
+ fprintf(fd, "\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_PLANARCONFIG)) {
+ fprintf(fd, " Planar Configuration: ");
+ switch (td->td_planarconfig) {
+ case PLANARCONFIG_CONTIG:
+ fprintf(fd, "single image plane\n");
+ break;
+ case PLANARCONFIG_SEPARATE:
+ fprintf(fd, "separate image planes\n");
+ break;
+ default:
+ fprintf(fd, "%u (0x%x)\n",
+ td->td_planarconfig, td->td_planarconfig);
+ break;
+ }
+ }
+ if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
+ fprintf(fd, " Page Number: %u-%u\n",
+ td->td_pagenumber[0], td->td_pagenumber[1]);
+ if (TIFFFieldSet(tif,FIELD_COLORMAP)) {
+ fprintf(fd, " Color Map: ");
+ if (flags & TIFFPRINT_COLORMAP) {
+ fprintf(fd, "\n");
+ n = 1L<<td->td_bitspersample;
+ for (l = 0; l < n; l++)
+ fprintf(fd, " %5ld: %5u %5u %5u\n",
+ l,
+ td->td_colormap[0][l],
+ td->td_colormap[1][l],
+ td->td_colormap[2][l]);
+ } else
+ fprintf(fd, "(present)\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE)) {
+ int i;
+ fprintf(fd, " Reference Black/White:\n");
+ for (i = 0; i < 3; i++)
+ fprintf(fd, " %2d: %5g %5g\n", i,
+ td->td_refblackwhite[2*i+0],
+ td->td_refblackwhite[2*i+1]);
+ }
+ if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION)) {
+ fprintf(fd, " Transfer Function: ");
+ if (flags & TIFFPRINT_CURVES) {
+ fprintf(fd, "\n");
+ n = 1L<<td->td_bitspersample;
+ for (l = 0; l < n; l++) {
+ uint16 i;
+ fprintf(fd, " %2ld: %5u",
+ l, td->td_transferfunction[0][l]);
+ for (i = 1; i < td->td_samplesperpixel - td->td_extrasamples && i < 3; i++)
+ fprintf(fd, " %5u",
+ td->td_transferfunction[i][l]);
+ fputc('\n', fd);
+ }
+ } else
+ fprintf(fd, "(present)\n");
+ }
+ if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd)) {
+ uint16 i;
+ fprintf(fd, " SubIFD Offsets:");
+ for (i = 0; i < td->td_nsubifd; i++)
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, " %5I64u",
+ (unsigned __int64) td->td_subifd[i]);
+#else
+ fprintf(fd, " %5llu",
+ (unsigned long long) td->td_subifd[i]);
+#endif
+ fputc('\n', fd);
+ }
+
+ /*
+ ** Custom tag support.
+ */
+ {
+ int i;
+ short count;
+
+ count = (short) TIFFGetTagListCount(tif);
+ for(i = 0; i < count; i++) {
+ uint32 tag = TIFFGetTagListEntry(tif, i);
+ const TIFFField *fip;
+ uint32 value_count;
+ int mem_alloc = 0;
+ void *raw_data;
+
+ fip = TIFFFieldWithTag(tif, tag);
+ if(fip == NULL)
+ continue;
+
+ if(fip->field_passcount) {
+ if (fip->field_readcount == TIFF_VARIABLE2 ) {
+ if(TIFFGetField(tif, tag, &value_count, &raw_data) != 1)
+ continue;
+ } else if (fip->field_readcount == TIFF_VARIABLE ) {
+ uint16 small_value_count;
+ if(TIFFGetField(tif, tag, &small_value_count, &raw_data) != 1)
+ continue;
+ value_count = small_value_count;
+ } else {
+ assert (fip->field_readcount == TIFF_VARIABLE
+ || fip->field_readcount == TIFF_VARIABLE2);
+ continue;
+ }
+ } else {
+ if (fip->field_readcount == TIFF_VARIABLE
+ || fip->field_readcount == TIFF_VARIABLE2)
+ value_count = 1;
+ else if (fip->field_readcount == TIFF_SPP)
+ value_count = td->td_samplesperpixel;
+ else
+ value_count = fip->field_readcount;
+ if (fip->field_tag == TIFFTAG_DOTRANGE
+ && strcmp(fip->field_name,"DotRange") == 0) {
+ /* TODO: This is an evil exception and should not have been
+ handled this way ... likely best if we move it into
+ the directory structure with an explicit field in
+ libtiff 4.1 and assign it a FIELD_ value */
+ static uint16 dotrange[2];
+ raw_data = dotrange;
+ TIFFGetField(tif, tag, dotrange+0, dotrange+1);
+ } else if (fip->field_type == TIFF_ASCII
+ || fip->field_readcount == TIFF_VARIABLE
+ || fip->field_readcount == TIFF_VARIABLE2
+ || fip->field_readcount == TIFF_SPP
+ || value_count > 1) {
+ if(TIFFGetField(tif, tag, &raw_data) != 1)
+ continue;
+ } else {
+ raw_data = _TIFFmalloc(
+ _TIFFDataSize(fip->field_type)
+ * value_count);
+ mem_alloc = 1;
+ if(TIFFGetField(tif, tag, raw_data) != 1) {
+ _TIFFfree(raw_data);
+ continue;
+ }
+ }
+ }
+
+ /*
+ * Catch the tags which needs to be specially handled
+ * and pretty print them. If tag not handled in
+ * _TIFFPrettyPrintField() fall down and print it as
+ * any other tag.
+ */
+ if (!_TIFFPrettyPrintField(tif, fip, fd, tag, value_count, raw_data))
+ _TIFFPrintField(fd, fip, value_count, raw_data);
+
+ if(mem_alloc)
+ _TIFFfree(raw_data);
+ }
+ }
+
+ if (tif->tif_tagmethods.printdir)
+ (*tif->tif_tagmethods.printdir)(tif, fd, flags);
+
+ _TIFFFillStriles( tif );
+
+ if ((flags & TIFFPRINT_STRIPS) &&
+ TIFFFieldSet(tif,FIELD_STRIPOFFSETS)) {
+ uint32 s;
+
+ fprintf(fd, " %lu %s:\n",
+ (unsigned long) td->td_nstrips,
+ isTiled(tif) ? "Tiles" : "Strips");
+ for (s = 0; s < td->td_nstrips; s++)
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ fprintf(fd, " %3lu: [%8I64u, %8I64u]\n",
+ (unsigned long) s,
+ td->td_stripoffset ? (unsigned __int64) td->td_stripoffset[s] : 0,
+ td->td_stripbytecount ? (unsigned __int64) td->td_stripbytecount[s] : 0);
+#else
+ fprintf(fd, " %3lu: [%8llu, %8llu]\n",
+ (unsigned long) s,
+ td->td_stripoffset ? (unsigned long long) td->td_stripoffset[s] : 0,
+ td->td_stripbytecount ? (unsigned long long) td->td_stripbytecount[s] : 0);
+#endif
+ }
+}
+
+void
+_TIFFprintAscii(FILE* fd, const char* cp)
+{
+ _TIFFprintAsciiBounded( fd, cp, strlen(cp));
+}
+
+static void
+_TIFFprintAsciiBounded(FILE* fd, const char* cp, size_t max_chars)
+{
+ for (; max_chars > 0 && *cp != '\0'; cp++, max_chars--) {
+ const char* tp;
+
+ if (isprint((int)*cp)) {
+ fputc(*cp, fd);
+ continue;
+ }
+ for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++)
+ if (*tp++ == *cp)
+ break;
+ if (*tp)
+ fprintf(fd, "\\%c", *tp);
+ else
+ fprintf(fd, "\\%03o", *cp & 0xff);
+ }
+}
+
+void
+_TIFFprintAsciiTag(FILE* fd, const char* name, const char* value)
+{
+ fprintf(fd, " %s: \"", name);
+ _TIFFprintAscii(fd, value);
+ fprintf(fd, "\"\n");
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_read.c b/test/monniaux/tiff-4.0.10/tif_read.c
new file mode 100644
index 00000000..e63810cc
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_read.c
@@ -0,0 +1,1577 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ * Scanline-oriented Read Support
+ */
+#include "tiffiop.h"
+#include <stdio.h>
+
+#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
+#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
+
+int TIFFFillStrip(TIFF* tif, uint32 strip);
+int TIFFFillTile(TIFF* tif, uint32 tile);
+static int TIFFStartStrip(TIFF* tif, uint32 strip);
+static int TIFFStartTile(TIFF* tif, uint32 tile);
+static int TIFFCheckRead(TIFF*, int);
+static tmsize_t
+TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
+static tmsize_t
+TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module);
+
+#define NOSTRIP ((uint32)(-1)) /* undefined state */
+#define NOTILE ((uint32)(-1)) /* undefined state */
+
+#define INITIAL_THRESHOLD (1024 * 1024)
+#define THRESHOLD_MULTIPLIER 10
+#define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
+
+/* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'
+ * Returns 1 in case of success, 0 otherwise. */
+static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
+ tmsize_t rawdata_offset,
+ int is_strip, uint32 strip_or_tile,
+ const char* module )
+{
+#if SIZEOF_SIZE_T == 8
+ tmsize_t threshold = INITIAL_THRESHOLD;
+#endif
+ tmsize_t already_read = 0;
+
+ /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
+ /* so as to avoid allocating too much memory in case the file is too */
+ /* short. We could ask for the file size, but this might be */
+ /* expensive with some I/O layers (think of reading a gzipped file) */
+ /* Restrict to 64 bit processes, so as to avoid reallocs() */
+ /* on 32 bit processes where virtual memory is scarce. */
+ while( already_read < size )
+ {
+ tmsize_t bytes_read;
+ tmsize_t to_read = size - already_read;
+#if SIZEOF_SIZE_T == 8
+ if( to_read >= threshold && threshold < MAX_THRESHOLD &&
+ already_read + to_read + rawdata_offset > tif->tif_rawdatasize )
+ {
+ to_read = threshold;
+ threshold *= THRESHOLD_MULTIPLIER;
+ }
+#endif
+ if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize) {
+ uint8* new_rawdata;
+ assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
+ tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
+ (uint64)already_read + to_read + rawdata_offset, 1024);
+ if (tif->tif_rawdatasize==0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalid buffer size");
+ return 0;
+ }
+ new_rawdata = (uint8*) _TIFFrealloc(
+ tif->tif_rawdata, tif->tif_rawdatasize);
+ if( new_rawdata == 0 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for data buffer at scanline %lu",
+ (unsigned long) tif->tif_row);
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = 0;
+ tif->tif_rawdatasize = 0;
+ return 0;
+ }
+ tif->tif_rawdata = new_rawdata;
+ }
+
+ bytes_read = TIFFReadFile(tif,
+ tif->tif_rawdata + rawdata_offset + already_read, to_read);
+ already_read += bytes_read;
+ if (bytes_read != to_read) {
+ memset( tif->tif_rawdata + rawdata_offset + already_read, 0,
+ tif->tif_rawdatasize - rawdata_offset - already_read );
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ if( is_strip )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu; got %I64u bytes, "
+ "expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) already_read,
+ (unsigned __int64) size);
+ }
+ else
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at row %lu, col %lu, tile %lu; "
+ "got %I64u bytes, expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) strip_or_tile,
+ (unsigned __int64) already_read,
+ (unsigned __int64) size);
+ }
+#else
+ if( is_strip )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu; got %llu bytes, "
+ "expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) already_read,
+ (unsigned long long) size);
+ }
+ else
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at row %lu, col %lu, tile %lu; "
+ "got %llu bytes, expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) strip_or_tile,
+ (unsigned long long) already_read,
+ (unsigned long long) size);
+ }
+#endif
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
+static int
+TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
+{
+ static const char module[] = "TIFFFillStripPartial";
+ register TIFFDirectory *td = &tif->tif_dir;
+ tmsize_t unused_data;
+ uint64 read_offset;
+ tmsize_t to_read;
+ tmsize_t read_ahead_mod;
+ /* tmsize_t bytecountm; */
+
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+
+ /*
+ * Expand raw data buffer, if needed, to hold data
+ * strip coming from file (perhaps should set upper
+ * bound on the size of a buffer we'll use?).
+ */
+
+ /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
+
+ /* Not completely sure where the * 2 comes from, but probably for */
+ /* an exponentional growth strategy of tif_rawdatasize */
+ if( read_ahead < TIFF_TMSIZE_T_MAX / 2 )
+ read_ahead_mod = read_ahead * 2;
+ else
+ read_ahead_mod = read_ahead;
+ if (read_ahead_mod > tif->tif_rawdatasize) {
+ assert( restart );
+
+ tif->tif_curstrip = NOSTRIP;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Data buffer too small to hold part of strip %lu",
+ (unsigned long) strip);
+ return (0);
+ }
+ }
+
+ if( restart )
+ {
+ tif->tif_rawdataloaded = 0;
+ tif->tif_rawdataoff = 0;
+ }
+
+ /*
+ ** If we are reading more data, move any unused data to the
+ ** start of the buffer.
+ */
+ if( tif->tif_rawdataloaded > 0 )
+ unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
+ else
+ unused_data = 0;
+
+ if( unused_data > 0 )
+ {
+ assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
+ memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
+ }
+
+ /*
+ ** Seek to the point in the file where more data should be read.
+ */
+ read_offset = td->td_stripoffset[strip]
+ + tif->tif_rawdataoff + tif->tif_rawdataloaded;
+
+ if (!SeekOK(tif, read_offset)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at scanline %lu, strip %lu",
+ (unsigned long) tif->tif_row, (unsigned long) strip);
+ return 0;
+ }
+
+ /*
+ ** How much do we want to read?
+ */
+ if( read_ahead_mod > tif->tif_rawdatasize )
+ to_read = read_ahead_mod - unused_data;
+ else
+ to_read = tif->tif_rawdatasize - unused_data;
+ if( (uint64) to_read > td->td_stripbytecount[strip]
+ - tif->tif_rawdataoff - tif->tif_rawdataloaded )
+ {
+ to_read = (tmsize_t) td->td_stripbytecount[strip]
+ - tif->tif_rawdataoff - tif->tif_rawdataloaded;
+ }
+
+ assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
+ if( !TIFFReadAndRealloc( tif, to_read, unused_data,
+ 1, /* is_strip */
+ 0, /* strip_or_tile */
+ module) )
+ {
+ return 0;
+ }
+
+ tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
+ tif->tif_rawdataloaded = unused_data + to_read;
+
+ tif->tif_rawcc = tif->tif_rawdataloaded;
+ tif->tif_rawcp = tif->tif_rawdata;
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0) {
+ assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
+ TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
+ }
+
+ /*
+ ** When starting a strip from the beginning we need to
+ ** restart the decoder.
+ */
+ if( restart )
+ {
+
+#ifdef JPEG_SUPPORT
+ /* A bit messy since breaks the codec abstraction. Ultimately */
+ /* there should be a function pointer for that, but it seems */
+ /* only JPEG is affected. */
+ /* For JPEG, if there are multiple scans (can generally be known */
+ /* with the read_ahead used), we need to read the whole strip */
+ if( tif->tif_dir.td_compression==COMPRESSION_JPEG &&
+ (uint64)tif->tif_rawcc < td->td_stripbytecount[strip] )
+ {
+ if( TIFFJPEGIsFullStripRequired(tif) )
+ {
+ return TIFFFillStrip(tif, strip);
+ }
+ }
+#endif
+
+ return TIFFStartStrip(tif, strip);
+ }
+ else
+ {
+ return 1;
+ }
+}
+
+/*
+ * Seek to a random row+sample in a file.
+ *
+ * Only used by TIFFReadScanline, and is only used on
+ * strip organized files. We do some tricky stuff to try
+ * and avoid reading the whole compressed raw data for big
+ * strips.
+ */
+static int
+TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
+{
+ register TIFFDirectory *td = &tif->tif_dir;
+ uint32 strip;
+ int whole_strip;
+ tmsize_t read_ahead = 0;
+
+ /*
+ ** Establish what strip we are working from.
+ */
+ if (row >= td->td_imagelength) { /* out of range */
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Row out of range, max %lu",
+ (unsigned long) row,
+ (unsigned long) td->td_imagelength);
+ return (0);
+ }
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ if (sample >= td->td_samplesperpixel) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Sample out of range, max %lu",
+ (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
+ return (0);
+ }
+ strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
+ } else
+ strip = row / td->td_rowsperstrip;
+
+ /*
+ * Do we want to treat this strip as one whole chunk or
+ * read it a few lines at a time?
+ */
+#if defined(CHUNKY_STRIP_READ_SUPPORT)
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+ whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
+ || isMapped(tif);
+ if( td->td_compression == COMPRESSION_LERC ||
+ td->td_compression == COMPRESSION_JBIG )
+ {
+ /* Ideally plugins should have a way to declare they don't support
+ * chunk strip */
+ whole_strip = 1;
+ }
+#else
+ whole_strip = 1;
+#endif
+
+ if( !whole_strip )
+ {
+ /* 16 is for YCbCr mode where we may need to read 16 */
+ /* lines at a time to get a decompressed line, and 5000 */
+ /* is some constant value, for example for JPEG tables */
+ if( tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
+ tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000 )
+ {
+ read_ahead = tif->tif_scanlinesize * 16 + 5000;
+ }
+ else
+ {
+ read_ahead = tif->tif_scanlinesize;
+ }
+ }
+
+ /*
+ * If we haven't loaded this strip, do so now, possibly
+ * only reading the first part.
+ */
+ if (strip != tif->tif_curstrip) { /* different strip, refill */
+
+ if( whole_strip )
+ {
+ if (!TIFFFillStrip(tif, strip))
+ return (0);
+ }
+ else
+ {
+ if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
+ return 0;
+ }
+ }
+
+ /*
+ ** If we already have some data loaded, do we need to read some more?
+ */
+ else if( !whole_strip )
+ {
+ if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead
+ && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
+ {
+ if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
+ return 0;
+ }
+ }
+
+ if (row < tif->tif_row) {
+ /*
+ * Moving backwards within the same strip: backup
+ * to the start and then decode forward (below).
+ *
+ * NB: If you're planning on lots of random access within a
+ * strip, it's better to just read and decode the entire
+ * strip, and then access the decoded data in a random fashion.
+ */
+
+ if( tif->tif_rawdataoff != 0 )
+ {
+ if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
+ return 0;
+ }
+ else
+ {
+ if (!TIFFStartStrip(tif, strip))
+ return (0);
+ }
+ }
+
+ if (row != tif->tif_row) {
+ /*
+ * Seek forward to the desired row.
+ */
+
+ /* TODO: Will this really work with partial buffers? */
+
+ if (!(*tif->tif_seek)(tif, row - tif->tif_row))
+ return (0);
+ tif->tif_row = row;
+ }
+
+ return (1);
+}
+
+int
+TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
+{
+ int e;
+
+ if (!TIFFCheckRead(tif, 0))
+ return (-1);
+ if( (e = TIFFSeek(tif, row, sample)) != 0) {
+ /*
+ * Decompress desired row into user buffer.
+ */
+ e = (*tif->tif_decoderow)
+ (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
+
+ /* we are now poised at the beginning of the next row */
+ tif->tif_row = row + 1;
+
+ if (e)
+ (*tif->tif_postdecode)(tif, (uint8*) buf,
+ tif->tif_scanlinesize);
+ }
+ return (e > 0 ? 1 : -1);
+}
+
+/*
+ * Calculate the strip size according to the number of
+ * rows in the strip (check for truncated last strip on any
+ * of the separations).
+ */
+static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF* tif, uint32 strip, uint16* pplane)
+{
+ static const char module[] = "TIFFReadEncodedStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 rowsperstrip;
+ uint32 stripsperplane;
+ uint32 stripinplane;
+ uint32 rows;
+ tmsize_t stripsize;
+ if (!TIFFCheckRead(tif,0))
+ return((tmsize_t)(-1));
+ if (strip>=td->td_nstrips)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "%lu: Strip out of range, max %lu",(unsigned long)strip,
+ (unsigned long)td->td_nstrips);
+ return((tmsize_t)(-1));
+ }
+
+ rowsperstrip=td->td_rowsperstrip;
+ if (rowsperstrip>td->td_imagelength)
+ rowsperstrip=td->td_imagelength;
+ stripsperplane= TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
+ stripinplane=(strip%stripsperplane);
+ if( pplane ) *pplane=(uint16)(strip/stripsperplane);
+ rows=td->td_imagelength-stripinplane*rowsperstrip;
+ if (rows>rowsperstrip)
+ rows=rowsperstrip;
+ stripsize=TIFFVStripSize(tif,rows);
+ if (stripsize==0)
+ return((tmsize_t)(-1));
+ return stripsize;
+}
+
+/*
+ * Read a strip of data and decompress the specified
+ * amount into the user-supplied buffer.
+ */
+tmsize_t
+TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
+{
+ static const char module[] = "TIFFReadEncodedStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ tmsize_t stripsize;
+ uint16 plane;
+
+ stripsize=TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
+ if (stripsize==((tmsize_t)(-1)))
+ return((tmsize_t)(-1));
+
+ /* shortcut to avoid an extra memcpy() */
+ if( td->td_compression == COMPRESSION_NONE &&
+ size!=(tmsize_t)(-1) && size >= stripsize &&
+ !isMapped(tif) &&
+ ((tif->tif_flags&TIFF_NOREADRAW)==0) )
+ {
+ if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
+ return ((tmsize_t)(-1));
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(buf,stripsize);
+
+ (*tif->tif_postdecode)(tif,buf,stripsize);
+ return (stripsize);
+ }
+
+ if ((size!=(tmsize_t)(-1))&&(size<stripsize))
+ stripsize=size;
+ if (!TIFFFillStrip(tif,strip))
+ return((tmsize_t)(-1));
+ if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
+ return((tmsize_t)(-1));
+ (*tif->tif_postdecode)(tif,buf,stripsize);
+ return(stripsize);
+}
+
+/* Variant of TIFFReadEncodedStrip() that does
+ * * if *buf == NULL, *buf = _TIFFmalloc(bufsizetoalloc) only after TIFFFillStrip() has
+ * succeeded. This avoid excessive memory allocation in case of truncated
+ * file.
+ * * calls regular TIFFReadEncodedStrip() if *buf != NULL
+ */
+tmsize_t
+_TIFFReadEncodedStripAndAllocBuffer(TIFF* tif, uint32 strip,
+ void **buf, tmsize_t bufsizetoalloc,
+ tmsize_t size_to_read)
+{
+ tmsize_t this_stripsize;
+ uint16 plane;
+
+ if( *buf != NULL )
+ {
+ return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
+ }
+
+ this_stripsize=TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
+ if (this_stripsize==((tmsize_t)(-1)))
+ return((tmsize_t)(-1));
+
+ if ((size_to_read!=(tmsize_t)(-1))&&(size_to_read<this_stripsize))
+ this_stripsize=size_to_read;
+ if (!TIFFFillStrip(tif,strip))
+ return((tmsize_t)(-1));
+
+ *buf = _TIFFmalloc(bufsizetoalloc);
+ if (*buf == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
+ return((tmsize_t)(-1));
+ }
+ _TIFFmemset(*buf, 0, bufsizetoalloc);
+
+ if ((*tif->tif_decodestrip)(tif,*buf,this_stripsize,plane)<=0)
+ return((tmsize_t)(-1));
+ (*tif->tif_postdecode)(tif,*buf,this_stripsize);
+ return(this_stripsize);
+
+
+}
+
+static tmsize_t
+TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
+ const char* module)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!_TIFFFillStriles( tif ))
+ return ((tmsize_t)(-1));
+
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
+ if (!isMapped(tif)) {
+ tmsize_t cc;
+
+ if (!SeekOK(tif, td->td_stripoffset[strip])) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at scanline %lu, strip %lu",
+ (unsigned long) tif->tif_row, (unsigned long) strip);
+ return ((tmsize_t)(-1));
+ }
+ cc = TIFFReadFile(tif, buf, size);
+ if (cc != size) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu; got %I64u bytes, expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) cc,
+ (unsigned __int64) size);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu; got %llu bytes, expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) cc,
+ (unsigned long long) size);
+#endif
+ return ((tmsize_t)(-1));
+ }
+ } else {
+ tmsize_t ma = 0;
+ tmsize_t n;
+ if ((td->td_stripoffset[strip] > (uint64)TIFF_TMSIZE_T_MAX)||
+ ((ma=(tmsize_t)td->td_stripoffset[strip])>tif->tif_size))
+ {
+ n=0;
+ }
+ else if( ma > TIFF_TMSIZE_T_MAX - size )
+ {
+ n=0;
+ }
+ else
+ {
+ tmsize_t mb=ma+size;
+ if (mb>tif->tif_size)
+ n=tif->tif_size-ma;
+ else
+ n=size;
+ }
+ if (n!=size) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned long) strip,
+ (unsigned __int64) n,
+ (unsigned __int64) size);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) strip,
+ (unsigned long long) n,
+ (unsigned long long) size);
+#endif
+ return ((tmsize_t)(-1));
+ }
+ _TIFFmemcpy(buf, tif->tif_base + ma,
+ size);
+ }
+ return (size);
+}
+
+static tmsize_t
+TIFFReadRawStripOrTile2(TIFF* tif, uint32 strip_or_tile, int is_strip,
+ tmsize_t size, const char* module)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ assert( !isMapped(tif) );
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
+
+ if (!SeekOK(tif, td->td_stripoffset[strip_or_tile])) {
+ if( is_strip )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at scanline %lu, strip %lu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) strip_or_tile);
+ }
+ else
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at row %lu, col %lu, tile %lu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) strip_or_tile);
+ }
+ return ((tmsize_t)(-1));
+ }
+
+ if( !TIFFReadAndRealloc( tif, size, 0, is_strip,
+ strip_or_tile, module ) )
+ {
+ return ((tmsize_t)(-1));
+ }
+
+ return (size);
+}
+
+/*
+ * Read a strip of data from the file.
+ */
+tmsize_t
+TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
+{
+ static const char module[] = "TIFFReadRawStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 bytecount;
+ tmsize_t bytecountm;
+
+ if (!TIFFCheckRead(tif, 0))
+ return ((tmsize_t)(-1));
+ if (strip >= td->td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Strip out of range, max %lu",
+ (unsigned long) strip,
+ (unsigned long) td->td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Compression scheme does not support access to raw uncompressed data");
+ return ((tmsize_t)(-1));
+ }
+ bytecount = td->td_stripbytecount[strip];
+ if ((int64)bytecount <= 0) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%I64u: Invalid strip byte count, strip %lu",
+ (unsigned __int64) bytecount,
+ (unsigned long) strip);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%llu: Invalid strip byte count, strip %lu",
+ (unsigned long long) bytecount,
+ (unsigned long) strip);
+#endif
+ return ((tmsize_t)(-1));
+ }
+ bytecountm = (tmsize_t)bytecount;
+ if ((uint64)bytecountm!=bytecount) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
+ return ((tmsize_t)(-1));
+ }
+ if (size != (tmsize_t)(-1) && size < bytecountm)
+ bytecountm = size;
+ return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
+}
+
+/*
+ * Read the specified strip and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the strip's data.
+ */
+int
+TIFFFillStrip(TIFF* tif, uint32 strip)
+{
+ static const char module[] = "TIFFFillStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+
+ if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+ {
+ uint64 bytecount = td->td_stripbytecount[strip];
+ if ((int64)bytecount <= 0) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalid strip byte count %I64u, strip %lu",
+ (unsigned __int64) bytecount,
+ (unsigned long) strip);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalid strip byte count %llu, strip %lu",
+ (unsigned long long) bytecount,
+ (unsigned long) strip);
+#endif
+ return (0);
+ }
+
+ /* To avoid excessive memory allocations: */
+ /* Byte count should normally not be larger than a number of */
+ /* times the uncompressed size plus some margin */
+ if( bytecount > 1024 * 1024 )
+ {
+ /* 10 and 4096 are just values that could be adjusted. */
+ /* Hopefully they are safe enough for all codecs */
+ tmsize_t stripsize = TIFFStripSize(tif);
+ if( stripsize != 0 &&
+ (bytecount - 4096) / 10 > (uint64)stripsize )
+ {
+ uint64 newbytecount = (uint64)stripsize * 10 + 4096;
+ if( (int64)newbytecount >= 0 )
+ {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Too large strip byte count %I64u, strip %lu. Limiting to %I64u",
+ (unsigned __int64) bytecount,
+ (unsigned long) strip,
+ (unsigned __int64) newbytecount);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Too large strip byte count %llu, strip %lu. Limiting to %llu",
+ (unsigned long long) bytecount,
+ (unsigned long) strip,
+ (unsigned long long) newbytecount);
+#endif
+ bytecount = newbytecount;
+ }
+ }
+ }
+
+ if (isMapped(tif)) {
+ /*
+ * We must check for overflow, potentially causing
+ * an OOB read. Instead of simple
+ *
+ * td->td_stripoffset[strip]+bytecount > tif->tif_size
+ *
+ * comparison (which can overflow) we do the following
+ * two comparisons:
+ */
+ if (bytecount > (uint64)tif->tif_size ||
+ td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
+ /*
+ * This error message might seem strange, but
+ * it's what would happen if a read were done
+ * instead.
+ */
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+
+ "Read error on strip %lu; "
+ "got %I64u bytes, expected %I64u",
+ (unsigned long) strip,
+ (unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
+ (unsigned __int64) bytecount);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+
+ "Read error on strip %lu; "
+ "got %llu bytes, expected %llu",
+ (unsigned long) strip,
+ (unsigned long long) tif->tif_size - td->td_stripoffset[strip],
+ (unsigned long long) bytecount);
+#endif
+ tif->tif_curstrip = NOSTRIP;
+ return (0);
+ }
+ }
+
+ if (isMapped(tif) &&
+ (isFillOrder(tif, td->td_fillorder)
+ || (tif->tif_flags & TIFF_NOBITREV))) {
+ /*
+ * The image is mapped into memory and we either don't
+ * need to flip bits or the compression routine is
+ * going to handle this operation itself. In this
+ * case, avoid copying the raw data and instead just
+ * reference the data from the memory mapped file
+ * image. This assumes that the decompression
+ * routines do not modify the contents of the raw data
+ * buffer (if they try to, the application will get a
+ * fault since the file is mapped read-only).
+ */
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawdatasize = 0;
+ }
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ tif->tif_rawdatasize = (tmsize_t)bytecount;
+ tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = (tmsize_t) bytecount;
+
+ /*
+ * When we have tif_rawdata reference directly into the memory mapped file
+ * we need to be pretty careful about how we use the rawdata. It is not
+ * a general purpose working buffer as it normally otherwise is. So we
+ * keep track of this fact to avoid using it improperly.
+ */
+ tif->tif_flags |= TIFF_BUFFERMMAP;
+ } else {
+ /*
+ * Expand raw data buffer, if needed, to hold data
+ * strip coming from file (perhaps should set upper
+ * bound on the size of a buffer we'll use?).
+ */
+ tmsize_t bytecountm;
+ bytecountm=(tmsize_t)bytecount;
+ if ((uint64)bytecountm!=bytecount)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ return(0);
+ }
+ if (bytecountm > tif->tif_rawdatasize) {
+ tif->tif_curstrip = NOSTRIP;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Data buffer too small to hold strip %lu",
+ (unsigned long) strip);
+ return (0);
+ }
+ }
+ if (tif->tif_flags&TIFF_BUFFERMMAP) {
+ tif->tif_curstrip = NOSTRIP;
+ tif->tif_rawdata = NULL;
+ tif->tif_rawdatasize = 0;
+ tif->tif_flags &= ~TIFF_BUFFERMMAP;
+ }
+
+ if( isMapped(tif) )
+ {
+ if (bytecountm > tif->tif_rawdatasize &&
+ !TIFFReadBufferSetup(tif, 0, bytecountm))
+ {
+ return (0);
+ }
+ if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
+ bytecountm, module) != bytecountm)
+ {
+ return (0);
+ }
+ }
+ else
+ {
+ if (TIFFReadRawStripOrTile2(tif, strip, 1,
+ bytecountm, module) != bytecountm)
+ {
+ return (0);
+ }
+ }
+
+
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = bytecountm;
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(tif->tif_rawdata, bytecountm);
+ }
+ }
+ return (TIFFStartStrip(tif, strip));
+}
+
+/*
+ * Tile-oriented Read Support
+ * Contributed by Nancy Cam (Silicon Graphics).
+ */
+
+/*
+ * Read and decompress a tile of data. The
+ * tile is selected by the (x,y,z,s) coordinates.
+ */
+tmsize_t
+TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
+{
+ if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
+ return ((tmsize_t)(-1));
+ return (TIFFReadEncodedTile(tif,
+ TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
+}
+
+/*
+ * Read a tile of data and decompress the specified
+ * amount into the user-supplied buffer.
+ */
+tmsize_t
+TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
+{
+ static const char module[] = "TIFFReadEncodedTile";
+ TIFFDirectory *td = &tif->tif_dir;
+ tmsize_t tilesize = tif->tif_tilesize;
+
+ if (!TIFFCheckRead(tif, 1))
+ return ((tmsize_t)(-1));
+ if (tile >= td->td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Tile out of range, max %lu",
+ (unsigned long) tile, (unsigned long) td->td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+
+ /* shortcut to avoid an extra memcpy() */
+ if( td->td_compression == COMPRESSION_NONE &&
+ size!=(tmsize_t)(-1) && size >= tilesize &&
+ !isMapped(tif) &&
+ ((tif->tif_flags&TIFF_NOREADRAW)==0) )
+ {
+ if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
+ return ((tmsize_t)(-1));
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(buf,tilesize);
+
+ (*tif->tif_postdecode)(tif,buf,tilesize);
+ return (tilesize);
+ }
+
+ if (size == (tmsize_t)(-1))
+ size = tilesize;
+ else if (size > tilesize)
+ size = tilesize;
+ if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
+ (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
+ (*tif->tif_postdecode)(tif, (uint8*) buf, size);
+ return (size);
+ } else
+ return ((tmsize_t)(-1));
+}
+
+/* Variant of TIFFReadTile() that does
+ * * if *buf == NULL, *buf = _TIFFmalloc(bufsizetoalloc) only after TIFFFillTile() has
+ * succeeded. This avoid excessive memory allocation in case of truncated
+ * file.
+ * * calls regular TIFFReadEncodedTile() if *buf != NULL
+ */
+tmsize_t
+_TIFFReadTileAndAllocBuffer(TIFF* tif,
+ void **buf, tmsize_t bufsizetoalloc,
+ uint32 x, uint32 y, uint32 z, uint16 s)
+{
+ if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
+ return ((tmsize_t)(-1));
+ return (_TIFFReadEncodedTileAndAllocBuffer(tif,
+ TIFFComputeTile(tif, x, y, z, s),
+ buf, bufsizetoalloc,
+ (tmsize_t)(-1)));
+}
+
+/* Variant of TIFFReadEncodedTile() that does
+ * * if *buf == NULL, *buf = _TIFFmalloc(bufsizetoalloc) only after TIFFFillTile() has
+ * succeeded. This avoid excessive memory allocation in case of truncated
+ * file.
+ * * calls regular TIFFReadEncodedTile() if *buf != NULL
+ */
+tmsize_t
+_TIFFReadEncodedTileAndAllocBuffer(TIFF* tif, uint32 tile,
+ void **buf, tmsize_t bufsizetoalloc,
+ tmsize_t size_to_read)
+{
+ static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
+ TIFFDirectory *td = &tif->tif_dir;
+ tmsize_t tilesize = tif->tif_tilesize;
+
+ if( *buf != NULL )
+ {
+ return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
+ }
+
+ if (!TIFFCheckRead(tif, 1))
+ return ((tmsize_t)(-1));
+ if (tile >= td->td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Tile out of range, max %lu",
+ (unsigned long) tile, (unsigned long) td->td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+
+ if (!TIFFFillTile(tif,tile))
+ return((tmsize_t)(-1));
+
+ *buf = _TIFFmalloc(bufsizetoalloc);
+ if (*buf == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+ "No space for tile buffer");
+ return((tmsize_t)(-1));
+ }
+ _TIFFmemset(*buf, 0, bufsizetoalloc);
+
+ if (size_to_read == (tmsize_t)(-1))
+ size_to_read = tilesize;
+ else if (size_to_read > tilesize)
+ size_to_read = tilesize;
+ if( (*tif->tif_decodetile)(tif,
+ (uint8*) *buf, size_to_read, (uint16)(tile/td->td_stripsperimage))) {
+ (*tif->tif_postdecode)(tif, (uint8*) *buf, size_to_read);
+ return (size_to_read);
+ } else
+ return ((tmsize_t)(-1));
+}
+
+static tmsize_t
+TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!_TIFFFillStriles( tif ))
+ return ((tmsize_t)(-1));
+
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
+ if (!isMapped(tif)) {
+ tmsize_t cc;
+
+ if (!SeekOK(tif, td->td_stripoffset[tile])) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at row %lu, col %lu, tile %lu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) tile);
+ return ((tmsize_t)(-1));
+ }
+ cc = TIFFReadFile(tif, buf, size);
+ if (cc != size) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned __int64) cc,
+ (unsigned __int64) size);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long long) cc,
+ (unsigned long long) size);
+#endif
+ return ((tmsize_t)(-1));
+ }
+ } else {
+ tmsize_t ma,mb;
+ tmsize_t n;
+ ma=(tmsize_t)td->td_stripoffset[tile];
+ mb=ma+size;
+ if ((td->td_stripoffset[tile] > (uint64)TIFF_TMSIZE_T_MAX)||(ma>tif->tif_size))
+ n=0;
+ else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
+ n=tif->tif_size-ma;
+ else
+ n=size;
+ if (n!=size) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+"Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) tile,
+ (unsigned __int64) n,
+ (unsigned __int64) size);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+"Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
+ (unsigned long) tif->tif_row,
+ (unsigned long) tif->tif_col,
+ (unsigned long) tile,
+ (unsigned long long) n,
+ (unsigned long long) size);
+#endif
+ return ((tmsize_t)(-1));
+ }
+ _TIFFmemcpy(buf, tif->tif_base + ma, size);
+ }
+ return (size);
+}
+
+/*
+ * Read a tile of data from the file.
+ */
+tmsize_t
+TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
+{
+ static const char module[] = "TIFFReadRawTile";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 bytecount64;
+ tmsize_t bytecountm;
+
+ if (!TIFFCheckRead(tif, 1))
+ return ((tmsize_t)(-1));
+ if (tile >= td->td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Tile out of range, max %lu",
+ (unsigned long) tile, (unsigned long) td->td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Compression scheme does not support access to raw uncompressed data");
+ return ((tmsize_t)(-1));
+ }
+ bytecount64 = td->td_stripbytecount[tile];
+ if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
+ bytecount64 = (uint64)size;
+ bytecountm = (tmsize_t)bytecount64;
+ if ((uint64)bytecountm!=bytecount64)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ return ((tmsize_t)(-1));
+ }
+ return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
+}
+
+/*
+ * Read the specified tile and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the tile's data.
+ */
+int
+TIFFFillTile(TIFF* tif, uint32 tile)
+{
+ static const char module[] = "TIFFFillTile";
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+
+ if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+ {
+ uint64 bytecount = td->td_stripbytecount[tile];
+ if ((int64)bytecount <= 0) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%I64u: Invalid tile byte count, tile %lu",
+ (unsigned __int64) bytecount,
+ (unsigned long) tile);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%llu: Invalid tile byte count, tile %lu",
+ (unsigned long long) bytecount,
+ (unsigned long) tile);
+#endif
+ return (0);
+ }
+
+ /* To avoid excessive memory allocations: */
+ /* Byte count should normally not be larger than a number of */
+ /* times the uncompressed size plus some margin */
+ if( bytecount > 1024 * 1024 )
+ {
+ /* 10 and 4096 are just values that could be adjusted. */
+ /* Hopefully they are safe enough for all codecs */
+ tmsize_t stripsize = TIFFTileSize(tif);
+ if( stripsize != 0 &&
+ (bytecount - 4096) / 10 > (uint64)stripsize )
+ {
+ uint64 newbytecount = (uint64)stripsize * 10 + 4096;
+ if( (int64)newbytecount >= 0 )
+ {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Too large tile byte count %I64u, tile %lu. Limiting to %I64u",
+ (unsigned __int64) bytecount,
+ (unsigned long) tile,
+ (unsigned __int64) newbytecount);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Too large tile byte count %llu, tile %lu. Limiting to %llu",
+ (unsigned long long) bytecount,
+ (unsigned long) tile,
+ (unsigned long long) newbytecount);
+#endif
+ bytecount = newbytecount;
+ }
+ }
+ }
+
+ if (isMapped(tif)) {
+ /*
+ * We must check for overflow, potentially causing
+ * an OOB read. Instead of simple
+ *
+ * td->td_stripoffset[tile]+bytecount > tif->tif_size
+ *
+ * comparison (which can overflow) we do the following
+ * two comparisons:
+ */
+ if (bytecount > (uint64)tif->tif_size ||
+ td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
+ tif->tif_curtile = NOTILE;
+ return (0);
+ }
+ }
+
+ if (isMapped(tif) &&
+ (isFillOrder(tif, td->td_fillorder)
+ || (tif->tif_flags & TIFF_NOBITREV))) {
+ /*
+ * The image is mapped into memory and we either don't
+ * need to flip bits or the compression routine is
+ * going to handle this operation itself. In this
+ * case, avoid copying the raw data and instead just
+ * reference the data from the memory mapped file
+ * image. This assumes that the decompression
+ * routines do not modify the contents of the raw data
+ * buffer (if they try to, the application will get a
+ * fault since the file is mapped read-only).
+ */
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawdatasize = 0;
+ }
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+
+ tif->tif_rawdatasize = (tmsize_t)bytecount;
+ tif->tif_rawdata =
+ tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = (tmsize_t) bytecount;
+ tif->tif_flags |= TIFF_BUFFERMMAP;
+ } else {
+ /*
+ * Expand raw data buffer, if needed, to hold data
+ * tile coming from file (perhaps should set upper
+ * bound on the size of a buffer we'll use?).
+ */
+ tmsize_t bytecountm;
+ bytecountm=(tmsize_t)bytecount;
+ if ((uint64)bytecountm!=bytecount)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ return(0);
+ }
+ if (bytecountm > tif->tif_rawdatasize) {
+ tif->tif_curtile = NOTILE;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Data buffer too small to hold tile %lu",
+ (unsigned long) tile);
+ return (0);
+ }
+ }
+ if (tif->tif_flags&TIFF_BUFFERMMAP) {
+ tif->tif_curtile = NOTILE;
+ tif->tif_rawdata = NULL;
+ tif->tif_rawdatasize = 0;
+ tif->tif_flags &= ~TIFF_BUFFERMMAP;
+ }
+
+ if( isMapped(tif) )
+ {
+ if (bytecountm > tif->tif_rawdatasize &&
+ !TIFFReadBufferSetup(tif, 0, bytecountm))
+ {
+ return (0);
+ }
+ if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
+ bytecountm, module) != bytecountm)
+ {
+ return (0);
+ }
+ }
+ else
+ {
+ if (TIFFReadRawStripOrTile2(tif, tile, 0,
+ bytecountm, module) != bytecountm)
+ {
+ return (0);
+ }
+ }
+
+
+ tif->tif_rawdataoff = 0;
+ tif->tif_rawdataloaded = bytecountm;
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(tif->tif_rawdata,
+ tif->tif_rawdataloaded);
+ }
+ }
+ return (TIFFStartTile(tif, tile));
+}
+
+/*
+ * Setup the raw data buffer in preparation for
+ * reading a strip of raw data. If the buffer
+ * is specified as zero, then a buffer of appropriate
+ * size is allocated by the library. Otherwise,
+ * the client must guarantee that the buffer is
+ * large enough to hold any individual strip of
+ * raw data.
+ */
+int
+TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
+{
+ static const char module[] = "TIFFReadBufferSetup";
+
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
+ tif->tif_flags &= ~TIFF_BUFFERMMAP;
+
+ if (tif->tif_rawdata) {
+ if (tif->tif_flags & TIFF_MYBUFFER)
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawdatasize = 0;
+ }
+ if (bp) {
+ tif->tif_rawdatasize = size;
+ tif->tif_rawdata = (uint8*) bp;
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ } else {
+ tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
+ if (tif->tif_rawdatasize==0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalid buffer size");
+ return (0);
+ }
+ /* Initialize to zero to avoid uninitialized buffers in case of */
+ /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
+ tif->tif_rawdata = (uint8*) _TIFFcalloc(1, tif->tif_rawdatasize);
+ tif->tif_flags |= TIFF_MYBUFFER;
+ }
+ if (tif->tif_rawdata == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for data buffer at scanline %lu",
+ (unsigned long) tif->tif_row);
+ tif->tif_rawdatasize = 0;
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * Set state to appear as if a
+ * strip has just been read in.
+ */
+static int
+TIFFStartStrip(TIFF* tif, uint32 strip)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+
+ if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
+ if (!(*tif->tif_setupdecode)(tif))
+ return (0);
+ tif->tif_flags |= TIFF_CODERSETUP;
+ }
+ tif->tif_curstrip = strip;
+ tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
+ tif->tif_flags &= ~TIFF_BUF4WRITE;
+
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ tif->tif_rawcp = NULL;
+ tif->tif_rawcc = 0;
+ }
+ else
+ {
+ tif->tif_rawcp = tif->tif_rawdata;
+ if( tif->tif_rawdataloaded > 0 )
+ tif->tif_rawcc = tif->tif_rawdataloaded;
+ else
+ tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
+ }
+ return ((*tif->tif_predecode)(tif,
+ (uint16)(strip / td->td_stripsperimage)));
+}
+
+/*
+ * Set state to appear as if a
+ * tile has just been read in.
+ */
+static int
+TIFFStartTile(TIFF* tif, uint32 tile)
+{
+ static const char module[] = "TIFFStartTile";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 howmany32;
+
+ if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
+ return 0;
+
+ if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
+ if (!(*tif->tif_setupdecode)(tif))
+ return (0);
+ tif->tif_flags |= TIFF_CODERSETUP;
+ }
+ tif->tif_curtile = tile;
+ howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
+ if (howmany32 == 0) {
+ TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
+ return 0;
+ }
+ tif->tif_row = (tile % howmany32) * td->td_tilelength;
+ howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
+ if (howmany32 == 0) {
+ TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
+ return 0;
+ }
+ tif->tif_col = (tile % howmany32) * td->td_tilewidth;
+ tif->tif_flags &= ~TIFF_BUF4WRITE;
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ tif->tif_rawcp = NULL;
+ tif->tif_rawcc = 0;
+ }
+ else
+ {
+ tif->tif_rawcp = tif->tif_rawdata;
+ if( tif->tif_rawdataloaded > 0 )
+ tif->tif_rawcc = tif->tif_rawdataloaded;
+ else
+ tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
+ }
+ return ((*tif->tif_predecode)(tif,
+ (uint16)(tile/td->td_stripsperimage)));
+}
+
+static int
+TIFFCheckRead(TIFF* tif, int tiles)
+{
+ if (tif->tif_mode == O_WRONLY) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
+ return (0);
+ }
+ if (tiles ^ isTiled(tif)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
+ "Can not read tiles from a stripped image" :
+ "Can not read scanlines from a tiled image");
+ return (0);
+ }
+ return (1);
+}
+
+void
+_TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ (void) tif; (void) buf; (void) cc;
+}
+
+void
+_TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ (void) tif;
+ assert((cc & 1) == 0);
+ TIFFSwabArrayOfShort((uint16*) buf, cc/2);
+}
+
+void
+_TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ (void) tif;
+ assert((cc % 3) == 0);
+ TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
+}
+
+void
+_TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ (void) tif;
+ assert((cc & 3) == 0);
+ TIFFSwabArrayOfLong((uint32*) buf, cc/4);
+}
+
+void
+_TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
+{
+ (void) tif;
+ assert((cc & 7) == 0);
+ TIFFSwabArrayOfDouble((double*) buf, cc/8);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_strip.c b/test/monniaux/tiff-4.0.10/tif_strip.c
new file mode 100644
index 00000000..5b76fba5
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_strip.c
@@ -0,0 +1,387 @@
+/*
+ * Copyright (c) 1991-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Strip-organized Image Support Routines.
+ */
+#include "tiffiop.h"
+
+/*
+ * Compute which strip a (row,sample) value is in.
+ */
+uint32
+TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
+{
+ static const char module[] = "TIFFComputeStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 strip;
+
+ strip = row / td->td_rowsperstrip;
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ if (sample >= td->td_samplesperpixel) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Sample out of range, max %lu",
+ (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
+ return (0);
+ }
+ strip += (uint32)sample*td->td_stripsperimage;
+ }
+ return (strip);
+}
+
+/*
+ * Compute how many strips are in an image.
+ */
+uint32
+TIFFNumberOfStrips(TIFF* tif)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 nstrips;
+
+ nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
+ TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+ nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
+ "TIFFNumberOfStrips");
+ return (nstrips);
+}
+
+/*
+ * Compute the # bytes in a variable height, row-aligned strip.
+ */
+uint64
+TIFFVStripSize64(TIFF* tif, uint32 nrows)
+{
+ static const char module[] = "TIFFVStripSize64";
+ TIFFDirectory *td = &tif->tif_dir;
+ if (nrows==(uint32)(-1))
+ nrows=td->td_imagelength;
+ if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
+ (td->td_photometric == PHOTOMETRIC_YCBCR)&&
+ (!isUpSampled(tif)))
+ {
+ /*
+ * Packed YCbCr data contain one Cb+Cr for every
+ * HorizontalSampling*VerticalSampling Y values.
+ * Must also roundup width and height when calculating
+ * since images that are not a multiple of the
+ * horizontal/vertical subsampling area include
+ * YCbCr data for the extended image.
+ */
+ uint16 ycbcrsubsampling[2];
+ uint16 samplingblock_samples;
+ uint32 samplingblocks_hor;
+ uint32 samplingblocks_ver;
+ uint64 samplingrow_samples;
+ uint64 samplingrow_size;
+ if(td->td_samplesperpixel!=3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Invalid td_samplesperpixel value");
+ return 0;
+ }
+ TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
+ ycbcrsubsampling+1);
+ if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
+ ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Invalid YCbCr subsampling (%dx%d)",
+ ycbcrsubsampling[0],
+ ycbcrsubsampling[1] );
+ return 0;
+ }
+ samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
+ samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
+ samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
+ samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
+ samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
+ return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
+ }
+ else
+ return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
+}
+tmsize_t
+TIFFVStripSize(TIFF* tif, uint32 nrows)
+{
+ static const char module[] = "TIFFVStripSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFVStripSize64(tif,nrows);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Compute the # bytes in a raw strip.
+ */
+uint64
+TIFFRawStripSize64(TIFF* tif, uint32 strip)
+{
+ static const char module[] = "TIFFRawStripSize64";
+ TIFFDirectory* td = &tif->tif_dir;
+ uint64 bytecount = td->td_stripbytecount[strip];
+
+ if (bytecount == 0)
+ {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%I64u: Invalid strip byte count, strip %lu",
+ (unsigned __int64) bytecount,
+ (unsigned long) strip);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%llu: Invalid strip byte count, strip %lu",
+ (unsigned long long) bytecount,
+ (unsigned long) strip);
+#endif
+ bytecount = (uint64) -1;
+ }
+
+ return bytecount;
+}
+tmsize_t
+TIFFRawStripSize(TIFF* tif, uint32 strip)
+{
+ static const char module[] = "TIFFRawStripSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFRawStripSize64(tif,strip);
+ if (m==(uint64)(-1))
+ n=(tmsize_t)(-1);
+ else
+ {
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ }
+ return(n);
+}
+
+/*
+ * Compute the # bytes in a (row-aligned) strip.
+ *
+ * Note that if RowsPerStrip is larger than the
+ * recorded ImageLength, then the strip size is
+ * truncated to reflect the actual space required
+ * to hold the strip.
+ */
+uint64
+TIFFStripSize64(TIFF* tif)
+{
+ TIFFDirectory* td = &tif->tif_dir;
+ uint32 rps = td->td_rowsperstrip;
+ if (rps > td->td_imagelength)
+ rps = td->td_imagelength;
+ return (TIFFVStripSize64(tif, rps));
+}
+tmsize_t
+TIFFStripSize(TIFF* tif)
+{
+ static const char module[] = "TIFFStripSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFStripSize64(tif);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Compute a default strip size based on the image
+ * characteristics and a requested value. If the
+ * request is <1 then we choose a strip size according
+ * to certain heuristics.
+ */
+uint32
+TIFFDefaultStripSize(TIFF* tif, uint32 request)
+{
+ return (*tif->tif_defstripsize)(tif, request);
+}
+
+uint32
+_TIFFDefaultStripSize(TIFF* tif, uint32 s)
+{
+ if ((int32) s < 1) {
+ /*
+ * If RowsPerStrip is unspecified, try to break the
+ * image up into strips that are approximately
+ * STRIP_SIZE_DEFAULT bytes long.
+ */
+ uint64 scanlinesize;
+ uint64 rows;
+ scanlinesize=TIFFScanlineSize64(tif);
+ if (scanlinesize==0)
+ scanlinesize=1;
+ rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
+ if (rows==0)
+ rows=1;
+ else if (rows>0xFFFFFFFF)
+ rows=0xFFFFFFFF;
+ s=(uint32)rows;
+ }
+ return (s);
+}
+
+/*
+ * Return the number of bytes to read/write in a call to
+ * one of the scanline-oriented i/o routines. Note that
+ * this number may be 1/samples-per-pixel if data is
+ * stored as separate planes.
+ * The ScanlineSize in case of YCbCrSubsampling is defined as the
+ * strip size divided by the strip height, i.e. the size of a pack of vertical
+ * subsampling lines divided by vertical subsampling. It should thus make
+ * sense when multiplied by a multiple of vertical subsampling.
+ */
+uint64
+TIFFScanlineSize64(TIFF* tif)
+{
+ static const char module[] = "TIFFScanlineSize64";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 scanline_size;
+ if (td->td_planarconfig==PLANARCONFIG_CONTIG)
+ {
+ if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
+ (td->td_samplesperpixel==3)&&
+ (!isUpSampled(tif)))
+ {
+ uint16 ycbcrsubsampling[2];
+ uint16 samplingblock_samples;
+ uint32 samplingblocks_hor;
+ uint64 samplingrow_samples;
+ uint64 samplingrow_size;
+ if(td->td_samplesperpixel!=3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Invalid td_samplesperpixel value");
+ return 0;
+ }
+ TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
+ ycbcrsubsampling+0,
+ ycbcrsubsampling+1);
+ if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
+ ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Invalid YCbCr subsampling");
+ return 0;
+ }
+ samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
+ samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
+ samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
+ samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
+ scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
+ }
+ else
+ {
+ uint64 scanline_samples;
+ scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
+ scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
+ }
+ }
+ else
+ {
+ scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
+ }
+ if (scanline_size == 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
+ return 0;
+ }
+ return(scanline_size);
+}
+tmsize_t
+TIFFScanlineSize(TIFF* tif)
+{
+ static const char module[] = "TIFFScanlineSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFScanlineSize64(tif);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m) {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Return the number of bytes required to store a complete
+ * decoded and packed raster scanline (as opposed to the
+ * I/O size returned by TIFFScanlineSize which may be less
+ * if data is store as separate planes).
+ */
+uint64
+TIFFRasterScanlineSize64(TIFF* tif)
+{
+ static const char module[] = "TIFFRasterScanlineSize64";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 scanline;
+
+ scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
+ return (TIFFhowmany8_64(scanline));
+ } else
+ return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
+ td->td_samplesperpixel, module));
+}
+tmsize_t
+TIFFRasterScanlineSize(TIFF* tif)
+{
+ static const char module[] = "TIFFRasterScanlineSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFRasterScanlineSize64(tif);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_swab.c b/test/monniaux/tiff-4.0.10/tif_swab.c
new file mode 100644
index 00000000..b174ba69
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_swab.c
@@ -0,0 +1,310 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library Bit & Byte Swapping Support.
+ *
+ * XXX We assume short = 16-bits and long = 32-bits XXX
+ */
+#include "tiffiop.h"
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabShort)
+void
+TIFFSwabShort(uint16* wp)
+{
+ register unsigned char* cp = (unsigned char*) wp;
+ unsigned char t;
+ assert(sizeof(uint16)==2);
+ t = cp[1]; cp[1] = cp[0]; cp[0] = t;
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong)
+void
+TIFFSwabLong(uint32* lp)
+{
+ register unsigned char* cp = (unsigned char*) lp;
+ unsigned char t;
+ assert(sizeof(uint32)==4);
+ t = cp[3]; cp[3] = cp[0]; cp[0] = t;
+ t = cp[2]; cp[2] = cp[1]; cp[1] = t;
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong8)
+void
+TIFFSwabLong8(uint64* lp)
+{
+ register unsigned char* cp = (unsigned char*) lp;
+ unsigned char t;
+ assert(sizeof(uint64)==8);
+ t = cp[7]; cp[7] = cp[0]; cp[0] = t;
+ t = cp[6]; cp[6] = cp[1]; cp[1] = t;
+ t = cp[5]; cp[5] = cp[2]; cp[2] = t;
+ t = cp[4]; cp[4] = cp[3]; cp[3] = t;
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfShort)
+void
+TIFFSwabArrayOfShort(register uint16* wp, tmsize_t n)
+{
+ register unsigned char* cp;
+ register unsigned char t;
+ assert(sizeof(uint16)==2);
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char*) wp;
+ t = cp[1]; cp[1] = cp[0]; cp[0] = t;
+ wp++;
+ }
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfTriples)
+void
+TIFFSwabArrayOfTriples(register uint8* tp, tmsize_t n)
+{
+ unsigned char* cp;
+ unsigned char t;
+
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char*) tp;
+ t = cp[2]; cp[2] = cp[0]; cp[0] = t;
+ tp += 3;
+ }
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong)
+void
+TIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)
+{
+ register unsigned char *cp;
+ register unsigned char t;
+ assert(sizeof(uint32)==4);
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char *)lp;
+ t = cp[3]; cp[3] = cp[0]; cp[0] = t;
+ t = cp[2]; cp[2] = cp[1]; cp[1] = t;
+ lp++;
+ }
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong8)
+void
+TIFFSwabArrayOfLong8(register uint64* lp, tmsize_t n)
+{
+ register unsigned char *cp;
+ register unsigned char t;
+ assert(sizeof(uint64)==8);
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char *)lp;
+ t = cp[7]; cp[7] = cp[0]; cp[0] = t;
+ t = cp[6]; cp[6] = cp[1]; cp[1] = t;
+ t = cp[5]; cp[5] = cp[2]; cp[2] = t;
+ t = cp[4]; cp[4] = cp[3]; cp[3] = t;
+ lp++;
+ }
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabFloat)
+void
+TIFFSwabFloat(float* fp)
+{
+ register unsigned char* cp = (unsigned char*) fp;
+ unsigned char t;
+ assert(sizeof(float)==4);
+ t = cp[3]; cp[3] = cp[0]; cp[0] = t;
+ t = cp[2]; cp[2] = cp[1]; cp[1] = t;
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfFloat)
+void
+TIFFSwabArrayOfFloat(register float* fp, tmsize_t n)
+{
+ register unsigned char *cp;
+ register unsigned char t;
+ assert(sizeof(float)==4);
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char *)fp;
+ t = cp[3]; cp[3] = cp[0]; cp[0] = t;
+ t = cp[2]; cp[2] = cp[1]; cp[1] = t;
+ fp++;
+ }
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabDouble)
+void
+TIFFSwabDouble(double *dp)
+{
+ register unsigned char* cp = (unsigned char*) dp;
+ unsigned char t;
+ assert(sizeof(double)==8);
+ t = cp[7]; cp[7] = cp[0]; cp[0] = t;
+ t = cp[6]; cp[6] = cp[1]; cp[1] = t;
+ t = cp[5]; cp[5] = cp[2]; cp[2] = t;
+ t = cp[4]; cp[4] = cp[3]; cp[3] = t;
+}
+#endif
+
+#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfDouble)
+void
+TIFFSwabArrayOfDouble(double* dp, tmsize_t n)
+{
+ register unsigned char *cp;
+ register unsigned char t;
+ assert(sizeof(double)==8);
+ /* XXX unroll loop some */
+ while (n-- > 0) {
+ cp = (unsigned char *)dp;
+ t = cp[7]; cp[7] = cp[0]; cp[0] = t;
+ t = cp[6]; cp[6] = cp[1]; cp[1] = t;
+ t = cp[5]; cp[5] = cp[2]; cp[2] = t;
+ t = cp[4]; cp[4] = cp[3]; cp[3] = t;
+ dp++;
+ }
+}
+#endif
+
+/*
+ * Bit reversal tables. TIFFBitRevTable[<byte>] gives
+ * the bit reversed value of <byte>. Used in various
+ * places in the library when the FillOrder requires
+ * bit reversal of byte values (e.g. CCITT Fax 3
+ * encoding/decoding). TIFFNoBitRevTable is provided
+ * for algorithms that want an equivalent table that
+ * do not reverse bit values.
+ */
+static const unsigned char TIFFBitRevTable[256] = {
+ 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
+ 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
+ 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
+ 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
+ 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
+ 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
+ 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
+ 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
+ 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
+ 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
+ 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
+ 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
+ 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
+ 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
+ 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
+ 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
+ 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
+ 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
+ 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
+ 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
+ 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
+ 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
+ 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
+ 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
+ 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
+ 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
+ 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
+ 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
+ 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
+ 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
+ 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
+ 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
+};
+static const unsigned char TIFFNoBitRevTable[256] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
+ 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
+ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
+ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
+};
+
+const unsigned char*
+TIFFGetBitRevTable(int reversed)
+{
+ return (reversed ? TIFFBitRevTable : TIFFNoBitRevTable);
+}
+
+void
+TIFFReverseBits(uint8* cp, tmsize_t n)
+{
+ for (; n > 8; n -= 8) {
+ cp[0] = TIFFBitRevTable[cp[0]];
+ cp[1] = TIFFBitRevTable[cp[1]];
+ cp[2] = TIFFBitRevTable[cp[2]];
+ cp[3] = TIFFBitRevTable[cp[3]];
+ cp[4] = TIFFBitRevTable[cp[4]];
+ cp[5] = TIFFBitRevTable[cp[5]];
+ cp[6] = TIFFBitRevTable[cp[6]];
+ cp[7] = TIFFBitRevTable[cp[7]];
+ cp += 8;
+ }
+ while (n-- > 0) {
+ *cp = TIFFBitRevTable[*cp];
+ cp++;
+ }
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_thunder.c b/test/monniaux/tiff-4.0.10/tif_thunder.c
new file mode 100644
index 00000000..2388dbb6
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_thunder.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#include <assert.h>
+#ifdef THUNDER_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * ThunderScan 4-bit Compression Algorithm Support
+ */
+
+/*
+ * ThunderScan uses an encoding scheme designed for
+ * 4-bit pixel values. Data is encoded in bytes, with
+ * each byte split into a 2-bit code word and a 6-bit
+ * data value. The encoding gives raw data, runs of
+ * pixels, or pixel values encoded as a delta from the
+ * previous pixel value. For the latter, either 2-bit
+ * or 3-bit delta values are used, with the deltas packed
+ * into a single byte.
+ */
+#define THUNDER_DATA 0x3f /* mask for 6-bit data */
+#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
+/* code values */
+#define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
+#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
+#define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
+#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
+#define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
+#define THUNDER_RAW 0xc0 /* raw data encoded */
+
+static const int twobitdeltas[4] = { 0, 1, 0, -1 };
+static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
+
+#define SETPIXEL(op, v) { \
+ lastpixel = (v) & 0xf; \
+ if ( npixels < maxpixels ) \
+ { \
+ if (npixels++ & 1) \
+ *op++ |= lastpixel; \
+ else \
+ op[0] = (uint8) (lastpixel << 4); \
+ } \
+}
+
+static int
+ThunderSetupDecode(TIFF* tif)
+{
+ static const char module[] = "ThunderSetupDecode";
+
+ if( tif->tif_dir.td_bitspersample != 4 )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
+ (int) tif->tif_dir.td_bitspersample );
+ return 0;
+ }
+
+
+ return (1);
+}
+
+static int
+ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
+{
+ static const char module[] = "ThunderDecode";
+ register unsigned char *bp;
+ register tmsize_t cc;
+ unsigned int lastpixel;
+ tmsize_t npixels;
+
+ bp = (unsigned char *)tif->tif_rawcp;
+ cc = tif->tif_rawcc;
+ lastpixel = 0;
+ npixels = 0;
+ while (cc > 0 && npixels < maxpixels) {
+ int n, delta;
+
+ n = *bp++;
+ cc--;
+ switch (n & THUNDER_CODE) {
+ case THUNDER_RUN: /* pixel run */
+ /*
+ * Replicate the last pixel n times,
+ * where n is the lower-order 6 bits.
+ */
+ if (npixels & 1) {
+ op[0] |= lastpixel;
+ lastpixel = *op++; npixels++; n--;
+ } else
+ lastpixel |= lastpixel << 4;
+ npixels += n;
+ if (npixels < maxpixels) {
+ for (; n > 0; n -= 2)
+ *op++ = (uint8) lastpixel;
+ }
+ if (n == -1)
+ *--op &= 0xf0;
+ lastpixel &= 0xf;
+ break;
+ case THUNDER_2BITDELTAS: /* 2-bit deltas */
+ if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
+ SETPIXEL(op, lastpixel + twobitdeltas[delta]);
+ if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
+ SETPIXEL(op, lastpixel + twobitdeltas[delta]);
+ if ((delta = (n & 3)) != DELTA2_SKIP)
+ SETPIXEL(op, lastpixel + twobitdeltas[delta]);
+ break;
+ case THUNDER_3BITDELTAS: /* 3-bit deltas */
+ if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
+ SETPIXEL(op, lastpixel + threebitdeltas[delta]);
+ if ((delta = (n & 7)) != DELTA3_SKIP)
+ SETPIXEL(op, lastpixel + threebitdeltas[delta]);
+ break;
+ case THUNDER_RAW: /* raw data */
+ SETPIXEL(op, n);
+ break;
+ }
+ }
+ tif->tif_rawcp = (uint8*) bp;
+ tif->tif_rawcc = cc;
+ if (npixels != maxpixels) {
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s data at scanline %lu (%I64u != %I64u)",
+ npixels < maxpixels ? "Not enough" : "Too much",
+ (unsigned long) tif->tif_row,
+ (unsigned __int64) npixels,
+ (unsigned __int64) maxpixels);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s data at scanline %lu (%llu != %llu)",
+ npixels < maxpixels ? "Not enough" : "Too much",
+ (unsigned long) tif->tif_row,
+ (unsigned long long) npixels,
+ (unsigned long long) maxpixels);
+#endif
+ return (0);
+ }
+
+ return (1);
+}
+
+static int
+ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "ThunderDecodeRow";
+ uint8* row = buf;
+
+ (void) s;
+ if (occ % tif->tif_scanlinesize)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+ return (0);
+ }
+ while (occ > 0) {
+ if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
+ return (0);
+ occ -= tif->tif_scanlinesize;
+ row += tif->tif_scanlinesize;
+ }
+ return (1);
+}
+
+int
+TIFFInitThunderScan(TIFF* tif, int scheme)
+{
+ (void) scheme;
+
+ tif->tif_setupdecode = ThunderSetupDecode;
+ tif->tif_decoderow = ThunderDecodeRow;
+ tif->tif_decodestrip = ThunderDecodeRow;
+ return (1);
+}
+#endif /* THUNDER_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_tile.c b/test/monniaux/tiff-4.0.10/tif_tile.c
new file mode 100644
index 00000000..58fe9354
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_tile.c
@@ -0,0 +1,320 @@
+/*
+ * Copyright (c) 1991-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Tiled Image Support Routines.
+ */
+#include "tiffiop.h"
+
+/*
+ * Compute which tile an (x,y,z,s) value is in.
+ */
+uint32
+TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 dx = td->td_tilewidth;
+ uint32 dy = td->td_tilelength;
+ uint32 dz = td->td_tiledepth;
+ uint32 tile = 1;
+
+ if (td->td_imagedepth == 1)
+ z = 0;
+ if (dx == (uint32) -1)
+ dx = td->td_imagewidth;
+ if (dy == (uint32) -1)
+ dy = td->td_imagelength;
+ if (dz == (uint32) -1)
+ dz = td->td_imagedepth;
+ if (dx != 0 && dy != 0 && dz != 0) {
+ uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
+ uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
+ uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
+
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+ tile = (xpt*ypt*zpt)*s +
+ (xpt*ypt)*(z/dz) +
+ xpt*(y/dy) +
+ x/dx;
+ else
+ tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
+ }
+ return (tile);
+}
+
+/*
+ * Check an (x,y,z,s) coordinate
+ * against the image bounds.
+ */
+int
+TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (x >= td->td_imagewidth) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Col out of range, max %lu",
+ (unsigned long) x,
+ (unsigned long) (td->td_imagewidth - 1));
+ return (0);
+ }
+ if (y >= td->td_imagelength) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Row out of range, max %lu",
+ (unsigned long) y,
+ (unsigned long) (td->td_imagelength - 1));
+ return (0);
+ }
+ if (z >= td->td_imagedepth) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Depth out of range, max %lu",
+ (unsigned long) z,
+ (unsigned long) (td->td_imagedepth - 1));
+ return (0);
+ }
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
+ s >= td->td_samplesperpixel) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Sample out of range, max %lu",
+ (unsigned long) s,
+ (unsigned long) (td->td_samplesperpixel - 1));
+ return (0);
+ }
+ return (1);
+}
+
+/*
+ * Compute how many tiles are in an image.
+ */
+uint32
+TIFFNumberOfTiles(TIFF* tif)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 dx = td->td_tilewidth;
+ uint32 dy = td->td_tilelength;
+ uint32 dz = td->td_tiledepth;
+ uint32 ntiles;
+
+ if (dx == (uint32) -1)
+ dx = td->td_imagewidth;
+ if (dy == (uint32) -1)
+ dy = td->td_imagelength;
+ if (dz == (uint32) -1)
+ dz = td->td_imagedepth;
+ ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
+ _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
+ TIFFhowmany_32(td->td_imagelength, dy),
+ "TIFFNumberOfTiles"),
+ TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+ ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
+ "TIFFNumberOfTiles");
+ return (ntiles);
+}
+
+/*
+ * Compute the # bytes in each row of a tile.
+ */
+uint64
+TIFFTileRowSize64(TIFF* tif)
+{
+ static const char module[] = "TIFFTileRowSize64";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 rowsize;
+ uint64 tilerowsize;
+
+ if (td->td_tilelength == 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Tile length is zero");
+ return 0;
+ }
+ if (td->td_tilewidth == 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Tile width is zero");
+ return (0);
+ }
+ rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
+ "TIFFTileRowSize");
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG)
+ {
+ if (td->td_samplesperpixel == 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Samples per pixel is zero");
+ return 0;
+ }
+ rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
+ "TIFFTileRowSize");
+ }
+ tilerowsize=TIFFhowmany8_64(rowsize);
+ if (tilerowsize == 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Computed tile row size is zero");
+ return 0;
+ }
+ return (tilerowsize);
+}
+tmsize_t
+TIFFTileRowSize(TIFF* tif)
+{
+ static const char module[] = "TIFFTileRowSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFTileRowSize64(tif);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Compute the # bytes in a variable length, row-aligned tile.
+ */
+uint64
+TIFFVTileSize64(TIFF* tif, uint32 nrows)
+{
+ static const char module[] = "TIFFVTileSize64";
+ TIFFDirectory *td = &tif->tif_dir;
+ if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
+ td->td_tiledepth == 0)
+ return (0);
+ if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
+ (td->td_photometric==PHOTOMETRIC_YCBCR)&&
+ (td->td_samplesperpixel==3)&&
+ (!isUpSampled(tif)))
+ {
+ /*
+ * Packed YCbCr data contain one Cb+Cr for every
+ * HorizontalSampling*VerticalSampling Y values.
+ * Must also roundup width and height when calculating
+ * since images that are not a multiple of the
+ * horizontal/vertical subsampling area include
+ * YCbCr data for the extended image.
+ */
+ uint16 ycbcrsubsampling[2];
+ uint16 samplingblock_samples;
+ uint32 samplingblocks_hor;
+ uint32 samplingblocks_ver;
+ uint64 samplingrow_samples;
+ uint64 samplingrow_size;
+ TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
+ ycbcrsubsampling+1);
+ if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
+ ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,
+ "Invalid YCbCr subsampling (%dx%d)",
+ ycbcrsubsampling[0],
+ ycbcrsubsampling[1] );
+ return 0;
+ }
+ samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
+ samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
+ samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
+ samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
+ samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
+ return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
+ }
+ else
+ return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
+}
+tmsize_t
+TIFFVTileSize(TIFF* tif, uint32 nrows)
+{
+ static const char module[] = "TIFFVTileSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFVTileSize64(tif,nrows);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Compute the # bytes in a row-aligned tile.
+ */
+uint64
+TIFFTileSize64(TIFF* tif)
+{
+ return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
+}
+tmsize_t
+TIFFTileSize(TIFF* tif)
+{
+ static const char module[] = "TIFFTileSize";
+ uint64 m;
+ tmsize_t n;
+ m=TIFFTileSize64(tif);
+ n=(tmsize_t)m;
+ if ((uint64)n!=m)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
+ n=0;
+ }
+ return(n);
+}
+
+/*
+ * Compute a default tile size based on the image
+ * characteristics and a requested value. If a
+ * request is <1 then we choose a size according
+ * to certain heuristics.
+ */
+void
+TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
+{
+ (*tif->tif_deftilesize)(tif, tw, th);
+}
+
+void
+_TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
+{
+ (void) tif;
+ if (*(int32*) tw < 1)
+ *tw = 256;
+ if (*(int32*) th < 1)
+ *th = 256;
+ /* roundup to a multiple of 16 per the spec */
+ if (*tw & 0xf)
+ *tw = TIFFroundup_32(*tw, 16);
+ if (*th & 0xf)
+ *th = TIFFroundup_32(*th, 16);
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_unix.c b/test/monniaux/tiff-4.0.10/tif_unix.c
new file mode 100644
index 00000000..874f1feb
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_unix.c
@@ -0,0 +1,384 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library UNIX-specific Routines. These are should also work with the
+ * Windows Common RunTime Library.
+ */
+
+#include "tif_config.h"
+
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+#include <errno.h>
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+
+#ifdef HAVE_IO_H
+# include <io.h>
+#endif
+
+#include "tiffiop.h"
+
+
+#define TIFF_IO_MAX 2147483647U
+
+
+typedef union fd_as_handle_union
+{
+ int fd;
+ thandle_t h;
+} fd_as_handle_union_t;
+
+static tmsize_t
+_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
+{
+ fd_as_handle_union_t fdh;
+ const size_t bytes_total = (size_t) size;
+ size_t bytes_read;
+ tmsize_t count = -1;
+ if ((tmsize_t) bytes_total != size)
+ {
+ errno=EINVAL;
+ return (tmsize_t) -1;
+ }
+ fdh.h = fd;
+ for (bytes_read=0; bytes_read < bytes_total; bytes_read+=count)
+ {
+ char *buf_offset = (char *) buf+bytes_read;
+ size_t io_size = bytes_total-bytes_read;
+ if (io_size > TIFF_IO_MAX)
+ io_size = TIFF_IO_MAX;
+ count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
+ if (count <= 0)
+ break;
+ }
+ if (count < 0)
+ return (tmsize_t)-1;
+ return (tmsize_t) bytes_read;
+}
+
+static tmsize_t
+_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
+{
+ fd_as_handle_union_t fdh;
+ const size_t bytes_total = (size_t) size;
+ size_t bytes_written;
+ tmsize_t count = -1;
+ if ((tmsize_t) bytes_total != size)
+ {
+ errno=EINVAL;
+ return (tmsize_t) -1;
+ }
+ fdh.h = fd;
+ for (bytes_written=0; bytes_written < bytes_total; bytes_written+=count)
+ {
+ const char *buf_offset = (char *) buf+bytes_written;
+ size_t io_size = bytes_total-bytes_written;
+ if (io_size > TIFF_IO_MAX)
+ io_size = TIFF_IO_MAX;
+ count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
+ if (count <= 0)
+ break;
+ }
+ if (count < 0)
+ return (tmsize_t)-1;
+ return (tmsize_t) bytes_written;
+ /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
+}
+
+static uint64
+_tiffSeekProc(thandle_t fd, uint64 off, int whence)
+{
+ fd_as_handle_union_t fdh;
+ _TIFF_off_t off_io = (_TIFF_off_t) off;
+ if ((uint64) off_io != off)
+ {
+ errno=EINVAL;
+ return (uint64) -1; /* this is really gross */
+ }
+ fdh.h = fd;
+ return((uint64)_TIFF_lseek_f(fdh.fd,off_io,whence));
+}
+
+static int
+_tiffCloseProc(thandle_t fd)
+{
+ fd_as_handle_union_t fdh;
+ fdh.h = fd;
+ return(close(fdh.fd));
+}
+
+static uint64
+_tiffSizeProc(thandle_t fd)
+{
+ _TIFF_stat_s sb;
+ fd_as_handle_union_t fdh;
+ fdh.h = fd;
+ if (_TIFF_fstat_f(fdh.fd,&sb)<0)
+ return(0);
+ else
+ return((uint64)sb.st_size);
+}
+
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+
+static int
+_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
+{
+ uint64 size64 = _tiffSizeProc(fd);
+ tmsize_t sizem = (tmsize_t)size64;
+ if ((uint64)sizem==size64) {
+ fd_as_handle_union_t fdh;
+ fdh.h = fd;
+ *pbase = (void*)
+ mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
+ if (*pbase != (void*) -1) {
+ *psize = (tmsize_t)sizem;
+ return (1);
+ }
+ }
+ return (0);
+}
+
+static void
+_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
+{
+ (void) fd;
+ (void) munmap(base, (off_t) size);
+}
+#else /* !HAVE_MMAP */
+static int
+_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
+{
+ (void) fd; (void) pbase; (void) psize;
+ return (0);
+}
+
+static void
+_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
+{
+ (void) fd; (void) base; (void) size;
+}
+#endif /* !HAVE_MMAP */
+
+/*
+ * Open a TIFF file descriptor for read/writing.
+ */
+TIFF*
+TIFFFdOpen(int fd, const char* name, const char* mode)
+{
+ TIFF* tif;
+
+ fd_as_handle_union_t fdh;
+ fdh.fd = fd;
+ tif = TIFFClientOpen(name, mode,
+ fdh.h,
+ _tiffReadProc, _tiffWriteProc,
+ _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
+ _tiffMapProc, _tiffUnmapProc);
+ if (tif)
+ tif->tif_fd = fd;
+ return (tif);
+}
+
+/*
+ * Open a TIFF file for read/writing.
+ */
+TIFF*
+TIFFOpen(const char* name, const char* mode)
+{
+ static const char module[] = "TIFFOpen";
+ int m, fd;
+ TIFF* tif;
+
+ m = _TIFFgetMode(mode, module);
+ if (m == -1)
+ return ((TIFF*)0);
+
+/* for cygwin and mingw */
+#ifdef O_BINARY
+ m |= O_BINARY;
+#endif
+
+ fd = open(name, m, 0666);
+ if (fd < 0) {
+ if (errno > 0 && strerror(errno) != NULL ) {
+ TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
+ } else {
+ TIFFErrorExt(0, module, "%s: Cannot open", name);
+ }
+ return ((TIFF *)0);
+ }
+
+ tif = TIFFFdOpen((int)fd, name, mode);
+ if(!tif)
+ close(fd);
+ return tif;
+}
+
+#ifdef __WIN32__
+#include <windows.h>
+/*
+ * Open a TIFF file with a Unicode filename, for read/writing.
+ */
+TIFF*
+TIFFOpenW(const wchar_t* name, const char* mode)
+{
+ static const char module[] = "TIFFOpenW";
+ int m, fd;
+ int mbsize;
+ char *mbname;
+ TIFF* tif;
+
+ m = _TIFFgetMode(mode, module);
+ if (m == -1)
+ return ((TIFF*)0);
+
+/* for cygwin and mingw */
+#ifdef O_BINARY
+ m |= O_BINARY;
+#endif
+
+ fd = _wopen(name, m, 0666);
+ if (fd < 0) {
+ TIFFErrorExt(0, module, "%ls: Cannot open", name);
+ return ((TIFF *)0);
+ }
+
+ mbname = NULL;
+ mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
+ if (mbsize > 0) {
+ mbname = _TIFFmalloc(mbsize);
+ if (!mbname) {
+ TIFFErrorExt(0, module,
+ "Can't allocate space for filename conversion buffer");
+ return ((TIFF*)0);
+ }
+
+ WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
+ NULL, NULL);
+ }
+
+ tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
+ mode);
+
+ _TIFFfree(mbname);
+
+ if(!tif)
+ close(fd);
+ return tif;
+}
+#endif
+
+void*
+_TIFFmalloc(tmsize_t s)
+{
+ if (s == 0)
+ return ((void *) NULL);
+
+ return (malloc((size_t) s));
+}
+
+void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
+{
+ if( nmemb == 0 || siz == 0 )
+ return ((void *) NULL);
+
+ return calloc((size_t) nmemb, (size_t)siz);
+}
+
+void
+_TIFFfree(void* p)
+{
+ free(p);
+}
+
+void*
+_TIFFrealloc(void* p, tmsize_t s)
+{
+ return (realloc(p, (size_t) s));
+}
+
+void
+_TIFFmemset(void* p, int v, tmsize_t c)
+{
+ memset(p, v, (size_t) c);
+}
+
+void
+_TIFFmemcpy(void* d, const void* s, tmsize_t c)
+{
+ memcpy(d, s, (size_t) c);
+}
+
+int
+_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
+{
+ return (memcmp(p1, p2, (size_t) c));
+}
+
+static void
+unixWarningHandler(const char* module, const char* fmt, va_list ap)
+{
+ if (module != NULL)
+ fprintf(stderr, "%s: ", module);
+ fprintf(stderr, "Warning, ");
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, ".\n");
+}
+TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
+
+static void
+unixErrorHandler(const char* module, const char* fmt, va_list ap)
+{
+ if (module != NULL)
+ fprintf(stderr, "%s: ", module);
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, ".\n");
+}
+TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_version.c b/test/monniaux/tiff-4.0.10/tif_version.c
new file mode 100644
index 00000000..60875bbf
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_version.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 1992-1997 Sam Leffler
+ * Copyright (c) 1992-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+#include "tiffiop.h"
+
+static const char TIFFVersion[] = TIFFLIB_VERSION_STR;
+
+const char*
+TIFFGetVersion(void)
+{
+ return (TIFFVersion);
+}
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_warning.c b/test/monniaux/tiff-4.0.10/tif_warning.c
new file mode 100644
index 00000000..c482785c
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_warning.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ */
+#include "tiffiop.h"
+
+TIFFErrorHandlerExt _TIFFwarningHandlerExt = NULL;
+
+TIFFErrorHandler
+TIFFSetWarningHandler(TIFFErrorHandler handler)
+{
+ TIFFErrorHandler prev = _TIFFwarningHandler;
+ _TIFFwarningHandler = handler;
+ return (prev);
+}
+
+TIFFErrorHandlerExt
+TIFFSetWarningHandlerExt(TIFFErrorHandlerExt handler)
+{
+ TIFFErrorHandlerExt prev = _TIFFwarningHandlerExt;
+ _TIFFwarningHandlerExt = handler;
+ return (prev);
+}
+
+void
+TIFFWarning(const char* module, const char* fmt, ...)
+{
+ va_list ap;
+ if (_TIFFwarningHandler) {
+ va_start(ap, fmt);
+ (*_TIFFwarningHandler)(module, fmt, ap);
+ va_end(ap);
+ }
+ if (_TIFFwarningHandlerExt) {
+ va_start(ap, fmt);
+ (*_TIFFwarningHandlerExt)(0, module, fmt, ap);
+ va_end(ap);
+ }
+}
+
+void
+TIFFWarningExt(thandle_t fd, const char* module, const char* fmt, ...)
+{
+ va_list ap;
+ if (_TIFFwarningHandler) {
+ va_start(ap, fmt);
+ (*_TIFFwarningHandler)(module, fmt, ap);
+ va_end(ap);
+ }
+ if (_TIFFwarningHandlerExt) {
+ va_start(ap, fmt);
+ (*_TIFFwarningHandlerExt)(fd, module, fmt, ap);
+ va_end(ap);
+ }
+}
+
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_webp.c b/test/monniaux/tiff-4.0.10/tif_webp.c
new file mode 100644
index 00000000..a002f481
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_webp.c
@@ -0,0 +1,684 @@
+/*
+* Copyright (c) 2018, Mapbox
+* Author: <norman.barker at mapbox.com>
+*
+* Permission to use, copy, modify, distribute, and sell this software and
+* its documentation for any purpose is hereby granted without fee, provided
+* that (i) the above copyright notices and this permission notice appear in
+* all copies of the software and related documentation, and (ii) the names of
+* Sam Leffler and Silicon Graphics may not be used in any advertising or
+* publicity relating to the software without the specific, prior written
+* permission of Sam Leffler and Silicon Graphics.
+*
+* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+*
+* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+* OF THIS SOFTWARE.
+*/
+
+#include "tiffiop.h"
+#ifdef WEBP_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * WEBP Compression Support
+ *
+ */
+
+#include "webp/decode.h"
+#include "webp/encode.h"
+
+#include <stdio.h>
+
+#define LSTATE_INIT_DECODE 0x01
+#define LSTATE_INIT_ENCODE 0x02
+/*
+ * State block for each open TIFF
+ * file using WEBP compression/decompression.
+ */
+typedef struct {
+ uint16 nSamples; /* number of samples per pixel */
+
+ int lossless; /* lossy/lossless compression */
+ int quality_level; /* compression level */
+ WebPPicture sPicture; /* WebP Picture */
+ WebPConfig sEncoderConfig; /* WebP encoder config */
+ uint8* pBuffer; /* buffer to hold raw data on encoding */
+ unsigned int buffer_offset; /* current offset into the buffer */
+ unsigned int buffer_size;
+
+ WebPIDecoder* psDecoder; /* WebPIDecoder */
+ WebPDecBuffer sDecBuffer; /* Decoder buffer */
+ int last_y; /* Last row decoded */
+
+ int state; /* state flags */
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+} WebPState;
+
+#define LState(tif) ((WebPState*) (tif)->tif_data)
+#define DecoderState(tif) LState(tif)
+#define EncoderState(tif) LState(tif)
+
+static int TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static
+int TWebPDatasetWriter(const uint8_t* data, size_t data_size,
+ const WebPPicture* const picture)
+{
+ static const char module[] = "TWebPDatasetWriter";
+ TIFF* tif = (TIFF*)(picture->custom_ptr);
+
+ if ( (tif->tif_rawcc + (tmsize_t)data_size) > tif->tif_rawdatasize ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Buffer too small by " TIFF_SIZE_FORMAT " bytes.",
+ (size_t) (tif->tif_rawcc + data_size - tif->tif_rawdatasize));
+ return 0;
+ } else {
+ _TIFFmemcpy(tif->tif_rawcp, data, data_size);
+ tif->tif_rawcc += data_size;
+ tif->tif_rawcp += data_size;
+ return 1;
+ }
+}
+
+/*
+ * Encode a chunk of pixels.
+ */
+static int
+TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "TWebPEncode";
+ WebPState *sp = EncoderState(tif);
+ (void) s;
+
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_ENCODE);
+
+ if( (uint64)sp->buffer_offset +
+ (uint64)cc > sp->buffer_size )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Too many bytes to be written");
+ return 0;
+ }
+
+ memcpy(sp->pBuffer + sp->buffer_offset,
+ bp, cc);
+ sp->buffer_offset += (unsigned)cc;
+
+ return 1;
+
+}
+
+static int
+TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "WebPDecode";
+ VP8StatusCode status = VP8_STATUS_OK;
+ WebPState *sp = DecoderState(tif);
+ (void) s;
+
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_DECODE);
+
+ if (occ % sp->sDecBuffer.u.RGBA.stride)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Fractional scanlines cannot be read");
+ return 0;
+ }
+
+ status = WebPIAppend(sp->psDecoder, tif->tif_rawcp, tif->tif_rawcc);
+
+ if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
+ if (status == VP8_STATUS_INVALID_PARAM) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Invalid parameter used.");
+ } else if (status == VP8_STATUS_OUT_OF_MEMORY) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Out of memory.");
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Unrecognized error.");
+ }
+ return 0;
+ } else {
+ int current_y, stride;
+ uint8_t* buf;
+
+ /* Returns the RGB/A image decoded so far */
+ buf = WebPIDecGetRGB(sp->psDecoder, &current_y, NULL, NULL, &stride);
+
+ if ((buf != NULL) &&
+ (occ <= stride * (current_y - sp->last_y))) {
+ memcpy(op,
+ buf + (sp->last_y * stride),
+ occ);
+
+ tif->tif_rawcp += tif->tif_rawcc;
+ tif->tif_rawcc = 0;
+ sp->last_y += occ / sp->sDecBuffer.u.RGBA.stride;
+ return 1;
+ } else {
+ TIFFErrorExt(tif->tif_clientdata, module, "Unable to decode WebP data.");
+ return 0;
+ }
+ }
+}
+
+static int
+TWebPFixupTags(TIFF* tif)
+{
+ (void) tif;
+ if (tif->tif_dir.td_planarconfig != PLANARCONFIG_CONTIG) {
+ static const char module[] = "TWebPFixupTags";
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "TIFF WEBP requires data to be stored contiguously in RGB e.g. RGBRGBRGB "
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ "or RGBARGBARGBA"
+#endif
+ );
+ return 0;
+ }
+ return 1;
+}
+
+static int
+TWebPSetupDecode(TIFF* tif)
+{
+ static const char module[] = "WebPSetupDecode";
+ uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
+ uint16 sampleFormat = tif->tif_dir.td_sampleformat;
+
+ WebPState* sp = DecoderState(tif);
+ assert(sp != NULL);
+
+ sp->nSamples = tif->tif_dir.td_samplesperpixel;
+
+ /* check band count */
+ if ( sp->nSamples != 3
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ && sp->nSamples != 4
+#endif
+ )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
+ #if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ "or 4 (RGBA) "
+ #endif
+ "bands.",
+ sp->nSamples );
+ return 0;
+ }
+
+ /* check bits per sample and data type */
+ if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WEBP driver requires 8 bit unsigned data");
+ return 0;
+ }
+
+ /* if we were last encoding, terminate this mode */
+ if (sp->state & LSTATE_INIT_ENCODE) {
+ WebPPictureFree(&sp->sPicture);
+ if (sp->pBuffer != NULL) {
+ _TIFFfree(sp->pBuffer);
+ sp->pBuffer = NULL;
+ }
+ sp->buffer_offset = 0;
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_DECODE;
+
+ return 1;
+}
+
+/*
+* Setup state for decoding a strip.
+*/
+static int
+TWebPPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "TWebPPreDecode";
+ uint32 segment_width, segment_height;
+ WebPState* sp = DecoderState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+ (void) s;
+ assert(sp != NULL);
+
+ if (isTiled(tif)) {
+ segment_width = td->td_tilewidth;
+ segment_height = td->td_tilelength;
+ } else {
+ segment_width = td->td_imagewidth;
+ segment_height = td->td_imagelength - tif->tif_row;
+ if (segment_height > td->td_rowsperstrip)
+ segment_height = td->td_rowsperstrip;
+ }
+
+ if( (sp->state & LSTATE_INIT_DECODE) == 0 )
+ tif->tif_setupdecode(tif);
+
+ if (sp->psDecoder != NULL) {
+ WebPIDelete(sp->psDecoder);
+ WebPFreeDecBuffer(&sp->sDecBuffer);
+ sp->psDecoder = NULL;
+ }
+
+ sp->last_y = 0;
+
+ WebPInitDecBuffer(&sp->sDecBuffer);
+
+ sp->sDecBuffer.is_external_memory = 0;
+ sp->sDecBuffer.width = segment_width;
+ sp->sDecBuffer.height = segment_height;
+ sp->sDecBuffer.u.RGBA.stride = segment_width * sp->nSamples;
+ sp->sDecBuffer.u.RGBA.size = segment_width * sp->nSamples * segment_height;
+
+ if (sp->nSamples > 3) {
+ sp->sDecBuffer.colorspace = MODE_RGBA;
+ } else {
+ sp->sDecBuffer.colorspace = MODE_RGB;
+ }
+
+ sp->psDecoder = WebPINewDecoder(&sp->sDecBuffer);
+
+ if (sp->psDecoder == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Unable to allocate WebP decoder.");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+TWebPSetupEncode(TIFF* tif)
+{
+ static const char module[] = "WebPSetupEncode";
+ uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
+ uint16 sampleFormat = tif->tif_dir.td_sampleformat;
+
+ WebPState* sp = EncoderState(tif);
+ assert(sp != NULL);
+
+ sp->nSamples = tif->tif_dir.td_samplesperpixel;
+
+ /* check band count */
+ if ( sp->nSamples != 3
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ && sp->nSamples != 4
+#endif
+ )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ "or 4 (RGBA) "
+#endif
+ "bands.",
+ sp->nSamples );
+ return 0;
+ }
+
+ /* check bits per sample and data type */
+ if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WEBP driver requires 8 bit unsigned data");
+ return 0;
+ }
+
+ if (sp->state & LSTATE_INIT_DECODE) {
+ WebPIDelete(sp->psDecoder);
+ WebPFreeDecBuffer(&sp->sDecBuffer);
+ sp->psDecoder = NULL;
+ sp->last_y = 0;
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_ENCODE;
+
+ if (!WebPConfigInitInternal(&sp->sEncoderConfig, WEBP_PRESET_DEFAULT,
+ sp->quality_level,
+ WEBP_ENCODER_ABI_VERSION)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error creating WebP encoder configuration.");
+ return 0;
+ }
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ sp->sEncoderConfig.lossless = sp->lossless;
+#endif
+
+ if (!WebPValidateConfig(&sp->sEncoderConfig)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error with WebP encoder configuration.");
+ return 0;
+ }
+
+ if (!WebPPictureInit(&sp->sPicture)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error initializing WebP picture.");
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+* Reset encoding state at the start of a strip.
+*/
+static int
+TWebPPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "TWebPPreEncode";
+ uint32 segment_width, segment_height;
+ WebPState *sp = EncoderState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ (void) s;
+
+ assert(sp != NULL);
+ if( sp->state != LSTATE_INIT_ENCODE )
+ tif->tif_setupencode(tif);
+
+ /*
+ * Set encoding parameters for this strip/tile.
+ */
+ if (isTiled(tif)) {
+ segment_width = td->td_tilewidth;
+ segment_height = td->td_tilelength;
+ } else {
+ segment_width = td->td_imagewidth;
+ segment_height = td->td_imagelength - tif->tif_row;
+ if (segment_height > td->td_rowsperstrip)
+ segment_height = td->td_rowsperstrip;
+ }
+
+ if( segment_width > 16383 || segment_height > 16383 ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WEBP maximum image dimensions are 16383 x 16383.");
+ return 0;
+ }
+
+ /* set up buffer for raw data */
+ /* given above check and that nSamples <= 4, buffer_size is <= 1 GB */
+ sp->buffer_size = segment_width * segment_height * sp->nSamples;
+ sp->pBuffer = _TIFFmalloc(sp->buffer_size);
+ if( !sp->pBuffer) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Cannot allocate buffer");
+ return 0;
+ }
+ sp->buffer_offset = 0;
+
+ sp->sPicture.width = segment_width;
+ sp->sPicture.height = segment_height;
+ sp->sPicture.writer = TWebPDatasetWriter;
+ sp->sPicture.custom_ptr = tif;
+
+ return 1;
+}
+
+/*
+* Finish off an encoded strip by flushing it.
+*/
+static int
+TWebPPostEncode(TIFF* tif)
+{
+ static const char module[] = "WebPPostEncode";
+ int64_t stride;
+ WebPState *sp = EncoderState(tif);
+ assert(sp != NULL);
+
+ assert(sp->state == LSTATE_INIT_ENCODE);
+
+ stride = (int64_t)sp->sPicture.width * sp->nSamples;
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ if (sp->nSamples == 4) {
+ if (!WebPPictureImportRGBA(&sp->sPicture, sp->pBuffer, (int)stride)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WebPPictureImportRGBA() failed" );
+ return 0;
+ }
+ }
+ else
+#endif
+ if (!WebPPictureImportRGB(&sp->sPicture, sp->pBuffer, (int)stride)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WebPPictureImportRGB() failed");
+ return 0;
+ }
+
+ if (!WebPEncode(&sp->sEncoderConfig, &sp->sPicture)) {
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ const char* pszErrorMsg = NULL;
+ switch(sp->sPicture.error_code) {
+ case VP8_ENC_ERROR_OUT_OF_MEMORY:
+ pszErrorMsg = "Out of memory"; break;
+ case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
+ pszErrorMsg = "Out of memory while flushing bits"; break;
+ case VP8_ENC_ERROR_NULL_PARAMETER:
+ pszErrorMsg = "A pointer parameter is NULL"; break;
+ case VP8_ENC_ERROR_INVALID_CONFIGURATION:
+ pszErrorMsg = "Configuration is invalid"; break;
+ case VP8_ENC_ERROR_BAD_DIMENSION:
+ pszErrorMsg = "Picture has invalid width/height"; break;
+ case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
+ pszErrorMsg = "Partition is bigger than 512k. Try using less "
+ "SEGMENTS, or increase PARTITION_LIMIT value";
+ break;
+ case VP8_ENC_ERROR_PARTITION_OVERFLOW:
+ pszErrorMsg = "Partition is bigger than 16M";
+ break;
+ case VP8_ENC_ERROR_BAD_WRITE:
+ pszErrorMsg = "Error while fludshing bytes"; break;
+ case VP8_ENC_ERROR_FILE_TOO_BIG:
+ pszErrorMsg = "File is bigger than 4G"; break;
+ case VP8_ENC_ERROR_USER_ABORT:
+ pszErrorMsg = "User interrupted";
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WebPEncode returned an unknown error code: %d",
+ sp->sPicture.error_code);
+ pszErrorMsg = "Unknown WebP error type.";
+ break;
+ }
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "WebPEncode() failed : %s", pszErrorMsg);
+#else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in WebPEncode()");
+#endif
+ return 0;
+ }
+
+ sp->sPicture.custom_ptr = NULL;
+
+ if (!TIFFFlushData1(tif))
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error flushing TIFF WebP encoder.");
+ return 0;
+ }
+
+ return 1;
+}
+
+static void
+TWebPCleanup(TIFF* tif)
+{
+ WebPState* sp = LState(tif);
+
+ assert(sp != 0);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->state & LSTATE_INIT_ENCODE) {
+ WebPPictureFree(&sp->sPicture);
+ }
+
+ if (sp->psDecoder != NULL) {
+ WebPIDelete(sp->psDecoder);
+ WebPFreeDecBuffer(&sp->sDecBuffer);
+ sp->psDecoder = NULL;
+ sp->last_y = 0;
+ }
+
+ if (sp->pBuffer != NULL) {
+ _TIFFfree(sp->pBuffer);
+ sp->pBuffer = NULL;
+ }
+
+ if (tif->tif_data) {
+ _TIFFfree(tif->tif_data);
+ tif->tif_data = NULL;
+ }
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+TWebPVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "WebPVSetField";
+ WebPState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_WEBP_LEVEL:
+ sp->quality_level = (int) va_arg(ap, int);
+ if( sp->quality_level <= 0 ||
+ sp->quality_level > 100.0f ) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "WEBP_LEVEL should be between 1 and 100");
+ }
+ return 1;
+ case TIFFTAG_WEBP_LOSSLESS:
+ #if WEBP_ENCODER_ABI_VERSION >= 0x0100
+ sp->lossless = va_arg(ap, int);
+ return 1;
+ #else
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Need to upgrade WEBP driver, this version doesn't support "
+ "lossless compression.");
+ return 0;
+ #endif
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+ /*NOTREACHED*/
+}
+
+static int
+TWebPVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ WebPState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_WEBP_LEVEL:
+ *va_arg(ap, int*) = sp->quality_level;
+ break;
+ case TIFFTAG_WEBP_LOSSLESS:
+ *va_arg(ap, int*) = sp->lossless;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return 1;
+}
+
+static const TIFFField TWebPFields[] = {
+ { TIFFTAG_WEBP_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+ TIFF_SETGET_UNDEFINED,
+ FIELD_PSEUDO, TRUE, FALSE, "WEBP quality", NULL },
+ { TIFFTAG_WEBP_LOSSLESS, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+ TIFF_SETGET_UNDEFINED,
+ FIELD_PSEUDO, TRUE, FALSE, "WEBP lossless/lossy", NULL
+ },
+};
+
+int
+TIFFInitWebP(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitWebP";
+ WebPState* sp;
+
+ assert( scheme == COMPRESSION_WEBP );
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if ( !_TIFFMergeFields(tif, TWebPFields, TIFFArrayCount(TWebPFields)) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging WebP codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof(WebPState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = LState(tif);
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = TWebPVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = TWebPVSetField; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->quality_level = 75.0f; /* default comp. level */
+ sp->lossless = 0; /* default to false */
+ sp->state = 0;
+ sp->nSamples = 0;
+ sp->psDecoder = NULL;
+ sp->last_y = 0;
+
+ sp->buffer_offset = 0;
+ sp->pBuffer = NULL;
+
+ /*
+ * Install codec methods.
+ * Notes:
+ * encoderow is not supported
+ */
+ tif->tif_fixuptags = TWebPFixupTags;
+ tif->tif_setupdecode = TWebPSetupDecode;
+ tif->tif_predecode = TWebPPreDecode;
+ tif->tif_decoderow = TWebPDecode;
+ tif->tif_decodestrip = TWebPDecode;
+ tif->tif_decodetile = TWebPDecode;
+ tif->tif_setupencode = TWebPSetupEncode;
+ tif->tif_preencode = TWebPPreEncode;
+ tif->tif_postencode = TWebPPostEncode;
+ tif->tif_encoderow = TWebPEncode;
+ tif->tif_encodestrip = TWebPEncode;
+ tif->tif_encodetile = TWebPEncode;
+ tif->tif_cleanup = TWebPCleanup;
+
+ return 1;
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for WebP state block");
+ return 0;
+}
+
+#endif /* WEBP_SUPPORT */
diff --git a/test/monniaux/tiff-4.0.10/tif_write.c b/test/monniaux/tiff-4.0.10/tif_write.c
new file mode 100644
index 00000000..a31ecd12
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_write.c
@@ -0,0 +1,834 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * Scanline-oriented Write Support
+ */
+#include "tiffiop.h"
+#include <stdio.h>
+
+#define STRIPINCR 20 /* expansion factor on strip array */
+
+#define WRITECHECKSTRIPS(tif, module) \
+ (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
+#define WRITECHECKTILES(tif, module) \
+ (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
+#define BUFFERCHECK(tif) \
+ ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \
+ TIFFWriteBufferSetup((tif), NULL, (tmsize_t) -1))
+
+static int TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module);
+static int TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc);
+
+int
+TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
+{
+ static const char module[] = "TIFFWriteScanline";
+ register TIFFDirectory *td;
+ int status, imagegrew = 0;
+ uint32 strip;
+
+ if (!WRITECHECKSTRIPS(tif, module))
+ return (-1);
+ /*
+ * Handle delayed allocation of data buffer. This
+ * permits it to be sized more intelligently (using
+ * directory information).
+ */
+ if (!BUFFERCHECK(tif))
+ return (-1);
+ tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/
+
+ td = &tif->tif_dir;
+ /*
+ * Extend image length if needed
+ * (but only for PlanarConfig=1).
+ */
+ if (row >= td->td_imagelength) { /* extend image */
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not change \"ImageLength\" when using separate planes");
+ return (-1);
+ }
+ td->td_imagelength = row+1;
+ imagegrew = 1;
+ }
+ /*
+ * Calculate strip and check for crossings.
+ */
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ if (sample >= td->td_samplesperpixel) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%lu: Sample out of range, max %lu",
+ (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
+ return (-1);
+ }
+ strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
+ } else
+ strip = row / td->td_rowsperstrip;
+ /*
+ * Check strip array to make sure there's space. We don't support
+ * dynamically growing files that have data organized in separate
+ * bitplanes because it's too painful. In that case we require that
+ * the imagelength be set properly before the first write (so that the
+ * strips array will be fully allocated above).
+ */
+ if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
+ return (-1);
+ if (strip != tif->tif_curstrip) {
+ /*
+ * Changing strips -- flush any data present.
+ */
+ if (!TIFFFlushData(tif))
+ return (-1);
+ tif->tif_curstrip = strip;
+ /*
+ * Watch out for a growing image. The value of strips/image
+ * will initially be 1 (since it can't be deduced until the
+ * imagelength is known).
+ */
+ if (strip >= td->td_stripsperimage && imagegrew)
+ td->td_stripsperimage =
+ TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
+ if (td->td_stripsperimage == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
+ return (-1);
+ }
+ tif->tif_row =
+ (strip % td->td_stripsperimage) * td->td_rowsperstrip;
+ if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
+ if (!(*tif->tif_setupencode)(tif))
+ return (-1);
+ tif->tif_flags |= TIFF_CODERSETUP;
+ }
+
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+
+ if( td->td_stripbytecount[strip] > 0 )
+ {
+ /* if we are writing over existing tiles, zero length */
+ td->td_stripbytecount[strip] = 0;
+
+ /* this forces TIFFAppendToStrip() to do a seek */
+ tif->tif_curoff = 0;
+ }
+
+ if (!(*tif->tif_preencode)(tif, sample))
+ return (-1);
+ tif->tif_flags |= TIFF_POSTENCODE;
+ }
+ /*
+ * Ensure the write is either sequential or at the
+ * beginning of a strip (or that we can randomly
+ * access the data -- i.e. no encoding).
+ */
+ if (row != tif->tif_row) {
+ if (row < tif->tif_row) {
+ /*
+ * Moving backwards within the same strip:
+ * backup to the start and then decode
+ * forward (below).
+ */
+ tif->tif_row = (strip % td->td_stripsperimage) *
+ td->td_rowsperstrip;
+ tif->tif_rawcp = tif->tif_rawdata;
+ }
+ /*
+ * Seek forward to the desired row.
+ */
+ if (!(*tif->tif_seek)(tif, row - tif->tif_row))
+ return (-1);
+ tif->tif_row = row;
+ }
+
+ /* swab if needed - note that source buffer will be altered */
+ tif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );
+
+ status = (*tif->tif_encoderow)(tif, (uint8*) buf,
+ tif->tif_scanlinesize, sample);
+
+ /* we are now poised at the beginning of the next row */
+ tif->tif_row = row + 1;
+ return (status);
+}
+
+/*
+ * Encode the supplied data and write it to the
+ * specified strip.
+ *
+ * NB: Image length must be setup before writing.
+ */
+tmsize_t
+TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
+{
+ static const char module[] = "TIFFWriteEncodedStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint16 sample;
+
+ if (!WRITECHECKSTRIPS(tif, module))
+ return ((tmsize_t) -1);
+ /*
+ * Check strip array to make sure there's space.
+ * We don't support dynamically growing files that
+ * have data organized in separate bitplanes because
+ * it's too painful. In that case we require that
+ * the imagelength be set properly before the first
+ * write (so that the strips array will be fully
+ * allocated above).
+ */
+ if (strip >= td->td_nstrips) {
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not grow image by strips when using separate planes");
+ return ((tmsize_t) -1);
+ }
+ if (!TIFFGrowStrips(tif, 1, module))
+ return ((tmsize_t) -1);
+ td->td_stripsperimage =
+ TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
+ }
+ /*
+ * Handle delayed allocation of data buffer. This
+ * permits it to be sized according to the directory
+ * info.
+ */
+ if (!BUFFERCHECK(tif))
+ return ((tmsize_t) -1);
+
+ tif->tif_flags |= TIFF_BUF4WRITE;
+ tif->tif_curstrip = strip;
+
+ if (td->td_stripsperimage == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
+ return ((tmsize_t) -1);
+ }
+
+ tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
+ if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
+ if (!(*tif->tif_setupencode)(tif))
+ return ((tmsize_t) -1);
+ tif->tif_flags |= TIFF_CODERSETUP;
+ }
+
+ if( td->td_stripbytecount[strip] > 0 )
+ {
+ /* Make sure that at the first attempt of rewriting the tile, we will have */
+ /* more bytes available in the output buffer than the previous byte count, */
+ /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
+ /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
+ if( tif->tif_rawdatasize <= (tmsize_t)td->td_stripbytecount[strip] )
+ {
+ if( !(TIFFWriteBufferSetup(tif, NULL,
+ (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[strip] + 1), 1024))) )
+ return ((tmsize_t)(-1));
+ }
+
+ /* Force TIFFAppendToStrip() to consider placing data at end
+ of file. */
+ tif->tif_curoff = 0;
+ }
+
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+
+ tif->tif_flags &= ~TIFF_POSTENCODE;
+
+ /* shortcut to avoid an extra memcpy() */
+ if( td->td_compression == COMPRESSION_NONE )
+ {
+ /* swab if needed - note that source buffer will be altered */
+ tif->tif_postdecode( tif, (uint8*) data, cc );
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits((uint8*) data, cc);
+
+ if (cc > 0 &&
+ !TIFFAppendToStrip(tif, strip, (uint8*) data, cc))
+ return ((tmsize_t) -1);
+ return (cc);
+ }
+
+ sample = (uint16)(strip / td->td_stripsperimage);
+ if (!(*tif->tif_preencode)(tif, sample))
+ return ((tmsize_t) -1);
+
+ /* swab if needed - note that source buffer will be altered */
+ tif->tif_postdecode( tif, (uint8*) data, cc );
+
+ if (!(*tif->tif_encodestrip)(tif, (uint8*) data, cc, sample))
+ return ((tmsize_t) -1);
+ if (!(*tif->tif_postencode)(tif))
+ return ((tmsize_t) -1);
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
+ if (tif->tif_rawcc > 0 &&
+ !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
+ return ((tmsize_t) -1);
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+ return (cc);
+}
+
+/*
+ * Write the supplied data to the specified strip.
+ *
+ * NB: Image length must be setup before writing.
+ */
+tmsize_t
+TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
+{
+ static const char module[] = "TIFFWriteRawStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+
+ if (!WRITECHECKSTRIPS(tif, module))
+ return ((tmsize_t) -1);
+ /*
+ * Check strip array to make sure there's space.
+ * We don't support dynamically growing files that
+ * have data organized in separate bitplanes because
+ * it's too painful. In that case we require that
+ * the imagelength be set properly before the first
+ * write (so that the strips array will be fully
+ * allocated above).
+ */
+ if (strip >= td->td_nstrips) {
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Can not grow image by strips when using separate planes");
+ return ((tmsize_t) -1);
+ }
+ /*
+ * Watch out for a growing image. The value of
+ * strips/image will initially be 1 (since it
+ * can't be deduced until the imagelength is known).
+ */
+ if (strip >= td->td_stripsperimage)
+ td->td_stripsperimage =
+ TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
+ if (!TIFFGrowStrips(tif, 1, module))
+ return ((tmsize_t) -1);
+ }
+ tif->tif_curstrip = strip;
+ if (td->td_stripsperimage == 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,"Zero strips per image");
+ return ((tmsize_t) -1);
+ }
+ tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
+ return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ?
+ cc : (tmsize_t) -1);
+}
+
+/*
+ * Write and compress a tile of data. The
+ * tile is selected by the (x,y,z,s) coordinates.
+ */
+tmsize_t
+TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
+{
+ if (!TIFFCheckTile(tif, x, y, z, s))
+ return ((tmsize_t)(-1));
+ /*
+ * NB: A tile size of -1 is used instead of tif_tilesize knowing
+ * that TIFFWriteEncodedTile will clamp this to the tile size.
+ * This is done because the tile size may not be defined until
+ * after the output buffer is setup in TIFFWriteBufferSetup.
+ */
+ return (TIFFWriteEncodedTile(tif,
+ TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
+}
+
+/*
+ * Encode the supplied data and write it to the
+ * specified tile. There must be space for the
+ * data. The function clamps individual writes
+ * to a tile to the tile size, but does not (and
+ * can not) check that multiple writes to the same
+ * tile do not write more than tile size data.
+ *
+ * NB: Image length must be setup before writing; this
+ * interface does not support automatically growing
+ * the image on each write (as TIFFWriteScanline does).
+ */
+tmsize_t
+TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
+{
+ static const char module[] = "TIFFWriteEncodedTile";
+ TIFFDirectory *td;
+ uint16 sample;
+ uint32 howmany32;
+
+ if (!WRITECHECKTILES(tif, module))
+ return ((tmsize_t)(-1));
+ td = &tif->tif_dir;
+ if (tile >= td->td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
+ (unsigned long) tile, (unsigned long) td->td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+ /*
+ * Handle delayed allocation of data buffer. This
+ * permits it to be sized more intelligently (using
+ * directory information).
+ */
+ if (!BUFFERCHECK(tif))
+ return ((tmsize_t)(-1));
+
+ tif->tif_flags |= TIFF_BUF4WRITE;
+ tif->tif_curtile = tile;
+
+ if( td->td_stripbytecount[tile] > 0 )
+ {
+ /* Make sure that at the first attempt of rewriting the tile, we will have */
+ /* more bytes available in the output buffer than the previous byte count, */
+ /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
+ /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
+ if( tif->tif_rawdatasize <= (tmsize_t) td->td_stripbytecount[tile] )
+ {
+ if( !(TIFFWriteBufferSetup(tif, NULL,
+ (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile] + 1), 1024))) )
+ return ((tmsize_t)(-1));
+ }
+
+ /* Force TIFFAppendToStrip() to consider placing data at end
+ of file. */
+ tif->tif_curoff = 0;
+ }
+
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+
+ /*
+ * Compute tiles per row & per column to compute
+ * current row and column
+ */
+ howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
+ if (howmany32 == 0) {
+ TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
+ return ((tmsize_t)(-1));
+ }
+ tif->tif_row = (tile % howmany32) * td->td_tilelength;
+ howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
+ if (howmany32 == 0) {
+ TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
+ return ((tmsize_t)(-1));
+ }
+ tif->tif_col = (tile % howmany32) * td->td_tilewidth;
+
+ if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
+ if (!(*tif->tif_setupencode)(tif))
+ return ((tmsize_t)(-1));
+ tif->tif_flags |= TIFF_CODERSETUP;
+ }
+ tif->tif_flags &= ~TIFF_POSTENCODE;
+
+ /*
+ * Clamp write amount to the tile size. This is mostly
+ * done so that callers can pass in some large number
+ * (e.g. -1) and have the tile size used instead.
+ */
+ if ( cc < 1 || cc > tif->tif_tilesize)
+ cc = tif->tif_tilesize;
+
+ /* shortcut to avoid an extra memcpy() */
+ if( td->td_compression == COMPRESSION_NONE )
+ {
+ /* swab if needed - note that source buffer will be altered */
+ tif->tif_postdecode( tif, (uint8*) data, cc );
+
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits((uint8*) data, cc);
+
+ if (cc > 0 &&
+ !TIFFAppendToStrip(tif, tile, (uint8*) data, cc))
+ return ((tmsize_t) -1);
+ return (cc);
+ }
+
+ sample = (uint16)(tile/td->td_stripsperimage);
+ if (!(*tif->tif_preencode)(tif, sample))
+ return ((tmsize_t)(-1));
+ /* swab if needed - note that source buffer will be altered */
+ tif->tif_postdecode( tif, (uint8*) data, cc );
+
+ if (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))
+ return ((tmsize_t) -1);
+ if (!(*tif->tif_postencode)(tif))
+ return ((tmsize_t)(-1));
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);
+ if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
+ tif->tif_rawdata, tif->tif_rawcc))
+ return ((tmsize_t)(-1));
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+ return (cc);
+}
+
+/*
+ * Write the supplied data to the specified strip.
+ * There must be space for the data; we don't check
+ * if strips overlap!
+ *
+ * NB: Image length must be setup before writing; this
+ * interface does not support automatically growing
+ * the image on each write (as TIFFWriteScanline does).
+ */
+tmsize_t
+TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
+{
+ static const char module[] = "TIFFWriteRawTile";
+
+ if (!WRITECHECKTILES(tif, module))
+ return ((tmsize_t)(-1));
+ if (tile >= tif->tif_dir.td_nstrips) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
+ (unsigned long) tile,
+ (unsigned long) tif->tif_dir.td_nstrips);
+ return ((tmsize_t)(-1));
+ }
+ return (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?
+ cc : (tmsize_t)(-1));
+}
+
+#define isUnspecified(tif, f) \
+ (TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
+
+int
+TIFFSetupStrips(TIFF* tif)
+{
+ TIFFDirectory* td = &tif->tif_dir;
+
+ if (isTiled(tif))
+ td->td_stripsperimage =
+ isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
+ td->td_samplesperpixel : TIFFNumberOfTiles(tif);
+ else
+ td->td_stripsperimage =
+ isUnspecified(tif, FIELD_ROWSPERSTRIP) ?
+ td->td_samplesperpixel : TIFFNumberOfStrips(tif);
+ td->td_nstrips = td->td_stripsperimage;
+ if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
+ td->td_stripsperimage /= td->td_samplesperpixel;
+ td->td_stripoffset = (uint64 *)
+ _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
+ "for \"StripOffsets\" array");
+ td->td_stripbytecount = (uint64 *)
+ _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
+ "for \"StripByteCounts\" array");
+ if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
+ return (0);
+ /*
+ * Place data at the end-of-file
+ * (by setting offsets to zero).
+ */
+ _TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));
+ _TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));
+ TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
+ TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
+ return (1);
+}
+#undef isUnspecified
+
+/*
+ * Verify file is writable and that the directory
+ * information is setup properly. In doing the latter
+ * we also "freeze" the state of the directory so
+ * that important information is not changed.
+ */
+int
+TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
+{
+ if (tif->tif_mode == O_RDONLY) {
+ TIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");
+ return (0);
+ }
+ if (tiles ^ isTiled(tif)) {
+ TIFFErrorExt(tif->tif_clientdata, module, tiles ?
+ "Can not write tiles to a stripped image" :
+ "Can not write scanlines to a tiled image");
+ return (0);
+ }
+
+ _TIFFFillStriles( tif );
+
+ /*
+ * On the first write verify all the required information
+ * has been setup and initialize any data structures that
+ * had to wait until directory information was set.
+ * Note that a lot of our work is assumed to remain valid
+ * because we disallow any of the important parameters
+ * from changing after we start writing (i.e. once
+ * TIFF_BEENWRITING is set, TIFFSetField will only allow
+ * the image's length to be changed).
+ */
+ if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Must set \"ImageWidth\" before writing data");
+ return (0);
+ }
+ if (tif->tif_dir.td_samplesperpixel == 1) {
+ /*
+ * Planarconfiguration is irrelevant in case of single band
+ * images and need not be included. We will set it anyway,
+ * because this field is used in other parts of library even
+ * in the single band case.
+ */
+ if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
+ tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
+ } else {
+ if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Must set \"PlanarConfiguration\" before writing data");
+ return (0);
+ }
+ }
+ if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
+ tif->tif_dir.td_nstrips = 0;
+ TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",
+ isTiled(tif) ? "tile" : "strip");
+ return (0);
+ }
+ if (isTiled(tif))
+ {
+ tif->tif_tilesize = TIFFTileSize(tif);
+ if (tif->tif_tilesize == 0)
+ return (0);
+ }
+ else
+ tif->tif_tilesize = (tmsize_t)(-1);
+ tif->tif_scanlinesize = TIFFScanlineSize(tif);
+ if (tif->tif_scanlinesize == 0)
+ return (0);
+ tif->tif_flags |= TIFF_BEENWRITING;
+ return (1);
+}
+
+/*
+ * Setup the raw data buffer used for encoding.
+ */
+int
+TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size)
+{
+ static const char module[] = "TIFFWriteBufferSetup";
+
+ if (tif->tif_rawdata) {
+ if (tif->tif_flags & TIFF_MYBUFFER) {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ }
+ tif->tif_rawdata = NULL;
+ }
+ if (size == (tmsize_t)(-1)) {
+ size = (isTiled(tif) ?
+ tif->tif_tilesize : TIFFStripSize(tif));
+ /*
+ * Make raw data buffer at least 8K
+ */
+ if (size < 8*1024)
+ size = 8*1024;
+ bp = NULL; /* NB: force malloc */
+ }
+ if (bp == NULL) {
+ bp = _TIFFmalloc(size);
+ if (bp == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, module, "No space for output buffer");
+ return (0);
+ }
+ tif->tif_flags |= TIFF_MYBUFFER;
+ } else
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ tif->tif_rawdata = (uint8*) bp;
+ tif->tif_rawdatasize = size;
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+ tif->tif_flags |= TIFF_BUFFERSETUP;
+ return (1);
+}
+
+/*
+ * Grow the strip data structures by delta strips.
+ */
+static int
+TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64* new_stripoffset;
+ uint64* new_stripbytecount;
+
+ assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
+ new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset,
+ (td->td_nstrips + delta) * sizeof (uint64));
+ new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount,
+ (td->td_nstrips + delta) * sizeof (uint64));
+ if (new_stripoffset == NULL || new_stripbytecount == NULL) {
+ if (new_stripoffset)
+ _TIFFfree(new_stripoffset);
+ if (new_stripbytecount)
+ _TIFFfree(new_stripbytecount);
+ td->td_nstrips = 0;
+ TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
+ return (0);
+ }
+ td->td_stripoffset = new_stripoffset;
+ td->td_stripbytecount = new_stripbytecount;
+ _TIFFmemset(td->td_stripoffset + td->td_nstrips,
+ 0, delta*sizeof (uint64));
+ _TIFFmemset(td->td_stripbytecount + td->td_nstrips,
+ 0, delta*sizeof (uint64));
+ td->td_nstrips += delta;
+ tif->tif_flags |= TIFF_DIRTYDIRECT;
+
+ return (1);
+}
+
+/*
+ * Append the data to the specified strip.
+ */
+static int
+TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc)
+{
+ static const char module[] = "TIFFAppendToStrip";
+ TIFFDirectory *td = &tif->tif_dir;
+ uint64 m;
+ int64 old_byte_count = -1;
+
+ if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
+ assert(td->td_nstrips > 0);
+
+ if( td->td_stripbytecount[strip] != 0
+ && td->td_stripoffset[strip] != 0
+ && td->td_stripbytecount[strip] >= (uint64) cc )
+ {
+ /*
+ * There is already tile data on disk, and the new tile
+ * data we have will fit in the same space. The only
+ * aspect of this that is risky is that there could be
+ * more data to append to this strip before we are done
+ * depending on how we are getting called.
+ */
+ if (!SeekOK(tif, td->td_stripoffset[strip])) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at scanline %lu",
+ (unsigned long)tif->tif_row);
+ return (0);
+ }
+ }
+ else
+ {
+ /*
+ * Seek to end of file, and set that as our location to
+ * write this strip.
+ */
+ td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
+ tif->tif_flags |= TIFF_DIRTYSTRIP;
+ }
+
+ tif->tif_curoff = td->td_stripoffset[strip];
+
+ /*
+ * We are starting a fresh strip/tile, so set the size to zero.
+ */
+ old_byte_count = td->td_stripbytecount[strip];
+ td->td_stripbytecount[strip] = 0;
+ }
+
+ m = tif->tif_curoff+cc;
+ if (!(tif->tif_flags&TIFF_BIGTIFF))
+ m = (uint32)m;
+ if ((m<tif->tif_curoff)||(m<(uint64)cc))
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "Maximum TIFF file size exceeded");
+ return (0);
+ }
+ if (!WriteOK(tif, data, cc)) {
+ TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
+ (unsigned long) tif->tif_row);
+ return (0);
+ }
+ tif->tif_curoff = m;
+ td->td_stripbytecount[strip] += cc;
+
+ if( (int64) td->td_stripbytecount[strip] != old_byte_count )
+ tif->tif_flags |= TIFF_DIRTYSTRIP;
+
+ return (1);
+}
+
+/*
+ * Internal version of TIFFFlushData that can be
+ * called by ``encodestrip routines'' w/o concern
+ * for infinite recursion.
+ */
+int
+TIFFFlushData1(TIFF* tif)
+{
+ if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {
+ if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits((uint8*)tif->tif_rawdata,
+ tif->tif_rawcc);
+ if (!TIFFAppendToStrip(tif,
+ isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
+ tif->tif_rawdata, tif->tif_rawcc))
+ {
+ /* We update those variables even in case of error since there's */
+ /* code that doesn't really check the return code of this */
+ /* function */
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+ return (0);
+ }
+ tif->tif_rawcc = 0;
+ tif->tif_rawcp = tif->tif_rawdata;
+ }
+ return (1);
+}
+
+/*
+ * Set the current write offset. This should only be
+ * used to set the offset to a known previous location
+ * (very carefully), or to 0 so that the next write gets
+ * appended to the end of the file.
+ */
+void
+TIFFSetWriteOffset(TIFF* tif, toff_t off)
+{
+ tif->tif_curoff = off;
+}
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_zip.c b/test/monniaux/tiff-4.0.10/tif_zip.c
new file mode 100644
index 00000000..9d4bceb1
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_zip.c
@@ -0,0 +1,474 @@
+/*
+ * Copyright (c) 1995-1997 Sam Leffler
+ * Copyright (c) 1995-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include "tiffiop.h"
+#ifdef ZIP_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * ZIP (aka Deflate) Compression Support
+ *
+ * This file is simply an interface to the zlib library written by
+ * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
+ * of the library: this code assumes the 1.0 API and also depends on
+ * the ability to write the zlib header multiple times (one per strip)
+ * which was not possible with versions prior to 0.95. Note also that
+ * older versions of this codec avoided this bug by suppressing the header
+ * entirely. This means that files written with the old library cannot
+ * be read; they should be converted to a different compression scheme
+ * and then reconverted.
+ *
+ * The data format used by the zlib library is described in the files
+ * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
+ * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
+ * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
+ */
+#include "tif_predict.h"
+#include "zlib.h"
+
+#include <stdio.h>
+
+/*
+ * Sigh, ZLIB_VERSION is defined as a string so there's no
+ * way to do a proper check here. Instead we guess based
+ * on the presence of #defines that were added between the
+ * 0.95 and 1.0 distributions.
+ */
+#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
+#error "Antiquated ZLIB software; you must use version 1.0 or later"
+#endif
+
+#define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
+
+/*
+ * State block for each open TIFF
+ * file using ZIP compression/decompression.
+ */
+typedef struct {
+ TIFFPredictorState predict;
+ z_stream stream;
+ int zipquality; /* compression level */
+ int state; /* state flags */
+#define ZSTATE_INIT_DECODE 0x01
+#define ZSTATE_INIT_ENCODE 0x02
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+} ZIPState;
+
+#define ZState(tif) ((ZIPState*) (tif)->tif_data)
+#define DecoderState(tif) ZState(tif)
+#define EncoderState(tif) ZState(tif)
+
+static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static int
+ZIPFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return (1);
+}
+
+static int
+ZIPSetupDecode(TIFF* tif)
+{
+ static const char module[] = "ZIPSetupDecode";
+ ZIPState* sp = DecoderState(tif);
+
+ assert(sp != NULL);
+
+ /* if we were last encoding, terminate this mode */
+ if (sp->state & ZSTATE_INIT_ENCODE) {
+ deflateEnd(&sp->stream);
+ sp->state = 0;
+ }
+
+ /* This function can possibly be called several times by */
+ /* PredictorSetupDecode() if this function succeeds but */
+ /* PredictorSetup() fails */
+ if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
+ inflateInit(&sp->stream) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
+ return (0);
+ } else {
+ sp->state |= ZSTATE_INIT_DECODE;
+ return (1);
+ }
+}
+
+/*
+ * Setup state for decoding a strip.
+ */
+static int
+ZIPPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "ZIPPreDecode";
+ ZIPState* sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+
+ if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
+ tif->tif_setupdecode( tif );
+
+ sp->stream.next_in = tif->tif_rawdata;
+ assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_in = (uInt) tif->tif_rawcc;
+ if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ return (inflateReset(&sp->stream) == Z_OK);
+}
+
+static int
+ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "ZIPDecode";
+ ZIPState* sp = DecoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ assert(sp->state == ZSTATE_INIT_DECODE);
+
+ sp->stream.next_in = tif->tif_rawcp;
+ sp->stream.avail_in = (uInt) tif->tif_rawcc;
+
+ sp->stream.next_out = op;
+ assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_out = (uInt) occ;
+ if ((tmsize_t)sp->stream.avail_out != occ)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ do {
+ int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
+ if (state == Z_STREAM_END)
+ break;
+ if (state == Z_DATA_ERROR) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Decoding error at scanline %lu, %s",
+ (unsigned long) tif->tif_row, SAFE_MSG(sp));
+ if (inflateSync(&sp->stream) != Z_OK)
+ return (0);
+ continue;
+ }
+ if (state != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "ZLib error: %s", SAFE_MSG(sp));
+ return (0);
+ }
+ } while (sp->stream.avail_out > 0);
+ if (sp->stream.avail_out != 0) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
+ (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
+ return (0);
+ }
+
+ tif->tif_rawcp = sp->stream.next_in;
+ tif->tif_rawcc = sp->stream.avail_in;
+
+ return (1);
+}
+
+static int
+ZIPSetupEncode(TIFF* tif)
+{
+ static const char module[] = "ZIPSetupEncode";
+ ZIPState* sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ if (sp->state & ZSTATE_INIT_DECODE) {
+ inflateEnd(&sp->stream);
+ sp->state = 0;
+ }
+
+ if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
+ return (0);
+ } else {
+ sp->state |= ZSTATE_INIT_ENCODE;
+ return (1);
+ }
+}
+
+/*
+ * Reset encoding state at the start of a strip.
+ */
+static int
+ZIPPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "ZIPPreEncode";
+ ZIPState *sp = EncoderState(tif);
+
+ (void) s;
+ assert(sp != NULL);
+ if( sp->state != ZSTATE_INIT_ENCODE )
+ tif->tif_setupencode( tif );
+
+ sp->stream.next_out = tif->tif_rawdata;
+ assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ return (deflateReset(&sp->stream) == Z_OK);
+}
+
+/*
+ * Encode a chunk of pixels.
+ */
+static int
+ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "ZIPEncode";
+ ZIPState *sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ assert(sp->state == ZSTATE_INIT_ENCODE);
+
+ (void) s;
+ sp->stream.next_in = bp;
+ assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
+ we need to simplify this code to reflect a ZLib that is likely updated
+ to deal with 8byte memory sizes, though this code will respond
+ appropriately even before we simplify it */
+ sp->stream.avail_in = (uInt) cc;
+ if ((tmsize_t)sp->stream.avail_in != cc)
+ {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+ return (0);
+ }
+ do {
+ if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Encoder error: %s",
+ SAFE_MSG(sp));
+ return (0);
+ }
+ if (sp->stream.avail_out == 0) {
+ tif->tif_rawcc = tif->tif_rawdatasize;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
+ }
+ } while (sp->stream.avail_in > 0);
+ return (1);
+}
+
+/*
+ * Finish off an encoded strip by flushing the last
+ * string and tacking on an End Of Information code.
+ */
+static int
+ZIPPostEncode(TIFF* tif)
+{
+ static const char module[] = "ZIPPostEncode";
+ ZIPState *sp = EncoderState(tif);
+ int state;
+
+ sp->stream.avail_in = 0;
+ do {
+ state = deflate(&sp->stream, Z_FINISH);
+ switch (state) {
+ case Z_STREAM_END:
+ case Z_OK:
+ if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
+ {
+ tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
+ TIFFFlushData1(tif);
+ sp->stream.next_out = tif->tif_rawdata;
+ sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
+ }
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "ZLib error: %s", SAFE_MSG(sp));
+ return (0);
+ }
+ } while (state != Z_STREAM_END);
+ return (1);
+}
+
+static void
+ZIPCleanup(TIFF* tif)
+{
+ ZIPState* sp = ZState(tif);
+
+ assert(sp != 0);
+
+ (void)TIFFPredictorCleanup(tif);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->state & ZSTATE_INIT_ENCODE) {
+ deflateEnd(&sp->stream);
+ sp->state = 0;
+ } else if( sp->state & ZSTATE_INIT_DECODE) {
+ inflateEnd(&sp->stream);
+ sp->state = 0;
+ }
+ _TIFFfree(sp);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "ZIPVSetField";
+ ZIPState* sp = ZState(tif);
+
+ switch (tag) {
+ case TIFFTAG_ZIPQUALITY:
+ sp->zipquality = (int) va_arg(ap, int);
+ if ( sp->state&ZSTATE_INIT_ENCODE ) {
+ if (deflateParams(&sp->stream,
+ sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
+ TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+ SAFE_MSG(sp));
+ return (0);
+ }
+ }
+ return (1);
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+ /*NOTREACHED*/
+}
+
+static int
+ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ ZIPState* sp = ZState(tif);
+
+ switch (tag) {
+ case TIFFTAG_ZIPQUALITY:
+ *va_arg(ap, int*) = sp->zipquality;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return (1);
+}
+
+static const TIFFField zipFields[] = {
+ { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
+};
+
+int
+TIFFInitZIP(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitZIP";
+ ZIPState* sp;
+
+ assert( (scheme == COMPRESSION_DEFLATE)
+ || (scheme == COMPRESSION_ADOBE_DEFLATE));
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging Deflate codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = ZState(tif);
+ sp->stream.zalloc = NULL;
+ sp->stream.zfree = NULL;
+ sp->stream.opaque = NULL;
+ sp->stream.data_type = Z_BINARY;
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
+ sp->state = 0;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = ZIPFixupTags;
+ tif->tif_setupdecode = ZIPSetupDecode;
+ tif->tif_predecode = ZIPPreDecode;
+ tif->tif_decoderow = ZIPDecode;
+ tif->tif_decodestrip = ZIPDecode;
+ tif->tif_decodetile = ZIPDecode;
+ tif->tif_setupencode = ZIPSetupEncode;
+ tif->tif_preencode = ZIPPreEncode;
+ tif->tif_postencode = ZIPPostEncode;
+ tif->tif_encoderow = ZIPEncode;
+ tif->tif_encodestrip = ZIPEncode;
+ tif->tif_encodetile = ZIPEncode;
+ tif->tif_cleanup = ZIPCleanup;
+ /*
+ * Setup predictor setup.
+ */
+ (void) TIFFPredictorInit(tif);
+ return (1);
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for ZIP state block");
+ return (0);
+}
+#endif /* ZIP_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tif_zstd.c b/test/monniaux/tiff-4.0.10/tif_zstd.c
new file mode 100644
index 00000000..21c935e2
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tif_zstd.c
@@ -0,0 +1,440 @@
+/*
+* Copyright (c) 2017, Planet Labs
+* Author: <even.rouault at spatialys.com>
+*
+* Permission to use, copy, modify, distribute, and sell this software and
+* its documentation for any purpose is hereby granted without fee, provided
+* that (i) the above copyright notices and this permission notice appear in
+* all copies of the software and related documentation, and (ii) the names of
+* Sam Leffler and Silicon Graphics may not be used in any advertising or
+* publicity relating to the software without the specific, prior written
+* permission of Sam Leffler and Silicon Graphics.
+*
+* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+*
+* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+* OF THIS SOFTWARE.
+*/
+
+#include "tiffiop.h"
+#ifdef ZSTD_SUPPORT
+/*
+* TIFF Library.
+*
+* ZSTD Compression Support
+*
+*/
+
+#include "tif_predict.h"
+#include "zstd.h"
+
+#include <stdio.h>
+
+/*
+* State block for each open TIFF file using ZSTD compression/decompression.
+*/
+typedef struct {
+ TIFFPredictorState predict;
+ ZSTD_DStream* dstream;
+ ZSTD_CStream* cstream;
+ int compression_level; /* compression level */
+ ZSTD_outBuffer out_buffer;
+ int state; /* state flags */
+#define LSTATE_INIT_DECODE 0x01
+#define LSTATE_INIT_ENCODE 0x02
+
+ TIFFVGetMethod vgetparent; /* super-class method */
+ TIFFVSetMethod vsetparent; /* super-class method */
+} ZSTDState;
+
+#define LState(tif) ((ZSTDState*) (tif)->tif_data)
+#define DecoderState(tif) LState(tif)
+#define EncoderState(tif) LState(tif)
+
+static int ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static int
+ZSTDFixupTags(TIFF* tif)
+{
+ (void) tif;
+ return 1;
+}
+
+static int
+ZSTDSetupDecode(TIFF* tif)
+{
+ ZSTDState* sp = DecoderState(tif);
+
+ assert(sp != NULL);
+
+ /* if we were last encoding, terminate this mode */
+ if (sp->state & LSTATE_INIT_ENCODE) {
+ ZSTD_freeCStream(sp->cstream);
+ sp->cstream = NULL;
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_DECODE;
+ return 1;
+}
+
+/*
+* Setup state for decoding a strip.
+*/
+static int
+ZSTDPreDecode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "ZSTDPreDecode";
+ ZSTDState* sp = DecoderState(tif);
+ size_t zstd_ret;
+
+ (void) s;
+ assert(sp != NULL);
+
+ if( (sp->state & LSTATE_INIT_DECODE) == 0 )
+ tif->tif_setupdecode(tif);
+
+ if( sp->dstream )
+ {
+ ZSTD_freeDStream(sp->dstream);
+ sp->dstream = NULL;
+ }
+
+ sp->dstream = ZSTD_createDStream();
+ if( sp->dstream == NULL ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot allocate decompression stream");
+ return 0;
+ }
+ zstd_ret = ZSTD_initDStream(sp->dstream);
+ if( ZSTD_isError(zstd_ret) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in ZSTD_initDStream(): %s",
+ ZSTD_getErrorName(zstd_ret));
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+ static const char module[] = "ZSTDDecode";
+ ZSTDState* sp = DecoderState(tif);
+ ZSTD_inBuffer in_buffer;
+ ZSTD_outBuffer out_buffer;
+ size_t zstd_ret;
+
+ (void) s;
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_DECODE);
+
+ in_buffer.src = tif->tif_rawcp;
+ in_buffer.size = (size_t) tif->tif_rawcc;
+ in_buffer.pos = 0;
+
+ out_buffer.dst = op;
+ out_buffer.size = (size_t) occ;
+ out_buffer.pos = 0;
+
+ do {
+ zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer,
+ &in_buffer);
+ if( ZSTD_isError(zstd_ret) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in ZSTD_decompressStream(): %s",
+ ZSTD_getErrorName(zstd_ret));
+ return 0;
+ }
+ } while( zstd_ret != 0 &&
+ in_buffer.pos < in_buffer.size &&
+ out_buffer.pos < out_buffer.size );
+
+ if (out_buffer.pos < (size_t)occ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Not enough data at scanline %lu (short %lu bytes)",
+ (unsigned long) tif->tif_row,
+ (unsigned long) (size_t)occ - out_buffer.pos);
+ return 0;
+ }
+
+ tif->tif_rawcp += in_buffer.pos;
+ tif->tif_rawcc -= in_buffer.pos;
+
+ return 1;
+}
+
+static int
+ZSTDSetupEncode(TIFF* tif)
+{
+ ZSTDState* sp = EncoderState(tif);
+
+ assert(sp != NULL);
+ if (sp->state & LSTATE_INIT_DECODE) {
+ ZSTD_freeDStream(sp->dstream);
+ sp->dstream = NULL;
+ sp->state = 0;
+ }
+
+ sp->state |= LSTATE_INIT_ENCODE;
+ return 1;
+}
+
+/*
+* Reset encoding state at the start of a strip.
+*/
+static int
+ZSTDPreEncode(TIFF* tif, uint16 s)
+{
+ static const char module[] = "ZSTDPreEncode";
+ ZSTDState *sp = EncoderState(tif);
+ size_t zstd_ret;
+
+ (void) s;
+ assert(sp != NULL);
+ if( sp->state != LSTATE_INIT_ENCODE )
+ tif->tif_setupencode(tif);
+
+ if (sp->cstream) {
+ ZSTD_freeCStream(sp->cstream);
+ sp->cstream = NULL;
+ }
+ sp->cstream = ZSTD_createCStream();
+ if( sp->cstream == NULL ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Cannot allocate compression stream");
+ return 0;
+ }
+
+ zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
+ if( ZSTD_isError(zstd_ret) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in ZSTD_initCStream(): %s",
+ ZSTD_getErrorName(zstd_ret));
+ return 0;
+ }
+
+ sp->out_buffer.dst = tif->tif_rawdata;
+ sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
+ sp->out_buffer.pos = 0;
+
+ return 1;
+}
+
+/*
+* Encode a chunk of pixels.
+*/
+static int
+ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+ static const char module[] = "ZSTDEncode";
+ ZSTDState *sp = EncoderState(tif);
+ ZSTD_inBuffer in_buffer;
+ size_t zstd_ret;
+
+ assert(sp != NULL);
+ assert(sp->state == LSTATE_INIT_ENCODE);
+
+ (void) s;
+
+ in_buffer.src = bp;
+ in_buffer.size = (size_t)cc;
+ in_buffer.pos = 0;
+
+ do {
+ zstd_ret = ZSTD_compressStream(sp->cstream, &sp->out_buffer,
+ &in_buffer);
+ if( ZSTD_isError(zstd_ret) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in ZSTD_compressStream(): %s",
+ ZSTD_getErrorName(zstd_ret));
+ return 0;
+ }
+ if( sp->out_buffer.pos == sp->out_buffer.size ) {
+ tif->tif_rawcc = tif->tif_rawdatasize;
+ TIFFFlushData1(tif);
+ sp->out_buffer.dst = tif->tif_rawcp;
+ sp->out_buffer.pos = 0;
+ }
+ } while( in_buffer.pos < in_buffer.size );
+
+ return 1;
+}
+
+/*
+* Finish off an encoded strip by flushing it.
+*/
+static int
+ZSTDPostEncode(TIFF* tif)
+{
+ static const char module[] = "ZSTDPostEncode";
+ ZSTDState *sp = EncoderState(tif);
+ size_t zstd_ret;
+
+ do {
+ zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
+ if( ZSTD_isError(zstd_ret) ) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error in ZSTD_endStream(): %s",
+ ZSTD_getErrorName(zstd_ret));
+ return 0;
+ }
+ if( sp->out_buffer.pos > 0 ) {
+ tif->tif_rawcc = sp->out_buffer.pos;
+ TIFFFlushData1(tif);
+ sp->out_buffer.dst = tif->tif_rawcp;
+ sp->out_buffer.pos = 0;
+ }
+ } while (zstd_ret != 0);
+ return 1;
+}
+
+static void
+ZSTDCleanup(TIFF* tif)
+{
+ ZSTDState* sp = LState(tif);
+
+ assert(sp != 0);
+
+ (void)TIFFPredictorCleanup(tif);
+
+ tif->tif_tagmethods.vgetfield = sp->vgetparent;
+ tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+ if (sp->dstream) {
+ ZSTD_freeDStream(sp->dstream);
+ sp->dstream = NULL;
+ }
+ if (sp->cstream) {
+ ZSTD_freeCStream(sp->cstream);
+ sp->cstream = NULL;
+ }
+ _TIFFfree(sp);
+ tif->tif_data = NULL;
+
+ _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+ZSTDVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ static const char module[] = "ZSTDVSetField";
+ ZSTDState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_ZSTD_LEVEL:
+ sp->compression_level = (int) va_arg(ap, int);
+ if( sp->compression_level <= 0 ||
+ sp->compression_level > ZSTD_maxCLevel() )
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "ZSTD_LEVEL should be between 1 and %d",
+ ZSTD_maxCLevel());
+ }
+ return 1;
+ default:
+ return (*sp->vsetparent)(tif, tag, ap);
+ }
+ /*NOTREACHED*/
+}
+
+static int
+ZSTDVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+ ZSTDState* sp = LState(tif);
+
+ switch (tag) {
+ case TIFFTAG_ZSTD_LEVEL:
+ *va_arg(ap, int*) = sp->compression_level;
+ break;
+ default:
+ return (*sp->vgetparent)(tif, tag, ap);
+ }
+ return 1;
+}
+
+static const TIFFField ZSTDFields[] = {
+ { TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+ TIFF_SETGET_UNDEFINED,
+ FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level", NULL },
+};
+
+int
+TIFFInitZSTD(TIFF* tif, int scheme)
+{
+ static const char module[] = "TIFFInitZSTD";
+ ZSTDState* sp;
+
+ assert( scheme == COMPRESSION_ZSTD );
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging ZSTD codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Allocate state block so tag methods have storage to record values.
+ */
+ tif->tif_data = (uint8*) _TIFFmalloc(sizeof(ZSTDState));
+ if (tif->tif_data == NULL)
+ goto bad;
+ sp = LState(tif);
+
+ /*
+ * Override parent get/set field methods.
+ */
+ sp->vgetparent = tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
+ sp->vsetparent = tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
+
+ /* Default values for codec-specific fields */
+ sp->compression_level = 9; /* default comp. level */
+ sp->state = 0;
+ sp->dstream = 0;
+ sp->cstream = 0;
+ sp->out_buffer.dst = NULL;
+ sp->out_buffer.size = 0;
+ sp->out_buffer.pos = 0;
+
+ /*
+ * Install codec methods.
+ */
+ tif->tif_fixuptags = ZSTDFixupTags;
+ tif->tif_setupdecode = ZSTDSetupDecode;
+ tif->tif_predecode = ZSTDPreDecode;
+ tif->tif_decoderow = ZSTDDecode;
+ tif->tif_decodestrip = ZSTDDecode;
+ tif->tif_decodetile = ZSTDDecode;
+ tif->tif_setupencode = ZSTDSetupEncode;
+ tif->tif_preencode = ZSTDPreEncode;
+ tif->tif_postencode = ZSTDPostEncode;
+ tif->tif_encoderow = ZSTDEncode;
+ tif->tif_encodestrip = ZSTDEncode;
+ tif->tif_encodetile = ZSTDEncode;
+ tif->tif_cleanup = ZSTDCleanup;
+ /*
+ * Setup predictor setup.
+ */
+ (void) TIFFPredictorInit(tif);
+ return 1;
+bad:
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for ZSTD state block");
+ return 0;
+}
+#endif /* ZSTD_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
diff --git a/test/monniaux/tiff-4.0.10/tiff.h b/test/monniaux/tiff-4.0.10/tiff.h
new file mode 100644
index 00000000..5b0a0c90
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiff.h
@@ -0,0 +1,695 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _TIFF_
+#define _TIFF_
+
+#include "tiffconf.h"
+
+/*
+ * Tag Image File Format (TIFF)
+ *
+ * Based on Rev 6.0 from:
+ * Developer's Desk
+ * Aldus Corporation
+ * 411 First Ave. South
+ * Suite 200
+ * Seattle, WA 98104
+ * 206-622-5500
+ *
+ * (http://partners.adobe.com/asn/developer/PDFS/TN/TIFF6.pdf)
+ *
+ * For BigTIFF design notes see the following links
+ * http://www.remotesensing.org/libtiff/bigtiffdesign.html
+ * http://www.awaresystems.be/imaging/tiff/bigtiff.html
+ */
+
+#define TIFF_VERSION_CLASSIC 42
+#define TIFF_VERSION_BIG 43
+
+#define TIFF_BIGENDIAN 0x4d4d
+#define TIFF_LITTLEENDIAN 0x4949
+#define MDI_LITTLEENDIAN 0x5045
+#define MDI_BIGENDIAN 0x4550
+
+/*
+ * Intrinsic data types required by the file format:
+ *
+ * 8-bit quantities int8/uint8
+ * 16-bit quantities int16/uint16
+ * 32-bit quantities int32/uint32
+ * 64-bit quantities int64/uint64
+ * strings unsigned char*
+ */
+
+typedef TIFF_INT8_T int8;
+typedef TIFF_UINT8_T uint8;
+
+typedef TIFF_INT16_T int16;
+typedef TIFF_UINT16_T uint16;
+
+typedef TIFF_INT32_T int32;
+typedef TIFF_UINT32_T uint32;
+
+typedef TIFF_INT64_T int64;
+typedef TIFF_UINT64_T uint64;
+
+/*
+ * Some types as promoted in a variable argument list
+ * We use uint16_vap rather then directly using int, because this way
+ * we document the type we actually want to pass through, conceptually,
+ * rather then confusing the issue by merely stating the type it gets
+ * promoted to
+ */
+
+typedef int uint16_vap;
+
+/*
+ * TIFF header.
+ */
+typedef struct {
+ uint16 tiff_magic; /* magic number (defines byte order) */
+ uint16 tiff_version; /* TIFF version number */
+} TIFFHeaderCommon;
+typedef struct {
+ uint16 tiff_magic; /* magic number (defines byte order) */
+ uint16 tiff_version; /* TIFF version number */
+ uint32 tiff_diroff; /* byte offset to first directory */
+} TIFFHeaderClassic;
+typedef struct {
+ uint16 tiff_magic; /* magic number (defines byte order) */
+ uint16 tiff_version; /* TIFF version number */
+ uint16 tiff_offsetsize; /* size of offsets, should be 8 */
+ uint16 tiff_unused; /* unused word, should be 0 */
+ uint64 tiff_diroff; /* byte offset to first directory */
+} TIFFHeaderBig;
+
+
+/*
+ * NB: In the comments below,
+ * - items marked with a + are obsoleted by revision 5.0,
+ * - items marked with a ! are introduced in revision 6.0.
+ * - items marked with a % are introduced post revision 6.0.
+ * - items marked with a $ are obsoleted by revision 6.0.
+ * - items marked with a & are introduced by Adobe DNG specification.
+ */
+
+/*
+ * Tag data type information.
+ *
+ * Note: RATIONALs are the ratio of two 32-bit integer values.
+ */
+typedef enum {
+ TIFF_NOTYPE = 0, /* placeholder */
+ TIFF_BYTE = 1, /* 8-bit unsigned integer */
+ TIFF_ASCII = 2, /* 8-bit bytes w/ last byte null */
+ TIFF_SHORT = 3, /* 16-bit unsigned integer */
+ TIFF_LONG = 4, /* 32-bit unsigned integer */
+ TIFF_RATIONAL = 5, /* 64-bit unsigned fraction */
+ TIFF_SBYTE = 6, /* !8-bit signed integer */
+ TIFF_UNDEFINED = 7, /* !8-bit untyped data */
+ TIFF_SSHORT = 8, /* !16-bit signed integer */
+ TIFF_SLONG = 9, /* !32-bit signed integer */
+ TIFF_SRATIONAL = 10, /* !64-bit signed fraction */
+ TIFF_FLOAT = 11, /* !32-bit IEEE floating point */
+ TIFF_DOUBLE = 12, /* !64-bit IEEE floating point */
+ TIFF_IFD = 13, /* %32-bit unsigned integer (offset) */
+ TIFF_LONG8 = 16, /* BigTIFF 64-bit unsigned integer */
+ TIFF_SLONG8 = 17, /* BigTIFF 64-bit signed integer */
+ TIFF_IFD8 = 18 /* BigTIFF 64-bit unsigned integer (offset) */
+} TIFFDataType;
+
+/*
+ * TIFF Tag Definitions.
+ */
+#define TIFFTAG_SUBFILETYPE 254 /* subfile data descriptor */
+#define FILETYPE_REDUCEDIMAGE 0x1 /* reduced resolution version */
+#define FILETYPE_PAGE 0x2 /* one page of many */
+#define FILETYPE_MASK 0x4 /* transparency mask */
+#define TIFFTAG_OSUBFILETYPE 255 /* +kind of data in subfile */
+#define OFILETYPE_IMAGE 1 /* full resolution image data */
+#define OFILETYPE_REDUCEDIMAGE 2 /* reduced size image data */
+#define OFILETYPE_PAGE 3 /* one page of many */
+#define TIFFTAG_IMAGEWIDTH 256 /* image width in pixels */
+#define TIFFTAG_IMAGELENGTH 257 /* image height in pixels */
+#define TIFFTAG_BITSPERSAMPLE 258 /* bits per channel (sample) */
+#define TIFFTAG_COMPRESSION 259 /* data compression technique */
+#define COMPRESSION_NONE 1 /* dump mode */
+#define COMPRESSION_CCITTRLE 2 /* CCITT modified Huffman RLE */
+#define COMPRESSION_CCITTFAX3 3 /* CCITT Group 3 fax encoding */
+#define COMPRESSION_CCITT_T4 3 /* CCITT T.4 (TIFF 6 name) */
+#define COMPRESSION_CCITTFAX4 4 /* CCITT Group 4 fax encoding */
+#define COMPRESSION_CCITT_T6 4 /* CCITT T.6 (TIFF 6 name) */
+#define COMPRESSION_LZW 5 /* Lempel-Ziv & Welch */
+#define COMPRESSION_OJPEG 6 /* !6.0 JPEG */
+#define COMPRESSION_JPEG 7 /* %JPEG DCT compression */
+#define COMPRESSION_T85 9 /* !TIFF/FX T.85 JBIG compression */
+#define COMPRESSION_T43 10 /* !TIFF/FX T.43 colour by layered JBIG compression */
+#define COMPRESSION_NEXT 32766 /* NeXT 2-bit RLE */
+#define COMPRESSION_CCITTRLEW 32771 /* #1 w/ word alignment */
+#define COMPRESSION_PACKBITS 32773 /* Macintosh RLE */
+#define COMPRESSION_THUNDERSCAN 32809 /* ThunderScan RLE */
+/* codes 32895-32898 are reserved for ANSI IT8 TIFF/IT <dkelly@apago.com) */
+#define COMPRESSION_IT8CTPAD 32895 /* IT8 CT w/padding */
+#define COMPRESSION_IT8LW 32896 /* IT8 Linework RLE */
+#define COMPRESSION_IT8MP 32897 /* IT8 Monochrome picture */
+#define COMPRESSION_IT8BL 32898 /* IT8 Binary line art */
+/* compression codes 32908-32911 are reserved for Pixar */
+#define COMPRESSION_PIXARFILM 32908 /* Pixar companded 10bit LZW */
+#define COMPRESSION_PIXARLOG 32909 /* Pixar companded 11bit ZIP */
+#define COMPRESSION_DEFLATE 32946 /* Deflate compression */
+#define COMPRESSION_ADOBE_DEFLATE 8 /* Deflate compression,
+ as recognized by Adobe */
+/* compression code 32947 is reserved for Oceana Matrix <dev@oceana.com> */
+#define COMPRESSION_DCS 32947 /* Kodak DCS encoding */
+#define COMPRESSION_JBIG 34661 /* ISO JBIG */
+#define COMPRESSION_SGILOG 34676 /* SGI Log Luminance RLE */
+#define COMPRESSION_SGILOG24 34677 /* SGI Log 24-bit packed */
+#define COMPRESSION_JP2000 34712 /* Leadtools JPEG2000 */
+#define COMPRESSION_LERC 34887 /* ESRI Lerc codec: https://github.com/Esri/lerc */
+/* compression codes 34887-34889 are reserved for ESRI */
+#define COMPRESSION_LZMA 34925 /* LZMA2 */
+#define COMPRESSION_ZSTD 50000 /* ZSTD: WARNING not registered in Adobe-maintained registry */
+#define COMPRESSION_WEBP 50001 /* WEBP: WARNING not registered in Adobe-maintained registry */
+#define TIFFTAG_PHOTOMETRIC 262 /* photometric interpretation */
+#define PHOTOMETRIC_MINISWHITE 0 /* min value is white */
+#define PHOTOMETRIC_MINISBLACK 1 /* min value is black */
+#define PHOTOMETRIC_RGB 2 /* RGB color model */
+#define PHOTOMETRIC_PALETTE 3 /* color map indexed */
+#define PHOTOMETRIC_MASK 4 /* $holdout mask */
+#define PHOTOMETRIC_SEPARATED 5 /* !color separations */
+#define PHOTOMETRIC_YCBCR 6 /* !CCIR 601 */
+#define PHOTOMETRIC_CIELAB 8 /* !1976 CIE L*a*b* */
+#define PHOTOMETRIC_ICCLAB 9 /* ICC L*a*b* [Adobe TIFF Technote 4] */
+#define PHOTOMETRIC_ITULAB 10 /* ITU L*a*b* */
+#define PHOTOMETRIC_CFA 32803 /* color filter array */
+#define PHOTOMETRIC_LOGL 32844 /* CIE Log2(L) */
+#define PHOTOMETRIC_LOGLUV 32845 /* CIE Log2(L) (u',v') */
+#define TIFFTAG_THRESHHOLDING 263 /* +thresholding used on data */
+#define THRESHHOLD_BILEVEL 1 /* b&w art scan */
+#define THRESHHOLD_HALFTONE 2 /* or dithered scan */
+#define THRESHHOLD_ERRORDIFFUSE 3 /* usually floyd-steinberg */
+#define TIFFTAG_CELLWIDTH 264 /* +dithering matrix width */
+#define TIFFTAG_CELLLENGTH 265 /* +dithering matrix height */
+#define TIFFTAG_FILLORDER 266 /* data order within a byte */
+#define FILLORDER_MSB2LSB 1 /* most significant -> least */
+#define FILLORDER_LSB2MSB 2 /* least significant -> most */
+#define TIFFTAG_DOCUMENTNAME 269 /* name of doc. image is from */
+#define TIFFTAG_IMAGEDESCRIPTION 270 /* info about image */
+#define TIFFTAG_MAKE 271 /* scanner manufacturer name */
+#define TIFFTAG_MODEL 272 /* scanner model name/number */
+#define TIFFTAG_STRIPOFFSETS 273 /* offsets to data strips */
+#define TIFFTAG_ORIENTATION 274 /* +image orientation */
+#define ORIENTATION_TOPLEFT 1 /* row 0 top, col 0 lhs */
+#define ORIENTATION_TOPRIGHT 2 /* row 0 top, col 0 rhs */
+#define ORIENTATION_BOTRIGHT 3 /* row 0 bottom, col 0 rhs */
+#define ORIENTATION_BOTLEFT 4 /* row 0 bottom, col 0 lhs */
+#define ORIENTATION_LEFTTOP 5 /* row 0 lhs, col 0 top */
+#define ORIENTATION_RIGHTTOP 6 /* row 0 rhs, col 0 top */
+#define ORIENTATION_RIGHTBOT 7 /* row 0 rhs, col 0 bottom */
+#define ORIENTATION_LEFTBOT 8 /* row 0 lhs, col 0 bottom */
+#define TIFFTAG_SAMPLESPERPIXEL 277 /* samples per pixel */
+#define TIFFTAG_ROWSPERSTRIP 278 /* rows per strip of data */
+#define TIFFTAG_STRIPBYTECOUNTS 279 /* bytes counts for strips */
+#define TIFFTAG_MINSAMPLEVALUE 280 /* +minimum sample value */
+#define TIFFTAG_MAXSAMPLEVALUE 281 /* +maximum sample value */
+#define TIFFTAG_XRESOLUTION 282 /* pixels/resolution in x */
+#define TIFFTAG_YRESOLUTION 283 /* pixels/resolution in y */
+#define TIFFTAG_PLANARCONFIG 284 /* storage organization */
+#define PLANARCONFIG_CONTIG 1 /* single image plane */
+#define PLANARCONFIG_SEPARATE 2 /* separate planes of data */
+#define TIFFTAG_PAGENAME 285 /* page name image is from */
+#define TIFFTAG_XPOSITION 286 /* x page offset of image lhs */
+#define TIFFTAG_YPOSITION 287 /* y page offset of image lhs */
+#define TIFFTAG_FREEOFFSETS 288 /* +byte offset to free block */
+#define TIFFTAG_FREEBYTECOUNTS 289 /* +sizes of free blocks */
+#define TIFFTAG_GRAYRESPONSEUNIT 290 /* $gray scale curve accuracy */
+#define GRAYRESPONSEUNIT_10S 1 /* tenths of a unit */
+#define GRAYRESPONSEUNIT_100S 2 /* hundredths of a unit */
+#define GRAYRESPONSEUNIT_1000S 3 /* thousandths of a unit */
+#define GRAYRESPONSEUNIT_10000S 4 /* ten-thousandths of a unit */
+#define GRAYRESPONSEUNIT_100000S 5 /* hundred-thousandths */
+#define TIFFTAG_GRAYRESPONSECURVE 291 /* $gray scale response curve */
+#define TIFFTAG_GROUP3OPTIONS 292 /* 32 flag bits */
+#define TIFFTAG_T4OPTIONS 292 /* TIFF 6.0 proper name alias */
+#define GROUP3OPT_2DENCODING 0x1 /* 2-dimensional coding */
+#define GROUP3OPT_UNCOMPRESSED 0x2 /* data not compressed */
+#define GROUP3OPT_FILLBITS 0x4 /* fill to byte boundary */
+#define TIFFTAG_GROUP4OPTIONS 293 /* 32 flag bits */
+#define TIFFTAG_T6OPTIONS 293 /* TIFF 6.0 proper name */
+#define GROUP4OPT_UNCOMPRESSED 0x2 /* data not compressed */
+#define TIFFTAG_RESOLUTIONUNIT 296 /* units of resolutions */
+#define RESUNIT_NONE 1 /* no meaningful units */
+#define RESUNIT_INCH 2 /* english */
+#define RESUNIT_CENTIMETER 3 /* metric */
+#define TIFFTAG_PAGENUMBER 297 /* page numbers of multi-page */
+#define TIFFTAG_COLORRESPONSEUNIT 300 /* $color curve accuracy */
+#define COLORRESPONSEUNIT_10S 1 /* tenths of a unit */
+#define COLORRESPONSEUNIT_100S 2 /* hundredths of a unit */
+#define COLORRESPONSEUNIT_1000S 3 /* thousandths of a unit */
+#define COLORRESPONSEUNIT_10000S 4 /* ten-thousandths of a unit */
+#define COLORRESPONSEUNIT_100000S 5 /* hundred-thousandths */
+#define TIFFTAG_TRANSFERFUNCTION 301 /* !colorimetry info */
+#define TIFFTAG_SOFTWARE 305 /* name & release */
+#define TIFFTAG_DATETIME 306 /* creation date and time */
+#define TIFFTAG_ARTIST 315 /* creator of image */
+#define TIFFTAG_HOSTCOMPUTER 316 /* machine where created */
+#define TIFFTAG_PREDICTOR 317 /* prediction scheme w/ LZW */
+#define PREDICTOR_NONE 1 /* no prediction scheme used */
+#define PREDICTOR_HORIZONTAL 2 /* horizontal differencing */
+#define PREDICTOR_FLOATINGPOINT 3 /* floating point predictor */
+#define TIFFTAG_WHITEPOINT 318 /* image white point */
+#define TIFFTAG_PRIMARYCHROMATICITIES 319 /* !primary chromaticities */
+#define TIFFTAG_COLORMAP 320 /* RGB map for palette image */
+#define TIFFTAG_HALFTONEHINTS 321 /* !highlight+shadow info */
+#define TIFFTAG_TILEWIDTH 322 /* !tile width in pixels */
+#define TIFFTAG_TILELENGTH 323 /* !tile height in pixels */
+#define TIFFTAG_TILEOFFSETS 324 /* !offsets to data tiles */
+#define TIFFTAG_TILEBYTECOUNTS 325 /* !byte counts for tiles */
+#define TIFFTAG_BADFAXLINES 326 /* lines w/ wrong pixel count */
+#define TIFFTAG_CLEANFAXDATA 327 /* regenerated line info */
+#define CLEANFAXDATA_CLEAN 0 /* no errors detected */
+#define CLEANFAXDATA_REGENERATED 1 /* receiver regenerated lines */
+#define CLEANFAXDATA_UNCLEAN 2 /* uncorrected errors exist */
+#define TIFFTAG_CONSECUTIVEBADFAXLINES 328 /* max consecutive bad lines */
+#define TIFFTAG_SUBIFD 330 /* subimage descriptors */
+#define TIFFTAG_INKSET 332 /* !inks in separated image */
+#define INKSET_CMYK 1 /* !cyan-magenta-yellow-black color */
+#define INKSET_MULTIINK 2 /* !multi-ink or hi-fi color */
+#define TIFFTAG_INKNAMES 333 /* !ascii names of inks */
+#define TIFFTAG_NUMBEROFINKS 334 /* !number of inks */
+#define TIFFTAG_DOTRANGE 336 /* !0% and 100% dot codes */
+#define TIFFTAG_TARGETPRINTER 337 /* !separation target */
+#define TIFFTAG_EXTRASAMPLES 338 /* !info about extra samples */
+#define EXTRASAMPLE_UNSPECIFIED 0 /* !unspecified data */
+#define EXTRASAMPLE_ASSOCALPHA 1 /* !associated alpha data */
+#define EXTRASAMPLE_UNASSALPHA 2 /* !unassociated alpha data */
+#define TIFFTAG_SAMPLEFORMAT 339 /* !data sample format */
+#define SAMPLEFORMAT_UINT 1 /* !unsigned integer data */
+#define SAMPLEFORMAT_INT 2 /* !signed integer data */
+#define SAMPLEFORMAT_IEEEFP 3 /* !IEEE floating point data */
+#define SAMPLEFORMAT_VOID 4 /* !untyped data */
+#define SAMPLEFORMAT_COMPLEXINT 5 /* !complex signed int */
+#define SAMPLEFORMAT_COMPLEXIEEEFP 6 /* !complex ieee floating */
+#define TIFFTAG_SMINSAMPLEVALUE 340 /* !variable MinSampleValue */
+#define TIFFTAG_SMAXSAMPLEVALUE 341 /* !variable MaxSampleValue */
+#define TIFFTAG_CLIPPATH 343 /* %ClipPath
+ [Adobe TIFF technote 2] */
+#define TIFFTAG_XCLIPPATHUNITS 344 /* %XClipPathUnits
+ [Adobe TIFF technote 2] */
+#define TIFFTAG_YCLIPPATHUNITS 345 /* %YClipPathUnits
+ [Adobe TIFF technote 2] */
+#define TIFFTAG_INDEXED 346 /* %Indexed
+ [Adobe TIFF Technote 3] */
+#define TIFFTAG_JPEGTABLES 347 /* %JPEG table stream */
+#define TIFFTAG_OPIPROXY 351 /* %OPI Proxy [Adobe TIFF technote] */
+/* Tags 400-435 are from the TIFF/FX spec */
+#define TIFFTAG_GLOBALPARAMETERSIFD 400 /* ! */
+#define TIFFTAG_PROFILETYPE 401 /* ! */
+#define PROFILETYPE_UNSPECIFIED 0 /* ! */
+#define PROFILETYPE_G3_FAX 1 /* ! */
+#define TIFFTAG_FAXPROFILE 402 /* ! */
+#define FAXPROFILE_S 1 /* !TIFF/FX FAX profile S */
+#define FAXPROFILE_F 2 /* !TIFF/FX FAX profile F */
+#define FAXPROFILE_J 3 /* !TIFF/FX FAX profile J */
+#define FAXPROFILE_C 4 /* !TIFF/FX FAX profile C */
+#define FAXPROFILE_L 5 /* !TIFF/FX FAX profile L */
+#define FAXPROFILE_M 6 /* !TIFF/FX FAX profile LM */
+#define TIFFTAG_CODINGMETHODS 403 /* !TIFF/FX coding methods */
+#define CODINGMETHODS_T4_1D (1 << 1) /* !T.4 1D */
+#define CODINGMETHODS_T4_2D (1 << 2) /* !T.4 2D */
+#define CODINGMETHODS_T6 (1 << 3) /* !T.6 */
+#define CODINGMETHODS_T85 (1 << 4) /* !T.85 JBIG */
+#define CODINGMETHODS_T42 (1 << 5) /* !T.42 JPEG */
+#define CODINGMETHODS_T43 (1 << 6) /* !T.43 colour by layered JBIG */
+#define TIFFTAG_VERSIONYEAR 404 /* !TIFF/FX version year */
+#define TIFFTAG_MODENUMBER 405 /* !TIFF/FX mode number */
+#define TIFFTAG_DECODE 433 /* !TIFF/FX decode */
+#define TIFFTAG_IMAGEBASECOLOR 434 /* !TIFF/FX image base colour */
+#define TIFFTAG_T82OPTIONS 435 /* !TIFF/FX T.82 options */
+/*
+ * Tags 512-521 are obsoleted by Technical Note #2 which specifies a
+ * revised JPEG-in-TIFF scheme.
+ */
+#define TIFFTAG_JPEGPROC 512 /* !JPEG processing algorithm */
+#define JPEGPROC_BASELINE 1 /* !baseline sequential */
+#define JPEGPROC_LOSSLESS 14 /* !Huffman coded lossless */
+#define TIFFTAG_JPEGIFOFFSET 513 /* !pointer to SOI marker */
+#define TIFFTAG_JPEGIFBYTECOUNT 514 /* !JFIF stream length */
+#define TIFFTAG_JPEGRESTARTINTERVAL 515 /* !restart interval length */
+#define TIFFTAG_JPEGLOSSLESSPREDICTORS 517 /* !lossless proc predictor */
+#define TIFFTAG_JPEGPOINTTRANSFORM 518 /* !lossless point transform */
+#define TIFFTAG_JPEGQTABLES 519 /* !Q matrix offsets */
+#define TIFFTAG_JPEGDCTABLES 520 /* !DCT table offsets */
+#define TIFFTAG_JPEGACTABLES 521 /* !AC coefficient offsets */
+#define TIFFTAG_YCBCRCOEFFICIENTS 529 /* !RGB -> YCbCr transform */
+#define TIFFTAG_YCBCRSUBSAMPLING 530 /* !YCbCr subsampling factors */
+#define TIFFTAG_YCBCRPOSITIONING 531 /* !subsample positioning */
+#define YCBCRPOSITION_CENTERED 1 /* !as in PostScript Level 2 */
+#define YCBCRPOSITION_COSITED 2 /* !as in CCIR 601-1 */
+#define TIFFTAG_REFERENCEBLACKWHITE 532 /* !colorimetry info */
+#define TIFFTAG_STRIPROWCOUNTS 559 /* !TIFF/FX strip row counts */
+#define TIFFTAG_XMLPACKET 700 /* %XML packet
+ [Adobe XMP Specification,
+ January 2004 */
+#define TIFFTAG_OPIIMAGEID 32781 /* %OPI ImageID
+ [Adobe TIFF technote] */
+/* tags 32952-32956 are private tags registered to Island Graphics */
+#define TIFFTAG_REFPTS 32953 /* image reference points */
+#define TIFFTAG_REGIONTACKPOINT 32954 /* region-xform tack point */
+#define TIFFTAG_REGIONWARPCORNERS 32955 /* warp quadrilateral */
+#define TIFFTAG_REGIONAFFINE 32956 /* affine transformation mat */
+/* tags 32995-32999 are private tags registered to SGI */
+#define TIFFTAG_MATTEING 32995 /* $use ExtraSamples */
+#define TIFFTAG_DATATYPE 32996 /* $use SampleFormat */
+#define TIFFTAG_IMAGEDEPTH 32997 /* z depth of image */
+#define TIFFTAG_TILEDEPTH 32998 /* z depth/data tile */
+/* tags 33300-33309 are private tags registered to Pixar */
+/*
+ * TIFFTAG_PIXAR_IMAGEFULLWIDTH and TIFFTAG_PIXAR_IMAGEFULLLENGTH
+ * are set when an image has been cropped out of a larger image.
+ * They reflect the size of the original uncropped image.
+ * The TIFFTAG_XPOSITION and TIFFTAG_YPOSITION can be used
+ * to determine the position of the smaller image in the larger one.
+ */
+#define TIFFTAG_PIXAR_IMAGEFULLWIDTH 33300 /* full image size in x */
+#define TIFFTAG_PIXAR_IMAGEFULLLENGTH 33301 /* full image size in y */
+ /* Tags 33302-33306 are used to identify special image modes and data
+ * used by Pixar's texture formats.
+ */
+#define TIFFTAG_PIXAR_TEXTUREFORMAT 33302 /* texture map format */
+#define TIFFTAG_PIXAR_WRAPMODES 33303 /* s & t wrap modes */
+#define TIFFTAG_PIXAR_FOVCOT 33304 /* cotan(fov) for env. maps */
+#define TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN 33305
+#define TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA 33306
+/* tag 33405 is a private tag registered to Eastman Kodak */
+#define TIFFTAG_WRITERSERIALNUMBER 33405 /* device serial number */
+#define TIFFTAG_CFAREPEATPATTERNDIM 33421 /* dimensions of CFA pattern */
+#define TIFFTAG_CFAPATTERN 33422 /* color filter array pattern */
+/* tag 33432 is listed in the 6.0 spec w/ unknown ownership */
+#define TIFFTAG_COPYRIGHT 33432 /* copyright string */
+/* IPTC TAG from RichTIFF specifications */
+#define TIFFTAG_RICHTIFFIPTC 33723
+/* 34016-34029 are reserved for ANSI IT8 TIFF/IT <dkelly@apago.com) */
+#define TIFFTAG_IT8SITE 34016 /* site name */
+#define TIFFTAG_IT8COLORSEQUENCE 34017 /* color seq. [RGB,CMYK,etc] */
+#define TIFFTAG_IT8HEADER 34018 /* DDES Header */
+#define TIFFTAG_IT8RASTERPADDING 34019 /* raster scanline padding */
+#define TIFFTAG_IT8BITSPERRUNLENGTH 34020 /* # of bits in short run */
+#define TIFFTAG_IT8BITSPEREXTENDEDRUNLENGTH 34021/* # of bits in long run */
+#define TIFFTAG_IT8COLORTABLE 34022 /* LW colortable */
+#define TIFFTAG_IT8IMAGECOLORINDICATOR 34023 /* BP/BL image color switch */
+#define TIFFTAG_IT8BKGCOLORINDICATOR 34024 /* BP/BL bg color switch */
+#define TIFFTAG_IT8IMAGECOLORVALUE 34025 /* BP/BL image color value */
+#define TIFFTAG_IT8BKGCOLORVALUE 34026 /* BP/BL bg color value */
+#define TIFFTAG_IT8PIXELINTENSITYRANGE 34027 /* MP pixel intensity value */
+#define TIFFTAG_IT8TRANSPARENCYINDICATOR 34028 /* HC transparency switch */
+#define TIFFTAG_IT8COLORCHARACTERIZATION 34029 /* color character. table */
+#define TIFFTAG_IT8HCUSAGE 34030 /* HC usage indicator */
+#define TIFFTAG_IT8TRAPINDICATOR 34031 /* Trapping indicator
+ (untrapped=0, trapped=1) */
+#define TIFFTAG_IT8CMYKEQUIVALENT 34032 /* CMYK color equivalents */
+/* tags 34232-34236 are private tags registered to Texas Instruments */
+#define TIFFTAG_FRAMECOUNT 34232 /* Sequence Frame Count */
+/* tag 34377 is private tag registered to Adobe for PhotoShop */
+#define TIFFTAG_PHOTOSHOP 34377
+/* tags 34665, 34853 and 40965 are documented in EXIF specification */
+#define TIFFTAG_EXIFIFD 34665 /* Pointer to EXIF private directory */
+/* tag 34750 is a private tag registered to Adobe? */
+#define TIFFTAG_ICCPROFILE 34675 /* ICC profile data */
+#define TIFFTAG_IMAGELAYER 34732 /* !TIFF/FX image layer information */
+/* tag 34750 is a private tag registered to Pixel Magic */
+#define TIFFTAG_JBIGOPTIONS 34750 /* JBIG options */
+#define TIFFTAG_GPSIFD 34853 /* Pointer to GPS private directory */
+/* tags 34908-34914 are private tags registered to SGI */
+#define TIFFTAG_FAXRECVPARAMS 34908 /* encoded Class 2 ses. parms */
+#define TIFFTAG_FAXSUBADDRESS 34909 /* received SubAddr string */
+#define TIFFTAG_FAXRECVTIME 34910 /* receive time (secs) */
+#define TIFFTAG_FAXDCS 34911 /* encoded fax ses. params, Table 2/T.30 */
+/* tags 37439-37443 are registered to SGI <gregl@sgi.com> */
+#define TIFFTAG_STONITS 37439 /* Sample value to Nits */
+/* tag 34929 is a private tag registered to FedEx */
+#define TIFFTAG_FEDEX_EDR 34929 /* unknown use */
+#define TIFFTAG_INTEROPERABILITYIFD 40965 /* Pointer to Interoperability private directory */
+/* tags 50674 to 50677 are reserved for ESRI */
+#define TIFFTAG_LERC_PARAMETERS 50674 /* Stores LERC version and additional compression method */
+/* Adobe Digital Negative (DNG) format tags */
+#define TIFFTAG_DNGVERSION 50706 /* &DNG version number */
+#define TIFFTAG_DNGBACKWARDVERSION 50707 /* &DNG compatibility version */
+#define TIFFTAG_UNIQUECAMERAMODEL 50708 /* &name for the camera model */
+#define TIFFTAG_LOCALIZEDCAMERAMODEL 50709 /* &localized camera model
+ name */
+#define TIFFTAG_CFAPLANECOLOR 50710 /* &CFAPattern->LinearRaw space
+ mapping */
+#define TIFFTAG_CFALAYOUT 50711 /* &spatial layout of the CFA */
+#define TIFFTAG_LINEARIZATIONTABLE 50712 /* &lookup table description */
+#define TIFFTAG_BLACKLEVELREPEATDIM 50713 /* &repeat pattern size for
+ the BlackLevel tag */
+#define TIFFTAG_BLACKLEVEL 50714 /* &zero light encoding level */
+#define TIFFTAG_BLACKLEVELDELTAH 50715 /* &zero light encoding level
+ differences (columns) */
+#define TIFFTAG_BLACKLEVELDELTAV 50716 /* &zero light encoding level
+ differences (rows) */
+#define TIFFTAG_WHITELEVEL 50717 /* &fully saturated encoding
+ level */
+#define TIFFTAG_DEFAULTSCALE 50718 /* &default scale factors */
+#define TIFFTAG_DEFAULTCROPORIGIN 50719 /* &origin of the final image
+ area */
+#define TIFFTAG_DEFAULTCROPSIZE 50720 /* &size of the final image
+ area */
+#define TIFFTAG_COLORMATRIX1 50721 /* &XYZ->reference color space
+ transformation matrix 1 */
+#define TIFFTAG_COLORMATRIX2 50722 /* &XYZ->reference color space
+ transformation matrix 2 */
+#define TIFFTAG_CAMERACALIBRATION1 50723 /* &calibration matrix 1 */
+#define TIFFTAG_CAMERACALIBRATION2 50724 /* &calibration matrix 2 */
+#define TIFFTAG_REDUCTIONMATRIX1 50725 /* &dimensionality reduction
+ matrix 1 */
+#define TIFFTAG_REDUCTIONMATRIX2 50726 /* &dimensionality reduction
+ matrix 2 */
+#define TIFFTAG_ANALOGBALANCE 50727 /* &gain applied the stored raw
+ values*/
+#define TIFFTAG_ASSHOTNEUTRAL 50728 /* &selected white balance in
+ linear reference space */
+#define TIFFTAG_ASSHOTWHITEXY 50729 /* &selected white balance in
+ x-y chromaticity
+ coordinates */
+#define TIFFTAG_BASELINEEXPOSURE 50730 /* &how much to move the zero
+ point */
+#define TIFFTAG_BASELINENOISE 50731 /* &relative noise level */
+#define TIFFTAG_BASELINESHARPNESS 50732 /* &relative amount of
+ sharpening */
+#define TIFFTAG_BAYERGREENSPLIT 50733 /* &how closely the values of
+ the green pixels in the
+ blue/green rows track the
+ values of the green pixels
+ in the red/green rows */
+#define TIFFTAG_LINEARRESPONSELIMIT 50734 /* &non-linear encoding range */
+#define TIFFTAG_CAMERASERIALNUMBER 50735 /* &camera's serial number */
+#define TIFFTAG_LENSINFO 50736 /* info about the lens */
+#define TIFFTAG_CHROMABLURRADIUS 50737 /* &chroma blur radius */
+#define TIFFTAG_ANTIALIASSTRENGTH 50738 /* &relative strength of the
+ camera's anti-alias filter */
+#define TIFFTAG_SHADOWSCALE 50739 /* &used by Adobe Camera Raw */
+#define TIFFTAG_DNGPRIVATEDATA 50740 /* &manufacturer's private data */
+#define TIFFTAG_MAKERNOTESAFETY 50741 /* &whether the EXIF MakerNote
+ tag is safe to preserve
+ along with the rest of the
+ EXIF data */
+#define TIFFTAG_CALIBRATIONILLUMINANT1 50778 /* &illuminant 1 */
+#define TIFFTAG_CALIBRATIONILLUMINANT2 50779 /* &illuminant 2 */
+#define TIFFTAG_BESTQUALITYSCALE 50780 /* &best quality multiplier */
+#define TIFFTAG_RAWDATAUNIQUEID 50781 /* &unique identifier for
+ the raw image data */
+#define TIFFTAG_ORIGINALRAWFILENAME 50827 /* &file name of the original
+ raw file */
+#define TIFFTAG_ORIGINALRAWFILEDATA 50828 /* &contents of the original
+ raw file */
+#define TIFFTAG_ACTIVEAREA 50829 /* &active (non-masked) pixels
+ of the sensor */
+#define TIFFTAG_MASKEDAREAS 50830 /* &list of coordinates
+ of fully masked pixels */
+#define TIFFTAG_ASSHOTICCPROFILE 50831 /* &these two tags used to */
+#define TIFFTAG_ASSHOTPREPROFILEMATRIX 50832 /* map cameras's color space
+ into ICC profile space */
+#define TIFFTAG_CURRENTICCPROFILE 50833 /* & */
+#define TIFFTAG_CURRENTPREPROFILEMATRIX 50834 /* & */
+/* tag 65535 is an undefined tag used by Eastman Kodak */
+#define TIFFTAG_DCSHUESHIFTVALUES 65535 /* hue shift correction data */
+
+/*
+ * The following are ``pseudo tags'' that can be used to control
+ * codec-specific functionality. These tags are not written to file.
+ * Note that these values start at 0xffff+1 so that they'll never
+ * collide with Aldus-assigned tags.
+ *
+ * If you want your private pseudo tags ``registered'' (i.e. added to
+ * this file), please post a bug report via the tracking system at
+ * http://www.remotesensing.org/libtiff/bugs.html with the appropriate
+ * C definitions to add.
+ */
+#define TIFFTAG_FAXMODE 65536 /* Group 3/4 format control */
+#define FAXMODE_CLASSIC 0x0000 /* default, include RTC */
+#define FAXMODE_NORTC 0x0001 /* no RTC at end of data */
+#define FAXMODE_NOEOL 0x0002 /* no EOL code at end of row */
+#define FAXMODE_BYTEALIGN 0x0004 /* byte align row */
+#define FAXMODE_WORDALIGN 0x0008 /* word align row */
+#define FAXMODE_CLASSF FAXMODE_NORTC /* TIFF Class F */
+#define TIFFTAG_JPEGQUALITY 65537 /* Compression quality level */
+/* Note: quality level is on the IJG 0-100 scale. Default value is 75 */
+#define TIFFTAG_JPEGCOLORMODE 65538 /* Auto RGB<=>YCbCr convert? */
+#define JPEGCOLORMODE_RAW 0x0000 /* no conversion (default) */
+#define JPEGCOLORMODE_RGB 0x0001 /* do auto conversion */
+#define TIFFTAG_JPEGTABLESMODE 65539 /* What to put in JPEGTables */
+#define JPEGTABLESMODE_QUANT 0x0001 /* include quantization tbls */
+#define JPEGTABLESMODE_HUFF 0x0002 /* include Huffman tbls */
+/* Note: default is JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF */
+#define TIFFTAG_FAXFILLFUNC 65540 /* G3/G4 fill function */
+#define TIFFTAG_PIXARLOGDATAFMT 65549 /* PixarLogCodec I/O data sz */
+#define PIXARLOGDATAFMT_8BIT 0 /* regular u_char samples */
+#define PIXARLOGDATAFMT_8BITABGR 1 /* ABGR-order u_chars */
+#define PIXARLOGDATAFMT_11BITLOG 2 /* 11-bit log-encoded (raw) */
+#define PIXARLOGDATAFMT_12BITPICIO 3 /* as per PICIO (1.0==2048) */
+#define PIXARLOGDATAFMT_16BIT 4 /* signed short samples */
+#define PIXARLOGDATAFMT_FLOAT 5 /* IEEE float samples */
+/* 65550-65556 are allocated to Oceana Matrix <dev@oceana.com> */
+#define TIFFTAG_DCSIMAGERTYPE 65550 /* imager model & filter */
+#define DCSIMAGERMODEL_M3 0 /* M3 chip (1280 x 1024) */
+#define DCSIMAGERMODEL_M5 1 /* M5 chip (1536 x 1024) */
+#define DCSIMAGERMODEL_M6 2 /* M6 chip (3072 x 2048) */
+#define DCSIMAGERFILTER_IR 0 /* infrared filter */
+#define DCSIMAGERFILTER_MONO 1 /* monochrome filter */
+#define DCSIMAGERFILTER_CFA 2 /* color filter array */
+#define DCSIMAGERFILTER_OTHER 3 /* other filter */
+#define TIFFTAG_DCSINTERPMODE 65551 /* interpolation mode */
+#define DCSINTERPMODE_NORMAL 0x0 /* whole image, default */
+#define DCSINTERPMODE_PREVIEW 0x1 /* preview of image (384x256) */
+#define TIFFTAG_DCSBALANCEARRAY 65552 /* color balance values */
+#define TIFFTAG_DCSCORRECTMATRIX 65553 /* color correction values */
+#define TIFFTAG_DCSGAMMA 65554 /* gamma value */
+#define TIFFTAG_DCSTOESHOULDERPTS 65555 /* toe & shoulder points */
+#define TIFFTAG_DCSCALIBRATIONFD 65556 /* calibration file desc */
+/* Note: quality level is on the ZLIB 1-9 scale. Default value is -1 */
+#define TIFFTAG_ZIPQUALITY 65557 /* compression quality level */
+#define TIFFTAG_PIXARLOGQUALITY 65558 /* PixarLog uses same scale */
+/* 65559 is allocated to Oceana Matrix <dev@oceana.com> */
+#define TIFFTAG_DCSCLIPRECTANGLE 65559 /* area of image to acquire */
+#define TIFFTAG_SGILOGDATAFMT 65560 /* SGILog user data format */
+#define SGILOGDATAFMT_FLOAT 0 /* IEEE float samples */
+#define SGILOGDATAFMT_16BIT 1 /* 16-bit samples */
+#define SGILOGDATAFMT_RAW 2 /* uninterpreted data */
+#define SGILOGDATAFMT_8BIT 3 /* 8-bit RGB monitor values */
+#define TIFFTAG_SGILOGENCODE 65561 /* SGILog data encoding control*/
+#define SGILOGENCODE_NODITHER 0 /* do not dither encoded values*/
+#define SGILOGENCODE_RANDITHER 1 /* randomly dither encd values */
+#define TIFFTAG_LZMAPRESET 65562 /* LZMA2 preset (compression level) */
+#define TIFFTAG_PERSAMPLE 65563 /* interface for per sample tags */
+#define PERSAMPLE_MERGED 0 /* present as a single value */
+#define PERSAMPLE_MULTI 1 /* present as multiple values */
+#define TIFFTAG_ZSTD_LEVEL 65564 /* ZSTD compression level */
+#define TIFFTAG_LERC_VERSION 65565 /* LERC version */
+#define LERC_VERSION_2_4 4
+#define TIFFTAG_LERC_ADD_COMPRESSION 65566 /* LERC additional compression */
+#define LERC_ADD_COMPRESSION_NONE 0
+#define LERC_ADD_COMPRESSION_DEFLATE 1
+#define LERC_ADD_COMPRESSION_ZSTD 2
+#define TIFFTAG_LERC_MAXZERROR 65567 /* LERC maximum error */
+#define TIFFTAG_WEBP_LEVEL 65568 /* WebP compression level: WARNING not registered in Adobe-maintained registry */
+#define TIFFTAG_WEBP_LOSSLESS 65569 /* WebP lossless/lossy : WARNING not registered in Adobe-maintained registry */
+
+/*
+ * EXIF tags
+ */
+#define EXIFTAG_EXPOSURETIME 33434 /* Exposure time */
+#define EXIFTAG_FNUMBER 33437 /* F number */
+#define EXIFTAG_EXPOSUREPROGRAM 34850 /* Exposure program */
+#define EXIFTAG_SPECTRALSENSITIVITY 34852 /* Spectral sensitivity */
+#define EXIFTAG_ISOSPEEDRATINGS 34855 /* ISO speed rating */
+#define EXIFTAG_OECF 34856 /* Optoelectric conversion
+ factor */
+#define EXIFTAG_EXIFVERSION 36864 /* Exif version */
+#define EXIFTAG_DATETIMEORIGINAL 36867 /* Date and time of original
+ data generation */
+#define EXIFTAG_DATETIMEDIGITIZED 36868 /* Date and time of digital
+ data generation */
+#define EXIFTAG_COMPONENTSCONFIGURATION 37121 /* Meaning of each component */
+#define EXIFTAG_COMPRESSEDBITSPERPIXEL 37122 /* Image compression mode */
+#define EXIFTAG_SHUTTERSPEEDVALUE 37377 /* Shutter speed */
+#define EXIFTAG_APERTUREVALUE 37378 /* Aperture */
+#define EXIFTAG_BRIGHTNESSVALUE 37379 /* Brightness */
+#define EXIFTAG_EXPOSUREBIASVALUE 37380 /* Exposure bias */
+#define EXIFTAG_MAXAPERTUREVALUE 37381 /* Maximum lens aperture */
+#define EXIFTAG_SUBJECTDISTANCE 37382 /* Subject distance */
+#define EXIFTAG_METERINGMODE 37383 /* Metering mode */
+#define EXIFTAG_LIGHTSOURCE 37384 /* Light source */
+#define EXIFTAG_FLASH 37385 /* Flash */
+#define EXIFTAG_FOCALLENGTH 37386 /* Lens focal length */
+#define EXIFTAG_SUBJECTAREA 37396 /* Subject area */
+#define EXIFTAG_MAKERNOTE 37500 /* Manufacturer notes */
+#define EXIFTAG_USERCOMMENT 37510 /* User comments */
+#define EXIFTAG_SUBSECTIME 37520 /* DateTime subseconds */
+#define EXIFTAG_SUBSECTIMEORIGINAL 37521 /* DateTimeOriginal subseconds */
+#define EXIFTAG_SUBSECTIMEDIGITIZED 37522 /* DateTimeDigitized subseconds */
+#define EXIFTAG_FLASHPIXVERSION 40960 /* Supported Flashpix version */
+#define EXIFTAG_COLORSPACE 40961 /* Color space information */
+#define EXIFTAG_PIXELXDIMENSION 40962 /* Valid image width */
+#define EXIFTAG_PIXELYDIMENSION 40963 /* Valid image height */
+#define EXIFTAG_RELATEDSOUNDFILE 40964 /* Related audio file */
+#define EXIFTAG_FLASHENERGY 41483 /* Flash energy */
+#define EXIFTAG_SPATIALFREQUENCYRESPONSE 41484 /* Spatial frequency response */
+#define EXIFTAG_FOCALPLANEXRESOLUTION 41486 /* Focal plane X resolution */
+#define EXIFTAG_FOCALPLANEYRESOLUTION 41487 /* Focal plane Y resolution */
+#define EXIFTAG_FOCALPLANERESOLUTIONUNIT 41488 /* Focal plane resolution unit */
+#define EXIFTAG_SUBJECTLOCATION 41492 /* Subject location */
+#define EXIFTAG_EXPOSUREINDEX 41493 /* Exposure index */
+#define EXIFTAG_SENSINGMETHOD 41495 /* Sensing method */
+#define EXIFTAG_FILESOURCE 41728 /* File source */
+#define EXIFTAG_SCENETYPE 41729 /* Scene type */
+#define EXIFTAG_CFAPATTERN 41730 /* CFA pattern */
+#define EXIFTAG_CUSTOMRENDERED 41985 /* Custom image processing */
+#define EXIFTAG_EXPOSUREMODE 41986 /* Exposure mode */
+#define EXIFTAG_WHITEBALANCE 41987 /* White balance */
+#define EXIFTAG_DIGITALZOOMRATIO 41988 /* Digital zoom ratio */
+#define EXIFTAG_FOCALLENGTHIN35MMFILM 41989 /* Focal length in 35 mm film */
+#define EXIFTAG_SCENECAPTURETYPE 41990 /* Scene capture type */
+#define EXIFTAG_GAINCONTROL 41991 /* Gain control */
+#define EXIFTAG_CONTRAST 41992 /* Contrast */
+#define EXIFTAG_SATURATION 41993 /* Saturation */
+#define EXIFTAG_SHARPNESS 41994 /* Sharpness */
+#define EXIFTAG_DEVICESETTINGDESCRIPTION 41995 /* Device settings description */
+#define EXIFTAG_SUBJECTDISTANCERANGE 41996 /* Subject distance range */
+#define EXIFTAG_GAINCONTROL 41991 /* Gain control */
+#define EXIFTAG_GAINCONTROL 41991 /* Gain control */
+#define EXIFTAG_IMAGEUNIQUEID 42016 /* Unique image ID */
+
+#endif /* _TIFF_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tiffconf.h b/test/monniaux/tiff-4.0.10/tiffconf.h
new file mode 100644
index 00000000..d7a61a3f
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffconf.h
@@ -0,0 +1,119 @@
+/* libtiff/tiffconf.h. Generated from tiffconf.h.in by configure. */
+/*
+ Configuration defines for installed libtiff.
+ This file maintained for backward compatibility. Do not use definitions
+ from this file in your programs.
+*/
+
+#ifndef _TIFFCONF_
+#define _TIFFCONF_
+
+/* Signed 16-bit type */
+#define TIFF_INT16_T signed short
+
+/* Signed 32-bit type */
+#define TIFF_INT32_T signed int
+
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed long
+
+/* Signed 8-bit type */
+#define TIFF_INT8_T signed char
+
+/* Unsigned 16-bit type */
+#define TIFF_UINT16_T unsigned short
+
+/* Unsigned 32-bit type */
+#define TIFF_UINT32_T unsigned int
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned long
+
+/* Unsigned 8-bit type */
+#define TIFF_UINT8_T unsigned char
+
+/* Signed size type */
+#define TIFF_SSIZE_T signed long
+
+/* Pointer difference type */
+#define TIFF_PTRDIFF_T ptrdiff_t
+
+/* Compatibility stuff. */
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
+#define HOST_FILLORDER FILLORDER_MSB2LSB
+
+/* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
+ (Intel) */
+#define HOST_BIGENDIAN 0
+
+/* Support CCITT Group 3 & 4 algorithms */
+#define CCITT_SUPPORT 1
+
+/* Support JPEG compression (requires IJG JPEG library) */
+/* #undef JPEG_SUPPORT */
+
+/* Support JBIG compression (requires JBIG-KIT library) */
+/* #undef JBIG_SUPPORT */
+
+/* Support LogLuv high dynamic range encoding */
+#define LOGLUV_SUPPORT 1
+
+/* Support LZW algorithm */
+#define LZW_SUPPORT 1
+
+/* Support NeXT 2-bit RLE algorithm */
+#define NEXT_SUPPORT 1
+
+/* Support Old JPEG compresson (read contrib/ojpeg/README first! Compilation
+ fails with unpatched IJG JPEG library) */
+/* #undef OJPEG_SUPPORT */
+
+/* Support Macintosh PackBits algorithm */
+#define PACKBITS_SUPPORT 1
+
+/* Support Pixar log-format algorithm (requires Zlib) */
+/* #undef PIXARLOG_SUPPORT */
+
+/* Support ThunderScan 4-bit RLE algorithm */
+#define THUNDER_SUPPORT 1
+
+/* Support Deflate compression */
+/* #undef ZIP_SUPPORT */
+
+/* Support strip chopping (whether or not to convert single-strip uncompressed
+ images to mutiple strips of ~8Kb to reduce memory usage) */
+#define STRIPCHOP_DEFAULT TIFF_STRIPCHOP
+
+/* Enable SubIFD tag (330) support */
+#define SUBIFD_SUPPORT 1
+
+/* Treat extra sample as alpha (default enabled). The RGBA interface will
+ treat a fourth sample with no EXTRASAMPLE_ value as being ASSOCALPHA. Many
+ packages produce RGBA files but don't mark the alpha properly. */
+#define DEFAULT_EXTRASAMPLE_AS_ALPHA 1
+
+/* Pick up YCbCr subsampling info from the JPEG data stream to support files
+ lacking the tag (default enabled). */
+#define CHECK_JPEG_YCBCR_SUBSAMPLING 1
+
+/* Support MS MDI magic number files as TIFF */
+#define MDI_SUPPORT 1
+
+/*
+ * Feature support definitions.
+ * XXX: These macros are obsoleted. Don't use them in your apps!
+ * Macros stays here for backward compatibility and should be always defined.
+ */
+#define COLORIMETRY_SUPPORT
+#define YCBCR_SUPPORT
+#define CMYK_SUPPORT
+#define ICC_SUPPORT
+#define PHOTOSHOP_SUPPORT
+#define IPTC_SUPPORT
+
+#endif /* _TIFFCONF_ */
diff --git a/test/monniaux/tiff-4.0.10/tiffconf.vc.h b/test/monniaux/tiff-4.0.10/tiffconf.vc.h
new file mode 100644
index 00000000..fb37a755
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffconf.vc.h
@@ -0,0 +1,152 @@
+/*
+ Configuration defines for installed libtiff.
+ This file maintained for backward compatibility. Do not use definitions
+ from this file in your programs.
+*/
+
+#ifndef _TIFFCONF_
+#define _TIFFCONF_
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* Signed 8-bit type */
+#define TIFF_INT8_T signed char
+
+/* Unsigned 8-bit type */
+#define TIFF_UINT8_T unsigned char
+
+/* Signed 16-bit type */
+#define TIFF_INT16_T signed short
+
+/* Unsigned 16-bit type */
+#define TIFF_UINT16_T unsigned short
+
+/* Signed 32-bit type formatter */
+#define TIFF_INT32_FORMAT "%d"
+
+/* Signed 32-bit type */
+#define TIFF_INT32_T signed int
+
+/* Unsigned 32-bit type formatter */
+#define TIFF_UINT32_FORMAT "%u"
+
+/* Unsigned 32-bit type */
+#define TIFF_UINT32_T unsigned int
+
+/* Signed 64-bit type formatter */
+#define TIFF_INT64_FORMAT "%I64d"
+
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed __int64
+
+/* Unsigned 64-bit type formatter */
+#define TIFF_UINT64_FORMAT "%I64u"
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned __int64
+
+#if _WIN64
+/*
+ Windows 64-bit build
+*/
+
+/* Signed size type */
+# define TIFF_SSIZE_T TIFF_INT64_T
+
+#else
+/*
+ Windows 32-bit build
+*/
+
+/* Signed size type */
+# define TIFF_SSIZE_T signed int
+
+#endif
+
+/* Compatibility stuff. */
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
+
+/* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
+ (Intel) */
+#define HOST_BIGENDIAN 0
+
+/* Support CCITT Group 3 & 4 algorithms */
+#define CCITT_SUPPORT 1
+
+/* Support JPEG compression (requires IJG JPEG library) */
+/* #undef JPEG_SUPPORT */
+
+/* Support JBIG compression (requires JBIG-KIT library) */
+/* #undef JBIG_SUPPORT */
+
+/* Support LogLuv high dynamic range encoding */
+#define LOGLUV_SUPPORT 1
+
+/* Support LZW algorithm */
+#define LZW_SUPPORT 1
+
+/* Support NeXT 2-bit RLE algorithm */
+#define NEXT_SUPPORT 1
+
+/* Support Old JPEG compresson (read contrib/ojpeg/README first! Compilation
+ fails with unpatched IJG JPEG library) */
+/* #undef OJPEG_SUPPORT */
+
+/* Support Macintosh PackBits algorithm */
+#define PACKBITS_SUPPORT 1
+
+/* Support Pixar log-format algorithm (requires Zlib) */
+/* #undef PIXARLOG_SUPPORT */
+
+/* Support ThunderScan 4-bit RLE algorithm */
+#define THUNDER_SUPPORT 1
+
+/* Support Deflate compression */
+/* #undef ZIP_SUPPORT */
+
+/* Support strip chopping (whether or not to convert single-strip uncompressed
+ images to mutiple strips of ~8Kb to reduce memory usage) */
+#define STRIPCHOP_DEFAULT TIFF_STRIPCHOP
+
+/* Enable SubIFD tag (330) support */
+#define SUBIFD_SUPPORT 1
+
+/* Treat extra sample as alpha (default enabled). The RGBA interface will
+ treat a fourth sample with no EXTRASAMPLE_ value as being ASSOCALPHA. Many
+ packages produce RGBA files but don't mark the alpha properly. */
+#define DEFAULT_EXTRASAMPLE_AS_ALPHA 1
+
+/* Pick up YCbCr subsampling info from the JPEG data stream to support files
+ lacking the tag (default enabled). */
+#define CHECK_JPEG_YCBCR_SUBSAMPLING 1
+
+/* Support MS MDI magic number files as TIFF */
+/* #undef MDI_SUPPORT */
+
+/*
+ * Feature support definitions.
+ * XXX: These macros are obsoleted. Don't use them in your apps!
+ * Macros stays here for backward compatibility and should be always defined.
+ */
+#define COLORIMETRY_SUPPORT
+#define YCBCR_SUPPORT
+#define CMYK_SUPPORT
+#define ICC_SUPPORT
+#define PHOTOSHOP_SUPPORT
+#define IPTC_SUPPORT
+
+#endif /* _TIFFCONF_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tiffconf.wince.h b/test/monniaux/tiff-4.0.10/tiffconf.wince.h
new file mode 100644
index 00000000..013b0960
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffconf.wince.h
@@ -0,0 +1,110 @@
+/*
+ * Windows CE platform tiffconf.wince.h
+ * Created by Mateusz Loskot (mateusz@loskot.net)
+ *
+ * NOTE: Requires WCELIBCEX library with wceex_* functions,
+ * It's an extension to C library on Windows CE platform.
+ * For example, HAVE_STDIO_H definition indicates there are
+ * following files available:
+ * stdio.h - from Windows CE / Windows Mobile SDK
+ * wce_stdio.h - from WCELIBCEX library
+ */
+
+
+/*
+ Configuration defines for installed libtiff.
+ This file maintained for backward compatibility. Do not use definitions
+ from this file in your programs.
+*/
+
+#ifndef _WIN32_WCE
+# error This version of tif_config.h header is dedicated for Windows CE platform!
+#endif
+
+
+#ifndef _TIFFCONF_
+#define _TIFFCONF_
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* Compatibility stuff. */
+
+/* Define as 0 or 1 according to the floating point format suported by the
+ machine */
+#define HAVE_IEEEFP 1
+
+/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
+
+/* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
+ (Intel) */
+#define HOST_BIGENDIAN 0
+
+/* Support CCITT Group 3 & 4 algorithms */
+#define CCITT_SUPPORT 1
+
+/* Support JPEG compression (requires IJG JPEG library) */
+/* #undef JPEG_SUPPORT */
+
+/* Support LogLuv high dynamic range encoding */
+#define LOGLUV_SUPPORT 1
+
+/* Support LZW algorithm */
+#define LZW_SUPPORT 1
+
+/* Support NeXT 2-bit RLE algorithm */
+#define NEXT_SUPPORT 1
+
+/* Support Old JPEG compresson (read contrib/ojpeg/README first! Compilation
+ fails with unpatched IJG JPEG library) */
+/* #undef OJPEG_SUPPORT */
+
+/* Support Macintosh PackBits algorithm */
+#define PACKBITS_SUPPORT 1
+
+/* Support Pixar log-format algorithm (requires Zlib) */
+/* #undef PIXARLOG_SUPPORT */
+
+/* Support ThunderScan 4-bit RLE algorithm */
+#define THUNDER_SUPPORT 1
+
+/* Support Deflate compression */
+/* #undef ZIP_SUPPORT */
+
+/* Support strip chopping (whether or not to convert single-strip uncompressed
+ images to mutiple strips of ~8Kb to reduce memory usage) */
+#define STRIPCHOP_DEFAULT TIFF_STRIPCHOP
+
+/* Enable SubIFD tag (330) support */
+#define SUBIFD_SUPPORT 1
+
+/* Treat extra sample as alpha (default enabled). The RGBA interface will
+ treat a fourth sample with no EXTRASAMPLE_ value as being ASSOCALPHA. Many
+ packages produce RGBA files but don't mark the alpha properly. */
+#define DEFAULT_EXTRASAMPLE_AS_ALPHA 1
+
+/* Pick up YCbCr subsampling info from the JPEG data stream to support files
+ lacking the tag (default enabled). */
+#define CHECK_JPEG_YCBCR_SUBSAMPLING 1
+
+/*
+ * Feature support definitions.
+ * XXX: These macros are obsoleted. Don't use them in your apps!
+ * Macros stays here for backward compatibility and should be always defined.
+ */
+#define COLORIMETRY_SUPPORT
+#define YCBCR_SUPPORT
+#define CMYK_SUPPORT
+#define ICC_SUPPORT
+#define PHOTOSHOP_SUPPORT
+#define IPTC_SUPPORT
+
+#endif /* _TIFFCONF_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tiffio.h b/test/monniaux/tiff-4.0.10/tiffio.h
new file mode 100644
index 00000000..31c2e676
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffio.h
@@ -0,0 +1,558 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _TIFFIO_
+#define _TIFFIO_
+
+/*
+ * TIFF I/O Library Definitions.
+ */
+#include "tiff.h"
+#include "tiffvers.h"
+
+/*
+ * TIFF is defined as an incomplete type to hide the
+ * library's internal data structures from clients.
+ */
+typedef struct tiff TIFF;
+
+/*
+ * The following typedefs define the intrinsic size of
+ * data types used in the *exported* interfaces. These
+ * definitions depend on the proper definition of types
+ * in tiff.h. Note also that the varargs interface used
+ * to pass tag types and values uses the types defined in
+ * tiff.h directly.
+ *
+ * NB: ttag_t is unsigned int and not unsigned short because
+ * ANSI C requires that the type before the ellipsis be a
+ * promoted type (i.e. one of int, unsigned int, pointer,
+ * or double) and because we defined pseudo-tags that are
+ * outside the range of legal Aldus-assigned tags.
+ * NB: tsize_t is signed and not unsigned because some functions
+ * return -1.
+ * NB: toff_t is not off_t for many reasons; TIFFs max out at
+ * 32-bit file offsets, and BigTIFF maxes out at 64-bit
+ * offsets being the most important, and to ensure use of
+ * a consistently unsigned type across architectures.
+ * Prior to libtiff 4.0, this was an unsigned 32 bit type.
+ */
+/*
+ * this is the machine addressing size type, only it's signed, so make it
+ * int32 on 32bit machines, int64 on 64bit machines
+ */
+typedef TIFF_SSIZE_T tmsize_t;
+typedef uint64 toff_t; /* file offset */
+/* the following are deprecated and should be replaced by their defining
+ counterparts */
+typedef uint32 ttag_t; /* directory tag */
+typedef uint16 tdir_t; /* directory index */
+typedef uint16 tsample_t; /* sample number */
+typedef uint32 tstrile_t; /* strip or tile number */
+typedef tstrile_t tstrip_t; /* strip number */
+typedef tstrile_t ttile_t; /* tile number */
+typedef tmsize_t tsize_t; /* i/o size in bytes */
+typedef void* tdata_t; /* image data ref */
+
+#if !defined(__WIN32__) && (defined(_WIN32) || defined(WIN32))
+#define __WIN32__
+#endif
+
+/*
+ * On windows you should define USE_WIN32_FILEIO if you are using tif_win32.c
+ * or AVOID_WIN32_FILEIO if you are using something else (like tif_unix.c).
+ *
+ * By default tif_unix.c is assumed.
+ */
+
+#if defined(_WINDOWS) || defined(__WIN32__) || defined(_Windows)
+# if !defined(__CYGWIN) && !defined(AVOID_WIN32_FILEIO) && !defined(USE_WIN32_FILEIO)
+# define AVOID_WIN32_FILEIO
+# endif
+#endif
+
+#if defined(USE_WIN32_FILEIO)
+# define VC_EXTRALEAN
+# include <windows.h>
+# ifdef __WIN32__
+DECLARE_HANDLE(thandle_t); /* Win32 file handle */
+# else
+typedef HFILE thandle_t; /* client data handle */
+# endif /* __WIN32__ */
+#else
+typedef void* thandle_t; /* client data handle */
+#endif /* USE_WIN32_FILEIO */
+
+/*
+ * Flags to pass to TIFFPrintDirectory to control
+ * printing of data structures that are potentially
+ * very large. Bit-or these flags to enable printing
+ * multiple items.
+ */
+#define TIFFPRINT_NONE 0x0 /* no extra info */
+#define TIFFPRINT_STRIPS 0x1 /* strips/tiles info */
+#define TIFFPRINT_CURVES 0x2 /* color/gray response curves */
+#define TIFFPRINT_COLORMAP 0x4 /* colormap */
+#define TIFFPRINT_JPEGQTABLES 0x100 /* JPEG Q matrices */
+#define TIFFPRINT_JPEGACTABLES 0x200 /* JPEG AC tables */
+#define TIFFPRINT_JPEGDCTABLES 0x200 /* JPEG DC tables */
+
+/*
+ * Colour conversion stuff
+ */
+
+/* reference white */
+#define D65_X0 (95.0470F)
+#define D65_Y0 (100.0F)
+#define D65_Z0 (108.8827F)
+
+#define D50_X0 (96.4250F)
+#define D50_Y0 (100.0F)
+#define D50_Z0 (82.4680F)
+
+/* Structure for holding information about a display device. */
+
+typedef unsigned char TIFFRGBValue; /* 8-bit samples */
+
+typedef struct {
+ float d_mat[3][3]; /* XYZ -> luminance matrix */
+ float d_YCR; /* Light o/p for reference white */
+ float d_YCG;
+ float d_YCB;
+ uint32 d_Vrwr; /* Pixel values for ref. white */
+ uint32 d_Vrwg;
+ uint32 d_Vrwb;
+ float d_Y0R; /* Residual light for black pixel */
+ float d_Y0G;
+ float d_Y0B;
+ float d_gammaR; /* Gamma values for the three guns */
+ float d_gammaG;
+ float d_gammaB;
+} TIFFDisplay;
+
+typedef struct { /* YCbCr->RGB support */
+ TIFFRGBValue* clamptab; /* range clamping table */
+ int* Cr_r_tab;
+ int* Cb_b_tab;
+ int32* Cr_g_tab;
+ int32* Cb_g_tab;
+ int32* Y_tab;
+} TIFFYCbCrToRGB;
+
+typedef struct { /* CIE Lab 1976->RGB support */
+ int range; /* Size of conversion table */
+#define CIELABTORGB_TABLE_RANGE 1500
+ float rstep, gstep, bstep;
+ float X0, Y0, Z0; /* Reference white point */
+ TIFFDisplay display;
+ float Yr2r[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yr to r */
+ float Yg2g[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yg to g */
+ float Yb2b[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yb to b */
+} TIFFCIELabToRGB;
+
+/*
+ * RGBA-style image support.
+ */
+typedef struct _TIFFRGBAImage TIFFRGBAImage;
+/*
+ * The image reading and conversion routines invoke
+ * ``put routines'' to copy/image/whatever tiles of
+ * raw image data. A default set of routines are
+ * provided to convert/copy raw image data to 8-bit
+ * packed ABGR format rasters. Applications can supply
+ * alternate routines that unpack the data into a
+ * different format or, for example, unpack the data
+ * and draw the unpacked raster on the display.
+ */
+typedef void (*tileContigRoutine)
+ (TIFFRGBAImage*, uint32*, uint32, uint32, uint32, uint32, int32, int32,
+ unsigned char*);
+typedef void (*tileSeparateRoutine)
+ (TIFFRGBAImage*, uint32*, uint32, uint32, uint32, uint32, int32, int32,
+ unsigned char*, unsigned char*, unsigned char*, unsigned char*);
+/*
+ * RGBA-reader state.
+ */
+struct _TIFFRGBAImage {
+ TIFF* tif; /* image handle */
+ int stoponerr; /* stop on read error */
+ int isContig; /* data is packed/separate */
+ int alpha; /* type of alpha data present */
+ uint32 width; /* image width */
+ uint32 height; /* image height */
+ uint16 bitspersample; /* image bits/sample */
+ uint16 samplesperpixel; /* image samples/pixel */
+ uint16 orientation; /* image orientation */
+ uint16 req_orientation; /* requested orientation */
+ uint16 photometric; /* image photometric interp */
+ uint16* redcmap; /* colormap palette */
+ uint16* greencmap;
+ uint16* bluecmap;
+ /* get image data routine */
+ int (*get)(TIFFRGBAImage*, uint32*, uint32, uint32);
+ /* put decoded strip/tile */
+ union {
+ void (*any)(TIFFRGBAImage*);
+ tileContigRoutine contig;
+ tileSeparateRoutine separate;
+ } put;
+ TIFFRGBValue* Map; /* sample mapping array */
+ uint32** BWmap; /* black&white map */
+ uint32** PALmap; /* palette image map */
+ TIFFYCbCrToRGB* ycbcr; /* YCbCr conversion state */
+ TIFFCIELabToRGB* cielab; /* CIE L*a*b conversion state */
+
+ uint8* UaToAa; /* Unassociated alpha to associated alpha conversion LUT */
+ uint8* Bitdepth16To8; /* LUT for conversion from 16bit to 8bit values */
+
+ int row_offset;
+ int col_offset;
+};
+
+/*
+ * Macros for extracting components from the
+ * packed ABGR form returned by TIFFReadRGBAImage.
+ */
+#define TIFFGetR(abgr) ((abgr) & 0xff)
+#define TIFFGetG(abgr) (((abgr) >> 8) & 0xff)
+#define TIFFGetB(abgr) (((abgr) >> 16) & 0xff)
+#define TIFFGetA(abgr) (((abgr) >> 24) & 0xff)
+
+/*
+ * A CODEC is a software package that implements decoding,
+ * encoding, or decoding+encoding of a compression algorithm.
+ * The library provides a collection of builtin codecs.
+ * More codecs may be registered through calls to the library
+ * and/or the builtin implementations may be overridden.
+ */
+typedef int (*TIFFInitMethod)(TIFF*, int);
+typedef struct {
+ char* name;
+ uint16 scheme;
+ TIFFInitMethod init;
+} TIFFCodec;
+
+#include <stdio.h>
+#include <stdarg.h>
+
+/* share internal LogLuv conversion routines? */
+#ifndef LOGLUV_PUBLIC
+#define LOGLUV_PUBLIC 1
+#endif
+
+#if !defined(__GNUC__) && !defined(__attribute__)
+# define __attribute__(x) /*nothing*/
+#endif
+
+#if defined(c_plusplus) || defined(__cplusplus)
+extern "C" {
+#endif
+typedef void (*TIFFErrorHandler)(const char*, const char*, va_list);
+typedef void (*TIFFErrorHandlerExt)(thandle_t, const char*, const char*, va_list);
+typedef tmsize_t (*TIFFReadWriteProc)(thandle_t, void*, tmsize_t);
+typedef toff_t (*TIFFSeekProc)(thandle_t, toff_t, int);
+typedef int (*TIFFCloseProc)(thandle_t);
+typedef toff_t (*TIFFSizeProc)(thandle_t);
+typedef int (*TIFFMapFileProc)(thandle_t, void** base, toff_t* size);
+typedef void (*TIFFUnmapFileProc)(thandle_t, void* base, toff_t size);
+typedef void (*TIFFExtendProc)(TIFF*);
+
+extern const char* TIFFGetVersion(void);
+
+extern const TIFFCodec* TIFFFindCODEC(uint16);
+extern TIFFCodec* TIFFRegisterCODEC(uint16, const char*, TIFFInitMethod);
+extern void TIFFUnRegisterCODEC(TIFFCodec*);
+extern int TIFFIsCODECConfigured(uint16);
+extern TIFFCodec* TIFFGetConfiguredCODECs(void);
+
+/*
+ * Auxiliary functions.
+ */
+
+extern void* _TIFFmalloc(tmsize_t s);
+extern void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz);
+extern void* _TIFFrealloc(void* p, tmsize_t s);
+extern void _TIFFmemset(void* p, int v, tmsize_t c);
+extern void _TIFFmemcpy(void* d, const void* s, tmsize_t c);
+extern int _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c);
+extern void _TIFFfree(void* p);
+
+/*
+** Stuff, related to tag handling and creating custom tags.
+*/
+extern int TIFFGetTagListCount( TIFF * );
+extern uint32 TIFFGetTagListEntry( TIFF *, int tag_index );
+
+#define TIFF_ANY TIFF_NOTYPE /* for field descriptor searching */
+#define TIFF_VARIABLE -1 /* marker for variable length tags */
+#define TIFF_SPP -2 /* marker for SamplesPerPixel tags */
+#define TIFF_VARIABLE2 -3 /* marker for uint32 var-length tags */
+
+#define FIELD_CUSTOM 65
+
+typedef struct _TIFFField TIFFField;
+typedef struct _TIFFFieldArray TIFFFieldArray;
+
+extern const TIFFField* TIFFFindField(TIFF *, uint32, TIFFDataType);
+extern const TIFFField* TIFFFieldWithTag(TIFF*, uint32);
+extern const TIFFField* TIFFFieldWithName(TIFF*, const char *);
+
+extern uint32 TIFFFieldTag(const TIFFField*);
+extern const char* TIFFFieldName(const TIFFField*);
+extern TIFFDataType TIFFFieldDataType(const TIFFField*);
+extern int TIFFFieldPassCount(const TIFFField*);
+extern int TIFFFieldReadCount(const TIFFField*);
+extern int TIFFFieldWriteCount(const TIFFField*);
+
+typedef int (*TIFFVSetMethod)(TIFF*, uint32, va_list);
+typedef int (*TIFFVGetMethod)(TIFF*, uint32, va_list);
+typedef void (*TIFFPrintMethod)(TIFF*, FILE*, long);
+
+typedef struct {
+ TIFFVSetMethod vsetfield; /* tag set routine */
+ TIFFVGetMethod vgetfield; /* tag get routine */
+ TIFFPrintMethod printdir; /* directory print routine */
+} TIFFTagMethods;
+
+extern TIFFTagMethods *TIFFAccessTagMethods(TIFF *);
+extern void *TIFFGetClientInfo(TIFF *, const char *);
+extern void TIFFSetClientInfo(TIFF *, void *, const char *);
+
+extern void TIFFCleanup(TIFF* tif);
+extern void TIFFClose(TIFF* tif);
+extern int TIFFFlush(TIFF* tif);
+extern int TIFFFlushData(TIFF* tif);
+extern int TIFFGetField(TIFF* tif, uint32 tag, ...);
+extern int TIFFVGetField(TIFF* tif, uint32 tag, va_list ap);
+extern int TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...);
+extern int TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap);
+extern int TIFFReadDirectory(TIFF* tif);
+extern int TIFFReadCustomDirectory(TIFF* tif, toff_t diroff, const TIFFFieldArray* infoarray);
+extern int TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff);
+extern uint64 TIFFScanlineSize64(TIFF* tif);
+extern tmsize_t TIFFScanlineSize(TIFF* tif);
+extern uint64 TIFFRasterScanlineSize64(TIFF* tif);
+extern tmsize_t TIFFRasterScanlineSize(TIFF* tif);
+extern uint64 TIFFStripSize64(TIFF* tif);
+extern tmsize_t TIFFStripSize(TIFF* tif);
+extern uint64 TIFFRawStripSize64(TIFF* tif, uint32 strip);
+extern tmsize_t TIFFRawStripSize(TIFF* tif, uint32 strip);
+extern uint64 TIFFVStripSize64(TIFF* tif, uint32 nrows);
+extern tmsize_t TIFFVStripSize(TIFF* tif, uint32 nrows);
+extern uint64 TIFFTileRowSize64(TIFF* tif);
+extern tmsize_t TIFFTileRowSize(TIFF* tif);
+extern uint64 TIFFTileSize64(TIFF* tif);
+extern tmsize_t TIFFTileSize(TIFF* tif);
+extern uint64 TIFFVTileSize64(TIFF* tif, uint32 nrows);
+extern tmsize_t TIFFVTileSize(TIFF* tif, uint32 nrows);
+extern uint32 TIFFDefaultStripSize(TIFF* tif, uint32 request);
+extern void TIFFDefaultTileSize(TIFF*, uint32*, uint32*);
+extern int TIFFFileno(TIFF*);
+extern int TIFFSetFileno(TIFF*, int);
+extern thandle_t TIFFClientdata(TIFF*);
+extern thandle_t TIFFSetClientdata(TIFF*, thandle_t);
+extern int TIFFGetMode(TIFF*);
+extern int TIFFSetMode(TIFF*, int);
+extern int TIFFIsTiled(TIFF*);
+extern int TIFFIsByteSwapped(TIFF*);
+extern int TIFFIsUpSampled(TIFF*);
+extern int TIFFIsMSB2LSB(TIFF*);
+extern int TIFFIsBigEndian(TIFF*);
+extern TIFFReadWriteProc TIFFGetReadProc(TIFF*);
+extern TIFFReadWriteProc TIFFGetWriteProc(TIFF*);
+extern TIFFSeekProc TIFFGetSeekProc(TIFF*);
+extern TIFFCloseProc TIFFGetCloseProc(TIFF*);
+extern TIFFSizeProc TIFFGetSizeProc(TIFF*);
+extern TIFFMapFileProc TIFFGetMapFileProc(TIFF*);
+extern TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF*);
+extern uint32 TIFFCurrentRow(TIFF*);
+extern uint16 TIFFCurrentDirectory(TIFF*);
+extern uint16 TIFFNumberOfDirectories(TIFF*);
+extern uint64 TIFFCurrentDirOffset(TIFF*);
+extern uint32 TIFFCurrentStrip(TIFF*);
+extern uint32 TIFFCurrentTile(TIFF* tif);
+extern int TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size);
+extern int TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size);
+extern int TIFFSetupStrips(TIFF *);
+extern int TIFFWriteCheck(TIFF*, int, const char *);
+extern void TIFFFreeDirectory(TIFF*);
+extern int TIFFCreateDirectory(TIFF*);
+extern int TIFFCreateCustomDirectory(TIFF*,const TIFFFieldArray*);
+extern int TIFFCreateEXIFDirectory(TIFF*);
+extern int TIFFLastDirectory(TIFF*);
+extern int TIFFSetDirectory(TIFF*, uint16);
+extern int TIFFSetSubDirectory(TIFF*, uint64);
+extern int TIFFUnlinkDirectory(TIFF*, uint16);
+extern int TIFFSetField(TIFF*, uint32, ...);
+extern int TIFFVSetField(TIFF*, uint32, va_list);
+extern int TIFFUnsetField(TIFF*, uint32);
+extern int TIFFWriteDirectory(TIFF *);
+extern int TIFFWriteCustomDirectory(TIFF *, uint64 *);
+extern int TIFFCheckpointDirectory(TIFF *);
+extern int TIFFRewriteDirectory(TIFF *);
+
+#if defined(c_plusplus) || defined(__cplusplus)
+extern void TIFFPrintDirectory(TIFF*, FILE*, long = 0);
+extern int TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample = 0);
+extern int TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample = 0);
+extern int TIFFReadRGBAImage(TIFF*, uint32, uint32, uint32*, int = 0);
+extern int TIFFReadRGBAImageOriented(TIFF*, uint32, uint32, uint32*,
+ int = ORIENTATION_BOTLEFT, int = 0);
+#else
+extern void TIFFPrintDirectory(TIFF*, FILE*, long);
+extern int TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample);
+extern int TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample);
+extern int TIFFReadRGBAImage(TIFF*, uint32, uint32, uint32*, int);
+extern int TIFFReadRGBAImageOriented(TIFF*, uint32, uint32, uint32*, int, int);
+#endif
+
+extern int TIFFReadRGBAStrip(TIFF*, uint32, uint32 * );
+extern int TIFFReadRGBATile(TIFF*, uint32, uint32, uint32 * );
+extern int TIFFReadRGBAStripExt(TIFF*, uint32, uint32 *, int stop_on_error );
+extern int TIFFReadRGBATileExt(TIFF*, uint32, uint32, uint32 *, int stop_on_error );
+extern int TIFFRGBAImageOK(TIFF*, char [1024]);
+extern int TIFFRGBAImageBegin(TIFFRGBAImage*, TIFF*, int, char [1024]);
+extern int TIFFRGBAImageGet(TIFFRGBAImage*, uint32*, uint32, uint32);
+extern void TIFFRGBAImageEnd(TIFFRGBAImage*);
+extern TIFF* TIFFOpen(const char*, const char*);
+# ifdef __WIN32__
+extern TIFF* TIFFOpenW(const wchar_t*, const char*);
+# endif /* __WIN32__ */
+extern TIFF* TIFFFdOpen(int, const char*, const char*);
+extern TIFF* TIFFClientOpen(const char*, const char*,
+ thandle_t,
+ TIFFReadWriteProc, TIFFReadWriteProc,
+ TIFFSeekProc, TIFFCloseProc,
+ TIFFSizeProc,
+ TIFFMapFileProc, TIFFUnmapFileProc);
+extern const char* TIFFFileName(TIFF*);
+extern const char* TIFFSetFileName(TIFF*, const char *);
+extern void TIFFError(const char*, const char*, ...) __attribute__((__format__ (__printf__,2,3)));
+extern void TIFFErrorExt(thandle_t, const char*, const char*, ...) __attribute__((__format__ (__printf__,3,4)));
+extern void TIFFWarning(const char*, const char*, ...) __attribute__((__format__ (__printf__,2,3)));
+extern void TIFFWarningExt(thandle_t, const char*, const char*, ...) __attribute__((__format__ (__printf__,3,4)));
+extern TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler);
+extern TIFFErrorHandlerExt TIFFSetErrorHandlerExt(TIFFErrorHandlerExt);
+extern TIFFErrorHandler TIFFSetWarningHandler(TIFFErrorHandler);
+extern TIFFErrorHandlerExt TIFFSetWarningHandlerExt(TIFFErrorHandlerExt);
+extern TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc);
+extern uint32 TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s);
+extern int TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s);
+extern uint32 TIFFNumberOfTiles(TIFF*);
+extern tmsize_t TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s);
+extern tmsize_t TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s);
+extern uint32 TIFFComputeStrip(TIFF*, uint32, uint16);
+extern uint32 TIFFNumberOfStrips(TIFF*);
+extern tmsize_t TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size);
+extern tmsize_t TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size);
+extern tmsize_t TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size);
+extern tmsize_t TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size);
+extern tmsize_t TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc);
+extern tmsize_t TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc);
+extern tmsize_t TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc);
+extern tmsize_t TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc);
+extern int TIFFDataWidth(TIFFDataType); /* table of tag datatype widths */
+extern void TIFFSetWriteOffset(TIFF* tif, toff_t off);
+extern void TIFFSwabShort(uint16*);
+extern void TIFFSwabLong(uint32*);
+extern void TIFFSwabLong8(uint64*);
+extern void TIFFSwabFloat(float*);
+extern void TIFFSwabDouble(double*);
+extern void TIFFSwabArrayOfShort(uint16* wp, tmsize_t n);
+extern void TIFFSwabArrayOfTriples(uint8* tp, tmsize_t n);
+extern void TIFFSwabArrayOfLong(uint32* lp, tmsize_t n);
+extern void TIFFSwabArrayOfLong8(uint64* lp, tmsize_t n);
+extern void TIFFSwabArrayOfFloat(float* fp, tmsize_t n);
+extern void TIFFSwabArrayOfDouble(double* dp, tmsize_t n);
+extern void TIFFReverseBits(uint8* cp, tmsize_t n);
+extern const unsigned char* TIFFGetBitRevTable(int);
+
+#ifdef LOGLUV_PUBLIC
+#define U_NEU 0.210526316
+#define V_NEU 0.473684211
+#define UVSCALE 410.
+extern double LogL16toY(int);
+extern double LogL10toY(int);
+extern void XYZtoRGB24(float*, uint8*);
+extern int uv_decode(double*, double*, int);
+extern void LogLuv24toXYZ(uint32, float*);
+extern void LogLuv32toXYZ(uint32, float*);
+#if defined(c_plusplus) || defined(__cplusplus)
+extern int LogL16fromY(double, int = SGILOGENCODE_NODITHER);
+extern int LogL10fromY(double, int = SGILOGENCODE_NODITHER);
+extern int uv_encode(double, double, int = SGILOGENCODE_NODITHER);
+extern uint32 LogLuv24fromXYZ(float*, int = SGILOGENCODE_NODITHER);
+extern uint32 LogLuv32fromXYZ(float*, int = SGILOGENCODE_NODITHER);
+#else
+extern int LogL16fromY(double, int);
+extern int LogL10fromY(double, int);
+extern int uv_encode(double, double, int);
+extern uint32 LogLuv24fromXYZ(float*, int);
+extern uint32 LogLuv32fromXYZ(float*, int);
+#endif
+#endif /* LOGLUV_PUBLIC */
+
+extern int TIFFCIELabToRGBInit(TIFFCIELabToRGB*, const TIFFDisplay *, float*);
+extern void TIFFCIELabToXYZ(TIFFCIELabToRGB *, uint32, int32, int32,
+ float *, float *, float *);
+extern void TIFFXYZToRGB(TIFFCIELabToRGB *, float, float, float,
+ uint32 *, uint32 *, uint32 *);
+
+extern int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB*, float*, float*);
+extern void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *, uint32, int32, int32,
+ uint32 *, uint32 *, uint32 *);
+
+/****************************************************************************
+ * O B S O L E T E D I N T E R F A C E S
+ *
+ * Don't use this stuff in your applications, it may be removed in the future
+ * libtiff versions.
+ ****************************************************************************/
+typedef struct {
+ ttag_t field_tag; /* field's tag */
+ short field_readcount; /* read count/TIFF_VARIABLE/TIFF_SPP */
+ short field_writecount; /* write count/TIFF_VARIABLE */
+ TIFFDataType field_type; /* type of associated data */
+ unsigned short field_bit; /* bit in fieldsset bit vector */
+ unsigned char field_oktochange; /* if true, can change while writing */
+ unsigned char field_passcount; /* if true, pass dir count on set */
+ char *field_name; /* ASCII name */
+} TIFFFieldInfo;
+
+extern int TIFFMergeFieldInfo(TIFF*, const TIFFFieldInfo[], uint32);
+
+#if defined(c_plusplus) || defined(__cplusplus)
+}
+#endif
+
+#endif /* _TIFFIO_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tiffiop.h b/test/monniaux/tiff-4.0.10/tiffiop.h
new file mode 100644
index 00000000..186c291f
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffiop.h
@@ -0,0 +1,454 @@
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _TIFFIOP_
+#define _TIFFIOP_
+/*
+ * ``Library-private'' definitions.
+ */
+
+#include "tif_config.h"
+
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+#ifdef HAVE_STRING_H
+# include <string.h>
+#endif
+
+#ifdef HAVE_ASSERT_H
+# include <assert.h>
+#else
+# define assert(x)
+#endif
+
+#ifdef HAVE_SEARCH_H
+# include <search.h>
+#else
+extern void *lfind(const void *, const void *, size_t *, size_t,
+ int (*)(const void *, const void *));
+#endif
+
+#if !defined(HAVE_SNPRINTF) && !defined(HAVE__SNPRINTF)
+#undef snprintf
+#define snprintf _TIFF_snprintf_f
+extern int snprintf(char* str, size_t size, const char* format, ...);
+#endif
+
+#include "tiffio.h"
+
+#include "tif_dir.h"
+
+#ifndef STRIP_SIZE_DEFAULT
+# define STRIP_SIZE_DEFAULT 8192
+#endif
+
+#define streq(a,b) (strcmp(a,b) == 0)
+#define strneq(a,b,n) (strncmp(a,b,n) == 0)
+
+#ifndef TRUE
+#define TRUE 1
+#define FALSE 0
+#endif
+
+typedef struct client_info {
+ struct client_info *next;
+ void *data;
+ char *name;
+} TIFFClientInfoLink;
+
+/*
+ * Typedefs for ``method pointers'' used internally.
+ * these are deprecated and provided only for backwards compatibility.
+ */
+typedef unsigned char tidataval_t; /* internal image data value type */
+typedef tidataval_t* tidata_t; /* reference to internal image data */
+
+typedef void (*TIFFVoidMethod)(TIFF*);
+typedef int (*TIFFBoolMethod)(TIFF*);
+typedef int (*TIFFPreMethod)(TIFF*, uint16);
+typedef int (*TIFFCodeMethod)(TIFF* tif, uint8* buf, tmsize_t size, uint16 sample);
+typedef int (*TIFFSeekMethod)(TIFF*, uint32);
+typedef void (*TIFFPostMethod)(TIFF* tif, uint8* buf, tmsize_t size);
+typedef uint32 (*TIFFStripMethod)(TIFF*, uint32);
+typedef void (*TIFFTileMethod)(TIFF*, uint32*, uint32*);
+
+struct tiff {
+ char* tif_name; /* name of open file */
+ int tif_fd; /* open file descriptor */
+ int tif_mode; /* open mode (O_*) */
+ uint32 tif_flags;
+ #define TIFF_FILLORDER 0x00003U /* natural bit fill order for machine */
+ #define TIFF_DIRTYHEADER 0x00004U /* header must be written on close */
+ #define TIFF_DIRTYDIRECT 0x00008U /* current directory must be written */
+ #define TIFF_BUFFERSETUP 0x00010U /* data buffers setup */
+ #define TIFF_CODERSETUP 0x00020U /* encoder/decoder setup done */
+ #define TIFF_BEENWRITING 0x00040U /* written 1+ scanlines to file */
+ #define TIFF_SWAB 0x00080U /* byte swap file information */
+ #define TIFF_NOBITREV 0x00100U /* inhibit bit reversal logic */
+ #define TIFF_MYBUFFER 0x00200U /* my raw data buffer; free on close */
+ #define TIFF_ISTILED 0x00400U /* file is tile, not strip- based */
+ #define TIFF_MAPPED 0x00800U /* file is mapped into memory */
+ #define TIFF_POSTENCODE 0x01000U /* need call to postencode routine */
+ #define TIFF_INSUBIFD 0x02000U /* currently writing a subifd */
+ #define TIFF_UPSAMPLED 0x04000U /* library is doing data up-sampling */
+ #define TIFF_STRIPCHOP 0x08000U /* enable strip chopping support */
+ #define TIFF_HEADERONLY 0x10000U /* read header only, do not process the first directory */
+ #define TIFF_NOREADRAW 0x20000U /* skip reading of raw uncompressed image data */
+ #define TIFF_INCUSTOMIFD 0x40000U /* currently writing a custom IFD */
+ #define TIFF_BIGTIFF 0x80000U /* read/write bigtiff */
+ #define TIFF_BUF4WRITE 0x100000U /* rawcc bytes are for writing */
+ #define TIFF_DIRTYSTRIP 0x200000U /* stripoffsets/stripbytecount dirty*/
+ #define TIFF_PERSAMPLE 0x400000U /* get/set per sample tags as arrays */
+ #define TIFF_BUFFERMMAP 0x800000U /* read buffer (tif_rawdata) points into mmap() memory */
+ uint64 tif_diroff; /* file offset of current directory */
+ uint64 tif_nextdiroff; /* file offset of following directory */
+ uint64* tif_dirlist; /* list of offsets to already seen directories to prevent IFD looping */
+ uint16 tif_dirlistsize; /* number of entries in offset list */
+ uint16 tif_dirnumber; /* number of already seen directories */
+ TIFFDirectory tif_dir; /* internal rep of current directory */
+ TIFFDirectory tif_customdir; /* custom IFDs are separated from the main ones */
+ union {
+ TIFFHeaderCommon common;
+ TIFFHeaderClassic classic;
+ TIFFHeaderBig big;
+ } tif_header;
+ uint16 tif_header_size; /* file's header block and its length */
+ uint32 tif_row; /* current scanline */
+ uint16 tif_curdir; /* current directory (index) */
+ uint32 tif_curstrip; /* current strip for read/write */
+ uint64 tif_curoff; /* current offset for read/write */
+ uint64 tif_dataoff; /* current offset for writing dir */
+ /* SubIFD support */
+ uint16 tif_nsubifd; /* remaining subifds to write */
+ uint64 tif_subifdoff; /* offset for patching SubIFD link */
+ /* tiling support */
+ uint32 tif_col; /* current column (offset by row too) */
+ uint32 tif_curtile; /* current tile for read/write */
+ tmsize_t tif_tilesize; /* # of bytes in a tile */
+ /* compression scheme hooks */
+ int tif_decodestatus;
+ TIFFBoolMethod tif_fixuptags; /* called in TIFFReadDirectory */
+ TIFFBoolMethod tif_setupdecode; /* called once before predecode */
+ TIFFPreMethod tif_predecode; /* pre- row/strip/tile decoding */
+ TIFFBoolMethod tif_setupencode; /* called once before preencode */
+ int tif_encodestatus;
+ TIFFPreMethod tif_preencode; /* pre- row/strip/tile encoding */
+ TIFFBoolMethod tif_postencode; /* post- row/strip/tile encoding */
+ TIFFCodeMethod tif_decoderow; /* scanline decoding routine */
+ TIFFCodeMethod tif_encoderow; /* scanline encoding routine */
+ TIFFCodeMethod tif_decodestrip; /* strip decoding routine */
+ TIFFCodeMethod tif_encodestrip; /* strip encoding routine */
+ TIFFCodeMethod tif_decodetile; /* tile decoding routine */
+ TIFFCodeMethod tif_encodetile; /* tile encoding routine */
+ TIFFVoidMethod tif_close; /* cleanup-on-close routine */
+ TIFFSeekMethod tif_seek; /* position within a strip routine */
+ TIFFVoidMethod tif_cleanup; /* cleanup state routine */
+ TIFFStripMethod tif_defstripsize; /* calculate/constrain strip size */
+ TIFFTileMethod tif_deftilesize; /* calculate/constrain tile size */
+ uint8* tif_data; /* compression scheme private data */
+ /* input/output buffering */
+ tmsize_t tif_scanlinesize; /* # of bytes in a scanline */
+ tmsize_t tif_scanlineskew; /* scanline skew for reading strips */
+ uint8* tif_rawdata; /* raw data buffer */
+ tmsize_t tif_rawdatasize; /* # of bytes in raw data buffer */
+ tmsize_t tif_rawdataoff; /* rawdata offset within strip */
+ tmsize_t tif_rawdataloaded;/* amount of data in rawdata */
+ uint8* tif_rawcp; /* current spot in raw buffer */
+ tmsize_t tif_rawcc; /* bytes unread from raw buffer */
+ /* memory-mapped file support */
+ uint8* tif_base; /* base of mapped file */
+ tmsize_t tif_size; /* size of mapped file region (bytes, thus tmsize_t) */
+ TIFFMapFileProc tif_mapproc; /* map file method */
+ TIFFUnmapFileProc tif_unmapproc; /* unmap file method */
+ /* input/output callback methods */
+ thandle_t tif_clientdata; /* callback parameter */
+ TIFFReadWriteProc tif_readproc; /* read method */
+ TIFFReadWriteProc tif_writeproc; /* write method */
+ TIFFSeekProc tif_seekproc; /* lseek method */
+ TIFFCloseProc tif_closeproc; /* close method */
+ TIFFSizeProc tif_sizeproc; /* filesize method */
+ /* post-decoding support */
+ TIFFPostMethod tif_postdecode; /* post decoding routine */
+ /* tag support */
+ TIFFField** tif_fields; /* sorted table of registered tags */
+ size_t tif_nfields; /* # entries in registered tag table */
+ const TIFFField* tif_foundfield; /* cached pointer to already found tag */
+ TIFFTagMethods tif_tagmethods; /* tag get/set/print routines */
+ TIFFClientInfoLink* tif_clientinfo; /* extra client information. */
+ /* Backward compatibility stuff. We need these two fields for
+ * setting up an old tag extension scheme. */
+ TIFFFieldArray* tif_fieldscompat;
+ size_t tif_nfieldscompat;
+};
+
+#define isPseudoTag(t) (t > 0xffff) /* is tag value normal or pseudo */
+
+#define isTiled(tif) (((tif)->tif_flags & TIFF_ISTILED) != 0)
+#define isMapped(tif) (((tif)->tif_flags & TIFF_MAPPED) != 0)
+#define isFillOrder(tif, o) (((tif)->tif_flags & (o)) != 0)
+#define isUpSampled(tif) (((tif)->tif_flags & TIFF_UPSAMPLED) != 0)
+#define TIFFReadFile(tif, buf, size) \
+ ((*(tif)->tif_readproc)((tif)->tif_clientdata,(buf),(size)))
+#define TIFFWriteFile(tif, buf, size) \
+ ((*(tif)->tif_writeproc)((tif)->tif_clientdata,(buf),(size)))
+#define TIFFSeekFile(tif, off, whence) \
+ ((*(tif)->tif_seekproc)((tif)->tif_clientdata,(off),(whence)))
+#define TIFFCloseFile(tif) \
+ ((*(tif)->tif_closeproc)((tif)->tif_clientdata))
+#define TIFFGetFileSize(tif) \
+ ((*(tif)->tif_sizeproc)((tif)->tif_clientdata))
+#define TIFFMapFileContents(tif, paddr, psize) \
+ ((*(tif)->tif_mapproc)((tif)->tif_clientdata,(paddr),(psize)))
+#define TIFFUnmapFileContents(tif, addr, size) \
+ ((*(tif)->tif_unmapproc)((tif)->tif_clientdata,(addr),(size)))
+
+/*
+ * Default Read/Seek/Write definitions.
+ */
+#ifndef ReadOK
+#define ReadOK(tif, buf, size) \
+ (TIFFReadFile((tif),(buf),(size))==(size))
+#endif
+#ifndef SeekOK
+#define SeekOK(tif, off) _TIFFSeekOK(tif, off)
+#endif
+#ifndef WriteOK
+#define WriteOK(tif, buf, size) \
+ (TIFFWriteFile((tif),(buf),(size))==(size))
+#endif
+
+/* NB: the uint32 casts are to silence certain ANSI-C compilers */
+#define TIFFhowmany_32(x, y) (((uint32)x < (0xffffffff - (uint32)(y-1))) ? \
+ ((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y))) : \
+ 0U)
+/* Variant of TIFFhowmany_32() that doesn't return 0 if x close to MAXUINT. */
+/* Caution: TIFFhowmany_32_maxuint_compat(x,y)*y might overflow */
+#define TIFFhowmany_32_maxuint_compat(x, y) \
+ (((uint32)(x) / (uint32)(y)) + ((((uint32)(x) % (uint32)(y)) != 0) ? 1 : 0))
+#define TIFFhowmany8_32(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
+#define TIFFroundup_32(x, y) (TIFFhowmany_32(x,y)*(y))
+#define TIFFhowmany_64(x, y) ((((uint64)(x))+(((uint64)(y))-1))/((uint64)(y)))
+#define TIFFhowmany8_64(x) (((x)&0x07)?((uint64)(x)>>3)+1:(uint64)(x)>>3)
+#define TIFFroundup_64(x, y) (TIFFhowmany_64(x,y)*(y))
+
+/* Safe multiply which returns zero if there is an integer overflow */
+#define TIFFSafeMultiply(t,v,m) ((((t)(m) != (t)0) && (((t)(((v)*(m))/(m))) == (t)(v))) ? (t)((v)*(m)) : (t)0)
+
+#define TIFFmax(A,B) ((A)>(B)?(A):(B))
+#define TIFFmin(A,B) ((A)<(B)?(A):(B))
+
+#define TIFFArrayCount(a) (sizeof (a) / sizeof ((a)[0]))
+
+/*
+ Support for large files.
+
+ Windows read/write APIs support only 'unsigned int' rather than 'size_t'.
+ Windows off_t is only 32-bit, even in 64-bit builds.
+*/
+#if defined(HAVE_FSEEKO)
+/*
+ Use fseeko() and ftello() if they are available since they use
+ 'off_t' rather than 'long'. It is wrong to use fseeko() and
+ ftello() only on systems with special LFS support since some systems
+ (e.g. FreeBSD) support a 64-bit off_t by default.
+
+ For MinGW, __MSVCRT_VERSION__ must be at least 0x800 to expose these
+ interfaces. The MinGW compiler must support the requested version. MinGW
+ does not distribute the CRT (it is supplied by Microsoft) so the correct CRT
+ must be available on the target computer in order for the program to run.
+*/
+#if defined(HAVE_FSEEKO)
+# define fseek(stream,offset,whence) fseeko(stream,offset,whence)
+# define ftell(stream,offset,whence) ftello(stream,offset,whence)
+#endif
+#endif
+#if defined(__WIN32__) && \
+ !(defined(_MSC_VER) && _MSC_VER < 1400) && \
+ !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
+typedef unsigned int TIFFIOSize_t;
+#define _TIFF_lseek_f(fildes,offset,whence) _lseeki64(fildes,/* __int64 */ offset,whence)
+/* #define _TIFF_tell_f(fildes) /\* __int64 *\/ _telli64(fildes) */
+#define _TIFF_fseek_f(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence)
+#define _TIFF_fstat_f(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff)
+/* #define _TIFF_ftell_f(stream) /\* __int64 *\/ _ftelli64(stream) */
+/* #define _TIFF_stat_f(path,stat_buff) _stati64(path,/\* struct _stati64 *\/ stat_buff) */
+#define _TIFF_stat_s struct _stati64
+#define _TIFF_off_t __int64
+#else
+typedef size_t TIFFIOSize_t;
+#define _TIFF_lseek_f(fildes,offset,whence) lseek(fildes,offset,whence)
+/* #define _TIFF_tell_f(fildes) (_TIFF_lseek_f(fildes,0,SEEK_CUR)) */
+#define _TIFF_fseek_f(stream,offset,whence) fseek(stream,offset,whence)
+#define _TIFF_fstat_f(fildes,stat_buff) fstat(fildes,stat_buff)
+/* #define _TIFF_ftell_f(stream) ftell(stream) */
+/* #define _TIFF_stat_f(path,stat_buff) stat(path,stat_buff) */
+#define _TIFF_stat_s struct stat
+#define _TIFF_off_t off_t
+#endif
+
+#if defined(__has_attribute) && defined(__clang__)
+#if __has_attribute(no_sanitize)
+#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
+#else
+#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+#endif
+#else
+#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+#endif
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+extern int _TIFFgetMode(const char* mode, const char* module);
+extern int _TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
+extern int _TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
+extern int _TIFFNoTileEncode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
+extern int _TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
+extern int _TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
+extern int _TIFFNoTileDecode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
+extern void _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
+extern int _TIFFNoPreCode(TIFF* tif, uint16 s);
+extern int _TIFFNoSeek(TIFF* tif, uint32 off);
+extern void _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc);
+extern void _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc);
+extern void _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc);
+extern void _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc);
+extern int TIFFFlushData1(TIFF* tif);
+extern int TIFFDefaultDirectory(TIFF* tif);
+extern void _TIFFSetDefaultCompressionState(TIFF* tif);
+extern int _TIFFRewriteField(TIFF *, uint16, TIFFDataType, tmsize_t, void *);
+extern int TIFFSetCompressionScheme(TIFF* tif, int scheme);
+extern int TIFFSetDefaultCompressionState(TIFF* tif);
+extern uint32 _TIFFDefaultStripSize(TIFF* tif, uint32 s);
+extern void _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th);
+extern int _TIFFDataSize(TIFFDataType type);
+
+extern void _TIFFsetByteArray(void**, void*, uint32);
+extern void _TIFFsetString(char**, char*);
+extern void _TIFFsetShortArray(uint16**, uint16*, uint32);
+extern void _TIFFsetLongArray(uint32**, uint32*, uint32);
+extern void _TIFFsetFloatArray(float**, float*, uint32);
+extern void _TIFFsetDoubleArray(double**, double*, uint32);
+
+extern void _TIFFprintAscii(FILE*, const char*);
+extern void _TIFFprintAsciiTag(FILE*, const char*, const char*);
+
+extern TIFFErrorHandler _TIFFwarningHandler;
+extern TIFFErrorHandler _TIFFerrorHandler;
+extern TIFFErrorHandlerExt _TIFFwarningHandlerExt;
+extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
+
+extern uint32 _TIFFMultiply32(TIFF*, uint32, uint32, const char*);
+extern uint64 _TIFFMultiply64(TIFF*, uint64, uint64, const char*);
+extern void* _TIFFCheckMalloc(TIFF*, tmsize_t, tmsize_t, const char*);
+extern void* _TIFFCheckRealloc(TIFF*, void*, tmsize_t, tmsize_t, const char*);
+
+extern double _TIFFUInt64ToDouble(uint64);
+extern float _TIFFUInt64ToFloat(uint64);
+
+extern tmsize_t
+_TIFFReadEncodedStripAndAllocBuffer(TIFF* tif, uint32 strip,
+ void **buf, tmsize_t bufsizetoalloc,
+ tmsize_t size_to_read);
+extern tmsize_t
+_TIFFReadEncodedTileAndAllocBuffer(TIFF* tif, uint32 tile,
+ void **buf, tmsize_t bufsizetoalloc,
+ tmsize_t size_to_read);
+extern tmsize_t
+_TIFFReadTileAndAllocBuffer(TIFF* tif,
+ void **buf, tmsize_t bufsizetoalloc,
+ uint32 x, uint32 y, uint32 z, uint16 s);
+extern int _TIFFSeekOK(TIFF* tif, toff_t off);
+
+extern int TIFFInitDumpMode(TIFF*, int);
+#ifdef PACKBITS_SUPPORT
+extern int TIFFInitPackBits(TIFF*, int);
+#endif
+#ifdef CCITT_SUPPORT
+extern int TIFFInitCCITTRLE(TIFF*, int), TIFFInitCCITTRLEW(TIFF*, int);
+extern int TIFFInitCCITTFax3(TIFF*, int), TIFFInitCCITTFax4(TIFF*, int);
+#endif
+#ifdef THUNDER_SUPPORT
+extern int TIFFInitThunderScan(TIFF*, int);
+#endif
+#ifdef NEXT_SUPPORT
+extern int TIFFInitNeXT(TIFF*, int);
+#endif
+#ifdef LZW_SUPPORT
+extern int TIFFInitLZW(TIFF*, int);
+#endif
+#ifdef OJPEG_SUPPORT
+extern int TIFFInitOJPEG(TIFF*, int);
+#endif
+#ifdef JPEG_SUPPORT
+extern int TIFFInitJPEG(TIFF*, int);
+extern int TIFFJPEGIsFullStripRequired(TIFF*);
+#endif
+#ifdef JBIG_SUPPORT
+extern int TIFFInitJBIG(TIFF*, int);
+#endif
+#ifdef ZIP_SUPPORT
+extern int TIFFInitZIP(TIFF*, int);
+#endif
+#ifdef PIXARLOG_SUPPORT
+extern int TIFFInitPixarLog(TIFF*, int);
+#endif
+#ifdef LOGLUV_SUPPORT
+extern int TIFFInitSGILog(TIFF*, int);
+#endif
+#ifdef LZMA_SUPPORT
+extern int TIFFInitLZMA(TIFF*, int);
+#endif
+#ifdef ZSTD_SUPPORT
+extern int TIFFInitZSTD(TIFF*, int);
+#endif
+#ifdef WEBP_SUPPORT
+extern int TIFFInitWebP(TIFF*, int);
+#endif
+#ifdef VMS
+extern const TIFFCodec _TIFFBuiltinCODECS[];
+#else
+extern TIFFCodec _TIFFBuiltinCODECS[];
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+#endif /* _TIFFIOP_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiff-4.0.10/tiffvers.h b/test/monniaux/tiff-4.0.10/tiffvers.h
new file mode 100644
index 00000000..403d61be
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/tiffvers.h
@@ -0,0 +1,9 @@
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.0.10\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
+/*
+ * This define can be used in code that requires
+ * compilation-related definitions specific to a
+ * version or versions of the library. Runtime
+ * version checking should be done based on the
+ * string returned by TIFFGetVersion.
+ */
+#define TIFFLIB_VERSION 20181110
diff --git a/test/monniaux/tiff-4.0.10/uvcode.h b/test/monniaux/tiff-4.0.10/uvcode.h
new file mode 100644
index 00000000..6286cfbb
--- /dev/null
+++ b/test/monniaux/tiff-4.0.10/uvcode.h
@@ -0,0 +1,180 @@
+/* Version 1.0 generated April 7, 1997 by Greg Ward Larson, SGI */
+#define UV_SQSIZ (float)0.003500
+#define UV_NDIVS 16289
+#define UV_VSTART (float)0.016940
+#define UV_NVS 163
+static const struct {
+ float ustart;
+ short nus, ncum;
+} uv_row[UV_NVS] = {
+ { (float)0.247663, 4, 0 },
+ { (float)0.243779, 6, 4 },
+ { (float)0.241684, 7, 10 },
+ { (float)0.237874, 9, 17 },
+ { (float)0.235906, 10, 26 },
+ { (float)0.232153, 12, 36 },
+ { (float)0.228352, 14, 48 },
+ { (float)0.226259, 15, 62 },
+ { (float)0.222371, 17, 77 },
+ { (float)0.220410, 18, 94 },
+ { (float)0.214710, 21, 112 },
+ { (float)0.212714, 22, 133 },
+ { (float)0.210721, 23, 155 },
+ { (float)0.204976, 26, 178 },
+ { (float)0.202986, 27, 204 },
+ { (float)0.199245, 29, 231 },
+ { (float)0.195525, 31, 260 },
+ { (float)0.193560, 32, 291 },
+ { (float)0.189878, 34, 323 },
+ { (float)0.186216, 36, 357 },
+ { (float)0.186216, 36, 393 },
+ { (float)0.182592, 38, 429 },
+ { (float)0.179003, 40, 467 },
+ { (float)0.175466, 42, 507 },
+ { (float)0.172001, 44, 549 },
+ { (float)0.172001, 44, 593 },
+ { (float)0.168612, 46, 637 },
+ { (float)0.168612, 46, 683 },
+ { (float)0.163575, 49, 729 },
+ { (float)0.158642, 52, 778 },
+ { (float)0.158642, 52, 830 },
+ { (float)0.158642, 52, 882 },
+ { (float)0.153815, 55, 934 },
+ { (float)0.153815, 55, 989 },
+ { (float)0.149097, 58, 1044 },
+ { (float)0.149097, 58, 1102 },
+ { (float)0.142746, 62, 1160 },
+ { (float)0.142746, 62, 1222 },
+ { (float)0.142746, 62, 1284 },
+ { (float)0.138270, 65, 1346 },
+ { (float)0.138270, 65, 1411 },
+ { (float)0.138270, 65, 1476 },
+ { (float)0.132166, 69, 1541 },
+ { (float)0.132166, 69, 1610 },
+ { (float)0.126204, 73, 1679 },
+ { (float)0.126204, 73, 1752 },
+ { (float)0.126204, 73, 1825 },
+ { (float)0.120381, 77, 1898 },
+ { (float)0.120381, 77, 1975 },
+ { (float)0.120381, 77, 2052 },
+ { (float)0.120381, 77, 2129 },
+ { (float)0.112962, 82, 2206 },
+ { (float)0.112962, 82, 2288 },
+ { (float)0.112962, 82, 2370 },
+ { (float)0.107450, 86, 2452 },
+ { (float)0.107450, 86, 2538 },
+ { (float)0.107450, 86, 2624 },
+ { (float)0.107450, 86, 2710 },
+ { (float)0.100343, 91, 2796 },
+ { (float)0.100343, 91, 2887 },
+ { (float)0.100343, 91, 2978 },
+ { (float)0.095126, 95, 3069 },
+ { (float)0.095126, 95, 3164 },
+ { (float)0.095126, 95, 3259 },
+ { (float)0.095126, 95, 3354 },
+ { (float)0.088276, 100, 3449 },
+ { (float)0.088276, 100, 3549 },
+ { (float)0.088276, 100, 3649 },
+ { (float)0.088276, 100, 3749 },
+ { (float)0.081523, 105, 3849 },
+ { (float)0.081523, 105, 3954 },
+ { (float)0.081523, 105, 4059 },
+ { (float)0.081523, 105, 4164 },
+ { (float)0.074861, 110, 4269 },
+ { (float)0.074861, 110, 4379 },
+ { (float)0.074861, 110, 4489 },
+ { (float)0.074861, 110, 4599 },
+ { (float)0.068290, 115, 4709 },
+ { (float)0.068290, 115, 4824 },
+ { (float)0.068290, 115, 4939 },
+ { (float)0.068290, 115, 5054 },
+ { (float)0.063573, 119, 5169 },
+ { (float)0.063573, 119, 5288 },
+ { (float)0.063573, 119, 5407 },
+ { (float)0.063573, 119, 5526 },
+ { (float)0.057219, 124, 5645 },
+ { (float)0.057219, 124, 5769 },
+ { (float)0.057219, 124, 5893 },
+ { (float)0.057219, 124, 6017 },
+ { (float)0.050985, 129, 6141 },
+ { (float)0.050985, 129, 6270 },
+ { (float)0.050985, 129, 6399 },
+ { (float)0.050985, 129, 6528 },
+ { (float)0.050985, 129, 6657 },
+ { (float)0.044859, 134, 6786 },
+ { (float)0.044859, 134, 6920 },
+ { (float)0.044859, 134, 7054 },
+ { (float)0.044859, 134, 7188 },
+ { (float)0.040571, 138, 7322 },
+ { (float)0.040571, 138, 7460 },
+ { (float)0.040571, 138, 7598 },
+ { (float)0.040571, 138, 7736 },
+ { (float)0.036339, 142, 7874 },
+ { (float)0.036339, 142, 8016 },
+ { (float)0.036339, 142, 8158 },
+ { (float)0.036339, 142, 8300 },
+ { (float)0.032139, 146, 8442 },
+ { (float)0.032139, 146, 8588 },
+ { (float)0.032139, 146, 8734 },
+ { (float)0.032139, 146, 8880 },
+ { (float)0.027947, 150, 9026 },
+ { (float)0.027947, 150, 9176 },
+ { (float)0.027947, 150, 9326 },
+ { (float)0.023739, 154, 9476 },
+ { (float)0.023739, 154, 9630 },
+ { (float)0.023739, 154, 9784 },
+ { (float)0.023739, 154, 9938 },
+ { (float)0.019504, 158, 10092 },
+ { (float)0.019504, 158, 10250 },
+ { (float)0.019504, 158, 10408 },
+ { (float)0.016976, 161, 10566 },
+ { (float)0.016976, 161, 10727 },
+ { (float)0.016976, 161, 10888 },
+ { (float)0.016976, 161, 11049 },
+ { (float)0.012639, 165, 11210 },
+ { (float)0.012639, 165, 11375 },
+ { (float)0.012639, 165, 11540 },
+ { (float)0.009991, 168, 11705 },
+ { (float)0.009991, 168, 11873 },
+ { (float)0.009991, 168, 12041 },
+ { (float)0.009016, 170, 12209 },
+ { (float)0.009016, 170, 12379 },
+ { (float)0.009016, 170, 12549 },
+ { (float)0.006217, 173, 12719 },
+ { (float)0.006217, 173, 12892 },
+ { (float)0.005097, 175, 13065 },
+ { (float)0.005097, 175, 13240 },
+ { (float)0.005097, 175, 13415 },
+ { (float)0.003909, 177, 13590 },
+ { (float)0.003909, 177, 13767 },
+ { (float)0.002340, 177, 13944 },
+ { (float)0.002389, 170, 14121 },
+ { (float)0.001068, 164, 14291 },
+ { (float)0.001653, 157, 14455 },
+ { (float)0.000717, 150, 14612 },
+ { (float)0.001614, 143, 14762 },
+ { (float)0.000270, 136, 14905 },
+ { (float)0.000484, 129, 15041 },
+ { (float)0.001103, 123, 15170 },
+ { (float)0.001242, 115, 15293 },
+ { (float)0.001188, 109, 15408 },
+ { (float)0.001011, 103, 15517 },
+ { (float)0.000709, 97, 15620 },
+ { (float)0.000301, 89, 15717 },
+ { (float)0.002416, 82, 15806 },
+ { (float)0.003251, 76, 15888 },
+ { (float)0.003246, 69, 15964 },
+ { (float)0.004141, 62, 16033 },
+ { (float)0.005963, 55, 16095 },
+ { (float)0.008839, 47, 16150 },
+ { (float)0.010490, 40, 16197 },
+ { (float)0.016994, 31, 16237 },
+ { (float)0.023659, 21, 16268 },
+};
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/test/monniaux/tiny-AES-c/Makefile b/test/monniaux/tiny-AES-c/Makefile
new file mode 100644
index 00000000..e9d821b3
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/Makefile
@@ -0,0 +1,61 @@
+#CC = avr-gcc
+#CFLAGS = -Wall -mmcu=atmega16 -Os -Wl,-Map,test.map
+#OBJCOPY = avr-objcopy
+CC = ../../../ccomp
+LD = ../../../ccomp
+AR = ar
+ARFLAGS = rcs
+CFLAGS = -Wall -Wno-c11-extensions -O3 -c
+LDFLAGS = -Wall -O3 # -Wl,-Map,test.map
+ifdef AES192
+CFLAGS += -DAES192=1
+endif
+ifdef AES256
+CFLAGS += -DAES256=1
+endif
+
+OBJCOPYFLAGS = -j .text -O ihex
+OBJCOPY = objcopy
+
+# include path to AVR library
+INCLUDE_PATH = /usr/lib/avr/include
+# splint static check
+SPLINT = splint test.c aes.c -I$(INCLUDE_PATH) +charindex -unrecog
+
+default: test.elf
+
+.SILENT:
+.PHONY: lint clean
+
+test.hex : test.elf
+ echo copy object-code to new image and format in hex
+ $(OBJCOPY) ${OBJCOPYFLAGS} $< $@
+
+test.o : test.c aes.h aes.o
+ echo [CC] $@ $(CFLAGS)
+ $(CC) $(CFLAGS) -o $@ $<
+
+aes.o : aes.c aes.h
+ echo [CC] $@ $(CFLAGS)
+ $(CC) $(CFLAGS) -o $@ $<
+
+test.elf : aes.o test.o
+ echo [LD] $@
+ $(LD) $(LDFLAGS) -o $@ $^
+
+aes.a : aes.o
+ echo [AR] $@
+ $(AR) $(ARFLAGS) $@ $^
+
+lib : aes.a
+
+clean:
+ rm -f *.OBJ *.LST *.o *.gch *.out *.hex *.map *.elf *.a
+
+test:
+ make clean && make && ./test.elf
+ make clean && make AES192=1 && ./test.elf
+ make clean && make AES256=1 && ./test.elf
+
+lint:
+ $(call SPLINT)
diff --git a/test/monniaux/tiny-AES-c/README.md b/test/monniaux/tiny-AES-c/README.md
new file mode 100644
index 00000000..e06cfdff
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/README.md
@@ -0,0 +1,80 @@
+### Tiny AES in C
+
+This is a small and portable implementation of the AES [ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29), [CTR](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29) and [CBC](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29) encryption algorithms written in C.
+
+You can override the default key-size of 128 bit with 192 or 256 bit by defining the symbols AES192 or AES256 in `aes.h`.
+
+The API is very simple and looks like this (I am using C99 `<stdint.h>`-style annotated types):
+
+```C
+/* Initialize context calling one of: */
+void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
+void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
+
+/* ... or reset IV at random point: */
+void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
+
+/* Then start encrypting and decrypting with the functions below: */
+void AES_ECB_encrypt(struct AES_ctx* ctx, uint8_t* buf);
+void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf);
+
+void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+
+/* Same function for encrypting as for decrypting in CTR mode */
+void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+```
+
+Note:
+ * No padding is provided so for CBC and ECB all buffers should be multiples of 16 bytes. For padding [PKCS7](https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7) is recommendable.
+ * ECB mode is considered unsafe for most uses and is not implemented in streaming mode. If you need this mode, call the function for every block of 16 bytes you need encrypted. See [wikipedia's article on ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_(ECB)) for more details.
+
+You can choose to use any or all of the modes-of-operations, by defining the symbols CBC, CTR or ECB. See the header file for clarification.
+
+C++ users should `#include` [aes.hpp](https://github.com/kokke/tiny-AES-c/blob/master/aes.hpp) instead of [aes.h](https://github.com/kokke/tiny-AES-c/blob/master/aes.h)
+
+There is no built-in error checking or protection from out-of-bounds memory access errors as a result of malicious input.
+
+The module uses less than 200 bytes of RAM and 1-2K ROM when compiled for ARM, but YMMV depending on which modes are enabled.
+
+It is one of the smallest implementations in C I've seen yet, but do contact me if you know of something smaller (or have improvements to the code here).
+
+I've successfully used the code on 64bit x86, 32bit ARM and 8 bit AVR platforms.
+
+
+GCC size output when only CTR mode is compiled for ARM:
+
+ $ arm-none-eabi-gcc -Os -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
+ $ size aes.o
+ text data bss dec hex filename
+ 1203 0 0 1203 4b3 aes.o
+
+.. and when compiling for the THUMB instruction set, we end up just below 1K in code size.
+
+ $ arm-none-eabi-gcc -Os -mthumb -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
+ $ size aes.o
+ text data bss dec hex filename
+ 955 0 0 955 3bb aes.o
+
+
+I am using the Free Software Foundation, ARM GCC compiler:
+
+ $ arm-none-eabi-gcc --version
+ arm-none-eabi-gcc (4.8.4-1+11-1) 4.8.4 20141219 (release)
+ Copyright (C) 2013 Free Software Foundation, Inc.
+ This is free software; see the source for copying conditions. There is NO
+ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+
+
+This implementation is verified against the data in:
+
+[National Institute of Standards and Technology Special Publication 800-38A 2001 ED](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf) Appendix F: Example Vectors for Modes of Operation of the AES.
+
+The other appendices in the document are valuable for implementation details on e.g. padding, generation of IVs and nonces in CTR-mode etc.
+
+
+A heartfelt thank-you to all the nice people out there who have contributed to this project.
+
+
+All material in this repository is in the public domain.
diff --git a/test/monniaux/tiny-AES-c/aes.c b/test/monniaux/tiny-AES-c/aes.c
new file mode 100644
index 00000000..f0f9ac90
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/aes.c
@@ -0,0 +1,571 @@
+/*
+
+This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode.
+Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
+
+The implementation is verified against the test vectors in:
+ National Institute of Standards and Technology Special Publication 800-38A 2001 ED
+
+ECB-AES128
+----------
+
+ plain-text:
+ 6bc1bee22e409f96e93d7e117393172a
+ ae2d8a571e03ac9c9eb76fac45af8e51
+ 30c81c46a35ce411e5fbc1191a0a52ef
+ f69f2445df4f9b17ad2b417be66c3710
+
+ key:
+ 2b7e151628aed2a6abf7158809cf4f3c
+
+ resulting cipher
+ 3ad77bb40d7a3660a89ecaf32466ef97
+ f5d3d58503b9699de785895a96fdbaaf
+ 43b1cd7f598ece23881b00e3ed030688
+ 7b0c785e27e8ad3f8223207104725dd4
+
+
+NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
+ You should pad the end of the string with zeros if this is not the case.
+ For AES192/256 the key size is proportionally larger.
+
+*/
+
+
+/*****************************************************************************/
+/* Includes: */
+/*****************************************************************************/
+#include <stdint.h>
+#include <string.h> // CBC mode, for memset
+#include "aes.h"
+
+/*****************************************************************************/
+/* Defines: */
+/*****************************************************************************/
+// The number of columns comprising a state in AES. This is a constant in AES. Value=4
+#define Nb 4
+
+#if defined(AES256) && (AES256 == 1)
+ #define Nk 8
+ #define Nr 14
+#elif defined(AES192) && (AES192 == 1)
+ #define Nk 6
+ #define Nr 12
+#else
+ #define Nk 4 // The number of 32 bit words in a key.
+ #define Nr 10 // The number of rounds in AES Cipher.
+#endif
+
+// jcallan@github points out that declaring Multiply as a function
+// reduces code size considerably with the Keil ARM compiler.
+// See this link for more information: https://github.com/kokke/tiny-AES-C/pull/3
+#ifndef MULTIPLY_AS_A_FUNCTION
+ #define MULTIPLY_AS_A_FUNCTION 0
+#endif
+
+
+
+
+/*****************************************************************************/
+/* Private variables: */
+/*****************************************************************************/
+// state - array holding the intermediate results during decryption.
+typedef uint8_t state_t[4][4];
+
+
+
+// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
+// The numbers below can be computed dynamically trading ROM for RAM -
+// This can be useful in (embedded) bootloader applications, where ROM is often limited.
+static const uint8_t sbox[256] = {
+ //0 1 2 3 4 5 6 7 8 9 A B C D E F
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
+ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
+ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
+ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
+ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
+ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
+ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
+
+static const uint8_t rsbox[256] = {
+ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
+ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
+ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
+ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
+ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
+ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
+ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
+ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
+ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
+ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
+ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
+ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
+ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
+ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
+ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
+ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
+
+// The round constant word array, Rcon[i], contains the values given by
+// x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
+static const uint8_t Rcon[11] = {
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
+
+/*
+ * Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES-C/pull/12),
+ * that you can remove most of the elements in the Rcon array, because they are unused.
+ *
+ * From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
+ *
+ * "Only the first some of these constants are actually used – up to rcon[10] for AES-128 (as 11 round keys are needed),
+ * up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
+ */
+
+
+/*****************************************************************************/
+/* Private functions: */
+/*****************************************************************************/
+/*
+static uint8_t getSBoxValue(uint8_t num)
+{
+ return sbox[num];
+}
+*/
+#define getSBoxValue(num) (sbox[(num)])
+/*
+static uint8_t getSBoxInvert(uint8_t num)
+{
+ return rsbox[num];
+}
+*/
+#define getSBoxInvert(num) (rsbox[(num)])
+
+// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
+static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
+{
+ unsigned i, j, k;
+ uint8_t tempa[4]; // Used for the column/row operations
+
+ // The first round key is the key itself.
+ for (i = 0; i < Nk; ++i)
+ {
+ RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
+ RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
+ RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
+ RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
+ }
+
+ // All other round keys are found from the previous round keys.
+ for (i = Nk; i < Nb * (Nr + 1); ++i)
+ {
+ {
+ k = (i - 1) * 4;
+ tempa[0]=RoundKey[k + 0];
+ tempa[1]=RoundKey[k + 1];
+ tempa[2]=RoundKey[k + 2];
+ tempa[3]=RoundKey[k + 3];
+
+ }
+
+ if (i % Nk == 0)
+ {
+ // This function shifts the 4 bytes in a word to the left once.
+ // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
+
+ // Function RotWord()
+ {
+ const uint8_t u8tmp = tempa[0];
+ tempa[0] = tempa[1];
+ tempa[1] = tempa[2];
+ tempa[2] = tempa[3];
+ tempa[3] = u8tmp;
+ }
+
+ // SubWord() is a function that takes a four-byte input word and
+ // applies the S-box to each of the four bytes to produce an output word.
+
+ // Function Subword()
+ {
+ tempa[0] = getSBoxValue(tempa[0]);
+ tempa[1] = getSBoxValue(tempa[1]);
+ tempa[2] = getSBoxValue(tempa[2]);
+ tempa[3] = getSBoxValue(tempa[3]);
+ }
+
+ tempa[0] = tempa[0] ^ Rcon[i/Nk];
+ }
+#if defined(AES256) && (AES256 == 1)
+ if (i % Nk == 4)
+ {
+ // Function Subword()
+ {
+ tempa[0] = getSBoxValue(tempa[0]);
+ tempa[1] = getSBoxValue(tempa[1]);
+ tempa[2] = getSBoxValue(tempa[2]);
+ tempa[3] = getSBoxValue(tempa[3]);
+ }
+ }
+#endif
+ j = i * 4; k=(i - Nk) * 4;
+ RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
+ RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
+ RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
+ RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
+ }
+}
+
+void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key)
+{
+ KeyExpansion(ctx->RoundKey, key);
+}
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv)
+{
+ KeyExpansion(ctx->RoundKey, key);
+ memcpy (ctx->Iv, iv, AES_BLOCKLEN);
+}
+void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv)
+{
+ memcpy (ctx->Iv, iv, AES_BLOCKLEN);
+}
+#endif
+
+// This function adds the round key to state.
+// The round key is added to the state by an XOR function.
+static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey)
+{
+ uint8_t i,j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
+ }
+ }
+}
+
+// The SubBytes Function Substitutes the values in the
+// state matrix with values in an S-box.
+static void SubBytes(state_t* state)
+{
+ uint8_t i, j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[j][i] = getSBoxValue((*state)[j][i]);
+ }
+ }
+}
+
+// The ShiftRows() function shifts the rows in the state to the left.
+// Each row is shifted with different offset.
+// Offset = Row number. So the first row is not shifted.
+static void ShiftRows(state_t* state)
+{
+ uint8_t temp;
+
+ // Rotate first row 1 columns to left
+ temp = (*state)[0][1];
+ (*state)[0][1] = (*state)[1][1];
+ (*state)[1][1] = (*state)[2][1];
+ (*state)[2][1] = (*state)[3][1];
+ (*state)[3][1] = temp;
+
+ // Rotate second row 2 columns to left
+ temp = (*state)[0][2];
+ (*state)[0][2] = (*state)[2][2];
+ (*state)[2][2] = temp;
+
+ temp = (*state)[1][2];
+ (*state)[1][2] = (*state)[3][2];
+ (*state)[3][2] = temp;
+
+ // Rotate third row 3 columns to left
+ temp = (*state)[0][3];
+ (*state)[0][3] = (*state)[3][3];
+ (*state)[3][3] = (*state)[2][3];
+ (*state)[2][3] = (*state)[1][3];
+ (*state)[1][3] = temp;
+}
+
+static uint8_t xtime(uint8_t x)
+{
+ return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
+}
+
+// MixColumns function mixes the columns of the state matrix
+static void MixColumns(state_t* state)
+{
+ uint8_t i;
+ uint8_t Tmp, Tm, t;
+ for (i = 0; i < 4; ++i)
+ {
+ t = (*state)[i][0];
+ Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
+ Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
+ }
+}
+
+// Multiply is used to multiply numbers in the field GF(2^8)
+// Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary
+// The compiler seems to be able to vectorize the operation better this way.
+// See https://github.com/kokke/tiny-AES-c/pull/34
+#if MULTIPLY_AS_A_FUNCTION
+static uint8_t Multiply(uint8_t x, uint8_t y)
+{
+ return (((y & 1) * x) ^
+ ((y>>1 & 1) * xtime(x)) ^
+ ((y>>2 & 1) * xtime(xtime(x))) ^
+ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
+ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */
+ }
+#else
+#define Multiply(x, y) \
+ ( ((y & 1) * x) ^ \
+ ((y>>1 & 1) * xtime(x)) ^ \
+ ((y>>2 & 1) * xtime(xtime(x))) ^ \
+ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
+ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
+
+#endif
+
+// MixColumns function mixes the columns of the state matrix.
+// The method used to multiply may be difficult to understand for the inexperienced.
+// Please use the references to gain more information.
+static void InvMixColumns(state_t* state)
+{
+ int i;
+ uint8_t a, b, c, d;
+ for (i = 0; i < 4; ++i)
+ {
+ a = (*state)[i][0];
+ b = (*state)[i][1];
+ c = (*state)[i][2];
+ d = (*state)[i][3];
+
+ (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
+ (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
+ (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
+ (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
+ }
+}
+
+
+// The SubBytes Function Substitutes the values in the
+// state matrix with values in an S-box.
+static void InvSubBytes(state_t* state)
+{
+ uint8_t i, j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[j][i] = getSBoxInvert((*state)[j][i]);
+ }
+ }
+}
+
+static void InvShiftRows(state_t* state)
+{
+ uint8_t temp;
+
+ // Rotate first row 1 columns to right
+ temp = (*state)[3][1];
+ (*state)[3][1] = (*state)[2][1];
+ (*state)[2][1] = (*state)[1][1];
+ (*state)[1][1] = (*state)[0][1];
+ (*state)[0][1] = temp;
+
+ // Rotate second row 2 columns to right
+ temp = (*state)[0][2];
+ (*state)[0][2] = (*state)[2][2];
+ (*state)[2][2] = temp;
+
+ temp = (*state)[1][2];
+ (*state)[1][2] = (*state)[3][2];
+ (*state)[3][2] = temp;
+
+ // Rotate third row 3 columns to right
+ temp = (*state)[0][3];
+ (*state)[0][3] = (*state)[1][3];
+ (*state)[1][3] = (*state)[2][3];
+ (*state)[2][3] = (*state)[3][3];
+ (*state)[3][3] = temp;
+}
+
+
+// Cipher is the main function that encrypts the PlainText.
+static void Cipher(state_t* state, uint8_t* RoundKey)
+{
+ uint8_t round = 0;
+
+ // Add the First round key to the state before starting the rounds.
+ AddRoundKey(0, state, RoundKey);
+
+ // There will be Nr rounds.
+ // The first Nr-1 rounds are identical.
+ // These Nr-1 rounds are executed in the loop below.
+ for (round = 1; round < Nr; ++round)
+ {
+ SubBytes(state);
+ ShiftRows(state);
+ MixColumns(state);
+ AddRoundKey(round, state, RoundKey);
+ }
+
+ // The last round is given below.
+ // The MixColumns function is not here in the last round.
+ SubBytes(state);
+ ShiftRows(state);
+ AddRoundKey(Nr, state, RoundKey);
+}
+
+#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+static void InvCipher(state_t* state,uint8_t* RoundKey)
+{
+ uint8_t round = 0;
+
+ // Add the First round key to the state before starting the rounds.
+ AddRoundKey(Nr, state, RoundKey);
+
+ // There will be Nr rounds.
+ // The first Nr-1 rounds are identical.
+ // These Nr-1 rounds are executed in the loop below.
+ for (round = (Nr - 1); round > 0; --round)
+ {
+ InvShiftRows(state);
+ InvSubBytes(state);
+ AddRoundKey(round, state, RoundKey);
+ InvMixColumns(state);
+ }
+
+ // The last round is given below.
+ // The MixColumns function is not here in the last round.
+ InvShiftRows(state);
+ InvSubBytes(state);
+ AddRoundKey(0, state, RoundKey);
+}
+#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+
+/*****************************************************************************/
+/* Public functions: */
+/*****************************************************************************/
+#if defined(ECB) && (ECB == 1)
+
+
+void AES_ECB_encrypt(struct AES_ctx *ctx, uint8_t* buf)
+{
+ // The next function call encrypts the PlainText with the Key using AES algorithm.
+ Cipher((state_t*)buf, ctx->RoundKey);
+}
+
+void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf)
+{
+ // The next function call decrypts the PlainText with the Key using AES algorithm.
+ InvCipher((state_t*)buf, ctx->RoundKey);
+}
+
+
+#endif // #if defined(ECB) && (ECB == 1)
+
+
+
+
+
+#if defined(CBC) && (CBC == 1)
+
+
+static void XorWithIv(uint8_t* buf, uint8_t* Iv)
+{
+ uint8_t i;
+ for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
+ {
+ buf[i] ^= Iv[i];
+ }
+}
+
+void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length)
+{
+ uintptr_t i;
+ uint8_t *Iv = ctx->Iv;
+ for (i = 0; i < length; i += AES_BLOCKLEN)
+ {
+ XorWithIv(buf, Iv);
+ Cipher((state_t*)buf, ctx->RoundKey);
+ Iv = buf;
+ buf += AES_BLOCKLEN;
+ //printf("Step %d - %d", i/16, i);
+ }
+ /* store Iv in ctx for next call */
+ memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
+}
+
+void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
+{
+ uintptr_t i;
+ uint8_t storeNextIv[AES_BLOCKLEN];
+ for (i = 0; i < length; i += AES_BLOCKLEN)
+ {
+ memcpy(storeNextIv, buf, AES_BLOCKLEN);
+ InvCipher((state_t*)buf, ctx->RoundKey);
+ XorWithIv(buf, ctx->Iv);
+ memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
+ buf += AES_BLOCKLEN;
+ }
+
+}
+
+#endif // #if defined(CBC) && (CBC == 1)
+
+
+
+#if defined(CTR) && (CTR == 1)
+
+/* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */
+void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
+{
+ uint8_t buffer[AES_BLOCKLEN];
+
+ unsigned i;
+ int bi;
+ for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
+ {
+ if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
+ {
+
+ memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
+ Cipher((state_t*)buffer,ctx->RoundKey);
+
+ /* Increment Iv and handle overflow */
+ for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
+ {
+ /* inc will owerflow */
+ if (ctx->Iv[bi] == 255)
+ {
+ ctx->Iv[bi] = 0;
+ continue;
+ }
+ ctx->Iv[bi] += 1;
+ break;
+ }
+ bi = 0;
+ }
+
+ buf[i] = (buf[i] ^ buffer[bi]);
+ }
+}
+
+#endif // #if defined(CTR) && (CTR == 1)
+
diff --git a/test/monniaux/tiny-AES-c/aes.h b/test/monniaux/tiny-AES-c/aes.h
new file mode 100644
index 00000000..1daab47b
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/aes.h
@@ -0,0 +1,90 @@
+#ifndef _AES_H_
+#define _AES_H_
+
+#include <stdint.h>
+
+// #define the macros below to 1/0 to enable/disable the mode of operation.
+//
+// CBC enables AES encryption in CBC-mode of operation.
+// CTR enables encryption in counter-mode.
+// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
+
+// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
+#ifndef CBC
+ #define CBC 1
+#endif
+
+#ifndef ECB
+ #define ECB 1
+#endif
+
+#ifndef CTR
+ #define CTR 1
+#endif
+
+
+#define AES128 1
+//#define AES192 1
+//#define AES256 1
+
+#define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only
+
+#if defined(AES256) && (AES256 == 1)
+ #define AES_KEYLEN 32
+ #define AES_keyExpSize 240
+#elif defined(AES192) && (AES192 == 1)
+ #define AES_KEYLEN 24
+ #define AES_keyExpSize 208
+#else
+ #define AES_KEYLEN 16 // Key length in bytes
+ #define AES_keyExpSize 176
+#endif
+
+struct AES_ctx
+{
+ uint8_t RoundKey[AES_keyExpSize];
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+ uint8_t Iv[AES_BLOCKLEN];
+#endif
+};
+
+void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
+void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
+#endif
+
+#if defined(ECB) && (ECB == 1)
+// buffer size is exactly AES_BLOCKLEN bytes;
+// you need only AES_init_ctx as IV is not used in ECB
+// NB: ECB is considered insecure for most uses
+void AES_ECB_encrypt(struct AES_ctx* ctx, uint8_t* buf);
+void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf);
+
+#endif // #if defined(ECB) && (ECB == !)
+
+
+#if defined(CBC) && (CBC == 1)
+// buffer size MUST be mutile of AES_BLOCKLEN;
+// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
+// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
+// no IV should ever be reused with the same key
+void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+
+#endif // #if defined(CBC) && (CBC == 1)
+
+
+#if defined(CTR) && (CTR == 1)
+
+// Same function for encrypting as for decrypting.
+// IV is incremented for every block, and used after encryption as XOR-compliment for output
+// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
+// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
+// no IV should ever be reused with the same key
+void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+
+#endif // #if defined(CTR) && (CTR == 1)
+
+
+#endif //_AES_H_
diff --git a/test/monniaux/tiny-AES-c/aes.hpp b/test/monniaux/tiny-AES-c/aes.hpp
new file mode 100644
index 00000000..ade16425
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/aes.hpp
@@ -0,0 +1,12 @@
+#ifndef _AES_HPP_
+#define _AES_HPP_
+
+#ifndef __cplusplus
+#error Do not include the hpp header in a c project!
+#endif //__cplusplus
+
+extern "C" {
+#include "aes.h"
+}
+
+#endif //_AES_HPP_
diff --git a/test/monniaux/tiny-AES-c/library.json b/test/monniaux/tiny-AES-c/library.json
new file mode 100644
index 00000000..d7abe89a
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/library.json
@@ -0,0 +1,13 @@
+{
+ "name": "tiny-AES-c",
+ "keywords": "cryptography, aes",
+ "description": "Small portable AES128/192/256 in C",
+ "repository":
+ {
+ "type": "git",
+ "url": "https://github.com/kokke/tiny-AES-c.git"
+ },
+ "frameworks": "*",
+ "platforms": "*",
+ "examples": "test.c"
+}
diff --git a/test/monniaux/tiny-AES-c/test.c b/test/monniaux/tiny-AES-c/test.c
new file mode 100644
index 00000000..67962831
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/test.c
@@ -0,0 +1,316 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+// Enable ECB, CTR and CBC mode. Note this can be done before including aes.h or at compile-time.
+// E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DCTR=1 -DECB=1
+#define CBC 1
+#define CTR 1
+#define ECB 1
+
+#include "aes.h"
+
+
+static void phex(uint8_t* str);
+static int test_encrypt_cbc(void);
+static int test_decrypt_cbc(void);
+static int test_encrypt_ctr(void);
+static int test_decrypt_ctr(void);
+static int test_encrypt_ecb(void);
+static int test_decrypt_ecb(void);
+static void test_encrypt_ecb_verbose(void);
+
+
+int main(void)
+{
+ int exit;
+
+#if defined(AES256)
+ printf("\nTesting AES256\n\n");
+#elif defined(AES192)
+ printf("\nTesting AES192\n\n");
+#elif defined(AES128)
+ printf("\nTesting AES128\n\n");
+#else
+ printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
+ return 0;
+#endif
+
+ exit = test_encrypt_cbc() + test_decrypt_cbc() +
+ test_encrypt_ctr() + test_decrypt_ctr() +
+ test_decrypt_ecb() + test_encrypt_ecb();
+ test_encrypt_ecb_verbose();
+
+ return exit;
+}
+
+
+// prints string as hex
+static void phex(uint8_t* str)
+{
+
+#if defined(AES256)
+ uint8_t len = 32;
+#elif defined(AES192)
+ uint8_t len = 24;
+#elif defined(AES128)
+ uint8_t len = 16;
+#endif
+
+ unsigned char i;
+ for (i = 0; i < len; ++i)
+ printf("%.2x", str[i]);
+ printf("\n");
+}
+
+static void test_encrypt_ecb_verbose(void)
+{
+ // Example of more verbose verification
+
+ uint8_t i;
+
+ // 128bit key
+ uint8_t key[16] = { (uint8_t) 0x2b, (uint8_t) 0x7e, (uint8_t) 0x15, (uint8_t) 0x16, (uint8_t) 0x28, (uint8_t) 0xae, (uint8_t) 0xd2, (uint8_t) 0xa6, (uint8_t) 0xab, (uint8_t) 0xf7, (uint8_t) 0x15, (uint8_t) 0x88, (uint8_t) 0x09, (uint8_t) 0xcf, (uint8_t) 0x4f, (uint8_t) 0x3c };
+ // 512bit text
+ uint8_t plain_text[64] = { (uint8_t) 0x6b, (uint8_t) 0xc1, (uint8_t) 0xbe, (uint8_t) 0xe2, (uint8_t) 0x2e, (uint8_t) 0x40, (uint8_t) 0x9f, (uint8_t) 0x96, (uint8_t) 0xe9, (uint8_t) 0x3d, (uint8_t) 0x7e, (uint8_t) 0x11, (uint8_t) 0x73, (uint8_t) 0x93, (uint8_t) 0x17, (uint8_t) 0x2a,
+ (uint8_t) 0xae, (uint8_t) 0x2d, (uint8_t) 0x8a, (uint8_t) 0x57, (uint8_t) 0x1e, (uint8_t) 0x03, (uint8_t) 0xac, (uint8_t) 0x9c, (uint8_t) 0x9e, (uint8_t) 0xb7, (uint8_t) 0x6f, (uint8_t) 0xac, (uint8_t) 0x45, (uint8_t) 0xaf, (uint8_t) 0x8e, (uint8_t) 0x51,
+ (uint8_t) 0x30, (uint8_t) 0xc8, (uint8_t) 0x1c, (uint8_t) 0x46, (uint8_t) 0xa3, (uint8_t) 0x5c, (uint8_t) 0xe4, (uint8_t) 0x11, (uint8_t) 0xe5, (uint8_t) 0xfb, (uint8_t) 0xc1, (uint8_t) 0x19, (uint8_t) 0x1a, (uint8_t) 0x0a, (uint8_t) 0x52, (uint8_t) 0xef,
+ (uint8_t) 0xf6, (uint8_t) 0x9f, (uint8_t) 0x24, (uint8_t) 0x45, (uint8_t) 0xdf, (uint8_t) 0x4f, (uint8_t) 0x9b, (uint8_t) 0x17, (uint8_t) 0xad, (uint8_t) 0x2b, (uint8_t) 0x41, (uint8_t) 0x7b, (uint8_t) 0xe6, (uint8_t) 0x6c, (uint8_t) 0x37, (uint8_t) 0x10 };
+
+ // print text to encrypt, key and IV
+ printf("ECB encrypt verbose:\n\n");
+ printf("plain text:\n");
+ for (i = (uint8_t) 0; i < (uint8_t) 4; ++i)
+ {
+ phex(plain_text + i * (uint8_t) 16);
+ }
+ printf("\n");
+
+ printf("key:\n");
+ phex(key);
+ printf("\n");
+
+ // print the resulting cipher as 4 x 16 byte strings
+ printf("ciphertext:\n");
+
+ struct AES_ctx ctx;
+ AES_init_ctx(&ctx, key);
+
+ for (i = 0; i < 4; ++i)
+ {
+ AES_ECB_encrypt(&ctx, plain_text + (i * 16));
+ phex(plain_text + (i * 16));
+ }
+ printf("\n");
+}
+
+
+static int test_encrypt_ecb(void)
+{
+#if defined(AES256)
+ uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
+ uint8_t out[] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
+#elif defined(AES192)
+ uint8_t key[] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
+ 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b };
+ uint8_t out[] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
+#elif defined(AES128)
+ uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ uint8_t out[] = { 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97 };
+#endif
+
+ uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
+ struct AES_ctx ctx;
+
+ AES_init_ctx(&ctx, key);
+ AES_ECB_encrypt(&ctx, in);
+
+ printf("ECB encrypt: ");
+
+ if (0 == memcmp((char*) out, (char*) in, 16)) {
+ printf("SUCCESS!\n");
+ return(0);
+ } else {
+ printf("FAILURE!\n");
+ return(1);
+ }
+}
+
+static int test_decrypt_cbc(void)
+{
+
+#if defined(AES256)
+ uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
+ uint8_t in[] = { 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
+ 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
+ 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
+ 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b };
+#elif defined(AES192)
+ uint8_t key[] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b };
+ uint8_t in[] = { 0x4f, 0x02, 0x1d, 0xb2, 0x43, 0xbc, 0x63, 0x3d, 0x71, 0x78, 0x18, 0x3a, 0x9f, 0xa0, 0x71, 0xe8,
+ 0xb4, 0xd9, 0xad, 0xa9, 0xad, 0x7d, 0xed, 0xf4, 0xe5, 0xe7, 0x38, 0x76, 0x3f, 0x69, 0x14, 0x5a,
+ 0x57, 0x1b, 0x24, 0x20, 0x12, 0xfb, 0x7a, 0xe0, 0x7f, 0xa9, 0xba, 0xac, 0x3d, 0xf1, 0x02, 0xe0,
+ 0x08, 0xb0, 0xe2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xd9, 0x20, 0xa9, 0xe6, 0x4f, 0x56, 0x15, 0xcd };
+#elif defined(AES128)
+ uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
+ 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
+ 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
+ 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
+#endif
+ uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
+ uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
+ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
+ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
+ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
+// uint8_t buffer[64];
+ struct AES_ctx ctx;
+
+ AES_init_ctx_iv(&ctx, key, iv);
+ AES_CBC_decrypt_buffer(&ctx, in, 64);
+
+ printf("CBC decrypt: ");
+
+ if (0 == memcmp((char*) out, (char*) in, 64)) {
+ printf("SUCCESS!\n");
+ return(0);
+ } else {
+ printf("FAILURE!\n");
+ return(1);
+ }
+}
+
+static int test_encrypt_cbc(void)
+{
+#if defined(AES256)
+ uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
+ uint8_t out[] = { 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
+ 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
+ 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
+ 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b };
+#elif defined(AES192)
+ uint8_t key[] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b };
+ uint8_t out[] = { 0x4f, 0x02, 0x1d, 0xb2, 0x43, 0xbc, 0x63, 0x3d, 0x71, 0x78, 0x18, 0x3a, 0x9f, 0xa0, 0x71, 0xe8,
+ 0xb4, 0xd9, 0xad, 0xa9, 0xad, 0x7d, 0xed, 0xf4, 0xe5, 0xe7, 0x38, 0x76, 0x3f, 0x69, 0x14, 0x5a,
+ 0x57, 0x1b, 0x24, 0x20, 0x12, 0xfb, 0x7a, 0xe0, 0x7f, 0xa9, 0xba, 0xac, 0x3d, 0xf1, 0x02, 0xe0,
+ 0x08, 0xb0, 0xe2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xd9, 0x20, 0xa9, 0xe6, 0x4f, 0x56, 0x15, 0xcd };
+#elif defined(AES128)
+ uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
+ 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
+ 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
+ 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
+#endif
+ uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
+ uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
+ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
+ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
+ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
+ struct AES_ctx ctx;
+
+ AES_init_ctx_iv(&ctx, key, iv);
+ AES_CBC_encrypt_buffer(&ctx, in, 64);
+
+ printf("CBC encrypt: ");
+
+ if (0 == memcmp((char*) out, (char*) in, 64)) {
+ printf("SUCCESS!\n");
+ return(0);
+ } else {
+ printf("FAILURE!\n");
+ return(1);
+ }
+}
+
+static int test_xcrypt_ctr(const char* xcrypt);
+static int test_encrypt_ctr(void)
+{
+ return test_xcrypt_ctr("encrypt");
+}
+
+static int test_decrypt_ctr(void)
+{
+ return test_xcrypt_ctr("decrypt");
+}
+
+static int test_xcrypt_ctr(const char* xcrypt)
+{
+#if defined(AES256)
+ uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
+ uint8_t in[64] = { 0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5, 0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28,
+ 0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a, 0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5,
+ 0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c, 0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d,
+ 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6, 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6 };
+#elif defined(AES192)
+ uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
+ 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b };
+ uint8_t in[64] = { 0x1a, 0xbc, 0x93, 0x24, 0x17, 0x52, 0x1c, 0xa2, 0x4f, 0x2b, 0x04, 0x59, 0xfe, 0x7e, 0x6e, 0x0b,
+ 0x09, 0x03, 0x39, 0xec, 0x0a, 0xa6, 0xfa, 0xef, 0xd5, 0xcc, 0xc2, 0xc6, 0xf4, 0xce, 0x8e, 0x94,
+ 0x1e, 0x36, 0xb2, 0x6b, 0xd1, 0xeb, 0xc6, 0x70, 0xd1, 0xbd, 0x1d, 0x66, 0x56, 0x20, 0xab, 0xf7,
+ 0x4f, 0x78, 0xa7, 0xf6, 0xd2, 0x98, 0x09, 0x58, 0x5a, 0x97, 0xda, 0xec, 0x58, 0xc6, 0xb0, 0x50 };
+#elif defined(AES128)
+ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ uint8_t in[64] = { 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
+ 0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
+ 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab,
+ 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee };
+#endif
+ uint8_t iv[16] = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
+ uint8_t out[64] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
+ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
+ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
+ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
+ struct AES_ctx ctx;
+
+ AES_init_ctx_iv(&ctx, key, iv);
+ AES_CTR_xcrypt_buffer(&ctx, in, 64);
+
+ printf("CTR %s: ", xcrypt);
+
+ if (0 == memcmp((char *) out, (char *) in, 64)) {
+ printf("SUCCESS!\n");
+ return(0);
+ } else {
+ printf("FAILURE!\n");
+ return(1);
+ }
+}
+
+
+static int test_decrypt_ecb(void)
+{
+#if defined(AES256)
+ uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
+ uint8_t in[] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
+#elif defined(AES192)
+ uint8_t key[] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
+ 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b };
+ uint8_t in[] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
+#elif defined(AES128)
+ uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ uint8_t in[] = { 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97 };
+#endif
+
+ uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
+ struct AES_ctx ctx;
+
+ AES_init_ctx(&ctx, key);
+ AES_ECB_decrypt(&ctx, in);
+
+ printf("ECB decrypt: ");
+
+ if (0 == memcmp((char*) out, (char*) in, 16)) {
+ printf("SUCCESS!\n");
+ return(0);
+ } else {
+ printf("FAILURE!\n");
+ return(1);
+ }
+}
+
+
diff --git a/test/monniaux/tiny-AES-c/unlicense.txt b/test/monniaux/tiny-AES-c/unlicense.txt
new file mode 100644
index 00000000..68a49daa
--- /dev/null
+++ b/test/monniaux/tiny-AES-c/unlicense.txt
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
diff --git a/test/monniaux/too_slow/Makefile b/test/monniaux/too_slow/Makefile
new file mode 100644
index 00000000..a1466d1d
--- /dev/null
+++ b/test/monniaux/too_slow/Makefile
@@ -0,0 +1,3 @@
+TARGET=memset_from_bitslices-aes
+
+include ../rules.mk
diff --git a/test/monniaux/too_slow/memset_from_bitsliced-aes.c b/test/monniaux/too_slow/memset_from_bitsliced-aes.c
new file mode 100644
index 00000000..32137b55
--- /dev/null
+++ b/test/monniaux/too_slow/memset_from_bitsliced-aes.c
@@ -0,0 +1,43 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include "../clock.h"
+
+typedef uint64_t a;
+a n[128];
+int o, bs_expand_key_k;
+void b(a (*)[], uint8_t *);
+void c(uint8_t d, uint8_t e, size_t f, uint8_t g, uint8_t iv) {
+ a i[1];
+ b(i, g);
+}
+
+void b(a (*i)[], uint8_t *j) {
+ for (; o < 176; o += 8) {
+ bs_expand_key_k = 4;
+ for (; bs_expand_key_k < 128; bs_expand_key_k += 128 / 64)
+ ;
+ memset(n, 0, sizeof(n));
+ }
+}
+
+void aes_ctr_test() {
+ uint8_t k = "";
+ uint8_t l = "";
+ uint8_t m = "";
+ uint8_t output[4];
+ c(output, m, 4, k, l);
+}
+
+int main(int argc, char * argv[])
+{
+ clock_prepare();
+
+ clock_start();
+
+ aes_ctr_test();
+ clock_stop();
+ print_total_clock();
+
+ return 0;
+}
diff --git a/test/monniaux/uzlib/LICENSE b/test/monniaux/uzlib/LICENSE
new file mode 100644
index 00000000..c9f29a05
--- /dev/null
+++ b/test/monniaux/uzlib/LICENSE
@@ -0,0 +1,65 @@
+Introduction
+------------
+
+uzlib as a whole is licensed under the terms of Zlib license, listed in
+the next section. It consists of substantial works of individuals whose
+copyrights listed in the next session. Some portions of the uzlib codebase
+originally were licensed under different license(s), however compatible
+with the Zlib license. Such license(s) are listed at the end of this file.
+
+License
+-------
+
+uzlib - Deflate/Zlib-compatible LZ77 compression/decompression library
+
+Copyright (c) 2003 Joergen Ibsen
+Copyright (c) 1997-2014 Simon Tatham
+Copyright (c) 2014-2018 Paul Sokolovsky
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must
+ not claim that you wrote the original software. If you use this
+ software in a product, an acknowledgment in the product
+ documentation would be appreciated but is not required.
+
+2. Altered source versions must be plainly marked as such, and must
+ not be misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any source
+ distribution.
+
+-------
+Original license for src/defl_static.c:
+
+PuTTY is copyright 1997-2014 Simon Tatham.
+
+Portions copyright Robert de Bath, Joris van Rantwijk, Delian
+Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
+Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
+Kuhn, Colin Watson, and CORE SDI S.A.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation files
+(the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/test/monniaux/uzlib/Makefile b/test/monniaux/uzlib/Makefile
new file mode 100644
index 00000000..b5593199
--- /dev/null
+++ b/test/monniaux/uzlib/Makefile
@@ -0,0 +1,4 @@
+all clean:
+ $(MAKE) -C src $@
+ $(MAKE) -C examples/tgzip $@
+ $(MAKE) -C examples/tgunzip $@
diff --git a/test/monniaux/uzlib/README.md b/test/monniaux/uzlib/README.md
new file mode 100644
index 00000000..55768424
--- /dev/null
+++ b/test/monniaux/uzlib/README.md
@@ -0,0 +1,310 @@
+uzlib - Deflate/Zlib-compatible LZ77 compression/decompression library
+======================================================================
+
+uzlib is a library which can decompress any valid Deflate, Zlib, and Gzip
+(further called just "Deflate") bitstream, and compress data to Deflate-
+compatible bitstream, albeit with lower compression ratio than Zlib Deflate
+algorithm (very basic LZ77 compression algorithm is used instead, static
+Deflate Huffman tree encoding is used for bitstream).
+
+uzlib aims for minimal code size and runtime memory requirements, and thus
+suitable for (deeply) embedded systems.
+
+uzlib is based on:
+
+* tinf library by Joergen Ibsen (Deflate decompression)
+* Deflate Static Huffman tree routines by Simon Tatham
+* LZ77 compressor by Paul Sokolovsky
+
+Library integrated and maintained by Paul Sokolovsky.
+
+(c) 2014-2018 Paul Sokolovsky
+
+uzlib library is licensed under Zlib license.
+
+
+Decompressor features
+---------------------
+
+Handling of input (compressed) stream:
+
+* Can reside (fully) in memory.
+* Can be received, byte by byte, from an application-defined callback
+ function (which e.g. can read it from file or another I/O device).
+* Combination of the above: a chunk of input is buffered in memory,
+ when buffer is exhausted, the application callback is called to refill
+ it.
+
+Handling of output (decompressed) stream:
+
+* In-memory decompression, where output stream fully resides in memory.
+* Streaming decompression, which allows to process arbitrary-sized streams
+ (longer than available memory), but requires in-memory buffer for Deflate
+ dictionary window.
+* Application specifies number of output bytes it wants to decompress,
+ which can be as high as UINT_MAX to decompress everything into memory
+ at once, or as low as 1 to decompress byte by byte, or any other value
+ to decompress a chunk of that size.
+
+Note that in regard to input stream handling, uzlib employs callback-based,
+"pull-style" design. The control flow looks as follows:
+
+1. Application requests uzlib to decompress given number of bytes.
+2. uzlib performs decompression.
+3. If more input is needed to decompress given number of bytes, uzlib
+ calls back into application to provide more input bytes. (An
+ implication of this is that uzlib will always return given number of
+ output bytes, unless end of stream (or error) happens).
+
+The original Zlib library instead features "push-style" design:
+
+1. An application prepares arbitrary number of input bytes in a buffer,
+ and free space in output buffer, and calls Zlib with these buffers.
+2. Zlib tries to decode as much as possible input and produce as much
+ as possible output. It returns back to the application if input
+ buffer is exhausted, or output buffer is full, whatever happens
+ first.
+
+Currently, uzlib doesn't support push-style operation a-la Zlib.
+
+Compressor features
+-------------------
+
+Compressor uses very basic implementation of LZ77 algorithm using hash
+table to find repeating substrings. The size of the hash table (on which
+compression efficiency depends) is currently hardcoded at the compile-time.
+Likewise, the size of LZ77 dictionary is also hardcoded at compile time.
+Both settings should be made runtime-configurable. The hash table is
+allocated on the stack, instead it should be allocated by user and passed
+as an argument to the function (dependency injection pattern).
+
+Currently, compressor doesn't support streaming operation, both input and
+output must reside in memory. Neither it supports incremental operation,
+entire input buffer is compressed at once with a single call to uzlib.
+
+Binary sizes
+------------
+
+To give an impression of code/data sizes of uzlib, the following figures
+are provided. Numbers for *.o files are code sizes of individual
+components (tinflate.o is decompressor, genlz77.o and defl_static.o -
+compressor), and TINF_DATA is the size of the corresponding data
+structure. These numbers are provided for different architectures,
+with default uzlib configuration, and with compilers/their options
+as specified.
+
+```
+gcc -m32 -Os
+gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
+2881 src/tinflate.o
+381 src/genlz77.o
+1891 src/defl_static.o
+1284 TINF_DATA
+
+arm-none-eabi-gcc -mthumb -mcpu=cortex-m4 -Os
+arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
+1620 src/tinflate.o
+188 src/genlz77.o
+1309 src/defl_static.o
+1284 TINF_DATA
+```
+
+---
+
+Original tinf library README
+============================
+
+For reference, the original "tinf" library README follows. NOTE: Some
+parts may no longer apply to uzlib.
+
+tinf - tiny inflate library
+===========================
+
+Version 1.00
+
+Copyright (c) 2003 Joergen Ibsen
+
+<http://www.ibsensoftware.com/>
+
+
+About
+-----
+
+tinf is a small library implementing the decompression algorithm for the
+deflate compressed data format (called 'inflate'). Deflate compression is
+used in e.g. zlib, gzip, zip and png.
+
+I wrote it because I needed a small in-memory zlib decompressor for a self-
+extracting archive, and the zlib library added 15k to my program. The tinf
+code added only 2k.
+
+Naturally the size difference is insignificant in most cases. Also, the
+zlib library has many more features, is more secure, and mostly faster.
+But if you have a project that calls for a small and simple deflate
+decompressor, give it a try :-)
+
+While the implementation should be fairly compliant, it does assume it is
+given valid compressed data, and that there is sufficient space for the
+decompressed data.
+
+Simple wrappers for decompressing zlib streams and gzip'ed data in memory
+are supplied.
+
+tgunzip, an example command-line gzip decompressor in C, is included.
+
+The inflate algorithm and data format are from 'DEFLATE Compressed Data
+Format Specification version 1.3' ([RFC 1951][1]).
+
+The zlib data format is from 'ZLIB Compressed Data Format Specification
+version 3.3' ([RFC 1950][2]).
+
+The gzip data format is from 'GZIP file format specification version 4.3'
+([RFC 1952][3]).
+
+Ideas for future versions:
+
+- the fixed Huffman trees could be built by `tinf_decode_trees()`
+ using a small table
+- memory for the `TINF_DATA` object should be passed, to avoid using
+ more than 1k of stack space
+- wrappers for unpacking zip archives and png images
+- implement more in x86 assembler
+- more sanity checks
+- in `tinf_uncompress`, the (entry value of) `destLen` and `sourceLen`
+ are not used
+- blocking of some sort, so everything does not have to be in memory
+- optional table-based huffman decoder
+
+[1]: http://www.rfc-editor.org/rfc/rfc1951.txt
+[2]: http://www.rfc-editor.org/rfc/rfc1950.txt
+[3]: http://www.rfc-editor.org/rfc/rfc1952.txt
+
+
+Functionality
+-------------
+
+ void tinf_init();
+
+Initialise the global uninitialised data used by the decompression code.
+This function must be called once before any calls to the decompression
+functions.
+
+ int tinf_uncompress(void *dest,
+ unsigned int *destLen,
+ const void *source,
+ unsigned int sourceLen);
+
+Decompress data in deflate compressed format from `source[]` to `dest[]`.
+`destLen` is set to the length of the decompressed data. Returns `TINF_OK`
+on success, and `TINF_DATA_ERROR` on error.
+
+ int tinf_gzip_uncompress(void *dest,
+ unsigned int *destLen,
+ const void *source,
+ unsigned int sourceLen);
+
+Decompress data in gzip compressed format from `source[]` to `dest[]`.
+`destLen` is set to the length of the decompressed data. Returns `TINF_OK`
+on success, and `TINF_DATA_ERROR` on error.
+
+ int tinf_zlib_uncompress(void *dest,
+ unsigned int *destLen,
+ const void *source,
+ unsigned int sourceLen);
+
+Decompress data in zlib compressed format from `source[]` to `dest[]`.
+`destLen` is set to the length of the decompressed data. Returns `TINF_OK`
+on success, and `TINF_DATA_ERROR` on error.
+
+ unsigned int tinf_adler32(const void *data,
+ unsigned int length);
+
+Computes the Adler-32 checksum of `length` bytes starting at `data`. Used by
+`tinf_zlib_uncompress()`.
+
+ unsigned int tinf_crc32(const void *data,
+ unsigned int length);
+
+Computes the CRC32 checksum of `length` bytes starting at `data`. Used by
+`tinf_gzip_uncompress()`.
+
+
+Source Code
+-----------
+
+The source code is ANSI C, and assumes that int is 32-bit. It has been
+tested on the x86 platform under Windows and Linux.
+
+The decompression functions should be endian-neutral, and also reentrant
+and thread-safe (not tested).
+
+In src/nasm there are 32-bit x86 assembler (386+) versions of some of the
+files.
+
+Makefiles (GNU Make style) for a number of compilers are included.
+
+
+Frequently Asked Questions
+--------------------------
+
+Q: Is it really free? Can I use it in my commercial ExpenZip software?
+
+A: It's open-source software, available under the zlib license (see
+ later), which means you can use it for free -- even in commercial
+ products. If you do, please be kind enough to add an acknowledgement.
+
+Q: Did you just strip stuff from the zlib source to make it smaller?
+
+A: No, tinf was written from scratch, using the RFC documentation of
+ the formats it supports.
+
+Q: What do you mean by: 'the zlib library .. is more secure'?
+
+A: The zlib decompression code checks the compressed data for validity
+ while decompressing, so even on corrupted data it will not crash.
+ The tinf code assumes it is given valid compressed data.
+
+Q: I'm a Delphi programmer, can I use tinf?
+
+A: Sure, the object files produced by both Borland C and Watcom C should
+ be linkable with Delphi.
+
+Q: Will tinf work on UltraSTRANGE machines running WhackOS?
+
+A: I have no idea .. please try it out and let me know!
+
+Q: Why are all the makefiles in GNU Make style?
+
+A: I'm used to GNU Make, and it has a number of features that are missing
+ in some of the other Make utilities.
+
+Q: This is the first release, how can there be frequently asked questions?
+
+A: Ok, ok .. I made the questions up ;-)
+
+
+License
+-------
+
+tinf - tiny inflate library
+
+Copyright (c) 2003 Joergen Ibsen
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must
+ not claim that you wrote the original software. If you use this
+ software in a product, an acknowledgment in the product
+ documentation would be appreciated but is not required.
+
+2. Altered source versions must be plainly marked as such, and must
+ not be misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any source
+ distribution.
diff --git a/test/monniaux/uzlib/compile.sh b/test/monniaux/uzlib/compile.sh
new file mode 100755
index 00000000..4b6b2563
--- /dev/null
+++ b/test/monniaux/uzlib/compile.sh
@@ -0,0 +1 @@
+make CC=/home/monniaux/work/Kalray/CompCert/ccomp COPT="-Wall -Wno-c11-extensions -fno-unprototyped -O3"
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile b/test/monniaux/uzlib/examples/tgunzip/makefile
new file mode 100644
index 00000000..bcc90815
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile
@@ -0,0 +1,31 @@
+##
+## tgunzip - gzip decompressor example
+##
+## GCC makefile (Linux, FreeBSD, BeOS and QNX)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgunzip
+objects = tgunzip.o
+libs = ../../lib/libtinf.a
+
+COPT = -Os
+CFLAGS = -Wall -I../../src $(COPT)
+LDFLAGS = $(CFLAGS)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects) $(libs)
+ $(CC) $(LDFLAGS) -o $@ $^ $(libs)
+
+%.o : %.c
+ $(CC) $(CFLAGS) -c $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.b32 b/test/monniaux/uzlib/examples/tgunzip/makefile.b32
new file mode 100644
index 00000000..7b559c9a
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.b32
@@ -0,0 +1,31 @@
+##
+## tgunzip - gzip decompressor example
+##
+## Borland C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgunzip.exe
+objects = tgunzip.obj
+libs = ..\..\lib\tinf.lib
+temps = tgunzip.tds
+
+cflags = -a16 -K -O2 -OS -I..\..\src
+ldflags = -C -q -Gn -x -Gz -ap -Tpe
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ ilink32 $(ldflags) c0x32 $(objects), $@ , , import32 cw32 $(libs) , ,
+
+%.obj : %.c
+ bcc32 $(cflags) -c $<
+
+clean:
+ $(RM) $(objects) $(target) $(temps)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.dj2 b/test/monniaux/uzlib/examples/tgunzip/makefile.dj2
new file mode 100644
index 00000000..c09312dc
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.dj2
@@ -0,0 +1,30 @@
+##
+## tgunzip - gzip decompressor example
+##
+## DJGPP makefile
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgunzip.exe
+objects = tgunzip.o
+libs = ../../lib/libtinf.a
+
+cflags = -s -Wall -Os -fomit-frame-pointer -I../../src
+ldflags = $(cflags)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ gcc $(ldflags) -o $@ $^ $(libs)
+
+%.o : %.c
+ gcc $(cflags) -o $@ -c $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.dmc b/test/monniaux/uzlib/examples/tgunzip/makefile.dmc
new file mode 100644
index 00000000..4c3d0297
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.dmc
@@ -0,0 +1,31 @@
+##
+## tgunzip - gzip decompressor example
+##
+## Digital Mars C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgunzip.exe
+objects = tgunzip.obj
+libs = ..\..\lib\tinf.lib
+temps = tgunzip.map
+
+cflags = -s -mn -o+all -I..\..\src
+ldflags = $(cflags)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ dmc $(ldflags) -o$@ $^ $(libs)
+
+%.obj : %.c
+ dmc $(cflags) -c $<
+
+clean:
+ $(RM) $(objects) $(target) $(temps)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.mgw b/test/monniaux/uzlib/examples/tgunzip/makefile.mgw
new file mode 100644
index 00000000..b48b4c10
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.mgw
@@ -0,0 +1,28 @@
+##
+## tgunzip - gzip decompressor example
+##
+## MinGW / Cygwin makefile
+##
+## Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+
+target = tgunzip.exe
+objects = tgunzip.o
+libs = ../../lib/libtinf.a
+
+cflags = -s -Wall -Os -fomit-frame-pointer -I../../src
+ldflags = $(cflags)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ gcc $(ldflags) -o $@ $^ $(libs)
+
+%.o : %.c
+ gcc $(cflags) -o $@ -c $<
+
+clean:
+ $(RM) $(target) $(objects)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.vc b/test/monniaux/uzlib/examples/tgunzip/makefile.vc
new file mode 100644
index 00000000..64dd2e6a
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.vc
@@ -0,0 +1,28 @@
+##
+## tgunzip - gzip decompressor example
+##
+## Visual C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+
+target = tgunzip.exe
+objects = tgunzip.obj
+libs = ../../lib/tinf.lib
+
+cflags = /nologo /W3 /O1 /G6 /W3 /Gy /GF /I..\..\src
+ldflags = /nologo /release /opt:ref /opt:icf
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ link $(ldflags) /out:$@ $^ $(libs)
+
+%.obj : %.c
+ cl $(cflags) -c $<
+
+clean:
+ $(RM) $(target) $(objects)
diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.wat b/test/monniaux/uzlib/examples/tgunzip/makefile.wat
new file mode 100644
index 00000000..2d27a985
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/makefile.wat
@@ -0,0 +1,31 @@
+##
+## tgunzip - gzip decompressor example
+##
+## Watcom / OpenWatcom C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgunzip.exe
+objects = tgunzip.obj
+libs = ../../lib/tinf.lib
+system = nt
+
+cflags = -bt=$(system) -d0 -ox -I..\..\src
+ldflags = system $(system)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ wlink $(ldflags) name $@ file {$^} library {$(libs)}
+
+%.obj : %.c
+ wcc386 $(cflags) $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/examples/tgunzip/tgunzip.c b/test/monniaux/uzlib/examples/tgunzip/tgunzip.c
new file mode 100644
index 00000000..dcf15eca
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgunzip/tgunzip.c
@@ -0,0 +1,167 @@
+/*
+ * tgunzip - gzip decompressor example
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * Copyright (c) 2014-2016 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "uzlib.h"
+
+/* produce decompressed output in chunks of this size */
+/* defauly is to decompress byte by byte; can be any other length */
+#define OUT_CHUNK_SIZE 1
+
+void exit_error(const char *what)
+{
+ printf("ERROR: %s\n", what);
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ FILE *fin, *fout;
+ unsigned int len, dlen, outlen;
+ const unsigned char *source;
+ unsigned char *dest;
+ int res;
+
+ printf("tgunzip - example from the tiny inflate library (www.ibsensoftware.com)\n\n");
+
+ if (argc < 3)
+ {
+ printf(
+ "Syntax: tgunzip <source> <destination>\n\n"
+ "Both input and output are kept in memory, so do not use this on huge files.\n");
+
+ return 1;
+ }
+
+ uzlib_init();
+
+ /* -- open files -- */
+
+ if ((fin = fopen(argv[1], "rb")) == NULL) exit_error("source file");
+
+ if ((fout = fopen(argv[2], "wb")) == NULL) exit_error("destination file");
+
+ /* -- read source -- */
+
+ fseek(fin, 0, SEEK_END);
+
+ len = ftell(fin);
+
+ fseek(fin, 0, SEEK_SET);
+
+ source = (unsigned char *)malloc(len);
+
+ if (source == NULL) exit_error("memory");
+
+ if (fread((unsigned char*)source, 1, len, fin) != len) exit_error("read");
+
+ fclose(fin);
+
+ if (len < 4) exit_error("file too small");
+
+ /* -- get decompressed length -- */
+
+ dlen = source[len - 1];
+ dlen = 256*dlen + source[len - 2];
+ dlen = 256*dlen + source[len - 3];
+ dlen = 256*dlen + source[len - 4];
+
+ outlen = dlen;
+
+ /* there can be mismatch between length in the trailer and actual
+ data stream; to avoid buffer overruns on overlong streams, reserve
+ one extra byte */
+ dlen++;
+
+ dest = (unsigned char *)malloc(dlen);
+
+ if (dest == NULL) exit_error("memory");
+
+ /* -- decompress data -- */
+
+ struct uzlib_uncomp d;
+// uzlib_uncompress_init(&d, malloc(32768), 32768);
+ uzlib_uncompress_init(&d, NULL, 0);
+
+ /* all 3 fields below must be initialized by user */
+ d.source = source;
+ d.source_limit = source + len - 4;
+ d.source_read_cb = NULL;
+
+ res = uzlib_gzip_parse_header(&d);
+ if (res != TINF_OK) {
+ printf("Error parsing header: %d\n", res);
+ exit(1);
+ }
+
+ d.dest_start = d.dest = dest;
+
+ while (dlen) {
+ unsigned int chunk_len = dlen < OUT_CHUNK_SIZE ? dlen : OUT_CHUNK_SIZE;
+ d.dest_limit = d.dest + chunk_len;
+ res = uzlib_uncompress_chksum(&d);
+ dlen -= chunk_len;
+ if (res != TINF_OK) {
+ break;
+ }
+ }
+
+ if (res != TINF_DONE) {
+ printf("Error during decompression: %d\n", res);
+ exit(-res);
+ }
+
+ printf("decompressed %lu bytes\n", d.dest - dest);
+
+#if 0
+ if (d.dest - dest != gz.dlen) {
+ printf("Invalid decompressed length: %lu vs %u\n", d.dest - dest, gz.dlen);
+ }
+
+ if (tinf_crc32(dest, gz.dlen) != gz.crc32) {
+ printf("Invalid decompressed crc32\n");
+ }
+#endif
+
+ /* -- write output -- */
+
+ fwrite(dest, 1, outlen, fout);
+
+ fclose(fout);
+
+ return 0;
+}
diff --git a/test/monniaux/uzlib/examples/tgzip/makefile b/test/monniaux/uzlib/examples/tgzip/makefile
new file mode 100644
index 00000000..fa064157
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgzip/makefile
@@ -0,0 +1,31 @@
+##
+## tgzip - gzip compressor example
+##
+## GCC makefile (Linux, FreeBSD, BeOS and QNX)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## Copyright (c) 2014 by Paul Sokolovsky
+##
+## http://www.ibsensoftware.com/
+##
+
+target = tgzip
+objects = tgzip.o
+libs = ../../lib/libtinf.a
+
+COPT = -Os
+CFLAGS = -Wall -I../../src $(COPT)
+LDFLAGS = $(CFLAGS)
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects) $(libs)
+ $(CC) $(LDFLAGS) -o $@ $^ $(libs)
+
+%.o : %.c
+ $(CC) $(CFLAGS) -c $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/examples/tgzip/tgzip.c b/test/monniaux/uzlib/examples/tgzip/tgzip.c
new file mode 100644
index 00000000..505169cb
--- /dev/null
+++ b/test/monniaux/uzlib/examples/tgzip/tgzip.c
@@ -0,0 +1,120 @@
+/*
+ * tgzip - gzip compressor example
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "uzlib.h"
+
+void exit_error(const char *what)
+{
+ printf("ERROR: %s\n", what);
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ FILE *fin, *fout;
+ unsigned int len;
+ unsigned char *source;
+
+ printf("tgzip - example from the uzlib library\n\n");
+
+ if (argc < 3)
+ {
+ printf(
+ "Syntax: tgunzip <source> <destination>\n\n"
+ "Both input and output are kept in memory, so do not use this on huge files.\n");
+
+ return 1;
+ }
+
+ /* -- open files -- */
+
+ if ((fin = fopen(argv[1], "rb")) == NULL) exit_error("source file");
+
+ if ((fout = fopen(argv[2], "wb")) == NULL) exit_error("destination file");
+
+ /* -- read source -- */
+
+ fseek(fin, 0, SEEK_END);
+
+ len = ftell(fin);
+
+ fseek(fin, 0, SEEK_SET);
+
+ source = (unsigned char *)malloc(len);
+
+ if (source == NULL) exit_error("memory");
+
+ if (fread(source, 1, len, fin) != len) exit_error("read");
+
+ fclose(fin);
+
+ /* -- compress data -- */
+
+ struct uzlib_comp comp = {0};
+ comp.dict_size = 32768;
+ comp.hash_bits = 12;
+ size_t hash_size = sizeof(uzlib_hash_entry_t) * (1 << comp.hash_bits);
+ comp.hash_table = malloc(hash_size);
+ memset(comp.hash_table, 0, hash_size);
+
+ zlib_start_block(&comp.out);
+ uzlib_compress(&comp, source, len);
+ zlib_finish_block(&comp.out);
+
+ printf("compressed to %u raw bytes\n", comp.out.outlen);
+
+ /* -- write output -- */
+
+ putc(0x1f, fout);
+ putc(0x8b, fout);
+ putc(0x08, fout);
+ putc(0x00, fout); // FLG
+ int mtime = 0;
+ fwrite(&mtime, sizeof(mtime), 1, fout);
+ putc(0x04, fout); // XFL
+ putc(0x03, fout); // OS
+
+ fwrite(comp.out.outbuf, 1, comp.out.outlen, fout);
+
+ unsigned crc = ~uzlib_crc32(source, len, ~0);
+ fwrite(&crc, sizeof(crc), 1, fout);
+ fwrite(&len, sizeof(len), 1, fout);
+
+ fclose(fout);
+
+ return 0;
+}
diff --git a/test/monniaux/uzlib/lib/empty.dir b/test/monniaux/uzlib/lib/empty.dir
new file mode 100644
index 00000000..70062289
--- /dev/null
+++ b/test/monniaux/uzlib/lib/empty.dir
@@ -0,0 +1 @@
+--- empty dir --- \ No newline at end of file
diff --git a/test/monniaux/uzlib/src/adler32.c b/test/monniaux/uzlib/src/adler32.c
new file mode 100644
index 00000000..1f175949
--- /dev/null
+++ b/test/monniaux/uzlib/src/adler32.c
@@ -0,0 +1,78 @@
+/*
+ * Adler-32 checksum
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+/*
+ * Adler-32 algorithm taken from the zlib source, which is
+ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
+ */
+
+#include "tinf.h"
+
+#define A32_BASE 65521
+#define A32_NMAX 5552
+
+uint32_t uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum /* 1 */)
+{
+ const unsigned char *buf = (const unsigned char *)data;
+
+ unsigned int s1 = prev_sum & 0xffff;
+ unsigned int s2 = prev_sum >> 16;
+
+ while (length > 0)
+ {
+ int k = length < A32_NMAX ? length : A32_NMAX;
+ int i;
+
+ for (i = k / 16; i; --i, buf += 16)
+ {
+ s1 += buf[0]; s2 += s1; s1 += buf[1]; s2 += s1;
+ s1 += buf[2]; s2 += s1; s1 += buf[3]; s2 += s1;
+ s1 += buf[4]; s2 += s1; s1 += buf[5]; s2 += s1;
+ s1 += buf[6]; s2 += s1; s1 += buf[7]; s2 += s1;
+
+ s1 += buf[8]; s2 += s1; s1 += buf[9]; s2 += s1;
+ s1 += buf[10]; s2 += s1; s1 += buf[11]; s2 += s1;
+ s1 += buf[12]; s2 += s1; s1 += buf[13]; s2 += s1;
+ s1 += buf[14]; s2 += s1; s1 += buf[15]; s2 += s1;
+ }
+
+ for (i = k % 16; i; --i) { s1 += *buf++; s2 += s1; }
+
+ s1 %= A32_BASE;
+ s2 %= A32_BASE;
+
+ length -= k;
+ }
+
+ return (s2 << 16) | s1;
+}
diff --git a/test/monniaux/uzlib/src/crc32.c b/test/monniaux/uzlib/src/crc32.c
new file mode 100644
index 00000000..e24c643b
--- /dev/null
+++ b/test/monniaux/uzlib/src/crc32.c
@@ -0,0 +1,63 @@
+/*
+ * CRC32 checksum
+ *
+ * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+/*
+ * CRC32 algorithm taken from the zlib source, which is
+ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
+ */
+
+#include "tinf.h"
+
+static const unsigned int tinf_crc32tab[16] = {
+ 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190,
+ 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344,
+ 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278,
+ 0xbdbdf21c
+};
+
+/* crc is previous value for incremental computation, 0xffffffff initially */
+uint32_t uzlib_crc32(const void *data, unsigned int length, uint32_t crc)
+{
+ const unsigned char *buf = (const unsigned char *)data;
+ unsigned int i;
+
+ for (i = 0; i < length; ++i)
+ {
+ crc ^= buf[i];
+ crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+ crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+ }
+
+ // return value suitable for passing in next time, for final value invert it
+ return crc/* ^ 0xffffffff*/;
+}
diff --git a/test/monniaux/uzlib/src/defl_static.c b/test/monniaux/uzlib/src/defl_static.c
new file mode 100644
index 00000000..80ea1977
--- /dev/null
+++ b/test/monniaux/uzlib/src/defl_static.c
@@ -0,0 +1,305 @@
+/*
+
+Routines in this file are based on:
+Zlib (RFC1950 / RFC1951) compression for PuTTY.
+
+PuTTY is copyright 1997-2014 Simon Tatham.
+
+Portions copyright Robert de Bath, Joris van Rantwijk, Delian
+Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
+Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
+Kuhn, Colin Watson, and CORE SDI S.A.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation files
+(the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include "defl_static.h"
+
+#define snew(type) ( (type *) malloc(sizeof(type)) )
+#define snewn(n, type) ( (type *) malloc((n) * sizeof(type)) )
+#define sresize(x, n, type) ( (type *) realloc((x), (n) * sizeof(type)) )
+#define sfree(x) ( free((x)) )
+
+#ifndef FALSE
+#define FALSE 0
+#define TRUE (!FALSE)
+#endif
+
+/* ----------------------------------------------------------------------
+ * Zlib compression. We always use the static Huffman tree option.
+ * Mostly this is because it's hard to scan a block in advance to
+ * work out better trees; dynamic trees are great when you're
+ * compressing a large file under no significant time constraint,
+ * but when you're compressing little bits in real time, things get
+ * hairier.
+ *
+ * I suppose it's possible that I could compute Huffman trees based
+ * on the frequencies in the _previous_ block, as a sort of
+ * heuristic, but I'm not confident that the gain would balance out
+ * having to transmit the trees.
+ */
+
+void outbits(struct Outbuf *out, unsigned long bits, int nbits)
+{
+ assert(out->noutbits + nbits <= 32);
+ out->outbits |= bits << out->noutbits;
+ out->noutbits += nbits;
+ while (out->noutbits >= 8) {
+ if (out->outlen >= out->outsize) {
+ out->outsize = out->outlen + 64;
+ out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
+ }
+ out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
+ out->outbits >>= 8;
+ out->noutbits -= 8;
+ }
+}
+
+static const unsigned char mirrorbytes[256] = {
+ 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
+ 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
+ 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
+ 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
+ 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
+ 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
+ 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
+ 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
+ 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
+ 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
+ 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
+ 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
+ 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
+ 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
+ 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
+ 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
+ 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
+ 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
+ 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
+ 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
+ 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
+ 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
+ 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
+ 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
+ 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
+ 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
+ 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
+ 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
+ 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
+ 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
+ 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
+ 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
+};
+
+typedef struct {
+ short code, extrabits;
+ uint16_t min, max;
+} coderecord;
+
+static const coderecord lencodes[] = {
+ {257, 0, 3, 3},
+ {258, 0, 4, 4},
+ {259, 0, 5, 5},
+ {260, 0, 6, 6},
+ {261, 0, 7, 7},
+ {262, 0, 8, 8},
+ {263, 0, 9, 9},
+ {264, 0, 10, 10},
+ {265, 1, 11, 12},
+ {266, 1, 13, 14},
+ {267, 1, 15, 16},
+ {268, 1, 17, 18},
+ {269, 2, 19, 22},
+ {270, 2, 23, 26},
+ {271, 2, 27, 30},
+ {272, 2, 31, 34},
+ {273, 3, 35, 42},
+ {274, 3, 43, 50},
+ {275, 3, 51, 58},
+ {276, 3, 59, 66},
+ {277, 4, 67, 82},
+ {278, 4, 83, 98},
+ {279, 4, 99, 114},
+ {280, 4, 115, 130},
+ {281, 5, 131, 162},
+ {282, 5, 163, 194},
+ {283, 5, 195, 226},
+ {284, 5, 227, 257},
+ {285, 0, 258, 258},
+};
+
+static const coderecord distcodes[] = {
+ {0, 0, 1, 1},
+ {1, 0, 2, 2},
+ {2, 0, 3, 3},
+ {3, 0, 4, 4},
+ {4, 1, 5, 6},
+ {5, 1, 7, 8},
+ {6, 2, 9, 12},
+ {7, 2, 13, 16},
+ {8, 3, 17, 24},
+ {9, 3, 25, 32},
+ {10, 4, 33, 48},
+ {11, 4, 49, 64},
+ {12, 5, 65, 96},
+ {13, 5, 97, 128},
+ {14, 6, 129, 192},
+ {15, 6, 193, 256},
+ {16, 7, 257, 384},
+ {17, 7, 385, 512},
+ {18, 8, 513, 768},
+ {19, 8, 769, 1024},
+ {20, 9, 1025, 1536},
+ {21, 9, 1537, 2048},
+ {22, 10, 2049, 3072},
+ {23, 10, 3073, 4096},
+ {24, 11, 4097, 6144},
+ {25, 11, 6145, 8192},
+ {26, 12, 8193, 12288},
+ {27, 12, 12289, 16384},
+ {28, 13, 16385, 24576},
+ {29, 13, 24577, 32768},
+};
+
+void zlib_literal(struct Outbuf *out, unsigned char c)
+{
+ if (out->comp_disabled) {
+ /*
+ * We're in an uncompressed block, so just output the byte.
+ */
+ outbits(out, c, 8);
+ return;
+ }
+
+ if (c <= 143) {
+ /* 0 through 143 are 8 bits long starting at 00110000. */
+ outbits(out, mirrorbytes[0x30 + c], 8);
+ } else {
+ /* 144 through 255 are 9 bits long starting at 110010000. */
+ outbits(out, 1 + 2 * mirrorbytes[0x90 - 144 + c], 9);
+ }
+}
+
+void zlib_match(struct Outbuf *out, int distance, int len)
+{
+ const coderecord *d, *l;
+ int i, j, k;
+
+ assert(!out->comp_disabled);
+
+ while (len > 0) {
+ int thislen;
+
+ /*
+ * We can transmit matches of lengths 3 through 258
+ * inclusive. So if len exceeds 258, we must transmit in
+ * several steps, with 258 or less in each step.
+ *
+ * Specifically: if len >= 261, we can transmit 258 and be
+ * sure of having at least 3 left for the next step. And if
+ * len <= 258, we can just transmit len. But if len == 259
+ * or 260, we must transmit len-3.
+ */
+ thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3);
+ len -= thislen;
+
+ /*
+ * Binary-search to find which length code we're
+ * transmitting.
+ */
+ i = -1;
+ j = sizeof(lencodes) / sizeof(*lencodes);
+ while (1) {
+ assert(j - i >= 2);
+ k = (j + i) / 2;
+ if (thislen < lencodes[k].min)
+ j = k;
+ else if (thislen > lencodes[k].max)
+ i = k;
+ else {
+ l = &lencodes[k];
+ break; /* found it! */
+ }
+ }
+
+ /*
+ * Transmit the length code. 256-279 are seven bits
+ * starting at 0000000; 280-287 are eight bits starting at
+ * 11000000.
+ */
+ if (l->code <= 279) {
+ outbits(out, mirrorbytes[(l->code - 256) * 2], 7);
+ } else {
+ outbits(out, mirrorbytes[0xc0 - 280 + l->code], 8);
+ }
+
+ /*
+ * Transmit the extra bits.
+ */
+ if (l->extrabits)
+ outbits(out, thislen - l->min, l->extrabits);
+
+ /*
+ * Binary-search to find which distance code we're
+ * transmitting.
+ */
+ i = -1;
+ j = sizeof(distcodes) / sizeof(*distcodes);
+ while (1) {
+ assert(j - i >= 2);
+ k = (j + i) / 2;
+ if (distance < distcodes[k].min)
+ j = k;
+ else if (distance > distcodes[k].max)
+ i = k;
+ else {
+ d = &distcodes[k];
+ break; /* found it! */
+ }
+ }
+
+ /*
+ * Transmit the distance code. Five bits starting at 00000.
+ */
+ outbits(out, mirrorbytes[d->code * 8], 5);
+
+ /*
+ * Transmit the extra bits.
+ */
+ if (d->extrabits)
+ outbits(out, distance - d->min, d->extrabits);
+ }
+}
+
+void zlib_start_block(struct Outbuf *out)
+{
+// outbits(out, 0x9C78, 16);
+ outbits(out, 1, 1); /* Final block */
+ outbits(out, 1, 2); /* Static huffman block */
+}
+
+void zlib_finish_block(struct Outbuf *out)
+{
+ outbits(out, 0, 7); /* close block */
+ outbits(out, 0, 7); /* Make sure all bits are flushed */
+}
diff --git a/test/monniaux/uzlib/src/defl_static.h b/test/monniaux/uzlib/src/defl_static.h
new file mode 100644
index 00000000..292734d7
--- /dev/null
+++ b/test/monniaux/uzlib/src/defl_static.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) uzlib authors
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+/* This files contains type declaration and prototypes for defl_static.c.
+ They may be altered/distinct from the originals used in PuTTY source
+ code. */
+
+struct Outbuf {
+ unsigned char *outbuf;
+ int outlen, outsize;
+ unsigned long outbits;
+ int noutbits;
+ int comp_disabled;
+};
+
+void outbits(struct Outbuf *out, unsigned long bits, int nbits);
+void zlib_start_block(struct Outbuf *ctx);
+void zlib_finish_block(struct Outbuf *ctx);
+void zlib_literal(struct Outbuf *ectx, unsigned char c);
+void zlib_match(struct Outbuf *ectx, int distance, int len);
diff --git a/test/monniaux/uzlib/src/genlz77.c b/test/monniaux/uzlib/src/genlz77.c
new file mode 100644
index 00000000..ede1fc9e
--- /dev/null
+++ b/test/monniaux/uzlib/src/genlz77.c
@@ -0,0 +1,124 @@
+/*
+ * genlz77 - Generic LZ77 compressor
+ *
+ * Copyright (c) 2014 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include "uzlib.h"
+
+#if 0
+#define HASH_BITS 12
+#else
+#define HASH_BITS data->hash_bits
+#endif
+
+#define HASH_SIZE (1<<HASH_BITS)
+
+/* Minimum and maximum length of matches to look for, inclusive */
+#define MIN_MATCH 3
+#define MAX_MATCH 258
+
+/* Max offset of the match to look for, inclusive */
+#if 0
+#define MAX_OFFSET 32768
+#else
+#define MAX_OFFSET data->dict_size
+#endif
+
+/* Hash function can be defined as macro or as inline function */
+
+/*#define HASH(p) (p[0] + p[1] + p[2])*/
+
+/* This is hash function from liblzf */
+static inline int HASH(struct uzlib_comp *data, const uint8_t *p) {
+ int v = (p[0] << 16) | (p[1] << 8) | p[2];
+ int hash = ((v >> (3*8 - HASH_BITS)) - v) & (HASH_SIZE - 1);
+ return hash;
+}
+
+#ifdef DUMP_LZTXT
+
+/* Counter for approximate compressed length in LZTXT mode. */
+/* Literal is counted as 1, copy as 2 bytes. */
+unsigned approx_compressed_len;
+
+void literal(void *data, uint8_t val)
+{
+ printf("L%02x # %c\n", val, (val >= 0x20 && val <= 0x7e) ? val : '?');
+ approx_compressed_len++;
+}
+
+void copy(void *data, unsigned offset, unsigned len)
+{
+ printf("C-%u,%u\n", offset, len);
+ approx_compressed_len += 2;
+}
+
+#else
+
+static inline void literal(void *data, uint8_t val)
+{
+ zlib_literal(data, val);
+}
+
+static inline void copy(void *data, unsigned offset, unsigned len)
+{
+ zlib_match(data, offset, len);
+}
+
+#endif
+
+
+void uzlib_compress(struct uzlib_comp *data, const uint8_t *src, unsigned slen)
+{
+ const uint8_t *top = src + slen - MIN_MATCH;
+ while (src < top) {
+ int h = HASH(data, src);
+ const uint8_t **bucket = &data->hash_table[h & (HASH_SIZE - 1)];
+ const uint8_t *subs = *bucket;
+ *bucket = src;
+ if (subs && src > subs && (src - subs) <= MAX_OFFSET && !memcmp(src, subs, MIN_MATCH)) {
+ src += MIN_MATCH;
+ const uint8_t *m = subs + MIN_MATCH;
+ int len = MIN_MATCH;
+ while (*src == *m && len < MAX_MATCH && src < top) {
+ src++; m++; len++;
+ }
+ copy(data, src - len - subs, len);
+ } else {
+ literal(data, *src++);
+ }
+ }
+ // Process buffer tail, which is less than MIN_MATCH
+ // (and so it doesn't make sense to look for matches there)
+ top += MIN_MATCH;
+ while (src < top) {
+ literal(data, *src++);
+ }
+}
diff --git a/test/monniaux/uzlib/src/makefile b/test/monniaux/uzlib/src/makefile
new file mode 100644
index 00000000..3ced616f
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile
@@ -0,0 +1,36 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## GCC makefile (Linux, FreeBSD, BeOS and QNX)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = ../lib/libtinf.a
+objects = tinflate.o tinfgzip.o tinfzlib.o adler32.o crc32.o \
+ defl_static.o genlz77.o
+
+COPT = -Os
+CFLAGS = -Wall $(COPT)
+LDFLAGS = $(CFLAGS) -s
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ ar -frs $@ $^
+ ranlib $@
+
+%.o : %.c
+ $(CC) $(CFLAGS) -o $@ -c $<
+
+%.o : %.nas
+ nasm -o $@ -f elf -D_ELF_ -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/src/makefile.b32 b/test/monniaux/uzlib/src/makefile.b32
new file mode 100644
index 00000000..52e17162
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.b32
@@ -0,0 +1,34 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## Borland C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = ..\lib\tinf.lib
+objects = tinflate.obj tinfgzip.obj tinfzlib.obj adler32.obj crc32.obj
+
+cflags = -a16 -K -O2 -OS
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ echo $(patsubst %,+%,$(objects)) >> lib.cmd
+ tlib $@ /C @lib.cmd
+ $(RM) lib.cmd
+
+%.obj : %.c
+ bcc32 $(cflags) -c $<
+
+%.obj : %.nas
+ nasm -o $@ -f obj -D_OBJ_ -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(objects) $(target) $(temps)
diff --git a/test/monniaux/uzlib/src/makefile.dj2 b/test/monniaux/uzlib/src/makefile.dj2
new file mode 100644
index 00000000..856ecfd6
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.dj2
@@ -0,0 +1,33 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## DJGPP makefile
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = ../lib/libtinf.a
+objects = tinflate.o tinfgzip.o tinfzlib.o adler32.o crc32.o
+
+cflags = -s -Wall -Os -fomit-frame-pointer
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ ar -frsv $@ $^
+ ranlib $@
+
+%.o : %.c
+ gcc $(cflags) -o $@ -c $<
+
+%.o : %.nas
+ nasm -o $@ -f coff -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/src/makefile.dmc b/test/monniaux/uzlib/src/makefile.dmc
new file mode 100644
index 00000000..7629b8f3
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.dmc
@@ -0,0 +1,32 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## Digital Mars C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = ..\lib\tinf.lib
+objects = tinflate.obj tinfgzip.obj tinfzlib.obj adler32.obj crc32.obj
+
+cflags = -s -mn -o+all
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ lib -c $@ $^
+
+%.obj : %.c
+ dmc $(cflags) -c $<
+
+%.obj : %.nas
+ nasm -o $@ -f obj -D_OBJ_ -O3 $<
+
+clean:
+ $(RM) $(objects) $(target) $(temps)
diff --git a/test/monniaux/uzlib/src/makefile.mgw b/test/monniaux/uzlib/src/makefile.mgw
new file mode 100644
index 00000000..020f1853
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.mgw
@@ -0,0 +1,31 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## MinGW / Cygwin makefile
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+
+target = ../lib/libtinf.a
+objects = tinflate.o tinfgzip.o tinfzlib.o adler32.o crc32.o
+
+cflags = -s -Wall -Os -fomit-frame-pointer
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ ar -frsv $@ $^
+ ranlib $@
+
+%.o : %.c
+ gcc $(cflags) -o $@ -c $<
+
+%.o : %.nas
+ nasm -o $@ -f win32 -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(target) $(objects)
diff --git a/test/monniaux/uzlib/src/makefile.vc b/test/monniaux/uzlib/src/makefile.vc
new file mode 100644
index 00000000..de37a20d
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.vc
@@ -0,0 +1,30 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## Visual C++ Makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+
+target = ../lib/tinf.lib
+objects = tinflate.obj tinfgzip.obj tinfzlib.obj adler32.obj crc32.obj
+
+cflags = /nologo /W3 /O1 /G6 /W3 /Gy /GF
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ lib /OUT:$@ $^
+
+%.obj : %.c
+ cl $(cflags) -c $<
+
+%.obj : %.nas
+ nasm -o $@ -f win32 -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(target) $(objects)
diff --git a/test/monniaux/uzlib/src/makefile.wat b/test/monniaux/uzlib/src/makefile.wat
new file mode 100644
index 00000000..573fb2d4
--- /dev/null
+++ b/test/monniaux/uzlib/src/makefile.wat
@@ -0,0 +1,35 @@
+##
+## tinflib - tiny inflate library (inflate, gzip, zlib)
+##
+## Watcom / OpenWatcom C/C++ makefile (GNU Make)
+##
+## Copyright (c) 2003 by Joergen Ibsen / Jibz
+## All Rights Reserved
+##
+## http://www.ibsensoftware.com/
+##
+
+target = ..\lib\tinf.lib
+objects = tinflate.obj tinfgzip.obj tinfzlib.obj adler32.obj crc32.obj
+system = nt
+
+cflags = -bt=$(system) -d0 -obmlrs -s -zl
+
+.PHONY: all clean
+
+all: $(target)
+
+$(target): $(objects)
+ $(RM) $@
+ echo $(patsubst %,+%,$(objects)) >> lib.cmd
+ wlib -c -n -q -s -fo -io $@ @lib.cmd
+ $(RM) lib.cmd
+
+%.obj : %.c
+ wcc386 $(cflags) $<
+
+%.obj : %.nas
+ nasm -o $@ -f obj -D_OBJ_ -O3 -Inasm/ $<
+
+clean:
+ $(RM) $(objects) $(target)
diff --git a/test/monniaux/uzlib/src/nasm/crc32.nas b/test/monniaux/uzlib/src/nasm/crc32.nas
new file mode 100644
index 00000000..bd91692b
--- /dev/null
+++ b/test/monniaux/uzlib/src/nasm/crc32.nas
@@ -0,0 +1,118 @@
+;;
+;; NASM assembler crc32
+;;
+;; Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
+;; All Rights Reserved
+;;
+;; http://www.ibsensoftware.com/
+;;
+;; This software is provided 'as-is', without any express
+;; or implied warranty. In no event will the authors be
+;; held liable for any damages arising from the use of
+;; this software.
+;;
+;; Permission is granted to anyone to use this software
+;; for any purpose, including commercial applications,
+;; and to alter it and redistribute it freely, subject to
+;; the following restrictions:
+;;
+;; 1. The origin of this software must not be
+;; misrepresented; you must not claim that you
+;; wrote the original software. If you use this
+;; software in a product, an acknowledgment in
+;; the product documentation would be appreciated
+;; but is not required.
+;;
+;; 2. Altered source versions must be plainly marked
+;; as such, and must not be misrepresented as
+;; being the original software.
+;;
+;; 3. This notice may not be removed or altered from
+;; any source distribution.
+;;
+
+; CRC32 algorithm taken from the zlib source, which is
+; Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
+
+cpu 386
+
+bits 32
+
+%include "nasmlcm.inc"
+
+section lcmtext
+
+lcmglobal tinf_crc32,8
+
+lcmexport tinf_crc32,8
+
+; =============================================================
+
+lcmlabel tinf_crc32,8
+ ; tinf_crc32(const void *data,
+ ; unsigned int length);
+
+ .len$ equ 2*4 + 4 + 4
+ .dat$ equ 2*4 + 4
+
+ push esi
+ push edi
+
+ mov esi, [esp + .dat$] ; esi -> buffer
+ mov ecx, [esp + .len$] ; ecx = length
+
+ sub eax, eax ; crc = 0
+
+ test esi, esi
+ jz short .c_exit
+
+ test ecx, ecx
+ jz short .c_exit
+
+ dec eax ; crc = 0xffffffff
+
+%ifdef _OBJ_
+ mov edi, tinf_crc32tab wrt FLAT ; edi -> crctab
+%else
+ mov edi, tinf_crc32tab ; edi -> crctab
+%endif
+
+ .c_next_byte:
+ xor al, [esi]
+ inc esi
+
+ mov edx, 0x0f
+ and edx, eax
+
+ shr eax, 4
+
+ xor eax, [edi + edx*4]
+
+ mov edx, 0x0f
+ and edx, eax
+
+ shr eax, 4
+
+ xor eax, [edi + edx*4]
+
+ dec ecx
+ jnz short .c_next_byte
+
+ not eax
+
+ .c_exit:
+ pop edi
+ pop esi
+
+ lcmret 8
+
+; =============================================================
+
+section lcmdata
+
+tinf_crc32tab dd 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190
+ dd 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344
+ dd 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278
+ dd 0xbdbdf21c
+
+; =============================================================
diff --git a/test/monniaux/uzlib/src/nasm/nasmlcm.inc b/test/monniaux/uzlib/src/nasm/nasmlcm.inc
new file mode 100644
index 00000000..5cbe7e0d
--- /dev/null
+++ b/test/monniaux/uzlib/src/nasm/nasmlcm.inc
@@ -0,0 +1,326 @@
+;;
+;; NASM linker compatibility macros 2002.07.24
+;;
+;; Copyright (c) 2001-2003 by Joergen Ibsen / Jibz
+;; All Rights Reserved
+;;
+;; http://www.ibsensoftware.com/
+;;
+
+; define _ELF_ for ELF32 object files
+; define _OBJ_ for OMF object files
+; define _OBJ_ and _DLL_ for OMF object files for a dll (stdcall)
+; define _MSLIBS_ for MS style Win32 import libs (lcmwinextern)
+; default is DJGPP/WIN32 COFF object files
+
+; remember to do lcm*extern before lcmimport
+
+; ====================================================================
+;
+; There are differences between how the object formats that NASM
+; supports work, and what features they support. Similarly there
+; are differences between how complete and standard compliant the
+; support for these formats are in linkers.
+;
+; The NASM linker compatibility macros (nasmlcm) were put together
+; to ease my work by allowing a single source file to be assembled
+; for use with a number of compilers/linkers.
+;
+; Currently obj/omf, win32/coff, djgpp/coff and elf32 output formats
+; are supported. The following macros are available:
+;
+; lcmtext - section name for the code section
+; lcmdata - section name for the initialized data section
+; lcmbss - section name for the uninitialized data section
+;
+; lcmglobal - declare a function (two arguments) or data (one
+; argument) as global in the current format
+; lcmcglobal - same as lcmglobal, but uses C name decoration
+;
+; lcmextern - declare a function (two arguments) or data (one
+; argument) as extern in the current format
+; lcmcextern - same as lcmextern, but uses C name decoration
+; lcmdllextern - same as lcmextern, but uses dll name decoration
+; lcmwinextern - same as lcmextern, but uses name decoration for
+; calling Win32 Api functions (see _MSLIBS_)
+;
+; lcmimport - declares a function (two arguments) or data (one
+; argument) as imported in the current format
+; lcmexport - declares a function (two arguments) or data (one
+; argument) as exported in the current format
+;
+; lcmlabel - start label for a function in the current format
+; lcmclabel - start label for a function with C name decoration
+; lcmadjust - adjust stack after a function call in the current
+; format
+; lcmret - return from a function in the current format
+; lcmcret - return from a C function
+;
+; The following defines change the format and behaviour:
+;
+; _ELF_ - the lcm*global macro adds :function and :data type
+; specifiers
+;
+; _OBJ_ - section names are similar to those produced by
+; Borland tools to increase compatibility with
+; various OMF compatible linkers
+;
+; _DLL_ - functions are exported and imported with added
+; size specifiers (_SomeFunction@12), lcmret adjusts
+; stack (stdcall)
+;
+; _MSLIBS_ - the lcmwinextern macro prepends an underscore and
+; adds size specification for functions, allowing
+; the object file to be linked with MS libraries.
+;
+; ====================================================================
+
+%ifndef NASMLCM_INC_INCLUDED
+%define NASMLCM_INC_INCLUDED
+
+%ifdef _DLL_
+ %ifndef _OBJ_
+ %error "_DLL_ needs _OBJ_ defined!"
+ %endif
+%endif
+
+; --- define lcm- section names ---
+;
+; a number of linkers require omf objects where the section
+; names are equal to those produces by tasm.
+
+%ifdef _OBJ_
+
+ %define lcmtext _TEXT class=CODE public use32 align=4 FLAT
+ %define lcmdata _DATA class=DATA public use32 align=4
+ %define lcmbss _BSS class=BSS public use32 align=4 FLAT
+
+ group FLAT
+ group DGROUP _DATA
+
+%else ; _OBJ_
+
+ %define lcmtext .text
+ %define lcmdata .data
+ %define lcmbss .bss
+
+%endif ; _OBJ_
+
+; --- define lcmglobal and lcm*extern macros ---
+;
+; special handling of functions and data for ELF32
+
+%ifdef _ELF_
+
+ %macro lcmglobal 2
+ global %{1}:function
+ %endmacro
+ %macro lcmglobal 1
+ global %{1}:data
+ %endmacro
+
+ %define lcmcglobal lcmglobal
+
+ %macro lcmextern 1-2
+ extern %1
+ %endmacro
+
+ %macro lcmcextern 0
+ %error lcmcextern not supported in ELF format
+ %endmacro
+
+ %macro lcmdllextern 0
+ %error lcmdllextern not supported in ELF format
+ %endmacro
+
+%else ; _ELF_
+
+ %ifdef _DLL_
+
+ %macro lcmglobal 2
+ global _%1
+ global _%1@%2
+ %endmacro
+ %macro lcmglobal 1
+ global _%1
+ %define %1 _%1
+ %endmacro
+
+ %macro lcmcglobal 2
+ global _%1
+ %endmacro
+ %macro lcmcglobal 1
+ global _%1
+ %define %1 _%1
+ %endmacro
+
+ %macro lcmextern 2
+ extern _%1@%2
+ %define %1 _%1@%2
+ %endmacro
+ %macro lcmextern 1
+ extern _%1
+ %define %1 _%1
+ %endmacro
+
+ %else
+
+ %macro lcmglobal 2
+ global _%1
+ %endmacro
+ %macro lcmglobal 1
+ global _%1
+ %define %1 _%1
+ %endmacro
+
+ %define lcmcglobal lcmglobal
+
+ %macro lcmextern 1-2
+ extern _%1
+ %define %1 _%1
+ %endmacro
+
+ %endif
+
+ %macro lcmcextern 1-2
+ extern _%1
+ %define %1 _%1
+ %endmacro
+
+ %macro lcmdllextern 2
+ extern _%1@%2
+ %define %1 _%1@%2
+ %endmacro
+ %macro lcmdllextern 1
+ extern _%1
+ %define %1 _%1
+ %endmacro
+
+ %macro lcmwinextern 2
+ %ifdef _MSLIBS_
+ extern _%1@%2
+ %define %1 _%1@%2
+ %else
+ extern %1
+ %endif
+ %endmacro
+
+%endif ; _ELF_
+
+; --- define lcmimport and lcmexport ---
+;
+
+%ifdef _OBJ_
+
+ %macro lcmimport 2-3
+ import %1 %2 %3
+ %rotate 1
+ %endmacro
+
+ %ifdef _DLL_
+
+ %macro lcmexport 2
+ export _%1
+ export _%1@%2
+ %endmacro
+ %macro lcmexport 1
+ export _%1
+ %endmacro
+
+ %else
+
+ %macro lcmexport 1-2
+ %endmacro
+
+ %endif
+
+%else ; _OBJ_
+
+ %macro lcmimport 2-3
+ %endmacro
+
+ %macro lcmexport 1-2
+ %endmacro
+
+%endif ; _OBJ_
+
+; --- define lcmlabel, lcmadjust and lcmret macros ---
+;
+; we need special labels and stdcall calling convention when
+; assembling for a dll
+
+%ifdef _ELF_
+
+ %macro lcmlabel 2
+ %1:
+ %endmacro
+
+ %define lcmclabel lcmlabel
+
+ %macro lcmadjust 1
+ %if %1 < 128
+ add esp, byte %1
+ %else
+ add esp, %1
+ %endif
+ %endmacro
+
+ %macro lcmret 1
+ ret
+ %endmacro
+
+ %define lcmcret lcmret
+
+%else ; _ELF_
+
+ %ifdef _DLL_
+ %macro lcmlabel 2
+ _%1:
+ _%1@%2:
+ %endmacro
+
+ %macro lcmclabel 2
+ _%1:
+ %endmacro
+
+ %macro lcmadjust 1
+ %endmacro
+
+ %macro lcmret 1
+ %if %1 > 0
+ ret %1
+ %else
+ ret
+ %endif
+ %endmacro
+
+ %macro lcmcret 1
+ ret
+ %endmacro
+
+ %else
+
+ %macro lcmlabel 2
+ _%1:
+ %endmacro
+
+ %define lcmclabel lcmlabel
+
+ %macro lcmadjust 1
+ %if %1 < 128
+ add esp, byte %1
+ %else
+ add esp, %1
+ %endif
+ %endmacro
+
+ %macro lcmret 1
+ ret
+ %endmacro
+
+ %define lcmcret lcmret
+ %endif
+
+%endif ; _ELF_
+
+%endif ; NASMLCM_INC_INCLUDED
diff --git a/test/monniaux/uzlib/src/nasm/tinfzlib.nas b/test/monniaux/uzlib/src/nasm/tinfzlib.nas
new file mode 100644
index 00000000..1f9519eb
--- /dev/null
+++ b/test/monniaux/uzlib/src/nasm/tinfzlib.nas
@@ -0,0 +1,160 @@
+;;
+;; tinfzlib - tiny zlib uncompress
+;;
+;; Copyright (c) 2003 by Joergen Ibsen / Jibz
+;; All Rights Reserved
+;;
+;; http://www.ibsensoftware.com/
+;;
+;; This software is provided 'as-is', without any express
+;; or implied warranty. In no event will the authors be
+;; held liable for any damages arising from the use of
+;; this software.
+;;
+;; Permission is granted to anyone to use this software
+;; for any purpose, including commercial applications,
+;; and to alter it and redistribute it freely, subject to
+;; the following restrictions:
+;;
+;; 1. The origin of this software must not be
+;; misrepresented; you must not claim that you
+;; wrote the original software. If you use this
+;; software in a product, an acknowledgment in
+;; the product documentation would be appreciated
+;; but is not required.
+;;
+;; 2. Altered source versions must be plainly marked
+;; as such, and must not be misrepresented as
+;; being the original software.
+;;
+;; 3. This notice may not be removed or altered from
+;; any source distribution.
+;;
+
+TINF_OK equ 0
+TINF_DATA_ERROR equ (-3)
+
+cpu 386
+
+bits 32
+
+%include "nasmlcm.inc"
+
+section lcmtext
+
+lcmglobal tinf_zlib_uncompress,16
+
+lcmexport tinf_zlib_uncompress,16
+
+lcmextern tinf_uncompress,16
+lcmextern tinf_adler32,8
+
+; =============================================================
+
+lcmlabel tinf_zlib_uncompress,16
+ ; tinf_zlib_uncompress(void *dest,
+ ; unsigned int *destLen,
+ ; const void *source,
+ ; unsigned int sourceLen)
+
+ .slen$ equ 2*4 + 4 + 12
+ .src$ equ 2*4 + 4 + 8
+ .dlen$ equ 2*4 + 4 + 4
+ .dst$ equ 2*4 + 4
+
+ push esi
+ push ebx
+
+ mov esi, [esp + .src$] ; esi -> source
+
+ ; -- get header bytes --
+
+ movzx eax, word [esi] ; al = cmf, ah = flg,
+
+ ; -- check format --
+
+ ; check method is deflate
+ ; if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR;
+ mov cl, 0x0f
+ and cl, al
+ cmp cl, 8
+ jne short .return_error
+
+ ; check window size is valid
+ ; if ((cmf >> 4) > 7) return TINF_DATA_ERROR;
+ mov ch, al
+ shr ch, 4
+ cmp ch, cl ; cl = 8 from above
+ jae short .return_error
+
+ ; check there is no preset dictionary
+ ; if (flg & 0x20) return TINF_DATA_ERROR;
+ test ah, 0x20
+ jnz short .return_error
+
+ ; check checksum
+ ; if ((256*cmf + flg) % 31) return TINF_DATA_ERROR;
+ xchg al, ah
+ xor edx, edx
+ lea ebx, [edx + 31]
+ div ebx
+ test edx, edx
+ jnz short .return_error
+
+ ; -- get adler32 checksum --
+
+ mov ecx, [esp + .slen$] ; ecx = sourceLen
+ mov ebx, [esi + ecx - 4]
+
+ %ifdef BSWAP_OK
+ bswap ebx
+ %else ; BSWAP_OK
+ xchg bl, bh
+ rol ebx, 16
+ xchg bl, bh
+ %endif ; BSWAP_OK
+
+ ; -- inflate --
+
+ ; res = tinf_uncompress(dst, destLen, src + 2, sourceLen - 6);
+ lea eax, [ecx - 6]
+ push eax
+ lea eax, [esi + 2]
+ push eax
+ push dword [esp + 8 + .dlen$]
+ push dword [esp + 12 + .dst$]
+ call tinf_uncompress
+ add esp, byte 16
+
+ ; if (res != TINF_OK) return TINF_DATA_ERROR;
+ test eax, eax
+ jnz short .return_error
+
+ ; -- check adler32 checksum --
+
+ ; if (a32 != tinf_adler32(dst, *destLen)) return TINF_DATA_ERROR;
+ mov eax, [esp + .dlen$];
+ push dword [eax]
+ push dword [esp + 4 + .dst$]
+ call tinf_adler32
+ add esp, byte 8
+
+ sub eax, ebx
+ jz short .return_eax
+
+ .return_error:
+ mov eax, TINF_DATA_ERROR
+
+ .return_eax:
+ pop ebx
+ pop esi
+
+ lcmret 16
+
+; =============================================================
+
+%ifdef _OBJ_
+ section lcmdata
+%endif
+
+; =============================================================
diff --git a/test/monniaux/uzlib/src/tinf.h b/test/monniaux/uzlib/src/tinf.h
new file mode 100644
index 00000000..ae6e1c40
--- /dev/null
+++ b/test/monniaux/uzlib/src/tinf.h
@@ -0,0 +1,3 @@
+/* Compatibility header for the original tinf lib/older versions of uzlib.
+ Note: may be removed in the future, please migrate to uzlib.h. */
+#include "uzlib.h"
diff --git a/test/monniaux/uzlib/src/tinf_compat.h b/test/monniaux/uzlib/src/tinf_compat.h
new file mode 100644
index 00000000..f763804b
--- /dev/null
+++ b/test/monniaux/uzlib/src/tinf_compat.h
@@ -0,0 +1,9 @@
+/* This header contains compatibility defines for the original tinf API
+ and uzlib 2.x and below API. These defines are deprecated and going
+ to be removed in the future, so applications should migrate to new
+ uzlib API. */
+#define TINF_DATA struct uzlib_uncomp
+
+#define destSize dest_size
+#define destStart dest_start
+#define readSource source_read_cb
diff --git a/test/monniaux/uzlib/src/tinfgzip.c b/test/monniaux/uzlib/src/tinfgzip.c
new file mode 100644
index 00000000..22b000df
--- /dev/null
+++ b/test/monniaux/uzlib/src/tinfgzip.c
@@ -0,0 +1,110 @@
+/*
+ * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#include "tinf.h"
+
+#define FTEXT 1
+#define FHCRC 2
+#define FEXTRA 4
+#define FNAME 8
+#define FCOMMENT 16
+
+void tinf_skip_bytes(TINF_DATA *d, int num);
+uint16_t tinf_get_uint16(TINF_DATA *d);
+
+void tinf_skip_bytes(TINF_DATA *d, int num)
+{
+ while (num--) uzlib_get_byte(d);
+}
+
+uint16_t tinf_get_uint16(TINF_DATA *d)
+{
+ unsigned int v = uzlib_get_byte(d);
+ v = (uzlib_get_byte(d) << 8) | v;
+ return v;
+}
+
+int uzlib_gzip_parse_header(TINF_DATA *d)
+{
+ unsigned char flg;
+
+ /* -- check format -- */
+
+ /* check id bytes */
+ if (uzlib_get_byte(d) != 0x1f || uzlib_get_byte(d) != 0x8b) return TINF_DATA_ERROR;
+
+ /* check method is deflate */
+ if (uzlib_get_byte(d) != 8) return TINF_DATA_ERROR;
+
+ /* get flag byte */
+ flg = uzlib_get_byte(d);
+
+ /* check that reserved bits are zero */
+ if (flg & 0xe0) return TINF_DATA_ERROR;
+
+ /* -- find start of compressed data -- */
+
+ /* skip rest of base header of 10 bytes */
+ tinf_skip_bytes(d, 6);
+
+ /* skip extra data if present */
+ if (flg & FEXTRA)
+ {
+ unsigned int xlen = tinf_get_uint16(d);
+ tinf_skip_bytes(d, xlen);
+ }
+
+ /* skip file name if present */
+ if (flg & FNAME) { while (uzlib_get_byte(d)); }
+
+ /* skip file comment if present */
+ if (flg & FCOMMENT) { while (uzlib_get_byte(d)); }
+
+ /* check header crc if present */
+ if (flg & FHCRC)
+ {
+ /*unsigned int hcrc =*/ tinf_get_uint16(d);
+
+ // TODO: Check!
+// if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
+// return TINF_DATA_ERROR;
+ }
+
+ /* initialize for crc32 checksum */
+ d->checksum_type = TINF_CHKSUM_CRC;
+ d->checksum = ~0;
+
+ return TINF_OK;
+}
diff --git a/test/monniaux/uzlib/src/tinflate.c b/test/monniaux/uzlib/src/tinflate.c
new file mode 100644
index 00000000..b93bc1fa
--- /dev/null
+++ b/test/monniaux/uzlib/src/tinflate.c
@@ -0,0 +1,659 @@
+/*
+ * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ * http://www.ibsensoftware.com/
+ *
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#include <assert.h>
+#include "tinf.h"
+
+#define UZLIB_DUMP_ARRAY(heading, arr, size) \
+ { \
+ printf("%s", heading); \
+ for (int i = 0; i < size; ++i) { \
+ printf(" %d", (arr)[i]); \
+ } \
+ printf("\n"); \
+ }
+
+uint32_t tinf_get_le_uint32(TINF_DATA *d);
+uint32_t tinf_get_be_uint32(TINF_DATA *d);
+
+/* --------------------------------------------------- *
+ * -- uninitialized global data (static structures) -- *
+ * --------------------------------------------------- */
+
+#ifdef RUNTIME_BITS_TABLES
+
+/* extra bits and base tables for length codes */
+unsigned char length_bits[30];
+unsigned short length_base[30];
+
+/* extra bits and base tables for distance codes */
+unsigned char dist_bits[30];
+unsigned short dist_base[30];
+
+#else
+
+const unsigned char length_bits[30] = {
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 1, 1, 1, 1, 2, 2, 2, 2,
+ 3, 3, 3, 3, 4, 4, 4, 4,
+ 5, 5, 5, 5
+};
+const unsigned short length_base[30] = {
+ 3, 4, 5, 6, 7, 8, 9, 10,
+ 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115,
+ 131, 163, 195, 227, 258
+};
+
+const unsigned char dist_bits[30] = {
+ 0, 0, 0, 0, 1, 1, 2, 2,
+ 3, 3, 4, 4, 5, 5, 6, 6,
+ 7, 7, 8, 8, 9, 9, 10, 10,
+ 11, 11, 12, 12, 13, 13
+};
+const unsigned short dist_base[30] = {
+ 1, 2, 3, 4, 5, 7, 9, 13,
+ 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073,
+ 4097, 6145, 8193, 12289, 16385, 24577
+};
+
+#endif
+
+/* special ordering of code length codes */
+const unsigned char clcidx[] = {
+ 16, 17, 18, 0, 8, 7, 9, 6,
+ 10, 5, 11, 4, 12, 3, 13, 2,
+ 14, 1, 15
+};
+
+/* ----------------------- *
+ * -- utility functions -- *
+ * ----------------------- */
+
+#ifdef RUNTIME_BITS_TABLES
+/* build extra bits and base tables */
+static void tinf_build_bits_base(unsigned char *bits, unsigned short *base, int delta, int first)
+{
+ int i, sum;
+
+ /* build bits table */
+ for (i = 0; i < delta; ++i) bits[i] = 0;
+ for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta;
+
+ /* build base table */
+ for (sum = first, i = 0; i < 30; ++i)
+ {
+ base[i] = sum;
+ sum += 1 << bits[i];
+ }
+}
+#endif
+
+/* build the fixed huffman trees */
+static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt)
+{
+ int i;
+
+ /* build fixed length tree */
+ for (i = 0; i < 7; ++i) lt->table[i] = 0;
+
+ lt->table[7] = 24;
+ lt->table[8] = 152;
+ lt->table[9] = 112;
+
+ for (i = 0; i < 24; ++i) lt->trans[i] = 256 + i;
+ for (i = 0; i < 144; ++i) lt->trans[24 + i] = i;
+ for (i = 0; i < 8; ++i) lt->trans[24 + 144 + i] = 280 + i;
+ for (i = 0; i < 112; ++i) lt->trans[24 + 144 + 8 + i] = 144 + i;
+
+ /* build fixed distance tree */
+ for (i = 0; i < 5; ++i) dt->table[i] = 0;
+
+ dt->table[5] = 32;
+
+ for (i = 0; i < 32; ++i) dt->trans[i] = i;
+}
+
+/* given an array of code lengths, build a tree */
+static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned int num)
+{
+ unsigned short offs[16];
+ unsigned int i, sum;
+
+ /* clear code length count table */
+ for (i = 0; i < 16; ++i) t->table[i] = 0;
+
+ /* scan symbol lengths, and sum code length counts */
+ for (i = 0; i < num; ++i) t->table[lengths[i]]++;
+
+ #if UZLIB_CONF_DEBUG_LOG >= 2
+ UZLIB_DUMP_ARRAY("codelen counts:", t->table, TINF_ARRAY_SIZE(t->table));
+ #endif
+
+ /* In the lengths array, 0 means unused code. So, t->table[0] now contains
+ number of unused codes. But table's purpose is to contain # of codes of
+ particular length, and there're 0 codes of length 0. */
+ t->table[0] = 0;
+
+ /* compute offset table for distribution sort */
+ for (sum = 0, i = 0; i < 16; ++i)
+ {
+ offs[i] = sum;
+ sum += t->table[i];
+ }
+
+ #if UZLIB_CONF_DEBUG_LOG >= 2
+ UZLIB_DUMP_ARRAY("codelen offsets:", offs, TINF_ARRAY_SIZE(offs));
+ #endif
+
+ /* create code->symbol translation table (symbols sorted by code) */
+ for (i = 0; i < num; ++i)
+ {
+ if (lengths[i]) t->trans[offs[lengths[i]]++] = i;
+ }
+}
+
+/* ---------------------- *
+ * -- decode functions -- *
+ * ---------------------- */
+
+unsigned char uzlib_get_byte(TINF_DATA *d)
+{
+ /* If end of source buffer is not reached, return next byte from source
+ buffer. */
+ if (d->source < d->source_limit) {
+ return *d->source++;
+ }
+
+ /* Otherwise if there's callback and we haven't seen EOF yet, try to
+ read next byte using it. (Note: the callback can also update ->source
+ and ->source_limit). */
+ if (d->readSource && !d->eof) {
+ int val = d->readSource(d);
+ if (val >= 0) {
+ return (unsigned char)val;
+ }
+ }
+
+ /* Otherwise, we hit EOF (either from ->readSource() or from exhaustion
+ of the buffer), and it will be "sticky", i.e. further calls to this
+ function will end up here too. */
+ d->eof = true;
+
+ return 0;
+}
+
+uint32_t tinf_get_le_uint32(TINF_DATA *d)
+{
+ uint32_t val = 0;
+ int i;
+ for (i = 4; i--;) {
+ val = val >> 8 | ((uint32_t)uzlib_get_byte(d)) << 24;
+ }
+ return val;
+}
+
+uint32_t tinf_get_be_uint32(TINF_DATA *d)
+{
+ uint32_t val = 0;
+ int i;
+ for (i = 4; i--;) {
+ val = val << 8 | uzlib_get_byte(d);
+ }
+ return val;
+}
+
+/* get one bit from source stream */
+static int tinf_getbit(TINF_DATA *d)
+{
+ unsigned int bit;
+
+ /* check if tag is empty */
+ if (!d->bitcount--)
+ {
+ /* load next tag */
+ d->tag = uzlib_get_byte(d);
+ d->bitcount = 7;
+ }
+
+ /* shift bit out of tag */
+ bit = d->tag & 0x01;
+ d->tag >>= 1;
+
+ return bit;
+}
+
+/* read a num bit value from a stream and add base */
+static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base)
+{
+ unsigned int val = 0;
+
+ /* read num bits */
+ if (num)
+ {
+ unsigned int limit = 1 << (num);
+ unsigned int mask;
+
+ for (mask = 1; mask < limit; mask *= 2)
+ if (tinf_getbit(d)) val += mask;
+ }
+
+ return val + base;
+}
+
+/* given a data stream and a tree, decode a symbol */
+static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
+{
+ int sum = 0, cur = 0, len = 0;
+
+ /* get more bits while code value is above sum */
+ do {
+
+ cur = 2*cur + tinf_getbit(d);
+
+ if (++len == TINF_ARRAY_SIZE(t->table)) {
+ return TINF_DATA_ERROR;
+ }
+
+ sum += t->table[len];
+ cur -= t->table[len];
+
+ } while (cur >= 0);
+
+ sum += cur;
+ #if UZLIB_CONF_PARANOID_CHECKS
+ if (sum < 0 || sum >= TINF_ARRAY_SIZE(t->trans)) {
+ return TINF_DATA_ERROR;
+ }
+ #endif
+
+ return t->trans[sum];
+}
+
+/* given a data stream, decode dynamic trees from it */
+static int tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
+{
+ /* code lengths for 288 literal/len symbols and 32 dist symbols */
+ unsigned char lengths[288+32];
+ unsigned int hlit, hdist, hclen, hlimit;
+ unsigned int i, num, length;
+
+ /* get 5 bits HLIT (257-286) */
+ hlit = tinf_read_bits(d, 5, 257);
+
+ /* get 5 bits HDIST (1-32) */
+ hdist = tinf_read_bits(d, 5, 1);
+
+ /* get 4 bits HCLEN (4-19) */
+ hclen = tinf_read_bits(d, 4, 4);
+
+ for (i = 0; i < 19; ++i) lengths[i] = 0;
+
+ /* read code lengths for code length alphabet */
+ for (i = 0; i < hclen; ++i)
+ {
+ /* get 3 bits code length (0-7) */
+ unsigned int clen = tinf_read_bits(d, 3, 0);
+
+ lengths[clcidx[i]] = clen;
+ }
+
+ /* build code length tree, temporarily use length tree */
+ tinf_build_tree(lt, lengths, 19);
+
+ /* decode code lengths for the dynamic trees */
+ hlimit = hlit + hdist;
+ for (num = 0; num < hlimit; )
+ {
+ int sym = tinf_decode_symbol(d, lt);
+ unsigned char fill_value = 0;
+ int lbits, lbase = 3;
+
+ /* error decoding */
+ if (sym < 0) return sym;
+
+ switch (sym)
+ {
+ case 16:
+ /* copy previous code length 3-6 times (read 2 bits) */
+ if (num == 0) return TINF_DATA_ERROR;
+ fill_value = lengths[num - 1];
+ lbits = 2;
+ break;
+ case 17:
+ /* repeat code length 0 for 3-10 times (read 3 bits) */
+ lbits = 3;
+ break;
+ case 18:
+ /* repeat code length 0 for 11-138 times (read 7 bits) */
+ lbits = 7;
+ lbase = 11;
+ break;
+ default:
+ /* values 0-15 represent the actual code lengths */
+ lengths[num++] = sym;
+ /* continue the for loop */
+ continue;
+ }
+
+ /* special code length 16-18 are handled here */
+ length = tinf_read_bits(d, lbits, lbase);
+ if (num + length > hlimit) return TINF_DATA_ERROR;
+ for (; length; --length)
+ {
+ lengths[num++] = fill_value;
+ }
+ }
+
+ #if UZLIB_CONF_DEBUG_LOG >= 2
+ printf("lit code lengths (%d):", hlit);
+ UZLIB_DUMP_ARRAY("", lengths, hlit);
+ printf("dist code lengths (%d):", hdist);
+ UZLIB_DUMP_ARRAY("", lengths + hlit, hdist);
+ #endif
+
+ #if UZLIB_CONF_PARANOID_CHECKS
+ /* Check that there's "end of block" symbol */
+ if (lengths[256] == 0) {
+ return TINF_DATA_ERROR;
+ }
+ #endif
+
+ /* build dynamic trees */
+ tinf_build_tree(lt, lengths, hlit);
+ tinf_build_tree(dt, lengths + hlit, hdist);
+
+ return TINF_OK;
+}
+
+/* ----------------------------- *
+ * -- block inflate functions -- *
+ * ----------------------------- */
+
+/* given a stream and two trees, inflate next byte of output */
+static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
+{
+ if (d->curlen == 0) {
+ unsigned int offs;
+ int dist;
+ int sym = tinf_decode_symbol(d, lt);
+ //printf("huff sym: %02x\n", sym);
+
+ if (d->eof) {
+ return TINF_DATA_ERROR;
+ }
+
+ /* literal byte */
+ if (sym < 256) {
+ TINF_PUT(d, sym);
+ return TINF_OK;
+ }
+
+ /* end of block */
+ if (sym == 256) {
+ return TINF_DONE;
+ }
+
+ /* substring from sliding dictionary */
+ sym -= 257;
+ if (sym >= 29) {
+ return TINF_DATA_ERROR;
+ }
+
+ /* possibly get more bits from length code */
+ d->curlen = tinf_read_bits(d, length_bits[sym], length_base[sym]);
+
+ dist = tinf_decode_symbol(d, dt);
+ if (dist >= 30) {
+ return TINF_DATA_ERROR;
+ }
+
+ /* possibly get more bits from distance code */
+ offs = tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
+
+ /* calculate and validate actual LZ offset to use */
+ if (d->dict_ring) {
+ if (offs > d->dict_size) {
+ return TINF_DICT_ERROR;
+ }
+ /* Note: unlike full-dest-in-memory case below, we don't
+ try to catch offset which points to not yet filled
+ part of the dictionary here. Doing so would require
+ keeping another variable to track "filled in" size
+ of the dictionary. Appearance of such an offset cannot
+ lead to accessing memory outside of the dictionary
+ buffer, and clients which don't want to leak unrelated
+ information, should explicitly initialize dictionary
+ buffer passed to uzlib. */
+
+ d->lzOff = d->dict_idx - offs;
+ if (d->lzOff < 0) {
+ d->lzOff += d->dict_size;
+ }
+ } else {
+ /* catch trying to point before the start of dest buffer */
+ if (offs > d->dest - d->destStart) {
+ return TINF_DATA_ERROR;
+ }
+ d->lzOff = -offs;
+ }
+ }
+
+ /* copy next byte from dict substring */
+ if (d->dict_ring) {
+ TINF_PUT(d, d->dict_ring[d->lzOff]);
+ if ((unsigned)++d->lzOff == d->dict_size) {
+ d->lzOff = 0;
+ }
+ } else {
+ d->dest[0] = d->dest[d->lzOff];
+ d->dest++;
+ }
+ d->curlen--;
+ return TINF_OK;
+}
+
+/* inflate next byte from uncompressed block of data */
+static int tinf_inflate_uncompressed_block(TINF_DATA *d)
+{
+ if (d->curlen == 0) {
+ unsigned int length, invlength;
+
+ /* get length */
+ length = uzlib_get_byte(d);
+ length += 256 * uzlib_get_byte(d);
+ /* get one's complement of length */
+ invlength = uzlib_get_byte(d);
+ invlength += 256 * uzlib_get_byte(d);
+ /* check length */
+ if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR;
+
+ /* increment length to properly return TINF_DONE below, without
+ producing data at the same time */
+ d->curlen = length + 1;
+
+ /* make sure we start next block on a byte boundary */
+ d->bitcount = 0;
+ }
+
+ if (--d->curlen == 0) {
+ return TINF_DONE;
+ }
+
+ unsigned char c = uzlib_get_byte(d);
+ TINF_PUT(d, c);
+ return TINF_OK;
+}
+
+/* ---------------------- *
+ * -- public functions -- *
+ * ---------------------- */
+
+/* initialize global (static) data */
+void uzlib_init(void)
+{
+#ifdef RUNTIME_BITS_TABLES
+ /* build extra bits and base tables */
+ tinf_build_bits_base(length_bits, length_base, 4, 3);
+ tinf_build_bits_base(dist_bits, dist_base, 2, 1);
+
+ /* fix a special case */
+ length_bits[28] = 0;
+ length_base[28] = 258;
+#endif
+}
+
+/* initialize decompression structure */
+void uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen)
+{
+ d->eof = 0;
+ d->bitcount = 0;
+ d->bfinal = 0;
+ d->btype = -1;
+ d->dict_size = dictLen;
+ d->dict_ring = dict;
+ d->dict_idx = 0;
+ d->curlen = 0;
+}
+
+/* inflate next output bytes from compressed stream */
+int uzlib_uncompress(TINF_DATA *d)
+{
+ do {
+ int res;
+
+ /* start a new block */
+ if (d->btype == -1) {
+next_blk:
+ /* read final block flag */
+ d->bfinal = tinf_getbit(d);
+ /* read block type (2 bits) */
+ d->btype = tinf_read_bits(d, 2, 0);
+
+ #if UZLIB_CONF_DEBUG_LOG >= 1
+ printf("Started new block: type=%d final=%d\n", d->btype, d->bfinal);
+ #endif
+
+ if (d->btype == 1) {
+ /* build fixed huffman trees */
+ tinf_build_fixed_trees(&d->ltree, &d->dtree);
+ } else if (d->btype == 2) {
+ /* decode trees from stream */
+ res = tinf_decode_trees(d, &d->ltree, &d->dtree);
+ if (res != TINF_OK) {
+ return res;
+ }
+ }
+ }
+
+ /* process current block */
+ switch (d->btype)
+ {
+ case 0:
+ /* decompress uncompressed block */
+ res = tinf_inflate_uncompressed_block(d);
+ break;
+ case 1:
+ case 2:
+ /* decompress block with fixed/dynamic huffman trees */
+ /* trees were decoded previously, so it's the same routine for both */
+ res = tinf_inflate_block_data(d, &d->ltree, &d->dtree);
+ break;
+ default:
+ return TINF_DATA_ERROR;
+ }
+
+ if (res == TINF_DONE && !d->bfinal) {
+ /* the block has ended (without producing more data), but we
+ can't return without data, so start procesing next block */
+ goto next_blk;
+ }
+
+ if (res != TINF_OK) {
+ return res;
+ }
+
+ } while (d->dest < d->dest_limit);
+
+ return TINF_OK;
+}
+
+/* inflate next output bytes from compressed stream, updating
+ checksum, and at the end of stream, verify it */
+int uzlib_uncompress_chksum(TINF_DATA *d)
+{
+ int res;
+ unsigned char *data = d->dest;
+
+ res = uzlib_uncompress(d);
+
+ if (res < 0) return res;
+
+ switch (d->checksum_type) {
+
+ case TINF_CHKSUM_ADLER:
+ d->checksum = uzlib_adler32(data, d->dest - data, d->checksum);
+ break;
+
+ case TINF_CHKSUM_CRC:
+ d->checksum = uzlib_crc32(data, d->dest - data, d->checksum);
+ break;
+ }
+
+ if (res == TINF_DONE) {
+ unsigned int val;
+
+ switch (d->checksum_type) {
+
+ case TINF_CHKSUM_ADLER:
+ val = tinf_get_be_uint32(d);
+ if (d->checksum != val) {
+ return TINF_CHKSUM_ERROR;
+ }
+ break;
+
+ case TINF_CHKSUM_CRC:
+ val = tinf_get_le_uint32(d);
+ if (~d->checksum != val) {
+ return TINF_CHKSUM_ERROR;
+ }
+ // Uncompressed size. TODO: Check
+ val = tinf_get_le_uint32(d);
+ break;
+ }
+ }
+
+ return res;
+}
diff --git a/test/monniaux/uzlib/src/tinfzlib.c b/test/monniaux/uzlib/src/tinfzlib.c
new file mode 100644
index 00000000..5cb8852f
--- /dev/null
+++ b/test/monniaux/uzlib/src/tinfzlib.c
@@ -0,0 +1,66 @@
+/*
+ * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#include "tinf.h"
+
+int uzlib_zlib_parse_header(TINF_DATA *d)
+{
+ unsigned char cmf, flg;
+
+ /* -- get header bytes -- */
+
+ cmf = uzlib_get_byte(d);
+ flg = uzlib_get_byte(d);
+
+ /* -- check format -- */
+
+ /* check checksum */
+ if ((256*cmf + flg) % 31) return TINF_DATA_ERROR;
+
+ /* check method is deflate */
+ if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR;
+
+ /* check window size is valid */
+ if ((cmf >> 4) > 7) return TINF_DATA_ERROR;
+
+ /* check there is no preset dictionary */
+ if (flg & 0x20) return TINF_DATA_ERROR;
+
+ /* initialize for adler32 checksum */
+ d->checksum_type = TINF_CHKSUM_ADLER;
+ d->checksum = 1;
+
+ return cmf >> 4;
+}
diff --git a/test/monniaux/uzlib/src/uzlib.h b/test/monniaux/uzlib/src/uzlib.h
new file mode 100644
index 00000000..3a4a1ad1
--- /dev/null
+++ b/test/monniaux/uzlib/src/uzlib.h
@@ -0,0 +1,169 @@
+/*
+ * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ * http://www.ibsensoftware.com/
+ *
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty. In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ * misrepresented; you must not claim that you
+ * wrote the original software. If you use this
+ * software in a product, an acknowledgment in
+ * the product documentation would be appreciated
+ * but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ * as such, and must not be misrepresented as
+ * being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ * any source distribution.
+ */
+
+#ifndef UZLIB_H_INCLUDED
+#define UZLIB_H_INCLUDED
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "defl_static.h"
+
+#include "uzlib_conf.h"
+#if UZLIB_CONF_DEBUG_LOG
+#include <stdio.h>
+#endif
+
+/* calling convention */
+#ifndef TINFCC
+ #ifdef __WATCOMC__
+ #define TINFCC __cdecl
+ #else
+ #define TINFCC
+ #endif
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* ok status, more data produced */
+#define TINF_OK 0
+/* end of compressed stream reached */
+#define TINF_DONE 1
+#define TINF_DATA_ERROR (-3)
+#define TINF_CHKSUM_ERROR (-4)
+#define TINF_DICT_ERROR (-5)
+
+/* checksum types */
+#define TINF_CHKSUM_NONE 0
+#define TINF_CHKSUM_ADLER 1
+#define TINF_CHKSUM_CRC 2
+
+/* helper macros */
+#define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
+
+/* data structures */
+
+typedef struct {
+ unsigned short table[16]; /* table of code length counts */
+ unsigned short trans[288]; /* code -> symbol translation table */
+} TINF_TREE;
+
+struct uzlib_uncomp {
+ /* Pointer to the next byte in the input buffer */
+ const unsigned char *source;
+ /* Pointer to the next byte past the input buffer (source_limit = source + len) */
+ const unsigned char *source_limit;
+ /* If source_limit == NULL, or source >= source_limit, this function
+ will be used to read next byte from source stream. The function may
+ also return -1 in case of EOF (or irrecoverable error). Note that
+ besides returning the next byte, it may also update source and
+ source_limit fields, thus allowing for buffered operation. */
+ int (*source_read_cb)(struct uzlib_uncomp *uncomp);
+
+ unsigned int tag;
+ unsigned int bitcount;
+
+ /* Destination (output) buffer start */
+ unsigned char *dest_start;
+ /* Current pointer in dest buffer */
+ unsigned char *dest;
+ /* Pointer past the end of the dest buffer, similar to source_limit */
+ unsigned char *dest_limit;
+
+ /* Accumulating checksum */
+ unsigned int checksum;
+ char checksum_type;
+ bool eof;
+
+ int btype;
+ int bfinal;
+ unsigned int curlen;
+ int lzOff;
+ unsigned char *dict_ring;
+ unsigned int dict_size;
+ unsigned int dict_idx;
+
+ TINF_TREE ltree; /* dynamic length/symbol tree */
+ TINF_TREE dtree; /* dynamic distance tree */
+};
+
+#include "tinf_compat.h"
+
+#define TINF_PUT(d, c) \
+ { \
+ *d->dest++ = c; \
+ if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \
+ }
+
+unsigned char TINFCC uzlib_get_byte(TINF_DATA *d);
+
+/* Decompression API */
+
+void TINFCC uzlib_init(void);
+void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen);
+int TINFCC uzlib_uncompress(TINF_DATA *d);
+int TINFCC uzlib_uncompress_chksum(TINF_DATA *d);
+
+int TINFCC uzlib_zlib_parse_header(TINF_DATA *d);
+int TINFCC uzlib_gzip_parse_header(TINF_DATA *d);
+
+/* Compression API */
+
+typedef const uint8_t *uzlib_hash_entry_t;
+
+struct uzlib_comp {
+ struct Outbuf out;
+
+ uzlib_hash_entry_t *hash_table;
+ unsigned int hash_bits;
+ unsigned int dict_size;
+};
+
+void TINFCC uzlib_compress(struct uzlib_comp *c, const uint8_t *src, unsigned slen);
+
+/* Checksum API */
+
+/* prev_sum is previous value for incremental computation, 1 initially */
+uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum);
+/* crc is previous value for incremental computation, 0xffffffff initially */
+uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UZLIB_H_INCLUDED */
diff --git a/test/monniaux/uzlib/src/uzlib_conf.h b/test/monniaux/uzlib/src/uzlib_conf.h
new file mode 100644
index 00000000..d6c94071
--- /dev/null
+++ b/test/monniaux/uzlib/src/uzlib_conf.h
@@ -0,0 +1,22 @@
+/*
+ * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
+ *
+ * Copyright (c) 2014-2018 by Paul Sokolovsky
+ */
+
+#ifndef UZLIB_CONF_H_INCLUDED
+#define UZLIB_CONF_H_INCLUDED
+
+#ifndef UZLIB_CONF_DEBUG_LOG
+/* Debug logging level 0, 1, 2, etc. */
+#define UZLIB_CONF_DEBUG_LOG 0
+#endif
+
+#ifndef UZLIB_CONF_PARANOID_CHECKS
+/* Perform extra checks on the input stream, even if they aren't proven
+ to be strictly required (== lack of them wasn't proven to lead to
+ crashes). */
+#define UZLIB_CONF_PARANOID_CHECKS 0
+#endif
+
+#endif /* UZLIB_CONF_H_INCLUDED */
diff --git a/test/monniaux/uzlib/tests/Makefile b/test/monniaux/uzlib/tests/Makefile
new file mode 100644
index 00000000..ce938e0d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/Makefile
@@ -0,0 +1,12 @@
+# Very basic smoke test for decompressor
+test:
+ $(MAKE) -C ../src
+ $(MAKE) -C ../examples/tgunzip
+ $(MAKE) -C ../examples/tgzip
+ ../examples/tgunzip/tgunzip corpus.tar.gz corpus-out.tar
+ md5sum -c corpus.md5sum
+
+clean:
+ $(MAKE) -C ../src $@
+ $(MAKE) -C ../examples/tgzip $@
+ $(MAKE) -C ../examples/tgunzip $@
diff --git a/test/monniaux/uzlib/tests/clang-ubsan-build.sh b/test/monniaux/uzlib/tests/clang-ubsan-build.sh
new file mode 100755
index 00000000..b478f3dc
--- /dev/null
+++ b/test/monniaux/uzlib/tests/clang-ubsan-build.sh
@@ -0,0 +1,5 @@
+# Build with Clang UBSan to catch any runtime issues.
+# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
+
+make clean
+make CC="clang -fsanitize=undefined -fno-sanitize-recover=undefined"
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs.ref b/test/monniaux/uzlib/tests/decomp-bad-inputs.ref
new file mode 100644
index 00000000..ebba94bf
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs.ref
@@ -0,0 +1,232 @@
+decomp-bad-inputs/00/id:000000,sig:11,src:000000,op:flip1,pos:10 3
+decomp-bad-inputs/00/id:000001,sig:11,src:000000,op:flip1,pos:10 3
+decomp-bad-inputs/00/id:000002,sig:11,src:000000,op:flip1,pos:10 3
+decomp-bad-inputs/00/id:000003,sig:11,src:000000,op:flip1,pos:12 3
+decomp-bad-inputs/00/id:000004,sig:11,src:000000,op:flip1,pos:12 3
+decomp-bad-inputs/00/id:000005,sig:11,src:000000,op:flip1,pos:12 3
+decomp-bad-inputs/00/id:000006,sig:11,src:000000,op:flip1,pos:12 3
+decomp-bad-inputs/00/id:000007,sig:11,src:000000,op:flip2,pos:11 3
+decomp-bad-inputs/00/id:000008,sig:11,src:000000,op:flip2,pos:12 3
+decomp-bad-inputs/00/id:000009,sig:11,src:000000,op:flip2,pos:12 3
+decomp-bad-inputs/00/id:000010,sig:11,src:000000,op:flip4,pos:10 3
+decomp-bad-inputs/00/id:000011,sig:11,src:000000,op:flip8,pos:10 3
+decomp-bad-inputs/00/id:000012,sig:11,src:000000,op:arith8,pos:11,val:+6 3
+decomp-bad-inputs/00/id:000013,sig:11,src:000000,op:arith8,pos:12,val:-9 3
+decomp-bad-inputs/00/id:000014,sig:11,src:000000,op:havoc,rep:16 3
+decomp-bad-inputs/00/id:000015,sig:11,src:000000,op:havoc,rep:2 3
+decomp-bad-inputs/00/id:000016,sig:11,src:000000,op:havoc,rep:4 3
+decomp-bad-inputs/00/id:000017,sig:11,src:000000,op:havoc,rep:4 3
+decomp-bad-inputs/00/id:000018,sig:11,src:000000,op:havoc,rep:2 3
+decomp-bad-inputs/00/id:000019,sig:11,src:000000,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000020,sig:11,src:000000,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000021,sig:11,src:000000,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000022,sig:11,src:000000,op:havoc,rep:4 3
+decomp-bad-inputs/00/id:000023,sig:06,src:000000,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000024,sig:06,src:000000,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000025,sig:06,src:000033,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000026,sig:11,src:000033,op:havoc,rep:2 3
+decomp-bad-inputs/00/id:000027,sig:11,src:000040,op:havoc,rep:2 3
+decomp-bad-inputs/00/id:000028,sig:11,src:000070,op:havoc,rep:2 3
+decomp-bad-inputs/00/id:000029,sig:11,src:000070,op:havoc,rep:8 3
+decomp-bad-inputs/00/id:000030,sig:11,src:000070,op:havoc,rep:16 3
+decomp-bad-inputs/00/id:000031,sig:11,src:000090,op:havoc,rep:4 3
+decomp-bad-inputs/00/id:000032,sig:11,src:000096,op:flip1,pos:10 3
+decomp-bad-inputs/00/id:000033,sig:11,src:000096,op:int16,pos:14,val:+1 3
+decomp-bad-inputs/00/id:000034,sig:11,src:000104,op:int32,pos:16,val:-32768 3
+decomp-bad-inputs/00/id:000035,sig:11,src:000118,op:flip1,pos:58 3
+decomp-bad-inputs/00/id:000036,sig:11,src:000187,op:flip1,pos:10 3
+decomp-bad-inputs/00/id:000037,sig:11,src:000187,op:flip1,pos:12 3
+decomp-bad-inputs/00/id:000038,sig:11,src:000187,op:flip1,pos:15 3
+decomp-bad-inputs/01/id:000000,sig:11,src:000001,op:flip1,pos:3 3
+decomp-bad-inputs/01/id:000001,sig:11,src:000001,op:flip1,pos:3 1
+decomp-bad-inputs/01/id:000002,sig:11,src:000001,op:flip2,pos:10 3
+decomp-bad-inputs/01/id:000003,sig:11,src:000001,op:flip4,pos:10 3
+decomp-bad-inputs/01/id:000004,sig:11,src:000001,op:flip4,pos:12 3
+decomp-bad-inputs/01/id:000005,sig:11,src:000001,op:flip16,pos:10 3
+decomp-bad-inputs/01/id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21 3
+decomp-bad-inputs/01/id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000 3
+decomp-bad-inputs/01/id:000008,sig:11,src:000001,op:havoc,rep:32 1
+decomp-bad-inputs/01/id:000009,sig:11,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/01/id:000010,sig:07,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/01/id:000011,sig:11,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/01/id:000012,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000013,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000014,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000015,sig:11,src:000001,op:havoc,rep:32 3
+decomp-bad-inputs/01/id:000016,sig:07,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/01/id:000017,sig:11,src:000001,op:havoc,rep:2 0
+decomp-bad-inputs/01/id:000018,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000019,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000020,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000021,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000022,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000023,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000024,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000025,sig:11,src:000005,op:flip1,pos:22 3
+decomp-bad-inputs/01/id:000026,sig:11,src:000005,op:havoc,rep:16 3
+decomp-bad-inputs/01/id:000027,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/01/id:000028,sig:11,src:000005,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000029,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000030,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000031,sig:11,src:000005,op:havoc,rep:4 3
+decomp-bad-inputs/01/id:000032,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000033,sig:11,src:000005,op:havoc,rep:16 3
+decomp-bad-inputs/01/id:000034,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/id:000035,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000000,sig:11,src:000001,op:flip1,pos:3 3
+decomp-bad-inputs/01/min:id:000002,sig:11,src:000001,op:flip2,pos:10 3
+decomp-bad-inputs/01/min:id:000003,sig:11,src:000001,op:flip4,pos:10 3
+decomp-bad-inputs/01/min:id:000004,sig:11,src:000001,op:flip4,pos:12 3
+decomp-bad-inputs/01/min:id:000005,sig:11,src:000001,op:flip16,pos:10 3
+decomp-bad-inputs/01/min:id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21 3
+decomp-bad-inputs/01/min:id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000 3
+decomp-bad-inputs/01/min:id:000009,sig:11,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/01/min:id:000010,sig:07,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/01/min:id:000011,sig:11,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/01/min:id:000012,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000013,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000014,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000015,sig:11,src:000001,op:havoc,rep:32 3
+decomp-bad-inputs/01/min:id:000016,sig:07,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/01/min:id:000017,sig:11,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/01/min:id:000018,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000019,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000020,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000021,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000022,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000023,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000024,sig:11,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000025,sig:11,src:000005,op:flip1,pos:22 3
+decomp-bad-inputs/01/min:id:000026,sig:11,src:000005,op:havoc,rep:16 3
+decomp-bad-inputs/01/min:id:000027,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/01/min:id:000028,sig:11,src:000005,op:havoc,rep:4 1
+decomp-bad-inputs/01/min:id:000029,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000030,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000031,sig:11,src:000005,op:havoc,rep:4 3
+decomp-bad-inputs/01/min:id:000032,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000033,sig:11,src:000005,op:havoc,rep:16 3
+decomp-bad-inputs/01/min:id:000034,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/01/min:id:000035,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/02/id:000000,sig:11,src:000001,op:havoc,rep:4 4
+decomp-bad-inputs/02/id:000001,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/02/id:000002,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/02/id:000003,sig:11,src:000002,op:havoc,rep:4 3
+decomp-bad-inputs/03/id:000000,sig:11,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/03/id:000001,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/03/id:000002,sig:11,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000003,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/03/id:000004,sig:11,src:000002,op:havoc,rep:4 0
+decomp-bad-inputs/03/id:000005,sig:11,src:000005,op:flip1,pos:20 3
+decomp-bad-inputs/03/id:000006,sig:11,src:000005,op:flip1,pos:22 3
+decomp-bad-inputs/03/id:000007,sig:11,src:000005,op:flip4,pos:24 3
+decomp-bad-inputs/03/id:000008,sig:11,src:000005,op:arith8,pos:3,val:+10 3
+decomp-bad-inputs/03/id:000009,sig:11,src:000005,op:arith8,pos:3,val:+18 3
+decomp-bad-inputs/03/id:000010,sig:11,src:000005,op:int8,pos:23,val:-1 3
+decomp-bad-inputs/03/id:000011,sig:11,src:000005,op:havoc,rep:4 3
+decomp-bad-inputs/03/id:000012,sig:11,src:000005,op:havoc,rep:16 3
+decomp-bad-inputs/03/id:000013,sig:11,src:000005,op:havoc,rep:8 3
+decomp-bad-inputs/03/id:000014,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000015,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000016,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000017,sig:11,src:000005,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000018,sig:11,src:000082,op:flip32,pos:19 3
+decomp-bad-inputs/03/id:000019,sig:11,src:000082,op:int32,pos:19,val:be:+65535 3
+decomp-bad-inputs/03/id:000020,sig:11,src:000155,op:flip2,pos:28 3
+decomp-bad-inputs/03/id:000021,sig:11,src:000166,op:flip2,pos:10 3
+decomp-bad-inputs/03/id:000022,sig:11,src:000156,op:int16,pos:26,val:-129 3
+decomp-bad-inputs/03/id:000023,sig:11,src:000193,op:int16,pos:31,val:be:+32 3
+decomp-bad-inputs/03/id:000024,sig:11,src:000230,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000025,sig:11,src:000230,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000026,sig:11,src:000245,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000027,sig:11,src:000254,op:flip1,pos:32 3
+decomp-bad-inputs/03/id:000028,sig:11,src:000079,op:int32,pos:20,val:-128 3
+decomp-bad-inputs/03/id:000029,sig:11,src:000247,op:havoc,rep:4 3
+decomp-bad-inputs/03/id:000030,sig:11,src:000257,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000031,sig:11,src:000257,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000032,sig:11,src:000248,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000033,sig:11,src:000291,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000034,sig:11,src:000301,op:havoc,rep:64 3
+decomp-bad-inputs/03/id:000035,sig:11,src:000252,op:havoc,rep:2 3
+decomp-bad-inputs/03/id:000036,sig:11,src:000310,op:arith32,pos:20,val:be:-8 3
+decomp-bad-inputs/03/id:000037,sig:11,src:000319+000163,op:splice,rep:2 3
+decomp-bad-inputs/03/id:000038,sig:11,src:000320,op:flip1,pos:27 3
+decomp-bad-inputs/03/id:000039,sig:11,src:000320,op:ext_AO,pos:43 3
+decomp-bad-inputs/03/id:000040,sig:11,src:000326,op:havoc,rep:32 3
+decomp-bad-inputs/03/id:000041,sig:11,src:000016,op:havoc,rep:4 0
+decomp-bad-inputs/03/id:000042,sig:11,src:000205+000322,op:splice,rep:64 3
+decomp-bad-inputs/03/id:000043,sig:11,src:000001+000321,op:splice,rep:4 3
+decomp-bad-inputs/04/id:000000,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/04/id:000001,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/04/id:000002,sig:11,src:000001,op:havoc,rep:2 4
+decomp-bad-inputs/04/id:000003,sig:11,src:000001,op:havoc,rep:4 0
+decomp-bad-inputs/04/id:000004,sig:11,src:000034,op:int32,pos:44,val:+1000 0
+decomp-bad-inputs/04/id:000005,sig:11,src:000063,op:int32,pos:17,val:+1000 0
+decomp-bad-inputs/04/id:000006,sig:11,src:000019+000004,op:splice,rep:2 4
+decomp-bad-inputs/04/id:000007,sig:11,src:000047+000264,op:splice,rep:2 0
+decomp-bad-inputs/04/id:000008,sig:11,src:000194+000266,op:splice,rep:2 0
+decomp-bad-inputs/04/id:000009,sig:11,src:000134+000005,op:splice,rep:2 4
+decomp-bad-inputs/04/id:000010,sig:11,src:000220+000226,op:splice,rep:8 4
+decomp-bad-inputs/05/id:000000,sig:06,src:000001,op:flip8,pos:12 3
+decomp-bad-inputs/05/id:000001,sig:06,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/05/id:000002,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/05/id:000003,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/05/id:000004,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/05/id:000005,sig:06,src:000012,op:flip1,pos:13 3
+decomp-bad-inputs/05/id:000006,sig:06,src:000012,op:havoc,rep:16 3
+decomp-bad-inputs/05/id:000007,sig:06,src:000108,op:arith16,pos:14,val:-2 3
+decomp-bad-inputs/05/id:000008,sig:06,src:000003,op:havoc,rep:4 3
+decomp-bad-inputs/05/id:000009,sig:06,src:000140,op:flip1,pos:15 3
+decomp-bad-inputs/05/id:000010,sig:06,src:000092,op:flip4,pos:3 3
+decomp-bad-inputs/05/id:000011,sig:06,src:000155,op:havoc,rep:8 3
+decomp-bad-inputs/05/id:000012,sig:06,src:000163+000116,op:splice,rep:8 0
+decomp-bad-inputs/05/id:000013,sig:06,src:000171,op:havoc,rep:2 0
+decomp-bad-inputs/05/id:000014,sig:06,src:000170,op:havoc,rep:8 3
+decomp-bad-inputs/06/id:000001,sig:06,src:000001,op:havoc,rep:16 3
+decomp-bad-inputs/06/id:000002,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/06/id:000003,sig:06,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/06/id:000004,sig:06,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/06/id:000005,sig:06,src:000001,op:havoc,rep:4 3
+decomp-bad-inputs/06/id:000007,sig:06,src:000121,op:arith8,pos:12,val:+26 3
+decomp-bad-inputs/06/id:000008,sig:06,src:000137,op:flip1,pos:58 3
+decomp-bad-inputs/07/id:000000,sig:06,src:000001,op:havoc,rep:8 3
+decomp-bad-inputs/07/id:000001,sig:06,src:000019,op:havoc,rep:4 3
+decomp-bad-inputs/07/id:000002,sig:06,src:000021,op:havoc,rep:8 3
+decomp-bad-inputs/08/id:000000,sig:06,src:000157,op:havoc,rep:4 0
+decomp-bad-inputs/09/id:000000,sig:11,src:000001,op:int16,pos:11,val:be:-128 3
+decomp-bad-inputs/09/id:000001,sig:11,src:000001,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000002,sig:11,src:000023,op:flip2,pos:12 3
+decomp-bad-inputs/09/id:000003,sig:11,src:000023,op:arith8,pos:14,val:-28 3
+decomp-bad-inputs/09/id:000004,sig:11,src:000075,op:flip4,pos:61 3
+decomp-bad-inputs/09/id:000005,sig:11,src:000075,op:arith8,pos:57,val:-5 3
+decomp-bad-inputs/09/id:000006,sig:11,src:000075,op:arith16,pos:62,val:+28 3
+decomp-bad-inputs/09/id:000007,sig:11,src:000075,op:havoc,rep:32 3
+decomp-bad-inputs/09/id:000008,sig:11,src:000133,op:arith8,pos:13,val:-34 3
+decomp-bad-inputs/09/id:000009,sig:11,src:000133,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000010,sig:11,src:000133,op:havoc,rep:4 3
+decomp-bad-inputs/09/id:000011,sig:11,src:000151,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000012,sig:11,src:000212,op:arith8,pos:45,val:+9 3
+decomp-bad-inputs/09/id:000013,sig:11,src:000212,op:int32,pos:15,val:+32767 3
+decomp-bad-inputs/09/id:000014,sig:11,src:000212,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000015,sig:11,src:000087,op:arith8,pos:20,val:+11 3
+decomp-bad-inputs/09/id:000016,sig:11,src:000098,op:flip1,pos:13 3
+decomp-bad-inputs/09/id:000017,sig:11,src:000098,op:flip2,pos:13 3
+decomp-bad-inputs/09/id:000018,sig:11,src:000098,op:flip4,pos:12 3
+decomp-bad-inputs/09/id:000019,sig:11,src:000098,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000020,sig:11,src:000133,op:havoc,rep:2 3
+decomp-bad-inputs/09/id:000021,sig:11,src:000134,op:int16,pos:13,val:be:-128 3
+decomp-bad-inputs/09/id:000022,sig:11,src:000218,op:flip2,pos:40 3
+decomp-bad-inputs/09/id:000023,sig:11,src:000218,op:int16,pos:111,val:+0 3
+decomp-bad-inputs/09/id:000025,sig:11,src:000235,op:havoc,rep:8 3
+decomp-bad-inputs/09/id:000026,sig:11,src:000143,op:arith8,pos:19,val:+3 3
+decomp-bad-inputs/09/id:000027,sig:11,src:000024+000141,op:splice,rep:4 3
+decomp-bad-inputs/09/id:000028,sig:11,src:000219,op:arith16,pos:62,val:be:-23 3
+decomp-bad-inputs/09/id:000031,sig:11,src:000256,op:flip2,pos:480 3
+decomp-bad-inputs/09/id:000032,sig:11,src:000256,op:flip4,pos:248 3
+decomp-bad-inputs/09/id:000033,sig:11,src:000256,op:flip4,pos:364 3
+decomp-bad-inputs/09/id:000034,sig:06,src:000256,op:arith8,pos:499,val:+8 3
+decomp-bad-inputs/09/id:000036,sig:11,src:000256+000131,op:splice,rep:8 3
+decomp-bad-inputs/09/id:000037,sig:11,src:000260,op:flip2,pos:264 3
+decomp-bad-inputs/09/id:000038,sig:11,src:000260,op:arith16,pos:254,val:-29 3
+decomp-bad-inputs/09/id:000039,sig:11,src:000260,op:int16,pos:397,val:+0 3
+decomp-bad-inputs/09/id:000040,sig:11,src:000272+000023,op:splice,rep:4 3
+decomp-bad-inputs/09/id:000041,sig:11,src:000253,op:havoc,rep:8 3
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000000,sig:11,src:000000,op:flip1,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000000,sig:11,src:000000,op:flip1,pos:10
new file mode 100644
index 00000000..f844cf11
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000000,sig:11,src:000000,op:flip1,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000001,sig:11,src:000000,op:flip1,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000001,sig:11,src:000000,op:flip1,pos:10
new file mode 100644
index 00000000..14391914
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000001,sig:11,src:000000,op:flip1,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000002,sig:11,src:000000,op:flip1,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000002,sig:11,src:000000,op:flip1,pos:10
new file mode 100644
index 00000000..96422f80
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000002,sig:11,src:000000,op:flip1,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000003,sig:11,src:000000,op:flip1,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000003,sig:11,src:000000,op:flip1,pos:12
new file mode 100644
index 00000000..ee903f14
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000003,sig:11,src:000000,op:flip1,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000004,sig:11,src:000000,op:flip1,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000004,sig:11,src:000000,op:flip1,pos:12
new file mode 100644
index 00000000..4f57966c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000004,sig:11,src:000000,op:flip1,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000005,sig:11,src:000000,op:flip1,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000005,sig:11,src:000000,op:flip1,pos:12
new file mode 100644
index 00000000..11f6ffe8
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000005,sig:11,src:000000,op:flip1,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000006,sig:11,src:000000,op:flip1,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000006,sig:11,src:000000,op:flip1,pos:12
new file mode 100644
index 00000000..0476e1bf
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000006,sig:11,src:000000,op:flip1,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000007,sig:11,src:000000,op:flip2,pos:11 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000007,sig:11,src:000000,op:flip2,pos:11
new file mode 100644
index 00000000..30c2cfa0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000007,sig:11,src:000000,op:flip2,pos:11
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000008,sig:11,src:000000,op:flip2,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000008,sig:11,src:000000,op:flip2,pos:12
new file mode 100644
index 00000000..a76ab4d1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000008,sig:11,src:000000,op:flip2,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000009,sig:11,src:000000,op:flip2,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000009,sig:11,src:000000,op:flip2,pos:12
new file mode 100644
index 00000000..d9aba6a4
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000009,sig:11,src:000000,op:flip2,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000010,sig:11,src:000000,op:flip4,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000010,sig:11,src:000000,op:flip4,pos:10
new file mode 100644
index 00000000..828f3d0c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000010,sig:11,src:000000,op:flip4,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000011,sig:11,src:000000,op:flip8,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000011,sig:11,src:000000,op:flip8,pos:10
new file mode 100644
index 00000000..34178410
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000011,sig:11,src:000000,op:flip8,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000012,sig:11,src:000000,op:arith8,pos:11,val:+6 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000012,sig:11,src:000000,op:arith8,pos:11,val:+6
new file mode 100644
index 00000000..6408096e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000012,sig:11,src:000000,op:arith8,pos:11,val:+6
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000013,sig:11,src:000000,op:arith8,pos:12,val:-9 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000013,sig:11,src:000000,op:arith8,pos:12,val:-9
new file mode 100644
index 00000000..d56d4d80
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000013,sig:11,src:000000,op:arith8,pos:12,val:-9
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000014,sig:11,src:000000,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000014,sig:11,src:000000,op:havoc,rep:16
new file mode 100644
index 00000000..6691f45c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000014,sig:11,src:000000,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000015,sig:11,src:000000,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000015,sig:11,src:000000,op:havoc,rep:2
new file mode 100644
index 00000000..36403d32
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000015,sig:11,src:000000,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000016,sig:11,src:000000,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000016,sig:11,src:000000,op:havoc,rep:4
new file mode 100644
index 00000000..3a28b830
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000016,sig:11,src:000000,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000017,sig:11,src:000000,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000017,sig:11,src:000000,op:havoc,rep:4
new file mode 100644
index 00000000..5fa11582
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000017,sig:11,src:000000,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000018,sig:11,src:000000,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000018,sig:11,src:000000,op:havoc,rep:2
new file mode 100644
index 00000000..1d1bf9bc
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000018,sig:11,src:000000,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000019,sig:11,src:000000,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000019,sig:11,src:000000,op:havoc,rep:8
new file mode 100644
index 00000000..f50b3f11
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000019,sig:11,src:000000,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000020,sig:11,src:000000,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000020,sig:11,src:000000,op:havoc,rep:8
new file mode 100644
index 00000000..e1bf78a0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000020,sig:11,src:000000,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000021,sig:11,src:000000,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000021,sig:11,src:000000,op:havoc,rep:8
new file mode 100644
index 00000000..c251aa33
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000021,sig:11,src:000000,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000022,sig:11,src:000000,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000022,sig:11,src:000000,op:havoc,rep:4
new file mode 100644
index 00000000..b9e41c9d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000022,sig:11,src:000000,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000023,sig:06,src:000000,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000023,sig:06,src:000000,op:havoc,rep:8
new file mode 100644
index 00000000..97222b72
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000023,sig:06,src:000000,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000024,sig:06,src:000000,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000024,sig:06,src:000000,op:havoc,rep:8
new file mode 100644
index 00000000..a4a43e3e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000024,sig:06,src:000000,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000025,sig:06,src:000033,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000025,sig:06,src:000033,op:havoc,rep:8
new file mode 100644
index 00000000..d03a3cac
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000025,sig:06,src:000033,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000026,sig:11,src:000033,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000026,sig:11,src:000033,op:havoc,rep:2
new file mode 100644
index 00000000..c6abd87f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000026,sig:11,src:000033,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000027,sig:11,src:000040,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000027,sig:11,src:000040,op:havoc,rep:2
new file mode 100644
index 00000000..50e23622
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000027,sig:11,src:000040,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000028,sig:11,src:000070,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000028,sig:11,src:000070,op:havoc,rep:2
new file mode 100644
index 00000000..e8eeb08e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000028,sig:11,src:000070,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000029,sig:11,src:000070,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000029,sig:11,src:000070,op:havoc,rep:8
new file mode 100644
index 00000000..8643a548
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000029,sig:11,src:000070,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000030,sig:11,src:000070,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000030,sig:11,src:000070,op:havoc,rep:16
new file mode 100644
index 00000000..4bcba51e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000030,sig:11,src:000070,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000031,sig:11,src:000090,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000031,sig:11,src:000090,op:havoc,rep:4
new file mode 100644
index 00000000..4c1838cd
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000031,sig:11,src:000090,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000032,sig:11,src:000096,op:flip1,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000032,sig:11,src:000096,op:flip1,pos:10
new file mode 100644
index 00000000..218cc128
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000032,sig:11,src:000096,op:flip1,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000033,sig:11,src:000096,op:int16,pos:14,val:+1 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000033,sig:11,src:000096,op:int16,pos:14,val:+1
new file mode 100644
index 00000000..2db1448a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000033,sig:11,src:000096,op:int16,pos:14,val:+1
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000034,sig:11,src:000104,op:int32,pos:16,val:-32768 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000034,sig:11,src:000104,op:int32,pos:16,val:-32768
new file mode 100644
index 00000000..e609ae4c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000034,sig:11,src:000104,op:int32,pos:16,val:-32768
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000035,sig:11,src:000118,op:flip1,pos:58 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000035,sig:11,src:000118,op:flip1,pos:58
new file mode 100644
index 00000000..e168734a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000035,sig:11,src:000118,op:flip1,pos:58
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000036,sig:11,src:000187,op:flip1,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000036,sig:11,src:000187,op:flip1,pos:10
new file mode 100644
index 00000000..379667f0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000036,sig:11,src:000187,op:flip1,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000037,sig:11,src:000187,op:flip1,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000037,sig:11,src:000187,op:flip1,pos:12
new file mode 100644
index 00000000..c450a8a3
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000037,sig:11,src:000187,op:flip1,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000038,sig:11,src:000187,op:flip1,pos:15 b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000038,sig:11,src:000187,op:flip1,pos:15
new file mode 100644
index 00000000..9883ac3c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/00/id:000038,sig:11,src:000187,op:flip1,pos:15
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000000,sig:11,src:000001,op:flip1,pos:3 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000000,sig:11,src:000001,op:flip1,pos:3
new file mode 100644
index 00000000..82b1a58d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000000,sig:11,src:000001,op:flip1,pos:3
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000001,sig:11,src:000001,op:flip1,pos:3 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000001,sig:11,src:000001,op:flip1,pos:3
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000001,sig:11,src:000001,op:flip1,pos:3
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000002,sig:11,src:000001,op:flip2,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000002,sig:11,src:000001,op:flip2,pos:10
new file mode 100644
index 00000000..0b1f6f11
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000002,sig:11,src:000001,op:flip2,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000003,sig:11,src:000001,op:flip4,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000003,sig:11,src:000001,op:flip4,pos:10
new file mode 100644
index 00000000..91eccbda
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000003,sig:11,src:000001,op:flip4,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000004,sig:11,src:000001,op:flip4,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000004,sig:11,src:000001,op:flip4,pos:12
new file mode 100644
index 00000000..b8d670cc
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000004,sig:11,src:000001,op:flip4,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000005,sig:11,src:000001,op:flip16,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000005,sig:11,src:000001,op:flip16,pos:10
new file mode 100644
index 00000000..981ebb95
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000005,sig:11,src:000001,op:flip16,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21
new file mode 100644
index 00000000..39f9f657
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000
new file mode 100644
index 00000000..0592cbc4
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000008,sig:11,src:000001,op:havoc,rep:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000008,sig:11,src:000001,op:havoc,rep:32
new file mode 100644
index 00000000..835a5816
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000008,sig:11,src:000001,op:havoc,rep:32
@@ -0,0 +1 @@
+î \ No newline at end of file
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000009,sig:11,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000009,sig:11,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..737a9788
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000009,sig:11,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000010,sig:07,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000010,sig:07,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..dc6dde68
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000010,sig:07,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000011,sig:11,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000011,sig:11,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..c4e7cf09
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000011,sig:11,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000012,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000012,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..6f6dfe16
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000012,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000013,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000013,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..0272ae91
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000013,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000014,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000014,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..323c9ba5
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000014,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000015,sig:11,src:000001,op:havoc,rep:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000015,sig:11,src:000001,op:havoc,rep:32
new file mode 100644
index 00000000..470c36bf
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000015,sig:11,src:000001,op:havoc,rep:32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000016,sig:07,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000016,sig:07,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..240bc7a3
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000016,sig:07,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000017,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000017,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..5f6d092d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000017,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000018,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000018,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..dc517ab3
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000018,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000019,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000019,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..3f9dbad9
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000019,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000020,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000020,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..0ecee308
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000020,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000021,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000021,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..afac42ff
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000021,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000022,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000022,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..8a0fd41c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000022,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000023,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000023,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..266d32f3
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000023,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000024,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000024,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..930f0ab9
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000024,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000025,sig:11,src:000005,op:flip1,pos:22 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000025,sig:11,src:000005,op:flip1,pos:22
new file mode 100644
index 00000000..f9b00a57
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000025,sig:11,src:000005,op:flip1,pos:22
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000026,sig:11,src:000005,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000026,sig:11,src:000005,op:havoc,rep:16
new file mode 100644
index 00000000..8f15b3b8
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000026,sig:11,src:000005,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000027,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000027,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..c84acd1d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000027,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000028,sig:11,src:000005,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000028,sig:11,src:000005,op:havoc,rep:4
new file mode 100644
index 00000000..da47546f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000028,sig:11,src:000005,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000029,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000029,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..f612d56d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000029,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000030,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000030,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..af3240ff
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000030,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000031,sig:11,src:000005,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000031,sig:11,src:000005,op:havoc,rep:4
new file mode 100644
index 00000000..a1b2af4c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000031,sig:11,src:000005,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000032,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000032,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..2e5a66ec
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000032,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000033,sig:11,src:000005,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000033,sig:11,src:000005,op:havoc,rep:16
new file mode 100644
index 00000000..70c4c059
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000033,sig:11,src:000005,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000034,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000034,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..8c42b38a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000034,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000035,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000035,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..734f065f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/id:000035,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000000,sig:11,src:000001,op:flip1,pos:3 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000000,sig:11,src:000001,op:flip1,pos:3
new file mode 100644
index 00000000..e6ab0798
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000000,sig:11,src:000001,op:flip1,pos:3
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000002,sig:11,src:000001,op:flip2,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000002,sig:11,src:000001,op:flip2,pos:10
new file mode 100644
index 00000000..0eec3af5
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000002,sig:11,src:000001,op:flip2,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000003,sig:11,src:000001,op:flip4,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000003,sig:11,src:000001,op:flip4,pos:10
new file mode 100644
index 00000000..2e70b6ee
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000003,sig:11,src:000001,op:flip4,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000004,sig:11,src:000001,op:flip4,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000004,sig:11,src:000001,op:flip4,pos:12
new file mode 100644
index 00000000..d0e41a5e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000004,sig:11,src:000001,op:flip4,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000005,sig:11,src:000001,op:flip16,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000005,sig:11,src:000001,op:flip16,pos:10
new file mode 100644
index 00000000..8f9cfef1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000005,sig:11,src:000001,op:flip16,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21
new file mode 100644
index 00000000..dd622d5c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000006,sig:11,src:000001,op:arith8,pos:12,val:-21
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000
new file mode 100644
index 00000000..f576d9f1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000007,sig:11,src:000001,op:int16,pos:12,val:be:+1000
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000009,sig:11,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000009,sig:11,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..93c7585f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000009,sig:11,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000010,sig:07,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000010,sig:07,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..4a544d9d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000010,sig:07,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000011,sig:11,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000011,sig:11,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..cc9a4cc1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000011,sig:11,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000012,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000012,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..5c3d4842
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000012,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000013,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000013,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..cb12cc2d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000013,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000014,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000014,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..423b283f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000014,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000015,sig:11,src:000001,op:havoc,rep:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000015,sig:11,src:000001,op:havoc,rep:32
new file mode 100644
index 00000000..ea93de61
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000015,sig:11,src:000001,op:havoc,rep:32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000016,sig:07,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000016,sig:07,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..f677834d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000016,sig:07,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000017,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000017,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..4361e80c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000017,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000018,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000018,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..84c80e30
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000018,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000019,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000019,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..4830ef20
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000019,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000020,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000020,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..8482edee
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000020,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000021,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000021,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..e1c19aca
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000021,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000022,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000022,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..9f119066
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000022,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000023,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000023,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..09aa0509
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000023,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000024,sig:11,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000024,sig:11,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..31239618
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000024,sig:11,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000025,sig:11,src:000005,op:flip1,pos:22 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000025,sig:11,src:000005,op:flip1,pos:22
new file mode 100644
index 00000000..91f2b568
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000025,sig:11,src:000005,op:flip1,pos:22
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000026,sig:11,src:000005,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000026,sig:11,src:000005,op:havoc,rep:16
new file mode 100644
index 00000000..ac40a875
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000026,sig:11,src:000005,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000027,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000027,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..e491f55e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000027,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000028,sig:11,src:000005,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000028,sig:11,src:000005,op:havoc,rep:4
new file mode 100644
index 00000000..d56535ea
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000028,sig:11,src:000005,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000029,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000029,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..1b223ff5
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000029,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000030,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000030,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..c4b62bf4
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000030,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000031,sig:11,src:000005,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000031,sig:11,src:000005,op:havoc,rep:4
new file mode 100644
index 00000000..4c7e2414
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000031,sig:11,src:000005,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000032,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000032,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..a11fc299
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000032,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000033,sig:11,src:000005,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000033,sig:11,src:000005,op:havoc,rep:16
new file mode 100644
index 00000000..e466d709
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000033,sig:11,src:000005,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000034,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000034,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..35934622
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000034,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000035,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000035,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..8469dc91
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/01/min:id:000035,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000000,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000000,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..b27ad3cd
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000000,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000001,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000001,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..814f592b
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000001,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000002,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000002,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..668f2406
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000002,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000003,sig:11,src:000002,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000003,sig:11,src:000002,op:havoc,rep:4
new file mode 100644
index 00000000..738b17ed
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/02/id:000003,sig:11,src:000002,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000000,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000000,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..f80de40a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000000,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000001,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000001,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..e2952fa6
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000001,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000002,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000002,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..c5741eec
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000002,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000003,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000003,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..b46770b7
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000003,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000004,sig:11,src:000002,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000004,sig:11,src:000002,op:havoc,rep:4
new file mode 100644
index 00000000..c8695271
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000004,sig:11,src:000002,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000005,sig:11,src:000005,op:flip1,pos:20 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000005,sig:11,src:000005,op:flip1,pos:20
new file mode 100644
index 00000000..da8c9f9c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000005,sig:11,src:000005,op:flip1,pos:20
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000006,sig:11,src:000005,op:flip1,pos:22 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000006,sig:11,src:000005,op:flip1,pos:22
new file mode 100644
index 00000000..667df759
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000006,sig:11,src:000005,op:flip1,pos:22
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000007,sig:11,src:000005,op:flip4,pos:24 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000007,sig:11,src:000005,op:flip4,pos:24
new file mode 100644
index 00000000..65bdede4
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000007,sig:11,src:000005,op:flip4,pos:24
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000008,sig:11,src:000005,op:arith8,pos:3,val:+10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000008,sig:11,src:000005,op:arith8,pos:3,val:+10
new file mode 100644
index 00000000..47f52f38
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000008,sig:11,src:000005,op:arith8,pos:3,val:+10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000009,sig:11,src:000005,op:arith8,pos:3,val:+18 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000009,sig:11,src:000005,op:arith8,pos:3,val:+18
new file mode 100644
index 00000000..0368ff9a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000009,sig:11,src:000005,op:arith8,pos:3,val:+18
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000010,sig:11,src:000005,op:int8,pos:23,val:-1 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000010,sig:11,src:000005,op:int8,pos:23,val:-1
new file mode 100644
index 00000000..3c37a2ea
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000010,sig:11,src:000005,op:int8,pos:23,val:-1
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000011,sig:11,src:000005,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000011,sig:11,src:000005,op:havoc,rep:4
new file mode 100644
index 00000000..d9924630
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000011,sig:11,src:000005,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000012,sig:11,src:000005,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000012,sig:11,src:000005,op:havoc,rep:16
new file mode 100644
index 00000000..e3ed23d9
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000012,sig:11,src:000005,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000013,sig:11,src:000005,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000013,sig:11,src:000005,op:havoc,rep:8
new file mode 100644
index 00000000..d816c03e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000013,sig:11,src:000005,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000014,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000014,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..be9da1ac
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000014,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000015,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000015,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..a82bc714
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000015,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000016,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000016,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..b9bff3d6
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000016,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000017,sig:11,src:000005,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000017,sig:11,src:000005,op:havoc,rep:2
new file mode 100644
index 00000000..68a96676
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000017,sig:11,src:000005,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000018,sig:11,src:000082,op:flip32,pos:19 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000018,sig:11,src:000082,op:flip32,pos:19
new file mode 100644
index 00000000..57b2844a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000018,sig:11,src:000082,op:flip32,pos:19
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000019,sig:11,src:000082,op:int32,pos:19,val:be:+65535 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000019,sig:11,src:000082,op:int32,pos:19,val:be:+65535
new file mode 100644
index 00000000..ce62db8c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000019,sig:11,src:000082,op:int32,pos:19,val:be:+65535
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000020,sig:11,src:000155,op:flip2,pos:28 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000020,sig:11,src:000155,op:flip2,pos:28
new file mode 100644
index 00000000..78022737
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000020,sig:11,src:000155,op:flip2,pos:28
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000021,sig:11,src:000166,op:flip2,pos:10 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000021,sig:11,src:000166,op:flip2,pos:10
new file mode 100644
index 00000000..842c3bf6
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000021,sig:11,src:000166,op:flip2,pos:10
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000022,sig:11,src:000156,op:int16,pos:26,val:-129 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000022,sig:11,src:000156,op:int16,pos:26,val:-129
new file mode 100644
index 00000000..6980e221
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000022,sig:11,src:000156,op:int16,pos:26,val:-129
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000023,sig:11,src:000193,op:int16,pos:31,val:be:+32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000023,sig:11,src:000193,op:int16,pos:31,val:be:+32
new file mode 100644
index 00000000..78c93522
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000023,sig:11,src:000193,op:int16,pos:31,val:be:+32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000024,sig:11,src:000230,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000024,sig:11,src:000230,op:havoc,rep:2
new file mode 100644
index 00000000..d19b3777
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000024,sig:11,src:000230,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000025,sig:11,src:000230,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000025,sig:11,src:000230,op:havoc,rep:2
new file mode 100644
index 00000000..98a8029b
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000025,sig:11,src:000230,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000026,sig:11,src:000245,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000026,sig:11,src:000245,op:havoc,rep:2
new file mode 100644
index 00000000..86690c14
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000026,sig:11,src:000245,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000027,sig:11,src:000254,op:flip1,pos:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000027,sig:11,src:000254,op:flip1,pos:32
new file mode 100644
index 00000000..f3d1967c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000027,sig:11,src:000254,op:flip1,pos:32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000028,sig:11,src:000079,op:int32,pos:20,val:-128 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000028,sig:11,src:000079,op:int32,pos:20,val:-128
new file mode 100644
index 00000000..81ed3e3d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000028,sig:11,src:000079,op:int32,pos:20,val:-128
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000029,sig:11,src:000247,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000029,sig:11,src:000247,op:havoc,rep:4
new file mode 100644
index 00000000..802dc2c5
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000029,sig:11,src:000247,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000030,sig:11,src:000257,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000030,sig:11,src:000257,op:havoc,rep:2
new file mode 100644
index 00000000..56694840
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000030,sig:11,src:000257,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000031,sig:11,src:000257,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000031,sig:11,src:000257,op:havoc,rep:2
new file mode 100644
index 00000000..d21b8d05
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000031,sig:11,src:000257,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000032,sig:11,src:000248,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000032,sig:11,src:000248,op:havoc,rep:2
new file mode 100644
index 00000000..e75141df
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000032,sig:11,src:000248,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000033,sig:11,src:000291,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000033,sig:11,src:000291,op:havoc,rep:2
new file mode 100644
index 00000000..7cd9223f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000033,sig:11,src:000291,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000034,sig:11,src:000301,op:havoc,rep:64 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000034,sig:11,src:000301,op:havoc,rep:64
new file mode 100644
index 00000000..2279e482
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000034,sig:11,src:000301,op:havoc,rep:64
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000035,sig:11,src:000252,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000035,sig:11,src:000252,op:havoc,rep:2
new file mode 100644
index 00000000..cac51ccd
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000035,sig:11,src:000252,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000036,sig:11,src:000310,op:arith32,pos:20,val:be:-8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000036,sig:11,src:000310,op:arith32,pos:20,val:be:-8
new file mode 100644
index 00000000..f9eae921
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000036,sig:11,src:000310,op:arith32,pos:20,val:be:-8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000037,sig:11,src:000319+000163,op:splice,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000037,sig:11,src:000319+000163,op:splice,rep:2
new file mode 100644
index 00000000..8ddcbe7d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000037,sig:11,src:000319+000163,op:splice,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000038,sig:11,src:000320,op:flip1,pos:27 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000038,sig:11,src:000320,op:flip1,pos:27
new file mode 100644
index 00000000..7e28b816
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000038,sig:11,src:000320,op:flip1,pos:27
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000039,sig:11,src:000320,op:ext_AO,pos:43 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000039,sig:11,src:000320,op:ext_AO,pos:43
new file mode 100644
index 00000000..6275ef11
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000039,sig:11,src:000320,op:ext_AO,pos:43
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000040,sig:11,src:000326,op:havoc,rep:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000040,sig:11,src:000326,op:havoc,rep:32
new file mode 100644
index 00000000..ad741c90
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000040,sig:11,src:000326,op:havoc,rep:32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000041,sig:11,src:000016,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000041,sig:11,src:000016,op:havoc,rep:4
new file mode 100644
index 00000000..954b66be
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000041,sig:11,src:000016,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000042,sig:11,src:000205+000322,op:splice,rep:64 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000042,sig:11,src:000205+000322,op:splice,rep:64
new file mode 100644
index 00000000..0b02e5d0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000042,sig:11,src:000205+000322,op:splice,rep:64
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000043,sig:11,src:000001+000321,op:splice,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000043,sig:11,src:000001+000321,op:splice,rep:4
new file mode 100644
index 00000000..858907c8
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/03/id:000043,sig:11,src:000001+000321,op:splice,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000000,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000000,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..20dd419a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000000,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000001,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000001,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..0b0d7ec0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000001,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000002,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000002,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..2279b9b2
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000002,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000003,sig:11,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000003,sig:11,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..a58c0517
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000003,sig:11,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000004,sig:11,src:000034,op:int32,pos:44,val:+1000 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000004,sig:11,src:000034,op:int32,pos:44,val:+1000
new file mode 100644
index 00000000..397b7d92
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000004,sig:11,src:000034,op:int32,pos:44,val:+1000
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000005,sig:11,src:000063,op:int32,pos:17,val:+1000 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000005,sig:11,src:000063,op:int32,pos:17,val:+1000
new file mode 100644
index 00000000..929bf7f5
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000005,sig:11,src:000063,op:int32,pos:17,val:+1000
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000006,sig:11,src:000019+000004,op:splice,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000006,sig:11,src:000019+000004,op:splice,rep:2
new file mode 100644
index 00000000..16eeb314
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000006,sig:11,src:000019+000004,op:splice,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000007,sig:11,src:000047+000264,op:splice,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000007,sig:11,src:000047+000264,op:splice,rep:2
new file mode 100644
index 00000000..5684a066
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000007,sig:11,src:000047+000264,op:splice,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000008,sig:11,src:000194+000266,op:splice,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000008,sig:11,src:000194+000266,op:splice,rep:2
new file mode 100644
index 00000000..2c9b9080
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000008,sig:11,src:000194+000266,op:splice,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000009,sig:11,src:000134+000005,op:splice,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000009,sig:11,src:000134+000005,op:splice,rep:2
new file mode 100644
index 00000000..f4ce7626
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000009,sig:11,src:000134+000005,op:splice,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000010,sig:11,src:000220+000226,op:splice,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000010,sig:11,src:000220+000226,op:splice,rep:8
new file mode 100644
index 00000000..00801420
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/04/id:000010,sig:11,src:000220+000226,op:splice,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000000,sig:06,src:000001,op:flip8,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000000,sig:06,src:000001,op:flip8,pos:12
new file mode 100644
index 00000000..defcd4aa
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000000,sig:06,src:000001,op:flip8,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000001,sig:06,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000001,sig:06,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..9d491552
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000001,sig:06,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000002,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000002,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..a89debe1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000002,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000003,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000003,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..e6d8d317
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000003,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000004,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000004,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..2bae79b7
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000004,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000005,sig:06,src:000012,op:flip1,pos:13 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000005,sig:06,src:000012,op:flip1,pos:13
new file mode 100644
index 00000000..2703e143
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000005,sig:06,src:000012,op:flip1,pos:13
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000006,sig:06,src:000012,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000006,sig:06,src:000012,op:havoc,rep:16
new file mode 100644
index 00000000..f305fc64
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000006,sig:06,src:000012,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000007,sig:06,src:000108,op:arith16,pos:14,val:-2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000007,sig:06,src:000108,op:arith16,pos:14,val:-2
new file mode 100644
index 00000000..f1d20195
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000007,sig:06,src:000108,op:arith16,pos:14,val:-2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000008,sig:06,src:000003,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000008,sig:06,src:000003,op:havoc,rep:4
new file mode 100644
index 00000000..b7d128c8
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000008,sig:06,src:000003,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000009,sig:06,src:000140,op:flip1,pos:15 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000009,sig:06,src:000140,op:flip1,pos:15
new file mode 100644
index 00000000..93962e4d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000009,sig:06,src:000140,op:flip1,pos:15
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000010,sig:06,src:000092,op:flip4,pos:3 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000010,sig:06,src:000092,op:flip4,pos:3
new file mode 100644
index 00000000..9c27406a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000010,sig:06,src:000092,op:flip4,pos:3
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000011,sig:06,src:000155,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000011,sig:06,src:000155,op:havoc,rep:8
new file mode 100644
index 00000000..8b0c48bc
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000011,sig:06,src:000155,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000012,sig:06,src:000163+000116,op:splice,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000012,sig:06,src:000163+000116,op:splice,rep:8
new file mode 100644
index 00000000..6ff46877
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000012,sig:06,src:000163+000116,op:splice,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000013,sig:06,src:000171,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000013,sig:06,src:000171,op:havoc,rep:2
new file mode 100644
index 00000000..ec0bcb2a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000013,sig:06,src:000171,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000014,sig:06,src:000170,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000014,sig:06,src:000170,op:havoc,rep:8
new file mode 100644
index 00000000..2eda2bea
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/05/id:000014,sig:06,src:000170,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000001,sig:06,src:000001,op:havoc,rep:16 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000001,sig:06,src:000001,op:havoc,rep:16
new file mode 100644
index 00000000..7e1a7324
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000001,sig:06,src:000001,op:havoc,rep:16
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000002,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000002,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..8e99e95c
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000002,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000003,sig:06,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000003,sig:06,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..390a864d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000003,sig:06,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000004,sig:06,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000004,sig:06,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..c463d1b7
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000004,sig:06,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000005,sig:06,src:000001,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000005,sig:06,src:000001,op:havoc,rep:4
new file mode 100644
index 00000000..bf1786fa
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000005,sig:06,src:000001,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000007,sig:06,src:000121,op:arith8,pos:12,val:+26 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000007,sig:06,src:000121,op:arith8,pos:12,val:+26
new file mode 100644
index 00000000..41a83035
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000007,sig:06,src:000121,op:arith8,pos:12,val:+26
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000008,sig:06,src:000137,op:flip1,pos:58 b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000008,sig:06,src:000137,op:flip1,pos:58
new file mode 100644
index 00000000..efbac959
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/06/id:000008,sig:06,src:000137,op:flip1,pos:58
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000000,sig:06,src:000001,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000000,sig:06,src:000001,op:havoc,rep:8
new file mode 100644
index 00000000..81700544
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000000,sig:06,src:000001,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000001,sig:06,src:000019,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000001,sig:06,src:000019,op:havoc,rep:4
new file mode 100644
index 00000000..67995064
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000001,sig:06,src:000019,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000002,sig:06,src:000021,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000002,sig:06,src:000021,op:havoc,rep:8
new file mode 100644
index 00000000..83dc6e7e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/07/id:000002,sig:06,src:000021,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/08/id:000000,sig:06,src:000157,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/08/id:000000,sig:06,src:000157,op:havoc,rep:4
new file mode 100644
index 00000000..2183b4f0
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/08/id:000000,sig:06,src:000157,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000000,sig:11,src:000001,op:int16,pos:11,val:be:-128 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000000,sig:11,src:000001,op:int16,pos:11,val:be:-128
new file mode 100644
index 00000000..05c62003
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000000,sig:11,src:000001,op:int16,pos:11,val:be:-128
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000001,sig:11,src:000001,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000001,sig:11,src:000001,op:havoc,rep:2
new file mode 100644
index 00000000..8d65128f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000001,sig:11,src:000001,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000002,sig:11,src:000023,op:flip2,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000002,sig:11,src:000023,op:flip2,pos:12
new file mode 100644
index 00000000..8a82d1c1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000002,sig:11,src:000023,op:flip2,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000003,sig:11,src:000023,op:arith8,pos:14,val:-28 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000003,sig:11,src:000023,op:arith8,pos:14,val:-28
new file mode 100644
index 00000000..8a0c6ef4
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000003,sig:11,src:000023,op:arith8,pos:14,val:-28
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000004,sig:11,src:000075,op:flip4,pos:61 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000004,sig:11,src:000075,op:flip4,pos:61
new file mode 100644
index 00000000..2ce385ef
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000004,sig:11,src:000075,op:flip4,pos:61
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000005,sig:11,src:000075,op:arith8,pos:57,val:-5 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000005,sig:11,src:000075,op:arith8,pos:57,val:-5
new file mode 100644
index 00000000..1ff3ea51
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000005,sig:11,src:000075,op:arith8,pos:57,val:-5
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000006,sig:11,src:000075,op:arith16,pos:62,val:+28 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000006,sig:11,src:000075,op:arith16,pos:62,val:+28
new file mode 100644
index 00000000..1b1b622f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000006,sig:11,src:000075,op:arith16,pos:62,val:+28
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000007,sig:11,src:000075,op:havoc,rep:32 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000007,sig:11,src:000075,op:havoc,rep:32
new file mode 100644
index 00000000..af339549
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000007,sig:11,src:000075,op:havoc,rep:32
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000008,sig:11,src:000133,op:arith8,pos:13,val:-34 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000008,sig:11,src:000133,op:arith8,pos:13,val:-34
new file mode 100644
index 00000000..59b67d22
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000008,sig:11,src:000133,op:arith8,pos:13,val:-34
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000009,sig:11,src:000133,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000009,sig:11,src:000133,op:havoc,rep:2
new file mode 100644
index 00000000..cfa1a087
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000009,sig:11,src:000133,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000010,sig:11,src:000133,op:havoc,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000010,sig:11,src:000133,op:havoc,rep:4
new file mode 100644
index 00000000..207cef2f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000010,sig:11,src:000133,op:havoc,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000011,sig:11,src:000151,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000011,sig:11,src:000151,op:havoc,rep:2
new file mode 100644
index 00000000..102fd05d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000011,sig:11,src:000151,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000012,sig:11,src:000212,op:arith8,pos:45,val:+9 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000012,sig:11,src:000212,op:arith8,pos:45,val:+9
new file mode 100644
index 00000000..29ec3e8f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000012,sig:11,src:000212,op:arith8,pos:45,val:+9
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000013,sig:11,src:000212,op:int32,pos:15,val:+32767 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000013,sig:11,src:000212,op:int32,pos:15,val:+32767
new file mode 100644
index 00000000..4c9fb140
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000013,sig:11,src:000212,op:int32,pos:15,val:+32767
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000014,sig:11,src:000212,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000014,sig:11,src:000212,op:havoc,rep:2
new file mode 100644
index 00000000..67e5b6c8
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000014,sig:11,src:000212,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000015,sig:11,src:000087,op:arith8,pos:20,val:+11 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000015,sig:11,src:000087,op:arith8,pos:20,val:+11
new file mode 100644
index 00000000..c450bf6f
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000015,sig:11,src:000087,op:arith8,pos:20,val:+11
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000016,sig:11,src:000098,op:flip1,pos:13 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000016,sig:11,src:000098,op:flip1,pos:13
new file mode 100644
index 00000000..32f78bac
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000016,sig:11,src:000098,op:flip1,pos:13
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000017,sig:11,src:000098,op:flip2,pos:13 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000017,sig:11,src:000098,op:flip2,pos:13
new file mode 100644
index 00000000..43a8d73d
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000017,sig:11,src:000098,op:flip2,pos:13
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000018,sig:11,src:000098,op:flip4,pos:12 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000018,sig:11,src:000098,op:flip4,pos:12
new file mode 100644
index 00000000..0d641109
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000018,sig:11,src:000098,op:flip4,pos:12
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000019,sig:11,src:000098,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000019,sig:11,src:000098,op:havoc,rep:2
new file mode 100644
index 00000000..fafd2963
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000019,sig:11,src:000098,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000020,sig:11,src:000133,op:havoc,rep:2 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000020,sig:11,src:000133,op:havoc,rep:2
new file mode 100644
index 00000000..ba4c396a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000020,sig:11,src:000133,op:havoc,rep:2
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000021,sig:11,src:000134,op:int16,pos:13,val:be:-128 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000021,sig:11,src:000134,op:int16,pos:13,val:be:-128
new file mode 100644
index 00000000..18d3adbf
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000021,sig:11,src:000134,op:int16,pos:13,val:be:-128
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000022,sig:11,src:000218,op:flip2,pos:40 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000022,sig:11,src:000218,op:flip2,pos:40
new file mode 100644
index 00000000..793a6b04
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000022,sig:11,src:000218,op:flip2,pos:40
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000023,sig:11,src:000218,op:int16,pos:111,val:+0 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000023,sig:11,src:000218,op:int16,pos:111,val:+0
new file mode 100644
index 00000000..2cff7c32
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000023,sig:11,src:000218,op:int16,pos:111,val:+0
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000025,sig:11,src:000235,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000025,sig:11,src:000235,op:havoc,rep:8
new file mode 100644
index 00000000..74e7a2f1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000025,sig:11,src:000235,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000026,sig:11,src:000143,op:arith8,pos:19,val:+3 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000026,sig:11,src:000143,op:arith8,pos:19,val:+3
new file mode 100644
index 00000000..5bfe3775
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000026,sig:11,src:000143,op:arith8,pos:19,val:+3
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000027,sig:11,src:000024+000141,op:splice,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000027,sig:11,src:000024+000141,op:splice,rep:4
new file mode 100644
index 00000000..340c3f17
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000027,sig:11,src:000024+000141,op:splice,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000028,sig:11,src:000219,op:arith16,pos:62,val:be:-23 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000028,sig:11,src:000219,op:arith16,pos:62,val:be:-23
new file mode 100644
index 00000000..b8a88c9b
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000028,sig:11,src:000219,op:arith16,pos:62,val:be:-23
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000031,sig:11,src:000256,op:flip2,pos:480 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000031,sig:11,src:000256,op:flip2,pos:480
new file mode 100644
index 00000000..916b391a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000031,sig:11,src:000256,op:flip2,pos:480
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000032,sig:11,src:000256,op:flip4,pos:248 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000032,sig:11,src:000256,op:flip4,pos:248
new file mode 100644
index 00000000..5379b0ad
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000032,sig:11,src:000256,op:flip4,pos:248
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000033,sig:11,src:000256,op:flip4,pos:364 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000033,sig:11,src:000256,op:flip4,pos:364
new file mode 100644
index 00000000..79838c13
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000033,sig:11,src:000256,op:flip4,pos:364
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000034,sig:06,src:000256,op:arith8,pos:499,val:+8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000034,sig:06,src:000256,op:arith8,pos:499,val:+8
new file mode 100644
index 00000000..f887616e
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000034,sig:06,src:000256,op:arith8,pos:499,val:+8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000036,sig:11,src:000256+000131,op:splice,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000036,sig:11,src:000256+000131,op:splice,rep:8
new file mode 100644
index 00000000..2e908bd1
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000036,sig:11,src:000256+000131,op:splice,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000037,sig:11,src:000260,op:flip2,pos:264 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000037,sig:11,src:000260,op:flip2,pos:264
new file mode 100644
index 00000000..441aa160
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000037,sig:11,src:000260,op:flip2,pos:264
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000038,sig:11,src:000260,op:arith16,pos:254,val:-29 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000038,sig:11,src:000260,op:arith16,pos:254,val:-29
new file mode 100644
index 00000000..7dea3f10
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000038,sig:11,src:000260,op:arith16,pos:254,val:-29
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000039,sig:11,src:000260,op:int16,pos:397,val:+0 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000039,sig:11,src:000260,op:int16,pos:397,val:+0
new file mode 100644
index 00000000..2150123a
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000039,sig:11,src:000260,op:int16,pos:397,val:+0
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000040,sig:11,src:000272+000023,op:splice,rep:4 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000040,sig:11,src:000272+000023,op:splice,rep:4
new file mode 100644
index 00000000..7b3a68c7
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000040,sig:11,src:000272+000023,op:splice,rep:4
Binary files differ
diff --git a/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000041,sig:11,src:000253,op:havoc,rep:8 b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000041,sig:11,src:000253,op:havoc,rep:8
new file mode 100644
index 00000000..e6147cda
--- /dev/null
+++ b/test/monniaux/uzlib/tests/decomp-bad-inputs/09/id:000041,sig:11,src:000253,op:havoc,rep:8
Binary files differ
diff --git a/test/monniaux/uzlib/tests/run-decomp-bad-inputs.sh b/test/monniaux/uzlib/tests/run-decomp-bad-inputs.sh
new file mode 100755
index 00000000..195bdf04
--- /dev/null
+++ b/test/monniaux/uzlib/tests/run-decomp-bad-inputs.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# Make sure that globbing order below is independent on the current
+# user locale
+export LANG=C.UTF-8
+unset LC_COLLATE
+
+rm -f decomp-bad-inputs.log
+
+for f in decomp-bad-inputs/*/*; do
+ echo "*" $f
+ ../examples/tgunzip/tgunzip "$f" /dev/null
+ echo $f $? >>decomp-bad-inputs.log
+done
+
+echo
+
+if diff -u decomp-bad-inputs.ref decomp-bad-inputs.log; then
+ echo "Test passed"
+else
+ echo "Test FAILED"
+fi
diff --git a/test/monniaux/varargs/Makefile b/test/monniaux/varargs/Makefile
new file mode 100644
index 00000000..938eff30
--- /dev/null
+++ b/test/monniaux/varargs/Makefile
@@ -0,0 +1,9 @@
+include ../rules.mk
+
+all: varargs.ccomp.kvx.s varargs.ccomp.kvx
+
+varargs.ccomp.kvx: varargs.ccomp.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+clean:
+ $(RM) -f *.kvx *.host *.o *.s
diff --git a/test/monniaux/varargs/varargs.c b/test/monniaux/varargs/varargs.c
new file mode 100644
index 00000000..7aa95799
--- /dev/null
+++ b/test/monniaux/varargs/varargs.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+void simple_printf(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ while (*fmt != '\0') {
+ if (*fmt == 'd') {
+ int i = va_arg(args, int);
+ printf("%d\n", i);
+ } else if (*fmt == 'c') {
+ // A 'char' variable will be promoted to 'int'
+ // A character literal in C is already 'int' by itself
+ int c = va_arg(args, int);
+ printf("%c\n", c);
+ } else if (*fmt == 'f') {
+ double d = va_arg(args, double);
+ printf("%f\n", d);
+ } else if (*fmt == 'l') {
+ long d = va_arg(args, long);
+ printf("%ld\n", d);
+ }
+ ++fmt;
+ }
+
+ va_end(args);
+}
+
+int main(void)
+{
+ simple_printf("dlcff", 3, 4L, 'a', 1.999, 42.5);
+}
diff --git a/test/monniaux/vocabulary.sh b/test/monniaux/vocabulary.sh
new file mode 100755
index 00000000..eb3b70b6
--- /dev/null
+++ b/test/monniaux/vocabulary.sh
@@ -0,0 +1,2 @@
+cat *.gcc.kvx.s|cut -f2|cut -d' ' -f1|sort -u|grep -v ':'|grep -v -F '.' > gcc_vocabulary.txt
+cat *.ccomp.kvx.s|cut -f2|cut -d' ' -f1|sort -u|grep -v ':'|grep -v -F '.' > ccomp_vocabulary.txt
diff --git a/test/monniaux/volatile/slow_volatile.c b/test/monniaux/volatile/slow_volatile.c
new file mode 100644
index 00000000..bbbc6ddf
--- /dev/null
+++ b/test/monniaux/volatile/slow_volatile.c
@@ -0,0 +1,14 @@
+#include "../clock.h"
+
+volatile int output;
+
+int main() {
+ clock_prepare();
+ clock_start();
+ for(int i=0; i<1000; i++) {
+ output = 0;
+ }
+ clock_stop();
+ print_total_clock();
+ return 0;
+}
diff --git a/test/monniaux/volatile/volatile_test.c b/test/monniaux/volatile/volatile_test.c
new file mode 100644
index 00000000..687544ec
--- /dev/null
+++ b/test/monniaux/volatile/volatile_test.c
@@ -0,0 +1,33 @@
+#include <stdint.h>
+#include <float.h>
+#include <stdio.h>
+
+#define TEST_TYPE(type, min, max) \
+ { \
+ volatile type var; \
+ var = min; \
+ if (var != min) { \
+ printf("%s: wrong min\n", #type); \
+ } \
+ var = max; \
+ if (var != max) { \
+ printf("%s: wrong max\n", #type); \
+ } \
+ }
+
+#define TEST_INT_TYPE(type, min, max) TEST_TYPE(type##_t, min, max)
+
+int main() {
+ TEST_INT_TYPE(uint8, 0, UINT8_MAX);
+ TEST_INT_TYPE(int8, INT8_MIN, INT8_MAX);
+ TEST_INT_TYPE(uint16, 0, UINT16_MAX);
+ TEST_INT_TYPE(int16, INT16_MIN, INT16_MAX);
+ TEST_INT_TYPE(uint32, 0, UINT32_MAX);
+ TEST_INT_TYPE(int32, INT32_MIN, INT32_MAX);
+ TEST_INT_TYPE(uint64, 0, UINT64_MAX);
+ TEST_INT_TYPE(int64, INT64_MIN, INT64_MAX);
+
+ TEST_TYPE(float, FLT_MIN, FLT_MAX);
+ TEST_TYPE(double, DBL_MIN, DBL_MAX);
+ return 0;
+}
diff --git a/test/monniaux/xor_and_mat/Makefile b/test/monniaux/xor_and_mat/Makefile
new file mode 100644
index 00000000..e6c4db77
--- /dev/null
+++ b/test/monniaux/xor_and_mat/Makefile
@@ -0,0 +1,4 @@
+TARGET=xor_and_mat
+MEASURES="c1 c2 c3 c4 c5 c6 c7"
+
+include ../rules.mk
diff --git a/test/monniaux/xor_and_mat/int_mat.c b/test/monniaux/xor_and_mat/int_mat.c
new file mode 100644
index 00000000..0bfe16b9
--- /dev/null
+++ b/test/monniaux/xor_and_mat/int_mat.c
@@ -0,0 +1,210 @@
+#include "xor_and.h"
+
+#define ADD ^=
+#define MUL &
+
+void xor_and_mat_mul1(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ c[i*stride_c+k] = 0;
+ }
+ }
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ for(unsigned j=0; j<n; j++) {
+ c[i*stride_c+k] ADD (a[i*stride_a+j] MUL b[j*stride_b+k]);
+ }
+ }
+ }
+}
+
+void xor_and_mat_mul2(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ xor_and total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total ADD (a[i*stride_a + j] MUL b[j*stride_b + k]);
+ }
+ c[i*stride_c+k] = total;
+ }
+ }
+}
+
+void xor_and_mat_mul3(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ xor_and total0 = 0, total1 = 0;
+ unsigned j;
+ for(j=0; j+1<n; j+=2) {
+ total0 ADD (a[i*stride_a + j] MUL b[j*stride_b + k]);
+ total1 ADD (a[i*stride_a + (j+1)] MUL b[(j+1)*stride_b + k]);
+ }
+ if (j < n) {
+ total0 ADD a[i*stride_a + j] MUL b[j*stride_b + k];
+ }
+ total0 ADD total1;
+ c[i*stride_c+k] = total0;
+ }
+ }
+}
+
+void xor_and_mat_mul4(unsigned m, unsigned n, unsigned p,
+ xor_and * c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ const xor_and *pa_i = a;
+ xor_and * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const xor_and *pb_j_k = b+k, *pa_i_j = pa_i;
+ xor_and total = 0;
+ for(unsigned j=0; j<n; j++) {
+ total ADD (*pa_i_j MUL *pb_j_k);
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+void xor_and_mat_mul5(unsigned m, unsigned n, unsigned p,
+ xor_and * c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ const xor_and *pa_i = a;
+ xor_and * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const xor_and *pb_j_k = b+k, *pa_i_j = pa_i;
+ xor_and total = 0;
+ for(unsigned j2=0, n2=n/2; j2<n2; j2++) {
+ xor_and p0 = *pa_i_j MUL *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ xor_and p1 = *pa_i_j MUL *pb_j_k;
+ pa_i_j ++;
+ pb_j_k += stride_b;
+ total ADD p0;
+ total ADD p1;
+ }
+ if (n%2) {
+ total ADD *pa_i_j MUL *pb_j_k;
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#define CHUNK \
+ total ADD (*pa_i_j MUL *pb_j_k); \
+ pa_i_j ++; \
+ pb_j_k += stride_b;
+
+void xor_and_mat_mul6(unsigned m, unsigned n, unsigned p,
+ xor_and * c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ const xor_and *pa_i = a;
+ xor_and * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const xor_and *pb_j_k = b+k, *pa_i_j = pa_i;
+ xor_and total = 0;
+ unsigned j2=0, n2=n/2;
+ if (n2 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ j2++;
+ } while (j2 < n2);
+ }
+ if (n%2) {
+ total ADD (*pa_i_j MUL *pb_j_k);
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+#define UNROLL 4
+void xor_and_mat_mul7(unsigned m, unsigned n, unsigned p,
+ xor_and * c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ const xor_and *pa_i = a;
+ xor_and * pc_i = c;
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned k=0; k<p; k++) {
+ const xor_and *pb_j_k = b+k, *pa_i_j = pa_i;
+ xor_and total = 0;
+ {
+ unsigned j4=0, n4=n/UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ CHUNK
+ CHUNK
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ {
+ unsigned j4=0, n4=n%UNROLL;
+ if (n4 > 0) {
+ do {
+ CHUNK
+ j4++;
+ } while (j4 < n4);
+ }
+ }
+ pc_i[k] = total;
+ }
+ pa_i += stride_a;
+ pc_i += stride_c;
+ }
+}
+
+xor_and xor_and_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next;
+}
+
+void xor_and_mat_random(unsigned m,
+ unsigned n,
+ xor_and *a, unsigned stride_a) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ a[i*stride_a + j] = xor_and_random();
+ }
+ }
+}
+
+bool xor_and_mat_equal(unsigned m,
+ unsigned n,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b) {
+ for(unsigned i=0; i<m; i++) {
+ for(unsigned j=0; j<n; j++) {
+ if (a[i*stride_a + j] != b[i*stride_b + j]) return false;
+ }
+ }
+ return true;
+}
diff --git a/test/monniaux/xor_and_mat/int_mat_run.c b/test/monniaux/xor_and_mat/int_mat_run.c
new file mode 100644
index 00000000..a6a821d9
--- /dev/null
+++ b/test/monniaux/xor_and_mat/int_mat_run.c
@@ -0,0 +1,90 @@
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "xor_and.h"
+#include "../clock.h"
+
+int main() {
+ const unsigned m = 60, n = 31, p = 50;
+ clock_prepare();
+ xor_and *a = malloc(sizeof(xor_and) * m * n);
+ xor_and_mat_random(m, n, a, n);
+ xor_and *b = malloc(sizeof(xor_and) * n * p);
+ xor_and_mat_random(n, p, b, p);
+
+ xor_and *c1 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c1_time = get_current_cycle();
+ xor_and_mat_mul1(m, n, p, c1, p, a, n, b, p);
+ c1_time = get_current_cycle()-c1_time;
+
+ xor_and *c2 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c2_time = get_current_cycle();
+ xor_and_mat_mul2(m, n, p, c2, p, a, n, b, p);
+ c2_time = get_current_cycle()-c2_time;
+
+ xor_and *c3 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c3_time = get_current_cycle();
+ xor_and_mat_mul3(m, n, p, c3, p, a, n, b, p);
+ c3_time = get_current_cycle()-c3_time;
+
+ xor_and *c4 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c4_time = get_current_cycle();
+ xor_and_mat_mul4(m, n, p, c4, p, a, n, b, p);
+ c4_time = get_current_cycle()-c4_time;
+
+ xor_and *c5 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c5_time = get_current_cycle();
+ xor_and_mat_mul5(m, n, p, c5, p, a, n, b, p);
+ c5_time = get_current_cycle()-c5_time;
+
+ xor_and *c6 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c6_time = get_current_cycle();
+ xor_and_mat_mul6(m, n, p, c6, p, a, n, b, p);
+ c6_time = get_current_cycle()-c6_time;
+
+ xor_and *c7 = malloc(sizeof(xor_and) * m * p);
+ cycle_t c7_time = get_current_cycle();
+ xor_and_mat_mul7(m, n, p, c7, p, a, n, b, p);
+ c7_time = get_current_cycle()-c7_time;
+
+ printf("c1==c2: %s\n"
+ "c1==c3: %s\n"
+ "c1==c4: %s\n"
+ "c1==c5: %s\n"
+ "c1==c6: %s\n"
+ "c1==c7: %s\n"
+ "c1 cycles: %" PRIu64 "\n"
+ "c2 cycles: %" PRIu64 "\n"
+ "c3 cycles: %" PRIu64 "\n"
+ "c4 cycles: %" PRIu64 "\n"
+ "c5 cycles: %" PRIu64 "\n"
+ "c6 cycles: %" PRIu64 "\n"
+ "c7 cycles: %" PRIu64 "\n",
+
+ xor_and_mat_equal(m, n, c1, p, c2, p)?"true":"false",
+ xor_and_mat_equal(m, n, c1, p, c3, p)?"true":"false",
+ xor_and_mat_equal(m, n, c1, p, c4, p)?"true":"false",
+ xor_and_mat_equal(m, n, c1, p, c5, p)?"true":"false",
+ xor_and_mat_equal(m, n, c1, p, c6, p)?"true":"false",
+ xor_and_mat_equal(m, n, c1, p, c7, p)?"true":"false",
+
+ c1_time,
+ c2_time,
+ c3_time,
+ c4_time,
+ c5_time,
+ c6_time,
+ c7_time);
+
+ free(a);
+ free(b);
+ free(c1);
+ free(c2);
+ free(c3);
+ free(c4);
+ free(c5);
+ free(c6);
+ free(c7);
+ return 0;
+}
diff --git a/test/monniaux/xor_and_mat/xor_and.h b/test/monniaux/xor_and_mat/xor_and.h
new file mode 100644
index 00000000..956ad800
--- /dev/null
+++ b/test/monniaux/xor_and_mat/xor_and.h
@@ -0,0 +1,50 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef uint64_t xor_and;
+
+void xor_and_mat_mul1(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul2(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul3(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul4(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul5(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul6(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+void xor_and_mat_mul7(unsigned m, unsigned n, unsigned p,
+ xor_and * restrict c, unsigned stride_c,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
+
+xor_and xor_and_random(void);
+
+void xor_and_mat_random(unsigned m,
+ unsigned n,
+ xor_and *a, unsigned stride_a);
+
+bool xor_and_mat_equal(unsigned m,
+ unsigned n,
+ const xor_and *a, unsigned stride_a,
+ const xor_and *b, unsigned stride_b);
diff --git a/test/monniaux/yarpgen/Makefile b/test/monniaux/yarpgen/Makefile
new file mode 100644
index 00000000..1a7ce1d0
--- /dev/null
+++ b/test/monniaux/yarpgen/Makefile
@@ -0,0 +1,133 @@
+TARGET_CCOMP=../../../ccomp
+TARGET_CC=gcc
+#EXECUTE=kvx-cluster --
+
+all:
+
+.SECONDARY:
+
+ifndef YARPGEN
+YARPGEN=./yarpgen
+GENERATOR=yarpgen
+endif
+
+ifdef BITS
+YARPGEN+=-m $(BITS)
+CFLAGS+=-m$(BITS)
+endif
+
+MAX=129
+PREFIX=ran%06.f
+
+CCOMPOPTS=-static
+CCOMPFLAGS+= -funprototyped -fbitfields -fno-cse2 -stdlib ../../../runtime
+
+TESTS_C=$(shell seq --format $(PREFIX)/func.c 1 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.c 1 $(MAX)) \
+ $(shell seq --format $(PREFIX)/init.c 1 $(MAX)) \
+ $(shell seq --format $(PREFIX)/hash.c 1 $(MAX)) \
+ $(shell seq --format $(PREFIX)/check.c 1 $(MAX)) \
+ $(shell seq --format $(PREFIX)/init.h 1 $(MAX))
+
+$(TESTS_C): $(GENERATOR)
+
+TESTS_CCOMP_TARGET_S=$(TEST_C:.c=.ccomp.target.s)
+TESTS_GCC_TARGET_S=$(TEST_C:.c=.gcc.target.s)
+TESTS_GCC_HOST_S=$(TEST_C:.c=.gcc.host.s)
+TESTS_GCC_O3_TARGET_S=$(TEST_C:.c=.gcc_O3.target.s)
+TESTS_GCC_O3_HOST_S=$(TEST_C:.c=.gcc_O3.host.s)
+TESTS_CCOMP_TARGET_OUT=$(shell seq --format $(PREFIX)/example.ccomp.target.out 1 $(MAX))
+TESTS_GCC_TARGET_OUT=$(shell seq --format $(PREFIX)/example.gcc.target.out 1 $(MAX))
+TESTS_GCC_O3_TARGET_OUT=$(shell seq --format $(PREFIX)/example.gcc_O3.target.out 1 $(MAX))
+TESTS_GCC_HOST_OUT=$(shell seq --format $(PREFIX)/example.gcc.host.out 1 $(MAX))
+TESTS_GCC_O3_HOST_OUT=$(shell seq --format $(PREFIX)/example.gcc.host.out 1 $(MAX))
+TESTS_CMP=$(shell seq --format $(PREFIX)/example.target.cmp 1 $(MAX))
+TESTS_CMP_TARGET_HOST=$(shell seq --format $(PREFIX)/example.host_target.cmp 1 $(MAX))
+TESTS_CMP_O3=$(shell seq --format $(PREFIX)/example.target.cmp_O3 1 $(MAX))
+
+# FIXME - test000089 fails in CI in arm and armhf because of memory consumption during register allocation being too high
+# Removing it from the pool
+BADID:=89
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/func.c $(BADID) $(BADID)),$(TESTS_C))
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/driver.c $(BADID) $(BADID)),$(TESTS_C))
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/init.c $(BADID) $(BADID)),$(TESTS_C))
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/hash.c $(BADID) $(BADID)),$(TESTS_C))
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/check.c $(BADID) $(BADID)),$(TESTS_C))
+TESTS_C:=$(filter-out $(shell seq --format $(PREFIX)/init.h $(BADID) $(BADID)),$(TESTS_C))
+TESTS_CMP:=$(filter-out $(shell seq --format $(PREFIX)/example.target.cmp $(BADID) $(BADID)),$(TESTS_CMP))
+TESTS_GCC_HOST_OUT:=$(filter-out $(shell seq --format $(PREFIX)/example.gcc.host.out $(BADID) $(BADID)),$(TESTS_GCC_HOST_OUT))
+TESTS_CCOMP_TARGET_OUT:=$(filter-out $(shell seq --format $(PREFIX)/example.ccomp.target.out $(BADID) $(BADID)),$(TESTS_CCOMP_TARGET_OUT))
+TESTS_GCC_TARGET_OUT:=$(filter-out $(shell seq --format $(PREFIX)/example.gcc.target.out $(BADID) $(BADID)),$(TESTS_GCC_TARGET_OUT))
+TESTS_GCC_O3_TARGET_OUT:=$(filter-out $(shell seq --format $(PREFIX)/example.gcc.target.out $(BADID) $(BADID)),$(TESTS_GCC_O3_TARGET_OUT))
+
+all: $(TESTS_CCOMP_TARGET_OUT) $(TESTS_GCC_TARGET_OUT) $(TESTS_CCOMP_TARGET_S) $(TESTS_GCC_TARGET_S) $(TESTS_CMP) $(TESTS_C)
+
+tests_c: $(TESTS_C)
+
+tests_s: $(TESTS_CCOMP_TARGET_S)
+
+tests_O3: $(TESTS_CMP_O3)
+
+%.ccomp.target.s : %.c
+ $(TARGET_CCOMP) $(CCOMPOPTS) $(CCOMPFLAGS) -S -o $@ $<
+
+%.gcc.target.s : %.c
+ $(TARGET_CC) $(CCOMPOPTS) -S -o $@ $<
+
+%.gcc_O3.target.s : %.c
+ $(TARGET_CC) $(CCOMPOPTS) -O3 -S -o $@ $<
+
+%.gcc.host.s : %.c
+ $(CC) $(CFLAGS) -S -o $@ $<
+
+%.target.o : %.target.s
+ $(TARGET_CC) -c -o $@ $<
+
+%.host.o : %.host.s
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+%.target.out : %.target
+ $(EXECUTE) $< | tee $@
+
+%.host.out : %.host
+ ./$< | tee $@
+
+ran%/func.ccomp.target.s ran%/func.gcc.target.s ran%/func.ccomp.host.s ran%/func.gcc.host.s ran%/init.gcc.host.s : ran%/init.h
+
+ran%/func.gcc_O3.target.s ran%/func.gcc_O3.host.s ran%/init.gcc_O3.host.s : ran%/init.h
+
+ran%/example.ccomp.target: ran%/func.ccomp.target.o ran%/driver.ccomp.target.o ran%/init.ccomp.target.o ran%/check.ccomp.target.o ran%/hash.ccomp.target.o
+ $(TARGET_CCOMP) $(CCOMPOPTS) $(CCOMPFLAGS) $+ -o $@
+
+ran%/example.gcc.target: ran%/func.gcc.target.o ran%/driver.gcc.target.o ran%/init.gcc.target.o ran%/check.gcc.target.o ran%/hash.gcc.target.o
+ $(TARGET_CC) $(TARGET_CFLAGS) $+ -o $@
+
+ran%/example.gcc_O3.target: ran%/func.gcc_O3.target.o ran%/driver.gcc_O3.target.o ran%/init.gcc_O3.target.o ran%/check.gcc_O3.target.o ran%/hash.gcc_O3.target.o
+ $(TARGET_CC) $(TARGET_CFLAGS) $+ -o $@
+
+ran%/example.gcc.host: ran%/func.gcc.host.o ran%/driver.gcc.host.o ran%/init.gcc.host.o ran%/check.gcc.host.o ran%/hash.gcc.host.o
+ $(CC) $(CFLAGS) $+ -o $@
+
+ran%/driver.c ran%/func.c ran%/init.c ran%/check.c ran%/hash.c ran%/init.h:
+ mkdir -p ran$*
+ $(YARPGEN) --seed=$* --out-dir=ran$*/ --std=c99
+
+ran%/example.target.cmp : ran%/example.gcc.target.out ran%/example.ccomp.target.out
+ cmp $+ > $@
+
+ran%/example.target.cmp_O3 : ran%/example.gcc.target.out ran%/example.gcc_O3.target.out
+ cmp $+ > $@
+
+ran%/example.host_target.cmp : ran%/example.gcc.host.out ran%/example.ccomp.target.out
+ cmp $+ > $@
+
+yarpgen:
+ curl -L -o yarpgen_v1.1.tar.gz https://github.com/intel/yarpgen/archive/v1.1.tar.gz
+ tar xfz yarpgen_v1.1.tar.gz
+ $(MAKE) CXX=g++ -C yarpgen-1.1
+ cp yarpgen-1.1/yarpgen $@
+
+.PHONY: all clean tests_c tests_c
+
+clean:
+ -rm -rf ran*
diff --git a/test/monniaux/yarpgen/Makefile.old b/test/monniaux/yarpgen/Makefile.old
new file mode 100644
index 00000000..316ec0f1
--- /dev/null
+++ b/test/monniaux/yarpgen/Makefile.old
@@ -0,0 +1,52 @@
+YARPGEN=yarpgen
+MAX=300
+PREFIX=ran%06.f
+include ../rules.mk
+
+KVX_CCOMPFLAGS += -funprototyped -fbitfields
+CCOMPFLAGS += -funprototyped -fbitfields
+
+TARGETS_C=$(shell seq --format $(PREFIX)/func.c 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.c 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/init.h 0 $(MAX))
+TARGETS_CCOMP_KVX_S=$(shell seq --format $(PREFIX)/func.ccomp.kvx.s 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.ccomp.kvx.s 0 $(MAX))
+TARGETS_GCC_KVX_S=$(shell seq --format $(PREFIX)/func.gcc.kvx.s 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.gcc.kvx.s 0 $(MAX))
+TARGETS_CCOMP_HOST_S=$(shell seq --format $(PREFIX)/func.ccomp.host.s 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.ccomp.host.s 0 $(MAX))
+TARGETS_GCC_HOST_S=$(shell seq --format $(PREFIX)/func.gcc.host.s 0 $(MAX)) \
+ $(shell seq --format $(PREFIX)/driver.gcc.host.s 0 $(MAX))
+TARGETS_CCOMP_KVX_OUT=$(shell seq --format $(PREFIX)/example.ccomp.kvx.out 0 $(MAX))
+TARGETS_GCC_KVX_OUT=$(shell seq --format $(PREFIX)/example.gcc.kvx.out 0 $(MAX))
+TARGETS_GCC_HOST_OUT=$(shell seq --format $(PREFIX)/example.gcc.host.out 0 $(MAX))
+TARGETS_CCOMP_HOST_OUT=$(shell seq --format $(PREFIX)/example.ccomp.host.out 0 $(MAX))
+TARGETS_CMP=$(shell seq --format $(PREFIX)/example.kvx.cmp 0 $(MAX))
+
+all: $(TARGETS_CCOMP_KVX_OUT) $(TARGETS_GCC_KVX_OUT) $(TARGETS_GCC_HOST_OUT) $(TARGETS_CCOMP_HOST_OUT) $(TARGETS_CCOMP_KVX_S) $(TARGETS_GCC_KVX_S) $(TARGETS_GCC_HOST_S) $(TARGETS_CCOMP_HOST_S) $(TARGETS_CMP) $(TARGETS_C)
+
+ran%/func.ccomp.kvx.s ran%/func.gcc.kvx.s ran%/func.ccomp.host.s ran%/func.gcc.host.s : ran%/init.h
+
+ran%/example.ccomp.kvx: ran%/func.ccomp.kvx.o ran%/driver.ccomp.kvx.o
+ $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -o $@
+
+ran%/example.gcc.kvx: ran%/func.gcc.kvx.o ran%/driver.gcc.kvx.o
+ $(KVX_CC) $(KVX_CFLAGS) $+ -o $@
+
+ran%/example.gcc.host: ran%/func.gcc.host.o ran%/driver.gcc.host.o
+ $(CC) $(CFLAGS) $+ -o $@
+
+ran%/example.ccomp.host: ran%/func.ccomp.host.o ran%/driver.ccomp.host.o
+ $(CCOMP) $(CCOMPFLAGS) $+ -o $@
+
+ran%/driver.c ran%/func.c ran%/init.h:
+ -mkdir ran$*
+ $(YARPGEN) --seed=$* --out-dir=ran$*/ --std=c99
+
+ran%/example.kvx.cmp : ran%/example.gcc.kvx.out ran%/example.ccomp.kvx.out
+ cmp $+ > $@
+
+.PHONY: all clean
+
+clean:
+ -rm -rf ran*
diff --git a/test/monniaux/zlib-1.2.11/Makefile b/test/monniaux/zlib-1.2.11/Makefile
new file mode 100644
index 00000000..52a7257b
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/Makefile
@@ -0,0 +1,62 @@
+TARGET=zlib
+
+ALL_CCOMPFLAGS=-faddx
+ALL_CFLAGS= -D_POSIX_C_SOURCE=2 -D_LARGEFILE64_SOURCE=1 -U__STRICT_ANSI__
+EXECUTE_ARGS=< zlib_small.txt > /dev/null 2> __BASE__.out
+
+include ../rules.mk
+
+
+#ALL_CCOMPFLAGS = -faddx
+#ALL_CFLAGS = -D_POSIX_C_SOURCE=2 -D_LARGEFILE64_SOURCE=1 -U__STRICT_ANSI__
+#
+#include ../rules.mk
+#
+#src=$(wildcard *.c)
+#
+#PRODUCTS?=minigzip.gcc.host minigzip.ccomp.host minigzip.gcc.kvx minigzip.gcc.o1.kvx minigzip.ccomp.kvx
+#PRODUCTS_OUT=$(addsuffix .out,$(PRODUCTS))
+#
+#all: $(PRODUCTS)
+#
+#.PHONY:
+#run: measures.csv
+#
+#
+#minigzip.gcc.host: $(src:.c=.gcc.host.o) ../clock.gcc.host.o
+# $(CC) $(CFLAGS) $+ -lm -o $@
+#minigzip.ccomp.host: $(src:.c=.ccomp.host.o) ../clock.gcc.host.o
+# $(CCOMP) $(CCOMPFLAGS) $+ -lm -o $@
+#minigzip.gcc.kvx: $(src:.c=.gcc.kvx.o) ../clock.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS) $+ -lm -o $@
+#minigzip.gcc.o1.kvx: $(src:.c=.gcc.o1.kvx.o) ../clock.gcc.kvx.o
+# $(KVX_CC) $(KVX_CFLAGS_O1) $+ -lm -o $@
+#minigzip.ccomp.kvx: $(src:.c=.ccomp.kvx.o) ../clock.gcc.kvx.o
+# $(KVX_CCOMP) $(KVX_CCOMPFLAGS) $+ -lm -o $@
+#measures.csv: $(PRODUCTS_OUT)
+# echo "benches, gcc host,ccomp host,gcc kvx,gcc o1 kvx,ccomp kvx" > $@
+# echo "zlib ", $$(grep 'cycles' minigzip.gcc.host.out | cut -d':' -f2), $$(grep 'cycles' minigzip.ccomp.host.out | cut -d':' -f2), $$(grep 'cycles' minigzip.gcc.kvx.out | cut -d':' -f2), $$(grep 'cycles' minigzip.gcc.o1.kvx.out | cut -d':' -f2), $$(grep 'cycles' minigzip.ccomp.kvx.out | cut -d':' -f2)>> $@
+#
+#SAMPLE_FILE=zlib.h
+#
+#minigzip.gcc.host.out minigzip.gcc.host.output: minigzip.gcc.host
+# ./$< < $(SAMPLE_FILE) > $<.output 2> $@
+#
+#minigzip.ccomp.host.out minigzip.ccomp.host.output: minigzip.ccomp.host
+# ./$< < $(SAMPLE_FILE) > $<.output 2> $@
+#
+#minigzip.gcc.kvx.out minigzip.gcc.kvx.output: minigzip.gcc.kvx
+# $(EXECUTE_CYCLES) $< < $(SAMPLE_FILE) > $<.output 2> $@
+#
+#minigzip.gcc.o1.kvx.out minigzip.gcc.o1.kvx.output: minigzip.gcc.o1.kvx
+# $(EXECUTE_CYCLES) $< < $(SAMPLE_FILE) > $<.output 2> $@
+#
+#minigzip.ccomp.kvx.out minigzip.ccomp.kvx.output: minigzip.ccomp.kvx
+# $(EXECUTE_CYCLES) $< < $(SAMPLE_FILE) > $<.output 2> $@
+#
+#.SECONDARY:
+#
+#.PHONY:
+#clean:
+# rm -f *.o *.s *.kvx *.csv
+#
diff --git a/test/monniaux/zlib-1.2.11/adler32.c b/test/monniaux/zlib-1.2.11/adler32.c
new file mode 100644
index 00000000..d0be4380
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/adler32.c
@@ -0,0 +1,186 @@
+/* adler32.c -- compute the Adler-32 checksum of a data stream
+ * Copyright (C) 1995-2011, 2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+
+local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
+
+#define BASE 65521U /* largest prime smaller than 65536 */
+#define NMAX 5552
+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
+
+#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
+#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf) DO8(buf,0); DO8(buf,8);
+
+/* use NO_DIVIDE if your processor does not do division in hardware --
+ try it both ways to see which is faster */
+#ifdef NO_DIVIDE
+/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
+ (thank you to John Reiser for pointing this out) */
+# define CHOP(a) \
+ do { \
+ unsigned long tmp = a >> 16; \
+ a &= 0xffffUL; \
+ a += (tmp << 4) - tmp; \
+ } while (0)
+# define MOD28(a) \
+ do { \
+ CHOP(a); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+# define MOD(a) \
+ do { \
+ CHOP(a); \
+ MOD28(a); \
+ } while (0)
+# define MOD63(a) \
+ do { /* this assumes a is not negative */ \
+ z_off64_t tmp = a >> 32; \
+ a &= 0xffffffffL; \
+ a += (tmp << 8) - (tmp << 5) + tmp; \
+ tmp = a >> 16; \
+ a &= 0xffffL; \
+ a += (tmp << 4) - tmp; \
+ tmp = a >> 16; \
+ a &= 0xffffL; \
+ a += (tmp << 4) - tmp; \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+#else
+# define MOD(a) a %= BASE
+# define MOD28(a) a %= BASE
+# define MOD63(a) a %= BASE
+#endif
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_z(adler, buf, len)
+ uLong adler;
+ const Bytef *buf;
+ z_size_t len;
+{
+ unsigned long sum2;
+ unsigned n;
+
+ /* split Adler-32 into component sums */
+ sum2 = (adler >> 16) & 0xffff;
+ adler &= 0xffff;
+
+ /* in case user likes doing a byte at a time, keep it fast */
+ if (len == 1) {
+ adler += buf[0];
+ if (adler >= BASE)
+ adler -= BASE;
+ sum2 += adler;
+ if (sum2 >= BASE)
+ sum2 -= BASE;
+ return adler | (sum2 << 16);
+ }
+
+ /* initial Adler-32 value (deferred check for len == 1 speed) */
+ if (buf == Z_NULL)
+ return 1L;
+
+ /* in case short lengths are provided, keep it somewhat fast */
+ if (len < 16) {
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ if (adler >= BASE)
+ adler -= BASE;
+ MOD28(sum2); /* only added so many BASE's */
+ return adler | (sum2 << 16);
+ }
+
+ /* do length NMAX blocks -- requires just one modulo operation */
+ while (len >= NMAX) {
+ len -= NMAX;
+ n = NMAX / 16; /* NMAX is divisible by 16 */
+ do {
+ DO16(buf); /* 16 sums unrolled */
+ buf += 16;
+ } while (--n);
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* do remaining bytes (less than NMAX, still just one modulo) */
+ if (len) { /* avoid modulos if none remaining */
+ while (len >= 16) {
+ len -= 16;
+ DO16(buf);
+ buf += 16;
+ }
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* return recombined sums */
+ return adler | (sum2 << 16);
+}
+
+/* ========================================================================= */
+uLong ZEXPORT adler32(adler, buf, len)
+ uLong adler;
+ const Bytef *buf;
+ uInt len;
+{
+ return adler32_z(adler, buf, len);
+}
+
+/* ========================================================================= */
+local uLong adler32_combine_(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off64_t len2;
+{
+ unsigned long sum1;
+ unsigned long sum2;
+ unsigned rem;
+
+ /* for negative len, return invalid adler32 as a clue for debugging */
+ if (len2 < 0)
+ return 0xffffffffUL;
+
+ /* the derivation of this formula is left as an exercise for the reader */
+ MOD63(len2); /* assumes len2 >= 0 */
+ rem = (unsigned)len2;
+ sum1 = adler1 & 0xffff;
+ sum2 = rem * sum1;
+ MOD(sum2);
+ sum1 += (adler2 & 0xffff) + BASE - 1;
+ sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
+ if (sum2 >= BASE) sum2 -= BASE;
+ return sum1 | (sum2 << 16);
+}
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off_t len2;
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
+
+uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off64_t len2;
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
diff --git a/test/monniaux/zlib-1.2.11/compress.c b/test/monniaux/zlib-1.2.11/compress.c
new file mode 100644
index 00000000..e2db404a
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/compress.c
@@ -0,0 +1,86 @@
+/* compress.c -- compress a memory buffer
+ * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least 0.1% larger than sourceLen plus
+ 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+ int level;
+{
+ z_stream stream;
+ int err;
+ const uInt max = (uInt)-1;
+ uLong left;
+
+ left = *destLen;
+ *destLen = 0;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+ stream.opaque = (voidpf)0;
+
+ err = deflateInit(&stream, level);
+ if (err != Z_OK) return err;
+
+ stream.next_out = dest;
+ stream.avail_out = 0;
+ stream.next_in = (z_const Bytef *)source;
+ stream.avail_in = 0;
+
+ do {
+ if (stream.avail_out == 0) {
+ stream.avail_out = left > (uLong)max ? max : (uInt)left;
+ left -= stream.avail_out;
+ }
+ if (stream.avail_in == 0) {
+ stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
+ sourceLen -= stream.avail_in;
+ }
+ err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
+ } while (err == Z_OK);
+
+ *destLen = stream.total_out;
+ deflateEnd(&stream);
+ return err == Z_STREAM_END ? Z_OK : err;
+}
+
+/* ===========================================================================
+ */
+int ZEXPORT compress (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+{
+ return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
+}
+
+/* ===========================================================================
+ If the default memLevel or windowBits for deflateInit() is changed, then
+ this function needs to be updated.
+ */
+uLong ZEXPORT compressBound (sourceLen)
+ uLong sourceLen;
+{
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13;
+}
diff --git a/test/monniaux/zlib-1.2.11/crc32.c b/test/monniaux/zlib-1.2.11/crc32.c
new file mode 100644
index 00000000..9580440c
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/crc32.c
@@ -0,0 +1,442 @@
+/* crc32.c -- compute the CRC-32 of a data stream
+ * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ *
+ * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
+ * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
+ * tables for updating the shift register in one step with three exclusive-ors
+ * instead of four steps with four exclusive-ors. This results in about a
+ * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
+ */
+
+/* @(#) $Id$ */
+
+/*
+ Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
+ protection on the static variables used to control the first-use generation
+ of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
+ first call get_crc_table() to initialize the tables before allowing more than
+ one thread to use crc32().
+
+ DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
+ */
+
+#ifdef MAKECRCH
+# include <stdio.h>
+# ifndef DYNAMIC_CRC_TABLE
+# define DYNAMIC_CRC_TABLE
+# endif /* !DYNAMIC_CRC_TABLE */
+#endif /* MAKECRCH */
+
+#include "zutil.h" /* for STDC and FAR definitions */
+
+/* Definitions for doing the crc four data bytes at a time. */
+#if !defined(NOBYFOUR) && defined(Z_U4)
+# define BYFOUR
+#endif
+#ifdef BYFOUR
+ local unsigned long crc32_little OF((unsigned long,
+ const unsigned char FAR *, z_size_t));
+ local unsigned long crc32_big OF((unsigned long,
+ const unsigned char FAR *, z_size_t));
+# define TBLS 8
+#else
+# define TBLS 1
+#endif /* BYFOUR */
+
+/* Local functions for crc concatenation */
+local unsigned long gf2_matrix_times OF((unsigned long *mat,
+ unsigned long vec));
+local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
+local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
+
+
+#ifdef DYNAMIC_CRC_TABLE
+
+local volatile int crc_table_empty = 1;
+local z_crc_t FAR crc_table[TBLS][256];
+local void make_crc_table OF((void));
+#ifdef MAKECRCH
+ local void write_table OF((FILE *, const z_crc_t FAR *));
+#endif /* MAKECRCH */
+/*
+ Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
+ x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
+
+ Polynomials over GF(2) are represented in binary, one bit per coefficient,
+ with the lowest powers in the most significant bit. Then adding polynomials
+ is just exclusive-or, and multiplying a polynomial by x is a right shift by
+ one. If we call the above polynomial p, and represent a byte as the
+ polynomial q, also with the lowest power in the most significant bit (so the
+ byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
+ where a mod b means the remainder after dividing a by b.
+
+ This calculation is done using the shift-register method of multiplying and
+ taking the remainder. The register is initialized to zero, and for each
+ incoming bit, x^32 is added mod p to the register if the bit is a one (where
+ x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
+ x (which is shifting right by one and adding x^32 mod p if the bit shifted
+ out is a one). We start with the highest power (least significant bit) of
+ q and repeat for all eight bits of q.
+
+ The first table is simply the CRC of all possible eight bit values. This is
+ all the information needed to generate CRCs on data a byte at a time for all
+ combinations of CRC register values and incoming bytes. The remaining tables
+ allow for word-at-a-time CRC calculation for both big-endian and little-
+ endian machines, where a word is four bytes.
+*/
+local void make_crc_table()
+{
+ z_crc_t c;
+ int n, k;
+ z_crc_t poly; /* polynomial exclusive-or pattern */
+ /* terms of polynomial defining this crc (except x^32): */
+ static volatile int first = 1; /* flag to limit concurrent making */
+ static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+ /* See if another task is already doing this (not thread-safe, but better
+ than nothing -- significantly reduces duration of vulnerability in
+ case the advice about DYNAMIC_CRC_TABLE is ignored) */
+ if (first) {
+ first = 0;
+
+ /* make exclusive-or pattern from polynomial (0xedb88320UL) */
+ poly = 0;
+ for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
+ poly |= (z_crc_t)1 << (31 - p[n]);
+
+ /* generate a crc for every 8-bit value */
+ for (n = 0; n < 256; n++) {
+ c = (z_crc_t)n;
+ for (k = 0; k < 8; k++)
+ c = c & 1 ? poly ^ (c >> 1) : c >> 1;
+ crc_table[0][n] = c;
+ }
+
+#ifdef BYFOUR
+ /* generate crc for each value followed by one, two, and three zeros,
+ and then the byte reversal of those as well as the first table */
+ for (n = 0; n < 256; n++) {
+ c = crc_table[0][n];
+ crc_table[4][n] = ZSWAP32(c);
+ for (k = 1; k < 4; k++) {
+ c = crc_table[0][c & 0xff] ^ (c >> 8);
+ crc_table[k][n] = c;
+ crc_table[k + 4][n] = ZSWAP32(c);
+ }
+ }
+#endif /* BYFOUR */
+
+ crc_table_empty = 0;
+ }
+ else { /* not first */
+ /* wait for the other guy to finish (not efficient, but rare) */
+ while (crc_table_empty)
+ ;
+ }
+
+#ifdef MAKECRCH
+ /* write out CRC tables to crc32.h */
+ {
+ FILE *out;
+
+ out = fopen("crc32.h", "w");
+ if (out == NULL) return;
+ fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
+ fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
+ fprintf(out, "local const z_crc_t FAR ");
+ fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
+ write_table(out, crc_table[0]);
+# ifdef BYFOUR
+ fprintf(out, "#ifdef BYFOUR\n");
+ for (k = 1; k < 8; k++) {
+ fprintf(out, " },\n {\n");
+ write_table(out, crc_table[k]);
+ }
+ fprintf(out, "#endif\n");
+# endif /* BYFOUR */
+ fprintf(out, " }\n};\n");
+ fclose(out);
+ }
+#endif /* MAKECRCH */
+}
+
+#ifdef MAKECRCH
+local void write_table(out, table)
+ FILE *out;
+ const z_crc_t FAR *table;
+{
+ int n;
+
+ for (n = 0; n < 256; n++)
+ fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
+ (unsigned long)(table[n]),
+ n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
+}
+#endif /* MAKECRCH */
+
+#else /* !DYNAMIC_CRC_TABLE */
+/* ========================================================================
+ * Tables of CRC-32s of all single-byte values, made by make_crc_table().
+ */
+#include "crc32.h"
+#endif /* DYNAMIC_CRC_TABLE */
+
+/* =========================================================================
+ * This function can be used by asm versions of crc32()
+ */
+const z_crc_t FAR * ZEXPORT get_crc_table()
+{
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+ return (const z_crc_t FAR *)crc_table;
+}
+
+/* ========================================================================= */
+#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
+#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
+
+/* ========================================================================= */
+unsigned long ZEXPORT crc32_z(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ z_size_t len;
+{
+ if (buf == Z_NULL) return 0UL;
+
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+
+#ifdef BYFOUR
+ if (sizeof(void *) == sizeof(ptrdiff_t)) {
+ z_crc_t endian;
+
+ endian = 1;
+ if (*((unsigned char *)(&endian)))
+ return crc32_little(crc, buf, len);
+ else
+ return crc32_big(crc, buf, len);
+ }
+#endif /* BYFOUR */
+ crc = crc ^ 0xffffffffUL;
+ while (len >= 8) {
+ DO8;
+ len -= 8;
+ }
+ if (len) do {
+ DO1;
+ } while (--len);
+ return crc ^ 0xffffffffUL;
+}
+
+/* ========================================================================= */
+unsigned long ZEXPORT crc32(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ uInt len;
+{
+ return crc32_z(crc, buf, len);
+}
+
+#ifdef BYFOUR
+
+/*
+ This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
+ integer pointer type. This violates the strict aliasing rule, where a
+ compiler can assume, for optimization purposes, that two pointers to
+ fundamentally different types won't ever point to the same memory. This can
+ manifest as a problem only if one of the pointers is written to. This code
+ only reads from those pointers. So long as this code remains isolated in
+ this compilation unit, there won't be a problem. For this reason, this code
+ should not be copied and pasted into a compilation unit in which other code
+ writes to the buffer that is passed to these routines.
+ */
+
+/* ========================================================================= */
+#define DOLIT4 c ^= *buf4++; \
+ c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
+ crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
+#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
+
+/* ========================================================================= */
+local unsigned long crc32_little(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ z_size_t len;
+{
+ register z_crc_t c;
+ register const z_crc_t FAR *buf4;
+
+ c = (z_crc_t)crc;
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ len--;
+ }
+
+ buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
+ while (len >= 32) {
+ DOLIT32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOLIT4;
+ len -= 4;
+ }
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)c;
+}
+
+/* ========================================================================= */
+#define DOBIG4 c ^= *buf4++; \
+ c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
+ crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
+#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
+
+/* ========================================================================= */
+local unsigned long crc32_big(crc, buf, len)
+ unsigned long crc;
+ const unsigned char FAR *buf;
+ z_size_t len;
+{
+ register z_crc_t c;
+ register const z_crc_t FAR *buf4;
+
+ c = ZSWAP32((z_crc_t)crc);
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ len--;
+ }
+
+ buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
+ while (len >= 32) {
+ DOBIG32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOBIG4;
+ len -= 4;
+ }
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)(ZSWAP32(c));
+}
+
+#endif /* BYFOUR */
+
+#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
+
+/* ========================================================================= */
+local unsigned long gf2_matrix_times(mat, vec)
+ unsigned long *mat;
+ unsigned long vec;
+{
+ unsigned long sum;
+
+ sum = 0;
+ while (vec) {
+ if (vec & 1)
+ sum ^= *mat;
+ vec >>= 1;
+ mat++;
+ }
+ return sum;
+}
+
+/* ========================================================================= */
+local void gf2_matrix_square(square, mat)
+ unsigned long *square;
+ unsigned long *mat;
+{
+ int n;
+
+ for (n = 0; n < GF2_DIM; n++)
+ square[n] = gf2_matrix_times(mat, mat[n]);
+}
+
+/* ========================================================================= */
+local uLong crc32_combine_(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off64_t len2;
+{
+ int n;
+ unsigned long row;
+ unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
+ unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
+
+ /* degenerate case (also disallow negative lengths) */
+ if (len2 <= 0)
+ return crc1;
+
+ /* put operator for one zero bit in odd */
+ odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
+ row = 1;
+ for (n = 1; n < GF2_DIM; n++) {
+ odd[n] = row;
+ row <<= 1;
+ }
+
+ /* put operator for two zero bits in even */
+ gf2_matrix_square(even, odd);
+
+ /* put operator for four zero bits in odd */
+ gf2_matrix_square(odd, even);
+
+ /* apply len2 zeros to crc1 (first square will put the operator for one
+ zero byte, eight zero bits, in even) */
+ do {
+ /* apply zeros operator for this bit of len2 */
+ gf2_matrix_square(even, odd);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(even, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ if (len2 == 0)
+ break;
+
+ /* another iteration of the loop with odd and even swapped */
+ gf2_matrix_square(odd, even);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(odd, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ } while (len2 != 0);
+
+ /* return combined crc */
+ crc1 ^= crc2;
+ return crc1;
+}
+
+/* ========================================================================= */
+uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off_t len2;
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
+
+uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
+ uLong crc1;
+ uLong crc2;
+ z_off64_t len2;
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
diff --git a/test/monniaux/zlib-1.2.11/crc32.h b/test/monniaux/zlib-1.2.11/crc32.h
new file mode 100644
index 00000000..9e0c7781
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/crc32.h
@@ -0,0 +1,441 @@
+/* crc32.h -- tables for rapid CRC calculation
+ * Generated automatically by crc32.c
+ */
+
+local const z_crc_t FAR crc_table[TBLS][256] =
+{
+ {
+ 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
+ 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
+ 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
+ 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
+ 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
+ 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
+ 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
+ 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
+ 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
+ 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
+ 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
+ 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
+ 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
+ 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
+ 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
+ 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
+ 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
+ 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
+ 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
+ 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
+ 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
+ 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
+ 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
+ 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
+ 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
+ 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
+ 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
+ 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
+ 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
+ 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
+ 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
+ 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
+ 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
+ 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
+ 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
+ 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
+ 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
+ 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
+ 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
+ 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
+ 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
+ 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
+ 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
+ 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
+ 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
+ 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
+ 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
+ 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
+ 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
+ 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
+ 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
+ 0x2d02ef8dUL
+#ifdef BYFOUR
+ },
+ {
+ 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
+ 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
+ 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
+ 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
+ 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
+ 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
+ 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
+ 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
+ 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
+ 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
+ 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
+ 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
+ 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
+ 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
+ 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
+ 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
+ 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
+ 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
+ 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
+ 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
+ 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
+ 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
+ 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
+ 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
+ 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
+ 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
+ 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
+ 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
+ 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
+ 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
+ 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
+ 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
+ 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
+ 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
+ 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
+ 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
+ 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
+ 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
+ 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
+ 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
+ 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
+ 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
+ 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
+ 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
+ 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
+ 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
+ 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
+ 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
+ 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
+ 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
+ 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
+ 0x9324fd72UL
+ },
+ {
+ 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
+ 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
+ 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
+ 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
+ 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
+ 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
+ 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
+ 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
+ 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
+ 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
+ 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
+ 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
+ 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
+ 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
+ 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
+ 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
+ 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
+ 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
+ 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
+ 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
+ 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
+ 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
+ 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
+ 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
+ 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
+ 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
+ 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
+ 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
+ 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
+ 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
+ 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
+ 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
+ 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
+ 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
+ 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
+ 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
+ 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
+ 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
+ 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
+ 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
+ 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
+ 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
+ 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
+ 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
+ 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
+ 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
+ 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
+ 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
+ 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
+ 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
+ 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
+ 0xbe9834edUL
+ },
+ {
+ 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
+ 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
+ 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
+ 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
+ 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
+ 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
+ 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
+ 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
+ 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
+ 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
+ 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
+ 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
+ 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
+ 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
+ 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
+ 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
+ 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
+ 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
+ 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
+ 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
+ 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
+ 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
+ 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
+ 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
+ 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
+ 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
+ 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
+ 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
+ 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
+ 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
+ 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
+ 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
+ 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
+ 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
+ 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
+ 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
+ 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
+ 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
+ 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
+ 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
+ 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
+ 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
+ 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
+ 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
+ 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
+ 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
+ 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
+ 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
+ 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
+ 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
+ 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
+ 0xde0506f1UL
+ },
+ {
+ 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
+ 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
+ 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
+ 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
+ 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
+ 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
+ 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
+ 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
+ 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
+ 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
+ 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
+ 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
+ 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
+ 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
+ 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
+ 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
+ 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
+ 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
+ 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
+ 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
+ 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
+ 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
+ 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
+ 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
+ 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
+ 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
+ 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
+ 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
+ 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
+ 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
+ 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
+ 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
+ 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
+ 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
+ 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
+ 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
+ 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
+ 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
+ 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
+ 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
+ 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
+ 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
+ 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
+ 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
+ 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
+ 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
+ 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
+ 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
+ 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
+ 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
+ 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
+ 0x8def022dUL
+ },
+ {
+ 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
+ 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
+ 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
+ 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
+ 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
+ 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
+ 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
+ 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
+ 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
+ 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
+ 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
+ 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
+ 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
+ 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
+ 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
+ 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
+ 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
+ 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
+ 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
+ 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
+ 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
+ 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
+ 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
+ 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
+ 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
+ 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
+ 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
+ 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
+ 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
+ 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
+ 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
+ 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
+ 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
+ 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
+ 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
+ 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
+ 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
+ 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
+ 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
+ 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
+ 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
+ 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
+ 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
+ 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
+ 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
+ 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
+ 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
+ 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
+ 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
+ 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
+ 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
+ 0x72fd2493UL
+ },
+ {
+ 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
+ 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
+ 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
+ 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
+ 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
+ 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
+ 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
+ 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
+ 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
+ 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
+ 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
+ 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
+ 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
+ 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
+ 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
+ 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
+ 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
+ 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
+ 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
+ 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
+ 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
+ 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
+ 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
+ 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
+ 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
+ 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
+ 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
+ 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
+ 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
+ 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
+ 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
+ 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
+ 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
+ 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
+ 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
+ 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
+ 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
+ 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
+ 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
+ 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
+ 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
+ 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
+ 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
+ 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
+ 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
+ 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
+ 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
+ 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
+ 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
+ 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
+ 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
+ 0xed3498beUL
+ },
+ {
+ 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
+ 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
+ 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
+ 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
+ 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
+ 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
+ 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
+ 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
+ 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
+ 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
+ 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
+ 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
+ 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
+ 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
+ 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
+ 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
+ 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
+ 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
+ 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
+ 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
+ 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
+ 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
+ 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
+ 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
+ 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
+ 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
+ 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
+ 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
+ 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
+ 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
+ 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
+ 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
+ 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
+ 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
+ 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
+ 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
+ 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
+ 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
+ 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
+ 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
+ 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
+ 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
+ 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
+ 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
+ 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
+ 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
+ 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
+ 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
+ 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
+ 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
+ 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
+ 0xf10605deUL
+#endif
+ }
+};
diff --git a/test/monniaux/zlib-1.2.11/deflate.c b/test/monniaux/zlib-1.2.11/deflate.c
new file mode 100644
index 00000000..1ec76144
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/deflate.c
@@ -0,0 +1,2163 @@
+/* deflate.c -- compress data using the deflation algorithm
+ * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process depends on being able to identify portions
+ * of the input text which are identical to earlier input (within a
+ * sliding window trailing behind the input currently being processed).
+ *
+ * The most straightforward technique turns out to be the fastest for
+ * most input files: try all possible matches and select the longest.
+ * The key feature of this algorithm is that insertions into the string
+ * dictionary are very simple and thus fast, and deletions are avoided
+ * completely. Insertions are performed at each input character, whereas
+ * string matches are performed only when the previous match ends. So it
+ * is preferable to spend more time in matches to allow very fast string
+ * insertions and avoid deletions. The matching algorithm for small
+ * strings is inspired from that of Rabin & Karp. A brute force approach
+ * is used to find longer strings when a small match has been found.
+ * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
+ * (by Leonid Broukhis).
+ * A previous version of this file used a more sophisticated algorithm
+ * (by Fiala and Greene) which is guaranteed to run in linear amortized
+ * time, but has a larger average cost, uses more memory and is patented.
+ * However the F&G algorithm may be faster for some highly redundant
+ * files if the parameter max_chain_length (described below) is too large.
+ *
+ * ACKNOWLEDGEMENTS
+ *
+ * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
+ * I found it in 'freeze' written by Leonid Broukhis.
+ * Thanks to many people for bug reports and testing.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
+ * Available in http://tools.ietf.org/html/rfc1951
+ *
+ * A description of the Rabin and Karp algorithm is given in the book
+ * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
+ *
+ * Fiala,E.R., and Greene,D.H.
+ * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
+ *
+ */
+
+/* @(#) $Id$ */
+
+#include "deflate.h"
+
+const char deflate_copyright[] =
+ " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/* ===========================================================================
+ * Function prototypes.
+ */
+typedef enum {
+ need_more, /* block not completed, need more input or more output */
+ block_done, /* block flush performed */
+ finish_started, /* finish started, need only more output at next deflate */
+ finish_done /* finish done, accept no more input or output */
+} block_state;
+
+typedef block_state (*compress_func) OF((deflate_state *s, int flush));
+/* Compression function. Returns the block state after the call. */
+
+local int deflateStateCheck OF((z_streamp strm));
+local void slide_hash OF((deflate_state *s));
+local void fill_window OF((deflate_state *s));
+local block_state deflate_stored OF((deflate_state *s, int flush));
+local block_state deflate_fast OF((deflate_state *s, int flush));
+#ifndef FASTEST
+local block_state deflate_slow OF((deflate_state *s, int flush));
+#endif
+local block_state deflate_rle OF((deflate_state *s, int flush));
+local block_state deflate_huff OF((deflate_state *s, int flush));
+local void lm_init OF((deflate_state *s));
+local void putShortMSB OF((deflate_state *s, uInt b));
+local void flush_pending OF((z_streamp strm));
+local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
+#ifdef ASMV
+# pragma message("Assembler code may have bugs -- use at your own risk")
+ void match_init OF((void)); /* asm code initialization */
+ uInt longest_match OF((deflate_state *s, IPos cur_match));
+#else
+local uInt longest_match OF((deflate_state *s, IPos cur_match));
+#endif
+
+#ifdef ZLIB_DEBUG
+local void check_match OF((deflate_state *s, IPos start, IPos match,
+ int length));
+#endif
+
+/* ===========================================================================
+ * Local data
+ */
+
+#define NIL 0
+/* Tail of hash chains */
+
+#ifndef TOO_FAR
+# define TOO_FAR 4096
+#endif
+/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
+
+/* Values for max_lazy_match, good_match and max_chain_length, depending on
+ * the desired pack level (0..9). The values given below have been tuned to
+ * exclude worst case performance for pathological files. Better values may be
+ * found for specific files.
+ */
+typedef struct config_s {
+ ush good_length; /* reduce lazy search above this match length */
+ ush max_lazy; /* do not perform lazy search above this match length */
+ ush nice_length; /* quit search above this match length */
+ ush max_chain;
+ compress_func func;
+} config;
+
+#ifdef FASTEST
+local const config configuration_table[2] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
+#else
+local const config configuration_table[10] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
+/* 2 */ {4, 5, 16, 8, deflate_fast},
+/* 3 */ {4, 6, 32, 32, deflate_fast},
+
+/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
+/* 5 */ {8, 16, 32, 32, deflate_slow},
+/* 6 */ {8, 16, 128, 128, deflate_slow},
+/* 7 */ {8, 32, 128, 256, deflate_slow},
+/* 8 */ {32, 128, 258, 1024, deflate_slow},
+/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
+#endif
+
+/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
+ * meaning.
+ */
+
+/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
+#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
+
+/* ===========================================================================
+ * Update a hash value with the given input byte
+ * IN assertion: all calls to UPDATE_HASH are made with consecutive input
+ * characters, so that a running hash key can be computed from the previous
+ * key instead of complete recalculation each time.
+ */
+#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
+
+
+/* ===========================================================================
+ * Insert string str in the dictionary and set match_head to the previous head
+ * of the hash chain (the most recent string with same hash key). Return
+ * the previous length of the hash chain.
+ * If this file is compiled with -DFASTEST, the compression level is forced
+ * to 1, and no hash chains are maintained.
+ * IN assertion: all calls to INSERT_STRING are made with consecutive input
+ * characters and the first MIN_MATCH bytes of str are valid (except for
+ * the last MIN_MATCH-1 bytes of the input file).
+ */
+#ifdef FASTEST
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#else
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#endif
+
+/* ===========================================================================
+ * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
+ * prev[] will be initialized on the fly.
+ */
+#define CLEAR_HASH(s) \
+ s->head[s->hash_size-1] = NIL; \
+ zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
+
+/* ===========================================================================
+ * Slide the hash table when sliding the window down (could be avoided with 32
+ * bit values at the expense of memory usage). We slide even when level == 0 to
+ * keep the hash table consistent if we switch back to level > 0 later.
+ */
+local void slide_hash(s)
+ deflate_state *s;
+{
+ unsigned n, m;
+ Posf *p;
+ uInt wsize = s->w_size;
+
+ n = s->hash_size;
+ p = &s->head[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
+ } while (--n);
+ n = wsize;
+#ifndef FASTEST
+ p = &s->prev[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
+ /* If n is not on any hash chain, prev[n] is garbage but
+ * its value will never be used.
+ */
+ } while (--n);
+#endif
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateInit_(strm, level, version, stream_size)
+ z_streamp strm;
+ int level;
+ const char *version;
+ int stream_size;
+{
+ return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
+ Z_DEFAULT_STRATEGY, version, stream_size);
+ /* To do: ignore strm->next_in if we use it as window */
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
+ version, stream_size)
+ z_streamp strm;
+ int level;
+ int method;
+ int windowBits;
+ int memLevel;
+ int strategy;
+ const char *version;
+ int stream_size;
+{
+ deflate_state *s;
+ int wrap = 1;
+ static const char my_version[] = ZLIB_VERSION;
+
+ ushf *overlay;
+ /* We overlay pending_buf and d_buf+l_buf. This works since the average
+ * output size for (length,distance) codes is <= 24 bits.
+ */
+
+ if (version == Z_NULL || version[0] != my_version[0] ||
+ stream_size != sizeof(z_stream)) {
+ return Z_VERSION_ERROR;
+ }
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+
+ strm->msg = Z_NULL;
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+
+ if (windowBits < 0) { /* suppress zlib wrapper */
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+#ifdef GZIP
+ else if (windowBits > 15) {
+ wrap = 2; /* write gzip wrapper instead */
+ windowBits -= 16;
+ }
+#endif
+ if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
+ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
+ strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
+ return Z_STREAM_ERROR;
+ }
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
+ s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
+ if (s == Z_NULL) return Z_MEM_ERROR;
+ strm->state = (struct internal_state FAR *)s;
+ s->strm = strm;
+ s->status = INIT_STATE; /* to pass state test in deflateReset() */
+
+ s->wrap = wrap;
+ s->gzhead = Z_NULL;
+ s->w_bits = (uInt)windowBits;
+ s->w_size = 1 << s->w_bits;
+ s->w_mask = s->w_size - 1;
+
+ s->hash_bits = (uInt)memLevel + 7;
+ s->hash_size = 1 << s->hash_bits;
+ s->hash_mask = s->hash_size - 1;
+ s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
+
+ s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
+ s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
+ s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
+
+ s->high_water = 0; /* nothing written to s->window yet */
+
+ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
+
+ overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
+ s->pending_buf = (uchf *) overlay;
+ s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
+
+ if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
+ s->pending_buf == Z_NULL) {
+ s->status = FINISH_STATE;
+ strm->msg = ERR_MSG(Z_MEM_ERROR);
+ deflateEnd (strm);
+ return Z_MEM_ERROR;
+ }
+ s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
+ s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
+
+ s->level = level;
+ s->strategy = strategy;
+ s->method = (Byte)method;
+
+ return deflateReset(strm);
+}
+
+/* =========================================================================
+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
+ */
+local int deflateStateCheck (strm)
+ z_streamp strm;
+{
+ deflate_state *s;
+ if (strm == Z_NULL ||
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
+ return 1;
+ s = strm->state;
+ if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
+#ifdef GZIP
+ s->status != GZIP_STATE &&
+#endif
+ s->status != EXTRA_STATE &&
+ s->status != NAME_STATE &&
+ s->status != COMMENT_STATE &&
+ s->status != HCRC_STATE &&
+ s->status != BUSY_STATE &&
+ s->status != FINISH_STATE))
+ return 1;
+ return 0;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
+ z_streamp strm;
+ const Bytef *dictionary;
+ uInt dictLength;
+{
+ deflate_state *s;
+ uInt str, n;
+ int wrap;
+ unsigned avail;
+ z_const unsigned char *next;
+
+ if (deflateStateCheck(strm) || dictionary == Z_NULL)
+ return Z_STREAM_ERROR;
+ s = strm->state;
+ wrap = s->wrap;
+ if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
+ return Z_STREAM_ERROR;
+
+ /* when using zlib wrappers, compute Adler-32 for provided dictionary */
+ if (wrap == 1)
+ strm->adler = adler32(strm->adler, dictionary, dictLength);
+ s->wrap = 0; /* avoid computing Adler-32 in read_buf */
+
+ /* if dictionary would fill window, just replace the history */
+ if (dictLength >= s->w_size) {
+ if (wrap == 0) { /* already empty otherwise */
+ CLEAR_HASH(s);
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->insert = 0;
+ }
+ dictionary += dictLength - s->w_size; /* use the tail */
+ dictLength = s->w_size;
+ }
+
+ /* insert dictionary into window and hash */
+ avail = strm->avail_in;
+ next = strm->next_in;
+ strm->avail_in = dictLength;
+ strm->next_in = (z_const Bytef *)dictionary;
+ fill_window(s);
+ while (s->lookahead >= MIN_MATCH) {
+ str = s->strstart;
+ n = s->lookahead - (MIN_MATCH-1);
+ do {
+ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+ s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+ s->head[s->ins_h] = (Pos)str;
+ str++;
+ } while (--n);
+ s->strstart = str;
+ s->lookahead = MIN_MATCH-1;
+ fill_window(s);
+ }
+ s->strstart += s->lookahead;
+ s->block_start = (long)s->strstart;
+ s->insert = s->lookahead;
+ s->lookahead = 0;
+ s->match_length = s->prev_length = MIN_MATCH-1;
+ s->match_available = 0;
+ strm->next_in = next;
+ strm->avail_in = avail;
+ s->wrap = wrap;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
+ z_streamp strm;
+ Bytef *dictionary;
+ uInt *dictLength;
+{
+ deflate_state *s;
+ uInt len;
+
+ if (deflateStateCheck(strm))
+ return Z_STREAM_ERROR;
+ s = strm->state;
+ len = s->strstart + s->lookahead;
+ if (len > s->w_size)
+ len = s->w_size;
+ if (dictionary != Z_NULL && len)
+ zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
+ if (dictLength != Z_NULL)
+ *dictLength = len;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateResetKeep (strm)
+ z_streamp strm;
+{
+ deflate_state *s;
+
+ if (deflateStateCheck(strm)) {
+ return Z_STREAM_ERROR;
+ }
+
+ strm->total_in = strm->total_out = 0;
+ strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
+ strm->data_type = Z_UNKNOWN;
+
+ s = (deflate_state *)strm->state;
+ s->pending = 0;
+ s->pending_out = s->pending_buf;
+
+ if (s->wrap < 0) {
+ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
+ }
+ s->status =
+#ifdef GZIP
+ s->wrap == 2 ? GZIP_STATE :
+#endif
+ s->wrap ? INIT_STATE : BUSY_STATE;
+ strm->adler =
+#ifdef GZIP
+ s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
+#endif
+ adler32(0L, Z_NULL, 0);
+ s->last_flush = Z_NO_FLUSH;
+
+ _tr_init(s);
+
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateReset (strm)
+ z_streamp strm;
+{
+ int ret;
+
+ ret = deflateResetKeep(strm);
+ if (ret == Z_OK)
+ lm_init(strm->state);
+ return ret;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetHeader (strm, head)
+ z_streamp strm;
+ gz_headerp head;
+{
+ if (deflateStateCheck(strm) || strm->state->wrap != 2)
+ return Z_STREAM_ERROR;
+ strm->state->gzhead = head;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflatePending (strm, pending, bits)
+ unsigned *pending;
+ int *bits;
+ z_streamp strm;
+{
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
+ if (pending != Z_NULL)
+ *pending = strm->state->pending;
+ if (bits != Z_NULL)
+ *bits = strm->state->bi_valid;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflatePrime (strm, bits, value)
+ z_streamp strm;
+ int bits;
+ int value;
+{
+ deflate_state *s;
+ int put;
+
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
+ s = strm->state;
+ if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
+ return Z_BUF_ERROR;
+ do {
+ put = Buf_size - s->bi_valid;
+ if (put > bits)
+ put = bits;
+ s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
+ s->bi_valid += put;
+ _tr_flush_bits(s);
+ value >>= put;
+ bits -= put;
+ } while (bits);
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateParams(strm, level, strategy)
+ z_streamp strm;
+ int level;
+ int strategy;
+{
+ deflate_state *s;
+ compress_func func;
+
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
+ s = strm->state;
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+ if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
+ return Z_STREAM_ERROR;
+ }
+ func = configuration_table[s->level].func;
+
+ if ((strategy != s->strategy || func != configuration_table[level].func) &&
+ s->high_water) {
+ /* Flush the last buffer: */
+ int err = deflate(strm, Z_BLOCK);
+ if (err == Z_STREAM_ERROR)
+ return err;
+ if (strm->avail_out == 0)
+ return Z_BUF_ERROR;
+ }
+ if (s->level != level) {
+ if (s->level == 0 && s->matches != 0) {
+ if (s->matches == 1)
+ slide_hash(s);
+ else
+ CLEAR_HASH(s);
+ s->matches = 0;
+ }
+ s->level = level;
+ s->max_lazy_match = configuration_table[level].max_lazy;
+ s->good_match = configuration_table[level].good_length;
+ s->nice_match = configuration_table[level].nice_length;
+ s->max_chain_length = configuration_table[level].max_chain;
+ }
+ s->strategy = strategy;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
+ z_streamp strm;
+ int good_length;
+ int max_lazy;
+ int nice_length;
+ int max_chain;
+{
+ deflate_state *s;
+
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
+ s = strm->state;
+ s->good_match = (uInt)good_length;
+ s->max_lazy_match = (uInt)max_lazy;
+ s->nice_match = nice_length;
+ s->max_chain_length = (uInt)max_chain;
+ return Z_OK;
+}
+
+/* =========================================================================
+ * For the default windowBits of 15 and memLevel of 8, this function returns
+ * a close to exact, as well as small, upper bound on the compressed size.
+ * They are coded as constants here for a reason--if the #define's are
+ * changed, then this function needs to be changed as well. The return
+ * value for 15 and 8 only works for those exact settings.
+ *
+ * For any setting other than those defaults for windowBits and memLevel,
+ * the value returned is a conservative worst case for the maximum expansion
+ * resulting from using fixed blocks instead of stored blocks, which deflate
+ * can emit on compressed data for some combinations of the parameters.
+ *
+ * This function could be more sophisticated to provide closer upper bounds for
+ * every combination of windowBits and memLevel. But even the conservative
+ * upper bound of about 14% expansion does not seem onerous for output buffer
+ * allocation.
+ */
+uLong ZEXPORT deflateBound(strm, sourceLen)
+ z_streamp strm;
+ uLong sourceLen;
+{
+ deflate_state *s;
+ uLong complen, wraplen;
+
+ /* conservative upper bound for compressed data */
+ complen = sourceLen +
+ ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
+
+ /* if can't get parameters, return conservative bound plus zlib wrapper */
+ if (deflateStateCheck(strm))
+ return complen + 6;
+
+ /* compute wrapper length */
+ s = strm->state;
+ switch (s->wrap) {
+ case 0: /* raw deflate */
+ wraplen = 0;
+ break;
+ case 1: /* zlib wrapper */
+ wraplen = 6 + (s->strstart ? 4 : 0);
+ break;
+#ifdef GZIP
+ case 2: /* gzip wrapper */
+ wraplen = 18;
+ if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
+ Bytef *str;
+ if (s->gzhead->extra != Z_NULL)
+ wraplen += 2 + s->gzhead->extra_len;
+ str = s->gzhead->name;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ str = s->gzhead->comment;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ if (s->gzhead->hcrc)
+ wraplen += 2;
+ }
+ break;
+#endif
+ default: /* for compiler happiness */
+ wraplen = 6;
+ }
+
+ /* if not default parameters, return conservative bound */
+ if (s->w_bits != 15 || s->hash_bits != 8 + 7)
+ return complen + wraplen;
+
+ /* default settings: return tight bound for that case */
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13 - 6 + wraplen;
+}
+
+/* =========================================================================
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
+ * IN assertion: the stream state is correct and there is enough room in
+ * pending_buf.
+ */
+local void putShortMSB (s, b)
+ deflate_state *s;
+ uInt b;
+{
+ put_byte(s, (Byte)(b >> 8));
+ put_byte(s, (Byte)(b & 0xff));
+}
+
+/* =========================================================================
+ * Flush as much pending output as possible. All deflate() output, except for
+ * some deflate_stored() output, goes through this function so some
+ * applications may wish to modify it to avoid allocating a large
+ * strm->next_out buffer and copying into it. (See also read_buf()).
+ */
+local void flush_pending(strm)
+ z_streamp strm;
+{
+ unsigned len;
+ deflate_state *s = strm->state;
+
+ _tr_flush_bits(s);
+ len = s->pending;
+ if (len > strm->avail_out) len = strm->avail_out;
+ if (len == 0) return;
+
+ zmemcpy(strm->next_out, s->pending_out, len);
+ strm->next_out += len;
+ s->pending_out += len;
+ strm->total_out += len;
+ strm->avail_out -= len;
+ s->pending -= len;
+ if (s->pending == 0) {
+ s->pending_out = s->pending_buf;
+ }
+}
+
+/* ===========================================================================
+ * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
+ */
+#define HCRC_UPDATE(beg) \
+ do { \
+ if (s->gzhead->hcrc && s->pending > (beg)) \
+ strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
+ s->pending - (beg)); \
+ } while (0)
+
+/* ========================================================================= */
+int ZEXPORT deflate (strm, flush)
+ z_streamp strm;
+ int flush;
+{
+ int old_flush; /* value of flush param for previous deflate call */
+ deflate_state *s;
+
+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
+ return Z_STREAM_ERROR;
+ }
+ s = strm->state;
+
+ if (strm->next_out == Z_NULL ||
+ (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
+ (s->status == FINISH_STATE && flush != Z_FINISH)) {
+ ERR_RETURN(strm, Z_STREAM_ERROR);
+ }
+ if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
+
+ old_flush = s->last_flush;
+ s->last_flush = flush;
+
+ /* Flush as much pending output as possible */
+ if (s->pending != 0) {
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ /* Since avail_out is 0, deflate will be called again with
+ * more output space, but possibly with both pending and
+ * avail_in equal to zero. There won't be anything to do,
+ * but this is not an error situation so make sure we
+ * return OK instead of BUF_ERROR at next call of deflate:
+ */
+ s->last_flush = -1;
+ return Z_OK;
+ }
+
+ /* Make sure there is something to do and avoid duplicate consecutive
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
+ */
+ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
+ flush != Z_FINISH) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* User must not provide more input after the first FINISH: */
+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* Write the header */
+ if (s->status == INIT_STATE) {
+ /* zlib header */
+ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
+ uInt level_flags;
+
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
+ level_flags = 0;
+ else if (s->level < 6)
+ level_flags = 1;
+ else if (s->level == 6)
+ level_flags = 2;
+ else
+ level_flags = 3;
+ header |= (level_flags << 6);
+ if (s->strstart != 0) header |= PRESET_DICT;
+ header += 31 - (header % 31);
+
+ putShortMSB(s, header);
+
+ /* Save the adler32 of the preset dictionary: */
+ if (s->strstart != 0) {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ strm->adler = adler32(0L, Z_NULL, 0);
+ s->status = BUSY_STATE;
+
+ /* Compression must start with an empty pending buffer */
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ }
+#ifdef GZIP
+ if (s->status == GZIP_STATE) {
+ /* gzip header */
+ strm->adler = crc32(0L, Z_NULL, 0);
+ put_byte(s, 31);
+ put_byte(s, 139);
+ put_byte(s, 8);
+ if (s->gzhead == Z_NULL) {
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, OS_CODE);
+ s->status = BUSY_STATE;
+
+ /* Compression must start with an empty pending buffer */
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ }
+ else {
+ put_byte(s, (s->gzhead->text ? 1 : 0) +
+ (s->gzhead->hcrc ? 2 : 0) +
+ (s->gzhead->extra == Z_NULL ? 0 : 4) +
+ (s->gzhead->name == Z_NULL ? 0 : 8) +
+ (s->gzhead->comment == Z_NULL ? 0 : 16)
+ );
+ put_byte(s, (Byte)(s->gzhead->time & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, s->gzhead->os & 0xff);
+ if (s->gzhead->extra != Z_NULL) {
+ put_byte(s, s->gzhead->extra_len & 0xff);
+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
+ }
+ if (s->gzhead->hcrc)
+ strm->adler = crc32(strm->adler, s->pending_buf,
+ s->pending);
+ s->gzindex = 0;
+ s->status = EXTRA_STATE;
+ }
+ }
+ if (s->status == EXTRA_STATE) {
+ if (s->gzhead->extra != Z_NULL) {
+ ulg beg = s->pending; /* start of bytes to update crc */
+ uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
+ while (s->pending + left > s->pending_buf_size) {
+ uInt copy = s->pending_buf_size - s->pending;
+ zmemcpy(s->pending_buf + s->pending,
+ s->gzhead->extra + s->gzindex, copy);
+ s->pending = s->pending_buf_size;
+ HCRC_UPDATE(beg);
+ s->gzindex += copy;
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ beg = 0;
+ left -= copy;
+ }
+ zmemcpy(s->pending_buf + s->pending,
+ s->gzhead->extra + s->gzindex, left);
+ s->pending += left;
+ HCRC_UPDATE(beg);
+ s->gzindex = 0;
+ }
+ s->status = NAME_STATE;
+ }
+ if (s->status == NAME_STATE) {
+ if (s->gzhead->name != Z_NULL) {
+ ulg beg = s->pending; /* start of bytes to update crc */
+ int val;
+ do {
+ if (s->pending == s->pending_buf_size) {
+ HCRC_UPDATE(beg);
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ beg = 0;
+ }
+ val = s->gzhead->name[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ HCRC_UPDATE(beg);
+ s->gzindex = 0;
+ }
+ s->status = COMMENT_STATE;
+ }
+ if (s->status == COMMENT_STATE) {
+ if (s->gzhead->comment != Z_NULL) {
+ ulg beg = s->pending; /* start of bytes to update crc */
+ int val;
+ do {
+ if (s->pending == s->pending_buf_size) {
+ HCRC_UPDATE(beg);
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ beg = 0;
+ }
+ val = s->gzhead->comment[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ HCRC_UPDATE(beg);
+ }
+ s->status = HCRC_STATE;
+ }
+ if (s->status == HCRC_STATE) {
+ if (s->gzhead->hcrc) {
+ if (s->pending + 2 > s->pending_buf_size) {
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ }
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ strm->adler = crc32(0L, Z_NULL, 0);
+ }
+ s->status = BUSY_STATE;
+
+ /* Compression must start with an empty pending buffer */
+ flush_pending(strm);
+ if (s->pending != 0) {
+ s->last_flush = -1;
+ return Z_OK;
+ }
+ }
+#endif
+
+ /* Start a new block or continue the current one.
+ */
+ if (strm->avail_in != 0 || s->lookahead != 0 ||
+ (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
+ block_state bstate;
+
+ bstate = s->level == 0 ? deflate_stored(s, flush) :
+ s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
+ s->strategy == Z_RLE ? deflate_rle(s, flush) :
+ (*(configuration_table[s->level].func))(s, flush);
+
+ if (bstate == finish_started || bstate == finish_done) {
+ s->status = FINISH_STATE;
+ }
+ if (bstate == need_more || bstate == finish_started) {
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
+ }
+ return Z_OK;
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
+ * of deflate should use the same flush parameter to make sure
+ * that the flush is complete. So we don't have to output an
+ * empty block here, this will be done at next call. This also
+ * ensures that for a very small output buffer, we emit at most
+ * one empty block.
+ */
+ }
+ if (bstate == block_done) {
+ if (flush == Z_PARTIAL_FLUSH) {
+ _tr_align(s);
+ } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
+ _tr_stored_block(s, (char*)0, 0L, 0);
+ /* For a full flush, this empty block will be recognized
+ * as a special marker by inflate_sync().
+ */
+ if (flush == Z_FULL_FLUSH) {
+ CLEAR_HASH(s); /* forget history */
+ if (s->lookahead == 0) {
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->insert = 0;
+ }
+ }
+ }
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
+ return Z_OK;
+ }
+ }
+ }
+
+ if (flush != Z_FINISH) return Z_OK;
+ if (s->wrap <= 0) return Z_STREAM_END;
+
+ /* Write the trailer */
+#ifdef GZIP
+ if (s->wrap == 2) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
+ put_byte(s, (Byte)(strm->total_in & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
+ }
+ else
+#endif
+ {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ flush_pending(strm);
+ /* If avail_out is zero, the application will call deflate again
+ * to flush the rest.
+ */
+ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
+ return s->pending != 0 ? Z_OK : Z_STREAM_END;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateEnd (strm)
+ z_streamp strm;
+{
+ int status;
+
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
+
+ status = strm->state->status;
+
+ /* Deallocate in reverse order of allocations: */
+ TRY_FREE(strm, strm->state->pending_buf);
+ TRY_FREE(strm, strm->state->head);
+ TRY_FREE(strm, strm->state->prev);
+ TRY_FREE(strm, strm->state->window);
+
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+
+ return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
+}
+
+/* =========================================================================
+ * Copy the source state to the destination state.
+ * To simplify the source, this is not supported for 16-bit MSDOS (which
+ * doesn't have enough memory anyway to duplicate compression states).
+ */
+int ZEXPORT deflateCopy (dest, source)
+ z_streamp dest;
+ z_streamp source;
+{
+#ifdef MAXSEG_64K
+ return Z_STREAM_ERROR;
+#else
+ deflate_state *ds;
+ deflate_state *ss;
+ ushf *overlay;
+
+
+ if (deflateStateCheck(source) || dest == Z_NULL) {
+ return Z_STREAM_ERROR;
+ }
+
+ ss = source->state;
+
+ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+
+ ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
+ if (ds == Z_NULL) return Z_MEM_ERROR;
+ dest->state = (struct internal_state FAR *) ds;
+ zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
+ ds->strm = dest;
+
+ ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
+ ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
+ ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
+ overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
+ ds->pending_buf = (uchf *) overlay;
+
+ if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
+ ds->pending_buf == Z_NULL) {
+ deflateEnd (dest);
+ return Z_MEM_ERROR;
+ }
+ /* following zmemcpy do not work for 16-bit MSDOS */
+ zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
+ zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
+ zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
+ zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
+
+ ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
+ ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
+ ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
+
+ ds->l_desc.dyn_tree = ds->dyn_ltree;
+ ds->d_desc.dyn_tree = ds->dyn_dtree;
+ ds->bl_desc.dyn_tree = ds->bl_tree;
+
+ return Z_OK;
+#endif /* MAXSEG_64K */
+}
+
+/* ===========================================================================
+ * Read a new buffer from the current input stream, update the adler32
+ * and total number of bytes read. All deflate() input goes through
+ * this function so some applications may wish to modify it to avoid
+ * allocating a large strm->next_in buffer and copying from it.
+ * (See also flush_pending()).
+ */
+local unsigned read_buf(strm, buf, size)
+ z_streamp strm;
+ Bytef *buf;
+ unsigned size;
+{
+ unsigned len = strm->avail_in;
+
+ if (len > size) len = size;
+ if (len == 0) return 0;
+
+ strm->avail_in -= len;
+
+ zmemcpy(buf, strm->next_in, len);
+ if (strm->state->wrap == 1) {
+ strm->adler = adler32(strm->adler, buf, len);
+ }
+#ifdef GZIP
+ else if (strm->state->wrap == 2) {
+ strm->adler = crc32(strm->adler, buf, len);
+ }
+#endif
+ strm->next_in += len;
+ strm->total_in += len;
+
+ return len;
+}
+
+/* ===========================================================================
+ * Initialize the "longest match" routines for a new zlib stream
+ */
+local void lm_init (s)
+ deflate_state *s;
+{
+ s->window_size = (ulg)2L*s->w_size;
+
+ CLEAR_HASH(s);
+
+ /* Set the default configuration parameters:
+ */
+ s->max_lazy_match = configuration_table[s->level].max_lazy;
+ s->good_match = configuration_table[s->level].good_length;
+ s->nice_match = configuration_table[s->level].nice_length;
+ s->max_chain_length = configuration_table[s->level].max_chain;
+
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->lookahead = 0;
+ s->insert = 0;
+ s->match_length = s->prev_length = MIN_MATCH-1;
+ s->match_available = 0;
+ s->ins_h = 0;
+#ifndef FASTEST
+#ifdef ASMV
+ match_init(); /* initialize the asm code */
+#endif
+#endif
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Set match_start to the longest match starting at the given string and
+ * return its length. Matches shorter or equal to prev_length are discarded,
+ * in which case the result is equal to prev_length and match_start is
+ * garbage.
+ * IN assertions: cur_match is the head of the hash chain for the current
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
+ * OUT assertion: the match length is not greater than s->lookahead.
+ */
+#ifndef ASMV
+/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
+ * match.S. The code will be functionally equivalent.
+ */
+local uInt longest_match(s, cur_match)
+ deflate_state *s;
+ IPos cur_match; /* current match */
+{
+ unsigned chain_length = s->max_chain_length;/* max hash chain length */
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ int best_len = (int)s->prev_length; /* best match length so far */
+ int nice_match = s->nice_match; /* stop if match long enough */
+ IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
+ s->strstart - (IPos)MAX_DIST(s) : NIL;
+ /* Stop when cur_match becomes <= limit. To simplify the code,
+ * we prevent matches with the string of window index 0.
+ */
+ Posf *prev = s->prev;
+ uInt wmask = s->w_mask;
+
+#ifdef UNALIGNED_OK
+ /* Compare two bytes at a time. Note: this is not always beneficial.
+ * Try with and without -DUNALIGNED_OK to check.
+ */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
+ register ush scan_start = *(ushf*)scan;
+ register ush scan_end = *(ushf*)(scan+best_len-1);
+#else
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+ register Byte scan_end1 = scan[best_len-1];
+ register Byte scan_end = scan[best_len];
+#endif
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ /* Do not waste too much time if we already have a good match: */
+ if (s->prev_length >= s->good_match) {
+ chain_length >>= 2;
+ }
+ /* Do not look for matches beyond the end of the input. This is necessary
+ * to make deflate deterministic.
+ */
+ if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ do {
+ Assert(cur_match < s->strstart, "no future");
+ match = s->window + cur_match;
+
+ /* Skip to next match if the match length cannot increase
+ * or if the match length is less than 2. Note that the checks below
+ * for insufficient lookahead only occur occasionally for performance
+ * reasons. Therefore uninitialized memory will be accessed, and
+ * conditional jumps will be made that depend on those values.
+ * However the length of the match is limited to the lookahead, so
+ * the output of deflate is not affected by the uninitialized values.
+ */
+#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
+ /* This code assumes sizeof(unsigned short) == 2. Do not use
+ * UNALIGNED_OK if your compiler uses a different size.
+ */
+ if (*(ushf*)(match+best_len-1) != scan_end ||
+ *(ushf*)match != scan_start) continue;
+
+ /* It is not necessary to compare scan[2] and match[2] since they are
+ * always equal when the other bytes match, given that the hash keys
+ * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
+ * strstart+3, +5, ... up to strstart+257. We check for insufficient
+ * lookahead only every 4th comparison; the 128th check will be made
+ * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
+ * necessary to put more guard bytes at the end of the window, or
+ * to check more often for insufficient lookahead.
+ */
+ Assert(scan[2] == match[2], "scan[2]?");
+ scan++, match++;
+ do {
+ } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ scan < strend);
+ /* The funny "do {}" generates better code on most compilers */
+
+ /* Here, scan <= window+strstart+257 */
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+ if (*scan == *match) scan++;
+
+ len = (MAX_MATCH - 1) - (int)(strend-scan);
+ scan = strend - (MAX_MATCH-1);
+
+#else /* UNALIGNED_OK */
+
+ if (match[best_len] != scan_end ||
+ match[best_len-1] != scan_end1 ||
+ *match != *scan ||
+ *++match != scan[1]) continue;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match++;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+ scan = strend - MAX_MATCH;
+
+#endif /* UNALIGNED_OK */
+
+ if (len > best_len) {
+ s->match_start = cur_match;
+ best_len = len;
+ if (len >= nice_match) break;
+#ifdef UNALIGNED_OK
+ scan_end = *(ushf*)(scan+best_len-1);
+#else
+ scan_end1 = scan[best_len-1];
+ scan_end = scan[best_len];
+#endif
+ }
+ } while ((cur_match = prev[cur_match & wmask]) > limit
+ && --chain_length != 0);
+
+ if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
+ return s->lookahead;
+}
+#endif /* ASMV */
+
+#else /* FASTEST */
+
+/* ---------------------------------------------------------------------------
+ * Optimized version for FASTEST only
+ */
+local uInt longest_match(s, cur_match)
+ deflate_state *s;
+ IPos cur_match; /* current match */
+{
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ Assert(cur_match < s->strstart, "no future");
+
+ match = s->window + cur_match;
+
+ /* Return failure if the match length is less than 2:
+ */
+ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match += 2;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+
+ if (len < MIN_MATCH) return MIN_MATCH - 1;
+
+ s->match_start = cur_match;
+ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
+}
+
+#endif /* FASTEST */
+
+#ifdef ZLIB_DEBUG
+
+#define EQUAL 0
+/* result of memcmp for equal strings */
+
+/* ===========================================================================
+ * Check that the match at match_start is indeed a match.
+ */
+local void check_match(s, start, match, length)
+ deflate_state *s;
+ IPos start, match;
+ int length;
+{
+ /* check that the match is indeed a match */
+ if (zmemcmp(s->window + match,
+ s->window + start, length) != EQUAL) {
+ fprintf(stderr, " start %u, match %u, length %d\n",
+ start, match, length);
+ do {
+ fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
+ } while (--length != 0);
+ z_error("invalid match");
+ }
+ if (z_verbose > 1) {
+ fprintf(stderr,"\\[%d,%d]", start-match, length);
+ do { putc(s->window[start++], stderr); } while (--length != 0);
+ }
+}
+#else
+# define check_match(s, start, match, length)
+#endif /* ZLIB_DEBUG */
+
+/* ===========================================================================
+ * Fill the window when the lookahead becomes insufficient.
+ * Updates strstart and lookahead.
+ *
+ * IN assertion: lookahead < MIN_LOOKAHEAD
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
+ * At least one byte has been read, or avail_in == 0; reads are
+ * performed for at least two bytes (required for the zip translate_eol
+ * option -- not supported here).
+ */
+local void fill_window(s)
+ deflate_state *s;
+{
+ unsigned n;
+ unsigned more; /* Amount of free space at the end of the window. */
+ uInt wsize = s->w_size;
+
+ Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
+
+ do {
+ more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
+
+ /* Deal with !@#$% 64K limit: */
+ if (sizeof(int) <= 2) {
+ if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
+ more = wsize;
+
+ } else if (more == (unsigned)(-1)) {
+ /* Very unlikely, but possible on 16 bit machine if
+ * strstart == 0 && lookahead == 1 (input done a byte at time)
+ */
+ more--;
+ }
+ }
+
+ /* If the window is almost full and there is insufficient lookahead,
+ * move the upper half to the lower one to make room in the upper half.
+ */
+ if (s->strstart >= wsize+MAX_DIST(s)) {
+
+ zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
+ s->match_start -= wsize;
+ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
+ s->block_start -= (long) wsize;
+ slide_hash(s);
+ more += wsize;
+ }
+ if (s->strm->avail_in == 0) break;
+
+ /* If there was no sliding:
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
+ * more == window_size - lookahead - strstart
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
+ * => more >= window_size - 2*WSIZE + 2
+ * In the BIG_MEM or MMAP case (not yet supported),
+ * window_size == input_size + MIN_LOOKAHEAD &&
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
+ */
+ Assert(more >= 2, "more < 2");
+
+ n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
+ s->lookahead += n;
+
+ /* Initialize the hash value now that we have some input: */
+ if (s->lookahead + s->insert >= MIN_MATCH) {
+ uInt str = s->strstart - s->insert;
+ s->ins_h = s->window[str];
+ UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ while (s->insert) {
+ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+ s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+ s->head[s->ins_h] = (Pos)str;
+ str++;
+ s->insert--;
+ if (s->lookahead + s->insert < MIN_MATCH)
+ break;
+ }
+ }
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
+ * but this is not important since only literal bytes will be emitted.
+ */
+
+ } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+
+ /* If the WIN_INIT bytes after the end of the current data have never been
+ * written, then zero those bytes in order to avoid memory check reports of
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
+ * the longest match routines. Update the high water mark for the next
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
+ */
+ if (s->high_water < s->window_size) {
+ ulg curr = s->strstart + (ulg)(s->lookahead);
+ ulg init;
+
+ if (s->high_water < curr) {
+ /* Previous high water mark below current data -- zero WIN_INIT
+ * bytes or up to end of window, whichever is less.
+ */
+ init = s->window_size - curr;
+ if (init > WIN_INIT)
+ init = WIN_INIT;
+ zmemzero(s->window + curr, (unsigned)init);
+ s->high_water = curr + init;
+ }
+ else if (s->high_water < (ulg)curr + WIN_INIT) {
+ /* High water mark at or above current data, but below current data
+ * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
+ * to end of window, whichever is less.
+ */
+ init = (ulg)curr + WIN_INIT - s->high_water;
+ if (init > s->window_size - s->high_water)
+ init = s->window_size - s->high_water;
+ zmemzero(s->window + s->high_water, (unsigned)init);
+ s->high_water += init;
+ }
+ }
+
+ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
+ "not enough room for search");
+}
+
+/* ===========================================================================
+ * Flush the current block, with given end-of-file flag.
+ * IN assertion: strstart is set to the end of the current match.
+ */
+#define FLUSH_BLOCK_ONLY(s, last) { \
+ _tr_flush_block(s, (s->block_start >= 0L ? \
+ (charf *)&s->window[(unsigned)s->block_start] : \
+ (charf *)Z_NULL), \
+ (ulg)((long)s->strstart - s->block_start), \
+ (last)); \
+ s->block_start = s->strstart; \
+ flush_pending(s->strm); \
+ Tracev((stderr,"[FLUSH]")); \
+}
+
+/* Same but force premature exit if necessary. */
+#define FLUSH_BLOCK(s, last) { \
+ FLUSH_BLOCK_ONLY(s, last); \
+ if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
+}
+
+/* Maximum stored block length in deflate format (not including header). */
+#define MAX_STORED 65535
+
+/* Minimum of a and b. */
+#define MIN(a, b) ((a) > (b) ? (b) : (a))
+
+/* ===========================================================================
+ * Copy without compression as much as possible from the input stream, return
+ * the current block state.
+ *
+ * In case deflateParams() is used to later switch to a non-zero compression
+ * level, s->matches (otherwise unused when storing) keeps track of the number
+ * of hash table slides to perform. If s->matches is 1, then one hash table
+ * slide will be done when switching. If s->matches is 2, the maximum value
+ * allowed here, then the hash table will be cleared, since two or more slides
+ * is the same as a clear.
+ *
+ * deflate_stored() is written to minimize the number of times an input byte is
+ * copied. It is most efficient with large input and output buffers, which
+ * maximizes the opportunites to have a single copy from next_in to next_out.
+ */
+local block_state deflate_stored(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ /* Smallest worthy block size when not flushing or finishing. By default
+ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
+ * large input and output buffers, the stored block size will be larger.
+ */
+ unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
+
+ /* Copy as many min_block or larger stored blocks directly to next_out as
+ * possible. If flushing, copy the remaining available input to next_out as
+ * stored blocks, if there is enough space.
+ */
+ unsigned len, left, have, last = 0;
+ unsigned used = s->strm->avail_in;
+ do {
+ /* Set len to the maximum size block that we can copy directly with the
+ * available input data and output space. Set left to how much of that
+ * would be copied from what's left in the window.
+ */
+ len = MAX_STORED; /* maximum deflate stored block length */
+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
+ if (s->strm->avail_out < have) /* need room for header */
+ break;
+ /* maximum stored block length that will fit in avail_out: */
+ have = s->strm->avail_out - have;
+ left = s->strstart - s->block_start; /* bytes left in window */
+ if (len > (ulg)left + s->strm->avail_in)
+ len = left + s->strm->avail_in; /* limit len to the input */
+ if (len > have)
+ len = have; /* limit len to the output */
+
+ /* If the stored block would be less than min_block in length, or if
+ * unable to copy all of the available input when flushing, then try
+ * copying to the window and the pending buffer instead. Also don't
+ * write an empty block when flushing -- deflate() does that.
+ */
+ if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
+ flush == Z_NO_FLUSH ||
+ len != left + s->strm->avail_in))
+ break;
+
+ /* Make a dummy stored block in pending to get the header bytes,
+ * including any pending bits. This also updates the debugging counts.
+ */
+ last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
+ _tr_stored_block(s, (char *)0, 0L, last);
+
+ /* Replace the lengths in the dummy stored block with len. */
+ s->pending_buf[s->pending - 4] = len;
+ s->pending_buf[s->pending - 3] = len >> 8;
+ s->pending_buf[s->pending - 2] = ~len;
+ s->pending_buf[s->pending - 1] = ~len >> 8;
+
+ /* Write the stored block header bytes. */
+ flush_pending(s->strm);
+
+#ifdef ZLIB_DEBUG
+ /* Update debugging counts for the data about to be copied. */
+ s->compressed_len += len << 3;
+ s->bits_sent += len << 3;
+#endif
+
+ /* Copy uncompressed bytes from the window to next_out. */
+ if (left) {
+ if (left > len)
+ left = len;
+ zmemcpy(s->strm->next_out, s->window + s->block_start, left);
+ s->strm->next_out += left;
+ s->strm->avail_out -= left;
+ s->strm->total_out += left;
+ s->block_start += left;
+ len -= left;
+ }
+
+ /* Copy uncompressed bytes directly from next_in to next_out, updating
+ * the check value.
+ */
+ if (len) {
+ read_buf(s->strm, s->strm->next_out, len);
+ s->strm->next_out += len;
+ s->strm->avail_out -= len;
+ s->strm->total_out += len;
+ }
+ } while (last == 0);
+
+ /* Update the sliding window with the last s->w_size bytes of the copied
+ * data, or append all of the copied data to the existing window if less
+ * than s->w_size bytes were copied. Also update the number of bytes to
+ * insert in the hash tables, in the event that deflateParams() switches to
+ * a non-zero compression level.
+ */
+ used -= s->strm->avail_in; /* number of input bytes directly copied */
+ if (used) {
+ /* If any input was used, then no unused input remains in the window,
+ * therefore s->block_start == s->strstart.
+ */
+ if (used >= s->w_size) { /* supplant the previous history */
+ s->matches = 2; /* clear hash */
+ zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
+ s->strstart = s->w_size;
+ }
+ else {
+ if (s->window_size - s->strstart <= used) {
+ /* Slide the window down. */
+ s->strstart -= s->w_size;
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
+ if (s->matches < 2)
+ s->matches++; /* add a pending slide_hash() */
+ }
+ zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
+ s->strstart += used;
+ }
+ s->block_start = s->strstart;
+ s->insert += MIN(used, s->w_size - s->insert);
+ }
+ if (s->high_water < s->strstart)
+ s->high_water = s->strstart;
+
+ /* If the last block was written to next_out, then done. */
+ if (last)
+ return finish_done;
+
+ /* If flushing and all input has been consumed, then done. */
+ if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
+ s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
+ return block_done;
+
+ /* Fill the window with any remaining input. */
+ have = s->window_size - s->strstart - 1;
+ if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
+ /* Slide the window down. */
+ s->block_start -= s->w_size;
+ s->strstart -= s->w_size;
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
+ if (s->matches < 2)
+ s->matches++; /* add a pending slide_hash() */
+ have += s->w_size; /* more space now */
+ }
+ if (have > s->strm->avail_in)
+ have = s->strm->avail_in;
+ if (have) {
+ read_buf(s->strm, s->window + s->strstart, have);
+ s->strstart += have;
+ }
+ if (s->high_water < s->strstart)
+ s->high_water = s->strstart;
+
+ /* There was not enough avail_out to write a complete worthy or flushed
+ * stored block to next_out. Write a stored block to pending instead, if we
+ * have enough input for a worthy block, or if flushing and there is enough
+ * room for the remaining input as a stored block in the pending buffer.
+ */
+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
+ /* maximum stored block length that will fit in pending: */
+ have = MIN(s->pending_buf_size - have, MAX_STORED);
+ min_block = MIN(have, s->w_size);
+ left = s->strstart - s->block_start;
+ if (left >= min_block ||
+ ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
+ s->strm->avail_in == 0 && left <= have)) {
+ len = MIN(left, have);
+ last = flush == Z_FINISH && s->strm->avail_in == 0 &&
+ len == left ? 1 : 0;
+ _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
+ s->block_start += len;
+ flush_pending(s->strm);
+ }
+
+ /* We've done all we can with the available input and output. */
+ return last ? finish_started : need_more;
+}
+
+/* ===========================================================================
+ * Compress as much as possible from the input stream, return the current
+ * block state.
+ * This function does not perform lazy evaluation of matches and inserts
+ * new strings in the dictionary only for unmatched strings or for short
+ * matches. It is used only for the fast compression options.
+ */
+local block_state deflate_fast(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ IPos hash_head; /* head of the hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ * At this point we have always match_length < MIN_MATCH
+ */
+ if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+ }
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->match_start, s->match_length);
+
+ _tr_tally_dist(s, s->strstart - s->match_start,
+ s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+
+ /* Insert new strings in the hash table only if the match length
+ * is not too large. This saves time but degrades compression.
+ */
+#ifndef FASTEST
+ if (s->match_length <= s->max_insert_length &&
+ s->lookahead >= MIN_MATCH) {
+ s->match_length--; /* string at strstart already in table */
+ do {
+ s->strstart++;
+ INSERT_STRING(s, s->strstart, hash_head);
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+ * always MIN_MATCH bytes ahead.
+ */
+ } while (--s->match_length != 0);
+ s->strstart++;
+ } else
+#endif
+ {
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ s->ins_h = s->window[s->strstart];
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
+ * matter since it will be recomputed at next deflate call.
+ */
+ }
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Same as above, but achieves better compression. We use a lazy
+ * evaluation for matches: a match is finally adopted only if there is
+ * no better match at the next window position.
+ */
+local block_state deflate_slow(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ IPos hash_head; /* head of hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ /* Process the input block. */
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ */
+ s->prev_length = s->match_length, s->prev_match = s->match_start;
+ s->match_length = MIN_MATCH-1;
+
+ if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
+ s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+
+ if (s->match_length <= 5 && (s->strategy == Z_FILTERED
+#if TOO_FAR <= 32767
+ || (s->match_length == MIN_MATCH &&
+ s->strstart - s->match_start > TOO_FAR)
+#endif
+ )) {
+
+ /* If prev_match is also MIN_MATCH, match_start is garbage
+ * but we will ignore the current match anyway.
+ */
+ s->match_length = MIN_MATCH-1;
+ }
+ }
+ /* If there was a match at the previous step and the current
+ * match is not better, output the previous match:
+ */
+ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
+ uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
+ /* Do not insert strings in hash table beyond this. */
+
+ check_match(s, s->strstart-1, s->prev_match, s->prev_length);
+
+ _tr_tally_dist(s, s->strstart -1 - s->prev_match,
+ s->prev_length - MIN_MATCH, bflush);
+
+ /* Insert in hash table all strings up to the end of the match.
+ * strstart-1 and strstart are already inserted. If there is not
+ * enough lookahead, the last two strings are not inserted in
+ * the hash table.
+ */
+ s->lookahead -= s->prev_length-1;
+ s->prev_length -= 2;
+ do {
+ if (++s->strstart <= max_insert) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+ } while (--s->prev_length != 0);
+ s->match_available = 0;
+ s->match_length = MIN_MATCH-1;
+ s->strstart++;
+
+ if (bflush) FLUSH_BLOCK(s, 0);
+
+ } else if (s->match_available) {
+ /* If there was no match at the previous position, output a
+ * single literal. If there was a match but the current match
+ * is longer, truncate the previous match to a single literal.
+ */
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ if (bflush) {
+ FLUSH_BLOCK_ONLY(s, 0);
+ }
+ s->strstart++;
+ s->lookahead--;
+ if (s->strm->avail_out == 0) return need_more;
+ } else {
+ /* There is no previous match to compare with, wait for
+ * the next step to decide.
+ */
+ s->match_available = 1;
+ s->strstart++;
+ s->lookahead--;
+ }
+ }
+ Assert (flush != Z_NO_FLUSH, "no flush?");
+ if (s->match_available) {
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ s->match_available = 0;
+ }
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+#endif /* FASTEST */
+
+/* ===========================================================================
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
+ * deflate switches away from Z_RLE.)
+ */
+local block_state deflate_rle(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ int bflush; /* set if current block must be flushed */
+ uInt prev; /* byte at distance one to match */
+ Bytef *scan, *strend; /* scan goes up to strend for length of run */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the longest run, plus one for the unrolled loop.
+ */
+ if (s->lookahead <= MAX_MATCH) {
+ fill_window(s);
+ if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* See how many times the previous byte repeats */
+ s->match_length = 0;
+ if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
+ scan = s->window + s->strstart - 1;
+ prev = *scan;
+ if (prev == *++scan && prev == *++scan && prev == *++scan) {
+ strend = s->window + s->strstart + MAX_MATCH;
+ do {
+ } while (prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ scan < strend);
+ s->match_length = MAX_MATCH - (uInt)(strend - scan);
+ if (s->match_length > s->lookahead)
+ s->match_length = s->lookahead;
+ }
+ Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
+ }
+
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->strstart - 1, s->match_length);
+
+ _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = 0;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+
+/* ===========================================================================
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
+ */
+local block_state deflate_huff(s, flush)
+ deflate_state *s;
+ int flush;
+{
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we have a literal to write. */
+ if (s->lookahead == 0) {
+ fill_window(s);
+ if (s->lookahead == 0) {
+ if (flush == Z_NO_FLUSH)
+ return need_more;
+ break; /* flush the current block */
+ }
+ }
+
+ /* Output a literal byte */
+ s->match_length = 0;
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = 0;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
diff --git a/test/monniaux/zlib-1.2.11/deflate.h b/test/monniaux/zlib-1.2.11/deflate.h
new file mode 100644
index 00000000..23ecdd31
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/deflate.h
@@ -0,0 +1,349 @@
+/* deflate.h -- internal compression state
+ * Copyright (C) 1995-2016 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* @(#) $Id$ */
+
+#ifndef DEFLATE_H
+#define DEFLATE_H
+
+#include "zutil.h"
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer creation by deflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip encoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GZIP
+#endif
+
+/* ===========================================================================
+ * Internal compression state.
+ */
+
+#define LENGTH_CODES 29
+/* number of length codes, not counting the special END_BLOCK code */
+
+#define LITERALS 256
+/* number of literal bytes 0..255 */
+
+#define L_CODES (LITERALS+1+LENGTH_CODES)
+/* number of Literal or Length codes, including the END_BLOCK code */
+
+#define D_CODES 30
+/* number of distance codes */
+
+#define BL_CODES 19
+/* number of codes used to transfer the bit lengths */
+
+#define HEAP_SIZE (2*L_CODES+1)
+/* maximum heap size */
+
+#define MAX_BITS 15
+/* All codes must not exceed MAX_BITS bits */
+
+#define Buf_size 16
+/* size of bit buffer in bi_buf */
+
+#define INIT_STATE 42 /* zlib header -> BUSY_STATE */
+#ifdef GZIP
+# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */
+#endif
+#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */
+#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */
+#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */
+#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */
+#define BUSY_STATE 113 /* deflate -> FINISH_STATE */
+#define FINISH_STATE 666 /* stream complete */
+/* Stream status */
+
+
+/* Data structure describing a single value and its code string. */
+typedef struct ct_data_s {
+ union {
+ ush freq; /* frequency count */
+ ush code; /* bit string */
+ } fc;
+ union {
+ ush dad; /* father node in Huffman tree */
+ ush len; /* length of bit string */
+ } dl;
+} FAR ct_data;
+
+#define Freq fc.freq
+#define Code fc.code
+#define Dad dl.dad
+#define Len dl.len
+
+typedef struct static_tree_desc_s static_tree_desc;
+
+typedef struct tree_desc_s {
+ ct_data *dyn_tree; /* the dynamic tree */
+ int max_code; /* largest code with non zero frequency */
+ const static_tree_desc *stat_desc; /* the corresponding static tree */
+} FAR tree_desc;
+
+typedef ush Pos;
+typedef Pos FAR Posf;
+typedef unsigned IPos;
+
+/* A Pos is an index in the character window. We use short instead of int to
+ * save space in the various tables. IPos is used only for parameter passing.
+ */
+
+typedef struct internal_state {
+ z_streamp strm; /* pointer back to this zlib stream */
+ int status; /* as the name implies */
+ Bytef *pending_buf; /* output still pending */
+ ulg pending_buf_size; /* size of pending_buf */
+ Bytef *pending_out; /* next pending byte to output to the stream */
+ ulg pending; /* nb of bytes in the pending buffer */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
+ gz_headerp gzhead; /* gzip header information to write */
+ ulg gzindex; /* where in extra, name, or comment */
+ Byte method; /* can only be DEFLATED */
+ int last_flush; /* value of flush param for previous deflate call */
+
+ /* used by deflate.c: */
+
+ uInt w_size; /* LZ77 window size (32K by default) */
+ uInt w_bits; /* log2(w_size) (8..16) */
+ uInt w_mask; /* w_size - 1 */
+
+ Bytef *window;
+ /* Sliding window. Input bytes are read into the second half of the window,
+ * and move to the first half later to keep a dictionary of at least wSize
+ * bytes. With this organization, matches are limited to a distance of
+ * wSize-MAX_MATCH bytes, but this ensures that IO is always
+ * performed with a length multiple of the block size. Also, it limits
+ * the window size to 64K, which is quite useful on MSDOS.
+ * To do: use the user input buffer as sliding window.
+ */
+
+ ulg window_size;
+ /* Actual size of window: 2*wSize, except when the user input buffer
+ * is directly used as sliding window.
+ */
+
+ Posf *prev;
+ /* Link to older string with same hash index. To limit the size of this
+ * array to 64K, this link is maintained only for the last 32K strings.
+ * An index in this array is thus a window index modulo 32K.
+ */
+
+ Posf *head; /* Heads of the hash chains or NIL. */
+
+ uInt ins_h; /* hash index of string to be inserted */
+ uInt hash_size; /* number of elements in hash table */
+ uInt hash_bits; /* log2(hash_size) */
+ uInt hash_mask; /* hash_size-1 */
+
+ uInt hash_shift;
+ /* Number of bits by which ins_h must be shifted at each input
+ * step. It must be such that after MIN_MATCH steps, the oldest
+ * byte no longer takes part in the hash key, that is:
+ * hash_shift * MIN_MATCH >= hash_bits
+ */
+
+ long block_start;
+ /* Window position at the beginning of the current output block. Gets
+ * negative when the window is moved backwards.
+ */
+
+ uInt match_length; /* length of best match */
+ IPos prev_match; /* previous match */
+ int match_available; /* set if previous match exists */
+ uInt strstart; /* start of string to insert */
+ uInt match_start; /* start of matching string */
+ uInt lookahead; /* number of valid bytes ahead in window */
+
+ uInt prev_length;
+ /* Length of the best match at previous step. Matches not greater than this
+ * are discarded. This is used in the lazy match evaluation.
+ */
+
+ uInt max_chain_length;
+ /* To speed up deflation, hash chains are never searched beyond this
+ * length. A higher limit improves compression ratio but degrades the
+ * speed.
+ */
+
+ uInt max_lazy_match;
+ /* Attempt to find a better match only when the current match is strictly
+ * smaller than this value. This mechanism is used only for compression
+ * levels >= 4.
+ */
+# define max_insert_length max_lazy_match
+ /* Insert new strings in the hash table only if the match length is not
+ * greater than this length. This saves time but degrades compression.
+ * max_insert_length is used only for compression levels <= 3.
+ */
+
+ int level; /* compression level (1..9) */
+ int strategy; /* favor or force Huffman coding*/
+
+ uInt good_match;
+ /* Use a faster search when the previous match is longer than this */
+
+ int nice_match; /* Stop searching when current match exceeds this */
+
+ /* used by trees.c: */
+ /* Didn't use ct_data typedef below to suppress compiler warning */
+ struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
+ struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
+ struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
+
+ struct tree_desc_s l_desc; /* desc. for literal tree */
+ struct tree_desc_s d_desc; /* desc. for distance tree */
+ struct tree_desc_s bl_desc; /* desc. for bit length tree */
+
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
+ int heap_len; /* number of elements in the heap */
+ int heap_max; /* element of largest frequency */
+ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
+ * The same heap array is used to build all trees.
+ */
+
+ uch depth[2*L_CODES+1];
+ /* Depth of each subtree used as tie breaker for trees of equal frequency
+ */
+
+ uchf *l_buf; /* buffer for literals or lengths */
+
+ uInt lit_bufsize;
+ /* Size of match buffer for literals/lengths. There are 4 reasons for
+ * limiting lit_bufsize to 64K:
+ * - frequencies can be kept in 16 bit counters
+ * - if compression is not successful for the first block, all input
+ * data is still in the window so we can still emit a stored block even
+ * when input comes from standard input. (This can also be done for
+ * all blocks if lit_bufsize is not greater than 32K.)
+ * - if compression is not successful for a file smaller than 64K, we can
+ * even emit a stored file instead of a stored block (saving 5 bytes).
+ * This is applicable only for zip (not gzip or zlib).
+ * - creating new Huffman trees less frequently may not provide fast
+ * adaptation to changes in the input data statistics. (Take for
+ * example a binary file with poorly compressible code followed by
+ * a highly compressible string table.) Smaller buffer sizes give
+ * fast adaptation but have of course the overhead of transmitting
+ * trees more frequently.
+ * - I can't count above 4
+ */
+
+ uInt last_lit; /* running index in l_buf */
+
+ ushf *d_buf;
+ /* Buffer for distances. To simplify the code, d_buf and l_buf have
+ * the same number of elements. To use different lengths, an extra flag
+ * array would be necessary.
+ */
+
+ ulg opt_len; /* bit length of current block with optimal trees */
+ ulg static_len; /* bit length of current block with static trees */
+ uInt matches; /* number of string matches in current block */
+ uInt insert; /* bytes at end of window left to insert */
+
+#ifdef ZLIB_DEBUG
+ ulg compressed_len; /* total bit length of compressed file mod 2^32 */
+ ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
+#endif
+
+ ush bi_buf;
+ /* Output buffer. bits are inserted starting at the bottom (least
+ * significant bits).
+ */
+ int bi_valid;
+ /* Number of valid bits in bi_buf. All bits above the last valid bit
+ * are always zero.
+ */
+
+ ulg high_water;
+ /* High water mark offset in window for initialized bytes -- bytes above
+ * this are set to zero in order to avoid memory check warnings when
+ * longest match routines access bytes past the input. This is then
+ * updated to the new high water mark.
+ */
+
+} FAR deflate_state;
+
+/* Output a byte on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
+
+
+#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
+/* Minimum amount of lookahead, except at the end of the input file.
+ * See deflate.c for comments about the MIN_MATCH+1.
+ */
+
+#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
+/* In order to simplify the code, particularly on 16 bit machines, match
+ * distances are limited to MAX_DIST instead of WSIZE.
+ */
+
+#define WIN_INIT MAX_MATCH
+/* Number of bytes after end of data in window to initialize in order to avoid
+ memory checker errors from longest match routines */
+
+ /* in trees.c */
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+
+#define d_code(dist) \
+ ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
+/* Mapping from a distance to a distance code. dist is the distance - 1 and
+ * must not have side effects. _dist_code[256] and _dist_code[257] are never
+ * used.
+ */
+
+#ifndef ZLIB_DEBUG
+/* Inline versions of _tr_tally for speed: */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+ extern uch ZLIB_INTERNAL _length_code[];
+ extern uch ZLIB_INTERNAL _dist_code[];
+#else
+ extern const uch ZLIB_INTERNAL _length_code[];
+ extern const uch ZLIB_INTERNAL _dist_code[];
+#endif
+
+# define _tr_tally_lit(s, c, flush) \
+ { uch cc = (c); \
+ s->d_buf[s->last_lit] = 0; \
+ s->l_buf[s->last_lit++] = cc; \
+ s->dyn_ltree[cc].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+# define _tr_tally_dist(s, distance, length, flush) \
+ { uch len = (uch)(length); \
+ ush dist = (ush)(distance); \
+ s->d_buf[s->last_lit] = dist; \
+ s->l_buf[s->last_lit++] = len; \
+ dist--; \
+ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
+ s->dyn_dtree[d_code(dist)].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+#else
+# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
+# define _tr_tally_dist(s, distance, length, flush) \
+ flush = _tr_tally(s, distance, length)
+#endif
+
+#endif /* DEFLATE_H */
diff --git a/test/monniaux/zlib-1.2.11/gzclose.c b/test/monniaux/zlib-1.2.11/gzclose.c
new file mode 100644
index 00000000..caeb99a3
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/gzclose.c
@@ -0,0 +1,25 @@
+/* gzclose.c -- zlib gzclose() function
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* gzclose() is in a separate file so that it is linked in only if it is used.
+ That way the other gzclose functions can be used instead to avoid linking in
+ unneeded compression or decompression routines. */
+int ZEXPORT gzclose(file)
+ gzFile file;
+{
+#ifndef NO_GZCOMPRESS
+ gz_statep state;
+
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
+#else
+ return gzclose_r(file);
+#endif
+}
diff --git a/test/monniaux/zlib-1.2.11/gzguts.h b/test/monniaux/zlib-1.2.11/gzguts.h
new file mode 100644
index 00000000..990a4d25
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/gzguts.h
@@ -0,0 +1,218 @@
+/* gzguts.h -- zlib internal header definitions for gz* operations
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#ifdef _LARGEFILE64_SOURCE
+# ifndef _LARGEFILE_SOURCE
+# define _LARGEFILE_SOURCE 1
+# endif
+# ifdef _FILE_OFFSET_BITS
+# undef _FILE_OFFSET_BITS
+# endif
+#endif
+
+#ifdef HAVE_HIDDEN
+# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+# define ZLIB_INTERNAL
+#endif
+
+#include <stdio.h>
+#include "zlib.h"
+#ifdef STDC
+# include <string.h>
+# include <stdlib.h>
+# include <limits.h>
+#endif
+
+#ifndef _POSIX_SOURCE
+# define _POSIX_SOURCE
+#endif
+#include <fcntl.h>
+
+#ifdef _WIN32
+# include <stddef.h>
+#endif
+
+#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
+# include <io.h>
+#endif
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+# define WIDECHAR
+#endif
+
+#ifdef WINAPI_FAMILY
+# define open _open
+# define read _read
+# define write _write
+# define close _close
+#endif
+
+#ifdef NO_DEFLATE /* for compatibility with old definition */
+# define NO_GZCOMPRESS
+#endif
+
+#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#if defined(__CYGWIN__)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#ifndef HAVE_VSNPRINTF
+# ifdef MSDOS
+/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
+ but for now we just assume it doesn't. */
+# define NO_vsnprintf
+# endif
+# ifdef __TURBOC__
+# define NO_vsnprintf
+# endif
+# ifdef WIN32
+/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
+# if !defined(vsnprintf) && !defined(NO_vsnprintf)
+# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
+# define vsnprintf _vsnprintf
+# endif
+# endif
+# endif
+# ifdef __SASC
+# define NO_vsnprintf
+# endif
+# ifdef VMS
+# define NO_vsnprintf
+# endif
+# ifdef __OS400__
+# define NO_vsnprintf
+# endif
+# ifdef __MVS__
+# define NO_vsnprintf
+# endif
+#endif
+
+/* unlike snprintf (which is required in C99), _snprintf does not guarantee
+ null termination of the result -- however this is only used in gzlib.c where
+ the result is assured to fit in the space provided */
+#if defined(_MSC_VER) && _MSC_VER < 1900
+# define snprintf _snprintf
+#endif
+
+#ifndef local
+# define local static
+#endif
+/* since "static" is used to mean two completely different things in C, we
+ define "local" for the non-static meaning of "static", for readability
+ (compile with -Dlocal if your debugger can't find static symbols) */
+
+/* gz* functions always use library allocation functions */
+#ifndef STDC
+ extern voidp malloc OF((uInt size));
+ extern void free OF((voidpf ptr));
+#endif
+
+/* get errno and strerror definition */
+#if defined UNDER_CE
+# include <windows.h>
+# define zstrerror() gz_strwinerror((DWORD)GetLastError())
+#else
+# ifndef NO_STRERROR
+# include <errno.h>
+# define zstrerror() strerror(errno)
+# else
+# define zstrerror() "stdio error (consult errno)"
+# endif
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+#endif
+
+/* default memLevel */
+#if MAX_MEM_LEVEL >= 8
+# define DEF_MEM_LEVEL 8
+#else
+# define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+
+/* default i/o buffer size -- double this for output when reading (this and
+ twice this must be able to fit in an unsigned type) */
+#define GZBUFSIZE 8192
+
+/* gzip modes, also provide a little integrity check on the passed structure */
+#define GZ_NONE 0
+#define GZ_READ 7247
+#define GZ_WRITE 31153
+#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
+
+/* values for gz_state how */
+#define LOOK 0 /* look for a gzip header */
+#define COPY 1 /* copy input directly */
+#define GZIP 2 /* decompress a gzip stream */
+
+/* internal gzip file state data structure */
+typedef struct {
+ /* exposed contents for gzgetc() macro */
+ struct gzFile_s x; /* "x" for exposed */
+ /* x.have: number of bytes available at x.next */
+ /* x.next: next output data to deliver or write */
+ /* x.pos: current position in uncompressed data */
+ /* used for both reading and writing */
+ int mode; /* see gzip modes above */
+ int fd; /* file descriptor */
+ char *path; /* path or fd for error messages */
+ unsigned size; /* buffer size, zero if not allocated yet */
+ unsigned want; /* requested buffer size, default is GZBUFSIZE */
+ unsigned char *in; /* input buffer (double-sized when writing) */
+ unsigned char *out; /* output buffer (double-sized when reading) */
+ int direct; /* 0 if processing gzip, 1 if transparent */
+ /* just for reading */
+ int how; /* 0: get header, 1: copy, 2: decompress */
+ z_off64_t start; /* where the gzip data started, for rewinding */
+ int eof; /* true if end of input file reached */
+ int past; /* true if read requested past end */
+ /* just for writing */
+ int level; /* compression level */
+ int strategy; /* compression strategy */
+ /* seek request */
+ z_off64_t skip; /* amount to skip (already rewound if backwards) */
+ int seek; /* true if seek request pending */
+ /* error information */
+ int err; /* error code */
+ char *msg; /* error message */
+ /* zlib inflate or deflate stream */
+ z_stream strm; /* stream structure in-place (not a pointer) */
+} gz_state;
+typedef gz_state FAR *gz_statep;
+
+/* shared functions */
+void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
+#if defined UNDER_CE
+char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
+#endif
+
+/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
+ value -- needed when comparing unsigned to z_off64_t, which is signed
+ (possible z_off64_t types off_t, off64_t, and long are all signed) */
+#ifdef INT_MAX
+# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
+#else
+unsigned ZLIB_INTERNAL gz_intmax OF((void));
+# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
+#endif
diff --git a/test/monniaux/zlib-1.2.11/gzlib.c b/test/monniaux/zlib-1.2.11/gzlib.c
new file mode 100644
index 00000000..4105e6af
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/gzlib.c
@@ -0,0 +1,637 @@
+/* gzlib.c -- zlib functions common to reading and writing gzip files
+ * Copyright (C) 2004-2017 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+#if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
+# define LSEEK _lseeki64
+#else
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+# define LSEEK lseek64
+#else
+# define LSEEK lseek
+#endif
+#endif
+
+/* Local functions */
+local void gz_reset OF((gz_statep));
+local gzFile gz_open OF((const void *, int, const char *));
+
+#if defined UNDER_CE
+
+/* Map the Windows error number in ERROR to a locale-dependent error message
+ string and return a pointer to it. Typically, the values for ERROR come
+ from GetLastError.
+
+ The string pointed to shall not be modified by the application, but may be
+ overwritten by a subsequent call to gz_strwinerror
+
+ The gz_strwinerror function does not change the current setting of
+ GetLastError. */
+char ZLIB_INTERNAL *gz_strwinerror (error)
+ DWORD error;
+{
+ static char buf[1024];
+
+ wchar_t *msgbuf;
+ DWORD lasterr = GetLastError();
+ DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL,
+ error,
+ 0, /* Default language */
+ (LPVOID)&msgbuf,
+ 0,
+ NULL);
+ if (chars != 0) {
+ /* If there is an \r\n appended, zap it. */
+ if (chars >= 2
+ && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+ chars -= 2;
+ msgbuf[chars] = 0;
+ }
+
+ if (chars > sizeof (buf) - 1) {
+ chars = sizeof (buf) - 1;
+ msgbuf[chars] = 0;
+ }
+
+ wcstombs(buf, msgbuf, chars + 1);
+ LocalFree(msgbuf);
+ }
+ else {
+ sprintf(buf, "unknown win32 error (%ld)", error);
+ }
+
+ SetLastError(lasterr);
+ return buf;
+}
+
+#endif /* UNDER_CE */
+
+/* Reset gzip file state */
+local void gz_reset(state)
+ gz_statep state;
+{
+ state->x.have = 0; /* no output data available */
+ if (state->mode == GZ_READ) { /* for reading ... */
+ state->eof = 0; /* not at end of file */
+ state->past = 0; /* have not read past end yet */
+ state->how = LOOK; /* look for gzip header */
+ }
+ state->seek = 0; /* no seek request pending */
+ gz_error(state, Z_OK, NULL); /* clear error */
+ state->x.pos = 0; /* no uncompressed data yet */
+ state->strm.avail_in = 0; /* no input data yet */
+}
+
+/* Open a gzip file either by name or file descriptor. */
+local gzFile gz_open(path, fd, mode)
+ const void *path;
+ int fd;
+ const char *mode;
+{
+ gz_statep state;
+ z_size_t len;
+ int oflag;
+#ifdef O_CLOEXEC
+ int cloexec = 0;
+#endif
+#ifdef O_EXCL
+ int exclusive = 0;
+#endif
+
+ /* check input */
+ if (path == NULL)
+ return NULL;
+
+ /* allocate gzFile structure to return */
+ state = (gz_statep)malloc(sizeof(gz_state));
+ if (state == NULL)
+ return NULL;
+ state->size = 0; /* no buffers allocated yet */
+ state->want = GZBUFSIZE; /* requested buffer size */
+ state->msg = NULL; /* no error message yet */
+
+ /* interpret mode */
+ state->mode = GZ_NONE;
+ state->level = Z_DEFAULT_COMPRESSION;
+ state->strategy = Z_DEFAULT_STRATEGY;
+ state->direct = 0;
+ while (*mode) {
+ if (*mode >= '0' && *mode <= '9')
+ state->level = *mode - '0';
+ else
+ switch (*mode) {
+ case 'r':
+ state->mode = GZ_READ;
+ break;
+#ifndef NO_GZCOMPRESS
+ case 'w':
+ state->mode = GZ_WRITE;
+ break;
+ case 'a':
+ state->mode = GZ_APPEND;
+ break;
+#endif
+ case '+': /* can't read and write at the same time */
+ free(state);
+ return NULL;
+ case 'b': /* ignore -- will request binary anyway */
+ break;
+#ifdef O_CLOEXEC
+ case 'e':
+ cloexec = 1;
+ break;
+#endif
+#ifdef O_EXCL
+ case 'x':
+ exclusive = 1;
+ break;
+#endif
+ case 'f':
+ state->strategy = Z_FILTERED;
+ break;
+ case 'h':
+ state->strategy = Z_HUFFMAN_ONLY;
+ break;
+ case 'R':
+ state->strategy = Z_RLE;
+ break;
+ case 'F':
+ state->strategy = Z_FIXED;
+ break;
+ case 'T':
+ state->direct = 1;
+ break;
+ default: /* could consider as an error, but just ignore */
+ ;
+ }
+ mode++;
+ }
+
+ /* must provide an "r", "w", or "a" */
+ if (state->mode == GZ_NONE) {
+ free(state);
+ return NULL;
+ }
+
+ /* can't force transparent read */
+ if (state->mode == GZ_READ) {
+ if (state->direct) {
+ free(state);
+ return NULL;
+ }
+ state->direct = 1; /* for empty file */
+ }
+
+ /* save the path name for error messages */
+#ifdef WIDECHAR
+ if (fd == -2) {
+ len = wcstombs(NULL, path, 0);
+ if (len == (z_size_t)-1)
+ len = 0;
+ }
+ else
+#endif
+ len = strlen((const char *)path);
+ state->path = (char *)malloc(len + 1);
+ if (state->path == NULL) {
+ free(state);
+ return NULL;
+ }
+#ifdef WIDECHAR
+ if (fd == -2)
+ if (len)
+ wcstombs(state->path, path, len + 1);
+ else
+ *(state->path) = 0;
+ else
+#endif
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ (void)snprintf(state->path, len + 1, "%s", (const char *)path);
+#else
+ strcpy(state->path, path);
+#endif
+
+ /* compute the flags for open() */
+ oflag =
+#ifdef O_LARGEFILE
+ O_LARGEFILE |
+#endif
+#ifdef O_BINARY
+ O_BINARY |
+#endif
+#ifdef O_CLOEXEC
+ (cloexec ? O_CLOEXEC : 0) |
+#endif
+ (state->mode == GZ_READ ?
+ O_RDONLY :
+ (O_WRONLY | O_CREAT |
+#ifdef O_EXCL
+ (exclusive ? O_EXCL : 0) |
+#endif
+ (state->mode == GZ_WRITE ?
+ O_TRUNC :
+ O_APPEND)));
+
+ /* open the file with the appropriate flags (or just use fd) */
+ state->fd = fd > -1 ? fd : (
+#ifdef WIDECHAR
+ fd == -2 ? _wopen(path, oflag, 0666) :
+#endif
+ open((const char *)path, oflag, 0666));
+ if (state->fd == -1) {
+ free(state->path);
+ free(state);
+ return NULL;
+ }
+ if (state->mode == GZ_APPEND) {
+ LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
+ state->mode = GZ_WRITE; /* simplify later checks */
+ }
+
+ /* save the current position for rewinding (only if reading) */
+ if (state->mode == GZ_READ) {
+ state->start = LSEEK(state->fd, 0, SEEK_CUR);
+ if (state->start == -1) state->start = 0;
+ }
+
+ /* initialize stream */
+ gz_reset(state);
+
+ /* return stream */
+ return (gzFile)state;
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen(path, mode)
+ const char *path;
+ const char *mode;
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen64(path, mode)
+ const char *path;
+ const char *mode;
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzdopen(fd, mode)
+ int fd;
+ const char *mode;
+{
+ char *path; /* identifier for error messages */
+ gzFile gz;
+
+ if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
+ return NULL;
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
+#else
+ sprintf(path, "<fd:%d>", fd); /* for debugging */
+#endif
+ gz = gz_open(path, fd, mode);
+ free(path);
+ return gz;
+}
+
+/* -- see zlib.h -- */
+#ifdef WIDECHAR
+gzFile ZEXPORT gzopen_w(path, mode)
+ const wchar_t *path;
+ const char *mode;
+{
+ return gz_open(path, -2, mode);
+}
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzbuffer(file, size)
+ gzFile file;
+ unsigned size;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* make sure we haven't already allocated memory */
+ if (state->size != 0)
+ return -1;
+
+ /* check and set requested size */
+ if ((size << 1) < size)
+ return -1; /* need to be able to double it */
+ if (size < 2)
+ size = 2; /* need two bytes to check magic header */
+ state->want = size;
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzrewind(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* back up and start over */
+ if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
+ return -1;
+ gz_reset(state);
+ return 0;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzseek64(file, offset, whence)
+ gzFile file;
+ z_off64_t offset;
+ int whence;
+{
+ unsigned n;
+ z_off64_t ret;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* check that there's no error */
+ if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+ return -1;
+
+ /* can only seek from start or relative to current position */
+ if (whence != SEEK_SET && whence != SEEK_CUR)
+ return -1;
+
+ /* normalize offset to a SEEK_CUR specification */
+ if (whence == SEEK_SET)
+ offset -= state->x.pos;
+ else if (state->seek)
+ offset += state->skip;
+ state->seek = 0;
+
+ /* if within raw area while reading, just go there */
+ if (state->mode == GZ_READ && state->how == COPY &&
+ state->x.pos + offset >= 0) {
+ ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
+ if (ret == -1)
+ return -1;
+ state->x.have = 0;
+ state->eof = 0;
+ state->past = 0;
+ state->seek = 0;
+ gz_error(state, Z_OK, NULL);
+ state->strm.avail_in = 0;
+ state->x.pos += offset;
+ return state->x.pos;
+ }
+
+ /* calculate skip amount, rewinding if needed for back seek when reading */
+ if (offset < 0) {
+ if (state->mode != GZ_READ) /* writing -- can't go backwards */
+ return -1;
+ offset += state->x.pos;
+ if (offset < 0) /* before start of file! */
+ return -1;
+ if (gzrewind(file) == -1) /* rewind, then skip to offset */
+ return -1;
+ }
+
+ /* if reading, skip what's in output buffer (one less gzgetc() check) */
+ if (state->mode == GZ_READ) {
+ n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
+ (unsigned)offset : state->x.have;
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ offset -= n;
+ }
+
+ /* request skip (if not zero) */
+ if (offset) {
+ state->seek = 1;
+ state->skip = offset;
+ }
+ return state->x.pos + offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzseek(file, offset, whence)
+ gzFile file;
+ z_off_t offset;
+ int whence;
+{
+ z_off64_t ret;
+
+ ret = gzseek64(file, (z_off64_t)offset, whence);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gztell64(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* return position */
+ return state->x.pos + (state->seek ? state->skip : 0);
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gztell(file)
+ gzFile file;
+{
+ z_off64_t ret;
+
+ ret = gztell64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzoffset64(file)
+ gzFile file;
+{
+ z_off64_t offset;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* compute and return effective offset in file */
+ offset = LSEEK(state->fd, 0, SEEK_CUR);
+ if (offset == -1)
+ return -1;
+ if (state->mode == GZ_READ) /* reading */
+ offset -= state->strm.avail_in; /* don't count buffered input */
+ return offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzoffset(file)
+ gzFile file;
+{
+ z_off64_t ret;
+
+ ret = gzoffset64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzeof(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return 0;
+
+ /* return end-of-file state */
+ return state->mode == GZ_READ ? state->past : 0;
+}
+
+/* -- see zlib.h -- */
+const char * ZEXPORT gzerror(file, errnum)
+ gzFile file;
+ int *errnum;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return NULL;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return NULL;
+
+ /* return error information */
+ if (errnum != NULL)
+ *errnum = state->err;
+ return state->err == Z_MEM_ERROR ? "out of memory" :
+ (state->msg == NULL ? "" : state->msg);
+}
+
+/* -- see zlib.h -- */
+void ZEXPORT gzclearerr(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return;
+
+ /* clear error and end-of-file */
+ if (state->mode == GZ_READ) {
+ state->eof = 0;
+ state->past = 0;
+ }
+ gz_error(state, Z_OK, NULL);
+}
+
+/* Create an error message in allocated memory and set state->err and
+ state->msg accordingly. Free any previous error message already there. Do
+ not try to free or allocate space if the error is Z_MEM_ERROR (out of
+ memory). Simply save the error message as a static string. If there is an
+ allocation failure constructing the error message, then convert the error to
+ out of memory. */
+void ZLIB_INTERNAL gz_error(state, err, msg)
+ gz_statep state;
+ int err;
+ const char *msg;
+{
+ /* free previously allocated message and clear */
+ if (state->msg != NULL) {
+ if (state->err != Z_MEM_ERROR)
+ free(state->msg);
+ state->msg = NULL;
+ }
+
+ /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
+ if (err != Z_OK && err != Z_BUF_ERROR)
+ state->x.have = 0;
+
+ /* set error code, and if no message, then done */
+ state->err = err;
+ if (msg == NULL)
+ return;
+
+ /* for an out of memory error, return literal string when requested */
+ if (err == Z_MEM_ERROR)
+ return;
+
+ /* construct error message with path */
+ if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
+ NULL) {
+ state->err = Z_MEM_ERROR;
+ return;
+ }
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
+ "%s%s%s", state->path, ": ", msg);
+#else
+ strcpy(state->msg, state->path);
+ strcat(state->msg, ": ");
+ strcat(state->msg, msg);
+#endif
+}
+
+#ifndef INT_MAX
+/* portably return maximum value for an int (when limits.h presumed not
+ available) -- we need to do this to cover cases where 2's complement not
+ used, since C standard permits 1's complement and sign-bit representations,
+ otherwise we could just use ((unsigned)-1) >> 1 */
+unsigned ZLIB_INTERNAL gz_intmax()
+{
+ unsigned p, q;
+
+ p = 1;
+ do {
+ q = p;
+ p <<= 1;
+ p++;
+ } while (p > q);
+ return q >> 1;
+}
+#endif
diff --git a/test/monniaux/zlib-1.2.11/gzread.c b/test/monniaux/zlib-1.2.11/gzread.c
new file mode 100644
index 00000000..956b91ea
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/gzread.c
@@ -0,0 +1,654 @@
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
+local int gz_avail OF((gz_statep));
+local int gz_look OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_fetch OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+local z_size_t gz_read OF((gz_statep, voidp, z_size_t));
+
+/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
+ state->fd, and update state->eof, state->err, and state->msg as appropriate.
+ This function needs to loop on read(), since read() is not guaranteed to
+ read the number of bytes requested, depending on the type of descriptor. */
+local int gz_load(state, buf, len, have)
+ gz_statep state;
+ unsigned char *buf;
+ unsigned len;
+ unsigned *have;
+{
+ int ret;
+ unsigned get, max = ((unsigned)-1 >> 2) + 1;
+
+ *have = 0;
+ do {
+ get = len - *have;
+ if (get > max)
+ get = max;
+ ret = read(state->fd, buf + *have, get);
+ if (ret <= 0)
+ break;
+ *have += (unsigned)ret;
+ } while (*have < len);
+ if (ret < 0) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ if (ret == 0)
+ state->eof = 1;
+ return 0;
+}
+
+/* Load up input buffer and set eof flag if last data loaded -- return -1 on
+ error, 0 otherwise. Note that the eof flag is set when the end of the input
+ file is reached, even though there may be unused data in the buffer. Once
+ that data has been used, no more attempts will be made to read the file.
+ If strm->avail_in != 0, then the current data is moved to the beginning of
+ the input buffer, and then the remainder of the buffer is loaded with the
+ available data from the input file. */
+local int gz_avail(state)
+ gz_statep state;
+{
+ unsigned got;
+ z_streamp strm = &(state->strm);
+
+ if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+ return -1;
+ if (state->eof == 0) {
+ if (strm->avail_in) { /* copy what's there to the start */
+ unsigned char *p = state->in;
+ unsigned const char *q = strm->next_in;
+ unsigned n = strm->avail_in;
+ do {
+ *p++ = *q++;
+ } while (--n);
+ }
+ if (gz_load(state, state->in + strm->avail_in,
+ state->size - strm->avail_in, &got) == -1)
+ return -1;
+ strm->avail_in += got;
+ strm->next_in = state->in;
+ }
+ return 0;
+}
+
+/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
+ If this is the first time in, allocate required memory. state->how will be
+ left unchanged if there is no more input data available, will be set to COPY
+ if there is no gzip header and direct copying will be performed, or it will
+ be set to GZIP for decompression. If direct copying, then leftover input
+ data from the input buffer will be copied to the output buffer. In that
+ case, all further file reads will be directly to either the output buffer or
+ a user buffer. If decompressing, the inflate state will be initialized.
+ gz_look() will return 0 on success or -1 on failure. */
+local int gz_look(state)
+ gz_statep state;
+{
+ z_streamp strm = &(state->strm);
+
+ /* allocate read buffers and inflate memory */
+ if (state->size == 0) {
+ /* allocate buffers */
+ state->in = (unsigned char *)malloc(state->want);
+ state->out = (unsigned char *)malloc(state->want << 1);
+ if (state->in == NULL || state->out == NULL) {
+ free(state->out);
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ state->size = state->want;
+
+ /* allocate inflate memory */
+ state->strm.zalloc = Z_NULL;
+ state->strm.zfree = Z_NULL;
+ state->strm.opaque = Z_NULL;
+ state->strm.avail_in = 0;
+ state->strm.next_in = Z_NULL;
+ if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
+ free(state->out);
+ free(state->in);
+ state->size = 0;
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ }
+
+ /* get at least the magic bytes in the input buffer */
+ if (strm->avail_in < 2) {
+ if (gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0)
+ return 0;
+ }
+
+ /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
+ a logical dilemma here when considering the case of a partially written
+ gzip file, to wit, if a single 31 byte is written, then we cannot tell
+ whether this is a single-byte file, or just a partially written gzip
+ file -- for here we assume that if a gzip file is being written, then
+ the header will be written in a single operation, so that reading a
+ single byte is sufficient indication that it is not a gzip file) */
+ if (strm->avail_in > 1 &&
+ strm->next_in[0] == 31 && strm->next_in[1] == 139) {
+ inflateReset(strm);
+ state->how = GZIP;
+ state->direct = 0;
+ return 0;
+ }
+
+ /* no gzip header -- if we were decoding gzip before, then this is trailing
+ garbage. Ignore the trailing garbage and finish. */
+ if (state->direct == 0) {
+ strm->avail_in = 0;
+ state->eof = 1;
+ state->x.have = 0;
+ return 0;
+ }
+
+ /* doing raw i/o, copy any leftover input to output -- this assumes that
+ the output buffer is larger than the input buffer, which also assures
+ space for gzungetc() */
+ state->x.next = state->out;
+ if (strm->avail_in) {
+ memcpy(state->x.next, strm->next_in, strm->avail_in);
+ state->x.have = strm->avail_in;
+ strm->avail_in = 0;
+ }
+ state->how = COPY;
+ state->direct = 1;
+ return 0;
+}
+
+/* Decompress from input to the provided next_out and avail_out in the state.
+ On return, state->x.have and state->x.next point to the just decompressed
+ data. If the gzip stream completes, state->how is reset to LOOK to look for
+ the next gzip stream or raw data, once state->x.have is depleted. Returns 0
+ on success, -1 on failure. */
+local int gz_decomp(state)
+ gz_statep state;
+{
+ int ret = Z_OK;
+ unsigned had;
+ z_streamp strm = &(state->strm);
+
+ /* fill output buffer up to end of deflate stream */
+ had = strm->avail_out;
+ do {
+ /* get more input for inflate() */
+ if (strm->avail_in == 0 && gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0) {
+ gz_error(state, Z_BUF_ERROR, "unexpected end of file");
+ break;
+ }
+
+ /* decompress and handle errors */
+ ret = inflate(strm, Z_NO_FLUSH);
+ if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: inflate stream corrupt");
+ return -1;
+ }
+ if (ret == Z_MEM_ERROR) {
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
+ gz_error(state, Z_DATA_ERROR,
+ strm->msg == NULL ? "compressed data error" : strm->msg);
+ return -1;
+ }
+ } while (strm->avail_out && ret != Z_STREAM_END);
+
+ /* update available output */
+ state->x.have = had - strm->avail_out;
+ state->x.next = strm->next_out - state->x.have;
+
+ /* if the gzip stream completed successfully, look for another */
+ if (ret == Z_STREAM_END)
+ state->how = LOOK;
+
+ /* good decompression */
+ return 0;
+}
+
+/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
+ Data is either copied from the input file or decompressed from the input
+ file depending on state->how. If state->how is LOOK, then a gzip header is
+ looked for to determine whether to copy or decompress. Returns -1 on error,
+ otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
+ end of the input file has been reached and all data has been processed. */
+local int gz_fetch(state)
+ gz_statep state;
+{
+ z_streamp strm = &(state->strm);
+
+ do {
+ switch(state->how) {
+ case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
+ if (gz_look(state) == -1)
+ return -1;
+ if (state->how == LOOK)
+ return 0;
+ break;
+ case COPY: /* -> COPY */
+ if (gz_load(state, state->out, state->size << 1, &(state->x.have))
+ == -1)
+ return -1;
+ state->x.next = state->out;
+ return 0;
+ case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
+ strm->avail_out = state->size << 1;
+ strm->next_out = state->out;
+ if (gz_decomp(state) == -1)
+ return -1;
+ }
+ } while (state->x.have == 0 && (!state->eof || strm->avail_in));
+ return 0;
+}
+
+/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
+local int gz_skip(state, len)
+ gz_statep state;
+ z_off64_t len;
+{
+ unsigned n;
+
+ /* skip over len bytes or reach end-of-file, whichever comes first */
+ while (len)
+ /* skip over whatever is in output buffer */
+ if (state->x.have) {
+ n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
+ (unsigned)len : state->x.have;
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ len -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && state->strm.avail_in == 0)
+ break;
+
+ /* need more data to skip -- load up output buffer */
+ else {
+ /* get more output, looking for header if required */
+ if (gz_fetch(state) == -1)
+ return -1;
+ }
+ return 0;
+}
+
+/* Read len bytes into buf from file, or less than len up to the end of the
+ input. Return the number of bytes read. If zero is returned, either the
+ end of file was reached, or there was an error. state->err must be
+ consulted in that case to determine which. */
+local z_size_t gz_read(state, buf, len)
+ gz_statep state;
+ voidp buf;
+ z_size_t len;
+{
+ z_size_t got;
+ unsigned n;
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* get len bytes to buf, or less than len if at the end */
+ got = 0;
+ do {
+ /* set n to the maximum amount of len that fits in an unsigned int */
+ n = -1;
+ if (n > len)
+ n = len;
+
+ /* first just try copying data from the output buffer */
+ if (state->x.have) {
+ if (state->x.have < n)
+ n = state->x.have;
+ memcpy(buf, state->x.next, n);
+ state->x.next += n;
+ state->x.have -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && state->strm.avail_in == 0) {
+ state->past = 1; /* tried to read past end */
+ break;
+ }
+
+ /* need output data -- for small len or new stream load up our output
+ buffer */
+ else if (state->how == LOOK || n < (state->size << 1)) {
+ /* get more output, looking for header if required */
+ if (gz_fetch(state) == -1)
+ return 0;
+ continue; /* no progress yet -- go back to copy above */
+ /* the copy above assures that we will leave with space in the
+ output buffer, allowing at least one gzungetc() to succeed */
+ }
+
+ /* large len -- read directly into user buffer */
+ else if (state->how == COPY) { /* read directly */
+ if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
+ return 0;
+ }
+
+ /* large len -- decompress directly into user buffer */
+ else { /* state->how == GZIP */
+ state->strm.avail_out = n;
+ state->strm.next_out = (unsigned char *)buf;
+ if (gz_decomp(state) == -1)
+ return 0;
+ n = state->x.have;
+ state->x.have = 0;
+ }
+
+ /* update progress */
+ len -= n;
+ buf = (char *)buf + n;
+ got += n;
+ state->x.pos += n;
+ } while (len);
+
+ /* return number of bytes read into user buffer */
+ return got;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzread(file, buf, len)
+ gzFile file;
+ voidp buf;
+ unsigned len;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids a flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
+ return -1;
+ }
+
+ /* read len or fewer bytes to buf */
+ len = gz_read(state, buf, len);
+
+ /* check for an error */
+ if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
+ return -1;
+
+ /* return the number of bytes read (this is assured to fit in an int) */
+ return (int)len;
+}
+
+/* -- see zlib.h -- */
+z_size_t ZEXPORT gzfread(buf, size, nitems, file)
+ voidp buf;
+ z_size_t size;
+ z_size_t nitems;
+ gzFile file;
+{
+ z_size_t len;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return 0;
+
+ /* compute bytes to read -- error on overflow */
+ len = nitems * size;
+ if (size && len / size != nitems) {
+ gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
+ return 0;
+ }
+
+ /* read len or fewer bytes to buf, return the number of full items read */
+ return len ? gz_read(state, buf, len) / size : 0;
+}
+
+/* -- see zlib.h -- */
+#ifdef Z_PREFIX_SET
+# undef z_gzgetc
+#else
+# undef gzgetc
+#endif
+int ZEXPORT gzgetc(file)
+ gzFile file;
+{
+ int ret;
+ unsigned char buf[1];
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* try output buffer (no need to check for skip request) */
+ if (state->x.have) {
+ state->x.have--;
+ state->x.pos++;
+ return *(state->x.next)++;
+ }
+
+ /* nothing there -- try gz_read() */
+ ret = gz_read(state, buf, 1);
+ return ret < 1 ? -1 : buf[0];
+}
+
+int ZEXPORT gzgetc_(file)
+gzFile file;
+{
+ return gzgetc(file);
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzungetc(c, file)
+ int c;
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* can't push EOF */
+ if (c < 0)
+ return -1;
+
+ /* if output buffer empty, put byte at end (allows more pushing) */
+ if (state->x.have == 0) {
+ state->x.have = 1;
+ state->x.next = state->out + (state->size << 1) - 1;
+ state->x.next[0] = (unsigned char)c;
+ state->x.pos--;
+ state->past = 0;
+ return c;
+ }
+
+ /* if no room, give up (must have already done a gzungetc()) */
+ if (state->x.have == (state->size << 1)) {
+ gz_error(state, Z_DATA_ERROR, "out of room to push characters");
+ return -1;
+ }
+
+ /* slide output data if needed and insert byte before existing data */
+ if (state->x.next == state->out) {
+ unsigned char *src = state->out + state->x.have;
+ unsigned char *dest = state->out + (state->size << 1);
+ while (src > state->out)
+ *--dest = *--src;
+ state->x.next = dest;
+ }
+ state->x.have++;
+ state->x.next--;
+ state->x.next[0] = (unsigned char)c;
+ state->x.pos--;
+ state->past = 0;
+ return c;
+}
+
+/* -- see zlib.h -- */
+char * ZEXPORT gzgets(file, buf, len)
+ gzFile file;
+ char *buf;
+ int len;
+{
+ unsigned left, n;
+ char *str;
+ unsigned char *eol;
+ gz_statep state;
+
+ /* check parameters and get internal structure */
+ if (file == NULL || buf == NULL || len < 1)
+ return NULL;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return NULL;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return NULL;
+ }
+
+ /* copy output bytes up to new line or len - 1, whichever comes first --
+ append a terminating zero to the string (we don't check for a zero in
+ the contents, let the user worry about that) */
+ str = buf;
+ left = (unsigned)len - 1;
+ if (left) do {
+ /* assure that something is in the output buffer */
+ if (state->x.have == 0 && gz_fetch(state) == -1)
+ return NULL; /* error */
+ if (state->x.have == 0) { /* end of file */
+ state->past = 1; /* read past end */
+ break; /* return what we have */
+ }
+
+ /* look for end-of-line in current output buffer */
+ n = state->x.have > left ? left : state->x.have;
+ eol = (unsigned char *)memchr(state->x.next, '\n', n);
+ if (eol != NULL)
+ n = (unsigned)(eol - state->x.next) + 1;
+
+ /* copy through end-of-line, or remainder if not found */
+ memcpy(buf, state->x.next, n);
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ left -= n;
+ buf += n;
+ } while (left && eol == NULL);
+
+ /* return terminated string, or if nothing, end of file */
+ if (buf == str)
+ return NULL;
+ buf[0] = 0;
+ return str;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzdirect(file)
+ gzFile file;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* if the state is not known, but we can find out, then do so (this is
+ mainly for right after a gzopen() or gzdopen()) */
+ if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
+ (void)gz_look(state);
+
+ /* return 1 if transparent, 0 if processing a gzip stream */
+ return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(file)
+ gzFile file;
+{
+ int ret, err;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're reading */
+ if (state->mode != GZ_READ)
+ return Z_STREAM_ERROR;
+
+ /* free memory and close file */
+ if (state->size) {
+ inflateEnd(&(state->strm));
+ free(state->out);
+ free(state->in);
+ }
+ err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ ret = close(state->fd);
+ free(state);
+ return ret ? Z_ERRNO : err;
+}
diff --git a/test/monniaux/zlib-1.2.11/gzwrite.c b/test/monniaux/zlib-1.2.11/gzwrite.c
new file mode 100644
index 00000000..c7b5651d
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/gzwrite.c
@@ -0,0 +1,665 @@
+/* gzwrite.c -- zlib functions for writing gzip files
+ * Copyright (C) 2004-2017 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_init OF((gz_statep));
+local int gz_comp OF((gz_statep, int));
+local int gz_zero OF((gz_statep, z_off64_t));
+local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
+
+/* Initialize state for writing a gzip file. Mark initialization by setting
+ state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
+ success. */
+local int gz_init(state)
+ gz_statep state;
+{
+ int ret;
+ z_streamp strm = &(state->strm);
+
+ /* allocate input buffer (double size for gzprintf) */
+ state->in = (unsigned char *)malloc(state->want << 1);
+ if (state->in == NULL) {
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* only need output buffer and deflate state if compressing */
+ if (!state->direct) {
+ /* allocate output buffer */
+ state->out = (unsigned char *)malloc(state->want);
+ if (state->out == NULL) {
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* allocate deflate memory, set up for gzip compression */
+ strm->zalloc = Z_NULL;
+ strm->zfree = Z_NULL;
+ strm->opaque = Z_NULL;
+ ret = deflateInit2(strm, state->level, Z_DEFLATED,
+ MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
+ if (ret != Z_OK) {
+ free(state->out);
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ strm->next_in = NULL;
+ }
+
+ /* mark state as initialized */
+ state->size = state->want;
+
+ /* initialize write buffer if compressing */
+ if (!state->direct) {
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ state->x.next = strm->next_out;
+ }
+ return 0;
+}
+
+/* Compress whatever is at avail_in and next_in and write to the output file.
+ Return -1 if there is an error writing to the output file or if gz_init()
+ fails to allocate memory, otherwise 0. flush is assumed to be a valid
+ deflate() flush value. If flush is Z_FINISH, then the deflate() state is
+ reset to start a new gzip stream. If gz->direct is true, then simply write
+ to the output file without compressing, and ignore flush. */
+local int gz_comp(state, flush)
+ gz_statep state;
+ int flush;
+{
+ int ret, writ;
+ unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
+ z_streamp strm = &(state->strm);
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return -1;
+
+ /* write directly if requested */
+ if (state->direct) {
+ while (strm->avail_in) {
+ put = strm->avail_in > max ? max : strm->avail_in;
+ writ = write(state->fd, strm->next_in, put);
+ if (writ < 0) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ strm->avail_in -= (unsigned)writ;
+ strm->next_in += writ;
+ }
+ return 0;
+ }
+
+ /* run deflate() on provided input until it produces no more output */
+ ret = Z_OK;
+ do {
+ /* write out current buffer contents if full, or if flushing, but if
+ doing Z_FINISH then don't write until we get to Z_STREAM_END */
+ if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
+ (flush != Z_FINISH || ret == Z_STREAM_END))) {
+ while (strm->next_out > state->x.next) {
+ put = strm->next_out - state->x.next > (int)max ? max :
+ (unsigned)(strm->next_out - state->x.next);
+ writ = write(state->fd, state->x.next, put);
+ if (writ < 0) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ state->x.next += writ;
+ }
+ if (strm->avail_out == 0) {
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ state->x.next = state->out;
+ }
+ }
+
+ /* compress */
+ have = strm->avail_out;
+ ret = deflate(strm, flush);
+ if (ret == Z_STREAM_ERROR) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: deflate stream corrupt");
+ return -1;
+ }
+ have -= strm->avail_out;
+ } while (have);
+
+ /* if that completed a deflate stream, allow another to start */
+ if (flush == Z_FINISH)
+ deflateReset(strm);
+
+ /* all done, no errors */
+ return 0;
+}
+
+/* Compress len zeros to output. Return -1 on a write error or memory
+ allocation failure by gz_comp(), or 0 on success. */
+local int gz_zero(state, len)
+ gz_statep state;
+ z_off64_t len;
+{
+ int first;
+ unsigned n;
+ z_streamp strm = &(state->strm);
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+
+ /* compress len zeros (len guaranteed > 0) */
+ first = 1;
+ while (len) {
+ n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
+ (unsigned)len : state->size;
+ if (first) {
+ memset(state->in, 0, n);
+ first = 0;
+ }
+ strm->avail_in = n;
+ strm->next_in = state->in;
+ state->x.pos += n;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+ len -= n;
+ }
+ return 0;
+}
+
+/* Write len bytes from buf to file. Return the number of bytes written. If
+ the returned value is less than len, then there was an error. */
+local z_size_t gz_write(state, buf, len)
+ gz_statep state;
+ voidpc buf;
+ z_size_t len;
+{
+ z_size_t put = len;
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* for small len, copy to input buffer, otherwise compress directly */
+ if (len < state->size) {
+ /* copy to input buffer, compress when full */
+ do {
+ unsigned have, copy;
+
+ if (state->strm.avail_in == 0)
+ state->strm.next_in = state->in;
+ have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
+ state->in);
+ copy = state->size - have;
+ if (copy > len)
+ copy = len;
+ memcpy(state->in + have, buf, copy);
+ state->strm.avail_in += copy;
+ state->x.pos += copy;
+ buf = (const char *)buf + copy;
+ len -= copy;
+ if (len && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ } while (len);
+ }
+ else {
+ /* consume whatever's left in the input buffer */
+ if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* directly compress user buffer to file */
+ state->strm.next_in = (z_const Bytef *)buf;
+ do {
+ unsigned n = (unsigned)-1;
+ if (n > len)
+ n = len;
+ state->strm.avail_in = n;
+ state->x.pos += n;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ len -= n;
+ } while (len);
+ }
+
+ /* input was all buffered or compressed */
+ return put;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzwrite(file, buf, len)
+ gzFile file;
+ voidpc buf;
+ unsigned len;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids a flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+ return 0;
+ }
+
+ /* write len bytes from buf (the return value will fit in an int) */
+ return (int)gz_write(state, buf, len);
+}
+
+/* -- see zlib.h -- */
+z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
+ voidpc buf;
+ z_size_t size;
+ z_size_t nitems;
+ gzFile file;
+{
+ z_size_t len;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* compute bytes to read -- error on overflow */
+ len = nitems * size;
+ if (size && len / size != nitems) {
+ gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
+ return 0;
+ }
+
+ /* write len bytes to buf, return the number of full items written */
+ return len ? gz_write(state, buf, len) / size : 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputc(file, c)
+ gzFile file;
+ int c;
+{
+ unsigned have;
+ unsigned char buf[1];
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return -1;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* try writing to input buffer for speed (state->size == 0 if buffer not
+ initialized) */
+ if (state->size) {
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
+ if (have < state->size) {
+ state->in[have] = (unsigned char)c;
+ strm->avail_in++;
+ state->x.pos++;
+ return c & 0xff;
+ }
+ }
+
+ /* no room in buffer or not initialized, use gz_write() */
+ buf[0] = (unsigned char)c;
+ if (gz_write(state, buf, 1) != 1)
+ return -1;
+ return c & 0xff;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputs(file, str)
+ gzFile file;
+ const char *str;
+{
+ int ret;
+ z_size_t len;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return -1;
+
+ /* write string */
+ len = strlen(str);
+ ret = gz_write(state, str, len);
+ return ret == 0 && len != 0 ? -1 : ret;
+}
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#include <stdarg.h>
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
+{
+ int len;
+ unsigned left;
+ char *next;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return state->err;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return state->err;
+ }
+
+ /* do the printf() into the input buffer, put length in len -- the input
+ buffer is double-sized just for this function, so there is guaranteed to
+ be state->size bytes available after the current contents */
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
+ next[state->size - 1] = 0;
+#ifdef NO_vsnprintf
+# ifdef HAS_vsprintf_void
+ (void)vsprintf(next, format, va);
+ for (len = 0; len < state->size; len++)
+ if (next[len] == 0) break;
+# else
+ len = vsprintf(next, format, va);
+# endif
+#else
+# ifdef HAS_vsnprintf_void
+ (void)vsnprintf(next, state->size, format, va);
+ len = strlen(next);
+# else
+ len = vsnprintf(next, state->size, format, va);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, compress first half if past that */
+ strm->avail_in += (unsigned)len;
+ state->x.pos += len;
+ if (strm->avail_in >= state->size) {
+ left = strm->avail_in - state->size;
+ strm->avail_in = state->size;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return state->err;
+ memcpy(state->in, state->in + state->size, left);
+ strm->next_in = state->in;
+ strm->avail_in = left;
+ }
+ return len;
+}
+
+int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
+{
+ va_list va;
+ int ret;
+
+ va_start(va, format);
+ ret = gzvprintf(file, format, va);
+ va_end(va);
+ return ret;
+}
+
+#else /* !STDC && !Z_HAVE_STDARG_H */
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+ a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
+ gzFile file;
+ const char *format;
+ int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+ a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
+{
+ unsigned len, left;
+ char *next;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that can really pass pointer in ints */
+ if (sizeof(int) != sizeof(void *))
+ return Z_STREAM_ERROR;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return state->error;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return state->error;
+ }
+
+ /* do the printf() into the input buffer, put length in len -- the input
+ buffer is double-sized just for this function, so there is guaranteed to
+ be state->size bytes available after the current contents */
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ next = (char *)(strm->next_in + strm->avail_in);
+ next[state->size - 1] = 0;
+#ifdef NO_snprintf
+# ifdef HAS_sprintf_void
+ sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
+ a13, a14, a15, a16, a17, a18, a19, a20);
+ for (len = 0; len < size; len++)
+ if (next[len] == 0)
+ break;
+# else
+ len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
+ a12, a13, a14, a15, a16, a17, a18, a19, a20);
+# endif
+#else
+# ifdef HAS_snprintf_void
+ snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
+ a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+ len = strlen(next);
+# else
+ len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len == 0 || len >= state->size || next[state->size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, compress first half if past that */
+ strm->avail_in += len;
+ state->x.pos += len;
+ if (strm->avail_in >= state->size) {
+ left = strm->avail_in - state->size;
+ strm->avail_in = state->size;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return state->err;
+ memcpy(state->in, state->in + state->size, left);
+ strm->next_in = state->in;
+ strm->avail_in = left;
+ }
+ return (int)len;
+}
+
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzflush(file, flush)
+ gzFile file;
+ int flush;
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* check flush parameter */
+ if (flush < 0 || flush > Z_FINISH)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return state->err;
+ }
+
+ /* compress remaining data with requested flush */
+ (void)gz_comp(state, flush);
+ return state->err;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzsetparams(file, level, strategy)
+ gzFile file;
+ int level;
+ int strategy;
+{
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* if no change is requested, then do nothing */
+ if (level == state->level && strategy == state->strategy)
+ return Z_OK;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return state->err;
+ }
+
+ /* change compression parameters for subsequent input */
+ if (state->size) {
+ /* flush previous input with previous parameters before changing */
+ if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
+ return state->err;
+ deflateParams(strm, level, strategy);
+ }
+ state->level = level;
+ state->strategy = strategy;
+ return Z_OK;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_w(file)
+ gzFile file;
+{
+ int ret = Z_OK;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're writing */
+ if (state->mode != GZ_WRITE)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ ret = state->err;
+ }
+
+ /* flush, free memory, and close file */
+ if (gz_comp(state, Z_FINISH) == -1)
+ ret = state->err;
+ if (state->size) {
+ if (!state->direct) {
+ (void)deflateEnd(&(state->strm));
+ free(state->out);
+ }
+ free(state->in);
+ }
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ if (close(state->fd) == -1)
+ ret = Z_ERRNO;
+ free(state);
+ return ret;
+}
diff --git a/test/monniaux/zlib-1.2.11/infback.c b/test/monniaux/zlib-1.2.11/infback.c
new file mode 100644
index 00000000..59679ecb
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/infback.c
@@ -0,0 +1,640 @@
+/* infback.c -- inflate using a call-back interface
+ * Copyright (C) 1995-2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ This code is largely copied from inflate.c. Normally either infback.o or
+ inflate.o would be linked into an application--not both. The interface
+ with inffast.c is retained so that optimized assembler-coded versions of
+ inflate_fast() can be used with either inflate.c or infback.c.
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+/* function prototypes */
+local void fixedtables OF((struct inflate_state FAR *state));
+
+/*
+ strm provides memory allocation functions in zalloc and zfree, or
+ Z_NULL to use the library memory allocation functions.
+
+ windowBits is in the range 8..15, and window is a user-supplied
+ window and output buffer that is 2**windowBits bytes.
+ */
+int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
+z_streamp strm;
+int windowBits;
+unsigned char FAR *window;
+const char *version;
+int stream_size;
+{
+ struct inflate_state FAR *state;
+
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+ stream_size != (int)(sizeof(z_stream)))
+ return Z_VERSION_ERROR;
+ if (strm == Z_NULL || window == Z_NULL ||
+ windowBits < 8 || windowBits > 15)
+ return Z_STREAM_ERROR;
+ strm->msg = Z_NULL; /* in case we return an error */
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+ state = (struct inflate_state FAR *)ZALLOC(strm, 1,
+ sizeof(struct inflate_state));
+ if (state == Z_NULL) return Z_MEM_ERROR;
+ Tracev((stderr, "inflate: allocated\n"));
+ strm->state = (struct internal_state FAR *)state;
+ state->dmax = 32768U;
+ state->wbits = (uInt)windowBits;
+ state->wsize = 1U << windowBits;
+ state->window = window;
+ state->wnext = 0;
+ state->whave = 0;
+ return Z_OK;
+}
+
+/*
+ Return state with length and distance decoding tables and index sizes set to
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
+ If BUILDFIXED is defined, then instead this routine builds the tables the
+ first time it's called, and returns those tables the first time and
+ thereafter. This reduces the size of the code by about 2K bytes, in
+ exchange for a little execution time. However, BUILDFIXED should not be
+ used for threaded applications, since the rewriting of the tables and virgin
+ may not be thread-safe.
+ */
+local void fixedtables(state)
+struct inflate_state FAR *state;
+{
+#ifdef BUILDFIXED
+ static int virgin = 1;
+ static code *lenfix, *distfix;
+ static code fixed[544];
+
+ /* build fixed huffman tables if first call (may not be thread safe) */
+ if (virgin) {
+ unsigned sym, bits;
+ static code *next;
+
+ /* literal/length table */
+ sym = 0;
+ while (sym < 144) state->lens[sym++] = 8;
+ while (sym < 256) state->lens[sym++] = 9;
+ while (sym < 280) state->lens[sym++] = 7;
+ while (sym < 288) state->lens[sym++] = 8;
+ next = fixed;
+ lenfix = next;
+ bits = 9;
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
+
+ /* distance table */
+ sym = 0;
+ while (sym < 32) state->lens[sym++] = 5;
+ distfix = next;
+ bits = 5;
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
+
+ /* do this just once */
+ virgin = 0;
+ }
+#else /* !BUILDFIXED */
+# include "inffixed.h"
+#endif /* BUILDFIXED */
+ state->lencode = lenfix;
+ state->lenbits = 9;
+ state->distcode = distfix;
+ state->distbits = 5;
+}
+
+/* Macros for inflateBack(): */
+
+/* Load returned state from inflate_fast() */
+#define LOAD() \
+ do { \
+ put = strm->next_out; \
+ left = strm->avail_out; \
+ next = strm->next_in; \
+ have = strm->avail_in; \
+ hold = state->hold; \
+ bits = state->bits; \
+ } while (0)
+
+/* Set state from registers for inflate_fast() */
+#define RESTORE() \
+ do { \
+ strm->next_out = put; \
+ strm->avail_out = left; \
+ strm->next_in = next; \
+ strm->avail_in = have; \
+ state->hold = hold; \
+ state->bits = bits; \
+ } while (0)
+
+/* Clear the input bit accumulator */
+#define INITBITS() \
+ do { \
+ hold = 0; \
+ bits = 0; \
+ } while (0)
+
+/* Assure that some input is available. If input is requested, but denied,
+ then return a Z_BUF_ERROR from inflateBack(). */
+#define PULL() \
+ do { \
+ if (have == 0) { \
+ have = in(in_desc, &next); \
+ if (have == 0) { \
+ next = Z_NULL; \
+ ret = Z_BUF_ERROR; \
+ goto inf_leave; \
+ } \
+ } \
+ } while (0)
+
+/* Get a byte of input into the bit accumulator, or return from inflateBack()
+ with an error if there is no input available. */
+#define PULLBYTE() \
+ do { \
+ PULL(); \
+ have--; \
+ hold += (unsigned long)(*next++) << bits; \
+ bits += 8; \
+ } while (0)
+
+/* Assure that there are at least n bits in the bit accumulator. If there is
+ not enough available input to do that, then return from inflateBack() with
+ an error. */
+#define NEEDBITS(n) \
+ do { \
+ while (bits < (unsigned)(n)) \
+ PULLBYTE(); \
+ } while (0)
+
+/* Return the low n bits of the bit accumulator (n < 16) */
+#define BITS(n) \
+ ((unsigned)hold & ((1U << (n)) - 1))
+
+/* Remove n bits from the bit accumulator */
+#define DROPBITS(n) \
+ do { \
+ hold >>= (n); \
+ bits -= (unsigned)(n); \
+ } while (0)
+
+/* Remove zero to seven bits as needed to go to a byte boundary */
+#define BYTEBITS() \
+ do { \
+ hold >>= bits & 7; \
+ bits -= bits & 7; \
+ } while (0)
+
+/* Assure that some output space is available, by writing out the window
+ if it's full. If the write fails, return from inflateBack() with a
+ Z_BUF_ERROR. */
+#define ROOM() \
+ do { \
+ if (left == 0) { \
+ put = state->window; \
+ left = state->wsize; \
+ state->whave = left; \
+ if (out(out_desc, put, left)) { \
+ ret = Z_BUF_ERROR; \
+ goto inf_leave; \
+ } \
+ } \
+ } while (0)
+
+/*
+ strm provides the memory allocation functions and window buffer on input,
+ and provides information on the unused input on return. For Z_DATA_ERROR
+ returns, strm will also provide an error message.
+
+ in() and out() are the call-back input and output functions. When
+ inflateBack() needs more input, it calls in(). When inflateBack() has
+ filled the window with output, or when it completes with data in the
+ window, it calls out() to write out the data. The application must not
+ change the provided input until in() is called again or inflateBack()
+ returns. The application must not change the window/output buffer until
+ inflateBack() returns.
+
+ in() and out() are called with a descriptor parameter provided in the
+ inflateBack() call. This parameter can be a structure that provides the
+ information required to do the read or write, as well as accumulated
+ information on the input and output such as totals and check values.
+
+ in() should return zero on failure. out() should return non-zero on
+ failure. If either in() or out() fails, than inflateBack() returns a
+ Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
+ was in() or out() that caused in the error. Otherwise, inflateBack()
+ returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
+ error, or Z_MEM_ERROR if it could not allocate memory for the state.
+ inflateBack() can also return Z_STREAM_ERROR if the input parameters
+ are not correct, i.e. strm is Z_NULL or the state was not initialized.
+ */
+int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
+z_streamp strm;
+in_func in;
+void FAR *in_desc;
+out_func out;
+void FAR *out_desc;
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *next; /* next input */
+ unsigned char FAR *put; /* next output */
+ unsigned have, left; /* available input and output */
+ unsigned long hold; /* bit buffer */
+ unsigned bits; /* bits in bit buffer */
+ unsigned copy; /* number of stored or match bytes to copy */
+ unsigned char FAR *from; /* where to copy match bytes from */
+ code here; /* current decoding table entry */
+ code last; /* parent table entry */
+ unsigned len; /* length to copy for repeats, bits to drop */
+ int ret; /* return code */
+ static const unsigned short order[19] = /* permutation of code lengths */
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ /* Check that the strm exists and that the state was initialized */
+ if (strm == Z_NULL || strm->state == Z_NULL)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* Reset the state */
+ strm->msg = Z_NULL;
+ state->mode = TYPE;
+ state->last = 0;
+ state->whave = 0;
+ next = strm->next_in;
+ have = next != Z_NULL ? strm->avail_in : 0;
+ hold = 0;
+ bits = 0;
+ put = state->window;
+ left = state->wsize;
+
+ /* Inflate until end of block marked as last */
+ for (;;)
+ switch (state->mode) {
+ case TYPE:
+ /* determine and dispatch block type */
+ if (state->last) {
+ BYTEBITS();
+ state->mode = DONE;
+ break;
+ }
+ NEEDBITS(3);
+ state->last = BITS(1);
+ DROPBITS(1);
+ switch (BITS(2)) {
+ case 0: /* stored block */
+ Tracev((stderr, "inflate: stored block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = STORED;
+ break;
+ case 1: /* fixed block */
+ fixedtables(state);
+ Tracev((stderr, "inflate: fixed codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = LEN; /* decode codes */
+ break;
+ case 2: /* dynamic block */
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = TABLE;
+ break;
+ case 3:
+ strm->msg = (char *)"invalid block type";
+ state->mode = BAD;
+ }
+ DROPBITS(2);
+ break;
+
+ case STORED:
+ /* get and verify stored block length */
+ BYTEBITS(); /* go to byte boundary */
+ NEEDBITS(32);
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
+ strm->msg = (char *)"invalid stored block lengths";
+ state->mode = BAD;
+ break;
+ }
+ state->length = (unsigned)hold & 0xffff;
+ Tracev((stderr, "inflate: stored length %u\n",
+ state->length));
+ INITBITS();
+
+ /* copy stored block from input to output */
+ while (state->length != 0) {
+ copy = state->length;
+ PULL();
+ ROOM();
+ if (copy > have) copy = have;
+ if (copy > left) copy = left;
+ zmemcpy(put, next, copy);
+ have -= copy;
+ next += copy;
+ left -= copy;
+ put += copy;
+ state->length -= copy;
+ }
+ Tracev((stderr, "inflate: stored end\n"));
+ state->mode = TYPE;
+ break;
+
+ case TABLE:
+ /* get dynamic table entries descriptor */
+ NEEDBITS(14);
+ state->nlen = BITS(5) + 257;
+ DROPBITS(5);
+ state->ndist = BITS(5) + 1;
+ DROPBITS(5);
+ state->ncode = BITS(4) + 4;
+ DROPBITS(4);
+#ifndef PKZIP_BUG_WORKAROUND
+ if (state->nlen > 286 || state->ndist > 30) {
+ strm->msg = (char *)"too many length or distance symbols";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracev((stderr, "inflate: table sizes ok\n"));
+
+ /* get code length code lengths (not a typo) */
+ state->have = 0;
+ while (state->have < state->ncode) {
+ NEEDBITS(3);
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
+ DROPBITS(3);
+ }
+ while (state->have < 19)
+ state->lens[order[state->have++]] = 0;
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 7;
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid code lengths set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: code lengths ok\n"));
+
+ /* get length and distance code code lengths */
+ state->have = 0;
+ while (state->have < state->nlen + state->ndist) {
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.val < 16) {
+ DROPBITS(here.bits);
+ state->lens[state->have++] = here.val;
+ }
+ else {
+ if (here.val == 16) {
+ NEEDBITS(here.bits + 2);
+ DROPBITS(here.bits);
+ if (state->have == 0) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ len = (unsigned)(state->lens[state->have - 1]);
+ copy = 3 + BITS(2);
+ DROPBITS(2);
+ }
+ else if (here.val == 17) {
+ NEEDBITS(here.bits + 3);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 3 + BITS(3);
+ DROPBITS(3);
+ }
+ else {
+ NEEDBITS(here.bits + 7);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 11 + BITS(7);
+ DROPBITS(7);
+ }
+ if (state->have + copy > state->nlen + state->ndist) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ while (copy--)
+ state->lens[state->have++] = (unsigned short)len;
+ }
+ }
+
+ /* handle error breaks in while */
+ if (state->mode == BAD) break;
+
+ /* check for end-of-block code (better have one) */
+ if (state->lens[256] == 0) {
+ strm->msg = (char *)"invalid code -- missing end-of-block";
+ state->mode = BAD;
+ break;
+ }
+
+ /* build code tables -- note: do not change the lenbits or distbits
+ values here (9 and 6) without reading the comments in inftrees.h
+ concerning the ENOUGH constants, which depend on those values */
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 9;
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid literal/lengths set";
+ state->mode = BAD;
+ break;
+ }
+ state->distcode = (code const FAR *)(state->next);
+ state->distbits = 6;
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
+ &(state->next), &(state->distbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid distances set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: codes ok\n"));
+ state->mode = LEN;
+
+ case LEN:
+ /* use inflate_fast() if we have enough input and output */
+ if (have >= 6 && left >= 258) {
+ RESTORE();
+ if (state->whave < state->wsize)
+ state->whave = state->wsize - left;
+ inflate_fast(strm, state->wsize);
+ LOAD();
+ break;
+ }
+
+ /* get a literal, length, or end-of-block code */
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.op && (here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->lencode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ }
+ DROPBITS(here.bits);
+ state->length = (unsigned)here.val;
+
+ /* process literal */
+ if (here.op == 0) {
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ ROOM();
+ *put++ = (unsigned char)(state->length);
+ left--;
+ state->mode = LEN;
+ break;
+ }
+
+ /* process end of block */
+ if (here.op & 32) {
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->mode = TYPE;
+ break;
+ }
+
+ /* invalid code */
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+
+ /* length code -- get extra bits, if any */
+ state->extra = (unsigned)(here.op) & 15;
+ if (state->extra != 0) {
+ NEEDBITS(state->extra);
+ state->length += BITS(state->extra);
+ DROPBITS(state->extra);
+ }
+ Tracevv((stderr, "inflate: length %u\n", state->length));
+
+ /* get distance code */
+ for (;;) {
+ here = state->distcode[BITS(state->distbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if ((here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->distcode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ }
+ DROPBITS(here.bits);
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ state->offset = (unsigned)here.val;
+
+ /* get distance extra bits, if any */
+ state->extra = (unsigned)(here.op) & 15;
+ if (state->extra != 0) {
+ NEEDBITS(state->extra);
+ state->offset += BITS(state->extra);
+ DROPBITS(state->extra);
+ }
+ if (state->offset > state->wsize - (state->whave < state->wsize ?
+ left : 0)) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
+
+ /* copy match from window to output */
+ do {
+ ROOM();
+ copy = state->wsize - state->offset;
+ if (copy < left) {
+ from = put + copy;
+ copy = left - copy;
+ }
+ else {
+ from = put - state->offset;
+ copy = left;
+ }
+ if (copy > state->length) copy = state->length;
+ state->length -= copy;
+ left -= copy;
+ do {
+ *put++ = *from++;
+ } while (--copy);
+ } while (state->length != 0);
+ break;
+
+ case DONE:
+ /* inflate stream terminated properly -- write leftover output */
+ ret = Z_STREAM_END;
+ if (left < state->wsize) {
+ if (out(out_desc, state->window, state->wsize - left))
+ ret = Z_BUF_ERROR;
+ }
+ goto inf_leave;
+
+ case BAD:
+ ret = Z_DATA_ERROR;
+ goto inf_leave;
+
+ default: /* can't happen, but makes compilers happy */
+ ret = Z_STREAM_ERROR;
+ goto inf_leave;
+ }
+
+ /* Return unused input */
+ inf_leave:
+ strm->next_in = next;
+ strm->avail_in = have;
+ return ret;
+}
+
+int ZEXPORT inflateBackEnd(strm)
+z_streamp strm;
+{
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+ Tracev((stderr, "inflate: end\n"));
+ return Z_OK;
+}
diff --git a/test/monniaux/zlib-1.2.11/inffast.c b/test/monniaux/zlib-1.2.11/inffast.c
new file mode 100644
index 00000000..0dbd1dbc
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inffast.c
@@ -0,0 +1,323 @@
+/* inffast.c -- fast decoding
+ * Copyright (C) 1995-2017 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifdef ASMINF
+# pragma message("Assembler code may have bugs -- use at your own risk")
+#else
+
+/*
+ Decode literal, length, and distance codes and write out the resulting
+ literal and match bytes until either not enough input or output is
+ available, an end-of-block is encountered, or a data error is encountered.
+ When large enough input and output buffers are supplied to inflate(), for
+ example, a 16K input buffer and a 64K output buffer, more than 95% of the
+ inflate execution time is spent in this routine.
+
+ Entry assumptions:
+
+ state->mode == LEN
+ strm->avail_in >= 6
+ strm->avail_out >= 258
+ start >= strm->avail_out
+ state->bits < 8
+
+ On return, state->mode is one of:
+
+ LEN -- ran out of enough output space or enough available input
+ TYPE -- reached end of block code, inflate() to interpret next block
+ BAD -- error in block data
+
+ Notes:
+
+ - The maximum input bits used by a length/distance pair is 15 bits for the
+ length code, 5 bits for the length extra, 15 bits for the distance code,
+ and 13 bits for the distance extra. This totals 48 bits, or six bytes.
+ Therefore if strm->avail_in >= 6, then there is enough input to avoid
+ checking for available input while decoding.
+
+ - The maximum bytes that a single length/distance pair can output is 258
+ bytes, which is the maximum length that can be coded. inflate_fast()
+ requires strm->avail_out >= 258 for each loop to avoid checking for
+ output space.
+ */
+void ZLIB_INTERNAL inflate_fast(strm, start)
+z_streamp strm;
+unsigned start; /* inflate()'s starting value for strm->avail_out */
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *in; /* local strm->next_in */
+ z_const unsigned char FAR *last; /* have enough input while in < last */
+ unsigned char FAR *out; /* local strm->next_out */
+ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
+ unsigned char FAR *end; /* while out < end, enough space available */
+#ifdef INFLATE_STRICT
+ unsigned dmax; /* maximum distance from zlib header */
+#endif
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
+ unsigned long hold; /* local strm->hold */
+ unsigned bits; /* local strm->bits */
+ code const FAR *lcode; /* local strm->lencode */
+ code const FAR *dcode; /* local strm->distcode */
+ unsigned lmask; /* mask for first level of length codes */
+ unsigned dmask; /* mask for first level of distance codes */
+ code here; /* retrieved table entry */
+ unsigned op; /* code bits, operation, extra bits, or */
+ /* window position, window bytes to copy */
+ unsigned len; /* match length, unused bytes */
+ unsigned dist; /* match distance */
+ unsigned char FAR *from; /* where to copy match from */
+
+ /* copy state to local variables */
+ state = (struct inflate_state FAR *)strm->state;
+ in = strm->next_in;
+ last = in + (strm->avail_in - 5);
+ out = strm->next_out;
+ beg = out - (start - strm->avail_out);
+ end = out + (strm->avail_out - 257);
+#ifdef INFLATE_STRICT
+ dmax = state->dmax;
+#endif
+ wsize = state->wsize;
+ whave = state->whave;
+ wnext = state->wnext;
+ window = state->window;
+ hold = state->hold;
+ bits = state->bits;
+ lcode = state->lencode;
+ dcode = state->distcode;
+ lmask = (1U << state->lenbits) - 1;
+ dmask = (1U << state->distbits) - 1;
+
+ /* decode literals and length/distances until end-of-block or not enough
+ input data or output space */
+ do {
+ if (bits < 15) {
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ }
+ here = lcode[hold & lmask];
+ dolen:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op == 0) { /* literal */
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ *out++ = (unsigned char)(here.val);
+ }
+ else if (op & 16) { /* length base */
+ len = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (op) {
+ if (bits < op) {
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ }
+ len += (unsigned)hold & ((1U << op) - 1);
+ hold >>= op;
+ bits -= op;
+ }
+ Tracevv((stderr, "inflate: length %u\n", len));
+ if (bits < 15) {
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ }
+ here = dcode[hold & dmask];
+ dodist:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op & 16) { /* distance base */
+ dist = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (bits < op) {
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ if (bits < op) {
+ hold += (unsigned long)(*in++) << bits;
+ bits += 8;
+ }
+ }
+ dist += (unsigned)hold & ((1U << op) - 1);
+#ifdef INFLATE_STRICT
+ if (dist > dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ hold >>= op;
+ bits -= op;
+ Tracevv((stderr, "inflate: distance %u\n", dist));
+ op = (unsigned)(out - beg); /* max distance in output */
+ if (dist > op) { /* see if copy from window */
+ op = dist - op; /* distance back in window */
+ if (op > whave) {
+ if (state->sane) {
+ strm->msg =
+ (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ if (len <= op - whave) {
+ do {
+ *out++ = 0;
+ } while (--len);
+ continue;
+ }
+ len -= op - whave;
+ do {
+ *out++ = 0;
+ } while (--op > whave);
+ if (op == 0) {
+ from = out - dist;
+ do {
+ *out++ = *from++;
+ } while (--len);
+ continue;
+ }
+#endif
+ }
+ from = window;
+ if (wnext == 0) { /* very common case */
+ from += wsize - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ *out++ = *from++;
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ else if (wnext < op) { /* wrap around window */
+ from += wsize + wnext - op;
+ op -= wnext;
+ if (op < len) { /* some from end of window */
+ len -= op;
+ do {
+ *out++ = *from++;
+ } while (--op);
+ from = window;
+ if (wnext < len) { /* some from start of window */
+ op = wnext;
+ len -= op;
+ do {
+ *out++ = *from++;
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ }
+ else { /* contiguous in window */
+ from += wnext - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ *out++ = *from++;
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ while (len > 2) {
+ *out++ = *from++;
+ *out++ = *from++;
+ *out++ = *from++;
+ len -= 3;
+ }
+ if (len) {
+ *out++ = *from++;
+ if (len > 1)
+ *out++ = *from++;
+ }
+ }
+ else {
+ from = out - dist; /* copy direct from output */
+ do { /* minimum length is three */
+ *out++ = *from++;
+ *out++ = *from++;
+ *out++ = *from++;
+ len -= 3;
+ } while (len > 2);
+ if (len) {
+ *out++ = *from++;
+ if (len > 1)
+ *out++ = *from++;
+ }
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level distance code */
+ here = dcode[here.val + (hold & ((1U << op) - 1))];
+ goto dodist;
+ }
+ else {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level length code */
+ here = lcode[here.val + (hold & ((1U << op) - 1))];
+ goto dolen;
+ }
+ else if (op & 32) { /* end-of-block */
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->mode = TYPE;
+ break;
+ }
+ else {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ } while (in < last && out < end);
+
+ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
+ len = bits >> 3;
+ in -= len;
+ bits -= len << 3;
+ hold &= (1U << bits) - 1;
+
+ /* update state and return */
+ strm->next_in = in;
+ strm->next_out = out;
+ strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
+ strm->avail_out = (unsigned)(out < end ?
+ 257 + (end - out) : 257 - (out - end));
+ state->hold = hold;
+ state->bits = bits;
+ return;
+}
+
+/*
+ inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
+ - Using bit fields for code structure
+ - Different op definition to avoid & for extra bits (do & for table bits)
+ - Three separate decoding do-loops for direct, window, and wnext == 0
+ - Special case for distance > 1 copies to do overlapped load and store copy
+ - Explicit branch predictions (based on measured branch probabilities)
+ - Deferring match copy and interspersed it with decoding subsequent codes
+ - Swapping literal/length else
+ - Swapping window/direct else
+ - Larger unrolled copy loops (three is about right)
+ - Moving len -= 3 statement into middle of loop
+ */
+
+#endif /* !ASMINF */
diff --git a/test/monniaux/zlib-1.2.11/inffast.h b/test/monniaux/zlib-1.2.11/inffast.h
new file mode 100644
index 00000000..e5c1aa4c
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inffast.h
@@ -0,0 +1,11 @@
+/* inffast.h -- header to use inffast.c
+ * Copyright (C) 1995-2003, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
diff --git a/test/monniaux/zlib-1.2.11/inffixed.h b/test/monniaux/zlib-1.2.11/inffixed.h
new file mode 100644
index 00000000..d6283277
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inffixed.h
@@ -0,0 +1,94 @@
+ /* inffixed.h -- table for decoding fixed codes
+ * Generated automatically by makefixed().
+ */
+
+ /* WARNING: this file should *not* be used by applications.
+ It is part of the implementation of this library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+ static const code lenfix[512] = {
+ {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
+ {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
+ {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
+ {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
+ {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
+ {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
+ {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
+ {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
+ {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
+ {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
+ {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
+ {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
+ {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
+ {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
+ {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
+ {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
+ {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
+ {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
+ {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
+ {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
+ {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
+ {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
+ {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
+ {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
+ {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
+ {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
+ {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
+ {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
+ {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
+ {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
+ {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
+ {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
+ {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
+ {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
+ {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
+ {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
+ {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
+ {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
+ {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
+ {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
+ {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
+ {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
+ {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
+ {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
+ {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
+ {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
+ {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
+ {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
+ {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
+ {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
+ {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
+ {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
+ {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
+ {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
+ {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
+ {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
+ {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
+ {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
+ {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
+ {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
+ {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
+ {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
+ {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
+ {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
+ {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
+ {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
+ {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
+ {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
+ {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
+ {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
+ {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
+ {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
+ {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
+ {0,9,255}
+ };
+
+ static const code distfix[32] = {
+ {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
+ {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
+ {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
+ {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
+ {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
+ {22,5,193},{64,5,0}
+ };
diff --git a/test/monniaux/zlib-1.2.11/inflate.c b/test/monniaux/zlib-1.2.11/inflate.c
new file mode 100644
index 00000000..ac333e8c
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inflate.c
@@ -0,0 +1,1561 @@
+/* inflate.c -- zlib decompression
+ * Copyright (C) 1995-2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * Change history:
+ *
+ * 1.2.beta0 24 Nov 2002
+ * - First version -- complete rewrite of inflate to simplify code, avoid
+ * creation of window when not needed, minimize use of window when it is
+ * needed, make inffast.c even faster, implement gzip decoding, and to
+ * improve code readability and style over the previous zlib inflate code
+ *
+ * 1.2.beta1 25 Nov 2002
+ * - Use pointers for available input and output checking in inffast.c
+ * - Remove input and output counters in inffast.c
+ * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
+ * - Remove unnecessary second byte pull from length extra in inffast.c
+ * - Unroll direct copy to three copies per loop in inffast.c
+ *
+ * 1.2.beta2 4 Dec 2002
+ * - Change external routine names to reduce potential conflicts
+ * - Correct filename to inffixed.h for fixed tables in inflate.c
+ * - Make hbuf[] unsigned char to match parameter type in inflate.c
+ * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
+ * to avoid negation problem on Alphas (64 bit) in inflate.c
+ *
+ * 1.2.beta3 22 Dec 2002
+ * - Add comments on state->bits assertion in inffast.c
+ * - Add comments on op field in inftrees.h
+ * - Fix bug in reuse of allocated window after inflateReset()
+ * - Remove bit fields--back to byte structure for speed
+ * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
+ * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
+ * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
+ * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
+ * - Use local copies of stream next and avail values, as well as local bit
+ * buffer and bit count in inflate()--for speed when inflate_fast() not used
+ *
+ * 1.2.beta4 1 Jan 2003
+ * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
+ * - Move a comment on output buffer sizes from inffast.c to inflate.c
+ * - Add comments in inffast.c to introduce the inflate_fast() routine
+ * - Rearrange window copies in inflate_fast() for speed and simplification
+ * - Unroll last copy for window match in inflate_fast()
+ * - Use local copies of window variables in inflate_fast() for speed
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
+ * - Make op and len in inflate_fast() unsigned for consistency
+ * - Add FAR to lcode and dcode declarations in inflate_fast()
+ * - Simplified bad distance check in inflate_fast()
+ * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
+ * source file infback.c to provide a call-back interface to inflate for
+ * programs like gzip and unzip -- uses window as output buffer to avoid
+ * window copying
+ *
+ * 1.2.beta5 1 Jan 2003
+ * - Improved inflateBack() interface to allow the caller to provide initial
+ * input in strm.
+ * - Fixed stored blocks bug in inflateBack()
+ *
+ * 1.2.beta6 4 Jan 2003
+ * - Added comments in inffast.c on effectiveness of POSTINC
+ * - Typecasting all around to reduce compiler warnings
+ * - Changed loops from while (1) or do {} while (1) to for (;;), again to
+ * make compilers happy
+ * - Changed type of window in inflateBackInit() to unsigned char *
+ *
+ * 1.2.beta7 27 Jan 2003
+ * - Changed many types to unsigned or unsigned short to avoid warnings
+ * - Added inflateCopy() function
+ *
+ * 1.2.0 9 Mar 2003
+ * - Changed inflateBack() interface to provide separate opaque descriptors
+ * for the in() and out() functions
+ * - Changed inflateBack() argument and in_func typedef to swap the length
+ * and buffer address return values for the input function
+ * - Check next_in and next_out for Z_NULL on entry to inflate()
+ *
+ * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifdef MAKEFIXED
+# ifndef BUILDFIXED
+# define BUILDFIXED
+# endif
+#endif
+
+/* function prototypes */
+local int inflateStateCheck OF((z_streamp strm));
+local void fixedtables OF((struct inflate_state FAR *state));
+local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
+ unsigned copy));
+#ifdef BUILDFIXED
+ void makefixed OF((void));
+#endif
+local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
+ unsigned len));
+
+local int inflateStateCheck(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+ if (strm == Z_NULL ||
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
+ return 1;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state == Z_NULL || state->strm != strm ||
+ state->mode < HEAD || state->mode > SYNC)
+ return 1;
+ return 0;
+}
+
+int ZEXPORT inflateResetKeep(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ strm->total_in = strm->total_out = state->total = 0;
+ strm->msg = Z_NULL;
+ if (state->wrap) /* to support ill-conceived Java test suite */
+ strm->adler = state->wrap & 1;
+ state->mode = HEAD;
+ state->last = 0;
+ state->havedict = 0;
+ state->dmax = 32768U;
+ state->head = Z_NULL;
+ state->hold = 0;
+ state->bits = 0;
+ state->lencode = state->distcode = state->next = state->codes;
+ state->sane = 1;
+ state->back = -1;
+ Tracev((stderr, "inflate: reset\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateReset(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ state->wsize = 0;
+ state->whave = 0;
+ state->wnext = 0;
+ return inflateResetKeep(strm);
+}
+
+int ZEXPORT inflateReset2(strm, windowBits)
+z_streamp strm;
+int windowBits;
+{
+ int wrap;
+ struct inflate_state FAR *state;
+
+ /* get the state */
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* extract wrap request from windowBits parameter */
+ if (windowBits < 0) {
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+ else {
+ wrap = (windowBits >> 4) + 5;
+#ifdef GUNZIP
+ if (windowBits < 48)
+ windowBits &= 15;
+#endif
+ }
+
+ /* set number of window bits, free window if different */
+ if (windowBits && (windowBits < 8 || windowBits > 15))
+ return Z_STREAM_ERROR;
+ if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
+ ZFREE(strm, state->window);
+ state->window = Z_NULL;
+ }
+
+ /* update state and reset the rest of it */
+ state->wrap = wrap;
+ state->wbits = (unsigned)windowBits;
+ return inflateReset(strm);
+}
+
+int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
+z_streamp strm;
+int windowBits;
+const char *version;
+int stream_size;
+{
+ int ret;
+ struct inflate_state FAR *state;
+
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+ stream_size != (int)(sizeof(z_stream)))
+ return Z_VERSION_ERROR;
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+ strm->msg = Z_NULL; /* in case we return an error */
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+ state = (struct inflate_state FAR *)
+ ZALLOC(strm, 1, sizeof(struct inflate_state));
+ if (state == Z_NULL) return Z_MEM_ERROR;
+ Tracev((stderr, "inflate: allocated\n"));
+ strm->state = (struct internal_state FAR *)state;
+ state->strm = strm;
+ state->window = Z_NULL;
+ state->mode = HEAD; /* to pass state test in inflateReset2() */
+ ret = inflateReset2(strm, windowBits);
+ if (ret != Z_OK) {
+ ZFREE(strm, state);
+ strm->state = Z_NULL;
+ }
+ return ret;
+}
+
+int ZEXPORT inflateInit_(strm, version, stream_size)
+z_streamp strm;
+const char *version;
+int stream_size;
+{
+ return inflateInit2_(strm, DEF_WBITS, version, stream_size);
+}
+
+int ZEXPORT inflatePrime(strm, bits, value)
+z_streamp strm;
+int bits;
+int value;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (bits < 0) {
+ state->hold = 0;
+ state->bits = 0;
+ return Z_OK;
+ }
+ if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
+ value &= (1L << bits) - 1;
+ state->hold += (unsigned)value << state->bits;
+ state->bits += (uInt)bits;
+ return Z_OK;
+}
+
+/*
+ Return state with length and distance decoding tables and index sizes set to
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
+ If BUILDFIXED is defined, then instead this routine builds the tables the
+ first time it's called, and returns those tables the first time and
+ thereafter. This reduces the size of the code by about 2K bytes, in
+ exchange for a little execution time. However, BUILDFIXED should not be
+ used for threaded applications, since the rewriting of the tables and virgin
+ may not be thread-safe.
+ */
+local void fixedtables(state)
+struct inflate_state FAR *state;
+{
+#ifdef BUILDFIXED
+ static int virgin = 1;
+ static code *lenfix, *distfix;
+ static code fixed[544];
+
+ /* build fixed huffman tables if first call (may not be thread safe) */
+ if (virgin) {
+ unsigned sym, bits;
+ static code *next;
+
+ /* literal/length table */
+ sym = 0;
+ while (sym < 144) state->lens[sym++] = 8;
+ while (sym < 256) state->lens[sym++] = 9;
+ while (sym < 280) state->lens[sym++] = 7;
+ while (sym < 288) state->lens[sym++] = 8;
+ next = fixed;
+ lenfix = next;
+ bits = 9;
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
+
+ /* distance table */
+ sym = 0;
+ while (sym < 32) state->lens[sym++] = 5;
+ distfix = next;
+ bits = 5;
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
+
+ /* do this just once */
+ virgin = 0;
+ }
+#else /* !BUILDFIXED */
+# include "inffixed.h"
+#endif /* BUILDFIXED */
+ state->lencode = lenfix;
+ state->lenbits = 9;
+ state->distcode = distfix;
+ state->distbits = 5;
+}
+
+#ifdef MAKEFIXED
+#include <stdio.h>
+
+/*
+ Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
+ defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
+ those tables to stdout, which would be piped to inffixed.h. A small program
+ can simply call makefixed to do this:
+
+ void makefixed(void);
+
+ int main(void)
+ {
+ makefixed();
+ return 0;
+ }
+
+ Then that can be linked with zlib built with MAKEFIXED defined and run:
+
+ a.out > inffixed.h
+ */
+void makefixed()
+{
+ unsigned low, size;
+ struct inflate_state state;
+
+ fixedtables(&state);
+ puts(" /* inffixed.h -- table for decoding fixed codes");
+ puts(" * Generated automatically by makefixed().");
+ puts(" */");
+ puts("");
+ puts(" /* WARNING: this file should *not* be used by applications.");
+ puts(" It is part of the implementation of this library and is");
+ puts(" subject to change. Applications should only use zlib.h.");
+ puts(" */");
+ puts("");
+ size = 1U << 9;
+ printf(" static const code lenfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 7) == 0) printf("\n ");
+ printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
+ state.lencode[low].bits, state.lencode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+ size = 1U << 5;
+ printf("\n static const code distfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 6) == 0) printf("\n ");
+ printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
+ state.distcode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+}
+#endif /* MAKEFIXED */
+
+/*
+ Update the window with the last wsize (normally 32K) bytes written before
+ returning. If window does not exist yet, create it. This is only called
+ when a window is already in use, or when output has been written during this
+ inflate call, but the end of the deflate stream has not been reached yet.
+ It is also called to create a window for dictionary data when a dictionary
+ is loaded.
+
+ Providing output buffers larger than 32K to inflate() should provide a speed
+ advantage, since only the last 32K of output is copied to the sliding window
+ upon return from inflate(), and since all distances after the first 32K of
+ output will fall in the output data, making match copies simpler and faster.
+ The advantage may be dependent on the size of the processor's data caches.
+ */
+local int updatewindow(strm, end, copy)
+z_streamp strm;
+const Bytef *end;
+unsigned copy;
+{
+ struct inflate_state FAR *state;
+ unsigned dist;
+
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* if it hasn't been done already, allocate space for the window */
+ if (state->window == Z_NULL) {
+ state->window = (unsigned char FAR *)
+ ZALLOC(strm, 1U << state->wbits,
+ sizeof(unsigned char));
+ if (state->window == Z_NULL) return 1;
+ }
+
+ /* if window not in use yet, initialize */
+ if (state->wsize == 0) {
+ state->wsize = 1U << state->wbits;
+ state->wnext = 0;
+ state->whave = 0;
+ }
+
+ /* copy state->wsize or less output bytes into the circular window */
+ if (copy >= state->wsize) {
+ zmemcpy(state->window, end - state->wsize, state->wsize);
+ state->wnext = 0;
+ state->whave = state->wsize;
+ }
+ else {
+ dist = state->wsize - state->wnext;
+ if (dist > copy) dist = copy;
+ zmemcpy(state->window + state->wnext, end - copy, dist);
+ copy -= dist;
+ if (copy) {
+ zmemcpy(state->window, end - copy, copy);
+ state->wnext = copy;
+ state->whave = state->wsize;
+ }
+ else {
+ state->wnext += dist;
+ if (state->wnext == state->wsize) state->wnext = 0;
+ if (state->whave < state->wsize) state->whave += dist;
+ }
+ }
+ return 0;
+}
+
+/* Macros for inflate(): */
+
+/* check function to use adler32() for zlib or crc32() for gzip */
+#ifdef GUNZIP
+# define UPDATE(check, buf, len) \
+ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
+#else
+# define UPDATE(check, buf, len) adler32(check, buf, len)
+#endif
+
+/* check macros for header crc */
+#ifdef GUNZIP
+# define CRC2(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ check = crc32(check, hbuf, 2); \
+ } while (0)
+
+# define CRC4(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ hbuf[2] = (unsigned char)((word) >> 16); \
+ hbuf[3] = (unsigned char)((word) >> 24); \
+ check = crc32(check, hbuf, 4); \
+ } while (0)
+#endif
+
+/* Load registers with state in inflate() for speed */
+#define LOAD() \
+ do { \
+ put = strm->next_out; \
+ left = strm->avail_out; \
+ next = strm->next_in; \
+ have = strm->avail_in; \
+ hold = state->hold; \
+ bits = state->bits; \
+ } while (0)
+
+/* Restore state from registers in inflate() */
+#define RESTORE() \
+ do { \
+ strm->next_out = put; \
+ strm->avail_out = left; \
+ strm->next_in = next; \
+ strm->avail_in = have; \
+ state->hold = hold; \
+ state->bits = bits; \
+ } while (0)
+
+/* Clear the input bit accumulator */
+#define INITBITS() \
+ do { \
+ hold = 0; \
+ bits = 0; \
+ } while (0)
+
+/* Get a byte of input into the bit accumulator, or return from inflate()
+ if there is no input available. */
+#define PULLBYTE() \
+ do { \
+ if (have == 0) goto inf_leave; \
+ have--; \
+ hold += (unsigned long)(*next++) << bits; \
+ bits += 8; \
+ } while (0)
+
+/* Assure that there are at least n bits in the bit accumulator. If there is
+ not enough available input to do that, then return from inflate(). */
+#define NEEDBITS(n) \
+ do { \
+ while (bits < (unsigned)(n)) \
+ PULLBYTE(); \
+ } while (0)
+
+/* Return the low n bits of the bit accumulator (n < 16) */
+#define BITS(n) \
+ ((unsigned)hold & ((1U << (n)) - 1))
+
+/* Remove n bits from the bit accumulator */
+#define DROPBITS(n) \
+ do { \
+ hold >>= (n); \
+ bits -= (unsigned)(n); \
+ } while (0)
+
+/* Remove zero to seven bits as needed to go to a byte boundary */
+#define BYTEBITS() \
+ do { \
+ hold >>= bits & 7; \
+ bits -= bits & 7; \
+ } while (0)
+
+/*
+ inflate() uses a state machine to process as much input data and generate as
+ much output data as possible before returning. The state machine is
+ structured roughly as follows:
+
+ for (;;) switch (state) {
+ ...
+ case STATEn:
+ if (not enough input data or output space to make progress)
+ return;
+ ... make progress ...
+ state = STATEm;
+ break;
+ ...
+ }
+
+ so when inflate() is called again, the same case is attempted again, and
+ if the appropriate resources are provided, the machine proceeds to the
+ next state. The NEEDBITS() macro is usually the way the state evaluates
+ whether it can proceed or should return. NEEDBITS() does the return if
+ the requested bits are not available. The typical use of the BITS macros
+ is:
+
+ NEEDBITS(n);
+ ... do something with BITS(n) ...
+ DROPBITS(n);
+
+ where NEEDBITS(n) either returns from inflate() if there isn't enough
+ input left to load n bits into the accumulator, or it continues. BITS(n)
+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
+ the low n bits off the accumulator. INITBITS() clears the accumulator
+ and sets the number of available bits to zero. BYTEBITS() discards just
+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
+
+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
+ if there is no input available. The decoding of variable length codes uses
+ PULLBYTE() directly in order to pull just enough bytes to decode the next
+ code, and no more.
+
+ Some states loop until they get enough input, making sure that enough
+ state information is maintained to continue the loop where it left off
+ if NEEDBITS() returns in the loop. For example, want, need, and keep
+ would all have to actually be part of the saved state in case NEEDBITS()
+ returns:
+
+ case STATEw:
+ while (want < need) {
+ NEEDBITS(n);
+ keep[want++] = BITS(n);
+ DROPBITS(n);
+ }
+ state = STATEx;
+ case STATEx:
+
+ As shown above, if the next state is also the next case, then the break
+ is omitted.
+
+ A state may also return if there is not enough output space available to
+ complete that state. Those states are copying stored data, writing a
+ literal byte, and copying a matching string.
+
+ When returning, a "goto inf_leave" is used to update the total counters,
+ update the check value, and determine whether any progress has been made
+ during that inflate() call in order to return the proper return code.
+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
+ When there is a window, goto inf_leave will update the window with the last
+ output written. If a goto inf_leave occurs in the middle of decompression
+ and there is no window currently, goto inf_leave will create one and copy
+ output to the window for the next call of inflate().
+
+ In this implementation, the flush parameter of inflate() only affects the
+ return code (per zlib.h). inflate() always writes as much as possible to
+ strm->next_out, given the space available and the provided input--the effect
+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
+ the allocation of and copying into a sliding window until necessary, which
+ provides the effect documented in zlib.h for Z_FINISH when the entire input
+ stream available. So the only thing the flush parameter actually does is:
+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
+ will return Z_BUF_ERROR if it has not reached the end of the stream.
+ */
+
+int ZEXPORT inflate(strm, flush)
+z_streamp strm;
+int flush;
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *next; /* next input */
+ unsigned char FAR *put; /* next output */
+ unsigned have, left; /* available input and output */
+ unsigned long hold; /* bit buffer */
+ unsigned bits; /* bits in bit buffer */
+ unsigned in, out; /* save starting available input and output */
+ unsigned copy; /* number of stored or match bytes to copy */
+ unsigned char FAR *from; /* where to copy match bytes from */
+ code here; /* current decoding table entry */
+ code last; /* parent table entry */
+ unsigned len; /* length to copy for repeats, bits to drop */
+ int ret; /* return code */
+#ifdef GUNZIP
+ unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
+#endif
+ static const unsigned short order[19] = /* permutation of code lengths */
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
+ (strm->next_in == Z_NULL && strm->avail_in != 0))
+ return Z_STREAM_ERROR;
+
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
+ LOAD();
+ in = have;
+ out = left;
+ ret = Z_OK;
+ for (;;)
+ switch (state->mode) {
+ case HEAD:
+ if (state->wrap == 0) {
+ state->mode = TYPEDO;
+ break;
+ }
+ NEEDBITS(16);
+#ifdef GUNZIP
+ if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
+ if (state->wbits == 0)
+ state->wbits = 15;
+ state->check = crc32(0L, Z_NULL, 0);
+ CRC2(state->check, hold);
+ INITBITS();
+ state->mode = FLAGS;
+ break;
+ }
+ state->flags = 0; /* expect zlib header */
+ if (state->head != Z_NULL)
+ state->head->done = -1;
+ if (!(state->wrap & 1) || /* check if zlib header allowed */
+#else
+ if (
+#endif
+ ((BITS(8) << 8) + (hold >> 8)) % 31) {
+ strm->msg = (char *)"incorrect header check";
+ state->mode = BAD;
+ break;
+ }
+ if (BITS(4) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ DROPBITS(4);
+ len = BITS(4) + 8;
+ if (state->wbits == 0)
+ state->wbits = len;
+ if (len > 15 || len > state->wbits) {
+ strm->msg = (char *)"invalid window size";
+ state->mode = BAD;
+ break;
+ }
+ state->dmax = 1U << len;
+ Tracev((stderr, "inflate: zlib header ok\n"));
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = hold & 0x200 ? DICTID : TYPE;
+ INITBITS();
+ break;
+#ifdef GUNZIP
+ case FLAGS:
+ NEEDBITS(16);
+ state->flags = (int)(hold);
+ if ((state->flags & 0xff) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ if (state->flags & 0xe000) {
+ strm->msg = (char *)"unknown header flags set";
+ state->mode = BAD;
+ break;
+ }
+ if (state->head != Z_NULL)
+ state->head->text = (int)((hold >> 8) & 1);
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ CRC2(state->check, hold);
+ INITBITS();
+ state->mode = TIME;
+ case TIME:
+ NEEDBITS(32);
+ if (state->head != Z_NULL)
+ state->head->time = hold;
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ CRC4(state->check, hold);
+ INITBITS();
+ state->mode = OS;
+ case OS:
+ NEEDBITS(16);
+ if (state->head != Z_NULL) {
+ state->head->xflags = (int)(hold & 0xff);
+ state->head->os = (int)(hold >> 8);
+ }
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ CRC2(state->check, hold);
+ INITBITS();
+ state->mode = EXLEN;
+ case EXLEN:
+ if (state->flags & 0x0400) {
+ NEEDBITS(16);
+ state->length = (unsigned)(hold);
+ if (state->head != Z_NULL)
+ state->head->extra_len = (unsigned)hold;
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ CRC2(state->check, hold);
+ INITBITS();
+ }
+ else if (state->head != Z_NULL)
+ state->head->extra = Z_NULL;
+ state->mode = EXTRA;
+ case EXTRA:
+ if (state->flags & 0x0400) {
+ copy = state->length;
+ if (copy > have) copy = have;
+ if (copy) {
+ if (state->head != Z_NULL &&
+ state->head->extra != Z_NULL) {
+ len = state->head->extra_len - state->length;
+ zmemcpy(state->head->extra + len, next,
+ len + copy > state->head->extra_max ?
+ state->head->extra_max - len : copy);
+ }
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ state->length -= copy;
+ }
+ if (state->length) goto inf_leave;
+ }
+ state->length = 0;
+ state->mode = NAME;
+ case NAME:
+ if (state->flags & 0x0800) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->name != Z_NULL &&
+ state->length < state->head->name_max)
+ state->head->name[state->length++] = (Bytef)len;
+ } while (len && copy < have);
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->name = Z_NULL;
+ state->length = 0;
+ state->mode = COMMENT;
+ case COMMENT:
+ if (state->flags & 0x1000) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->comment != Z_NULL &&
+ state->length < state->head->comm_max)
+ state->head->comment[state->length++] = (Bytef)len;
+ } while (len && copy < have);
+ if ((state->flags & 0x0200) && (state->wrap & 4))
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->comment = Z_NULL;
+ state->mode = HCRC;
+ case HCRC:
+ if (state->flags & 0x0200) {
+ NEEDBITS(16);
+ if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
+ strm->msg = (char *)"header crc mismatch";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ }
+ if (state->head != Z_NULL) {
+ state->head->hcrc = (int)((state->flags >> 9) & 1);
+ state->head->done = 1;
+ }
+ strm->adler = state->check = crc32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ break;
+#endif
+ case DICTID:
+ NEEDBITS(32);
+ strm->adler = state->check = ZSWAP32(hold);
+ INITBITS();
+ state->mode = DICT;
+ case DICT:
+ if (state->havedict == 0) {
+ RESTORE();
+ return Z_NEED_DICT;
+ }
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ case TYPE:
+ if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
+ case TYPEDO:
+ if (state->last) {
+ BYTEBITS();
+ state->mode = CHECK;
+ break;
+ }
+ NEEDBITS(3);
+ state->last = BITS(1);
+ DROPBITS(1);
+ switch (BITS(2)) {
+ case 0: /* stored block */
+ Tracev((stderr, "inflate: stored block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = STORED;
+ break;
+ case 1: /* fixed block */
+ fixedtables(state);
+ Tracev((stderr, "inflate: fixed codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = LEN_; /* decode codes */
+ if (flush == Z_TREES) {
+ DROPBITS(2);
+ goto inf_leave;
+ }
+ break;
+ case 2: /* dynamic block */
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = TABLE;
+ break;
+ case 3:
+ strm->msg = (char *)"invalid block type";
+ state->mode = BAD;
+ }
+ DROPBITS(2);
+ break;
+ case STORED:
+ BYTEBITS(); /* go to byte boundary */
+ NEEDBITS(32);
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
+ strm->msg = (char *)"invalid stored block lengths";
+ state->mode = BAD;
+ break;
+ }
+ state->length = (unsigned)hold & 0xffff;
+ Tracev((stderr, "inflate: stored length %u\n",
+ state->length));
+ INITBITS();
+ state->mode = COPY_;
+ if (flush == Z_TREES) goto inf_leave;
+ case COPY_:
+ state->mode = COPY;
+ case COPY:
+ copy = state->length;
+ if (copy) {
+ if (copy > have) copy = have;
+ if (copy > left) copy = left;
+ if (copy == 0) goto inf_leave;
+ zmemcpy(put, next, copy);
+ have -= copy;
+ next += copy;
+ left -= copy;
+ put += copy;
+ state->length -= copy;
+ break;
+ }
+ Tracev((stderr, "inflate: stored end\n"));
+ state->mode = TYPE;
+ break;
+ case TABLE:
+ NEEDBITS(14);
+ state->nlen = BITS(5) + 257;
+ DROPBITS(5);
+ state->ndist = BITS(5) + 1;
+ DROPBITS(5);
+ state->ncode = BITS(4) + 4;
+ DROPBITS(4);
+#ifndef PKZIP_BUG_WORKAROUND
+ if (state->nlen > 286 || state->ndist > 30) {
+ strm->msg = (char *)"too many length or distance symbols";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracev((stderr, "inflate: table sizes ok\n"));
+ state->have = 0;
+ state->mode = LENLENS;
+ case LENLENS:
+ while (state->have < state->ncode) {
+ NEEDBITS(3);
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
+ DROPBITS(3);
+ }
+ while (state->have < 19)
+ state->lens[order[state->have++]] = 0;
+ state->next = state->codes;
+ state->lencode = (const code FAR *)(state->next);
+ state->lenbits = 7;
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid code lengths set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: code lengths ok\n"));
+ state->have = 0;
+ state->mode = CODELENS;
+ case CODELENS:
+ while (state->have < state->nlen + state->ndist) {
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.val < 16) {
+ DROPBITS(here.bits);
+ state->lens[state->have++] = here.val;
+ }
+ else {
+ if (here.val == 16) {
+ NEEDBITS(here.bits + 2);
+ DROPBITS(here.bits);
+ if (state->have == 0) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ len = state->lens[state->have - 1];
+ copy = 3 + BITS(2);
+ DROPBITS(2);
+ }
+ else if (here.val == 17) {
+ NEEDBITS(here.bits + 3);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 3 + BITS(3);
+ DROPBITS(3);
+ }
+ else {
+ NEEDBITS(here.bits + 7);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 11 + BITS(7);
+ DROPBITS(7);
+ }
+ if (state->have + copy > state->nlen + state->ndist) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ while (copy--)
+ state->lens[state->have++] = (unsigned short)len;
+ }
+ }
+
+ /* handle error breaks in while */
+ if (state->mode == BAD) break;
+
+ /* check for end-of-block code (better have one) */
+ if (state->lens[256] == 0) {
+ strm->msg = (char *)"invalid code -- missing end-of-block";
+ state->mode = BAD;
+ break;
+ }
+
+ /* build code tables -- note: do not change the lenbits or distbits
+ values here (9 and 6) without reading the comments in inftrees.h
+ concerning the ENOUGH constants, which depend on those values */
+ state->next = state->codes;
+ state->lencode = (const code FAR *)(state->next);
+ state->lenbits = 9;
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid literal/lengths set";
+ state->mode = BAD;
+ break;
+ }
+ state->distcode = (const code FAR *)(state->next);
+ state->distbits = 6;
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
+ &(state->next), &(state->distbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid distances set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: codes ok\n"));
+ state->mode = LEN_;
+ if (flush == Z_TREES) goto inf_leave;
+ case LEN_:
+ state->mode = LEN;
+ case LEN:
+ if (have >= 6 && left >= 258) {
+ RESTORE();
+ inflate_fast(strm, out);
+ LOAD();
+ if (state->mode == TYPE)
+ state->back = -1;
+ break;
+ }
+ state->back = 0;
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.op && (here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->lencode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ state->length = (unsigned)here.val;
+ if ((int)(here.op) == 0) {
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ state->mode = LIT;
+ break;
+ }
+ if (here.op & 32) {
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->back = -1;
+ state->mode = TYPE;
+ break;
+ }
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = LENEXT;
+ case LENEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->length += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+ Tracevv((stderr, "inflate: length %u\n", state->length));
+ state->was = state->length;
+ state->mode = DIST;
+ case DIST:
+ for (;;) {
+ here = state->distcode[BITS(state->distbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if ((here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->distcode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ state->offset = (unsigned)here.val;
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = DISTEXT;
+ case DISTEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->offset += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+#ifdef INFLATE_STRICT
+ if (state->offset > state->dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
+ state->mode = MATCH;
+ case MATCH:
+ if (left == 0) goto inf_leave;
+ copy = out - left;
+ if (state->offset > copy) { /* copy from window */
+ copy = state->offset - copy;
+ if (copy > state->whave) {
+ if (state->sane) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ Trace((stderr, "inflate.c too far\n"));
+ copy -= state->whave;
+ if (copy > state->length) copy = state->length;
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = 0;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+#endif
+ }
+ if (copy > state->wnext) {
+ copy -= state->wnext;
+ from = state->window + (state->wsize - copy);
+ }
+ else
+ from = state->window + (state->wnext - copy);
+ if (copy > state->length) copy = state->length;
+ }
+ else { /* copy from output */
+ from = put - state->offset;
+ copy = state->length;
+ }
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = *from++;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+ case LIT:
+ if (left == 0) goto inf_leave;
+ *put++ = (unsigned char)(state->length);
+ left--;
+ state->mode = LEN;
+ break;
+ case CHECK:
+ if (state->wrap) {
+ NEEDBITS(32);
+ out -= left;
+ strm->total_out += out;
+ state->total += out;
+ if ((state->wrap & 4) && out)
+ strm->adler = state->check =
+ UPDATE(state->check, put - out, out);
+ out = left;
+ if ((state->wrap & 4) && (
+#ifdef GUNZIP
+ state->flags ? hold :
+#endif
+ ZSWAP32(hold)) != state->check) {
+ strm->msg = (char *)"incorrect data check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: check matches trailer\n"));
+ }
+#ifdef GUNZIP
+ state->mode = LENGTH;
+ case LENGTH:
+ if (state->wrap && state->flags) {
+ NEEDBITS(32);
+ if (hold != (state->total & 0xffffffffUL)) {
+ strm->msg = (char *)"incorrect length check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: length matches trailer\n"));
+ }
+#endif
+ state->mode = DONE;
+ case DONE:
+ ret = Z_STREAM_END;
+ goto inf_leave;
+ case BAD:
+ ret = Z_DATA_ERROR;
+ goto inf_leave;
+ case MEM:
+ return Z_MEM_ERROR;
+ case SYNC:
+ default:
+ return Z_STREAM_ERROR;
+ }
+
+ /*
+ Return from inflate(), updating the total counts and the check value.
+ If there was no progress during the inflate() call, return a buffer
+ error. Call updatewindow() to create and/or update the window state.
+ Note: a memory error from inflate() is non-recoverable.
+ */
+ inf_leave:
+ RESTORE();
+ if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
+ (state->mode < CHECK || flush != Z_FINISH)))
+ if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ in -= strm->avail_in;
+ out -= strm->avail_out;
+ strm->total_in += in;
+ strm->total_out += out;
+ state->total += out;
+ if ((state->wrap & 4) && out)
+ strm->adler = state->check =
+ UPDATE(state->check, strm->next_out - out, out);
+ strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
+ (state->mode == TYPE ? 128 : 0) +
+ (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
+ if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
+ ret = Z_BUF_ERROR;
+ return ret;
+}
+
+int ZEXPORT inflateEnd(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+ if (inflateStateCheck(strm))
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->window != Z_NULL) ZFREE(strm, state->window);
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+ Tracev((stderr, "inflate: end\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
+z_streamp strm;
+Bytef *dictionary;
+uInt *dictLength;
+{
+ struct inflate_state FAR *state;
+
+ /* check state */
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* copy dictionary */
+ if (state->whave && dictionary != Z_NULL) {
+ zmemcpy(dictionary, state->window + state->wnext,
+ state->whave - state->wnext);
+ zmemcpy(dictionary + state->whave - state->wnext,
+ state->window, state->wnext);
+ }
+ if (dictLength != Z_NULL)
+ *dictLength = state->whave;
+ return Z_OK;
+}
+
+int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
+z_streamp strm;
+const Bytef *dictionary;
+uInt dictLength;
+{
+ struct inflate_state FAR *state;
+ unsigned long dictid;
+ int ret;
+
+ /* check state */
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->wrap != 0 && state->mode != DICT)
+ return Z_STREAM_ERROR;
+
+ /* check for correct dictionary identifier */
+ if (state->mode == DICT) {
+ dictid = adler32(0L, Z_NULL, 0);
+ dictid = adler32(dictid, dictionary, dictLength);
+ if (dictid != state->check)
+ return Z_DATA_ERROR;
+ }
+
+ /* copy dictionary to window using updatewindow(), which will amend the
+ existing dictionary if appropriate */
+ ret = updatewindow(strm, dictionary + dictLength, dictLength);
+ if (ret) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ state->havedict = 1;
+ Tracev((stderr, "inflate: dictionary set\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateGetHeader(strm, head)
+z_streamp strm;
+gz_headerp head;
+{
+ struct inflate_state FAR *state;
+
+ /* check state */
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
+
+ /* save header structure */
+ state->head = head;
+ head->done = 0;
+ return Z_OK;
+}
+
+/*
+ Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
+ or when out of input. When called, *have is the number of pattern bytes
+ found in order so far, in 0..3. On return *have is updated to the new
+ state. If on return *have equals four, then the pattern was found and the
+ return value is how many bytes were read including the last byte of the
+ pattern. If *have is less than four, then the pattern has not been found
+ yet and the return value is len. In the latter case, syncsearch() can be
+ called again with more data and the *have state. *have is initialized to
+ zero for the first call.
+ */
+local unsigned syncsearch(have, buf, len)
+unsigned FAR *have;
+const unsigned char FAR *buf;
+unsigned len;
+{
+ unsigned got;
+ unsigned next;
+
+ got = *have;
+ next = 0;
+ while (next < len && got < 4) {
+ if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
+ got++;
+ else if (buf[next])
+ got = 0;
+ else
+ got = 4 - got;
+ next++;
+ }
+ *have = got;
+ return next;
+}
+
+int ZEXPORT inflateSync(strm)
+z_streamp strm;
+{
+ unsigned len; /* number of bytes to look at or looked at */
+ unsigned long in, out; /* temporary to save total_in and total_out */
+ unsigned char buf[4]; /* to restore bit buffer to byte string */
+ struct inflate_state FAR *state;
+
+ /* check parameters */
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
+
+ /* if first time, start search in bit buffer */
+ if (state->mode != SYNC) {
+ state->mode = SYNC;
+ state->hold <<= state->bits & 7;
+ state->bits -= state->bits & 7;
+ len = 0;
+ while (state->bits >= 8) {
+ buf[len++] = (unsigned char)(state->hold);
+ state->hold >>= 8;
+ state->bits -= 8;
+ }
+ state->have = 0;
+ syncsearch(&(state->have), buf, len);
+ }
+
+ /* search available input */
+ len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
+ strm->avail_in -= len;
+ strm->next_in += len;
+ strm->total_in += len;
+
+ /* return no joy or set up to restart inflate() on a new block */
+ if (state->have != 4) return Z_DATA_ERROR;
+ in = strm->total_in; out = strm->total_out;
+ inflateReset(strm);
+ strm->total_in = in; strm->total_out = out;
+ state->mode = TYPE;
+ return Z_OK;
+}
+
+/*
+ Returns true if inflate is currently at the end of a block generated by
+ Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
+ implementation to provide an additional safety check. PPP uses
+ Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
+ block. When decompressing, PPP checks that at the end of input packet,
+ inflate is waiting for these length bytes.
+ */
+int ZEXPORT inflateSyncPoint(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ return state->mode == STORED && state->bits == 0;
+}
+
+int ZEXPORT inflateCopy(dest, source)
+z_streamp dest;
+z_streamp source;
+{
+ struct inflate_state FAR *state;
+ struct inflate_state FAR *copy;
+ unsigned char FAR *window;
+ unsigned wsize;
+
+ /* check input */
+ if (inflateStateCheck(source) || dest == Z_NULL)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)source->state;
+
+ /* allocate space */
+ copy = (struct inflate_state FAR *)
+ ZALLOC(source, 1, sizeof(struct inflate_state));
+ if (copy == Z_NULL) return Z_MEM_ERROR;
+ window = Z_NULL;
+ if (state->window != Z_NULL) {
+ window = (unsigned char FAR *)
+ ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
+ if (window == Z_NULL) {
+ ZFREE(source, copy);
+ return Z_MEM_ERROR;
+ }
+ }
+
+ /* copy state */
+ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+ zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
+ copy->strm = dest;
+ if (state->lencode >= state->codes &&
+ state->lencode <= state->codes + ENOUGH - 1) {
+ copy->lencode = copy->codes + (state->lencode - state->codes);
+ copy->distcode = copy->codes + (state->distcode - state->codes);
+ }
+ copy->next = copy->codes + (state->next - state->codes);
+ if (window != Z_NULL) {
+ wsize = 1U << state->wbits;
+ zmemcpy(window, state->window, wsize);
+ }
+ copy->window = window;
+ dest->state = (struct internal_state FAR *)copy;
+ return Z_OK;
+}
+
+int ZEXPORT inflateUndermine(strm, subvert)
+z_streamp strm;
+int subvert;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ state->sane = !subvert;
+ return Z_OK;
+#else
+ (void)subvert;
+ state->sane = 1;
+ return Z_DATA_ERROR;
+#endif
+}
+
+int ZEXPORT inflateValidate(strm, check)
+z_streamp strm;
+int check;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (check)
+ state->wrap |= 4;
+ else
+ state->wrap &= ~4;
+ return Z_OK;
+}
+
+long ZEXPORT inflateMark(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+
+ if (inflateStateCheck(strm))
+ return -(1L << 16);
+ state = (struct inflate_state FAR *)strm->state;
+ return (long)(((unsigned long)((long)state->back)) << 16) +
+ (state->mode == COPY ? state->length :
+ (state->mode == MATCH ? state->was - state->length : 0));
+}
+
+unsigned long ZEXPORT inflateCodesUsed(strm)
+z_streamp strm;
+{
+ struct inflate_state FAR *state;
+ if (inflateStateCheck(strm)) return (unsigned long)-1;
+ state = (struct inflate_state FAR *)strm->state;
+ return (unsigned long)(state->next - state->codes);
+}
diff --git a/test/monniaux/zlib-1.2.11/inflate.h b/test/monniaux/zlib-1.2.11/inflate.h
new file mode 100644
index 00000000..a46cce6b
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inflate.h
@@ -0,0 +1,125 @@
+/* inflate.h -- internal inflate state definition
+ * Copyright (C) 1995-2016 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip decoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GUNZIP
+#endif
+
+/* Possible inflate modes between inflate() calls */
+typedef enum {
+ HEAD = 16180, /* i: waiting for magic header */
+ FLAGS, /* i: waiting for method and flags (gzip) */
+ TIME, /* i: waiting for modification time (gzip) */
+ OS, /* i: waiting for extra flags and operating system (gzip) */
+ EXLEN, /* i: waiting for extra length (gzip) */
+ EXTRA, /* i: waiting for extra bytes (gzip) */
+ NAME, /* i: waiting for end of file name (gzip) */
+ COMMENT, /* i: waiting for end of comment (gzip) */
+ HCRC, /* i: waiting for header crc (gzip) */
+ DICTID, /* i: waiting for dictionary check value */
+ DICT, /* waiting for inflateSetDictionary() call */
+ TYPE, /* i: waiting for type bits, including last-flag bit */
+ TYPEDO, /* i: same, but skip check to exit inflate on new block */
+ STORED, /* i: waiting for stored size (length and complement) */
+ COPY_, /* i/o: same as COPY below, but only first time in */
+ COPY, /* i/o: waiting for input or output to copy stored block */
+ TABLE, /* i: waiting for dynamic block table lengths */
+ LENLENS, /* i: waiting for code length code lengths */
+ CODELENS, /* i: waiting for length/lit and distance code lengths */
+ LEN_, /* i: same as LEN below, but only first time in */
+ LEN, /* i: waiting for length/lit/eob code */
+ LENEXT, /* i: waiting for length extra bits */
+ DIST, /* i: waiting for distance code */
+ DISTEXT, /* i: waiting for distance extra bits */
+ MATCH, /* o: waiting for output space to copy string */
+ LIT, /* o: waiting for output space to write literal */
+ CHECK, /* i: waiting for 32-bit check value */
+ LENGTH, /* i: waiting for 32-bit length (gzip) */
+ DONE, /* finished check, done -- remain here until reset */
+ BAD, /* got a data error -- remain here until reset */
+ MEM, /* got an inflate() memory error -- remain here until reset */
+ SYNC /* looking for synchronization bytes to restart inflate() */
+} inflate_mode;
+
+/*
+ State transitions between above modes -
+
+ (most modes can go to BAD or MEM on error -- not shown for clarity)
+
+ Process header:
+ HEAD -> (gzip) or (zlib) or (raw)
+ (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
+ HCRC -> TYPE
+ (zlib) -> DICTID or TYPE
+ DICTID -> DICT -> TYPE
+ (raw) -> TYPEDO
+ Read deflate blocks:
+ TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
+ STORED -> COPY_ -> COPY -> TYPE
+ TABLE -> LENLENS -> CODELENS -> LEN_
+ LEN_ -> LEN
+ Read deflate codes in fixed or dynamic block:
+ LEN -> LENEXT or LIT or TYPE
+ LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
+ LIT -> LEN
+ Process trailer:
+ CHECK -> LENGTH -> DONE
+ */
+
+/* State maintained between inflate() calls -- approximately 7K bytes, not
+ including the allocated sliding window, which is up to 32K bytes. */
+struct inflate_state {
+ z_streamp strm; /* pointer back to this zlib stream */
+ inflate_mode mode; /* current inflate mode */
+ int last; /* true if processing last block */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
+ bit 2 true to validate check value */
+ int havedict; /* true if dictionary provided */
+ int flags; /* gzip header method and flags (0 if zlib) */
+ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
+ unsigned long check; /* protected copy of check value */
+ unsigned long total; /* protected copy of output count */
+ gz_headerp head; /* where to save gzip header information */
+ /* sliding window */
+ unsigned wbits; /* log base 2 of requested window size */
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if needed */
+ /* bit accumulator */
+ unsigned long hold; /* input bit accumulator */
+ unsigned bits; /* number of bits in "in" */
+ /* for string and stored block copying */
+ unsigned length; /* literal or length of data to copy */
+ unsigned offset; /* distance back to copy string from */
+ /* for table and code decoding */
+ unsigned extra; /* extra bits needed */
+ /* fixed and dynamic code tables */
+ code const FAR *lencode; /* starting table for length/literal codes */
+ code const FAR *distcode; /* starting table for distance codes */
+ unsigned lenbits; /* index bits for lencode */
+ unsigned distbits; /* index bits for distcode */
+ /* dynamic table building */
+ unsigned ncode; /* number of code length code lengths */
+ unsigned nlen; /* number of length code lengths */
+ unsigned ndist; /* number of distance code lengths */
+ unsigned have; /* number of code lengths in lens[] */
+ code FAR *next; /* next available space in codes[] */
+ unsigned short lens[320]; /* temporary storage for code lengths */
+ unsigned short work[288]; /* work area for code table building */
+ code codes[ENOUGH]; /* space for code tables */
+ int sane; /* if false, allow invalid distance too far */
+ int back; /* bits back of last unprocessed length/lit */
+ unsigned was; /* initial length of match */
+};
diff --git a/test/monniaux/zlib-1.2.11/inftrees.c b/test/monniaux/zlib-1.2.11/inftrees.c
new file mode 100644
index 00000000..2ea08fc1
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inftrees.c
@@ -0,0 +1,304 @@
+/* inftrees.c -- generate Huffman trees for efficient decoding
+ * Copyright (C) 1995-2017 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+
+#define MAXBITS 15
+
+const char inflate_copyright[] =
+ " inflate 1.2.11 Copyright 1995-2017 Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/*
+ Build a set of tables to decode the provided canonical Huffman code.
+ The code lengths are lens[0..codes-1]. The result starts at *table,
+ whose indices are 0..2^bits-1. work is a writable array of at least
+ lens shorts, which is used as a work area. type is the type of code
+ to be generated, CODES, LENS, or DISTS. On return, zero is success,
+ -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
+ on return points to the next available entry's address. bits is the
+ requested root table index bits, and on return it is the actual root
+ table index bits. It will differ if the request is greater than the
+ longest code or if it is less than the shortest code.
+ */
+int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
+codetype type;
+unsigned short FAR *lens;
+unsigned codes;
+code FAR * FAR *table;
+unsigned FAR *bits;
+unsigned short FAR *work;
+{
+ unsigned len; /* a code's length in bits */
+ unsigned sym; /* index of code symbols */
+ unsigned min, max; /* minimum and maximum code lengths */
+ unsigned root; /* number of index bits for root table */
+ unsigned curr; /* number of index bits for current table */
+ unsigned drop; /* code bits to drop for sub-table */
+ int left; /* number of prefix codes available */
+ unsigned used; /* code entries in table used */
+ unsigned huff; /* Huffman code */
+ unsigned incr; /* for incrementing code, index */
+ unsigned fill; /* index for replicating entries */
+ unsigned low; /* low bits for current root entry */
+ unsigned mask; /* mask for low root bits */
+ code here; /* table entry for duplication */
+ code FAR *next; /* next available space in table */
+ const unsigned short FAR *base; /* base value table to use */
+ const unsigned short FAR *extra; /* extra bits table to use */
+ unsigned match; /* use base and extra for symbol >= match */
+ unsigned short count[MAXBITS+1]; /* number of codes of each length */
+ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
+ static const unsigned short lbase[31] = { /* Length codes 257..285 base */
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
+ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
+ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
+ 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
+ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577, 0, 0};
+ static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
+ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
+ 28, 28, 29, 29, 64, 64};
+
+ /*
+ Process a set of code lengths to create a canonical Huffman code. The
+ code lengths are lens[0..codes-1]. Each length corresponds to the
+ symbols 0..codes-1. The Huffman code is generated by first sorting the
+ symbols by length from short to long, and retaining the symbol order
+ for codes with equal lengths. Then the code starts with all zero bits
+ for the first code of the shortest length, and the codes are integer
+ increments for the same length, and zeros are appended as the length
+ increases. For the deflate format, these bits are stored backwards
+ from their more natural integer increment ordering, and so when the
+ decoding tables are built in the large loop below, the integer codes
+ are incremented backwards.
+
+ This routine assumes, but does not check, that all of the entries in
+ lens[] are in the range 0..MAXBITS. The caller must assure this.
+ 1..MAXBITS is interpreted as that code length. zero means that that
+ symbol does not occur in this code.
+
+ The codes are sorted by computing a count of codes for each length,
+ creating from that a table of starting indices for each length in the
+ sorted table, and then entering the symbols in order in the sorted
+ table. The sorted table is work[], with that space being provided by
+ the caller.
+
+ The length counts are used for other purposes as well, i.e. finding
+ the minimum and maximum length codes, determining if there are any
+ codes at all, checking for a valid set of lengths, and looking ahead
+ at length counts to determine sub-table sizes when building the
+ decoding tables.
+ */
+
+ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
+ for (len = 0; len <= MAXBITS; len++)
+ count[len] = 0;
+ for (sym = 0; sym < codes; sym++)
+ count[lens[sym]]++;
+
+ /* bound code lengths, force root to be within code lengths */
+ root = *bits;
+ for (max = MAXBITS; max >= 1; max--)
+ if (count[max] != 0) break;
+ if (root > max) root = max;
+ if (max == 0) { /* no symbols to code at all */
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)1;
+ here.val = (unsigned short)0;
+ *(*table)++ = here; /* make a table to force an error */
+ *(*table)++ = here;
+ *bits = 1;
+ return 0; /* no symbols, but wait for decoding to report error */
+ }
+ for (min = 1; min < max; min++)
+ if (count[min] != 0) break;
+ if (root < min) root = min;
+
+ /* check for an over-subscribed or incomplete set of lengths */
+ left = 1;
+ for (len = 1; len <= MAXBITS; len++) {
+ left <<= 1;
+ left -= count[len];
+ if (left < 0) return -1; /* over-subscribed */
+ }
+ if (left > 0 && (type == CODES || max != 1))
+ return -1; /* incomplete set */
+
+ /* generate offsets into symbol table for each length for sorting */
+ offs[1] = 0;
+ for (len = 1; len < MAXBITS; len++)
+ offs[len + 1] = offs[len] + count[len];
+
+ /* sort symbols by length, by symbol order within each length */
+ for (sym = 0; sym < codes; sym++)
+ if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
+
+ /*
+ Create and fill in decoding tables. In this loop, the table being
+ filled is at next and has curr index bits. The code being used is huff
+ with length len. That code is converted to an index by dropping drop
+ bits off of the bottom. For codes where len is less than drop + curr,
+ those top drop + curr - len bits are incremented through all values to
+ fill the table with replicated entries.
+
+ root is the number of index bits for the root table. When len exceeds
+ root, sub-tables are created pointed to by the root entry with an index
+ of the low root bits of huff. This is saved in low to check for when a
+ new sub-table should be started. drop is zero when the root table is
+ being filled, and drop is root when sub-tables are being filled.
+
+ When a new sub-table is needed, it is necessary to look ahead in the
+ code lengths to determine what size sub-table is needed. The length
+ counts are used for this, and so count[] is decremented as codes are
+ entered in the tables.
+
+ used keeps track of how many table entries have been allocated from the
+ provided *table space. It is checked for LENS and DIST tables against
+ the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
+ the initial root table size constants. See the comments in inftrees.h
+ for more information.
+
+ sym increments through all symbols, and the loop terminates when
+ all codes of length max, i.e. all codes, have been processed. This
+ routine permits incomplete codes, so another loop after this one fills
+ in the rest of the decoding tables with invalid code markers.
+ */
+
+ /* set up for code type */
+ switch (type) {
+ case CODES:
+ base = extra = work; /* dummy value--not used */
+ match = 20;
+ break;
+ case LENS:
+ base = lbase;
+ extra = lext;
+ match = 257;
+ break;
+ default: /* DISTS */
+ base = dbase;
+ extra = dext;
+ match = 0;
+ }
+
+ /* initialize state for loop */
+ huff = 0; /* starting code */
+ sym = 0; /* starting code symbol */
+ len = min; /* starting code length */
+ next = *table; /* current table to fill in */
+ curr = root; /* current table index bits */
+ drop = 0; /* current bits to drop from code for index */
+ low = (unsigned)(-1); /* trigger new sub-table when len > root */
+ used = 1U << root; /* use root table entries */
+ mask = used - 1; /* mask for comparing low */
+
+ /* check available table space */
+ if ((type == LENS && used > ENOUGH_LENS) ||
+ (type == DISTS && used > ENOUGH_DISTS))
+ return 1;
+
+ /* process all codes and make table entries */
+ for (;;) {
+ /* create table entry */
+ here.bits = (unsigned char)(len - drop);
+ if (work[sym] + 1U < match) {
+ here.op = (unsigned char)0;
+ here.val = work[sym];
+ }
+ else if (work[sym] >= match) {
+ here.op = (unsigned char)(extra[work[sym] - match]);
+ here.val = base[work[sym] - match];
+ }
+ else {
+ here.op = (unsigned char)(32 + 64); /* end of block */
+ here.val = 0;
+ }
+
+ /* replicate for those indices with low len bits equal to huff */
+ incr = 1U << (len - drop);
+ fill = 1U << curr;
+ min = fill; /* save offset to next table */
+ do {
+ fill -= incr;
+ next[(huff >> drop) + fill] = here;
+ } while (fill != 0);
+
+ /* backwards increment the len-bit code huff */
+ incr = 1U << (len - 1);
+ while (huff & incr)
+ incr >>= 1;
+ if (incr != 0) {
+ huff &= incr - 1;
+ huff += incr;
+ }
+ else
+ huff = 0;
+
+ /* go to next symbol, update count, len */
+ sym++;
+ if (--(count[len]) == 0) {
+ if (len == max) break;
+ len = lens[work[sym]];
+ }
+
+ /* create new sub-table if needed */
+ if (len > root && (huff & mask) != low) {
+ /* if first time, transition to sub-tables */
+ if (drop == 0)
+ drop = root;
+
+ /* increment past last table */
+ next += min; /* here min is 1 << curr */
+
+ /* determine length of next table */
+ curr = len - drop;
+ left = (int)(1 << curr);
+ while (curr + drop < max) {
+ left -= count[curr + drop];
+ if (left <= 0) break;
+ curr++;
+ left <<= 1;
+ }
+
+ /* check for enough space */
+ used += 1U << curr;
+ if ((type == LENS && used > ENOUGH_LENS) ||
+ (type == DISTS && used > ENOUGH_DISTS))
+ return 1;
+
+ /* point entry in root table to sub-table */
+ low = huff & mask;
+ (*table)[low].op = (unsigned char)curr;
+ (*table)[low].bits = (unsigned char)root;
+ (*table)[low].val = (unsigned short)(next - *table);
+ }
+ }
+
+ /* fill in remaining table entry if code is incomplete (guaranteed to have
+ at most one remaining entry, since if the code is incomplete, the
+ maximum code length that was allowed to get this far is one bit) */
+ if (huff != 0) {
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)(len - drop);
+ here.val = (unsigned short)0;
+ next[huff] = here;
+ }
+
+ /* set return parameters */
+ *table += used;
+ *bits = root;
+ return 0;
+}
diff --git a/test/monniaux/zlib-1.2.11/inftrees.h b/test/monniaux/zlib-1.2.11/inftrees.h
new file mode 100644
index 00000000..baa53a0b
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/inftrees.h
@@ -0,0 +1,62 @@
+/* inftrees.h -- header to use inftrees.c
+ * Copyright (C) 1995-2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* Structure for decoding tables. Each entry provides either the
+ information needed to do the operation requested by the code that
+ indexed that table entry, or it provides a pointer to another
+ table that indexes more bits of the code. op indicates whether
+ the entry is a pointer to another table, a literal, a length or
+ distance, an end-of-block, or an invalid code. For a table
+ pointer, the low four bits of op is the number of index bits of
+ that table. For a length or distance, the low four bits of op
+ is the number of extra bits to get after the code. bits is
+ the number of bits in this code or part of the code to drop off
+ of the bit buffer. val is the actual byte to output in the case
+ of a literal, the base length or distance, or the offset from
+ the current table to the next table. Each entry is four bytes. */
+typedef struct {
+ unsigned char op; /* operation, extra bits, table bits */
+ unsigned char bits; /* bits in this part of the code */
+ unsigned short val; /* offset in table or code value */
+} code;
+
+/* op values as set by inflate_table():
+ 00000000 - literal
+ 0000tttt - table link, tttt != 0 is the number of table index bits
+ 0001eeee - length or distance, eeee is the number of extra bits
+ 01100000 - end of block
+ 01000000 - invalid code
+ */
+
+/* Maximum size of the dynamic table. The maximum number of code structures is
+ 1444, which is the sum of 852 for literal/length codes and 592 for distance
+ codes. These values were found by exhaustive searches using the program
+ examples/enough.c found in the zlib distribtution. The arguments to that
+ program are the number of symbols, the initial root table size, and the
+ maximum bit length of a code. "enough 286 9 15" for literal/length codes
+ returns returns 852, and "enough 30 6 15" for distance codes returns 592.
+ The initial root table size (9 or 6) is found in the fifth argument of the
+ inflate_table() calls in inflate.c and infback.c. If the root table size is
+ changed, then these maximum sizes would be need to be recalculated and
+ updated. */
+#define ENOUGH_LENS 852
+#define ENOUGH_DISTS 592
+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
+
+/* Type of code to build for inflate_table() */
+typedef enum {
+ CODES,
+ LENS,
+ DISTS
+} codetype;
+
+int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
+ unsigned codes, code FAR * FAR *table,
+ unsigned FAR *bits, unsigned short FAR *work));
diff --git a/test/monniaux/zlib-1.2.11/make.proto b/test/monniaux/zlib-1.2.11/make.proto
new file mode 100644
index 00000000..cb7cf3c2
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/make.proto
@@ -0,0 +1,4 @@
+sources: "$(wildcard *.c)"
+target: minigzip
+measures: [cycles]
+name: zlib
diff --git a/test/monniaux/zlib-1.2.11/minigzip.c b/test/monniaux/zlib-1.2.11/minigzip.c
new file mode 100644
index 00000000..0a88f14b
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/minigzip.c
@@ -0,0 +1,663 @@
+/* minigzip.c -- simulate gzip using the zlib compression library
+ * Copyright (C) 1995-2006, 2010, 2011, 2016 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * minigzip is a minimal implementation of the gzip utility. This is
+ * only an example of using zlib and isn't meant to replace the
+ * full-featured gzip. No attempt is made to deal with file systems
+ * limiting names to 14 or 8+3 characters, etc... Error checking is
+ * very limited. So use minigzip only for testing; use gzip for the
+ * real thing. On MSDOS, use only on file names without extension
+ * or in pipe mode.
+ */
+
+/* @(#) $Id$ */
+
+#define VERIMAG
+#ifdef VERIMAG
+#include "../clock.h"
+#endif
+
+#include "zlib.h"
+#include <stdio.h>
+
+#ifdef STDC
+# include <string.h>
+# include <stdlib.h>
+#endif
+
+#ifdef USE_MMAP
+# include <sys/types.h>
+# include <sys/mman.h>
+# include <sys/stat.h>
+#endif
+
+#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
+# include <fcntl.h>
+# include <io.h>
+# ifdef UNDER_CE
+# include <stdlib.h>
+# endif
+# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
+#else
+# define SET_BINARY_MODE(file)
+#endif
+
+#if defined(_MSC_VER) && _MSC_VER < 1900
+# define snprintf _snprintf
+#endif
+
+#ifdef VMS
+# define unlink delete
+# define GZ_SUFFIX "-gz"
+#endif
+#ifdef RISCOS
+# define unlink remove
+# define GZ_SUFFIX "-gz"
+# define fileno(file) file->__file
+#endif
+#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
+# include <unix.h> /* for fileno */
+#endif
+
+#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
+#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
+ extern int unlink OF((const char *));
+#endif
+#endif
+
+#if defined(UNDER_CE)
+# include <windows.h>
+# define perror(s) pwinerror(s)
+
+/* Map the Windows error number in ERROR to a locale-dependent error
+ message string and return a pointer to it. Typically, the values
+ for ERROR come from GetLastError.
+
+ The string pointed to shall not be modified by the application,
+ but may be overwritten by a subsequent call to strwinerror
+
+ The strwinerror function does not change the current setting
+ of GetLastError. */
+
+static char *strwinerror (error)
+ DWORD error;
+{
+ static char buf[1024];
+
+ wchar_t *msgbuf;
+ DWORD lasterr = GetLastError();
+ DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL,
+ error,
+ 0, /* Default language */
+ (LPVOID)&msgbuf,
+ 0,
+ NULL);
+ if (chars != 0) {
+ /* If there is an \r\n appended, zap it. */
+ if (chars >= 2
+ && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+ chars -= 2;
+ msgbuf[chars] = 0;
+ }
+
+ if (chars > sizeof (buf) - 1) {
+ chars = sizeof (buf) - 1;
+ msgbuf[chars] = 0;
+ }
+
+ wcstombs(buf, msgbuf, chars + 1);
+ LocalFree(msgbuf);
+ }
+ else {
+ sprintf(buf, "unknown win32 error (%ld)", error);
+ }
+
+ SetLastError(lasterr);
+ return buf;
+}
+
+static void pwinerror (s)
+ const char *s;
+{
+ if (s && *s)
+ fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
+ else
+ fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
+}
+
+#endif /* UNDER_CE */
+
+#ifndef GZ_SUFFIX
+# define GZ_SUFFIX ".gz"
+#endif
+#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
+
+#define BUFLEN 16384
+#define MAX_NAME_LEN 1024
+
+#ifdef MAXSEG_64K
+# define local static
+ /* Needed for systems with limitation on stack size. */
+#else
+# define local
+#endif
+
+#ifdef Z_SOLO
+/* for Z_SOLO, create simplified gz* functions using deflate and inflate */
+
+#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
+# include <unistd.h> /* for unlink() */
+#endif
+
+void *myalloc OF((void *, unsigned, unsigned));
+void myfree OF((void *, void *));
+
+void *myalloc(q, n, m)
+ void *q;
+ unsigned n, m;
+{
+ (void)q;
+ return calloc(n, m);
+}
+
+void myfree(q, p)
+ void *q, *p;
+{
+ (void)q;
+ free(p);
+}
+
+typedef struct gzFile_s {
+ FILE *file;
+ int write;
+ int err;
+ char *msg;
+ z_stream strm;
+} *gzFile;
+
+gzFile gzopen OF((const char *, const char *));
+gzFile gzdopen OF((int, const char *));
+gzFile gz_open OF((const char *, int, const char *));
+
+gzFile gzopen(path, mode)
+const char *path;
+const char *mode;
+{
+ return gz_open(path, -1, mode);
+}
+
+gzFile gzdopen(fd, mode)
+int fd;
+const char *mode;
+{
+ return gz_open(NULL, fd, mode);
+}
+
+gzFile gz_open(path, fd, mode)
+ const char *path;
+ int fd;
+ const char *mode;
+{
+ gzFile gz;
+ int ret;
+
+ gz = malloc(sizeof(struct gzFile_s));
+ if (gz == NULL)
+ return NULL;
+ gz->write = strchr(mode, 'w') != NULL;
+ gz->strm.zalloc = myalloc;
+ gz->strm.zfree = myfree;
+ gz->strm.opaque = Z_NULL;
+ if (gz->write)
+ ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
+ else {
+ gz->strm.next_in = 0;
+ gz->strm.avail_in = Z_NULL;
+ ret = inflateInit2(&(gz->strm), 15 + 16);
+ }
+ if (ret != Z_OK) {
+ free(gz);
+ return NULL;
+ }
+ gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
+ fopen(path, gz->write ? "wb" : "rb");
+ if (gz->file == NULL) {
+ gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
+ free(gz);
+ return NULL;
+ }
+ gz->err = 0;
+ gz->msg = "";
+ return gz;
+}
+
+int gzwrite OF((gzFile, const void *, unsigned));
+
+int gzwrite(gz, buf, len)
+ gzFile gz;
+ const void *buf;
+ unsigned len;
+{
+ z_stream *strm;
+ unsigned char out[BUFLEN];
+
+ if (gz == NULL || !gz->write)
+ return 0;
+ strm = &(gz->strm);
+ strm->next_in = (void *)buf;
+ strm->avail_in = len;
+ do {
+ strm->next_out = out;
+ strm->avail_out = BUFLEN;
+ (void)deflate(strm, Z_NO_FLUSH);
+ fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
+ } while (strm->avail_out == 0);
+ return len;
+}
+
+int gzread OF((gzFile, void *, unsigned));
+
+int gzread(gz, buf, len)
+ gzFile gz;
+ void *buf;
+ unsigned len;
+{
+ int ret;
+ unsigned got;
+ unsigned char in[1];
+ z_stream *strm;
+
+ if (gz == NULL || gz->write)
+ return 0;
+ if (gz->err)
+ return 0;
+ strm = &(gz->strm);
+ strm->next_out = (void *)buf;
+ strm->avail_out = len;
+ do {
+ got = fread(in, 1, 1, gz->file);
+ if (got == 0)
+ break;
+ strm->next_in = in;
+ strm->avail_in = 1;
+ ret = inflate(strm, Z_NO_FLUSH);
+ if (ret == Z_DATA_ERROR) {
+ gz->err = Z_DATA_ERROR;
+ gz->msg = strm->msg;
+ return 0;
+ }
+ if (ret == Z_STREAM_END)
+ inflateReset(strm);
+ } while (strm->avail_out);
+ return len - strm->avail_out;
+}
+
+int gzclose OF((gzFile));
+
+int gzclose(gz)
+ gzFile gz;
+{
+ z_stream *strm;
+ unsigned char out[BUFLEN];
+
+ if (gz == NULL)
+ return Z_STREAM_ERROR;
+ strm = &(gz->strm);
+ if (gz->write) {
+ strm->next_in = Z_NULL;
+ strm->avail_in = 0;
+ do {
+ strm->next_out = out;
+ strm->avail_out = BUFLEN;
+ (void)deflate(strm, Z_FINISH);
+ fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
+ } while (strm->avail_out == 0);
+ deflateEnd(strm);
+ }
+ else
+ inflateEnd(strm);
+ fclose(gz->file);
+ free(gz);
+ return Z_OK;
+}
+
+const char *gzerror OF((gzFile, int *));
+
+const char *gzerror(gz, err)
+ gzFile gz;
+ int *err;
+{
+ *err = gz->err;
+ return gz->msg;
+}
+
+#endif
+
+static char *prog;
+
+void error OF((const char *msg));
+void gz_compress OF((FILE *in, gzFile out));
+#ifdef USE_MMAP
+int gz_compress_mmap OF((FILE *in, gzFile out));
+#endif
+void gz_uncompress OF((gzFile in, FILE *out));
+void file_compress OF((char *file, char *mode));
+void file_uncompress OF((char *file));
+int main OF((int argc, char *argv[]));
+
+/* ===========================================================================
+ * Display error message and exit
+ */
+void error(msg)
+ const char *msg;
+{
+ fprintf(stderr, "%s: %s\n", prog, msg);
+ exit(1);
+}
+
+/* ===========================================================================
+ * Compress input to output then close both files.
+ */
+
+void gz_compress(in, out)
+ FILE *in;
+ gzFile out;
+{
+ local char buf[BUFLEN];
+ int len;
+ int err;
+
+#ifdef USE_MMAP
+ /* Try first compressing with mmap. If mmap fails (minigzip used in a
+ * pipe), use the normal fread loop.
+ */
+ if (gz_compress_mmap(in, out) == Z_OK) return;
+#endif
+ for (;;) {
+ len = (int)fread(buf, 1, sizeof(buf), in);
+ if (ferror(in)) {
+ perror("fread");
+ exit(1);
+ }
+ if (len == 0) break;
+
+ if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
+ }
+ fclose(in);
+ if (gzclose(out) != Z_OK) error("failed gzclose");
+}
+
+#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
+
+/* Try compressing the input file at once using mmap. Return Z_OK if
+ * if success, Z_ERRNO otherwise.
+ */
+int gz_compress_mmap(in, out)
+ FILE *in;
+ gzFile out;
+{
+ int len;
+ int err;
+ int ifd = fileno(in);
+ caddr_t buf; /* mmap'ed buffer for the entire input file */
+ off_t buf_len; /* length of the input file */
+ struct stat sb;
+
+ /* Determine the size of the file, needed for mmap: */
+ if (fstat(ifd, &sb) < 0) return Z_ERRNO;
+ buf_len = sb.st_size;
+ if (buf_len <= 0) return Z_ERRNO;
+
+ /* Now do the actual mmap: */
+ buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
+ if (buf == (caddr_t)(-1)) return Z_ERRNO;
+
+ /* Compress the whole file at once: */
+ len = gzwrite(out, (char *)buf, (unsigned)buf_len);
+
+ if (len != (int)buf_len) error(gzerror(out, &err));
+
+ munmap(buf, buf_len);
+ fclose(in);
+ if (gzclose(out) != Z_OK) error("failed gzclose");
+ return Z_OK;
+}
+#endif /* USE_MMAP */
+
+/* ===========================================================================
+ * Uncompress input to output then close both files.
+ */
+void gz_uncompress(in, out)
+ gzFile in;
+ FILE *out;
+{
+ local char buf[BUFLEN];
+ int len;
+ int err;
+
+ for (;;) {
+ len = gzread(in, buf, sizeof(buf));
+ if (len < 0) error (gzerror(in, &err));
+ if (len == 0) break;
+
+ if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
+ error("failed fwrite");
+ }
+ }
+ if (fclose(out)) error("failed fclose");
+
+ if (gzclose(in) != Z_OK) error("failed gzclose");
+}
+
+
+/* ===========================================================================
+ * Compress the given file: create a corresponding .gz file and remove the
+ * original.
+ */
+void file_compress(file, mode)
+ char *file;
+ char *mode;
+{
+ local char outfile[MAX_NAME_LEN];
+ FILE *in;
+ gzFile out;
+
+ if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
+ fprintf(stderr, "%s: filename too long\n", prog);
+ exit(1);
+ }
+
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX);
+#else
+ strcpy(outfile, file);
+ strcat(outfile, GZ_SUFFIX);
+#endif
+
+ in = fopen(file, "rb");
+ if (in == NULL) {
+ perror(file);
+ exit(1);
+ }
+ out = gzopen(outfile, mode);
+ if (out == NULL) {
+ fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
+ exit(1);
+ }
+ gz_compress(in, out);
+
+ unlink(file);
+}
+
+
+/* ===========================================================================
+ * Uncompress the given file and remove the original.
+ */
+void file_uncompress(file)
+ char *file;
+{
+ local char buf[MAX_NAME_LEN];
+ char *infile, *outfile;
+ FILE *out;
+ gzFile in;
+ unsigned len = strlen(file);
+
+ if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
+ fprintf(stderr, "%s: filename too long\n", prog);
+ exit(1);
+ }
+
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(buf, sizeof(buf), "%s", file);
+#else
+ strcpy(buf, file);
+#endif
+
+ if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
+ infile = file;
+ outfile = buf;
+ outfile[len-3] = '\0';
+ } else {
+ outfile = file;
+ infile = buf;
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX);
+#else
+ strcat(infile, GZ_SUFFIX);
+#endif
+ }
+ in = gzopen(infile, "rb");
+ if (in == NULL) {
+ fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
+ exit(1);
+ }
+ out = fopen(outfile, "wb");
+ if (out == NULL) {
+ perror(file);
+ exit(1);
+ }
+
+ gz_uncompress(in, out);
+
+ unlink(infile);
+}
+
+
+/* ===========================================================================
+ * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
+ * -c : write to standard output
+ * -d : decompress
+ * -f : compress with Z_FILTERED
+ * -h : compress with Z_HUFFMAN_ONLY
+ * -r : compress with Z_RLE
+ * -1 to -9 : compression level
+ */
+
+int main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ int copyout = 0;
+ int uncompr = 0;
+ gzFile file;
+ char *bname, outmode[20];
+
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(outmode, sizeof(outmode), "%s", "wb6 ");
+#else
+ strcpy(outmode, "wb6 ");
+#endif
+
+ prog = argv[0];
+ bname = strrchr(argv[0], '/');
+ if (bname)
+ bname++;
+ else
+ bname = argv[0];
+ argc--, argv++;
+
+ if (!strcmp(bname, "gunzip"))
+ uncompr = 1;
+ else if (!strcmp(bname, "zcat"))
+ copyout = uncompr = 1;
+
+ while (argc > 0) {
+ if (strcmp(*argv, "-c") == 0)
+ copyout = 1;
+ else if (strcmp(*argv, "-d") == 0)
+ uncompr = 1;
+ else if (strcmp(*argv, "-f") == 0)
+ outmode[3] = 'f';
+ else if (strcmp(*argv, "-h") == 0)
+ outmode[3] = 'h';
+ else if (strcmp(*argv, "-r") == 0)
+ outmode[3] = 'R';
+ else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
+ (*argv)[2] == 0)
+ outmode[2] = (*argv)[1];
+ else
+ break;
+ argc--, argv++;
+ }
+ if (outmode[3] == ' ')
+ outmode[3] = 0;
+ if (argc == 0) {
+ SET_BINARY_MODE(stdin);
+ SET_BINARY_MODE(stdout);
+ if (uncompr) {
+ file = gzdopen(fileno(stdin), "rb");
+ if (file == NULL) error("can't gzdopen stdin");
+ gz_uncompress(file, stdout);
+ } else {
+ file = gzdopen(fileno(stdout), outmode);
+ if (file == NULL) error("can't gzdopen stdout");
+#ifdef VERIMAG
+ clock_prepare();
+ clock_start();
+#endif
+ gz_compress(stdin, file);
+#ifdef VERIMAG
+ clock_stop();
+ printerr_total_clock();
+#endif
+ }
+ } else {
+ if (copyout) {
+ SET_BINARY_MODE(stdout);
+ }
+ do {
+ if (uncompr) {
+ if (copyout) {
+ file = gzopen(*argv, "rb");
+ if (file == NULL)
+ fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
+ else
+ gz_uncompress(file, stdout);
+ } else {
+ file_uncompress(*argv);
+ }
+ } else {
+ if (copyout) {
+ FILE * in = fopen(*argv, "rb");
+
+ if (in == NULL) {
+ perror(*argv);
+ } else {
+ file = gzdopen(fileno(stdout), outmode);
+ if (file == NULL) error("can't gzdopen stdout");
+ gz_compress(in, file);
+ }
+
+ } else {
+ file_compress(*argv, outmode);
+ }
+ }
+ } while (argv++, --argc);
+ }
+ return 0;
+}
diff --git a/test/monniaux/zlib-1.2.11/trees.c b/test/monniaux/zlib-1.2.11/trees.c
new file mode 100644
index 00000000..50cf4b45
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/trees.c
@@ -0,0 +1,1203 @@
+/* trees.c -- output deflated data using Huffman coding
+ * Copyright (C) 1995-2017 Jean-loup Gailly
+ * detect_data_type() function provided freely by Cosmin Truta, 2006
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process uses several Huffman trees. The more
+ * common source values are represented by shorter bit sequences.
+ *
+ * Each code tree is stored in a compressed form which is itself
+ * a Huffman encoding of the lengths of all the code strings (in
+ * ascending order by source values). The actual code strings are
+ * reconstructed from the lengths in the inflate process, as described
+ * in the deflate specification.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
+ * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
+ *
+ * Storer, James A.
+ * Data Compression: Methods and Theory, pp. 49-50.
+ * Computer Science Press, 1988. ISBN 0-7167-8156-5.
+ *
+ * Sedgewick, R.
+ * Algorithms, p290.
+ * Addison-Wesley, 1983. ISBN 0-201-06672-6.
+ */
+
+/* @(#) $Id$ */
+
+/* #define GEN_TREES_H */
+
+#include "deflate.h"
+
+#ifdef ZLIB_DEBUG
+# include <ctype.h>
+#endif
+
+/* ===========================================================================
+ * Constants
+ */
+
+#define MAX_BL_BITS 7
+/* Bit length codes must not exceed MAX_BL_BITS bits */
+
+#define END_BLOCK 256
+/* end of block literal code */
+
+#define REP_3_6 16
+/* repeat previous bit length 3-6 times (2 bits of repeat count) */
+
+#define REPZ_3_10 17
+/* repeat a zero length 3-10 times (3 bits of repeat count) */
+
+#define REPZ_11_138 18
+/* repeat a zero length 11-138 times (7 bits of repeat count) */
+
+local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
+ = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
+
+local const int extra_dbits[D_CODES] /* extra bits for each distance code */
+ = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
+
+local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
+ = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
+
+local const uch bl_order[BL_CODES]
+ = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
+/* The lengths of the bit length codes are sent in order of decreasing
+ * probability, to avoid transmitting the lengths for unused bit length codes.
+ */
+
+/* ===========================================================================
+ * Local data. These are initialized only once.
+ */
+
+#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+/* non ANSI compilers may not accept trees.h */
+
+local ct_data static_ltree[L_CODES+2];
+/* The static literal tree. Since the bit lengths are imposed, there is no
+ * need for the L_CODES extra codes used during heap construction. However
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
+ * below).
+ */
+
+local ct_data static_dtree[D_CODES];
+/* The static distance tree. (Actually a trivial tree since all codes use
+ * 5 bits.)
+ */
+
+uch _dist_code[DIST_CODE_LEN];
+/* Distance codes. The first 256 values correspond to the distances
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
+ * the 15 bit distances.
+ */
+
+uch _length_code[MAX_MATCH-MIN_MATCH+1];
+/* length code for each normalized match length (0 == MIN_MATCH) */
+
+local int base_length[LENGTH_CODES];
+/* First normalized length for each code (0 = MIN_MATCH) */
+
+local int base_dist[D_CODES];
+/* First normalized distance for each code (0 = distance of 1) */
+
+#else
+# include "trees.h"
+#endif /* GEN_TREES_H */
+
+struct static_tree_desc_s {
+ const ct_data *static_tree; /* static tree or NULL */
+ const intf *extra_bits; /* extra bits for each code or NULL */
+ int extra_base; /* base index for extra_bits */
+ int elems; /* max number of elements in the tree */
+ int max_length; /* max bit length for the codes */
+};
+
+local const static_tree_desc static_l_desc =
+{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
+
+local const static_tree_desc static_d_desc =
+{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
+
+local const static_tree_desc static_bl_desc =
+{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
+
+/* ===========================================================================
+ * Local (static) routines in this file.
+ */
+
+local void tr_static_init OF((void));
+local void init_block OF((deflate_state *s));
+local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
+local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
+local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
+local void build_tree OF((deflate_state *s, tree_desc *desc));
+local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local int build_bl_tree OF((deflate_state *s));
+local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
+ int blcodes));
+local void compress_block OF((deflate_state *s, const ct_data *ltree,
+ const ct_data *dtree));
+local int detect_data_type OF((deflate_state *s));
+local unsigned bi_reverse OF((unsigned value, int length));
+local void bi_windup OF((deflate_state *s));
+local void bi_flush OF((deflate_state *s));
+
+#ifdef GEN_TREES_H
+local void gen_trees_header OF((void));
+#endif
+
+#ifndef ZLIB_DEBUG
+# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
+ /* Send a code of the given tree. c and tree must not have side effects */
+
+#else /* !ZLIB_DEBUG */
+# define send_code(s, c, tree) \
+ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
+ send_bits(s, tree[c].Code, tree[c].Len); }
+#endif
+
+/* ===========================================================================
+ * Output a short LSB first on the stream.
+ * IN assertion: there is enough room in pendingBuf.
+ */
+#define put_short(s, w) { \
+ put_byte(s, (uch)((w) & 0xff)); \
+ put_byte(s, (uch)((ush)(w) >> 8)); \
+}
+
+/* ===========================================================================
+ * Send a value on a given number of bits.
+ * IN assertion: length <= 16 and value fits in length bits.
+ */
+#ifdef ZLIB_DEBUG
+local void send_bits OF((deflate_state *s, int value, int length));
+
+local void send_bits(s, value, length)
+ deflate_state *s;
+ int value; /* value to send */
+ int length; /* number of bits */
+{
+ Tracevv((stderr," l %2d v %4x ", length, value));
+ Assert(length > 0 && length <= 15, "invalid length");
+ s->bits_sent += (ulg)length;
+
+ /* If not enough room in bi_buf, use (valid) bits from bi_buf and
+ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
+ * unused bits in value.
+ */
+ if (s->bi_valid > (int)Buf_size - length) {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ put_short(s, s->bi_buf);
+ s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
+ s->bi_valid += length - Buf_size;
+ } else {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ s->bi_valid += length;
+ }
+}
+#else /* !ZLIB_DEBUG */
+
+#define send_bits(s, value, length) \
+{ int len = length;\
+ if (s->bi_valid > (int)Buf_size - len) {\
+ int val = (int)value;\
+ s->bi_buf |= (ush)val << s->bi_valid;\
+ put_short(s, s->bi_buf);\
+ s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
+ s->bi_valid += len - Buf_size;\
+ } else {\
+ s->bi_buf |= (ush)(value) << s->bi_valid;\
+ s->bi_valid += len;\
+ }\
+}
+#endif /* ZLIB_DEBUG */
+
+
+/* the arguments must not have side effects */
+
+/* ===========================================================================
+ * Initialize the various 'constant' tables.
+ */
+local void tr_static_init()
+{
+#if defined(GEN_TREES_H) || !defined(STDC)
+ static int static_init_done = 0;
+ int n; /* iterates over tree elements */
+ int bits; /* bit counter */
+ int length; /* length value */
+ int code; /* code value */
+ int dist; /* distance index */
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ if (static_init_done) return;
+
+ /* For some embedded targets, global variables are not initialized: */
+#ifdef NO_INIT_GLOBAL_POINTERS
+ static_l_desc.static_tree = static_ltree;
+ static_l_desc.extra_bits = extra_lbits;
+ static_d_desc.static_tree = static_dtree;
+ static_d_desc.extra_bits = extra_dbits;
+ static_bl_desc.extra_bits = extra_blbits;
+#endif
+
+ /* Initialize the mapping length (0..255) -> length code (0..28) */
+ length = 0;
+ for (code = 0; code < LENGTH_CODES-1; code++) {
+ base_length[code] = length;
+ for (n = 0; n < (1<<extra_lbits[code]); n++) {
+ _length_code[length++] = (uch)code;
+ }
+ }
+ Assert (length == 256, "tr_static_init: length != 256");
+ /* Note that the length 255 (match length 258) can be represented
+ * in two different ways: code 284 + 5 bits or code 285, so we
+ * overwrite length_code[255] to use the best encoding:
+ */
+ _length_code[length-1] = (uch)code;
+
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
+ dist = 0;
+ for (code = 0 ; code < 16; code++) {
+ base_dist[code] = dist;
+ for (n = 0; n < (1<<extra_dbits[code]); n++) {
+ _dist_code[dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: dist != 256");
+ dist >>= 7; /* from now on, all distances are divided by 128 */
+ for ( ; code < D_CODES; code++) {
+ base_dist[code] = dist << 7;
+ for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
+ _dist_code[256 + dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: 256+dist != 512");
+
+ /* Construct the codes of the static literal tree */
+ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
+ n = 0;
+ while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
+ while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
+ while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
+ while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
+ /* Codes 286 and 287 do not exist, but we must include them in the
+ * tree construction to get a canonical Huffman tree (longest code
+ * all ones)
+ */
+ gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
+
+ /* The static distance tree is trivial: */
+ for (n = 0; n < D_CODES; n++) {
+ static_dtree[n].Len = 5;
+ static_dtree[n].Code = bi_reverse((unsigned)n, 5);
+ }
+ static_init_done = 1;
+
+# ifdef GEN_TREES_H
+ gen_trees_header();
+# endif
+#endif /* defined(GEN_TREES_H) || !defined(STDC) */
+}
+
+/* ===========================================================================
+ * Genererate the file trees.h describing the static trees.
+ */
+#ifdef GEN_TREES_H
+# ifndef ZLIB_DEBUG
+# include <stdio.h>
+# endif
+
+# define SEPARATOR(i, last, width) \
+ ((i) == (last)? "\n};\n\n" : \
+ ((i) % (width) == (width)-1 ? ",\n" : ", "))
+
+void gen_trees_header()
+{
+ FILE *header = fopen("trees.h", "w");
+ int i;
+
+ Assert (header != NULL, "Can't open trees.h");
+ fprintf(header,
+ "/* header created automatically with -DGEN_TREES_H */\n\n");
+
+ fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
+ for (i = 0; i < L_CODES+2; i++) {
+ fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
+ static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
+ }
+
+ fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
+ static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
+ }
+
+ fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
+ for (i = 0; i < DIST_CODE_LEN; i++) {
+ fprintf(header, "%2u%s", _dist_code[i],
+ SEPARATOR(i, DIST_CODE_LEN-1, 20));
+ }
+
+ fprintf(header,
+ "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
+ for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
+ fprintf(header, "%2u%s", _length_code[i],
+ SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
+ }
+
+ fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
+ for (i = 0; i < LENGTH_CODES; i++) {
+ fprintf(header, "%1u%s", base_length[i],
+ SEPARATOR(i, LENGTH_CODES-1, 20));
+ }
+
+ fprintf(header, "local const int base_dist[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "%5u%s", base_dist[i],
+ SEPARATOR(i, D_CODES-1, 10));
+ }
+
+ fclose(header);
+}
+#endif /* GEN_TREES_H */
+
+/* ===========================================================================
+ * Initialize the tree data structures for a new zlib stream.
+ */
+void ZLIB_INTERNAL _tr_init(s)
+ deflate_state *s;
+{
+ tr_static_init();
+
+ s->l_desc.dyn_tree = s->dyn_ltree;
+ s->l_desc.stat_desc = &static_l_desc;
+
+ s->d_desc.dyn_tree = s->dyn_dtree;
+ s->d_desc.stat_desc = &static_d_desc;
+
+ s->bl_desc.dyn_tree = s->bl_tree;
+ s->bl_desc.stat_desc = &static_bl_desc;
+
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+#ifdef ZLIB_DEBUG
+ s->compressed_len = 0L;
+ s->bits_sent = 0L;
+#endif
+
+ /* Initialize the first block of the first file: */
+ init_block(s);
+}
+
+/* ===========================================================================
+ * Initialize a new block.
+ */
+local void init_block(s)
+ deflate_state *s;
+{
+ int n; /* iterates over tree elements */
+
+ /* Initialize the trees. */
+ for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
+ for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
+ for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
+
+ s->dyn_ltree[END_BLOCK].Freq = 1;
+ s->opt_len = s->static_len = 0L;
+ s->last_lit = s->matches = 0;
+}
+
+#define SMALLEST 1
+/* Index within the heap array of least frequent node in the Huffman tree */
+
+
+/* ===========================================================================
+ * Remove the smallest element from the heap and recreate the heap with
+ * one less element. Updates heap and heap_len.
+ */
+#define pqremove(s, tree, top) \
+{\
+ top = s->heap[SMALLEST]; \
+ s->heap[SMALLEST] = s->heap[s->heap_len--]; \
+ pqdownheap(s, tree, SMALLEST); \
+}
+
+/* ===========================================================================
+ * Compares to subtrees, using the tree depth as tie breaker when
+ * the subtrees have equal frequency. This minimizes the worst case length.
+ */
+#define smaller(tree, n, m, depth) \
+ (tree[n].Freq < tree[m].Freq || \
+ (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
+
+/* ===========================================================================
+ * Restore the heap property by moving down the tree starting at node k,
+ * exchanging a node with the smallest of its two sons if necessary, stopping
+ * when the heap property is re-established (each father smaller than its
+ * two sons).
+ */
+local void pqdownheap(s, tree, k)
+ deflate_state *s;
+ ct_data *tree; /* the tree to restore */
+ int k; /* node to move down */
+{
+ int v = s->heap[k];
+ int j = k << 1; /* left son of k */
+ while (j <= s->heap_len) {
+ /* Set j to the smallest of the two sons: */
+ if (j < s->heap_len &&
+ smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
+ j++;
+ }
+ /* Exit if v is smaller than both sons */
+ if (smaller(tree, v, s->heap[j], s->depth)) break;
+
+ /* Exchange v with the smallest son */
+ s->heap[k] = s->heap[j]; k = j;
+
+ /* And continue down the tree, setting j to the left son of k */
+ j <<= 1;
+ }
+ s->heap[k] = v;
+}
+
+/* ===========================================================================
+ * Compute the optimal bit lengths for a tree and update the total bit length
+ * for the current block.
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
+ * above are the tree nodes sorted by increasing frequency.
+ * OUT assertions: the field len is set to the optimal bit length, the
+ * array bl_count contains the frequencies for each bit length.
+ * The length opt_len is updated; static_len is also updated if stree is
+ * not null.
+ */
+local void gen_bitlen(s, desc)
+ deflate_state *s;
+ tree_desc *desc; /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ int max_code = desc->max_code;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ const intf *extra = desc->stat_desc->extra_bits;
+ int base = desc->stat_desc->extra_base;
+ int max_length = desc->stat_desc->max_length;
+ int h; /* heap index */
+ int n, m; /* iterate over the tree elements */
+ int bits; /* bit length */
+ int xbits; /* extra bits */
+ ush f; /* frequency */
+ int overflow = 0; /* number of elements with bit length too large */
+
+ for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
+
+ /* In a first pass, compute the optimal bit lengths (which may
+ * overflow in the case of the bit length tree).
+ */
+ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
+
+ for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
+ n = s->heap[h];
+ bits = tree[tree[n].Dad].Len + 1;
+ if (bits > max_length) bits = max_length, overflow++;
+ tree[n].Len = (ush)bits;
+ /* We overwrite tree[n].Dad which is no longer needed */
+
+ if (n > max_code) continue; /* not a leaf node */
+
+ s->bl_count[bits]++;
+ xbits = 0;
+ if (n >= base) xbits = extra[n-base];
+ f = tree[n].Freq;
+ s->opt_len += (ulg)f * (unsigned)(bits + xbits);
+ if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits);
+ }
+ if (overflow == 0) return;
+
+ Tracev((stderr,"\nbit length overflow\n"));
+ /* This happens for example on obj2 and pic of the Calgary corpus */
+
+ /* Find the first bit length which could increase: */
+ do {
+ bits = max_length-1;
+ while (s->bl_count[bits] == 0) bits--;
+ s->bl_count[bits]--; /* move one leaf down the tree */
+ s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
+ s->bl_count[max_length]--;
+ /* The brother of the overflow item also moves one step up,
+ * but this does not affect bl_count[max_length]
+ */
+ overflow -= 2;
+ } while (overflow > 0);
+
+ /* Now recompute all bit lengths, scanning in increasing frequency.
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
+ * lengths instead of fixing only the wrong ones. This idea is taken
+ * from 'ar' written by Haruhiko Okumura.)
+ */
+ for (bits = max_length; bits != 0; bits--) {
+ n = s->bl_count[bits];
+ while (n != 0) {
+ m = s->heap[--h];
+ if (m > max_code) continue;
+ if ((unsigned) tree[m].Len != (unsigned) bits) {
+ Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
+ s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq;
+ tree[m].Len = (ush)bits;
+ }
+ n--;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Generate the codes for a given tree and bit counts (which need not be
+ * optimal).
+ * IN assertion: the array bl_count contains the bit length statistics for
+ * the given tree and the field len is set for all tree elements.
+ * OUT assertion: the field code is set for all tree elements of non
+ * zero code length.
+ */
+local void gen_codes (tree, max_code, bl_count)
+ ct_data *tree; /* the tree to decorate */
+ int max_code; /* largest code with non zero frequency */
+ ushf *bl_count; /* number of codes at each bit length */
+{
+ ush next_code[MAX_BITS+1]; /* next code value for each bit length */
+ unsigned code = 0; /* running code value */
+ int bits; /* bit index */
+ int n; /* code index */
+
+ /* The distribution counts are first used to generate the code values
+ * without bit reversal.
+ */
+ for (bits = 1; bits <= MAX_BITS; bits++) {
+ code = (code + bl_count[bits-1]) << 1;
+ next_code[bits] = (ush)code;
+ }
+ /* Check that the bit counts in bl_count are consistent. The last code
+ * must be all ones.
+ */
+ Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
+ "inconsistent bit counts");
+ Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
+
+ for (n = 0; n <= max_code; n++) {
+ int len = tree[n].Len;
+ if (len == 0) continue;
+ /* Now reverse the bits */
+ tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
+
+ Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
+ n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
+ }
+}
+
+/* ===========================================================================
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
+ * Update the total bit length for the current block.
+ * IN assertion: the field freq is set for all tree elements.
+ * OUT assertions: the fields len and code are set to the optimal bit length
+ * and corresponding code. The length opt_len is updated; static_len is
+ * also updated if stree is not null. The field max_code is set.
+ */
+local void build_tree(s, desc)
+ deflate_state *s;
+ tree_desc *desc; /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ int elems = desc->stat_desc->elems;
+ int n, m; /* iterate over heap elements */
+ int max_code = -1; /* largest code with non zero frequency */
+ int node; /* new node being created */
+
+ /* Construct the initial heap, with least frequent element in
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
+ * heap[0] is not used.
+ */
+ s->heap_len = 0, s->heap_max = HEAP_SIZE;
+
+ for (n = 0; n < elems; n++) {
+ if (tree[n].Freq != 0) {
+ s->heap[++(s->heap_len)] = max_code = n;
+ s->depth[n] = 0;
+ } else {
+ tree[n].Len = 0;
+ }
+ }
+
+ /* The pkzip format requires that at least one distance code exists,
+ * and that at least one bit should be sent even if there is only one
+ * possible code. So to avoid special checks later on we force at least
+ * two codes of non zero frequency.
+ */
+ while (s->heap_len < 2) {
+ node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
+ tree[node].Freq = 1;
+ s->depth[node] = 0;
+ s->opt_len--; if (stree) s->static_len -= stree[node].Len;
+ /* node is 0 or 1 so it does not have extra bits */
+ }
+ desc->max_code = max_code;
+
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
+ * establish sub-heaps of increasing lengths:
+ */
+ for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
+
+ /* Construct the Huffman tree by repeatedly combining the least two
+ * frequent nodes.
+ */
+ node = elems; /* next internal node of the tree */
+ do {
+ pqremove(s, tree, n); /* n = node of least frequency */
+ m = s->heap[SMALLEST]; /* m = node of next least frequency */
+
+ s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
+ s->heap[--(s->heap_max)] = m;
+
+ /* Create a new node father of n and m */
+ tree[node].Freq = tree[n].Freq + tree[m].Freq;
+ s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
+ s->depth[n] : s->depth[m]) + 1);
+ tree[n].Dad = tree[m].Dad = (ush)node;
+#ifdef DUMP_BL_TREE
+ if (tree == s->bl_tree) {
+ fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
+ node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
+ }
+#endif
+ /* and insert the new node in the heap */
+ s->heap[SMALLEST] = node++;
+ pqdownheap(s, tree, SMALLEST);
+
+ } while (s->heap_len >= 2);
+
+ s->heap[--(s->heap_max)] = s->heap[SMALLEST];
+
+ /* At this point, the fields freq and dad are set. We can now
+ * generate the bit lengths.
+ */
+ gen_bitlen(s, (tree_desc *)desc);
+
+ /* The field len is now set, we can generate the bit codes */
+ gen_codes ((ct_data *)tree, max_code, s->bl_count);
+}
+
+/* ===========================================================================
+ * Scan a literal or distance tree to determine the frequencies of the codes
+ * in the bit length tree.
+ */
+local void scan_tree (s, tree, max_code)
+ deflate_state *s;
+ ct_data *tree; /* the tree to be scanned */
+ int max_code; /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ if (nextlen == 0) max_count = 138, min_count = 3;
+ tree[max_code+1].Len = (ush)0xffff; /* guard */
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ s->bl_tree[curlen].Freq += count;
+ } else if (curlen != 0) {
+ if (curlen != prevlen) s->bl_tree[curlen].Freq++;
+ s->bl_tree[REP_3_6].Freq++;
+ } else if (count <= 10) {
+ s->bl_tree[REPZ_3_10].Freq++;
+ } else {
+ s->bl_tree[REPZ_11_138].Freq++;
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Send a literal or distance tree in compressed form, using the codes in
+ * bl_tree.
+ */
+local void send_tree (s, tree, max_code)
+ deflate_state *s;
+ ct_data *tree; /* the tree to be scanned */
+ int max_code; /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ /* tree[max_code+1].Len = -1; */ /* guard already set */
+ if (nextlen == 0) max_count = 138, min_count = 3;
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
+
+ } else if (curlen != 0) {
+ if (curlen != prevlen) {
+ send_code(s, curlen, s->bl_tree); count--;
+ }
+ Assert(count >= 3 && count <= 6, " 3_6?");
+ send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
+
+ } else if (count <= 10) {
+ send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
+
+ } else {
+ send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Construct the Huffman tree for the bit lengths and return the index in
+ * bl_order of the last bit length code to send.
+ */
+local int build_bl_tree(s)
+ deflate_state *s;
+{
+ int max_blindex; /* index of last bit length code of non zero freq */
+
+ /* Determine the bit length frequencies for literal and distance trees */
+ scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
+ scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
+
+ /* Build the bit length tree: */
+ build_tree(s, (tree_desc *)(&(s->bl_desc)));
+ /* opt_len now includes the length of the tree representations, except
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
+ */
+
+ /* Determine the number of bit length codes to send. The pkzip format
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
+ * 3 but the actual value used is 4.)
+ */
+ for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
+ if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
+ }
+ /* Update opt_len to include the bit length tree and counts */
+ s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4;
+ Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
+ s->opt_len, s->static_len));
+
+ return max_blindex;
+}
+
+/* ===========================================================================
+ * Send the header for a block using dynamic Huffman trees: the counts, the
+ * lengths of the bit length codes, the literal tree and the distance tree.
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
+ */
+local void send_all_trees(s, lcodes, dcodes, blcodes)
+ deflate_state *s;
+ int lcodes, dcodes, blcodes; /* number of codes for each tree */
+{
+ int rank; /* index in bl_order */
+
+ Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
+ Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
+ "too many codes");
+ Tracev((stderr, "\nbl counts: "));
+ send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
+ send_bits(s, dcodes-1, 5);
+ send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
+ for (rank = 0; rank < blcodes; rank++) {
+ Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
+ send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
+ }
+ Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
+ Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
+ Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
+}
+
+/* ===========================================================================
+ * Send a stored block
+ */
+void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
+ deflate_state *s;
+ charf *buf; /* input block */
+ ulg stored_len; /* length of input block */
+ int last; /* one if this is the last block for a file */
+{
+ send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
+ bi_windup(s); /* align on byte boundary */
+ put_short(s, (ush)stored_len);
+ put_short(s, (ush)~stored_len);
+ zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
+ s->pending += stored_len;
+#ifdef ZLIB_DEBUG
+ s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
+ s->compressed_len += (stored_len + 4) << 3;
+ s->bits_sent += 2*16;
+ s->bits_sent += stored_len<<3;
+#endif
+}
+
+/* ===========================================================================
+ * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
+ */
+void ZLIB_INTERNAL _tr_flush_bits(s)
+ deflate_state *s;
+{
+ bi_flush(s);
+}
+
+/* ===========================================================================
+ * Send one empty static block to give enough lookahead for inflate.
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
+ */
+void ZLIB_INTERNAL _tr_align(s)
+ deflate_state *s;
+{
+ send_bits(s, STATIC_TREES<<1, 3);
+ send_code(s, END_BLOCK, static_ltree);
+#ifdef ZLIB_DEBUG
+ s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
+#endif
+ bi_flush(s);
+}
+
+/* ===========================================================================
+ * Determine the best encoding for the current block: dynamic trees, static
+ * trees or store, and write out the encoded block.
+ */
+void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
+ deflate_state *s;
+ charf *buf; /* input block, or NULL if too old */
+ ulg stored_len; /* length of input block */
+ int last; /* one if this is the last block for a file */
+{
+ ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
+ int max_blindex = 0; /* index of last bit length code of non zero freq */
+
+ /* Build the Huffman trees unless a stored block is forced */
+ if (s->level > 0) {
+
+ /* Check if the file is binary or text */
+ if (s->strm->data_type == Z_UNKNOWN)
+ s->strm->data_type = detect_data_type(s);
+
+ /* Construct the literal and distance trees */
+ build_tree(s, (tree_desc *)(&(s->l_desc)));
+ Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+
+ build_tree(s, (tree_desc *)(&(s->d_desc)));
+ Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+ /* At this point, opt_len and static_len are the total bit lengths of
+ * the compressed block data, excluding the tree representations.
+ */
+
+ /* Build the bit length tree for the above two trees, and get the index
+ * in bl_order of the last bit length code to send.
+ */
+ max_blindex = build_bl_tree(s);
+
+ /* Determine the best encoding. Compute the block lengths in bytes. */
+ opt_lenb = (s->opt_len+3+7)>>3;
+ static_lenb = (s->static_len+3+7)>>3;
+
+ Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
+ opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
+ s->last_lit));
+
+ if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
+
+ } else {
+ Assert(buf != (char*)0, "lost buf");
+ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
+ }
+
+#ifdef FORCE_STORED
+ if (buf != (char*)0) { /* force stored block */
+#else
+ if (stored_len+4 <= opt_lenb && buf != (char*)0) {
+ /* 4: two words for the lengths */
+#endif
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
+ * Otherwise we can't have processed more than WSIZE input bytes since
+ * the last block flush, because compression would have been
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
+ * transform a block into a stored block.
+ */
+ _tr_stored_block(s, buf, stored_len, last);
+
+#ifdef FORCE_STATIC
+ } else if (static_lenb >= 0) { /* force static trees */
+#else
+ } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
+#endif
+ send_bits(s, (STATIC_TREES<<1)+last, 3);
+ compress_block(s, (const ct_data *)static_ltree,
+ (const ct_data *)static_dtree);
+#ifdef ZLIB_DEBUG
+ s->compressed_len += 3 + s->static_len;
+#endif
+ } else {
+ send_bits(s, (DYN_TREES<<1)+last, 3);
+ send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
+ max_blindex+1);
+ compress_block(s, (const ct_data *)s->dyn_ltree,
+ (const ct_data *)s->dyn_dtree);
+#ifdef ZLIB_DEBUG
+ s->compressed_len += 3 + s->opt_len;
+#endif
+ }
+ Assert (s->compressed_len == s->bits_sent, "bad compressed size");
+ /* The above check is made mod 2^32, for files larger than 512 MB
+ * and uLong implemented on 32 bits.
+ */
+ init_block(s);
+
+ if (last) {
+ bi_windup(s);
+#ifdef ZLIB_DEBUG
+ s->compressed_len += 7; /* align on byte boundary */
+#endif
+ }
+ Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
+ s->compressed_len-7*last));
+}
+
+/* ===========================================================================
+ * Save the match info and tally the frequency counts. Return true if
+ * the current block must be flushed.
+ */
+int ZLIB_INTERNAL _tr_tally (s, dist, lc)
+ deflate_state *s;
+ unsigned dist; /* distance of matched string */
+ unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
+{
+ s->d_buf[s->last_lit] = (ush)dist;
+ s->l_buf[s->last_lit++] = (uch)lc;
+ if (dist == 0) {
+ /* lc is the unmatched char */
+ s->dyn_ltree[lc].Freq++;
+ } else {
+ s->matches++;
+ /* Here, lc is the match length - MIN_MATCH */
+ dist--; /* dist = match distance - 1 */
+ Assert((ush)dist < (ush)MAX_DIST(s) &&
+ (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
+ (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
+
+ s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
+ s->dyn_dtree[d_code(dist)].Freq++;
+ }
+
+#ifdef TRUNCATE_BLOCK
+ /* Try to guess if it is profitable to stop the current block here */
+ if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
+ /* Compute an upper bound for the compressed length */
+ ulg out_length = (ulg)s->last_lit*8L;
+ ulg in_length = (ulg)((long)s->strstart - s->block_start);
+ int dcode;
+ for (dcode = 0; dcode < D_CODES; dcode++) {
+ out_length += (ulg)s->dyn_dtree[dcode].Freq *
+ (5L+extra_dbits[dcode]);
+ }
+ out_length >>= 3;
+ Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
+ s->last_lit, in_length, out_length,
+ 100L - out_length*100L/in_length));
+ if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
+ }
+#endif
+ return (s->last_lit == s->lit_bufsize-1);
+ /* We avoid equality with lit_bufsize because of wraparound at 64K
+ * on 16 bit machines and because stored blocks are restricted to
+ * 64K-1 bytes.
+ */
+}
+
+/* ===========================================================================
+ * Send the block data compressed using the given Huffman trees
+ */
+local void compress_block(s, ltree, dtree)
+ deflate_state *s;
+ const ct_data *ltree; /* literal tree */
+ const ct_data *dtree; /* distance tree */
+{
+ unsigned dist; /* distance of matched string */
+ int lc; /* match length or unmatched char (if dist == 0) */
+ unsigned lx = 0; /* running index in l_buf */
+ unsigned code; /* the code to send */
+ int extra; /* number of extra bits to send */
+
+ if (s->last_lit != 0) do {
+ dist = s->d_buf[lx];
+ lc = s->l_buf[lx++];
+ if (dist == 0) {
+ send_code(s, lc, ltree); /* send a literal byte */
+ Tracecv(isgraph(lc), (stderr," '%c' ", lc));
+ } else {
+ /* Here, lc is the match length - MIN_MATCH */
+ code = _length_code[lc];
+ send_code(s, code+LITERALS+1, ltree); /* send the length code */
+ extra = extra_lbits[code];
+ if (extra != 0) {
+ lc -= base_length[code];
+ send_bits(s, lc, extra); /* send the extra length bits */
+ }
+ dist--; /* dist is now the match distance - 1 */
+ code = d_code(dist);
+ Assert (code < D_CODES, "bad d_code");
+
+ send_code(s, code, dtree); /* send the distance code */
+ extra = extra_dbits[code];
+ if (extra != 0) {
+ dist -= (unsigned)base_dist[code];
+ send_bits(s, dist, extra); /* send the extra distance bits */
+ }
+ } /* literal or match pair ? */
+
+ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
+ Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
+ "pendingBuf overflow");
+
+ } while (lx < s->last_lit);
+
+ send_code(s, END_BLOCK, ltree);
+}
+
+/* ===========================================================================
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
+ * - TEXT if the two conditions below are satisfied:
+ * a) There are no non-portable control characters belonging to the
+ * "black list" (0..6, 14..25, 28..31).
+ * b) There is at least one printable character belonging to the
+ * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
+ * - BINARY otherwise.
+ * - The following partially-portable control characters form a
+ * "gray list" that is ignored in this detection algorithm:
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
+ * IN assertion: the fields Freq of dyn_ltree are set.
+ */
+local int detect_data_type(s)
+ deflate_state *s;
+{
+ /* black_mask is the bit mask of black-listed bytes
+ * set bits 0..6, 14..25, and 28..31
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
+ */
+ unsigned long black_mask = 0xf3ffc07fUL;
+ int n;
+
+ /* Check for non-textual ("black-listed") bytes. */
+ for (n = 0; n <= 31; n++, black_mask >>= 1)
+ if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
+ return Z_BINARY;
+
+ /* Check for textual ("white-listed") bytes. */
+ if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
+ || s->dyn_ltree[13].Freq != 0)
+ return Z_TEXT;
+ for (n = 32; n < LITERALS; n++)
+ if (s->dyn_ltree[n].Freq != 0)
+ return Z_TEXT;
+
+ /* There are no "black-listed" or "white-listed" bytes:
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
+ */
+ return Z_BINARY;
+}
+
+/* ===========================================================================
+ * Reverse the first len bits of a code, using straightforward code (a faster
+ * method would use a table)
+ * IN assertion: 1 <= len <= 15
+ */
+local unsigned bi_reverse(code, len)
+ unsigned code; /* the value to invert */
+ int len; /* its bit length */
+{
+ register unsigned res = 0;
+ do {
+ res |= code & 1;
+ code >>= 1, res <<= 1;
+ } while (--len > 0);
+ return res >> 1;
+}
+
+/* ===========================================================================
+ * Flush the bit buffer, keeping at most 7 bits in it.
+ */
+local void bi_flush(s)
+ deflate_state *s;
+{
+ if (s->bi_valid == 16) {
+ put_short(s, s->bi_buf);
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+ } else if (s->bi_valid >= 8) {
+ put_byte(s, (Byte)s->bi_buf);
+ s->bi_buf >>= 8;
+ s->bi_valid -= 8;
+ }
+}
+
+/* ===========================================================================
+ * Flush the bit buffer and align the output on a byte boundary
+ */
+local void bi_windup(s)
+ deflate_state *s;
+{
+ if (s->bi_valid > 8) {
+ put_short(s, s->bi_buf);
+ } else if (s->bi_valid > 0) {
+ put_byte(s, (Byte)s->bi_buf);
+ }
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+#ifdef ZLIB_DEBUG
+ s->bits_sent = (s->bits_sent+7) & ~7;
+#endif
+}
diff --git a/test/monniaux/zlib-1.2.11/trees.h b/test/monniaux/zlib-1.2.11/trees.h
new file mode 100644
index 00000000..d35639d8
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/trees.h
@@ -0,0 +1,128 @@
+/* header created automatically with -DGEN_TREES_H */
+
+local const ct_data static_ltree[L_CODES+2] = {
+{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
+{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
+{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
+{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
+{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
+{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
+{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
+{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
+{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
+{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
+{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
+{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
+{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
+{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
+{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
+{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
+{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
+{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
+{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
+{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
+{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
+{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
+{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
+{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
+{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
+{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
+{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
+{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
+{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
+{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
+{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
+{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
+{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
+{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
+{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
+{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
+{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
+{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
+{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
+{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
+{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
+{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
+{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
+{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
+{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
+{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
+{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
+{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
+{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
+{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
+{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
+{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
+{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
+{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
+{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
+{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
+{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
+{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
+};
+
+local const ct_data static_dtree[D_CODES] = {
+{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
+{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
+{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
+{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
+{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
+{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
+};
+
+const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
+ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
+ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
+10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
+12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
+18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
+};
+
+const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
+13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
+17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
+19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
+22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
+};
+
+local const int base_length[LENGTH_CODES] = {
+0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
+64, 80, 96, 112, 128, 160, 192, 224, 0
+};
+
+local const int base_dist[D_CODES] = {
+ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
+ 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
+ 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
+};
+
diff --git a/test/monniaux/zlib-1.2.11/uncompr.c b/test/monniaux/zlib-1.2.11/uncompr.c
new file mode 100644
index 00000000..f03a1a86
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/uncompr.c
@@ -0,0 +1,93 @@
+/* uncompr.c -- decompress a memory buffer
+ * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Decompresses the source buffer into the destination buffer. *sourceLen is
+ the byte length of the source buffer. Upon entry, *destLen is the total size
+ of the destination buffer, which must be large enough to hold the entire
+ uncompressed data. (The size of the uncompressed data must have been saved
+ previously by the compressor and transmitted to the decompressor by some
+ mechanism outside the scope of this compression library.) Upon exit,
+ *destLen is the size of the decompressed data and *sourceLen is the number
+ of source bytes consumed. Upon return, source + *sourceLen points to the
+ first unused input byte.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
+ Z_DATA_ERROR if the input data was corrupted, including if the input data is
+ an incomplete zlib stream.
+*/
+int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong *sourceLen;
+{
+ z_stream stream;
+ int err;
+ const uInt max = (uInt)-1;
+ uLong len, left;
+ Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
+
+ len = *sourceLen;
+ if (*destLen) {
+ left = *destLen;
+ *destLen = 0;
+ }
+ else {
+ left = 1;
+ dest = buf;
+ }
+
+ stream.next_in = (z_const Bytef *)source;
+ stream.avail_in = 0;
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+ stream.opaque = (voidpf)0;
+
+ err = inflateInit(&stream);
+ if (err != Z_OK) return err;
+
+ stream.next_out = dest;
+ stream.avail_out = 0;
+
+ do {
+ if (stream.avail_out == 0) {
+ stream.avail_out = left > (uLong)max ? max : (uInt)left;
+ left -= stream.avail_out;
+ }
+ if (stream.avail_in == 0) {
+ stream.avail_in = len > (uLong)max ? max : (uInt)len;
+ len -= stream.avail_in;
+ }
+ err = inflate(&stream, Z_NO_FLUSH);
+ } while (err == Z_OK);
+
+ *sourceLen -= len + stream.avail_in;
+ if (dest != buf)
+ *destLen = stream.total_out;
+ else if (stream.total_out && err == Z_BUF_ERROR)
+ left = 1;
+
+ inflateEnd(&stream);
+ return err == Z_STREAM_END ? Z_OK :
+ err == Z_NEED_DICT ? Z_DATA_ERROR :
+ err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
+ err;
+}
+
+int ZEXPORT uncompress (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+{
+ return uncompress2(dest, destLen, source, &sourceLen);
+}
diff --git a/test/monniaux/zlib-1.2.11/zconf.h b/test/monniaux/zlib-1.2.11/zconf.h
new file mode 100644
index 00000000..5e1d68a0
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/zconf.h
@@ -0,0 +1,534 @@
+/* zconf.h -- configuration of the zlib compression library
+ * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#ifndef ZCONF_H
+#define ZCONF_H
+
+/*
+ * If you *really* need a unique prefix for all types and library functions,
+ * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ * Even better than compiling with -DZ_PREFIX would be to use configure to set
+ * this permanently in zconf.h using "./configure --zprefix".
+ */
+#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */
+# define Z_PREFIX_SET
+
+/* all linked symbols and init macros */
+# define _dist_code z__dist_code
+# define _length_code z__length_code
+# define _tr_align z__tr_align
+# define _tr_flush_bits z__tr_flush_bits
+# define _tr_flush_block z__tr_flush_block
+# define _tr_init z__tr_init
+# define _tr_stored_block z__tr_stored_block
+# define _tr_tally z__tr_tally
+# define adler32 z_adler32
+# define adler32_combine z_adler32_combine
+# define adler32_combine64 z_adler32_combine64
+# define adler32_z z_adler32_z
+# ifndef Z_SOLO
+# define compress z_compress
+# define compress2 z_compress2
+# define compressBound z_compressBound
+# endif
+# define crc32 z_crc32
+# define crc32_combine z_crc32_combine
+# define crc32_combine64 z_crc32_combine64
+# define crc32_z z_crc32_z
+# define deflate z_deflate
+# define deflateBound z_deflateBound
+# define deflateCopy z_deflateCopy
+# define deflateEnd z_deflateEnd
+# define deflateGetDictionary z_deflateGetDictionary
+# define deflateInit z_deflateInit
+# define deflateInit2 z_deflateInit2
+# define deflateInit2_ z_deflateInit2_
+# define deflateInit_ z_deflateInit_
+# define deflateParams z_deflateParams
+# define deflatePending z_deflatePending
+# define deflatePrime z_deflatePrime
+# define deflateReset z_deflateReset
+# define deflateResetKeep z_deflateResetKeep
+# define deflateSetDictionary z_deflateSetDictionary
+# define deflateSetHeader z_deflateSetHeader
+# define deflateTune z_deflateTune
+# define deflate_copyright z_deflate_copyright
+# define get_crc_table z_get_crc_table
+# ifndef Z_SOLO
+# define gz_error z_gz_error
+# define gz_intmax z_gz_intmax
+# define gz_strwinerror z_gz_strwinerror
+# define gzbuffer z_gzbuffer
+# define gzclearerr z_gzclearerr
+# define gzclose z_gzclose
+# define gzclose_r z_gzclose_r
+# define gzclose_w z_gzclose_w
+# define gzdirect z_gzdirect
+# define gzdopen z_gzdopen
+# define gzeof z_gzeof
+# define gzerror z_gzerror
+# define gzflush z_gzflush
+# define gzfread z_gzfread
+# define gzfwrite z_gzfwrite
+# define gzgetc z_gzgetc
+# define gzgetc_ z_gzgetc_
+# define gzgets z_gzgets
+# define gzoffset z_gzoffset
+# define gzoffset64 z_gzoffset64
+# define gzopen z_gzopen
+# define gzopen64 z_gzopen64
+# ifdef _WIN32
+# define gzopen_w z_gzopen_w
+# endif
+# define gzprintf z_gzprintf
+# define gzputc z_gzputc
+# define gzputs z_gzputs
+# define gzread z_gzread
+# define gzrewind z_gzrewind
+# define gzseek z_gzseek
+# define gzseek64 z_gzseek64
+# define gzsetparams z_gzsetparams
+# define gztell z_gztell
+# define gztell64 z_gztell64
+# define gzungetc z_gzungetc
+# define gzvprintf z_gzvprintf
+# define gzwrite z_gzwrite
+# endif
+# define inflate z_inflate
+# define inflateBack z_inflateBack
+# define inflateBackEnd z_inflateBackEnd
+# define inflateBackInit z_inflateBackInit
+# define inflateBackInit_ z_inflateBackInit_
+# define inflateCodesUsed z_inflateCodesUsed
+# define inflateCopy z_inflateCopy
+# define inflateEnd z_inflateEnd
+# define inflateGetDictionary z_inflateGetDictionary
+# define inflateGetHeader z_inflateGetHeader
+# define inflateInit z_inflateInit
+# define inflateInit2 z_inflateInit2
+# define inflateInit2_ z_inflateInit2_
+# define inflateInit_ z_inflateInit_
+# define inflateMark z_inflateMark
+# define inflatePrime z_inflatePrime
+# define inflateReset z_inflateReset
+# define inflateReset2 z_inflateReset2
+# define inflateResetKeep z_inflateResetKeep
+# define inflateSetDictionary z_inflateSetDictionary
+# define inflateSync z_inflateSync
+# define inflateSyncPoint z_inflateSyncPoint
+# define inflateUndermine z_inflateUndermine
+# define inflateValidate z_inflateValidate
+# define inflate_copyright z_inflate_copyright
+# define inflate_fast z_inflate_fast
+# define inflate_table z_inflate_table
+# ifndef Z_SOLO
+# define uncompress z_uncompress
+# define uncompress2 z_uncompress2
+# endif
+# define zError z_zError
+# ifndef Z_SOLO
+# define zcalloc z_zcalloc
+# define zcfree z_zcfree
+# endif
+# define zlibCompileFlags z_zlibCompileFlags
+# define zlibVersion z_zlibVersion
+
+/* all zlib typedefs in zlib.h and zconf.h */
+# define Byte z_Byte
+# define Bytef z_Bytef
+# define alloc_func z_alloc_func
+# define charf z_charf
+# define free_func z_free_func
+# ifndef Z_SOLO
+# define gzFile z_gzFile
+# endif
+# define gz_header z_gz_header
+# define gz_headerp z_gz_headerp
+# define in_func z_in_func
+# define intf z_intf
+# define out_func z_out_func
+# define uInt z_uInt
+# define uIntf z_uIntf
+# define uLong z_uLong
+# define uLongf z_uLongf
+# define voidp z_voidp
+# define voidpc z_voidpc
+# define voidpf z_voidpf
+
+/* all zlib structs in zlib.h and zconf.h */
+# define gz_header_s z_gz_header_s
+# define internal_state z_internal_state
+
+#endif
+
+#if defined(__MSDOS__) && !defined(MSDOS)
+# define MSDOS
+#endif
+#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
+# define OS2
+#endif
+#if defined(_WINDOWS) && !defined(WINDOWS)
+# define WINDOWS
+#endif
+#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
+# ifndef WIN32
+# define WIN32
+# endif
+#endif
+#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
+# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
+# ifndef SYS16BIT
+# define SYS16BIT
+# endif
+# endif
+#endif
+
+/*
+ * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
+ * than 64k bytes at a time (needed on systems with 16-bit int).
+ */
+#ifdef SYS16BIT
+# define MAXSEG_64K
+#endif
+#ifdef MSDOS
+# define UNALIGNED_OK
+#endif
+
+#ifdef __STDC_VERSION__
+# ifndef STDC
+# define STDC
+# endif
+# if __STDC_VERSION__ >= 199901L
+# ifndef STDC99
+# define STDC99
+# endif
+# endif
+#endif
+#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
+# define STDC
+#endif
+
+#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
+# define STDC
+#endif
+
+#ifndef STDC
+# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
+# define const /* note: need a more gentle solution here */
+# endif
+#endif
+
+#if defined(ZLIB_CONST) && !defined(z_const)
+# define z_const const
+#else
+# define z_const
+#endif
+
+#ifdef Z_SOLO
+ typedef unsigned long z_size_t;
+#else
+# define z_longlong long long
+# if defined(NO_SIZE_T)
+ typedef unsigned NO_SIZE_T z_size_t;
+# elif defined(STDC)
+# include <stddef.h>
+ typedef size_t z_size_t;
+# else
+ typedef unsigned long z_size_t;
+# endif
+# undef z_longlong
+#endif
+
+/* Maximum value for memLevel in deflateInit2 */
+#ifndef MAX_MEM_LEVEL
+# ifdef MAXSEG_64K
+# define MAX_MEM_LEVEL 8
+# else
+# define MAX_MEM_LEVEL 9
+# endif
+#endif
+
+/* Maximum value for windowBits in deflateInit2 and inflateInit2.
+ * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
+ * created by gzip. (Files created by minigzip can still be extracted by
+ * gzip.)
+ */
+#ifndef MAX_WBITS
+# define MAX_WBITS 15 /* 32K LZ77 window */
+#endif
+
+/* The memory requirements for deflate are (in bytes):
+ (1 << (windowBits+2)) + (1 << (memLevel+9))
+ that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
+ plus a few kilobytes for small objects. For example, if you want to reduce
+ the default memory requirements from 256K to 128K, compile with
+ make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+ Of course this will generally degrade compression (there's no free lunch).
+
+ The memory requirements for inflate are (in bytes) 1 << windowBits
+ that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
+ for small objects.
+*/
+
+ /* Type declarations */
+
+#ifndef OF /* function prototypes */
+# ifdef STDC
+# define OF(args) args
+# else
+# define OF(args) ()
+# endif
+#endif
+
+#ifndef Z_ARG /* function prototypes for stdarg */
+# if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# define Z_ARG(args) args
+# else
+# define Z_ARG(args) ()
+# endif
+#endif
+
+/* The following definitions for FAR are needed only for MSDOS mixed
+ * model programming (small or medium model with some far allocations).
+ * This was tested only with MSC; for other MSDOS compilers you may have
+ * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
+ * just define FAR to be empty.
+ */
+#ifdef SYS16BIT
+# if defined(M_I86SM) || defined(M_I86MM)
+ /* MSC small or medium model */
+# define SMALL_MEDIUM
+# ifdef _MSC_VER
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+# if (defined(__SMALL__) || defined(__MEDIUM__))
+ /* Turbo C small or medium model */
+# define SMALL_MEDIUM
+# ifdef __BORLANDC__
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+#endif
+
+#if defined(WINDOWS) || defined(WIN32)
+ /* If building or using zlib as a DLL, define ZLIB_DLL.
+ * This is not mandatory, but it offers a little performance increase.
+ */
+# ifdef ZLIB_DLL
+# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
+# ifdef ZLIB_INTERNAL
+# define ZEXTERN extern __declspec(dllexport)
+# else
+# define ZEXTERN extern __declspec(dllimport)
+# endif
+# endif
+# endif /* ZLIB_DLL */
+ /* If building or using zlib with the WINAPI/WINAPIV calling convention,
+ * define ZLIB_WINAPI.
+ * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
+ */
+# ifdef ZLIB_WINAPI
+# ifdef FAR
+# undef FAR
+# endif
+# include <windows.h>
+ /* No need for _export, use ZLIB.DEF instead. */
+ /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+# define ZEXPORT WINAPI
+# ifdef WIN32
+# define ZEXPORTVA WINAPIV
+# else
+# define ZEXPORTVA FAR CDECL
+# endif
+# endif
+#endif
+
+#if defined (__BEOS__)
+# ifdef ZLIB_DLL
+# ifdef ZLIB_INTERNAL
+# define ZEXPORT __declspec(dllexport)
+# define ZEXPORTVA __declspec(dllexport)
+# else
+# define ZEXPORT __declspec(dllimport)
+# define ZEXPORTVA __declspec(dllimport)
+# endif
+# endif
+#endif
+
+#ifndef ZEXTERN
+# define ZEXTERN extern
+#endif
+#ifndef ZEXPORT
+# define ZEXPORT
+#endif
+#ifndef ZEXPORTVA
+# define ZEXPORTVA
+#endif
+
+#ifndef FAR
+# define FAR
+#endif
+
+#if !defined(__MACTYPES__)
+typedef unsigned char Byte; /* 8 bits */
+#endif
+typedef unsigned int uInt; /* 16 bits or more */
+typedef unsigned long uLong; /* 32 bits or more */
+
+#ifdef SMALL_MEDIUM
+ /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
+# define Bytef Byte FAR
+#else
+ typedef Byte FAR Bytef;
+#endif
+typedef char FAR charf;
+typedef int FAR intf;
+typedef uInt FAR uIntf;
+typedef uLong FAR uLongf;
+
+#ifdef STDC
+ typedef void const *voidpc;
+ typedef void FAR *voidpf;
+ typedef void *voidp;
+#else
+ typedef Byte const *voidpc;
+ typedef Byte FAR *voidpf;
+ typedef Byte *voidp;
+#endif
+
+#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC)
+# include <limits.h>
+# if (UINT_MAX == 0xffffffffUL)
+# define Z_U4 unsigned
+# elif (ULONG_MAX == 0xffffffffUL)
+# define Z_U4 unsigned long
+# elif (USHRT_MAX == 0xffffffffUL)
+# define Z_U4 unsigned short
+# endif
+#endif
+
+#ifdef Z_U4
+ typedef Z_U4 z_crc_t;
+#else
+ typedef unsigned long z_crc_t;
+#endif
+
+#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */
+# define Z_HAVE_UNISTD_H
+#endif
+
+#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */
+# define Z_HAVE_STDARG_H
+#endif
+
+#ifdef STDC
+# ifndef Z_SOLO
+# include <sys/types.h> /* for off_t */
+# endif
+#endif
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifndef Z_SOLO
+# include <stdarg.h> /* for va_list */
+# endif
+#endif
+
+#ifdef _WIN32
+# ifndef Z_SOLO
+# include <stddef.h> /* for wchar_t */
+# endif
+#endif
+
+/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
+ * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
+ * though the former does not conform to the LFS document), but considering
+ * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
+ * equivalently requesting no 64-bit operations
+ */
+#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
+# undef _LARGEFILE64_SOURCE
+#endif
+
+#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H)
+# define Z_HAVE_UNISTD_H
+#endif
+#ifndef Z_SOLO
+# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
+# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
+# ifdef VMS
+# include <unixio.h> /* for off_t */
+# endif
+# ifndef z_off_t
+# define z_off_t off_t
+# endif
+# endif
+#endif
+
+#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
+# define Z_LFS64
+#endif
+
+#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
+# define Z_LARGE64
+#endif
+
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
+# define Z_WANT64
+#endif
+
+#if !defined(SEEK_SET) && !defined(Z_SOLO)
+# define SEEK_SET 0 /* Seek from beginning of file. */
+# define SEEK_CUR 1 /* Seek from current position. */
+# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
+#endif
+
+#ifndef z_off_t
+# define z_off_t long
+#endif
+
+#if !defined(_WIN32) && defined(Z_LARGE64)
+# define z_off64_t off64_t
+#else
+# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO)
+# define z_off64_t __int64
+# else
+# define z_off64_t z_off_t
+# endif
+#endif
+
+/* MVS linker does not support external names larger than 8 bytes */
+#if defined(__MVS__)
+ #pragma map(deflateInit_,"DEIN")
+ #pragma map(deflateInit2_,"DEIN2")
+ #pragma map(deflateEnd,"DEEND")
+ #pragma map(deflateBound,"DEBND")
+ #pragma map(inflateInit_,"ININ")
+ #pragma map(inflateInit2_,"ININ2")
+ #pragma map(inflateEnd,"INEND")
+ #pragma map(inflateSync,"INSY")
+ #pragma map(inflateSetDictionary,"INSEDI")
+ #pragma map(compressBound,"CMBND")
+ #pragma map(inflate_table,"INTABL")
+ #pragma map(inflate_fast,"INFA")
+ #pragma map(inflate_copyright,"INCOPY")
+#endif
+
+#endif /* ZCONF_H */
diff --git a/test/monniaux/zlib-1.2.11/zlib.h b/test/monniaux/zlib-1.2.11/zlib.h
new file mode 100644
index 00000000..f09cdaf1
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/zlib.h
@@ -0,0 +1,1912 @@
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+ version 1.2.11, January 15th, 2017
+
+ Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Jean-loup Gailly Mark Adler
+ jloup@gzip.org madler@alumni.caltech.edu
+
+
+ The data format used by the zlib library is described by RFCs (Request for
+ Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
+ (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
+*/
+
+#ifndef ZLIB_H
+#define ZLIB_H
+
+#include "zconf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ZLIB_VERSION "1.2.11"
+#define ZLIB_VERNUM 0x12b0
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 11
+#define ZLIB_VER_SUBREVISION 0
+
+/*
+ The 'zlib' compression library provides in-memory compression and
+ decompression functions, including integrity checks of the uncompressed data.
+ This version of the library supports only one compression method (deflation)
+ but other algorithms will be added later and will have the same stream
+ interface.
+
+ Compression can be done in a single step if the buffers are large enough,
+ or can be done by repeated calls of the compression function. In the latter
+ case, the application must provide more input and/or consume the output
+ (providing more output space) before each call.
+
+ The compressed data format used by default by the in-memory functions is
+ the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
+ around a deflate stream, which is itself documented in RFC 1951.
+
+ The library also supports reading and writing files in gzip (.gz) format
+ with an interface similar to that of stdio using the functions that start
+ with "gz". The gzip format is different from the zlib format. gzip is a
+ gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
+
+ This library can optionally read and write gzip and raw deflate streams in
+ memory as well.
+
+ The zlib format was designed to be compact and fast for use in memory
+ and on communications channels. The gzip format was designed for single-
+ file compression on file systems, has a larger header than zlib to maintain
+ directory information, and uses a different, slower check method than zlib.
+
+ The library does not install any signal handler. The decoder checks
+ the consistency of the compressed data, so the library should never crash
+ even in the case of corrupted input.
+*/
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void (*free_func) OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+ z_const Bytef *next_in; /* next input byte */
+ uInt avail_in; /* number of bytes available at next_in */
+ uLong total_in; /* total number of input bytes read so far */
+
+ Bytef *next_out; /* next output byte will go here */
+ uInt avail_out; /* remaining free space at next_out */
+ uLong total_out; /* total number of bytes output so far */
+
+ z_const char *msg; /* last error message, NULL if no error */
+ struct internal_state FAR *state; /* not visible by applications */
+
+ alloc_func zalloc; /* used to allocate the internal state */
+ free_func zfree; /* used to free the internal state */
+ voidpf opaque; /* private data object passed to zalloc and zfree */
+
+ int data_type; /* best guess about the data type: binary or text
+ for deflate, or the decoding state for inflate */
+ uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */
+ uLong reserved; /* reserved for future use */
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+/*
+ gzip header information passed to and from zlib routines. See RFC 1952
+ for more details on the meanings of these fields.
+*/
+typedef struct gz_header_s {
+ int text; /* true if compressed data believed to be text */
+ uLong time; /* modification time */
+ int xflags; /* extra flags (not used when writing a gzip file) */
+ int os; /* operating system */
+ Bytef *extra; /* pointer to extra field or Z_NULL if none */
+ uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
+ uInt extra_max; /* space at extra (only when reading header) */
+ Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
+ uInt name_max; /* space at name (only when reading header) */
+ Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
+ uInt comm_max; /* space at comment (only when reading header) */
+ int hcrc; /* true if there was or will be a header crc */
+ int done; /* true when done reading gzip header (not used
+ when writing a gzip file) */
+} gz_header;
+
+typedef gz_header FAR *gz_headerp;
+
+/*
+ The application must update next_in and avail_in when avail_in has dropped
+ to zero. It must update next_out and avail_out when avail_out has dropped
+ to zero. The application must initialize zalloc, zfree and opaque before
+ calling the init function. All other fields are set by the compression
+ library and must not be updated by the application.
+
+ The opaque value provided by the application will be passed as the first
+ parameter for calls of zalloc and zfree. This can be useful for custom
+ memory management. The compression library attaches no meaning to the
+ opaque value.
+
+ zalloc must return Z_NULL if there is not enough memory for the object.
+ If zlib is used in a multi-threaded application, zalloc and zfree must be
+ thread safe. In that case, zlib is thread-safe. When zalloc and zfree are
+ Z_NULL on entry to the initialization function, they are set to internal
+ routines that use the standard library functions malloc() and free().
+
+ On 16-bit systems, the functions zalloc and zfree must be able to allocate
+ exactly 65536 bytes, but will not be required to allocate more than this if
+ the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers
+ returned by zalloc for objects of exactly 65536 bytes *must* have their
+ offset normalized to zero. The default allocation function provided by this
+ library ensures this (see zutil.c). To reduce memory requirements and avoid
+ any allocation of 64K objects, at the expense of compression ratio, compile
+ the library with -DMAX_WBITS=14 (see zconf.h).
+
+ The fields total_in and total_out can be used for statistics or progress
+ reports. After compression, total_in holds the total size of the
+ uncompressed data and may be saved for use by the decompressor (particularly
+ if the decompressor wants to decompress everything in a single step).
+*/
+
+ /* constants */
+
+#define Z_NO_FLUSH 0
+#define Z_PARTIAL_FLUSH 1
+#define Z_SYNC_FLUSH 2
+#define Z_FULL_FLUSH 3
+#define Z_FINISH 4
+#define Z_BLOCK 5
+#define Z_TREES 6
+/* Allowed flush values; see deflate() and inflate() below for details */
+
+#define Z_OK 0
+#define Z_STREAM_END 1
+#define Z_NEED_DICT 2
+#define Z_ERRNO (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR (-3)
+#define Z_MEM_ERROR (-4)
+#define Z_BUF_ERROR (-5)
+#define Z_VERSION_ERROR (-6)
+/* Return codes for the compression/decompression functions. Negative values
+ * are errors, positive values are used for special but normal events.
+ */
+
+#define Z_NO_COMPRESSION 0
+#define Z_BEST_SPEED 1
+#define Z_BEST_COMPRESSION 9
+#define Z_DEFAULT_COMPRESSION (-1)
+/* compression levels */
+
+#define Z_FILTERED 1
+#define Z_HUFFMAN_ONLY 2
+#define Z_RLE 3
+#define Z_FIXED 4
+#define Z_DEFAULT_STRATEGY 0
+/* compression strategy; see deflateInit2() below for details */
+
+#define Z_BINARY 0
+#define Z_TEXT 1
+#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
+#define Z_UNKNOWN 2
+/* Possible values of the data_type field for deflate() */
+
+#define Z_DEFLATED 8
+/* The deflate compression method (the only one supported in this version) */
+
+#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
+
+#define zlib_version zlibVersion()
+/* for compatibility with versions < 1.0.2 */
+
+
+ /* basic functions */
+
+ZEXTERN const char * ZEXPORT zlibVersion OF((void));
+/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
+ If the first character differs, the library code actually used is not
+ compatible with the zlib.h header file used by the application. This check
+ is automatically made by deflateInit and inflateInit.
+ */
+
+/*
+ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
+
+ Initializes the internal stream state for compression. The fields
+ zalloc, zfree and opaque must be initialized before by the caller. If
+ zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
+ allocation functions.
+
+ The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
+ 1 gives best speed, 9 gives best compression, 0 gives no compression at all
+ (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION
+ requests a default compromise between speed and compression (currently
+ equivalent to level 6).
+
+ deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if level is not a valid compression level, or
+ Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
+ with the version assumed by the caller (ZLIB_VERSION). msg is set to null
+ if there is no error message. deflateInit does not perform any compression:
+ this will be done by deflate().
+*/
+
+
+ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
+/*
+ deflate compresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. deflate performs one or both of the
+ following actions:
+
+ - Compress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), next_in and avail_in are updated and
+ processing will resume at this point for the next call of deflate().
+
+ - Generate more output starting at next_out and update next_out and avail_out
+ accordingly. This action is forced if the parameter flush is non zero.
+ Forcing flush frequently degrades the compression ratio, so this parameter
+ should be set only when necessary. Some output may be provided even if
+ flush is zero.
+
+ Before the call of deflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating avail_in or avail_out accordingly; avail_out should
+ never be zero before the call. The application can consume the compressed
+ output when it wants, for example when the output buffer is full (avail_out
+ == 0), or after each call of deflate(). If deflate returns Z_OK and with
+ zero avail_out, it must be called again after making room in the output
+ buffer because there might be more output pending. See deflatePending(),
+ which can be used if desired to determine whether or not there is more ouput
+ in that case.
+
+ Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
+ decide how much data to accumulate before producing output, in order to
+ maximize compression.
+
+ If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
+ flushed to the output buffer and the output is aligned on a byte boundary, so
+ that the decompressor can get all input data available so far. (In
+ particular avail_in is zero after the call if enough output space has been
+ provided before the call.) Flushing may degrade compression for some
+ compression algorithms and so it should be used only when necessary. This
+ completes the current deflate block and follows it with an empty stored block
+ that is three bits plus filler bits to the next byte, followed by four bytes
+ (00 00 ff ff).
+
+ If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
+ output buffer, but the output is not aligned to a byte boundary. All of the
+ input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
+ This completes the current deflate block and follows it with an empty fixed
+ codes block that is 10 bits long. This assures that enough bytes are output
+ in order for the decompressor to finish the block before the empty fixed
+ codes block.
+
+ If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
+ for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
+ seven bits of the current block are held to be written as the next byte after
+ the next deflate block is completed. In this case, the decompressor may not
+ be provided enough bits at this point in order to complete decompression of
+ the data provided so far to the compressor. It may need to wait for the next
+ block to be emitted. This is for advanced applications that need to control
+ the emission of deflate blocks.
+
+ If flush is set to Z_FULL_FLUSH, all output is flushed as with
+ Z_SYNC_FLUSH, and the compression state is reset so that decompression can
+ restart from this point if previous compressed data has been damaged or if
+ random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+ compression.
+
+ If deflate returns with avail_out == 0, this function must be called again
+ with the same value of the flush parameter and more output space (updated
+ avail_out), until the flush is complete (deflate returns with non-zero
+ avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+ avail_out is greater than six to avoid repeated flush markers due to
+ avail_out == 0 on return.
+
+ If the parameter flush is set to Z_FINISH, pending input is processed,
+ pending output is flushed and deflate returns with Z_STREAM_END if there was
+ enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this
+ function must be called again with Z_FINISH and more output space (updated
+ avail_out) but no more input data, until it returns with Z_STREAM_END or an
+ error. After deflate has returned Z_STREAM_END, the only possible operations
+ on the stream are deflateReset or deflateEnd.
+
+ Z_FINISH can be used in the first deflate call after deflateInit if all the
+ compression is to be done in a single step. In order to complete in one
+ call, avail_out must be at least the value returned by deflateBound (see
+ below). Then deflate is guaranteed to return Z_STREAM_END. If not enough
+ output space is provided, deflate will not return Z_STREAM_END, and it must
+ be called again as described above.
+
+ deflate() sets strm->adler to the Adler-32 checksum of all input read
+ so far (that is, total_in bytes). If a gzip stream is being generated, then
+ strm->adler will be the CRC-32 checksum of the input read so far. (See
+ deflateInit2 below.)
+
+ deflate() may update strm->data_type if it can make a good guess about
+ the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is
+ considered binary. This field is only for information purposes and does not
+ affect the compression algorithm in any manner.
+
+ deflate() returns Z_OK if some progress has been made (more input
+ processed or more output produced), Z_STREAM_END if all input has been
+ consumed and all output has been produced (only when flush is set to
+ Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
+ if next_in or next_out was Z_NULL or the state was inadvertently written over
+ by the application), or Z_BUF_ERROR if no progress is possible (for example
+ avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and
+ deflate() can be called again with more input and more output space to
+ continue compressing.
+*/
+
+
+ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
+ stream state was inconsistent, Z_DATA_ERROR if the stream was freed
+ prematurely (some input or output was discarded). In the error case, msg
+ may be set but then points to a static string (which must not be
+ deallocated).
+*/
+
+
+/*
+ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
+
+ Initializes the internal stream state for decompression. The fields
+ next_in, avail_in, zalloc, zfree and opaque must be initialized before by
+ the caller. In the current version of inflate, the provided input is not
+ read or consumed. The allocation of a sliding window will be deferred to
+ the first call of inflate (if the decompression does not complete on the
+ first call). If zalloc and zfree are set to Z_NULL, inflateInit updates
+ them to use default allocation functions.
+
+ inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit does not perform any decompression.
+ Actual decompression will be done by inflate(). So next_in, and avail_in,
+ next_out, and avail_out are unused and unchanged. The current
+ implementation of inflateInit() does not process any header information --
+ that is deferred until inflate() is called.
+*/
+
+
+ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
+/*
+ inflate decompresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. inflate performs one or both of the
+ following actions:
+
+ - Decompress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), then next_in and avail_in are updated
+ accordingly, and processing will resume at this point for the next call of
+ inflate().
+
+ - Generate more output starting at next_out and update next_out and avail_out
+ accordingly. inflate() provides as much output as possible, until there is
+ no more input data or no more space in the output buffer (see below about
+ the flush parameter).
+
+ Before the call of inflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating the next_* and avail_* values accordingly. If the
+ caller of inflate() does not provide both available input and available
+ output space, it is possible that there will be no progress made. The
+ application can consume the uncompressed output when it wants, for example
+ when the output buffer is full (avail_out == 0), or after each call of
+ inflate(). If inflate returns Z_OK and with zero avail_out, it must be
+ called again after making room in the output buffer because there might be
+ more output pending.
+
+ The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
+ Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much
+ output as possible to the output buffer. Z_BLOCK requests that inflate()
+ stop if and when it gets to the next deflate block boundary. When decoding
+ the zlib or gzip format, this will cause inflate() to return immediately
+ after the header and before the first block. When doing a raw inflate,
+ inflate() will go ahead and process the first block, and will return when it
+ gets to the end of that block, or when it runs out of data.
+
+ The Z_BLOCK option assists in appending to or combining deflate streams.
+ To assist in this, on return inflate() always sets strm->data_type to the
+ number of unused bits in the last byte taken from strm->next_in, plus 64 if
+ inflate() is currently decoding the last block in the deflate stream, plus
+ 128 if inflate() returned immediately after decoding an end-of-block code or
+ decoding the complete header up to just before the first byte of the deflate
+ stream. The end-of-block will not be indicated until all of the uncompressed
+ data from that block has been written to strm->next_out. The number of
+ unused bits may in general be greater than seven, except when bit 7 of
+ data_type is set, in which case the number of unused bits will be less than
+ eight. data_type is set as noted here every time inflate() returns for all
+ flush options, and so can be used to determine the amount of currently
+ consumed input in bits.
+
+ The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
+ end of each deflate block header is reached, before any actual data in that
+ block is decoded. This allows the caller to determine the length of the
+ deflate block header for later use in random access within a deflate block.
+ 256 is added to the value of strm->data_type when inflate() returns
+ immediately after reaching the end of the deflate block header.
+
+ inflate() should normally be called until it returns Z_STREAM_END or an
+ error. However if all decompression is to be performed in a single step (a
+ single call of inflate), the parameter flush should be set to Z_FINISH. In
+ this case all pending input is processed and all pending output is flushed;
+ avail_out must be large enough to hold all of the uncompressed data for the
+ operation to complete. (The size of the uncompressed data may have been
+ saved by the compressor for this purpose.) The use of Z_FINISH is not
+ required to perform an inflation in one step. However it may be used to
+ inform inflate that a faster approach can be used for the single inflate()
+ call. Z_FINISH also informs inflate to not maintain a sliding window if the
+ stream completes, which reduces inflate's memory footprint. If the stream
+ does not complete, either because not all of the stream is provided or not
+ enough output space is provided, then a sliding window will be allocated and
+ inflate() can be called again to continue the operation as if Z_NO_FLUSH had
+ been used.
+
+ In this implementation, inflate() always flushes as much output as
+ possible to the output buffer, and always uses the faster approach on the
+ first call. So the effects of the flush parameter in this implementation are
+ on the return value of inflate() as noted below, when inflate() returns early
+ when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
+ memory for a sliding window when Z_FINISH is used.
+
+ If a preset dictionary is needed after this call (see inflateSetDictionary
+ below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
+ chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
+ strm->adler to the Adler-32 checksum of all output produced so far (that is,
+ total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
+ below. At the end of the stream, inflate() checks that its computed Adler-32
+ checksum is equal to that saved by the compressor and returns Z_STREAM_END
+ only if the checksum is correct.
+
+ inflate() can decompress and check either zlib-wrapped or gzip-wrapped
+ deflate data. The header type is detected automatically, if requested when
+ initializing with inflateInit2(). Any information contained in the gzip
+ header is not retained unless inflateGetHeader() is used. When processing
+ gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
+ produced so far. The CRC-32 is checked against the gzip trailer, as is the
+ uncompressed length, modulo 2^32.
+
+ inflate() returns Z_OK if some progress has been made (more input processed
+ or more output produced), Z_STREAM_END if the end of the compressed data has
+ been reached and all uncompressed output has been produced, Z_NEED_DICT if a
+ preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
+ corrupted (input stream not conforming to the zlib format or incorrect check
+ value, in which case strm->msg points to a string with a more specific
+ error), Z_STREAM_ERROR if the stream structure was inconsistent (for example
+ next_in or next_out was Z_NULL, or the state was inadvertently written over
+ by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR
+ if no progress was possible or if there was not enough room in the output
+ buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+ inflate() can be called again with more input and more output space to
+ continue decompressing. If Z_DATA_ERROR is returned, the application may
+ then call inflateSync() to look for a good compression block if a partial
+ recovery of the data is to be attempted.
+*/
+
+
+ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state
+ was inconsistent.
+*/
+
+
+ /* Advanced functions */
+
+/*
+ The following functions are needed only in some special applications.
+*/
+
+/*
+ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
+ int level,
+ int method,
+ int windowBits,
+ int memLevel,
+ int strategy));
+
+ This is another version of deflateInit with more compression options. The
+ fields next_in, zalloc, zfree and opaque must be initialized before by the
+ caller.
+
+ The method parameter is the compression method. It must be Z_DEFLATED in
+ this version of the library.
+
+ The windowBits parameter is the base two logarithm of the window size
+ (the size of the history buffer). It should be in the range 8..15 for this
+ version of the library. Larger values of this parameter result in better
+ compression at the expense of memory usage. The default value is 15 if
+ deflateInit is used instead.
+
+ For the current implementation of deflate(), a windowBits value of 8 (a
+ window size of 256 bytes) is not supported. As a result, a request for 8
+ will result in 9 (a 512-byte window). In that case, providing 8 to
+ inflateInit2() will result in an error when the zlib header with 9 is
+ checked against the initialization of inflate(). The remedy is to not use 8
+ with deflateInit2() with this initialization, or at least in that case use 9
+ with inflateInit2().
+
+ windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
+ determines the window size. deflate() will then generate raw deflate data
+ with no zlib header or trailer, and will not compute a check value.
+
+ windowBits can also be greater than 15 for optional gzip encoding. Add
+ 16 to windowBits to write a simple gzip header and trailer around the
+ compressed data instead of a zlib wrapper. The gzip header will have no
+ file name, no extra data, no comment, no modification time (set to zero), no
+ header crc, and the operating system will be set to the appropriate value,
+ if the operating system was determined at compile time. If a gzip stream is
+ being written, strm->adler is a CRC-32 instead of an Adler-32.
+
+ For raw deflate or gzip encoding, a request for a 256-byte window is
+ rejected as invalid, since only the zlib header provides a means of
+ transmitting the window size to the decompressor.
+
+ The memLevel parameter specifies how much memory should be allocated
+ for the internal compression state. memLevel=1 uses minimum memory but is
+ slow and reduces compression ratio; memLevel=9 uses maximum memory for
+ optimal speed. The default value is 8. See zconf.h for total memory usage
+ as a function of windowBits and memLevel.
+
+ The strategy parameter is used to tune the compression algorithm. Use the
+ value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
+ filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
+ string match), or Z_RLE to limit match distances to one (run-length
+ encoding). Filtered data consists mostly of small values with a somewhat
+ random distribution. In this case, the compression algorithm is tuned to
+ compress them better. The effect of Z_FILTERED is to force more Huffman
+ coding and less string matching; it is somewhat intermediate between
+ Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as
+ fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The
+ strategy parameter only affects the compression ratio but not the
+ correctness of the compressed output even if it is not set appropriately.
+ Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
+ decoder for special applications.
+
+ deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
+ method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
+ incompatible with the version assumed by the caller (ZLIB_VERSION). msg is
+ set to null if there is no error message. deflateInit2 does not perform any
+ compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the compression dictionary from the given byte sequence
+ without producing any compressed output. When using the zlib format, this
+ function must be called immediately after deflateInit, deflateInit2 or
+ deflateReset, and before any call of deflate. When doing raw deflate, this
+ function must be called either before any call of deflate, or immediately
+ after the completion of a deflate block, i.e. after all input has been
+ consumed and all output has been delivered when using any of the flush
+ options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The
+ compressor and decompressor must use exactly the same dictionary (see
+ inflateSetDictionary).
+
+ The dictionary should consist of strings (byte sequences) that are likely
+ to be encountered later in the data to be compressed, with the most commonly
+ used strings preferably put towards the end of the dictionary. Using a
+ dictionary is most useful when the data to be compressed is short and can be
+ predicted with good accuracy; the data can then be compressed better than
+ with the default empty dictionary.
+
+ Depending on the size of the compression data structures selected by
+ deflateInit or deflateInit2, a part of the dictionary may in effect be
+ discarded, for example if the dictionary is larger than the window size
+ provided in deflateInit or deflateInit2. Thus the strings most likely to be
+ useful should be put at the end of the dictionary, not at the front. In
+ addition, the current implementation of deflate will use at most the window
+ size minus 262 bytes of the provided dictionary.
+
+ Upon return of this function, strm->adler is set to the Adler-32 value
+ of the dictionary; the decompressor may later use this value to determine
+ which dictionary has been used by the compressor. (The Adler-32 value
+ applies to the whole dictionary even if only a subset of the dictionary is
+ actually used by the compressor.) If a raw deflate was requested, then the
+ Adler-32 value is not computed and strm->adler is not set.
+
+ deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent (for example if deflate has already been called for this stream
+ or if not at a block boundary for raw deflate). deflateSetDictionary does
+ not perform any compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
+ Bytef *dictionary,
+ uInt *dictLength));
+/*
+ Returns the sliding dictionary being maintained by deflate. dictLength is
+ set to the number of bytes in the dictionary, and that many bytes are copied
+ to dictionary. dictionary must have enough space, where 32768 bytes is
+ always enough. If deflateGetDictionary() is called with dictionary equal to
+ Z_NULL, then only the dictionary length is returned, and nothing is copied.
+ Similary, if dictLength is Z_NULL, then it is not set.
+
+ deflateGetDictionary() may return a length less than the window size, even
+ when more than the window size in input has been provided. It may return up
+ to 258 bytes less in that case, due to how zlib's implementation of deflate
+ manages the sliding window and lookahead for matches, where matches can be
+ up to 258 bytes long. If the application needs the last window-size bytes of
+ input, then that would need to be saved by the application outside of zlib.
+
+ deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
+ stream state is inconsistent.
+*/
+
+ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when several compression strategies will be
+ tried, for example when there are several ways of pre-processing the input
+ data with a filter. The streams that will be discarded should then be freed
+ by calling deflateEnd. Note that deflateCopy duplicates the internal
+ compression state which can be quite large, so this strategy is slow and can
+ consume lots of memory.
+
+ deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to deflateEnd followed by deflateInit, but
+ does not free and reallocate the internal compression state. The stream
+ will leave the compression level and any other attributes that may have been
+ set unchanged.
+
+ deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
+ int level,
+ int strategy));
+/*
+ Dynamically update the compression level and compression strategy. The
+ interpretation of level and strategy is as in deflateInit2(). This can be
+ used to switch between compression and straight copy of the input data, or
+ to switch to a different kind of input data requiring a different strategy.
+ If the compression approach (which is a function of the level) or the
+ strategy is changed, and if any input has been consumed in a previous
+ deflate() call, then the input available so far is compressed with the old
+ level and strategy using deflate(strm, Z_BLOCK). There are three approaches
+ for the compression levels 0, 1..3, and 4..9 respectively. The new level
+ and strategy will take effect at the next call of deflate().
+
+ If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
+ not have enough output space to complete, then the parameter change will not
+ take effect. In this case, deflateParams() can be called again with the
+ same parameters and more output space to try again.
+
+ In order to assure a change in the parameters on the first try, the
+ deflate stream should be flushed using deflate() with Z_BLOCK or other flush
+ request until strm.avail_out is not zero, before calling deflateParams().
+ Then no more input data should be provided before the deflateParams() call.
+ If this is done, the old level and strategy will be applied to the data
+ compressed before deflateParams(), and the new level and strategy will be
+ applied to the the data compressed after deflateParams().
+
+ deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream
+ state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if
+ there was not enough output space to complete the compression of the
+ available input data before a change in the strategy or approach. Note that
+ in the case of a Z_BUF_ERROR, the parameters are not changed. A return
+ value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be
+ retried with more output space.
+*/
+
+ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
+ int good_length,
+ int max_lazy,
+ int nice_length,
+ int max_chain));
+/*
+ Fine tune deflate's internal compression parameters. This should only be
+ used by someone who understands the algorithm used by zlib's deflate for
+ searching for the best matching string, and even then only by the most
+ fanatic optimizer trying to squeeze out the last compressed bit for their
+ specific input data. Read the deflate.c source code for the meaning of the
+ max_lazy, good_length, nice_length, and max_chain parameters.
+
+ deflateTune() can be called after deflateInit() or deflateInit2(), and
+ returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
+ */
+
+ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
+ uLong sourceLen));
+/*
+ deflateBound() returns an upper bound on the compressed size after
+ deflation of sourceLen bytes. It must be called after deflateInit() or
+ deflateInit2(), and after deflateSetHeader(), if used. This would be used
+ to allocate an output buffer for deflation in a single pass, and so would be
+ called before deflate(). If that first deflate() call is provided the
+ sourceLen input bytes, an output buffer allocated to the size returned by
+ deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed
+ to return Z_STREAM_END. Note that it is possible for the compressed size to
+ be larger than the value returned by deflateBound() if flush options other
+ than Z_FINISH or Z_NO_FLUSH are used.
+*/
+
+ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm,
+ unsigned *pending,
+ int *bits));
+/*
+ deflatePending() returns the number of bytes and bits of output that have
+ been generated, but not yet provided in the available output. The bytes not
+ provided would be due to the available output space having being consumed.
+ The number of bits of output not provided are between 0 and 7, where they
+ await more bits to join them in order to fill out a full byte. If pending
+ or bits are Z_NULL, then those values are not set.
+
+ deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+ */
+
+ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ deflatePrime() inserts bits in the deflate output stream. The intent
+ is that this function is used to start off the deflate output with the bits
+ leftover from a previous deflate stream when appending to it. As such, this
+ function can only be used for raw deflate, and must be used before the first
+ deflate() call after a deflateInit2() or deflateReset(). bits must be less
+ than or equal to 16, and that many of the least significant bits of value
+ will be inserted in the output.
+
+ deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
+ room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
+ source stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ deflateSetHeader() provides gzip header information for when a gzip
+ stream is requested by deflateInit2(). deflateSetHeader() may be called
+ after deflateInit2() or deflateReset() and before the first call of
+ deflate(). The text, time, os, extra field, name, and comment information
+ in the provided gz_header structure are written to the gzip header (xflag is
+ ignored -- the extra flags are set according to the compression level). The
+ caller must assure that, if not Z_NULL, name and comment are terminated with
+ a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
+ available there. If hcrc is true, a gzip header crc is included. Note that
+ the current versions of the command-line version of gzip (up through version
+ 1.3.x) do not support header crc's, and will report that it is a "multi-part
+ gzip file" and give up.
+
+ If deflateSetHeader is not used, the default gzip header has text false,
+ the time set to zero, and os set to 255, with no extra, name, or comment
+ fields. The gzip header is returned to the default state by deflateReset().
+
+ deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
+ int windowBits));
+
+ This is another version of inflateInit with an extra parameter. The
+ fields next_in, avail_in, zalloc, zfree and opaque must be initialized
+ before by the caller.
+
+ The windowBits parameter is the base two logarithm of the maximum window
+ size (the size of the history buffer). It should be in the range 8..15 for
+ this version of the library. The default value is 15 if inflateInit is used
+ instead. windowBits must be greater than or equal to the windowBits value
+ provided to deflateInit2() while compressing, or it must be equal to 15 if
+ deflateInit2() was not used. If a compressed stream with a larger window
+ size is given as input, inflate() will return with the error code
+ Z_DATA_ERROR instead of trying to allocate a larger window.
+
+ windowBits can also be zero to request that inflate use the window size in
+ the zlib header of the compressed stream.
+
+ windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
+ determines the window size. inflate() will then process raw deflate data,
+ not looking for a zlib or gzip header, not generating a check value, and not
+ looking for any check values for comparison at the end of the stream. This
+ is for use with other formats that use the deflate compressed data format
+ such as zip. Those formats provide their own check values. If a custom
+ format is developed using the raw deflate format for compressed data, it is
+ recommended that a check value such as an Adler-32 or a CRC-32 be applied to
+ the uncompressed data as is done in the zlib, gzip, and zip formats. For
+ most applications, the zlib format should be used as is. Note that comments
+ above on the use in deflateInit2() applies to the magnitude of windowBits.
+
+ windowBits can also be greater than 15 for optional gzip decoding. Add
+ 32 to windowBits to enable zlib and gzip decoding with automatic header
+ detection, or add 16 to decode only the gzip format (the zlib format will
+ return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
+ CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see
+ below), inflate() will not automatically decode concatenated gzip streams.
+ inflate() will return Z_STREAM_END at the end of the gzip stream. The state
+ would need to be reset to continue decoding a subsequent gzip stream.
+
+ inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit2 does not perform any decompression
+ apart from possibly reading the zlib header if present: actual decompression
+ will be done by inflate(). (So next_in and avail_in may be modified, but
+ next_out and avail_out are unused and unchanged.) The current implementation
+ of inflateInit2() does not process any header information -- that is
+ deferred until inflate() is called.
+*/
+
+ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the decompression dictionary from the given uncompressed byte
+ sequence. This function must be called immediately after a call of inflate,
+ if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+ can be determined from the Adler-32 value returned by that call of inflate.
+ The compressor and decompressor must use exactly the same dictionary (see
+ deflateSetDictionary). For raw inflate, this function can be called at any
+ time to set the dictionary. If the provided dictionary is smaller than the
+ window and there is already data in the window, then the provided dictionary
+ will amend what's there. The application must insure that the dictionary
+ that was used for compression is provided.
+
+ inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
+ expected one (incorrect Adler-32 value). inflateSetDictionary does not
+ perform any decompression: this will be done by subsequent calls of
+ inflate().
+*/
+
+ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
+ Bytef *dictionary,
+ uInt *dictLength));
+/*
+ Returns the sliding dictionary being maintained by inflate. dictLength is
+ set to the number of bytes in the dictionary, and that many bytes are copied
+ to dictionary. dictionary must have enough space, where 32768 bytes is
+ always enough. If inflateGetDictionary() is called with dictionary equal to
+ Z_NULL, then only the dictionary length is returned, and nothing is copied.
+ Similary, if dictLength is Z_NULL, then it is not set.
+
+ inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
+ stream state is inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+/*
+ Skips invalid compressed data until a possible full flush point (see above
+ for the description of deflate with Z_FULL_FLUSH) can be found, or until all
+ available input is skipped. No output is provided.
+
+ inflateSync searches for a 00 00 FF FF pattern in the compressed data.
+ All full flush points have this pattern, but not all occurrences of this
+ pattern are full flush points.
+
+ inflateSync returns Z_OK if a possible full flush point has been found,
+ Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
+ has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
+ In the success case, the application may save the current current value of
+ total_in which indicates where valid compressed data was found. In the
+ error case, the application may repeatedly call inflateSync, providing more
+ input each time, until success or end of the input data.
+*/
+
+ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when randomly accessing a large stream. The
+ first pass through the stream can periodically record the inflate state,
+ allowing restarting inflate at those points when randomly accessing the
+ stream.
+
+ inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to inflateEnd followed by inflateInit,
+ but does not free and reallocate the internal decompression state. The
+ stream will keep attributes that may have been set by inflateInit2.
+
+ inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+ int windowBits));
+/*
+ This function is the same as inflateReset, but it also permits changing
+ the wrap and window size requests. The windowBits parameter is interpreted
+ the same as it is for inflateInit2. If the window size is changed, then the
+ memory allocated for the window is freed, and the window will be reallocated
+ by inflate() if needed.
+
+ inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL), or if
+ the windowBits parameter is invalid.
+*/
+
+ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ This function inserts bits in the inflate input stream. The intent is
+ that this function is used to start inflating at a bit position in the
+ middle of a byte. The provided bits will be used before any bytes are used
+ from next_in. This function should only be used with raw inflate, and
+ should be used before the first inflate() call after inflateInit2() or
+ inflateReset(). bits must be less than or equal to 16, and that many of the
+ least significant bits of value will be inserted in the input.
+
+ If bits is negative, then the input stream bit buffer is emptied. Then
+ inflatePrime() can be called again to put bits in the buffer. This is used
+ to clear out bits leftover after feeding inflate a block description prior
+ to feeding inflate codes.
+
+ inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+/*
+ This function returns two values, one in the lower 16 bits of the return
+ value, and the other in the remaining upper bits, obtained by shifting the
+ return value down 16 bits. If the upper value is -1 and the lower value is
+ zero, then inflate() is currently decoding information outside of a block.
+ If the upper value is -1 and the lower value is non-zero, then inflate is in
+ the middle of a stored block, with the lower value equaling the number of
+ bytes from the input remaining to copy. If the upper value is not -1, then
+ it is the number of bits back from the current bit position in the input of
+ the code (literal or length/distance pair) currently being processed. In
+ that case the lower value is the number of bytes already emitted for that
+ code.
+
+ A code is being processed if inflate is waiting for more input to complete
+ decoding of the code, or if it has completed decoding but is waiting for
+ more output space to write the literal or match data.
+
+ inflateMark() is used to mark locations in the input data for random
+ access, which may be at bit positions, and to note those cases where the
+ output of a code may span boundaries of random access blocks. The current
+ location in the input stream can be determined from avail_in and data_type
+ as noted in the description for the Z_BLOCK flush parameter for inflate.
+
+ inflateMark returns the value noted above, or -65536 if the provided
+ source stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ inflateGetHeader() requests that gzip header information be stored in the
+ provided gz_header structure. inflateGetHeader() may be called after
+ inflateInit2() or inflateReset(), and before the first call of inflate().
+ As inflate() processes the gzip stream, head->done is zero until the header
+ is completed, at which time head->done is set to one. If a zlib stream is
+ being decoded, then head->done is set to -1 to indicate that there will be
+ no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be
+ used to force inflate() to return immediately after header processing is
+ complete and before any actual data is decompressed.
+
+ The text, time, xflags, and os fields are filled in with the gzip header
+ contents. hcrc is set to true if there is a header CRC. (The header CRC
+ was valid if done is set to one.) If extra is not Z_NULL, then extra_max
+ contains the maximum number of bytes to write to extra. Once done is true,
+ extra_len contains the actual extra field length, and extra contains the
+ extra field, or that field truncated if extra_max is less than extra_len.
+ If name is not Z_NULL, then up to name_max characters are written there,
+ terminated with a zero unless the length is greater than name_max. If
+ comment is not Z_NULL, then up to comm_max characters are written there,
+ terminated with a zero unless the length is greater than comm_max. When any
+ of extra, name, or comment are not Z_NULL and the respective field is not
+ present in the header, then that field is set to Z_NULL to signal its
+ absence. This allows the use of deflateSetHeader() with the returned
+ structure to duplicate the header. However if those fields are set to
+ allocated memory, then the application will need to save those pointers
+ elsewhere so that they can be eventually freed.
+
+ If inflateGetHeader is not used, then the header information is simply
+ discarded. The header is always checked for validity, including the header
+ CRC if present. inflateReset() will reset the process to discard the header
+ information. The application would need to call inflateGetHeader() again to
+ retrieve the header from the next gzip stream.
+
+ inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window));
+
+ Initialize the internal stream state for decompression using inflateBack()
+ calls. The fields zalloc, zfree and opaque in strm must be initialized
+ before the call. If zalloc and zfree are Z_NULL, then the default library-
+ derived memory allocation routines are used. windowBits is the base two
+ logarithm of the window size, in the range 8..15. window is a caller
+ supplied buffer of that size. Except for special applications where it is
+ assured that deflate was used with small window sizes, windowBits must be 15
+ and a 32K byte window must be supplied to be able to decompress general
+ deflate streams.
+
+ See inflateBack() for the usage of these routines.
+
+ inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
+ the parameters are invalid, Z_MEM_ERROR if the internal state could not be
+ allocated, or Z_VERSION_ERROR if the version of the library does not match
+ the version of the header file.
+*/
+
+typedef unsigned (*in_func) OF((void FAR *,
+ z_const unsigned char FAR * FAR *));
+typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
+
+ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
+ in_func in, void FAR *in_desc,
+ out_func out, void FAR *out_desc));
+/*
+ inflateBack() does a raw inflate with a single call using a call-back
+ interface for input and output. This is potentially more efficient than
+ inflate() for file i/o applications, in that it avoids copying between the
+ output and the sliding window by simply making the window itself the output
+ buffer. inflate() can be faster on modern CPUs when used with large
+ buffers. inflateBack() trusts the application to not change the output
+ buffer passed by the output function, at least until inflateBack() returns.
+
+ inflateBackInit() must be called first to allocate the internal state
+ and to initialize the state with the user-provided window buffer.
+ inflateBack() may then be used multiple times to inflate a complete, raw
+ deflate stream with each call. inflateBackEnd() is then called to free the
+ allocated state.
+
+ A raw deflate stream is one with no zlib or gzip header or trailer.
+ This routine would normally be used in a utility that reads zip or gzip
+ files and writes out uncompressed files. The utility would decode the
+ header and process the trailer on its own, hence this routine expects only
+ the raw deflate stream to decompress. This is different from the default
+ behavior of inflate(), which expects a zlib header and trailer around the
+ deflate stream.
+
+ inflateBack() uses two subroutines supplied by the caller that are then
+ called by inflateBack() for input and output. inflateBack() calls those
+ routines until it reads a complete deflate stream and writes out all of the
+ uncompressed data, or until it encounters an error. The function's
+ parameters and return types are defined above in the in_func and out_func
+ typedefs. inflateBack() will call in(in_desc, &buf) which should return the
+ number of bytes of provided input, and a pointer to that input in buf. If
+ there is no input available, in() must return zero -- buf is ignored in that
+ case -- and inflateBack() will return a buffer error. inflateBack() will
+ call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
+ out() should return zero on success, or non-zero on failure. If out()
+ returns non-zero, inflateBack() will return with an error. Neither in() nor
+ out() are permitted to change the contents of the window provided to
+ inflateBackInit(), which is also the buffer that out() uses to write from.
+ The length written by out() will be at most the window size. Any non-zero
+ amount of input may be provided by in().
+
+ For convenience, inflateBack() can be provided input on the first call by
+ setting strm->next_in and strm->avail_in. If that input is exhausted, then
+ in() will be called. Therefore strm->next_in must be initialized before
+ calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
+ immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
+ must also be initialized, and then if strm->avail_in is not zero, input will
+ initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+
+ The in_desc and out_desc parameters of inflateBack() is passed as the
+ first parameter of in() and out() respectively when they are called. These
+ descriptors can be optionally used to pass any information that the caller-
+ supplied in() and out() functions need to do their job.
+
+ On return, inflateBack() will set strm->next_in and strm->avail_in to
+ pass back any unused input that was provided by the last in() call. The
+ return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
+ if in() or out() returned an error, Z_DATA_ERROR if there was a format error
+ in the deflate stream (in which case strm->msg is set to indicate the nature
+ of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
+ In the case of Z_BUF_ERROR, an input or output error can be distinguished
+ using strm->next_in which will be Z_NULL only if in() returned an error. If
+ strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
+ non-zero. (in() will always be called before out(), so strm->next_in is
+ assured to be defined if out() returns non-zero.) Note that inflateBack()
+ cannot return Z_OK.
+*/
+
+ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
+/*
+ All memory allocated by inflateBackInit() is freed.
+
+ inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
+ state was inconsistent.
+*/
+
+ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
+/* Return flags indicating compile-time options.
+
+ Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
+ 1.0: size of uInt
+ 3.2: size of uLong
+ 5.4: size of voidpf (pointer)
+ 7.6: size of z_off_t
+
+ Compiler, assembler, and debug options:
+ 8: ZLIB_DEBUG
+ 9: ASMV or ASMINF -- use ASM code
+ 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
+ 11: 0 (reserved)
+
+ One-time table building (smaller code, but not thread-safe if true):
+ 12: BUILDFIXED -- build static block decoding tables when needed
+ 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
+ 14,15: 0 (reserved)
+
+ Library content (indicates missing functionality):
+ 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
+ deflate code when not needed)
+ 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
+ and decode gzip streams (to avoid linking crc code)
+ 18-19: 0 (reserved)
+
+ Operation variations (changes in library functionality):
+ 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
+ 21: FASTEST -- deflate algorithm with only one, lowest compression level
+ 22,23: 0 (reserved)
+
+ The sprintf variant used by gzprintf (zero is best):
+ 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
+ 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
+ 26: 0 = returns value, 1 = void -- 1 means inferred string length returned
+
+ Remainder:
+ 27-31: 0 (reserved)
+ */
+
+#ifndef Z_SOLO
+
+ /* utility functions */
+
+/*
+ The following utility functions are implemented on top of the basic
+ stream-oriented functions. To simplify the interface, some default options
+ are assumed (compression level and memory usage, standard memory allocation
+ functions). The source code of these utility functions can be modified if
+ you need special options.
+*/
+
+ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Compresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed data. compress() is equivalent to compress2() with a level
+ parameter of Z_DEFAULT_COMPRESSION.
+
+ compress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer.
+*/
+
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen,
+ int level));
+/*
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed data.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+
+ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
+/*
+ compressBound() returns an upper bound on the compressed size after
+ compress() or compress2() on sourceLen bytes. It would be used before a
+ compress() or compress2() call to allocate the destination buffer.
+*/
+
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Decompresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be large enough to hold the entire
+ uncompressed data. (The size of the uncompressed data must have been saved
+ previously by the compressor and transmitted to the decompressor by some
+ mechanism outside the scope of this compression library.) Upon exit, destLen
+ is the actual size of the uncompressed data.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In
+ the case where there is not enough room, uncompress() will fill the output
+ buffer with the uncompressed data up to that point.
+*/
+
+ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong *sourceLen));
+/*
+ Same as uncompress, except that sourceLen is a pointer, where the
+ length of the source is *sourceLen. On return, *sourceLen is the number of
+ source bytes consumed.
+*/
+
+ /* gzip file access functions */
+
+/*
+ This library supports reading and writing files in gzip (.gz) format with
+ an interface similar to that of stdio, using the functions that start with
+ "gz". The gzip format is different from the zlib format. gzip is a gzip
+ wrapper, documented in RFC 1952, wrapped around a deflate stream.
+*/
+
+typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */
+
+/*
+ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
+
+ Opens a gzip (.gz) file for reading or writing. The mode parameter is as
+ in fopen ("rb" or "wb") but can also include a compression level ("wb9") or
+ a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only
+ compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F'
+ for fixed code compression as in "wb9F". (See the description of
+ deflateInit2 for more information about the strategy parameter.) 'T' will
+ request transparent writing or appending with no compression and not using
+ the gzip format.
+
+ "a" can be used instead of "w" to request that the gzip stream that will
+ be written be appended to the file. "+" will result in an error, since
+ reading and writing to the same gzip file is not supported. The addition of
+ "x" when writing will create the file exclusively, which fails if the file
+ already exists. On systems that support it, the addition of "e" when
+ reading or writing will set the flag to close the file on an execve() call.
+
+ These functions, as well as gzip, will read and decode a sequence of gzip
+ streams in a file. The append function of gzopen() can be used to create
+ such a file. (Also see gzflush() for another way to do this.) When
+ appending, gzopen does not test whether the file begins with a gzip stream,
+ nor does it look for the end of the gzip streams to begin appending. gzopen
+ will simply append a gzip stream to the existing file.
+
+ gzopen can be used to read a file which is not in gzip format; in this
+ case gzread will directly read from the file without decompression. When
+ reading, this will be detected automatically by looking for the magic two-
+ byte gzip header.
+
+ gzopen returns NULL if the file could not be opened, if there was
+ insufficient memory to allocate the gzFile state, or if an invalid mode was
+ specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
+ errno can be checked to determine if the reason gzopen failed was that the
+ file could not be opened.
+*/
+
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
+/*
+ gzdopen associates a gzFile with the file descriptor fd. File descriptors
+ are obtained from calls like open, dup, creat, pipe or fileno (if the file
+ has been previously opened with fopen). The mode parameter is as in gzopen.
+
+ The next call of gzclose on the returned gzFile will also close the file
+ descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
+ fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
+ mode);. The duplicated descriptor should be saved to avoid a leak, since
+ gzdopen does not close fd if it fails. If you are using fileno() to get the
+ file descriptor from a FILE *, then you will have to use dup() to avoid
+ double-close()ing the file descriptor. Both gzclose() and fclose() will
+ close the associated file descriptor, so they need to have different file
+ descriptors.
+
+ gzdopen returns NULL if there was insufficient memory to allocate the
+ gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
+ provided, or '+' was provided), or if fd is -1. The file descriptor is not
+ used until the next gz* read, write, seek, or close operation, so gzdopen
+ will not detect if fd is invalid (unless fd is -1).
+*/
+
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
+/*
+ Set the internal buffer size used by this library's functions. The
+ default buffer size is 8192 bytes. This function must be called after
+ gzopen() or gzdopen(), and before any other calls that read or write the
+ file. The buffer memory allocation is always deferred to the first read or
+ write. Three times that size in buffer space is allocated. A larger buffer
+ size of, for example, 64K or 128K bytes will noticeably increase the speed
+ of decompression (reading).
+
+ The new buffer size also affects the maximum length for gzprintf().
+
+ gzbuffer() returns 0 on success, or -1 on failure, such as being called
+ too late.
+*/
+
+ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
+/*
+ Dynamically update the compression level or strategy. See the description
+ of deflateInit2 for the meaning of these parameters. Previously provided
+ data is flushed before the parameter change.
+
+ gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
+ opened for writing, Z_ERRNO if there is an error writing the flushed data,
+ or Z_MEM_ERROR if there is a memory allocation error.
+*/
+
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
+/*
+ Reads the given number of uncompressed bytes from the compressed file. If
+ the input file is not in gzip format, gzread copies the given number of
+ bytes into the buffer directly from the file.
+
+ After reaching the end of a gzip stream in the input, gzread will continue
+ to read, looking for another gzip stream. Any number of gzip streams may be
+ concatenated in the input file, and will all be decompressed by gzread().
+ If something other than a gzip stream is encountered after a gzip stream,
+ that remaining trailing garbage is ignored (and no error is returned).
+
+ gzread can be used to read a gzip file that is being concurrently written.
+ Upon reaching the end of the input, gzread will return with the available
+ data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then
+ gzclearerr can be used to clear the end of file indicator in order to permit
+ gzread to be tried again. Z_OK indicates that a gzip stream was completed
+ on the last gzread. Z_BUF_ERROR indicates that the input file ended in the
+ middle of a gzip stream. Note that gzread does not return -1 in the event
+ of an incomplete gzip stream. This error is deferred until gzclose(), which
+ will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip
+ stream. Alternatively, gzerror can be used before gzclose to detect this
+ case.
+
+ gzread returns the number of uncompressed bytes actually read, less than
+ len for end of file, or -1 for error. If len is too large to fit in an int,
+ then nothing is read, -1 is returned, and the error state is set to
+ Z_STREAM_ERROR.
+*/
+
+ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
+ gzFile file));
+/*
+ Read up to nitems items of size size from file to buf, otherwise operating
+ as gzread() does. This duplicates the interface of stdio's fread(), with
+ size_t request and return types. If the library defines size_t, then
+ z_size_t is identical to size_t. If not, then z_size_t is an unsigned
+ integer type that can contain a pointer.
+
+ gzfread() returns the number of full items read of size size, or zero if
+ the end of the file was reached and a full item could not be read, or if
+ there was an error. gzerror() must be consulted if zero is returned in
+ order to determine if there was an error. If the multiplication of size and
+ nitems overflows, i.e. the product does not fit in a z_size_t, then nothing
+ is read, zero is returned, and the error state is set to Z_STREAM_ERROR.
+
+ In the event that the end of file is reached and only a partial item is
+ available at the end, i.e. the remaining uncompressed data length is not a
+ multiple of size, then the final partial item is nevetheless read into buf
+ and the end-of-file flag is set. The length of the partial item read is not
+ provided, but could be inferred from the result of gztell(). This behavior
+ is the same as the behavior of fread() implementations in common libraries,
+ but it prevents the direct use of gzfread() to read a concurrently written
+ file, reseting and retrying on end-of-file, when size is not 1.
+*/
+
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+ voidpc buf, unsigned len));
+/*
+ Writes the given number of uncompressed bytes into the compressed file.
+ gzwrite returns the number of uncompressed bytes written or 0 in case of
+ error.
+*/
+
+ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
+ z_size_t nitems, gzFile file));
+/*
+ gzfwrite() writes nitems items of size size from buf to file, duplicating
+ the interface of stdio's fwrite(), with size_t request and return types. If
+ the library defines size_t, then z_size_t is identical to size_t. If not,
+ then z_size_t is an unsigned integer type that can contain a pointer.
+
+ gzfwrite() returns the number of full items written of size size, or zero
+ if there was an error. If the multiplication of size and nitems overflows,
+ i.e. the product does not fit in a z_size_t, then nothing is written, zero
+ is returned, and the error state is set to Z_STREAM_ERROR.
+*/
+
+ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
+/*
+ Converts, formats, and writes the arguments to the compressed file under
+ control of the format string, as in fprintf. gzprintf returns the number of
+ uncompressed bytes actually written, or a negative zlib error code in case
+ of error. The number of uncompressed bytes written is limited to 8191, or
+ one less than the buffer size given to gzbuffer(). The caller should assure
+ that this limit is not exceeded. If it is exceeded, then gzprintf() will
+ return an error (0) with nothing written. In this case, there may also be a
+ buffer overflow with unpredictable consequences, which is possible only if
+ zlib was compiled with the insecure functions sprintf() or vsprintf()
+ because the secure snprintf() or vsnprintf() functions were not available.
+ This can be determined using zlibCompileFlags().
+*/
+
+ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
+/*
+ Writes the given null-terminated string to the compressed file, excluding
+ the terminating null character.
+
+ gzputs returns the number of characters written, or -1 in case of error.
+*/
+
+ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
+/*
+ Reads bytes from the compressed file until len-1 characters are read, or a
+ newline character is read and transferred to buf, or an end-of-file
+ condition is encountered. If any characters are read or if len == 1, the
+ string is terminated with a null character. If no characters are read due
+ to an end-of-file or len < 1, then the buffer is left untouched.
+
+ gzgets returns buf which is a null-terminated string, or it returns NULL
+ for end-of-file or in case of error. If there was an error, the contents at
+ buf are indeterminate.
+*/
+
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
+/*
+ Writes c, converted to an unsigned char, into the compressed file. gzputc
+ returns the value that was written, or -1 in case of error.
+*/
+
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
+/*
+ Reads one byte from the compressed file. gzgetc returns this byte or -1
+ in case of end of file or error. This is implemented as a macro for speed.
+ As such, it does not do all of the checking the other functions do. I.e.
+ it does not check to see if file is NULL, nor whether the structure file
+ points to has been clobbered or not.
+*/
+
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
+/*
+ Push one character back onto the stream to be read as the first character
+ on the next read. At least one character of push-back is allowed.
+ gzungetc() returns the character pushed, or -1 on failure. gzungetc() will
+ fail if c is -1, and may fail if a character has been pushed but not read
+ yet. If gzungetc is used immediately after gzopen or gzdopen, at least the
+ output buffer size of pushed characters is allowed. (See gzbuffer above.)
+ The pushed character will be discarded if the stream is repositioned with
+ gzseek() or gzrewind().
+*/
+
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
+/*
+ Flushes all pending output into the compressed file. The parameter flush
+ is as in the deflate() function. The return value is the zlib error number
+ (see function gzerror below). gzflush is only permitted when writing.
+
+ If the flush parameter is Z_FINISH, the remaining data is written and the
+ gzip stream is completed in the output. If gzwrite() is called again, a new
+ gzip stream will be started in the output. gzread() is able to read such
+ concatenated gzip streams.
+
+ gzflush should be called only when strictly necessary because it will
+ degrade compression if called too often.
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
+ z_off_t offset, int whence));
+
+ Sets the starting position for the next gzread or gzwrite on the given
+ compressed file. The offset represents a number of bytes in the
+ uncompressed data stream. The whence parameter is defined as in lseek(2);
+ the value SEEK_END is not supported.
+
+ If the file is opened for reading, this function is emulated but can be
+ extremely slow. If the file is opened for writing, only forward seeks are
+ supported; gzseek then compresses a sequence of zeroes up to the new
+ starting position.
+
+ gzseek returns the resulting offset location as measured in bytes from
+ the beginning of the uncompressed stream, or -1 in case of error, in
+ particular if the file is opened for writing and the new starting position
+ would be before the current position.
+*/
+
+ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
+/*
+ Rewinds the given file. This function is supported only for reading.
+
+ gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
+
+ Returns the starting position for the next gzread or gzwrite on the given
+ compressed file. This position represents a number of bytes in the
+ uncompressed data stream, and is zero when starting, even if appending or
+ reading a gzip stream from the middle of a file using gzdopen().
+
+ gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
+
+ Returns the current offset in the file being read or written. This offset
+ includes the count of bytes that precede the gzip stream, for example when
+ appending or when using gzdopen() for reading. When reading, the offset
+ does not include as yet unused buffered input. This information can be used
+ for a progress indicator. On error, gzoffset() returns -1.
+*/
+
+ZEXTERN int ZEXPORT gzeof OF((gzFile file));
+/*
+ Returns true (1) if the end-of-file indicator has been set while reading,
+ false (0) otherwise. Note that the end-of-file indicator is set only if the
+ read tried to go past the end of the input, but came up short. Therefore,
+ just like feof(), gzeof() may return false even if there is no more data to
+ read, in the event that the last read request was for the exact number of
+ bytes remaining in the input file. This will happen if the input file size
+ is an exact multiple of the buffer size.
+
+ If gzeof() returns true, then the read functions will return no more data,
+ unless the end-of-file indicator is reset by gzclearerr() and the input file
+ has grown since the previous end of file was detected.
+*/
+
+ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
+/*
+ Returns true (1) if file is being copied directly while reading, or false
+ (0) if file is a gzip stream being decompressed.
+
+ If the input file is empty, gzdirect() will return true, since the input
+ does not contain a gzip stream.
+
+ If gzdirect() is used immediately after gzopen() or gzdopen() it will
+ cause buffers to be allocated to allow reading the file to determine if it
+ is a gzip file. Therefore if gzbuffer() is used, it should be called before
+ gzdirect().
+
+ When writing, gzdirect() returns true (1) if transparent writing was
+ requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note:
+ gzdirect() is not needed when writing. Transparent writing must be
+ explicitly requested, so the application already knows the answer. When
+ linking statically, using gzdirect() will include all of the zlib code for
+ gzip file reading and decompression, which may not be desired.)
+*/
+
+ZEXTERN int ZEXPORT gzclose OF((gzFile file));
+/*
+ Flushes all pending output if necessary, closes the compressed file and
+ deallocates the (de)compression state. Note that once file is closed, you
+ cannot call gzerror with file, since its structures have been deallocated.
+ gzclose must not be called more than once on the same file, just as free
+ must not be called more than once on the same allocation.
+
+ gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
+ file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
+ last read ended in the middle of a gzip stream, or Z_OK on success.
+*/
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+/*
+ Same as gzclose(), but gzclose_r() is only for use when reading, and
+ gzclose_w() is only for use when writing or appending. The advantage to
+ using these instead of gzclose() is that they avoid linking in zlib
+ compression or decompression code that is not used when only reading or only
+ writing respectively. If gzclose() is used, then both compression and
+ decompression code will be included the application when linking to a static
+ zlib library.
+*/
+
+ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
+/*
+ Returns the error message for the last error which occurred on the given
+ compressed file. errnum is set to zlib error number. If an error occurred
+ in the file system and not in the compression library, errnum is set to
+ Z_ERRNO and the application may consult errno to get the exact error code.
+
+ The application must not modify the returned string. Future calls to
+ this function may invalidate the previously returned string. If file is
+ closed, then the string previously returned by gzerror will no longer be
+ available.
+
+ gzerror() should be used to distinguish errors from end-of-file for those
+ functions above that do not distinguish those cases in their return values.
+*/
+
+ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
+/*
+ Clears the error and end-of-file flags for file. This is analogous to the
+ clearerr() function in stdio. This is useful for continuing to read a gzip
+ file that is being written concurrently.
+*/
+
+#endif /* !Z_SOLO */
+
+ /* checksum functions */
+
+/*
+ These functions are not related to compression but are exported
+ anyway because they might be useful in applications using the compression
+ library.
+*/
+
+ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+/*
+ Update a running Adler-32 checksum with the bytes buf[0..len-1] and
+ return the updated checksum. If buf is Z_NULL, this function returns the
+ required initial value for the checksum.
+
+ An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
+ much faster.
+
+ Usage example:
+
+ uLong adler = adler32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ adler = adler32(adler, buffer, length);
+ }
+ if (adler != original_adler) error();
+*/
+
+ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf,
+ z_size_t len));
+/*
+ Same as adler32(), but with a size_t length.
+*/
+
+/*
+ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
+ z_off_t len2));
+
+ Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
+ and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
+ each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
+ seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note
+ that the z_off_t type (like off_t) is a signed integer. If len2 is
+ negative, the result has no meaning or utility.
+*/
+
+ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
+/*
+ Update a running CRC-32 with the bytes buf[0..len-1] and return the
+ updated CRC-32. If buf is Z_NULL, this function returns the required
+ initial value for the crc. Pre- and post-conditioning (one's complement) is
+ performed within this function so it shouldn't be done by the application.
+
+ Usage example:
+
+ uLong crc = crc32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ crc = crc32(crc, buffer, length);
+ }
+ if (crc != original_crc) error();
+*/
+
+ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf,
+ z_size_t len));
+/*
+ Same as crc32(), but with a size_t length.
+*/
+
+/*
+ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
+
+ Combine two CRC-32 check values into one. For two sequences of bytes,
+ seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
+ calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
+ check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
+ len2.
+*/
+
+
+ /* various hacks, don't look :) */
+
+/* deflateInit and inflateInit are macros to allow checking the zlib version
+ * and the compiler's view of z_stream:
+ */
+ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
+ int windowBits, int memLevel,
+ int strategy, const char *version,
+ int stream_size));
+ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window,
+ const char *version,
+ int stream_size));
+#ifdef Z_PREFIX_SET
+# define z_deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+ (int)sizeof(z_stream))
+# define z_inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, (int)sizeof(z_stream))
+#else
+# define deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
+# define inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
+# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
+# define inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+ (int)sizeof(z_stream))
+# define inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, (int)sizeof(z_stream))
+#endif
+
+#ifndef Z_SOLO
+
+/* gzgetc() macro and its supporting function and exposed data structure. Note
+ * that the real internal state is much larger than the exposed structure.
+ * This abbreviated structure exposes just enough for the gzgetc() macro. The
+ * user should not mess with these exposed elements, since their names or
+ * behavior could change in the future, perhaps even capriciously. They can
+ * only be used by the gzgetc() macro. You have been warned.
+ */
+struct gzFile_s {
+ unsigned have;
+ unsigned char *next;
+ z_off64_t pos;
+};
+ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
+#ifdef Z_PREFIX_SET
+# undef z_gzgetc
+# define z_gzgetc(g) \
+ ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
+#else
+# define gzgetc(g) \
+ ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
+#endif
+
+/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
+ * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
+ * both are true, the application gets the *64 functions, and the regular
+ * functions are changed to 64 bits) -- in case these are set on systems
+ * without large file support, _LFS64_LARGEFILE must also be true
+ */
+#ifdef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
+# ifdef Z_PREFIX_SET
+# define z_gzopen z_gzopen64
+# define z_gzseek z_gzseek64
+# define z_gztell z_gztell64
+# define z_gzoffset z_gzoffset64
+# define z_adler32_combine z_adler32_combine64
+# define z_crc32_combine z_crc32_combine64
+# else
+# define gzopen gzopen64
+# define gzseek gzseek64
+# define gztell gztell64
+# define gzoffset gzoffset64
+# define adler32_combine adler32_combine64
+# define crc32_combine crc32_combine64
+# endif
+# ifndef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+# endif
+#else
+ ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
+
+#else /* Z_SOLO */
+
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+
+#endif /* !Z_SOLO */
+
+/* undocumented functions */
+ZEXTERN const char * ZEXPORT zError OF((int));
+ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp));
+ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void));
+ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int));
+ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int));
+ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp));
+ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp));
+ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp));
+#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO)
+ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path,
+ const char *mode));
+#endif
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifndef Z_SOLO
+ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file,
+ const char *format,
+ va_list va));
+# endif
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ZLIB_H */
diff --git a/test/monniaux/zlib-1.2.11/zlib_small.txt b/test/monniaux/zlib-1.2.11/zlib_small.txt
new file mode 100644
index 00000000..2c494200
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/zlib_small.txt
@@ -0,0 +1,539 @@
+
+
+
+#ifndef ZLIB_H
+#define ZLIB_H
+
+#include "zconf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ZLIB_VERSION "1.2.11"
+#define ZLIB_VERNUM 0x12b0
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 11
+#define ZLIB_VER_SUBREVISION 0
+
+
+
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void (*free_func) OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+ z_const Bytef *next_in;
+
+ uLong total_in;
+
+ uInt avail_out;
+
+
+ z_const char *msg;
+
+
+ alloc_func zalloc;
+
+ voidpf opaque;
+
+ uLong adler;
+
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+
+
+typedef struct gz_header_s {
+ int text;
+
+ int xflags;
+
+ Bytef *extra;
+
+ uInt extra_max;
+
+ uInt name_max;
+
+ uInt comm_max;
+
+ int done;
+
+} gz_header;
+
+typedef gz_header FAR *gz_headerp;
+
+
+
+
+
+
+
+#define Z_OK 0
+#define Z_STREAM_END 1
+#define Z_NEED_DICT 2
+#define Z_ERRNO (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR (-3)
+#define Z_MEM_ERROR (-4)
+#define Z_BUF_ERROR (-5)
+#define Z_VERSION_ERROR (-6)
+
+
+
+#define Z_NO_COMPRESSION 0
+#define Z_BEST_SPEED 1
+#define Z_BEST_COMPRESSION 9
+#define Z_DEFAULT_COMPRESSION (-1)
+
+
+
+#define Z_BINARY 0
+#define Z_TEXT 1
+#define Z_ASCII Z_TEXT
+
+
+#define Z_DEFLATED 8
+
+
+
+#define zlib_version zlibVersion()
+
+
+
+ZEXTERN const char * ZEXPORT zlibVersion OF((void));
+
+
+
+
+
+
+
+ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
+
+
+
+
+ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
+
+
+
+
+
+
+
+
+ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
+
+
+
+
+ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
+
+
+
+
+
+
+
+
+
+
+ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+
+
+
+ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
+ Bytef *dictionary,
+ uInt *dictLength));
+
+
+
+ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
+ z_streamp source));
+
+
+
+ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
+
+
+
+ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
+ int level,
+ int strategy));
+
+
+
+ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
+ int good_length,
+ int max_lazy,
+ int nice_length,
+ int max_chain));
+
+
+
+ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
+ uLong sourceLen));
+
+
+
+ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm,
+ unsigned *pending,
+ int *bits));
+
+
+
+ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+
+
+
+ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
+ gz_headerp head));
+
+
+
+
+
+
+ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+
+
+
+ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
+ Bytef *dictionary,
+ uInt *dictLength));
+
+
+
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+
+
+
+ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
+ z_streamp source));
+
+
+
+ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
+
+
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+ int windowBits));
+
+
+
+ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+
+
+
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+
+
+
+ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
+ gz_headerp head));
+
+
+
+
+
+
+typedef unsigned (*in_func) OF((void FAR *,
+ z_const unsigned char FAR * FAR *));
+typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
+
+ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
+ in_func in, void FAR *in_desc,
+ out_func out, void FAR *out_desc));
+
+
+
+ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
+
+
+
+ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
+
+
+
+#ifndef Z_SOLO
+
+
+
+
+ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+
+
+
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen,
+ int level));
+
+
+
+ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
+
+
+
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+
+
+
+ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong *sourceLen));
+
+
+
+
+
+
+typedef struct gzFile_s *gzFile;
+
+
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
+
+
+
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
+
+
+
+ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
+
+
+
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
+
+
+
+ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
+ gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+ voidpc buf, unsigned len));
+
+
+
+ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
+ z_size_t nitems, gzFile file));
+
+
+
+ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
+
+
+
+ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
+
+
+
+ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
+
+
+
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
+
+
+
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
+
+
+
+
+
+
+ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
+
+
+
+
+
+
+
+
+
+ZEXTERN int ZEXPORT gzeof OF((gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzclose OF((gzFile file));
+
+
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+
+
+
+ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
+
+
+
+ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
+
+
+
+#endif
+
+
+
+
+
+ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+
+
+
+ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf,
+ z_size_t len));
+
+
+
+
+
+
+ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
+
+
+
+ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf,
+ z_size_t len));
+
+
+
+
+
+
+
+
+
+ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
+ int windowBits, int memLevel,
+ int strategy, const char *version,
+ int stream_size));
+ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window,
+ const char *version,
+ int stream_size));
+#ifdef Z_PREFIX_SET
+# define z_deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
+# define z_inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+ (int)sizeof(z_stream))
+# define z_inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, (int)sizeof(z_stream))
+#else
+# define deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
+# define inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
+# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
+# define inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+ (int)sizeof(z_stream))
+# define inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, (int)sizeof(z_stream))
+#endif
+
+#ifndef Z_SOLO
+
+
+
+struct gzFile_s {
+ unsigned have;
+ unsigned char *next;
+ z_off64_t pos;
+};
+ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file));
+
+#ifdef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
+# ifdef Z_PREFIX_SET
+# define z_gzopen z_gzopen64
+# define z_gzseek z_gzseek64
+# define z_gztell z_gztell64
+# define z_gzoffset z_gzoffset64
+# define z_adler32_combine z_adler32_combine64
+# define z_crc32_combine z_crc32_combine64
+# else
+# define gzopen gzopen64
+# define gzseek gzseek64
+# define gztell gztell64
+# define gzoffset gzoffset64
+# define adler32_combine adler32_combine64
+# define crc32_combine crc32_combine64
+# endif
+# ifndef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+# endif
+#else
+ ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
+
+#else
+
+
+
+
diff --git a/test/monniaux/zlib-1.2.11/zutil.c b/test/monniaux/zlib-1.2.11/zutil.c
new file mode 100644
index 00000000..a76c6b0c
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/zutil.c
@@ -0,0 +1,325 @@
+/* zutil.c -- target dependent utility functions for the compression library
+ * Copyright (C) 1995-2017 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+#ifndef Z_SOLO
+# include "gzguts.h"
+#endif
+
+z_const char * const z_errmsg[10] = {
+ (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */
+ (z_const char *)"stream end", /* Z_STREAM_END 1 */
+ (z_const char *)"", /* Z_OK 0 */
+ (z_const char *)"file error", /* Z_ERRNO (-1) */
+ (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */
+ (z_const char *)"data error", /* Z_DATA_ERROR (-3) */
+ (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */
+ (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */
+ (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
+ (z_const char *)""
+};
+
+
+const char * ZEXPORT zlibVersion()
+{
+ return ZLIB_VERSION;
+}
+
+uLong ZEXPORT zlibCompileFlags()
+{
+ uLong flags;
+
+ flags = 0;
+ switch ((int)(sizeof(uInt))) {
+ case 2: break;
+ case 4: flags += 1; break;
+ case 8: flags += 2; break;
+ default: flags += 3;
+ }
+ switch ((int)(sizeof(uLong))) {
+ case 2: break;
+ case 4: flags += 1 << 2; break;
+ case 8: flags += 2 << 2; break;
+ default: flags += 3 << 2;
+ }
+ switch ((int)(sizeof(voidpf))) {
+ case 2: break;
+ case 4: flags += 1 << 4; break;
+ case 8: flags += 2 << 4; break;
+ default: flags += 3 << 4;
+ }
+ switch ((int)(sizeof(z_off_t))) {
+ case 2: break;
+ case 4: flags += 1 << 6; break;
+ case 8: flags += 2 << 6; break;
+ default: flags += 3 << 6;
+ }
+#ifdef ZLIB_DEBUG
+ flags += 1 << 8;
+#endif
+#if defined(ASMV) || defined(ASMINF)
+ flags += 1 << 9;
+#endif
+#ifdef ZLIB_WINAPI
+ flags += 1 << 10;
+#endif
+#ifdef BUILDFIXED
+ flags += 1 << 12;
+#endif
+#ifdef DYNAMIC_CRC_TABLE
+ flags += 1 << 13;
+#endif
+#ifdef NO_GZCOMPRESS
+ flags += 1L << 16;
+#endif
+#ifdef NO_GZIP
+ flags += 1L << 17;
+#endif
+#ifdef PKZIP_BUG_WORKAROUND
+ flags += 1L << 20;
+#endif
+#ifdef FASTEST
+ flags += 1L << 21;
+#endif
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifdef NO_vsnprintf
+ flags += 1L << 25;
+# ifdef HAS_vsprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_vsnprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#else
+ flags += 1L << 24;
+# ifdef NO_snprintf
+ flags += 1L << 25;
+# ifdef HAS_sprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_snprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#endif
+ return flags;
+}
+
+#ifdef ZLIB_DEBUG
+#include <stdlib.h>
+# ifndef verbose
+# define verbose 0
+# endif
+int ZLIB_INTERNAL z_verbose = verbose;
+
+void ZLIB_INTERNAL z_error (m)
+ char *m;
+{
+ fprintf(stderr, "%s\n", m);
+ exit(1);
+}
+#endif
+
+/* exported to allow conversion of error code to string for compress() and
+ * uncompress()
+ */
+const char * ZEXPORT zError(err)
+ int err;
+{
+ return ERR_MSG(err);
+}
+
+#if defined(_WIN32_WCE)
+ /* The Microsoft C Run-Time Library for Windows CE doesn't have
+ * errno. We define it as a global variable to simplify porting.
+ * Its value is always 0 and should not be used.
+ */
+ int errno = 0;
+#endif
+
+#ifndef HAVE_MEMCPY
+
+void ZLIB_INTERNAL zmemcpy(dest, source, len)
+ Bytef* dest;
+ const Bytef* source;
+ uInt len;
+{
+ if (len == 0) return;
+ do {
+ *dest++ = *source++; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+
+int ZLIB_INTERNAL zmemcmp(s1, s2, len)
+ const Bytef* s1;
+ const Bytef* s2;
+ uInt len;
+{
+ uInt j;
+
+ for (j = 0; j < len; j++) {
+ if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
+ }
+ return 0;
+}
+
+void ZLIB_INTERNAL zmemzero(dest, len)
+ Bytef* dest;
+ uInt len;
+{
+ if (len == 0) return;
+ do {
+ *dest++ = 0; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+#endif
+
+#ifndef Z_SOLO
+
+#ifdef SYS16BIT
+
+#ifdef __TURBOC__
+/* Turbo C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
+ * and farmalloc(64K) returns a pointer with an offset of 8, so we
+ * must fix the pointer. Warning: the pointer must be put back to its
+ * original form in order to free it, use zcfree().
+ */
+
+#define MAX_PTR 10
+/* 10*64K = 640K */
+
+local int next_ptr = 0;
+
+typedef struct ptr_table_s {
+ voidpf org_ptr;
+ voidpf new_ptr;
+} ptr_table;
+
+local ptr_table table[MAX_PTR];
+/* This table is used to remember the original form of pointers
+ * to large buffers (64K). Such pointers are normalized with a zero offset.
+ * Since MSDOS is not a preemptive multitasking OS, this table is not
+ * protected from concurrent access. This hack doesn't work anyway on
+ * a protected system like OS/2. Use Microsoft C instead.
+ */
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
+{
+ voidpf buf;
+ ulg bsize = (ulg)items*size;
+
+ (void)opaque;
+
+ /* If we allocate less than 65520 bytes, we assume that farmalloc
+ * will return a usable pointer which doesn't have to be normalized.
+ */
+ if (bsize < 65520L) {
+ buf = farmalloc(bsize);
+ if (*(ush*)&buf != 0) return buf;
+ } else {
+ buf = farmalloc(bsize + 16L);
+ }
+ if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
+ table[next_ptr].org_ptr = buf;
+
+ /* Normalize the pointer to seg:0 */
+ *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
+ *(ush*)&buf = 0;
+ table[next_ptr++].new_ptr = buf;
+ return buf;
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ int n;
+
+ (void)opaque;
+
+ if (*(ush*)&ptr != 0) { /* object < 64K */
+ farfree(ptr);
+ return;
+ }
+ /* Find the original pointer */
+ for (n = 0; n < next_ptr; n++) {
+ if (ptr != table[n].new_ptr) continue;
+
+ farfree(table[n].org_ptr);
+ while (++n < next_ptr) {
+ table[n-1] = table[n];
+ }
+ next_ptr--;
+ return;
+ }
+ Assert(0, "zcfree: ptr not found");
+}
+
+#endif /* __TURBOC__ */
+
+
+#ifdef M_I86
+/* Microsoft C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
+# define _halloc halloc
+# define _hfree hfree
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
+{
+ (void)opaque;
+ return _halloc((long)items, size);
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ (void)opaque;
+ _hfree(ptr);
+}
+
+#endif /* M_I86 */
+
+#endif /* SYS16BIT */
+
+
+#ifndef MY_ZCALLOC /* Any system without a special alloc function */
+
+#ifndef STDC
+extern voidp malloc OF((uInt size));
+extern voidp calloc OF((uInt items, uInt size));
+extern void free OF((voidpf ptr));
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
+ voidpf opaque;
+ unsigned items;
+ unsigned size;
+{
+ (void)opaque;
+ return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
+ (voidpf)calloc(items, size);
+}
+
+void ZLIB_INTERNAL zcfree (opaque, ptr)
+ voidpf opaque;
+ voidpf ptr;
+{
+ (void)opaque;
+ free(ptr);
+}
+
+#endif /* MY_ZCALLOC */
+
+#endif /* !Z_SOLO */
diff --git a/test/monniaux/zlib-1.2.11/zutil.h b/test/monniaux/zlib-1.2.11/zutil.h
new file mode 100644
index 00000000..b079ea6a
--- /dev/null
+++ b/test/monniaux/zlib-1.2.11/zutil.h
@@ -0,0 +1,271 @@
+/* zutil.h -- internal interface and configuration of the compression library
+ * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* @(#) $Id$ */
+
+#ifndef ZUTIL_H
+#define ZUTIL_H
+
+#ifdef HAVE_HIDDEN
+# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+# define ZLIB_INTERNAL
+#endif
+
+#include "zlib.h"
+
+#if defined(STDC) && !defined(Z_SOLO)
+# if !(defined(_WIN32_WCE) && defined(_MSC_VER))
+# include <stddef.h>
+# endif
+# include <string.h>
+# include <stdlib.h>
+#endif
+
+#ifdef Z_SOLO
+ typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */
+#endif
+
+#ifndef local
+# define local static
+#endif
+/* since "static" is used to mean two completely different things in C, we
+ define "local" for the non-static meaning of "static", for readability
+ (compile with -Dlocal if your debugger can't find static symbols) */
+
+typedef unsigned char uch;
+typedef uch FAR uchf;
+typedef unsigned short ush;
+typedef ush FAR ushf;
+typedef unsigned long ulg;
+
+extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
+/* (size given to avoid silly warnings with Visual C++) */
+
+#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
+
+#define ERR_RETURN(strm,err) \
+ return (strm->msg = ERR_MSG(err), (err))
+/* To be used only when the state is known to be valid */
+
+ /* common constants */
+
+#ifndef DEF_WBITS
+# define DEF_WBITS MAX_WBITS
+#endif
+/* default windowBits for decompression. MAX_WBITS is for compression only */
+
+#if MAX_MEM_LEVEL >= 8
+# define DEF_MEM_LEVEL 8
+#else
+# define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+/* default memLevel */
+
+#define STORED_BLOCK 0
+#define STATIC_TREES 1
+#define DYN_TREES 2
+/* The three kinds of block type */
+
+#define MIN_MATCH 3
+#define MAX_MATCH 258
+/* The minimum and maximum match lengths */
+
+#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
+
+ /* target dependencies */
+
+#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
+# define OS_CODE 0x00
+# ifndef Z_SOLO
+# if defined(__TURBOC__) || defined(__BORLANDC__)
+# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
+ /* Allow compilation with ANSI keywords only enabled */
+ void _Cdecl farfree( void *block );
+ void *_Cdecl farmalloc( unsigned long nbytes );
+# else
+# include <alloc.h>
+# endif
+# else /* MSC or DJGPP */
+# include <malloc.h>
+# endif
+# endif
+#endif
+
+#ifdef AMIGA
+# define OS_CODE 1
+#endif
+
+#if defined(VAXC) || defined(VMS)
+# define OS_CODE 2
+# define F_OPEN(name, mode) \
+ fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
+#endif
+
+#ifdef __370__
+# if __TARGET_LIB__ < 0x20000000
+# define OS_CODE 4
+# elif __TARGET_LIB__ < 0x40000000
+# define OS_CODE 11
+# else
+# define OS_CODE 8
+# endif
+#endif
+
+#if defined(ATARI) || defined(atarist)
+# define OS_CODE 5
+#endif
+
+#ifdef OS2
+# define OS_CODE 6
+# if defined(M_I86) && !defined(Z_SOLO)
+# include <malloc.h>
+# endif
+#endif
+
+#if defined(MACOS) || defined(TARGET_OS_MAC)
+# define OS_CODE 7
+# ifndef Z_SOLO
+# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
+# include <unix.h> /* for fdopen */
+# else
+# ifndef fdopen
+# define fdopen(fd,mode) NULL /* No fdopen() */
+# endif
+# endif
+# endif
+#endif
+
+#ifdef __acorn
+# define OS_CODE 13
+#endif
+
+#if defined(WIN32) && !defined(__CYGWIN__)
+# define OS_CODE 10
+#endif
+
+#ifdef _BEOS_
+# define OS_CODE 16
+#endif
+
+#ifdef __TOS_OS400__
+# define OS_CODE 18
+#endif
+
+#ifdef __APPLE__
+# define OS_CODE 19
+#endif
+
+#if defined(_BEOS_) || defined(RISCOS)
+# define fdopen(fd,mode) NULL /* No fdopen() */
+#endif
+
+#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
+# if defined(_WIN32_WCE)
+# define fdopen(fd,mode) NULL /* No fdopen() */
+# ifndef _PTRDIFF_T_DEFINED
+ typedef int ptrdiff_t;
+# define _PTRDIFF_T_DEFINED
+# endif
+# else
+# define fdopen(fd,type) _fdopen(fd,type)
+# endif
+#endif
+
+#if defined(__BORLANDC__) && !defined(MSDOS)
+ #pragma warn -8004
+ #pragma warn -8008
+ #pragma warn -8066
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_WIN32) && \
+ (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#endif
+
+ /* common defaults */
+
+#ifndef OS_CODE
+# define OS_CODE 3 /* assume Unix */
+#endif
+
+#ifndef F_OPEN
+# define F_OPEN(name, mode) fopen((name), (mode))
+#endif
+
+ /* functions */
+
+#if defined(pyr) || defined(Z_SOLO)
+# define NO_MEMCPY
+#endif
+#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
+ /* Use our own functions for small and medium model with MSC <= 5.0.
+ * You may have to use the same strategy for Borland C (untested).
+ * The __SC__ check is for Symantec.
+ */
+# define NO_MEMCPY
+#endif
+#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
+# define HAVE_MEMCPY
+#endif
+#ifdef HAVE_MEMCPY
+# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
+# define zmemcpy _fmemcpy
+# define zmemcmp _fmemcmp
+# define zmemzero(dest, len) _fmemset(dest, 0, len)
+# else
+# define zmemcpy memcpy
+# define zmemcmp memcmp
+# define zmemzero(dest, len) memset(dest, 0, len)
+# endif
+#else
+ void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
+ int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
+ void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
+#endif
+
+/* Diagnostic functions */
+#ifdef ZLIB_DEBUG
+# include <stdio.h>
+ extern int ZLIB_INTERNAL z_verbose;
+ extern void ZLIB_INTERNAL z_error OF((char *m));
+# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
+# define Trace(x) {if (z_verbose>=0) fprintf x ;}
+# define Tracev(x) {if (z_verbose>0) fprintf x ;}
+# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
+# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
+# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
+#else
+# define Assert(cond,msg)
+# define Trace(x)
+# define Tracev(x)
+# define Tracevv(x)
+# define Tracec(c,x)
+# define Tracecv(c,x)
+#endif
+
+#ifndef Z_SOLO
+ voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
+ unsigned size));
+ void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
+#endif
+
+#define ZALLOC(strm, items, size) \
+ (*((strm)->zalloc))((strm)->opaque, (items), (size))
+#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
+#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
+
+/* Reverse the bytes in a 32-bit value */
+#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+ (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+
+#endif /* ZUTIL_H */
diff --git a/test/nardino/scheduling/entry_regs.c b/test/nardino/scheduling/entry_regs.c
new file mode 100644
index 00000000..9e6adacb
--- /dev/null
+++ b/test/nardino/scheduling/entry_regs.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int f(int n) {
+ if (n > 0)
+ return 42;
+ else
+ return n;
+}
+
+
+int main(int argc, char *argv[]) {
+ int a=1;
+ float b=2.;
+ int c = f(a);
+ a = 3;
+ int d = f(a);
+ printf("%e, %d, %d, %d", b, a, c, d);
+ return 0;
+}
diff --git a/test/nardino/scheduling/spille_backw.c b/test/nardino/scheduling/spille_backw.c
new file mode 100644
index 00000000..1c36ee86
--- /dev/null
+++ b/test/nardino/scheduling/spille_backw.c
@@ -0,0 +1,114 @@
+int f(int k) {
+ int a1 = k;
+ int b1 = 2*a1;
+ int c = a1;
+ int a2 = k+1;
+ int b2 = 2*a2;
+ c += a2;
+ int a3 = k+2;
+ int b3 = 2*a3;
+ c += a3;
+ int a4 = k+3;
+ int b4 = 2*a4;
+ c += a4;
+ int a5 = k+4;
+ int b5 = 2*a5;
+ c += a5;
+ int a6 = k+5;
+ int b6 = 2*a6;
+ c += a6;
+ int a7 = k+6;
+ int b7 = 2*a7;
+ c += a7;
+ int a8 = k+7;
+ int b8 = 2*a8;
+ c += a8;
+ int a9 = k+8;
+ int b9 = 2*a9;
+ c += a9;
+ int a10 = k+9;
+ int b10 = 2*a10;
+ c += a10;
+ int a11 = k+10;
+ int b11 = 2*a11;
+ c += a11;
+ int a12 = k+11;
+ int b12 = 2*a12;
+ c += a12;
+ int a13 = k+12;
+ int b13 = 2*a13;
+ c += a13;
+ int a14 = k+13;
+ int b14 = 2*a14;
+ c += a14;
+ int a15 = k+14;
+ int b15 = 2*a15;
+ c += a15;
+ int a16 = k+15;
+ int b16 = 2*a16;
+ c += a16;
+ int a17 = k+16;
+ int b17 = 2*a17;
+ c += a17;
+ int a18 = k+17;
+ int b18 = 2*a18;
+ c += a18;
+ int a19 = k+18;
+ int b19 = 2*a19;
+ c += a19;
+ int a20 = k+19;
+ int b20 = 2*a20;
+ c += a20;
+ int a21 = k+20;
+ int b21 = 2*a21;
+ c += a21;
+ int a22 = k+21;
+ int b22 = 2*a22;
+ c += a22;
+ int a23 = k+22;
+ int b23 = 2*a23;
+ c += a23;
+ int a24 = k+23;
+ int b24 = 2*a24;
+ c += a24;
+ int a25 = k+24;
+ int b25 = 2*a25;
+ c += a25;
+ int a26 = k+25;
+ int b26 = 2*a26;
+ c += a26;
+ return
+ b13+
+ b12+
+ b11+
+ b10+
+ b9+
+ b8+
+ b7+
+ b6+
+ b5+
+ b4+
+ b3+
+ b2+
+ b1+
+ b14+
+ b15+
+ b16+
+ b17+
+ b18+
+ b19+
+ b20+
+ b21+
+ b22+
+ b23+
+ b23+
+ b24+
+ b25+
+ b26+
+ c;
+}
+
+int main(int argc, char *argv[]) {
+ f(3);
+ return 0;
+}
diff --git a/test/nardino/scheduling/spille_forw.c b/test/nardino/scheduling/spille_forw.c
new file mode 100644
index 00000000..db88588b
--- /dev/null
+++ b/test/nardino/scheduling/spille_forw.c
@@ -0,0 +1,166 @@
+#include <stdio.h>
+
+int f(int n, float * arr) {
+ float a1 = (float) n;
+ float b1 = 2.*a1;
+ float c = a1;
+ float a2 = (float) n+1;
+ float b2 = 2.*a2;
+ c += a2;
+ float a3 = (float) n+2;
+ float b3 = 2.*a3;
+ c += a3;
+ float a4 = (float) n+3;
+ float b4 = 2.*a4;
+ c += a4;
+ float a5 = (float) n+4;
+ float b5 = 2.*a5;
+ c += a5;
+ float a6 = (float) n+5;
+ float b6 = 2.*a6;
+ c += a6;
+ float a7 = (float) n+6;
+ float b7 = 2.*a7;
+ c += a7;
+ float a8 = (float) n+7;
+ float b8 = 2.*a8;
+ c += a8;
+ float a9 = (float) n+8;
+ float b9 = 2.*a9;
+ c += a9;
+ float a10 = (float) n+9;
+ float b10 = 2.*a10;
+ c += a10;
+ float a11 = (float) n+10;
+ float b11 = 2.*a11;
+ c += a11;
+ float a12 = (float) n+11;
+ float b12 = 2.*a12;
+ c += a12;
+ float a13 = (float) n+12;
+ float b13 = 2.*a13;
+ c += a13;
+ float a14 = (float) n+13;
+ float b14 = 2.*a14;
+ c += a14;
+ float a15 = (float) n+14;
+ float b15 = 2.*a15;
+ c += a15;
+ float a16 = (float) n+15;
+ float b16 = 2.*a16;
+ c += a16;
+ float a17 = (float) n+16;
+ float b17 = 2.*a17;
+ c += a17;
+ float a18 = (float) n+17;
+ float b18 = 2.*a18;
+ c += a18;
+ float a19 = (float) n+18;
+ float b19 = 2.*a19;
+ c += a19;
+ float a20 = (float) n+19;
+ float b20 = 2.*a20;
+ c += a20;
+ float a21 = (float) n+20;
+ float b21 = 2.*a21;
+ c += a21;
+ float a22 = (float) n+21;
+ float b22 = 2.*a22;
+ c += a22;
+ float a23 = (float) n+22;
+ float b23 = 2.*a23;
+ c += a23;
+ float a24 = (float) n+23;
+ float b24 = 2.*a24;
+ c += a24;
+ float a25 = (float) n+24;
+ float b25 = 2.*a25;
+ c += a25;
+ float a26 = (float) n+25;
+ float b26 = 2.*a26;
+ c += a26;
+ float a27 = (float) n+26;
+ float b27 = 2.*a27;
+ c += a27;
+ float a28 = (float) n+27;
+ float b28 = 2.*a28;
+ c += a28;
+ float a29 = (float) n+28;
+ float b29 = 2.*a29;
+ c += a29;
+ float a30 = (float) n+29;
+ float b30 = 2.*a30;
+ c += a30;
+ /* arr[0] = a1; */
+ /* arr[1] = a2; */
+ /* arr[2] = a3; */
+ /* arr[3] = a4; */
+ /* arr[4] = a5; */
+ /* arr[5] = a6; */
+ /* arr[6] = a7; */
+ /* arr[7] = a8; */
+ /* arr[8] = a9; */
+ /* arr[9] = a10; */
+ /* arr[10] = a11; */
+ /* arr[11] = a12; */
+ /* arr[12] = a13; */
+ /* arr[13] = a14; */
+ /* arr[14] = a15; */
+ /* arr[15] = a16; */
+ /* arr[16] = a17; */
+ /* arr[17] = a18; */
+ /* arr[18] = a19; */
+ /* arr[19] = a20; */
+ /* arr[20] = a21; */
+ /* arr[21] = a22; */
+ /* arr[22] = a23; */
+ /* arr[23] = a24; */
+ /* arr[24] = a25; */
+ /* arr[25] = a26; */
+ /* arr[26] = a27; */
+ /* arr[27] = a28; */
+ /* arr[28] = a29; */
+ /* arr[29] = a30; */
+ return c +
+ b1+
+ b2+
+ b3+
+ b4+
+ b5+
+ b6+
+ b7+
+ b8+
+ b9+
+ b10+
+ b11+
+ b12+
+ b13+
+ b14+
+ b15+
+ b16+
+ b17+
+ b18+
+ b19+
+ b20+
+ b21+
+ b22+
+ b23+
+ b24+
+ b25+
+ b26+
+ b27+
+ b28+
+ b29+
+ b30;
+}
+
+
+
+
+
+
+int main(int argc, char *argv[]) {
+ float arr[30];
+ f(5, arr);
+ return 0;
+}
diff --git a/test/regression/Makefile b/test/regression/Makefile
index 33a9f993..b3a2eee8 100644
--- a/test/regression/Makefile
+++ b/test/regression/Makefile
@@ -1,6 +1,8 @@
include ../../Makefile.config
CCOMP=../../ccomp
+# TODO - temporary
+# CCOMPOPTS:=$(CCOMPOPTS) -fall-loads-nontrap -fduplicate 2 -fprepass
CCOMPFLAGS=$(CCOMPOPTS) -stdlib ../../runtime \
-dparse -dc -dclight -dasm -fall \
-DARCH_$(ARCH) -DMODEL_$(MODEL)
@@ -10,25 +12,33 @@ LIBS=$(LIBMATH)
# Can run, both in compiled mode and in interpreter mode,
# and have reference output in Results
-TESTS=int32 int64 floats floats-basics floats-lit \
+TESTS?=int32 int64 floats floats-basics floats-lit \
expr1 expr6 funptr2 initializers initializers2 initializers3 \
volatile1 volatile2 volatile3 volatile4 \
funct3 expr5 struct7 struct8 struct11 struct12 casts1 casts2 char1 \
sizeof1 sizeof2 binops bool for1 for2 switch switch2 compound \
decl1 bitfields9 ptrs3 \
- parsing krfun ifconv
+ parsing krfun ifconv many_parameters union_passing
# Can run, but only in compiled mode, and have reference output in Results
-TESTS_COMP=attribs1 bitfields1 bitfields2 bitfields3 bitfields4 \
+TESTS_COMP?=attribs1 bitfields1 bitfields2 bitfields3 bitfields4 \
bitfields5 bitfields6 bitfields7 bitfields8 bitfields_uint_t bitfields10 \
builtins-common builtins-$(ARCH) packedstruct1 packedstruct2 alignas \
varargs1 varargs2 varargs3 sections alias aligned
+ifeq ($(ARCH),kvx)
+ TESTS_COMP:=$(filter-out packedstruct1,$(TESTS_COMP))
+ TESTS_COMP:=$(filter-out packedstruct2,$(TESTS_COMP))
+endif
+
# Can run, both in compiled mode and in interpreter mode,
# but produce processor-dependent results, so no reference output in Results
TESTS_DIFF=NaNs
+# FIXME ifeq ($(ARCH),kvx)
+ TESTS_DIFF:=$(filter-out NaNs,$(TESTS_DIFF))
+# endif
# Other tests: should compile to .s without errors (but expect warnings)
diff --git a/test/regression/Results/builtins-common-kvx b/test/regression/Results/builtins-common-kvx
new file mode 100644
index 00000000..fad75e7c
--- /dev/null
+++ b/test/regression/Results/builtins-common-kvx
@@ -0,0 +1,391 @@
+bswap(12345678) = 78563412
+bswap16(1234) = 3412
+bswap64(123456789abcdef0) = f0debc9a78563412
+clz(ffffffff) = 0
+clz(80000000) = 0
+clz(7fffffff) = 1
+clz(40000000) = 1
+clz(3fffffff) = 2
+clz(20000000) = 2
+clz(1fffffff) = 3
+clz(10000000) = 3
+clz(0fffffff) = 4
+clz(08000000) = 4
+clz(07ffffff) = 5
+clz(04000000) = 5
+clz(03ffffff) = 6
+clz(02000000) = 6
+clz(01ffffff) = 7
+clz(01000000) = 7
+clz(00ffffff) = 8
+clz(00800000) = 8
+clz(007fffff) = 9
+clz(00400000) = 9
+clz(003fffff) = 10
+clz(00200000) = 10
+clz(001fffff) = 11
+clz(00100000) = 11
+clz(000fffff) = 12
+clz(00080000) = 12
+clz(0007ffff) = 13
+clz(00040000) = 13
+clz(0003ffff) = 14
+clz(00020000) = 14
+clz(0001ffff) = 15
+clz(00010000) = 15
+clz(0000ffff) = 16
+clz(00008000) = 16
+clz(00007fff) = 17
+clz(00004000) = 17
+clz(00003fff) = 18
+clz(00002000) = 18
+clz(00001fff) = 19
+clz(00001000) = 19
+clz(00000fff) = 20
+clz(00000800) = 20
+clz(000007ff) = 21
+clz(00000400) = 21
+clz(000003ff) = 22
+clz(00000200) = 22
+clz(000001ff) = 23
+clz(00000100) = 23
+clz(000000ff) = 24
+clz(00000080) = 24
+clz(0000007f) = 25
+clz(00000040) = 25
+clz(0000003f) = 26
+clz(00000020) = 26
+clz(0000001f) = 27
+clz(00000010) = 27
+clz(0000000f) = 28
+clz(00000008) = 28
+clz(00000007) = 29
+clz(00000004) = 29
+clz(00000003) = 30
+clz(00000002) = 30
+clz(00000001) = 31
+clz(00000001) = 31
+clzll(ffffffffffffffff) = 0
+clzll(8000000000000000) = 0
+clzll(7fffffffffffffff) = 1
+clzll(4000000000000000) = 1
+clzll(3fffffffffffffff) = 2
+clzll(2000000000000000) = 2
+clzll(1fffffffffffffff) = 3
+clzll(1000000000000000) = 3
+clzll(0fffffffffffffff) = 4
+clzll(0800000000000000) = 4
+clzll(07ffffffffffffff) = 5
+clzll(0400000000000000) = 5
+clzll(03ffffffffffffff) = 6
+clzll(0200000000000000) = 6
+clzll(01ffffffffffffff) = 7
+clzll(0100000000000000) = 7
+clzll(00ffffffffffffff) = 8
+clzll(0080000000000000) = 8
+clzll(007fffffffffffff) = 9
+clzll(0040000000000000) = 9
+clzll(003fffffffffffff) = 10
+clzll(0020000000000000) = 10
+clzll(001fffffffffffff) = 11
+clzll(0010000000000000) = 11
+clzll(000fffffffffffff) = 12
+clzll(0008000000000000) = 12
+clzll(0007ffffffffffff) = 13
+clzll(0004000000000000) = 13
+clzll(0003ffffffffffff) = 14
+clzll(0002000000000000) = 14
+clzll(0001ffffffffffff) = 15
+clzll(0001000000000000) = 15
+clzll(0000ffffffffffff) = 16
+clzll(0000800000000000) = 16
+clzll(00007fffffffffff) = 17
+clzll(0000400000000000) = 17
+clzll(00003fffffffffff) = 18
+clzll(0000200000000000) = 18
+clzll(00001fffffffffff) = 19
+clzll(0000100000000000) = 19
+clzll(00000fffffffffff) = 20
+clzll(0000080000000000) = 20
+clzll(000007ffffffffff) = 21
+clzll(0000040000000000) = 21
+clzll(000003ffffffffff) = 22
+clzll(0000020000000000) = 22
+clzll(000001ffffffffff) = 23
+clzll(0000010000000000) = 23
+clzll(000000ffffffffff) = 24
+clzll(0000008000000000) = 24
+clzll(0000007fffffffff) = 25
+clzll(0000004000000000) = 25
+clzll(0000003fffffffff) = 26
+clzll(0000002000000000) = 26
+clzll(0000001fffffffff) = 27
+clzll(0000001000000000) = 27
+clzll(0000000fffffffff) = 28
+clzll(0000000800000000) = 28
+clzll(00000007ffffffff) = 29
+clzll(0000000400000000) = 29
+clzll(00000003ffffffff) = 30
+clzll(0000000200000000) = 30
+clzll(00000001ffffffff) = 31
+clzll(0000000100000000) = 31
+clzll(00000000ffffffff) = 32
+clzll(0000000080000000) = 32
+clzll(000000007fffffff) = 33
+clzll(0000000040000000) = 33
+clzll(000000003fffffff) = 34
+clzll(0000000020000000) = 34
+clzll(000000001fffffff) = 35
+clzll(0000000010000000) = 35
+clzll(000000000fffffff) = 36
+clzll(0000000008000000) = 36
+clzll(0000000007ffffff) = 37
+clzll(0000000004000000) = 37
+clzll(0000000003ffffff) = 38
+clzll(0000000002000000) = 38
+clzll(0000000001ffffff) = 39
+clzll(0000000001000000) = 39
+clzll(0000000000ffffff) = 40
+clzll(0000000000800000) = 40
+clzll(00000000007fffff) = 41
+clzll(0000000000400000) = 41
+clzll(00000000003fffff) = 42
+clzll(0000000000200000) = 42
+clzll(00000000001fffff) = 43
+clzll(0000000000100000) = 43
+clzll(00000000000fffff) = 44
+clzll(0000000000080000) = 44
+clzll(000000000007ffff) = 45
+clzll(0000000000040000) = 45
+clzll(000000000003ffff) = 46
+clzll(0000000000020000) = 46
+clzll(000000000001ffff) = 47
+clzll(0000000000010000) = 47
+clzll(000000000000ffff) = 48
+clzll(0000000000008000) = 48
+clzll(0000000000007fff) = 49
+clzll(0000000000004000) = 49
+clzll(0000000000003fff) = 50
+clzll(0000000000002000) = 50
+clzll(0000000000001fff) = 51
+clzll(0000000000001000) = 51
+clzll(0000000000000fff) = 52
+clzll(0000000000000800) = 52
+clzll(00000000000007ff) = 53
+clzll(0000000000000400) = 53
+clzll(00000000000003ff) = 54
+clzll(0000000000000200) = 54
+clzll(00000000000001ff) = 55
+clzll(0000000000000100) = 55
+clzll(00000000000000ff) = 56
+clzll(0000000000000080) = 56
+clzll(000000000000007f) = 57
+clzll(0000000000000040) = 57
+clzll(000000000000003f) = 58
+clzll(0000000000000020) = 58
+clzll(000000000000001f) = 59
+clzll(0000000000000010) = 59
+clzll(000000000000000f) = 60
+clzll(0000000000000008) = 60
+clzll(0000000000000007) = 61
+clzll(0000000000000004) = 61
+clzll(0000000000000003) = 62
+clzll(0000000000000002) = 62
+clzll(0000000000000001) = 63
+clzll(0000000000000001) = 63
+ctz(00000001) = 0
+ctz(ffffffff) = 0
+ctz(00000002) = 1
+ctz(fffffffe) = 1
+ctz(00000004) = 2
+ctz(fffffffc) = 2
+ctz(00000008) = 3
+ctz(fffffff8) = 3
+ctz(00000010) = 4
+ctz(fffffff0) = 4
+ctz(00000020) = 5
+ctz(ffffffe0) = 5
+ctz(00000040) = 6
+ctz(ffffffc0) = 6
+ctz(00000080) = 7
+ctz(ffffff80) = 7
+ctz(00000100) = 8
+ctz(ffffff00) = 8
+ctz(00000200) = 9
+ctz(fffffe00) = 9
+ctz(00000400) = 10
+ctz(fffffc00) = 10
+ctz(00000800) = 11
+ctz(fffff800) = 11
+ctz(00001000) = 12
+ctz(fffff000) = 12
+ctz(00002000) = 13
+ctz(ffffe000) = 13
+ctz(00004000) = 14
+ctz(ffffc000) = 14
+ctz(00008000) = 15
+ctz(ffff8000) = 15
+ctz(00010000) = 16
+ctz(ffff0000) = 16
+ctz(00020000) = 17
+ctz(fffe0000) = 17
+ctz(00040000) = 18
+ctz(fffc0000) = 18
+ctz(00080000) = 19
+ctz(fff80000) = 19
+ctz(00100000) = 20
+ctz(fff00000) = 20
+ctz(00200000) = 21
+ctz(ffe00000) = 21
+ctz(00400000) = 22
+ctz(ffc00000) = 22
+ctz(00800000) = 23
+ctz(ff800000) = 23
+ctz(01000000) = 24
+ctz(ff000000) = 24
+ctz(02000000) = 25
+ctz(fe000000) = 25
+ctz(04000000) = 26
+ctz(fc000000) = 26
+ctz(08000000) = 27
+ctz(f8000000) = 27
+ctz(10000000) = 28
+ctz(f0000000) = 28
+ctz(20000000) = 29
+ctz(e0000000) = 29
+ctz(40000000) = 30
+ctz(c0000000) = 30
+ctz(80000000) = 31
+ctz(80000000) = 31
+ctzll(0000000000000001) = 0
+ctzll(ffffffffffffffff) = 0
+ctzll(0000000000000002) = 1
+ctzll(fffffffffffffffe) = 1
+ctzll(0000000000000004) = 2
+ctzll(fffffffffffffffc) = 2
+ctzll(0000000000000008) = 3
+ctzll(fffffffffffffff8) = 3
+ctzll(0000000000000010) = 4
+ctzll(fffffffffffffff0) = 4
+ctzll(0000000000000020) = 5
+ctzll(ffffffffffffffe0) = 5
+ctzll(0000000000000040) = 6
+ctzll(ffffffffffffffc0) = 6
+ctzll(0000000000000080) = 7
+ctzll(ffffffffffffff80) = 7
+ctzll(0000000000000100) = 8
+ctzll(ffffffffffffff00) = 8
+ctzll(0000000000000200) = 9
+ctzll(fffffffffffffe00) = 9
+ctzll(0000000000000400) = 10
+ctzll(fffffffffffffc00) = 10
+ctzll(0000000000000800) = 11
+ctzll(fffffffffffff800) = 11
+ctzll(0000000000001000) = 12
+ctzll(fffffffffffff000) = 12
+ctzll(0000000000002000) = 13
+ctzll(ffffffffffffe000) = 13
+ctzll(0000000000004000) = 14
+ctzll(ffffffffffffc000) = 14
+ctzll(0000000000008000) = 15
+ctzll(ffffffffffff8000) = 15
+ctzll(0000000000010000) = 16
+ctzll(ffffffffffff0000) = 16
+ctzll(0000000000020000) = 17
+ctzll(fffffffffffe0000) = 17
+ctzll(0000000000040000) = 18
+ctzll(fffffffffffc0000) = 18
+ctzll(0000000000080000) = 19
+ctzll(fffffffffff80000) = 19
+ctzll(0000000000100000) = 20
+ctzll(fffffffffff00000) = 20
+ctzll(0000000000200000) = 21
+ctzll(ffffffffffe00000) = 21
+ctzll(0000000000400000) = 22
+ctzll(ffffffffffc00000) = 22
+ctzll(0000000000800000) = 23
+ctzll(ffffffffff800000) = 23
+ctzll(0000000001000000) = 24
+ctzll(ffffffffff000000) = 24
+ctzll(0000000002000000) = 25
+ctzll(fffffffffe000000) = 25
+ctzll(0000000004000000) = 26
+ctzll(fffffffffc000000) = 26
+ctzll(0000000008000000) = 27
+ctzll(fffffffff8000000) = 27
+ctzll(0000000010000000) = 28
+ctzll(fffffffff0000000) = 28
+ctzll(0000000020000000) = 29
+ctzll(ffffffffe0000000) = 29
+ctzll(0000000040000000) = 30
+ctzll(ffffffffc0000000) = 30
+ctzll(0000000080000000) = 31
+ctzll(ffffffff80000000) = 31
+ctzll(0000000100000000) = 32
+ctzll(ffffffff00000000) = 32
+ctzll(0000000200000000) = 33
+ctzll(fffffffe00000000) = 33
+ctzll(0000000400000000) = 34
+ctzll(fffffffc00000000) = 34
+ctzll(0000000800000000) = 35
+ctzll(fffffff800000000) = 35
+ctzll(0000001000000000) = 36
+ctzll(fffffff000000000) = 36
+ctzll(0000002000000000) = 37
+ctzll(ffffffe000000000) = 37
+ctzll(0000004000000000) = 38
+ctzll(ffffffc000000000) = 38
+ctzll(0000008000000000) = 39
+ctzll(ffffff8000000000) = 39
+ctzll(0000010000000000) = 40
+ctzll(ffffff0000000000) = 40
+ctzll(0000020000000000) = 41
+ctzll(fffffe0000000000) = 41
+ctzll(0000040000000000) = 42
+ctzll(fffffc0000000000) = 42
+ctzll(0000080000000000) = 43
+ctzll(fffff80000000000) = 43
+ctzll(0000100000000000) = 44
+ctzll(fffff00000000000) = 44
+ctzll(0000200000000000) = 45
+ctzll(ffffe00000000000) = 45
+ctzll(0000400000000000) = 46
+ctzll(ffffc00000000000) = 46
+ctzll(0000800000000000) = 47
+ctzll(ffff800000000000) = 47
+ctzll(0001000000000000) = 48
+ctzll(ffff000000000000) = 48
+ctzll(0002000000000000) = 49
+ctzll(fffe000000000000) = 49
+ctzll(0004000000000000) = 50
+ctzll(fffc000000000000) = 50
+ctzll(0008000000000000) = 51
+ctzll(fff8000000000000) = 51
+ctzll(0010000000000000) = 52
+ctzll(fff0000000000000) = 52
+ctzll(0020000000000000) = 53
+ctzll(ffe0000000000000) = 53
+ctzll(0040000000000000) = 54
+ctzll(ffc0000000000000) = 54
+ctzll(0080000000000000) = 55
+ctzll(ff80000000000000) = 55
+ctzll(0100000000000000) = 56
+ctzll(ff00000000000000) = 56
+ctzll(0200000000000000) = 57
+ctzll(fe00000000000000) = 57
+ctzll(0400000000000000) = 58
+ctzll(fc00000000000000) = 58
+ctzll(0800000000000000) = 59
+ctzll(f800000000000000) = 59
+ctzll(1000000000000000) = 60
+ctzll(f000000000000000) = 60
+ctzll(2000000000000000) = 61
+ctzll(e000000000000000) = 61
+ctzll(4000000000000000) = 62
+ctzll(c000000000000000) = 62
+ctzll(8000000000000000) = 63
+ctzll(8000000000000000) = 63
+fabs(3.141590) = 3.141590
+fabs(-3.141590) = 3.141590
+fabsf(7.250000) = 7.250000
+fabsf(-7.250000) = 7.250000
diff --git a/test/regression/Results/many_parameters b/test/regression/Results/many_parameters
new file mode 100644
index 00000000..838cfd7e
--- /dev/null
+++ b/test/regression/Results/many_parameters
@@ -0,0 +1 @@
+1780
diff --git a/test/regression/Results/varargs2-kvx b/test/regression/Results/varargs2-kvx
new file mode 100644
index 00000000..0576ca01
--- /dev/null
+++ b/test/regression/Results/varargs2-kvx
@@ -0,0 +1,12 @@
+An int: 42
+A long long: 123456789012345
+A string: Hello world
+A double: 3.141592654
+A mixture: x & Hello, world! & 42 & 123456789012345 & 3.141592654 & 2.718281746
+Twice: -1 1.23
+Twice: -1 1.23
+With va_copy: -1 1.23
+With va_copy: -1 1.23
+With extra args: x & Hello, world! & 42 & 123456789012345 & 3.141592654 & 2.718281746
+With extra FP args: 3.141592654 & 2.718281746 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 42
+va_list compatibility: x & Hello, world! & 42 & 123456789012345 & 3.141592654 & 2.718281746
diff --git a/test/regression/builtins-common.c b/test/regression/builtins-common.c
index 51d0584e..72839a61 100644
--- a/test/regression/builtins-common.c
+++ b/test/regression/builtins-common.c
@@ -44,8 +44,10 @@ int main(int argc, char ** argv)
printf("fabs(%f) = %f\n", -a, __builtin_fabs(-a));
printf("fabsf(%f) = %f\n", f, __builtin_fabsf(f));
printf("fabsf(%f) = %f\n", -f, __builtin_fabsf(-f));
+#ifndef __KVX__ // no builtin FSQRT or SQRT on KVX
printf("fsqrt(%f) = %f\n", a, __builtin_fsqrt(a));
printf("sqrt(%f) = %f\n", a, __builtin_sqrt(a));
+#endif
/* Make sure that ignoring the result of a builtin
doesn't cause an internal error */
diff --git a/test/regression/builtins-kvx.c b/test/regression/builtins-kvx.c
new file mode 100644
index 00000000..cbf51387
--- /dev/null
+++ b/test/regression/builtins-kvx.c
@@ -0,0 +1,72 @@
+/* Fun with builtins */
+
+#include <stdio.h>
+#include <math.h>
+
+char * check_relative_error(double exact, double actual, double precision)
+{
+ double relative_error = (actual - exact) / exact;
+ return fabs(relative_error) <= precision ? "OK" : "ERROR";
+}
+
+//unsigned int x = 0x12345678;
+//unsigned int y = 0xDEADBEEF;
+//unsigned long long xx = 0x1234567812345678ULL;
+//double a = 3.14159;
+//double b = 2.718;
+//double c = 1.414;
+//unsigned short s = 0x1234;
+
+int main(int argc, char ** argv)
+{
+ unsigned z;
+
+ //printf("mulhw(%x, %x) = %x\n", x, y, __builtin_mulhw(x, y));
+ //printf("mulhwu(%x, %x) = %x\n", x, y, __builtin_mulhwu(x, y));
+ //printf("clz(%x) = %d\n", x, __builtin_clz(x));
+ //printf("clzll(%llx) = %d\n", (unsigned long long) x, __builtin_clzll(x));
+ //printf("clzll(%llx) = %d\n", xx, __builtin_clzll(xx));
+ //z = __builtin_bswap(x);
+ //printf("clzll(%lx) = %d\n", z, __builtin_clzll(z));
+ //printf("bswap(%x) = %x\n", x, __builtin_bswap(x));
+ //printf("bswap16(%x) = %x\n", s, __builtin_bswap16(s));
+
+ //printf("fmadd(%f, %f, %f) = %f\n", a, b, c, __builtin_fmadd(a, b, c));
+ //printf("fmsub(%f, %f, %f) = %f\n", a, b, c, __builtin_fmsub(a, b, c));
+ //printf("fabs(%f) = %f\n", a, __builtin_fabs(a));
+ //printf("fabs(%f) = %f\n", -a, __builtin_fabs(-a));
+ //printf("fsqrt(%f) = %f\n", a, __builtin_fsqrt(a));
+ //printf("frsqrte(%f) = %s\n",
+ // a, check_relative_error(1.0 / sqrt(a), __builtin_frsqrte(a), 1./32.));
+ //printf("fres(%f) = %s\n",
+ // a, check_relative_error(1.0 / a, __builtin_fres(a), 1./256.));
+ //printf("fsel(%f, %f, %f) = %f\n", a, b, c, __builtin_fsel(a, b, c));
+ //printf("fsel(%f, %f, %f) = %f\n", -a, b, c, __builtin_fsel(-a, b, c));
+ //printf("fcti(%f) = %d\n", a, __builtin_fcti(a));
+ //printf("fcti(%f) = %d\n", b, __builtin_fcti(b));
+ //printf("fcti(%f) = %d\n", c, __builtin_fcti(c));
+ //__builtin_eieio();
+ //__builtin_sync();
+ //__builtin_isync();
+ //printf("isel(%d, %d, %d) = %d\n", 0, x, y, __builtin_isel(0, x, y));
+ //printf("isel(%d, %d, %d) = %d\n", 42, x, y, __builtin_isel(42, x, y));
+ //printf ("read_16_rev = %x\n", __builtin_read16_reversed(&s));
+ //printf ("read_32_rev = %x\n", __builtin_read32_reversed(&y));
+ //__builtin_write16_reversed(&s, 0x789A);
+ //printf ("after write_16_rev: %x\n", s);
+ //__builtin_write32_reversed(&y, 0x12345678);
+ //printf ("after write_32_rev: %x\n", y);
+ //y = 0;
+ //__builtin_write32_reversed(&y, 0x12345678);
+ //printf ("CSE write_32_rev: %s\n", y == 0x78563412 ? "ok" : "ERROR");
+ ///* Make sure that ignoring the result of a builtin
+ // doesn't cause an internal error */
+ //(void) __builtin_bswap(x);
+ //(void) __builtin_fsqrt(a);
+ return 0;
+}
+
+
+
+
+
diff --git a/test/regression/extasm.c b/test/regression/extasm.c
index 297178d1..e78fb741 100644
--- a/test/regression/extasm.c
+++ b/test/regression/extasm.c
@@ -24,6 +24,7 @@ int clobbers(int x, int z)
|| (defined(ARCH_riscV) && defined(MODEL_64)) \
|| (defined(ARCH_powerpc) && defined(MODEL_ppc64)) \
|| (defined(ARCH_powerpc) && defined(MODEL_e5500)) \
+ || (defined(ARCH_kvx) && defined(MODEL_64)) \
|| defined(ARCH_aarch64)
#define SIXTYFOUR
#else
diff --git a/test/regression/many_parameters.c b/test/regression/many_parameters.c
new file mode 100644
index 00000000..4d2529c0
--- /dev/null
+++ b/test/regression/many_parameters.c
@@ -0,0 +1,313 @@
+#include <stdio.h>
+
+int call1(
+ int call00,
+ int call01,
+ int call02,
+ int call03,
+ int call04,
+ int call05,
+ int call06,
+ int call07,
+ int call08,
+ int call09,
+ int call10,
+ int call11,
+ int call12,
+ int call13,
+ int call14,
+ int call15,
+ int call16,
+ int call17,
+ int call18,
+ int call19,
+ int call20,
+ int call21,
+ int call22,
+ int call23,
+ int call24,
+ int call25,
+ int call26,
+ int call27,
+ int call28,
+ int call29,
+ int call30,
+ int call31,
+ int call32,
+ int call33,
+ int call34,
+ int call35,
+ int call36,
+ int call37,
+ int call38,
+ int call39,
+ int call40,
+ int call41,
+ int call42,
+ int call43,
+ int call44,
+ int call45,
+ int call46,
+ int call47,
+ int call48,
+ int call49,
+ int call50,
+ int call51,
+ int call52,
+ int call53,
+ int call54,
+ int call55,
+ int call56,
+ int call57,
+ int call58,
+ int call59) {
+ return ( call00
+ + call01
+ + call02
+ + call03
+ + call04
+ + call05
+ + call06
+ + call07
+ + call08
+ + call09
+ + call10
+ + call11
+ + call12
+ + call13
+ + call14
+ + call15
+ + call16
+ + call17
+ + call18
+ + call19
+ + call20
+ + call21
+ + call22
+ + call23
+ + call24
+ + call25
+ + call26
+ + call27
+ + call28
+ + call29
+ + call30
+ + call31
+ + call32
+ + call33
+ + call34
+ + call35
+ + call36
+ + call37
+ + call38
+ + call39
+ + call40
+ + call41
+ + call42
+ + call43
+ + call44
+ + call45
+ + call46
+ + call47
+ + call48
+ + call49
+ + call50
+ + call51
+ + call52
+ + call53
+ + call54
+ + call55
+ + call56
+ + call57
+ + call58
+ + call59);
+}
+
+int call2(
+ int call00,
+ int call01,
+ int call02,
+ int call03,
+ int call04,
+ int call05,
+ int call06,
+ int call07,
+ int call08,
+ int call09,
+ int call10,
+ int call11,
+ int call12,
+ int call13,
+ int call14,
+ int call15,
+ int call16,
+ int call17,
+ int call18,
+ int call19,
+ int call20,
+ int call21,
+ int call22,
+ int call23,
+ int call24,
+ int call25,
+ int call26,
+ int call27,
+ int call28,
+ int call29,
+ int call30,
+ int call31,
+ int call32,
+ int call33,
+ int call34,
+ int call35,
+ int call36,
+ int call37,
+ int call38,
+ int call39,
+ int call40,
+ int call41,
+ int call42,
+ int call43,
+ int call44,
+ int call45,
+ int call46,
+ int call47,
+ int call48,
+ int call49,
+ int call50,
+ int call51,
+ int call52,
+ int call53,
+ int call54,
+ int call55,
+ int call56,
+ int call57,
+ int call58,
+ int call59) {
+ return 10 + call1(
+ call00,
+ call01,
+ call02,
+ call03,
+ call04,
+ call05,
+ call06,
+ call07,
+ call08,
+ call09,
+ call10,
+ call11,
+ call12,
+ call13,
+ call14,
+ call15,
+ call16,
+ call17,
+ call18,
+ call19,
+ call20,
+ call21,
+ call22,
+ call23,
+ call24,
+ call25,
+ call26,
+ call27,
+ call28,
+ call29,
+ call30,
+ call31,
+ call32,
+ call33,
+ call34,
+ call35,
+ call36,
+ call37,
+ call38,
+ call39,
+ call40,
+ call41,
+ call42,
+ call43,
+ call44,
+ call45,
+ call46,
+ call47,
+ call48,
+ call49,
+ call50,
+ call51,
+ call52,
+ call53,
+ call54,
+ call55,
+ call56,
+ call57,
+ call58,
+ call59);
+}
+
+int main() {
+ int x =
+ call2( 0,
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9,
+ 10,
+ 11,
+ 12,
+ 13,
+ 14,
+ 15,
+ 16,
+ 17,
+ 18,
+ 19,
+ 20,
+ 21,
+ 22,
+ 23,
+ 24,
+ 25,
+ 26,
+ 27,
+ 28,
+ 29,
+ 30,
+ 31,
+ 32,
+ 33,
+ 34,
+ 35,
+ 36,
+ 37,
+ 38,
+ 39,
+ 40,
+ 41,
+ 42,
+ 43,
+ 44,
+ 45,
+ 46,
+ 47,
+ 48,
+ 49,
+ 50,
+ 51,
+ 52,
+ 53,
+ 54,
+ 55,
+ 56,
+ 57,
+ 58,
+ 59);
+ printf("%d\n", x);
+}
diff --git a/test/regression/packedstruct1.c b/test/regression/packedstruct1.c
index 5d3e7124..b805c92a 100644
--- a/test/regression/packedstruct1.c
+++ b/test/regression/packedstruct1.c
@@ -2,8 +2,8 @@
#include <stdio.h>
-/* offsetof is the offset computed by the verified front-end (cfrontend/) */
-#define offsetof(s,f) (int)&(((struct s *)0)->f)
+/* offsetOf is the offset computed by the verified front-end (cfrontend/) */
+#define offsetOf(s,f) (int)&(((struct s *)0)->f)
/* boffsetof is the offset computed by the elaborator (cparser/) */
#define boffsetof(s,f) (int)__builtin_offsetof(struct s, f)
@@ -24,7 +24,7 @@ void test1(void)
printf("sizeof(struct s1) = %d\n", szof(s1));
printf("precomputed sizeof(struct s1) = %d\n", bszof(s1));
printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
- offsetof(s1,x), offsetof(s1,y), offsetof(s1,z));
+ offsetOf(s1,x), offsetOf(s1,y), offsetOf(s1,z));
printf("precomputed offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
boffsetof(s1,x), boffsetof(s1,y), boffsetof(s1,z));
s1.x = 123; s1.y = -456; s1.z = 3.14159;
@@ -45,7 +45,7 @@ void test2(void)
printf("precomputed sizeof(struct s2) = %d\n", bszof(s2));
printf("&s2 mod 16 = %d\n", ((int) &s2) & 0xF);
printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
- offsetof(s2,x), offsetof(s2,y), offsetof(s2,z));
+ offsetOf(s2,x), offsetOf(s2,y), offsetOf(s2,z));
printf("precomputed offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
boffsetof(s2,x), boffsetof(s2,y), boffsetof(s2,z));
s2.x = 12345; s2.y = -456; s2.z = 3.14159;
@@ -73,7 +73,7 @@ void test3(void)
printf("sizeof(struct s3) = %d\n", szof(s3));
printf("precomputed sizeof(struct s3) = %d\n", bszof(s3));
- printf("offsetof(s) = %d\n", offsetof(s3,s));
+ printf("offsetof(s) = %d\n", offsetOf(s3,s));
printf("precomputed offsetof(s) = %d\n", boffsetof(s3,s));
s3.x = 123;
s3.y = 45678;
@@ -104,7 +104,7 @@ void test4(void)
printf("sizeof(struct s4) = %d\n", szof(s4));
printf("precomputed sizeof(struct s4) = %d\n", bszof(s4));
printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
- offsetof(s4,x), offsetof(s4,y), offsetof(s4,z));
+ offsetOf(s4,x), offsetOf(s4,y), offsetOf(s4,z));
printf("precomputed offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
boffsetof(s4,x), boffsetof(s4,y), boffsetof(s4,z));
s4.x = 123; s4.y = -456; s4.z = 3.14159;
@@ -122,7 +122,7 @@ void test5(void)
printf("sizeof(struct s5) = %d\n", szof(s5));
printf("precomputed sizeof(struct s5) = %d\n", bszof(s5));
printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
- offsetof(s5,x), offsetof(s5,y), offsetof(s5,z));
+ offsetOf(s5,x), offsetOf(s5,y), offsetOf(s5,z));
printf("precomputed offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
boffsetof(s5,x), boffsetof(s5,y), boffsetof(s5,z));
s5.x = 123; s5.y = -456; s5.z = 3.14159;
@@ -140,7 +140,7 @@ void test6(void)
printf("sizeof(struct s6) = %d\n", szof(s6));
printf("precomputed sizeof(struct s6) = %d\n", bszof(s6));
printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
- offsetof(s6,x), offsetof(s6,y), offsetof(s6,z));
+ offsetOf(s6,x), offsetOf(s6,y), offsetOf(s6,z));
printf("precomputed offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n",
boffsetof(s6,x), boffsetof(s6,y), boffsetof(s6,z));
s62.x = 123; s62.y = -456; s62.z = 3.14159;
diff --git a/test/regression/ran000373_reduced.c b/test/regression/ran000373_reduced.c
new file mode 100644
index 00000000..3f26e88a
--- /dev/null
+++ b/test/regression/ran000373_reduced.c
@@ -0,0 +1,36 @@
+#include <stdint.h>
+#include <stdio.h>
+uint32_t au, cc, bk;
+union ba {
+ int64_t bb;
+};
+int32_t *ck;
+int32_t **cr = &ck;
+void a(uint32_t c, int d) { printf("checksum = %X\n", c); }
+void av(uint8_t b) { au = b; }
+void aw(uint64_t ax) { av(ax); }
+void ay(uint64_t ax, char *az, int d) { aw(ax); }
+static const int32_t df(int64_t, union ba, uint16_t, uint8_t);
+const int32_t dh(int32_t, int32_t, uint32_t);
+int16_t e(void) {
+ union ba f = {5};
+ int32_t g = 5;
+ int32_t i[90269];
+ uint32_t s = df(dh(0, 0, cc), f, g, 7);
+ return s;
+}
+const int32_t df(int64_t j, union ba k, uint16_t l, uint8_t m) {
+ *ck = k.bb;
+ return l;
+}
+const int32_t dh(int32_t n, int32_t o, uint32_t p) {
+ int32_t *t = &bk;
+ uint64_t q = 3;
+ *cr = t;
+ return q;
+}
+int main(void) {
+ int r = e();
+ ay(bk, "", r);
+ a(au, r);
+}
diff --git a/test/regression/union_passing.c b/test/regression/union_passing.c
new file mode 100644
index 00000000..b0ce6319
--- /dev/null
+++ b/test/regression/union_passing.c
@@ -0,0 +1,23 @@
+#include <stdint.h>
+#include <stdio.h>
+static int32_t bk;
+union ba {
+ int64_t bb;
+};
+static void dada(union ba);
+void nothing(void);
+void stuff(void) {
+ union ba f = {5};
+ int32_t i[1000];
+ nothing();
+ dada(f);
+}
+static void dada(union ba k) {
+ bk = k.bb;
+}
+void nothing(void) {
+}
+int main() {
+ stuff();
+ printf("result = %d\n", bk);
+}
diff --git a/test/regression/varargs2.c b/test/regression/varargs2.c
index d64509e5..e3492ead 100644
--- a/test/regression/varargs2.c
+++ b/test/regression/varargs2.c
@@ -126,15 +126,27 @@ void printf_compat(const char * fmt, ...)
}
/* The test harness */
-
int main()
{
miniprintf("An int: %d\n", 42);
miniprintf("A long long: %l\n", 123456789012345LL);
miniprintf("A string: %s\n", "Hello world");
miniprintf("A double: %e\n", 3.141592654);
+
+#ifndef __KVX__
miniprintf("A small struct: %y\n", (struct Y) { 'x', 12 });
miniprintf("A bigger struct: %z\n", (struct Z) { 123, 456, 789 });
+#endif
+
+#ifdef __KVX__
+ miniprintf("A mixture: %c & %s & %d & %l & %e & %f\n",
+ 'x',
+ "Hello, world!",
+ 42,
+ 123456789012345LL,
+ 3.141592654,
+ 2.71828182);
+#else
miniprintf("A mixture: %c & %s & %y & %d & %l & %e & %f\n",
'x',
"Hello, world!",
@@ -143,6 +155,8 @@ int main()
123456789012345LL,
3.141592654,
2.71828182);
+#endif
+
miniprintf2("Twice: %d %e\n", -1, 1.23);
miniprintf3("With va_copy: %d %e\n", -1, 1.23);
miniprintf_extra(0, 1, 2, 3, 4, 5, 6, 7,
diff --git a/tools/compiler_expand.ml b/tools/compiler_expand.ml
new file mode 100644
index 00000000..45ffc828
--- /dev/null
+++ b/tools/compiler_expand.ml
@@ -0,0 +1,194 @@
+(*
+The Compcert verified compiler
+
+Compiler.vexpand -> Compiler.v
+
+Expand the list of RTL compiler passes into Compiler.v
+
+David Monniaux, CNRS, VERIMAG
+ *)
+
+type is_partial = TOTAL | PARTIAL;;
+type print_result = Noprint | Print of string;;
+type when_triggered = Always | Option of string;;
+type needs_require = Require | NoRequire;;
+
+let rtl_tunneling = PARTIAL, Always, Require, (Some "RTL Branch Tunneling"), "RTLTunneling"
+
+(* FIXME - The gestion of NoRequire is a bit ugly right now. *)
+let rtl_passes =
+[|
+TOTAL, (Option "optim_tailcalls"), Require, (Some "Tail calls"), "Tailcall";
+PARTIAL, Always, Require, (Some "Inlining"), "Inlining";
+TOTAL, (Option "profile_arcs"), Require, (Some "Profiling insertion"), "Profiling";
+TOTAL, (Option "branch_probabilities"), Require, (Some "Profiling use"), "ProfilingExploit";
+TOTAL, (Option "optim_move_loop_invariants"), Require, (Some "Inserting initial nop"), "FirstNop";
+TOTAL, Always, Require, (Some "Renumbering"), "Renumber";
+PARTIAL, (Option "optim_CSE"), Require, (Some "CSE"), "CSE";
+PARTIAL, Always, NoRequire, (Some "Static Prediction + inverting conditions"), "Staticpredict";
+PARTIAL, Always, NoRequire, (Some "Unrolling one iteration out of innermost loops"), "Unrollsingle";
+TOTAL, Always, NoRequire, (Some "Renumbering pre tail duplication"), "Renumber";
+PARTIAL, Always, NoRequire, (Some "Performing tail duplication"), "Tailduplicate";
+TOTAL, Always, NoRequire, (Some "Renumbering pre unrolling"), "Renumber";
+PARTIAL, Always, NoRequire, (Some "Unrolling the body of innermost loops"), "Unrollbody";
+TOTAL, Always, NoRequire, (Some "Renumbering pre constprop"), "Renumber";
+TOTAL, (Option "optim_constprop"), Require, (Some "Constant propagation"), "Constprop";
+TOTAL, Always, NoRequire, (Some "Renumbering pre CSE"), "Renumber";
+PARTIAL, (Option "optim_CSE"), Require, (Some "CSE"), "CSE";
+TOTAL, (Option "optim_CSE2"), Require, (Some "CSE2"), "CSE2";
+PARTIAL, (Option "optim_CSE3"), Require, (Some "CSE3"), "CSE3";
+TOTAL, (Option "optim_CSE3"), Require, (Some "Kill useless moves after CSE3"), "KillUselessMoves";
+TOTAL, (Option "optim_forward_moves"), Require, (Some "Forwarding moves"), "ForwardMoves";
+PARTIAL, (Option "optim_redundancy"), Require, (Some "Redundancy elimination"), "Deadcode";
+rtl_tunneling;
+TOTAL, Always, Require, (Some "Renumbering pre rotate"), "Renumber";
+PARTIAL, Always, NoRequire, (Some "Loop Rotate"), "Looprotate";
+TOTAL, (Option "optim_move_loop_invariants"), NoRequire, (Some "Renumbering for LICM"), "Renumber";
+PARTIAL, (Option "optim_move_loop_invariants"), Require, (Some "LICM"), "LICM";
+TOTAL, (Option "optim_move_loop_invariants"), NoRequire, (Some "Renumbering for LICM"), "Renumber";
+PARTIAL, (Option "optim_move_loop_invariants"), NoRequire, (Some "CSE3 for LICM"), "CSE3";
+PARTIAL, (Option "optim_move_loop_invariants"), NoRequire, (Some "Redundancy elimination for LICM"), "Deadcode";
+TOTAL, (Option "all_loads_nontrap"), Require, None, "Allnontrap";
+PARTIAL, Always, Require, (Some "Unused globals"), "Unusedglob";
+|];;
+
+let post_rtl_passes =
+[|
+ PARTIAL, Always, Require, (Some "BTL generation"), "RTLtoBTL", Noprint;
+ PARTIAL, Always, Require, (Some "Prepass scheduling"), "BTL_Scheduler", Noprint;
+ PARTIAL, Always, Require, (Some "Projection to RTL"), "BTLtoRTL", (Print (Printf.sprintf "RTL %d" ((Array.length rtl_passes) + 1)));
+ PARTIAL, Always, Require, (Some "Register allocation"), "Allocation", (Print "LTL 1");
+ PARTIAL, Always, Require, (Some "LTL Branch tunneling"), "LTLTunneling", (Print "LTL 2");
+ PARTIAL, Always, Require, (Some "CFG linearization"), "Linearize", Noprint;
+ TOTAL, Always, Require, (Some "Label cleanup"), "CleanupLabels", Noprint;
+ PARTIAL, (Option "debug"), Require, (Some "Debugging info for local variables"), "Debugvar", Noprint;
+ PARTIAL, Always, Require, (Some "Mach generation"), "Stacking", (Print "Mach")
+|];;
+
+let all_passes =
+ Array.concat
+ [Array.mapi
+ (fun i (a,b,r,c,d) -> (a,b,r,c,d, Print (Printf.sprintf "RTL %d" (i+1))))
+ rtl_passes;
+ post_rtl_passes];;
+
+let totality = function TOTAL -> "total" | PARTIAL -> "partial";;
+
+let print_rtl_require oc =
+ Array.iter (fun (partial, trigger, require, time_label, pass_name, printing) ->
+ match require with Require ->
+ Printf.fprintf oc "Require %s.\n" pass_name
+ | _ -> ()
+ ) all_passes;;
+
+let print_rtl_require_proof oc =
+ Array.iter (fun (partial, trigger, require, time_label, pass_name, printing) ->
+ match require with Require ->
+ Printf.fprintf oc "Require %sproof.\n" pass_name
+ | _ -> ()
+ ) all_passes;;
+
+let print_rtl_transf oc =
+ Array.iteri
+ (fun i (partial, trigger, require, time_label, pass_name, printing) ->
+ output_string oc (match partial with
+ | TOTAL -> " @@ "
+ | PARTIAL -> " @@@ ");
+ (match trigger with
+ | Always -> ()
+ | Option s ->
+ Printf.fprintf oc "%s_if Compopts.%s " (totality partial) s);
+ output_char oc '(';
+ (match time_label with
+ | None -> ()
+ | Some s ->
+ Printf.fprintf oc "time \"%s\" " s);
+ Printf.fprintf oc "%s.transf_program)\n" pass_name;
+ (match printing with
+ | Noprint -> ()
+ | Print s ->
+ Printf.fprintf oc " @@ print (print_%s)\n" s)
+ ) all_passes;;
+
+let print_rtl_mkpass oc =
+ Array.iter (fun (partial, trigger, require, time_label, pass_name, printing) ->
+ output_string oc " ::: mkpass (";
+ (match trigger with
+ | Always -> ()
+ | Option s ->
+ Printf.fprintf oc "match_if Compopts.%s " s);
+ Printf.fprintf oc "%sproof.match_prog)\n" pass_name)
+ all_passes;;
+
+let print_if kind oc = function
+ | Always -> ()
+ | Option s -> Printf.fprintf oc "%s_if %s " kind s;;
+
+let numbering_base = 7
+
+let print_rtl_proof oc =
+ Array.iteri (fun i (partial, trigger, require, time_label, pass_name, printing) ->
+ let j = i+numbering_base in
+ match partial with
+ | TOTAL ->
+ Printf.fprintf oc "set (p%d := %a%s.transf_program p%d) in *.\n"
+ j (print_if "total") trigger pass_name (pred j)
+ | PARTIAL ->
+ Printf.fprintf oc "destruct (%a%s.transf_program p%d) as [p%d|e] eqn:P%d; cbn in T; try discriminate.\n"
+ (print_if "partial") trigger pass_name (pred j) j j)
+ all_passes;;
+
+let print_rtl_proof2 oc =
+ Array.iteri (fun i (partial, trigger, require, time_label, pass_name, printing) ->
+ let j = i+numbering_base in
+ Printf.fprintf oc " exists p%d; split. " j;
+ (match trigger with
+ | Always -> ()
+ | Option _ ->
+ (match partial with
+ | TOTAL -> output_string oc "apply total_if_match. "
+ | PARTIAL -> output_string oc "eapply partial_if_match; eauto. "));
+ Printf.fprintf oc "apply %sproof.transf_program_match; auto.\n" pass_name)
+ all_passes;;
+
+let print_rtl_forward_simulations oc =
+ Array.iter (fun (partial, trigger, require, time_label, pass_name) ->
+ output_string oc " eapply compose_forward_simulations.\n ";
+ (match trigger with
+ | Always -> ()
+ | Option s -> output_string oc "eapply match_if_simulation. eassumption. ");
+ Printf.fprintf oc "eapply %sproof.transf_program_correct; eassumption." pass_name
+ )
+ rtl_passes;;
+
+if (Array.length Sys.argv)<>3
+then exit 1;;
+
+let filename_in = Sys.argv.(1) and filename_out = Sys.argv.(2) in
+ let ic = open_in filename_in and oc = open_out filename_out in
+ try
+ while true
+ do
+ match input_line ic with
+ | "EXPAND_RTL_TRANSF_PROGRAM" ->
+ print_rtl_transf oc
+ | "EXPAND_RTL_REQUIRE" ->
+ print_rtl_require oc
+ | "EXPAND_RTL_REQUIRE_PROOF" ->
+ print_rtl_require_proof oc
+ | "EXPAND_RTL_MKPASS" ->
+ print_rtl_mkpass oc
+ | "EXPAND_RTL_PROOF" ->
+ print_rtl_proof oc
+ | "EXPAND_RTL_PROOF2" ->
+ print_rtl_proof2 oc
+ | "EXPAND_ASM_SEMANTICS" ->
+ Printf.fprintf oc " (Asm.semantics p%d)\n"
+ ((Array.length all_passes) + 7)
+ | "EXPAND_RTL_FORWARD_SIMULATIONS" ->
+ print_rtl_forward_simulations oc
+ | line -> (output_string oc line;
+ output_char oc '\n')
+ done
+ with End_of_file ->
+ (close_in ic; close_out oc);;
diff --git a/tools/fix_html_date.sh b/tools/fix_html_date.sh
new file mode 100755
index 00000000..c7fbdabe
--- /dev/null
+++ b/tools/fix_html_date.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+#
+# Replace an HTML comment "<!--@DATE@-->" by the current date
+# in the file given by $1 (with $2 as prefix and $3 as suffix)
+#
+# Result on standard output
+
+sed -e "s/<!--@DATE@-->/$2$(date +'%F')$3/g" $1
diff --git a/x86/Asmexpand.ml b/x86/Asmexpand.ml
index a1c24f2d..f81b9165 100644
--- a/x86/Asmexpand.ml
+++ b/x86/Asmexpand.ml
@@ -624,7 +624,7 @@ let expand_instruction instr =
expand_builtin_memcpy (Z.to_int sz) (Z.to_int al) args
| EF_annot_val(kind,txt, targ) ->
expand_annot_val kind txt targ args res
- | EF_annot _ | EF_debug _ | EF_inline_asm _ ->
+ | EF_annot _ | EF_debug _ | EF_inline_asm _ | EF_profiling _ ->
emit instr
| _ ->
assert false
diff --git a/x86/Asmgen.v b/x86/Asmgen.v
index 73e3263e..99e9fc2b 100644
--- a/x86/Asmgen.v
+++ b/x86/Asmgen.v
@@ -636,9 +636,14 @@ Definition transl_op
(** Translation of memory loads and stores *)
-Definition transl_load (chunk: memory_chunk)
+Definition transl_load
+ (trap : trapping_mode)
+ (chunk: memory_chunk)
(addr: addressing) (args: list mreg) (dest: mreg)
(k: code) : res code :=
+ match trap with
+ | NOTRAP => Error (msg "Asmgen.transl_load x86 does not support non trapping loads")
+ | TRAP =>
do am <- transl_addressing addr args;
match chunk with
| Mint8unsigned =>
@@ -659,6 +664,7 @@ Definition transl_load (chunk: memory_chunk)
do r <- freg_of dest; OK(Pmovsd_fm r am :: k)
| _ =>
Error (msg "Asmgen.transl_load")
+ end
end.
Definition transl_store (chunk: memory_chunk)
@@ -699,8 +705,8 @@ Definition transl_instr (f: Mach.function) (i: Mach.instruction)
loadind RSP f.(fn_link_ofs) Tptr AX k1)
| Mop op args res =>
transl_op op args res k
- | Mload chunk addr args dst =>
- transl_load chunk addr args dst k
+ | Mload trap chunk addr args dst =>
+ transl_load trap chunk addr args dst k
| Mstore chunk addr args src =>
transl_store chunk addr args src k
| Mcall sig (inl reg) =>
diff --git a/x86/Asmgenproof.v b/x86/Asmgenproof.v
index 67c42b2b..8c28fb1b 100644
--- a/x86/Asmgenproof.v
+++ b/x86/Asmgenproof.v
@@ -235,11 +235,11 @@ Proof.
Qed.
Remark transl_load_label:
- forall chunk addr args dest k c,
- transl_load chunk addr args dest k = OK c ->
+ forall trap chunk addr args dest k c,
+ transl_load trap chunk addr args dest k = OK c ->
tail_nolabel k c.
Proof.
- intros. monadInv H. destruct chunk; TailNoLabel.
+ intros. destruct trap; try discriminate. monadInv H. destruct chunk; TailNoLabel.
Qed.
Remark transl_store_label:
@@ -567,6 +567,12 @@ Opaque loadind.
split. eapply agree_set_undef_mreg; eauto. congruence.
simpl; congruence.
+- (* Mload notrap *) (* isn't there a nicer way? *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
+- (* Mload notrap *)
+ inv AT. simpl in *. unfold bind in *. destruct (transl_code _ _ _) in *; discriminate.
+
- (* Mstore *)
assert (eval_addressing tge sp addr rs##args = Some a).
rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
diff --git a/x86/Asmgenproof1.v b/x86/Asmgenproof1.v
index fd88954e..7cff1047 100644
--- a/x86/Asmgenproof1.v
+++ b/x86/Asmgenproof1.v
@@ -1464,8 +1464,8 @@ Qed.
(** Translation of memory loads. *)
Lemma transl_load_correct:
- forall chunk addr args dest k c (rs: regset) m a v,
- transl_load chunk addr args dest k = OK c ->
+ forall trap chunk addr args dest k c (rs: regset) m a v,
+ transl_load trap chunk addr args dest k = OK c ->
eval_addressing ge (rs#RSP) addr (map rs (map preg_of args)) = Some a ->
Mem.loadv chunk m a = Some v ->
exists rs',
@@ -1473,7 +1473,9 @@ Lemma transl_load_correct:
/\ rs'#(preg_of dest) = v
/\ forall r, data_preg r = true -> r <> preg_of dest -> rs'#r = rs#r.
Proof.
- unfold transl_load; intros. monadInv H.
+ unfold transl_load; intros.
+ destruct trap; simpl; try discriminate.
+ monadInv H.
exploit transl_addressing_mode_correct; eauto. intro EA.
assert (EA': eval_addrmode ge x rs = a). destruct a; simpl in H1; try discriminate; inv EA; auto.
set (rs2 := nextinstr_nf (rs#(preg_of dest) <- v)).
diff --git a/x86/BTL_SEsimplify.v b/x86/BTL_SEsimplify.v
new file mode 120000
index 00000000..f190e6d5
--- /dev/null
+++ b/x86/BTL_SEsimplify.v
@@ -0,0 +1 @@
+../aarch64/BTL_SEsimplify.v \ No newline at end of file
diff --git a/x86/CSE2deps.v b/x86/CSE2deps.v
new file mode 100644
index 00000000..757966b8
--- /dev/null
+++ b/x86/CSE2deps.v
@@ -0,0 +1,38 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import BoolEqual Coqlib.
+Require Import AST Integers Floats.
+Require Import Values Memory Globalenvs Events.
+Require Import Op.
+
+Definition can_swap_accesses_ofs ofsr chunkr ofsw chunkw :=
+ (0 <=? ofsw) && (ofsw <=? (Ptrofs.modulus - largest_size_chunk))
+ && (0 <=? ofsr) && (ofsr <=? (Ptrofs.modulus - largest_size_chunk))
+ && ((ofsw + size_chunk chunkw <=? ofsr) ||
+ (ofsr + size_chunk chunkr <=? ofsw)).
+
+Definition may_overlap chunk addr args chunk' addr' args' :=
+ match addr, addr', args, args' with
+ | (Aindexed ofs), (Aindexed ofs'),
+ (base :: nil), (base' :: nil) =>
+ if peq base base'
+ then negb (can_swap_accesses_ofs ofs' chunk' ofs chunk)
+ else true
+ | (Aglobal symb ofs), (Aglobal symb' ofs'), nil, nil =>
+ if peq symb symb'
+ then negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ else false
+ | (Ainstack ofs), (Ainstack ofs'), _, _ =>
+ negb (can_swap_accesses_ofs (Ptrofs.unsigned ofs') chunk' (Ptrofs.unsigned ofs) chunk)
+ | _, _, _, _ => true
+ end.
diff --git a/x86/CSE2depsproof.v b/x86/CSE2depsproof.v
new file mode 100644
index 00000000..e181b8f4
--- /dev/null
+++ b/x86/CSE2depsproof.v
@@ -0,0 +1,339 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* David Monniaux CNRS, VERIMAG *)
+(* *)
+(* Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
+Require Import AST Linking.
+Require Import Memory Registers Op RTL Maps.
+
+Require Import Globalenvs Values.
+Require Import Linking Values Memory Globalenvs Events Smallstep.
+Require Import Registers Op RTL.
+Require Import CSE2 CSE2deps.
+Require Import Lia.
+
+Lemma ptrofs_modulus :
+ Ptrofs.modulus = if Archi.ptr64
+ then 18446744073709551616
+ else 4294967296.
+Proof.
+ reflexivity.
+Qed.
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Section STACK_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Ainstack ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Ainstack ofsr) nil = Some addrr.
+
+ Lemma stack_load_store_away1 :
+ forall RANGEW : 0 <= Ptrofs.unsigned ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= Ptrofs.unsigned ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : Ptrofs.unsigned ofsw + size_chunk chunkw <= Ptrofs.unsigned ofsr
+ \/ Ptrofs.unsigned ofsr + size_chunk chunkr <= Ptrofs.unsigned ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in *.
+
+ inv ADDRR.
+ inv ADDRW.
+
+ destruct sp; try discriminate.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsr) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i ofsw) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: try rewrite ptrofs_modulus in *.
+ all: destruct Archi.ptr64.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem stack_load_store_away :
+ can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply stack_load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+End STACK_WRITE.
+
+Section MEMORY_WRITE.
+ Variable m m2 : mem.
+ Variable chunkw chunkr : memory_chunk.
+ Variable base : val.
+
+ Variable addrw addrr valw : val.
+ Hypothesis STORE : Mem.storev chunkw m addrw valw = Some m2.
+
+ Section INDEXED_AWAY.
+ Variable ofsw ofsr : Z.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aindexed ofsw) (base :: nil) = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aindexed ofsr) (base :: nil) = Some addrr.
+
+ Lemma load_store_away1 :
+ forall RANGEW : 0 <= ofsw <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= ofsr <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : ofsw + size_chunk chunkw <= ofsr
+ \/ ofsr + size_chunk chunkr <= ofsw,
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ try change (Ptrofs.modulus - largest_size_chunk) with 4294967288 in *.
+ try change (Ptrofs.modulus - largest_size_chunk) with 18446744073709551608 in *.
+ destruct addrr ; simpl in * ; trivial.
+ unfold eval_addressing, eval_addressing32, eval_addressing64 in *.
+ destruct Archi.ptr64 eqn:PTR64; destruct base; simpl in *; try discriminate.
+ rewrite PTR64 in *.
+
+ inv ADDRR.
+ inv ADDRW.
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+
+ all: try (destruct (Ptrofs.unsigned_add_either i0
+ (Ptrofs.of_int (Int.repr ofsr))) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i0
+ (Ptrofs.of_int64 (Int64.repr ofsr))) as [OFSR | OFSR];
+ rewrite OFSR).
+ all: try (destruct (Ptrofs.unsigned_add_either i0
+ (Ptrofs.of_int (Int.repr ofsw))) as [OFSW | OFSW];
+ rewrite OFSW).
+ all: try (destruct (Ptrofs.unsigned_add_either i0
+ (Ptrofs.of_int64 (Int64.repr ofsw))) as [OFSW | OFSW];
+ rewrite OFSW).
+
+ all: unfold Ptrofs.of_int64.
+ all: unfold Ptrofs.of_int.
+
+
+ all: repeat rewrite Int.unsigned_repr by (change Int.max_unsigned with 4294967295; lia).
+ all: repeat rewrite Ptrofs.unsigned_repr by (change Ptrofs.max_unsigned with 4294967295; lia).
+ all: repeat rewrite Int64.unsigned_repr by (change Int64.max_unsigned with 18446744073709551615; lia).
+ all: repeat rewrite Ptrofs.unsigned_repr by (change Ptrofs.max_unsigned with 18446744073709551615; lia).
+
+ all: try change Ptrofs.modulus with 4294967296.
+ all: try change Ptrofs.modulus with 18446744073709551616.
+
+ all: intuition lia.
+ Qed.
+
+ Theorem load_store_away :
+ can_swap_accesses_ofs ofsr chunkr ofsw chunkw = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_away1.
+ all: tauto.
+ Qed.
+ End INDEXED_AWAY.
+
+ Section DIFFERENT_GLOBALS.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis symw symr : ident.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aglobal symw ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aglobal symr ofsr) nil = Some addrr.
+
+ Lemma ptr64_cases:
+ forall {T : Type},
+ forall b : bool,
+ forall x y : T,
+ (if b then (if b then x else y) else (if b then y else x)) = x.
+ Proof.
+ destruct b; reflexivity.
+ Qed.
+
+ (* not needed
+ Lemma bool_cases_same:
+ forall {T : Type},
+ forall b : bool,
+ forall x : T,
+ (if b then x else x) = x.
+ Proof.
+ destruct b; reflexivity.
+ Qed.
+ *)
+
+ Lemma load_store_diff_globals :
+ symw <> symr ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+ unfold eval_addressing in *.
+ simpl in *.
+ rewrite ptr64_cases in ADDRR.
+ rewrite ptr64_cases in ADDRW.
+ unfold Genv.symbol_address in *.
+ unfold Genv.find_symbol in *.
+ destruct ((Genv.genv_symb genv) ! symw) as [bw |] eqn:SYMW; inv ADDRW.
+ 2: simpl in STORE; discriminate.
+ destruct ((Genv.genv_symb genv) ! symr) as [br |] eqn:SYMR; inv ADDRR.
+ 2: reflexivity.
+ assert (br <> bw).
+ {
+ intro EQ.
+ subst br.
+ assert (symr = symw).
+ {
+ eapply Genv.genv_vars_inj; eauto.
+ }
+ congruence.
+ }
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := bw).
+ - exact STORE.
+ - left. assumption.
+ Qed.
+ End DIFFERENT_GLOBALS.
+
+ Section SAME_GLOBALS.
+ Variable ofsw ofsr : ptrofs.
+ Hypothesis sym : ident.
+ Hypothesis ADDRW : eval_addressing genv sp
+ (Aglobal sym ofsw) nil = Some addrw.
+ Hypothesis ADDRR : eval_addressing genv sp
+ (Aglobal sym ofsr) nil = Some addrr.
+
+ Lemma load_store_glob_away1 :
+ forall RANGEW : 0 <= (Ptrofs.unsigned ofsw) <= Ptrofs.modulus - largest_size_chunk,
+ forall RANGER : 0 <= (Ptrofs.unsigned ofsr) <= Ptrofs.modulus - largest_size_chunk,
+ forall SWAPPABLE : (Ptrofs.unsigned ofsw) + size_chunk chunkw <= (Ptrofs.unsigned ofsr)
+ \/ (Ptrofs.unsigned ofsr) + size_chunk chunkr <= (Ptrofs.unsigned ofsw),
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intros.
+
+ pose proof (max_size_chunk chunkr) as size_chunkr_bounded.
+ pose proof (max_size_chunk chunkw) as size_chunkw_bounded.
+ unfold largest_size_chunk in size_chunkr_bounded, size_chunkw_bounded.
+ try change (Ptrofs.modulus - largest_size_chunk) with 4294967288 in *.
+ try change (Ptrofs.modulus - largest_size_chunk) with 18446744073709551608 in *.
+ unfold eval_addressing, eval_addressing32, eval_addressing64 in *.
+
+ rewrite ptr64_cases in ADDRR.
+ rewrite ptr64_cases in ADDRW.
+ unfold Genv.symbol_address in *.
+ inv ADDRR.
+ inv ADDRW.
+ destruct (Genv.find_symbol genv sym).
+ 2: discriminate.
+
+ eapply Mem.load_store_other with (chunk := chunkw) (v := valw) (b := b).
+ exact STORE.
+ right.
+ tauto.
+ Qed.
+
+ Lemma load_store_glob_away :
+ (can_swap_accesses_ofs (Ptrofs.unsigned ofsr) chunkr (Ptrofs.unsigned ofsw) chunkw) = true ->
+ Mem.loadv chunkr m2 addrr = Mem.loadv chunkr m addrr.
+ Proof.
+ intro SWAP.
+ unfold can_swap_accesses_ofs in SWAP.
+ repeat rewrite andb_true_iff in SWAP.
+ repeat rewrite orb_true_iff in SWAP.
+ repeat rewrite Z.leb_le in SWAP.
+ apply load_store_glob_away1.
+ all: tauto.
+ Qed.
+ End SAME_GLOBALS.
+End MEMORY_WRITE.
+End SOUNDNESS.
+
+
+Section SOUNDNESS.
+ Variable F V : Type.
+ Variable genv: Genv.t F V.
+ Variable sp : val.
+
+Lemma may_overlap_sound:
+ forall m m' : mem,
+ forall chunk addr args chunk' addr' args' v a a' rs,
+ (eval_addressing genv sp addr (rs ## args)) = Some a ->
+ (eval_addressing genv sp addr' (rs ## args')) = Some a' ->
+ (may_overlap chunk addr args chunk' addr' args') = false ->
+ (Mem.storev chunk m a v) = Some m' ->
+ (Mem.loadv chunk' m' a') = (Mem.loadv chunk' m a').
+Proof.
+ intros until rs.
+ intros ADDR ADDR' OVERLAP STORE.
+ destruct addr; destruct addr'; try discriminate.
+- (* Aindexed / Aindexed *)
+ destruct args as [ | base [ | ]]. 1,3: discriminate.
+ destruct args' as [ | base' [ | ]]. 1,3: discriminate.
+ simpl in OVERLAP.
+ destruct (peq base base'). 2: discriminate.
+ subst base'.
+ destruct (can_swap_accesses_ofs z0 chunk' z chunk) eqn:SWAP.
+ 2: discriminate.
+ simpl in *.
+ eapply load_store_away; eassumption.
+- (* Aglobal / Aglobal *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ simpl in *.
+ destruct (peq i i1).
+ {
+ subst i1.
+ rewrite negb_false_iff in OVERLAP.
+ eapply load_store_glob_away; eassumption.
+ }
+ eapply load_store_diff_globals; eassumption.
+- (* Ainstack / Ainstack *)
+ destruct args. 2: discriminate.
+ destruct args'. 2: discriminate.
+ cbn in OVERLAP.
+ destruct (can_swap_accesses_ofs (Ptrofs.unsigned i0) chunk' (Ptrofs.unsigned i) chunk) eqn:SWAP.
+ 2: discriminate.
+ cbn in *.
+ eapply stack_load_store_away with (F:=F) (V:=V) (genv:=genv) (sp:=sp); eassumption.
+Qed.
+
+End SOUNDNESS.
diff --git a/x86/Conventions1.v b/x86/Conventions1.v
index a4e3b970..b6fb2620 100644
--- a/x86/Conventions1.v
+++ b/x86/Conventions1.v
@@ -15,6 +15,7 @@
Require Import Coqlib Decidableplus.
Require Import AST Machregs Locations.
+Require Import Errors.
(** * Classification of machine registers *)
@@ -26,7 +27,7 @@ Require Import AST Machregs Locations.
We follow the x86-32 and x86-64 application binary interfaces (ABI)
in our choice of callee- and caller-save registers.
*)
-
+
Definition is_callee_save (r: mreg) : bool :=
match r with
| AX | CX | DX => false
diff --git a/x86/DuplicateOpcodeHeuristic.ml b/x86/DuplicateOpcodeHeuristic.ml
new file mode 100644
index 00000000..38702e1b
--- /dev/null
+++ b/x86/DuplicateOpcodeHeuristic.ml
@@ -0,0 +1,41 @@
+(* *************************************************************)
+(* *)
+(* The Compcert verified compiler *)
+(* *)
+(* Sylvain Boulmé Grenoble-INP, VERIMAG *)
+(* David Monniaux CNRS, VERIMAG *)
+(* Cyril Six Kalray *)
+(* *)
+(* Copyright Kalray. Copyright VERIMAG. All rights reserved. *)
+(* This file is distributed under the terms of the INRIA *)
+(* Non-Commercial License Agreement. *)
+(* *)
+(* *************************************************************)
+
+(* open Camlcoq *)
+open Op
+open Integers
+
+let opcode_heuristic code cond ifso ifnot is_loop_header =
+ match cond with
+ | Ccompimm (c, n) | Ccompuimm (c, n) -> if n == Integers.Int.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccomplimm (c, n) | Ccompluimm (c, n) -> if n == Integers.Int64.zero then (match c with
+ | Clt | Cle -> Some false
+ | Cgt | Cge -> Some true
+ | _ -> None
+ ) else None
+ | Ccompf c | Ccompfs c -> (match c with
+ | Ceq -> Some false
+ | Cne -> Some true
+ | _ -> None
+ )
+ | Cnotcompf c | Cnotcompfs c -> (match c with
+ | Ceq -> Some true
+ | Cne -> Some false
+ | _ -> None
+ )
+ | _ -> None
diff --git a/x86/ExpansionOracle.ml b/x86/ExpansionOracle.ml
new file mode 120000
index 00000000..ee2674bf
--- /dev/null
+++ b/x86/ExpansionOracle.ml
@@ -0,0 +1 @@
+../aarch64/ExpansionOracle.ml \ No newline at end of file
diff --git a/x86/Machregsaux.ml b/x86/Machregsaux.ml
index a48749a5..840943e7 100644
--- a/x86/Machregsaux.ml
+++ b/x86/Machregsaux.ml
@@ -13,3 +13,8 @@
(** Auxiliary functions on machine registers *)
let is_scratch_register r = false
+
+let class_of_type = function
+ | AST.Tint | AST.Tlong -> 0
+ | AST.Tfloat | AST.Tsingle -> 1
+ | AST.Tany32 | AST.Tany64 -> assert false
diff --git a/x86/Machregsaux.mli b/x86/Machregsaux.mli
index f3d52849..01b0f9fd 100644
--- a/x86/Machregsaux.mli
+++ b/x86/Machregsaux.mli
@@ -13,3 +13,5 @@
(** Auxiliary functions on machine registers *)
val is_scratch_register: string -> bool
+
+val class_of_type: AST.typ -> int
diff --git a/x86/Op.v b/x86/Op.v
index 16d75426..caa63235 100644
--- a/x86/Op.v
+++ b/x86/Op.v
@@ -742,6 +742,42 @@ Proof with (try exact I; try reflexivity).
unfold Val.select. destruct (eval_condition c vl m). apply Val.normalize_type. exact I.
Qed.
+
+Definition is_trapping_op (op : operation) :=
+ match op with
+ | Odiv | Odivl | Odivu | Odivlu
+ | Omod | Omodl | Omodu | Omodlu
+ | Oshrximm _ | Oshrxlimm _
+ | Ointoffloat
+ | Ointofsingle
+ | Olongoffloat
+ | Olongofsingle
+ | Osingleofint
+ | Osingleoflong
+ | Ofloatofint
+ | Ofloatoflong
+ | Olea _ | Oleal _ (* TODO this is suboptimal *) => true
+ | _ => false
+ end.
+
+Definition args_of_operation op :=
+ if eq_operation op Omove
+ then 1%nat
+ else List.length (fst (type_of_operation op)).
+
+Lemma is_trapping_op_sound:
+ forall op vl sp m,
+ is_trapping_op op = false ->
+ (List.length vl) = args_of_operation op ->
+ eval_operation genv sp op vl m <> None.
+Proof.
+ unfold args_of_operation.
+ destruct op; destruct eq_operation; intros; simpl in *; try congruence.
+ all: try (destruct vl as [ | vh1 vl1]; try discriminate).
+ all: try (destruct vl1 as [ | vh2 vl2]; try discriminate).
+ all: try (destruct vl2 as [ | vh3 vl3]; try discriminate).
+ all: try (destruct vl3 as [ | vh4 vl4]; try discriminate).
+Qed.
End SOUNDNESS.
(** * Manipulating and transforming operations *)
@@ -963,7 +999,7 @@ Definition is_trivial_op (op: operation) : bool :=
(** Operations that depend on the memory state. *)
-Definition condition_depends_on_memory (c: condition) : bool :=
+Definition cond_depends_on_memory (c: condition) : bool :=
match c with
| Ccompu _ => negb Archi.ptr64
| Ccompuimm _ _ => negb Archi.ptr64
@@ -974,14 +1010,14 @@ Definition condition_depends_on_memory (c: condition) : bool :=
Definition op_depends_on_memory (op: operation) : bool :=
match op with
- | Ocmp c => condition_depends_on_memory c
- | Osel c ty => condition_depends_on_memory c
+ | Ocmp c => cond_depends_on_memory c
+ | Osel c ty => cond_depends_on_memory c
| _ => false
end.
-Lemma condition_depends_on_memory_correct:
+Lemma cond_depends_on_memory_correct:
forall c args m1 m2,
- condition_depends_on_memory c = false ->
+ cond_depends_on_memory c = false ->
eval_condition c args m1 = eval_condition c args m2.
Proof.
intros until m2.
@@ -995,12 +1031,36 @@ Lemma op_depends_on_memory_correct:
eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
Proof.
intros until m2. destruct op; simpl; try congruence; intros C.
-- f_equal; f_equal; apply condition_depends_on_memory_correct; auto.
+- f_equal; f_equal; apply cond_depends_on_memory_correct; auto.
- destruct args; auto. destruct args; auto.
- rewrite (condition_depends_on_memory_correct c args m1 m2 C).
+ rewrite (cond_depends_on_memory_correct c args m1 m2 C).
auto.
Qed.
+Lemma cond_valid_pointer_eq:
+ forall cond args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_condition cond args m1 = eval_condition cond args m2.
+Proof.
+ intros until m2. intro MEM. destruct cond eqn:COND; simpl; try congruence.
+ all: repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
+Lemma op_valid_pointer_eq:
+ forall (F V: Type) (ge: Genv.t F V) sp op args m1 m2,
+ (forall b z, Mem.valid_pointer m1 b z = Mem.valid_pointer m2 b z) ->
+ eval_operation ge sp op args m1 = eval_operation ge sp op args m2.
+Proof.
+ intros until m2. destruct op eqn:OP; simpl; try congruence.
+ - intros MEM; destruct cond; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+ - intro MEM; destruct c; simpl; try congruence;
+ repeat (destruct args; simpl; try congruence);
+ erewrite cmpu_bool_valid_pointer_eq || erewrite cmplu_bool_valid_pointer_eq; eauto.
+Qed.
+
(** Global variables mentioned in an operation or addressing mode *)
Definition globals_addressing (addr: addressing) : list ident :=
@@ -1199,6 +1259,21 @@ Proof.
unfold eval_addressing; intros. destruct Archi.ptr64; eauto using eval_addressing32_inj, eval_addressing64_inj.
Qed.
+Lemma eval_addressing_inj_none:
+ forall addr sp1 vl1 sp2 vl2,
+ (forall id ofs,
+ In id (globals_addressing addr) ->
+ Val.inject f (Genv.symbol_address ge1 id ofs) (Genv.symbol_address ge2 id ofs)) ->
+ Val.inject f sp1 sp2 ->
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing ge1 sp1 addr vl1 = None ->
+ eval_addressing ge2 sp2 addr vl2 = None.
+Proof.
+ intros until vl2. intros Hglobal Hinjsp Hinjvl.
+ destruct addr; simpl in *;
+ inv Hinjvl; trivial; try discriminate; inv H0; trivial; try discriminate; inv H2; trivial; try discriminate.
+Qed.
+
Lemma eval_operation_inj:
forall op sp1 vl1 sp2 vl2 v1,
(forall id ofs,
@@ -1425,6 +1500,19 @@ Proof.
destruct H1 as [v2 [A B]]. exists v2; split; auto. rewrite val_inject_lessdef; auto.
Qed.
+Lemma eval_addressing_lessdef_none:
+ forall sp addr vl1 vl2,
+ Val.lessdef_list vl1 vl2 ->
+ eval_addressing genv sp addr vl1 = None ->
+ eval_addressing genv sp addr vl2 = None.
+Proof.
+ intros until vl2. intros Hlessdef Heval1.
+ destruct addr; simpl in *;
+ inv Hlessdef; trivial; try discriminate;
+ inv H0; trivial; try discriminate;
+ inv H2; trivial; try discriminate.
+Qed.
+
End EVAL_LESSDEF.
(** Compatibility of the evaluation functions with memory injections. *)
@@ -1477,6 +1565,19 @@ Proof.
econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
Qed.
+Lemma eval_addressing_inject_none:
+ forall addr vl1 vl2,
+ Val.inject_list f vl1 vl2 ->
+ eval_addressing genv (Vptr sp1 Ptrofs.zero) addr vl1 = None ->
+ eval_addressing genv (Vptr sp2 Ptrofs.zero) (shift_stack_addressing delta addr) vl2 = None.
+Proof.
+ intros.
+ rewrite eval_shift_stack_addressing.
+ eapply eval_addressing_inj_none with (sp1 := Vptr sp1 Ptrofs.zero); eauto.
+ intros. apply symbol_address_inject.
+ econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
+Qed.
+
Lemma eval_operation_inject:
forall op vl1 vl2 v1 m1 m2,
Val.inject_list f vl1 vl2 ->
diff --git a/x86/PrepassSchedulingOracle.ml b/x86/PrepassSchedulingOracle.ml
new file mode 100644
index 00000000..a8ba6720
--- /dev/null
+++ b/x86/PrepassSchedulingOracle.ml
@@ -0,0 +1,4 @@
+(* Do not do anything *)
+let schedule_sequence seqa btl (live_regs_entry : Registers.Regset.t)
+ (typing : RTLtyping.regenv) reference =
+ None
diff --git a/x86/SelectLong.vp b/x86/SelectLong.vp
index b213e23f..4f9fb518 100644
--- a/x86/SelectLong.vp
+++ b/x86/SelectLong.vp
@@ -16,7 +16,7 @@ Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats.
Require Import Op CminorSel.
-Require Import SelectOp SplitLong.
+Require Import OpHelpers SelectOp SplitLong.
Local Open Scope cminorsel_scope.
Local Open Scope string_scope.
diff --git a/x86/SelectLongproof.v b/x86/SelectLongproof.v
index 3bef632d..f008f39e 100644
--- a/x86/SelectLongproof.v
+++ b/x86/SelectLongproof.v
@@ -16,6 +16,7 @@ Require Import String Coqlib Maps Integers Floats Errors.
Require Archi.
Require Import AST Values Memory Globalenvs Events.
Require Import Cminor Op CminorSel.
+Require Import OpHelpers OpHelpersproof.
Require Import SelectOp SelectOpproof SplitLong SplitLongproof.
Require Import SelectLong.
diff --git a/x86/SelectOp.vp b/x86/SelectOp.vp
index 31be8c32..2a09207b 100644
--- a/x86/SelectOp.vp
+++ b/x86/SelectOp.vp
@@ -40,6 +40,7 @@ Require Import Coqlib.
Require Import Compopts.
Require Import AST Integers Floats Builtins.
Require Import Op CminorSel.
+Require Import OpHelpers.
Require Archi.
Local Open Scope cminorsel_scope.
@@ -502,7 +503,7 @@ Definition intuoffloat (e: expr) :=
if Archi.splitlong then
Elet e
(Elet (Eop (Ofloatconst (Float.of_intu Float.ox8000_0000)) Enil)
- (Econdition (CEcond (Ccompf Clt) (Eletvar 1 ::: Eletvar 0 ::: Enil))
+ (Econdition (CEcond (Ccompf Clt) None (Eletvar 1 ::: Eletvar 0 ::: Enil))
(intoffloat (Eletvar 1))
(addimm Float.ox8000_0000 (intoffloat (subf (Eletvar 1) (Eletvar 0))))))%nat
else
@@ -515,7 +516,7 @@ Nondetfunction floatofintu (e: expr) :=
if Archi.splitlong then
let f := Eop (Ofloatconst (Float.of_intu Float.ox8000_0000)) Enil in
Elet e
- (Econdition (CEcond (Ccompuimm Clt Float.ox8000_0000) (Eletvar O ::: Enil))
+ (Econdition (CEcond (Ccompuimm Clt Float.ox8000_0000) None (Eletvar O ::: Enil))
(floatofint (Eletvar O))
(addf (floatofint (addimm (Int.neg Float.ox8000_0000) (Eletvar O))) f))
else
@@ -576,6 +577,13 @@ Nondetfunction builtin_arg (e: expr) :=
| _ => BA e
end.
+(* floats *)
+Definition divf_base (e1: expr) (e2: expr) :=
+ Eop Odivf (e1 ::: e2 ::: Enil).
+
+Definition divfs_base (e1: expr) (e2: expr) :=
+ Eop Odivfs (e1 ::: e2 ::: Enil).
+
(** Platform-specific known builtins *)
Definition platform_builtin (b: platform_builtin) (args: exprlist) : option expr :=
diff --git a/x86/SelectOpproof.v b/x86/SelectOpproof.v
index d8ab32a4..c43beb56 100644
--- a/x86/SelectOpproof.v
+++ b/x86/SelectOpproof.v
@@ -17,6 +17,8 @@ Require Import AST Integers Floats.
Require Import Values Memory Builtins Globalenvs.
Require Import Cminor Op CminorSel.
Require Import SelectOp.
+Require Import OpHelpers.
+Require Import OpHelpersproof.
Local Open Scope cminorsel_scope.
@@ -68,8 +70,10 @@ Ltac TrivialExists :=
(** * Correctness of the smart constructors *)
Section CMCONSTR.
-
-Variable ge: genv.
+Variable prog: program.
+Variable hf: helper_functions.
+Hypothesis HELPERS: helper_functions_declared prog hf.
+Let ge := Genv.globalenv prog.
Variable sp: val.
Variable e: env.
Variable m: mem.
@@ -1012,6 +1016,27 @@ Proof.
- constructor; auto.
Qed.
+(* floating-point division without HELPERS *)
+Theorem eval_divf_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divf_base a b) v /\ Val.lessdef (Val.divf x y) v.
+Proof.
+ intros; unfold divf_base.
+ TrivialExists.
+Qed.
+
+Theorem eval_divfs_base:
+ forall le a b x y,
+ eval_expr ge sp e m le a x ->
+ eval_expr ge sp e m le b y ->
+ exists v, eval_expr ge sp e m le (divfs_base a b) v /\ Val.lessdef (Val.divfs x y) v.
+Proof.
+ intros; unfold divfs_base.
+ TrivialExists.
+Qed.
+
(** Platform-specific known builtins *)
Theorem eval_platform_builtin:
diff --git a/x86/TargetPrinter.ml b/x86/TargetPrinter.ml
index 5bc2be1c..00e70f65 100644
--- a/x86/TargetPrinter.ml
+++ b/x86/TargetPrinter.ml
@@ -134,7 +134,9 @@ module ELF_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".section .rodata" i
@@ -166,7 +168,44 @@ module ELF_System : SYSTEM =
let print_var_info = elf_print_var_info
- let print_epilogue _ = ()
+ let print_atexit oc to_be_called =
+ if Archi.ptr64
+ then
+ begin
+ fprintf oc " leaq %s(%%rip), %%rdi\n" to_be_called;
+ fprintf oc " jmp atexit\n"
+ end
+ else
+ begin
+ fprintf oc " pushl $%s\n" to_be_called;
+ fprintf oc " call atexit\n";
+ fprintf oc " addl $4, %%esp\n";
+ fprintf oc " ret\n"
+ end
+
+ let x86_profiling_stub oc nr_items
+ profiling_id_table_name
+ profiling_counter_table_name =
+ if Archi.ptr64
+ then
+ begin
+ fprintf oc " leaq %s(%%rip), %%rdx\n" profiling_counter_table_name;
+ fprintf oc " leaq %s(%%rip), %%rsi\n" profiling_id_table_name;
+ fprintf oc " movl $%d, %%edi\n" nr_items;
+ fprintf oc " jmp %s\n" profiling_write_table_helper
+ end
+ else
+ begin
+ fprintf oc " pushl $%s\n" profiling_counter_table_name;
+ fprintf oc " pushl $%s\n" profiling_id_table_name;
+ fprintf oc " pushl $%d\n" nr_items;
+ fprintf oc " call %s\n" profiling_write_table_helper ;
+ fprintf oc " addl $12, %%esp\n";
+ fprintf oc " ret\n"
+ end;;
+
+ let print_epilogue oc =
+ print_profiling_epilogue elf_text_print_fun_info (Init_atexit print_atexit) x86_profiling_stub oc;;
let print_comm_decl oc name sz al =
fprintf oc " .comm %a, %s, %d\n" symbol name (Z.to_string sz) al
@@ -196,7 +235,9 @@ module MacOS_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".const" ~reloc:".const_data" i
@@ -261,7 +302,9 @@ module Cygwin_System : SYSTEM =
let name_of_section = function
| Section_text -> ".text"
- | Section_data i | Section_small_data i ->
+ | Section_data(i, true) ->
+ failwith "_Thread_local unsupported on this platform"
+ | Section_data(i, false) | Section_small_data i ->
variable_section ~sec:".data" ~bss:".bss" i
| Section_const i | Section_small_const i ->
variable_section ~sec:".section .rdata,\"dr\"" i
@@ -409,8 +452,28 @@ module Target(System: SYSTEM):TARGET =
fprintf oc "%a(%%rip)" label lbl
end
-
-
+ let print_profiling_logger oc id kind =
+ assert (kind >= 0);
+ assert (kind <= 1);
+ let ofs = profiling_offset id kind in
+ if Archi.ptr64
+ then
+ begin
+ fprintf oc "%s profiling %a %d: atomic increment\n" comment
+ Profilingaux.pp_id id kind;
+ fprintf oc " lock addq $1, %s+%d(%%rip)\n"
+ profiling_counter_table_name ofs
+ end
+ else
+ begin
+ fprintf oc "%s begin profiling %a %d: increment\n" comment
+ Profilingaux.pp_id id kind;
+ fprintf oc " addl $1, %s+%d\n" profiling_counter_table_name ofs;
+ fprintf oc " adcl $1, %s+%d\n" profiling_counter_table_name (ofs+4);
+ fprintf oc "%s end profiling %a %d: increment\n" comment
+ Profilingaux.pp_id id kind;
+ end
+
(* Printing of instructions *)
(* Reminder on X86 assembly syntaxes:
@@ -850,6 +913,8 @@ module Target(System: SYSTEM):TARGET =
fprintf oc "%s begin inline assembly\n\t" comment;
print_inline_asm preg_asm oc (camlstring_of_coqstring txt) sg args res;
fprintf oc "%s end inline assembly\n" comment
+ | EF_profiling(id, coq_kind) ->
+ print_profiling_logger oc id (Z.to_int coq_kind)
| _ ->
assert false
end
diff --git a/x86/ValueAOp.v b/x86/ValueAOp.v
index d0b8427a..e5584b6a 100644
--- a/x86/ValueAOp.v
+++ b/x86/ValueAOp.v
@@ -261,6 +261,25 @@ Proof.
apply of_optbool_sound. eapply eval_static_condition_sound; eauto.
apply select_sound; auto. eapply eval_static_condition_sound; eauto.
Qed.
-
+(*
+Theorem eval_static_addressing_sound_none:
+ forall addr vargs aargs,
+ eval_addressing ge (Vptr sp Ptrofs.zero) addr vargs = None ->
+ list_forall2 (vmatch bc) vargs aargs ->
+ (eval_static_addressing addr aargs) = Vbot.
+Proof.
+ unfold eval_addressing, eval_static_addressing.
+ intros until aargs. intros Heval_none Hlist.
+ destruct (Archi.ptr64).
+ inv Hlist.
+ destruct addr; trivial; discriminate.
+ inv H0.
+ destruct addr; trivial; try discriminate. simpl in *.
+ inv H2.
+ destruct addr; trivial; discriminate.
+ inv H3;
+ destruct addr; trivial; discriminate.
+Qed.
+*)
End SOUNDNESS.
diff --git a/x86_32/Archi.v b/x86_32/Archi.v
index 0a7f365b..b3bd434c 100644
--- a/x86_32/Archi.v
+++ b/x86_32/Archi.v
@@ -68,3 +68,4 @@ Global Opaque ptr64 big_endian splitlong
fma_order fma_invalid_mul_is_nan
float_of_single_preserves_sNaN.
+Definition has_notrap_loads := false.
diff --git a/x86_64/Archi.v b/x86_64/Archi.v
index ed6dc317..99e4f795 100644
--- a/x86_64/Archi.v
+++ b/x86_64/Archi.v
@@ -67,3 +67,5 @@ Global Opaque ptr64 big_endian splitlong
default_nan_32 choose_nan_32
fma_order fma_invalid_mul_is_nan
float_of_single_preserves_sNaN.
+
+Definition has_notrap_loads := false.